summaryrefslogtreecommitdiff
path: root/fs/bcachefs/opts.h
blob: bdf1e4fb606e713f1b19f585a47169bbf9061e1e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
#ifndef _BCACHEFS_OPTS_H
#define _BCACHEFS_OPTS_H

#include <linux/bug.h>
#include <linux/log2.h>
#include <linux/string.h>
#include <linux/sysfs.h>
#include "bcachefs_format.h"

extern const char * const bch2_error_actions[];
extern const char * const bch2_csum_types[];
extern const char * const bch2_compression_types[];
extern const char * const bch2_str_hash_types[];
extern const char * const bch2_data_types[];
extern const char * const bch2_cache_replacement_policies[];
extern const char * const bch2_cache_modes[];
extern const char * const bch2_dev_state[];

/*
 * Mount options; we also store defaults in the superblock.
 *
 * Also exposed via sysfs: if an option is writeable, and it's also stored in
 * the superblock, changing it via sysfs (currently? might change this) also
 * updates the superblock.
 *
 * We store options as signed integers, where -1 means undefined. This means we
 * can pass the mount options to bch2_fs_alloc() as a whole struct, and then only
 * apply the options from that struct that are defined.
 */

/* dummy option, for options that aren't stored in the superblock */
LE64_BITMASK(NO_SB_OPT,		struct bch_sb, flags[0], 0, 0);

enum opt_mode {
	OPT_INTERNAL,
	OPT_FORMAT,
	OPT_MOUNT,
	OPT_RUNTIME,
};

enum opt_type {
	BCH_OPT_BOOL,
	BCH_OPT_UINT,
	BCH_OPT_STR,
	BCH_OPT_FN,
};

/**
 * BCH_OPT(name, type, in mem type, mode, sb_opt)
 *
 * @name	- name of mount option, sysfs attribute, and struct bch_opts
 *		  member
 *
 * @mode	- when opt may be set
 *
 * @sb_option	- name of corresponding superblock option
 *
 * @type	- one of OPT_BOOL, OPT_UINT, OPT_STR
 */

/*
 * XXX: add fields for
 *  - default value
 *  - helptext
 */

#define BCH_OPTS()							\
	BCH_OPT(block_size,		u16,	OPT_FORMAT,		\
		OPT_UINT(1, 128),					\
		BCH_SB_BLOCK_SIZE,		8)			\
	BCH_OPT(btree_node_size,	u16,	OPT_FORMAT,		\
		OPT_UINT(1, 128),					\
		BCH_SB_BTREE_NODE_SIZE,		512)			\
	BCH_OPT(errors,			u8,	OPT_RUNTIME,		\
		OPT_STR(bch2_error_actions),				\
		BCH_SB_ERROR_ACTION,		BCH_ON_ERROR_RO)	\
	BCH_OPT(metadata_replicas,	u8,	OPT_RUNTIME,		\
		OPT_UINT(1, BCH_REPLICAS_MAX),				\
		BCH_SB_META_REPLICAS_WANT,	1)			\
	BCH_OPT(data_replicas,		u8,	OPT_RUNTIME,		\
		OPT_UINT(1, BCH_REPLICAS_MAX),				\
		BCH_SB_DATA_REPLICAS_WANT,	1)			\
	BCH_OPT(metadata_replicas_required, u8,	OPT_MOUNT,		\
		OPT_UINT(1, BCH_REPLICAS_MAX),				\
		BCH_SB_META_REPLICAS_REQ,	1)			\
	BCH_OPT(data_replicas_required, u8,	OPT_MOUNT,		\
		OPT_UINT(1, BCH_REPLICAS_MAX),				\
		BCH_SB_DATA_REPLICAS_REQ,	1)			\
	BCH_OPT(metadata_checksum,	u8,	OPT_RUNTIME,		\
		OPT_STR(bch2_csum_types),				\
		BCH_SB_META_CSUM_TYPE,		BCH_CSUM_OPT_CRC32C)	\
	BCH_OPT(data_checksum,		u8,	OPT_RUNTIME,		\
		OPT_STR(bch2_csum_types),				\
		BCH_SB_DATA_CSUM_TYPE,		BCH_CSUM_OPT_CRC32C)	\
	BCH_OPT(compression,		u8,	OPT_RUNTIME,		\
		OPT_STR(bch2_compression_types),			\
		BCH_SB_COMPRESSION_TYPE,	BCH_COMPRESSION_OPT_NONE)\
	BCH_OPT(background_compression,	u8,	OPT_RUNTIME,		\
		OPT_STR(bch2_compression_types),			\
		BCH_SB_BACKGROUND_COMPRESSION_TYPE,BCH_COMPRESSION_OPT_NONE)\
	BCH_OPT(str_hash,		u8,	OPT_RUNTIME,		\
		OPT_STR(bch2_str_hash_types),				\
		BCH_SB_STR_HASH_TYPE,		BCH_STR_HASH_SIPHASH)	\
	BCH_OPT(foreground_target,	u16,	OPT_RUNTIME,		\
		OPT_FN(bch2_opt_target),				\
		BCH_SB_FOREGROUND_TARGET,	0)			\
	BCH_OPT(background_target,	u16,	OPT_RUNTIME,		\
		OPT_FN(bch2_opt_target),				\
		BCH_SB_BACKGROUND_TARGET,	0)			\
	BCH_OPT(promote_target,		u16,	OPT_RUNTIME,		\
		OPT_FN(bch2_opt_target),				\
		BCH_SB_PROMOTE_TARGET,	0)				\
	BCH_OPT(inodes_32bit,		u8,	OPT_RUNTIME,		\
		OPT_BOOL(),						\
		BCH_SB_INODE_32BIT,		false)			\
	BCH_OPT(gc_reserve_percent,	u8,	OPT_RUNTIME,		\
		OPT_UINT(5, 21),					\
		BCH_SB_GC_RESERVE,		8)			\
	BCH_OPT(gc_reserve_bytes,	u64,	OPT_RUNTIME,		\
		OPT_UINT(0, U64_MAX),					\
		BCH_SB_GC_RESERVE_BYTES,	0)			\
	BCH_OPT(root_reserve_percent,	u8,	OPT_MOUNT,		\
		OPT_UINT(0, 100),					\
		BCH_SB_ROOT_RESERVE,		0)			\
	BCH_OPT(wide_macs,		u8,	OPT_RUNTIME,		\
		OPT_BOOL(),						\
		BCH_SB_128_BIT_MACS,		false)			\
	BCH_OPT(acl,			u8,	OPT_MOUNT,		\
		OPT_BOOL(),						\
		BCH_SB_POSIX_ACL,		true)			\
	BCH_OPT(usrquota,		u8,	OPT_MOUNT,		\
		OPT_BOOL(),						\
		BCH_SB_USRQUOTA,		false)			\
	BCH_OPT(grpquota,		u8,	OPT_MOUNT,		\
		OPT_BOOL(),						\
		BCH_SB_GRPQUOTA,		false)			\
	BCH_OPT(prjquota,		u8,	OPT_MOUNT,		\
		OPT_BOOL(),						\
		BCH_SB_PRJQUOTA,		false)			\
	BCH_OPT(degraded,		u8,	OPT_MOUNT,		\
		OPT_BOOL(),						\
		NO_SB_OPT,			false)			\
	BCH_OPT(discard,		u8,	OPT_MOUNT,		\
		OPT_BOOL(),						\
		NO_SB_OPT,			false)			\
	BCH_OPT(verbose_recovery,	u8,	OPT_MOUNT,		\
		OPT_BOOL(),						\
		NO_SB_OPT,			false)			\
	BCH_OPT(verbose_init,		u8,	OPT_MOUNT,		\
		OPT_BOOL(),						\
		NO_SB_OPT,			false)			\
	BCH_OPT(journal_flush_disabled, u8,	OPT_RUNTIME,		\
		OPT_BOOL(),						\
		NO_SB_OPT,			false)			\
	BCH_OPT(fsck,			u8,	OPT_MOUNT,		\
		OPT_BOOL(),						\
		NO_SB_OPT,			true)			\
	BCH_OPT(fix_errors,		u8,	OPT_MOUNT,		\
		OPT_BOOL(),						\
		NO_SB_OPT,			false)			\
	BCH_OPT(nochanges,		u8,	OPT_MOUNT,		\
		OPT_BOOL(),						\
		NO_SB_OPT,			false)			\
	BCH_OPT(noreplay,		u8,	OPT_MOUNT,		\
		OPT_BOOL(),						\
		NO_SB_OPT,			false)			\
	BCH_OPT(norecovery,		u8,	OPT_MOUNT,		\
		OPT_BOOL(),						\
		NO_SB_OPT,			false)			\
	BCH_OPT(noexcl,			u8,	OPT_MOUNT,		\
		OPT_BOOL(),						\
		NO_SB_OPT,			false)			\
	BCH_OPT(sb,			u64,	OPT_MOUNT,		\
		OPT_UINT(0, S64_MAX),					\
		NO_SB_OPT,			BCH_SB_SECTOR)		\
	BCH_OPT(read_only,		u8,	OPT_INTERNAL,		\
		OPT_BOOL(),						\
		NO_SB_OPT,			false)			\
	BCH_OPT(nostart,		u8,	OPT_INTERNAL,		\
		OPT_BOOL(),						\
		NO_SB_OPT,			false)

struct bch_opts {
#define BCH_OPT(_name, _bits, ...)	unsigned _name##_defined:1;
	BCH_OPTS()
#undef BCH_OPT

#define BCH_OPT(_name, _bits, ...)	_bits	_name;
	BCH_OPTS()
#undef BCH_OPT
};

static const struct bch_opts bch2_opts_default = {
#define BCH_OPT(_name, _bits, _mode, _type, _sb_opt, _default)		\
	._name##_defined = true,					\
	._name = _default,						\

	BCH_OPTS()
#undef BCH_OPT
};

#define opt_defined(_opts, _name)	((_opts)._name##_defined)

#define opt_get(_opts, _name)						\
	(opt_defined(_opts, _name) ? (_opts)._name : bch2_opts_default._name)

#define opt_set(_opts, _name, _v)					\
do {									\
	(_opts)._name##_defined = true;					\
	(_opts)._name = _v;						\
} while (0)

static inline struct bch_opts bch2_opts_empty(void)
{
	return (struct bch_opts) { 0 };
}

void bch2_opts_apply(struct bch_opts *, struct bch_opts);

enum bch_opt_id {
#define BCH_OPT(_name, ...)	Opt_##_name,
	BCH_OPTS()
#undef BCH_OPT
	bch2_opts_nr
};

struct bch_fs;
struct printbuf;

struct bch_option {
	struct attribute	attr;
	void			(*set_sb)(struct bch_sb *, u64);
	enum opt_mode		mode;
	enum opt_type		type;

	union {
	struct {
		u64		min, max;
	};
	struct {
		const char * const *choices;
	};
	struct {
		int (*parse)(struct bch_fs *, const char *, u64 *);
		void (*to_text)(struct printbuf *, struct bch_fs *, u64);
	};
	};

};

extern const struct bch_option bch2_opt_table[];

bool bch2_opt_defined_by_id(const struct bch_opts *, enum bch_opt_id);
u64 bch2_opt_get_by_id(const struct bch_opts *, enum bch_opt_id);
void bch2_opt_set_by_id(struct bch_opts *, enum bch_opt_id, u64);

struct bch_opts bch2_opts_from_sb(struct bch_sb *);

int bch2_opt_lookup(const char *);
int bch2_opt_parse(struct bch_fs *, const struct bch_option *, const char *, u64 *);

#define OPT_SHOW_FULL_LIST	(1 << 0)
#define OPT_SHOW_MOUNT_STYLE	(1 << 1)

void bch2_opt_to_text(struct printbuf *, struct bch_fs *,
		      const struct bch_option *, u64, unsigned);

int bch2_opt_check_may_set(struct bch_fs *, int, u64);
int bch2_parse_mount_opts(struct bch_opts *, char *);

/* inode opts: */

#define BCH_INODE_OPTS()					\
	BCH_INODE_OPT(data_checksum,			8)	\
	BCH_INODE_OPT(compression,			8)	\
	BCH_INODE_OPT(background_compression,		8)	\
	BCH_INODE_OPT(data_replicas,			8)	\
	BCH_INODE_OPT(promote_target,			16)	\
	BCH_INODE_OPT(foreground_target,		16)	\
	BCH_INODE_OPT(background_target,		16)

struct bch_io_opts {
#define BCH_INODE_OPT(_name, _bits)	unsigned _name##_defined:1;
	BCH_INODE_OPTS()
#undef BCH_INODE_OPT

#define BCH_INODE_OPT(_name, _bits)	u##_bits _name;
	BCH_INODE_OPTS()
#undef BCH_INODE_OPT
};

struct bch_io_opts bch2_opts_to_inode_opts(struct bch_opts);
struct bch_opts bch2_inode_opts_to_opts(struct bch_io_opts);
void bch2_io_opts_apply(struct bch_io_opts *, struct bch_io_opts);
bool bch2_opt_is_inode_opt(enum bch_opt_id);

#endif /* _BCACHEFS_OPTS_H */