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