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