]> git.sesse.net Git - bcachefs-tools-debian/blob - libbcachefs/opts.h
87114277876356d966bb64c5d7256ac3c6a28681
[bcachefs-tools-debian] / libbcachefs / opts.h
1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef _BCACHEFS_OPTS_H
3 #define _BCACHEFS_OPTS_H
4
5 #include <linux/bug.h>
6 #include <linux/log2.h>
7 #include <linux/string.h>
8 #include <linux/sysfs.h>
9 #include "bcachefs_format.h"
10
11 extern const char * const bch2_error_actions[];
12 extern const char * const bch2_sb_features[];
13 extern const char * const bch2_sb_compat[];
14 extern const char * const bch2_btree_ids[];
15 extern const char * const bch2_csum_types[];
16 extern const char * const bch2_csum_opts[];
17 extern const char * const bch2_compression_types[];
18 extern const char * const bch2_compression_opts[];
19 extern const char * const bch2_str_hash_types[];
20 extern const char * const bch2_str_hash_opts[];
21 extern const char * const bch2_data_types[];
22 extern const char * const bch2_cache_replacement_policies[];
23 extern const char * const bch2_member_states[];
24 extern const char * const bch2_d_types[];
25
26 static inline const char *bch2_d_type_str(unsigned d_type)
27 {
28         return (d_type < BCH_DT_MAX ? bch2_d_types[d_type] : NULL) ?: "(bad d_type)";
29 }
30
31 /*
32  * Mount options; we also store defaults in the superblock.
33  *
34  * Also exposed via sysfs: if an option is writeable, and it's also stored in
35  * the superblock, changing it via sysfs (currently? might change this) also
36  * updates the superblock.
37  *
38  * We store options as signed integers, where -1 means undefined. This means we
39  * can pass the mount options to bch2_fs_alloc() as a whole struct, and then only
40  * apply the options from that struct that are defined.
41  */
42
43 /* dummy option, for options that aren't stored in the superblock */
44 LE64_BITMASK(NO_SB_OPT,         struct bch_sb, flags[0], 0, 0);
45
46 /* When can be set: */
47 enum opt_mode {
48         OPT_FS          = (1 << 0),     /* Filesystem option */
49         OPT_DEVICE      = (1 << 1),     /* Device option */
50         OPT_INODE       = (1 << 2),     /* Inode option */
51         OPT_FORMAT      = (1 << 3),     /* May be specified at format time */
52         OPT_MOUNT       = (1 << 4),     /* May be specified at mount time */
53         OPT_RUNTIME     = (1 << 5),     /* May be specified at runtime */
54 };
55
56 enum opt_type {
57         BCH_OPT_BOOL,
58         BCH_OPT_UINT,
59         BCH_OPT_SECTORS,
60         BCH_OPT_STR,
61         BCH_OPT_FN,
62 };
63
64 /**
65  * x(name, shortopt, type, in mem type, mode, sb_opt)
66  *
67  * @name        - name of mount option, sysfs attribute, and struct bch_opts
68  *                member
69  *
70  * @mode        - when opt may be set
71  *
72  * @sb_option   - name of corresponding superblock option
73  *
74  * @type        - one of OPT_BOOL, OPT_UINT, OPT_STR
75  */
76
77 /*
78  * XXX: add fields for
79  *  - default value
80  *  - helptext
81  */
82
83 #ifdef __KERNEL__
84 #define RATELIMIT_ERRORS_DEFAULT true
85 #else
86 #define RATELIMIT_ERRORS_DEFAULT false
87 #endif
88
89 #define BCH_OPTS()                                                      \
90         x(block_size,                   u16,                            \
91           OPT_FS|OPT_FORMAT,                                            \
92           OPT_SECTORS(1, 128),                                          \
93           BCH_SB_BLOCK_SIZE,            8,                              \
94           "size",       NULL)                                           \
95         x(btree_node_size,              u16,                            \
96           OPT_FS|OPT_FORMAT,                                            \
97           OPT_SECTORS(1, 512),                                          \
98           BCH_SB_BTREE_NODE_SIZE,       512,                            \
99           "size",       "Btree node size, default 256k")                \
100         x(errors,                       u8,                             \
101           OPT_FS|OPT_FORMAT|OPT_MOUNT|OPT_RUNTIME,                      \
102           OPT_STR(bch2_error_actions),                                  \
103           BCH_SB_ERROR_ACTION,          BCH_ON_ERROR_ro,                \
104           NULL,         "Action to take on filesystem error")           \
105         x(metadata_replicas,            u8,                             \
106           OPT_FS|OPT_FORMAT|OPT_MOUNT|OPT_RUNTIME,                      \
107           OPT_UINT(1, BCH_REPLICAS_MAX),                                \
108           BCH_SB_META_REPLICAS_WANT,    1,                              \
109           "#",          "Number of metadata replicas")                  \
110         x(data_replicas,                u8,                             \
111           OPT_FS|OPT_INODE|OPT_FORMAT|OPT_MOUNT|OPT_RUNTIME,            \
112           OPT_UINT(1, BCH_REPLICAS_MAX),                                \
113           BCH_SB_DATA_REPLICAS_WANT,    1,                              \
114           "#",          "Number of data replicas")                      \
115         x(metadata_replicas_required, u8,                               \
116           OPT_FS|OPT_FORMAT|OPT_MOUNT,                                  \
117           OPT_UINT(1, BCH_REPLICAS_MAX),                                \
118           BCH_SB_META_REPLICAS_REQ,     1,                              \
119           "#",          NULL)                                           \
120         x(data_replicas_required,       u8,                             \
121           OPT_FS|OPT_FORMAT|OPT_MOUNT,                                  \
122           OPT_UINT(1, BCH_REPLICAS_MAX),                                \
123           BCH_SB_DATA_REPLICAS_REQ,     1,                              \
124           "#",          NULL)                                           \
125         x(metadata_checksum,            u8,                             \
126           OPT_FS|OPT_FORMAT|OPT_MOUNT|OPT_RUNTIME,                      \
127           OPT_STR(bch2_csum_opts),                                      \
128           BCH_SB_META_CSUM_TYPE,        BCH_CSUM_OPT_crc32c,            \
129           NULL,         NULL)                                           \
130         x(data_checksum,                u8,                             \
131           OPT_FS|OPT_INODE|OPT_FORMAT|OPT_MOUNT|OPT_RUNTIME,            \
132           OPT_STR(bch2_csum_opts),                                      \
133           BCH_SB_DATA_CSUM_TYPE,        BCH_CSUM_OPT_crc32c,            \
134           NULL,         NULL)                                           \
135         x(compression,                  u8,                             \
136           OPT_FS|OPT_INODE|OPT_FORMAT|OPT_MOUNT|OPT_RUNTIME,            \
137           OPT_STR(bch2_compression_opts),                               \
138           BCH_SB_COMPRESSION_TYPE,      BCH_COMPRESSION_OPT_none,       \
139           NULL,         NULL)                                           \
140         x(background_compression,       u8,                             \
141           OPT_FS|OPT_INODE|OPT_FORMAT|OPT_MOUNT|OPT_RUNTIME,            \
142           OPT_STR(bch2_compression_opts),                               \
143           BCH_SB_BACKGROUND_COMPRESSION_TYPE,BCH_COMPRESSION_OPT_none,  \
144           NULL,         NULL)                                           \
145         x(str_hash,                     u8,                             \
146           OPT_FS|OPT_FORMAT|OPT_MOUNT|OPT_RUNTIME,                      \
147           OPT_STR(bch2_str_hash_opts),                                  \
148           BCH_SB_STR_HASH_TYPE,         BCH_STR_HASH_OPT_siphash,       \
149           NULL,         "Hash function for directory entries and xattrs")\
150         x(metadata_target,              u16,                            \
151           OPT_FS|OPT_INODE|OPT_FORMAT|OPT_MOUNT|OPT_RUNTIME,            \
152           OPT_FN(bch2_opt_target),                                      \
153           BCH_SB_METADATA_TARGET,       0,                              \
154           "(target)",   "Device or disk group for metadata writes")     \
155         x(foreground_target,            u16,                            \
156           OPT_FS|OPT_INODE|OPT_FORMAT|OPT_MOUNT|OPT_RUNTIME,            \
157           OPT_FN(bch2_opt_target),                                      \
158           BCH_SB_FOREGROUND_TARGET,     0,                              \
159           "(target)",   "Device or disk group for foreground writes")   \
160         x(background_target,            u16,                            \
161           OPT_FS|OPT_INODE|OPT_FORMAT|OPT_MOUNT|OPT_RUNTIME,            \
162           OPT_FN(bch2_opt_target),                                      \
163           BCH_SB_BACKGROUND_TARGET,     0,                              \
164           "(target)",   "Device or disk group to move data to in the background")\
165         x(promote_target,               u16,                            \
166           OPT_FS|OPT_INODE|OPT_FORMAT|OPT_MOUNT|OPT_RUNTIME,            \
167           OPT_FN(bch2_opt_target),                                      \
168           BCH_SB_PROMOTE_TARGET,        0,                              \
169           "(target)",   "Device or disk group to promote data to on read")\
170         x(erasure_code,                 u16,                            \
171           OPT_FS|OPT_INODE|OPT_FORMAT|OPT_MOUNT|OPT_RUNTIME,            \
172           OPT_BOOL(),                                                   \
173           BCH_SB_ERASURE_CODE,          false,                          \
174           NULL,         "Enable erasure coding (DO NOT USE YET)")       \
175         x(inodes_32bit,                 u8,                             \
176           OPT_FS|OPT_FORMAT|OPT_MOUNT|OPT_RUNTIME,                      \
177           OPT_BOOL(),                                                   \
178           BCH_SB_INODE_32BIT,           true,                           \
179           NULL,         "Constrain inode numbers to 32 bits")           \
180         x(shard_inode_numbers,          u8,                             \
181           OPT_FS|OPT_FORMAT|OPT_MOUNT|OPT_RUNTIME,                      \
182           OPT_BOOL(),                                                   \
183           BCH_SB_SHARD_INUMS,           true,                           \
184           NULL,         "Shard new inode numbers by CPU id")            \
185         x(inodes_use_key_cache, u8,                                     \
186           OPT_FS|OPT_FORMAT|OPT_MOUNT,                                  \
187           OPT_BOOL(),                                                   \
188           BCH_SB_INODES_USE_KEY_CACHE,  true,                           \
189           NULL,         "Use the btree key cache for the inodes btree") \
190         x(btree_node_mem_ptr_optimization, u8,                          \
191           OPT_FS|OPT_MOUNT|OPT_RUNTIME,                                 \
192           OPT_BOOL(),                                                   \
193           NO_SB_OPT,                    true,                           \
194           NULL,         "Stash pointer to in memory btree node in btree ptr")\
195         x(gc_reserve_percent,           u8,                             \
196           OPT_FS|OPT_FORMAT|OPT_MOUNT|OPT_RUNTIME,                      \
197           OPT_UINT(5, 21),                                              \
198           BCH_SB_GC_RESERVE,            8,                              \
199           "%",          "Percentage of disk space to reserve for copygc")\
200         x(gc_reserve_bytes,             u64,                            \
201           OPT_FS|OPT_FORMAT|OPT_MOUNT|OPT_RUNTIME,                      \
202           OPT_SECTORS(0, U64_MAX),                                      \
203           BCH_SB_GC_RESERVE_BYTES,      0,                              \
204           "%",          "Amount of disk space to reserve for copygc\n"  \
205                         "Takes precedence over gc_reserve_percent if set")\
206         x(root_reserve_percent,         u8,                             \
207           OPT_FS|OPT_FORMAT|OPT_MOUNT,                                  \
208           OPT_UINT(0, 100),                                             \
209           BCH_SB_ROOT_RESERVE,          0,                              \
210           "%",          "Percentage of disk space to reserve for superuser")\
211         x(wide_macs,                    u8,                             \
212           OPT_FS|OPT_FORMAT|OPT_MOUNT|OPT_RUNTIME,                      \
213           OPT_BOOL(),                                                   \
214           BCH_SB_128_BIT_MACS,          false,                          \
215           NULL,         "Store full 128 bits of cryptographic MACs, instead of 80")\
216         x(inline_data,                  u8,                             \
217           OPT_FS|OPT_MOUNT|OPT_RUNTIME,                                 \
218           OPT_BOOL(),                                                   \
219           NO_SB_OPT,                    true,                           \
220           NULL,         "Enable inline data extents")                   \
221         x(acl,                          u8,                             \
222           OPT_FS|OPT_FORMAT|OPT_MOUNT,                                  \
223           OPT_BOOL(),                                                   \
224           BCH_SB_POSIX_ACL,             true,                           \
225           NULL,         "Enable POSIX acls")                            \
226         x(usrquota,                     u8,                             \
227           OPT_FS|OPT_FORMAT|OPT_MOUNT,                                  \
228           OPT_BOOL(),                                                   \
229           BCH_SB_USRQUOTA,              false,                          \
230           NULL,         "Enable user quotas")                           \
231         x(grpquota,                     u8,                             \
232           OPT_FS|OPT_FORMAT|OPT_MOUNT,                                  \
233           OPT_BOOL(),                                                   \
234           BCH_SB_GRPQUOTA,              false,                          \
235           NULL,         "Enable group quotas")                          \
236         x(prjquota,                     u8,                             \
237           OPT_FS|OPT_FORMAT|OPT_MOUNT,                                  \
238           OPT_BOOL(),                                                   \
239           BCH_SB_PRJQUOTA,              false,                          \
240           NULL,         "Enable project quotas")                        \
241         x(degraded,                     u8,                             \
242           OPT_FS|OPT_MOUNT,                                             \
243           OPT_BOOL(),                                                   \
244           NO_SB_OPT,                    false,                          \
245           NULL,         "Allow mounting in degraded mode")              \
246         x(very_degraded,                u8,                             \
247           OPT_FS|OPT_MOUNT,                                             \
248           OPT_BOOL(),                                                   \
249           NO_SB_OPT,                    false,                          \
250           NULL,         "Allow mounting in when data will be missing")  \
251         x(discard,                      u8,                             \
252           OPT_FS|OPT_MOUNT|OPT_DEVICE,                                  \
253           OPT_BOOL(),                                                   \
254           NO_SB_OPT,                    false,                          \
255           NULL,         "Enable discard/TRIM support")                  \
256         x(verbose,                      u8,                             \
257           OPT_FS|OPT_MOUNT,                                             \
258           OPT_BOOL(),                                                   \
259           NO_SB_OPT,                    false,                          \
260           NULL,         "Extra debugging information during mount/recovery")\
261         x(journal_flush_delay,          u32,                            \
262           OPT_FS|OPT_MOUNT|OPT_RUNTIME,                                 \
263           OPT_UINT(0, U32_MAX),                                         \
264           BCH_SB_JOURNAL_FLUSH_DELAY,   1000,                           \
265           NULL,         "Delay in milliseconds before automatic journal commits")\
266         x(journal_flush_disabled,       u8,                             \
267           OPT_FS|OPT_MOUNT|OPT_RUNTIME,                                 \
268           OPT_BOOL(),                                                   \
269           BCH_SB_JOURNAL_FLUSH_DISABLED,false,                          \
270           NULL,         "Disable journal flush on sync/fsync\n"         \
271                         "If enabled, writes can be lost, but only since the\n"\
272                         "last journal write (default 1 second)")        \
273         x(journal_reclaim_delay,        u32,                            \
274           OPT_FS|OPT_MOUNT|OPT_RUNTIME,                                 \
275           OPT_UINT(0, U32_MAX),                                         \
276           BCH_SB_JOURNAL_RECLAIM_DELAY, 100,                            \
277           NULL,         "Delay in milliseconds before automatic journal reclaim")\
278         x(fsck,                         u8,                             \
279           OPT_FS|OPT_MOUNT,                                             \
280           OPT_BOOL(),                                                   \
281           NO_SB_OPT,                    false,                          \
282           NULL,         "Run fsck on mount")                            \
283         x(fix_errors,                   u8,                             \
284           OPT_FS|OPT_MOUNT,                                             \
285           OPT_BOOL(),                                                   \
286           NO_SB_OPT,                    false,                          \
287           NULL,         "Fix errors during fsck without asking")        \
288         x(ratelimit_errors,             u8,                             \
289           OPT_FS|OPT_MOUNT,                                             \
290           OPT_BOOL(),                                                   \
291           NO_SB_OPT,                    RATELIMIT_ERRORS_DEFAULT,       \
292           NULL,         "Ratelimit error messages during fsck")         \
293         x(nochanges,                    u8,                             \
294           OPT_FS|OPT_MOUNT,                                             \
295           OPT_BOOL(),                                                   \
296           NO_SB_OPT,                    false,                          \
297           NULL,         "Super read only mode - no writes at all will be issued,\n"\
298                         "even if we have to replay the journal")        \
299         x(norecovery,                   u8,                             \
300           OPT_FS|OPT_MOUNT,                                             \
301           OPT_BOOL(),                                                   \
302           NO_SB_OPT,                    false,                          \
303           NULL,         "Don't replay the journal")                     \
304         x(rebuild_replicas,             u8,                             \
305           OPT_FS|OPT_MOUNT,                                             \
306           OPT_BOOL(),                                                   \
307           NO_SB_OPT,                    false,                          \
308           NULL,         "Rebuild the superblock replicas section")      \
309         x(keep_journal,                 u8,                             \
310           0,                                                            \
311           OPT_BOOL(),                                                   \
312           NO_SB_OPT,                    false,                          \
313           NULL,         "Don't free journal entries/keys after startup")\
314         x(read_entire_journal,          u8,                             \
315           0,                                                            \
316           OPT_BOOL(),                                                   \
317           NO_SB_OPT,                    false,                          \
318           NULL,         "Read all journal entries, not just dirty ones")\
319         x(noexcl,                       u8,                             \
320           OPT_FS|OPT_MOUNT,                                             \
321           OPT_BOOL(),                                                   \
322           NO_SB_OPT,                    false,                          \
323           NULL,         "Don't open device in exclusive mode")          \
324         x(sb,                           u64,                            \
325           OPT_MOUNT,                                                    \
326           OPT_UINT(0, S64_MAX),                                         \
327           NO_SB_OPT,                    BCH_SB_SECTOR,                  \
328           "offset",     "Sector offset of superblock")                  \
329         x(read_only,                    u8,                             \
330           OPT_FS,                                                       \
331           OPT_BOOL(),                                                   \
332           NO_SB_OPT,                    false,                          \
333           NULL,         NULL)                                           \
334         x(nostart,                      u8,                             \
335           0,                                                            \
336           OPT_BOOL(),                                                   \
337           NO_SB_OPT,                    false,                          \
338           NULL,         "Don\'t start filesystem, only open devices")   \
339         x(reconstruct_alloc,            u8,                             \
340           OPT_FS|OPT_MOUNT,                                             \
341           OPT_BOOL(),                                                   \
342           NO_SB_OPT,                    false,                          \
343           NULL,         "Reconstruct alloc btree")                      \
344         x(version_upgrade,              u8,                             \
345           OPT_FS|OPT_MOUNT,                                             \
346           OPT_BOOL(),                                                   \
347           NO_SB_OPT,                    false,                          \
348           NULL,         "Set superblock to latest version,\n"           \
349                         "allowing any new features to be used")         \
350         x(project,                      u8,                             \
351           OPT_INODE,                                                    \
352           OPT_BOOL(),                                                   \
353           NO_SB_OPT,                    false,                          \
354           NULL,         NULL)                                           \
355         x(fs_size,                      u64,                            \
356           OPT_DEVICE,                                                   \
357           OPT_SECTORS(0, S64_MAX),                                      \
358           NO_SB_OPT,                    0,                              \
359           "size",       "Size of filesystem on device")                 \
360         x(bucket,                       u32,                            \
361           OPT_DEVICE,                                                   \
362           OPT_SECTORS(0, S64_MAX),                                      \
363           NO_SB_OPT,                    0,                              \
364           "size",       "Size of filesystem on device")                 \
365         x(durability,                   u8,                             \
366           OPT_DEVICE,                                                   \
367           OPT_UINT(0, BCH_REPLICAS_MAX),                                \
368           NO_SB_OPT,                    1,                              \
369           "n",          "Data written to this device will be considered\n"\
370                         "to have already been replicated n times")
371
372 struct bch_opts {
373 #define x(_name, _bits, ...)    unsigned _name##_defined:1;
374         BCH_OPTS()
375 #undef x
376
377 #define x(_name, _bits, ...)    _bits   _name;
378         BCH_OPTS()
379 #undef x
380 };
381
382 static const struct bch_opts bch2_opts_default = {
383 #define x(_name, _bits, _mode, _type, _sb_opt, _default, ...)           \
384         ._name##_defined = true,                                        \
385         ._name = _default,                                              \
386
387         BCH_OPTS()
388 #undef x
389 };
390
391 #define opt_defined(_opts, _name)       ((_opts)._name##_defined)
392
393 #define opt_get(_opts, _name)                                           \
394         (opt_defined(_opts, _name) ? (_opts)._name : bch2_opts_default._name)
395
396 #define opt_set(_opts, _name, _v)                                       \
397 do {                                                                    \
398         (_opts)._name##_defined = true;                                 \
399         (_opts)._name = _v;                                             \
400 } while (0)
401
402 static inline struct bch_opts bch2_opts_empty(void)
403 {
404         return (struct bch_opts) { 0 };
405 }
406
407 void bch2_opts_apply(struct bch_opts *, struct bch_opts);
408
409 enum bch_opt_id {
410 #define x(_name, ...)   Opt_##_name,
411         BCH_OPTS()
412 #undef x
413         bch2_opts_nr
414 };
415
416 struct bch_fs;
417 struct printbuf;
418
419 struct bch_option {
420         struct attribute        attr;
421         void                    (*set_sb)(struct bch_sb *, u64);
422         enum opt_mode           mode;
423         enum opt_type           type;
424
425         union {
426         struct {
427                 u64             min, max;
428         };
429         struct {
430                 const char * const *choices;
431         };
432         struct {
433                 int (*parse)(struct bch_fs *, const char *, u64 *);
434                 void (*to_text)(struct printbuf *, struct bch_fs *, u64);
435         };
436         };
437
438         const char              *hint;
439         const char              *help;
440
441 };
442
443 extern const struct bch_option bch2_opt_table[];
444
445 bool bch2_opt_defined_by_id(const struct bch_opts *, enum bch_opt_id);
446 u64 bch2_opt_get_by_id(const struct bch_opts *, enum bch_opt_id);
447 void bch2_opt_set_by_id(struct bch_opts *, enum bch_opt_id, u64);
448
449 struct bch_opts bch2_opts_from_sb(struct bch_sb *);
450
451 int bch2_opt_lookup(const char *);
452 int bch2_opt_parse(struct bch_fs *, const struct bch_option *, const char *, u64 *);
453
454 #define OPT_SHOW_FULL_LIST      (1 << 0)
455 #define OPT_SHOW_MOUNT_STYLE    (1 << 1)
456
457 void bch2_opt_to_text(struct printbuf *, struct bch_fs *,
458                       const struct bch_option *, u64, unsigned);
459
460 int bch2_opt_check_may_set(struct bch_fs *, int, u64);
461 int bch2_opts_check_may_set(struct bch_fs *);
462 int bch2_parse_mount_opts(struct bch_fs *, struct bch_opts *, char *);
463
464 /* inode opts: */
465
466 struct bch_io_opts {
467 #define x(_name, _bits) unsigned _name##_defined:1;
468         BCH_INODE_OPTS()
469 #undef x
470
471 #define x(_name, _bits) u##_bits _name;
472         BCH_INODE_OPTS()
473 #undef x
474 };
475
476 struct bch_io_opts bch2_opts_to_inode_opts(struct bch_opts);
477 struct bch_opts bch2_inode_opts_to_opts(struct bch_io_opts);
478 void bch2_io_opts_apply(struct bch_io_opts *, struct bch_io_opts);
479 bool bch2_opt_is_inode_opt(enum bch_opt_id);
480
481 #endif /* _BCACHEFS_OPTS_H */