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