]> git.sesse.net Git - bcachefs-tools-debian/blob - libbcachefs/opts.h
Update bcachefs sources to e99d29e402 bcachefs: zstd support, compression refactoring
[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_RUNTIME,            \
77                 OPT_UINT(1, BCH_REPLICAS_MAX),                          \
78                 BCH_SB_META_REPLICAS_WANT,      1)                      \
79         BCH_OPT(data_replicas,          u8,     OPT_RUNTIME,            \
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(verbose_init,           u8,     OPT_MOUNT,              \
131                 OPT_BOOL(),                                             \
132                 NO_SB_OPT,                      false)                  \
133         BCH_OPT(journal_flush_disabled, u8,     OPT_RUNTIME,            \
134                 OPT_BOOL(),                                             \
135                 NO_SB_OPT,                      false)                  \
136         BCH_OPT(nofsck,                 u8,     OPT_MOUNT,              \
137                 OPT_BOOL(),                                             \
138                 NO_SB_OPT,                      false)                  \
139         BCH_OPT(fix_errors,             u8,     OPT_MOUNT,              \
140                 OPT_BOOL(),                                             \
141                 NO_SB_OPT,                      false)                  \
142         BCH_OPT(nochanges,              u8,     OPT_MOUNT,              \
143                 OPT_BOOL(),                                             \
144                 NO_SB_OPT,                      false)                  \
145         BCH_OPT(noreplay,               u8,     OPT_MOUNT,              \
146                 OPT_BOOL(),                                             \
147                 NO_SB_OPT,                      false)                  \
148         BCH_OPT(norecovery,             u8,     OPT_MOUNT,              \
149                 OPT_BOOL(),                                             \
150                 NO_SB_OPT,                      false)                  \
151         BCH_OPT(noexcl,                 u8,     OPT_MOUNT,              \
152                 OPT_BOOL(),                                             \
153                 NO_SB_OPT,                      false)                  \
154         BCH_OPT(sb,                     u64,    OPT_MOUNT,              \
155                 OPT_UINT(0, S64_MAX),                                   \
156                 NO_SB_OPT,                      BCH_SB_SECTOR)          \
157         BCH_OPT(read_only,              u8,     OPT_INTERNAL,           \
158                 OPT_BOOL(),                                             \
159                 NO_SB_OPT,                      false)                  \
160         BCH_OPT(nostart,                u8,     OPT_INTERNAL,           \
161                 OPT_BOOL(),                                             \
162                 NO_SB_OPT,                      false)
163
164 struct bch_opts {
165 #define BCH_OPT(_name, _bits, ...)      unsigned _name##_defined:1;
166         BCH_OPTS()
167 #undef BCH_OPT
168
169 #define BCH_OPT(_name, _bits, ...)      _bits   _name;
170         BCH_OPTS()
171 #undef BCH_OPT
172 };
173
174 static const struct bch_opts bch2_opts_default = {
175 #define BCH_OPT(_name, _bits, _mode, _type, _sb_opt, _default)          \
176         ._name##_defined = true,                                        \
177         ._name = _default,                                              \
178
179         BCH_OPTS()
180 #undef BCH_OPT
181 };
182
183 #define opt_defined(_opts, _name)       ((_opts)._name##_defined)
184
185 #define opt_get(_opts, _name)                                           \
186         (opt_defined(_opts, _name) ? (_opts)._name : bch2_opts_default._name)
187
188 #define opt_set(_opts, _name, _v)                                       \
189 do {                                                                    \
190         (_opts)._name##_defined = true;                                 \
191         (_opts)._name = _v;                                             \
192 } while (0)
193
194 static inline struct bch_opts bch2_opts_empty(void)
195 {
196         return (struct bch_opts) { 0 };
197 }
198
199 void bch2_opts_apply(struct bch_opts *, struct bch_opts);
200
201 enum bch_opt_id {
202 #define BCH_OPT(_name, ...)     Opt_##_name,
203         BCH_OPTS()
204 #undef BCH_OPT
205         bch2_opts_nr
206 };
207
208 struct bch_option {
209         struct attribute        attr;
210         void                    (*set_sb)(struct bch_sb *, u64);
211         enum opt_mode           mode;
212         enum opt_type           type;
213
214         union {
215         struct {
216                 u64             min, max;
217         };
218         struct {
219                 const char * const *choices;
220         };
221         };
222
223 };
224
225 extern const struct bch_option bch2_opt_table[];
226
227 bool bch2_opt_defined_by_id(const struct bch_opts *, enum bch_opt_id);
228 u64 bch2_opt_get_by_id(const struct bch_opts *, enum bch_opt_id);
229 void bch2_opt_set_by_id(struct bch_opts *, enum bch_opt_id, u64);
230
231 struct bch_opts bch2_opts_from_sb(struct bch_sb *);
232
233 int bch2_opt_lookup(const char *);
234 int bch2_opt_parse(const struct bch_option *, const char *, u64 *);
235 int bch2_parse_mount_opts(struct bch_opts *, char *);
236
237 /* inode opts: */
238
239 #define BCH_INODE_OPTS()                                        \
240         BCH_INODE_OPT(data_checksum,                    8)      \
241         BCH_INODE_OPT(compression,                      8)
242
243 struct bch_io_opts {
244 #define BCH_INODE_OPT(_name, _bits)     unsigned _name##_defined:1;
245         BCH_INODE_OPTS()
246 #undef BCH_INODE_OPT
247
248 #define BCH_INODE_OPT(_name, _bits)     u##_bits _name;
249         BCH_INODE_OPTS()
250 #undef BCH_INODE_OPT
251 };
252
253 struct bch_io_opts bch2_opts_to_inode_opts(struct bch_opts);
254 struct bch_opts bch2_inode_opts_to_opts(struct bch_io_opts);
255 void bch2_io_opts_apply(struct bch_io_opts *, struct bch_io_opts);
256 bool bch2_opt_is_inode_opt(enum bch_opt_id);
257
258 #endif /* _BCACHEFS_OPTS_H */