]> git.sesse.net Git - bcachefs-tools-debian/blob - libbcachefs/opts.h
Update bcachefs sources to 380885b0b8 bcachefs: Fix counting iterators for reflink...
[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 #define BCH_OPTS()                                                      \
72         x(block_size,                   u16,                            \
73           OPT_FORMAT,                                                   \
74           OPT_SECTORS(1, 128),                                          \
75           BCH_SB_BLOCK_SIZE,            8,                              \
76           "size",       NULL)                                           \
77         x(btree_node_size,              u16,                            \
78           OPT_FORMAT,                                                   \
79           OPT_SECTORS(1, 128),                                          \
80           BCH_SB_BTREE_NODE_SIZE,       512,                            \
81           "size",       "Btree node size, default 256k")                \
82         x(errors,                       u8,                             \
83           OPT_FORMAT|OPT_MOUNT|OPT_RUNTIME,                             \
84           OPT_STR(bch2_error_actions),                                  \
85           BCH_SB_ERROR_ACTION,          BCH_ON_ERROR_RO,                \
86           NULL,         "Action to take on filesystem error")           \
87         x(metadata_replicas,            u8,                             \
88           OPT_FORMAT|OPT_MOUNT|OPT_RUNTIME,                             \
89           OPT_UINT(1, BCH_REPLICAS_MAX),                                \
90           BCH_SB_META_REPLICAS_WANT,    1,                              \
91           "#",          "Number of metadata replicas")                  \
92         x(data_replicas,                u8,                             \
93           OPT_FORMAT|OPT_MOUNT|OPT_RUNTIME|OPT_INODE,                   \
94           OPT_UINT(1, BCH_REPLICAS_MAX),                                \
95           BCH_SB_DATA_REPLICAS_WANT,    1,                              \
96           "#",          "Number of data replicas")                      \
97         x(metadata_replicas_required, u8,                               \
98           OPT_FORMAT|OPT_MOUNT,                                         \
99           OPT_UINT(1, BCH_REPLICAS_MAX),                                \
100           BCH_SB_META_REPLICAS_REQ,     1,                              \
101           "#",          NULL)                                           \
102         x(data_replicas_required,       u8,                             \
103           OPT_FORMAT|OPT_MOUNT,                                         \
104           OPT_UINT(1, BCH_REPLICAS_MAX),                                \
105           BCH_SB_DATA_REPLICAS_REQ,     1,                              \
106           "#",          NULL)                                           \
107         x(metadata_checksum,            u8,                             \
108           OPT_FORMAT|OPT_MOUNT|OPT_RUNTIME,                             \
109           OPT_STR(bch2_csum_types),                                     \
110           BCH_SB_META_CSUM_TYPE,        BCH_CSUM_OPT_CRC32C,            \
111           NULL,         NULL)                                           \
112         x(data_checksum,                u8,                             \
113           OPT_FORMAT|OPT_MOUNT|OPT_RUNTIME|OPT_INODE,                   \
114           OPT_STR(bch2_csum_types),                                     \
115           BCH_SB_DATA_CSUM_TYPE,        BCH_CSUM_OPT_CRC32C,            \
116           NULL,         NULL)                                           \
117         x(compression,                  u8,                             \
118           OPT_FORMAT|OPT_MOUNT|OPT_RUNTIME|OPT_INODE,                   \
119           OPT_STR(bch2_compression_types),                              \
120           BCH_SB_COMPRESSION_TYPE,      BCH_COMPRESSION_OPT_NONE,       \
121           NULL,         NULL)                                           \
122         x(background_compression,       u8,                             \
123           OPT_FORMAT|OPT_MOUNT|OPT_RUNTIME|OPT_INODE,                   \
124           OPT_STR(bch2_compression_types),                              \
125           BCH_SB_BACKGROUND_COMPRESSION_TYPE,BCH_COMPRESSION_OPT_NONE,  \
126           NULL,         NULL)                                           \
127         x(str_hash,                     u8,                             \
128           OPT_FORMAT|OPT_MOUNT|OPT_RUNTIME,                             \
129           OPT_STR(bch2_str_hash_types),                                 \
130           BCH_SB_STR_HASH_TYPE,         BCH_STR_HASH_SIPHASH,           \
131           NULL,         "Hash function for directory entries and xattrs")\
132         x(foreground_target,            u16,                            \
133           OPT_FORMAT|OPT_MOUNT|OPT_RUNTIME|OPT_INODE,                   \
134           OPT_FN(bch2_opt_target),                                      \
135           BCH_SB_FOREGROUND_TARGET,     0,                              \
136           "(target)",   "Device or disk group for foreground writes")   \
137         x(background_target,            u16,                            \
138           OPT_FORMAT|OPT_MOUNT|OPT_RUNTIME|OPT_INODE,                   \
139           OPT_FN(bch2_opt_target),                                      \
140           BCH_SB_BACKGROUND_TARGET,     0,                              \
141           "(target)",   "Device or disk group to move data to in the background")\
142         x(promote_target,               u16,                            \
143           OPT_FORMAT|OPT_MOUNT|OPT_RUNTIME|OPT_INODE,                   \
144           OPT_FN(bch2_opt_target),                                      \
145           BCH_SB_PROMOTE_TARGET,        0,                              \
146           "(target)",   "Device or disk group to promote data to on read")\
147         x(erasure_code,                 u16,                            \
148           OPT_FORMAT|OPT_MOUNT|OPT_RUNTIME|OPT_INODE,                   \
149           OPT_BOOL(),                                                   \
150           BCH_SB_ERASURE_CODE,          false,                          \
151           NULL,         "Enable erasure coding (DO NOT USE YET)")       \
152         x(inodes_32bit,                 u8,                             \
153           OPT_FORMAT|OPT_MOUNT|OPT_RUNTIME,                             \
154           OPT_BOOL(),                                                   \
155           BCH_SB_INODE_32BIT,           false,                          \
156           NULL,         "Constrain inode numbers to 32 bits")           \
157         x(gc_reserve_percent,           u8,                             \
158           OPT_FORMAT|OPT_MOUNT|OPT_RUNTIME,                             \
159           OPT_UINT(5, 21),                                              \
160           BCH_SB_GC_RESERVE,            8,                              \
161           "%",          "Percentage of disk space to reserve for copygc")\
162         x(gc_reserve_bytes,             u64,                            \
163           OPT_FORMAT|OPT_MOUNT|OPT_RUNTIME,                             \
164           OPT_SECTORS(0, U64_MAX),                                      \
165           BCH_SB_GC_RESERVE_BYTES,      0,                              \
166           "%",          "Amount of disk space to reserve for copygc\n"  \
167                         "Takes precedence over gc_reserve_percent if set")\
168         x(root_reserve_percent,         u8,                             \
169           OPT_FORMAT|OPT_MOUNT,                                         \
170           OPT_UINT(0, 100),                                             \
171           BCH_SB_ROOT_RESERVE,          0,                              \
172           "%",          "Percentage of disk space to reserve for superuser")\
173         x(wide_macs,                    u8,                             \
174           OPT_FORMAT|OPT_MOUNT|OPT_RUNTIME,                             \
175           OPT_BOOL(),                                                   \
176           BCH_SB_128_BIT_MACS,          false,                          \
177           NULL,         "Store full 128 bits of cryptographic MACs, instead of 80")\
178         x(acl,                          u8,                             \
179           OPT_FORMAT|OPT_MOUNT,                                         \
180           OPT_BOOL(),                                                   \
181           BCH_SB_POSIX_ACL,             true,                           \
182           NULL,         "Enable POSIX acls")                            \
183         x(usrquota,                     u8,                             \
184           OPT_FORMAT|OPT_MOUNT,                                         \
185           OPT_BOOL(),                                                   \
186           BCH_SB_USRQUOTA,              false,                          \
187           NULL,         "Enable user quotas")                           \
188         x(grpquota,                     u8,                             \
189           OPT_FORMAT|OPT_MOUNT,                                         \
190           OPT_BOOL(),                                                   \
191           BCH_SB_GRPQUOTA,              false,                          \
192           NULL,         "Enable group quotas")                          \
193         x(prjquota,                     u8,                             \
194           OPT_FORMAT|OPT_MOUNT,                                         \
195           OPT_BOOL(),                                                   \
196           BCH_SB_PRJQUOTA,              false,                          \
197           NULL,         "Enable project quotas")                        \
198         x(degraded,                     u8,                             \
199           OPT_MOUNT,                                                    \
200           OPT_BOOL(),                                                   \
201           NO_SB_OPT,                    false,                          \
202           NULL,         "Allow mounting in degraded mode")              \
203         x(discard,                      u8,                             \
204           OPT_MOUNT|OPT_DEVICE,                                         \
205           OPT_BOOL(),                                                   \
206           NO_SB_OPT,                    false,                          \
207           NULL,         "Enable discard/TRIM support")                  \
208         x(verbose,                      u8,                             \
209           OPT_MOUNT,                                                    \
210           OPT_BOOL(),                                                   \
211           NO_SB_OPT,                    false,                          \
212           NULL,         "Extra debugging information during mount/recovery")\
213         x(journal_flush_disabled,       u8,                             \
214           OPT_MOUNT|OPT_RUNTIME,                                        \
215           OPT_BOOL(),                                                   \
216           NO_SB_OPT,                    false,                          \
217           NULL,         "Disable journal flush on sync/fsync\n"         \
218                         "If enabled, writes can be lost, but only since the\n"\
219                         "last journal write (default 1 second)")        \
220         x(fsck,                         u8,                             \
221           OPT_MOUNT,                                                    \
222           OPT_BOOL(),                                                   \
223           NO_SB_OPT,                    false,                          \
224           NULL,         "Run fsck on mount")                            \
225         x(fix_errors,                   u8,                             \
226           OPT_MOUNT,                                                    \
227           OPT_BOOL(),                                                   \
228           NO_SB_OPT,                    false,                          \
229           NULL,         "Fix errors during fsck without asking")        \
230         x(nochanges,                    u8,                             \
231           OPT_MOUNT,                                                    \
232           OPT_BOOL(),                                                   \
233           NO_SB_OPT,                    false,                          \
234           NULL,         "Super read only mode - no writes at all will be issued,\n"\
235                         "even if we have to replay the journal")        \
236         x(norecovery,                   u8,                             \
237           OPT_MOUNT,                                                    \
238           OPT_BOOL(),                                                   \
239           NO_SB_OPT,                    false,                          \
240           NULL,         "Don't replay the journal")                     \
241         x(noexcl,                       u8,                             \
242           OPT_MOUNT,                                                    \
243           OPT_BOOL(),                                                   \
244           NO_SB_OPT,                    false,                          \
245           NULL,         "Don't open device in exclusive mode")          \
246         x(sb,                           u64,                            \
247           OPT_MOUNT,                                                    \
248           OPT_UINT(0, S64_MAX),                                         \
249           NO_SB_OPT,                    BCH_SB_SECTOR,                  \
250           "offset",     "Sector offset of superblock")                  \
251         x(read_only,                    u8,                             \
252           0,                                                            \
253           OPT_BOOL(),                                                   \
254           NO_SB_OPT,                    false,                          \
255           NULL,         NULL)                                           \
256         x(nostart,                      u8,                             \
257           0,                                                            \
258           OPT_BOOL(),                                                   \
259           NO_SB_OPT,                    false,                          \
260           NULL,         "Don\'t start filesystem, only open devices")   \
261         x(reconstruct_alloc,            u8,                             \
262           OPT_MOUNT,                                                    \
263           OPT_BOOL(),                                                   \
264           NO_SB_OPT,                    false,                          \
265           NULL,         "Reconstruct alloc btree")                      \
266         x(version_upgrade,              u8,                             \
267           OPT_MOUNT,                                                    \
268           OPT_BOOL(),                                                   \
269           NO_SB_OPT,                    false,                          \
270           NULL,         "Set superblock to latest version,\n"           \
271                         "allowing any new features to be used")         \
272         x(project,                      u8,                             \
273           OPT_INODE,                                                    \
274           OPT_BOOL(),                                                   \
275           NO_SB_OPT,                    false,                          \
276           NULL,         NULL)                                           \
277         x(fs_size,                      u64,                            \
278           OPT_DEVICE,                                                   \
279           OPT_SECTORS(0, S64_MAX),                                      \
280           NO_SB_OPT,                    0,                              \
281           "size",       "Size of filesystem on device")                 \
282         x(bucket,                       u32,                            \
283           OPT_DEVICE,                                                   \
284           OPT_SECTORS(0, S64_MAX),                                      \
285           NO_SB_OPT,                    0,                              \
286           "size",       "Size of filesystem on device")                 \
287         x(durability,                   u8,                             \
288           OPT_DEVICE,                                                   \
289           OPT_UINT(0, BCH_REPLICAS_MAX),                                \
290           NO_SB_OPT,                    1,                              \
291           "n",          "Data written to this device will be considered\n"\
292                         "to have already been replicated n times")
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 */