]> git.sesse.net Git - bcachefs-tools-debian/blob - libbcachefs/opts.h
New upstream snapshot
[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(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,                    true,                           \
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(rebuild_replicas,             u8,                             \
264           OPT_MOUNT,                                                    \
265           OPT_BOOL(),                                                   \
266           NO_SB_OPT,                    false,                          \
267           NULL,         "Rebuild the superblock replicas section")      \
268         x(keep_journal,                 u8,                             \
269           OPT_MOUNT,                                                    \
270           OPT_BOOL(),                                                   \
271           NO_SB_OPT,                    false,                          \
272           NULL,         "Don't free journal entries/keys after startup")\
273         x(read_entire_journal,          u8,                             \
274           0,                                                            \
275           OPT_BOOL(),                                                   \
276           NO_SB_OPT,                    false,                          \
277           NULL,         "Read all journal entries, not just dirty ones")\
278         x(noexcl,                       u8,                             \
279           OPT_MOUNT,                                                    \
280           OPT_BOOL(),                                                   \
281           NO_SB_OPT,                    false,                          \
282           NULL,         "Don't open device in exclusive mode")          \
283         x(sb,                           u64,                            \
284           OPT_MOUNT,                                                    \
285           OPT_UINT(0, S64_MAX),                                         \
286           NO_SB_OPT,                    BCH_SB_SECTOR,                  \
287           "offset",     "Sector offset of superblock")                  \
288         x(read_only,                    u8,                             \
289           0,                                                            \
290           OPT_BOOL(),                                                   \
291           NO_SB_OPT,                    false,                          \
292           NULL,         NULL)                                           \
293         x(nostart,                      u8,                             \
294           0,                                                            \
295           OPT_BOOL(),                                                   \
296           NO_SB_OPT,                    false,                          \
297           NULL,         "Don\'t start filesystem, only open devices")   \
298         x(reconstruct_alloc,            u8,                             \
299           OPT_MOUNT,                                                    \
300           OPT_BOOL(),                                                   \
301           NO_SB_OPT,                    false,                          \
302           NULL,         "Reconstruct alloc btree")                      \
303         x(version_upgrade,              u8,                             \
304           OPT_MOUNT,                                                    \
305           OPT_BOOL(),                                                   \
306           NO_SB_OPT,                    false,                          \
307           NULL,         "Set superblock to latest version,\n"           \
308                         "allowing any new features to be used")         \
309         x(project,                      u8,                             \
310           OPT_INODE,                                                    \
311           OPT_BOOL(),                                                   \
312           NO_SB_OPT,                    false,                          \
313           NULL,         NULL)                                           \
314         x(fs_size,                      u64,                            \
315           OPT_DEVICE,                                                   \
316           OPT_SECTORS(0, S64_MAX),                                      \
317           NO_SB_OPT,                    0,                              \
318           "size",       "Size of filesystem on device")                 \
319         x(bucket,                       u32,                            \
320           OPT_DEVICE,                                                   \
321           OPT_SECTORS(0, S64_MAX),                                      \
322           NO_SB_OPT,                    0,                              \
323           "size",       "Size of filesystem on device")                 \
324         x(durability,                   u8,                             \
325           OPT_DEVICE,                                                   \
326           OPT_UINT(0, BCH_REPLICAS_MAX),                                \
327           NO_SB_OPT,                    1,                              \
328           "n",          "Data written to this device will be considered\n"\
329                         "to have already been replicated n times")
330
331 struct bch_opts {
332 #define x(_name, _bits, ...)    unsigned _name##_defined:1;
333         BCH_OPTS()
334 #undef x
335
336 #define x(_name, _bits, ...)    _bits   _name;
337         BCH_OPTS()
338 #undef x
339 };
340
341 static const struct bch_opts bch2_opts_default = {
342 #define x(_name, _bits, _mode, _type, _sb_opt, _default, ...)           \
343         ._name##_defined = true,                                        \
344         ._name = _default,                                              \
345
346         BCH_OPTS()
347 #undef x
348 };
349
350 #define opt_defined(_opts, _name)       ((_opts)._name##_defined)
351
352 #define opt_get(_opts, _name)                                           \
353         (opt_defined(_opts, _name) ? (_opts)._name : bch2_opts_default._name)
354
355 #define opt_set(_opts, _name, _v)                                       \
356 do {                                                                    \
357         (_opts)._name##_defined = true;                                 \
358         (_opts)._name = _v;                                             \
359 } while (0)
360
361 static inline struct bch_opts bch2_opts_empty(void)
362 {
363         return (struct bch_opts) { 0 };
364 }
365
366 void bch2_opts_apply(struct bch_opts *, struct bch_opts);
367
368 enum bch_opt_id {
369 #define x(_name, ...)   Opt_##_name,
370         BCH_OPTS()
371 #undef x
372         bch2_opts_nr
373 };
374
375 struct bch_fs;
376 struct printbuf;
377
378 struct bch_option {
379         struct attribute        attr;
380         void                    (*set_sb)(struct bch_sb *, u64);
381         enum opt_mode           mode;
382         enum opt_type           type;
383
384         union {
385         struct {
386                 u64             min, max;
387         };
388         struct {
389                 const char * const *choices;
390         };
391         struct {
392                 int (*parse)(struct bch_fs *, const char *, u64 *);
393                 void (*to_text)(struct printbuf *, struct bch_fs *, u64);
394         };
395         };
396
397         const char              *hint;
398         const char              *help;
399
400 };
401
402 extern const struct bch_option bch2_opt_table[];
403
404 bool bch2_opt_defined_by_id(const struct bch_opts *, enum bch_opt_id);
405 u64 bch2_opt_get_by_id(const struct bch_opts *, enum bch_opt_id);
406 void bch2_opt_set_by_id(struct bch_opts *, enum bch_opt_id, u64);
407
408 struct bch_opts bch2_opts_from_sb(struct bch_sb *);
409
410 int bch2_opt_lookup(const char *);
411 int bch2_opt_parse(struct bch_fs *, const struct bch_option *, const char *, u64 *);
412
413 #define OPT_SHOW_FULL_LIST      (1 << 0)
414 #define OPT_SHOW_MOUNT_STYLE    (1 << 1)
415
416 void bch2_opt_to_text(struct printbuf *, struct bch_fs *,
417                       const struct bch_option *, u64, unsigned);
418
419 int bch2_opt_check_may_set(struct bch_fs *, int, u64);
420 int bch2_opts_check_may_set(struct bch_fs *);
421 int bch2_parse_mount_opts(struct bch_fs *, struct bch_opts *, char *);
422
423 /* inode opts: */
424
425 struct bch_io_opts {
426 #define x(_name, _bits) unsigned _name##_defined:1;
427         BCH_INODE_OPTS()
428 #undef x
429
430 #define x(_name, _bits) u##_bits _name;
431         BCH_INODE_OPTS()
432 #undef x
433 };
434
435 struct bch_io_opts bch2_opts_to_inode_opts(struct bch_opts);
436 struct bch_opts bch2_inode_opts_to_opts(struct bch_io_opts);
437 void bch2_io_opts_apply(struct bch_io_opts *, struct bch_io_opts);
438 bool bch2_opt_is_inode_opt(enum bch_opt_id);
439
440 #endif /* _BCACHEFS_OPTS_H */