]> git.sesse.net Git - bcachefs-tools-debian/blob - libbcachefs/bcachefs_ioctl.h
New upstream snapshot
[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 /* ioctl below act on a particular file, not the filesystem as a whole: */
82
83 #define BCHFS_IOC_REINHERIT_ATTRS       _IOR(0xbc, 64, const char __user *)
84
85 /*
86  * BCH_IOCTL_QUERY_UUID: get filesystem UUID
87  *
88  * Returns user visible UUID, not internal UUID (which may not ever be changed);
89  * the filesystem's sysfs directory may be found under /sys/fs/bcachefs with
90  * this UUID.
91  */
92 struct bch_ioctl_query_uuid {
93         uuid_le                 uuid;
94 };
95
96 #if 0
97 struct bch_ioctl_start {
98         __u32                   flags;
99         __u32                   pad;
100 };
101 #endif
102
103 /*
104  * BCH_IOCTL_DISK_ADD: add a new device to an existing filesystem
105  *
106  * The specified device must not be open or in use. On success, the new device
107  * will be an online member of the filesystem just like any other member.
108  *
109  * The device must first be prepared by userspace by formatting with a bcachefs
110  * superblock, which is only used for passing in superblock options/parameters
111  * for that device (in struct bch_member). The new device's superblock should
112  * not claim to be a member of any existing filesystem - UUIDs on it will be
113  * ignored.
114  */
115
116 /*
117  * BCH_IOCTL_DISK_REMOVE: permanently remove a member device from a filesystem
118  *
119  * Any data present on @dev will be permanently deleted, and @dev will be
120  * removed from its slot in the filesystem's list of member devices. The device
121  * may be either offline or offline.
122  *
123  * Will fail removing @dev would leave us with insufficient read write devices
124  * or degraded/unavailable data, unless the approprate BCH_FORCE_IF_* flags are
125  * set.
126  */
127
128 /*
129  * BCH_IOCTL_DISK_ONLINE: given a disk that is already a member of a filesystem
130  * but is not open (e.g. because we started in degraded mode), bring it online
131  *
132  * all existing data on @dev will be available once the device is online,
133  * exactly as if @dev was present when the filesystem was first mounted
134  */
135
136 /*
137  * BCH_IOCTL_DISK_OFFLINE: offline a disk, causing the kernel to close that
138  * block device, without removing it from the filesystem (so it can be brought
139  * back online later)
140  *
141  * Data present on @dev will be unavailable while @dev is offline (unless
142  * replicated), but will still be intact and untouched if @dev is brought back
143  * online
144  *
145  * Will fail (similarly to BCH_IOCTL_DISK_SET_STATE) if offlining @dev would
146  * leave us with insufficient read write devices or degraded/unavailable data,
147  * unless the approprate BCH_FORCE_IF_* flags are set.
148  */
149
150 struct bch_ioctl_disk {
151         __u32                   flags;
152         __u32                   pad;
153         __u64                   dev;
154 };
155
156 /*
157  * BCH_IOCTL_DISK_SET_STATE: modify state of a member device of a filesystem
158  *
159  * @new_state           - one of the bch_member_state states (rw, ro, failed,
160  *                        spare)
161  *
162  * Will refuse to change member state if we would then have insufficient devices
163  * to write to, or if it would result in degraded data (when @new_state is
164  * failed or spare) unless the appropriate BCH_FORCE_IF_* flags are set.
165  */
166 struct bch_ioctl_disk_set_state {
167         __u32                   flags;
168         __u8                    new_state;
169         __u8                    pad[3];
170         __u64                   dev;
171 };
172
173 enum bch_data_ops {
174         BCH_DATA_OP_SCRUB               = 0,
175         BCH_DATA_OP_REREPLICATE         = 1,
176         BCH_DATA_OP_MIGRATE             = 2,
177         BCH_DATA_OP_REWRITE_OLD_NODES   = 3,
178         BCH_DATA_OP_NR                  = 4,
179 };
180
181 /*
182  * BCH_IOCTL_DATA: operations that walk and manipulate filesystem data (e.g.
183  * scrub, rereplicate, migrate).
184  *
185  * This ioctl kicks off a job in the background, and returns a file descriptor.
186  * Reading from the file descriptor returns a struct bch_ioctl_data_event,
187  * indicating current progress, and closing the file descriptor will stop the
188  * job. The file descriptor is O_CLOEXEC.
189  */
190 struct bch_ioctl_data {
191         __u16                   op;
192         __u8                    start_btree;
193         __u8                    end_btree;
194         __u32                   flags;
195
196         struct bpos             start_pos;
197         struct bpos             end_pos;
198
199         union {
200         struct {
201                 __u32           dev;
202                 __u32           pad;
203         }                       migrate;
204         struct {
205                 __u64           pad[8];
206         };
207         };
208 } __attribute__((packed, aligned(8)));
209
210 enum bch_data_event {
211         BCH_DATA_EVENT_PROGRESS = 0,
212         /* XXX: add an event for reporting errors */
213         BCH_DATA_EVENT_NR       = 1,
214 };
215
216 struct bch_ioctl_data_progress {
217         __u8                    data_type;
218         __u8                    btree_id;
219         __u8                    pad[2];
220         struct bpos             pos;
221
222         __u64                   sectors_done;
223         __u64                   sectors_total;
224 } __attribute__((packed, aligned(8)));
225
226 struct bch_ioctl_data_event {
227         __u8                    type;
228         __u8                    pad[7];
229         union {
230         struct bch_ioctl_data_progress p;
231         __u64                   pad2[15];
232         };
233 } __attribute__((packed, aligned(8)));
234
235 struct bch_replicas_usage {
236         __u64                   sectors;
237         struct bch_replicas_entry r;
238 } __attribute__((packed));
239
240 static inline struct bch_replicas_usage *
241 replicas_usage_next(struct bch_replicas_usage *u)
242 {
243         return (void *) u + replicas_entry_bytes(&u->r) + 8;
244 }
245
246 /*
247  * BCH_IOCTL_FS_USAGE: query filesystem disk space usage
248  *
249  * Returns disk space usage broken out by data type, number of replicas, and
250  * by component device
251  *
252  * @replica_entries_bytes - size, in bytes, allocated for replica usage entries
253  *
254  * On success, @replica_entries_bytes will be changed to indicate the number of
255  * bytes actually used.
256  *
257  * Returns -ERANGE if @replica_entries_bytes was too small
258  */
259 struct bch_ioctl_fs_usage {
260         __u64                   capacity;
261         __u64                   used;
262         __u64                   online_reserved;
263         __u64                   persistent_reserved[BCH_REPLICAS_MAX];
264
265         __u32                   replica_entries_bytes;
266         __u32                   pad;
267
268         struct bch_replicas_usage replicas[0];
269 };
270
271 /*
272  * BCH_IOCTL_DEV_USAGE: query device disk space usage
273  *
274  * Returns disk space usage broken out by data type - both by buckets and
275  * sectors.
276  */
277 struct bch_ioctl_dev_usage {
278         __u64                   dev;
279         __u32                   flags;
280         __u8                    state;
281         __u8                    pad[7];
282
283         __u32                   bucket_size;
284         __u64                   nr_buckets;
285         __u64                   available_buckets;
286
287         __u64                   buckets[BCH_DATA_NR];
288         __u64                   sectors[BCH_DATA_NR];
289
290         __u64                   ec_buckets;
291         __u64                   ec_sectors;
292 };
293
294 /*
295  * BCH_IOCTL_READ_SUPER: read filesystem superblock
296  *
297  * Equivalent to reading the superblock directly from the block device, except
298  * avoids racing with the kernel writing the superblock or having to figure out
299  * which block device to read
300  *
301  * @sb          - buffer to read into
302  * @size        - size of userspace allocated buffer
303  * @dev         - device to read superblock for, if BCH_READ_DEV flag is
304  *                specified
305  *
306  * Returns -ERANGE if buffer provided is too small
307  */
308 struct bch_ioctl_read_super {
309         __u32                   flags;
310         __u32                   pad;
311         __u64                   dev;
312         __u64                   size;
313         __u64                   sb;
314 };
315
316 /*
317  * BCH_IOCTL_DISK_GET_IDX: give a path to a block device, query filesystem to
318  * determine if disk is a (online) member - if so, returns device's index
319  *
320  * Returns -ENOENT if not found
321  */
322 struct bch_ioctl_disk_get_idx {
323         __u64                   dev;
324 };
325
326 /*
327  * BCH_IOCTL_DISK_RESIZE: resize filesystem on a device
328  *
329  * @dev         - member to resize
330  * @nbuckets    - new number of buckets
331  */
332 struct bch_ioctl_disk_resize {
333         __u32                   flags;
334         __u32                   pad;
335         __u64                   dev;
336         __u64                   nbuckets;
337 };
338
339 /*
340  * BCH_IOCTL_DISK_RESIZE_JOURNAL: resize journal on a device
341  *
342  * @dev         - member to resize
343  * @nbuckets    - new number of buckets
344  */
345 struct bch_ioctl_disk_resize_journal {
346         __u32                   flags;
347         __u32                   pad;
348         __u64                   dev;
349         __u64                   nbuckets;
350 };
351
352 #endif /* _BCACHEFS_IOCTL_H */