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