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