xref: /linux/fs/bcachefs/bcachefs_ioctl.h (revision 4a4b30ea80d8cb5e8c4c62bb86201f4ea0d9b030)
1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef _BCACHEFS_IOCTL_H
3 #define _BCACHEFS_IOCTL_H
4 
5 #include <linux/uuid.h>
6 #include <asm/ioctl.h>
7 #include "bcachefs_format.h"
8 #include "bkey_types.h"
9 
10 /*
11  * Flags common to multiple ioctls:
12  */
13 #define BCH_FORCE_IF_DATA_LOST		(1 << 0)
14 #define BCH_FORCE_IF_METADATA_LOST	(1 << 1)
15 #define BCH_FORCE_IF_DATA_DEGRADED	(1 << 2)
16 #define BCH_FORCE_IF_METADATA_DEGRADED	(1 << 3)
17 
18 #define BCH_FORCE_IF_LOST			\
19 	(BCH_FORCE_IF_DATA_LOST|		\
20 	 BCH_FORCE_IF_METADATA_LOST)
21 #define BCH_FORCE_IF_DEGRADED			\
22 	(BCH_FORCE_IF_DATA_DEGRADED|		\
23 	 BCH_FORCE_IF_METADATA_DEGRADED)
24 
25 /*
26  * If cleared, ioctl that refer to a device pass it as a pointer to a pathname
27  * (e.g. /dev/sda1); if set, the dev field is the device's index within the
28  * filesystem:
29  */
30 #define BCH_BY_INDEX			(1 << 4)
31 
32 /*
33  * For BCH_IOCTL_READ_SUPER: get superblock of a specific device, not filesystem
34  * wide superblock:
35  */
36 #define BCH_READ_DEV			(1 << 5)
37 
38 /* global control dev: */
39 
40 /* These are currently broken, and probably unnecessary: */
41 #if 0
42 #define BCH_IOCTL_ASSEMBLE	_IOW(0xbc, 1, struct bch_ioctl_assemble)
43 #define BCH_IOCTL_INCREMENTAL	_IOW(0xbc, 2, struct bch_ioctl_incremental)
44 
45 struct bch_ioctl_assemble {
46 	__u32			flags;
47 	__u32			nr_devs;
48 	__u64			pad;
49 	__u64			devs[];
50 };
51 
52 struct bch_ioctl_incremental {
53 	__u32			flags;
54 	__u64			pad;
55 	__u64			dev;
56 };
57 #endif
58 
59 /* filesystem ioctls: */
60 
61 #define BCH_IOCTL_QUERY_UUID	_IOR(0xbc,	1,  struct bch_ioctl_query_uuid)
62 
63 /* These only make sense when we also have incremental assembly */
64 #if 0
65 #define BCH_IOCTL_START		_IOW(0xbc,	2,  struct bch_ioctl_start)
66 #define BCH_IOCTL_STOP		_IO(0xbc,	3)
67 #endif
68 
69 #define BCH_IOCTL_DISK_ADD	_IOW(0xbc,	4,  struct bch_ioctl_disk)
70 #define BCH_IOCTL_DISK_REMOVE	_IOW(0xbc,	5,  struct bch_ioctl_disk)
71 #define BCH_IOCTL_DISK_ONLINE	_IOW(0xbc,	6,  struct bch_ioctl_disk)
72 #define BCH_IOCTL_DISK_OFFLINE	_IOW(0xbc,	7,  struct bch_ioctl_disk)
73 #define BCH_IOCTL_DISK_SET_STATE _IOW(0xbc,	8,  struct bch_ioctl_disk_set_state)
74 #define BCH_IOCTL_DATA		_IOW(0xbc,	10, struct bch_ioctl_data)
75 #define BCH_IOCTL_FS_USAGE	_IOWR(0xbc,	11, struct bch_ioctl_fs_usage)
76 #define BCH_IOCTL_DEV_USAGE	_IOWR(0xbc,	11, struct bch_ioctl_dev_usage)
77 #define BCH_IOCTL_READ_SUPER	_IOW(0xbc,	12, struct bch_ioctl_read_super)
78 #define BCH_IOCTL_DISK_GET_IDX	_IOW(0xbc,	13,  struct bch_ioctl_disk_get_idx)
79 #define BCH_IOCTL_DISK_RESIZE	_IOW(0xbc,	14,  struct bch_ioctl_disk_resize)
80 #define BCH_IOCTL_DISK_RESIZE_JOURNAL _IOW(0xbc,15,  struct bch_ioctl_disk_resize_journal)
81 
82 #define BCH_IOCTL_SUBVOLUME_CREATE _IOW(0xbc,	16,  struct bch_ioctl_subvolume)
83 #define BCH_IOCTL_SUBVOLUME_DESTROY _IOW(0xbc,	17,  struct bch_ioctl_subvolume)
84 
85 #define BCH_IOCTL_DEV_USAGE_V2	_IOWR(0xbc,	18, struct bch_ioctl_dev_usage_v2)
86 
87 #define BCH_IOCTL_FSCK_OFFLINE	_IOW(0xbc,	19,  struct bch_ioctl_fsck_offline)
88 #define BCH_IOCTL_FSCK_ONLINE	_IOW(0xbc,	20,  struct bch_ioctl_fsck_online)
89 #define BCH_IOCTL_QUERY_ACCOUNTING _IOW(0xbc,	21,  struct bch_ioctl_query_accounting)
90 #define BCH_IOCTL_QUERY_COUNTERS _IOW(0xbc,	21,  struct bch_ioctl_query_counters)
91 
92 /* ioctl below act on a particular file, not the filesystem as a whole: */
93 
94 #define BCHFS_IOC_REINHERIT_ATTRS	_IOR(0xbc, 64, const char __user *)
95 
96 /*
97  * BCH_IOCTL_QUERY_UUID: get filesystem UUID
98  *
99  * Returns user visible UUID, not internal UUID (which may not ever be changed);
100  * the filesystem's sysfs directory may be found under /sys/fs/bcachefs with
101  * this UUID.
102  */
103 struct bch_ioctl_query_uuid {
104 	__uuid_t		uuid;
105 };
106 
107 #if 0
108 struct bch_ioctl_start {
109 	__u32			flags;
110 	__u32			pad;
111 };
112 #endif
113 
114 /*
115  * BCH_IOCTL_DISK_ADD: add a new device to an existing filesystem
116  *
117  * The specified device must not be open or in use. On success, the new device
118  * will be an online member of the filesystem just like any other member.
119  *
120  * The device must first be prepared by userspace by formatting with a bcachefs
121  * superblock, which is only used for passing in superblock options/parameters
122  * for that device (in struct bch_member). The new device's superblock should
123  * not claim to be a member of any existing filesystem - UUIDs on it will be
124  * ignored.
125  */
126 
127 /*
128  * BCH_IOCTL_DISK_REMOVE: permanently remove a member device from a filesystem
129  *
130  * Any data present on @dev will be permanently deleted, and @dev will be
131  * removed from its slot in the filesystem's list of member devices. The device
132  * may be either offline or offline.
133  *
134  * Will fail removing @dev would leave us with insufficient read write devices
135  * or degraded/unavailable data, unless the approprate BCH_FORCE_IF_* flags are
136  * set.
137  */
138 
139 /*
140  * BCH_IOCTL_DISK_ONLINE: given a disk that is already a member of a filesystem
141  * but is not open (e.g. because we started in degraded mode), bring it online
142  *
143  * all existing data on @dev will be available once the device is online,
144  * exactly as if @dev was present when the filesystem was first mounted
145  */
146 
147 /*
148  * BCH_IOCTL_DISK_OFFLINE: offline a disk, causing the kernel to close that
149  * block device, without removing it from the filesystem (so it can be brought
150  * back online later)
151  *
152  * Data present on @dev will be unavailable while @dev is offline (unless
153  * replicated), but will still be intact and untouched if @dev is brought back
154  * online
155  *
156  * Will fail (similarly to BCH_IOCTL_DISK_SET_STATE) if offlining @dev would
157  * leave us with insufficient read write devices or degraded/unavailable data,
158  * unless the approprate BCH_FORCE_IF_* flags are set.
159  */
160 
161 struct bch_ioctl_disk {
162 	__u32			flags;
163 	__u32			pad;
164 	__u64			dev;
165 };
166 
167 /*
168  * BCH_IOCTL_DISK_SET_STATE: modify state of a member device of a filesystem
169  *
170  * @new_state		- one of the bch_member_state states (rw, ro, failed,
171  *			  spare)
172  *
173  * Will refuse to change member state if we would then have insufficient devices
174  * to write to, or if it would result in degraded data (when @new_state is
175  * failed or spare) unless the appropriate BCH_FORCE_IF_* flags are set.
176  */
177 struct bch_ioctl_disk_set_state {
178 	__u32			flags;
179 	__u8			new_state;
180 	__u8			pad[3];
181 	__u64			dev;
182 };
183 
184 #define BCH_DATA_OPS()			\
185 	x(scrub,		0)	\
186 	x(rereplicate,		1)	\
187 	x(migrate,		2)	\
188 	x(rewrite_old_nodes,	3)	\
189 	x(drop_extra_replicas,	4)
190 
191 enum bch_data_ops {
192 #define x(t, n) BCH_DATA_OP_##t = n,
193 	BCH_DATA_OPS()
194 #undef x
195 	BCH_DATA_OP_NR
196 };
197 
198 /*
199  * BCH_IOCTL_DATA: operations that walk and manipulate filesystem data (e.g.
200  * scrub, rereplicate, migrate).
201  *
202  * This ioctl kicks off a job in the background, and returns a file descriptor.
203  * Reading from the file descriptor returns a struct bch_ioctl_data_event,
204  * indicating current progress, and closing the file descriptor will stop the
205  * job. The file descriptor is O_CLOEXEC.
206  */
207 struct bch_ioctl_data {
208 	__u16			op;
209 	__u8			start_btree;
210 	__u8			end_btree;
211 	__u32			flags;
212 
213 	struct bpos		start_pos;
214 	struct bpos		end_pos;
215 
216 	union {
217 	struct {
218 		__u32		dev;
219 		__u32		data_types;
220 	}			scrub;
221 	struct {
222 		__u32		dev;
223 		__u32		pad;
224 	}			migrate;
225 	struct {
226 		__u64		pad[8];
227 	};
228 	};
229 } __packed __aligned(8);
230 
231 enum bch_data_event {
232 	BCH_DATA_EVENT_PROGRESS	= 0,
233 	/* XXX: add an event for reporting errors */
234 	BCH_DATA_EVENT_NR	= 1,
235 };
236 
237 enum data_progress_data_type_special {
238 	DATA_PROGRESS_DATA_TYPE_phys	= 254,
239 	DATA_PROGRESS_DATA_TYPE_done	= 255,
240 };
241 
242 struct bch_ioctl_data_progress {
243 	__u8			data_type;
244 	__u8			btree_id;
245 	__u8			pad[2];
246 	struct bpos		pos;
247 
248 	__u64			sectors_done;
249 	__u64			sectors_total;
250 	__u64			sectors_error_corrected;
251 	__u64			sectors_error_uncorrected;
252 } __packed __aligned(8);
253 
254 enum bch_ioctl_data_event_ret {
255 	BCH_IOCTL_DATA_EVENT_RET_done		= 1,
256 	BCH_IOCTL_DATA_EVENT_RET_device_offline	= 2,
257 };
258 
259 struct bch_ioctl_data_event {
260 	__u8			type;
261 	__u8			ret;
262 	__u8			pad[6];
263 	union {
264 	struct bch_ioctl_data_progress p;
265 	__u64			pad2[15];
266 	};
267 } __packed __aligned(8);
268 
269 struct bch_replicas_usage {
270 	__u64			sectors;
271 	struct bch_replicas_entry_v1 r;
272 } __packed;
273 
replicas_usage_bytes(struct bch_replicas_usage * u)274 static inline unsigned replicas_usage_bytes(struct bch_replicas_usage *u)
275 {
276 	return offsetof(struct bch_replicas_usage, r) + replicas_entry_bytes(&u->r);
277 }
278 
279 static inline struct bch_replicas_usage *
replicas_usage_next(struct bch_replicas_usage * u)280 replicas_usage_next(struct bch_replicas_usage *u)
281 {
282 	return (void *) u + replicas_usage_bytes(u);
283 }
284 
285 /* Obsolete */
286 /*
287  * BCH_IOCTL_FS_USAGE: query filesystem disk space usage
288  *
289  * Returns disk space usage broken out by data type, number of replicas, and
290  * by component device
291  *
292  * @replica_entries_bytes - size, in bytes, allocated for replica usage entries
293  *
294  * On success, @replica_entries_bytes will be changed to indicate the number of
295  * bytes actually used.
296  *
297  * Returns -ERANGE if @replica_entries_bytes was too small
298  */
299 struct bch_ioctl_fs_usage {
300 	__u64			capacity;
301 	__u64			used;
302 	__u64			online_reserved;
303 	__u64			persistent_reserved[BCH_REPLICAS_MAX];
304 
305 	__u32			replica_entries_bytes;
306 	__u32			pad;
307 
308 	struct bch_replicas_usage replicas[];
309 };
310 
311 /* Obsolete */
312 /*
313  * BCH_IOCTL_DEV_USAGE: query device disk space usage
314  *
315  * Returns disk space usage broken out by data type - both by buckets and
316  * sectors.
317  */
318 struct bch_ioctl_dev_usage {
319 	__u64			dev;
320 	__u32			flags;
321 	__u8			state;
322 	__u8			pad[7];
323 
324 	__u32			bucket_size;
325 	__u64			nr_buckets;
326 
327 	__u64			buckets_ec;
328 
329 	struct bch_ioctl_dev_usage_type {
330 		__u64		buckets;
331 		__u64		sectors;
332 		__u64		fragmented;
333 	}			d[10];
334 };
335 
336 /* Obsolete */
337 struct bch_ioctl_dev_usage_v2 {
338 	__u64			dev;
339 	__u32			flags;
340 	__u8			state;
341 	__u8			nr_data_types;
342 	__u8			pad[6];
343 
344 	__u32			bucket_size;
345 	__u64			nr_buckets;
346 
347 	struct bch_ioctl_dev_usage_type d[];
348 };
349 
350 /*
351  * BCH_IOCTL_READ_SUPER: read filesystem superblock
352  *
353  * Equivalent to reading the superblock directly from the block device, except
354  * avoids racing with the kernel writing the superblock or having to figure out
355  * which block device to read
356  *
357  * @sb		- buffer to read into
358  * @size	- size of userspace allocated buffer
359  * @dev		- device to read superblock for, if BCH_READ_DEV flag is
360  *		  specified
361  *
362  * Returns -ERANGE if buffer provided is too small
363  */
364 struct bch_ioctl_read_super {
365 	__u32			flags;
366 	__u32			pad;
367 	__u64			dev;
368 	__u64			size;
369 	__u64			sb;
370 };
371 
372 /*
373  * BCH_IOCTL_DISK_GET_IDX: give a path to a block device, query filesystem to
374  * determine if disk is a (online) member - if so, returns device's index
375  *
376  * Returns -ENOENT if not found
377  */
378 struct bch_ioctl_disk_get_idx {
379 	__u64			dev;
380 };
381 
382 /*
383  * BCH_IOCTL_DISK_RESIZE: resize filesystem on a device
384  *
385  * @dev		- member to resize
386  * @nbuckets	- new number of buckets
387  */
388 struct bch_ioctl_disk_resize {
389 	__u32			flags;
390 	__u32			pad;
391 	__u64			dev;
392 	__u64			nbuckets;
393 };
394 
395 /*
396  * BCH_IOCTL_DISK_RESIZE_JOURNAL: resize journal on a device
397  *
398  * @dev		- member to resize
399  * @nbuckets	- new number of buckets
400  */
401 struct bch_ioctl_disk_resize_journal {
402 	__u32			flags;
403 	__u32			pad;
404 	__u64			dev;
405 	__u64			nbuckets;
406 };
407 
408 struct bch_ioctl_subvolume {
409 	__u32			flags;
410 	__u32			dirfd;
411 	__u16			mode;
412 	__u16			pad[3];
413 	__u64			dst_ptr;
414 	__u64			src_ptr;
415 };
416 
417 #define BCH_SUBVOL_SNAPSHOT_CREATE	(1U << 0)
418 #define BCH_SUBVOL_SNAPSHOT_RO		(1U << 1)
419 
420 /*
421  * BCH_IOCTL_FSCK_OFFLINE: run fsck from the 'bcachefs fsck' userspace command,
422  * but with the kernel's implementation of fsck:
423  */
424 struct bch_ioctl_fsck_offline {
425 	__u64			flags;
426 	__u64			opts;		/* string */
427 	__u64			nr_devs;
428 	__u64			devs[] __counted_by(nr_devs);
429 };
430 
431 /*
432  * BCH_IOCTL_FSCK_ONLINE: run fsck from the 'bcachefs fsck' userspace command,
433  * but with the kernel's implementation of fsck:
434  */
435 struct bch_ioctl_fsck_online {
436 	__u64			flags;
437 	__u64			opts;		/* string */
438 };
439 
440 /*
441  * BCH_IOCTL_QUERY_ACCOUNTING: query filesystem disk accounting
442  *
443  * Returns disk space usage broken out by data type, number of replicas, and
444  * by component device
445  *
446  * @replica_entries_bytes - size, in bytes, allocated for replica usage entries
447  *
448  * On success, @replica_entries_bytes will be changed to indicate the number of
449  * bytes actually used.
450  *
451  * Returns -ERANGE if @replica_entries_bytes was too small
452  */
453 struct bch_ioctl_query_accounting {
454 	__u64			capacity;
455 	__u64			used;
456 	__u64			online_reserved;
457 
458 	__u32			accounting_u64s; /* input parameter */
459 	__u32			accounting_types_mask; /* input parameter */
460 
461 	struct bkey_i_accounting accounting[];
462 };
463 
464 #define BCH_IOCTL_QUERY_COUNTERS_MOUNT	(1 << 0)
465 
466 struct bch_ioctl_query_counters {
467 	__u16			nr;
468 	__u16			flags;
469 	__u32			pad;
470 	__u64			d[];
471 };
472 
473 #endif /* _BCACHEFS_IOCTL_H */
474