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