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