xref: /openbmc/linux/fs/btrfs/ioctl.c (revision abccd00f)
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 
62*abccd00fSHugo Mills #ifdef CONFIG_64BIT
63*abccd00fSHugo Mills /* If we have a 32-bit userspace and 64-bit kernel, then the UAPI
64*abccd00fSHugo Mills  * structures are incorrect, as the timespec structure from userspace
65*abccd00fSHugo Mills  * is 4 bytes too small. We define these alternatives here to teach
66*abccd00fSHugo Mills  * the kernel about the 32-bit struct packing.
67*abccd00fSHugo Mills  */
68*abccd00fSHugo Mills struct btrfs_ioctl_timespec_32 {
69*abccd00fSHugo Mills 	__u64 sec;
70*abccd00fSHugo Mills 	__u32 nsec;
71*abccd00fSHugo Mills } __attribute__ ((__packed__));
72*abccd00fSHugo Mills 
73*abccd00fSHugo Mills struct btrfs_ioctl_received_subvol_args_32 {
74*abccd00fSHugo Mills 	char	uuid[BTRFS_UUID_SIZE];	/* in */
75*abccd00fSHugo Mills 	__u64	stransid;		/* in */
76*abccd00fSHugo Mills 	__u64	rtransid;		/* out */
77*abccd00fSHugo Mills 	struct btrfs_ioctl_timespec_32 stime; /* in */
78*abccd00fSHugo Mills 	struct btrfs_ioctl_timespec_32 rtime; /* out */
79*abccd00fSHugo Mills 	__u64	flags;			/* in */
80*abccd00fSHugo Mills 	__u64	reserved[16];		/* in */
81*abccd00fSHugo Mills } __attribute__ ((__packed__));
82*abccd00fSHugo Mills 
83*abccd00fSHugo Mills #define BTRFS_IOC_SET_RECEIVED_SUBVOL_32 _IOWR(BTRFS_IOCTL_MAGIC, 37, \
84*abccd00fSHugo Mills 				struct btrfs_ioctl_received_subvol_args_32)
85*abccd00fSHugo Mills #endif
86*abccd00fSHugo Mills 
87*abccd00fSHugo 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 
614e9662f70SMiao Xie static int create_snapshot(struct btrfs_root *root, struct inode *dir,
615e9662f70SMiao Xie 			   struct dentry *dentry, char *name, int namelen,
616e9662f70SMiao Xie 			   u64 *async_transid, bool readonly,
617e9662f70SMiao Xie 			   struct btrfs_qgroup_inherit *inherit)
618f46b5a66SChristoph Hellwig {
6192e4bfab9SYan, Zheng 	struct inode *inode;
620f46b5a66SChristoph Hellwig 	struct btrfs_pending_snapshot *pending_snapshot;
621f46b5a66SChristoph Hellwig 	struct btrfs_trans_handle *trans;
6222e4bfab9SYan, Zheng 	int ret;
623f46b5a66SChristoph Hellwig 
624f46b5a66SChristoph Hellwig 	if (!root->ref_cows)
625f46b5a66SChristoph Hellwig 		return -EINVAL;
626f46b5a66SChristoph Hellwig 
6276a03843dSMiao Xie 	ret = btrfs_start_delalloc_inodes(root, 0);
6286a03843dSMiao Xie 	if (ret)
6296a03843dSMiao Xie 		return ret;
6306a03843dSMiao Xie 
631b0244199SMiao Xie 	btrfs_wait_ordered_extents(root, -1);
6326a03843dSMiao Xie 
633a22285a6SYan, Zheng 	pending_snapshot = kzalloc(sizeof(*pending_snapshot), GFP_NOFS);
634a22285a6SYan, Zheng 	if (!pending_snapshot)
635a22285a6SYan, Zheng 		return -ENOMEM;
636a22285a6SYan, Zheng 
63766d8f3ddSMiao Xie 	btrfs_init_block_rsv(&pending_snapshot->block_rsv,
63866d8f3ddSMiao Xie 			     BTRFS_BLOCK_RSV_TEMP);
639d5c12070SMiao Xie 	/*
640d5c12070SMiao Xie 	 * 1 - parent dir inode
641d5c12070SMiao Xie 	 * 2 - dir entries
642d5c12070SMiao Xie 	 * 1 - root item
643d5c12070SMiao Xie 	 * 2 - root ref/backref
644d5c12070SMiao Xie 	 * 1 - root of snapshot
645dd5f9615SStefan Behrens 	 * 1 - UUID item
646d5c12070SMiao Xie 	 */
647d5c12070SMiao Xie 	ret = btrfs_subvolume_reserve_metadata(BTRFS_I(dir)->root,
648dd5f9615SStefan Behrens 					&pending_snapshot->block_rsv, 8,
649ee3441b4SJeff Mahoney 					&pending_snapshot->qgroup_reserved,
650ee3441b4SJeff Mahoney 					false);
651d5c12070SMiao Xie 	if (ret)
652d5c12070SMiao Xie 		goto out;
653d5c12070SMiao Xie 
654a22285a6SYan, Zheng 	pending_snapshot->dentry = dentry;
655a22285a6SYan, Zheng 	pending_snapshot->root = root;
656b83cc969SLi Zefan 	pending_snapshot->readonly = readonly;
657e9662f70SMiao Xie 	pending_snapshot->dir = dir;
6588696c533SMiao Xie 	pending_snapshot->inherit = inherit;
659a22285a6SYan, Zheng 
660d5c12070SMiao Xie 	trans = btrfs_start_transaction(root, 0);
661a22285a6SYan, Zheng 	if (IS_ERR(trans)) {
662a22285a6SYan, Zheng 		ret = PTR_ERR(trans);
663a22285a6SYan, Zheng 		goto fail;
664a22285a6SYan, Zheng 	}
665a22285a6SYan, Zheng 
6668351583eSJosef Bacik 	spin_lock(&root->fs_info->trans_lock);
667a22285a6SYan, Zheng 	list_add(&pending_snapshot->list,
668a22285a6SYan, Zheng 		 &trans->transaction->pending_snapshots);
6698351583eSJosef Bacik 	spin_unlock(&root->fs_info->trans_lock);
67072fd032eSSage Weil 	if (async_transid) {
67172fd032eSSage Weil 		*async_transid = trans->transid;
67272fd032eSSage Weil 		ret = btrfs_commit_transaction_async(trans,
67372fd032eSSage Weil 				     root->fs_info->extent_root, 1);
67400d71c9cSMiao Xie 		if (ret)
67500d71c9cSMiao Xie 			ret = btrfs_commit_transaction(trans, root);
67672fd032eSSage Weil 	} else {
67772fd032eSSage Weil 		ret = btrfs_commit_transaction(trans,
67872fd032eSSage Weil 					       root->fs_info->extent_root);
67972fd032eSSage Weil 	}
680aec8030aSMiao Xie 	if (ret)
681c37b2b62SJosef Bacik 		goto fail;
682a22285a6SYan, Zheng 
683a22285a6SYan, Zheng 	ret = pending_snapshot->error;
684f46b5a66SChristoph Hellwig 	if (ret)
6852e4bfab9SYan, Zheng 		goto fail;
686f46b5a66SChristoph Hellwig 
68766b4ffd1SJosef Bacik 	ret = btrfs_orphan_cleanup(pending_snapshot->snap);
68866b4ffd1SJosef Bacik 	if (ret)
68966b4ffd1SJosef Bacik 		goto fail;
690f46b5a66SChristoph Hellwig 
6912fbe8c8aSAl Viro 	inode = btrfs_lookup_dentry(dentry->d_parent->d_inode, dentry);
6922e4bfab9SYan, Zheng 	if (IS_ERR(inode)) {
6932e4bfab9SYan, Zheng 		ret = PTR_ERR(inode);
6942e4bfab9SYan, Zheng 		goto fail;
6952e4bfab9SYan, Zheng 	}
6965662344bSTsutomu Itoh 
6972e4bfab9SYan, Zheng 	d_instantiate(dentry, inode);
6982e4bfab9SYan, Zheng 	ret = 0;
6992e4bfab9SYan, Zheng fail:
700d5c12070SMiao Xie 	btrfs_subvolume_release_metadata(BTRFS_I(dir)->root,
701d5c12070SMiao Xie 					 &pending_snapshot->block_rsv,
702d5c12070SMiao Xie 					 pending_snapshot->qgroup_reserved);
703d5c12070SMiao Xie out:
704a22285a6SYan, Zheng 	kfree(pending_snapshot);
705f46b5a66SChristoph Hellwig 	return ret;
706f46b5a66SChristoph Hellwig }
707f46b5a66SChristoph Hellwig 
7084260f7c7SSage Weil /*  copy of check_sticky in fs/namei.c()
7094260f7c7SSage Weil * It's inline, so penalty for filesystems that don't use sticky bit is
7104260f7c7SSage Weil * minimal.
7114260f7c7SSage Weil */
7124260f7c7SSage Weil static inline int btrfs_check_sticky(struct inode *dir, struct inode *inode)
7134260f7c7SSage Weil {
7142f2f43d3SEric W. Biederman 	kuid_t fsuid = current_fsuid();
7154260f7c7SSage Weil 
7164260f7c7SSage Weil 	if (!(dir->i_mode & S_ISVTX))
7174260f7c7SSage Weil 		return 0;
7182f2f43d3SEric W. Biederman 	if (uid_eq(inode->i_uid, fsuid))
7194260f7c7SSage Weil 		return 0;
7202f2f43d3SEric W. Biederman 	if (uid_eq(dir->i_uid, fsuid))
7214260f7c7SSage Weil 		return 0;
7224260f7c7SSage Weil 	return !capable(CAP_FOWNER);
7234260f7c7SSage Weil }
7244260f7c7SSage Weil 
7254260f7c7SSage Weil /*  copy of may_delete in fs/namei.c()
7264260f7c7SSage Weil  *	Check whether we can remove a link victim from directory dir, check
7274260f7c7SSage Weil  *  whether the type of victim is right.
7284260f7c7SSage Weil  *  1. We can't do it if dir is read-only (done in permission())
7294260f7c7SSage Weil  *  2. We should have write and exec permissions on dir
7304260f7c7SSage Weil  *  3. We can't remove anything from append-only dir
7314260f7c7SSage Weil  *  4. We can't do anything with immutable dir (done in permission())
7324260f7c7SSage Weil  *  5. If the sticky bit on dir is set we should either
7334260f7c7SSage Weil  *	a. be owner of dir, or
7344260f7c7SSage Weil  *	b. be owner of victim, or
7354260f7c7SSage Weil  *	c. have CAP_FOWNER capability
7364260f7c7SSage Weil  *  6. If the victim is append-only or immutable we can't do antyhing with
7374260f7c7SSage Weil  *     links pointing to it.
7384260f7c7SSage Weil  *  7. If we were asked to remove a directory and victim isn't one - ENOTDIR.
7394260f7c7SSage Weil  *  8. If we were asked to remove a non-directory and victim isn't one - EISDIR.
7404260f7c7SSage Weil  *  9. We can't remove a root or mountpoint.
7414260f7c7SSage Weil  * 10. We don't allow removal of NFS sillyrenamed files; it's handled by
7424260f7c7SSage Weil  *     nfs_async_unlink().
7434260f7c7SSage Weil  */
7444260f7c7SSage Weil 
7454260f7c7SSage Weil static int btrfs_may_delete(struct inode *dir, struct dentry *victim, int isdir)
7464260f7c7SSage Weil {
7474260f7c7SSage Weil 	int error;
7484260f7c7SSage Weil 
7494260f7c7SSage Weil 	if (!victim->d_inode)
7504260f7c7SSage Weil 		return -ENOENT;
7514260f7c7SSage Weil 
7524260f7c7SSage Weil 	BUG_ON(victim->d_parent->d_inode != dir);
7534fa6b5ecSJeff Layton 	audit_inode_child(dir, victim, AUDIT_TYPE_CHILD_DELETE);
7544260f7c7SSage Weil 
7554260f7c7SSage Weil 	error = inode_permission(dir, MAY_WRITE | MAY_EXEC);
7564260f7c7SSage Weil 	if (error)
7574260f7c7SSage Weil 		return error;
7584260f7c7SSage Weil 	if (IS_APPEND(dir))
7594260f7c7SSage Weil 		return -EPERM;
7604260f7c7SSage Weil 	if (btrfs_check_sticky(dir, victim->d_inode)||
7614260f7c7SSage Weil 		IS_APPEND(victim->d_inode)||
7624260f7c7SSage Weil 	    IS_IMMUTABLE(victim->d_inode) || IS_SWAPFILE(victim->d_inode))
7634260f7c7SSage Weil 		return -EPERM;
7644260f7c7SSage Weil 	if (isdir) {
7654260f7c7SSage Weil 		if (!S_ISDIR(victim->d_inode->i_mode))
7664260f7c7SSage Weil 			return -ENOTDIR;
7674260f7c7SSage Weil 		if (IS_ROOT(victim))
7684260f7c7SSage Weil 			return -EBUSY;
7694260f7c7SSage Weil 	} else if (S_ISDIR(victim->d_inode->i_mode))
7704260f7c7SSage Weil 		return -EISDIR;
7714260f7c7SSage Weil 	if (IS_DEADDIR(dir))
7724260f7c7SSage Weil 		return -ENOENT;
7734260f7c7SSage Weil 	if (victim->d_flags & DCACHE_NFSFS_RENAMED)
7744260f7c7SSage Weil 		return -EBUSY;
7754260f7c7SSage Weil 	return 0;
7764260f7c7SSage Weil }
7774260f7c7SSage Weil 
778cb8e7090SChristoph Hellwig /* copy of may_create in fs/namei.c() */
779cb8e7090SChristoph Hellwig static inline int btrfs_may_create(struct inode *dir, struct dentry *child)
780cb8e7090SChristoph Hellwig {
781cb8e7090SChristoph Hellwig 	if (child->d_inode)
782cb8e7090SChristoph Hellwig 		return -EEXIST;
783cb8e7090SChristoph Hellwig 	if (IS_DEADDIR(dir))
784cb8e7090SChristoph Hellwig 		return -ENOENT;
785cb8e7090SChristoph Hellwig 	return inode_permission(dir, MAY_WRITE | MAY_EXEC);
786cb8e7090SChristoph Hellwig }
787cb8e7090SChristoph Hellwig 
788cb8e7090SChristoph Hellwig /*
789cb8e7090SChristoph Hellwig  * Create a new subvolume below @parent.  This is largely modeled after
790cb8e7090SChristoph Hellwig  * sys_mkdirat and vfs_mkdir, but we only do a single component lookup
791cb8e7090SChristoph Hellwig  * inside this filesystem so it's quite a bit simpler.
792cb8e7090SChristoph Hellwig  */
79376dda93cSYan, Zheng static noinline int btrfs_mksubvol(struct path *parent,
79476dda93cSYan, Zheng 				   char *name, int namelen,
79572fd032eSSage Weil 				   struct btrfs_root *snap_src,
7966f72c7e2SArne Jansen 				   u64 *async_transid, bool readonly,
7978696c533SMiao Xie 				   struct btrfs_qgroup_inherit *inherit)
798cb8e7090SChristoph Hellwig {
79976dda93cSYan, Zheng 	struct inode *dir  = parent->dentry->d_inode;
800cb8e7090SChristoph Hellwig 	struct dentry *dentry;
801cb8e7090SChristoph Hellwig 	int error;
802cb8e7090SChristoph Hellwig 
8035c50c9b8SDavid Sterba 	error = mutex_lock_killable_nested(&dir->i_mutex, I_MUTEX_PARENT);
8045c50c9b8SDavid Sterba 	if (error == -EINTR)
8055c50c9b8SDavid Sterba 		return error;
806cb8e7090SChristoph Hellwig 
807cb8e7090SChristoph Hellwig 	dentry = lookup_one_len(name, parent->dentry, namelen);
808cb8e7090SChristoph Hellwig 	error = PTR_ERR(dentry);
809cb8e7090SChristoph Hellwig 	if (IS_ERR(dentry))
810cb8e7090SChristoph Hellwig 		goto out_unlock;
811cb8e7090SChristoph Hellwig 
812cb8e7090SChristoph Hellwig 	error = -EEXIST;
813cb8e7090SChristoph Hellwig 	if (dentry->d_inode)
814cb8e7090SChristoph Hellwig 		goto out_dput;
815cb8e7090SChristoph Hellwig 
81676dda93cSYan, Zheng 	error = btrfs_may_create(dir, dentry);
817cb8e7090SChristoph Hellwig 	if (error)
818a874a63eSLiu Bo 		goto out_dput;
819cb8e7090SChristoph Hellwig 
8209c52057cSChris Mason 	/*
8219c52057cSChris Mason 	 * even if this name doesn't exist, we may get hash collisions.
8229c52057cSChris Mason 	 * check for them now when we can safely fail
8239c52057cSChris Mason 	 */
8249c52057cSChris Mason 	error = btrfs_check_dir_item_collision(BTRFS_I(dir)->root,
8259c52057cSChris Mason 					       dir->i_ino, name,
8269c52057cSChris Mason 					       namelen);
8279c52057cSChris Mason 	if (error)
8289c52057cSChris Mason 		goto out_dput;
8299c52057cSChris Mason 
83076dda93cSYan, Zheng 	down_read(&BTRFS_I(dir)->root->fs_info->subvol_sem);
83176dda93cSYan, Zheng 
83276dda93cSYan, Zheng 	if (btrfs_root_refs(&BTRFS_I(dir)->root->root_item) == 0)
83376dda93cSYan, Zheng 		goto out_up_read;
83476dda93cSYan, Zheng 
8353de4586cSChris Mason 	if (snap_src) {
836e9662f70SMiao Xie 		error = create_snapshot(snap_src, dir, dentry, name, namelen,
8376f72c7e2SArne Jansen 					async_transid, readonly, inherit);
8383de4586cSChris Mason 	} else {
839d5c12070SMiao Xie 		error = create_subvol(dir, dentry, name, namelen,
840d5c12070SMiao Xie 				      async_transid, inherit);
8413de4586cSChris Mason 	}
84276dda93cSYan, Zheng 	if (!error)
84376dda93cSYan, Zheng 		fsnotify_mkdir(dir, dentry);
84476dda93cSYan, Zheng out_up_read:
84576dda93cSYan, Zheng 	up_read(&BTRFS_I(dir)->root->fs_info->subvol_sem);
846cb8e7090SChristoph Hellwig out_dput:
847cb8e7090SChristoph Hellwig 	dput(dentry);
848cb8e7090SChristoph Hellwig out_unlock:
84976dda93cSYan, Zheng 	mutex_unlock(&dir->i_mutex);
850cb8e7090SChristoph Hellwig 	return error;
851cb8e7090SChristoph Hellwig }
852cb8e7090SChristoph Hellwig 
8534cb5300bSChris Mason /*
8544cb5300bSChris Mason  * When we're defragging a range, we don't want to kick it off again
8554cb5300bSChris Mason  * if it is really just waiting for delalloc to send it down.
8564cb5300bSChris Mason  * If we find a nice big extent or delalloc range for the bytes in the
8574cb5300bSChris Mason  * file you want to defrag, we return 0 to let you know to skip this
8584cb5300bSChris Mason  * part of the file
8594cb5300bSChris Mason  */
8604cb5300bSChris Mason static int check_defrag_in_cache(struct inode *inode, u64 offset, int thresh)
8614cb5300bSChris Mason {
8624cb5300bSChris Mason 	struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
8634cb5300bSChris Mason 	struct extent_map *em = NULL;
8644cb5300bSChris Mason 	struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
8654cb5300bSChris Mason 	u64 end;
8664cb5300bSChris Mason 
8674cb5300bSChris Mason 	read_lock(&em_tree->lock);
8684cb5300bSChris Mason 	em = lookup_extent_mapping(em_tree, offset, PAGE_CACHE_SIZE);
8694cb5300bSChris Mason 	read_unlock(&em_tree->lock);
8704cb5300bSChris Mason 
8714cb5300bSChris Mason 	if (em) {
8724cb5300bSChris Mason 		end = extent_map_end(em);
8734cb5300bSChris Mason 		free_extent_map(em);
8744cb5300bSChris Mason 		if (end - offset > thresh)
8754cb5300bSChris Mason 			return 0;
8764cb5300bSChris Mason 	}
8774cb5300bSChris Mason 	/* if we already have a nice delalloc here, just stop */
8784cb5300bSChris Mason 	thresh /= 2;
8794cb5300bSChris Mason 	end = count_range_bits(io_tree, &offset, offset + thresh,
8804cb5300bSChris Mason 			       thresh, EXTENT_DELALLOC, 1);
8814cb5300bSChris Mason 	if (end >= thresh)
8824cb5300bSChris Mason 		return 0;
8834cb5300bSChris Mason 	return 1;
8844cb5300bSChris Mason }
8854cb5300bSChris Mason 
8864cb5300bSChris Mason /*
8874cb5300bSChris Mason  * helper function to walk through a file and find extents
8884cb5300bSChris Mason  * newer than a specific transid, and smaller than thresh.
8894cb5300bSChris Mason  *
8904cb5300bSChris Mason  * This is used by the defragging code to find new and small
8914cb5300bSChris Mason  * extents
8924cb5300bSChris Mason  */
8934cb5300bSChris Mason static int find_new_extents(struct btrfs_root *root,
8944cb5300bSChris Mason 			    struct inode *inode, u64 newer_than,
8954cb5300bSChris Mason 			    u64 *off, int thresh)
8964cb5300bSChris Mason {
8974cb5300bSChris Mason 	struct btrfs_path *path;
8984cb5300bSChris Mason 	struct btrfs_key min_key;
8994cb5300bSChris Mason 	struct extent_buffer *leaf;
9004cb5300bSChris Mason 	struct btrfs_file_extent_item *extent;
9014cb5300bSChris Mason 	int type;
9024cb5300bSChris Mason 	int ret;
903a4689d2bSDavid Sterba 	u64 ino = btrfs_ino(inode);
9044cb5300bSChris Mason 
9054cb5300bSChris Mason 	path = btrfs_alloc_path();
9064cb5300bSChris Mason 	if (!path)
9074cb5300bSChris Mason 		return -ENOMEM;
9084cb5300bSChris Mason 
909a4689d2bSDavid Sterba 	min_key.objectid = ino;
9104cb5300bSChris Mason 	min_key.type = BTRFS_EXTENT_DATA_KEY;
9114cb5300bSChris Mason 	min_key.offset = *off;
9124cb5300bSChris Mason 
9134cb5300bSChris Mason 	path->keep_locks = 1;
9144cb5300bSChris Mason 
9154cb5300bSChris Mason 	while (1) {
9166174d3cbSFilipe David Borba Manana 		ret = btrfs_search_forward(root, &min_key, path, newer_than);
9174cb5300bSChris Mason 		if (ret != 0)
9184cb5300bSChris Mason 			goto none;
919a4689d2bSDavid Sterba 		if (min_key.objectid != ino)
9204cb5300bSChris Mason 			goto none;
9214cb5300bSChris Mason 		if (min_key.type != BTRFS_EXTENT_DATA_KEY)
9224cb5300bSChris Mason 			goto none;
9234cb5300bSChris Mason 
9244cb5300bSChris Mason 		leaf = path->nodes[0];
9254cb5300bSChris Mason 		extent = btrfs_item_ptr(leaf, path->slots[0],
9264cb5300bSChris Mason 					struct btrfs_file_extent_item);
9274cb5300bSChris Mason 
9284cb5300bSChris Mason 		type = btrfs_file_extent_type(leaf, extent);
9294cb5300bSChris Mason 		if (type == BTRFS_FILE_EXTENT_REG &&
9304cb5300bSChris Mason 		    btrfs_file_extent_num_bytes(leaf, extent) < thresh &&
9314cb5300bSChris Mason 		    check_defrag_in_cache(inode, min_key.offset, thresh)) {
9324cb5300bSChris Mason 			*off = min_key.offset;
9334cb5300bSChris Mason 			btrfs_free_path(path);
9344cb5300bSChris Mason 			return 0;
9354cb5300bSChris Mason 		}
9364cb5300bSChris Mason 
9374cb5300bSChris Mason 		if (min_key.offset == (u64)-1)
9384cb5300bSChris Mason 			goto none;
9394cb5300bSChris Mason 
9404cb5300bSChris Mason 		min_key.offset++;
9414cb5300bSChris Mason 		btrfs_release_path(path);
9424cb5300bSChris Mason 	}
9434cb5300bSChris Mason none:
9444cb5300bSChris Mason 	btrfs_free_path(path);
9454cb5300bSChris Mason 	return -ENOENT;
9464cb5300bSChris Mason }
9474cb5300bSChris Mason 
9486c282eb4SLi Zefan static struct extent_map *defrag_lookup_extent(struct inode *inode, u64 start)
94917ce6ef8SLiu Bo {
95017ce6ef8SLiu Bo 	struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
951940100a4SChris Mason 	struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
9526c282eb4SLi Zefan 	struct extent_map *em;
9536c282eb4SLi Zefan 	u64 len = PAGE_CACHE_SIZE;
954940100a4SChris Mason 
955940100a4SChris Mason 	/*
956940100a4SChris Mason 	 * hopefully we have this extent in the tree already, try without
957940100a4SChris Mason 	 * the full extent lock
958940100a4SChris Mason 	 */
959940100a4SChris Mason 	read_lock(&em_tree->lock);
960940100a4SChris Mason 	em = lookup_extent_mapping(em_tree, start, len);
961940100a4SChris Mason 	read_unlock(&em_tree->lock);
962940100a4SChris Mason 
963940100a4SChris Mason 	if (!em) {
964940100a4SChris Mason 		/* get the big lock and read metadata off disk */
965d0082371SJeff Mahoney 		lock_extent(io_tree, start, start + len - 1);
966940100a4SChris Mason 		em = btrfs_get_extent(inode, NULL, 0, start, len, 0);
967d0082371SJeff Mahoney 		unlock_extent(io_tree, start, start + len - 1);
968940100a4SChris Mason 
9696cf8bfbfSDan Carpenter 		if (IS_ERR(em))
9706c282eb4SLi Zefan 			return NULL;
971940100a4SChris Mason 	}
972940100a4SChris Mason 
9736c282eb4SLi Zefan 	return em;
9746c282eb4SLi Zefan }
9756c282eb4SLi Zefan 
9766c282eb4SLi Zefan static bool defrag_check_next_extent(struct inode *inode, struct extent_map *em)
9776c282eb4SLi Zefan {
9786c282eb4SLi Zefan 	struct extent_map *next;
9796c282eb4SLi Zefan 	bool ret = true;
9806c282eb4SLi Zefan 
9816c282eb4SLi Zefan 	/* this is the last extent */
9826c282eb4SLi Zefan 	if (em->start + em->len >= i_size_read(inode))
9836c282eb4SLi Zefan 		return false;
9846c282eb4SLi Zefan 
9856c282eb4SLi Zefan 	next = defrag_lookup_extent(inode, em->start + em->len);
9866c282eb4SLi Zefan 	if (!next || next->block_start >= EXTENT_MAP_LAST_BYTE)
9876c282eb4SLi Zefan 		ret = false;
9886c282eb4SLi Zefan 
9896c282eb4SLi Zefan 	free_extent_map(next);
9906c282eb4SLi Zefan 	return ret;
9916c282eb4SLi Zefan }
9926c282eb4SLi Zefan 
9936c282eb4SLi Zefan static int should_defrag_range(struct inode *inode, u64 start, int thresh,
994a43a2111SAndrew Mahone 			       u64 *last_len, u64 *skip, u64 *defrag_end,
995a43a2111SAndrew Mahone 			       int compress)
9966c282eb4SLi Zefan {
9976c282eb4SLi Zefan 	struct extent_map *em;
9986c282eb4SLi Zefan 	int ret = 1;
9996c282eb4SLi Zefan 	bool next_mergeable = true;
10006c282eb4SLi Zefan 
10016c282eb4SLi Zefan 	/*
10026c282eb4SLi Zefan 	 * make sure that once we start defragging an extent, we keep on
10036c282eb4SLi Zefan 	 * defragging it
10046c282eb4SLi Zefan 	 */
10056c282eb4SLi Zefan 	if (start < *defrag_end)
10066c282eb4SLi Zefan 		return 1;
10076c282eb4SLi Zefan 
10086c282eb4SLi Zefan 	*skip = 0;
10096c282eb4SLi Zefan 
10106c282eb4SLi Zefan 	em = defrag_lookup_extent(inode, start);
10116c282eb4SLi Zefan 	if (!em)
10126c282eb4SLi Zefan 		return 0;
10136c282eb4SLi Zefan 
1014940100a4SChris Mason 	/* this will cover holes, and inline extents */
101517ce6ef8SLiu Bo 	if (em->block_start >= EXTENT_MAP_LAST_BYTE) {
1016940100a4SChris Mason 		ret = 0;
101717ce6ef8SLiu Bo 		goto out;
101817ce6ef8SLiu Bo 	}
101917ce6ef8SLiu Bo 
10206c282eb4SLi Zefan 	next_mergeable = defrag_check_next_extent(inode, em);
1021940100a4SChris Mason 
1022940100a4SChris Mason 	/*
10236c282eb4SLi Zefan 	 * we hit a real extent, if it is big or the next extent is not a
10246c282eb4SLi Zefan 	 * real extent, don't bother defragging it
1025940100a4SChris Mason 	 */
1026a43a2111SAndrew Mahone 	if (!compress && (*last_len == 0 || *last_len >= thresh) &&
10276c282eb4SLi Zefan 	    (em->len >= thresh || !next_mergeable))
1028940100a4SChris Mason 		ret = 0;
102917ce6ef8SLiu Bo out:
1030940100a4SChris Mason 	/*
1031940100a4SChris Mason 	 * last_len ends up being a counter of how many bytes we've defragged.
1032940100a4SChris Mason 	 * every time we choose not to defrag an extent, we reset *last_len
1033940100a4SChris Mason 	 * so that the next tiny extent will force a defrag.
1034940100a4SChris Mason 	 *
1035940100a4SChris Mason 	 * The end result of this is that tiny extents before a single big
1036940100a4SChris Mason 	 * extent will force at least part of that big extent to be defragged.
1037940100a4SChris Mason 	 */
1038940100a4SChris Mason 	if (ret) {
1039940100a4SChris Mason 		*defrag_end = extent_map_end(em);
1040940100a4SChris Mason 	} else {
1041940100a4SChris Mason 		*last_len = 0;
1042940100a4SChris Mason 		*skip = extent_map_end(em);
1043940100a4SChris Mason 		*defrag_end = 0;
1044940100a4SChris Mason 	}
1045940100a4SChris Mason 
1046940100a4SChris Mason 	free_extent_map(em);
1047940100a4SChris Mason 	return ret;
1048940100a4SChris Mason }
1049940100a4SChris Mason 
10504cb5300bSChris Mason /*
10514cb5300bSChris Mason  * it doesn't do much good to defrag one or two pages
10524cb5300bSChris Mason  * at a time.  This pulls in a nice chunk of pages
10534cb5300bSChris Mason  * to COW and defrag.
10544cb5300bSChris Mason  *
10554cb5300bSChris Mason  * It also makes sure the delalloc code has enough
10564cb5300bSChris Mason  * dirty data to avoid making new small extents as part
10574cb5300bSChris Mason  * of the defrag
10584cb5300bSChris Mason  *
10594cb5300bSChris Mason  * It's a good idea to start RA on this range
10604cb5300bSChris Mason  * before calling this.
10614cb5300bSChris Mason  */
10624cb5300bSChris Mason static int cluster_pages_for_defrag(struct inode *inode,
10634cb5300bSChris Mason 				    struct page **pages,
10644cb5300bSChris Mason 				    unsigned long start_index,
1065c41570c9SJustin Maggard 				    unsigned long num_pages)
1066f46b5a66SChristoph Hellwig {
10674cb5300bSChris Mason 	unsigned long file_end;
10684cb5300bSChris Mason 	u64 isize = i_size_read(inode);
1069f46b5a66SChristoph Hellwig 	u64 page_start;
1070f46b5a66SChristoph Hellwig 	u64 page_end;
10711f12bd06SLiu Bo 	u64 page_cnt;
10724cb5300bSChris Mason 	int ret;
10734cb5300bSChris Mason 	int i;
10744cb5300bSChris Mason 	int i_done;
10754cb5300bSChris Mason 	struct btrfs_ordered_extent *ordered;
10764cb5300bSChris Mason 	struct extent_state *cached_state = NULL;
1077600a45e1SMiao Xie 	struct extent_io_tree *tree;
10783b16a4e3SJosef Bacik 	gfp_t mask = btrfs_alloc_write_mask(inode->i_mapping);
10794cb5300bSChris Mason 
10804cb5300bSChris Mason 	file_end = (isize - 1) >> PAGE_CACHE_SHIFT;
10811f12bd06SLiu Bo 	if (!isize || start_index > file_end)
10821f12bd06SLiu Bo 		return 0;
10831f12bd06SLiu Bo 
10841f12bd06SLiu Bo 	page_cnt = min_t(u64, (u64)num_pages, (u64)file_end - start_index + 1);
10854cb5300bSChris Mason 
10864cb5300bSChris Mason 	ret = btrfs_delalloc_reserve_space(inode,
10871f12bd06SLiu Bo 					   page_cnt << PAGE_CACHE_SHIFT);
10884cb5300bSChris Mason 	if (ret)
10894cb5300bSChris Mason 		return ret;
10904cb5300bSChris Mason 	i_done = 0;
1091600a45e1SMiao Xie 	tree = &BTRFS_I(inode)->io_tree;
10924cb5300bSChris Mason 
10934cb5300bSChris Mason 	/* step one, lock all the pages */
10941f12bd06SLiu Bo 	for (i = 0; i < page_cnt; i++) {
10954cb5300bSChris Mason 		struct page *page;
1096600a45e1SMiao Xie again:
1097a94733d0SJosef Bacik 		page = find_or_create_page(inode->i_mapping,
10983b16a4e3SJosef Bacik 					   start_index + i, mask);
10994cb5300bSChris Mason 		if (!page)
11004cb5300bSChris Mason 			break;
11014cb5300bSChris Mason 
1102600a45e1SMiao Xie 		page_start = page_offset(page);
1103600a45e1SMiao Xie 		page_end = page_start + PAGE_CACHE_SIZE - 1;
1104600a45e1SMiao Xie 		while (1) {
1105d0082371SJeff Mahoney 			lock_extent(tree, page_start, page_end);
1106600a45e1SMiao Xie 			ordered = btrfs_lookup_ordered_extent(inode,
1107600a45e1SMiao Xie 							      page_start);
1108d0082371SJeff Mahoney 			unlock_extent(tree, page_start, page_end);
1109600a45e1SMiao Xie 			if (!ordered)
1110600a45e1SMiao Xie 				break;
1111600a45e1SMiao Xie 
1112600a45e1SMiao Xie 			unlock_page(page);
1113600a45e1SMiao Xie 			btrfs_start_ordered_extent(inode, ordered, 1);
1114600a45e1SMiao Xie 			btrfs_put_ordered_extent(ordered);
1115600a45e1SMiao Xie 			lock_page(page);
11161f12bd06SLiu Bo 			/*
11171f12bd06SLiu Bo 			 * we unlocked the page above, so we need check if
11181f12bd06SLiu Bo 			 * it was released or not.
11191f12bd06SLiu Bo 			 */
11201f12bd06SLiu Bo 			if (page->mapping != inode->i_mapping) {
11211f12bd06SLiu Bo 				unlock_page(page);
11221f12bd06SLiu Bo 				page_cache_release(page);
11231f12bd06SLiu Bo 				goto again;
11241f12bd06SLiu Bo 			}
1125600a45e1SMiao Xie 		}
1126600a45e1SMiao Xie 
11274cb5300bSChris Mason 		if (!PageUptodate(page)) {
11284cb5300bSChris Mason 			btrfs_readpage(NULL, page);
11294cb5300bSChris Mason 			lock_page(page);
11304cb5300bSChris Mason 			if (!PageUptodate(page)) {
11314cb5300bSChris Mason 				unlock_page(page);
11324cb5300bSChris Mason 				page_cache_release(page);
11334cb5300bSChris Mason 				ret = -EIO;
11344cb5300bSChris Mason 				break;
11354cb5300bSChris Mason 			}
11364cb5300bSChris Mason 		}
1137600a45e1SMiao Xie 
1138600a45e1SMiao Xie 		if (page->mapping != inode->i_mapping) {
1139600a45e1SMiao Xie 			unlock_page(page);
1140600a45e1SMiao Xie 			page_cache_release(page);
1141600a45e1SMiao Xie 			goto again;
1142600a45e1SMiao Xie 		}
1143600a45e1SMiao Xie 
11444cb5300bSChris Mason 		pages[i] = page;
11454cb5300bSChris Mason 		i_done++;
11464cb5300bSChris Mason 	}
11474cb5300bSChris Mason 	if (!i_done || ret)
11484cb5300bSChris Mason 		goto out;
11494cb5300bSChris Mason 
11504cb5300bSChris Mason 	if (!(inode->i_sb->s_flags & MS_ACTIVE))
11514cb5300bSChris Mason 		goto out;
11524cb5300bSChris Mason 
11534cb5300bSChris Mason 	/*
11544cb5300bSChris Mason 	 * so now we have a nice long stream of locked
11554cb5300bSChris Mason 	 * and up to date pages, lets wait on them
11564cb5300bSChris Mason 	 */
11574cb5300bSChris Mason 	for (i = 0; i < i_done; i++)
11584cb5300bSChris Mason 		wait_on_page_writeback(pages[i]);
11594cb5300bSChris Mason 
11604cb5300bSChris Mason 	page_start = page_offset(pages[0]);
11614cb5300bSChris Mason 	page_end = page_offset(pages[i_done - 1]) + PAGE_CACHE_SIZE;
11624cb5300bSChris Mason 
11634cb5300bSChris Mason 	lock_extent_bits(&BTRFS_I(inode)->io_tree,
1164d0082371SJeff Mahoney 			 page_start, page_end - 1, 0, &cached_state);
11654cb5300bSChris Mason 	clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start,
11664cb5300bSChris Mason 			  page_end - 1, EXTENT_DIRTY | EXTENT_DELALLOC |
11679e8a4a8bSLiu Bo 			  EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG, 0, 0,
11689e8a4a8bSLiu Bo 			  &cached_state, GFP_NOFS);
11694cb5300bSChris Mason 
11701f12bd06SLiu Bo 	if (i_done != page_cnt) {
11719e0baf60SJosef Bacik 		spin_lock(&BTRFS_I(inode)->lock);
11729e0baf60SJosef Bacik 		BTRFS_I(inode)->outstanding_extents++;
11739e0baf60SJosef Bacik 		spin_unlock(&BTRFS_I(inode)->lock);
11744cb5300bSChris Mason 		btrfs_delalloc_release_space(inode,
11751f12bd06SLiu Bo 				     (page_cnt - i_done) << PAGE_CACHE_SHIFT);
11764cb5300bSChris Mason 	}
11774cb5300bSChris Mason 
11784cb5300bSChris Mason 
11799e8a4a8bSLiu Bo 	set_extent_defrag(&BTRFS_I(inode)->io_tree, page_start, page_end - 1,
11809e8a4a8bSLiu Bo 			  &cached_state, GFP_NOFS);
11814cb5300bSChris Mason 
11824cb5300bSChris Mason 	unlock_extent_cached(&BTRFS_I(inode)->io_tree,
11834cb5300bSChris Mason 			     page_start, page_end - 1, &cached_state,
11844cb5300bSChris Mason 			     GFP_NOFS);
11854cb5300bSChris Mason 
11864cb5300bSChris Mason 	for (i = 0; i < i_done; i++) {
11874cb5300bSChris Mason 		clear_page_dirty_for_io(pages[i]);
11884cb5300bSChris Mason 		ClearPageChecked(pages[i]);
11894cb5300bSChris Mason 		set_page_extent_mapped(pages[i]);
11904cb5300bSChris Mason 		set_page_dirty(pages[i]);
11914cb5300bSChris Mason 		unlock_page(pages[i]);
11924cb5300bSChris Mason 		page_cache_release(pages[i]);
11934cb5300bSChris Mason 	}
11944cb5300bSChris Mason 	return i_done;
11954cb5300bSChris Mason out:
11964cb5300bSChris Mason 	for (i = 0; i < i_done; i++) {
11974cb5300bSChris Mason 		unlock_page(pages[i]);
11984cb5300bSChris Mason 		page_cache_release(pages[i]);
11994cb5300bSChris Mason 	}
12001f12bd06SLiu Bo 	btrfs_delalloc_release_space(inode, page_cnt << PAGE_CACHE_SHIFT);
12014cb5300bSChris Mason 	return ret;
12024cb5300bSChris Mason 
12034cb5300bSChris Mason }
12044cb5300bSChris Mason 
12054cb5300bSChris Mason int btrfs_defrag_file(struct inode *inode, struct file *file,
12064cb5300bSChris Mason 		      struct btrfs_ioctl_defrag_range_args *range,
12074cb5300bSChris Mason 		      u64 newer_than, unsigned long max_to_defrag)
12084cb5300bSChris Mason {
12094cb5300bSChris Mason 	struct btrfs_root *root = BTRFS_I(inode)->root;
12104cb5300bSChris Mason 	struct file_ra_state *ra = NULL;
12114cb5300bSChris Mason 	unsigned long last_index;
1212151a31b2SLi Zefan 	u64 isize = i_size_read(inode);
1213940100a4SChris Mason 	u64 last_len = 0;
1214940100a4SChris Mason 	u64 skip = 0;
1215940100a4SChris Mason 	u64 defrag_end = 0;
12164cb5300bSChris Mason 	u64 newer_off = range->start;
1217f46b5a66SChristoph Hellwig 	unsigned long i;
1218008873eaSLi Zefan 	unsigned long ra_index = 0;
1219f46b5a66SChristoph Hellwig 	int ret;
12204cb5300bSChris Mason 	int defrag_count = 0;
12211a419d85SLi Zefan 	int compress_type = BTRFS_COMPRESS_ZLIB;
12224cb5300bSChris Mason 	int extent_thresh = range->extent_thresh;
1223c41570c9SJustin Maggard 	unsigned long max_cluster = (256 * 1024) >> PAGE_CACHE_SHIFT;
1224c41570c9SJustin Maggard 	unsigned long cluster = max_cluster;
12254cb5300bSChris Mason 	u64 new_align = ~((u64)128 * 1024 - 1);
12264cb5300bSChris Mason 	struct page **pages = NULL;
12274cb5300bSChris Mason 
12280abd5b17SLiu Bo 	if (isize == 0)
12290abd5b17SLiu Bo 		return 0;
12300abd5b17SLiu Bo 
12310abd5b17SLiu Bo 	if (range->start >= isize)
12320abd5b17SLiu Bo 		return -EINVAL;
12331a419d85SLi Zefan 
12341a419d85SLi Zefan 	if (range->flags & BTRFS_DEFRAG_RANGE_COMPRESS) {
12351a419d85SLi Zefan 		if (range->compress_type > BTRFS_COMPRESS_TYPES)
12361a419d85SLi Zefan 			return -EINVAL;
12371a419d85SLi Zefan 		if (range->compress_type)
12381a419d85SLi Zefan 			compress_type = range->compress_type;
12391a419d85SLi Zefan 	}
1240f46b5a66SChristoph Hellwig 
12410abd5b17SLiu Bo 	if (extent_thresh == 0)
12420abd5b17SLiu Bo 		extent_thresh = 256 * 1024;
1243f46b5a66SChristoph Hellwig 
12444cb5300bSChris Mason 	/*
12454cb5300bSChris Mason 	 * if we were not given a file, allocate a readahead
12464cb5300bSChris Mason 	 * context
12474cb5300bSChris Mason 	 */
12484cb5300bSChris Mason 	if (!file) {
12494cb5300bSChris Mason 		ra = kzalloc(sizeof(*ra), GFP_NOFS);
12504cb5300bSChris Mason 		if (!ra)
12514cb5300bSChris Mason 			return -ENOMEM;
12524cb5300bSChris Mason 		file_ra_state_init(ra, inode->i_mapping);
12534cb5300bSChris Mason 	} else {
12544cb5300bSChris Mason 		ra = &file->f_ra;
12554cb5300bSChris Mason 	}
12564cb5300bSChris Mason 
1257d9b0d9baSDulshani Gunawardhana 	pages = kmalloc_array(max_cluster, sizeof(struct page *),
12584cb5300bSChris Mason 			GFP_NOFS);
12594cb5300bSChris Mason 	if (!pages) {
12604cb5300bSChris Mason 		ret = -ENOMEM;
12614cb5300bSChris Mason 		goto out_ra;
12624cb5300bSChris Mason 	}
12634cb5300bSChris Mason 
12644cb5300bSChris Mason 	/* find the last page to defrag */
12651e701a32SChris Mason 	if (range->start + range->len > range->start) {
1266151a31b2SLi Zefan 		last_index = min_t(u64, isize - 1,
12671e701a32SChris Mason 			 range->start + range->len - 1) >> PAGE_CACHE_SHIFT;
12681e701a32SChris Mason 	} else {
1269151a31b2SLi Zefan 		last_index = (isize - 1) >> PAGE_CACHE_SHIFT;
12701e701a32SChris Mason 	}
12711e701a32SChris Mason 
12724cb5300bSChris Mason 	if (newer_than) {
12734cb5300bSChris Mason 		ret = find_new_extents(root, inode, newer_than,
12744cb5300bSChris Mason 				       &newer_off, 64 * 1024);
12754cb5300bSChris Mason 		if (!ret) {
12764cb5300bSChris Mason 			range->start = newer_off;
12774cb5300bSChris Mason 			/*
12784cb5300bSChris Mason 			 * we always align our defrag to help keep
12794cb5300bSChris Mason 			 * the extents in the file evenly spaced
12804cb5300bSChris Mason 			 */
12814cb5300bSChris Mason 			i = (newer_off & new_align) >> PAGE_CACHE_SHIFT;
12824cb5300bSChris Mason 		} else
12834cb5300bSChris Mason 			goto out_ra;
12844cb5300bSChris Mason 	} else {
12851e701a32SChris Mason 		i = range->start >> PAGE_CACHE_SHIFT;
12864cb5300bSChris Mason 	}
12874cb5300bSChris Mason 	if (!max_to_defrag)
12887ec31b54SLiu Bo 		max_to_defrag = last_index + 1;
12894cb5300bSChris Mason 
12902a0f7f57SLi Zefan 	/*
12912a0f7f57SLi Zefan 	 * make writeback starts from i, so the defrag range can be
12922a0f7f57SLi Zefan 	 * written sequentially.
12932a0f7f57SLi Zefan 	 */
12942a0f7f57SLi Zefan 	if (i < inode->i_mapping->writeback_index)
12952a0f7f57SLi Zefan 		inode->i_mapping->writeback_index = i;
12962a0f7f57SLi Zefan 
1297f7f43cc8SChris Mason 	while (i <= last_index && defrag_count < max_to_defrag &&
1298f7f43cc8SChris Mason 	       (i < (i_size_read(inode) + PAGE_CACHE_SIZE - 1) >>
1299f7f43cc8SChris Mason 		PAGE_CACHE_SHIFT)) {
13004cb5300bSChris Mason 		/*
13014cb5300bSChris Mason 		 * make sure we stop running if someone unmounts
13024cb5300bSChris Mason 		 * the FS
13034cb5300bSChris Mason 		 */
13044cb5300bSChris Mason 		if (!(inode->i_sb->s_flags & MS_ACTIVE))
13054cb5300bSChris Mason 			break;
13064cb5300bSChris Mason 
1307210549ebSDavid Sterba 		if (btrfs_defrag_cancelled(root->fs_info)) {
1308efe120a0SFrank Holton 			printk(KERN_DEBUG "BTRFS: defrag_file cancelled\n");
1309210549ebSDavid Sterba 			ret = -EAGAIN;
1310210549ebSDavid Sterba 			break;
1311210549ebSDavid Sterba 		}
1312210549ebSDavid Sterba 
131366c26892SLiu Bo 		if (!should_defrag_range(inode, (u64)i << PAGE_CACHE_SHIFT,
13146c282eb4SLi Zefan 					 extent_thresh, &last_len, &skip,
1315a43a2111SAndrew Mahone 					 &defrag_end, range->flags &
1316a43a2111SAndrew Mahone 					 BTRFS_DEFRAG_RANGE_COMPRESS)) {
1317940100a4SChris Mason 			unsigned long next;
1318940100a4SChris Mason 			/*
1319940100a4SChris Mason 			 * the should_defrag function tells us how much to skip
1320940100a4SChris Mason 			 * bump our counter by the suggested amount
1321940100a4SChris Mason 			 */
1322940100a4SChris Mason 			next = (skip + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
1323940100a4SChris Mason 			i = max(i + 1, next);
1324940100a4SChris Mason 			continue;
1325940100a4SChris Mason 		}
1326008873eaSLi Zefan 
1327008873eaSLi Zefan 		if (!newer_than) {
1328008873eaSLi Zefan 			cluster = (PAGE_CACHE_ALIGN(defrag_end) >>
1329008873eaSLi Zefan 				   PAGE_CACHE_SHIFT) - i;
1330008873eaSLi Zefan 			cluster = min(cluster, max_cluster);
1331008873eaSLi Zefan 		} else {
1332008873eaSLi Zefan 			cluster = max_cluster;
1333008873eaSLi Zefan 		}
1334008873eaSLi Zefan 
1335008873eaSLi Zefan 		if (i + cluster > ra_index) {
1336008873eaSLi Zefan 			ra_index = max(i, ra_index);
1337008873eaSLi Zefan 			btrfs_force_ra(inode->i_mapping, ra, file, ra_index,
1338008873eaSLi Zefan 				       cluster);
1339008873eaSLi Zefan 			ra_index += max_cluster;
1340008873eaSLi Zefan 		}
13414cb5300bSChris Mason 
1342ecb8bea8SLiu Bo 		mutex_lock(&inode->i_mutex);
1343633085c7SFilipe David Borba Manana 		if (range->flags & BTRFS_DEFRAG_RANGE_COMPRESS)
1344633085c7SFilipe David Borba Manana 			BTRFS_I(inode)->force_compress = compress_type;
1345008873eaSLi Zefan 		ret = cluster_pages_for_defrag(inode, pages, i, cluster);
1346ecb8bea8SLiu Bo 		if (ret < 0) {
1347ecb8bea8SLiu Bo 			mutex_unlock(&inode->i_mutex);
13484cb5300bSChris Mason 			goto out_ra;
1349ecb8bea8SLiu Bo 		}
13504cb5300bSChris Mason 
13514cb5300bSChris Mason 		defrag_count += ret;
1352d0e1d66bSNamjae Jeon 		balance_dirty_pages_ratelimited(inode->i_mapping);
1353ecb8bea8SLiu Bo 		mutex_unlock(&inode->i_mutex);
13544cb5300bSChris Mason 
13554cb5300bSChris Mason 		if (newer_than) {
13564cb5300bSChris Mason 			if (newer_off == (u64)-1)
13574cb5300bSChris Mason 				break;
13584cb5300bSChris Mason 
1359e1f041e1SLiu Bo 			if (ret > 0)
1360e1f041e1SLiu Bo 				i += ret;
1361e1f041e1SLiu Bo 
13624cb5300bSChris Mason 			newer_off = max(newer_off + 1,
13634cb5300bSChris Mason 					(u64)i << PAGE_CACHE_SHIFT);
13644cb5300bSChris Mason 
13654cb5300bSChris Mason 			ret = find_new_extents(root, inode,
13664cb5300bSChris Mason 					       newer_than, &newer_off,
13674cb5300bSChris Mason 					       64 * 1024);
13684cb5300bSChris Mason 			if (!ret) {
13694cb5300bSChris Mason 				range->start = newer_off;
13704cb5300bSChris Mason 				i = (newer_off & new_align) >> PAGE_CACHE_SHIFT;
13714cb5300bSChris Mason 			} else {
13724cb5300bSChris Mason 				break;
1373940100a4SChris Mason 			}
13744cb5300bSChris Mason 		} else {
1375008873eaSLi Zefan 			if (ret > 0) {
1376cbcc8326SLi Zefan 				i += ret;
1377008873eaSLi Zefan 				last_len += ret << PAGE_CACHE_SHIFT;
1378008873eaSLi Zefan 			} else {
1379940100a4SChris Mason 				i++;
1380008873eaSLi Zefan 				last_len = 0;
1381008873eaSLi Zefan 			}
1382f46b5a66SChristoph Hellwig 		}
13834cb5300bSChris Mason 	}
1384f46b5a66SChristoph Hellwig 
13851e701a32SChris Mason 	if ((range->flags & BTRFS_DEFRAG_RANGE_START_IO))
13861e701a32SChris Mason 		filemap_flush(inode->i_mapping);
13871e701a32SChris Mason 
13881e701a32SChris Mason 	if ((range->flags & BTRFS_DEFRAG_RANGE_COMPRESS)) {
13891e701a32SChris Mason 		/* the filemap_flush will queue IO into the worker threads, but
13901e701a32SChris Mason 		 * we have to make sure the IO is actually started and that
13911e701a32SChris Mason 		 * ordered extents get created before we return
13921e701a32SChris Mason 		 */
13931e701a32SChris Mason 		atomic_inc(&root->fs_info->async_submit_draining);
13941e701a32SChris Mason 		while (atomic_read(&root->fs_info->nr_async_submits) ||
13951e701a32SChris Mason 		      atomic_read(&root->fs_info->async_delalloc_pages)) {
13961e701a32SChris Mason 			wait_event(root->fs_info->async_submit_wait,
13971e701a32SChris Mason 			   (atomic_read(&root->fs_info->nr_async_submits) == 0 &&
13981e701a32SChris Mason 			    atomic_read(&root->fs_info->async_delalloc_pages) == 0));
13991e701a32SChris Mason 		}
14001e701a32SChris Mason 		atomic_dec(&root->fs_info->async_submit_draining);
14011e701a32SChris Mason 	}
14021e701a32SChris Mason 
14031a419d85SLi Zefan 	if (range->compress_type == BTRFS_COMPRESS_LZO) {
14042b0ce2c2SMitch Harder 		btrfs_set_fs_incompat(root->fs_info, COMPRESS_LZO);
14051a419d85SLi Zefan 	}
14061a419d85SLi Zefan 
140760ccf82fSDiego Calleja 	ret = defrag_count;
1408940100a4SChris Mason 
14094cb5300bSChris Mason out_ra:
1410633085c7SFilipe David Borba Manana 	if (range->flags & BTRFS_DEFRAG_RANGE_COMPRESS) {
1411633085c7SFilipe David Borba Manana 		mutex_lock(&inode->i_mutex);
1412633085c7SFilipe David Borba Manana 		BTRFS_I(inode)->force_compress = BTRFS_COMPRESS_NONE;
1413633085c7SFilipe David Borba Manana 		mutex_unlock(&inode->i_mutex);
1414633085c7SFilipe David Borba Manana 	}
14154cb5300bSChris Mason 	if (!file)
14164cb5300bSChris Mason 		kfree(ra);
14174cb5300bSChris Mason 	kfree(pages);
1418940100a4SChris Mason 	return ret;
1419f46b5a66SChristoph Hellwig }
1420f46b5a66SChristoph Hellwig 
1421198605a8SMiao Xie static noinline int btrfs_ioctl_resize(struct file *file,
142276dda93cSYan, Zheng 					void __user *arg)
1423f46b5a66SChristoph Hellwig {
1424f46b5a66SChristoph Hellwig 	u64 new_size;
1425f46b5a66SChristoph Hellwig 	u64 old_size;
1426f46b5a66SChristoph Hellwig 	u64 devid = 1;
1427496ad9aaSAl Viro 	struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
1428f46b5a66SChristoph Hellwig 	struct btrfs_ioctl_vol_args *vol_args;
1429f46b5a66SChristoph Hellwig 	struct btrfs_trans_handle *trans;
1430f46b5a66SChristoph Hellwig 	struct btrfs_device *device = NULL;
1431f46b5a66SChristoph Hellwig 	char *sizestr;
1432f46b5a66SChristoph Hellwig 	char *devstr = NULL;
1433f46b5a66SChristoph Hellwig 	int ret = 0;
1434f46b5a66SChristoph Hellwig 	int mod = 0;
1435f46b5a66SChristoph Hellwig 
1436e441d54dSChris Mason 	if (!capable(CAP_SYS_ADMIN))
1437e441d54dSChris Mason 		return -EPERM;
1438e441d54dSChris Mason 
1439198605a8SMiao Xie 	ret = mnt_want_write_file(file);
1440198605a8SMiao Xie 	if (ret)
1441198605a8SMiao Xie 		return ret;
1442198605a8SMiao Xie 
14435ac00addSStefan Behrens 	if (atomic_xchg(&root->fs_info->mutually_exclusive_operation_running,
14445ac00addSStefan Behrens 			1)) {
144597547676SMiao Xie 		mnt_drop_write_file(file);
1446e57138b3SAnand Jain 		return BTRFS_ERROR_DEV_EXCL_RUN_IN_PROGRESS;
1447c9e9f97bSIlya Dryomov 	}
1448c9e9f97bSIlya Dryomov 
14495ac00addSStefan Behrens 	mutex_lock(&root->fs_info->volume_mutex);
1450dae7b665SLi Zefan 	vol_args = memdup_user(arg, sizeof(*vol_args));
1451c9e9f97bSIlya Dryomov 	if (IS_ERR(vol_args)) {
1452c9e9f97bSIlya Dryomov 		ret = PTR_ERR(vol_args);
1453c9e9f97bSIlya Dryomov 		goto out;
1454c9e9f97bSIlya Dryomov 	}
14555516e595SMark Fasheh 
14565516e595SMark Fasheh 	vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
1457f46b5a66SChristoph Hellwig 
1458f46b5a66SChristoph Hellwig 	sizestr = vol_args->name;
1459f46b5a66SChristoph Hellwig 	devstr = strchr(sizestr, ':');
1460f46b5a66SChristoph Hellwig 	if (devstr) {
1461f46b5a66SChristoph Hellwig 		char *end;
1462f46b5a66SChristoph Hellwig 		sizestr = devstr + 1;
1463f46b5a66SChristoph Hellwig 		*devstr = '\0';
1464f46b5a66SChristoph Hellwig 		devstr = vol_args->name;
1465f46b5a66SChristoph Hellwig 		devid = simple_strtoull(devstr, &end, 10);
1466dfd79829SMiao Xie 		if (!devid) {
1467dfd79829SMiao Xie 			ret = -EINVAL;
1468dfd79829SMiao Xie 			goto out_free;
1469dfd79829SMiao Xie 		}
1470efe120a0SFrank Holton 		btrfs_info(root->fs_info, "resizing devid %llu", devid);
1471f46b5a66SChristoph Hellwig 	}
1472dba60f3fSMiao Xie 
1473aa1b8cd4SStefan Behrens 	device = btrfs_find_device(root->fs_info, devid, NULL, NULL);
1474f46b5a66SChristoph Hellwig 	if (!device) {
1475efe120a0SFrank Holton 		btrfs_info(root->fs_info, "resizer unable to find device %llu",
1476c1c9ff7cSGeert Uytterhoeven 		       devid);
1477dfd79829SMiao Xie 		ret = -ENODEV;
1478c9e9f97bSIlya Dryomov 		goto out_free;
1479f46b5a66SChristoph Hellwig 	}
1480dba60f3fSMiao Xie 
1481dba60f3fSMiao Xie 	if (!device->writeable) {
1482efe120a0SFrank Holton 		btrfs_info(root->fs_info,
1483efe120a0SFrank Holton 			   "resizer unable to apply on readonly device %llu",
1484c1c9ff7cSGeert Uytterhoeven 		       devid);
1485dfd79829SMiao Xie 		ret = -EPERM;
14864e42ae1bSLiu Bo 		goto out_free;
14874e42ae1bSLiu Bo 	}
14884e42ae1bSLiu Bo 
1489f46b5a66SChristoph Hellwig 	if (!strcmp(sizestr, "max"))
1490f46b5a66SChristoph Hellwig 		new_size = device->bdev->bd_inode->i_size;
1491f46b5a66SChristoph Hellwig 	else {
1492f46b5a66SChristoph Hellwig 		if (sizestr[0] == '-') {
1493f46b5a66SChristoph Hellwig 			mod = -1;
1494f46b5a66SChristoph Hellwig 			sizestr++;
1495f46b5a66SChristoph Hellwig 		} else if (sizestr[0] == '+') {
1496f46b5a66SChristoph Hellwig 			mod = 1;
1497f46b5a66SChristoph Hellwig 			sizestr++;
1498f46b5a66SChristoph Hellwig 		}
149991748467SAkinobu Mita 		new_size = memparse(sizestr, NULL);
1500f46b5a66SChristoph Hellwig 		if (new_size == 0) {
1501f46b5a66SChristoph Hellwig 			ret = -EINVAL;
1502c9e9f97bSIlya Dryomov 			goto out_free;
1503f46b5a66SChristoph Hellwig 		}
1504f46b5a66SChristoph Hellwig 	}
1505f46b5a66SChristoph Hellwig 
150663a212abSStefan Behrens 	if (device->is_tgtdev_for_dev_replace) {
1507dfd79829SMiao Xie 		ret = -EPERM;
150863a212abSStefan Behrens 		goto out_free;
150963a212abSStefan Behrens 	}
151063a212abSStefan Behrens 
1511f46b5a66SChristoph Hellwig 	old_size = device->total_bytes;
1512f46b5a66SChristoph Hellwig 
1513f46b5a66SChristoph Hellwig 	if (mod < 0) {
1514f46b5a66SChristoph Hellwig 		if (new_size > old_size) {
1515f46b5a66SChristoph Hellwig 			ret = -EINVAL;
1516c9e9f97bSIlya Dryomov 			goto out_free;
1517f46b5a66SChristoph Hellwig 		}
1518f46b5a66SChristoph Hellwig 		new_size = old_size - new_size;
1519f46b5a66SChristoph Hellwig 	} else if (mod > 0) {
1520eb8052e0SWenliang Fan 		if (new_size > ULLONG_MAX - old_size) {
1521eb8052e0SWenliang Fan 			ret = -EINVAL;
1522eb8052e0SWenliang Fan 			goto out_free;
1523eb8052e0SWenliang Fan 		}
1524f46b5a66SChristoph Hellwig 		new_size = old_size + new_size;
1525f46b5a66SChristoph Hellwig 	}
1526f46b5a66SChristoph Hellwig 
1527f46b5a66SChristoph Hellwig 	if (new_size < 256 * 1024 * 1024) {
1528f46b5a66SChristoph Hellwig 		ret = -EINVAL;
1529c9e9f97bSIlya Dryomov 		goto out_free;
1530f46b5a66SChristoph Hellwig 	}
1531f46b5a66SChristoph Hellwig 	if (new_size > device->bdev->bd_inode->i_size) {
1532f46b5a66SChristoph Hellwig 		ret = -EFBIG;
1533c9e9f97bSIlya Dryomov 		goto out_free;
1534f46b5a66SChristoph Hellwig 	}
1535f46b5a66SChristoph Hellwig 
1536f46b5a66SChristoph Hellwig 	do_div(new_size, root->sectorsize);
1537f46b5a66SChristoph Hellwig 	new_size *= root->sectorsize;
1538f46b5a66SChristoph Hellwig 
1539efe120a0SFrank Holton 	printk_in_rcu(KERN_INFO "BTRFS: new size for %s is %llu\n",
1540c1c9ff7cSGeert Uytterhoeven 		      rcu_str_deref(device->name), new_size);
1541f46b5a66SChristoph Hellwig 
1542f46b5a66SChristoph Hellwig 	if (new_size > old_size) {
1543a22285a6SYan, Zheng 		trans = btrfs_start_transaction(root, 0);
154498d5dc13STsutomu Itoh 		if (IS_ERR(trans)) {
154598d5dc13STsutomu Itoh 			ret = PTR_ERR(trans);
1546c9e9f97bSIlya Dryomov 			goto out_free;
154798d5dc13STsutomu Itoh 		}
1548f46b5a66SChristoph Hellwig 		ret = btrfs_grow_device(trans, device, new_size);
1549f46b5a66SChristoph Hellwig 		btrfs_commit_transaction(trans, root);
1550ece7d20eSMike Fleetwood 	} else if (new_size < old_size) {
1551f46b5a66SChristoph Hellwig 		ret = btrfs_shrink_device(device, new_size);
15520253f40eSjeff.liu 	} /* equal, nothing need to do */
1553f46b5a66SChristoph Hellwig 
1554c9e9f97bSIlya Dryomov out_free:
1555f46b5a66SChristoph Hellwig 	kfree(vol_args);
1556c9e9f97bSIlya Dryomov out:
1557c9e9f97bSIlya Dryomov 	mutex_unlock(&root->fs_info->volume_mutex);
15585ac00addSStefan Behrens 	atomic_set(&root->fs_info->mutually_exclusive_operation_running, 0);
155918f39c41SIlya Dryomov 	mnt_drop_write_file(file);
1560f46b5a66SChristoph Hellwig 	return ret;
1561f46b5a66SChristoph Hellwig }
1562f46b5a66SChristoph Hellwig 
156372fd032eSSage Weil static noinline int btrfs_ioctl_snap_create_transid(struct file *file,
15646f72c7e2SArne Jansen 				char *name, unsigned long fd, int subvol,
15656f72c7e2SArne Jansen 				u64 *transid, bool readonly,
15668696c533SMiao Xie 				struct btrfs_qgroup_inherit *inherit)
1567f46b5a66SChristoph Hellwig {
1568f46b5a66SChristoph Hellwig 	int namelen;
15693de4586cSChris Mason 	int ret = 0;
1570f46b5a66SChristoph Hellwig 
1571a874a63eSLiu Bo 	ret = mnt_want_write_file(file);
1572a874a63eSLiu Bo 	if (ret)
1573a874a63eSLiu Bo 		goto out;
1574a874a63eSLiu Bo 
157572fd032eSSage Weil 	namelen = strlen(name);
157672fd032eSSage Weil 	if (strchr(name, '/')) {
1577f46b5a66SChristoph Hellwig 		ret = -EINVAL;
1578a874a63eSLiu Bo 		goto out_drop_write;
1579f46b5a66SChristoph Hellwig 	}
1580f46b5a66SChristoph Hellwig 
158116780cabSChris Mason 	if (name[0] == '.' &&
158216780cabSChris Mason 	   (namelen == 1 || (name[1] == '.' && namelen == 2))) {
158316780cabSChris Mason 		ret = -EEXIST;
1584a874a63eSLiu Bo 		goto out_drop_write;
158516780cabSChris Mason 	}
158616780cabSChris Mason 
15873de4586cSChris Mason 	if (subvol) {
158872fd032eSSage Weil 		ret = btrfs_mksubvol(&file->f_path, name, namelen,
15896f72c7e2SArne Jansen 				     NULL, transid, readonly, inherit);
1590cb8e7090SChristoph Hellwig 	} else {
15912903ff01SAl Viro 		struct fd src = fdget(fd);
15923de4586cSChris Mason 		struct inode *src_inode;
15932903ff01SAl Viro 		if (!src.file) {
15943de4586cSChris Mason 			ret = -EINVAL;
1595a874a63eSLiu Bo 			goto out_drop_write;
15963de4586cSChris Mason 		}
15973de4586cSChris Mason 
1598496ad9aaSAl Viro 		src_inode = file_inode(src.file);
1599496ad9aaSAl Viro 		if (src_inode->i_sb != file_inode(file)->i_sb) {
1600efe120a0SFrank Holton 			btrfs_info(BTRFS_I(src_inode)->root->fs_info,
1601efe120a0SFrank Holton 				   "Snapshot src from another FS");
160223ad5b17SKusanagi Kouichi 			ret = -EXDEV;
1603d0242061SDavid Sterba 		} else if (!inode_owner_or_capable(src_inode)) {
1604d0242061SDavid Sterba 			/*
1605d0242061SDavid Sterba 			 * Subvolume creation is not restricted, but snapshots
1606d0242061SDavid Sterba 			 * are limited to own subvolumes only
1607d0242061SDavid Sterba 			 */
1608d0242061SDavid Sterba 			ret = -EPERM;
1609ecd18815SAl Viro 		} else {
161072fd032eSSage Weil 			ret = btrfs_mksubvol(&file->f_path, name, namelen,
161172fd032eSSage Weil 					     BTRFS_I(src_inode)->root,
16126f72c7e2SArne Jansen 					     transid, readonly, inherit);
1613ecd18815SAl Viro 		}
16142903ff01SAl Viro 		fdput(src);
1615cb8e7090SChristoph Hellwig 	}
1616a874a63eSLiu Bo out_drop_write:
1617a874a63eSLiu Bo 	mnt_drop_write_file(file);
1618f46b5a66SChristoph Hellwig out:
161972fd032eSSage Weil 	return ret;
162072fd032eSSage Weil }
162172fd032eSSage Weil 
162272fd032eSSage Weil static noinline int btrfs_ioctl_snap_create(struct file *file,
1623fa0d2b9bSLi Zefan 					    void __user *arg, int subvol)
162472fd032eSSage Weil {
1625fa0d2b9bSLi Zefan 	struct btrfs_ioctl_vol_args *vol_args;
162672fd032eSSage Weil 	int ret;
162772fd032eSSage Weil 
1628fa0d2b9bSLi Zefan 	vol_args = memdup_user(arg, sizeof(*vol_args));
1629fa0d2b9bSLi Zefan 	if (IS_ERR(vol_args))
1630fa0d2b9bSLi Zefan 		return PTR_ERR(vol_args);
1631fa0d2b9bSLi Zefan 	vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
1632fa0d2b9bSLi Zefan 
1633fa0d2b9bSLi Zefan 	ret = btrfs_ioctl_snap_create_transid(file, vol_args->name,
1634b83cc969SLi Zefan 					      vol_args->fd, subvol,
16356f72c7e2SArne Jansen 					      NULL, false, NULL);
1636fa0d2b9bSLi Zefan 
1637fa0d2b9bSLi Zefan 	kfree(vol_args);
1638fa0d2b9bSLi Zefan 	return ret;
1639fa0d2b9bSLi Zefan }
1640fa0d2b9bSLi Zefan 
1641fa0d2b9bSLi Zefan static noinline int btrfs_ioctl_snap_create_v2(struct file *file,
1642fa0d2b9bSLi Zefan 					       void __user *arg, int subvol)
1643fa0d2b9bSLi Zefan {
1644fa0d2b9bSLi Zefan 	struct btrfs_ioctl_vol_args_v2 *vol_args;
1645fa0d2b9bSLi Zefan 	int ret;
1646fdfb1e4fSLi Zefan 	u64 transid = 0;
1647fdfb1e4fSLi Zefan 	u64 *ptr = NULL;
1648b83cc969SLi Zefan 	bool readonly = false;
16496f72c7e2SArne Jansen 	struct btrfs_qgroup_inherit *inherit = NULL;
165072fd032eSSage Weil 
1651fa0d2b9bSLi Zefan 	vol_args = memdup_user(arg, sizeof(*vol_args));
1652fa0d2b9bSLi Zefan 	if (IS_ERR(vol_args))
1653fa0d2b9bSLi Zefan 		return PTR_ERR(vol_args);
1654fa0d2b9bSLi Zefan 	vol_args->name[BTRFS_SUBVOL_NAME_MAX] = '\0';
1655fdfb1e4fSLi Zefan 
1656b83cc969SLi Zefan 	if (vol_args->flags &
16576f72c7e2SArne Jansen 	    ~(BTRFS_SUBVOL_CREATE_ASYNC | BTRFS_SUBVOL_RDONLY |
16586f72c7e2SArne Jansen 	      BTRFS_SUBVOL_QGROUP_INHERIT)) {
1659b83cc969SLi Zefan 		ret = -EOPNOTSUPP;
1660fdfb1e4fSLi Zefan 		goto out;
1661fdfb1e4fSLi Zefan 	}
1662fdfb1e4fSLi Zefan 
1663fa0d2b9bSLi Zefan 	if (vol_args->flags & BTRFS_SUBVOL_CREATE_ASYNC)
1664fdfb1e4fSLi Zefan 		ptr = &transid;
1665b83cc969SLi Zefan 	if (vol_args->flags & BTRFS_SUBVOL_RDONLY)
1666b83cc969SLi Zefan 		readonly = true;
16676f72c7e2SArne Jansen 	if (vol_args->flags & BTRFS_SUBVOL_QGROUP_INHERIT) {
16686f72c7e2SArne Jansen 		if (vol_args->size > PAGE_CACHE_SIZE) {
16696f72c7e2SArne Jansen 			ret = -EINVAL;
16706f72c7e2SArne Jansen 			goto out;
16716f72c7e2SArne Jansen 		}
16726f72c7e2SArne Jansen 		inherit = memdup_user(vol_args->qgroup_inherit, vol_args->size);
16736f72c7e2SArne Jansen 		if (IS_ERR(inherit)) {
16746f72c7e2SArne Jansen 			ret = PTR_ERR(inherit);
16756f72c7e2SArne Jansen 			goto out;
16766f72c7e2SArne Jansen 		}
16776f72c7e2SArne Jansen 	}
167875eaa0e2SSage Weil 
1679fa0d2b9bSLi Zefan 	ret = btrfs_ioctl_snap_create_transid(file, vol_args->name,
16806f72c7e2SArne Jansen 					      vol_args->fd, subvol, ptr,
16818696c533SMiao Xie 					      readonly, inherit);
168275eaa0e2SSage Weil 
1683fdfb1e4fSLi Zefan 	if (ret == 0 && ptr &&
168475eaa0e2SSage Weil 	    copy_to_user(arg +
1685fdfb1e4fSLi Zefan 			 offsetof(struct btrfs_ioctl_vol_args_v2,
1686fdfb1e4fSLi Zefan 				  transid), ptr, sizeof(*ptr)))
168775eaa0e2SSage Weil 		ret = -EFAULT;
1688fdfb1e4fSLi Zefan out:
1689f46b5a66SChristoph Hellwig 	kfree(vol_args);
16906f72c7e2SArne Jansen 	kfree(inherit);
1691f46b5a66SChristoph Hellwig 	return ret;
1692f46b5a66SChristoph Hellwig }
1693f46b5a66SChristoph Hellwig 
16940caa102dSLi Zefan static noinline int btrfs_ioctl_subvol_getflags(struct file *file,
16950caa102dSLi Zefan 						void __user *arg)
16960caa102dSLi Zefan {
1697496ad9aaSAl Viro 	struct inode *inode = file_inode(file);
16980caa102dSLi Zefan 	struct btrfs_root *root = BTRFS_I(inode)->root;
16990caa102dSLi Zefan 	int ret = 0;
17000caa102dSLi Zefan 	u64 flags = 0;
17010caa102dSLi Zefan 
170233345d01SLi Zefan 	if (btrfs_ino(inode) != BTRFS_FIRST_FREE_OBJECTID)
17030caa102dSLi Zefan 		return -EINVAL;
17040caa102dSLi Zefan 
17050caa102dSLi Zefan 	down_read(&root->fs_info->subvol_sem);
17060caa102dSLi Zefan 	if (btrfs_root_readonly(root))
17070caa102dSLi Zefan 		flags |= BTRFS_SUBVOL_RDONLY;
17080caa102dSLi Zefan 	up_read(&root->fs_info->subvol_sem);
17090caa102dSLi Zefan 
17100caa102dSLi Zefan 	if (copy_to_user(arg, &flags, sizeof(flags)))
17110caa102dSLi Zefan 		ret = -EFAULT;
17120caa102dSLi Zefan 
17130caa102dSLi Zefan 	return ret;
17140caa102dSLi Zefan }
17150caa102dSLi Zefan 
17160caa102dSLi Zefan static noinline int btrfs_ioctl_subvol_setflags(struct file *file,
17170caa102dSLi Zefan 					      void __user *arg)
17180caa102dSLi Zefan {
1719496ad9aaSAl Viro 	struct inode *inode = file_inode(file);
17200caa102dSLi Zefan 	struct btrfs_root *root = BTRFS_I(inode)->root;
17210caa102dSLi Zefan 	struct btrfs_trans_handle *trans;
17220caa102dSLi Zefan 	u64 root_flags;
17230caa102dSLi Zefan 	u64 flags;
17240caa102dSLi Zefan 	int ret = 0;
17250caa102dSLi Zefan 
1726bd60ea0fSDavid Sterba 	if (!inode_owner_or_capable(inode))
1727bd60ea0fSDavid Sterba 		return -EPERM;
1728bd60ea0fSDavid Sterba 
1729b9ca0664SLiu Bo 	ret = mnt_want_write_file(file);
1730b9ca0664SLiu Bo 	if (ret)
1731b9ca0664SLiu Bo 		goto out;
17320caa102dSLi Zefan 
1733b9ca0664SLiu Bo 	if (btrfs_ino(inode) != BTRFS_FIRST_FREE_OBJECTID) {
1734b9ca0664SLiu Bo 		ret = -EINVAL;
1735b9ca0664SLiu Bo 		goto out_drop_write;
1736b9ca0664SLiu Bo 	}
17370caa102dSLi Zefan 
1738b9ca0664SLiu Bo 	if (copy_from_user(&flags, arg, sizeof(flags))) {
1739b9ca0664SLiu Bo 		ret = -EFAULT;
1740b9ca0664SLiu Bo 		goto out_drop_write;
1741b9ca0664SLiu Bo 	}
17420caa102dSLi Zefan 
1743b9ca0664SLiu Bo 	if (flags & BTRFS_SUBVOL_CREATE_ASYNC) {
1744b9ca0664SLiu Bo 		ret = -EINVAL;
1745b9ca0664SLiu Bo 		goto out_drop_write;
1746b9ca0664SLiu Bo 	}
17470caa102dSLi Zefan 
1748b9ca0664SLiu Bo 	if (flags & ~BTRFS_SUBVOL_RDONLY) {
1749b9ca0664SLiu Bo 		ret = -EOPNOTSUPP;
1750b9ca0664SLiu Bo 		goto out_drop_write;
1751b9ca0664SLiu Bo 	}
17520caa102dSLi Zefan 
17530caa102dSLi Zefan 	down_write(&root->fs_info->subvol_sem);
17540caa102dSLi Zefan 
17550caa102dSLi Zefan 	/* nothing to do */
17560caa102dSLi Zefan 	if (!!(flags & BTRFS_SUBVOL_RDONLY) == btrfs_root_readonly(root))
1757b9ca0664SLiu Bo 		goto out_drop_sem;
17580caa102dSLi Zefan 
17590caa102dSLi Zefan 	root_flags = btrfs_root_flags(&root->root_item);
17602c686537SDavid Sterba 	if (flags & BTRFS_SUBVOL_RDONLY) {
17610caa102dSLi Zefan 		btrfs_set_root_flags(&root->root_item,
17620caa102dSLi Zefan 				     root_flags | BTRFS_ROOT_SUBVOL_RDONLY);
17632c686537SDavid Sterba 	} else {
17642c686537SDavid Sterba 		/*
17652c686537SDavid Sterba 		 * Block RO -> RW transition if this subvolume is involved in
17662c686537SDavid Sterba 		 * send
17672c686537SDavid Sterba 		 */
17682c686537SDavid Sterba 		spin_lock(&root->root_item_lock);
17692c686537SDavid Sterba 		if (root->send_in_progress == 0) {
17700caa102dSLi Zefan 			btrfs_set_root_flags(&root->root_item,
17710caa102dSLi Zefan 				     root_flags & ~BTRFS_ROOT_SUBVOL_RDONLY);
17722c686537SDavid Sterba 			spin_unlock(&root->root_item_lock);
17732c686537SDavid Sterba 		} else {
17742c686537SDavid Sterba 			spin_unlock(&root->root_item_lock);
17752c686537SDavid Sterba 			btrfs_warn(root->fs_info,
17762c686537SDavid Sterba 			"Attempt to set subvolume %llu read-write during send",
17772c686537SDavid Sterba 					root->root_key.objectid);
17782c686537SDavid Sterba 			ret = -EPERM;
17792c686537SDavid Sterba 			goto out_drop_sem;
17802c686537SDavid Sterba 		}
17812c686537SDavid Sterba 	}
17820caa102dSLi Zefan 
17830caa102dSLi Zefan 	trans = btrfs_start_transaction(root, 1);
17840caa102dSLi Zefan 	if (IS_ERR(trans)) {
17850caa102dSLi Zefan 		ret = PTR_ERR(trans);
17860caa102dSLi Zefan 		goto out_reset;
17870caa102dSLi Zefan 	}
17880caa102dSLi Zefan 
1789b4dc2b8cSLi Zefan 	ret = btrfs_update_root(trans, root->fs_info->tree_root,
17900caa102dSLi Zefan 				&root->root_key, &root->root_item);
17910caa102dSLi Zefan 
17920caa102dSLi Zefan 	btrfs_commit_transaction(trans, root);
17930caa102dSLi Zefan out_reset:
17940caa102dSLi Zefan 	if (ret)
17950caa102dSLi Zefan 		btrfs_set_root_flags(&root->root_item, root_flags);
1796b9ca0664SLiu Bo out_drop_sem:
17970caa102dSLi Zefan 	up_write(&root->fs_info->subvol_sem);
1798b9ca0664SLiu Bo out_drop_write:
1799b9ca0664SLiu Bo 	mnt_drop_write_file(file);
1800b9ca0664SLiu Bo out:
18010caa102dSLi Zefan 	return ret;
18020caa102dSLi Zefan }
18030caa102dSLi Zefan 
180476dda93cSYan, Zheng /*
180576dda93cSYan, Zheng  * helper to check if the subvolume references other subvolumes
180676dda93cSYan, Zheng  */
180776dda93cSYan, Zheng static noinline int may_destroy_subvol(struct btrfs_root *root)
180876dda93cSYan, Zheng {
180976dda93cSYan, Zheng 	struct btrfs_path *path;
1810175a2b87SJosef Bacik 	struct btrfs_dir_item *di;
181176dda93cSYan, Zheng 	struct btrfs_key key;
1812175a2b87SJosef Bacik 	u64 dir_id;
181376dda93cSYan, Zheng 	int ret;
181476dda93cSYan, Zheng 
181576dda93cSYan, Zheng 	path = btrfs_alloc_path();
181676dda93cSYan, Zheng 	if (!path)
181776dda93cSYan, Zheng 		return -ENOMEM;
181876dda93cSYan, Zheng 
1819175a2b87SJosef Bacik 	/* Make sure this root isn't set as the default subvol */
1820175a2b87SJosef Bacik 	dir_id = btrfs_super_root_dir(root->fs_info->super_copy);
1821175a2b87SJosef Bacik 	di = btrfs_lookup_dir_item(NULL, root->fs_info->tree_root, path,
1822175a2b87SJosef Bacik 				   dir_id, "default", 7, 0);
1823175a2b87SJosef Bacik 	if (di && !IS_ERR(di)) {
1824175a2b87SJosef Bacik 		btrfs_dir_item_key_to_cpu(path->nodes[0], di, &key);
1825175a2b87SJosef Bacik 		if (key.objectid == root->root_key.objectid) {
1826175a2b87SJosef Bacik 			ret = -ENOTEMPTY;
1827175a2b87SJosef Bacik 			goto out;
1828175a2b87SJosef Bacik 		}
1829175a2b87SJosef Bacik 		btrfs_release_path(path);
1830175a2b87SJosef Bacik 	}
1831175a2b87SJosef Bacik 
183276dda93cSYan, Zheng 	key.objectid = root->root_key.objectid;
183376dda93cSYan, Zheng 	key.type = BTRFS_ROOT_REF_KEY;
183476dda93cSYan, Zheng 	key.offset = (u64)-1;
183576dda93cSYan, Zheng 
183676dda93cSYan, Zheng 	ret = btrfs_search_slot(NULL, root->fs_info->tree_root,
183776dda93cSYan, Zheng 				&key, path, 0, 0);
183876dda93cSYan, Zheng 	if (ret < 0)
183976dda93cSYan, Zheng 		goto out;
184076dda93cSYan, Zheng 	BUG_ON(ret == 0);
184176dda93cSYan, Zheng 
184276dda93cSYan, Zheng 	ret = 0;
184376dda93cSYan, Zheng 	if (path->slots[0] > 0) {
184476dda93cSYan, Zheng 		path->slots[0]--;
184576dda93cSYan, Zheng 		btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
184676dda93cSYan, Zheng 		if (key.objectid == root->root_key.objectid &&
184776dda93cSYan, Zheng 		    key.type == BTRFS_ROOT_REF_KEY)
184876dda93cSYan, Zheng 			ret = -ENOTEMPTY;
184976dda93cSYan, Zheng 	}
185076dda93cSYan, Zheng out:
185176dda93cSYan, Zheng 	btrfs_free_path(path);
185276dda93cSYan, Zheng 	return ret;
185376dda93cSYan, Zheng }
185476dda93cSYan, Zheng 
1855ac8e9819SChris Mason static noinline int key_in_sk(struct btrfs_key *key,
1856ac8e9819SChris Mason 			      struct btrfs_ioctl_search_key *sk)
1857ac8e9819SChris Mason {
1858abc6e134SChris Mason 	struct btrfs_key test;
1859abc6e134SChris Mason 	int ret;
1860abc6e134SChris Mason 
1861abc6e134SChris Mason 	test.objectid = sk->min_objectid;
1862abc6e134SChris Mason 	test.type = sk->min_type;
1863abc6e134SChris Mason 	test.offset = sk->min_offset;
1864abc6e134SChris Mason 
1865abc6e134SChris Mason 	ret = btrfs_comp_cpu_keys(key, &test);
1866abc6e134SChris Mason 	if (ret < 0)
1867ac8e9819SChris Mason 		return 0;
1868abc6e134SChris Mason 
1869abc6e134SChris Mason 	test.objectid = sk->max_objectid;
1870abc6e134SChris Mason 	test.type = sk->max_type;
1871abc6e134SChris Mason 	test.offset = sk->max_offset;
1872abc6e134SChris Mason 
1873abc6e134SChris Mason 	ret = btrfs_comp_cpu_keys(key, &test);
1874abc6e134SChris Mason 	if (ret > 0)
1875ac8e9819SChris Mason 		return 0;
1876ac8e9819SChris Mason 	return 1;
1877ac8e9819SChris Mason }
1878ac8e9819SChris Mason 
1879ac8e9819SChris Mason static noinline int copy_to_sk(struct btrfs_root *root,
1880ac8e9819SChris Mason 			       struct btrfs_path *path,
1881ac8e9819SChris Mason 			       struct btrfs_key *key,
1882ac8e9819SChris Mason 			       struct btrfs_ioctl_search_key *sk,
1883ac8e9819SChris Mason 			       char *buf,
1884ac8e9819SChris Mason 			       unsigned long *sk_offset,
1885ac8e9819SChris Mason 			       int *num_found)
1886ac8e9819SChris Mason {
1887ac8e9819SChris Mason 	u64 found_transid;
1888ac8e9819SChris Mason 	struct extent_buffer *leaf;
1889ac8e9819SChris Mason 	struct btrfs_ioctl_search_header sh;
1890ac8e9819SChris Mason 	unsigned long item_off;
1891ac8e9819SChris Mason 	unsigned long item_len;
1892ac8e9819SChris Mason 	int nritems;
1893ac8e9819SChris Mason 	int i;
1894ac8e9819SChris Mason 	int slot;
1895ac8e9819SChris Mason 	int ret = 0;
1896ac8e9819SChris Mason 
1897ac8e9819SChris Mason 	leaf = path->nodes[0];
1898ac8e9819SChris Mason 	slot = path->slots[0];
1899ac8e9819SChris Mason 	nritems = btrfs_header_nritems(leaf);
1900ac8e9819SChris Mason 
1901ac8e9819SChris Mason 	if (btrfs_header_generation(leaf) > sk->max_transid) {
1902ac8e9819SChris Mason 		i = nritems;
1903ac8e9819SChris Mason 		goto advance_key;
1904ac8e9819SChris Mason 	}
1905ac8e9819SChris Mason 	found_transid = btrfs_header_generation(leaf);
1906ac8e9819SChris Mason 
1907ac8e9819SChris Mason 	for (i = slot; i < nritems; i++) {
1908ac8e9819SChris Mason 		item_off = btrfs_item_ptr_offset(leaf, i);
1909ac8e9819SChris Mason 		item_len = btrfs_item_size_nr(leaf, i);
1910ac8e9819SChris Mason 
191103b71c6cSGabriel de Perthuis 		btrfs_item_key_to_cpu(leaf, key, i);
191203b71c6cSGabriel de Perthuis 		if (!key_in_sk(key, sk))
191303b71c6cSGabriel de Perthuis 			continue;
191403b71c6cSGabriel de Perthuis 
191503b71c6cSGabriel de Perthuis 		if (sizeof(sh) + item_len > BTRFS_SEARCH_ARGS_BUFSIZE)
1916ac8e9819SChris Mason 			item_len = 0;
1917ac8e9819SChris Mason 
1918ac8e9819SChris Mason 		if (sizeof(sh) + item_len + *sk_offset >
1919ac8e9819SChris Mason 		    BTRFS_SEARCH_ARGS_BUFSIZE) {
1920ac8e9819SChris Mason 			ret = 1;
1921ac8e9819SChris Mason 			goto overflow;
1922ac8e9819SChris Mason 		}
1923ac8e9819SChris Mason 
1924ac8e9819SChris Mason 		sh.objectid = key->objectid;
1925ac8e9819SChris Mason 		sh.offset = key->offset;
1926ac8e9819SChris Mason 		sh.type = key->type;
1927ac8e9819SChris Mason 		sh.len = item_len;
1928ac8e9819SChris Mason 		sh.transid = found_transid;
1929ac8e9819SChris Mason 
1930ac8e9819SChris Mason 		/* copy search result header */
1931ac8e9819SChris Mason 		memcpy(buf + *sk_offset, &sh, sizeof(sh));
1932ac8e9819SChris Mason 		*sk_offset += sizeof(sh);
1933ac8e9819SChris Mason 
1934ac8e9819SChris Mason 		if (item_len) {
1935ac8e9819SChris Mason 			char *p = buf + *sk_offset;
1936ac8e9819SChris Mason 			/* copy the item */
1937ac8e9819SChris Mason 			read_extent_buffer(leaf, p,
1938ac8e9819SChris Mason 					   item_off, item_len);
1939ac8e9819SChris Mason 			*sk_offset += item_len;
1940ac8e9819SChris Mason 		}
1941e2156867SHugo Mills 		(*num_found)++;
1942ac8e9819SChris Mason 
1943ac8e9819SChris Mason 		if (*num_found >= sk->nr_items)
1944ac8e9819SChris Mason 			break;
1945ac8e9819SChris Mason 	}
1946ac8e9819SChris Mason advance_key:
1947ac8e9819SChris Mason 	ret = 0;
1948abc6e134SChris Mason 	if (key->offset < (u64)-1 && key->offset < sk->max_offset)
1949abc6e134SChris Mason 		key->offset++;
1950abc6e134SChris Mason 	else if (key->type < (u8)-1 && key->type < sk->max_type) {
1951abc6e134SChris Mason 		key->offset = 0;
1952abc6e134SChris Mason 		key->type++;
1953abc6e134SChris Mason 	} else if (key->objectid < (u64)-1 && key->objectid < sk->max_objectid) {
1954abc6e134SChris Mason 		key->offset = 0;
1955abc6e134SChris Mason 		key->type = 0;
1956abc6e134SChris Mason 		key->objectid++;
1957abc6e134SChris Mason 	} else
1958abc6e134SChris Mason 		ret = 1;
1959ac8e9819SChris Mason overflow:
1960ac8e9819SChris Mason 	return ret;
1961ac8e9819SChris Mason }
1962ac8e9819SChris Mason 
1963ac8e9819SChris Mason static noinline int search_ioctl(struct inode *inode,
1964ac8e9819SChris Mason 				 struct btrfs_ioctl_search_args *args)
1965ac8e9819SChris Mason {
1966ac8e9819SChris Mason 	struct btrfs_root *root;
1967ac8e9819SChris Mason 	struct btrfs_key key;
1968ac8e9819SChris Mason 	struct btrfs_path *path;
1969ac8e9819SChris Mason 	struct btrfs_ioctl_search_key *sk = &args->key;
1970ac8e9819SChris Mason 	struct btrfs_fs_info *info = BTRFS_I(inode)->root->fs_info;
1971ac8e9819SChris Mason 	int ret;
1972ac8e9819SChris Mason 	int num_found = 0;
1973ac8e9819SChris Mason 	unsigned long sk_offset = 0;
1974ac8e9819SChris Mason 
1975ac8e9819SChris Mason 	path = btrfs_alloc_path();
1976ac8e9819SChris Mason 	if (!path)
1977ac8e9819SChris Mason 		return -ENOMEM;
1978ac8e9819SChris Mason 
1979ac8e9819SChris Mason 	if (sk->tree_id == 0) {
1980ac8e9819SChris Mason 		/* search the root of the inode that was passed */
1981ac8e9819SChris Mason 		root = BTRFS_I(inode)->root;
1982ac8e9819SChris Mason 	} else {
1983ac8e9819SChris Mason 		key.objectid = sk->tree_id;
1984ac8e9819SChris Mason 		key.type = BTRFS_ROOT_ITEM_KEY;
1985ac8e9819SChris Mason 		key.offset = (u64)-1;
1986ac8e9819SChris Mason 		root = btrfs_read_fs_root_no_name(info, &key);
1987ac8e9819SChris Mason 		if (IS_ERR(root)) {
1988efe120a0SFrank Holton 			printk(KERN_ERR "BTRFS: could not find root %llu\n",
1989ac8e9819SChris Mason 			       sk->tree_id);
1990ac8e9819SChris Mason 			btrfs_free_path(path);
1991ac8e9819SChris Mason 			return -ENOENT;
1992ac8e9819SChris Mason 		}
1993ac8e9819SChris Mason 	}
1994ac8e9819SChris Mason 
1995ac8e9819SChris Mason 	key.objectid = sk->min_objectid;
1996ac8e9819SChris Mason 	key.type = sk->min_type;
1997ac8e9819SChris Mason 	key.offset = sk->min_offset;
1998ac8e9819SChris Mason 
1999ac8e9819SChris Mason 	path->keep_locks = 1;
2000ac8e9819SChris Mason 
2001ac8e9819SChris Mason 	while (1) {
20026174d3cbSFilipe David Borba Manana 		ret = btrfs_search_forward(root, &key, path, sk->min_transid);
2003ac8e9819SChris Mason 		if (ret != 0) {
2004ac8e9819SChris Mason 			if (ret > 0)
2005ac8e9819SChris Mason 				ret = 0;
2006ac8e9819SChris Mason 			goto err;
2007ac8e9819SChris Mason 		}
2008ac8e9819SChris Mason 		ret = copy_to_sk(root, path, &key, sk, args->buf,
2009ac8e9819SChris Mason 				 &sk_offset, &num_found);
2010b3b4aa74SDavid Sterba 		btrfs_release_path(path);
2011ac8e9819SChris Mason 		if (ret || num_found >= sk->nr_items)
2012ac8e9819SChris Mason 			break;
2013ac8e9819SChris Mason 
2014ac8e9819SChris Mason 	}
2015ac8e9819SChris Mason 	ret = 0;
2016ac8e9819SChris Mason err:
2017ac8e9819SChris Mason 	sk->nr_items = num_found;
2018ac8e9819SChris Mason 	btrfs_free_path(path);
2019ac8e9819SChris Mason 	return ret;
2020ac8e9819SChris Mason }
2021ac8e9819SChris Mason 
2022ac8e9819SChris Mason static noinline int btrfs_ioctl_tree_search(struct file *file,
2023ac8e9819SChris Mason 					   void __user *argp)
2024ac8e9819SChris Mason {
2025ac8e9819SChris Mason 	 struct btrfs_ioctl_search_args *args;
2026ac8e9819SChris Mason 	 struct inode *inode;
2027ac8e9819SChris Mason 	 int ret;
2028ac8e9819SChris Mason 
2029ac8e9819SChris Mason 	if (!capable(CAP_SYS_ADMIN))
2030ac8e9819SChris Mason 		return -EPERM;
2031ac8e9819SChris Mason 
20322354d08fSJulia Lawall 	args = memdup_user(argp, sizeof(*args));
20332354d08fSJulia Lawall 	if (IS_ERR(args))
20342354d08fSJulia Lawall 		return PTR_ERR(args);
2035ac8e9819SChris Mason 
2036496ad9aaSAl Viro 	inode = file_inode(file);
2037ac8e9819SChris Mason 	ret = search_ioctl(inode, args);
2038ac8e9819SChris Mason 	if (ret == 0 && copy_to_user(argp, args, sizeof(*args)))
2039ac8e9819SChris Mason 		ret = -EFAULT;
2040ac8e9819SChris Mason 	kfree(args);
2041ac8e9819SChris Mason 	return ret;
2042ac8e9819SChris Mason }
2043ac8e9819SChris Mason 
204498d377a0STARUISI Hiroaki /*
2045ac8e9819SChris Mason  * Search INODE_REFs to identify path name of 'dirid' directory
2046ac8e9819SChris Mason  * in a 'tree_id' tree. and sets path name to 'name'.
204798d377a0STARUISI Hiroaki  */
204898d377a0STARUISI Hiroaki static noinline int btrfs_search_path_in_tree(struct btrfs_fs_info *info,
204998d377a0STARUISI Hiroaki 				u64 tree_id, u64 dirid, char *name)
205098d377a0STARUISI Hiroaki {
205198d377a0STARUISI Hiroaki 	struct btrfs_root *root;
205298d377a0STARUISI Hiroaki 	struct btrfs_key key;
2053ac8e9819SChris Mason 	char *ptr;
205498d377a0STARUISI Hiroaki 	int ret = -1;
205598d377a0STARUISI Hiroaki 	int slot;
205698d377a0STARUISI Hiroaki 	int len;
205798d377a0STARUISI Hiroaki 	int total_len = 0;
205898d377a0STARUISI Hiroaki 	struct btrfs_inode_ref *iref;
205998d377a0STARUISI Hiroaki 	struct extent_buffer *l;
206098d377a0STARUISI Hiroaki 	struct btrfs_path *path;
206198d377a0STARUISI Hiroaki 
206298d377a0STARUISI Hiroaki 	if (dirid == BTRFS_FIRST_FREE_OBJECTID) {
206398d377a0STARUISI Hiroaki 		name[0]='\0';
206498d377a0STARUISI Hiroaki 		return 0;
206598d377a0STARUISI Hiroaki 	}
206698d377a0STARUISI Hiroaki 
206798d377a0STARUISI Hiroaki 	path = btrfs_alloc_path();
206898d377a0STARUISI Hiroaki 	if (!path)
206998d377a0STARUISI Hiroaki 		return -ENOMEM;
207098d377a0STARUISI Hiroaki 
2071ac8e9819SChris Mason 	ptr = &name[BTRFS_INO_LOOKUP_PATH_MAX];
207298d377a0STARUISI Hiroaki 
207398d377a0STARUISI Hiroaki 	key.objectid = tree_id;
207498d377a0STARUISI Hiroaki 	key.type = BTRFS_ROOT_ITEM_KEY;
207598d377a0STARUISI Hiroaki 	key.offset = (u64)-1;
207698d377a0STARUISI Hiroaki 	root = btrfs_read_fs_root_no_name(info, &key);
207798d377a0STARUISI Hiroaki 	if (IS_ERR(root)) {
2078efe120a0SFrank Holton 		printk(KERN_ERR "BTRFS: could not find root %llu\n", tree_id);
20798ad6fcabSChris Mason 		ret = -ENOENT;
20808ad6fcabSChris Mason 		goto out;
208198d377a0STARUISI Hiroaki 	}
208298d377a0STARUISI Hiroaki 
208398d377a0STARUISI Hiroaki 	key.objectid = dirid;
208498d377a0STARUISI Hiroaki 	key.type = BTRFS_INODE_REF_KEY;
20858ad6fcabSChris Mason 	key.offset = (u64)-1;
208698d377a0STARUISI Hiroaki 
208798d377a0STARUISI Hiroaki 	while (1) {
208898d377a0STARUISI Hiroaki 		ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
208998d377a0STARUISI Hiroaki 		if (ret < 0)
209098d377a0STARUISI Hiroaki 			goto out;
209118674c6cSFilipe David Borba Manana 		else if (ret > 0) {
209218674c6cSFilipe David Borba Manana 			ret = btrfs_previous_item(root, path, dirid,
209318674c6cSFilipe David Borba Manana 						  BTRFS_INODE_REF_KEY);
209418674c6cSFilipe David Borba Manana 			if (ret < 0)
209518674c6cSFilipe David Borba Manana 				goto out;
209618674c6cSFilipe David Borba Manana 			else if (ret > 0) {
2097ac8e9819SChris Mason 				ret = -ENOENT;
209898d377a0STARUISI Hiroaki 				goto out;
2099ac8e9819SChris Mason 			}
210018674c6cSFilipe David Borba Manana 		}
210118674c6cSFilipe David Borba Manana 
210218674c6cSFilipe David Borba Manana 		l = path->nodes[0];
210318674c6cSFilipe David Borba Manana 		slot = path->slots[0];
210418674c6cSFilipe David Borba Manana 		btrfs_item_key_to_cpu(l, &key, slot);
210598d377a0STARUISI Hiroaki 
210698d377a0STARUISI Hiroaki 		iref = btrfs_item_ptr(l, slot, struct btrfs_inode_ref);
210798d377a0STARUISI Hiroaki 		len = btrfs_inode_ref_name_len(l, iref);
210898d377a0STARUISI Hiroaki 		ptr -= len + 1;
210998d377a0STARUISI Hiroaki 		total_len += len + 1;
2110a696cf35SFilipe David Borba Manana 		if (ptr < name) {
2111a696cf35SFilipe David Borba Manana 			ret = -ENAMETOOLONG;
211298d377a0STARUISI Hiroaki 			goto out;
2113a696cf35SFilipe David Borba Manana 		}
211498d377a0STARUISI Hiroaki 
211598d377a0STARUISI Hiroaki 		*(ptr + len) = '/';
211698d377a0STARUISI Hiroaki 		read_extent_buffer(l, ptr, (unsigned long)(iref + 1), len);
211798d377a0STARUISI Hiroaki 
211898d377a0STARUISI Hiroaki 		if (key.offset == BTRFS_FIRST_FREE_OBJECTID)
211998d377a0STARUISI Hiroaki 			break;
212098d377a0STARUISI Hiroaki 
2121b3b4aa74SDavid Sterba 		btrfs_release_path(path);
212298d377a0STARUISI Hiroaki 		key.objectid = key.offset;
21238ad6fcabSChris Mason 		key.offset = (u64)-1;
212498d377a0STARUISI Hiroaki 		dirid = key.objectid;
212598d377a0STARUISI Hiroaki 	}
212677906a50SLi Zefan 	memmove(name, ptr, total_len);
212798d377a0STARUISI Hiroaki 	name[total_len] = '\0';
212898d377a0STARUISI Hiroaki 	ret = 0;
212998d377a0STARUISI Hiroaki out:
213098d377a0STARUISI Hiroaki 	btrfs_free_path(path);
2131ac8e9819SChris Mason 	return ret;
2132ac8e9819SChris Mason }
2133ac8e9819SChris Mason 
2134ac8e9819SChris Mason static noinline int btrfs_ioctl_ino_lookup(struct file *file,
2135ac8e9819SChris Mason 					   void __user *argp)
2136ac8e9819SChris Mason {
2137ac8e9819SChris Mason 	 struct btrfs_ioctl_ino_lookup_args *args;
2138ac8e9819SChris Mason 	 struct inode *inode;
2139ac8e9819SChris Mason 	 int ret;
2140ac8e9819SChris Mason 
2141ac8e9819SChris Mason 	if (!capable(CAP_SYS_ADMIN))
2142ac8e9819SChris Mason 		return -EPERM;
2143ac8e9819SChris Mason 
21442354d08fSJulia Lawall 	args = memdup_user(argp, sizeof(*args));
21452354d08fSJulia Lawall 	if (IS_ERR(args))
21462354d08fSJulia Lawall 		return PTR_ERR(args);
2147c2b96929SDan Carpenter 
2148496ad9aaSAl Viro 	inode = file_inode(file);
2149ac8e9819SChris Mason 
21501b53ac4dSChris Mason 	if (args->treeid == 0)
21511b53ac4dSChris Mason 		args->treeid = BTRFS_I(inode)->root->root_key.objectid;
21521b53ac4dSChris Mason 
2153ac8e9819SChris Mason 	ret = btrfs_search_path_in_tree(BTRFS_I(inode)->root->fs_info,
2154ac8e9819SChris Mason 					args->treeid, args->objectid,
2155ac8e9819SChris Mason 					args->name);
2156ac8e9819SChris Mason 
2157ac8e9819SChris Mason 	if (ret == 0 && copy_to_user(argp, args, sizeof(*args)))
2158ac8e9819SChris Mason 		ret = -EFAULT;
2159ac8e9819SChris Mason 
2160ac8e9819SChris Mason 	kfree(args);
216198d377a0STARUISI Hiroaki 	return ret;
216298d377a0STARUISI Hiroaki }
216398d377a0STARUISI Hiroaki 
216476dda93cSYan, Zheng static noinline int btrfs_ioctl_snap_destroy(struct file *file,
216576dda93cSYan, Zheng 					     void __user *arg)
216676dda93cSYan, Zheng {
216754563d41SAl Viro 	struct dentry *parent = file->f_path.dentry;
216876dda93cSYan, Zheng 	struct dentry *dentry;
216976dda93cSYan, Zheng 	struct inode *dir = parent->d_inode;
217076dda93cSYan, Zheng 	struct inode *inode;
217176dda93cSYan, Zheng 	struct btrfs_root *root = BTRFS_I(dir)->root;
217276dda93cSYan, Zheng 	struct btrfs_root *dest = NULL;
217376dda93cSYan, Zheng 	struct btrfs_ioctl_vol_args *vol_args;
217476dda93cSYan, Zheng 	struct btrfs_trans_handle *trans;
2175c58aaad2SMiao Xie 	struct btrfs_block_rsv block_rsv;
2176c58aaad2SMiao Xie 	u64 qgroup_reserved;
217776dda93cSYan, Zheng 	int namelen;
217876dda93cSYan, Zheng 	int ret;
217976dda93cSYan, Zheng 	int err = 0;
218076dda93cSYan, Zheng 
218176dda93cSYan, Zheng 	vol_args = memdup_user(arg, sizeof(*vol_args));
218276dda93cSYan, Zheng 	if (IS_ERR(vol_args))
218376dda93cSYan, Zheng 		return PTR_ERR(vol_args);
218476dda93cSYan, Zheng 
218576dda93cSYan, Zheng 	vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
218676dda93cSYan, Zheng 	namelen = strlen(vol_args->name);
218776dda93cSYan, Zheng 	if (strchr(vol_args->name, '/') ||
218876dda93cSYan, Zheng 	    strncmp(vol_args->name, "..", namelen) == 0) {
218976dda93cSYan, Zheng 		err = -EINVAL;
219076dda93cSYan, Zheng 		goto out;
219176dda93cSYan, Zheng 	}
219276dda93cSYan, Zheng 
2193a561be71SAl Viro 	err = mnt_want_write_file(file);
219476dda93cSYan, Zheng 	if (err)
219576dda93cSYan, Zheng 		goto out;
219676dda93cSYan, Zheng 
21975c50c9b8SDavid Sterba 	err = mutex_lock_killable_nested(&dir->i_mutex, I_MUTEX_PARENT);
21985c50c9b8SDavid Sterba 	if (err == -EINTR)
2199e43f998eSDavid Sterba 		goto out_drop_write;
220076dda93cSYan, Zheng 	dentry = lookup_one_len(vol_args->name, parent, namelen);
220176dda93cSYan, Zheng 	if (IS_ERR(dentry)) {
220276dda93cSYan, Zheng 		err = PTR_ERR(dentry);
220376dda93cSYan, Zheng 		goto out_unlock_dir;
220476dda93cSYan, Zheng 	}
220576dda93cSYan, Zheng 
220676dda93cSYan, Zheng 	if (!dentry->d_inode) {
220776dda93cSYan, Zheng 		err = -ENOENT;
220876dda93cSYan, Zheng 		goto out_dput;
220976dda93cSYan, Zheng 	}
221076dda93cSYan, Zheng 
221176dda93cSYan, Zheng 	inode = dentry->d_inode;
22124260f7c7SSage Weil 	dest = BTRFS_I(inode)->root;
22134260f7c7SSage Weil 	if (!capable(CAP_SYS_ADMIN)) {
22144260f7c7SSage Weil 		/*
22154260f7c7SSage Weil 		 * Regular user.  Only allow this with a special mount
22164260f7c7SSage Weil 		 * option, when the user has write+exec access to the
22174260f7c7SSage Weil 		 * subvol root, and when rmdir(2) would have been
22184260f7c7SSage Weil 		 * allowed.
22194260f7c7SSage Weil 		 *
22204260f7c7SSage Weil 		 * Note that this is _not_ check that the subvol is
22214260f7c7SSage Weil 		 * empty or doesn't contain data that we wouldn't
22224260f7c7SSage Weil 		 * otherwise be able to delete.
22234260f7c7SSage Weil 		 *
22244260f7c7SSage Weil 		 * Users who want to delete empty subvols should try
22254260f7c7SSage Weil 		 * rmdir(2).
22264260f7c7SSage Weil 		 */
22274260f7c7SSage Weil 		err = -EPERM;
22284260f7c7SSage Weil 		if (!btrfs_test_opt(root, USER_SUBVOL_RM_ALLOWED))
22294260f7c7SSage Weil 			goto out_dput;
22304260f7c7SSage Weil 
22314260f7c7SSage Weil 		/*
22324260f7c7SSage Weil 		 * Do not allow deletion if the parent dir is the same
22334260f7c7SSage Weil 		 * as the dir to be deleted.  That means the ioctl
22344260f7c7SSage Weil 		 * must be called on the dentry referencing the root
22354260f7c7SSage Weil 		 * of the subvol, not a random directory contained
22364260f7c7SSage Weil 		 * within it.
22374260f7c7SSage Weil 		 */
22384260f7c7SSage Weil 		err = -EINVAL;
22394260f7c7SSage Weil 		if (root == dest)
22404260f7c7SSage Weil 			goto out_dput;
22414260f7c7SSage Weil 
22424260f7c7SSage Weil 		err = inode_permission(inode, MAY_WRITE | MAY_EXEC);
22434260f7c7SSage Weil 		if (err)
22444260f7c7SSage Weil 			goto out_dput;
22455c39da5bSMiao Xie 	}
22464260f7c7SSage Weil 
22475c39da5bSMiao Xie 	/* check if subvolume may be deleted by a user */
22484260f7c7SSage Weil 	err = btrfs_may_delete(dir, dentry, 1);
22494260f7c7SSage Weil 	if (err)
22504260f7c7SSage Weil 		goto out_dput;
22514260f7c7SSage Weil 
225233345d01SLi Zefan 	if (btrfs_ino(inode) != BTRFS_FIRST_FREE_OBJECTID) {
225376dda93cSYan, Zheng 		err = -EINVAL;
225476dda93cSYan, Zheng 		goto out_dput;
225576dda93cSYan, Zheng 	}
225676dda93cSYan, Zheng 
225776dda93cSYan, Zheng 	mutex_lock(&inode->i_mutex);
225876dda93cSYan, Zheng 	err = d_invalidate(dentry);
225976dda93cSYan, Zheng 	if (err)
226076dda93cSYan, Zheng 		goto out_unlock;
226176dda93cSYan, Zheng 
226276dda93cSYan, Zheng 	down_write(&root->fs_info->subvol_sem);
226376dda93cSYan, Zheng 
226476dda93cSYan, Zheng 	err = may_destroy_subvol(dest);
226576dda93cSYan, Zheng 	if (err)
226676dda93cSYan, Zheng 		goto out_up_write;
226776dda93cSYan, Zheng 
2268c58aaad2SMiao Xie 	btrfs_init_block_rsv(&block_rsv, BTRFS_BLOCK_RSV_TEMP);
2269c58aaad2SMiao Xie 	/*
2270c58aaad2SMiao Xie 	 * One for dir inode, two for dir entries, two for root
2271c58aaad2SMiao Xie 	 * ref/backref.
2272c58aaad2SMiao Xie 	 */
2273c58aaad2SMiao Xie 	err = btrfs_subvolume_reserve_metadata(root, &block_rsv,
2274ee3441b4SJeff Mahoney 					       5, &qgroup_reserved, true);
2275c58aaad2SMiao Xie 	if (err)
2276c58aaad2SMiao Xie 		goto out_up_write;
2277c58aaad2SMiao Xie 
2278a22285a6SYan, Zheng 	trans = btrfs_start_transaction(root, 0);
2279a22285a6SYan, Zheng 	if (IS_ERR(trans)) {
2280a22285a6SYan, Zheng 		err = PTR_ERR(trans);
2281c58aaad2SMiao Xie 		goto out_release;
2282a22285a6SYan, Zheng 	}
2283c58aaad2SMiao Xie 	trans->block_rsv = &block_rsv;
2284c58aaad2SMiao Xie 	trans->bytes_reserved = block_rsv.size;
2285a22285a6SYan, Zheng 
228676dda93cSYan, Zheng 	ret = btrfs_unlink_subvol(trans, root, dir,
228776dda93cSYan, Zheng 				dest->root_key.objectid,
228876dda93cSYan, Zheng 				dentry->d_name.name,
228976dda93cSYan, Zheng 				dentry->d_name.len);
229079787eaaSJeff Mahoney 	if (ret) {
229179787eaaSJeff Mahoney 		err = ret;
229279787eaaSJeff Mahoney 		btrfs_abort_transaction(trans, root, ret);
229379787eaaSJeff Mahoney 		goto out_end_trans;
229479787eaaSJeff Mahoney 	}
229576dda93cSYan, Zheng 
229676dda93cSYan, Zheng 	btrfs_record_root_in_trans(trans, dest);
229776dda93cSYan, Zheng 
229876dda93cSYan, Zheng 	memset(&dest->root_item.drop_progress, 0,
229976dda93cSYan, Zheng 		sizeof(dest->root_item.drop_progress));
230076dda93cSYan, Zheng 	dest->root_item.drop_level = 0;
230176dda93cSYan, Zheng 	btrfs_set_root_refs(&dest->root_item, 0);
230276dda93cSYan, Zheng 
2303d68fc57bSYan, Zheng 	if (!xchg(&dest->orphan_item_inserted, 1)) {
230476dda93cSYan, Zheng 		ret = btrfs_insert_orphan_item(trans,
230576dda93cSYan, Zheng 					root->fs_info->tree_root,
230676dda93cSYan, Zheng 					dest->root_key.objectid);
230779787eaaSJeff Mahoney 		if (ret) {
230879787eaaSJeff Mahoney 			btrfs_abort_transaction(trans, root, ret);
230979787eaaSJeff Mahoney 			err = ret;
231079787eaaSJeff Mahoney 			goto out_end_trans;
2311d68fc57bSYan, Zheng 		}
231279787eaaSJeff Mahoney 	}
2313dd5f9615SStefan Behrens 
2314dd5f9615SStefan Behrens 	ret = btrfs_uuid_tree_rem(trans, root->fs_info->uuid_root,
2315dd5f9615SStefan Behrens 				  dest->root_item.uuid, BTRFS_UUID_KEY_SUBVOL,
2316dd5f9615SStefan Behrens 				  dest->root_key.objectid);
2317dd5f9615SStefan Behrens 	if (ret && ret != -ENOENT) {
2318dd5f9615SStefan Behrens 		btrfs_abort_transaction(trans, root, ret);
2319dd5f9615SStefan Behrens 		err = ret;
2320dd5f9615SStefan Behrens 		goto out_end_trans;
2321dd5f9615SStefan Behrens 	}
2322dd5f9615SStefan Behrens 	if (!btrfs_is_empty_uuid(dest->root_item.received_uuid)) {
2323dd5f9615SStefan Behrens 		ret = btrfs_uuid_tree_rem(trans, root->fs_info->uuid_root,
2324dd5f9615SStefan Behrens 					  dest->root_item.received_uuid,
2325dd5f9615SStefan Behrens 					  BTRFS_UUID_KEY_RECEIVED_SUBVOL,
2326dd5f9615SStefan Behrens 					  dest->root_key.objectid);
2327dd5f9615SStefan Behrens 		if (ret && ret != -ENOENT) {
2328dd5f9615SStefan Behrens 			btrfs_abort_transaction(trans, root, ret);
2329dd5f9615SStefan Behrens 			err = ret;
2330dd5f9615SStefan Behrens 			goto out_end_trans;
2331dd5f9615SStefan Behrens 		}
2332dd5f9615SStefan Behrens 	}
2333dd5f9615SStefan Behrens 
233479787eaaSJeff Mahoney out_end_trans:
2335c58aaad2SMiao Xie 	trans->block_rsv = NULL;
2336c58aaad2SMiao Xie 	trans->bytes_reserved = 0;
2337531cb13fSSage Weil 	ret = btrfs_end_transaction(trans, root);
233879787eaaSJeff Mahoney 	if (ret && !err)
233979787eaaSJeff Mahoney 		err = ret;
234076dda93cSYan, Zheng 	inode->i_flags |= S_DEAD;
2341c58aaad2SMiao Xie out_release:
2342c58aaad2SMiao Xie 	btrfs_subvolume_release_metadata(root, &block_rsv, qgroup_reserved);
234376dda93cSYan, Zheng out_up_write:
234476dda93cSYan, Zheng 	up_write(&root->fs_info->subvol_sem);
234576dda93cSYan, Zheng out_unlock:
234676dda93cSYan, Zheng 	mutex_unlock(&inode->i_mutex);
234776dda93cSYan, Zheng 	if (!err) {
2348efefb143SYan, Zheng 		shrink_dcache_sb(root->fs_info->sb);
234976dda93cSYan, Zheng 		btrfs_invalidate_inodes(dest);
235076dda93cSYan, Zheng 		d_delete(dentry);
2351fa6ac876SLiu Bo 
2352fa6ac876SLiu Bo 		/* the last ref */
2353fa6ac876SLiu Bo 		if (dest->cache_inode) {
2354fa6ac876SLiu Bo 			iput(dest->cache_inode);
2355fa6ac876SLiu Bo 			dest->cache_inode = NULL;
2356fa6ac876SLiu Bo 		}
235776dda93cSYan, Zheng 	}
235876dda93cSYan, Zheng out_dput:
235976dda93cSYan, Zheng 	dput(dentry);
236076dda93cSYan, Zheng out_unlock_dir:
236176dda93cSYan, Zheng 	mutex_unlock(&dir->i_mutex);
2362e43f998eSDavid Sterba out_drop_write:
23632a79f17eSAl Viro 	mnt_drop_write_file(file);
236476dda93cSYan, Zheng out:
236576dda93cSYan, Zheng 	kfree(vol_args);
236676dda93cSYan, Zheng 	return err;
236776dda93cSYan, Zheng }
236876dda93cSYan, Zheng 
23691e701a32SChris Mason static int btrfs_ioctl_defrag(struct file *file, void __user *argp)
2370f46b5a66SChristoph Hellwig {
2371496ad9aaSAl Viro 	struct inode *inode = file_inode(file);
2372f46b5a66SChristoph Hellwig 	struct btrfs_root *root = BTRFS_I(inode)->root;
23731e701a32SChris Mason 	struct btrfs_ioctl_defrag_range_args *range;
2374c146afadSYan Zheng 	int ret;
2375c146afadSYan Zheng 
237625122d15SIlya Dryomov 	ret = mnt_want_write_file(file);
237725122d15SIlya Dryomov 	if (ret)
237825122d15SIlya Dryomov 		return ret;
2379b83cc969SLi Zefan 
238025122d15SIlya Dryomov 	if (btrfs_root_readonly(root)) {
238125122d15SIlya Dryomov 		ret = -EROFS;
238225122d15SIlya Dryomov 		goto out;
23835ac00addSStefan Behrens 	}
2384f46b5a66SChristoph Hellwig 
2385f46b5a66SChristoph Hellwig 	switch (inode->i_mode & S_IFMT) {
2386f46b5a66SChristoph Hellwig 	case S_IFDIR:
2387e441d54dSChris Mason 		if (!capable(CAP_SYS_ADMIN)) {
2388e441d54dSChris Mason 			ret = -EPERM;
2389e441d54dSChris Mason 			goto out;
2390e441d54dSChris Mason 		}
2391de78b51aSEric Sandeen 		ret = btrfs_defrag_root(root);
23928929ecfaSYan, Zheng 		if (ret)
23938929ecfaSYan, Zheng 			goto out;
2394de78b51aSEric Sandeen 		ret = btrfs_defrag_root(root->fs_info->extent_root);
2395f46b5a66SChristoph Hellwig 		break;
2396f46b5a66SChristoph Hellwig 	case S_IFREG:
2397e441d54dSChris Mason 		if (!(file->f_mode & FMODE_WRITE)) {
2398e441d54dSChris Mason 			ret = -EINVAL;
2399e441d54dSChris Mason 			goto out;
2400e441d54dSChris Mason 		}
24011e701a32SChris Mason 
24021e701a32SChris Mason 		range = kzalloc(sizeof(*range), GFP_KERNEL);
24031e701a32SChris Mason 		if (!range) {
24041e701a32SChris Mason 			ret = -ENOMEM;
24051e701a32SChris Mason 			goto out;
24061e701a32SChris Mason 		}
24071e701a32SChris Mason 
24081e701a32SChris Mason 		if (argp) {
24091e701a32SChris Mason 			if (copy_from_user(range, argp,
24101e701a32SChris Mason 					   sizeof(*range))) {
24111e701a32SChris Mason 				ret = -EFAULT;
24121e701a32SChris Mason 				kfree(range);
2413683be16eSDan Carpenter 				goto out;
24141e701a32SChris Mason 			}
24151e701a32SChris Mason 			/* compression requires us to start the IO */
24161e701a32SChris Mason 			if ((range->flags & BTRFS_DEFRAG_RANGE_COMPRESS)) {
24171e701a32SChris Mason 				range->flags |= BTRFS_DEFRAG_RANGE_START_IO;
24181e701a32SChris Mason 				range->extent_thresh = (u32)-1;
24191e701a32SChris Mason 			}
24201e701a32SChris Mason 		} else {
24211e701a32SChris Mason 			/* the rest are all set to zero by kzalloc */
24221e701a32SChris Mason 			range->len = (u64)-1;
24231e701a32SChris Mason 		}
2424496ad9aaSAl Viro 		ret = btrfs_defrag_file(file_inode(file), file,
24254cb5300bSChris Mason 					range, 0, 0);
24264cb5300bSChris Mason 		if (ret > 0)
24274cb5300bSChris Mason 			ret = 0;
24281e701a32SChris Mason 		kfree(range);
2429f46b5a66SChristoph Hellwig 		break;
24308929ecfaSYan, Zheng 	default:
24318929ecfaSYan, Zheng 		ret = -EINVAL;
2432f46b5a66SChristoph Hellwig 	}
2433e441d54dSChris Mason out:
243425122d15SIlya Dryomov 	mnt_drop_write_file(file);
2435e441d54dSChris Mason 	return ret;
2436f46b5a66SChristoph Hellwig }
2437f46b5a66SChristoph Hellwig 
2438b2950863SChristoph Hellwig static long btrfs_ioctl_add_dev(struct btrfs_root *root, void __user *arg)
2439f46b5a66SChristoph Hellwig {
2440f46b5a66SChristoph Hellwig 	struct btrfs_ioctl_vol_args *vol_args;
2441f46b5a66SChristoph Hellwig 	int ret;
2442f46b5a66SChristoph Hellwig 
2443e441d54dSChris Mason 	if (!capable(CAP_SYS_ADMIN))
2444e441d54dSChris Mason 		return -EPERM;
2445e441d54dSChris Mason 
24465ac00addSStefan Behrens 	if (atomic_xchg(&root->fs_info->mutually_exclusive_operation_running,
24475ac00addSStefan Behrens 			1)) {
2448e57138b3SAnand Jain 		return BTRFS_ERROR_DEV_EXCL_RUN_IN_PROGRESS;
2449c9e9f97bSIlya Dryomov 	}
2450c9e9f97bSIlya Dryomov 
24515ac00addSStefan Behrens 	mutex_lock(&root->fs_info->volume_mutex);
2452dae7b665SLi Zefan 	vol_args = memdup_user(arg, sizeof(*vol_args));
2453c9e9f97bSIlya Dryomov 	if (IS_ERR(vol_args)) {
2454c9e9f97bSIlya Dryomov 		ret = PTR_ERR(vol_args);
2455c9e9f97bSIlya Dryomov 		goto out;
2456c9e9f97bSIlya Dryomov 	}
2457f46b5a66SChristoph Hellwig 
24585516e595SMark Fasheh 	vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
2459f46b5a66SChristoph Hellwig 	ret = btrfs_init_new_device(root, vol_args->name);
2460f46b5a66SChristoph Hellwig 
2461f46b5a66SChristoph Hellwig 	kfree(vol_args);
2462c9e9f97bSIlya Dryomov out:
2463c9e9f97bSIlya Dryomov 	mutex_unlock(&root->fs_info->volume_mutex);
24645ac00addSStefan Behrens 	atomic_set(&root->fs_info->mutually_exclusive_operation_running, 0);
2465f46b5a66SChristoph Hellwig 	return ret;
2466f46b5a66SChristoph Hellwig }
2467f46b5a66SChristoph Hellwig 
2468da24927bSMiao Xie static long btrfs_ioctl_rm_dev(struct file *file, void __user *arg)
2469f46b5a66SChristoph Hellwig {
2470496ad9aaSAl Viro 	struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
2471f46b5a66SChristoph Hellwig 	struct btrfs_ioctl_vol_args *vol_args;
2472f46b5a66SChristoph Hellwig 	int ret;
2473f46b5a66SChristoph Hellwig 
2474e441d54dSChris Mason 	if (!capable(CAP_SYS_ADMIN))
2475e441d54dSChris Mason 		return -EPERM;
2476e441d54dSChris Mason 
2477da24927bSMiao Xie 	ret = mnt_want_write_file(file);
2478da24927bSMiao Xie 	if (ret)
2479da24927bSMiao Xie 		return ret;
2480c146afadSYan Zheng 
2481dae7b665SLi Zefan 	vol_args = memdup_user(arg, sizeof(*vol_args));
2482c9e9f97bSIlya Dryomov 	if (IS_ERR(vol_args)) {
2483c9e9f97bSIlya Dryomov 		ret = PTR_ERR(vol_args);
2484c9e9f97bSIlya Dryomov 		goto out;
2485c9e9f97bSIlya Dryomov 	}
2486f46b5a66SChristoph Hellwig 
24875516e595SMark Fasheh 	vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
2488f46b5a66SChristoph Hellwig 
2489183860f6SAnand Jain 	if (atomic_xchg(&root->fs_info->mutually_exclusive_operation_running,
2490183860f6SAnand Jain 			1)) {
2491183860f6SAnand Jain 		ret = BTRFS_ERROR_DEV_EXCL_RUN_IN_PROGRESS;
2492183860f6SAnand Jain 		goto out;
2493183860f6SAnand Jain 	}
2494183860f6SAnand Jain 
2495183860f6SAnand Jain 	mutex_lock(&root->fs_info->volume_mutex);
2496183860f6SAnand Jain 	ret = btrfs_rm_device(root, vol_args->name);
2497c9e9f97bSIlya Dryomov 	mutex_unlock(&root->fs_info->volume_mutex);
24985ac00addSStefan Behrens 	atomic_set(&root->fs_info->mutually_exclusive_operation_running, 0);
2499183860f6SAnand Jain 
2500183860f6SAnand Jain out:
2501183860f6SAnand Jain 	kfree(vol_args);
25024ac20c70SIlya Dryomov 	mnt_drop_write_file(file);
2503f46b5a66SChristoph Hellwig 	return ret;
2504f46b5a66SChristoph Hellwig }
2505f46b5a66SChristoph Hellwig 
2506475f6387SJan Schmidt static long btrfs_ioctl_fs_info(struct btrfs_root *root, void __user *arg)
2507475f6387SJan Schmidt {
2508027ed2f0SLi Zefan 	struct btrfs_ioctl_fs_info_args *fi_args;
2509475f6387SJan Schmidt 	struct btrfs_device *device;
2510475f6387SJan Schmidt 	struct btrfs_device *next;
2511475f6387SJan Schmidt 	struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices;
2512027ed2f0SLi Zefan 	int ret = 0;
2513475f6387SJan Schmidt 
2514475f6387SJan Schmidt 	if (!capable(CAP_SYS_ADMIN))
2515475f6387SJan Schmidt 		return -EPERM;
2516475f6387SJan Schmidt 
2517027ed2f0SLi Zefan 	fi_args = kzalloc(sizeof(*fi_args), GFP_KERNEL);
2518027ed2f0SLi Zefan 	if (!fi_args)
2519027ed2f0SLi Zefan 		return -ENOMEM;
2520027ed2f0SLi Zefan 
2521f7171750SFilipe David Borba Manana 	mutex_lock(&fs_devices->device_list_mutex);
2522027ed2f0SLi Zefan 	fi_args->num_devices = fs_devices->num_devices;
2523027ed2f0SLi Zefan 	memcpy(&fi_args->fsid, root->fs_info->fsid, sizeof(fi_args->fsid));
2524475f6387SJan Schmidt 
2525475f6387SJan Schmidt 	list_for_each_entry_safe(device, next, &fs_devices->devices, dev_list) {
2526027ed2f0SLi Zefan 		if (device->devid > fi_args->max_id)
2527027ed2f0SLi Zefan 			fi_args->max_id = device->devid;
2528475f6387SJan Schmidt 	}
2529475f6387SJan Schmidt 	mutex_unlock(&fs_devices->device_list_mutex);
2530475f6387SJan Schmidt 
2531027ed2f0SLi Zefan 	if (copy_to_user(arg, fi_args, sizeof(*fi_args)))
2532027ed2f0SLi Zefan 		ret = -EFAULT;
2533475f6387SJan Schmidt 
2534027ed2f0SLi Zefan 	kfree(fi_args);
2535027ed2f0SLi Zefan 	return ret;
2536475f6387SJan Schmidt }
2537475f6387SJan Schmidt 
2538475f6387SJan Schmidt static long btrfs_ioctl_dev_info(struct btrfs_root *root, void __user *arg)
2539475f6387SJan Schmidt {
2540475f6387SJan Schmidt 	struct btrfs_ioctl_dev_info_args *di_args;
2541475f6387SJan Schmidt 	struct btrfs_device *dev;
2542475f6387SJan Schmidt 	struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices;
2543475f6387SJan Schmidt 	int ret = 0;
2544475f6387SJan Schmidt 	char *s_uuid = NULL;
2545475f6387SJan Schmidt 
2546475f6387SJan Schmidt 	if (!capable(CAP_SYS_ADMIN))
2547475f6387SJan Schmidt 		return -EPERM;
2548475f6387SJan Schmidt 
2549475f6387SJan Schmidt 	di_args = memdup_user(arg, sizeof(*di_args));
2550475f6387SJan Schmidt 	if (IS_ERR(di_args))
2551475f6387SJan Schmidt 		return PTR_ERR(di_args);
2552475f6387SJan Schmidt 
2553dd5f9615SStefan Behrens 	if (!btrfs_is_empty_uuid(di_args->uuid))
2554475f6387SJan Schmidt 		s_uuid = di_args->uuid;
2555475f6387SJan Schmidt 
2556475f6387SJan Schmidt 	mutex_lock(&fs_devices->device_list_mutex);
2557aa1b8cd4SStefan Behrens 	dev = btrfs_find_device(root->fs_info, di_args->devid, s_uuid, NULL);
2558475f6387SJan Schmidt 
2559475f6387SJan Schmidt 	if (!dev) {
2560475f6387SJan Schmidt 		ret = -ENODEV;
2561475f6387SJan Schmidt 		goto out;
2562475f6387SJan Schmidt 	}
2563475f6387SJan Schmidt 
2564475f6387SJan Schmidt 	di_args->devid = dev->devid;
2565475f6387SJan Schmidt 	di_args->bytes_used = dev->bytes_used;
2566475f6387SJan Schmidt 	di_args->total_bytes = dev->total_bytes;
2567475f6387SJan Schmidt 	memcpy(di_args->uuid, dev->uuid, sizeof(di_args->uuid));
2568a27202fbSJim Meyering 	if (dev->name) {
2569606686eeSJosef Bacik 		struct rcu_string *name;
2570606686eeSJosef Bacik 
2571606686eeSJosef Bacik 		rcu_read_lock();
2572606686eeSJosef Bacik 		name = rcu_dereference(dev->name);
2573606686eeSJosef Bacik 		strncpy(di_args->path, name->str, sizeof(di_args->path));
2574606686eeSJosef Bacik 		rcu_read_unlock();
2575a27202fbSJim Meyering 		di_args->path[sizeof(di_args->path) - 1] = 0;
2576a27202fbSJim Meyering 	} else {
257799ba55adSStefan Behrens 		di_args->path[0] = '\0';
2578a27202fbSJim Meyering 	}
2579475f6387SJan Schmidt 
2580475f6387SJan Schmidt out:
258155793c0dSDavid Sterba 	mutex_unlock(&fs_devices->device_list_mutex);
2582475f6387SJan Schmidt 	if (ret == 0 && copy_to_user(arg, di_args, sizeof(*di_args)))
2583475f6387SJan Schmidt 		ret = -EFAULT;
2584475f6387SJan Schmidt 
2585475f6387SJan Schmidt 	kfree(di_args);
2586475f6387SJan Schmidt 	return ret;
2587475f6387SJan Schmidt }
2588475f6387SJan Schmidt 
2589416161dbSMark Fasheh static struct page *extent_same_get_page(struct inode *inode, u64 off)
2590416161dbSMark Fasheh {
2591416161dbSMark Fasheh 	struct page *page;
2592416161dbSMark Fasheh 	pgoff_t index;
2593416161dbSMark Fasheh 	struct extent_io_tree *tree = &BTRFS_I(inode)->io_tree;
2594416161dbSMark Fasheh 
2595416161dbSMark Fasheh 	index = off >> PAGE_CACHE_SHIFT;
2596416161dbSMark Fasheh 
2597416161dbSMark Fasheh 	page = grab_cache_page(inode->i_mapping, index);
2598416161dbSMark Fasheh 	if (!page)
2599416161dbSMark Fasheh 		return NULL;
2600416161dbSMark Fasheh 
2601416161dbSMark Fasheh 	if (!PageUptodate(page)) {
2602416161dbSMark Fasheh 		if (extent_read_full_page_nolock(tree, page, btrfs_get_extent,
2603416161dbSMark Fasheh 						 0))
2604416161dbSMark Fasheh 			return NULL;
2605416161dbSMark Fasheh 		lock_page(page);
2606416161dbSMark Fasheh 		if (!PageUptodate(page)) {
2607416161dbSMark Fasheh 			unlock_page(page);
2608416161dbSMark Fasheh 			page_cache_release(page);
2609416161dbSMark Fasheh 			return NULL;
2610416161dbSMark Fasheh 		}
2611416161dbSMark Fasheh 	}
2612416161dbSMark Fasheh 	unlock_page(page);
2613416161dbSMark Fasheh 
2614416161dbSMark Fasheh 	return page;
2615416161dbSMark Fasheh }
2616416161dbSMark Fasheh 
261777fe20dcSMark Fasheh static inline void lock_extent_range(struct inode *inode, u64 off, u64 len)
261877fe20dcSMark Fasheh {
261977fe20dcSMark Fasheh 	/* do any pending delalloc/csum calc on src, one way or
262077fe20dcSMark Fasheh 	   another, and lock file content */
262177fe20dcSMark Fasheh 	while (1) {
262277fe20dcSMark Fasheh 		struct btrfs_ordered_extent *ordered;
262377fe20dcSMark Fasheh 		lock_extent(&BTRFS_I(inode)->io_tree, off, off + len - 1);
262477fe20dcSMark Fasheh 		ordered = btrfs_lookup_first_ordered_extent(inode,
262577fe20dcSMark Fasheh 							    off + len - 1);
262677fe20dcSMark Fasheh 		if (!ordered &&
262777fe20dcSMark Fasheh 		    !test_range_bit(&BTRFS_I(inode)->io_tree, off,
262877fe20dcSMark Fasheh 				    off + len - 1, EXTENT_DELALLOC, 0, NULL))
262977fe20dcSMark Fasheh 			break;
263077fe20dcSMark Fasheh 		unlock_extent(&BTRFS_I(inode)->io_tree, off, off + len - 1);
263177fe20dcSMark Fasheh 		if (ordered)
263277fe20dcSMark Fasheh 			btrfs_put_ordered_extent(ordered);
263377fe20dcSMark Fasheh 		btrfs_wait_ordered_range(inode, off, len);
263477fe20dcSMark Fasheh 	}
263577fe20dcSMark Fasheh }
263677fe20dcSMark Fasheh 
2637416161dbSMark Fasheh static void btrfs_double_unlock(struct inode *inode1, u64 loff1,
2638416161dbSMark Fasheh 				struct inode *inode2, u64 loff2, u64 len)
2639416161dbSMark Fasheh {
2640416161dbSMark Fasheh 	unlock_extent(&BTRFS_I(inode1)->io_tree, loff1, loff1 + len - 1);
2641416161dbSMark Fasheh 	unlock_extent(&BTRFS_I(inode2)->io_tree, loff2, loff2 + len - 1);
2642416161dbSMark Fasheh 
2643416161dbSMark Fasheh 	mutex_unlock(&inode1->i_mutex);
2644416161dbSMark Fasheh 	mutex_unlock(&inode2->i_mutex);
2645416161dbSMark Fasheh }
2646416161dbSMark Fasheh 
2647416161dbSMark Fasheh static void btrfs_double_lock(struct inode *inode1, u64 loff1,
2648416161dbSMark Fasheh 			      struct inode *inode2, u64 loff2, u64 len)
2649416161dbSMark Fasheh {
2650416161dbSMark Fasheh 	if (inode1 < inode2) {
2651416161dbSMark Fasheh 		swap(inode1, inode2);
2652416161dbSMark Fasheh 		swap(loff1, loff2);
2653416161dbSMark Fasheh 	}
2654416161dbSMark Fasheh 
2655416161dbSMark Fasheh 	mutex_lock_nested(&inode1->i_mutex, I_MUTEX_PARENT);
2656416161dbSMark Fasheh 	lock_extent_range(inode1, loff1, len);
2657416161dbSMark Fasheh 	if (inode1 != inode2) {
2658416161dbSMark Fasheh 		mutex_lock_nested(&inode2->i_mutex, I_MUTEX_CHILD);
2659416161dbSMark Fasheh 		lock_extent_range(inode2, loff2, len);
2660416161dbSMark Fasheh 	}
2661416161dbSMark Fasheh }
2662416161dbSMark Fasheh 
2663416161dbSMark Fasheh static int btrfs_cmp_data(struct inode *src, u64 loff, struct inode *dst,
2664416161dbSMark Fasheh 			  u64 dst_loff, u64 len)
2665416161dbSMark Fasheh {
2666416161dbSMark Fasheh 	int ret = 0;
2667416161dbSMark Fasheh 	struct page *src_page, *dst_page;
2668416161dbSMark Fasheh 	unsigned int cmp_len = PAGE_CACHE_SIZE;
2669416161dbSMark Fasheh 	void *addr, *dst_addr;
2670416161dbSMark Fasheh 
2671416161dbSMark Fasheh 	while (len) {
2672416161dbSMark Fasheh 		if (len < PAGE_CACHE_SIZE)
2673416161dbSMark Fasheh 			cmp_len = len;
2674416161dbSMark Fasheh 
2675416161dbSMark Fasheh 		src_page = extent_same_get_page(src, loff);
2676416161dbSMark Fasheh 		if (!src_page)
2677416161dbSMark Fasheh 			return -EINVAL;
2678416161dbSMark Fasheh 		dst_page = extent_same_get_page(dst, dst_loff);
2679416161dbSMark Fasheh 		if (!dst_page) {
2680416161dbSMark Fasheh 			page_cache_release(src_page);
2681416161dbSMark Fasheh 			return -EINVAL;
2682416161dbSMark Fasheh 		}
2683416161dbSMark Fasheh 		addr = kmap_atomic(src_page);
2684416161dbSMark Fasheh 		dst_addr = kmap_atomic(dst_page);
2685416161dbSMark Fasheh 
2686416161dbSMark Fasheh 		flush_dcache_page(src_page);
2687416161dbSMark Fasheh 		flush_dcache_page(dst_page);
2688416161dbSMark Fasheh 
2689416161dbSMark Fasheh 		if (memcmp(addr, dst_addr, cmp_len))
2690416161dbSMark Fasheh 			ret = BTRFS_SAME_DATA_DIFFERS;
2691416161dbSMark Fasheh 
2692416161dbSMark Fasheh 		kunmap_atomic(addr);
2693416161dbSMark Fasheh 		kunmap_atomic(dst_addr);
2694416161dbSMark Fasheh 		page_cache_release(src_page);
2695416161dbSMark Fasheh 		page_cache_release(dst_page);
2696416161dbSMark Fasheh 
2697416161dbSMark Fasheh 		if (ret)
2698416161dbSMark Fasheh 			break;
2699416161dbSMark Fasheh 
2700416161dbSMark Fasheh 		loff += cmp_len;
2701416161dbSMark Fasheh 		dst_loff += cmp_len;
2702416161dbSMark Fasheh 		len -= cmp_len;
2703416161dbSMark Fasheh 	}
2704416161dbSMark Fasheh 
2705416161dbSMark Fasheh 	return ret;
2706416161dbSMark Fasheh }
2707416161dbSMark Fasheh 
2708416161dbSMark Fasheh static int extent_same_check_offsets(struct inode *inode, u64 off, u64 len)
2709416161dbSMark Fasheh {
2710416161dbSMark Fasheh 	u64 bs = BTRFS_I(inode)->root->fs_info->sb->s_blocksize;
2711416161dbSMark Fasheh 
2712416161dbSMark Fasheh 	if (off + len > inode->i_size || off + len < off)
2713416161dbSMark Fasheh 		return -EINVAL;
2714416161dbSMark Fasheh 	/* Check that we are block aligned - btrfs_clone() requires this */
2715416161dbSMark Fasheh 	if (!IS_ALIGNED(off, bs) || !IS_ALIGNED(off + len, bs))
2716416161dbSMark Fasheh 		return -EINVAL;
2717416161dbSMark Fasheh 
2718416161dbSMark Fasheh 	return 0;
2719416161dbSMark Fasheh }
2720416161dbSMark Fasheh 
2721416161dbSMark Fasheh static int btrfs_extent_same(struct inode *src, u64 loff, u64 len,
2722416161dbSMark Fasheh 			     struct inode *dst, u64 dst_loff)
2723416161dbSMark Fasheh {
2724416161dbSMark Fasheh 	int ret;
2725416161dbSMark Fasheh 
2726416161dbSMark Fasheh 	/*
2727416161dbSMark Fasheh 	 * btrfs_clone() can't handle extents in the same file
2728416161dbSMark Fasheh 	 * yet. Once that works, we can drop this check and replace it
2729416161dbSMark Fasheh 	 * with a check for the same inode, but overlapping extents.
2730416161dbSMark Fasheh 	 */
2731416161dbSMark Fasheh 	if (src == dst)
2732416161dbSMark Fasheh 		return -EINVAL;
2733416161dbSMark Fasheh 
2734416161dbSMark Fasheh 	btrfs_double_lock(src, loff, dst, dst_loff, len);
2735416161dbSMark Fasheh 
2736416161dbSMark Fasheh 	ret = extent_same_check_offsets(src, loff, len);
2737416161dbSMark Fasheh 	if (ret)
2738416161dbSMark Fasheh 		goto out_unlock;
2739416161dbSMark Fasheh 
2740416161dbSMark Fasheh 	ret = extent_same_check_offsets(dst, dst_loff, len);
2741416161dbSMark Fasheh 	if (ret)
2742416161dbSMark Fasheh 		goto out_unlock;
2743416161dbSMark Fasheh 
2744416161dbSMark Fasheh 	/* don't make the dst file partly checksummed */
2745416161dbSMark Fasheh 	if ((BTRFS_I(src)->flags & BTRFS_INODE_NODATASUM) !=
2746416161dbSMark Fasheh 	    (BTRFS_I(dst)->flags & BTRFS_INODE_NODATASUM)) {
2747416161dbSMark Fasheh 		ret = -EINVAL;
2748416161dbSMark Fasheh 		goto out_unlock;
2749416161dbSMark Fasheh 	}
2750416161dbSMark Fasheh 
2751416161dbSMark Fasheh 	ret = btrfs_cmp_data(src, loff, dst, dst_loff, len);
2752416161dbSMark Fasheh 	if (ret == 0)
2753416161dbSMark Fasheh 		ret = btrfs_clone(src, dst, loff, len, len, dst_loff);
2754416161dbSMark Fasheh 
2755416161dbSMark Fasheh out_unlock:
2756416161dbSMark Fasheh 	btrfs_double_unlock(src, loff, dst, dst_loff, len);
2757416161dbSMark Fasheh 
2758416161dbSMark Fasheh 	return ret;
2759416161dbSMark Fasheh }
2760416161dbSMark Fasheh 
2761416161dbSMark Fasheh #define BTRFS_MAX_DEDUPE_LEN	(16 * 1024 * 1024)
2762416161dbSMark Fasheh 
2763416161dbSMark Fasheh static long btrfs_ioctl_file_extent_same(struct file *file,
2764416161dbSMark Fasheh 					 void __user *argp)
2765416161dbSMark Fasheh {
2766cbf8b8caSMark Fasheh 	struct btrfs_ioctl_same_args tmp;
2767cbf8b8caSMark Fasheh 	struct btrfs_ioctl_same_args *same;
2768cbf8b8caSMark Fasheh 	struct btrfs_ioctl_same_extent_info *info;
2769416161dbSMark Fasheh 	struct inode *src = file->f_dentry->d_inode;
2770416161dbSMark Fasheh 	struct file *dst_file = NULL;
2771416161dbSMark Fasheh 	struct inode *dst;
2772416161dbSMark Fasheh 	u64 off;
2773416161dbSMark Fasheh 	u64 len;
2774416161dbSMark Fasheh 	int i;
2775416161dbSMark Fasheh 	int ret;
2776cbf8b8caSMark Fasheh 	unsigned long size;
2777416161dbSMark Fasheh 	u64 bs = BTRFS_I(src)->root->fs_info->sb->s_blocksize;
2778416161dbSMark Fasheh 	bool is_admin = capable(CAP_SYS_ADMIN);
2779416161dbSMark Fasheh 
2780416161dbSMark Fasheh 	if (!(file->f_mode & FMODE_READ))
2781416161dbSMark Fasheh 		return -EINVAL;
2782416161dbSMark Fasheh 
2783416161dbSMark Fasheh 	ret = mnt_want_write_file(file);
2784416161dbSMark Fasheh 	if (ret)
2785416161dbSMark Fasheh 		return ret;
2786416161dbSMark Fasheh 
2787cbf8b8caSMark Fasheh 	if (copy_from_user(&tmp,
2788416161dbSMark Fasheh 			   (struct btrfs_ioctl_same_args __user *)argp,
2789cbf8b8caSMark Fasheh 			   sizeof(tmp))) {
2790416161dbSMark Fasheh 		ret = -EFAULT;
2791416161dbSMark Fasheh 		goto out;
2792416161dbSMark Fasheh 	}
2793416161dbSMark Fasheh 
2794cbf8b8caSMark Fasheh 	size = sizeof(tmp) +
2795cbf8b8caSMark Fasheh 		tmp.dest_count * sizeof(struct btrfs_ioctl_same_extent_info);
2796cbf8b8caSMark Fasheh 
2797229eed43SGeyslan G. Bem 	same = memdup_user((struct btrfs_ioctl_same_args __user *)argp, size);
2798cbf8b8caSMark Fasheh 
2799229eed43SGeyslan G. Bem 	if (IS_ERR(same)) {
2800229eed43SGeyslan G. Bem 		ret = PTR_ERR(same);
2801cbf8b8caSMark Fasheh 		goto out;
2802cbf8b8caSMark Fasheh 	}
2803cbf8b8caSMark Fasheh 
2804cbf8b8caSMark Fasheh 	off = same->logical_offset;
2805cbf8b8caSMark Fasheh 	len = same->length;
2806416161dbSMark Fasheh 
2807416161dbSMark Fasheh 	/*
2808416161dbSMark Fasheh 	 * Limit the total length we will dedupe for each operation.
2809416161dbSMark Fasheh 	 * This is intended to bound the total time spent in this
2810416161dbSMark Fasheh 	 * ioctl to something sane.
2811416161dbSMark Fasheh 	 */
2812416161dbSMark Fasheh 	if (len > BTRFS_MAX_DEDUPE_LEN)
2813416161dbSMark Fasheh 		len = BTRFS_MAX_DEDUPE_LEN;
2814416161dbSMark Fasheh 
2815416161dbSMark Fasheh 	if (WARN_ON_ONCE(bs < PAGE_CACHE_SIZE)) {
2816416161dbSMark Fasheh 		/*
2817416161dbSMark Fasheh 		 * Btrfs does not support blocksize < page_size. As a
2818416161dbSMark Fasheh 		 * result, btrfs_cmp_data() won't correctly handle
2819416161dbSMark Fasheh 		 * this situation without an update.
2820416161dbSMark Fasheh 		 */
2821416161dbSMark Fasheh 		ret = -EINVAL;
2822416161dbSMark Fasheh 		goto out;
2823416161dbSMark Fasheh 	}
2824416161dbSMark Fasheh 
2825416161dbSMark Fasheh 	ret = -EISDIR;
2826416161dbSMark Fasheh 	if (S_ISDIR(src->i_mode))
2827416161dbSMark Fasheh 		goto out;
2828416161dbSMark Fasheh 
2829416161dbSMark Fasheh 	ret = -EACCES;
2830416161dbSMark Fasheh 	if (!S_ISREG(src->i_mode))
2831416161dbSMark Fasheh 		goto out;
2832416161dbSMark Fasheh 
2833cbf8b8caSMark Fasheh 	/* pre-format output fields to sane values */
2834cbf8b8caSMark Fasheh 	for (i = 0; i < same->dest_count; i++) {
2835cbf8b8caSMark Fasheh 		same->info[i].bytes_deduped = 0ULL;
2836cbf8b8caSMark Fasheh 		same->info[i].status = 0;
2837416161dbSMark Fasheh 	}
2838416161dbSMark Fasheh 
2839cbf8b8caSMark Fasheh 	ret = 0;
2840cbf8b8caSMark Fasheh 	for (i = 0; i < same->dest_count; i++) {
2841cbf8b8caSMark Fasheh 		info = &same->info[i];
2842416161dbSMark Fasheh 
2843cbf8b8caSMark Fasheh 		dst_file = fget(info->fd);
2844416161dbSMark Fasheh 		if (!dst_file) {
2845cbf8b8caSMark Fasheh 			info->status = -EBADF;
2846416161dbSMark Fasheh 			goto next;
2847416161dbSMark Fasheh 		}
2848416161dbSMark Fasheh 
2849416161dbSMark Fasheh 		if (!(is_admin || (dst_file->f_mode & FMODE_WRITE))) {
2850cbf8b8caSMark Fasheh 			info->status = -EINVAL;
2851416161dbSMark Fasheh 			goto next;
2852416161dbSMark Fasheh 		}
2853416161dbSMark Fasheh 
2854cbf8b8caSMark Fasheh 		info->status = -EXDEV;
2855416161dbSMark Fasheh 		if (file->f_path.mnt != dst_file->f_path.mnt)
2856416161dbSMark Fasheh 			goto next;
2857416161dbSMark Fasheh 
2858416161dbSMark Fasheh 		dst = dst_file->f_dentry->d_inode;
2859416161dbSMark Fasheh 		if (src->i_sb != dst->i_sb)
2860416161dbSMark Fasheh 			goto next;
2861416161dbSMark Fasheh 
2862416161dbSMark Fasheh 		if (S_ISDIR(dst->i_mode)) {
2863cbf8b8caSMark Fasheh 			info->status = -EISDIR;
2864416161dbSMark Fasheh 			goto next;
2865416161dbSMark Fasheh 		}
2866416161dbSMark Fasheh 
2867416161dbSMark Fasheh 		if (!S_ISREG(dst->i_mode)) {
2868cbf8b8caSMark Fasheh 			info->status = -EACCES;
2869416161dbSMark Fasheh 			goto next;
2870416161dbSMark Fasheh 		}
2871416161dbSMark Fasheh 
2872cbf8b8caSMark Fasheh 		info->status = btrfs_extent_same(src, off, len, dst,
2873cbf8b8caSMark Fasheh 						info->logical_offset);
2874cbf8b8caSMark Fasheh 		if (info->status == 0)
2875cbf8b8caSMark Fasheh 			info->bytes_deduped += len;
2876416161dbSMark Fasheh 
2877416161dbSMark Fasheh next:
2878416161dbSMark Fasheh 		if (dst_file)
2879416161dbSMark Fasheh 			fput(dst_file);
2880cbf8b8caSMark Fasheh 	}
2881416161dbSMark Fasheh 
2882cbf8b8caSMark Fasheh 	ret = copy_to_user(argp, same, size);
2883cbf8b8caSMark Fasheh 	if (ret)
2884416161dbSMark Fasheh 		ret = -EFAULT;
2885416161dbSMark Fasheh 
2886416161dbSMark Fasheh out:
2887416161dbSMark Fasheh 	mnt_drop_write_file(file);
2888416161dbSMark Fasheh 	return ret;
2889416161dbSMark Fasheh }
2890416161dbSMark Fasheh 
289132b7c687SMark Fasheh /**
289232b7c687SMark Fasheh  * btrfs_clone() - clone a range from inode file to another
289332b7c687SMark Fasheh  *
289432b7c687SMark Fasheh  * @src: Inode to clone from
289532b7c687SMark Fasheh  * @inode: Inode to clone to
289632b7c687SMark Fasheh  * @off: Offset within source to start clone from
289732b7c687SMark Fasheh  * @olen: Original length, passed by user, of range to clone
289832b7c687SMark Fasheh  * @olen_aligned: Block-aligned value of olen, extent_same uses
289932b7c687SMark Fasheh  *               identical values here
290032b7c687SMark Fasheh  * @destoff: Offset within @inode to start clone
290132b7c687SMark Fasheh  */
290232b7c687SMark Fasheh static int btrfs_clone(struct inode *src, struct inode *inode,
290332b7c687SMark Fasheh 		       u64 off, u64 olen, u64 olen_aligned, u64 destoff)
2904f46b5a66SChristoph Hellwig {
2905f46b5a66SChristoph Hellwig 	struct btrfs_root *root = BTRFS_I(inode)->root;
290632b7c687SMark Fasheh 	struct btrfs_path *path = NULL;
2907f46b5a66SChristoph Hellwig 	struct extent_buffer *leaf;
290832b7c687SMark Fasheh 	struct btrfs_trans_handle *trans;
290932b7c687SMark Fasheh 	char *buf = NULL;
2910ae01a0abSYan Zheng 	struct btrfs_key key;
2911f46b5a66SChristoph Hellwig 	u32 nritems;
2912f46b5a66SChristoph Hellwig 	int slot;
2913ae01a0abSYan Zheng 	int ret;
291432b7c687SMark Fasheh 	u64 len = olen_aligned;
2915ae01a0abSYan Zheng 
2916ae01a0abSYan Zheng 	ret = -ENOMEM;
2917ae01a0abSYan Zheng 	buf = vmalloc(btrfs_level_size(root, 0));
2918ae01a0abSYan Zheng 	if (!buf)
291932b7c687SMark Fasheh 		return ret;
2920ae01a0abSYan Zheng 
2921ae01a0abSYan Zheng 	path = btrfs_alloc_path();
2922ae01a0abSYan Zheng 	if (!path) {
2923ae01a0abSYan Zheng 		vfree(buf);
292432b7c687SMark Fasheh 		return ret;
2925ae01a0abSYan Zheng 	}
292632b7c687SMark Fasheh 
2927ae01a0abSYan Zheng 	path->reada = 2;
2928c5c9cd4dSSage Weil 	/* clone data */
292933345d01SLi Zefan 	key.objectid = btrfs_ino(src);
2930ae01a0abSYan Zheng 	key.type = BTRFS_EXTENT_DATA_KEY;
2931ae01a0abSYan Zheng 	key.offset = 0;
2932f46b5a66SChristoph Hellwig 
2933f46b5a66SChristoph Hellwig 	while (1) {
2934f46b5a66SChristoph Hellwig 		/*
2935f46b5a66SChristoph Hellwig 		 * note the key will change type as we walk through the
2936f46b5a66SChristoph Hellwig 		 * tree.
2937f46b5a66SChristoph Hellwig 		 */
2938e4355f34SFilipe David Borba Manana 		path->leave_spinning = 1;
2939362a20c5SDavid Sterba 		ret = btrfs_search_slot(NULL, BTRFS_I(src)->root, &key, path,
2940362a20c5SDavid Sterba 				0, 0);
2941f46b5a66SChristoph Hellwig 		if (ret < 0)
2942f46b5a66SChristoph Hellwig 			goto out;
2943f46b5a66SChristoph Hellwig 
2944ae01a0abSYan Zheng 		nritems = btrfs_header_nritems(path->nodes[0]);
2945e4355f34SFilipe David Borba Manana process_slot:
2946ae01a0abSYan Zheng 		if (path->slots[0] >= nritems) {
2947362a20c5SDavid Sterba 			ret = btrfs_next_leaf(BTRFS_I(src)->root, path);
2948f46b5a66SChristoph Hellwig 			if (ret < 0)
2949f46b5a66SChristoph Hellwig 				goto out;
2950f46b5a66SChristoph Hellwig 			if (ret > 0)
2951f46b5a66SChristoph Hellwig 				break;
2952ae01a0abSYan Zheng 			nritems = btrfs_header_nritems(path->nodes[0]);
2953f46b5a66SChristoph Hellwig 		}
2954f46b5a66SChristoph Hellwig 		leaf = path->nodes[0];
2955f46b5a66SChristoph Hellwig 		slot = path->slots[0];
2956f46b5a66SChristoph Hellwig 
2957ae01a0abSYan Zheng 		btrfs_item_key_to_cpu(leaf, &key, slot);
2958d20f7043SChris Mason 		if (btrfs_key_type(&key) > BTRFS_EXTENT_DATA_KEY ||
295933345d01SLi Zefan 		    key.objectid != btrfs_ino(src))
2960f46b5a66SChristoph Hellwig 			break;
2961f46b5a66SChristoph Hellwig 
2962c5c9cd4dSSage Weil 		if (btrfs_key_type(&key) == BTRFS_EXTENT_DATA_KEY) {
2963c5c9cd4dSSage Weil 			struct btrfs_file_extent_item *extent;
2964c5c9cd4dSSage Weil 			int type;
296531840ae1SZheng Yan 			u32 size;
296631840ae1SZheng Yan 			struct btrfs_key new_key;
2967c5c9cd4dSSage Weil 			u64 disko = 0, diskl = 0;
2968c5c9cd4dSSage Weil 			u64 datao = 0, datal = 0;
2969c5c9cd4dSSage Weil 			u8 comp;
2970b5384d48SSage Weil 			u64 endoff;
297131840ae1SZheng Yan 
2972c5c9cd4dSSage Weil 			extent = btrfs_item_ptr(leaf, slot,
2973c5c9cd4dSSage Weil 						struct btrfs_file_extent_item);
2974c5c9cd4dSSage Weil 			comp = btrfs_file_extent_compression(leaf, extent);
2975c5c9cd4dSSage Weil 			type = btrfs_file_extent_type(leaf, extent);
2976c8a894d7SChris Mason 			if (type == BTRFS_FILE_EXTENT_REG ||
2977c8a894d7SChris Mason 			    type == BTRFS_FILE_EXTENT_PREALLOC) {
2978d397712bSChris Mason 				disko = btrfs_file_extent_disk_bytenr(leaf,
2979d397712bSChris Mason 								      extent);
2980d397712bSChris Mason 				diskl = btrfs_file_extent_disk_num_bytes(leaf,
2981d397712bSChris Mason 								 extent);
2982c5c9cd4dSSage Weil 				datao = btrfs_file_extent_offset(leaf, extent);
2983d397712bSChris Mason 				datal = btrfs_file_extent_num_bytes(leaf,
2984d397712bSChris Mason 								    extent);
2985c5c9cd4dSSage Weil 			} else if (type == BTRFS_FILE_EXTENT_INLINE) {
2986c5c9cd4dSSage Weil 				/* take upper bound, may be compressed */
2987c5c9cd4dSSage Weil 				datal = btrfs_file_extent_ram_bytes(leaf,
2988c5c9cd4dSSage Weil 								    extent);
2989c5c9cd4dSSage Weil 			}
299031840ae1SZheng Yan 
2991050006a7SSage Weil 			if (key.offset + datal <= off ||
2992e4355f34SFilipe David Borba Manana 			    key.offset >= off + len - 1) {
2993e4355f34SFilipe David Borba Manana 				path->slots[0]++;
2994e4355f34SFilipe David Borba Manana 				goto process_slot;
2995e4355f34SFilipe David Borba Manana 			}
2996e4355f34SFilipe David Borba Manana 
2997e4355f34SFilipe David Borba Manana 			size = btrfs_item_size_nr(leaf, slot);
2998e4355f34SFilipe David Borba Manana 			read_extent_buffer(leaf, buf,
2999e4355f34SFilipe David Borba Manana 					   btrfs_item_ptr_offset(leaf, slot),
3000e4355f34SFilipe David Borba Manana 					   size);
3001e4355f34SFilipe David Borba Manana 
3002e4355f34SFilipe David Borba Manana 			btrfs_release_path(path);
3003e4355f34SFilipe David Borba Manana 			path->leave_spinning = 0;
3004c5c9cd4dSSage Weil 
300531840ae1SZheng Yan 			memcpy(&new_key, &key, sizeof(new_key));
300633345d01SLi Zefan 			new_key.objectid = btrfs_ino(inode);
30074d728ec7SLi Zefan 			if (off <= key.offset)
3008c5c9cd4dSSage Weil 				new_key.offset = key.offset + destoff - off;
30094d728ec7SLi Zefan 			else
30104d728ec7SLi Zefan 				new_key.offset = destoff;
3011c5c9cd4dSSage Weil 
3012b6f3409bSSage Weil 			/*
3013b6f3409bSSage Weil 			 * 1 - adjusting old extent (we may have to split it)
3014b6f3409bSSage Weil 			 * 1 - add new extent
3015b6f3409bSSage Weil 			 * 1 - inode update
3016b6f3409bSSage Weil 			 */
3017b6f3409bSSage Weil 			trans = btrfs_start_transaction(root, 3);
3018a22285a6SYan, Zheng 			if (IS_ERR(trans)) {
3019a22285a6SYan, Zheng 				ret = PTR_ERR(trans);
3020a22285a6SYan, Zheng 				goto out;
3021a22285a6SYan, Zheng 			}
3022a22285a6SYan, Zheng 
3023c8a894d7SChris Mason 			if (type == BTRFS_FILE_EXTENT_REG ||
3024c8a894d7SChris Mason 			    type == BTRFS_FILE_EXTENT_PREALLOC) {
3025d72c0842SLi Zefan 				/*
3026d72c0842SLi Zefan 				 *    a  | --- range to clone ---|  b
3027d72c0842SLi Zefan 				 * | ------------- extent ------------- |
3028d72c0842SLi Zefan 				 */
3029d72c0842SLi Zefan 
3030d72c0842SLi Zefan 				/* substract range b */
3031d72c0842SLi Zefan 				if (key.offset + datal > off + len)
3032d72c0842SLi Zefan 					datal = off + len - key.offset;
3033d72c0842SLi Zefan 
3034d72c0842SLi Zefan 				/* substract range a */
3035a22285a6SYan, Zheng 				if (off > key.offset) {
3036a22285a6SYan, Zheng 					datao += off - key.offset;
3037a22285a6SYan, Zheng 					datal -= off - key.offset;
3038a22285a6SYan, Zheng 				}
3039a22285a6SYan, Zheng 
30405dc562c5SJosef Bacik 				ret = btrfs_drop_extents(trans, root, inode,
3041a22285a6SYan, Zheng 							 new_key.offset,
3042a22285a6SYan, Zheng 							 new_key.offset + datal,
30432671485dSJosef Bacik 							 1);
304479787eaaSJeff Mahoney 				if (ret) {
304579787eaaSJeff Mahoney 					btrfs_abort_transaction(trans, root,
304679787eaaSJeff Mahoney 								ret);
304779787eaaSJeff Mahoney 					btrfs_end_transaction(trans, root);
304879787eaaSJeff Mahoney 					goto out;
304979787eaaSJeff Mahoney 				}
3050a22285a6SYan, Zheng 
305131840ae1SZheng Yan 				ret = btrfs_insert_empty_item(trans, root, path,
305231840ae1SZheng Yan 							      &new_key, size);
305379787eaaSJeff Mahoney 				if (ret) {
305479787eaaSJeff Mahoney 					btrfs_abort_transaction(trans, root,
305579787eaaSJeff Mahoney 								ret);
305679787eaaSJeff Mahoney 					btrfs_end_transaction(trans, root);
305779787eaaSJeff Mahoney 					goto out;
305879787eaaSJeff Mahoney 				}
305931840ae1SZheng Yan 
306031840ae1SZheng Yan 				leaf = path->nodes[0];
306131840ae1SZheng Yan 				slot = path->slots[0];
306231840ae1SZheng Yan 				write_extent_buffer(leaf, buf,
306331840ae1SZheng Yan 					    btrfs_item_ptr_offset(leaf, slot),
306431840ae1SZheng Yan 					    size);
3065ae01a0abSYan Zheng 
3066f46b5a66SChristoph Hellwig 				extent = btrfs_item_ptr(leaf, slot,
3067f46b5a66SChristoph Hellwig 						struct btrfs_file_extent_item);
3068c5c9cd4dSSage Weil 
3069c5c9cd4dSSage Weil 				/* disko == 0 means it's a hole */
3070c5c9cd4dSSage Weil 				if (!disko)
3071c5c9cd4dSSage Weil 					datao = 0;
3072c5c9cd4dSSage Weil 
3073c5c9cd4dSSage Weil 				btrfs_set_file_extent_offset(leaf, extent,
3074c5c9cd4dSSage Weil 							     datao);
3075c5c9cd4dSSage Weil 				btrfs_set_file_extent_num_bytes(leaf, extent,
3076c5c9cd4dSSage Weil 								datal);
3077c5c9cd4dSSage Weil 				if (disko) {
3078c5c9cd4dSSage Weil 					inode_add_bytes(inode, datal);
3079ae01a0abSYan Zheng 					ret = btrfs_inc_extent_ref(trans, root,
30805d4f98a2SYan Zheng 							disko, diskl, 0,
3081f46b5a66SChristoph Hellwig 							root->root_key.objectid,
308233345d01SLi Zefan 							btrfs_ino(inode),
308366d7e7f0SArne Jansen 							new_key.offset - datao,
308466d7e7f0SArne Jansen 							0);
308579787eaaSJeff Mahoney 					if (ret) {
308679787eaaSJeff Mahoney 						btrfs_abort_transaction(trans,
308779787eaaSJeff Mahoney 									root,
308879787eaaSJeff Mahoney 									ret);
308979787eaaSJeff Mahoney 						btrfs_end_transaction(trans,
309079787eaaSJeff Mahoney 								      root);
309179787eaaSJeff Mahoney 						goto out;
309279787eaaSJeff Mahoney 
309379787eaaSJeff Mahoney 					}
3094f46b5a66SChristoph Hellwig 				}
3095c5c9cd4dSSage Weil 			} else if (type == BTRFS_FILE_EXTENT_INLINE) {
3096c5c9cd4dSSage Weil 				u64 skip = 0;
3097c5c9cd4dSSage Weil 				u64 trim = 0;
3098c5c9cd4dSSage Weil 				if (off > key.offset) {
3099c5c9cd4dSSage Weil 					skip = off - key.offset;
3100c5c9cd4dSSage Weil 					new_key.offset += skip;
310131840ae1SZheng Yan 				}
3102d397712bSChris Mason 
3103c5c9cd4dSSage Weil 				if (key.offset + datal > off + len)
3104c5c9cd4dSSage Weil 					trim = key.offset + datal - (off + len);
3105d397712bSChris Mason 
3106c5c9cd4dSSage Weil 				if (comp && (skip || trim)) {
3107c5c9cd4dSSage Weil 					ret = -EINVAL;
3108a22285a6SYan, Zheng 					btrfs_end_transaction(trans, root);
3109c5c9cd4dSSage Weil 					goto out;
311031840ae1SZheng Yan 				}
3111c5c9cd4dSSage Weil 				size -= skip + trim;
3112c5c9cd4dSSage Weil 				datal -= skip + trim;
3113a22285a6SYan, Zheng 
31145dc562c5SJosef Bacik 				ret = btrfs_drop_extents(trans, root, inode,
3115a22285a6SYan, Zheng 							 new_key.offset,
3116a22285a6SYan, Zheng 							 new_key.offset + datal,
31172671485dSJosef Bacik 							 1);
311879787eaaSJeff Mahoney 				if (ret) {
311979787eaaSJeff Mahoney 					btrfs_abort_transaction(trans, root,
312079787eaaSJeff Mahoney 								ret);
312179787eaaSJeff Mahoney 					btrfs_end_transaction(trans, root);
312279787eaaSJeff Mahoney 					goto out;
312379787eaaSJeff Mahoney 				}
3124a22285a6SYan, Zheng 
3125c5c9cd4dSSage Weil 				ret = btrfs_insert_empty_item(trans, root, path,
3126c5c9cd4dSSage Weil 							      &new_key, size);
312779787eaaSJeff Mahoney 				if (ret) {
312879787eaaSJeff Mahoney 					btrfs_abort_transaction(trans, root,
312979787eaaSJeff Mahoney 								ret);
313079787eaaSJeff Mahoney 					btrfs_end_transaction(trans, root);
313179787eaaSJeff Mahoney 					goto out;
313279787eaaSJeff Mahoney 				}
3133c5c9cd4dSSage Weil 
3134c5c9cd4dSSage Weil 				if (skip) {
3135d397712bSChris Mason 					u32 start =
3136d397712bSChris Mason 					  btrfs_file_extent_calc_inline_size(0);
3137c5c9cd4dSSage Weil 					memmove(buf+start, buf+start+skip,
3138c5c9cd4dSSage Weil 						datal);
3139c5c9cd4dSSage Weil 				}
3140c5c9cd4dSSage Weil 
3141c5c9cd4dSSage Weil 				leaf = path->nodes[0];
3142c5c9cd4dSSage Weil 				slot = path->slots[0];
3143c5c9cd4dSSage Weil 				write_extent_buffer(leaf, buf,
3144c5c9cd4dSSage Weil 					    btrfs_item_ptr_offset(leaf, slot),
3145c5c9cd4dSSage Weil 					    size);
3146c5c9cd4dSSage Weil 				inode_add_bytes(inode, datal);
3147c5c9cd4dSSage Weil 			}
3148c5c9cd4dSSage Weil 
3149c5c9cd4dSSage Weil 			btrfs_mark_buffer_dirty(leaf);
3150b3b4aa74SDavid Sterba 			btrfs_release_path(path);
3151c5c9cd4dSSage Weil 
31520c4d2d95SJosef Bacik 			inode_inc_iversion(inode);
3153a22285a6SYan, Zheng 			inode->i_mtime = inode->i_ctime = CURRENT_TIME;
3154b5384d48SSage Weil 
3155b5384d48SSage Weil 			/*
3156b5384d48SSage Weil 			 * we round up to the block size at eof when
3157b5384d48SSage Weil 			 * determining which extents to clone above,
3158b5384d48SSage Weil 			 * but shouldn't round up the file size
3159b5384d48SSage Weil 			 */
3160b5384d48SSage Weil 			endoff = new_key.offset + datal;
31615f3888ffSLi Zefan 			if (endoff > destoff+olen)
31625f3888ffSLi Zefan 				endoff = destoff+olen;
3163b5384d48SSage Weil 			if (endoff > inode->i_size)
3164b5384d48SSage Weil 				btrfs_i_size_write(inode, endoff);
3165b5384d48SSage Weil 
3166a22285a6SYan, Zheng 			ret = btrfs_update_inode(trans, root, inode);
316779787eaaSJeff Mahoney 			if (ret) {
316879787eaaSJeff Mahoney 				btrfs_abort_transaction(trans, root, ret);
3169a22285a6SYan, Zheng 				btrfs_end_transaction(trans, root);
317079787eaaSJeff Mahoney 				goto out;
317179787eaaSJeff Mahoney 			}
317279787eaaSJeff Mahoney 			ret = btrfs_end_transaction(trans, root);
3173a22285a6SYan, Zheng 		}
3174b3b4aa74SDavid Sterba 		btrfs_release_path(path);
3175ae01a0abSYan Zheng 		key.offset++;
3176ae01a0abSYan Zheng 	}
3177f46b5a66SChristoph Hellwig 	ret = 0;
317832b7c687SMark Fasheh 
3179f46b5a66SChristoph Hellwig out:
3180b3b4aa74SDavid Sterba 	btrfs_release_path(path);
318132b7c687SMark Fasheh 	btrfs_free_path(path);
318232b7c687SMark Fasheh 	vfree(buf);
318332b7c687SMark Fasheh 	return ret;
318432b7c687SMark Fasheh }
318532b7c687SMark Fasheh 
318632b7c687SMark Fasheh static noinline long btrfs_ioctl_clone(struct file *file, unsigned long srcfd,
318732b7c687SMark Fasheh 				       u64 off, u64 olen, u64 destoff)
318832b7c687SMark Fasheh {
318954563d41SAl Viro 	struct inode *inode = file_inode(file);
319032b7c687SMark Fasheh 	struct btrfs_root *root = BTRFS_I(inode)->root;
319132b7c687SMark Fasheh 	struct fd src_file;
319232b7c687SMark Fasheh 	struct inode *src;
319332b7c687SMark Fasheh 	int ret;
319432b7c687SMark Fasheh 	u64 len = olen;
319532b7c687SMark Fasheh 	u64 bs = root->fs_info->sb->s_blocksize;
319632b7c687SMark Fasheh 	int same_inode = 0;
319732b7c687SMark Fasheh 
319832b7c687SMark Fasheh 	/*
319932b7c687SMark Fasheh 	 * TODO:
320032b7c687SMark Fasheh 	 * - split compressed inline extents.  annoying: we need to
320132b7c687SMark Fasheh 	 *   decompress into destination's address_space (the file offset
320232b7c687SMark Fasheh 	 *   may change, so source mapping won't do), then recompress (or
320332b7c687SMark Fasheh 	 *   otherwise reinsert) a subrange.
320432b7c687SMark Fasheh 	 * - allow ranges within the same file to be cloned (provided
320532b7c687SMark Fasheh 	 *   they don't overlap)?
320632b7c687SMark Fasheh 	 */
320732b7c687SMark Fasheh 
320832b7c687SMark Fasheh 	/* the destination must be opened for writing */
320932b7c687SMark Fasheh 	if (!(file->f_mode & FMODE_WRITE) || (file->f_flags & O_APPEND))
321032b7c687SMark Fasheh 		return -EINVAL;
321132b7c687SMark Fasheh 
321232b7c687SMark Fasheh 	if (btrfs_root_readonly(root))
321332b7c687SMark Fasheh 		return -EROFS;
321432b7c687SMark Fasheh 
321532b7c687SMark Fasheh 	ret = mnt_want_write_file(file);
321632b7c687SMark Fasheh 	if (ret)
321732b7c687SMark Fasheh 		return ret;
321832b7c687SMark Fasheh 
321932b7c687SMark Fasheh 	src_file = fdget(srcfd);
322032b7c687SMark Fasheh 	if (!src_file.file) {
322132b7c687SMark Fasheh 		ret = -EBADF;
322232b7c687SMark Fasheh 		goto out_drop_write;
322332b7c687SMark Fasheh 	}
322432b7c687SMark Fasheh 
322532b7c687SMark Fasheh 	ret = -EXDEV;
322632b7c687SMark Fasheh 	if (src_file.file->f_path.mnt != file->f_path.mnt)
322732b7c687SMark Fasheh 		goto out_fput;
322832b7c687SMark Fasheh 
322932b7c687SMark Fasheh 	src = file_inode(src_file.file);
323032b7c687SMark Fasheh 
323132b7c687SMark Fasheh 	ret = -EINVAL;
323232b7c687SMark Fasheh 	if (src == inode)
323332b7c687SMark Fasheh 		same_inode = 1;
323432b7c687SMark Fasheh 
323532b7c687SMark Fasheh 	/* the src must be open for reading */
323632b7c687SMark Fasheh 	if (!(src_file.file->f_mode & FMODE_READ))
323732b7c687SMark Fasheh 		goto out_fput;
323832b7c687SMark Fasheh 
323932b7c687SMark Fasheh 	/* don't make the dst file partly checksummed */
324032b7c687SMark Fasheh 	if ((BTRFS_I(src)->flags & BTRFS_INODE_NODATASUM) !=
324132b7c687SMark Fasheh 	    (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM))
324232b7c687SMark Fasheh 		goto out_fput;
324332b7c687SMark Fasheh 
324432b7c687SMark Fasheh 	ret = -EISDIR;
324532b7c687SMark Fasheh 	if (S_ISDIR(src->i_mode) || S_ISDIR(inode->i_mode))
324632b7c687SMark Fasheh 		goto out_fput;
324732b7c687SMark Fasheh 
324832b7c687SMark Fasheh 	ret = -EXDEV;
324932b7c687SMark Fasheh 	if (src->i_sb != inode->i_sb)
325032b7c687SMark Fasheh 		goto out_fput;
325132b7c687SMark Fasheh 
325232b7c687SMark Fasheh 	if (!same_inode) {
325332b7c687SMark Fasheh 		if (inode < src) {
325432b7c687SMark Fasheh 			mutex_lock_nested(&inode->i_mutex, I_MUTEX_PARENT);
325532b7c687SMark Fasheh 			mutex_lock_nested(&src->i_mutex, I_MUTEX_CHILD);
325632b7c687SMark Fasheh 		} else {
325732b7c687SMark Fasheh 			mutex_lock_nested(&src->i_mutex, I_MUTEX_PARENT);
325832b7c687SMark Fasheh 			mutex_lock_nested(&inode->i_mutex, I_MUTEX_CHILD);
325932b7c687SMark Fasheh 		}
326032b7c687SMark Fasheh 	} else {
326132b7c687SMark Fasheh 		mutex_lock(&src->i_mutex);
326232b7c687SMark Fasheh 	}
326332b7c687SMark Fasheh 
326432b7c687SMark Fasheh 	/* determine range to clone */
326532b7c687SMark Fasheh 	ret = -EINVAL;
326632b7c687SMark Fasheh 	if (off + len > src->i_size || off + len < off)
326732b7c687SMark Fasheh 		goto out_unlock;
326832b7c687SMark Fasheh 	if (len == 0)
326932b7c687SMark Fasheh 		olen = len = src->i_size - off;
327032b7c687SMark Fasheh 	/* if we extend to eof, continue to block boundary */
327132b7c687SMark Fasheh 	if (off + len == src->i_size)
327232b7c687SMark Fasheh 		len = ALIGN(src->i_size, bs) - off;
327332b7c687SMark Fasheh 
327432b7c687SMark Fasheh 	/* verify the end result is block aligned */
327532b7c687SMark Fasheh 	if (!IS_ALIGNED(off, bs) || !IS_ALIGNED(off + len, bs) ||
327632b7c687SMark Fasheh 	    !IS_ALIGNED(destoff, bs))
327732b7c687SMark Fasheh 		goto out_unlock;
327832b7c687SMark Fasheh 
327932b7c687SMark Fasheh 	/* verify if ranges are overlapped within the same file */
328032b7c687SMark Fasheh 	if (same_inode) {
328132b7c687SMark Fasheh 		if (destoff + len > off && destoff < off + len)
328232b7c687SMark Fasheh 			goto out_unlock;
328332b7c687SMark Fasheh 	}
328432b7c687SMark Fasheh 
328532b7c687SMark Fasheh 	if (destoff > inode->i_size) {
328632b7c687SMark Fasheh 		ret = btrfs_cont_expand(inode, inode->i_size, destoff);
328732b7c687SMark Fasheh 		if (ret)
328832b7c687SMark Fasheh 			goto out_unlock;
328932b7c687SMark Fasheh 	}
329032b7c687SMark Fasheh 
329132b7c687SMark Fasheh 	/* truncate page cache pages from target inode range */
329232b7c687SMark Fasheh 	truncate_inode_pages_range(&inode->i_data, destoff,
329332b7c687SMark Fasheh 				   PAGE_CACHE_ALIGN(destoff + len) - 1);
329432b7c687SMark Fasheh 
329532b7c687SMark Fasheh 	lock_extent_range(src, off, len);
329632b7c687SMark Fasheh 
329732b7c687SMark Fasheh 	ret = btrfs_clone(src, inode, off, olen, len, destoff);
329832b7c687SMark Fasheh 
3299aa42ffd9SLiu Bo 	unlock_extent(&BTRFS_I(src)->io_tree, off, off + len - 1);
3300f46b5a66SChristoph Hellwig out_unlock:
3301c57c2b3eSFilipe David Borba Manana 	if (!same_inode) {
3302c57c2b3eSFilipe David Borba Manana 		if (inode < src) {
3303f46b5a66SChristoph Hellwig 			mutex_unlock(&src->i_mutex);
3304f46b5a66SChristoph Hellwig 			mutex_unlock(&inode->i_mutex);
3305c57c2b3eSFilipe David Borba Manana 		} else {
3306c57c2b3eSFilipe David Borba Manana 			mutex_unlock(&inode->i_mutex);
3307c57c2b3eSFilipe David Borba Manana 			mutex_unlock(&src->i_mutex);
3308c57c2b3eSFilipe David Borba Manana 		}
3309c57c2b3eSFilipe David Borba Manana 	} else {
3310c57c2b3eSFilipe David Borba Manana 		mutex_unlock(&src->i_mutex);
3311c57c2b3eSFilipe David Borba Manana 	}
3312f46b5a66SChristoph Hellwig out_fput:
33132903ff01SAl Viro 	fdput(src_file);
3314ab67b7c1SYan Zheng out_drop_write:
33152a79f17eSAl Viro 	mnt_drop_write_file(file);
3316f46b5a66SChristoph Hellwig 	return ret;
3317f46b5a66SChristoph Hellwig }
3318f46b5a66SChristoph Hellwig 
33197a865e8aSChristoph Hellwig static long btrfs_ioctl_clone_range(struct file *file, void __user *argp)
3320c5c9cd4dSSage Weil {
3321c5c9cd4dSSage Weil 	struct btrfs_ioctl_clone_range_args args;
3322c5c9cd4dSSage Weil 
33237a865e8aSChristoph Hellwig 	if (copy_from_user(&args, argp, sizeof(args)))
3324c5c9cd4dSSage Weil 		return -EFAULT;
3325c5c9cd4dSSage Weil 	return btrfs_ioctl_clone(file, args.src_fd, args.src_offset,
3326c5c9cd4dSSage Weil 				 args.src_length, args.dest_offset);
3327c5c9cd4dSSage Weil }
3328c5c9cd4dSSage Weil 
3329f46b5a66SChristoph Hellwig /*
3330f46b5a66SChristoph Hellwig  * there are many ways the trans_start and trans_end ioctls can lead
3331f46b5a66SChristoph Hellwig  * to deadlocks.  They should only be used by applications that
3332f46b5a66SChristoph Hellwig  * basically own the machine, and have a very in depth understanding
3333f46b5a66SChristoph Hellwig  * of all the possible deadlocks and enospc problems.
3334f46b5a66SChristoph Hellwig  */
3335b2950863SChristoph Hellwig static long btrfs_ioctl_trans_start(struct file *file)
3336f46b5a66SChristoph Hellwig {
3337496ad9aaSAl Viro 	struct inode *inode = file_inode(file);
3338f46b5a66SChristoph Hellwig 	struct btrfs_root *root = BTRFS_I(inode)->root;
3339f46b5a66SChristoph Hellwig 	struct btrfs_trans_handle *trans;
33401ab86aedSSage Weil 	int ret;
3341f46b5a66SChristoph Hellwig 
33421ab86aedSSage Weil 	ret = -EPERM;
3343df5b5520SChristoph Hellwig 	if (!capable(CAP_SYS_ADMIN))
3344f46b5a66SChristoph Hellwig 		goto out;
33451ab86aedSSage Weil 
33461ab86aedSSage Weil 	ret = -EINPROGRESS;
33471ab86aedSSage Weil 	if (file->private_data)
33481ab86aedSSage Weil 		goto out;
33499ca9ee09SSage Weil 
3350b83cc969SLi Zefan 	ret = -EROFS;
3351b83cc969SLi Zefan 	if (btrfs_root_readonly(root))
3352b83cc969SLi Zefan 		goto out;
3353b83cc969SLi Zefan 
3354a561be71SAl Viro 	ret = mnt_want_write_file(file);
3355c146afadSYan Zheng 	if (ret)
3356c146afadSYan Zheng 		goto out;
3357c146afadSYan Zheng 
3358a4abeea4SJosef Bacik 	atomic_inc(&root->fs_info->open_ioctl_trans);
33599ca9ee09SSage Weil 
3360f46b5a66SChristoph Hellwig 	ret = -ENOMEM;
33617a7eaa40SJosef Bacik 	trans = btrfs_start_ioctl_transaction(root);
3362abd30bb0STsutomu Itoh 	if (IS_ERR(trans))
33631ab86aedSSage Weil 		goto out_drop;
33641ab86aedSSage Weil 
33651ab86aedSSage Weil 	file->private_data = trans;
33661ab86aedSSage Weil 	return 0;
33671ab86aedSSage Weil 
33681ab86aedSSage Weil out_drop:
3369a4abeea4SJosef Bacik 	atomic_dec(&root->fs_info->open_ioctl_trans);
33702a79f17eSAl Viro 	mnt_drop_write_file(file);
3371f46b5a66SChristoph Hellwig out:
3372f46b5a66SChristoph Hellwig 	return ret;
3373f46b5a66SChristoph Hellwig }
3374f46b5a66SChristoph Hellwig 
33756ef5ed0dSJosef Bacik static long btrfs_ioctl_default_subvol(struct file *file, void __user *argp)
33766ef5ed0dSJosef Bacik {
3377496ad9aaSAl Viro 	struct inode *inode = file_inode(file);
33786ef5ed0dSJosef Bacik 	struct btrfs_root *root = BTRFS_I(inode)->root;
33796ef5ed0dSJosef Bacik 	struct btrfs_root *new_root;
33806ef5ed0dSJosef Bacik 	struct btrfs_dir_item *di;
33816ef5ed0dSJosef Bacik 	struct btrfs_trans_handle *trans;
33826ef5ed0dSJosef Bacik 	struct btrfs_path *path;
33836ef5ed0dSJosef Bacik 	struct btrfs_key location;
33846ef5ed0dSJosef Bacik 	struct btrfs_disk_key disk_key;
33856ef5ed0dSJosef Bacik 	u64 objectid = 0;
33866ef5ed0dSJosef Bacik 	u64 dir_id;
33873c04ce01SMiao Xie 	int ret;
33886ef5ed0dSJosef Bacik 
33896ef5ed0dSJosef Bacik 	if (!capable(CAP_SYS_ADMIN))
33906ef5ed0dSJosef Bacik 		return -EPERM;
33916ef5ed0dSJosef Bacik 
33923c04ce01SMiao Xie 	ret = mnt_want_write_file(file);
33933c04ce01SMiao Xie 	if (ret)
33943c04ce01SMiao Xie 		return ret;
33953c04ce01SMiao Xie 
33963c04ce01SMiao Xie 	if (copy_from_user(&objectid, argp, sizeof(objectid))) {
33973c04ce01SMiao Xie 		ret = -EFAULT;
33983c04ce01SMiao Xie 		goto out;
33993c04ce01SMiao Xie 	}
34006ef5ed0dSJosef Bacik 
34016ef5ed0dSJosef Bacik 	if (!objectid)
34021cecf579Schandan 		objectid = BTRFS_FS_TREE_OBJECTID;
34036ef5ed0dSJosef Bacik 
34046ef5ed0dSJosef Bacik 	location.objectid = objectid;
34056ef5ed0dSJosef Bacik 	location.type = BTRFS_ROOT_ITEM_KEY;
34066ef5ed0dSJosef Bacik 	location.offset = (u64)-1;
34076ef5ed0dSJosef Bacik 
34086ef5ed0dSJosef Bacik 	new_root = btrfs_read_fs_root_no_name(root->fs_info, &location);
34093c04ce01SMiao Xie 	if (IS_ERR(new_root)) {
34103c04ce01SMiao Xie 		ret = PTR_ERR(new_root);
34113c04ce01SMiao Xie 		goto out;
34123c04ce01SMiao Xie 	}
34136ef5ed0dSJosef Bacik 
34146ef5ed0dSJosef Bacik 	path = btrfs_alloc_path();
34153c04ce01SMiao Xie 	if (!path) {
34163c04ce01SMiao Xie 		ret = -ENOMEM;
34173c04ce01SMiao Xie 		goto out;
34183c04ce01SMiao Xie 	}
34196ef5ed0dSJosef Bacik 	path->leave_spinning = 1;
34206ef5ed0dSJosef Bacik 
34216ef5ed0dSJosef Bacik 	trans = btrfs_start_transaction(root, 1);
342298d5dc13STsutomu Itoh 	if (IS_ERR(trans)) {
34236ef5ed0dSJosef Bacik 		btrfs_free_path(path);
34243c04ce01SMiao Xie 		ret = PTR_ERR(trans);
34253c04ce01SMiao Xie 		goto out;
34266ef5ed0dSJosef Bacik 	}
34276ef5ed0dSJosef Bacik 
34286c41761fSDavid Sterba 	dir_id = btrfs_super_root_dir(root->fs_info->super_copy);
34296ef5ed0dSJosef Bacik 	di = btrfs_lookup_dir_item(trans, root->fs_info->tree_root, path,
34306ef5ed0dSJosef Bacik 				   dir_id, "default", 7, 1);
3431cf1e99a4SDan Carpenter 	if (IS_ERR_OR_NULL(di)) {
34326ef5ed0dSJosef Bacik 		btrfs_free_path(path);
34336ef5ed0dSJosef Bacik 		btrfs_end_transaction(trans, root);
3434efe120a0SFrank Holton 		btrfs_err(new_root->fs_info, "Umm, you don't have the default dir"
3435efe120a0SFrank Holton 			   "item, this isn't going to work");
34363c04ce01SMiao Xie 		ret = -ENOENT;
34373c04ce01SMiao Xie 		goto out;
34386ef5ed0dSJosef Bacik 	}
34396ef5ed0dSJosef Bacik 
34406ef5ed0dSJosef Bacik 	btrfs_cpu_key_to_disk(&disk_key, &new_root->root_key);
34416ef5ed0dSJosef Bacik 	btrfs_set_dir_item_key(path->nodes[0], di, &disk_key);
34426ef5ed0dSJosef Bacik 	btrfs_mark_buffer_dirty(path->nodes[0]);
34436ef5ed0dSJosef Bacik 	btrfs_free_path(path);
34446ef5ed0dSJosef Bacik 
34452b0ce2c2SMitch Harder 	btrfs_set_fs_incompat(root->fs_info, DEFAULT_SUBVOL);
34466ef5ed0dSJosef Bacik 	btrfs_end_transaction(trans, root);
34473c04ce01SMiao Xie out:
34483c04ce01SMiao Xie 	mnt_drop_write_file(file);
34493c04ce01SMiao Xie 	return ret;
34506ef5ed0dSJosef Bacik }
34516ef5ed0dSJosef Bacik 
34525af3e8ccSStefan Behrens void btrfs_get_block_group_info(struct list_head *groups_list,
3453bf5fc093SJosef Bacik 				struct btrfs_ioctl_space_info *space)
3454bf5fc093SJosef Bacik {
3455bf5fc093SJosef Bacik 	struct btrfs_block_group_cache *block_group;
3456bf5fc093SJosef Bacik 
3457bf5fc093SJosef Bacik 	space->total_bytes = 0;
3458bf5fc093SJosef Bacik 	space->used_bytes = 0;
3459bf5fc093SJosef Bacik 	space->flags = 0;
3460bf5fc093SJosef Bacik 	list_for_each_entry(block_group, groups_list, list) {
3461bf5fc093SJosef Bacik 		space->flags = block_group->flags;
3462bf5fc093SJosef Bacik 		space->total_bytes += block_group->key.offset;
3463bf5fc093SJosef Bacik 		space->used_bytes +=
3464bf5fc093SJosef Bacik 			btrfs_block_group_used(&block_group->item);
3465bf5fc093SJosef Bacik 	}
3466bf5fc093SJosef Bacik }
3467bf5fc093SJosef Bacik 
346848a3b636SEric Sandeen static long btrfs_ioctl_space_info(struct btrfs_root *root, void __user *arg)
34691406e432SJosef Bacik {
34701406e432SJosef Bacik 	struct btrfs_ioctl_space_args space_args;
34711406e432SJosef Bacik 	struct btrfs_ioctl_space_info space;
34721406e432SJosef Bacik 	struct btrfs_ioctl_space_info *dest;
34737fde62bfSChris Mason 	struct btrfs_ioctl_space_info *dest_orig;
347413f2696fSDaniel J Blueman 	struct btrfs_ioctl_space_info __user *user_dest;
34751406e432SJosef Bacik 	struct btrfs_space_info *info;
3476bf5fc093SJosef Bacik 	u64 types[] = {BTRFS_BLOCK_GROUP_DATA,
3477bf5fc093SJosef Bacik 		       BTRFS_BLOCK_GROUP_SYSTEM,
3478bf5fc093SJosef Bacik 		       BTRFS_BLOCK_GROUP_METADATA,
3479bf5fc093SJosef Bacik 		       BTRFS_BLOCK_GROUP_DATA | BTRFS_BLOCK_GROUP_METADATA};
3480bf5fc093SJosef Bacik 	int num_types = 4;
34817fde62bfSChris Mason 	int alloc_size;
34821406e432SJosef Bacik 	int ret = 0;
348351788b1bSDan Rosenberg 	u64 slot_count = 0;
3484bf5fc093SJosef Bacik 	int i, c;
34851406e432SJosef Bacik 
34861406e432SJosef Bacik 	if (copy_from_user(&space_args,
34871406e432SJosef Bacik 			   (struct btrfs_ioctl_space_args __user *)arg,
34881406e432SJosef Bacik 			   sizeof(space_args)))
34891406e432SJosef Bacik 		return -EFAULT;
34901406e432SJosef Bacik 
3491bf5fc093SJosef Bacik 	for (i = 0; i < num_types; i++) {
3492bf5fc093SJosef Bacik 		struct btrfs_space_info *tmp;
3493bf5fc093SJosef Bacik 
3494bf5fc093SJosef Bacik 		info = NULL;
34957fde62bfSChris Mason 		rcu_read_lock();
3496bf5fc093SJosef Bacik 		list_for_each_entry_rcu(tmp, &root->fs_info->space_info,
3497bf5fc093SJosef Bacik 					list) {
3498bf5fc093SJosef Bacik 			if (tmp->flags == types[i]) {
3499bf5fc093SJosef Bacik 				info = tmp;
3500bf5fc093SJosef Bacik 				break;
3501bf5fc093SJosef Bacik 			}
3502bf5fc093SJosef Bacik 		}
35037fde62bfSChris Mason 		rcu_read_unlock();
35041406e432SJosef Bacik 
3505bf5fc093SJosef Bacik 		if (!info)
3506bf5fc093SJosef Bacik 			continue;
3507bf5fc093SJosef Bacik 
3508bf5fc093SJosef Bacik 		down_read(&info->groups_sem);
3509bf5fc093SJosef Bacik 		for (c = 0; c < BTRFS_NR_RAID_TYPES; c++) {
3510bf5fc093SJosef Bacik 			if (!list_empty(&info->block_groups[c]))
3511bf5fc093SJosef Bacik 				slot_count++;
3512bf5fc093SJosef Bacik 		}
3513bf5fc093SJosef Bacik 		up_read(&info->groups_sem);
3514bf5fc093SJosef Bacik 	}
3515bf5fc093SJosef Bacik 
35167fde62bfSChris Mason 	/* space_slots == 0 means they are asking for a count */
35177fde62bfSChris Mason 	if (space_args.space_slots == 0) {
35187fde62bfSChris Mason 		space_args.total_spaces = slot_count;
35197fde62bfSChris Mason 		goto out;
35207fde62bfSChris Mason 	}
3521bf5fc093SJosef Bacik 
352251788b1bSDan Rosenberg 	slot_count = min_t(u64, space_args.space_slots, slot_count);
3523bf5fc093SJosef Bacik 
35247fde62bfSChris Mason 	alloc_size = sizeof(*dest) * slot_count;
3525bf5fc093SJosef Bacik 
35267fde62bfSChris Mason 	/* we generally have at most 6 or so space infos, one for each raid
35277fde62bfSChris Mason 	 * level.  So, a whole page should be more than enough for everyone
35287fde62bfSChris Mason 	 */
35297fde62bfSChris Mason 	if (alloc_size > PAGE_CACHE_SIZE)
35307fde62bfSChris Mason 		return -ENOMEM;
35317fde62bfSChris Mason 
35327fde62bfSChris Mason 	space_args.total_spaces = 0;
35337fde62bfSChris Mason 	dest = kmalloc(alloc_size, GFP_NOFS);
35347fde62bfSChris Mason 	if (!dest)
35357fde62bfSChris Mason 		return -ENOMEM;
35367fde62bfSChris Mason 	dest_orig = dest;
35377fde62bfSChris Mason 
35387fde62bfSChris Mason 	/* now we have a buffer to copy into */
3539bf5fc093SJosef Bacik 	for (i = 0; i < num_types; i++) {
3540bf5fc093SJosef Bacik 		struct btrfs_space_info *tmp;
3541bf5fc093SJosef Bacik 
354251788b1bSDan Rosenberg 		if (!slot_count)
354351788b1bSDan Rosenberg 			break;
354451788b1bSDan Rosenberg 
3545bf5fc093SJosef Bacik 		info = NULL;
35461406e432SJosef Bacik 		rcu_read_lock();
3547bf5fc093SJosef Bacik 		list_for_each_entry_rcu(tmp, &root->fs_info->space_info,
3548bf5fc093SJosef Bacik 					list) {
3549bf5fc093SJosef Bacik 			if (tmp->flags == types[i]) {
3550bf5fc093SJosef Bacik 				info = tmp;
35517fde62bfSChris Mason 				break;
3552bf5fc093SJosef Bacik 			}
3553bf5fc093SJosef Bacik 		}
3554bf5fc093SJosef Bacik 		rcu_read_unlock();
35557fde62bfSChris Mason 
3556bf5fc093SJosef Bacik 		if (!info)
3557bf5fc093SJosef Bacik 			continue;
3558bf5fc093SJosef Bacik 		down_read(&info->groups_sem);
3559bf5fc093SJosef Bacik 		for (c = 0; c < BTRFS_NR_RAID_TYPES; c++) {
3560bf5fc093SJosef Bacik 			if (!list_empty(&info->block_groups[c])) {
35615af3e8ccSStefan Behrens 				btrfs_get_block_group_info(
35625af3e8ccSStefan Behrens 					&info->block_groups[c], &space);
35637fde62bfSChris Mason 				memcpy(dest, &space, sizeof(space));
35641406e432SJosef Bacik 				dest++;
35651406e432SJosef Bacik 				space_args.total_spaces++;
356651788b1bSDan Rosenberg 				slot_count--;
35671406e432SJosef Bacik 			}
356851788b1bSDan Rosenberg 			if (!slot_count)
356951788b1bSDan Rosenberg 				break;
3570bf5fc093SJosef Bacik 		}
3571bf5fc093SJosef Bacik 		up_read(&info->groups_sem);
3572bf5fc093SJosef Bacik 	}
35731406e432SJosef Bacik 
35742eec6c81SDaniel J Blueman 	user_dest = (struct btrfs_ioctl_space_info __user *)
35757fde62bfSChris Mason 		(arg + sizeof(struct btrfs_ioctl_space_args));
35767fde62bfSChris Mason 
35777fde62bfSChris Mason 	if (copy_to_user(user_dest, dest_orig, alloc_size))
35787fde62bfSChris Mason 		ret = -EFAULT;
35797fde62bfSChris Mason 
35807fde62bfSChris Mason 	kfree(dest_orig);
35817fde62bfSChris Mason out:
35827fde62bfSChris Mason 	if (ret == 0 && copy_to_user(arg, &space_args, sizeof(space_args)))
35831406e432SJosef Bacik 		ret = -EFAULT;
35841406e432SJosef Bacik 
35851406e432SJosef Bacik 	return ret;
35861406e432SJosef Bacik }
35871406e432SJosef Bacik 
3588f46b5a66SChristoph Hellwig /*
3589f46b5a66SChristoph Hellwig  * there are many ways the trans_start and trans_end ioctls can lead
3590f46b5a66SChristoph Hellwig  * to deadlocks.  They should only be used by applications that
3591f46b5a66SChristoph Hellwig  * basically own the machine, and have a very in depth understanding
3592f46b5a66SChristoph Hellwig  * of all the possible deadlocks and enospc problems.
3593f46b5a66SChristoph Hellwig  */
3594f46b5a66SChristoph Hellwig long btrfs_ioctl_trans_end(struct file *file)
3595f46b5a66SChristoph Hellwig {
3596496ad9aaSAl Viro 	struct inode *inode = file_inode(file);
3597f46b5a66SChristoph Hellwig 	struct btrfs_root *root = BTRFS_I(inode)->root;
3598f46b5a66SChristoph Hellwig 	struct btrfs_trans_handle *trans;
3599f46b5a66SChristoph Hellwig 
3600f46b5a66SChristoph Hellwig 	trans = file->private_data;
36011ab86aedSSage Weil 	if (!trans)
36021ab86aedSSage Weil 		return -EINVAL;
3603b214107eSChristoph Hellwig 	file->private_data = NULL;
36049ca9ee09SSage Weil 
36051ab86aedSSage Weil 	btrfs_end_transaction(trans, root);
36061ab86aedSSage Weil 
3607a4abeea4SJosef Bacik 	atomic_dec(&root->fs_info->open_ioctl_trans);
36089ca9ee09SSage Weil 
36092a79f17eSAl Viro 	mnt_drop_write_file(file);
36101ab86aedSSage Weil 	return 0;
3611f46b5a66SChristoph Hellwig }
3612f46b5a66SChristoph Hellwig 
36139a8c28beSMiao Xie static noinline long btrfs_ioctl_start_sync(struct btrfs_root *root,
36149a8c28beSMiao Xie 					    void __user *argp)
361546204592SSage Weil {
361646204592SSage Weil 	struct btrfs_trans_handle *trans;
361746204592SSage Weil 	u64 transid;
3618db5b493aSTsutomu Itoh 	int ret;
361946204592SSage Weil 
3620d4edf39bSMiao Xie 	trans = btrfs_attach_transaction_barrier(root);
3621ff7c1d33SMiao Xie 	if (IS_ERR(trans)) {
3622ff7c1d33SMiao Xie 		if (PTR_ERR(trans) != -ENOENT)
362398d5dc13STsutomu Itoh 			return PTR_ERR(trans);
3624ff7c1d33SMiao Xie 
3625ff7c1d33SMiao Xie 		/* No running transaction, don't bother */
3626ff7c1d33SMiao Xie 		transid = root->fs_info->last_trans_committed;
3627ff7c1d33SMiao Xie 		goto out;
3628ff7c1d33SMiao Xie 	}
362946204592SSage Weil 	transid = trans->transid;
3630db5b493aSTsutomu Itoh 	ret = btrfs_commit_transaction_async(trans, root, 0);
36318b2b2d3cSTsutomu Itoh 	if (ret) {
36328b2b2d3cSTsutomu Itoh 		btrfs_end_transaction(trans, root);
3633db5b493aSTsutomu Itoh 		return ret;
36348b2b2d3cSTsutomu Itoh 	}
3635ff7c1d33SMiao Xie out:
363646204592SSage Weil 	if (argp)
363746204592SSage Weil 		if (copy_to_user(argp, &transid, sizeof(transid)))
363846204592SSage Weil 			return -EFAULT;
363946204592SSage Weil 	return 0;
364046204592SSage Weil }
364146204592SSage Weil 
36429a8c28beSMiao Xie static noinline long btrfs_ioctl_wait_sync(struct btrfs_root *root,
36439a8c28beSMiao Xie 					   void __user *argp)
364446204592SSage Weil {
364546204592SSage Weil 	u64 transid;
364646204592SSage Weil 
364746204592SSage Weil 	if (argp) {
364846204592SSage Weil 		if (copy_from_user(&transid, argp, sizeof(transid)))
364946204592SSage Weil 			return -EFAULT;
365046204592SSage Weil 	} else {
365146204592SSage Weil 		transid = 0;  /* current trans */
365246204592SSage Weil 	}
365346204592SSage Weil 	return btrfs_wait_for_commit(root, transid);
365446204592SSage Weil }
365546204592SSage Weil 
3656b8e95489SMiao Xie static long btrfs_ioctl_scrub(struct file *file, void __user *arg)
3657475f6387SJan Schmidt {
3658496ad9aaSAl Viro 	struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
3659475f6387SJan Schmidt 	struct btrfs_ioctl_scrub_args *sa;
3660b8e95489SMiao Xie 	int ret;
3661475f6387SJan Schmidt 
3662475f6387SJan Schmidt 	if (!capable(CAP_SYS_ADMIN))
3663475f6387SJan Schmidt 		return -EPERM;
3664475f6387SJan Schmidt 
3665475f6387SJan Schmidt 	sa = memdup_user(arg, sizeof(*sa));
3666475f6387SJan Schmidt 	if (IS_ERR(sa))
3667475f6387SJan Schmidt 		return PTR_ERR(sa);
3668475f6387SJan Schmidt 
3669b8e95489SMiao Xie 	if (!(sa->flags & BTRFS_SCRUB_READONLY)) {
3670b8e95489SMiao Xie 		ret = mnt_want_write_file(file);
3671b8e95489SMiao Xie 		if (ret)
3672b8e95489SMiao Xie 			goto out;
3673b8e95489SMiao Xie 	}
3674b8e95489SMiao Xie 
3675aa1b8cd4SStefan Behrens 	ret = btrfs_scrub_dev(root->fs_info, sa->devid, sa->start, sa->end,
367663a212abSStefan Behrens 			      &sa->progress, sa->flags & BTRFS_SCRUB_READONLY,
367763a212abSStefan Behrens 			      0);
3678475f6387SJan Schmidt 
3679475f6387SJan Schmidt 	if (copy_to_user(arg, sa, sizeof(*sa)))
3680475f6387SJan Schmidt 		ret = -EFAULT;
3681475f6387SJan Schmidt 
3682b8e95489SMiao Xie 	if (!(sa->flags & BTRFS_SCRUB_READONLY))
3683b8e95489SMiao Xie 		mnt_drop_write_file(file);
3684b8e95489SMiao Xie out:
3685475f6387SJan Schmidt 	kfree(sa);
3686475f6387SJan Schmidt 	return ret;
3687475f6387SJan Schmidt }
3688475f6387SJan Schmidt 
3689475f6387SJan Schmidt static long btrfs_ioctl_scrub_cancel(struct btrfs_root *root, void __user *arg)
3690475f6387SJan Schmidt {
3691475f6387SJan Schmidt 	if (!capable(CAP_SYS_ADMIN))
3692475f6387SJan Schmidt 		return -EPERM;
3693475f6387SJan Schmidt 
3694aa1b8cd4SStefan Behrens 	return btrfs_scrub_cancel(root->fs_info);
3695475f6387SJan Schmidt }
3696475f6387SJan Schmidt 
3697475f6387SJan Schmidt static long btrfs_ioctl_scrub_progress(struct btrfs_root *root,
3698475f6387SJan Schmidt 				       void __user *arg)
3699475f6387SJan Schmidt {
3700475f6387SJan Schmidt 	struct btrfs_ioctl_scrub_args *sa;
3701475f6387SJan Schmidt 	int ret;
3702475f6387SJan Schmidt 
3703475f6387SJan Schmidt 	if (!capable(CAP_SYS_ADMIN))
3704475f6387SJan Schmidt 		return -EPERM;
3705475f6387SJan Schmidt 
3706475f6387SJan Schmidt 	sa = memdup_user(arg, sizeof(*sa));
3707475f6387SJan Schmidt 	if (IS_ERR(sa))
3708475f6387SJan Schmidt 		return PTR_ERR(sa);
3709475f6387SJan Schmidt 
3710475f6387SJan Schmidt 	ret = btrfs_scrub_progress(root, sa->devid, &sa->progress);
3711475f6387SJan Schmidt 
3712475f6387SJan Schmidt 	if (copy_to_user(arg, sa, sizeof(*sa)))
3713475f6387SJan Schmidt 		ret = -EFAULT;
3714475f6387SJan Schmidt 
3715475f6387SJan Schmidt 	kfree(sa);
3716475f6387SJan Schmidt 	return ret;
3717475f6387SJan Schmidt }
3718475f6387SJan Schmidt 
3719c11d2c23SStefan Behrens static long btrfs_ioctl_get_dev_stats(struct btrfs_root *root,
3720b27f7c0cSDavid Sterba 				      void __user *arg)
3721c11d2c23SStefan Behrens {
3722c11d2c23SStefan Behrens 	struct btrfs_ioctl_get_dev_stats *sa;
3723c11d2c23SStefan Behrens 	int ret;
3724c11d2c23SStefan Behrens 
3725c11d2c23SStefan Behrens 	sa = memdup_user(arg, sizeof(*sa));
3726c11d2c23SStefan Behrens 	if (IS_ERR(sa))
3727c11d2c23SStefan Behrens 		return PTR_ERR(sa);
3728c11d2c23SStefan Behrens 
3729b27f7c0cSDavid Sterba 	if ((sa->flags & BTRFS_DEV_STATS_RESET) && !capable(CAP_SYS_ADMIN)) {
3730b27f7c0cSDavid Sterba 		kfree(sa);
3731b27f7c0cSDavid Sterba 		return -EPERM;
3732b27f7c0cSDavid Sterba 	}
3733b27f7c0cSDavid Sterba 
3734b27f7c0cSDavid Sterba 	ret = btrfs_get_dev_stats(root, sa);
3735c11d2c23SStefan Behrens 
3736c11d2c23SStefan Behrens 	if (copy_to_user(arg, sa, sizeof(*sa)))
3737c11d2c23SStefan Behrens 		ret = -EFAULT;
3738c11d2c23SStefan Behrens 
3739c11d2c23SStefan Behrens 	kfree(sa);
3740c11d2c23SStefan Behrens 	return ret;
3741c11d2c23SStefan Behrens }
3742c11d2c23SStefan Behrens 
37433f6bcfbdSStefan Behrens static long btrfs_ioctl_dev_replace(struct btrfs_root *root, void __user *arg)
37443f6bcfbdSStefan Behrens {
37453f6bcfbdSStefan Behrens 	struct btrfs_ioctl_dev_replace_args *p;
37463f6bcfbdSStefan Behrens 	int ret;
37473f6bcfbdSStefan Behrens 
37483f6bcfbdSStefan Behrens 	if (!capable(CAP_SYS_ADMIN))
37493f6bcfbdSStefan Behrens 		return -EPERM;
37503f6bcfbdSStefan Behrens 
37513f6bcfbdSStefan Behrens 	p = memdup_user(arg, sizeof(*p));
37523f6bcfbdSStefan Behrens 	if (IS_ERR(p))
37533f6bcfbdSStefan Behrens 		return PTR_ERR(p);
37543f6bcfbdSStefan Behrens 
37553f6bcfbdSStefan Behrens 	switch (p->cmd) {
37563f6bcfbdSStefan Behrens 	case BTRFS_IOCTL_DEV_REPLACE_CMD_START:
3757adfa97cbSIlya Dryomov 		if (root->fs_info->sb->s_flags & MS_RDONLY) {
3758adfa97cbSIlya Dryomov 			ret = -EROFS;
3759adfa97cbSIlya Dryomov 			goto out;
3760adfa97cbSIlya Dryomov 		}
37613f6bcfbdSStefan Behrens 		if (atomic_xchg(
37623f6bcfbdSStefan Behrens 			&root->fs_info->mutually_exclusive_operation_running,
37633f6bcfbdSStefan Behrens 			1)) {
3764e57138b3SAnand Jain 			ret = BTRFS_ERROR_DEV_EXCL_RUN_IN_PROGRESS;
37653f6bcfbdSStefan Behrens 		} else {
37663f6bcfbdSStefan Behrens 			ret = btrfs_dev_replace_start(root, p);
37673f6bcfbdSStefan Behrens 			atomic_set(
37683f6bcfbdSStefan Behrens 			 &root->fs_info->mutually_exclusive_operation_running,
37693f6bcfbdSStefan Behrens 			 0);
37703f6bcfbdSStefan Behrens 		}
37713f6bcfbdSStefan Behrens 		break;
37723f6bcfbdSStefan Behrens 	case BTRFS_IOCTL_DEV_REPLACE_CMD_STATUS:
37733f6bcfbdSStefan Behrens 		btrfs_dev_replace_status(root->fs_info, p);
37743f6bcfbdSStefan Behrens 		ret = 0;
37753f6bcfbdSStefan Behrens 		break;
37763f6bcfbdSStefan Behrens 	case BTRFS_IOCTL_DEV_REPLACE_CMD_CANCEL:
37773f6bcfbdSStefan Behrens 		ret = btrfs_dev_replace_cancel(root->fs_info, p);
37783f6bcfbdSStefan Behrens 		break;
37793f6bcfbdSStefan Behrens 	default:
37803f6bcfbdSStefan Behrens 		ret = -EINVAL;
37813f6bcfbdSStefan Behrens 		break;
37823f6bcfbdSStefan Behrens 	}
37833f6bcfbdSStefan Behrens 
37843f6bcfbdSStefan Behrens 	if (copy_to_user(arg, p, sizeof(*p)))
37853f6bcfbdSStefan Behrens 		ret = -EFAULT;
3786adfa97cbSIlya Dryomov out:
37873f6bcfbdSStefan Behrens 	kfree(p);
37883f6bcfbdSStefan Behrens 	return ret;
37893f6bcfbdSStefan Behrens }
37903f6bcfbdSStefan Behrens 
3791d7728c96SJan Schmidt static long btrfs_ioctl_ino_to_path(struct btrfs_root *root, void __user *arg)
3792d7728c96SJan Schmidt {
3793d7728c96SJan Schmidt 	int ret = 0;
3794d7728c96SJan Schmidt 	int i;
3795740c3d22SChris Mason 	u64 rel_ptr;
3796d7728c96SJan Schmidt 	int size;
3797806468f8SChris Mason 	struct btrfs_ioctl_ino_path_args *ipa = NULL;
3798d7728c96SJan Schmidt 	struct inode_fs_paths *ipath = NULL;
3799d7728c96SJan Schmidt 	struct btrfs_path *path;
3800d7728c96SJan Schmidt 
380182b22ac8SKusanagi Kouichi 	if (!capable(CAP_DAC_READ_SEARCH))
3802d7728c96SJan Schmidt 		return -EPERM;
3803d7728c96SJan Schmidt 
3804d7728c96SJan Schmidt 	path = btrfs_alloc_path();
3805d7728c96SJan Schmidt 	if (!path) {
3806d7728c96SJan Schmidt 		ret = -ENOMEM;
3807d7728c96SJan Schmidt 		goto out;
3808d7728c96SJan Schmidt 	}
3809d7728c96SJan Schmidt 
3810d7728c96SJan Schmidt 	ipa = memdup_user(arg, sizeof(*ipa));
3811d7728c96SJan Schmidt 	if (IS_ERR(ipa)) {
3812d7728c96SJan Schmidt 		ret = PTR_ERR(ipa);
3813d7728c96SJan Schmidt 		ipa = NULL;
3814d7728c96SJan Schmidt 		goto out;
3815d7728c96SJan Schmidt 	}
3816d7728c96SJan Schmidt 
3817d7728c96SJan Schmidt 	size = min_t(u32, ipa->size, 4096);
3818d7728c96SJan Schmidt 	ipath = init_ipath(size, root, path);
3819d7728c96SJan Schmidt 	if (IS_ERR(ipath)) {
3820d7728c96SJan Schmidt 		ret = PTR_ERR(ipath);
3821d7728c96SJan Schmidt 		ipath = NULL;
3822d7728c96SJan Schmidt 		goto out;
3823d7728c96SJan Schmidt 	}
3824d7728c96SJan Schmidt 
3825d7728c96SJan Schmidt 	ret = paths_from_inode(ipa->inum, ipath);
3826d7728c96SJan Schmidt 	if (ret < 0)
3827d7728c96SJan Schmidt 		goto out;
3828d7728c96SJan Schmidt 
3829d7728c96SJan Schmidt 	for (i = 0; i < ipath->fspath->elem_cnt; ++i) {
3830745c4d8eSJeff Mahoney 		rel_ptr = ipath->fspath->val[i] -
3831745c4d8eSJeff Mahoney 			  (u64)(unsigned long)ipath->fspath->val;
3832740c3d22SChris Mason 		ipath->fspath->val[i] = rel_ptr;
3833d7728c96SJan Schmidt 	}
3834d7728c96SJan Schmidt 
3835745c4d8eSJeff Mahoney 	ret = copy_to_user((void *)(unsigned long)ipa->fspath,
3836745c4d8eSJeff Mahoney 			   (void *)(unsigned long)ipath->fspath, size);
3837d7728c96SJan Schmidt 	if (ret) {
3838d7728c96SJan Schmidt 		ret = -EFAULT;
3839d7728c96SJan Schmidt 		goto out;
3840d7728c96SJan Schmidt 	}
3841d7728c96SJan Schmidt 
3842d7728c96SJan Schmidt out:
3843d7728c96SJan Schmidt 	btrfs_free_path(path);
3844d7728c96SJan Schmidt 	free_ipath(ipath);
3845d7728c96SJan Schmidt 	kfree(ipa);
3846d7728c96SJan Schmidt 
3847d7728c96SJan Schmidt 	return ret;
3848d7728c96SJan Schmidt }
3849d7728c96SJan Schmidt 
3850d7728c96SJan Schmidt static int build_ino_list(u64 inum, u64 offset, u64 root, void *ctx)
3851d7728c96SJan Schmidt {
3852d7728c96SJan Schmidt 	struct btrfs_data_container *inodes = ctx;
3853d7728c96SJan Schmidt 	const size_t c = 3 * sizeof(u64);
3854d7728c96SJan Schmidt 
3855d7728c96SJan Schmidt 	if (inodes->bytes_left >= c) {
3856d7728c96SJan Schmidt 		inodes->bytes_left -= c;
3857d7728c96SJan Schmidt 		inodes->val[inodes->elem_cnt] = inum;
3858d7728c96SJan Schmidt 		inodes->val[inodes->elem_cnt + 1] = offset;
3859d7728c96SJan Schmidt 		inodes->val[inodes->elem_cnt + 2] = root;
3860d7728c96SJan Schmidt 		inodes->elem_cnt += 3;
3861d7728c96SJan Schmidt 	} else {
3862d7728c96SJan Schmidt 		inodes->bytes_missing += c - inodes->bytes_left;
3863d7728c96SJan Schmidt 		inodes->bytes_left = 0;
3864d7728c96SJan Schmidt 		inodes->elem_missed += 3;
3865d7728c96SJan Schmidt 	}
3866d7728c96SJan Schmidt 
3867d7728c96SJan Schmidt 	return 0;
3868d7728c96SJan Schmidt }
3869d7728c96SJan Schmidt 
3870d7728c96SJan Schmidt static long btrfs_ioctl_logical_to_ino(struct btrfs_root *root,
3871d7728c96SJan Schmidt 					void __user *arg)
3872d7728c96SJan Schmidt {
3873d7728c96SJan Schmidt 	int ret = 0;
3874d7728c96SJan Schmidt 	int size;
3875d7728c96SJan Schmidt 	struct btrfs_ioctl_logical_ino_args *loi;
3876d7728c96SJan Schmidt 	struct btrfs_data_container *inodes = NULL;
3877d7728c96SJan Schmidt 	struct btrfs_path *path = NULL;
3878d7728c96SJan Schmidt 
3879d7728c96SJan Schmidt 	if (!capable(CAP_SYS_ADMIN))
3880d7728c96SJan Schmidt 		return -EPERM;
3881d7728c96SJan Schmidt 
3882d7728c96SJan Schmidt 	loi = memdup_user(arg, sizeof(*loi));
3883d7728c96SJan Schmidt 	if (IS_ERR(loi)) {
3884d7728c96SJan Schmidt 		ret = PTR_ERR(loi);
3885d7728c96SJan Schmidt 		loi = NULL;
3886d7728c96SJan Schmidt 		goto out;
3887d7728c96SJan Schmidt 	}
3888d7728c96SJan Schmidt 
3889d7728c96SJan Schmidt 	path = btrfs_alloc_path();
3890d7728c96SJan Schmidt 	if (!path) {
3891d7728c96SJan Schmidt 		ret = -ENOMEM;
3892d7728c96SJan Schmidt 		goto out;
3893d7728c96SJan Schmidt 	}
3894d7728c96SJan Schmidt 
3895425d17a2SLiu Bo 	size = min_t(u32, loi->size, 64 * 1024);
3896d7728c96SJan Schmidt 	inodes = init_data_container(size);
3897d7728c96SJan Schmidt 	if (IS_ERR(inodes)) {
3898d7728c96SJan Schmidt 		ret = PTR_ERR(inodes);
3899d7728c96SJan Schmidt 		inodes = NULL;
3900d7728c96SJan Schmidt 		goto out;
3901d7728c96SJan Schmidt 	}
3902d7728c96SJan Schmidt 
3903df031f07SLiu Bo 	ret = iterate_inodes_from_logical(loi->logical, root->fs_info, path,
3904df031f07SLiu Bo 					  build_ino_list, inodes);
3905df031f07SLiu Bo 	if (ret == -EINVAL)
3906d7728c96SJan Schmidt 		ret = -ENOENT;
3907d7728c96SJan Schmidt 	if (ret < 0)
3908d7728c96SJan Schmidt 		goto out;
3909d7728c96SJan Schmidt 
3910745c4d8eSJeff Mahoney 	ret = copy_to_user((void *)(unsigned long)loi->inodes,
3911745c4d8eSJeff Mahoney 			   (void *)(unsigned long)inodes, size);
3912d7728c96SJan Schmidt 	if (ret)
3913d7728c96SJan Schmidt 		ret = -EFAULT;
3914d7728c96SJan Schmidt 
3915d7728c96SJan Schmidt out:
3916d7728c96SJan Schmidt 	btrfs_free_path(path);
3917425d17a2SLiu Bo 	vfree(inodes);
3918d7728c96SJan Schmidt 	kfree(loi);
3919d7728c96SJan Schmidt 
3920d7728c96SJan Schmidt 	return ret;
3921d7728c96SJan Schmidt }
3922d7728c96SJan Schmidt 
392319a39dceSIlya Dryomov void update_ioctl_balance_args(struct btrfs_fs_info *fs_info, int lock,
3924c9e9f97bSIlya Dryomov 			       struct btrfs_ioctl_balance_args *bargs)
3925c9e9f97bSIlya Dryomov {
3926c9e9f97bSIlya Dryomov 	struct btrfs_balance_control *bctl = fs_info->balance_ctl;
3927c9e9f97bSIlya Dryomov 
3928c9e9f97bSIlya Dryomov 	bargs->flags = bctl->flags;
3929c9e9f97bSIlya Dryomov 
3930837d5b6eSIlya Dryomov 	if (atomic_read(&fs_info->balance_running))
3931837d5b6eSIlya Dryomov 		bargs->state |= BTRFS_BALANCE_STATE_RUNNING;
3932837d5b6eSIlya Dryomov 	if (atomic_read(&fs_info->balance_pause_req))
3933837d5b6eSIlya Dryomov 		bargs->state |= BTRFS_BALANCE_STATE_PAUSE_REQ;
3934a7e99c69SIlya Dryomov 	if (atomic_read(&fs_info->balance_cancel_req))
3935a7e99c69SIlya Dryomov 		bargs->state |= BTRFS_BALANCE_STATE_CANCEL_REQ;
3936837d5b6eSIlya Dryomov 
3937c9e9f97bSIlya Dryomov 	memcpy(&bargs->data, &bctl->data, sizeof(bargs->data));
3938c9e9f97bSIlya Dryomov 	memcpy(&bargs->meta, &bctl->meta, sizeof(bargs->meta));
3939c9e9f97bSIlya Dryomov 	memcpy(&bargs->sys, &bctl->sys, sizeof(bargs->sys));
394019a39dceSIlya Dryomov 
394119a39dceSIlya Dryomov 	if (lock) {
394219a39dceSIlya Dryomov 		spin_lock(&fs_info->balance_lock);
394319a39dceSIlya Dryomov 		memcpy(&bargs->stat, &bctl->stat, sizeof(bargs->stat));
394419a39dceSIlya Dryomov 		spin_unlock(&fs_info->balance_lock);
394519a39dceSIlya Dryomov 	} else {
394619a39dceSIlya Dryomov 		memcpy(&bargs->stat, &bctl->stat, sizeof(bargs->stat));
394719a39dceSIlya Dryomov 	}
3948c9e9f97bSIlya Dryomov }
3949c9e9f97bSIlya Dryomov 
39509ba1f6e4SLiu Bo static long btrfs_ioctl_balance(struct file *file, void __user *arg)
3951c9e9f97bSIlya Dryomov {
3952496ad9aaSAl Viro 	struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
3953c9e9f97bSIlya Dryomov 	struct btrfs_fs_info *fs_info = root->fs_info;
3954c9e9f97bSIlya Dryomov 	struct btrfs_ioctl_balance_args *bargs;
3955c9e9f97bSIlya Dryomov 	struct btrfs_balance_control *bctl;
3956ed0fb78fSIlya Dryomov 	bool need_unlock; /* for mut. excl. ops lock */
3957c9e9f97bSIlya Dryomov 	int ret;
3958c9e9f97bSIlya Dryomov 
3959c9e9f97bSIlya Dryomov 	if (!capable(CAP_SYS_ADMIN))
3960c9e9f97bSIlya Dryomov 		return -EPERM;
3961c9e9f97bSIlya Dryomov 
3962e54bfa31SLiu Bo 	ret = mnt_want_write_file(file);
39639ba1f6e4SLiu Bo 	if (ret)
39649ba1f6e4SLiu Bo 		return ret;
39659ba1f6e4SLiu Bo 
3966ed0fb78fSIlya Dryomov again:
3967ed0fb78fSIlya Dryomov 	if (!atomic_xchg(&fs_info->mutually_exclusive_operation_running, 1)) {
3968c9e9f97bSIlya Dryomov 		mutex_lock(&fs_info->volume_mutex);
3969c9e9f97bSIlya Dryomov 		mutex_lock(&fs_info->balance_mutex);
3970ed0fb78fSIlya Dryomov 		need_unlock = true;
3971ed0fb78fSIlya Dryomov 		goto locked;
3972ed0fb78fSIlya Dryomov 	}
3973ed0fb78fSIlya Dryomov 
3974ed0fb78fSIlya Dryomov 	/*
3975ed0fb78fSIlya Dryomov 	 * mut. excl. ops lock is locked.  Three possibilites:
3976ed0fb78fSIlya Dryomov 	 *   (1) some other op is running
3977ed0fb78fSIlya Dryomov 	 *   (2) balance is running
3978ed0fb78fSIlya Dryomov 	 *   (3) balance is paused -- special case (think resume)
3979ed0fb78fSIlya Dryomov 	 */
3980ed0fb78fSIlya Dryomov 	mutex_lock(&fs_info->balance_mutex);
3981ed0fb78fSIlya Dryomov 	if (fs_info->balance_ctl) {
3982ed0fb78fSIlya Dryomov 		/* this is either (2) or (3) */
3983ed0fb78fSIlya Dryomov 		if (!atomic_read(&fs_info->balance_running)) {
3984ed0fb78fSIlya Dryomov 			mutex_unlock(&fs_info->balance_mutex);
3985ed0fb78fSIlya Dryomov 			if (!mutex_trylock(&fs_info->volume_mutex))
3986ed0fb78fSIlya Dryomov 				goto again;
3987ed0fb78fSIlya Dryomov 			mutex_lock(&fs_info->balance_mutex);
3988ed0fb78fSIlya Dryomov 
3989ed0fb78fSIlya Dryomov 			if (fs_info->balance_ctl &&
3990ed0fb78fSIlya Dryomov 			    !atomic_read(&fs_info->balance_running)) {
3991ed0fb78fSIlya Dryomov 				/* this is (3) */
3992ed0fb78fSIlya Dryomov 				need_unlock = false;
3993ed0fb78fSIlya Dryomov 				goto locked;
3994ed0fb78fSIlya Dryomov 			}
3995ed0fb78fSIlya Dryomov 
3996ed0fb78fSIlya Dryomov 			mutex_unlock(&fs_info->balance_mutex);
3997ed0fb78fSIlya Dryomov 			mutex_unlock(&fs_info->volume_mutex);
3998ed0fb78fSIlya Dryomov 			goto again;
3999ed0fb78fSIlya Dryomov 		} else {
4000ed0fb78fSIlya Dryomov 			/* this is (2) */
4001ed0fb78fSIlya Dryomov 			mutex_unlock(&fs_info->balance_mutex);
4002ed0fb78fSIlya Dryomov 			ret = -EINPROGRESS;
4003ed0fb78fSIlya Dryomov 			goto out;
4004ed0fb78fSIlya Dryomov 		}
4005ed0fb78fSIlya Dryomov 	} else {
4006ed0fb78fSIlya Dryomov 		/* this is (1) */
4007ed0fb78fSIlya Dryomov 		mutex_unlock(&fs_info->balance_mutex);
4008e57138b3SAnand Jain 		ret = BTRFS_ERROR_DEV_EXCL_RUN_IN_PROGRESS;
4009ed0fb78fSIlya Dryomov 		goto out;
4010ed0fb78fSIlya Dryomov 	}
4011ed0fb78fSIlya Dryomov 
4012ed0fb78fSIlya Dryomov locked:
4013ed0fb78fSIlya Dryomov 	BUG_ON(!atomic_read(&fs_info->mutually_exclusive_operation_running));
4014c9e9f97bSIlya Dryomov 
4015c9e9f97bSIlya Dryomov 	if (arg) {
4016c9e9f97bSIlya Dryomov 		bargs = memdup_user(arg, sizeof(*bargs));
4017c9e9f97bSIlya Dryomov 		if (IS_ERR(bargs)) {
4018c9e9f97bSIlya Dryomov 			ret = PTR_ERR(bargs);
4019ed0fb78fSIlya Dryomov 			goto out_unlock;
4020c9e9f97bSIlya Dryomov 		}
4021de322263SIlya Dryomov 
4022de322263SIlya Dryomov 		if (bargs->flags & BTRFS_BALANCE_RESUME) {
4023de322263SIlya Dryomov 			if (!fs_info->balance_ctl) {
4024de322263SIlya Dryomov 				ret = -ENOTCONN;
4025de322263SIlya Dryomov 				goto out_bargs;
4026de322263SIlya Dryomov 			}
4027de322263SIlya Dryomov 
4028de322263SIlya Dryomov 			bctl = fs_info->balance_ctl;
4029de322263SIlya Dryomov 			spin_lock(&fs_info->balance_lock);
4030de322263SIlya Dryomov 			bctl->flags |= BTRFS_BALANCE_RESUME;
4031de322263SIlya Dryomov 			spin_unlock(&fs_info->balance_lock);
4032de322263SIlya Dryomov 
4033de322263SIlya Dryomov 			goto do_balance;
4034de322263SIlya Dryomov 		}
4035c9e9f97bSIlya Dryomov 	} else {
4036c9e9f97bSIlya Dryomov 		bargs = NULL;
4037c9e9f97bSIlya Dryomov 	}
4038c9e9f97bSIlya Dryomov 
4039ed0fb78fSIlya Dryomov 	if (fs_info->balance_ctl) {
4040837d5b6eSIlya Dryomov 		ret = -EINPROGRESS;
4041837d5b6eSIlya Dryomov 		goto out_bargs;
4042837d5b6eSIlya Dryomov 	}
4043837d5b6eSIlya Dryomov 
4044c9e9f97bSIlya Dryomov 	bctl = kzalloc(sizeof(*bctl), GFP_NOFS);
4045c9e9f97bSIlya Dryomov 	if (!bctl) {
4046c9e9f97bSIlya Dryomov 		ret = -ENOMEM;
4047c9e9f97bSIlya Dryomov 		goto out_bargs;
4048c9e9f97bSIlya Dryomov 	}
4049c9e9f97bSIlya Dryomov 
4050c9e9f97bSIlya Dryomov 	bctl->fs_info = fs_info;
4051c9e9f97bSIlya Dryomov 	if (arg) {
4052c9e9f97bSIlya Dryomov 		memcpy(&bctl->data, &bargs->data, sizeof(bctl->data));
4053c9e9f97bSIlya Dryomov 		memcpy(&bctl->meta, &bargs->meta, sizeof(bctl->meta));
4054c9e9f97bSIlya Dryomov 		memcpy(&bctl->sys, &bargs->sys, sizeof(bctl->sys));
4055c9e9f97bSIlya Dryomov 
4056c9e9f97bSIlya Dryomov 		bctl->flags = bargs->flags;
4057f43ffb60SIlya Dryomov 	} else {
4058f43ffb60SIlya Dryomov 		/* balance everything - no filters */
4059f43ffb60SIlya Dryomov 		bctl->flags |= BTRFS_BALANCE_TYPE_MASK;
4060c9e9f97bSIlya Dryomov 	}
4061c9e9f97bSIlya Dryomov 
4062de322263SIlya Dryomov do_balance:
4063c9e9f97bSIlya Dryomov 	/*
4064ed0fb78fSIlya Dryomov 	 * Ownership of bctl and mutually_exclusive_operation_running
4065ed0fb78fSIlya Dryomov 	 * goes to to btrfs_balance.  bctl is freed in __cancel_balance,
4066ed0fb78fSIlya Dryomov 	 * or, if restriper was paused all the way until unmount, in
4067ed0fb78fSIlya Dryomov 	 * free_fs_info.  mutually_exclusive_operation_running is
4068ed0fb78fSIlya Dryomov 	 * cleared in __cancel_balance.
4069c9e9f97bSIlya Dryomov 	 */
4070ed0fb78fSIlya Dryomov 	need_unlock = false;
4071ed0fb78fSIlya Dryomov 
4072ed0fb78fSIlya Dryomov 	ret = btrfs_balance(bctl, bargs);
4073ed0fb78fSIlya Dryomov 
4074c9e9f97bSIlya Dryomov 	if (arg) {
4075c9e9f97bSIlya Dryomov 		if (copy_to_user(arg, bargs, sizeof(*bargs)))
4076c9e9f97bSIlya Dryomov 			ret = -EFAULT;
4077c9e9f97bSIlya Dryomov 	}
4078c9e9f97bSIlya Dryomov 
4079c9e9f97bSIlya Dryomov out_bargs:
4080c9e9f97bSIlya Dryomov 	kfree(bargs);
4081ed0fb78fSIlya Dryomov out_unlock:
4082c9e9f97bSIlya Dryomov 	mutex_unlock(&fs_info->balance_mutex);
4083c9e9f97bSIlya Dryomov 	mutex_unlock(&fs_info->volume_mutex);
4084ed0fb78fSIlya Dryomov 	if (need_unlock)
4085ed0fb78fSIlya Dryomov 		atomic_set(&fs_info->mutually_exclusive_operation_running, 0);
4086ed0fb78fSIlya Dryomov out:
4087e54bfa31SLiu Bo 	mnt_drop_write_file(file);
4088c9e9f97bSIlya Dryomov 	return ret;
4089c9e9f97bSIlya Dryomov }
4090c9e9f97bSIlya Dryomov 
4091837d5b6eSIlya Dryomov static long btrfs_ioctl_balance_ctl(struct btrfs_root *root, int cmd)
4092837d5b6eSIlya Dryomov {
4093837d5b6eSIlya Dryomov 	if (!capable(CAP_SYS_ADMIN))
4094837d5b6eSIlya Dryomov 		return -EPERM;
4095837d5b6eSIlya Dryomov 
4096837d5b6eSIlya Dryomov 	switch (cmd) {
4097837d5b6eSIlya Dryomov 	case BTRFS_BALANCE_CTL_PAUSE:
4098837d5b6eSIlya Dryomov 		return btrfs_pause_balance(root->fs_info);
4099a7e99c69SIlya Dryomov 	case BTRFS_BALANCE_CTL_CANCEL:
4100a7e99c69SIlya Dryomov 		return btrfs_cancel_balance(root->fs_info);
4101837d5b6eSIlya Dryomov 	}
4102837d5b6eSIlya Dryomov 
4103837d5b6eSIlya Dryomov 	return -EINVAL;
4104837d5b6eSIlya Dryomov }
4105837d5b6eSIlya Dryomov 
410619a39dceSIlya Dryomov static long btrfs_ioctl_balance_progress(struct btrfs_root *root,
410719a39dceSIlya Dryomov 					 void __user *arg)
410819a39dceSIlya Dryomov {
410919a39dceSIlya Dryomov 	struct btrfs_fs_info *fs_info = root->fs_info;
411019a39dceSIlya Dryomov 	struct btrfs_ioctl_balance_args *bargs;
411119a39dceSIlya Dryomov 	int ret = 0;
411219a39dceSIlya Dryomov 
411319a39dceSIlya Dryomov 	if (!capable(CAP_SYS_ADMIN))
411419a39dceSIlya Dryomov 		return -EPERM;
411519a39dceSIlya Dryomov 
411619a39dceSIlya Dryomov 	mutex_lock(&fs_info->balance_mutex);
411719a39dceSIlya Dryomov 	if (!fs_info->balance_ctl) {
411819a39dceSIlya Dryomov 		ret = -ENOTCONN;
411919a39dceSIlya Dryomov 		goto out;
412019a39dceSIlya Dryomov 	}
412119a39dceSIlya Dryomov 
412219a39dceSIlya Dryomov 	bargs = kzalloc(sizeof(*bargs), GFP_NOFS);
412319a39dceSIlya Dryomov 	if (!bargs) {
412419a39dceSIlya Dryomov 		ret = -ENOMEM;
412519a39dceSIlya Dryomov 		goto out;
412619a39dceSIlya Dryomov 	}
412719a39dceSIlya Dryomov 
412819a39dceSIlya Dryomov 	update_ioctl_balance_args(fs_info, 1, bargs);
412919a39dceSIlya Dryomov 
413019a39dceSIlya Dryomov 	if (copy_to_user(arg, bargs, sizeof(*bargs)))
413119a39dceSIlya Dryomov 		ret = -EFAULT;
413219a39dceSIlya Dryomov 
413319a39dceSIlya Dryomov 	kfree(bargs);
413419a39dceSIlya Dryomov out:
413519a39dceSIlya Dryomov 	mutex_unlock(&fs_info->balance_mutex);
413619a39dceSIlya Dryomov 	return ret;
413719a39dceSIlya Dryomov }
413819a39dceSIlya Dryomov 
4139905b0ddaSMiao Xie static long btrfs_ioctl_quota_ctl(struct file *file, void __user *arg)
41405d13a37bSArne Jansen {
4141496ad9aaSAl Viro 	struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
41425d13a37bSArne Jansen 	struct btrfs_ioctl_quota_ctl_args *sa;
41435d13a37bSArne Jansen 	struct btrfs_trans_handle *trans = NULL;
41445d13a37bSArne Jansen 	int ret;
41455d13a37bSArne Jansen 	int err;
41465d13a37bSArne Jansen 
41475d13a37bSArne Jansen 	if (!capable(CAP_SYS_ADMIN))
41485d13a37bSArne Jansen 		return -EPERM;
41495d13a37bSArne Jansen 
4150905b0ddaSMiao Xie 	ret = mnt_want_write_file(file);
4151905b0ddaSMiao Xie 	if (ret)
4152905b0ddaSMiao Xie 		return ret;
41535d13a37bSArne Jansen 
41545d13a37bSArne Jansen 	sa = memdup_user(arg, sizeof(*sa));
4155905b0ddaSMiao Xie 	if (IS_ERR(sa)) {
4156905b0ddaSMiao Xie 		ret = PTR_ERR(sa);
4157905b0ddaSMiao Xie 		goto drop_write;
4158905b0ddaSMiao Xie 	}
41595d13a37bSArne Jansen 
41607708f029SWang Shilong 	down_write(&root->fs_info->subvol_sem);
416192f183aaSWang Shilong 	trans = btrfs_start_transaction(root->fs_info->tree_root, 2);
41625d13a37bSArne Jansen 	if (IS_ERR(trans)) {
41635d13a37bSArne Jansen 		ret = PTR_ERR(trans);
41645d13a37bSArne Jansen 		goto out;
41655d13a37bSArne Jansen 	}
41665d13a37bSArne Jansen 
41675d13a37bSArne Jansen 	switch (sa->cmd) {
41685d13a37bSArne Jansen 	case BTRFS_QUOTA_CTL_ENABLE:
41695d13a37bSArne Jansen 		ret = btrfs_quota_enable(trans, root->fs_info);
41705d13a37bSArne Jansen 		break;
41715d13a37bSArne Jansen 	case BTRFS_QUOTA_CTL_DISABLE:
41725d13a37bSArne Jansen 		ret = btrfs_quota_disable(trans, root->fs_info);
41735d13a37bSArne Jansen 		break;
41745d13a37bSArne Jansen 	default:
41755d13a37bSArne Jansen 		ret = -EINVAL;
41765d13a37bSArne Jansen 		break;
41775d13a37bSArne Jansen 	}
41785d13a37bSArne Jansen 
417992f183aaSWang Shilong 	err = btrfs_commit_transaction(trans, root->fs_info->tree_root);
41805d13a37bSArne Jansen 	if (err && !ret)
41815d13a37bSArne Jansen 		ret = err;
41825d13a37bSArne Jansen out:
41835d13a37bSArne Jansen 	kfree(sa);
41847708f029SWang Shilong 	up_write(&root->fs_info->subvol_sem);
4185905b0ddaSMiao Xie drop_write:
4186905b0ddaSMiao Xie 	mnt_drop_write_file(file);
41875d13a37bSArne Jansen 	return ret;
41885d13a37bSArne Jansen }
41895d13a37bSArne Jansen 
4190905b0ddaSMiao Xie static long btrfs_ioctl_qgroup_assign(struct file *file, void __user *arg)
41915d13a37bSArne Jansen {
4192496ad9aaSAl Viro 	struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
41935d13a37bSArne Jansen 	struct btrfs_ioctl_qgroup_assign_args *sa;
41945d13a37bSArne Jansen 	struct btrfs_trans_handle *trans;
41955d13a37bSArne Jansen 	int ret;
41965d13a37bSArne Jansen 	int err;
41975d13a37bSArne Jansen 
41985d13a37bSArne Jansen 	if (!capable(CAP_SYS_ADMIN))
41995d13a37bSArne Jansen 		return -EPERM;
42005d13a37bSArne Jansen 
4201905b0ddaSMiao Xie 	ret = mnt_want_write_file(file);
4202905b0ddaSMiao Xie 	if (ret)
4203905b0ddaSMiao Xie 		return ret;
42045d13a37bSArne Jansen 
42055d13a37bSArne Jansen 	sa = memdup_user(arg, sizeof(*sa));
4206905b0ddaSMiao Xie 	if (IS_ERR(sa)) {
4207905b0ddaSMiao Xie 		ret = PTR_ERR(sa);
4208905b0ddaSMiao Xie 		goto drop_write;
4209905b0ddaSMiao Xie 	}
42105d13a37bSArne Jansen 
42115d13a37bSArne Jansen 	trans = btrfs_join_transaction(root);
42125d13a37bSArne Jansen 	if (IS_ERR(trans)) {
42135d13a37bSArne Jansen 		ret = PTR_ERR(trans);
42145d13a37bSArne Jansen 		goto out;
42155d13a37bSArne Jansen 	}
42165d13a37bSArne Jansen 
42175d13a37bSArne Jansen 	/* FIXME: check if the IDs really exist */
42185d13a37bSArne Jansen 	if (sa->assign) {
42195d13a37bSArne Jansen 		ret = btrfs_add_qgroup_relation(trans, root->fs_info,
42205d13a37bSArne Jansen 						sa->src, sa->dst);
42215d13a37bSArne Jansen 	} else {
42225d13a37bSArne Jansen 		ret = btrfs_del_qgroup_relation(trans, root->fs_info,
42235d13a37bSArne Jansen 						sa->src, sa->dst);
42245d13a37bSArne Jansen 	}
42255d13a37bSArne Jansen 
42265d13a37bSArne Jansen 	err = btrfs_end_transaction(trans, root);
42275d13a37bSArne Jansen 	if (err && !ret)
42285d13a37bSArne Jansen 		ret = err;
42295d13a37bSArne Jansen 
42305d13a37bSArne Jansen out:
42315d13a37bSArne Jansen 	kfree(sa);
4232905b0ddaSMiao Xie drop_write:
4233905b0ddaSMiao Xie 	mnt_drop_write_file(file);
42345d13a37bSArne Jansen 	return ret;
42355d13a37bSArne Jansen }
42365d13a37bSArne Jansen 
4237905b0ddaSMiao Xie static long btrfs_ioctl_qgroup_create(struct file *file, void __user *arg)
42385d13a37bSArne Jansen {
4239496ad9aaSAl Viro 	struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
42405d13a37bSArne Jansen 	struct btrfs_ioctl_qgroup_create_args *sa;
42415d13a37bSArne Jansen 	struct btrfs_trans_handle *trans;
42425d13a37bSArne Jansen 	int ret;
42435d13a37bSArne Jansen 	int err;
42445d13a37bSArne Jansen 
42455d13a37bSArne Jansen 	if (!capable(CAP_SYS_ADMIN))
42465d13a37bSArne Jansen 		return -EPERM;
42475d13a37bSArne Jansen 
4248905b0ddaSMiao Xie 	ret = mnt_want_write_file(file);
4249905b0ddaSMiao Xie 	if (ret)
4250905b0ddaSMiao Xie 		return ret;
42515d13a37bSArne Jansen 
42525d13a37bSArne Jansen 	sa = memdup_user(arg, sizeof(*sa));
4253905b0ddaSMiao Xie 	if (IS_ERR(sa)) {
4254905b0ddaSMiao Xie 		ret = PTR_ERR(sa);
4255905b0ddaSMiao Xie 		goto drop_write;
4256905b0ddaSMiao Xie 	}
42575d13a37bSArne Jansen 
4258d86e56cfSMiao Xie 	if (!sa->qgroupid) {
4259d86e56cfSMiao Xie 		ret = -EINVAL;
4260d86e56cfSMiao Xie 		goto out;
4261d86e56cfSMiao Xie 	}
4262d86e56cfSMiao Xie 
42635d13a37bSArne Jansen 	trans = btrfs_join_transaction(root);
42645d13a37bSArne Jansen 	if (IS_ERR(trans)) {
42655d13a37bSArne Jansen 		ret = PTR_ERR(trans);
42665d13a37bSArne Jansen 		goto out;
42675d13a37bSArne Jansen 	}
42685d13a37bSArne Jansen 
42695d13a37bSArne Jansen 	/* FIXME: check if the IDs really exist */
42705d13a37bSArne Jansen 	if (sa->create) {
42715d13a37bSArne Jansen 		ret = btrfs_create_qgroup(trans, root->fs_info, sa->qgroupid,
42725d13a37bSArne Jansen 					  NULL);
42735d13a37bSArne Jansen 	} else {
42745d13a37bSArne Jansen 		ret = btrfs_remove_qgroup(trans, root->fs_info, sa->qgroupid);
42755d13a37bSArne Jansen 	}
42765d13a37bSArne Jansen 
42775d13a37bSArne Jansen 	err = btrfs_end_transaction(trans, root);
42785d13a37bSArne Jansen 	if (err && !ret)
42795d13a37bSArne Jansen 		ret = err;
42805d13a37bSArne Jansen 
42815d13a37bSArne Jansen out:
42825d13a37bSArne Jansen 	kfree(sa);
4283905b0ddaSMiao Xie drop_write:
4284905b0ddaSMiao Xie 	mnt_drop_write_file(file);
42855d13a37bSArne Jansen 	return ret;
42865d13a37bSArne Jansen }
42875d13a37bSArne Jansen 
4288905b0ddaSMiao Xie static long btrfs_ioctl_qgroup_limit(struct file *file, void __user *arg)
42895d13a37bSArne Jansen {
4290496ad9aaSAl Viro 	struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
42915d13a37bSArne Jansen 	struct btrfs_ioctl_qgroup_limit_args *sa;
42925d13a37bSArne Jansen 	struct btrfs_trans_handle *trans;
42935d13a37bSArne Jansen 	int ret;
42945d13a37bSArne Jansen 	int err;
42955d13a37bSArne Jansen 	u64 qgroupid;
42965d13a37bSArne Jansen 
42975d13a37bSArne Jansen 	if (!capable(CAP_SYS_ADMIN))
42985d13a37bSArne Jansen 		return -EPERM;
42995d13a37bSArne Jansen 
4300905b0ddaSMiao Xie 	ret = mnt_want_write_file(file);
4301905b0ddaSMiao Xie 	if (ret)
4302905b0ddaSMiao Xie 		return ret;
43035d13a37bSArne Jansen 
43045d13a37bSArne Jansen 	sa = memdup_user(arg, sizeof(*sa));
4305905b0ddaSMiao Xie 	if (IS_ERR(sa)) {
4306905b0ddaSMiao Xie 		ret = PTR_ERR(sa);
4307905b0ddaSMiao Xie 		goto drop_write;
4308905b0ddaSMiao Xie 	}
43095d13a37bSArne Jansen 
43105d13a37bSArne Jansen 	trans = btrfs_join_transaction(root);
43115d13a37bSArne Jansen 	if (IS_ERR(trans)) {
43125d13a37bSArne Jansen 		ret = PTR_ERR(trans);
43135d13a37bSArne Jansen 		goto out;
43145d13a37bSArne Jansen 	}
43155d13a37bSArne Jansen 
43165d13a37bSArne Jansen 	qgroupid = sa->qgroupid;
43175d13a37bSArne Jansen 	if (!qgroupid) {
43185d13a37bSArne Jansen 		/* take the current subvol as qgroup */
43195d13a37bSArne Jansen 		qgroupid = root->root_key.objectid;
43205d13a37bSArne Jansen 	}
43215d13a37bSArne Jansen 
43225d13a37bSArne Jansen 	/* FIXME: check if the IDs really exist */
43235d13a37bSArne Jansen 	ret = btrfs_limit_qgroup(trans, root->fs_info, qgroupid, &sa->lim);
43245d13a37bSArne Jansen 
43255d13a37bSArne Jansen 	err = btrfs_end_transaction(trans, root);
43265d13a37bSArne Jansen 	if (err && !ret)
43275d13a37bSArne Jansen 		ret = err;
43285d13a37bSArne Jansen 
43295d13a37bSArne Jansen out:
43305d13a37bSArne Jansen 	kfree(sa);
4331905b0ddaSMiao Xie drop_write:
4332905b0ddaSMiao Xie 	mnt_drop_write_file(file);
43335d13a37bSArne Jansen 	return ret;
43345d13a37bSArne Jansen }
43355d13a37bSArne Jansen 
43362f232036SJan Schmidt static long btrfs_ioctl_quota_rescan(struct file *file, void __user *arg)
43372f232036SJan Schmidt {
43386d0379ecSAl Viro 	struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
43392f232036SJan Schmidt 	struct btrfs_ioctl_quota_rescan_args *qsa;
43402f232036SJan Schmidt 	int ret;
43412f232036SJan Schmidt 
43422f232036SJan Schmidt 	if (!capable(CAP_SYS_ADMIN))
43432f232036SJan Schmidt 		return -EPERM;
43442f232036SJan Schmidt 
43452f232036SJan Schmidt 	ret = mnt_want_write_file(file);
43462f232036SJan Schmidt 	if (ret)
43472f232036SJan Schmidt 		return ret;
43482f232036SJan Schmidt 
43492f232036SJan Schmidt 	qsa = memdup_user(arg, sizeof(*qsa));
43502f232036SJan Schmidt 	if (IS_ERR(qsa)) {
43512f232036SJan Schmidt 		ret = PTR_ERR(qsa);
43522f232036SJan Schmidt 		goto drop_write;
43532f232036SJan Schmidt 	}
43542f232036SJan Schmidt 
43552f232036SJan Schmidt 	if (qsa->flags) {
43562f232036SJan Schmidt 		ret = -EINVAL;
43572f232036SJan Schmidt 		goto out;
43582f232036SJan Schmidt 	}
43592f232036SJan Schmidt 
43602f232036SJan Schmidt 	ret = btrfs_qgroup_rescan(root->fs_info);
43612f232036SJan Schmidt 
43622f232036SJan Schmidt out:
43632f232036SJan Schmidt 	kfree(qsa);
43642f232036SJan Schmidt drop_write:
43652f232036SJan Schmidt 	mnt_drop_write_file(file);
43662f232036SJan Schmidt 	return ret;
43672f232036SJan Schmidt }
43682f232036SJan Schmidt 
43692f232036SJan Schmidt static long btrfs_ioctl_quota_rescan_status(struct file *file, void __user *arg)
43702f232036SJan Schmidt {
43716d0379ecSAl Viro 	struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
43722f232036SJan Schmidt 	struct btrfs_ioctl_quota_rescan_args *qsa;
43732f232036SJan Schmidt 	int ret = 0;
43742f232036SJan Schmidt 
43752f232036SJan Schmidt 	if (!capable(CAP_SYS_ADMIN))
43762f232036SJan Schmidt 		return -EPERM;
43772f232036SJan Schmidt 
43782f232036SJan Schmidt 	qsa = kzalloc(sizeof(*qsa), GFP_NOFS);
43792f232036SJan Schmidt 	if (!qsa)
43802f232036SJan Schmidt 		return -ENOMEM;
43812f232036SJan Schmidt 
43822f232036SJan Schmidt 	if (root->fs_info->qgroup_flags & BTRFS_QGROUP_STATUS_FLAG_RESCAN) {
43832f232036SJan Schmidt 		qsa->flags = 1;
43842f232036SJan Schmidt 		qsa->progress = root->fs_info->qgroup_rescan_progress.objectid;
43852f232036SJan Schmidt 	}
43862f232036SJan Schmidt 
43872f232036SJan Schmidt 	if (copy_to_user(arg, qsa, sizeof(*qsa)))
43882f232036SJan Schmidt 		ret = -EFAULT;
43892f232036SJan Schmidt 
43902f232036SJan Schmidt 	kfree(qsa);
43912f232036SJan Schmidt 	return ret;
43922f232036SJan Schmidt }
43932f232036SJan Schmidt 
439457254b6eSJan Schmidt static long btrfs_ioctl_quota_rescan_wait(struct file *file, void __user *arg)
439557254b6eSJan Schmidt {
439654563d41SAl Viro 	struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
439757254b6eSJan Schmidt 
439857254b6eSJan Schmidt 	if (!capable(CAP_SYS_ADMIN))
439957254b6eSJan Schmidt 		return -EPERM;
440057254b6eSJan Schmidt 
440157254b6eSJan Schmidt 	return btrfs_qgroup_wait_for_completion(root->fs_info);
440257254b6eSJan Schmidt }
440357254b6eSJan Schmidt 
4404*abccd00fSHugo Mills static long _btrfs_ioctl_set_received_subvol(struct file *file,
4405*abccd00fSHugo Mills 					    struct btrfs_ioctl_received_subvol_args *sa)
44068ea05e3aSAlexander Block {
4407496ad9aaSAl Viro 	struct inode *inode = file_inode(file);
44088ea05e3aSAlexander Block 	struct btrfs_root *root = BTRFS_I(inode)->root;
44098ea05e3aSAlexander Block 	struct btrfs_root_item *root_item = &root->root_item;
44108ea05e3aSAlexander Block 	struct btrfs_trans_handle *trans;
44118ea05e3aSAlexander Block 	struct timespec ct = CURRENT_TIME;
44128ea05e3aSAlexander Block 	int ret = 0;
4413dd5f9615SStefan Behrens 	int received_uuid_changed;
44148ea05e3aSAlexander Block 
4415bd60ea0fSDavid Sterba 	if (!inode_owner_or_capable(inode))
4416bd60ea0fSDavid Sterba 		return -EPERM;
4417bd60ea0fSDavid Sterba 
44188ea05e3aSAlexander Block 	ret = mnt_want_write_file(file);
44198ea05e3aSAlexander Block 	if (ret < 0)
44208ea05e3aSAlexander Block 		return ret;
44218ea05e3aSAlexander Block 
44228ea05e3aSAlexander Block 	down_write(&root->fs_info->subvol_sem);
44238ea05e3aSAlexander Block 
44248ea05e3aSAlexander Block 	if (btrfs_ino(inode) != BTRFS_FIRST_FREE_OBJECTID) {
44258ea05e3aSAlexander Block 		ret = -EINVAL;
44268ea05e3aSAlexander Block 		goto out;
44278ea05e3aSAlexander Block 	}
44288ea05e3aSAlexander Block 
44298ea05e3aSAlexander Block 	if (btrfs_root_readonly(root)) {
44308ea05e3aSAlexander Block 		ret = -EROFS;
44318ea05e3aSAlexander Block 		goto out;
44328ea05e3aSAlexander Block 	}
44338ea05e3aSAlexander Block 
4434dd5f9615SStefan Behrens 	/*
4435dd5f9615SStefan Behrens 	 * 1 - root item
4436dd5f9615SStefan Behrens 	 * 2 - uuid items (received uuid + subvol uuid)
4437dd5f9615SStefan Behrens 	 */
4438dd5f9615SStefan Behrens 	trans = btrfs_start_transaction(root, 3);
44398ea05e3aSAlexander Block 	if (IS_ERR(trans)) {
44408ea05e3aSAlexander Block 		ret = PTR_ERR(trans);
44418ea05e3aSAlexander Block 		trans = NULL;
44428ea05e3aSAlexander Block 		goto out;
44438ea05e3aSAlexander Block 	}
44448ea05e3aSAlexander Block 
44458ea05e3aSAlexander Block 	sa->rtransid = trans->transid;
44468ea05e3aSAlexander Block 	sa->rtime.sec = ct.tv_sec;
44478ea05e3aSAlexander Block 	sa->rtime.nsec = ct.tv_nsec;
44488ea05e3aSAlexander Block 
4449dd5f9615SStefan Behrens 	received_uuid_changed = memcmp(root_item->received_uuid, sa->uuid,
4450dd5f9615SStefan Behrens 				       BTRFS_UUID_SIZE);
4451dd5f9615SStefan Behrens 	if (received_uuid_changed &&
4452dd5f9615SStefan Behrens 	    !btrfs_is_empty_uuid(root_item->received_uuid))
4453dd5f9615SStefan Behrens 		btrfs_uuid_tree_rem(trans, root->fs_info->uuid_root,
4454dd5f9615SStefan Behrens 				    root_item->received_uuid,
4455dd5f9615SStefan Behrens 				    BTRFS_UUID_KEY_RECEIVED_SUBVOL,
4456dd5f9615SStefan Behrens 				    root->root_key.objectid);
44578ea05e3aSAlexander Block 	memcpy(root_item->received_uuid, sa->uuid, BTRFS_UUID_SIZE);
44588ea05e3aSAlexander Block 	btrfs_set_root_stransid(root_item, sa->stransid);
44598ea05e3aSAlexander Block 	btrfs_set_root_rtransid(root_item, sa->rtransid);
44603cae210fSQu Wenruo 	btrfs_set_stack_timespec_sec(&root_item->stime, sa->stime.sec);
44613cae210fSQu Wenruo 	btrfs_set_stack_timespec_nsec(&root_item->stime, sa->stime.nsec);
44623cae210fSQu Wenruo 	btrfs_set_stack_timespec_sec(&root_item->rtime, sa->rtime.sec);
44633cae210fSQu Wenruo 	btrfs_set_stack_timespec_nsec(&root_item->rtime, sa->rtime.nsec);
44648ea05e3aSAlexander Block 
44658ea05e3aSAlexander Block 	ret = btrfs_update_root(trans, root->fs_info->tree_root,
44668ea05e3aSAlexander Block 				&root->root_key, &root->root_item);
44678ea05e3aSAlexander Block 	if (ret < 0) {
44688ea05e3aSAlexander Block 		btrfs_end_transaction(trans, root);
44698ea05e3aSAlexander Block 		goto out;
4470dd5f9615SStefan Behrens 	}
4471dd5f9615SStefan Behrens 	if (received_uuid_changed && !btrfs_is_empty_uuid(sa->uuid)) {
4472dd5f9615SStefan Behrens 		ret = btrfs_uuid_tree_add(trans, root->fs_info->uuid_root,
4473dd5f9615SStefan Behrens 					  sa->uuid,
4474dd5f9615SStefan Behrens 					  BTRFS_UUID_KEY_RECEIVED_SUBVOL,
4475dd5f9615SStefan Behrens 					  root->root_key.objectid);
4476dd5f9615SStefan Behrens 		if (ret < 0 && ret != -EEXIST) {
4477dd5f9615SStefan Behrens 			btrfs_abort_transaction(trans, root, ret);
4478dd5f9615SStefan Behrens 			goto out;
4479dd5f9615SStefan Behrens 		}
4480dd5f9615SStefan Behrens 	}
44818ea05e3aSAlexander Block 	ret = btrfs_commit_transaction(trans, root);
4482dd5f9615SStefan Behrens 	if (ret < 0) {
4483dd5f9615SStefan Behrens 		btrfs_abort_transaction(trans, root, ret);
44848ea05e3aSAlexander Block 		goto out;
44858ea05e3aSAlexander Block 	}
44868ea05e3aSAlexander Block 
4487*abccd00fSHugo Mills out:
4488*abccd00fSHugo Mills 	up_write(&root->fs_info->subvol_sem);
4489*abccd00fSHugo Mills 	mnt_drop_write_file(file);
4490*abccd00fSHugo Mills 	return ret;
4491*abccd00fSHugo Mills }
4492*abccd00fSHugo Mills 
4493*abccd00fSHugo Mills #ifdef CONFIG_64BIT
4494*abccd00fSHugo Mills static long btrfs_ioctl_set_received_subvol_32(struct file *file,
4495*abccd00fSHugo Mills 						void __user *arg)
4496*abccd00fSHugo Mills {
4497*abccd00fSHugo Mills 	struct btrfs_ioctl_received_subvol_args_32 *args32 = NULL;
4498*abccd00fSHugo Mills 	struct btrfs_ioctl_received_subvol_args *args64 = NULL;
4499*abccd00fSHugo Mills 	int ret = 0;
4500*abccd00fSHugo Mills 
4501*abccd00fSHugo Mills 	args32 = memdup_user(arg, sizeof(*args32));
4502*abccd00fSHugo Mills 	if (IS_ERR(args32)) {
4503*abccd00fSHugo Mills 		ret = PTR_ERR(args32);
4504*abccd00fSHugo Mills 		args32 = NULL;
4505*abccd00fSHugo Mills 		goto out;
4506*abccd00fSHugo Mills 	}
4507*abccd00fSHugo Mills 
4508*abccd00fSHugo Mills 	args64 = kmalloc(sizeof(*args64), GFP_NOFS);
4509*abccd00fSHugo Mills 	if (IS_ERR(args64)) {
4510*abccd00fSHugo Mills 		ret = PTR_ERR(args64);
4511*abccd00fSHugo Mills 		args64 = NULL;
4512*abccd00fSHugo Mills 		goto out;
4513*abccd00fSHugo Mills 	}
4514*abccd00fSHugo Mills 
4515*abccd00fSHugo Mills 	memcpy(args64->uuid, args32->uuid, BTRFS_UUID_SIZE);
4516*abccd00fSHugo Mills 	args64->stransid = args32->stransid;
4517*abccd00fSHugo Mills 	args64->rtransid = args32->rtransid;
4518*abccd00fSHugo Mills 	args64->stime.sec = args32->stime.sec;
4519*abccd00fSHugo Mills 	args64->stime.nsec = args32->stime.nsec;
4520*abccd00fSHugo Mills 	args64->rtime.sec = args32->rtime.sec;
4521*abccd00fSHugo Mills 	args64->rtime.nsec = args32->rtime.nsec;
4522*abccd00fSHugo Mills 	args64->flags = args32->flags;
4523*abccd00fSHugo Mills 
4524*abccd00fSHugo Mills 	ret = _btrfs_ioctl_set_received_subvol(file, args64);
4525*abccd00fSHugo Mills 	if (ret)
4526*abccd00fSHugo Mills 		goto out;
4527*abccd00fSHugo Mills 
4528*abccd00fSHugo Mills 	memcpy(args32->uuid, args64->uuid, BTRFS_UUID_SIZE);
4529*abccd00fSHugo Mills 	args32->stransid = args64->stransid;
4530*abccd00fSHugo Mills 	args32->rtransid = args64->rtransid;
4531*abccd00fSHugo Mills 	args32->stime.sec = args64->stime.sec;
4532*abccd00fSHugo Mills 	args32->stime.nsec = args64->stime.nsec;
4533*abccd00fSHugo Mills 	args32->rtime.sec = args64->rtime.sec;
4534*abccd00fSHugo Mills 	args32->rtime.nsec = args64->rtime.nsec;
4535*abccd00fSHugo Mills 	args32->flags = args64->flags;
4536*abccd00fSHugo Mills 
4537*abccd00fSHugo Mills 	ret = copy_to_user(arg, args32, sizeof(*args32));
4538*abccd00fSHugo Mills 	if (ret)
4539*abccd00fSHugo Mills 		ret = -EFAULT;
4540*abccd00fSHugo Mills 
4541*abccd00fSHugo Mills out:
4542*abccd00fSHugo Mills 	kfree(args32);
4543*abccd00fSHugo Mills 	kfree(args64);
4544*abccd00fSHugo Mills 	return ret;
4545*abccd00fSHugo Mills }
4546*abccd00fSHugo Mills #endif
4547*abccd00fSHugo Mills 
4548*abccd00fSHugo Mills static long btrfs_ioctl_set_received_subvol(struct file *file,
4549*abccd00fSHugo Mills 					    void __user *arg)
4550*abccd00fSHugo Mills {
4551*abccd00fSHugo Mills 	struct btrfs_ioctl_received_subvol_args *sa = NULL;
4552*abccd00fSHugo Mills 	int ret = 0;
4553*abccd00fSHugo Mills 
4554*abccd00fSHugo Mills 	sa = memdup_user(arg, sizeof(*sa));
4555*abccd00fSHugo Mills 	if (IS_ERR(sa)) {
4556*abccd00fSHugo Mills 		ret = PTR_ERR(sa);
4557*abccd00fSHugo Mills 		sa = NULL;
4558*abccd00fSHugo Mills 		goto out;
4559*abccd00fSHugo Mills 	}
4560*abccd00fSHugo Mills 
4561*abccd00fSHugo Mills 	ret = _btrfs_ioctl_set_received_subvol(file, sa);
4562*abccd00fSHugo Mills 
4563*abccd00fSHugo Mills 	if (ret)
4564*abccd00fSHugo Mills 		goto out;
4565*abccd00fSHugo Mills 
45668ea05e3aSAlexander Block 	ret = copy_to_user(arg, sa, sizeof(*sa));
45678ea05e3aSAlexander Block 	if (ret)
45688ea05e3aSAlexander Block 		ret = -EFAULT;
45698ea05e3aSAlexander Block 
45708ea05e3aSAlexander Block out:
45718ea05e3aSAlexander Block 	kfree(sa);
45728ea05e3aSAlexander Block 	return ret;
45738ea05e3aSAlexander Block }
45748ea05e3aSAlexander Block 
4575867ab667Sjeff.liu static int btrfs_ioctl_get_fslabel(struct file *file, void __user *arg)
4576867ab667Sjeff.liu {
45776d0379ecSAl Viro 	struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
4578a1b83ac5SAnand Jain 	size_t len;
4579867ab667Sjeff.liu 	int ret;
4580a1b83ac5SAnand Jain 	char label[BTRFS_LABEL_SIZE];
4581a1b83ac5SAnand Jain 
4582a1b83ac5SAnand Jain 	spin_lock(&root->fs_info->super_lock);
4583a1b83ac5SAnand Jain 	memcpy(label, root->fs_info->super_copy->label, BTRFS_LABEL_SIZE);
4584a1b83ac5SAnand Jain 	spin_unlock(&root->fs_info->super_lock);
4585a1b83ac5SAnand Jain 
4586a1b83ac5SAnand Jain 	len = strnlen(label, BTRFS_LABEL_SIZE);
4587867ab667Sjeff.liu 
4588867ab667Sjeff.liu 	if (len == BTRFS_LABEL_SIZE) {
4589efe120a0SFrank Holton 		btrfs_warn(root->fs_info,
4590efe120a0SFrank Holton 			"label is too long, return the first %zu bytes", --len);
4591867ab667Sjeff.liu 	}
4592867ab667Sjeff.liu 
4593867ab667Sjeff.liu 	ret = copy_to_user(arg, label, len);
4594867ab667Sjeff.liu 
4595867ab667Sjeff.liu 	return ret ? -EFAULT : 0;
4596867ab667Sjeff.liu }
4597867ab667Sjeff.liu 
4598a8bfd4abSjeff.liu static int btrfs_ioctl_set_fslabel(struct file *file, void __user *arg)
4599a8bfd4abSjeff.liu {
46006d0379ecSAl Viro 	struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
4601a8bfd4abSjeff.liu 	struct btrfs_super_block *super_block = root->fs_info->super_copy;
4602a8bfd4abSjeff.liu 	struct btrfs_trans_handle *trans;
4603a8bfd4abSjeff.liu 	char label[BTRFS_LABEL_SIZE];
4604a8bfd4abSjeff.liu 	int ret;
4605a8bfd4abSjeff.liu 
4606a8bfd4abSjeff.liu 	if (!capable(CAP_SYS_ADMIN))
4607a8bfd4abSjeff.liu 		return -EPERM;
4608a8bfd4abSjeff.liu 
4609a8bfd4abSjeff.liu 	if (copy_from_user(label, arg, sizeof(label)))
4610a8bfd4abSjeff.liu 		return -EFAULT;
4611a8bfd4abSjeff.liu 
4612a8bfd4abSjeff.liu 	if (strnlen(label, BTRFS_LABEL_SIZE) == BTRFS_LABEL_SIZE) {
4613efe120a0SFrank Holton 		btrfs_err(root->fs_info, "unable to set label with more than %d bytes",
4614a8bfd4abSjeff.liu 		       BTRFS_LABEL_SIZE - 1);
4615a8bfd4abSjeff.liu 		return -EINVAL;
4616a8bfd4abSjeff.liu 	}
4617a8bfd4abSjeff.liu 
4618a8bfd4abSjeff.liu 	ret = mnt_want_write_file(file);
4619a8bfd4abSjeff.liu 	if (ret)
4620a8bfd4abSjeff.liu 		return ret;
4621a8bfd4abSjeff.liu 
4622a8bfd4abSjeff.liu 	trans = btrfs_start_transaction(root, 0);
4623a8bfd4abSjeff.liu 	if (IS_ERR(trans)) {
4624a8bfd4abSjeff.liu 		ret = PTR_ERR(trans);
4625a8bfd4abSjeff.liu 		goto out_unlock;
4626a8bfd4abSjeff.liu 	}
4627a8bfd4abSjeff.liu 
4628a1b83ac5SAnand Jain 	spin_lock(&root->fs_info->super_lock);
4629a8bfd4abSjeff.liu 	strcpy(super_block->label, label);
4630a1b83ac5SAnand Jain 	spin_unlock(&root->fs_info->super_lock);
4631d0270acaSJeff Mahoney 	ret = btrfs_commit_transaction(trans, root);
4632a8bfd4abSjeff.liu 
4633a8bfd4abSjeff.liu out_unlock:
4634a8bfd4abSjeff.liu 	mnt_drop_write_file(file);
4635a8bfd4abSjeff.liu 	return ret;
4636a8bfd4abSjeff.liu }
4637a8bfd4abSjeff.liu 
46382eaa055fSJeff Mahoney #define INIT_FEATURE_FLAGS(suffix) \
46392eaa055fSJeff Mahoney 	{ .compat_flags = BTRFS_FEATURE_COMPAT_##suffix, \
46402eaa055fSJeff Mahoney 	  .compat_ro_flags = BTRFS_FEATURE_COMPAT_RO_##suffix, \
46412eaa055fSJeff Mahoney 	  .incompat_flags = BTRFS_FEATURE_INCOMPAT_##suffix }
46422eaa055fSJeff Mahoney 
46432eaa055fSJeff Mahoney static int btrfs_ioctl_get_supported_features(struct file *file,
46442eaa055fSJeff Mahoney 					      void __user *arg)
46452eaa055fSJeff Mahoney {
46462eaa055fSJeff Mahoney 	static struct btrfs_ioctl_feature_flags features[3] = {
46472eaa055fSJeff Mahoney 		INIT_FEATURE_FLAGS(SUPP),
46482eaa055fSJeff Mahoney 		INIT_FEATURE_FLAGS(SAFE_SET),
46492eaa055fSJeff Mahoney 		INIT_FEATURE_FLAGS(SAFE_CLEAR)
46502eaa055fSJeff Mahoney 	};
46512eaa055fSJeff Mahoney 
46522eaa055fSJeff Mahoney 	if (copy_to_user(arg, &features, sizeof(features)))
46532eaa055fSJeff Mahoney 		return -EFAULT;
46542eaa055fSJeff Mahoney 
46552eaa055fSJeff Mahoney 	return 0;
46562eaa055fSJeff Mahoney }
46572eaa055fSJeff Mahoney 
46582eaa055fSJeff Mahoney static int btrfs_ioctl_get_features(struct file *file, void __user *arg)
46592eaa055fSJeff Mahoney {
46602eaa055fSJeff Mahoney 	struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
46612eaa055fSJeff Mahoney 	struct btrfs_super_block *super_block = root->fs_info->super_copy;
46622eaa055fSJeff Mahoney 	struct btrfs_ioctl_feature_flags features;
46632eaa055fSJeff Mahoney 
46642eaa055fSJeff Mahoney 	features.compat_flags = btrfs_super_compat_flags(super_block);
46652eaa055fSJeff Mahoney 	features.compat_ro_flags = btrfs_super_compat_ro_flags(super_block);
46662eaa055fSJeff Mahoney 	features.incompat_flags = btrfs_super_incompat_flags(super_block);
46672eaa055fSJeff Mahoney 
46682eaa055fSJeff Mahoney 	if (copy_to_user(arg, &features, sizeof(features)))
46692eaa055fSJeff Mahoney 		return -EFAULT;
46702eaa055fSJeff Mahoney 
46712eaa055fSJeff Mahoney 	return 0;
46722eaa055fSJeff Mahoney }
46732eaa055fSJeff Mahoney 
46743b02a68aSJeff Mahoney static int check_feature_bits(struct btrfs_root *root,
46753b02a68aSJeff Mahoney 			      enum btrfs_feature_set set,
46762eaa055fSJeff Mahoney 			      u64 change_mask, u64 flags, u64 supported_flags,
46772eaa055fSJeff Mahoney 			      u64 safe_set, u64 safe_clear)
46782eaa055fSJeff Mahoney {
46793b02a68aSJeff Mahoney 	const char *type = btrfs_feature_set_names[set];
46803b02a68aSJeff Mahoney 	char *names;
46812eaa055fSJeff Mahoney 	u64 disallowed, unsupported;
46822eaa055fSJeff Mahoney 	u64 set_mask = flags & change_mask;
46832eaa055fSJeff Mahoney 	u64 clear_mask = ~flags & change_mask;
46842eaa055fSJeff Mahoney 
46852eaa055fSJeff Mahoney 	unsupported = set_mask & ~supported_flags;
46862eaa055fSJeff Mahoney 	if (unsupported) {
46873b02a68aSJeff Mahoney 		names = btrfs_printable_features(set, unsupported);
46883b02a68aSJeff Mahoney 		if (names) {
46893b02a68aSJeff Mahoney 			btrfs_warn(root->fs_info,
46903b02a68aSJeff Mahoney 			   "this kernel does not support the %s feature bit%s",
46913b02a68aSJeff Mahoney 			   names, strchr(names, ',') ? "s" : "");
46923b02a68aSJeff Mahoney 			kfree(names);
46933b02a68aSJeff Mahoney 		} else
46942eaa055fSJeff Mahoney 			btrfs_warn(root->fs_info,
46952eaa055fSJeff Mahoney 			   "this kernel does not support %s bits 0x%llx",
46962eaa055fSJeff Mahoney 			   type, unsupported);
46972eaa055fSJeff Mahoney 		return -EOPNOTSUPP;
46982eaa055fSJeff Mahoney 	}
46992eaa055fSJeff Mahoney 
47002eaa055fSJeff Mahoney 	disallowed = set_mask & ~safe_set;
47012eaa055fSJeff Mahoney 	if (disallowed) {
47023b02a68aSJeff Mahoney 		names = btrfs_printable_features(set, disallowed);
47033b02a68aSJeff Mahoney 		if (names) {
47043b02a68aSJeff Mahoney 			btrfs_warn(root->fs_info,
47053b02a68aSJeff Mahoney 			   "can't set the %s feature bit%s while mounted",
47063b02a68aSJeff Mahoney 			   names, strchr(names, ',') ? "s" : "");
47073b02a68aSJeff Mahoney 			kfree(names);
47083b02a68aSJeff Mahoney 		} else
47092eaa055fSJeff Mahoney 			btrfs_warn(root->fs_info,
47102eaa055fSJeff Mahoney 			   "can't set %s bits 0x%llx while mounted",
47112eaa055fSJeff Mahoney 			   type, disallowed);
47122eaa055fSJeff Mahoney 		return -EPERM;
47132eaa055fSJeff Mahoney 	}
47142eaa055fSJeff Mahoney 
47152eaa055fSJeff Mahoney 	disallowed = clear_mask & ~safe_clear;
47162eaa055fSJeff Mahoney 	if (disallowed) {
47173b02a68aSJeff Mahoney 		names = btrfs_printable_features(set, disallowed);
47183b02a68aSJeff Mahoney 		if (names) {
47193b02a68aSJeff Mahoney 			btrfs_warn(root->fs_info,
47203b02a68aSJeff Mahoney 			   "can't clear the %s feature bit%s while mounted",
47213b02a68aSJeff Mahoney 			   names, strchr(names, ',') ? "s" : "");
47223b02a68aSJeff Mahoney 			kfree(names);
47233b02a68aSJeff Mahoney 		} else
47242eaa055fSJeff Mahoney 			btrfs_warn(root->fs_info,
47252eaa055fSJeff Mahoney 			   "can't clear %s bits 0x%llx while mounted",
47262eaa055fSJeff Mahoney 			   type, disallowed);
47272eaa055fSJeff Mahoney 		return -EPERM;
47282eaa055fSJeff Mahoney 	}
47292eaa055fSJeff Mahoney 
47302eaa055fSJeff Mahoney 	return 0;
47312eaa055fSJeff Mahoney }
47322eaa055fSJeff Mahoney 
47332eaa055fSJeff Mahoney #define check_feature(root, change_mask, flags, mask_base)	\
47343b02a68aSJeff Mahoney check_feature_bits(root, FEAT_##mask_base, change_mask, flags,	\
47352eaa055fSJeff Mahoney 		   BTRFS_FEATURE_ ## mask_base ## _SUPP,	\
47362eaa055fSJeff Mahoney 		   BTRFS_FEATURE_ ## mask_base ## _SAFE_SET,	\
47372eaa055fSJeff Mahoney 		   BTRFS_FEATURE_ ## mask_base ## _SAFE_CLEAR)
47382eaa055fSJeff Mahoney 
47392eaa055fSJeff Mahoney static int btrfs_ioctl_set_features(struct file *file, void __user *arg)
47402eaa055fSJeff Mahoney {
47412eaa055fSJeff Mahoney 	struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
47422eaa055fSJeff Mahoney 	struct btrfs_super_block *super_block = root->fs_info->super_copy;
47432eaa055fSJeff Mahoney 	struct btrfs_ioctl_feature_flags flags[2];
47442eaa055fSJeff Mahoney 	struct btrfs_trans_handle *trans;
47452eaa055fSJeff Mahoney 	u64 newflags;
47462eaa055fSJeff Mahoney 	int ret;
47472eaa055fSJeff Mahoney 
47482eaa055fSJeff Mahoney 	if (!capable(CAP_SYS_ADMIN))
47492eaa055fSJeff Mahoney 		return -EPERM;
47502eaa055fSJeff Mahoney 
47512eaa055fSJeff Mahoney 	if (copy_from_user(flags, arg, sizeof(flags)))
47522eaa055fSJeff Mahoney 		return -EFAULT;
47532eaa055fSJeff Mahoney 
47542eaa055fSJeff Mahoney 	/* Nothing to do */
47552eaa055fSJeff Mahoney 	if (!flags[0].compat_flags && !flags[0].compat_ro_flags &&
47562eaa055fSJeff Mahoney 	    !flags[0].incompat_flags)
47572eaa055fSJeff Mahoney 		return 0;
47582eaa055fSJeff Mahoney 
47592eaa055fSJeff Mahoney 	ret = check_feature(root, flags[0].compat_flags,
47602eaa055fSJeff Mahoney 			    flags[1].compat_flags, COMPAT);
47612eaa055fSJeff Mahoney 	if (ret)
47622eaa055fSJeff Mahoney 		return ret;
47632eaa055fSJeff Mahoney 
47642eaa055fSJeff Mahoney 	ret = check_feature(root, flags[0].compat_ro_flags,
47652eaa055fSJeff Mahoney 			    flags[1].compat_ro_flags, COMPAT_RO);
47662eaa055fSJeff Mahoney 	if (ret)
47672eaa055fSJeff Mahoney 		return ret;
47682eaa055fSJeff Mahoney 
47692eaa055fSJeff Mahoney 	ret = check_feature(root, flags[0].incompat_flags,
47702eaa055fSJeff Mahoney 			    flags[1].incompat_flags, INCOMPAT);
47712eaa055fSJeff Mahoney 	if (ret)
47722eaa055fSJeff Mahoney 		return ret;
47732eaa055fSJeff Mahoney 
47748051aa1aSDavid Sterba 	trans = btrfs_start_transaction(root, 0);
47752eaa055fSJeff Mahoney 	if (IS_ERR(trans))
47762eaa055fSJeff Mahoney 		return PTR_ERR(trans);
47772eaa055fSJeff Mahoney 
47782eaa055fSJeff Mahoney 	spin_lock(&root->fs_info->super_lock);
47792eaa055fSJeff Mahoney 	newflags = btrfs_super_compat_flags(super_block);
47802eaa055fSJeff Mahoney 	newflags |= flags[0].compat_flags & flags[1].compat_flags;
47812eaa055fSJeff Mahoney 	newflags &= ~(flags[0].compat_flags & ~flags[1].compat_flags);
47822eaa055fSJeff Mahoney 	btrfs_set_super_compat_flags(super_block, newflags);
47832eaa055fSJeff Mahoney 
47842eaa055fSJeff Mahoney 	newflags = btrfs_super_compat_ro_flags(super_block);
47852eaa055fSJeff Mahoney 	newflags |= flags[0].compat_ro_flags & flags[1].compat_ro_flags;
47862eaa055fSJeff Mahoney 	newflags &= ~(flags[0].compat_ro_flags & ~flags[1].compat_ro_flags);
47872eaa055fSJeff Mahoney 	btrfs_set_super_compat_ro_flags(super_block, newflags);
47882eaa055fSJeff Mahoney 
47892eaa055fSJeff Mahoney 	newflags = btrfs_super_incompat_flags(super_block);
47902eaa055fSJeff Mahoney 	newflags |= flags[0].incompat_flags & flags[1].incompat_flags;
47912eaa055fSJeff Mahoney 	newflags &= ~(flags[0].incompat_flags & ~flags[1].incompat_flags);
47922eaa055fSJeff Mahoney 	btrfs_set_super_incompat_flags(super_block, newflags);
47932eaa055fSJeff Mahoney 	spin_unlock(&root->fs_info->super_lock);
47942eaa055fSJeff Mahoney 
4795d0270acaSJeff Mahoney 	return btrfs_commit_transaction(trans, root);
47962eaa055fSJeff Mahoney }
47972eaa055fSJeff Mahoney 
4798f46b5a66SChristoph Hellwig long btrfs_ioctl(struct file *file, unsigned int
4799f46b5a66SChristoph Hellwig 		cmd, unsigned long arg)
4800f46b5a66SChristoph Hellwig {
4801496ad9aaSAl Viro 	struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
48024bcabaa3SChristoph Hellwig 	void __user *argp = (void __user *)arg;
4803f46b5a66SChristoph Hellwig 
4804f46b5a66SChristoph Hellwig 	switch (cmd) {
48056cbff00fSChristoph Hellwig 	case FS_IOC_GETFLAGS:
48066cbff00fSChristoph Hellwig 		return btrfs_ioctl_getflags(file, argp);
48076cbff00fSChristoph Hellwig 	case FS_IOC_SETFLAGS:
48086cbff00fSChristoph Hellwig 		return btrfs_ioctl_setflags(file, argp);
48096cbff00fSChristoph Hellwig 	case FS_IOC_GETVERSION:
48106cbff00fSChristoph Hellwig 		return btrfs_ioctl_getversion(file, argp);
4811f7039b1dSLi Dongyang 	case FITRIM:
4812f7039b1dSLi Dongyang 		return btrfs_ioctl_fitrim(file, argp);
4813f46b5a66SChristoph Hellwig 	case BTRFS_IOC_SNAP_CREATE:
4814fa0d2b9bSLi Zefan 		return btrfs_ioctl_snap_create(file, argp, 0);
4815fdfb1e4fSLi Zefan 	case BTRFS_IOC_SNAP_CREATE_V2:
4816fa0d2b9bSLi Zefan 		return btrfs_ioctl_snap_create_v2(file, argp, 0);
48173de4586cSChris Mason 	case BTRFS_IOC_SUBVOL_CREATE:
4818fa0d2b9bSLi Zefan 		return btrfs_ioctl_snap_create(file, argp, 1);
48196f72c7e2SArne Jansen 	case BTRFS_IOC_SUBVOL_CREATE_V2:
48206f72c7e2SArne Jansen 		return btrfs_ioctl_snap_create_v2(file, argp, 1);
482176dda93cSYan, Zheng 	case BTRFS_IOC_SNAP_DESTROY:
482276dda93cSYan, Zheng 		return btrfs_ioctl_snap_destroy(file, argp);
48230caa102dSLi Zefan 	case BTRFS_IOC_SUBVOL_GETFLAGS:
48240caa102dSLi Zefan 		return btrfs_ioctl_subvol_getflags(file, argp);
48250caa102dSLi Zefan 	case BTRFS_IOC_SUBVOL_SETFLAGS:
48260caa102dSLi Zefan 		return btrfs_ioctl_subvol_setflags(file, argp);
48276ef5ed0dSJosef Bacik 	case BTRFS_IOC_DEFAULT_SUBVOL:
48286ef5ed0dSJosef Bacik 		return btrfs_ioctl_default_subvol(file, argp);
4829f46b5a66SChristoph Hellwig 	case BTRFS_IOC_DEFRAG:
48301e701a32SChris Mason 		return btrfs_ioctl_defrag(file, NULL);
48311e701a32SChris Mason 	case BTRFS_IOC_DEFRAG_RANGE:
48321e701a32SChris Mason 		return btrfs_ioctl_defrag(file, argp);
4833f46b5a66SChristoph Hellwig 	case BTRFS_IOC_RESIZE:
4834198605a8SMiao Xie 		return btrfs_ioctl_resize(file, argp);
4835f46b5a66SChristoph Hellwig 	case BTRFS_IOC_ADD_DEV:
48364bcabaa3SChristoph Hellwig 		return btrfs_ioctl_add_dev(root, argp);
4837f46b5a66SChristoph Hellwig 	case BTRFS_IOC_RM_DEV:
4838da24927bSMiao Xie 		return btrfs_ioctl_rm_dev(file, argp);
4839475f6387SJan Schmidt 	case BTRFS_IOC_FS_INFO:
4840475f6387SJan Schmidt 		return btrfs_ioctl_fs_info(root, argp);
4841475f6387SJan Schmidt 	case BTRFS_IOC_DEV_INFO:
4842475f6387SJan Schmidt 		return btrfs_ioctl_dev_info(root, argp);
4843f46b5a66SChristoph Hellwig 	case BTRFS_IOC_BALANCE:
48449ba1f6e4SLiu Bo 		return btrfs_ioctl_balance(file, NULL);
4845f46b5a66SChristoph Hellwig 	case BTRFS_IOC_CLONE:
4846c5c9cd4dSSage Weil 		return btrfs_ioctl_clone(file, arg, 0, 0, 0);
4847c5c9cd4dSSage Weil 	case BTRFS_IOC_CLONE_RANGE:
48487a865e8aSChristoph Hellwig 		return btrfs_ioctl_clone_range(file, argp);
4849f46b5a66SChristoph Hellwig 	case BTRFS_IOC_TRANS_START:
4850f46b5a66SChristoph Hellwig 		return btrfs_ioctl_trans_start(file);
4851f46b5a66SChristoph Hellwig 	case BTRFS_IOC_TRANS_END:
4852f46b5a66SChristoph Hellwig 		return btrfs_ioctl_trans_end(file);
4853ac8e9819SChris Mason 	case BTRFS_IOC_TREE_SEARCH:
4854ac8e9819SChris Mason 		return btrfs_ioctl_tree_search(file, argp);
4855ac8e9819SChris Mason 	case BTRFS_IOC_INO_LOOKUP:
4856ac8e9819SChris Mason 		return btrfs_ioctl_ino_lookup(file, argp);
4857d7728c96SJan Schmidt 	case BTRFS_IOC_INO_PATHS:
4858d7728c96SJan Schmidt 		return btrfs_ioctl_ino_to_path(root, argp);
4859d7728c96SJan Schmidt 	case BTRFS_IOC_LOGICAL_INO:
4860d7728c96SJan Schmidt 		return btrfs_ioctl_logical_to_ino(root, argp);
48611406e432SJosef Bacik 	case BTRFS_IOC_SPACE_INFO:
48621406e432SJosef Bacik 		return btrfs_ioctl_space_info(root, argp);
48639b199859SFilipe David Borba Manana 	case BTRFS_IOC_SYNC: {
48649b199859SFilipe David Borba Manana 		int ret;
48659b199859SFilipe David Borba Manana 
486691aef86fSMiao Xie 		ret = btrfs_start_delalloc_roots(root->fs_info, 0);
48679b199859SFilipe David Borba Manana 		if (ret)
48689b199859SFilipe David Borba Manana 			return ret;
48699b199859SFilipe David Borba Manana 		ret = btrfs_sync_fs(file->f_dentry->d_sb, 1);
48709b199859SFilipe David Borba Manana 		return ret;
48719b199859SFilipe David Borba Manana 	}
487246204592SSage Weil 	case BTRFS_IOC_START_SYNC:
48739a8c28beSMiao Xie 		return btrfs_ioctl_start_sync(root, argp);
487446204592SSage Weil 	case BTRFS_IOC_WAIT_SYNC:
48759a8c28beSMiao Xie 		return btrfs_ioctl_wait_sync(root, argp);
4876475f6387SJan Schmidt 	case BTRFS_IOC_SCRUB:
4877b8e95489SMiao Xie 		return btrfs_ioctl_scrub(file, argp);
4878475f6387SJan Schmidt 	case BTRFS_IOC_SCRUB_CANCEL:
4879475f6387SJan Schmidt 		return btrfs_ioctl_scrub_cancel(root, argp);
4880475f6387SJan Schmidt 	case BTRFS_IOC_SCRUB_PROGRESS:
4881475f6387SJan Schmidt 		return btrfs_ioctl_scrub_progress(root, argp);
4882c9e9f97bSIlya Dryomov 	case BTRFS_IOC_BALANCE_V2:
48839ba1f6e4SLiu Bo 		return btrfs_ioctl_balance(file, argp);
4884837d5b6eSIlya Dryomov 	case BTRFS_IOC_BALANCE_CTL:
4885837d5b6eSIlya Dryomov 		return btrfs_ioctl_balance_ctl(root, arg);
488619a39dceSIlya Dryomov 	case BTRFS_IOC_BALANCE_PROGRESS:
488719a39dceSIlya Dryomov 		return btrfs_ioctl_balance_progress(root, argp);
48888ea05e3aSAlexander Block 	case BTRFS_IOC_SET_RECEIVED_SUBVOL:
48898ea05e3aSAlexander Block 		return btrfs_ioctl_set_received_subvol(file, argp);
4890*abccd00fSHugo Mills #ifdef CONFIG_64BIT
4891*abccd00fSHugo Mills 	case BTRFS_IOC_SET_RECEIVED_SUBVOL_32:
4892*abccd00fSHugo Mills 		return btrfs_ioctl_set_received_subvol_32(file, argp);
4893*abccd00fSHugo Mills #endif
489431db9f7cSAlexander Block 	case BTRFS_IOC_SEND:
489531db9f7cSAlexander Block 		return btrfs_ioctl_send(file, argp);
4896c11d2c23SStefan Behrens 	case BTRFS_IOC_GET_DEV_STATS:
4897b27f7c0cSDavid Sterba 		return btrfs_ioctl_get_dev_stats(root, argp);
48985d13a37bSArne Jansen 	case BTRFS_IOC_QUOTA_CTL:
4899905b0ddaSMiao Xie 		return btrfs_ioctl_quota_ctl(file, argp);
49005d13a37bSArne Jansen 	case BTRFS_IOC_QGROUP_ASSIGN:
4901905b0ddaSMiao Xie 		return btrfs_ioctl_qgroup_assign(file, argp);
49025d13a37bSArne Jansen 	case BTRFS_IOC_QGROUP_CREATE:
4903905b0ddaSMiao Xie 		return btrfs_ioctl_qgroup_create(file, argp);
49045d13a37bSArne Jansen 	case BTRFS_IOC_QGROUP_LIMIT:
4905905b0ddaSMiao Xie 		return btrfs_ioctl_qgroup_limit(file, argp);
49062f232036SJan Schmidt 	case BTRFS_IOC_QUOTA_RESCAN:
49072f232036SJan Schmidt 		return btrfs_ioctl_quota_rescan(file, argp);
49082f232036SJan Schmidt 	case BTRFS_IOC_QUOTA_RESCAN_STATUS:
49092f232036SJan Schmidt 		return btrfs_ioctl_quota_rescan_status(file, argp);
491057254b6eSJan Schmidt 	case BTRFS_IOC_QUOTA_RESCAN_WAIT:
491157254b6eSJan Schmidt 		return btrfs_ioctl_quota_rescan_wait(file, argp);
49123f6bcfbdSStefan Behrens 	case BTRFS_IOC_DEV_REPLACE:
49133f6bcfbdSStefan Behrens 		return btrfs_ioctl_dev_replace(root, argp);
4914867ab667Sjeff.liu 	case BTRFS_IOC_GET_FSLABEL:
4915867ab667Sjeff.liu 		return btrfs_ioctl_get_fslabel(file, argp);
4916a8bfd4abSjeff.liu 	case BTRFS_IOC_SET_FSLABEL:
4917a8bfd4abSjeff.liu 		return btrfs_ioctl_set_fslabel(file, argp);
4918416161dbSMark Fasheh 	case BTRFS_IOC_FILE_EXTENT_SAME:
4919416161dbSMark Fasheh 		return btrfs_ioctl_file_extent_same(file, argp);
49202eaa055fSJeff Mahoney 	case BTRFS_IOC_GET_SUPPORTED_FEATURES:
49212eaa055fSJeff Mahoney 		return btrfs_ioctl_get_supported_features(file, argp);
49222eaa055fSJeff Mahoney 	case BTRFS_IOC_GET_FEATURES:
49232eaa055fSJeff Mahoney 		return btrfs_ioctl_get_features(file, argp);
49242eaa055fSJeff Mahoney 	case BTRFS_IOC_SET_FEATURES:
49252eaa055fSJeff Mahoney 		return btrfs_ioctl_set_features(file, argp);
4926f46b5a66SChristoph Hellwig 	}
4927f46b5a66SChristoph Hellwig 
4928f46b5a66SChristoph Hellwig 	return -ENOTTY;
4929f46b5a66SChristoph Hellwig }
4930