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