xref: /linux/fs/btrfs/ioctl.c (revision c27e360545373b7aee9862a5beef3b9fb3df0c25)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Copyright (C) 2007 Oracle.  All rights reserved.
4  */
5 
6 #include <linux/kernel.h>
7 #include <linux/bio.h>
8 #include <linux/file.h>
9 #include <linux/fs.h>
10 #include <linux/fsnotify.h>
11 #include <linux/pagemap.h>
12 #include <linux/highmem.h>
13 #include <linux/time.h>
14 #include <linux/string.h>
15 #include <linux/backing-dev.h>
16 #include <linux/mount.h>
17 #include <linux/namei.h>
18 #include <linux/writeback.h>
19 #include <linux/compat.h>
20 #include <linux/security.h>
21 #include <linux/xattr.h>
22 #include <linux/mm.h>
23 #include <linux/slab.h>
24 #include <linux/blkdev.h>
25 #include <linux/uuid.h>
26 #include <linux/btrfs.h>
27 #include <linux/uaccess.h>
28 #include <linux/iversion.h>
29 #include <linux/fileattr.h>
30 #include <linux/fsverity.h>
31 #include <linux/sched/xacct.h>
32 #include <linux/io_uring/cmd.h>
33 #include "ctree.h"
34 #include "disk-io.h"
35 #include "export.h"
36 #include "transaction.h"
37 #include "btrfs_inode.h"
38 #include "volumes.h"
39 #include "locking.h"
40 #include "backref.h"
41 #include "send.h"
42 #include "dev-replace.h"
43 #include "props.h"
44 #include "sysfs.h"
45 #include "qgroup.h"
46 #include "tree-log.h"
47 #include "compression.h"
48 #include "space-info.h"
49 #include "block-group.h"
50 #include "fs.h"
51 #include "accessors.h"
52 #include "extent-tree.h"
53 #include "root-tree.h"
54 #include "defrag.h"
55 #include "dir-item.h"
56 #include "uuid-tree.h"
57 #include "ioctl.h"
58 #include "file.h"
59 #include "file-item.h"
60 #include "scrub.h"
61 #include "super.h"
62 
63 #ifdef CONFIG_64BIT
64 /* If we have a 32-bit userspace and 64-bit kernel, then the UAPI
65  * structures are incorrect, as the timespec structure from userspace
66  * is 4 bytes too small. We define these alternatives here to teach
67  * the kernel about the 32-bit struct packing.
68  */
69 struct btrfs_ioctl_timespec_32 {
70 	__u64 sec;
71 	__u32 nsec;
72 } __attribute__ ((__packed__));
73 
74 struct btrfs_ioctl_received_subvol_args_32 {
75 	char	uuid[BTRFS_UUID_SIZE];	/* in */
76 	__u64	stransid;		/* in */
77 	__u64	rtransid;		/* out */
78 	struct btrfs_ioctl_timespec_32 stime; /* in */
79 	struct btrfs_ioctl_timespec_32 rtime; /* out */
80 	__u64	flags;			/* in */
81 	__u64	reserved[16];		/* in */
82 } __attribute__ ((__packed__));
83 
84 #define BTRFS_IOC_SET_RECEIVED_SUBVOL_32 _IOWR(BTRFS_IOCTL_MAGIC, 37, \
85 				struct btrfs_ioctl_received_subvol_args_32)
86 
87 struct btrfs_ioctl_get_subvol_info_args_32 {
88 	__u64 treeid;
89 	char name[BTRFS_VOL_NAME_MAX + 1];
90 	__u64 parent_id;
91 	__u64 dirid;
92 	__u64 generation;
93 	__u64 flags;
94 	__u8 uuid[BTRFS_UUID_SIZE];
95 	__u8 parent_uuid[BTRFS_UUID_SIZE];
96 	__u8 received_uuid[BTRFS_UUID_SIZE];
97 	__u64 ctransid;
98 	__u64 otransid;
99 	__u64 stransid;
100 	__u64 rtransid;
101 	struct btrfs_ioctl_timespec_32 ctime;
102 	struct btrfs_ioctl_timespec_32 otime;
103 	struct btrfs_ioctl_timespec_32 stime;
104 	struct btrfs_ioctl_timespec_32 rtime;
105 	__u64 reserved[8];
106 } __attribute__ ((__packed__));
107 
108 #define BTRFS_IOC_GET_SUBVOL_INFO_32 _IOR(BTRFS_IOCTL_MAGIC, 60, \
109 				struct btrfs_ioctl_get_subvol_info_args_32)
110 #endif
111 
112 #if defined(CONFIG_64BIT) && defined(CONFIG_COMPAT)
113 struct btrfs_ioctl_send_args_32 {
114 	__s64 send_fd;			/* in */
115 	__u64 clone_sources_count;	/* in */
116 	compat_uptr_t clone_sources;	/* in */
117 	__u64 parent_root;		/* in */
118 	__u64 flags;			/* in */
119 	__u32 version;			/* in */
120 	__u8  reserved[28];		/* in */
121 } __attribute__ ((__packed__));
122 
123 #define BTRFS_IOC_SEND_32 _IOW(BTRFS_IOCTL_MAGIC, 38, \
124 			       struct btrfs_ioctl_send_args_32)
125 
126 struct btrfs_ioctl_encoded_io_args_32 {
127 	compat_uptr_t iov;
128 	compat_ulong_t iovcnt;
129 	__s64 offset;
130 	__u64 flags;
131 	__u64 len;
132 	__u64 unencoded_len;
133 	__u64 unencoded_offset;
134 	__u32 compression;
135 	__u32 encryption;
136 	__u8 reserved[64];
137 };
138 
139 #define BTRFS_IOC_ENCODED_READ_32 _IOR(BTRFS_IOCTL_MAGIC, 64, \
140 				       struct btrfs_ioctl_encoded_io_args_32)
141 #define BTRFS_IOC_ENCODED_WRITE_32 _IOW(BTRFS_IOCTL_MAGIC, 64, \
142 					struct btrfs_ioctl_encoded_io_args_32)
143 #endif
144 
145 /* Mask out flags that are inappropriate for the given type of inode. */
146 static unsigned int btrfs_mask_fsflags_for_type(const struct inode *inode,
147 						unsigned int flags)
148 {
149 	if (S_ISDIR(inode->i_mode))
150 		return flags;
151 	else if (S_ISREG(inode->i_mode))
152 		return flags & ~FS_DIRSYNC_FL;
153 	else
154 		return flags & (FS_NODUMP_FL | FS_NOATIME_FL);
155 }
156 
157 /*
158  * Export internal inode flags to the format expected by the FS_IOC_GETFLAGS
159  * ioctl.
160  */
161 static unsigned int btrfs_inode_flags_to_fsflags(const struct btrfs_inode *inode)
162 {
163 	unsigned int iflags = 0;
164 	u32 flags = inode->flags;
165 	u32 ro_flags = inode->ro_flags;
166 
167 	if (flags & BTRFS_INODE_SYNC)
168 		iflags |= FS_SYNC_FL;
169 	if (flags & BTRFS_INODE_IMMUTABLE)
170 		iflags |= FS_IMMUTABLE_FL;
171 	if (flags & BTRFS_INODE_APPEND)
172 		iflags |= FS_APPEND_FL;
173 	if (flags & BTRFS_INODE_NODUMP)
174 		iflags |= FS_NODUMP_FL;
175 	if (flags & BTRFS_INODE_NOATIME)
176 		iflags |= FS_NOATIME_FL;
177 	if (flags & BTRFS_INODE_DIRSYNC)
178 		iflags |= FS_DIRSYNC_FL;
179 	if (flags & BTRFS_INODE_NODATACOW)
180 		iflags |= FS_NOCOW_FL;
181 	if (ro_flags & BTRFS_INODE_RO_VERITY)
182 		iflags |= FS_VERITY_FL;
183 
184 	if (flags & BTRFS_INODE_NOCOMPRESS)
185 		iflags |= FS_NOCOMP_FL;
186 	else if (flags & BTRFS_INODE_COMPRESS)
187 		iflags |= FS_COMPR_FL;
188 
189 	return iflags;
190 }
191 
192 /*
193  * Update inode->i_flags based on the btrfs internal flags.
194  */
195 void btrfs_sync_inode_flags_to_i_flags(struct btrfs_inode *inode)
196 {
197 	unsigned int new_fl = 0;
198 
199 	if (inode->flags & BTRFS_INODE_SYNC)
200 		new_fl |= S_SYNC;
201 	if (inode->flags & BTRFS_INODE_IMMUTABLE)
202 		new_fl |= S_IMMUTABLE;
203 	if (inode->flags & BTRFS_INODE_APPEND)
204 		new_fl |= S_APPEND;
205 	if (inode->flags & BTRFS_INODE_NOATIME)
206 		new_fl |= S_NOATIME;
207 	if (inode->flags & BTRFS_INODE_DIRSYNC)
208 		new_fl |= S_DIRSYNC;
209 	if (inode->ro_flags & BTRFS_INODE_RO_VERITY)
210 		new_fl |= S_VERITY;
211 
212 	set_mask_bits(&inode->vfs_inode.i_flags,
213 		      S_SYNC | S_APPEND | S_IMMUTABLE | S_NOATIME | S_DIRSYNC |
214 		      S_VERITY, new_fl);
215 }
216 
217 /*
218  * Check if @flags are a supported and valid set of FS_*_FL flags and that
219  * the old and new flags are not conflicting
220  */
221 static int check_fsflags(unsigned int old_flags, unsigned int flags)
222 {
223 	if (flags & ~(FS_IMMUTABLE_FL | FS_APPEND_FL | \
224 		      FS_NOATIME_FL | FS_NODUMP_FL | \
225 		      FS_SYNC_FL | FS_DIRSYNC_FL | \
226 		      FS_NOCOMP_FL | FS_COMPR_FL |
227 		      FS_NOCOW_FL))
228 		return -EOPNOTSUPP;
229 
230 	/* COMPR and NOCOMP on new/old are valid */
231 	if ((flags & FS_NOCOMP_FL) && (flags & FS_COMPR_FL))
232 		return -EINVAL;
233 
234 	if ((flags & FS_COMPR_FL) && (flags & FS_NOCOW_FL))
235 		return -EINVAL;
236 
237 	/* NOCOW and compression options are mutually exclusive */
238 	if ((old_flags & FS_NOCOW_FL) && (flags & (FS_COMPR_FL | FS_NOCOMP_FL)))
239 		return -EINVAL;
240 	if ((flags & FS_NOCOW_FL) && (old_flags & (FS_COMPR_FL | FS_NOCOMP_FL)))
241 		return -EINVAL;
242 
243 	return 0;
244 }
245 
246 static int check_fsflags_compatible(const struct btrfs_fs_info *fs_info,
247 				    unsigned int flags)
248 {
249 	if (btrfs_is_zoned(fs_info) && (flags & FS_NOCOW_FL))
250 		return -EPERM;
251 
252 	return 0;
253 }
254 
255 int btrfs_check_ioctl_vol_args_path(const struct btrfs_ioctl_vol_args *vol_args)
256 {
257 	if (memchr(vol_args->name, 0, sizeof(vol_args->name)) == NULL)
258 		return -ENAMETOOLONG;
259 	return 0;
260 }
261 
262 static int btrfs_check_ioctl_vol_args2_subvol_name(const struct btrfs_ioctl_vol_args_v2 *vol_args2)
263 {
264 	if (memchr(vol_args2->name, 0, sizeof(vol_args2->name)) == NULL)
265 		return -ENAMETOOLONG;
266 	return 0;
267 }
268 
269 /*
270  * Set flags/xflags from the internal inode flags. The remaining items of
271  * fsxattr are zeroed.
272  */
273 int btrfs_fileattr_get(struct dentry *dentry, struct file_kattr *fa)
274 {
275 	const struct btrfs_inode *inode = BTRFS_I(d_inode(dentry));
276 
277 	fileattr_fill_flags(fa, btrfs_inode_flags_to_fsflags(inode));
278 	return 0;
279 }
280 
281 int btrfs_fileattr_set(struct mnt_idmap *idmap,
282 		       struct dentry *dentry, struct file_kattr *fa)
283 {
284 	struct btrfs_inode *inode = BTRFS_I(d_inode(dentry));
285 	struct btrfs_root *root = inode->root;
286 	struct btrfs_fs_info *fs_info = root->fs_info;
287 	struct btrfs_trans_handle *trans;
288 	unsigned int fsflags, old_fsflags;
289 	int ret;
290 	const char *comp = NULL;
291 	u32 inode_flags;
292 	bool prop_set = false;
293 
294 	if (btrfs_root_readonly(root))
295 		return -EROFS;
296 
297 	if (fileattr_has_fsx(fa))
298 		return -EOPNOTSUPP;
299 
300 	fsflags = btrfs_mask_fsflags_for_type(&inode->vfs_inode, fa->flags);
301 	old_fsflags = btrfs_inode_flags_to_fsflags(inode);
302 	ret = check_fsflags(old_fsflags, fsflags);
303 	if (ret)
304 		return ret;
305 
306 	ret = check_fsflags_compatible(fs_info, fsflags);
307 	if (ret)
308 		return ret;
309 
310 	inode_flags = inode->flags;
311 	if (fsflags & FS_SYNC_FL)
312 		inode_flags |= BTRFS_INODE_SYNC;
313 	else
314 		inode_flags &= ~BTRFS_INODE_SYNC;
315 	if (fsflags & FS_IMMUTABLE_FL)
316 		inode_flags |= BTRFS_INODE_IMMUTABLE;
317 	else
318 		inode_flags &= ~BTRFS_INODE_IMMUTABLE;
319 	if (fsflags & FS_APPEND_FL)
320 		inode_flags |= BTRFS_INODE_APPEND;
321 	else
322 		inode_flags &= ~BTRFS_INODE_APPEND;
323 	if (fsflags & FS_NODUMP_FL)
324 		inode_flags |= BTRFS_INODE_NODUMP;
325 	else
326 		inode_flags &= ~BTRFS_INODE_NODUMP;
327 	if (fsflags & FS_NOATIME_FL)
328 		inode_flags |= BTRFS_INODE_NOATIME;
329 	else
330 		inode_flags &= ~BTRFS_INODE_NOATIME;
331 
332 	/* If coming from FS_IOC_FSSETXATTR then skip unconverted flags */
333 	if (!fa->flags_valid) {
334 		/* 1 item for the inode */
335 		trans = btrfs_start_transaction(root, 1);
336 		if (IS_ERR(trans))
337 			return PTR_ERR(trans);
338 		goto update_flags;
339 	}
340 
341 	if (fsflags & FS_DIRSYNC_FL)
342 		inode_flags |= BTRFS_INODE_DIRSYNC;
343 	else
344 		inode_flags &= ~BTRFS_INODE_DIRSYNC;
345 	if (fsflags & FS_NOCOW_FL) {
346 		if (S_ISREG(inode->vfs_inode.i_mode)) {
347 			/*
348 			 * It's safe to turn csums off here, no extents exist.
349 			 * Otherwise we want the flag to reflect the real COW
350 			 * status of the file and will not set it.
351 			 */
352 			if (inode->vfs_inode.i_size == 0)
353 				inode_flags |= BTRFS_INODE_NODATACOW |
354 					       BTRFS_INODE_NODATASUM;
355 		} else {
356 			inode_flags |= BTRFS_INODE_NODATACOW;
357 		}
358 	} else {
359 		/*
360 		 * Revert back under same assumptions as above
361 		 */
362 		if (S_ISREG(inode->vfs_inode.i_mode)) {
363 			if (inode->vfs_inode.i_size == 0)
364 				inode_flags &= ~(BTRFS_INODE_NODATACOW |
365 						 BTRFS_INODE_NODATASUM);
366 		} else {
367 			inode_flags &= ~BTRFS_INODE_NODATACOW;
368 		}
369 	}
370 
371 	/*
372 	 * The COMPRESS flag can only be changed by users, while the NOCOMPRESS
373 	 * flag may be changed automatically if compression code won't make
374 	 * things smaller.
375 	 */
376 	if (fsflags & FS_NOCOMP_FL) {
377 		inode_flags &= ~BTRFS_INODE_COMPRESS;
378 		inode_flags |= BTRFS_INODE_NOCOMPRESS;
379 	} else if (fsflags & FS_COMPR_FL) {
380 
381 		if (IS_SWAPFILE(&inode->vfs_inode))
382 			return -ETXTBSY;
383 
384 		inode_flags |= BTRFS_INODE_COMPRESS;
385 		inode_flags &= ~BTRFS_INODE_NOCOMPRESS;
386 
387 		comp = btrfs_compress_type2str(fs_info->compress_type);
388 		if (!comp || comp[0] == 0)
389 			comp = btrfs_compress_type2str(BTRFS_COMPRESS_ZLIB);
390 	} else {
391 		inode_flags &= ~(BTRFS_INODE_COMPRESS | BTRFS_INODE_NOCOMPRESS);
392 	}
393 
394 	/*
395 	 * 1 for inode item
396 	 * 2 for properties
397 	 */
398 	trans = btrfs_start_transaction(root, 3);
399 	if (IS_ERR(trans))
400 		return PTR_ERR(trans);
401 
402 	if (comp) {
403 		ret = btrfs_set_prop(trans, inode, "btrfs.compression",
404 				     comp, strlen(comp), 0);
405 		if (ret)
406 			goto out_end_trans;
407 		prop_set = true;
408 	} else {
409 		ret = btrfs_set_prop(trans, inode, "btrfs.compression", NULL, 0, 0);
410 		prop_set = (ret == 0);
411 		/* If ret == -ENODATA ignore and proceed to update inode item. */
412 		if (ret && ret != -ENODATA)
413 			goto out_end_trans;
414 	}
415 
416 update_flags:
417 	inode->flags = inode_flags;
418 	btrfs_update_inode_mapping_flags(inode);
419 	btrfs_sync_inode_flags_to_i_flags(inode);
420 	inode_inc_iversion(&inode->vfs_inode);
421 	inode_set_ctime_current(&inode->vfs_inode);
422 	ret = btrfs_update_inode(trans, inode);
423 	/*
424 	 * If we set a property or deleted one, we must abort if we fail to
425 	 * update the inode, to avoid persisting an inconsistent state.
426 	 */
427 	if (unlikely(ret && prop_set))
428 		btrfs_abort_transaction(trans, ret);
429 
430  out_end_trans:
431 	btrfs_end_transaction(trans);
432 	return ret;
433 }
434 
435 static int btrfs_ioctl_getversion(const struct inode *inode, int __user *arg)
436 {
437 	return put_user(inode->i_generation, arg);
438 }
439 
440 static noinline int btrfs_ioctl_fitrim(struct btrfs_fs_info *fs_info,
441 					void __user *arg)
442 {
443 	struct btrfs_device *device;
444 	struct fstrim_range range;
445 	u64 minlen = ULLONG_MAX;
446 	u64 num_devices = 0;
447 	int ret;
448 
449 	if (!capable(CAP_SYS_ADMIN))
450 		return -EPERM;
451 
452 	/*
453 	 * btrfs_trim_block_group() depends on space cache, which is not
454 	 * available in zoned filesystem. So, disallow fitrim on a zoned
455 	 * filesystem for now.
456 	 */
457 	if (btrfs_is_zoned(fs_info))
458 		return -EOPNOTSUPP;
459 
460 	/*
461 	 * If the fs is mounted with nologreplay, which requires it to be
462 	 * mounted in RO mode as well, we can not allow discard on free space
463 	 * inside block groups, because log trees refer to extents that are not
464 	 * pinned in a block group's free space cache (pinning the extents is
465 	 * precisely the first phase of replaying a log tree).
466 	 */
467 	if (btrfs_test_opt(fs_info, NOLOGREPLAY))
468 		return -EROFS;
469 
470 	rcu_read_lock();
471 	list_for_each_entry_rcu(device, &fs_info->fs_devices->devices,
472 				dev_list) {
473 		if (!device->bdev || !bdev_max_discard_sectors(device->bdev))
474 			continue;
475 		num_devices++;
476 		minlen = min_t(u64, bdev_discard_granularity(device->bdev),
477 				    minlen);
478 	}
479 	rcu_read_unlock();
480 
481 	if (!num_devices)
482 		return -EOPNOTSUPP;
483 	if (copy_from_user(&range, arg, sizeof(range)))
484 		return -EFAULT;
485 
486 	/*
487 	 * NOTE: Don't truncate the range using super->total_bytes.  Bytenr of
488 	 * block group is in the logical address space, which can be any
489 	 * sectorsize aligned bytenr in  the range [0, U64_MAX].
490 	 */
491 	if (range.len < fs_info->sectorsize)
492 		return -EINVAL;
493 
494 	range.minlen = max(range.minlen, minlen);
495 	ret = btrfs_trim_fs(fs_info, &range);
496 
497 	if (copy_to_user(arg, &range, sizeof(range)))
498 		return -EFAULT;
499 
500 	return ret;
501 }
502 
503 /*
504  * Calculate the number of transaction items to reserve for creating a subvolume
505  * or snapshot, not including the inode, directory entries, or parent directory.
506  */
507 static unsigned int create_subvol_num_items(const struct btrfs_qgroup_inherit *inherit)
508 {
509 	/*
510 	 * 1 to add root block
511 	 * 1 to add root item
512 	 * 1 to add root ref
513 	 * 1 to add root backref
514 	 * 1 to add UUID item
515 	 * 1 to add qgroup info
516 	 * 1 to add qgroup limit
517 	 *
518 	 * Ideally the last two would only be accounted if qgroups are enabled,
519 	 * but that can change between now and the time we would insert them.
520 	 */
521 	unsigned int num_items = 7;
522 
523 	if (inherit) {
524 		/* 2 to add qgroup relations for each inherited qgroup */
525 		num_items += 2 * inherit->num_qgroups;
526 	}
527 	return num_items;
528 }
529 
530 static noinline int create_subvol(struct mnt_idmap *idmap,
531 				  struct inode *dir, struct dentry *dentry,
532 				  struct btrfs_qgroup_inherit *inherit)
533 {
534 	struct btrfs_fs_info *fs_info = inode_to_fs_info(dir);
535 	struct btrfs_trans_handle *trans;
536 	struct btrfs_key key;
537 	struct btrfs_root_item AUTO_KFREE(root_item);
538 	struct btrfs_inode_item *inode_item;
539 	struct extent_buffer *leaf;
540 	struct btrfs_root *root = BTRFS_I(dir)->root;
541 	struct btrfs_root *new_root;
542 	struct btrfs_block_rsv block_rsv;
543 	struct timespec64 cur_time = current_time(dir);
544 	struct btrfs_new_inode_args new_inode_args = {
545 		.dir = dir,
546 		.dentry = dentry,
547 		.subvol = true,
548 	};
549 	unsigned int trans_num_items;
550 	int ret;
551 	dev_t anon_dev;
552 	u64 objectid;
553 	u64 qgroup_reserved = 0;
554 
555 	root_item = kzalloc(sizeof(*root_item), GFP_KERNEL);
556 	if (!root_item)
557 		return -ENOMEM;
558 
559 	ret = btrfs_get_free_objectid(fs_info->tree_root, &objectid);
560 	if (ret)
561 		return ret;
562 
563 	/*
564 	 * Don't create subvolume whose level is not zero. Or qgroup will be
565 	 * screwed up since it assumes subvolume qgroup's level to be 0.
566 	 */
567 	if (btrfs_qgroup_level(objectid))
568 		return -ENOSPC;
569 
570 	ret = get_anon_bdev(&anon_dev);
571 	if (ret < 0)
572 		return ret;
573 
574 	new_inode_args.inode = btrfs_new_subvol_inode(idmap, dir);
575 	if (!new_inode_args.inode) {
576 		ret = -ENOMEM;
577 		goto out_anon_dev;
578 	}
579 	ret = btrfs_new_inode_prepare(&new_inode_args, &trans_num_items);
580 	if (ret)
581 		goto out_inode;
582 	trans_num_items += create_subvol_num_items(inherit);
583 
584 	btrfs_init_block_rsv(&block_rsv, BTRFS_BLOCK_RSV_TEMP);
585 	ret = btrfs_subvolume_reserve_metadata(root, &block_rsv,
586 					       trans_num_items, false);
587 	if (ret)
588 		goto out_new_inode_args;
589 	qgroup_reserved = block_rsv.qgroup_rsv_reserved;
590 
591 	trans = btrfs_start_transaction(root, 0);
592 	if (IS_ERR(trans)) {
593 		ret = PTR_ERR(trans);
594 		goto out_release_rsv;
595 	}
596 	btrfs_qgroup_convert_reserved_meta(root, qgroup_reserved);
597 	qgroup_reserved = 0;
598 	trans->block_rsv = &block_rsv;
599 	trans->bytes_reserved = block_rsv.size;
600 
601 	ret = btrfs_qgroup_inherit(trans, 0, objectid, btrfs_root_id(root), inherit);
602 	if (ret)
603 		goto out;
604 
605 	leaf = btrfs_alloc_tree_block(trans, root, 0, objectid, NULL, 0, 0, 0,
606 				      0, BTRFS_NESTING_NORMAL);
607 	if (IS_ERR(leaf)) {
608 		ret = PTR_ERR(leaf);
609 		goto out;
610 	}
611 
612 	btrfs_mark_buffer_dirty(trans, leaf);
613 
614 	inode_item = &root_item->inode;
615 	btrfs_set_stack_inode_generation(inode_item, 1);
616 	btrfs_set_stack_inode_size(inode_item, 3);
617 	btrfs_set_stack_inode_nlink(inode_item, 1);
618 	btrfs_set_stack_inode_nbytes(inode_item,
619 				     fs_info->nodesize);
620 	btrfs_set_stack_inode_mode(inode_item, S_IFDIR | 0755);
621 
622 	btrfs_set_root_flags(root_item, 0);
623 	btrfs_set_root_limit(root_item, 0);
624 	btrfs_set_stack_inode_flags(inode_item, BTRFS_INODE_ROOT_ITEM_INIT);
625 
626 	btrfs_set_root_bytenr(root_item, leaf->start);
627 	btrfs_set_root_generation(root_item, trans->transid);
628 	btrfs_set_root_level(root_item, 0);
629 	btrfs_set_root_refs(root_item, 1);
630 	btrfs_set_root_used(root_item, leaf->len);
631 	btrfs_set_root_last_snapshot(root_item, 0);
632 
633 	btrfs_set_root_generation_v2(root_item,
634 			btrfs_root_generation(root_item));
635 	generate_random_guid(root_item->uuid);
636 	btrfs_set_stack_timespec_sec(&root_item->otime, cur_time.tv_sec);
637 	btrfs_set_stack_timespec_nsec(&root_item->otime, cur_time.tv_nsec);
638 	root_item->ctime = root_item->otime;
639 	btrfs_set_root_ctransid(root_item, trans->transid);
640 	btrfs_set_root_otransid(root_item, trans->transid);
641 
642 	btrfs_tree_unlock(leaf);
643 
644 	btrfs_set_root_dirid(root_item, BTRFS_FIRST_FREE_OBJECTID);
645 
646 	key.objectid = objectid;
647 	key.type = BTRFS_ROOT_ITEM_KEY;
648 	key.offset = 0;
649 	ret = btrfs_insert_root(trans, fs_info->tree_root, &key,
650 				root_item);
651 	if (ret) {
652 		int ret2;
653 
654 		/*
655 		 * Since we don't abort the transaction in this case, free the
656 		 * tree block so that we don't leak space and leave the
657 		 * filesystem in an inconsistent state (an extent item in the
658 		 * extent tree with a backreference for a root that does not
659 		 * exists).
660 		 */
661 		btrfs_tree_lock(leaf);
662 		btrfs_clear_buffer_dirty(trans, leaf);
663 		btrfs_tree_unlock(leaf);
664 		ret2 = btrfs_free_tree_block(trans, objectid, leaf, 0, 1);
665 		if (unlikely(ret2 < 0))
666 			btrfs_abort_transaction(trans, ret2);
667 		free_extent_buffer(leaf);
668 		goto out;
669 	}
670 
671 	free_extent_buffer(leaf);
672 	leaf = NULL;
673 
674 	new_root = btrfs_get_new_fs_root(fs_info, objectid, &anon_dev);
675 	if (IS_ERR(new_root)) {
676 		ret = PTR_ERR(new_root);
677 		btrfs_abort_transaction(trans, ret);
678 		goto out;
679 	}
680 	/* anon_dev is owned by new_root now. */
681 	anon_dev = 0;
682 	BTRFS_I(new_inode_args.inode)->root = new_root;
683 	/* ... and new_root is owned by new_inode_args.inode now. */
684 
685 	ret = btrfs_record_root_in_trans(trans, new_root);
686 	if (unlikely(ret)) {
687 		btrfs_abort_transaction(trans, ret);
688 		goto out;
689 	}
690 
691 	ret = btrfs_uuid_tree_add(trans, root_item->uuid,
692 				  BTRFS_UUID_KEY_SUBVOL, objectid);
693 	if (unlikely(ret)) {
694 		btrfs_abort_transaction(trans, ret);
695 		goto out;
696 	}
697 
698 	btrfs_record_new_subvolume(trans, BTRFS_I(dir));
699 
700 	ret = btrfs_create_new_inode(trans, &new_inode_args);
701 	if (unlikely(ret)) {
702 		btrfs_abort_transaction(trans, ret);
703 		goto out;
704 	}
705 
706 	/*
707 	 * Subvolumes have orphans cleaned on first dentry lookup. A new
708 	 * subvolume cannot have any orphans, so we should set the bit before we
709 	 * add the subvolume dentry to the dentry cache, so that it is in the
710 	 * same state as a subvolume after first lookup.
711 	 */
712 	set_bit(BTRFS_ROOT_ORPHAN_CLEANUP, &new_root->state);
713 	d_instantiate_new(dentry, new_inode_args.inode);
714 	new_inode_args.inode = NULL;
715 
716 out:
717 	trans->block_rsv = NULL;
718 	trans->bytes_reserved = 0;
719 	btrfs_end_transaction(trans);
720 out_release_rsv:
721 	btrfs_block_rsv_release(fs_info, &block_rsv, (u64)-1, NULL);
722 	if (qgroup_reserved)
723 		btrfs_qgroup_free_meta_prealloc(root, qgroup_reserved);
724 out_new_inode_args:
725 	btrfs_new_inode_args_destroy(&new_inode_args);
726 out_inode:
727 	iput(new_inode_args.inode);
728 out_anon_dev:
729 	if (anon_dev)
730 		free_anon_bdev(anon_dev);
731 
732 	return ret;
733 }
734 
735 static int create_snapshot(struct btrfs_root *root, struct inode *dir,
736 			   struct dentry *dentry, bool readonly,
737 			   struct btrfs_qgroup_inherit *inherit)
738 {
739 	struct btrfs_fs_info *fs_info = inode_to_fs_info(dir);
740 	struct inode *inode;
741 	struct btrfs_pending_snapshot AUTO_KFREE(pending_snapshot);
742 	unsigned int trans_num_items;
743 	struct btrfs_trans_handle *trans;
744 	struct btrfs_block_rsv *block_rsv;
745 	u64 qgroup_reserved = 0;
746 	int ret;
747 
748 	/* We do not support snapshotting right now. */
749 	if (btrfs_fs_incompat(fs_info, EXTENT_TREE_V2)) {
750 		btrfs_warn(fs_info,
751 			   "extent tree v2 doesn't support snapshotting yet");
752 		return -EOPNOTSUPP;
753 	}
754 
755 	if (btrfs_root_refs(&root->root_item) == 0)
756 		return -ENOENT;
757 
758 	if (!test_bit(BTRFS_ROOT_SHAREABLE, &root->state))
759 		return -EINVAL;
760 
761 	if (atomic_read(&root->nr_swapfiles)) {
762 		btrfs_warn(fs_info,
763 			   "cannot snapshot subvolume with active swapfile");
764 		return -ETXTBSY;
765 	}
766 
767 	pending_snapshot = kzalloc_obj(*pending_snapshot);
768 	if (!pending_snapshot)
769 		return -ENOMEM;
770 
771 	ret = get_anon_bdev(&pending_snapshot->anon_dev);
772 	if (ret < 0)
773 		goto free_pending;
774 	pending_snapshot->root_item = kzalloc_obj(struct btrfs_root_item);
775 	pending_snapshot->path = btrfs_alloc_path();
776 	if (!pending_snapshot->root_item || !pending_snapshot->path) {
777 		ret = -ENOMEM;
778 		goto free_pending;
779 	}
780 
781 	block_rsv = &pending_snapshot->block_rsv;
782 	btrfs_init_block_rsv(block_rsv, BTRFS_BLOCK_RSV_TEMP);
783 	/*
784 	 * 1 to add dir item
785 	 * 1 to add dir index
786 	 * 1 to update parent inode item
787 	 */
788 	trans_num_items = create_subvol_num_items(inherit) + 3;
789 	ret = btrfs_subvolume_reserve_metadata(BTRFS_I(dir)->root, block_rsv,
790 					       trans_num_items, false);
791 	if (ret)
792 		goto free_pending;
793 	qgroup_reserved = block_rsv->qgroup_rsv_reserved;
794 
795 	pending_snapshot->dentry = dentry;
796 	pending_snapshot->root = root;
797 	pending_snapshot->readonly = readonly;
798 	pending_snapshot->dir = BTRFS_I(dir);
799 	pending_snapshot->inherit = inherit;
800 
801 	trans = btrfs_start_transaction(root, 0);
802 	if (IS_ERR(trans)) {
803 		ret = PTR_ERR(trans);
804 		goto fail;
805 	}
806 	ret = btrfs_record_root_in_trans(trans, BTRFS_I(dir)->root);
807 	if (ret) {
808 		btrfs_end_transaction(trans);
809 		goto fail;
810 	}
811 	btrfs_qgroup_convert_reserved_meta(root, qgroup_reserved);
812 	qgroup_reserved = 0;
813 
814 	trans->pending_snapshot = pending_snapshot;
815 
816 	ret = btrfs_commit_transaction(trans);
817 	if (ret)
818 		goto fail;
819 
820 	ret = pending_snapshot->error;
821 	if (ret)
822 		goto fail;
823 
824 	ret = btrfs_orphan_cleanup(pending_snapshot->snap);
825 	if (ret)
826 		goto fail;
827 
828 	inode = btrfs_lookup_dentry(d_inode(dentry->d_parent), dentry);
829 	if (IS_ERR(inode)) {
830 		ret = PTR_ERR(inode);
831 		goto fail;
832 	}
833 
834 	d_instantiate(dentry, inode);
835 	ret = 0;
836 	pending_snapshot->anon_dev = 0;
837 fail:
838 	/* Prevent double freeing of anon_dev */
839 	if (ret && pending_snapshot->snap)
840 		pending_snapshot->snap->anon_dev = 0;
841 	btrfs_put_root(pending_snapshot->snap);
842 	btrfs_block_rsv_release(fs_info, block_rsv, (u64)-1, NULL);
843 	if (qgroup_reserved)
844 		btrfs_qgroup_free_meta_prealloc(root, qgroup_reserved);
845 free_pending:
846 	if (pending_snapshot->anon_dev)
847 		free_anon_bdev(pending_snapshot->anon_dev);
848 	kfree(pending_snapshot->root_item);
849 	btrfs_free_path(pending_snapshot->path);
850 
851 	return ret;
852 }
853 
854 /*
855  * Create a new subvolume below @parent.  This is largely modeled after
856  * sys_mkdirat and vfs_mkdir, but we only do a single component lookup
857  * inside this filesystem so it's quite a bit simpler.
858  */
859 static noinline int btrfs_mksubvol(struct dentry *parent,
860 				   struct mnt_idmap *idmap,
861 				   struct qstr *qname, struct btrfs_root *snap_src,
862 				   bool readonly,
863 				   struct btrfs_qgroup_inherit *inherit)
864 {
865 	struct inode *dir = d_inode(parent);
866 	struct btrfs_fs_info *fs_info = inode_to_fs_info(dir);
867 	struct dentry *dentry;
868 	struct fscrypt_str name_str = FSTR_INIT((char *)qname->name, qname->len);
869 	int ret;
870 
871 	dentry = start_creating_killable(idmap, parent, qname);
872 	if (IS_ERR(dentry))
873 		return PTR_ERR(dentry);
874 
875 	ret = may_create_dentry(idmap, dir, dentry);
876 	if (ret)
877 		goto out_dput;
878 
879 	/*
880 	 * even if this name doesn't exist, we may get hash collisions.
881 	 * check for them now when we can safely fail
882 	 */
883 	ret = btrfs_check_dir_item_collision(BTRFS_I(dir)->root, dir->i_ino, &name_str);
884 	if (ret)
885 		goto out_dput;
886 
887 	down_read(&fs_info->subvol_sem);
888 
889 	if (btrfs_root_refs(&BTRFS_I(dir)->root->root_item) == 0)
890 		goto out_up_read;
891 
892 	if (snap_src)
893 		ret = create_snapshot(snap_src, dir, dentry, readonly, inherit);
894 	else
895 		ret = create_subvol(idmap, dir, dentry, inherit);
896 
897 	if (!ret)
898 		fsnotify_mkdir(dir, dentry);
899 out_up_read:
900 	up_read(&fs_info->subvol_sem);
901 out_dput:
902 	end_creating(dentry);
903 	return ret;
904 }
905 
906 static noinline int btrfs_mksnapshot(struct dentry *parent,
907 				   struct mnt_idmap *idmap,
908 				   struct qstr *qname,
909 				   struct btrfs_root *root,
910 				   bool readonly,
911 				   struct btrfs_qgroup_inherit *inherit)
912 {
913 	int ret;
914 
915 	/*
916 	 * Force new buffered writes to reserve space even when NOCOW is
917 	 * possible. This is to avoid later writeback (running delalloc) to
918 	 * fallback to COW mode and unexpectedly fail with ENOSPC.
919 	 */
920 	btrfs_drew_read_lock(&root->snapshot_lock);
921 
922 	ret = btrfs_start_delalloc_snapshot(root, false);
923 	if (ret)
924 		goto out;
925 
926 	/*
927 	 * All previous writes have started writeback in NOCOW mode, so now
928 	 * we force future writes to fallback to COW mode during snapshot
929 	 * creation.
930 	 */
931 	atomic_inc(&root->snapshot_force_cow);
932 
933 	btrfs_wait_ordered_extents(root, U64_MAX, NULL);
934 
935 	ret = btrfs_mksubvol(parent, idmap, qname, root, readonly, inherit);
936 
937 	atomic_dec(&root->snapshot_force_cow);
938 out:
939 	btrfs_drew_read_unlock(&root->snapshot_lock);
940 	return ret;
941 }
942 
943 /*
944  * Try to start exclusive operation @type or cancel it if it's running.
945  *
946  * Return:
947  *   0        - normal mode, newly claimed op started
948  *  >0        - normal mode, something else is running,
949  *              return BTRFS_ERROR_DEV_EXCL_RUN_IN_PROGRESS to user space
950  * ECANCELED  - cancel mode, successful cancel
951  * ENOTCONN   - cancel mode, operation not running anymore
952  */
953 static int exclop_start_or_cancel_reloc(struct btrfs_fs_info *fs_info,
954 			enum btrfs_exclusive_operation type, bool cancel)
955 {
956 	if (!cancel) {
957 		/* Start normal op */
958 		if (!btrfs_exclop_start(fs_info, type))
959 			return BTRFS_ERROR_DEV_EXCL_RUN_IN_PROGRESS;
960 		/* Exclusive operation is now claimed */
961 		return 0;
962 	}
963 
964 	/* Cancel running op */
965 	if (btrfs_exclop_start_try_lock(fs_info, type)) {
966 		/*
967 		 * This blocks any exclop finish from setting it to NONE, so we
968 		 * request cancellation. Either it runs and we will wait for it,
969 		 * or it has finished and no waiting will happen.
970 		 */
971 		atomic_inc(&fs_info->reloc_cancel_req);
972 		btrfs_exclop_start_unlock(fs_info);
973 
974 		if (test_bit(BTRFS_FS_RELOC_RUNNING, &fs_info->flags))
975 			wait_on_bit(&fs_info->flags, BTRFS_FS_RELOC_RUNNING,
976 				    TASK_INTERRUPTIBLE);
977 
978 		return -ECANCELED;
979 	}
980 
981 	/* Something else is running or none */
982 	return -ENOTCONN;
983 }
984 
985 static noinline int btrfs_ioctl_resize(struct file *file,
986 					void __user *arg)
987 {
988 	BTRFS_DEV_LOOKUP_ARGS(args);
989 	struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
990 	struct btrfs_fs_info *fs_info = root->fs_info;
991 	u64 new_size;
992 	u64 old_size;
993 	u64 devid = 1;
994 	struct btrfs_ioctl_vol_args AUTO_KFREE(vol_args);
995 	struct btrfs_device *device = NULL;
996 	char *sizestr;
997 	char *devstr = NULL;
998 	int ret = 0;
999 	int mod = 0;
1000 	bool cancel;
1001 
1002 	if (!capable(CAP_SYS_ADMIN))
1003 		return -EPERM;
1004 
1005 	ret = mnt_want_write_file(file);
1006 	if (ret)
1007 		return ret;
1008 
1009 	/*
1010 	 * Read the arguments before checking exclusivity to be able to
1011 	 * distinguish regular resize and cancel
1012 	 */
1013 	vol_args = memdup_user(arg, sizeof(*vol_args));
1014 	if (IS_ERR(vol_args)) {
1015 		ret = PTR_ERR(vol_args);
1016 		goto out_drop;
1017 	}
1018 	ret = btrfs_check_ioctl_vol_args_path(vol_args);
1019 	if (ret < 0)
1020 		goto out_drop;
1021 
1022 	sizestr = vol_args->name;
1023 	cancel = (strcmp("cancel", sizestr) == 0);
1024 	ret = exclop_start_or_cancel_reloc(fs_info, BTRFS_EXCLOP_RESIZE, cancel);
1025 	if (ret)
1026 		goto out_drop;
1027 	/* Exclusive operation is now claimed */
1028 
1029 	devstr = strchr(sizestr, ':');
1030 	if (devstr) {
1031 		sizestr = devstr + 1;
1032 		*devstr = '\0';
1033 		devstr = vol_args->name;
1034 		ret = kstrtoull(devstr, 10, &devid);
1035 		if (ret)
1036 			goto out_finish;
1037 		if (!devid) {
1038 			ret = -EINVAL;
1039 			goto out_finish;
1040 		}
1041 		btrfs_info(fs_info, "resizing devid %llu", devid);
1042 	}
1043 
1044 	args.devid = devid;
1045 	device = btrfs_find_device(fs_info->fs_devices, &args);
1046 	if (!device) {
1047 		btrfs_info(fs_info, "resizer unable to find device %llu",
1048 			   devid);
1049 		ret = -ENODEV;
1050 		goto out_finish;
1051 	}
1052 
1053 	if (!test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state)) {
1054 		btrfs_info(fs_info,
1055 			   "resizer unable to apply on readonly device %llu",
1056 		       devid);
1057 		ret = -EPERM;
1058 		goto out_finish;
1059 	}
1060 
1061 	if (!strcmp(sizestr, "max"))
1062 		new_size = bdev_nr_bytes(device->bdev);
1063 	else {
1064 		char *retptr;
1065 
1066 		if (sizestr[0] == '-') {
1067 			mod = -1;
1068 			sizestr++;
1069 		} else if (sizestr[0] == '+') {
1070 			mod = 1;
1071 			sizestr++;
1072 		}
1073 		new_size = memparse(sizestr, &retptr);
1074 		if (*retptr != '\0' || new_size == 0) {
1075 			ret = -EINVAL;
1076 			goto out_finish;
1077 		}
1078 	}
1079 
1080 	if (test_bit(BTRFS_DEV_STATE_REPLACE_TGT, &device->dev_state)) {
1081 		ret = -EPERM;
1082 		goto out_finish;
1083 	}
1084 
1085 	old_size = btrfs_device_get_total_bytes(device);
1086 
1087 	if (mod < 0) {
1088 		if (new_size > old_size) {
1089 			ret = -EINVAL;
1090 			goto out_finish;
1091 		}
1092 		new_size = old_size - new_size;
1093 	} else if (mod > 0) {
1094 		if (new_size > ULLONG_MAX - old_size) {
1095 			ret = -ERANGE;
1096 			goto out_finish;
1097 		}
1098 		new_size = old_size + new_size;
1099 	}
1100 
1101 	if (new_size < SZ_256M) {
1102 		ret = -EINVAL;
1103 		goto out_finish;
1104 	}
1105 	if (new_size > bdev_nr_bytes(device->bdev)) {
1106 		ret = -EFBIG;
1107 		goto out_finish;
1108 	}
1109 
1110 	new_size = round_down(new_size, fs_info->sectorsize);
1111 
1112 	if (new_size > old_size) {
1113 		struct btrfs_trans_handle *trans;
1114 
1115 		trans = btrfs_start_transaction(root, 0);
1116 		if (IS_ERR(trans)) {
1117 			ret = PTR_ERR(trans);
1118 			goto out_finish;
1119 		}
1120 		ret = btrfs_grow_device(trans, device, new_size);
1121 		btrfs_commit_transaction(trans);
1122 	} else if (new_size < old_size) {
1123 		ret = btrfs_shrink_device(device, new_size);
1124 	} /* equal, nothing need to do */
1125 
1126 	if (ret == 0 && new_size != old_size)
1127 		btrfs_info(fs_info,
1128 			"resize device %s (devid %llu) from %llu to %llu",
1129 			btrfs_dev_name(device), device->devid,
1130 			old_size, new_size);
1131 out_finish:
1132 	btrfs_exclop_finish(fs_info);
1133 out_drop:
1134 	mnt_drop_write_file(file);
1135 	return ret;
1136 }
1137 
1138 static noinline int __btrfs_ioctl_snap_create(struct file *file,
1139 				struct mnt_idmap *idmap,
1140 				const char *name, unsigned long fd, bool subvol,
1141 				bool readonly,
1142 				struct btrfs_qgroup_inherit *inherit)
1143 {
1144 	int ret;
1145 	struct qstr qname = QSTR(name);
1146 
1147 	if (!S_ISDIR(file_inode(file)->i_mode))
1148 		return -ENOTDIR;
1149 
1150 	ret = mnt_want_write_file(file);
1151 	if (ret)
1152 		return ret;
1153 
1154 	if (strchr(name, '/')) {
1155 		ret = -EINVAL;
1156 		goto out_drop_write;
1157 	}
1158 
1159 	if (qname.name[0] == '.' &&
1160 	   (qname.len == 1 || (qname.name[1] == '.' && qname.len == 2))) {
1161 		ret = -EEXIST;
1162 		goto out_drop_write;
1163 	}
1164 
1165 	if (subvol) {
1166 		ret = btrfs_mksubvol(file_dentry(file), idmap, &qname, NULL,
1167 				     readonly, inherit);
1168 	} else {
1169 		CLASS(fd, src)(fd);
1170 		struct inode *src_inode;
1171 		if (fd_empty(src)) {
1172 			ret = -EINVAL;
1173 			goto out_drop_write;
1174 		}
1175 
1176 		src_inode = file_inode(fd_file(src));
1177 		if (src_inode->i_sb != file_inode(file)->i_sb) {
1178 			btrfs_info(BTRFS_I(file_inode(file))->root->fs_info,
1179 				   "Snapshot src from another FS");
1180 			ret = -EXDEV;
1181 		} else if (!inode_owner_or_capable(idmap, src_inode)) {
1182 			/*
1183 			 * Subvolume creation is not restricted, but snapshots
1184 			 * are limited to own subvolumes only
1185 			 */
1186 			ret = -EPERM;
1187 		} else if (btrfs_ino(BTRFS_I(src_inode)) != BTRFS_FIRST_FREE_OBJECTID) {
1188 			/*
1189 			 * Snapshots must be made with the src_inode referring
1190 			 * to the subvolume inode, otherwise the permission
1191 			 * checking above is useless because we may have
1192 			 * permission on a lower directory but not the subvol
1193 			 * itself.
1194 			 */
1195 			ret = -EINVAL;
1196 		} else {
1197 			ret = btrfs_mksnapshot(file_dentry(file), idmap, &qname,
1198 					       BTRFS_I(src_inode)->root,
1199 					       readonly, inherit);
1200 		}
1201 	}
1202 out_drop_write:
1203 	mnt_drop_write_file(file);
1204 	return ret;
1205 }
1206 
1207 static noinline int btrfs_ioctl_snap_create(struct file *file,
1208 					    void __user *arg, bool subvol)
1209 {
1210 	struct btrfs_ioctl_vol_args AUTO_KFREE(vol_args);
1211 	int ret;
1212 
1213 	if (!S_ISDIR(file_inode(file)->i_mode))
1214 		return -ENOTDIR;
1215 
1216 	vol_args = memdup_user(arg, sizeof(*vol_args));
1217 	if (IS_ERR(vol_args))
1218 		return PTR_ERR(vol_args);
1219 	ret = btrfs_check_ioctl_vol_args_path(vol_args);
1220 	if (ret < 0)
1221 		return ret;
1222 
1223 	return __btrfs_ioctl_snap_create(file, file_mnt_idmap(file),
1224 					 vol_args->name, vol_args->fd, subvol,
1225 					 false, NULL);
1226 }
1227 
1228 static noinline int btrfs_ioctl_snap_create_v2(struct file *file,
1229 					       void __user *arg, bool subvol)
1230 {
1231 	struct btrfs_ioctl_vol_args_v2 AUTO_KFREE(vol_args);
1232 	struct btrfs_qgroup_inherit AUTO_KFREE(inherit);
1233 	int ret;
1234 	bool readonly = false;
1235 
1236 	if (!S_ISDIR(file_inode(file)->i_mode))
1237 		return -ENOTDIR;
1238 
1239 	vol_args = memdup_user(arg, sizeof(*vol_args));
1240 	if (IS_ERR(vol_args))
1241 		return PTR_ERR(vol_args);
1242 	ret = btrfs_check_ioctl_vol_args2_subvol_name(vol_args);
1243 	if (ret < 0)
1244 		return ret;
1245 
1246 	if (vol_args->flags & ~BTRFS_SUBVOL_CREATE_ARGS_MASK)
1247 		return -EOPNOTSUPP;
1248 
1249 	if (vol_args->flags & BTRFS_SUBVOL_RDONLY)
1250 		readonly = true;
1251 	if (vol_args->flags & BTRFS_SUBVOL_QGROUP_INHERIT) {
1252 		struct btrfs_fs_info *fs_info = inode_to_fs_info(file_inode(file));
1253 
1254 		if (vol_args->size < sizeof(*inherit) || vol_args->size > PAGE_SIZE)
1255 			return -EINVAL;
1256 
1257 		inherit = memdup_user(vol_args->qgroup_inherit, vol_args->size);
1258 		if (IS_ERR(inherit)) {
1259 			return PTR_ERR(inherit);
1260 		}
1261 
1262 		ret = btrfs_qgroup_check_inherit(fs_info, inherit, vol_args->size);
1263 		if (ret < 0)
1264 			return ret;
1265 	}
1266 
1267 	return __btrfs_ioctl_snap_create(file, file_mnt_idmap(file),
1268 					 vol_args->name, vol_args->fd, subvol,
1269 					 readonly, inherit);
1270 }
1271 
1272 static noinline int btrfs_ioctl_subvol_getflags(struct btrfs_inode *inode,
1273 						void __user *arg)
1274 {
1275 	struct btrfs_root *root = inode->root;
1276 	struct btrfs_fs_info *fs_info = root->fs_info;
1277 	int ret = 0;
1278 	u64 flags = 0;
1279 
1280 	if (btrfs_ino(inode) != BTRFS_FIRST_FREE_OBJECTID)
1281 		return -EINVAL;
1282 
1283 	down_read(&fs_info->subvol_sem);
1284 	if (btrfs_root_readonly(root))
1285 		flags |= BTRFS_SUBVOL_RDONLY;
1286 	up_read(&fs_info->subvol_sem);
1287 
1288 	if (copy_to_user(arg, &flags, sizeof(flags)))
1289 		ret = -EFAULT;
1290 
1291 	return ret;
1292 }
1293 
1294 static noinline int btrfs_ioctl_subvol_setflags(struct file *file,
1295 					      void __user *arg)
1296 {
1297 	struct inode *inode = file_inode(file);
1298 	struct btrfs_fs_info *fs_info = inode_to_fs_info(inode);
1299 	struct btrfs_root *root = BTRFS_I(inode)->root;
1300 	struct btrfs_trans_handle *trans;
1301 	u64 root_flags;
1302 	u64 flags;
1303 	int ret;
1304 
1305 	if (!inode_owner_or_capable(file_mnt_idmap(file), inode))
1306 		return -EPERM;
1307 
1308 	ret = mnt_want_write_file(file);
1309 	if (ret)
1310 		return ret;
1311 
1312 	if (btrfs_ino(BTRFS_I(inode)) != BTRFS_FIRST_FREE_OBJECTID) {
1313 		ret = -EINVAL;
1314 		goto out_drop_write;
1315 	}
1316 
1317 	if (copy_from_user(&flags, arg, sizeof(flags))) {
1318 		ret = -EFAULT;
1319 		goto out_drop_write;
1320 	}
1321 
1322 	if (flags & ~BTRFS_SUBVOL_RDONLY) {
1323 		ret = -EOPNOTSUPP;
1324 		goto out_drop_write;
1325 	}
1326 
1327 	down_write(&fs_info->subvol_sem);
1328 
1329 	/* nothing to do */
1330 	if (!!(flags & BTRFS_SUBVOL_RDONLY) == btrfs_root_readonly(root))
1331 		goto out_drop_sem;
1332 
1333 	root_flags = btrfs_root_flags(&root->root_item);
1334 	if (flags & BTRFS_SUBVOL_RDONLY) {
1335 		btrfs_set_root_flags(&root->root_item,
1336 				     root_flags | BTRFS_ROOT_SUBVOL_RDONLY);
1337 	} else {
1338 		/*
1339 		 * Block RO -> RW transition if this subvolume is involved in
1340 		 * send
1341 		 */
1342 		spin_lock(&root->root_item_lock);
1343 		if (root->send_in_progress == 0) {
1344 			btrfs_set_root_flags(&root->root_item,
1345 				     root_flags & ~BTRFS_ROOT_SUBVOL_RDONLY);
1346 			spin_unlock(&root->root_item_lock);
1347 		} else {
1348 			spin_unlock(&root->root_item_lock);
1349 			btrfs_warn(fs_info,
1350 				   "Attempt to set subvolume %llu read-write during send",
1351 				   btrfs_root_id(root));
1352 			ret = -EPERM;
1353 			goto out_drop_sem;
1354 		}
1355 	}
1356 
1357 	trans = btrfs_start_transaction(root, 1);
1358 	if (IS_ERR(trans)) {
1359 		ret = PTR_ERR(trans);
1360 		goto out_reset;
1361 	}
1362 
1363 	ret = btrfs_update_root(trans, fs_info->tree_root,
1364 				&root->root_key, &root->root_item);
1365 	if (ret < 0) {
1366 		btrfs_end_transaction(trans);
1367 		goto out_reset;
1368 	}
1369 
1370 	ret = btrfs_commit_transaction(trans);
1371 
1372 out_reset:
1373 	if (ret)
1374 		btrfs_set_root_flags(&root->root_item, root_flags);
1375 out_drop_sem:
1376 	up_write(&fs_info->subvol_sem);
1377 out_drop_write:
1378 	mnt_drop_write_file(file);
1379 	return ret;
1380 }
1381 
1382 static noinline bool key_in_sk(const struct btrfs_key *key,
1383 			       const struct btrfs_ioctl_search_key *sk)
1384 {
1385 	struct btrfs_key test;
1386 	int ret;
1387 
1388 	test.objectid = sk->min_objectid;
1389 	test.type = sk->min_type;
1390 	test.offset = sk->min_offset;
1391 
1392 	ret = btrfs_comp_cpu_keys(key, &test);
1393 	if (ret < 0)
1394 		return false;
1395 
1396 	test.objectid = sk->max_objectid;
1397 	test.type = sk->max_type;
1398 	test.offset = sk->max_offset;
1399 
1400 	ret = btrfs_comp_cpu_keys(key, &test);
1401 	if (ret > 0)
1402 		return false;
1403 	return true;
1404 }
1405 
1406 static noinline int copy_to_sk(struct btrfs_path *path,
1407 			       struct btrfs_key *key,
1408 			       const struct btrfs_ioctl_search_key *sk,
1409 			       u64 *buf_size,
1410 			       char __user *ubuf,
1411 			       unsigned long *sk_offset,
1412 			       int *num_found)
1413 {
1414 	u64 found_transid;
1415 	struct extent_buffer *leaf;
1416 	struct btrfs_ioctl_search_header sh;
1417 	struct btrfs_key test;
1418 	unsigned long item_off;
1419 	unsigned long item_len;
1420 	int nritems;
1421 	int i;
1422 	int slot;
1423 	int ret = 0;
1424 
1425 	leaf = path->nodes[0];
1426 	slot = path->slots[0];
1427 	nritems = btrfs_header_nritems(leaf);
1428 
1429 	if (btrfs_header_generation(leaf) > sk->max_transid) {
1430 		i = nritems;
1431 		goto advance_key;
1432 	}
1433 	found_transid = btrfs_header_generation(leaf);
1434 
1435 	for (i = slot; i < nritems; i++) {
1436 		item_off = btrfs_item_ptr_offset(leaf, i);
1437 		item_len = btrfs_item_size(leaf, i);
1438 
1439 		btrfs_item_key_to_cpu(leaf, key, i);
1440 		if (!key_in_sk(key, sk))
1441 			continue;
1442 
1443 		if (sizeof(sh) + item_len > *buf_size) {
1444 			if (*num_found)
1445 				return 1;
1446 
1447 			/*
1448 			 * return one empty item back for v1, which does not
1449 			 * handle -EOVERFLOW
1450 			 */
1451 
1452 			*buf_size = sizeof(sh) + item_len;
1453 			item_len = 0;
1454 			ret = -EOVERFLOW;
1455 		}
1456 
1457 		if (sizeof(sh) + item_len + *sk_offset > *buf_size)
1458 			return 1;
1459 
1460 		sh.objectid = key->objectid;
1461 		sh.type = key->type;
1462 		sh.offset = key->offset;
1463 		sh.len = item_len;
1464 		sh.transid = found_transid;
1465 
1466 		/*
1467 		 * Copy search result header. If we fault then loop again so we
1468 		 * can fault in the pages and -EFAULT there if there's a
1469 		 * problem. Otherwise we'll fault and then copy the buffer in
1470 		 * properly this next time through
1471 		 */
1472 		if (copy_to_user_nofault(ubuf + *sk_offset, &sh, sizeof(sh)))
1473 			return 0;
1474 
1475 		*sk_offset += sizeof(sh);
1476 
1477 		if (item_len) {
1478 			char __user *up = ubuf + *sk_offset;
1479 			/*
1480 			 * Copy the item, same behavior as above, but reset the
1481 			 * * sk_offset so we copy the full thing again.
1482 			 */
1483 			if (read_extent_buffer_to_user_nofault(leaf, up,
1484 						item_off, item_len)) {
1485 				*sk_offset -= sizeof(sh);
1486 				return 0;
1487 			}
1488 
1489 			*sk_offset += item_len;
1490 		}
1491 		(*num_found)++;
1492 
1493 		/* -EOVERFLOW from above. */
1494 		if (ret)
1495 			return ret;
1496 
1497 		if (*num_found >= sk->nr_items)
1498 			return 1;
1499 	}
1500 advance_key:
1501 	ret = 0;
1502 	test.objectid = sk->max_objectid;
1503 	test.type = sk->max_type;
1504 	test.offset = sk->max_offset;
1505 	if (btrfs_comp_cpu_keys(key, &test) >= 0)
1506 		ret = 1;
1507 	else if (key->offset < (u64)-1)
1508 		key->offset++;
1509 	else if (key->type < (u8)-1) {
1510 		key->offset = 0;
1511 		key->type++;
1512 	} else if (key->objectid < (u64)-1) {
1513 		key->offset = 0;
1514 		key->type = 0;
1515 		key->objectid++;
1516 	} else
1517 		ret = 1;
1518 
1519 	/*
1520 	 *  0: all items from this leaf copied, continue with next
1521 	 *  1: * more items can be copied, but unused buffer is too small
1522 	 *     * all items were found
1523 	 *     Either way, it will stops the loop which iterates to the next
1524 	 *     leaf
1525 	 *  -EOVERFLOW: item was to large for buffer
1526 	 *  -EFAULT: could not copy extent buffer back to userspace
1527 	 */
1528 	return ret;
1529 }
1530 
1531 static noinline int search_ioctl(struct btrfs_root *root,
1532 				 struct btrfs_ioctl_search_key *sk,
1533 				 u64 *buf_size,
1534 				 char __user *ubuf)
1535 {
1536 	struct btrfs_fs_info *info = root->fs_info;
1537 	struct btrfs_key key;
1538 	BTRFS_PATH_AUTO_FREE(path);
1539 	int ret;
1540 	int num_found = 0;
1541 	unsigned long sk_offset = 0;
1542 
1543 	if (*buf_size < sizeof(struct btrfs_ioctl_search_header)) {
1544 		*buf_size = sizeof(struct btrfs_ioctl_search_header);
1545 		return -EOVERFLOW;
1546 	}
1547 
1548 	path = btrfs_alloc_path();
1549 	if (!path)
1550 		return -ENOMEM;
1551 
1552 	if (sk->tree_id == 0) {
1553 		/* Search the root that we got passed. */
1554 		root = btrfs_grab_root(root);
1555 	} else {
1556 		/* Look up the root from the arguments. */
1557 		root = btrfs_get_fs_root(info, sk->tree_id, true);
1558 		if (IS_ERR(root))
1559 			return PTR_ERR(root);
1560 	}
1561 
1562 	key.objectid = sk->min_objectid;
1563 	key.type = sk->min_type;
1564 	key.offset = sk->min_offset;
1565 
1566 	while (1) {
1567 		/*
1568 		 * Ensure that the whole user buffer is faulted in at sub-page
1569 		 * granularity, otherwise the loop may live-lock.
1570 		 */
1571 		if (fault_in_subpage_writeable(ubuf + sk_offset, *buf_size - sk_offset)) {
1572 			ret = -EFAULT;
1573 			break;
1574 		}
1575 
1576 		ret = btrfs_search_forward(root, &key, path, sk->min_transid);
1577 		if (ret)
1578 			break;
1579 
1580 		ret = copy_to_sk(path, &key, sk, buf_size, ubuf,
1581 				 &sk_offset, &num_found);
1582 		btrfs_release_path(path);
1583 		if (ret)
1584 			break;
1585 
1586 	}
1587 	/* Normalize return values from btrfs_search_forward() and copy_to_sk(). */
1588 	if (ret > 0)
1589 		ret = 0;
1590 
1591 	sk->nr_items = num_found;
1592 	btrfs_put_root(root);
1593 	return ret;
1594 }
1595 
1596 static noinline int btrfs_ioctl_tree_search(struct btrfs_root *root,
1597 					    void __user *argp)
1598 {
1599 	struct btrfs_ioctl_search_args __user *uargs = argp;
1600 	struct btrfs_ioctl_search_key sk;
1601 	int ret;
1602 	u64 buf_size;
1603 
1604 	if (!capable(CAP_SYS_ADMIN))
1605 		return -EPERM;
1606 
1607 	if (copy_from_user(&sk, &uargs->key, sizeof(sk)))
1608 		return -EFAULT;
1609 
1610 	buf_size = sizeof(uargs->buf);
1611 
1612 	ret = search_ioctl(root, &sk, &buf_size, uargs->buf);
1613 
1614 	/*
1615 	 * In the origin implementation an overflow is handled by returning a
1616 	 * search header with a len of zero, so reset ret.
1617 	 */
1618 	if (ret == -EOVERFLOW)
1619 		ret = 0;
1620 
1621 	if (ret == 0 && copy_to_user(&uargs->key, &sk, sizeof(sk)))
1622 		ret = -EFAULT;
1623 	return ret;
1624 }
1625 
1626 static noinline int btrfs_ioctl_tree_search_v2(struct btrfs_root *root,
1627 					       void __user *argp)
1628 {
1629 	struct btrfs_ioctl_search_args_v2 __user *uarg = argp;
1630 	struct btrfs_ioctl_search_args_v2 args;
1631 	int ret;
1632 	u64 buf_size;
1633 	const u64 buf_limit = SZ_16M;
1634 
1635 	if (!capable(CAP_SYS_ADMIN))
1636 		return -EPERM;
1637 
1638 	/* copy search header and buffer size */
1639 	if (copy_from_user(&args, uarg, sizeof(args)))
1640 		return -EFAULT;
1641 
1642 	buf_size = args.buf_size;
1643 
1644 	/* limit result size to 16MB */
1645 	if (buf_size > buf_limit)
1646 		buf_size = buf_limit;
1647 
1648 	ret = search_ioctl(root, &args.key, &buf_size,
1649 			   (char __user *)(&uarg->buf[0]));
1650 	if (ret == 0 && copy_to_user(&uarg->key, &args.key, sizeof(args.key)))
1651 		ret = -EFAULT;
1652 	else if (ret == -EOVERFLOW &&
1653 		copy_to_user(&uarg->buf_size, &buf_size, sizeof(buf_size)))
1654 		ret = -EFAULT;
1655 
1656 	return ret;
1657 }
1658 
1659 /*
1660  * Search INODE_REFs to identify path name of 'dirid' directory
1661  * in a 'tree_id' tree. and sets path name to 'name'.
1662  */
1663 static noinline int btrfs_search_path_in_tree(struct btrfs_fs_info *info,
1664 				u64 tree_id, u64 dirid, char *name)
1665 {
1666 	struct btrfs_root *root;
1667 	struct btrfs_key key;
1668 	char *ptr;
1669 	int ret = -1;
1670 	int slot;
1671 	int len;
1672 	int total_len = 0;
1673 	struct btrfs_inode_ref *iref;
1674 	struct extent_buffer *l;
1675 	BTRFS_PATH_AUTO_FREE(path);
1676 
1677 	if (dirid == BTRFS_FIRST_FREE_OBJECTID) {
1678 		name[0]='\0';
1679 		return 0;
1680 	}
1681 
1682 	path = btrfs_alloc_path();
1683 	if (!path)
1684 		return -ENOMEM;
1685 
1686 	ptr = &name[BTRFS_INO_LOOKUP_PATH_MAX - 1];
1687 
1688 	root = btrfs_get_fs_root(info, tree_id, true);
1689 	if (IS_ERR(root)) {
1690 		ret = PTR_ERR(root);
1691 		root = NULL;
1692 		goto out;
1693 	}
1694 
1695 	key.objectid = dirid;
1696 	key.type = BTRFS_INODE_REF_KEY;
1697 	key.offset = (u64)-1;
1698 
1699 	while (1) {
1700 		ret = btrfs_search_backwards(root, &key, path);
1701 		if (ret < 0)
1702 			goto out;
1703 		else if (ret > 0) {
1704 			ret = -ENOENT;
1705 			goto out;
1706 		}
1707 
1708 		l = path->nodes[0];
1709 		slot = path->slots[0];
1710 
1711 		iref = btrfs_item_ptr(l, slot, struct btrfs_inode_ref);
1712 		len = btrfs_inode_ref_name_len(l, iref);
1713 		ptr -= len + 1;
1714 		total_len += len + 1;
1715 		if (ptr < name) {
1716 			ret = -ENAMETOOLONG;
1717 			goto out;
1718 		}
1719 
1720 		*(ptr + len) = '/';
1721 		read_extent_buffer(l, ptr, (unsigned long)(iref + 1), len);
1722 
1723 		if (key.offset == BTRFS_FIRST_FREE_OBJECTID)
1724 			break;
1725 
1726 		btrfs_release_path(path);
1727 		key.objectid = key.offset;
1728 		key.offset = (u64)-1;
1729 		dirid = key.objectid;
1730 	}
1731 	memmove(name, ptr, total_len);
1732 	name[total_len] = '\0';
1733 	ret = 0;
1734 out:
1735 	btrfs_put_root(root);
1736 	return ret;
1737 }
1738 
1739 static int btrfs_search_path_in_tree_user(struct mnt_idmap *idmap,
1740 				struct inode *inode,
1741 				struct btrfs_ioctl_ino_lookup_user_args *args)
1742 {
1743 	struct btrfs_fs_info *fs_info = BTRFS_I(inode)->root->fs_info;
1744 	u64 upper_limit = btrfs_ino(BTRFS_I(inode));
1745 	u64 treeid = btrfs_root_id(BTRFS_I(inode)->root);
1746 	u64 dirid = args->dirid;
1747 	unsigned long item_off;
1748 	unsigned long item_len;
1749 	struct btrfs_inode_ref *iref;
1750 	struct btrfs_root_ref *rref;
1751 	struct btrfs_root *root = NULL;
1752 	BTRFS_PATH_AUTO_FREE(path);
1753 	struct btrfs_key key;
1754 	struct extent_buffer *leaf;
1755 	char *ptr;
1756 	int slot;
1757 	int len;
1758 	int total_len = 0;
1759 	int ret;
1760 
1761 	path = btrfs_alloc_path();
1762 	if (!path)
1763 		return -ENOMEM;
1764 
1765 	/*
1766 	 * If the bottom subvolume does not exist directly under upper_limit,
1767 	 * construct the path in from the bottom up.
1768 	 */
1769 	if (dirid != upper_limit) {
1770 		ptr = &args->path[BTRFS_INO_LOOKUP_USER_PATH_MAX - 1];
1771 
1772 		root = btrfs_get_fs_root(fs_info, treeid, true);
1773 		if (IS_ERR(root))
1774 			return PTR_ERR(root);
1775 
1776 		key.objectid = dirid;
1777 		key.type = BTRFS_INODE_REF_KEY;
1778 		key.offset = (u64)-1;
1779 		while (1) {
1780 			struct btrfs_inode *temp_inode;
1781 
1782 			ret = btrfs_search_backwards(root, &key, path);
1783 			if (ret < 0)
1784 				goto out_put;
1785 			else if (ret > 0) {
1786 				ret = -ENOENT;
1787 				goto out_put;
1788 			}
1789 
1790 			leaf = path->nodes[0];
1791 			slot = path->slots[0];
1792 
1793 			iref = btrfs_item_ptr(leaf, slot, struct btrfs_inode_ref);
1794 			len = btrfs_inode_ref_name_len(leaf, iref);
1795 			ptr -= len + 1;
1796 			total_len += len + 1;
1797 			if (ptr < args->path) {
1798 				ret = -ENAMETOOLONG;
1799 				goto out_put;
1800 			}
1801 
1802 			*(ptr + len) = '/';
1803 			read_extent_buffer(leaf, ptr,
1804 					(unsigned long)(iref + 1), len);
1805 
1806 			/*
1807 			 * We don't need the path anymore, so release it and
1808 			 * avoid deadlocks and lockdep warnings in case
1809 			 * btrfs_iget() needs to lookup the inode from its root
1810 			 * btree and lock the same leaf.
1811 			 */
1812 			btrfs_release_path(path);
1813 			temp_inode = btrfs_iget(key.offset, root);
1814 			if (IS_ERR(temp_inode)) {
1815 				ret = PTR_ERR(temp_inode);
1816 				goto out_put;
1817 			}
1818 			/* Check the read+exec permission of this directory. */
1819 			ret = inode_permission(idmap, &temp_inode->vfs_inode,
1820 					       MAY_READ | MAY_EXEC);
1821 			iput(&temp_inode->vfs_inode);
1822 			if (ret)
1823 				goto out_put;
1824 
1825 			if (key.offset == upper_limit)
1826 				break;
1827 			if (key.objectid == BTRFS_FIRST_FREE_OBJECTID) {
1828 				ret = -EACCES;
1829 				goto out_put;
1830 			}
1831 
1832 			key.objectid = key.offset;
1833 			key.offset = (u64)-1;
1834 			dirid = key.objectid;
1835 		}
1836 
1837 		memmove(args->path, ptr, total_len);
1838 		args->path[total_len] = '\0';
1839 		btrfs_put_root(root);
1840 		root = NULL;
1841 		btrfs_release_path(path);
1842 	}
1843 
1844 	/* Get the bottom subvolume's name from ROOT_REF */
1845 	key.objectid = treeid;
1846 	key.type = BTRFS_ROOT_REF_KEY;
1847 	key.offset = args->treeid;
1848 	ret = btrfs_search_slot(NULL, fs_info->tree_root, &key, path, 0, 0);
1849 	if (ret < 0)
1850 		return ret;
1851 	else if (ret > 0)
1852 		return -ENOENT;
1853 
1854 	leaf = path->nodes[0];
1855 	slot = path->slots[0];
1856 	btrfs_item_key_to_cpu(leaf, &key, slot);
1857 
1858 	item_off = btrfs_item_ptr_offset(leaf, slot);
1859 	item_len = btrfs_item_size(leaf, slot);
1860 	/* Check if dirid in ROOT_REF corresponds to passed dirid */
1861 	rref = btrfs_item_ptr(leaf, slot, struct btrfs_root_ref);
1862 	if (args->dirid != btrfs_root_ref_dirid(leaf, rref))
1863 		return -EINVAL;
1864 
1865 	/* Copy subvolume's name */
1866 	item_off += sizeof(struct btrfs_root_ref);
1867 	item_len -= sizeof(struct btrfs_root_ref);
1868 	read_extent_buffer(leaf, args->name, item_off, item_len);
1869 	args->name[item_len] = 0;
1870 
1871 out_put:
1872 	btrfs_put_root(root);
1873 
1874 	return ret;
1875 }
1876 
1877 static noinline int btrfs_ioctl_ino_lookup(struct btrfs_root *root,
1878 					   void __user *argp)
1879 {
1880 	struct btrfs_ioctl_ino_lookup_args AUTO_KFREE(args);
1881 	int ret = 0;
1882 
1883 	args = memdup_user(argp, sizeof(*args));
1884 	if (IS_ERR(args))
1885 		return PTR_ERR(args);
1886 
1887 	/*
1888 	 * Unprivileged query to obtain the containing subvolume root id. The
1889 	 * path is reset so it's consistent with btrfs_search_path_in_tree.
1890 	 */
1891 	if (args->treeid == 0)
1892 		args->treeid = btrfs_root_id(root);
1893 
1894 	if (args->objectid == BTRFS_FIRST_FREE_OBJECTID) {
1895 		args->name[0] = 0;
1896 		goto out;
1897 	}
1898 
1899 	if (!capable(CAP_SYS_ADMIN)) {
1900 		ret = -EPERM;
1901 		goto out;
1902 	}
1903 
1904 	ret = btrfs_search_path_in_tree(root->fs_info,
1905 					args->treeid, args->objectid,
1906 					args->name);
1907 
1908 out:
1909 	if (ret == 0 && copy_to_user(argp, args, sizeof(*args)))
1910 		return -EFAULT;
1911 
1912 	return ret;
1913 }
1914 
1915 /*
1916  * Version of ino_lookup ioctl (unprivileged)
1917  *
1918  * The main differences from ino_lookup ioctl are:
1919  *
1920  *   1. Read + Exec permission will be checked using inode_permission() during
1921  *      path construction. -EACCES will be returned in case of failure.
1922  *   2. Path construction will be stopped at the inode number which corresponds
1923  *      to the fd with which this ioctl is called. If constructed path does not
1924  *      exist under fd's inode, -EACCES will be returned.
1925  *   3. The name of bottom subvolume is also searched and filled.
1926  */
1927 static int btrfs_ioctl_ino_lookup_user(struct file *file, void __user *argp)
1928 {
1929 	struct btrfs_ioctl_ino_lookup_user_args AUTO_KFREE(args);
1930 	struct inode *inode;
1931 	int ret;
1932 
1933 	args = memdup_user(argp, sizeof(*args));
1934 	if (IS_ERR(args))
1935 		return PTR_ERR(args);
1936 
1937 	inode = file_inode(file);
1938 
1939 	if (args->dirid == BTRFS_FIRST_FREE_OBJECTID &&
1940 	    btrfs_ino(BTRFS_I(inode)) != BTRFS_FIRST_FREE_OBJECTID) {
1941 		/*
1942 		 * The subvolume does not exist under fd with which this is
1943 		 * called
1944 		 */
1945 		return -EACCES;
1946 	}
1947 
1948 	ret = btrfs_search_path_in_tree_user(file_mnt_idmap(file), inode, args);
1949 
1950 	if (ret == 0 && copy_to_user(argp, args, sizeof(*args)))
1951 		ret = -EFAULT;
1952 
1953 	return ret;
1954 }
1955 
1956 /* Get the subvolume information in BTRFS_ROOT_ITEM and BTRFS_ROOT_BACKREF */
1957 static int _btrfs_ioctl_get_subvol_info(struct inode *inode,
1958 					struct btrfs_ioctl_get_subvol_info_args *subvol_info)
1959 {
1960 	struct btrfs_fs_info *fs_info;
1961 	struct btrfs_root *root;
1962 	struct btrfs_path *path;
1963 	struct btrfs_key key;
1964 	struct btrfs_root_item *root_item;
1965 	struct btrfs_root_ref *rref;
1966 	struct extent_buffer *leaf;
1967 	unsigned long item_off;
1968 	int slot;
1969 	int ret = 0;
1970 
1971 	path = btrfs_alloc_path();
1972 	if (!path)
1973 		return -ENOMEM;
1974 
1975 	fs_info = BTRFS_I(inode)->root->fs_info;
1976 
1977 	/* Get root_item of inode's subvolume */
1978 	key.objectid = btrfs_root_id(BTRFS_I(inode)->root);
1979 	root = btrfs_get_fs_root(fs_info, key.objectid, true);
1980 	if (IS_ERR(root)) {
1981 		ret = PTR_ERR(root);
1982 		goto out_free;
1983 	}
1984 	root_item = &root->root_item;
1985 
1986 	subvol_info->treeid = key.objectid;
1987 
1988 	subvol_info->generation = btrfs_root_generation(root_item);
1989 	subvol_info->flags = btrfs_root_flags(root_item);
1990 
1991 	memcpy(subvol_info->uuid, root_item->uuid, BTRFS_UUID_SIZE);
1992 	memcpy(subvol_info->parent_uuid, root_item->parent_uuid,
1993 						    BTRFS_UUID_SIZE);
1994 	memcpy(subvol_info->received_uuid, root_item->received_uuid,
1995 						    BTRFS_UUID_SIZE);
1996 
1997 	subvol_info->ctransid = btrfs_root_ctransid(root_item);
1998 	subvol_info->ctime.sec = btrfs_stack_timespec_sec(&root_item->ctime);
1999 	subvol_info->ctime.nsec = btrfs_stack_timespec_nsec(&root_item->ctime);
2000 
2001 	subvol_info->otransid = btrfs_root_otransid(root_item);
2002 	subvol_info->otime.sec = btrfs_stack_timespec_sec(&root_item->otime);
2003 	subvol_info->otime.nsec = btrfs_stack_timespec_nsec(&root_item->otime);
2004 
2005 	subvol_info->stransid = btrfs_root_stransid(root_item);
2006 	subvol_info->stime.sec = btrfs_stack_timespec_sec(&root_item->stime);
2007 	subvol_info->stime.nsec = btrfs_stack_timespec_nsec(&root_item->stime);
2008 
2009 	subvol_info->rtransid = btrfs_root_rtransid(root_item);
2010 	subvol_info->rtime.sec = btrfs_stack_timespec_sec(&root_item->rtime);
2011 	subvol_info->rtime.nsec = btrfs_stack_timespec_nsec(&root_item->rtime);
2012 
2013 	if (key.objectid != BTRFS_FS_TREE_OBJECTID) {
2014 		/* Search root tree for ROOT_BACKREF of this subvolume */
2015 		key.type = BTRFS_ROOT_BACKREF_KEY;
2016 		key.offset = 0;
2017 		ret = btrfs_search_slot(NULL, fs_info->tree_root, &key, path, 0, 0);
2018 		if (ret < 0) {
2019 			goto out;
2020 		} else if (path->slots[0] >=
2021 			   btrfs_header_nritems(path->nodes[0])) {
2022 			ret = btrfs_next_leaf(fs_info->tree_root, path);
2023 			if (ret < 0) {
2024 				goto out;
2025 			} else if (unlikely(ret > 0)) {
2026 				ret = -EUCLEAN;
2027 				goto out;
2028 			}
2029 		}
2030 
2031 		leaf = path->nodes[0];
2032 		slot = path->slots[0];
2033 		btrfs_item_key_to_cpu(leaf, &key, slot);
2034 		if (key.objectid == subvol_info->treeid &&
2035 		    key.type == BTRFS_ROOT_BACKREF_KEY) {
2036 			u16 name_len;
2037 
2038 			subvol_info->parent_id = key.offset;
2039 
2040 			rref = btrfs_item_ptr(leaf, slot, struct btrfs_root_ref);
2041 			name_len = btrfs_root_ref_name_len(leaf, rref);
2042 			subvol_info->dirid = btrfs_root_ref_dirid(leaf, rref);
2043 
2044 			item_off = btrfs_item_ptr_offset(leaf, slot) + sizeof(*rref);
2045 			read_extent_buffer(leaf, subvol_info->name,
2046 					   item_off, name_len);
2047 		} else {
2048 			ret = -ENOENT;
2049 			goto out;
2050 		}
2051 		ret = 0;
2052 	}
2053 
2054 out:
2055 	btrfs_put_root(root);
2056 out_free:
2057 	btrfs_free_path(path);
2058 	return ret;
2059 }
2060 
2061 #ifdef CONFIG_64BIT
2062 static int btrfs_ioctl_get_subvol_info_32(struct inode *inode, void __user *argp)
2063 {
2064 	struct btrfs_ioctl_get_subvol_info_args AUTO_KFREE(subvol_info);
2065 	struct btrfs_ioctl_get_subvol_info_args_32 AUTO_KFREE(subvol_info_32);
2066 	int ret;
2067 
2068 	subvol_info = kzalloc_obj(*subvol_info);
2069 	if (!subvol_info)
2070 		return -ENOMEM;
2071 
2072 	subvol_info_32 = kzalloc_obj(*subvol_info_32);
2073 	if (!subvol_info_32)
2074 		return -ENOMEM;
2075 
2076 	ret = _btrfs_ioctl_get_subvol_info(inode, subvol_info);
2077 	if (ret)
2078 		return ret;
2079 
2080 	subvol_info_32->treeid = subvol_info->treeid;
2081 	memcpy(subvol_info_32->name, subvol_info->name, sizeof(subvol_info_32->name));
2082 	subvol_info_32->parent_id = subvol_info->parent_id;
2083 	subvol_info_32->dirid = subvol_info->dirid;
2084 	subvol_info_32->generation = subvol_info->generation;
2085 	subvol_info_32->flags = subvol_info->flags;
2086 	memcpy(subvol_info_32->uuid, subvol_info->uuid, BTRFS_UUID_SIZE);
2087 	memcpy(subvol_info_32->parent_uuid, subvol_info->parent_uuid, BTRFS_UUID_SIZE);
2088 	memcpy(subvol_info_32->received_uuid, subvol_info->received_uuid, BTRFS_UUID_SIZE);
2089 	subvol_info_32->ctransid = subvol_info->ctransid;
2090 	subvol_info_32->otransid = subvol_info->otransid;
2091 	subvol_info_32->stransid = subvol_info->stransid;
2092 	subvol_info_32->rtransid = subvol_info->rtransid;
2093 	subvol_info_32->ctime.sec = subvol_info->ctime.sec;
2094 	subvol_info_32->ctime.nsec = subvol_info->ctime.nsec;
2095 	subvol_info_32->otime.sec = subvol_info->otime.sec;
2096 	subvol_info_32->otime.nsec = subvol_info->otime.nsec;
2097 	subvol_info_32->stime.sec = subvol_info->stime.sec;
2098 	subvol_info_32->stime.nsec = subvol_info->stime.nsec;
2099 	subvol_info_32->rtime.sec = subvol_info->rtime.sec;
2100 	subvol_info_32->rtime.nsec = subvol_info->rtime.nsec;
2101 
2102 	if (copy_to_user(argp, subvol_info_32, sizeof(*subvol_info_32)))
2103 		ret = -EFAULT;
2104 
2105 	return ret;
2106 }
2107 #endif
2108 
2109 static int btrfs_ioctl_get_subvol_info(struct inode *inode, void __user *argp)
2110 {
2111 	struct btrfs_ioctl_get_subvol_info_args AUTO_KFREE(subvol_info);
2112 	int ret;
2113 
2114 	subvol_info = kzalloc_obj(*subvol_info);
2115 	if (!subvol_info)
2116 		return -ENOMEM;
2117 
2118 	ret = _btrfs_ioctl_get_subvol_info(inode, subvol_info);
2119 	if (!ret && copy_to_user(argp, subvol_info, sizeof(*subvol_info)))
2120 		ret = -EFAULT;
2121 
2122 	return ret;
2123 }
2124 
2125 /*
2126  * Return ROOT_REF information of the subvolume containing this inode
2127  * except the subvolume name.
2128  */
2129 static int btrfs_ioctl_get_subvol_rootref(struct btrfs_root *root,
2130 					  void __user *argp)
2131 {
2132 	struct btrfs_ioctl_get_subvol_rootref_args AUTO_KFREE(rootrefs);
2133 	struct btrfs_root_ref *rref;
2134 	struct btrfs_path *path;
2135 	struct btrfs_key key;
2136 	struct extent_buffer *leaf;
2137 	u64 objectid;
2138 	int slot;
2139 	int ret;
2140 	u8 found;
2141 
2142 	path = btrfs_alloc_path();
2143 	if (!path)
2144 		return -ENOMEM;
2145 
2146 	rootrefs = memdup_user(argp, sizeof(*rootrefs));
2147 	if (IS_ERR(rootrefs)) {
2148 		btrfs_free_path(path);
2149 		return PTR_ERR(rootrefs);
2150 	}
2151 
2152 	objectid = btrfs_root_id(root);
2153 	key.objectid = objectid;
2154 	key.type = BTRFS_ROOT_REF_KEY;
2155 	key.offset = rootrefs->min_treeid;
2156 	found = 0;
2157 
2158 	root = root->fs_info->tree_root;
2159 	ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2160 	if (ret < 0) {
2161 		goto out;
2162 	} else if (path->slots[0] >=
2163 		   btrfs_header_nritems(path->nodes[0])) {
2164 		ret = btrfs_next_leaf(root, path);
2165 		if (ret < 0) {
2166 			goto out;
2167 		} else if (unlikely(ret > 0)) {
2168 			ret = -EUCLEAN;
2169 			goto out;
2170 		}
2171 	}
2172 	while (1) {
2173 		leaf = path->nodes[0];
2174 		slot = path->slots[0];
2175 
2176 		btrfs_item_key_to_cpu(leaf, &key, slot);
2177 		if (key.objectid != objectid || key.type != BTRFS_ROOT_REF_KEY) {
2178 			ret = 0;
2179 			goto out;
2180 		}
2181 
2182 		if (found == BTRFS_MAX_ROOTREF_BUFFER_NUM) {
2183 			ret = -EOVERFLOW;
2184 			goto out;
2185 		}
2186 
2187 		rref = btrfs_item_ptr(leaf, slot, struct btrfs_root_ref);
2188 		rootrefs->rootref[found].treeid = key.offset;
2189 		rootrefs->rootref[found].dirid =
2190 				  btrfs_root_ref_dirid(leaf, rref);
2191 		found++;
2192 
2193 		ret = btrfs_next_item(root, path);
2194 		if (ret < 0) {
2195 			goto out;
2196 		} else if (unlikely(ret > 0)) {
2197 			ret = -EUCLEAN;
2198 			goto out;
2199 		}
2200 	}
2201 
2202 out:
2203 	btrfs_free_path(path);
2204 
2205 	if (!ret || ret == -EOVERFLOW) {
2206 		rootrefs->num_items = found;
2207 		/* update min_treeid for next search */
2208 		if (found)
2209 			rootrefs->min_treeid =
2210 				rootrefs->rootref[found - 1].treeid + 1;
2211 		if (copy_to_user(argp, rootrefs, sizeof(*rootrefs)))
2212 			ret = -EFAULT;
2213 	}
2214 
2215 	return ret;
2216 }
2217 
2218 static noinline int btrfs_ioctl_snap_destroy(struct file *file,
2219 					     void __user *arg,
2220 					     bool destroy_v2)
2221 {
2222 	struct dentry *parent = file->f_path.dentry;
2223 	struct dentry *dentry;
2224 	struct inode *dir = d_inode(parent);
2225 	struct btrfs_fs_info *fs_info = inode_to_fs_info(dir);
2226 	struct inode *inode;
2227 	struct btrfs_root *root = BTRFS_I(dir)->root;
2228 	struct btrfs_root *dest = NULL;
2229 	struct btrfs_ioctl_vol_args AUTO_KFREE(vol_args);
2230 	struct btrfs_ioctl_vol_args_v2 AUTO_KFREE(vol_args2);
2231 	struct mnt_idmap *idmap = file_mnt_idmap(file);
2232 	char *subvol_name, *subvol_name_ptr = NULL;
2233 	int ret = 0;
2234 	bool destroy_parent = false;
2235 
2236 	/* We don't support snapshots with extent tree v2 yet. */
2237 	if (btrfs_fs_incompat(fs_info, EXTENT_TREE_V2)) {
2238 		btrfs_err(fs_info,
2239 			  "extent tree v2 doesn't support snapshot deletion yet");
2240 		return -EOPNOTSUPP;
2241 	}
2242 
2243 	if (destroy_v2) {
2244 		vol_args2 = memdup_user(arg, sizeof(*vol_args2));
2245 		if (IS_ERR(vol_args2))
2246 			return PTR_ERR(vol_args2);
2247 
2248 		if (vol_args2->flags & ~BTRFS_SUBVOL_DELETE_ARGS_MASK)
2249 			return -EOPNOTSUPP;
2250 
2251 		/*
2252 		 * If SPEC_BY_ID is not set, we are looking for the subvolume by
2253 		 * name, same as v1 currently does.
2254 		 */
2255 		if (!(vol_args2->flags & BTRFS_SUBVOL_SPEC_BY_ID)) {
2256 			ret = btrfs_check_ioctl_vol_args2_subvol_name(vol_args2);
2257 			if (ret < 0)
2258 				return ret;
2259 			subvol_name = vol_args2->name;
2260 
2261 			ret = mnt_want_write_file(file);
2262 			if (ret)
2263 				return ret;
2264 		} else {
2265 			struct inode *old_dir;
2266 
2267 			if (vol_args2->subvolid < BTRFS_FIRST_FREE_OBJECTID)
2268 				return -EINVAL;
2269 
2270 			ret = mnt_want_write_file(file);
2271 			if (ret)
2272 				return ret;
2273 
2274 			dentry = btrfs_get_dentry(fs_info->sb,
2275 					BTRFS_FIRST_FREE_OBJECTID,
2276 					vol_args2->subvolid, 0);
2277 			if (IS_ERR(dentry)) {
2278 				ret = PTR_ERR(dentry);
2279 				goto out_drop_write;
2280 			}
2281 
2282 			/*
2283 			 * Change the default parent since the subvolume being
2284 			 * deleted can be outside of the current mount point.
2285 			 */
2286 			parent = btrfs_get_parent(dentry);
2287 
2288 			/*
2289 			 * At this point dentry->d_name can point to '/' if the
2290 			 * subvolume we want to destroy is outsite of the
2291 			 * current mount point, so we need to release the
2292 			 * current dentry and execute the lookup to return a new
2293 			 * one with ->d_name pointing to the
2294 			 * <mount point>/subvol_name.
2295 			 */
2296 			dput(dentry);
2297 			if (IS_ERR(parent)) {
2298 				ret = PTR_ERR(parent);
2299 				goto out_drop_write;
2300 			}
2301 			old_dir = dir;
2302 			dir = d_inode(parent);
2303 
2304 			/*
2305 			 * If v2 was used with SPEC_BY_ID, a new parent was
2306 			 * allocated since the subvolume can be outside of the
2307 			 * current mount point. Later on we need to release this
2308 			 * new parent dentry.
2309 			 */
2310 			destroy_parent = true;
2311 
2312 			/*
2313 			 * On idmapped mounts, deletion via subvolid is
2314 			 * restricted to subvolumes that are immediate
2315 			 * ancestors of the inode referenced by the file
2316 			 * descriptor in the ioctl. Otherwise the idmapping
2317 			 * could potentially be abused to delete subvolumes
2318 			 * anywhere in the filesystem the user wouldn't be able
2319 			 * to delete without an idmapped mount.
2320 			 */
2321 			if (old_dir != dir && idmap != &nop_mnt_idmap) {
2322 				ret = -EOPNOTSUPP;
2323 				goto free_parent;
2324 			}
2325 
2326 			subvol_name_ptr = btrfs_get_subvol_name_from_objectid(
2327 						fs_info, vol_args2->subvolid);
2328 			if (IS_ERR(subvol_name_ptr)) {
2329 				ret = PTR_ERR(subvol_name_ptr);
2330 				goto free_parent;
2331 			}
2332 			/* subvol_name_ptr is already nul terminated */
2333 			subvol_name = (char *)kbasename(subvol_name_ptr);
2334 		}
2335 	} else {
2336 		vol_args = memdup_user(arg, sizeof(*vol_args));
2337 		if (IS_ERR(vol_args))
2338 			return PTR_ERR(vol_args);
2339 
2340 		ret = btrfs_check_ioctl_vol_args_path(vol_args);
2341 		if (ret < 0)
2342 			return ret;
2343 
2344 		subvol_name = vol_args->name;
2345 
2346 		ret = mnt_want_write_file(file);
2347 		if (ret)
2348 			return ret;
2349 	}
2350 
2351 	if (strchr(subvol_name, '/') ||
2352 	    strcmp(subvol_name, "..") == 0) {
2353 		ret = -EINVAL;
2354 		goto free_subvol_name;
2355 	}
2356 
2357 	if (!S_ISDIR(dir->i_mode)) {
2358 		ret = -ENOTDIR;
2359 		goto free_subvol_name;
2360 	}
2361 
2362 	dentry = start_removing_killable(idmap, parent, &QSTR(subvol_name));
2363 	if (IS_ERR(dentry)) {
2364 		ret = PTR_ERR(dentry);
2365 		goto out_end_removing;
2366 	}
2367 
2368 	inode = d_inode(dentry);
2369 	dest = BTRFS_I(inode)->root;
2370 	if (!capable(CAP_SYS_ADMIN)) {
2371 		/*
2372 		 * Regular user.  Only allow this with a special mount
2373 		 * option, when the user has write+exec access to the
2374 		 * subvol root, and when rmdir(2) would have been
2375 		 * allowed.
2376 		 *
2377 		 * Note that this is _not_ check that the subvol is
2378 		 * empty or doesn't contain data that we wouldn't
2379 		 * otherwise be able to delete.
2380 		 *
2381 		 * Users who want to delete empty subvols should try
2382 		 * rmdir(2).
2383 		 */
2384 		ret = -EPERM;
2385 		if (!btrfs_test_opt(fs_info, USER_SUBVOL_RM_ALLOWED))
2386 			goto out_end_removing;
2387 
2388 		/*
2389 		 * Do not allow deletion if the parent dir is the same
2390 		 * as the dir to be deleted.  That means the ioctl
2391 		 * must be called on the dentry referencing the root
2392 		 * of the subvol, not a random directory contained
2393 		 * within it.
2394 		 */
2395 		ret = -EINVAL;
2396 		if (root == dest)
2397 			goto out_end_removing;
2398 
2399 		ret = inode_permission(idmap, inode, MAY_WRITE | MAY_EXEC);
2400 		if (ret)
2401 			goto out_end_removing;
2402 	}
2403 
2404 	/* check if subvolume may be deleted by a user */
2405 	ret = may_delete_dentry(idmap, dir, dentry, true);
2406 	if (ret)
2407 		goto out_end_removing;
2408 
2409 	if (btrfs_ino(BTRFS_I(inode)) != BTRFS_FIRST_FREE_OBJECTID) {
2410 		ret = -EINVAL;
2411 		goto out_end_removing;
2412 	}
2413 
2414 	btrfs_inode_lock(BTRFS_I(inode), 0);
2415 	ret = btrfs_delete_subvolume(BTRFS_I(dir), dentry);
2416 	btrfs_inode_unlock(BTRFS_I(inode), 0);
2417 	if (!ret)
2418 		d_delete_notify(dir, dentry);
2419 
2420 out_end_removing:
2421 	end_removing(dentry);
2422 free_subvol_name:
2423 	kfree(subvol_name_ptr);
2424 free_parent:
2425 	if (destroy_parent)
2426 		dput(parent);
2427 out_drop_write:
2428 	mnt_drop_write_file(file);
2429 	return ret;
2430 }
2431 
2432 static int btrfs_ioctl_defrag(struct file *file, void __user *argp)
2433 {
2434 	struct inode *inode = file_inode(file);
2435 	struct btrfs_root *root = BTRFS_I(inode)->root;
2436 	struct btrfs_ioctl_defrag_range_args range = {0};
2437 	int ret;
2438 
2439 	ret = mnt_want_write_file(file);
2440 	if (ret)
2441 		return ret;
2442 
2443 	if (btrfs_root_readonly(root)) {
2444 		ret = -EROFS;
2445 		goto out;
2446 	}
2447 
2448 	switch (inode->i_mode & S_IFMT) {
2449 	case S_IFDIR:
2450 		if (!capable(CAP_SYS_ADMIN)) {
2451 			ret = -EPERM;
2452 			goto out;
2453 		}
2454 		ret = btrfs_defrag_root(root);
2455 		break;
2456 	case S_IFREG:
2457 		/*
2458 		 * Note that this does not check the file descriptor for write
2459 		 * access. This prevents defragmenting executables that are
2460 		 * running and allows defrag on files open in read-only mode.
2461 		 */
2462 		if (!capable(CAP_SYS_ADMIN) &&
2463 		    inode_permission(&nop_mnt_idmap, inode, MAY_WRITE)) {
2464 			ret = -EPERM;
2465 			goto out;
2466 		}
2467 
2468 		/*
2469 		 * Don't allow defrag on pre-content watched files, as it could
2470 		 * populate the page cache with 0's via readahead.
2471 		 */
2472 		if (unlikely(FMODE_FSNOTIFY_HSM(file->f_mode))) {
2473 			ret = -EINVAL;
2474 			goto out;
2475 		}
2476 
2477 		if (argp) {
2478 			if (copy_from_user(&range, argp, sizeof(range))) {
2479 				ret = -EFAULT;
2480 				goto out;
2481 			}
2482 			if (range.flags & ~BTRFS_DEFRAG_RANGE_FLAGS_SUPP) {
2483 				ret = -EOPNOTSUPP;
2484 				goto out;
2485 			}
2486 			if ((range.flags & BTRFS_DEFRAG_RANGE_COMPRESS) &&
2487 			    (range.flags & BTRFS_DEFRAG_RANGE_NOCOMPRESS)) {
2488 				ret = -EINVAL;
2489 				goto out;
2490 			}
2491 			/* Compression or no-compression require to start the IO. */
2492 			if ((range.flags & BTRFS_DEFRAG_RANGE_COMPRESS) ||
2493 			    (range.flags & BTRFS_DEFRAG_RANGE_NOCOMPRESS)) {
2494 				range.flags |= BTRFS_DEFRAG_RANGE_START_IO;
2495 				range.extent_thresh = (u32)-1;
2496 			}
2497 		} else {
2498 			/* the rest are all set to zero by kzalloc */
2499 			range.len = (u64)-1;
2500 		}
2501 		ret = btrfs_defrag_file(BTRFS_I(file_inode(file)), &file->f_ra,
2502 					&range, BTRFS_OLDEST_GENERATION, 0);
2503 		if (ret > 0)
2504 			ret = 0;
2505 		break;
2506 	default:
2507 		ret = -EINVAL;
2508 	}
2509 out:
2510 	mnt_drop_write_file(file);
2511 	return ret;
2512 }
2513 
2514 static long btrfs_ioctl_add_dev(struct btrfs_fs_info *fs_info, void __user *arg)
2515 {
2516 	struct btrfs_ioctl_vol_args AUTO_KFREE(vol_args);
2517 	bool restore_op = false;
2518 	int ret;
2519 
2520 	if (!capable(CAP_SYS_ADMIN))
2521 		return -EPERM;
2522 
2523 	if (btrfs_fs_incompat(fs_info, EXTENT_TREE_V2)) {
2524 		btrfs_err(fs_info, "device add not supported on extent tree v2 yet");
2525 		return -EINVAL;
2526 	}
2527 
2528 	if (fs_info->fs_devices->temp_fsid) {
2529 		btrfs_err(fs_info,
2530 			  "device add not supported on cloned temp-fsid mount");
2531 		return -EINVAL;
2532 	}
2533 
2534 	if (!btrfs_exclop_start(fs_info, BTRFS_EXCLOP_DEV_ADD)) {
2535 		if (!btrfs_exclop_start_try_lock(fs_info, BTRFS_EXCLOP_DEV_ADD))
2536 			return BTRFS_ERROR_DEV_EXCL_RUN_IN_PROGRESS;
2537 
2538 		/*
2539 		 * We can do the device add because we have a paused balanced,
2540 		 * change the exclusive op type and remember we should bring
2541 		 * back the paused balance
2542 		 */
2543 		fs_info->exclusive_operation = BTRFS_EXCLOP_DEV_ADD;
2544 		btrfs_exclop_start_unlock(fs_info);
2545 		restore_op = true;
2546 	}
2547 
2548 	vol_args = memdup_user(arg, sizeof(*vol_args));
2549 	if (IS_ERR(vol_args)) {
2550 		ret = PTR_ERR(vol_args);
2551 		goto out;
2552 	}
2553 
2554 	ret = btrfs_check_ioctl_vol_args_path(vol_args);
2555 	if (ret < 0)
2556 		goto out;
2557 
2558 	ret = btrfs_init_new_device(fs_info, vol_args->name);
2559 
2560 	if (!ret)
2561 		btrfs_info(fs_info, "disk added %s", vol_args->name);
2562 
2563 out:
2564 	if (restore_op)
2565 		btrfs_exclop_balance(fs_info, BTRFS_EXCLOP_BALANCE_PAUSED);
2566 	else
2567 		btrfs_exclop_finish(fs_info);
2568 	return ret;
2569 }
2570 
2571 static long btrfs_ioctl_rm_dev_v2(struct file *file, void __user *arg)
2572 {
2573 	BTRFS_DEV_LOOKUP_ARGS(args);
2574 	struct inode *inode = file_inode(file);
2575 	struct btrfs_fs_info *fs_info = inode_to_fs_info(inode);
2576 	struct btrfs_ioctl_vol_args_v2 AUTO_KFREE(vol_args);
2577 	struct file *bdev_file = NULL;
2578 	int ret;
2579 	bool cancel = false;
2580 
2581 	if (!capable(CAP_SYS_ADMIN))
2582 		return -EPERM;
2583 
2584 	vol_args = memdup_user(arg, sizeof(*vol_args));
2585 	if (IS_ERR(vol_args))
2586 		return PTR_ERR(vol_args);
2587 
2588 	if (vol_args->flags & ~BTRFS_DEVICE_REMOVE_ARGS_MASK) {
2589 		ret = -EOPNOTSUPP;
2590 		goto out;
2591 	}
2592 
2593 	ret = btrfs_check_ioctl_vol_args2_subvol_name(vol_args);
2594 	if (ret < 0)
2595 		goto out;
2596 
2597 	if (vol_args->flags & BTRFS_DEVICE_SPEC_BY_ID) {
2598 		args.devid = vol_args->devid;
2599 	} else if (!strcmp("cancel", vol_args->name)) {
2600 		cancel = true;
2601 	} else {
2602 		ret = btrfs_get_dev_args_from_path(fs_info, &args, vol_args->name);
2603 		if (ret)
2604 			goto out;
2605 	}
2606 
2607 	ret = mnt_want_write_file(file);
2608 	if (ret)
2609 		goto out;
2610 
2611 	ret = exclop_start_or_cancel_reloc(fs_info, BTRFS_EXCLOP_DEV_REMOVE,
2612 					   cancel);
2613 	if (ret)
2614 		goto err_drop;
2615 
2616 	/* Exclusive operation is now claimed */
2617 	ret = btrfs_rm_device(fs_info, &args, &bdev_file);
2618 
2619 	btrfs_exclop_finish(fs_info);
2620 
2621 	if (!ret) {
2622 		if (vol_args->flags & BTRFS_DEVICE_SPEC_BY_ID)
2623 			btrfs_info(fs_info, "device deleted: id %llu",
2624 					vol_args->devid);
2625 		else
2626 			btrfs_info(fs_info, "device deleted: %s",
2627 					vol_args->name);
2628 	}
2629 err_drop:
2630 	mnt_drop_write_file(file);
2631 	if (bdev_file)
2632 		bdev_fput(bdev_file);
2633 out:
2634 	btrfs_put_dev_args_from_path(&args);
2635 	return ret;
2636 }
2637 
2638 static long btrfs_ioctl_rm_dev(struct file *file, void __user *arg)
2639 {
2640 	BTRFS_DEV_LOOKUP_ARGS(args);
2641 	struct inode *inode = file_inode(file);
2642 	struct btrfs_fs_info *fs_info = inode_to_fs_info(inode);
2643 	struct btrfs_ioctl_vol_args AUTO_KFREE(vol_args);
2644 	struct file *bdev_file = NULL;
2645 	int ret;
2646 	bool cancel = false;
2647 
2648 	if (!capable(CAP_SYS_ADMIN))
2649 		return -EPERM;
2650 
2651 	vol_args = memdup_user(arg, sizeof(*vol_args));
2652 	if (IS_ERR(vol_args))
2653 		return PTR_ERR(vol_args);
2654 
2655 	ret = btrfs_check_ioctl_vol_args_path(vol_args);
2656 	if (ret < 0)
2657 		return ret;
2658 
2659 	if (!strcmp("cancel", vol_args->name)) {
2660 		cancel = true;
2661 	} else {
2662 		ret = btrfs_get_dev_args_from_path(fs_info, &args, vol_args->name);
2663 		if (ret)
2664 			goto out;
2665 	}
2666 
2667 	ret = mnt_want_write_file(file);
2668 	if (ret)
2669 		goto out;
2670 
2671 	ret = exclop_start_or_cancel_reloc(fs_info, BTRFS_EXCLOP_DEV_REMOVE,
2672 					   cancel);
2673 	if (ret == 0) {
2674 		ret = btrfs_rm_device(fs_info, &args, &bdev_file);
2675 		if (!ret)
2676 			btrfs_info(fs_info, "disk deleted %s", vol_args->name);
2677 		btrfs_exclop_finish(fs_info);
2678 	}
2679 
2680 	mnt_drop_write_file(file);
2681 	if (bdev_file)
2682 		bdev_fput(bdev_file);
2683 out:
2684 	btrfs_put_dev_args_from_path(&args);
2685 	return ret;
2686 }
2687 
2688 static long btrfs_ioctl_fs_info(const struct btrfs_fs_info *fs_info,
2689 				void __user *arg)
2690 {
2691 	struct btrfs_ioctl_fs_info_args AUTO_KFREE(fi_args);
2692 	struct btrfs_device *device;
2693 	struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
2694 	u64 flags_in;
2695 
2696 	fi_args = memdup_user(arg, sizeof(*fi_args));
2697 	if (IS_ERR(fi_args))
2698 		return PTR_ERR(fi_args);
2699 
2700 	flags_in = fi_args->flags;
2701 	memset(fi_args, 0, sizeof(*fi_args));
2702 
2703 	rcu_read_lock();
2704 	fi_args->num_devices = fs_devices->num_devices;
2705 
2706 	list_for_each_entry_rcu(device, &fs_devices->devices, dev_list) {
2707 		if (device->devid > fi_args->max_id)
2708 			fi_args->max_id = device->devid;
2709 	}
2710 	rcu_read_unlock();
2711 
2712 	memcpy(&fi_args->fsid, fs_devices->fsid, sizeof(fi_args->fsid));
2713 	fi_args->nodesize = fs_info->nodesize;
2714 	fi_args->sectorsize = fs_info->sectorsize;
2715 	fi_args->clone_alignment = fs_info->sectorsize;
2716 
2717 	if (flags_in & BTRFS_FS_INFO_FLAG_CSUM_INFO) {
2718 		fi_args->csum_type = btrfs_super_csum_type(fs_info->super_copy);
2719 		fi_args->csum_size = btrfs_super_csum_size(fs_info->super_copy);
2720 		fi_args->flags |= BTRFS_FS_INFO_FLAG_CSUM_INFO;
2721 	}
2722 
2723 	if (flags_in & BTRFS_FS_INFO_FLAG_GENERATION) {
2724 		fi_args->generation = btrfs_get_fs_generation(fs_info);
2725 		fi_args->flags |= BTRFS_FS_INFO_FLAG_GENERATION;
2726 	}
2727 
2728 	if (flags_in & BTRFS_FS_INFO_FLAG_METADATA_UUID) {
2729 		memcpy(&fi_args->metadata_uuid, fs_devices->metadata_uuid,
2730 		       sizeof(fi_args->metadata_uuid));
2731 		fi_args->flags |= BTRFS_FS_INFO_FLAG_METADATA_UUID;
2732 	}
2733 
2734 	if (copy_to_user(arg, fi_args, sizeof(*fi_args)))
2735 		return -EFAULT;
2736 
2737 	return 0;
2738 }
2739 
2740 static long btrfs_ioctl_dev_info(const struct btrfs_fs_info *fs_info,
2741 				 void __user *arg)
2742 {
2743 	BTRFS_DEV_LOOKUP_ARGS(args);
2744 	struct btrfs_ioctl_dev_info_args AUTO_KFREE(di_args);
2745 	struct btrfs_device *dev;
2746 	int ret = 0;
2747 
2748 	di_args = memdup_user(arg, sizeof(*di_args));
2749 	if (IS_ERR(di_args))
2750 		return PTR_ERR(di_args);
2751 
2752 	args.devid = di_args->devid;
2753 	if (!btrfs_is_empty_uuid(di_args->uuid))
2754 		args.uuid = di_args->uuid;
2755 
2756 	rcu_read_lock();
2757 	dev = btrfs_find_device(fs_info->fs_devices, &args);
2758 	if (!dev) {
2759 		ret = -ENODEV;
2760 		goto out;
2761 	}
2762 
2763 	di_args->devid = dev->devid;
2764 	di_args->bytes_used = btrfs_device_get_bytes_used(dev);
2765 	di_args->total_bytes = btrfs_device_get_total_bytes(dev);
2766 	memcpy(di_args->uuid, dev->uuid, sizeof(di_args->uuid));
2767 	memcpy(di_args->fsid, dev->fs_devices->fsid, BTRFS_UUID_SIZE);
2768 	if (dev->name)
2769 		strscpy(di_args->path, btrfs_dev_name(dev), sizeof(di_args->path));
2770 	else
2771 		di_args->path[0] = '\0';
2772 
2773 out:
2774 	rcu_read_unlock();
2775 	if (ret == 0 && copy_to_user(arg, di_args, sizeof(*di_args)))
2776 		ret = -EFAULT;
2777 
2778 	return ret;
2779 }
2780 
2781 static long btrfs_ioctl_default_subvol(struct file *file, void __user *argp)
2782 {
2783 	struct inode *inode = file_inode(file);
2784 	struct btrfs_fs_info *fs_info = inode_to_fs_info(inode);
2785 	struct btrfs_root *root = BTRFS_I(inode)->root;
2786 	struct btrfs_root *new_root;
2787 	struct btrfs_dir_item *di;
2788 	struct btrfs_trans_handle *trans;
2789 	struct btrfs_path *path = NULL;
2790 	struct btrfs_disk_key disk_key;
2791 	struct fscrypt_str name = FSTR_INIT("default", 7);
2792 	u64 objectid = 0;
2793 	u64 dir_id;
2794 	int ret;
2795 
2796 	if (!capable(CAP_SYS_ADMIN))
2797 		return -EPERM;
2798 
2799 	ret = mnt_want_write_file(file);
2800 	if (ret)
2801 		return ret;
2802 
2803 	if (copy_from_user(&objectid, argp, sizeof(objectid))) {
2804 		ret = -EFAULT;
2805 		goto out;
2806 	}
2807 
2808 	if (!objectid)
2809 		objectid = BTRFS_FS_TREE_OBJECTID;
2810 
2811 	new_root = btrfs_get_fs_root(fs_info, objectid, true);
2812 	if (IS_ERR(new_root)) {
2813 		ret = PTR_ERR(new_root);
2814 		goto out;
2815 	}
2816 	if (!btrfs_is_fstree(btrfs_root_id(new_root))) {
2817 		ret = -ENOENT;
2818 		goto out_free;
2819 	}
2820 
2821 	path = btrfs_alloc_path();
2822 	if (!path) {
2823 		ret = -ENOMEM;
2824 		goto out_free;
2825 	}
2826 
2827 	trans = btrfs_start_transaction(root, 1);
2828 	if (IS_ERR(trans)) {
2829 		ret = PTR_ERR(trans);
2830 		goto out_free;
2831 	}
2832 
2833 	dir_id = btrfs_super_root_dir(fs_info->super_copy);
2834 	di = btrfs_lookup_dir_item(trans, fs_info->tree_root, path,
2835 				   dir_id, &name, 1);
2836 	if (IS_ERR_OR_NULL(di)) {
2837 		btrfs_release_path(path);
2838 		btrfs_end_transaction(trans);
2839 		if (di)
2840 			ret = PTR_ERR(di);
2841 		else
2842 			ret = -ENOENT;
2843 		btrfs_err(fs_info,
2844 			  "could not find default diritem for dir %llu: %d",
2845 			  dir_id, ret);
2846 		goto out_free;
2847 	}
2848 
2849 	btrfs_cpu_key_to_disk(&disk_key, &new_root->root_key);
2850 	btrfs_set_dir_item_key(path->nodes[0], di, &disk_key);
2851 	btrfs_release_path(path);
2852 
2853 	btrfs_set_fs_incompat(fs_info, DEFAULT_SUBVOL);
2854 	btrfs_end_transaction(trans);
2855 out_free:
2856 	btrfs_put_root(new_root);
2857 	btrfs_free_path(path);
2858 out:
2859 	mnt_drop_write_file(file);
2860 	return ret;
2861 }
2862 
2863 static void get_block_group_info(struct list_head *groups_list,
2864 				 struct btrfs_ioctl_space_info *space)
2865 {
2866 	struct btrfs_block_group *block_group;
2867 
2868 	space->total_bytes = 0;
2869 	space->used_bytes = 0;
2870 	space->flags = 0;
2871 	list_for_each_entry(block_group, groups_list, list) {
2872 		space->flags = block_group->flags;
2873 		space->total_bytes += block_group->length;
2874 		space->used_bytes += block_group->used;
2875 	}
2876 }
2877 
2878 static long btrfs_ioctl_space_info(struct btrfs_fs_info *fs_info,
2879 				   void __user *arg)
2880 {
2881 	struct btrfs_ioctl_space_args space_args = { 0 };
2882 	struct btrfs_ioctl_space_info space;
2883 	struct btrfs_ioctl_space_info *dest;
2884 	struct btrfs_ioctl_space_info AUTO_KFREE(dest_orig);
2885 	struct btrfs_ioctl_space_info __user *user_dest;
2886 	struct btrfs_space_info *info;
2887 	static const u64 types[] = {
2888 		BTRFS_BLOCK_GROUP_DATA,
2889 		BTRFS_BLOCK_GROUP_SYSTEM,
2890 		BTRFS_BLOCK_GROUP_METADATA,
2891 		BTRFS_BLOCK_GROUP_DATA | BTRFS_BLOCK_GROUP_METADATA
2892 	};
2893 	int num_types = 4;
2894 	int alloc_size;
2895 	int ret = 0;
2896 	u64 slot_count = 0;
2897 	int i, c;
2898 
2899 	if (copy_from_user(&space_args,
2900 			   (struct btrfs_ioctl_space_args __user *)arg,
2901 			   sizeof(space_args)))
2902 		return -EFAULT;
2903 
2904 	for (i = 0; i < num_types; i++) {
2905 		struct btrfs_space_info *tmp;
2906 
2907 		info = NULL;
2908 		list_for_each_entry(tmp, &fs_info->space_info, list) {
2909 			if (tmp->flags == types[i]) {
2910 				info = tmp;
2911 				break;
2912 			}
2913 		}
2914 
2915 		if (!info)
2916 			continue;
2917 
2918 		down_read(&info->groups_sem);
2919 		for (c = 0; c < BTRFS_NR_RAID_TYPES; c++) {
2920 			if (!list_empty(&info->block_groups[c]))
2921 				slot_count++;
2922 		}
2923 		up_read(&info->groups_sem);
2924 	}
2925 
2926 	/*
2927 	 * Global block reserve, exported as a space_info
2928 	 */
2929 	slot_count++;
2930 
2931 	/* space_slots == 0 means they are asking for a count */
2932 	if (space_args.space_slots == 0) {
2933 		space_args.total_spaces = slot_count;
2934 		goto out;
2935 	}
2936 
2937 	slot_count = min_t(u64, space_args.space_slots, slot_count);
2938 
2939 	alloc_size = sizeof(*dest) * slot_count;
2940 
2941 	/* we generally have at most 6 or so space infos, one for each raid
2942 	 * level.  So, a whole page should be more than enough for everyone
2943 	 */
2944 	if (alloc_size > PAGE_SIZE)
2945 		return -ENOMEM;
2946 
2947 	space_args.total_spaces = 0;
2948 	dest = kzalloc(alloc_size, GFP_KERNEL);
2949 	if (!dest)
2950 		return -ENOMEM;
2951 	dest_orig = dest;
2952 
2953 	/* now we have a buffer to copy into */
2954 	for (i = 0; i < num_types; i++) {
2955 		struct btrfs_space_info *tmp;
2956 
2957 		if (!slot_count)
2958 			break;
2959 
2960 		info = NULL;
2961 		list_for_each_entry(tmp, &fs_info->space_info, list) {
2962 			if (tmp->flags == types[i]) {
2963 				info = tmp;
2964 				break;
2965 			}
2966 		}
2967 
2968 		if (!info)
2969 			continue;
2970 		down_read(&info->groups_sem);
2971 		for (c = 0; c < BTRFS_NR_RAID_TYPES; c++) {
2972 			if (!list_empty(&info->block_groups[c])) {
2973 				get_block_group_info(&info->block_groups[c],
2974 						     &space);
2975 				memcpy(dest, &space, sizeof(space));
2976 				dest++;
2977 				space_args.total_spaces++;
2978 				slot_count--;
2979 			}
2980 			if (!slot_count)
2981 				break;
2982 		}
2983 		up_read(&info->groups_sem);
2984 	}
2985 
2986 	/*
2987 	 * Add global block reserve
2988 	 */
2989 	if (slot_count) {
2990 		struct btrfs_block_rsv *block_rsv = &fs_info->global_block_rsv;
2991 
2992 		spin_lock(&block_rsv->lock);
2993 		space.total_bytes = block_rsv->size;
2994 		space.used_bytes = block_rsv->size - block_rsv->reserved;
2995 		spin_unlock(&block_rsv->lock);
2996 		space.flags = BTRFS_SPACE_INFO_GLOBAL_RSV;
2997 		memcpy(dest, &space, sizeof(space));
2998 		space_args.total_spaces++;
2999 	}
3000 
3001 	user_dest = (struct btrfs_ioctl_space_info __user *)
3002 		(arg + sizeof(struct btrfs_ioctl_space_args));
3003 
3004 	if (copy_to_user(user_dest, dest_orig,
3005 		 space_args.total_spaces * sizeof(*dest_orig)))
3006 		return -EFAULT;
3007 
3008 out:
3009 	if (ret == 0 && copy_to_user(arg, &space_args, sizeof(space_args)))
3010 		ret = -EFAULT;
3011 
3012 	return ret;
3013 }
3014 
3015 static noinline long btrfs_ioctl_start_sync(struct btrfs_root *root,
3016 					    void __user *argp)
3017 {
3018 	struct btrfs_trans_handle *trans;
3019 	u64 transid;
3020 
3021 	/*
3022 	 * Start orphan cleanup here for the given root in case it hasn't been
3023 	 * started already by other means. Errors are handled in the other
3024 	 * functions during transaction commit.
3025 	 */
3026 	btrfs_orphan_cleanup(root);
3027 
3028 	trans = btrfs_attach_transaction_barrier(root);
3029 	if (IS_ERR(trans)) {
3030 		if (PTR_ERR(trans) != -ENOENT)
3031 			return PTR_ERR(trans);
3032 
3033 		/* No running transaction, don't bother */
3034 		transid = btrfs_get_last_trans_committed(root->fs_info);
3035 		goto out;
3036 	}
3037 	transid = trans->transid;
3038 	btrfs_commit_transaction_async(trans);
3039 out:
3040 	if (argp)
3041 		if (copy_to_user(argp, &transid, sizeof(transid)))
3042 			return -EFAULT;
3043 	return 0;
3044 }
3045 
3046 static noinline long btrfs_ioctl_wait_sync(struct btrfs_fs_info *fs_info,
3047 					   void __user *argp)
3048 {
3049 	/* By default wait for the current transaction. */
3050 	u64 transid = 0;
3051 
3052 	if (argp)
3053 		if (copy_from_user(&transid, argp, sizeof(transid)))
3054 			return -EFAULT;
3055 
3056 	return btrfs_wait_for_commit(fs_info, transid);
3057 }
3058 
3059 static long btrfs_ioctl_scrub(struct file *file, void __user *arg)
3060 {
3061 	struct btrfs_fs_info *fs_info = inode_to_fs_info(file_inode(file));
3062 	struct btrfs_ioctl_scrub_args AUTO_KFREE(sa);
3063 	int ret;
3064 
3065 	if (!capable(CAP_SYS_ADMIN))
3066 		return -EPERM;
3067 
3068 	if (btrfs_fs_incompat(fs_info, EXTENT_TREE_V2)) {
3069 		btrfs_err(fs_info, "scrub: extent tree v2 not yet supported");
3070 		return -EINVAL;
3071 	}
3072 
3073 	sa = memdup_user(arg, sizeof(*sa));
3074 	if (IS_ERR(sa))
3075 		return PTR_ERR(sa);
3076 
3077 	if (sa->flags & ~BTRFS_SCRUB_SUPPORTED_FLAGS)
3078 		return -EOPNOTSUPP;
3079 
3080 	if (!(sa->flags & BTRFS_SCRUB_READONLY)) {
3081 		ret = mnt_want_write_file(file);
3082 		if (ret)
3083 			return ret;
3084 	}
3085 
3086 	ret = btrfs_scrub_dev(fs_info, sa->devid, sa->start, sa->end,
3087 			      &sa->progress, sa->flags & BTRFS_SCRUB_READONLY,
3088 			      false);
3089 
3090 	/*
3091 	 * Copy scrub args to user space even if btrfs_scrub_dev() returned an
3092 	 * error. This is important as it allows user space to know how much
3093 	 * progress scrub has done. For example, if scrub is canceled we get
3094 	 * -ECANCELED from btrfs_scrub_dev() and return that error back to user
3095 	 * space. Later user space can inspect the progress from the structure
3096 	 * btrfs_ioctl_scrub_args and resume scrub from where it left off
3097 	 * previously (btrfs-progs does this).
3098 	 * If we fail to copy the btrfs_ioctl_scrub_args structure to user space
3099 	 * then return -EFAULT to signal the structure was not copied or it may
3100 	 * be corrupt and unreliable due to a partial copy.
3101 	 */
3102 	if (copy_to_user(arg, sa, sizeof(*sa)))
3103 		ret = -EFAULT;
3104 
3105 	if (!(sa->flags & BTRFS_SCRUB_READONLY))
3106 		mnt_drop_write_file(file);
3107 
3108 	return ret;
3109 }
3110 
3111 static long btrfs_ioctl_scrub_cancel(struct btrfs_fs_info *fs_info)
3112 {
3113 	if (!capable(CAP_SYS_ADMIN))
3114 		return -EPERM;
3115 
3116 	return btrfs_scrub_cancel(fs_info);
3117 }
3118 
3119 static long btrfs_ioctl_scrub_progress(struct btrfs_fs_info *fs_info,
3120 				       void __user *arg)
3121 {
3122 	struct btrfs_ioctl_scrub_args AUTO_KFREE(sa);
3123 	int ret;
3124 
3125 	if (!capable(CAP_SYS_ADMIN))
3126 		return -EPERM;
3127 
3128 	sa = memdup_user(arg, sizeof(*sa));
3129 	if (IS_ERR(sa))
3130 		return PTR_ERR(sa);
3131 
3132 	ret = btrfs_scrub_progress(fs_info, sa->devid, &sa->progress);
3133 
3134 	if (ret == 0 && copy_to_user(arg, sa, sizeof(*sa)))
3135 		return -EFAULT;
3136 
3137 	return ret;
3138 }
3139 
3140 static long btrfs_ioctl_get_dev_stats(struct btrfs_fs_info *fs_info,
3141 				      void __user *arg)
3142 {
3143 	struct btrfs_ioctl_get_dev_stats AUTO_KFREE(sa);
3144 	int ret;
3145 
3146 	sa = memdup_user(arg, sizeof(*sa));
3147 	if (IS_ERR(sa))
3148 		return PTR_ERR(sa);
3149 
3150 	if ((sa->flags & BTRFS_DEV_STATS_RESET) && !capable(CAP_SYS_ADMIN))
3151 		return -EPERM;
3152 
3153 	ret = btrfs_get_dev_stats(fs_info, sa);
3154 
3155 	if (ret == 0 && copy_to_user(arg, sa, sizeof(*sa)))
3156 		return -EFAULT;
3157 
3158 	return ret;
3159 }
3160 
3161 static long btrfs_ioctl_dev_replace(struct btrfs_fs_info *fs_info,
3162 				    void __user *arg)
3163 {
3164 	struct btrfs_ioctl_dev_replace_args AUTO_KFREE(p);
3165 	int ret;
3166 
3167 	if (!capable(CAP_SYS_ADMIN))
3168 		return -EPERM;
3169 
3170 	if (btrfs_fs_incompat(fs_info, EXTENT_TREE_V2)) {
3171 		btrfs_err(fs_info, "device replace not supported on extent tree v2 yet");
3172 		return -EINVAL;
3173 	}
3174 
3175 	p = memdup_user(arg, sizeof(*p));
3176 	if (IS_ERR(p))
3177 		return PTR_ERR(p);
3178 
3179 	switch (p->cmd) {
3180 	case BTRFS_IOCTL_DEV_REPLACE_CMD_START:
3181 		if (sb_rdonly(fs_info->sb))
3182 			return -EROFS;
3183 		if (!btrfs_exclop_start(fs_info, BTRFS_EXCLOP_DEV_REPLACE)) {
3184 			ret = BTRFS_ERROR_DEV_EXCL_RUN_IN_PROGRESS;
3185 		} else {
3186 			ret = btrfs_dev_replace_by_ioctl(fs_info, p);
3187 			btrfs_exclop_finish(fs_info);
3188 		}
3189 		break;
3190 	case BTRFS_IOCTL_DEV_REPLACE_CMD_STATUS:
3191 		btrfs_dev_replace_status(fs_info, p);
3192 		ret = 0;
3193 		break;
3194 	case BTRFS_IOCTL_DEV_REPLACE_CMD_CANCEL:
3195 		p->result = btrfs_dev_replace_cancel(fs_info);
3196 		ret = 0;
3197 		break;
3198 	default:
3199 		ret = -EINVAL;
3200 		break;
3201 	}
3202 
3203 	if ((ret == 0 || ret == -ECANCELED) && copy_to_user(arg, p, sizeof(*p)))
3204 		return -EFAULT;
3205 
3206 	return ret;
3207 }
3208 
3209 static long btrfs_ioctl_ino_to_path(struct btrfs_root *root, void __user *arg)
3210 {
3211 	int ret = 0;
3212 	int i;
3213 	u64 rel_ptr;
3214 	int size;
3215 	struct btrfs_ioctl_ino_path_args AUTO_KFREE(ipa);
3216 	struct inode_fs_paths *ipath __free(inode_fs_paths) = NULL;
3217 	struct btrfs_path *path;
3218 
3219 	if (!capable(CAP_DAC_READ_SEARCH))
3220 		return -EPERM;
3221 
3222 	path = btrfs_alloc_path();
3223 	if (!path) {
3224 		ret = -ENOMEM;
3225 		goto out;
3226 	}
3227 
3228 	ipa = memdup_user(arg, sizeof(*ipa));
3229 	if (IS_ERR(ipa)) {
3230 		ret = PTR_ERR(ipa);
3231 		ipa = NULL;
3232 		goto out;
3233 	}
3234 
3235 	size = min_t(u32, ipa->size, 4096);
3236 	ipath = init_ipath(size, root, path);
3237 	if (IS_ERR(ipath)) {
3238 		ret = PTR_ERR(ipath);
3239 		ipath = NULL;
3240 		goto out;
3241 	}
3242 
3243 	ret = paths_from_inode(ipa->inum, ipath);
3244 	if (ret < 0)
3245 		goto out;
3246 
3247 	for (i = 0; i < ipath->fspath->elem_cnt; ++i) {
3248 		rel_ptr = ipath->fspath->val[i] -
3249 			  (u64)(unsigned long)ipath->fspath->val;
3250 		ipath->fspath->val[i] = rel_ptr;
3251 	}
3252 
3253 	btrfs_free_path(path);
3254 	path = NULL;
3255 	ret = copy_to_user((void __user *)(unsigned long)ipa->fspath,
3256 			   ipath->fspath, size);
3257 	if (ret) {
3258 		ret = -EFAULT;
3259 		goto out;
3260 	}
3261 
3262 out:
3263 	btrfs_free_path(path);
3264 
3265 	return ret;
3266 }
3267 
3268 static long btrfs_ioctl_logical_to_ino(struct btrfs_fs_info *fs_info,
3269 					void __user *arg, int version)
3270 {
3271 	int ret = 0;
3272 	int size;
3273 	struct btrfs_ioctl_logical_ino_args AUTO_KFREE(loi);
3274 	struct btrfs_data_container AUTO_KVFREE(inodes);
3275 	bool ignore_offset;
3276 
3277 	if (!capable(CAP_SYS_ADMIN))
3278 		return -EPERM;
3279 
3280 	loi = memdup_user(arg, sizeof(*loi));
3281 	if (IS_ERR(loi))
3282 		return PTR_ERR(loi);
3283 
3284 	if (version == 1) {
3285 		ignore_offset = false;
3286 		size = min_t(u32, loi->size, SZ_64K);
3287 	} else {
3288 		/* All reserved bits must be 0 for now */
3289 		if (memchr_inv(loi->reserved, 0, sizeof(loi->reserved)))
3290 			return -EINVAL;
3291 
3292 		/* Only accept flags we have defined so far */
3293 		if (loi->flags & ~(BTRFS_LOGICAL_INO_ARGS_IGNORE_OFFSET))
3294 			return -EINVAL;
3295 
3296 		ignore_offset = loi->flags & BTRFS_LOGICAL_INO_ARGS_IGNORE_OFFSET;
3297 		size = min_t(u32, loi->size, SZ_16M);
3298 	}
3299 
3300 	inodes = init_data_container(size);
3301 	if (IS_ERR(inodes))
3302 		return PTR_ERR(inodes);
3303 
3304 	ret = iterate_inodes_from_logical(loi->logical, fs_info, inodes, ignore_offset);
3305 	if (ret == -EINVAL)
3306 		return -ENOENT;
3307 	if (ret < 0)
3308 		return ret;
3309 
3310 	ret = copy_to_user((void __user *)(unsigned long)loi->inodes, inodes,
3311 			   size);
3312 	if (ret)
3313 		ret = -EFAULT;
3314 
3315 	return ret;
3316 }
3317 
3318 void btrfs_update_ioctl_balance_args(struct btrfs_fs_info *fs_info,
3319 			       struct btrfs_ioctl_balance_args *bargs)
3320 {
3321 	struct btrfs_balance_control *bctl = fs_info->balance_ctl;
3322 
3323 	bargs->flags = bctl->flags;
3324 
3325 	if (test_bit(BTRFS_FS_BALANCE_RUNNING, &fs_info->flags))
3326 		bargs->state |= BTRFS_BALANCE_STATE_RUNNING;
3327 	if (atomic_read(&fs_info->balance_pause_req))
3328 		bargs->state |= BTRFS_BALANCE_STATE_PAUSE_REQ;
3329 	if (atomic_read(&fs_info->balance_cancel_req))
3330 		bargs->state |= BTRFS_BALANCE_STATE_CANCEL_REQ;
3331 
3332 	memcpy(&bargs->data, &bctl->data, sizeof(bargs->data));
3333 	memcpy(&bargs->meta, &bctl->meta, sizeof(bargs->meta));
3334 	memcpy(&bargs->sys, &bctl->sys, sizeof(bargs->sys));
3335 
3336 	spin_lock(&fs_info->balance_lock);
3337 	memcpy(&bargs->stat, &bctl->stat, sizeof(bargs->stat));
3338 	spin_unlock(&fs_info->balance_lock);
3339 }
3340 
3341 /*
3342  * Try to acquire fs_info::balance_mutex as well as set BTRFS_EXLCOP_BALANCE as
3343  * required.
3344  *
3345  * @fs_info:       the filesystem
3346  * @excl_acquired: ptr to boolean value which is set to false in case balance
3347  *                 is being resumed
3348  *
3349  * Return 0 on success in which case both fs_info::balance is acquired as well
3350  * as exclusive ops are blocked. In case of failure return an error code.
3351  */
3352 static int btrfs_try_lock_balance(struct btrfs_fs_info *fs_info, bool *excl_acquired)
3353 {
3354 	int ret;
3355 
3356 	/*
3357 	 * Exclusive operation is locked. Three possibilities:
3358 	 *   (1) some other op is running
3359 	 *   (2) balance is running
3360 	 *   (3) balance is paused -- special case (think resume)
3361 	 */
3362 	while (1) {
3363 		if (btrfs_exclop_start(fs_info, BTRFS_EXCLOP_BALANCE)) {
3364 			*excl_acquired = true;
3365 			mutex_lock(&fs_info->balance_mutex);
3366 			return 0;
3367 		}
3368 
3369 		mutex_lock(&fs_info->balance_mutex);
3370 		if (fs_info->balance_ctl) {
3371 			/* This is either (2) or (3) */
3372 			if (test_bit(BTRFS_FS_BALANCE_RUNNING, &fs_info->flags)) {
3373 				/* This is (2) */
3374 				ret = -EINPROGRESS;
3375 				goto out_failure;
3376 
3377 			} else {
3378 				mutex_unlock(&fs_info->balance_mutex);
3379 				/*
3380 				 * Lock released to allow other waiters to
3381 				 * continue, we'll reexamine the status again.
3382 				 */
3383 				mutex_lock(&fs_info->balance_mutex);
3384 
3385 				if (fs_info->balance_ctl &&
3386 				    !test_bit(BTRFS_FS_BALANCE_RUNNING, &fs_info->flags)) {
3387 					/* This is (3) */
3388 					*excl_acquired = false;
3389 					return 0;
3390 				}
3391 			}
3392 		} else {
3393 			/* This is (1) */
3394 			ret = BTRFS_ERROR_DEV_EXCL_RUN_IN_PROGRESS;
3395 			goto out_failure;
3396 		}
3397 
3398 		mutex_unlock(&fs_info->balance_mutex);
3399 	}
3400 
3401 out_failure:
3402 	mutex_unlock(&fs_info->balance_mutex);
3403 	*excl_acquired = false;
3404 	return ret;
3405 }
3406 
3407 static long btrfs_ioctl_balance(struct file *file, void __user *arg)
3408 {
3409 	struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
3410 	struct btrfs_fs_info *fs_info = root->fs_info;
3411 	struct btrfs_ioctl_balance_args AUTO_KFREE(bargs);
3412 	struct btrfs_balance_control *bctl;
3413 	bool need_unlock = true;
3414 	int ret;
3415 
3416 	if (!capable(CAP_SYS_ADMIN))
3417 		return -EPERM;
3418 
3419 	ret = mnt_want_write_file(file);
3420 	if (ret)
3421 		return ret;
3422 
3423 	bargs = memdup_user(arg, sizeof(*bargs));
3424 	if (IS_ERR(bargs)) {
3425 		ret = PTR_ERR(bargs);
3426 		bargs = NULL;
3427 		goto out;
3428 	}
3429 
3430 	ret = btrfs_try_lock_balance(fs_info, &need_unlock);
3431 	if (ret)
3432 		goto out;
3433 
3434 	lockdep_assert_held(&fs_info->balance_mutex);
3435 
3436 	if (bargs->flags & BTRFS_BALANCE_RESUME) {
3437 		if (!fs_info->balance_ctl) {
3438 			ret = -ENOTCONN;
3439 			goto out_unlock;
3440 		}
3441 
3442 		bctl = fs_info->balance_ctl;
3443 		spin_lock(&fs_info->balance_lock);
3444 		bctl->flags |= BTRFS_BALANCE_RESUME;
3445 		spin_unlock(&fs_info->balance_lock);
3446 		btrfs_exclop_balance(fs_info, BTRFS_EXCLOP_BALANCE);
3447 
3448 		goto do_balance;
3449 	}
3450 
3451 	if (bargs->flags & ~(BTRFS_BALANCE_ARGS_MASK | BTRFS_BALANCE_TYPE_MASK)) {
3452 		ret = -EINVAL;
3453 		goto out_unlock;
3454 	}
3455 
3456 	if (fs_info->balance_ctl) {
3457 		ret = -EINPROGRESS;
3458 		goto out_unlock;
3459 	}
3460 
3461 	bctl = kzalloc_obj(*bctl);
3462 	if (!bctl) {
3463 		ret = -ENOMEM;
3464 		goto out_unlock;
3465 	}
3466 
3467 	memcpy(&bctl->data, &bargs->data, sizeof(bctl->data));
3468 	memcpy(&bctl->meta, &bargs->meta, sizeof(bctl->meta));
3469 	memcpy(&bctl->sys, &bargs->sys, sizeof(bctl->sys));
3470 
3471 	bctl->flags = bargs->flags;
3472 do_balance:
3473 	/*
3474 	 * Ownership of bctl and exclusive operation goes to btrfs_balance.
3475 	 * bctl is freed in reset_balance_state, or, if restriper was paused
3476 	 * all the way until unmount, in free_fs_info.  The flag should be
3477 	 * cleared after reset_balance_state.
3478 	 */
3479 	need_unlock = false;
3480 
3481 	ret = btrfs_balance(fs_info, bctl, bargs);
3482 	bctl = NULL;
3483 
3484 	if (ret == 0 || ret == -ECANCELED) {
3485 		if (copy_to_user(arg, bargs, sizeof(*bargs)))
3486 			ret = -EFAULT;
3487 	}
3488 
3489 	kfree(bctl);
3490 out_unlock:
3491 	mutex_unlock(&fs_info->balance_mutex);
3492 	if (need_unlock)
3493 		btrfs_exclop_finish(fs_info);
3494 out:
3495 	mnt_drop_write_file(file);
3496 	return ret;
3497 }
3498 
3499 static long btrfs_ioctl_balance_ctl(struct btrfs_fs_info *fs_info, int cmd)
3500 {
3501 	if (!capable(CAP_SYS_ADMIN))
3502 		return -EPERM;
3503 
3504 	switch (cmd) {
3505 	case BTRFS_BALANCE_CTL_PAUSE:
3506 		return btrfs_pause_balance(fs_info);
3507 	case BTRFS_BALANCE_CTL_CANCEL:
3508 		return btrfs_cancel_balance(fs_info);
3509 	}
3510 
3511 	return -EINVAL;
3512 }
3513 
3514 static long btrfs_ioctl_balance_progress(struct btrfs_fs_info *fs_info,
3515 					 void __user *arg)
3516 {
3517 	struct btrfs_ioctl_balance_args AUTO_KFREE(bargs);
3518 	int ret = 0;
3519 
3520 	if (!capable(CAP_SYS_ADMIN))
3521 		return -EPERM;
3522 
3523 	mutex_lock(&fs_info->balance_mutex);
3524 	if (!fs_info->balance_ctl) {
3525 		ret = -ENOTCONN;
3526 		goto out;
3527 	}
3528 
3529 	bargs = kzalloc(sizeof(*bargs), GFP_KERNEL);
3530 	if (!bargs) {
3531 		ret = -ENOMEM;
3532 		goto out;
3533 	}
3534 
3535 	btrfs_update_ioctl_balance_args(fs_info, bargs);
3536 
3537 	if (copy_to_user(arg, bargs, sizeof(*bargs)))
3538 		ret = -EFAULT;
3539 out:
3540 	mutex_unlock(&fs_info->balance_mutex);
3541 	return ret;
3542 }
3543 
3544 static long btrfs_ioctl_quota_ctl(struct file *file, void __user *arg)
3545 {
3546 	struct inode *inode = file_inode(file);
3547 	struct btrfs_fs_info *fs_info = inode_to_fs_info(inode);
3548 	struct btrfs_ioctl_quota_ctl_args AUTO_KFREE(sa);
3549 	int ret;
3550 
3551 	if (!capable(CAP_SYS_ADMIN))
3552 		return -EPERM;
3553 
3554 	ret = mnt_want_write_file(file);
3555 	if (ret)
3556 		return ret;
3557 
3558 	sa = memdup_user(arg, sizeof(*sa));
3559 	if (IS_ERR(sa)) {
3560 		ret = PTR_ERR(sa);
3561 		goto drop_write;
3562 	}
3563 
3564 	switch (sa->cmd) {
3565 	case BTRFS_QUOTA_CTL_ENABLE:
3566 	case BTRFS_QUOTA_CTL_ENABLE_SIMPLE_QUOTA:
3567 		down_write(&fs_info->subvol_sem);
3568 		ret = btrfs_quota_enable(fs_info, sa);
3569 		up_write(&fs_info->subvol_sem);
3570 		break;
3571 	case BTRFS_QUOTA_CTL_DISABLE:
3572 		/*
3573 		 * Lock the cleaner mutex to prevent races with concurrent
3574 		 * relocation, because relocation may be building backrefs for
3575 		 * blocks of the quota root while we are deleting the root. This
3576 		 * is like dropping fs roots of deleted snapshots/subvolumes, we
3577 		 * need the same protection.
3578 		 *
3579 		 * This also prevents races between concurrent tasks trying to
3580 		 * disable quotas, because we will unlock and relock
3581 		 * qgroup_ioctl_lock across BTRFS_FS_QUOTA_ENABLED changes.
3582 		 *
3583 		 * We take this here because we have the dependency of
3584 		 *
3585 		 * inode_lock -> subvol_sem
3586 		 *
3587 		 * because of rename.  With relocation we can prealloc extents,
3588 		 * so that makes the dependency chain
3589 		 *
3590 		 * cleaner_mutex -> inode_lock -> subvol_sem
3591 		 *
3592 		 * so we must take the cleaner_mutex here before we take the
3593 		 * subvol_sem.  The deadlock can't actually happen, but this
3594 		 * quiets lockdep.
3595 		 */
3596 		mutex_lock(&fs_info->cleaner_mutex);
3597 		down_write(&fs_info->subvol_sem);
3598 		ret = btrfs_quota_disable(fs_info);
3599 		up_write(&fs_info->subvol_sem);
3600 		mutex_unlock(&fs_info->cleaner_mutex);
3601 		break;
3602 	default:
3603 		ret = -EINVAL;
3604 		break;
3605 	}
3606 
3607 drop_write:
3608 	mnt_drop_write_file(file);
3609 	return ret;
3610 }
3611 
3612 static long btrfs_ioctl_qgroup_assign(struct file *file, void __user *arg)
3613 {
3614 	struct inode *inode = file_inode(file);
3615 	struct btrfs_fs_info *fs_info = inode_to_fs_info(inode);
3616 	struct btrfs_root *root = BTRFS_I(inode)->root;
3617 	struct btrfs_ioctl_qgroup_assign_args AUTO_KFREE(sa);
3618 	struct btrfs_qgroup_list AUTO_KFREE(prealloc);
3619 	struct btrfs_trans_handle *trans;
3620 	int ret;
3621 	int err;
3622 
3623 	if (!capable(CAP_SYS_ADMIN))
3624 		return -EPERM;
3625 
3626 	if (!btrfs_qgroup_enabled(fs_info))
3627 		return -ENOTCONN;
3628 
3629 	ret = mnt_want_write_file(file);
3630 	if (ret)
3631 		return ret;
3632 
3633 	sa = memdup_user(arg, sizeof(*sa));
3634 	if (IS_ERR(sa)) {
3635 		ret = PTR_ERR(sa);
3636 		goto drop_write;
3637 	}
3638 
3639 	if (sa->assign) {
3640 		prealloc = kzalloc_obj(*prealloc);
3641 		if (!prealloc) {
3642 			ret = -ENOMEM;
3643 			goto drop_write;
3644 		}
3645 	}
3646 
3647 	/* 2 BTRFS_QGROUP_RELATION_KEY items. */
3648 	trans = btrfs_start_transaction(root, 2);
3649 	if (IS_ERR(trans)) {
3650 		ret = PTR_ERR(trans);
3651 		goto drop_write;
3652 	}
3653 
3654 	/*
3655 	 * Prealloc ownership is moved to the relation handler, there it's used
3656 	 * or freed on error.
3657 	 */
3658 	if (sa->assign) {
3659 		ret = btrfs_add_qgroup_relation(trans, sa->src, sa->dst, prealloc);
3660 		prealloc = NULL;
3661 	} else {
3662 		ret = btrfs_del_qgroup_relation(trans, sa->src, sa->dst);
3663 	}
3664 
3665 	/* update qgroup status and info */
3666 	mutex_lock(&fs_info->qgroup_ioctl_lock);
3667 	err = btrfs_run_qgroups(trans);
3668 	mutex_unlock(&fs_info->qgroup_ioctl_lock);
3669 	if (err < 0)
3670 		btrfs_warn(fs_info,
3671 			   "qgroup status update failed after %s relation, marked as inconsistent",
3672 			   sa->assign ? "adding" : "deleting");
3673 	err = btrfs_end_transaction(trans);
3674 	if (err && !ret)
3675 		ret = err;
3676 
3677 drop_write:
3678 	mnt_drop_write_file(file);
3679 	return ret;
3680 }
3681 
3682 static long btrfs_ioctl_qgroup_create(struct file *file, void __user *arg)
3683 {
3684 	struct inode *inode = file_inode(file);
3685 	struct btrfs_root *root = BTRFS_I(inode)->root;
3686 	struct btrfs_ioctl_qgroup_create_args AUTO_KFREE(sa);
3687 	struct btrfs_trans_handle *trans;
3688 	int ret;
3689 	int err;
3690 
3691 	if (!capable(CAP_SYS_ADMIN))
3692 		return -EPERM;
3693 
3694 	if (!btrfs_qgroup_enabled(root->fs_info))
3695 		return -ENOTCONN;
3696 
3697 	ret = mnt_want_write_file(file);
3698 	if (ret)
3699 		return ret;
3700 
3701 	sa = memdup_user(arg, sizeof(*sa));
3702 	if (IS_ERR(sa)) {
3703 		ret = PTR_ERR(sa);
3704 		goto drop_write;
3705 	}
3706 
3707 	if (!sa->qgroupid) {
3708 		ret = -EINVAL;
3709 		goto drop_write;
3710 	}
3711 
3712 	if (sa->create && btrfs_is_fstree(sa->qgroupid)) {
3713 		ret = -EINVAL;
3714 		goto drop_write;
3715 	}
3716 
3717 	/*
3718 	 * 1 BTRFS_QGROUP_INFO_KEY item.
3719 	 * 1 BTRFS_QGROUP_LIMIT_KEY item.
3720 	 */
3721 	trans = btrfs_start_transaction(root, 2);
3722 	if (IS_ERR(trans)) {
3723 		ret = PTR_ERR(trans);
3724 		goto drop_write;
3725 	}
3726 
3727 	if (sa->create) {
3728 		ret = btrfs_create_qgroup(trans, sa->qgroupid);
3729 	} else {
3730 		ret = btrfs_remove_qgroup(trans, sa->qgroupid);
3731 	}
3732 
3733 	err = btrfs_end_transaction(trans);
3734 	if (err && !ret)
3735 		ret = err;
3736 
3737 drop_write:
3738 	mnt_drop_write_file(file);
3739 	return ret;
3740 }
3741 
3742 static long btrfs_ioctl_qgroup_limit(struct file *file, void __user *arg)
3743 {
3744 	struct inode *inode = file_inode(file);
3745 	struct btrfs_root *root = BTRFS_I(inode)->root;
3746 	struct btrfs_ioctl_qgroup_limit_args AUTO_KFREE(sa);
3747 	struct btrfs_trans_handle *trans;
3748 	int ret;
3749 	int err;
3750 	u64 qgroupid;
3751 
3752 	if (!capable(CAP_SYS_ADMIN))
3753 		return -EPERM;
3754 
3755 	if (!btrfs_qgroup_enabled(root->fs_info))
3756 		return -ENOTCONN;
3757 
3758 	ret = mnt_want_write_file(file);
3759 	if (ret)
3760 		return ret;
3761 
3762 	sa = memdup_user(arg, sizeof(*sa));
3763 	if (IS_ERR(sa)) {
3764 		ret = PTR_ERR(sa);
3765 		goto drop_write;
3766 	}
3767 
3768 	/* 1 BTRFS_QGROUP_LIMIT_KEY item. */
3769 	trans = btrfs_start_transaction(root, 1);
3770 	if (IS_ERR(trans)) {
3771 		ret = PTR_ERR(trans);
3772 		goto drop_write;
3773 	}
3774 
3775 	qgroupid = sa->qgroupid;
3776 	if (!qgroupid) {
3777 		/* take the current subvol as qgroup */
3778 		qgroupid = btrfs_root_id(root);
3779 	}
3780 
3781 	ret = btrfs_limit_qgroup(trans, qgroupid, &sa->lim);
3782 
3783 	err = btrfs_end_transaction(trans);
3784 	if (err && !ret)
3785 		ret = err;
3786 
3787 drop_write:
3788 	mnt_drop_write_file(file);
3789 	return ret;
3790 }
3791 
3792 static long btrfs_ioctl_quota_rescan(struct file *file, void __user *arg)
3793 {
3794 	struct inode *inode = file_inode(file);
3795 	struct btrfs_fs_info *fs_info = inode_to_fs_info(inode);
3796 	struct btrfs_ioctl_quota_rescan_args AUTO_KFREE(qsa);
3797 	int ret;
3798 
3799 	if (!capable(CAP_SYS_ADMIN))
3800 		return -EPERM;
3801 
3802 	if (!btrfs_qgroup_enabled(fs_info))
3803 		return -ENOTCONN;
3804 
3805 	ret = mnt_want_write_file(file);
3806 	if (ret)
3807 		return ret;
3808 
3809 	qsa = memdup_user(arg, sizeof(*qsa));
3810 	if (IS_ERR(qsa)) {
3811 		ret = PTR_ERR(qsa);
3812 		goto drop_write;
3813 	}
3814 
3815 	if (qsa->flags) {
3816 		ret = -EINVAL;
3817 		goto drop_write;
3818 	}
3819 
3820 	ret = btrfs_qgroup_rescan(fs_info);
3821 
3822 drop_write:
3823 	mnt_drop_write_file(file);
3824 	return ret;
3825 }
3826 
3827 static long btrfs_ioctl_quota_rescan_status(struct btrfs_fs_info *fs_info,
3828 						void __user *arg)
3829 {
3830 	struct btrfs_ioctl_quota_rescan_args qsa = {0};
3831 
3832 	if (!capable(CAP_SYS_ADMIN))
3833 		return -EPERM;
3834 
3835 	if (fs_info->qgroup_flags & BTRFS_QGROUP_STATUS_FLAG_RESCAN) {
3836 		qsa.flags = 1;
3837 		qsa.progress = fs_info->qgroup_rescan_progress.objectid;
3838 	}
3839 
3840 	if (copy_to_user(arg, &qsa, sizeof(qsa)))
3841 		return -EFAULT;
3842 
3843 	return 0;
3844 }
3845 
3846 static long btrfs_ioctl_quota_rescan_wait(struct btrfs_fs_info *fs_info)
3847 {
3848 	if (!capable(CAP_SYS_ADMIN))
3849 		return -EPERM;
3850 
3851 	return btrfs_qgroup_wait_for_completion(fs_info, true);
3852 }
3853 
3854 static long _btrfs_ioctl_set_received_subvol(struct file *file,
3855 					    struct mnt_idmap *idmap,
3856 					    struct btrfs_ioctl_received_subvol_args *sa)
3857 {
3858 	struct inode *inode = file_inode(file);
3859 	struct btrfs_fs_info *fs_info = inode_to_fs_info(inode);
3860 	struct btrfs_root *root = BTRFS_I(inode)->root;
3861 	struct btrfs_root_item *root_item = &root->root_item;
3862 	struct btrfs_trans_handle *trans;
3863 	struct timespec64 ct = current_time(inode);
3864 	int ret = 0;
3865 	int received_uuid_changed;
3866 
3867 	if (!inode_owner_or_capable(idmap, inode))
3868 		return -EPERM;
3869 
3870 	ret = mnt_want_write_file(file);
3871 	if (ret < 0)
3872 		return ret;
3873 
3874 	down_write(&fs_info->subvol_sem);
3875 
3876 	if (btrfs_ino(BTRFS_I(inode)) != BTRFS_FIRST_FREE_OBJECTID) {
3877 		ret = -EINVAL;
3878 		goto out;
3879 	}
3880 
3881 	if (btrfs_root_readonly(root)) {
3882 		ret = -EROFS;
3883 		goto out;
3884 	}
3885 
3886 	received_uuid_changed = memcmp(root_item->received_uuid, sa->uuid,
3887 				       BTRFS_UUID_SIZE);
3888 
3889 	/*
3890 	 * Before we attempt to add the new received uuid, check if we have room
3891 	 * for it in case there's already an item. If the size of the existing
3892 	 * item plus this root's ID (u64) exceeds the maximum item size, we can
3893 	 * return here without the need to abort a transaction. If we don't do
3894 	 * this check, the btrfs_uuid_tree_add() call below would fail with
3895 	 * -EOVERFLOW and result in a transaction abort. Malicious users could
3896 	 * exploit this to turn the fs into RO mode.
3897 	 */
3898 	if (received_uuid_changed && !btrfs_is_empty_uuid(sa->uuid)) {
3899 		ret = btrfs_uuid_tree_check_overflow(fs_info, sa->uuid,
3900 						     BTRFS_UUID_KEY_RECEIVED_SUBVOL);
3901 		if (ret < 0)
3902 			goto out;
3903 	}
3904 
3905 	/*
3906 	 * 1 - root item
3907 	 * 2 - uuid items (received uuid + subvol uuid)
3908 	 */
3909 	trans = btrfs_start_transaction(root, 3);
3910 	if (IS_ERR(trans)) {
3911 		ret = PTR_ERR(trans);
3912 		trans = NULL;
3913 		goto out;
3914 	}
3915 
3916 	sa->rtransid = trans->transid;
3917 	sa->rtime.sec = ct.tv_sec;
3918 	sa->rtime.nsec = ct.tv_nsec;
3919 
3920 	if (received_uuid_changed &&
3921 	    !btrfs_is_empty_uuid(root_item->received_uuid)) {
3922 		ret = btrfs_uuid_tree_remove(trans, root_item->received_uuid,
3923 					  BTRFS_UUID_KEY_RECEIVED_SUBVOL,
3924 					  btrfs_root_id(root));
3925 		if (unlikely(ret && ret != -ENOENT)) {
3926 		        btrfs_end_transaction(trans);
3927 		        goto out;
3928 		}
3929 	}
3930 	memcpy(root_item->received_uuid, sa->uuid, BTRFS_UUID_SIZE);
3931 	btrfs_set_root_stransid(root_item, sa->stransid);
3932 	btrfs_set_root_rtransid(root_item, sa->rtransid);
3933 	btrfs_set_stack_timespec_sec(&root_item->stime, sa->stime.sec);
3934 	btrfs_set_stack_timespec_nsec(&root_item->stime, sa->stime.nsec);
3935 	btrfs_set_stack_timespec_sec(&root_item->rtime, sa->rtime.sec);
3936 	btrfs_set_stack_timespec_nsec(&root_item->rtime, sa->rtime.nsec);
3937 
3938 	ret = btrfs_update_root(trans, fs_info->tree_root,
3939 				&root->root_key, &root->root_item);
3940 	if (unlikely(ret < 0)) {
3941 		btrfs_abort_transaction(trans, ret);
3942 		btrfs_end_transaction(trans);
3943 		goto out;
3944 	}
3945 	if (received_uuid_changed && !btrfs_is_empty_uuid(sa->uuid)) {
3946 		ret = btrfs_uuid_tree_add(trans, sa->uuid,
3947 					  BTRFS_UUID_KEY_RECEIVED_SUBVOL,
3948 					  btrfs_root_id(root));
3949 		if (unlikely(ret < 0)) {
3950 			btrfs_abort_transaction(trans, ret);
3951 			btrfs_end_transaction(trans);
3952 			goto out;
3953 		}
3954 	}
3955 	ret = btrfs_commit_transaction(trans);
3956 out:
3957 	up_write(&fs_info->subvol_sem);
3958 	mnt_drop_write_file(file);
3959 	return ret;
3960 }
3961 
3962 #ifdef CONFIG_64BIT
3963 static long btrfs_ioctl_set_received_subvol_32(struct file *file,
3964 						void __user *arg)
3965 {
3966 	struct btrfs_ioctl_received_subvol_args_32 AUTO_KFREE(args32);
3967 	struct btrfs_ioctl_received_subvol_args AUTO_KFREE(args64);
3968 	int ret = 0;
3969 
3970 	args32 = memdup_user(arg, sizeof(*args32));
3971 	if (IS_ERR(args32))
3972 		return PTR_ERR(args32);
3973 
3974 	args64 = kmalloc_obj(*args64);
3975 	if (!args64)
3976 		return -ENOMEM;
3977 
3978 	memcpy(args64->uuid, args32->uuid, BTRFS_UUID_SIZE);
3979 	args64->stransid = args32->stransid;
3980 	args64->rtransid = args32->rtransid;
3981 	args64->stime.sec = args32->stime.sec;
3982 	args64->stime.nsec = args32->stime.nsec;
3983 	args64->rtime.sec = args32->rtime.sec;
3984 	args64->rtime.nsec = args32->rtime.nsec;
3985 	args64->flags = args32->flags;
3986 
3987 	ret = _btrfs_ioctl_set_received_subvol(file, file_mnt_idmap(file), args64);
3988 	if (ret)
3989 		return ret;
3990 
3991 	memcpy(args32->uuid, args64->uuid, BTRFS_UUID_SIZE);
3992 	args32->stransid = args64->stransid;
3993 	args32->rtransid = args64->rtransid;
3994 	args32->stime.sec = args64->stime.sec;
3995 	args32->stime.nsec = args64->stime.nsec;
3996 	args32->rtime.sec = args64->rtime.sec;
3997 	args32->rtime.nsec = args64->rtime.nsec;
3998 	args32->flags = args64->flags;
3999 
4000 	ret = copy_to_user(arg, args32, sizeof(*args32));
4001 	if (ret)
4002 		return -EFAULT;
4003 
4004 	return 0;
4005 }
4006 #endif
4007 
4008 static long btrfs_ioctl_set_received_subvol(struct file *file,
4009 					    void __user *arg)
4010 {
4011 	struct btrfs_ioctl_received_subvol_args AUTO_KFREE(sa);
4012 	int ret = 0;
4013 
4014 	sa = memdup_user(arg, sizeof(*sa));
4015 	if (IS_ERR(sa))
4016 		return PTR_ERR(sa);
4017 
4018 	ret = _btrfs_ioctl_set_received_subvol(file, file_mnt_idmap(file), sa);
4019 	if (ret)
4020 		return ret;
4021 
4022 	ret = copy_to_user(arg, sa, sizeof(*sa));
4023 	if (ret)
4024 		return -EFAULT;
4025 
4026 	return 0;
4027 }
4028 
4029 static int btrfs_ioctl_get_fslabel(struct btrfs_fs_info *fs_info,
4030 					void __user *arg)
4031 {
4032 	size_t len;
4033 	int ret;
4034 	char label[BTRFS_LABEL_SIZE];
4035 
4036 	spin_lock(&fs_info->super_lock);
4037 	memcpy(label, fs_info->super_copy->label, BTRFS_LABEL_SIZE);
4038 	spin_unlock(&fs_info->super_lock);
4039 
4040 	len = strnlen(label, BTRFS_LABEL_SIZE);
4041 
4042 	if (len == BTRFS_LABEL_SIZE) {
4043 		btrfs_warn(fs_info,
4044 			   "label is too long, return the first %zu bytes",
4045 			   --len);
4046 	}
4047 
4048 	ret = copy_to_user(arg, label, len);
4049 
4050 	return ret ? -EFAULT : 0;
4051 }
4052 
4053 static int btrfs_ioctl_set_fslabel(struct file *file, void __user *arg)
4054 {
4055 	struct inode *inode = file_inode(file);
4056 	struct btrfs_fs_info *fs_info = inode_to_fs_info(inode);
4057 	struct btrfs_root *root = BTRFS_I(inode)->root;
4058 	struct btrfs_super_block *super_block = fs_info->super_copy;
4059 	struct btrfs_trans_handle *trans;
4060 	char label[BTRFS_LABEL_SIZE];
4061 	int ret;
4062 
4063 	if (!capable(CAP_SYS_ADMIN))
4064 		return -EPERM;
4065 
4066 	if (copy_from_user(label, arg, sizeof(label)))
4067 		return -EFAULT;
4068 
4069 	if (strnlen(label, BTRFS_LABEL_SIZE) == BTRFS_LABEL_SIZE) {
4070 		btrfs_err(fs_info,
4071 			  "unable to set label with more than %d bytes",
4072 			  BTRFS_LABEL_SIZE - 1);
4073 		return -EINVAL;
4074 	}
4075 
4076 	ret = mnt_want_write_file(file);
4077 	if (ret)
4078 		return ret;
4079 
4080 	trans = btrfs_start_transaction(root, 0);
4081 	if (IS_ERR(trans)) {
4082 		ret = PTR_ERR(trans);
4083 		goto out_unlock;
4084 	}
4085 
4086 	spin_lock(&fs_info->super_lock);
4087 	strscpy(super_block->label, label);
4088 	spin_unlock(&fs_info->super_lock);
4089 	ret = btrfs_commit_transaction(trans);
4090 
4091 out_unlock:
4092 	mnt_drop_write_file(file);
4093 	return ret;
4094 }
4095 
4096 #define INIT_FEATURE_FLAGS(suffix) \
4097 	{ .compat_flags = BTRFS_FEATURE_COMPAT_##suffix, \
4098 	  .compat_ro_flags = BTRFS_FEATURE_COMPAT_RO_##suffix, \
4099 	  .incompat_flags = BTRFS_FEATURE_INCOMPAT_##suffix }
4100 
4101 int btrfs_ioctl_get_supported_features(void __user *arg)
4102 {
4103 	static const struct btrfs_ioctl_feature_flags features[3] = {
4104 		INIT_FEATURE_FLAGS(SUPP),
4105 		INIT_FEATURE_FLAGS(SAFE_SET),
4106 		INIT_FEATURE_FLAGS(SAFE_CLEAR)
4107 	};
4108 
4109 	if (copy_to_user(arg, &features, sizeof(features)))
4110 		return -EFAULT;
4111 
4112 	return 0;
4113 }
4114 
4115 static int btrfs_ioctl_get_features(struct btrfs_fs_info *fs_info,
4116 					void __user *arg)
4117 {
4118 	struct btrfs_super_block *super_block = fs_info->super_copy;
4119 	struct btrfs_ioctl_feature_flags features;
4120 
4121 	features.compat_flags = btrfs_super_compat_flags(super_block);
4122 	features.compat_ro_flags = btrfs_super_compat_ro_flags(super_block);
4123 	features.incompat_flags = btrfs_super_incompat_flags(super_block);
4124 
4125 	if (copy_to_user(arg, &features, sizeof(features)))
4126 		return -EFAULT;
4127 
4128 	return 0;
4129 }
4130 
4131 static int check_feature_bits(const struct btrfs_fs_info *fs_info,
4132 			      enum btrfs_feature_set set,
4133 			      u64 change_mask, u64 flags, u64 supported_flags,
4134 			      u64 safe_set, u64 safe_clear)
4135 {
4136 	const char *type = btrfs_feature_set_name(set);
4137 	const char AUTO_KFREE(names);
4138 	u64 disallowed, unsupported;
4139 	u64 set_mask = flags & change_mask;
4140 	u64 clear_mask = ~flags & change_mask;
4141 
4142 	unsupported = set_mask & ~supported_flags;
4143 	if (unsupported) {
4144 		names = btrfs_printable_features(set, unsupported);
4145 		if (names)
4146 			btrfs_warn(fs_info,
4147 				   "this kernel does not support the %s feature bit%s",
4148 				   names, strchr(names, ',') ? "s" : "");
4149 		else
4150 			btrfs_warn(fs_info,
4151 				   "this kernel does not support %s bits 0x%llx",
4152 				   type, unsupported);
4153 		return -EOPNOTSUPP;
4154 	}
4155 
4156 	disallowed = set_mask & ~safe_set;
4157 	if (disallowed) {
4158 		names = btrfs_printable_features(set, disallowed);
4159 		if (names)
4160 			btrfs_warn(fs_info,
4161 				   "can't set the %s feature bit%s while mounted",
4162 				   names, strchr(names, ',') ? "s" : "");
4163 		else
4164 			btrfs_warn(fs_info,
4165 				   "can't set %s bits 0x%llx while mounted",
4166 				   type, disallowed);
4167 		return -EPERM;
4168 	}
4169 
4170 	disallowed = clear_mask & ~safe_clear;
4171 	if (disallowed) {
4172 		names = btrfs_printable_features(set, disallowed);
4173 		if (names)
4174 			btrfs_warn(fs_info,
4175 				   "can't clear the %s feature bit%s while mounted",
4176 				   names, strchr(names, ',') ? "s" : "");
4177 		else
4178 			btrfs_warn(fs_info,
4179 				   "can't clear %s bits 0x%llx while mounted",
4180 				   type, disallowed);
4181 		return -EPERM;
4182 	}
4183 
4184 	return 0;
4185 }
4186 
4187 #define check_feature(fs_info, change_mask, flags, mask_base)	\
4188 check_feature_bits(fs_info, FEAT_##mask_base, change_mask, flags,	\
4189 		   BTRFS_FEATURE_ ## mask_base ## _SUPP,	\
4190 		   BTRFS_FEATURE_ ## mask_base ## _SAFE_SET,	\
4191 		   BTRFS_FEATURE_ ## mask_base ## _SAFE_CLEAR)
4192 
4193 static int btrfs_ioctl_set_features(struct file *file, void __user *arg)
4194 {
4195 	struct inode *inode = file_inode(file);
4196 	struct btrfs_fs_info *fs_info = inode_to_fs_info(inode);
4197 	struct btrfs_root *root = BTRFS_I(inode)->root;
4198 	struct btrfs_super_block *super_block = fs_info->super_copy;
4199 	struct btrfs_ioctl_feature_flags flags[2];
4200 	struct btrfs_trans_handle *trans;
4201 	u64 newflags;
4202 	int ret;
4203 
4204 	if (!capable(CAP_SYS_ADMIN))
4205 		return -EPERM;
4206 
4207 	if (copy_from_user(flags, arg, sizeof(flags)))
4208 		return -EFAULT;
4209 
4210 	/* Nothing to do */
4211 	if (!flags[0].compat_flags && !flags[0].compat_ro_flags &&
4212 	    !flags[0].incompat_flags)
4213 		return 0;
4214 
4215 	ret = check_feature(fs_info, flags[0].compat_flags,
4216 			    flags[1].compat_flags, COMPAT);
4217 	if (ret)
4218 		return ret;
4219 
4220 	ret = check_feature(fs_info, flags[0].compat_ro_flags,
4221 			    flags[1].compat_ro_flags, COMPAT_RO);
4222 	if (ret)
4223 		return ret;
4224 
4225 	ret = check_feature(fs_info, flags[0].incompat_flags,
4226 			    flags[1].incompat_flags, INCOMPAT);
4227 	if (ret)
4228 		return ret;
4229 
4230 	ret = mnt_want_write_file(file);
4231 	if (ret)
4232 		return ret;
4233 
4234 	trans = btrfs_start_transaction(root, 0);
4235 	if (IS_ERR(trans)) {
4236 		ret = PTR_ERR(trans);
4237 		goto out_drop_write;
4238 	}
4239 
4240 	spin_lock(&fs_info->super_lock);
4241 	newflags = btrfs_super_compat_flags(super_block);
4242 	newflags |= flags[0].compat_flags & flags[1].compat_flags;
4243 	newflags &= ~(flags[0].compat_flags & ~flags[1].compat_flags);
4244 	btrfs_set_super_compat_flags(super_block, newflags);
4245 
4246 	newflags = btrfs_super_compat_ro_flags(super_block);
4247 	newflags |= flags[0].compat_ro_flags & flags[1].compat_ro_flags;
4248 	newflags &= ~(flags[0].compat_ro_flags & ~flags[1].compat_ro_flags);
4249 	btrfs_set_super_compat_ro_flags(super_block, newflags);
4250 
4251 	newflags = btrfs_super_incompat_flags(super_block);
4252 	newflags |= flags[0].incompat_flags & flags[1].incompat_flags;
4253 	newflags &= ~(flags[0].incompat_flags & ~flags[1].incompat_flags);
4254 	btrfs_set_super_incompat_flags(super_block, newflags);
4255 	spin_unlock(&fs_info->super_lock);
4256 
4257 	ret = btrfs_commit_transaction(trans);
4258 out_drop_write:
4259 	mnt_drop_write_file(file);
4260 
4261 	return ret;
4262 }
4263 
4264 static int _btrfs_ioctl_send(struct btrfs_root *root, void __user *argp, bool compat)
4265 {
4266 	struct btrfs_ioctl_send_args AUTO_KFREE(arg);
4267 
4268 	if (compat) {
4269 #if defined(CONFIG_64BIT) && defined(CONFIG_COMPAT)
4270 		int ret;
4271 		struct btrfs_ioctl_send_args_32 args32 = { 0 };
4272 
4273 		ret = copy_from_user(&args32, argp, sizeof(args32));
4274 		if (ret)
4275 			return -EFAULT;
4276 		arg = kzalloc_obj(*arg);
4277 		if (!arg)
4278 			return -ENOMEM;
4279 		arg->send_fd = args32.send_fd;
4280 		arg->clone_sources_count = args32.clone_sources_count;
4281 		arg->clone_sources = compat_ptr(args32.clone_sources);
4282 		arg->parent_root = args32.parent_root;
4283 		arg->flags = args32.flags;
4284 		arg->version = args32.version;
4285 		memcpy(arg->reserved, args32.reserved,
4286 		       sizeof(args32.reserved));
4287 #else
4288 		return -ENOTTY;
4289 #endif
4290 	} else {
4291 		arg = memdup_user(argp, sizeof(*arg));
4292 		if (IS_ERR(arg))
4293 			return PTR_ERR(arg);
4294 	}
4295 	return btrfs_ioctl_send(root, arg);
4296 }
4297 
4298 static int btrfs_ioctl_encoded_read(struct file *file, void __user *argp,
4299 				    bool compat)
4300 {
4301 	struct btrfs_ioctl_encoded_io_args args = { 0 };
4302 	size_t copy_end_kernel = offsetofend(struct btrfs_ioctl_encoded_io_args,
4303 					     flags);
4304 	size_t copy_end;
4305 	struct btrfs_inode *inode = BTRFS_I(file_inode(file));
4306 	struct btrfs_fs_info *fs_info = inode->root->fs_info;
4307 	struct extent_io_tree *io_tree = &inode->io_tree;
4308 	struct iovec iovstack[UIO_FASTIOV];
4309 	struct iovec *iov = iovstack;
4310 	struct iov_iter iter;
4311 	loff_t pos;
4312 	struct kiocb kiocb;
4313 	ssize_t ret;
4314 	u64 disk_bytenr, disk_io_size;
4315 	struct extent_state *cached_state = NULL;
4316 
4317 	if (!capable(CAP_SYS_ADMIN)) {
4318 		ret = -EPERM;
4319 		goto out_acct;
4320 	}
4321 
4322 	if (compat) {
4323 #if defined(CONFIG_64BIT) && defined(CONFIG_COMPAT)
4324 		struct btrfs_ioctl_encoded_io_args_32 args32;
4325 
4326 		copy_end = offsetofend(struct btrfs_ioctl_encoded_io_args_32,
4327 				       flags);
4328 		if (copy_from_user(&args32, argp, copy_end)) {
4329 			ret = -EFAULT;
4330 			goto out_acct;
4331 		}
4332 		args.iov = compat_ptr(args32.iov);
4333 		args.iovcnt = args32.iovcnt;
4334 		args.offset = args32.offset;
4335 		args.flags = args32.flags;
4336 #else
4337 		return -ENOTTY;
4338 #endif
4339 	} else {
4340 		copy_end = copy_end_kernel;
4341 		if (copy_from_user(&args, argp, copy_end)) {
4342 			ret = -EFAULT;
4343 			goto out_acct;
4344 		}
4345 	}
4346 	if (args.flags != 0) {
4347 		ret = -EINVAL;
4348 		goto out_acct;
4349 	}
4350 
4351 	ret = import_iovec(ITER_DEST, args.iov, args.iovcnt, ARRAY_SIZE(iovstack),
4352 			   &iov, &iter);
4353 	if (ret < 0)
4354 		goto out_acct;
4355 
4356 	if (iov_iter_count(&iter) == 0) {
4357 		ret = 0;
4358 		goto out_iov;
4359 	}
4360 	pos = args.offset;
4361 	ret = rw_verify_area(READ, file, &pos, args.len);
4362 	if (ret < 0)
4363 		goto out_iov;
4364 
4365 	init_sync_kiocb(&kiocb, file);
4366 	kiocb.ki_pos = pos;
4367 
4368 	ret = btrfs_encoded_read(&kiocb, &iter, &args, &cached_state,
4369 				 &disk_bytenr, &disk_io_size);
4370 
4371 	if (ret == -EIOCBQUEUED) {
4372 		bool unlocked = false;
4373 		u64 start, lockend, count;
4374 
4375 		start = ALIGN_DOWN(kiocb.ki_pos, fs_info->sectorsize);
4376 		lockend = start + BTRFS_MAX_UNCOMPRESSED - 1;
4377 
4378 		if (args.compression)
4379 			count = disk_io_size;
4380 		else
4381 			count = args.len;
4382 
4383 		ret = btrfs_encoded_read_regular(&kiocb, &iter, start, lockend,
4384 						 &cached_state, disk_bytenr,
4385 						 disk_io_size, count,
4386 						 args.compression, &unlocked);
4387 
4388 		if (!unlocked) {
4389 			btrfs_unlock_extent(io_tree, start, lockend, &cached_state);
4390 			btrfs_inode_unlock(inode, BTRFS_ILOCK_SHARED);
4391 		}
4392 	}
4393 
4394 	if (ret >= 0) {
4395 		fsnotify_access(file);
4396 		if (copy_to_user(argp + copy_end,
4397 				 (char *)&args + copy_end_kernel,
4398 				 sizeof(args) - copy_end_kernel))
4399 			ret = -EFAULT;
4400 	}
4401 
4402 out_iov:
4403 	kfree(iov);
4404 out_acct:
4405 	if (ret > 0)
4406 		add_rchar(current, ret);
4407 	inc_syscr(current);
4408 	return ret;
4409 }
4410 
4411 static int btrfs_ioctl_encoded_write(struct file *file, void __user *argp, bool compat)
4412 {
4413 	struct btrfs_ioctl_encoded_io_args args;
4414 	struct iovec iovstack[UIO_FASTIOV];
4415 	struct iovec *iov = iovstack;
4416 	struct iov_iter iter;
4417 	loff_t pos;
4418 	struct kiocb kiocb;
4419 	ssize_t ret;
4420 
4421 	if (!capable(CAP_SYS_ADMIN)) {
4422 		ret = -EPERM;
4423 		goto out_acct;
4424 	}
4425 
4426 	if (!(file->f_mode & FMODE_WRITE)) {
4427 		ret = -EBADF;
4428 		goto out_acct;
4429 	}
4430 
4431 	if (compat) {
4432 #if defined(CONFIG_64BIT) && defined(CONFIG_COMPAT)
4433 		struct btrfs_ioctl_encoded_io_args_32 args32;
4434 
4435 		if (copy_from_user(&args32, argp, sizeof(args32))) {
4436 			ret = -EFAULT;
4437 			goto out_acct;
4438 		}
4439 		args.iov = compat_ptr(args32.iov);
4440 		args.iovcnt = args32.iovcnt;
4441 		args.offset = args32.offset;
4442 		args.flags = args32.flags;
4443 		args.len = args32.len;
4444 		args.unencoded_len = args32.unencoded_len;
4445 		args.unencoded_offset = args32.unencoded_offset;
4446 		args.compression = args32.compression;
4447 		args.encryption = args32.encryption;
4448 		memcpy(args.reserved, args32.reserved, sizeof(args.reserved));
4449 #else
4450 		return -ENOTTY;
4451 #endif
4452 	} else {
4453 		if (copy_from_user(&args, argp, sizeof(args))) {
4454 			ret = -EFAULT;
4455 			goto out_acct;
4456 		}
4457 	}
4458 
4459 	ret = -EINVAL;
4460 	if (args.flags != 0)
4461 		goto out_acct;
4462 	if (memchr_inv(args.reserved, 0, sizeof(args.reserved)))
4463 		goto out_acct;
4464 	if (args.compression == BTRFS_ENCODED_IO_COMPRESSION_NONE &&
4465 	    args.encryption == BTRFS_ENCODED_IO_ENCRYPTION_NONE)
4466 		goto out_acct;
4467 	if (args.compression >= BTRFS_ENCODED_IO_COMPRESSION_TYPES ||
4468 	    args.encryption >= BTRFS_ENCODED_IO_ENCRYPTION_TYPES)
4469 		goto out_acct;
4470 	if (args.unencoded_offset > args.unencoded_len)
4471 		goto out_acct;
4472 	if (args.len > args.unencoded_len - args.unencoded_offset)
4473 		goto out_acct;
4474 
4475 	ret = import_iovec(ITER_SOURCE, args.iov, args.iovcnt, ARRAY_SIZE(iovstack),
4476 			   &iov, &iter);
4477 	if (ret < 0)
4478 		goto out_acct;
4479 
4480 	if (iov_iter_count(&iter) == 0) {
4481 		ret = 0;
4482 		goto out_iov;
4483 	}
4484 	pos = args.offset;
4485 	ret = rw_verify_area(WRITE, file, &pos, args.len);
4486 	if (ret < 0)
4487 		goto out_iov;
4488 
4489 	init_sync_kiocb(&kiocb, file);
4490 	ret = kiocb_set_rw_flags(&kiocb, 0, WRITE);
4491 	if (ret)
4492 		goto out_iov;
4493 	kiocb.ki_pos = pos;
4494 
4495 	file_start_write(file);
4496 
4497 	ret = btrfs_do_write_iter(&kiocb, &iter, &args);
4498 	if (ret > 0)
4499 		fsnotify_modify(file);
4500 
4501 	file_end_write(file);
4502 out_iov:
4503 	kfree(iov);
4504 out_acct:
4505 	if (ret > 0)
4506 		add_wchar(current, ret);
4507 	inc_syscw(current);
4508 	return ret;
4509 }
4510 
4511 struct btrfs_uring_encoded_data {
4512 	struct btrfs_ioctl_encoded_io_args args;
4513 	struct iovec iovstack[UIO_FASTIOV];
4514 	struct iovec *iov;
4515 	struct iov_iter iter;
4516 };
4517 
4518 /*
4519  * Context that's attached to an encoded read io_uring command, in cmd->pdu. It
4520  * contains the fields in btrfs_uring_read_extent that are necessary to finish
4521  * off and cleanup the I/O in btrfs_uring_read_finished.
4522  */
4523 struct btrfs_uring_priv {
4524 	struct io_uring_cmd *cmd;
4525 	struct page **pages;
4526 	unsigned long nr_pages;
4527 	struct kiocb iocb;
4528 	struct iovec *iov;
4529 	struct iov_iter iter;
4530 	struct extent_state *cached_state;
4531 	u64 count;
4532 	u64 start;
4533 	u64 lockend;
4534 	int err;
4535 	bool compressed;
4536 };
4537 
4538 struct io_btrfs_cmd {
4539 	struct btrfs_uring_encoded_data *data;
4540 	struct btrfs_uring_priv *priv;
4541 };
4542 
4543 static void btrfs_uring_read_finished(struct io_tw_req tw_req, io_tw_token_t tw)
4544 {
4545 	struct io_uring_cmd *cmd = io_uring_cmd_from_tw(tw_req);
4546 	struct io_btrfs_cmd *bc = io_uring_cmd_to_pdu(cmd, struct io_btrfs_cmd);
4547 	struct btrfs_uring_priv *priv = bc->priv;
4548 	struct btrfs_inode *inode = BTRFS_I(file_inode(priv->iocb.ki_filp));
4549 	struct extent_io_tree *io_tree = &inode->io_tree;
4550 	pgoff_t index;
4551 	u64 cur;
4552 	size_t page_offset;
4553 	ssize_t ret;
4554 
4555 	/* The inode lock has already been acquired in btrfs_uring_read_extent.  */
4556 	btrfs_lockdep_inode_acquire(inode, i_rwsem);
4557 
4558 	if (priv->err) {
4559 		ret = priv->err;
4560 		goto out;
4561 	}
4562 
4563 	if (priv->compressed) {
4564 		index = 0;
4565 		page_offset = 0;
4566 	} else {
4567 		index = (priv->iocb.ki_pos - priv->start) >> PAGE_SHIFT;
4568 		page_offset = offset_in_page(priv->iocb.ki_pos - priv->start);
4569 	}
4570 	cur = 0;
4571 	while (cur < priv->count) {
4572 		size_t bytes = min_t(size_t, priv->count - cur, PAGE_SIZE - page_offset);
4573 
4574 		if (copy_page_to_iter(priv->pages[index], page_offset, bytes,
4575 				      &priv->iter) != bytes) {
4576 			ret = -EFAULT;
4577 			goto out;
4578 		}
4579 
4580 		index++;
4581 		cur += bytes;
4582 		page_offset = 0;
4583 	}
4584 	ret = priv->count;
4585 
4586 out:
4587 	btrfs_unlock_extent(io_tree, priv->start, priv->lockend, &priv->cached_state);
4588 	btrfs_inode_unlock(inode, BTRFS_ILOCK_SHARED);
4589 
4590 	io_uring_cmd_done(cmd, ret, IO_URING_CMD_TASK_WORK_ISSUE_FLAGS);
4591 	add_rchar(current, ret);
4592 
4593 	for (index = 0; index < priv->nr_pages; index++)
4594 		__free_page(priv->pages[index]);
4595 
4596 	kfree(priv->pages);
4597 	kfree(priv->iov);
4598 	kfree(priv);
4599 	kfree(bc->data);
4600 }
4601 
4602 void btrfs_uring_read_extent_endio(void *ctx, int err)
4603 {
4604 	struct btrfs_uring_priv *priv = ctx;
4605 	struct io_btrfs_cmd *bc = io_uring_cmd_to_pdu(priv->cmd, struct io_btrfs_cmd);
4606 
4607 	priv->err = err;
4608 	bc->priv = priv;
4609 
4610 	io_uring_cmd_complete_in_task(priv->cmd, btrfs_uring_read_finished);
4611 }
4612 
4613 static int btrfs_uring_read_extent(struct kiocb *iocb, struct iov_iter *iter,
4614 				   u64 start, u64 lockend,
4615 				   struct extent_state *cached_state,
4616 				   u64 disk_bytenr, u64 disk_io_size,
4617 				   size_t count, bool compressed,
4618 				   struct iovec *iov, struct io_uring_cmd *cmd)
4619 {
4620 	struct btrfs_inode *inode = BTRFS_I(file_inode(iocb->ki_filp));
4621 	struct extent_io_tree *io_tree = &inode->io_tree;
4622 	struct page **pages = NULL;
4623 	struct btrfs_uring_priv *priv = NULL;
4624 	unsigned long nr_pages;
4625 	int ret;
4626 
4627 	nr_pages = DIV_ROUND_UP(disk_io_size, PAGE_SIZE);
4628 	pages = kzalloc_objs(struct page *, nr_pages, GFP_NOFS);
4629 	if (!pages)
4630 		return -ENOMEM;
4631 	ret = btrfs_alloc_page_array(nr_pages, pages, GFP_NOFS);
4632 	if (ret) {
4633 		ret = -ENOMEM;
4634 		goto out_fail;
4635 	}
4636 
4637 	priv = kmalloc_obj(*priv, GFP_NOFS);
4638 	if (!priv) {
4639 		ret = -ENOMEM;
4640 		goto out_fail;
4641 	}
4642 
4643 	priv->iocb = *iocb;
4644 	priv->iov = iov;
4645 	priv->iter = *iter;
4646 	priv->count = count;
4647 	priv->cmd = cmd;
4648 	priv->cached_state = cached_state;
4649 	priv->compressed = compressed;
4650 	priv->nr_pages = nr_pages;
4651 	priv->pages = pages;
4652 	priv->start = start;
4653 	priv->lockend = lockend;
4654 	priv->err = 0;
4655 
4656 	ret = btrfs_encoded_read_regular_fill_pages(inode, disk_bytenr,
4657 						    disk_io_size, pages, priv);
4658 	if (ret && ret != -EIOCBQUEUED)
4659 		goto out_fail;
4660 
4661 	/*
4662 	 * If we return -EIOCBQUEUED, we're deferring the cleanup to
4663 	 * btrfs_uring_read_finished(), which will handle unlocking the extent
4664 	 * and inode and freeing the allocations.
4665 	 */
4666 
4667 	/*
4668 	 * We're returning to userspace with the inode lock held, and that's
4669 	 * okay - it'll get unlocked in a worker thread.  Call
4670 	 * btrfs_lockdep_inode_release() to avoid confusing lockdep.
4671 	 */
4672 	btrfs_lockdep_inode_release(inode, i_rwsem);
4673 
4674 	return -EIOCBQUEUED;
4675 
4676 out_fail:
4677 	btrfs_unlock_extent(io_tree, start, lockend, &cached_state);
4678 	btrfs_inode_unlock(inode, BTRFS_ILOCK_SHARED);
4679 	kfree(priv);
4680 	for (int i = 0; i < nr_pages; i++) {
4681 		if (pages[i])
4682 			__free_page(pages[i]);
4683 	}
4684 	kfree(pages);
4685 	return ret;
4686 }
4687 
4688 static int btrfs_uring_encoded_read(struct io_uring_cmd *cmd, unsigned int issue_flags)
4689 {
4690 	struct file *file = cmd->file;
4691 	struct btrfs_inode *inode = BTRFS_I(file->f_inode);
4692 	struct extent_io_tree *io_tree = &inode->io_tree;
4693 	struct btrfs_fs_info *fs_info = inode->root->fs_info;
4694 	size_t copy_end_kernel = offsetofend(struct btrfs_ioctl_encoded_io_args, flags);
4695 	size_t copy_end;
4696 	int ret;
4697 	u64 disk_bytenr, disk_io_size;
4698 	loff_t pos;
4699 	struct kiocb kiocb;
4700 	struct extent_state *cached_state = NULL;
4701 	u64 start, lockend;
4702 	void __user *sqe_addr;
4703 	struct io_btrfs_cmd *bc = io_uring_cmd_to_pdu(cmd, struct io_btrfs_cmd);
4704 	struct btrfs_uring_encoded_data *data = NULL;
4705 
4706 	if (cmd->flags & IORING_URING_CMD_REISSUE)
4707 		data = bc->data;
4708 
4709 	if (!capable(CAP_SYS_ADMIN)) {
4710 		ret = -EPERM;
4711 		goto out_acct;
4712 	}
4713 	sqe_addr = u64_to_user_ptr(READ_ONCE(cmd->sqe->addr));
4714 
4715 	if (issue_flags & IO_URING_F_COMPAT) {
4716 #if defined(CONFIG_64BIT) && defined(CONFIG_COMPAT)
4717 		copy_end = offsetofend(struct btrfs_ioctl_encoded_io_args_32, flags);
4718 #else
4719 		ret = -ENOTTY;
4720 		goto out_acct;
4721 #endif
4722 	} else {
4723 		copy_end = copy_end_kernel;
4724 	}
4725 
4726 	if (!data) {
4727 		data = kzalloc_obj(*data, GFP_NOFS);
4728 		if (!data) {
4729 			ret = -ENOMEM;
4730 			goto out_acct;
4731 		}
4732 
4733 		bc->data = data;
4734 
4735 		if (issue_flags & IO_URING_F_COMPAT) {
4736 #if defined(CONFIG_64BIT) && defined(CONFIG_COMPAT)
4737 			struct btrfs_ioctl_encoded_io_args_32 args32;
4738 
4739 			if (copy_from_user(&args32, sqe_addr, copy_end)) {
4740 				ret = -EFAULT;
4741 				goto out_acct;
4742 			}
4743 
4744 			data->args.iov = compat_ptr(args32.iov);
4745 			data->args.iovcnt = args32.iovcnt;
4746 			data->args.offset = args32.offset;
4747 			data->args.flags = args32.flags;
4748 #endif
4749 		} else {
4750 			if (copy_from_user(&data->args, sqe_addr, copy_end)) {
4751 				ret = -EFAULT;
4752 				goto out_acct;
4753 			}
4754 		}
4755 
4756 		if (data->args.flags != 0) {
4757 			ret = -EINVAL;
4758 			goto out_acct;
4759 		}
4760 
4761 		data->iov = data->iovstack;
4762 		ret = import_iovec(ITER_DEST, data->args.iov, data->args.iovcnt,
4763 				   ARRAY_SIZE(data->iovstack), &data->iov,
4764 				   &data->iter);
4765 		if (ret < 0)
4766 			goto out_acct;
4767 
4768 		if (iov_iter_count(&data->iter) == 0) {
4769 			ret = 0;
4770 			goto out_free;
4771 		}
4772 	}
4773 
4774 	pos = data->args.offset;
4775 	ret = rw_verify_area(READ, file, &pos, data->args.len);
4776 	if (ret < 0)
4777 		goto out_free;
4778 
4779 	init_sync_kiocb(&kiocb, file);
4780 	kiocb.ki_pos = pos;
4781 
4782 	if (issue_flags & IO_URING_F_NONBLOCK)
4783 		kiocb.ki_flags |= IOCB_NOWAIT;
4784 
4785 	start = ALIGN_DOWN(pos, fs_info->sectorsize);
4786 	lockend = start + BTRFS_MAX_UNCOMPRESSED - 1;
4787 
4788 	ret = btrfs_encoded_read(&kiocb, &data->iter, &data->args, &cached_state,
4789 				 &disk_bytenr, &disk_io_size);
4790 	if (ret == -EAGAIN)
4791 		goto out_acct;
4792 	if (ret < 0 && ret != -EIOCBQUEUED)
4793 		goto out_free;
4794 
4795 	file_accessed(file);
4796 
4797 	if (copy_to_user(sqe_addr + copy_end,
4798 			 (const char *)&data->args + copy_end_kernel,
4799 			 sizeof(data->args) - copy_end_kernel)) {
4800 		if (ret == -EIOCBQUEUED) {
4801 			btrfs_unlock_extent(io_tree, start, lockend, &cached_state);
4802 			btrfs_inode_unlock(inode, BTRFS_ILOCK_SHARED);
4803 		}
4804 		ret = -EFAULT;
4805 		goto out_free;
4806 	}
4807 
4808 	if (ret == -EIOCBQUEUED) {
4809 		u64 count = min_t(u64, iov_iter_count(&data->iter), disk_io_size);
4810 
4811 		/* Match ioctl by not returning past EOF if uncompressed. */
4812 		if (!data->args.compression)
4813 			count = min_t(u64, count, data->args.len);
4814 
4815 		ret = btrfs_uring_read_extent(&kiocb, &data->iter, start, lockend,
4816 					      cached_state, disk_bytenr, disk_io_size,
4817 					      count, data->args.compression,
4818 					      data->iov, cmd);
4819 
4820 		goto out_acct;
4821 	}
4822 
4823 out_free:
4824 	kfree(data->iov);
4825 
4826 out_acct:
4827 	if (ret > 0)
4828 		add_rchar(current, ret);
4829 	inc_syscr(current);
4830 
4831 	if (ret != -EIOCBQUEUED && ret != -EAGAIN)
4832 		kfree(data);
4833 
4834 	return ret;
4835 }
4836 
4837 static int btrfs_uring_encoded_write(struct io_uring_cmd *cmd, unsigned int issue_flags)
4838 {
4839 	struct file *file = cmd->file;
4840 	loff_t pos;
4841 	struct kiocb kiocb;
4842 	ssize_t ret;
4843 	void __user *sqe_addr;
4844 	struct io_btrfs_cmd *bc = io_uring_cmd_to_pdu(cmd, struct io_btrfs_cmd);
4845 	struct btrfs_uring_encoded_data *data = NULL;
4846 
4847 	if (cmd->flags & IORING_URING_CMD_REISSUE)
4848 		data = bc->data;
4849 
4850 	if (!capable(CAP_SYS_ADMIN)) {
4851 		ret = -EPERM;
4852 		goto out_acct;
4853 	}
4854 	sqe_addr = u64_to_user_ptr(READ_ONCE(cmd->sqe->addr));
4855 
4856 	if (!(file->f_mode & FMODE_WRITE)) {
4857 		ret = -EBADF;
4858 		goto out_acct;
4859 	}
4860 
4861 	if (!data) {
4862 		data = kzalloc_obj(*data, GFP_NOFS);
4863 		if (!data) {
4864 			ret = -ENOMEM;
4865 			goto out_acct;
4866 		}
4867 
4868 		bc->data = data;
4869 
4870 		if (issue_flags & IO_URING_F_COMPAT) {
4871 #if defined(CONFIG_64BIT) && defined(CONFIG_COMPAT)
4872 			struct btrfs_ioctl_encoded_io_args_32 args32;
4873 
4874 			if (copy_from_user(&args32, sqe_addr, sizeof(args32))) {
4875 				ret = -EFAULT;
4876 				goto out_acct;
4877 			}
4878 			data->args.iov = compat_ptr(args32.iov);
4879 			data->args.iovcnt = args32.iovcnt;
4880 			data->args.offset = args32.offset;
4881 			data->args.flags = args32.flags;
4882 			data->args.len = args32.len;
4883 			data->args.unencoded_len = args32.unencoded_len;
4884 			data->args.unencoded_offset = args32.unencoded_offset;
4885 			data->args.compression = args32.compression;
4886 			data->args.encryption = args32.encryption;
4887 			memcpy(data->args.reserved, args32.reserved,
4888 			       sizeof(data->args.reserved));
4889 #else
4890 			ret = -ENOTTY;
4891 			goto out_acct;
4892 #endif
4893 		} else {
4894 			if (copy_from_user(&data->args, sqe_addr, sizeof(data->args))) {
4895 				ret = -EFAULT;
4896 				goto out_acct;
4897 			}
4898 		}
4899 
4900 		ret = -EINVAL;
4901 		if (data->args.flags != 0)
4902 			goto out_acct;
4903 		if (memchr_inv(data->args.reserved, 0, sizeof(data->args.reserved)))
4904 			goto out_acct;
4905 		if (data->args.compression == BTRFS_ENCODED_IO_COMPRESSION_NONE &&
4906 		    data->args.encryption == BTRFS_ENCODED_IO_ENCRYPTION_NONE)
4907 			goto out_acct;
4908 		if (data->args.compression >= BTRFS_ENCODED_IO_COMPRESSION_TYPES ||
4909 		    data->args.encryption >= BTRFS_ENCODED_IO_ENCRYPTION_TYPES)
4910 			goto out_acct;
4911 		if (data->args.unencoded_offset > data->args.unencoded_len)
4912 			goto out_acct;
4913 		if (data->args.len > data->args.unencoded_len - data->args.unencoded_offset)
4914 			goto out_acct;
4915 
4916 		data->iov = data->iovstack;
4917 		ret = import_iovec(ITER_SOURCE, data->args.iov, data->args.iovcnt,
4918 				   ARRAY_SIZE(data->iovstack), &data->iov,
4919 				   &data->iter);
4920 		if (ret < 0)
4921 			goto out_acct;
4922 
4923 		if (iov_iter_count(&data->iter) == 0) {
4924 			ret = 0;
4925 			goto out_iov;
4926 		}
4927 	}
4928 
4929 	if (issue_flags & IO_URING_F_NONBLOCK) {
4930 		ret = -EAGAIN;
4931 		goto out_acct;
4932 	}
4933 
4934 	pos = data->args.offset;
4935 	ret = rw_verify_area(WRITE, file, &pos, data->args.len);
4936 	if (ret < 0)
4937 		goto out_iov;
4938 
4939 	init_sync_kiocb(&kiocb, file);
4940 	ret = kiocb_set_rw_flags(&kiocb, 0, WRITE);
4941 	if (ret)
4942 		goto out_iov;
4943 	kiocb.ki_pos = pos;
4944 
4945 	file_start_write(file);
4946 
4947 	ret = btrfs_do_write_iter(&kiocb, &data->iter, &data->args);
4948 	if (ret > 0)
4949 		fsnotify_modify(file);
4950 
4951 	file_end_write(file);
4952 out_iov:
4953 	kfree(data->iov);
4954 out_acct:
4955 	if (ret > 0)
4956 		add_wchar(current, ret);
4957 	inc_syscw(current);
4958 
4959 	if (ret != -EAGAIN)
4960 		kfree(data);
4961 	return ret;
4962 }
4963 
4964 int btrfs_uring_cmd(struct io_uring_cmd *cmd, unsigned int issue_flags)
4965 {
4966 	if (btrfs_is_shutdown(inode_to_fs_info(file_inode(cmd->file))))
4967 		return -EIO;
4968 
4969 	switch (cmd->cmd_op) {
4970 	case BTRFS_IOC_ENCODED_READ:
4971 #if defined(CONFIG_64BIT) && defined(CONFIG_COMPAT)
4972 	case BTRFS_IOC_ENCODED_READ_32:
4973 #endif
4974 		return btrfs_uring_encoded_read(cmd, issue_flags);
4975 
4976 	case BTRFS_IOC_ENCODED_WRITE:
4977 #if defined(CONFIG_64BIT) && defined(CONFIG_COMPAT)
4978 	case BTRFS_IOC_ENCODED_WRITE_32:
4979 #endif
4980 		return btrfs_uring_encoded_write(cmd, issue_flags);
4981 	}
4982 
4983 	return -EINVAL;
4984 }
4985 
4986 static int btrfs_ioctl_subvol_sync(struct btrfs_fs_info *fs_info, void __user *argp)
4987 {
4988 	struct btrfs_root *root;
4989 	struct btrfs_ioctl_subvol_wait args = { 0 };
4990 	signed long sched_ret;
4991 	int refs;
4992 	u64 root_flags;
4993 	bool wait_for_deletion = false;
4994 	bool found = false;
4995 
4996 	if (copy_from_user(&args, argp, sizeof(args)))
4997 		return -EFAULT;
4998 
4999 	switch (args.mode) {
5000 	case BTRFS_SUBVOL_SYNC_WAIT_FOR_QUEUED:
5001 		/*
5002 		 * Wait for the first one deleted that waits until all previous
5003 		 * are cleaned.
5004 		 */
5005 		spin_lock(&fs_info->trans_lock);
5006 		if (!list_empty(&fs_info->dead_roots)) {
5007 			root = list_last_entry(&fs_info->dead_roots,
5008 					       struct btrfs_root, root_list);
5009 			args.subvolid = btrfs_root_id(root);
5010 			found = true;
5011 		}
5012 		spin_unlock(&fs_info->trans_lock);
5013 		if (!found)
5014 			return -ENOENT;
5015 
5016 		fallthrough;
5017 	case BTRFS_SUBVOL_SYNC_WAIT_FOR_ONE:
5018 		if ((0 < args.subvolid && args.subvolid < BTRFS_FIRST_FREE_OBJECTID) ||
5019 		    BTRFS_LAST_FREE_OBJECTID < args.subvolid)
5020 			return -EINVAL;
5021 		break;
5022 	case BTRFS_SUBVOL_SYNC_COUNT:
5023 		spin_lock(&fs_info->trans_lock);
5024 		args.count = list_count_nodes(&fs_info->dead_roots);
5025 		spin_unlock(&fs_info->trans_lock);
5026 		if (copy_to_user(argp, &args, sizeof(args)))
5027 			return -EFAULT;
5028 		return 0;
5029 	case BTRFS_SUBVOL_SYNC_PEEK_FIRST:
5030 		spin_lock(&fs_info->trans_lock);
5031 		/* Last in the list was deleted first. */
5032 		if (!list_empty(&fs_info->dead_roots)) {
5033 			root = list_last_entry(&fs_info->dead_roots,
5034 					       struct btrfs_root, root_list);
5035 			args.subvolid = btrfs_root_id(root);
5036 		} else {
5037 			args.subvolid = 0;
5038 		}
5039 		spin_unlock(&fs_info->trans_lock);
5040 		if (copy_to_user(argp, &args, sizeof(args)))
5041 			return -EFAULT;
5042 		return 0;
5043 	case BTRFS_SUBVOL_SYNC_PEEK_LAST:
5044 		spin_lock(&fs_info->trans_lock);
5045 		/* First in the list was deleted last. */
5046 		if (!list_empty(&fs_info->dead_roots)) {
5047 			root = list_first_entry(&fs_info->dead_roots,
5048 						struct btrfs_root, root_list);
5049 			args.subvolid = btrfs_root_id(root);
5050 		} else {
5051 			args.subvolid = 0;
5052 		}
5053 		spin_unlock(&fs_info->trans_lock);
5054 		if (copy_to_user(argp, &args, sizeof(args)))
5055 			return -EFAULT;
5056 		return 0;
5057 	default:
5058 		return -EINVAL;
5059 	}
5060 
5061 	/* 32bit limitation: fs_roots_radix key is not wide enough. */
5062 	if (sizeof(unsigned long) != sizeof(u64) && args.subvolid > U32_MAX)
5063 		return -EOVERFLOW;
5064 
5065 	while (1) {
5066 		/* Wait for the specific one. */
5067 		if (down_read_interruptible(&fs_info->subvol_sem) == -EINTR)
5068 			return -EINTR;
5069 		refs = -1;
5070 		spin_lock(&fs_info->fs_roots_radix_lock);
5071 		root = radix_tree_lookup(&fs_info->fs_roots_radix,
5072 					 (unsigned long)args.subvolid);
5073 		if (root) {
5074 			spin_lock(&root->root_item_lock);
5075 			refs = btrfs_root_refs(&root->root_item);
5076 			root_flags = btrfs_root_flags(&root->root_item);
5077 			spin_unlock(&root->root_item_lock);
5078 		}
5079 		spin_unlock(&fs_info->fs_roots_radix_lock);
5080 		up_read(&fs_info->subvol_sem);
5081 
5082 		/* Subvolume does not exist. */
5083 		if (!root)
5084 			return -ENOENT;
5085 
5086 		/* Subvolume not deleted at all. */
5087 		if (refs > 0)
5088 			return -EEXIST;
5089 		/* We've waited and now the subvolume is gone. */
5090 		if (wait_for_deletion && refs == -1) {
5091 			/* Return the one we waited for as the last one. */
5092 			if (copy_to_user(argp, &args, sizeof(args)))
5093 				return -EFAULT;
5094 			return 0;
5095 		}
5096 
5097 		/* Subvolume not found on the first try (deleted or never existed). */
5098 		if (refs == -1)
5099 			return -ENOENT;
5100 
5101 		wait_for_deletion = true;
5102 		ASSERT(root_flags & BTRFS_ROOT_SUBVOL_DEAD, "root_flags=0x%llx",
5103 		       root_flags);
5104 		sched_ret = schedule_timeout_interruptible(HZ);
5105 		/* Early wake up or error. */
5106 		if (sched_ret != 0)
5107 			return -EINTR;
5108 	}
5109 
5110 	return 0;
5111 }
5112 
5113 static int btrfs_ioctl_shutdown(struct btrfs_fs_info *fs_info, unsigned long arg)
5114 {
5115 	int ret = 0;
5116 	u32 flags;
5117 
5118 	if (!capable(CAP_SYS_ADMIN))
5119 		return -EPERM;
5120 
5121 	if (get_user(flags, (u32 __user *)arg))
5122 		return -EFAULT;
5123 
5124 	if (flags >= BTRFS_SHUTDOWN_FLAGS_LAST)
5125 		return -EINVAL;
5126 
5127 	if (btrfs_is_shutdown(fs_info))
5128 		return 0;
5129 
5130 	switch (flags) {
5131 	case BTRFS_SHUTDOWN_FLAGS_LOGFLUSH:
5132 	case BTRFS_SHUTDOWN_FLAGS_DEFAULT:
5133 		ret = freeze_super(fs_info->sb, FREEZE_HOLDER_KERNEL, NULL);
5134 		if (ret)
5135 			return ret;
5136 		btrfs_force_shutdown(fs_info);
5137 		ret = thaw_super(fs_info->sb, FREEZE_HOLDER_KERNEL, NULL);
5138 		if (ret)
5139 			return ret;
5140 		break;
5141 	case BTRFS_SHUTDOWN_FLAGS_NOLOGFLUSH:
5142 		btrfs_force_shutdown(fs_info);
5143 		break;
5144 	default:
5145 		ret = -EINVAL;
5146 		break;
5147 	}
5148 	return ret;
5149 }
5150 
5151 #define GET_CSUMS_BUF_MAX	SZ_16M
5152 
5153 static int copy_csums_to_user(struct btrfs_fs_info *fs_info, u64 disk_bytenr,
5154 			      u64 len, u8 __user *buf)
5155 {
5156 	struct btrfs_root *csum_root;
5157 	struct btrfs_ordered_sum *sums;
5158 	LIST_HEAD(list);
5159 	const u32 csum_size = fs_info->csum_size;
5160 	int ret;
5161 
5162 	csum_root = btrfs_csum_root(fs_info, disk_bytenr);
5163 	if (unlikely(!csum_root)) {
5164 		btrfs_err(fs_info, "missing csum root for extent at bytenr %llu", disk_bytenr);
5165 		return -EUCLEAN;
5166 	}
5167 
5168 	ret = btrfs_lookup_csums_list(csum_root, disk_bytenr,
5169 				      disk_bytenr + len - 1, &list, false);
5170 	if (ret < 0)
5171 		return ret;
5172 
5173 	ret = 0;
5174 	while (!list_empty(&list)) {
5175 		u64 offset;
5176 		size_t copy_size;
5177 
5178 		sums = list_first_entry(&list, struct btrfs_ordered_sum, list);
5179 		list_del(&sums->list);
5180 
5181 		offset = ((sums->logical - disk_bytenr) >> fs_info->sectorsize_bits) * csum_size;
5182 		copy_size = (sums->len >> fs_info->sectorsize_bits) * csum_size;
5183 
5184 		if (copy_to_user(buf + offset, sums->sums, copy_size)) {
5185 			kfree(sums);
5186 			ret = -EFAULT;
5187 			goto out;
5188 		}
5189 
5190 		kfree(sums);
5191 	}
5192 
5193 out:
5194 	while (!list_empty(&list)) {
5195 		sums = list_first_entry(&list, struct btrfs_ordered_sum, list);
5196 		list_del(&sums->list);
5197 		kfree(sums);
5198 	}
5199 	return ret;
5200 }
5201 
5202 static int btrfs_ioctl_get_csums(struct file *file, void __user *argp)
5203 {
5204 	struct inode *vfs_inode = file_inode(file);
5205 	struct btrfs_inode *inode = BTRFS_I(vfs_inode);
5206 	struct btrfs_fs_info *fs_info = inode->root->fs_info;
5207 	struct btrfs_root *root = inode->root;
5208 	struct btrfs_ioctl_get_csums_args args = { 0 };
5209 	BTRFS_PATH_AUTO_FREE(path);
5210 	const u64 ino = btrfs_ino(inode);
5211 	const u32 csum_size = fs_info->csum_size;
5212 	u8 __user *ubuf;
5213 	u64 buf_limit;
5214 	u64 buf_used = 0;
5215 	u64 cur_offset;
5216 	u64 end_offset;
5217 	u64 prev_extent_end;
5218 	struct btrfs_key key;
5219 	int ret;
5220 
5221 	if (!(file->f_mode & FMODE_READ))
5222 		return -EBADF;
5223 
5224 	if (!S_ISREG(vfs_inode->i_mode))
5225 		return -EINVAL;
5226 
5227 	if (copy_from_user(&args, argp, sizeof(args)))
5228 		return -EFAULT;
5229 
5230 	if (!IS_ALIGNED(args.offset, fs_info->sectorsize) ||
5231 	    !IS_ALIGNED(args.length, fs_info->sectorsize))
5232 		return -EINVAL;
5233 	if (args.length == 0)
5234 		return -EINVAL;
5235 	if (args.offset + args.length < args.offset)
5236 		return -EOVERFLOW;
5237 	if (args.flags != 0)
5238 		return -EINVAL;
5239 	if (args.buf_size < sizeof(struct btrfs_ioctl_get_csums_entry))
5240 		return -EINVAL;
5241 
5242 	buf_limit = min_t(u64, args.buf_size, GET_CSUMS_BUF_MAX);
5243 	ubuf = (u8 __user *)(argp + offsetof(struct btrfs_ioctl_get_csums_args, buf));
5244 
5245 	if (clear_user(ubuf, buf_limit))
5246 		return -EFAULT;
5247 
5248 	cur_offset = args.offset;
5249 	end_offset = args.offset + args.length;
5250 
5251 	path = btrfs_alloc_path();
5252 	if (!path)
5253 		return -ENOMEM;
5254 
5255 	ret = btrfs_wait_ordered_range(inode, cur_offset, args.length);
5256 	if (ret)
5257 		return ret;
5258 
5259 	ret = down_read_interruptible(&vfs_inode->i_rwsem);
5260 	if (ret)
5261 		return ret;
5262 
5263 	ret = btrfs_wait_ordered_range(inode, cur_offset, args.length);
5264 	if (ret)
5265 		goto out_unlock;
5266 
5267 	/* NODATASUM early exit. */
5268 	if (inode->flags & BTRFS_INODE_NODATASUM) {
5269 		struct btrfs_ioctl_get_csums_entry entry = {
5270 			.offset = cur_offset,
5271 			.length = end_offset - cur_offset,
5272 			.type = BTRFS_GET_CSUMS_NODATASUM,
5273 		};
5274 
5275 		if (copy_to_user(ubuf, &entry, sizeof(entry))) {
5276 			ret = -EFAULT;
5277 			goto out_unlock;
5278 		}
5279 
5280 		buf_used = sizeof(entry);
5281 		cur_offset = end_offset;
5282 		goto done;
5283 	}
5284 
5285 	prev_extent_end = cur_offset;
5286 
5287 	while (cur_offset < end_offset) {
5288 		struct btrfs_file_extent_item *ei;
5289 		struct extent_buffer *leaf;
5290 		struct btrfs_ioctl_get_csums_entry entry = { 0 };
5291 		u64 extent_end;
5292 		u64 disk_bytenr = 0;
5293 		u64 extent_offset = 0;
5294 		u64 range_start, range_len;
5295 		u64 entry_csum_size;
5296 		u64 key_offset;
5297 		int extent_type;
5298 		u8 compression;
5299 		u8 encryption;
5300 
5301 		/* Search for the extent at or before cur_offset. */
5302 		key.objectid = ino;
5303 		key.type = BTRFS_EXTENT_DATA_KEY;
5304 		key.offset = cur_offset;
5305 
5306 		ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
5307 		if (ret < 0)
5308 			goto out_unlock;
5309 
5310 		if (ret > 0 && path->slots[0] > 0) {
5311 			btrfs_item_key_to_cpu(path->nodes[0], &key,
5312 					      path->slots[0] - 1);
5313 			if (key.objectid == ino && key.type == BTRFS_EXTENT_DATA_KEY) {
5314 				path->slots[0]--;
5315 				if (btrfs_file_extent_end(path) <= cur_offset)
5316 					path->slots[0]++;
5317 			}
5318 		}
5319 
5320 		if (path->slots[0] >= btrfs_header_nritems(path->nodes[0])) {
5321 			ret = btrfs_next_leaf(root, path);
5322 			if (ret < 0)
5323 				goto out_unlock;
5324 			if (ret > 0) {
5325 				ret = 0;
5326 				btrfs_release_path(path);
5327 				break;
5328 			}
5329 		}
5330 
5331 		leaf = path->nodes[0];
5332 
5333 		btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
5334 		if (key.objectid != ino || key.type != BTRFS_EXTENT_DATA_KEY) {
5335 			btrfs_release_path(path);
5336 			break;
5337 		}
5338 
5339 		extent_end = btrfs_file_extent_end(path);
5340 		key_offset = key.offset;
5341 
5342 		/* Read extent fields before releasing the path. */
5343 		ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_file_extent_item);
5344 		extent_type = btrfs_file_extent_type(leaf, ei);
5345 		compression = btrfs_file_extent_compression(leaf, ei);
5346 		encryption = btrfs_file_extent_encryption(leaf, ei);
5347 
5348 		if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
5349 			disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, ei);
5350 			if (disk_bytenr && compression == BTRFS_COMPRESS_NONE)
5351 				extent_offset = btrfs_file_extent_offset(leaf, ei);
5352 		}
5353 
5354 		btrfs_release_path(path);
5355 
5356 		/* Implicit hole (NO_HOLES feature). */
5357 		if (prev_extent_end < key_offset) {
5358 			u64 hole_end = min(key_offset, end_offset);
5359 			u64 hole_len = hole_end - prev_extent_end;
5360 
5361 			if (prev_extent_end >= cur_offset) {
5362 				entry.offset = prev_extent_end;
5363 				entry.length = hole_len;
5364 				entry.type = BTRFS_GET_CSUMS_ZEROED;
5365 
5366 				if (buf_used + sizeof(entry) > buf_limit)
5367 					goto done;
5368 				if (copy_to_user(ubuf + buf_used, &entry, sizeof(entry))) {
5369 					ret = -EFAULT;
5370 					goto out_unlock;
5371 				}
5372 				buf_used += sizeof(entry);
5373 				cur_offset = hole_end;
5374 			}
5375 
5376 			if (key_offset >= end_offset) {
5377 				cur_offset = end_offset;
5378 				break;
5379 			}
5380 		}
5381 
5382 		/* Clamp to our query range. */
5383 		range_start = max(cur_offset, key_offset);
5384 		range_len = min(extent_end, end_offset) - range_start;
5385 
5386 		entry.offset = range_start;
5387 		entry.length = range_len;
5388 
5389 		if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
5390 			entry.type = BTRFS_GET_CSUMS_INLINE;
5391 			if (compression != BTRFS_COMPRESS_NONE)
5392 				entry.type |= BTRFS_GET_CSUMS_COMPRESSED;
5393 			if (encryption != 0)
5394 				entry.type |= BTRFS_GET_CSUMS_ENCRYPTED;
5395 			entry_csum_size = 0;
5396 		} else if (extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
5397 			entry.type = BTRFS_GET_CSUMS_ZEROED;
5398 			entry_csum_size = 0;
5399 		} else {
5400 			/* BTRFS_FILE_EXTENT_REG */
5401 			if (disk_bytenr == 0) {
5402 				/* Explicit hole. */
5403 				entry.type = BTRFS_GET_CSUMS_ZEROED;
5404 				entry_csum_size = 0;
5405 			} else if (encryption != 0 || compression != BTRFS_COMPRESS_NONE) {
5406 				entry.type = 0;
5407 				if (encryption != 0)
5408 					entry.type |= BTRFS_GET_CSUMS_ENCRYPTED;
5409 				if (compression != BTRFS_COMPRESS_NONE)
5410 					entry.type |= BTRFS_GET_CSUMS_COMPRESSED;
5411 				entry_csum_size = 0;
5412 			} else {
5413 				entry.type = BTRFS_GET_CSUMS_HAS_CSUMS;
5414 				entry_csum_size = (range_len >> fs_info->sectorsize_bits) * csum_size;
5415 			}
5416 		}
5417 
5418 		/* Check if this entry (+ csum data) fits in the buffer. */
5419 		if (buf_used + sizeof(entry) + entry_csum_size > buf_limit) {
5420 			if (buf_used == 0) {
5421 				ret = -EOVERFLOW;
5422 				goto out_unlock;
5423 			}
5424 			goto done;
5425 		}
5426 
5427 		if (copy_to_user(ubuf + buf_used, &entry, sizeof(entry))) {
5428 			ret = -EFAULT;
5429 			goto out_unlock;
5430 		}
5431 		buf_used += sizeof(entry);
5432 
5433 		if (entry.type == BTRFS_GET_CSUMS_HAS_CSUMS) {
5434 			ret = copy_csums_to_user(fs_info,
5435 				disk_bytenr + extent_offset + (range_start - key_offset),
5436 				range_len, ubuf + buf_used);
5437 			if (ret)
5438 				goto out_unlock;
5439 			buf_used += entry_csum_size;
5440 		}
5441 
5442 		cur_offset = range_start + range_len;
5443 		prev_extent_end = extent_end;
5444 
5445 		if (fatal_signal_pending(current)) {
5446 			if (buf_used == 0) {
5447 				ret = -EINTR;
5448 				goto out_unlock;
5449 			}
5450 			goto done;
5451 		}
5452 
5453 		cond_resched();
5454 	}
5455 
5456 	/* Handle trailing implicit hole. */
5457 	if (cur_offset < end_offset) {
5458 		struct btrfs_ioctl_get_csums_entry entry = {
5459 			.offset = prev_extent_end,
5460 			.length = end_offset - prev_extent_end,
5461 			.type = BTRFS_GET_CSUMS_ZEROED,
5462 		};
5463 
5464 		if (buf_used + sizeof(entry) <= buf_limit) {
5465 			if (copy_to_user(ubuf + buf_used, &entry, sizeof(entry))) {
5466 				ret = -EFAULT;
5467 				goto out_unlock;
5468 			}
5469 			buf_used += sizeof(entry);
5470 			cur_offset = end_offset;
5471 		}
5472 	}
5473 
5474 done:
5475 	args.offset = cur_offset;
5476 	args.length = (cur_offset < end_offset) ? end_offset - cur_offset : 0;
5477 	args.buf_size = buf_used;
5478 
5479 	if (copy_to_user(argp, &args, sizeof(args)))
5480 		ret = -EFAULT;
5481 
5482 out_unlock:
5483 	up_read(&vfs_inode->i_rwsem);
5484 	return ret;
5485 }
5486 
5487 long btrfs_ioctl(struct file *file, unsigned int
5488 		cmd, unsigned long arg)
5489 {
5490 	struct inode *inode = file_inode(file);
5491 	struct btrfs_fs_info *fs_info = inode_to_fs_info(inode);
5492 	struct btrfs_root *root = BTRFS_I(inode)->root;
5493 	void __user *argp = (void __user *)arg;
5494 
5495 	switch (cmd) {
5496 	case FS_IOC_GETVERSION:
5497 		return btrfs_ioctl_getversion(inode, argp);
5498 	case FS_IOC_GETFSLABEL:
5499 		return btrfs_ioctl_get_fslabel(fs_info, argp);
5500 	case FS_IOC_SETFSLABEL:
5501 		return btrfs_ioctl_set_fslabel(file, argp);
5502 	case FITRIM:
5503 		return btrfs_ioctl_fitrim(fs_info, argp);
5504 	case BTRFS_IOC_SNAP_CREATE:
5505 		return btrfs_ioctl_snap_create(file, argp, false);
5506 	case BTRFS_IOC_SNAP_CREATE_V2:
5507 		return btrfs_ioctl_snap_create_v2(file, argp, false);
5508 	case BTRFS_IOC_SUBVOL_CREATE:
5509 		return btrfs_ioctl_snap_create(file, argp, true);
5510 	case BTRFS_IOC_SUBVOL_CREATE_V2:
5511 		return btrfs_ioctl_snap_create_v2(file, argp, true);
5512 	case BTRFS_IOC_SNAP_DESTROY:
5513 		return btrfs_ioctl_snap_destroy(file, argp, false);
5514 	case BTRFS_IOC_SNAP_DESTROY_V2:
5515 		return btrfs_ioctl_snap_destroy(file, argp, true);
5516 	case BTRFS_IOC_SUBVOL_GETFLAGS:
5517 		return btrfs_ioctl_subvol_getflags(BTRFS_I(inode), argp);
5518 	case BTRFS_IOC_SUBVOL_SETFLAGS:
5519 		return btrfs_ioctl_subvol_setflags(file, argp);
5520 	case BTRFS_IOC_DEFAULT_SUBVOL:
5521 		return btrfs_ioctl_default_subvol(file, argp);
5522 	case BTRFS_IOC_DEFRAG:
5523 		return btrfs_ioctl_defrag(file, NULL);
5524 	case BTRFS_IOC_DEFRAG_RANGE:
5525 		return btrfs_ioctl_defrag(file, argp);
5526 	case BTRFS_IOC_RESIZE:
5527 		return btrfs_ioctl_resize(file, argp);
5528 	case BTRFS_IOC_ADD_DEV:
5529 		return btrfs_ioctl_add_dev(fs_info, argp);
5530 	case BTRFS_IOC_RM_DEV:
5531 		return btrfs_ioctl_rm_dev(file, argp);
5532 	case BTRFS_IOC_RM_DEV_V2:
5533 		return btrfs_ioctl_rm_dev_v2(file, argp);
5534 	case BTRFS_IOC_FS_INFO:
5535 		return btrfs_ioctl_fs_info(fs_info, argp);
5536 	case BTRFS_IOC_DEV_INFO:
5537 		return btrfs_ioctl_dev_info(fs_info, argp);
5538 	case BTRFS_IOC_TREE_SEARCH:
5539 		return btrfs_ioctl_tree_search(root, argp);
5540 	case BTRFS_IOC_TREE_SEARCH_V2:
5541 		return btrfs_ioctl_tree_search_v2(root, argp);
5542 	case BTRFS_IOC_INO_LOOKUP:
5543 		return btrfs_ioctl_ino_lookup(root, argp);
5544 	case BTRFS_IOC_INO_PATHS:
5545 		return btrfs_ioctl_ino_to_path(root, argp);
5546 	case BTRFS_IOC_LOGICAL_INO:
5547 		return btrfs_ioctl_logical_to_ino(fs_info, argp, 1);
5548 	case BTRFS_IOC_LOGICAL_INO_V2:
5549 		return btrfs_ioctl_logical_to_ino(fs_info, argp, 2);
5550 	case BTRFS_IOC_SPACE_INFO:
5551 		return btrfs_ioctl_space_info(fs_info, argp);
5552 	case BTRFS_IOC_SYNC: {
5553 		int ret;
5554 
5555 		ret = btrfs_start_delalloc_roots(fs_info, LONG_MAX, false);
5556 		if (ret)
5557 			return ret;
5558 		ret = btrfs_sync_fs(inode->i_sb, 1);
5559 		/*
5560 		 * There may be work for the cleaner kthread to do (subvolume
5561 		 * deletion, delayed iputs, defrag inodes, etc), so wake it up.
5562 		 */
5563 		wake_up_process(fs_info->cleaner_kthread);
5564 		return ret;
5565 	}
5566 	case BTRFS_IOC_START_SYNC:
5567 		return btrfs_ioctl_start_sync(root, argp);
5568 	case BTRFS_IOC_WAIT_SYNC:
5569 		return btrfs_ioctl_wait_sync(fs_info, argp);
5570 	case BTRFS_IOC_SCRUB:
5571 		return btrfs_ioctl_scrub(file, argp);
5572 	case BTRFS_IOC_SCRUB_CANCEL:
5573 		return btrfs_ioctl_scrub_cancel(fs_info);
5574 	case BTRFS_IOC_SCRUB_PROGRESS:
5575 		return btrfs_ioctl_scrub_progress(fs_info, argp);
5576 	case BTRFS_IOC_BALANCE_V2:
5577 		return btrfs_ioctl_balance(file, argp);
5578 	case BTRFS_IOC_BALANCE_CTL:
5579 		return btrfs_ioctl_balance_ctl(fs_info, arg);
5580 	case BTRFS_IOC_BALANCE_PROGRESS:
5581 		return btrfs_ioctl_balance_progress(fs_info, argp);
5582 	case BTRFS_IOC_SET_RECEIVED_SUBVOL:
5583 		return btrfs_ioctl_set_received_subvol(file, argp);
5584 #ifdef CONFIG_64BIT
5585 	case BTRFS_IOC_SET_RECEIVED_SUBVOL_32:
5586 		return btrfs_ioctl_set_received_subvol_32(file, argp);
5587 #endif
5588 	case BTRFS_IOC_SEND:
5589 		return _btrfs_ioctl_send(root, argp, false);
5590 #if defined(CONFIG_64BIT) && defined(CONFIG_COMPAT)
5591 	case BTRFS_IOC_SEND_32:
5592 		return _btrfs_ioctl_send(root, argp, true);
5593 #endif
5594 	case BTRFS_IOC_GET_DEV_STATS:
5595 		return btrfs_ioctl_get_dev_stats(fs_info, argp);
5596 	case BTRFS_IOC_QUOTA_CTL:
5597 		return btrfs_ioctl_quota_ctl(file, argp);
5598 	case BTRFS_IOC_QGROUP_ASSIGN:
5599 		return btrfs_ioctl_qgroup_assign(file, argp);
5600 	case BTRFS_IOC_QGROUP_CREATE:
5601 		return btrfs_ioctl_qgroup_create(file, argp);
5602 	case BTRFS_IOC_QGROUP_LIMIT:
5603 		return btrfs_ioctl_qgroup_limit(file, argp);
5604 	case BTRFS_IOC_QUOTA_RESCAN:
5605 		return btrfs_ioctl_quota_rescan(file, argp);
5606 	case BTRFS_IOC_QUOTA_RESCAN_STATUS:
5607 		return btrfs_ioctl_quota_rescan_status(fs_info, argp);
5608 	case BTRFS_IOC_QUOTA_RESCAN_WAIT:
5609 		return btrfs_ioctl_quota_rescan_wait(fs_info);
5610 	case BTRFS_IOC_DEV_REPLACE:
5611 		return btrfs_ioctl_dev_replace(fs_info, argp);
5612 	case BTRFS_IOC_GET_SUPPORTED_FEATURES:
5613 		return btrfs_ioctl_get_supported_features(argp);
5614 	case BTRFS_IOC_GET_FEATURES:
5615 		return btrfs_ioctl_get_features(fs_info, argp);
5616 	case BTRFS_IOC_SET_FEATURES:
5617 		return btrfs_ioctl_set_features(file, argp);
5618 	case BTRFS_IOC_GET_SUBVOL_INFO:
5619 		return btrfs_ioctl_get_subvol_info(inode, argp);
5620 #ifdef CONFIG_64BIT
5621 	case BTRFS_IOC_GET_SUBVOL_INFO_32:
5622 		return btrfs_ioctl_get_subvol_info_32(inode, argp);
5623 #endif
5624 	case BTRFS_IOC_GET_SUBVOL_ROOTREF:
5625 		return btrfs_ioctl_get_subvol_rootref(root, argp);
5626 	case BTRFS_IOC_INO_LOOKUP_USER:
5627 		return btrfs_ioctl_ino_lookup_user(file, argp);
5628 	case FS_IOC_ENABLE_VERITY:
5629 		return fsverity_ioctl_enable(file, (const void __user *)argp);
5630 	case FS_IOC_MEASURE_VERITY:
5631 		return fsverity_ioctl_measure(file, argp);
5632 	case FS_IOC_READ_VERITY_METADATA:
5633 		return fsverity_ioctl_read_metadata(file, argp);
5634 	case BTRFS_IOC_ENCODED_READ:
5635 		return btrfs_ioctl_encoded_read(file, argp, false);
5636 	case BTRFS_IOC_ENCODED_WRITE:
5637 		return btrfs_ioctl_encoded_write(file, argp, false);
5638 #if defined(CONFIG_64BIT) && defined(CONFIG_COMPAT)
5639 	case BTRFS_IOC_ENCODED_READ_32:
5640 		return btrfs_ioctl_encoded_read(file, argp, true);
5641 	case BTRFS_IOC_ENCODED_WRITE_32:
5642 		return btrfs_ioctl_encoded_write(file, argp, true);
5643 #endif
5644 	case BTRFS_IOC_SUBVOL_SYNC_WAIT:
5645 		return btrfs_ioctl_subvol_sync(fs_info, argp);
5646 	case BTRFS_IOC_SHUTDOWN:
5647 		return btrfs_ioctl_shutdown(fs_info, arg);
5648 	case BTRFS_IOC_GET_CSUMS:
5649 		return btrfs_ioctl_get_csums(file, argp);
5650 	}
5651 
5652 	return -ENOTTY;
5653 }
5654 
5655 #ifdef CONFIG_COMPAT
5656 long btrfs_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
5657 {
5658 	/*
5659 	 * These all access 32-bit values anyway so no further
5660 	 * handling is necessary.
5661 	 */
5662 	switch (cmd) {
5663 	case FS_IOC32_GETVERSION:
5664 		cmd = FS_IOC_GETVERSION;
5665 		break;
5666 	}
5667 
5668 	return btrfs_ioctl(file, cmd, (unsigned long) compat_ptr(arg));
5669 }
5670 #endif
5671