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