xref: /openbmc/linux/fs/btrfs/ioctl.c (revision cc68a8a5)
1f46b5a66SChristoph Hellwig /*
2f46b5a66SChristoph Hellwig  * Copyright (C) 2007 Oracle.  All rights reserved.
3f46b5a66SChristoph Hellwig  *
4f46b5a66SChristoph Hellwig  * This program is free software; you can redistribute it and/or
5f46b5a66SChristoph Hellwig  * modify it under the terms of the GNU General Public
6f46b5a66SChristoph Hellwig  * License v2 as published by the Free Software Foundation.
7f46b5a66SChristoph Hellwig  *
8f46b5a66SChristoph Hellwig  * This program is distributed in the hope that it will be useful,
9f46b5a66SChristoph Hellwig  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10f46b5a66SChristoph Hellwig  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11f46b5a66SChristoph Hellwig  * General Public License for more details.
12f46b5a66SChristoph Hellwig  *
13f46b5a66SChristoph Hellwig  * You should have received a copy of the GNU General Public
14f46b5a66SChristoph Hellwig  * License along with this program; if not, write to the
15f46b5a66SChristoph Hellwig  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
16f46b5a66SChristoph Hellwig  * Boston, MA 021110-1307, USA.
17f46b5a66SChristoph Hellwig  */
18f46b5a66SChristoph Hellwig 
19f46b5a66SChristoph Hellwig #include <linux/kernel.h>
20f46b5a66SChristoph Hellwig #include <linux/bio.h>
21f46b5a66SChristoph Hellwig #include <linux/buffer_head.h>
22f46b5a66SChristoph Hellwig #include <linux/file.h>
23f46b5a66SChristoph Hellwig #include <linux/fs.h>
24cb8e7090SChristoph Hellwig #include <linux/fsnotify.h>
25f46b5a66SChristoph Hellwig #include <linux/pagemap.h>
26f46b5a66SChristoph Hellwig #include <linux/highmem.h>
27f46b5a66SChristoph Hellwig #include <linux/time.h>
28f46b5a66SChristoph Hellwig #include <linux/init.h>
29f46b5a66SChristoph Hellwig #include <linux/string.h>
30f46b5a66SChristoph Hellwig #include <linux/backing-dev.h>
31cb8e7090SChristoph Hellwig #include <linux/mount.h>
32f46b5a66SChristoph Hellwig #include <linux/mpage.h>
33cb8e7090SChristoph Hellwig #include <linux/namei.h>
34f46b5a66SChristoph Hellwig #include <linux/swap.h>
35f46b5a66SChristoph Hellwig #include <linux/writeback.h>
36f46b5a66SChristoph Hellwig #include <linux/statfs.h>
37f46b5a66SChristoph Hellwig #include <linux/compat.h>
38f46b5a66SChristoph Hellwig #include <linux/bit_spinlock.h>
39cb8e7090SChristoph Hellwig #include <linux/security.h>
40f46b5a66SChristoph Hellwig #include <linux/xattr.h>
417ea394f1SYan Zheng #include <linux/vmalloc.h>
425a0e3ad6STejun Heo #include <linux/slab.h>
43f7039b1dSLi Dongyang #include <linux/blkdev.h>
448ea05e3aSAlexander Block #include <linux/uuid.h>
4555e301fdSFilipe Brandenburger #include <linux/btrfs.h>
46416161dbSMark Fasheh #include <linux/uaccess.h>
47f46b5a66SChristoph Hellwig #include "ctree.h"
48f46b5a66SChristoph Hellwig #include "disk-io.h"
49f46b5a66SChristoph Hellwig #include "transaction.h"
50f46b5a66SChristoph Hellwig #include "btrfs_inode.h"
51f46b5a66SChristoph Hellwig #include "print-tree.h"
52f46b5a66SChristoph Hellwig #include "volumes.h"
53925baeddSChris Mason #include "locking.h"
54581bb050SLi Zefan #include "inode-map.h"
55d7728c96SJan Schmidt #include "backref.h"
56606686eeSJosef Bacik #include "rcu-string.h"
5731db9f7cSAlexander Block #include "send.h"
583f6bcfbdSStefan Behrens #include "dev-replace.h"
5963541927SFilipe David Borba Manana #include "props.h"
603b02a68aSJeff Mahoney #include "sysfs.h"
61fcebe456SJosef Bacik #include "qgroup.h"
62f46b5a66SChristoph Hellwig 
63abccd00fSHugo Mills #ifdef CONFIG_64BIT
64abccd00fSHugo Mills /* If we have a 32-bit userspace and 64-bit kernel, then the UAPI
65abccd00fSHugo Mills  * structures are incorrect, as the timespec structure from userspace
66abccd00fSHugo Mills  * is 4 bytes too small. We define these alternatives here to teach
67abccd00fSHugo Mills  * the kernel about the 32-bit struct packing.
68abccd00fSHugo Mills  */
69abccd00fSHugo Mills struct btrfs_ioctl_timespec_32 {
70abccd00fSHugo Mills 	__u64 sec;
71abccd00fSHugo Mills 	__u32 nsec;
72abccd00fSHugo Mills } __attribute__ ((__packed__));
73abccd00fSHugo Mills 
74abccd00fSHugo Mills struct btrfs_ioctl_received_subvol_args_32 {
75abccd00fSHugo Mills 	char	uuid[BTRFS_UUID_SIZE];	/* in */
76abccd00fSHugo Mills 	__u64	stransid;		/* in */
77abccd00fSHugo Mills 	__u64	rtransid;		/* out */
78abccd00fSHugo Mills 	struct btrfs_ioctl_timespec_32 stime; /* in */
79abccd00fSHugo Mills 	struct btrfs_ioctl_timespec_32 rtime; /* out */
80abccd00fSHugo Mills 	__u64	flags;			/* in */
81abccd00fSHugo Mills 	__u64	reserved[16];		/* in */
82abccd00fSHugo Mills } __attribute__ ((__packed__));
83abccd00fSHugo Mills 
84abccd00fSHugo Mills #define BTRFS_IOC_SET_RECEIVED_SUBVOL_32 _IOWR(BTRFS_IOCTL_MAGIC, 37, \
85abccd00fSHugo Mills 				struct btrfs_ioctl_received_subvol_args_32)
86abccd00fSHugo Mills #endif
87abccd00fSHugo Mills 
88abccd00fSHugo Mills 
89416161dbSMark Fasheh static int btrfs_clone(struct inode *src, struct inode *inode,
90416161dbSMark Fasheh 		       u64 off, u64 olen, u64 olen_aligned, u64 destoff);
91416161dbSMark Fasheh 
926cbff00fSChristoph Hellwig /* Mask out flags that are inappropriate for the given type of inode. */
936cbff00fSChristoph Hellwig static inline __u32 btrfs_mask_flags(umode_t mode, __u32 flags)
946cbff00fSChristoph Hellwig {
956cbff00fSChristoph Hellwig 	if (S_ISDIR(mode))
966cbff00fSChristoph Hellwig 		return flags;
976cbff00fSChristoph Hellwig 	else if (S_ISREG(mode))
986cbff00fSChristoph Hellwig 		return flags & ~FS_DIRSYNC_FL;
996cbff00fSChristoph Hellwig 	else
1006cbff00fSChristoph Hellwig 		return flags & (FS_NODUMP_FL | FS_NOATIME_FL);
1016cbff00fSChristoph Hellwig }
102f46b5a66SChristoph Hellwig 
1036cbff00fSChristoph Hellwig /*
1046cbff00fSChristoph Hellwig  * Export inode flags to the format expected by the FS_IOC_GETFLAGS ioctl.
1056cbff00fSChristoph Hellwig  */
1066cbff00fSChristoph Hellwig static unsigned int btrfs_flags_to_ioctl(unsigned int flags)
1076cbff00fSChristoph Hellwig {
1086cbff00fSChristoph Hellwig 	unsigned int iflags = 0;
1096cbff00fSChristoph Hellwig 
1106cbff00fSChristoph Hellwig 	if (flags & BTRFS_INODE_SYNC)
1116cbff00fSChristoph Hellwig 		iflags |= FS_SYNC_FL;
1126cbff00fSChristoph Hellwig 	if (flags & BTRFS_INODE_IMMUTABLE)
1136cbff00fSChristoph Hellwig 		iflags |= FS_IMMUTABLE_FL;
1146cbff00fSChristoph Hellwig 	if (flags & BTRFS_INODE_APPEND)
1156cbff00fSChristoph Hellwig 		iflags |= FS_APPEND_FL;
1166cbff00fSChristoph Hellwig 	if (flags & BTRFS_INODE_NODUMP)
1176cbff00fSChristoph Hellwig 		iflags |= FS_NODUMP_FL;
1186cbff00fSChristoph Hellwig 	if (flags & BTRFS_INODE_NOATIME)
1196cbff00fSChristoph Hellwig 		iflags |= FS_NOATIME_FL;
1206cbff00fSChristoph Hellwig 	if (flags & BTRFS_INODE_DIRSYNC)
1216cbff00fSChristoph Hellwig 		iflags |= FS_DIRSYNC_FL;
122d0092bddSLi Zefan 	if (flags & BTRFS_INODE_NODATACOW)
123d0092bddSLi Zefan 		iflags |= FS_NOCOW_FL;
124d0092bddSLi Zefan 
125d0092bddSLi Zefan 	if ((flags & BTRFS_INODE_COMPRESS) && !(flags & BTRFS_INODE_NOCOMPRESS))
126d0092bddSLi Zefan 		iflags |= FS_COMPR_FL;
127d0092bddSLi Zefan 	else if (flags & BTRFS_INODE_NOCOMPRESS)
128d0092bddSLi Zefan 		iflags |= FS_NOCOMP_FL;
1296cbff00fSChristoph Hellwig 
1306cbff00fSChristoph Hellwig 	return iflags;
1316cbff00fSChristoph Hellwig }
1326cbff00fSChristoph Hellwig 
1336cbff00fSChristoph Hellwig /*
1346cbff00fSChristoph Hellwig  * Update inode->i_flags based on the btrfs internal flags.
1356cbff00fSChristoph Hellwig  */
1366cbff00fSChristoph Hellwig void btrfs_update_iflags(struct inode *inode)
1376cbff00fSChristoph Hellwig {
1386cbff00fSChristoph Hellwig 	struct btrfs_inode *ip = BTRFS_I(inode);
1396cbff00fSChristoph Hellwig 
1406cbff00fSChristoph Hellwig 	inode->i_flags &= ~(S_SYNC|S_APPEND|S_IMMUTABLE|S_NOATIME|S_DIRSYNC);
1416cbff00fSChristoph Hellwig 
1426cbff00fSChristoph Hellwig 	if (ip->flags & BTRFS_INODE_SYNC)
1436cbff00fSChristoph Hellwig 		inode->i_flags |= S_SYNC;
1446cbff00fSChristoph Hellwig 	if (ip->flags & BTRFS_INODE_IMMUTABLE)
1456cbff00fSChristoph Hellwig 		inode->i_flags |= S_IMMUTABLE;
1466cbff00fSChristoph Hellwig 	if (ip->flags & BTRFS_INODE_APPEND)
1476cbff00fSChristoph Hellwig 		inode->i_flags |= S_APPEND;
1486cbff00fSChristoph Hellwig 	if (ip->flags & BTRFS_INODE_NOATIME)
1496cbff00fSChristoph Hellwig 		inode->i_flags |= S_NOATIME;
1506cbff00fSChristoph Hellwig 	if (ip->flags & BTRFS_INODE_DIRSYNC)
1516cbff00fSChristoph Hellwig 		inode->i_flags |= S_DIRSYNC;
1526cbff00fSChristoph Hellwig }
1536cbff00fSChristoph Hellwig 
1546cbff00fSChristoph Hellwig /*
1556cbff00fSChristoph Hellwig  * Inherit flags from the parent inode.
1566cbff00fSChristoph Hellwig  *
157e27425d6SJosef Bacik  * Currently only the compression flags and the cow flags are inherited.
1586cbff00fSChristoph Hellwig  */
1596cbff00fSChristoph Hellwig void btrfs_inherit_iflags(struct inode *inode, struct inode *dir)
1606cbff00fSChristoph Hellwig {
1610b4dcea5SChris Mason 	unsigned int flags;
1620b4dcea5SChris Mason 
1630b4dcea5SChris Mason 	if (!dir)
1640b4dcea5SChris Mason 		return;
1650b4dcea5SChris Mason 
1660b4dcea5SChris Mason 	flags = BTRFS_I(dir)->flags;
1676cbff00fSChristoph Hellwig 
168e27425d6SJosef Bacik 	if (flags & BTRFS_INODE_NOCOMPRESS) {
169e27425d6SJosef Bacik 		BTRFS_I(inode)->flags &= ~BTRFS_INODE_COMPRESS;
170e27425d6SJosef Bacik 		BTRFS_I(inode)->flags |= BTRFS_INODE_NOCOMPRESS;
171e27425d6SJosef Bacik 	} else if (flags & BTRFS_INODE_COMPRESS) {
172e27425d6SJosef Bacik 		BTRFS_I(inode)->flags &= ~BTRFS_INODE_NOCOMPRESS;
173e27425d6SJosef Bacik 		BTRFS_I(inode)->flags |= BTRFS_INODE_COMPRESS;
174e27425d6SJosef Bacik 	}
1756cbff00fSChristoph Hellwig 
176213490b3SLiu Bo 	if (flags & BTRFS_INODE_NODATACOW) {
177e27425d6SJosef Bacik 		BTRFS_I(inode)->flags |= BTRFS_INODE_NODATACOW;
178213490b3SLiu Bo 		if (S_ISREG(inode->i_mode))
179213490b3SLiu Bo 			BTRFS_I(inode)->flags |= BTRFS_INODE_NODATASUM;
180213490b3SLiu Bo 	}
181e27425d6SJosef Bacik 
1826cbff00fSChristoph Hellwig 	btrfs_update_iflags(inode);
1836cbff00fSChristoph Hellwig }
1846cbff00fSChristoph Hellwig 
1856cbff00fSChristoph Hellwig static int btrfs_ioctl_getflags(struct file *file, void __user *arg)
1866cbff00fSChristoph Hellwig {
187496ad9aaSAl Viro 	struct btrfs_inode *ip = BTRFS_I(file_inode(file));
1886cbff00fSChristoph Hellwig 	unsigned int flags = btrfs_flags_to_ioctl(ip->flags);
1896cbff00fSChristoph Hellwig 
1906cbff00fSChristoph Hellwig 	if (copy_to_user(arg, &flags, sizeof(flags)))
1916cbff00fSChristoph Hellwig 		return -EFAULT;
1926cbff00fSChristoph Hellwig 	return 0;
1936cbff00fSChristoph Hellwig }
1946cbff00fSChristoph Hellwig 
19575e7cb7fSLiu Bo static int check_flags(unsigned int flags)
19675e7cb7fSLiu Bo {
19775e7cb7fSLiu Bo 	if (flags & ~(FS_IMMUTABLE_FL | FS_APPEND_FL | \
19875e7cb7fSLiu Bo 		      FS_NOATIME_FL | FS_NODUMP_FL | \
19975e7cb7fSLiu Bo 		      FS_SYNC_FL | FS_DIRSYNC_FL | \
200e1e8fb6aSLi Zefan 		      FS_NOCOMP_FL | FS_COMPR_FL |
201e1e8fb6aSLi Zefan 		      FS_NOCOW_FL))
20275e7cb7fSLiu Bo 		return -EOPNOTSUPP;
20375e7cb7fSLiu Bo 
20475e7cb7fSLiu Bo 	if ((flags & FS_NOCOMP_FL) && (flags & FS_COMPR_FL))
20575e7cb7fSLiu Bo 		return -EINVAL;
20675e7cb7fSLiu Bo 
20775e7cb7fSLiu Bo 	return 0;
20875e7cb7fSLiu Bo }
20975e7cb7fSLiu Bo 
2106cbff00fSChristoph Hellwig static int btrfs_ioctl_setflags(struct file *file, void __user *arg)
2116cbff00fSChristoph Hellwig {
212496ad9aaSAl Viro 	struct inode *inode = file_inode(file);
2136cbff00fSChristoph Hellwig 	struct btrfs_inode *ip = BTRFS_I(inode);
2146cbff00fSChristoph Hellwig 	struct btrfs_root *root = ip->root;
2156cbff00fSChristoph Hellwig 	struct btrfs_trans_handle *trans;
2166cbff00fSChristoph Hellwig 	unsigned int flags, oldflags;
2176cbff00fSChristoph Hellwig 	int ret;
218f062abf0SLi Zefan 	u64 ip_oldflags;
219f062abf0SLi Zefan 	unsigned int i_oldflags;
2207e97b8daSDavid Sterba 	umode_t mode;
2216cbff00fSChristoph Hellwig 
222bd60ea0fSDavid Sterba 	if (!inode_owner_or_capable(inode))
223bd60ea0fSDavid Sterba 		return -EPERM;
224bd60ea0fSDavid Sterba 
225b83cc969SLi Zefan 	if (btrfs_root_readonly(root))
226b83cc969SLi Zefan 		return -EROFS;
227b83cc969SLi Zefan 
2286cbff00fSChristoph Hellwig 	if (copy_from_user(&flags, arg, sizeof(flags)))
2296cbff00fSChristoph Hellwig 		return -EFAULT;
2306cbff00fSChristoph Hellwig 
23175e7cb7fSLiu Bo 	ret = check_flags(flags);
23275e7cb7fSLiu Bo 	if (ret)
23375e7cb7fSLiu Bo 		return ret;
2346cbff00fSChristoph Hellwig 
235e7848683SJan Kara 	ret = mnt_want_write_file(file);
236e7848683SJan Kara 	if (ret)
237e7848683SJan Kara 		return ret;
238e7848683SJan Kara 
2396cbff00fSChristoph Hellwig 	mutex_lock(&inode->i_mutex);
2406cbff00fSChristoph Hellwig 
241f062abf0SLi Zefan 	ip_oldflags = ip->flags;
242f062abf0SLi Zefan 	i_oldflags = inode->i_flags;
2437e97b8daSDavid Sterba 	mode = inode->i_mode;
244f062abf0SLi Zefan 
2456cbff00fSChristoph Hellwig 	flags = btrfs_mask_flags(inode->i_mode, flags);
2466cbff00fSChristoph Hellwig 	oldflags = btrfs_flags_to_ioctl(ip->flags);
2476cbff00fSChristoph Hellwig 	if ((flags ^ oldflags) & (FS_APPEND_FL | FS_IMMUTABLE_FL)) {
2486cbff00fSChristoph Hellwig 		if (!capable(CAP_LINUX_IMMUTABLE)) {
2496cbff00fSChristoph Hellwig 			ret = -EPERM;
2506cbff00fSChristoph Hellwig 			goto out_unlock;
2516cbff00fSChristoph Hellwig 		}
2526cbff00fSChristoph Hellwig 	}
2536cbff00fSChristoph Hellwig 
2546cbff00fSChristoph Hellwig 	if (flags & FS_SYNC_FL)
2556cbff00fSChristoph Hellwig 		ip->flags |= BTRFS_INODE_SYNC;
2566cbff00fSChristoph Hellwig 	else
2576cbff00fSChristoph Hellwig 		ip->flags &= ~BTRFS_INODE_SYNC;
2586cbff00fSChristoph Hellwig 	if (flags & FS_IMMUTABLE_FL)
2596cbff00fSChristoph Hellwig 		ip->flags |= BTRFS_INODE_IMMUTABLE;
2606cbff00fSChristoph Hellwig 	else
2616cbff00fSChristoph Hellwig 		ip->flags &= ~BTRFS_INODE_IMMUTABLE;
2626cbff00fSChristoph Hellwig 	if (flags & FS_APPEND_FL)
2636cbff00fSChristoph Hellwig 		ip->flags |= BTRFS_INODE_APPEND;
2646cbff00fSChristoph Hellwig 	else
2656cbff00fSChristoph Hellwig 		ip->flags &= ~BTRFS_INODE_APPEND;
2666cbff00fSChristoph Hellwig 	if (flags & FS_NODUMP_FL)
2676cbff00fSChristoph Hellwig 		ip->flags |= BTRFS_INODE_NODUMP;
2686cbff00fSChristoph Hellwig 	else
2696cbff00fSChristoph Hellwig 		ip->flags &= ~BTRFS_INODE_NODUMP;
2706cbff00fSChristoph Hellwig 	if (flags & FS_NOATIME_FL)
2716cbff00fSChristoph Hellwig 		ip->flags |= BTRFS_INODE_NOATIME;
2726cbff00fSChristoph Hellwig 	else
2736cbff00fSChristoph Hellwig 		ip->flags &= ~BTRFS_INODE_NOATIME;
2746cbff00fSChristoph Hellwig 	if (flags & FS_DIRSYNC_FL)
2756cbff00fSChristoph Hellwig 		ip->flags |= BTRFS_INODE_DIRSYNC;
2766cbff00fSChristoph Hellwig 	else
2776cbff00fSChristoph Hellwig 		ip->flags &= ~BTRFS_INODE_DIRSYNC;
2787e97b8daSDavid Sterba 	if (flags & FS_NOCOW_FL) {
2797e97b8daSDavid Sterba 		if (S_ISREG(mode)) {
2807e97b8daSDavid Sterba 			/*
2817e97b8daSDavid Sterba 			 * It's safe to turn csums off here, no extents exist.
2827e97b8daSDavid Sterba 			 * Otherwise we want the flag to reflect the real COW
2837e97b8daSDavid Sterba 			 * status of the file and will not set it.
2847e97b8daSDavid Sterba 			 */
2857e97b8daSDavid Sterba 			if (inode->i_size == 0)
2867e97b8daSDavid Sterba 				ip->flags |= BTRFS_INODE_NODATACOW
2877e97b8daSDavid Sterba 					   | BTRFS_INODE_NODATASUM;
2887e97b8daSDavid Sterba 		} else {
289e1e8fb6aSLi Zefan 			ip->flags |= BTRFS_INODE_NODATACOW;
2907e97b8daSDavid Sterba 		}
2917e97b8daSDavid Sterba 	} else {
2927e97b8daSDavid Sterba 		/*
2937e97b8daSDavid Sterba 		 * Revert back under same assuptions as above
2947e97b8daSDavid Sterba 		 */
2957e97b8daSDavid Sterba 		if (S_ISREG(mode)) {
2967e97b8daSDavid Sterba 			if (inode->i_size == 0)
2977e97b8daSDavid Sterba 				ip->flags &= ~(BTRFS_INODE_NODATACOW
2987e97b8daSDavid Sterba 				             | BTRFS_INODE_NODATASUM);
2997e97b8daSDavid Sterba 		} else {
300e1e8fb6aSLi Zefan 			ip->flags &= ~BTRFS_INODE_NODATACOW;
3017e97b8daSDavid Sterba 		}
3027e97b8daSDavid Sterba 	}
3036cbff00fSChristoph Hellwig 
30475e7cb7fSLiu Bo 	/*
30575e7cb7fSLiu Bo 	 * The COMPRESS flag can only be changed by users, while the NOCOMPRESS
30675e7cb7fSLiu Bo 	 * flag may be changed automatically if compression code won't make
30775e7cb7fSLiu Bo 	 * things smaller.
30875e7cb7fSLiu Bo 	 */
30975e7cb7fSLiu Bo 	if (flags & FS_NOCOMP_FL) {
31075e7cb7fSLiu Bo 		ip->flags &= ~BTRFS_INODE_COMPRESS;
31175e7cb7fSLiu Bo 		ip->flags |= BTRFS_INODE_NOCOMPRESS;
31263541927SFilipe David Borba Manana 
31363541927SFilipe David Borba Manana 		ret = btrfs_set_prop(inode, "btrfs.compression", NULL, 0, 0);
31463541927SFilipe David Borba Manana 		if (ret && ret != -ENODATA)
31563541927SFilipe David Borba Manana 			goto out_drop;
31675e7cb7fSLiu Bo 	} else if (flags & FS_COMPR_FL) {
31763541927SFilipe David Borba Manana 		const char *comp;
31863541927SFilipe David Borba Manana 
31975e7cb7fSLiu Bo 		ip->flags |= BTRFS_INODE_COMPRESS;
32075e7cb7fSLiu Bo 		ip->flags &= ~BTRFS_INODE_NOCOMPRESS;
32163541927SFilipe David Borba Manana 
32263541927SFilipe David Borba Manana 		if (root->fs_info->compress_type == BTRFS_COMPRESS_LZO)
32363541927SFilipe David Borba Manana 			comp = "lzo";
32463541927SFilipe David Borba Manana 		else
32563541927SFilipe David Borba Manana 			comp = "zlib";
32663541927SFilipe David Borba Manana 		ret = btrfs_set_prop(inode, "btrfs.compression",
32763541927SFilipe David Borba Manana 				     comp, strlen(comp), 0);
32863541927SFilipe David Borba Manana 		if (ret)
32963541927SFilipe David Borba Manana 			goto out_drop;
33063541927SFilipe David Borba Manana 
331ebcb904dSLi Zefan 	} else {
332ebcb904dSLi Zefan 		ip->flags &= ~(BTRFS_INODE_COMPRESS | BTRFS_INODE_NOCOMPRESS);
33375e7cb7fSLiu Bo 	}
3346cbff00fSChristoph Hellwig 
3354da6f1a3SLi Zefan 	trans = btrfs_start_transaction(root, 1);
336f062abf0SLi Zefan 	if (IS_ERR(trans)) {
337f062abf0SLi Zefan 		ret = PTR_ERR(trans);
338f062abf0SLi Zefan 		goto out_drop;
339f062abf0SLi Zefan 	}
3406cbff00fSChristoph Hellwig 
341306424ccSLi Zefan 	btrfs_update_iflags(inode);
3420c4d2d95SJosef Bacik 	inode_inc_iversion(inode);
343306424ccSLi Zefan 	inode->i_ctime = CURRENT_TIME;
3446cbff00fSChristoph Hellwig 	ret = btrfs_update_inode(trans, root, inode);
3456cbff00fSChristoph Hellwig 
3466cbff00fSChristoph Hellwig 	btrfs_end_transaction(trans, root);
347f062abf0SLi Zefan  out_drop:
348f062abf0SLi Zefan 	if (ret) {
349f062abf0SLi Zefan 		ip->flags = ip_oldflags;
350f062abf0SLi Zefan 		inode->i_flags = i_oldflags;
351f062abf0SLi Zefan 	}
3526cbff00fSChristoph Hellwig 
3536cbff00fSChristoph Hellwig  out_unlock:
3546cbff00fSChristoph Hellwig 	mutex_unlock(&inode->i_mutex);
355e7848683SJan Kara 	mnt_drop_write_file(file);
3562d4e6f6aSliubo 	return ret;
3576cbff00fSChristoph Hellwig }
3586cbff00fSChristoph Hellwig 
3596cbff00fSChristoph Hellwig static int btrfs_ioctl_getversion(struct file *file, int __user *arg)
3606cbff00fSChristoph Hellwig {
361496ad9aaSAl Viro 	struct inode *inode = file_inode(file);
3626cbff00fSChristoph Hellwig 
3636cbff00fSChristoph Hellwig 	return put_user(inode->i_generation, arg);
3646cbff00fSChristoph Hellwig }
365f46b5a66SChristoph Hellwig 
366f7039b1dSLi Dongyang static noinline int btrfs_ioctl_fitrim(struct file *file, void __user *arg)
367f7039b1dSLi Dongyang {
36854563d41SAl Viro 	struct btrfs_fs_info *fs_info = btrfs_sb(file_inode(file)->i_sb);
369f7039b1dSLi Dongyang 	struct btrfs_device *device;
370f7039b1dSLi Dongyang 	struct request_queue *q;
371f7039b1dSLi Dongyang 	struct fstrim_range range;
372f7039b1dSLi Dongyang 	u64 minlen = ULLONG_MAX;
373f7039b1dSLi Dongyang 	u64 num_devices = 0;
374815745cfSAl Viro 	u64 total_bytes = btrfs_super_total_bytes(fs_info->super_copy);
375f7039b1dSLi Dongyang 	int ret;
376f7039b1dSLi Dongyang 
377f7039b1dSLi Dongyang 	if (!capable(CAP_SYS_ADMIN))
378f7039b1dSLi Dongyang 		return -EPERM;
379f7039b1dSLi Dongyang 
3801f78160cSXiao Guangrong 	rcu_read_lock();
3811f78160cSXiao Guangrong 	list_for_each_entry_rcu(device, &fs_info->fs_devices->devices,
3821f78160cSXiao Guangrong 				dev_list) {
383f7039b1dSLi Dongyang 		if (!device->bdev)
384f7039b1dSLi Dongyang 			continue;
385f7039b1dSLi Dongyang 		q = bdev_get_queue(device->bdev);
386f7039b1dSLi Dongyang 		if (blk_queue_discard(q)) {
387f7039b1dSLi Dongyang 			num_devices++;
388f7039b1dSLi Dongyang 			minlen = min((u64)q->limits.discard_granularity,
389f7039b1dSLi Dongyang 				     minlen);
390f7039b1dSLi Dongyang 		}
391f7039b1dSLi Dongyang 	}
3921f78160cSXiao Guangrong 	rcu_read_unlock();
393f4c697e6SLukas Czerner 
394f7039b1dSLi Dongyang 	if (!num_devices)
395f7039b1dSLi Dongyang 		return -EOPNOTSUPP;
396f7039b1dSLi Dongyang 	if (copy_from_user(&range, arg, sizeof(range)))
397f7039b1dSLi Dongyang 		return -EFAULT;
398e515c18bSLukas Czerner 	if (range.start > total_bytes ||
399e515c18bSLukas Czerner 	    range.len < fs_info->sb->s_blocksize)
400f4c697e6SLukas Czerner 		return -EINVAL;
401f7039b1dSLi Dongyang 
402f4c697e6SLukas Czerner 	range.len = min(range.len, total_bytes - range.start);
403f7039b1dSLi Dongyang 	range.minlen = max(range.minlen, minlen);
404815745cfSAl Viro 	ret = btrfs_trim_fs(fs_info->tree_root, &range);
405f7039b1dSLi Dongyang 	if (ret < 0)
406f7039b1dSLi Dongyang 		return ret;
407f7039b1dSLi Dongyang 
408f7039b1dSLi Dongyang 	if (copy_to_user(arg, &range, sizeof(range)))
409f7039b1dSLi Dongyang 		return -EFAULT;
410f7039b1dSLi Dongyang 
411f7039b1dSLi Dongyang 	return 0;
412f7039b1dSLi Dongyang }
413f7039b1dSLi Dongyang 
414dd5f9615SStefan Behrens int btrfs_is_empty_uuid(u8 *uuid)
415dd5f9615SStefan Behrens {
41646e0f66aSChris Mason 	int i;
41746e0f66aSChris Mason 
41846e0f66aSChris Mason 	for (i = 0; i < BTRFS_UUID_SIZE; i++) {
41946e0f66aSChris Mason 		if (uuid[i])
42046e0f66aSChris Mason 			return 0;
42146e0f66aSChris Mason 	}
42246e0f66aSChris Mason 	return 1;
423dd5f9615SStefan Behrens }
424dd5f9615SStefan Behrens 
425d5c12070SMiao Xie static noinline int create_subvol(struct inode *dir,
426cb8e7090SChristoph Hellwig 				  struct dentry *dentry,
42772fd032eSSage Weil 				  char *name, int namelen,
4286f72c7e2SArne Jansen 				  u64 *async_transid,
4298696c533SMiao Xie 				  struct btrfs_qgroup_inherit *inherit)
430f46b5a66SChristoph Hellwig {
431f46b5a66SChristoph Hellwig 	struct btrfs_trans_handle *trans;
432f46b5a66SChristoph Hellwig 	struct btrfs_key key;
433f46b5a66SChristoph Hellwig 	struct btrfs_root_item root_item;
434f46b5a66SChristoph Hellwig 	struct btrfs_inode_item *inode_item;
435f46b5a66SChristoph Hellwig 	struct extent_buffer *leaf;
436d5c12070SMiao Xie 	struct btrfs_root *root = BTRFS_I(dir)->root;
43776dda93cSYan, Zheng 	struct btrfs_root *new_root;
438d5c12070SMiao Xie 	struct btrfs_block_rsv block_rsv;
4398ea05e3aSAlexander Block 	struct timespec cur_time = CURRENT_TIME;
4405662344bSTsutomu Itoh 	struct inode *inode;
441f46b5a66SChristoph Hellwig 	int ret;
442f46b5a66SChristoph Hellwig 	int err;
443f46b5a66SChristoph Hellwig 	u64 objectid;
444f46b5a66SChristoph Hellwig 	u64 new_dirid = BTRFS_FIRST_FREE_OBJECTID;
4453de4586cSChris Mason 	u64 index = 0;
446d5c12070SMiao Xie 	u64 qgroup_reserved;
4478ea05e3aSAlexander Block 	uuid_le new_uuid;
448f46b5a66SChristoph Hellwig 
449581bb050SLi Zefan 	ret = btrfs_find_free_objectid(root->fs_info->tree_root, &objectid);
4502fbe8c8aSAl Viro 	if (ret)
451a22285a6SYan, Zheng 		return ret;
4526a912213SJosef Bacik 
453d5c12070SMiao Xie 	btrfs_init_block_rsv(&block_rsv, BTRFS_BLOCK_RSV_TEMP);
4549ed74f2dSJosef Bacik 	/*
455d5c12070SMiao Xie 	 * The same as the snapshot creation, please see the comment
456d5c12070SMiao Xie 	 * of create_snapshot().
4579ed74f2dSJosef Bacik 	 */
458d5c12070SMiao Xie 	ret = btrfs_subvolume_reserve_metadata(root, &block_rsv,
459dd5f9615SStefan Behrens 					       8, &qgroup_reserved, false);
460d5c12070SMiao Xie 	if (ret)
461d5c12070SMiao Xie 		return ret;
462f46b5a66SChristoph Hellwig 
463d5c12070SMiao Xie 	trans = btrfs_start_transaction(root, 0);
464d5c12070SMiao Xie 	if (IS_ERR(trans)) {
465d5c12070SMiao Xie 		ret = PTR_ERR(trans);
466de6e8200SLiu Bo 		btrfs_subvolume_release_metadata(root, &block_rsv,
467de6e8200SLiu Bo 						 qgroup_reserved);
468de6e8200SLiu Bo 		return ret;
469d5c12070SMiao Xie 	}
470d5c12070SMiao Xie 	trans->block_rsv = &block_rsv;
471d5c12070SMiao Xie 	trans->bytes_reserved = block_rsv.size;
472f46b5a66SChristoph Hellwig 
4738696c533SMiao Xie 	ret = btrfs_qgroup_inherit(trans, root->fs_info, 0, objectid, inherit);
4746f72c7e2SArne Jansen 	if (ret)
4756f72c7e2SArne Jansen 		goto fail;
4766f72c7e2SArne Jansen 
4775d4f98a2SYan Zheng 	leaf = btrfs_alloc_free_block(trans, root, root->leafsize,
4785581a51aSJan Schmidt 				      0, objectid, NULL, 0, 0, 0);
4798e8a1e31SJosef Bacik 	if (IS_ERR(leaf)) {
4808e8a1e31SJosef Bacik 		ret = PTR_ERR(leaf);
4818e8a1e31SJosef Bacik 		goto fail;
4828e8a1e31SJosef Bacik 	}
483f46b5a66SChristoph Hellwig 
4845d4f98a2SYan Zheng 	memset_extent_buffer(leaf, 0, 0, sizeof(struct btrfs_header));
485f46b5a66SChristoph Hellwig 	btrfs_set_header_bytenr(leaf, leaf->start);
486f46b5a66SChristoph Hellwig 	btrfs_set_header_generation(leaf, trans->transid);
4875d4f98a2SYan Zheng 	btrfs_set_header_backref_rev(leaf, BTRFS_MIXED_BACKREF_REV);
488f46b5a66SChristoph Hellwig 	btrfs_set_header_owner(leaf, objectid);
489f46b5a66SChristoph Hellwig 
4900a4e5586SRoss Kirk 	write_extent_buffer(leaf, root->fs_info->fsid, btrfs_header_fsid(),
491f46b5a66SChristoph Hellwig 			    BTRFS_FSID_SIZE);
4925d4f98a2SYan Zheng 	write_extent_buffer(leaf, root->fs_info->chunk_tree_uuid,
493b308bc2fSGeert Uytterhoeven 			    btrfs_header_chunk_tree_uuid(leaf),
4945d4f98a2SYan Zheng 			    BTRFS_UUID_SIZE);
495f46b5a66SChristoph Hellwig 	btrfs_mark_buffer_dirty(leaf);
496f46b5a66SChristoph Hellwig 
4978ea05e3aSAlexander Block 	memset(&root_item, 0, sizeof(root_item));
4988ea05e3aSAlexander Block 
499f46b5a66SChristoph Hellwig 	inode_item = &root_item.inode;
5003cae210fSQu Wenruo 	btrfs_set_stack_inode_generation(inode_item, 1);
5013cae210fSQu Wenruo 	btrfs_set_stack_inode_size(inode_item, 3);
5023cae210fSQu Wenruo 	btrfs_set_stack_inode_nlink(inode_item, 1);
5033cae210fSQu Wenruo 	btrfs_set_stack_inode_nbytes(inode_item, root->leafsize);
5043cae210fSQu Wenruo 	btrfs_set_stack_inode_mode(inode_item, S_IFDIR | 0755);
505f46b5a66SChristoph Hellwig 
5063cae210fSQu Wenruo 	btrfs_set_root_flags(&root_item, 0);
5073cae210fSQu Wenruo 	btrfs_set_root_limit(&root_item, 0);
5083cae210fSQu Wenruo 	btrfs_set_stack_inode_flags(inode_item, BTRFS_INODE_ROOT_ITEM_INIT);
50908fe4db1SLi Zefan 
510f46b5a66SChristoph Hellwig 	btrfs_set_root_bytenr(&root_item, leaf->start);
51184234f3aSYan Zheng 	btrfs_set_root_generation(&root_item, trans->transid);
512f46b5a66SChristoph Hellwig 	btrfs_set_root_level(&root_item, 0);
513f46b5a66SChristoph Hellwig 	btrfs_set_root_refs(&root_item, 1);
51486b9f2ecSYan, Zheng 	btrfs_set_root_used(&root_item, leaf->len);
51580ff3856SYan Zheng 	btrfs_set_root_last_snapshot(&root_item, 0);
516f46b5a66SChristoph Hellwig 
5178ea05e3aSAlexander Block 	btrfs_set_root_generation_v2(&root_item,
5188ea05e3aSAlexander Block 			btrfs_root_generation(&root_item));
5198ea05e3aSAlexander Block 	uuid_le_gen(&new_uuid);
5208ea05e3aSAlexander Block 	memcpy(root_item.uuid, new_uuid.b, BTRFS_UUID_SIZE);
5213cae210fSQu Wenruo 	btrfs_set_stack_timespec_sec(&root_item.otime, cur_time.tv_sec);
5223cae210fSQu Wenruo 	btrfs_set_stack_timespec_nsec(&root_item.otime, cur_time.tv_nsec);
5238ea05e3aSAlexander Block 	root_item.ctime = root_item.otime;
5248ea05e3aSAlexander Block 	btrfs_set_root_ctransid(&root_item, trans->transid);
5258ea05e3aSAlexander Block 	btrfs_set_root_otransid(&root_item, trans->transid);
526f46b5a66SChristoph Hellwig 
527925baeddSChris Mason 	btrfs_tree_unlock(leaf);
528f46b5a66SChristoph Hellwig 	free_extent_buffer(leaf);
529f46b5a66SChristoph Hellwig 	leaf = NULL;
530f46b5a66SChristoph Hellwig 
531f46b5a66SChristoph Hellwig 	btrfs_set_root_dirid(&root_item, new_dirid);
532f46b5a66SChristoph Hellwig 
533f46b5a66SChristoph Hellwig 	key.objectid = objectid;
5345d4f98a2SYan Zheng 	key.offset = 0;
535f46b5a66SChristoph Hellwig 	btrfs_set_key_type(&key, BTRFS_ROOT_ITEM_KEY);
536f46b5a66SChristoph Hellwig 	ret = btrfs_insert_root(trans, root->fs_info->tree_root, &key,
537f46b5a66SChristoph Hellwig 				&root_item);
538f46b5a66SChristoph Hellwig 	if (ret)
539f46b5a66SChristoph Hellwig 		goto fail;
540f46b5a66SChristoph Hellwig 
54176dda93cSYan, Zheng 	key.offset = (u64)-1;
54276dda93cSYan, Zheng 	new_root = btrfs_read_fs_root_no_name(root->fs_info, &key);
54379787eaaSJeff Mahoney 	if (IS_ERR(new_root)) {
54479787eaaSJeff Mahoney 		btrfs_abort_transaction(trans, root, PTR_ERR(new_root));
54579787eaaSJeff Mahoney 		ret = PTR_ERR(new_root);
54679787eaaSJeff Mahoney 		goto fail;
54779787eaaSJeff Mahoney 	}
54876dda93cSYan, Zheng 
54976dda93cSYan, Zheng 	btrfs_record_root_in_trans(trans, new_root);
55076dda93cSYan, Zheng 
55163541927SFilipe David Borba Manana 	ret = btrfs_create_subvol_root(trans, new_root, root, new_dirid);
552ce598979SMark Fasheh 	if (ret) {
553ce598979SMark Fasheh 		/* We potentially lose an unused inode item here */
55479787eaaSJeff Mahoney 		btrfs_abort_transaction(trans, root, ret);
555ce598979SMark Fasheh 		goto fail;
556ce598979SMark Fasheh 	}
557ce598979SMark Fasheh 
558f46b5a66SChristoph Hellwig 	/*
559f46b5a66SChristoph Hellwig 	 * insert the directory item
560f46b5a66SChristoph Hellwig 	 */
5613de4586cSChris Mason 	ret = btrfs_set_inode_index(dir, &index);
56279787eaaSJeff Mahoney 	if (ret) {
56379787eaaSJeff Mahoney 		btrfs_abort_transaction(trans, root, ret);
56479787eaaSJeff Mahoney 		goto fail;
56579787eaaSJeff Mahoney 	}
5663de4586cSChris Mason 
5673de4586cSChris Mason 	ret = btrfs_insert_dir_item(trans, root,
56816cdcec7SMiao Xie 				    name, namelen, dir, &key,
5693de4586cSChris Mason 				    BTRFS_FT_DIR, index);
57079787eaaSJeff Mahoney 	if (ret) {
57179787eaaSJeff Mahoney 		btrfs_abort_transaction(trans, root, ret);
572f46b5a66SChristoph Hellwig 		goto fail;
57379787eaaSJeff Mahoney 	}
5740660b5afSChris Mason 
57552c26179SYan Zheng 	btrfs_i_size_write(dir, dir->i_size + namelen * 2);
57652c26179SYan Zheng 	ret = btrfs_update_inode(trans, root, dir);
57752c26179SYan Zheng 	BUG_ON(ret);
57852c26179SYan Zheng 
5790660b5afSChris Mason 	ret = btrfs_add_root_ref(trans, root->fs_info->tree_root,
5804df27c4dSYan, Zheng 				 objectid, root->root_key.objectid,
58133345d01SLi Zefan 				 btrfs_ino(dir), index, name, namelen);
5820660b5afSChris Mason 	BUG_ON(ret);
5830660b5afSChris Mason 
584dd5f9615SStefan Behrens 	ret = btrfs_uuid_tree_add(trans, root->fs_info->uuid_root,
585dd5f9615SStefan Behrens 				  root_item.uuid, BTRFS_UUID_KEY_SUBVOL,
586dd5f9615SStefan Behrens 				  objectid);
587dd5f9615SStefan Behrens 	if (ret)
588dd5f9615SStefan Behrens 		btrfs_abort_transaction(trans, root, ret);
589dd5f9615SStefan Behrens 
590f46b5a66SChristoph Hellwig fail:
591d5c12070SMiao Xie 	trans->block_rsv = NULL;
592d5c12070SMiao Xie 	trans->bytes_reserved = 0;
593de6e8200SLiu Bo 	btrfs_subvolume_release_metadata(root, &block_rsv, qgroup_reserved);
594de6e8200SLiu Bo 
59572fd032eSSage Weil 	if (async_transid) {
59672fd032eSSage Weil 		*async_transid = trans->transid;
59772fd032eSSage Weil 		err = btrfs_commit_transaction_async(trans, root, 1);
59800d71c9cSMiao Xie 		if (err)
59900d71c9cSMiao Xie 			err = btrfs_commit_transaction(trans, root);
60072fd032eSSage Weil 	} else {
60176dda93cSYan, Zheng 		err = btrfs_commit_transaction(trans, root);
60272fd032eSSage Weil 	}
603f46b5a66SChristoph Hellwig 	if (err && !ret)
604f46b5a66SChristoph Hellwig 		ret = err;
6051a65e24bSChris Mason 
6065662344bSTsutomu Itoh 	if (!ret) {
6075662344bSTsutomu Itoh 		inode = btrfs_lookup_dentry(dir, dentry);
608de6e8200SLiu Bo 		if (IS_ERR(inode))
609de6e8200SLiu Bo 			return PTR_ERR(inode);
6105662344bSTsutomu Itoh 		d_instantiate(dentry, inode);
6115662344bSTsutomu Itoh 	}
612f46b5a66SChristoph Hellwig 	return ret;
613f46b5a66SChristoph Hellwig }
614f46b5a66SChristoph Hellwig 
6158257b2dcSMiao Xie static void btrfs_wait_nocow_write(struct btrfs_root *root)
6168257b2dcSMiao Xie {
6178257b2dcSMiao Xie 	s64 writers;
6188257b2dcSMiao Xie 	DEFINE_WAIT(wait);
6198257b2dcSMiao Xie 
6208257b2dcSMiao Xie 	do {
6218257b2dcSMiao Xie 		prepare_to_wait(&root->subv_writers->wait, &wait,
6228257b2dcSMiao Xie 				TASK_UNINTERRUPTIBLE);
6238257b2dcSMiao Xie 
6248257b2dcSMiao Xie 		writers = percpu_counter_sum(&root->subv_writers->counter);
6258257b2dcSMiao Xie 		if (writers)
6268257b2dcSMiao Xie 			schedule();
6278257b2dcSMiao Xie 
6288257b2dcSMiao Xie 		finish_wait(&root->subv_writers->wait, &wait);
6298257b2dcSMiao Xie 	} while (writers);
6308257b2dcSMiao Xie }
6318257b2dcSMiao Xie 
632e9662f70SMiao Xie static int create_snapshot(struct btrfs_root *root, struct inode *dir,
633e9662f70SMiao Xie 			   struct dentry *dentry, char *name, int namelen,
634e9662f70SMiao Xie 			   u64 *async_transid, bool readonly,
635e9662f70SMiao Xie 			   struct btrfs_qgroup_inherit *inherit)
636f46b5a66SChristoph Hellwig {
6372e4bfab9SYan, Zheng 	struct inode *inode;
638f46b5a66SChristoph Hellwig 	struct btrfs_pending_snapshot *pending_snapshot;
639f46b5a66SChristoph Hellwig 	struct btrfs_trans_handle *trans;
6402e4bfab9SYan, Zheng 	int ret;
641f46b5a66SChristoph Hellwig 
64227cdeb70SMiao Xie 	if (!test_bit(BTRFS_ROOT_REF_COWS, &root->state))
643f46b5a66SChristoph Hellwig 		return -EINVAL;
644f46b5a66SChristoph Hellwig 
6458257b2dcSMiao Xie 	atomic_inc(&root->will_be_snapshoted);
6468257b2dcSMiao Xie 	smp_mb__after_atomic_inc();
6478257b2dcSMiao Xie 	btrfs_wait_nocow_write(root);
6488257b2dcSMiao Xie 
6496a03843dSMiao Xie 	ret = btrfs_start_delalloc_inodes(root, 0);
6506a03843dSMiao Xie 	if (ret)
6518257b2dcSMiao Xie 		goto out;
6526a03843dSMiao Xie 
653b0244199SMiao Xie 	btrfs_wait_ordered_extents(root, -1);
6546a03843dSMiao Xie 
655a22285a6SYan, Zheng 	pending_snapshot = kzalloc(sizeof(*pending_snapshot), GFP_NOFS);
6568257b2dcSMiao Xie 	if (!pending_snapshot) {
6578257b2dcSMiao Xie 		ret = -ENOMEM;
6588257b2dcSMiao Xie 		goto out;
6598257b2dcSMiao Xie 	}
660a22285a6SYan, Zheng 
66166d8f3ddSMiao Xie 	btrfs_init_block_rsv(&pending_snapshot->block_rsv,
66266d8f3ddSMiao Xie 			     BTRFS_BLOCK_RSV_TEMP);
663d5c12070SMiao Xie 	/*
664d5c12070SMiao Xie 	 * 1 - parent dir inode
665d5c12070SMiao Xie 	 * 2 - dir entries
666d5c12070SMiao Xie 	 * 1 - root item
667d5c12070SMiao Xie 	 * 2 - root ref/backref
668d5c12070SMiao Xie 	 * 1 - root of snapshot
669dd5f9615SStefan Behrens 	 * 1 - UUID item
670d5c12070SMiao Xie 	 */
671d5c12070SMiao Xie 	ret = btrfs_subvolume_reserve_metadata(BTRFS_I(dir)->root,
672dd5f9615SStefan Behrens 					&pending_snapshot->block_rsv, 8,
673ee3441b4SJeff Mahoney 					&pending_snapshot->qgroup_reserved,
674ee3441b4SJeff Mahoney 					false);
675d5c12070SMiao Xie 	if (ret)
6768257b2dcSMiao Xie 		goto free;
677d5c12070SMiao Xie 
678a22285a6SYan, Zheng 	pending_snapshot->dentry = dentry;
679a22285a6SYan, Zheng 	pending_snapshot->root = root;
680b83cc969SLi Zefan 	pending_snapshot->readonly = readonly;
681e9662f70SMiao Xie 	pending_snapshot->dir = dir;
6828696c533SMiao Xie 	pending_snapshot->inherit = inherit;
683a22285a6SYan, Zheng 
684d5c12070SMiao Xie 	trans = btrfs_start_transaction(root, 0);
685a22285a6SYan, Zheng 	if (IS_ERR(trans)) {
686a22285a6SYan, Zheng 		ret = PTR_ERR(trans);
687a22285a6SYan, Zheng 		goto fail;
688a22285a6SYan, Zheng 	}
689a22285a6SYan, Zheng 
6908351583eSJosef Bacik 	spin_lock(&root->fs_info->trans_lock);
691a22285a6SYan, Zheng 	list_add(&pending_snapshot->list,
692a22285a6SYan, Zheng 		 &trans->transaction->pending_snapshots);
6938351583eSJosef Bacik 	spin_unlock(&root->fs_info->trans_lock);
69472fd032eSSage Weil 	if (async_transid) {
69572fd032eSSage Weil 		*async_transid = trans->transid;
69672fd032eSSage Weil 		ret = btrfs_commit_transaction_async(trans,
69772fd032eSSage Weil 				     root->fs_info->extent_root, 1);
69800d71c9cSMiao Xie 		if (ret)
69900d71c9cSMiao Xie 			ret = btrfs_commit_transaction(trans, root);
70072fd032eSSage Weil 	} else {
70172fd032eSSage Weil 		ret = btrfs_commit_transaction(trans,
70272fd032eSSage Weil 					       root->fs_info->extent_root);
70372fd032eSSage Weil 	}
704aec8030aSMiao Xie 	if (ret)
705c37b2b62SJosef Bacik 		goto fail;
706a22285a6SYan, Zheng 
707a22285a6SYan, Zheng 	ret = pending_snapshot->error;
708f46b5a66SChristoph Hellwig 	if (ret)
7092e4bfab9SYan, Zheng 		goto fail;
710f46b5a66SChristoph Hellwig 
71166b4ffd1SJosef Bacik 	ret = btrfs_orphan_cleanup(pending_snapshot->snap);
71266b4ffd1SJosef Bacik 	if (ret)
71366b4ffd1SJosef Bacik 		goto fail;
714f46b5a66SChristoph Hellwig 
7153821f348SFilipe Manana 	/*
7163821f348SFilipe Manana 	 * If orphan cleanup did remove any orphans, it means the tree was
7173821f348SFilipe Manana 	 * modified and therefore the commit root is not the same as the
7183821f348SFilipe Manana 	 * current root anymore. This is a problem, because send uses the
7193821f348SFilipe Manana 	 * commit root and therefore can see inode items that don't exist
7203821f348SFilipe Manana 	 * in the current root anymore, and for example make calls to
7213821f348SFilipe Manana 	 * btrfs_iget, which will do tree lookups based on the current root
7223821f348SFilipe Manana 	 * and not on the commit root. Those lookups will fail, returning a
7233821f348SFilipe Manana 	 * -ESTALE error, and making send fail with that error. So make sure
7243821f348SFilipe Manana 	 * a send does not see any orphans we have just removed, and that it
7253821f348SFilipe Manana 	 * will see the same inodes regardless of whether a transaction
7263821f348SFilipe Manana 	 * commit happened before it started (meaning that the commit root
7273821f348SFilipe Manana 	 * will be the same as the current root) or not.
7283821f348SFilipe Manana 	 */
7293821f348SFilipe Manana 	if (readonly && pending_snapshot->snap->node !=
7303821f348SFilipe Manana 	    pending_snapshot->snap->commit_root) {
7313821f348SFilipe Manana 		trans = btrfs_join_transaction(pending_snapshot->snap);
7323821f348SFilipe Manana 		if (IS_ERR(trans) && PTR_ERR(trans) != -ENOENT) {
7333821f348SFilipe Manana 			ret = PTR_ERR(trans);
7343821f348SFilipe Manana 			goto fail;
7353821f348SFilipe Manana 		}
7363821f348SFilipe Manana 		if (!IS_ERR(trans)) {
7373821f348SFilipe Manana 			ret = btrfs_commit_transaction(trans,
7383821f348SFilipe Manana 						       pending_snapshot->snap);
7393821f348SFilipe Manana 			if (ret)
7403821f348SFilipe Manana 				goto fail;
7413821f348SFilipe Manana 		}
7423821f348SFilipe Manana 	}
7433821f348SFilipe Manana 
7442fbe8c8aSAl Viro 	inode = btrfs_lookup_dentry(dentry->d_parent->d_inode, dentry);
7452e4bfab9SYan, Zheng 	if (IS_ERR(inode)) {
7462e4bfab9SYan, Zheng 		ret = PTR_ERR(inode);
7472e4bfab9SYan, Zheng 		goto fail;
7482e4bfab9SYan, Zheng 	}
7495662344bSTsutomu Itoh 
7502e4bfab9SYan, Zheng 	d_instantiate(dentry, inode);
7512e4bfab9SYan, Zheng 	ret = 0;
7522e4bfab9SYan, Zheng fail:
753d5c12070SMiao Xie 	btrfs_subvolume_release_metadata(BTRFS_I(dir)->root,
754d5c12070SMiao Xie 					 &pending_snapshot->block_rsv,
755d5c12070SMiao Xie 					 pending_snapshot->qgroup_reserved);
7568257b2dcSMiao Xie free:
757a22285a6SYan, Zheng 	kfree(pending_snapshot);
7588257b2dcSMiao Xie out:
7598257b2dcSMiao Xie 	atomic_dec(&root->will_be_snapshoted);
760f46b5a66SChristoph Hellwig 	return ret;
761f46b5a66SChristoph Hellwig }
762f46b5a66SChristoph Hellwig 
7634260f7c7SSage Weil /*  copy of check_sticky in fs/namei.c()
7644260f7c7SSage Weil * It's inline, so penalty for filesystems that don't use sticky bit is
7654260f7c7SSage Weil * minimal.
7664260f7c7SSage Weil */
7674260f7c7SSage Weil static inline int btrfs_check_sticky(struct inode *dir, struct inode *inode)
7684260f7c7SSage Weil {
7692f2f43d3SEric W. Biederman 	kuid_t fsuid = current_fsuid();
7704260f7c7SSage Weil 
7714260f7c7SSage Weil 	if (!(dir->i_mode & S_ISVTX))
7724260f7c7SSage Weil 		return 0;
7732f2f43d3SEric W. Biederman 	if (uid_eq(inode->i_uid, fsuid))
7744260f7c7SSage Weil 		return 0;
7752f2f43d3SEric W. Biederman 	if (uid_eq(dir->i_uid, fsuid))
7764260f7c7SSage Weil 		return 0;
7774260f7c7SSage Weil 	return !capable(CAP_FOWNER);
7784260f7c7SSage Weil }
7794260f7c7SSage Weil 
7804260f7c7SSage Weil /*  copy of may_delete in fs/namei.c()
7814260f7c7SSage Weil  *	Check whether we can remove a link victim from directory dir, check
7824260f7c7SSage Weil  *  whether the type of victim is right.
7834260f7c7SSage Weil  *  1. We can't do it if dir is read-only (done in permission())
7844260f7c7SSage Weil  *  2. We should have write and exec permissions on dir
7854260f7c7SSage Weil  *  3. We can't remove anything from append-only dir
7864260f7c7SSage Weil  *  4. We can't do anything with immutable dir (done in permission())
7874260f7c7SSage Weil  *  5. If the sticky bit on dir is set we should either
7884260f7c7SSage Weil  *	a. be owner of dir, or
7894260f7c7SSage Weil  *	b. be owner of victim, or
7904260f7c7SSage Weil  *	c. have CAP_FOWNER capability
7914260f7c7SSage Weil  *  6. If the victim is append-only or immutable we can't do antyhing with
7924260f7c7SSage Weil  *     links pointing to it.
7934260f7c7SSage Weil  *  7. If we were asked to remove a directory and victim isn't one - ENOTDIR.
7944260f7c7SSage Weil  *  8. If we were asked to remove a non-directory and victim isn't one - EISDIR.
7954260f7c7SSage Weil  *  9. We can't remove a root or mountpoint.
7964260f7c7SSage Weil  * 10. We don't allow removal of NFS sillyrenamed files; it's handled by
7974260f7c7SSage Weil  *     nfs_async_unlink().
7984260f7c7SSage Weil  */
7994260f7c7SSage Weil 
8004260f7c7SSage Weil static int btrfs_may_delete(struct inode *dir, struct dentry *victim, int isdir)
8014260f7c7SSage Weil {
8024260f7c7SSage Weil 	int error;
8034260f7c7SSage Weil 
8044260f7c7SSage Weil 	if (!victim->d_inode)
8054260f7c7SSage Weil 		return -ENOENT;
8064260f7c7SSage Weil 
8074260f7c7SSage Weil 	BUG_ON(victim->d_parent->d_inode != dir);
8084fa6b5ecSJeff Layton 	audit_inode_child(dir, victim, AUDIT_TYPE_CHILD_DELETE);
8094260f7c7SSage Weil 
8104260f7c7SSage Weil 	error = inode_permission(dir, MAY_WRITE | MAY_EXEC);
8114260f7c7SSage Weil 	if (error)
8124260f7c7SSage Weil 		return error;
8134260f7c7SSage Weil 	if (IS_APPEND(dir))
8144260f7c7SSage Weil 		return -EPERM;
8154260f7c7SSage Weil 	if (btrfs_check_sticky(dir, victim->d_inode)||
8164260f7c7SSage Weil 		IS_APPEND(victim->d_inode)||
8174260f7c7SSage Weil 	    IS_IMMUTABLE(victim->d_inode) || IS_SWAPFILE(victim->d_inode))
8184260f7c7SSage Weil 		return -EPERM;
8194260f7c7SSage Weil 	if (isdir) {
8204260f7c7SSage Weil 		if (!S_ISDIR(victim->d_inode->i_mode))
8214260f7c7SSage Weil 			return -ENOTDIR;
8224260f7c7SSage Weil 		if (IS_ROOT(victim))
8234260f7c7SSage Weil 			return -EBUSY;
8244260f7c7SSage Weil 	} else if (S_ISDIR(victim->d_inode->i_mode))
8254260f7c7SSage Weil 		return -EISDIR;
8264260f7c7SSage Weil 	if (IS_DEADDIR(dir))
8274260f7c7SSage Weil 		return -ENOENT;
8284260f7c7SSage Weil 	if (victim->d_flags & DCACHE_NFSFS_RENAMED)
8294260f7c7SSage Weil 		return -EBUSY;
8304260f7c7SSage Weil 	return 0;
8314260f7c7SSage Weil }
8324260f7c7SSage Weil 
833cb8e7090SChristoph Hellwig /* copy of may_create in fs/namei.c() */
834cb8e7090SChristoph Hellwig static inline int btrfs_may_create(struct inode *dir, struct dentry *child)
835cb8e7090SChristoph Hellwig {
836cb8e7090SChristoph Hellwig 	if (child->d_inode)
837cb8e7090SChristoph Hellwig 		return -EEXIST;
838cb8e7090SChristoph Hellwig 	if (IS_DEADDIR(dir))
839cb8e7090SChristoph Hellwig 		return -ENOENT;
840cb8e7090SChristoph Hellwig 	return inode_permission(dir, MAY_WRITE | MAY_EXEC);
841cb8e7090SChristoph Hellwig }
842cb8e7090SChristoph Hellwig 
843cb8e7090SChristoph Hellwig /*
844cb8e7090SChristoph Hellwig  * Create a new subvolume below @parent.  This is largely modeled after
845cb8e7090SChristoph Hellwig  * sys_mkdirat and vfs_mkdir, but we only do a single component lookup
846cb8e7090SChristoph Hellwig  * inside this filesystem so it's quite a bit simpler.
847cb8e7090SChristoph Hellwig  */
84876dda93cSYan, Zheng static noinline int btrfs_mksubvol(struct path *parent,
84976dda93cSYan, Zheng 				   char *name, int namelen,
85072fd032eSSage Weil 				   struct btrfs_root *snap_src,
8516f72c7e2SArne Jansen 				   u64 *async_transid, bool readonly,
8528696c533SMiao Xie 				   struct btrfs_qgroup_inherit *inherit)
853cb8e7090SChristoph Hellwig {
85476dda93cSYan, Zheng 	struct inode *dir  = parent->dentry->d_inode;
855cb8e7090SChristoph Hellwig 	struct dentry *dentry;
856cb8e7090SChristoph Hellwig 	int error;
857cb8e7090SChristoph Hellwig 
8585c50c9b8SDavid Sterba 	error = mutex_lock_killable_nested(&dir->i_mutex, I_MUTEX_PARENT);
8595c50c9b8SDavid Sterba 	if (error == -EINTR)
8605c50c9b8SDavid Sterba 		return error;
861cb8e7090SChristoph Hellwig 
862cb8e7090SChristoph Hellwig 	dentry = lookup_one_len(name, parent->dentry, namelen);
863cb8e7090SChristoph Hellwig 	error = PTR_ERR(dentry);
864cb8e7090SChristoph Hellwig 	if (IS_ERR(dentry))
865cb8e7090SChristoph Hellwig 		goto out_unlock;
866cb8e7090SChristoph Hellwig 
867cb8e7090SChristoph Hellwig 	error = -EEXIST;
868cb8e7090SChristoph Hellwig 	if (dentry->d_inode)
869cb8e7090SChristoph Hellwig 		goto out_dput;
870cb8e7090SChristoph Hellwig 
87176dda93cSYan, Zheng 	error = btrfs_may_create(dir, dentry);
872cb8e7090SChristoph Hellwig 	if (error)
873a874a63eSLiu Bo 		goto out_dput;
874cb8e7090SChristoph Hellwig 
8759c52057cSChris Mason 	/*
8769c52057cSChris Mason 	 * even if this name doesn't exist, we may get hash collisions.
8779c52057cSChris Mason 	 * check for them now when we can safely fail
8789c52057cSChris Mason 	 */
8799c52057cSChris Mason 	error = btrfs_check_dir_item_collision(BTRFS_I(dir)->root,
8809c52057cSChris Mason 					       dir->i_ino, name,
8819c52057cSChris Mason 					       namelen);
8829c52057cSChris Mason 	if (error)
8839c52057cSChris Mason 		goto out_dput;
8849c52057cSChris Mason 
88576dda93cSYan, Zheng 	down_read(&BTRFS_I(dir)->root->fs_info->subvol_sem);
88676dda93cSYan, Zheng 
88776dda93cSYan, Zheng 	if (btrfs_root_refs(&BTRFS_I(dir)->root->root_item) == 0)
88876dda93cSYan, Zheng 		goto out_up_read;
88976dda93cSYan, Zheng 
8903de4586cSChris Mason 	if (snap_src) {
891e9662f70SMiao Xie 		error = create_snapshot(snap_src, dir, dentry, name, namelen,
8926f72c7e2SArne Jansen 					async_transid, readonly, inherit);
8933de4586cSChris Mason 	} else {
894d5c12070SMiao Xie 		error = create_subvol(dir, dentry, name, namelen,
895d5c12070SMiao Xie 				      async_transid, inherit);
8963de4586cSChris Mason 	}
89776dda93cSYan, Zheng 	if (!error)
89876dda93cSYan, Zheng 		fsnotify_mkdir(dir, dentry);
89976dda93cSYan, Zheng out_up_read:
90076dda93cSYan, Zheng 	up_read(&BTRFS_I(dir)->root->fs_info->subvol_sem);
901cb8e7090SChristoph Hellwig out_dput:
902cb8e7090SChristoph Hellwig 	dput(dentry);
903cb8e7090SChristoph Hellwig out_unlock:
90476dda93cSYan, Zheng 	mutex_unlock(&dir->i_mutex);
905cb8e7090SChristoph Hellwig 	return error;
906cb8e7090SChristoph Hellwig }
907cb8e7090SChristoph Hellwig 
9084cb5300bSChris Mason /*
9094cb5300bSChris Mason  * When we're defragging a range, we don't want to kick it off again
9104cb5300bSChris Mason  * if it is really just waiting for delalloc to send it down.
9114cb5300bSChris Mason  * If we find a nice big extent or delalloc range for the bytes in the
9124cb5300bSChris Mason  * file you want to defrag, we return 0 to let you know to skip this
9134cb5300bSChris Mason  * part of the file
9144cb5300bSChris Mason  */
9154cb5300bSChris Mason static int check_defrag_in_cache(struct inode *inode, u64 offset, int thresh)
9164cb5300bSChris Mason {
9174cb5300bSChris Mason 	struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
9184cb5300bSChris Mason 	struct extent_map *em = NULL;
9194cb5300bSChris Mason 	struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
9204cb5300bSChris Mason 	u64 end;
9214cb5300bSChris Mason 
9224cb5300bSChris Mason 	read_lock(&em_tree->lock);
9234cb5300bSChris Mason 	em = lookup_extent_mapping(em_tree, offset, PAGE_CACHE_SIZE);
9244cb5300bSChris Mason 	read_unlock(&em_tree->lock);
9254cb5300bSChris Mason 
9264cb5300bSChris Mason 	if (em) {
9274cb5300bSChris Mason 		end = extent_map_end(em);
9284cb5300bSChris Mason 		free_extent_map(em);
9294cb5300bSChris Mason 		if (end - offset > thresh)
9304cb5300bSChris Mason 			return 0;
9314cb5300bSChris Mason 	}
9324cb5300bSChris Mason 	/* if we already have a nice delalloc here, just stop */
9334cb5300bSChris Mason 	thresh /= 2;
9344cb5300bSChris Mason 	end = count_range_bits(io_tree, &offset, offset + thresh,
9354cb5300bSChris Mason 			       thresh, EXTENT_DELALLOC, 1);
9364cb5300bSChris Mason 	if (end >= thresh)
9374cb5300bSChris Mason 		return 0;
9384cb5300bSChris Mason 	return 1;
9394cb5300bSChris Mason }
9404cb5300bSChris Mason 
9414cb5300bSChris Mason /*
9424cb5300bSChris Mason  * helper function to walk through a file and find extents
9434cb5300bSChris Mason  * newer than a specific transid, and smaller than thresh.
9444cb5300bSChris Mason  *
9454cb5300bSChris Mason  * This is used by the defragging code to find new and small
9464cb5300bSChris Mason  * extents
9474cb5300bSChris Mason  */
9484cb5300bSChris Mason static int find_new_extents(struct btrfs_root *root,
9494cb5300bSChris Mason 			    struct inode *inode, u64 newer_than,
9504cb5300bSChris Mason 			    u64 *off, int thresh)
9514cb5300bSChris Mason {
9524cb5300bSChris Mason 	struct btrfs_path *path;
9534cb5300bSChris Mason 	struct btrfs_key min_key;
9544cb5300bSChris Mason 	struct extent_buffer *leaf;
9554cb5300bSChris Mason 	struct btrfs_file_extent_item *extent;
9564cb5300bSChris Mason 	int type;
9574cb5300bSChris Mason 	int ret;
958a4689d2bSDavid Sterba 	u64 ino = btrfs_ino(inode);
9594cb5300bSChris Mason 
9604cb5300bSChris Mason 	path = btrfs_alloc_path();
9614cb5300bSChris Mason 	if (!path)
9624cb5300bSChris Mason 		return -ENOMEM;
9634cb5300bSChris Mason 
964a4689d2bSDavid Sterba 	min_key.objectid = ino;
9654cb5300bSChris Mason 	min_key.type = BTRFS_EXTENT_DATA_KEY;
9664cb5300bSChris Mason 	min_key.offset = *off;
9674cb5300bSChris Mason 
9684cb5300bSChris Mason 	while (1) {
969f094c9bdSFilipe Manana 		path->keep_locks = 1;
9706174d3cbSFilipe David Borba Manana 		ret = btrfs_search_forward(root, &min_key, path, newer_than);
9714cb5300bSChris Mason 		if (ret != 0)
9724cb5300bSChris Mason 			goto none;
973f094c9bdSFilipe Manana 		path->keep_locks = 0;
974f094c9bdSFilipe Manana 		btrfs_unlock_up_safe(path, 1);
975f094c9bdSFilipe Manana process_slot:
976a4689d2bSDavid Sterba 		if (min_key.objectid != ino)
9774cb5300bSChris Mason 			goto none;
9784cb5300bSChris Mason 		if (min_key.type != BTRFS_EXTENT_DATA_KEY)
9794cb5300bSChris Mason 			goto none;
9804cb5300bSChris Mason 
9814cb5300bSChris Mason 		leaf = path->nodes[0];
9824cb5300bSChris Mason 		extent = btrfs_item_ptr(leaf, path->slots[0],
9834cb5300bSChris Mason 					struct btrfs_file_extent_item);
9844cb5300bSChris Mason 
9854cb5300bSChris Mason 		type = btrfs_file_extent_type(leaf, extent);
9864cb5300bSChris Mason 		if (type == BTRFS_FILE_EXTENT_REG &&
9874cb5300bSChris Mason 		    btrfs_file_extent_num_bytes(leaf, extent) < thresh &&
9884cb5300bSChris Mason 		    check_defrag_in_cache(inode, min_key.offset, thresh)) {
9894cb5300bSChris Mason 			*off = min_key.offset;
9904cb5300bSChris Mason 			btrfs_free_path(path);
9914cb5300bSChris Mason 			return 0;
9924cb5300bSChris Mason 		}
9934cb5300bSChris Mason 
994f094c9bdSFilipe Manana 		path->slots[0]++;
995f094c9bdSFilipe Manana 		if (path->slots[0] < btrfs_header_nritems(leaf)) {
996f094c9bdSFilipe Manana 			btrfs_item_key_to_cpu(leaf, &min_key, path->slots[0]);
997f094c9bdSFilipe Manana 			goto process_slot;
998f094c9bdSFilipe Manana 		}
999f094c9bdSFilipe Manana 
10004cb5300bSChris Mason 		if (min_key.offset == (u64)-1)
10014cb5300bSChris Mason 			goto none;
10024cb5300bSChris Mason 
10034cb5300bSChris Mason 		min_key.offset++;
10044cb5300bSChris Mason 		btrfs_release_path(path);
10054cb5300bSChris Mason 	}
10064cb5300bSChris Mason none:
10074cb5300bSChris Mason 	btrfs_free_path(path);
10084cb5300bSChris Mason 	return -ENOENT;
10094cb5300bSChris Mason }
10104cb5300bSChris Mason 
10116c282eb4SLi Zefan static struct extent_map *defrag_lookup_extent(struct inode *inode, u64 start)
101217ce6ef8SLiu Bo {
101317ce6ef8SLiu Bo 	struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
1014940100a4SChris Mason 	struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
10156c282eb4SLi Zefan 	struct extent_map *em;
10166c282eb4SLi Zefan 	u64 len = PAGE_CACHE_SIZE;
1017940100a4SChris Mason 
1018940100a4SChris Mason 	/*
1019940100a4SChris Mason 	 * hopefully we have this extent in the tree already, try without
1020940100a4SChris Mason 	 * the full extent lock
1021940100a4SChris Mason 	 */
1022940100a4SChris Mason 	read_lock(&em_tree->lock);
1023940100a4SChris Mason 	em = lookup_extent_mapping(em_tree, start, len);
1024940100a4SChris Mason 	read_unlock(&em_tree->lock);
1025940100a4SChris Mason 
1026940100a4SChris Mason 	if (!em) {
1027308d9800SFilipe Manana 		struct extent_state *cached = NULL;
1028308d9800SFilipe Manana 		u64 end = start + len - 1;
1029308d9800SFilipe Manana 
1030940100a4SChris Mason 		/* get the big lock and read metadata off disk */
1031308d9800SFilipe Manana 		lock_extent_bits(io_tree, start, end, 0, &cached);
1032940100a4SChris Mason 		em = btrfs_get_extent(inode, NULL, 0, start, len, 0);
1033308d9800SFilipe Manana 		unlock_extent_cached(io_tree, start, end, &cached, GFP_NOFS);
1034940100a4SChris Mason 
10356cf8bfbfSDan Carpenter 		if (IS_ERR(em))
10366c282eb4SLi Zefan 			return NULL;
1037940100a4SChris Mason 	}
1038940100a4SChris Mason 
10396c282eb4SLi Zefan 	return em;
10406c282eb4SLi Zefan }
10416c282eb4SLi Zefan 
10426c282eb4SLi Zefan static bool defrag_check_next_extent(struct inode *inode, struct extent_map *em)
10436c282eb4SLi Zefan {
10446c282eb4SLi Zefan 	struct extent_map *next;
10456c282eb4SLi Zefan 	bool ret = true;
10466c282eb4SLi Zefan 
10476c282eb4SLi Zefan 	/* this is the last extent */
10486c282eb4SLi Zefan 	if (em->start + em->len >= i_size_read(inode))
10496c282eb4SLi Zefan 		return false;
10506c282eb4SLi Zefan 
10516c282eb4SLi Zefan 	next = defrag_lookup_extent(inode, em->start + em->len);
1052e2127cf0SFilipe Manana 	if (!next || next->block_start >= EXTENT_MAP_LAST_BYTE ||
1053e2127cf0SFilipe Manana 	    (em->block_start + em->block_len == next->block_start))
10546c282eb4SLi Zefan 		ret = false;
10556c282eb4SLi Zefan 
10566c282eb4SLi Zefan 	free_extent_map(next);
10576c282eb4SLi Zefan 	return ret;
10586c282eb4SLi Zefan }
10596c282eb4SLi Zefan 
10606c282eb4SLi Zefan static int should_defrag_range(struct inode *inode, u64 start, int thresh,
1061a43a2111SAndrew Mahone 			       u64 *last_len, u64 *skip, u64 *defrag_end,
1062a43a2111SAndrew Mahone 			       int compress)
10636c282eb4SLi Zefan {
10646c282eb4SLi Zefan 	struct extent_map *em;
10656c282eb4SLi Zefan 	int ret = 1;
10666c282eb4SLi Zefan 	bool next_mergeable = true;
10676c282eb4SLi Zefan 
10686c282eb4SLi Zefan 	/*
10696c282eb4SLi Zefan 	 * make sure that once we start defragging an extent, we keep on
10706c282eb4SLi Zefan 	 * defragging it
10716c282eb4SLi Zefan 	 */
10726c282eb4SLi Zefan 	if (start < *defrag_end)
10736c282eb4SLi Zefan 		return 1;
10746c282eb4SLi Zefan 
10756c282eb4SLi Zefan 	*skip = 0;
10766c282eb4SLi Zefan 
10776c282eb4SLi Zefan 	em = defrag_lookup_extent(inode, start);
10786c282eb4SLi Zefan 	if (!em)
10796c282eb4SLi Zefan 		return 0;
10806c282eb4SLi Zefan 
1081940100a4SChris Mason 	/* this will cover holes, and inline extents */
108217ce6ef8SLiu Bo 	if (em->block_start >= EXTENT_MAP_LAST_BYTE) {
1083940100a4SChris Mason 		ret = 0;
108417ce6ef8SLiu Bo 		goto out;
108517ce6ef8SLiu Bo 	}
108617ce6ef8SLiu Bo 
10876c282eb4SLi Zefan 	next_mergeable = defrag_check_next_extent(inode, em);
1088940100a4SChris Mason 
1089940100a4SChris Mason 	/*
10906c282eb4SLi Zefan 	 * we hit a real extent, if it is big or the next extent is not a
10916c282eb4SLi Zefan 	 * real extent, don't bother defragging it
1092940100a4SChris Mason 	 */
1093a43a2111SAndrew Mahone 	if (!compress && (*last_len == 0 || *last_len >= thresh) &&
10946c282eb4SLi Zefan 	    (em->len >= thresh || !next_mergeable))
1095940100a4SChris Mason 		ret = 0;
109617ce6ef8SLiu Bo out:
1097940100a4SChris Mason 	/*
1098940100a4SChris Mason 	 * last_len ends up being a counter of how many bytes we've defragged.
1099940100a4SChris Mason 	 * every time we choose not to defrag an extent, we reset *last_len
1100940100a4SChris Mason 	 * so that the next tiny extent will force a defrag.
1101940100a4SChris Mason 	 *
1102940100a4SChris Mason 	 * The end result of this is that tiny extents before a single big
1103940100a4SChris Mason 	 * extent will force at least part of that big extent to be defragged.
1104940100a4SChris Mason 	 */
1105940100a4SChris Mason 	if (ret) {
1106940100a4SChris Mason 		*defrag_end = extent_map_end(em);
1107940100a4SChris Mason 	} else {
1108940100a4SChris Mason 		*last_len = 0;
1109940100a4SChris Mason 		*skip = extent_map_end(em);
1110940100a4SChris Mason 		*defrag_end = 0;
1111940100a4SChris Mason 	}
1112940100a4SChris Mason 
1113940100a4SChris Mason 	free_extent_map(em);
1114940100a4SChris Mason 	return ret;
1115940100a4SChris Mason }
1116940100a4SChris Mason 
11174cb5300bSChris Mason /*
11184cb5300bSChris Mason  * it doesn't do much good to defrag one or two pages
11194cb5300bSChris Mason  * at a time.  This pulls in a nice chunk of pages
11204cb5300bSChris Mason  * to COW and defrag.
11214cb5300bSChris Mason  *
11224cb5300bSChris Mason  * It also makes sure the delalloc code has enough
11234cb5300bSChris Mason  * dirty data to avoid making new small extents as part
11244cb5300bSChris Mason  * of the defrag
11254cb5300bSChris Mason  *
11264cb5300bSChris Mason  * It's a good idea to start RA on this range
11274cb5300bSChris Mason  * before calling this.
11284cb5300bSChris Mason  */
11294cb5300bSChris Mason static int cluster_pages_for_defrag(struct inode *inode,
11304cb5300bSChris Mason 				    struct page **pages,
11314cb5300bSChris Mason 				    unsigned long start_index,
1132c41570c9SJustin Maggard 				    unsigned long num_pages)
1133f46b5a66SChristoph Hellwig {
11344cb5300bSChris Mason 	unsigned long file_end;
11354cb5300bSChris Mason 	u64 isize = i_size_read(inode);
1136f46b5a66SChristoph Hellwig 	u64 page_start;
1137f46b5a66SChristoph Hellwig 	u64 page_end;
11381f12bd06SLiu Bo 	u64 page_cnt;
11394cb5300bSChris Mason 	int ret;
11404cb5300bSChris Mason 	int i;
11414cb5300bSChris Mason 	int i_done;
11424cb5300bSChris Mason 	struct btrfs_ordered_extent *ordered;
11434cb5300bSChris Mason 	struct extent_state *cached_state = NULL;
1144600a45e1SMiao Xie 	struct extent_io_tree *tree;
11453b16a4e3SJosef Bacik 	gfp_t mask = btrfs_alloc_write_mask(inode->i_mapping);
11464cb5300bSChris Mason 
11474cb5300bSChris Mason 	file_end = (isize - 1) >> PAGE_CACHE_SHIFT;
11481f12bd06SLiu Bo 	if (!isize || start_index > file_end)
11491f12bd06SLiu Bo 		return 0;
11501f12bd06SLiu Bo 
11511f12bd06SLiu Bo 	page_cnt = min_t(u64, (u64)num_pages, (u64)file_end - start_index + 1);
11524cb5300bSChris Mason 
11534cb5300bSChris Mason 	ret = btrfs_delalloc_reserve_space(inode,
11541f12bd06SLiu Bo 					   page_cnt << PAGE_CACHE_SHIFT);
11554cb5300bSChris Mason 	if (ret)
11564cb5300bSChris Mason 		return ret;
11574cb5300bSChris Mason 	i_done = 0;
1158600a45e1SMiao Xie 	tree = &BTRFS_I(inode)->io_tree;
11594cb5300bSChris Mason 
11604cb5300bSChris Mason 	/* step one, lock all the pages */
11611f12bd06SLiu Bo 	for (i = 0; i < page_cnt; i++) {
11624cb5300bSChris Mason 		struct page *page;
1163600a45e1SMiao Xie again:
1164a94733d0SJosef Bacik 		page = find_or_create_page(inode->i_mapping,
11653b16a4e3SJosef Bacik 					   start_index + i, mask);
11664cb5300bSChris Mason 		if (!page)
11674cb5300bSChris Mason 			break;
11684cb5300bSChris Mason 
1169600a45e1SMiao Xie 		page_start = page_offset(page);
1170600a45e1SMiao Xie 		page_end = page_start + PAGE_CACHE_SIZE - 1;
1171600a45e1SMiao Xie 		while (1) {
1172308d9800SFilipe Manana 			lock_extent_bits(tree, page_start, page_end,
1173308d9800SFilipe Manana 					 0, &cached_state);
1174600a45e1SMiao Xie 			ordered = btrfs_lookup_ordered_extent(inode,
1175600a45e1SMiao Xie 							      page_start);
1176308d9800SFilipe Manana 			unlock_extent_cached(tree, page_start, page_end,
1177308d9800SFilipe Manana 					     &cached_state, GFP_NOFS);
1178600a45e1SMiao Xie 			if (!ordered)
1179600a45e1SMiao Xie 				break;
1180600a45e1SMiao Xie 
1181600a45e1SMiao Xie 			unlock_page(page);
1182600a45e1SMiao Xie 			btrfs_start_ordered_extent(inode, ordered, 1);
1183600a45e1SMiao Xie 			btrfs_put_ordered_extent(ordered);
1184600a45e1SMiao Xie 			lock_page(page);
11851f12bd06SLiu Bo 			/*
11861f12bd06SLiu Bo 			 * we unlocked the page above, so we need check if
11871f12bd06SLiu Bo 			 * it was released or not.
11881f12bd06SLiu Bo 			 */
11891f12bd06SLiu Bo 			if (page->mapping != inode->i_mapping) {
11901f12bd06SLiu Bo 				unlock_page(page);
11911f12bd06SLiu Bo 				page_cache_release(page);
11921f12bd06SLiu Bo 				goto again;
11931f12bd06SLiu Bo 			}
1194600a45e1SMiao Xie 		}
1195600a45e1SMiao Xie 
11964cb5300bSChris Mason 		if (!PageUptodate(page)) {
11974cb5300bSChris Mason 			btrfs_readpage(NULL, page);
11984cb5300bSChris Mason 			lock_page(page);
11994cb5300bSChris Mason 			if (!PageUptodate(page)) {
12004cb5300bSChris Mason 				unlock_page(page);
12014cb5300bSChris Mason 				page_cache_release(page);
12024cb5300bSChris Mason 				ret = -EIO;
12034cb5300bSChris Mason 				break;
12044cb5300bSChris Mason 			}
12054cb5300bSChris Mason 		}
1206600a45e1SMiao Xie 
1207600a45e1SMiao Xie 		if (page->mapping != inode->i_mapping) {
1208600a45e1SMiao Xie 			unlock_page(page);
1209600a45e1SMiao Xie 			page_cache_release(page);
1210600a45e1SMiao Xie 			goto again;
1211600a45e1SMiao Xie 		}
1212600a45e1SMiao Xie 
12134cb5300bSChris Mason 		pages[i] = page;
12144cb5300bSChris Mason 		i_done++;
12154cb5300bSChris Mason 	}
12164cb5300bSChris Mason 	if (!i_done || ret)
12174cb5300bSChris Mason 		goto out;
12184cb5300bSChris Mason 
12194cb5300bSChris Mason 	if (!(inode->i_sb->s_flags & MS_ACTIVE))
12204cb5300bSChris Mason 		goto out;
12214cb5300bSChris Mason 
12224cb5300bSChris Mason 	/*
12234cb5300bSChris Mason 	 * so now we have a nice long stream of locked
12244cb5300bSChris Mason 	 * and up to date pages, lets wait on them
12254cb5300bSChris Mason 	 */
12264cb5300bSChris Mason 	for (i = 0; i < i_done; i++)
12274cb5300bSChris Mason 		wait_on_page_writeback(pages[i]);
12284cb5300bSChris Mason 
12294cb5300bSChris Mason 	page_start = page_offset(pages[0]);
12304cb5300bSChris Mason 	page_end = page_offset(pages[i_done - 1]) + PAGE_CACHE_SIZE;
12314cb5300bSChris Mason 
12324cb5300bSChris Mason 	lock_extent_bits(&BTRFS_I(inode)->io_tree,
1233d0082371SJeff Mahoney 			 page_start, page_end - 1, 0, &cached_state);
12344cb5300bSChris Mason 	clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start,
12354cb5300bSChris Mason 			  page_end - 1, EXTENT_DIRTY | EXTENT_DELALLOC |
12369e8a4a8bSLiu Bo 			  EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG, 0, 0,
12379e8a4a8bSLiu Bo 			  &cached_state, GFP_NOFS);
12384cb5300bSChris Mason 
12391f12bd06SLiu Bo 	if (i_done != page_cnt) {
12409e0baf60SJosef Bacik 		spin_lock(&BTRFS_I(inode)->lock);
12419e0baf60SJosef Bacik 		BTRFS_I(inode)->outstanding_extents++;
12429e0baf60SJosef Bacik 		spin_unlock(&BTRFS_I(inode)->lock);
12434cb5300bSChris Mason 		btrfs_delalloc_release_space(inode,
12441f12bd06SLiu Bo 				     (page_cnt - i_done) << PAGE_CACHE_SHIFT);
12454cb5300bSChris Mason 	}
12464cb5300bSChris Mason 
12474cb5300bSChris Mason 
12489e8a4a8bSLiu Bo 	set_extent_defrag(&BTRFS_I(inode)->io_tree, page_start, page_end - 1,
12499e8a4a8bSLiu Bo 			  &cached_state, GFP_NOFS);
12504cb5300bSChris Mason 
12514cb5300bSChris Mason 	unlock_extent_cached(&BTRFS_I(inode)->io_tree,
12524cb5300bSChris Mason 			     page_start, page_end - 1, &cached_state,
12534cb5300bSChris Mason 			     GFP_NOFS);
12544cb5300bSChris Mason 
12554cb5300bSChris Mason 	for (i = 0; i < i_done; i++) {
12564cb5300bSChris Mason 		clear_page_dirty_for_io(pages[i]);
12574cb5300bSChris Mason 		ClearPageChecked(pages[i]);
12584cb5300bSChris Mason 		set_page_extent_mapped(pages[i]);
12594cb5300bSChris Mason 		set_page_dirty(pages[i]);
12604cb5300bSChris Mason 		unlock_page(pages[i]);
12614cb5300bSChris Mason 		page_cache_release(pages[i]);
12624cb5300bSChris Mason 	}
12634cb5300bSChris Mason 	return i_done;
12644cb5300bSChris Mason out:
12654cb5300bSChris Mason 	for (i = 0; i < i_done; i++) {
12664cb5300bSChris Mason 		unlock_page(pages[i]);
12674cb5300bSChris Mason 		page_cache_release(pages[i]);
12684cb5300bSChris Mason 	}
12691f12bd06SLiu Bo 	btrfs_delalloc_release_space(inode, page_cnt << PAGE_CACHE_SHIFT);
12704cb5300bSChris Mason 	return ret;
12714cb5300bSChris Mason 
12724cb5300bSChris Mason }
12734cb5300bSChris Mason 
12744cb5300bSChris Mason int btrfs_defrag_file(struct inode *inode, struct file *file,
12754cb5300bSChris Mason 		      struct btrfs_ioctl_defrag_range_args *range,
12764cb5300bSChris Mason 		      u64 newer_than, unsigned long max_to_defrag)
12774cb5300bSChris Mason {
12784cb5300bSChris Mason 	struct btrfs_root *root = BTRFS_I(inode)->root;
12794cb5300bSChris Mason 	struct file_ra_state *ra = NULL;
12804cb5300bSChris Mason 	unsigned long last_index;
1281151a31b2SLi Zefan 	u64 isize = i_size_read(inode);
1282940100a4SChris Mason 	u64 last_len = 0;
1283940100a4SChris Mason 	u64 skip = 0;
1284940100a4SChris Mason 	u64 defrag_end = 0;
12854cb5300bSChris Mason 	u64 newer_off = range->start;
1286f46b5a66SChristoph Hellwig 	unsigned long i;
1287008873eaSLi Zefan 	unsigned long ra_index = 0;
1288f46b5a66SChristoph Hellwig 	int ret;
12894cb5300bSChris Mason 	int defrag_count = 0;
12901a419d85SLi Zefan 	int compress_type = BTRFS_COMPRESS_ZLIB;
12914cb5300bSChris Mason 	int extent_thresh = range->extent_thresh;
1292c41570c9SJustin Maggard 	unsigned long max_cluster = (256 * 1024) >> PAGE_CACHE_SHIFT;
1293c41570c9SJustin Maggard 	unsigned long cluster = max_cluster;
12944cb5300bSChris Mason 	u64 new_align = ~((u64)128 * 1024 - 1);
12954cb5300bSChris Mason 	struct page **pages = NULL;
12964cb5300bSChris Mason 
12970abd5b17SLiu Bo 	if (isize == 0)
12980abd5b17SLiu Bo 		return 0;
12990abd5b17SLiu Bo 
13000abd5b17SLiu Bo 	if (range->start >= isize)
13010abd5b17SLiu Bo 		return -EINVAL;
13021a419d85SLi Zefan 
13031a419d85SLi Zefan 	if (range->flags & BTRFS_DEFRAG_RANGE_COMPRESS) {
13041a419d85SLi Zefan 		if (range->compress_type > BTRFS_COMPRESS_TYPES)
13051a419d85SLi Zefan 			return -EINVAL;
13061a419d85SLi Zefan 		if (range->compress_type)
13071a419d85SLi Zefan 			compress_type = range->compress_type;
13081a419d85SLi Zefan 	}
1309f46b5a66SChristoph Hellwig 
13100abd5b17SLiu Bo 	if (extent_thresh == 0)
13110abd5b17SLiu Bo 		extent_thresh = 256 * 1024;
1312f46b5a66SChristoph Hellwig 
13134cb5300bSChris Mason 	/*
13144cb5300bSChris Mason 	 * if we were not given a file, allocate a readahead
13154cb5300bSChris Mason 	 * context
13164cb5300bSChris Mason 	 */
13174cb5300bSChris Mason 	if (!file) {
13184cb5300bSChris Mason 		ra = kzalloc(sizeof(*ra), GFP_NOFS);
13194cb5300bSChris Mason 		if (!ra)
13204cb5300bSChris Mason 			return -ENOMEM;
13214cb5300bSChris Mason 		file_ra_state_init(ra, inode->i_mapping);
13224cb5300bSChris Mason 	} else {
13234cb5300bSChris Mason 		ra = &file->f_ra;
13244cb5300bSChris Mason 	}
13254cb5300bSChris Mason 
1326d9b0d9baSDulshani Gunawardhana 	pages = kmalloc_array(max_cluster, sizeof(struct page *),
13274cb5300bSChris Mason 			GFP_NOFS);
13284cb5300bSChris Mason 	if (!pages) {
13294cb5300bSChris Mason 		ret = -ENOMEM;
13304cb5300bSChris Mason 		goto out_ra;
13314cb5300bSChris Mason 	}
13324cb5300bSChris Mason 
13334cb5300bSChris Mason 	/* find the last page to defrag */
13341e701a32SChris Mason 	if (range->start + range->len > range->start) {
1335151a31b2SLi Zefan 		last_index = min_t(u64, isize - 1,
13361e701a32SChris Mason 			 range->start + range->len - 1) >> PAGE_CACHE_SHIFT;
13371e701a32SChris Mason 	} else {
1338151a31b2SLi Zefan 		last_index = (isize - 1) >> PAGE_CACHE_SHIFT;
13391e701a32SChris Mason 	}
13401e701a32SChris Mason 
13414cb5300bSChris Mason 	if (newer_than) {
13424cb5300bSChris Mason 		ret = find_new_extents(root, inode, newer_than,
13434cb5300bSChris Mason 				       &newer_off, 64 * 1024);
13444cb5300bSChris Mason 		if (!ret) {
13454cb5300bSChris Mason 			range->start = newer_off;
13464cb5300bSChris Mason 			/*
13474cb5300bSChris Mason 			 * we always align our defrag to help keep
13484cb5300bSChris Mason 			 * the extents in the file evenly spaced
13494cb5300bSChris Mason 			 */
13504cb5300bSChris Mason 			i = (newer_off & new_align) >> PAGE_CACHE_SHIFT;
13514cb5300bSChris Mason 		} else
13524cb5300bSChris Mason 			goto out_ra;
13534cb5300bSChris Mason 	} else {
13541e701a32SChris Mason 		i = range->start >> PAGE_CACHE_SHIFT;
13554cb5300bSChris Mason 	}
13564cb5300bSChris Mason 	if (!max_to_defrag)
13577ec31b54SLiu Bo 		max_to_defrag = last_index + 1;
13584cb5300bSChris Mason 
13592a0f7f57SLi Zefan 	/*
13602a0f7f57SLi Zefan 	 * make writeback starts from i, so the defrag range can be
13612a0f7f57SLi Zefan 	 * written sequentially.
13622a0f7f57SLi Zefan 	 */
13632a0f7f57SLi Zefan 	if (i < inode->i_mapping->writeback_index)
13642a0f7f57SLi Zefan 		inode->i_mapping->writeback_index = i;
13652a0f7f57SLi Zefan 
1366f7f43cc8SChris Mason 	while (i <= last_index && defrag_count < max_to_defrag &&
1367f7f43cc8SChris Mason 	       (i < (i_size_read(inode) + PAGE_CACHE_SIZE - 1) >>
1368f7f43cc8SChris Mason 		PAGE_CACHE_SHIFT)) {
13694cb5300bSChris Mason 		/*
13704cb5300bSChris Mason 		 * make sure we stop running if someone unmounts
13714cb5300bSChris Mason 		 * the FS
13724cb5300bSChris Mason 		 */
13734cb5300bSChris Mason 		if (!(inode->i_sb->s_flags & MS_ACTIVE))
13744cb5300bSChris Mason 			break;
13754cb5300bSChris Mason 
1376210549ebSDavid Sterba 		if (btrfs_defrag_cancelled(root->fs_info)) {
1377efe120a0SFrank Holton 			printk(KERN_DEBUG "BTRFS: defrag_file cancelled\n");
1378210549ebSDavid Sterba 			ret = -EAGAIN;
1379210549ebSDavid Sterba 			break;
1380210549ebSDavid Sterba 		}
1381210549ebSDavid Sterba 
138266c26892SLiu Bo 		if (!should_defrag_range(inode, (u64)i << PAGE_CACHE_SHIFT,
13836c282eb4SLi Zefan 					 extent_thresh, &last_len, &skip,
1384a43a2111SAndrew Mahone 					 &defrag_end, range->flags &
1385a43a2111SAndrew Mahone 					 BTRFS_DEFRAG_RANGE_COMPRESS)) {
1386940100a4SChris Mason 			unsigned long next;
1387940100a4SChris Mason 			/*
1388940100a4SChris Mason 			 * the should_defrag function tells us how much to skip
1389940100a4SChris Mason 			 * bump our counter by the suggested amount
1390940100a4SChris Mason 			 */
1391940100a4SChris Mason 			next = (skip + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
1392940100a4SChris Mason 			i = max(i + 1, next);
1393940100a4SChris Mason 			continue;
1394940100a4SChris Mason 		}
1395008873eaSLi Zefan 
1396008873eaSLi Zefan 		if (!newer_than) {
1397008873eaSLi Zefan 			cluster = (PAGE_CACHE_ALIGN(defrag_end) >>
1398008873eaSLi Zefan 				   PAGE_CACHE_SHIFT) - i;
1399008873eaSLi Zefan 			cluster = min(cluster, max_cluster);
1400008873eaSLi Zefan 		} else {
1401008873eaSLi Zefan 			cluster = max_cluster;
1402008873eaSLi Zefan 		}
1403008873eaSLi Zefan 
1404008873eaSLi Zefan 		if (i + cluster > ra_index) {
1405008873eaSLi Zefan 			ra_index = max(i, ra_index);
1406008873eaSLi Zefan 			btrfs_force_ra(inode->i_mapping, ra, file, ra_index,
1407008873eaSLi Zefan 				       cluster);
1408008873eaSLi Zefan 			ra_index += max_cluster;
1409008873eaSLi Zefan 		}
14104cb5300bSChris Mason 
1411ecb8bea8SLiu Bo 		mutex_lock(&inode->i_mutex);
1412633085c7SFilipe David Borba Manana 		if (range->flags & BTRFS_DEFRAG_RANGE_COMPRESS)
1413633085c7SFilipe David Borba Manana 			BTRFS_I(inode)->force_compress = compress_type;
1414008873eaSLi Zefan 		ret = cluster_pages_for_defrag(inode, pages, i, cluster);
1415ecb8bea8SLiu Bo 		if (ret < 0) {
1416ecb8bea8SLiu Bo 			mutex_unlock(&inode->i_mutex);
14174cb5300bSChris Mason 			goto out_ra;
1418ecb8bea8SLiu Bo 		}
14194cb5300bSChris Mason 
14204cb5300bSChris Mason 		defrag_count += ret;
1421d0e1d66bSNamjae Jeon 		balance_dirty_pages_ratelimited(inode->i_mapping);
1422ecb8bea8SLiu Bo 		mutex_unlock(&inode->i_mutex);
14234cb5300bSChris Mason 
14244cb5300bSChris Mason 		if (newer_than) {
14254cb5300bSChris Mason 			if (newer_off == (u64)-1)
14264cb5300bSChris Mason 				break;
14274cb5300bSChris Mason 
1428e1f041e1SLiu Bo 			if (ret > 0)
1429e1f041e1SLiu Bo 				i += ret;
1430e1f041e1SLiu Bo 
14314cb5300bSChris Mason 			newer_off = max(newer_off + 1,
14324cb5300bSChris Mason 					(u64)i << PAGE_CACHE_SHIFT);
14334cb5300bSChris Mason 
14344cb5300bSChris Mason 			ret = find_new_extents(root, inode,
14354cb5300bSChris Mason 					       newer_than, &newer_off,
14364cb5300bSChris Mason 					       64 * 1024);
14374cb5300bSChris Mason 			if (!ret) {
14384cb5300bSChris Mason 				range->start = newer_off;
14394cb5300bSChris Mason 				i = (newer_off & new_align) >> PAGE_CACHE_SHIFT;
14404cb5300bSChris Mason 			} else {
14414cb5300bSChris Mason 				break;
1442940100a4SChris Mason 			}
14434cb5300bSChris Mason 		} else {
1444008873eaSLi Zefan 			if (ret > 0) {
1445cbcc8326SLi Zefan 				i += ret;
1446008873eaSLi Zefan 				last_len += ret << PAGE_CACHE_SHIFT;
1447008873eaSLi Zefan 			} else {
1448940100a4SChris Mason 				i++;
1449008873eaSLi Zefan 				last_len = 0;
1450008873eaSLi Zefan 			}
1451f46b5a66SChristoph Hellwig 		}
14524cb5300bSChris Mason 	}
1453f46b5a66SChristoph Hellwig 
1454dec8ef90SFilipe Manana 	if ((range->flags & BTRFS_DEFRAG_RANGE_START_IO)) {
14551e701a32SChris Mason 		filemap_flush(inode->i_mapping);
1456dec8ef90SFilipe Manana 		if (test_bit(BTRFS_INODE_HAS_ASYNC_EXTENT,
1457dec8ef90SFilipe Manana 			     &BTRFS_I(inode)->runtime_flags))
1458dec8ef90SFilipe Manana 			filemap_flush(inode->i_mapping);
1459dec8ef90SFilipe Manana 	}
14601e701a32SChris Mason 
14611e701a32SChris Mason 	if ((range->flags & BTRFS_DEFRAG_RANGE_COMPRESS)) {
14621e701a32SChris Mason 		/* the filemap_flush will queue IO into the worker threads, but
14631e701a32SChris Mason 		 * we have to make sure the IO is actually started and that
14641e701a32SChris Mason 		 * ordered extents get created before we return
14651e701a32SChris Mason 		 */
14661e701a32SChris Mason 		atomic_inc(&root->fs_info->async_submit_draining);
14671e701a32SChris Mason 		while (atomic_read(&root->fs_info->nr_async_submits) ||
14681e701a32SChris Mason 		      atomic_read(&root->fs_info->async_delalloc_pages)) {
14691e701a32SChris Mason 			wait_event(root->fs_info->async_submit_wait,
14701e701a32SChris Mason 			   (atomic_read(&root->fs_info->nr_async_submits) == 0 &&
14711e701a32SChris Mason 			    atomic_read(&root->fs_info->async_delalloc_pages) == 0));
14721e701a32SChris Mason 		}
14731e701a32SChris Mason 		atomic_dec(&root->fs_info->async_submit_draining);
14741e701a32SChris Mason 	}
14751e701a32SChris Mason 
14761a419d85SLi Zefan 	if (range->compress_type == BTRFS_COMPRESS_LZO) {
14772b0ce2c2SMitch Harder 		btrfs_set_fs_incompat(root->fs_info, COMPRESS_LZO);
14781a419d85SLi Zefan 	}
14791a419d85SLi Zefan 
148060ccf82fSDiego Calleja 	ret = defrag_count;
1481940100a4SChris Mason 
14824cb5300bSChris Mason out_ra:
1483633085c7SFilipe David Borba Manana 	if (range->flags & BTRFS_DEFRAG_RANGE_COMPRESS) {
1484633085c7SFilipe David Borba Manana 		mutex_lock(&inode->i_mutex);
1485633085c7SFilipe David Borba Manana 		BTRFS_I(inode)->force_compress = BTRFS_COMPRESS_NONE;
1486633085c7SFilipe David Borba Manana 		mutex_unlock(&inode->i_mutex);
1487633085c7SFilipe David Borba Manana 	}
14884cb5300bSChris Mason 	if (!file)
14894cb5300bSChris Mason 		kfree(ra);
14904cb5300bSChris Mason 	kfree(pages);
1491940100a4SChris Mason 	return ret;
1492f46b5a66SChristoph Hellwig }
1493f46b5a66SChristoph Hellwig 
1494198605a8SMiao Xie static noinline int btrfs_ioctl_resize(struct file *file,
149576dda93cSYan, Zheng 					void __user *arg)
1496f46b5a66SChristoph Hellwig {
1497f46b5a66SChristoph Hellwig 	u64 new_size;
1498f46b5a66SChristoph Hellwig 	u64 old_size;
1499f46b5a66SChristoph Hellwig 	u64 devid = 1;
1500496ad9aaSAl Viro 	struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
1501f46b5a66SChristoph Hellwig 	struct btrfs_ioctl_vol_args *vol_args;
1502f46b5a66SChristoph Hellwig 	struct btrfs_trans_handle *trans;
1503f46b5a66SChristoph Hellwig 	struct btrfs_device *device = NULL;
1504f46b5a66SChristoph Hellwig 	char *sizestr;
15059a40f122SGui Hecheng 	char *retptr;
1506f46b5a66SChristoph Hellwig 	char *devstr = NULL;
1507f46b5a66SChristoph Hellwig 	int ret = 0;
1508f46b5a66SChristoph Hellwig 	int mod = 0;
1509f46b5a66SChristoph Hellwig 
1510e441d54dSChris Mason 	if (!capable(CAP_SYS_ADMIN))
1511e441d54dSChris Mason 		return -EPERM;
1512e441d54dSChris Mason 
1513198605a8SMiao Xie 	ret = mnt_want_write_file(file);
1514198605a8SMiao Xie 	if (ret)
1515198605a8SMiao Xie 		return ret;
1516198605a8SMiao Xie 
15175ac00addSStefan Behrens 	if (atomic_xchg(&root->fs_info->mutually_exclusive_operation_running,
15185ac00addSStefan Behrens 			1)) {
151997547676SMiao Xie 		mnt_drop_write_file(file);
1520e57138b3SAnand Jain 		return BTRFS_ERROR_DEV_EXCL_RUN_IN_PROGRESS;
1521c9e9f97bSIlya Dryomov 	}
1522c9e9f97bSIlya Dryomov 
15235ac00addSStefan Behrens 	mutex_lock(&root->fs_info->volume_mutex);
1524dae7b665SLi Zefan 	vol_args = memdup_user(arg, sizeof(*vol_args));
1525c9e9f97bSIlya Dryomov 	if (IS_ERR(vol_args)) {
1526c9e9f97bSIlya Dryomov 		ret = PTR_ERR(vol_args);
1527c9e9f97bSIlya Dryomov 		goto out;
1528c9e9f97bSIlya Dryomov 	}
15295516e595SMark Fasheh 
15305516e595SMark Fasheh 	vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
1531f46b5a66SChristoph Hellwig 
1532f46b5a66SChristoph Hellwig 	sizestr = vol_args->name;
1533f46b5a66SChristoph Hellwig 	devstr = strchr(sizestr, ':');
1534f46b5a66SChristoph Hellwig 	if (devstr) {
1535f46b5a66SChristoph Hellwig 		sizestr = devstr + 1;
1536f46b5a66SChristoph Hellwig 		*devstr = '\0';
1537f46b5a66SChristoph Hellwig 		devstr = vol_args->name;
153858dfae63SZhangZhen 		ret = kstrtoull(devstr, 10, &devid);
153958dfae63SZhangZhen 		if (ret)
154058dfae63SZhangZhen 			goto out_free;
1541dfd79829SMiao Xie 		if (!devid) {
1542dfd79829SMiao Xie 			ret = -EINVAL;
1543dfd79829SMiao Xie 			goto out_free;
1544dfd79829SMiao Xie 		}
1545efe120a0SFrank Holton 		btrfs_info(root->fs_info, "resizing devid %llu", devid);
1546f46b5a66SChristoph Hellwig 	}
1547dba60f3fSMiao Xie 
1548aa1b8cd4SStefan Behrens 	device = btrfs_find_device(root->fs_info, devid, NULL, NULL);
1549f46b5a66SChristoph Hellwig 	if (!device) {
1550efe120a0SFrank Holton 		btrfs_info(root->fs_info, "resizer unable to find device %llu",
1551c1c9ff7cSGeert Uytterhoeven 		       devid);
1552dfd79829SMiao Xie 		ret = -ENODEV;
1553c9e9f97bSIlya Dryomov 		goto out_free;
1554f46b5a66SChristoph Hellwig 	}
1555dba60f3fSMiao Xie 
1556dba60f3fSMiao Xie 	if (!device->writeable) {
1557efe120a0SFrank Holton 		btrfs_info(root->fs_info,
1558efe120a0SFrank Holton 			   "resizer unable to apply on readonly device %llu",
1559c1c9ff7cSGeert Uytterhoeven 		       devid);
1560dfd79829SMiao Xie 		ret = -EPERM;
15614e42ae1bSLiu Bo 		goto out_free;
15624e42ae1bSLiu Bo 	}
15634e42ae1bSLiu Bo 
1564f46b5a66SChristoph Hellwig 	if (!strcmp(sizestr, "max"))
1565f46b5a66SChristoph Hellwig 		new_size = device->bdev->bd_inode->i_size;
1566f46b5a66SChristoph Hellwig 	else {
1567f46b5a66SChristoph Hellwig 		if (sizestr[0] == '-') {
1568f46b5a66SChristoph Hellwig 			mod = -1;
1569f46b5a66SChristoph Hellwig 			sizestr++;
1570f46b5a66SChristoph Hellwig 		} else if (sizestr[0] == '+') {
1571f46b5a66SChristoph Hellwig 			mod = 1;
1572f46b5a66SChristoph Hellwig 			sizestr++;
1573f46b5a66SChristoph Hellwig 		}
15749a40f122SGui Hecheng 		new_size = memparse(sizestr, &retptr);
15759a40f122SGui Hecheng 		if (*retptr != '\0' || new_size == 0) {
1576f46b5a66SChristoph Hellwig 			ret = -EINVAL;
1577c9e9f97bSIlya Dryomov 			goto out_free;
1578f46b5a66SChristoph Hellwig 		}
1579f46b5a66SChristoph Hellwig 	}
1580f46b5a66SChristoph Hellwig 
158163a212abSStefan Behrens 	if (device->is_tgtdev_for_dev_replace) {
1582dfd79829SMiao Xie 		ret = -EPERM;
158363a212abSStefan Behrens 		goto out_free;
158463a212abSStefan Behrens 	}
158563a212abSStefan Behrens 
1586f46b5a66SChristoph Hellwig 	old_size = device->total_bytes;
1587f46b5a66SChristoph Hellwig 
1588f46b5a66SChristoph Hellwig 	if (mod < 0) {
1589f46b5a66SChristoph Hellwig 		if (new_size > old_size) {
1590f46b5a66SChristoph Hellwig 			ret = -EINVAL;
1591c9e9f97bSIlya Dryomov 			goto out_free;
1592f46b5a66SChristoph Hellwig 		}
1593f46b5a66SChristoph Hellwig 		new_size = old_size - new_size;
1594f46b5a66SChristoph Hellwig 	} else if (mod > 0) {
1595eb8052e0SWenliang Fan 		if (new_size > ULLONG_MAX - old_size) {
1596902c68a4SGui Hecheng 			ret = -ERANGE;
1597eb8052e0SWenliang Fan 			goto out_free;
1598eb8052e0SWenliang Fan 		}
1599f46b5a66SChristoph Hellwig 		new_size = old_size + new_size;
1600f46b5a66SChristoph Hellwig 	}
1601f46b5a66SChristoph Hellwig 
1602f46b5a66SChristoph Hellwig 	if (new_size < 256 * 1024 * 1024) {
1603f46b5a66SChristoph Hellwig 		ret = -EINVAL;
1604c9e9f97bSIlya Dryomov 		goto out_free;
1605f46b5a66SChristoph Hellwig 	}
1606f46b5a66SChristoph Hellwig 	if (new_size > device->bdev->bd_inode->i_size) {
1607f46b5a66SChristoph Hellwig 		ret = -EFBIG;
1608c9e9f97bSIlya Dryomov 		goto out_free;
1609f46b5a66SChristoph Hellwig 	}
1610f46b5a66SChristoph Hellwig 
1611f46b5a66SChristoph Hellwig 	do_div(new_size, root->sectorsize);
1612f46b5a66SChristoph Hellwig 	new_size *= root->sectorsize;
1613f46b5a66SChristoph Hellwig 
1614efe120a0SFrank Holton 	printk_in_rcu(KERN_INFO "BTRFS: new size for %s is %llu\n",
1615c1c9ff7cSGeert Uytterhoeven 		      rcu_str_deref(device->name), new_size);
1616f46b5a66SChristoph Hellwig 
1617f46b5a66SChristoph Hellwig 	if (new_size > old_size) {
1618a22285a6SYan, Zheng 		trans = btrfs_start_transaction(root, 0);
161998d5dc13STsutomu Itoh 		if (IS_ERR(trans)) {
162098d5dc13STsutomu Itoh 			ret = PTR_ERR(trans);
1621c9e9f97bSIlya Dryomov 			goto out_free;
162298d5dc13STsutomu Itoh 		}
1623f46b5a66SChristoph Hellwig 		ret = btrfs_grow_device(trans, device, new_size);
1624f46b5a66SChristoph Hellwig 		btrfs_commit_transaction(trans, root);
1625ece7d20eSMike Fleetwood 	} else if (new_size < old_size) {
1626f46b5a66SChristoph Hellwig 		ret = btrfs_shrink_device(device, new_size);
16270253f40eSjeff.liu 	} /* equal, nothing need to do */
1628f46b5a66SChristoph Hellwig 
1629c9e9f97bSIlya Dryomov out_free:
1630f46b5a66SChristoph Hellwig 	kfree(vol_args);
1631c9e9f97bSIlya Dryomov out:
1632c9e9f97bSIlya Dryomov 	mutex_unlock(&root->fs_info->volume_mutex);
16335ac00addSStefan Behrens 	atomic_set(&root->fs_info->mutually_exclusive_operation_running, 0);
163418f39c41SIlya Dryomov 	mnt_drop_write_file(file);
1635f46b5a66SChristoph Hellwig 	return ret;
1636f46b5a66SChristoph Hellwig }
1637f46b5a66SChristoph Hellwig 
163872fd032eSSage Weil static noinline int btrfs_ioctl_snap_create_transid(struct file *file,
16396f72c7e2SArne Jansen 				char *name, unsigned long fd, int subvol,
16406f72c7e2SArne Jansen 				u64 *transid, bool readonly,
16418696c533SMiao Xie 				struct btrfs_qgroup_inherit *inherit)
1642f46b5a66SChristoph Hellwig {
1643f46b5a66SChristoph Hellwig 	int namelen;
16443de4586cSChris Mason 	int ret = 0;
1645f46b5a66SChristoph Hellwig 
1646a874a63eSLiu Bo 	ret = mnt_want_write_file(file);
1647a874a63eSLiu Bo 	if (ret)
1648a874a63eSLiu Bo 		goto out;
1649a874a63eSLiu Bo 
165072fd032eSSage Weil 	namelen = strlen(name);
165172fd032eSSage Weil 	if (strchr(name, '/')) {
1652f46b5a66SChristoph Hellwig 		ret = -EINVAL;
1653a874a63eSLiu Bo 		goto out_drop_write;
1654f46b5a66SChristoph Hellwig 	}
1655f46b5a66SChristoph Hellwig 
165616780cabSChris Mason 	if (name[0] == '.' &&
165716780cabSChris Mason 	   (namelen == 1 || (name[1] == '.' && namelen == 2))) {
165816780cabSChris Mason 		ret = -EEXIST;
1659a874a63eSLiu Bo 		goto out_drop_write;
166016780cabSChris Mason 	}
166116780cabSChris Mason 
16623de4586cSChris Mason 	if (subvol) {
166372fd032eSSage Weil 		ret = btrfs_mksubvol(&file->f_path, name, namelen,
16646f72c7e2SArne Jansen 				     NULL, transid, readonly, inherit);
1665cb8e7090SChristoph Hellwig 	} else {
16662903ff01SAl Viro 		struct fd src = fdget(fd);
16673de4586cSChris Mason 		struct inode *src_inode;
16682903ff01SAl Viro 		if (!src.file) {
16693de4586cSChris Mason 			ret = -EINVAL;
1670a874a63eSLiu Bo 			goto out_drop_write;
16713de4586cSChris Mason 		}
16723de4586cSChris Mason 
1673496ad9aaSAl Viro 		src_inode = file_inode(src.file);
1674496ad9aaSAl Viro 		if (src_inode->i_sb != file_inode(file)->i_sb) {
1675efe120a0SFrank Holton 			btrfs_info(BTRFS_I(src_inode)->root->fs_info,
1676efe120a0SFrank Holton 				   "Snapshot src from another FS");
167723ad5b17SKusanagi Kouichi 			ret = -EXDEV;
1678d0242061SDavid Sterba 		} else if (!inode_owner_or_capable(src_inode)) {
1679d0242061SDavid Sterba 			/*
1680d0242061SDavid Sterba 			 * Subvolume creation is not restricted, but snapshots
1681d0242061SDavid Sterba 			 * are limited to own subvolumes only
1682d0242061SDavid Sterba 			 */
1683d0242061SDavid Sterba 			ret = -EPERM;
1684ecd18815SAl Viro 		} else {
168572fd032eSSage Weil 			ret = btrfs_mksubvol(&file->f_path, name, namelen,
168672fd032eSSage Weil 					     BTRFS_I(src_inode)->root,
16876f72c7e2SArne Jansen 					     transid, readonly, inherit);
1688ecd18815SAl Viro 		}
16892903ff01SAl Viro 		fdput(src);
1690cb8e7090SChristoph Hellwig 	}
1691a874a63eSLiu Bo out_drop_write:
1692a874a63eSLiu Bo 	mnt_drop_write_file(file);
1693f46b5a66SChristoph Hellwig out:
169472fd032eSSage Weil 	return ret;
169572fd032eSSage Weil }
169672fd032eSSage Weil 
169772fd032eSSage Weil static noinline int btrfs_ioctl_snap_create(struct file *file,
1698fa0d2b9bSLi Zefan 					    void __user *arg, int subvol)
169972fd032eSSage Weil {
1700fa0d2b9bSLi Zefan 	struct btrfs_ioctl_vol_args *vol_args;
170172fd032eSSage Weil 	int ret;
170272fd032eSSage Weil 
1703fa0d2b9bSLi Zefan 	vol_args = memdup_user(arg, sizeof(*vol_args));
1704fa0d2b9bSLi Zefan 	if (IS_ERR(vol_args))
1705fa0d2b9bSLi Zefan 		return PTR_ERR(vol_args);
1706fa0d2b9bSLi Zefan 	vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
1707fa0d2b9bSLi Zefan 
1708fa0d2b9bSLi Zefan 	ret = btrfs_ioctl_snap_create_transid(file, vol_args->name,
1709b83cc969SLi Zefan 					      vol_args->fd, subvol,
17106f72c7e2SArne Jansen 					      NULL, false, NULL);
1711fa0d2b9bSLi Zefan 
1712fa0d2b9bSLi Zefan 	kfree(vol_args);
1713fa0d2b9bSLi Zefan 	return ret;
1714fa0d2b9bSLi Zefan }
1715fa0d2b9bSLi Zefan 
1716fa0d2b9bSLi Zefan static noinline int btrfs_ioctl_snap_create_v2(struct file *file,
1717fa0d2b9bSLi Zefan 					       void __user *arg, int subvol)
1718fa0d2b9bSLi Zefan {
1719fa0d2b9bSLi Zefan 	struct btrfs_ioctl_vol_args_v2 *vol_args;
1720fa0d2b9bSLi Zefan 	int ret;
1721fdfb1e4fSLi Zefan 	u64 transid = 0;
1722fdfb1e4fSLi Zefan 	u64 *ptr = NULL;
1723b83cc969SLi Zefan 	bool readonly = false;
17246f72c7e2SArne Jansen 	struct btrfs_qgroup_inherit *inherit = NULL;
172572fd032eSSage Weil 
1726fa0d2b9bSLi Zefan 	vol_args = memdup_user(arg, sizeof(*vol_args));
1727fa0d2b9bSLi Zefan 	if (IS_ERR(vol_args))
1728fa0d2b9bSLi Zefan 		return PTR_ERR(vol_args);
1729fa0d2b9bSLi Zefan 	vol_args->name[BTRFS_SUBVOL_NAME_MAX] = '\0';
1730fdfb1e4fSLi Zefan 
1731b83cc969SLi Zefan 	if (vol_args->flags &
17326f72c7e2SArne Jansen 	    ~(BTRFS_SUBVOL_CREATE_ASYNC | BTRFS_SUBVOL_RDONLY |
17336f72c7e2SArne Jansen 	      BTRFS_SUBVOL_QGROUP_INHERIT)) {
1734b83cc969SLi Zefan 		ret = -EOPNOTSUPP;
1735fdfb1e4fSLi Zefan 		goto out;
1736fdfb1e4fSLi Zefan 	}
1737fdfb1e4fSLi Zefan 
1738fa0d2b9bSLi Zefan 	if (vol_args->flags & BTRFS_SUBVOL_CREATE_ASYNC)
1739fdfb1e4fSLi Zefan 		ptr = &transid;
1740b83cc969SLi Zefan 	if (vol_args->flags & BTRFS_SUBVOL_RDONLY)
1741b83cc969SLi Zefan 		readonly = true;
17426f72c7e2SArne Jansen 	if (vol_args->flags & BTRFS_SUBVOL_QGROUP_INHERIT) {
17436f72c7e2SArne Jansen 		if (vol_args->size > PAGE_CACHE_SIZE) {
17446f72c7e2SArne Jansen 			ret = -EINVAL;
17456f72c7e2SArne Jansen 			goto out;
17466f72c7e2SArne Jansen 		}
17476f72c7e2SArne Jansen 		inherit = memdup_user(vol_args->qgroup_inherit, vol_args->size);
17486f72c7e2SArne Jansen 		if (IS_ERR(inherit)) {
17496f72c7e2SArne Jansen 			ret = PTR_ERR(inherit);
17506f72c7e2SArne Jansen 			goto out;
17516f72c7e2SArne Jansen 		}
17526f72c7e2SArne Jansen 	}
175375eaa0e2SSage Weil 
1754fa0d2b9bSLi Zefan 	ret = btrfs_ioctl_snap_create_transid(file, vol_args->name,
17556f72c7e2SArne Jansen 					      vol_args->fd, subvol, ptr,
17568696c533SMiao Xie 					      readonly, inherit);
175775eaa0e2SSage Weil 
1758fdfb1e4fSLi Zefan 	if (ret == 0 && ptr &&
175975eaa0e2SSage Weil 	    copy_to_user(arg +
1760fdfb1e4fSLi Zefan 			 offsetof(struct btrfs_ioctl_vol_args_v2,
1761fdfb1e4fSLi Zefan 				  transid), ptr, sizeof(*ptr)))
176275eaa0e2SSage Weil 		ret = -EFAULT;
1763fdfb1e4fSLi Zefan out:
1764f46b5a66SChristoph Hellwig 	kfree(vol_args);
17656f72c7e2SArne Jansen 	kfree(inherit);
1766f46b5a66SChristoph Hellwig 	return ret;
1767f46b5a66SChristoph Hellwig }
1768f46b5a66SChristoph Hellwig 
17690caa102dSLi Zefan static noinline int btrfs_ioctl_subvol_getflags(struct file *file,
17700caa102dSLi Zefan 						void __user *arg)
17710caa102dSLi Zefan {
1772496ad9aaSAl Viro 	struct inode *inode = file_inode(file);
17730caa102dSLi Zefan 	struct btrfs_root *root = BTRFS_I(inode)->root;
17740caa102dSLi Zefan 	int ret = 0;
17750caa102dSLi Zefan 	u64 flags = 0;
17760caa102dSLi Zefan 
177733345d01SLi Zefan 	if (btrfs_ino(inode) != BTRFS_FIRST_FREE_OBJECTID)
17780caa102dSLi Zefan 		return -EINVAL;
17790caa102dSLi Zefan 
17800caa102dSLi Zefan 	down_read(&root->fs_info->subvol_sem);
17810caa102dSLi Zefan 	if (btrfs_root_readonly(root))
17820caa102dSLi Zefan 		flags |= BTRFS_SUBVOL_RDONLY;
17830caa102dSLi Zefan 	up_read(&root->fs_info->subvol_sem);
17840caa102dSLi Zefan 
17850caa102dSLi Zefan 	if (copy_to_user(arg, &flags, sizeof(flags)))
17860caa102dSLi Zefan 		ret = -EFAULT;
17870caa102dSLi Zefan 
17880caa102dSLi Zefan 	return ret;
17890caa102dSLi Zefan }
17900caa102dSLi Zefan 
17910caa102dSLi Zefan static noinline int btrfs_ioctl_subvol_setflags(struct file *file,
17920caa102dSLi Zefan 					      void __user *arg)
17930caa102dSLi Zefan {
1794496ad9aaSAl Viro 	struct inode *inode = file_inode(file);
17950caa102dSLi Zefan 	struct btrfs_root *root = BTRFS_I(inode)->root;
17960caa102dSLi Zefan 	struct btrfs_trans_handle *trans;
17970caa102dSLi Zefan 	u64 root_flags;
17980caa102dSLi Zefan 	u64 flags;
17990caa102dSLi Zefan 	int ret = 0;
18000caa102dSLi Zefan 
1801bd60ea0fSDavid Sterba 	if (!inode_owner_or_capable(inode))
1802bd60ea0fSDavid Sterba 		return -EPERM;
1803bd60ea0fSDavid Sterba 
1804b9ca0664SLiu Bo 	ret = mnt_want_write_file(file);
1805b9ca0664SLiu Bo 	if (ret)
1806b9ca0664SLiu Bo 		goto out;
18070caa102dSLi Zefan 
1808b9ca0664SLiu Bo 	if (btrfs_ino(inode) != BTRFS_FIRST_FREE_OBJECTID) {
1809b9ca0664SLiu Bo 		ret = -EINVAL;
1810b9ca0664SLiu Bo 		goto out_drop_write;
1811b9ca0664SLiu Bo 	}
18120caa102dSLi Zefan 
1813b9ca0664SLiu Bo 	if (copy_from_user(&flags, arg, sizeof(flags))) {
1814b9ca0664SLiu Bo 		ret = -EFAULT;
1815b9ca0664SLiu Bo 		goto out_drop_write;
1816b9ca0664SLiu Bo 	}
18170caa102dSLi Zefan 
1818b9ca0664SLiu Bo 	if (flags & BTRFS_SUBVOL_CREATE_ASYNC) {
1819b9ca0664SLiu Bo 		ret = -EINVAL;
1820b9ca0664SLiu Bo 		goto out_drop_write;
1821b9ca0664SLiu Bo 	}
18220caa102dSLi Zefan 
1823b9ca0664SLiu Bo 	if (flags & ~BTRFS_SUBVOL_RDONLY) {
1824b9ca0664SLiu Bo 		ret = -EOPNOTSUPP;
1825b9ca0664SLiu Bo 		goto out_drop_write;
1826b9ca0664SLiu Bo 	}
18270caa102dSLi Zefan 
18280caa102dSLi Zefan 	down_write(&root->fs_info->subvol_sem);
18290caa102dSLi Zefan 
18300caa102dSLi Zefan 	/* nothing to do */
18310caa102dSLi Zefan 	if (!!(flags & BTRFS_SUBVOL_RDONLY) == btrfs_root_readonly(root))
1832b9ca0664SLiu Bo 		goto out_drop_sem;
18330caa102dSLi Zefan 
18340caa102dSLi Zefan 	root_flags = btrfs_root_flags(&root->root_item);
18352c686537SDavid Sterba 	if (flags & BTRFS_SUBVOL_RDONLY) {
18360caa102dSLi Zefan 		btrfs_set_root_flags(&root->root_item,
18370caa102dSLi Zefan 				     root_flags | BTRFS_ROOT_SUBVOL_RDONLY);
18382c686537SDavid Sterba 	} else {
18392c686537SDavid Sterba 		/*
18402c686537SDavid Sterba 		 * Block RO -> RW transition if this subvolume is involved in
18412c686537SDavid Sterba 		 * send
18422c686537SDavid Sterba 		 */
18432c686537SDavid Sterba 		spin_lock(&root->root_item_lock);
18442c686537SDavid Sterba 		if (root->send_in_progress == 0) {
18450caa102dSLi Zefan 			btrfs_set_root_flags(&root->root_item,
18460caa102dSLi Zefan 				     root_flags & ~BTRFS_ROOT_SUBVOL_RDONLY);
18472c686537SDavid Sterba 			spin_unlock(&root->root_item_lock);
18482c686537SDavid Sterba 		} else {
18492c686537SDavid Sterba 			spin_unlock(&root->root_item_lock);
18502c686537SDavid Sterba 			btrfs_warn(root->fs_info,
18512c686537SDavid Sterba 			"Attempt to set subvolume %llu read-write during send",
18522c686537SDavid Sterba 					root->root_key.objectid);
18532c686537SDavid Sterba 			ret = -EPERM;
18542c686537SDavid Sterba 			goto out_drop_sem;
18552c686537SDavid Sterba 		}
18562c686537SDavid Sterba 	}
18570caa102dSLi Zefan 
18580caa102dSLi Zefan 	trans = btrfs_start_transaction(root, 1);
18590caa102dSLi Zefan 	if (IS_ERR(trans)) {
18600caa102dSLi Zefan 		ret = PTR_ERR(trans);
18610caa102dSLi Zefan 		goto out_reset;
18620caa102dSLi Zefan 	}
18630caa102dSLi Zefan 
1864b4dc2b8cSLi Zefan 	ret = btrfs_update_root(trans, root->fs_info->tree_root,
18650caa102dSLi Zefan 				&root->root_key, &root->root_item);
18660caa102dSLi Zefan 
18670caa102dSLi Zefan 	btrfs_commit_transaction(trans, root);
18680caa102dSLi Zefan out_reset:
18690caa102dSLi Zefan 	if (ret)
18700caa102dSLi Zefan 		btrfs_set_root_flags(&root->root_item, root_flags);
1871b9ca0664SLiu Bo out_drop_sem:
18720caa102dSLi Zefan 	up_write(&root->fs_info->subvol_sem);
1873b9ca0664SLiu Bo out_drop_write:
1874b9ca0664SLiu Bo 	mnt_drop_write_file(file);
1875b9ca0664SLiu Bo out:
18760caa102dSLi Zefan 	return ret;
18770caa102dSLi Zefan }
18780caa102dSLi Zefan 
187976dda93cSYan, Zheng /*
188076dda93cSYan, Zheng  * helper to check if the subvolume references other subvolumes
188176dda93cSYan, Zheng  */
188276dda93cSYan, Zheng static noinline int may_destroy_subvol(struct btrfs_root *root)
188376dda93cSYan, Zheng {
188476dda93cSYan, Zheng 	struct btrfs_path *path;
1885175a2b87SJosef Bacik 	struct btrfs_dir_item *di;
188676dda93cSYan, Zheng 	struct btrfs_key key;
1887175a2b87SJosef Bacik 	u64 dir_id;
188876dda93cSYan, Zheng 	int ret;
188976dda93cSYan, Zheng 
189076dda93cSYan, Zheng 	path = btrfs_alloc_path();
189176dda93cSYan, Zheng 	if (!path)
189276dda93cSYan, Zheng 		return -ENOMEM;
189376dda93cSYan, Zheng 
1894175a2b87SJosef Bacik 	/* Make sure this root isn't set as the default subvol */
1895175a2b87SJosef Bacik 	dir_id = btrfs_super_root_dir(root->fs_info->super_copy);
1896175a2b87SJosef Bacik 	di = btrfs_lookup_dir_item(NULL, root->fs_info->tree_root, path,
1897175a2b87SJosef Bacik 				   dir_id, "default", 7, 0);
1898175a2b87SJosef Bacik 	if (di && !IS_ERR(di)) {
1899175a2b87SJosef Bacik 		btrfs_dir_item_key_to_cpu(path->nodes[0], di, &key);
1900175a2b87SJosef Bacik 		if (key.objectid == root->root_key.objectid) {
190172de6b53SGuangyu Sun 			ret = -EPERM;
190272de6b53SGuangyu Sun 			btrfs_err(root->fs_info, "deleting default subvolume "
190372de6b53SGuangyu Sun 				  "%llu is not allowed", key.objectid);
1904175a2b87SJosef Bacik 			goto out;
1905175a2b87SJosef Bacik 		}
1906175a2b87SJosef Bacik 		btrfs_release_path(path);
1907175a2b87SJosef Bacik 	}
1908175a2b87SJosef Bacik 
190976dda93cSYan, Zheng 	key.objectid = root->root_key.objectid;
191076dda93cSYan, Zheng 	key.type = BTRFS_ROOT_REF_KEY;
191176dda93cSYan, Zheng 	key.offset = (u64)-1;
191276dda93cSYan, Zheng 
191376dda93cSYan, Zheng 	ret = btrfs_search_slot(NULL, root->fs_info->tree_root,
191476dda93cSYan, Zheng 				&key, path, 0, 0);
191576dda93cSYan, Zheng 	if (ret < 0)
191676dda93cSYan, Zheng 		goto out;
191776dda93cSYan, Zheng 	BUG_ON(ret == 0);
191876dda93cSYan, Zheng 
191976dda93cSYan, Zheng 	ret = 0;
192076dda93cSYan, Zheng 	if (path->slots[0] > 0) {
192176dda93cSYan, Zheng 		path->slots[0]--;
192276dda93cSYan, Zheng 		btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
192376dda93cSYan, Zheng 		if (key.objectid == root->root_key.objectid &&
192476dda93cSYan, Zheng 		    key.type == BTRFS_ROOT_REF_KEY)
192576dda93cSYan, Zheng 			ret = -ENOTEMPTY;
192676dda93cSYan, Zheng 	}
192776dda93cSYan, Zheng out:
192876dda93cSYan, Zheng 	btrfs_free_path(path);
192976dda93cSYan, Zheng 	return ret;
193076dda93cSYan, Zheng }
193176dda93cSYan, Zheng 
1932ac8e9819SChris Mason static noinline int key_in_sk(struct btrfs_key *key,
1933ac8e9819SChris Mason 			      struct btrfs_ioctl_search_key *sk)
1934ac8e9819SChris Mason {
1935abc6e134SChris Mason 	struct btrfs_key test;
1936abc6e134SChris Mason 	int ret;
1937abc6e134SChris Mason 
1938abc6e134SChris Mason 	test.objectid = sk->min_objectid;
1939abc6e134SChris Mason 	test.type = sk->min_type;
1940abc6e134SChris Mason 	test.offset = sk->min_offset;
1941abc6e134SChris Mason 
1942abc6e134SChris Mason 	ret = btrfs_comp_cpu_keys(key, &test);
1943abc6e134SChris Mason 	if (ret < 0)
1944ac8e9819SChris Mason 		return 0;
1945abc6e134SChris Mason 
1946abc6e134SChris Mason 	test.objectid = sk->max_objectid;
1947abc6e134SChris Mason 	test.type = sk->max_type;
1948abc6e134SChris Mason 	test.offset = sk->max_offset;
1949abc6e134SChris Mason 
1950abc6e134SChris Mason 	ret = btrfs_comp_cpu_keys(key, &test);
1951abc6e134SChris Mason 	if (ret > 0)
1952ac8e9819SChris Mason 		return 0;
1953ac8e9819SChris Mason 	return 1;
1954ac8e9819SChris Mason }
1955ac8e9819SChris Mason 
1956ac8e9819SChris Mason static noinline int copy_to_sk(struct btrfs_root *root,
1957ac8e9819SChris Mason 			       struct btrfs_path *path,
1958ac8e9819SChris Mason 			       struct btrfs_key *key,
1959ac8e9819SChris Mason 			       struct btrfs_ioctl_search_key *sk,
19609b6e817dSGerhard Heift 			       size_t *buf_size,
1961ba346b35SGerhard Heift 			       char __user *ubuf,
1962ac8e9819SChris Mason 			       unsigned long *sk_offset,
1963ac8e9819SChris Mason 			       int *num_found)
1964ac8e9819SChris Mason {
1965ac8e9819SChris Mason 	u64 found_transid;
1966ac8e9819SChris Mason 	struct extent_buffer *leaf;
1967ac8e9819SChris Mason 	struct btrfs_ioctl_search_header sh;
1968ac8e9819SChris Mason 	unsigned long item_off;
1969ac8e9819SChris Mason 	unsigned long item_len;
1970ac8e9819SChris Mason 	int nritems;
1971ac8e9819SChris Mason 	int i;
1972ac8e9819SChris Mason 	int slot;
1973ac8e9819SChris Mason 	int ret = 0;
1974ac8e9819SChris Mason 
1975ac8e9819SChris Mason 	leaf = path->nodes[0];
1976ac8e9819SChris Mason 	slot = path->slots[0];
1977ac8e9819SChris Mason 	nritems = btrfs_header_nritems(leaf);
1978ac8e9819SChris Mason 
1979ac8e9819SChris Mason 	if (btrfs_header_generation(leaf) > sk->max_transid) {
1980ac8e9819SChris Mason 		i = nritems;
1981ac8e9819SChris Mason 		goto advance_key;
1982ac8e9819SChris Mason 	}
1983ac8e9819SChris Mason 	found_transid = btrfs_header_generation(leaf);
1984ac8e9819SChris Mason 
1985ac8e9819SChris Mason 	for (i = slot; i < nritems; i++) {
1986ac8e9819SChris Mason 		item_off = btrfs_item_ptr_offset(leaf, i);
1987ac8e9819SChris Mason 		item_len = btrfs_item_size_nr(leaf, i);
1988ac8e9819SChris Mason 
198903b71c6cSGabriel de Perthuis 		btrfs_item_key_to_cpu(leaf, key, i);
199003b71c6cSGabriel de Perthuis 		if (!key_in_sk(key, sk))
199103b71c6cSGabriel de Perthuis 			continue;
199203b71c6cSGabriel de Perthuis 
19939b6e817dSGerhard Heift 		if (sizeof(sh) + item_len > *buf_size) {
19948f5f6178SGerhard Heift 			if (*num_found) {
19958f5f6178SGerhard Heift 				ret = 1;
19968f5f6178SGerhard Heift 				goto out;
19978f5f6178SGerhard Heift 			}
19988f5f6178SGerhard Heift 
19998f5f6178SGerhard Heift 			/*
20008f5f6178SGerhard Heift 			 * return one empty item back for v1, which does not
20018f5f6178SGerhard Heift 			 * handle -EOVERFLOW
20028f5f6178SGerhard Heift 			 */
20038f5f6178SGerhard Heift 
20049b6e817dSGerhard Heift 			*buf_size = sizeof(sh) + item_len;
2005ac8e9819SChris Mason 			item_len = 0;
20068f5f6178SGerhard Heift 			ret = -EOVERFLOW;
20078f5f6178SGerhard Heift 		}
2008ac8e9819SChris Mason 
20099b6e817dSGerhard Heift 		if (sizeof(sh) + item_len + *sk_offset > *buf_size) {
2010ac8e9819SChris Mason 			ret = 1;
201125c9bc2eSGerhard Heift 			goto out;
2012ac8e9819SChris Mason 		}
2013ac8e9819SChris Mason 
2014ac8e9819SChris Mason 		sh.objectid = key->objectid;
2015ac8e9819SChris Mason 		sh.offset = key->offset;
2016ac8e9819SChris Mason 		sh.type = key->type;
2017ac8e9819SChris Mason 		sh.len = item_len;
2018ac8e9819SChris Mason 		sh.transid = found_transid;
2019ac8e9819SChris Mason 
2020ac8e9819SChris Mason 		/* copy search result header */
2021ba346b35SGerhard Heift 		if (copy_to_user(ubuf + *sk_offset, &sh, sizeof(sh))) {
2022ba346b35SGerhard Heift 			ret = -EFAULT;
2023ba346b35SGerhard Heift 			goto out;
2024ba346b35SGerhard Heift 		}
2025ba346b35SGerhard Heift 
2026ac8e9819SChris Mason 		*sk_offset += sizeof(sh);
2027ac8e9819SChris Mason 
2028ac8e9819SChris Mason 		if (item_len) {
2029ba346b35SGerhard Heift 			char __user *up = ubuf + *sk_offset;
2030ac8e9819SChris Mason 			/* copy the item */
2031ba346b35SGerhard Heift 			if (read_extent_buffer_to_user(leaf, up,
2032ba346b35SGerhard Heift 						       item_off, item_len)) {
2033ba346b35SGerhard Heift 				ret = -EFAULT;
2034ba346b35SGerhard Heift 				goto out;
2035ba346b35SGerhard Heift 			}
2036ba346b35SGerhard Heift 
2037ac8e9819SChris Mason 			*sk_offset += item_len;
2038ac8e9819SChris Mason 		}
2039e2156867SHugo Mills 		(*num_found)++;
2040ac8e9819SChris Mason 
20418f5f6178SGerhard Heift 		if (ret) /* -EOVERFLOW from above */
20428f5f6178SGerhard Heift 			goto out;
20438f5f6178SGerhard Heift 
204425c9bc2eSGerhard Heift 		if (*num_found >= sk->nr_items) {
204525c9bc2eSGerhard Heift 			ret = 1;
204625c9bc2eSGerhard Heift 			goto out;
204725c9bc2eSGerhard Heift 		}
2048ac8e9819SChris Mason 	}
2049ac8e9819SChris Mason advance_key:
2050ac8e9819SChris Mason 	ret = 0;
2051abc6e134SChris Mason 	if (key->offset < (u64)-1 && key->offset < sk->max_offset)
2052abc6e134SChris Mason 		key->offset++;
2053abc6e134SChris Mason 	else if (key->type < (u8)-1 && key->type < sk->max_type) {
2054abc6e134SChris Mason 		key->offset = 0;
2055abc6e134SChris Mason 		key->type++;
2056abc6e134SChris Mason 	} else if (key->objectid < (u64)-1 && key->objectid < sk->max_objectid) {
2057abc6e134SChris Mason 		key->offset = 0;
2058abc6e134SChris Mason 		key->type = 0;
2059abc6e134SChris Mason 		key->objectid++;
2060abc6e134SChris Mason 	} else
2061abc6e134SChris Mason 		ret = 1;
206225c9bc2eSGerhard Heift out:
2063ba346b35SGerhard Heift 	/*
2064ba346b35SGerhard Heift 	 *  0: all items from this leaf copied, continue with next
2065ba346b35SGerhard Heift 	 *  1: * more items can be copied, but unused buffer is too small
2066ba346b35SGerhard Heift 	 *     * all items were found
2067ba346b35SGerhard Heift 	 *     Either way, it will stops the loop which iterates to the next
2068ba346b35SGerhard Heift 	 *     leaf
2069ba346b35SGerhard Heift 	 *  -EOVERFLOW: item was to large for buffer
2070ba346b35SGerhard Heift 	 *  -EFAULT: could not copy extent buffer back to userspace
2071ba346b35SGerhard Heift 	 */
2072ac8e9819SChris Mason 	return ret;
2073ac8e9819SChris Mason }
2074ac8e9819SChris Mason 
2075ac8e9819SChris Mason static noinline int search_ioctl(struct inode *inode,
207612544442SGerhard Heift 				 struct btrfs_ioctl_search_key *sk,
20779b6e817dSGerhard Heift 				 size_t *buf_size,
2078ba346b35SGerhard Heift 				 char __user *ubuf)
2079ac8e9819SChris Mason {
2080ac8e9819SChris Mason 	struct btrfs_root *root;
2081ac8e9819SChris Mason 	struct btrfs_key key;
2082ac8e9819SChris Mason 	struct btrfs_path *path;
2083ac8e9819SChris Mason 	struct btrfs_fs_info *info = BTRFS_I(inode)->root->fs_info;
2084ac8e9819SChris Mason 	int ret;
2085ac8e9819SChris Mason 	int num_found = 0;
2086ac8e9819SChris Mason 	unsigned long sk_offset = 0;
2087ac8e9819SChris Mason 
20889b6e817dSGerhard Heift 	if (*buf_size < sizeof(struct btrfs_ioctl_search_header)) {
20899b6e817dSGerhard Heift 		*buf_size = sizeof(struct btrfs_ioctl_search_header);
209012544442SGerhard Heift 		return -EOVERFLOW;
20919b6e817dSGerhard Heift 	}
209212544442SGerhard Heift 
2093ac8e9819SChris Mason 	path = btrfs_alloc_path();
2094ac8e9819SChris Mason 	if (!path)
2095ac8e9819SChris Mason 		return -ENOMEM;
2096ac8e9819SChris Mason 
2097ac8e9819SChris Mason 	if (sk->tree_id == 0) {
2098ac8e9819SChris Mason 		/* search the root of the inode that was passed */
2099ac8e9819SChris Mason 		root = BTRFS_I(inode)->root;
2100ac8e9819SChris Mason 	} else {
2101ac8e9819SChris Mason 		key.objectid = sk->tree_id;
2102ac8e9819SChris Mason 		key.type = BTRFS_ROOT_ITEM_KEY;
2103ac8e9819SChris Mason 		key.offset = (u64)-1;
2104ac8e9819SChris Mason 		root = btrfs_read_fs_root_no_name(info, &key);
2105ac8e9819SChris Mason 		if (IS_ERR(root)) {
2106efe120a0SFrank Holton 			printk(KERN_ERR "BTRFS: could not find root %llu\n",
2107ac8e9819SChris Mason 			       sk->tree_id);
2108ac8e9819SChris Mason 			btrfs_free_path(path);
2109ac8e9819SChris Mason 			return -ENOENT;
2110ac8e9819SChris Mason 		}
2111ac8e9819SChris Mason 	}
2112ac8e9819SChris Mason 
2113ac8e9819SChris Mason 	key.objectid = sk->min_objectid;
2114ac8e9819SChris Mason 	key.type = sk->min_type;
2115ac8e9819SChris Mason 	key.offset = sk->min_offset;
2116ac8e9819SChris Mason 
2117ac8e9819SChris Mason 	path->keep_locks = 1;
2118ac8e9819SChris Mason 
2119ac8e9819SChris Mason 	while (1) {
21206174d3cbSFilipe David Borba Manana 		ret = btrfs_search_forward(root, &key, path, sk->min_transid);
2121ac8e9819SChris Mason 		if (ret != 0) {
2122ac8e9819SChris Mason 			if (ret > 0)
2123ac8e9819SChris Mason 				ret = 0;
2124ac8e9819SChris Mason 			goto err;
2125ac8e9819SChris Mason 		}
2126ba346b35SGerhard Heift 		ret = copy_to_sk(root, path, &key, sk, buf_size, ubuf,
2127ac8e9819SChris Mason 				 &sk_offset, &num_found);
2128b3b4aa74SDavid Sterba 		btrfs_release_path(path);
212925c9bc2eSGerhard Heift 		if (ret)
2130ac8e9819SChris Mason 			break;
2131ac8e9819SChris Mason 
2132ac8e9819SChris Mason 	}
21338f5f6178SGerhard Heift 	if (ret > 0)
2134ac8e9819SChris Mason 		ret = 0;
2135ac8e9819SChris Mason err:
2136ac8e9819SChris Mason 	sk->nr_items = num_found;
2137ac8e9819SChris Mason 	btrfs_free_path(path);
2138ac8e9819SChris Mason 	return ret;
2139ac8e9819SChris Mason }
2140ac8e9819SChris Mason 
2141ac8e9819SChris Mason static noinline int btrfs_ioctl_tree_search(struct file *file,
2142ac8e9819SChris Mason 					   void __user *argp)
2143ac8e9819SChris Mason {
2144ba346b35SGerhard Heift 	struct btrfs_ioctl_search_args __user *uargs;
2145ba346b35SGerhard Heift 	struct btrfs_ioctl_search_key sk;
2146ac8e9819SChris Mason 	struct inode *inode;
2147ac8e9819SChris Mason 	int ret;
21489b6e817dSGerhard Heift 	size_t buf_size;
2149ac8e9819SChris Mason 
2150ac8e9819SChris Mason 	if (!capable(CAP_SYS_ADMIN))
2151ac8e9819SChris Mason 		return -EPERM;
2152ac8e9819SChris Mason 
2153ba346b35SGerhard Heift 	uargs = (struct btrfs_ioctl_search_args __user *)argp;
2154ac8e9819SChris Mason 
2155ba346b35SGerhard Heift 	if (copy_from_user(&sk, &uargs->key, sizeof(sk)))
2156ba346b35SGerhard Heift 		return -EFAULT;
2157ba346b35SGerhard Heift 
2158ba346b35SGerhard Heift 	buf_size = sizeof(uargs->buf);
21599b6e817dSGerhard Heift 
2160496ad9aaSAl Viro 	inode = file_inode(file);
2161ba346b35SGerhard Heift 	ret = search_ioctl(inode, &sk, &buf_size, uargs->buf);
21628f5f6178SGerhard Heift 
21638f5f6178SGerhard Heift 	/*
21648f5f6178SGerhard Heift 	 * In the origin implementation an overflow is handled by returning a
21658f5f6178SGerhard Heift 	 * search header with a len of zero, so reset ret.
21668f5f6178SGerhard Heift 	 */
21678f5f6178SGerhard Heift 	if (ret == -EOVERFLOW)
21688f5f6178SGerhard Heift 		ret = 0;
21698f5f6178SGerhard Heift 
2170ba346b35SGerhard Heift 	if (ret == 0 && copy_to_user(&uargs->key, &sk, sizeof(sk)))
2171ac8e9819SChris Mason 		ret = -EFAULT;
2172ac8e9819SChris Mason 	return ret;
2173ac8e9819SChris Mason }
2174ac8e9819SChris Mason 
2175*cc68a8a5SGerhard Heift static noinline int btrfs_ioctl_tree_search_v2(struct file *file,
2176*cc68a8a5SGerhard Heift 					       void __user *argp)
2177*cc68a8a5SGerhard Heift {
2178*cc68a8a5SGerhard Heift 	struct btrfs_ioctl_search_args_v2 __user *uarg;
2179*cc68a8a5SGerhard Heift 	struct btrfs_ioctl_search_args_v2 args;
2180*cc68a8a5SGerhard Heift 	struct inode *inode;
2181*cc68a8a5SGerhard Heift 	int ret;
2182*cc68a8a5SGerhard Heift 	size_t buf_size;
2183*cc68a8a5SGerhard Heift 	const size_t buf_limit = 16 * 1024 * 1024;
2184*cc68a8a5SGerhard Heift 
2185*cc68a8a5SGerhard Heift 	if (!capable(CAP_SYS_ADMIN))
2186*cc68a8a5SGerhard Heift 		return -EPERM;
2187*cc68a8a5SGerhard Heift 
2188*cc68a8a5SGerhard Heift 	/* copy search header and buffer size */
2189*cc68a8a5SGerhard Heift 	uarg = (struct btrfs_ioctl_search_args_v2 __user *)argp;
2190*cc68a8a5SGerhard Heift 	if (copy_from_user(&args, uarg, sizeof(args)))
2191*cc68a8a5SGerhard Heift 		return -EFAULT;
2192*cc68a8a5SGerhard Heift 
2193*cc68a8a5SGerhard Heift 	buf_size = args.buf_size;
2194*cc68a8a5SGerhard Heift 
2195*cc68a8a5SGerhard Heift 	if (buf_size < sizeof(struct btrfs_ioctl_search_header))
2196*cc68a8a5SGerhard Heift 		return -EOVERFLOW;
2197*cc68a8a5SGerhard Heift 
2198*cc68a8a5SGerhard Heift 	/* limit result size to 16MB */
2199*cc68a8a5SGerhard Heift 	if (buf_size > buf_limit)
2200*cc68a8a5SGerhard Heift 		buf_size = buf_limit;
2201*cc68a8a5SGerhard Heift 
2202*cc68a8a5SGerhard Heift 	inode = file_inode(file);
2203*cc68a8a5SGerhard Heift 	ret = search_ioctl(inode, &args.key, &buf_size,
2204*cc68a8a5SGerhard Heift 			   (char *)(&uarg->buf[0]));
2205*cc68a8a5SGerhard Heift 	if (ret == 0 && copy_to_user(&uarg->key, &args.key, sizeof(args.key)))
2206*cc68a8a5SGerhard Heift 		ret = -EFAULT;
2207*cc68a8a5SGerhard Heift 	else if (ret == -EOVERFLOW &&
2208*cc68a8a5SGerhard Heift 		copy_to_user(&uarg->buf_size, &buf_size, sizeof(buf_size)))
2209*cc68a8a5SGerhard Heift 		ret = -EFAULT;
2210*cc68a8a5SGerhard Heift 
2211*cc68a8a5SGerhard Heift 	return ret;
2212*cc68a8a5SGerhard Heift }
2213*cc68a8a5SGerhard Heift 
221498d377a0STARUISI Hiroaki /*
2215ac8e9819SChris Mason  * Search INODE_REFs to identify path name of 'dirid' directory
2216ac8e9819SChris Mason  * in a 'tree_id' tree. and sets path name to 'name'.
221798d377a0STARUISI Hiroaki  */
221898d377a0STARUISI Hiroaki static noinline int btrfs_search_path_in_tree(struct btrfs_fs_info *info,
221998d377a0STARUISI Hiroaki 				u64 tree_id, u64 dirid, char *name)
222098d377a0STARUISI Hiroaki {
222198d377a0STARUISI Hiroaki 	struct btrfs_root *root;
222298d377a0STARUISI Hiroaki 	struct btrfs_key key;
2223ac8e9819SChris Mason 	char *ptr;
222498d377a0STARUISI Hiroaki 	int ret = -1;
222598d377a0STARUISI Hiroaki 	int slot;
222698d377a0STARUISI Hiroaki 	int len;
222798d377a0STARUISI Hiroaki 	int total_len = 0;
222898d377a0STARUISI Hiroaki 	struct btrfs_inode_ref *iref;
222998d377a0STARUISI Hiroaki 	struct extent_buffer *l;
223098d377a0STARUISI Hiroaki 	struct btrfs_path *path;
223198d377a0STARUISI Hiroaki 
223298d377a0STARUISI Hiroaki 	if (dirid == BTRFS_FIRST_FREE_OBJECTID) {
223398d377a0STARUISI Hiroaki 		name[0]='\0';
223498d377a0STARUISI Hiroaki 		return 0;
223598d377a0STARUISI Hiroaki 	}
223698d377a0STARUISI Hiroaki 
223798d377a0STARUISI Hiroaki 	path = btrfs_alloc_path();
223898d377a0STARUISI Hiroaki 	if (!path)
223998d377a0STARUISI Hiroaki 		return -ENOMEM;
224098d377a0STARUISI Hiroaki 
2241ac8e9819SChris Mason 	ptr = &name[BTRFS_INO_LOOKUP_PATH_MAX];
224298d377a0STARUISI Hiroaki 
224398d377a0STARUISI Hiroaki 	key.objectid = tree_id;
224498d377a0STARUISI Hiroaki 	key.type = BTRFS_ROOT_ITEM_KEY;
224598d377a0STARUISI Hiroaki 	key.offset = (u64)-1;
224698d377a0STARUISI Hiroaki 	root = btrfs_read_fs_root_no_name(info, &key);
224798d377a0STARUISI Hiroaki 	if (IS_ERR(root)) {
2248efe120a0SFrank Holton 		printk(KERN_ERR "BTRFS: could not find root %llu\n", tree_id);
22498ad6fcabSChris Mason 		ret = -ENOENT;
22508ad6fcabSChris Mason 		goto out;
225198d377a0STARUISI Hiroaki 	}
225298d377a0STARUISI Hiroaki 
225398d377a0STARUISI Hiroaki 	key.objectid = dirid;
225498d377a0STARUISI Hiroaki 	key.type = BTRFS_INODE_REF_KEY;
22558ad6fcabSChris Mason 	key.offset = (u64)-1;
225698d377a0STARUISI Hiroaki 
225798d377a0STARUISI Hiroaki 	while (1) {
225898d377a0STARUISI Hiroaki 		ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
225998d377a0STARUISI Hiroaki 		if (ret < 0)
226098d377a0STARUISI Hiroaki 			goto out;
226118674c6cSFilipe David Borba Manana 		else if (ret > 0) {
226218674c6cSFilipe David Borba Manana 			ret = btrfs_previous_item(root, path, dirid,
226318674c6cSFilipe David Borba Manana 						  BTRFS_INODE_REF_KEY);
226418674c6cSFilipe David Borba Manana 			if (ret < 0)
226518674c6cSFilipe David Borba Manana 				goto out;
226618674c6cSFilipe David Borba Manana 			else if (ret > 0) {
2267ac8e9819SChris Mason 				ret = -ENOENT;
226898d377a0STARUISI Hiroaki 				goto out;
2269ac8e9819SChris Mason 			}
227018674c6cSFilipe David Borba Manana 		}
227118674c6cSFilipe David Borba Manana 
227218674c6cSFilipe David Borba Manana 		l = path->nodes[0];
227318674c6cSFilipe David Borba Manana 		slot = path->slots[0];
227418674c6cSFilipe David Borba Manana 		btrfs_item_key_to_cpu(l, &key, slot);
227598d377a0STARUISI Hiroaki 
227698d377a0STARUISI Hiroaki 		iref = btrfs_item_ptr(l, slot, struct btrfs_inode_ref);
227798d377a0STARUISI Hiroaki 		len = btrfs_inode_ref_name_len(l, iref);
227898d377a0STARUISI Hiroaki 		ptr -= len + 1;
227998d377a0STARUISI Hiroaki 		total_len += len + 1;
2280a696cf35SFilipe David Borba Manana 		if (ptr < name) {
2281a696cf35SFilipe David Borba Manana 			ret = -ENAMETOOLONG;
228298d377a0STARUISI Hiroaki 			goto out;
2283a696cf35SFilipe David Borba Manana 		}
228498d377a0STARUISI Hiroaki 
228598d377a0STARUISI Hiroaki 		*(ptr + len) = '/';
228698d377a0STARUISI Hiroaki 		read_extent_buffer(l, ptr, (unsigned long)(iref + 1), len);
228798d377a0STARUISI Hiroaki 
228898d377a0STARUISI Hiroaki 		if (key.offset == BTRFS_FIRST_FREE_OBJECTID)
228998d377a0STARUISI Hiroaki 			break;
229098d377a0STARUISI Hiroaki 
2291b3b4aa74SDavid Sterba 		btrfs_release_path(path);
229298d377a0STARUISI Hiroaki 		key.objectid = key.offset;
22938ad6fcabSChris Mason 		key.offset = (u64)-1;
229498d377a0STARUISI Hiroaki 		dirid = key.objectid;
229598d377a0STARUISI Hiroaki 	}
229677906a50SLi Zefan 	memmove(name, ptr, total_len);
229798d377a0STARUISI Hiroaki 	name[total_len] = '\0';
229898d377a0STARUISI Hiroaki 	ret = 0;
229998d377a0STARUISI Hiroaki out:
230098d377a0STARUISI Hiroaki 	btrfs_free_path(path);
2301ac8e9819SChris Mason 	return ret;
2302ac8e9819SChris Mason }
2303ac8e9819SChris Mason 
2304ac8e9819SChris Mason static noinline int btrfs_ioctl_ino_lookup(struct file *file,
2305ac8e9819SChris Mason 					   void __user *argp)
2306ac8e9819SChris Mason {
2307ac8e9819SChris Mason 	 struct btrfs_ioctl_ino_lookup_args *args;
2308ac8e9819SChris Mason 	 struct inode *inode;
2309ac8e9819SChris Mason 	 int ret;
2310ac8e9819SChris Mason 
2311ac8e9819SChris Mason 	if (!capable(CAP_SYS_ADMIN))
2312ac8e9819SChris Mason 		return -EPERM;
2313ac8e9819SChris Mason 
23142354d08fSJulia Lawall 	args = memdup_user(argp, sizeof(*args));
23152354d08fSJulia Lawall 	if (IS_ERR(args))
23162354d08fSJulia Lawall 		return PTR_ERR(args);
2317c2b96929SDan Carpenter 
2318496ad9aaSAl Viro 	inode = file_inode(file);
2319ac8e9819SChris Mason 
23201b53ac4dSChris Mason 	if (args->treeid == 0)
23211b53ac4dSChris Mason 		args->treeid = BTRFS_I(inode)->root->root_key.objectid;
23221b53ac4dSChris Mason 
2323ac8e9819SChris Mason 	ret = btrfs_search_path_in_tree(BTRFS_I(inode)->root->fs_info,
2324ac8e9819SChris Mason 					args->treeid, args->objectid,
2325ac8e9819SChris Mason 					args->name);
2326ac8e9819SChris Mason 
2327ac8e9819SChris Mason 	if (ret == 0 && copy_to_user(argp, args, sizeof(*args)))
2328ac8e9819SChris Mason 		ret = -EFAULT;
2329ac8e9819SChris Mason 
2330ac8e9819SChris Mason 	kfree(args);
233198d377a0STARUISI Hiroaki 	return ret;
233298d377a0STARUISI Hiroaki }
233398d377a0STARUISI Hiroaki 
233476dda93cSYan, Zheng static noinline int btrfs_ioctl_snap_destroy(struct file *file,
233576dda93cSYan, Zheng 					     void __user *arg)
233676dda93cSYan, Zheng {
233754563d41SAl Viro 	struct dentry *parent = file->f_path.dentry;
233876dda93cSYan, Zheng 	struct dentry *dentry;
233976dda93cSYan, Zheng 	struct inode *dir = parent->d_inode;
234076dda93cSYan, Zheng 	struct inode *inode;
234176dda93cSYan, Zheng 	struct btrfs_root *root = BTRFS_I(dir)->root;
234276dda93cSYan, Zheng 	struct btrfs_root *dest = NULL;
234376dda93cSYan, Zheng 	struct btrfs_ioctl_vol_args *vol_args;
234476dda93cSYan, Zheng 	struct btrfs_trans_handle *trans;
2345c58aaad2SMiao Xie 	struct btrfs_block_rsv block_rsv;
2346521e0546SDavid Sterba 	u64 root_flags;
2347c58aaad2SMiao Xie 	u64 qgroup_reserved;
234876dda93cSYan, Zheng 	int namelen;
234976dda93cSYan, Zheng 	int ret;
235076dda93cSYan, Zheng 	int err = 0;
235176dda93cSYan, Zheng 
235276dda93cSYan, Zheng 	vol_args = memdup_user(arg, sizeof(*vol_args));
235376dda93cSYan, Zheng 	if (IS_ERR(vol_args))
235476dda93cSYan, Zheng 		return PTR_ERR(vol_args);
235576dda93cSYan, Zheng 
235676dda93cSYan, Zheng 	vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
235776dda93cSYan, Zheng 	namelen = strlen(vol_args->name);
235876dda93cSYan, Zheng 	if (strchr(vol_args->name, '/') ||
235976dda93cSYan, Zheng 	    strncmp(vol_args->name, "..", namelen) == 0) {
236076dda93cSYan, Zheng 		err = -EINVAL;
236176dda93cSYan, Zheng 		goto out;
236276dda93cSYan, Zheng 	}
236376dda93cSYan, Zheng 
2364a561be71SAl Viro 	err = mnt_want_write_file(file);
236576dda93cSYan, Zheng 	if (err)
236676dda93cSYan, Zheng 		goto out;
236776dda93cSYan, Zheng 
2368521e0546SDavid Sterba 
23695c50c9b8SDavid Sterba 	err = mutex_lock_killable_nested(&dir->i_mutex, I_MUTEX_PARENT);
23705c50c9b8SDavid Sterba 	if (err == -EINTR)
2371e43f998eSDavid Sterba 		goto out_drop_write;
237276dda93cSYan, Zheng 	dentry = lookup_one_len(vol_args->name, parent, namelen);
237376dda93cSYan, Zheng 	if (IS_ERR(dentry)) {
237476dda93cSYan, Zheng 		err = PTR_ERR(dentry);
237576dda93cSYan, Zheng 		goto out_unlock_dir;
237676dda93cSYan, Zheng 	}
237776dda93cSYan, Zheng 
237876dda93cSYan, Zheng 	if (!dentry->d_inode) {
237976dda93cSYan, Zheng 		err = -ENOENT;
238076dda93cSYan, Zheng 		goto out_dput;
238176dda93cSYan, Zheng 	}
238276dda93cSYan, Zheng 
238376dda93cSYan, Zheng 	inode = dentry->d_inode;
23844260f7c7SSage Weil 	dest = BTRFS_I(inode)->root;
23854260f7c7SSage Weil 	if (!capable(CAP_SYS_ADMIN)) {
23864260f7c7SSage Weil 		/*
23874260f7c7SSage Weil 		 * Regular user.  Only allow this with a special mount
23884260f7c7SSage Weil 		 * option, when the user has write+exec access to the
23894260f7c7SSage Weil 		 * subvol root, and when rmdir(2) would have been
23904260f7c7SSage Weil 		 * allowed.
23914260f7c7SSage Weil 		 *
23924260f7c7SSage Weil 		 * Note that this is _not_ check that the subvol is
23934260f7c7SSage Weil 		 * empty or doesn't contain data that we wouldn't
23944260f7c7SSage Weil 		 * otherwise be able to delete.
23954260f7c7SSage Weil 		 *
23964260f7c7SSage Weil 		 * Users who want to delete empty subvols should try
23974260f7c7SSage Weil 		 * rmdir(2).
23984260f7c7SSage Weil 		 */
23994260f7c7SSage Weil 		err = -EPERM;
24004260f7c7SSage Weil 		if (!btrfs_test_opt(root, USER_SUBVOL_RM_ALLOWED))
24014260f7c7SSage Weil 			goto out_dput;
24024260f7c7SSage Weil 
24034260f7c7SSage Weil 		/*
24044260f7c7SSage Weil 		 * Do not allow deletion if the parent dir is the same
24054260f7c7SSage Weil 		 * as the dir to be deleted.  That means the ioctl
24064260f7c7SSage Weil 		 * must be called on the dentry referencing the root
24074260f7c7SSage Weil 		 * of the subvol, not a random directory contained
24084260f7c7SSage Weil 		 * within it.
24094260f7c7SSage Weil 		 */
24104260f7c7SSage Weil 		err = -EINVAL;
24114260f7c7SSage Weil 		if (root == dest)
24124260f7c7SSage Weil 			goto out_dput;
24134260f7c7SSage Weil 
24144260f7c7SSage Weil 		err = inode_permission(inode, MAY_WRITE | MAY_EXEC);
24154260f7c7SSage Weil 		if (err)
24164260f7c7SSage Weil 			goto out_dput;
24175c39da5bSMiao Xie 	}
24184260f7c7SSage Weil 
24195c39da5bSMiao Xie 	/* check if subvolume may be deleted by a user */
24204260f7c7SSage Weil 	err = btrfs_may_delete(dir, dentry, 1);
24214260f7c7SSage Weil 	if (err)
24224260f7c7SSage Weil 		goto out_dput;
24234260f7c7SSage Weil 
242433345d01SLi Zefan 	if (btrfs_ino(inode) != BTRFS_FIRST_FREE_OBJECTID) {
242576dda93cSYan, Zheng 		err = -EINVAL;
242676dda93cSYan, Zheng 		goto out_dput;
242776dda93cSYan, Zheng 	}
242876dda93cSYan, Zheng 
242976dda93cSYan, Zheng 	mutex_lock(&inode->i_mutex);
2430521e0546SDavid Sterba 
2431521e0546SDavid Sterba 	/*
2432521e0546SDavid Sterba 	 * Don't allow to delete a subvolume with send in progress. This is
2433521e0546SDavid Sterba 	 * inside the i_mutex so the error handling that has to drop the bit
2434521e0546SDavid Sterba 	 * again is not run concurrently.
2435521e0546SDavid Sterba 	 */
2436521e0546SDavid Sterba 	spin_lock(&dest->root_item_lock);
2437c55bfa67SFilipe Manana 	root_flags = btrfs_root_flags(&dest->root_item);
2438c55bfa67SFilipe Manana 	if (dest->send_in_progress == 0) {
2439c55bfa67SFilipe Manana 		btrfs_set_root_flags(&dest->root_item,
2440521e0546SDavid Sterba 				root_flags | BTRFS_ROOT_SUBVOL_DEAD);
2441521e0546SDavid Sterba 		spin_unlock(&dest->root_item_lock);
2442521e0546SDavid Sterba 	} else {
2443521e0546SDavid Sterba 		spin_unlock(&dest->root_item_lock);
2444521e0546SDavid Sterba 		btrfs_warn(root->fs_info,
2445521e0546SDavid Sterba 			"Attempt to delete subvolume %llu during send",
2446c55bfa67SFilipe Manana 			dest->root_key.objectid);
2447521e0546SDavid Sterba 		err = -EPERM;
2448521e0546SDavid Sterba 		goto out_dput;
2449521e0546SDavid Sterba 	}
2450521e0546SDavid Sterba 
245176dda93cSYan, Zheng 	err = d_invalidate(dentry);
245276dda93cSYan, Zheng 	if (err)
245376dda93cSYan, Zheng 		goto out_unlock;
245476dda93cSYan, Zheng 
245576dda93cSYan, Zheng 	down_write(&root->fs_info->subvol_sem);
245676dda93cSYan, Zheng 
245776dda93cSYan, Zheng 	err = may_destroy_subvol(dest);
245876dda93cSYan, Zheng 	if (err)
245976dda93cSYan, Zheng 		goto out_up_write;
246076dda93cSYan, Zheng 
2461c58aaad2SMiao Xie 	btrfs_init_block_rsv(&block_rsv, BTRFS_BLOCK_RSV_TEMP);
2462c58aaad2SMiao Xie 	/*
2463c58aaad2SMiao Xie 	 * One for dir inode, two for dir entries, two for root
2464c58aaad2SMiao Xie 	 * ref/backref.
2465c58aaad2SMiao Xie 	 */
2466c58aaad2SMiao Xie 	err = btrfs_subvolume_reserve_metadata(root, &block_rsv,
2467ee3441b4SJeff Mahoney 					       5, &qgroup_reserved, true);
2468c58aaad2SMiao Xie 	if (err)
2469c58aaad2SMiao Xie 		goto out_up_write;
2470c58aaad2SMiao Xie 
2471a22285a6SYan, Zheng 	trans = btrfs_start_transaction(root, 0);
2472a22285a6SYan, Zheng 	if (IS_ERR(trans)) {
2473a22285a6SYan, Zheng 		err = PTR_ERR(trans);
2474c58aaad2SMiao Xie 		goto out_release;
2475a22285a6SYan, Zheng 	}
2476c58aaad2SMiao Xie 	trans->block_rsv = &block_rsv;
2477c58aaad2SMiao Xie 	trans->bytes_reserved = block_rsv.size;
2478a22285a6SYan, Zheng 
247976dda93cSYan, Zheng 	ret = btrfs_unlink_subvol(trans, root, dir,
248076dda93cSYan, Zheng 				dest->root_key.objectid,
248176dda93cSYan, Zheng 				dentry->d_name.name,
248276dda93cSYan, Zheng 				dentry->d_name.len);
248379787eaaSJeff Mahoney 	if (ret) {
248479787eaaSJeff Mahoney 		err = ret;
248579787eaaSJeff Mahoney 		btrfs_abort_transaction(trans, root, ret);
248679787eaaSJeff Mahoney 		goto out_end_trans;
248779787eaaSJeff Mahoney 	}
248876dda93cSYan, Zheng 
248976dda93cSYan, Zheng 	btrfs_record_root_in_trans(trans, dest);
249076dda93cSYan, Zheng 
249176dda93cSYan, Zheng 	memset(&dest->root_item.drop_progress, 0,
249276dda93cSYan, Zheng 		sizeof(dest->root_item.drop_progress));
249376dda93cSYan, Zheng 	dest->root_item.drop_level = 0;
249476dda93cSYan, Zheng 	btrfs_set_root_refs(&dest->root_item, 0);
249576dda93cSYan, Zheng 
249627cdeb70SMiao Xie 	if (!test_and_set_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED, &dest->state)) {
249776dda93cSYan, Zheng 		ret = btrfs_insert_orphan_item(trans,
249876dda93cSYan, Zheng 					root->fs_info->tree_root,
249976dda93cSYan, Zheng 					dest->root_key.objectid);
250079787eaaSJeff Mahoney 		if (ret) {
250179787eaaSJeff Mahoney 			btrfs_abort_transaction(trans, root, ret);
250279787eaaSJeff Mahoney 			err = ret;
250379787eaaSJeff Mahoney 			goto out_end_trans;
2504d68fc57bSYan, Zheng 		}
250579787eaaSJeff Mahoney 	}
2506dd5f9615SStefan Behrens 
2507dd5f9615SStefan Behrens 	ret = btrfs_uuid_tree_rem(trans, root->fs_info->uuid_root,
2508dd5f9615SStefan Behrens 				  dest->root_item.uuid, BTRFS_UUID_KEY_SUBVOL,
2509dd5f9615SStefan Behrens 				  dest->root_key.objectid);
2510dd5f9615SStefan Behrens 	if (ret && ret != -ENOENT) {
2511dd5f9615SStefan Behrens 		btrfs_abort_transaction(trans, root, ret);
2512dd5f9615SStefan Behrens 		err = ret;
2513dd5f9615SStefan Behrens 		goto out_end_trans;
2514dd5f9615SStefan Behrens 	}
2515dd5f9615SStefan Behrens 	if (!btrfs_is_empty_uuid(dest->root_item.received_uuid)) {
2516dd5f9615SStefan Behrens 		ret = btrfs_uuid_tree_rem(trans, root->fs_info->uuid_root,
2517dd5f9615SStefan Behrens 					  dest->root_item.received_uuid,
2518dd5f9615SStefan Behrens 					  BTRFS_UUID_KEY_RECEIVED_SUBVOL,
2519dd5f9615SStefan Behrens 					  dest->root_key.objectid);
2520dd5f9615SStefan Behrens 		if (ret && ret != -ENOENT) {
2521dd5f9615SStefan Behrens 			btrfs_abort_transaction(trans, root, ret);
2522dd5f9615SStefan Behrens 			err = ret;
2523dd5f9615SStefan Behrens 			goto out_end_trans;
2524dd5f9615SStefan Behrens 		}
2525dd5f9615SStefan Behrens 	}
2526dd5f9615SStefan Behrens 
252779787eaaSJeff Mahoney out_end_trans:
2528c58aaad2SMiao Xie 	trans->block_rsv = NULL;
2529c58aaad2SMiao Xie 	trans->bytes_reserved = 0;
2530531cb13fSSage Weil 	ret = btrfs_end_transaction(trans, root);
253179787eaaSJeff Mahoney 	if (ret && !err)
253279787eaaSJeff Mahoney 		err = ret;
253376dda93cSYan, Zheng 	inode->i_flags |= S_DEAD;
2534c58aaad2SMiao Xie out_release:
2535c58aaad2SMiao Xie 	btrfs_subvolume_release_metadata(root, &block_rsv, qgroup_reserved);
253676dda93cSYan, Zheng out_up_write:
253776dda93cSYan, Zheng 	up_write(&root->fs_info->subvol_sem);
253876dda93cSYan, Zheng out_unlock:
2539521e0546SDavid Sterba 	if (err) {
2540521e0546SDavid Sterba 		spin_lock(&dest->root_item_lock);
2541c55bfa67SFilipe Manana 		root_flags = btrfs_root_flags(&dest->root_item);
2542c55bfa67SFilipe Manana 		btrfs_set_root_flags(&dest->root_item,
2543521e0546SDavid Sterba 				root_flags & ~BTRFS_ROOT_SUBVOL_DEAD);
2544521e0546SDavid Sterba 		spin_unlock(&dest->root_item_lock);
2545521e0546SDavid Sterba 	}
254676dda93cSYan, Zheng 	mutex_unlock(&inode->i_mutex);
254776dda93cSYan, Zheng 	if (!err) {
2548efefb143SYan, Zheng 		shrink_dcache_sb(root->fs_info->sb);
254976dda93cSYan, Zheng 		btrfs_invalidate_inodes(dest);
255076dda93cSYan, Zheng 		d_delete(dentry);
255161155aa0SDavid Sterba 		ASSERT(dest->send_in_progress == 0);
2552fa6ac876SLiu Bo 
2553fa6ac876SLiu Bo 		/* the last ref */
2554fa6ac876SLiu Bo 		if (dest->cache_inode) {
2555fa6ac876SLiu Bo 			iput(dest->cache_inode);
2556fa6ac876SLiu Bo 			dest->cache_inode = NULL;
2557fa6ac876SLiu Bo 		}
255876dda93cSYan, Zheng 	}
255976dda93cSYan, Zheng out_dput:
256076dda93cSYan, Zheng 	dput(dentry);
256176dda93cSYan, Zheng out_unlock_dir:
256276dda93cSYan, Zheng 	mutex_unlock(&dir->i_mutex);
2563e43f998eSDavid Sterba out_drop_write:
25642a79f17eSAl Viro 	mnt_drop_write_file(file);
256576dda93cSYan, Zheng out:
256676dda93cSYan, Zheng 	kfree(vol_args);
256776dda93cSYan, Zheng 	return err;
256876dda93cSYan, Zheng }
256976dda93cSYan, Zheng 
25701e701a32SChris Mason static int btrfs_ioctl_defrag(struct file *file, void __user *argp)
2571f46b5a66SChristoph Hellwig {
2572496ad9aaSAl Viro 	struct inode *inode = file_inode(file);
2573f46b5a66SChristoph Hellwig 	struct btrfs_root *root = BTRFS_I(inode)->root;
25741e701a32SChris Mason 	struct btrfs_ioctl_defrag_range_args *range;
2575c146afadSYan Zheng 	int ret;
2576c146afadSYan Zheng 
257725122d15SIlya Dryomov 	ret = mnt_want_write_file(file);
257825122d15SIlya Dryomov 	if (ret)
257925122d15SIlya Dryomov 		return ret;
2580b83cc969SLi Zefan 
258125122d15SIlya Dryomov 	if (btrfs_root_readonly(root)) {
258225122d15SIlya Dryomov 		ret = -EROFS;
258325122d15SIlya Dryomov 		goto out;
25845ac00addSStefan Behrens 	}
2585f46b5a66SChristoph Hellwig 
2586f46b5a66SChristoph Hellwig 	switch (inode->i_mode & S_IFMT) {
2587f46b5a66SChristoph Hellwig 	case S_IFDIR:
2588e441d54dSChris Mason 		if (!capable(CAP_SYS_ADMIN)) {
2589e441d54dSChris Mason 			ret = -EPERM;
2590e441d54dSChris Mason 			goto out;
2591e441d54dSChris Mason 		}
2592de78b51aSEric Sandeen 		ret = btrfs_defrag_root(root);
25938929ecfaSYan, Zheng 		if (ret)
25948929ecfaSYan, Zheng 			goto out;
2595de78b51aSEric Sandeen 		ret = btrfs_defrag_root(root->fs_info->extent_root);
2596f46b5a66SChristoph Hellwig 		break;
2597f46b5a66SChristoph Hellwig 	case S_IFREG:
2598e441d54dSChris Mason 		if (!(file->f_mode & FMODE_WRITE)) {
2599e441d54dSChris Mason 			ret = -EINVAL;
2600e441d54dSChris Mason 			goto out;
2601e441d54dSChris Mason 		}
26021e701a32SChris Mason 
26031e701a32SChris Mason 		range = kzalloc(sizeof(*range), GFP_KERNEL);
26041e701a32SChris Mason 		if (!range) {
26051e701a32SChris Mason 			ret = -ENOMEM;
26061e701a32SChris Mason 			goto out;
26071e701a32SChris Mason 		}
26081e701a32SChris Mason 
26091e701a32SChris Mason 		if (argp) {
26101e701a32SChris Mason 			if (copy_from_user(range, argp,
26111e701a32SChris Mason 					   sizeof(*range))) {
26121e701a32SChris Mason 				ret = -EFAULT;
26131e701a32SChris Mason 				kfree(range);
2614683be16eSDan Carpenter 				goto out;
26151e701a32SChris Mason 			}
26161e701a32SChris Mason 			/* compression requires us to start the IO */
26171e701a32SChris Mason 			if ((range->flags & BTRFS_DEFRAG_RANGE_COMPRESS)) {
26181e701a32SChris Mason 				range->flags |= BTRFS_DEFRAG_RANGE_START_IO;
26191e701a32SChris Mason 				range->extent_thresh = (u32)-1;
26201e701a32SChris Mason 			}
26211e701a32SChris Mason 		} else {
26221e701a32SChris Mason 			/* the rest are all set to zero by kzalloc */
26231e701a32SChris Mason 			range->len = (u64)-1;
26241e701a32SChris Mason 		}
2625496ad9aaSAl Viro 		ret = btrfs_defrag_file(file_inode(file), file,
26264cb5300bSChris Mason 					range, 0, 0);
26274cb5300bSChris Mason 		if (ret > 0)
26284cb5300bSChris Mason 			ret = 0;
26291e701a32SChris Mason 		kfree(range);
2630f46b5a66SChristoph Hellwig 		break;
26318929ecfaSYan, Zheng 	default:
26328929ecfaSYan, Zheng 		ret = -EINVAL;
2633f46b5a66SChristoph Hellwig 	}
2634e441d54dSChris Mason out:
263525122d15SIlya Dryomov 	mnt_drop_write_file(file);
2636e441d54dSChris Mason 	return ret;
2637f46b5a66SChristoph Hellwig }
2638f46b5a66SChristoph Hellwig 
2639b2950863SChristoph Hellwig static long btrfs_ioctl_add_dev(struct btrfs_root *root, void __user *arg)
2640f46b5a66SChristoph Hellwig {
2641f46b5a66SChristoph Hellwig 	struct btrfs_ioctl_vol_args *vol_args;
2642f46b5a66SChristoph Hellwig 	int ret;
2643f46b5a66SChristoph Hellwig 
2644e441d54dSChris Mason 	if (!capable(CAP_SYS_ADMIN))
2645e441d54dSChris Mason 		return -EPERM;
2646e441d54dSChris Mason 
26475ac00addSStefan Behrens 	if (atomic_xchg(&root->fs_info->mutually_exclusive_operation_running,
26485ac00addSStefan Behrens 			1)) {
2649e57138b3SAnand Jain 		return BTRFS_ERROR_DEV_EXCL_RUN_IN_PROGRESS;
2650c9e9f97bSIlya Dryomov 	}
2651c9e9f97bSIlya Dryomov 
26525ac00addSStefan Behrens 	mutex_lock(&root->fs_info->volume_mutex);
2653dae7b665SLi Zefan 	vol_args = memdup_user(arg, sizeof(*vol_args));
2654c9e9f97bSIlya Dryomov 	if (IS_ERR(vol_args)) {
2655c9e9f97bSIlya Dryomov 		ret = PTR_ERR(vol_args);
2656c9e9f97bSIlya Dryomov 		goto out;
2657c9e9f97bSIlya Dryomov 	}
2658f46b5a66SChristoph Hellwig 
26595516e595SMark Fasheh 	vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
2660f46b5a66SChristoph Hellwig 	ret = btrfs_init_new_device(root, vol_args->name);
2661f46b5a66SChristoph Hellwig 
2662f46b5a66SChristoph Hellwig 	kfree(vol_args);
2663c9e9f97bSIlya Dryomov out:
2664c9e9f97bSIlya Dryomov 	mutex_unlock(&root->fs_info->volume_mutex);
26655ac00addSStefan Behrens 	atomic_set(&root->fs_info->mutually_exclusive_operation_running, 0);
2666f46b5a66SChristoph Hellwig 	return ret;
2667f46b5a66SChristoph Hellwig }
2668f46b5a66SChristoph Hellwig 
2669da24927bSMiao Xie static long btrfs_ioctl_rm_dev(struct file *file, void __user *arg)
2670f46b5a66SChristoph Hellwig {
2671496ad9aaSAl Viro 	struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
2672f46b5a66SChristoph Hellwig 	struct btrfs_ioctl_vol_args *vol_args;
2673f46b5a66SChristoph Hellwig 	int ret;
2674f46b5a66SChristoph Hellwig 
2675e441d54dSChris Mason 	if (!capable(CAP_SYS_ADMIN))
2676e441d54dSChris Mason 		return -EPERM;
2677e441d54dSChris Mason 
2678da24927bSMiao Xie 	ret = mnt_want_write_file(file);
2679da24927bSMiao Xie 	if (ret)
2680da24927bSMiao Xie 		return ret;
2681c146afadSYan Zheng 
2682dae7b665SLi Zefan 	vol_args = memdup_user(arg, sizeof(*vol_args));
2683c9e9f97bSIlya Dryomov 	if (IS_ERR(vol_args)) {
2684c9e9f97bSIlya Dryomov 		ret = PTR_ERR(vol_args);
2685c9e9f97bSIlya Dryomov 		goto out;
2686c9e9f97bSIlya Dryomov 	}
2687f46b5a66SChristoph Hellwig 
26885516e595SMark Fasheh 	vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
2689f46b5a66SChristoph Hellwig 
2690183860f6SAnand Jain 	if (atomic_xchg(&root->fs_info->mutually_exclusive_operation_running,
2691183860f6SAnand Jain 			1)) {
2692183860f6SAnand Jain 		ret = BTRFS_ERROR_DEV_EXCL_RUN_IN_PROGRESS;
2693183860f6SAnand Jain 		goto out;
2694183860f6SAnand Jain 	}
2695183860f6SAnand Jain 
2696183860f6SAnand Jain 	mutex_lock(&root->fs_info->volume_mutex);
2697183860f6SAnand Jain 	ret = btrfs_rm_device(root, vol_args->name);
2698c9e9f97bSIlya Dryomov 	mutex_unlock(&root->fs_info->volume_mutex);
26995ac00addSStefan Behrens 	atomic_set(&root->fs_info->mutually_exclusive_operation_running, 0);
2700183860f6SAnand Jain 
2701183860f6SAnand Jain out:
2702183860f6SAnand Jain 	kfree(vol_args);
27034ac20c70SIlya Dryomov 	mnt_drop_write_file(file);
2704f46b5a66SChristoph Hellwig 	return ret;
2705f46b5a66SChristoph Hellwig }
2706f46b5a66SChristoph Hellwig 
2707475f6387SJan Schmidt static long btrfs_ioctl_fs_info(struct btrfs_root *root, void __user *arg)
2708475f6387SJan Schmidt {
2709027ed2f0SLi Zefan 	struct btrfs_ioctl_fs_info_args *fi_args;
2710475f6387SJan Schmidt 	struct btrfs_device *device;
2711475f6387SJan Schmidt 	struct btrfs_device *next;
2712475f6387SJan Schmidt 	struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices;
2713027ed2f0SLi Zefan 	int ret = 0;
2714475f6387SJan Schmidt 
2715027ed2f0SLi Zefan 	fi_args = kzalloc(sizeof(*fi_args), GFP_KERNEL);
2716027ed2f0SLi Zefan 	if (!fi_args)
2717027ed2f0SLi Zefan 		return -ENOMEM;
2718027ed2f0SLi Zefan 
2719f7171750SFilipe David Borba Manana 	mutex_lock(&fs_devices->device_list_mutex);
2720027ed2f0SLi Zefan 	fi_args->num_devices = fs_devices->num_devices;
2721027ed2f0SLi Zefan 	memcpy(&fi_args->fsid, root->fs_info->fsid, sizeof(fi_args->fsid));
2722475f6387SJan Schmidt 
2723475f6387SJan Schmidt 	list_for_each_entry_safe(device, next, &fs_devices->devices, dev_list) {
2724027ed2f0SLi Zefan 		if (device->devid > fi_args->max_id)
2725027ed2f0SLi Zefan 			fi_args->max_id = device->devid;
2726475f6387SJan Schmidt 	}
2727475f6387SJan Schmidt 	mutex_unlock(&fs_devices->device_list_mutex);
2728475f6387SJan Schmidt 
272980a773fbSDavid Sterba 	fi_args->nodesize = root->fs_info->super_copy->nodesize;
273080a773fbSDavid Sterba 	fi_args->sectorsize = root->fs_info->super_copy->sectorsize;
273180a773fbSDavid Sterba 	fi_args->clone_alignment = root->fs_info->super_copy->sectorsize;
273280a773fbSDavid Sterba 
2733027ed2f0SLi Zefan 	if (copy_to_user(arg, fi_args, sizeof(*fi_args)))
2734027ed2f0SLi Zefan 		ret = -EFAULT;
2735475f6387SJan Schmidt 
2736027ed2f0SLi Zefan 	kfree(fi_args);
2737027ed2f0SLi Zefan 	return ret;
2738475f6387SJan Schmidt }
2739475f6387SJan Schmidt 
2740475f6387SJan Schmidt static long btrfs_ioctl_dev_info(struct btrfs_root *root, void __user *arg)
2741475f6387SJan Schmidt {
2742475f6387SJan Schmidt 	struct btrfs_ioctl_dev_info_args *di_args;
2743475f6387SJan Schmidt 	struct btrfs_device *dev;
2744475f6387SJan Schmidt 	struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices;
2745475f6387SJan Schmidt 	int ret = 0;
2746475f6387SJan Schmidt 	char *s_uuid = NULL;
2747475f6387SJan Schmidt 
2748475f6387SJan Schmidt 	di_args = memdup_user(arg, sizeof(*di_args));
2749475f6387SJan Schmidt 	if (IS_ERR(di_args))
2750475f6387SJan Schmidt 		return PTR_ERR(di_args);
2751475f6387SJan Schmidt 
2752dd5f9615SStefan Behrens 	if (!btrfs_is_empty_uuid(di_args->uuid))
2753475f6387SJan Schmidt 		s_uuid = di_args->uuid;
2754475f6387SJan Schmidt 
2755475f6387SJan Schmidt 	mutex_lock(&fs_devices->device_list_mutex);
2756aa1b8cd4SStefan Behrens 	dev = btrfs_find_device(root->fs_info, di_args->devid, s_uuid, NULL);
2757475f6387SJan Schmidt 
2758475f6387SJan Schmidt 	if (!dev) {
2759475f6387SJan Schmidt 		ret = -ENODEV;
2760475f6387SJan Schmidt 		goto out;
2761475f6387SJan Schmidt 	}
2762475f6387SJan Schmidt 
2763475f6387SJan Schmidt 	di_args->devid = dev->devid;
2764475f6387SJan Schmidt 	di_args->bytes_used = dev->bytes_used;
2765475f6387SJan Schmidt 	di_args->total_bytes = dev->total_bytes;
2766475f6387SJan Schmidt 	memcpy(di_args->uuid, dev->uuid, sizeof(di_args->uuid));
2767a27202fbSJim Meyering 	if (dev->name) {
2768606686eeSJosef Bacik 		struct rcu_string *name;
2769606686eeSJosef Bacik 
2770606686eeSJosef Bacik 		rcu_read_lock();
2771606686eeSJosef Bacik 		name = rcu_dereference(dev->name);
2772606686eeSJosef Bacik 		strncpy(di_args->path, name->str, sizeof(di_args->path));
2773606686eeSJosef Bacik 		rcu_read_unlock();
2774a27202fbSJim Meyering 		di_args->path[sizeof(di_args->path) - 1] = 0;
2775a27202fbSJim Meyering 	} else {
277699ba55adSStefan Behrens 		di_args->path[0] = '\0';
2777a27202fbSJim Meyering 	}
2778475f6387SJan Schmidt 
2779475f6387SJan Schmidt out:
278055793c0dSDavid Sterba 	mutex_unlock(&fs_devices->device_list_mutex);
2781475f6387SJan Schmidt 	if (ret == 0 && copy_to_user(arg, di_args, sizeof(*di_args)))
2782475f6387SJan Schmidt 		ret = -EFAULT;
2783475f6387SJan Schmidt 
2784475f6387SJan Schmidt 	kfree(di_args);
2785475f6387SJan Schmidt 	return ret;
2786475f6387SJan Schmidt }
2787475f6387SJan Schmidt 
2788416161dbSMark Fasheh static struct page *extent_same_get_page(struct inode *inode, u64 off)
2789416161dbSMark Fasheh {
2790416161dbSMark Fasheh 	struct page *page;
2791416161dbSMark Fasheh 	pgoff_t index;
2792416161dbSMark Fasheh 	struct extent_io_tree *tree = &BTRFS_I(inode)->io_tree;
2793416161dbSMark Fasheh 
2794416161dbSMark Fasheh 	index = off >> PAGE_CACHE_SHIFT;
2795416161dbSMark Fasheh 
2796416161dbSMark Fasheh 	page = grab_cache_page(inode->i_mapping, index);
2797416161dbSMark Fasheh 	if (!page)
2798416161dbSMark Fasheh 		return NULL;
2799416161dbSMark Fasheh 
2800416161dbSMark Fasheh 	if (!PageUptodate(page)) {
2801416161dbSMark Fasheh 		if (extent_read_full_page_nolock(tree, page, btrfs_get_extent,
2802416161dbSMark Fasheh 						 0))
2803416161dbSMark Fasheh 			return NULL;
2804416161dbSMark Fasheh 		lock_page(page);
2805416161dbSMark Fasheh 		if (!PageUptodate(page)) {
2806416161dbSMark Fasheh 			unlock_page(page);
2807416161dbSMark Fasheh 			page_cache_release(page);
2808416161dbSMark Fasheh 			return NULL;
2809416161dbSMark Fasheh 		}
2810416161dbSMark Fasheh 	}
2811416161dbSMark Fasheh 	unlock_page(page);
2812416161dbSMark Fasheh 
2813416161dbSMark Fasheh 	return page;
2814416161dbSMark Fasheh }
2815416161dbSMark Fasheh 
281677fe20dcSMark Fasheh static inline void lock_extent_range(struct inode *inode, u64 off, u64 len)
281777fe20dcSMark Fasheh {
281877fe20dcSMark Fasheh 	/* do any pending delalloc/csum calc on src, one way or
281977fe20dcSMark Fasheh 	   another, and lock file content */
282077fe20dcSMark Fasheh 	while (1) {
282177fe20dcSMark Fasheh 		struct btrfs_ordered_extent *ordered;
282277fe20dcSMark Fasheh 		lock_extent(&BTRFS_I(inode)->io_tree, off, off + len - 1);
282377fe20dcSMark Fasheh 		ordered = btrfs_lookup_first_ordered_extent(inode,
282477fe20dcSMark Fasheh 							    off + len - 1);
2825ff5df9b8SFilipe Manana 		if ((!ordered ||
2826ff5df9b8SFilipe Manana 		     ordered->file_offset + ordered->len <= off ||
2827ff5df9b8SFilipe Manana 		     ordered->file_offset >= off + len) &&
282877fe20dcSMark Fasheh 		    !test_range_bit(&BTRFS_I(inode)->io_tree, off,
2829ff5df9b8SFilipe Manana 				    off + len - 1, EXTENT_DELALLOC, 0, NULL)) {
2830ff5df9b8SFilipe Manana 			if (ordered)
2831ff5df9b8SFilipe Manana 				btrfs_put_ordered_extent(ordered);
283277fe20dcSMark Fasheh 			break;
2833ff5df9b8SFilipe Manana 		}
283477fe20dcSMark Fasheh 		unlock_extent(&BTRFS_I(inode)->io_tree, off, off + len - 1);
283577fe20dcSMark Fasheh 		if (ordered)
283677fe20dcSMark Fasheh 			btrfs_put_ordered_extent(ordered);
283777fe20dcSMark Fasheh 		btrfs_wait_ordered_range(inode, off, len);
283877fe20dcSMark Fasheh 	}
283977fe20dcSMark Fasheh }
284077fe20dcSMark Fasheh 
2841416161dbSMark Fasheh static void btrfs_double_unlock(struct inode *inode1, u64 loff1,
2842416161dbSMark Fasheh 				struct inode *inode2, u64 loff2, u64 len)
2843416161dbSMark Fasheh {
2844416161dbSMark Fasheh 	unlock_extent(&BTRFS_I(inode1)->io_tree, loff1, loff1 + len - 1);
2845416161dbSMark Fasheh 	unlock_extent(&BTRFS_I(inode2)->io_tree, loff2, loff2 + len - 1);
2846416161dbSMark Fasheh 
2847416161dbSMark Fasheh 	mutex_unlock(&inode1->i_mutex);
2848416161dbSMark Fasheh 	mutex_unlock(&inode2->i_mutex);
2849416161dbSMark Fasheh }
2850416161dbSMark Fasheh 
2851416161dbSMark Fasheh static void btrfs_double_lock(struct inode *inode1, u64 loff1,
2852416161dbSMark Fasheh 			      struct inode *inode2, u64 loff2, u64 len)
2853416161dbSMark Fasheh {
2854416161dbSMark Fasheh 	if (inode1 < inode2) {
2855416161dbSMark Fasheh 		swap(inode1, inode2);
2856416161dbSMark Fasheh 		swap(loff1, loff2);
2857416161dbSMark Fasheh 	}
2858416161dbSMark Fasheh 
2859416161dbSMark Fasheh 	mutex_lock_nested(&inode1->i_mutex, I_MUTEX_PARENT);
2860416161dbSMark Fasheh 	lock_extent_range(inode1, loff1, len);
2861416161dbSMark Fasheh 	if (inode1 != inode2) {
2862416161dbSMark Fasheh 		mutex_lock_nested(&inode2->i_mutex, I_MUTEX_CHILD);
2863416161dbSMark Fasheh 		lock_extent_range(inode2, loff2, len);
2864416161dbSMark Fasheh 	}
2865416161dbSMark Fasheh }
2866416161dbSMark Fasheh 
2867416161dbSMark Fasheh static int btrfs_cmp_data(struct inode *src, u64 loff, struct inode *dst,
2868416161dbSMark Fasheh 			  u64 dst_loff, u64 len)
2869416161dbSMark Fasheh {
2870416161dbSMark Fasheh 	int ret = 0;
2871416161dbSMark Fasheh 	struct page *src_page, *dst_page;
2872416161dbSMark Fasheh 	unsigned int cmp_len = PAGE_CACHE_SIZE;
2873416161dbSMark Fasheh 	void *addr, *dst_addr;
2874416161dbSMark Fasheh 
2875416161dbSMark Fasheh 	while (len) {
2876416161dbSMark Fasheh 		if (len < PAGE_CACHE_SIZE)
2877416161dbSMark Fasheh 			cmp_len = len;
2878416161dbSMark Fasheh 
2879416161dbSMark Fasheh 		src_page = extent_same_get_page(src, loff);
2880416161dbSMark Fasheh 		if (!src_page)
2881416161dbSMark Fasheh 			return -EINVAL;
2882416161dbSMark Fasheh 		dst_page = extent_same_get_page(dst, dst_loff);
2883416161dbSMark Fasheh 		if (!dst_page) {
2884416161dbSMark Fasheh 			page_cache_release(src_page);
2885416161dbSMark Fasheh 			return -EINVAL;
2886416161dbSMark Fasheh 		}
2887416161dbSMark Fasheh 		addr = kmap_atomic(src_page);
2888416161dbSMark Fasheh 		dst_addr = kmap_atomic(dst_page);
2889416161dbSMark Fasheh 
2890416161dbSMark Fasheh 		flush_dcache_page(src_page);
2891416161dbSMark Fasheh 		flush_dcache_page(dst_page);
2892416161dbSMark Fasheh 
2893416161dbSMark Fasheh 		if (memcmp(addr, dst_addr, cmp_len))
2894416161dbSMark Fasheh 			ret = BTRFS_SAME_DATA_DIFFERS;
2895416161dbSMark Fasheh 
2896416161dbSMark Fasheh 		kunmap_atomic(addr);
2897416161dbSMark Fasheh 		kunmap_atomic(dst_addr);
2898416161dbSMark Fasheh 		page_cache_release(src_page);
2899416161dbSMark Fasheh 		page_cache_release(dst_page);
2900416161dbSMark Fasheh 
2901416161dbSMark Fasheh 		if (ret)
2902416161dbSMark Fasheh 			break;
2903416161dbSMark Fasheh 
2904416161dbSMark Fasheh 		loff += cmp_len;
2905416161dbSMark Fasheh 		dst_loff += cmp_len;
2906416161dbSMark Fasheh 		len -= cmp_len;
2907416161dbSMark Fasheh 	}
2908416161dbSMark Fasheh 
2909416161dbSMark Fasheh 	return ret;
2910416161dbSMark Fasheh }
2911416161dbSMark Fasheh 
2912416161dbSMark Fasheh static int extent_same_check_offsets(struct inode *inode, u64 off, u64 len)
2913416161dbSMark Fasheh {
2914416161dbSMark Fasheh 	u64 bs = BTRFS_I(inode)->root->fs_info->sb->s_blocksize;
2915416161dbSMark Fasheh 
2916416161dbSMark Fasheh 	if (off + len > inode->i_size || off + len < off)
2917416161dbSMark Fasheh 		return -EINVAL;
2918416161dbSMark Fasheh 	/* Check that we are block aligned - btrfs_clone() requires this */
2919416161dbSMark Fasheh 	if (!IS_ALIGNED(off, bs) || !IS_ALIGNED(off + len, bs))
2920416161dbSMark Fasheh 		return -EINVAL;
2921416161dbSMark Fasheh 
2922416161dbSMark Fasheh 	return 0;
2923416161dbSMark Fasheh }
2924416161dbSMark Fasheh 
2925416161dbSMark Fasheh static int btrfs_extent_same(struct inode *src, u64 loff, u64 len,
2926416161dbSMark Fasheh 			     struct inode *dst, u64 dst_loff)
2927416161dbSMark Fasheh {
2928416161dbSMark Fasheh 	int ret;
2929416161dbSMark Fasheh 
2930416161dbSMark Fasheh 	/*
2931416161dbSMark Fasheh 	 * btrfs_clone() can't handle extents in the same file
2932416161dbSMark Fasheh 	 * yet. Once that works, we can drop this check and replace it
2933416161dbSMark Fasheh 	 * with a check for the same inode, but overlapping extents.
2934416161dbSMark Fasheh 	 */
2935416161dbSMark Fasheh 	if (src == dst)
2936416161dbSMark Fasheh 		return -EINVAL;
2937416161dbSMark Fasheh 
2938416161dbSMark Fasheh 	btrfs_double_lock(src, loff, dst, dst_loff, len);
2939416161dbSMark Fasheh 
2940416161dbSMark Fasheh 	ret = extent_same_check_offsets(src, loff, len);
2941416161dbSMark Fasheh 	if (ret)
2942416161dbSMark Fasheh 		goto out_unlock;
2943416161dbSMark Fasheh 
2944416161dbSMark Fasheh 	ret = extent_same_check_offsets(dst, dst_loff, len);
2945416161dbSMark Fasheh 	if (ret)
2946416161dbSMark Fasheh 		goto out_unlock;
2947416161dbSMark Fasheh 
2948416161dbSMark Fasheh 	/* don't make the dst file partly checksummed */
2949416161dbSMark Fasheh 	if ((BTRFS_I(src)->flags & BTRFS_INODE_NODATASUM) !=
2950416161dbSMark Fasheh 	    (BTRFS_I(dst)->flags & BTRFS_INODE_NODATASUM)) {
2951416161dbSMark Fasheh 		ret = -EINVAL;
2952416161dbSMark Fasheh 		goto out_unlock;
2953416161dbSMark Fasheh 	}
2954416161dbSMark Fasheh 
2955416161dbSMark Fasheh 	ret = btrfs_cmp_data(src, loff, dst, dst_loff, len);
2956416161dbSMark Fasheh 	if (ret == 0)
2957416161dbSMark Fasheh 		ret = btrfs_clone(src, dst, loff, len, len, dst_loff);
2958416161dbSMark Fasheh 
2959416161dbSMark Fasheh out_unlock:
2960416161dbSMark Fasheh 	btrfs_double_unlock(src, loff, dst, dst_loff, len);
2961416161dbSMark Fasheh 
2962416161dbSMark Fasheh 	return ret;
2963416161dbSMark Fasheh }
2964416161dbSMark Fasheh 
2965416161dbSMark Fasheh #define BTRFS_MAX_DEDUPE_LEN	(16 * 1024 * 1024)
2966416161dbSMark Fasheh 
2967416161dbSMark Fasheh static long btrfs_ioctl_file_extent_same(struct file *file,
29681c1c8747SAl Viro 			struct btrfs_ioctl_same_args __user *argp)
2969416161dbSMark Fasheh {
2970cbf8b8caSMark Fasheh 	struct btrfs_ioctl_same_args *same;
2971cbf8b8caSMark Fasheh 	struct btrfs_ioctl_same_extent_info *info;
29721c1c8747SAl Viro 	struct inode *src = file_inode(file);
2973416161dbSMark Fasheh 	u64 off;
2974416161dbSMark Fasheh 	u64 len;
2975416161dbSMark Fasheh 	int i;
2976416161dbSMark Fasheh 	int ret;
2977cbf8b8caSMark Fasheh 	unsigned long size;
2978416161dbSMark Fasheh 	u64 bs = BTRFS_I(src)->root->fs_info->sb->s_blocksize;
2979416161dbSMark Fasheh 	bool is_admin = capable(CAP_SYS_ADMIN);
29801c1c8747SAl Viro 	u16 count;
2981416161dbSMark Fasheh 
2982416161dbSMark Fasheh 	if (!(file->f_mode & FMODE_READ))
2983416161dbSMark Fasheh 		return -EINVAL;
2984416161dbSMark Fasheh 
2985416161dbSMark Fasheh 	ret = mnt_want_write_file(file);
2986416161dbSMark Fasheh 	if (ret)
2987416161dbSMark Fasheh 		return ret;
2988416161dbSMark Fasheh 
29891c1c8747SAl Viro 	if (get_user(count, &argp->dest_count)) {
2990416161dbSMark Fasheh 		ret = -EFAULT;
2991416161dbSMark Fasheh 		goto out;
2992416161dbSMark Fasheh 	}
2993416161dbSMark Fasheh 
29941c1c8747SAl Viro 	size = offsetof(struct btrfs_ioctl_same_args __user, info[count]);
2995cbf8b8caSMark Fasheh 
29961c1c8747SAl Viro 	same = memdup_user(argp, size);
2997cbf8b8caSMark Fasheh 
2998229eed43SGeyslan G. Bem 	if (IS_ERR(same)) {
2999229eed43SGeyslan G. Bem 		ret = PTR_ERR(same);
3000cbf8b8caSMark Fasheh 		goto out;
3001cbf8b8caSMark Fasheh 	}
3002cbf8b8caSMark Fasheh 
3003cbf8b8caSMark Fasheh 	off = same->logical_offset;
3004cbf8b8caSMark Fasheh 	len = same->length;
3005416161dbSMark Fasheh 
3006416161dbSMark Fasheh 	/*
3007416161dbSMark Fasheh 	 * Limit the total length we will dedupe for each operation.
3008416161dbSMark Fasheh 	 * This is intended to bound the total time spent in this
3009416161dbSMark Fasheh 	 * ioctl to something sane.
3010416161dbSMark Fasheh 	 */
3011416161dbSMark Fasheh 	if (len > BTRFS_MAX_DEDUPE_LEN)
3012416161dbSMark Fasheh 		len = BTRFS_MAX_DEDUPE_LEN;
3013416161dbSMark Fasheh 
3014416161dbSMark Fasheh 	if (WARN_ON_ONCE(bs < PAGE_CACHE_SIZE)) {
3015416161dbSMark Fasheh 		/*
3016416161dbSMark Fasheh 		 * Btrfs does not support blocksize < page_size. As a
3017416161dbSMark Fasheh 		 * result, btrfs_cmp_data() won't correctly handle
3018416161dbSMark Fasheh 		 * this situation without an update.
3019416161dbSMark Fasheh 		 */
3020416161dbSMark Fasheh 		ret = -EINVAL;
3021416161dbSMark Fasheh 		goto out;
3022416161dbSMark Fasheh 	}
3023416161dbSMark Fasheh 
3024416161dbSMark Fasheh 	ret = -EISDIR;
3025416161dbSMark Fasheh 	if (S_ISDIR(src->i_mode))
3026416161dbSMark Fasheh 		goto out;
3027416161dbSMark Fasheh 
3028416161dbSMark Fasheh 	ret = -EACCES;
3029416161dbSMark Fasheh 	if (!S_ISREG(src->i_mode))
3030416161dbSMark Fasheh 		goto out;
3031416161dbSMark Fasheh 
3032cbf8b8caSMark Fasheh 	/* pre-format output fields to sane values */
30331c1c8747SAl Viro 	for (i = 0; i < count; i++) {
3034cbf8b8caSMark Fasheh 		same->info[i].bytes_deduped = 0ULL;
3035cbf8b8caSMark Fasheh 		same->info[i].status = 0;
3036416161dbSMark Fasheh 	}
3037416161dbSMark Fasheh 
30381c1c8747SAl Viro 	for (i = 0, info = same->info; i < count; i++, info++) {
30391c1c8747SAl Viro 		struct inode *dst;
30401c1c8747SAl Viro 		struct fd dst_file = fdget(info->fd);
30411c1c8747SAl Viro 		if (!dst_file.file) {
3042cbf8b8caSMark Fasheh 			info->status = -EBADF;
30431c1c8747SAl Viro 			continue;
3044416161dbSMark Fasheh 		}
30451c1c8747SAl Viro 		dst = file_inode(dst_file.file);
3046416161dbSMark Fasheh 
30471c1c8747SAl Viro 		if (!(is_admin || (dst_file.file->f_mode & FMODE_WRITE))) {
3048cbf8b8caSMark Fasheh 			info->status = -EINVAL;
30491c1c8747SAl Viro 		} else if (file->f_path.mnt != dst_file.file->f_path.mnt) {
3050cbf8b8caSMark Fasheh 			info->status = -EXDEV;
30511c1c8747SAl Viro 		} else if (S_ISDIR(dst->i_mode)) {
3052cbf8b8caSMark Fasheh 			info->status = -EISDIR;
30531c1c8747SAl Viro 		} else if (!S_ISREG(dst->i_mode)) {
3054cbf8b8caSMark Fasheh 			info->status = -EACCES;
30551c1c8747SAl Viro 		} else {
3056cbf8b8caSMark Fasheh 			info->status = btrfs_extent_same(src, off, len, dst,
3057cbf8b8caSMark Fasheh 							info->logical_offset);
3058cbf8b8caSMark Fasheh 			if (info->status == 0)
3059cbf8b8caSMark Fasheh 				info->bytes_deduped += len;
30601c1c8747SAl Viro 		}
30611c1c8747SAl Viro 		fdput(dst_file);
3062cbf8b8caSMark Fasheh 	}
3063416161dbSMark Fasheh 
3064cbf8b8caSMark Fasheh 	ret = copy_to_user(argp, same, size);
3065cbf8b8caSMark Fasheh 	if (ret)
3066416161dbSMark Fasheh 		ret = -EFAULT;
3067416161dbSMark Fasheh 
3068416161dbSMark Fasheh out:
3069416161dbSMark Fasheh 	mnt_drop_write_file(file);
3070416161dbSMark Fasheh 	return ret;
3071416161dbSMark Fasheh }
3072416161dbSMark Fasheh 
3073fcebe456SJosef Bacik /* Helper to check and see if this root currently has a ref on the given disk
3074fcebe456SJosef Bacik  * bytenr.  If it does then we need to update the quota for this root.  This
3075fcebe456SJosef Bacik  * doesn't do anything if quotas aren't enabled.
3076fcebe456SJosef Bacik  */
3077fcebe456SJosef Bacik static int check_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root,
3078fcebe456SJosef Bacik 		     u64 disko)
3079fcebe456SJosef Bacik {
3080fcebe456SJosef Bacik 	struct seq_list tree_mod_seq_elem = {};
3081fcebe456SJosef Bacik 	struct ulist *roots;
3082fcebe456SJosef Bacik 	struct ulist_iterator uiter;
3083fcebe456SJosef Bacik 	struct ulist_node *root_node = NULL;
3084fcebe456SJosef Bacik 	int ret;
3085fcebe456SJosef Bacik 
3086fcebe456SJosef Bacik 	if (!root->fs_info->quota_enabled)
3087fcebe456SJosef Bacik 		return 1;
3088fcebe456SJosef Bacik 
3089fcebe456SJosef Bacik 	btrfs_get_tree_mod_seq(root->fs_info, &tree_mod_seq_elem);
3090fcebe456SJosef Bacik 	ret = btrfs_find_all_roots(trans, root->fs_info, disko,
3091fcebe456SJosef Bacik 				   tree_mod_seq_elem.seq, &roots);
3092fcebe456SJosef Bacik 	if (ret < 0)
3093fcebe456SJosef Bacik 		goto out;
3094fcebe456SJosef Bacik 	ret = 0;
3095fcebe456SJosef Bacik 	ULIST_ITER_INIT(&uiter);
3096fcebe456SJosef Bacik 	while ((root_node = ulist_next(roots, &uiter))) {
3097fcebe456SJosef Bacik 		if (root_node->val == root->objectid) {
3098fcebe456SJosef Bacik 			ret = 1;
3099fcebe456SJosef Bacik 			break;
3100fcebe456SJosef Bacik 		}
3101fcebe456SJosef Bacik 	}
3102fcebe456SJosef Bacik 	ulist_free(roots);
3103fcebe456SJosef Bacik out:
3104fcebe456SJosef Bacik 	btrfs_put_tree_mod_seq(root->fs_info, &tree_mod_seq_elem);
3105fcebe456SJosef Bacik 	return ret;
3106fcebe456SJosef Bacik }
3107fcebe456SJosef Bacik 
3108f82a9901SFilipe Manana static int clone_finish_inode_update(struct btrfs_trans_handle *trans,
3109f82a9901SFilipe Manana 				     struct inode *inode,
3110f82a9901SFilipe Manana 				     u64 endoff,
3111f82a9901SFilipe Manana 				     const u64 destoff,
3112f82a9901SFilipe Manana 				     const u64 olen)
3113f82a9901SFilipe Manana {
3114f82a9901SFilipe Manana 	struct btrfs_root *root = BTRFS_I(inode)->root;
3115f82a9901SFilipe Manana 	int ret;
3116f82a9901SFilipe Manana 
3117f82a9901SFilipe Manana 	inode_inc_iversion(inode);
3118f82a9901SFilipe Manana 	inode->i_mtime = inode->i_ctime = CURRENT_TIME;
3119f82a9901SFilipe Manana 	/*
3120f82a9901SFilipe Manana 	 * We round up to the block size at eof when determining which
3121f82a9901SFilipe Manana 	 * extents to clone above, but shouldn't round up the file size.
3122f82a9901SFilipe Manana 	 */
3123f82a9901SFilipe Manana 	if (endoff > destoff + olen)
3124f82a9901SFilipe Manana 		endoff = destoff + olen;
3125f82a9901SFilipe Manana 	if (endoff > inode->i_size)
3126f82a9901SFilipe Manana 		btrfs_i_size_write(inode, endoff);
3127f82a9901SFilipe Manana 
3128f82a9901SFilipe Manana 	ret = btrfs_update_inode(trans, root, inode);
3129f82a9901SFilipe Manana 	if (ret) {
3130f82a9901SFilipe Manana 		btrfs_abort_transaction(trans, root, ret);
3131f82a9901SFilipe Manana 		btrfs_end_transaction(trans, root);
3132f82a9901SFilipe Manana 		goto out;
3133f82a9901SFilipe Manana 	}
3134f82a9901SFilipe Manana 	ret = btrfs_end_transaction(trans, root);
3135f82a9901SFilipe Manana out:
3136f82a9901SFilipe Manana 	return ret;
3137f82a9901SFilipe Manana }
3138f82a9901SFilipe Manana 
31397ffbb598SFilipe Manana static void clone_update_extent_map(struct inode *inode,
31407ffbb598SFilipe Manana 				    const struct btrfs_trans_handle *trans,
31417ffbb598SFilipe Manana 				    const struct btrfs_path *path,
31427ffbb598SFilipe Manana 				    struct btrfs_file_extent_item *fi,
31437ffbb598SFilipe Manana 				    const u64 hole_offset,
31447ffbb598SFilipe Manana 				    const u64 hole_len)
31457ffbb598SFilipe Manana {
31467ffbb598SFilipe Manana 	struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
31477ffbb598SFilipe Manana 	struct extent_map *em;
31487ffbb598SFilipe Manana 	int ret;
31497ffbb598SFilipe Manana 
31507ffbb598SFilipe Manana 	em = alloc_extent_map();
31517ffbb598SFilipe Manana 	if (!em) {
31527ffbb598SFilipe Manana 		set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
31537ffbb598SFilipe Manana 			&BTRFS_I(inode)->runtime_flags);
31547ffbb598SFilipe Manana 		return;
31557ffbb598SFilipe Manana 	}
31567ffbb598SFilipe Manana 
31577ffbb598SFilipe Manana 	if (fi) {
31587ffbb598SFilipe Manana 		btrfs_extent_item_to_extent_map(inode, path, fi, false, em);
31597ffbb598SFilipe Manana 		em->generation = -1;
31607ffbb598SFilipe Manana 		if (btrfs_file_extent_type(path->nodes[0], fi) ==
31617ffbb598SFilipe Manana 		    BTRFS_FILE_EXTENT_INLINE)
31627ffbb598SFilipe Manana 			set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
31637ffbb598SFilipe Manana 				&BTRFS_I(inode)->runtime_flags);
31647ffbb598SFilipe Manana 	} else {
31657ffbb598SFilipe Manana 		em->start = hole_offset;
31667ffbb598SFilipe Manana 		em->len = hole_len;
31677ffbb598SFilipe Manana 		em->ram_bytes = em->len;
31687ffbb598SFilipe Manana 		em->orig_start = hole_offset;
31697ffbb598SFilipe Manana 		em->block_start = EXTENT_MAP_HOLE;
31707ffbb598SFilipe Manana 		em->block_len = 0;
31717ffbb598SFilipe Manana 		em->orig_block_len = 0;
31727ffbb598SFilipe Manana 		em->compress_type = BTRFS_COMPRESS_NONE;
31737ffbb598SFilipe Manana 		em->generation = trans->transid;
31747ffbb598SFilipe Manana 	}
31757ffbb598SFilipe Manana 
31767ffbb598SFilipe Manana 	while (1) {
31777ffbb598SFilipe Manana 		write_lock(&em_tree->lock);
31787ffbb598SFilipe Manana 		ret = add_extent_mapping(em_tree, em, 1);
31797ffbb598SFilipe Manana 		write_unlock(&em_tree->lock);
31807ffbb598SFilipe Manana 		if (ret != -EEXIST) {
31817ffbb598SFilipe Manana 			free_extent_map(em);
31827ffbb598SFilipe Manana 			break;
31837ffbb598SFilipe Manana 		}
31847ffbb598SFilipe Manana 		btrfs_drop_extent_cache(inode, em->start,
31857ffbb598SFilipe Manana 					em->start + em->len - 1, 0);
31867ffbb598SFilipe Manana 	}
31877ffbb598SFilipe Manana 
31887ffbb598SFilipe Manana 	if (unlikely(ret))
31897ffbb598SFilipe Manana 		set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
31907ffbb598SFilipe Manana 			&BTRFS_I(inode)->runtime_flags);
31917ffbb598SFilipe Manana }
31927ffbb598SFilipe Manana 
319332b7c687SMark Fasheh /**
319432b7c687SMark Fasheh  * btrfs_clone() - clone a range from inode file to another
319532b7c687SMark Fasheh  *
319632b7c687SMark Fasheh  * @src: Inode to clone from
319732b7c687SMark Fasheh  * @inode: Inode to clone to
319832b7c687SMark Fasheh  * @off: Offset within source to start clone from
319932b7c687SMark Fasheh  * @olen: Original length, passed by user, of range to clone
320032b7c687SMark Fasheh  * @olen_aligned: Block-aligned value of olen, extent_same uses
320132b7c687SMark Fasheh  *               identical values here
320232b7c687SMark Fasheh  * @destoff: Offset within @inode to start clone
320332b7c687SMark Fasheh  */
320432b7c687SMark Fasheh static int btrfs_clone(struct inode *src, struct inode *inode,
3205f82a9901SFilipe Manana 		       const u64 off, const u64 olen, const u64 olen_aligned,
3206f82a9901SFilipe Manana 		       const u64 destoff)
3207f46b5a66SChristoph Hellwig {
3208f46b5a66SChristoph Hellwig 	struct btrfs_root *root = BTRFS_I(inode)->root;
320932b7c687SMark Fasheh 	struct btrfs_path *path = NULL;
3210f46b5a66SChristoph Hellwig 	struct extent_buffer *leaf;
321132b7c687SMark Fasheh 	struct btrfs_trans_handle *trans;
321232b7c687SMark Fasheh 	char *buf = NULL;
3213ae01a0abSYan Zheng 	struct btrfs_key key;
3214f46b5a66SChristoph Hellwig 	u32 nritems;
3215f46b5a66SChristoph Hellwig 	int slot;
3216ae01a0abSYan Zheng 	int ret;
3217fcebe456SJosef Bacik 	int no_quota;
3218f82a9901SFilipe Manana 	const u64 len = olen_aligned;
3219fcebe456SJosef Bacik 	u64 last_disko = 0;
3220f82a9901SFilipe Manana 	u64 last_dest_end = destoff;
3221ae01a0abSYan Zheng 
3222ae01a0abSYan Zheng 	ret = -ENOMEM;
3223ae01a0abSYan Zheng 	buf = vmalloc(btrfs_level_size(root, 0));
3224ae01a0abSYan Zheng 	if (!buf)
322532b7c687SMark Fasheh 		return ret;
3226ae01a0abSYan Zheng 
3227ae01a0abSYan Zheng 	path = btrfs_alloc_path();
3228ae01a0abSYan Zheng 	if (!path) {
3229ae01a0abSYan Zheng 		vfree(buf);
323032b7c687SMark Fasheh 		return ret;
3231ae01a0abSYan Zheng 	}
323232b7c687SMark Fasheh 
3233ae01a0abSYan Zheng 	path->reada = 2;
3234c5c9cd4dSSage Weil 	/* clone data */
323533345d01SLi Zefan 	key.objectid = btrfs_ino(src);
3236ae01a0abSYan Zheng 	key.type = BTRFS_EXTENT_DATA_KEY;
32372c463823SFilipe Manana 	key.offset = off;
3238f46b5a66SChristoph Hellwig 
3239f46b5a66SChristoph Hellwig 	while (1) {
3240f46b5a66SChristoph Hellwig 		/*
3241f46b5a66SChristoph Hellwig 		 * note the key will change type as we walk through the
3242f46b5a66SChristoph Hellwig 		 * tree.
3243f46b5a66SChristoph Hellwig 		 */
3244e4355f34SFilipe David Borba Manana 		path->leave_spinning = 1;
3245362a20c5SDavid Sterba 		ret = btrfs_search_slot(NULL, BTRFS_I(src)->root, &key, path,
3246362a20c5SDavid Sterba 				0, 0);
3247f46b5a66SChristoph Hellwig 		if (ret < 0)
3248f46b5a66SChristoph Hellwig 			goto out;
32492c463823SFilipe Manana 		/*
32502c463823SFilipe Manana 		 * First search, if no extent item that starts at offset off was
32512c463823SFilipe Manana 		 * found but the previous item is an extent item, it's possible
32522c463823SFilipe Manana 		 * it might overlap our target range, therefore process it.
32532c463823SFilipe Manana 		 */
32542c463823SFilipe Manana 		if (key.offset == off && ret > 0 && path->slots[0] > 0) {
32552c463823SFilipe Manana 			btrfs_item_key_to_cpu(path->nodes[0], &key,
32562c463823SFilipe Manana 					      path->slots[0] - 1);
32572c463823SFilipe Manana 			if (key.type == BTRFS_EXTENT_DATA_KEY)
32582c463823SFilipe Manana 				path->slots[0]--;
32592c463823SFilipe Manana 		}
3260f46b5a66SChristoph Hellwig 
3261ae01a0abSYan Zheng 		nritems = btrfs_header_nritems(path->nodes[0]);
3262e4355f34SFilipe David Borba Manana process_slot:
3263fcebe456SJosef Bacik 		no_quota = 1;
3264ae01a0abSYan Zheng 		if (path->slots[0] >= nritems) {
3265362a20c5SDavid Sterba 			ret = btrfs_next_leaf(BTRFS_I(src)->root, path);
3266f46b5a66SChristoph Hellwig 			if (ret < 0)
3267f46b5a66SChristoph Hellwig 				goto out;
3268f46b5a66SChristoph Hellwig 			if (ret > 0)
3269f46b5a66SChristoph Hellwig 				break;
3270ae01a0abSYan Zheng 			nritems = btrfs_header_nritems(path->nodes[0]);
3271f46b5a66SChristoph Hellwig 		}
3272f46b5a66SChristoph Hellwig 		leaf = path->nodes[0];
3273f46b5a66SChristoph Hellwig 		slot = path->slots[0];
3274f46b5a66SChristoph Hellwig 
3275ae01a0abSYan Zheng 		btrfs_item_key_to_cpu(leaf, &key, slot);
3276d20f7043SChris Mason 		if (btrfs_key_type(&key) > BTRFS_EXTENT_DATA_KEY ||
327733345d01SLi Zefan 		    key.objectid != btrfs_ino(src))
3278f46b5a66SChristoph Hellwig 			break;
3279f46b5a66SChristoph Hellwig 
3280c5c9cd4dSSage Weil 		if (btrfs_key_type(&key) == BTRFS_EXTENT_DATA_KEY) {
3281c5c9cd4dSSage Weil 			struct btrfs_file_extent_item *extent;
3282c5c9cd4dSSage Weil 			int type;
328331840ae1SZheng Yan 			u32 size;
328431840ae1SZheng Yan 			struct btrfs_key new_key;
3285c5c9cd4dSSage Weil 			u64 disko = 0, diskl = 0;
3286c5c9cd4dSSage Weil 			u64 datao = 0, datal = 0;
3287c5c9cd4dSSage Weil 			u8 comp;
3288f82a9901SFilipe Manana 			u64 drop_start;
328931840ae1SZheng Yan 
3290c5c9cd4dSSage Weil 			extent = btrfs_item_ptr(leaf, slot,
3291c5c9cd4dSSage Weil 						struct btrfs_file_extent_item);
3292c5c9cd4dSSage Weil 			comp = btrfs_file_extent_compression(leaf, extent);
3293c5c9cd4dSSage Weil 			type = btrfs_file_extent_type(leaf, extent);
3294c8a894d7SChris Mason 			if (type == BTRFS_FILE_EXTENT_REG ||
3295c8a894d7SChris Mason 			    type == BTRFS_FILE_EXTENT_PREALLOC) {
3296d397712bSChris Mason 				disko = btrfs_file_extent_disk_bytenr(leaf,
3297d397712bSChris Mason 								      extent);
3298d397712bSChris Mason 				diskl = btrfs_file_extent_disk_num_bytes(leaf,
3299d397712bSChris Mason 								 extent);
3300c5c9cd4dSSage Weil 				datao = btrfs_file_extent_offset(leaf, extent);
3301d397712bSChris Mason 				datal = btrfs_file_extent_num_bytes(leaf,
3302d397712bSChris Mason 								    extent);
3303c5c9cd4dSSage Weil 			} else if (type == BTRFS_FILE_EXTENT_INLINE) {
3304c5c9cd4dSSage Weil 				/* take upper bound, may be compressed */
3305c5c9cd4dSSage Weil 				datal = btrfs_file_extent_ram_bytes(leaf,
3306c5c9cd4dSSage Weil 								    extent);
3307c5c9cd4dSSage Weil 			}
330831840ae1SZheng Yan 
33092c463823SFilipe Manana 			/*
33102c463823SFilipe Manana 			 * The first search might have left us at an extent
33112c463823SFilipe Manana 			 * item that ends before our target range's start, can
33122c463823SFilipe Manana 			 * happen if we have holes and NO_HOLES feature enabled.
33132c463823SFilipe Manana 			 */
33142c463823SFilipe Manana 			if (key.offset + datal <= off) {
3315e4355f34SFilipe David Borba Manana 				path->slots[0]++;
3316e4355f34SFilipe David Borba Manana 				goto process_slot;
33172c463823SFilipe Manana 			} else if (key.offset >= off + len) {
33182c463823SFilipe Manana 				break;
3319e4355f34SFilipe David Borba Manana 			}
3320e4355f34SFilipe David Borba Manana 
3321e4355f34SFilipe David Borba Manana 			size = btrfs_item_size_nr(leaf, slot);
3322e4355f34SFilipe David Borba Manana 			read_extent_buffer(leaf, buf,
3323e4355f34SFilipe David Borba Manana 					   btrfs_item_ptr_offset(leaf, slot),
3324e4355f34SFilipe David Borba Manana 					   size);
3325e4355f34SFilipe David Borba Manana 
3326e4355f34SFilipe David Borba Manana 			btrfs_release_path(path);
3327e4355f34SFilipe David Borba Manana 			path->leave_spinning = 0;
3328c5c9cd4dSSage Weil 
332931840ae1SZheng Yan 			memcpy(&new_key, &key, sizeof(new_key));
333033345d01SLi Zefan 			new_key.objectid = btrfs_ino(inode);
33314d728ec7SLi Zefan 			if (off <= key.offset)
3332c5c9cd4dSSage Weil 				new_key.offset = key.offset + destoff - off;
33334d728ec7SLi Zefan 			else
33344d728ec7SLi Zefan 				new_key.offset = destoff;
3335c5c9cd4dSSage Weil 
3336b6f3409bSSage Weil 			/*
3337f82a9901SFilipe Manana 			 * Deal with a hole that doesn't have an extent item
3338f82a9901SFilipe Manana 			 * that represents it (NO_HOLES feature enabled).
3339f82a9901SFilipe Manana 			 * This hole is either in the middle of the cloning
3340f82a9901SFilipe Manana 			 * range or at the beginning (fully overlaps it or
3341f82a9901SFilipe Manana 			 * partially overlaps it).
3342f82a9901SFilipe Manana 			 */
3343f82a9901SFilipe Manana 			if (new_key.offset != last_dest_end)
3344f82a9901SFilipe Manana 				drop_start = last_dest_end;
3345f82a9901SFilipe Manana 			else
3346f82a9901SFilipe Manana 				drop_start = new_key.offset;
3347f82a9901SFilipe Manana 
3348f82a9901SFilipe Manana 			/*
3349b6f3409bSSage Weil 			 * 1 - adjusting old extent (we may have to split it)
3350b6f3409bSSage Weil 			 * 1 - add new extent
3351b6f3409bSSage Weil 			 * 1 - inode update
3352b6f3409bSSage Weil 			 */
3353b6f3409bSSage Weil 			trans = btrfs_start_transaction(root, 3);
3354a22285a6SYan, Zheng 			if (IS_ERR(trans)) {
3355a22285a6SYan, Zheng 				ret = PTR_ERR(trans);
3356a22285a6SYan, Zheng 				goto out;
3357a22285a6SYan, Zheng 			}
3358a22285a6SYan, Zheng 
3359c8a894d7SChris Mason 			if (type == BTRFS_FILE_EXTENT_REG ||
3360c8a894d7SChris Mason 			    type == BTRFS_FILE_EXTENT_PREALLOC) {
3361d72c0842SLi Zefan 				/*
3362d72c0842SLi Zefan 				 *    a  | --- range to clone ---|  b
3363d72c0842SLi Zefan 				 * | ------------- extent ------------- |
3364d72c0842SLi Zefan 				 */
3365d72c0842SLi Zefan 
336693915584SAntonio Ospite 				/* subtract range b */
3367d72c0842SLi Zefan 				if (key.offset + datal > off + len)
3368d72c0842SLi Zefan 					datal = off + len - key.offset;
3369d72c0842SLi Zefan 
337093915584SAntonio Ospite 				/* subtract range a */
3371a22285a6SYan, Zheng 				if (off > key.offset) {
3372a22285a6SYan, Zheng 					datao += off - key.offset;
3373a22285a6SYan, Zheng 					datal -= off - key.offset;
3374a22285a6SYan, Zheng 				}
3375a22285a6SYan, Zheng 
33765dc562c5SJosef Bacik 				ret = btrfs_drop_extents(trans, root, inode,
3377f82a9901SFilipe Manana 							 drop_start,
3378a22285a6SYan, Zheng 							 new_key.offset + datal,
33792671485dSJosef Bacik 							 1);
338079787eaaSJeff Mahoney 				if (ret) {
33813f9e3df8SDavid Sterba 					if (ret != -EOPNOTSUPP)
338200fdf13aSLiu Bo 						btrfs_abort_transaction(trans,
338300fdf13aSLiu Bo 								root, ret);
338479787eaaSJeff Mahoney 					btrfs_end_transaction(trans, root);
338579787eaaSJeff Mahoney 					goto out;
338679787eaaSJeff Mahoney 				}
3387a22285a6SYan, Zheng 
338831840ae1SZheng Yan 				ret = btrfs_insert_empty_item(trans, root, path,
338931840ae1SZheng Yan 							      &new_key, size);
339079787eaaSJeff Mahoney 				if (ret) {
339179787eaaSJeff Mahoney 					btrfs_abort_transaction(trans, root,
339279787eaaSJeff Mahoney 								ret);
339379787eaaSJeff Mahoney 					btrfs_end_transaction(trans, root);
339479787eaaSJeff Mahoney 					goto out;
339579787eaaSJeff Mahoney 				}
339631840ae1SZheng Yan 
339731840ae1SZheng Yan 				leaf = path->nodes[0];
339831840ae1SZheng Yan 				slot = path->slots[0];
339931840ae1SZheng Yan 				write_extent_buffer(leaf, buf,
340031840ae1SZheng Yan 					    btrfs_item_ptr_offset(leaf, slot),
340131840ae1SZheng Yan 					    size);
3402ae01a0abSYan Zheng 
3403f46b5a66SChristoph Hellwig 				extent = btrfs_item_ptr(leaf, slot,
3404f46b5a66SChristoph Hellwig 						struct btrfs_file_extent_item);
3405c5c9cd4dSSage Weil 
3406c5c9cd4dSSage Weil 				/* disko == 0 means it's a hole */
3407c5c9cd4dSSage Weil 				if (!disko)
3408c5c9cd4dSSage Weil 					datao = 0;
3409c5c9cd4dSSage Weil 
3410c5c9cd4dSSage Weil 				btrfs_set_file_extent_offset(leaf, extent,
3411c5c9cd4dSSage Weil 							     datao);
3412c5c9cd4dSSage Weil 				btrfs_set_file_extent_num_bytes(leaf, extent,
3413c5c9cd4dSSage Weil 								datal);
3414fcebe456SJosef Bacik 
3415fcebe456SJosef Bacik 				/*
3416fcebe456SJosef Bacik 				 * We need to look up the roots that point at
3417fcebe456SJosef Bacik 				 * this bytenr and see if the new root does.  If
3418fcebe456SJosef Bacik 				 * it does not we need to make sure we update
3419fcebe456SJosef Bacik 				 * quotas appropriately.
3420fcebe456SJosef Bacik 				 */
3421fcebe456SJosef Bacik 				if (disko && root != BTRFS_I(src)->root &&
3422fcebe456SJosef Bacik 				    disko != last_disko) {
3423fcebe456SJosef Bacik 					no_quota = check_ref(trans, root,
3424fcebe456SJosef Bacik 							     disko);
3425fcebe456SJosef Bacik 					if (no_quota < 0) {
3426fcebe456SJosef Bacik 						btrfs_abort_transaction(trans,
3427fcebe456SJosef Bacik 									root,
3428fcebe456SJosef Bacik 									ret);
3429fcebe456SJosef Bacik 						btrfs_end_transaction(trans,
3430fcebe456SJosef Bacik 								      root);
3431fcebe456SJosef Bacik 						ret = no_quota;
3432fcebe456SJosef Bacik 						goto out;
3433fcebe456SJosef Bacik 					}
3434fcebe456SJosef Bacik 				}
3435fcebe456SJosef Bacik 
3436c5c9cd4dSSage Weil 				if (disko) {
3437c5c9cd4dSSage Weil 					inode_add_bytes(inode, datal);
3438ae01a0abSYan Zheng 					ret = btrfs_inc_extent_ref(trans, root,
34395d4f98a2SYan Zheng 							disko, diskl, 0,
3440f46b5a66SChristoph Hellwig 							root->root_key.objectid,
344133345d01SLi Zefan 							btrfs_ino(inode),
344266d7e7f0SArne Jansen 							new_key.offset - datao,
3443fcebe456SJosef Bacik 							no_quota);
344479787eaaSJeff Mahoney 					if (ret) {
344579787eaaSJeff Mahoney 						btrfs_abort_transaction(trans,
344679787eaaSJeff Mahoney 									root,
344779787eaaSJeff Mahoney 									ret);
344879787eaaSJeff Mahoney 						btrfs_end_transaction(trans,
344979787eaaSJeff Mahoney 								      root);
345079787eaaSJeff Mahoney 						goto out;
345179787eaaSJeff Mahoney 
345279787eaaSJeff Mahoney 					}
3453f46b5a66SChristoph Hellwig 				}
3454c5c9cd4dSSage Weil 			} else if (type == BTRFS_FILE_EXTENT_INLINE) {
3455c5c9cd4dSSage Weil 				u64 skip = 0;
3456c5c9cd4dSSage Weil 				u64 trim = 0;
3457d3ecfcdfSLiu Bo 				u64 aligned_end = 0;
3458d3ecfcdfSLiu Bo 
3459c5c9cd4dSSage Weil 				if (off > key.offset) {
3460c5c9cd4dSSage Weil 					skip = off - key.offset;
3461c5c9cd4dSSage Weil 					new_key.offset += skip;
346231840ae1SZheng Yan 				}
3463d397712bSChris Mason 
3464c5c9cd4dSSage Weil 				if (key.offset + datal > off + len)
3465c5c9cd4dSSage Weil 					trim = key.offset + datal - (off + len);
3466d397712bSChris Mason 
3467c5c9cd4dSSage Weil 				if (comp && (skip || trim)) {
3468c5c9cd4dSSage Weil 					ret = -EINVAL;
3469a22285a6SYan, Zheng 					btrfs_end_transaction(trans, root);
3470c5c9cd4dSSage Weil 					goto out;
347131840ae1SZheng Yan 				}
3472c5c9cd4dSSage Weil 				size -= skip + trim;
3473c5c9cd4dSSage Weil 				datal -= skip + trim;
3474a22285a6SYan, Zheng 
3475d3ecfcdfSLiu Bo 				aligned_end = ALIGN(new_key.offset + datal,
3476d3ecfcdfSLiu Bo 						    root->sectorsize);
34775dc562c5SJosef Bacik 				ret = btrfs_drop_extents(trans, root, inode,
3478f82a9901SFilipe Manana 							 drop_start,
3479d3ecfcdfSLiu Bo 							 aligned_end,
34802671485dSJosef Bacik 							 1);
348179787eaaSJeff Mahoney 				if (ret) {
34823f9e3df8SDavid Sterba 					if (ret != -EOPNOTSUPP)
34833a29bc09SChris Mason 						btrfs_abort_transaction(trans,
34843a29bc09SChris Mason 							root, ret);
348579787eaaSJeff Mahoney 					btrfs_end_transaction(trans, root);
348679787eaaSJeff Mahoney 					goto out;
348779787eaaSJeff Mahoney 				}
3488a22285a6SYan, Zheng 
3489c5c9cd4dSSage Weil 				ret = btrfs_insert_empty_item(trans, root, path,
3490c5c9cd4dSSage Weil 							      &new_key, size);
349179787eaaSJeff Mahoney 				if (ret) {
349279787eaaSJeff Mahoney 					btrfs_abort_transaction(trans, root,
349379787eaaSJeff Mahoney 								ret);
349479787eaaSJeff Mahoney 					btrfs_end_transaction(trans, root);
349579787eaaSJeff Mahoney 					goto out;
349679787eaaSJeff Mahoney 				}
3497c5c9cd4dSSage Weil 
3498c5c9cd4dSSage Weil 				if (skip) {
3499d397712bSChris Mason 					u32 start =
3500d397712bSChris Mason 					  btrfs_file_extent_calc_inline_size(0);
3501c5c9cd4dSSage Weil 					memmove(buf+start, buf+start+skip,
3502c5c9cd4dSSage Weil 						datal);
3503c5c9cd4dSSage Weil 				}
3504c5c9cd4dSSage Weil 
3505c5c9cd4dSSage Weil 				leaf = path->nodes[0];
3506c5c9cd4dSSage Weil 				slot = path->slots[0];
3507c5c9cd4dSSage Weil 				write_extent_buffer(leaf, buf,
3508c5c9cd4dSSage Weil 					    btrfs_item_ptr_offset(leaf, slot),
3509c5c9cd4dSSage Weil 					    size);
3510c5c9cd4dSSage Weil 				inode_add_bytes(inode, datal);
35117ffbb598SFilipe Manana 				extent = btrfs_item_ptr(leaf, slot,
35127ffbb598SFilipe Manana 						struct btrfs_file_extent_item);
3513c5c9cd4dSSage Weil 			}
3514c5c9cd4dSSage Weil 
35157ffbb598SFilipe Manana 			/* If we have an implicit hole (NO_HOLES feature). */
35167ffbb598SFilipe Manana 			if (drop_start < new_key.offset)
35177ffbb598SFilipe Manana 				clone_update_extent_map(inode, trans,
35187ffbb598SFilipe Manana 						path, NULL, drop_start,
35197ffbb598SFilipe Manana 						new_key.offset - drop_start);
35207ffbb598SFilipe Manana 
35217ffbb598SFilipe Manana 			clone_update_extent_map(inode, trans, path,
35227ffbb598SFilipe Manana 						extent, 0, 0);
35237ffbb598SFilipe Manana 
3524c5c9cd4dSSage Weil 			btrfs_mark_buffer_dirty(leaf);
3525b3b4aa74SDavid Sterba 			btrfs_release_path(path);
3526c5c9cd4dSSage Weil 
3527f82a9901SFilipe Manana 			last_dest_end = new_key.offset + datal;
3528f82a9901SFilipe Manana 			ret = clone_finish_inode_update(trans, inode,
3529f82a9901SFilipe Manana 							last_dest_end,
3530f82a9901SFilipe Manana 							destoff, olen);
3531f82a9901SFilipe Manana 			if (ret)
353279787eaaSJeff Mahoney 				goto out;
35332c463823SFilipe Manana 			if (new_key.offset + datal >= destoff + len)
35342c463823SFilipe Manana 				break;
3535a22285a6SYan, Zheng 		}
3536b3b4aa74SDavid Sterba 		btrfs_release_path(path);
3537ae01a0abSYan Zheng 		key.offset++;
3538ae01a0abSYan Zheng 	}
3539f46b5a66SChristoph Hellwig 	ret = 0;
354032b7c687SMark Fasheh 
3541f82a9901SFilipe Manana 	if (last_dest_end < destoff + len) {
3542f82a9901SFilipe Manana 		/*
3543f82a9901SFilipe Manana 		 * We have an implicit hole (NO_HOLES feature is enabled) that
3544f82a9901SFilipe Manana 		 * fully or partially overlaps our cloning range at its end.
3545f82a9901SFilipe Manana 		 */
3546f82a9901SFilipe Manana 		btrfs_release_path(path);
3547f82a9901SFilipe Manana 
3548f82a9901SFilipe Manana 		/*
3549f82a9901SFilipe Manana 		 * 1 - remove extent(s)
3550f82a9901SFilipe Manana 		 * 1 - inode update
3551f82a9901SFilipe Manana 		 */
3552f82a9901SFilipe Manana 		trans = btrfs_start_transaction(root, 2);
3553f82a9901SFilipe Manana 		if (IS_ERR(trans)) {
3554f82a9901SFilipe Manana 			ret = PTR_ERR(trans);
3555f82a9901SFilipe Manana 			goto out;
3556f82a9901SFilipe Manana 		}
3557f82a9901SFilipe Manana 		ret = btrfs_drop_extents(trans, root, inode,
3558f82a9901SFilipe Manana 					 last_dest_end, destoff + len, 1);
3559f82a9901SFilipe Manana 		if (ret) {
3560f82a9901SFilipe Manana 			if (ret != -EOPNOTSUPP)
3561f82a9901SFilipe Manana 				btrfs_abort_transaction(trans, root, ret);
3562f82a9901SFilipe Manana 			btrfs_end_transaction(trans, root);
3563f82a9901SFilipe Manana 			goto out;
3564f82a9901SFilipe Manana 		}
3565f82a9901SFilipe Manana 		ret = clone_finish_inode_update(trans, inode, destoff + len,
3566f82a9901SFilipe Manana 						destoff, olen);
35677ffbb598SFilipe Manana 		if (ret)
35687ffbb598SFilipe Manana 			goto out;
35697ffbb598SFilipe Manana 		clone_update_extent_map(inode, trans, path, NULL, last_dest_end,
35707ffbb598SFilipe Manana 					destoff + len - last_dest_end);
3571f82a9901SFilipe Manana 	}
3572f82a9901SFilipe Manana 
3573f46b5a66SChristoph Hellwig out:
357432b7c687SMark Fasheh 	btrfs_free_path(path);
357532b7c687SMark Fasheh 	vfree(buf);
357632b7c687SMark Fasheh 	return ret;
357732b7c687SMark Fasheh }
357832b7c687SMark Fasheh 
357932b7c687SMark Fasheh static noinline long btrfs_ioctl_clone(struct file *file, unsigned long srcfd,
358032b7c687SMark Fasheh 				       u64 off, u64 olen, u64 destoff)
358132b7c687SMark Fasheh {
358254563d41SAl Viro 	struct inode *inode = file_inode(file);
358332b7c687SMark Fasheh 	struct btrfs_root *root = BTRFS_I(inode)->root;
358432b7c687SMark Fasheh 	struct fd src_file;
358532b7c687SMark Fasheh 	struct inode *src;
358632b7c687SMark Fasheh 	int ret;
358732b7c687SMark Fasheh 	u64 len = olen;
358832b7c687SMark Fasheh 	u64 bs = root->fs_info->sb->s_blocksize;
358932b7c687SMark Fasheh 	int same_inode = 0;
359032b7c687SMark Fasheh 
359132b7c687SMark Fasheh 	/*
359232b7c687SMark Fasheh 	 * TODO:
359332b7c687SMark Fasheh 	 * - split compressed inline extents.  annoying: we need to
359432b7c687SMark Fasheh 	 *   decompress into destination's address_space (the file offset
359532b7c687SMark Fasheh 	 *   may change, so source mapping won't do), then recompress (or
359632b7c687SMark Fasheh 	 *   otherwise reinsert) a subrange.
359700fdf13aSLiu Bo 	 *
359800fdf13aSLiu Bo 	 * - split destination inode's inline extents.  The inline extents can
359900fdf13aSLiu Bo 	 *   be either compressed or non-compressed.
360032b7c687SMark Fasheh 	 */
360132b7c687SMark Fasheh 
360232b7c687SMark Fasheh 	/* the destination must be opened for writing */
360332b7c687SMark Fasheh 	if (!(file->f_mode & FMODE_WRITE) || (file->f_flags & O_APPEND))
360432b7c687SMark Fasheh 		return -EINVAL;
360532b7c687SMark Fasheh 
360632b7c687SMark Fasheh 	if (btrfs_root_readonly(root))
360732b7c687SMark Fasheh 		return -EROFS;
360832b7c687SMark Fasheh 
360932b7c687SMark Fasheh 	ret = mnt_want_write_file(file);
361032b7c687SMark Fasheh 	if (ret)
361132b7c687SMark Fasheh 		return ret;
361232b7c687SMark Fasheh 
361332b7c687SMark Fasheh 	src_file = fdget(srcfd);
361432b7c687SMark Fasheh 	if (!src_file.file) {
361532b7c687SMark Fasheh 		ret = -EBADF;
361632b7c687SMark Fasheh 		goto out_drop_write;
361732b7c687SMark Fasheh 	}
361832b7c687SMark Fasheh 
361932b7c687SMark Fasheh 	ret = -EXDEV;
362032b7c687SMark Fasheh 	if (src_file.file->f_path.mnt != file->f_path.mnt)
362132b7c687SMark Fasheh 		goto out_fput;
362232b7c687SMark Fasheh 
362332b7c687SMark Fasheh 	src = file_inode(src_file.file);
362432b7c687SMark Fasheh 
362532b7c687SMark Fasheh 	ret = -EINVAL;
362632b7c687SMark Fasheh 	if (src == inode)
362732b7c687SMark Fasheh 		same_inode = 1;
362832b7c687SMark Fasheh 
362932b7c687SMark Fasheh 	/* the src must be open for reading */
363032b7c687SMark Fasheh 	if (!(src_file.file->f_mode & FMODE_READ))
363132b7c687SMark Fasheh 		goto out_fput;
363232b7c687SMark Fasheh 
363332b7c687SMark Fasheh 	/* don't make the dst file partly checksummed */
363432b7c687SMark Fasheh 	if ((BTRFS_I(src)->flags & BTRFS_INODE_NODATASUM) !=
363532b7c687SMark Fasheh 	    (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM))
363632b7c687SMark Fasheh 		goto out_fput;
363732b7c687SMark Fasheh 
363832b7c687SMark Fasheh 	ret = -EISDIR;
363932b7c687SMark Fasheh 	if (S_ISDIR(src->i_mode) || S_ISDIR(inode->i_mode))
364032b7c687SMark Fasheh 		goto out_fput;
364132b7c687SMark Fasheh 
364232b7c687SMark Fasheh 	ret = -EXDEV;
364332b7c687SMark Fasheh 	if (src->i_sb != inode->i_sb)
364432b7c687SMark Fasheh 		goto out_fput;
364532b7c687SMark Fasheh 
364632b7c687SMark Fasheh 	if (!same_inode) {
364732b7c687SMark Fasheh 		if (inode < src) {
364832b7c687SMark Fasheh 			mutex_lock_nested(&inode->i_mutex, I_MUTEX_PARENT);
364932b7c687SMark Fasheh 			mutex_lock_nested(&src->i_mutex, I_MUTEX_CHILD);
365032b7c687SMark Fasheh 		} else {
365132b7c687SMark Fasheh 			mutex_lock_nested(&src->i_mutex, I_MUTEX_PARENT);
365232b7c687SMark Fasheh 			mutex_lock_nested(&inode->i_mutex, I_MUTEX_CHILD);
365332b7c687SMark Fasheh 		}
365432b7c687SMark Fasheh 	} else {
365532b7c687SMark Fasheh 		mutex_lock(&src->i_mutex);
365632b7c687SMark Fasheh 	}
365732b7c687SMark Fasheh 
365832b7c687SMark Fasheh 	/* determine range to clone */
365932b7c687SMark Fasheh 	ret = -EINVAL;
366032b7c687SMark Fasheh 	if (off + len > src->i_size || off + len < off)
366132b7c687SMark Fasheh 		goto out_unlock;
366232b7c687SMark Fasheh 	if (len == 0)
366332b7c687SMark Fasheh 		olen = len = src->i_size - off;
366432b7c687SMark Fasheh 	/* if we extend to eof, continue to block boundary */
366532b7c687SMark Fasheh 	if (off + len == src->i_size)
366632b7c687SMark Fasheh 		len = ALIGN(src->i_size, bs) - off;
366732b7c687SMark Fasheh 
366832b7c687SMark Fasheh 	/* verify the end result is block aligned */
366932b7c687SMark Fasheh 	if (!IS_ALIGNED(off, bs) || !IS_ALIGNED(off + len, bs) ||
367032b7c687SMark Fasheh 	    !IS_ALIGNED(destoff, bs))
367132b7c687SMark Fasheh 		goto out_unlock;
367232b7c687SMark Fasheh 
367332b7c687SMark Fasheh 	/* verify if ranges are overlapped within the same file */
367432b7c687SMark Fasheh 	if (same_inode) {
367532b7c687SMark Fasheh 		if (destoff + len > off && destoff < off + len)
367632b7c687SMark Fasheh 			goto out_unlock;
367732b7c687SMark Fasheh 	}
367832b7c687SMark Fasheh 
367932b7c687SMark Fasheh 	if (destoff > inode->i_size) {
368032b7c687SMark Fasheh 		ret = btrfs_cont_expand(inode, inode->i_size, destoff);
368132b7c687SMark Fasheh 		if (ret)
368232b7c687SMark Fasheh 			goto out_unlock;
368332b7c687SMark Fasheh 	}
368432b7c687SMark Fasheh 
3685c125b8bfSFilipe Manana 	/*
3686c125b8bfSFilipe Manana 	 * Lock the target range too. Right after we replace the file extent
3687c125b8bfSFilipe Manana 	 * items in the fs tree (which now point to the cloned data), we might
3688c125b8bfSFilipe Manana 	 * have a worker replace them with extent items relative to a write
3689c125b8bfSFilipe Manana 	 * operation that was issued before this clone operation (i.e. confront
3690c125b8bfSFilipe Manana 	 * with inode.c:btrfs_finish_ordered_io).
3691c125b8bfSFilipe Manana 	 */
3692c125b8bfSFilipe Manana 	if (same_inode) {
3693c125b8bfSFilipe Manana 		u64 lock_start = min_t(u64, off, destoff);
3694c125b8bfSFilipe Manana 		u64 lock_len = max_t(u64, off, destoff) + len - lock_start;
369532b7c687SMark Fasheh 
3696c125b8bfSFilipe Manana 		lock_extent_range(src, lock_start, lock_len);
3697c125b8bfSFilipe Manana 	} else {
369832b7c687SMark Fasheh 		lock_extent_range(src, off, len);
3699c125b8bfSFilipe Manana 		lock_extent_range(inode, destoff, len);
3700c125b8bfSFilipe Manana 	}
370132b7c687SMark Fasheh 
370232b7c687SMark Fasheh 	ret = btrfs_clone(src, inode, off, olen, len, destoff);
370332b7c687SMark Fasheh 
3704c125b8bfSFilipe Manana 	if (same_inode) {
3705c125b8bfSFilipe Manana 		u64 lock_start = min_t(u64, off, destoff);
3706c125b8bfSFilipe Manana 		u64 lock_end = max_t(u64, off, destoff) + len - 1;
3707c125b8bfSFilipe Manana 
3708c125b8bfSFilipe Manana 		unlock_extent(&BTRFS_I(src)->io_tree, lock_start, lock_end);
3709c125b8bfSFilipe Manana 	} else {
3710aa42ffd9SLiu Bo 		unlock_extent(&BTRFS_I(src)->io_tree, off, off + len - 1);
3711c125b8bfSFilipe Manana 		unlock_extent(&BTRFS_I(inode)->io_tree, destoff,
3712c125b8bfSFilipe Manana 			      destoff + len - 1);
3713c125b8bfSFilipe Manana 	}
3714c125b8bfSFilipe Manana 	/*
3715c125b8bfSFilipe Manana 	 * Truncate page cache pages so that future reads will see the cloned
3716c125b8bfSFilipe Manana 	 * data immediately and not the previous data.
3717c125b8bfSFilipe Manana 	 */
3718c125b8bfSFilipe Manana 	truncate_inode_pages_range(&inode->i_data, destoff,
3719c125b8bfSFilipe Manana 				   PAGE_CACHE_ALIGN(destoff + len) - 1);
3720f46b5a66SChristoph Hellwig out_unlock:
3721c57c2b3eSFilipe David Borba Manana 	if (!same_inode) {
3722c57c2b3eSFilipe David Borba Manana 		if (inode < src) {
3723f46b5a66SChristoph Hellwig 			mutex_unlock(&src->i_mutex);
3724f46b5a66SChristoph Hellwig 			mutex_unlock(&inode->i_mutex);
3725c57c2b3eSFilipe David Borba Manana 		} else {
3726c57c2b3eSFilipe David Borba Manana 			mutex_unlock(&inode->i_mutex);
3727c57c2b3eSFilipe David Borba Manana 			mutex_unlock(&src->i_mutex);
3728c57c2b3eSFilipe David Borba Manana 		}
3729c57c2b3eSFilipe David Borba Manana 	} else {
3730c57c2b3eSFilipe David Borba Manana 		mutex_unlock(&src->i_mutex);
3731c57c2b3eSFilipe David Borba Manana 	}
3732f46b5a66SChristoph Hellwig out_fput:
37332903ff01SAl Viro 	fdput(src_file);
3734ab67b7c1SYan Zheng out_drop_write:
37352a79f17eSAl Viro 	mnt_drop_write_file(file);
3736f46b5a66SChristoph Hellwig 	return ret;
3737f46b5a66SChristoph Hellwig }
3738f46b5a66SChristoph Hellwig 
37397a865e8aSChristoph Hellwig static long btrfs_ioctl_clone_range(struct file *file, void __user *argp)
3740c5c9cd4dSSage Weil {
3741c5c9cd4dSSage Weil 	struct btrfs_ioctl_clone_range_args args;
3742c5c9cd4dSSage Weil 
37437a865e8aSChristoph Hellwig 	if (copy_from_user(&args, argp, sizeof(args)))
3744c5c9cd4dSSage Weil 		return -EFAULT;
3745c5c9cd4dSSage Weil 	return btrfs_ioctl_clone(file, args.src_fd, args.src_offset,
3746c5c9cd4dSSage Weil 				 args.src_length, args.dest_offset);
3747c5c9cd4dSSage Weil }
3748c5c9cd4dSSage Weil 
3749f46b5a66SChristoph Hellwig /*
3750f46b5a66SChristoph Hellwig  * there are many ways the trans_start and trans_end ioctls can lead
3751f46b5a66SChristoph Hellwig  * to deadlocks.  They should only be used by applications that
3752f46b5a66SChristoph Hellwig  * basically own the machine, and have a very in depth understanding
3753f46b5a66SChristoph Hellwig  * of all the possible deadlocks and enospc problems.
3754f46b5a66SChristoph Hellwig  */
3755b2950863SChristoph Hellwig static long btrfs_ioctl_trans_start(struct file *file)
3756f46b5a66SChristoph Hellwig {
3757496ad9aaSAl Viro 	struct inode *inode = file_inode(file);
3758f46b5a66SChristoph Hellwig 	struct btrfs_root *root = BTRFS_I(inode)->root;
3759f46b5a66SChristoph Hellwig 	struct btrfs_trans_handle *trans;
37601ab86aedSSage Weil 	int ret;
3761f46b5a66SChristoph Hellwig 
37621ab86aedSSage Weil 	ret = -EPERM;
3763df5b5520SChristoph Hellwig 	if (!capable(CAP_SYS_ADMIN))
3764f46b5a66SChristoph Hellwig 		goto out;
37651ab86aedSSage Weil 
37661ab86aedSSage Weil 	ret = -EINPROGRESS;
37671ab86aedSSage Weil 	if (file->private_data)
37681ab86aedSSage Weil 		goto out;
37699ca9ee09SSage Weil 
3770b83cc969SLi Zefan 	ret = -EROFS;
3771b83cc969SLi Zefan 	if (btrfs_root_readonly(root))
3772b83cc969SLi Zefan 		goto out;
3773b83cc969SLi Zefan 
3774a561be71SAl Viro 	ret = mnt_want_write_file(file);
3775c146afadSYan Zheng 	if (ret)
3776c146afadSYan Zheng 		goto out;
3777c146afadSYan Zheng 
3778a4abeea4SJosef Bacik 	atomic_inc(&root->fs_info->open_ioctl_trans);
37799ca9ee09SSage Weil 
3780f46b5a66SChristoph Hellwig 	ret = -ENOMEM;
37817a7eaa40SJosef Bacik 	trans = btrfs_start_ioctl_transaction(root);
3782abd30bb0STsutomu Itoh 	if (IS_ERR(trans))
37831ab86aedSSage Weil 		goto out_drop;
37841ab86aedSSage Weil 
37851ab86aedSSage Weil 	file->private_data = trans;
37861ab86aedSSage Weil 	return 0;
37871ab86aedSSage Weil 
37881ab86aedSSage Weil out_drop:
3789a4abeea4SJosef Bacik 	atomic_dec(&root->fs_info->open_ioctl_trans);
37902a79f17eSAl Viro 	mnt_drop_write_file(file);
3791f46b5a66SChristoph Hellwig out:
3792f46b5a66SChristoph Hellwig 	return ret;
3793f46b5a66SChristoph Hellwig }
3794f46b5a66SChristoph Hellwig 
37956ef5ed0dSJosef Bacik static long btrfs_ioctl_default_subvol(struct file *file, void __user *argp)
37966ef5ed0dSJosef Bacik {
3797496ad9aaSAl Viro 	struct inode *inode = file_inode(file);
37986ef5ed0dSJosef Bacik 	struct btrfs_root *root = BTRFS_I(inode)->root;
37996ef5ed0dSJosef Bacik 	struct btrfs_root *new_root;
38006ef5ed0dSJosef Bacik 	struct btrfs_dir_item *di;
38016ef5ed0dSJosef Bacik 	struct btrfs_trans_handle *trans;
38026ef5ed0dSJosef Bacik 	struct btrfs_path *path;
38036ef5ed0dSJosef Bacik 	struct btrfs_key location;
38046ef5ed0dSJosef Bacik 	struct btrfs_disk_key disk_key;
38056ef5ed0dSJosef Bacik 	u64 objectid = 0;
38066ef5ed0dSJosef Bacik 	u64 dir_id;
38073c04ce01SMiao Xie 	int ret;
38086ef5ed0dSJosef Bacik 
38096ef5ed0dSJosef Bacik 	if (!capable(CAP_SYS_ADMIN))
38106ef5ed0dSJosef Bacik 		return -EPERM;
38116ef5ed0dSJosef Bacik 
38123c04ce01SMiao Xie 	ret = mnt_want_write_file(file);
38133c04ce01SMiao Xie 	if (ret)
38143c04ce01SMiao Xie 		return ret;
38153c04ce01SMiao Xie 
38163c04ce01SMiao Xie 	if (copy_from_user(&objectid, argp, sizeof(objectid))) {
38173c04ce01SMiao Xie 		ret = -EFAULT;
38183c04ce01SMiao Xie 		goto out;
38193c04ce01SMiao Xie 	}
38206ef5ed0dSJosef Bacik 
38216ef5ed0dSJosef Bacik 	if (!objectid)
38221cecf579Schandan 		objectid = BTRFS_FS_TREE_OBJECTID;
38236ef5ed0dSJosef Bacik 
38246ef5ed0dSJosef Bacik 	location.objectid = objectid;
38256ef5ed0dSJosef Bacik 	location.type = BTRFS_ROOT_ITEM_KEY;
38266ef5ed0dSJosef Bacik 	location.offset = (u64)-1;
38276ef5ed0dSJosef Bacik 
38286ef5ed0dSJosef Bacik 	new_root = btrfs_read_fs_root_no_name(root->fs_info, &location);
38293c04ce01SMiao Xie 	if (IS_ERR(new_root)) {
38303c04ce01SMiao Xie 		ret = PTR_ERR(new_root);
38313c04ce01SMiao Xie 		goto out;
38323c04ce01SMiao Xie 	}
38336ef5ed0dSJosef Bacik 
38346ef5ed0dSJosef Bacik 	path = btrfs_alloc_path();
38353c04ce01SMiao Xie 	if (!path) {
38363c04ce01SMiao Xie 		ret = -ENOMEM;
38373c04ce01SMiao Xie 		goto out;
38383c04ce01SMiao Xie 	}
38396ef5ed0dSJosef Bacik 	path->leave_spinning = 1;
38406ef5ed0dSJosef Bacik 
38416ef5ed0dSJosef Bacik 	trans = btrfs_start_transaction(root, 1);
384298d5dc13STsutomu Itoh 	if (IS_ERR(trans)) {
38436ef5ed0dSJosef Bacik 		btrfs_free_path(path);
38443c04ce01SMiao Xie 		ret = PTR_ERR(trans);
38453c04ce01SMiao Xie 		goto out;
38466ef5ed0dSJosef Bacik 	}
38476ef5ed0dSJosef Bacik 
38486c41761fSDavid Sterba 	dir_id = btrfs_super_root_dir(root->fs_info->super_copy);
38496ef5ed0dSJosef Bacik 	di = btrfs_lookup_dir_item(trans, root->fs_info->tree_root, path,
38506ef5ed0dSJosef Bacik 				   dir_id, "default", 7, 1);
3851cf1e99a4SDan Carpenter 	if (IS_ERR_OR_NULL(di)) {
38526ef5ed0dSJosef Bacik 		btrfs_free_path(path);
38536ef5ed0dSJosef Bacik 		btrfs_end_transaction(trans, root);
3854efe120a0SFrank Holton 		btrfs_err(new_root->fs_info, "Umm, you don't have the default dir"
3855efe120a0SFrank Holton 			   "item, this isn't going to work");
38563c04ce01SMiao Xie 		ret = -ENOENT;
38573c04ce01SMiao Xie 		goto out;
38586ef5ed0dSJosef Bacik 	}
38596ef5ed0dSJosef Bacik 
38606ef5ed0dSJosef Bacik 	btrfs_cpu_key_to_disk(&disk_key, &new_root->root_key);
38616ef5ed0dSJosef Bacik 	btrfs_set_dir_item_key(path->nodes[0], di, &disk_key);
38626ef5ed0dSJosef Bacik 	btrfs_mark_buffer_dirty(path->nodes[0]);
38636ef5ed0dSJosef Bacik 	btrfs_free_path(path);
38646ef5ed0dSJosef Bacik 
38652b0ce2c2SMitch Harder 	btrfs_set_fs_incompat(root->fs_info, DEFAULT_SUBVOL);
38666ef5ed0dSJosef Bacik 	btrfs_end_transaction(trans, root);
38673c04ce01SMiao Xie out:
38683c04ce01SMiao Xie 	mnt_drop_write_file(file);
38693c04ce01SMiao Xie 	return ret;
38706ef5ed0dSJosef Bacik }
38716ef5ed0dSJosef Bacik 
38725af3e8ccSStefan Behrens void btrfs_get_block_group_info(struct list_head *groups_list,
3873bf5fc093SJosef Bacik 				struct btrfs_ioctl_space_info *space)
3874bf5fc093SJosef Bacik {
3875bf5fc093SJosef Bacik 	struct btrfs_block_group_cache *block_group;
3876bf5fc093SJosef Bacik 
3877bf5fc093SJosef Bacik 	space->total_bytes = 0;
3878bf5fc093SJosef Bacik 	space->used_bytes = 0;
3879bf5fc093SJosef Bacik 	space->flags = 0;
3880bf5fc093SJosef Bacik 	list_for_each_entry(block_group, groups_list, list) {
3881bf5fc093SJosef Bacik 		space->flags = block_group->flags;
3882bf5fc093SJosef Bacik 		space->total_bytes += block_group->key.offset;
3883bf5fc093SJosef Bacik 		space->used_bytes +=
3884bf5fc093SJosef Bacik 			btrfs_block_group_used(&block_group->item);
3885bf5fc093SJosef Bacik 	}
3886bf5fc093SJosef Bacik }
3887bf5fc093SJosef Bacik 
388848a3b636SEric Sandeen static long btrfs_ioctl_space_info(struct btrfs_root *root, void __user *arg)
38891406e432SJosef Bacik {
38901406e432SJosef Bacik 	struct btrfs_ioctl_space_args space_args;
38911406e432SJosef Bacik 	struct btrfs_ioctl_space_info space;
38921406e432SJosef Bacik 	struct btrfs_ioctl_space_info *dest;
38937fde62bfSChris Mason 	struct btrfs_ioctl_space_info *dest_orig;
389413f2696fSDaniel J Blueman 	struct btrfs_ioctl_space_info __user *user_dest;
38951406e432SJosef Bacik 	struct btrfs_space_info *info;
3896bf5fc093SJosef Bacik 	u64 types[] = {BTRFS_BLOCK_GROUP_DATA,
3897bf5fc093SJosef Bacik 		       BTRFS_BLOCK_GROUP_SYSTEM,
3898bf5fc093SJosef Bacik 		       BTRFS_BLOCK_GROUP_METADATA,
3899bf5fc093SJosef Bacik 		       BTRFS_BLOCK_GROUP_DATA | BTRFS_BLOCK_GROUP_METADATA};
3900bf5fc093SJosef Bacik 	int num_types = 4;
39017fde62bfSChris Mason 	int alloc_size;
39021406e432SJosef Bacik 	int ret = 0;
390351788b1bSDan Rosenberg 	u64 slot_count = 0;
3904bf5fc093SJosef Bacik 	int i, c;
39051406e432SJosef Bacik 
39061406e432SJosef Bacik 	if (copy_from_user(&space_args,
39071406e432SJosef Bacik 			   (struct btrfs_ioctl_space_args __user *)arg,
39081406e432SJosef Bacik 			   sizeof(space_args)))
39091406e432SJosef Bacik 		return -EFAULT;
39101406e432SJosef Bacik 
3911bf5fc093SJosef Bacik 	for (i = 0; i < num_types; i++) {
3912bf5fc093SJosef Bacik 		struct btrfs_space_info *tmp;
3913bf5fc093SJosef Bacik 
3914bf5fc093SJosef Bacik 		info = NULL;
39157fde62bfSChris Mason 		rcu_read_lock();
3916bf5fc093SJosef Bacik 		list_for_each_entry_rcu(tmp, &root->fs_info->space_info,
3917bf5fc093SJosef Bacik 					list) {
3918bf5fc093SJosef Bacik 			if (tmp->flags == types[i]) {
3919bf5fc093SJosef Bacik 				info = tmp;
3920bf5fc093SJosef Bacik 				break;
3921bf5fc093SJosef Bacik 			}
3922bf5fc093SJosef Bacik 		}
39237fde62bfSChris Mason 		rcu_read_unlock();
39241406e432SJosef Bacik 
3925bf5fc093SJosef Bacik 		if (!info)
3926bf5fc093SJosef Bacik 			continue;
3927bf5fc093SJosef Bacik 
3928bf5fc093SJosef Bacik 		down_read(&info->groups_sem);
3929bf5fc093SJosef Bacik 		for (c = 0; c < BTRFS_NR_RAID_TYPES; c++) {
3930bf5fc093SJosef Bacik 			if (!list_empty(&info->block_groups[c]))
3931bf5fc093SJosef Bacik 				slot_count++;
3932bf5fc093SJosef Bacik 		}
3933bf5fc093SJosef Bacik 		up_read(&info->groups_sem);
3934bf5fc093SJosef Bacik 	}
3935bf5fc093SJosef Bacik 
393636523e95SDavid Sterba 	/*
393736523e95SDavid Sterba 	 * Global block reserve, exported as a space_info
393836523e95SDavid Sterba 	 */
393936523e95SDavid Sterba 	slot_count++;
394036523e95SDavid Sterba 
39417fde62bfSChris Mason 	/* space_slots == 0 means they are asking for a count */
39427fde62bfSChris Mason 	if (space_args.space_slots == 0) {
39437fde62bfSChris Mason 		space_args.total_spaces = slot_count;
39447fde62bfSChris Mason 		goto out;
39457fde62bfSChris Mason 	}
3946bf5fc093SJosef Bacik 
394751788b1bSDan Rosenberg 	slot_count = min_t(u64, space_args.space_slots, slot_count);
3948bf5fc093SJosef Bacik 
39497fde62bfSChris Mason 	alloc_size = sizeof(*dest) * slot_count;
3950bf5fc093SJosef Bacik 
39517fde62bfSChris Mason 	/* we generally have at most 6 or so space infos, one for each raid
39527fde62bfSChris Mason 	 * level.  So, a whole page should be more than enough for everyone
39537fde62bfSChris Mason 	 */
39547fde62bfSChris Mason 	if (alloc_size > PAGE_CACHE_SIZE)
39557fde62bfSChris Mason 		return -ENOMEM;
39567fde62bfSChris Mason 
39577fde62bfSChris Mason 	space_args.total_spaces = 0;
39587fde62bfSChris Mason 	dest = kmalloc(alloc_size, GFP_NOFS);
39597fde62bfSChris Mason 	if (!dest)
39607fde62bfSChris Mason 		return -ENOMEM;
39617fde62bfSChris Mason 	dest_orig = dest;
39627fde62bfSChris Mason 
39637fde62bfSChris Mason 	/* now we have a buffer to copy into */
3964bf5fc093SJosef Bacik 	for (i = 0; i < num_types; i++) {
3965bf5fc093SJosef Bacik 		struct btrfs_space_info *tmp;
3966bf5fc093SJosef Bacik 
396751788b1bSDan Rosenberg 		if (!slot_count)
396851788b1bSDan Rosenberg 			break;
396951788b1bSDan Rosenberg 
3970bf5fc093SJosef Bacik 		info = NULL;
39711406e432SJosef Bacik 		rcu_read_lock();
3972bf5fc093SJosef Bacik 		list_for_each_entry_rcu(tmp, &root->fs_info->space_info,
3973bf5fc093SJosef Bacik 					list) {
3974bf5fc093SJosef Bacik 			if (tmp->flags == types[i]) {
3975bf5fc093SJosef Bacik 				info = tmp;
39767fde62bfSChris Mason 				break;
3977bf5fc093SJosef Bacik 			}
3978bf5fc093SJosef Bacik 		}
3979bf5fc093SJosef Bacik 		rcu_read_unlock();
39807fde62bfSChris Mason 
3981bf5fc093SJosef Bacik 		if (!info)
3982bf5fc093SJosef Bacik 			continue;
3983bf5fc093SJosef Bacik 		down_read(&info->groups_sem);
3984bf5fc093SJosef Bacik 		for (c = 0; c < BTRFS_NR_RAID_TYPES; c++) {
3985bf5fc093SJosef Bacik 			if (!list_empty(&info->block_groups[c])) {
39865af3e8ccSStefan Behrens 				btrfs_get_block_group_info(
39875af3e8ccSStefan Behrens 					&info->block_groups[c], &space);
39887fde62bfSChris Mason 				memcpy(dest, &space, sizeof(space));
39891406e432SJosef Bacik 				dest++;
39901406e432SJosef Bacik 				space_args.total_spaces++;
399151788b1bSDan Rosenberg 				slot_count--;
39921406e432SJosef Bacik 			}
399351788b1bSDan Rosenberg 			if (!slot_count)
399451788b1bSDan Rosenberg 				break;
3995bf5fc093SJosef Bacik 		}
3996bf5fc093SJosef Bacik 		up_read(&info->groups_sem);
3997bf5fc093SJosef Bacik 	}
39981406e432SJosef Bacik 
399936523e95SDavid Sterba 	/*
400036523e95SDavid Sterba 	 * Add global block reserve
400136523e95SDavid Sterba 	 */
400236523e95SDavid Sterba 	if (slot_count) {
400336523e95SDavid Sterba 		struct btrfs_block_rsv *block_rsv = &root->fs_info->global_block_rsv;
400436523e95SDavid Sterba 
400536523e95SDavid Sterba 		spin_lock(&block_rsv->lock);
400636523e95SDavid Sterba 		space.total_bytes = block_rsv->size;
400736523e95SDavid Sterba 		space.used_bytes = block_rsv->size - block_rsv->reserved;
400836523e95SDavid Sterba 		spin_unlock(&block_rsv->lock);
400936523e95SDavid Sterba 		space.flags = BTRFS_SPACE_INFO_GLOBAL_RSV;
401036523e95SDavid Sterba 		memcpy(dest, &space, sizeof(space));
401136523e95SDavid Sterba 		space_args.total_spaces++;
401236523e95SDavid Sterba 	}
401336523e95SDavid Sterba 
40142eec6c81SDaniel J Blueman 	user_dest = (struct btrfs_ioctl_space_info __user *)
40157fde62bfSChris Mason 		(arg + sizeof(struct btrfs_ioctl_space_args));
40167fde62bfSChris Mason 
40177fde62bfSChris Mason 	if (copy_to_user(user_dest, dest_orig, alloc_size))
40187fde62bfSChris Mason 		ret = -EFAULT;
40197fde62bfSChris Mason 
40207fde62bfSChris Mason 	kfree(dest_orig);
40217fde62bfSChris Mason out:
40227fde62bfSChris Mason 	if (ret == 0 && copy_to_user(arg, &space_args, sizeof(space_args)))
40231406e432SJosef Bacik 		ret = -EFAULT;
40241406e432SJosef Bacik 
40251406e432SJosef Bacik 	return ret;
40261406e432SJosef Bacik }
40271406e432SJosef Bacik 
4028f46b5a66SChristoph Hellwig /*
4029f46b5a66SChristoph Hellwig  * there are many ways the trans_start and trans_end ioctls can lead
4030f46b5a66SChristoph Hellwig  * to deadlocks.  They should only be used by applications that
4031f46b5a66SChristoph Hellwig  * basically own the machine, and have a very in depth understanding
4032f46b5a66SChristoph Hellwig  * of all the possible deadlocks and enospc problems.
4033f46b5a66SChristoph Hellwig  */
4034f46b5a66SChristoph Hellwig long btrfs_ioctl_trans_end(struct file *file)
4035f46b5a66SChristoph Hellwig {
4036496ad9aaSAl Viro 	struct inode *inode = file_inode(file);
4037f46b5a66SChristoph Hellwig 	struct btrfs_root *root = BTRFS_I(inode)->root;
4038f46b5a66SChristoph Hellwig 	struct btrfs_trans_handle *trans;
4039f46b5a66SChristoph Hellwig 
4040f46b5a66SChristoph Hellwig 	trans = file->private_data;
40411ab86aedSSage Weil 	if (!trans)
40421ab86aedSSage Weil 		return -EINVAL;
4043b214107eSChristoph Hellwig 	file->private_data = NULL;
40449ca9ee09SSage Weil 
40451ab86aedSSage Weil 	btrfs_end_transaction(trans, root);
40461ab86aedSSage Weil 
4047a4abeea4SJosef Bacik 	atomic_dec(&root->fs_info->open_ioctl_trans);
40489ca9ee09SSage Weil 
40492a79f17eSAl Viro 	mnt_drop_write_file(file);
40501ab86aedSSage Weil 	return 0;
4051f46b5a66SChristoph Hellwig }
4052f46b5a66SChristoph Hellwig 
40539a8c28beSMiao Xie static noinline long btrfs_ioctl_start_sync(struct btrfs_root *root,
40549a8c28beSMiao Xie 					    void __user *argp)
405546204592SSage Weil {
405646204592SSage Weil 	struct btrfs_trans_handle *trans;
405746204592SSage Weil 	u64 transid;
4058db5b493aSTsutomu Itoh 	int ret;
405946204592SSage Weil 
4060d4edf39bSMiao Xie 	trans = btrfs_attach_transaction_barrier(root);
4061ff7c1d33SMiao Xie 	if (IS_ERR(trans)) {
4062ff7c1d33SMiao Xie 		if (PTR_ERR(trans) != -ENOENT)
406398d5dc13STsutomu Itoh 			return PTR_ERR(trans);
4064ff7c1d33SMiao Xie 
4065ff7c1d33SMiao Xie 		/* No running transaction, don't bother */
4066ff7c1d33SMiao Xie 		transid = root->fs_info->last_trans_committed;
4067ff7c1d33SMiao Xie 		goto out;
4068ff7c1d33SMiao Xie 	}
406946204592SSage Weil 	transid = trans->transid;
4070db5b493aSTsutomu Itoh 	ret = btrfs_commit_transaction_async(trans, root, 0);
40718b2b2d3cSTsutomu Itoh 	if (ret) {
40728b2b2d3cSTsutomu Itoh 		btrfs_end_transaction(trans, root);
4073db5b493aSTsutomu Itoh 		return ret;
40748b2b2d3cSTsutomu Itoh 	}
4075ff7c1d33SMiao Xie out:
407646204592SSage Weil 	if (argp)
407746204592SSage Weil 		if (copy_to_user(argp, &transid, sizeof(transid)))
407846204592SSage Weil 			return -EFAULT;
407946204592SSage Weil 	return 0;
408046204592SSage Weil }
408146204592SSage Weil 
40829a8c28beSMiao Xie static noinline long btrfs_ioctl_wait_sync(struct btrfs_root *root,
40839a8c28beSMiao Xie 					   void __user *argp)
408446204592SSage Weil {
408546204592SSage Weil 	u64 transid;
408646204592SSage Weil 
408746204592SSage Weil 	if (argp) {
408846204592SSage Weil 		if (copy_from_user(&transid, argp, sizeof(transid)))
408946204592SSage Weil 			return -EFAULT;
409046204592SSage Weil 	} else {
409146204592SSage Weil 		transid = 0;  /* current trans */
409246204592SSage Weil 	}
409346204592SSage Weil 	return btrfs_wait_for_commit(root, transid);
409446204592SSage Weil }
409546204592SSage Weil 
4096b8e95489SMiao Xie static long btrfs_ioctl_scrub(struct file *file, void __user *arg)
4097475f6387SJan Schmidt {
4098496ad9aaSAl Viro 	struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
4099475f6387SJan Schmidt 	struct btrfs_ioctl_scrub_args *sa;
4100b8e95489SMiao Xie 	int ret;
4101475f6387SJan Schmidt 
4102475f6387SJan Schmidt 	if (!capable(CAP_SYS_ADMIN))
4103475f6387SJan Schmidt 		return -EPERM;
4104475f6387SJan Schmidt 
4105475f6387SJan Schmidt 	sa = memdup_user(arg, sizeof(*sa));
4106475f6387SJan Schmidt 	if (IS_ERR(sa))
4107475f6387SJan Schmidt 		return PTR_ERR(sa);
4108475f6387SJan Schmidt 
4109b8e95489SMiao Xie 	if (!(sa->flags & BTRFS_SCRUB_READONLY)) {
4110b8e95489SMiao Xie 		ret = mnt_want_write_file(file);
4111b8e95489SMiao Xie 		if (ret)
4112b8e95489SMiao Xie 			goto out;
4113b8e95489SMiao Xie 	}
4114b8e95489SMiao Xie 
4115aa1b8cd4SStefan Behrens 	ret = btrfs_scrub_dev(root->fs_info, sa->devid, sa->start, sa->end,
411663a212abSStefan Behrens 			      &sa->progress, sa->flags & BTRFS_SCRUB_READONLY,
411763a212abSStefan Behrens 			      0);
4118475f6387SJan Schmidt 
4119475f6387SJan Schmidt 	if (copy_to_user(arg, sa, sizeof(*sa)))
4120475f6387SJan Schmidt 		ret = -EFAULT;
4121475f6387SJan Schmidt 
4122b8e95489SMiao Xie 	if (!(sa->flags & BTRFS_SCRUB_READONLY))
4123b8e95489SMiao Xie 		mnt_drop_write_file(file);
4124b8e95489SMiao Xie out:
4125475f6387SJan Schmidt 	kfree(sa);
4126475f6387SJan Schmidt 	return ret;
4127475f6387SJan Schmidt }
4128475f6387SJan Schmidt 
4129475f6387SJan Schmidt static long btrfs_ioctl_scrub_cancel(struct btrfs_root *root, void __user *arg)
4130475f6387SJan Schmidt {
4131475f6387SJan Schmidt 	if (!capable(CAP_SYS_ADMIN))
4132475f6387SJan Schmidt 		return -EPERM;
4133475f6387SJan Schmidt 
4134aa1b8cd4SStefan Behrens 	return btrfs_scrub_cancel(root->fs_info);
4135475f6387SJan Schmidt }
4136475f6387SJan Schmidt 
4137475f6387SJan Schmidt static long btrfs_ioctl_scrub_progress(struct btrfs_root *root,
4138475f6387SJan Schmidt 				       void __user *arg)
4139475f6387SJan Schmidt {
4140475f6387SJan Schmidt 	struct btrfs_ioctl_scrub_args *sa;
4141475f6387SJan Schmidt 	int ret;
4142475f6387SJan Schmidt 
4143475f6387SJan Schmidt 	if (!capable(CAP_SYS_ADMIN))
4144475f6387SJan Schmidt 		return -EPERM;
4145475f6387SJan Schmidt 
4146475f6387SJan Schmidt 	sa = memdup_user(arg, sizeof(*sa));
4147475f6387SJan Schmidt 	if (IS_ERR(sa))
4148475f6387SJan Schmidt 		return PTR_ERR(sa);
4149475f6387SJan Schmidt 
4150475f6387SJan Schmidt 	ret = btrfs_scrub_progress(root, sa->devid, &sa->progress);
4151475f6387SJan Schmidt 
4152475f6387SJan Schmidt 	if (copy_to_user(arg, sa, sizeof(*sa)))
4153475f6387SJan Schmidt 		ret = -EFAULT;
4154475f6387SJan Schmidt 
4155475f6387SJan Schmidt 	kfree(sa);
4156475f6387SJan Schmidt 	return ret;
4157475f6387SJan Schmidt }
4158475f6387SJan Schmidt 
4159c11d2c23SStefan Behrens static long btrfs_ioctl_get_dev_stats(struct btrfs_root *root,
4160b27f7c0cSDavid Sterba 				      void __user *arg)
4161c11d2c23SStefan Behrens {
4162c11d2c23SStefan Behrens 	struct btrfs_ioctl_get_dev_stats *sa;
4163c11d2c23SStefan Behrens 	int ret;
4164c11d2c23SStefan Behrens 
4165c11d2c23SStefan Behrens 	sa = memdup_user(arg, sizeof(*sa));
4166c11d2c23SStefan Behrens 	if (IS_ERR(sa))
4167c11d2c23SStefan Behrens 		return PTR_ERR(sa);
4168c11d2c23SStefan Behrens 
4169b27f7c0cSDavid Sterba 	if ((sa->flags & BTRFS_DEV_STATS_RESET) && !capable(CAP_SYS_ADMIN)) {
4170b27f7c0cSDavid Sterba 		kfree(sa);
4171b27f7c0cSDavid Sterba 		return -EPERM;
4172b27f7c0cSDavid Sterba 	}
4173b27f7c0cSDavid Sterba 
4174b27f7c0cSDavid Sterba 	ret = btrfs_get_dev_stats(root, sa);
4175c11d2c23SStefan Behrens 
4176c11d2c23SStefan Behrens 	if (copy_to_user(arg, sa, sizeof(*sa)))
4177c11d2c23SStefan Behrens 		ret = -EFAULT;
4178c11d2c23SStefan Behrens 
4179c11d2c23SStefan Behrens 	kfree(sa);
4180c11d2c23SStefan Behrens 	return ret;
4181c11d2c23SStefan Behrens }
4182c11d2c23SStefan Behrens 
41833f6bcfbdSStefan Behrens static long btrfs_ioctl_dev_replace(struct btrfs_root *root, void __user *arg)
41843f6bcfbdSStefan Behrens {
41853f6bcfbdSStefan Behrens 	struct btrfs_ioctl_dev_replace_args *p;
41863f6bcfbdSStefan Behrens 	int ret;
41873f6bcfbdSStefan Behrens 
41883f6bcfbdSStefan Behrens 	if (!capable(CAP_SYS_ADMIN))
41893f6bcfbdSStefan Behrens 		return -EPERM;
41903f6bcfbdSStefan Behrens 
41913f6bcfbdSStefan Behrens 	p = memdup_user(arg, sizeof(*p));
41923f6bcfbdSStefan Behrens 	if (IS_ERR(p))
41933f6bcfbdSStefan Behrens 		return PTR_ERR(p);
41943f6bcfbdSStefan Behrens 
41953f6bcfbdSStefan Behrens 	switch (p->cmd) {
41963f6bcfbdSStefan Behrens 	case BTRFS_IOCTL_DEV_REPLACE_CMD_START:
4197adfa97cbSIlya Dryomov 		if (root->fs_info->sb->s_flags & MS_RDONLY) {
4198adfa97cbSIlya Dryomov 			ret = -EROFS;
4199adfa97cbSIlya Dryomov 			goto out;
4200adfa97cbSIlya Dryomov 		}
42013f6bcfbdSStefan Behrens 		if (atomic_xchg(
42023f6bcfbdSStefan Behrens 			&root->fs_info->mutually_exclusive_operation_running,
42033f6bcfbdSStefan Behrens 			1)) {
4204e57138b3SAnand Jain 			ret = BTRFS_ERROR_DEV_EXCL_RUN_IN_PROGRESS;
42053f6bcfbdSStefan Behrens 		} else {
42063f6bcfbdSStefan Behrens 			ret = btrfs_dev_replace_start(root, p);
42073f6bcfbdSStefan Behrens 			atomic_set(
42083f6bcfbdSStefan Behrens 			 &root->fs_info->mutually_exclusive_operation_running,
42093f6bcfbdSStefan Behrens 			 0);
42103f6bcfbdSStefan Behrens 		}
42113f6bcfbdSStefan Behrens 		break;
42123f6bcfbdSStefan Behrens 	case BTRFS_IOCTL_DEV_REPLACE_CMD_STATUS:
42133f6bcfbdSStefan Behrens 		btrfs_dev_replace_status(root->fs_info, p);
42143f6bcfbdSStefan Behrens 		ret = 0;
42153f6bcfbdSStefan Behrens 		break;
42163f6bcfbdSStefan Behrens 	case BTRFS_IOCTL_DEV_REPLACE_CMD_CANCEL:
42173f6bcfbdSStefan Behrens 		ret = btrfs_dev_replace_cancel(root->fs_info, p);
42183f6bcfbdSStefan Behrens 		break;
42193f6bcfbdSStefan Behrens 	default:
42203f6bcfbdSStefan Behrens 		ret = -EINVAL;
42213f6bcfbdSStefan Behrens 		break;
42223f6bcfbdSStefan Behrens 	}
42233f6bcfbdSStefan Behrens 
42243f6bcfbdSStefan Behrens 	if (copy_to_user(arg, p, sizeof(*p)))
42253f6bcfbdSStefan Behrens 		ret = -EFAULT;
4226adfa97cbSIlya Dryomov out:
42273f6bcfbdSStefan Behrens 	kfree(p);
42283f6bcfbdSStefan Behrens 	return ret;
42293f6bcfbdSStefan Behrens }
42303f6bcfbdSStefan Behrens 
4231d7728c96SJan Schmidt static long btrfs_ioctl_ino_to_path(struct btrfs_root *root, void __user *arg)
4232d7728c96SJan Schmidt {
4233d7728c96SJan Schmidt 	int ret = 0;
4234d7728c96SJan Schmidt 	int i;
4235740c3d22SChris Mason 	u64 rel_ptr;
4236d7728c96SJan Schmidt 	int size;
4237806468f8SChris Mason 	struct btrfs_ioctl_ino_path_args *ipa = NULL;
4238d7728c96SJan Schmidt 	struct inode_fs_paths *ipath = NULL;
4239d7728c96SJan Schmidt 	struct btrfs_path *path;
4240d7728c96SJan Schmidt 
424182b22ac8SKusanagi Kouichi 	if (!capable(CAP_DAC_READ_SEARCH))
4242d7728c96SJan Schmidt 		return -EPERM;
4243d7728c96SJan Schmidt 
4244d7728c96SJan Schmidt 	path = btrfs_alloc_path();
4245d7728c96SJan Schmidt 	if (!path) {
4246d7728c96SJan Schmidt 		ret = -ENOMEM;
4247d7728c96SJan Schmidt 		goto out;
4248d7728c96SJan Schmidt 	}
4249d7728c96SJan Schmidt 
4250d7728c96SJan Schmidt 	ipa = memdup_user(arg, sizeof(*ipa));
4251d7728c96SJan Schmidt 	if (IS_ERR(ipa)) {
4252d7728c96SJan Schmidt 		ret = PTR_ERR(ipa);
4253d7728c96SJan Schmidt 		ipa = NULL;
4254d7728c96SJan Schmidt 		goto out;
4255d7728c96SJan Schmidt 	}
4256d7728c96SJan Schmidt 
4257d7728c96SJan Schmidt 	size = min_t(u32, ipa->size, 4096);
4258d7728c96SJan Schmidt 	ipath = init_ipath(size, root, path);
4259d7728c96SJan Schmidt 	if (IS_ERR(ipath)) {
4260d7728c96SJan Schmidt 		ret = PTR_ERR(ipath);
4261d7728c96SJan Schmidt 		ipath = NULL;
4262d7728c96SJan Schmidt 		goto out;
4263d7728c96SJan Schmidt 	}
4264d7728c96SJan Schmidt 
4265d7728c96SJan Schmidt 	ret = paths_from_inode(ipa->inum, ipath);
4266d7728c96SJan Schmidt 	if (ret < 0)
4267d7728c96SJan Schmidt 		goto out;
4268d7728c96SJan Schmidt 
4269d7728c96SJan Schmidt 	for (i = 0; i < ipath->fspath->elem_cnt; ++i) {
4270745c4d8eSJeff Mahoney 		rel_ptr = ipath->fspath->val[i] -
4271745c4d8eSJeff Mahoney 			  (u64)(unsigned long)ipath->fspath->val;
4272740c3d22SChris Mason 		ipath->fspath->val[i] = rel_ptr;
4273d7728c96SJan Schmidt 	}
4274d7728c96SJan Schmidt 
4275745c4d8eSJeff Mahoney 	ret = copy_to_user((void *)(unsigned long)ipa->fspath,
4276745c4d8eSJeff Mahoney 			   (void *)(unsigned long)ipath->fspath, size);
4277d7728c96SJan Schmidt 	if (ret) {
4278d7728c96SJan Schmidt 		ret = -EFAULT;
4279d7728c96SJan Schmidt 		goto out;
4280d7728c96SJan Schmidt 	}
4281d7728c96SJan Schmidt 
4282d7728c96SJan Schmidt out:
4283d7728c96SJan Schmidt 	btrfs_free_path(path);
4284d7728c96SJan Schmidt 	free_ipath(ipath);
4285d7728c96SJan Schmidt 	kfree(ipa);
4286d7728c96SJan Schmidt 
4287d7728c96SJan Schmidt 	return ret;
4288d7728c96SJan Schmidt }
4289d7728c96SJan Schmidt 
4290d7728c96SJan Schmidt static int build_ino_list(u64 inum, u64 offset, u64 root, void *ctx)
4291d7728c96SJan Schmidt {
4292d7728c96SJan Schmidt 	struct btrfs_data_container *inodes = ctx;
4293d7728c96SJan Schmidt 	const size_t c = 3 * sizeof(u64);
4294d7728c96SJan Schmidt 
4295d7728c96SJan Schmidt 	if (inodes->bytes_left >= c) {
4296d7728c96SJan Schmidt 		inodes->bytes_left -= c;
4297d7728c96SJan Schmidt 		inodes->val[inodes->elem_cnt] = inum;
4298d7728c96SJan Schmidt 		inodes->val[inodes->elem_cnt + 1] = offset;
4299d7728c96SJan Schmidt 		inodes->val[inodes->elem_cnt + 2] = root;
4300d7728c96SJan Schmidt 		inodes->elem_cnt += 3;
4301d7728c96SJan Schmidt 	} else {
4302d7728c96SJan Schmidt 		inodes->bytes_missing += c - inodes->bytes_left;
4303d7728c96SJan Schmidt 		inodes->bytes_left = 0;
4304d7728c96SJan Schmidt 		inodes->elem_missed += 3;
4305d7728c96SJan Schmidt 	}
4306d7728c96SJan Schmidt 
4307d7728c96SJan Schmidt 	return 0;
4308d7728c96SJan Schmidt }
4309d7728c96SJan Schmidt 
4310d7728c96SJan Schmidt static long btrfs_ioctl_logical_to_ino(struct btrfs_root *root,
4311d7728c96SJan Schmidt 					void __user *arg)
4312d7728c96SJan Schmidt {
4313d7728c96SJan Schmidt 	int ret = 0;
4314d7728c96SJan Schmidt 	int size;
4315d7728c96SJan Schmidt 	struct btrfs_ioctl_logical_ino_args *loi;
4316d7728c96SJan Schmidt 	struct btrfs_data_container *inodes = NULL;
4317d7728c96SJan Schmidt 	struct btrfs_path *path = NULL;
4318d7728c96SJan Schmidt 
4319d7728c96SJan Schmidt 	if (!capable(CAP_SYS_ADMIN))
4320d7728c96SJan Schmidt 		return -EPERM;
4321d7728c96SJan Schmidt 
4322d7728c96SJan Schmidt 	loi = memdup_user(arg, sizeof(*loi));
4323d7728c96SJan Schmidt 	if (IS_ERR(loi)) {
4324d7728c96SJan Schmidt 		ret = PTR_ERR(loi);
4325d7728c96SJan Schmidt 		loi = NULL;
4326d7728c96SJan Schmidt 		goto out;
4327d7728c96SJan Schmidt 	}
4328d7728c96SJan Schmidt 
4329d7728c96SJan Schmidt 	path = btrfs_alloc_path();
4330d7728c96SJan Schmidt 	if (!path) {
4331d7728c96SJan Schmidt 		ret = -ENOMEM;
4332d7728c96SJan Schmidt 		goto out;
4333d7728c96SJan Schmidt 	}
4334d7728c96SJan Schmidt 
4335425d17a2SLiu Bo 	size = min_t(u32, loi->size, 64 * 1024);
4336d7728c96SJan Schmidt 	inodes = init_data_container(size);
4337d7728c96SJan Schmidt 	if (IS_ERR(inodes)) {
4338d7728c96SJan Schmidt 		ret = PTR_ERR(inodes);
4339d7728c96SJan Schmidt 		inodes = NULL;
4340d7728c96SJan Schmidt 		goto out;
4341d7728c96SJan Schmidt 	}
4342d7728c96SJan Schmidt 
4343df031f07SLiu Bo 	ret = iterate_inodes_from_logical(loi->logical, root->fs_info, path,
4344df031f07SLiu Bo 					  build_ino_list, inodes);
4345df031f07SLiu Bo 	if (ret == -EINVAL)
4346d7728c96SJan Schmidt 		ret = -ENOENT;
4347d7728c96SJan Schmidt 	if (ret < 0)
4348d7728c96SJan Schmidt 		goto out;
4349d7728c96SJan Schmidt 
4350745c4d8eSJeff Mahoney 	ret = copy_to_user((void *)(unsigned long)loi->inodes,
4351745c4d8eSJeff Mahoney 			   (void *)(unsigned long)inodes, size);
4352d7728c96SJan Schmidt 	if (ret)
4353d7728c96SJan Schmidt 		ret = -EFAULT;
4354d7728c96SJan Schmidt 
4355d7728c96SJan Schmidt out:
4356d7728c96SJan Schmidt 	btrfs_free_path(path);
4357425d17a2SLiu Bo 	vfree(inodes);
4358d7728c96SJan Schmidt 	kfree(loi);
4359d7728c96SJan Schmidt 
4360d7728c96SJan Schmidt 	return ret;
4361d7728c96SJan Schmidt }
4362d7728c96SJan Schmidt 
436319a39dceSIlya Dryomov void update_ioctl_balance_args(struct btrfs_fs_info *fs_info, int lock,
4364c9e9f97bSIlya Dryomov 			       struct btrfs_ioctl_balance_args *bargs)
4365c9e9f97bSIlya Dryomov {
4366c9e9f97bSIlya Dryomov 	struct btrfs_balance_control *bctl = fs_info->balance_ctl;
4367c9e9f97bSIlya Dryomov 
4368c9e9f97bSIlya Dryomov 	bargs->flags = bctl->flags;
4369c9e9f97bSIlya Dryomov 
4370837d5b6eSIlya Dryomov 	if (atomic_read(&fs_info->balance_running))
4371837d5b6eSIlya Dryomov 		bargs->state |= BTRFS_BALANCE_STATE_RUNNING;
4372837d5b6eSIlya Dryomov 	if (atomic_read(&fs_info->balance_pause_req))
4373837d5b6eSIlya Dryomov 		bargs->state |= BTRFS_BALANCE_STATE_PAUSE_REQ;
4374a7e99c69SIlya Dryomov 	if (atomic_read(&fs_info->balance_cancel_req))
4375a7e99c69SIlya Dryomov 		bargs->state |= BTRFS_BALANCE_STATE_CANCEL_REQ;
4376837d5b6eSIlya Dryomov 
4377c9e9f97bSIlya Dryomov 	memcpy(&bargs->data, &bctl->data, sizeof(bargs->data));
4378c9e9f97bSIlya Dryomov 	memcpy(&bargs->meta, &bctl->meta, sizeof(bargs->meta));
4379c9e9f97bSIlya Dryomov 	memcpy(&bargs->sys, &bctl->sys, sizeof(bargs->sys));
438019a39dceSIlya Dryomov 
438119a39dceSIlya Dryomov 	if (lock) {
438219a39dceSIlya Dryomov 		spin_lock(&fs_info->balance_lock);
438319a39dceSIlya Dryomov 		memcpy(&bargs->stat, &bctl->stat, sizeof(bargs->stat));
438419a39dceSIlya Dryomov 		spin_unlock(&fs_info->balance_lock);
438519a39dceSIlya Dryomov 	} else {
438619a39dceSIlya Dryomov 		memcpy(&bargs->stat, &bctl->stat, sizeof(bargs->stat));
438719a39dceSIlya Dryomov 	}
4388c9e9f97bSIlya Dryomov }
4389c9e9f97bSIlya Dryomov 
43909ba1f6e4SLiu Bo static long btrfs_ioctl_balance(struct file *file, void __user *arg)
4391c9e9f97bSIlya Dryomov {
4392496ad9aaSAl Viro 	struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
4393c9e9f97bSIlya Dryomov 	struct btrfs_fs_info *fs_info = root->fs_info;
4394c9e9f97bSIlya Dryomov 	struct btrfs_ioctl_balance_args *bargs;
4395c9e9f97bSIlya Dryomov 	struct btrfs_balance_control *bctl;
4396ed0fb78fSIlya Dryomov 	bool need_unlock; /* for mut. excl. ops lock */
4397c9e9f97bSIlya Dryomov 	int ret;
4398c9e9f97bSIlya Dryomov 
4399c9e9f97bSIlya Dryomov 	if (!capable(CAP_SYS_ADMIN))
4400c9e9f97bSIlya Dryomov 		return -EPERM;
4401c9e9f97bSIlya Dryomov 
4402e54bfa31SLiu Bo 	ret = mnt_want_write_file(file);
44039ba1f6e4SLiu Bo 	if (ret)
44049ba1f6e4SLiu Bo 		return ret;
44059ba1f6e4SLiu Bo 
4406ed0fb78fSIlya Dryomov again:
4407ed0fb78fSIlya Dryomov 	if (!atomic_xchg(&fs_info->mutually_exclusive_operation_running, 1)) {
4408c9e9f97bSIlya Dryomov 		mutex_lock(&fs_info->volume_mutex);
4409c9e9f97bSIlya Dryomov 		mutex_lock(&fs_info->balance_mutex);
4410ed0fb78fSIlya Dryomov 		need_unlock = true;
4411ed0fb78fSIlya Dryomov 		goto locked;
4412ed0fb78fSIlya Dryomov 	}
4413ed0fb78fSIlya Dryomov 
4414ed0fb78fSIlya Dryomov 	/*
4415ed0fb78fSIlya Dryomov 	 * mut. excl. ops lock is locked.  Three possibilites:
4416ed0fb78fSIlya Dryomov 	 *   (1) some other op is running
4417ed0fb78fSIlya Dryomov 	 *   (2) balance is running
4418ed0fb78fSIlya Dryomov 	 *   (3) balance is paused -- special case (think resume)
4419ed0fb78fSIlya Dryomov 	 */
4420ed0fb78fSIlya Dryomov 	mutex_lock(&fs_info->balance_mutex);
4421ed0fb78fSIlya Dryomov 	if (fs_info->balance_ctl) {
4422ed0fb78fSIlya Dryomov 		/* this is either (2) or (3) */
4423ed0fb78fSIlya Dryomov 		if (!atomic_read(&fs_info->balance_running)) {
4424ed0fb78fSIlya Dryomov 			mutex_unlock(&fs_info->balance_mutex);
4425ed0fb78fSIlya Dryomov 			if (!mutex_trylock(&fs_info->volume_mutex))
4426ed0fb78fSIlya Dryomov 				goto again;
4427ed0fb78fSIlya Dryomov 			mutex_lock(&fs_info->balance_mutex);
4428ed0fb78fSIlya Dryomov 
4429ed0fb78fSIlya Dryomov 			if (fs_info->balance_ctl &&
4430ed0fb78fSIlya Dryomov 			    !atomic_read(&fs_info->balance_running)) {
4431ed0fb78fSIlya Dryomov 				/* this is (3) */
4432ed0fb78fSIlya Dryomov 				need_unlock = false;
4433ed0fb78fSIlya Dryomov 				goto locked;
4434ed0fb78fSIlya Dryomov 			}
4435ed0fb78fSIlya Dryomov 
4436ed0fb78fSIlya Dryomov 			mutex_unlock(&fs_info->balance_mutex);
4437ed0fb78fSIlya Dryomov 			mutex_unlock(&fs_info->volume_mutex);
4438ed0fb78fSIlya Dryomov 			goto again;
4439ed0fb78fSIlya Dryomov 		} else {
4440ed0fb78fSIlya Dryomov 			/* this is (2) */
4441ed0fb78fSIlya Dryomov 			mutex_unlock(&fs_info->balance_mutex);
4442ed0fb78fSIlya Dryomov 			ret = -EINPROGRESS;
4443ed0fb78fSIlya Dryomov 			goto out;
4444ed0fb78fSIlya Dryomov 		}
4445ed0fb78fSIlya Dryomov 	} else {
4446ed0fb78fSIlya Dryomov 		/* this is (1) */
4447ed0fb78fSIlya Dryomov 		mutex_unlock(&fs_info->balance_mutex);
4448e57138b3SAnand Jain 		ret = BTRFS_ERROR_DEV_EXCL_RUN_IN_PROGRESS;
4449ed0fb78fSIlya Dryomov 		goto out;
4450ed0fb78fSIlya Dryomov 	}
4451ed0fb78fSIlya Dryomov 
4452ed0fb78fSIlya Dryomov locked:
4453ed0fb78fSIlya Dryomov 	BUG_ON(!atomic_read(&fs_info->mutually_exclusive_operation_running));
4454c9e9f97bSIlya Dryomov 
4455c9e9f97bSIlya Dryomov 	if (arg) {
4456c9e9f97bSIlya Dryomov 		bargs = memdup_user(arg, sizeof(*bargs));
4457c9e9f97bSIlya Dryomov 		if (IS_ERR(bargs)) {
4458c9e9f97bSIlya Dryomov 			ret = PTR_ERR(bargs);
4459ed0fb78fSIlya Dryomov 			goto out_unlock;
4460c9e9f97bSIlya Dryomov 		}
4461de322263SIlya Dryomov 
4462de322263SIlya Dryomov 		if (bargs->flags & BTRFS_BALANCE_RESUME) {
4463de322263SIlya Dryomov 			if (!fs_info->balance_ctl) {
4464de322263SIlya Dryomov 				ret = -ENOTCONN;
4465de322263SIlya Dryomov 				goto out_bargs;
4466de322263SIlya Dryomov 			}
4467de322263SIlya Dryomov 
4468de322263SIlya Dryomov 			bctl = fs_info->balance_ctl;
4469de322263SIlya Dryomov 			spin_lock(&fs_info->balance_lock);
4470de322263SIlya Dryomov 			bctl->flags |= BTRFS_BALANCE_RESUME;
4471de322263SIlya Dryomov 			spin_unlock(&fs_info->balance_lock);
4472de322263SIlya Dryomov 
4473de322263SIlya Dryomov 			goto do_balance;
4474de322263SIlya Dryomov 		}
4475c9e9f97bSIlya Dryomov 	} else {
4476c9e9f97bSIlya Dryomov 		bargs = NULL;
4477c9e9f97bSIlya Dryomov 	}
4478c9e9f97bSIlya Dryomov 
4479ed0fb78fSIlya Dryomov 	if (fs_info->balance_ctl) {
4480837d5b6eSIlya Dryomov 		ret = -EINPROGRESS;
4481837d5b6eSIlya Dryomov 		goto out_bargs;
4482837d5b6eSIlya Dryomov 	}
4483837d5b6eSIlya Dryomov 
4484c9e9f97bSIlya Dryomov 	bctl = kzalloc(sizeof(*bctl), GFP_NOFS);
4485c9e9f97bSIlya Dryomov 	if (!bctl) {
4486c9e9f97bSIlya Dryomov 		ret = -ENOMEM;
4487c9e9f97bSIlya Dryomov 		goto out_bargs;
4488c9e9f97bSIlya Dryomov 	}
4489c9e9f97bSIlya Dryomov 
4490c9e9f97bSIlya Dryomov 	bctl->fs_info = fs_info;
4491c9e9f97bSIlya Dryomov 	if (arg) {
4492c9e9f97bSIlya Dryomov 		memcpy(&bctl->data, &bargs->data, sizeof(bctl->data));
4493c9e9f97bSIlya Dryomov 		memcpy(&bctl->meta, &bargs->meta, sizeof(bctl->meta));
4494c9e9f97bSIlya Dryomov 		memcpy(&bctl->sys, &bargs->sys, sizeof(bctl->sys));
4495c9e9f97bSIlya Dryomov 
4496c9e9f97bSIlya Dryomov 		bctl->flags = bargs->flags;
4497f43ffb60SIlya Dryomov 	} else {
4498f43ffb60SIlya Dryomov 		/* balance everything - no filters */
4499f43ffb60SIlya Dryomov 		bctl->flags |= BTRFS_BALANCE_TYPE_MASK;
4500c9e9f97bSIlya Dryomov 	}
4501c9e9f97bSIlya Dryomov 
4502de322263SIlya Dryomov do_balance:
4503c9e9f97bSIlya Dryomov 	/*
4504ed0fb78fSIlya Dryomov 	 * Ownership of bctl and mutually_exclusive_operation_running
4505ed0fb78fSIlya Dryomov 	 * goes to to btrfs_balance.  bctl is freed in __cancel_balance,
4506ed0fb78fSIlya Dryomov 	 * or, if restriper was paused all the way until unmount, in
4507ed0fb78fSIlya Dryomov 	 * free_fs_info.  mutually_exclusive_operation_running is
4508ed0fb78fSIlya Dryomov 	 * cleared in __cancel_balance.
4509c9e9f97bSIlya Dryomov 	 */
4510ed0fb78fSIlya Dryomov 	need_unlock = false;
4511ed0fb78fSIlya Dryomov 
4512ed0fb78fSIlya Dryomov 	ret = btrfs_balance(bctl, bargs);
4513ed0fb78fSIlya Dryomov 
4514c9e9f97bSIlya Dryomov 	if (arg) {
4515c9e9f97bSIlya Dryomov 		if (copy_to_user(arg, bargs, sizeof(*bargs)))
4516c9e9f97bSIlya Dryomov 			ret = -EFAULT;
4517c9e9f97bSIlya Dryomov 	}
4518c9e9f97bSIlya Dryomov 
4519c9e9f97bSIlya Dryomov out_bargs:
4520c9e9f97bSIlya Dryomov 	kfree(bargs);
4521ed0fb78fSIlya Dryomov out_unlock:
4522c9e9f97bSIlya Dryomov 	mutex_unlock(&fs_info->balance_mutex);
4523c9e9f97bSIlya Dryomov 	mutex_unlock(&fs_info->volume_mutex);
4524ed0fb78fSIlya Dryomov 	if (need_unlock)
4525ed0fb78fSIlya Dryomov 		atomic_set(&fs_info->mutually_exclusive_operation_running, 0);
4526ed0fb78fSIlya Dryomov out:
4527e54bfa31SLiu Bo 	mnt_drop_write_file(file);
4528c9e9f97bSIlya Dryomov 	return ret;
4529c9e9f97bSIlya Dryomov }
4530c9e9f97bSIlya Dryomov 
4531837d5b6eSIlya Dryomov static long btrfs_ioctl_balance_ctl(struct btrfs_root *root, int cmd)
4532837d5b6eSIlya Dryomov {
4533837d5b6eSIlya Dryomov 	if (!capable(CAP_SYS_ADMIN))
4534837d5b6eSIlya Dryomov 		return -EPERM;
4535837d5b6eSIlya Dryomov 
4536837d5b6eSIlya Dryomov 	switch (cmd) {
4537837d5b6eSIlya Dryomov 	case BTRFS_BALANCE_CTL_PAUSE:
4538837d5b6eSIlya Dryomov 		return btrfs_pause_balance(root->fs_info);
4539a7e99c69SIlya Dryomov 	case BTRFS_BALANCE_CTL_CANCEL:
4540a7e99c69SIlya Dryomov 		return btrfs_cancel_balance(root->fs_info);
4541837d5b6eSIlya Dryomov 	}
4542837d5b6eSIlya Dryomov 
4543837d5b6eSIlya Dryomov 	return -EINVAL;
4544837d5b6eSIlya Dryomov }
4545837d5b6eSIlya Dryomov 
454619a39dceSIlya Dryomov static long btrfs_ioctl_balance_progress(struct btrfs_root *root,
454719a39dceSIlya Dryomov 					 void __user *arg)
454819a39dceSIlya Dryomov {
454919a39dceSIlya Dryomov 	struct btrfs_fs_info *fs_info = root->fs_info;
455019a39dceSIlya Dryomov 	struct btrfs_ioctl_balance_args *bargs;
455119a39dceSIlya Dryomov 	int ret = 0;
455219a39dceSIlya Dryomov 
455319a39dceSIlya Dryomov 	if (!capable(CAP_SYS_ADMIN))
455419a39dceSIlya Dryomov 		return -EPERM;
455519a39dceSIlya Dryomov 
455619a39dceSIlya Dryomov 	mutex_lock(&fs_info->balance_mutex);
455719a39dceSIlya Dryomov 	if (!fs_info->balance_ctl) {
455819a39dceSIlya Dryomov 		ret = -ENOTCONN;
455919a39dceSIlya Dryomov 		goto out;
456019a39dceSIlya Dryomov 	}
456119a39dceSIlya Dryomov 
456219a39dceSIlya Dryomov 	bargs = kzalloc(sizeof(*bargs), GFP_NOFS);
456319a39dceSIlya Dryomov 	if (!bargs) {
456419a39dceSIlya Dryomov 		ret = -ENOMEM;
456519a39dceSIlya Dryomov 		goto out;
456619a39dceSIlya Dryomov 	}
456719a39dceSIlya Dryomov 
456819a39dceSIlya Dryomov 	update_ioctl_balance_args(fs_info, 1, bargs);
456919a39dceSIlya Dryomov 
457019a39dceSIlya Dryomov 	if (copy_to_user(arg, bargs, sizeof(*bargs)))
457119a39dceSIlya Dryomov 		ret = -EFAULT;
457219a39dceSIlya Dryomov 
457319a39dceSIlya Dryomov 	kfree(bargs);
457419a39dceSIlya Dryomov out:
457519a39dceSIlya Dryomov 	mutex_unlock(&fs_info->balance_mutex);
457619a39dceSIlya Dryomov 	return ret;
457719a39dceSIlya Dryomov }
457819a39dceSIlya Dryomov 
4579905b0ddaSMiao Xie static long btrfs_ioctl_quota_ctl(struct file *file, void __user *arg)
45805d13a37bSArne Jansen {
4581496ad9aaSAl Viro 	struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
45825d13a37bSArne Jansen 	struct btrfs_ioctl_quota_ctl_args *sa;
45835d13a37bSArne Jansen 	struct btrfs_trans_handle *trans = NULL;
45845d13a37bSArne Jansen 	int ret;
45855d13a37bSArne Jansen 	int err;
45865d13a37bSArne Jansen 
45875d13a37bSArne Jansen 	if (!capable(CAP_SYS_ADMIN))
45885d13a37bSArne Jansen 		return -EPERM;
45895d13a37bSArne Jansen 
4590905b0ddaSMiao Xie 	ret = mnt_want_write_file(file);
4591905b0ddaSMiao Xie 	if (ret)
4592905b0ddaSMiao Xie 		return ret;
45935d13a37bSArne Jansen 
45945d13a37bSArne Jansen 	sa = memdup_user(arg, sizeof(*sa));
4595905b0ddaSMiao Xie 	if (IS_ERR(sa)) {
4596905b0ddaSMiao Xie 		ret = PTR_ERR(sa);
4597905b0ddaSMiao Xie 		goto drop_write;
4598905b0ddaSMiao Xie 	}
45995d13a37bSArne Jansen 
46007708f029SWang Shilong 	down_write(&root->fs_info->subvol_sem);
460192f183aaSWang Shilong 	trans = btrfs_start_transaction(root->fs_info->tree_root, 2);
46025d13a37bSArne Jansen 	if (IS_ERR(trans)) {
46035d13a37bSArne Jansen 		ret = PTR_ERR(trans);
46045d13a37bSArne Jansen 		goto out;
46055d13a37bSArne Jansen 	}
46065d13a37bSArne Jansen 
46075d13a37bSArne Jansen 	switch (sa->cmd) {
46085d13a37bSArne Jansen 	case BTRFS_QUOTA_CTL_ENABLE:
46095d13a37bSArne Jansen 		ret = btrfs_quota_enable(trans, root->fs_info);
46105d13a37bSArne Jansen 		break;
46115d13a37bSArne Jansen 	case BTRFS_QUOTA_CTL_DISABLE:
46125d13a37bSArne Jansen 		ret = btrfs_quota_disable(trans, root->fs_info);
46135d13a37bSArne Jansen 		break;
46145d13a37bSArne Jansen 	default:
46155d13a37bSArne Jansen 		ret = -EINVAL;
46165d13a37bSArne Jansen 		break;
46175d13a37bSArne Jansen 	}
46185d13a37bSArne Jansen 
461992f183aaSWang Shilong 	err = btrfs_commit_transaction(trans, root->fs_info->tree_root);
46205d13a37bSArne Jansen 	if (err && !ret)
46215d13a37bSArne Jansen 		ret = err;
46225d13a37bSArne Jansen out:
46235d13a37bSArne Jansen 	kfree(sa);
46247708f029SWang Shilong 	up_write(&root->fs_info->subvol_sem);
4625905b0ddaSMiao Xie drop_write:
4626905b0ddaSMiao Xie 	mnt_drop_write_file(file);
46275d13a37bSArne Jansen 	return ret;
46285d13a37bSArne Jansen }
46295d13a37bSArne Jansen 
4630905b0ddaSMiao Xie static long btrfs_ioctl_qgroup_assign(struct file *file, void __user *arg)
46315d13a37bSArne Jansen {
4632496ad9aaSAl Viro 	struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
46335d13a37bSArne Jansen 	struct btrfs_ioctl_qgroup_assign_args *sa;
46345d13a37bSArne Jansen 	struct btrfs_trans_handle *trans;
46355d13a37bSArne Jansen 	int ret;
46365d13a37bSArne Jansen 	int err;
46375d13a37bSArne Jansen 
46385d13a37bSArne Jansen 	if (!capable(CAP_SYS_ADMIN))
46395d13a37bSArne Jansen 		return -EPERM;
46405d13a37bSArne Jansen 
4641905b0ddaSMiao Xie 	ret = mnt_want_write_file(file);
4642905b0ddaSMiao Xie 	if (ret)
4643905b0ddaSMiao Xie 		return ret;
46445d13a37bSArne Jansen 
46455d13a37bSArne Jansen 	sa = memdup_user(arg, sizeof(*sa));
4646905b0ddaSMiao Xie 	if (IS_ERR(sa)) {
4647905b0ddaSMiao Xie 		ret = PTR_ERR(sa);
4648905b0ddaSMiao Xie 		goto drop_write;
4649905b0ddaSMiao Xie 	}
46505d13a37bSArne Jansen 
46515d13a37bSArne Jansen 	trans = btrfs_join_transaction(root);
46525d13a37bSArne Jansen 	if (IS_ERR(trans)) {
46535d13a37bSArne Jansen 		ret = PTR_ERR(trans);
46545d13a37bSArne Jansen 		goto out;
46555d13a37bSArne Jansen 	}
46565d13a37bSArne Jansen 
46575d13a37bSArne Jansen 	/* FIXME: check if the IDs really exist */
46585d13a37bSArne Jansen 	if (sa->assign) {
46595d13a37bSArne Jansen 		ret = btrfs_add_qgroup_relation(trans, root->fs_info,
46605d13a37bSArne Jansen 						sa->src, sa->dst);
46615d13a37bSArne Jansen 	} else {
46625d13a37bSArne Jansen 		ret = btrfs_del_qgroup_relation(trans, root->fs_info,
46635d13a37bSArne Jansen 						sa->src, sa->dst);
46645d13a37bSArne Jansen 	}
46655d13a37bSArne Jansen 
46665d13a37bSArne Jansen 	err = btrfs_end_transaction(trans, root);
46675d13a37bSArne Jansen 	if (err && !ret)
46685d13a37bSArne Jansen 		ret = err;
46695d13a37bSArne Jansen 
46705d13a37bSArne Jansen out:
46715d13a37bSArne Jansen 	kfree(sa);
4672905b0ddaSMiao Xie drop_write:
4673905b0ddaSMiao Xie 	mnt_drop_write_file(file);
46745d13a37bSArne Jansen 	return ret;
46755d13a37bSArne Jansen }
46765d13a37bSArne Jansen 
4677905b0ddaSMiao Xie static long btrfs_ioctl_qgroup_create(struct file *file, void __user *arg)
46785d13a37bSArne Jansen {
4679496ad9aaSAl Viro 	struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
46805d13a37bSArne Jansen 	struct btrfs_ioctl_qgroup_create_args *sa;
46815d13a37bSArne Jansen 	struct btrfs_trans_handle *trans;
46825d13a37bSArne Jansen 	int ret;
46835d13a37bSArne Jansen 	int err;
46845d13a37bSArne Jansen 
46855d13a37bSArne Jansen 	if (!capable(CAP_SYS_ADMIN))
46865d13a37bSArne Jansen 		return -EPERM;
46875d13a37bSArne Jansen 
4688905b0ddaSMiao Xie 	ret = mnt_want_write_file(file);
4689905b0ddaSMiao Xie 	if (ret)
4690905b0ddaSMiao Xie 		return ret;
46915d13a37bSArne Jansen 
46925d13a37bSArne Jansen 	sa = memdup_user(arg, sizeof(*sa));
4693905b0ddaSMiao Xie 	if (IS_ERR(sa)) {
4694905b0ddaSMiao Xie 		ret = PTR_ERR(sa);
4695905b0ddaSMiao Xie 		goto drop_write;
4696905b0ddaSMiao Xie 	}
46975d13a37bSArne Jansen 
4698d86e56cfSMiao Xie 	if (!sa->qgroupid) {
4699d86e56cfSMiao Xie 		ret = -EINVAL;
4700d86e56cfSMiao Xie 		goto out;
4701d86e56cfSMiao Xie 	}
4702d86e56cfSMiao Xie 
47035d13a37bSArne Jansen 	trans = btrfs_join_transaction(root);
47045d13a37bSArne Jansen 	if (IS_ERR(trans)) {
47055d13a37bSArne Jansen 		ret = PTR_ERR(trans);
47065d13a37bSArne Jansen 		goto out;
47075d13a37bSArne Jansen 	}
47085d13a37bSArne Jansen 
47095d13a37bSArne Jansen 	/* FIXME: check if the IDs really exist */
47105d13a37bSArne Jansen 	if (sa->create) {
47115d13a37bSArne Jansen 		ret = btrfs_create_qgroup(trans, root->fs_info, sa->qgroupid,
47125d13a37bSArne Jansen 					  NULL);
47135d13a37bSArne Jansen 	} else {
47145d13a37bSArne Jansen 		ret = btrfs_remove_qgroup(trans, root->fs_info, sa->qgroupid);
47155d13a37bSArne Jansen 	}
47165d13a37bSArne Jansen 
47175d13a37bSArne Jansen 	err = btrfs_end_transaction(trans, root);
47185d13a37bSArne Jansen 	if (err && !ret)
47195d13a37bSArne Jansen 		ret = err;
47205d13a37bSArne Jansen 
47215d13a37bSArne Jansen out:
47225d13a37bSArne Jansen 	kfree(sa);
4723905b0ddaSMiao Xie drop_write:
4724905b0ddaSMiao Xie 	mnt_drop_write_file(file);
47255d13a37bSArne Jansen 	return ret;
47265d13a37bSArne Jansen }
47275d13a37bSArne Jansen 
4728905b0ddaSMiao Xie static long btrfs_ioctl_qgroup_limit(struct file *file, void __user *arg)
47295d13a37bSArne Jansen {
4730496ad9aaSAl Viro 	struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
47315d13a37bSArne Jansen 	struct btrfs_ioctl_qgroup_limit_args *sa;
47325d13a37bSArne Jansen 	struct btrfs_trans_handle *trans;
47335d13a37bSArne Jansen 	int ret;
47345d13a37bSArne Jansen 	int err;
47355d13a37bSArne Jansen 	u64 qgroupid;
47365d13a37bSArne Jansen 
47375d13a37bSArne Jansen 	if (!capable(CAP_SYS_ADMIN))
47385d13a37bSArne Jansen 		return -EPERM;
47395d13a37bSArne Jansen 
4740905b0ddaSMiao Xie 	ret = mnt_want_write_file(file);
4741905b0ddaSMiao Xie 	if (ret)
4742905b0ddaSMiao Xie 		return ret;
47435d13a37bSArne Jansen 
47445d13a37bSArne Jansen 	sa = memdup_user(arg, sizeof(*sa));
4745905b0ddaSMiao Xie 	if (IS_ERR(sa)) {
4746905b0ddaSMiao Xie 		ret = PTR_ERR(sa);
4747905b0ddaSMiao Xie 		goto drop_write;
4748905b0ddaSMiao Xie 	}
47495d13a37bSArne Jansen 
47505d13a37bSArne Jansen 	trans = btrfs_join_transaction(root);
47515d13a37bSArne Jansen 	if (IS_ERR(trans)) {
47525d13a37bSArne Jansen 		ret = PTR_ERR(trans);
47535d13a37bSArne Jansen 		goto out;
47545d13a37bSArne Jansen 	}
47555d13a37bSArne Jansen 
47565d13a37bSArne Jansen 	qgroupid = sa->qgroupid;
47575d13a37bSArne Jansen 	if (!qgroupid) {
47585d13a37bSArne Jansen 		/* take the current subvol as qgroup */
47595d13a37bSArne Jansen 		qgroupid = root->root_key.objectid;
47605d13a37bSArne Jansen 	}
47615d13a37bSArne Jansen 
47625d13a37bSArne Jansen 	/* FIXME: check if the IDs really exist */
47635d13a37bSArne Jansen 	ret = btrfs_limit_qgroup(trans, root->fs_info, qgroupid, &sa->lim);
47645d13a37bSArne Jansen 
47655d13a37bSArne Jansen 	err = btrfs_end_transaction(trans, root);
47665d13a37bSArne Jansen 	if (err && !ret)
47675d13a37bSArne Jansen 		ret = err;
47685d13a37bSArne Jansen 
47695d13a37bSArne Jansen out:
47705d13a37bSArne Jansen 	kfree(sa);
4771905b0ddaSMiao Xie drop_write:
4772905b0ddaSMiao Xie 	mnt_drop_write_file(file);
47735d13a37bSArne Jansen 	return ret;
47745d13a37bSArne Jansen }
47755d13a37bSArne Jansen 
47762f232036SJan Schmidt static long btrfs_ioctl_quota_rescan(struct file *file, void __user *arg)
47772f232036SJan Schmidt {
47786d0379ecSAl Viro 	struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
47792f232036SJan Schmidt 	struct btrfs_ioctl_quota_rescan_args *qsa;
47802f232036SJan Schmidt 	int ret;
47812f232036SJan Schmidt 
47822f232036SJan Schmidt 	if (!capable(CAP_SYS_ADMIN))
47832f232036SJan Schmidt 		return -EPERM;
47842f232036SJan Schmidt 
47852f232036SJan Schmidt 	ret = mnt_want_write_file(file);
47862f232036SJan Schmidt 	if (ret)
47872f232036SJan Schmidt 		return ret;
47882f232036SJan Schmidt 
47892f232036SJan Schmidt 	qsa = memdup_user(arg, sizeof(*qsa));
47902f232036SJan Schmidt 	if (IS_ERR(qsa)) {
47912f232036SJan Schmidt 		ret = PTR_ERR(qsa);
47922f232036SJan Schmidt 		goto drop_write;
47932f232036SJan Schmidt 	}
47942f232036SJan Schmidt 
47952f232036SJan Schmidt 	if (qsa->flags) {
47962f232036SJan Schmidt 		ret = -EINVAL;
47972f232036SJan Schmidt 		goto out;
47982f232036SJan Schmidt 	}
47992f232036SJan Schmidt 
48002f232036SJan Schmidt 	ret = btrfs_qgroup_rescan(root->fs_info);
48012f232036SJan Schmidt 
48022f232036SJan Schmidt out:
48032f232036SJan Schmidt 	kfree(qsa);
48042f232036SJan Schmidt drop_write:
48052f232036SJan Schmidt 	mnt_drop_write_file(file);
48062f232036SJan Schmidt 	return ret;
48072f232036SJan Schmidt }
48082f232036SJan Schmidt 
48092f232036SJan Schmidt static long btrfs_ioctl_quota_rescan_status(struct file *file, void __user *arg)
48102f232036SJan Schmidt {
48116d0379ecSAl Viro 	struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
48122f232036SJan Schmidt 	struct btrfs_ioctl_quota_rescan_args *qsa;
48132f232036SJan Schmidt 	int ret = 0;
48142f232036SJan Schmidt 
48152f232036SJan Schmidt 	if (!capable(CAP_SYS_ADMIN))
48162f232036SJan Schmidt 		return -EPERM;
48172f232036SJan Schmidt 
48182f232036SJan Schmidt 	qsa = kzalloc(sizeof(*qsa), GFP_NOFS);
48192f232036SJan Schmidt 	if (!qsa)
48202f232036SJan Schmidt 		return -ENOMEM;
48212f232036SJan Schmidt 
48222f232036SJan Schmidt 	if (root->fs_info->qgroup_flags & BTRFS_QGROUP_STATUS_FLAG_RESCAN) {
48232f232036SJan Schmidt 		qsa->flags = 1;
48242f232036SJan Schmidt 		qsa->progress = root->fs_info->qgroup_rescan_progress.objectid;
48252f232036SJan Schmidt 	}
48262f232036SJan Schmidt 
48272f232036SJan Schmidt 	if (copy_to_user(arg, qsa, sizeof(*qsa)))
48282f232036SJan Schmidt 		ret = -EFAULT;
48292f232036SJan Schmidt 
48302f232036SJan Schmidt 	kfree(qsa);
48312f232036SJan Schmidt 	return ret;
48322f232036SJan Schmidt }
48332f232036SJan Schmidt 
483457254b6eSJan Schmidt static long btrfs_ioctl_quota_rescan_wait(struct file *file, void __user *arg)
483557254b6eSJan Schmidt {
483654563d41SAl Viro 	struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
483757254b6eSJan Schmidt 
483857254b6eSJan Schmidt 	if (!capable(CAP_SYS_ADMIN))
483957254b6eSJan Schmidt 		return -EPERM;
484057254b6eSJan Schmidt 
484157254b6eSJan Schmidt 	return btrfs_qgroup_wait_for_completion(root->fs_info);
484257254b6eSJan Schmidt }
484357254b6eSJan Schmidt 
4844abccd00fSHugo Mills static long _btrfs_ioctl_set_received_subvol(struct file *file,
4845abccd00fSHugo Mills 					    struct btrfs_ioctl_received_subvol_args *sa)
48468ea05e3aSAlexander Block {
4847496ad9aaSAl Viro 	struct inode *inode = file_inode(file);
48488ea05e3aSAlexander Block 	struct btrfs_root *root = BTRFS_I(inode)->root;
48498ea05e3aSAlexander Block 	struct btrfs_root_item *root_item = &root->root_item;
48508ea05e3aSAlexander Block 	struct btrfs_trans_handle *trans;
48518ea05e3aSAlexander Block 	struct timespec ct = CURRENT_TIME;
48528ea05e3aSAlexander Block 	int ret = 0;
4853dd5f9615SStefan Behrens 	int received_uuid_changed;
48548ea05e3aSAlexander Block 
4855bd60ea0fSDavid Sterba 	if (!inode_owner_or_capable(inode))
4856bd60ea0fSDavid Sterba 		return -EPERM;
4857bd60ea0fSDavid Sterba 
48588ea05e3aSAlexander Block 	ret = mnt_want_write_file(file);
48598ea05e3aSAlexander Block 	if (ret < 0)
48608ea05e3aSAlexander Block 		return ret;
48618ea05e3aSAlexander Block 
48628ea05e3aSAlexander Block 	down_write(&root->fs_info->subvol_sem);
48638ea05e3aSAlexander Block 
48648ea05e3aSAlexander Block 	if (btrfs_ino(inode) != BTRFS_FIRST_FREE_OBJECTID) {
48658ea05e3aSAlexander Block 		ret = -EINVAL;
48668ea05e3aSAlexander Block 		goto out;
48678ea05e3aSAlexander Block 	}
48688ea05e3aSAlexander Block 
48698ea05e3aSAlexander Block 	if (btrfs_root_readonly(root)) {
48708ea05e3aSAlexander Block 		ret = -EROFS;
48718ea05e3aSAlexander Block 		goto out;
48728ea05e3aSAlexander Block 	}
48738ea05e3aSAlexander Block 
4874dd5f9615SStefan Behrens 	/*
4875dd5f9615SStefan Behrens 	 * 1 - root item
4876dd5f9615SStefan Behrens 	 * 2 - uuid items (received uuid + subvol uuid)
4877dd5f9615SStefan Behrens 	 */
4878dd5f9615SStefan Behrens 	trans = btrfs_start_transaction(root, 3);
48798ea05e3aSAlexander Block 	if (IS_ERR(trans)) {
48808ea05e3aSAlexander Block 		ret = PTR_ERR(trans);
48818ea05e3aSAlexander Block 		trans = NULL;
48828ea05e3aSAlexander Block 		goto out;
48838ea05e3aSAlexander Block 	}
48848ea05e3aSAlexander Block 
48858ea05e3aSAlexander Block 	sa->rtransid = trans->transid;
48868ea05e3aSAlexander Block 	sa->rtime.sec = ct.tv_sec;
48878ea05e3aSAlexander Block 	sa->rtime.nsec = ct.tv_nsec;
48888ea05e3aSAlexander Block 
4889dd5f9615SStefan Behrens 	received_uuid_changed = memcmp(root_item->received_uuid, sa->uuid,
4890dd5f9615SStefan Behrens 				       BTRFS_UUID_SIZE);
4891dd5f9615SStefan Behrens 	if (received_uuid_changed &&
4892dd5f9615SStefan Behrens 	    !btrfs_is_empty_uuid(root_item->received_uuid))
4893dd5f9615SStefan Behrens 		btrfs_uuid_tree_rem(trans, root->fs_info->uuid_root,
4894dd5f9615SStefan Behrens 				    root_item->received_uuid,
4895dd5f9615SStefan Behrens 				    BTRFS_UUID_KEY_RECEIVED_SUBVOL,
4896dd5f9615SStefan Behrens 				    root->root_key.objectid);
48978ea05e3aSAlexander Block 	memcpy(root_item->received_uuid, sa->uuid, BTRFS_UUID_SIZE);
48988ea05e3aSAlexander Block 	btrfs_set_root_stransid(root_item, sa->stransid);
48998ea05e3aSAlexander Block 	btrfs_set_root_rtransid(root_item, sa->rtransid);
49003cae210fSQu Wenruo 	btrfs_set_stack_timespec_sec(&root_item->stime, sa->stime.sec);
49013cae210fSQu Wenruo 	btrfs_set_stack_timespec_nsec(&root_item->stime, sa->stime.nsec);
49023cae210fSQu Wenruo 	btrfs_set_stack_timespec_sec(&root_item->rtime, sa->rtime.sec);
49033cae210fSQu Wenruo 	btrfs_set_stack_timespec_nsec(&root_item->rtime, sa->rtime.nsec);
49048ea05e3aSAlexander Block 
49058ea05e3aSAlexander Block 	ret = btrfs_update_root(trans, root->fs_info->tree_root,
49068ea05e3aSAlexander Block 				&root->root_key, &root->root_item);
49078ea05e3aSAlexander Block 	if (ret < 0) {
49088ea05e3aSAlexander Block 		btrfs_end_transaction(trans, root);
49098ea05e3aSAlexander Block 		goto out;
4910dd5f9615SStefan Behrens 	}
4911dd5f9615SStefan Behrens 	if (received_uuid_changed && !btrfs_is_empty_uuid(sa->uuid)) {
4912dd5f9615SStefan Behrens 		ret = btrfs_uuid_tree_add(trans, root->fs_info->uuid_root,
4913dd5f9615SStefan Behrens 					  sa->uuid,
4914dd5f9615SStefan Behrens 					  BTRFS_UUID_KEY_RECEIVED_SUBVOL,
4915dd5f9615SStefan Behrens 					  root->root_key.objectid);
4916dd5f9615SStefan Behrens 		if (ret < 0 && ret != -EEXIST) {
4917dd5f9615SStefan Behrens 			btrfs_abort_transaction(trans, root, ret);
4918dd5f9615SStefan Behrens 			goto out;
4919dd5f9615SStefan Behrens 		}
4920dd5f9615SStefan Behrens 	}
49218ea05e3aSAlexander Block 	ret = btrfs_commit_transaction(trans, root);
4922dd5f9615SStefan Behrens 	if (ret < 0) {
4923dd5f9615SStefan Behrens 		btrfs_abort_transaction(trans, root, ret);
49248ea05e3aSAlexander Block 		goto out;
49258ea05e3aSAlexander Block 	}
49268ea05e3aSAlexander Block 
4927abccd00fSHugo Mills out:
4928abccd00fSHugo Mills 	up_write(&root->fs_info->subvol_sem);
4929abccd00fSHugo Mills 	mnt_drop_write_file(file);
4930abccd00fSHugo Mills 	return ret;
4931abccd00fSHugo Mills }
4932abccd00fSHugo Mills 
4933abccd00fSHugo Mills #ifdef CONFIG_64BIT
4934abccd00fSHugo Mills static long btrfs_ioctl_set_received_subvol_32(struct file *file,
4935abccd00fSHugo Mills 						void __user *arg)
4936abccd00fSHugo Mills {
4937abccd00fSHugo Mills 	struct btrfs_ioctl_received_subvol_args_32 *args32 = NULL;
4938abccd00fSHugo Mills 	struct btrfs_ioctl_received_subvol_args *args64 = NULL;
4939abccd00fSHugo Mills 	int ret = 0;
4940abccd00fSHugo Mills 
4941abccd00fSHugo Mills 	args32 = memdup_user(arg, sizeof(*args32));
4942abccd00fSHugo Mills 	if (IS_ERR(args32)) {
4943abccd00fSHugo Mills 		ret = PTR_ERR(args32);
4944abccd00fSHugo Mills 		args32 = NULL;
4945abccd00fSHugo Mills 		goto out;
4946abccd00fSHugo Mills 	}
4947abccd00fSHugo Mills 
4948abccd00fSHugo Mills 	args64 = kmalloc(sizeof(*args64), GFP_NOFS);
494984dbeb87SDan Carpenter 	if (!args64) {
495084dbeb87SDan Carpenter 		ret = -ENOMEM;
4951abccd00fSHugo Mills 		goto out;
4952abccd00fSHugo Mills 	}
4953abccd00fSHugo Mills 
4954abccd00fSHugo Mills 	memcpy(args64->uuid, args32->uuid, BTRFS_UUID_SIZE);
4955abccd00fSHugo Mills 	args64->stransid = args32->stransid;
4956abccd00fSHugo Mills 	args64->rtransid = args32->rtransid;
4957abccd00fSHugo Mills 	args64->stime.sec = args32->stime.sec;
4958abccd00fSHugo Mills 	args64->stime.nsec = args32->stime.nsec;
4959abccd00fSHugo Mills 	args64->rtime.sec = args32->rtime.sec;
4960abccd00fSHugo Mills 	args64->rtime.nsec = args32->rtime.nsec;
4961abccd00fSHugo Mills 	args64->flags = args32->flags;
4962abccd00fSHugo Mills 
4963abccd00fSHugo Mills 	ret = _btrfs_ioctl_set_received_subvol(file, args64);
4964abccd00fSHugo Mills 	if (ret)
4965abccd00fSHugo Mills 		goto out;
4966abccd00fSHugo Mills 
4967abccd00fSHugo Mills 	memcpy(args32->uuid, args64->uuid, BTRFS_UUID_SIZE);
4968abccd00fSHugo Mills 	args32->stransid = args64->stransid;
4969abccd00fSHugo Mills 	args32->rtransid = args64->rtransid;
4970abccd00fSHugo Mills 	args32->stime.sec = args64->stime.sec;
4971abccd00fSHugo Mills 	args32->stime.nsec = args64->stime.nsec;
4972abccd00fSHugo Mills 	args32->rtime.sec = args64->rtime.sec;
4973abccd00fSHugo Mills 	args32->rtime.nsec = args64->rtime.nsec;
4974abccd00fSHugo Mills 	args32->flags = args64->flags;
4975abccd00fSHugo Mills 
4976abccd00fSHugo Mills 	ret = copy_to_user(arg, args32, sizeof(*args32));
4977abccd00fSHugo Mills 	if (ret)
4978abccd00fSHugo Mills 		ret = -EFAULT;
4979abccd00fSHugo Mills 
4980abccd00fSHugo Mills out:
4981abccd00fSHugo Mills 	kfree(args32);
4982abccd00fSHugo Mills 	kfree(args64);
4983abccd00fSHugo Mills 	return ret;
4984abccd00fSHugo Mills }
4985abccd00fSHugo Mills #endif
4986abccd00fSHugo Mills 
4987abccd00fSHugo Mills static long btrfs_ioctl_set_received_subvol(struct file *file,
4988abccd00fSHugo Mills 					    void __user *arg)
4989abccd00fSHugo Mills {
4990abccd00fSHugo Mills 	struct btrfs_ioctl_received_subvol_args *sa = NULL;
4991abccd00fSHugo Mills 	int ret = 0;
4992abccd00fSHugo Mills 
4993abccd00fSHugo Mills 	sa = memdup_user(arg, sizeof(*sa));
4994abccd00fSHugo Mills 	if (IS_ERR(sa)) {
4995abccd00fSHugo Mills 		ret = PTR_ERR(sa);
4996abccd00fSHugo Mills 		sa = NULL;
4997abccd00fSHugo Mills 		goto out;
4998abccd00fSHugo Mills 	}
4999abccd00fSHugo Mills 
5000abccd00fSHugo Mills 	ret = _btrfs_ioctl_set_received_subvol(file, sa);
5001abccd00fSHugo Mills 
5002abccd00fSHugo Mills 	if (ret)
5003abccd00fSHugo Mills 		goto out;
5004abccd00fSHugo Mills 
50058ea05e3aSAlexander Block 	ret = copy_to_user(arg, sa, sizeof(*sa));
50068ea05e3aSAlexander Block 	if (ret)
50078ea05e3aSAlexander Block 		ret = -EFAULT;
50088ea05e3aSAlexander Block 
50098ea05e3aSAlexander Block out:
50108ea05e3aSAlexander Block 	kfree(sa);
50118ea05e3aSAlexander Block 	return ret;
50128ea05e3aSAlexander Block }
50138ea05e3aSAlexander Block 
5014867ab667Sjeff.liu static int btrfs_ioctl_get_fslabel(struct file *file, void __user *arg)
5015867ab667Sjeff.liu {
50166d0379ecSAl Viro 	struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
5017a1b83ac5SAnand Jain 	size_t len;
5018867ab667Sjeff.liu 	int ret;
5019a1b83ac5SAnand Jain 	char label[BTRFS_LABEL_SIZE];
5020a1b83ac5SAnand Jain 
5021a1b83ac5SAnand Jain 	spin_lock(&root->fs_info->super_lock);
5022a1b83ac5SAnand Jain 	memcpy(label, root->fs_info->super_copy->label, BTRFS_LABEL_SIZE);
5023a1b83ac5SAnand Jain 	spin_unlock(&root->fs_info->super_lock);
5024a1b83ac5SAnand Jain 
5025a1b83ac5SAnand Jain 	len = strnlen(label, BTRFS_LABEL_SIZE);
5026867ab667Sjeff.liu 
5027867ab667Sjeff.liu 	if (len == BTRFS_LABEL_SIZE) {
5028efe120a0SFrank Holton 		btrfs_warn(root->fs_info,
5029efe120a0SFrank Holton 			"label is too long, return the first %zu bytes", --len);
5030867ab667Sjeff.liu 	}
5031867ab667Sjeff.liu 
5032867ab667Sjeff.liu 	ret = copy_to_user(arg, label, len);
5033867ab667Sjeff.liu 
5034867ab667Sjeff.liu 	return ret ? -EFAULT : 0;
5035867ab667Sjeff.liu }
5036867ab667Sjeff.liu 
5037a8bfd4abSjeff.liu static int btrfs_ioctl_set_fslabel(struct file *file, void __user *arg)
5038a8bfd4abSjeff.liu {
50396d0379ecSAl Viro 	struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
5040a8bfd4abSjeff.liu 	struct btrfs_super_block *super_block = root->fs_info->super_copy;
5041a8bfd4abSjeff.liu 	struct btrfs_trans_handle *trans;
5042a8bfd4abSjeff.liu 	char label[BTRFS_LABEL_SIZE];
5043a8bfd4abSjeff.liu 	int ret;
5044a8bfd4abSjeff.liu 
5045a8bfd4abSjeff.liu 	if (!capable(CAP_SYS_ADMIN))
5046a8bfd4abSjeff.liu 		return -EPERM;
5047a8bfd4abSjeff.liu 
5048a8bfd4abSjeff.liu 	if (copy_from_user(label, arg, sizeof(label)))
5049a8bfd4abSjeff.liu 		return -EFAULT;
5050a8bfd4abSjeff.liu 
5051a8bfd4abSjeff.liu 	if (strnlen(label, BTRFS_LABEL_SIZE) == BTRFS_LABEL_SIZE) {
5052efe120a0SFrank Holton 		btrfs_err(root->fs_info, "unable to set label with more than %d bytes",
5053a8bfd4abSjeff.liu 		       BTRFS_LABEL_SIZE - 1);
5054a8bfd4abSjeff.liu 		return -EINVAL;
5055a8bfd4abSjeff.liu 	}
5056a8bfd4abSjeff.liu 
5057a8bfd4abSjeff.liu 	ret = mnt_want_write_file(file);
5058a8bfd4abSjeff.liu 	if (ret)
5059a8bfd4abSjeff.liu 		return ret;
5060a8bfd4abSjeff.liu 
5061a8bfd4abSjeff.liu 	trans = btrfs_start_transaction(root, 0);
5062a8bfd4abSjeff.liu 	if (IS_ERR(trans)) {
5063a8bfd4abSjeff.liu 		ret = PTR_ERR(trans);
5064a8bfd4abSjeff.liu 		goto out_unlock;
5065a8bfd4abSjeff.liu 	}
5066a8bfd4abSjeff.liu 
5067a1b83ac5SAnand Jain 	spin_lock(&root->fs_info->super_lock);
5068a8bfd4abSjeff.liu 	strcpy(super_block->label, label);
5069a1b83ac5SAnand Jain 	spin_unlock(&root->fs_info->super_lock);
5070d0270acaSJeff Mahoney 	ret = btrfs_commit_transaction(trans, root);
5071a8bfd4abSjeff.liu 
5072a8bfd4abSjeff.liu out_unlock:
5073a8bfd4abSjeff.liu 	mnt_drop_write_file(file);
5074a8bfd4abSjeff.liu 	return ret;
5075a8bfd4abSjeff.liu }
5076a8bfd4abSjeff.liu 
50772eaa055fSJeff Mahoney #define INIT_FEATURE_FLAGS(suffix) \
50782eaa055fSJeff Mahoney 	{ .compat_flags = BTRFS_FEATURE_COMPAT_##suffix, \
50792eaa055fSJeff Mahoney 	  .compat_ro_flags = BTRFS_FEATURE_COMPAT_RO_##suffix, \
50802eaa055fSJeff Mahoney 	  .incompat_flags = BTRFS_FEATURE_INCOMPAT_##suffix }
50812eaa055fSJeff Mahoney 
50822eaa055fSJeff Mahoney static int btrfs_ioctl_get_supported_features(struct file *file,
50832eaa055fSJeff Mahoney 					      void __user *arg)
50842eaa055fSJeff Mahoney {
50852eaa055fSJeff Mahoney 	static struct btrfs_ioctl_feature_flags features[3] = {
50862eaa055fSJeff Mahoney 		INIT_FEATURE_FLAGS(SUPP),
50872eaa055fSJeff Mahoney 		INIT_FEATURE_FLAGS(SAFE_SET),
50882eaa055fSJeff Mahoney 		INIT_FEATURE_FLAGS(SAFE_CLEAR)
50892eaa055fSJeff Mahoney 	};
50902eaa055fSJeff Mahoney 
50912eaa055fSJeff Mahoney 	if (copy_to_user(arg, &features, sizeof(features)))
50922eaa055fSJeff Mahoney 		return -EFAULT;
50932eaa055fSJeff Mahoney 
50942eaa055fSJeff Mahoney 	return 0;
50952eaa055fSJeff Mahoney }
50962eaa055fSJeff Mahoney 
50972eaa055fSJeff Mahoney static int btrfs_ioctl_get_features(struct file *file, void __user *arg)
50982eaa055fSJeff Mahoney {
50992eaa055fSJeff Mahoney 	struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
51002eaa055fSJeff Mahoney 	struct btrfs_super_block *super_block = root->fs_info->super_copy;
51012eaa055fSJeff Mahoney 	struct btrfs_ioctl_feature_flags features;
51022eaa055fSJeff Mahoney 
51032eaa055fSJeff Mahoney 	features.compat_flags = btrfs_super_compat_flags(super_block);
51042eaa055fSJeff Mahoney 	features.compat_ro_flags = btrfs_super_compat_ro_flags(super_block);
51052eaa055fSJeff Mahoney 	features.incompat_flags = btrfs_super_incompat_flags(super_block);
51062eaa055fSJeff Mahoney 
51072eaa055fSJeff Mahoney 	if (copy_to_user(arg, &features, sizeof(features)))
51082eaa055fSJeff Mahoney 		return -EFAULT;
51092eaa055fSJeff Mahoney 
51102eaa055fSJeff Mahoney 	return 0;
51112eaa055fSJeff Mahoney }
51122eaa055fSJeff Mahoney 
51133b02a68aSJeff Mahoney static int check_feature_bits(struct btrfs_root *root,
51143b02a68aSJeff Mahoney 			      enum btrfs_feature_set set,
51152eaa055fSJeff Mahoney 			      u64 change_mask, u64 flags, u64 supported_flags,
51162eaa055fSJeff Mahoney 			      u64 safe_set, u64 safe_clear)
51172eaa055fSJeff Mahoney {
51183b02a68aSJeff Mahoney 	const char *type = btrfs_feature_set_names[set];
51193b02a68aSJeff Mahoney 	char *names;
51202eaa055fSJeff Mahoney 	u64 disallowed, unsupported;
51212eaa055fSJeff Mahoney 	u64 set_mask = flags & change_mask;
51222eaa055fSJeff Mahoney 	u64 clear_mask = ~flags & change_mask;
51232eaa055fSJeff Mahoney 
51242eaa055fSJeff Mahoney 	unsupported = set_mask & ~supported_flags;
51252eaa055fSJeff Mahoney 	if (unsupported) {
51263b02a68aSJeff Mahoney 		names = btrfs_printable_features(set, unsupported);
51273b02a68aSJeff Mahoney 		if (names) {
51283b02a68aSJeff Mahoney 			btrfs_warn(root->fs_info,
51293b02a68aSJeff Mahoney 			   "this kernel does not support the %s feature bit%s",
51303b02a68aSJeff Mahoney 			   names, strchr(names, ',') ? "s" : "");
51313b02a68aSJeff Mahoney 			kfree(names);
51323b02a68aSJeff Mahoney 		} else
51332eaa055fSJeff Mahoney 			btrfs_warn(root->fs_info,
51342eaa055fSJeff Mahoney 			   "this kernel does not support %s bits 0x%llx",
51352eaa055fSJeff Mahoney 			   type, unsupported);
51362eaa055fSJeff Mahoney 		return -EOPNOTSUPP;
51372eaa055fSJeff Mahoney 	}
51382eaa055fSJeff Mahoney 
51392eaa055fSJeff Mahoney 	disallowed = set_mask & ~safe_set;
51402eaa055fSJeff Mahoney 	if (disallowed) {
51413b02a68aSJeff Mahoney 		names = btrfs_printable_features(set, disallowed);
51423b02a68aSJeff Mahoney 		if (names) {
51433b02a68aSJeff Mahoney 			btrfs_warn(root->fs_info,
51443b02a68aSJeff Mahoney 			   "can't set the %s feature bit%s while mounted",
51453b02a68aSJeff Mahoney 			   names, strchr(names, ',') ? "s" : "");
51463b02a68aSJeff Mahoney 			kfree(names);
51473b02a68aSJeff Mahoney 		} else
51482eaa055fSJeff Mahoney 			btrfs_warn(root->fs_info,
51492eaa055fSJeff Mahoney 			   "can't set %s bits 0x%llx while mounted",
51502eaa055fSJeff Mahoney 			   type, disallowed);
51512eaa055fSJeff Mahoney 		return -EPERM;
51522eaa055fSJeff Mahoney 	}
51532eaa055fSJeff Mahoney 
51542eaa055fSJeff Mahoney 	disallowed = clear_mask & ~safe_clear;
51552eaa055fSJeff Mahoney 	if (disallowed) {
51563b02a68aSJeff Mahoney 		names = btrfs_printable_features(set, disallowed);
51573b02a68aSJeff Mahoney 		if (names) {
51583b02a68aSJeff Mahoney 			btrfs_warn(root->fs_info,
51593b02a68aSJeff Mahoney 			   "can't clear the %s feature bit%s while mounted",
51603b02a68aSJeff Mahoney 			   names, strchr(names, ',') ? "s" : "");
51613b02a68aSJeff Mahoney 			kfree(names);
51623b02a68aSJeff Mahoney 		} else
51632eaa055fSJeff Mahoney 			btrfs_warn(root->fs_info,
51642eaa055fSJeff Mahoney 			   "can't clear %s bits 0x%llx while mounted",
51652eaa055fSJeff Mahoney 			   type, disallowed);
51662eaa055fSJeff Mahoney 		return -EPERM;
51672eaa055fSJeff Mahoney 	}
51682eaa055fSJeff Mahoney 
51692eaa055fSJeff Mahoney 	return 0;
51702eaa055fSJeff Mahoney }
51712eaa055fSJeff Mahoney 
51722eaa055fSJeff Mahoney #define check_feature(root, change_mask, flags, mask_base)	\
51733b02a68aSJeff Mahoney check_feature_bits(root, FEAT_##mask_base, change_mask, flags,	\
51742eaa055fSJeff Mahoney 		   BTRFS_FEATURE_ ## mask_base ## _SUPP,	\
51752eaa055fSJeff Mahoney 		   BTRFS_FEATURE_ ## mask_base ## _SAFE_SET,	\
51762eaa055fSJeff Mahoney 		   BTRFS_FEATURE_ ## mask_base ## _SAFE_CLEAR)
51772eaa055fSJeff Mahoney 
51782eaa055fSJeff Mahoney static int btrfs_ioctl_set_features(struct file *file, void __user *arg)
51792eaa055fSJeff Mahoney {
51802eaa055fSJeff Mahoney 	struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
51812eaa055fSJeff Mahoney 	struct btrfs_super_block *super_block = root->fs_info->super_copy;
51822eaa055fSJeff Mahoney 	struct btrfs_ioctl_feature_flags flags[2];
51832eaa055fSJeff Mahoney 	struct btrfs_trans_handle *trans;
51842eaa055fSJeff Mahoney 	u64 newflags;
51852eaa055fSJeff Mahoney 	int ret;
51862eaa055fSJeff Mahoney 
51872eaa055fSJeff Mahoney 	if (!capable(CAP_SYS_ADMIN))
51882eaa055fSJeff Mahoney 		return -EPERM;
51892eaa055fSJeff Mahoney 
51902eaa055fSJeff Mahoney 	if (copy_from_user(flags, arg, sizeof(flags)))
51912eaa055fSJeff Mahoney 		return -EFAULT;
51922eaa055fSJeff Mahoney 
51932eaa055fSJeff Mahoney 	/* Nothing to do */
51942eaa055fSJeff Mahoney 	if (!flags[0].compat_flags && !flags[0].compat_ro_flags &&
51952eaa055fSJeff Mahoney 	    !flags[0].incompat_flags)
51962eaa055fSJeff Mahoney 		return 0;
51972eaa055fSJeff Mahoney 
51982eaa055fSJeff Mahoney 	ret = check_feature(root, flags[0].compat_flags,
51992eaa055fSJeff Mahoney 			    flags[1].compat_flags, COMPAT);
52002eaa055fSJeff Mahoney 	if (ret)
52012eaa055fSJeff Mahoney 		return ret;
52022eaa055fSJeff Mahoney 
52032eaa055fSJeff Mahoney 	ret = check_feature(root, flags[0].compat_ro_flags,
52042eaa055fSJeff Mahoney 			    flags[1].compat_ro_flags, COMPAT_RO);
52052eaa055fSJeff Mahoney 	if (ret)
52062eaa055fSJeff Mahoney 		return ret;
52072eaa055fSJeff Mahoney 
52082eaa055fSJeff Mahoney 	ret = check_feature(root, flags[0].incompat_flags,
52092eaa055fSJeff Mahoney 			    flags[1].incompat_flags, INCOMPAT);
52102eaa055fSJeff Mahoney 	if (ret)
52112eaa055fSJeff Mahoney 		return ret;
52122eaa055fSJeff Mahoney 
52138051aa1aSDavid Sterba 	trans = btrfs_start_transaction(root, 0);
52142eaa055fSJeff Mahoney 	if (IS_ERR(trans))
52152eaa055fSJeff Mahoney 		return PTR_ERR(trans);
52162eaa055fSJeff Mahoney 
52172eaa055fSJeff Mahoney 	spin_lock(&root->fs_info->super_lock);
52182eaa055fSJeff Mahoney 	newflags = btrfs_super_compat_flags(super_block);
52192eaa055fSJeff Mahoney 	newflags |= flags[0].compat_flags & flags[1].compat_flags;
52202eaa055fSJeff Mahoney 	newflags &= ~(flags[0].compat_flags & ~flags[1].compat_flags);
52212eaa055fSJeff Mahoney 	btrfs_set_super_compat_flags(super_block, newflags);
52222eaa055fSJeff Mahoney 
52232eaa055fSJeff Mahoney 	newflags = btrfs_super_compat_ro_flags(super_block);
52242eaa055fSJeff Mahoney 	newflags |= flags[0].compat_ro_flags & flags[1].compat_ro_flags;
52252eaa055fSJeff Mahoney 	newflags &= ~(flags[0].compat_ro_flags & ~flags[1].compat_ro_flags);
52262eaa055fSJeff Mahoney 	btrfs_set_super_compat_ro_flags(super_block, newflags);
52272eaa055fSJeff Mahoney 
52282eaa055fSJeff Mahoney 	newflags = btrfs_super_incompat_flags(super_block);
52292eaa055fSJeff Mahoney 	newflags |= flags[0].incompat_flags & flags[1].incompat_flags;
52302eaa055fSJeff Mahoney 	newflags &= ~(flags[0].incompat_flags & ~flags[1].incompat_flags);
52312eaa055fSJeff Mahoney 	btrfs_set_super_incompat_flags(super_block, newflags);
52322eaa055fSJeff Mahoney 	spin_unlock(&root->fs_info->super_lock);
52332eaa055fSJeff Mahoney 
5234d0270acaSJeff Mahoney 	return btrfs_commit_transaction(trans, root);
52352eaa055fSJeff Mahoney }
52362eaa055fSJeff Mahoney 
5237f46b5a66SChristoph Hellwig long btrfs_ioctl(struct file *file, unsigned int
5238f46b5a66SChristoph Hellwig 		cmd, unsigned long arg)
5239f46b5a66SChristoph Hellwig {
5240496ad9aaSAl Viro 	struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
52414bcabaa3SChristoph Hellwig 	void __user *argp = (void __user *)arg;
5242f46b5a66SChristoph Hellwig 
5243f46b5a66SChristoph Hellwig 	switch (cmd) {
52446cbff00fSChristoph Hellwig 	case FS_IOC_GETFLAGS:
52456cbff00fSChristoph Hellwig 		return btrfs_ioctl_getflags(file, argp);
52466cbff00fSChristoph Hellwig 	case FS_IOC_SETFLAGS:
52476cbff00fSChristoph Hellwig 		return btrfs_ioctl_setflags(file, argp);
52486cbff00fSChristoph Hellwig 	case FS_IOC_GETVERSION:
52496cbff00fSChristoph Hellwig 		return btrfs_ioctl_getversion(file, argp);
5250f7039b1dSLi Dongyang 	case FITRIM:
5251f7039b1dSLi Dongyang 		return btrfs_ioctl_fitrim(file, argp);
5252f46b5a66SChristoph Hellwig 	case BTRFS_IOC_SNAP_CREATE:
5253fa0d2b9bSLi Zefan 		return btrfs_ioctl_snap_create(file, argp, 0);
5254fdfb1e4fSLi Zefan 	case BTRFS_IOC_SNAP_CREATE_V2:
5255fa0d2b9bSLi Zefan 		return btrfs_ioctl_snap_create_v2(file, argp, 0);
52563de4586cSChris Mason 	case BTRFS_IOC_SUBVOL_CREATE:
5257fa0d2b9bSLi Zefan 		return btrfs_ioctl_snap_create(file, argp, 1);
52586f72c7e2SArne Jansen 	case BTRFS_IOC_SUBVOL_CREATE_V2:
52596f72c7e2SArne Jansen 		return btrfs_ioctl_snap_create_v2(file, argp, 1);
526076dda93cSYan, Zheng 	case BTRFS_IOC_SNAP_DESTROY:
526176dda93cSYan, Zheng 		return btrfs_ioctl_snap_destroy(file, argp);
52620caa102dSLi Zefan 	case BTRFS_IOC_SUBVOL_GETFLAGS:
52630caa102dSLi Zefan 		return btrfs_ioctl_subvol_getflags(file, argp);
52640caa102dSLi Zefan 	case BTRFS_IOC_SUBVOL_SETFLAGS:
52650caa102dSLi Zefan 		return btrfs_ioctl_subvol_setflags(file, argp);
52666ef5ed0dSJosef Bacik 	case BTRFS_IOC_DEFAULT_SUBVOL:
52676ef5ed0dSJosef Bacik 		return btrfs_ioctl_default_subvol(file, argp);
5268f46b5a66SChristoph Hellwig 	case BTRFS_IOC_DEFRAG:
52691e701a32SChris Mason 		return btrfs_ioctl_defrag(file, NULL);
52701e701a32SChris Mason 	case BTRFS_IOC_DEFRAG_RANGE:
52711e701a32SChris Mason 		return btrfs_ioctl_defrag(file, argp);
5272f46b5a66SChristoph Hellwig 	case BTRFS_IOC_RESIZE:
5273198605a8SMiao Xie 		return btrfs_ioctl_resize(file, argp);
5274f46b5a66SChristoph Hellwig 	case BTRFS_IOC_ADD_DEV:
52754bcabaa3SChristoph Hellwig 		return btrfs_ioctl_add_dev(root, argp);
5276f46b5a66SChristoph Hellwig 	case BTRFS_IOC_RM_DEV:
5277da24927bSMiao Xie 		return btrfs_ioctl_rm_dev(file, argp);
5278475f6387SJan Schmidt 	case BTRFS_IOC_FS_INFO:
5279475f6387SJan Schmidt 		return btrfs_ioctl_fs_info(root, argp);
5280475f6387SJan Schmidt 	case BTRFS_IOC_DEV_INFO:
5281475f6387SJan Schmidt 		return btrfs_ioctl_dev_info(root, argp);
5282f46b5a66SChristoph Hellwig 	case BTRFS_IOC_BALANCE:
52839ba1f6e4SLiu Bo 		return btrfs_ioctl_balance(file, NULL);
5284f46b5a66SChristoph Hellwig 	case BTRFS_IOC_CLONE:
5285c5c9cd4dSSage Weil 		return btrfs_ioctl_clone(file, arg, 0, 0, 0);
5286c5c9cd4dSSage Weil 	case BTRFS_IOC_CLONE_RANGE:
52877a865e8aSChristoph Hellwig 		return btrfs_ioctl_clone_range(file, argp);
5288f46b5a66SChristoph Hellwig 	case BTRFS_IOC_TRANS_START:
5289f46b5a66SChristoph Hellwig 		return btrfs_ioctl_trans_start(file);
5290f46b5a66SChristoph Hellwig 	case BTRFS_IOC_TRANS_END:
5291f46b5a66SChristoph Hellwig 		return btrfs_ioctl_trans_end(file);
5292ac8e9819SChris Mason 	case BTRFS_IOC_TREE_SEARCH:
5293ac8e9819SChris Mason 		return btrfs_ioctl_tree_search(file, argp);
5294*cc68a8a5SGerhard Heift 	case BTRFS_IOC_TREE_SEARCH_V2:
5295*cc68a8a5SGerhard Heift 		return btrfs_ioctl_tree_search_v2(file, argp);
5296ac8e9819SChris Mason 	case BTRFS_IOC_INO_LOOKUP:
5297ac8e9819SChris Mason 		return btrfs_ioctl_ino_lookup(file, argp);
5298d7728c96SJan Schmidt 	case BTRFS_IOC_INO_PATHS:
5299d7728c96SJan Schmidt 		return btrfs_ioctl_ino_to_path(root, argp);
5300d7728c96SJan Schmidt 	case BTRFS_IOC_LOGICAL_INO:
5301d7728c96SJan Schmidt 		return btrfs_ioctl_logical_to_ino(root, argp);
53021406e432SJosef Bacik 	case BTRFS_IOC_SPACE_INFO:
53031406e432SJosef Bacik 		return btrfs_ioctl_space_info(root, argp);
53049b199859SFilipe David Borba Manana 	case BTRFS_IOC_SYNC: {
53059b199859SFilipe David Borba Manana 		int ret;
53069b199859SFilipe David Borba Manana 
53076c255e67SMiao Xie 		ret = btrfs_start_delalloc_roots(root->fs_info, 0, -1);
53089b199859SFilipe David Borba Manana 		if (ret)
53099b199859SFilipe David Borba Manana 			return ret;
53109b199859SFilipe David Borba Manana 		ret = btrfs_sync_fs(file->f_dentry->d_sb, 1);
53119b199859SFilipe David Borba Manana 		return ret;
53129b199859SFilipe David Borba Manana 	}
531346204592SSage Weil 	case BTRFS_IOC_START_SYNC:
53149a8c28beSMiao Xie 		return btrfs_ioctl_start_sync(root, argp);
531546204592SSage Weil 	case BTRFS_IOC_WAIT_SYNC:
53169a8c28beSMiao Xie 		return btrfs_ioctl_wait_sync(root, argp);
5317475f6387SJan Schmidt 	case BTRFS_IOC_SCRUB:
5318b8e95489SMiao Xie 		return btrfs_ioctl_scrub(file, argp);
5319475f6387SJan Schmidt 	case BTRFS_IOC_SCRUB_CANCEL:
5320475f6387SJan Schmidt 		return btrfs_ioctl_scrub_cancel(root, argp);
5321475f6387SJan Schmidt 	case BTRFS_IOC_SCRUB_PROGRESS:
5322475f6387SJan Schmidt 		return btrfs_ioctl_scrub_progress(root, argp);
5323c9e9f97bSIlya Dryomov 	case BTRFS_IOC_BALANCE_V2:
53249ba1f6e4SLiu Bo 		return btrfs_ioctl_balance(file, argp);
5325837d5b6eSIlya Dryomov 	case BTRFS_IOC_BALANCE_CTL:
5326837d5b6eSIlya Dryomov 		return btrfs_ioctl_balance_ctl(root, arg);
532719a39dceSIlya Dryomov 	case BTRFS_IOC_BALANCE_PROGRESS:
532819a39dceSIlya Dryomov 		return btrfs_ioctl_balance_progress(root, argp);
53298ea05e3aSAlexander Block 	case BTRFS_IOC_SET_RECEIVED_SUBVOL:
53308ea05e3aSAlexander Block 		return btrfs_ioctl_set_received_subvol(file, argp);
5331abccd00fSHugo Mills #ifdef CONFIG_64BIT
5332abccd00fSHugo Mills 	case BTRFS_IOC_SET_RECEIVED_SUBVOL_32:
5333abccd00fSHugo Mills 		return btrfs_ioctl_set_received_subvol_32(file, argp);
5334abccd00fSHugo Mills #endif
533531db9f7cSAlexander Block 	case BTRFS_IOC_SEND:
533631db9f7cSAlexander Block 		return btrfs_ioctl_send(file, argp);
5337c11d2c23SStefan Behrens 	case BTRFS_IOC_GET_DEV_STATS:
5338b27f7c0cSDavid Sterba 		return btrfs_ioctl_get_dev_stats(root, argp);
53395d13a37bSArne Jansen 	case BTRFS_IOC_QUOTA_CTL:
5340905b0ddaSMiao Xie 		return btrfs_ioctl_quota_ctl(file, argp);
53415d13a37bSArne Jansen 	case BTRFS_IOC_QGROUP_ASSIGN:
5342905b0ddaSMiao Xie 		return btrfs_ioctl_qgroup_assign(file, argp);
53435d13a37bSArne Jansen 	case BTRFS_IOC_QGROUP_CREATE:
5344905b0ddaSMiao Xie 		return btrfs_ioctl_qgroup_create(file, argp);
53455d13a37bSArne Jansen 	case BTRFS_IOC_QGROUP_LIMIT:
5346905b0ddaSMiao Xie 		return btrfs_ioctl_qgroup_limit(file, argp);
53472f232036SJan Schmidt 	case BTRFS_IOC_QUOTA_RESCAN:
53482f232036SJan Schmidt 		return btrfs_ioctl_quota_rescan(file, argp);
53492f232036SJan Schmidt 	case BTRFS_IOC_QUOTA_RESCAN_STATUS:
53502f232036SJan Schmidt 		return btrfs_ioctl_quota_rescan_status(file, argp);
535157254b6eSJan Schmidt 	case BTRFS_IOC_QUOTA_RESCAN_WAIT:
535257254b6eSJan Schmidt 		return btrfs_ioctl_quota_rescan_wait(file, argp);
53533f6bcfbdSStefan Behrens 	case BTRFS_IOC_DEV_REPLACE:
53543f6bcfbdSStefan Behrens 		return btrfs_ioctl_dev_replace(root, argp);
5355867ab667Sjeff.liu 	case BTRFS_IOC_GET_FSLABEL:
5356867ab667Sjeff.liu 		return btrfs_ioctl_get_fslabel(file, argp);
5357a8bfd4abSjeff.liu 	case BTRFS_IOC_SET_FSLABEL:
5358a8bfd4abSjeff.liu 		return btrfs_ioctl_set_fslabel(file, argp);
5359416161dbSMark Fasheh 	case BTRFS_IOC_FILE_EXTENT_SAME:
5360416161dbSMark Fasheh 		return btrfs_ioctl_file_extent_same(file, argp);
53612eaa055fSJeff Mahoney 	case BTRFS_IOC_GET_SUPPORTED_FEATURES:
53622eaa055fSJeff Mahoney 		return btrfs_ioctl_get_supported_features(file, argp);
53632eaa055fSJeff Mahoney 	case BTRFS_IOC_GET_FEATURES:
53642eaa055fSJeff Mahoney 		return btrfs_ioctl_get_features(file, argp);
53652eaa055fSJeff Mahoney 	case BTRFS_IOC_SET_FEATURES:
53662eaa055fSJeff Mahoney 		return btrfs_ioctl_set_features(file, argp);
5367f46b5a66SChristoph Hellwig 	}
5368f46b5a66SChristoph Hellwig 
5369f46b5a66SChristoph Hellwig 	return -ENOTTY;
5370f46b5a66SChristoph Hellwig }
5371