xref: /openbmc/linux/fs/btrfs/ioctl.c (revision 3cc79392)
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);
1393cc79392SFilipe Manana 	unsigned int new_fl = 0;
1406cbff00fSChristoph Hellwig 
1416cbff00fSChristoph Hellwig 	if (ip->flags & BTRFS_INODE_SYNC)
1423cc79392SFilipe Manana 		new_fl |= S_SYNC;
1436cbff00fSChristoph Hellwig 	if (ip->flags & BTRFS_INODE_IMMUTABLE)
1443cc79392SFilipe Manana 		new_fl |= S_IMMUTABLE;
1456cbff00fSChristoph Hellwig 	if (ip->flags & BTRFS_INODE_APPEND)
1463cc79392SFilipe Manana 		new_fl |= S_APPEND;
1476cbff00fSChristoph Hellwig 	if (ip->flags & BTRFS_INODE_NOATIME)
1483cc79392SFilipe Manana 		new_fl |= S_NOATIME;
1496cbff00fSChristoph Hellwig 	if (ip->flags & BTRFS_INODE_DIRSYNC)
1503cc79392SFilipe Manana 		new_fl |= S_DIRSYNC;
1513cc79392SFilipe Manana 
1523cc79392SFilipe Manana 	set_mask_bits(&inode->i_flags,
1533cc79392SFilipe Manana 		      S_SYNC | S_APPEND | S_IMMUTABLE | S_NOATIME | S_DIRSYNC,
1543cc79392SFilipe Manana 		      new_fl);
1556cbff00fSChristoph Hellwig }
1566cbff00fSChristoph Hellwig 
1576cbff00fSChristoph Hellwig /*
1586cbff00fSChristoph Hellwig  * Inherit flags from the parent inode.
1596cbff00fSChristoph Hellwig  *
160e27425d6SJosef Bacik  * Currently only the compression flags and the cow flags are inherited.
1616cbff00fSChristoph Hellwig  */
1626cbff00fSChristoph Hellwig void btrfs_inherit_iflags(struct inode *inode, struct inode *dir)
1636cbff00fSChristoph Hellwig {
1640b4dcea5SChris Mason 	unsigned int flags;
1650b4dcea5SChris Mason 
1660b4dcea5SChris Mason 	if (!dir)
1670b4dcea5SChris Mason 		return;
1680b4dcea5SChris Mason 
1690b4dcea5SChris Mason 	flags = BTRFS_I(dir)->flags;
1706cbff00fSChristoph Hellwig 
171e27425d6SJosef Bacik 	if (flags & BTRFS_INODE_NOCOMPRESS) {
172e27425d6SJosef Bacik 		BTRFS_I(inode)->flags &= ~BTRFS_INODE_COMPRESS;
173e27425d6SJosef Bacik 		BTRFS_I(inode)->flags |= BTRFS_INODE_NOCOMPRESS;
174e27425d6SJosef Bacik 	} else if (flags & BTRFS_INODE_COMPRESS) {
175e27425d6SJosef Bacik 		BTRFS_I(inode)->flags &= ~BTRFS_INODE_NOCOMPRESS;
176e27425d6SJosef Bacik 		BTRFS_I(inode)->flags |= BTRFS_INODE_COMPRESS;
177e27425d6SJosef Bacik 	}
1786cbff00fSChristoph Hellwig 
179213490b3SLiu Bo 	if (flags & BTRFS_INODE_NODATACOW) {
180e27425d6SJosef Bacik 		BTRFS_I(inode)->flags |= BTRFS_INODE_NODATACOW;
181213490b3SLiu Bo 		if (S_ISREG(inode->i_mode))
182213490b3SLiu Bo 			BTRFS_I(inode)->flags |= BTRFS_INODE_NODATASUM;
183213490b3SLiu Bo 	}
184e27425d6SJosef Bacik 
1856cbff00fSChristoph Hellwig 	btrfs_update_iflags(inode);
1866cbff00fSChristoph Hellwig }
1876cbff00fSChristoph Hellwig 
1886cbff00fSChristoph Hellwig static int btrfs_ioctl_getflags(struct file *file, void __user *arg)
1896cbff00fSChristoph Hellwig {
190496ad9aaSAl Viro 	struct btrfs_inode *ip = BTRFS_I(file_inode(file));
1916cbff00fSChristoph Hellwig 	unsigned int flags = btrfs_flags_to_ioctl(ip->flags);
1926cbff00fSChristoph Hellwig 
1936cbff00fSChristoph Hellwig 	if (copy_to_user(arg, &flags, sizeof(flags)))
1946cbff00fSChristoph Hellwig 		return -EFAULT;
1956cbff00fSChristoph Hellwig 	return 0;
1966cbff00fSChristoph Hellwig }
1976cbff00fSChristoph Hellwig 
19875e7cb7fSLiu Bo static int check_flags(unsigned int flags)
19975e7cb7fSLiu Bo {
20075e7cb7fSLiu Bo 	if (flags & ~(FS_IMMUTABLE_FL | FS_APPEND_FL | \
20175e7cb7fSLiu Bo 		      FS_NOATIME_FL | FS_NODUMP_FL | \
20275e7cb7fSLiu Bo 		      FS_SYNC_FL | FS_DIRSYNC_FL | \
203e1e8fb6aSLi Zefan 		      FS_NOCOMP_FL | FS_COMPR_FL |
204e1e8fb6aSLi Zefan 		      FS_NOCOW_FL))
20575e7cb7fSLiu Bo 		return -EOPNOTSUPP;
20675e7cb7fSLiu Bo 
20775e7cb7fSLiu Bo 	if ((flags & FS_NOCOMP_FL) && (flags & FS_COMPR_FL))
20875e7cb7fSLiu Bo 		return -EINVAL;
20975e7cb7fSLiu Bo 
21075e7cb7fSLiu Bo 	return 0;
21175e7cb7fSLiu Bo }
21275e7cb7fSLiu Bo 
2136cbff00fSChristoph Hellwig static int btrfs_ioctl_setflags(struct file *file, void __user *arg)
2146cbff00fSChristoph Hellwig {
215496ad9aaSAl Viro 	struct inode *inode = file_inode(file);
2166cbff00fSChristoph Hellwig 	struct btrfs_inode *ip = BTRFS_I(inode);
2176cbff00fSChristoph Hellwig 	struct btrfs_root *root = ip->root;
2186cbff00fSChristoph Hellwig 	struct btrfs_trans_handle *trans;
2196cbff00fSChristoph Hellwig 	unsigned int flags, oldflags;
2206cbff00fSChristoph Hellwig 	int ret;
221f062abf0SLi Zefan 	u64 ip_oldflags;
222f062abf0SLi Zefan 	unsigned int i_oldflags;
2237e97b8daSDavid Sterba 	umode_t mode;
2246cbff00fSChristoph Hellwig 
225bd60ea0fSDavid Sterba 	if (!inode_owner_or_capable(inode))
226bd60ea0fSDavid Sterba 		return -EPERM;
227bd60ea0fSDavid Sterba 
228b83cc969SLi Zefan 	if (btrfs_root_readonly(root))
229b83cc969SLi Zefan 		return -EROFS;
230b83cc969SLi Zefan 
2316cbff00fSChristoph Hellwig 	if (copy_from_user(&flags, arg, sizeof(flags)))
2326cbff00fSChristoph Hellwig 		return -EFAULT;
2336cbff00fSChristoph Hellwig 
23475e7cb7fSLiu Bo 	ret = check_flags(flags);
23575e7cb7fSLiu Bo 	if (ret)
23675e7cb7fSLiu Bo 		return ret;
2376cbff00fSChristoph Hellwig 
238e7848683SJan Kara 	ret = mnt_want_write_file(file);
239e7848683SJan Kara 	if (ret)
240e7848683SJan Kara 		return ret;
241e7848683SJan Kara 
2426cbff00fSChristoph Hellwig 	mutex_lock(&inode->i_mutex);
2436cbff00fSChristoph Hellwig 
244f062abf0SLi Zefan 	ip_oldflags = ip->flags;
245f062abf0SLi Zefan 	i_oldflags = inode->i_flags;
2467e97b8daSDavid Sterba 	mode = inode->i_mode;
247f062abf0SLi Zefan 
2486cbff00fSChristoph Hellwig 	flags = btrfs_mask_flags(inode->i_mode, flags);
2496cbff00fSChristoph Hellwig 	oldflags = btrfs_flags_to_ioctl(ip->flags);
2506cbff00fSChristoph Hellwig 	if ((flags ^ oldflags) & (FS_APPEND_FL | FS_IMMUTABLE_FL)) {
2516cbff00fSChristoph Hellwig 		if (!capable(CAP_LINUX_IMMUTABLE)) {
2526cbff00fSChristoph Hellwig 			ret = -EPERM;
2536cbff00fSChristoph Hellwig 			goto out_unlock;
2546cbff00fSChristoph Hellwig 		}
2556cbff00fSChristoph Hellwig 	}
2566cbff00fSChristoph Hellwig 
2576cbff00fSChristoph Hellwig 	if (flags & FS_SYNC_FL)
2586cbff00fSChristoph Hellwig 		ip->flags |= BTRFS_INODE_SYNC;
2596cbff00fSChristoph Hellwig 	else
2606cbff00fSChristoph Hellwig 		ip->flags &= ~BTRFS_INODE_SYNC;
2616cbff00fSChristoph Hellwig 	if (flags & FS_IMMUTABLE_FL)
2626cbff00fSChristoph Hellwig 		ip->flags |= BTRFS_INODE_IMMUTABLE;
2636cbff00fSChristoph Hellwig 	else
2646cbff00fSChristoph Hellwig 		ip->flags &= ~BTRFS_INODE_IMMUTABLE;
2656cbff00fSChristoph Hellwig 	if (flags & FS_APPEND_FL)
2666cbff00fSChristoph Hellwig 		ip->flags |= BTRFS_INODE_APPEND;
2676cbff00fSChristoph Hellwig 	else
2686cbff00fSChristoph Hellwig 		ip->flags &= ~BTRFS_INODE_APPEND;
2696cbff00fSChristoph Hellwig 	if (flags & FS_NODUMP_FL)
2706cbff00fSChristoph Hellwig 		ip->flags |= BTRFS_INODE_NODUMP;
2716cbff00fSChristoph Hellwig 	else
2726cbff00fSChristoph Hellwig 		ip->flags &= ~BTRFS_INODE_NODUMP;
2736cbff00fSChristoph Hellwig 	if (flags & FS_NOATIME_FL)
2746cbff00fSChristoph Hellwig 		ip->flags |= BTRFS_INODE_NOATIME;
2756cbff00fSChristoph Hellwig 	else
2766cbff00fSChristoph Hellwig 		ip->flags &= ~BTRFS_INODE_NOATIME;
2776cbff00fSChristoph Hellwig 	if (flags & FS_DIRSYNC_FL)
2786cbff00fSChristoph Hellwig 		ip->flags |= BTRFS_INODE_DIRSYNC;
2796cbff00fSChristoph Hellwig 	else
2806cbff00fSChristoph Hellwig 		ip->flags &= ~BTRFS_INODE_DIRSYNC;
2817e97b8daSDavid Sterba 	if (flags & FS_NOCOW_FL) {
2827e97b8daSDavid Sterba 		if (S_ISREG(mode)) {
2837e97b8daSDavid Sterba 			/*
2847e97b8daSDavid Sterba 			 * It's safe to turn csums off here, no extents exist.
2857e97b8daSDavid Sterba 			 * Otherwise we want the flag to reflect the real COW
2867e97b8daSDavid Sterba 			 * status of the file and will not set it.
2877e97b8daSDavid Sterba 			 */
2887e97b8daSDavid Sterba 			if (inode->i_size == 0)
2897e97b8daSDavid Sterba 				ip->flags |= BTRFS_INODE_NODATACOW
2907e97b8daSDavid Sterba 					   | BTRFS_INODE_NODATASUM;
2917e97b8daSDavid Sterba 		} else {
292e1e8fb6aSLi Zefan 			ip->flags |= BTRFS_INODE_NODATACOW;
2937e97b8daSDavid Sterba 		}
2947e97b8daSDavid Sterba 	} else {
2957e97b8daSDavid Sterba 		/*
2967e97b8daSDavid Sterba 		 * Revert back under same assuptions as above
2977e97b8daSDavid Sterba 		 */
2987e97b8daSDavid Sterba 		if (S_ISREG(mode)) {
2997e97b8daSDavid Sterba 			if (inode->i_size == 0)
3007e97b8daSDavid Sterba 				ip->flags &= ~(BTRFS_INODE_NODATACOW
3017e97b8daSDavid Sterba 				             | BTRFS_INODE_NODATASUM);
3027e97b8daSDavid Sterba 		} else {
303e1e8fb6aSLi Zefan 			ip->flags &= ~BTRFS_INODE_NODATACOW;
3047e97b8daSDavid Sterba 		}
3057e97b8daSDavid Sterba 	}
3066cbff00fSChristoph Hellwig 
30775e7cb7fSLiu Bo 	/*
30875e7cb7fSLiu Bo 	 * The COMPRESS flag can only be changed by users, while the NOCOMPRESS
30975e7cb7fSLiu Bo 	 * flag may be changed automatically if compression code won't make
31075e7cb7fSLiu Bo 	 * things smaller.
31175e7cb7fSLiu Bo 	 */
31275e7cb7fSLiu Bo 	if (flags & FS_NOCOMP_FL) {
31375e7cb7fSLiu Bo 		ip->flags &= ~BTRFS_INODE_COMPRESS;
31475e7cb7fSLiu Bo 		ip->flags |= BTRFS_INODE_NOCOMPRESS;
31563541927SFilipe David Borba Manana 
31663541927SFilipe David Borba Manana 		ret = btrfs_set_prop(inode, "btrfs.compression", NULL, 0, 0);
31763541927SFilipe David Borba Manana 		if (ret && ret != -ENODATA)
31863541927SFilipe David Borba Manana 			goto out_drop;
31975e7cb7fSLiu Bo 	} else if (flags & FS_COMPR_FL) {
32063541927SFilipe David Borba Manana 		const char *comp;
32163541927SFilipe David Borba Manana 
32275e7cb7fSLiu Bo 		ip->flags |= BTRFS_INODE_COMPRESS;
32375e7cb7fSLiu Bo 		ip->flags &= ~BTRFS_INODE_NOCOMPRESS;
32463541927SFilipe David Borba Manana 
32563541927SFilipe David Borba Manana 		if (root->fs_info->compress_type == BTRFS_COMPRESS_LZO)
32663541927SFilipe David Borba Manana 			comp = "lzo";
32763541927SFilipe David Borba Manana 		else
32863541927SFilipe David Borba Manana 			comp = "zlib";
32963541927SFilipe David Borba Manana 		ret = btrfs_set_prop(inode, "btrfs.compression",
33063541927SFilipe David Borba Manana 				     comp, strlen(comp), 0);
33163541927SFilipe David Borba Manana 		if (ret)
33263541927SFilipe David Borba Manana 			goto out_drop;
33363541927SFilipe David Borba Manana 
334ebcb904dSLi Zefan 	} else {
335ebcb904dSLi Zefan 		ip->flags &= ~(BTRFS_INODE_COMPRESS | BTRFS_INODE_NOCOMPRESS);
33675e7cb7fSLiu Bo 	}
3376cbff00fSChristoph Hellwig 
3384da6f1a3SLi Zefan 	trans = btrfs_start_transaction(root, 1);
339f062abf0SLi Zefan 	if (IS_ERR(trans)) {
340f062abf0SLi Zefan 		ret = PTR_ERR(trans);
341f062abf0SLi Zefan 		goto out_drop;
342f062abf0SLi Zefan 	}
3436cbff00fSChristoph Hellwig 
344306424ccSLi Zefan 	btrfs_update_iflags(inode);
3450c4d2d95SJosef Bacik 	inode_inc_iversion(inode);
346306424ccSLi Zefan 	inode->i_ctime = CURRENT_TIME;
3476cbff00fSChristoph Hellwig 	ret = btrfs_update_inode(trans, root, inode);
3486cbff00fSChristoph Hellwig 
3496cbff00fSChristoph Hellwig 	btrfs_end_transaction(trans, root);
350f062abf0SLi Zefan  out_drop:
351f062abf0SLi Zefan 	if (ret) {
352f062abf0SLi Zefan 		ip->flags = ip_oldflags;
353f062abf0SLi Zefan 		inode->i_flags = i_oldflags;
354f062abf0SLi Zefan 	}
3556cbff00fSChristoph Hellwig 
3566cbff00fSChristoph Hellwig  out_unlock:
3576cbff00fSChristoph Hellwig 	mutex_unlock(&inode->i_mutex);
358e7848683SJan Kara 	mnt_drop_write_file(file);
3592d4e6f6aSliubo 	return ret;
3606cbff00fSChristoph Hellwig }
3616cbff00fSChristoph Hellwig 
3626cbff00fSChristoph Hellwig static int btrfs_ioctl_getversion(struct file *file, int __user *arg)
3636cbff00fSChristoph Hellwig {
364496ad9aaSAl Viro 	struct inode *inode = file_inode(file);
3656cbff00fSChristoph Hellwig 
3666cbff00fSChristoph Hellwig 	return put_user(inode->i_generation, arg);
3676cbff00fSChristoph Hellwig }
368f46b5a66SChristoph Hellwig 
369f7039b1dSLi Dongyang static noinline int btrfs_ioctl_fitrim(struct file *file, void __user *arg)
370f7039b1dSLi Dongyang {
37154563d41SAl Viro 	struct btrfs_fs_info *fs_info = btrfs_sb(file_inode(file)->i_sb);
372f7039b1dSLi Dongyang 	struct btrfs_device *device;
373f7039b1dSLi Dongyang 	struct request_queue *q;
374f7039b1dSLi Dongyang 	struct fstrim_range range;
375f7039b1dSLi Dongyang 	u64 minlen = ULLONG_MAX;
376f7039b1dSLi Dongyang 	u64 num_devices = 0;
377815745cfSAl Viro 	u64 total_bytes = btrfs_super_total_bytes(fs_info->super_copy);
378f7039b1dSLi Dongyang 	int ret;
379f7039b1dSLi Dongyang 
380f7039b1dSLi Dongyang 	if (!capable(CAP_SYS_ADMIN))
381f7039b1dSLi Dongyang 		return -EPERM;
382f7039b1dSLi Dongyang 
3831f78160cSXiao Guangrong 	rcu_read_lock();
3841f78160cSXiao Guangrong 	list_for_each_entry_rcu(device, &fs_info->fs_devices->devices,
3851f78160cSXiao Guangrong 				dev_list) {
386f7039b1dSLi Dongyang 		if (!device->bdev)
387f7039b1dSLi Dongyang 			continue;
388f7039b1dSLi Dongyang 		q = bdev_get_queue(device->bdev);
389f7039b1dSLi Dongyang 		if (blk_queue_discard(q)) {
390f7039b1dSLi Dongyang 			num_devices++;
391f7039b1dSLi Dongyang 			minlen = min((u64)q->limits.discard_granularity,
392f7039b1dSLi Dongyang 				     minlen);
393f7039b1dSLi Dongyang 		}
394f7039b1dSLi Dongyang 	}
3951f78160cSXiao Guangrong 	rcu_read_unlock();
396f4c697e6SLukas Czerner 
397f7039b1dSLi Dongyang 	if (!num_devices)
398f7039b1dSLi Dongyang 		return -EOPNOTSUPP;
399f7039b1dSLi Dongyang 	if (copy_from_user(&range, arg, sizeof(range)))
400f7039b1dSLi Dongyang 		return -EFAULT;
401e515c18bSLukas Czerner 	if (range.start > total_bytes ||
402e515c18bSLukas Czerner 	    range.len < fs_info->sb->s_blocksize)
403f4c697e6SLukas Czerner 		return -EINVAL;
404f7039b1dSLi Dongyang 
405f4c697e6SLukas Czerner 	range.len = min(range.len, total_bytes - range.start);
406f7039b1dSLi Dongyang 	range.minlen = max(range.minlen, minlen);
407815745cfSAl Viro 	ret = btrfs_trim_fs(fs_info->tree_root, &range);
408f7039b1dSLi Dongyang 	if (ret < 0)
409f7039b1dSLi Dongyang 		return ret;
410f7039b1dSLi Dongyang 
411f7039b1dSLi Dongyang 	if (copy_to_user(arg, &range, sizeof(range)))
412f7039b1dSLi Dongyang 		return -EFAULT;
413f7039b1dSLi Dongyang 
414f7039b1dSLi Dongyang 	return 0;
415f7039b1dSLi Dongyang }
416f7039b1dSLi Dongyang 
417dd5f9615SStefan Behrens int btrfs_is_empty_uuid(u8 *uuid)
418dd5f9615SStefan Behrens {
41946e0f66aSChris Mason 	int i;
42046e0f66aSChris Mason 
42146e0f66aSChris Mason 	for (i = 0; i < BTRFS_UUID_SIZE; i++) {
42246e0f66aSChris Mason 		if (uuid[i])
42346e0f66aSChris Mason 			return 0;
42446e0f66aSChris Mason 	}
42546e0f66aSChris Mason 	return 1;
426dd5f9615SStefan Behrens }
427dd5f9615SStefan Behrens 
428d5c12070SMiao Xie static noinline int create_subvol(struct inode *dir,
429cb8e7090SChristoph Hellwig 				  struct dentry *dentry,
43072fd032eSSage Weil 				  char *name, int namelen,
4316f72c7e2SArne Jansen 				  u64 *async_transid,
4328696c533SMiao Xie 				  struct btrfs_qgroup_inherit *inherit)
433f46b5a66SChristoph Hellwig {
434f46b5a66SChristoph Hellwig 	struct btrfs_trans_handle *trans;
435f46b5a66SChristoph Hellwig 	struct btrfs_key key;
436f46b5a66SChristoph Hellwig 	struct btrfs_root_item root_item;
437f46b5a66SChristoph Hellwig 	struct btrfs_inode_item *inode_item;
438f46b5a66SChristoph Hellwig 	struct extent_buffer *leaf;
439d5c12070SMiao Xie 	struct btrfs_root *root = BTRFS_I(dir)->root;
44076dda93cSYan, Zheng 	struct btrfs_root *new_root;
441d5c12070SMiao Xie 	struct btrfs_block_rsv block_rsv;
4428ea05e3aSAlexander Block 	struct timespec cur_time = CURRENT_TIME;
4435662344bSTsutomu Itoh 	struct inode *inode;
444f46b5a66SChristoph Hellwig 	int ret;
445f46b5a66SChristoph Hellwig 	int err;
446f46b5a66SChristoph Hellwig 	u64 objectid;
447f46b5a66SChristoph Hellwig 	u64 new_dirid = BTRFS_FIRST_FREE_OBJECTID;
4483de4586cSChris Mason 	u64 index = 0;
449d5c12070SMiao Xie 	u64 qgroup_reserved;
4508ea05e3aSAlexander Block 	uuid_le new_uuid;
451f46b5a66SChristoph Hellwig 
452581bb050SLi Zefan 	ret = btrfs_find_free_objectid(root->fs_info->tree_root, &objectid);
4532fbe8c8aSAl Viro 	if (ret)
454a22285a6SYan, Zheng 		return ret;
4556a912213SJosef Bacik 
456d5c12070SMiao Xie 	btrfs_init_block_rsv(&block_rsv, BTRFS_BLOCK_RSV_TEMP);
4579ed74f2dSJosef Bacik 	/*
458d5c12070SMiao Xie 	 * The same as the snapshot creation, please see the comment
459d5c12070SMiao Xie 	 * of create_snapshot().
4609ed74f2dSJosef Bacik 	 */
461d5c12070SMiao Xie 	ret = btrfs_subvolume_reserve_metadata(root, &block_rsv,
462dd5f9615SStefan Behrens 					       8, &qgroup_reserved, false);
463d5c12070SMiao Xie 	if (ret)
464d5c12070SMiao Xie 		return ret;
465f46b5a66SChristoph Hellwig 
466d5c12070SMiao Xie 	trans = btrfs_start_transaction(root, 0);
467d5c12070SMiao Xie 	if (IS_ERR(trans)) {
468d5c12070SMiao Xie 		ret = PTR_ERR(trans);
469de6e8200SLiu Bo 		btrfs_subvolume_release_metadata(root, &block_rsv,
470de6e8200SLiu Bo 						 qgroup_reserved);
471de6e8200SLiu Bo 		return ret;
472d5c12070SMiao Xie 	}
473d5c12070SMiao Xie 	trans->block_rsv = &block_rsv;
474d5c12070SMiao Xie 	trans->bytes_reserved = block_rsv.size;
475f46b5a66SChristoph Hellwig 
4768696c533SMiao Xie 	ret = btrfs_qgroup_inherit(trans, root->fs_info, 0, objectid, inherit);
4776f72c7e2SArne Jansen 	if (ret)
4786f72c7e2SArne Jansen 		goto fail;
4796f72c7e2SArne Jansen 
4805d4f98a2SYan Zheng 	leaf = btrfs_alloc_free_block(trans, root, root->leafsize,
4815581a51aSJan Schmidt 				      0, objectid, NULL, 0, 0, 0);
4828e8a1e31SJosef Bacik 	if (IS_ERR(leaf)) {
4838e8a1e31SJosef Bacik 		ret = PTR_ERR(leaf);
4848e8a1e31SJosef Bacik 		goto fail;
4858e8a1e31SJosef Bacik 	}
486f46b5a66SChristoph Hellwig 
4875d4f98a2SYan Zheng 	memset_extent_buffer(leaf, 0, 0, sizeof(struct btrfs_header));
488f46b5a66SChristoph Hellwig 	btrfs_set_header_bytenr(leaf, leaf->start);
489f46b5a66SChristoph Hellwig 	btrfs_set_header_generation(leaf, trans->transid);
4905d4f98a2SYan Zheng 	btrfs_set_header_backref_rev(leaf, BTRFS_MIXED_BACKREF_REV);
491f46b5a66SChristoph Hellwig 	btrfs_set_header_owner(leaf, objectid);
492f46b5a66SChristoph Hellwig 
4930a4e5586SRoss Kirk 	write_extent_buffer(leaf, root->fs_info->fsid, btrfs_header_fsid(),
494f46b5a66SChristoph Hellwig 			    BTRFS_FSID_SIZE);
4955d4f98a2SYan Zheng 	write_extent_buffer(leaf, root->fs_info->chunk_tree_uuid,
496b308bc2fSGeert Uytterhoeven 			    btrfs_header_chunk_tree_uuid(leaf),
4975d4f98a2SYan Zheng 			    BTRFS_UUID_SIZE);
498f46b5a66SChristoph Hellwig 	btrfs_mark_buffer_dirty(leaf);
499f46b5a66SChristoph Hellwig 
5008ea05e3aSAlexander Block 	memset(&root_item, 0, sizeof(root_item));
5018ea05e3aSAlexander Block 
502f46b5a66SChristoph Hellwig 	inode_item = &root_item.inode;
5033cae210fSQu Wenruo 	btrfs_set_stack_inode_generation(inode_item, 1);
5043cae210fSQu Wenruo 	btrfs_set_stack_inode_size(inode_item, 3);
5053cae210fSQu Wenruo 	btrfs_set_stack_inode_nlink(inode_item, 1);
5063cae210fSQu Wenruo 	btrfs_set_stack_inode_nbytes(inode_item, root->leafsize);
5073cae210fSQu Wenruo 	btrfs_set_stack_inode_mode(inode_item, S_IFDIR | 0755);
508f46b5a66SChristoph Hellwig 
5093cae210fSQu Wenruo 	btrfs_set_root_flags(&root_item, 0);
5103cae210fSQu Wenruo 	btrfs_set_root_limit(&root_item, 0);
5113cae210fSQu Wenruo 	btrfs_set_stack_inode_flags(inode_item, BTRFS_INODE_ROOT_ITEM_INIT);
51208fe4db1SLi Zefan 
513f46b5a66SChristoph Hellwig 	btrfs_set_root_bytenr(&root_item, leaf->start);
51484234f3aSYan Zheng 	btrfs_set_root_generation(&root_item, trans->transid);
515f46b5a66SChristoph Hellwig 	btrfs_set_root_level(&root_item, 0);
516f46b5a66SChristoph Hellwig 	btrfs_set_root_refs(&root_item, 1);
51786b9f2ecSYan, Zheng 	btrfs_set_root_used(&root_item, leaf->len);
51880ff3856SYan Zheng 	btrfs_set_root_last_snapshot(&root_item, 0);
519f46b5a66SChristoph Hellwig 
5208ea05e3aSAlexander Block 	btrfs_set_root_generation_v2(&root_item,
5218ea05e3aSAlexander Block 			btrfs_root_generation(&root_item));
5228ea05e3aSAlexander Block 	uuid_le_gen(&new_uuid);
5238ea05e3aSAlexander Block 	memcpy(root_item.uuid, new_uuid.b, BTRFS_UUID_SIZE);
5243cae210fSQu Wenruo 	btrfs_set_stack_timespec_sec(&root_item.otime, cur_time.tv_sec);
5253cae210fSQu Wenruo 	btrfs_set_stack_timespec_nsec(&root_item.otime, cur_time.tv_nsec);
5268ea05e3aSAlexander Block 	root_item.ctime = root_item.otime;
5278ea05e3aSAlexander Block 	btrfs_set_root_ctransid(&root_item, trans->transid);
5288ea05e3aSAlexander Block 	btrfs_set_root_otransid(&root_item, trans->transid);
529f46b5a66SChristoph Hellwig 
530925baeddSChris Mason 	btrfs_tree_unlock(leaf);
531f46b5a66SChristoph Hellwig 	free_extent_buffer(leaf);
532f46b5a66SChristoph Hellwig 	leaf = NULL;
533f46b5a66SChristoph Hellwig 
534f46b5a66SChristoph Hellwig 	btrfs_set_root_dirid(&root_item, new_dirid);
535f46b5a66SChristoph Hellwig 
536f46b5a66SChristoph Hellwig 	key.objectid = objectid;
5375d4f98a2SYan Zheng 	key.offset = 0;
538f46b5a66SChristoph Hellwig 	btrfs_set_key_type(&key, BTRFS_ROOT_ITEM_KEY);
539f46b5a66SChristoph Hellwig 	ret = btrfs_insert_root(trans, root->fs_info->tree_root, &key,
540f46b5a66SChristoph Hellwig 				&root_item);
541f46b5a66SChristoph Hellwig 	if (ret)
542f46b5a66SChristoph Hellwig 		goto fail;
543f46b5a66SChristoph Hellwig 
54476dda93cSYan, Zheng 	key.offset = (u64)-1;
54576dda93cSYan, Zheng 	new_root = btrfs_read_fs_root_no_name(root->fs_info, &key);
54679787eaaSJeff Mahoney 	if (IS_ERR(new_root)) {
54779787eaaSJeff Mahoney 		btrfs_abort_transaction(trans, root, PTR_ERR(new_root));
54879787eaaSJeff Mahoney 		ret = PTR_ERR(new_root);
54979787eaaSJeff Mahoney 		goto fail;
55079787eaaSJeff Mahoney 	}
55176dda93cSYan, Zheng 
55276dda93cSYan, Zheng 	btrfs_record_root_in_trans(trans, new_root);
55376dda93cSYan, Zheng 
55463541927SFilipe David Borba Manana 	ret = btrfs_create_subvol_root(trans, new_root, root, new_dirid);
555ce598979SMark Fasheh 	if (ret) {
556ce598979SMark Fasheh 		/* We potentially lose an unused inode item here */
55779787eaaSJeff Mahoney 		btrfs_abort_transaction(trans, root, ret);
558ce598979SMark Fasheh 		goto fail;
559ce598979SMark Fasheh 	}
560ce598979SMark Fasheh 
561f46b5a66SChristoph Hellwig 	/*
562f46b5a66SChristoph Hellwig 	 * insert the directory item
563f46b5a66SChristoph Hellwig 	 */
5643de4586cSChris Mason 	ret = btrfs_set_inode_index(dir, &index);
56579787eaaSJeff Mahoney 	if (ret) {
56679787eaaSJeff Mahoney 		btrfs_abort_transaction(trans, root, ret);
56779787eaaSJeff Mahoney 		goto fail;
56879787eaaSJeff Mahoney 	}
5693de4586cSChris Mason 
5703de4586cSChris Mason 	ret = btrfs_insert_dir_item(trans, root,
57116cdcec7SMiao Xie 				    name, namelen, dir, &key,
5723de4586cSChris Mason 				    BTRFS_FT_DIR, index);
57379787eaaSJeff Mahoney 	if (ret) {
57479787eaaSJeff Mahoney 		btrfs_abort_transaction(trans, root, ret);
575f46b5a66SChristoph Hellwig 		goto fail;
57679787eaaSJeff Mahoney 	}
5770660b5afSChris Mason 
57852c26179SYan Zheng 	btrfs_i_size_write(dir, dir->i_size + namelen * 2);
57952c26179SYan Zheng 	ret = btrfs_update_inode(trans, root, dir);
58052c26179SYan Zheng 	BUG_ON(ret);
58152c26179SYan Zheng 
5820660b5afSChris Mason 	ret = btrfs_add_root_ref(trans, root->fs_info->tree_root,
5834df27c4dSYan, Zheng 				 objectid, root->root_key.objectid,
58433345d01SLi Zefan 				 btrfs_ino(dir), index, name, namelen);
5850660b5afSChris Mason 	BUG_ON(ret);
5860660b5afSChris Mason 
587dd5f9615SStefan Behrens 	ret = btrfs_uuid_tree_add(trans, root->fs_info->uuid_root,
588dd5f9615SStefan Behrens 				  root_item.uuid, BTRFS_UUID_KEY_SUBVOL,
589dd5f9615SStefan Behrens 				  objectid);
590dd5f9615SStefan Behrens 	if (ret)
591dd5f9615SStefan Behrens 		btrfs_abort_transaction(trans, root, ret);
592dd5f9615SStefan Behrens 
593f46b5a66SChristoph Hellwig fail:
594d5c12070SMiao Xie 	trans->block_rsv = NULL;
595d5c12070SMiao Xie 	trans->bytes_reserved = 0;
596de6e8200SLiu Bo 	btrfs_subvolume_release_metadata(root, &block_rsv, qgroup_reserved);
597de6e8200SLiu Bo 
59872fd032eSSage Weil 	if (async_transid) {
59972fd032eSSage Weil 		*async_transid = trans->transid;
60072fd032eSSage Weil 		err = btrfs_commit_transaction_async(trans, root, 1);
60100d71c9cSMiao Xie 		if (err)
60200d71c9cSMiao Xie 			err = btrfs_commit_transaction(trans, root);
60372fd032eSSage Weil 	} else {
60476dda93cSYan, Zheng 		err = btrfs_commit_transaction(trans, root);
60572fd032eSSage Weil 	}
606f46b5a66SChristoph Hellwig 	if (err && !ret)
607f46b5a66SChristoph Hellwig 		ret = err;
6081a65e24bSChris Mason 
6095662344bSTsutomu Itoh 	if (!ret) {
6105662344bSTsutomu Itoh 		inode = btrfs_lookup_dentry(dir, dentry);
611de6e8200SLiu Bo 		if (IS_ERR(inode))
612de6e8200SLiu Bo 			return PTR_ERR(inode);
6135662344bSTsutomu Itoh 		d_instantiate(dentry, inode);
6145662344bSTsutomu Itoh 	}
615f46b5a66SChristoph Hellwig 	return ret;
616f46b5a66SChristoph Hellwig }
617f46b5a66SChristoph Hellwig 
6188257b2dcSMiao Xie static void btrfs_wait_nocow_write(struct btrfs_root *root)
6198257b2dcSMiao Xie {
6208257b2dcSMiao Xie 	s64 writers;
6218257b2dcSMiao Xie 	DEFINE_WAIT(wait);
6228257b2dcSMiao Xie 
6238257b2dcSMiao Xie 	do {
6248257b2dcSMiao Xie 		prepare_to_wait(&root->subv_writers->wait, &wait,
6258257b2dcSMiao Xie 				TASK_UNINTERRUPTIBLE);
6268257b2dcSMiao Xie 
6278257b2dcSMiao Xie 		writers = percpu_counter_sum(&root->subv_writers->counter);
6288257b2dcSMiao Xie 		if (writers)
6298257b2dcSMiao Xie 			schedule();
6308257b2dcSMiao Xie 
6318257b2dcSMiao Xie 		finish_wait(&root->subv_writers->wait, &wait);
6328257b2dcSMiao Xie 	} while (writers);
6338257b2dcSMiao Xie }
6348257b2dcSMiao Xie 
635e9662f70SMiao Xie static int create_snapshot(struct btrfs_root *root, struct inode *dir,
636e9662f70SMiao Xie 			   struct dentry *dentry, char *name, int namelen,
637e9662f70SMiao Xie 			   u64 *async_transid, bool readonly,
638e9662f70SMiao Xie 			   struct btrfs_qgroup_inherit *inherit)
639f46b5a66SChristoph Hellwig {
6402e4bfab9SYan, Zheng 	struct inode *inode;
641f46b5a66SChristoph Hellwig 	struct btrfs_pending_snapshot *pending_snapshot;
642f46b5a66SChristoph Hellwig 	struct btrfs_trans_handle *trans;
6432e4bfab9SYan, Zheng 	int ret;
644f46b5a66SChristoph Hellwig 
64527cdeb70SMiao Xie 	if (!test_bit(BTRFS_ROOT_REF_COWS, &root->state))
646f46b5a66SChristoph Hellwig 		return -EINVAL;
647f46b5a66SChristoph Hellwig 
6488257b2dcSMiao Xie 	atomic_inc(&root->will_be_snapshoted);
6498257b2dcSMiao Xie 	smp_mb__after_atomic_inc();
6508257b2dcSMiao Xie 	btrfs_wait_nocow_write(root);
6518257b2dcSMiao Xie 
6526a03843dSMiao Xie 	ret = btrfs_start_delalloc_inodes(root, 0);
6536a03843dSMiao Xie 	if (ret)
6548257b2dcSMiao Xie 		goto out;
6556a03843dSMiao Xie 
656b0244199SMiao Xie 	btrfs_wait_ordered_extents(root, -1);
6576a03843dSMiao Xie 
658a22285a6SYan, Zheng 	pending_snapshot = kzalloc(sizeof(*pending_snapshot), GFP_NOFS);
6598257b2dcSMiao Xie 	if (!pending_snapshot) {
6608257b2dcSMiao Xie 		ret = -ENOMEM;
6618257b2dcSMiao Xie 		goto out;
6628257b2dcSMiao Xie 	}
663a22285a6SYan, Zheng 
66466d8f3ddSMiao Xie 	btrfs_init_block_rsv(&pending_snapshot->block_rsv,
66566d8f3ddSMiao Xie 			     BTRFS_BLOCK_RSV_TEMP);
666d5c12070SMiao Xie 	/*
667d5c12070SMiao Xie 	 * 1 - parent dir inode
668d5c12070SMiao Xie 	 * 2 - dir entries
669d5c12070SMiao Xie 	 * 1 - root item
670d5c12070SMiao Xie 	 * 2 - root ref/backref
671d5c12070SMiao Xie 	 * 1 - root of snapshot
672dd5f9615SStefan Behrens 	 * 1 - UUID item
673d5c12070SMiao Xie 	 */
674d5c12070SMiao Xie 	ret = btrfs_subvolume_reserve_metadata(BTRFS_I(dir)->root,
675dd5f9615SStefan Behrens 					&pending_snapshot->block_rsv, 8,
676ee3441b4SJeff Mahoney 					&pending_snapshot->qgroup_reserved,
677ee3441b4SJeff Mahoney 					false);
678d5c12070SMiao Xie 	if (ret)
6798257b2dcSMiao Xie 		goto free;
680d5c12070SMiao Xie 
681a22285a6SYan, Zheng 	pending_snapshot->dentry = dentry;
682a22285a6SYan, Zheng 	pending_snapshot->root = root;
683b83cc969SLi Zefan 	pending_snapshot->readonly = readonly;
684e9662f70SMiao Xie 	pending_snapshot->dir = dir;
6858696c533SMiao Xie 	pending_snapshot->inherit = inherit;
686a22285a6SYan, Zheng 
687d5c12070SMiao Xie 	trans = btrfs_start_transaction(root, 0);
688a22285a6SYan, Zheng 	if (IS_ERR(trans)) {
689a22285a6SYan, Zheng 		ret = PTR_ERR(trans);
690a22285a6SYan, Zheng 		goto fail;
691a22285a6SYan, Zheng 	}
692a22285a6SYan, Zheng 
6938351583eSJosef Bacik 	spin_lock(&root->fs_info->trans_lock);
694a22285a6SYan, Zheng 	list_add(&pending_snapshot->list,
695a22285a6SYan, Zheng 		 &trans->transaction->pending_snapshots);
6968351583eSJosef Bacik 	spin_unlock(&root->fs_info->trans_lock);
69772fd032eSSage Weil 	if (async_transid) {
69872fd032eSSage Weil 		*async_transid = trans->transid;
69972fd032eSSage Weil 		ret = btrfs_commit_transaction_async(trans,
70072fd032eSSage Weil 				     root->fs_info->extent_root, 1);
70100d71c9cSMiao Xie 		if (ret)
70200d71c9cSMiao Xie 			ret = btrfs_commit_transaction(trans, root);
70372fd032eSSage Weil 	} else {
70472fd032eSSage Weil 		ret = btrfs_commit_transaction(trans,
70572fd032eSSage Weil 					       root->fs_info->extent_root);
70672fd032eSSage Weil 	}
707aec8030aSMiao Xie 	if (ret)
708c37b2b62SJosef Bacik 		goto fail;
709a22285a6SYan, Zheng 
710a22285a6SYan, Zheng 	ret = pending_snapshot->error;
711f46b5a66SChristoph Hellwig 	if (ret)
7122e4bfab9SYan, Zheng 		goto fail;
713f46b5a66SChristoph Hellwig 
71466b4ffd1SJosef Bacik 	ret = btrfs_orphan_cleanup(pending_snapshot->snap);
71566b4ffd1SJosef Bacik 	if (ret)
71666b4ffd1SJosef Bacik 		goto fail;
717f46b5a66SChristoph Hellwig 
7183821f348SFilipe Manana 	/*
7193821f348SFilipe Manana 	 * If orphan cleanup did remove any orphans, it means the tree was
7203821f348SFilipe Manana 	 * modified and therefore the commit root is not the same as the
7213821f348SFilipe Manana 	 * current root anymore. This is a problem, because send uses the
7223821f348SFilipe Manana 	 * commit root and therefore can see inode items that don't exist
7233821f348SFilipe Manana 	 * in the current root anymore, and for example make calls to
7243821f348SFilipe Manana 	 * btrfs_iget, which will do tree lookups based on the current root
7253821f348SFilipe Manana 	 * and not on the commit root. Those lookups will fail, returning a
7263821f348SFilipe Manana 	 * -ESTALE error, and making send fail with that error. So make sure
7273821f348SFilipe Manana 	 * a send does not see any orphans we have just removed, and that it
7283821f348SFilipe Manana 	 * will see the same inodes regardless of whether a transaction
7293821f348SFilipe Manana 	 * commit happened before it started (meaning that the commit root
7303821f348SFilipe Manana 	 * will be the same as the current root) or not.
7313821f348SFilipe Manana 	 */
7323821f348SFilipe Manana 	if (readonly && pending_snapshot->snap->node !=
7333821f348SFilipe Manana 	    pending_snapshot->snap->commit_root) {
7343821f348SFilipe Manana 		trans = btrfs_join_transaction(pending_snapshot->snap);
7353821f348SFilipe Manana 		if (IS_ERR(trans) && PTR_ERR(trans) != -ENOENT) {
7363821f348SFilipe Manana 			ret = PTR_ERR(trans);
7373821f348SFilipe Manana 			goto fail;
7383821f348SFilipe Manana 		}
7393821f348SFilipe Manana 		if (!IS_ERR(trans)) {
7403821f348SFilipe Manana 			ret = btrfs_commit_transaction(trans,
7413821f348SFilipe Manana 						       pending_snapshot->snap);
7423821f348SFilipe Manana 			if (ret)
7433821f348SFilipe Manana 				goto fail;
7443821f348SFilipe Manana 		}
7453821f348SFilipe Manana 	}
7463821f348SFilipe Manana 
7472fbe8c8aSAl Viro 	inode = btrfs_lookup_dentry(dentry->d_parent->d_inode, dentry);
7482e4bfab9SYan, Zheng 	if (IS_ERR(inode)) {
7492e4bfab9SYan, Zheng 		ret = PTR_ERR(inode);
7502e4bfab9SYan, Zheng 		goto fail;
7512e4bfab9SYan, Zheng 	}
7525662344bSTsutomu Itoh 
7532e4bfab9SYan, Zheng 	d_instantiate(dentry, inode);
7542e4bfab9SYan, Zheng 	ret = 0;
7552e4bfab9SYan, Zheng fail:
756d5c12070SMiao Xie 	btrfs_subvolume_release_metadata(BTRFS_I(dir)->root,
757d5c12070SMiao Xie 					 &pending_snapshot->block_rsv,
758d5c12070SMiao Xie 					 pending_snapshot->qgroup_reserved);
7598257b2dcSMiao Xie free:
760a22285a6SYan, Zheng 	kfree(pending_snapshot);
7618257b2dcSMiao Xie out:
7628257b2dcSMiao Xie 	atomic_dec(&root->will_be_snapshoted);
763f46b5a66SChristoph Hellwig 	return ret;
764f46b5a66SChristoph Hellwig }
765f46b5a66SChristoph Hellwig 
7664260f7c7SSage Weil /*  copy of check_sticky in fs/namei.c()
7674260f7c7SSage Weil * It's inline, so penalty for filesystems that don't use sticky bit is
7684260f7c7SSage Weil * minimal.
7694260f7c7SSage Weil */
7704260f7c7SSage Weil static inline int btrfs_check_sticky(struct inode *dir, struct inode *inode)
7714260f7c7SSage Weil {
7722f2f43d3SEric W. Biederman 	kuid_t fsuid = current_fsuid();
7734260f7c7SSage Weil 
7744260f7c7SSage Weil 	if (!(dir->i_mode & S_ISVTX))
7754260f7c7SSage Weil 		return 0;
7762f2f43d3SEric W. Biederman 	if (uid_eq(inode->i_uid, fsuid))
7774260f7c7SSage Weil 		return 0;
7782f2f43d3SEric W. Biederman 	if (uid_eq(dir->i_uid, fsuid))
7794260f7c7SSage Weil 		return 0;
7804260f7c7SSage Weil 	return !capable(CAP_FOWNER);
7814260f7c7SSage Weil }
7824260f7c7SSage Weil 
7834260f7c7SSage Weil /*  copy of may_delete in fs/namei.c()
7844260f7c7SSage Weil  *	Check whether we can remove a link victim from directory dir, check
7854260f7c7SSage Weil  *  whether the type of victim is right.
7864260f7c7SSage Weil  *  1. We can't do it if dir is read-only (done in permission())
7874260f7c7SSage Weil  *  2. We should have write and exec permissions on dir
7884260f7c7SSage Weil  *  3. We can't remove anything from append-only dir
7894260f7c7SSage Weil  *  4. We can't do anything with immutable dir (done in permission())
7904260f7c7SSage Weil  *  5. If the sticky bit on dir is set we should either
7914260f7c7SSage Weil  *	a. be owner of dir, or
7924260f7c7SSage Weil  *	b. be owner of victim, or
7934260f7c7SSage Weil  *	c. have CAP_FOWNER capability
7944260f7c7SSage Weil  *  6. If the victim is append-only or immutable we can't do antyhing with
7954260f7c7SSage Weil  *     links pointing to it.
7964260f7c7SSage Weil  *  7. If we were asked to remove a directory and victim isn't one - ENOTDIR.
7974260f7c7SSage Weil  *  8. If we were asked to remove a non-directory and victim isn't one - EISDIR.
7984260f7c7SSage Weil  *  9. We can't remove a root or mountpoint.
7994260f7c7SSage Weil  * 10. We don't allow removal of NFS sillyrenamed files; it's handled by
8004260f7c7SSage Weil  *     nfs_async_unlink().
8014260f7c7SSage Weil  */
8024260f7c7SSage Weil 
8034260f7c7SSage Weil static int btrfs_may_delete(struct inode *dir, struct dentry *victim, int isdir)
8044260f7c7SSage Weil {
8054260f7c7SSage Weil 	int error;
8064260f7c7SSage Weil 
8074260f7c7SSage Weil 	if (!victim->d_inode)
8084260f7c7SSage Weil 		return -ENOENT;
8094260f7c7SSage Weil 
8104260f7c7SSage Weil 	BUG_ON(victim->d_parent->d_inode != dir);
8114fa6b5ecSJeff Layton 	audit_inode_child(dir, victim, AUDIT_TYPE_CHILD_DELETE);
8124260f7c7SSage Weil 
8134260f7c7SSage Weil 	error = inode_permission(dir, MAY_WRITE | MAY_EXEC);
8144260f7c7SSage Weil 	if (error)
8154260f7c7SSage Weil 		return error;
8164260f7c7SSage Weil 	if (IS_APPEND(dir))
8174260f7c7SSage Weil 		return -EPERM;
8184260f7c7SSage Weil 	if (btrfs_check_sticky(dir, victim->d_inode)||
8194260f7c7SSage Weil 		IS_APPEND(victim->d_inode)||
8204260f7c7SSage Weil 	    IS_IMMUTABLE(victim->d_inode) || IS_SWAPFILE(victim->d_inode))
8214260f7c7SSage Weil 		return -EPERM;
8224260f7c7SSage Weil 	if (isdir) {
8234260f7c7SSage Weil 		if (!S_ISDIR(victim->d_inode->i_mode))
8244260f7c7SSage Weil 			return -ENOTDIR;
8254260f7c7SSage Weil 		if (IS_ROOT(victim))
8264260f7c7SSage Weil 			return -EBUSY;
8274260f7c7SSage Weil 	} else if (S_ISDIR(victim->d_inode->i_mode))
8284260f7c7SSage Weil 		return -EISDIR;
8294260f7c7SSage Weil 	if (IS_DEADDIR(dir))
8304260f7c7SSage Weil 		return -ENOENT;
8314260f7c7SSage Weil 	if (victim->d_flags & DCACHE_NFSFS_RENAMED)
8324260f7c7SSage Weil 		return -EBUSY;
8334260f7c7SSage Weil 	return 0;
8344260f7c7SSage Weil }
8354260f7c7SSage Weil 
836cb8e7090SChristoph Hellwig /* copy of may_create in fs/namei.c() */
837cb8e7090SChristoph Hellwig static inline int btrfs_may_create(struct inode *dir, struct dentry *child)
838cb8e7090SChristoph Hellwig {
839cb8e7090SChristoph Hellwig 	if (child->d_inode)
840cb8e7090SChristoph Hellwig 		return -EEXIST;
841cb8e7090SChristoph Hellwig 	if (IS_DEADDIR(dir))
842cb8e7090SChristoph Hellwig 		return -ENOENT;
843cb8e7090SChristoph Hellwig 	return inode_permission(dir, MAY_WRITE | MAY_EXEC);
844cb8e7090SChristoph Hellwig }
845cb8e7090SChristoph Hellwig 
846cb8e7090SChristoph Hellwig /*
847cb8e7090SChristoph Hellwig  * Create a new subvolume below @parent.  This is largely modeled after
848cb8e7090SChristoph Hellwig  * sys_mkdirat and vfs_mkdir, but we only do a single component lookup
849cb8e7090SChristoph Hellwig  * inside this filesystem so it's quite a bit simpler.
850cb8e7090SChristoph Hellwig  */
85176dda93cSYan, Zheng static noinline int btrfs_mksubvol(struct path *parent,
85276dda93cSYan, Zheng 				   char *name, int namelen,
85372fd032eSSage Weil 				   struct btrfs_root *snap_src,
8546f72c7e2SArne Jansen 				   u64 *async_transid, bool readonly,
8558696c533SMiao Xie 				   struct btrfs_qgroup_inherit *inherit)
856cb8e7090SChristoph Hellwig {
85776dda93cSYan, Zheng 	struct inode *dir  = parent->dentry->d_inode;
858cb8e7090SChristoph Hellwig 	struct dentry *dentry;
859cb8e7090SChristoph Hellwig 	int error;
860cb8e7090SChristoph Hellwig 
8615c50c9b8SDavid Sterba 	error = mutex_lock_killable_nested(&dir->i_mutex, I_MUTEX_PARENT);
8625c50c9b8SDavid Sterba 	if (error == -EINTR)
8635c50c9b8SDavid Sterba 		return error;
864cb8e7090SChristoph Hellwig 
865cb8e7090SChristoph Hellwig 	dentry = lookup_one_len(name, parent->dentry, namelen);
866cb8e7090SChristoph Hellwig 	error = PTR_ERR(dentry);
867cb8e7090SChristoph Hellwig 	if (IS_ERR(dentry))
868cb8e7090SChristoph Hellwig 		goto out_unlock;
869cb8e7090SChristoph Hellwig 
870cb8e7090SChristoph Hellwig 	error = -EEXIST;
871cb8e7090SChristoph Hellwig 	if (dentry->d_inode)
872cb8e7090SChristoph Hellwig 		goto out_dput;
873cb8e7090SChristoph Hellwig 
87476dda93cSYan, Zheng 	error = btrfs_may_create(dir, dentry);
875cb8e7090SChristoph Hellwig 	if (error)
876a874a63eSLiu Bo 		goto out_dput;
877cb8e7090SChristoph Hellwig 
8789c52057cSChris Mason 	/*
8799c52057cSChris Mason 	 * even if this name doesn't exist, we may get hash collisions.
8809c52057cSChris Mason 	 * check for them now when we can safely fail
8819c52057cSChris Mason 	 */
8829c52057cSChris Mason 	error = btrfs_check_dir_item_collision(BTRFS_I(dir)->root,
8839c52057cSChris Mason 					       dir->i_ino, name,
8849c52057cSChris Mason 					       namelen);
8859c52057cSChris Mason 	if (error)
8869c52057cSChris Mason 		goto out_dput;
8879c52057cSChris Mason 
88876dda93cSYan, Zheng 	down_read(&BTRFS_I(dir)->root->fs_info->subvol_sem);
88976dda93cSYan, Zheng 
89076dda93cSYan, Zheng 	if (btrfs_root_refs(&BTRFS_I(dir)->root->root_item) == 0)
89176dda93cSYan, Zheng 		goto out_up_read;
89276dda93cSYan, Zheng 
8933de4586cSChris Mason 	if (snap_src) {
894e9662f70SMiao Xie 		error = create_snapshot(snap_src, dir, dentry, name, namelen,
8956f72c7e2SArne Jansen 					async_transid, readonly, inherit);
8963de4586cSChris Mason 	} else {
897d5c12070SMiao Xie 		error = create_subvol(dir, dentry, name, namelen,
898d5c12070SMiao Xie 				      async_transid, inherit);
8993de4586cSChris Mason 	}
90076dda93cSYan, Zheng 	if (!error)
90176dda93cSYan, Zheng 		fsnotify_mkdir(dir, dentry);
90276dda93cSYan, Zheng out_up_read:
90376dda93cSYan, Zheng 	up_read(&BTRFS_I(dir)->root->fs_info->subvol_sem);
904cb8e7090SChristoph Hellwig out_dput:
905cb8e7090SChristoph Hellwig 	dput(dentry);
906cb8e7090SChristoph Hellwig out_unlock:
90776dda93cSYan, Zheng 	mutex_unlock(&dir->i_mutex);
908cb8e7090SChristoph Hellwig 	return error;
909cb8e7090SChristoph Hellwig }
910cb8e7090SChristoph Hellwig 
9114cb5300bSChris Mason /*
9124cb5300bSChris Mason  * When we're defragging a range, we don't want to kick it off again
9134cb5300bSChris Mason  * if it is really just waiting for delalloc to send it down.
9144cb5300bSChris Mason  * If we find a nice big extent or delalloc range for the bytes in the
9154cb5300bSChris Mason  * file you want to defrag, we return 0 to let you know to skip this
9164cb5300bSChris Mason  * part of the file
9174cb5300bSChris Mason  */
9184cb5300bSChris Mason static int check_defrag_in_cache(struct inode *inode, u64 offset, int thresh)
9194cb5300bSChris Mason {
9204cb5300bSChris Mason 	struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
9214cb5300bSChris Mason 	struct extent_map *em = NULL;
9224cb5300bSChris Mason 	struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
9234cb5300bSChris Mason 	u64 end;
9244cb5300bSChris Mason 
9254cb5300bSChris Mason 	read_lock(&em_tree->lock);
9264cb5300bSChris Mason 	em = lookup_extent_mapping(em_tree, offset, PAGE_CACHE_SIZE);
9274cb5300bSChris Mason 	read_unlock(&em_tree->lock);
9284cb5300bSChris Mason 
9294cb5300bSChris Mason 	if (em) {
9304cb5300bSChris Mason 		end = extent_map_end(em);
9314cb5300bSChris Mason 		free_extent_map(em);
9324cb5300bSChris Mason 		if (end - offset > thresh)
9334cb5300bSChris Mason 			return 0;
9344cb5300bSChris Mason 	}
9354cb5300bSChris Mason 	/* if we already have a nice delalloc here, just stop */
9364cb5300bSChris Mason 	thresh /= 2;
9374cb5300bSChris Mason 	end = count_range_bits(io_tree, &offset, offset + thresh,
9384cb5300bSChris Mason 			       thresh, EXTENT_DELALLOC, 1);
9394cb5300bSChris Mason 	if (end >= thresh)
9404cb5300bSChris Mason 		return 0;
9414cb5300bSChris Mason 	return 1;
9424cb5300bSChris Mason }
9434cb5300bSChris Mason 
9444cb5300bSChris Mason /*
9454cb5300bSChris Mason  * helper function to walk through a file and find extents
9464cb5300bSChris Mason  * newer than a specific transid, and smaller than thresh.
9474cb5300bSChris Mason  *
9484cb5300bSChris Mason  * This is used by the defragging code to find new and small
9494cb5300bSChris Mason  * extents
9504cb5300bSChris Mason  */
9514cb5300bSChris Mason static int find_new_extents(struct btrfs_root *root,
9524cb5300bSChris Mason 			    struct inode *inode, u64 newer_than,
9534cb5300bSChris Mason 			    u64 *off, int thresh)
9544cb5300bSChris Mason {
9554cb5300bSChris Mason 	struct btrfs_path *path;
9564cb5300bSChris Mason 	struct btrfs_key min_key;
9574cb5300bSChris Mason 	struct extent_buffer *leaf;
9584cb5300bSChris Mason 	struct btrfs_file_extent_item *extent;
9594cb5300bSChris Mason 	int type;
9604cb5300bSChris Mason 	int ret;
961a4689d2bSDavid Sterba 	u64 ino = btrfs_ino(inode);
9624cb5300bSChris Mason 
9634cb5300bSChris Mason 	path = btrfs_alloc_path();
9644cb5300bSChris Mason 	if (!path)
9654cb5300bSChris Mason 		return -ENOMEM;
9664cb5300bSChris Mason 
967a4689d2bSDavid Sterba 	min_key.objectid = ino;
9684cb5300bSChris Mason 	min_key.type = BTRFS_EXTENT_DATA_KEY;
9694cb5300bSChris Mason 	min_key.offset = *off;
9704cb5300bSChris Mason 
9714cb5300bSChris Mason 	while (1) {
972f094c9bdSFilipe Manana 		path->keep_locks = 1;
9736174d3cbSFilipe David Borba Manana 		ret = btrfs_search_forward(root, &min_key, path, newer_than);
9744cb5300bSChris Mason 		if (ret != 0)
9754cb5300bSChris Mason 			goto none;
976f094c9bdSFilipe Manana 		path->keep_locks = 0;
977f094c9bdSFilipe Manana 		btrfs_unlock_up_safe(path, 1);
978f094c9bdSFilipe Manana process_slot:
979a4689d2bSDavid Sterba 		if (min_key.objectid != ino)
9804cb5300bSChris Mason 			goto none;
9814cb5300bSChris Mason 		if (min_key.type != BTRFS_EXTENT_DATA_KEY)
9824cb5300bSChris Mason 			goto none;
9834cb5300bSChris Mason 
9844cb5300bSChris Mason 		leaf = path->nodes[0];
9854cb5300bSChris Mason 		extent = btrfs_item_ptr(leaf, path->slots[0],
9864cb5300bSChris Mason 					struct btrfs_file_extent_item);
9874cb5300bSChris Mason 
9884cb5300bSChris Mason 		type = btrfs_file_extent_type(leaf, extent);
9894cb5300bSChris Mason 		if (type == BTRFS_FILE_EXTENT_REG &&
9904cb5300bSChris Mason 		    btrfs_file_extent_num_bytes(leaf, extent) < thresh &&
9914cb5300bSChris Mason 		    check_defrag_in_cache(inode, min_key.offset, thresh)) {
9924cb5300bSChris Mason 			*off = min_key.offset;
9934cb5300bSChris Mason 			btrfs_free_path(path);
9944cb5300bSChris Mason 			return 0;
9954cb5300bSChris Mason 		}
9964cb5300bSChris Mason 
997f094c9bdSFilipe Manana 		path->slots[0]++;
998f094c9bdSFilipe Manana 		if (path->slots[0] < btrfs_header_nritems(leaf)) {
999f094c9bdSFilipe Manana 			btrfs_item_key_to_cpu(leaf, &min_key, path->slots[0]);
1000f094c9bdSFilipe Manana 			goto process_slot;
1001f094c9bdSFilipe Manana 		}
1002f094c9bdSFilipe Manana 
10034cb5300bSChris Mason 		if (min_key.offset == (u64)-1)
10044cb5300bSChris Mason 			goto none;
10054cb5300bSChris Mason 
10064cb5300bSChris Mason 		min_key.offset++;
10074cb5300bSChris Mason 		btrfs_release_path(path);
10084cb5300bSChris Mason 	}
10094cb5300bSChris Mason none:
10104cb5300bSChris Mason 	btrfs_free_path(path);
10114cb5300bSChris Mason 	return -ENOENT;
10124cb5300bSChris Mason }
10134cb5300bSChris Mason 
10146c282eb4SLi Zefan static struct extent_map *defrag_lookup_extent(struct inode *inode, u64 start)
101517ce6ef8SLiu Bo {
101617ce6ef8SLiu Bo 	struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
1017940100a4SChris Mason 	struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
10186c282eb4SLi Zefan 	struct extent_map *em;
10196c282eb4SLi Zefan 	u64 len = PAGE_CACHE_SIZE;
1020940100a4SChris Mason 
1021940100a4SChris Mason 	/*
1022940100a4SChris Mason 	 * hopefully we have this extent in the tree already, try without
1023940100a4SChris Mason 	 * the full extent lock
1024940100a4SChris Mason 	 */
1025940100a4SChris Mason 	read_lock(&em_tree->lock);
1026940100a4SChris Mason 	em = lookup_extent_mapping(em_tree, start, len);
1027940100a4SChris Mason 	read_unlock(&em_tree->lock);
1028940100a4SChris Mason 
1029940100a4SChris Mason 	if (!em) {
1030308d9800SFilipe Manana 		struct extent_state *cached = NULL;
1031308d9800SFilipe Manana 		u64 end = start + len - 1;
1032308d9800SFilipe Manana 
1033940100a4SChris Mason 		/* get the big lock and read metadata off disk */
1034308d9800SFilipe Manana 		lock_extent_bits(io_tree, start, end, 0, &cached);
1035940100a4SChris Mason 		em = btrfs_get_extent(inode, NULL, 0, start, len, 0);
1036308d9800SFilipe Manana 		unlock_extent_cached(io_tree, start, end, &cached, GFP_NOFS);
1037940100a4SChris Mason 
10386cf8bfbfSDan Carpenter 		if (IS_ERR(em))
10396c282eb4SLi Zefan 			return NULL;
1040940100a4SChris Mason 	}
1041940100a4SChris Mason 
10426c282eb4SLi Zefan 	return em;
10436c282eb4SLi Zefan }
10446c282eb4SLi Zefan 
10456c282eb4SLi Zefan static bool defrag_check_next_extent(struct inode *inode, struct extent_map *em)
10466c282eb4SLi Zefan {
10476c282eb4SLi Zefan 	struct extent_map *next;
10486c282eb4SLi Zefan 	bool ret = true;
10496c282eb4SLi Zefan 
10506c282eb4SLi Zefan 	/* this is the last extent */
10516c282eb4SLi Zefan 	if (em->start + em->len >= i_size_read(inode))
10526c282eb4SLi Zefan 		return false;
10536c282eb4SLi Zefan 
10546c282eb4SLi Zefan 	next = defrag_lookup_extent(inode, em->start + em->len);
1055e2127cf0SFilipe Manana 	if (!next || next->block_start >= EXTENT_MAP_LAST_BYTE ||
1056e2127cf0SFilipe Manana 	    (em->block_start + em->block_len == next->block_start))
10576c282eb4SLi Zefan 		ret = false;
10586c282eb4SLi Zefan 
10596c282eb4SLi Zefan 	free_extent_map(next);
10606c282eb4SLi Zefan 	return ret;
10616c282eb4SLi Zefan }
10626c282eb4SLi Zefan 
10636c282eb4SLi Zefan static int should_defrag_range(struct inode *inode, u64 start, int thresh,
1064a43a2111SAndrew Mahone 			       u64 *last_len, u64 *skip, u64 *defrag_end,
1065a43a2111SAndrew Mahone 			       int compress)
10666c282eb4SLi Zefan {
10676c282eb4SLi Zefan 	struct extent_map *em;
10686c282eb4SLi Zefan 	int ret = 1;
10696c282eb4SLi Zefan 	bool next_mergeable = true;
10706c282eb4SLi Zefan 
10716c282eb4SLi Zefan 	/*
10726c282eb4SLi Zefan 	 * make sure that once we start defragging an extent, we keep on
10736c282eb4SLi Zefan 	 * defragging it
10746c282eb4SLi Zefan 	 */
10756c282eb4SLi Zefan 	if (start < *defrag_end)
10766c282eb4SLi Zefan 		return 1;
10776c282eb4SLi Zefan 
10786c282eb4SLi Zefan 	*skip = 0;
10796c282eb4SLi Zefan 
10806c282eb4SLi Zefan 	em = defrag_lookup_extent(inode, start);
10816c282eb4SLi Zefan 	if (!em)
10826c282eb4SLi Zefan 		return 0;
10836c282eb4SLi Zefan 
1084940100a4SChris Mason 	/* this will cover holes, and inline extents */
108517ce6ef8SLiu Bo 	if (em->block_start >= EXTENT_MAP_LAST_BYTE) {
1086940100a4SChris Mason 		ret = 0;
108717ce6ef8SLiu Bo 		goto out;
108817ce6ef8SLiu Bo 	}
108917ce6ef8SLiu Bo 
10906c282eb4SLi Zefan 	next_mergeable = defrag_check_next_extent(inode, em);
1091940100a4SChris Mason 
1092940100a4SChris Mason 	/*
10936c282eb4SLi Zefan 	 * we hit a real extent, if it is big or the next extent is not a
10946c282eb4SLi Zefan 	 * real extent, don't bother defragging it
1095940100a4SChris Mason 	 */
1096a43a2111SAndrew Mahone 	if (!compress && (*last_len == 0 || *last_len >= thresh) &&
10976c282eb4SLi Zefan 	    (em->len >= thresh || !next_mergeable))
1098940100a4SChris Mason 		ret = 0;
109917ce6ef8SLiu Bo out:
1100940100a4SChris Mason 	/*
1101940100a4SChris Mason 	 * last_len ends up being a counter of how many bytes we've defragged.
1102940100a4SChris Mason 	 * every time we choose not to defrag an extent, we reset *last_len
1103940100a4SChris Mason 	 * so that the next tiny extent will force a defrag.
1104940100a4SChris Mason 	 *
1105940100a4SChris Mason 	 * The end result of this is that tiny extents before a single big
1106940100a4SChris Mason 	 * extent will force at least part of that big extent to be defragged.
1107940100a4SChris Mason 	 */
1108940100a4SChris Mason 	if (ret) {
1109940100a4SChris Mason 		*defrag_end = extent_map_end(em);
1110940100a4SChris Mason 	} else {
1111940100a4SChris Mason 		*last_len = 0;
1112940100a4SChris Mason 		*skip = extent_map_end(em);
1113940100a4SChris Mason 		*defrag_end = 0;
1114940100a4SChris Mason 	}
1115940100a4SChris Mason 
1116940100a4SChris Mason 	free_extent_map(em);
1117940100a4SChris Mason 	return ret;
1118940100a4SChris Mason }
1119940100a4SChris Mason 
11204cb5300bSChris Mason /*
11214cb5300bSChris Mason  * it doesn't do much good to defrag one or two pages
11224cb5300bSChris Mason  * at a time.  This pulls in a nice chunk of pages
11234cb5300bSChris Mason  * to COW and defrag.
11244cb5300bSChris Mason  *
11254cb5300bSChris Mason  * It also makes sure the delalloc code has enough
11264cb5300bSChris Mason  * dirty data to avoid making new small extents as part
11274cb5300bSChris Mason  * of the defrag
11284cb5300bSChris Mason  *
11294cb5300bSChris Mason  * It's a good idea to start RA on this range
11304cb5300bSChris Mason  * before calling this.
11314cb5300bSChris Mason  */
11324cb5300bSChris Mason static int cluster_pages_for_defrag(struct inode *inode,
11334cb5300bSChris Mason 				    struct page **pages,
11344cb5300bSChris Mason 				    unsigned long start_index,
1135c41570c9SJustin Maggard 				    unsigned long num_pages)
1136f46b5a66SChristoph Hellwig {
11374cb5300bSChris Mason 	unsigned long file_end;
11384cb5300bSChris Mason 	u64 isize = i_size_read(inode);
1139f46b5a66SChristoph Hellwig 	u64 page_start;
1140f46b5a66SChristoph Hellwig 	u64 page_end;
11411f12bd06SLiu Bo 	u64 page_cnt;
11424cb5300bSChris Mason 	int ret;
11434cb5300bSChris Mason 	int i;
11444cb5300bSChris Mason 	int i_done;
11454cb5300bSChris Mason 	struct btrfs_ordered_extent *ordered;
11464cb5300bSChris Mason 	struct extent_state *cached_state = NULL;
1147600a45e1SMiao Xie 	struct extent_io_tree *tree;
11483b16a4e3SJosef Bacik 	gfp_t mask = btrfs_alloc_write_mask(inode->i_mapping);
11494cb5300bSChris Mason 
11504cb5300bSChris Mason 	file_end = (isize - 1) >> PAGE_CACHE_SHIFT;
11511f12bd06SLiu Bo 	if (!isize || start_index > file_end)
11521f12bd06SLiu Bo 		return 0;
11531f12bd06SLiu Bo 
11541f12bd06SLiu Bo 	page_cnt = min_t(u64, (u64)num_pages, (u64)file_end - start_index + 1);
11554cb5300bSChris Mason 
11564cb5300bSChris Mason 	ret = btrfs_delalloc_reserve_space(inode,
11571f12bd06SLiu Bo 					   page_cnt << PAGE_CACHE_SHIFT);
11584cb5300bSChris Mason 	if (ret)
11594cb5300bSChris Mason 		return ret;
11604cb5300bSChris Mason 	i_done = 0;
1161600a45e1SMiao Xie 	tree = &BTRFS_I(inode)->io_tree;
11624cb5300bSChris Mason 
11634cb5300bSChris Mason 	/* step one, lock all the pages */
11641f12bd06SLiu Bo 	for (i = 0; i < page_cnt; i++) {
11654cb5300bSChris Mason 		struct page *page;
1166600a45e1SMiao Xie again:
1167a94733d0SJosef Bacik 		page = find_or_create_page(inode->i_mapping,
11683b16a4e3SJosef Bacik 					   start_index + i, mask);
11694cb5300bSChris Mason 		if (!page)
11704cb5300bSChris Mason 			break;
11714cb5300bSChris Mason 
1172600a45e1SMiao Xie 		page_start = page_offset(page);
1173600a45e1SMiao Xie 		page_end = page_start + PAGE_CACHE_SIZE - 1;
1174600a45e1SMiao Xie 		while (1) {
1175308d9800SFilipe Manana 			lock_extent_bits(tree, page_start, page_end,
1176308d9800SFilipe Manana 					 0, &cached_state);
1177600a45e1SMiao Xie 			ordered = btrfs_lookup_ordered_extent(inode,
1178600a45e1SMiao Xie 							      page_start);
1179308d9800SFilipe Manana 			unlock_extent_cached(tree, page_start, page_end,
1180308d9800SFilipe Manana 					     &cached_state, GFP_NOFS);
1181600a45e1SMiao Xie 			if (!ordered)
1182600a45e1SMiao Xie 				break;
1183600a45e1SMiao Xie 
1184600a45e1SMiao Xie 			unlock_page(page);
1185600a45e1SMiao Xie 			btrfs_start_ordered_extent(inode, ordered, 1);
1186600a45e1SMiao Xie 			btrfs_put_ordered_extent(ordered);
1187600a45e1SMiao Xie 			lock_page(page);
11881f12bd06SLiu Bo 			/*
11891f12bd06SLiu Bo 			 * we unlocked the page above, so we need check if
11901f12bd06SLiu Bo 			 * it was released or not.
11911f12bd06SLiu Bo 			 */
11921f12bd06SLiu Bo 			if (page->mapping != inode->i_mapping) {
11931f12bd06SLiu Bo 				unlock_page(page);
11941f12bd06SLiu Bo 				page_cache_release(page);
11951f12bd06SLiu Bo 				goto again;
11961f12bd06SLiu Bo 			}
1197600a45e1SMiao Xie 		}
1198600a45e1SMiao Xie 
11994cb5300bSChris Mason 		if (!PageUptodate(page)) {
12004cb5300bSChris Mason 			btrfs_readpage(NULL, page);
12014cb5300bSChris Mason 			lock_page(page);
12024cb5300bSChris Mason 			if (!PageUptodate(page)) {
12034cb5300bSChris Mason 				unlock_page(page);
12044cb5300bSChris Mason 				page_cache_release(page);
12054cb5300bSChris Mason 				ret = -EIO;
12064cb5300bSChris Mason 				break;
12074cb5300bSChris Mason 			}
12084cb5300bSChris Mason 		}
1209600a45e1SMiao Xie 
1210600a45e1SMiao Xie 		if (page->mapping != inode->i_mapping) {
1211600a45e1SMiao Xie 			unlock_page(page);
1212600a45e1SMiao Xie 			page_cache_release(page);
1213600a45e1SMiao Xie 			goto again;
1214600a45e1SMiao Xie 		}
1215600a45e1SMiao Xie 
12164cb5300bSChris Mason 		pages[i] = page;
12174cb5300bSChris Mason 		i_done++;
12184cb5300bSChris Mason 	}
12194cb5300bSChris Mason 	if (!i_done || ret)
12204cb5300bSChris Mason 		goto out;
12214cb5300bSChris Mason 
12224cb5300bSChris Mason 	if (!(inode->i_sb->s_flags & MS_ACTIVE))
12234cb5300bSChris Mason 		goto out;
12244cb5300bSChris Mason 
12254cb5300bSChris Mason 	/*
12264cb5300bSChris Mason 	 * so now we have a nice long stream of locked
12274cb5300bSChris Mason 	 * and up to date pages, lets wait on them
12284cb5300bSChris Mason 	 */
12294cb5300bSChris Mason 	for (i = 0; i < i_done; i++)
12304cb5300bSChris Mason 		wait_on_page_writeback(pages[i]);
12314cb5300bSChris Mason 
12324cb5300bSChris Mason 	page_start = page_offset(pages[0]);
12334cb5300bSChris Mason 	page_end = page_offset(pages[i_done - 1]) + PAGE_CACHE_SIZE;
12344cb5300bSChris Mason 
12354cb5300bSChris Mason 	lock_extent_bits(&BTRFS_I(inode)->io_tree,
1236d0082371SJeff Mahoney 			 page_start, page_end - 1, 0, &cached_state);
12374cb5300bSChris Mason 	clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start,
12384cb5300bSChris Mason 			  page_end - 1, EXTENT_DIRTY | EXTENT_DELALLOC |
12399e8a4a8bSLiu Bo 			  EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG, 0, 0,
12409e8a4a8bSLiu Bo 			  &cached_state, GFP_NOFS);
12414cb5300bSChris Mason 
12421f12bd06SLiu Bo 	if (i_done != page_cnt) {
12439e0baf60SJosef Bacik 		spin_lock(&BTRFS_I(inode)->lock);
12449e0baf60SJosef Bacik 		BTRFS_I(inode)->outstanding_extents++;
12459e0baf60SJosef Bacik 		spin_unlock(&BTRFS_I(inode)->lock);
12464cb5300bSChris Mason 		btrfs_delalloc_release_space(inode,
12471f12bd06SLiu Bo 				     (page_cnt - i_done) << PAGE_CACHE_SHIFT);
12484cb5300bSChris Mason 	}
12494cb5300bSChris Mason 
12504cb5300bSChris Mason 
12519e8a4a8bSLiu Bo 	set_extent_defrag(&BTRFS_I(inode)->io_tree, page_start, page_end - 1,
12529e8a4a8bSLiu Bo 			  &cached_state, GFP_NOFS);
12534cb5300bSChris Mason 
12544cb5300bSChris Mason 	unlock_extent_cached(&BTRFS_I(inode)->io_tree,
12554cb5300bSChris Mason 			     page_start, page_end - 1, &cached_state,
12564cb5300bSChris Mason 			     GFP_NOFS);
12574cb5300bSChris Mason 
12584cb5300bSChris Mason 	for (i = 0; i < i_done; i++) {
12594cb5300bSChris Mason 		clear_page_dirty_for_io(pages[i]);
12604cb5300bSChris Mason 		ClearPageChecked(pages[i]);
12614cb5300bSChris Mason 		set_page_extent_mapped(pages[i]);
12624cb5300bSChris Mason 		set_page_dirty(pages[i]);
12634cb5300bSChris Mason 		unlock_page(pages[i]);
12644cb5300bSChris Mason 		page_cache_release(pages[i]);
12654cb5300bSChris Mason 	}
12664cb5300bSChris Mason 	return i_done;
12674cb5300bSChris Mason out:
12684cb5300bSChris Mason 	for (i = 0; i < i_done; i++) {
12694cb5300bSChris Mason 		unlock_page(pages[i]);
12704cb5300bSChris Mason 		page_cache_release(pages[i]);
12714cb5300bSChris Mason 	}
12721f12bd06SLiu Bo 	btrfs_delalloc_release_space(inode, page_cnt << PAGE_CACHE_SHIFT);
12734cb5300bSChris Mason 	return ret;
12744cb5300bSChris Mason 
12754cb5300bSChris Mason }
12764cb5300bSChris Mason 
12774cb5300bSChris Mason int btrfs_defrag_file(struct inode *inode, struct file *file,
12784cb5300bSChris Mason 		      struct btrfs_ioctl_defrag_range_args *range,
12794cb5300bSChris Mason 		      u64 newer_than, unsigned long max_to_defrag)
12804cb5300bSChris Mason {
12814cb5300bSChris Mason 	struct btrfs_root *root = BTRFS_I(inode)->root;
12824cb5300bSChris Mason 	struct file_ra_state *ra = NULL;
12834cb5300bSChris Mason 	unsigned long last_index;
1284151a31b2SLi Zefan 	u64 isize = i_size_read(inode);
1285940100a4SChris Mason 	u64 last_len = 0;
1286940100a4SChris Mason 	u64 skip = 0;
1287940100a4SChris Mason 	u64 defrag_end = 0;
12884cb5300bSChris Mason 	u64 newer_off = range->start;
1289f46b5a66SChristoph Hellwig 	unsigned long i;
1290008873eaSLi Zefan 	unsigned long ra_index = 0;
1291f46b5a66SChristoph Hellwig 	int ret;
12924cb5300bSChris Mason 	int defrag_count = 0;
12931a419d85SLi Zefan 	int compress_type = BTRFS_COMPRESS_ZLIB;
12944cb5300bSChris Mason 	int extent_thresh = range->extent_thresh;
1295c41570c9SJustin Maggard 	unsigned long max_cluster = (256 * 1024) >> PAGE_CACHE_SHIFT;
1296c41570c9SJustin Maggard 	unsigned long cluster = max_cluster;
12974cb5300bSChris Mason 	u64 new_align = ~((u64)128 * 1024 - 1);
12984cb5300bSChris Mason 	struct page **pages = NULL;
12994cb5300bSChris Mason 
13000abd5b17SLiu Bo 	if (isize == 0)
13010abd5b17SLiu Bo 		return 0;
13020abd5b17SLiu Bo 
13030abd5b17SLiu Bo 	if (range->start >= isize)
13040abd5b17SLiu Bo 		return -EINVAL;
13051a419d85SLi Zefan 
13061a419d85SLi Zefan 	if (range->flags & BTRFS_DEFRAG_RANGE_COMPRESS) {
13071a419d85SLi Zefan 		if (range->compress_type > BTRFS_COMPRESS_TYPES)
13081a419d85SLi Zefan 			return -EINVAL;
13091a419d85SLi Zefan 		if (range->compress_type)
13101a419d85SLi Zefan 			compress_type = range->compress_type;
13111a419d85SLi Zefan 	}
1312f46b5a66SChristoph Hellwig 
13130abd5b17SLiu Bo 	if (extent_thresh == 0)
13140abd5b17SLiu Bo 		extent_thresh = 256 * 1024;
1315f46b5a66SChristoph Hellwig 
13164cb5300bSChris Mason 	/*
13174cb5300bSChris Mason 	 * if we were not given a file, allocate a readahead
13184cb5300bSChris Mason 	 * context
13194cb5300bSChris Mason 	 */
13204cb5300bSChris Mason 	if (!file) {
13214cb5300bSChris Mason 		ra = kzalloc(sizeof(*ra), GFP_NOFS);
13224cb5300bSChris Mason 		if (!ra)
13234cb5300bSChris Mason 			return -ENOMEM;
13244cb5300bSChris Mason 		file_ra_state_init(ra, inode->i_mapping);
13254cb5300bSChris Mason 	} else {
13264cb5300bSChris Mason 		ra = &file->f_ra;
13274cb5300bSChris Mason 	}
13284cb5300bSChris Mason 
1329d9b0d9baSDulshani Gunawardhana 	pages = kmalloc_array(max_cluster, sizeof(struct page *),
13304cb5300bSChris Mason 			GFP_NOFS);
13314cb5300bSChris Mason 	if (!pages) {
13324cb5300bSChris Mason 		ret = -ENOMEM;
13334cb5300bSChris Mason 		goto out_ra;
13344cb5300bSChris Mason 	}
13354cb5300bSChris Mason 
13364cb5300bSChris Mason 	/* find the last page to defrag */
13371e701a32SChris Mason 	if (range->start + range->len > range->start) {
1338151a31b2SLi Zefan 		last_index = min_t(u64, isize - 1,
13391e701a32SChris Mason 			 range->start + range->len - 1) >> PAGE_CACHE_SHIFT;
13401e701a32SChris Mason 	} else {
1341151a31b2SLi Zefan 		last_index = (isize - 1) >> PAGE_CACHE_SHIFT;
13421e701a32SChris Mason 	}
13431e701a32SChris Mason 
13444cb5300bSChris Mason 	if (newer_than) {
13454cb5300bSChris Mason 		ret = find_new_extents(root, inode, newer_than,
13464cb5300bSChris Mason 				       &newer_off, 64 * 1024);
13474cb5300bSChris Mason 		if (!ret) {
13484cb5300bSChris Mason 			range->start = newer_off;
13494cb5300bSChris Mason 			/*
13504cb5300bSChris Mason 			 * we always align our defrag to help keep
13514cb5300bSChris Mason 			 * the extents in the file evenly spaced
13524cb5300bSChris Mason 			 */
13534cb5300bSChris Mason 			i = (newer_off & new_align) >> PAGE_CACHE_SHIFT;
13544cb5300bSChris Mason 		} else
13554cb5300bSChris Mason 			goto out_ra;
13564cb5300bSChris Mason 	} else {
13571e701a32SChris Mason 		i = range->start >> PAGE_CACHE_SHIFT;
13584cb5300bSChris Mason 	}
13594cb5300bSChris Mason 	if (!max_to_defrag)
13607ec31b54SLiu Bo 		max_to_defrag = last_index + 1;
13614cb5300bSChris Mason 
13622a0f7f57SLi Zefan 	/*
13632a0f7f57SLi Zefan 	 * make writeback starts from i, so the defrag range can be
13642a0f7f57SLi Zefan 	 * written sequentially.
13652a0f7f57SLi Zefan 	 */
13662a0f7f57SLi Zefan 	if (i < inode->i_mapping->writeback_index)
13672a0f7f57SLi Zefan 		inode->i_mapping->writeback_index = i;
13682a0f7f57SLi Zefan 
1369f7f43cc8SChris Mason 	while (i <= last_index && defrag_count < max_to_defrag &&
1370f7f43cc8SChris Mason 	       (i < (i_size_read(inode) + PAGE_CACHE_SIZE - 1) >>
1371f7f43cc8SChris Mason 		PAGE_CACHE_SHIFT)) {
13724cb5300bSChris Mason 		/*
13734cb5300bSChris Mason 		 * make sure we stop running if someone unmounts
13744cb5300bSChris Mason 		 * the FS
13754cb5300bSChris Mason 		 */
13764cb5300bSChris Mason 		if (!(inode->i_sb->s_flags & MS_ACTIVE))
13774cb5300bSChris Mason 			break;
13784cb5300bSChris Mason 
1379210549ebSDavid Sterba 		if (btrfs_defrag_cancelled(root->fs_info)) {
1380efe120a0SFrank Holton 			printk(KERN_DEBUG "BTRFS: defrag_file cancelled\n");
1381210549ebSDavid Sterba 			ret = -EAGAIN;
1382210549ebSDavid Sterba 			break;
1383210549ebSDavid Sterba 		}
1384210549ebSDavid Sterba 
138566c26892SLiu Bo 		if (!should_defrag_range(inode, (u64)i << PAGE_CACHE_SHIFT,
13866c282eb4SLi Zefan 					 extent_thresh, &last_len, &skip,
1387a43a2111SAndrew Mahone 					 &defrag_end, range->flags &
1388a43a2111SAndrew Mahone 					 BTRFS_DEFRAG_RANGE_COMPRESS)) {
1389940100a4SChris Mason 			unsigned long next;
1390940100a4SChris Mason 			/*
1391940100a4SChris Mason 			 * the should_defrag function tells us how much to skip
1392940100a4SChris Mason 			 * bump our counter by the suggested amount
1393940100a4SChris Mason 			 */
1394940100a4SChris Mason 			next = (skip + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
1395940100a4SChris Mason 			i = max(i + 1, next);
1396940100a4SChris Mason 			continue;
1397940100a4SChris Mason 		}
1398008873eaSLi Zefan 
1399008873eaSLi Zefan 		if (!newer_than) {
1400008873eaSLi Zefan 			cluster = (PAGE_CACHE_ALIGN(defrag_end) >>
1401008873eaSLi Zefan 				   PAGE_CACHE_SHIFT) - i;
1402008873eaSLi Zefan 			cluster = min(cluster, max_cluster);
1403008873eaSLi Zefan 		} else {
1404008873eaSLi Zefan 			cluster = max_cluster;
1405008873eaSLi Zefan 		}
1406008873eaSLi Zefan 
1407008873eaSLi Zefan 		if (i + cluster > ra_index) {
1408008873eaSLi Zefan 			ra_index = max(i, ra_index);
1409008873eaSLi Zefan 			btrfs_force_ra(inode->i_mapping, ra, file, ra_index,
1410008873eaSLi Zefan 				       cluster);
1411008873eaSLi Zefan 			ra_index += max_cluster;
1412008873eaSLi Zefan 		}
14134cb5300bSChris Mason 
1414ecb8bea8SLiu Bo 		mutex_lock(&inode->i_mutex);
1415633085c7SFilipe David Borba Manana 		if (range->flags & BTRFS_DEFRAG_RANGE_COMPRESS)
1416633085c7SFilipe David Borba Manana 			BTRFS_I(inode)->force_compress = compress_type;
1417008873eaSLi Zefan 		ret = cluster_pages_for_defrag(inode, pages, i, cluster);
1418ecb8bea8SLiu Bo 		if (ret < 0) {
1419ecb8bea8SLiu Bo 			mutex_unlock(&inode->i_mutex);
14204cb5300bSChris Mason 			goto out_ra;
1421ecb8bea8SLiu Bo 		}
14224cb5300bSChris Mason 
14234cb5300bSChris Mason 		defrag_count += ret;
1424d0e1d66bSNamjae Jeon 		balance_dirty_pages_ratelimited(inode->i_mapping);
1425ecb8bea8SLiu Bo 		mutex_unlock(&inode->i_mutex);
14264cb5300bSChris Mason 
14274cb5300bSChris Mason 		if (newer_than) {
14284cb5300bSChris Mason 			if (newer_off == (u64)-1)
14294cb5300bSChris Mason 				break;
14304cb5300bSChris Mason 
1431e1f041e1SLiu Bo 			if (ret > 0)
1432e1f041e1SLiu Bo 				i += ret;
1433e1f041e1SLiu Bo 
14344cb5300bSChris Mason 			newer_off = max(newer_off + 1,
14354cb5300bSChris Mason 					(u64)i << PAGE_CACHE_SHIFT);
14364cb5300bSChris Mason 
14374cb5300bSChris Mason 			ret = find_new_extents(root, inode,
14384cb5300bSChris Mason 					       newer_than, &newer_off,
14394cb5300bSChris Mason 					       64 * 1024);
14404cb5300bSChris Mason 			if (!ret) {
14414cb5300bSChris Mason 				range->start = newer_off;
14424cb5300bSChris Mason 				i = (newer_off & new_align) >> PAGE_CACHE_SHIFT;
14434cb5300bSChris Mason 			} else {
14444cb5300bSChris Mason 				break;
1445940100a4SChris Mason 			}
14464cb5300bSChris Mason 		} else {
1447008873eaSLi Zefan 			if (ret > 0) {
1448cbcc8326SLi Zefan 				i += ret;
1449008873eaSLi Zefan 				last_len += ret << PAGE_CACHE_SHIFT;
1450008873eaSLi Zefan 			} else {
1451940100a4SChris Mason 				i++;
1452008873eaSLi Zefan 				last_len = 0;
1453008873eaSLi Zefan 			}
1454f46b5a66SChristoph Hellwig 		}
14554cb5300bSChris Mason 	}
1456f46b5a66SChristoph Hellwig 
1457dec8ef90SFilipe Manana 	if ((range->flags & BTRFS_DEFRAG_RANGE_START_IO)) {
14581e701a32SChris Mason 		filemap_flush(inode->i_mapping);
1459dec8ef90SFilipe Manana 		if (test_bit(BTRFS_INODE_HAS_ASYNC_EXTENT,
1460dec8ef90SFilipe Manana 			     &BTRFS_I(inode)->runtime_flags))
1461dec8ef90SFilipe Manana 			filemap_flush(inode->i_mapping);
1462dec8ef90SFilipe Manana 	}
14631e701a32SChris Mason 
14641e701a32SChris Mason 	if ((range->flags & BTRFS_DEFRAG_RANGE_COMPRESS)) {
14651e701a32SChris Mason 		/* the filemap_flush will queue IO into the worker threads, but
14661e701a32SChris Mason 		 * we have to make sure the IO is actually started and that
14671e701a32SChris Mason 		 * ordered extents get created before we return
14681e701a32SChris Mason 		 */
14691e701a32SChris Mason 		atomic_inc(&root->fs_info->async_submit_draining);
14701e701a32SChris Mason 		while (atomic_read(&root->fs_info->nr_async_submits) ||
14711e701a32SChris Mason 		      atomic_read(&root->fs_info->async_delalloc_pages)) {
14721e701a32SChris Mason 			wait_event(root->fs_info->async_submit_wait,
14731e701a32SChris Mason 			   (atomic_read(&root->fs_info->nr_async_submits) == 0 &&
14741e701a32SChris Mason 			    atomic_read(&root->fs_info->async_delalloc_pages) == 0));
14751e701a32SChris Mason 		}
14761e701a32SChris Mason 		atomic_dec(&root->fs_info->async_submit_draining);
14771e701a32SChris Mason 	}
14781e701a32SChris Mason 
14791a419d85SLi Zefan 	if (range->compress_type == BTRFS_COMPRESS_LZO) {
14802b0ce2c2SMitch Harder 		btrfs_set_fs_incompat(root->fs_info, COMPRESS_LZO);
14811a419d85SLi Zefan 	}
14821a419d85SLi Zefan 
148360ccf82fSDiego Calleja 	ret = defrag_count;
1484940100a4SChris Mason 
14854cb5300bSChris Mason out_ra:
1486633085c7SFilipe David Borba Manana 	if (range->flags & BTRFS_DEFRAG_RANGE_COMPRESS) {
1487633085c7SFilipe David Borba Manana 		mutex_lock(&inode->i_mutex);
1488633085c7SFilipe David Borba Manana 		BTRFS_I(inode)->force_compress = BTRFS_COMPRESS_NONE;
1489633085c7SFilipe David Borba Manana 		mutex_unlock(&inode->i_mutex);
1490633085c7SFilipe David Borba Manana 	}
14914cb5300bSChris Mason 	if (!file)
14924cb5300bSChris Mason 		kfree(ra);
14934cb5300bSChris Mason 	kfree(pages);
1494940100a4SChris Mason 	return ret;
1495f46b5a66SChristoph Hellwig }
1496f46b5a66SChristoph Hellwig 
1497198605a8SMiao Xie static noinline int btrfs_ioctl_resize(struct file *file,
149876dda93cSYan, Zheng 					void __user *arg)
1499f46b5a66SChristoph Hellwig {
1500f46b5a66SChristoph Hellwig 	u64 new_size;
1501f46b5a66SChristoph Hellwig 	u64 old_size;
1502f46b5a66SChristoph Hellwig 	u64 devid = 1;
1503496ad9aaSAl Viro 	struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
1504f46b5a66SChristoph Hellwig 	struct btrfs_ioctl_vol_args *vol_args;
1505f46b5a66SChristoph Hellwig 	struct btrfs_trans_handle *trans;
1506f46b5a66SChristoph Hellwig 	struct btrfs_device *device = NULL;
1507f46b5a66SChristoph Hellwig 	char *sizestr;
15089a40f122SGui Hecheng 	char *retptr;
1509f46b5a66SChristoph Hellwig 	char *devstr = NULL;
1510f46b5a66SChristoph Hellwig 	int ret = 0;
1511f46b5a66SChristoph Hellwig 	int mod = 0;
1512f46b5a66SChristoph Hellwig 
1513e441d54dSChris Mason 	if (!capable(CAP_SYS_ADMIN))
1514e441d54dSChris Mason 		return -EPERM;
1515e441d54dSChris Mason 
1516198605a8SMiao Xie 	ret = mnt_want_write_file(file);
1517198605a8SMiao Xie 	if (ret)
1518198605a8SMiao Xie 		return ret;
1519198605a8SMiao Xie 
15205ac00addSStefan Behrens 	if (atomic_xchg(&root->fs_info->mutually_exclusive_operation_running,
15215ac00addSStefan Behrens 			1)) {
152297547676SMiao Xie 		mnt_drop_write_file(file);
1523e57138b3SAnand Jain 		return BTRFS_ERROR_DEV_EXCL_RUN_IN_PROGRESS;
1524c9e9f97bSIlya Dryomov 	}
1525c9e9f97bSIlya Dryomov 
15265ac00addSStefan Behrens 	mutex_lock(&root->fs_info->volume_mutex);
1527dae7b665SLi Zefan 	vol_args = memdup_user(arg, sizeof(*vol_args));
1528c9e9f97bSIlya Dryomov 	if (IS_ERR(vol_args)) {
1529c9e9f97bSIlya Dryomov 		ret = PTR_ERR(vol_args);
1530c9e9f97bSIlya Dryomov 		goto out;
1531c9e9f97bSIlya Dryomov 	}
15325516e595SMark Fasheh 
15335516e595SMark Fasheh 	vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
1534f46b5a66SChristoph Hellwig 
1535f46b5a66SChristoph Hellwig 	sizestr = vol_args->name;
1536f46b5a66SChristoph Hellwig 	devstr = strchr(sizestr, ':');
1537f46b5a66SChristoph Hellwig 	if (devstr) {
1538f46b5a66SChristoph Hellwig 		sizestr = devstr + 1;
1539f46b5a66SChristoph Hellwig 		*devstr = '\0';
1540f46b5a66SChristoph Hellwig 		devstr = vol_args->name;
154158dfae63SZhangZhen 		ret = kstrtoull(devstr, 10, &devid);
154258dfae63SZhangZhen 		if (ret)
154358dfae63SZhangZhen 			goto out_free;
1544dfd79829SMiao Xie 		if (!devid) {
1545dfd79829SMiao Xie 			ret = -EINVAL;
1546dfd79829SMiao Xie 			goto out_free;
1547dfd79829SMiao Xie 		}
1548efe120a0SFrank Holton 		btrfs_info(root->fs_info, "resizing devid %llu", devid);
1549f46b5a66SChristoph Hellwig 	}
1550dba60f3fSMiao Xie 
1551aa1b8cd4SStefan Behrens 	device = btrfs_find_device(root->fs_info, devid, NULL, NULL);
1552f46b5a66SChristoph Hellwig 	if (!device) {
1553efe120a0SFrank Holton 		btrfs_info(root->fs_info, "resizer unable to find device %llu",
1554c1c9ff7cSGeert Uytterhoeven 		       devid);
1555dfd79829SMiao Xie 		ret = -ENODEV;
1556c9e9f97bSIlya Dryomov 		goto out_free;
1557f46b5a66SChristoph Hellwig 	}
1558dba60f3fSMiao Xie 
1559dba60f3fSMiao Xie 	if (!device->writeable) {
1560efe120a0SFrank Holton 		btrfs_info(root->fs_info,
1561efe120a0SFrank Holton 			   "resizer unable to apply on readonly device %llu",
1562c1c9ff7cSGeert Uytterhoeven 		       devid);
1563dfd79829SMiao Xie 		ret = -EPERM;
15644e42ae1bSLiu Bo 		goto out_free;
15654e42ae1bSLiu Bo 	}
15664e42ae1bSLiu Bo 
1567f46b5a66SChristoph Hellwig 	if (!strcmp(sizestr, "max"))
1568f46b5a66SChristoph Hellwig 		new_size = device->bdev->bd_inode->i_size;
1569f46b5a66SChristoph Hellwig 	else {
1570f46b5a66SChristoph Hellwig 		if (sizestr[0] == '-') {
1571f46b5a66SChristoph Hellwig 			mod = -1;
1572f46b5a66SChristoph Hellwig 			sizestr++;
1573f46b5a66SChristoph Hellwig 		} else if (sizestr[0] == '+') {
1574f46b5a66SChristoph Hellwig 			mod = 1;
1575f46b5a66SChristoph Hellwig 			sizestr++;
1576f46b5a66SChristoph Hellwig 		}
15779a40f122SGui Hecheng 		new_size = memparse(sizestr, &retptr);
15789a40f122SGui Hecheng 		if (*retptr != '\0' || new_size == 0) {
1579f46b5a66SChristoph Hellwig 			ret = -EINVAL;
1580c9e9f97bSIlya Dryomov 			goto out_free;
1581f46b5a66SChristoph Hellwig 		}
1582f46b5a66SChristoph Hellwig 	}
1583f46b5a66SChristoph Hellwig 
158463a212abSStefan Behrens 	if (device->is_tgtdev_for_dev_replace) {
1585dfd79829SMiao Xie 		ret = -EPERM;
158663a212abSStefan Behrens 		goto out_free;
158763a212abSStefan Behrens 	}
158863a212abSStefan Behrens 
1589f46b5a66SChristoph Hellwig 	old_size = device->total_bytes;
1590f46b5a66SChristoph Hellwig 
1591f46b5a66SChristoph Hellwig 	if (mod < 0) {
1592f46b5a66SChristoph Hellwig 		if (new_size > old_size) {
1593f46b5a66SChristoph Hellwig 			ret = -EINVAL;
1594c9e9f97bSIlya Dryomov 			goto out_free;
1595f46b5a66SChristoph Hellwig 		}
1596f46b5a66SChristoph Hellwig 		new_size = old_size - new_size;
1597f46b5a66SChristoph Hellwig 	} else if (mod > 0) {
1598eb8052e0SWenliang Fan 		if (new_size > ULLONG_MAX - old_size) {
1599902c68a4SGui Hecheng 			ret = -ERANGE;
1600eb8052e0SWenliang Fan 			goto out_free;
1601eb8052e0SWenliang Fan 		}
1602f46b5a66SChristoph Hellwig 		new_size = old_size + new_size;
1603f46b5a66SChristoph Hellwig 	}
1604f46b5a66SChristoph Hellwig 
1605f46b5a66SChristoph Hellwig 	if (new_size < 256 * 1024 * 1024) {
1606f46b5a66SChristoph Hellwig 		ret = -EINVAL;
1607c9e9f97bSIlya Dryomov 		goto out_free;
1608f46b5a66SChristoph Hellwig 	}
1609f46b5a66SChristoph Hellwig 	if (new_size > device->bdev->bd_inode->i_size) {
1610f46b5a66SChristoph Hellwig 		ret = -EFBIG;
1611c9e9f97bSIlya Dryomov 		goto out_free;
1612f46b5a66SChristoph Hellwig 	}
1613f46b5a66SChristoph Hellwig 
1614f46b5a66SChristoph Hellwig 	do_div(new_size, root->sectorsize);
1615f46b5a66SChristoph Hellwig 	new_size *= root->sectorsize;
1616f46b5a66SChristoph Hellwig 
1617efe120a0SFrank Holton 	printk_in_rcu(KERN_INFO "BTRFS: new size for %s is %llu\n",
1618c1c9ff7cSGeert Uytterhoeven 		      rcu_str_deref(device->name), new_size);
1619f46b5a66SChristoph Hellwig 
1620f46b5a66SChristoph Hellwig 	if (new_size > old_size) {
1621a22285a6SYan, Zheng 		trans = btrfs_start_transaction(root, 0);
162298d5dc13STsutomu Itoh 		if (IS_ERR(trans)) {
162398d5dc13STsutomu Itoh 			ret = PTR_ERR(trans);
1624c9e9f97bSIlya Dryomov 			goto out_free;
162598d5dc13STsutomu Itoh 		}
1626f46b5a66SChristoph Hellwig 		ret = btrfs_grow_device(trans, device, new_size);
1627f46b5a66SChristoph Hellwig 		btrfs_commit_transaction(trans, root);
1628ece7d20eSMike Fleetwood 	} else if (new_size < old_size) {
1629f46b5a66SChristoph Hellwig 		ret = btrfs_shrink_device(device, new_size);
16300253f40eSjeff.liu 	} /* equal, nothing need to do */
1631f46b5a66SChristoph Hellwig 
1632c9e9f97bSIlya Dryomov out_free:
1633f46b5a66SChristoph Hellwig 	kfree(vol_args);
1634c9e9f97bSIlya Dryomov out:
1635c9e9f97bSIlya Dryomov 	mutex_unlock(&root->fs_info->volume_mutex);
16365ac00addSStefan Behrens 	atomic_set(&root->fs_info->mutually_exclusive_operation_running, 0);
163718f39c41SIlya Dryomov 	mnt_drop_write_file(file);
1638f46b5a66SChristoph Hellwig 	return ret;
1639f46b5a66SChristoph Hellwig }
1640f46b5a66SChristoph Hellwig 
164172fd032eSSage Weil static noinline int btrfs_ioctl_snap_create_transid(struct file *file,
16426f72c7e2SArne Jansen 				char *name, unsigned long fd, int subvol,
16436f72c7e2SArne Jansen 				u64 *transid, bool readonly,
16448696c533SMiao Xie 				struct btrfs_qgroup_inherit *inherit)
1645f46b5a66SChristoph Hellwig {
1646f46b5a66SChristoph Hellwig 	int namelen;
16473de4586cSChris Mason 	int ret = 0;
1648f46b5a66SChristoph Hellwig 
1649a874a63eSLiu Bo 	ret = mnt_want_write_file(file);
1650a874a63eSLiu Bo 	if (ret)
1651a874a63eSLiu Bo 		goto out;
1652a874a63eSLiu Bo 
165372fd032eSSage Weil 	namelen = strlen(name);
165472fd032eSSage Weil 	if (strchr(name, '/')) {
1655f46b5a66SChristoph Hellwig 		ret = -EINVAL;
1656a874a63eSLiu Bo 		goto out_drop_write;
1657f46b5a66SChristoph Hellwig 	}
1658f46b5a66SChristoph Hellwig 
165916780cabSChris Mason 	if (name[0] == '.' &&
166016780cabSChris Mason 	   (namelen == 1 || (name[1] == '.' && namelen == 2))) {
166116780cabSChris Mason 		ret = -EEXIST;
1662a874a63eSLiu Bo 		goto out_drop_write;
166316780cabSChris Mason 	}
166416780cabSChris Mason 
16653de4586cSChris Mason 	if (subvol) {
166672fd032eSSage Weil 		ret = btrfs_mksubvol(&file->f_path, name, namelen,
16676f72c7e2SArne Jansen 				     NULL, transid, readonly, inherit);
1668cb8e7090SChristoph Hellwig 	} else {
16692903ff01SAl Viro 		struct fd src = fdget(fd);
16703de4586cSChris Mason 		struct inode *src_inode;
16712903ff01SAl Viro 		if (!src.file) {
16723de4586cSChris Mason 			ret = -EINVAL;
1673a874a63eSLiu Bo 			goto out_drop_write;
16743de4586cSChris Mason 		}
16753de4586cSChris Mason 
1676496ad9aaSAl Viro 		src_inode = file_inode(src.file);
1677496ad9aaSAl Viro 		if (src_inode->i_sb != file_inode(file)->i_sb) {
1678efe120a0SFrank Holton 			btrfs_info(BTRFS_I(src_inode)->root->fs_info,
1679efe120a0SFrank Holton 				   "Snapshot src from another FS");
168023ad5b17SKusanagi Kouichi 			ret = -EXDEV;
1681d0242061SDavid Sterba 		} else if (!inode_owner_or_capable(src_inode)) {
1682d0242061SDavid Sterba 			/*
1683d0242061SDavid Sterba 			 * Subvolume creation is not restricted, but snapshots
1684d0242061SDavid Sterba 			 * are limited to own subvolumes only
1685d0242061SDavid Sterba 			 */
1686d0242061SDavid Sterba 			ret = -EPERM;
1687ecd18815SAl Viro 		} else {
168872fd032eSSage Weil 			ret = btrfs_mksubvol(&file->f_path, name, namelen,
168972fd032eSSage Weil 					     BTRFS_I(src_inode)->root,
16906f72c7e2SArne Jansen 					     transid, readonly, inherit);
1691ecd18815SAl Viro 		}
16922903ff01SAl Viro 		fdput(src);
1693cb8e7090SChristoph Hellwig 	}
1694a874a63eSLiu Bo out_drop_write:
1695a874a63eSLiu Bo 	mnt_drop_write_file(file);
1696f46b5a66SChristoph Hellwig out:
169772fd032eSSage Weil 	return ret;
169872fd032eSSage Weil }
169972fd032eSSage Weil 
170072fd032eSSage Weil static noinline int btrfs_ioctl_snap_create(struct file *file,
1701fa0d2b9bSLi Zefan 					    void __user *arg, int subvol)
170272fd032eSSage Weil {
1703fa0d2b9bSLi Zefan 	struct btrfs_ioctl_vol_args *vol_args;
170472fd032eSSage Weil 	int ret;
170572fd032eSSage Weil 
1706fa0d2b9bSLi Zefan 	vol_args = memdup_user(arg, sizeof(*vol_args));
1707fa0d2b9bSLi Zefan 	if (IS_ERR(vol_args))
1708fa0d2b9bSLi Zefan 		return PTR_ERR(vol_args);
1709fa0d2b9bSLi Zefan 	vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
1710fa0d2b9bSLi Zefan 
1711fa0d2b9bSLi Zefan 	ret = btrfs_ioctl_snap_create_transid(file, vol_args->name,
1712b83cc969SLi Zefan 					      vol_args->fd, subvol,
17136f72c7e2SArne Jansen 					      NULL, false, NULL);
1714fa0d2b9bSLi Zefan 
1715fa0d2b9bSLi Zefan 	kfree(vol_args);
1716fa0d2b9bSLi Zefan 	return ret;
1717fa0d2b9bSLi Zefan }
1718fa0d2b9bSLi Zefan 
1719fa0d2b9bSLi Zefan static noinline int btrfs_ioctl_snap_create_v2(struct file *file,
1720fa0d2b9bSLi Zefan 					       void __user *arg, int subvol)
1721fa0d2b9bSLi Zefan {
1722fa0d2b9bSLi Zefan 	struct btrfs_ioctl_vol_args_v2 *vol_args;
1723fa0d2b9bSLi Zefan 	int ret;
1724fdfb1e4fSLi Zefan 	u64 transid = 0;
1725fdfb1e4fSLi Zefan 	u64 *ptr = NULL;
1726b83cc969SLi Zefan 	bool readonly = false;
17276f72c7e2SArne Jansen 	struct btrfs_qgroup_inherit *inherit = NULL;
172872fd032eSSage Weil 
1729fa0d2b9bSLi Zefan 	vol_args = memdup_user(arg, sizeof(*vol_args));
1730fa0d2b9bSLi Zefan 	if (IS_ERR(vol_args))
1731fa0d2b9bSLi Zefan 		return PTR_ERR(vol_args);
1732fa0d2b9bSLi Zefan 	vol_args->name[BTRFS_SUBVOL_NAME_MAX] = '\0';
1733fdfb1e4fSLi Zefan 
1734b83cc969SLi Zefan 	if (vol_args->flags &
17356f72c7e2SArne Jansen 	    ~(BTRFS_SUBVOL_CREATE_ASYNC | BTRFS_SUBVOL_RDONLY |
17366f72c7e2SArne Jansen 	      BTRFS_SUBVOL_QGROUP_INHERIT)) {
1737b83cc969SLi Zefan 		ret = -EOPNOTSUPP;
1738fdfb1e4fSLi Zefan 		goto out;
1739fdfb1e4fSLi Zefan 	}
1740fdfb1e4fSLi Zefan 
1741fa0d2b9bSLi Zefan 	if (vol_args->flags & BTRFS_SUBVOL_CREATE_ASYNC)
1742fdfb1e4fSLi Zefan 		ptr = &transid;
1743b83cc969SLi Zefan 	if (vol_args->flags & BTRFS_SUBVOL_RDONLY)
1744b83cc969SLi Zefan 		readonly = true;
17456f72c7e2SArne Jansen 	if (vol_args->flags & BTRFS_SUBVOL_QGROUP_INHERIT) {
17466f72c7e2SArne Jansen 		if (vol_args->size > PAGE_CACHE_SIZE) {
17476f72c7e2SArne Jansen 			ret = -EINVAL;
17486f72c7e2SArne Jansen 			goto out;
17496f72c7e2SArne Jansen 		}
17506f72c7e2SArne Jansen 		inherit = memdup_user(vol_args->qgroup_inherit, vol_args->size);
17516f72c7e2SArne Jansen 		if (IS_ERR(inherit)) {
17526f72c7e2SArne Jansen 			ret = PTR_ERR(inherit);
17536f72c7e2SArne Jansen 			goto out;
17546f72c7e2SArne Jansen 		}
17556f72c7e2SArne Jansen 	}
175675eaa0e2SSage Weil 
1757fa0d2b9bSLi Zefan 	ret = btrfs_ioctl_snap_create_transid(file, vol_args->name,
17586f72c7e2SArne Jansen 					      vol_args->fd, subvol, ptr,
17598696c533SMiao Xie 					      readonly, inherit);
176075eaa0e2SSage Weil 
1761fdfb1e4fSLi Zefan 	if (ret == 0 && ptr &&
176275eaa0e2SSage Weil 	    copy_to_user(arg +
1763fdfb1e4fSLi Zefan 			 offsetof(struct btrfs_ioctl_vol_args_v2,
1764fdfb1e4fSLi Zefan 				  transid), ptr, sizeof(*ptr)))
176575eaa0e2SSage Weil 		ret = -EFAULT;
1766fdfb1e4fSLi Zefan out:
1767f46b5a66SChristoph Hellwig 	kfree(vol_args);
17686f72c7e2SArne Jansen 	kfree(inherit);
1769f46b5a66SChristoph Hellwig 	return ret;
1770f46b5a66SChristoph Hellwig }
1771f46b5a66SChristoph Hellwig 
17720caa102dSLi Zefan static noinline int btrfs_ioctl_subvol_getflags(struct file *file,
17730caa102dSLi Zefan 						void __user *arg)
17740caa102dSLi Zefan {
1775496ad9aaSAl Viro 	struct inode *inode = file_inode(file);
17760caa102dSLi Zefan 	struct btrfs_root *root = BTRFS_I(inode)->root;
17770caa102dSLi Zefan 	int ret = 0;
17780caa102dSLi Zefan 	u64 flags = 0;
17790caa102dSLi Zefan 
178033345d01SLi Zefan 	if (btrfs_ino(inode) != BTRFS_FIRST_FREE_OBJECTID)
17810caa102dSLi Zefan 		return -EINVAL;
17820caa102dSLi Zefan 
17830caa102dSLi Zefan 	down_read(&root->fs_info->subvol_sem);
17840caa102dSLi Zefan 	if (btrfs_root_readonly(root))
17850caa102dSLi Zefan 		flags |= BTRFS_SUBVOL_RDONLY;
17860caa102dSLi Zefan 	up_read(&root->fs_info->subvol_sem);
17870caa102dSLi Zefan 
17880caa102dSLi Zefan 	if (copy_to_user(arg, &flags, sizeof(flags)))
17890caa102dSLi Zefan 		ret = -EFAULT;
17900caa102dSLi Zefan 
17910caa102dSLi Zefan 	return ret;
17920caa102dSLi Zefan }
17930caa102dSLi Zefan 
17940caa102dSLi Zefan static noinline int btrfs_ioctl_subvol_setflags(struct file *file,
17950caa102dSLi Zefan 					      void __user *arg)
17960caa102dSLi Zefan {
1797496ad9aaSAl Viro 	struct inode *inode = file_inode(file);
17980caa102dSLi Zefan 	struct btrfs_root *root = BTRFS_I(inode)->root;
17990caa102dSLi Zefan 	struct btrfs_trans_handle *trans;
18000caa102dSLi Zefan 	u64 root_flags;
18010caa102dSLi Zefan 	u64 flags;
18020caa102dSLi Zefan 	int ret = 0;
18030caa102dSLi Zefan 
1804bd60ea0fSDavid Sterba 	if (!inode_owner_or_capable(inode))
1805bd60ea0fSDavid Sterba 		return -EPERM;
1806bd60ea0fSDavid Sterba 
1807b9ca0664SLiu Bo 	ret = mnt_want_write_file(file);
1808b9ca0664SLiu Bo 	if (ret)
1809b9ca0664SLiu Bo 		goto out;
18100caa102dSLi Zefan 
1811b9ca0664SLiu Bo 	if (btrfs_ino(inode) != BTRFS_FIRST_FREE_OBJECTID) {
1812b9ca0664SLiu Bo 		ret = -EINVAL;
1813b9ca0664SLiu Bo 		goto out_drop_write;
1814b9ca0664SLiu Bo 	}
18150caa102dSLi Zefan 
1816b9ca0664SLiu Bo 	if (copy_from_user(&flags, arg, sizeof(flags))) {
1817b9ca0664SLiu Bo 		ret = -EFAULT;
1818b9ca0664SLiu Bo 		goto out_drop_write;
1819b9ca0664SLiu Bo 	}
18200caa102dSLi Zefan 
1821b9ca0664SLiu Bo 	if (flags & BTRFS_SUBVOL_CREATE_ASYNC) {
1822b9ca0664SLiu Bo 		ret = -EINVAL;
1823b9ca0664SLiu Bo 		goto out_drop_write;
1824b9ca0664SLiu Bo 	}
18250caa102dSLi Zefan 
1826b9ca0664SLiu Bo 	if (flags & ~BTRFS_SUBVOL_RDONLY) {
1827b9ca0664SLiu Bo 		ret = -EOPNOTSUPP;
1828b9ca0664SLiu Bo 		goto out_drop_write;
1829b9ca0664SLiu Bo 	}
18300caa102dSLi Zefan 
18310caa102dSLi Zefan 	down_write(&root->fs_info->subvol_sem);
18320caa102dSLi Zefan 
18330caa102dSLi Zefan 	/* nothing to do */
18340caa102dSLi Zefan 	if (!!(flags & BTRFS_SUBVOL_RDONLY) == btrfs_root_readonly(root))
1835b9ca0664SLiu Bo 		goto out_drop_sem;
18360caa102dSLi Zefan 
18370caa102dSLi Zefan 	root_flags = btrfs_root_flags(&root->root_item);
18382c686537SDavid Sterba 	if (flags & BTRFS_SUBVOL_RDONLY) {
18390caa102dSLi Zefan 		btrfs_set_root_flags(&root->root_item,
18400caa102dSLi Zefan 				     root_flags | BTRFS_ROOT_SUBVOL_RDONLY);
18412c686537SDavid Sterba 	} else {
18422c686537SDavid Sterba 		/*
18432c686537SDavid Sterba 		 * Block RO -> RW transition if this subvolume is involved in
18442c686537SDavid Sterba 		 * send
18452c686537SDavid Sterba 		 */
18462c686537SDavid Sterba 		spin_lock(&root->root_item_lock);
18472c686537SDavid Sterba 		if (root->send_in_progress == 0) {
18480caa102dSLi Zefan 			btrfs_set_root_flags(&root->root_item,
18490caa102dSLi Zefan 				     root_flags & ~BTRFS_ROOT_SUBVOL_RDONLY);
18502c686537SDavid Sterba 			spin_unlock(&root->root_item_lock);
18512c686537SDavid Sterba 		} else {
18522c686537SDavid Sterba 			spin_unlock(&root->root_item_lock);
18532c686537SDavid Sterba 			btrfs_warn(root->fs_info,
18542c686537SDavid Sterba 			"Attempt to set subvolume %llu read-write during send",
18552c686537SDavid Sterba 					root->root_key.objectid);
18562c686537SDavid Sterba 			ret = -EPERM;
18572c686537SDavid Sterba 			goto out_drop_sem;
18582c686537SDavid Sterba 		}
18592c686537SDavid Sterba 	}
18600caa102dSLi Zefan 
18610caa102dSLi Zefan 	trans = btrfs_start_transaction(root, 1);
18620caa102dSLi Zefan 	if (IS_ERR(trans)) {
18630caa102dSLi Zefan 		ret = PTR_ERR(trans);
18640caa102dSLi Zefan 		goto out_reset;
18650caa102dSLi Zefan 	}
18660caa102dSLi Zefan 
1867b4dc2b8cSLi Zefan 	ret = btrfs_update_root(trans, root->fs_info->tree_root,
18680caa102dSLi Zefan 				&root->root_key, &root->root_item);
18690caa102dSLi Zefan 
18700caa102dSLi Zefan 	btrfs_commit_transaction(trans, root);
18710caa102dSLi Zefan out_reset:
18720caa102dSLi Zefan 	if (ret)
18730caa102dSLi Zefan 		btrfs_set_root_flags(&root->root_item, root_flags);
1874b9ca0664SLiu Bo out_drop_sem:
18750caa102dSLi Zefan 	up_write(&root->fs_info->subvol_sem);
1876b9ca0664SLiu Bo out_drop_write:
1877b9ca0664SLiu Bo 	mnt_drop_write_file(file);
1878b9ca0664SLiu Bo out:
18790caa102dSLi Zefan 	return ret;
18800caa102dSLi Zefan }
18810caa102dSLi Zefan 
188276dda93cSYan, Zheng /*
188376dda93cSYan, Zheng  * helper to check if the subvolume references other subvolumes
188476dda93cSYan, Zheng  */
188576dda93cSYan, Zheng static noinline int may_destroy_subvol(struct btrfs_root *root)
188676dda93cSYan, Zheng {
188776dda93cSYan, Zheng 	struct btrfs_path *path;
1888175a2b87SJosef Bacik 	struct btrfs_dir_item *di;
188976dda93cSYan, Zheng 	struct btrfs_key key;
1890175a2b87SJosef Bacik 	u64 dir_id;
189176dda93cSYan, Zheng 	int ret;
189276dda93cSYan, Zheng 
189376dda93cSYan, Zheng 	path = btrfs_alloc_path();
189476dda93cSYan, Zheng 	if (!path)
189576dda93cSYan, Zheng 		return -ENOMEM;
189676dda93cSYan, Zheng 
1897175a2b87SJosef Bacik 	/* Make sure this root isn't set as the default subvol */
1898175a2b87SJosef Bacik 	dir_id = btrfs_super_root_dir(root->fs_info->super_copy);
1899175a2b87SJosef Bacik 	di = btrfs_lookup_dir_item(NULL, root->fs_info->tree_root, path,
1900175a2b87SJosef Bacik 				   dir_id, "default", 7, 0);
1901175a2b87SJosef Bacik 	if (di && !IS_ERR(di)) {
1902175a2b87SJosef Bacik 		btrfs_dir_item_key_to_cpu(path->nodes[0], di, &key);
1903175a2b87SJosef Bacik 		if (key.objectid == root->root_key.objectid) {
190472de6b53SGuangyu Sun 			ret = -EPERM;
190572de6b53SGuangyu Sun 			btrfs_err(root->fs_info, "deleting default subvolume "
190672de6b53SGuangyu Sun 				  "%llu is not allowed", key.objectid);
1907175a2b87SJosef Bacik 			goto out;
1908175a2b87SJosef Bacik 		}
1909175a2b87SJosef Bacik 		btrfs_release_path(path);
1910175a2b87SJosef Bacik 	}
1911175a2b87SJosef Bacik 
191276dda93cSYan, Zheng 	key.objectid = root->root_key.objectid;
191376dda93cSYan, Zheng 	key.type = BTRFS_ROOT_REF_KEY;
191476dda93cSYan, Zheng 	key.offset = (u64)-1;
191576dda93cSYan, Zheng 
191676dda93cSYan, Zheng 	ret = btrfs_search_slot(NULL, root->fs_info->tree_root,
191776dda93cSYan, Zheng 				&key, path, 0, 0);
191876dda93cSYan, Zheng 	if (ret < 0)
191976dda93cSYan, Zheng 		goto out;
192076dda93cSYan, Zheng 	BUG_ON(ret == 0);
192176dda93cSYan, Zheng 
192276dda93cSYan, Zheng 	ret = 0;
192376dda93cSYan, Zheng 	if (path->slots[0] > 0) {
192476dda93cSYan, Zheng 		path->slots[0]--;
192576dda93cSYan, Zheng 		btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
192676dda93cSYan, Zheng 		if (key.objectid == root->root_key.objectid &&
192776dda93cSYan, Zheng 		    key.type == BTRFS_ROOT_REF_KEY)
192876dda93cSYan, Zheng 			ret = -ENOTEMPTY;
192976dda93cSYan, Zheng 	}
193076dda93cSYan, Zheng out:
193176dda93cSYan, Zheng 	btrfs_free_path(path);
193276dda93cSYan, Zheng 	return ret;
193376dda93cSYan, Zheng }
193476dda93cSYan, Zheng 
1935ac8e9819SChris Mason static noinline int key_in_sk(struct btrfs_key *key,
1936ac8e9819SChris Mason 			      struct btrfs_ioctl_search_key *sk)
1937ac8e9819SChris Mason {
1938abc6e134SChris Mason 	struct btrfs_key test;
1939abc6e134SChris Mason 	int ret;
1940abc6e134SChris Mason 
1941abc6e134SChris Mason 	test.objectid = sk->min_objectid;
1942abc6e134SChris Mason 	test.type = sk->min_type;
1943abc6e134SChris Mason 	test.offset = sk->min_offset;
1944abc6e134SChris Mason 
1945abc6e134SChris Mason 	ret = btrfs_comp_cpu_keys(key, &test);
1946abc6e134SChris Mason 	if (ret < 0)
1947ac8e9819SChris Mason 		return 0;
1948abc6e134SChris Mason 
1949abc6e134SChris Mason 	test.objectid = sk->max_objectid;
1950abc6e134SChris Mason 	test.type = sk->max_type;
1951abc6e134SChris Mason 	test.offset = sk->max_offset;
1952abc6e134SChris Mason 
1953abc6e134SChris Mason 	ret = btrfs_comp_cpu_keys(key, &test);
1954abc6e134SChris Mason 	if (ret > 0)
1955ac8e9819SChris Mason 		return 0;
1956ac8e9819SChris Mason 	return 1;
1957ac8e9819SChris Mason }
1958ac8e9819SChris Mason 
1959ac8e9819SChris Mason static noinline int copy_to_sk(struct btrfs_root *root,
1960ac8e9819SChris Mason 			       struct btrfs_path *path,
1961ac8e9819SChris Mason 			       struct btrfs_key *key,
1962ac8e9819SChris Mason 			       struct btrfs_ioctl_search_key *sk,
19639b6e817dSGerhard Heift 			       size_t *buf_size,
1964ba346b35SGerhard Heift 			       char __user *ubuf,
1965ac8e9819SChris Mason 			       unsigned long *sk_offset,
1966ac8e9819SChris Mason 			       int *num_found)
1967ac8e9819SChris Mason {
1968ac8e9819SChris Mason 	u64 found_transid;
1969ac8e9819SChris Mason 	struct extent_buffer *leaf;
1970ac8e9819SChris Mason 	struct btrfs_ioctl_search_header sh;
1971ac8e9819SChris Mason 	unsigned long item_off;
1972ac8e9819SChris Mason 	unsigned long item_len;
1973ac8e9819SChris Mason 	int nritems;
1974ac8e9819SChris Mason 	int i;
1975ac8e9819SChris Mason 	int slot;
1976ac8e9819SChris Mason 	int ret = 0;
1977ac8e9819SChris Mason 
1978ac8e9819SChris Mason 	leaf = path->nodes[0];
1979ac8e9819SChris Mason 	slot = path->slots[0];
1980ac8e9819SChris Mason 	nritems = btrfs_header_nritems(leaf);
1981ac8e9819SChris Mason 
1982ac8e9819SChris Mason 	if (btrfs_header_generation(leaf) > sk->max_transid) {
1983ac8e9819SChris Mason 		i = nritems;
1984ac8e9819SChris Mason 		goto advance_key;
1985ac8e9819SChris Mason 	}
1986ac8e9819SChris Mason 	found_transid = btrfs_header_generation(leaf);
1987ac8e9819SChris Mason 
1988ac8e9819SChris Mason 	for (i = slot; i < nritems; i++) {
1989ac8e9819SChris Mason 		item_off = btrfs_item_ptr_offset(leaf, i);
1990ac8e9819SChris Mason 		item_len = btrfs_item_size_nr(leaf, i);
1991ac8e9819SChris Mason 
199203b71c6cSGabriel de Perthuis 		btrfs_item_key_to_cpu(leaf, key, i);
199303b71c6cSGabriel de Perthuis 		if (!key_in_sk(key, sk))
199403b71c6cSGabriel de Perthuis 			continue;
199503b71c6cSGabriel de Perthuis 
19969b6e817dSGerhard Heift 		if (sizeof(sh) + item_len > *buf_size) {
19978f5f6178SGerhard Heift 			if (*num_found) {
19988f5f6178SGerhard Heift 				ret = 1;
19998f5f6178SGerhard Heift 				goto out;
20008f5f6178SGerhard Heift 			}
20018f5f6178SGerhard Heift 
20028f5f6178SGerhard Heift 			/*
20038f5f6178SGerhard Heift 			 * return one empty item back for v1, which does not
20048f5f6178SGerhard Heift 			 * handle -EOVERFLOW
20058f5f6178SGerhard Heift 			 */
20068f5f6178SGerhard Heift 
20079b6e817dSGerhard Heift 			*buf_size = sizeof(sh) + item_len;
2008ac8e9819SChris Mason 			item_len = 0;
20098f5f6178SGerhard Heift 			ret = -EOVERFLOW;
20108f5f6178SGerhard Heift 		}
2011ac8e9819SChris Mason 
20129b6e817dSGerhard Heift 		if (sizeof(sh) + item_len + *sk_offset > *buf_size) {
2013ac8e9819SChris Mason 			ret = 1;
201425c9bc2eSGerhard Heift 			goto out;
2015ac8e9819SChris Mason 		}
2016ac8e9819SChris Mason 
2017ac8e9819SChris Mason 		sh.objectid = key->objectid;
2018ac8e9819SChris Mason 		sh.offset = key->offset;
2019ac8e9819SChris Mason 		sh.type = key->type;
2020ac8e9819SChris Mason 		sh.len = item_len;
2021ac8e9819SChris Mason 		sh.transid = found_transid;
2022ac8e9819SChris Mason 
2023ac8e9819SChris Mason 		/* copy search result header */
2024ba346b35SGerhard Heift 		if (copy_to_user(ubuf + *sk_offset, &sh, sizeof(sh))) {
2025ba346b35SGerhard Heift 			ret = -EFAULT;
2026ba346b35SGerhard Heift 			goto out;
2027ba346b35SGerhard Heift 		}
2028ba346b35SGerhard Heift 
2029ac8e9819SChris Mason 		*sk_offset += sizeof(sh);
2030ac8e9819SChris Mason 
2031ac8e9819SChris Mason 		if (item_len) {
2032ba346b35SGerhard Heift 			char __user *up = ubuf + *sk_offset;
2033ac8e9819SChris Mason 			/* copy the item */
2034ba346b35SGerhard Heift 			if (read_extent_buffer_to_user(leaf, up,
2035ba346b35SGerhard Heift 						       item_off, item_len)) {
2036ba346b35SGerhard Heift 				ret = -EFAULT;
2037ba346b35SGerhard Heift 				goto out;
2038ba346b35SGerhard Heift 			}
2039ba346b35SGerhard Heift 
2040ac8e9819SChris Mason 			*sk_offset += item_len;
2041ac8e9819SChris Mason 		}
2042e2156867SHugo Mills 		(*num_found)++;
2043ac8e9819SChris Mason 
20448f5f6178SGerhard Heift 		if (ret) /* -EOVERFLOW from above */
20458f5f6178SGerhard Heift 			goto out;
20468f5f6178SGerhard Heift 
204725c9bc2eSGerhard Heift 		if (*num_found >= sk->nr_items) {
204825c9bc2eSGerhard Heift 			ret = 1;
204925c9bc2eSGerhard Heift 			goto out;
205025c9bc2eSGerhard Heift 		}
2051ac8e9819SChris Mason 	}
2052ac8e9819SChris Mason advance_key:
2053ac8e9819SChris Mason 	ret = 0;
2054abc6e134SChris Mason 	if (key->offset < (u64)-1 && key->offset < sk->max_offset)
2055abc6e134SChris Mason 		key->offset++;
2056abc6e134SChris Mason 	else if (key->type < (u8)-1 && key->type < sk->max_type) {
2057abc6e134SChris Mason 		key->offset = 0;
2058abc6e134SChris Mason 		key->type++;
2059abc6e134SChris Mason 	} else if (key->objectid < (u64)-1 && key->objectid < sk->max_objectid) {
2060abc6e134SChris Mason 		key->offset = 0;
2061abc6e134SChris Mason 		key->type = 0;
2062abc6e134SChris Mason 		key->objectid++;
2063abc6e134SChris Mason 	} else
2064abc6e134SChris Mason 		ret = 1;
206525c9bc2eSGerhard Heift out:
2066ba346b35SGerhard Heift 	/*
2067ba346b35SGerhard Heift 	 *  0: all items from this leaf copied, continue with next
2068ba346b35SGerhard Heift 	 *  1: * more items can be copied, but unused buffer is too small
2069ba346b35SGerhard Heift 	 *     * all items were found
2070ba346b35SGerhard Heift 	 *     Either way, it will stops the loop which iterates to the next
2071ba346b35SGerhard Heift 	 *     leaf
2072ba346b35SGerhard Heift 	 *  -EOVERFLOW: item was to large for buffer
2073ba346b35SGerhard Heift 	 *  -EFAULT: could not copy extent buffer back to userspace
2074ba346b35SGerhard Heift 	 */
2075ac8e9819SChris Mason 	return ret;
2076ac8e9819SChris Mason }
2077ac8e9819SChris Mason 
2078ac8e9819SChris Mason static noinline int search_ioctl(struct inode *inode,
207912544442SGerhard Heift 				 struct btrfs_ioctl_search_key *sk,
20809b6e817dSGerhard Heift 				 size_t *buf_size,
2081ba346b35SGerhard Heift 				 char __user *ubuf)
2082ac8e9819SChris Mason {
2083ac8e9819SChris Mason 	struct btrfs_root *root;
2084ac8e9819SChris Mason 	struct btrfs_key key;
2085ac8e9819SChris Mason 	struct btrfs_path *path;
2086ac8e9819SChris Mason 	struct btrfs_fs_info *info = BTRFS_I(inode)->root->fs_info;
2087ac8e9819SChris Mason 	int ret;
2088ac8e9819SChris Mason 	int num_found = 0;
2089ac8e9819SChris Mason 	unsigned long sk_offset = 0;
2090ac8e9819SChris Mason 
20919b6e817dSGerhard Heift 	if (*buf_size < sizeof(struct btrfs_ioctl_search_header)) {
20929b6e817dSGerhard Heift 		*buf_size = sizeof(struct btrfs_ioctl_search_header);
209312544442SGerhard Heift 		return -EOVERFLOW;
20949b6e817dSGerhard Heift 	}
209512544442SGerhard Heift 
2096ac8e9819SChris Mason 	path = btrfs_alloc_path();
2097ac8e9819SChris Mason 	if (!path)
2098ac8e9819SChris Mason 		return -ENOMEM;
2099ac8e9819SChris Mason 
2100ac8e9819SChris Mason 	if (sk->tree_id == 0) {
2101ac8e9819SChris Mason 		/* search the root of the inode that was passed */
2102ac8e9819SChris Mason 		root = BTRFS_I(inode)->root;
2103ac8e9819SChris Mason 	} else {
2104ac8e9819SChris Mason 		key.objectid = sk->tree_id;
2105ac8e9819SChris Mason 		key.type = BTRFS_ROOT_ITEM_KEY;
2106ac8e9819SChris Mason 		key.offset = (u64)-1;
2107ac8e9819SChris Mason 		root = btrfs_read_fs_root_no_name(info, &key);
2108ac8e9819SChris Mason 		if (IS_ERR(root)) {
2109efe120a0SFrank Holton 			printk(KERN_ERR "BTRFS: could not find root %llu\n",
2110ac8e9819SChris Mason 			       sk->tree_id);
2111ac8e9819SChris Mason 			btrfs_free_path(path);
2112ac8e9819SChris Mason 			return -ENOENT;
2113ac8e9819SChris Mason 		}
2114ac8e9819SChris Mason 	}
2115ac8e9819SChris Mason 
2116ac8e9819SChris Mason 	key.objectid = sk->min_objectid;
2117ac8e9819SChris Mason 	key.type = sk->min_type;
2118ac8e9819SChris Mason 	key.offset = sk->min_offset;
2119ac8e9819SChris Mason 
2120ac8e9819SChris Mason 	path->keep_locks = 1;
2121ac8e9819SChris Mason 
2122ac8e9819SChris Mason 	while (1) {
21236174d3cbSFilipe David Borba Manana 		ret = btrfs_search_forward(root, &key, path, sk->min_transid);
2124ac8e9819SChris Mason 		if (ret != 0) {
2125ac8e9819SChris Mason 			if (ret > 0)
2126ac8e9819SChris Mason 				ret = 0;
2127ac8e9819SChris Mason 			goto err;
2128ac8e9819SChris Mason 		}
2129ba346b35SGerhard Heift 		ret = copy_to_sk(root, path, &key, sk, buf_size, ubuf,
2130ac8e9819SChris Mason 				 &sk_offset, &num_found);
2131b3b4aa74SDavid Sterba 		btrfs_release_path(path);
213225c9bc2eSGerhard Heift 		if (ret)
2133ac8e9819SChris Mason 			break;
2134ac8e9819SChris Mason 
2135ac8e9819SChris Mason 	}
21368f5f6178SGerhard Heift 	if (ret > 0)
2137ac8e9819SChris Mason 		ret = 0;
2138ac8e9819SChris Mason err:
2139ac8e9819SChris Mason 	sk->nr_items = num_found;
2140ac8e9819SChris Mason 	btrfs_free_path(path);
2141ac8e9819SChris Mason 	return ret;
2142ac8e9819SChris Mason }
2143ac8e9819SChris Mason 
2144ac8e9819SChris Mason static noinline int btrfs_ioctl_tree_search(struct file *file,
2145ac8e9819SChris Mason 					   void __user *argp)
2146ac8e9819SChris Mason {
2147ba346b35SGerhard Heift 	struct btrfs_ioctl_search_args __user *uargs;
2148ba346b35SGerhard Heift 	struct btrfs_ioctl_search_key sk;
2149ac8e9819SChris Mason 	struct inode *inode;
2150ac8e9819SChris Mason 	int ret;
21519b6e817dSGerhard Heift 	size_t buf_size;
2152ac8e9819SChris Mason 
2153ac8e9819SChris Mason 	if (!capable(CAP_SYS_ADMIN))
2154ac8e9819SChris Mason 		return -EPERM;
2155ac8e9819SChris Mason 
2156ba346b35SGerhard Heift 	uargs = (struct btrfs_ioctl_search_args __user *)argp;
2157ac8e9819SChris Mason 
2158ba346b35SGerhard Heift 	if (copy_from_user(&sk, &uargs->key, sizeof(sk)))
2159ba346b35SGerhard Heift 		return -EFAULT;
2160ba346b35SGerhard Heift 
2161ba346b35SGerhard Heift 	buf_size = sizeof(uargs->buf);
21629b6e817dSGerhard Heift 
2163496ad9aaSAl Viro 	inode = file_inode(file);
2164ba346b35SGerhard Heift 	ret = search_ioctl(inode, &sk, &buf_size, uargs->buf);
21658f5f6178SGerhard Heift 
21668f5f6178SGerhard Heift 	/*
21678f5f6178SGerhard Heift 	 * In the origin implementation an overflow is handled by returning a
21688f5f6178SGerhard Heift 	 * search header with a len of zero, so reset ret.
21698f5f6178SGerhard Heift 	 */
21708f5f6178SGerhard Heift 	if (ret == -EOVERFLOW)
21718f5f6178SGerhard Heift 		ret = 0;
21728f5f6178SGerhard Heift 
2173ba346b35SGerhard Heift 	if (ret == 0 && copy_to_user(&uargs->key, &sk, sizeof(sk)))
2174ac8e9819SChris Mason 		ret = -EFAULT;
2175ac8e9819SChris Mason 	return ret;
2176ac8e9819SChris Mason }
2177ac8e9819SChris Mason 
2178cc68a8a5SGerhard Heift static noinline int btrfs_ioctl_tree_search_v2(struct file *file,
2179cc68a8a5SGerhard Heift 					       void __user *argp)
2180cc68a8a5SGerhard Heift {
2181cc68a8a5SGerhard Heift 	struct btrfs_ioctl_search_args_v2 __user *uarg;
2182cc68a8a5SGerhard Heift 	struct btrfs_ioctl_search_args_v2 args;
2183cc68a8a5SGerhard Heift 	struct inode *inode;
2184cc68a8a5SGerhard Heift 	int ret;
2185cc68a8a5SGerhard Heift 	size_t buf_size;
2186cc68a8a5SGerhard Heift 	const size_t buf_limit = 16 * 1024 * 1024;
2187cc68a8a5SGerhard Heift 
2188cc68a8a5SGerhard Heift 	if (!capable(CAP_SYS_ADMIN))
2189cc68a8a5SGerhard Heift 		return -EPERM;
2190cc68a8a5SGerhard Heift 
2191cc68a8a5SGerhard Heift 	/* copy search header and buffer size */
2192cc68a8a5SGerhard Heift 	uarg = (struct btrfs_ioctl_search_args_v2 __user *)argp;
2193cc68a8a5SGerhard Heift 	if (copy_from_user(&args, uarg, sizeof(args)))
2194cc68a8a5SGerhard Heift 		return -EFAULT;
2195cc68a8a5SGerhard Heift 
2196cc68a8a5SGerhard Heift 	buf_size = args.buf_size;
2197cc68a8a5SGerhard Heift 
2198cc68a8a5SGerhard Heift 	if (buf_size < sizeof(struct btrfs_ioctl_search_header))
2199cc68a8a5SGerhard Heift 		return -EOVERFLOW;
2200cc68a8a5SGerhard Heift 
2201cc68a8a5SGerhard Heift 	/* limit result size to 16MB */
2202cc68a8a5SGerhard Heift 	if (buf_size > buf_limit)
2203cc68a8a5SGerhard Heift 		buf_size = buf_limit;
2204cc68a8a5SGerhard Heift 
2205cc68a8a5SGerhard Heift 	inode = file_inode(file);
2206cc68a8a5SGerhard Heift 	ret = search_ioctl(inode, &args.key, &buf_size,
2207cc68a8a5SGerhard Heift 			   (char *)(&uarg->buf[0]));
2208cc68a8a5SGerhard Heift 	if (ret == 0 && copy_to_user(&uarg->key, &args.key, sizeof(args.key)))
2209cc68a8a5SGerhard Heift 		ret = -EFAULT;
2210cc68a8a5SGerhard Heift 	else if (ret == -EOVERFLOW &&
2211cc68a8a5SGerhard Heift 		copy_to_user(&uarg->buf_size, &buf_size, sizeof(buf_size)))
2212cc68a8a5SGerhard Heift 		ret = -EFAULT;
2213cc68a8a5SGerhard Heift 
2214cc68a8a5SGerhard Heift 	return ret;
2215cc68a8a5SGerhard Heift }
2216cc68a8a5SGerhard Heift 
221798d377a0STARUISI Hiroaki /*
2218ac8e9819SChris Mason  * Search INODE_REFs to identify path name of 'dirid' directory
2219ac8e9819SChris Mason  * in a 'tree_id' tree. and sets path name to 'name'.
222098d377a0STARUISI Hiroaki  */
222198d377a0STARUISI Hiroaki static noinline int btrfs_search_path_in_tree(struct btrfs_fs_info *info,
222298d377a0STARUISI Hiroaki 				u64 tree_id, u64 dirid, char *name)
222398d377a0STARUISI Hiroaki {
222498d377a0STARUISI Hiroaki 	struct btrfs_root *root;
222598d377a0STARUISI Hiroaki 	struct btrfs_key key;
2226ac8e9819SChris Mason 	char *ptr;
222798d377a0STARUISI Hiroaki 	int ret = -1;
222898d377a0STARUISI Hiroaki 	int slot;
222998d377a0STARUISI Hiroaki 	int len;
223098d377a0STARUISI Hiroaki 	int total_len = 0;
223198d377a0STARUISI Hiroaki 	struct btrfs_inode_ref *iref;
223298d377a0STARUISI Hiroaki 	struct extent_buffer *l;
223398d377a0STARUISI Hiroaki 	struct btrfs_path *path;
223498d377a0STARUISI Hiroaki 
223598d377a0STARUISI Hiroaki 	if (dirid == BTRFS_FIRST_FREE_OBJECTID) {
223698d377a0STARUISI Hiroaki 		name[0]='\0';
223798d377a0STARUISI Hiroaki 		return 0;
223898d377a0STARUISI Hiroaki 	}
223998d377a0STARUISI Hiroaki 
224098d377a0STARUISI Hiroaki 	path = btrfs_alloc_path();
224198d377a0STARUISI Hiroaki 	if (!path)
224298d377a0STARUISI Hiroaki 		return -ENOMEM;
224398d377a0STARUISI Hiroaki 
2244ac8e9819SChris Mason 	ptr = &name[BTRFS_INO_LOOKUP_PATH_MAX];
224598d377a0STARUISI Hiroaki 
224698d377a0STARUISI Hiroaki 	key.objectid = tree_id;
224798d377a0STARUISI Hiroaki 	key.type = BTRFS_ROOT_ITEM_KEY;
224898d377a0STARUISI Hiroaki 	key.offset = (u64)-1;
224998d377a0STARUISI Hiroaki 	root = btrfs_read_fs_root_no_name(info, &key);
225098d377a0STARUISI Hiroaki 	if (IS_ERR(root)) {
2251efe120a0SFrank Holton 		printk(KERN_ERR "BTRFS: could not find root %llu\n", tree_id);
22528ad6fcabSChris Mason 		ret = -ENOENT;
22538ad6fcabSChris Mason 		goto out;
225498d377a0STARUISI Hiroaki 	}
225598d377a0STARUISI Hiroaki 
225698d377a0STARUISI Hiroaki 	key.objectid = dirid;
225798d377a0STARUISI Hiroaki 	key.type = BTRFS_INODE_REF_KEY;
22588ad6fcabSChris Mason 	key.offset = (u64)-1;
225998d377a0STARUISI Hiroaki 
226098d377a0STARUISI Hiroaki 	while (1) {
226198d377a0STARUISI Hiroaki 		ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
226298d377a0STARUISI Hiroaki 		if (ret < 0)
226398d377a0STARUISI Hiroaki 			goto out;
226418674c6cSFilipe David Borba Manana 		else if (ret > 0) {
226518674c6cSFilipe David Borba Manana 			ret = btrfs_previous_item(root, path, dirid,
226618674c6cSFilipe David Borba Manana 						  BTRFS_INODE_REF_KEY);
226718674c6cSFilipe David Borba Manana 			if (ret < 0)
226818674c6cSFilipe David Borba Manana 				goto out;
226918674c6cSFilipe David Borba Manana 			else if (ret > 0) {
2270ac8e9819SChris Mason 				ret = -ENOENT;
227198d377a0STARUISI Hiroaki 				goto out;
2272ac8e9819SChris Mason 			}
227318674c6cSFilipe David Borba Manana 		}
227418674c6cSFilipe David Borba Manana 
227518674c6cSFilipe David Borba Manana 		l = path->nodes[0];
227618674c6cSFilipe David Borba Manana 		slot = path->slots[0];
227718674c6cSFilipe David Borba Manana 		btrfs_item_key_to_cpu(l, &key, slot);
227898d377a0STARUISI Hiroaki 
227998d377a0STARUISI Hiroaki 		iref = btrfs_item_ptr(l, slot, struct btrfs_inode_ref);
228098d377a0STARUISI Hiroaki 		len = btrfs_inode_ref_name_len(l, iref);
228198d377a0STARUISI Hiroaki 		ptr -= len + 1;
228298d377a0STARUISI Hiroaki 		total_len += len + 1;
2283a696cf35SFilipe David Borba Manana 		if (ptr < name) {
2284a696cf35SFilipe David Borba Manana 			ret = -ENAMETOOLONG;
228598d377a0STARUISI Hiroaki 			goto out;
2286a696cf35SFilipe David Borba Manana 		}
228798d377a0STARUISI Hiroaki 
228898d377a0STARUISI Hiroaki 		*(ptr + len) = '/';
228998d377a0STARUISI Hiroaki 		read_extent_buffer(l, ptr, (unsigned long)(iref + 1), len);
229098d377a0STARUISI Hiroaki 
229198d377a0STARUISI Hiroaki 		if (key.offset == BTRFS_FIRST_FREE_OBJECTID)
229298d377a0STARUISI Hiroaki 			break;
229398d377a0STARUISI Hiroaki 
2294b3b4aa74SDavid Sterba 		btrfs_release_path(path);
229598d377a0STARUISI Hiroaki 		key.objectid = key.offset;
22968ad6fcabSChris Mason 		key.offset = (u64)-1;
229798d377a0STARUISI Hiroaki 		dirid = key.objectid;
229898d377a0STARUISI Hiroaki 	}
229977906a50SLi Zefan 	memmove(name, ptr, total_len);
230098d377a0STARUISI Hiroaki 	name[total_len] = '\0';
230198d377a0STARUISI Hiroaki 	ret = 0;
230298d377a0STARUISI Hiroaki out:
230398d377a0STARUISI Hiroaki 	btrfs_free_path(path);
2304ac8e9819SChris Mason 	return ret;
2305ac8e9819SChris Mason }
2306ac8e9819SChris Mason 
2307ac8e9819SChris Mason static noinline int btrfs_ioctl_ino_lookup(struct file *file,
2308ac8e9819SChris Mason 					   void __user *argp)
2309ac8e9819SChris Mason {
2310ac8e9819SChris Mason 	 struct btrfs_ioctl_ino_lookup_args *args;
2311ac8e9819SChris Mason 	 struct inode *inode;
2312ac8e9819SChris Mason 	 int ret;
2313ac8e9819SChris Mason 
2314ac8e9819SChris Mason 	if (!capable(CAP_SYS_ADMIN))
2315ac8e9819SChris Mason 		return -EPERM;
2316ac8e9819SChris Mason 
23172354d08fSJulia Lawall 	args = memdup_user(argp, sizeof(*args));
23182354d08fSJulia Lawall 	if (IS_ERR(args))
23192354d08fSJulia Lawall 		return PTR_ERR(args);
2320c2b96929SDan Carpenter 
2321496ad9aaSAl Viro 	inode = file_inode(file);
2322ac8e9819SChris Mason 
23231b53ac4dSChris Mason 	if (args->treeid == 0)
23241b53ac4dSChris Mason 		args->treeid = BTRFS_I(inode)->root->root_key.objectid;
23251b53ac4dSChris Mason 
2326ac8e9819SChris Mason 	ret = btrfs_search_path_in_tree(BTRFS_I(inode)->root->fs_info,
2327ac8e9819SChris Mason 					args->treeid, args->objectid,
2328ac8e9819SChris Mason 					args->name);
2329ac8e9819SChris Mason 
2330ac8e9819SChris Mason 	if (ret == 0 && copy_to_user(argp, args, sizeof(*args)))
2331ac8e9819SChris Mason 		ret = -EFAULT;
2332ac8e9819SChris Mason 
2333ac8e9819SChris Mason 	kfree(args);
233498d377a0STARUISI Hiroaki 	return ret;
233598d377a0STARUISI Hiroaki }
233698d377a0STARUISI Hiroaki 
233776dda93cSYan, Zheng static noinline int btrfs_ioctl_snap_destroy(struct file *file,
233876dda93cSYan, Zheng 					     void __user *arg)
233976dda93cSYan, Zheng {
234054563d41SAl Viro 	struct dentry *parent = file->f_path.dentry;
234176dda93cSYan, Zheng 	struct dentry *dentry;
234276dda93cSYan, Zheng 	struct inode *dir = parent->d_inode;
234376dda93cSYan, Zheng 	struct inode *inode;
234476dda93cSYan, Zheng 	struct btrfs_root *root = BTRFS_I(dir)->root;
234576dda93cSYan, Zheng 	struct btrfs_root *dest = NULL;
234676dda93cSYan, Zheng 	struct btrfs_ioctl_vol_args *vol_args;
234776dda93cSYan, Zheng 	struct btrfs_trans_handle *trans;
2348c58aaad2SMiao Xie 	struct btrfs_block_rsv block_rsv;
2349521e0546SDavid Sterba 	u64 root_flags;
2350c58aaad2SMiao Xie 	u64 qgroup_reserved;
235176dda93cSYan, Zheng 	int namelen;
235276dda93cSYan, Zheng 	int ret;
235376dda93cSYan, Zheng 	int err = 0;
235476dda93cSYan, Zheng 
235576dda93cSYan, Zheng 	vol_args = memdup_user(arg, sizeof(*vol_args));
235676dda93cSYan, Zheng 	if (IS_ERR(vol_args))
235776dda93cSYan, Zheng 		return PTR_ERR(vol_args);
235876dda93cSYan, Zheng 
235976dda93cSYan, Zheng 	vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
236076dda93cSYan, Zheng 	namelen = strlen(vol_args->name);
236176dda93cSYan, Zheng 	if (strchr(vol_args->name, '/') ||
236276dda93cSYan, Zheng 	    strncmp(vol_args->name, "..", namelen) == 0) {
236376dda93cSYan, Zheng 		err = -EINVAL;
236476dda93cSYan, Zheng 		goto out;
236576dda93cSYan, Zheng 	}
236676dda93cSYan, Zheng 
2367a561be71SAl Viro 	err = mnt_want_write_file(file);
236876dda93cSYan, Zheng 	if (err)
236976dda93cSYan, Zheng 		goto out;
237076dda93cSYan, Zheng 
2371521e0546SDavid Sterba 
23725c50c9b8SDavid Sterba 	err = mutex_lock_killable_nested(&dir->i_mutex, I_MUTEX_PARENT);
23735c50c9b8SDavid Sterba 	if (err == -EINTR)
2374e43f998eSDavid Sterba 		goto out_drop_write;
237576dda93cSYan, Zheng 	dentry = lookup_one_len(vol_args->name, parent, namelen);
237676dda93cSYan, Zheng 	if (IS_ERR(dentry)) {
237776dda93cSYan, Zheng 		err = PTR_ERR(dentry);
237876dda93cSYan, Zheng 		goto out_unlock_dir;
237976dda93cSYan, Zheng 	}
238076dda93cSYan, Zheng 
238176dda93cSYan, Zheng 	if (!dentry->d_inode) {
238276dda93cSYan, Zheng 		err = -ENOENT;
238376dda93cSYan, Zheng 		goto out_dput;
238476dda93cSYan, Zheng 	}
238576dda93cSYan, Zheng 
238676dda93cSYan, Zheng 	inode = dentry->d_inode;
23874260f7c7SSage Weil 	dest = BTRFS_I(inode)->root;
23884260f7c7SSage Weil 	if (!capable(CAP_SYS_ADMIN)) {
23894260f7c7SSage Weil 		/*
23904260f7c7SSage Weil 		 * Regular user.  Only allow this with a special mount
23914260f7c7SSage Weil 		 * option, when the user has write+exec access to the
23924260f7c7SSage Weil 		 * subvol root, and when rmdir(2) would have been
23934260f7c7SSage Weil 		 * allowed.
23944260f7c7SSage Weil 		 *
23954260f7c7SSage Weil 		 * Note that this is _not_ check that the subvol is
23964260f7c7SSage Weil 		 * empty or doesn't contain data that we wouldn't
23974260f7c7SSage Weil 		 * otherwise be able to delete.
23984260f7c7SSage Weil 		 *
23994260f7c7SSage Weil 		 * Users who want to delete empty subvols should try
24004260f7c7SSage Weil 		 * rmdir(2).
24014260f7c7SSage Weil 		 */
24024260f7c7SSage Weil 		err = -EPERM;
24034260f7c7SSage Weil 		if (!btrfs_test_opt(root, USER_SUBVOL_RM_ALLOWED))
24044260f7c7SSage Weil 			goto out_dput;
24054260f7c7SSage Weil 
24064260f7c7SSage Weil 		/*
24074260f7c7SSage Weil 		 * Do not allow deletion if the parent dir is the same
24084260f7c7SSage Weil 		 * as the dir to be deleted.  That means the ioctl
24094260f7c7SSage Weil 		 * must be called on the dentry referencing the root
24104260f7c7SSage Weil 		 * of the subvol, not a random directory contained
24114260f7c7SSage Weil 		 * within it.
24124260f7c7SSage Weil 		 */
24134260f7c7SSage Weil 		err = -EINVAL;
24144260f7c7SSage Weil 		if (root == dest)
24154260f7c7SSage Weil 			goto out_dput;
24164260f7c7SSage Weil 
24174260f7c7SSage Weil 		err = inode_permission(inode, MAY_WRITE | MAY_EXEC);
24184260f7c7SSage Weil 		if (err)
24194260f7c7SSage Weil 			goto out_dput;
24205c39da5bSMiao Xie 	}
24214260f7c7SSage Weil 
24225c39da5bSMiao Xie 	/* check if subvolume may be deleted by a user */
24234260f7c7SSage Weil 	err = btrfs_may_delete(dir, dentry, 1);
24244260f7c7SSage Weil 	if (err)
24254260f7c7SSage Weil 		goto out_dput;
24264260f7c7SSage Weil 
242733345d01SLi Zefan 	if (btrfs_ino(inode) != BTRFS_FIRST_FREE_OBJECTID) {
242876dda93cSYan, Zheng 		err = -EINVAL;
242976dda93cSYan, Zheng 		goto out_dput;
243076dda93cSYan, Zheng 	}
243176dda93cSYan, Zheng 
243276dda93cSYan, Zheng 	mutex_lock(&inode->i_mutex);
2433521e0546SDavid Sterba 
2434521e0546SDavid Sterba 	/*
2435521e0546SDavid Sterba 	 * Don't allow to delete a subvolume with send in progress. This is
2436521e0546SDavid Sterba 	 * inside the i_mutex so the error handling that has to drop the bit
2437521e0546SDavid Sterba 	 * again is not run concurrently.
2438521e0546SDavid Sterba 	 */
2439521e0546SDavid Sterba 	spin_lock(&dest->root_item_lock);
2440c55bfa67SFilipe Manana 	root_flags = btrfs_root_flags(&dest->root_item);
2441c55bfa67SFilipe Manana 	if (dest->send_in_progress == 0) {
2442c55bfa67SFilipe Manana 		btrfs_set_root_flags(&dest->root_item,
2443521e0546SDavid Sterba 				root_flags | BTRFS_ROOT_SUBVOL_DEAD);
2444521e0546SDavid Sterba 		spin_unlock(&dest->root_item_lock);
2445521e0546SDavid Sterba 	} else {
2446521e0546SDavid Sterba 		spin_unlock(&dest->root_item_lock);
2447521e0546SDavid Sterba 		btrfs_warn(root->fs_info,
2448521e0546SDavid Sterba 			"Attempt to delete subvolume %llu during send",
2449c55bfa67SFilipe Manana 			dest->root_key.objectid);
2450521e0546SDavid Sterba 		err = -EPERM;
2451521e0546SDavid Sterba 		goto out_dput;
2452521e0546SDavid Sterba 	}
2453521e0546SDavid Sterba 
245476dda93cSYan, Zheng 	err = d_invalidate(dentry);
245576dda93cSYan, Zheng 	if (err)
245676dda93cSYan, Zheng 		goto out_unlock;
245776dda93cSYan, Zheng 
245876dda93cSYan, Zheng 	down_write(&root->fs_info->subvol_sem);
245976dda93cSYan, Zheng 
246076dda93cSYan, Zheng 	err = may_destroy_subvol(dest);
246176dda93cSYan, Zheng 	if (err)
246276dda93cSYan, Zheng 		goto out_up_write;
246376dda93cSYan, Zheng 
2464c58aaad2SMiao Xie 	btrfs_init_block_rsv(&block_rsv, BTRFS_BLOCK_RSV_TEMP);
2465c58aaad2SMiao Xie 	/*
2466c58aaad2SMiao Xie 	 * One for dir inode, two for dir entries, two for root
2467c58aaad2SMiao Xie 	 * ref/backref.
2468c58aaad2SMiao Xie 	 */
2469c58aaad2SMiao Xie 	err = btrfs_subvolume_reserve_metadata(root, &block_rsv,
2470ee3441b4SJeff Mahoney 					       5, &qgroup_reserved, true);
2471c58aaad2SMiao Xie 	if (err)
2472c58aaad2SMiao Xie 		goto out_up_write;
2473c58aaad2SMiao Xie 
2474a22285a6SYan, Zheng 	trans = btrfs_start_transaction(root, 0);
2475a22285a6SYan, Zheng 	if (IS_ERR(trans)) {
2476a22285a6SYan, Zheng 		err = PTR_ERR(trans);
2477c58aaad2SMiao Xie 		goto out_release;
2478a22285a6SYan, Zheng 	}
2479c58aaad2SMiao Xie 	trans->block_rsv = &block_rsv;
2480c58aaad2SMiao Xie 	trans->bytes_reserved = block_rsv.size;
2481a22285a6SYan, Zheng 
248276dda93cSYan, Zheng 	ret = btrfs_unlink_subvol(trans, root, dir,
248376dda93cSYan, Zheng 				dest->root_key.objectid,
248476dda93cSYan, Zheng 				dentry->d_name.name,
248576dda93cSYan, Zheng 				dentry->d_name.len);
248679787eaaSJeff Mahoney 	if (ret) {
248779787eaaSJeff Mahoney 		err = ret;
248879787eaaSJeff Mahoney 		btrfs_abort_transaction(trans, root, ret);
248979787eaaSJeff Mahoney 		goto out_end_trans;
249079787eaaSJeff Mahoney 	}
249176dda93cSYan, Zheng 
249276dda93cSYan, Zheng 	btrfs_record_root_in_trans(trans, dest);
249376dda93cSYan, Zheng 
249476dda93cSYan, Zheng 	memset(&dest->root_item.drop_progress, 0,
249576dda93cSYan, Zheng 		sizeof(dest->root_item.drop_progress));
249676dda93cSYan, Zheng 	dest->root_item.drop_level = 0;
249776dda93cSYan, Zheng 	btrfs_set_root_refs(&dest->root_item, 0);
249876dda93cSYan, Zheng 
249927cdeb70SMiao Xie 	if (!test_and_set_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED, &dest->state)) {
250076dda93cSYan, Zheng 		ret = btrfs_insert_orphan_item(trans,
250176dda93cSYan, Zheng 					root->fs_info->tree_root,
250276dda93cSYan, Zheng 					dest->root_key.objectid);
250379787eaaSJeff Mahoney 		if (ret) {
250479787eaaSJeff Mahoney 			btrfs_abort_transaction(trans, root, ret);
250579787eaaSJeff Mahoney 			err = ret;
250679787eaaSJeff Mahoney 			goto out_end_trans;
2507d68fc57bSYan, Zheng 		}
250879787eaaSJeff Mahoney 	}
2509dd5f9615SStefan Behrens 
2510dd5f9615SStefan Behrens 	ret = btrfs_uuid_tree_rem(trans, root->fs_info->uuid_root,
2511dd5f9615SStefan Behrens 				  dest->root_item.uuid, BTRFS_UUID_KEY_SUBVOL,
2512dd5f9615SStefan Behrens 				  dest->root_key.objectid);
2513dd5f9615SStefan Behrens 	if (ret && ret != -ENOENT) {
2514dd5f9615SStefan Behrens 		btrfs_abort_transaction(trans, root, ret);
2515dd5f9615SStefan Behrens 		err = ret;
2516dd5f9615SStefan Behrens 		goto out_end_trans;
2517dd5f9615SStefan Behrens 	}
2518dd5f9615SStefan Behrens 	if (!btrfs_is_empty_uuid(dest->root_item.received_uuid)) {
2519dd5f9615SStefan Behrens 		ret = btrfs_uuid_tree_rem(trans, root->fs_info->uuid_root,
2520dd5f9615SStefan Behrens 					  dest->root_item.received_uuid,
2521dd5f9615SStefan Behrens 					  BTRFS_UUID_KEY_RECEIVED_SUBVOL,
2522dd5f9615SStefan Behrens 					  dest->root_key.objectid);
2523dd5f9615SStefan Behrens 		if (ret && ret != -ENOENT) {
2524dd5f9615SStefan Behrens 			btrfs_abort_transaction(trans, root, ret);
2525dd5f9615SStefan Behrens 			err = ret;
2526dd5f9615SStefan Behrens 			goto out_end_trans;
2527dd5f9615SStefan Behrens 		}
2528dd5f9615SStefan Behrens 	}
2529dd5f9615SStefan Behrens 
253079787eaaSJeff Mahoney out_end_trans:
2531c58aaad2SMiao Xie 	trans->block_rsv = NULL;
2532c58aaad2SMiao Xie 	trans->bytes_reserved = 0;
2533531cb13fSSage Weil 	ret = btrfs_end_transaction(trans, root);
253479787eaaSJeff Mahoney 	if (ret && !err)
253579787eaaSJeff Mahoney 		err = ret;
253676dda93cSYan, Zheng 	inode->i_flags |= S_DEAD;
2537c58aaad2SMiao Xie out_release:
2538c58aaad2SMiao Xie 	btrfs_subvolume_release_metadata(root, &block_rsv, qgroup_reserved);
253976dda93cSYan, Zheng out_up_write:
254076dda93cSYan, Zheng 	up_write(&root->fs_info->subvol_sem);
254176dda93cSYan, Zheng out_unlock:
2542521e0546SDavid Sterba 	if (err) {
2543521e0546SDavid Sterba 		spin_lock(&dest->root_item_lock);
2544c55bfa67SFilipe Manana 		root_flags = btrfs_root_flags(&dest->root_item);
2545c55bfa67SFilipe Manana 		btrfs_set_root_flags(&dest->root_item,
2546521e0546SDavid Sterba 				root_flags & ~BTRFS_ROOT_SUBVOL_DEAD);
2547521e0546SDavid Sterba 		spin_unlock(&dest->root_item_lock);
2548521e0546SDavid Sterba 	}
254976dda93cSYan, Zheng 	mutex_unlock(&inode->i_mutex);
255076dda93cSYan, Zheng 	if (!err) {
2551efefb143SYan, Zheng 		shrink_dcache_sb(root->fs_info->sb);
255276dda93cSYan, Zheng 		btrfs_invalidate_inodes(dest);
255376dda93cSYan, Zheng 		d_delete(dentry);
255461155aa0SDavid Sterba 		ASSERT(dest->send_in_progress == 0);
2555fa6ac876SLiu Bo 
2556fa6ac876SLiu Bo 		/* the last ref */
2557fa6ac876SLiu Bo 		if (dest->cache_inode) {
2558fa6ac876SLiu Bo 			iput(dest->cache_inode);
2559fa6ac876SLiu Bo 			dest->cache_inode = NULL;
2560fa6ac876SLiu Bo 		}
256176dda93cSYan, Zheng 	}
256276dda93cSYan, Zheng out_dput:
256376dda93cSYan, Zheng 	dput(dentry);
256476dda93cSYan, Zheng out_unlock_dir:
256576dda93cSYan, Zheng 	mutex_unlock(&dir->i_mutex);
2566e43f998eSDavid Sterba out_drop_write:
25672a79f17eSAl Viro 	mnt_drop_write_file(file);
256876dda93cSYan, Zheng out:
256976dda93cSYan, Zheng 	kfree(vol_args);
257076dda93cSYan, Zheng 	return err;
257176dda93cSYan, Zheng }
257276dda93cSYan, Zheng 
25731e701a32SChris Mason static int btrfs_ioctl_defrag(struct file *file, void __user *argp)
2574f46b5a66SChristoph Hellwig {
2575496ad9aaSAl Viro 	struct inode *inode = file_inode(file);
2576f46b5a66SChristoph Hellwig 	struct btrfs_root *root = BTRFS_I(inode)->root;
25771e701a32SChris Mason 	struct btrfs_ioctl_defrag_range_args *range;
2578c146afadSYan Zheng 	int ret;
2579c146afadSYan Zheng 
258025122d15SIlya Dryomov 	ret = mnt_want_write_file(file);
258125122d15SIlya Dryomov 	if (ret)
258225122d15SIlya Dryomov 		return ret;
2583b83cc969SLi Zefan 
258425122d15SIlya Dryomov 	if (btrfs_root_readonly(root)) {
258525122d15SIlya Dryomov 		ret = -EROFS;
258625122d15SIlya Dryomov 		goto out;
25875ac00addSStefan Behrens 	}
2588f46b5a66SChristoph Hellwig 
2589f46b5a66SChristoph Hellwig 	switch (inode->i_mode & S_IFMT) {
2590f46b5a66SChristoph Hellwig 	case S_IFDIR:
2591e441d54dSChris Mason 		if (!capable(CAP_SYS_ADMIN)) {
2592e441d54dSChris Mason 			ret = -EPERM;
2593e441d54dSChris Mason 			goto out;
2594e441d54dSChris Mason 		}
2595de78b51aSEric Sandeen 		ret = btrfs_defrag_root(root);
25968929ecfaSYan, Zheng 		if (ret)
25978929ecfaSYan, Zheng 			goto out;
2598de78b51aSEric Sandeen 		ret = btrfs_defrag_root(root->fs_info->extent_root);
2599f46b5a66SChristoph Hellwig 		break;
2600f46b5a66SChristoph Hellwig 	case S_IFREG:
2601e441d54dSChris Mason 		if (!(file->f_mode & FMODE_WRITE)) {
2602e441d54dSChris Mason 			ret = -EINVAL;
2603e441d54dSChris Mason 			goto out;
2604e441d54dSChris Mason 		}
26051e701a32SChris Mason 
26061e701a32SChris Mason 		range = kzalloc(sizeof(*range), GFP_KERNEL);
26071e701a32SChris Mason 		if (!range) {
26081e701a32SChris Mason 			ret = -ENOMEM;
26091e701a32SChris Mason 			goto out;
26101e701a32SChris Mason 		}
26111e701a32SChris Mason 
26121e701a32SChris Mason 		if (argp) {
26131e701a32SChris Mason 			if (copy_from_user(range, argp,
26141e701a32SChris Mason 					   sizeof(*range))) {
26151e701a32SChris Mason 				ret = -EFAULT;
26161e701a32SChris Mason 				kfree(range);
2617683be16eSDan Carpenter 				goto out;
26181e701a32SChris Mason 			}
26191e701a32SChris Mason 			/* compression requires us to start the IO */
26201e701a32SChris Mason 			if ((range->flags & BTRFS_DEFRAG_RANGE_COMPRESS)) {
26211e701a32SChris Mason 				range->flags |= BTRFS_DEFRAG_RANGE_START_IO;
26221e701a32SChris Mason 				range->extent_thresh = (u32)-1;
26231e701a32SChris Mason 			}
26241e701a32SChris Mason 		} else {
26251e701a32SChris Mason 			/* the rest are all set to zero by kzalloc */
26261e701a32SChris Mason 			range->len = (u64)-1;
26271e701a32SChris Mason 		}
2628496ad9aaSAl Viro 		ret = btrfs_defrag_file(file_inode(file), file,
26294cb5300bSChris Mason 					range, 0, 0);
26304cb5300bSChris Mason 		if (ret > 0)
26314cb5300bSChris Mason 			ret = 0;
26321e701a32SChris Mason 		kfree(range);
2633f46b5a66SChristoph Hellwig 		break;
26348929ecfaSYan, Zheng 	default:
26358929ecfaSYan, Zheng 		ret = -EINVAL;
2636f46b5a66SChristoph Hellwig 	}
2637e441d54dSChris Mason out:
263825122d15SIlya Dryomov 	mnt_drop_write_file(file);
2639e441d54dSChris Mason 	return ret;
2640f46b5a66SChristoph Hellwig }
2641f46b5a66SChristoph Hellwig 
2642b2950863SChristoph Hellwig static long btrfs_ioctl_add_dev(struct btrfs_root *root, void __user *arg)
2643f46b5a66SChristoph Hellwig {
2644f46b5a66SChristoph Hellwig 	struct btrfs_ioctl_vol_args *vol_args;
2645f46b5a66SChristoph Hellwig 	int ret;
2646f46b5a66SChristoph Hellwig 
2647e441d54dSChris Mason 	if (!capable(CAP_SYS_ADMIN))
2648e441d54dSChris Mason 		return -EPERM;
2649e441d54dSChris Mason 
26505ac00addSStefan Behrens 	if (atomic_xchg(&root->fs_info->mutually_exclusive_operation_running,
26515ac00addSStefan Behrens 			1)) {
2652e57138b3SAnand Jain 		return BTRFS_ERROR_DEV_EXCL_RUN_IN_PROGRESS;
2653c9e9f97bSIlya Dryomov 	}
2654c9e9f97bSIlya Dryomov 
26555ac00addSStefan Behrens 	mutex_lock(&root->fs_info->volume_mutex);
2656dae7b665SLi Zefan 	vol_args = memdup_user(arg, sizeof(*vol_args));
2657c9e9f97bSIlya Dryomov 	if (IS_ERR(vol_args)) {
2658c9e9f97bSIlya Dryomov 		ret = PTR_ERR(vol_args);
2659c9e9f97bSIlya Dryomov 		goto out;
2660c9e9f97bSIlya Dryomov 	}
2661f46b5a66SChristoph Hellwig 
26625516e595SMark Fasheh 	vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
2663f46b5a66SChristoph Hellwig 	ret = btrfs_init_new_device(root, vol_args->name);
2664f46b5a66SChristoph Hellwig 
2665f46b5a66SChristoph Hellwig 	kfree(vol_args);
2666c9e9f97bSIlya Dryomov out:
2667c9e9f97bSIlya Dryomov 	mutex_unlock(&root->fs_info->volume_mutex);
26685ac00addSStefan Behrens 	atomic_set(&root->fs_info->mutually_exclusive_operation_running, 0);
2669f46b5a66SChristoph Hellwig 	return ret;
2670f46b5a66SChristoph Hellwig }
2671f46b5a66SChristoph Hellwig 
2672da24927bSMiao Xie static long btrfs_ioctl_rm_dev(struct file *file, void __user *arg)
2673f46b5a66SChristoph Hellwig {
2674496ad9aaSAl Viro 	struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
2675f46b5a66SChristoph Hellwig 	struct btrfs_ioctl_vol_args *vol_args;
2676f46b5a66SChristoph Hellwig 	int ret;
2677f46b5a66SChristoph Hellwig 
2678e441d54dSChris Mason 	if (!capable(CAP_SYS_ADMIN))
2679e441d54dSChris Mason 		return -EPERM;
2680e441d54dSChris Mason 
2681da24927bSMiao Xie 	ret = mnt_want_write_file(file);
2682da24927bSMiao Xie 	if (ret)
2683da24927bSMiao Xie 		return ret;
2684c146afadSYan Zheng 
2685dae7b665SLi Zefan 	vol_args = memdup_user(arg, sizeof(*vol_args));
2686c9e9f97bSIlya Dryomov 	if (IS_ERR(vol_args)) {
2687c9e9f97bSIlya Dryomov 		ret = PTR_ERR(vol_args);
2688c9e9f97bSIlya Dryomov 		goto out;
2689c9e9f97bSIlya Dryomov 	}
2690f46b5a66SChristoph Hellwig 
26915516e595SMark Fasheh 	vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
2692f46b5a66SChristoph Hellwig 
2693183860f6SAnand Jain 	if (atomic_xchg(&root->fs_info->mutually_exclusive_operation_running,
2694183860f6SAnand Jain 			1)) {
2695183860f6SAnand Jain 		ret = BTRFS_ERROR_DEV_EXCL_RUN_IN_PROGRESS;
2696183860f6SAnand Jain 		goto out;
2697183860f6SAnand Jain 	}
2698183860f6SAnand Jain 
2699183860f6SAnand Jain 	mutex_lock(&root->fs_info->volume_mutex);
2700183860f6SAnand Jain 	ret = btrfs_rm_device(root, vol_args->name);
2701c9e9f97bSIlya Dryomov 	mutex_unlock(&root->fs_info->volume_mutex);
27025ac00addSStefan Behrens 	atomic_set(&root->fs_info->mutually_exclusive_operation_running, 0);
2703183860f6SAnand Jain 
2704183860f6SAnand Jain out:
2705183860f6SAnand Jain 	kfree(vol_args);
27064ac20c70SIlya Dryomov 	mnt_drop_write_file(file);
2707f46b5a66SChristoph Hellwig 	return ret;
2708f46b5a66SChristoph Hellwig }
2709f46b5a66SChristoph Hellwig 
2710475f6387SJan Schmidt static long btrfs_ioctl_fs_info(struct btrfs_root *root, void __user *arg)
2711475f6387SJan Schmidt {
2712027ed2f0SLi Zefan 	struct btrfs_ioctl_fs_info_args *fi_args;
2713475f6387SJan Schmidt 	struct btrfs_device *device;
2714475f6387SJan Schmidt 	struct btrfs_device *next;
2715475f6387SJan Schmidt 	struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices;
2716027ed2f0SLi Zefan 	int ret = 0;
2717475f6387SJan Schmidt 
2718027ed2f0SLi Zefan 	fi_args = kzalloc(sizeof(*fi_args), GFP_KERNEL);
2719027ed2f0SLi Zefan 	if (!fi_args)
2720027ed2f0SLi Zefan 		return -ENOMEM;
2721027ed2f0SLi Zefan 
2722f7171750SFilipe David Borba Manana 	mutex_lock(&fs_devices->device_list_mutex);
2723027ed2f0SLi Zefan 	fi_args->num_devices = fs_devices->num_devices;
2724027ed2f0SLi Zefan 	memcpy(&fi_args->fsid, root->fs_info->fsid, sizeof(fi_args->fsid));
2725475f6387SJan Schmidt 
2726475f6387SJan Schmidt 	list_for_each_entry_safe(device, next, &fs_devices->devices, dev_list) {
2727027ed2f0SLi Zefan 		if (device->devid > fi_args->max_id)
2728027ed2f0SLi Zefan 			fi_args->max_id = device->devid;
2729475f6387SJan Schmidt 	}
2730475f6387SJan Schmidt 	mutex_unlock(&fs_devices->device_list_mutex);
2731475f6387SJan Schmidt 
273280a773fbSDavid Sterba 	fi_args->nodesize = root->fs_info->super_copy->nodesize;
273380a773fbSDavid Sterba 	fi_args->sectorsize = root->fs_info->super_copy->sectorsize;
273480a773fbSDavid Sterba 	fi_args->clone_alignment = root->fs_info->super_copy->sectorsize;
273580a773fbSDavid Sterba 
2736027ed2f0SLi Zefan 	if (copy_to_user(arg, fi_args, sizeof(*fi_args)))
2737027ed2f0SLi Zefan 		ret = -EFAULT;
2738475f6387SJan Schmidt 
2739027ed2f0SLi Zefan 	kfree(fi_args);
2740027ed2f0SLi Zefan 	return ret;
2741475f6387SJan Schmidt }
2742475f6387SJan Schmidt 
2743475f6387SJan Schmidt static long btrfs_ioctl_dev_info(struct btrfs_root *root, void __user *arg)
2744475f6387SJan Schmidt {
2745475f6387SJan Schmidt 	struct btrfs_ioctl_dev_info_args *di_args;
2746475f6387SJan Schmidt 	struct btrfs_device *dev;
2747475f6387SJan Schmidt 	struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices;
2748475f6387SJan Schmidt 	int ret = 0;
2749475f6387SJan Schmidt 	char *s_uuid = NULL;
2750475f6387SJan Schmidt 
2751475f6387SJan Schmidt 	di_args = memdup_user(arg, sizeof(*di_args));
2752475f6387SJan Schmidt 	if (IS_ERR(di_args))
2753475f6387SJan Schmidt 		return PTR_ERR(di_args);
2754475f6387SJan Schmidt 
2755dd5f9615SStefan Behrens 	if (!btrfs_is_empty_uuid(di_args->uuid))
2756475f6387SJan Schmidt 		s_uuid = di_args->uuid;
2757475f6387SJan Schmidt 
2758475f6387SJan Schmidt 	mutex_lock(&fs_devices->device_list_mutex);
2759aa1b8cd4SStefan Behrens 	dev = btrfs_find_device(root->fs_info, di_args->devid, s_uuid, NULL);
2760475f6387SJan Schmidt 
2761475f6387SJan Schmidt 	if (!dev) {
2762475f6387SJan Schmidt 		ret = -ENODEV;
2763475f6387SJan Schmidt 		goto out;
2764475f6387SJan Schmidt 	}
2765475f6387SJan Schmidt 
2766475f6387SJan Schmidt 	di_args->devid = dev->devid;
2767475f6387SJan Schmidt 	di_args->bytes_used = dev->bytes_used;
2768475f6387SJan Schmidt 	di_args->total_bytes = dev->total_bytes;
2769475f6387SJan Schmidt 	memcpy(di_args->uuid, dev->uuid, sizeof(di_args->uuid));
2770a27202fbSJim Meyering 	if (dev->name) {
2771606686eeSJosef Bacik 		struct rcu_string *name;
2772606686eeSJosef Bacik 
2773606686eeSJosef Bacik 		rcu_read_lock();
2774606686eeSJosef Bacik 		name = rcu_dereference(dev->name);
2775606686eeSJosef Bacik 		strncpy(di_args->path, name->str, sizeof(di_args->path));
2776606686eeSJosef Bacik 		rcu_read_unlock();
2777a27202fbSJim Meyering 		di_args->path[sizeof(di_args->path) - 1] = 0;
2778a27202fbSJim Meyering 	} else {
277999ba55adSStefan Behrens 		di_args->path[0] = '\0';
2780a27202fbSJim Meyering 	}
2781475f6387SJan Schmidt 
2782475f6387SJan Schmidt out:
278355793c0dSDavid Sterba 	mutex_unlock(&fs_devices->device_list_mutex);
2784475f6387SJan Schmidt 	if (ret == 0 && copy_to_user(arg, di_args, sizeof(*di_args)))
2785475f6387SJan Schmidt 		ret = -EFAULT;
2786475f6387SJan Schmidt 
2787475f6387SJan Schmidt 	kfree(di_args);
2788475f6387SJan Schmidt 	return ret;
2789475f6387SJan Schmidt }
2790475f6387SJan Schmidt 
2791416161dbSMark Fasheh static struct page *extent_same_get_page(struct inode *inode, u64 off)
2792416161dbSMark Fasheh {
2793416161dbSMark Fasheh 	struct page *page;
2794416161dbSMark Fasheh 	pgoff_t index;
2795416161dbSMark Fasheh 	struct extent_io_tree *tree = &BTRFS_I(inode)->io_tree;
2796416161dbSMark Fasheh 
2797416161dbSMark Fasheh 	index = off >> PAGE_CACHE_SHIFT;
2798416161dbSMark Fasheh 
2799416161dbSMark Fasheh 	page = grab_cache_page(inode->i_mapping, index);
2800416161dbSMark Fasheh 	if (!page)
2801416161dbSMark Fasheh 		return NULL;
2802416161dbSMark Fasheh 
2803416161dbSMark Fasheh 	if (!PageUptodate(page)) {
2804416161dbSMark Fasheh 		if (extent_read_full_page_nolock(tree, page, btrfs_get_extent,
2805416161dbSMark Fasheh 						 0))
2806416161dbSMark Fasheh 			return NULL;
2807416161dbSMark Fasheh 		lock_page(page);
2808416161dbSMark Fasheh 		if (!PageUptodate(page)) {
2809416161dbSMark Fasheh 			unlock_page(page);
2810416161dbSMark Fasheh 			page_cache_release(page);
2811416161dbSMark Fasheh 			return NULL;
2812416161dbSMark Fasheh 		}
2813416161dbSMark Fasheh 	}
2814416161dbSMark Fasheh 	unlock_page(page);
2815416161dbSMark Fasheh 
2816416161dbSMark Fasheh 	return page;
2817416161dbSMark Fasheh }
2818416161dbSMark Fasheh 
281977fe20dcSMark Fasheh static inline void lock_extent_range(struct inode *inode, u64 off, u64 len)
282077fe20dcSMark Fasheh {
282177fe20dcSMark Fasheh 	/* do any pending delalloc/csum calc on src, one way or
282277fe20dcSMark Fasheh 	   another, and lock file content */
282377fe20dcSMark Fasheh 	while (1) {
282477fe20dcSMark Fasheh 		struct btrfs_ordered_extent *ordered;
282577fe20dcSMark Fasheh 		lock_extent(&BTRFS_I(inode)->io_tree, off, off + len - 1);
282677fe20dcSMark Fasheh 		ordered = btrfs_lookup_first_ordered_extent(inode,
282777fe20dcSMark Fasheh 							    off + len - 1);
2828ff5df9b8SFilipe Manana 		if ((!ordered ||
2829ff5df9b8SFilipe Manana 		     ordered->file_offset + ordered->len <= off ||
2830ff5df9b8SFilipe Manana 		     ordered->file_offset >= off + len) &&
283177fe20dcSMark Fasheh 		    !test_range_bit(&BTRFS_I(inode)->io_tree, off,
2832ff5df9b8SFilipe Manana 				    off + len - 1, EXTENT_DELALLOC, 0, NULL)) {
2833ff5df9b8SFilipe Manana 			if (ordered)
2834ff5df9b8SFilipe Manana 				btrfs_put_ordered_extent(ordered);
283577fe20dcSMark Fasheh 			break;
2836ff5df9b8SFilipe Manana 		}
283777fe20dcSMark Fasheh 		unlock_extent(&BTRFS_I(inode)->io_tree, off, off + len - 1);
283877fe20dcSMark Fasheh 		if (ordered)
283977fe20dcSMark Fasheh 			btrfs_put_ordered_extent(ordered);
284077fe20dcSMark Fasheh 		btrfs_wait_ordered_range(inode, off, len);
284177fe20dcSMark Fasheh 	}
284277fe20dcSMark Fasheh }
284377fe20dcSMark Fasheh 
2844416161dbSMark Fasheh static void btrfs_double_unlock(struct inode *inode1, u64 loff1,
2845416161dbSMark Fasheh 				struct inode *inode2, u64 loff2, u64 len)
2846416161dbSMark Fasheh {
2847416161dbSMark Fasheh 	unlock_extent(&BTRFS_I(inode1)->io_tree, loff1, loff1 + len - 1);
2848416161dbSMark Fasheh 	unlock_extent(&BTRFS_I(inode2)->io_tree, loff2, loff2 + len - 1);
2849416161dbSMark Fasheh 
2850416161dbSMark Fasheh 	mutex_unlock(&inode1->i_mutex);
2851416161dbSMark Fasheh 	mutex_unlock(&inode2->i_mutex);
2852416161dbSMark Fasheh }
2853416161dbSMark Fasheh 
2854416161dbSMark Fasheh static void btrfs_double_lock(struct inode *inode1, u64 loff1,
2855416161dbSMark Fasheh 			      struct inode *inode2, u64 loff2, u64 len)
2856416161dbSMark Fasheh {
2857416161dbSMark Fasheh 	if (inode1 < inode2) {
2858416161dbSMark Fasheh 		swap(inode1, inode2);
2859416161dbSMark Fasheh 		swap(loff1, loff2);
2860416161dbSMark Fasheh 	}
2861416161dbSMark Fasheh 
2862416161dbSMark Fasheh 	mutex_lock_nested(&inode1->i_mutex, I_MUTEX_PARENT);
2863416161dbSMark Fasheh 	lock_extent_range(inode1, loff1, len);
2864416161dbSMark Fasheh 	if (inode1 != inode2) {
2865416161dbSMark Fasheh 		mutex_lock_nested(&inode2->i_mutex, I_MUTEX_CHILD);
2866416161dbSMark Fasheh 		lock_extent_range(inode2, loff2, len);
2867416161dbSMark Fasheh 	}
2868416161dbSMark Fasheh }
2869416161dbSMark Fasheh 
2870416161dbSMark Fasheh static int btrfs_cmp_data(struct inode *src, u64 loff, struct inode *dst,
2871416161dbSMark Fasheh 			  u64 dst_loff, u64 len)
2872416161dbSMark Fasheh {
2873416161dbSMark Fasheh 	int ret = 0;
2874416161dbSMark Fasheh 	struct page *src_page, *dst_page;
2875416161dbSMark Fasheh 	unsigned int cmp_len = PAGE_CACHE_SIZE;
2876416161dbSMark Fasheh 	void *addr, *dst_addr;
2877416161dbSMark Fasheh 
2878416161dbSMark Fasheh 	while (len) {
2879416161dbSMark Fasheh 		if (len < PAGE_CACHE_SIZE)
2880416161dbSMark Fasheh 			cmp_len = len;
2881416161dbSMark Fasheh 
2882416161dbSMark Fasheh 		src_page = extent_same_get_page(src, loff);
2883416161dbSMark Fasheh 		if (!src_page)
2884416161dbSMark Fasheh 			return -EINVAL;
2885416161dbSMark Fasheh 		dst_page = extent_same_get_page(dst, dst_loff);
2886416161dbSMark Fasheh 		if (!dst_page) {
2887416161dbSMark Fasheh 			page_cache_release(src_page);
2888416161dbSMark Fasheh 			return -EINVAL;
2889416161dbSMark Fasheh 		}
2890416161dbSMark Fasheh 		addr = kmap_atomic(src_page);
2891416161dbSMark Fasheh 		dst_addr = kmap_atomic(dst_page);
2892416161dbSMark Fasheh 
2893416161dbSMark Fasheh 		flush_dcache_page(src_page);
2894416161dbSMark Fasheh 		flush_dcache_page(dst_page);
2895416161dbSMark Fasheh 
2896416161dbSMark Fasheh 		if (memcmp(addr, dst_addr, cmp_len))
2897416161dbSMark Fasheh 			ret = BTRFS_SAME_DATA_DIFFERS;
2898416161dbSMark Fasheh 
2899416161dbSMark Fasheh 		kunmap_atomic(addr);
2900416161dbSMark Fasheh 		kunmap_atomic(dst_addr);
2901416161dbSMark Fasheh 		page_cache_release(src_page);
2902416161dbSMark Fasheh 		page_cache_release(dst_page);
2903416161dbSMark Fasheh 
2904416161dbSMark Fasheh 		if (ret)
2905416161dbSMark Fasheh 			break;
2906416161dbSMark Fasheh 
2907416161dbSMark Fasheh 		loff += cmp_len;
2908416161dbSMark Fasheh 		dst_loff += cmp_len;
2909416161dbSMark Fasheh 		len -= cmp_len;
2910416161dbSMark Fasheh 	}
2911416161dbSMark Fasheh 
2912416161dbSMark Fasheh 	return ret;
2913416161dbSMark Fasheh }
2914416161dbSMark Fasheh 
2915416161dbSMark Fasheh static int extent_same_check_offsets(struct inode *inode, u64 off, u64 len)
2916416161dbSMark Fasheh {
2917416161dbSMark Fasheh 	u64 bs = BTRFS_I(inode)->root->fs_info->sb->s_blocksize;
2918416161dbSMark Fasheh 
2919416161dbSMark Fasheh 	if (off + len > inode->i_size || off + len < off)
2920416161dbSMark Fasheh 		return -EINVAL;
2921416161dbSMark Fasheh 	/* Check that we are block aligned - btrfs_clone() requires this */
2922416161dbSMark Fasheh 	if (!IS_ALIGNED(off, bs) || !IS_ALIGNED(off + len, bs))
2923416161dbSMark Fasheh 		return -EINVAL;
2924416161dbSMark Fasheh 
2925416161dbSMark Fasheh 	return 0;
2926416161dbSMark Fasheh }
2927416161dbSMark Fasheh 
2928416161dbSMark Fasheh static int btrfs_extent_same(struct inode *src, u64 loff, u64 len,
2929416161dbSMark Fasheh 			     struct inode *dst, u64 dst_loff)
2930416161dbSMark Fasheh {
2931416161dbSMark Fasheh 	int ret;
2932416161dbSMark Fasheh 
2933416161dbSMark Fasheh 	/*
2934416161dbSMark Fasheh 	 * btrfs_clone() can't handle extents in the same file
2935416161dbSMark Fasheh 	 * yet. Once that works, we can drop this check and replace it
2936416161dbSMark Fasheh 	 * with a check for the same inode, but overlapping extents.
2937416161dbSMark Fasheh 	 */
2938416161dbSMark Fasheh 	if (src == dst)
2939416161dbSMark Fasheh 		return -EINVAL;
2940416161dbSMark Fasheh 
2941416161dbSMark Fasheh 	btrfs_double_lock(src, loff, dst, dst_loff, len);
2942416161dbSMark Fasheh 
2943416161dbSMark Fasheh 	ret = extent_same_check_offsets(src, loff, len);
2944416161dbSMark Fasheh 	if (ret)
2945416161dbSMark Fasheh 		goto out_unlock;
2946416161dbSMark Fasheh 
2947416161dbSMark Fasheh 	ret = extent_same_check_offsets(dst, dst_loff, len);
2948416161dbSMark Fasheh 	if (ret)
2949416161dbSMark Fasheh 		goto out_unlock;
2950416161dbSMark Fasheh 
2951416161dbSMark Fasheh 	/* don't make the dst file partly checksummed */
2952416161dbSMark Fasheh 	if ((BTRFS_I(src)->flags & BTRFS_INODE_NODATASUM) !=
2953416161dbSMark Fasheh 	    (BTRFS_I(dst)->flags & BTRFS_INODE_NODATASUM)) {
2954416161dbSMark Fasheh 		ret = -EINVAL;
2955416161dbSMark Fasheh 		goto out_unlock;
2956416161dbSMark Fasheh 	}
2957416161dbSMark Fasheh 
2958416161dbSMark Fasheh 	ret = btrfs_cmp_data(src, loff, dst, dst_loff, len);
2959416161dbSMark Fasheh 	if (ret == 0)
2960416161dbSMark Fasheh 		ret = btrfs_clone(src, dst, loff, len, len, dst_loff);
2961416161dbSMark Fasheh 
2962416161dbSMark Fasheh out_unlock:
2963416161dbSMark Fasheh 	btrfs_double_unlock(src, loff, dst, dst_loff, len);
2964416161dbSMark Fasheh 
2965416161dbSMark Fasheh 	return ret;
2966416161dbSMark Fasheh }
2967416161dbSMark Fasheh 
2968416161dbSMark Fasheh #define BTRFS_MAX_DEDUPE_LEN	(16 * 1024 * 1024)
2969416161dbSMark Fasheh 
2970416161dbSMark Fasheh static long btrfs_ioctl_file_extent_same(struct file *file,
29711c1c8747SAl Viro 			struct btrfs_ioctl_same_args __user *argp)
2972416161dbSMark Fasheh {
2973cbf8b8caSMark Fasheh 	struct btrfs_ioctl_same_args *same;
2974cbf8b8caSMark Fasheh 	struct btrfs_ioctl_same_extent_info *info;
29751c1c8747SAl Viro 	struct inode *src = file_inode(file);
2976416161dbSMark Fasheh 	u64 off;
2977416161dbSMark Fasheh 	u64 len;
2978416161dbSMark Fasheh 	int i;
2979416161dbSMark Fasheh 	int ret;
2980cbf8b8caSMark Fasheh 	unsigned long size;
2981416161dbSMark Fasheh 	u64 bs = BTRFS_I(src)->root->fs_info->sb->s_blocksize;
2982416161dbSMark Fasheh 	bool is_admin = capable(CAP_SYS_ADMIN);
29831c1c8747SAl Viro 	u16 count;
2984416161dbSMark Fasheh 
2985416161dbSMark Fasheh 	if (!(file->f_mode & FMODE_READ))
2986416161dbSMark Fasheh 		return -EINVAL;
2987416161dbSMark Fasheh 
2988416161dbSMark Fasheh 	ret = mnt_want_write_file(file);
2989416161dbSMark Fasheh 	if (ret)
2990416161dbSMark Fasheh 		return ret;
2991416161dbSMark Fasheh 
29921c1c8747SAl Viro 	if (get_user(count, &argp->dest_count)) {
2993416161dbSMark Fasheh 		ret = -EFAULT;
2994416161dbSMark Fasheh 		goto out;
2995416161dbSMark Fasheh 	}
2996416161dbSMark Fasheh 
29971c1c8747SAl Viro 	size = offsetof(struct btrfs_ioctl_same_args __user, info[count]);
2998cbf8b8caSMark Fasheh 
29991c1c8747SAl Viro 	same = memdup_user(argp, size);
3000cbf8b8caSMark Fasheh 
3001229eed43SGeyslan G. Bem 	if (IS_ERR(same)) {
3002229eed43SGeyslan G. Bem 		ret = PTR_ERR(same);
3003cbf8b8caSMark Fasheh 		goto out;
3004cbf8b8caSMark Fasheh 	}
3005cbf8b8caSMark Fasheh 
3006cbf8b8caSMark Fasheh 	off = same->logical_offset;
3007cbf8b8caSMark Fasheh 	len = same->length;
3008416161dbSMark Fasheh 
3009416161dbSMark Fasheh 	/*
3010416161dbSMark Fasheh 	 * Limit the total length we will dedupe for each operation.
3011416161dbSMark Fasheh 	 * This is intended to bound the total time spent in this
3012416161dbSMark Fasheh 	 * ioctl to something sane.
3013416161dbSMark Fasheh 	 */
3014416161dbSMark Fasheh 	if (len > BTRFS_MAX_DEDUPE_LEN)
3015416161dbSMark Fasheh 		len = BTRFS_MAX_DEDUPE_LEN;
3016416161dbSMark Fasheh 
3017416161dbSMark Fasheh 	if (WARN_ON_ONCE(bs < PAGE_CACHE_SIZE)) {
3018416161dbSMark Fasheh 		/*
3019416161dbSMark Fasheh 		 * Btrfs does not support blocksize < page_size. As a
3020416161dbSMark Fasheh 		 * result, btrfs_cmp_data() won't correctly handle
3021416161dbSMark Fasheh 		 * this situation without an update.
3022416161dbSMark Fasheh 		 */
3023416161dbSMark Fasheh 		ret = -EINVAL;
3024416161dbSMark Fasheh 		goto out;
3025416161dbSMark Fasheh 	}
3026416161dbSMark Fasheh 
3027416161dbSMark Fasheh 	ret = -EISDIR;
3028416161dbSMark Fasheh 	if (S_ISDIR(src->i_mode))
3029416161dbSMark Fasheh 		goto out;
3030416161dbSMark Fasheh 
3031416161dbSMark Fasheh 	ret = -EACCES;
3032416161dbSMark Fasheh 	if (!S_ISREG(src->i_mode))
3033416161dbSMark Fasheh 		goto out;
3034416161dbSMark Fasheh 
3035cbf8b8caSMark Fasheh 	/* pre-format output fields to sane values */
30361c1c8747SAl Viro 	for (i = 0; i < count; i++) {
3037cbf8b8caSMark Fasheh 		same->info[i].bytes_deduped = 0ULL;
3038cbf8b8caSMark Fasheh 		same->info[i].status = 0;
3039416161dbSMark Fasheh 	}
3040416161dbSMark Fasheh 
30411c1c8747SAl Viro 	for (i = 0, info = same->info; i < count; i++, info++) {
30421c1c8747SAl Viro 		struct inode *dst;
30431c1c8747SAl Viro 		struct fd dst_file = fdget(info->fd);
30441c1c8747SAl Viro 		if (!dst_file.file) {
3045cbf8b8caSMark Fasheh 			info->status = -EBADF;
30461c1c8747SAl Viro 			continue;
3047416161dbSMark Fasheh 		}
30481c1c8747SAl Viro 		dst = file_inode(dst_file.file);
3049416161dbSMark Fasheh 
30501c1c8747SAl Viro 		if (!(is_admin || (dst_file.file->f_mode & FMODE_WRITE))) {
3051cbf8b8caSMark Fasheh 			info->status = -EINVAL;
30521c1c8747SAl Viro 		} else if (file->f_path.mnt != dst_file.file->f_path.mnt) {
3053cbf8b8caSMark Fasheh 			info->status = -EXDEV;
30541c1c8747SAl Viro 		} else if (S_ISDIR(dst->i_mode)) {
3055cbf8b8caSMark Fasheh 			info->status = -EISDIR;
30561c1c8747SAl Viro 		} else if (!S_ISREG(dst->i_mode)) {
3057cbf8b8caSMark Fasheh 			info->status = -EACCES;
30581c1c8747SAl Viro 		} else {
3059cbf8b8caSMark Fasheh 			info->status = btrfs_extent_same(src, off, len, dst,
3060cbf8b8caSMark Fasheh 							info->logical_offset);
3061cbf8b8caSMark Fasheh 			if (info->status == 0)
3062cbf8b8caSMark Fasheh 				info->bytes_deduped += len;
30631c1c8747SAl Viro 		}
30641c1c8747SAl Viro 		fdput(dst_file);
3065cbf8b8caSMark Fasheh 	}
3066416161dbSMark Fasheh 
3067cbf8b8caSMark Fasheh 	ret = copy_to_user(argp, same, size);
3068cbf8b8caSMark Fasheh 	if (ret)
3069416161dbSMark Fasheh 		ret = -EFAULT;
3070416161dbSMark Fasheh 
3071416161dbSMark Fasheh out:
3072416161dbSMark Fasheh 	mnt_drop_write_file(file);
3073416161dbSMark Fasheh 	return ret;
3074416161dbSMark Fasheh }
3075416161dbSMark Fasheh 
3076fcebe456SJosef Bacik /* Helper to check and see if this root currently has a ref on the given disk
3077fcebe456SJosef Bacik  * bytenr.  If it does then we need to update the quota for this root.  This
3078fcebe456SJosef Bacik  * doesn't do anything if quotas aren't enabled.
3079fcebe456SJosef Bacik  */
3080fcebe456SJosef Bacik static int check_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root,
3081fcebe456SJosef Bacik 		     u64 disko)
3082fcebe456SJosef Bacik {
3083fcebe456SJosef Bacik 	struct seq_list tree_mod_seq_elem = {};
3084fcebe456SJosef Bacik 	struct ulist *roots;
3085fcebe456SJosef Bacik 	struct ulist_iterator uiter;
3086fcebe456SJosef Bacik 	struct ulist_node *root_node = NULL;
3087fcebe456SJosef Bacik 	int ret;
3088fcebe456SJosef Bacik 
3089fcebe456SJosef Bacik 	if (!root->fs_info->quota_enabled)
3090fcebe456SJosef Bacik 		return 1;
3091fcebe456SJosef Bacik 
3092fcebe456SJosef Bacik 	btrfs_get_tree_mod_seq(root->fs_info, &tree_mod_seq_elem);
3093fcebe456SJosef Bacik 	ret = btrfs_find_all_roots(trans, root->fs_info, disko,
3094fcebe456SJosef Bacik 				   tree_mod_seq_elem.seq, &roots);
3095fcebe456SJosef Bacik 	if (ret < 0)
3096fcebe456SJosef Bacik 		goto out;
3097fcebe456SJosef Bacik 	ret = 0;
3098fcebe456SJosef Bacik 	ULIST_ITER_INIT(&uiter);
3099fcebe456SJosef Bacik 	while ((root_node = ulist_next(roots, &uiter))) {
3100fcebe456SJosef Bacik 		if (root_node->val == root->objectid) {
3101fcebe456SJosef Bacik 			ret = 1;
3102fcebe456SJosef Bacik 			break;
3103fcebe456SJosef Bacik 		}
3104fcebe456SJosef Bacik 	}
3105fcebe456SJosef Bacik 	ulist_free(roots);
3106fcebe456SJosef Bacik out:
3107fcebe456SJosef Bacik 	btrfs_put_tree_mod_seq(root->fs_info, &tree_mod_seq_elem);
3108fcebe456SJosef Bacik 	return ret;
3109fcebe456SJosef Bacik }
3110fcebe456SJosef Bacik 
3111f82a9901SFilipe Manana static int clone_finish_inode_update(struct btrfs_trans_handle *trans,
3112f82a9901SFilipe Manana 				     struct inode *inode,
3113f82a9901SFilipe Manana 				     u64 endoff,
3114f82a9901SFilipe Manana 				     const u64 destoff,
3115f82a9901SFilipe Manana 				     const u64 olen)
3116f82a9901SFilipe Manana {
3117f82a9901SFilipe Manana 	struct btrfs_root *root = BTRFS_I(inode)->root;
3118f82a9901SFilipe Manana 	int ret;
3119f82a9901SFilipe Manana 
3120f82a9901SFilipe Manana 	inode_inc_iversion(inode);
3121f82a9901SFilipe Manana 	inode->i_mtime = inode->i_ctime = CURRENT_TIME;
3122f82a9901SFilipe Manana 	/*
3123f82a9901SFilipe Manana 	 * We round up to the block size at eof when determining which
3124f82a9901SFilipe Manana 	 * extents to clone above, but shouldn't round up the file size.
3125f82a9901SFilipe Manana 	 */
3126f82a9901SFilipe Manana 	if (endoff > destoff + olen)
3127f82a9901SFilipe Manana 		endoff = destoff + olen;
3128f82a9901SFilipe Manana 	if (endoff > inode->i_size)
3129f82a9901SFilipe Manana 		btrfs_i_size_write(inode, endoff);
3130f82a9901SFilipe Manana 
3131f82a9901SFilipe Manana 	ret = btrfs_update_inode(trans, root, inode);
3132f82a9901SFilipe Manana 	if (ret) {
3133f82a9901SFilipe Manana 		btrfs_abort_transaction(trans, root, ret);
3134f82a9901SFilipe Manana 		btrfs_end_transaction(trans, root);
3135f82a9901SFilipe Manana 		goto out;
3136f82a9901SFilipe Manana 	}
3137f82a9901SFilipe Manana 	ret = btrfs_end_transaction(trans, root);
3138f82a9901SFilipe Manana out:
3139f82a9901SFilipe Manana 	return ret;
3140f82a9901SFilipe Manana }
3141f82a9901SFilipe Manana 
31427ffbb598SFilipe Manana static void clone_update_extent_map(struct inode *inode,
31437ffbb598SFilipe Manana 				    const struct btrfs_trans_handle *trans,
31447ffbb598SFilipe Manana 				    const struct btrfs_path *path,
31457ffbb598SFilipe Manana 				    struct btrfs_file_extent_item *fi,
31467ffbb598SFilipe Manana 				    const u64 hole_offset,
31477ffbb598SFilipe Manana 				    const u64 hole_len)
31487ffbb598SFilipe Manana {
31497ffbb598SFilipe Manana 	struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
31507ffbb598SFilipe Manana 	struct extent_map *em;
31517ffbb598SFilipe Manana 	int ret;
31527ffbb598SFilipe Manana 
31537ffbb598SFilipe Manana 	em = alloc_extent_map();
31547ffbb598SFilipe Manana 	if (!em) {
31557ffbb598SFilipe Manana 		set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
31567ffbb598SFilipe Manana 			&BTRFS_I(inode)->runtime_flags);
31577ffbb598SFilipe Manana 		return;
31587ffbb598SFilipe Manana 	}
31597ffbb598SFilipe Manana 
31607ffbb598SFilipe Manana 	if (fi) {
31617ffbb598SFilipe Manana 		btrfs_extent_item_to_extent_map(inode, path, fi, false, em);
31627ffbb598SFilipe Manana 		em->generation = -1;
31637ffbb598SFilipe Manana 		if (btrfs_file_extent_type(path->nodes[0], fi) ==
31647ffbb598SFilipe Manana 		    BTRFS_FILE_EXTENT_INLINE)
31657ffbb598SFilipe Manana 			set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
31667ffbb598SFilipe Manana 				&BTRFS_I(inode)->runtime_flags);
31677ffbb598SFilipe Manana 	} else {
31687ffbb598SFilipe Manana 		em->start = hole_offset;
31697ffbb598SFilipe Manana 		em->len = hole_len;
31707ffbb598SFilipe Manana 		em->ram_bytes = em->len;
31717ffbb598SFilipe Manana 		em->orig_start = hole_offset;
31727ffbb598SFilipe Manana 		em->block_start = EXTENT_MAP_HOLE;
31737ffbb598SFilipe Manana 		em->block_len = 0;
31747ffbb598SFilipe Manana 		em->orig_block_len = 0;
31757ffbb598SFilipe Manana 		em->compress_type = BTRFS_COMPRESS_NONE;
31767ffbb598SFilipe Manana 		em->generation = trans->transid;
31777ffbb598SFilipe Manana 	}
31787ffbb598SFilipe Manana 
31797ffbb598SFilipe Manana 	while (1) {
31807ffbb598SFilipe Manana 		write_lock(&em_tree->lock);
31817ffbb598SFilipe Manana 		ret = add_extent_mapping(em_tree, em, 1);
31827ffbb598SFilipe Manana 		write_unlock(&em_tree->lock);
31837ffbb598SFilipe Manana 		if (ret != -EEXIST) {
31847ffbb598SFilipe Manana 			free_extent_map(em);
31857ffbb598SFilipe Manana 			break;
31867ffbb598SFilipe Manana 		}
31877ffbb598SFilipe Manana 		btrfs_drop_extent_cache(inode, em->start,
31887ffbb598SFilipe Manana 					em->start + em->len - 1, 0);
31897ffbb598SFilipe Manana 	}
31907ffbb598SFilipe Manana 
31917ffbb598SFilipe Manana 	if (unlikely(ret))
31927ffbb598SFilipe Manana 		set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
31937ffbb598SFilipe Manana 			&BTRFS_I(inode)->runtime_flags);
31947ffbb598SFilipe Manana }
31957ffbb598SFilipe Manana 
319632b7c687SMark Fasheh /**
319732b7c687SMark Fasheh  * btrfs_clone() - clone a range from inode file to another
319832b7c687SMark Fasheh  *
319932b7c687SMark Fasheh  * @src: Inode to clone from
320032b7c687SMark Fasheh  * @inode: Inode to clone to
320132b7c687SMark Fasheh  * @off: Offset within source to start clone from
320232b7c687SMark Fasheh  * @olen: Original length, passed by user, of range to clone
320332b7c687SMark Fasheh  * @olen_aligned: Block-aligned value of olen, extent_same uses
320432b7c687SMark Fasheh  *               identical values here
320532b7c687SMark Fasheh  * @destoff: Offset within @inode to start clone
320632b7c687SMark Fasheh  */
320732b7c687SMark Fasheh static int btrfs_clone(struct inode *src, struct inode *inode,
3208f82a9901SFilipe Manana 		       const u64 off, const u64 olen, const u64 olen_aligned,
3209f82a9901SFilipe Manana 		       const u64 destoff)
3210f46b5a66SChristoph Hellwig {
3211f46b5a66SChristoph Hellwig 	struct btrfs_root *root = BTRFS_I(inode)->root;
321232b7c687SMark Fasheh 	struct btrfs_path *path = NULL;
3213f46b5a66SChristoph Hellwig 	struct extent_buffer *leaf;
321432b7c687SMark Fasheh 	struct btrfs_trans_handle *trans;
321532b7c687SMark Fasheh 	char *buf = NULL;
3216ae01a0abSYan Zheng 	struct btrfs_key key;
3217f46b5a66SChristoph Hellwig 	u32 nritems;
3218f46b5a66SChristoph Hellwig 	int slot;
3219ae01a0abSYan Zheng 	int ret;
3220fcebe456SJosef Bacik 	int no_quota;
3221f82a9901SFilipe Manana 	const u64 len = olen_aligned;
3222fcebe456SJosef Bacik 	u64 last_disko = 0;
3223f82a9901SFilipe Manana 	u64 last_dest_end = destoff;
3224ae01a0abSYan Zheng 
3225ae01a0abSYan Zheng 	ret = -ENOMEM;
3226ae01a0abSYan Zheng 	buf = vmalloc(btrfs_level_size(root, 0));
3227ae01a0abSYan Zheng 	if (!buf)
322832b7c687SMark Fasheh 		return ret;
3229ae01a0abSYan Zheng 
3230ae01a0abSYan Zheng 	path = btrfs_alloc_path();
3231ae01a0abSYan Zheng 	if (!path) {
3232ae01a0abSYan Zheng 		vfree(buf);
323332b7c687SMark Fasheh 		return ret;
3234ae01a0abSYan Zheng 	}
323532b7c687SMark Fasheh 
3236ae01a0abSYan Zheng 	path->reada = 2;
3237c5c9cd4dSSage Weil 	/* clone data */
323833345d01SLi Zefan 	key.objectid = btrfs_ino(src);
3239ae01a0abSYan Zheng 	key.type = BTRFS_EXTENT_DATA_KEY;
32402c463823SFilipe Manana 	key.offset = off;
3241f46b5a66SChristoph Hellwig 
3242f46b5a66SChristoph Hellwig 	while (1) {
3243f46b5a66SChristoph Hellwig 		/*
3244f46b5a66SChristoph Hellwig 		 * note the key will change type as we walk through the
3245f46b5a66SChristoph Hellwig 		 * tree.
3246f46b5a66SChristoph Hellwig 		 */
3247e4355f34SFilipe David Borba Manana 		path->leave_spinning = 1;
3248362a20c5SDavid Sterba 		ret = btrfs_search_slot(NULL, BTRFS_I(src)->root, &key, path,
3249362a20c5SDavid Sterba 				0, 0);
3250f46b5a66SChristoph Hellwig 		if (ret < 0)
3251f46b5a66SChristoph Hellwig 			goto out;
32522c463823SFilipe Manana 		/*
32532c463823SFilipe Manana 		 * First search, if no extent item that starts at offset off was
32542c463823SFilipe Manana 		 * found but the previous item is an extent item, it's possible
32552c463823SFilipe Manana 		 * it might overlap our target range, therefore process it.
32562c463823SFilipe Manana 		 */
32572c463823SFilipe Manana 		if (key.offset == off && ret > 0 && path->slots[0] > 0) {
32582c463823SFilipe Manana 			btrfs_item_key_to_cpu(path->nodes[0], &key,
32592c463823SFilipe Manana 					      path->slots[0] - 1);
32602c463823SFilipe Manana 			if (key.type == BTRFS_EXTENT_DATA_KEY)
32612c463823SFilipe Manana 				path->slots[0]--;
32622c463823SFilipe Manana 		}
3263f46b5a66SChristoph Hellwig 
3264ae01a0abSYan Zheng 		nritems = btrfs_header_nritems(path->nodes[0]);
3265e4355f34SFilipe David Borba Manana process_slot:
3266fcebe456SJosef Bacik 		no_quota = 1;
3267ae01a0abSYan Zheng 		if (path->slots[0] >= nritems) {
3268362a20c5SDavid Sterba 			ret = btrfs_next_leaf(BTRFS_I(src)->root, path);
3269f46b5a66SChristoph Hellwig 			if (ret < 0)
3270f46b5a66SChristoph Hellwig 				goto out;
3271f46b5a66SChristoph Hellwig 			if (ret > 0)
3272f46b5a66SChristoph Hellwig 				break;
3273ae01a0abSYan Zheng 			nritems = btrfs_header_nritems(path->nodes[0]);
3274f46b5a66SChristoph Hellwig 		}
3275f46b5a66SChristoph Hellwig 		leaf = path->nodes[0];
3276f46b5a66SChristoph Hellwig 		slot = path->slots[0];
3277f46b5a66SChristoph Hellwig 
3278ae01a0abSYan Zheng 		btrfs_item_key_to_cpu(leaf, &key, slot);
3279d20f7043SChris Mason 		if (btrfs_key_type(&key) > BTRFS_EXTENT_DATA_KEY ||
328033345d01SLi Zefan 		    key.objectid != btrfs_ino(src))
3281f46b5a66SChristoph Hellwig 			break;
3282f46b5a66SChristoph Hellwig 
3283c5c9cd4dSSage Weil 		if (btrfs_key_type(&key) == BTRFS_EXTENT_DATA_KEY) {
3284c5c9cd4dSSage Weil 			struct btrfs_file_extent_item *extent;
3285c5c9cd4dSSage Weil 			int type;
328631840ae1SZheng Yan 			u32 size;
328731840ae1SZheng Yan 			struct btrfs_key new_key;
3288c5c9cd4dSSage Weil 			u64 disko = 0, diskl = 0;
3289c5c9cd4dSSage Weil 			u64 datao = 0, datal = 0;
3290c5c9cd4dSSage Weil 			u8 comp;
3291f82a9901SFilipe Manana 			u64 drop_start;
329231840ae1SZheng Yan 
3293c5c9cd4dSSage Weil 			extent = btrfs_item_ptr(leaf, slot,
3294c5c9cd4dSSage Weil 						struct btrfs_file_extent_item);
3295c5c9cd4dSSage Weil 			comp = btrfs_file_extent_compression(leaf, extent);
3296c5c9cd4dSSage Weil 			type = btrfs_file_extent_type(leaf, extent);
3297c8a894d7SChris Mason 			if (type == BTRFS_FILE_EXTENT_REG ||
3298c8a894d7SChris Mason 			    type == BTRFS_FILE_EXTENT_PREALLOC) {
3299d397712bSChris Mason 				disko = btrfs_file_extent_disk_bytenr(leaf,
3300d397712bSChris Mason 								      extent);
3301d397712bSChris Mason 				diskl = btrfs_file_extent_disk_num_bytes(leaf,
3302d397712bSChris Mason 								 extent);
3303c5c9cd4dSSage Weil 				datao = btrfs_file_extent_offset(leaf, extent);
3304d397712bSChris Mason 				datal = btrfs_file_extent_num_bytes(leaf,
3305d397712bSChris Mason 								    extent);
3306c5c9cd4dSSage Weil 			} else if (type == BTRFS_FILE_EXTENT_INLINE) {
3307c5c9cd4dSSage Weil 				/* take upper bound, may be compressed */
3308c5c9cd4dSSage Weil 				datal = btrfs_file_extent_ram_bytes(leaf,
3309c5c9cd4dSSage Weil 								    extent);
3310c5c9cd4dSSage Weil 			}
331131840ae1SZheng Yan 
33122c463823SFilipe Manana 			/*
33132c463823SFilipe Manana 			 * The first search might have left us at an extent
33142c463823SFilipe Manana 			 * item that ends before our target range's start, can
33152c463823SFilipe Manana 			 * happen if we have holes and NO_HOLES feature enabled.
33162c463823SFilipe Manana 			 */
33172c463823SFilipe Manana 			if (key.offset + datal <= off) {
3318e4355f34SFilipe David Borba Manana 				path->slots[0]++;
3319e4355f34SFilipe David Borba Manana 				goto process_slot;
33202c463823SFilipe Manana 			} else if (key.offset >= off + len) {
33212c463823SFilipe Manana 				break;
3322e4355f34SFilipe David Borba Manana 			}
3323e4355f34SFilipe David Borba Manana 
3324e4355f34SFilipe David Borba Manana 			size = btrfs_item_size_nr(leaf, slot);
3325e4355f34SFilipe David Borba Manana 			read_extent_buffer(leaf, buf,
3326e4355f34SFilipe David Borba Manana 					   btrfs_item_ptr_offset(leaf, slot),
3327e4355f34SFilipe David Borba Manana 					   size);
3328e4355f34SFilipe David Borba Manana 
3329e4355f34SFilipe David Borba Manana 			btrfs_release_path(path);
3330e4355f34SFilipe David Borba Manana 			path->leave_spinning = 0;
3331c5c9cd4dSSage Weil 
333231840ae1SZheng Yan 			memcpy(&new_key, &key, sizeof(new_key));
333333345d01SLi Zefan 			new_key.objectid = btrfs_ino(inode);
33344d728ec7SLi Zefan 			if (off <= key.offset)
3335c5c9cd4dSSage Weil 				new_key.offset = key.offset + destoff - off;
33364d728ec7SLi Zefan 			else
33374d728ec7SLi Zefan 				new_key.offset = destoff;
3338c5c9cd4dSSage Weil 
3339b6f3409bSSage Weil 			/*
3340f82a9901SFilipe Manana 			 * Deal with a hole that doesn't have an extent item
3341f82a9901SFilipe Manana 			 * that represents it (NO_HOLES feature enabled).
3342f82a9901SFilipe Manana 			 * This hole is either in the middle of the cloning
3343f82a9901SFilipe Manana 			 * range or at the beginning (fully overlaps it or
3344f82a9901SFilipe Manana 			 * partially overlaps it).
3345f82a9901SFilipe Manana 			 */
3346f82a9901SFilipe Manana 			if (new_key.offset != last_dest_end)
3347f82a9901SFilipe Manana 				drop_start = last_dest_end;
3348f82a9901SFilipe Manana 			else
3349f82a9901SFilipe Manana 				drop_start = new_key.offset;
3350f82a9901SFilipe Manana 
3351f82a9901SFilipe Manana 			/*
3352b6f3409bSSage Weil 			 * 1 - adjusting old extent (we may have to split it)
3353b6f3409bSSage Weil 			 * 1 - add new extent
3354b6f3409bSSage Weil 			 * 1 - inode update
3355b6f3409bSSage Weil 			 */
3356b6f3409bSSage Weil 			trans = btrfs_start_transaction(root, 3);
3357a22285a6SYan, Zheng 			if (IS_ERR(trans)) {
3358a22285a6SYan, Zheng 				ret = PTR_ERR(trans);
3359a22285a6SYan, Zheng 				goto out;
3360a22285a6SYan, Zheng 			}
3361a22285a6SYan, Zheng 
3362c8a894d7SChris Mason 			if (type == BTRFS_FILE_EXTENT_REG ||
3363c8a894d7SChris Mason 			    type == BTRFS_FILE_EXTENT_PREALLOC) {
3364d72c0842SLi Zefan 				/*
3365d72c0842SLi Zefan 				 *    a  | --- range to clone ---|  b
3366d72c0842SLi Zefan 				 * | ------------- extent ------------- |
3367d72c0842SLi Zefan 				 */
3368d72c0842SLi Zefan 
336993915584SAntonio Ospite 				/* subtract range b */
3370d72c0842SLi Zefan 				if (key.offset + datal > off + len)
3371d72c0842SLi Zefan 					datal = off + len - key.offset;
3372d72c0842SLi Zefan 
337393915584SAntonio Ospite 				/* subtract range a */
3374a22285a6SYan, Zheng 				if (off > key.offset) {
3375a22285a6SYan, Zheng 					datao += off - key.offset;
3376a22285a6SYan, Zheng 					datal -= off - key.offset;
3377a22285a6SYan, Zheng 				}
3378a22285a6SYan, Zheng 
33795dc562c5SJosef Bacik 				ret = btrfs_drop_extents(trans, root, inode,
3380f82a9901SFilipe Manana 							 drop_start,
3381a22285a6SYan, Zheng 							 new_key.offset + datal,
33822671485dSJosef Bacik 							 1);
338379787eaaSJeff Mahoney 				if (ret) {
33843f9e3df8SDavid Sterba 					if (ret != -EOPNOTSUPP)
338500fdf13aSLiu Bo 						btrfs_abort_transaction(trans,
338600fdf13aSLiu Bo 								root, ret);
338779787eaaSJeff Mahoney 					btrfs_end_transaction(trans, root);
338879787eaaSJeff Mahoney 					goto out;
338979787eaaSJeff Mahoney 				}
3390a22285a6SYan, Zheng 
339131840ae1SZheng Yan 				ret = btrfs_insert_empty_item(trans, root, path,
339231840ae1SZheng Yan 							      &new_key, size);
339379787eaaSJeff Mahoney 				if (ret) {
339479787eaaSJeff Mahoney 					btrfs_abort_transaction(trans, root,
339579787eaaSJeff Mahoney 								ret);
339679787eaaSJeff Mahoney 					btrfs_end_transaction(trans, root);
339779787eaaSJeff Mahoney 					goto out;
339879787eaaSJeff Mahoney 				}
339931840ae1SZheng Yan 
340031840ae1SZheng Yan 				leaf = path->nodes[0];
340131840ae1SZheng Yan 				slot = path->slots[0];
340231840ae1SZheng Yan 				write_extent_buffer(leaf, buf,
340331840ae1SZheng Yan 					    btrfs_item_ptr_offset(leaf, slot),
340431840ae1SZheng Yan 					    size);
3405ae01a0abSYan Zheng 
3406f46b5a66SChristoph Hellwig 				extent = btrfs_item_ptr(leaf, slot,
3407f46b5a66SChristoph Hellwig 						struct btrfs_file_extent_item);
3408c5c9cd4dSSage Weil 
3409c5c9cd4dSSage Weil 				/* disko == 0 means it's a hole */
3410c5c9cd4dSSage Weil 				if (!disko)
3411c5c9cd4dSSage Weil 					datao = 0;
3412c5c9cd4dSSage Weil 
3413c5c9cd4dSSage Weil 				btrfs_set_file_extent_offset(leaf, extent,
3414c5c9cd4dSSage Weil 							     datao);
3415c5c9cd4dSSage Weil 				btrfs_set_file_extent_num_bytes(leaf, extent,
3416c5c9cd4dSSage Weil 								datal);
3417fcebe456SJosef Bacik 
3418fcebe456SJosef Bacik 				/*
3419fcebe456SJosef Bacik 				 * We need to look up the roots that point at
3420fcebe456SJosef Bacik 				 * this bytenr and see if the new root does.  If
3421fcebe456SJosef Bacik 				 * it does not we need to make sure we update
3422fcebe456SJosef Bacik 				 * quotas appropriately.
3423fcebe456SJosef Bacik 				 */
3424fcebe456SJosef Bacik 				if (disko && root != BTRFS_I(src)->root &&
3425fcebe456SJosef Bacik 				    disko != last_disko) {
3426fcebe456SJosef Bacik 					no_quota = check_ref(trans, root,
3427fcebe456SJosef Bacik 							     disko);
3428fcebe456SJosef Bacik 					if (no_quota < 0) {
3429fcebe456SJosef Bacik 						btrfs_abort_transaction(trans,
3430fcebe456SJosef Bacik 									root,
3431fcebe456SJosef Bacik 									ret);
3432fcebe456SJosef Bacik 						btrfs_end_transaction(trans,
3433fcebe456SJosef Bacik 								      root);
3434fcebe456SJosef Bacik 						ret = no_quota;
3435fcebe456SJosef Bacik 						goto out;
3436fcebe456SJosef Bacik 					}
3437fcebe456SJosef Bacik 				}
3438fcebe456SJosef Bacik 
3439c5c9cd4dSSage Weil 				if (disko) {
3440c5c9cd4dSSage Weil 					inode_add_bytes(inode, datal);
3441ae01a0abSYan Zheng 					ret = btrfs_inc_extent_ref(trans, root,
34425d4f98a2SYan Zheng 							disko, diskl, 0,
3443f46b5a66SChristoph Hellwig 							root->root_key.objectid,
344433345d01SLi Zefan 							btrfs_ino(inode),
344566d7e7f0SArne Jansen 							new_key.offset - datao,
3446fcebe456SJosef Bacik 							no_quota);
344779787eaaSJeff Mahoney 					if (ret) {
344879787eaaSJeff Mahoney 						btrfs_abort_transaction(trans,
344979787eaaSJeff Mahoney 									root,
345079787eaaSJeff Mahoney 									ret);
345179787eaaSJeff Mahoney 						btrfs_end_transaction(trans,
345279787eaaSJeff Mahoney 								      root);
345379787eaaSJeff Mahoney 						goto out;
345479787eaaSJeff Mahoney 
345579787eaaSJeff Mahoney 					}
3456f46b5a66SChristoph Hellwig 				}
3457c5c9cd4dSSage Weil 			} else if (type == BTRFS_FILE_EXTENT_INLINE) {
3458c5c9cd4dSSage Weil 				u64 skip = 0;
3459c5c9cd4dSSage Weil 				u64 trim = 0;
3460d3ecfcdfSLiu Bo 				u64 aligned_end = 0;
3461d3ecfcdfSLiu Bo 
3462c5c9cd4dSSage Weil 				if (off > key.offset) {
3463c5c9cd4dSSage Weil 					skip = off - key.offset;
3464c5c9cd4dSSage Weil 					new_key.offset += skip;
346531840ae1SZheng Yan 				}
3466d397712bSChris Mason 
3467c5c9cd4dSSage Weil 				if (key.offset + datal > off + len)
3468c5c9cd4dSSage Weil 					trim = key.offset + datal - (off + len);
3469d397712bSChris Mason 
3470c5c9cd4dSSage Weil 				if (comp && (skip || trim)) {
3471c5c9cd4dSSage Weil 					ret = -EINVAL;
3472a22285a6SYan, Zheng 					btrfs_end_transaction(trans, root);
3473c5c9cd4dSSage Weil 					goto out;
347431840ae1SZheng Yan 				}
3475c5c9cd4dSSage Weil 				size -= skip + trim;
3476c5c9cd4dSSage Weil 				datal -= skip + trim;
3477a22285a6SYan, Zheng 
3478d3ecfcdfSLiu Bo 				aligned_end = ALIGN(new_key.offset + datal,
3479d3ecfcdfSLiu Bo 						    root->sectorsize);
34805dc562c5SJosef Bacik 				ret = btrfs_drop_extents(trans, root, inode,
3481f82a9901SFilipe Manana 							 drop_start,
3482d3ecfcdfSLiu Bo 							 aligned_end,
34832671485dSJosef Bacik 							 1);
348479787eaaSJeff Mahoney 				if (ret) {
34853f9e3df8SDavid Sterba 					if (ret != -EOPNOTSUPP)
34863a29bc09SChris Mason 						btrfs_abort_transaction(trans,
34873a29bc09SChris Mason 							root, ret);
348879787eaaSJeff Mahoney 					btrfs_end_transaction(trans, root);
348979787eaaSJeff Mahoney 					goto out;
349079787eaaSJeff Mahoney 				}
3491a22285a6SYan, Zheng 
3492c5c9cd4dSSage Weil 				ret = btrfs_insert_empty_item(trans, root, path,
3493c5c9cd4dSSage Weil 							      &new_key, size);
349479787eaaSJeff Mahoney 				if (ret) {
349579787eaaSJeff Mahoney 					btrfs_abort_transaction(trans, root,
349679787eaaSJeff Mahoney 								ret);
349779787eaaSJeff Mahoney 					btrfs_end_transaction(trans, root);
349879787eaaSJeff Mahoney 					goto out;
349979787eaaSJeff Mahoney 				}
3500c5c9cd4dSSage Weil 
3501c5c9cd4dSSage Weil 				if (skip) {
3502d397712bSChris Mason 					u32 start =
3503d397712bSChris Mason 					  btrfs_file_extent_calc_inline_size(0);
3504c5c9cd4dSSage Weil 					memmove(buf+start, buf+start+skip,
3505c5c9cd4dSSage Weil 						datal);
3506c5c9cd4dSSage Weil 				}
3507c5c9cd4dSSage Weil 
3508c5c9cd4dSSage Weil 				leaf = path->nodes[0];
3509c5c9cd4dSSage Weil 				slot = path->slots[0];
3510c5c9cd4dSSage Weil 				write_extent_buffer(leaf, buf,
3511c5c9cd4dSSage Weil 					    btrfs_item_ptr_offset(leaf, slot),
3512c5c9cd4dSSage Weil 					    size);
3513c5c9cd4dSSage Weil 				inode_add_bytes(inode, datal);
35147ffbb598SFilipe Manana 				extent = btrfs_item_ptr(leaf, slot,
35157ffbb598SFilipe Manana 						struct btrfs_file_extent_item);
3516c5c9cd4dSSage Weil 			}
3517c5c9cd4dSSage Weil 
35187ffbb598SFilipe Manana 			/* If we have an implicit hole (NO_HOLES feature). */
35197ffbb598SFilipe Manana 			if (drop_start < new_key.offset)
35207ffbb598SFilipe Manana 				clone_update_extent_map(inode, trans,
35217ffbb598SFilipe Manana 						path, NULL, drop_start,
35227ffbb598SFilipe Manana 						new_key.offset - drop_start);
35237ffbb598SFilipe Manana 
35247ffbb598SFilipe Manana 			clone_update_extent_map(inode, trans, path,
35257ffbb598SFilipe Manana 						extent, 0, 0);
35267ffbb598SFilipe Manana 
3527c5c9cd4dSSage Weil 			btrfs_mark_buffer_dirty(leaf);
3528b3b4aa74SDavid Sterba 			btrfs_release_path(path);
3529c5c9cd4dSSage Weil 
3530f82a9901SFilipe Manana 			last_dest_end = new_key.offset + datal;
3531f82a9901SFilipe Manana 			ret = clone_finish_inode_update(trans, inode,
3532f82a9901SFilipe Manana 							last_dest_end,
3533f82a9901SFilipe Manana 							destoff, olen);
3534f82a9901SFilipe Manana 			if (ret)
353579787eaaSJeff Mahoney 				goto out;
35362c463823SFilipe Manana 			if (new_key.offset + datal >= destoff + len)
35372c463823SFilipe Manana 				break;
3538a22285a6SYan, Zheng 		}
3539b3b4aa74SDavid Sterba 		btrfs_release_path(path);
3540ae01a0abSYan Zheng 		key.offset++;
3541ae01a0abSYan Zheng 	}
3542f46b5a66SChristoph Hellwig 	ret = 0;
354332b7c687SMark Fasheh 
3544f82a9901SFilipe Manana 	if (last_dest_end < destoff + len) {
3545f82a9901SFilipe Manana 		/*
3546f82a9901SFilipe Manana 		 * We have an implicit hole (NO_HOLES feature is enabled) that
3547f82a9901SFilipe Manana 		 * fully or partially overlaps our cloning range at its end.
3548f82a9901SFilipe Manana 		 */
3549f82a9901SFilipe Manana 		btrfs_release_path(path);
3550f82a9901SFilipe Manana 
3551f82a9901SFilipe Manana 		/*
3552f82a9901SFilipe Manana 		 * 1 - remove extent(s)
3553f82a9901SFilipe Manana 		 * 1 - inode update
3554f82a9901SFilipe Manana 		 */
3555f82a9901SFilipe Manana 		trans = btrfs_start_transaction(root, 2);
3556f82a9901SFilipe Manana 		if (IS_ERR(trans)) {
3557f82a9901SFilipe Manana 			ret = PTR_ERR(trans);
3558f82a9901SFilipe Manana 			goto out;
3559f82a9901SFilipe Manana 		}
3560f82a9901SFilipe Manana 		ret = btrfs_drop_extents(trans, root, inode,
3561f82a9901SFilipe Manana 					 last_dest_end, destoff + len, 1);
3562f82a9901SFilipe Manana 		if (ret) {
3563f82a9901SFilipe Manana 			if (ret != -EOPNOTSUPP)
3564f82a9901SFilipe Manana 				btrfs_abort_transaction(trans, root, ret);
3565f82a9901SFilipe Manana 			btrfs_end_transaction(trans, root);
3566f82a9901SFilipe Manana 			goto out;
3567f82a9901SFilipe Manana 		}
3568f82a9901SFilipe Manana 		ret = clone_finish_inode_update(trans, inode, destoff + len,
3569f82a9901SFilipe Manana 						destoff, olen);
35707ffbb598SFilipe Manana 		if (ret)
35717ffbb598SFilipe Manana 			goto out;
35727ffbb598SFilipe Manana 		clone_update_extent_map(inode, trans, path, NULL, last_dest_end,
35737ffbb598SFilipe Manana 					destoff + len - last_dest_end);
3574f82a9901SFilipe Manana 	}
3575f82a9901SFilipe Manana 
3576f46b5a66SChristoph Hellwig out:
357732b7c687SMark Fasheh 	btrfs_free_path(path);
357832b7c687SMark Fasheh 	vfree(buf);
357932b7c687SMark Fasheh 	return ret;
358032b7c687SMark Fasheh }
358132b7c687SMark Fasheh 
358232b7c687SMark Fasheh static noinline long btrfs_ioctl_clone(struct file *file, unsigned long srcfd,
358332b7c687SMark Fasheh 				       u64 off, u64 olen, u64 destoff)
358432b7c687SMark Fasheh {
358554563d41SAl Viro 	struct inode *inode = file_inode(file);
358632b7c687SMark Fasheh 	struct btrfs_root *root = BTRFS_I(inode)->root;
358732b7c687SMark Fasheh 	struct fd src_file;
358832b7c687SMark Fasheh 	struct inode *src;
358932b7c687SMark Fasheh 	int ret;
359032b7c687SMark Fasheh 	u64 len = olen;
359132b7c687SMark Fasheh 	u64 bs = root->fs_info->sb->s_blocksize;
359232b7c687SMark Fasheh 	int same_inode = 0;
359332b7c687SMark Fasheh 
359432b7c687SMark Fasheh 	/*
359532b7c687SMark Fasheh 	 * TODO:
359632b7c687SMark Fasheh 	 * - split compressed inline extents.  annoying: we need to
359732b7c687SMark Fasheh 	 *   decompress into destination's address_space (the file offset
359832b7c687SMark Fasheh 	 *   may change, so source mapping won't do), then recompress (or
359932b7c687SMark Fasheh 	 *   otherwise reinsert) a subrange.
360000fdf13aSLiu Bo 	 *
360100fdf13aSLiu Bo 	 * - split destination inode's inline extents.  The inline extents can
360200fdf13aSLiu Bo 	 *   be either compressed or non-compressed.
360332b7c687SMark Fasheh 	 */
360432b7c687SMark Fasheh 
360532b7c687SMark Fasheh 	/* the destination must be opened for writing */
360632b7c687SMark Fasheh 	if (!(file->f_mode & FMODE_WRITE) || (file->f_flags & O_APPEND))
360732b7c687SMark Fasheh 		return -EINVAL;
360832b7c687SMark Fasheh 
360932b7c687SMark Fasheh 	if (btrfs_root_readonly(root))
361032b7c687SMark Fasheh 		return -EROFS;
361132b7c687SMark Fasheh 
361232b7c687SMark Fasheh 	ret = mnt_want_write_file(file);
361332b7c687SMark Fasheh 	if (ret)
361432b7c687SMark Fasheh 		return ret;
361532b7c687SMark Fasheh 
361632b7c687SMark Fasheh 	src_file = fdget(srcfd);
361732b7c687SMark Fasheh 	if (!src_file.file) {
361832b7c687SMark Fasheh 		ret = -EBADF;
361932b7c687SMark Fasheh 		goto out_drop_write;
362032b7c687SMark Fasheh 	}
362132b7c687SMark Fasheh 
362232b7c687SMark Fasheh 	ret = -EXDEV;
362332b7c687SMark Fasheh 	if (src_file.file->f_path.mnt != file->f_path.mnt)
362432b7c687SMark Fasheh 		goto out_fput;
362532b7c687SMark Fasheh 
362632b7c687SMark Fasheh 	src = file_inode(src_file.file);
362732b7c687SMark Fasheh 
362832b7c687SMark Fasheh 	ret = -EINVAL;
362932b7c687SMark Fasheh 	if (src == inode)
363032b7c687SMark Fasheh 		same_inode = 1;
363132b7c687SMark Fasheh 
363232b7c687SMark Fasheh 	/* the src must be open for reading */
363332b7c687SMark Fasheh 	if (!(src_file.file->f_mode & FMODE_READ))
363432b7c687SMark Fasheh 		goto out_fput;
363532b7c687SMark Fasheh 
363632b7c687SMark Fasheh 	/* don't make the dst file partly checksummed */
363732b7c687SMark Fasheh 	if ((BTRFS_I(src)->flags & BTRFS_INODE_NODATASUM) !=
363832b7c687SMark Fasheh 	    (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM))
363932b7c687SMark Fasheh 		goto out_fput;
364032b7c687SMark Fasheh 
364132b7c687SMark Fasheh 	ret = -EISDIR;
364232b7c687SMark Fasheh 	if (S_ISDIR(src->i_mode) || S_ISDIR(inode->i_mode))
364332b7c687SMark Fasheh 		goto out_fput;
364432b7c687SMark Fasheh 
364532b7c687SMark Fasheh 	ret = -EXDEV;
364632b7c687SMark Fasheh 	if (src->i_sb != inode->i_sb)
364732b7c687SMark Fasheh 		goto out_fput;
364832b7c687SMark Fasheh 
364932b7c687SMark Fasheh 	if (!same_inode) {
365032b7c687SMark Fasheh 		if (inode < src) {
365132b7c687SMark Fasheh 			mutex_lock_nested(&inode->i_mutex, I_MUTEX_PARENT);
365232b7c687SMark Fasheh 			mutex_lock_nested(&src->i_mutex, I_MUTEX_CHILD);
365332b7c687SMark Fasheh 		} else {
365432b7c687SMark Fasheh 			mutex_lock_nested(&src->i_mutex, I_MUTEX_PARENT);
365532b7c687SMark Fasheh 			mutex_lock_nested(&inode->i_mutex, I_MUTEX_CHILD);
365632b7c687SMark Fasheh 		}
365732b7c687SMark Fasheh 	} else {
365832b7c687SMark Fasheh 		mutex_lock(&src->i_mutex);
365932b7c687SMark Fasheh 	}
366032b7c687SMark Fasheh 
366132b7c687SMark Fasheh 	/* determine range to clone */
366232b7c687SMark Fasheh 	ret = -EINVAL;
366332b7c687SMark Fasheh 	if (off + len > src->i_size || off + len < off)
366432b7c687SMark Fasheh 		goto out_unlock;
366532b7c687SMark Fasheh 	if (len == 0)
366632b7c687SMark Fasheh 		olen = len = src->i_size - off;
366732b7c687SMark Fasheh 	/* if we extend to eof, continue to block boundary */
366832b7c687SMark Fasheh 	if (off + len == src->i_size)
366932b7c687SMark Fasheh 		len = ALIGN(src->i_size, bs) - off;
367032b7c687SMark Fasheh 
367132b7c687SMark Fasheh 	/* verify the end result is block aligned */
367232b7c687SMark Fasheh 	if (!IS_ALIGNED(off, bs) || !IS_ALIGNED(off + len, bs) ||
367332b7c687SMark Fasheh 	    !IS_ALIGNED(destoff, bs))
367432b7c687SMark Fasheh 		goto out_unlock;
367532b7c687SMark Fasheh 
367632b7c687SMark Fasheh 	/* verify if ranges are overlapped within the same file */
367732b7c687SMark Fasheh 	if (same_inode) {
367832b7c687SMark Fasheh 		if (destoff + len > off && destoff < off + len)
367932b7c687SMark Fasheh 			goto out_unlock;
368032b7c687SMark Fasheh 	}
368132b7c687SMark Fasheh 
368232b7c687SMark Fasheh 	if (destoff > inode->i_size) {
368332b7c687SMark Fasheh 		ret = btrfs_cont_expand(inode, inode->i_size, destoff);
368432b7c687SMark Fasheh 		if (ret)
368532b7c687SMark Fasheh 			goto out_unlock;
368632b7c687SMark Fasheh 	}
368732b7c687SMark Fasheh 
3688c125b8bfSFilipe Manana 	/*
3689c125b8bfSFilipe Manana 	 * Lock the target range too. Right after we replace the file extent
3690c125b8bfSFilipe Manana 	 * items in the fs tree (which now point to the cloned data), we might
3691c125b8bfSFilipe Manana 	 * have a worker replace them with extent items relative to a write
3692c125b8bfSFilipe Manana 	 * operation that was issued before this clone operation (i.e. confront
3693c125b8bfSFilipe Manana 	 * with inode.c:btrfs_finish_ordered_io).
3694c125b8bfSFilipe Manana 	 */
3695c125b8bfSFilipe Manana 	if (same_inode) {
3696c125b8bfSFilipe Manana 		u64 lock_start = min_t(u64, off, destoff);
3697c125b8bfSFilipe Manana 		u64 lock_len = max_t(u64, off, destoff) + len - lock_start;
369832b7c687SMark Fasheh 
3699c125b8bfSFilipe Manana 		lock_extent_range(src, lock_start, lock_len);
3700c125b8bfSFilipe Manana 	} else {
370132b7c687SMark Fasheh 		lock_extent_range(src, off, len);
3702c125b8bfSFilipe Manana 		lock_extent_range(inode, destoff, len);
3703c125b8bfSFilipe Manana 	}
370432b7c687SMark Fasheh 
370532b7c687SMark Fasheh 	ret = btrfs_clone(src, inode, off, olen, len, destoff);
370632b7c687SMark Fasheh 
3707c125b8bfSFilipe Manana 	if (same_inode) {
3708c125b8bfSFilipe Manana 		u64 lock_start = min_t(u64, off, destoff);
3709c125b8bfSFilipe Manana 		u64 lock_end = max_t(u64, off, destoff) + len - 1;
3710c125b8bfSFilipe Manana 
3711c125b8bfSFilipe Manana 		unlock_extent(&BTRFS_I(src)->io_tree, lock_start, lock_end);
3712c125b8bfSFilipe Manana 	} else {
3713aa42ffd9SLiu Bo 		unlock_extent(&BTRFS_I(src)->io_tree, off, off + len - 1);
3714c125b8bfSFilipe Manana 		unlock_extent(&BTRFS_I(inode)->io_tree, destoff,
3715c125b8bfSFilipe Manana 			      destoff + len - 1);
3716c125b8bfSFilipe Manana 	}
3717c125b8bfSFilipe Manana 	/*
3718c125b8bfSFilipe Manana 	 * Truncate page cache pages so that future reads will see the cloned
3719c125b8bfSFilipe Manana 	 * data immediately and not the previous data.
3720c125b8bfSFilipe Manana 	 */
3721c125b8bfSFilipe Manana 	truncate_inode_pages_range(&inode->i_data, destoff,
3722c125b8bfSFilipe Manana 				   PAGE_CACHE_ALIGN(destoff + len) - 1);
3723f46b5a66SChristoph Hellwig out_unlock:
3724c57c2b3eSFilipe David Borba Manana 	if (!same_inode) {
3725c57c2b3eSFilipe David Borba Manana 		if (inode < src) {
3726f46b5a66SChristoph Hellwig 			mutex_unlock(&src->i_mutex);
3727f46b5a66SChristoph Hellwig 			mutex_unlock(&inode->i_mutex);
3728c57c2b3eSFilipe David Borba Manana 		} else {
3729c57c2b3eSFilipe David Borba Manana 			mutex_unlock(&inode->i_mutex);
3730c57c2b3eSFilipe David Borba Manana 			mutex_unlock(&src->i_mutex);
3731c57c2b3eSFilipe David Borba Manana 		}
3732c57c2b3eSFilipe David Borba Manana 	} else {
3733c57c2b3eSFilipe David Borba Manana 		mutex_unlock(&src->i_mutex);
3734c57c2b3eSFilipe David Borba Manana 	}
3735f46b5a66SChristoph Hellwig out_fput:
37362903ff01SAl Viro 	fdput(src_file);
3737ab67b7c1SYan Zheng out_drop_write:
37382a79f17eSAl Viro 	mnt_drop_write_file(file);
3739f46b5a66SChristoph Hellwig 	return ret;
3740f46b5a66SChristoph Hellwig }
3741f46b5a66SChristoph Hellwig 
37427a865e8aSChristoph Hellwig static long btrfs_ioctl_clone_range(struct file *file, void __user *argp)
3743c5c9cd4dSSage Weil {
3744c5c9cd4dSSage Weil 	struct btrfs_ioctl_clone_range_args args;
3745c5c9cd4dSSage Weil 
37467a865e8aSChristoph Hellwig 	if (copy_from_user(&args, argp, sizeof(args)))
3747c5c9cd4dSSage Weil 		return -EFAULT;
3748c5c9cd4dSSage Weil 	return btrfs_ioctl_clone(file, args.src_fd, args.src_offset,
3749c5c9cd4dSSage Weil 				 args.src_length, args.dest_offset);
3750c5c9cd4dSSage Weil }
3751c5c9cd4dSSage Weil 
3752f46b5a66SChristoph Hellwig /*
3753f46b5a66SChristoph Hellwig  * there are many ways the trans_start and trans_end ioctls can lead
3754f46b5a66SChristoph Hellwig  * to deadlocks.  They should only be used by applications that
3755f46b5a66SChristoph Hellwig  * basically own the machine, and have a very in depth understanding
3756f46b5a66SChristoph Hellwig  * of all the possible deadlocks and enospc problems.
3757f46b5a66SChristoph Hellwig  */
3758b2950863SChristoph Hellwig static long btrfs_ioctl_trans_start(struct file *file)
3759f46b5a66SChristoph Hellwig {
3760496ad9aaSAl Viro 	struct inode *inode = file_inode(file);
3761f46b5a66SChristoph Hellwig 	struct btrfs_root *root = BTRFS_I(inode)->root;
3762f46b5a66SChristoph Hellwig 	struct btrfs_trans_handle *trans;
37631ab86aedSSage Weil 	int ret;
3764f46b5a66SChristoph Hellwig 
37651ab86aedSSage Weil 	ret = -EPERM;
3766df5b5520SChristoph Hellwig 	if (!capable(CAP_SYS_ADMIN))
3767f46b5a66SChristoph Hellwig 		goto out;
37681ab86aedSSage Weil 
37691ab86aedSSage Weil 	ret = -EINPROGRESS;
37701ab86aedSSage Weil 	if (file->private_data)
37711ab86aedSSage Weil 		goto out;
37729ca9ee09SSage Weil 
3773b83cc969SLi Zefan 	ret = -EROFS;
3774b83cc969SLi Zefan 	if (btrfs_root_readonly(root))
3775b83cc969SLi Zefan 		goto out;
3776b83cc969SLi Zefan 
3777a561be71SAl Viro 	ret = mnt_want_write_file(file);
3778c146afadSYan Zheng 	if (ret)
3779c146afadSYan Zheng 		goto out;
3780c146afadSYan Zheng 
3781a4abeea4SJosef Bacik 	atomic_inc(&root->fs_info->open_ioctl_trans);
37829ca9ee09SSage Weil 
3783f46b5a66SChristoph Hellwig 	ret = -ENOMEM;
37847a7eaa40SJosef Bacik 	trans = btrfs_start_ioctl_transaction(root);
3785abd30bb0STsutomu Itoh 	if (IS_ERR(trans))
37861ab86aedSSage Weil 		goto out_drop;
37871ab86aedSSage Weil 
37881ab86aedSSage Weil 	file->private_data = trans;
37891ab86aedSSage Weil 	return 0;
37901ab86aedSSage Weil 
37911ab86aedSSage Weil out_drop:
3792a4abeea4SJosef Bacik 	atomic_dec(&root->fs_info->open_ioctl_trans);
37932a79f17eSAl Viro 	mnt_drop_write_file(file);
3794f46b5a66SChristoph Hellwig out:
3795f46b5a66SChristoph Hellwig 	return ret;
3796f46b5a66SChristoph Hellwig }
3797f46b5a66SChristoph Hellwig 
37986ef5ed0dSJosef Bacik static long btrfs_ioctl_default_subvol(struct file *file, void __user *argp)
37996ef5ed0dSJosef Bacik {
3800496ad9aaSAl Viro 	struct inode *inode = file_inode(file);
38016ef5ed0dSJosef Bacik 	struct btrfs_root *root = BTRFS_I(inode)->root;
38026ef5ed0dSJosef Bacik 	struct btrfs_root *new_root;
38036ef5ed0dSJosef Bacik 	struct btrfs_dir_item *di;
38046ef5ed0dSJosef Bacik 	struct btrfs_trans_handle *trans;
38056ef5ed0dSJosef Bacik 	struct btrfs_path *path;
38066ef5ed0dSJosef Bacik 	struct btrfs_key location;
38076ef5ed0dSJosef Bacik 	struct btrfs_disk_key disk_key;
38086ef5ed0dSJosef Bacik 	u64 objectid = 0;
38096ef5ed0dSJosef Bacik 	u64 dir_id;
38103c04ce01SMiao Xie 	int ret;
38116ef5ed0dSJosef Bacik 
38126ef5ed0dSJosef Bacik 	if (!capable(CAP_SYS_ADMIN))
38136ef5ed0dSJosef Bacik 		return -EPERM;
38146ef5ed0dSJosef Bacik 
38153c04ce01SMiao Xie 	ret = mnt_want_write_file(file);
38163c04ce01SMiao Xie 	if (ret)
38173c04ce01SMiao Xie 		return ret;
38183c04ce01SMiao Xie 
38193c04ce01SMiao Xie 	if (copy_from_user(&objectid, argp, sizeof(objectid))) {
38203c04ce01SMiao Xie 		ret = -EFAULT;
38213c04ce01SMiao Xie 		goto out;
38223c04ce01SMiao Xie 	}
38236ef5ed0dSJosef Bacik 
38246ef5ed0dSJosef Bacik 	if (!objectid)
38251cecf579Schandan 		objectid = BTRFS_FS_TREE_OBJECTID;
38266ef5ed0dSJosef Bacik 
38276ef5ed0dSJosef Bacik 	location.objectid = objectid;
38286ef5ed0dSJosef Bacik 	location.type = BTRFS_ROOT_ITEM_KEY;
38296ef5ed0dSJosef Bacik 	location.offset = (u64)-1;
38306ef5ed0dSJosef Bacik 
38316ef5ed0dSJosef Bacik 	new_root = btrfs_read_fs_root_no_name(root->fs_info, &location);
38323c04ce01SMiao Xie 	if (IS_ERR(new_root)) {
38333c04ce01SMiao Xie 		ret = PTR_ERR(new_root);
38343c04ce01SMiao Xie 		goto out;
38353c04ce01SMiao Xie 	}
38366ef5ed0dSJosef Bacik 
38376ef5ed0dSJosef Bacik 	path = btrfs_alloc_path();
38383c04ce01SMiao Xie 	if (!path) {
38393c04ce01SMiao Xie 		ret = -ENOMEM;
38403c04ce01SMiao Xie 		goto out;
38413c04ce01SMiao Xie 	}
38426ef5ed0dSJosef Bacik 	path->leave_spinning = 1;
38436ef5ed0dSJosef Bacik 
38446ef5ed0dSJosef Bacik 	trans = btrfs_start_transaction(root, 1);
384598d5dc13STsutomu Itoh 	if (IS_ERR(trans)) {
38466ef5ed0dSJosef Bacik 		btrfs_free_path(path);
38473c04ce01SMiao Xie 		ret = PTR_ERR(trans);
38483c04ce01SMiao Xie 		goto out;
38496ef5ed0dSJosef Bacik 	}
38506ef5ed0dSJosef Bacik 
38516c41761fSDavid Sterba 	dir_id = btrfs_super_root_dir(root->fs_info->super_copy);
38526ef5ed0dSJosef Bacik 	di = btrfs_lookup_dir_item(trans, root->fs_info->tree_root, path,
38536ef5ed0dSJosef Bacik 				   dir_id, "default", 7, 1);
3854cf1e99a4SDan Carpenter 	if (IS_ERR_OR_NULL(di)) {
38556ef5ed0dSJosef Bacik 		btrfs_free_path(path);
38566ef5ed0dSJosef Bacik 		btrfs_end_transaction(trans, root);
3857efe120a0SFrank Holton 		btrfs_err(new_root->fs_info, "Umm, you don't have the default dir"
3858efe120a0SFrank Holton 			   "item, this isn't going to work");
38593c04ce01SMiao Xie 		ret = -ENOENT;
38603c04ce01SMiao Xie 		goto out;
38616ef5ed0dSJosef Bacik 	}
38626ef5ed0dSJosef Bacik 
38636ef5ed0dSJosef Bacik 	btrfs_cpu_key_to_disk(&disk_key, &new_root->root_key);
38646ef5ed0dSJosef Bacik 	btrfs_set_dir_item_key(path->nodes[0], di, &disk_key);
38656ef5ed0dSJosef Bacik 	btrfs_mark_buffer_dirty(path->nodes[0]);
38666ef5ed0dSJosef Bacik 	btrfs_free_path(path);
38676ef5ed0dSJosef Bacik 
38682b0ce2c2SMitch Harder 	btrfs_set_fs_incompat(root->fs_info, DEFAULT_SUBVOL);
38696ef5ed0dSJosef Bacik 	btrfs_end_transaction(trans, root);
38703c04ce01SMiao Xie out:
38713c04ce01SMiao Xie 	mnt_drop_write_file(file);
38723c04ce01SMiao Xie 	return ret;
38736ef5ed0dSJosef Bacik }
38746ef5ed0dSJosef Bacik 
38755af3e8ccSStefan Behrens void btrfs_get_block_group_info(struct list_head *groups_list,
3876bf5fc093SJosef Bacik 				struct btrfs_ioctl_space_info *space)
3877bf5fc093SJosef Bacik {
3878bf5fc093SJosef Bacik 	struct btrfs_block_group_cache *block_group;
3879bf5fc093SJosef Bacik 
3880bf5fc093SJosef Bacik 	space->total_bytes = 0;
3881bf5fc093SJosef Bacik 	space->used_bytes = 0;
3882bf5fc093SJosef Bacik 	space->flags = 0;
3883bf5fc093SJosef Bacik 	list_for_each_entry(block_group, groups_list, list) {
3884bf5fc093SJosef Bacik 		space->flags = block_group->flags;
3885bf5fc093SJosef Bacik 		space->total_bytes += block_group->key.offset;
3886bf5fc093SJosef Bacik 		space->used_bytes +=
3887bf5fc093SJosef Bacik 			btrfs_block_group_used(&block_group->item);
3888bf5fc093SJosef Bacik 	}
3889bf5fc093SJosef Bacik }
3890bf5fc093SJosef Bacik 
389148a3b636SEric Sandeen static long btrfs_ioctl_space_info(struct btrfs_root *root, void __user *arg)
38921406e432SJosef Bacik {
38931406e432SJosef Bacik 	struct btrfs_ioctl_space_args space_args;
38941406e432SJosef Bacik 	struct btrfs_ioctl_space_info space;
38951406e432SJosef Bacik 	struct btrfs_ioctl_space_info *dest;
38967fde62bfSChris Mason 	struct btrfs_ioctl_space_info *dest_orig;
389713f2696fSDaniel J Blueman 	struct btrfs_ioctl_space_info __user *user_dest;
38981406e432SJosef Bacik 	struct btrfs_space_info *info;
3899bf5fc093SJosef Bacik 	u64 types[] = {BTRFS_BLOCK_GROUP_DATA,
3900bf5fc093SJosef Bacik 		       BTRFS_BLOCK_GROUP_SYSTEM,
3901bf5fc093SJosef Bacik 		       BTRFS_BLOCK_GROUP_METADATA,
3902bf5fc093SJosef Bacik 		       BTRFS_BLOCK_GROUP_DATA | BTRFS_BLOCK_GROUP_METADATA};
3903bf5fc093SJosef Bacik 	int num_types = 4;
39047fde62bfSChris Mason 	int alloc_size;
39051406e432SJosef Bacik 	int ret = 0;
390651788b1bSDan Rosenberg 	u64 slot_count = 0;
3907bf5fc093SJosef Bacik 	int i, c;
39081406e432SJosef Bacik 
39091406e432SJosef Bacik 	if (copy_from_user(&space_args,
39101406e432SJosef Bacik 			   (struct btrfs_ioctl_space_args __user *)arg,
39111406e432SJosef Bacik 			   sizeof(space_args)))
39121406e432SJosef Bacik 		return -EFAULT;
39131406e432SJosef Bacik 
3914bf5fc093SJosef Bacik 	for (i = 0; i < num_types; i++) {
3915bf5fc093SJosef Bacik 		struct btrfs_space_info *tmp;
3916bf5fc093SJosef Bacik 
3917bf5fc093SJosef Bacik 		info = NULL;
39187fde62bfSChris Mason 		rcu_read_lock();
3919bf5fc093SJosef Bacik 		list_for_each_entry_rcu(tmp, &root->fs_info->space_info,
3920bf5fc093SJosef Bacik 					list) {
3921bf5fc093SJosef Bacik 			if (tmp->flags == types[i]) {
3922bf5fc093SJosef Bacik 				info = tmp;
3923bf5fc093SJosef Bacik 				break;
3924bf5fc093SJosef Bacik 			}
3925bf5fc093SJosef Bacik 		}
39267fde62bfSChris Mason 		rcu_read_unlock();
39271406e432SJosef Bacik 
3928bf5fc093SJosef Bacik 		if (!info)
3929bf5fc093SJosef Bacik 			continue;
3930bf5fc093SJosef Bacik 
3931bf5fc093SJosef Bacik 		down_read(&info->groups_sem);
3932bf5fc093SJosef Bacik 		for (c = 0; c < BTRFS_NR_RAID_TYPES; c++) {
3933bf5fc093SJosef Bacik 			if (!list_empty(&info->block_groups[c]))
3934bf5fc093SJosef Bacik 				slot_count++;
3935bf5fc093SJosef Bacik 		}
3936bf5fc093SJosef Bacik 		up_read(&info->groups_sem);
3937bf5fc093SJosef Bacik 	}
3938bf5fc093SJosef Bacik 
393936523e95SDavid Sterba 	/*
394036523e95SDavid Sterba 	 * Global block reserve, exported as a space_info
394136523e95SDavid Sterba 	 */
394236523e95SDavid Sterba 	slot_count++;
394336523e95SDavid Sterba 
39447fde62bfSChris Mason 	/* space_slots == 0 means they are asking for a count */
39457fde62bfSChris Mason 	if (space_args.space_slots == 0) {
39467fde62bfSChris Mason 		space_args.total_spaces = slot_count;
39477fde62bfSChris Mason 		goto out;
39487fde62bfSChris Mason 	}
3949bf5fc093SJosef Bacik 
395051788b1bSDan Rosenberg 	slot_count = min_t(u64, space_args.space_slots, slot_count);
3951bf5fc093SJosef Bacik 
39527fde62bfSChris Mason 	alloc_size = sizeof(*dest) * slot_count;
3953bf5fc093SJosef Bacik 
39547fde62bfSChris Mason 	/* we generally have at most 6 or so space infos, one for each raid
39557fde62bfSChris Mason 	 * level.  So, a whole page should be more than enough for everyone
39567fde62bfSChris Mason 	 */
39577fde62bfSChris Mason 	if (alloc_size > PAGE_CACHE_SIZE)
39587fde62bfSChris Mason 		return -ENOMEM;
39597fde62bfSChris Mason 
39607fde62bfSChris Mason 	space_args.total_spaces = 0;
39617fde62bfSChris Mason 	dest = kmalloc(alloc_size, GFP_NOFS);
39627fde62bfSChris Mason 	if (!dest)
39637fde62bfSChris Mason 		return -ENOMEM;
39647fde62bfSChris Mason 	dest_orig = dest;
39657fde62bfSChris Mason 
39667fde62bfSChris Mason 	/* now we have a buffer to copy into */
3967bf5fc093SJosef Bacik 	for (i = 0; i < num_types; i++) {
3968bf5fc093SJosef Bacik 		struct btrfs_space_info *tmp;
3969bf5fc093SJosef Bacik 
397051788b1bSDan Rosenberg 		if (!slot_count)
397151788b1bSDan Rosenberg 			break;
397251788b1bSDan Rosenberg 
3973bf5fc093SJosef Bacik 		info = NULL;
39741406e432SJosef Bacik 		rcu_read_lock();
3975bf5fc093SJosef Bacik 		list_for_each_entry_rcu(tmp, &root->fs_info->space_info,
3976bf5fc093SJosef Bacik 					list) {
3977bf5fc093SJosef Bacik 			if (tmp->flags == types[i]) {
3978bf5fc093SJosef Bacik 				info = tmp;
39797fde62bfSChris Mason 				break;
3980bf5fc093SJosef Bacik 			}
3981bf5fc093SJosef Bacik 		}
3982bf5fc093SJosef Bacik 		rcu_read_unlock();
39837fde62bfSChris Mason 
3984bf5fc093SJosef Bacik 		if (!info)
3985bf5fc093SJosef Bacik 			continue;
3986bf5fc093SJosef Bacik 		down_read(&info->groups_sem);
3987bf5fc093SJosef Bacik 		for (c = 0; c < BTRFS_NR_RAID_TYPES; c++) {
3988bf5fc093SJosef Bacik 			if (!list_empty(&info->block_groups[c])) {
39895af3e8ccSStefan Behrens 				btrfs_get_block_group_info(
39905af3e8ccSStefan Behrens 					&info->block_groups[c], &space);
39917fde62bfSChris Mason 				memcpy(dest, &space, sizeof(space));
39921406e432SJosef Bacik 				dest++;
39931406e432SJosef Bacik 				space_args.total_spaces++;
399451788b1bSDan Rosenberg 				slot_count--;
39951406e432SJosef Bacik 			}
399651788b1bSDan Rosenberg 			if (!slot_count)
399751788b1bSDan Rosenberg 				break;
3998bf5fc093SJosef Bacik 		}
3999bf5fc093SJosef Bacik 		up_read(&info->groups_sem);
4000bf5fc093SJosef Bacik 	}
40011406e432SJosef Bacik 
400236523e95SDavid Sterba 	/*
400336523e95SDavid Sterba 	 * Add global block reserve
400436523e95SDavid Sterba 	 */
400536523e95SDavid Sterba 	if (slot_count) {
400636523e95SDavid Sterba 		struct btrfs_block_rsv *block_rsv = &root->fs_info->global_block_rsv;
400736523e95SDavid Sterba 
400836523e95SDavid Sterba 		spin_lock(&block_rsv->lock);
400936523e95SDavid Sterba 		space.total_bytes = block_rsv->size;
401036523e95SDavid Sterba 		space.used_bytes = block_rsv->size - block_rsv->reserved;
401136523e95SDavid Sterba 		spin_unlock(&block_rsv->lock);
401236523e95SDavid Sterba 		space.flags = BTRFS_SPACE_INFO_GLOBAL_RSV;
401336523e95SDavid Sterba 		memcpy(dest, &space, sizeof(space));
401436523e95SDavid Sterba 		space_args.total_spaces++;
401536523e95SDavid Sterba 	}
401636523e95SDavid Sterba 
40172eec6c81SDaniel J Blueman 	user_dest = (struct btrfs_ioctl_space_info __user *)
40187fde62bfSChris Mason 		(arg + sizeof(struct btrfs_ioctl_space_args));
40197fde62bfSChris Mason 
40207fde62bfSChris Mason 	if (copy_to_user(user_dest, dest_orig, alloc_size))
40217fde62bfSChris Mason 		ret = -EFAULT;
40227fde62bfSChris Mason 
40237fde62bfSChris Mason 	kfree(dest_orig);
40247fde62bfSChris Mason out:
40257fde62bfSChris Mason 	if (ret == 0 && copy_to_user(arg, &space_args, sizeof(space_args)))
40261406e432SJosef Bacik 		ret = -EFAULT;
40271406e432SJosef Bacik 
40281406e432SJosef Bacik 	return ret;
40291406e432SJosef Bacik }
40301406e432SJosef Bacik 
4031f46b5a66SChristoph Hellwig /*
4032f46b5a66SChristoph Hellwig  * there are many ways the trans_start and trans_end ioctls can lead
4033f46b5a66SChristoph Hellwig  * to deadlocks.  They should only be used by applications that
4034f46b5a66SChristoph Hellwig  * basically own the machine, and have a very in depth understanding
4035f46b5a66SChristoph Hellwig  * of all the possible deadlocks and enospc problems.
4036f46b5a66SChristoph Hellwig  */
4037f46b5a66SChristoph Hellwig long btrfs_ioctl_trans_end(struct file *file)
4038f46b5a66SChristoph Hellwig {
4039496ad9aaSAl Viro 	struct inode *inode = file_inode(file);
4040f46b5a66SChristoph Hellwig 	struct btrfs_root *root = BTRFS_I(inode)->root;
4041f46b5a66SChristoph Hellwig 	struct btrfs_trans_handle *trans;
4042f46b5a66SChristoph Hellwig 
4043f46b5a66SChristoph Hellwig 	trans = file->private_data;
40441ab86aedSSage Weil 	if (!trans)
40451ab86aedSSage Weil 		return -EINVAL;
4046b214107eSChristoph Hellwig 	file->private_data = NULL;
40479ca9ee09SSage Weil 
40481ab86aedSSage Weil 	btrfs_end_transaction(trans, root);
40491ab86aedSSage Weil 
4050a4abeea4SJosef Bacik 	atomic_dec(&root->fs_info->open_ioctl_trans);
40519ca9ee09SSage Weil 
40522a79f17eSAl Viro 	mnt_drop_write_file(file);
40531ab86aedSSage Weil 	return 0;
4054f46b5a66SChristoph Hellwig }
4055f46b5a66SChristoph Hellwig 
40569a8c28beSMiao Xie static noinline long btrfs_ioctl_start_sync(struct btrfs_root *root,
40579a8c28beSMiao Xie 					    void __user *argp)
405846204592SSage Weil {
405946204592SSage Weil 	struct btrfs_trans_handle *trans;
406046204592SSage Weil 	u64 transid;
4061db5b493aSTsutomu Itoh 	int ret;
406246204592SSage Weil 
4063d4edf39bSMiao Xie 	trans = btrfs_attach_transaction_barrier(root);
4064ff7c1d33SMiao Xie 	if (IS_ERR(trans)) {
4065ff7c1d33SMiao Xie 		if (PTR_ERR(trans) != -ENOENT)
406698d5dc13STsutomu Itoh 			return PTR_ERR(trans);
4067ff7c1d33SMiao Xie 
4068ff7c1d33SMiao Xie 		/* No running transaction, don't bother */
4069ff7c1d33SMiao Xie 		transid = root->fs_info->last_trans_committed;
4070ff7c1d33SMiao Xie 		goto out;
4071ff7c1d33SMiao Xie 	}
407246204592SSage Weil 	transid = trans->transid;
4073db5b493aSTsutomu Itoh 	ret = btrfs_commit_transaction_async(trans, root, 0);
40748b2b2d3cSTsutomu Itoh 	if (ret) {
40758b2b2d3cSTsutomu Itoh 		btrfs_end_transaction(trans, root);
4076db5b493aSTsutomu Itoh 		return ret;
40778b2b2d3cSTsutomu Itoh 	}
4078ff7c1d33SMiao Xie out:
407946204592SSage Weil 	if (argp)
408046204592SSage Weil 		if (copy_to_user(argp, &transid, sizeof(transid)))
408146204592SSage Weil 			return -EFAULT;
408246204592SSage Weil 	return 0;
408346204592SSage Weil }
408446204592SSage Weil 
40859a8c28beSMiao Xie static noinline long btrfs_ioctl_wait_sync(struct btrfs_root *root,
40869a8c28beSMiao Xie 					   void __user *argp)
408746204592SSage Weil {
408846204592SSage Weil 	u64 transid;
408946204592SSage Weil 
409046204592SSage Weil 	if (argp) {
409146204592SSage Weil 		if (copy_from_user(&transid, argp, sizeof(transid)))
409246204592SSage Weil 			return -EFAULT;
409346204592SSage Weil 	} else {
409446204592SSage Weil 		transid = 0;  /* current trans */
409546204592SSage Weil 	}
409646204592SSage Weil 	return btrfs_wait_for_commit(root, transid);
409746204592SSage Weil }
409846204592SSage Weil 
4099b8e95489SMiao Xie static long btrfs_ioctl_scrub(struct file *file, void __user *arg)
4100475f6387SJan Schmidt {
4101496ad9aaSAl Viro 	struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
4102475f6387SJan Schmidt 	struct btrfs_ioctl_scrub_args *sa;
4103b8e95489SMiao Xie 	int ret;
4104475f6387SJan Schmidt 
4105475f6387SJan Schmidt 	if (!capable(CAP_SYS_ADMIN))
4106475f6387SJan Schmidt 		return -EPERM;
4107475f6387SJan Schmidt 
4108475f6387SJan Schmidt 	sa = memdup_user(arg, sizeof(*sa));
4109475f6387SJan Schmidt 	if (IS_ERR(sa))
4110475f6387SJan Schmidt 		return PTR_ERR(sa);
4111475f6387SJan Schmidt 
4112b8e95489SMiao Xie 	if (!(sa->flags & BTRFS_SCRUB_READONLY)) {
4113b8e95489SMiao Xie 		ret = mnt_want_write_file(file);
4114b8e95489SMiao Xie 		if (ret)
4115b8e95489SMiao Xie 			goto out;
4116b8e95489SMiao Xie 	}
4117b8e95489SMiao Xie 
4118aa1b8cd4SStefan Behrens 	ret = btrfs_scrub_dev(root->fs_info, sa->devid, sa->start, sa->end,
411963a212abSStefan Behrens 			      &sa->progress, sa->flags & BTRFS_SCRUB_READONLY,
412063a212abSStefan Behrens 			      0);
4121475f6387SJan Schmidt 
4122475f6387SJan Schmidt 	if (copy_to_user(arg, sa, sizeof(*sa)))
4123475f6387SJan Schmidt 		ret = -EFAULT;
4124475f6387SJan Schmidt 
4125b8e95489SMiao Xie 	if (!(sa->flags & BTRFS_SCRUB_READONLY))
4126b8e95489SMiao Xie 		mnt_drop_write_file(file);
4127b8e95489SMiao Xie out:
4128475f6387SJan Schmidt 	kfree(sa);
4129475f6387SJan Schmidt 	return ret;
4130475f6387SJan Schmidt }
4131475f6387SJan Schmidt 
4132475f6387SJan Schmidt static long btrfs_ioctl_scrub_cancel(struct btrfs_root *root, void __user *arg)
4133475f6387SJan Schmidt {
4134475f6387SJan Schmidt 	if (!capable(CAP_SYS_ADMIN))
4135475f6387SJan Schmidt 		return -EPERM;
4136475f6387SJan Schmidt 
4137aa1b8cd4SStefan Behrens 	return btrfs_scrub_cancel(root->fs_info);
4138475f6387SJan Schmidt }
4139475f6387SJan Schmidt 
4140475f6387SJan Schmidt static long btrfs_ioctl_scrub_progress(struct btrfs_root *root,
4141475f6387SJan Schmidt 				       void __user *arg)
4142475f6387SJan Schmidt {
4143475f6387SJan Schmidt 	struct btrfs_ioctl_scrub_args *sa;
4144475f6387SJan Schmidt 	int ret;
4145475f6387SJan Schmidt 
4146475f6387SJan Schmidt 	if (!capable(CAP_SYS_ADMIN))
4147475f6387SJan Schmidt 		return -EPERM;
4148475f6387SJan Schmidt 
4149475f6387SJan Schmidt 	sa = memdup_user(arg, sizeof(*sa));
4150475f6387SJan Schmidt 	if (IS_ERR(sa))
4151475f6387SJan Schmidt 		return PTR_ERR(sa);
4152475f6387SJan Schmidt 
4153475f6387SJan Schmidt 	ret = btrfs_scrub_progress(root, sa->devid, &sa->progress);
4154475f6387SJan Schmidt 
4155475f6387SJan Schmidt 	if (copy_to_user(arg, sa, sizeof(*sa)))
4156475f6387SJan Schmidt 		ret = -EFAULT;
4157475f6387SJan Schmidt 
4158475f6387SJan Schmidt 	kfree(sa);
4159475f6387SJan Schmidt 	return ret;
4160475f6387SJan Schmidt }
4161475f6387SJan Schmidt 
4162c11d2c23SStefan Behrens static long btrfs_ioctl_get_dev_stats(struct btrfs_root *root,
4163b27f7c0cSDavid Sterba 				      void __user *arg)
4164c11d2c23SStefan Behrens {
4165c11d2c23SStefan Behrens 	struct btrfs_ioctl_get_dev_stats *sa;
4166c11d2c23SStefan Behrens 	int ret;
4167c11d2c23SStefan Behrens 
4168c11d2c23SStefan Behrens 	sa = memdup_user(arg, sizeof(*sa));
4169c11d2c23SStefan Behrens 	if (IS_ERR(sa))
4170c11d2c23SStefan Behrens 		return PTR_ERR(sa);
4171c11d2c23SStefan Behrens 
4172b27f7c0cSDavid Sterba 	if ((sa->flags & BTRFS_DEV_STATS_RESET) && !capable(CAP_SYS_ADMIN)) {
4173b27f7c0cSDavid Sterba 		kfree(sa);
4174b27f7c0cSDavid Sterba 		return -EPERM;
4175b27f7c0cSDavid Sterba 	}
4176b27f7c0cSDavid Sterba 
4177b27f7c0cSDavid Sterba 	ret = btrfs_get_dev_stats(root, sa);
4178c11d2c23SStefan Behrens 
4179c11d2c23SStefan Behrens 	if (copy_to_user(arg, sa, sizeof(*sa)))
4180c11d2c23SStefan Behrens 		ret = -EFAULT;
4181c11d2c23SStefan Behrens 
4182c11d2c23SStefan Behrens 	kfree(sa);
4183c11d2c23SStefan Behrens 	return ret;
4184c11d2c23SStefan Behrens }
4185c11d2c23SStefan Behrens 
41863f6bcfbdSStefan Behrens static long btrfs_ioctl_dev_replace(struct btrfs_root *root, void __user *arg)
41873f6bcfbdSStefan Behrens {
41883f6bcfbdSStefan Behrens 	struct btrfs_ioctl_dev_replace_args *p;
41893f6bcfbdSStefan Behrens 	int ret;
41903f6bcfbdSStefan Behrens 
41913f6bcfbdSStefan Behrens 	if (!capable(CAP_SYS_ADMIN))
41923f6bcfbdSStefan Behrens 		return -EPERM;
41933f6bcfbdSStefan Behrens 
41943f6bcfbdSStefan Behrens 	p = memdup_user(arg, sizeof(*p));
41953f6bcfbdSStefan Behrens 	if (IS_ERR(p))
41963f6bcfbdSStefan Behrens 		return PTR_ERR(p);
41973f6bcfbdSStefan Behrens 
41983f6bcfbdSStefan Behrens 	switch (p->cmd) {
41993f6bcfbdSStefan Behrens 	case BTRFS_IOCTL_DEV_REPLACE_CMD_START:
4200adfa97cbSIlya Dryomov 		if (root->fs_info->sb->s_flags & MS_RDONLY) {
4201adfa97cbSIlya Dryomov 			ret = -EROFS;
4202adfa97cbSIlya Dryomov 			goto out;
4203adfa97cbSIlya Dryomov 		}
42043f6bcfbdSStefan Behrens 		if (atomic_xchg(
42053f6bcfbdSStefan Behrens 			&root->fs_info->mutually_exclusive_operation_running,
42063f6bcfbdSStefan Behrens 			1)) {
4207e57138b3SAnand Jain 			ret = BTRFS_ERROR_DEV_EXCL_RUN_IN_PROGRESS;
42083f6bcfbdSStefan Behrens 		} else {
42093f6bcfbdSStefan Behrens 			ret = btrfs_dev_replace_start(root, p);
42103f6bcfbdSStefan Behrens 			atomic_set(
42113f6bcfbdSStefan Behrens 			 &root->fs_info->mutually_exclusive_operation_running,
42123f6bcfbdSStefan Behrens 			 0);
42133f6bcfbdSStefan Behrens 		}
42143f6bcfbdSStefan Behrens 		break;
42153f6bcfbdSStefan Behrens 	case BTRFS_IOCTL_DEV_REPLACE_CMD_STATUS:
42163f6bcfbdSStefan Behrens 		btrfs_dev_replace_status(root->fs_info, p);
42173f6bcfbdSStefan Behrens 		ret = 0;
42183f6bcfbdSStefan Behrens 		break;
42193f6bcfbdSStefan Behrens 	case BTRFS_IOCTL_DEV_REPLACE_CMD_CANCEL:
42203f6bcfbdSStefan Behrens 		ret = btrfs_dev_replace_cancel(root->fs_info, p);
42213f6bcfbdSStefan Behrens 		break;
42223f6bcfbdSStefan Behrens 	default:
42233f6bcfbdSStefan Behrens 		ret = -EINVAL;
42243f6bcfbdSStefan Behrens 		break;
42253f6bcfbdSStefan Behrens 	}
42263f6bcfbdSStefan Behrens 
42273f6bcfbdSStefan Behrens 	if (copy_to_user(arg, p, sizeof(*p)))
42283f6bcfbdSStefan Behrens 		ret = -EFAULT;
4229adfa97cbSIlya Dryomov out:
42303f6bcfbdSStefan Behrens 	kfree(p);
42313f6bcfbdSStefan Behrens 	return ret;
42323f6bcfbdSStefan Behrens }
42333f6bcfbdSStefan Behrens 
4234d7728c96SJan Schmidt static long btrfs_ioctl_ino_to_path(struct btrfs_root *root, void __user *arg)
4235d7728c96SJan Schmidt {
4236d7728c96SJan Schmidt 	int ret = 0;
4237d7728c96SJan Schmidt 	int i;
4238740c3d22SChris Mason 	u64 rel_ptr;
4239d7728c96SJan Schmidt 	int size;
4240806468f8SChris Mason 	struct btrfs_ioctl_ino_path_args *ipa = NULL;
4241d7728c96SJan Schmidt 	struct inode_fs_paths *ipath = NULL;
4242d7728c96SJan Schmidt 	struct btrfs_path *path;
4243d7728c96SJan Schmidt 
424482b22ac8SKusanagi Kouichi 	if (!capable(CAP_DAC_READ_SEARCH))
4245d7728c96SJan Schmidt 		return -EPERM;
4246d7728c96SJan Schmidt 
4247d7728c96SJan Schmidt 	path = btrfs_alloc_path();
4248d7728c96SJan Schmidt 	if (!path) {
4249d7728c96SJan Schmidt 		ret = -ENOMEM;
4250d7728c96SJan Schmidt 		goto out;
4251d7728c96SJan Schmidt 	}
4252d7728c96SJan Schmidt 
4253d7728c96SJan Schmidt 	ipa = memdup_user(arg, sizeof(*ipa));
4254d7728c96SJan Schmidt 	if (IS_ERR(ipa)) {
4255d7728c96SJan Schmidt 		ret = PTR_ERR(ipa);
4256d7728c96SJan Schmidt 		ipa = NULL;
4257d7728c96SJan Schmidt 		goto out;
4258d7728c96SJan Schmidt 	}
4259d7728c96SJan Schmidt 
4260d7728c96SJan Schmidt 	size = min_t(u32, ipa->size, 4096);
4261d7728c96SJan Schmidt 	ipath = init_ipath(size, root, path);
4262d7728c96SJan Schmidt 	if (IS_ERR(ipath)) {
4263d7728c96SJan Schmidt 		ret = PTR_ERR(ipath);
4264d7728c96SJan Schmidt 		ipath = NULL;
4265d7728c96SJan Schmidt 		goto out;
4266d7728c96SJan Schmidt 	}
4267d7728c96SJan Schmidt 
4268d7728c96SJan Schmidt 	ret = paths_from_inode(ipa->inum, ipath);
4269d7728c96SJan Schmidt 	if (ret < 0)
4270d7728c96SJan Schmidt 		goto out;
4271d7728c96SJan Schmidt 
4272d7728c96SJan Schmidt 	for (i = 0; i < ipath->fspath->elem_cnt; ++i) {
4273745c4d8eSJeff Mahoney 		rel_ptr = ipath->fspath->val[i] -
4274745c4d8eSJeff Mahoney 			  (u64)(unsigned long)ipath->fspath->val;
4275740c3d22SChris Mason 		ipath->fspath->val[i] = rel_ptr;
4276d7728c96SJan Schmidt 	}
4277d7728c96SJan Schmidt 
4278745c4d8eSJeff Mahoney 	ret = copy_to_user((void *)(unsigned long)ipa->fspath,
4279745c4d8eSJeff Mahoney 			   (void *)(unsigned long)ipath->fspath, size);
4280d7728c96SJan Schmidt 	if (ret) {
4281d7728c96SJan Schmidt 		ret = -EFAULT;
4282d7728c96SJan Schmidt 		goto out;
4283d7728c96SJan Schmidt 	}
4284d7728c96SJan Schmidt 
4285d7728c96SJan Schmidt out:
4286d7728c96SJan Schmidt 	btrfs_free_path(path);
4287d7728c96SJan Schmidt 	free_ipath(ipath);
4288d7728c96SJan Schmidt 	kfree(ipa);
4289d7728c96SJan Schmidt 
4290d7728c96SJan Schmidt 	return ret;
4291d7728c96SJan Schmidt }
4292d7728c96SJan Schmidt 
4293d7728c96SJan Schmidt static int build_ino_list(u64 inum, u64 offset, u64 root, void *ctx)
4294d7728c96SJan Schmidt {
4295d7728c96SJan Schmidt 	struct btrfs_data_container *inodes = ctx;
4296d7728c96SJan Schmidt 	const size_t c = 3 * sizeof(u64);
4297d7728c96SJan Schmidt 
4298d7728c96SJan Schmidt 	if (inodes->bytes_left >= c) {
4299d7728c96SJan Schmidt 		inodes->bytes_left -= c;
4300d7728c96SJan Schmidt 		inodes->val[inodes->elem_cnt] = inum;
4301d7728c96SJan Schmidt 		inodes->val[inodes->elem_cnt + 1] = offset;
4302d7728c96SJan Schmidt 		inodes->val[inodes->elem_cnt + 2] = root;
4303d7728c96SJan Schmidt 		inodes->elem_cnt += 3;
4304d7728c96SJan Schmidt 	} else {
4305d7728c96SJan Schmidt 		inodes->bytes_missing += c - inodes->bytes_left;
4306d7728c96SJan Schmidt 		inodes->bytes_left = 0;
4307d7728c96SJan Schmidt 		inodes->elem_missed += 3;
4308d7728c96SJan Schmidt 	}
4309d7728c96SJan Schmidt 
4310d7728c96SJan Schmidt 	return 0;
4311d7728c96SJan Schmidt }
4312d7728c96SJan Schmidt 
4313d7728c96SJan Schmidt static long btrfs_ioctl_logical_to_ino(struct btrfs_root *root,
4314d7728c96SJan Schmidt 					void __user *arg)
4315d7728c96SJan Schmidt {
4316d7728c96SJan Schmidt 	int ret = 0;
4317d7728c96SJan Schmidt 	int size;
4318d7728c96SJan Schmidt 	struct btrfs_ioctl_logical_ino_args *loi;
4319d7728c96SJan Schmidt 	struct btrfs_data_container *inodes = NULL;
4320d7728c96SJan Schmidt 	struct btrfs_path *path = NULL;
4321d7728c96SJan Schmidt 
4322d7728c96SJan Schmidt 	if (!capable(CAP_SYS_ADMIN))
4323d7728c96SJan Schmidt 		return -EPERM;
4324d7728c96SJan Schmidt 
4325d7728c96SJan Schmidt 	loi = memdup_user(arg, sizeof(*loi));
4326d7728c96SJan Schmidt 	if (IS_ERR(loi)) {
4327d7728c96SJan Schmidt 		ret = PTR_ERR(loi);
4328d7728c96SJan Schmidt 		loi = NULL;
4329d7728c96SJan Schmidt 		goto out;
4330d7728c96SJan Schmidt 	}
4331d7728c96SJan Schmidt 
4332d7728c96SJan Schmidt 	path = btrfs_alloc_path();
4333d7728c96SJan Schmidt 	if (!path) {
4334d7728c96SJan Schmidt 		ret = -ENOMEM;
4335d7728c96SJan Schmidt 		goto out;
4336d7728c96SJan Schmidt 	}
4337d7728c96SJan Schmidt 
4338425d17a2SLiu Bo 	size = min_t(u32, loi->size, 64 * 1024);
4339d7728c96SJan Schmidt 	inodes = init_data_container(size);
4340d7728c96SJan Schmidt 	if (IS_ERR(inodes)) {
4341d7728c96SJan Schmidt 		ret = PTR_ERR(inodes);
4342d7728c96SJan Schmidt 		inodes = NULL;
4343d7728c96SJan Schmidt 		goto out;
4344d7728c96SJan Schmidt 	}
4345d7728c96SJan Schmidt 
4346df031f07SLiu Bo 	ret = iterate_inodes_from_logical(loi->logical, root->fs_info, path,
4347df031f07SLiu Bo 					  build_ino_list, inodes);
4348df031f07SLiu Bo 	if (ret == -EINVAL)
4349d7728c96SJan Schmidt 		ret = -ENOENT;
4350d7728c96SJan Schmidt 	if (ret < 0)
4351d7728c96SJan Schmidt 		goto out;
4352d7728c96SJan Schmidt 
4353745c4d8eSJeff Mahoney 	ret = copy_to_user((void *)(unsigned long)loi->inodes,
4354745c4d8eSJeff Mahoney 			   (void *)(unsigned long)inodes, size);
4355d7728c96SJan Schmidt 	if (ret)
4356d7728c96SJan Schmidt 		ret = -EFAULT;
4357d7728c96SJan Schmidt 
4358d7728c96SJan Schmidt out:
4359d7728c96SJan Schmidt 	btrfs_free_path(path);
4360425d17a2SLiu Bo 	vfree(inodes);
4361d7728c96SJan Schmidt 	kfree(loi);
4362d7728c96SJan Schmidt 
4363d7728c96SJan Schmidt 	return ret;
4364d7728c96SJan Schmidt }
4365d7728c96SJan Schmidt 
436619a39dceSIlya Dryomov void update_ioctl_balance_args(struct btrfs_fs_info *fs_info, int lock,
4367c9e9f97bSIlya Dryomov 			       struct btrfs_ioctl_balance_args *bargs)
4368c9e9f97bSIlya Dryomov {
4369c9e9f97bSIlya Dryomov 	struct btrfs_balance_control *bctl = fs_info->balance_ctl;
4370c9e9f97bSIlya Dryomov 
4371c9e9f97bSIlya Dryomov 	bargs->flags = bctl->flags;
4372c9e9f97bSIlya Dryomov 
4373837d5b6eSIlya Dryomov 	if (atomic_read(&fs_info->balance_running))
4374837d5b6eSIlya Dryomov 		bargs->state |= BTRFS_BALANCE_STATE_RUNNING;
4375837d5b6eSIlya Dryomov 	if (atomic_read(&fs_info->balance_pause_req))
4376837d5b6eSIlya Dryomov 		bargs->state |= BTRFS_BALANCE_STATE_PAUSE_REQ;
4377a7e99c69SIlya Dryomov 	if (atomic_read(&fs_info->balance_cancel_req))
4378a7e99c69SIlya Dryomov 		bargs->state |= BTRFS_BALANCE_STATE_CANCEL_REQ;
4379837d5b6eSIlya Dryomov 
4380c9e9f97bSIlya Dryomov 	memcpy(&bargs->data, &bctl->data, sizeof(bargs->data));
4381c9e9f97bSIlya Dryomov 	memcpy(&bargs->meta, &bctl->meta, sizeof(bargs->meta));
4382c9e9f97bSIlya Dryomov 	memcpy(&bargs->sys, &bctl->sys, sizeof(bargs->sys));
438319a39dceSIlya Dryomov 
438419a39dceSIlya Dryomov 	if (lock) {
438519a39dceSIlya Dryomov 		spin_lock(&fs_info->balance_lock);
438619a39dceSIlya Dryomov 		memcpy(&bargs->stat, &bctl->stat, sizeof(bargs->stat));
438719a39dceSIlya Dryomov 		spin_unlock(&fs_info->balance_lock);
438819a39dceSIlya Dryomov 	} else {
438919a39dceSIlya Dryomov 		memcpy(&bargs->stat, &bctl->stat, sizeof(bargs->stat));
439019a39dceSIlya Dryomov 	}
4391c9e9f97bSIlya Dryomov }
4392c9e9f97bSIlya Dryomov 
43939ba1f6e4SLiu Bo static long btrfs_ioctl_balance(struct file *file, void __user *arg)
4394c9e9f97bSIlya Dryomov {
4395496ad9aaSAl Viro 	struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
4396c9e9f97bSIlya Dryomov 	struct btrfs_fs_info *fs_info = root->fs_info;
4397c9e9f97bSIlya Dryomov 	struct btrfs_ioctl_balance_args *bargs;
4398c9e9f97bSIlya Dryomov 	struct btrfs_balance_control *bctl;
4399ed0fb78fSIlya Dryomov 	bool need_unlock; /* for mut. excl. ops lock */
4400c9e9f97bSIlya Dryomov 	int ret;
4401c9e9f97bSIlya Dryomov 
4402c9e9f97bSIlya Dryomov 	if (!capable(CAP_SYS_ADMIN))
4403c9e9f97bSIlya Dryomov 		return -EPERM;
4404c9e9f97bSIlya Dryomov 
4405e54bfa31SLiu Bo 	ret = mnt_want_write_file(file);
44069ba1f6e4SLiu Bo 	if (ret)
44079ba1f6e4SLiu Bo 		return ret;
44089ba1f6e4SLiu Bo 
4409ed0fb78fSIlya Dryomov again:
4410ed0fb78fSIlya Dryomov 	if (!atomic_xchg(&fs_info->mutually_exclusive_operation_running, 1)) {
4411c9e9f97bSIlya Dryomov 		mutex_lock(&fs_info->volume_mutex);
4412c9e9f97bSIlya Dryomov 		mutex_lock(&fs_info->balance_mutex);
4413ed0fb78fSIlya Dryomov 		need_unlock = true;
4414ed0fb78fSIlya Dryomov 		goto locked;
4415ed0fb78fSIlya Dryomov 	}
4416ed0fb78fSIlya Dryomov 
4417ed0fb78fSIlya Dryomov 	/*
4418ed0fb78fSIlya Dryomov 	 * mut. excl. ops lock is locked.  Three possibilites:
4419ed0fb78fSIlya Dryomov 	 *   (1) some other op is running
4420ed0fb78fSIlya Dryomov 	 *   (2) balance is running
4421ed0fb78fSIlya Dryomov 	 *   (3) balance is paused -- special case (think resume)
4422ed0fb78fSIlya Dryomov 	 */
4423ed0fb78fSIlya Dryomov 	mutex_lock(&fs_info->balance_mutex);
4424ed0fb78fSIlya Dryomov 	if (fs_info->balance_ctl) {
4425ed0fb78fSIlya Dryomov 		/* this is either (2) or (3) */
4426ed0fb78fSIlya Dryomov 		if (!atomic_read(&fs_info->balance_running)) {
4427ed0fb78fSIlya Dryomov 			mutex_unlock(&fs_info->balance_mutex);
4428ed0fb78fSIlya Dryomov 			if (!mutex_trylock(&fs_info->volume_mutex))
4429ed0fb78fSIlya Dryomov 				goto again;
4430ed0fb78fSIlya Dryomov 			mutex_lock(&fs_info->balance_mutex);
4431ed0fb78fSIlya Dryomov 
4432ed0fb78fSIlya Dryomov 			if (fs_info->balance_ctl &&
4433ed0fb78fSIlya Dryomov 			    !atomic_read(&fs_info->balance_running)) {
4434ed0fb78fSIlya Dryomov 				/* this is (3) */
4435ed0fb78fSIlya Dryomov 				need_unlock = false;
4436ed0fb78fSIlya Dryomov 				goto locked;
4437ed0fb78fSIlya Dryomov 			}
4438ed0fb78fSIlya Dryomov 
4439ed0fb78fSIlya Dryomov 			mutex_unlock(&fs_info->balance_mutex);
4440ed0fb78fSIlya Dryomov 			mutex_unlock(&fs_info->volume_mutex);
4441ed0fb78fSIlya Dryomov 			goto again;
4442ed0fb78fSIlya Dryomov 		} else {
4443ed0fb78fSIlya Dryomov 			/* this is (2) */
4444ed0fb78fSIlya Dryomov 			mutex_unlock(&fs_info->balance_mutex);
4445ed0fb78fSIlya Dryomov 			ret = -EINPROGRESS;
4446ed0fb78fSIlya Dryomov 			goto out;
4447ed0fb78fSIlya Dryomov 		}
4448ed0fb78fSIlya Dryomov 	} else {
4449ed0fb78fSIlya Dryomov 		/* this is (1) */
4450ed0fb78fSIlya Dryomov 		mutex_unlock(&fs_info->balance_mutex);
4451e57138b3SAnand Jain 		ret = BTRFS_ERROR_DEV_EXCL_RUN_IN_PROGRESS;
4452ed0fb78fSIlya Dryomov 		goto out;
4453ed0fb78fSIlya Dryomov 	}
4454ed0fb78fSIlya Dryomov 
4455ed0fb78fSIlya Dryomov locked:
4456ed0fb78fSIlya Dryomov 	BUG_ON(!atomic_read(&fs_info->mutually_exclusive_operation_running));
4457c9e9f97bSIlya Dryomov 
4458c9e9f97bSIlya Dryomov 	if (arg) {
4459c9e9f97bSIlya Dryomov 		bargs = memdup_user(arg, sizeof(*bargs));
4460c9e9f97bSIlya Dryomov 		if (IS_ERR(bargs)) {
4461c9e9f97bSIlya Dryomov 			ret = PTR_ERR(bargs);
4462ed0fb78fSIlya Dryomov 			goto out_unlock;
4463c9e9f97bSIlya Dryomov 		}
4464de322263SIlya Dryomov 
4465de322263SIlya Dryomov 		if (bargs->flags & BTRFS_BALANCE_RESUME) {
4466de322263SIlya Dryomov 			if (!fs_info->balance_ctl) {
4467de322263SIlya Dryomov 				ret = -ENOTCONN;
4468de322263SIlya Dryomov 				goto out_bargs;
4469de322263SIlya Dryomov 			}
4470de322263SIlya Dryomov 
4471de322263SIlya Dryomov 			bctl = fs_info->balance_ctl;
4472de322263SIlya Dryomov 			spin_lock(&fs_info->balance_lock);
4473de322263SIlya Dryomov 			bctl->flags |= BTRFS_BALANCE_RESUME;
4474de322263SIlya Dryomov 			spin_unlock(&fs_info->balance_lock);
4475de322263SIlya Dryomov 
4476de322263SIlya Dryomov 			goto do_balance;
4477de322263SIlya Dryomov 		}
4478c9e9f97bSIlya Dryomov 	} else {
4479c9e9f97bSIlya Dryomov 		bargs = NULL;
4480c9e9f97bSIlya Dryomov 	}
4481c9e9f97bSIlya Dryomov 
4482ed0fb78fSIlya Dryomov 	if (fs_info->balance_ctl) {
4483837d5b6eSIlya Dryomov 		ret = -EINPROGRESS;
4484837d5b6eSIlya Dryomov 		goto out_bargs;
4485837d5b6eSIlya Dryomov 	}
4486837d5b6eSIlya Dryomov 
4487c9e9f97bSIlya Dryomov 	bctl = kzalloc(sizeof(*bctl), GFP_NOFS);
4488c9e9f97bSIlya Dryomov 	if (!bctl) {
4489c9e9f97bSIlya Dryomov 		ret = -ENOMEM;
4490c9e9f97bSIlya Dryomov 		goto out_bargs;
4491c9e9f97bSIlya Dryomov 	}
4492c9e9f97bSIlya Dryomov 
4493c9e9f97bSIlya Dryomov 	bctl->fs_info = fs_info;
4494c9e9f97bSIlya Dryomov 	if (arg) {
4495c9e9f97bSIlya Dryomov 		memcpy(&bctl->data, &bargs->data, sizeof(bctl->data));
4496c9e9f97bSIlya Dryomov 		memcpy(&bctl->meta, &bargs->meta, sizeof(bctl->meta));
4497c9e9f97bSIlya Dryomov 		memcpy(&bctl->sys, &bargs->sys, sizeof(bctl->sys));
4498c9e9f97bSIlya Dryomov 
4499c9e9f97bSIlya Dryomov 		bctl->flags = bargs->flags;
4500f43ffb60SIlya Dryomov 	} else {
4501f43ffb60SIlya Dryomov 		/* balance everything - no filters */
4502f43ffb60SIlya Dryomov 		bctl->flags |= BTRFS_BALANCE_TYPE_MASK;
4503c9e9f97bSIlya Dryomov 	}
4504c9e9f97bSIlya Dryomov 
4505de322263SIlya Dryomov do_balance:
4506c9e9f97bSIlya Dryomov 	/*
4507ed0fb78fSIlya Dryomov 	 * Ownership of bctl and mutually_exclusive_operation_running
4508ed0fb78fSIlya Dryomov 	 * goes to to btrfs_balance.  bctl is freed in __cancel_balance,
4509ed0fb78fSIlya Dryomov 	 * or, if restriper was paused all the way until unmount, in
4510ed0fb78fSIlya Dryomov 	 * free_fs_info.  mutually_exclusive_operation_running is
4511ed0fb78fSIlya Dryomov 	 * cleared in __cancel_balance.
4512c9e9f97bSIlya Dryomov 	 */
4513ed0fb78fSIlya Dryomov 	need_unlock = false;
4514ed0fb78fSIlya Dryomov 
4515ed0fb78fSIlya Dryomov 	ret = btrfs_balance(bctl, bargs);
4516ed0fb78fSIlya Dryomov 
4517c9e9f97bSIlya Dryomov 	if (arg) {
4518c9e9f97bSIlya Dryomov 		if (copy_to_user(arg, bargs, sizeof(*bargs)))
4519c9e9f97bSIlya Dryomov 			ret = -EFAULT;
4520c9e9f97bSIlya Dryomov 	}
4521c9e9f97bSIlya Dryomov 
4522c9e9f97bSIlya Dryomov out_bargs:
4523c9e9f97bSIlya Dryomov 	kfree(bargs);
4524ed0fb78fSIlya Dryomov out_unlock:
4525c9e9f97bSIlya Dryomov 	mutex_unlock(&fs_info->balance_mutex);
4526c9e9f97bSIlya Dryomov 	mutex_unlock(&fs_info->volume_mutex);
4527ed0fb78fSIlya Dryomov 	if (need_unlock)
4528ed0fb78fSIlya Dryomov 		atomic_set(&fs_info->mutually_exclusive_operation_running, 0);
4529ed0fb78fSIlya Dryomov out:
4530e54bfa31SLiu Bo 	mnt_drop_write_file(file);
4531c9e9f97bSIlya Dryomov 	return ret;
4532c9e9f97bSIlya Dryomov }
4533c9e9f97bSIlya Dryomov 
4534837d5b6eSIlya Dryomov static long btrfs_ioctl_balance_ctl(struct btrfs_root *root, int cmd)
4535837d5b6eSIlya Dryomov {
4536837d5b6eSIlya Dryomov 	if (!capable(CAP_SYS_ADMIN))
4537837d5b6eSIlya Dryomov 		return -EPERM;
4538837d5b6eSIlya Dryomov 
4539837d5b6eSIlya Dryomov 	switch (cmd) {
4540837d5b6eSIlya Dryomov 	case BTRFS_BALANCE_CTL_PAUSE:
4541837d5b6eSIlya Dryomov 		return btrfs_pause_balance(root->fs_info);
4542a7e99c69SIlya Dryomov 	case BTRFS_BALANCE_CTL_CANCEL:
4543a7e99c69SIlya Dryomov 		return btrfs_cancel_balance(root->fs_info);
4544837d5b6eSIlya Dryomov 	}
4545837d5b6eSIlya Dryomov 
4546837d5b6eSIlya Dryomov 	return -EINVAL;
4547837d5b6eSIlya Dryomov }
4548837d5b6eSIlya Dryomov 
454919a39dceSIlya Dryomov static long btrfs_ioctl_balance_progress(struct btrfs_root *root,
455019a39dceSIlya Dryomov 					 void __user *arg)
455119a39dceSIlya Dryomov {
455219a39dceSIlya Dryomov 	struct btrfs_fs_info *fs_info = root->fs_info;
455319a39dceSIlya Dryomov 	struct btrfs_ioctl_balance_args *bargs;
455419a39dceSIlya Dryomov 	int ret = 0;
455519a39dceSIlya Dryomov 
455619a39dceSIlya Dryomov 	if (!capable(CAP_SYS_ADMIN))
455719a39dceSIlya Dryomov 		return -EPERM;
455819a39dceSIlya Dryomov 
455919a39dceSIlya Dryomov 	mutex_lock(&fs_info->balance_mutex);
456019a39dceSIlya Dryomov 	if (!fs_info->balance_ctl) {
456119a39dceSIlya Dryomov 		ret = -ENOTCONN;
456219a39dceSIlya Dryomov 		goto out;
456319a39dceSIlya Dryomov 	}
456419a39dceSIlya Dryomov 
456519a39dceSIlya Dryomov 	bargs = kzalloc(sizeof(*bargs), GFP_NOFS);
456619a39dceSIlya Dryomov 	if (!bargs) {
456719a39dceSIlya Dryomov 		ret = -ENOMEM;
456819a39dceSIlya Dryomov 		goto out;
456919a39dceSIlya Dryomov 	}
457019a39dceSIlya Dryomov 
457119a39dceSIlya Dryomov 	update_ioctl_balance_args(fs_info, 1, bargs);
457219a39dceSIlya Dryomov 
457319a39dceSIlya Dryomov 	if (copy_to_user(arg, bargs, sizeof(*bargs)))
457419a39dceSIlya Dryomov 		ret = -EFAULT;
457519a39dceSIlya Dryomov 
457619a39dceSIlya Dryomov 	kfree(bargs);
457719a39dceSIlya Dryomov out:
457819a39dceSIlya Dryomov 	mutex_unlock(&fs_info->balance_mutex);
457919a39dceSIlya Dryomov 	return ret;
458019a39dceSIlya Dryomov }
458119a39dceSIlya Dryomov 
4582905b0ddaSMiao Xie static long btrfs_ioctl_quota_ctl(struct file *file, void __user *arg)
45835d13a37bSArne Jansen {
4584496ad9aaSAl Viro 	struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
45855d13a37bSArne Jansen 	struct btrfs_ioctl_quota_ctl_args *sa;
45865d13a37bSArne Jansen 	struct btrfs_trans_handle *trans = NULL;
45875d13a37bSArne Jansen 	int ret;
45885d13a37bSArne Jansen 	int err;
45895d13a37bSArne Jansen 
45905d13a37bSArne Jansen 	if (!capable(CAP_SYS_ADMIN))
45915d13a37bSArne Jansen 		return -EPERM;
45925d13a37bSArne Jansen 
4593905b0ddaSMiao Xie 	ret = mnt_want_write_file(file);
4594905b0ddaSMiao Xie 	if (ret)
4595905b0ddaSMiao Xie 		return ret;
45965d13a37bSArne Jansen 
45975d13a37bSArne Jansen 	sa = memdup_user(arg, sizeof(*sa));
4598905b0ddaSMiao Xie 	if (IS_ERR(sa)) {
4599905b0ddaSMiao Xie 		ret = PTR_ERR(sa);
4600905b0ddaSMiao Xie 		goto drop_write;
4601905b0ddaSMiao Xie 	}
46025d13a37bSArne Jansen 
46037708f029SWang Shilong 	down_write(&root->fs_info->subvol_sem);
460492f183aaSWang Shilong 	trans = btrfs_start_transaction(root->fs_info->tree_root, 2);
46055d13a37bSArne Jansen 	if (IS_ERR(trans)) {
46065d13a37bSArne Jansen 		ret = PTR_ERR(trans);
46075d13a37bSArne Jansen 		goto out;
46085d13a37bSArne Jansen 	}
46095d13a37bSArne Jansen 
46105d13a37bSArne Jansen 	switch (sa->cmd) {
46115d13a37bSArne Jansen 	case BTRFS_QUOTA_CTL_ENABLE:
46125d13a37bSArne Jansen 		ret = btrfs_quota_enable(trans, root->fs_info);
46135d13a37bSArne Jansen 		break;
46145d13a37bSArne Jansen 	case BTRFS_QUOTA_CTL_DISABLE:
46155d13a37bSArne Jansen 		ret = btrfs_quota_disable(trans, root->fs_info);
46165d13a37bSArne Jansen 		break;
46175d13a37bSArne Jansen 	default:
46185d13a37bSArne Jansen 		ret = -EINVAL;
46195d13a37bSArne Jansen 		break;
46205d13a37bSArne Jansen 	}
46215d13a37bSArne Jansen 
462292f183aaSWang Shilong 	err = btrfs_commit_transaction(trans, root->fs_info->tree_root);
46235d13a37bSArne Jansen 	if (err && !ret)
46245d13a37bSArne Jansen 		ret = err;
46255d13a37bSArne Jansen out:
46265d13a37bSArne Jansen 	kfree(sa);
46277708f029SWang Shilong 	up_write(&root->fs_info->subvol_sem);
4628905b0ddaSMiao Xie drop_write:
4629905b0ddaSMiao Xie 	mnt_drop_write_file(file);
46305d13a37bSArne Jansen 	return ret;
46315d13a37bSArne Jansen }
46325d13a37bSArne Jansen 
4633905b0ddaSMiao Xie static long btrfs_ioctl_qgroup_assign(struct file *file, void __user *arg)
46345d13a37bSArne Jansen {
4635496ad9aaSAl Viro 	struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
46365d13a37bSArne Jansen 	struct btrfs_ioctl_qgroup_assign_args *sa;
46375d13a37bSArne Jansen 	struct btrfs_trans_handle *trans;
46385d13a37bSArne Jansen 	int ret;
46395d13a37bSArne Jansen 	int err;
46405d13a37bSArne Jansen 
46415d13a37bSArne Jansen 	if (!capable(CAP_SYS_ADMIN))
46425d13a37bSArne Jansen 		return -EPERM;
46435d13a37bSArne Jansen 
4644905b0ddaSMiao Xie 	ret = mnt_want_write_file(file);
4645905b0ddaSMiao Xie 	if (ret)
4646905b0ddaSMiao Xie 		return ret;
46475d13a37bSArne Jansen 
46485d13a37bSArne Jansen 	sa = memdup_user(arg, sizeof(*sa));
4649905b0ddaSMiao Xie 	if (IS_ERR(sa)) {
4650905b0ddaSMiao Xie 		ret = PTR_ERR(sa);
4651905b0ddaSMiao Xie 		goto drop_write;
4652905b0ddaSMiao Xie 	}
46535d13a37bSArne Jansen 
46545d13a37bSArne Jansen 	trans = btrfs_join_transaction(root);
46555d13a37bSArne Jansen 	if (IS_ERR(trans)) {
46565d13a37bSArne Jansen 		ret = PTR_ERR(trans);
46575d13a37bSArne Jansen 		goto out;
46585d13a37bSArne Jansen 	}
46595d13a37bSArne Jansen 
46605d13a37bSArne Jansen 	/* FIXME: check if the IDs really exist */
46615d13a37bSArne Jansen 	if (sa->assign) {
46625d13a37bSArne Jansen 		ret = btrfs_add_qgroup_relation(trans, root->fs_info,
46635d13a37bSArne Jansen 						sa->src, sa->dst);
46645d13a37bSArne Jansen 	} else {
46655d13a37bSArne Jansen 		ret = btrfs_del_qgroup_relation(trans, root->fs_info,
46665d13a37bSArne Jansen 						sa->src, sa->dst);
46675d13a37bSArne Jansen 	}
46685d13a37bSArne Jansen 
46695d13a37bSArne Jansen 	err = btrfs_end_transaction(trans, root);
46705d13a37bSArne Jansen 	if (err && !ret)
46715d13a37bSArne Jansen 		ret = err;
46725d13a37bSArne Jansen 
46735d13a37bSArne Jansen out:
46745d13a37bSArne Jansen 	kfree(sa);
4675905b0ddaSMiao Xie drop_write:
4676905b0ddaSMiao Xie 	mnt_drop_write_file(file);
46775d13a37bSArne Jansen 	return ret;
46785d13a37bSArne Jansen }
46795d13a37bSArne Jansen 
4680905b0ddaSMiao Xie static long btrfs_ioctl_qgroup_create(struct file *file, void __user *arg)
46815d13a37bSArne Jansen {
4682496ad9aaSAl Viro 	struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
46835d13a37bSArne Jansen 	struct btrfs_ioctl_qgroup_create_args *sa;
46845d13a37bSArne Jansen 	struct btrfs_trans_handle *trans;
46855d13a37bSArne Jansen 	int ret;
46865d13a37bSArne Jansen 	int err;
46875d13a37bSArne Jansen 
46885d13a37bSArne Jansen 	if (!capable(CAP_SYS_ADMIN))
46895d13a37bSArne Jansen 		return -EPERM;
46905d13a37bSArne Jansen 
4691905b0ddaSMiao Xie 	ret = mnt_want_write_file(file);
4692905b0ddaSMiao Xie 	if (ret)
4693905b0ddaSMiao Xie 		return ret;
46945d13a37bSArne Jansen 
46955d13a37bSArne Jansen 	sa = memdup_user(arg, sizeof(*sa));
4696905b0ddaSMiao Xie 	if (IS_ERR(sa)) {
4697905b0ddaSMiao Xie 		ret = PTR_ERR(sa);
4698905b0ddaSMiao Xie 		goto drop_write;
4699905b0ddaSMiao Xie 	}
47005d13a37bSArne Jansen 
4701d86e56cfSMiao Xie 	if (!sa->qgroupid) {
4702d86e56cfSMiao Xie 		ret = -EINVAL;
4703d86e56cfSMiao Xie 		goto out;
4704d86e56cfSMiao Xie 	}
4705d86e56cfSMiao Xie 
47065d13a37bSArne Jansen 	trans = btrfs_join_transaction(root);
47075d13a37bSArne Jansen 	if (IS_ERR(trans)) {
47085d13a37bSArne Jansen 		ret = PTR_ERR(trans);
47095d13a37bSArne Jansen 		goto out;
47105d13a37bSArne Jansen 	}
47115d13a37bSArne Jansen 
47125d13a37bSArne Jansen 	/* FIXME: check if the IDs really exist */
47135d13a37bSArne Jansen 	if (sa->create) {
47145d13a37bSArne Jansen 		ret = btrfs_create_qgroup(trans, root->fs_info, sa->qgroupid,
47155d13a37bSArne Jansen 					  NULL);
47165d13a37bSArne Jansen 	} else {
47175d13a37bSArne Jansen 		ret = btrfs_remove_qgroup(trans, root->fs_info, sa->qgroupid);
47185d13a37bSArne Jansen 	}
47195d13a37bSArne Jansen 
47205d13a37bSArne Jansen 	err = btrfs_end_transaction(trans, root);
47215d13a37bSArne Jansen 	if (err && !ret)
47225d13a37bSArne Jansen 		ret = err;
47235d13a37bSArne Jansen 
47245d13a37bSArne Jansen out:
47255d13a37bSArne Jansen 	kfree(sa);
4726905b0ddaSMiao Xie drop_write:
4727905b0ddaSMiao Xie 	mnt_drop_write_file(file);
47285d13a37bSArne Jansen 	return ret;
47295d13a37bSArne Jansen }
47305d13a37bSArne Jansen 
4731905b0ddaSMiao Xie static long btrfs_ioctl_qgroup_limit(struct file *file, void __user *arg)
47325d13a37bSArne Jansen {
4733496ad9aaSAl Viro 	struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
47345d13a37bSArne Jansen 	struct btrfs_ioctl_qgroup_limit_args *sa;
47355d13a37bSArne Jansen 	struct btrfs_trans_handle *trans;
47365d13a37bSArne Jansen 	int ret;
47375d13a37bSArne Jansen 	int err;
47385d13a37bSArne Jansen 	u64 qgroupid;
47395d13a37bSArne Jansen 
47405d13a37bSArne Jansen 	if (!capable(CAP_SYS_ADMIN))
47415d13a37bSArne Jansen 		return -EPERM;
47425d13a37bSArne Jansen 
4743905b0ddaSMiao Xie 	ret = mnt_want_write_file(file);
4744905b0ddaSMiao Xie 	if (ret)
4745905b0ddaSMiao Xie 		return ret;
47465d13a37bSArne Jansen 
47475d13a37bSArne Jansen 	sa = memdup_user(arg, sizeof(*sa));
4748905b0ddaSMiao Xie 	if (IS_ERR(sa)) {
4749905b0ddaSMiao Xie 		ret = PTR_ERR(sa);
4750905b0ddaSMiao Xie 		goto drop_write;
4751905b0ddaSMiao Xie 	}
47525d13a37bSArne Jansen 
47535d13a37bSArne Jansen 	trans = btrfs_join_transaction(root);
47545d13a37bSArne Jansen 	if (IS_ERR(trans)) {
47555d13a37bSArne Jansen 		ret = PTR_ERR(trans);
47565d13a37bSArne Jansen 		goto out;
47575d13a37bSArne Jansen 	}
47585d13a37bSArne Jansen 
47595d13a37bSArne Jansen 	qgroupid = sa->qgroupid;
47605d13a37bSArne Jansen 	if (!qgroupid) {
47615d13a37bSArne Jansen 		/* take the current subvol as qgroup */
47625d13a37bSArne Jansen 		qgroupid = root->root_key.objectid;
47635d13a37bSArne Jansen 	}
47645d13a37bSArne Jansen 
47655d13a37bSArne Jansen 	/* FIXME: check if the IDs really exist */
47665d13a37bSArne Jansen 	ret = btrfs_limit_qgroup(trans, root->fs_info, qgroupid, &sa->lim);
47675d13a37bSArne Jansen 
47685d13a37bSArne Jansen 	err = btrfs_end_transaction(trans, root);
47695d13a37bSArne Jansen 	if (err && !ret)
47705d13a37bSArne Jansen 		ret = err;
47715d13a37bSArne Jansen 
47725d13a37bSArne Jansen out:
47735d13a37bSArne Jansen 	kfree(sa);
4774905b0ddaSMiao Xie drop_write:
4775905b0ddaSMiao Xie 	mnt_drop_write_file(file);
47765d13a37bSArne Jansen 	return ret;
47775d13a37bSArne Jansen }
47785d13a37bSArne Jansen 
47792f232036SJan Schmidt static long btrfs_ioctl_quota_rescan(struct file *file, void __user *arg)
47802f232036SJan Schmidt {
47816d0379ecSAl Viro 	struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
47822f232036SJan Schmidt 	struct btrfs_ioctl_quota_rescan_args *qsa;
47832f232036SJan Schmidt 	int ret;
47842f232036SJan Schmidt 
47852f232036SJan Schmidt 	if (!capable(CAP_SYS_ADMIN))
47862f232036SJan Schmidt 		return -EPERM;
47872f232036SJan Schmidt 
47882f232036SJan Schmidt 	ret = mnt_want_write_file(file);
47892f232036SJan Schmidt 	if (ret)
47902f232036SJan Schmidt 		return ret;
47912f232036SJan Schmidt 
47922f232036SJan Schmidt 	qsa = memdup_user(arg, sizeof(*qsa));
47932f232036SJan Schmidt 	if (IS_ERR(qsa)) {
47942f232036SJan Schmidt 		ret = PTR_ERR(qsa);
47952f232036SJan Schmidt 		goto drop_write;
47962f232036SJan Schmidt 	}
47972f232036SJan Schmidt 
47982f232036SJan Schmidt 	if (qsa->flags) {
47992f232036SJan Schmidt 		ret = -EINVAL;
48002f232036SJan Schmidt 		goto out;
48012f232036SJan Schmidt 	}
48022f232036SJan Schmidt 
48032f232036SJan Schmidt 	ret = btrfs_qgroup_rescan(root->fs_info);
48042f232036SJan Schmidt 
48052f232036SJan Schmidt out:
48062f232036SJan Schmidt 	kfree(qsa);
48072f232036SJan Schmidt drop_write:
48082f232036SJan Schmidt 	mnt_drop_write_file(file);
48092f232036SJan Schmidt 	return ret;
48102f232036SJan Schmidt }
48112f232036SJan Schmidt 
48122f232036SJan Schmidt static long btrfs_ioctl_quota_rescan_status(struct file *file, void __user *arg)
48132f232036SJan Schmidt {
48146d0379ecSAl Viro 	struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
48152f232036SJan Schmidt 	struct btrfs_ioctl_quota_rescan_args *qsa;
48162f232036SJan Schmidt 	int ret = 0;
48172f232036SJan Schmidt 
48182f232036SJan Schmidt 	if (!capable(CAP_SYS_ADMIN))
48192f232036SJan Schmidt 		return -EPERM;
48202f232036SJan Schmidt 
48212f232036SJan Schmidt 	qsa = kzalloc(sizeof(*qsa), GFP_NOFS);
48222f232036SJan Schmidt 	if (!qsa)
48232f232036SJan Schmidt 		return -ENOMEM;
48242f232036SJan Schmidt 
48252f232036SJan Schmidt 	if (root->fs_info->qgroup_flags & BTRFS_QGROUP_STATUS_FLAG_RESCAN) {
48262f232036SJan Schmidt 		qsa->flags = 1;
48272f232036SJan Schmidt 		qsa->progress = root->fs_info->qgroup_rescan_progress.objectid;
48282f232036SJan Schmidt 	}
48292f232036SJan Schmidt 
48302f232036SJan Schmidt 	if (copy_to_user(arg, qsa, sizeof(*qsa)))
48312f232036SJan Schmidt 		ret = -EFAULT;
48322f232036SJan Schmidt 
48332f232036SJan Schmidt 	kfree(qsa);
48342f232036SJan Schmidt 	return ret;
48352f232036SJan Schmidt }
48362f232036SJan Schmidt 
483757254b6eSJan Schmidt static long btrfs_ioctl_quota_rescan_wait(struct file *file, void __user *arg)
483857254b6eSJan Schmidt {
483954563d41SAl Viro 	struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
484057254b6eSJan Schmidt 
484157254b6eSJan Schmidt 	if (!capable(CAP_SYS_ADMIN))
484257254b6eSJan Schmidt 		return -EPERM;
484357254b6eSJan Schmidt 
484457254b6eSJan Schmidt 	return btrfs_qgroup_wait_for_completion(root->fs_info);
484557254b6eSJan Schmidt }
484657254b6eSJan Schmidt 
4847abccd00fSHugo Mills static long _btrfs_ioctl_set_received_subvol(struct file *file,
4848abccd00fSHugo Mills 					    struct btrfs_ioctl_received_subvol_args *sa)
48498ea05e3aSAlexander Block {
4850496ad9aaSAl Viro 	struct inode *inode = file_inode(file);
48518ea05e3aSAlexander Block 	struct btrfs_root *root = BTRFS_I(inode)->root;
48528ea05e3aSAlexander Block 	struct btrfs_root_item *root_item = &root->root_item;
48538ea05e3aSAlexander Block 	struct btrfs_trans_handle *trans;
48548ea05e3aSAlexander Block 	struct timespec ct = CURRENT_TIME;
48558ea05e3aSAlexander Block 	int ret = 0;
4856dd5f9615SStefan Behrens 	int received_uuid_changed;
48578ea05e3aSAlexander Block 
4858bd60ea0fSDavid Sterba 	if (!inode_owner_or_capable(inode))
4859bd60ea0fSDavid Sterba 		return -EPERM;
4860bd60ea0fSDavid Sterba 
48618ea05e3aSAlexander Block 	ret = mnt_want_write_file(file);
48628ea05e3aSAlexander Block 	if (ret < 0)
48638ea05e3aSAlexander Block 		return ret;
48648ea05e3aSAlexander Block 
48658ea05e3aSAlexander Block 	down_write(&root->fs_info->subvol_sem);
48668ea05e3aSAlexander Block 
48678ea05e3aSAlexander Block 	if (btrfs_ino(inode) != BTRFS_FIRST_FREE_OBJECTID) {
48688ea05e3aSAlexander Block 		ret = -EINVAL;
48698ea05e3aSAlexander Block 		goto out;
48708ea05e3aSAlexander Block 	}
48718ea05e3aSAlexander Block 
48728ea05e3aSAlexander Block 	if (btrfs_root_readonly(root)) {
48738ea05e3aSAlexander Block 		ret = -EROFS;
48748ea05e3aSAlexander Block 		goto out;
48758ea05e3aSAlexander Block 	}
48768ea05e3aSAlexander Block 
4877dd5f9615SStefan Behrens 	/*
4878dd5f9615SStefan Behrens 	 * 1 - root item
4879dd5f9615SStefan Behrens 	 * 2 - uuid items (received uuid + subvol uuid)
4880dd5f9615SStefan Behrens 	 */
4881dd5f9615SStefan Behrens 	trans = btrfs_start_transaction(root, 3);
48828ea05e3aSAlexander Block 	if (IS_ERR(trans)) {
48838ea05e3aSAlexander Block 		ret = PTR_ERR(trans);
48848ea05e3aSAlexander Block 		trans = NULL;
48858ea05e3aSAlexander Block 		goto out;
48868ea05e3aSAlexander Block 	}
48878ea05e3aSAlexander Block 
48888ea05e3aSAlexander Block 	sa->rtransid = trans->transid;
48898ea05e3aSAlexander Block 	sa->rtime.sec = ct.tv_sec;
48908ea05e3aSAlexander Block 	sa->rtime.nsec = ct.tv_nsec;
48918ea05e3aSAlexander Block 
4892dd5f9615SStefan Behrens 	received_uuid_changed = memcmp(root_item->received_uuid, sa->uuid,
4893dd5f9615SStefan Behrens 				       BTRFS_UUID_SIZE);
4894dd5f9615SStefan Behrens 	if (received_uuid_changed &&
4895dd5f9615SStefan Behrens 	    !btrfs_is_empty_uuid(root_item->received_uuid))
4896dd5f9615SStefan Behrens 		btrfs_uuid_tree_rem(trans, root->fs_info->uuid_root,
4897dd5f9615SStefan Behrens 				    root_item->received_uuid,
4898dd5f9615SStefan Behrens 				    BTRFS_UUID_KEY_RECEIVED_SUBVOL,
4899dd5f9615SStefan Behrens 				    root->root_key.objectid);
49008ea05e3aSAlexander Block 	memcpy(root_item->received_uuid, sa->uuid, BTRFS_UUID_SIZE);
49018ea05e3aSAlexander Block 	btrfs_set_root_stransid(root_item, sa->stransid);
49028ea05e3aSAlexander Block 	btrfs_set_root_rtransid(root_item, sa->rtransid);
49033cae210fSQu Wenruo 	btrfs_set_stack_timespec_sec(&root_item->stime, sa->stime.sec);
49043cae210fSQu Wenruo 	btrfs_set_stack_timespec_nsec(&root_item->stime, sa->stime.nsec);
49053cae210fSQu Wenruo 	btrfs_set_stack_timespec_sec(&root_item->rtime, sa->rtime.sec);
49063cae210fSQu Wenruo 	btrfs_set_stack_timespec_nsec(&root_item->rtime, sa->rtime.nsec);
49078ea05e3aSAlexander Block 
49088ea05e3aSAlexander Block 	ret = btrfs_update_root(trans, root->fs_info->tree_root,
49098ea05e3aSAlexander Block 				&root->root_key, &root->root_item);
49108ea05e3aSAlexander Block 	if (ret < 0) {
49118ea05e3aSAlexander Block 		btrfs_end_transaction(trans, root);
49128ea05e3aSAlexander Block 		goto out;
4913dd5f9615SStefan Behrens 	}
4914dd5f9615SStefan Behrens 	if (received_uuid_changed && !btrfs_is_empty_uuid(sa->uuid)) {
4915dd5f9615SStefan Behrens 		ret = btrfs_uuid_tree_add(trans, root->fs_info->uuid_root,
4916dd5f9615SStefan Behrens 					  sa->uuid,
4917dd5f9615SStefan Behrens 					  BTRFS_UUID_KEY_RECEIVED_SUBVOL,
4918dd5f9615SStefan Behrens 					  root->root_key.objectid);
4919dd5f9615SStefan Behrens 		if (ret < 0 && ret != -EEXIST) {
4920dd5f9615SStefan Behrens 			btrfs_abort_transaction(trans, root, ret);
4921dd5f9615SStefan Behrens 			goto out;
4922dd5f9615SStefan Behrens 		}
4923dd5f9615SStefan Behrens 	}
49248ea05e3aSAlexander Block 	ret = btrfs_commit_transaction(trans, root);
4925dd5f9615SStefan Behrens 	if (ret < 0) {
4926dd5f9615SStefan Behrens 		btrfs_abort_transaction(trans, root, ret);
49278ea05e3aSAlexander Block 		goto out;
49288ea05e3aSAlexander Block 	}
49298ea05e3aSAlexander Block 
4930abccd00fSHugo Mills out:
4931abccd00fSHugo Mills 	up_write(&root->fs_info->subvol_sem);
4932abccd00fSHugo Mills 	mnt_drop_write_file(file);
4933abccd00fSHugo Mills 	return ret;
4934abccd00fSHugo Mills }
4935abccd00fSHugo Mills 
4936abccd00fSHugo Mills #ifdef CONFIG_64BIT
4937abccd00fSHugo Mills static long btrfs_ioctl_set_received_subvol_32(struct file *file,
4938abccd00fSHugo Mills 						void __user *arg)
4939abccd00fSHugo Mills {
4940abccd00fSHugo Mills 	struct btrfs_ioctl_received_subvol_args_32 *args32 = NULL;
4941abccd00fSHugo Mills 	struct btrfs_ioctl_received_subvol_args *args64 = NULL;
4942abccd00fSHugo Mills 	int ret = 0;
4943abccd00fSHugo Mills 
4944abccd00fSHugo Mills 	args32 = memdup_user(arg, sizeof(*args32));
4945abccd00fSHugo Mills 	if (IS_ERR(args32)) {
4946abccd00fSHugo Mills 		ret = PTR_ERR(args32);
4947abccd00fSHugo Mills 		args32 = NULL;
4948abccd00fSHugo Mills 		goto out;
4949abccd00fSHugo Mills 	}
4950abccd00fSHugo Mills 
4951abccd00fSHugo Mills 	args64 = kmalloc(sizeof(*args64), GFP_NOFS);
495284dbeb87SDan Carpenter 	if (!args64) {
495384dbeb87SDan Carpenter 		ret = -ENOMEM;
4954abccd00fSHugo Mills 		goto out;
4955abccd00fSHugo Mills 	}
4956abccd00fSHugo Mills 
4957abccd00fSHugo Mills 	memcpy(args64->uuid, args32->uuid, BTRFS_UUID_SIZE);
4958abccd00fSHugo Mills 	args64->stransid = args32->stransid;
4959abccd00fSHugo Mills 	args64->rtransid = args32->rtransid;
4960abccd00fSHugo Mills 	args64->stime.sec = args32->stime.sec;
4961abccd00fSHugo Mills 	args64->stime.nsec = args32->stime.nsec;
4962abccd00fSHugo Mills 	args64->rtime.sec = args32->rtime.sec;
4963abccd00fSHugo Mills 	args64->rtime.nsec = args32->rtime.nsec;
4964abccd00fSHugo Mills 	args64->flags = args32->flags;
4965abccd00fSHugo Mills 
4966abccd00fSHugo Mills 	ret = _btrfs_ioctl_set_received_subvol(file, args64);
4967abccd00fSHugo Mills 	if (ret)
4968abccd00fSHugo Mills 		goto out;
4969abccd00fSHugo Mills 
4970abccd00fSHugo Mills 	memcpy(args32->uuid, args64->uuid, BTRFS_UUID_SIZE);
4971abccd00fSHugo Mills 	args32->stransid = args64->stransid;
4972abccd00fSHugo Mills 	args32->rtransid = args64->rtransid;
4973abccd00fSHugo Mills 	args32->stime.sec = args64->stime.sec;
4974abccd00fSHugo Mills 	args32->stime.nsec = args64->stime.nsec;
4975abccd00fSHugo Mills 	args32->rtime.sec = args64->rtime.sec;
4976abccd00fSHugo Mills 	args32->rtime.nsec = args64->rtime.nsec;
4977abccd00fSHugo Mills 	args32->flags = args64->flags;
4978abccd00fSHugo Mills 
4979abccd00fSHugo Mills 	ret = copy_to_user(arg, args32, sizeof(*args32));
4980abccd00fSHugo Mills 	if (ret)
4981abccd00fSHugo Mills 		ret = -EFAULT;
4982abccd00fSHugo Mills 
4983abccd00fSHugo Mills out:
4984abccd00fSHugo Mills 	kfree(args32);
4985abccd00fSHugo Mills 	kfree(args64);
4986abccd00fSHugo Mills 	return ret;
4987abccd00fSHugo Mills }
4988abccd00fSHugo Mills #endif
4989abccd00fSHugo Mills 
4990abccd00fSHugo Mills static long btrfs_ioctl_set_received_subvol(struct file *file,
4991abccd00fSHugo Mills 					    void __user *arg)
4992abccd00fSHugo Mills {
4993abccd00fSHugo Mills 	struct btrfs_ioctl_received_subvol_args *sa = NULL;
4994abccd00fSHugo Mills 	int ret = 0;
4995abccd00fSHugo Mills 
4996abccd00fSHugo Mills 	sa = memdup_user(arg, sizeof(*sa));
4997abccd00fSHugo Mills 	if (IS_ERR(sa)) {
4998abccd00fSHugo Mills 		ret = PTR_ERR(sa);
4999abccd00fSHugo Mills 		sa = NULL;
5000abccd00fSHugo Mills 		goto out;
5001abccd00fSHugo Mills 	}
5002abccd00fSHugo Mills 
5003abccd00fSHugo Mills 	ret = _btrfs_ioctl_set_received_subvol(file, sa);
5004abccd00fSHugo Mills 
5005abccd00fSHugo Mills 	if (ret)
5006abccd00fSHugo Mills 		goto out;
5007abccd00fSHugo Mills 
50088ea05e3aSAlexander Block 	ret = copy_to_user(arg, sa, sizeof(*sa));
50098ea05e3aSAlexander Block 	if (ret)
50108ea05e3aSAlexander Block 		ret = -EFAULT;
50118ea05e3aSAlexander Block 
50128ea05e3aSAlexander Block out:
50138ea05e3aSAlexander Block 	kfree(sa);
50148ea05e3aSAlexander Block 	return ret;
50158ea05e3aSAlexander Block }
50168ea05e3aSAlexander Block 
5017867ab667Sjeff.liu static int btrfs_ioctl_get_fslabel(struct file *file, void __user *arg)
5018867ab667Sjeff.liu {
50196d0379ecSAl Viro 	struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
5020a1b83ac5SAnand Jain 	size_t len;
5021867ab667Sjeff.liu 	int ret;
5022a1b83ac5SAnand Jain 	char label[BTRFS_LABEL_SIZE];
5023a1b83ac5SAnand Jain 
5024a1b83ac5SAnand Jain 	spin_lock(&root->fs_info->super_lock);
5025a1b83ac5SAnand Jain 	memcpy(label, root->fs_info->super_copy->label, BTRFS_LABEL_SIZE);
5026a1b83ac5SAnand Jain 	spin_unlock(&root->fs_info->super_lock);
5027a1b83ac5SAnand Jain 
5028a1b83ac5SAnand Jain 	len = strnlen(label, BTRFS_LABEL_SIZE);
5029867ab667Sjeff.liu 
5030867ab667Sjeff.liu 	if (len == BTRFS_LABEL_SIZE) {
5031efe120a0SFrank Holton 		btrfs_warn(root->fs_info,
5032efe120a0SFrank Holton 			"label is too long, return the first %zu bytes", --len);
5033867ab667Sjeff.liu 	}
5034867ab667Sjeff.liu 
5035867ab667Sjeff.liu 	ret = copy_to_user(arg, label, len);
5036867ab667Sjeff.liu 
5037867ab667Sjeff.liu 	return ret ? -EFAULT : 0;
5038867ab667Sjeff.liu }
5039867ab667Sjeff.liu 
5040a8bfd4abSjeff.liu static int btrfs_ioctl_set_fslabel(struct file *file, void __user *arg)
5041a8bfd4abSjeff.liu {
50426d0379ecSAl Viro 	struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
5043a8bfd4abSjeff.liu 	struct btrfs_super_block *super_block = root->fs_info->super_copy;
5044a8bfd4abSjeff.liu 	struct btrfs_trans_handle *trans;
5045a8bfd4abSjeff.liu 	char label[BTRFS_LABEL_SIZE];
5046a8bfd4abSjeff.liu 	int ret;
5047a8bfd4abSjeff.liu 
5048a8bfd4abSjeff.liu 	if (!capable(CAP_SYS_ADMIN))
5049a8bfd4abSjeff.liu 		return -EPERM;
5050a8bfd4abSjeff.liu 
5051a8bfd4abSjeff.liu 	if (copy_from_user(label, arg, sizeof(label)))
5052a8bfd4abSjeff.liu 		return -EFAULT;
5053a8bfd4abSjeff.liu 
5054a8bfd4abSjeff.liu 	if (strnlen(label, BTRFS_LABEL_SIZE) == BTRFS_LABEL_SIZE) {
5055efe120a0SFrank Holton 		btrfs_err(root->fs_info, "unable to set label with more than %d bytes",
5056a8bfd4abSjeff.liu 		       BTRFS_LABEL_SIZE - 1);
5057a8bfd4abSjeff.liu 		return -EINVAL;
5058a8bfd4abSjeff.liu 	}
5059a8bfd4abSjeff.liu 
5060a8bfd4abSjeff.liu 	ret = mnt_want_write_file(file);
5061a8bfd4abSjeff.liu 	if (ret)
5062a8bfd4abSjeff.liu 		return ret;
5063a8bfd4abSjeff.liu 
5064a8bfd4abSjeff.liu 	trans = btrfs_start_transaction(root, 0);
5065a8bfd4abSjeff.liu 	if (IS_ERR(trans)) {
5066a8bfd4abSjeff.liu 		ret = PTR_ERR(trans);
5067a8bfd4abSjeff.liu 		goto out_unlock;
5068a8bfd4abSjeff.liu 	}
5069a8bfd4abSjeff.liu 
5070a1b83ac5SAnand Jain 	spin_lock(&root->fs_info->super_lock);
5071a8bfd4abSjeff.liu 	strcpy(super_block->label, label);
5072a1b83ac5SAnand Jain 	spin_unlock(&root->fs_info->super_lock);
5073d0270acaSJeff Mahoney 	ret = btrfs_commit_transaction(trans, root);
5074a8bfd4abSjeff.liu 
5075a8bfd4abSjeff.liu out_unlock:
5076a8bfd4abSjeff.liu 	mnt_drop_write_file(file);
5077a8bfd4abSjeff.liu 	return ret;
5078a8bfd4abSjeff.liu }
5079a8bfd4abSjeff.liu 
50802eaa055fSJeff Mahoney #define INIT_FEATURE_FLAGS(suffix) \
50812eaa055fSJeff Mahoney 	{ .compat_flags = BTRFS_FEATURE_COMPAT_##suffix, \
50822eaa055fSJeff Mahoney 	  .compat_ro_flags = BTRFS_FEATURE_COMPAT_RO_##suffix, \
50832eaa055fSJeff Mahoney 	  .incompat_flags = BTRFS_FEATURE_INCOMPAT_##suffix }
50842eaa055fSJeff Mahoney 
50852eaa055fSJeff Mahoney static int btrfs_ioctl_get_supported_features(struct file *file,
50862eaa055fSJeff Mahoney 					      void __user *arg)
50872eaa055fSJeff Mahoney {
50882eaa055fSJeff Mahoney 	static struct btrfs_ioctl_feature_flags features[3] = {
50892eaa055fSJeff Mahoney 		INIT_FEATURE_FLAGS(SUPP),
50902eaa055fSJeff Mahoney 		INIT_FEATURE_FLAGS(SAFE_SET),
50912eaa055fSJeff Mahoney 		INIT_FEATURE_FLAGS(SAFE_CLEAR)
50922eaa055fSJeff Mahoney 	};
50932eaa055fSJeff Mahoney 
50942eaa055fSJeff Mahoney 	if (copy_to_user(arg, &features, sizeof(features)))
50952eaa055fSJeff Mahoney 		return -EFAULT;
50962eaa055fSJeff Mahoney 
50972eaa055fSJeff Mahoney 	return 0;
50982eaa055fSJeff Mahoney }
50992eaa055fSJeff Mahoney 
51002eaa055fSJeff Mahoney static int btrfs_ioctl_get_features(struct file *file, void __user *arg)
51012eaa055fSJeff Mahoney {
51022eaa055fSJeff Mahoney 	struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
51032eaa055fSJeff Mahoney 	struct btrfs_super_block *super_block = root->fs_info->super_copy;
51042eaa055fSJeff Mahoney 	struct btrfs_ioctl_feature_flags features;
51052eaa055fSJeff Mahoney 
51062eaa055fSJeff Mahoney 	features.compat_flags = btrfs_super_compat_flags(super_block);
51072eaa055fSJeff Mahoney 	features.compat_ro_flags = btrfs_super_compat_ro_flags(super_block);
51082eaa055fSJeff Mahoney 	features.incompat_flags = btrfs_super_incompat_flags(super_block);
51092eaa055fSJeff Mahoney 
51102eaa055fSJeff Mahoney 	if (copy_to_user(arg, &features, sizeof(features)))
51112eaa055fSJeff Mahoney 		return -EFAULT;
51122eaa055fSJeff Mahoney 
51132eaa055fSJeff Mahoney 	return 0;
51142eaa055fSJeff Mahoney }
51152eaa055fSJeff Mahoney 
51163b02a68aSJeff Mahoney static int check_feature_bits(struct btrfs_root *root,
51173b02a68aSJeff Mahoney 			      enum btrfs_feature_set set,
51182eaa055fSJeff Mahoney 			      u64 change_mask, u64 flags, u64 supported_flags,
51192eaa055fSJeff Mahoney 			      u64 safe_set, u64 safe_clear)
51202eaa055fSJeff Mahoney {
51213b02a68aSJeff Mahoney 	const char *type = btrfs_feature_set_names[set];
51223b02a68aSJeff Mahoney 	char *names;
51232eaa055fSJeff Mahoney 	u64 disallowed, unsupported;
51242eaa055fSJeff Mahoney 	u64 set_mask = flags & change_mask;
51252eaa055fSJeff Mahoney 	u64 clear_mask = ~flags & change_mask;
51262eaa055fSJeff Mahoney 
51272eaa055fSJeff Mahoney 	unsupported = set_mask & ~supported_flags;
51282eaa055fSJeff Mahoney 	if (unsupported) {
51293b02a68aSJeff Mahoney 		names = btrfs_printable_features(set, unsupported);
51303b02a68aSJeff Mahoney 		if (names) {
51313b02a68aSJeff Mahoney 			btrfs_warn(root->fs_info,
51323b02a68aSJeff Mahoney 			   "this kernel does not support the %s feature bit%s",
51333b02a68aSJeff Mahoney 			   names, strchr(names, ',') ? "s" : "");
51343b02a68aSJeff Mahoney 			kfree(names);
51353b02a68aSJeff Mahoney 		} else
51362eaa055fSJeff Mahoney 			btrfs_warn(root->fs_info,
51372eaa055fSJeff Mahoney 			   "this kernel does not support %s bits 0x%llx",
51382eaa055fSJeff Mahoney 			   type, unsupported);
51392eaa055fSJeff Mahoney 		return -EOPNOTSUPP;
51402eaa055fSJeff Mahoney 	}
51412eaa055fSJeff Mahoney 
51422eaa055fSJeff Mahoney 	disallowed = set_mask & ~safe_set;
51432eaa055fSJeff Mahoney 	if (disallowed) {
51443b02a68aSJeff Mahoney 		names = btrfs_printable_features(set, disallowed);
51453b02a68aSJeff Mahoney 		if (names) {
51463b02a68aSJeff Mahoney 			btrfs_warn(root->fs_info,
51473b02a68aSJeff Mahoney 			   "can't set the %s feature bit%s while mounted",
51483b02a68aSJeff Mahoney 			   names, strchr(names, ',') ? "s" : "");
51493b02a68aSJeff Mahoney 			kfree(names);
51503b02a68aSJeff Mahoney 		} else
51512eaa055fSJeff Mahoney 			btrfs_warn(root->fs_info,
51522eaa055fSJeff Mahoney 			   "can't set %s bits 0x%llx while mounted",
51532eaa055fSJeff Mahoney 			   type, disallowed);
51542eaa055fSJeff Mahoney 		return -EPERM;
51552eaa055fSJeff Mahoney 	}
51562eaa055fSJeff Mahoney 
51572eaa055fSJeff Mahoney 	disallowed = clear_mask & ~safe_clear;
51582eaa055fSJeff Mahoney 	if (disallowed) {
51593b02a68aSJeff Mahoney 		names = btrfs_printable_features(set, disallowed);
51603b02a68aSJeff Mahoney 		if (names) {
51613b02a68aSJeff Mahoney 			btrfs_warn(root->fs_info,
51623b02a68aSJeff Mahoney 			   "can't clear the %s feature bit%s while mounted",
51633b02a68aSJeff Mahoney 			   names, strchr(names, ',') ? "s" : "");
51643b02a68aSJeff Mahoney 			kfree(names);
51653b02a68aSJeff Mahoney 		} else
51662eaa055fSJeff Mahoney 			btrfs_warn(root->fs_info,
51672eaa055fSJeff Mahoney 			   "can't clear %s bits 0x%llx while mounted",
51682eaa055fSJeff Mahoney 			   type, disallowed);
51692eaa055fSJeff Mahoney 		return -EPERM;
51702eaa055fSJeff Mahoney 	}
51712eaa055fSJeff Mahoney 
51722eaa055fSJeff Mahoney 	return 0;
51732eaa055fSJeff Mahoney }
51742eaa055fSJeff Mahoney 
51752eaa055fSJeff Mahoney #define check_feature(root, change_mask, flags, mask_base)	\
51763b02a68aSJeff Mahoney check_feature_bits(root, FEAT_##mask_base, change_mask, flags,	\
51772eaa055fSJeff Mahoney 		   BTRFS_FEATURE_ ## mask_base ## _SUPP,	\
51782eaa055fSJeff Mahoney 		   BTRFS_FEATURE_ ## mask_base ## _SAFE_SET,	\
51792eaa055fSJeff Mahoney 		   BTRFS_FEATURE_ ## mask_base ## _SAFE_CLEAR)
51802eaa055fSJeff Mahoney 
51812eaa055fSJeff Mahoney static int btrfs_ioctl_set_features(struct file *file, void __user *arg)
51822eaa055fSJeff Mahoney {
51832eaa055fSJeff Mahoney 	struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
51842eaa055fSJeff Mahoney 	struct btrfs_super_block *super_block = root->fs_info->super_copy;
51852eaa055fSJeff Mahoney 	struct btrfs_ioctl_feature_flags flags[2];
51862eaa055fSJeff Mahoney 	struct btrfs_trans_handle *trans;
51872eaa055fSJeff Mahoney 	u64 newflags;
51882eaa055fSJeff Mahoney 	int ret;
51892eaa055fSJeff Mahoney 
51902eaa055fSJeff Mahoney 	if (!capable(CAP_SYS_ADMIN))
51912eaa055fSJeff Mahoney 		return -EPERM;
51922eaa055fSJeff Mahoney 
51932eaa055fSJeff Mahoney 	if (copy_from_user(flags, arg, sizeof(flags)))
51942eaa055fSJeff Mahoney 		return -EFAULT;
51952eaa055fSJeff Mahoney 
51962eaa055fSJeff Mahoney 	/* Nothing to do */
51972eaa055fSJeff Mahoney 	if (!flags[0].compat_flags && !flags[0].compat_ro_flags &&
51982eaa055fSJeff Mahoney 	    !flags[0].incompat_flags)
51992eaa055fSJeff Mahoney 		return 0;
52002eaa055fSJeff Mahoney 
52012eaa055fSJeff Mahoney 	ret = check_feature(root, flags[0].compat_flags,
52022eaa055fSJeff Mahoney 			    flags[1].compat_flags, COMPAT);
52032eaa055fSJeff Mahoney 	if (ret)
52042eaa055fSJeff Mahoney 		return ret;
52052eaa055fSJeff Mahoney 
52062eaa055fSJeff Mahoney 	ret = check_feature(root, flags[0].compat_ro_flags,
52072eaa055fSJeff Mahoney 			    flags[1].compat_ro_flags, COMPAT_RO);
52082eaa055fSJeff Mahoney 	if (ret)
52092eaa055fSJeff Mahoney 		return ret;
52102eaa055fSJeff Mahoney 
52112eaa055fSJeff Mahoney 	ret = check_feature(root, flags[0].incompat_flags,
52122eaa055fSJeff Mahoney 			    flags[1].incompat_flags, INCOMPAT);
52132eaa055fSJeff Mahoney 	if (ret)
52142eaa055fSJeff Mahoney 		return ret;
52152eaa055fSJeff Mahoney 
52168051aa1aSDavid Sterba 	trans = btrfs_start_transaction(root, 0);
52172eaa055fSJeff Mahoney 	if (IS_ERR(trans))
52182eaa055fSJeff Mahoney 		return PTR_ERR(trans);
52192eaa055fSJeff Mahoney 
52202eaa055fSJeff Mahoney 	spin_lock(&root->fs_info->super_lock);
52212eaa055fSJeff Mahoney 	newflags = btrfs_super_compat_flags(super_block);
52222eaa055fSJeff Mahoney 	newflags |= flags[0].compat_flags & flags[1].compat_flags;
52232eaa055fSJeff Mahoney 	newflags &= ~(flags[0].compat_flags & ~flags[1].compat_flags);
52242eaa055fSJeff Mahoney 	btrfs_set_super_compat_flags(super_block, newflags);
52252eaa055fSJeff Mahoney 
52262eaa055fSJeff Mahoney 	newflags = btrfs_super_compat_ro_flags(super_block);
52272eaa055fSJeff Mahoney 	newflags |= flags[0].compat_ro_flags & flags[1].compat_ro_flags;
52282eaa055fSJeff Mahoney 	newflags &= ~(flags[0].compat_ro_flags & ~flags[1].compat_ro_flags);
52292eaa055fSJeff Mahoney 	btrfs_set_super_compat_ro_flags(super_block, newflags);
52302eaa055fSJeff Mahoney 
52312eaa055fSJeff Mahoney 	newflags = btrfs_super_incompat_flags(super_block);
52322eaa055fSJeff Mahoney 	newflags |= flags[0].incompat_flags & flags[1].incompat_flags;
52332eaa055fSJeff Mahoney 	newflags &= ~(flags[0].incompat_flags & ~flags[1].incompat_flags);
52342eaa055fSJeff Mahoney 	btrfs_set_super_incompat_flags(super_block, newflags);
52352eaa055fSJeff Mahoney 	spin_unlock(&root->fs_info->super_lock);
52362eaa055fSJeff Mahoney 
5237d0270acaSJeff Mahoney 	return btrfs_commit_transaction(trans, root);
52382eaa055fSJeff Mahoney }
52392eaa055fSJeff Mahoney 
5240f46b5a66SChristoph Hellwig long btrfs_ioctl(struct file *file, unsigned int
5241f46b5a66SChristoph Hellwig 		cmd, unsigned long arg)
5242f46b5a66SChristoph Hellwig {
5243496ad9aaSAl Viro 	struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
52444bcabaa3SChristoph Hellwig 	void __user *argp = (void __user *)arg;
5245f46b5a66SChristoph Hellwig 
5246f46b5a66SChristoph Hellwig 	switch (cmd) {
52476cbff00fSChristoph Hellwig 	case FS_IOC_GETFLAGS:
52486cbff00fSChristoph Hellwig 		return btrfs_ioctl_getflags(file, argp);
52496cbff00fSChristoph Hellwig 	case FS_IOC_SETFLAGS:
52506cbff00fSChristoph Hellwig 		return btrfs_ioctl_setflags(file, argp);
52516cbff00fSChristoph Hellwig 	case FS_IOC_GETVERSION:
52526cbff00fSChristoph Hellwig 		return btrfs_ioctl_getversion(file, argp);
5253f7039b1dSLi Dongyang 	case FITRIM:
5254f7039b1dSLi Dongyang 		return btrfs_ioctl_fitrim(file, argp);
5255f46b5a66SChristoph Hellwig 	case BTRFS_IOC_SNAP_CREATE:
5256fa0d2b9bSLi Zefan 		return btrfs_ioctl_snap_create(file, argp, 0);
5257fdfb1e4fSLi Zefan 	case BTRFS_IOC_SNAP_CREATE_V2:
5258fa0d2b9bSLi Zefan 		return btrfs_ioctl_snap_create_v2(file, argp, 0);
52593de4586cSChris Mason 	case BTRFS_IOC_SUBVOL_CREATE:
5260fa0d2b9bSLi Zefan 		return btrfs_ioctl_snap_create(file, argp, 1);
52616f72c7e2SArne Jansen 	case BTRFS_IOC_SUBVOL_CREATE_V2:
52626f72c7e2SArne Jansen 		return btrfs_ioctl_snap_create_v2(file, argp, 1);
526376dda93cSYan, Zheng 	case BTRFS_IOC_SNAP_DESTROY:
526476dda93cSYan, Zheng 		return btrfs_ioctl_snap_destroy(file, argp);
52650caa102dSLi Zefan 	case BTRFS_IOC_SUBVOL_GETFLAGS:
52660caa102dSLi Zefan 		return btrfs_ioctl_subvol_getflags(file, argp);
52670caa102dSLi Zefan 	case BTRFS_IOC_SUBVOL_SETFLAGS:
52680caa102dSLi Zefan 		return btrfs_ioctl_subvol_setflags(file, argp);
52696ef5ed0dSJosef Bacik 	case BTRFS_IOC_DEFAULT_SUBVOL:
52706ef5ed0dSJosef Bacik 		return btrfs_ioctl_default_subvol(file, argp);
5271f46b5a66SChristoph Hellwig 	case BTRFS_IOC_DEFRAG:
52721e701a32SChris Mason 		return btrfs_ioctl_defrag(file, NULL);
52731e701a32SChris Mason 	case BTRFS_IOC_DEFRAG_RANGE:
52741e701a32SChris Mason 		return btrfs_ioctl_defrag(file, argp);
5275f46b5a66SChristoph Hellwig 	case BTRFS_IOC_RESIZE:
5276198605a8SMiao Xie 		return btrfs_ioctl_resize(file, argp);
5277f46b5a66SChristoph Hellwig 	case BTRFS_IOC_ADD_DEV:
52784bcabaa3SChristoph Hellwig 		return btrfs_ioctl_add_dev(root, argp);
5279f46b5a66SChristoph Hellwig 	case BTRFS_IOC_RM_DEV:
5280da24927bSMiao Xie 		return btrfs_ioctl_rm_dev(file, argp);
5281475f6387SJan Schmidt 	case BTRFS_IOC_FS_INFO:
5282475f6387SJan Schmidt 		return btrfs_ioctl_fs_info(root, argp);
5283475f6387SJan Schmidt 	case BTRFS_IOC_DEV_INFO:
5284475f6387SJan Schmidt 		return btrfs_ioctl_dev_info(root, argp);
5285f46b5a66SChristoph Hellwig 	case BTRFS_IOC_BALANCE:
52869ba1f6e4SLiu Bo 		return btrfs_ioctl_balance(file, NULL);
5287f46b5a66SChristoph Hellwig 	case BTRFS_IOC_CLONE:
5288c5c9cd4dSSage Weil 		return btrfs_ioctl_clone(file, arg, 0, 0, 0);
5289c5c9cd4dSSage Weil 	case BTRFS_IOC_CLONE_RANGE:
52907a865e8aSChristoph Hellwig 		return btrfs_ioctl_clone_range(file, argp);
5291f46b5a66SChristoph Hellwig 	case BTRFS_IOC_TRANS_START:
5292f46b5a66SChristoph Hellwig 		return btrfs_ioctl_trans_start(file);
5293f46b5a66SChristoph Hellwig 	case BTRFS_IOC_TRANS_END:
5294f46b5a66SChristoph Hellwig 		return btrfs_ioctl_trans_end(file);
5295ac8e9819SChris Mason 	case BTRFS_IOC_TREE_SEARCH:
5296ac8e9819SChris Mason 		return btrfs_ioctl_tree_search(file, argp);
5297cc68a8a5SGerhard Heift 	case BTRFS_IOC_TREE_SEARCH_V2:
5298cc68a8a5SGerhard Heift 		return btrfs_ioctl_tree_search_v2(file, argp);
5299ac8e9819SChris Mason 	case BTRFS_IOC_INO_LOOKUP:
5300ac8e9819SChris Mason 		return btrfs_ioctl_ino_lookup(file, argp);
5301d7728c96SJan Schmidt 	case BTRFS_IOC_INO_PATHS:
5302d7728c96SJan Schmidt 		return btrfs_ioctl_ino_to_path(root, argp);
5303d7728c96SJan Schmidt 	case BTRFS_IOC_LOGICAL_INO:
5304d7728c96SJan Schmidt 		return btrfs_ioctl_logical_to_ino(root, argp);
53051406e432SJosef Bacik 	case BTRFS_IOC_SPACE_INFO:
53061406e432SJosef Bacik 		return btrfs_ioctl_space_info(root, argp);
53079b199859SFilipe David Borba Manana 	case BTRFS_IOC_SYNC: {
53089b199859SFilipe David Borba Manana 		int ret;
53099b199859SFilipe David Borba Manana 
53106c255e67SMiao Xie 		ret = btrfs_start_delalloc_roots(root->fs_info, 0, -1);
53119b199859SFilipe David Borba Manana 		if (ret)
53129b199859SFilipe David Borba Manana 			return ret;
53139b199859SFilipe David Borba Manana 		ret = btrfs_sync_fs(file->f_dentry->d_sb, 1);
53149b199859SFilipe David Borba Manana 		return ret;
53159b199859SFilipe David Borba Manana 	}
531646204592SSage Weil 	case BTRFS_IOC_START_SYNC:
53179a8c28beSMiao Xie 		return btrfs_ioctl_start_sync(root, argp);
531846204592SSage Weil 	case BTRFS_IOC_WAIT_SYNC:
53199a8c28beSMiao Xie 		return btrfs_ioctl_wait_sync(root, argp);
5320475f6387SJan Schmidt 	case BTRFS_IOC_SCRUB:
5321b8e95489SMiao Xie 		return btrfs_ioctl_scrub(file, argp);
5322475f6387SJan Schmidt 	case BTRFS_IOC_SCRUB_CANCEL:
5323475f6387SJan Schmidt 		return btrfs_ioctl_scrub_cancel(root, argp);
5324475f6387SJan Schmidt 	case BTRFS_IOC_SCRUB_PROGRESS:
5325475f6387SJan Schmidt 		return btrfs_ioctl_scrub_progress(root, argp);
5326c9e9f97bSIlya Dryomov 	case BTRFS_IOC_BALANCE_V2:
53279ba1f6e4SLiu Bo 		return btrfs_ioctl_balance(file, argp);
5328837d5b6eSIlya Dryomov 	case BTRFS_IOC_BALANCE_CTL:
5329837d5b6eSIlya Dryomov 		return btrfs_ioctl_balance_ctl(root, arg);
533019a39dceSIlya Dryomov 	case BTRFS_IOC_BALANCE_PROGRESS:
533119a39dceSIlya Dryomov 		return btrfs_ioctl_balance_progress(root, argp);
53328ea05e3aSAlexander Block 	case BTRFS_IOC_SET_RECEIVED_SUBVOL:
53338ea05e3aSAlexander Block 		return btrfs_ioctl_set_received_subvol(file, argp);
5334abccd00fSHugo Mills #ifdef CONFIG_64BIT
5335abccd00fSHugo Mills 	case BTRFS_IOC_SET_RECEIVED_SUBVOL_32:
5336abccd00fSHugo Mills 		return btrfs_ioctl_set_received_subvol_32(file, argp);
5337abccd00fSHugo Mills #endif
533831db9f7cSAlexander Block 	case BTRFS_IOC_SEND:
533931db9f7cSAlexander Block 		return btrfs_ioctl_send(file, argp);
5340c11d2c23SStefan Behrens 	case BTRFS_IOC_GET_DEV_STATS:
5341b27f7c0cSDavid Sterba 		return btrfs_ioctl_get_dev_stats(root, argp);
53425d13a37bSArne Jansen 	case BTRFS_IOC_QUOTA_CTL:
5343905b0ddaSMiao Xie 		return btrfs_ioctl_quota_ctl(file, argp);
53445d13a37bSArne Jansen 	case BTRFS_IOC_QGROUP_ASSIGN:
5345905b0ddaSMiao Xie 		return btrfs_ioctl_qgroup_assign(file, argp);
53465d13a37bSArne Jansen 	case BTRFS_IOC_QGROUP_CREATE:
5347905b0ddaSMiao Xie 		return btrfs_ioctl_qgroup_create(file, argp);
53485d13a37bSArne Jansen 	case BTRFS_IOC_QGROUP_LIMIT:
5349905b0ddaSMiao Xie 		return btrfs_ioctl_qgroup_limit(file, argp);
53502f232036SJan Schmidt 	case BTRFS_IOC_QUOTA_RESCAN:
53512f232036SJan Schmidt 		return btrfs_ioctl_quota_rescan(file, argp);
53522f232036SJan Schmidt 	case BTRFS_IOC_QUOTA_RESCAN_STATUS:
53532f232036SJan Schmidt 		return btrfs_ioctl_quota_rescan_status(file, argp);
535457254b6eSJan Schmidt 	case BTRFS_IOC_QUOTA_RESCAN_WAIT:
535557254b6eSJan Schmidt 		return btrfs_ioctl_quota_rescan_wait(file, argp);
53563f6bcfbdSStefan Behrens 	case BTRFS_IOC_DEV_REPLACE:
53573f6bcfbdSStefan Behrens 		return btrfs_ioctl_dev_replace(root, argp);
5358867ab667Sjeff.liu 	case BTRFS_IOC_GET_FSLABEL:
5359867ab667Sjeff.liu 		return btrfs_ioctl_get_fslabel(file, argp);
5360a8bfd4abSjeff.liu 	case BTRFS_IOC_SET_FSLABEL:
5361a8bfd4abSjeff.liu 		return btrfs_ioctl_set_fslabel(file, argp);
5362416161dbSMark Fasheh 	case BTRFS_IOC_FILE_EXTENT_SAME:
5363416161dbSMark Fasheh 		return btrfs_ioctl_file_extent_same(file, argp);
53642eaa055fSJeff Mahoney 	case BTRFS_IOC_GET_SUPPORTED_FEATURES:
53652eaa055fSJeff Mahoney 		return btrfs_ioctl_get_supported_features(file, argp);
53662eaa055fSJeff Mahoney 	case BTRFS_IOC_GET_FEATURES:
53672eaa055fSJeff Mahoney 		return btrfs_ioctl_get_features(file, argp);
53682eaa055fSJeff Mahoney 	case BTRFS_IOC_SET_FEATURES:
53692eaa055fSJeff Mahoney 		return btrfs_ioctl_set_features(file, argp);
5370f46b5a66SChristoph Hellwig 	}
5371f46b5a66SChristoph Hellwig 
5372f46b5a66SChristoph Hellwig 	return -ENOTTY;
5373f46b5a66SChristoph Hellwig }
5374