1 /* SPDX-License-Identifier: GPL-2.0 WITH Linux-syscall-note */
2 /*
3  * Copyright (C) 2007 Oracle.  All rights reserved.
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU General Public
7  * License v2 as published by the Free Software Foundation.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public
15  * License along with this program; if not, write to the
16  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17  * Boston, MA 021110-1307, USA.
18  */
19 
20 #ifndef _UAPI_LINUX_BTRFS_H
21 #define _UAPI_LINUX_BTRFS_H
22 #include <linux/types.h>
23 #include <sys/ioctl.h>
24 
25 #define BTRFS_IOCTL_MAGIC 0x94
26 #define BTRFS_VOL_NAME_MAX 255
27 #define BTRFS_LABEL_SIZE 256
28 
29 /* this should be 4k */
30 #define BTRFS_PATH_NAME_MAX 4087
31 struct btrfs_ioctl_vol_args {
32 	int64_t fd;
33 	char name[BTRFS_PATH_NAME_MAX + 1];
34 };
35 
36 #define BTRFS_DEVICE_PATH_NAME_MAX	1024
37 #define BTRFS_SUBVOL_NAME_MAX 		4039
38 
39 #ifndef __KERNEL__
40 /* Deprecated since 5.7 */
41 # define BTRFS_SUBVOL_CREATE_ASYNC	(1ULL << 0)
42 #endif
43 #define BTRFS_SUBVOL_RDONLY		(1ULL << 1)
44 #define BTRFS_SUBVOL_QGROUP_INHERIT	(1ULL << 2)
45 
46 #define BTRFS_DEVICE_SPEC_BY_ID		(1ULL << 3)
47 
48 #define BTRFS_SUBVOL_SPEC_BY_ID	(1ULL << 4)
49 
50 #define BTRFS_VOL_ARG_V2_FLAGS_SUPPORTED		\
51 			(BTRFS_SUBVOL_RDONLY |		\
52 			BTRFS_SUBVOL_QGROUP_INHERIT |	\
53 			BTRFS_DEVICE_SPEC_BY_ID |	\
54 			BTRFS_SUBVOL_SPEC_BY_ID)
55 
56 #define BTRFS_FSID_SIZE 16
57 #define BTRFS_UUID_SIZE 16
58 #define BTRFS_UUID_UNPARSED_SIZE	37
59 
60 /*
61  * flags definition for qgroup limits
62  *
63  * Used by:
64  * struct btrfs_qgroup_limit.flags
65  * struct btrfs_qgroup_limit_item.flags
66  */
67 #define BTRFS_QGROUP_LIMIT_MAX_RFER	(1ULL << 0)
68 #define BTRFS_QGROUP_LIMIT_MAX_EXCL	(1ULL << 1)
69 #define BTRFS_QGROUP_LIMIT_RSV_RFER	(1ULL << 2)
70 #define BTRFS_QGROUP_LIMIT_RSV_EXCL	(1ULL << 3)
71 #define BTRFS_QGROUP_LIMIT_RFER_CMPR	(1ULL << 4)
72 #define BTRFS_QGROUP_LIMIT_EXCL_CMPR	(1ULL << 5)
73 
74 struct btrfs_qgroup_limit {
75 	uint64_t	flags;
76 	uint64_t	max_rfer;
77 	uint64_t	max_excl;
78 	uint64_t	rsv_rfer;
79 	uint64_t	rsv_excl;
80 };
81 
82 /*
83  * flags definition for qgroup inheritance
84  *
85  * Used by:
86  * struct btrfs_qgroup_inherit.flags
87  */
88 #define BTRFS_QGROUP_INHERIT_SET_LIMITS	(1ULL << 0)
89 
90 struct btrfs_qgroup_inherit {
91 	uint64_t	flags;
92 	uint64_t	num_qgroups;
93 	uint64_t	num_ref_copies;
94 	uint64_t	num_excl_copies;
95 	struct btrfs_qgroup_limit lim;
96 	uint64_t	qgroups[0];
97 };
98 
99 struct btrfs_ioctl_qgroup_limit_args {
100 	uint64_t	qgroupid;
101 	struct btrfs_qgroup_limit lim;
102 };
103 
104 /*
105  * Arguments for specification of subvolumes or devices, supporting by-name or
106  * by-id and flags
107  *
108  * The set of supported flags depends on the ioctl
109  *
110  * BTRFS_SUBVOL_RDONLY is also provided/consumed by the following ioctls:
111  * - BTRFS_IOC_SUBVOL_GETFLAGS
112  * - BTRFS_IOC_SUBVOL_SETFLAGS
113  */
114 
115 /* Supported flags for BTRFS_IOC_RM_DEV_V2 */
116 #define BTRFS_DEVICE_REMOVE_ARGS_MASK					\
117 	(BTRFS_DEVICE_SPEC_BY_ID)
118 
119 /* Supported flags for BTRFS_IOC_SNAP_CREATE_V2 and BTRFS_IOC_SUBVOL_CREATE_V2 */
120 #define BTRFS_SUBVOL_CREATE_ARGS_MASK					\
121 	 (BTRFS_SUBVOL_RDONLY |						\
122 	 BTRFS_SUBVOL_QGROUP_INHERIT)
123 
124 /* Supported flags for BTRFS_IOC_SNAP_DESTROY_V2 */
125 #define BTRFS_SUBVOL_DELETE_ARGS_MASK					\
126 	(BTRFS_SUBVOL_SPEC_BY_ID)
127 
128 struct btrfs_ioctl_vol_args_v2 {
129 	int64_t fd;
130 	uint64_t transid;
131 	uint64_t flags;
132 	union {
133 		struct {
134 			uint64_t size;
135 			struct btrfs_qgroup_inherit  *qgroup_inherit;
136 		};
137 		uint64_t unused[4];
138 	};
139 	union {
140 		char name[BTRFS_SUBVOL_NAME_MAX + 1];
141 		uint64_t devid;
142 		uint64_t subvolid;
143 	};
144 };
145 
146 /*
147  * structure to report errors and progress to userspace, either as a
148  * result of a finished scrub, a canceled scrub or a progress inquiry
149  */
150 struct btrfs_scrub_progress {
151 	uint64_t data_extents_scrubbed;	/* # of data extents scrubbed */
152 	uint64_t tree_extents_scrubbed;	/* # of tree extents scrubbed */
153 	uint64_t data_bytes_scrubbed;	/* # of data bytes scrubbed */
154 	uint64_t tree_bytes_scrubbed;	/* # of tree bytes scrubbed */
155 	uint64_t read_errors;		/* # of read errors encountered (EIO) */
156 	uint64_t csum_errors;		/* # of failed csum checks */
157 	uint64_t verify_errors;		/* # of occurences, where the metadata
158 					 * of a tree block did not match the
159 					 * expected values, like generation or
160 					 * logical */
161 	uint64_t no_csum;			/* # of 4k data block for which no csum
162 					 * is present, probably the result of
163 					 * data written with nodatasum */
164 	uint64_t csum_discards;		/* # of csum for which no data was found
165 					 * in the extent tree. */
166 	uint64_t super_errors;		/* # of bad super blocks encountered */
167 	uint64_t malloc_errors;		/* # of internal kmalloc errors. These
168 					 * will likely cause an incomplete
169 					 * scrub */
170 	uint64_t uncorrectable_errors;	/* # of errors where either no intact
171 					 * copy was found or the writeback
172 					 * failed */
173 	uint64_t corrected_errors;		/* # of errors corrected */
174 	uint64_t last_physical;		/* last physical address scrubbed. In
175 					 * case a scrub was aborted, this can
176 					 * be used to restart the scrub */
177 	uint64_t unverified_errors;	/* # of occurences where a read for a
178 					 * full (64k) bio failed, but the re-
179 					 * check succeeded for each 4k piece.
180 					 * Intermittent error. */
181 };
182 
183 #define BTRFS_SCRUB_READONLY	1
184 struct btrfs_ioctl_scrub_args {
185 	uint64_t devid;				/* in */
186 	uint64_t start;				/* in */
187 	uint64_t end;				/* in */
188 	uint64_t flags;				/* in */
189 	struct btrfs_scrub_progress progress;	/* out */
190 	/* pad to 1k */
191 	uint64_t unused[(1024-32-sizeof(struct btrfs_scrub_progress))/8];
192 };
193 
194 #define BTRFS_IOCTL_DEV_REPLACE_CONT_READING_FROM_SRCDEV_MODE_ALWAYS	0
195 #define BTRFS_IOCTL_DEV_REPLACE_CONT_READING_FROM_SRCDEV_MODE_AVOID	1
196 struct btrfs_ioctl_dev_replace_start_params {
197 	uint64_t srcdevid;	/* in, if 0, use srcdev_name instead */
198 	uint64_t cont_reading_from_srcdev_mode;	/* in, see #define
199 						 * above */
200 	uint8_t srcdev_name[BTRFS_DEVICE_PATH_NAME_MAX + 1];	/* in */
201 	uint8_t tgtdev_name[BTRFS_DEVICE_PATH_NAME_MAX + 1];	/* in */
202 };
203 
204 #define BTRFS_IOCTL_DEV_REPLACE_STATE_NEVER_STARTED	0
205 #define BTRFS_IOCTL_DEV_REPLACE_STATE_STARTED		1
206 #define BTRFS_IOCTL_DEV_REPLACE_STATE_FINISHED		2
207 #define BTRFS_IOCTL_DEV_REPLACE_STATE_CANCELED		3
208 #define BTRFS_IOCTL_DEV_REPLACE_STATE_SUSPENDED		4
209 struct btrfs_ioctl_dev_replace_status_params {
210 	uint64_t replace_state;	/* out, see #define above */
211 	uint64_t progress_1000;	/* out, 0 <= x <= 1000 */
212 	uint64_t time_started;	/* out, seconds since 1-Jan-1970 */
213 	uint64_t time_stopped;	/* out, seconds since 1-Jan-1970 */
214 	uint64_t num_write_errors;	/* out */
215 	uint64_t num_uncorrectable_read_errors;	/* out */
216 };
217 
218 #define BTRFS_IOCTL_DEV_REPLACE_CMD_START			0
219 #define BTRFS_IOCTL_DEV_REPLACE_CMD_STATUS			1
220 #define BTRFS_IOCTL_DEV_REPLACE_CMD_CANCEL			2
221 #define BTRFS_IOCTL_DEV_REPLACE_RESULT_NO_ERROR			0
222 #define BTRFS_IOCTL_DEV_REPLACE_RESULT_NOT_STARTED		1
223 #define BTRFS_IOCTL_DEV_REPLACE_RESULT_ALREADY_STARTED		2
224 #define BTRFS_IOCTL_DEV_REPLACE_RESULT_SCRUB_INPROGRESS		3
225 struct btrfs_ioctl_dev_replace_args {
226 	uint64_t cmd;	/* in */
227 	uint64_t result;	/* out */
228 
229 	union {
230 		struct btrfs_ioctl_dev_replace_start_params start;
231 		struct btrfs_ioctl_dev_replace_status_params status;
232 	};	/* in/out */
233 
234 	uint64_t spare[64];
235 };
236 
237 struct btrfs_ioctl_dev_info_args {
238 	uint64_t devid;				/* in/out */
239 	uint8_t uuid[BTRFS_UUID_SIZE];		/* in/out */
240 	uint64_t bytes_used;			/* out */
241 	uint64_t total_bytes;			/* out */
242 	uint64_t unused[379];			/* pad to 4k */
243 	uint8_t path[BTRFS_DEVICE_PATH_NAME_MAX];	/* out */
244 };
245 
246 /*
247  * Retrieve information about the filesystem
248  */
249 
250 /* Request information about checksum type and size */
251 #define BTRFS_FS_INFO_FLAG_CSUM_INFO			(1 << 0)
252 
253 /* Request information about filesystem generation */
254 #define BTRFS_FS_INFO_FLAG_GENERATION			(1 << 1)
255 /* Request information about filesystem metadata UUID */
256 #define BTRFS_FS_INFO_FLAG_METADATA_UUID		(1 << 2)
257 
258 struct btrfs_ioctl_fs_info_args {
259 	uint64_t max_id;				/* out */
260 	uint64_t num_devices;			/* out */
261 	uint8_t fsid[BTRFS_FSID_SIZE];		/* out */
262 	uint32_t nodesize;				/* out */
263 	uint32_t sectorsize;			/* out */
264 	uint32_t clone_alignment;			/* out */
265 	/* See BTRFS_FS_INFO_FLAG_* */
266 	uint16_t csum_type;			/* out */
267 	uint16_t csum_size;			/* out */
268 	uint64_t flags;				/* in/out */
269 	uint64_t generation;			/* out */
270 	uint8_t metadata_uuid[BTRFS_FSID_SIZE];	/* out */
271 	uint8_t reserved[944];			/* pad to 1k */
272 };
273 
274 /*
275  * feature flags
276  *
277  * Used by:
278  * struct btrfs_ioctl_feature_flags
279  */
280 #define BTRFS_FEATURE_COMPAT_RO_FREE_SPACE_TREE		(1ULL << 0)
281 /*
282  * Older kernels (< 4.9) on big-endian systems produced broken free space tree
283  * bitmaps, and btrfs-progs also used to corrupt the free space tree (versions
284  * < 4.7.3).  If this bit is clear, then the free space tree cannot be trusted.
285  * btrfs-progs can also intentionally clear this bit to ask the kernel to
286  * rebuild the free space tree, however this might not work on older kernels
287  * that do not know about this bit. If not sure, clear the cache manually on
288  * first mount when booting older kernel versions.
289  */
290 #define BTRFS_FEATURE_COMPAT_RO_FREE_SPACE_TREE_VALID	(1ULL << 1)
291 
292 #define BTRFS_FEATURE_INCOMPAT_MIXED_BACKREF	(1ULL << 0)
293 #define BTRFS_FEATURE_INCOMPAT_DEFAULT_SUBVOL	(1ULL << 1)
294 #define BTRFS_FEATURE_INCOMPAT_MIXED_GROUPS	(1ULL << 2)
295 #define BTRFS_FEATURE_INCOMPAT_COMPRESS_LZO	(1ULL << 3)
296 #define BTRFS_FEATURE_INCOMPAT_COMPRESS_ZSTD	(1ULL << 4)
297 
298 /*
299  * older kernels tried to do bigger metadata blocks, but the
300  * code was pretty buggy.  Lets not let them try anymore.
301  */
302 #define BTRFS_FEATURE_INCOMPAT_BIG_METADATA	(1ULL << 5)
303 
304 #define BTRFS_FEATURE_INCOMPAT_EXTENDED_IREF	(1ULL << 6)
305 #define BTRFS_FEATURE_INCOMPAT_RAID56		(1ULL << 7)
306 #define BTRFS_FEATURE_INCOMPAT_SKINNY_METADATA	(1ULL << 8)
307 #define BTRFS_FEATURE_INCOMPAT_NO_HOLES		(1ULL << 9)
308 #define BTRFS_FEATURE_INCOMPAT_METADATA_UUID	(1ULL << 10)
309 #define BTRFS_FEATURE_INCOMPAT_RAID1C34		(1ULL << 11)
310 #define BTRFS_FEATURE_INCOMPAT_ZONED		(1ULL << 12)
311 
312 struct btrfs_ioctl_feature_flags {
313 	uint64_t compat_flags;
314 	uint64_t compat_ro_flags;
315 	uint64_t incompat_flags;
316 };
317 
318 /* balance control ioctl modes */
319 #define BTRFS_BALANCE_CTL_PAUSE		1
320 #define BTRFS_BALANCE_CTL_CANCEL	2
321 
322 /*
323  * this is packed, because it should be exactly the same as its disk
324  * byte order counterpart (struct btrfs_disk_balance_args)
325  */
326 struct btrfs_balance_args {
327 	uint64_t profiles;
328 	union {
329 		uint64_t usage;
330 		struct {
331 			uint32_t usage_min;
332 			uint32_t usage_max;
333 		};
334 	};
335 	uint64_t devid;
336 	uint64_t pstart;
337 	uint64_t pend;
338 	uint64_t vstart;
339 	uint64_t vend;
340 
341 	uint64_t target;
342 
343 	uint64_t flags;
344 
345 	/*
346 	 * BTRFS_BALANCE_ARGS_LIMIT with value 'limit'
347 	 * BTRFS_BALANCE_ARGS_LIMIT_RANGE - the extend version can use minimum
348 	 * and maximum
349 	 */
350 	union {
351 		uint64_t limit;		/* limit number of processed chunks */
352 		struct {
353 			uint32_t limit_min;
354 			uint32_t limit_max;
355 		};
356 	};
357 
358 	/*
359 	 * Process chunks that cross stripes_min..stripes_max devices,
360 	 * BTRFS_BALANCE_ARGS_STRIPES_RANGE
361 	 */
362 	uint32_t stripes_min;
363 	uint32_t stripes_max;
364 
365 	uint64_t unused[6];
366 } __attribute__ ((__packed__));
367 
368 /* report balance progress to userspace */
369 struct btrfs_balance_progress {
370 	uint64_t expected;		/* estimated # of chunks that will be
371 				 * relocated to fulfill the request */
372 	uint64_t considered;	/* # of chunks we have considered so far */
373 	uint64_t completed;	/* # of chunks relocated so far */
374 };
375 
376 /*
377  * flags definition for balance
378  *
379  * Restriper's general type filter
380  *
381  * Used by:
382  * btrfs_ioctl_balance_args.flags
383  * btrfs_balance_control.flags (internal)
384  */
385 #define BTRFS_BALANCE_DATA		(1ULL << 0)
386 #define BTRFS_BALANCE_SYSTEM		(1ULL << 1)
387 #define BTRFS_BALANCE_METADATA		(1ULL << 2)
388 
389 #define BTRFS_BALANCE_TYPE_MASK		(BTRFS_BALANCE_DATA |	    \
390 					 BTRFS_BALANCE_SYSTEM |	    \
391 					 BTRFS_BALANCE_METADATA)
392 
393 #define BTRFS_BALANCE_FORCE		(1ULL << 3)
394 #define BTRFS_BALANCE_RESUME		(1ULL << 4)
395 
396 /*
397  * flags definitions for per-type balance args
398  *
399  * Balance filters
400  *
401  * Used by:
402  * struct btrfs_balance_args
403  */
404 #define BTRFS_BALANCE_ARGS_PROFILES	(1ULL << 0)
405 #define BTRFS_BALANCE_ARGS_USAGE	(1ULL << 1)
406 #define BTRFS_BALANCE_ARGS_DEVID	(1ULL << 2)
407 #define BTRFS_BALANCE_ARGS_DRANGE	(1ULL << 3)
408 #define BTRFS_BALANCE_ARGS_VRANGE	(1ULL << 4)
409 #define BTRFS_BALANCE_ARGS_LIMIT	(1ULL << 5)
410 #define BTRFS_BALANCE_ARGS_LIMIT_RANGE	(1ULL << 6)
411 #define BTRFS_BALANCE_ARGS_STRIPES_RANGE (1ULL << 7)
412 #define BTRFS_BALANCE_ARGS_USAGE_RANGE	(1ULL << 10)
413 
414 #define BTRFS_BALANCE_ARGS_MASK			\
415 	(BTRFS_BALANCE_ARGS_PROFILES |		\
416 	 BTRFS_BALANCE_ARGS_USAGE |		\
417 	 BTRFS_BALANCE_ARGS_DEVID | 		\
418 	 BTRFS_BALANCE_ARGS_DRANGE |		\
419 	 BTRFS_BALANCE_ARGS_VRANGE |		\
420 	 BTRFS_BALANCE_ARGS_LIMIT |		\
421 	 BTRFS_BALANCE_ARGS_LIMIT_RANGE |	\
422 	 BTRFS_BALANCE_ARGS_STRIPES_RANGE |	\
423 	 BTRFS_BALANCE_ARGS_USAGE_RANGE)
424 
425 /*
426  * Profile changing flags.  When SOFT is set we won't relocate chunk if
427  * it already has the target profile (even though it may be
428  * half-filled).
429  */
430 #define BTRFS_BALANCE_ARGS_CONVERT	(1ULL << 8)
431 #define BTRFS_BALANCE_ARGS_SOFT		(1ULL << 9)
432 
433 
434 /*
435  * flags definition for balance state
436  *
437  * Used by:
438  * struct btrfs_ioctl_balance_args.state
439  */
440 #define BTRFS_BALANCE_STATE_RUNNING	(1ULL << 0)
441 #define BTRFS_BALANCE_STATE_PAUSE_REQ	(1ULL << 1)
442 #define BTRFS_BALANCE_STATE_CANCEL_REQ	(1ULL << 2)
443 
444 struct btrfs_ioctl_balance_args {
445 	uint64_t flags;				/* in/out */
446 	uint64_t state;				/* out */
447 
448 	struct btrfs_balance_args data;		/* in/out */
449 	struct btrfs_balance_args meta;		/* in/out */
450 	struct btrfs_balance_args sys;		/* in/out */
451 
452 	struct btrfs_balance_progress stat;	/* out */
453 
454 	uint64_t unused[72];			/* pad to 1k */
455 };
456 
457 #define BTRFS_INO_LOOKUP_PATH_MAX 4080
458 struct btrfs_ioctl_ino_lookup_args {
459 	uint64_t treeid;
460 	uint64_t objectid;
461 	char name[BTRFS_INO_LOOKUP_PATH_MAX];
462 };
463 
464 #define BTRFS_INO_LOOKUP_USER_PATH_MAX (4080 - BTRFS_VOL_NAME_MAX - 1)
465 struct btrfs_ioctl_ino_lookup_user_args {
466 	/* in, inode number containing the subvolume of 'subvolid' */
467 	uint64_t dirid;
468 	/* in */
469 	uint64_t treeid;
470 	/* out, name of the subvolume of 'treeid' */
471 	char name[BTRFS_VOL_NAME_MAX + 1];
472 	/*
473 	 * out, constructed path from the directory with which the ioctl is
474 	 * called to dirid
475 	 */
476 	char path[BTRFS_INO_LOOKUP_USER_PATH_MAX];
477 };
478 
479 /* Search criteria for the btrfs SEARCH ioctl family. */
480 struct btrfs_ioctl_search_key {
481 	/*
482 	 * The tree we're searching in. 1 is the tree of tree roots, 2 is the
483 	 * extent tree, etc...
484 	 *
485 	 * A special tree_id value of 0 will cause a search in the subvolume
486 	 * tree that the inode which is passed to the ioctl is part of.
487 	 */
488 	uint64_t tree_id;		/* in */
489 
490 	/*
491 	 * When doing a tree search, we're actually taking a slice from a
492 	 * linear search space of 136-bit keys.
493 	 *
494 	 * A full 136-bit tree key is composed as:
495 	 *   (objectid << 72) + (type << 64) + offset
496 	 *
497 	 * The individual min and max values for objectid, type and offset
498 	 * define the min_key and max_key values for the search range. All
499 	 * metadata items with a key in the interval [min_key, max_key] will be
500 	 * returned.
501 	 *
502 	 * Additionally, we can filter the items returned on transaction id of
503 	 * the metadata block they're stored in by specifying a transid range.
504 	 * Be aware that this transaction id only denotes when the metadata
505 	 * page that currently contains the item got written the last time as
506 	 * result of a COW operation.  The number does not have any meaning
507 	 * related to the transaction in which an individual item that is being
508 	 * returned was created or changed.
509 	 */
510 	uint64_t min_objectid;	/* in */
511 	uint64_t max_objectid;	/* in */
512 	uint64_t min_offset;	/* in */
513 	uint64_t max_offset;	/* in */
514 	uint64_t min_transid;	/* in */
515 	uint64_t max_transid;	/* in */
516 	uint32_t min_type;		/* in */
517 	uint32_t max_type;		/* in */
518 
519 	/*
520 	 * input: The maximum amount of results desired.
521 	 * output: The actual amount of items returned, restricted by any of:
522 	 *  - reaching the upper bound of the search range
523 	 *  - reaching the input nr_items amount of items
524 	 *  - completely filling the supplied memory buffer
525 	 */
526 	uint32_t nr_items;		/* in/out */
527 
528 	/* align to 64 bits */
529 	uint32_t unused;
530 
531 	/* some extra for later */
532 	uint64_t unused1;
533 	uint64_t unused2;
534 	uint64_t unused3;
535 	uint64_t unused4;
536 };
537 
538 struct btrfs_ioctl_search_header {
539 	uint64_t transid;
540 	uint64_t objectid;
541 	uint64_t offset;
542 	uint32_t type;
543 	uint32_t len;
544 };
545 
546 #define BTRFS_SEARCH_ARGS_BUFSIZE (4096 - sizeof(struct btrfs_ioctl_search_key))
547 /*
548  * the buf is an array of search headers where
549  * each header is followed by the actual item
550  * the type field is expanded to 32 bits for alignment
551  */
552 struct btrfs_ioctl_search_args {
553 	struct btrfs_ioctl_search_key key;
554 	char buf[BTRFS_SEARCH_ARGS_BUFSIZE];
555 };
556 
557 struct btrfs_ioctl_search_args_v2 {
558 	struct btrfs_ioctl_search_key key; /* in/out - search parameters */
559 	uint64_t buf_size;		   /* in - size of buffer
560 					    * out - on EOVERFLOW: needed size
561 					    *       to store item */
562 	uint64_t buf[0];                       /* out - found items */
563 };
564 
565 struct btrfs_ioctl_clone_range_args {
566   int64_t src_fd;
567   uint64_t src_offset, src_length;
568   uint64_t dest_offset;
569 };
570 
571 /*
572  * flags definition for the defrag range ioctl
573  *
574  * Used by:
575  * struct btrfs_ioctl_defrag_range_args.flags
576  */
577 #define BTRFS_DEFRAG_RANGE_COMPRESS 1
578 #define BTRFS_DEFRAG_RANGE_START_IO 2
579 struct btrfs_ioctl_defrag_range_args {
580 	/* start of the defrag operation */
581 	uint64_t start;
582 
583 	/* number of bytes to defrag, use (u64)-1 to say all */
584 	uint64_t len;
585 
586 	/*
587 	 * flags for the operation, which can include turning
588 	 * on compression for this one defrag
589 	 */
590 	uint64_t flags;
591 
592 	/*
593 	 * any extent bigger than this will be considered
594 	 * already defragged.  Use 0 to take the kernel default
595 	 * Use 1 to say every single extent must be rewritten
596 	 */
597 	uint32_t extent_thresh;
598 
599 	/*
600 	 * which compression method to use if turning on compression
601 	 * for this defrag operation.  If unspecified, zlib will
602 	 * be used
603 	 */
604 	uint32_t compress_type;
605 
606 	/* spare for later */
607 	uint32_t unused[4];
608 };
609 
610 
611 #define BTRFS_SAME_DATA_DIFFERS	1
612 /* For extent-same ioctl */
613 struct btrfs_ioctl_same_extent_info {
614 	int64_t fd;		/* in - destination file */
615 	uint64_t logical_offset;	/* in - start of extent in destination */
616 	uint64_t bytes_deduped;	/* out - total # of bytes we were able
617 				 * to dedupe from this file */
618 	/* status of this dedupe operation:
619 	 * 0 if dedup succeeds
620 	 * < 0 for error
621 	 * == BTRFS_SAME_DATA_DIFFERS if data differs
622 	 */
623 	int32_t status;		/* out - see above description */
624 	uint32_t reserved;
625 };
626 
627 struct btrfs_ioctl_same_args {
628 	uint64_t logical_offset;	/* in - start of extent in source */
629 	uint64_t length;		/* in - length of extent */
630 	uint16_t dest_count;	/* in - total elements in info array */
631 	uint16_t reserved1;
632 	uint32_t reserved2;
633 	struct btrfs_ioctl_same_extent_info info[0];
634 };
635 
636 struct btrfs_ioctl_space_info {
637 	uint64_t flags;
638 	uint64_t total_bytes;
639 	uint64_t used_bytes;
640 };
641 
642 struct btrfs_ioctl_space_args {
643 	uint64_t space_slots;
644 	uint64_t total_spaces;
645 	struct btrfs_ioctl_space_info spaces[0];
646 };
647 
648 struct btrfs_data_container {
649 	uint32_t	bytes_left;	/* out -- bytes not needed to deliver output */
650 	uint32_t	bytes_missing;	/* out -- additional bytes needed for result */
651 	uint32_t	elem_cnt;	/* out */
652 	uint32_t	elem_missed;	/* out */
653 	uint64_t	val[0];		/* out */
654 };
655 
656 struct btrfs_ioctl_ino_path_args {
657 	uint64_t				inum;		/* in */
658 	uint64_t				size;		/* in */
659 	uint64_t				reserved[4];
660 	/* struct btrfs_data_container	*fspath;	   out */
661 	uint64_t				fspath;		/* out */
662 };
663 
664 struct btrfs_ioctl_logical_ino_args {
665 	uint64_t				logical;	/* in */
666 	uint64_t				size;		/* in */
667 	uint64_t				reserved[3];	/* must be 0 for now */
668 	uint64_t				flags;		/* in, v2 only */
669 	/* struct btrfs_data_container	*inodes;	out   */
670 	uint64_t				inodes;
671 };
672 /* Return every ref to the extent, not just those containing logical block.
673  * Requires logical == extent bytenr. */
674 #define BTRFS_LOGICAL_INO_ARGS_IGNORE_OFFSET	(1ULL << 0)
675 
676 enum btrfs_dev_stat_values {
677 	/* disk I/O failure stats */
678 	BTRFS_DEV_STAT_WRITE_ERRS, /* EIO or EREMOTEIO from lower layers */
679 	BTRFS_DEV_STAT_READ_ERRS, /* EIO or EREMOTEIO from lower layers */
680 	BTRFS_DEV_STAT_FLUSH_ERRS, /* EIO or EREMOTEIO from lower layers */
681 
682 	/* stats for indirect indications for I/O failures */
683 	BTRFS_DEV_STAT_CORRUPTION_ERRS, /* checksum error, bytenr error or
684 					 * contents is illegal: this is an
685 					 * indication that the block was damaged
686 					 * during read or write, or written to
687 					 * wrong location or read from wrong
688 					 * location */
689 	BTRFS_DEV_STAT_GENERATION_ERRS, /* an indication that blocks have not
690 					 * been written */
691 
692 	BTRFS_DEV_STAT_VALUES_MAX
693 };
694 
695 /* Reset statistics after reading; needs SYS_ADMIN capability */
696 #define	BTRFS_DEV_STATS_RESET		(1ULL << 0)
697 
698 struct btrfs_ioctl_get_dev_stats {
699 	uint64_t devid;				/* in */
700 	uint64_t nr_items;				/* in/out */
701 	uint64_t flags;				/* in/out */
702 
703 	/* out values: */
704 	uint64_t values[BTRFS_DEV_STAT_VALUES_MAX];
705 
706 	/*
707 	 * This pads the struct to 1032 bytes. It was originally meant to pad to
708 	 * 1024 bytes, but when adding the flags field, the padding calculation
709 	 * was not adjusted.
710 	 */
711 	uint64_t unused[128 - 2 - BTRFS_DEV_STAT_VALUES_MAX];
712 };
713 
714 #define BTRFS_QUOTA_CTL_ENABLE	1
715 #define BTRFS_QUOTA_CTL_DISABLE	2
716 #define BTRFS_QUOTA_CTL_RESCAN__NOTUSED	3
717 struct btrfs_ioctl_quota_ctl_args {
718 	uint64_t cmd;
719 	uint64_t status;
720 };
721 
722 struct btrfs_ioctl_quota_rescan_args {
723 	uint64_t	flags;
724 	uint64_t   progress;
725 	uint64_t   reserved[6];
726 };
727 
728 struct btrfs_ioctl_qgroup_assign_args {
729 	uint64_t assign;
730 	uint64_t src;
731 	uint64_t dst;
732 };
733 
734 struct btrfs_ioctl_qgroup_create_args {
735 	uint64_t create;
736 	uint64_t qgroupid;
737 };
738 struct btrfs_ioctl_timespec {
739 	uint64_t sec;
740 	uint32_t nsec;
741 };
742 
743 struct btrfs_ioctl_received_subvol_args {
744 	char	uuid[BTRFS_UUID_SIZE];	/* in */
745 	uint64_t	stransid;		/* in */
746 	uint64_t	rtransid;		/* out */
747 	struct btrfs_ioctl_timespec stime; /* in */
748 	struct btrfs_ioctl_timespec rtime; /* out */
749 	uint64_t	flags;			/* in */
750 	uint64_t	reserved[16];		/* in */
751 };
752 
753 /*
754  * Caller doesn't want file data in the send stream, even if the
755  * search of clone sources doesn't find an extent. UPDATE_EXTENT
756  * commands will be sent instead of WRITE commands.
757  */
758 #define BTRFS_SEND_FLAG_NO_FILE_DATA		0x1
759 
760 /*
761  * Do not add the leading stream header. Used when multiple snapshots
762  * are sent back to back.
763  */
764 #define BTRFS_SEND_FLAG_OMIT_STREAM_HEADER	0x2
765 
766 /*
767  * Omit the command at the end of the stream that indicated the end
768  * of the stream. This option is used when multiple snapshots are
769  * sent back to back.
770  */
771 #define BTRFS_SEND_FLAG_OMIT_END_CMD		0x4
772 
773 #define BTRFS_SEND_FLAG_MASK \
774 	(BTRFS_SEND_FLAG_NO_FILE_DATA | \
775 	 BTRFS_SEND_FLAG_OMIT_STREAM_HEADER | \
776 	 BTRFS_SEND_FLAG_OMIT_END_CMD)
777 
778 struct btrfs_ioctl_send_args {
779 	int64_t send_fd;			/* in */
780 	uint64_t clone_sources_count;	/* in */
781 	uint64_t  *clone_sources;	/* in */
782 	uint64_t parent_root;		/* in */
783 	uint64_t flags;			/* in */
784 	uint64_t reserved[4];		/* in */
785 };
786 
787 /*
788  * Information about a fs tree root.
789  *
790  * All items are filled by the ioctl
791  */
792 struct btrfs_ioctl_get_subvol_info_args {
793 	/* Id of this subvolume */
794 	uint64_t treeid;
795 
796 	/* Name of this subvolume, used to get the real name at mount point */
797 	char name[BTRFS_VOL_NAME_MAX + 1];
798 
799 	/*
800 	 * Id of the subvolume which contains this subvolume.
801 	 * Zero for top-level subvolume or a deleted subvolume.
802 	 */
803 	uint64_t parent_id;
804 
805 	/*
806 	 * Inode number of the directory which contains this subvolume.
807 	 * Zero for top-level subvolume or a deleted subvolume
808 	 */
809 	uint64_t dirid;
810 
811 	/* Latest transaction id of this subvolume */
812 	uint64_t generation;
813 
814 	/* Flags of this subvolume */
815 	uint64_t flags;
816 
817 	/* UUID of this subvolume */
818 	uint8_t uuid[BTRFS_UUID_SIZE];
819 
820 	/*
821 	 * UUID of the subvolume of which this subvolume is a snapshot.
822 	 * All zero for a non-snapshot subvolume.
823 	 */
824 	uint8_t parent_uuid[BTRFS_UUID_SIZE];
825 
826 	/*
827 	 * UUID of the subvolume from which this subvolume was received.
828 	 * All zero for non-received subvolume.
829 	 */
830 	uint8_t received_uuid[BTRFS_UUID_SIZE];
831 
832 	/* Transaction id indicating when change/create/send/receive happened */
833 	uint64_t ctransid;
834 	uint64_t otransid;
835 	uint64_t stransid;
836 	uint64_t rtransid;
837 	/* Time corresponding to c/o/s/rtransid */
838 	struct btrfs_ioctl_timespec ctime;
839 	struct btrfs_ioctl_timespec otime;
840 	struct btrfs_ioctl_timespec stime;
841 	struct btrfs_ioctl_timespec rtime;
842 
843 	/* Must be zero */
844 	uint64_t reserved[8];
845 };
846 
847 #define BTRFS_MAX_ROOTREF_BUFFER_NUM 255
848 struct btrfs_ioctl_get_subvol_rootref_args {
849 		/* in/out, minimum id of rootref's treeid to be searched */
850 		uint64_t min_treeid;
851 
852 		/* out */
853 		struct {
854 			uint64_t treeid;
855 			uint64_t dirid;
856 		} rootref[BTRFS_MAX_ROOTREF_BUFFER_NUM];
857 
858 		/* out, number of found items */
859 		uint8_t num_items;
860 		uint8_t align[7];
861 };
862 
863 /* Error codes as returned by the kernel */
864 enum btrfs_err_code {
865 	BTRFS_ERROR_DEV_RAID1_MIN_NOT_MET = 1,
866 	BTRFS_ERROR_DEV_RAID10_MIN_NOT_MET,
867 	BTRFS_ERROR_DEV_RAID5_MIN_NOT_MET,
868 	BTRFS_ERROR_DEV_RAID6_MIN_NOT_MET,
869 	BTRFS_ERROR_DEV_TGT_REPLACE,
870 	BTRFS_ERROR_DEV_MISSING_NOT_FOUND,
871 	BTRFS_ERROR_DEV_ONLY_WRITABLE,
872 	BTRFS_ERROR_DEV_EXCL_RUN_IN_PROGRESS,
873 	BTRFS_ERROR_DEV_RAID1C3_MIN_NOT_MET,
874 	BTRFS_ERROR_DEV_RAID1C4_MIN_NOT_MET,
875 };
876 
877 #define BTRFS_IOC_SNAP_CREATE _IOW(BTRFS_IOCTL_MAGIC, 1, \
878 				   struct btrfs_ioctl_vol_args)
879 #define BTRFS_IOC_DEFRAG _IOW(BTRFS_IOCTL_MAGIC, 2, \
880 				   struct btrfs_ioctl_vol_args)
881 #define BTRFS_IOC_RESIZE _IOW(BTRFS_IOCTL_MAGIC, 3, \
882 				   struct btrfs_ioctl_vol_args)
883 #define BTRFS_IOC_SCAN_DEV _IOW(BTRFS_IOCTL_MAGIC, 4, \
884 				   struct btrfs_ioctl_vol_args)
885 #define BTRFS_IOC_FORGET_DEV _IOW(BTRFS_IOCTL_MAGIC, 5, \
886 				   struct btrfs_ioctl_vol_args)
887 /* trans start and trans end are dangerous, and only for
888  * use by applications that know how to avoid the
889  * resulting deadlocks
890  */
891 #define BTRFS_IOC_TRANS_START  _IO(BTRFS_IOCTL_MAGIC, 6)
892 #define BTRFS_IOC_TRANS_END    _IO(BTRFS_IOCTL_MAGIC, 7)
893 #define BTRFS_IOC_SYNC         _IO(BTRFS_IOCTL_MAGIC, 8)
894 
895 #define BTRFS_IOC_CLONE        _IOW(BTRFS_IOCTL_MAGIC, 9, int)
896 #define BTRFS_IOC_ADD_DEV _IOW(BTRFS_IOCTL_MAGIC, 10, \
897 				   struct btrfs_ioctl_vol_args)
898 #define BTRFS_IOC_RM_DEV _IOW(BTRFS_IOCTL_MAGIC, 11, \
899 				   struct btrfs_ioctl_vol_args)
900 #define BTRFS_IOC_BALANCE _IOW(BTRFS_IOCTL_MAGIC, 12, \
901 				   struct btrfs_ioctl_vol_args)
902 
903 #define BTRFS_IOC_CLONE_RANGE _IOW(BTRFS_IOCTL_MAGIC, 13, \
904 				  struct btrfs_ioctl_clone_range_args)
905 
906 #define BTRFS_IOC_SUBVOL_CREATE _IOW(BTRFS_IOCTL_MAGIC, 14, \
907 				   struct btrfs_ioctl_vol_args)
908 #define BTRFS_IOC_SNAP_DESTROY _IOW(BTRFS_IOCTL_MAGIC, 15, \
909 				struct btrfs_ioctl_vol_args)
910 #define BTRFS_IOC_DEFRAG_RANGE _IOW(BTRFS_IOCTL_MAGIC, 16, \
911 				struct btrfs_ioctl_defrag_range_args)
912 #define BTRFS_IOC_TREE_SEARCH _IOWR(BTRFS_IOCTL_MAGIC, 17, \
913 				   struct btrfs_ioctl_search_args)
914 #define BTRFS_IOC_TREE_SEARCH_V2 _IOWR(BTRFS_IOCTL_MAGIC, 17, \
915 					   struct btrfs_ioctl_search_args_v2)
916 #define BTRFS_IOC_INO_LOOKUP _IOWR(BTRFS_IOCTL_MAGIC, 18, \
917 				   struct btrfs_ioctl_ino_lookup_args)
918 #define BTRFS_IOC_DEFAULT_SUBVOL _IOW(BTRFS_IOCTL_MAGIC, 19, uint64_t)
919 #define BTRFS_IOC_SPACE_INFO _IOWR(BTRFS_IOCTL_MAGIC, 20, \
920 				    struct btrfs_ioctl_space_args)
921 #define BTRFS_IOC_START_SYNC _IOR(BTRFS_IOCTL_MAGIC, 24, uint64_t)
922 #define BTRFS_IOC_WAIT_SYNC  _IOW(BTRFS_IOCTL_MAGIC, 22, uint64_t)
923 #define BTRFS_IOC_SNAP_CREATE_V2 _IOW(BTRFS_IOCTL_MAGIC, 23, \
924 				   struct btrfs_ioctl_vol_args_v2)
925 #define BTRFS_IOC_SUBVOL_CREATE_V2 _IOW(BTRFS_IOCTL_MAGIC, 24, \
926 				   struct btrfs_ioctl_vol_args_v2)
927 #define BTRFS_IOC_SUBVOL_GETFLAGS _IOR(BTRFS_IOCTL_MAGIC, 25, uint64_t)
928 #define BTRFS_IOC_SUBVOL_SETFLAGS _IOW(BTRFS_IOCTL_MAGIC, 26, uint64_t)
929 #define BTRFS_IOC_SCRUB _IOWR(BTRFS_IOCTL_MAGIC, 27, \
930 			      struct btrfs_ioctl_scrub_args)
931 #define BTRFS_IOC_SCRUB_CANCEL _IO(BTRFS_IOCTL_MAGIC, 28)
932 #define BTRFS_IOC_SCRUB_PROGRESS _IOWR(BTRFS_IOCTL_MAGIC, 29, \
933 				       struct btrfs_ioctl_scrub_args)
934 #define BTRFS_IOC_DEV_INFO _IOWR(BTRFS_IOCTL_MAGIC, 30, \
935 				 struct btrfs_ioctl_dev_info_args)
936 #define BTRFS_IOC_FS_INFO _IOR(BTRFS_IOCTL_MAGIC, 31, \
937 			       struct btrfs_ioctl_fs_info_args)
938 #define BTRFS_IOC_BALANCE_V2 _IOWR(BTRFS_IOCTL_MAGIC, 32, \
939 				   struct btrfs_ioctl_balance_args)
940 #define BTRFS_IOC_BALANCE_CTL _IOW(BTRFS_IOCTL_MAGIC, 33, int)
941 #define BTRFS_IOC_BALANCE_PROGRESS _IOR(BTRFS_IOCTL_MAGIC, 34, \
942 					struct btrfs_ioctl_balance_args)
943 #define BTRFS_IOC_INO_PATHS _IOWR(BTRFS_IOCTL_MAGIC, 35, \
944 					struct btrfs_ioctl_ino_path_args)
945 #define BTRFS_IOC_LOGICAL_INO _IOWR(BTRFS_IOCTL_MAGIC, 36, \
946 					struct btrfs_ioctl_logical_ino_args)
947 #define BTRFS_IOC_SET_RECEIVED_SUBVOL _IOWR(BTRFS_IOCTL_MAGIC, 37, \
948 				struct btrfs_ioctl_received_subvol_args)
949 #define BTRFS_IOC_SEND _IOW(BTRFS_IOCTL_MAGIC, 38, struct btrfs_ioctl_send_args)
950 #define BTRFS_IOC_DEVICES_READY _IOR(BTRFS_IOCTL_MAGIC, 39, \
951 				     struct btrfs_ioctl_vol_args)
952 #define BTRFS_IOC_QUOTA_CTL _IOWR(BTRFS_IOCTL_MAGIC, 40, \
953 			       struct btrfs_ioctl_quota_ctl_args)
954 #define BTRFS_IOC_QGROUP_ASSIGN _IOW(BTRFS_IOCTL_MAGIC, 41, \
955 			       struct btrfs_ioctl_qgroup_assign_args)
956 #define BTRFS_IOC_QGROUP_CREATE _IOW(BTRFS_IOCTL_MAGIC, 42, \
957 			       struct btrfs_ioctl_qgroup_create_args)
958 #define BTRFS_IOC_QGROUP_LIMIT _IOR(BTRFS_IOCTL_MAGIC, 43, \
959 			       struct btrfs_ioctl_qgroup_limit_args)
960 #define BTRFS_IOC_QUOTA_RESCAN _IOW(BTRFS_IOCTL_MAGIC, 44, \
961 			       struct btrfs_ioctl_quota_rescan_args)
962 #define BTRFS_IOC_QUOTA_RESCAN_STATUS _IOR(BTRFS_IOCTL_MAGIC, 45, \
963 			       struct btrfs_ioctl_quota_rescan_args)
964 #define BTRFS_IOC_QUOTA_RESCAN_WAIT _IO(BTRFS_IOCTL_MAGIC, 46)
965 #define BTRFS_IOC_GET_FSLABEL 	FS_IOC_GETFSLABEL
966 #define BTRFS_IOC_SET_FSLABEL	FS_IOC_SETFSLABEL
967 #define BTRFS_IOC_GET_DEV_STATS _IOWR(BTRFS_IOCTL_MAGIC, 52, \
968 				      struct btrfs_ioctl_get_dev_stats)
969 #define BTRFS_IOC_DEV_REPLACE _IOWR(BTRFS_IOCTL_MAGIC, 53, \
970 				    struct btrfs_ioctl_dev_replace_args)
971 #define BTRFS_IOC_FILE_EXTENT_SAME _IOWR(BTRFS_IOCTL_MAGIC, 54, \
972 					 struct btrfs_ioctl_same_args)
973 #define BTRFS_IOC_GET_FEATURES _IOR(BTRFS_IOCTL_MAGIC, 57, \
974 				   struct btrfs_ioctl_feature_flags)
975 #define BTRFS_IOC_SET_FEATURES _IOW(BTRFS_IOCTL_MAGIC, 57, \
976 				   struct btrfs_ioctl_feature_flags[2])
977 #define BTRFS_IOC_GET_SUPPORTED_FEATURES _IOR(BTRFS_IOCTL_MAGIC, 57, \
978 				   struct btrfs_ioctl_feature_flags[3])
979 #define BTRFS_IOC_RM_DEV_V2 _IOW(BTRFS_IOCTL_MAGIC, 58, \
980 				   struct btrfs_ioctl_vol_args_v2)
981 #define BTRFS_IOC_LOGICAL_INO_V2 _IOWR(BTRFS_IOCTL_MAGIC, 59, \
982 					struct btrfs_ioctl_logical_ino_args)
983 #define BTRFS_IOC_GET_SUBVOL_INFO _IOR(BTRFS_IOCTL_MAGIC, 60, \
984 				struct btrfs_ioctl_get_subvol_info_args)
985 #define BTRFS_IOC_GET_SUBVOL_ROOTREF _IOWR(BTRFS_IOCTL_MAGIC, 61, \
986 				struct btrfs_ioctl_get_subvol_rootref_args)
987 #define BTRFS_IOC_INO_LOOKUP_USER _IOWR(BTRFS_IOCTL_MAGIC, 62, \
988 				struct btrfs_ioctl_ino_lookup_user_args)
989 #define BTRFS_IOC_SNAP_DESTROY_V2 _IOW(BTRFS_IOCTL_MAGIC, 63, \
990 				struct btrfs_ioctl_vol_args_v2)
991 
992 #endif /* _UAPI_LINUX_BTRFS_H */
993