bcachefs: Make bkey types globally unique
[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[];
12extern const char * const bch2_csum_types[];
13extern const char * const bch2_compression_types[];
14extern const char * const bch2_str_hash_types[];
15extern const char * const bch2_data_types[];
16extern const char * const bch2_cache_replacement_policies[];
17extern const char * const bch2_cache_modes[];
18extern 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 */
33LE64_BITMASK(NO_SB_OPT, struct bch_sb, flags[0], 0, 0);
34
35enum opt_mode {
36 OPT_INTERNAL,
37 OPT_FORMAT,
38 OPT_MOUNT,
39 OPT_RUNTIME,
40};
41
42enum opt_type {
43 BCH_OPT_BOOL,
44 BCH_OPT_UINT,
45 BCH_OPT_STR,
46 BCH_OPT_FN,
47};
48
49/**
50 * BCH_OPT(name, type, in mem type, mode, sb_opt)
51 *
52 * @name - name of mount option, sysfs attribute, and struct bch_opts
53 * member
54 *
55 * @mode - when opt may be set
56 *
57 * @sb_option - name of corresponding superblock option
58 *
59 * @type - one of OPT_BOOL, OPT_UINT, OPT_STR
60 */
61
62/*
63 * XXX: add fields for
64 * - default value
65 * - helptext
66 */
67
68#define BCH_OPTS() \
69 BCH_OPT(block_size, u16, OPT_FORMAT, \
70 OPT_UINT(1, 128), \
71 BCH_SB_BLOCK_SIZE, 8) \
72 BCH_OPT(btree_node_size, u16, OPT_FORMAT, \
73 OPT_UINT(1, 128), \
74 BCH_SB_BTREE_NODE_SIZE, 512) \
75 BCH_OPT(errors, u8, OPT_RUNTIME, \
76 OPT_STR(bch2_error_actions), \
77 BCH_SB_ERROR_ACTION, BCH_ON_ERROR_RO) \
78 BCH_OPT(metadata_replicas, u8, OPT_RUNTIME, \
79 OPT_UINT(1, BCH_REPLICAS_MAX), \
80 BCH_SB_META_REPLICAS_WANT, 1) \
81 BCH_OPT(data_replicas, u8, OPT_RUNTIME, \
82 OPT_UINT(1, BCH_REPLICAS_MAX), \
83 BCH_SB_DATA_REPLICAS_WANT, 1) \
84 BCH_OPT(metadata_replicas_required, u8, OPT_MOUNT, \
85 OPT_UINT(1, BCH_REPLICAS_MAX), \
86 BCH_SB_META_REPLICAS_REQ, 1) \
87 BCH_OPT(data_replicas_required, u8, OPT_MOUNT, \
88 OPT_UINT(1, BCH_REPLICAS_MAX), \
89 BCH_SB_DATA_REPLICAS_REQ, 1) \
90 BCH_OPT(metadata_checksum, u8, OPT_RUNTIME, \
91 OPT_STR(bch2_csum_types), \
92 BCH_SB_META_CSUM_TYPE, BCH_CSUM_OPT_CRC32C) \
93 BCH_OPT(data_checksum, u8, OPT_RUNTIME, \
94 OPT_STR(bch2_csum_types), \
95 BCH_SB_DATA_CSUM_TYPE, BCH_CSUM_OPT_CRC32C) \
96 BCH_OPT(compression, u8, OPT_RUNTIME, \
97 OPT_STR(bch2_compression_types), \
98 BCH_SB_COMPRESSION_TYPE, BCH_COMPRESSION_OPT_NONE)\
99 BCH_OPT(background_compression, u8, OPT_RUNTIME, \
100 OPT_STR(bch2_compression_types), \
101 BCH_SB_BACKGROUND_COMPRESSION_TYPE,BCH_COMPRESSION_OPT_NONE)\
102 BCH_OPT(str_hash, u8, OPT_RUNTIME, \
103 OPT_STR(bch2_str_hash_types), \
104 BCH_SB_STR_HASH_TYPE, BCH_STR_HASH_SIPHASH) \
105 BCH_OPT(foreground_target, u16, OPT_RUNTIME, \
106 OPT_FN(bch2_opt_target), \
107 BCH_SB_FOREGROUND_TARGET, 0) \
108 BCH_OPT(background_target, u16, OPT_RUNTIME, \
109 OPT_FN(bch2_opt_target), \
110 BCH_SB_BACKGROUND_TARGET, 0) \
111 BCH_OPT(promote_target, u16, OPT_RUNTIME, \
112 OPT_FN(bch2_opt_target), \
113 BCH_SB_PROMOTE_TARGET, 0) \
cd575ddf
KO
114 BCH_OPT(erasure_code, u16, OPT_RUNTIME, \
115 OPT_BOOL(), \
116 BCH_SB_ERASURE_CODE, false) \
1c6fdbd8
KO
117 BCH_OPT(inodes_32bit, u8, OPT_RUNTIME, \
118 OPT_BOOL(), \
119 BCH_SB_INODE_32BIT, false) \
a50ed7c8 120 BCH_OPT(gc_reserve_percent, u8, OPT_RUNTIME, \
1c6fdbd8
KO
121 OPT_UINT(5, 21), \
122 BCH_SB_GC_RESERVE, 8) \
a50ed7c8
KO
123 BCH_OPT(gc_reserve_bytes, u64, OPT_RUNTIME, \
124 OPT_UINT(0, U64_MAX), \
125 BCH_SB_GC_RESERVE_BYTES, 0) \
1c6fdbd8
KO
126 BCH_OPT(root_reserve_percent, u8, OPT_MOUNT, \
127 OPT_UINT(0, 100), \
128 BCH_SB_ROOT_RESERVE, 0) \
129 BCH_OPT(wide_macs, u8, OPT_RUNTIME, \
130 OPT_BOOL(), \
131 BCH_SB_128_BIT_MACS, false) \
132 BCH_OPT(acl, u8, OPT_MOUNT, \
133 OPT_BOOL(), \
134 BCH_SB_POSIX_ACL, true) \
135 BCH_OPT(usrquota, u8, OPT_MOUNT, \
136 OPT_BOOL(), \
137 BCH_SB_USRQUOTA, false) \
138 BCH_OPT(grpquota, u8, OPT_MOUNT, \
139 OPT_BOOL(), \
140 BCH_SB_GRPQUOTA, false) \
141 BCH_OPT(prjquota, u8, OPT_MOUNT, \
142 OPT_BOOL(), \
143 BCH_SB_PRJQUOTA, false) \
144 BCH_OPT(degraded, u8, OPT_MOUNT, \
145 OPT_BOOL(), \
146 NO_SB_OPT, false) \
147 BCH_OPT(discard, u8, OPT_MOUNT, \
148 OPT_BOOL(), \
149 NO_SB_OPT, false) \
150 BCH_OPT(verbose_recovery, u8, OPT_MOUNT, \
151 OPT_BOOL(), \
152 NO_SB_OPT, false) \
153 BCH_OPT(verbose_init, u8, OPT_MOUNT, \
154 OPT_BOOL(), \
155 NO_SB_OPT, false) \
156 BCH_OPT(journal_flush_disabled, u8, OPT_RUNTIME, \
157 OPT_BOOL(), \
158 NO_SB_OPT, false) \
02f1a96c 159 BCH_OPT(fsck, u8, OPT_MOUNT, \
1c6fdbd8 160 OPT_BOOL(), \
02f1a96c 161 NO_SB_OPT, true) \
1c6fdbd8
KO
162 BCH_OPT(fix_errors, u8, OPT_MOUNT, \
163 OPT_BOOL(), \
164 NO_SB_OPT, false) \
165 BCH_OPT(nochanges, u8, OPT_MOUNT, \
166 OPT_BOOL(), \
167 NO_SB_OPT, false) \
168 BCH_OPT(noreplay, u8, OPT_MOUNT, \
169 OPT_BOOL(), \
170 NO_SB_OPT, false) \
171 BCH_OPT(norecovery, u8, OPT_MOUNT, \
172 OPT_BOOL(), \
173 NO_SB_OPT, false) \
174 BCH_OPT(noexcl, u8, OPT_MOUNT, \
175 OPT_BOOL(), \
176 NO_SB_OPT, false) \
177 BCH_OPT(sb, u64, OPT_MOUNT, \
178 OPT_UINT(0, S64_MAX), \
179 NO_SB_OPT, BCH_SB_SECTOR) \
180 BCH_OPT(read_only, u8, OPT_INTERNAL, \
181 OPT_BOOL(), \
182 NO_SB_OPT, false) \
183 BCH_OPT(nostart, u8, OPT_INTERNAL, \
184 OPT_BOOL(), \
185 NO_SB_OPT, false) \
186 BCH_OPT(no_data_io, u8, OPT_MOUNT, \
26609b61
KO
187 OPT_BOOL(), \
188 NO_SB_OPT, false) \
189 BCH_OPT(version_upgrade, u8, OPT_MOUNT, \
1c6fdbd8
KO
190 OPT_BOOL(), \
191 NO_SB_OPT, false)
192
193struct bch_opts {
194#define BCH_OPT(_name, _bits, ...) unsigned _name##_defined:1;
195 BCH_OPTS()
196#undef BCH_OPT
197
198#define BCH_OPT(_name, _bits, ...) _bits _name;
199 BCH_OPTS()
200#undef BCH_OPT
201};
202
203static const struct bch_opts bch2_opts_default = {
204#define BCH_OPT(_name, _bits, _mode, _type, _sb_opt, _default) \
205 ._name##_defined = true, \
206 ._name = _default, \
207
208 BCH_OPTS()
209#undef BCH_OPT
210};
211
212#define opt_defined(_opts, _name) ((_opts)._name##_defined)
213
214#define opt_get(_opts, _name) \
215 (opt_defined(_opts, _name) ? (_opts)._name : bch2_opts_default._name)
216
217#define opt_set(_opts, _name, _v) \
218do { \
219 (_opts)._name##_defined = true; \
220 (_opts)._name = _v; \
221} while (0)
222
223static inline struct bch_opts bch2_opts_empty(void)
224{
225 return (struct bch_opts) { 0 };
226}
227
228void bch2_opts_apply(struct bch_opts *, struct bch_opts);
229
230enum bch_opt_id {
231#define BCH_OPT(_name, ...) Opt_##_name,
232 BCH_OPTS()
233#undef BCH_OPT
234 bch2_opts_nr
235};
236
237struct bch_fs;
319f9ac3 238struct printbuf;
1c6fdbd8
KO
239
240struct bch_option {
241 struct attribute attr;
242 void (*set_sb)(struct bch_sb *, u64);
243 enum opt_mode mode;
244 enum opt_type type;
245
246 union {
247 struct {
248 u64 min, max;
249 };
250 struct {
251 const char * const *choices;
252 };
253 struct {
254 int (*parse)(struct bch_fs *, const char *, u64 *);
319f9ac3 255 void (*to_text)(struct printbuf *, struct bch_fs *, u64);
1c6fdbd8
KO
256 };
257 };
258
259};
260
261extern const struct bch_option bch2_opt_table[];
262
263bool bch2_opt_defined_by_id(const struct bch_opts *, enum bch_opt_id);
264u64 bch2_opt_get_by_id(const struct bch_opts *, enum bch_opt_id);
265void bch2_opt_set_by_id(struct bch_opts *, enum bch_opt_id, u64);
266
267struct bch_opts bch2_opts_from_sb(struct bch_sb *);
268
269int bch2_opt_lookup(const char *);
270int bch2_opt_parse(struct bch_fs *, const struct bch_option *, const char *, u64 *);
271
272#define OPT_SHOW_FULL_LIST (1 << 0)
273#define OPT_SHOW_MOUNT_STYLE (1 << 1)
274
319f9ac3
KO
275void bch2_opt_to_text(struct printbuf *, struct bch_fs *,
276 const struct bch_option *, u64, unsigned);
1c6fdbd8 277
c258f28e 278int bch2_opt_check_may_set(struct bch_fs *, int, u64);
cd575ddf 279int bch2_opts_check_may_set(struct bch_fs *);
1c6fdbd8
KO
280int bch2_parse_mount_opts(struct bch_opts *, char *);
281
282/* inode opts: */
283
284#define BCH_INODE_OPTS() \
285 BCH_INODE_OPT(data_checksum, 8) \
286 BCH_INODE_OPT(compression, 8) \
287 BCH_INODE_OPT(background_compression, 8) \
288 BCH_INODE_OPT(data_replicas, 8) \
289 BCH_INODE_OPT(promote_target, 16) \
290 BCH_INODE_OPT(foreground_target, 16) \
cd575ddf
KO
291 BCH_INODE_OPT(background_target, 16) \
292 BCH_INODE_OPT(erasure_code, 16)
1c6fdbd8
KO
293
294struct bch_io_opts {
295#define BCH_INODE_OPT(_name, _bits) unsigned _name##_defined:1;
296 BCH_INODE_OPTS()
297#undef BCH_INODE_OPT
298
299#define BCH_INODE_OPT(_name, _bits) u##_bits _name;
300 BCH_INODE_OPTS()
301#undef BCH_INODE_OPT
302};
303
304struct bch_io_opts bch2_opts_to_inode_opts(struct bch_opts);
305struct bch_opts bch2_inode_opts_to_opts(struct bch_io_opts);
306void bch2_io_opts_apply(struct bch_io_opts *, struct bch_io_opts);
307bool bch2_opt_is_inode_opt(enum bch_opt_id);
308
309#endif /* _BCACHEFS_OPTS_H */