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