xref: /openbmc/linux/fs/btrfs/ioctl.c (revision 521e0546)
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"
61f46b5a66SChristoph Hellwig 
62abccd00fSHugo Mills #ifdef CONFIG_64BIT
63abccd00fSHugo Mills /* If we have a 32-bit userspace and 64-bit kernel, then the UAPI
64abccd00fSHugo Mills  * structures are incorrect, as the timespec structure from userspace
65abccd00fSHugo Mills  * is 4 bytes too small. We define these alternatives here to teach
66abccd00fSHugo Mills  * the kernel about the 32-bit struct packing.
67abccd00fSHugo Mills  */
68abccd00fSHugo Mills struct btrfs_ioctl_timespec_32 {
69abccd00fSHugo Mills 	__u64 sec;
70abccd00fSHugo Mills 	__u32 nsec;
71abccd00fSHugo Mills } __attribute__ ((__packed__));
72abccd00fSHugo Mills 
73abccd00fSHugo Mills struct btrfs_ioctl_received_subvol_args_32 {
74abccd00fSHugo Mills 	char	uuid[BTRFS_UUID_SIZE];	/* in */
75abccd00fSHugo Mills 	__u64	stransid;		/* in */
76abccd00fSHugo Mills 	__u64	rtransid;		/* out */
77abccd00fSHugo Mills 	struct btrfs_ioctl_timespec_32 stime; /* in */
78abccd00fSHugo Mills 	struct btrfs_ioctl_timespec_32 rtime; /* out */
79abccd00fSHugo Mills 	__u64	flags;			/* in */
80abccd00fSHugo Mills 	__u64	reserved[16];		/* in */
81abccd00fSHugo Mills } __attribute__ ((__packed__));
82abccd00fSHugo Mills 
83abccd00fSHugo Mills #define BTRFS_IOC_SET_RECEIVED_SUBVOL_32 _IOWR(BTRFS_IOCTL_MAGIC, 37, \
84abccd00fSHugo Mills 				struct btrfs_ioctl_received_subvol_args_32)
85abccd00fSHugo Mills #endif
86abccd00fSHugo Mills 
87abccd00fSHugo Mills 
88416161dbSMark Fasheh static int btrfs_clone(struct inode *src, struct inode *inode,
89416161dbSMark Fasheh 		       u64 off, u64 olen, u64 olen_aligned, u64 destoff);
90416161dbSMark Fasheh 
916cbff00fSChristoph Hellwig /* Mask out flags that are inappropriate for the given type of inode. */
926cbff00fSChristoph Hellwig static inline __u32 btrfs_mask_flags(umode_t mode, __u32 flags)
936cbff00fSChristoph Hellwig {
946cbff00fSChristoph Hellwig 	if (S_ISDIR(mode))
956cbff00fSChristoph Hellwig 		return flags;
966cbff00fSChristoph Hellwig 	else if (S_ISREG(mode))
976cbff00fSChristoph Hellwig 		return flags & ~FS_DIRSYNC_FL;
986cbff00fSChristoph Hellwig 	else
996cbff00fSChristoph Hellwig 		return flags & (FS_NODUMP_FL | FS_NOATIME_FL);
1006cbff00fSChristoph Hellwig }
101f46b5a66SChristoph Hellwig 
1026cbff00fSChristoph Hellwig /*
1036cbff00fSChristoph Hellwig  * Export inode flags to the format expected by the FS_IOC_GETFLAGS ioctl.
1046cbff00fSChristoph Hellwig  */
1056cbff00fSChristoph Hellwig static unsigned int btrfs_flags_to_ioctl(unsigned int flags)
1066cbff00fSChristoph Hellwig {
1076cbff00fSChristoph Hellwig 	unsigned int iflags = 0;
1086cbff00fSChristoph Hellwig 
1096cbff00fSChristoph Hellwig 	if (flags & BTRFS_INODE_SYNC)
1106cbff00fSChristoph Hellwig 		iflags |= FS_SYNC_FL;
1116cbff00fSChristoph Hellwig 	if (flags & BTRFS_INODE_IMMUTABLE)
1126cbff00fSChristoph Hellwig 		iflags |= FS_IMMUTABLE_FL;
1136cbff00fSChristoph Hellwig 	if (flags & BTRFS_INODE_APPEND)
1146cbff00fSChristoph Hellwig 		iflags |= FS_APPEND_FL;
1156cbff00fSChristoph Hellwig 	if (flags & BTRFS_INODE_NODUMP)
1166cbff00fSChristoph Hellwig 		iflags |= FS_NODUMP_FL;
1176cbff00fSChristoph Hellwig 	if (flags & BTRFS_INODE_NOATIME)
1186cbff00fSChristoph Hellwig 		iflags |= FS_NOATIME_FL;
1196cbff00fSChristoph Hellwig 	if (flags & BTRFS_INODE_DIRSYNC)
1206cbff00fSChristoph Hellwig 		iflags |= FS_DIRSYNC_FL;
121d0092bddSLi Zefan 	if (flags & BTRFS_INODE_NODATACOW)
122d0092bddSLi Zefan 		iflags |= FS_NOCOW_FL;
123d0092bddSLi Zefan 
124d0092bddSLi Zefan 	if ((flags & BTRFS_INODE_COMPRESS) && !(flags & BTRFS_INODE_NOCOMPRESS))
125d0092bddSLi Zefan 		iflags |= FS_COMPR_FL;
126d0092bddSLi Zefan 	else if (flags & BTRFS_INODE_NOCOMPRESS)
127d0092bddSLi Zefan 		iflags |= FS_NOCOMP_FL;
1286cbff00fSChristoph Hellwig 
1296cbff00fSChristoph Hellwig 	return iflags;
1306cbff00fSChristoph Hellwig }
1316cbff00fSChristoph Hellwig 
1326cbff00fSChristoph Hellwig /*
1336cbff00fSChristoph Hellwig  * Update inode->i_flags based on the btrfs internal flags.
1346cbff00fSChristoph Hellwig  */
1356cbff00fSChristoph Hellwig void btrfs_update_iflags(struct inode *inode)
1366cbff00fSChristoph Hellwig {
1376cbff00fSChristoph Hellwig 	struct btrfs_inode *ip = BTRFS_I(inode);
1386cbff00fSChristoph Hellwig 
1396cbff00fSChristoph Hellwig 	inode->i_flags &= ~(S_SYNC|S_APPEND|S_IMMUTABLE|S_NOATIME|S_DIRSYNC);
1406cbff00fSChristoph Hellwig 
1416cbff00fSChristoph Hellwig 	if (ip->flags & BTRFS_INODE_SYNC)
1426cbff00fSChristoph Hellwig 		inode->i_flags |= S_SYNC;
1436cbff00fSChristoph Hellwig 	if (ip->flags & BTRFS_INODE_IMMUTABLE)
1446cbff00fSChristoph Hellwig 		inode->i_flags |= S_IMMUTABLE;
1456cbff00fSChristoph Hellwig 	if (ip->flags & BTRFS_INODE_APPEND)
1466cbff00fSChristoph Hellwig 		inode->i_flags |= S_APPEND;
1476cbff00fSChristoph Hellwig 	if (ip->flags & BTRFS_INODE_NOATIME)
1486cbff00fSChristoph Hellwig 		inode->i_flags |= S_NOATIME;
1496cbff00fSChristoph Hellwig 	if (ip->flags & BTRFS_INODE_DIRSYNC)
1506cbff00fSChristoph Hellwig 		inode->i_flags |= S_DIRSYNC;
1516cbff00fSChristoph Hellwig }
1526cbff00fSChristoph Hellwig 
1536cbff00fSChristoph Hellwig /*
1546cbff00fSChristoph Hellwig  * Inherit flags from the parent inode.
1556cbff00fSChristoph Hellwig  *
156e27425d6SJosef Bacik  * Currently only the compression flags and the cow flags are inherited.
1576cbff00fSChristoph Hellwig  */
1586cbff00fSChristoph Hellwig void btrfs_inherit_iflags(struct inode *inode, struct inode *dir)
1596cbff00fSChristoph Hellwig {
1600b4dcea5SChris Mason 	unsigned int flags;
1610b4dcea5SChris Mason 
1620b4dcea5SChris Mason 	if (!dir)
1630b4dcea5SChris Mason 		return;
1640b4dcea5SChris Mason 
1650b4dcea5SChris Mason 	flags = BTRFS_I(dir)->flags;
1666cbff00fSChristoph Hellwig 
167e27425d6SJosef Bacik 	if (flags & BTRFS_INODE_NOCOMPRESS) {
168e27425d6SJosef Bacik 		BTRFS_I(inode)->flags &= ~BTRFS_INODE_COMPRESS;
169e27425d6SJosef Bacik 		BTRFS_I(inode)->flags |= BTRFS_INODE_NOCOMPRESS;
170e27425d6SJosef Bacik 	} else if (flags & BTRFS_INODE_COMPRESS) {
171e27425d6SJosef Bacik 		BTRFS_I(inode)->flags &= ~BTRFS_INODE_NOCOMPRESS;
172e27425d6SJosef Bacik 		BTRFS_I(inode)->flags |= BTRFS_INODE_COMPRESS;
173e27425d6SJosef Bacik 	}
1746cbff00fSChristoph Hellwig 
175213490b3SLiu Bo 	if (flags & BTRFS_INODE_NODATACOW) {
176e27425d6SJosef Bacik 		BTRFS_I(inode)->flags |= BTRFS_INODE_NODATACOW;
177213490b3SLiu Bo 		if (S_ISREG(inode->i_mode))
178213490b3SLiu Bo 			BTRFS_I(inode)->flags |= BTRFS_INODE_NODATASUM;
179213490b3SLiu Bo 	}
180e27425d6SJosef Bacik 
1816cbff00fSChristoph Hellwig 	btrfs_update_iflags(inode);
1826cbff00fSChristoph Hellwig }
1836cbff00fSChristoph Hellwig 
1846cbff00fSChristoph Hellwig static int btrfs_ioctl_getflags(struct file *file, void __user *arg)
1856cbff00fSChristoph Hellwig {
186496ad9aaSAl Viro 	struct btrfs_inode *ip = BTRFS_I(file_inode(file));
1876cbff00fSChristoph Hellwig 	unsigned int flags = btrfs_flags_to_ioctl(ip->flags);
1886cbff00fSChristoph Hellwig 
1896cbff00fSChristoph Hellwig 	if (copy_to_user(arg, &flags, sizeof(flags)))
1906cbff00fSChristoph Hellwig 		return -EFAULT;
1916cbff00fSChristoph Hellwig 	return 0;
1926cbff00fSChristoph Hellwig }
1936cbff00fSChristoph Hellwig 
19475e7cb7fSLiu Bo static int check_flags(unsigned int flags)
19575e7cb7fSLiu Bo {
19675e7cb7fSLiu Bo 	if (flags & ~(FS_IMMUTABLE_FL | FS_APPEND_FL | \
19775e7cb7fSLiu Bo 		      FS_NOATIME_FL | FS_NODUMP_FL | \
19875e7cb7fSLiu Bo 		      FS_SYNC_FL | FS_DIRSYNC_FL | \
199e1e8fb6aSLi Zefan 		      FS_NOCOMP_FL | FS_COMPR_FL |
200e1e8fb6aSLi Zefan 		      FS_NOCOW_FL))
20175e7cb7fSLiu Bo 		return -EOPNOTSUPP;
20275e7cb7fSLiu Bo 
20375e7cb7fSLiu Bo 	if ((flags & FS_NOCOMP_FL) && (flags & FS_COMPR_FL))
20475e7cb7fSLiu Bo 		return -EINVAL;
20575e7cb7fSLiu Bo 
20675e7cb7fSLiu Bo 	return 0;
20775e7cb7fSLiu Bo }
20875e7cb7fSLiu Bo 
2096cbff00fSChristoph Hellwig static int btrfs_ioctl_setflags(struct file *file, void __user *arg)
2106cbff00fSChristoph Hellwig {
211496ad9aaSAl Viro 	struct inode *inode = file_inode(file);
2126cbff00fSChristoph Hellwig 	struct btrfs_inode *ip = BTRFS_I(inode);
2136cbff00fSChristoph Hellwig 	struct btrfs_root *root = ip->root;
2146cbff00fSChristoph Hellwig 	struct btrfs_trans_handle *trans;
2156cbff00fSChristoph Hellwig 	unsigned int flags, oldflags;
2166cbff00fSChristoph Hellwig 	int ret;
217f062abf0SLi Zefan 	u64 ip_oldflags;
218f062abf0SLi Zefan 	unsigned int i_oldflags;
2197e97b8daSDavid Sterba 	umode_t mode;
2206cbff00fSChristoph Hellwig 
221bd60ea0fSDavid Sterba 	if (!inode_owner_or_capable(inode))
222bd60ea0fSDavid Sterba 		return -EPERM;
223bd60ea0fSDavid Sterba 
224b83cc969SLi Zefan 	if (btrfs_root_readonly(root))
225b83cc969SLi Zefan 		return -EROFS;
226b83cc969SLi Zefan 
2276cbff00fSChristoph Hellwig 	if (copy_from_user(&flags, arg, sizeof(flags)))
2286cbff00fSChristoph Hellwig 		return -EFAULT;
2296cbff00fSChristoph Hellwig 
23075e7cb7fSLiu Bo 	ret = check_flags(flags);
23175e7cb7fSLiu Bo 	if (ret)
23275e7cb7fSLiu Bo 		return ret;
2336cbff00fSChristoph Hellwig 
234e7848683SJan Kara 	ret = mnt_want_write_file(file);
235e7848683SJan Kara 	if (ret)
236e7848683SJan Kara 		return ret;
237e7848683SJan Kara 
2386cbff00fSChristoph Hellwig 	mutex_lock(&inode->i_mutex);
2396cbff00fSChristoph Hellwig 
240f062abf0SLi Zefan 	ip_oldflags = ip->flags;
241f062abf0SLi Zefan 	i_oldflags = inode->i_flags;
2427e97b8daSDavid Sterba 	mode = inode->i_mode;
243f062abf0SLi Zefan 
2446cbff00fSChristoph Hellwig 	flags = btrfs_mask_flags(inode->i_mode, flags);
2456cbff00fSChristoph Hellwig 	oldflags = btrfs_flags_to_ioctl(ip->flags);
2466cbff00fSChristoph Hellwig 	if ((flags ^ oldflags) & (FS_APPEND_FL | FS_IMMUTABLE_FL)) {
2476cbff00fSChristoph Hellwig 		if (!capable(CAP_LINUX_IMMUTABLE)) {
2486cbff00fSChristoph Hellwig 			ret = -EPERM;
2496cbff00fSChristoph Hellwig 			goto out_unlock;
2506cbff00fSChristoph Hellwig 		}
2516cbff00fSChristoph Hellwig 	}
2526cbff00fSChristoph Hellwig 
2536cbff00fSChristoph Hellwig 	if (flags & FS_SYNC_FL)
2546cbff00fSChristoph Hellwig 		ip->flags |= BTRFS_INODE_SYNC;
2556cbff00fSChristoph Hellwig 	else
2566cbff00fSChristoph Hellwig 		ip->flags &= ~BTRFS_INODE_SYNC;
2576cbff00fSChristoph Hellwig 	if (flags & FS_IMMUTABLE_FL)
2586cbff00fSChristoph Hellwig 		ip->flags |= BTRFS_INODE_IMMUTABLE;
2596cbff00fSChristoph Hellwig 	else
2606cbff00fSChristoph Hellwig 		ip->flags &= ~BTRFS_INODE_IMMUTABLE;
2616cbff00fSChristoph Hellwig 	if (flags & FS_APPEND_FL)
2626cbff00fSChristoph Hellwig 		ip->flags |= BTRFS_INODE_APPEND;
2636cbff00fSChristoph Hellwig 	else
2646cbff00fSChristoph Hellwig 		ip->flags &= ~BTRFS_INODE_APPEND;
2656cbff00fSChristoph Hellwig 	if (flags & FS_NODUMP_FL)
2666cbff00fSChristoph Hellwig 		ip->flags |= BTRFS_INODE_NODUMP;
2676cbff00fSChristoph Hellwig 	else
2686cbff00fSChristoph Hellwig 		ip->flags &= ~BTRFS_INODE_NODUMP;
2696cbff00fSChristoph Hellwig 	if (flags & FS_NOATIME_FL)
2706cbff00fSChristoph Hellwig 		ip->flags |= BTRFS_INODE_NOATIME;
2716cbff00fSChristoph Hellwig 	else
2726cbff00fSChristoph Hellwig 		ip->flags &= ~BTRFS_INODE_NOATIME;
2736cbff00fSChristoph Hellwig 	if (flags & FS_DIRSYNC_FL)
2746cbff00fSChristoph Hellwig 		ip->flags |= BTRFS_INODE_DIRSYNC;
2756cbff00fSChristoph Hellwig 	else
2766cbff00fSChristoph Hellwig 		ip->flags &= ~BTRFS_INODE_DIRSYNC;
2777e97b8daSDavid Sterba 	if (flags & FS_NOCOW_FL) {
2787e97b8daSDavid Sterba 		if (S_ISREG(mode)) {
2797e97b8daSDavid Sterba 			/*
2807e97b8daSDavid Sterba 			 * It's safe to turn csums off here, no extents exist.
2817e97b8daSDavid Sterba 			 * Otherwise we want the flag to reflect the real COW
2827e97b8daSDavid Sterba 			 * status of the file and will not set it.
2837e97b8daSDavid Sterba 			 */
2847e97b8daSDavid Sterba 			if (inode->i_size == 0)
2857e97b8daSDavid Sterba 				ip->flags |= BTRFS_INODE_NODATACOW
2867e97b8daSDavid Sterba 					   | BTRFS_INODE_NODATASUM;
2877e97b8daSDavid Sterba 		} else {
288e1e8fb6aSLi Zefan 			ip->flags |= BTRFS_INODE_NODATACOW;
2897e97b8daSDavid Sterba 		}
2907e97b8daSDavid Sterba 	} else {
2917e97b8daSDavid Sterba 		/*
2927e97b8daSDavid Sterba 		 * Revert back under same assuptions as above
2937e97b8daSDavid Sterba 		 */
2947e97b8daSDavid Sterba 		if (S_ISREG(mode)) {
2957e97b8daSDavid Sterba 			if (inode->i_size == 0)
2967e97b8daSDavid Sterba 				ip->flags &= ~(BTRFS_INODE_NODATACOW
2977e97b8daSDavid Sterba 				             | BTRFS_INODE_NODATASUM);
2987e97b8daSDavid Sterba 		} else {
299e1e8fb6aSLi Zefan 			ip->flags &= ~BTRFS_INODE_NODATACOW;
3007e97b8daSDavid Sterba 		}
3017e97b8daSDavid Sterba 	}
3026cbff00fSChristoph Hellwig 
30375e7cb7fSLiu Bo 	/*
30475e7cb7fSLiu Bo 	 * The COMPRESS flag can only be changed by users, while the NOCOMPRESS
30575e7cb7fSLiu Bo 	 * flag may be changed automatically if compression code won't make
30675e7cb7fSLiu Bo 	 * things smaller.
30775e7cb7fSLiu Bo 	 */
30875e7cb7fSLiu Bo 	if (flags & FS_NOCOMP_FL) {
30975e7cb7fSLiu Bo 		ip->flags &= ~BTRFS_INODE_COMPRESS;
31075e7cb7fSLiu Bo 		ip->flags |= BTRFS_INODE_NOCOMPRESS;
31163541927SFilipe David Borba Manana 
31263541927SFilipe David Borba Manana 		ret = btrfs_set_prop(inode, "btrfs.compression", NULL, 0, 0);
31363541927SFilipe David Borba Manana 		if (ret && ret != -ENODATA)
31463541927SFilipe David Borba Manana 			goto out_drop;
31575e7cb7fSLiu Bo 	} else if (flags & FS_COMPR_FL) {
31663541927SFilipe David Borba Manana 		const char *comp;
31763541927SFilipe David Borba Manana 
31875e7cb7fSLiu Bo 		ip->flags |= BTRFS_INODE_COMPRESS;
31975e7cb7fSLiu Bo 		ip->flags &= ~BTRFS_INODE_NOCOMPRESS;
32063541927SFilipe David Borba Manana 
32163541927SFilipe David Borba Manana 		if (root->fs_info->compress_type == BTRFS_COMPRESS_LZO)
32263541927SFilipe David Borba Manana 			comp = "lzo";
32363541927SFilipe David Borba Manana 		else
32463541927SFilipe David Borba Manana 			comp = "zlib";
32563541927SFilipe David Borba Manana 		ret = btrfs_set_prop(inode, "btrfs.compression",
32663541927SFilipe David Borba Manana 				     comp, strlen(comp), 0);
32763541927SFilipe David Borba Manana 		if (ret)
32863541927SFilipe David Borba Manana 			goto out_drop;
32963541927SFilipe David Borba Manana 
330ebcb904dSLi Zefan 	} else {
331ebcb904dSLi Zefan 		ip->flags &= ~(BTRFS_INODE_COMPRESS | BTRFS_INODE_NOCOMPRESS);
33275e7cb7fSLiu Bo 	}
3336cbff00fSChristoph Hellwig 
3344da6f1a3SLi Zefan 	trans = btrfs_start_transaction(root, 1);
335f062abf0SLi Zefan 	if (IS_ERR(trans)) {
336f062abf0SLi Zefan 		ret = PTR_ERR(trans);
337f062abf0SLi Zefan 		goto out_drop;
338f062abf0SLi Zefan 	}
3396cbff00fSChristoph Hellwig 
340306424ccSLi Zefan 	btrfs_update_iflags(inode);
3410c4d2d95SJosef Bacik 	inode_inc_iversion(inode);
342306424ccSLi Zefan 	inode->i_ctime = CURRENT_TIME;
3436cbff00fSChristoph Hellwig 	ret = btrfs_update_inode(trans, root, inode);
3446cbff00fSChristoph Hellwig 
3456cbff00fSChristoph Hellwig 	btrfs_end_transaction(trans, root);
346f062abf0SLi Zefan  out_drop:
347f062abf0SLi Zefan 	if (ret) {
348f062abf0SLi Zefan 		ip->flags = ip_oldflags;
349f062abf0SLi Zefan 		inode->i_flags = i_oldflags;
350f062abf0SLi Zefan 	}
3516cbff00fSChristoph Hellwig 
3526cbff00fSChristoph Hellwig  out_unlock:
3536cbff00fSChristoph Hellwig 	mutex_unlock(&inode->i_mutex);
354e7848683SJan Kara 	mnt_drop_write_file(file);
3552d4e6f6aSliubo 	return ret;
3566cbff00fSChristoph Hellwig }
3576cbff00fSChristoph Hellwig 
3586cbff00fSChristoph Hellwig static int btrfs_ioctl_getversion(struct file *file, int __user *arg)
3596cbff00fSChristoph Hellwig {
360496ad9aaSAl Viro 	struct inode *inode = file_inode(file);
3616cbff00fSChristoph Hellwig 
3626cbff00fSChristoph Hellwig 	return put_user(inode->i_generation, arg);
3636cbff00fSChristoph Hellwig }
364f46b5a66SChristoph Hellwig 
365f7039b1dSLi Dongyang static noinline int btrfs_ioctl_fitrim(struct file *file, void __user *arg)
366f7039b1dSLi Dongyang {
36754563d41SAl Viro 	struct btrfs_fs_info *fs_info = btrfs_sb(file_inode(file)->i_sb);
368f7039b1dSLi Dongyang 	struct btrfs_device *device;
369f7039b1dSLi Dongyang 	struct request_queue *q;
370f7039b1dSLi Dongyang 	struct fstrim_range range;
371f7039b1dSLi Dongyang 	u64 minlen = ULLONG_MAX;
372f7039b1dSLi Dongyang 	u64 num_devices = 0;
373815745cfSAl Viro 	u64 total_bytes = btrfs_super_total_bytes(fs_info->super_copy);
374f7039b1dSLi Dongyang 	int ret;
375f7039b1dSLi Dongyang 
376f7039b1dSLi Dongyang 	if (!capable(CAP_SYS_ADMIN))
377f7039b1dSLi Dongyang 		return -EPERM;
378f7039b1dSLi Dongyang 
3791f78160cSXiao Guangrong 	rcu_read_lock();
3801f78160cSXiao Guangrong 	list_for_each_entry_rcu(device, &fs_info->fs_devices->devices,
3811f78160cSXiao Guangrong 				dev_list) {
382f7039b1dSLi Dongyang 		if (!device->bdev)
383f7039b1dSLi Dongyang 			continue;
384f7039b1dSLi Dongyang 		q = bdev_get_queue(device->bdev);
385f7039b1dSLi Dongyang 		if (blk_queue_discard(q)) {
386f7039b1dSLi Dongyang 			num_devices++;
387f7039b1dSLi Dongyang 			minlen = min((u64)q->limits.discard_granularity,
388f7039b1dSLi Dongyang 				     minlen);
389f7039b1dSLi Dongyang 		}
390f7039b1dSLi Dongyang 	}
3911f78160cSXiao Guangrong 	rcu_read_unlock();
392f4c697e6SLukas Czerner 
393f7039b1dSLi Dongyang 	if (!num_devices)
394f7039b1dSLi Dongyang 		return -EOPNOTSUPP;
395f7039b1dSLi Dongyang 	if (copy_from_user(&range, arg, sizeof(range)))
396f7039b1dSLi Dongyang 		return -EFAULT;
397e515c18bSLukas Czerner 	if (range.start > total_bytes ||
398e515c18bSLukas Czerner 	    range.len < fs_info->sb->s_blocksize)
399f4c697e6SLukas Czerner 		return -EINVAL;
400f7039b1dSLi Dongyang 
401f4c697e6SLukas Czerner 	range.len = min(range.len, total_bytes - range.start);
402f7039b1dSLi Dongyang 	range.minlen = max(range.minlen, minlen);
403815745cfSAl Viro 	ret = btrfs_trim_fs(fs_info->tree_root, &range);
404f7039b1dSLi Dongyang 	if (ret < 0)
405f7039b1dSLi Dongyang 		return ret;
406f7039b1dSLi Dongyang 
407f7039b1dSLi Dongyang 	if (copy_to_user(arg, &range, sizeof(range)))
408f7039b1dSLi Dongyang 		return -EFAULT;
409f7039b1dSLi Dongyang 
410f7039b1dSLi Dongyang 	return 0;
411f7039b1dSLi Dongyang }
412f7039b1dSLi Dongyang 
413dd5f9615SStefan Behrens int btrfs_is_empty_uuid(u8 *uuid)
414dd5f9615SStefan Behrens {
41546e0f66aSChris Mason 	int i;
41646e0f66aSChris Mason 
41746e0f66aSChris Mason 	for (i = 0; i < BTRFS_UUID_SIZE; i++) {
41846e0f66aSChris Mason 		if (uuid[i])
41946e0f66aSChris Mason 			return 0;
42046e0f66aSChris Mason 	}
42146e0f66aSChris Mason 	return 1;
422dd5f9615SStefan Behrens }
423dd5f9615SStefan Behrens 
424d5c12070SMiao Xie static noinline int create_subvol(struct inode *dir,
425cb8e7090SChristoph Hellwig 				  struct dentry *dentry,
42672fd032eSSage Weil 				  char *name, int namelen,
4276f72c7e2SArne Jansen 				  u64 *async_transid,
4288696c533SMiao Xie 				  struct btrfs_qgroup_inherit *inherit)
429f46b5a66SChristoph Hellwig {
430f46b5a66SChristoph Hellwig 	struct btrfs_trans_handle *trans;
431f46b5a66SChristoph Hellwig 	struct btrfs_key key;
432f46b5a66SChristoph Hellwig 	struct btrfs_root_item root_item;
433f46b5a66SChristoph Hellwig 	struct btrfs_inode_item *inode_item;
434f46b5a66SChristoph Hellwig 	struct extent_buffer *leaf;
435d5c12070SMiao Xie 	struct btrfs_root *root = BTRFS_I(dir)->root;
43676dda93cSYan, Zheng 	struct btrfs_root *new_root;
437d5c12070SMiao Xie 	struct btrfs_block_rsv block_rsv;
4388ea05e3aSAlexander Block 	struct timespec cur_time = CURRENT_TIME;
4395662344bSTsutomu Itoh 	struct inode *inode;
440f46b5a66SChristoph Hellwig 	int ret;
441f46b5a66SChristoph Hellwig 	int err;
442f46b5a66SChristoph Hellwig 	u64 objectid;
443f46b5a66SChristoph Hellwig 	u64 new_dirid = BTRFS_FIRST_FREE_OBJECTID;
4443de4586cSChris Mason 	u64 index = 0;
445d5c12070SMiao Xie 	u64 qgroup_reserved;
4468ea05e3aSAlexander Block 	uuid_le new_uuid;
447f46b5a66SChristoph Hellwig 
448581bb050SLi Zefan 	ret = btrfs_find_free_objectid(root->fs_info->tree_root, &objectid);
4492fbe8c8aSAl Viro 	if (ret)
450a22285a6SYan, Zheng 		return ret;
4516a912213SJosef Bacik 
452d5c12070SMiao Xie 	btrfs_init_block_rsv(&block_rsv, BTRFS_BLOCK_RSV_TEMP);
4539ed74f2dSJosef Bacik 	/*
454d5c12070SMiao Xie 	 * The same as the snapshot creation, please see the comment
455d5c12070SMiao Xie 	 * of create_snapshot().
4569ed74f2dSJosef Bacik 	 */
457d5c12070SMiao Xie 	ret = btrfs_subvolume_reserve_metadata(root, &block_rsv,
458dd5f9615SStefan Behrens 					       8, &qgroup_reserved, false);
459d5c12070SMiao Xie 	if (ret)
460d5c12070SMiao Xie 		return ret;
461f46b5a66SChristoph Hellwig 
462d5c12070SMiao Xie 	trans = btrfs_start_transaction(root, 0);
463d5c12070SMiao Xie 	if (IS_ERR(trans)) {
464d5c12070SMiao Xie 		ret = PTR_ERR(trans);
465de6e8200SLiu Bo 		btrfs_subvolume_release_metadata(root, &block_rsv,
466de6e8200SLiu Bo 						 qgroup_reserved);
467de6e8200SLiu Bo 		return ret;
468d5c12070SMiao Xie 	}
469d5c12070SMiao Xie 	trans->block_rsv = &block_rsv;
470d5c12070SMiao Xie 	trans->bytes_reserved = block_rsv.size;
471f46b5a66SChristoph Hellwig 
4728696c533SMiao Xie 	ret = btrfs_qgroup_inherit(trans, root->fs_info, 0, objectid, inherit);
4736f72c7e2SArne Jansen 	if (ret)
4746f72c7e2SArne Jansen 		goto fail;
4756f72c7e2SArne Jansen 
4765d4f98a2SYan Zheng 	leaf = btrfs_alloc_free_block(trans, root, root->leafsize,
4775581a51aSJan Schmidt 				      0, objectid, NULL, 0, 0, 0);
4788e8a1e31SJosef Bacik 	if (IS_ERR(leaf)) {
4798e8a1e31SJosef Bacik 		ret = PTR_ERR(leaf);
4808e8a1e31SJosef Bacik 		goto fail;
4818e8a1e31SJosef Bacik 	}
482f46b5a66SChristoph Hellwig 
4835d4f98a2SYan Zheng 	memset_extent_buffer(leaf, 0, 0, sizeof(struct btrfs_header));
484f46b5a66SChristoph Hellwig 	btrfs_set_header_bytenr(leaf, leaf->start);
485f46b5a66SChristoph Hellwig 	btrfs_set_header_generation(leaf, trans->transid);
4865d4f98a2SYan Zheng 	btrfs_set_header_backref_rev(leaf, BTRFS_MIXED_BACKREF_REV);
487f46b5a66SChristoph Hellwig 	btrfs_set_header_owner(leaf, objectid);
488f46b5a66SChristoph Hellwig 
4890a4e5586SRoss Kirk 	write_extent_buffer(leaf, root->fs_info->fsid, btrfs_header_fsid(),
490f46b5a66SChristoph Hellwig 			    BTRFS_FSID_SIZE);
4915d4f98a2SYan Zheng 	write_extent_buffer(leaf, root->fs_info->chunk_tree_uuid,
492b308bc2fSGeert Uytterhoeven 			    btrfs_header_chunk_tree_uuid(leaf),
4935d4f98a2SYan Zheng 			    BTRFS_UUID_SIZE);
494f46b5a66SChristoph Hellwig 	btrfs_mark_buffer_dirty(leaf);
495f46b5a66SChristoph Hellwig 
4968ea05e3aSAlexander Block 	memset(&root_item, 0, sizeof(root_item));
4978ea05e3aSAlexander Block 
498f46b5a66SChristoph Hellwig 	inode_item = &root_item.inode;
4993cae210fSQu Wenruo 	btrfs_set_stack_inode_generation(inode_item, 1);
5003cae210fSQu Wenruo 	btrfs_set_stack_inode_size(inode_item, 3);
5013cae210fSQu Wenruo 	btrfs_set_stack_inode_nlink(inode_item, 1);
5023cae210fSQu Wenruo 	btrfs_set_stack_inode_nbytes(inode_item, root->leafsize);
5033cae210fSQu Wenruo 	btrfs_set_stack_inode_mode(inode_item, S_IFDIR | 0755);
504f46b5a66SChristoph Hellwig 
5053cae210fSQu Wenruo 	btrfs_set_root_flags(&root_item, 0);
5063cae210fSQu Wenruo 	btrfs_set_root_limit(&root_item, 0);
5073cae210fSQu Wenruo 	btrfs_set_stack_inode_flags(inode_item, BTRFS_INODE_ROOT_ITEM_INIT);
50808fe4db1SLi Zefan 
509f46b5a66SChristoph Hellwig 	btrfs_set_root_bytenr(&root_item, leaf->start);
51084234f3aSYan Zheng 	btrfs_set_root_generation(&root_item, trans->transid);
511f46b5a66SChristoph Hellwig 	btrfs_set_root_level(&root_item, 0);
512f46b5a66SChristoph Hellwig 	btrfs_set_root_refs(&root_item, 1);
51386b9f2ecSYan, Zheng 	btrfs_set_root_used(&root_item, leaf->len);
51480ff3856SYan Zheng 	btrfs_set_root_last_snapshot(&root_item, 0);
515f46b5a66SChristoph Hellwig 
5168ea05e3aSAlexander Block 	btrfs_set_root_generation_v2(&root_item,
5178ea05e3aSAlexander Block 			btrfs_root_generation(&root_item));
5188ea05e3aSAlexander Block 	uuid_le_gen(&new_uuid);
5198ea05e3aSAlexander Block 	memcpy(root_item.uuid, new_uuid.b, BTRFS_UUID_SIZE);
5203cae210fSQu Wenruo 	btrfs_set_stack_timespec_sec(&root_item.otime, cur_time.tv_sec);
5213cae210fSQu Wenruo 	btrfs_set_stack_timespec_nsec(&root_item.otime, cur_time.tv_nsec);
5228ea05e3aSAlexander Block 	root_item.ctime = root_item.otime;
5238ea05e3aSAlexander Block 	btrfs_set_root_ctransid(&root_item, trans->transid);
5248ea05e3aSAlexander Block 	btrfs_set_root_otransid(&root_item, trans->transid);
525f46b5a66SChristoph Hellwig 
526925baeddSChris Mason 	btrfs_tree_unlock(leaf);
527f46b5a66SChristoph Hellwig 	free_extent_buffer(leaf);
528f46b5a66SChristoph Hellwig 	leaf = NULL;
529f46b5a66SChristoph Hellwig 
530f46b5a66SChristoph Hellwig 	btrfs_set_root_dirid(&root_item, new_dirid);
531f46b5a66SChristoph Hellwig 
532f46b5a66SChristoph Hellwig 	key.objectid = objectid;
5335d4f98a2SYan Zheng 	key.offset = 0;
534f46b5a66SChristoph Hellwig 	btrfs_set_key_type(&key, BTRFS_ROOT_ITEM_KEY);
535f46b5a66SChristoph Hellwig 	ret = btrfs_insert_root(trans, root->fs_info->tree_root, &key,
536f46b5a66SChristoph Hellwig 				&root_item);
537f46b5a66SChristoph Hellwig 	if (ret)
538f46b5a66SChristoph Hellwig 		goto fail;
539f46b5a66SChristoph Hellwig 
54076dda93cSYan, Zheng 	key.offset = (u64)-1;
54176dda93cSYan, Zheng 	new_root = btrfs_read_fs_root_no_name(root->fs_info, &key);
54279787eaaSJeff Mahoney 	if (IS_ERR(new_root)) {
54379787eaaSJeff Mahoney 		btrfs_abort_transaction(trans, root, PTR_ERR(new_root));
54479787eaaSJeff Mahoney 		ret = PTR_ERR(new_root);
54579787eaaSJeff Mahoney 		goto fail;
54679787eaaSJeff Mahoney 	}
54776dda93cSYan, Zheng 
54876dda93cSYan, Zheng 	btrfs_record_root_in_trans(trans, new_root);
54976dda93cSYan, Zheng 
55063541927SFilipe David Borba Manana 	ret = btrfs_create_subvol_root(trans, new_root, root, new_dirid);
551ce598979SMark Fasheh 	if (ret) {
552ce598979SMark Fasheh 		/* We potentially lose an unused inode item here */
55379787eaaSJeff Mahoney 		btrfs_abort_transaction(trans, root, ret);
554ce598979SMark Fasheh 		goto fail;
555ce598979SMark Fasheh 	}
556ce598979SMark Fasheh 
557f46b5a66SChristoph Hellwig 	/*
558f46b5a66SChristoph Hellwig 	 * insert the directory item
559f46b5a66SChristoph Hellwig 	 */
5603de4586cSChris Mason 	ret = btrfs_set_inode_index(dir, &index);
56179787eaaSJeff Mahoney 	if (ret) {
56279787eaaSJeff Mahoney 		btrfs_abort_transaction(trans, root, ret);
56379787eaaSJeff Mahoney 		goto fail;
56479787eaaSJeff Mahoney 	}
5653de4586cSChris Mason 
5663de4586cSChris Mason 	ret = btrfs_insert_dir_item(trans, root,
56716cdcec7SMiao Xie 				    name, namelen, dir, &key,
5683de4586cSChris Mason 				    BTRFS_FT_DIR, index);
56979787eaaSJeff Mahoney 	if (ret) {
57079787eaaSJeff Mahoney 		btrfs_abort_transaction(trans, root, ret);
571f46b5a66SChristoph Hellwig 		goto fail;
57279787eaaSJeff Mahoney 	}
5730660b5afSChris Mason 
57452c26179SYan Zheng 	btrfs_i_size_write(dir, dir->i_size + namelen * 2);
57552c26179SYan Zheng 	ret = btrfs_update_inode(trans, root, dir);
57652c26179SYan Zheng 	BUG_ON(ret);
57752c26179SYan Zheng 
5780660b5afSChris Mason 	ret = btrfs_add_root_ref(trans, root->fs_info->tree_root,
5794df27c4dSYan, Zheng 				 objectid, root->root_key.objectid,
58033345d01SLi Zefan 				 btrfs_ino(dir), index, name, namelen);
5810660b5afSChris Mason 	BUG_ON(ret);
5820660b5afSChris Mason 
583dd5f9615SStefan Behrens 	ret = btrfs_uuid_tree_add(trans, root->fs_info->uuid_root,
584dd5f9615SStefan Behrens 				  root_item.uuid, BTRFS_UUID_KEY_SUBVOL,
585dd5f9615SStefan Behrens 				  objectid);
586dd5f9615SStefan Behrens 	if (ret)
587dd5f9615SStefan Behrens 		btrfs_abort_transaction(trans, root, ret);
588dd5f9615SStefan Behrens 
589f46b5a66SChristoph Hellwig fail:
590d5c12070SMiao Xie 	trans->block_rsv = NULL;
591d5c12070SMiao Xie 	trans->bytes_reserved = 0;
592de6e8200SLiu Bo 	btrfs_subvolume_release_metadata(root, &block_rsv, qgroup_reserved);
593de6e8200SLiu Bo 
59472fd032eSSage Weil 	if (async_transid) {
59572fd032eSSage Weil 		*async_transid = trans->transid;
59672fd032eSSage Weil 		err = btrfs_commit_transaction_async(trans, root, 1);
59700d71c9cSMiao Xie 		if (err)
59800d71c9cSMiao Xie 			err = btrfs_commit_transaction(trans, root);
59972fd032eSSage Weil 	} else {
60076dda93cSYan, Zheng 		err = btrfs_commit_transaction(trans, root);
60172fd032eSSage Weil 	}
602f46b5a66SChristoph Hellwig 	if (err && !ret)
603f46b5a66SChristoph Hellwig 		ret = err;
6041a65e24bSChris Mason 
6055662344bSTsutomu Itoh 	if (!ret) {
6065662344bSTsutomu Itoh 		inode = btrfs_lookup_dentry(dir, dentry);
607de6e8200SLiu Bo 		if (IS_ERR(inode))
608de6e8200SLiu Bo 			return PTR_ERR(inode);
6095662344bSTsutomu Itoh 		d_instantiate(dentry, inode);
6105662344bSTsutomu Itoh 	}
611f46b5a66SChristoph Hellwig 	return ret;
612f46b5a66SChristoph Hellwig }
613f46b5a66SChristoph Hellwig 
6148257b2dcSMiao Xie static void btrfs_wait_nocow_write(struct btrfs_root *root)
6158257b2dcSMiao Xie {
6168257b2dcSMiao Xie 	s64 writers;
6178257b2dcSMiao Xie 	DEFINE_WAIT(wait);
6188257b2dcSMiao Xie 
6198257b2dcSMiao Xie 	do {
6208257b2dcSMiao Xie 		prepare_to_wait(&root->subv_writers->wait, &wait,
6218257b2dcSMiao Xie 				TASK_UNINTERRUPTIBLE);
6228257b2dcSMiao Xie 
6238257b2dcSMiao Xie 		writers = percpu_counter_sum(&root->subv_writers->counter);
6248257b2dcSMiao Xie 		if (writers)
6258257b2dcSMiao Xie 			schedule();
6268257b2dcSMiao Xie 
6278257b2dcSMiao Xie 		finish_wait(&root->subv_writers->wait, &wait);
6288257b2dcSMiao Xie 	} while (writers);
6298257b2dcSMiao Xie }
6308257b2dcSMiao Xie 
631e9662f70SMiao Xie static int create_snapshot(struct btrfs_root *root, struct inode *dir,
632e9662f70SMiao Xie 			   struct dentry *dentry, char *name, int namelen,
633e9662f70SMiao Xie 			   u64 *async_transid, bool readonly,
634e9662f70SMiao Xie 			   struct btrfs_qgroup_inherit *inherit)
635f46b5a66SChristoph Hellwig {
6362e4bfab9SYan, Zheng 	struct inode *inode;
637f46b5a66SChristoph Hellwig 	struct btrfs_pending_snapshot *pending_snapshot;
638f46b5a66SChristoph Hellwig 	struct btrfs_trans_handle *trans;
6392e4bfab9SYan, Zheng 	int ret;
640f46b5a66SChristoph Hellwig 
641f46b5a66SChristoph Hellwig 	if (!root->ref_cows)
642f46b5a66SChristoph Hellwig 		return -EINVAL;
643f46b5a66SChristoph Hellwig 
6448257b2dcSMiao Xie 	atomic_inc(&root->will_be_snapshoted);
6458257b2dcSMiao Xie 	smp_mb__after_atomic_inc();
6468257b2dcSMiao Xie 	btrfs_wait_nocow_write(root);
6478257b2dcSMiao Xie 
6486a03843dSMiao Xie 	ret = btrfs_start_delalloc_inodes(root, 0);
6496a03843dSMiao Xie 	if (ret)
6508257b2dcSMiao Xie 		goto out;
6516a03843dSMiao Xie 
652b0244199SMiao Xie 	btrfs_wait_ordered_extents(root, -1);
6536a03843dSMiao Xie 
654a22285a6SYan, Zheng 	pending_snapshot = kzalloc(sizeof(*pending_snapshot), GFP_NOFS);
6558257b2dcSMiao Xie 	if (!pending_snapshot) {
6568257b2dcSMiao Xie 		ret = -ENOMEM;
6578257b2dcSMiao Xie 		goto out;
6588257b2dcSMiao Xie 	}
659a22285a6SYan, Zheng 
66066d8f3ddSMiao Xie 	btrfs_init_block_rsv(&pending_snapshot->block_rsv,
66166d8f3ddSMiao Xie 			     BTRFS_BLOCK_RSV_TEMP);
662d5c12070SMiao Xie 	/*
663d5c12070SMiao Xie 	 * 1 - parent dir inode
664d5c12070SMiao Xie 	 * 2 - dir entries
665d5c12070SMiao Xie 	 * 1 - root item
666d5c12070SMiao Xie 	 * 2 - root ref/backref
667d5c12070SMiao Xie 	 * 1 - root of snapshot
668dd5f9615SStefan Behrens 	 * 1 - UUID item
669d5c12070SMiao Xie 	 */
670d5c12070SMiao Xie 	ret = btrfs_subvolume_reserve_metadata(BTRFS_I(dir)->root,
671dd5f9615SStefan Behrens 					&pending_snapshot->block_rsv, 8,
672ee3441b4SJeff Mahoney 					&pending_snapshot->qgroup_reserved,
673ee3441b4SJeff Mahoney 					false);
674d5c12070SMiao Xie 	if (ret)
6758257b2dcSMiao Xie 		goto free;
676d5c12070SMiao Xie 
677a22285a6SYan, Zheng 	pending_snapshot->dentry = dentry;
678a22285a6SYan, Zheng 	pending_snapshot->root = root;
679b83cc969SLi Zefan 	pending_snapshot->readonly = readonly;
680e9662f70SMiao Xie 	pending_snapshot->dir = dir;
6818696c533SMiao Xie 	pending_snapshot->inherit = inherit;
682a22285a6SYan, Zheng 
683d5c12070SMiao Xie 	trans = btrfs_start_transaction(root, 0);
684a22285a6SYan, Zheng 	if (IS_ERR(trans)) {
685a22285a6SYan, Zheng 		ret = PTR_ERR(trans);
686a22285a6SYan, Zheng 		goto fail;
687a22285a6SYan, Zheng 	}
688a22285a6SYan, Zheng 
6898351583eSJosef Bacik 	spin_lock(&root->fs_info->trans_lock);
690a22285a6SYan, Zheng 	list_add(&pending_snapshot->list,
691a22285a6SYan, Zheng 		 &trans->transaction->pending_snapshots);
6928351583eSJosef Bacik 	spin_unlock(&root->fs_info->trans_lock);
69372fd032eSSage Weil 	if (async_transid) {
69472fd032eSSage Weil 		*async_transid = trans->transid;
69572fd032eSSage Weil 		ret = btrfs_commit_transaction_async(trans,
69672fd032eSSage Weil 				     root->fs_info->extent_root, 1);
69700d71c9cSMiao Xie 		if (ret)
69800d71c9cSMiao Xie 			ret = btrfs_commit_transaction(trans, root);
69972fd032eSSage Weil 	} else {
70072fd032eSSage Weil 		ret = btrfs_commit_transaction(trans,
70172fd032eSSage Weil 					       root->fs_info->extent_root);
70272fd032eSSage Weil 	}
703aec8030aSMiao Xie 	if (ret)
704c37b2b62SJosef Bacik 		goto fail;
705a22285a6SYan, Zheng 
706a22285a6SYan, Zheng 	ret = pending_snapshot->error;
707f46b5a66SChristoph Hellwig 	if (ret)
7082e4bfab9SYan, Zheng 		goto fail;
709f46b5a66SChristoph Hellwig 
71066b4ffd1SJosef Bacik 	ret = btrfs_orphan_cleanup(pending_snapshot->snap);
71166b4ffd1SJosef Bacik 	if (ret)
71266b4ffd1SJosef Bacik 		goto fail;
713f46b5a66SChristoph Hellwig 
7142fbe8c8aSAl Viro 	inode = btrfs_lookup_dentry(dentry->d_parent->d_inode, dentry);
7152e4bfab9SYan, Zheng 	if (IS_ERR(inode)) {
7162e4bfab9SYan, Zheng 		ret = PTR_ERR(inode);
7172e4bfab9SYan, Zheng 		goto fail;
7182e4bfab9SYan, Zheng 	}
7195662344bSTsutomu Itoh 
7202e4bfab9SYan, Zheng 	d_instantiate(dentry, inode);
7212e4bfab9SYan, Zheng 	ret = 0;
7222e4bfab9SYan, Zheng fail:
723d5c12070SMiao Xie 	btrfs_subvolume_release_metadata(BTRFS_I(dir)->root,
724d5c12070SMiao Xie 					 &pending_snapshot->block_rsv,
725d5c12070SMiao Xie 					 pending_snapshot->qgroup_reserved);
7268257b2dcSMiao Xie free:
727a22285a6SYan, Zheng 	kfree(pending_snapshot);
7288257b2dcSMiao Xie out:
7298257b2dcSMiao Xie 	atomic_dec(&root->will_be_snapshoted);
730f46b5a66SChristoph Hellwig 	return ret;
731f46b5a66SChristoph Hellwig }
732f46b5a66SChristoph Hellwig 
7334260f7c7SSage Weil /*  copy of check_sticky in fs/namei.c()
7344260f7c7SSage Weil * It's inline, so penalty for filesystems that don't use sticky bit is
7354260f7c7SSage Weil * minimal.
7364260f7c7SSage Weil */
7374260f7c7SSage Weil static inline int btrfs_check_sticky(struct inode *dir, struct inode *inode)
7384260f7c7SSage Weil {
7392f2f43d3SEric W. Biederman 	kuid_t fsuid = current_fsuid();
7404260f7c7SSage Weil 
7414260f7c7SSage Weil 	if (!(dir->i_mode & S_ISVTX))
7424260f7c7SSage Weil 		return 0;
7432f2f43d3SEric W. Biederman 	if (uid_eq(inode->i_uid, fsuid))
7444260f7c7SSage Weil 		return 0;
7452f2f43d3SEric W. Biederman 	if (uid_eq(dir->i_uid, fsuid))
7464260f7c7SSage Weil 		return 0;
7474260f7c7SSage Weil 	return !capable(CAP_FOWNER);
7484260f7c7SSage Weil }
7494260f7c7SSage Weil 
7504260f7c7SSage Weil /*  copy of may_delete in fs/namei.c()
7514260f7c7SSage Weil  *	Check whether we can remove a link victim from directory dir, check
7524260f7c7SSage Weil  *  whether the type of victim is right.
7534260f7c7SSage Weil  *  1. We can't do it if dir is read-only (done in permission())
7544260f7c7SSage Weil  *  2. We should have write and exec permissions on dir
7554260f7c7SSage Weil  *  3. We can't remove anything from append-only dir
7564260f7c7SSage Weil  *  4. We can't do anything with immutable dir (done in permission())
7574260f7c7SSage Weil  *  5. If the sticky bit on dir is set we should either
7584260f7c7SSage Weil  *	a. be owner of dir, or
7594260f7c7SSage Weil  *	b. be owner of victim, or
7604260f7c7SSage Weil  *	c. have CAP_FOWNER capability
7614260f7c7SSage Weil  *  6. If the victim is append-only or immutable we can't do antyhing with
7624260f7c7SSage Weil  *     links pointing to it.
7634260f7c7SSage Weil  *  7. If we were asked to remove a directory and victim isn't one - ENOTDIR.
7644260f7c7SSage Weil  *  8. If we were asked to remove a non-directory and victim isn't one - EISDIR.
7654260f7c7SSage Weil  *  9. We can't remove a root or mountpoint.
7664260f7c7SSage Weil  * 10. We don't allow removal of NFS sillyrenamed files; it's handled by
7674260f7c7SSage Weil  *     nfs_async_unlink().
7684260f7c7SSage Weil  */
7694260f7c7SSage Weil 
7704260f7c7SSage Weil static int btrfs_may_delete(struct inode *dir, struct dentry *victim, int isdir)
7714260f7c7SSage Weil {
7724260f7c7SSage Weil 	int error;
7734260f7c7SSage Weil 
7744260f7c7SSage Weil 	if (!victim->d_inode)
7754260f7c7SSage Weil 		return -ENOENT;
7764260f7c7SSage Weil 
7774260f7c7SSage Weil 	BUG_ON(victim->d_parent->d_inode != dir);
7784fa6b5ecSJeff Layton 	audit_inode_child(dir, victim, AUDIT_TYPE_CHILD_DELETE);
7794260f7c7SSage Weil 
7804260f7c7SSage Weil 	error = inode_permission(dir, MAY_WRITE | MAY_EXEC);
7814260f7c7SSage Weil 	if (error)
7824260f7c7SSage Weil 		return error;
7834260f7c7SSage Weil 	if (IS_APPEND(dir))
7844260f7c7SSage Weil 		return -EPERM;
7854260f7c7SSage Weil 	if (btrfs_check_sticky(dir, victim->d_inode)||
7864260f7c7SSage Weil 		IS_APPEND(victim->d_inode)||
7874260f7c7SSage Weil 	    IS_IMMUTABLE(victim->d_inode) || IS_SWAPFILE(victim->d_inode))
7884260f7c7SSage Weil 		return -EPERM;
7894260f7c7SSage Weil 	if (isdir) {
7904260f7c7SSage Weil 		if (!S_ISDIR(victim->d_inode->i_mode))
7914260f7c7SSage Weil 			return -ENOTDIR;
7924260f7c7SSage Weil 		if (IS_ROOT(victim))
7934260f7c7SSage Weil 			return -EBUSY;
7944260f7c7SSage Weil 	} else if (S_ISDIR(victim->d_inode->i_mode))
7954260f7c7SSage Weil 		return -EISDIR;
7964260f7c7SSage Weil 	if (IS_DEADDIR(dir))
7974260f7c7SSage Weil 		return -ENOENT;
7984260f7c7SSage Weil 	if (victim->d_flags & DCACHE_NFSFS_RENAMED)
7994260f7c7SSage Weil 		return -EBUSY;
8004260f7c7SSage Weil 	return 0;
8014260f7c7SSage Weil }
8024260f7c7SSage Weil 
803cb8e7090SChristoph Hellwig /* copy of may_create in fs/namei.c() */
804cb8e7090SChristoph Hellwig static inline int btrfs_may_create(struct inode *dir, struct dentry *child)
805cb8e7090SChristoph Hellwig {
806cb8e7090SChristoph Hellwig 	if (child->d_inode)
807cb8e7090SChristoph Hellwig 		return -EEXIST;
808cb8e7090SChristoph Hellwig 	if (IS_DEADDIR(dir))
809cb8e7090SChristoph Hellwig 		return -ENOENT;
810cb8e7090SChristoph Hellwig 	return inode_permission(dir, MAY_WRITE | MAY_EXEC);
811cb8e7090SChristoph Hellwig }
812cb8e7090SChristoph Hellwig 
813cb8e7090SChristoph Hellwig /*
814cb8e7090SChristoph Hellwig  * Create a new subvolume below @parent.  This is largely modeled after
815cb8e7090SChristoph Hellwig  * sys_mkdirat and vfs_mkdir, but we only do a single component lookup
816cb8e7090SChristoph Hellwig  * inside this filesystem so it's quite a bit simpler.
817cb8e7090SChristoph Hellwig  */
81876dda93cSYan, Zheng static noinline int btrfs_mksubvol(struct path *parent,
81976dda93cSYan, Zheng 				   char *name, int namelen,
82072fd032eSSage Weil 				   struct btrfs_root *snap_src,
8216f72c7e2SArne Jansen 				   u64 *async_transid, bool readonly,
8228696c533SMiao Xie 				   struct btrfs_qgroup_inherit *inherit)
823cb8e7090SChristoph Hellwig {
82476dda93cSYan, Zheng 	struct inode *dir  = parent->dentry->d_inode;
825cb8e7090SChristoph Hellwig 	struct dentry *dentry;
826cb8e7090SChristoph Hellwig 	int error;
827cb8e7090SChristoph Hellwig 
8285c50c9b8SDavid Sterba 	error = mutex_lock_killable_nested(&dir->i_mutex, I_MUTEX_PARENT);
8295c50c9b8SDavid Sterba 	if (error == -EINTR)
8305c50c9b8SDavid Sterba 		return error;
831cb8e7090SChristoph Hellwig 
832cb8e7090SChristoph Hellwig 	dentry = lookup_one_len(name, parent->dentry, namelen);
833cb8e7090SChristoph Hellwig 	error = PTR_ERR(dentry);
834cb8e7090SChristoph Hellwig 	if (IS_ERR(dentry))
835cb8e7090SChristoph Hellwig 		goto out_unlock;
836cb8e7090SChristoph Hellwig 
837cb8e7090SChristoph Hellwig 	error = -EEXIST;
838cb8e7090SChristoph Hellwig 	if (dentry->d_inode)
839cb8e7090SChristoph Hellwig 		goto out_dput;
840cb8e7090SChristoph Hellwig 
84176dda93cSYan, Zheng 	error = btrfs_may_create(dir, dentry);
842cb8e7090SChristoph Hellwig 	if (error)
843a874a63eSLiu Bo 		goto out_dput;
844cb8e7090SChristoph Hellwig 
8459c52057cSChris Mason 	/*
8469c52057cSChris Mason 	 * even if this name doesn't exist, we may get hash collisions.
8479c52057cSChris Mason 	 * check for them now when we can safely fail
8489c52057cSChris Mason 	 */
8499c52057cSChris Mason 	error = btrfs_check_dir_item_collision(BTRFS_I(dir)->root,
8509c52057cSChris Mason 					       dir->i_ino, name,
8519c52057cSChris Mason 					       namelen);
8529c52057cSChris Mason 	if (error)
8539c52057cSChris Mason 		goto out_dput;
8549c52057cSChris Mason 
85576dda93cSYan, Zheng 	down_read(&BTRFS_I(dir)->root->fs_info->subvol_sem);
85676dda93cSYan, Zheng 
85776dda93cSYan, Zheng 	if (btrfs_root_refs(&BTRFS_I(dir)->root->root_item) == 0)
85876dda93cSYan, Zheng 		goto out_up_read;
85976dda93cSYan, Zheng 
8603de4586cSChris Mason 	if (snap_src) {
861e9662f70SMiao Xie 		error = create_snapshot(snap_src, dir, dentry, name, namelen,
8626f72c7e2SArne Jansen 					async_transid, readonly, inherit);
8633de4586cSChris Mason 	} else {
864d5c12070SMiao Xie 		error = create_subvol(dir, dentry, name, namelen,
865d5c12070SMiao Xie 				      async_transid, inherit);
8663de4586cSChris Mason 	}
86776dda93cSYan, Zheng 	if (!error)
86876dda93cSYan, Zheng 		fsnotify_mkdir(dir, dentry);
86976dda93cSYan, Zheng out_up_read:
87076dda93cSYan, Zheng 	up_read(&BTRFS_I(dir)->root->fs_info->subvol_sem);
871cb8e7090SChristoph Hellwig out_dput:
872cb8e7090SChristoph Hellwig 	dput(dentry);
873cb8e7090SChristoph Hellwig out_unlock:
87476dda93cSYan, Zheng 	mutex_unlock(&dir->i_mutex);
875cb8e7090SChristoph Hellwig 	return error;
876cb8e7090SChristoph Hellwig }
877cb8e7090SChristoph Hellwig 
8784cb5300bSChris Mason /*
8794cb5300bSChris Mason  * When we're defragging a range, we don't want to kick it off again
8804cb5300bSChris Mason  * if it is really just waiting for delalloc to send it down.
8814cb5300bSChris Mason  * If we find a nice big extent or delalloc range for the bytes in the
8824cb5300bSChris Mason  * file you want to defrag, we return 0 to let you know to skip this
8834cb5300bSChris Mason  * part of the file
8844cb5300bSChris Mason  */
8854cb5300bSChris Mason static int check_defrag_in_cache(struct inode *inode, u64 offset, int thresh)
8864cb5300bSChris Mason {
8874cb5300bSChris Mason 	struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
8884cb5300bSChris Mason 	struct extent_map *em = NULL;
8894cb5300bSChris Mason 	struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
8904cb5300bSChris Mason 	u64 end;
8914cb5300bSChris Mason 
8924cb5300bSChris Mason 	read_lock(&em_tree->lock);
8934cb5300bSChris Mason 	em = lookup_extent_mapping(em_tree, offset, PAGE_CACHE_SIZE);
8944cb5300bSChris Mason 	read_unlock(&em_tree->lock);
8954cb5300bSChris Mason 
8964cb5300bSChris Mason 	if (em) {
8974cb5300bSChris Mason 		end = extent_map_end(em);
8984cb5300bSChris Mason 		free_extent_map(em);
8994cb5300bSChris Mason 		if (end - offset > thresh)
9004cb5300bSChris Mason 			return 0;
9014cb5300bSChris Mason 	}
9024cb5300bSChris Mason 	/* if we already have a nice delalloc here, just stop */
9034cb5300bSChris Mason 	thresh /= 2;
9044cb5300bSChris Mason 	end = count_range_bits(io_tree, &offset, offset + thresh,
9054cb5300bSChris Mason 			       thresh, EXTENT_DELALLOC, 1);
9064cb5300bSChris Mason 	if (end >= thresh)
9074cb5300bSChris Mason 		return 0;
9084cb5300bSChris Mason 	return 1;
9094cb5300bSChris Mason }
9104cb5300bSChris Mason 
9114cb5300bSChris Mason /*
9124cb5300bSChris Mason  * helper function to walk through a file and find extents
9134cb5300bSChris Mason  * newer than a specific transid, and smaller than thresh.
9144cb5300bSChris Mason  *
9154cb5300bSChris Mason  * This is used by the defragging code to find new and small
9164cb5300bSChris Mason  * extents
9174cb5300bSChris Mason  */
9184cb5300bSChris Mason static int find_new_extents(struct btrfs_root *root,
9194cb5300bSChris Mason 			    struct inode *inode, u64 newer_than,
9204cb5300bSChris Mason 			    u64 *off, int thresh)
9214cb5300bSChris Mason {
9224cb5300bSChris Mason 	struct btrfs_path *path;
9234cb5300bSChris Mason 	struct btrfs_key min_key;
9244cb5300bSChris Mason 	struct extent_buffer *leaf;
9254cb5300bSChris Mason 	struct btrfs_file_extent_item *extent;
9264cb5300bSChris Mason 	int type;
9274cb5300bSChris Mason 	int ret;
928a4689d2bSDavid Sterba 	u64 ino = btrfs_ino(inode);
9294cb5300bSChris Mason 
9304cb5300bSChris Mason 	path = btrfs_alloc_path();
9314cb5300bSChris Mason 	if (!path)
9324cb5300bSChris Mason 		return -ENOMEM;
9334cb5300bSChris Mason 
934a4689d2bSDavid Sterba 	min_key.objectid = ino;
9354cb5300bSChris Mason 	min_key.type = BTRFS_EXTENT_DATA_KEY;
9364cb5300bSChris Mason 	min_key.offset = *off;
9374cb5300bSChris Mason 
9384cb5300bSChris Mason 	while (1) {
939f094c9bdSFilipe Manana 		path->keep_locks = 1;
9406174d3cbSFilipe David Borba Manana 		ret = btrfs_search_forward(root, &min_key, path, newer_than);
9414cb5300bSChris Mason 		if (ret != 0)
9424cb5300bSChris Mason 			goto none;
943f094c9bdSFilipe Manana 		path->keep_locks = 0;
944f094c9bdSFilipe Manana 		btrfs_unlock_up_safe(path, 1);
945f094c9bdSFilipe Manana process_slot:
946a4689d2bSDavid Sterba 		if (min_key.objectid != ino)
9474cb5300bSChris Mason 			goto none;
9484cb5300bSChris Mason 		if (min_key.type != BTRFS_EXTENT_DATA_KEY)
9494cb5300bSChris Mason 			goto none;
9504cb5300bSChris Mason 
9514cb5300bSChris Mason 		leaf = path->nodes[0];
9524cb5300bSChris Mason 		extent = btrfs_item_ptr(leaf, path->slots[0],
9534cb5300bSChris Mason 					struct btrfs_file_extent_item);
9544cb5300bSChris Mason 
9554cb5300bSChris Mason 		type = btrfs_file_extent_type(leaf, extent);
9564cb5300bSChris Mason 		if (type == BTRFS_FILE_EXTENT_REG &&
9574cb5300bSChris Mason 		    btrfs_file_extent_num_bytes(leaf, extent) < thresh &&
9584cb5300bSChris Mason 		    check_defrag_in_cache(inode, min_key.offset, thresh)) {
9594cb5300bSChris Mason 			*off = min_key.offset;
9604cb5300bSChris Mason 			btrfs_free_path(path);
9614cb5300bSChris Mason 			return 0;
9624cb5300bSChris Mason 		}
9634cb5300bSChris Mason 
964f094c9bdSFilipe Manana 		path->slots[0]++;
965f094c9bdSFilipe Manana 		if (path->slots[0] < btrfs_header_nritems(leaf)) {
966f094c9bdSFilipe Manana 			btrfs_item_key_to_cpu(leaf, &min_key, path->slots[0]);
967f094c9bdSFilipe Manana 			goto process_slot;
968f094c9bdSFilipe Manana 		}
969f094c9bdSFilipe Manana 
9704cb5300bSChris Mason 		if (min_key.offset == (u64)-1)
9714cb5300bSChris Mason 			goto none;
9724cb5300bSChris Mason 
9734cb5300bSChris Mason 		min_key.offset++;
9744cb5300bSChris Mason 		btrfs_release_path(path);
9754cb5300bSChris Mason 	}
9764cb5300bSChris Mason none:
9774cb5300bSChris Mason 	btrfs_free_path(path);
9784cb5300bSChris Mason 	return -ENOENT;
9794cb5300bSChris Mason }
9804cb5300bSChris Mason 
9816c282eb4SLi Zefan static struct extent_map *defrag_lookup_extent(struct inode *inode, u64 start)
98217ce6ef8SLiu Bo {
98317ce6ef8SLiu Bo 	struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
984940100a4SChris Mason 	struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
9856c282eb4SLi Zefan 	struct extent_map *em;
9866c282eb4SLi Zefan 	u64 len = PAGE_CACHE_SIZE;
987940100a4SChris Mason 
988940100a4SChris Mason 	/*
989940100a4SChris Mason 	 * hopefully we have this extent in the tree already, try without
990940100a4SChris Mason 	 * the full extent lock
991940100a4SChris Mason 	 */
992940100a4SChris Mason 	read_lock(&em_tree->lock);
993940100a4SChris Mason 	em = lookup_extent_mapping(em_tree, start, len);
994940100a4SChris Mason 	read_unlock(&em_tree->lock);
995940100a4SChris Mason 
996940100a4SChris Mason 	if (!em) {
997308d9800SFilipe Manana 		struct extent_state *cached = NULL;
998308d9800SFilipe Manana 		u64 end = start + len - 1;
999308d9800SFilipe Manana 
1000940100a4SChris Mason 		/* get the big lock and read metadata off disk */
1001308d9800SFilipe Manana 		lock_extent_bits(io_tree, start, end, 0, &cached);
1002940100a4SChris Mason 		em = btrfs_get_extent(inode, NULL, 0, start, len, 0);
1003308d9800SFilipe Manana 		unlock_extent_cached(io_tree, start, end, &cached, GFP_NOFS);
1004940100a4SChris Mason 
10056cf8bfbfSDan Carpenter 		if (IS_ERR(em))
10066c282eb4SLi Zefan 			return NULL;
1007940100a4SChris Mason 	}
1008940100a4SChris Mason 
10096c282eb4SLi Zefan 	return em;
10106c282eb4SLi Zefan }
10116c282eb4SLi Zefan 
10126c282eb4SLi Zefan static bool defrag_check_next_extent(struct inode *inode, struct extent_map *em)
10136c282eb4SLi Zefan {
10146c282eb4SLi Zefan 	struct extent_map *next;
10156c282eb4SLi Zefan 	bool ret = true;
10166c282eb4SLi Zefan 
10176c282eb4SLi Zefan 	/* this is the last extent */
10186c282eb4SLi Zefan 	if (em->start + em->len >= i_size_read(inode))
10196c282eb4SLi Zefan 		return false;
10206c282eb4SLi Zefan 
10216c282eb4SLi Zefan 	next = defrag_lookup_extent(inode, em->start + em->len);
1022e2127cf0SFilipe Manana 	if (!next || next->block_start >= EXTENT_MAP_LAST_BYTE ||
1023e2127cf0SFilipe Manana 	    (em->block_start + em->block_len == next->block_start))
10246c282eb4SLi Zefan 		ret = false;
10256c282eb4SLi Zefan 
10266c282eb4SLi Zefan 	free_extent_map(next);
10276c282eb4SLi Zefan 	return ret;
10286c282eb4SLi Zefan }
10296c282eb4SLi Zefan 
10306c282eb4SLi Zefan static int should_defrag_range(struct inode *inode, u64 start, int thresh,
1031a43a2111SAndrew Mahone 			       u64 *last_len, u64 *skip, u64 *defrag_end,
1032a43a2111SAndrew Mahone 			       int compress)
10336c282eb4SLi Zefan {
10346c282eb4SLi Zefan 	struct extent_map *em;
10356c282eb4SLi Zefan 	int ret = 1;
10366c282eb4SLi Zefan 	bool next_mergeable = true;
10376c282eb4SLi Zefan 
10386c282eb4SLi Zefan 	/*
10396c282eb4SLi Zefan 	 * make sure that once we start defragging an extent, we keep on
10406c282eb4SLi Zefan 	 * defragging it
10416c282eb4SLi Zefan 	 */
10426c282eb4SLi Zefan 	if (start < *defrag_end)
10436c282eb4SLi Zefan 		return 1;
10446c282eb4SLi Zefan 
10456c282eb4SLi Zefan 	*skip = 0;
10466c282eb4SLi Zefan 
10476c282eb4SLi Zefan 	em = defrag_lookup_extent(inode, start);
10486c282eb4SLi Zefan 	if (!em)
10496c282eb4SLi Zefan 		return 0;
10506c282eb4SLi Zefan 
1051940100a4SChris Mason 	/* this will cover holes, and inline extents */
105217ce6ef8SLiu Bo 	if (em->block_start >= EXTENT_MAP_LAST_BYTE) {
1053940100a4SChris Mason 		ret = 0;
105417ce6ef8SLiu Bo 		goto out;
105517ce6ef8SLiu Bo 	}
105617ce6ef8SLiu Bo 
10576c282eb4SLi Zefan 	next_mergeable = defrag_check_next_extent(inode, em);
1058940100a4SChris Mason 
1059940100a4SChris Mason 	/*
10606c282eb4SLi Zefan 	 * we hit a real extent, if it is big or the next extent is not a
10616c282eb4SLi Zefan 	 * real extent, don't bother defragging it
1062940100a4SChris Mason 	 */
1063a43a2111SAndrew Mahone 	if (!compress && (*last_len == 0 || *last_len >= thresh) &&
10646c282eb4SLi Zefan 	    (em->len >= thresh || !next_mergeable))
1065940100a4SChris Mason 		ret = 0;
106617ce6ef8SLiu Bo out:
1067940100a4SChris Mason 	/*
1068940100a4SChris Mason 	 * last_len ends up being a counter of how many bytes we've defragged.
1069940100a4SChris Mason 	 * every time we choose not to defrag an extent, we reset *last_len
1070940100a4SChris Mason 	 * so that the next tiny extent will force a defrag.
1071940100a4SChris Mason 	 *
1072940100a4SChris Mason 	 * The end result of this is that tiny extents before a single big
1073940100a4SChris Mason 	 * extent will force at least part of that big extent to be defragged.
1074940100a4SChris Mason 	 */
1075940100a4SChris Mason 	if (ret) {
1076940100a4SChris Mason 		*defrag_end = extent_map_end(em);
1077940100a4SChris Mason 	} else {
1078940100a4SChris Mason 		*last_len = 0;
1079940100a4SChris Mason 		*skip = extent_map_end(em);
1080940100a4SChris Mason 		*defrag_end = 0;
1081940100a4SChris Mason 	}
1082940100a4SChris Mason 
1083940100a4SChris Mason 	free_extent_map(em);
1084940100a4SChris Mason 	return ret;
1085940100a4SChris Mason }
1086940100a4SChris Mason 
10874cb5300bSChris Mason /*
10884cb5300bSChris Mason  * it doesn't do much good to defrag one or two pages
10894cb5300bSChris Mason  * at a time.  This pulls in a nice chunk of pages
10904cb5300bSChris Mason  * to COW and defrag.
10914cb5300bSChris Mason  *
10924cb5300bSChris Mason  * It also makes sure the delalloc code has enough
10934cb5300bSChris Mason  * dirty data to avoid making new small extents as part
10944cb5300bSChris Mason  * of the defrag
10954cb5300bSChris Mason  *
10964cb5300bSChris Mason  * It's a good idea to start RA on this range
10974cb5300bSChris Mason  * before calling this.
10984cb5300bSChris Mason  */
10994cb5300bSChris Mason static int cluster_pages_for_defrag(struct inode *inode,
11004cb5300bSChris Mason 				    struct page **pages,
11014cb5300bSChris Mason 				    unsigned long start_index,
1102c41570c9SJustin Maggard 				    unsigned long num_pages)
1103f46b5a66SChristoph Hellwig {
11044cb5300bSChris Mason 	unsigned long file_end;
11054cb5300bSChris Mason 	u64 isize = i_size_read(inode);
1106f46b5a66SChristoph Hellwig 	u64 page_start;
1107f46b5a66SChristoph Hellwig 	u64 page_end;
11081f12bd06SLiu Bo 	u64 page_cnt;
11094cb5300bSChris Mason 	int ret;
11104cb5300bSChris Mason 	int i;
11114cb5300bSChris Mason 	int i_done;
11124cb5300bSChris Mason 	struct btrfs_ordered_extent *ordered;
11134cb5300bSChris Mason 	struct extent_state *cached_state = NULL;
1114600a45e1SMiao Xie 	struct extent_io_tree *tree;
11153b16a4e3SJosef Bacik 	gfp_t mask = btrfs_alloc_write_mask(inode->i_mapping);
11164cb5300bSChris Mason 
11174cb5300bSChris Mason 	file_end = (isize - 1) >> PAGE_CACHE_SHIFT;
11181f12bd06SLiu Bo 	if (!isize || start_index > file_end)
11191f12bd06SLiu Bo 		return 0;
11201f12bd06SLiu Bo 
11211f12bd06SLiu Bo 	page_cnt = min_t(u64, (u64)num_pages, (u64)file_end - start_index + 1);
11224cb5300bSChris Mason 
11234cb5300bSChris Mason 	ret = btrfs_delalloc_reserve_space(inode,
11241f12bd06SLiu Bo 					   page_cnt << PAGE_CACHE_SHIFT);
11254cb5300bSChris Mason 	if (ret)
11264cb5300bSChris Mason 		return ret;
11274cb5300bSChris Mason 	i_done = 0;
1128600a45e1SMiao Xie 	tree = &BTRFS_I(inode)->io_tree;
11294cb5300bSChris Mason 
11304cb5300bSChris Mason 	/* step one, lock all the pages */
11311f12bd06SLiu Bo 	for (i = 0; i < page_cnt; i++) {
11324cb5300bSChris Mason 		struct page *page;
1133600a45e1SMiao Xie again:
1134a94733d0SJosef Bacik 		page = find_or_create_page(inode->i_mapping,
11353b16a4e3SJosef Bacik 					   start_index + i, mask);
11364cb5300bSChris Mason 		if (!page)
11374cb5300bSChris Mason 			break;
11384cb5300bSChris Mason 
1139600a45e1SMiao Xie 		page_start = page_offset(page);
1140600a45e1SMiao Xie 		page_end = page_start + PAGE_CACHE_SIZE - 1;
1141600a45e1SMiao Xie 		while (1) {
1142308d9800SFilipe Manana 			lock_extent_bits(tree, page_start, page_end,
1143308d9800SFilipe Manana 					 0, &cached_state);
1144600a45e1SMiao Xie 			ordered = btrfs_lookup_ordered_extent(inode,
1145600a45e1SMiao Xie 							      page_start);
1146308d9800SFilipe Manana 			unlock_extent_cached(tree, page_start, page_end,
1147308d9800SFilipe Manana 					     &cached_state, GFP_NOFS);
1148600a45e1SMiao Xie 			if (!ordered)
1149600a45e1SMiao Xie 				break;
1150600a45e1SMiao Xie 
1151600a45e1SMiao Xie 			unlock_page(page);
1152600a45e1SMiao Xie 			btrfs_start_ordered_extent(inode, ordered, 1);
1153600a45e1SMiao Xie 			btrfs_put_ordered_extent(ordered);
1154600a45e1SMiao Xie 			lock_page(page);
11551f12bd06SLiu Bo 			/*
11561f12bd06SLiu Bo 			 * we unlocked the page above, so we need check if
11571f12bd06SLiu Bo 			 * it was released or not.
11581f12bd06SLiu Bo 			 */
11591f12bd06SLiu Bo 			if (page->mapping != inode->i_mapping) {
11601f12bd06SLiu Bo 				unlock_page(page);
11611f12bd06SLiu Bo 				page_cache_release(page);
11621f12bd06SLiu Bo 				goto again;
11631f12bd06SLiu Bo 			}
1164600a45e1SMiao Xie 		}
1165600a45e1SMiao Xie 
11664cb5300bSChris Mason 		if (!PageUptodate(page)) {
11674cb5300bSChris Mason 			btrfs_readpage(NULL, page);
11684cb5300bSChris Mason 			lock_page(page);
11694cb5300bSChris Mason 			if (!PageUptodate(page)) {
11704cb5300bSChris Mason 				unlock_page(page);
11714cb5300bSChris Mason 				page_cache_release(page);
11724cb5300bSChris Mason 				ret = -EIO;
11734cb5300bSChris Mason 				break;
11744cb5300bSChris Mason 			}
11754cb5300bSChris Mason 		}
1176600a45e1SMiao Xie 
1177600a45e1SMiao Xie 		if (page->mapping != inode->i_mapping) {
1178600a45e1SMiao Xie 			unlock_page(page);
1179600a45e1SMiao Xie 			page_cache_release(page);
1180600a45e1SMiao Xie 			goto again;
1181600a45e1SMiao Xie 		}
1182600a45e1SMiao Xie 
11834cb5300bSChris Mason 		pages[i] = page;
11844cb5300bSChris Mason 		i_done++;
11854cb5300bSChris Mason 	}
11864cb5300bSChris Mason 	if (!i_done || ret)
11874cb5300bSChris Mason 		goto out;
11884cb5300bSChris Mason 
11894cb5300bSChris Mason 	if (!(inode->i_sb->s_flags & MS_ACTIVE))
11904cb5300bSChris Mason 		goto out;
11914cb5300bSChris Mason 
11924cb5300bSChris Mason 	/*
11934cb5300bSChris Mason 	 * so now we have a nice long stream of locked
11944cb5300bSChris Mason 	 * and up to date pages, lets wait on them
11954cb5300bSChris Mason 	 */
11964cb5300bSChris Mason 	for (i = 0; i < i_done; i++)
11974cb5300bSChris Mason 		wait_on_page_writeback(pages[i]);
11984cb5300bSChris Mason 
11994cb5300bSChris Mason 	page_start = page_offset(pages[0]);
12004cb5300bSChris Mason 	page_end = page_offset(pages[i_done - 1]) + PAGE_CACHE_SIZE;
12014cb5300bSChris Mason 
12024cb5300bSChris Mason 	lock_extent_bits(&BTRFS_I(inode)->io_tree,
1203d0082371SJeff Mahoney 			 page_start, page_end - 1, 0, &cached_state);
12044cb5300bSChris Mason 	clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start,
12054cb5300bSChris Mason 			  page_end - 1, EXTENT_DIRTY | EXTENT_DELALLOC |
12069e8a4a8bSLiu Bo 			  EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG, 0, 0,
12079e8a4a8bSLiu Bo 			  &cached_state, GFP_NOFS);
12084cb5300bSChris Mason 
12091f12bd06SLiu Bo 	if (i_done != page_cnt) {
12109e0baf60SJosef Bacik 		spin_lock(&BTRFS_I(inode)->lock);
12119e0baf60SJosef Bacik 		BTRFS_I(inode)->outstanding_extents++;
12129e0baf60SJosef Bacik 		spin_unlock(&BTRFS_I(inode)->lock);
12134cb5300bSChris Mason 		btrfs_delalloc_release_space(inode,
12141f12bd06SLiu Bo 				     (page_cnt - i_done) << PAGE_CACHE_SHIFT);
12154cb5300bSChris Mason 	}
12164cb5300bSChris Mason 
12174cb5300bSChris Mason 
12189e8a4a8bSLiu Bo 	set_extent_defrag(&BTRFS_I(inode)->io_tree, page_start, page_end - 1,
12199e8a4a8bSLiu Bo 			  &cached_state, GFP_NOFS);
12204cb5300bSChris Mason 
12214cb5300bSChris Mason 	unlock_extent_cached(&BTRFS_I(inode)->io_tree,
12224cb5300bSChris Mason 			     page_start, page_end - 1, &cached_state,
12234cb5300bSChris Mason 			     GFP_NOFS);
12244cb5300bSChris Mason 
12254cb5300bSChris Mason 	for (i = 0; i < i_done; i++) {
12264cb5300bSChris Mason 		clear_page_dirty_for_io(pages[i]);
12274cb5300bSChris Mason 		ClearPageChecked(pages[i]);
12284cb5300bSChris Mason 		set_page_extent_mapped(pages[i]);
12294cb5300bSChris Mason 		set_page_dirty(pages[i]);
12304cb5300bSChris Mason 		unlock_page(pages[i]);
12314cb5300bSChris Mason 		page_cache_release(pages[i]);
12324cb5300bSChris Mason 	}
12334cb5300bSChris Mason 	return i_done;
12344cb5300bSChris Mason out:
12354cb5300bSChris Mason 	for (i = 0; i < i_done; i++) {
12364cb5300bSChris Mason 		unlock_page(pages[i]);
12374cb5300bSChris Mason 		page_cache_release(pages[i]);
12384cb5300bSChris Mason 	}
12391f12bd06SLiu Bo 	btrfs_delalloc_release_space(inode, page_cnt << PAGE_CACHE_SHIFT);
12404cb5300bSChris Mason 	return ret;
12414cb5300bSChris Mason 
12424cb5300bSChris Mason }
12434cb5300bSChris Mason 
12444cb5300bSChris Mason int btrfs_defrag_file(struct inode *inode, struct file *file,
12454cb5300bSChris Mason 		      struct btrfs_ioctl_defrag_range_args *range,
12464cb5300bSChris Mason 		      u64 newer_than, unsigned long max_to_defrag)
12474cb5300bSChris Mason {
12484cb5300bSChris Mason 	struct btrfs_root *root = BTRFS_I(inode)->root;
12494cb5300bSChris Mason 	struct file_ra_state *ra = NULL;
12504cb5300bSChris Mason 	unsigned long last_index;
1251151a31b2SLi Zefan 	u64 isize = i_size_read(inode);
1252940100a4SChris Mason 	u64 last_len = 0;
1253940100a4SChris Mason 	u64 skip = 0;
1254940100a4SChris Mason 	u64 defrag_end = 0;
12554cb5300bSChris Mason 	u64 newer_off = range->start;
1256f46b5a66SChristoph Hellwig 	unsigned long i;
1257008873eaSLi Zefan 	unsigned long ra_index = 0;
1258f46b5a66SChristoph Hellwig 	int ret;
12594cb5300bSChris Mason 	int defrag_count = 0;
12601a419d85SLi Zefan 	int compress_type = BTRFS_COMPRESS_ZLIB;
12614cb5300bSChris Mason 	int extent_thresh = range->extent_thresh;
1262c41570c9SJustin Maggard 	unsigned long max_cluster = (256 * 1024) >> PAGE_CACHE_SHIFT;
1263c41570c9SJustin Maggard 	unsigned long cluster = max_cluster;
12644cb5300bSChris Mason 	u64 new_align = ~((u64)128 * 1024 - 1);
12654cb5300bSChris Mason 	struct page **pages = NULL;
12664cb5300bSChris Mason 
12670abd5b17SLiu Bo 	if (isize == 0)
12680abd5b17SLiu Bo 		return 0;
12690abd5b17SLiu Bo 
12700abd5b17SLiu Bo 	if (range->start >= isize)
12710abd5b17SLiu Bo 		return -EINVAL;
12721a419d85SLi Zefan 
12731a419d85SLi Zefan 	if (range->flags & BTRFS_DEFRAG_RANGE_COMPRESS) {
12741a419d85SLi Zefan 		if (range->compress_type > BTRFS_COMPRESS_TYPES)
12751a419d85SLi Zefan 			return -EINVAL;
12761a419d85SLi Zefan 		if (range->compress_type)
12771a419d85SLi Zefan 			compress_type = range->compress_type;
12781a419d85SLi Zefan 	}
1279f46b5a66SChristoph Hellwig 
12800abd5b17SLiu Bo 	if (extent_thresh == 0)
12810abd5b17SLiu Bo 		extent_thresh = 256 * 1024;
1282f46b5a66SChristoph Hellwig 
12834cb5300bSChris Mason 	/*
12844cb5300bSChris Mason 	 * if we were not given a file, allocate a readahead
12854cb5300bSChris Mason 	 * context
12864cb5300bSChris Mason 	 */
12874cb5300bSChris Mason 	if (!file) {
12884cb5300bSChris Mason 		ra = kzalloc(sizeof(*ra), GFP_NOFS);
12894cb5300bSChris Mason 		if (!ra)
12904cb5300bSChris Mason 			return -ENOMEM;
12914cb5300bSChris Mason 		file_ra_state_init(ra, inode->i_mapping);
12924cb5300bSChris Mason 	} else {
12934cb5300bSChris Mason 		ra = &file->f_ra;
12944cb5300bSChris Mason 	}
12954cb5300bSChris Mason 
1296d9b0d9baSDulshani Gunawardhana 	pages = kmalloc_array(max_cluster, sizeof(struct page *),
12974cb5300bSChris Mason 			GFP_NOFS);
12984cb5300bSChris Mason 	if (!pages) {
12994cb5300bSChris Mason 		ret = -ENOMEM;
13004cb5300bSChris Mason 		goto out_ra;
13014cb5300bSChris Mason 	}
13024cb5300bSChris Mason 
13034cb5300bSChris Mason 	/* find the last page to defrag */
13041e701a32SChris Mason 	if (range->start + range->len > range->start) {
1305151a31b2SLi Zefan 		last_index = min_t(u64, isize - 1,
13061e701a32SChris Mason 			 range->start + range->len - 1) >> PAGE_CACHE_SHIFT;
13071e701a32SChris Mason 	} else {
1308151a31b2SLi Zefan 		last_index = (isize - 1) >> PAGE_CACHE_SHIFT;
13091e701a32SChris Mason 	}
13101e701a32SChris Mason 
13114cb5300bSChris Mason 	if (newer_than) {
13124cb5300bSChris Mason 		ret = find_new_extents(root, inode, newer_than,
13134cb5300bSChris Mason 				       &newer_off, 64 * 1024);
13144cb5300bSChris Mason 		if (!ret) {
13154cb5300bSChris Mason 			range->start = newer_off;
13164cb5300bSChris Mason 			/*
13174cb5300bSChris Mason 			 * we always align our defrag to help keep
13184cb5300bSChris Mason 			 * the extents in the file evenly spaced
13194cb5300bSChris Mason 			 */
13204cb5300bSChris Mason 			i = (newer_off & new_align) >> PAGE_CACHE_SHIFT;
13214cb5300bSChris Mason 		} else
13224cb5300bSChris Mason 			goto out_ra;
13234cb5300bSChris Mason 	} else {
13241e701a32SChris Mason 		i = range->start >> PAGE_CACHE_SHIFT;
13254cb5300bSChris Mason 	}
13264cb5300bSChris Mason 	if (!max_to_defrag)
13277ec31b54SLiu Bo 		max_to_defrag = last_index + 1;
13284cb5300bSChris Mason 
13292a0f7f57SLi Zefan 	/*
13302a0f7f57SLi Zefan 	 * make writeback starts from i, so the defrag range can be
13312a0f7f57SLi Zefan 	 * written sequentially.
13322a0f7f57SLi Zefan 	 */
13332a0f7f57SLi Zefan 	if (i < inode->i_mapping->writeback_index)
13342a0f7f57SLi Zefan 		inode->i_mapping->writeback_index = i;
13352a0f7f57SLi Zefan 
1336f7f43cc8SChris Mason 	while (i <= last_index && defrag_count < max_to_defrag &&
1337f7f43cc8SChris Mason 	       (i < (i_size_read(inode) + PAGE_CACHE_SIZE - 1) >>
1338f7f43cc8SChris Mason 		PAGE_CACHE_SHIFT)) {
13394cb5300bSChris Mason 		/*
13404cb5300bSChris Mason 		 * make sure we stop running if someone unmounts
13414cb5300bSChris Mason 		 * the FS
13424cb5300bSChris Mason 		 */
13434cb5300bSChris Mason 		if (!(inode->i_sb->s_flags & MS_ACTIVE))
13444cb5300bSChris Mason 			break;
13454cb5300bSChris Mason 
1346210549ebSDavid Sterba 		if (btrfs_defrag_cancelled(root->fs_info)) {
1347efe120a0SFrank Holton 			printk(KERN_DEBUG "BTRFS: defrag_file cancelled\n");
1348210549ebSDavid Sterba 			ret = -EAGAIN;
1349210549ebSDavid Sterba 			break;
1350210549ebSDavid Sterba 		}
1351210549ebSDavid Sterba 
135266c26892SLiu Bo 		if (!should_defrag_range(inode, (u64)i << PAGE_CACHE_SHIFT,
13536c282eb4SLi Zefan 					 extent_thresh, &last_len, &skip,
1354a43a2111SAndrew Mahone 					 &defrag_end, range->flags &
1355a43a2111SAndrew Mahone 					 BTRFS_DEFRAG_RANGE_COMPRESS)) {
1356940100a4SChris Mason 			unsigned long next;
1357940100a4SChris Mason 			/*
1358940100a4SChris Mason 			 * the should_defrag function tells us how much to skip
1359940100a4SChris Mason 			 * bump our counter by the suggested amount
1360940100a4SChris Mason 			 */
1361940100a4SChris Mason 			next = (skip + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
1362940100a4SChris Mason 			i = max(i + 1, next);
1363940100a4SChris Mason 			continue;
1364940100a4SChris Mason 		}
1365008873eaSLi Zefan 
1366008873eaSLi Zefan 		if (!newer_than) {
1367008873eaSLi Zefan 			cluster = (PAGE_CACHE_ALIGN(defrag_end) >>
1368008873eaSLi Zefan 				   PAGE_CACHE_SHIFT) - i;
1369008873eaSLi Zefan 			cluster = min(cluster, max_cluster);
1370008873eaSLi Zefan 		} else {
1371008873eaSLi Zefan 			cluster = max_cluster;
1372008873eaSLi Zefan 		}
1373008873eaSLi Zefan 
1374008873eaSLi Zefan 		if (i + cluster > ra_index) {
1375008873eaSLi Zefan 			ra_index = max(i, ra_index);
1376008873eaSLi Zefan 			btrfs_force_ra(inode->i_mapping, ra, file, ra_index,
1377008873eaSLi Zefan 				       cluster);
1378008873eaSLi Zefan 			ra_index += max_cluster;
1379008873eaSLi Zefan 		}
13804cb5300bSChris Mason 
1381ecb8bea8SLiu Bo 		mutex_lock(&inode->i_mutex);
1382633085c7SFilipe David Borba Manana 		if (range->flags & BTRFS_DEFRAG_RANGE_COMPRESS)
1383633085c7SFilipe David Borba Manana 			BTRFS_I(inode)->force_compress = compress_type;
1384008873eaSLi Zefan 		ret = cluster_pages_for_defrag(inode, pages, i, cluster);
1385ecb8bea8SLiu Bo 		if (ret < 0) {
1386ecb8bea8SLiu Bo 			mutex_unlock(&inode->i_mutex);
13874cb5300bSChris Mason 			goto out_ra;
1388ecb8bea8SLiu Bo 		}
13894cb5300bSChris Mason 
13904cb5300bSChris Mason 		defrag_count += ret;
1391d0e1d66bSNamjae Jeon 		balance_dirty_pages_ratelimited(inode->i_mapping);
1392ecb8bea8SLiu Bo 		mutex_unlock(&inode->i_mutex);
13934cb5300bSChris Mason 
13944cb5300bSChris Mason 		if (newer_than) {
13954cb5300bSChris Mason 			if (newer_off == (u64)-1)
13964cb5300bSChris Mason 				break;
13974cb5300bSChris Mason 
1398e1f041e1SLiu Bo 			if (ret > 0)
1399e1f041e1SLiu Bo 				i += ret;
1400e1f041e1SLiu Bo 
14014cb5300bSChris Mason 			newer_off = max(newer_off + 1,
14024cb5300bSChris Mason 					(u64)i << PAGE_CACHE_SHIFT);
14034cb5300bSChris Mason 
14044cb5300bSChris Mason 			ret = find_new_extents(root, inode,
14054cb5300bSChris Mason 					       newer_than, &newer_off,
14064cb5300bSChris Mason 					       64 * 1024);
14074cb5300bSChris Mason 			if (!ret) {
14084cb5300bSChris Mason 				range->start = newer_off;
14094cb5300bSChris Mason 				i = (newer_off & new_align) >> PAGE_CACHE_SHIFT;
14104cb5300bSChris Mason 			} else {
14114cb5300bSChris Mason 				break;
1412940100a4SChris Mason 			}
14134cb5300bSChris Mason 		} else {
1414008873eaSLi Zefan 			if (ret > 0) {
1415cbcc8326SLi Zefan 				i += ret;
1416008873eaSLi Zefan 				last_len += ret << PAGE_CACHE_SHIFT;
1417008873eaSLi Zefan 			} else {
1418940100a4SChris Mason 				i++;
1419008873eaSLi Zefan 				last_len = 0;
1420008873eaSLi Zefan 			}
1421f46b5a66SChristoph Hellwig 		}
14224cb5300bSChris Mason 	}
1423f46b5a66SChristoph Hellwig 
1424dec8ef90SFilipe Manana 	if ((range->flags & BTRFS_DEFRAG_RANGE_START_IO)) {
14251e701a32SChris Mason 		filemap_flush(inode->i_mapping);
1426dec8ef90SFilipe Manana 		if (test_bit(BTRFS_INODE_HAS_ASYNC_EXTENT,
1427dec8ef90SFilipe Manana 			     &BTRFS_I(inode)->runtime_flags))
1428dec8ef90SFilipe Manana 			filemap_flush(inode->i_mapping);
1429dec8ef90SFilipe Manana 	}
14301e701a32SChris Mason 
14311e701a32SChris Mason 	if ((range->flags & BTRFS_DEFRAG_RANGE_COMPRESS)) {
14321e701a32SChris Mason 		/* the filemap_flush will queue IO into the worker threads, but
14331e701a32SChris Mason 		 * we have to make sure the IO is actually started and that
14341e701a32SChris Mason 		 * ordered extents get created before we return
14351e701a32SChris Mason 		 */
14361e701a32SChris Mason 		atomic_inc(&root->fs_info->async_submit_draining);
14371e701a32SChris Mason 		while (atomic_read(&root->fs_info->nr_async_submits) ||
14381e701a32SChris Mason 		      atomic_read(&root->fs_info->async_delalloc_pages)) {
14391e701a32SChris Mason 			wait_event(root->fs_info->async_submit_wait,
14401e701a32SChris Mason 			   (atomic_read(&root->fs_info->nr_async_submits) == 0 &&
14411e701a32SChris Mason 			    atomic_read(&root->fs_info->async_delalloc_pages) == 0));
14421e701a32SChris Mason 		}
14431e701a32SChris Mason 		atomic_dec(&root->fs_info->async_submit_draining);
14441e701a32SChris Mason 	}
14451e701a32SChris Mason 
14461a419d85SLi Zefan 	if (range->compress_type == BTRFS_COMPRESS_LZO) {
14472b0ce2c2SMitch Harder 		btrfs_set_fs_incompat(root->fs_info, COMPRESS_LZO);
14481a419d85SLi Zefan 	}
14491a419d85SLi Zefan 
145060ccf82fSDiego Calleja 	ret = defrag_count;
1451940100a4SChris Mason 
14524cb5300bSChris Mason out_ra:
1453633085c7SFilipe David Borba Manana 	if (range->flags & BTRFS_DEFRAG_RANGE_COMPRESS) {
1454633085c7SFilipe David Borba Manana 		mutex_lock(&inode->i_mutex);
1455633085c7SFilipe David Borba Manana 		BTRFS_I(inode)->force_compress = BTRFS_COMPRESS_NONE;
1456633085c7SFilipe David Borba Manana 		mutex_unlock(&inode->i_mutex);
1457633085c7SFilipe David Borba Manana 	}
14584cb5300bSChris Mason 	if (!file)
14594cb5300bSChris Mason 		kfree(ra);
14604cb5300bSChris Mason 	kfree(pages);
1461940100a4SChris Mason 	return ret;
1462f46b5a66SChristoph Hellwig }
1463f46b5a66SChristoph Hellwig 
1464198605a8SMiao Xie static noinline int btrfs_ioctl_resize(struct file *file,
146576dda93cSYan, Zheng 					void __user *arg)
1466f46b5a66SChristoph Hellwig {
1467f46b5a66SChristoph Hellwig 	u64 new_size;
1468f46b5a66SChristoph Hellwig 	u64 old_size;
1469f46b5a66SChristoph Hellwig 	u64 devid = 1;
1470496ad9aaSAl Viro 	struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
1471f46b5a66SChristoph Hellwig 	struct btrfs_ioctl_vol_args *vol_args;
1472f46b5a66SChristoph Hellwig 	struct btrfs_trans_handle *trans;
1473f46b5a66SChristoph Hellwig 	struct btrfs_device *device = NULL;
1474f46b5a66SChristoph Hellwig 	char *sizestr;
14759a40f122SGui Hecheng 	char *retptr;
1476f46b5a66SChristoph Hellwig 	char *devstr = NULL;
1477f46b5a66SChristoph Hellwig 	int ret = 0;
1478f46b5a66SChristoph Hellwig 	int mod = 0;
1479f46b5a66SChristoph Hellwig 
1480e441d54dSChris Mason 	if (!capable(CAP_SYS_ADMIN))
1481e441d54dSChris Mason 		return -EPERM;
1482e441d54dSChris Mason 
1483198605a8SMiao Xie 	ret = mnt_want_write_file(file);
1484198605a8SMiao Xie 	if (ret)
1485198605a8SMiao Xie 		return ret;
1486198605a8SMiao Xie 
14875ac00addSStefan Behrens 	if (atomic_xchg(&root->fs_info->mutually_exclusive_operation_running,
14885ac00addSStefan Behrens 			1)) {
148997547676SMiao Xie 		mnt_drop_write_file(file);
1490e57138b3SAnand Jain 		return BTRFS_ERROR_DEV_EXCL_RUN_IN_PROGRESS;
1491c9e9f97bSIlya Dryomov 	}
1492c9e9f97bSIlya Dryomov 
14935ac00addSStefan Behrens 	mutex_lock(&root->fs_info->volume_mutex);
1494dae7b665SLi Zefan 	vol_args = memdup_user(arg, sizeof(*vol_args));
1495c9e9f97bSIlya Dryomov 	if (IS_ERR(vol_args)) {
1496c9e9f97bSIlya Dryomov 		ret = PTR_ERR(vol_args);
1497c9e9f97bSIlya Dryomov 		goto out;
1498c9e9f97bSIlya Dryomov 	}
14995516e595SMark Fasheh 
15005516e595SMark Fasheh 	vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
1501f46b5a66SChristoph Hellwig 
1502f46b5a66SChristoph Hellwig 	sizestr = vol_args->name;
1503f46b5a66SChristoph Hellwig 	devstr = strchr(sizestr, ':');
1504f46b5a66SChristoph Hellwig 	if (devstr) {
1505f46b5a66SChristoph Hellwig 		char *end;
1506f46b5a66SChristoph Hellwig 		sizestr = devstr + 1;
1507f46b5a66SChristoph Hellwig 		*devstr = '\0';
1508f46b5a66SChristoph Hellwig 		devstr = vol_args->name;
1509f46b5a66SChristoph Hellwig 		devid = simple_strtoull(devstr, &end, 10);
1510dfd79829SMiao Xie 		if (!devid) {
1511dfd79829SMiao Xie 			ret = -EINVAL;
1512dfd79829SMiao Xie 			goto out_free;
1513dfd79829SMiao Xie 		}
1514efe120a0SFrank Holton 		btrfs_info(root->fs_info, "resizing devid %llu", devid);
1515f46b5a66SChristoph Hellwig 	}
1516dba60f3fSMiao Xie 
1517aa1b8cd4SStefan Behrens 	device = btrfs_find_device(root->fs_info, devid, NULL, NULL);
1518f46b5a66SChristoph Hellwig 	if (!device) {
1519efe120a0SFrank Holton 		btrfs_info(root->fs_info, "resizer unable to find device %llu",
1520c1c9ff7cSGeert Uytterhoeven 		       devid);
1521dfd79829SMiao Xie 		ret = -ENODEV;
1522c9e9f97bSIlya Dryomov 		goto out_free;
1523f46b5a66SChristoph Hellwig 	}
1524dba60f3fSMiao Xie 
1525dba60f3fSMiao Xie 	if (!device->writeable) {
1526efe120a0SFrank Holton 		btrfs_info(root->fs_info,
1527efe120a0SFrank Holton 			   "resizer unable to apply on readonly device %llu",
1528c1c9ff7cSGeert Uytterhoeven 		       devid);
1529dfd79829SMiao Xie 		ret = -EPERM;
15304e42ae1bSLiu Bo 		goto out_free;
15314e42ae1bSLiu Bo 	}
15324e42ae1bSLiu Bo 
1533f46b5a66SChristoph Hellwig 	if (!strcmp(sizestr, "max"))
1534f46b5a66SChristoph Hellwig 		new_size = device->bdev->bd_inode->i_size;
1535f46b5a66SChristoph Hellwig 	else {
1536f46b5a66SChristoph Hellwig 		if (sizestr[0] == '-') {
1537f46b5a66SChristoph Hellwig 			mod = -1;
1538f46b5a66SChristoph Hellwig 			sizestr++;
1539f46b5a66SChristoph Hellwig 		} else if (sizestr[0] == '+') {
1540f46b5a66SChristoph Hellwig 			mod = 1;
1541f46b5a66SChristoph Hellwig 			sizestr++;
1542f46b5a66SChristoph Hellwig 		}
15439a40f122SGui Hecheng 		new_size = memparse(sizestr, &retptr);
15449a40f122SGui Hecheng 		if (*retptr != '\0' || new_size == 0) {
1545f46b5a66SChristoph Hellwig 			ret = -EINVAL;
1546c9e9f97bSIlya Dryomov 			goto out_free;
1547f46b5a66SChristoph Hellwig 		}
1548f46b5a66SChristoph Hellwig 	}
1549f46b5a66SChristoph Hellwig 
155063a212abSStefan Behrens 	if (device->is_tgtdev_for_dev_replace) {
1551dfd79829SMiao Xie 		ret = -EPERM;
155263a212abSStefan Behrens 		goto out_free;
155363a212abSStefan Behrens 	}
155463a212abSStefan Behrens 
1555f46b5a66SChristoph Hellwig 	old_size = device->total_bytes;
1556f46b5a66SChristoph Hellwig 
1557f46b5a66SChristoph Hellwig 	if (mod < 0) {
1558f46b5a66SChristoph Hellwig 		if (new_size > old_size) {
1559f46b5a66SChristoph Hellwig 			ret = -EINVAL;
1560c9e9f97bSIlya Dryomov 			goto out_free;
1561f46b5a66SChristoph Hellwig 		}
1562f46b5a66SChristoph Hellwig 		new_size = old_size - new_size;
1563f46b5a66SChristoph Hellwig 	} else if (mod > 0) {
1564eb8052e0SWenliang Fan 		if (new_size > ULLONG_MAX - old_size) {
1565eb8052e0SWenliang Fan 			ret = -EINVAL;
1566eb8052e0SWenliang Fan 			goto out_free;
1567eb8052e0SWenliang Fan 		}
1568f46b5a66SChristoph Hellwig 		new_size = old_size + new_size;
1569f46b5a66SChristoph Hellwig 	}
1570f46b5a66SChristoph Hellwig 
1571f46b5a66SChristoph Hellwig 	if (new_size < 256 * 1024 * 1024) {
1572f46b5a66SChristoph Hellwig 		ret = -EINVAL;
1573c9e9f97bSIlya Dryomov 		goto out_free;
1574f46b5a66SChristoph Hellwig 	}
1575f46b5a66SChristoph Hellwig 	if (new_size > device->bdev->bd_inode->i_size) {
1576f46b5a66SChristoph Hellwig 		ret = -EFBIG;
1577c9e9f97bSIlya Dryomov 		goto out_free;
1578f46b5a66SChristoph Hellwig 	}
1579f46b5a66SChristoph Hellwig 
1580f46b5a66SChristoph Hellwig 	do_div(new_size, root->sectorsize);
1581f46b5a66SChristoph Hellwig 	new_size *= root->sectorsize;
1582f46b5a66SChristoph Hellwig 
1583efe120a0SFrank Holton 	printk_in_rcu(KERN_INFO "BTRFS: new size for %s is %llu\n",
1584c1c9ff7cSGeert Uytterhoeven 		      rcu_str_deref(device->name), new_size);
1585f46b5a66SChristoph Hellwig 
1586f46b5a66SChristoph Hellwig 	if (new_size > old_size) {
1587a22285a6SYan, Zheng 		trans = btrfs_start_transaction(root, 0);
158898d5dc13STsutomu Itoh 		if (IS_ERR(trans)) {
158998d5dc13STsutomu Itoh 			ret = PTR_ERR(trans);
1590c9e9f97bSIlya Dryomov 			goto out_free;
159198d5dc13STsutomu Itoh 		}
1592f46b5a66SChristoph Hellwig 		ret = btrfs_grow_device(trans, device, new_size);
1593f46b5a66SChristoph Hellwig 		btrfs_commit_transaction(trans, root);
1594ece7d20eSMike Fleetwood 	} else if (new_size < old_size) {
1595f46b5a66SChristoph Hellwig 		ret = btrfs_shrink_device(device, new_size);
15960253f40eSjeff.liu 	} /* equal, nothing need to do */
1597f46b5a66SChristoph Hellwig 
1598c9e9f97bSIlya Dryomov out_free:
1599f46b5a66SChristoph Hellwig 	kfree(vol_args);
1600c9e9f97bSIlya Dryomov out:
1601c9e9f97bSIlya Dryomov 	mutex_unlock(&root->fs_info->volume_mutex);
16025ac00addSStefan Behrens 	atomic_set(&root->fs_info->mutually_exclusive_operation_running, 0);
160318f39c41SIlya Dryomov 	mnt_drop_write_file(file);
1604f46b5a66SChristoph Hellwig 	return ret;
1605f46b5a66SChristoph Hellwig }
1606f46b5a66SChristoph Hellwig 
160772fd032eSSage Weil static noinline int btrfs_ioctl_snap_create_transid(struct file *file,
16086f72c7e2SArne Jansen 				char *name, unsigned long fd, int subvol,
16096f72c7e2SArne Jansen 				u64 *transid, bool readonly,
16108696c533SMiao Xie 				struct btrfs_qgroup_inherit *inherit)
1611f46b5a66SChristoph Hellwig {
1612f46b5a66SChristoph Hellwig 	int namelen;
16133de4586cSChris Mason 	int ret = 0;
1614f46b5a66SChristoph Hellwig 
1615a874a63eSLiu Bo 	ret = mnt_want_write_file(file);
1616a874a63eSLiu Bo 	if (ret)
1617a874a63eSLiu Bo 		goto out;
1618a874a63eSLiu Bo 
161972fd032eSSage Weil 	namelen = strlen(name);
162072fd032eSSage Weil 	if (strchr(name, '/')) {
1621f46b5a66SChristoph Hellwig 		ret = -EINVAL;
1622a874a63eSLiu Bo 		goto out_drop_write;
1623f46b5a66SChristoph Hellwig 	}
1624f46b5a66SChristoph Hellwig 
162516780cabSChris Mason 	if (name[0] == '.' &&
162616780cabSChris Mason 	   (namelen == 1 || (name[1] == '.' && namelen == 2))) {
162716780cabSChris Mason 		ret = -EEXIST;
1628a874a63eSLiu Bo 		goto out_drop_write;
162916780cabSChris Mason 	}
163016780cabSChris Mason 
16313de4586cSChris Mason 	if (subvol) {
163272fd032eSSage Weil 		ret = btrfs_mksubvol(&file->f_path, name, namelen,
16336f72c7e2SArne Jansen 				     NULL, transid, readonly, inherit);
1634cb8e7090SChristoph Hellwig 	} else {
16352903ff01SAl Viro 		struct fd src = fdget(fd);
16363de4586cSChris Mason 		struct inode *src_inode;
16372903ff01SAl Viro 		if (!src.file) {
16383de4586cSChris Mason 			ret = -EINVAL;
1639a874a63eSLiu Bo 			goto out_drop_write;
16403de4586cSChris Mason 		}
16413de4586cSChris Mason 
1642496ad9aaSAl Viro 		src_inode = file_inode(src.file);
1643496ad9aaSAl Viro 		if (src_inode->i_sb != file_inode(file)->i_sb) {
1644efe120a0SFrank Holton 			btrfs_info(BTRFS_I(src_inode)->root->fs_info,
1645efe120a0SFrank Holton 				   "Snapshot src from another FS");
164623ad5b17SKusanagi Kouichi 			ret = -EXDEV;
1647d0242061SDavid Sterba 		} else if (!inode_owner_or_capable(src_inode)) {
1648d0242061SDavid Sterba 			/*
1649d0242061SDavid Sterba 			 * Subvolume creation is not restricted, but snapshots
1650d0242061SDavid Sterba 			 * are limited to own subvolumes only
1651d0242061SDavid Sterba 			 */
1652d0242061SDavid Sterba 			ret = -EPERM;
1653ecd18815SAl Viro 		} else {
165472fd032eSSage Weil 			ret = btrfs_mksubvol(&file->f_path, name, namelen,
165572fd032eSSage Weil 					     BTRFS_I(src_inode)->root,
16566f72c7e2SArne Jansen 					     transid, readonly, inherit);
1657ecd18815SAl Viro 		}
16582903ff01SAl Viro 		fdput(src);
1659cb8e7090SChristoph Hellwig 	}
1660a874a63eSLiu Bo out_drop_write:
1661a874a63eSLiu Bo 	mnt_drop_write_file(file);
1662f46b5a66SChristoph Hellwig out:
166372fd032eSSage Weil 	return ret;
166472fd032eSSage Weil }
166572fd032eSSage Weil 
166672fd032eSSage Weil static noinline int btrfs_ioctl_snap_create(struct file *file,
1667fa0d2b9bSLi Zefan 					    void __user *arg, int subvol)
166872fd032eSSage Weil {
1669fa0d2b9bSLi Zefan 	struct btrfs_ioctl_vol_args *vol_args;
167072fd032eSSage Weil 	int ret;
167172fd032eSSage Weil 
1672fa0d2b9bSLi Zefan 	vol_args = memdup_user(arg, sizeof(*vol_args));
1673fa0d2b9bSLi Zefan 	if (IS_ERR(vol_args))
1674fa0d2b9bSLi Zefan 		return PTR_ERR(vol_args);
1675fa0d2b9bSLi Zefan 	vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
1676fa0d2b9bSLi Zefan 
1677fa0d2b9bSLi Zefan 	ret = btrfs_ioctl_snap_create_transid(file, vol_args->name,
1678b83cc969SLi Zefan 					      vol_args->fd, subvol,
16796f72c7e2SArne Jansen 					      NULL, false, NULL);
1680fa0d2b9bSLi Zefan 
1681fa0d2b9bSLi Zefan 	kfree(vol_args);
1682fa0d2b9bSLi Zefan 	return ret;
1683fa0d2b9bSLi Zefan }
1684fa0d2b9bSLi Zefan 
1685fa0d2b9bSLi Zefan static noinline int btrfs_ioctl_snap_create_v2(struct file *file,
1686fa0d2b9bSLi Zefan 					       void __user *arg, int subvol)
1687fa0d2b9bSLi Zefan {
1688fa0d2b9bSLi Zefan 	struct btrfs_ioctl_vol_args_v2 *vol_args;
1689fa0d2b9bSLi Zefan 	int ret;
1690fdfb1e4fSLi Zefan 	u64 transid = 0;
1691fdfb1e4fSLi Zefan 	u64 *ptr = NULL;
1692b83cc969SLi Zefan 	bool readonly = false;
16936f72c7e2SArne Jansen 	struct btrfs_qgroup_inherit *inherit = NULL;
169472fd032eSSage Weil 
1695fa0d2b9bSLi Zefan 	vol_args = memdup_user(arg, sizeof(*vol_args));
1696fa0d2b9bSLi Zefan 	if (IS_ERR(vol_args))
1697fa0d2b9bSLi Zefan 		return PTR_ERR(vol_args);
1698fa0d2b9bSLi Zefan 	vol_args->name[BTRFS_SUBVOL_NAME_MAX] = '\0';
1699fdfb1e4fSLi Zefan 
1700b83cc969SLi Zefan 	if (vol_args->flags &
17016f72c7e2SArne Jansen 	    ~(BTRFS_SUBVOL_CREATE_ASYNC | BTRFS_SUBVOL_RDONLY |
17026f72c7e2SArne Jansen 	      BTRFS_SUBVOL_QGROUP_INHERIT)) {
1703b83cc969SLi Zefan 		ret = -EOPNOTSUPP;
1704fdfb1e4fSLi Zefan 		goto out;
1705fdfb1e4fSLi Zefan 	}
1706fdfb1e4fSLi Zefan 
1707fa0d2b9bSLi Zefan 	if (vol_args->flags & BTRFS_SUBVOL_CREATE_ASYNC)
1708fdfb1e4fSLi Zefan 		ptr = &transid;
1709b83cc969SLi Zefan 	if (vol_args->flags & BTRFS_SUBVOL_RDONLY)
1710b83cc969SLi Zefan 		readonly = true;
17116f72c7e2SArne Jansen 	if (vol_args->flags & BTRFS_SUBVOL_QGROUP_INHERIT) {
17126f72c7e2SArne Jansen 		if (vol_args->size > PAGE_CACHE_SIZE) {
17136f72c7e2SArne Jansen 			ret = -EINVAL;
17146f72c7e2SArne Jansen 			goto out;
17156f72c7e2SArne Jansen 		}
17166f72c7e2SArne Jansen 		inherit = memdup_user(vol_args->qgroup_inherit, vol_args->size);
17176f72c7e2SArne Jansen 		if (IS_ERR(inherit)) {
17186f72c7e2SArne Jansen 			ret = PTR_ERR(inherit);
17196f72c7e2SArne Jansen 			goto out;
17206f72c7e2SArne Jansen 		}
17216f72c7e2SArne Jansen 	}
172275eaa0e2SSage Weil 
1723fa0d2b9bSLi Zefan 	ret = btrfs_ioctl_snap_create_transid(file, vol_args->name,
17246f72c7e2SArne Jansen 					      vol_args->fd, subvol, ptr,
17258696c533SMiao Xie 					      readonly, inherit);
172675eaa0e2SSage Weil 
1727fdfb1e4fSLi Zefan 	if (ret == 0 && ptr &&
172875eaa0e2SSage Weil 	    copy_to_user(arg +
1729fdfb1e4fSLi Zefan 			 offsetof(struct btrfs_ioctl_vol_args_v2,
1730fdfb1e4fSLi Zefan 				  transid), ptr, sizeof(*ptr)))
173175eaa0e2SSage Weil 		ret = -EFAULT;
1732fdfb1e4fSLi Zefan out:
1733f46b5a66SChristoph Hellwig 	kfree(vol_args);
17346f72c7e2SArne Jansen 	kfree(inherit);
1735f46b5a66SChristoph Hellwig 	return ret;
1736f46b5a66SChristoph Hellwig }
1737f46b5a66SChristoph Hellwig 
17380caa102dSLi Zefan static noinline int btrfs_ioctl_subvol_getflags(struct file *file,
17390caa102dSLi Zefan 						void __user *arg)
17400caa102dSLi Zefan {
1741496ad9aaSAl Viro 	struct inode *inode = file_inode(file);
17420caa102dSLi Zefan 	struct btrfs_root *root = BTRFS_I(inode)->root;
17430caa102dSLi Zefan 	int ret = 0;
17440caa102dSLi Zefan 	u64 flags = 0;
17450caa102dSLi Zefan 
174633345d01SLi Zefan 	if (btrfs_ino(inode) != BTRFS_FIRST_FREE_OBJECTID)
17470caa102dSLi Zefan 		return -EINVAL;
17480caa102dSLi Zefan 
17490caa102dSLi Zefan 	down_read(&root->fs_info->subvol_sem);
17500caa102dSLi Zefan 	if (btrfs_root_readonly(root))
17510caa102dSLi Zefan 		flags |= BTRFS_SUBVOL_RDONLY;
17520caa102dSLi Zefan 	up_read(&root->fs_info->subvol_sem);
17530caa102dSLi Zefan 
17540caa102dSLi Zefan 	if (copy_to_user(arg, &flags, sizeof(flags)))
17550caa102dSLi Zefan 		ret = -EFAULT;
17560caa102dSLi Zefan 
17570caa102dSLi Zefan 	return ret;
17580caa102dSLi Zefan }
17590caa102dSLi Zefan 
17600caa102dSLi Zefan static noinline int btrfs_ioctl_subvol_setflags(struct file *file,
17610caa102dSLi Zefan 					      void __user *arg)
17620caa102dSLi Zefan {
1763496ad9aaSAl Viro 	struct inode *inode = file_inode(file);
17640caa102dSLi Zefan 	struct btrfs_root *root = BTRFS_I(inode)->root;
17650caa102dSLi Zefan 	struct btrfs_trans_handle *trans;
17660caa102dSLi Zefan 	u64 root_flags;
17670caa102dSLi Zefan 	u64 flags;
17680caa102dSLi Zefan 	int ret = 0;
17690caa102dSLi Zefan 
1770bd60ea0fSDavid Sterba 	if (!inode_owner_or_capable(inode))
1771bd60ea0fSDavid Sterba 		return -EPERM;
1772bd60ea0fSDavid Sterba 
1773b9ca0664SLiu Bo 	ret = mnt_want_write_file(file);
1774b9ca0664SLiu Bo 	if (ret)
1775b9ca0664SLiu Bo 		goto out;
17760caa102dSLi Zefan 
1777b9ca0664SLiu Bo 	if (btrfs_ino(inode) != BTRFS_FIRST_FREE_OBJECTID) {
1778b9ca0664SLiu Bo 		ret = -EINVAL;
1779b9ca0664SLiu Bo 		goto out_drop_write;
1780b9ca0664SLiu Bo 	}
17810caa102dSLi Zefan 
1782b9ca0664SLiu Bo 	if (copy_from_user(&flags, arg, sizeof(flags))) {
1783b9ca0664SLiu Bo 		ret = -EFAULT;
1784b9ca0664SLiu Bo 		goto out_drop_write;
1785b9ca0664SLiu Bo 	}
17860caa102dSLi Zefan 
1787b9ca0664SLiu Bo 	if (flags & BTRFS_SUBVOL_CREATE_ASYNC) {
1788b9ca0664SLiu Bo 		ret = -EINVAL;
1789b9ca0664SLiu Bo 		goto out_drop_write;
1790b9ca0664SLiu Bo 	}
17910caa102dSLi Zefan 
1792b9ca0664SLiu Bo 	if (flags & ~BTRFS_SUBVOL_RDONLY) {
1793b9ca0664SLiu Bo 		ret = -EOPNOTSUPP;
1794b9ca0664SLiu Bo 		goto out_drop_write;
1795b9ca0664SLiu Bo 	}
17960caa102dSLi Zefan 
17970caa102dSLi Zefan 	down_write(&root->fs_info->subvol_sem);
17980caa102dSLi Zefan 
17990caa102dSLi Zefan 	/* nothing to do */
18000caa102dSLi Zefan 	if (!!(flags & BTRFS_SUBVOL_RDONLY) == btrfs_root_readonly(root))
1801b9ca0664SLiu Bo 		goto out_drop_sem;
18020caa102dSLi Zefan 
18030caa102dSLi Zefan 	root_flags = btrfs_root_flags(&root->root_item);
18042c686537SDavid Sterba 	if (flags & BTRFS_SUBVOL_RDONLY) {
18050caa102dSLi Zefan 		btrfs_set_root_flags(&root->root_item,
18060caa102dSLi Zefan 				     root_flags | BTRFS_ROOT_SUBVOL_RDONLY);
18072c686537SDavid Sterba 	} else {
18082c686537SDavid Sterba 		/*
18092c686537SDavid Sterba 		 * Block RO -> RW transition if this subvolume is involved in
18102c686537SDavid Sterba 		 * send
18112c686537SDavid Sterba 		 */
18122c686537SDavid Sterba 		spin_lock(&root->root_item_lock);
18132c686537SDavid Sterba 		if (root->send_in_progress == 0) {
18140caa102dSLi Zefan 			btrfs_set_root_flags(&root->root_item,
18150caa102dSLi Zefan 				     root_flags & ~BTRFS_ROOT_SUBVOL_RDONLY);
18162c686537SDavid Sterba 			spin_unlock(&root->root_item_lock);
18172c686537SDavid Sterba 		} else {
18182c686537SDavid Sterba 			spin_unlock(&root->root_item_lock);
18192c686537SDavid Sterba 			btrfs_warn(root->fs_info,
18202c686537SDavid Sterba 			"Attempt to set subvolume %llu read-write during send",
18212c686537SDavid Sterba 					root->root_key.objectid);
18222c686537SDavid Sterba 			ret = -EPERM;
18232c686537SDavid Sterba 			goto out_drop_sem;
18242c686537SDavid Sterba 		}
18252c686537SDavid Sterba 	}
18260caa102dSLi Zefan 
18270caa102dSLi Zefan 	trans = btrfs_start_transaction(root, 1);
18280caa102dSLi Zefan 	if (IS_ERR(trans)) {
18290caa102dSLi Zefan 		ret = PTR_ERR(trans);
18300caa102dSLi Zefan 		goto out_reset;
18310caa102dSLi Zefan 	}
18320caa102dSLi Zefan 
1833b4dc2b8cSLi Zefan 	ret = btrfs_update_root(trans, root->fs_info->tree_root,
18340caa102dSLi Zefan 				&root->root_key, &root->root_item);
18350caa102dSLi Zefan 
18360caa102dSLi Zefan 	btrfs_commit_transaction(trans, root);
18370caa102dSLi Zefan out_reset:
18380caa102dSLi Zefan 	if (ret)
18390caa102dSLi Zefan 		btrfs_set_root_flags(&root->root_item, root_flags);
1840b9ca0664SLiu Bo out_drop_sem:
18410caa102dSLi Zefan 	up_write(&root->fs_info->subvol_sem);
1842b9ca0664SLiu Bo out_drop_write:
1843b9ca0664SLiu Bo 	mnt_drop_write_file(file);
1844b9ca0664SLiu Bo out:
18450caa102dSLi Zefan 	return ret;
18460caa102dSLi Zefan }
18470caa102dSLi Zefan 
184876dda93cSYan, Zheng /*
184976dda93cSYan, Zheng  * helper to check if the subvolume references other subvolumes
185076dda93cSYan, Zheng  */
185176dda93cSYan, Zheng static noinline int may_destroy_subvol(struct btrfs_root *root)
185276dda93cSYan, Zheng {
185376dda93cSYan, Zheng 	struct btrfs_path *path;
1854175a2b87SJosef Bacik 	struct btrfs_dir_item *di;
185576dda93cSYan, Zheng 	struct btrfs_key key;
1856175a2b87SJosef Bacik 	u64 dir_id;
185776dda93cSYan, Zheng 	int ret;
185876dda93cSYan, Zheng 
185976dda93cSYan, Zheng 	path = btrfs_alloc_path();
186076dda93cSYan, Zheng 	if (!path)
186176dda93cSYan, Zheng 		return -ENOMEM;
186276dda93cSYan, Zheng 
1863175a2b87SJosef Bacik 	/* Make sure this root isn't set as the default subvol */
1864175a2b87SJosef Bacik 	dir_id = btrfs_super_root_dir(root->fs_info->super_copy);
1865175a2b87SJosef Bacik 	di = btrfs_lookup_dir_item(NULL, root->fs_info->tree_root, path,
1866175a2b87SJosef Bacik 				   dir_id, "default", 7, 0);
1867175a2b87SJosef Bacik 	if (di && !IS_ERR(di)) {
1868175a2b87SJosef Bacik 		btrfs_dir_item_key_to_cpu(path->nodes[0], di, &key);
1869175a2b87SJosef Bacik 		if (key.objectid == root->root_key.objectid) {
187072de6b53SGuangyu Sun 			ret = -EPERM;
187172de6b53SGuangyu Sun 			btrfs_err(root->fs_info, "deleting default subvolume "
187272de6b53SGuangyu Sun 				  "%llu is not allowed", key.objectid);
1873175a2b87SJosef Bacik 			goto out;
1874175a2b87SJosef Bacik 		}
1875175a2b87SJosef Bacik 		btrfs_release_path(path);
1876175a2b87SJosef Bacik 	}
1877175a2b87SJosef Bacik 
187876dda93cSYan, Zheng 	key.objectid = root->root_key.objectid;
187976dda93cSYan, Zheng 	key.type = BTRFS_ROOT_REF_KEY;
188076dda93cSYan, Zheng 	key.offset = (u64)-1;
188176dda93cSYan, Zheng 
188276dda93cSYan, Zheng 	ret = btrfs_search_slot(NULL, root->fs_info->tree_root,
188376dda93cSYan, Zheng 				&key, path, 0, 0);
188476dda93cSYan, Zheng 	if (ret < 0)
188576dda93cSYan, Zheng 		goto out;
188676dda93cSYan, Zheng 	BUG_ON(ret == 0);
188776dda93cSYan, Zheng 
188876dda93cSYan, Zheng 	ret = 0;
188976dda93cSYan, Zheng 	if (path->slots[0] > 0) {
189076dda93cSYan, Zheng 		path->slots[0]--;
189176dda93cSYan, Zheng 		btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
189276dda93cSYan, Zheng 		if (key.objectid == root->root_key.objectid &&
189376dda93cSYan, Zheng 		    key.type == BTRFS_ROOT_REF_KEY)
189476dda93cSYan, Zheng 			ret = -ENOTEMPTY;
189576dda93cSYan, Zheng 	}
189676dda93cSYan, Zheng out:
189776dda93cSYan, Zheng 	btrfs_free_path(path);
189876dda93cSYan, Zheng 	return ret;
189976dda93cSYan, Zheng }
190076dda93cSYan, Zheng 
1901ac8e9819SChris Mason static noinline int key_in_sk(struct btrfs_key *key,
1902ac8e9819SChris Mason 			      struct btrfs_ioctl_search_key *sk)
1903ac8e9819SChris Mason {
1904abc6e134SChris Mason 	struct btrfs_key test;
1905abc6e134SChris Mason 	int ret;
1906abc6e134SChris Mason 
1907abc6e134SChris Mason 	test.objectid = sk->min_objectid;
1908abc6e134SChris Mason 	test.type = sk->min_type;
1909abc6e134SChris Mason 	test.offset = sk->min_offset;
1910abc6e134SChris Mason 
1911abc6e134SChris Mason 	ret = btrfs_comp_cpu_keys(key, &test);
1912abc6e134SChris Mason 	if (ret < 0)
1913ac8e9819SChris Mason 		return 0;
1914abc6e134SChris Mason 
1915abc6e134SChris Mason 	test.objectid = sk->max_objectid;
1916abc6e134SChris Mason 	test.type = sk->max_type;
1917abc6e134SChris Mason 	test.offset = sk->max_offset;
1918abc6e134SChris Mason 
1919abc6e134SChris Mason 	ret = btrfs_comp_cpu_keys(key, &test);
1920abc6e134SChris Mason 	if (ret > 0)
1921ac8e9819SChris Mason 		return 0;
1922ac8e9819SChris Mason 	return 1;
1923ac8e9819SChris Mason }
1924ac8e9819SChris Mason 
1925ac8e9819SChris Mason static noinline int copy_to_sk(struct btrfs_root *root,
1926ac8e9819SChris Mason 			       struct btrfs_path *path,
1927ac8e9819SChris Mason 			       struct btrfs_key *key,
1928ac8e9819SChris Mason 			       struct btrfs_ioctl_search_key *sk,
1929ac8e9819SChris Mason 			       char *buf,
1930ac8e9819SChris Mason 			       unsigned long *sk_offset,
1931ac8e9819SChris Mason 			       int *num_found)
1932ac8e9819SChris Mason {
1933ac8e9819SChris Mason 	u64 found_transid;
1934ac8e9819SChris Mason 	struct extent_buffer *leaf;
1935ac8e9819SChris Mason 	struct btrfs_ioctl_search_header sh;
1936ac8e9819SChris Mason 	unsigned long item_off;
1937ac8e9819SChris Mason 	unsigned long item_len;
1938ac8e9819SChris Mason 	int nritems;
1939ac8e9819SChris Mason 	int i;
1940ac8e9819SChris Mason 	int slot;
1941ac8e9819SChris Mason 	int ret = 0;
1942ac8e9819SChris Mason 
1943ac8e9819SChris Mason 	leaf = path->nodes[0];
1944ac8e9819SChris Mason 	slot = path->slots[0];
1945ac8e9819SChris Mason 	nritems = btrfs_header_nritems(leaf);
1946ac8e9819SChris Mason 
1947ac8e9819SChris Mason 	if (btrfs_header_generation(leaf) > sk->max_transid) {
1948ac8e9819SChris Mason 		i = nritems;
1949ac8e9819SChris Mason 		goto advance_key;
1950ac8e9819SChris Mason 	}
1951ac8e9819SChris Mason 	found_transid = btrfs_header_generation(leaf);
1952ac8e9819SChris Mason 
1953ac8e9819SChris Mason 	for (i = slot; i < nritems; i++) {
1954ac8e9819SChris Mason 		item_off = btrfs_item_ptr_offset(leaf, i);
1955ac8e9819SChris Mason 		item_len = btrfs_item_size_nr(leaf, i);
1956ac8e9819SChris Mason 
195703b71c6cSGabriel de Perthuis 		btrfs_item_key_to_cpu(leaf, key, i);
195803b71c6cSGabriel de Perthuis 		if (!key_in_sk(key, sk))
195903b71c6cSGabriel de Perthuis 			continue;
196003b71c6cSGabriel de Perthuis 
196103b71c6cSGabriel de Perthuis 		if (sizeof(sh) + item_len > BTRFS_SEARCH_ARGS_BUFSIZE)
1962ac8e9819SChris Mason 			item_len = 0;
1963ac8e9819SChris Mason 
1964ac8e9819SChris Mason 		if (sizeof(sh) + item_len + *sk_offset >
1965ac8e9819SChris Mason 		    BTRFS_SEARCH_ARGS_BUFSIZE) {
1966ac8e9819SChris Mason 			ret = 1;
1967ac8e9819SChris Mason 			goto overflow;
1968ac8e9819SChris Mason 		}
1969ac8e9819SChris Mason 
1970ac8e9819SChris Mason 		sh.objectid = key->objectid;
1971ac8e9819SChris Mason 		sh.offset = key->offset;
1972ac8e9819SChris Mason 		sh.type = key->type;
1973ac8e9819SChris Mason 		sh.len = item_len;
1974ac8e9819SChris Mason 		sh.transid = found_transid;
1975ac8e9819SChris Mason 
1976ac8e9819SChris Mason 		/* copy search result header */
1977ac8e9819SChris Mason 		memcpy(buf + *sk_offset, &sh, sizeof(sh));
1978ac8e9819SChris Mason 		*sk_offset += sizeof(sh);
1979ac8e9819SChris Mason 
1980ac8e9819SChris Mason 		if (item_len) {
1981ac8e9819SChris Mason 			char *p = buf + *sk_offset;
1982ac8e9819SChris Mason 			/* copy the item */
1983ac8e9819SChris Mason 			read_extent_buffer(leaf, p,
1984ac8e9819SChris Mason 					   item_off, item_len);
1985ac8e9819SChris Mason 			*sk_offset += item_len;
1986ac8e9819SChris Mason 		}
1987e2156867SHugo Mills 		(*num_found)++;
1988ac8e9819SChris Mason 
1989ac8e9819SChris Mason 		if (*num_found >= sk->nr_items)
1990ac8e9819SChris Mason 			break;
1991ac8e9819SChris Mason 	}
1992ac8e9819SChris Mason advance_key:
1993ac8e9819SChris Mason 	ret = 0;
1994abc6e134SChris Mason 	if (key->offset < (u64)-1 && key->offset < sk->max_offset)
1995abc6e134SChris Mason 		key->offset++;
1996abc6e134SChris Mason 	else if (key->type < (u8)-1 && key->type < sk->max_type) {
1997abc6e134SChris Mason 		key->offset = 0;
1998abc6e134SChris Mason 		key->type++;
1999abc6e134SChris Mason 	} else if (key->objectid < (u64)-1 && key->objectid < sk->max_objectid) {
2000abc6e134SChris Mason 		key->offset = 0;
2001abc6e134SChris Mason 		key->type = 0;
2002abc6e134SChris Mason 		key->objectid++;
2003abc6e134SChris Mason 	} else
2004abc6e134SChris Mason 		ret = 1;
2005ac8e9819SChris Mason overflow:
2006ac8e9819SChris Mason 	return ret;
2007ac8e9819SChris Mason }
2008ac8e9819SChris Mason 
2009ac8e9819SChris Mason static noinline int search_ioctl(struct inode *inode,
2010ac8e9819SChris Mason 				 struct btrfs_ioctl_search_args *args)
2011ac8e9819SChris Mason {
2012ac8e9819SChris Mason 	struct btrfs_root *root;
2013ac8e9819SChris Mason 	struct btrfs_key key;
2014ac8e9819SChris Mason 	struct btrfs_path *path;
2015ac8e9819SChris Mason 	struct btrfs_ioctl_search_key *sk = &args->key;
2016ac8e9819SChris Mason 	struct btrfs_fs_info *info = BTRFS_I(inode)->root->fs_info;
2017ac8e9819SChris Mason 	int ret;
2018ac8e9819SChris Mason 	int num_found = 0;
2019ac8e9819SChris Mason 	unsigned long sk_offset = 0;
2020ac8e9819SChris Mason 
2021ac8e9819SChris Mason 	path = btrfs_alloc_path();
2022ac8e9819SChris Mason 	if (!path)
2023ac8e9819SChris Mason 		return -ENOMEM;
2024ac8e9819SChris Mason 
2025ac8e9819SChris Mason 	if (sk->tree_id == 0) {
2026ac8e9819SChris Mason 		/* search the root of the inode that was passed */
2027ac8e9819SChris Mason 		root = BTRFS_I(inode)->root;
2028ac8e9819SChris Mason 	} else {
2029ac8e9819SChris Mason 		key.objectid = sk->tree_id;
2030ac8e9819SChris Mason 		key.type = BTRFS_ROOT_ITEM_KEY;
2031ac8e9819SChris Mason 		key.offset = (u64)-1;
2032ac8e9819SChris Mason 		root = btrfs_read_fs_root_no_name(info, &key);
2033ac8e9819SChris Mason 		if (IS_ERR(root)) {
2034efe120a0SFrank Holton 			printk(KERN_ERR "BTRFS: could not find root %llu\n",
2035ac8e9819SChris Mason 			       sk->tree_id);
2036ac8e9819SChris Mason 			btrfs_free_path(path);
2037ac8e9819SChris Mason 			return -ENOENT;
2038ac8e9819SChris Mason 		}
2039ac8e9819SChris Mason 	}
2040ac8e9819SChris Mason 
2041ac8e9819SChris Mason 	key.objectid = sk->min_objectid;
2042ac8e9819SChris Mason 	key.type = sk->min_type;
2043ac8e9819SChris Mason 	key.offset = sk->min_offset;
2044ac8e9819SChris Mason 
2045ac8e9819SChris Mason 	path->keep_locks = 1;
2046ac8e9819SChris Mason 
2047ac8e9819SChris Mason 	while (1) {
20486174d3cbSFilipe David Borba Manana 		ret = btrfs_search_forward(root, &key, path, sk->min_transid);
2049ac8e9819SChris Mason 		if (ret != 0) {
2050ac8e9819SChris Mason 			if (ret > 0)
2051ac8e9819SChris Mason 				ret = 0;
2052ac8e9819SChris Mason 			goto err;
2053ac8e9819SChris Mason 		}
2054ac8e9819SChris Mason 		ret = copy_to_sk(root, path, &key, sk, args->buf,
2055ac8e9819SChris Mason 				 &sk_offset, &num_found);
2056b3b4aa74SDavid Sterba 		btrfs_release_path(path);
2057ac8e9819SChris Mason 		if (ret || num_found >= sk->nr_items)
2058ac8e9819SChris Mason 			break;
2059ac8e9819SChris Mason 
2060ac8e9819SChris Mason 	}
2061ac8e9819SChris Mason 	ret = 0;
2062ac8e9819SChris Mason err:
2063ac8e9819SChris Mason 	sk->nr_items = num_found;
2064ac8e9819SChris Mason 	btrfs_free_path(path);
2065ac8e9819SChris Mason 	return ret;
2066ac8e9819SChris Mason }
2067ac8e9819SChris Mason 
2068ac8e9819SChris Mason static noinline int btrfs_ioctl_tree_search(struct file *file,
2069ac8e9819SChris Mason 					   void __user *argp)
2070ac8e9819SChris Mason {
2071ac8e9819SChris Mason 	 struct btrfs_ioctl_search_args *args;
2072ac8e9819SChris Mason 	 struct inode *inode;
2073ac8e9819SChris Mason 	 int ret;
2074ac8e9819SChris Mason 
2075ac8e9819SChris Mason 	if (!capable(CAP_SYS_ADMIN))
2076ac8e9819SChris Mason 		return -EPERM;
2077ac8e9819SChris Mason 
20782354d08fSJulia Lawall 	args = memdup_user(argp, sizeof(*args));
20792354d08fSJulia Lawall 	if (IS_ERR(args))
20802354d08fSJulia Lawall 		return PTR_ERR(args);
2081ac8e9819SChris Mason 
2082496ad9aaSAl Viro 	inode = file_inode(file);
2083ac8e9819SChris Mason 	ret = search_ioctl(inode, args);
2084ac8e9819SChris Mason 	if (ret == 0 && copy_to_user(argp, args, sizeof(*args)))
2085ac8e9819SChris Mason 		ret = -EFAULT;
2086ac8e9819SChris Mason 	kfree(args);
2087ac8e9819SChris Mason 	return ret;
2088ac8e9819SChris Mason }
2089ac8e9819SChris Mason 
209098d377a0STARUISI Hiroaki /*
2091ac8e9819SChris Mason  * Search INODE_REFs to identify path name of 'dirid' directory
2092ac8e9819SChris Mason  * in a 'tree_id' tree. and sets path name to 'name'.
209398d377a0STARUISI Hiroaki  */
209498d377a0STARUISI Hiroaki static noinline int btrfs_search_path_in_tree(struct btrfs_fs_info *info,
209598d377a0STARUISI Hiroaki 				u64 tree_id, u64 dirid, char *name)
209698d377a0STARUISI Hiroaki {
209798d377a0STARUISI Hiroaki 	struct btrfs_root *root;
209898d377a0STARUISI Hiroaki 	struct btrfs_key key;
2099ac8e9819SChris Mason 	char *ptr;
210098d377a0STARUISI Hiroaki 	int ret = -1;
210198d377a0STARUISI Hiroaki 	int slot;
210298d377a0STARUISI Hiroaki 	int len;
210398d377a0STARUISI Hiroaki 	int total_len = 0;
210498d377a0STARUISI Hiroaki 	struct btrfs_inode_ref *iref;
210598d377a0STARUISI Hiroaki 	struct extent_buffer *l;
210698d377a0STARUISI Hiroaki 	struct btrfs_path *path;
210798d377a0STARUISI Hiroaki 
210898d377a0STARUISI Hiroaki 	if (dirid == BTRFS_FIRST_FREE_OBJECTID) {
210998d377a0STARUISI Hiroaki 		name[0]='\0';
211098d377a0STARUISI Hiroaki 		return 0;
211198d377a0STARUISI Hiroaki 	}
211298d377a0STARUISI Hiroaki 
211398d377a0STARUISI Hiroaki 	path = btrfs_alloc_path();
211498d377a0STARUISI Hiroaki 	if (!path)
211598d377a0STARUISI Hiroaki 		return -ENOMEM;
211698d377a0STARUISI Hiroaki 
2117ac8e9819SChris Mason 	ptr = &name[BTRFS_INO_LOOKUP_PATH_MAX];
211898d377a0STARUISI Hiroaki 
211998d377a0STARUISI Hiroaki 	key.objectid = tree_id;
212098d377a0STARUISI Hiroaki 	key.type = BTRFS_ROOT_ITEM_KEY;
212198d377a0STARUISI Hiroaki 	key.offset = (u64)-1;
212298d377a0STARUISI Hiroaki 	root = btrfs_read_fs_root_no_name(info, &key);
212398d377a0STARUISI Hiroaki 	if (IS_ERR(root)) {
2124efe120a0SFrank Holton 		printk(KERN_ERR "BTRFS: could not find root %llu\n", tree_id);
21258ad6fcabSChris Mason 		ret = -ENOENT;
21268ad6fcabSChris Mason 		goto out;
212798d377a0STARUISI Hiroaki 	}
212898d377a0STARUISI Hiroaki 
212998d377a0STARUISI Hiroaki 	key.objectid = dirid;
213098d377a0STARUISI Hiroaki 	key.type = BTRFS_INODE_REF_KEY;
21318ad6fcabSChris Mason 	key.offset = (u64)-1;
213298d377a0STARUISI Hiroaki 
213398d377a0STARUISI Hiroaki 	while (1) {
213498d377a0STARUISI Hiroaki 		ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
213598d377a0STARUISI Hiroaki 		if (ret < 0)
213698d377a0STARUISI Hiroaki 			goto out;
213718674c6cSFilipe David Borba Manana 		else if (ret > 0) {
213818674c6cSFilipe David Borba Manana 			ret = btrfs_previous_item(root, path, dirid,
213918674c6cSFilipe David Borba Manana 						  BTRFS_INODE_REF_KEY);
214018674c6cSFilipe David Borba Manana 			if (ret < 0)
214118674c6cSFilipe David Borba Manana 				goto out;
214218674c6cSFilipe David Borba Manana 			else if (ret > 0) {
2143ac8e9819SChris Mason 				ret = -ENOENT;
214498d377a0STARUISI Hiroaki 				goto out;
2145ac8e9819SChris Mason 			}
214618674c6cSFilipe David Borba Manana 		}
214718674c6cSFilipe David Borba Manana 
214818674c6cSFilipe David Borba Manana 		l = path->nodes[0];
214918674c6cSFilipe David Borba Manana 		slot = path->slots[0];
215018674c6cSFilipe David Borba Manana 		btrfs_item_key_to_cpu(l, &key, slot);
215198d377a0STARUISI Hiroaki 
215298d377a0STARUISI Hiroaki 		iref = btrfs_item_ptr(l, slot, struct btrfs_inode_ref);
215398d377a0STARUISI Hiroaki 		len = btrfs_inode_ref_name_len(l, iref);
215498d377a0STARUISI Hiroaki 		ptr -= len + 1;
215598d377a0STARUISI Hiroaki 		total_len += len + 1;
2156a696cf35SFilipe David Borba Manana 		if (ptr < name) {
2157a696cf35SFilipe David Borba Manana 			ret = -ENAMETOOLONG;
215898d377a0STARUISI Hiroaki 			goto out;
2159a696cf35SFilipe David Borba Manana 		}
216098d377a0STARUISI Hiroaki 
216198d377a0STARUISI Hiroaki 		*(ptr + len) = '/';
216298d377a0STARUISI Hiroaki 		read_extent_buffer(l, ptr, (unsigned long)(iref + 1), len);
216398d377a0STARUISI Hiroaki 
216498d377a0STARUISI Hiroaki 		if (key.offset == BTRFS_FIRST_FREE_OBJECTID)
216598d377a0STARUISI Hiroaki 			break;
216698d377a0STARUISI Hiroaki 
2167b3b4aa74SDavid Sterba 		btrfs_release_path(path);
216898d377a0STARUISI Hiroaki 		key.objectid = key.offset;
21698ad6fcabSChris Mason 		key.offset = (u64)-1;
217098d377a0STARUISI Hiroaki 		dirid = key.objectid;
217198d377a0STARUISI Hiroaki 	}
217277906a50SLi Zefan 	memmove(name, ptr, total_len);
217398d377a0STARUISI Hiroaki 	name[total_len] = '\0';
217498d377a0STARUISI Hiroaki 	ret = 0;
217598d377a0STARUISI Hiroaki out:
217698d377a0STARUISI Hiroaki 	btrfs_free_path(path);
2177ac8e9819SChris Mason 	return ret;
2178ac8e9819SChris Mason }
2179ac8e9819SChris Mason 
2180ac8e9819SChris Mason static noinline int btrfs_ioctl_ino_lookup(struct file *file,
2181ac8e9819SChris Mason 					   void __user *argp)
2182ac8e9819SChris Mason {
2183ac8e9819SChris Mason 	 struct btrfs_ioctl_ino_lookup_args *args;
2184ac8e9819SChris Mason 	 struct inode *inode;
2185ac8e9819SChris Mason 	 int ret;
2186ac8e9819SChris Mason 
2187ac8e9819SChris Mason 	if (!capable(CAP_SYS_ADMIN))
2188ac8e9819SChris Mason 		return -EPERM;
2189ac8e9819SChris Mason 
21902354d08fSJulia Lawall 	args = memdup_user(argp, sizeof(*args));
21912354d08fSJulia Lawall 	if (IS_ERR(args))
21922354d08fSJulia Lawall 		return PTR_ERR(args);
2193c2b96929SDan Carpenter 
2194496ad9aaSAl Viro 	inode = file_inode(file);
2195ac8e9819SChris Mason 
21961b53ac4dSChris Mason 	if (args->treeid == 0)
21971b53ac4dSChris Mason 		args->treeid = BTRFS_I(inode)->root->root_key.objectid;
21981b53ac4dSChris Mason 
2199ac8e9819SChris Mason 	ret = btrfs_search_path_in_tree(BTRFS_I(inode)->root->fs_info,
2200ac8e9819SChris Mason 					args->treeid, args->objectid,
2201ac8e9819SChris Mason 					args->name);
2202ac8e9819SChris Mason 
2203ac8e9819SChris Mason 	if (ret == 0 && copy_to_user(argp, args, sizeof(*args)))
2204ac8e9819SChris Mason 		ret = -EFAULT;
2205ac8e9819SChris Mason 
2206ac8e9819SChris Mason 	kfree(args);
220798d377a0STARUISI Hiroaki 	return ret;
220898d377a0STARUISI Hiroaki }
220998d377a0STARUISI Hiroaki 
221076dda93cSYan, Zheng static noinline int btrfs_ioctl_snap_destroy(struct file *file,
221176dda93cSYan, Zheng 					     void __user *arg)
221276dda93cSYan, Zheng {
221354563d41SAl Viro 	struct dentry *parent = file->f_path.dentry;
221476dda93cSYan, Zheng 	struct dentry *dentry;
221576dda93cSYan, Zheng 	struct inode *dir = parent->d_inode;
221676dda93cSYan, Zheng 	struct inode *inode;
221776dda93cSYan, Zheng 	struct btrfs_root *root = BTRFS_I(dir)->root;
221876dda93cSYan, Zheng 	struct btrfs_root *dest = NULL;
221976dda93cSYan, Zheng 	struct btrfs_ioctl_vol_args *vol_args;
222076dda93cSYan, Zheng 	struct btrfs_trans_handle *trans;
2221c58aaad2SMiao Xie 	struct btrfs_block_rsv block_rsv;
2222521e0546SDavid Sterba 	u64 root_flags;
2223c58aaad2SMiao Xie 	u64 qgroup_reserved;
222476dda93cSYan, Zheng 	int namelen;
222576dda93cSYan, Zheng 	int ret;
222676dda93cSYan, Zheng 	int err = 0;
222776dda93cSYan, Zheng 
222876dda93cSYan, Zheng 	vol_args = memdup_user(arg, sizeof(*vol_args));
222976dda93cSYan, Zheng 	if (IS_ERR(vol_args))
223076dda93cSYan, Zheng 		return PTR_ERR(vol_args);
223176dda93cSYan, Zheng 
223276dda93cSYan, Zheng 	vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
223376dda93cSYan, Zheng 	namelen = strlen(vol_args->name);
223476dda93cSYan, Zheng 	if (strchr(vol_args->name, '/') ||
223576dda93cSYan, Zheng 	    strncmp(vol_args->name, "..", namelen) == 0) {
223676dda93cSYan, Zheng 		err = -EINVAL;
223776dda93cSYan, Zheng 		goto out;
223876dda93cSYan, Zheng 	}
223976dda93cSYan, Zheng 
2240a561be71SAl Viro 	err = mnt_want_write_file(file);
224176dda93cSYan, Zheng 	if (err)
224276dda93cSYan, Zheng 		goto out;
224376dda93cSYan, Zheng 
2244521e0546SDavid Sterba 
22455c50c9b8SDavid Sterba 	err = mutex_lock_killable_nested(&dir->i_mutex, I_MUTEX_PARENT);
22465c50c9b8SDavid Sterba 	if (err == -EINTR)
2247e43f998eSDavid Sterba 		goto out_drop_write;
224876dda93cSYan, Zheng 	dentry = lookup_one_len(vol_args->name, parent, namelen);
224976dda93cSYan, Zheng 	if (IS_ERR(dentry)) {
225076dda93cSYan, Zheng 		err = PTR_ERR(dentry);
225176dda93cSYan, Zheng 		goto out_unlock_dir;
225276dda93cSYan, Zheng 	}
225376dda93cSYan, Zheng 
225476dda93cSYan, Zheng 	if (!dentry->d_inode) {
225576dda93cSYan, Zheng 		err = -ENOENT;
225676dda93cSYan, Zheng 		goto out_dput;
225776dda93cSYan, Zheng 	}
225876dda93cSYan, Zheng 
225976dda93cSYan, Zheng 	inode = dentry->d_inode;
22604260f7c7SSage Weil 	dest = BTRFS_I(inode)->root;
22614260f7c7SSage Weil 	if (!capable(CAP_SYS_ADMIN)) {
22624260f7c7SSage Weil 		/*
22634260f7c7SSage Weil 		 * Regular user.  Only allow this with a special mount
22644260f7c7SSage Weil 		 * option, when the user has write+exec access to the
22654260f7c7SSage Weil 		 * subvol root, and when rmdir(2) would have been
22664260f7c7SSage Weil 		 * allowed.
22674260f7c7SSage Weil 		 *
22684260f7c7SSage Weil 		 * Note that this is _not_ check that the subvol is
22694260f7c7SSage Weil 		 * empty or doesn't contain data that we wouldn't
22704260f7c7SSage Weil 		 * otherwise be able to delete.
22714260f7c7SSage Weil 		 *
22724260f7c7SSage Weil 		 * Users who want to delete empty subvols should try
22734260f7c7SSage Weil 		 * rmdir(2).
22744260f7c7SSage Weil 		 */
22754260f7c7SSage Weil 		err = -EPERM;
22764260f7c7SSage Weil 		if (!btrfs_test_opt(root, USER_SUBVOL_RM_ALLOWED))
22774260f7c7SSage Weil 			goto out_dput;
22784260f7c7SSage Weil 
22794260f7c7SSage Weil 		/*
22804260f7c7SSage Weil 		 * Do not allow deletion if the parent dir is the same
22814260f7c7SSage Weil 		 * as the dir to be deleted.  That means the ioctl
22824260f7c7SSage Weil 		 * must be called on the dentry referencing the root
22834260f7c7SSage Weil 		 * of the subvol, not a random directory contained
22844260f7c7SSage Weil 		 * within it.
22854260f7c7SSage Weil 		 */
22864260f7c7SSage Weil 		err = -EINVAL;
22874260f7c7SSage Weil 		if (root == dest)
22884260f7c7SSage Weil 			goto out_dput;
22894260f7c7SSage Weil 
22904260f7c7SSage Weil 		err = inode_permission(inode, MAY_WRITE | MAY_EXEC);
22914260f7c7SSage Weil 		if (err)
22924260f7c7SSage Weil 			goto out_dput;
22935c39da5bSMiao Xie 	}
22944260f7c7SSage Weil 
22955c39da5bSMiao Xie 	/* check if subvolume may be deleted by a user */
22964260f7c7SSage Weil 	err = btrfs_may_delete(dir, dentry, 1);
22974260f7c7SSage Weil 	if (err)
22984260f7c7SSage Weil 		goto out_dput;
22994260f7c7SSage Weil 
230033345d01SLi Zefan 	if (btrfs_ino(inode) != BTRFS_FIRST_FREE_OBJECTID) {
230176dda93cSYan, Zheng 		err = -EINVAL;
230276dda93cSYan, Zheng 		goto out_dput;
230376dda93cSYan, Zheng 	}
230476dda93cSYan, Zheng 
230576dda93cSYan, Zheng 	mutex_lock(&inode->i_mutex);
2306521e0546SDavid Sterba 
2307521e0546SDavid Sterba 	/*
2308521e0546SDavid Sterba 	 * Don't allow to delete a subvolume with send in progress. This is
2309521e0546SDavid Sterba 	 * inside the i_mutex so the error handling that has to drop the bit
2310521e0546SDavid Sterba 	 * again is not run concurrently.
2311521e0546SDavid Sterba 	 */
2312521e0546SDavid Sterba 	spin_lock(&dest->root_item_lock);
2313521e0546SDavid Sterba 	root_flags = btrfs_root_flags(&root->root_item);
2314521e0546SDavid Sterba 	if (root->send_in_progress == 0) {
2315521e0546SDavid Sterba 		btrfs_set_root_flags(&root->root_item,
2316521e0546SDavid Sterba 				root_flags | BTRFS_ROOT_SUBVOL_DEAD);
2317521e0546SDavid Sterba 		spin_unlock(&dest->root_item_lock);
2318521e0546SDavid Sterba 	} else {
2319521e0546SDavid Sterba 		spin_unlock(&dest->root_item_lock);
2320521e0546SDavid Sterba 		btrfs_warn(root->fs_info,
2321521e0546SDavid Sterba 			"Attempt to delete subvolume %llu during send",
2322521e0546SDavid Sterba 			root->root_key.objectid);
2323521e0546SDavid Sterba 		err = -EPERM;
2324521e0546SDavid Sterba 		goto out_dput;
2325521e0546SDavid Sterba 	}
2326521e0546SDavid Sterba 
232776dda93cSYan, Zheng 	err = d_invalidate(dentry);
232876dda93cSYan, Zheng 	if (err)
232976dda93cSYan, Zheng 		goto out_unlock;
233076dda93cSYan, Zheng 
233176dda93cSYan, Zheng 	down_write(&root->fs_info->subvol_sem);
233276dda93cSYan, Zheng 
233376dda93cSYan, Zheng 	err = may_destroy_subvol(dest);
233476dda93cSYan, Zheng 	if (err)
233576dda93cSYan, Zheng 		goto out_up_write;
233676dda93cSYan, Zheng 
2337c58aaad2SMiao Xie 	btrfs_init_block_rsv(&block_rsv, BTRFS_BLOCK_RSV_TEMP);
2338c58aaad2SMiao Xie 	/*
2339c58aaad2SMiao Xie 	 * One for dir inode, two for dir entries, two for root
2340c58aaad2SMiao Xie 	 * ref/backref.
2341c58aaad2SMiao Xie 	 */
2342c58aaad2SMiao Xie 	err = btrfs_subvolume_reserve_metadata(root, &block_rsv,
2343ee3441b4SJeff Mahoney 					       5, &qgroup_reserved, true);
2344c58aaad2SMiao Xie 	if (err)
2345c58aaad2SMiao Xie 		goto out_up_write;
2346c58aaad2SMiao Xie 
2347a22285a6SYan, Zheng 	trans = btrfs_start_transaction(root, 0);
2348a22285a6SYan, Zheng 	if (IS_ERR(trans)) {
2349a22285a6SYan, Zheng 		err = PTR_ERR(trans);
2350c58aaad2SMiao Xie 		goto out_release;
2351a22285a6SYan, Zheng 	}
2352c58aaad2SMiao Xie 	trans->block_rsv = &block_rsv;
2353c58aaad2SMiao Xie 	trans->bytes_reserved = block_rsv.size;
2354a22285a6SYan, Zheng 
235576dda93cSYan, Zheng 	ret = btrfs_unlink_subvol(trans, root, dir,
235676dda93cSYan, Zheng 				dest->root_key.objectid,
235776dda93cSYan, Zheng 				dentry->d_name.name,
235876dda93cSYan, Zheng 				dentry->d_name.len);
235979787eaaSJeff Mahoney 	if (ret) {
236079787eaaSJeff Mahoney 		err = ret;
236179787eaaSJeff Mahoney 		btrfs_abort_transaction(trans, root, ret);
236279787eaaSJeff Mahoney 		goto out_end_trans;
236379787eaaSJeff Mahoney 	}
236476dda93cSYan, Zheng 
236576dda93cSYan, Zheng 	btrfs_record_root_in_trans(trans, dest);
236676dda93cSYan, Zheng 
236776dda93cSYan, Zheng 	memset(&dest->root_item.drop_progress, 0,
236876dda93cSYan, Zheng 		sizeof(dest->root_item.drop_progress));
236976dda93cSYan, Zheng 	dest->root_item.drop_level = 0;
237076dda93cSYan, Zheng 	btrfs_set_root_refs(&dest->root_item, 0);
237176dda93cSYan, Zheng 
2372d68fc57bSYan, Zheng 	if (!xchg(&dest->orphan_item_inserted, 1)) {
237376dda93cSYan, Zheng 		ret = btrfs_insert_orphan_item(trans,
237476dda93cSYan, Zheng 					root->fs_info->tree_root,
237576dda93cSYan, Zheng 					dest->root_key.objectid);
237679787eaaSJeff Mahoney 		if (ret) {
237779787eaaSJeff Mahoney 			btrfs_abort_transaction(trans, root, ret);
237879787eaaSJeff Mahoney 			err = ret;
237979787eaaSJeff Mahoney 			goto out_end_trans;
2380d68fc57bSYan, Zheng 		}
238179787eaaSJeff Mahoney 	}
2382dd5f9615SStefan Behrens 
2383dd5f9615SStefan Behrens 	ret = btrfs_uuid_tree_rem(trans, root->fs_info->uuid_root,
2384dd5f9615SStefan Behrens 				  dest->root_item.uuid, BTRFS_UUID_KEY_SUBVOL,
2385dd5f9615SStefan Behrens 				  dest->root_key.objectid);
2386dd5f9615SStefan Behrens 	if (ret && ret != -ENOENT) {
2387dd5f9615SStefan Behrens 		btrfs_abort_transaction(trans, root, ret);
2388dd5f9615SStefan Behrens 		err = ret;
2389dd5f9615SStefan Behrens 		goto out_end_trans;
2390dd5f9615SStefan Behrens 	}
2391dd5f9615SStefan Behrens 	if (!btrfs_is_empty_uuid(dest->root_item.received_uuid)) {
2392dd5f9615SStefan Behrens 		ret = btrfs_uuid_tree_rem(trans, root->fs_info->uuid_root,
2393dd5f9615SStefan Behrens 					  dest->root_item.received_uuid,
2394dd5f9615SStefan Behrens 					  BTRFS_UUID_KEY_RECEIVED_SUBVOL,
2395dd5f9615SStefan Behrens 					  dest->root_key.objectid);
2396dd5f9615SStefan Behrens 		if (ret && ret != -ENOENT) {
2397dd5f9615SStefan Behrens 			btrfs_abort_transaction(trans, root, ret);
2398dd5f9615SStefan Behrens 			err = ret;
2399dd5f9615SStefan Behrens 			goto out_end_trans;
2400dd5f9615SStefan Behrens 		}
2401dd5f9615SStefan Behrens 	}
2402dd5f9615SStefan Behrens 
240379787eaaSJeff Mahoney out_end_trans:
2404c58aaad2SMiao Xie 	trans->block_rsv = NULL;
2405c58aaad2SMiao Xie 	trans->bytes_reserved = 0;
2406531cb13fSSage Weil 	ret = btrfs_end_transaction(trans, root);
240779787eaaSJeff Mahoney 	if (ret && !err)
240879787eaaSJeff Mahoney 		err = ret;
240976dda93cSYan, Zheng 	inode->i_flags |= S_DEAD;
2410c58aaad2SMiao Xie out_release:
2411c58aaad2SMiao Xie 	btrfs_subvolume_release_metadata(root, &block_rsv, qgroup_reserved);
241276dda93cSYan, Zheng out_up_write:
241376dda93cSYan, Zheng 	up_write(&root->fs_info->subvol_sem);
241476dda93cSYan, Zheng out_unlock:
2415521e0546SDavid Sterba 	if (err) {
2416521e0546SDavid Sterba 		spin_lock(&dest->root_item_lock);
2417521e0546SDavid Sterba 		root_flags = btrfs_root_flags(&root->root_item);
2418521e0546SDavid Sterba 		btrfs_set_root_flags(&root->root_item,
2419521e0546SDavid Sterba 				root_flags & ~BTRFS_ROOT_SUBVOL_DEAD);
2420521e0546SDavid Sterba 		spin_unlock(&dest->root_item_lock);
2421521e0546SDavid Sterba 	}
242276dda93cSYan, Zheng 	mutex_unlock(&inode->i_mutex);
242376dda93cSYan, Zheng 	if (!err) {
2424efefb143SYan, Zheng 		shrink_dcache_sb(root->fs_info->sb);
242576dda93cSYan, Zheng 		btrfs_invalidate_inodes(dest);
242676dda93cSYan, Zheng 		d_delete(dentry);
2427fa6ac876SLiu Bo 
2428fa6ac876SLiu Bo 		/* the last ref */
2429fa6ac876SLiu Bo 		if (dest->cache_inode) {
2430fa6ac876SLiu Bo 			iput(dest->cache_inode);
2431fa6ac876SLiu Bo 			dest->cache_inode = NULL;
2432fa6ac876SLiu Bo 		}
243376dda93cSYan, Zheng 	}
243476dda93cSYan, Zheng out_dput:
243576dda93cSYan, Zheng 	dput(dentry);
243676dda93cSYan, Zheng out_unlock_dir:
243776dda93cSYan, Zheng 	mutex_unlock(&dir->i_mutex);
2438e43f998eSDavid Sterba out_drop_write:
24392a79f17eSAl Viro 	mnt_drop_write_file(file);
244076dda93cSYan, Zheng out:
244176dda93cSYan, Zheng 	kfree(vol_args);
244276dda93cSYan, Zheng 	return err;
244376dda93cSYan, Zheng }
244476dda93cSYan, Zheng 
24451e701a32SChris Mason static int btrfs_ioctl_defrag(struct file *file, void __user *argp)
2446f46b5a66SChristoph Hellwig {
2447496ad9aaSAl Viro 	struct inode *inode = file_inode(file);
2448f46b5a66SChristoph Hellwig 	struct btrfs_root *root = BTRFS_I(inode)->root;
24491e701a32SChris Mason 	struct btrfs_ioctl_defrag_range_args *range;
2450c146afadSYan Zheng 	int ret;
2451c146afadSYan Zheng 
245225122d15SIlya Dryomov 	ret = mnt_want_write_file(file);
245325122d15SIlya Dryomov 	if (ret)
245425122d15SIlya Dryomov 		return ret;
2455b83cc969SLi Zefan 
245625122d15SIlya Dryomov 	if (btrfs_root_readonly(root)) {
245725122d15SIlya Dryomov 		ret = -EROFS;
245825122d15SIlya Dryomov 		goto out;
24595ac00addSStefan Behrens 	}
2460f46b5a66SChristoph Hellwig 
2461f46b5a66SChristoph Hellwig 	switch (inode->i_mode & S_IFMT) {
2462f46b5a66SChristoph Hellwig 	case S_IFDIR:
2463e441d54dSChris Mason 		if (!capable(CAP_SYS_ADMIN)) {
2464e441d54dSChris Mason 			ret = -EPERM;
2465e441d54dSChris Mason 			goto out;
2466e441d54dSChris Mason 		}
2467de78b51aSEric Sandeen 		ret = btrfs_defrag_root(root);
24688929ecfaSYan, Zheng 		if (ret)
24698929ecfaSYan, Zheng 			goto out;
2470de78b51aSEric Sandeen 		ret = btrfs_defrag_root(root->fs_info->extent_root);
2471f46b5a66SChristoph Hellwig 		break;
2472f46b5a66SChristoph Hellwig 	case S_IFREG:
2473e441d54dSChris Mason 		if (!(file->f_mode & FMODE_WRITE)) {
2474e441d54dSChris Mason 			ret = -EINVAL;
2475e441d54dSChris Mason 			goto out;
2476e441d54dSChris Mason 		}
24771e701a32SChris Mason 
24781e701a32SChris Mason 		range = kzalloc(sizeof(*range), GFP_KERNEL);
24791e701a32SChris Mason 		if (!range) {
24801e701a32SChris Mason 			ret = -ENOMEM;
24811e701a32SChris Mason 			goto out;
24821e701a32SChris Mason 		}
24831e701a32SChris Mason 
24841e701a32SChris Mason 		if (argp) {
24851e701a32SChris Mason 			if (copy_from_user(range, argp,
24861e701a32SChris Mason 					   sizeof(*range))) {
24871e701a32SChris Mason 				ret = -EFAULT;
24881e701a32SChris Mason 				kfree(range);
2489683be16eSDan Carpenter 				goto out;
24901e701a32SChris Mason 			}
24911e701a32SChris Mason 			/* compression requires us to start the IO */
24921e701a32SChris Mason 			if ((range->flags & BTRFS_DEFRAG_RANGE_COMPRESS)) {
24931e701a32SChris Mason 				range->flags |= BTRFS_DEFRAG_RANGE_START_IO;
24941e701a32SChris Mason 				range->extent_thresh = (u32)-1;
24951e701a32SChris Mason 			}
24961e701a32SChris Mason 		} else {
24971e701a32SChris Mason 			/* the rest are all set to zero by kzalloc */
24981e701a32SChris Mason 			range->len = (u64)-1;
24991e701a32SChris Mason 		}
2500496ad9aaSAl Viro 		ret = btrfs_defrag_file(file_inode(file), file,
25014cb5300bSChris Mason 					range, 0, 0);
25024cb5300bSChris Mason 		if (ret > 0)
25034cb5300bSChris Mason 			ret = 0;
25041e701a32SChris Mason 		kfree(range);
2505f46b5a66SChristoph Hellwig 		break;
25068929ecfaSYan, Zheng 	default:
25078929ecfaSYan, Zheng 		ret = -EINVAL;
2508f46b5a66SChristoph Hellwig 	}
2509e441d54dSChris Mason out:
251025122d15SIlya Dryomov 	mnt_drop_write_file(file);
2511e441d54dSChris Mason 	return ret;
2512f46b5a66SChristoph Hellwig }
2513f46b5a66SChristoph Hellwig 
2514b2950863SChristoph Hellwig static long btrfs_ioctl_add_dev(struct btrfs_root *root, void __user *arg)
2515f46b5a66SChristoph Hellwig {
2516f46b5a66SChristoph Hellwig 	struct btrfs_ioctl_vol_args *vol_args;
2517f46b5a66SChristoph Hellwig 	int ret;
2518f46b5a66SChristoph Hellwig 
2519e441d54dSChris Mason 	if (!capable(CAP_SYS_ADMIN))
2520e441d54dSChris Mason 		return -EPERM;
2521e441d54dSChris Mason 
25225ac00addSStefan Behrens 	if (atomic_xchg(&root->fs_info->mutually_exclusive_operation_running,
25235ac00addSStefan Behrens 			1)) {
2524e57138b3SAnand Jain 		return BTRFS_ERROR_DEV_EXCL_RUN_IN_PROGRESS;
2525c9e9f97bSIlya Dryomov 	}
2526c9e9f97bSIlya Dryomov 
25275ac00addSStefan Behrens 	mutex_lock(&root->fs_info->volume_mutex);
2528dae7b665SLi Zefan 	vol_args = memdup_user(arg, sizeof(*vol_args));
2529c9e9f97bSIlya Dryomov 	if (IS_ERR(vol_args)) {
2530c9e9f97bSIlya Dryomov 		ret = PTR_ERR(vol_args);
2531c9e9f97bSIlya Dryomov 		goto out;
2532c9e9f97bSIlya Dryomov 	}
2533f46b5a66SChristoph Hellwig 
25345516e595SMark Fasheh 	vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
2535f46b5a66SChristoph Hellwig 	ret = btrfs_init_new_device(root, vol_args->name);
2536f46b5a66SChristoph Hellwig 
2537f46b5a66SChristoph Hellwig 	kfree(vol_args);
2538c9e9f97bSIlya Dryomov out:
2539c9e9f97bSIlya Dryomov 	mutex_unlock(&root->fs_info->volume_mutex);
25405ac00addSStefan Behrens 	atomic_set(&root->fs_info->mutually_exclusive_operation_running, 0);
2541f46b5a66SChristoph Hellwig 	return ret;
2542f46b5a66SChristoph Hellwig }
2543f46b5a66SChristoph Hellwig 
2544da24927bSMiao Xie static long btrfs_ioctl_rm_dev(struct file *file, void __user *arg)
2545f46b5a66SChristoph Hellwig {
2546496ad9aaSAl Viro 	struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
2547f46b5a66SChristoph Hellwig 	struct btrfs_ioctl_vol_args *vol_args;
2548f46b5a66SChristoph Hellwig 	int ret;
2549f46b5a66SChristoph Hellwig 
2550e441d54dSChris Mason 	if (!capable(CAP_SYS_ADMIN))
2551e441d54dSChris Mason 		return -EPERM;
2552e441d54dSChris Mason 
2553da24927bSMiao Xie 	ret = mnt_want_write_file(file);
2554da24927bSMiao Xie 	if (ret)
2555da24927bSMiao Xie 		return ret;
2556c146afadSYan Zheng 
2557dae7b665SLi Zefan 	vol_args = memdup_user(arg, sizeof(*vol_args));
2558c9e9f97bSIlya Dryomov 	if (IS_ERR(vol_args)) {
2559c9e9f97bSIlya Dryomov 		ret = PTR_ERR(vol_args);
2560c9e9f97bSIlya Dryomov 		goto out;
2561c9e9f97bSIlya Dryomov 	}
2562f46b5a66SChristoph Hellwig 
25635516e595SMark Fasheh 	vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
2564f46b5a66SChristoph Hellwig 
2565183860f6SAnand Jain 	if (atomic_xchg(&root->fs_info->mutually_exclusive_operation_running,
2566183860f6SAnand Jain 			1)) {
2567183860f6SAnand Jain 		ret = BTRFS_ERROR_DEV_EXCL_RUN_IN_PROGRESS;
2568183860f6SAnand Jain 		goto out;
2569183860f6SAnand Jain 	}
2570183860f6SAnand Jain 
2571183860f6SAnand Jain 	mutex_lock(&root->fs_info->volume_mutex);
2572183860f6SAnand Jain 	ret = btrfs_rm_device(root, vol_args->name);
2573c9e9f97bSIlya Dryomov 	mutex_unlock(&root->fs_info->volume_mutex);
25745ac00addSStefan Behrens 	atomic_set(&root->fs_info->mutually_exclusive_operation_running, 0);
2575183860f6SAnand Jain 
2576183860f6SAnand Jain out:
2577183860f6SAnand Jain 	kfree(vol_args);
25784ac20c70SIlya Dryomov 	mnt_drop_write_file(file);
2579f46b5a66SChristoph Hellwig 	return ret;
2580f46b5a66SChristoph Hellwig }
2581f46b5a66SChristoph Hellwig 
2582475f6387SJan Schmidt static long btrfs_ioctl_fs_info(struct btrfs_root *root, void __user *arg)
2583475f6387SJan Schmidt {
2584027ed2f0SLi Zefan 	struct btrfs_ioctl_fs_info_args *fi_args;
2585475f6387SJan Schmidt 	struct btrfs_device *device;
2586475f6387SJan Schmidt 	struct btrfs_device *next;
2587475f6387SJan Schmidt 	struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices;
2588027ed2f0SLi Zefan 	int ret = 0;
2589475f6387SJan Schmidt 
2590027ed2f0SLi Zefan 	fi_args = kzalloc(sizeof(*fi_args), GFP_KERNEL);
2591027ed2f0SLi Zefan 	if (!fi_args)
2592027ed2f0SLi Zefan 		return -ENOMEM;
2593027ed2f0SLi Zefan 
2594f7171750SFilipe David Borba Manana 	mutex_lock(&fs_devices->device_list_mutex);
2595027ed2f0SLi Zefan 	fi_args->num_devices = fs_devices->num_devices;
2596027ed2f0SLi Zefan 	memcpy(&fi_args->fsid, root->fs_info->fsid, sizeof(fi_args->fsid));
2597475f6387SJan Schmidt 
2598475f6387SJan Schmidt 	list_for_each_entry_safe(device, next, &fs_devices->devices, dev_list) {
2599027ed2f0SLi Zefan 		if (device->devid > fi_args->max_id)
2600027ed2f0SLi Zefan 			fi_args->max_id = device->devid;
2601475f6387SJan Schmidt 	}
2602475f6387SJan Schmidt 	mutex_unlock(&fs_devices->device_list_mutex);
2603475f6387SJan Schmidt 
260480a773fbSDavid Sterba 	fi_args->nodesize = root->fs_info->super_copy->nodesize;
260580a773fbSDavid Sterba 	fi_args->sectorsize = root->fs_info->super_copy->sectorsize;
260680a773fbSDavid Sterba 	fi_args->clone_alignment = root->fs_info->super_copy->sectorsize;
260780a773fbSDavid Sterba 
2608027ed2f0SLi Zefan 	if (copy_to_user(arg, fi_args, sizeof(*fi_args)))
2609027ed2f0SLi Zefan 		ret = -EFAULT;
2610475f6387SJan Schmidt 
2611027ed2f0SLi Zefan 	kfree(fi_args);
2612027ed2f0SLi Zefan 	return ret;
2613475f6387SJan Schmidt }
2614475f6387SJan Schmidt 
2615475f6387SJan Schmidt static long btrfs_ioctl_dev_info(struct btrfs_root *root, void __user *arg)
2616475f6387SJan Schmidt {
2617475f6387SJan Schmidt 	struct btrfs_ioctl_dev_info_args *di_args;
2618475f6387SJan Schmidt 	struct btrfs_device *dev;
2619475f6387SJan Schmidt 	struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices;
2620475f6387SJan Schmidt 	int ret = 0;
2621475f6387SJan Schmidt 	char *s_uuid = NULL;
2622475f6387SJan Schmidt 
2623475f6387SJan Schmidt 	di_args = memdup_user(arg, sizeof(*di_args));
2624475f6387SJan Schmidt 	if (IS_ERR(di_args))
2625475f6387SJan Schmidt 		return PTR_ERR(di_args);
2626475f6387SJan Schmidt 
2627dd5f9615SStefan Behrens 	if (!btrfs_is_empty_uuid(di_args->uuid))
2628475f6387SJan Schmidt 		s_uuid = di_args->uuid;
2629475f6387SJan Schmidt 
2630475f6387SJan Schmidt 	mutex_lock(&fs_devices->device_list_mutex);
2631aa1b8cd4SStefan Behrens 	dev = btrfs_find_device(root->fs_info, di_args->devid, s_uuid, NULL);
2632475f6387SJan Schmidt 
2633475f6387SJan Schmidt 	if (!dev) {
2634475f6387SJan Schmidt 		ret = -ENODEV;
2635475f6387SJan Schmidt 		goto out;
2636475f6387SJan Schmidt 	}
2637475f6387SJan Schmidt 
2638475f6387SJan Schmidt 	di_args->devid = dev->devid;
2639475f6387SJan Schmidt 	di_args->bytes_used = dev->bytes_used;
2640475f6387SJan Schmidt 	di_args->total_bytes = dev->total_bytes;
2641475f6387SJan Schmidt 	memcpy(di_args->uuid, dev->uuid, sizeof(di_args->uuid));
2642a27202fbSJim Meyering 	if (dev->name) {
2643606686eeSJosef Bacik 		struct rcu_string *name;
2644606686eeSJosef Bacik 
2645606686eeSJosef Bacik 		rcu_read_lock();
2646606686eeSJosef Bacik 		name = rcu_dereference(dev->name);
2647606686eeSJosef Bacik 		strncpy(di_args->path, name->str, sizeof(di_args->path));
2648606686eeSJosef Bacik 		rcu_read_unlock();
2649a27202fbSJim Meyering 		di_args->path[sizeof(di_args->path) - 1] = 0;
2650a27202fbSJim Meyering 	} else {
265199ba55adSStefan Behrens 		di_args->path[0] = '\0';
2652a27202fbSJim Meyering 	}
2653475f6387SJan Schmidt 
2654475f6387SJan Schmidt out:
265555793c0dSDavid Sterba 	mutex_unlock(&fs_devices->device_list_mutex);
2656475f6387SJan Schmidt 	if (ret == 0 && copy_to_user(arg, di_args, sizeof(*di_args)))
2657475f6387SJan Schmidt 		ret = -EFAULT;
2658475f6387SJan Schmidt 
2659475f6387SJan Schmidt 	kfree(di_args);
2660475f6387SJan Schmidt 	return ret;
2661475f6387SJan Schmidt }
2662475f6387SJan Schmidt 
2663416161dbSMark Fasheh static struct page *extent_same_get_page(struct inode *inode, u64 off)
2664416161dbSMark Fasheh {
2665416161dbSMark Fasheh 	struct page *page;
2666416161dbSMark Fasheh 	pgoff_t index;
2667416161dbSMark Fasheh 	struct extent_io_tree *tree = &BTRFS_I(inode)->io_tree;
2668416161dbSMark Fasheh 
2669416161dbSMark Fasheh 	index = off >> PAGE_CACHE_SHIFT;
2670416161dbSMark Fasheh 
2671416161dbSMark Fasheh 	page = grab_cache_page(inode->i_mapping, index);
2672416161dbSMark Fasheh 	if (!page)
2673416161dbSMark Fasheh 		return NULL;
2674416161dbSMark Fasheh 
2675416161dbSMark Fasheh 	if (!PageUptodate(page)) {
2676416161dbSMark Fasheh 		if (extent_read_full_page_nolock(tree, page, btrfs_get_extent,
2677416161dbSMark Fasheh 						 0))
2678416161dbSMark Fasheh 			return NULL;
2679416161dbSMark Fasheh 		lock_page(page);
2680416161dbSMark Fasheh 		if (!PageUptodate(page)) {
2681416161dbSMark Fasheh 			unlock_page(page);
2682416161dbSMark Fasheh 			page_cache_release(page);
2683416161dbSMark Fasheh 			return NULL;
2684416161dbSMark Fasheh 		}
2685416161dbSMark Fasheh 	}
2686416161dbSMark Fasheh 	unlock_page(page);
2687416161dbSMark Fasheh 
2688416161dbSMark Fasheh 	return page;
2689416161dbSMark Fasheh }
2690416161dbSMark Fasheh 
269177fe20dcSMark Fasheh static inline void lock_extent_range(struct inode *inode, u64 off, u64 len)
269277fe20dcSMark Fasheh {
269377fe20dcSMark Fasheh 	/* do any pending delalloc/csum calc on src, one way or
269477fe20dcSMark Fasheh 	   another, and lock file content */
269577fe20dcSMark Fasheh 	while (1) {
269677fe20dcSMark Fasheh 		struct btrfs_ordered_extent *ordered;
269777fe20dcSMark Fasheh 		lock_extent(&BTRFS_I(inode)->io_tree, off, off + len - 1);
269877fe20dcSMark Fasheh 		ordered = btrfs_lookup_first_ordered_extent(inode,
269977fe20dcSMark Fasheh 							    off + len - 1);
270077fe20dcSMark Fasheh 		if (!ordered &&
270177fe20dcSMark Fasheh 		    !test_range_bit(&BTRFS_I(inode)->io_tree, off,
270277fe20dcSMark Fasheh 				    off + len - 1, EXTENT_DELALLOC, 0, NULL))
270377fe20dcSMark Fasheh 			break;
270477fe20dcSMark Fasheh 		unlock_extent(&BTRFS_I(inode)->io_tree, off, off + len - 1);
270577fe20dcSMark Fasheh 		if (ordered)
270677fe20dcSMark Fasheh 			btrfs_put_ordered_extent(ordered);
270777fe20dcSMark Fasheh 		btrfs_wait_ordered_range(inode, off, len);
270877fe20dcSMark Fasheh 	}
270977fe20dcSMark Fasheh }
271077fe20dcSMark Fasheh 
2711416161dbSMark Fasheh static void btrfs_double_unlock(struct inode *inode1, u64 loff1,
2712416161dbSMark Fasheh 				struct inode *inode2, u64 loff2, u64 len)
2713416161dbSMark Fasheh {
2714416161dbSMark Fasheh 	unlock_extent(&BTRFS_I(inode1)->io_tree, loff1, loff1 + len - 1);
2715416161dbSMark Fasheh 	unlock_extent(&BTRFS_I(inode2)->io_tree, loff2, loff2 + len - 1);
2716416161dbSMark Fasheh 
2717416161dbSMark Fasheh 	mutex_unlock(&inode1->i_mutex);
2718416161dbSMark Fasheh 	mutex_unlock(&inode2->i_mutex);
2719416161dbSMark Fasheh }
2720416161dbSMark Fasheh 
2721416161dbSMark Fasheh static void btrfs_double_lock(struct inode *inode1, u64 loff1,
2722416161dbSMark Fasheh 			      struct inode *inode2, u64 loff2, u64 len)
2723416161dbSMark Fasheh {
2724416161dbSMark Fasheh 	if (inode1 < inode2) {
2725416161dbSMark Fasheh 		swap(inode1, inode2);
2726416161dbSMark Fasheh 		swap(loff1, loff2);
2727416161dbSMark Fasheh 	}
2728416161dbSMark Fasheh 
2729416161dbSMark Fasheh 	mutex_lock_nested(&inode1->i_mutex, I_MUTEX_PARENT);
2730416161dbSMark Fasheh 	lock_extent_range(inode1, loff1, len);
2731416161dbSMark Fasheh 	if (inode1 != inode2) {
2732416161dbSMark Fasheh 		mutex_lock_nested(&inode2->i_mutex, I_MUTEX_CHILD);
2733416161dbSMark Fasheh 		lock_extent_range(inode2, loff2, len);
2734416161dbSMark Fasheh 	}
2735416161dbSMark Fasheh }
2736416161dbSMark Fasheh 
2737416161dbSMark Fasheh static int btrfs_cmp_data(struct inode *src, u64 loff, struct inode *dst,
2738416161dbSMark Fasheh 			  u64 dst_loff, u64 len)
2739416161dbSMark Fasheh {
2740416161dbSMark Fasheh 	int ret = 0;
2741416161dbSMark Fasheh 	struct page *src_page, *dst_page;
2742416161dbSMark Fasheh 	unsigned int cmp_len = PAGE_CACHE_SIZE;
2743416161dbSMark Fasheh 	void *addr, *dst_addr;
2744416161dbSMark Fasheh 
2745416161dbSMark Fasheh 	while (len) {
2746416161dbSMark Fasheh 		if (len < PAGE_CACHE_SIZE)
2747416161dbSMark Fasheh 			cmp_len = len;
2748416161dbSMark Fasheh 
2749416161dbSMark Fasheh 		src_page = extent_same_get_page(src, loff);
2750416161dbSMark Fasheh 		if (!src_page)
2751416161dbSMark Fasheh 			return -EINVAL;
2752416161dbSMark Fasheh 		dst_page = extent_same_get_page(dst, dst_loff);
2753416161dbSMark Fasheh 		if (!dst_page) {
2754416161dbSMark Fasheh 			page_cache_release(src_page);
2755416161dbSMark Fasheh 			return -EINVAL;
2756416161dbSMark Fasheh 		}
2757416161dbSMark Fasheh 		addr = kmap_atomic(src_page);
2758416161dbSMark Fasheh 		dst_addr = kmap_atomic(dst_page);
2759416161dbSMark Fasheh 
2760416161dbSMark Fasheh 		flush_dcache_page(src_page);
2761416161dbSMark Fasheh 		flush_dcache_page(dst_page);
2762416161dbSMark Fasheh 
2763416161dbSMark Fasheh 		if (memcmp(addr, dst_addr, cmp_len))
2764416161dbSMark Fasheh 			ret = BTRFS_SAME_DATA_DIFFERS;
2765416161dbSMark Fasheh 
2766416161dbSMark Fasheh 		kunmap_atomic(addr);
2767416161dbSMark Fasheh 		kunmap_atomic(dst_addr);
2768416161dbSMark Fasheh 		page_cache_release(src_page);
2769416161dbSMark Fasheh 		page_cache_release(dst_page);
2770416161dbSMark Fasheh 
2771416161dbSMark Fasheh 		if (ret)
2772416161dbSMark Fasheh 			break;
2773416161dbSMark Fasheh 
2774416161dbSMark Fasheh 		loff += cmp_len;
2775416161dbSMark Fasheh 		dst_loff += cmp_len;
2776416161dbSMark Fasheh 		len -= cmp_len;
2777416161dbSMark Fasheh 	}
2778416161dbSMark Fasheh 
2779416161dbSMark Fasheh 	return ret;
2780416161dbSMark Fasheh }
2781416161dbSMark Fasheh 
2782416161dbSMark Fasheh static int extent_same_check_offsets(struct inode *inode, u64 off, u64 len)
2783416161dbSMark Fasheh {
2784416161dbSMark Fasheh 	u64 bs = BTRFS_I(inode)->root->fs_info->sb->s_blocksize;
2785416161dbSMark Fasheh 
2786416161dbSMark Fasheh 	if (off + len > inode->i_size || off + len < off)
2787416161dbSMark Fasheh 		return -EINVAL;
2788416161dbSMark Fasheh 	/* Check that we are block aligned - btrfs_clone() requires this */
2789416161dbSMark Fasheh 	if (!IS_ALIGNED(off, bs) || !IS_ALIGNED(off + len, bs))
2790416161dbSMark Fasheh 		return -EINVAL;
2791416161dbSMark Fasheh 
2792416161dbSMark Fasheh 	return 0;
2793416161dbSMark Fasheh }
2794416161dbSMark Fasheh 
2795416161dbSMark Fasheh static int btrfs_extent_same(struct inode *src, u64 loff, u64 len,
2796416161dbSMark Fasheh 			     struct inode *dst, u64 dst_loff)
2797416161dbSMark Fasheh {
2798416161dbSMark Fasheh 	int ret;
2799416161dbSMark Fasheh 
2800416161dbSMark Fasheh 	/*
2801416161dbSMark Fasheh 	 * btrfs_clone() can't handle extents in the same file
2802416161dbSMark Fasheh 	 * yet. Once that works, we can drop this check and replace it
2803416161dbSMark Fasheh 	 * with a check for the same inode, but overlapping extents.
2804416161dbSMark Fasheh 	 */
2805416161dbSMark Fasheh 	if (src == dst)
2806416161dbSMark Fasheh 		return -EINVAL;
2807416161dbSMark Fasheh 
2808416161dbSMark Fasheh 	btrfs_double_lock(src, loff, dst, dst_loff, len);
2809416161dbSMark Fasheh 
2810416161dbSMark Fasheh 	ret = extent_same_check_offsets(src, loff, len);
2811416161dbSMark Fasheh 	if (ret)
2812416161dbSMark Fasheh 		goto out_unlock;
2813416161dbSMark Fasheh 
2814416161dbSMark Fasheh 	ret = extent_same_check_offsets(dst, dst_loff, len);
2815416161dbSMark Fasheh 	if (ret)
2816416161dbSMark Fasheh 		goto out_unlock;
2817416161dbSMark Fasheh 
2818416161dbSMark Fasheh 	/* don't make the dst file partly checksummed */
2819416161dbSMark Fasheh 	if ((BTRFS_I(src)->flags & BTRFS_INODE_NODATASUM) !=
2820416161dbSMark Fasheh 	    (BTRFS_I(dst)->flags & BTRFS_INODE_NODATASUM)) {
2821416161dbSMark Fasheh 		ret = -EINVAL;
2822416161dbSMark Fasheh 		goto out_unlock;
2823416161dbSMark Fasheh 	}
2824416161dbSMark Fasheh 
2825416161dbSMark Fasheh 	ret = btrfs_cmp_data(src, loff, dst, dst_loff, len);
2826416161dbSMark Fasheh 	if (ret == 0)
2827416161dbSMark Fasheh 		ret = btrfs_clone(src, dst, loff, len, len, dst_loff);
2828416161dbSMark Fasheh 
2829416161dbSMark Fasheh out_unlock:
2830416161dbSMark Fasheh 	btrfs_double_unlock(src, loff, dst, dst_loff, len);
2831416161dbSMark Fasheh 
2832416161dbSMark Fasheh 	return ret;
2833416161dbSMark Fasheh }
2834416161dbSMark Fasheh 
2835416161dbSMark Fasheh #define BTRFS_MAX_DEDUPE_LEN	(16 * 1024 * 1024)
2836416161dbSMark Fasheh 
2837416161dbSMark Fasheh static long btrfs_ioctl_file_extent_same(struct file *file,
28381c1c8747SAl Viro 			struct btrfs_ioctl_same_args __user *argp)
2839416161dbSMark Fasheh {
2840cbf8b8caSMark Fasheh 	struct btrfs_ioctl_same_args *same;
2841cbf8b8caSMark Fasheh 	struct btrfs_ioctl_same_extent_info *info;
28421c1c8747SAl Viro 	struct inode *src = file_inode(file);
2843416161dbSMark Fasheh 	u64 off;
2844416161dbSMark Fasheh 	u64 len;
2845416161dbSMark Fasheh 	int i;
2846416161dbSMark Fasheh 	int ret;
2847cbf8b8caSMark Fasheh 	unsigned long size;
2848416161dbSMark Fasheh 	u64 bs = BTRFS_I(src)->root->fs_info->sb->s_blocksize;
2849416161dbSMark Fasheh 	bool is_admin = capable(CAP_SYS_ADMIN);
28501c1c8747SAl Viro 	u16 count;
2851416161dbSMark Fasheh 
2852416161dbSMark Fasheh 	if (!(file->f_mode & FMODE_READ))
2853416161dbSMark Fasheh 		return -EINVAL;
2854416161dbSMark Fasheh 
2855416161dbSMark Fasheh 	ret = mnt_want_write_file(file);
2856416161dbSMark Fasheh 	if (ret)
2857416161dbSMark Fasheh 		return ret;
2858416161dbSMark Fasheh 
28591c1c8747SAl Viro 	if (get_user(count, &argp->dest_count)) {
2860416161dbSMark Fasheh 		ret = -EFAULT;
2861416161dbSMark Fasheh 		goto out;
2862416161dbSMark Fasheh 	}
2863416161dbSMark Fasheh 
28641c1c8747SAl Viro 	size = offsetof(struct btrfs_ioctl_same_args __user, info[count]);
2865cbf8b8caSMark Fasheh 
28661c1c8747SAl Viro 	same = memdup_user(argp, size);
2867cbf8b8caSMark Fasheh 
2868229eed43SGeyslan G. Bem 	if (IS_ERR(same)) {
2869229eed43SGeyslan G. Bem 		ret = PTR_ERR(same);
2870cbf8b8caSMark Fasheh 		goto out;
2871cbf8b8caSMark Fasheh 	}
2872cbf8b8caSMark Fasheh 
2873cbf8b8caSMark Fasheh 	off = same->logical_offset;
2874cbf8b8caSMark Fasheh 	len = same->length;
2875416161dbSMark Fasheh 
2876416161dbSMark Fasheh 	/*
2877416161dbSMark Fasheh 	 * Limit the total length we will dedupe for each operation.
2878416161dbSMark Fasheh 	 * This is intended to bound the total time spent in this
2879416161dbSMark Fasheh 	 * ioctl to something sane.
2880416161dbSMark Fasheh 	 */
2881416161dbSMark Fasheh 	if (len > BTRFS_MAX_DEDUPE_LEN)
2882416161dbSMark Fasheh 		len = BTRFS_MAX_DEDUPE_LEN;
2883416161dbSMark Fasheh 
2884416161dbSMark Fasheh 	if (WARN_ON_ONCE(bs < PAGE_CACHE_SIZE)) {
2885416161dbSMark Fasheh 		/*
2886416161dbSMark Fasheh 		 * Btrfs does not support blocksize < page_size. As a
2887416161dbSMark Fasheh 		 * result, btrfs_cmp_data() won't correctly handle
2888416161dbSMark Fasheh 		 * this situation without an update.
2889416161dbSMark Fasheh 		 */
2890416161dbSMark Fasheh 		ret = -EINVAL;
2891416161dbSMark Fasheh 		goto out;
2892416161dbSMark Fasheh 	}
2893416161dbSMark Fasheh 
2894416161dbSMark Fasheh 	ret = -EISDIR;
2895416161dbSMark Fasheh 	if (S_ISDIR(src->i_mode))
2896416161dbSMark Fasheh 		goto out;
2897416161dbSMark Fasheh 
2898416161dbSMark Fasheh 	ret = -EACCES;
2899416161dbSMark Fasheh 	if (!S_ISREG(src->i_mode))
2900416161dbSMark Fasheh 		goto out;
2901416161dbSMark Fasheh 
2902cbf8b8caSMark Fasheh 	/* pre-format output fields to sane values */
29031c1c8747SAl Viro 	for (i = 0; i < count; i++) {
2904cbf8b8caSMark Fasheh 		same->info[i].bytes_deduped = 0ULL;
2905cbf8b8caSMark Fasheh 		same->info[i].status = 0;
2906416161dbSMark Fasheh 	}
2907416161dbSMark Fasheh 
29081c1c8747SAl Viro 	for (i = 0, info = same->info; i < count; i++, info++) {
29091c1c8747SAl Viro 		struct inode *dst;
29101c1c8747SAl Viro 		struct fd dst_file = fdget(info->fd);
29111c1c8747SAl Viro 		if (!dst_file.file) {
2912cbf8b8caSMark Fasheh 			info->status = -EBADF;
29131c1c8747SAl Viro 			continue;
2914416161dbSMark Fasheh 		}
29151c1c8747SAl Viro 		dst = file_inode(dst_file.file);
2916416161dbSMark Fasheh 
29171c1c8747SAl Viro 		if (!(is_admin || (dst_file.file->f_mode & FMODE_WRITE))) {
2918cbf8b8caSMark Fasheh 			info->status = -EINVAL;
29191c1c8747SAl Viro 		} else if (file->f_path.mnt != dst_file.file->f_path.mnt) {
2920cbf8b8caSMark Fasheh 			info->status = -EXDEV;
29211c1c8747SAl Viro 		} else if (S_ISDIR(dst->i_mode)) {
2922cbf8b8caSMark Fasheh 			info->status = -EISDIR;
29231c1c8747SAl Viro 		} else if (!S_ISREG(dst->i_mode)) {
2924cbf8b8caSMark Fasheh 			info->status = -EACCES;
29251c1c8747SAl Viro 		} else {
2926cbf8b8caSMark Fasheh 			info->status = btrfs_extent_same(src, off, len, dst,
2927cbf8b8caSMark Fasheh 							info->logical_offset);
2928cbf8b8caSMark Fasheh 			if (info->status == 0)
2929cbf8b8caSMark Fasheh 				info->bytes_deduped += len;
29301c1c8747SAl Viro 		}
29311c1c8747SAl Viro 		fdput(dst_file);
2932cbf8b8caSMark Fasheh 	}
2933416161dbSMark Fasheh 
2934cbf8b8caSMark Fasheh 	ret = copy_to_user(argp, same, size);
2935cbf8b8caSMark Fasheh 	if (ret)
2936416161dbSMark Fasheh 		ret = -EFAULT;
2937416161dbSMark Fasheh 
2938416161dbSMark Fasheh out:
2939416161dbSMark Fasheh 	mnt_drop_write_file(file);
2940416161dbSMark Fasheh 	return ret;
2941416161dbSMark Fasheh }
2942416161dbSMark Fasheh 
294332b7c687SMark Fasheh /**
294432b7c687SMark Fasheh  * btrfs_clone() - clone a range from inode file to another
294532b7c687SMark Fasheh  *
294632b7c687SMark Fasheh  * @src: Inode to clone from
294732b7c687SMark Fasheh  * @inode: Inode to clone to
294832b7c687SMark Fasheh  * @off: Offset within source to start clone from
294932b7c687SMark Fasheh  * @olen: Original length, passed by user, of range to clone
295032b7c687SMark Fasheh  * @olen_aligned: Block-aligned value of olen, extent_same uses
295132b7c687SMark Fasheh  *               identical values here
295232b7c687SMark Fasheh  * @destoff: Offset within @inode to start clone
295332b7c687SMark Fasheh  */
295432b7c687SMark Fasheh static int btrfs_clone(struct inode *src, struct inode *inode,
295532b7c687SMark Fasheh 		       u64 off, u64 olen, u64 olen_aligned, u64 destoff)
2956f46b5a66SChristoph Hellwig {
2957f46b5a66SChristoph Hellwig 	struct btrfs_root *root = BTRFS_I(inode)->root;
295832b7c687SMark Fasheh 	struct btrfs_path *path = NULL;
2959f46b5a66SChristoph Hellwig 	struct extent_buffer *leaf;
296032b7c687SMark Fasheh 	struct btrfs_trans_handle *trans;
296132b7c687SMark Fasheh 	char *buf = NULL;
2962ae01a0abSYan Zheng 	struct btrfs_key key;
2963f46b5a66SChristoph Hellwig 	u32 nritems;
2964f46b5a66SChristoph Hellwig 	int slot;
2965ae01a0abSYan Zheng 	int ret;
296632b7c687SMark Fasheh 	u64 len = olen_aligned;
2967ae01a0abSYan Zheng 
2968ae01a0abSYan Zheng 	ret = -ENOMEM;
2969ae01a0abSYan Zheng 	buf = vmalloc(btrfs_level_size(root, 0));
2970ae01a0abSYan Zheng 	if (!buf)
297132b7c687SMark Fasheh 		return ret;
2972ae01a0abSYan Zheng 
2973ae01a0abSYan Zheng 	path = btrfs_alloc_path();
2974ae01a0abSYan Zheng 	if (!path) {
2975ae01a0abSYan Zheng 		vfree(buf);
297632b7c687SMark Fasheh 		return ret;
2977ae01a0abSYan Zheng 	}
297832b7c687SMark Fasheh 
2979ae01a0abSYan Zheng 	path->reada = 2;
2980c5c9cd4dSSage Weil 	/* clone data */
298133345d01SLi Zefan 	key.objectid = btrfs_ino(src);
2982ae01a0abSYan Zheng 	key.type = BTRFS_EXTENT_DATA_KEY;
2983ae01a0abSYan Zheng 	key.offset = 0;
2984f46b5a66SChristoph Hellwig 
2985f46b5a66SChristoph Hellwig 	while (1) {
2986f46b5a66SChristoph Hellwig 		/*
2987f46b5a66SChristoph Hellwig 		 * note the key will change type as we walk through the
2988f46b5a66SChristoph Hellwig 		 * tree.
2989f46b5a66SChristoph Hellwig 		 */
2990e4355f34SFilipe David Borba Manana 		path->leave_spinning = 1;
2991362a20c5SDavid Sterba 		ret = btrfs_search_slot(NULL, BTRFS_I(src)->root, &key, path,
2992362a20c5SDavid Sterba 				0, 0);
2993f46b5a66SChristoph Hellwig 		if (ret < 0)
2994f46b5a66SChristoph Hellwig 			goto out;
2995f46b5a66SChristoph Hellwig 
2996ae01a0abSYan Zheng 		nritems = btrfs_header_nritems(path->nodes[0]);
2997e4355f34SFilipe David Borba Manana process_slot:
2998ae01a0abSYan Zheng 		if (path->slots[0] >= nritems) {
2999362a20c5SDavid Sterba 			ret = btrfs_next_leaf(BTRFS_I(src)->root, path);
3000f46b5a66SChristoph Hellwig 			if (ret < 0)
3001f46b5a66SChristoph Hellwig 				goto out;
3002f46b5a66SChristoph Hellwig 			if (ret > 0)
3003f46b5a66SChristoph Hellwig 				break;
3004ae01a0abSYan Zheng 			nritems = btrfs_header_nritems(path->nodes[0]);
3005f46b5a66SChristoph Hellwig 		}
3006f46b5a66SChristoph Hellwig 		leaf = path->nodes[0];
3007f46b5a66SChristoph Hellwig 		slot = path->slots[0];
3008f46b5a66SChristoph Hellwig 
3009ae01a0abSYan Zheng 		btrfs_item_key_to_cpu(leaf, &key, slot);
3010d20f7043SChris Mason 		if (btrfs_key_type(&key) > BTRFS_EXTENT_DATA_KEY ||
301133345d01SLi Zefan 		    key.objectid != btrfs_ino(src))
3012f46b5a66SChristoph Hellwig 			break;
3013f46b5a66SChristoph Hellwig 
3014c5c9cd4dSSage Weil 		if (btrfs_key_type(&key) == BTRFS_EXTENT_DATA_KEY) {
3015c5c9cd4dSSage Weil 			struct btrfs_file_extent_item *extent;
3016c5c9cd4dSSage Weil 			int type;
301731840ae1SZheng Yan 			u32 size;
301831840ae1SZheng Yan 			struct btrfs_key new_key;
3019c5c9cd4dSSage Weil 			u64 disko = 0, diskl = 0;
3020c5c9cd4dSSage Weil 			u64 datao = 0, datal = 0;
3021c5c9cd4dSSage Weil 			u8 comp;
3022b5384d48SSage Weil 			u64 endoff;
302331840ae1SZheng Yan 
3024c5c9cd4dSSage Weil 			extent = btrfs_item_ptr(leaf, slot,
3025c5c9cd4dSSage Weil 						struct btrfs_file_extent_item);
3026c5c9cd4dSSage Weil 			comp = btrfs_file_extent_compression(leaf, extent);
3027c5c9cd4dSSage Weil 			type = btrfs_file_extent_type(leaf, extent);
3028c8a894d7SChris Mason 			if (type == BTRFS_FILE_EXTENT_REG ||
3029c8a894d7SChris Mason 			    type == BTRFS_FILE_EXTENT_PREALLOC) {
3030d397712bSChris Mason 				disko = btrfs_file_extent_disk_bytenr(leaf,
3031d397712bSChris Mason 								      extent);
3032d397712bSChris Mason 				diskl = btrfs_file_extent_disk_num_bytes(leaf,
3033d397712bSChris Mason 								 extent);
3034c5c9cd4dSSage Weil 				datao = btrfs_file_extent_offset(leaf, extent);
3035d397712bSChris Mason 				datal = btrfs_file_extent_num_bytes(leaf,
3036d397712bSChris Mason 								    extent);
3037c5c9cd4dSSage Weil 			} else if (type == BTRFS_FILE_EXTENT_INLINE) {
3038c5c9cd4dSSage Weil 				/* take upper bound, may be compressed */
3039c5c9cd4dSSage Weil 				datal = btrfs_file_extent_ram_bytes(leaf,
3040c5c9cd4dSSage Weil 								    extent);
3041c5c9cd4dSSage Weil 			}
304231840ae1SZheng Yan 
3043050006a7SSage Weil 			if (key.offset + datal <= off ||
3044e4355f34SFilipe David Borba Manana 			    key.offset >= off + len - 1) {
3045e4355f34SFilipe David Borba Manana 				path->slots[0]++;
3046e4355f34SFilipe David Borba Manana 				goto process_slot;
3047e4355f34SFilipe David Borba Manana 			}
3048e4355f34SFilipe David Borba Manana 
3049e4355f34SFilipe David Borba Manana 			size = btrfs_item_size_nr(leaf, slot);
3050e4355f34SFilipe David Borba Manana 			read_extent_buffer(leaf, buf,
3051e4355f34SFilipe David Borba Manana 					   btrfs_item_ptr_offset(leaf, slot),
3052e4355f34SFilipe David Borba Manana 					   size);
3053e4355f34SFilipe David Borba Manana 
3054e4355f34SFilipe David Borba Manana 			btrfs_release_path(path);
3055e4355f34SFilipe David Borba Manana 			path->leave_spinning = 0;
3056c5c9cd4dSSage Weil 
305731840ae1SZheng Yan 			memcpy(&new_key, &key, sizeof(new_key));
305833345d01SLi Zefan 			new_key.objectid = btrfs_ino(inode);
30594d728ec7SLi Zefan 			if (off <= key.offset)
3060c5c9cd4dSSage Weil 				new_key.offset = key.offset + destoff - off;
30614d728ec7SLi Zefan 			else
30624d728ec7SLi Zefan 				new_key.offset = destoff;
3063c5c9cd4dSSage Weil 
3064b6f3409bSSage Weil 			/*
3065b6f3409bSSage Weil 			 * 1 - adjusting old extent (we may have to split it)
3066b6f3409bSSage Weil 			 * 1 - add new extent
3067b6f3409bSSage Weil 			 * 1 - inode update
3068b6f3409bSSage Weil 			 */
3069b6f3409bSSage Weil 			trans = btrfs_start_transaction(root, 3);
3070a22285a6SYan, Zheng 			if (IS_ERR(trans)) {
3071a22285a6SYan, Zheng 				ret = PTR_ERR(trans);
3072a22285a6SYan, Zheng 				goto out;
3073a22285a6SYan, Zheng 			}
3074a22285a6SYan, Zheng 
3075c8a894d7SChris Mason 			if (type == BTRFS_FILE_EXTENT_REG ||
3076c8a894d7SChris Mason 			    type == BTRFS_FILE_EXTENT_PREALLOC) {
3077d72c0842SLi Zefan 				/*
3078d72c0842SLi Zefan 				 *    a  | --- range to clone ---|  b
3079d72c0842SLi Zefan 				 * | ------------- extent ------------- |
3080d72c0842SLi Zefan 				 */
3081d72c0842SLi Zefan 
3082d72c0842SLi Zefan 				/* substract range b */
3083d72c0842SLi Zefan 				if (key.offset + datal > off + len)
3084d72c0842SLi Zefan 					datal = off + len - key.offset;
3085d72c0842SLi Zefan 
3086d72c0842SLi Zefan 				/* substract range a */
3087a22285a6SYan, Zheng 				if (off > key.offset) {
3088a22285a6SYan, Zheng 					datao += off - key.offset;
3089a22285a6SYan, Zheng 					datal -= off - key.offset;
3090a22285a6SYan, Zheng 				}
3091a22285a6SYan, Zheng 
30925dc562c5SJosef Bacik 				ret = btrfs_drop_extents(trans, root, inode,
3093a22285a6SYan, Zheng 							 new_key.offset,
3094a22285a6SYan, Zheng 							 new_key.offset + datal,
30952671485dSJosef Bacik 							 1);
309679787eaaSJeff Mahoney 				if (ret) {
30973f9e3df8SDavid Sterba 					if (ret != -EOPNOTSUPP)
309800fdf13aSLiu Bo 						btrfs_abort_transaction(trans,
309900fdf13aSLiu Bo 								root, ret);
310079787eaaSJeff Mahoney 					btrfs_end_transaction(trans, root);
310179787eaaSJeff Mahoney 					goto out;
310279787eaaSJeff Mahoney 				}
3103a22285a6SYan, Zheng 
310431840ae1SZheng Yan 				ret = btrfs_insert_empty_item(trans, root, path,
310531840ae1SZheng Yan 							      &new_key, size);
310679787eaaSJeff Mahoney 				if (ret) {
310779787eaaSJeff Mahoney 					btrfs_abort_transaction(trans, root,
310879787eaaSJeff Mahoney 								ret);
310979787eaaSJeff Mahoney 					btrfs_end_transaction(trans, root);
311079787eaaSJeff Mahoney 					goto out;
311179787eaaSJeff Mahoney 				}
311231840ae1SZheng Yan 
311331840ae1SZheng Yan 				leaf = path->nodes[0];
311431840ae1SZheng Yan 				slot = path->slots[0];
311531840ae1SZheng Yan 				write_extent_buffer(leaf, buf,
311631840ae1SZheng Yan 					    btrfs_item_ptr_offset(leaf, slot),
311731840ae1SZheng Yan 					    size);
3118ae01a0abSYan Zheng 
3119f46b5a66SChristoph Hellwig 				extent = btrfs_item_ptr(leaf, slot,
3120f46b5a66SChristoph Hellwig 						struct btrfs_file_extent_item);
3121c5c9cd4dSSage Weil 
3122c5c9cd4dSSage Weil 				/* disko == 0 means it's a hole */
3123c5c9cd4dSSage Weil 				if (!disko)
3124c5c9cd4dSSage Weil 					datao = 0;
3125c5c9cd4dSSage Weil 
3126c5c9cd4dSSage Weil 				btrfs_set_file_extent_offset(leaf, extent,
3127c5c9cd4dSSage Weil 							     datao);
3128c5c9cd4dSSage Weil 				btrfs_set_file_extent_num_bytes(leaf, extent,
3129c5c9cd4dSSage Weil 								datal);
3130c5c9cd4dSSage Weil 				if (disko) {
3131c5c9cd4dSSage Weil 					inode_add_bytes(inode, datal);
3132ae01a0abSYan Zheng 					ret = btrfs_inc_extent_ref(trans, root,
31335d4f98a2SYan Zheng 							disko, diskl, 0,
3134f46b5a66SChristoph Hellwig 							root->root_key.objectid,
313533345d01SLi Zefan 							btrfs_ino(inode),
313666d7e7f0SArne Jansen 							new_key.offset - datao,
313766d7e7f0SArne Jansen 							0);
313879787eaaSJeff Mahoney 					if (ret) {
313979787eaaSJeff Mahoney 						btrfs_abort_transaction(trans,
314079787eaaSJeff Mahoney 									root,
314179787eaaSJeff Mahoney 									ret);
314279787eaaSJeff Mahoney 						btrfs_end_transaction(trans,
314379787eaaSJeff Mahoney 								      root);
314479787eaaSJeff Mahoney 						goto out;
314579787eaaSJeff Mahoney 
314679787eaaSJeff Mahoney 					}
3147f46b5a66SChristoph Hellwig 				}
3148c5c9cd4dSSage Weil 			} else if (type == BTRFS_FILE_EXTENT_INLINE) {
3149c5c9cd4dSSage Weil 				u64 skip = 0;
3150c5c9cd4dSSage Weil 				u64 trim = 0;
3151d3ecfcdfSLiu Bo 				u64 aligned_end = 0;
3152d3ecfcdfSLiu Bo 
3153c5c9cd4dSSage Weil 				if (off > key.offset) {
3154c5c9cd4dSSage Weil 					skip = off - key.offset;
3155c5c9cd4dSSage Weil 					new_key.offset += skip;
315631840ae1SZheng Yan 				}
3157d397712bSChris Mason 
3158c5c9cd4dSSage Weil 				if (key.offset + datal > off + len)
3159c5c9cd4dSSage Weil 					trim = key.offset + datal - (off + len);
3160d397712bSChris Mason 
3161c5c9cd4dSSage Weil 				if (comp && (skip || trim)) {
3162c5c9cd4dSSage Weil 					ret = -EINVAL;
3163a22285a6SYan, Zheng 					btrfs_end_transaction(trans, root);
3164c5c9cd4dSSage Weil 					goto out;
316531840ae1SZheng Yan 				}
3166c5c9cd4dSSage Weil 				size -= skip + trim;
3167c5c9cd4dSSage Weil 				datal -= skip + trim;
3168a22285a6SYan, Zheng 
3169d3ecfcdfSLiu Bo 				aligned_end = ALIGN(new_key.offset + datal,
3170d3ecfcdfSLiu Bo 						    root->sectorsize);
31715dc562c5SJosef Bacik 				ret = btrfs_drop_extents(trans, root, inode,
3172a22285a6SYan, Zheng 							 new_key.offset,
3173d3ecfcdfSLiu Bo 							 aligned_end,
31742671485dSJosef Bacik 							 1);
317579787eaaSJeff Mahoney 				if (ret) {
31763f9e3df8SDavid Sterba 					if (ret != -EOPNOTSUPP)
31773a29bc09SChris Mason 						btrfs_abort_transaction(trans,
31783a29bc09SChris Mason 							root, ret);
317979787eaaSJeff Mahoney 					btrfs_end_transaction(trans, root);
318079787eaaSJeff Mahoney 					goto out;
318179787eaaSJeff Mahoney 				}
3182a22285a6SYan, Zheng 
3183c5c9cd4dSSage Weil 				ret = btrfs_insert_empty_item(trans, root, path,
3184c5c9cd4dSSage Weil 							      &new_key, size);
318579787eaaSJeff Mahoney 				if (ret) {
318679787eaaSJeff Mahoney 					btrfs_abort_transaction(trans, root,
318779787eaaSJeff Mahoney 								ret);
318879787eaaSJeff Mahoney 					btrfs_end_transaction(trans, root);
318979787eaaSJeff Mahoney 					goto out;
319079787eaaSJeff Mahoney 				}
3191c5c9cd4dSSage Weil 
3192c5c9cd4dSSage Weil 				if (skip) {
3193d397712bSChris Mason 					u32 start =
3194d397712bSChris Mason 					  btrfs_file_extent_calc_inline_size(0);
3195c5c9cd4dSSage Weil 					memmove(buf+start, buf+start+skip,
3196c5c9cd4dSSage Weil 						datal);
3197c5c9cd4dSSage Weil 				}
3198c5c9cd4dSSage Weil 
3199c5c9cd4dSSage Weil 				leaf = path->nodes[0];
3200c5c9cd4dSSage Weil 				slot = path->slots[0];
3201c5c9cd4dSSage Weil 				write_extent_buffer(leaf, buf,
3202c5c9cd4dSSage Weil 					    btrfs_item_ptr_offset(leaf, slot),
3203c5c9cd4dSSage Weil 					    size);
3204c5c9cd4dSSage Weil 				inode_add_bytes(inode, datal);
3205c5c9cd4dSSage Weil 			}
3206c5c9cd4dSSage Weil 
3207c5c9cd4dSSage Weil 			btrfs_mark_buffer_dirty(leaf);
3208b3b4aa74SDavid Sterba 			btrfs_release_path(path);
3209c5c9cd4dSSage Weil 
32100c4d2d95SJosef Bacik 			inode_inc_iversion(inode);
3211a22285a6SYan, Zheng 			inode->i_mtime = inode->i_ctime = CURRENT_TIME;
3212b5384d48SSage Weil 
3213b5384d48SSage Weil 			/*
3214b5384d48SSage Weil 			 * we round up to the block size at eof when
3215b5384d48SSage Weil 			 * determining which extents to clone above,
3216b5384d48SSage Weil 			 * but shouldn't round up the file size
3217b5384d48SSage Weil 			 */
3218b5384d48SSage Weil 			endoff = new_key.offset + datal;
32195f3888ffSLi Zefan 			if (endoff > destoff+olen)
32205f3888ffSLi Zefan 				endoff = destoff+olen;
3221b5384d48SSage Weil 			if (endoff > inode->i_size)
3222b5384d48SSage Weil 				btrfs_i_size_write(inode, endoff);
3223b5384d48SSage Weil 
3224a22285a6SYan, Zheng 			ret = btrfs_update_inode(trans, root, inode);
322579787eaaSJeff Mahoney 			if (ret) {
322679787eaaSJeff Mahoney 				btrfs_abort_transaction(trans, root, ret);
3227a22285a6SYan, Zheng 				btrfs_end_transaction(trans, root);
322879787eaaSJeff Mahoney 				goto out;
322979787eaaSJeff Mahoney 			}
323079787eaaSJeff Mahoney 			ret = btrfs_end_transaction(trans, root);
3231a22285a6SYan, Zheng 		}
3232b3b4aa74SDavid Sterba 		btrfs_release_path(path);
3233ae01a0abSYan Zheng 		key.offset++;
3234ae01a0abSYan Zheng 	}
3235f46b5a66SChristoph Hellwig 	ret = 0;
323632b7c687SMark Fasheh 
3237f46b5a66SChristoph Hellwig out:
3238b3b4aa74SDavid Sterba 	btrfs_release_path(path);
323932b7c687SMark Fasheh 	btrfs_free_path(path);
324032b7c687SMark Fasheh 	vfree(buf);
324132b7c687SMark Fasheh 	return ret;
324232b7c687SMark Fasheh }
324332b7c687SMark Fasheh 
324432b7c687SMark Fasheh static noinline long btrfs_ioctl_clone(struct file *file, unsigned long srcfd,
324532b7c687SMark Fasheh 				       u64 off, u64 olen, u64 destoff)
324632b7c687SMark Fasheh {
324754563d41SAl Viro 	struct inode *inode = file_inode(file);
324832b7c687SMark Fasheh 	struct btrfs_root *root = BTRFS_I(inode)->root;
324932b7c687SMark Fasheh 	struct fd src_file;
325032b7c687SMark Fasheh 	struct inode *src;
325132b7c687SMark Fasheh 	int ret;
325232b7c687SMark Fasheh 	u64 len = olen;
325332b7c687SMark Fasheh 	u64 bs = root->fs_info->sb->s_blocksize;
325432b7c687SMark Fasheh 	int same_inode = 0;
325532b7c687SMark Fasheh 
325632b7c687SMark Fasheh 	/*
325732b7c687SMark Fasheh 	 * TODO:
325832b7c687SMark Fasheh 	 * - split compressed inline extents.  annoying: we need to
325932b7c687SMark Fasheh 	 *   decompress into destination's address_space (the file offset
326032b7c687SMark Fasheh 	 *   may change, so source mapping won't do), then recompress (or
326132b7c687SMark Fasheh 	 *   otherwise reinsert) a subrange.
326200fdf13aSLiu Bo 	 *
326300fdf13aSLiu Bo 	 * - split destination inode's inline extents.  The inline extents can
326400fdf13aSLiu Bo 	 *   be either compressed or non-compressed.
326532b7c687SMark Fasheh 	 */
326632b7c687SMark Fasheh 
326732b7c687SMark Fasheh 	/* the destination must be opened for writing */
326832b7c687SMark Fasheh 	if (!(file->f_mode & FMODE_WRITE) || (file->f_flags & O_APPEND))
326932b7c687SMark Fasheh 		return -EINVAL;
327032b7c687SMark Fasheh 
327132b7c687SMark Fasheh 	if (btrfs_root_readonly(root))
327232b7c687SMark Fasheh 		return -EROFS;
327332b7c687SMark Fasheh 
327432b7c687SMark Fasheh 	ret = mnt_want_write_file(file);
327532b7c687SMark Fasheh 	if (ret)
327632b7c687SMark Fasheh 		return ret;
327732b7c687SMark Fasheh 
327832b7c687SMark Fasheh 	src_file = fdget(srcfd);
327932b7c687SMark Fasheh 	if (!src_file.file) {
328032b7c687SMark Fasheh 		ret = -EBADF;
328132b7c687SMark Fasheh 		goto out_drop_write;
328232b7c687SMark Fasheh 	}
328332b7c687SMark Fasheh 
328432b7c687SMark Fasheh 	ret = -EXDEV;
328532b7c687SMark Fasheh 	if (src_file.file->f_path.mnt != file->f_path.mnt)
328632b7c687SMark Fasheh 		goto out_fput;
328732b7c687SMark Fasheh 
328832b7c687SMark Fasheh 	src = file_inode(src_file.file);
328932b7c687SMark Fasheh 
329032b7c687SMark Fasheh 	ret = -EINVAL;
329132b7c687SMark Fasheh 	if (src == inode)
329232b7c687SMark Fasheh 		same_inode = 1;
329332b7c687SMark Fasheh 
329432b7c687SMark Fasheh 	/* the src must be open for reading */
329532b7c687SMark Fasheh 	if (!(src_file.file->f_mode & FMODE_READ))
329632b7c687SMark Fasheh 		goto out_fput;
329732b7c687SMark Fasheh 
329832b7c687SMark Fasheh 	/* don't make the dst file partly checksummed */
329932b7c687SMark Fasheh 	if ((BTRFS_I(src)->flags & BTRFS_INODE_NODATASUM) !=
330032b7c687SMark Fasheh 	    (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM))
330132b7c687SMark Fasheh 		goto out_fput;
330232b7c687SMark Fasheh 
330332b7c687SMark Fasheh 	ret = -EISDIR;
330432b7c687SMark Fasheh 	if (S_ISDIR(src->i_mode) || S_ISDIR(inode->i_mode))
330532b7c687SMark Fasheh 		goto out_fput;
330632b7c687SMark Fasheh 
330732b7c687SMark Fasheh 	ret = -EXDEV;
330832b7c687SMark Fasheh 	if (src->i_sb != inode->i_sb)
330932b7c687SMark Fasheh 		goto out_fput;
331032b7c687SMark Fasheh 
331132b7c687SMark Fasheh 	if (!same_inode) {
331232b7c687SMark Fasheh 		if (inode < src) {
331332b7c687SMark Fasheh 			mutex_lock_nested(&inode->i_mutex, I_MUTEX_PARENT);
331432b7c687SMark Fasheh 			mutex_lock_nested(&src->i_mutex, I_MUTEX_CHILD);
331532b7c687SMark Fasheh 		} else {
331632b7c687SMark Fasheh 			mutex_lock_nested(&src->i_mutex, I_MUTEX_PARENT);
331732b7c687SMark Fasheh 			mutex_lock_nested(&inode->i_mutex, I_MUTEX_CHILD);
331832b7c687SMark Fasheh 		}
331932b7c687SMark Fasheh 	} else {
332032b7c687SMark Fasheh 		mutex_lock(&src->i_mutex);
332132b7c687SMark Fasheh 	}
332232b7c687SMark Fasheh 
332332b7c687SMark Fasheh 	/* determine range to clone */
332432b7c687SMark Fasheh 	ret = -EINVAL;
332532b7c687SMark Fasheh 	if (off + len > src->i_size || off + len < off)
332632b7c687SMark Fasheh 		goto out_unlock;
332732b7c687SMark Fasheh 	if (len == 0)
332832b7c687SMark Fasheh 		olen = len = src->i_size - off;
332932b7c687SMark Fasheh 	/* if we extend to eof, continue to block boundary */
333032b7c687SMark Fasheh 	if (off + len == src->i_size)
333132b7c687SMark Fasheh 		len = ALIGN(src->i_size, bs) - off;
333232b7c687SMark Fasheh 
333332b7c687SMark Fasheh 	/* verify the end result is block aligned */
333432b7c687SMark Fasheh 	if (!IS_ALIGNED(off, bs) || !IS_ALIGNED(off + len, bs) ||
333532b7c687SMark Fasheh 	    !IS_ALIGNED(destoff, bs))
333632b7c687SMark Fasheh 		goto out_unlock;
333732b7c687SMark Fasheh 
333832b7c687SMark Fasheh 	/* verify if ranges are overlapped within the same file */
333932b7c687SMark Fasheh 	if (same_inode) {
334032b7c687SMark Fasheh 		if (destoff + len > off && destoff < off + len)
334132b7c687SMark Fasheh 			goto out_unlock;
334232b7c687SMark Fasheh 	}
334332b7c687SMark Fasheh 
334432b7c687SMark Fasheh 	if (destoff > inode->i_size) {
334532b7c687SMark Fasheh 		ret = btrfs_cont_expand(inode, inode->i_size, destoff);
334632b7c687SMark Fasheh 		if (ret)
334732b7c687SMark Fasheh 			goto out_unlock;
334832b7c687SMark Fasheh 	}
334932b7c687SMark Fasheh 
335032b7c687SMark Fasheh 	/* truncate page cache pages from target inode range */
335132b7c687SMark Fasheh 	truncate_inode_pages_range(&inode->i_data, destoff,
335232b7c687SMark Fasheh 				   PAGE_CACHE_ALIGN(destoff + len) - 1);
335332b7c687SMark Fasheh 
335432b7c687SMark Fasheh 	lock_extent_range(src, off, len);
335532b7c687SMark Fasheh 
335632b7c687SMark Fasheh 	ret = btrfs_clone(src, inode, off, olen, len, destoff);
335732b7c687SMark Fasheh 
3358aa42ffd9SLiu Bo 	unlock_extent(&BTRFS_I(src)->io_tree, off, off + len - 1);
3359f46b5a66SChristoph Hellwig out_unlock:
3360c57c2b3eSFilipe David Borba Manana 	if (!same_inode) {
3361c57c2b3eSFilipe David Borba Manana 		if (inode < src) {
3362f46b5a66SChristoph Hellwig 			mutex_unlock(&src->i_mutex);
3363f46b5a66SChristoph Hellwig 			mutex_unlock(&inode->i_mutex);
3364c57c2b3eSFilipe David Borba Manana 		} else {
3365c57c2b3eSFilipe David Borba Manana 			mutex_unlock(&inode->i_mutex);
3366c57c2b3eSFilipe David Borba Manana 			mutex_unlock(&src->i_mutex);
3367c57c2b3eSFilipe David Borba Manana 		}
3368c57c2b3eSFilipe David Borba Manana 	} else {
3369c57c2b3eSFilipe David Borba Manana 		mutex_unlock(&src->i_mutex);
3370c57c2b3eSFilipe David Borba Manana 	}
3371f46b5a66SChristoph Hellwig out_fput:
33722903ff01SAl Viro 	fdput(src_file);
3373ab67b7c1SYan Zheng out_drop_write:
33742a79f17eSAl Viro 	mnt_drop_write_file(file);
3375f46b5a66SChristoph Hellwig 	return ret;
3376f46b5a66SChristoph Hellwig }
3377f46b5a66SChristoph Hellwig 
33787a865e8aSChristoph Hellwig static long btrfs_ioctl_clone_range(struct file *file, void __user *argp)
3379c5c9cd4dSSage Weil {
3380c5c9cd4dSSage Weil 	struct btrfs_ioctl_clone_range_args args;
3381c5c9cd4dSSage Weil 
33827a865e8aSChristoph Hellwig 	if (copy_from_user(&args, argp, sizeof(args)))
3383c5c9cd4dSSage Weil 		return -EFAULT;
3384c5c9cd4dSSage Weil 	return btrfs_ioctl_clone(file, args.src_fd, args.src_offset,
3385c5c9cd4dSSage Weil 				 args.src_length, args.dest_offset);
3386c5c9cd4dSSage Weil }
3387c5c9cd4dSSage Weil 
3388f46b5a66SChristoph Hellwig /*
3389f46b5a66SChristoph Hellwig  * there are many ways the trans_start and trans_end ioctls can lead
3390f46b5a66SChristoph Hellwig  * to deadlocks.  They should only be used by applications that
3391f46b5a66SChristoph Hellwig  * basically own the machine, and have a very in depth understanding
3392f46b5a66SChristoph Hellwig  * of all the possible deadlocks and enospc problems.
3393f46b5a66SChristoph Hellwig  */
3394b2950863SChristoph Hellwig static long btrfs_ioctl_trans_start(struct file *file)
3395f46b5a66SChristoph Hellwig {
3396496ad9aaSAl Viro 	struct inode *inode = file_inode(file);
3397f46b5a66SChristoph Hellwig 	struct btrfs_root *root = BTRFS_I(inode)->root;
3398f46b5a66SChristoph Hellwig 	struct btrfs_trans_handle *trans;
33991ab86aedSSage Weil 	int ret;
3400f46b5a66SChristoph Hellwig 
34011ab86aedSSage Weil 	ret = -EPERM;
3402df5b5520SChristoph Hellwig 	if (!capable(CAP_SYS_ADMIN))
3403f46b5a66SChristoph Hellwig 		goto out;
34041ab86aedSSage Weil 
34051ab86aedSSage Weil 	ret = -EINPROGRESS;
34061ab86aedSSage Weil 	if (file->private_data)
34071ab86aedSSage Weil 		goto out;
34089ca9ee09SSage Weil 
3409b83cc969SLi Zefan 	ret = -EROFS;
3410b83cc969SLi Zefan 	if (btrfs_root_readonly(root))
3411b83cc969SLi Zefan 		goto out;
3412b83cc969SLi Zefan 
3413a561be71SAl Viro 	ret = mnt_want_write_file(file);
3414c146afadSYan Zheng 	if (ret)
3415c146afadSYan Zheng 		goto out;
3416c146afadSYan Zheng 
3417a4abeea4SJosef Bacik 	atomic_inc(&root->fs_info->open_ioctl_trans);
34189ca9ee09SSage Weil 
3419f46b5a66SChristoph Hellwig 	ret = -ENOMEM;
34207a7eaa40SJosef Bacik 	trans = btrfs_start_ioctl_transaction(root);
3421abd30bb0STsutomu Itoh 	if (IS_ERR(trans))
34221ab86aedSSage Weil 		goto out_drop;
34231ab86aedSSage Weil 
34241ab86aedSSage Weil 	file->private_data = trans;
34251ab86aedSSage Weil 	return 0;
34261ab86aedSSage Weil 
34271ab86aedSSage Weil out_drop:
3428a4abeea4SJosef Bacik 	atomic_dec(&root->fs_info->open_ioctl_trans);
34292a79f17eSAl Viro 	mnt_drop_write_file(file);
3430f46b5a66SChristoph Hellwig out:
3431f46b5a66SChristoph Hellwig 	return ret;
3432f46b5a66SChristoph Hellwig }
3433f46b5a66SChristoph Hellwig 
34346ef5ed0dSJosef Bacik static long btrfs_ioctl_default_subvol(struct file *file, void __user *argp)
34356ef5ed0dSJosef Bacik {
3436496ad9aaSAl Viro 	struct inode *inode = file_inode(file);
34376ef5ed0dSJosef Bacik 	struct btrfs_root *root = BTRFS_I(inode)->root;
34386ef5ed0dSJosef Bacik 	struct btrfs_root *new_root;
34396ef5ed0dSJosef Bacik 	struct btrfs_dir_item *di;
34406ef5ed0dSJosef Bacik 	struct btrfs_trans_handle *trans;
34416ef5ed0dSJosef Bacik 	struct btrfs_path *path;
34426ef5ed0dSJosef Bacik 	struct btrfs_key location;
34436ef5ed0dSJosef Bacik 	struct btrfs_disk_key disk_key;
34446ef5ed0dSJosef Bacik 	u64 objectid = 0;
34456ef5ed0dSJosef Bacik 	u64 dir_id;
34463c04ce01SMiao Xie 	int ret;
34476ef5ed0dSJosef Bacik 
34486ef5ed0dSJosef Bacik 	if (!capable(CAP_SYS_ADMIN))
34496ef5ed0dSJosef Bacik 		return -EPERM;
34506ef5ed0dSJosef Bacik 
34513c04ce01SMiao Xie 	ret = mnt_want_write_file(file);
34523c04ce01SMiao Xie 	if (ret)
34533c04ce01SMiao Xie 		return ret;
34543c04ce01SMiao Xie 
34553c04ce01SMiao Xie 	if (copy_from_user(&objectid, argp, sizeof(objectid))) {
34563c04ce01SMiao Xie 		ret = -EFAULT;
34573c04ce01SMiao Xie 		goto out;
34583c04ce01SMiao Xie 	}
34596ef5ed0dSJosef Bacik 
34606ef5ed0dSJosef Bacik 	if (!objectid)
34611cecf579Schandan 		objectid = BTRFS_FS_TREE_OBJECTID;
34626ef5ed0dSJosef Bacik 
34636ef5ed0dSJosef Bacik 	location.objectid = objectid;
34646ef5ed0dSJosef Bacik 	location.type = BTRFS_ROOT_ITEM_KEY;
34656ef5ed0dSJosef Bacik 	location.offset = (u64)-1;
34666ef5ed0dSJosef Bacik 
34676ef5ed0dSJosef Bacik 	new_root = btrfs_read_fs_root_no_name(root->fs_info, &location);
34683c04ce01SMiao Xie 	if (IS_ERR(new_root)) {
34693c04ce01SMiao Xie 		ret = PTR_ERR(new_root);
34703c04ce01SMiao Xie 		goto out;
34713c04ce01SMiao Xie 	}
34726ef5ed0dSJosef Bacik 
34736ef5ed0dSJosef Bacik 	path = btrfs_alloc_path();
34743c04ce01SMiao Xie 	if (!path) {
34753c04ce01SMiao Xie 		ret = -ENOMEM;
34763c04ce01SMiao Xie 		goto out;
34773c04ce01SMiao Xie 	}
34786ef5ed0dSJosef Bacik 	path->leave_spinning = 1;
34796ef5ed0dSJosef Bacik 
34806ef5ed0dSJosef Bacik 	trans = btrfs_start_transaction(root, 1);
348198d5dc13STsutomu Itoh 	if (IS_ERR(trans)) {
34826ef5ed0dSJosef Bacik 		btrfs_free_path(path);
34833c04ce01SMiao Xie 		ret = PTR_ERR(trans);
34843c04ce01SMiao Xie 		goto out;
34856ef5ed0dSJosef Bacik 	}
34866ef5ed0dSJosef Bacik 
34876c41761fSDavid Sterba 	dir_id = btrfs_super_root_dir(root->fs_info->super_copy);
34886ef5ed0dSJosef Bacik 	di = btrfs_lookup_dir_item(trans, root->fs_info->tree_root, path,
34896ef5ed0dSJosef Bacik 				   dir_id, "default", 7, 1);
3490cf1e99a4SDan Carpenter 	if (IS_ERR_OR_NULL(di)) {
34916ef5ed0dSJosef Bacik 		btrfs_free_path(path);
34926ef5ed0dSJosef Bacik 		btrfs_end_transaction(trans, root);
3493efe120a0SFrank Holton 		btrfs_err(new_root->fs_info, "Umm, you don't have the default dir"
3494efe120a0SFrank Holton 			   "item, this isn't going to work");
34953c04ce01SMiao Xie 		ret = -ENOENT;
34963c04ce01SMiao Xie 		goto out;
34976ef5ed0dSJosef Bacik 	}
34986ef5ed0dSJosef Bacik 
34996ef5ed0dSJosef Bacik 	btrfs_cpu_key_to_disk(&disk_key, &new_root->root_key);
35006ef5ed0dSJosef Bacik 	btrfs_set_dir_item_key(path->nodes[0], di, &disk_key);
35016ef5ed0dSJosef Bacik 	btrfs_mark_buffer_dirty(path->nodes[0]);
35026ef5ed0dSJosef Bacik 	btrfs_free_path(path);
35036ef5ed0dSJosef Bacik 
35042b0ce2c2SMitch Harder 	btrfs_set_fs_incompat(root->fs_info, DEFAULT_SUBVOL);
35056ef5ed0dSJosef Bacik 	btrfs_end_transaction(trans, root);
35063c04ce01SMiao Xie out:
35073c04ce01SMiao Xie 	mnt_drop_write_file(file);
35083c04ce01SMiao Xie 	return ret;
35096ef5ed0dSJosef Bacik }
35106ef5ed0dSJosef Bacik 
35115af3e8ccSStefan Behrens void btrfs_get_block_group_info(struct list_head *groups_list,
3512bf5fc093SJosef Bacik 				struct btrfs_ioctl_space_info *space)
3513bf5fc093SJosef Bacik {
3514bf5fc093SJosef Bacik 	struct btrfs_block_group_cache *block_group;
3515bf5fc093SJosef Bacik 
3516bf5fc093SJosef Bacik 	space->total_bytes = 0;
3517bf5fc093SJosef Bacik 	space->used_bytes = 0;
3518bf5fc093SJosef Bacik 	space->flags = 0;
3519bf5fc093SJosef Bacik 	list_for_each_entry(block_group, groups_list, list) {
3520bf5fc093SJosef Bacik 		space->flags = block_group->flags;
3521bf5fc093SJosef Bacik 		space->total_bytes += block_group->key.offset;
3522bf5fc093SJosef Bacik 		space->used_bytes +=
3523bf5fc093SJosef Bacik 			btrfs_block_group_used(&block_group->item);
3524bf5fc093SJosef Bacik 	}
3525bf5fc093SJosef Bacik }
3526bf5fc093SJosef Bacik 
352748a3b636SEric Sandeen static long btrfs_ioctl_space_info(struct btrfs_root *root, void __user *arg)
35281406e432SJosef Bacik {
35291406e432SJosef Bacik 	struct btrfs_ioctl_space_args space_args;
35301406e432SJosef Bacik 	struct btrfs_ioctl_space_info space;
35311406e432SJosef Bacik 	struct btrfs_ioctl_space_info *dest;
35327fde62bfSChris Mason 	struct btrfs_ioctl_space_info *dest_orig;
353313f2696fSDaniel J Blueman 	struct btrfs_ioctl_space_info __user *user_dest;
35341406e432SJosef Bacik 	struct btrfs_space_info *info;
3535bf5fc093SJosef Bacik 	u64 types[] = {BTRFS_BLOCK_GROUP_DATA,
3536bf5fc093SJosef Bacik 		       BTRFS_BLOCK_GROUP_SYSTEM,
3537bf5fc093SJosef Bacik 		       BTRFS_BLOCK_GROUP_METADATA,
3538bf5fc093SJosef Bacik 		       BTRFS_BLOCK_GROUP_DATA | BTRFS_BLOCK_GROUP_METADATA};
3539bf5fc093SJosef Bacik 	int num_types = 4;
35407fde62bfSChris Mason 	int alloc_size;
35411406e432SJosef Bacik 	int ret = 0;
354251788b1bSDan Rosenberg 	u64 slot_count = 0;
3543bf5fc093SJosef Bacik 	int i, c;
35441406e432SJosef Bacik 
35451406e432SJosef Bacik 	if (copy_from_user(&space_args,
35461406e432SJosef Bacik 			   (struct btrfs_ioctl_space_args __user *)arg,
35471406e432SJosef Bacik 			   sizeof(space_args)))
35481406e432SJosef Bacik 		return -EFAULT;
35491406e432SJosef Bacik 
3550bf5fc093SJosef Bacik 	for (i = 0; i < num_types; i++) {
3551bf5fc093SJosef Bacik 		struct btrfs_space_info *tmp;
3552bf5fc093SJosef Bacik 
3553bf5fc093SJosef Bacik 		info = NULL;
35547fde62bfSChris Mason 		rcu_read_lock();
3555bf5fc093SJosef Bacik 		list_for_each_entry_rcu(tmp, &root->fs_info->space_info,
3556bf5fc093SJosef Bacik 					list) {
3557bf5fc093SJosef Bacik 			if (tmp->flags == types[i]) {
3558bf5fc093SJosef Bacik 				info = tmp;
3559bf5fc093SJosef Bacik 				break;
3560bf5fc093SJosef Bacik 			}
3561bf5fc093SJosef Bacik 		}
35627fde62bfSChris Mason 		rcu_read_unlock();
35631406e432SJosef Bacik 
3564bf5fc093SJosef Bacik 		if (!info)
3565bf5fc093SJosef Bacik 			continue;
3566bf5fc093SJosef Bacik 
3567bf5fc093SJosef Bacik 		down_read(&info->groups_sem);
3568bf5fc093SJosef Bacik 		for (c = 0; c < BTRFS_NR_RAID_TYPES; c++) {
3569bf5fc093SJosef Bacik 			if (!list_empty(&info->block_groups[c]))
3570bf5fc093SJosef Bacik 				slot_count++;
3571bf5fc093SJosef Bacik 		}
3572bf5fc093SJosef Bacik 		up_read(&info->groups_sem);
3573bf5fc093SJosef Bacik 	}
3574bf5fc093SJosef Bacik 
357536523e95SDavid Sterba 	/*
357636523e95SDavid Sterba 	 * Global block reserve, exported as a space_info
357736523e95SDavid Sterba 	 */
357836523e95SDavid Sterba 	slot_count++;
357936523e95SDavid Sterba 
35807fde62bfSChris Mason 	/* space_slots == 0 means they are asking for a count */
35817fde62bfSChris Mason 	if (space_args.space_slots == 0) {
35827fde62bfSChris Mason 		space_args.total_spaces = slot_count;
35837fde62bfSChris Mason 		goto out;
35847fde62bfSChris Mason 	}
3585bf5fc093SJosef Bacik 
358651788b1bSDan Rosenberg 	slot_count = min_t(u64, space_args.space_slots, slot_count);
3587bf5fc093SJosef Bacik 
35887fde62bfSChris Mason 	alloc_size = sizeof(*dest) * slot_count;
3589bf5fc093SJosef Bacik 
35907fde62bfSChris Mason 	/* we generally have at most 6 or so space infos, one for each raid
35917fde62bfSChris Mason 	 * level.  So, a whole page should be more than enough for everyone
35927fde62bfSChris Mason 	 */
35937fde62bfSChris Mason 	if (alloc_size > PAGE_CACHE_SIZE)
35947fde62bfSChris Mason 		return -ENOMEM;
35957fde62bfSChris Mason 
35967fde62bfSChris Mason 	space_args.total_spaces = 0;
35977fde62bfSChris Mason 	dest = kmalloc(alloc_size, GFP_NOFS);
35987fde62bfSChris Mason 	if (!dest)
35997fde62bfSChris Mason 		return -ENOMEM;
36007fde62bfSChris Mason 	dest_orig = dest;
36017fde62bfSChris Mason 
36027fde62bfSChris Mason 	/* now we have a buffer to copy into */
3603bf5fc093SJosef Bacik 	for (i = 0; i < num_types; i++) {
3604bf5fc093SJosef Bacik 		struct btrfs_space_info *tmp;
3605bf5fc093SJosef Bacik 
360651788b1bSDan Rosenberg 		if (!slot_count)
360751788b1bSDan Rosenberg 			break;
360851788b1bSDan Rosenberg 
3609bf5fc093SJosef Bacik 		info = NULL;
36101406e432SJosef Bacik 		rcu_read_lock();
3611bf5fc093SJosef Bacik 		list_for_each_entry_rcu(tmp, &root->fs_info->space_info,
3612bf5fc093SJosef Bacik 					list) {
3613bf5fc093SJosef Bacik 			if (tmp->flags == types[i]) {
3614bf5fc093SJosef Bacik 				info = tmp;
36157fde62bfSChris Mason 				break;
3616bf5fc093SJosef Bacik 			}
3617bf5fc093SJosef Bacik 		}
3618bf5fc093SJosef Bacik 		rcu_read_unlock();
36197fde62bfSChris Mason 
3620bf5fc093SJosef Bacik 		if (!info)
3621bf5fc093SJosef Bacik 			continue;
3622bf5fc093SJosef Bacik 		down_read(&info->groups_sem);
3623bf5fc093SJosef Bacik 		for (c = 0; c < BTRFS_NR_RAID_TYPES; c++) {
3624bf5fc093SJosef Bacik 			if (!list_empty(&info->block_groups[c])) {
36255af3e8ccSStefan Behrens 				btrfs_get_block_group_info(
36265af3e8ccSStefan Behrens 					&info->block_groups[c], &space);
36277fde62bfSChris Mason 				memcpy(dest, &space, sizeof(space));
36281406e432SJosef Bacik 				dest++;
36291406e432SJosef Bacik 				space_args.total_spaces++;
363051788b1bSDan Rosenberg 				slot_count--;
36311406e432SJosef Bacik 			}
363251788b1bSDan Rosenberg 			if (!slot_count)
363351788b1bSDan Rosenberg 				break;
3634bf5fc093SJosef Bacik 		}
3635bf5fc093SJosef Bacik 		up_read(&info->groups_sem);
3636bf5fc093SJosef Bacik 	}
36371406e432SJosef Bacik 
363836523e95SDavid Sterba 	/*
363936523e95SDavid Sterba 	 * Add global block reserve
364036523e95SDavid Sterba 	 */
364136523e95SDavid Sterba 	if (slot_count) {
364236523e95SDavid Sterba 		struct btrfs_block_rsv *block_rsv = &root->fs_info->global_block_rsv;
364336523e95SDavid Sterba 
364436523e95SDavid Sterba 		spin_lock(&block_rsv->lock);
364536523e95SDavid Sterba 		space.total_bytes = block_rsv->size;
364636523e95SDavid Sterba 		space.used_bytes = block_rsv->size - block_rsv->reserved;
364736523e95SDavid Sterba 		spin_unlock(&block_rsv->lock);
364836523e95SDavid Sterba 		space.flags = BTRFS_SPACE_INFO_GLOBAL_RSV;
364936523e95SDavid Sterba 		memcpy(dest, &space, sizeof(space));
365036523e95SDavid Sterba 		space_args.total_spaces++;
365136523e95SDavid Sterba 	}
365236523e95SDavid Sterba 
36532eec6c81SDaniel J Blueman 	user_dest = (struct btrfs_ioctl_space_info __user *)
36547fde62bfSChris Mason 		(arg + sizeof(struct btrfs_ioctl_space_args));
36557fde62bfSChris Mason 
36567fde62bfSChris Mason 	if (copy_to_user(user_dest, dest_orig, alloc_size))
36577fde62bfSChris Mason 		ret = -EFAULT;
36587fde62bfSChris Mason 
36597fde62bfSChris Mason 	kfree(dest_orig);
36607fde62bfSChris Mason out:
36617fde62bfSChris Mason 	if (ret == 0 && copy_to_user(arg, &space_args, sizeof(space_args)))
36621406e432SJosef Bacik 		ret = -EFAULT;
36631406e432SJosef Bacik 
36641406e432SJosef Bacik 	return ret;
36651406e432SJosef Bacik }
36661406e432SJosef Bacik 
3667f46b5a66SChristoph Hellwig /*
3668f46b5a66SChristoph Hellwig  * there are many ways the trans_start and trans_end ioctls can lead
3669f46b5a66SChristoph Hellwig  * to deadlocks.  They should only be used by applications that
3670f46b5a66SChristoph Hellwig  * basically own the machine, and have a very in depth understanding
3671f46b5a66SChristoph Hellwig  * of all the possible deadlocks and enospc problems.
3672f46b5a66SChristoph Hellwig  */
3673f46b5a66SChristoph Hellwig long btrfs_ioctl_trans_end(struct file *file)
3674f46b5a66SChristoph Hellwig {
3675496ad9aaSAl Viro 	struct inode *inode = file_inode(file);
3676f46b5a66SChristoph Hellwig 	struct btrfs_root *root = BTRFS_I(inode)->root;
3677f46b5a66SChristoph Hellwig 	struct btrfs_trans_handle *trans;
3678f46b5a66SChristoph Hellwig 
3679f46b5a66SChristoph Hellwig 	trans = file->private_data;
36801ab86aedSSage Weil 	if (!trans)
36811ab86aedSSage Weil 		return -EINVAL;
3682b214107eSChristoph Hellwig 	file->private_data = NULL;
36839ca9ee09SSage Weil 
36841ab86aedSSage Weil 	btrfs_end_transaction(trans, root);
36851ab86aedSSage Weil 
3686a4abeea4SJosef Bacik 	atomic_dec(&root->fs_info->open_ioctl_trans);
36879ca9ee09SSage Weil 
36882a79f17eSAl Viro 	mnt_drop_write_file(file);
36891ab86aedSSage Weil 	return 0;
3690f46b5a66SChristoph Hellwig }
3691f46b5a66SChristoph Hellwig 
36929a8c28beSMiao Xie static noinline long btrfs_ioctl_start_sync(struct btrfs_root *root,
36939a8c28beSMiao Xie 					    void __user *argp)
369446204592SSage Weil {
369546204592SSage Weil 	struct btrfs_trans_handle *trans;
369646204592SSage Weil 	u64 transid;
3697db5b493aSTsutomu Itoh 	int ret;
369846204592SSage Weil 
3699d4edf39bSMiao Xie 	trans = btrfs_attach_transaction_barrier(root);
3700ff7c1d33SMiao Xie 	if (IS_ERR(trans)) {
3701ff7c1d33SMiao Xie 		if (PTR_ERR(trans) != -ENOENT)
370298d5dc13STsutomu Itoh 			return PTR_ERR(trans);
3703ff7c1d33SMiao Xie 
3704ff7c1d33SMiao Xie 		/* No running transaction, don't bother */
3705ff7c1d33SMiao Xie 		transid = root->fs_info->last_trans_committed;
3706ff7c1d33SMiao Xie 		goto out;
3707ff7c1d33SMiao Xie 	}
370846204592SSage Weil 	transid = trans->transid;
3709db5b493aSTsutomu Itoh 	ret = btrfs_commit_transaction_async(trans, root, 0);
37108b2b2d3cSTsutomu Itoh 	if (ret) {
37118b2b2d3cSTsutomu Itoh 		btrfs_end_transaction(trans, root);
3712db5b493aSTsutomu Itoh 		return ret;
37138b2b2d3cSTsutomu Itoh 	}
3714ff7c1d33SMiao Xie out:
371546204592SSage Weil 	if (argp)
371646204592SSage Weil 		if (copy_to_user(argp, &transid, sizeof(transid)))
371746204592SSage Weil 			return -EFAULT;
371846204592SSage Weil 	return 0;
371946204592SSage Weil }
372046204592SSage Weil 
37219a8c28beSMiao Xie static noinline long btrfs_ioctl_wait_sync(struct btrfs_root *root,
37229a8c28beSMiao Xie 					   void __user *argp)
372346204592SSage Weil {
372446204592SSage Weil 	u64 transid;
372546204592SSage Weil 
372646204592SSage Weil 	if (argp) {
372746204592SSage Weil 		if (copy_from_user(&transid, argp, sizeof(transid)))
372846204592SSage Weil 			return -EFAULT;
372946204592SSage Weil 	} else {
373046204592SSage Weil 		transid = 0;  /* current trans */
373146204592SSage Weil 	}
373246204592SSage Weil 	return btrfs_wait_for_commit(root, transid);
373346204592SSage Weil }
373446204592SSage Weil 
3735b8e95489SMiao Xie static long btrfs_ioctl_scrub(struct file *file, void __user *arg)
3736475f6387SJan Schmidt {
3737496ad9aaSAl Viro 	struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
3738475f6387SJan Schmidt 	struct btrfs_ioctl_scrub_args *sa;
3739b8e95489SMiao Xie 	int ret;
3740475f6387SJan Schmidt 
3741475f6387SJan Schmidt 	if (!capable(CAP_SYS_ADMIN))
3742475f6387SJan Schmidt 		return -EPERM;
3743475f6387SJan Schmidt 
3744475f6387SJan Schmidt 	sa = memdup_user(arg, sizeof(*sa));
3745475f6387SJan Schmidt 	if (IS_ERR(sa))
3746475f6387SJan Schmidt 		return PTR_ERR(sa);
3747475f6387SJan Schmidt 
3748b8e95489SMiao Xie 	if (!(sa->flags & BTRFS_SCRUB_READONLY)) {
3749b8e95489SMiao Xie 		ret = mnt_want_write_file(file);
3750b8e95489SMiao Xie 		if (ret)
3751b8e95489SMiao Xie 			goto out;
3752b8e95489SMiao Xie 	}
3753b8e95489SMiao Xie 
3754aa1b8cd4SStefan Behrens 	ret = btrfs_scrub_dev(root->fs_info, sa->devid, sa->start, sa->end,
375563a212abSStefan Behrens 			      &sa->progress, sa->flags & BTRFS_SCRUB_READONLY,
375663a212abSStefan Behrens 			      0);
3757475f6387SJan Schmidt 
3758475f6387SJan Schmidt 	if (copy_to_user(arg, sa, sizeof(*sa)))
3759475f6387SJan Schmidt 		ret = -EFAULT;
3760475f6387SJan Schmidt 
3761b8e95489SMiao Xie 	if (!(sa->flags & BTRFS_SCRUB_READONLY))
3762b8e95489SMiao Xie 		mnt_drop_write_file(file);
3763b8e95489SMiao Xie out:
3764475f6387SJan Schmidt 	kfree(sa);
3765475f6387SJan Schmidt 	return ret;
3766475f6387SJan Schmidt }
3767475f6387SJan Schmidt 
3768475f6387SJan Schmidt static long btrfs_ioctl_scrub_cancel(struct btrfs_root *root, void __user *arg)
3769475f6387SJan Schmidt {
3770475f6387SJan Schmidt 	if (!capable(CAP_SYS_ADMIN))
3771475f6387SJan Schmidt 		return -EPERM;
3772475f6387SJan Schmidt 
3773aa1b8cd4SStefan Behrens 	return btrfs_scrub_cancel(root->fs_info);
3774475f6387SJan Schmidt }
3775475f6387SJan Schmidt 
3776475f6387SJan Schmidt static long btrfs_ioctl_scrub_progress(struct btrfs_root *root,
3777475f6387SJan Schmidt 				       void __user *arg)
3778475f6387SJan Schmidt {
3779475f6387SJan Schmidt 	struct btrfs_ioctl_scrub_args *sa;
3780475f6387SJan Schmidt 	int ret;
3781475f6387SJan Schmidt 
3782475f6387SJan Schmidt 	if (!capable(CAP_SYS_ADMIN))
3783475f6387SJan Schmidt 		return -EPERM;
3784475f6387SJan Schmidt 
3785475f6387SJan Schmidt 	sa = memdup_user(arg, sizeof(*sa));
3786475f6387SJan Schmidt 	if (IS_ERR(sa))
3787475f6387SJan Schmidt 		return PTR_ERR(sa);
3788475f6387SJan Schmidt 
3789475f6387SJan Schmidt 	ret = btrfs_scrub_progress(root, sa->devid, &sa->progress);
3790475f6387SJan Schmidt 
3791475f6387SJan Schmidt 	if (copy_to_user(arg, sa, sizeof(*sa)))
3792475f6387SJan Schmidt 		ret = -EFAULT;
3793475f6387SJan Schmidt 
3794475f6387SJan Schmidt 	kfree(sa);
3795475f6387SJan Schmidt 	return ret;
3796475f6387SJan Schmidt }
3797475f6387SJan Schmidt 
3798c11d2c23SStefan Behrens static long btrfs_ioctl_get_dev_stats(struct btrfs_root *root,
3799b27f7c0cSDavid Sterba 				      void __user *arg)
3800c11d2c23SStefan Behrens {
3801c11d2c23SStefan Behrens 	struct btrfs_ioctl_get_dev_stats *sa;
3802c11d2c23SStefan Behrens 	int ret;
3803c11d2c23SStefan Behrens 
3804c11d2c23SStefan Behrens 	sa = memdup_user(arg, sizeof(*sa));
3805c11d2c23SStefan Behrens 	if (IS_ERR(sa))
3806c11d2c23SStefan Behrens 		return PTR_ERR(sa);
3807c11d2c23SStefan Behrens 
3808b27f7c0cSDavid Sterba 	if ((sa->flags & BTRFS_DEV_STATS_RESET) && !capable(CAP_SYS_ADMIN)) {
3809b27f7c0cSDavid Sterba 		kfree(sa);
3810b27f7c0cSDavid Sterba 		return -EPERM;
3811b27f7c0cSDavid Sterba 	}
3812b27f7c0cSDavid Sterba 
3813b27f7c0cSDavid Sterba 	ret = btrfs_get_dev_stats(root, sa);
3814c11d2c23SStefan Behrens 
3815c11d2c23SStefan Behrens 	if (copy_to_user(arg, sa, sizeof(*sa)))
3816c11d2c23SStefan Behrens 		ret = -EFAULT;
3817c11d2c23SStefan Behrens 
3818c11d2c23SStefan Behrens 	kfree(sa);
3819c11d2c23SStefan Behrens 	return ret;
3820c11d2c23SStefan Behrens }
3821c11d2c23SStefan Behrens 
38223f6bcfbdSStefan Behrens static long btrfs_ioctl_dev_replace(struct btrfs_root *root, void __user *arg)
38233f6bcfbdSStefan Behrens {
38243f6bcfbdSStefan Behrens 	struct btrfs_ioctl_dev_replace_args *p;
38253f6bcfbdSStefan Behrens 	int ret;
38263f6bcfbdSStefan Behrens 
38273f6bcfbdSStefan Behrens 	if (!capable(CAP_SYS_ADMIN))
38283f6bcfbdSStefan Behrens 		return -EPERM;
38293f6bcfbdSStefan Behrens 
38303f6bcfbdSStefan Behrens 	p = memdup_user(arg, sizeof(*p));
38313f6bcfbdSStefan Behrens 	if (IS_ERR(p))
38323f6bcfbdSStefan Behrens 		return PTR_ERR(p);
38333f6bcfbdSStefan Behrens 
38343f6bcfbdSStefan Behrens 	switch (p->cmd) {
38353f6bcfbdSStefan Behrens 	case BTRFS_IOCTL_DEV_REPLACE_CMD_START:
3836adfa97cbSIlya Dryomov 		if (root->fs_info->sb->s_flags & MS_RDONLY) {
3837adfa97cbSIlya Dryomov 			ret = -EROFS;
3838adfa97cbSIlya Dryomov 			goto out;
3839adfa97cbSIlya Dryomov 		}
38403f6bcfbdSStefan Behrens 		if (atomic_xchg(
38413f6bcfbdSStefan Behrens 			&root->fs_info->mutually_exclusive_operation_running,
38423f6bcfbdSStefan Behrens 			1)) {
3843e57138b3SAnand Jain 			ret = BTRFS_ERROR_DEV_EXCL_RUN_IN_PROGRESS;
38443f6bcfbdSStefan Behrens 		} else {
38453f6bcfbdSStefan Behrens 			ret = btrfs_dev_replace_start(root, p);
38463f6bcfbdSStefan Behrens 			atomic_set(
38473f6bcfbdSStefan Behrens 			 &root->fs_info->mutually_exclusive_operation_running,
38483f6bcfbdSStefan Behrens 			 0);
38493f6bcfbdSStefan Behrens 		}
38503f6bcfbdSStefan Behrens 		break;
38513f6bcfbdSStefan Behrens 	case BTRFS_IOCTL_DEV_REPLACE_CMD_STATUS:
38523f6bcfbdSStefan Behrens 		btrfs_dev_replace_status(root->fs_info, p);
38533f6bcfbdSStefan Behrens 		ret = 0;
38543f6bcfbdSStefan Behrens 		break;
38553f6bcfbdSStefan Behrens 	case BTRFS_IOCTL_DEV_REPLACE_CMD_CANCEL:
38563f6bcfbdSStefan Behrens 		ret = btrfs_dev_replace_cancel(root->fs_info, p);
38573f6bcfbdSStefan Behrens 		break;
38583f6bcfbdSStefan Behrens 	default:
38593f6bcfbdSStefan Behrens 		ret = -EINVAL;
38603f6bcfbdSStefan Behrens 		break;
38613f6bcfbdSStefan Behrens 	}
38623f6bcfbdSStefan Behrens 
38633f6bcfbdSStefan Behrens 	if (copy_to_user(arg, p, sizeof(*p)))
38643f6bcfbdSStefan Behrens 		ret = -EFAULT;
3865adfa97cbSIlya Dryomov out:
38663f6bcfbdSStefan Behrens 	kfree(p);
38673f6bcfbdSStefan Behrens 	return ret;
38683f6bcfbdSStefan Behrens }
38693f6bcfbdSStefan Behrens 
3870d7728c96SJan Schmidt static long btrfs_ioctl_ino_to_path(struct btrfs_root *root, void __user *arg)
3871d7728c96SJan Schmidt {
3872d7728c96SJan Schmidt 	int ret = 0;
3873d7728c96SJan Schmidt 	int i;
3874740c3d22SChris Mason 	u64 rel_ptr;
3875d7728c96SJan Schmidt 	int size;
3876806468f8SChris Mason 	struct btrfs_ioctl_ino_path_args *ipa = NULL;
3877d7728c96SJan Schmidt 	struct inode_fs_paths *ipath = NULL;
3878d7728c96SJan Schmidt 	struct btrfs_path *path;
3879d7728c96SJan Schmidt 
388082b22ac8SKusanagi Kouichi 	if (!capable(CAP_DAC_READ_SEARCH))
3881d7728c96SJan Schmidt 		return -EPERM;
3882d7728c96SJan Schmidt 
3883d7728c96SJan Schmidt 	path = btrfs_alloc_path();
3884d7728c96SJan Schmidt 	if (!path) {
3885d7728c96SJan Schmidt 		ret = -ENOMEM;
3886d7728c96SJan Schmidt 		goto out;
3887d7728c96SJan Schmidt 	}
3888d7728c96SJan Schmidt 
3889d7728c96SJan Schmidt 	ipa = memdup_user(arg, sizeof(*ipa));
3890d7728c96SJan Schmidt 	if (IS_ERR(ipa)) {
3891d7728c96SJan Schmidt 		ret = PTR_ERR(ipa);
3892d7728c96SJan Schmidt 		ipa = NULL;
3893d7728c96SJan Schmidt 		goto out;
3894d7728c96SJan Schmidt 	}
3895d7728c96SJan Schmidt 
3896d7728c96SJan Schmidt 	size = min_t(u32, ipa->size, 4096);
3897d7728c96SJan Schmidt 	ipath = init_ipath(size, root, path);
3898d7728c96SJan Schmidt 	if (IS_ERR(ipath)) {
3899d7728c96SJan Schmidt 		ret = PTR_ERR(ipath);
3900d7728c96SJan Schmidt 		ipath = NULL;
3901d7728c96SJan Schmidt 		goto out;
3902d7728c96SJan Schmidt 	}
3903d7728c96SJan Schmidt 
3904d7728c96SJan Schmidt 	ret = paths_from_inode(ipa->inum, ipath);
3905d7728c96SJan Schmidt 	if (ret < 0)
3906d7728c96SJan Schmidt 		goto out;
3907d7728c96SJan Schmidt 
3908d7728c96SJan Schmidt 	for (i = 0; i < ipath->fspath->elem_cnt; ++i) {
3909745c4d8eSJeff Mahoney 		rel_ptr = ipath->fspath->val[i] -
3910745c4d8eSJeff Mahoney 			  (u64)(unsigned long)ipath->fspath->val;
3911740c3d22SChris Mason 		ipath->fspath->val[i] = rel_ptr;
3912d7728c96SJan Schmidt 	}
3913d7728c96SJan Schmidt 
3914745c4d8eSJeff Mahoney 	ret = copy_to_user((void *)(unsigned long)ipa->fspath,
3915745c4d8eSJeff Mahoney 			   (void *)(unsigned long)ipath->fspath, size);
3916d7728c96SJan Schmidt 	if (ret) {
3917d7728c96SJan Schmidt 		ret = -EFAULT;
3918d7728c96SJan Schmidt 		goto out;
3919d7728c96SJan Schmidt 	}
3920d7728c96SJan Schmidt 
3921d7728c96SJan Schmidt out:
3922d7728c96SJan Schmidt 	btrfs_free_path(path);
3923d7728c96SJan Schmidt 	free_ipath(ipath);
3924d7728c96SJan Schmidt 	kfree(ipa);
3925d7728c96SJan Schmidt 
3926d7728c96SJan Schmidt 	return ret;
3927d7728c96SJan Schmidt }
3928d7728c96SJan Schmidt 
3929d7728c96SJan Schmidt static int build_ino_list(u64 inum, u64 offset, u64 root, void *ctx)
3930d7728c96SJan Schmidt {
3931d7728c96SJan Schmidt 	struct btrfs_data_container *inodes = ctx;
3932d7728c96SJan Schmidt 	const size_t c = 3 * sizeof(u64);
3933d7728c96SJan Schmidt 
3934d7728c96SJan Schmidt 	if (inodes->bytes_left >= c) {
3935d7728c96SJan Schmidt 		inodes->bytes_left -= c;
3936d7728c96SJan Schmidt 		inodes->val[inodes->elem_cnt] = inum;
3937d7728c96SJan Schmidt 		inodes->val[inodes->elem_cnt + 1] = offset;
3938d7728c96SJan Schmidt 		inodes->val[inodes->elem_cnt + 2] = root;
3939d7728c96SJan Schmidt 		inodes->elem_cnt += 3;
3940d7728c96SJan Schmidt 	} else {
3941d7728c96SJan Schmidt 		inodes->bytes_missing += c - inodes->bytes_left;
3942d7728c96SJan Schmidt 		inodes->bytes_left = 0;
3943d7728c96SJan Schmidt 		inodes->elem_missed += 3;
3944d7728c96SJan Schmidt 	}
3945d7728c96SJan Schmidt 
3946d7728c96SJan Schmidt 	return 0;
3947d7728c96SJan Schmidt }
3948d7728c96SJan Schmidt 
3949d7728c96SJan Schmidt static long btrfs_ioctl_logical_to_ino(struct btrfs_root *root,
3950d7728c96SJan Schmidt 					void __user *arg)
3951d7728c96SJan Schmidt {
3952d7728c96SJan Schmidt 	int ret = 0;
3953d7728c96SJan Schmidt 	int size;
3954d7728c96SJan Schmidt 	struct btrfs_ioctl_logical_ino_args *loi;
3955d7728c96SJan Schmidt 	struct btrfs_data_container *inodes = NULL;
3956d7728c96SJan Schmidt 	struct btrfs_path *path = NULL;
3957d7728c96SJan Schmidt 
3958d7728c96SJan Schmidt 	if (!capable(CAP_SYS_ADMIN))
3959d7728c96SJan Schmidt 		return -EPERM;
3960d7728c96SJan Schmidt 
3961d7728c96SJan Schmidt 	loi = memdup_user(arg, sizeof(*loi));
3962d7728c96SJan Schmidt 	if (IS_ERR(loi)) {
3963d7728c96SJan Schmidt 		ret = PTR_ERR(loi);
3964d7728c96SJan Schmidt 		loi = NULL;
3965d7728c96SJan Schmidt 		goto out;
3966d7728c96SJan Schmidt 	}
3967d7728c96SJan Schmidt 
3968d7728c96SJan Schmidt 	path = btrfs_alloc_path();
3969d7728c96SJan Schmidt 	if (!path) {
3970d7728c96SJan Schmidt 		ret = -ENOMEM;
3971d7728c96SJan Schmidt 		goto out;
3972d7728c96SJan Schmidt 	}
3973d7728c96SJan Schmidt 
3974425d17a2SLiu Bo 	size = min_t(u32, loi->size, 64 * 1024);
3975d7728c96SJan Schmidt 	inodes = init_data_container(size);
3976d7728c96SJan Schmidt 	if (IS_ERR(inodes)) {
3977d7728c96SJan Schmidt 		ret = PTR_ERR(inodes);
3978d7728c96SJan Schmidt 		inodes = NULL;
3979d7728c96SJan Schmidt 		goto out;
3980d7728c96SJan Schmidt 	}
3981d7728c96SJan Schmidt 
3982df031f07SLiu Bo 	ret = iterate_inodes_from_logical(loi->logical, root->fs_info, path,
3983df031f07SLiu Bo 					  build_ino_list, inodes);
3984df031f07SLiu Bo 	if (ret == -EINVAL)
3985d7728c96SJan Schmidt 		ret = -ENOENT;
3986d7728c96SJan Schmidt 	if (ret < 0)
3987d7728c96SJan Schmidt 		goto out;
3988d7728c96SJan Schmidt 
3989745c4d8eSJeff Mahoney 	ret = copy_to_user((void *)(unsigned long)loi->inodes,
3990745c4d8eSJeff Mahoney 			   (void *)(unsigned long)inodes, size);
3991d7728c96SJan Schmidt 	if (ret)
3992d7728c96SJan Schmidt 		ret = -EFAULT;
3993d7728c96SJan Schmidt 
3994d7728c96SJan Schmidt out:
3995d7728c96SJan Schmidt 	btrfs_free_path(path);
3996425d17a2SLiu Bo 	vfree(inodes);
3997d7728c96SJan Schmidt 	kfree(loi);
3998d7728c96SJan Schmidt 
3999d7728c96SJan Schmidt 	return ret;
4000d7728c96SJan Schmidt }
4001d7728c96SJan Schmidt 
400219a39dceSIlya Dryomov void update_ioctl_balance_args(struct btrfs_fs_info *fs_info, int lock,
4003c9e9f97bSIlya Dryomov 			       struct btrfs_ioctl_balance_args *bargs)
4004c9e9f97bSIlya Dryomov {
4005c9e9f97bSIlya Dryomov 	struct btrfs_balance_control *bctl = fs_info->balance_ctl;
4006c9e9f97bSIlya Dryomov 
4007c9e9f97bSIlya Dryomov 	bargs->flags = bctl->flags;
4008c9e9f97bSIlya Dryomov 
4009837d5b6eSIlya Dryomov 	if (atomic_read(&fs_info->balance_running))
4010837d5b6eSIlya Dryomov 		bargs->state |= BTRFS_BALANCE_STATE_RUNNING;
4011837d5b6eSIlya Dryomov 	if (atomic_read(&fs_info->balance_pause_req))
4012837d5b6eSIlya Dryomov 		bargs->state |= BTRFS_BALANCE_STATE_PAUSE_REQ;
4013a7e99c69SIlya Dryomov 	if (atomic_read(&fs_info->balance_cancel_req))
4014a7e99c69SIlya Dryomov 		bargs->state |= BTRFS_BALANCE_STATE_CANCEL_REQ;
4015837d5b6eSIlya Dryomov 
4016c9e9f97bSIlya Dryomov 	memcpy(&bargs->data, &bctl->data, sizeof(bargs->data));
4017c9e9f97bSIlya Dryomov 	memcpy(&bargs->meta, &bctl->meta, sizeof(bargs->meta));
4018c9e9f97bSIlya Dryomov 	memcpy(&bargs->sys, &bctl->sys, sizeof(bargs->sys));
401919a39dceSIlya Dryomov 
402019a39dceSIlya Dryomov 	if (lock) {
402119a39dceSIlya Dryomov 		spin_lock(&fs_info->balance_lock);
402219a39dceSIlya Dryomov 		memcpy(&bargs->stat, &bctl->stat, sizeof(bargs->stat));
402319a39dceSIlya Dryomov 		spin_unlock(&fs_info->balance_lock);
402419a39dceSIlya Dryomov 	} else {
402519a39dceSIlya Dryomov 		memcpy(&bargs->stat, &bctl->stat, sizeof(bargs->stat));
402619a39dceSIlya Dryomov 	}
4027c9e9f97bSIlya Dryomov }
4028c9e9f97bSIlya Dryomov 
40299ba1f6e4SLiu Bo static long btrfs_ioctl_balance(struct file *file, void __user *arg)
4030c9e9f97bSIlya Dryomov {
4031496ad9aaSAl Viro 	struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
4032c9e9f97bSIlya Dryomov 	struct btrfs_fs_info *fs_info = root->fs_info;
4033c9e9f97bSIlya Dryomov 	struct btrfs_ioctl_balance_args *bargs;
4034c9e9f97bSIlya Dryomov 	struct btrfs_balance_control *bctl;
4035ed0fb78fSIlya Dryomov 	bool need_unlock; /* for mut. excl. ops lock */
4036c9e9f97bSIlya Dryomov 	int ret;
4037c9e9f97bSIlya Dryomov 
4038c9e9f97bSIlya Dryomov 	if (!capable(CAP_SYS_ADMIN))
4039c9e9f97bSIlya Dryomov 		return -EPERM;
4040c9e9f97bSIlya Dryomov 
4041e54bfa31SLiu Bo 	ret = mnt_want_write_file(file);
40429ba1f6e4SLiu Bo 	if (ret)
40439ba1f6e4SLiu Bo 		return ret;
40449ba1f6e4SLiu Bo 
4045ed0fb78fSIlya Dryomov again:
4046ed0fb78fSIlya Dryomov 	if (!atomic_xchg(&fs_info->mutually_exclusive_operation_running, 1)) {
4047c9e9f97bSIlya Dryomov 		mutex_lock(&fs_info->volume_mutex);
4048c9e9f97bSIlya Dryomov 		mutex_lock(&fs_info->balance_mutex);
4049ed0fb78fSIlya Dryomov 		need_unlock = true;
4050ed0fb78fSIlya Dryomov 		goto locked;
4051ed0fb78fSIlya Dryomov 	}
4052ed0fb78fSIlya Dryomov 
4053ed0fb78fSIlya Dryomov 	/*
4054ed0fb78fSIlya Dryomov 	 * mut. excl. ops lock is locked.  Three possibilites:
4055ed0fb78fSIlya Dryomov 	 *   (1) some other op is running
4056ed0fb78fSIlya Dryomov 	 *   (2) balance is running
4057ed0fb78fSIlya Dryomov 	 *   (3) balance is paused -- special case (think resume)
4058ed0fb78fSIlya Dryomov 	 */
4059ed0fb78fSIlya Dryomov 	mutex_lock(&fs_info->balance_mutex);
4060ed0fb78fSIlya Dryomov 	if (fs_info->balance_ctl) {
4061ed0fb78fSIlya Dryomov 		/* this is either (2) or (3) */
4062ed0fb78fSIlya Dryomov 		if (!atomic_read(&fs_info->balance_running)) {
4063ed0fb78fSIlya Dryomov 			mutex_unlock(&fs_info->balance_mutex);
4064ed0fb78fSIlya Dryomov 			if (!mutex_trylock(&fs_info->volume_mutex))
4065ed0fb78fSIlya Dryomov 				goto again;
4066ed0fb78fSIlya Dryomov 			mutex_lock(&fs_info->balance_mutex);
4067ed0fb78fSIlya Dryomov 
4068ed0fb78fSIlya Dryomov 			if (fs_info->balance_ctl &&
4069ed0fb78fSIlya Dryomov 			    !atomic_read(&fs_info->balance_running)) {
4070ed0fb78fSIlya Dryomov 				/* this is (3) */
4071ed0fb78fSIlya Dryomov 				need_unlock = false;
4072ed0fb78fSIlya Dryomov 				goto locked;
4073ed0fb78fSIlya Dryomov 			}
4074ed0fb78fSIlya Dryomov 
4075ed0fb78fSIlya Dryomov 			mutex_unlock(&fs_info->balance_mutex);
4076ed0fb78fSIlya Dryomov 			mutex_unlock(&fs_info->volume_mutex);
4077ed0fb78fSIlya Dryomov 			goto again;
4078ed0fb78fSIlya Dryomov 		} else {
4079ed0fb78fSIlya Dryomov 			/* this is (2) */
4080ed0fb78fSIlya Dryomov 			mutex_unlock(&fs_info->balance_mutex);
4081ed0fb78fSIlya Dryomov 			ret = -EINPROGRESS;
4082ed0fb78fSIlya Dryomov 			goto out;
4083ed0fb78fSIlya Dryomov 		}
4084ed0fb78fSIlya Dryomov 	} else {
4085ed0fb78fSIlya Dryomov 		/* this is (1) */
4086ed0fb78fSIlya Dryomov 		mutex_unlock(&fs_info->balance_mutex);
4087e57138b3SAnand Jain 		ret = BTRFS_ERROR_DEV_EXCL_RUN_IN_PROGRESS;
4088ed0fb78fSIlya Dryomov 		goto out;
4089ed0fb78fSIlya Dryomov 	}
4090ed0fb78fSIlya Dryomov 
4091ed0fb78fSIlya Dryomov locked:
4092ed0fb78fSIlya Dryomov 	BUG_ON(!atomic_read(&fs_info->mutually_exclusive_operation_running));
4093c9e9f97bSIlya Dryomov 
4094c9e9f97bSIlya Dryomov 	if (arg) {
4095c9e9f97bSIlya Dryomov 		bargs = memdup_user(arg, sizeof(*bargs));
4096c9e9f97bSIlya Dryomov 		if (IS_ERR(bargs)) {
4097c9e9f97bSIlya Dryomov 			ret = PTR_ERR(bargs);
4098ed0fb78fSIlya Dryomov 			goto out_unlock;
4099c9e9f97bSIlya Dryomov 		}
4100de322263SIlya Dryomov 
4101de322263SIlya Dryomov 		if (bargs->flags & BTRFS_BALANCE_RESUME) {
4102de322263SIlya Dryomov 			if (!fs_info->balance_ctl) {
4103de322263SIlya Dryomov 				ret = -ENOTCONN;
4104de322263SIlya Dryomov 				goto out_bargs;
4105de322263SIlya Dryomov 			}
4106de322263SIlya Dryomov 
4107de322263SIlya Dryomov 			bctl = fs_info->balance_ctl;
4108de322263SIlya Dryomov 			spin_lock(&fs_info->balance_lock);
4109de322263SIlya Dryomov 			bctl->flags |= BTRFS_BALANCE_RESUME;
4110de322263SIlya Dryomov 			spin_unlock(&fs_info->balance_lock);
4111de322263SIlya Dryomov 
4112de322263SIlya Dryomov 			goto do_balance;
4113de322263SIlya Dryomov 		}
4114c9e9f97bSIlya Dryomov 	} else {
4115c9e9f97bSIlya Dryomov 		bargs = NULL;
4116c9e9f97bSIlya Dryomov 	}
4117c9e9f97bSIlya Dryomov 
4118ed0fb78fSIlya Dryomov 	if (fs_info->balance_ctl) {
4119837d5b6eSIlya Dryomov 		ret = -EINPROGRESS;
4120837d5b6eSIlya Dryomov 		goto out_bargs;
4121837d5b6eSIlya Dryomov 	}
4122837d5b6eSIlya Dryomov 
4123c9e9f97bSIlya Dryomov 	bctl = kzalloc(sizeof(*bctl), GFP_NOFS);
4124c9e9f97bSIlya Dryomov 	if (!bctl) {
4125c9e9f97bSIlya Dryomov 		ret = -ENOMEM;
4126c9e9f97bSIlya Dryomov 		goto out_bargs;
4127c9e9f97bSIlya Dryomov 	}
4128c9e9f97bSIlya Dryomov 
4129c9e9f97bSIlya Dryomov 	bctl->fs_info = fs_info;
4130c9e9f97bSIlya Dryomov 	if (arg) {
4131c9e9f97bSIlya Dryomov 		memcpy(&bctl->data, &bargs->data, sizeof(bctl->data));
4132c9e9f97bSIlya Dryomov 		memcpy(&bctl->meta, &bargs->meta, sizeof(bctl->meta));
4133c9e9f97bSIlya Dryomov 		memcpy(&bctl->sys, &bargs->sys, sizeof(bctl->sys));
4134c9e9f97bSIlya Dryomov 
4135c9e9f97bSIlya Dryomov 		bctl->flags = bargs->flags;
4136f43ffb60SIlya Dryomov 	} else {
4137f43ffb60SIlya Dryomov 		/* balance everything - no filters */
4138f43ffb60SIlya Dryomov 		bctl->flags |= BTRFS_BALANCE_TYPE_MASK;
4139c9e9f97bSIlya Dryomov 	}
4140c9e9f97bSIlya Dryomov 
4141de322263SIlya Dryomov do_balance:
4142c9e9f97bSIlya Dryomov 	/*
4143ed0fb78fSIlya Dryomov 	 * Ownership of bctl and mutually_exclusive_operation_running
4144ed0fb78fSIlya Dryomov 	 * goes to to btrfs_balance.  bctl is freed in __cancel_balance,
4145ed0fb78fSIlya Dryomov 	 * or, if restriper was paused all the way until unmount, in
4146ed0fb78fSIlya Dryomov 	 * free_fs_info.  mutually_exclusive_operation_running is
4147ed0fb78fSIlya Dryomov 	 * cleared in __cancel_balance.
4148c9e9f97bSIlya Dryomov 	 */
4149ed0fb78fSIlya Dryomov 	need_unlock = false;
4150ed0fb78fSIlya Dryomov 
4151ed0fb78fSIlya Dryomov 	ret = btrfs_balance(bctl, bargs);
4152ed0fb78fSIlya Dryomov 
4153c9e9f97bSIlya Dryomov 	if (arg) {
4154c9e9f97bSIlya Dryomov 		if (copy_to_user(arg, bargs, sizeof(*bargs)))
4155c9e9f97bSIlya Dryomov 			ret = -EFAULT;
4156c9e9f97bSIlya Dryomov 	}
4157c9e9f97bSIlya Dryomov 
4158c9e9f97bSIlya Dryomov out_bargs:
4159c9e9f97bSIlya Dryomov 	kfree(bargs);
4160ed0fb78fSIlya Dryomov out_unlock:
4161c9e9f97bSIlya Dryomov 	mutex_unlock(&fs_info->balance_mutex);
4162c9e9f97bSIlya Dryomov 	mutex_unlock(&fs_info->volume_mutex);
4163ed0fb78fSIlya Dryomov 	if (need_unlock)
4164ed0fb78fSIlya Dryomov 		atomic_set(&fs_info->mutually_exclusive_operation_running, 0);
4165ed0fb78fSIlya Dryomov out:
4166e54bfa31SLiu Bo 	mnt_drop_write_file(file);
4167c9e9f97bSIlya Dryomov 	return ret;
4168c9e9f97bSIlya Dryomov }
4169c9e9f97bSIlya Dryomov 
4170837d5b6eSIlya Dryomov static long btrfs_ioctl_balance_ctl(struct btrfs_root *root, int cmd)
4171837d5b6eSIlya Dryomov {
4172837d5b6eSIlya Dryomov 	if (!capable(CAP_SYS_ADMIN))
4173837d5b6eSIlya Dryomov 		return -EPERM;
4174837d5b6eSIlya Dryomov 
4175837d5b6eSIlya Dryomov 	switch (cmd) {
4176837d5b6eSIlya Dryomov 	case BTRFS_BALANCE_CTL_PAUSE:
4177837d5b6eSIlya Dryomov 		return btrfs_pause_balance(root->fs_info);
4178a7e99c69SIlya Dryomov 	case BTRFS_BALANCE_CTL_CANCEL:
4179a7e99c69SIlya Dryomov 		return btrfs_cancel_balance(root->fs_info);
4180837d5b6eSIlya Dryomov 	}
4181837d5b6eSIlya Dryomov 
4182837d5b6eSIlya Dryomov 	return -EINVAL;
4183837d5b6eSIlya Dryomov }
4184837d5b6eSIlya Dryomov 
418519a39dceSIlya Dryomov static long btrfs_ioctl_balance_progress(struct btrfs_root *root,
418619a39dceSIlya Dryomov 					 void __user *arg)
418719a39dceSIlya Dryomov {
418819a39dceSIlya Dryomov 	struct btrfs_fs_info *fs_info = root->fs_info;
418919a39dceSIlya Dryomov 	struct btrfs_ioctl_balance_args *bargs;
419019a39dceSIlya Dryomov 	int ret = 0;
419119a39dceSIlya Dryomov 
419219a39dceSIlya Dryomov 	if (!capable(CAP_SYS_ADMIN))
419319a39dceSIlya Dryomov 		return -EPERM;
419419a39dceSIlya Dryomov 
419519a39dceSIlya Dryomov 	mutex_lock(&fs_info->balance_mutex);
419619a39dceSIlya Dryomov 	if (!fs_info->balance_ctl) {
419719a39dceSIlya Dryomov 		ret = -ENOTCONN;
419819a39dceSIlya Dryomov 		goto out;
419919a39dceSIlya Dryomov 	}
420019a39dceSIlya Dryomov 
420119a39dceSIlya Dryomov 	bargs = kzalloc(sizeof(*bargs), GFP_NOFS);
420219a39dceSIlya Dryomov 	if (!bargs) {
420319a39dceSIlya Dryomov 		ret = -ENOMEM;
420419a39dceSIlya Dryomov 		goto out;
420519a39dceSIlya Dryomov 	}
420619a39dceSIlya Dryomov 
420719a39dceSIlya Dryomov 	update_ioctl_balance_args(fs_info, 1, bargs);
420819a39dceSIlya Dryomov 
420919a39dceSIlya Dryomov 	if (copy_to_user(arg, bargs, sizeof(*bargs)))
421019a39dceSIlya Dryomov 		ret = -EFAULT;
421119a39dceSIlya Dryomov 
421219a39dceSIlya Dryomov 	kfree(bargs);
421319a39dceSIlya Dryomov out:
421419a39dceSIlya Dryomov 	mutex_unlock(&fs_info->balance_mutex);
421519a39dceSIlya Dryomov 	return ret;
421619a39dceSIlya Dryomov }
421719a39dceSIlya Dryomov 
4218905b0ddaSMiao Xie static long btrfs_ioctl_quota_ctl(struct file *file, void __user *arg)
42195d13a37bSArne Jansen {
4220496ad9aaSAl Viro 	struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
42215d13a37bSArne Jansen 	struct btrfs_ioctl_quota_ctl_args *sa;
42225d13a37bSArne Jansen 	struct btrfs_trans_handle *trans = NULL;
42235d13a37bSArne Jansen 	int ret;
42245d13a37bSArne Jansen 	int err;
42255d13a37bSArne Jansen 
42265d13a37bSArne Jansen 	if (!capable(CAP_SYS_ADMIN))
42275d13a37bSArne Jansen 		return -EPERM;
42285d13a37bSArne Jansen 
4229905b0ddaSMiao Xie 	ret = mnt_want_write_file(file);
4230905b0ddaSMiao Xie 	if (ret)
4231905b0ddaSMiao Xie 		return ret;
42325d13a37bSArne Jansen 
42335d13a37bSArne Jansen 	sa = memdup_user(arg, sizeof(*sa));
4234905b0ddaSMiao Xie 	if (IS_ERR(sa)) {
4235905b0ddaSMiao Xie 		ret = PTR_ERR(sa);
4236905b0ddaSMiao Xie 		goto drop_write;
4237905b0ddaSMiao Xie 	}
42385d13a37bSArne Jansen 
42397708f029SWang Shilong 	down_write(&root->fs_info->subvol_sem);
424092f183aaSWang Shilong 	trans = btrfs_start_transaction(root->fs_info->tree_root, 2);
42415d13a37bSArne Jansen 	if (IS_ERR(trans)) {
42425d13a37bSArne Jansen 		ret = PTR_ERR(trans);
42435d13a37bSArne Jansen 		goto out;
42445d13a37bSArne Jansen 	}
42455d13a37bSArne Jansen 
42465d13a37bSArne Jansen 	switch (sa->cmd) {
42475d13a37bSArne Jansen 	case BTRFS_QUOTA_CTL_ENABLE:
42485d13a37bSArne Jansen 		ret = btrfs_quota_enable(trans, root->fs_info);
42495d13a37bSArne Jansen 		break;
42505d13a37bSArne Jansen 	case BTRFS_QUOTA_CTL_DISABLE:
42515d13a37bSArne Jansen 		ret = btrfs_quota_disable(trans, root->fs_info);
42525d13a37bSArne Jansen 		break;
42535d13a37bSArne Jansen 	default:
42545d13a37bSArne Jansen 		ret = -EINVAL;
42555d13a37bSArne Jansen 		break;
42565d13a37bSArne Jansen 	}
42575d13a37bSArne Jansen 
425892f183aaSWang Shilong 	err = btrfs_commit_transaction(trans, root->fs_info->tree_root);
42595d13a37bSArne Jansen 	if (err && !ret)
42605d13a37bSArne Jansen 		ret = err;
42615d13a37bSArne Jansen out:
42625d13a37bSArne Jansen 	kfree(sa);
42637708f029SWang Shilong 	up_write(&root->fs_info->subvol_sem);
4264905b0ddaSMiao Xie drop_write:
4265905b0ddaSMiao Xie 	mnt_drop_write_file(file);
42665d13a37bSArne Jansen 	return ret;
42675d13a37bSArne Jansen }
42685d13a37bSArne Jansen 
4269905b0ddaSMiao Xie static long btrfs_ioctl_qgroup_assign(struct file *file, void __user *arg)
42705d13a37bSArne Jansen {
4271496ad9aaSAl Viro 	struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
42725d13a37bSArne Jansen 	struct btrfs_ioctl_qgroup_assign_args *sa;
42735d13a37bSArne Jansen 	struct btrfs_trans_handle *trans;
42745d13a37bSArne Jansen 	int ret;
42755d13a37bSArne Jansen 	int err;
42765d13a37bSArne Jansen 
42775d13a37bSArne Jansen 	if (!capable(CAP_SYS_ADMIN))
42785d13a37bSArne Jansen 		return -EPERM;
42795d13a37bSArne Jansen 
4280905b0ddaSMiao Xie 	ret = mnt_want_write_file(file);
4281905b0ddaSMiao Xie 	if (ret)
4282905b0ddaSMiao Xie 		return ret;
42835d13a37bSArne Jansen 
42845d13a37bSArne Jansen 	sa = memdup_user(arg, sizeof(*sa));
4285905b0ddaSMiao Xie 	if (IS_ERR(sa)) {
4286905b0ddaSMiao Xie 		ret = PTR_ERR(sa);
4287905b0ddaSMiao Xie 		goto drop_write;
4288905b0ddaSMiao Xie 	}
42895d13a37bSArne Jansen 
42905d13a37bSArne Jansen 	trans = btrfs_join_transaction(root);
42915d13a37bSArne Jansen 	if (IS_ERR(trans)) {
42925d13a37bSArne Jansen 		ret = PTR_ERR(trans);
42935d13a37bSArne Jansen 		goto out;
42945d13a37bSArne Jansen 	}
42955d13a37bSArne Jansen 
42965d13a37bSArne Jansen 	/* FIXME: check if the IDs really exist */
42975d13a37bSArne Jansen 	if (sa->assign) {
42985d13a37bSArne Jansen 		ret = btrfs_add_qgroup_relation(trans, root->fs_info,
42995d13a37bSArne Jansen 						sa->src, sa->dst);
43005d13a37bSArne Jansen 	} else {
43015d13a37bSArne Jansen 		ret = btrfs_del_qgroup_relation(trans, root->fs_info,
43025d13a37bSArne Jansen 						sa->src, sa->dst);
43035d13a37bSArne Jansen 	}
43045d13a37bSArne Jansen 
43055d13a37bSArne Jansen 	err = btrfs_end_transaction(trans, root);
43065d13a37bSArne Jansen 	if (err && !ret)
43075d13a37bSArne Jansen 		ret = err;
43085d13a37bSArne Jansen 
43095d13a37bSArne Jansen out:
43105d13a37bSArne Jansen 	kfree(sa);
4311905b0ddaSMiao Xie drop_write:
4312905b0ddaSMiao Xie 	mnt_drop_write_file(file);
43135d13a37bSArne Jansen 	return ret;
43145d13a37bSArne Jansen }
43155d13a37bSArne Jansen 
4316905b0ddaSMiao Xie static long btrfs_ioctl_qgroup_create(struct file *file, void __user *arg)
43175d13a37bSArne Jansen {
4318496ad9aaSAl Viro 	struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
43195d13a37bSArne Jansen 	struct btrfs_ioctl_qgroup_create_args *sa;
43205d13a37bSArne Jansen 	struct btrfs_trans_handle *trans;
43215d13a37bSArne Jansen 	int ret;
43225d13a37bSArne Jansen 	int err;
43235d13a37bSArne Jansen 
43245d13a37bSArne Jansen 	if (!capable(CAP_SYS_ADMIN))
43255d13a37bSArne Jansen 		return -EPERM;
43265d13a37bSArne Jansen 
4327905b0ddaSMiao Xie 	ret = mnt_want_write_file(file);
4328905b0ddaSMiao Xie 	if (ret)
4329905b0ddaSMiao Xie 		return ret;
43305d13a37bSArne Jansen 
43315d13a37bSArne Jansen 	sa = memdup_user(arg, sizeof(*sa));
4332905b0ddaSMiao Xie 	if (IS_ERR(sa)) {
4333905b0ddaSMiao Xie 		ret = PTR_ERR(sa);
4334905b0ddaSMiao Xie 		goto drop_write;
4335905b0ddaSMiao Xie 	}
43365d13a37bSArne Jansen 
4337d86e56cfSMiao Xie 	if (!sa->qgroupid) {
4338d86e56cfSMiao Xie 		ret = -EINVAL;
4339d86e56cfSMiao Xie 		goto out;
4340d86e56cfSMiao Xie 	}
4341d86e56cfSMiao Xie 
43425d13a37bSArne Jansen 	trans = btrfs_join_transaction(root);
43435d13a37bSArne Jansen 	if (IS_ERR(trans)) {
43445d13a37bSArne Jansen 		ret = PTR_ERR(trans);
43455d13a37bSArne Jansen 		goto out;
43465d13a37bSArne Jansen 	}
43475d13a37bSArne Jansen 
43485d13a37bSArne Jansen 	/* FIXME: check if the IDs really exist */
43495d13a37bSArne Jansen 	if (sa->create) {
43505d13a37bSArne Jansen 		ret = btrfs_create_qgroup(trans, root->fs_info, sa->qgroupid,
43515d13a37bSArne Jansen 					  NULL);
43525d13a37bSArne Jansen 	} else {
43535d13a37bSArne Jansen 		ret = btrfs_remove_qgroup(trans, root->fs_info, sa->qgroupid);
43545d13a37bSArne Jansen 	}
43555d13a37bSArne Jansen 
43565d13a37bSArne Jansen 	err = btrfs_end_transaction(trans, root);
43575d13a37bSArne Jansen 	if (err && !ret)
43585d13a37bSArne Jansen 		ret = err;
43595d13a37bSArne Jansen 
43605d13a37bSArne Jansen out:
43615d13a37bSArne Jansen 	kfree(sa);
4362905b0ddaSMiao Xie drop_write:
4363905b0ddaSMiao Xie 	mnt_drop_write_file(file);
43645d13a37bSArne Jansen 	return ret;
43655d13a37bSArne Jansen }
43665d13a37bSArne Jansen 
4367905b0ddaSMiao Xie static long btrfs_ioctl_qgroup_limit(struct file *file, void __user *arg)
43685d13a37bSArne Jansen {
4369496ad9aaSAl Viro 	struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
43705d13a37bSArne Jansen 	struct btrfs_ioctl_qgroup_limit_args *sa;
43715d13a37bSArne Jansen 	struct btrfs_trans_handle *trans;
43725d13a37bSArne Jansen 	int ret;
43735d13a37bSArne Jansen 	int err;
43745d13a37bSArne Jansen 	u64 qgroupid;
43755d13a37bSArne Jansen 
43765d13a37bSArne Jansen 	if (!capable(CAP_SYS_ADMIN))
43775d13a37bSArne Jansen 		return -EPERM;
43785d13a37bSArne Jansen 
4379905b0ddaSMiao Xie 	ret = mnt_want_write_file(file);
4380905b0ddaSMiao Xie 	if (ret)
4381905b0ddaSMiao Xie 		return ret;
43825d13a37bSArne Jansen 
43835d13a37bSArne Jansen 	sa = memdup_user(arg, sizeof(*sa));
4384905b0ddaSMiao Xie 	if (IS_ERR(sa)) {
4385905b0ddaSMiao Xie 		ret = PTR_ERR(sa);
4386905b0ddaSMiao Xie 		goto drop_write;
4387905b0ddaSMiao Xie 	}
43885d13a37bSArne Jansen 
43895d13a37bSArne Jansen 	trans = btrfs_join_transaction(root);
43905d13a37bSArne Jansen 	if (IS_ERR(trans)) {
43915d13a37bSArne Jansen 		ret = PTR_ERR(trans);
43925d13a37bSArne Jansen 		goto out;
43935d13a37bSArne Jansen 	}
43945d13a37bSArne Jansen 
43955d13a37bSArne Jansen 	qgroupid = sa->qgroupid;
43965d13a37bSArne Jansen 	if (!qgroupid) {
43975d13a37bSArne Jansen 		/* take the current subvol as qgroup */
43985d13a37bSArne Jansen 		qgroupid = root->root_key.objectid;
43995d13a37bSArne Jansen 	}
44005d13a37bSArne Jansen 
44015d13a37bSArne Jansen 	/* FIXME: check if the IDs really exist */
44025d13a37bSArne Jansen 	ret = btrfs_limit_qgroup(trans, root->fs_info, qgroupid, &sa->lim);
44035d13a37bSArne Jansen 
44045d13a37bSArne Jansen 	err = btrfs_end_transaction(trans, root);
44055d13a37bSArne Jansen 	if (err && !ret)
44065d13a37bSArne Jansen 		ret = err;
44075d13a37bSArne Jansen 
44085d13a37bSArne Jansen out:
44095d13a37bSArne Jansen 	kfree(sa);
4410905b0ddaSMiao Xie drop_write:
4411905b0ddaSMiao Xie 	mnt_drop_write_file(file);
44125d13a37bSArne Jansen 	return ret;
44135d13a37bSArne Jansen }
44145d13a37bSArne Jansen 
44152f232036SJan Schmidt static long btrfs_ioctl_quota_rescan(struct file *file, void __user *arg)
44162f232036SJan Schmidt {
44176d0379ecSAl Viro 	struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
44182f232036SJan Schmidt 	struct btrfs_ioctl_quota_rescan_args *qsa;
44192f232036SJan Schmidt 	int ret;
44202f232036SJan Schmidt 
44212f232036SJan Schmidt 	if (!capable(CAP_SYS_ADMIN))
44222f232036SJan Schmidt 		return -EPERM;
44232f232036SJan Schmidt 
44242f232036SJan Schmidt 	ret = mnt_want_write_file(file);
44252f232036SJan Schmidt 	if (ret)
44262f232036SJan Schmidt 		return ret;
44272f232036SJan Schmidt 
44282f232036SJan Schmidt 	qsa = memdup_user(arg, sizeof(*qsa));
44292f232036SJan Schmidt 	if (IS_ERR(qsa)) {
44302f232036SJan Schmidt 		ret = PTR_ERR(qsa);
44312f232036SJan Schmidt 		goto drop_write;
44322f232036SJan Schmidt 	}
44332f232036SJan Schmidt 
44342f232036SJan Schmidt 	if (qsa->flags) {
44352f232036SJan Schmidt 		ret = -EINVAL;
44362f232036SJan Schmidt 		goto out;
44372f232036SJan Schmidt 	}
44382f232036SJan Schmidt 
44392f232036SJan Schmidt 	ret = btrfs_qgroup_rescan(root->fs_info);
44402f232036SJan Schmidt 
44412f232036SJan Schmidt out:
44422f232036SJan Schmidt 	kfree(qsa);
44432f232036SJan Schmidt drop_write:
44442f232036SJan Schmidt 	mnt_drop_write_file(file);
44452f232036SJan Schmidt 	return ret;
44462f232036SJan Schmidt }
44472f232036SJan Schmidt 
44482f232036SJan Schmidt static long btrfs_ioctl_quota_rescan_status(struct file *file, void __user *arg)
44492f232036SJan Schmidt {
44506d0379ecSAl Viro 	struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
44512f232036SJan Schmidt 	struct btrfs_ioctl_quota_rescan_args *qsa;
44522f232036SJan Schmidt 	int ret = 0;
44532f232036SJan Schmidt 
44542f232036SJan Schmidt 	if (!capable(CAP_SYS_ADMIN))
44552f232036SJan Schmidt 		return -EPERM;
44562f232036SJan Schmidt 
44572f232036SJan Schmidt 	qsa = kzalloc(sizeof(*qsa), GFP_NOFS);
44582f232036SJan Schmidt 	if (!qsa)
44592f232036SJan Schmidt 		return -ENOMEM;
44602f232036SJan Schmidt 
44612f232036SJan Schmidt 	if (root->fs_info->qgroup_flags & BTRFS_QGROUP_STATUS_FLAG_RESCAN) {
44622f232036SJan Schmidt 		qsa->flags = 1;
44632f232036SJan Schmidt 		qsa->progress = root->fs_info->qgroup_rescan_progress.objectid;
44642f232036SJan Schmidt 	}
44652f232036SJan Schmidt 
44662f232036SJan Schmidt 	if (copy_to_user(arg, qsa, sizeof(*qsa)))
44672f232036SJan Schmidt 		ret = -EFAULT;
44682f232036SJan Schmidt 
44692f232036SJan Schmidt 	kfree(qsa);
44702f232036SJan Schmidt 	return ret;
44712f232036SJan Schmidt }
44722f232036SJan Schmidt 
447357254b6eSJan Schmidt static long btrfs_ioctl_quota_rescan_wait(struct file *file, void __user *arg)
447457254b6eSJan Schmidt {
447554563d41SAl Viro 	struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
447657254b6eSJan Schmidt 
447757254b6eSJan Schmidt 	if (!capable(CAP_SYS_ADMIN))
447857254b6eSJan Schmidt 		return -EPERM;
447957254b6eSJan Schmidt 
448057254b6eSJan Schmidt 	return btrfs_qgroup_wait_for_completion(root->fs_info);
448157254b6eSJan Schmidt }
448257254b6eSJan Schmidt 
4483abccd00fSHugo Mills static long _btrfs_ioctl_set_received_subvol(struct file *file,
4484abccd00fSHugo Mills 					    struct btrfs_ioctl_received_subvol_args *sa)
44858ea05e3aSAlexander Block {
4486496ad9aaSAl Viro 	struct inode *inode = file_inode(file);
44878ea05e3aSAlexander Block 	struct btrfs_root *root = BTRFS_I(inode)->root;
44888ea05e3aSAlexander Block 	struct btrfs_root_item *root_item = &root->root_item;
44898ea05e3aSAlexander Block 	struct btrfs_trans_handle *trans;
44908ea05e3aSAlexander Block 	struct timespec ct = CURRENT_TIME;
44918ea05e3aSAlexander Block 	int ret = 0;
4492dd5f9615SStefan Behrens 	int received_uuid_changed;
44938ea05e3aSAlexander Block 
4494bd60ea0fSDavid Sterba 	if (!inode_owner_or_capable(inode))
4495bd60ea0fSDavid Sterba 		return -EPERM;
4496bd60ea0fSDavid Sterba 
44978ea05e3aSAlexander Block 	ret = mnt_want_write_file(file);
44988ea05e3aSAlexander Block 	if (ret < 0)
44998ea05e3aSAlexander Block 		return ret;
45008ea05e3aSAlexander Block 
45018ea05e3aSAlexander Block 	down_write(&root->fs_info->subvol_sem);
45028ea05e3aSAlexander Block 
45038ea05e3aSAlexander Block 	if (btrfs_ino(inode) != BTRFS_FIRST_FREE_OBJECTID) {
45048ea05e3aSAlexander Block 		ret = -EINVAL;
45058ea05e3aSAlexander Block 		goto out;
45068ea05e3aSAlexander Block 	}
45078ea05e3aSAlexander Block 
45088ea05e3aSAlexander Block 	if (btrfs_root_readonly(root)) {
45098ea05e3aSAlexander Block 		ret = -EROFS;
45108ea05e3aSAlexander Block 		goto out;
45118ea05e3aSAlexander Block 	}
45128ea05e3aSAlexander Block 
4513dd5f9615SStefan Behrens 	/*
4514dd5f9615SStefan Behrens 	 * 1 - root item
4515dd5f9615SStefan Behrens 	 * 2 - uuid items (received uuid + subvol uuid)
4516dd5f9615SStefan Behrens 	 */
4517dd5f9615SStefan Behrens 	trans = btrfs_start_transaction(root, 3);
45188ea05e3aSAlexander Block 	if (IS_ERR(trans)) {
45198ea05e3aSAlexander Block 		ret = PTR_ERR(trans);
45208ea05e3aSAlexander Block 		trans = NULL;
45218ea05e3aSAlexander Block 		goto out;
45228ea05e3aSAlexander Block 	}
45238ea05e3aSAlexander Block 
45248ea05e3aSAlexander Block 	sa->rtransid = trans->transid;
45258ea05e3aSAlexander Block 	sa->rtime.sec = ct.tv_sec;
45268ea05e3aSAlexander Block 	sa->rtime.nsec = ct.tv_nsec;
45278ea05e3aSAlexander Block 
4528dd5f9615SStefan Behrens 	received_uuid_changed = memcmp(root_item->received_uuid, sa->uuid,
4529dd5f9615SStefan Behrens 				       BTRFS_UUID_SIZE);
4530dd5f9615SStefan Behrens 	if (received_uuid_changed &&
4531dd5f9615SStefan Behrens 	    !btrfs_is_empty_uuid(root_item->received_uuid))
4532dd5f9615SStefan Behrens 		btrfs_uuid_tree_rem(trans, root->fs_info->uuid_root,
4533dd5f9615SStefan Behrens 				    root_item->received_uuid,
4534dd5f9615SStefan Behrens 				    BTRFS_UUID_KEY_RECEIVED_SUBVOL,
4535dd5f9615SStefan Behrens 				    root->root_key.objectid);
45368ea05e3aSAlexander Block 	memcpy(root_item->received_uuid, sa->uuid, BTRFS_UUID_SIZE);
45378ea05e3aSAlexander Block 	btrfs_set_root_stransid(root_item, sa->stransid);
45388ea05e3aSAlexander Block 	btrfs_set_root_rtransid(root_item, sa->rtransid);
45393cae210fSQu Wenruo 	btrfs_set_stack_timespec_sec(&root_item->stime, sa->stime.sec);
45403cae210fSQu Wenruo 	btrfs_set_stack_timespec_nsec(&root_item->stime, sa->stime.nsec);
45413cae210fSQu Wenruo 	btrfs_set_stack_timespec_sec(&root_item->rtime, sa->rtime.sec);
45423cae210fSQu Wenruo 	btrfs_set_stack_timespec_nsec(&root_item->rtime, sa->rtime.nsec);
45438ea05e3aSAlexander Block 
45448ea05e3aSAlexander Block 	ret = btrfs_update_root(trans, root->fs_info->tree_root,
45458ea05e3aSAlexander Block 				&root->root_key, &root->root_item);
45468ea05e3aSAlexander Block 	if (ret < 0) {
45478ea05e3aSAlexander Block 		btrfs_end_transaction(trans, root);
45488ea05e3aSAlexander Block 		goto out;
4549dd5f9615SStefan Behrens 	}
4550dd5f9615SStefan Behrens 	if (received_uuid_changed && !btrfs_is_empty_uuid(sa->uuid)) {
4551dd5f9615SStefan Behrens 		ret = btrfs_uuid_tree_add(trans, root->fs_info->uuid_root,
4552dd5f9615SStefan Behrens 					  sa->uuid,
4553dd5f9615SStefan Behrens 					  BTRFS_UUID_KEY_RECEIVED_SUBVOL,
4554dd5f9615SStefan Behrens 					  root->root_key.objectid);
4555dd5f9615SStefan Behrens 		if (ret < 0 && ret != -EEXIST) {
4556dd5f9615SStefan Behrens 			btrfs_abort_transaction(trans, root, ret);
4557dd5f9615SStefan Behrens 			goto out;
4558dd5f9615SStefan Behrens 		}
4559dd5f9615SStefan Behrens 	}
45608ea05e3aSAlexander Block 	ret = btrfs_commit_transaction(trans, root);
4561dd5f9615SStefan Behrens 	if (ret < 0) {
4562dd5f9615SStefan Behrens 		btrfs_abort_transaction(trans, root, ret);
45638ea05e3aSAlexander Block 		goto out;
45648ea05e3aSAlexander Block 	}
45658ea05e3aSAlexander Block 
4566abccd00fSHugo Mills out:
4567abccd00fSHugo Mills 	up_write(&root->fs_info->subvol_sem);
4568abccd00fSHugo Mills 	mnt_drop_write_file(file);
4569abccd00fSHugo Mills 	return ret;
4570abccd00fSHugo Mills }
4571abccd00fSHugo Mills 
4572abccd00fSHugo Mills #ifdef CONFIG_64BIT
4573abccd00fSHugo Mills static long btrfs_ioctl_set_received_subvol_32(struct file *file,
4574abccd00fSHugo Mills 						void __user *arg)
4575abccd00fSHugo Mills {
4576abccd00fSHugo Mills 	struct btrfs_ioctl_received_subvol_args_32 *args32 = NULL;
4577abccd00fSHugo Mills 	struct btrfs_ioctl_received_subvol_args *args64 = NULL;
4578abccd00fSHugo Mills 	int ret = 0;
4579abccd00fSHugo Mills 
4580abccd00fSHugo Mills 	args32 = memdup_user(arg, sizeof(*args32));
4581abccd00fSHugo Mills 	if (IS_ERR(args32)) {
4582abccd00fSHugo Mills 		ret = PTR_ERR(args32);
4583abccd00fSHugo Mills 		args32 = NULL;
4584abccd00fSHugo Mills 		goto out;
4585abccd00fSHugo Mills 	}
4586abccd00fSHugo Mills 
4587abccd00fSHugo Mills 	args64 = kmalloc(sizeof(*args64), GFP_NOFS);
458884dbeb87SDan Carpenter 	if (!args64) {
458984dbeb87SDan Carpenter 		ret = -ENOMEM;
4590abccd00fSHugo Mills 		goto out;
4591abccd00fSHugo Mills 	}
4592abccd00fSHugo Mills 
4593abccd00fSHugo Mills 	memcpy(args64->uuid, args32->uuid, BTRFS_UUID_SIZE);
4594abccd00fSHugo Mills 	args64->stransid = args32->stransid;
4595abccd00fSHugo Mills 	args64->rtransid = args32->rtransid;
4596abccd00fSHugo Mills 	args64->stime.sec = args32->stime.sec;
4597abccd00fSHugo Mills 	args64->stime.nsec = args32->stime.nsec;
4598abccd00fSHugo Mills 	args64->rtime.sec = args32->rtime.sec;
4599abccd00fSHugo Mills 	args64->rtime.nsec = args32->rtime.nsec;
4600abccd00fSHugo Mills 	args64->flags = args32->flags;
4601abccd00fSHugo Mills 
4602abccd00fSHugo Mills 	ret = _btrfs_ioctl_set_received_subvol(file, args64);
4603abccd00fSHugo Mills 	if (ret)
4604abccd00fSHugo Mills 		goto out;
4605abccd00fSHugo Mills 
4606abccd00fSHugo Mills 	memcpy(args32->uuid, args64->uuid, BTRFS_UUID_SIZE);
4607abccd00fSHugo Mills 	args32->stransid = args64->stransid;
4608abccd00fSHugo Mills 	args32->rtransid = args64->rtransid;
4609abccd00fSHugo Mills 	args32->stime.sec = args64->stime.sec;
4610abccd00fSHugo Mills 	args32->stime.nsec = args64->stime.nsec;
4611abccd00fSHugo Mills 	args32->rtime.sec = args64->rtime.sec;
4612abccd00fSHugo Mills 	args32->rtime.nsec = args64->rtime.nsec;
4613abccd00fSHugo Mills 	args32->flags = args64->flags;
4614abccd00fSHugo Mills 
4615abccd00fSHugo Mills 	ret = copy_to_user(arg, args32, sizeof(*args32));
4616abccd00fSHugo Mills 	if (ret)
4617abccd00fSHugo Mills 		ret = -EFAULT;
4618abccd00fSHugo Mills 
4619abccd00fSHugo Mills out:
4620abccd00fSHugo Mills 	kfree(args32);
4621abccd00fSHugo Mills 	kfree(args64);
4622abccd00fSHugo Mills 	return ret;
4623abccd00fSHugo Mills }
4624abccd00fSHugo Mills #endif
4625abccd00fSHugo Mills 
4626abccd00fSHugo Mills static long btrfs_ioctl_set_received_subvol(struct file *file,
4627abccd00fSHugo Mills 					    void __user *arg)
4628abccd00fSHugo Mills {
4629abccd00fSHugo Mills 	struct btrfs_ioctl_received_subvol_args *sa = NULL;
4630abccd00fSHugo Mills 	int ret = 0;
4631abccd00fSHugo Mills 
4632abccd00fSHugo Mills 	sa = memdup_user(arg, sizeof(*sa));
4633abccd00fSHugo Mills 	if (IS_ERR(sa)) {
4634abccd00fSHugo Mills 		ret = PTR_ERR(sa);
4635abccd00fSHugo Mills 		sa = NULL;
4636abccd00fSHugo Mills 		goto out;
4637abccd00fSHugo Mills 	}
4638abccd00fSHugo Mills 
4639abccd00fSHugo Mills 	ret = _btrfs_ioctl_set_received_subvol(file, sa);
4640abccd00fSHugo Mills 
4641abccd00fSHugo Mills 	if (ret)
4642abccd00fSHugo Mills 		goto out;
4643abccd00fSHugo Mills 
46448ea05e3aSAlexander Block 	ret = copy_to_user(arg, sa, sizeof(*sa));
46458ea05e3aSAlexander Block 	if (ret)
46468ea05e3aSAlexander Block 		ret = -EFAULT;
46478ea05e3aSAlexander Block 
46488ea05e3aSAlexander Block out:
46498ea05e3aSAlexander Block 	kfree(sa);
46508ea05e3aSAlexander Block 	return ret;
46518ea05e3aSAlexander Block }
46528ea05e3aSAlexander Block 
4653867ab667Sjeff.liu static int btrfs_ioctl_get_fslabel(struct file *file, void __user *arg)
4654867ab667Sjeff.liu {
46556d0379ecSAl Viro 	struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
4656a1b83ac5SAnand Jain 	size_t len;
4657867ab667Sjeff.liu 	int ret;
4658a1b83ac5SAnand Jain 	char label[BTRFS_LABEL_SIZE];
4659a1b83ac5SAnand Jain 
4660a1b83ac5SAnand Jain 	spin_lock(&root->fs_info->super_lock);
4661a1b83ac5SAnand Jain 	memcpy(label, root->fs_info->super_copy->label, BTRFS_LABEL_SIZE);
4662a1b83ac5SAnand Jain 	spin_unlock(&root->fs_info->super_lock);
4663a1b83ac5SAnand Jain 
4664a1b83ac5SAnand Jain 	len = strnlen(label, BTRFS_LABEL_SIZE);
4665867ab667Sjeff.liu 
4666867ab667Sjeff.liu 	if (len == BTRFS_LABEL_SIZE) {
4667efe120a0SFrank Holton 		btrfs_warn(root->fs_info,
4668efe120a0SFrank Holton 			"label is too long, return the first %zu bytes", --len);
4669867ab667Sjeff.liu 	}
4670867ab667Sjeff.liu 
4671867ab667Sjeff.liu 	ret = copy_to_user(arg, label, len);
4672867ab667Sjeff.liu 
4673867ab667Sjeff.liu 	return ret ? -EFAULT : 0;
4674867ab667Sjeff.liu }
4675867ab667Sjeff.liu 
4676a8bfd4abSjeff.liu static int btrfs_ioctl_set_fslabel(struct file *file, void __user *arg)
4677a8bfd4abSjeff.liu {
46786d0379ecSAl Viro 	struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
4679a8bfd4abSjeff.liu 	struct btrfs_super_block *super_block = root->fs_info->super_copy;
4680a8bfd4abSjeff.liu 	struct btrfs_trans_handle *trans;
4681a8bfd4abSjeff.liu 	char label[BTRFS_LABEL_SIZE];
4682a8bfd4abSjeff.liu 	int ret;
4683a8bfd4abSjeff.liu 
4684a8bfd4abSjeff.liu 	if (!capable(CAP_SYS_ADMIN))
4685a8bfd4abSjeff.liu 		return -EPERM;
4686a8bfd4abSjeff.liu 
4687a8bfd4abSjeff.liu 	if (copy_from_user(label, arg, sizeof(label)))
4688a8bfd4abSjeff.liu 		return -EFAULT;
4689a8bfd4abSjeff.liu 
4690a8bfd4abSjeff.liu 	if (strnlen(label, BTRFS_LABEL_SIZE) == BTRFS_LABEL_SIZE) {
4691efe120a0SFrank Holton 		btrfs_err(root->fs_info, "unable to set label with more than %d bytes",
4692a8bfd4abSjeff.liu 		       BTRFS_LABEL_SIZE - 1);
4693a8bfd4abSjeff.liu 		return -EINVAL;
4694a8bfd4abSjeff.liu 	}
4695a8bfd4abSjeff.liu 
4696a8bfd4abSjeff.liu 	ret = mnt_want_write_file(file);
4697a8bfd4abSjeff.liu 	if (ret)
4698a8bfd4abSjeff.liu 		return ret;
4699a8bfd4abSjeff.liu 
4700a8bfd4abSjeff.liu 	trans = btrfs_start_transaction(root, 0);
4701a8bfd4abSjeff.liu 	if (IS_ERR(trans)) {
4702a8bfd4abSjeff.liu 		ret = PTR_ERR(trans);
4703a8bfd4abSjeff.liu 		goto out_unlock;
4704a8bfd4abSjeff.liu 	}
4705a8bfd4abSjeff.liu 
4706a1b83ac5SAnand Jain 	spin_lock(&root->fs_info->super_lock);
4707a8bfd4abSjeff.liu 	strcpy(super_block->label, label);
4708a1b83ac5SAnand Jain 	spin_unlock(&root->fs_info->super_lock);
4709d0270acaSJeff Mahoney 	ret = btrfs_commit_transaction(trans, root);
4710a8bfd4abSjeff.liu 
4711a8bfd4abSjeff.liu out_unlock:
4712a8bfd4abSjeff.liu 	mnt_drop_write_file(file);
4713a8bfd4abSjeff.liu 	return ret;
4714a8bfd4abSjeff.liu }
4715a8bfd4abSjeff.liu 
47162eaa055fSJeff Mahoney #define INIT_FEATURE_FLAGS(suffix) \
47172eaa055fSJeff Mahoney 	{ .compat_flags = BTRFS_FEATURE_COMPAT_##suffix, \
47182eaa055fSJeff Mahoney 	  .compat_ro_flags = BTRFS_FEATURE_COMPAT_RO_##suffix, \
47192eaa055fSJeff Mahoney 	  .incompat_flags = BTRFS_FEATURE_INCOMPAT_##suffix }
47202eaa055fSJeff Mahoney 
47212eaa055fSJeff Mahoney static int btrfs_ioctl_get_supported_features(struct file *file,
47222eaa055fSJeff Mahoney 					      void __user *arg)
47232eaa055fSJeff Mahoney {
47242eaa055fSJeff Mahoney 	static struct btrfs_ioctl_feature_flags features[3] = {
47252eaa055fSJeff Mahoney 		INIT_FEATURE_FLAGS(SUPP),
47262eaa055fSJeff Mahoney 		INIT_FEATURE_FLAGS(SAFE_SET),
47272eaa055fSJeff Mahoney 		INIT_FEATURE_FLAGS(SAFE_CLEAR)
47282eaa055fSJeff Mahoney 	};
47292eaa055fSJeff Mahoney 
47302eaa055fSJeff Mahoney 	if (copy_to_user(arg, &features, sizeof(features)))
47312eaa055fSJeff Mahoney 		return -EFAULT;
47322eaa055fSJeff Mahoney 
47332eaa055fSJeff Mahoney 	return 0;
47342eaa055fSJeff Mahoney }
47352eaa055fSJeff Mahoney 
47362eaa055fSJeff Mahoney static int btrfs_ioctl_get_features(struct file *file, void __user *arg)
47372eaa055fSJeff Mahoney {
47382eaa055fSJeff Mahoney 	struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
47392eaa055fSJeff Mahoney 	struct btrfs_super_block *super_block = root->fs_info->super_copy;
47402eaa055fSJeff Mahoney 	struct btrfs_ioctl_feature_flags features;
47412eaa055fSJeff Mahoney 
47422eaa055fSJeff Mahoney 	features.compat_flags = btrfs_super_compat_flags(super_block);
47432eaa055fSJeff Mahoney 	features.compat_ro_flags = btrfs_super_compat_ro_flags(super_block);
47442eaa055fSJeff Mahoney 	features.incompat_flags = btrfs_super_incompat_flags(super_block);
47452eaa055fSJeff Mahoney 
47462eaa055fSJeff Mahoney 	if (copy_to_user(arg, &features, sizeof(features)))
47472eaa055fSJeff Mahoney 		return -EFAULT;
47482eaa055fSJeff Mahoney 
47492eaa055fSJeff Mahoney 	return 0;
47502eaa055fSJeff Mahoney }
47512eaa055fSJeff Mahoney 
47523b02a68aSJeff Mahoney static int check_feature_bits(struct btrfs_root *root,
47533b02a68aSJeff Mahoney 			      enum btrfs_feature_set set,
47542eaa055fSJeff Mahoney 			      u64 change_mask, u64 flags, u64 supported_flags,
47552eaa055fSJeff Mahoney 			      u64 safe_set, u64 safe_clear)
47562eaa055fSJeff Mahoney {
47573b02a68aSJeff Mahoney 	const char *type = btrfs_feature_set_names[set];
47583b02a68aSJeff Mahoney 	char *names;
47592eaa055fSJeff Mahoney 	u64 disallowed, unsupported;
47602eaa055fSJeff Mahoney 	u64 set_mask = flags & change_mask;
47612eaa055fSJeff Mahoney 	u64 clear_mask = ~flags & change_mask;
47622eaa055fSJeff Mahoney 
47632eaa055fSJeff Mahoney 	unsupported = set_mask & ~supported_flags;
47642eaa055fSJeff Mahoney 	if (unsupported) {
47653b02a68aSJeff Mahoney 		names = btrfs_printable_features(set, unsupported);
47663b02a68aSJeff Mahoney 		if (names) {
47673b02a68aSJeff Mahoney 			btrfs_warn(root->fs_info,
47683b02a68aSJeff Mahoney 			   "this kernel does not support the %s feature bit%s",
47693b02a68aSJeff Mahoney 			   names, strchr(names, ',') ? "s" : "");
47703b02a68aSJeff Mahoney 			kfree(names);
47713b02a68aSJeff Mahoney 		} else
47722eaa055fSJeff Mahoney 			btrfs_warn(root->fs_info,
47732eaa055fSJeff Mahoney 			   "this kernel does not support %s bits 0x%llx",
47742eaa055fSJeff Mahoney 			   type, unsupported);
47752eaa055fSJeff Mahoney 		return -EOPNOTSUPP;
47762eaa055fSJeff Mahoney 	}
47772eaa055fSJeff Mahoney 
47782eaa055fSJeff Mahoney 	disallowed = set_mask & ~safe_set;
47792eaa055fSJeff Mahoney 	if (disallowed) {
47803b02a68aSJeff Mahoney 		names = btrfs_printable_features(set, disallowed);
47813b02a68aSJeff Mahoney 		if (names) {
47823b02a68aSJeff Mahoney 			btrfs_warn(root->fs_info,
47833b02a68aSJeff Mahoney 			   "can't set the %s feature bit%s while mounted",
47843b02a68aSJeff Mahoney 			   names, strchr(names, ',') ? "s" : "");
47853b02a68aSJeff Mahoney 			kfree(names);
47863b02a68aSJeff Mahoney 		} else
47872eaa055fSJeff Mahoney 			btrfs_warn(root->fs_info,
47882eaa055fSJeff Mahoney 			   "can't set %s bits 0x%llx while mounted",
47892eaa055fSJeff Mahoney 			   type, disallowed);
47902eaa055fSJeff Mahoney 		return -EPERM;
47912eaa055fSJeff Mahoney 	}
47922eaa055fSJeff Mahoney 
47932eaa055fSJeff Mahoney 	disallowed = clear_mask & ~safe_clear;
47942eaa055fSJeff Mahoney 	if (disallowed) {
47953b02a68aSJeff Mahoney 		names = btrfs_printable_features(set, disallowed);
47963b02a68aSJeff Mahoney 		if (names) {
47973b02a68aSJeff Mahoney 			btrfs_warn(root->fs_info,
47983b02a68aSJeff Mahoney 			   "can't clear the %s feature bit%s while mounted",
47993b02a68aSJeff Mahoney 			   names, strchr(names, ',') ? "s" : "");
48003b02a68aSJeff Mahoney 			kfree(names);
48013b02a68aSJeff Mahoney 		} else
48022eaa055fSJeff Mahoney 			btrfs_warn(root->fs_info,
48032eaa055fSJeff Mahoney 			   "can't clear %s bits 0x%llx while mounted",
48042eaa055fSJeff Mahoney 			   type, disallowed);
48052eaa055fSJeff Mahoney 		return -EPERM;
48062eaa055fSJeff Mahoney 	}
48072eaa055fSJeff Mahoney 
48082eaa055fSJeff Mahoney 	return 0;
48092eaa055fSJeff Mahoney }
48102eaa055fSJeff Mahoney 
48112eaa055fSJeff Mahoney #define check_feature(root, change_mask, flags, mask_base)	\
48123b02a68aSJeff Mahoney check_feature_bits(root, FEAT_##mask_base, change_mask, flags,	\
48132eaa055fSJeff Mahoney 		   BTRFS_FEATURE_ ## mask_base ## _SUPP,	\
48142eaa055fSJeff Mahoney 		   BTRFS_FEATURE_ ## mask_base ## _SAFE_SET,	\
48152eaa055fSJeff Mahoney 		   BTRFS_FEATURE_ ## mask_base ## _SAFE_CLEAR)
48162eaa055fSJeff Mahoney 
48172eaa055fSJeff Mahoney static int btrfs_ioctl_set_features(struct file *file, void __user *arg)
48182eaa055fSJeff Mahoney {
48192eaa055fSJeff Mahoney 	struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
48202eaa055fSJeff Mahoney 	struct btrfs_super_block *super_block = root->fs_info->super_copy;
48212eaa055fSJeff Mahoney 	struct btrfs_ioctl_feature_flags flags[2];
48222eaa055fSJeff Mahoney 	struct btrfs_trans_handle *trans;
48232eaa055fSJeff Mahoney 	u64 newflags;
48242eaa055fSJeff Mahoney 	int ret;
48252eaa055fSJeff Mahoney 
48262eaa055fSJeff Mahoney 	if (!capable(CAP_SYS_ADMIN))
48272eaa055fSJeff Mahoney 		return -EPERM;
48282eaa055fSJeff Mahoney 
48292eaa055fSJeff Mahoney 	if (copy_from_user(flags, arg, sizeof(flags)))
48302eaa055fSJeff Mahoney 		return -EFAULT;
48312eaa055fSJeff Mahoney 
48322eaa055fSJeff Mahoney 	/* Nothing to do */
48332eaa055fSJeff Mahoney 	if (!flags[0].compat_flags && !flags[0].compat_ro_flags &&
48342eaa055fSJeff Mahoney 	    !flags[0].incompat_flags)
48352eaa055fSJeff Mahoney 		return 0;
48362eaa055fSJeff Mahoney 
48372eaa055fSJeff Mahoney 	ret = check_feature(root, flags[0].compat_flags,
48382eaa055fSJeff Mahoney 			    flags[1].compat_flags, COMPAT);
48392eaa055fSJeff Mahoney 	if (ret)
48402eaa055fSJeff Mahoney 		return ret;
48412eaa055fSJeff Mahoney 
48422eaa055fSJeff Mahoney 	ret = check_feature(root, flags[0].compat_ro_flags,
48432eaa055fSJeff Mahoney 			    flags[1].compat_ro_flags, COMPAT_RO);
48442eaa055fSJeff Mahoney 	if (ret)
48452eaa055fSJeff Mahoney 		return ret;
48462eaa055fSJeff Mahoney 
48472eaa055fSJeff Mahoney 	ret = check_feature(root, flags[0].incompat_flags,
48482eaa055fSJeff Mahoney 			    flags[1].incompat_flags, INCOMPAT);
48492eaa055fSJeff Mahoney 	if (ret)
48502eaa055fSJeff Mahoney 		return ret;
48512eaa055fSJeff Mahoney 
48528051aa1aSDavid Sterba 	trans = btrfs_start_transaction(root, 0);
48532eaa055fSJeff Mahoney 	if (IS_ERR(trans))
48542eaa055fSJeff Mahoney 		return PTR_ERR(trans);
48552eaa055fSJeff Mahoney 
48562eaa055fSJeff Mahoney 	spin_lock(&root->fs_info->super_lock);
48572eaa055fSJeff Mahoney 	newflags = btrfs_super_compat_flags(super_block);
48582eaa055fSJeff Mahoney 	newflags |= flags[0].compat_flags & flags[1].compat_flags;
48592eaa055fSJeff Mahoney 	newflags &= ~(flags[0].compat_flags & ~flags[1].compat_flags);
48602eaa055fSJeff Mahoney 	btrfs_set_super_compat_flags(super_block, newflags);
48612eaa055fSJeff Mahoney 
48622eaa055fSJeff Mahoney 	newflags = btrfs_super_compat_ro_flags(super_block);
48632eaa055fSJeff Mahoney 	newflags |= flags[0].compat_ro_flags & flags[1].compat_ro_flags;
48642eaa055fSJeff Mahoney 	newflags &= ~(flags[0].compat_ro_flags & ~flags[1].compat_ro_flags);
48652eaa055fSJeff Mahoney 	btrfs_set_super_compat_ro_flags(super_block, newflags);
48662eaa055fSJeff Mahoney 
48672eaa055fSJeff Mahoney 	newflags = btrfs_super_incompat_flags(super_block);
48682eaa055fSJeff Mahoney 	newflags |= flags[0].incompat_flags & flags[1].incompat_flags;
48692eaa055fSJeff Mahoney 	newflags &= ~(flags[0].incompat_flags & ~flags[1].incompat_flags);
48702eaa055fSJeff Mahoney 	btrfs_set_super_incompat_flags(super_block, newflags);
48712eaa055fSJeff Mahoney 	spin_unlock(&root->fs_info->super_lock);
48722eaa055fSJeff Mahoney 
4873d0270acaSJeff Mahoney 	return btrfs_commit_transaction(trans, root);
48742eaa055fSJeff Mahoney }
48752eaa055fSJeff Mahoney 
4876f46b5a66SChristoph Hellwig long btrfs_ioctl(struct file *file, unsigned int
4877f46b5a66SChristoph Hellwig 		cmd, unsigned long arg)
4878f46b5a66SChristoph Hellwig {
4879496ad9aaSAl Viro 	struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
48804bcabaa3SChristoph Hellwig 	void __user *argp = (void __user *)arg;
4881f46b5a66SChristoph Hellwig 
4882f46b5a66SChristoph Hellwig 	switch (cmd) {
48836cbff00fSChristoph Hellwig 	case FS_IOC_GETFLAGS:
48846cbff00fSChristoph Hellwig 		return btrfs_ioctl_getflags(file, argp);
48856cbff00fSChristoph Hellwig 	case FS_IOC_SETFLAGS:
48866cbff00fSChristoph Hellwig 		return btrfs_ioctl_setflags(file, argp);
48876cbff00fSChristoph Hellwig 	case FS_IOC_GETVERSION:
48886cbff00fSChristoph Hellwig 		return btrfs_ioctl_getversion(file, argp);
4889f7039b1dSLi Dongyang 	case FITRIM:
4890f7039b1dSLi Dongyang 		return btrfs_ioctl_fitrim(file, argp);
4891f46b5a66SChristoph Hellwig 	case BTRFS_IOC_SNAP_CREATE:
4892fa0d2b9bSLi Zefan 		return btrfs_ioctl_snap_create(file, argp, 0);
4893fdfb1e4fSLi Zefan 	case BTRFS_IOC_SNAP_CREATE_V2:
4894fa0d2b9bSLi Zefan 		return btrfs_ioctl_snap_create_v2(file, argp, 0);
48953de4586cSChris Mason 	case BTRFS_IOC_SUBVOL_CREATE:
4896fa0d2b9bSLi Zefan 		return btrfs_ioctl_snap_create(file, argp, 1);
48976f72c7e2SArne Jansen 	case BTRFS_IOC_SUBVOL_CREATE_V2:
48986f72c7e2SArne Jansen 		return btrfs_ioctl_snap_create_v2(file, argp, 1);
489976dda93cSYan, Zheng 	case BTRFS_IOC_SNAP_DESTROY:
490076dda93cSYan, Zheng 		return btrfs_ioctl_snap_destroy(file, argp);
49010caa102dSLi Zefan 	case BTRFS_IOC_SUBVOL_GETFLAGS:
49020caa102dSLi Zefan 		return btrfs_ioctl_subvol_getflags(file, argp);
49030caa102dSLi Zefan 	case BTRFS_IOC_SUBVOL_SETFLAGS:
49040caa102dSLi Zefan 		return btrfs_ioctl_subvol_setflags(file, argp);
49056ef5ed0dSJosef Bacik 	case BTRFS_IOC_DEFAULT_SUBVOL:
49066ef5ed0dSJosef Bacik 		return btrfs_ioctl_default_subvol(file, argp);
4907f46b5a66SChristoph Hellwig 	case BTRFS_IOC_DEFRAG:
49081e701a32SChris Mason 		return btrfs_ioctl_defrag(file, NULL);
49091e701a32SChris Mason 	case BTRFS_IOC_DEFRAG_RANGE:
49101e701a32SChris Mason 		return btrfs_ioctl_defrag(file, argp);
4911f46b5a66SChristoph Hellwig 	case BTRFS_IOC_RESIZE:
4912198605a8SMiao Xie 		return btrfs_ioctl_resize(file, argp);
4913f46b5a66SChristoph Hellwig 	case BTRFS_IOC_ADD_DEV:
49144bcabaa3SChristoph Hellwig 		return btrfs_ioctl_add_dev(root, argp);
4915f46b5a66SChristoph Hellwig 	case BTRFS_IOC_RM_DEV:
4916da24927bSMiao Xie 		return btrfs_ioctl_rm_dev(file, argp);
4917475f6387SJan Schmidt 	case BTRFS_IOC_FS_INFO:
4918475f6387SJan Schmidt 		return btrfs_ioctl_fs_info(root, argp);
4919475f6387SJan Schmidt 	case BTRFS_IOC_DEV_INFO:
4920475f6387SJan Schmidt 		return btrfs_ioctl_dev_info(root, argp);
4921f46b5a66SChristoph Hellwig 	case BTRFS_IOC_BALANCE:
49229ba1f6e4SLiu Bo 		return btrfs_ioctl_balance(file, NULL);
4923f46b5a66SChristoph Hellwig 	case BTRFS_IOC_CLONE:
4924c5c9cd4dSSage Weil 		return btrfs_ioctl_clone(file, arg, 0, 0, 0);
4925c5c9cd4dSSage Weil 	case BTRFS_IOC_CLONE_RANGE:
49267a865e8aSChristoph Hellwig 		return btrfs_ioctl_clone_range(file, argp);
4927f46b5a66SChristoph Hellwig 	case BTRFS_IOC_TRANS_START:
4928f46b5a66SChristoph Hellwig 		return btrfs_ioctl_trans_start(file);
4929f46b5a66SChristoph Hellwig 	case BTRFS_IOC_TRANS_END:
4930f46b5a66SChristoph Hellwig 		return btrfs_ioctl_trans_end(file);
4931ac8e9819SChris Mason 	case BTRFS_IOC_TREE_SEARCH:
4932ac8e9819SChris Mason 		return btrfs_ioctl_tree_search(file, argp);
4933ac8e9819SChris Mason 	case BTRFS_IOC_INO_LOOKUP:
4934ac8e9819SChris Mason 		return btrfs_ioctl_ino_lookup(file, argp);
4935d7728c96SJan Schmidt 	case BTRFS_IOC_INO_PATHS:
4936d7728c96SJan Schmidt 		return btrfs_ioctl_ino_to_path(root, argp);
4937d7728c96SJan Schmidt 	case BTRFS_IOC_LOGICAL_INO:
4938d7728c96SJan Schmidt 		return btrfs_ioctl_logical_to_ino(root, argp);
49391406e432SJosef Bacik 	case BTRFS_IOC_SPACE_INFO:
49401406e432SJosef Bacik 		return btrfs_ioctl_space_info(root, argp);
49419b199859SFilipe David Borba Manana 	case BTRFS_IOC_SYNC: {
49429b199859SFilipe David Borba Manana 		int ret;
49439b199859SFilipe David Borba Manana 
49446c255e67SMiao Xie 		ret = btrfs_start_delalloc_roots(root->fs_info, 0, -1);
49459b199859SFilipe David Borba Manana 		if (ret)
49469b199859SFilipe David Borba Manana 			return ret;
49479b199859SFilipe David Borba Manana 		ret = btrfs_sync_fs(file->f_dentry->d_sb, 1);
49489b199859SFilipe David Borba Manana 		return ret;
49499b199859SFilipe David Borba Manana 	}
495046204592SSage Weil 	case BTRFS_IOC_START_SYNC:
49519a8c28beSMiao Xie 		return btrfs_ioctl_start_sync(root, argp);
495246204592SSage Weil 	case BTRFS_IOC_WAIT_SYNC:
49539a8c28beSMiao Xie 		return btrfs_ioctl_wait_sync(root, argp);
4954475f6387SJan Schmidt 	case BTRFS_IOC_SCRUB:
4955b8e95489SMiao Xie 		return btrfs_ioctl_scrub(file, argp);
4956475f6387SJan Schmidt 	case BTRFS_IOC_SCRUB_CANCEL:
4957475f6387SJan Schmidt 		return btrfs_ioctl_scrub_cancel(root, argp);
4958475f6387SJan Schmidt 	case BTRFS_IOC_SCRUB_PROGRESS:
4959475f6387SJan Schmidt 		return btrfs_ioctl_scrub_progress(root, argp);
4960c9e9f97bSIlya Dryomov 	case BTRFS_IOC_BALANCE_V2:
49619ba1f6e4SLiu Bo 		return btrfs_ioctl_balance(file, argp);
4962837d5b6eSIlya Dryomov 	case BTRFS_IOC_BALANCE_CTL:
4963837d5b6eSIlya Dryomov 		return btrfs_ioctl_balance_ctl(root, arg);
496419a39dceSIlya Dryomov 	case BTRFS_IOC_BALANCE_PROGRESS:
496519a39dceSIlya Dryomov 		return btrfs_ioctl_balance_progress(root, argp);
49668ea05e3aSAlexander Block 	case BTRFS_IOC_SET_RECEIVED_SUBVOL:
49678ea05e3aSAlexander Block 		return btrfs_ioctl_set_received_subvol(file, argp);
4968abccd00fSHugo Mills #ifdef CONFIG_64BIT
4969abccd00fSHugo Mills 	case BTRFS_IOC_SET_RECEIVED_SUBVOL_32:
4970abccd00fSHugo Mills 		return btrfs_ioctl_set_received_subvol_32(file, argp);
4971abccd00fSHugo Mills #endif
497231db9f7cSAlexander Block 	case BTRFS_IOC_SEND:
497331db9f7cSAlexander Block 		return btrfs_ioctl_send(file, argp);
4974c11d2c23SStefan Behrens 	case BTRFS_IOC_GET_DEV_STATS:
4975b27f7c0cSDavid Sterba 		return btrfs_ioctl_get_dev_stats(root, argp);
49765d13a37bSArne Jansen 	case BTRFS_IOC_QUOTA_CTL:
4977905b0ddaSMiao Xie 		return btrfs_ioctl_quota_ctl(file, argp);
49785d13a37bSArne Jansen 	case BTRFS_IOC_QGROUP_ASSIGN:
4979905b0ddaSMiao Xie 		return btrfs_ioctl_qgroup_assign(file, argp);
49805d13a37bSArne Jansen 	case BTRFS_IOC_QGROUP_CREATE:
4981905b0ddaSMiao Xie 		return btrfs_ioctl_qgroup_create(file, argp);
49825d13a37bSArne Jansen 	case BTRFS_IOC_QGROUP_LIMIT:
4983905b0ddaSMiao Xie 		return btrfs_ioctl_qgroup_limit(file, argp);
49842f232036SJan Schmidt 	case BTRFS_IOC_QUOTA_RESCAN:
49852f232036SJan Schmidt 		return btrfs_ioctl_quota_rescan(file, argp);
49862f232036SJan Schmidt 	case BTRFS_IOC_QUOTA_RESCAN_STATUS:
49872f232036SJan Schmidt 		return btrfs_ioctl_quota_rescan_status(file, argp);
498857254b6eSJan Schmidt 	case BTRFS_IOC_QUOTA_RESCAN_WAIT:
498957254b6eSJan Schmidt 		return btrfs_ioctl_quota_rescan_wait(file, argp);
49903f6bcfbdSStefan Behrens 	case BTRFS_IOC_DEV_REPLACE:
49913f6bcfbdSStefan Behrens 		return btrfs_ioctl_dev_replace(root, argp);
4992867ab667Sjeff.liu 	case BTRFS_IOC_GET_FSLABEL:
4993867ab667Sjeff.liu 		return btrfs_ioctl_get_fslabel(file, argp);
4994a8bfd4abSjeff.liu 	case BTRFS_IOC_SET_FSLABEL:
4995a8bfd4abSjeff.liu 		return btrfs_ioctl_set_fslabel(file, argp);
4996416161dbSMark Fasheh 	case BTRFS_IOC_FILE_EXTENT_SAME:
4997416161dbSMark Fasheh 		return btrfs_ioctl_file_extent_same(file, argp);
49982eaa055fSJeff Mahoney 	case BTRFS_IOC_GET_SUPPORTED_FEATURES:
49992eaa055fSJeff Mahoney 		return btrfs_ioctl_get_supported_features(file, argp);
50002eaa055fSJeff Mahoney 	case BTRFS_IOC_GET_FEATURES:
50012eaa055fSJeff Mahoney 		return btrfs_ioctl_get_features(file, argp);
50022eaa055fSJeff Mahoney 	case BTRFS_IOC_SET_FEATURES:
50032eaa055fSJeff Mahoney 		return btrfs_ioctl_set_features(file, argp);
5004f46b5a66SChristoph Hellwig 	}
5005f46b5a66SChristoph Hellwig 
5006f46b5a66SChristoph Hellwig 	return -ENOTTY;
5007f46b5a66SChristoph Hellwig }
5008