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