]> git.sesse.net Git - bcachefs-tools-debian/blob - libbcachefs/opts.h
Update bcachefs sources to b964c6cba8 bcachefs: Change lockrestart_do() to always...
[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(inodes_use_key_cache, u8,                                     \
177           OPT_FORMAT|OPT_MOUNT,                                         \
178           OPT_BOOL(),                                                   \
179           BCH_SB_INODES_USE_KEY_CACHE,  true,                           \
180           NULL,         "Use the btree key cache for the inodes btree") \
181         x(btree_node_mem_ptr_optimization, u8,                          \
182           OPT_MOUNT|OPT_RUNTIME,                                        \
183           OPT_BOOL(),                                                   \
184           NO_SB_OPT,                    true,                           \
185           NULL,         "Stash pointer to in memory btree node in btree ptr")\
186         x(gc_reserve_percent,           u8,                             \
187           OPT_FORMAT|OPT_MOUNT|OPT_RUNTIME,                             \
188           OPT_UINT(5, 21),                                              \
189           BCH_SB_GC_RESERVE,            8,                              \
190           "%",          "Percentage of disk space to reserve for copygc")\
191         x(gc_reserve_bytes,             u64,                            \
192           OPT_FORMAT|OPT_MOUNT|OPT_RUNTIME,                             \
193           OPT_SECTORS(0, U64_MAX),                                      \
194           BCH_SB_GC_RESERVE_BYTES,      0,                              \
195           "%",          "Amount of disk space to reserve for copygc\n"  \
196                         "Takes precedence over gc_reserve_percent if set")\
197         x(root_reserve_percent,         u8,                             \
198           OPT_FORMAT|OPT_MOUNT,                                         \
199           OPT_UINT(0, 100),                                             \
200           BCH_SB_ROOT_RESERVE,          0,                              \
201           "%",          "Percentage of disk space to reserve for superuser")\
202         x(wide_macs,                    u8,                             \
203           OPT_FORMAT|OPT_MOUNT|OPT_RUNTIME,                             \
204           OPT_BOOL(),                                                   \
205           BCH_SB_128_BIT_MACS,          false,                          \
206           NULL,         "Store full 128 bits of cryptographic MACs, instead of 80")\
207         x(inline_data,                  u8,                             \
208           OPT_MOUNT|OPT_RUNTIME,                                        \
209           OPT_BOOL(),                                                   \
210           NO_SB_OPT,                    true,                           \
211           NULL,         "Enable inline data extents")                   \
212         x(acl,                          u8,                             \
213           OPT_FORMAT|OPT_MOUNT,                                         \
214           OPT_BOOL(),                                                   \
215           BCH_SB_POSIX_ACL,             true,                           \
216           NULL,         "Enable POSIX acls")                            \
217         x(usrquota,                     u8,                             \
218           OPT_FORMAT|OPT_MOUNT,                                         \
219           OPT_BOOL(),                                                   \
220           BCH_SB_USRQUOTA,              false,                          \
221           NULL,         "Enable user quotas")                           \
222         x(grpquota,                     u8,                             \
223           OPT_FORMAT|OPT_MOUNT,                                         \
224           OPT_BOOL(),                                                   \
225           BCH_SB_GRPQUOTA,              false,                          \
226           NULL,         "Enable group quotas")                          \
227         x(prjquota,                     u8,                             \
228           OPT_FORMAT|OPT_MOUNT,                                         \
229           OPT_BOOL(),                                                   \
230           BCH_SB_PRJQUOTA,              false,                          \
231           NULL,         "Enable project quotas")                        \
232         x(degraded,                     u8,                             \
233           OPT_MOUNT,                                                    \
234           OPT_BOOL(),                                                   \
235           NO_SB_OPT,                    false,                          \
236           NULL,         "Allow mounting in degraded mode")              \
237         x(very_degraded,                u8,                             \
238           OPT_MOUNT,                                                    \
239           OPT_BOOL(),                                                   \
240           NO_SB_OPT,                    false,                          \
241           NULL,         "Allow mounting in when data will be missing")  \
242         x(discard,                      u8,                             \
243           OPT_MOUNT|OPT_DEVICE,                                         \
244           OPT_BOOL(),                                                   \
245           NO_SB_OPT,                    false,                          \
246           NULL,         "Enable discard/TRIM support")                  \
247         x(verbose,                      u8,                             \
248           OPT_MOUNT,                                                    \
249           OPT_BOOL(),                                                   \
250           NO_SB_OPT,                    false,                          \
251           NULL,         "Extra debugging information during mount/recovery")\
252         x(journal_flush_disabled,       u8,                             \
253           OPT_MOUNT|OPT_RUNTIME,                                        \
254           OPT_BOOL(),                                                   \
255           NO_SB_OPT,                    false,                          \
256           NULL,         "Disable journal flush on sync/fsync\n"         \
257                         "If enabled, writes can be lost, but only since the\n"\
258                         "last journal write (default 1 second)")        \
259         x(fsck,                         u8,                             \
260           OPT_MOUNT,                                                    \
261           OPT_BOOL(),                                                   \
262           NO_SB_OPT,                    false,                          \
263           NULL,         "Run fsck on mount")                            \
264         x(fix_errors,                   u8,                             \
265           OPT_MOUNT,                                                    \
266           OPT_BOOL(),                                                   \
267           NO_SB_OPT,                    false,                          \
268           NULL,         "Fix errors during fsck without asking")        \
269         x(ratelimit_errors,             u8,                             \
270           OPT_MOUNT,                                                    \
271           OPT_BOOL(),                                                   \
272           NO_SB_OPT,                    RATELIMIT_ERRORS,               \
273           NULL,         "Ratelimit error messages during fsck")         \
274         x(nochanges,                    u8,                             \
275           OPT_MOUNT,                                                    \
276           OPT_BOOL(),                                                   \
277           NO_SB_OPT,                    false,                          \
278           NULL,         "Super read only mode - no writes at all will be issued,\n"\
279                         "even if we have to replay the journal")        \
280         x(norecovery,                   u8,                             \
281           OPT_MOUNT,                                                    \
282           OPT_BOOL(),                                                   \
283           NO_SB_OPT,                    false,                          \
284           NULL,         "Don't replay the journal")                     \
285         x(rebuild_replicas,             u8,                             \
286           OPT_MOUNT,                                                    \
287           OPT_BOOL(),                                                   \
288           NO_SB_OPT,                    false,                          \
289           NULL,         "Rebuild the superblock replicas section")      \
290         x(keep_journal,                 u8,                             \
291           OPT_MOUNT,                                                    \
292           OPT_BOOL(),                                                   \
293           NO_SB_OPT,                    false,                          \
294           NULL,         "Don't free journal entries/keys after startup")\
295         x(read_entire_journal,          u8,                             \
296           0,                                                            \
297           OPT_BOOL(),                                                   \
298           NO_SB_OPT,                    false,                          \
299           NULL,         "Read all journal entries, not just dirty ones")\
300         x(noexcl,                       u8,                             \
301           OPT_MOUNT,                                                    \
302           OPT_BOOL(),                                                   \
303           NO_SB_OPT,                    false,                          \
304           NULL,         "Don't open device in exclusive mode")          \
305         x(sb,                           u64,                            \
306           OPT_MOUNT,                                                    \
307           OPT_UINT(0, S64_MAX),                                         \
308           NO_SB_OPT,                    BCH_SB_SECTOR,                  \
309           "offset",     "Sector offset of superblock")                  \
310         x(read_only,                    u8,                             \
311           0,                                                            \
312           OPT_BOOL(),                                                   \
313           NO_SB_OPT,                    false,                          \
314           NULL,         NULL)                                           \
315         x(nostart,                      u8,                             \
316           0,                                                            \
317           OPT_BOOL(),                                                   \
318           NO_SB_OPT,                    false,                          \
319           NULL,         "Don\'t start filesystem, only open devices")   \
320         x(reconstruct_alloc,            u8,                             \
321           OPT_MOUNT,                                                    \
322           OPT_BOOL(),                                                   \
323           NO_SB_OPT,                    false,                          \
324           NULL,         "Reconstruct alloc btree")                      \
325         x(version_upgrade,              u8,                             \
326           OPT_MOUNT,                                                    \
327           OPT_BOOL(),                                                   \
328           NO_SB_OPT,                    false,                          \
329           NULL,         "Set superblock to latest version,\n"           \
330                         "allowing any new features to be used")         \
331         x(project,                      u8,                             \
332           OPT_INODE,                                                    \
333           OPT_BOOL(),                                                   \
334           NO_SB_OPT,                    false,                          \
335           NULL,         NULL)                                           \
336         x(fs_size,                      u64,                            \
337           OPT_DEVICE,                                                   \
338           OPT_SECTORS(0, S64_MAX),                                      \
339           NO_SB_OPT,                    0,                              \
340           "size",       "Size of filesystem on device")                 \
341         x(bucket,                       u32,                            \
342           OPT_DEVICE,                                                   \
343           OPT_SECTORS(0, S64_MAX),                                      \
344           NO_SB_OPT,                    0,                              \
345           "size",       "Size of filesystem on device")                 \
346         x(durability,                   u8,                             \
347           OPT_DEVICE,                                                   \
348           OPT_UINT(0, BCH_REPLICAS_MAX),                                \
349           NO_SB_OPT,                    1,                              \
350           "n",          "Data written to this device will be considered\n"\
351                         "to have already been replicated n times")
352
353 struct bch_opts {
354 #define x(_name, _bits, ...)    unsigned _name##_defined:1;
355         BCH_OPTS()
356 #undef x
357
358 #define x(_name, _bits, ...)    _bits   _name;
359         BCH_OPTS()
360 #undef x
361 };
362
363 static const struct bch_opts bch2_opts_default = {
364 #define x(_name, _bits, _mode, _type, _sb_opt, _default, ...)           \
365         ._name##_defined = true,                                        \
366         ._name = _default,                                              \
367
368         BCH_OPTS()
369 #undef x
370 };
371
372 #define opt_defined(_opts, _name)       ((_opts)._name##_defined)
373
374 #define opt_get(_opts, _name)                                           \
375         (opt_defined(_opts, _name) ? (_opts)._name : bch2_opts_default._name)
376
377 #define opt_set(_opts, _name, _v)                                       \
378 do {                                                                    \
379         (_opts)._name##_defined = true;                                 \
380         (_opts)._name = _v;                                             \
381 } while (0)
382
383 static inline struct bch_opts bch2_opts_empty(void)
384 {
385         return (struct bch_opts) { 0 };
386 }
387
388 void bch2_opts_apply(struct bch_opts *, struct bch_opts);
389
390 enum bch_opt_id {
391 #define x(_name, ...)   Opt_##_name,
392         BCH_OPTS()
393 #undef x
394         bch2_opts_nr
395 };
396
397 struct bch_fs;
398 struct printbuf;
399
400 struct bch_option {
401         struct attribute        attr;
402         void                    (*set_sb)(struct bch_sb *, u64);
403         enum opt_mode           mode;
404         enum opt_type           type;
405
406         union {
407         struct {
408                 u64             min, max;
409         };
410         struct {
411                 const char * const *choices;
412         };
413         struct {
414                 int (*parse)(struct bch_fs *, const char *, u64 *);
415                 void (*to_text)(struct printbuf *, struct bch_fs *, u64);
416         };
417         };
418
419         const char              *hint;
420         const char              *help;
421
422 };
423
424 extern const struct bch_option bch2_opt_table[];
425
426 bool bch2_opt_defined_by_id(const struct bch_opts *, enum bch_opt_id);
427 u64 bch2_opt_get_by_id(const struct bch_opts *, enum bch_opt_id);
428 void bch2_opt_set_by_id(struct bch_opts *, enum bch_opt_id, u64);
429
430 struct bch_opts bch2_opts_from_sb(struct bch_sb *);
431
432 int bch2_opt_lookup(const char *);
433 int bch2_opt_parse(struct bch_fs *, const struct bch_option *, const char *, u64 *);
434
435 #define OPT_SHOW_FULL_LIST      (1 << 0)
436 #define OPT_SHOW_MOUNT_STYLE    (1 << 1)
437
438 void bch2_opt_to_text(struct printbuf *, struct bch_fs *,
439                       const struct bch_option *, u64, unsigned);
440
441 int bch2_opt_check_may_set(struct bch_fs *, int, u64);
442 int bch2_opts_check_may_set(struct bch_fs *);
443 int bch2_parse_mount_opts(struct bch_fs *, struct bch_opts *, char *);
444
445 /* inode opts: */
446
447 struct bch_io_opts {
448 #define x(_name, _bits) unsigned _name##_defined:1;
449         BCH_INODE_OPTS()
450 #undef x
451
452 #define x(_name, _bits) u##_bits _name;
453         BCH_INODE_OPTS()
454 #undef x
455 };
456
457 struct bch_io_opts bch2_opts_to_inode_opts(struct bch_opts);
458 struct bch_opts bch2_inode_opts_to_opts(struct bch_io_opts);
459 void bch2_io_opts_apply(struct bch_io_opts *, struct bch_io_opts);
460 bool bch2_opt_is_inode_opt(enum bch_opt_id);
461
462 #endif /* _BCACHEFS_OPTS_H */