xref: /openbmc/linux/fs/btrfs/ioctl.c (revision 63a212ab)
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>
454b4e25f2SChris Mason #include "compat.h"
46f46b5a66SChristoph Hellwig #include "ctree.h"
47f46b5a66SChristoph Hellwig #include "disk-io.h"
48f46b5a66SChristoph Hellwig #include "transaction.h"
49f46b5a66SChristoph Hellwig #include "btrfs_inode.h"
50f46b5a66SChristoph Hellwig #include "ioctl.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"
58f46b5a66SChristoph Hellwig 
596cbff00fSChristoph Hellwig /* Mask out flags that are inappropriate for the given type of inode. */
606cbff00fSChristoph Hellwig static inline __u32 btrfs_mask_flags(umode_t mode, __u32 flags)
616cbff00fSChristoph Hellwig {
626cbff00fSChristoph Hellwig 	if (S_ISDIR(mode))
636cbff00fSChristoph Hellwig 		return flags;
646cbff00fSChristoph Hellwig 	else if (S_ISREG(mode))
656cbff00fSChristoph Hellwig 		return flags & ~FS_DIRSYNC_FL;
666cbff00fSChristoph Hellwig 	else
676cbff00fSChristoph Hellwig 		return flags & (FS_NODUMP_FL | FS_NOATIME_FL);
686cbff00fSChristoph Hellwig }
69f46b5a66SChristoph Hellwig 
706cbff00fSChristoph Hellwig /*
716cbff00fSChristoph Hellwig  * Export inode flags to the format expected by the FS_IOC_GETFLAGS ioctl.
726cbff00fSChristoph Hellwig  */
736cbff00fSChristoph Hellwig static unsigned int btrfs_flags_to_ioctl(unsigned int flags)
746cbff00fSChristoph Hellwig {
756cbff00fSChristoph Hellwig 	unsigned int iflags = 0;
766cbff00fSChristoph Hellwig 
776cbff00fSChristoph Hellwig 	if (flags & BTRFS_INODE_SYNC)
786cbff00fSChristoph Hellwig 		iflags |= FS_SYNC_FL;
796cbff00fSChristoph Hellwig 	if (flags & BTRFS_INODE_IMMUTABLE)
806cbff00fSChristoph Hellwig 		iflags |= FS_IMMUTABLE_FL;
816cbff00fSChristoph Hellwig 	if (flags & BTRFS_INODE_APPEND)
826cbff00fSChristoph Hellwig 		iflags |= FS_APPEND_FL;
836cbff00fSChristoph Hellwig 	if (flags & BTRFS_INODE_NODUMP)
846cbff00fSChristoph Hellwig 		iflags |= FS_NODUMP_FL;
856cbff00fSChristoph Hellwig 	if (flags & BTRFS_INODE_NOATIME)
866cbff00fSChristoph Hellwig 		iflags |= FS_NOATIME_FL;
876cbff00fSChristoph Hellwig 	if (flags & BTRFS_INODE_DIRSYNC)
886cbff00fSChristoph Hellwig 		iflags |= FS_DIRSYNC_FL;
89d0092bddSLi Zefan 	if (flags & BTRFS_INODE_NODATACOW)
90d0092bddSLi Zefan 		iflags |= FS_NOCOW_FL;
91d0092bddSLi Zefan 
92d0092bddSLi Zefan 	if ((flags & BTRFS_INODE_COMPRESS) && !(flags & BTRFS_INODE_NOCOMPRESS))
93d0092bddSLi Zefan 		iflags |= FS_COMPR_FL;
94d0092bddSLi Zefan 	else if (flags & BTRFS_INODE_NOCOMPRESS)
95d0092bddSLi Zefan 		iflags |= FS_NOCOMP_FL;
966cbff00fSChristoph Hellwig 
976cbff00fSChristoph Hellwig 	return iflags;
986cbff00fSChristoph Hellwig }
996cbff00fSChristoph Hellwig 
1006cbff00fSChristoph Hellwig /*
1016cbff00fSChristoph Hellwig  * Update inode->i_flags based on the btrfs internal flags.
1026cbff00fSChristoph Hellwig  */
1036cbff00fSChristoph Hellwig void btrfs_update_iflags(struct inode *inode)
1046cbff00fSChristoph Hellwig {
1056cbff00fSChristoph Hellwig 	struct btrfs_inode *ip = BTRFS_I(inode);
1066cbff00fSChristoph Hellwig 
1076cbff00fSChristoph Hellwig 	inode->i_flags &= ~(S_SYNC|S_APPEND|S_IMMUTABLE|S_NOATIME|S_DIRSYNC);
1086cbff00fSChristoph Hellwig 
1096cbff00fSChristoph Hellwig 	if (ip->flags & BTRFS_INODE_SYNC)
1106cbff00fSChristoph Hellwig 		inode->i_flags |= S_SYNC;
1116cbff00fSChristoph Hellwig 	if (ip->flags & BTRFS_INODE_IMMUTABLE)
1126cbff00fSChristoph Hellwig 		inode->i_flags |= S_IMMUTABLE;
1136cbff00fSChristoph Hellwig 	if (ip->flags & BTRFS_INODE_APPEND)
1146cbff00fSChristoph Hellwig 		inode->i_flags |= S_APPEND;
1156cbff00fSChristoph Hellwig 	if (ip->flags & BTRFS_INODE_NOATIME)
1166cbff00fSChristoph Hellwig 		inode->i_flags |= S_NOATIME;
1176cbff00fSChristoph Hellwig 	if (ip->flags & BTRFS_INODE_DIRSYNC)
1186cbff00fSChristoph Hellwig 		inode->i_flags |= S_DIRSYNC;
1196cbff00fSChristoph Hellwig }
1206cbff00fSChristoph Hellwig 
1216cbff00fSChristoph Hellwig /*
1226cbff00fSChristoph Hellwig  * Inherit flags from the parent inode.
1236cbff00fSChristoph Hellwig  *
124e27425d6SJosef Bacik  * Currently only the compression flags and the cow flags are inherited.
1256cbff00fSChristoph Hellwig  */
1266cbff00fSChristoph Hellwig void btrfs_inherit_iflags(struct inode *inode, struct inode *dir)
1276cbff00fSChristoph Hellwig {
1280b4dcea5SChris Mason 	unsigned int flags;
1290b4dcea5SChris Mason 
1300b4dcea5SChris Mason 	if (!dir)
1310b4dcea5SChris Mason 		return;
1320b4dcea5SChris Mason 
1330b4dcea5SChris Mason 	flags = BTRFS_I(dir)->flags;
1346cbff00fSChristoph Hellwig 
135e27425d6SJosef Bacik 	if (flags & BTRFS_INODE_NOCOMPRESS) {
136e27425d6SJosef Bacik 		BTRFS_I(inode)->flags &= ~BTRFS_INODE_COMPRESS;
137e27425d6SJosef Bacik 		BTRFS_I(inode)->flags |= BTRFS_INODE_NOCOMPRESS;
138e27425d6SJosef Bacik 	} else if (flags & BTRFS_INODE_COMPRESS) {
139e27425d6SJosef Bacik 		BTRFS_I(inode)->flags &= ~BTRFS_INODE_NOCOMPRESS;
140e27425d6SJosef Bacik 		BTRFS_I(inode)->flags |= BTRFS_INODE_COMPRESS;
141e27425d6SJosef Bacik 	}
1426cbff00fSChristoph Hellwig 
143e27425d6SJosef Bacik 	if (flags & BTRFS_INODE_NODATACOW)
144e27425d6SJosef Bacik 		BTRFS_I(inode)->flags |= BTRFS_INODE_NODATACOW;
145e27425d6SJosef Bacik 
1466cbff00fSChristoph Hellwig 	btrfs_update_iflags(inode);
1476cbff00fSChristoph Hellwig }
1486cbff00fSChristoph Hellwig 
1496cbff00fSChristoph Hellwig static int btrfs_ioctl_getflags(struct file *file, void __user *arg)
1506cbff00fSChristoph Hellwig {
1516cbff00fSChristoph Hellwig 	struct btrfs_inode *ip = BTRFS_I(file->f_path.dentry->d_inode);
1526cbff00fSChristoph Hellwig 	unsigned int flags = btrfs_flags_to_ioctl(ip->flags);
1536cbff00fSChristoph Hellwig 
1546cbff00fSChristoph Hellwig 	if (copy_to_user(arg, &flags, sizeof(flags)))
1556cbff00fSChristoph Hellwig 		return -EFAULT;
1566cbff00fSChristoph Hellwig 	return 0;
1576cbff00fSChristoph Hellwig }
1586cbff00fSChristoph Hellwig 
15975e7cb7fSLiu Bo static int check_flags(unsigned int flags)
16075e7cb7fSLiu Bo {
16175e7cb7fSLiu Bo 	if (flags & ~(FS_IMMUTABLE_FL | FS_APPEND_FL | \
16275e7cb7fSLiu Bo 		      FS_NOATIME_FL | FS_NODUMP_FL | \
16375e7cb7fSLiu Bo 		      FS_SYNC_FL | FS_DIRSYNC_FL | \
164e1e8fb6aSLi Zefan 		      FS_NOCOMP_FL | FS_COMPR_FL |
165e1e8fb6aSLi Zefan 		      FS_NOCOW_FL))
16675e7cb7fSLiu Bo 		return -EOPNOTSUPP;
16775e7cb7fSLiu Bo 
16875e7cb7fSLiu Bo 	if ((flags & FS_NOCOMP_FL) && (flags & FS_COMPR_FL))
16975e7cb7fSLiu Bo 		return -EINVAL;
17075e7cb7fSLiu Bo 
17175e7cb7fSLiu Bo 	return 0;
17275e7cb7fSLiu Bo }
17375e7cb7fSLiu Bo 
1746cbff00fSChristoph Hellwig static int btrfs_ioctl_setflags(struct file *file, void __user *arg)
1756cbff00fSChristoph Hellwig {
1766cbff00fSChristoph Hellwig 	struct inode *inode = file->f_path.dentry->d_inode;
1776cbff00fSChristoph Hellwig 	struct btrfs_inode *ip = BTRFS_I(inode);
1786cbff00fSChristoph Hellwig 	struct btrfs_root *root = ip->root;
1796cbff00fSChristoph Hellwig 	struct btrfs_trans_handle *trans;
1806cbff00fSChristoph Hellwig 	unsigned int flags, oldflags;
1816cbff00fSChristoph Hellwig 	int ret;
182f062abf0SLi Zefan 	u64 ip_oldflags;
183f062abf0SLi Zefan 	unsigned int i_oldflags;
1847e97b8daSDavid Sterba 	umode_t mode;
1856cbff00fSChristoph Hellwig 
186b83cc969SLi Zefan 	if (btrfs_root_readonly(root))
187b83cc969SLi Zefan 		return -EROFS;
188b83cc969SLi Zefan 
1896cbff00fSChristoph Hellwig 	if (copy_from_user(&flags, arg, sizeof(flags)))
1906cbff00fSChristoph Hellwig 		return -EFAULT;
1916cbff00fSChristoph Hellwig 
19275e7cb7fSLiu Bo 	ret = check_flags(flags);
19375e7cb7fSLiu Bo 	if (ret)
19475e7cb7fSLiu Bo 		return ret;
1956cbff00fSChristoph Hellwig 
1962e149670SSerge E. Hallyn 	if (!inode_owner_or_capable(inode))
1976cbff00fSChristoph Hellwig 		return -EACCES;
1986cbff00fSChristoph Hellwig 
199e7848683SJan Kara 	ret = mnt_want_write_file(file);
200e7848683SJan Kara 	if (ret)
201e7848683SJan Kara 		return ret;
202e7848683SJan Kara 
2036cbff00fSChristoph Hellwig 	mutex_lock(&inode->i_mutex);
2046cbff00fSChristoph Hellwig 
205f062abf0SLi Zefan 	ip_oldflags = ip->flags;
206f062abf0SLi Zefan 	i_oldflags = inode->i_flags;
2077e97b8daSDavid Sterba 	mode = inode->i_mode;
208f062abf0SLi Zefan 
2096cbff00fSChristoph Hellwig 	flags = btrfs_mask_flags(inode->i_mode, flags);
2106cbff00fSChristoph Hellwig 	oldflags = btrfs_flags_to_ioctl(ip->flags);
2116cbff00fSChristoph Hellwig 	if ((flags ^ oldflags) & (FS_APPEND_FL | FS_IMMUTABLE_FL)) {
2126cbff00fSChristoph Hellwig 		if (!capable(CAP_LINUX_IMMUTABLE)) {
2136cbff00fSChristoph Hellwig 			ret = -EPERM;
2146cbff00fSChristoph Hellwig 			goto out_unlock;
2156cbff00fSChristoph Hellwig 		}
2166cbff00fSChristoph Hellwig 	}
2176cbff00fSChristoph Hellwig 
2186cbff00fSChristoph Hellwig 	if (flags & FS_SYNC_FL)
2196cbff00fSChristoph Hellwig 		ip->flags |= BTRFS_INODE_SYNC;
2206cbff00fSChristoph Hellwig 	else
2216cbff00fSChristoph Hellwig 		ip->flags &= ~BTRFS_INODE_SYNC;
2226cbff00fSChristoph Hellwig 	if (flags & FS_IMMUTABLE_FL)
2236cbff00fSChristoph Hellwig 		ip->flags |= BTRFS_INODE_IMMUTABLE;
2246cbff00fSChristoph Hellwig 	else
2256cbff00fSChristoph Hellwig 		ip->flags &= ~BTRFS_INODE_IMMUTABLE;
2266cbff00fSChristoph Hellwig 	if (flags & FS_APPEND_FL)
2276cbff00fSChristoph Hellwig 		ip->flags |= BTRFS_INODE_APPEND;
2286cbff00fSChristoph Hellwig 	else
2296cbff00fSChristoph Hellwig 		ip->flags &= ~BTRFS_INODE_APPEND;
2306cbff00fSChristoph Hellwig 	if (flags & FS_NODUMP_FL)
2316cbff00fSChristoph Hellwig 		ip->flags |= BTRFS_INODE_NODUMP;
2326cbff00fSChristoph Hellwig 	else
2336cbff00fSChristoph Hellwig 		ip->flags &= ~BTRFS_INODE_NODUMP;
2346cbff00fSChristoph Hellwig 	if (flags & FS_NOATIME_FL)
2356cbff00fSChristoph Hellwig 		ip->flags |= BTRFS_INODE_NOATIME;
2366cbff00fSChristoph Hellwig 	else
2376cbff00fSChristoph Hellwig 		ip->flags &= ~BTRFS_INODE_NOATIME;
2386cbff00fSChristoph Hellwig 	if (flags & FS_DIRSYNC_FL)
2396cbff00fSChristoph Hellwig 		ip->flags |= BTRFS_INODE_DIRSYNC;
2406cbff00fSChristoph Hellwig 	else
2416cbff00fSChristoph Hellwig 		ip->flags &= ~BTRFS_INODE_DIRSYNC;
2427e97b8daSDavid Sterba 	if (flags & FS_NOCOW_FL) {
2437e97b8daSDavid Sterba 		if (S_ISREG(mode)) {
2447e97b8daSDavid Sterba 			/*
2457e97b8daSDavid Sterba 			 * It's safe to turn csums off here, no extents exist.
2467e97b8daSDavid Sterba 			 * Otherwise we want the flag to reflect the real COW
2477e97b8daSDavid Sterba 			 * status of the file and will not set it.
2487e97b8daSDavid Sterba 			 */
2497e97b8daSDavid Sterba 			if (inode->i_size == 0)
2507e97b8daSDavid Sterba 				ip->flags |= BTRFS_INODE_NODATACOW
2517e97b8daSDavid Sterba 					   | BTRFS_INODE_NODATASUM;
2527e97b8daSDavid Sterba 		} else {
253e1e8fb6aSLi Zefan 			ip->flags |= BTRFS_INODE_NODATACOW;
2547e97b8daSDavid Sterba 		}
2557e97b8daSDavid Sterba 	} else {
2567e97b8daSDavid Sterba 		/*
2577e97b8daSDavid Sterba 		 * Revert back under same assuptions as above
2587e97b8daSDavid Sterba 		 */
2597e97b8daSDavid Sterba 		if (S_ISREG(mode)) {
2607e97b8daSDavid Sterba 			if (inode->i_size == 0)
2617e97b8daSDavid Sterba 				ip->flags &= ~(BTRFS_INODE_NODATACOW
2627e97b8daSDavid Sterba 				             | BTRFS_INODE_NODATASUM);
2637e97b8daSDavid Sterba 		} else {
264e1e8fb6aSLi Zefan 			ip->flags &= ~BTRFS_INODE_NODATACOW;
2657e97b8daSDavid Sterba 		}
2667e97b8daSDavid Sterba 	}
2676cbff00fSChristoph Hellwig 
26875e7cb7fSLiu Bo 	/*
26975e7cb7fSLiu Bo 	 * The COMPRESS flag can only be changed by users, while the NOCOMPRESS
27075e7cb7fSLiu Bo 	 * flag may be changed automatically if compression code won't make
27175e7cb7fSLiu Bo 	 * things smaller.
27275e7cb7fSLiu Bo 	 */
27375e7cb7fSLiu Bo 	if (flags & FS_NOCOMP_FL) {
27475e7cb7fSLiu Bo 		ip->flags &= ~BTRFS_INODE_COMPRESS;
27575e7cb7fSLiu Bo 		ip->flags |= BTRFS_INODE_NOCOMPRESS;
27675e7cb7fSLiu Bo 	} else if (flags & FS_COMPR_FL) {
27775e7cb7fSLiu Bo 		ip->flags |= BTRFS_INODE_COMPRESS;
27875e7cb7fSLiu Bo 		ip->flags &= ~BTRFS_INODE_NOCOMPRESS;
279ebcb904dSLi Zefan 	} else {
280ebcb904dSLi Zefan 		ip->flags &= ~(BTRFS_INODE_COMPRESS | BTRFS_INODE_NOCOMPRESS);
28175e7cb7fSLiu Bo 	}
2826cbff00fSChristoph Hellwig 
2834da6f1a3SLi Zefan 	trans = btrfs_start_transaction(root, 1);
284f062abf0SLi Zefan 	if (IS_ERR(trans)) {
285f062abf0SLi Zefan 		ret = PTR_ERR(trans);
286f062abf0SLi Zefan 		goto out_drop;
287f062abf0SLi Zefan 	}
2886cbff00fSChristoph Hellwig 
289306424ccSLi Zefan 	btrfs_update_iflags(inode);
2900c4d2d95SJosef Bacik 	inode_inc_iversion(inode);
291306424ccSLi Zefan 	inode->i_ctime = CURRENT_TIME;
2926cbff00fSChristoph Hellwig 	ret = btrfs_update_inode(trans, root, inode);
2936cbff00fSChristoph Hellwig 
2946cbff00fSChristoph Hellwig 	btrfs_end_transaction(trans, root);
295f062abf0SLi Zefan  out_drop:
296f062abf0SLi Zefan 	if (ret) {
297f062abf0SLi Zefan 		ip->flags = ip_oldflags;
298f062abf0SLi Zefan 		inode->i_flags = i_oldflags;
299f062abf0SLi Zefan 	}
3006cbff00fSChristoph Hellwig 
3016cbff00fSChristoph Hellwig  out_unlock:
3026cbff00fSChristoph Hellwig 	mutex_unlock(&inode->i_mutex);
303e7848683SJan Kara 	mnt_drop_write_file(file);
3042d4e6f6aSliubo 	return ret;
3056cbff00fSChristoph Hellwig }
3066cbff00fSChristoph Hellwig 
3076cbff00fSChristoph Hellwig static int btrfs_ioctl_getversion(struct file *file, int __user *arg)
3086cbff00fSChristoph Hellwig {
3096cbff00fSChristoph Hellwig 	struct inode *inode = file->f_path.dentry->d_inode;
3106cbff00fSChristoph Hellwig 
3116cbff00fSChristoph Hellwig 	return put_user(inode->i_generation, arg);
3126cbff00fSChristoph Hellwig }
313f46b5a66SChristoph Hellwig 
314f7039b1dSLi Dongyang static noinline int btrfs_ioctl_fitrim(struct file *file, void __user *arg)
315f7039b1dSLi Dongyang {
316815745cfSAl Viro 	struct btrfs_fs_info *fs_info = btrfs_sb(fdentry(file)->d_sb);
317f7039b1dSLi Dongyang 	struct btrfs_device *device;
318f7039b1dSLi Dongyang 	struct request_queue *q;
319f7039b1dSLi Dongyang 	struct fstrim_range range;
320f7039b1dSLi Dongyang 	u64 minlen = ULLONG_MAX;
321f7039b1dSLi Dongyang 	u64 num_devices = 0;
322815745cfSAl Viro 	u64 total_bytes = btrfs_super_total_bytes(fs_info->super_copy);
323f7039b1dSLi Dongyang 	int ret;
324f7039b1dSLi Dongyang 
325f7039b1dSLi Dongyang 	if (!capable(CAP_SYS_ADMIN))
326f7039b1dSLi Dongyang 		return -EPERM;
327f7039b1dSLi Dongyang 
3281f78160cSXiao Guangrong 	rcu_read_lock();
3291f78160cSXiao Guangrong 	list_for_each_entry_rcu(device, &fs_info->fs_devices->devices,
3301f78160cSXiao Guangrong 				dev_list) {
331f7039b1dSLi Dongyang 		if (!device->bdev)
332f7039b1dSLi Dongyang 			continue;
333f7039b1dSLi Dongyang 		q = bdev_get_queue(device->bdev);
334f7039b1dSLi Dongyang 		if (blk_queue_discard(q)) {
335f7039b1dSLi Dongyang 			num_devices++;
336f7039b1dSLi Dongyang 			minlen = min((u64)q->limits.discard_granularity,
337f7039b1dSLi Dongyang 				     minlen);
338f7039b1dSLi Dongyang 		}
339f7039b1dSLi Dongyang 	}
3401f78160cSXiao Guangrong 	rcu_read_unlock();
341f4c697e6SLukas Czerner 
342f7039b1dSLi Dongyang 	if (!num_devices)
343f7039b1dSLi Dongyang 		return -EOPNOTSUPP;
344f7039b1dSLi Dongyang 	if (copy_from_user(&range, arg, sizeof(range)))
345f7039b1dSLi Dongyang 		return -EFAULT;
346e515c18bSLukas Czerner 	if (range.start > total_bytes ||
347e515c18bSLukas Czerner 	    range.len < fs_info->sb->s_blocksize)
348f4c697e6SLukas Czerner 		return -EINVAL;
349f7039b1dSLi Dongyang 
350f4c697e6SLukas Czerner 	range.len = min(range.len, total_bytes - range.start);
351f7039b1dSLi Dongyang 	range.minlen = max(range.minlen, minlen);
352815745cfSAl Viro 	ret = btrfs_trim_fs(fs_info->tree_root, &range);
353f7039b1dSLi Dongyang 	if (ret < 0)
354f7039b1dSLi Dongyang 		return ret;
355f7039b1dSLi Dongyang 
356f7039b1dSLi Dongyang 	if (copy_to_user(arg, &range, sizeof(range)))
357f7039b1dSLi Dongyang 		return -EFAULT;
358f7039b1dSLi Dongyang 
359f7039b1dSLi Dongyang 	return 0;
360f7039b1dSLi Dongyang }
361f7039b1dSLi Dongyang 
362cb8e7090SChristoph Hellwig static noinline int create_subvol(struct btrfs_root *root,
363cb8e7090SChristoph Hellwig 				  struct dentry *dentry,
36472fd032eSSage Weil 				  char *name, int namelen,
3656f72c7e2SArne Jansen 				  u64 *async_transid,
3666f72c7e2SArne Jansen 				  struct btrfs_qgroup_inherit **inherit)
367f46b5a66SChristoph Hellwig {
368f46b5a66SChristoph Hellwig 	struct btrfs_trans_handle *trans;
369f46b5a66SChristoph Hellwig 	struct btrfs_key key;
370f46b5a66SChristoph Hellwig 	struct btrfs_root_item root_item;
371f46b5a66SChristoph Hellwig 	struct btrfs_inode_item *inode_item;
372f46b5a66SChristoph Hellwig 	struct extent_buffer *leaf;
37376dda93cSYan, Zheng 	struct btrfs_root *new_root;
3742fbe8c8aSAl Viro 	struct dentry *parent = dentry->d_parent;
3756a912213SJosef Bacik 	struct inode *dir;
3768ea05e3aSAlexander Block 	struct timespec cur_time = CURRENT_TIME;
377f46b5a66SChristoph Hellwig 	int ret;
378f46b5a66SChristoph Hellwig 	int err;
379f46b5a66SChristoph Hellwig 	u64 objectid;
380f46b5a66SChristoph Hellwig 	u64 new_dirid = BTRFS_FIRST_FREE_OBJECTID;
3813de4586cSChris Mason 	u64 index = 0;
3828ea05e3aSAlexander Block 	uuid_le new_uuid;
383f46b5a66SChristoph Hellwig 
384581bb050SLi Zefan 	ret = btrfs_find_free_objectid(root->fs_info->tree_root, &objectid);
3852fbe8c8aSAl Viro 	if (ret)
386a22285a6SYan, Zheng 		return ret;
3876a912213SJosef Bacik 
3886a912213SJosef Bacik 	dir = parent->d_inode;
3896a912213SJosef Bacik 
3909ed74f2dSJosef Bacik 	/*
3919ed74f2dSJosef Bacik 	 * 1 - inode item
3929ed74f2dSJosef Bacik 	 * 2 - refs
3939ed74f2dSJosef Bacik 	 * 1 - root item
3949ed74f2dSJosef Bacik 	 * 2 - dir items
3959ed74f2dSJosef Bacik 	 */
396a22285a6SYan, Zheng 	trans = btrfs_start_transaction(root, 6);
3972fbe8c8aSAl Viro 	if (IS_ERR(trans))
398a22285a6SYan, Zheng 		return PTR_ERR(trans);
399f46b5a66SChristoph Hellwig 
4006f72c7e2SArne Jansen 	ret = btrfs_qgroup_inherit(trans, root->fs_info, 0, objectid,
4016f72c7e2SArne Jansen 				   inherit ? *inherit : NULL);
4026f72c7e2SArne Jansen 	if (ret)
4036f72c7e2SArne Jansen 		goto fail;
4046f72c7e2SArne Jansen 
4055d4f98a2SYan Zheng 	leaf = btrfs_alloc_free_block(trans, root, root->leafsize,
4065581a51aSJan Schmidt 				      0, objectid, NULL, 0, 0, 0);
4078e8a1e31SJosef Bacik 	if (IS_ERR(leaf)) {
4088e8a1e31SJosef Bacik 		ret = PTR_ERR(leaf);
4098e8a1e31SJosef Bacik 		goto fail;
4108e8a1e31SJosef Bacik 	}
411f46b5a66SChristoph Hellwig 
4125d4f98a2SYan Zheng 	memset_extent_buffer(leaf, 0, 0, sizeof(struct btrfs_header));
413f46b5a66SChristoph Hellwig 	btrfs_set_header_bytenr(leaf, leaf->start);
414f46b5a66SChristoph Hellwig 	btrfs_set_header_generation(leaf, trans->transid);
4155d4f98a2SYan Zheng 	btrfs_set_header_backref_rev(leaf, BTRFS_MIXED_BACKREF_REV);
416f46b5a66SChristoph Hellwig 	btrfs_set_header_owner(leaf, objectid);
417f46b5a66SChristoph Hellwig 
418f46b5a66SChristoph Hellwig 	write_extent_buffer(leaf, root->fs_info->fsid,
419f46b5a66SChristoph Hellwig 			    (unsigned long)btrfs_header_fsid(leaf),
420f46b5a66SChristoph Hellwig 			    BTRFS_FSID_SIZE);
4215d4f98a2SYan Zheng 	write_extent_buffer(leaf, root->fs_info->chunk_tree_uuid,
4225d4f98a2SYan Zheng 			    (unsigned long)btrfs_header_chunk_tree_uuid(leaf),
4235d4f98a2SYan Zheng 			    BTRFS_UUID_SIZE);
424f46b5a66SChristoph Hellwig 	btrfs_mark_buffer_dirty(leaf);
425f46b5a66SChristoph Hellwig 
4268ea05e3aSAlexander Block 	memset(&root_item, 0, sizeof(root_item));
4278ea05e3aSAlexander Block 
428f46b5a66SChristoph Hellwig 	inode_item = &root_item.inode;
429f46b5a66SChristoph Hellwig 	inode_item->generation = cpu_to_le64(1);
430f46b5a66SChristoph Hellwig 	inode_item->size = cpu_to_le64(3);
431f46b5a66SChristoph Hellwig 	inode_item->nlink = cpu_to_le32(1);
432a76a3cd4SYan Zheng 	inode_item->nbytes = cpu_to_le64(root->leafsize);
433f46b5a66SChristoph Hellwig 	inode_item->mode = cpu_to_le32(S_IFDIR | 0755);
434f46b5a66SChristoph Hellwig 
43508fe4db1SLi Zefan 	root_item.flags = 0;
43608fe4db1SLi Zefan 	root_item.byte_limit = 0;
43708fe4db1SLi Zefan 	inode_item->flags = cpu_to_le64(BTRFS_INODE_ROOT_ITEM_INIT);
43808fe4db1SLi Zefan 
439f46b5a66SChristoph Hellwig 	btrfs_set_root_bytenr(&root_item, leaf->start);
44084234f3aSYan Zheng 	btrfs_set_root_generation(&root_item, trans->transid);
441f46b5a66SChristoph Hellwig 	btrfs_set_root_level(&root_item, 0);
442f46b5a66SChristoph Hellwig 	btrfs_set_root_refs(&root_item, 1);
44386b9f2ecSYan, Zheng 	btrfs_set_root_used(&root_item, leaf->len);
44480ff3856SYan Zheng 	btrfs_set_root_last_snapshot(&root_item, 0);
445f46b5a66SChristoph Hellwig 
4468ea05e3aSAlexander Block 	btrfs_set_root_generation_v2(&root_item,
4478ea05e3aSAlexander Block 			btrfs_root_generation(&root_item));
4488ea05e3aSAlexander Block 	uuid_le_gen(&new_uuid);
4498ea05e3aSAlexander Block 	memcpy(root_item.uuid, new_uuid.b, BTRFS_UUID_SIZE);
4508ea05e3aSAlexander Block 	root_item.otime.sec = cpu_to_le64(cur_time.tv_sec);
451dadd1105SDan Carpenter 	root_item.otime.nsec = cpu_to_le32(cur_time.tv_nsec);
4528ea05e3aSAlexander Block 	root_item.ctime = root_item.otime;
4538ea05e3aSAlexander Block 	btrfs_set_root_ctransid(&root_item, trans->transid);
4548ea05e3aSAlexander Block 	btrfs_set_root_otransid(&root_item, trans->transid);
455f46b5a66SChristoph Hellwig 
456925baeddSChris Mason 	btrfs_tree_unlock(leaf);
457f46b5a66SChristoph Hellwig 	free_extent_buffer(leaf);
458f46b5a66SChristoph Hellwig 	leaf = NULL;
459f46b5a66SChristoph Hellwig 
460f46b5a66SChristoph Hellwig 	btrfs_set_root_dirid(&root_item, new_dirid);
461f46b5a66SChristoph Hellwig 
462f46b5a66SChristoph Hellwig 	key.objectid = objectid;
4635d4f98a2SYan Zheng 	key.offset = 0;
464f46b5a66SChristoph Hellwig 	btrfs_set_key_type(&key, BTRFS_ROOT_ITEM_KEY);
465f46b5a66SChristoph Hellwig 	ret = btrfs_insert_root(trans, root->fs_info->tree_root, &key,
466f46b5a66SChristoph Hellwig 				&root_item);
467f46b5a66SChristoph Hellwig 	if (ret)
468f46b5a66SChristoph Hellwig 		goto fail;
469f46b5a66SChristoph Hellwig 
47076dda93cSYan, Zheng 	key.offset = (u64)-1;
47176dda93cSYan, Zheng 	new_root = btrfs_read_fs_root_no_name(root->fs_info, &key);
47279787eaaSJeff Mahoney 	if (IS_ERR(new_root)) {
47379787eaaSJeff Mahoney 		btrfs_abort_transaction(trans, root, PTR_ERR(new_root));
47479787eaaSJeff Mahoney 		ret = PTR_ERR(new_root);
47579787eaaSJeff Mahoney 		goto fail;
47679787eaaSJeff Mahoney 	}
47776dda93cSYan, Zheng 
47876dda93cSYan, Zheng 	btrfs_record_root_in_trans(trans, new_root);
47976dda93cSYan, Zheng 
480d82a6f1dSJosef Bacik 	ret = btrfs_create_subvol_root(trans, new_root, new_dirid);
481ce598979SMark Fasheh 	if (ret) {
482ce598979SMark Fasheh 		/* We potentially lose an unused inode item here */
48379787eaaSJeff Mahoney 		btrfs_abort_transaction(trans, root, ret);
484ce598979SMark Fasheh 		goto fail;
485ce598979SMark Fasheh 	}
486ce598979SMark Fasheh 
487f46b5a66SChristoph Hellwig 	/*
488f46b5a66SChristoph Hellwig 	 * insert the directory item
489f46b5a66SChristoph Hellwig 	 */
4903de4586cSChris Mason 	ret = btrfs_set_inode_index(dir, &index);
49179787eaaSJeff Mahoney 	if (ret) {
49279787eaaSJeff Mahoney 		btrfs_abort_transaction(trans, root, ret);
49379787eaaSJeff Mahoney 		goto fail;
49479787eaaSJeff Mahoney 	}
4953de4586cSChris Mason 
4963de4586cSChris Mason 	ret = btrfs_insert_dir_item(trans, root,
49716cdcec7SMiao Xie 				    name, namelen, dir, &key,
4983de4586cSChris Mason 				    BTRFS_FT_DIR, index);
49979787eaaSJeff Mahoney 	if (ret) {
50079787eaaSJeff Mahoney 		btrfs_abort_transaction(trans, root, ret);
501f46b5a66SChristoph Hellwig 		goto fail;
50279787eaaSJeff Mahoney 	}
5030660b5afSChris Mason 
50452c26179SYan Zheng 	btrfs_i_size_write(dir, dir->i_size + namelen * 2);
50552c26179SYan Zheng 	ret = btrfs_update_inode(trans, root, dir);
50652c26179SYan Zheng 	BUG_ON(ret);
50752c26179SYan Zheng 
5080660b5afSChris Mason 	ret = btrfs_add_root_ref(trans, root->fs_info->tree_root,
5094df27c4dSYan, Zheng 				 objectid, root->root_key.objectid,
51033345d01SLi Zefan 				 btrfs_ino(dir), index, name, namelen);
51176dda93cSYan, Zheng 
5120660b5afSChris Mason 	BUG_ON(ret);
5130660b5afSChris Mason 
51476dda93cSYan, Zheng 	d_instantiate(dentry, btrfs_lookup_dentry(dir, dentry));
515f46b5a66SChristoph Hellwig fail:
51672fd032eSSage Weil 	if (async_transid) {
51772fd032eSSage Weil 		*async_transid = trans->transid;
51872fd032eSSage Weil 		err = btrfs_commit_transaction_async(trans, root, 1);
51972fd032eSSage Weil 	} else {
52076dda93cSYan, Zheng 		err = btrfs_commit_transaction(trans, root);
52172fd032eSSage Weil 	}
522f46b5a66SChristoph Hellwig 	if (err && !ret)
523f46b5a66SChristoph Hellwig 		ret = err;
524f46b5a66SChristoph Hellwig 	return ret;
525f46b5a66SChristoph Hellwig }
526f46b5a66SChristoph Hellwig 
52772fd032eSSage Weil static int create_snapshot(struct btrfs_root *root, struct dentry *dentry,
528b83cc969SLi Zefan 			   char *name, int namelen, u64 *async_transid,
5296f72c7e2SArne Jansen 			   bool readonly, struct btrfs_qgroup_inherit **inherit)
530f46b5a66SChristoph Hellwig {
5312e4bfab9SYan, Zheng 	struct inode *inode;
532f46b5a66SChristoph Hellwig 	struct btrfs_pending_snapshot *pending_snapshot;
533f46b5a66SChristoph Hellwig 	struct btrfs_trans_handle *trans;
5342e4bfab9SYan, Zheng 	int ret;
535f46b5a66SChristoph Hellwig 
536f46b5a66SChristoph Hellwig 	if (!root->ref_cows)
537f46b5a66SChristoph Hellwig 		return -EINVAL;
538f46b5a66SChristoph Hellwig 
539a22285a6SYan, Zheng 	pending_snapshot = kzalloc(sizeof(*pending_snapshot), GFP_NOFS);
540a22285a6SYan, Zheng 	if (!pending_snapshot)
541a22285a6SYan, Zheng 		return -ENOMEM;
542a22285a6SYan, Zheng 
54366d8f3ddSMiao Xie 	btrfs_init_block_rsv(&pending_snapshot->block_rsv,
54466d8f3ddSMiao Xie 			     BTRFS_BLOCK_RSV_TEMP);
545a22285a6SYan, Zheng 	pending_snapshot->dentry = dentry;
546a22285a6SYan, Zheng 	pending_snapshot->root = root;
547b83cc969SLi Zefan 	pending_snapshot->readonly = readonly;
5486f72c7e2SArne Jansen 	if (inherit) {
5496f72c7e2SArne Jansen 		pending_snapshot->inherit = *inherit;
5506f72c7e2SArne Jansen 		*inherit = NULL;	/* take responsibility to free it */
5516f72c7e2SArne Jansen 	}
552a22285a6SYan, Zheng 
55348c03c4bSMiao Xie 	trans = btrfs_start_transaction(root->fs_info->extent_root, 6);
554a22285a6SYan, Zheng 	if (IS_ERR(trans)) {
555a22285a6SYan, Zheng 		ret = PTR_ERR(trans);
556a22285a6SYan, Zheng 		goto fail;
557a22285a6SYan, Zheng 	}
558a22285a6SYan, Zheng 
559a22285a6SYan, Zheng 	ret = btrfs_snap_reserve_metadata(trans, pending_snapshot);
560a22285a6SYan, Zheng 	BUG_ON(ret);
561a22285a6SYan, Zheng 
5628351583eSJosef Bacik 	spin_lock(&root->fs_info->trans_lock);
563a22285a6SYan, Zheng 	list_add(&pending_snapshot->list,
564a22285a6SYan, Zheng 		 &trans->transaction->pending_snapshots);
5658351583eSJosef Bacik 	spin_unlock(&root->fs_info->trans_lock);
56672fd032eSSage Weil 	if (async_transid) {
56772fd032eSSage Weil 		*async_transid = trans->transid;
56872fd032eSSage Weil 		ret = btrfs_commit_transaction_async(trans,
56972fd032eSSage Weil 				     root->fs_info->extent_root, 1);
57072fd032eSSage Weil 	} else {
57172fd032eSSage Weil 		ret = btrfs_commit_transaction(trans,
57272fd032eSSage Weil 					       root->fs_info->extent_root);
57372fd032eSSage Weil 	}
574109f2365SLiu Bo 	if (ret) {
575109f2365SLiu Bo 		/* cleanup_transaction has freed this for us */
576109f2365SLiu Bo 		if (trans->aborted)
577109f2365SLiu Bo 			pending_snapshot = NULL;
578c37b2b62SJosef Bacik 		goto fail;
579109f2365SLiu Bo 	}
580a22285a6SYan, Zheng 
581a22285a6SYan, Zheng 	ret = pending_snapshot->error;
582f46b5a66SChristoph Hellwig 	if (ret)
5832e4bfab9SYan, Zheng 		goto fail;
584f46b5a66SChristoph Hellwig 
58566b4ffd1SJosef Bacik 	ret = btrfs_orphan_cleanup(pending_snapshot->snap);
58666b4ffd1SJosef Bacik 	if (ret)
58766b4ffd1SJosef Bacik 		goto fail;
588f46b5a66SChristoph Hellwig 
5892fbe8c8aSAl Viro 	inode = btrfs_lookup_dentry(dentry->d_parent->d_inode, dentry);
5902e4bfab9SYan, Zheng 	if (IS_ERR(inode)) {
5912e4bfab9SYan, Zheng 		ret = PTR_ERR(inode);
5922e4bfab9SYan, Zheng 		goto fail;
5932e4bfab9SYan, Zheng 	}
5942e4bfab9SYan, Zheng 	BUG_ON(!inode);
5952e4bfab9SYan, Zheng 	d_instantiate(dentry, inode);
5962e4bfab9SYan, Zheng 	ret = 0;
5972e4bfab9SYan, Zheng fail:
598a22285a6SYan, Zheng 	kfree(pending_snapshot);
599f46b5a66SChristoph Hellwig 	return ret;
600f46b5a66SChristoph Hellwig }
601f46b5a66SChristoph Hellwig 
6024260f7c7SSage Weil /*  copy of check_sticky in fs/namei.c()
6034260f7c7SSage Weil * It's inline, so penalty for filesystems that don't use sticky bit is
6044260f7c7SSage Weil * minimal.
6054260f7c7SSage Weil */
6064260f7c7SSage Weil static inline int btrfs_check_sticky(struct inode *dir, struct inode *inode)
6074260f7c7SSage Weil {
6082f2f43d3SEric W. Biederman 	kuid_t fsuid = current_fsuid();
6094260f7c7SSage Weil 
6104260f7c7SSage Weil 	if (!(dir->i_mode & S_ISVTX))
6114260f7c7SSage Weil 		return 0;
6122f2f43d3SEric W. Biederman 	if (uid_eq(inode->i_uid, fsuid))
6134260f7c7SSage Weil 		return 0;
6142f2f43d3SEric W. Biederman 	if (uid_eq(dir->i_uid, fsuid))
6154260f7c7SSage Weil 		return 0;
6164260f7c7SSage Weil 	return !capable(CAP_FOWNER);
6174260f7c7SSage Weil }
6184260f7c7SSage Weil 
6194260f7c7SSage Weil /*  copy of may_delete in fs/namei.c()
6204260f7c7SSage Weil  *	Check whether we can remove a link victim from directory dir, check
6214260f7c7SSage Weil  *  whether the type of victim is right.
6224260f7c7SSage Weil  *  1. We can't do it if dir is read-only (done in permission())
6234260f7c7SSage Weil  *  2. We should have write and exec permissions on dir
6244260f7c7SSage Weil  *  3. We can't remove anything from append-only dir
6254260f7c7SSage Weil  *  4. We can't do anything with immutable dir (done in permission())
6264260f7c7SSage Weil  *  5. If the sticky bit on dir is set we should either
6274260f7c7SSage Weil  *	a. be owner of dir, or
6284260f7c7SSage Weil  *	b. be owner of victim, or
6294260f7c7SSage Weil  *	c. have CAP_FOWNER capability
6304260f7c7SSage Weil  *  6. If the victim is append-only or immutable we can't do antyhing with
6314260f7c7SSage Weil  *     links pointing to it.
6324260f7c7SSage Weil  *  7. If we were asked to remove a directory and victim isn't one - ENOTDIR.
6334260f7c7SSage Weil  *  8. If we were asked to remove a non-directory and victim isn't one - EISDIR.
6344260f7c7SSage Weil  *  9. We can't remove a root or mountpoint.
6354260f7c7SSage Weil  * 10. We don't allow removal of NFS sillyrenamed files; it's handled by
6364260f7c7SSage Weil  *     nfs_async_unlink().
6374260f7c7SSage Weil  */
6384260f7c7SSage Weil 
6394260f7c7SSage Weil static int btrfs_may_delete(struct inode *dir,struct dentry *victim,int isdir)
6404260f7c7SSage Weil {
6414260f7c7SSage Weil 	int error;
6424260f7c7SSage Weil 
6434260f7c7SSage Weil 	if (!victim->d_inode)
6444260f7c7SSage Weil 		return -ENOENT;
6454260f7c7SSage Weil 
6464260f7c7SSage Weil 	BUG_ON(victim->d_parent->d_inode != dir);
6474fa6b5ecSJeff Layton 	audit_inode_child(dir, victim, AUDIT_TYPE_CHILD_DELETE);
6484260f7c7SSage Weil 
6494260f7c7SSage Weil 	error = inode_permission(dir, MAY_WRITE | MAY_EXEC);
6504260f7c7SSage Weil 	if (error)
6514260f7c7SSage Weil 		return error;
6524260f7c7SSage Weil 	if (IS_APPEND(dir))
6534260f7c7SSage Weil 		return -EPERM;
6544260f7c7SSage Weil 	if (btrfs_check_sticky(dir, victim->d_inode)||
6554260f7c7SSage Weil 		IS_APPEND(victim->d_inode)||
6564260f7c7SSage Weil 	    IS_IMMUTABLE(victim->d_inode) || IS_SWAPFILE(victim->d_inode))
6574260f7c7SSage Weil 		return -EPERM;
6584260f7c7SSage Weil 	if (isdir) {
6594260f7c7SSage Weil 		if (!S_ISDIR(victim->d_inode->i_mode))
6604260f7c7SSage Weil 			return -ENOTDIR;
6614260f7c7SSage Weil 		if (IS_ROOT(victim))
6624260f7c7SSage Weil 			return -EBUSY;
6634260f7c7SSage Weil 	} else if (S_ISDIR(victim->d_inode->i_mode))
6644260f7c7SSage Weil 		return -EISDIR;
6654260f7c7SSage Weil 	if (IS_DEADDIR(dir))
6664260f7c7SSage Weil 		return -ENOENT;
6674260f7c7SSage Weil 	if (victim->d_flags & DCACHE_NFSFS_RENAMED)
6684260f7c7SSage Weil 		return -EBUSY;
6694260f7c7SSage Weil 	return 0;
6704260f7c7SSage Weil }
6714260f7c7SSage Weil 
672cb8e7090SChristoph Hellwig /* copy of may_create in fs/namei.c() */
673cb8e7090SChristoph Hellwig static inline int btrfs_may_create(struct inode *dir, struct dentry *child)
674cb8e7090SChristoph Hellwig {
675cb8e7090SChristoph Hellwig 	if (child->d_inode)
676cb8e7090SChristoph Hellwig 		return -EEXIST;
677cb8e7090SChristoph Hellwig 	if (IS_DEADDIR(dir))
678cb8e7090SChristoph Hellwig 		return -ENOENT;
679cb8e7090SChristoph Hellwig 	return inode_permission(dir, MAY_WRITE | MAY_EXEC);
680cb8e7090SChristoph Hellwig }
681cb8e7090SChristoph Hellwig 
682cb8e7090SChristoph Hellwig /*
683cb8e7090SChristoph Hellwig  * Create a new subvolume below @parent.  This is largely modeled after
684cb8e7090SChristoph Hellwig  * sys_mkdirat and vfs_mkdir, but we only do a single component lookup
685cb8e7090SChristoph Hellwig  * inside this filesystem so it's quite a bit simpler.
686cb8e7090SChristoph Hellwig  */
68776dda93cSYan, Zheng static noinline int btrfs_mksubvol(struct path *parent,
68876dda93cSYan, Zheng 				   char *name, int namelen,
68972fd032eSSage Weil 				   struct btrfs_root *snap_src,
6906f72c7e2SArne Jansen 				   u64 *async_transid, bool readonly,
6916f72c7e2SArne Jansen 				   struct btrfs_qgroup_inherit **inherit)
692cb8e7090SChristoph Hellwig {
69376dda93cSYan, Zheng 	struct inode *dir  = parent->dentry->d_inode;
694cb8e7090SChristoph Hellwig 	struct dentry *dentry;
695cb8e7090SChristoph Hellwig 	int error;
696cb8e7090SChristoph Hellwig 
69776dda93cSYan, Zheng 	mutex_lock_nested(&dir->i_mutex, I_MUTEX_PARENT);
698cb8e7090SChristoph Hellwig 
699cb8e7090SChristoph Hellwig 	dentry = lookup_one_len(name, parent->dentry, namelen);
700cb8e7090SChristoph Hellwig 	error = PTR_ERR(dentry);
701cb8e7090SChristoph Hellwig 	if (IS_ERR(dentry))
702cb8e7090SChristoph Hellwig 		goto out_unlock;
703cb8e7090SChristoph Hellwig 
704cb8e7090SChristoph Hellwig 	error = -EEXIST;
705cb8e7090SChristoph Hellwig 	if (dentry->d_inode)
706cb8e7090SChristoph Hellwig 		goto out_dput;
707cb8e7090SChristoph Hellwig 
70876dda93cSYan, Zheng 	error = btrfs_may_create(dir, dentry);
709cb8e7090SChristoph Hellwig 	if (error)
710a874a63eSLiu Bo 		goto out_dput;
711cb8e7090SChristoph Hellwig 
71276dda93cSYan, Zheng 	down_read(&BTRFS_I(dir)->root->fs_info->subvol_sem);
71376dda93cSYan, Zheng 
71476dda93cSYan, Zheng 	if (btrfs_root_refs(&BTRFS_I(dir)->root->root_item) == 0)
71576dda93cSYan, Zheng 		goto out_up_read;
71676dda93cSYan, Zheng 
7173de4586cSChris Mason 	if (snap_src) {
7186f72c7e2SArne Jansen 		error = create_snapshot(snap_src, dentry, name, namelen,
7196f72c7e2SArne Jansen 					async_transid, readonly, inherit);
7203de4586cSChris Mason 	} else {
72176dda93cSYan, Zheng 		error = create_subvol(BTRFS_I(dir)->root, dentry,
7226f72c7e2SArne Jansen 				      name, namelen, async_transid, inherit);
7233de4586cSChris Mason 	}
72476dda93cSYan, Zheng 	if (!error)
72576dda93cSYan, Zheng 		fsnotify_mkdir(dir, dentry);
72676dda93cSYan, Zheng out_up_read:
72776dda93cSYan, Zheng 	up_read(&BTRFS_I(dir)->root->fs_info->subvol_sem);
728cb8e7090SChristoph Hellwig out_dput:
729cb8e7090SChristoph Hellwig 	dput(dentry);
730cb8e7090SChristoph Hellwig out_unlock:
73176dda93cSYan, Zheng 	mutex_unlock(&dir->i_mutex);
732cb8e7090SChristoph Hellwig 	return error;
733cb8e7090SChristoph Hellwig }
734cb8e7090SChristoph Hellwig 
7354cb5300bSChris Mason /*
7364cb5300bSChris Mason  * When we're defragging a range, we don't want to kick it off again
7374cb5300bSChris Mason  * if it is really just waiting for delalloc to send it down.
7384cb5300bSChris Mason  * If we find a nice big extent or delalloc range for the bytes in the
7394cb5300bSChris Mason  * file you want to defrag, we return 0 to let you know to skip this
7404cb5300bSChris Mason  * part of the file
7414cb5300bSChris Mason  */
7424cb5300bSChris Mason static int check_defrag_in_cache(struct inode *inode, u64 offset, int thresh)
7434cb5300bSChris Mason {
7444cb5300bSChris Mason 	struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
7454cb5300bSChris Mason 	struct extent_map *em = NULL;
7464cb5300bSChris Mason 	struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
7474cb5300bSChris Mason 	u64 end;
7484cb5300bSChris Mason 
7494cb5300bSChris Mason 	read_lock(&em_tree->lock);
7504cb5300bSChris Mason 	em = lookup_extent_mapping(em_tree, offset, PAGE_CACHE_SIZE);
7514cb5300bSChris Mason 	read_unlock(&em_tree->lock);
7524cb5300bSChris Mason 
7534cb5300bSChris Mason 	if (em) {
7544cb5300bSChris Mason 		end = extent_map_end(em);
7554cb5300bSChris Mason 		free_extent_map(em);
7564cb5300bSChris Mason 		if (end - offset > thresh)
7574cb5300bSChris Mason 			return 0;
7584cb5300bSChris Mason 	}
7594cb5300bSChris Mason 	/* if we already have a nice delalloc here, just stop */
7604cb5300bSChris Mason 	thresh /= 2;
7614cb5300bSChris Mason 	end = count_range_bits(io_tree, &offset, offset + thresh,
7624cb5300bSChris Mason 			       thresh, EXTENT_DELALLOC, 1);
7634cb5300bSChris Mason 	if (end >= thresh)
7644cb5300bSChris Mason 		return 0;
7654cb5300bSChris Mason 	return 1;
7664cb5300bSChris Mason }
7674cb5300bSChris Mason 
7684cb5300bSChris Mason /*
7694cb5300bSChris Mason  * helper function to walk through a file and find extents
7704cb5300bSChris Mason  * newer than a specific transid, and smaller than thresh.
7714cb5300bSChris Mason  *
7724cb5300bSChris Mason  * This is used by the defragging code to find new and small
7734cb5300bSChris Mason  * extents
7744cb5300bSChris Mason  */
7754cb5300bSChris Mason static int find_new_extents(struct btrfs_root *root,
7764cb5300bSChris Mason 			    struct inode *inode, u64 newer_than,
7774cb5300bSChris Mason 			    u64 *off, int thresh)
7784cb5300bSChris Mason {
7794cb5300bSChris Mason 	struct btrfs_path *path;
7804cb5300bSChris Mason 	struct btrfs_key min_key;
7814cb5300bSChris Mason 	struct btrfs_key max_key;
7824cb5300bSChris Mason 	struct extent_buffer *leaf;
7834cb5300bSChris Mason 	struct btrfs_file_extent_item *extent;
7844cb5300bSChris Mason 	int type;
7854cb5300bSChris Mason 	int ret;
786a4689d2bSDavid Sterba 	u64 ino = btrfs_ino(inode);
7874cb5300bSChris Mason 
7884cb5300bSChris Mason 	path = btrfs_alloc_path();
7894cb5300bSChris Mason 	if (!path)
7904cb5300bSChris Mason 		return -ENOMEM;
7914cb5300bSChris Mason 
792a4689d2bSDavid Sterba 	min_key.objectid = ino;
7934cb5300bSChris Mason 	min_key.type = BTRFS_EXTENT_DATA_KEY;
7944cb5300bSChris Mason 	min_key.offset = *off;
7954cb5300bSChris Mason 
796a4689d2bSDavid Sterba 	max_key.objectid = ino;
7974cb5300bSChris Mason 	max_key.type = (u8)-1;
7984cb5300bSChris Mason 	max_key.offset = (u64)-1;
7994cb5300bSChris Mason 
8004cb5300bSChris Mason 	path->keep_locks = 1;
8014cb5300bSChris Mason 
8024cb5300bSChris Mason 	while(1) {
8034cb5300bSChris Mason 		ret = btrfs_search_forward(root, &min_key, &max_key,
8044cb5300bSChris Mason 					   path, 0, newer_than);
8054cb5300bSChris Mason 		if (ret != 0)
8064cb5300bSChris Mason 			goto none;
807a4689d2bSDavid Sterba 		if (min_key.objectid != ino)
8084cb5300bSChris Mason 			goto none;
8094cb5300bSChris Mason 		if (min_key.type != BTRFS_EXTENT_DATA_KEY)
8104cb5300bSChris Mason 			goto none;
8114cb5300bSChris Mason 
8124cb5300bSChris Mason 		leaf = path->nodes[0];
8134cb5300bSChris Mason 		extent = btrfs_item_ptr(leaf, path->slots[0],
8144cb5300bSChris Mason 					struct btrfs_file_extent_item);
8154cb5300bSChris Mason 
8164cb5300bSChris Mason 		type = btrfs_file_extent_type(leaf, extent);
8174cb5300bSChris Mason 		if (type == BTRFS_FILE_EXTENT_REG &&
8184cb5300bSChris Mason 		    btrfs_file_extent_num_bytes(leaf, extent) < thresh &&
8194cb5300bSChris Mason 		    check_defrag_in_cache(inode, min_key.offset, thresh)) {
8204cb5300bSChris Mason 			*off = min_key.offset;
8214cb5300bSChris Mason 			btrfs_free_path(path);
8224cb5300bSChris Mason 			return 0;
8234cb5300bSChris Mason 		}
8244cb5300bSChris Mason 
8254cb5300bSChris Mason 		if (min_key.offset == (u64)-1)
8264cb5300bSChris Mason 			goto none;
8274cb5300bSChris Mason 
8284cb5300bSChris Mason 		min_key.offset++;
8294cb5300bSChris Mason 		btrfs_release_path(path);
8304cb5300bSChris Mason 	}
8314cb5300bSChris Mason none:
8324cb5300bSChris Mason 	btrfs_free_path(path);
8334cb5300bSChris Mason 	return -ENOENT;
8344cb5300bSChris Mason }
8354cb5300bSChris Mason 
8366c282eb4SLi Zefan static struct extent_map *defrag_lookup_extent(struct inode *inode, u64 start)
83717ce6ef8SLiu Bo {
83817ce6ef8SLiu Bo 	struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
839940100a4SChris Mason 	struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
8406c282eb4SLi Zefan 	struct extent_map *em;
8416c282eb4SLi Zefan 	u64 len = PAGE_CACHE_SIZE;
842940100a4SChris Mason 
843940100a4SChris Mason 	/*
844940100a4SChris Mason 	 * hopefully we have this extent in the tree already, try without
845940100a4SChris Mason 	 * the full extent lock
846940100a4SChris Mason 	 */
847940100a4SChris Mason 	read_lock(&em_tree->lock);
848940100a4SChris Mason 	em = lookup_extent_mapping(em_tree, start, len);
849940100a4SChris Mason 	read_unlock(&em_tree->lock);
850940100a4SChris Mason 
851940100a4SChris Mason 	if (!em) {
852940100a4SChris Mason 		/* get the big lock and read metadata off disk */
853d0082371SJeff Mahoney 		lock_extent(io_tree, start, start + len - 1);
854940100a4SChris Mason 		em = btrfs_get_extent(inode, NULL, 0, start, len, 0);
855d0082371SJeff Mahoney 		unlock_extent(io_tree, start, start + len - 1);
856940100a4SChris Mason 
8576cf8bfbfSDan Carpenter 		if (IS_ERR(em))
8586c282eb4SLi Zefan 			return NULL;
859940100a4SChris Mason 	}
860940100a4SChris Mason 
8616c282eb4SLi Zefan 	return em;
8626c282eb4SLi Zefan }
8636c282eb4SLi Zefan 
8646c282eb4SLi Zefan static bool defrag_check_next_extent(struct inode *inode, struct extent_map *em)
8656c282eb4SLi Zefan {
8666c282eb4SLi Zefan 	struct extent_map *next;
8676c282eb4SLi Zefan 	bool ret = true;
8686c282eb4SLi Zefan 
8696c282eb4SLi Zefan 	/* this is the last extent */
8706c282eb4SLi Zefan 	if (em->start + em->len >= i_size_read(inode))
8716c282eb4SLi Zefan 		return false;
8726c282eb4SLi Zefan 
8736c282eb4SLi Zefan 	next = defrag_lookup_extent(inode, em->start + em->len);
8746c282eb4SLi Zefan 	if (!next || next->block_start >= EXTENT_MAP_LAST_BYTE)
8756c282eb4SLi Zefan 		ret = false;
8766c282eb4SLi Zefan 
8776c282eb4SLi Zefan 	free_extent_map(next);
8786c282eb4SLi Zefan 	return ret;
8796c282eb4SLi Zefan }
8806c282eb4SLi Zefan 
8816c282eb4SLi Zefan static int should_defrag_range(struct inode *inode, u64 start, int thresh,
882a43a2111SAndrew Mahone 			       u64 *last_len, u64 *skip, u64 *defrag_end,
883a43a2111SAndrew Mahone 			       int compress)
8846c282eb4SLi Zefan {
8856c282eb4SLi Zefan 	struct extent_map *em;
8866c282eb4SLi Zefan 	int ret = 1;
8876c282eb4SLi Zefan 	bool next_mergeable = true;
8886c282eb4SLi Zefan 
8896c282eb4SLi Zefan 	/*
8906c282eb4SLi Zefan 	 * make sure that once we start defragging an extent, we keep on
8916c282eb4SLi Zefan 	 * defragging it
8926c282eb4SLi Zefan 	 */
8936c282eb4SLi Zefan 	if (start < *defrag_end)
8946c282eb4SLi Zefan 		return 1;
8956c282eb4SLi Zefan 
8966c282eb4SLi Zefan 	*skip = 0;
8976c282eb4SLi Zefan 
8986c282eb4SLi Zefan 	em = defrag_lookup_extent(inode, start);
8996c282eb4SLi Zefan 	if (!em)
9006c282eb4SLi Zefan 		return 0;
9016c282eb4SLi Zefan 
902940100a4SChris Mason 	/* this will cover holes, and inline extents */
90317ce6ef8SLiu Bo 	if (em->block_start >= EXTENT_MAP_LAST_BYTE) {
904940100a4SChris Mason 		ret = 0;
90517ce6ef8SLiu Bo 		goto out;
90617ce6ef8SLiu Bo 	}
90717ce6ef8SLiu Bo 
9086c282eb4SLi Zefan 	next_mergeable = defrag_check_next_extent(inode, em);
909940100a4SChris Mason 
910940100a4SChris Mason 	/*
9116c282eb4SLi Zefan 	 * we hit a real extent, if it is big or the next extent is not a
9126c282eb4SLi Zefan 	 * real extent, don't bother defragging it
913940100a4SChris Mason 	 */
914a43a2111SAndrew Mahone 	if (!compress && (*last_len == 0 || *last_len >= thresh) &&
9156c282eb4SLi Zefan 	    (em->len >= thresh || !next_mergeable))
916940100a4SChris Mason 		ret = 0;
91717ce6ef8SLiu Bo out:
918940100a4SChris Mason 	/*
919940100a4SChris Mason 	 * last_len ends up being a counter of how many bytes we've defragged.
920940100a4SChris Mason 	 * every time we choose not to defrag an extent, we reset *last_len
921940100a4SChris Mason 	 * so that the next tiny extent will force a defrag.
922940100a4SChris Mason 	 *
923940100a4SChris Mason 	 * The end result of this is that tiny extents before a single big
924940100a4SChris Mason 	 * extent will force at least part of that big extent to be defragged.
925940100a4SChris Mason 	 */
926940100a4SChris Mason 	if (ret) {
927940100a4SChris Mason 		*defrag_end = extent_map_end(em);
928940100a4SChris Mason 	} else {
929940100a4SChris Mason 		*last_len = 0;
930940100a4SChris Mason 		*skip = extent_map_end(em);
931940100a4SChris Mason 		*defrag_end = 0;
932940100a4SChris Mason 	}
933940100a4SChris Mason 
934940100a4SChris Mason 	free_extent_map(em);
935940100a4SChris Mason 	return ret;
936940100a4SChris Mason }
937940100a4SChris Mason 
9384cb5300bSChris Mason /*
9394cb5300bSChris Mason  * it doesn't do much good to defrag one or two pages
9404cb5300bSChris Mason  * at a time.  This pulls in a nice chunk of pages
9414cb5300bSChris Mason  * to COW and defrag.
9424cb5300bSChris Mason  *
9434cb5300bSChris Mason  * It also makes sure the delalloc code has enough
9444cb5300bSChris Mason  * dirty data to avoid making new small extents as part
9454cb5300bSChris Mason  * of the defrag
9464cb5300bSChris Mason  *
9474cb5300bSChris Mason  * It's a good idea to start RA on this range
9484cb5300bSChris Mason  * before calling this.
9494cb5300bSChris Mason  */
9504cb5300bSChris Mason static int cluster_pages_for_defrag(struct inode *inode,
9514cb5300bSChris Mason 				    struct page **pages,
9524cb5300bSChris Mason 				    unsigned long start_index,
9534cb5300bSChris Mason 				    int num_pages)
954f46b5a66SChristoph Hellwig {
9554cb5300bSChris Mason 	unsigned long file_end;
9564cb5300bSChris Mason 	u64 isize = i_size_read(inode);
957f46b5a66SChristoph Hellwig 	u64 page_start;
958f46b5a66SChristoph Hellwig 	u64 page_end;
9591f12bd06SLiu Bo 	u64 page_cnt;
9604cb5300bSChris Mason 	int ret;
9614cb5300bSChris Mason 	int i;
9624cb5300bSChris Mason 	int i_done;
9634cb5300bSChris Mason 	struct btrfs_ordered_extent *ordered;
9644cb5300bSChris Mason 	struct extent_state *cached_state = NULL;
965600a45e1SMiao Xie 	struct extent_io_tree *tree;
9663b16a4e3SJosef Bacik 	gfp_t mask = btrfs_alloc_write_mask(inode->i_mapping);
9674cb5300bSChris Mason 
9684cb5300bSChris Mason 	file_end = (isize - 1) >> PAGE_CACHE_SHIFT;
9691f12bd06SLiu Bo 	if (!isize || start_index > file_end)
9701f12bd06SLiu Bo 		return 0;
9711f12bd06SLiu Bo 
9721f12bd06SLiu Bo 	page_cnt = min_t(u64, (u64)num_pages, (u64)file_end - start_index + 1);
9734cb5300bSChris Mason 
9744cb5300bSChris Mason 	ret = btrfs_delalloc_reserve_space(inode,
9751f12bd06SLiu Bo 					   page_cnt << PAGE_CACHE_SHIFT);
9764cb5300bSChris Mason 	if (ret)
9774cb5300bSChris Mason 		return ret;
9784cb5300bSChris Mason 	i_done = 0;
979600a45e1SMiao Xie 	tree = &BTRFS_I(inode)->io_tree;
9804cb5300bSChris Mason 
9814cb5300bSChris Mason 	/* step one, lock all the pages */
9821f12bd06SLiu Bo 	for (i = 0; i < page_cnt; i++) {
9834cb5300bSChris Mason 		struct page *page;
984600a45e1SMiao Xie again:
985a94733d0SJosef Bacik 		page = find_or_create_page(inode->i_mapping,
9863b16a4e3SJosef Bacik 					   start_index + i, mask);
9874cb5300bSChris Mason 		if (!page)
9884cb5300bSChris Mason 			break;
9894cb5300bSChris Mason 
990600a45e1SMiao Xie 		page_start = page_offset(page);
991600a45e1SMiao Xie 		page_end = page_start + PAGE_CACHE_SIZE - 1;
992600a45e1SMiao Xie 		while (1) {
993d0082371SJeff Mahoney 			lock_extent(tree, page_start, page_end);
994600a45e1SMiao Xie 			ordered = btrfs_lookup_ordered_extent(inode,
995600a45e1SMiao Xie 							      page_start);
996d0082371SJeff Mahoney 			unlock_extent(tree, page_start, page_end);
997600a45e1SMiao Xie 			if (!ordered)
998600a45e1SMiao Xie 				break;
999600a45e1SMiao Xie 
1000600a45e1SMiao Xie 			unlock_page(page);
1001600a45e1SMiao Xie 			btrfs_start_ordered_extent(inode, ordered, 1);
1002600a45e1SMiao Xie 			btrfs_put_ordered_extent(ordered);
1003600a45e1SMiao Xie 			lock_page(page);
10041f12bd06SLiu Bo 			/*
10051f12bd06SLiu Bo 			 * we unlocked the page above, so we need check if
10061f12bd06SLiu Bo 			 * it was released or not.
10071f12bd06SLiu Bo 			 */
10081f12bd06SLiu Bo 			if (page->mapping != inode->i_mapping) {
10091f12bd06SLiu Bo 				unlock_page(page);
10101f12bd06SLiu Bo 				page_cache_release(page);
10111f12bd06SLiu Bo 				goto again;
10121f12bd06SLiu Bo 			}
1013600a45e1SMiao Xie 		}
1014600a45e1SMiao Xie 
10154cb5300bSChris Mason 		if (!PageUptodate(page)) {
10164cb5300bSChris Mason 			btrfs_readpage(NULL, page);
10174cb5300bSChris Mason 			lock_page(page);
10184cb5300bSChris Mason 			if (!PageUptodate(page)) {
10194cb5300bSChris Mason 				unlock_page(page);
10204cb5300bSChris Mason 				page_cache_release(page);
10214cb5300bSChris Mason 				ret = -EIO;
10224cb5300bSChris Mason 				break;
10234cb5300bSChris Mason 			}
10244cb5300bSChris Mason 		}
1025600a45e1SMiao Xie 
1026600a45e1SMiao Xie 		if (page->mapping != inode->i_mapping) {
1027600a45e1SMiao Xie 			unlock_page(page);
1028600a45e1SMiao Xie 			page_cache_release(page);
1029600a45e1SMiao Xie 			goto again;
1030600a45e1SMiao Xie 		}
1031600a45e1SMiao Xie 
10324cb5300bSChris Mason 		pages[i] = page;
10334cb5300bSChris Mason 		i_done++;
10344cb5300bSChris Mason 	}
10354cb5300bSChris Mason 	if (!i_done || ret)
10364cb5300bSChris Mason 		goto out;
10374cb5300bSChris Mason 
10384cb5300bSChris Mason 	if (!(inode->i_sb->s_flags & MS_ACTIVE))
10394cb5300bSChris Mason 		goto out;
10404cb5300bSChris Mason 
10414cb5300bSChris Mason 	/*
10424cb5300bSChris Mason 	 * so now we have a nice long stream of locked
10434cb5300bSChris Mason 	 * and up to date pages, lets wait on them
10444cb5300bSChris Mason 	 */
10454cb5300bSChris Mason 	for (i = 0; i < i_done; i++)
10464cb5300bSChris Mason 		wait_on_page_writeback(pages[i]);
10474cb5300bSChris Mason 
10484cb5300bSChris Mason 	page_start = page_offset(pages[0]);
10494cb5300bSChris Mason 	page_end = page_offset(pages[i_done - 1]) + PAGE_CACHE_SIZE;
10504cb5300bSChris Mason 
10514cb5300bSChris Mason 	lock_extent_bits(&BTRFS_I(inode)->io_tree,
1052d0082371SJeff Mahoney 			 page_start, page_end - 1, 0, &cached_state);
10534cb5300bSChris Mason 	clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start,
10544cb5300bSChris Mason 			  page_end - 1, EXTENT_DIRTY | EXTENT_DELALLOC |
10559e8a4a8bSLiu Bo 			  EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG, 0, 0,
10569e8a4a8bSLiu Bo 			  &cached_state, GFP_NOFS);
10574cb5300bSChris Mason 
10581f12bd06SLiu Bo 	if (i_done != page_cnt) {
10599e0baf60SJosef Bacik 		spin_lock(&BTRFS_I(inode)->lock);
10609e0baf60SJosef Bacik 		BTRFS_I(inode)->outstanding_extents++;
10619e0baf60SJosef Bacik 		spin_unlock(&BTRFS_I(inode)->lock);
10624cb5300bSChris Mason 		btrfs_delalloc_release_space(inode,
10631f12bd06SLiu Bo 				     (page_cnt - i_done) << PAGE_CACHE_SHIFT);
10644cb5300bSChris Mason 	}
10654cb5300bSChris Mason 
10664cb5300bSChris Mason 
10679e8a4a8bSLiu Bo 	set_extent_defrag(&BTRFS_I(inode)->io_tree, page_start, page_end - 1,
10689e8a4a8bSLiu Bo 			  &cached_state, GFP_NOFS);
10694cb5300bSChris Mason 
10704cb5300bSChris Mason 	unlock_extent_cached(&BTRFS_I(inode)->io_tree,
10714cb5300bSChris Mason 			     page_start, page_end - 1, &cached_state,
10724cb5300bSChris Mason 			     GFP_NOFS);
10734cb5300bSChris Mason 
10744cb5300bSChris Mason 	for (i = 0; i < i_done; i++) {
10754cb5300bSChris Mason 		clear_page_dirty_for_io(pages[i]);
10764cb5300bSChris Mason 		ClearPageChecked(pages[i]);
10774cb5300bSChris Mason 		set_page_extent_mapped(pages[i]);
10784cb5300bSChris Mason 		set_page_dirty(pages[i]);
10794cb5300bSChris Mason 		unlock_page(pages[i]);
10804cb5300bSChris Mason 		page_cache_release(pages[i]);
10814cb5300bSChris Mason 	}
10824cb5300bSChris Mason 	return i_done;
10834cb5300bSChris Mason out:
10844cb5300bSChris Mason 	for (i = 0; i < i_done; i++) {
10854cb5300bSChris Mason 		unlock_page(pages[i]);
10864cb5300bSChris Mason 		page_cache_release(pages[i]);
10874cb5300bSChris Mason 	}
10881f12bd06SLiu Bo 	btrfs_delalloc_release_space(inode, page_cnt << PAGE_CACHE_SHIFT);
10894cb5300bSChris Mason 	return ret;
10904cb5300bSChris Mason 
10914cb5300bSChris Mason }
10924cb5300bSChris Mason 
10934cb5300bSChris Mason int btrfs_defrag_file(struct inode *inode, struct file *file,
10944cb5300bSChris Mason 		      struct btrfs_ioctl_defrag_range_args *range,
10954cb5300bSChris Mason 		      u64 newer_than, unsigned long max_to_defrag)
10964cb5300bSChris Mason {
10974cb5300bSChris Mason 	struct btrfs_root *root = BTRFS_I(inode)->root;
10984cb5300bSChris Mason 	struct file_ra_state *ra = NULL;
10994cb5300bSChris Mason 	unsigned long last_index;
1100151a31b2SLi Zefan 	u64 isize = i_size_read(inode);
1101940100a4SChris Mason 	u64 last_len = 0;
1102940100a4SChris Mason 	u64 skip = 0;
1103940100a4SChris Mason 	u64 defrag_end = 0;
11044cb5300bSChris Mason 	u64 newer_off = range->start;
1105f46b5a66SChristoph Hellwig 	unsigned long i;
1106008873eaSLi Zefan 	unsigned long ra_index = 0;
1107f46b5a66SChristoph Hellwig 	int ret;
11084cb5300bSChris Mason 	int defrag_count = 0;
11091a419d85SLi Zefan 	int compress_type = BTRFS_COMPRESS_ZLIB;
11104cb5300bSChris Mason 	int extent_thresh = range->extent_thresh;
1111008873eaSLi Zefan 	int max_cluster = (256 * 1024) >> PAGE_CACHE_SHIFT;
1112008873eaSLi Zefan 	int cluster = max_cluster;
11134cb5300bSChris Mason 	u64 new_align = ~((u64)128 * 1024 - 1);
11144cb5300bSChris Mason 	struct page **pages = NULL;
11154cb5300bSChris Mason 
11164cb5300bSChris Mason 	if (extent_thresh == 0)
11174cb5300bSChris Mason 		extent_thresh = 256 * 1024;
11181a419d85SLi Zefan 
11191a419d85SLi Zefan 	if (range->flags & BTRFS_DEFRAG_RANGE_COMPRESS) {
11201a419d85SLi Zefan 		if (range->compress_type > BTRFS_COMPRESS_TYPES)
11211a419d85SLi Zefan 			return -EINVAL;
11221a419d85SLi Zefan 		if (range->compress_type)
11231a419d85SLi Zefan 			compress_type = range->compress_type;
11241a419d85SLi Zefan 	}
1125f46b5a66SChristoph Hellwig 
1126151a31b2SLi Zefan 	if (isize == 0)
1127940100a4SChris Mason 		return 0;
1128f46b5a66SChristoph Hellwig 
11294cb5300bSChris Mason 	/*
11304cb5300bSChris Mason 	 * if we were not given a file, allocate a readahead
11314cb5300bSChris Mason 	 * context
11324cb5300bSChris Mason 	 */
11334cb5300bSChris Mason 	if (!file) {
11344cb5300bSChris Mason 		ra = kzalloc(sizeof(*ra), GFP_NOFS);
11354cb5300bSChris Mason 		if (!ra)
11364cb5300bSChris Mason 			return -ENOMEM;
11374cb5300bSChris Mason 		file_ra_state_init(ra, inode->i_mapping);
11384cb5300bSChris Mason 	} else {
11394cb5300bSChris Mason 		ra = &file->f_ra;
11404cb5300bSChris Mason 	}
11414cb5300bSChris Mason 
1142008873eaSLi Zefan 	pages = kmalloc(sizeof(struct page *) * max_cluster,
11434cb5300bSChris Mason 			GFP_NOFS);
11444cb5300bSChris Mason 	if (!pages) {
11454cb5300bSChris Mason 		ret = -ENOMEM;
11464cb5300bSChris Mason 		goto out_ra;
11474cb5300bSChris Mason 	}
11484cb5300bSChris Mason 
11494cb5300bSChris Mason 	/* find the last page to defrag */
11501e701a32SChris Mason 	if (range->start + range->len > range->start) {
1151151a31b2SLi Zefan 		last_index = min_t(u64, isize - 1,
11521e701a32SChris Mason 			 range->start + range->len - 1) >> PAGE_CACHE_SHIFT;
11531e701a32SChris Mason 	} else {
1154151a31b2SLi Zefan 		last_index = (isize - 1) >> PAGE_CACHE_SHIFT;
11551e701a32SChris Mason 	}
11561e701a32SChris Mason 
11574cb5300bSChris Mason 	if (newer_than) {
11584cb5300bSChris Mason 		ret = find_new_extents(root, inode, newer_than,
11594cb5300bSChris Mason 				       &newer_off, 64 * 1024);
11604cb5300bSChris Mason 		if (!ret) {
11614cb5300bSChris Mason 			range->start = newer_off;
11624cb5300bSChris Mason 			/*
11634cb5300bSChris Mason 			 * we always align our defrag to help keep
11644cb5300bSChris Mason 			 * the extents in the file evenly spaced
11654cb5300bSChris Mason 			 */
11664cb5300bSChris Mason 			i = (newer_off & new_align) >> PAGE_CACHE_SHIFT;
11674cb5300bSChris Mason 		} else
11684cb5300bSChris Mason 			goto out_ra;
11694cb5300bSChris Mason 	} else {
11701e701a32SChris Mason 		i = range->start >> PAGE_CACHE_SHIFT;
11714cb5300bSChris Mason 	}
11724cb5300bSChris Mason 	if (!max_to_defrag)
11737ec31b54SLiu Bo 		max_to_defrag = last_index + 1;
11744cb5300bSChris Mason 
11752a0f7f57SLi Zefan 	/*
11762a0f7f57SLi Zefan 	 * make writeback starts from i, so the defrag range can be
11772a0f7f57SLi Zefan 	 * written sequentially.
11782a0f7f57SLi Zefan 	 */
11792a0f7f57SLi Zefan 	if (i < inode->i_mapping->writeback_index)
11802a0f7f57SLi Zefan 		inode->i_mapping->writeback_index = i;
11812a0f7f57SLi Zefan 
1182f7f43cc8SChris Mason 	while (i <= last_index && defrag_count < max_to_defrag &&
1183f7f43cc8SChris Mason 	       (i < (i_size_read(inode) + PAGE_CACHE_SIZE - 1) >>
1184f7f43cc8SChris Mason 		PAGE_CACHE_SHIFT)) {
11854cb5300bSChris Mason 		/*
11864cb5300bSChris Mason 		 * make sure we stop running if someone unmounts
11874cb5300bSChris Mason 		 * the FS
11884cb5300bSChris Mason 		 */
11894cb5300bSChris Mason 		if (!(inode->i_sb->s_flags & MS_ACTIVE))
11904cb5300bSChris Mason 			break;
11914cb5300bSChris Mason 
119266c26892SLiu Bo 		if (!should_defrag_range(inode, (u64)i << PAGE_CACHE_SHIFT,
11936c282eb4SLi Zefan 					 extent_thresh, &last_len, &skip,
1194a43a2111SAndrew Mahone 					 &defrag_end, range->flags &
1195a43a2111SAndrew Mahone 					 BTRFS_DEFRAG_RANGE_COMPRESS)) {
1196940100a4SChris Mason 			unsigned long next;
1197940100a4SChris Mason 			/*
1198940100a4SChris Mason 			 * the should_defrag function tells us how much to skip
1199940100a4SChris Mason 			 * bump our counter by the suggested amount
1200940100a4SChris Mason 			 */
1201940100a4SChris Mason 			next = (skip + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
1202940100a4SChris Mason 			i = max(i + 1, next);
1203940100a4SChris Mason 			continue;
1204940100a4SChris Mason 		}
1205008873eaSLi Zefan 
1206008873eaSLi Zefan 		if (!newer_than) {
1207008873eaSLi Zefan 			cluster = (PAGE_CACHE_ALIGN(defrag_end) >>
1208008873eaSLi Zefan 				   PAGE_CACHE_SHIFT) - i;
1209008873eaSLi Zefan 			cluster = min(cluster, max_cluster);
1210008873eaSLi Zefan 		} else {
1211008873eaSLi Zefan 			cluster = max_cluster;
1212008873eaSLi Zefan 		}
1213008873eaSLi Zefan 
12141e701a32SChris Mason 		if (range->flags & BTRFS_DEFRAG_RANGE_COMPRESS)
12151a419d85SLi Zefan 			BTRFS_I(inode)->force_compress = compress_type;
1216940100a4SChris Mason 
1217008873eaSLi Zefan 		if (i + cluster > ra_index) {
1218008873eaSLi Zefan 			ra_index = max(i, ra_index);
1219008873eaSLi Zefan 			btrfs_force_ra(inode->i_mapping, ra, file, ra_index,
1220008873eaSLi Zefan 				       cluster);
1221008873eaSLi Zefan 			ra_index += max_cluster;
1222008873eaSLi Zefan 		}
12234cb5300bSChris Mason 
1224ecb8bea8SLiu Bo 		mutex_lock(&inode->i_mutex);
1225008873eaSLi Zefan 		ret = cluster_pages_for_defrag(inode, pages, i, cluster);
1226ecb8bea8SLiu Bo 		if (ret < 0) {
1227ecb8bea8SLiu Bo 			mutex_unlock(&inode->i_mutex);
12284cb5300bSChris Mason 			goto out_ra;
1229ecb8bea8SLiu Bo 		}
12304cb5300bSChris Mason 
12314cb5300bSChris Mason 		defrag_count += ret;
12324cb5300bSChris Mason 		balance_dirty_pages_ratelimited_nr(inode->i_mapping, ret);
1233ecb8bea8SLiu Bo 		mutex_unlock(&inode->i_mutex);
12344cb5300bSChris Mason 
12354cb5300bSChris Mason 		if (newer_than) {
12364cb5300bSChris Mason 			if (newer_off == (u64)-1)
12374cb5300bSChris Mason 				break;
12384cb5300bSChris Mason 
1239e1f041e1SLiu Bo 			if (ret > 0)
1240e1f041e1SLiu Bo 				i += ret;
1241e1f041e1SLiu Bo 
12424cb5300bSChris Mason 			newer_off = max(newer_off + 1,
12434cb5300bSChris Mason 					(u64)i << PAGE_CACHE_SHIFT);
12444cb5300bSChris Mason 
12454cb5300bSChris Mason 			ret = find_new_extents(root, inode,
12464cb5300bSChris Mason 					       newer_than, &newer_off,
12474cb5300bSChris Mason 					       64 * 1024);
12484cb5300bSChris Mason 			if (!ret) {
12494cb5300bSChris Mason 				range->start = newer_off;
12504cb5300bSChris Mason 				i = (newer_off & new_align) >> PAGE_CACHE_SHIFT;
12514cb5300bSChris Mason 			} else {
12524cb5300bSChris Mason 				break;
1253940100a4SChris Mason 			}
12544cb5300bSChris Mason 		} else {
1255008873eaSLi Zefan 			if (ret > 0) {
1256cbcc8326SLi Zefan 				i += ret;
1257008873eaSLi Zefan 				last_len += ret << PAGE_CACHE_SHIFT;
1258008873eaSLi Zefan 			} else {
1259940100a4SChris Mason 				i++;
1260008873eaSLi Zefan 				last_len = 0;
1261008873eaSLi Zefan 			}
1262f46b5a66SChristoph Hellwig 		}
12634cb5300bSChris Mason 	}
1264f46b5a66SChristoph Hellwig 
12651e701a32SChris Mason 	if ((range->flags & BTRFS_DEFRAG_RANGE_START_IO))
12661e701a32SChris Mason 		filemap_flush(inode->i_mapping);
12671e701a32SChris Mason 
12681e701a32SChris Mason 	if ((range->flags & BTRFS_DEFRAG_RANGE_COMPRESS)) {
12691e701a32SChris Mason 		/* the filemap_flush will queue IO into the worker threads, but
12701e701a32SChris Mason 		 * we have to make sure the IO is actually started and that
12711e701a32SChris Mason 		 * ordered extents get created before we return
12721e701a32SChris Mason 		 */
12731e701a32SChris Mason 		atomic_inc(&root->fs_info->async_submit_draining);
12741e701a32SChris Mason 		while (atomic_read(&root->fs_info->nr_async_submits) ||
12751e701a32SChris Mason 		      atomic_read(&root->fs_info->async_delalloc_pages)) {
12761e701a32SChris Mason 			wait_event(root->fs_info->async_submit_wait,
12771e701a32SChris Mason 			   (atomic_read(&root->fs_info->nr_async_submits) == 0 &&
12781e701a32SChris Mason 			    atomic_read(&root->fs_info->async_delalloc_pages) == 0));
12791e701a32SChris Mason 		}
12801e701a32SChris Mason 		atomic_dec(&root->fs_info->async_submit_draining);
12811e701a32SChris Mason 
12821e701a32SChris Mason 		mutex_lock(&inode->i_mutex);
1283261507a0SLi Zefan 		BTRFS_I(inode)->force_compress = BTRFS_COMPRESS_NONE;
12841e701a32SChris Mason 		mutex_unlock(&inode->i_mutex);
12851e701a32SChris Mason 	}
12861e701a32SChris Mason 
12871a419d85SLi Zefan 	if (range->compress_type == BTRFS_COMPRESS_LZO) {
12882b0ce2c2SMitch Harder 		btrfs_set_fs_incompat(root->fs_info, COMPRESS_LZO);
12891a419d85SLi Zefan 	}
12901a419d85SLi Zefan 
129160ccf82fSDiego Calleja 	ret = defrag_count;
1292940100a4SChris Mason 
12934cb5300bSChris Mason out_ra:
12944cb5300bSChris Mason 	if (!file)
12954cb5300bSChris Mason 		kfree(ra);
12964cb5300bSChris Mason 	kfree(pages);
1297940100a4SChris Mason 	return ret;
1298f46b5a66SChristoph Hellwig }
1299f46b5a66SChristoph Hellwig 
130076dda93cSYan, Zheng static noinline int btrfs_ioctl_resize(struct btrfs_root *root,
130176dda93cSYan, Zheng 					void __user *arg)
1302f46b5a66SChristoph Hellwig {
1303f46b5a66SChristoph Hellwig 	u64 new_size;
1304f46b5a66SChristoph Hellwig 	u64 old_size;
1305f46b5a66SChristoph Hellwig 	u64 devid = 1;
1306f46b5a66SChristoph Hellwig 	struct btrfs_ioctl_vol_args *vol_args;
1307f46b5a66SChristoph Hellwig 	struct btrfs_trans_handle *trans;
1308f46b5a66SChristoph Hellwig 	struct btrfs_device *device = NULL;
1309f46b5a66SChristoph Hellwig 	char *sizestr;
1310f46b5a66SChristoph Hellwig 	char *devstr = NULL;
1311f46b5a66SChristoph Hellwig 	int ret = 0;
1312f46b5a66SChristoph Hellwig 	int mod = 0;
1313f46b5a66SChristoph Hellwig 
1314c146afadSYan Zheng 	if (root->fs_info->sb->s_flags & MS_RDONLY)
1315c146afadSYan Zheng 		return -EROFS;
1316c146afadSYan Zheng 
1317e441d54dSChris Mason 	if (!capable(CAP_SYS_ADMIN))
1318e441d54dSChris Mason 		return -EPERM;
1319e441d54dSChris Mason 
13205ac00addSStefan Behrens 	if (atomic_xchg(&root->fs_info->mutually_exclusive_operation_running,
13215ac00addSStefan Behrens 			1)) {
13225ac00addSStefan Behrens 		pr_info("btrfs: dev add/delete/balance/replace/resize operation in progress\n");
13235ac00addSStefan Behrens 		return -EINPROGRESS;
1324c9e9f97bSIlya Dryomov 	}
1325c9e9f97bSIlya Dryomov 
13265ac00addSStefan Behrens 	mutex_lock(&root->fs_info->volume_mutex);
1327dae7b665SLi Zefan 	vol_args = memdup_user(arg, sizeof(*vol_args));
1328c9e9f97bSIlya Dryomov 	if (IS_ERR(vol_args)) {
1329c9e9f97bSIlya Dryomov 		ret = PTR_ERR(vol_args);
1330c9e9f97bSIlya Dryomov 		goto out;
1331c9e9f97bSIlya Dryomov 	}
13325516e595SMark Fasheh 
13335516e595SMark Fasheh 	vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
1334f46b5a66SChristoph Hellwig 
1335f46b5a66SChristoph Hellwig 	sizestr = vol_args->name;
1336f46b5a66SChristoph Hellwig 	devstr = strchr(sizestr, ':');
1337f46b5a66SChristoph Hellwig 	if (devstr) {
1338f46b5a66SChristoph Hellwig 		char *end;
1339f46b5a66SChristoph Hellwig 		sizestr = devstr + 1;
1340f46b5a66SChristoph Hellwig 		*devstr = '\0';
1341f46b5a66SChristoph Hellwig 		devstr = vol_args->name;
1342f46b5a66SChristoph Hellwig 		devid = simple_strtoull(devstr, &end, 10);
13435bb14682SArnd Hannemann 		printk(KERN_INFO "btrfs: resizing devid %llu\n",
134421380931SJoel Becker 		       (unsigned long long)devid);
1345f46b5a66SChristoph Hellwig 	}
1346aa1b8cd4SStefan Behrens 	device = btrfs_find_device(root->fs_info, devid, NULL, NULL);
1347f46b5a66SChristoph Hellwig 	if (!device) {
13485bb14682SArnd Hannemann 		printk(KERN_INFO "btrfs: resizer unable to find device %llu\n",
134921380931SJoel Becker 		       (unsigned long long)devid);
1350f46b5a66SChristoph Hellwig 		ret = -EINVAL;
1351c9e9f97bSIlya Dryomov 		goto out_free;
1352f46b5a66SChristoph Hellwig 	}
13534e42ae1bSLiu Bo 	if (device->fs_devices && device->fs_devices->seeding) {
13544e42ae1bSLiu Bo 		printk(KERN_INFO "btrfs: resizer unable to apply on "
1355a8c4a33bSChris Mason 		       "seeding device %llu\n",
1356a8c4a33bSChris Mason 		       (unsigned long long)devid);
13574e42ae1bSLiu Bo 		ret = -EINVAL;
13584e42ae1bSLiu Bo 		goto out_free;
13594e42ae1bSLiu Bo 	}
13604e42ae1bSLiu Bo 
1361f46b5a66SChristoph Hellwig 	if (!strcmp(sizestr, "max"))
1362f46b5a66SChristoph Hellwig 		new_size = device->bdev->bd_inode->i_size;
1363f46b5a66SChristoph Hellwig 	else {
1364f46b5a66SChristoph Hellwig 		if (sizestr[0] == '-') {
1365f46b5a66SChristoph Hellwig 			mod = -1;
1366f46b5a66SChristoph Hellwig 			sizestr++;
1367f46b5a66SChristoph Hellwig 		} else if (sizestr[0] == '+') {
1368f46b5a66SChristoph Hellwig 			mod = 1;
1369f46b5a66SChristoph Hellwig 			sizestr++;
1370f46b5a66SChristoph Hellwig 		}
137191748467SAkinobu Mita 		new_size = memparse(sizestr, NULL);
1372f46b5a66SChristoph Hellwig 		if (new_size == 0) {
1373f46b5a66SChristoph Hellwig 			ret = -EINVAL;
1374c9e9f97bSIlya Dryomov 			goto out_free;
1375f46b5a66SChristoph Hellwig 		}
1376f46b5a66SChristoph Hellwig 	}
1377f46b5a66SChristoph Hellwig 
137863a212abSStefan Behrens 	if (device->is_tgtdev_for_dev_replace) {
137963a212abSStefan Behrens 		ret = -EINVAL;
138063a212abSStefan Behrens 		goto out_free;
138163a212abSStefan Behrens 	}
138263a212abSStefan Behrens 
1383f46b5a66SChristoph Hellwig 	old_size = device->total_bytes;
1384f46b5a66SChristoph Hellwig 
1385f46b5a66SChristoph Hellwig 	if (mod < 0) {
1386f46b5a66SChristoph Hellwig 		if (new_size > old_size) {
1387f46b5a66SChristoph Hellwig 			ret = -EINVAL;
1388c9e9f97bSIlya Dryomov 			goto out_free;
1389f46b5a66SChristoph Hellwig 		}
1390f46b5a66SChristoph Hellwig 		new_size = old_size - new_size;
1391f46b5a66SChristoph Hellwig 	} else if (mod > 0) {
1392f46b5a66SChristoph Hellwig 		new_size = old_size + new_size;
1393f46b5a66SChristoph Hellwig 	}
1394f46b5a66SChristoph Hellwig 
1395f46b5a66SChristoph Hellwig 	if (new_size < 256 * 1024 * 1024) {
1396f46b5a66SChristoph Hellwig 		ret = -EINVAL;
1397c9e9f97bSIlya Dryomov 		goto out_free;
1398f46b5a66SChristoph Hellwig 	}
1399f46b5a66SChristoph Hellwig 	if (new_size > device->bdev->bd_inode->i_size) {
1400f46b5a66SChristoph Hellwig 		ret = -EFBIG;
1401c9e9f97bSIlya Dryomov 		goto out_free;
1402f46b5a66SChristoph Hellwig 	}
1403f46b5a66SChristoph Hellwig 
1404f46b5a66SChristoph Hellwig 	do_div(new_size, root->sectorsize);
1405f46b5a66SChristoph Hellwig 	new_size *= root->sectorsize;
1406f46b5a66SChristoph Hellwig 
1407606686eeSJosef Bacik 	printk_in_rcu(KERN_INFO "btrfs: new size for %s is %llu\n",
1408606686eeSJosef Bacik 		      rcu_str_deref(device->name),
1409606686eeSJosef Bacik 		      (unsigned long long)new_size);
1410f46b5a66SChristoph Hellwig 
1411f46b5a66SChristoph Hellwig 	if (new_size > old_size) {
1412a22285a6SYan, Zheng 		trans = btrfs_start_transaction(root, 0);
141398d5dc13STsutomu Itoh 		if (IS_ERR(trans)) {
141498d5dc13STsutomu Itoh 			ret = PTR_ERR(trans);
1415c9e9f97bSIlya Dryomov 			goto out_free;
141698d5dc13STsutomu Itoh 		}
1417f46b5a66SChristoph Hellwig 		ret = btrfs_grow_device(trans, device, new_size);
1418f46b5a66SChristoph Hellwig 		btrfs_commit_transaction(trans, root);
1419ece7d20eSMike Fleetwood 	} else if (new_size < old_size) {
1420f46b5a66SChristoph Hellwig 		ret = btrfs_shrink_device(device, new_size);
14210253f40eSjeff.liu 	} /* equal, nothing need to do */
1422f46b5a66SChristoph Hellwig 
1423c9e9f97bSIlya Dryomov out_free:
1424f46b5a66SChristoph Hellwig 	kfree(vol_args);
1425c9e9f97bSIlya Dryomov out:
1426c9e9f97bSIlya Dryomov 	mutex_unlock(&root->fs_info->volume_mutex);
14275ac00addSStefan Behrens 	atomic_set(&root->fs_info->mutually_exclusive_operation_running, 0);
1428f46b5a66SChristoph Hellwig 	return ret;
1429f46b5a66SChristoph Hellwig }
1430f46b5a66SChristoph Hellwig 
143172fd032eSSage Weil static noinline int btrfs_ioctl_snap_create_transid(struct file *file,
14326f72c7e2SArne Jansen 				char *name, unsigned long fd, int subvol,
14336f72c7e2SArne Jansen 				u64 *transid, bool readonly,
14346f72c7e2SArne Jansen 				struct btrfs_qgroup_inherit **inherit)
1435f46b5a66SChristoph Hellwig {
1436f46b5a66SChristoph Hellwig 	int namelen;
14373de4586cSChris Mason 	int ret = 0;
1438f46b5a66SChristoph Hellwig 
1439a874a63eSLiu Bo 	ret = mnt_want_write_file(file);
1440a874a63eSLiu Bo 	if (ret)
1441a874a63eSLiu Bo 		goto out;
1442a874a63eSLiu Bo 
144372fd032eSSage Weil 	namelen = strlen(name);
144472fd032eSSage Weil 	if (strchr(name, '/')) {
1445f46b5a66SChristoph Hellwig 		ret = -EINVAL;
1446a874a63eSLiu Bo 		goto out_drop_write;
1447f46b5a66SChristoph Hellwig 	}
1448f46b5a66SChristoph Hellwig 
144916780cabSChris Mason 	if (name[0] == '.' &&
145016780cabSChris Mason 	   (namelen == 1 || (name[1] == '.' && namelen == 2))) {
145116780cabSChris Mason 		ret = -EEXIST;
1452a874a63eSLiu Bo 		goto out_drop_write;
145316780cabSChris Mason 	}
145416780cabSChris Mason 
14553de4586cSChris Mason 	if (subvol) {
145672fd032eSSage Weil 		ret = btrfs_mksubvol(&file->f_path, name, namelen,
14576f72c7e2SArne Jansen 				     NULL, transid, readonly, inherit);
1458cb8e7090SChristoph Hellwig 	} else {
14592903ff01SAl Viro 		struct fd src = fdget(fd);
14603de4586cSChris Mason 		struct inode *src_inode;
14612903ff01SAl Viro 		if (!src.file) {
14623de4586cSChris Mason 			ret = -EINVAL;
1463a874a63eSLiu Bo 			goto out_drop_write;
14643de4586cSChris Mason 		}
14653de4586cSChris Mason 
14662903ff01SAl Viro 		src_inode = src.file->f_path.dentry->d_inode;
14673de4586cSChris Mason 		if (src_inode->i_sb != file->f_path.dentry->d_inode->i_sb) {
1468d397712bSChris Mason 			printk(KERN_INFO "btrfs: Snapshot src from "
1469d397712bSChris Mason 			       "another FS\n");
14703de4586cSChris Mason 			ret = -EINVAL;
1471ecd18815SAl Viro 		} else {
147272fd032eSSage Weil 			ret = btrfs_mksubvol(&file->f_path, name, namelen,
147372fd032eSSage Weil 					     BTRFS_I(src_inode)->root,
14746f72c7e2SArne Jansen 					     transid, readonly, inherit);
1475ecd18815SAl Viro 		}
14762903ff01SAl Viro 		fdput(src);
1477cb8e7090SChristoph Hellwig 	}
1478a874a63eSLiu Bo out_drop_write:
1479a874a63eSLiu Bo 	mnt_drop_write_file(file);
1480f46b5a66SChristoph Hellwig out:
148172fd032eSSage Weil 	return ret;
148272fd032eSSage Weil }
148372fd032eSSage Weil 
148472fd032eSSage Weil static noinline int btrfs_ioctl_snap_create(struct file *file,
1485fa0d2b9bSLi Zefan 					    void __user *arg, int subvol)
148672fd032eSSage Weil {
1487fa0d2b9bSLi Zefan 	struct btrfs_ioctl_vol_args *vol_args;
148872fd032eSSage Weil 	int ret;
148972fd032eSSage Weil 
1490fa0d2b9bSLi Zefan 	vol_args = memdup_user(arg, sizeof(*vol_args));
1491fa0d2b9bSLi Zefan 	if (IS_ERR(vol_args))
1492fa0d2b9bSLi Zefan 		return PTR_ERR(vol_args);
1493fa0d2b9bSLi Zefan 	vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
1494fa0d2b9bSLi Zefan 
1495fa0d2b9bSLi Zefan 	ret = btrfs_ioctl_snap_create_transid(file, vol_args->name,
1496b83cc969SLi Zefan 					      vol_args->fd, subvol,
14976f72c7e2SArne Jansen 					      NULL, false, NULL);
1498fa0d2b9bSLi Zefan 
1499fa0d2b9bSLi Zefan 	kfree(vol_args);
1500fa0d2b9bSLi Zefan 	return ret;
1501fa0d2b9bSLi Zefan }
1502fa0d2b9bSLi Zefan 
1503fa0d2b9bSLi Zefan static noinline int btrfs_ioctl_snap_create_v2(struct file *file,
1504fa0d2b9bSLi Zefan 					       void __user *arg, int subvol)
1505fa0d2b9bSLi Zefan {
1506fa0d2b9bSLi Zefan 	struct btrfs_ioctl_vol_args_v2 *vol_args;
1507fa0d2b9bSLi Zefan 	int ret;
1508fdfb1e4fSLi Zefan 	u64 transid = 0;
1509fdfb1e4fSLi Zefan 	u64 *ptr = NULL;
1510b83cc969SLi Zefan 	bool readonly = false;
15116f72c7e2SArne Jansen 	struct btrfs_qgroup_inherit *inherit = NULL;
151272fd032eSSage Weil 
1513fa0d2b9bSLi Zefan 	vol_args = memdup_user(arg, sizeof(*vol_args));
1514fa0d2b9bSLi Zefan 	if (IS_ERR(vol_args))
1515fa0d2b9bSLi Zefan 		return PTR_ERR(vol_args);
1516fa0d2b9bSLi Zefan 	vol_args->name[BTRFS_SUBVOL_NAME_MAX] = '\0';
1517fdfb1e4fSLi Zefan 
1518b83cc969SLi Zefan 	if (vol_args->flags &
15196f72c7e2SArne Jansen 	    ~(BTRFS_SUBVOL_CREATE_ASYNC | BTRFS_SUBVOL_RDONLY |
15206f72c7e2SArne Jansen 	      BTRFS_SUBVOL_QGROUP_INHERIT)) {
1521b83cc969SLi Zefan 		ret = -EOPNOTSUPP;
1522fdfb1e4fSLi Zefan 		goto out;
1523fdfb1e4fSLi Zefan 	}
1524fdfb1e4fSLi Zefan 
1525fa0d2b9bSLi Zefan 	if (vol_args->flags & BTRFS_SUBVOL_CREATE_ASYNC)
1526fdfb1e4fSLi Zefan 		ptr = &transid;
1527b83cc969SLi Zefan 	if (vol_args->flags & BTRFS_SUBVOL_RDONLY)
1528b83cc969SLi Zefan 		readonly = true;
15296f72c7e2SArne Jansen 	if (vol_args->flags & BTRFS_SUBVOL_QGROUP_INHERIT) {
15306f72c7e2SArne Jansen 		if (vol_args->size > PAGE_CACHE_SIZE) {
15316f72c7e2SArne Jansen 			ret = -EINVAL;
15326f72c7e2SArne Jansen 			goto out;
15336f72c7e2SArne Jansen 		}
15346f72c7e2SArne Jansen 		inherit = memdup_user(vol_args->qgroup_inherit, vol_args->size);
15356f72c7e2SArne Jansen 		if (IS_ERR(inherit)) {
15366f72c7e2SArne Jansen 			ret = PTR_ERR(inherit);
15376f72c7e2SArne Jansen 			goto out;
15386f72c7e2SArne Jansen 		}
15396f72c7e2SArne Jansen 	}
154075eaa0e2SSage Weil 
1541fa0d2b9bSLi Zefan 	ret = btrfs_ioctl_snap_create_transid(file, vol_args->name,
15426f72c7e2SArne Jansen 					      vol_args->fd, subvol, ptr,
15436f72c7e2SArne Jansen 					      readonly, &inherit);
154475eaa0e2SSage Weil 
1545fdfb1e4fSLi Zefan 	if (ret == 0 && ptr &&
154675eaa0e2SSage Weil 	    copy_to_user(arg +
1547fdfb1e4fSLi Zefan 			 offsetof(struct btrfs_ioctl_vol_args_v2,
1548fdfb1e4fSLi Zefan 				  transid), ptr, sizeof(*ptr)))
154975eaa0e2SSage Weil 		ret = -EFAULT;
1550fdfb1e4fSLi Zefan out:
1551f46b5a66SChristoph Hellwig 	kfree(vol_args);
15526f72c7e2SArne Jansen 	kfree(inherit);
1553f46b5a66SChristoph Hellwig 	return ret;
1554f46b5a66SChristoph Hellwig }
1555f46b5a66SChristoph Hellwig 
15560caa102dSLi Zefan static noinline int btrfs_ioctl_subvol_getflags(struct file *file,
15570caa102dSLi Zefan 						void __user *arg)
15580caa102dSLi Zefan {
15590caa102dSLi Zefan 	struct inode *inode = fdentry(file)->d_inode;
15600caa102dSLi Zefan 	struct btrfs_root *root = BTRFS_I(inode)->root;
15610caa102dSLi Zefan 	int ret = 0;
15620caa102dSLi Zefan 	u64 flags = 0;
15630caa102dSLi Zefan 
156433345d01SLi Zefan 	if (btrfs_ino(inode) != BTRFS_FIRST_FREE_OBJECTID)
15650caa102dSLi Zefan 		return -EINVAL;
15660caa102dSLi Zefan 
15670caa102dSLi Zefan 	down_read(&root->fs_info->subvol_sem);
15680caa102dSLi Zefan 	if (btrfs_root_readonly(root))
15690caa102dSLi Zefan 		flags |= BTRFS_SUBVOL_RDONLY;
15700caa102dSLi Zefan 	up_read(&root->fs_info->subvol_sem);
15710caa102dSLi Zefan 
15720caa102dSLi Zefan 	if (copy_to_user(arg, &flags, sizeof(flags)))
15730caa102dSLi Zefan 		ret = -EFAULT;
15740caa102dSLi Zefan 
15750caa102dSLi Zefan 	return ret;
15760caa102dSLi Zefan }
15770caa102dSLi Zefan 
15780caa102dSLi Zefan static noinline int btrfs_ioctl_subvol_setflags(struct file *file,
15790caa102dSLi Zefan 					      void __user *arg)
15800caa102dSLi Zefan {
15810caa102dSLi Zefan 	struct inode *inode = fdentry(file)->d_inode;
15820caa102dSLi Zefan 	struct btrfs_root *root = BTRFS_I(inode)->root;
15830caa102dSLi Zefan 	struct btrfs_trans_handle *trans;
15840caa102dSLi Zefan 	u64 root_flags;
15850caa102dSLi Zefan 	u64 flags;
15860caa102dSLi Zefan 	int ret = 0;
15870caa102dSLi Zefan 
1588b9ca0664SLiu Bo 	ret = mnt_want_write_file(file);
1589b9ca0664SLiu Bo 	if (ret)
1590b9ca0664SLiu Bo 		goto out;
15910caa102dSLi Zefan 
1592b9ca0664SLiu Bo 	if (btrfs_ino(inode) != BTRFS_FIRST_FREE_OBJECTID) {
1593b9ca0664SLiu Bo 		ret = -EINVAL;
1594b9ca0664SLiu Bo 		goto out_drop_write;
1595b9ca0664SLiu Bo 	}
15960caa102dSLi Zefan 
1597b9ca0664SLiu Bo 	if (copy_from_user(&flags, arg, sizeof(flags))) {
1598b9ca0664SLiu Bo 		ret = -EFAULT;
1599b9ca0664SLiu Bo 		goto out_drop_write;
1600b9ca0664SLiu Bo 	}
16010caa102dSLi Zefan 
1602b9ca0664SLiu Bo 	if (flags & BTRFS_SUBVOL_CREATE_ASYNC) {
1603b9ca0664SLiu Bo 		ret = -EINVAL;
1604b9ca0664SLiu Bo 		goto out_drop_write;
1605b9ca0664SLiu Bo 	}
16060caa102dSLi Zefan 
1607b9ca0664SLiu Bo 	if (flags & ~BTRFS_SUBVOL_RDONLY) {
1608b9ca0664SLiu Bo 		ret = -EOPNOTSUPP;
1609b9ca0664SLiu Bo 		goto out_drop_write;
1610b9ca0664SLiu Bo 	}
16110caa102dSLi Zefan 
1612b9ca0664SLiu Bo 	if (!inode_owner_or_capable(inode)) {
1613b9ca0664SLiu Bo 		ret = -EACCES;
1614b9ca0664SLiu Bo 		goto out_drop_write;
1615b9ca0664SLiu Bo 	}
1616b4dc2b8cSLi Zefan 
16170caa102dSLi Zefan 	down_write(&root->fs_info->subvol_sem);
16180caa102dSLi Zefan 
16190caa102dSLi Zefan 	/* nothing to do */
16200caa102dSLi Zefan 	if (!!(flags & BTRFS_SUBVOL_RDONLY) == btrfs_root_readonly(root))
1621b9ca0664SLiu Bo 		goto out_drop_sem;
16220caa102dSLi Zefan 
16230caa102dSLi Zefan 	root_flags = btrfs_root_flags(&root->root_item);
16240caa102dSLi Zefan 	if (flags & BTRFS_SUBVOL_RDONLY)
16250caa102dSLi Zefan 		btrfs_set_root_flags(&root->root_item,
16260caa102dSLi Zefan 				     root_flags | BTRFS_ROOT_SUBVOL_RDONLY);
16270caa102dSLi Zefan 	else
16280caa102dSLi Zefan 		btrfs_set_root_flags(&root->root_item,
16290caa102dSLi Zefan 				     root_flags & ~BTRFS_ROOT_SUBVOL_RDONLY);
16300caa102dSLi Zefan 
16310caa102dSLi Zefan 	trans = btrfs_start_transaction(root, 1);
16320caa102dSLi Zefan 	if (IS_ERR(trans)) {
16330caa102dSLi Zefan 		ret = PTR_ERR(trans);
16340caa102dSLi Zefan 		goto out_reset;
16350caa102dSLi Zefan 	}
16360caa102dSLi Zefan 
1637b4dc2b8cSLi Zefan 	ret = btrfs_update_root(trans, root->fs_info->tree_root,
16380caa102dSLi Zefan 				&root->root_key, &root->root_item);
16390caa102dSLi Zefan 
16400caa102dSLi Zefan 	btrfs_commit_transaction(trans, root);
16410caa102dSLi Zefan out_reset:
16420caa102dSLi Zefan 	if (ret)
16430caa102dSLi Zefan 		btrfs_set_root_flags(&root->root_item, root_flags);
1644b9ca0664SLiu Bo out_drop_sem:
16450caa102dSLi Zefan 	up_write(&root->fs_info->subvol_sem);
1646b9ca0664SLiu Bo out_drop_write:
1647b9ca0664SLiu Bo 	mnt_drop_write_file(file);
1648b9ca0664SLiu Bo out:
16490caa102dSLi Zefan 	return ret;
16500caa102dSLi Zefan }
16510caa102dSLi Zefan 
165276dda93cSYan, Zheng /*
165376dda93cSYan, Zheng  * helper to check if the subvolume references other subvolumes
165476dda93cSYan, Zheng  */
165576dda93cSYan, Zheng static noinline int may_destroy_subvol(struct btrfs_root *root)
165676dda93cSYan, Zheng {
165776dda93cSYan, Zheng 	struct btrfs_path *path;
165876dda93cSYan, Zheng 	struct btrfs_key key;
165976dda93cSYan, Zheng 	int ret;
166076dda93cSYan, Zheng 
166176dda93cSYan, Zheng 	path = btrfs_alloc_path();
166276dda93cSYan, Zheng 	if (!path)
166376dda93cSYan, Zheng 		return -ENOMEM;
166476dda93cSYan, Zheng 
166576dda93cSYan, Zheng 	key.objectid = root->root_key.objectid;
166676dda93cSYan, Zheng 	key.type = BTRFS_ROOT_REF_KEY;
166776dda93cSYan, Zheng 	key.offset = (u64)-1;
166876dda93cSYan, Zheng 
166976dda93cSYan, Zheng 	ret = btrfs_search_slot(NULL, root->fs_info->tree_root,
167076dda93cSYan, Zheng 				&key, path, 0, 0);
167176dda93cSYan, Zheng 	if (ret < 0)
167276dda93cSYan, Zheng 		goto out;
167376dda93cSYan, Zheng 	BUG_ON(ret == 0);
167476dda93cSYan, Zheng 
167576dda93cSYan, Zheng 	ret = 0;
167676dda93cSYan, Zheng 	if (path->slots[0] > 0) {
167776dda93cSYan, Zheng 		path->slots[0]--;
167876dda93cSYan, Zheng 		btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
167976dda93cSYan, Zheng 		if (key.objectid == root->root_key.objectid &&
168076dda93cSYan, Zheng 		    key.type == BTRFS_ROOT_REF_KEY)
168176dda93cSYan, Zheng 			ret = -ENOTEMPTY;
168276dda93cSYan, Zheng 	}
168376dda93cSYan, Zheng out:
168476dda93cSYan, Zheng 	btrfs_free_path(path);
168576dda93cSYan, Zheng 	return ret;
168676dda93cSYan, Zheng }
168776dda93cSYan, Zheng 
1688ac8e9819SChris Mason static noinline int key_in_sk(struct btrfs_key *key,
1689ac8e9819SChris Mason 			      struct btrfs_ioctl_search_key *sk)
1690ac8e9819SChris Mason {
1691abc6e134SChris Mason 	struct btrfs_key test;
1692abc6e134SChris Mason 	int ret;
1693abc6e134SChris Mason 
1694abc6e134SChris Mason 	test.objectid = sk->min_objectid;
1695abc6e134SChris Mason 	test.type = sk->min_type;
1696abc6e134SChris Mason 	test.offset = sk->min_offset;
1697abc6e134SChris Mason 
1698abc6e134SChris Mason 	ret = btrfs_comp_cpu_keys(key, &test);
1699abc6e134SChris Mason 	if (ret < 0)
1700ac8e9819SChris Mason 		return 0;
1701abc6e134SChris Mason 
1702abc6e134SChris Mason 	test.objectid = sk->max_objectid;
1703abc6e134SChris Mason 	test.type = sk->max_type;
1704abc6e134SChris Mason 	test.offset = sk->max_offset;
1705abc6e134SChris Mason 
1706abc6e134SChris Mason 	ret = btrfs_comp_cpu_keys(key, &test);
1707abc6e134SChris Mason 	if (ret > 0)
1708ac8e9819SChris Mason 		return 0;
1709ac8e9819SChris Mason 	return 1;
1710ac8e9819SChris Mason }
1711ac8e9819SChris Mason 
1712ac8e9819SChris Mason static noinline int copy_to_sk(struct btrfs_root *root,
1713ac8e9819SChris Mason 			       struct btrfs_path *path,
1714ac8e9819SChris Mason 			       struct btrfs_key *key,
1715ac8e9819SChris Mason 			       struct btrfs_ioctl_search_key *sk,
1716ac8e9819SChris Mason 			       char *buf,
1717ac8e9819SChris Mason 			       unsigned long *sk_offset,
1718ac8e9819SChris Mason 			       int *num_found)
1719ac8e9819SChris Mason {
1720ac8e9819SChris Mason 	u64 found_transid;
1721ac8e9819SChris Mason 	struct extent_buffer *leaf;
1722ac8e9819SChris Mason 	struct btrfs_ioctl_search_header sh;
1723ac8e9819SChris Mason 	unsigned long item_off;
1724ac8e9819SChris Mason 	unsigned long item_len;
1725ac8e9819SChris Mason 	int nritems;
1726ac8e9819SChris Mason 	int i;
1727ac8e9819SChris Mason 	int slot;
1728ac8e9819SChris Mason 	int ret = 0;
1729ac8e9819SChris Mason 
1730ac8e9819SChris Mason 	leaf = path->nodes[0];
1731ac8e9819SChris Mason 	slot = path->slots[0];
1732ac8e9819SChris Mason 	nritems = btrfs_header_nritems(leaf);
1733ac8e9819SChris Mason 
1734ac8e9819SChris Mason 	if (btrfs_header_generation(leaf) > sk->max_transid) {
1735ac8e9819SChris Mason 		i = nritems;
1736ac8e9819SChris Mason 		goto advance_key;
1737ac8e9819SChris Mason 	}
1738ac8e9819SChris Mason 	found_transid = btrfs_header_generation(leaf);
1739ac8e9819SChris Mason 
1740ac8e9819SChris Mason 	for (i = slot; i < nritems; i++) {
1741ac8e9819SChris Mason 		item_off = btrfs_item_ptr_offset(leaf, i);
1742ac8e9819SChris Mason 		item_len = btrfs_item_size_nr(leaf, i);
1743ac8e9819SChris Mason 
1744ac8e9819SChris Mason 		if (item_len > BTRFS_SEARCH_ARGS_BUFSIZE)
1745ac8e9819SChris Mason 			item_len = 0;
1746ac8e9819SChris Mason 
1747ac8e9819SChris Mason 		if (sizeof(sh) + item_len + *sk_offset >
1748ac8e9819SChris Mason 		    BTRFS_SEARCH_ARGS_BUFSIZE) {
1749ac8e9819SChris Mason 			ret = 1;
1750ac8e9819SChris Mason 			goto overflow;
1751ac8e9819SChris Mason 		}
1752ac8e9819SChris Mason 
1753ac8e9819SChris Mason 		btrfs_item_key_to_cpu(leaf, key, i);
1754ac8e9819SChris Mason 		if (!key_in_sk(key, sk))
1755ac8e9819SChris Mason 			continue;
1756ac8e9819SChris Mason 
1757ac8e9819SChris Mason 		sh.objectid = key->objectid;
1758ac8e9819SChris Mason 		sh.offset = key->offset;
1759ac8e9819SChris Mason 		sh.type = key->type;
1760ac8e9819SChris Mason 		sh.len = item_len;
1761ac8e9819SChris Mason 		sh.transid = found_transid;
1762ac8e9819SChris Mason 
1763ac8e9819SChris Mason 		/* copy search result header */
1764ac8e9819SChris Mason 		memcpy(buf + *sk_offset, &sh, sizeof(sh));
1765ac8e9819SChris Mason 		*sk_offset += sizeof(sh);
1766ac8e9819SChris Mason 
1767ac8e9819SChris Mason 		if (item_len) {
1768ac8e9819SChris Mason 			char *p = buf + *sk_offset;
1769ac8e9819SChris Mason 			/* copy the item */
1770ac8e9819SChris Mason 			read_extent_buffer(leaf, p,
1771ac8e9819SChris Mason 					   item_off, item_len);
1772ac8e9819SChris Mason 			*sk_offset += item_len;
1773ac8e9819SChris Mason 		}
1774e2156867SHugo Mills 		(*num_found)++;
1775ac8e9819SChris Mason 
1776ac8e9819SChris Mason 		if (*num_found >= sk->nr_items)
1777ac8e9819SChris Mason 			break;
1778ac8e9819SChris Mason 	}
1779ac8e9819SChris Mason advance_key:
1780ac8e9819SChris Mason 	ret = 0;
1781abc6e134SChris Mason 	if (key->offset < (u64)-1 && key->offset < sk->max_offset)
1782abc6e134SChris Mason 		key->offset++;
1783abc6e134SChris Mason 	else if (key->type < (u8)-1 && key->type < sk->max_type) {
1784abc6e134SChris Mason 		key->offset = 0;
1785abc6e134SChris Mason 		key->type++;
1786abc6e134SChris Mason 	} else if (key->objectid < (u64)-1 && key->objectid < sk->max_objectid) {
1787abc6e134SChris Mason 		key->offset = 0;
1788abc6e134SChris Mason 		key->type = 0;
1789abc6e134SChris Mason 		key->objectid++;
1790abc6e134SChris Mason 	} else
1791abc6e134SChris Mason 		ret = 1;
1792ac8e9819SChris Mason overflow:
1793ac8e9819SChris Mason 	return ret;
1794ac8e9819SChris Mason }
1795ac8e9819SChris Mason 
1796ac8e9819SChris Mason static noinline int search_ioctl(struct inode *inode,
1797ac8e9819SChris Mason 				 struct btrfs_ioctl_search_args *args)
1798ac8e9819SChris Mason {
1799ac8e9819SChris Mason 	struct btrfs_root *root;
1800ac8e9819SChris Mason 	struct btrfs_key key;
1801ac8e9819SChris Mason 	struct btrfs_key max_key;
1802ac8e9819SChris Mason 	struct btrfs_path *path;
1803ac8e9819SChris Mason 	struct btrfs_ioctl_search_key *sk = &args->key;
1804ac8e9819SChris Mason 	struct btrfs_fs_info *info = BTRFS_I(inode)->root->fs_info;
1805ac8e9819SChris Mason 	int ret;
1806ac8e9819SChris Mason 	int num_found = 0;
1807ac8e9819SChris Mason 	unsigned long sk_offset = 0;
1808ac8e9819SChris Mason 
1809ac8e9819SChris Mason 	path = btrfs_alloc_path();
1810ac8e9819SChris Mason 	if (!path)
1811ac8e9819SChris Mason 		return -ENOMEM;
1812ac8e9819SChris Mason 
1813ac8e9819SChris Mason 	if (sk->tree_id == 0) {
1814ac8e9819SChris Mason 		/* search the root of the inode that was passed */
1815ac8e9819SChris Mason 		root = BTRFS_I(inode)->root;
1816ac8e9819SChris Mason 	} else {
1817ac8e9819SChris Mason 		key.objectid = sk->tree_id;
1818ac8e9819SChris Mason 		key.type = BTRFS_ROOT_ITEM_KEY;
1819ac8e9819SChris Mason 		key.offset = (u64)-1;
1820ac8e9819SChris Mason 		root = btrfs_read_fs_root_no_name(info, &key);
1821ac8e9819SChris Mason 		if (IS_ERR(root)) {
1822ac8e9819SChris Mason 			printk(KERN_ERR "could not find root %llu\n",
1823ac8e9819SChris Mason 			       sk->tree_id);
1824ac8e9819SChris Mason 			btrfs_free_path(path);
1825ac8e9819SChris Mason 			return -ENOENT;
1826ac8e9819SChris Mason 		}
1827ac8e9819SChris Mason 	}
1828ac8e9819SChris Mason 
1829ac8e9819SChris Mason 	key.objectid = sk->min_objectid;
1830ac8e9819SChris Mason 	key.type = sk->min_type;
1831ac8e9819SChris Mason 	key.offset = sk->min_offset;
1832ac8e9819SChris Mason 
1833ac8e9819SChris Mason 	max_key.objectid = sk->max_objectid;
1834ac8e9819SChris Mason 	max_key.type = sk->max_type;
1835ac8e9819SChris Mason 	max_key.offset = sk->max_offset;
1836ac8e9819SChris Mason 
1837ac8e9819SChris Mason 	path->keep_locks = 1;
1838ac8e9819SChris Mason 
1839ac8e9819SChris Mason 	while(1) {
1840ac8e9819SChris Mason 		ret = btrfs_search_forward(root, &key, &max_key, path, 0,
1841ac8e9819SChris Mason 					   sk->min_transid);
1842ac8e9819SChris Mason 		if (ret != 0) {
1843ac8e9819SChris Mason 			if (ret > 0)
1844ac8e9819SChris Mason 				ret = 0;
1845ac8e9819SChris Mason 			goto err;
1846ac8e9819SChris Mason 		}
1847ac8e9819SChris Mason 		ret = copy_to_sk(root, path, &key, sk, args->buf,
1848ac8e9819SChris Mason 				 &sk_offset, &num_found);
1849b3b4aa74SDavid Sterba 		btrfs_release_path(path);
1850ac8e9819SChris Mason 		if (ret || num_found >= sk->nr_items)
1851ac8e9819SChris Mason 			break;
1852ac8e9819SChris Mason 
1853ac8e9819SChris Mason 	}
1854ac8e9819SChris Mason 	ret = 0;
1855ac8e9819SChris Mason err:
1856ac8e9819SChris Mason 	sk->nr_items = num_found;
1857ac8e9819SChris Mason 	btrfs_free_path(path);
1858ac8e9819SChris Mason 	return ret;
1859ac8e9819SChris Mason }
1860ac8e9819SChris Mason 
1861ac8e9819SChris Mason static noinline int btrfs_ioctl_tree_search(struct file *file,
1862ac8e9819SChris Mason 					   void __user *argp)
1863ac8e9819SChris Mason {
1864ac8e9819SChris Mason 	 struct btrfs_ioctl_search_args *args;
1865ac8e9819SChris Mason 	 struct inode *inode;
1866ac8e9819SChris Mason 	 int ret;
1867ac8e9819SChris Mason 
1868ac8e9819SChris Mason 	if (!capable(CAP_SYS_ADMIN))
1869ac8e9819SChris Mason 		return -EPERM;
1870ac8e9819SChris Mason 
18712354d08fSJulia Lawall 	args = memdup_user(argp, sizeof(*args));
18722354d08fSJulia Lawall 	if (IS_ERR(args))
18732354d08fSJulia Lawall 		return PTR_ERR(args);
1874ac8e9819SChris Mason 
1875ac8e9819SChris Mason 	inode = fdentry(file)->d_inode;
1876ac8e9819SChris Mason 	ret = search_ioctl(inode, args);
1877ac8e9819SChris Mason 	if (ret == 0 && copy_to_user(argp, args, sizeof(*args)))
1878ac8e9819SChris Mason 		ret = -EFAULT;
1879ac8e9819SChris Mason 	kfree(args);
1880ac8e9819SChris Mason 	return ret;
1881ac8e9819SChris Mason }
1882ac8e9819SChris Mason 
188398d377a0STARUISI Hiroaki /*
1884ac8e9819SChris Mason  * Search INODE_REFs to identify path name of 'dirid' directory
1885ac8e9819SChris Mason  * in a 'tree_id' tree. and sets path name to 'name'.
188698d377a0STARUISI Hiroaki  */
188798d377a0STARUISI Hiroaki static noinline int btrfs_search_path_in_tree(struct btrfs_fs_info *info,
188898d377a0STARUISI Hiroaki 				u64 tree_id, u64 dirid, char *name)
188998d377a0STARUISI Hiroaki {
189098d377a0STARUISI Hiroaki 	struct btrfs_root *root;
189198d377a0STARUISI Hiroaki 	struct btrfs_key key;
1892ac8e9819SChris Mason 	char *ptr;
189398d377a0STARUISI Hiroaki 	int ret = -1;
189498d377a0STARUISI Hiroaki 	int slot;
189598d377a0STARUISI Hiroaki 	int len;
189698d377a0STARUISI Hiroaki 	int total_len = 0;
189798d377a0STARUISI Hiroaki 	struct btrfs_inode_ref *iref;
189898d377a0STARUISI Hiroaki 	struct extent_buffer *l;
189998d377a0STARUISI Hiroaki 	struct btrfs_path *path;
190098d377a0STARUISI Hiroaki 
190198d377a0STARUISI Hiroaki 	if (dirid == BTRFS_FIRST_FREE_OBJECTID) {
190298d377a0STARUISI Hiroaki 		name[0]='\0';
190398d377a0STARUISI Hiroaki 		return 0;
190498d377a0STARUISI Hiroaki 	}
190598d377a0STARUISI Hiroaki 
190698d377a0STARUISI Hiroaki 	path = btrfs_alloc_path();
190798d377a0STARUISI Hiroaki 	if (!path)
190898d377a0STARUISI Hiroaki 		return -ENOMEM;
190998d377a0STARUISI Hiroaki 
1910ac8e9819SChris Mason 	ptr = &name[BTRFS_INO_LOOKUP_PATH_MAX];
191198d377a0STARUISI Hiroaki 
191298d377a0STARUISI Hiroaki 	key.objectid = tree_id;
191398d377a0STARUISI Hiroaki 	key.type = BTRFS_ROOT_ITEM_KEY;
191498d377a0STARUISI Hiroaki 	key.offset = (u64)-1;
191598d377a0STARUISI Hiroaki 	root = btrfs_read_fs_root_no_name(info, &key);
191698d377a0STARUISI Hiroaki 	if (IS_ERR(root)) {
191798d377a0STARUISI Hiroaki 		printk(KERN_ERR "could not find root %llu\n", tree_id);
19188ad6fcabSChris Mason 		ret = -ENOENT;
19198ad6fcabSChris Mason 		goto out;
192098d377a0STARUISI Hiroaki 	}
192198d377a0STARUISI Hiroaki 
192298d377a0STARUISI Hiroaki 	key.objectid = dirid;
192398d377a0STARUISI Hiroaki 	key.type = BTRFS_INODE_REF_KEY;
19248ad6fcabSChris Mason 	key.offset = (u64)-1;
192598d377a0STARUISI Hiroaki 
192698d377a0STARUISI Hiroaki 	while(1) {
192798d377a0STARUISI Hiroaki 		ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
192898d377a0STARUISI Hiroaki 		if (ret < 0)
192998d377a0STARUISI Hiroaki 			goto out;
193098d377a0STARUISI Hiroaki 
193198d377a0STARUISI Hiroaki 		l = path->nodes[0];
193298d377a0STARUISI Hiroaki 		slot = path->slots[0];
19338ad6fcabSChris Mason 		if (ret > 0 && slot > 0)
19348ad6fcabSChris Mason 			slot--;
193598d377a0STARUISI Hiroaki 		btrfs_item_key_to_cpu(l, &key, slot);
193698d377a0STARUISI Hiroaki 
193798d377a0STARUISI Hiroaki 		if (ret > 0 && (key.objectid != dirid ||
1938ac8e9819SChris Mason 				key.type != BTRFS_INODE_REF_KEY)) {
1939ac8e9819SChris Mason 			ret = -ENOENT;
194098d377a0STARUISI Hiroaki 			goto out;
1941ac8e9819SChris Mason 		}
194298d377a0STARUISI Hiroaki 
194398d377a0STARUISI Hiroaki 		iref = btrfs_item_ptr(l, slot, struct btrfs_inode_ref);
194498d377a0STARUISI Hiroaki 		len = btrfs_inode_ref_name_len(l, iref);
194598d377a0STARUISI Hiroaki 		ptr -= len + 1;
194698d377a0STARUISI Hiroaki 		total_len += len + 1;
1947ac8e9819SChris Mason 		if (ptr < name)
194898d377a0STARUISI Hiroaki 			goto out;
194998d377a0STARUISI Hiroaki 
195098d377a0STARUISI Hiroaki 		*(ptr + len) = '/';
195198d377a0STARUISI Hiroaki 		read_extent_buffer(l, ptr,(unsigned long)(iref + 1), len);
195298d377a0STARUISI Hiroaki 
195398d377a0STARUISI Hiroaki 		if (key.offset == BTRFS_FIRST_FREE_OBJECTID)
195498d377a0STARUISI Hiroaki 			break;
195598d377a0STARUISI Hiroaki 
1956b3b4aa74SDavid Sterba 		btrfs_release_path(path);
195798d377a0STARUISI Hiroaki 		key.objectid = key.offset;
19588ad6fcabSChris Mason 		key.offset = (u64)-1;
195998d377a0STARUISI Hiroaki 		dirid = key.objectid;
196098d377a0STARUISI Hiroaki 	}
1961ac8e9819SChris Mason 	if (ptr < name)
196298d377a0STARUISI Hiroaki 		goto out;
196377906a50SLi Zefan 	memmove(name, ptr, total_len);
196498d377a0STARUISI Hiroaki 	name[total_len]='\0';
196598d377a0STARUISI Hiroaki 	ret = 0;
196698d377a0STARUISI Hiroaki out:
196798d377a0STARUISI Hiroaki 	btrfs_free_path(path);
1968ac8e9819SChris Mason 	return ret;
1969ac8e9819SChris Mason }
1970ac8e9819SChris Mason 
1971ac8e9819SChris Mason static noinline int btrfs_ioctl_ino_lookup(struct file *file,
1972ac8e9819SChris Mason 					   void __user *argp)
1973ac8e9819SChris Mason {
1974ac8e9819SChris Mason 	 struct btrfs_ioctl_ino_lookup_args *args;
1975ac8e9819SChris Mason 	 struct inode *inode;
1976ac8e9819SChris Mason 	 int ret;
1977ac8e9819SChris Mason 
1978ac8e9819SChris Mason 	if (!capable(CAP_SYS_ADMIN))
1979ac8e9819SChris Mason 		return -EPERM;
1980ac8e9819SChris Mason 
19812354d08fSJulia Lawall 	args = memdup_user(argp, sizeof(*args));
19822354d08fSJulia Lawall 	if (IS_ERR(args))
19832354d08fSJulia Lawall 		return PTR_ERR(args);
1984c2b96929SDan Carpenter 
1985ac8e9819SChris Mason 	inode = fdentry(file)->d_inode;
1986ac8e9819SChris Mason 
19871b53ac4dSChris Mason 	if (args->treeid == 0)
19881b53ac4dSChris Mason 		args->treeid = BTRFS_I(inode)->root->root_key.objectid;
19891b53ac4dSChris Mason 
1990ac8e9819SChris Mason 	ret = btrfs_search_path_in_tree(BTRFS_I(inode)->root->fs_info,
1991ac8e9819SChris Mason 					args->treeid, args->objectid,
1992ac8e9819SChris Mason 					args->name);
1993ac8e9819SChris Mason 
1994ac8e9819SChris Mason 	if (ret == 0 && copy_to_user(argp, args, sizeof(*args)))
1995ac8e9819SChris Mason 		ret = -EFAULT;
1996ac8e9819SChris Mason 
1997ac8e9819SChris Mason 	kfree(args);
199898d377a0STARUISI Hiroaki 	return ret;
199998d377a0STARUISI Hiroaki }
200098d377a0STARUISI Hiroaki 
200176dda93cSYan, Zheng static noinline int btrfs_ioctl_snap_destroy(struct file *file,
200276dda93cSYan, Zheng 					     void __user *arg)
200376dda93cSYan, Zheng {
200476dda93cSYan, Zheng 	struct dentry *parent = fdentry(file);
200576dda93cSYan, Zheng 	struct dentry *dentry;
200676dda93cSYan, Zheng 	struct inode *dir = parent->d_inode;
200776dda93cSYan, Zheng 	struct inode *inode;
200876dda93cSYan, Zheng 	struct btrfs_root *root = BTRFS_I(dir)->root;
200976dda93cSYan, Zheng 	struct btrfs_root *dest = NULL;
201076dda93cSYan, Zheng 	struct btrfs_ioctl_vol_args *vol_args;
201176dda93cSYan, Zheng 	struct btrfs_trans_handle *trans;
201276dda93cSYan, Zheng 	int namelen;
201376dda93cSYan, Zheng 	int ret;
201476dda93cSYan, Zheng 	int err = 0;
201576dda93cSYan, Zheng 
201676dda93cSYan, Zheng 	vol_args = memdup_user(arg, sizeof(*vol_args));
201776dda93cSYan, Zheng 	if (IS_ERR(vol_args))
201876dda93cSYan, Zheng 		return PTR_ERR(vol_args);
201976dda93cSYan, Zheng 
202076dda93cSYan, Zheng 	vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
202176dda93cSYan, Zheng 	namelen = strlen(vol_args->name);
202276dda93cSYan, Zheng 	if (strchr(vol_args->name, '/') ||
202376dda93cSYan, Zheng 	    strncmp(vol_args->name, "..", namelen) == 0) {
202476dda93cSYan, Zheng 		err = -EINVAL;
202576dda93cSYan, Zheng 		goto out;
202676dda93cSYan, Zheng 	}
202776dda93cSYan, Zheng 
2028a561be71SAl Viro 	err = mnt_want_write_file(file);
202976dda93cSYan, Zheng 	if (err)
203076dda93cSYan, Zheng 		goto out;
203176dda93cSYan, Zheng 
203276dda93cSYan, Zheng 	mutex_lock_nested(&dir->i_mutex, I_MUTEX_PARENT);
203376dda93cSYan, Zheng 	dentry = lookup_one_len(vol_args->name, parent, namelen);
203476dda93cSYan, Zheng 	if (IS_ERR(dentry)) {
203576dda93cSYan, Zheng 		err = PTR_ERR(dentry);
203676dda93cSYan, Zheng 		goto out_unlock_dir;
203776dda93cSYan, Zheng 	}
203876dda93cSYan, Zheng 
203976dda93cSYan, Zheng 	if (!dentry->d_inode) {
204076dda93cSYan, Zheng 		err = -ENOENT;
204176dda93cSYan, Zheng 		goto out_dput;
204276dda93cSYan, Zheng 	}
204376dda93cSYan, Zheng 
204476dda93cSYan, Zheng 	inode = dentry->d_inode;
20454260f7c7SSage Weil 	dest = BTRFS_I(inode)->root;
20464260f7c7SSage Weil 	if (!capable(CAP_SYS_ADMIN)){
20474260f7c7SSage Weil 		/*
20484260f7c7SSage Weil 		 * Regular user.  Only allow this with a special mount
20494260f7c7SSage Weil 		 * option, when the user has write+exec access to the
20504260f7c7SSage Weil 		 * subvol root, and when rmdir(2) would have been
20514260f7c7SSage Weil 		 * allowed.
20524260f7c7SSage Weil 		 *
20534260f7c7SSage Weil 		 * Note that this is _not_ check that the subvol is
20544260f7c7SSage Weil 		 * empty or doesn't contain data that we wouldn't
20554260f7c7SSage Weil 		 * otherwise be able to delete.
20564260f7c7SSage Weil 		 *
20574260f7c7SSage Weil 		 * Users who want to delete empty subvols should try
20584260f7c7SSage Weil 		 * rmdir(2).
20594260f7c7SSage Weil 		 */
20604260f7c7SSage Weil 		err = -EPERM;
20614260f7c7SSage Weil 		if (!btrfs_test_opt(root, USER_SUBVOL_RM_ALLOWED))
20624260f7c7SSage Weil 			goto out_dput;
20634260f7c7SSage Weil 
20644260f7c7SSage Weil 		/*
20654260f7c7SSage Weil 		 * Do not allow deletion if the parent dir is the same
20664260f7c7SSage Weil 		 * as the dir to be deleted.  That means the ioctl
20674260f7c7SSage Weil 		 * must be called on the dentry referencing the root
20684260f7c7SSage Weil 		 * of the subvol, not a random directory contained
20694260f7c7SSage Weil 		 * within it.
20704260f7c7SSage Weil 		 */
20714260f7c7SSage Weil 		err = -EINVAL;
20724260f7c7SSage Weil 		if (root == dest)
20734260f7c7SSage Weil 			goto out_dput;
20744260f7c7SSage Weil 
20754260f7c7SSage Weil 		err = inode_permission(inode, MAY_WRITE | MAY_EXEC);
20764260f7c7SSage Weil 		if (err)
20774260f7c7SSage Weil 			goto out_dput;
20784260f7c7SSage Weil 
20794260f7c7SSage Weil 		/* check if subvolume may be deleted by a non-root user */
20804260f7c7SSage Weil 		err = btrfs_may_delete(dir, dentry, 1);
20814260f7c7SSage Weil 		if (err)
20824260f7c7SSage Weil 			goto out_dput;
20834260f7c7SSage Weil 	}
20844260f7c7SSage Weil 
208533345d01SLi Zefan 	if (btrfs_ino(inode) != BTRFS_FIRST_FREE_OBJECTID) {
208676dda93cSYan, Zheng 		err = -EINVAL;
208776dda93cSYan, Zheng 		goto out_dput;
208876dda93cSYan, Zheng 	}
208976dda93cSYan, Zheng 
209076dda93cSYan, Zheng 	mutex_lock(&inode->i_mutex);
209176dda93cSYan, Zheng 	err = d_invalidate(dentry);
209276dda93cSYan, Zheng 	if (err)
209376dda93cSYan, Zheng 		goto out_unlock;
209476dda93cSYan, Zheng 
209576dda93cSYan, Zheng 	down_write(&root->fs_info->subvol_sem);
209676dda93cSYan, Zheng 
209776dda93cSYan, Zheng 	err = may_destroy_subvol(dest);
209876dda93cSYan, Zheng 	if (err)
209976dda93cSYan, Zheng 		goto out_up_write;
210076dda93cSYan, Zheng 
2101a22285a6SYan, Zheng 	trans = btrfs_start_transaction(root, 0);
2102a22285a6SYan, Zheng 	if (IS_ERR(trans)) {
2103a22285a6SYan, Zheng 		err = PTR_ERR(trans);
2104d327099aSDan Carpenter 		goto out_up_write;
2105a22285a6SYan, Zheng 	}
2106a22285a6SYan, Zheng 	trans->block_rsv = &root->fs_info->global_block_rsv;
2107a22285a6SYan, Zheng 
210876dda93cSYan, Zheng 	ret = btrfs_unlink_subvol(trans, root, dir,
210976dda93cSYan, Zheng 				dest->root_key.objectid,
211076dda93cSYan, Zheng 				dentry->d_name.name,
211176dda93cSYan, Zheng 				dentry->d_name.len);
211279787eaaSJeff Mahoney 	if (ret) {
211379787eaaSJeff Mahoney 		err = ret;
211479787eaaSJeff Mahoney 		btrfs_abort_transaction(trans, root, ret);
211579787eaaSJeff Mahoney 		goto out_end_trans;
211679787eaaSJeff Mahoney 	}
211776dda93cSYan, Zheng 
211876dda93cSYan, Zheng 	btrfs_record_root_in_trans(trans, dest);
211976dda93cSYan, Zheng 
212076dda93cSYan, Zheng 	memset(&dest->root_item.drop_progress, 0,
212176dda93cSYan, Zheng 		sizeof(dest->root_item.drop_progress));
212276dda93cSYan, Zheng 	dest->root_item.drop_level = 0;
212376dda93cSYan, Zheng 	btrfs_set_root_refs(&dest->root_item, 0);
212476dda93cSYan, Zheng 
2125d68fc57bSYan, Zheng 	if (!xchg(&dest->orphan_item_inserted, 1)) {
212676dda93cSYan, Zheng 		ret = btrfs_insert_orphan_item(trans,
212776dda93cSYan, Zheng 					root->fs_info->tree_root,
212876dda93cSYan, Zheng 					dest->root_key.objectid);
212979787eaaSJeff Mahoney 		if (ret) {
213079787eaaSJeff Mahoney 			btrfs_abort_transaction(trans, root, ret);
213179787eaaSJeff Mahoney 			err = ret;
213279787eaaSJeff Mahoney 			goto out_end_trans;
2133d68fc57bSYan, Zheng 		}
213479787eaaSJeff Mahoney 	}
213579787eaaSJeff Mahoney out_end_trans:
2136531cb13fSSage Weil 	ret = btrfs_end_transaction(trans, root);
213779787eaaSJeff Mahoney 	if (ret && !err)
213879787eaaSJeff Mahoney 		err = ret;
213976dda93cSYan, Zheng 	inode->i_flags |= S_DEAD;
214076dda93cSYan, Zheng out_up_write:
214176dda93cSYan, Zheng 	up_write(&root->fs_info->subvol_sem);
214276dda93cSYan, Zheng out_unlock:
214376dda93cSYan, Zheng 	mutex_unlock(&inode->i_mutex);
214476dda93cSYan, Zheng 	if (!err) {
2145efefb143SYan, Zheng 		shrink_dcache_sb(root->fs_info->sb);
214676dda93cSYan, Zheng 		btrfs_invalidate_inodes(dest);
214776dda93cSYan, Zheng 		d_delete(dentry);
214876dda93cSYan, Zheng 	}
214976dda93cSYan, Zheng out_dput:
215076dda93cSYan, Zheng 	dput(dentry);
215176dda93cSYan, Zheng out_unlock_dir:
215276dda93cSYan, Zheng 	mutex_unlock(&dir->i_mutex);
21532a79f17eSAl Viro 	mnt_drop_write_file(file);
215476dda93cSYan, Zheng out:
215576dda93cSYan, Zheng 	kfree(vol_args);
215676dda93cSYan, Zheng 	return err;
215776dda93cSYan, Zheng }
215876dda93cSYan, Zheng 
21591e701a32SChris Mason static int btrfs_ioctl_defrag(struct file *file, void __user *argp)
2160f46b5a66SChristoph Hellwig {
2161f46b5a66SChristoph Hellwig 	struct inode *inode = fdentry(file)->d_inode;
2162f46b5a66SChristoph Hellwig 	struct btrfs_root *root = BTRFS_I(inode)->root;
21631e701a32SChris Mason 	struct btrfs_ioctl_defrag_range_args *range;
2164c146afadSYan Zheng 	int ret;
2165c146afadSYan Zheng 
2166b83cc969SLi Zefan 	if (btrfs_root_readonly(root))
2167b83cc969SLi Zefan 		return -EROFS;
2168b83cc969SLi Zefan 
21695ac00addSStefan Behrens 	if (atomic_xchg(&root->fs_info->mutually_exclusive_operation_running,
21705ac00addSStefan Behrens 			1)) {
21715ac00addSStefan Behrens 		pr_info("btrfs: dev add/delete/balance/replace/resize operation in progress\n");
21725ac00addSStefan Behrens 		return -EINPROGRESS;
21735ac00addSStefan Behrens 	}
2174a561be71SAl Viro 	ret = mnt_want_write_file(file);
21755ac00addSStefan Behrens 	if (ret) {
21765ac00addSStefan Behrens 		atomic_set(&root->fs_info->mutually_exclusive_operation_running,
21775ac00addSStefan Behrens 			   0);
2178c146afadSYan Zheng 		return ret;
21795ac00addSStefan Behrens 	}
2180f46b5a66SChristoph Hellwig 
2181f46b5a66SChristoph Hellwig 	switch (inode->i_mode & S_IFMT) {
2182f46b5a66SChristoph Hellwig 	case S_IFDIR:
2183e441d54dSChris Mason 		if (!capable(CAP_SYS_ADMIN)) {
2184e441d54dSChris Mason 			ret = -EPERM;
2185e441d54dSChris Mason 			goto out;
2186e441d54dSChris Mason 		}
21878929ecfaSYan, Zheng 		ret = btrfs_defrag_root(root, 0);
21888929ecfaSYan, Zheng 		if (ret)
21898929ecfaSYan, Zheng 			goto out;
21908929ecfaSYan, Zheng 		ret = btrfs_defrag_root(root->fs_info->extent_root, 0);
2191f46b5a66SChristoph Hellwig 		break;
2192f46b5a66SChristoph Hellwig 	case S_IFREG:
2193e441d54dSChris Mason 		if (!(file->f_mode & FMODE_WRITE)) {
2194e441d54dSChris Mason 			ret = -EINVAL;
2195e441d54dSChris Mason 			goto out;
2196e441d54dSChris Mason 		}
21971e701a32SChris Mason 
21981e701a32SChris Mason 		range = kzalloc(sizeof(*range), GFP_KERNEL);
21991e701a32SChris Mason 		if (!range) {
22001e701a32SChris Mason 			ret = -ENOMEM;
22011e701a32SChris Mason 			goto out;
22021e701a32SChris Mason 		}
22031e701a32SChris Mason 
22041e701a32SChris Mason 		if (argp) {
22051e701a32SChris Mason 			if (copy_from_user(range, argp,
22061e701a32SChris Mason 					   sizeof(*range))) {
22071e701a32SChris Mason 				ret = -EFAULT;
22081e701a32SChris Mason 				kfree(range);
2209683be16eSDan Carpenter 				goto out;
22101e701a32SChris Mason 			}
22111e701a32SChris Mason 			/* compression requires us to start the IO */
22121e701a32SChris Mason 			if ((range->flags & BTRFS_DEFRAG_RANGE_COMPRESS)) {
22131e701a32SChris Mason 				range->flags |= BTRFS_DEFRAG_RANGE_START_IO;
22141e701a32SChris Mason 				range->extent_thresh = (u32)-1;
22151e701a32SChris Mason 			}
22161e701a32SChris Mason 		} else {
22171e701a32SChris Mason 			/* the rest are all set to zero by kzalloc */
22181e701a32SChris Mason 			range->len = (u64)-1;
22191e701a32SChris Mason 		}
22204cb5300bSChris Mason 		ret = btrfs_defrag_file(fdentry(file)->d_inode, file,
22214cb5300bSChris Mason 					range, 0, 0);
22224cb5300bSChris Mason 		if (ret > 0)
22234cb5300bSChris Mason 			ret = 0;
22241e701a32SChris Mason 		kfree(range);
2225f46b5a66SChristoph Hellwig 		break;
22268929ecfaSYan, Zheng 	default:
22278929ecfaSYan, Zheng 		ret = -EINVAL;
2228f46b5a66SChristoph Hellwig 	}
2229e441d54dSChris Mason out:
22302a79f17eSAl Viro 	mnt_drop_write_file(file);
22315ac00addSStefan Behrens 	atomic_set(&root->fs_info->mutually_exclusive_operation_running, 0);
2232e441d54dSChris Mason 	return ret;
2233f46b5a66SChristoph Hellwig }
2234f46b5a66SChristoph Hellwig 
2235b2950863SChristoph Hellwig static long btrfs_ioctl_add_dev(struct btrfs_root *root, void __user *arg)
2236f46b5a66SChristoph Hellwig {
2237f46b5a66SChristoph Hellwig 	struct btrfs_ioctl_vol_args *vol_args;
2238f46b5a66SChristoph Hellwig 	int ret;
2239f46b5a66SChristoph Hellwig 
2240e441d54dSChris Mason 	if (!capable(CAP_SYS_ADMIN))
2241e441d54dSChris Mason 		return -EPERM;
2242e441d54dSChris Mason 
22435ac00addSStefan Behrens 	if (atomic_xchg(&root->fs_info->mutually_exclusive_operation_running,
22445ac00addSStefan Behrens 			1)) {
22455ac00addSStefan Behrens 		pr_info("btrfs: dev add/delete/balance/replace/resize operation in progress\n");
22465ac00addSStefan Behrens 		return -EINPROGRESS;
2247c9e9f97bSIlya Dryomov 	}
2248c9e9f97bSIlya Dryomov 
22495ac00addSStefan Behrens 	mutex_lock(&root->fs_info->volume_mutex);
2250dae7b665SLi Zefan 	vol_args = memdup_user(arg, sizeof(*vol_args));
2251c9e9f97bSIlya Dryomov 	if (IS_ERR(vol_args)) {
2252c9e9f97bSIlya Dryomov 		ret = PTR_ERR(vol_args);
2253c9e9f97bSIlya Dryomov 		goto out;
2254c9e9f97bSIlya Dryomov 	}
2255f46b5a66SChristoph Hellwig 
22565516e595SMark Fasheh 	vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
2257f46b5a66SChristoph Hellwig 	ret = btrfs_init_new_device(root, vol_args->name);
2258f46b5a66SChristoph Hellwig 
2259f46b5a66SChristoph Hellwig 	kfree(vol_args);
2260c9e9f97bSIlya Dryomov out:
2261c9e9f97bSIlya Dryomov 	mutex_unlock(&root->fs_info->volume_mutex);
22625ac00addSStefan Behrens 	atomic_set(&root->fs_info->mutually_exclusive_operation_running, 0);
2263f46b5a66SChristoph Hellwig 	return ret;
2264f46b5a66SChristoph Hellwig }
2265f46b5a66SChristoph Hellwig 
2266b2950863SChristoph Hellwig static long btrfs_ioctl_rm_dev(struct btrfs_root *root, void __user *arg)
2267f46b5a66SChristoph Hellwig {
2268f46b5a66SChristoph Hellwig 	struct btrfs_ioctl_vol_args *vol_args;
2269f46b5a66SChristoph Hellwig 	int ret;
2270f46b5a66SChristoph Hellwig 
2271e441d54dSChris Mason 	if (!capable(CAP_SYS_ADMIN))
2272e441d54dSChris Mason 		return -EPERM;
2273e441d54dSChris Mason 
2274c146afadSYan Zheng 	if (root->fs_info->sb->s_flags & MS_RDONLY)
2275c146afadSYan Zheng 		return -EROFS;
2276c146afadSYan Zheng 
22775ac00addSStefan Behrens 	if (atomic_xchg(&root->fs_info->mutually_exclusive_operation_running,
22785ac00addSStefan Behrens 			1)) {
22795ac00addSStefan Behrens 		pr_info("btrfs: dev add/delete/balance/replace/resize operation in progress\n");
22805ac00addSStefan Behrens 		return -EINPROGRESS;
2281c9e9f97bSIlya Dryomov 	}
2282c9e9f97bSIlya Dryomov 
22835ac00addSStefan Behrens 	mutex_lock(&root->fs_info->volume_mutex);
2284dae7b665SLi Zefan 	vol_args = memdup_user(arg, sizeof(*vol_args));
2285c9e9f97bSIlya Dryomov 	if (IS_ERR(vol_args)) {
2286c9e9f97bSIlya Dryomov 		ret = PTR_ERR(vol_args);
2287c9e9f97bSIlya Dryomov 		goto out;
2288c9e9f97bSIlya Dryomov 	}
2289f46b5a66SChristoph Hellwig 
22905516e595SMark Fasheh 	vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
2291f46b5a66SChristoph Hellwig 	ret = btrfs_rm_device(root, vol_args->name);
2292f46b5a66SChristoph Hellwig 
2293f46b5a66SChristoph Hellwig 	kfree(vol_args);
2294c9e9f97bSIlya Dryomov out:
2295c9e9f97bSIlya Dryomov 	mutex_unlock(&root->fs_info->volume_mutex);
22965ac00addSStefan Behrens 	atomic_set(&root->fs_info->mutually_exclusive_operation_running, 0);
2297f46b5a66SChristoph Hellwig 	return ret;
2298f46b5a66SChristoph Hellwig }
2299f46b5a66SChristoph Hellwig 
2300475f6387SJan Schmidt static long btrfs_ioctl_fs_info(struct btrfs_root *root, void __user *arg)
2301475f6387SJan Schmidt {
2302027ed2f0SLi Zefan 	struct btrfs_ioctl_fs_info_args *fi_args;
2303475f6387SJan Schmidt 	struct btrfs_device *device;
2304475f6387SJan Schmidt 	struct btrfs_device *next;
2305475f6387SJan Schmidt 	struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices;
2306027ed2f0SLi Zefan 	int ret = 0;
2307475f6387SJan Schmidt 
2308475f6387SJan Schmidt 	if (!capable(CAP_SYS_ADMIN))
2309475f6387SJan Schmidt 		return -EPERM;
2310475f6387SJan Schmidt 
2311027ed2f0SLi Zefan 	fi_args = kzalloc(sizeof(*fi_args), GFP_KERNEL);
2312027ed2f0SLi Zefan 	if (!fi_args)
2313027ed2f0SLi Zefan 		return -ENOMEM;
2314027ed2f0SLi Zefan 
2315027ed2f0SLi Zefan 	fi_args->num_devices = fs_devices->num_devices;
2316027ed2f0SLi Zefan 	memcpy(&fi_args->fsid, root->fs_info->fsid, sizeof(fi_args->fsid));
2317475f6387SJan Schmidt 
2318475f6387SJan Schmidt 	mutex_lock(&fs_devices->device_list_mutex);
2319475f6387SJan Schmidt 	list_for_each_entry_safe(device, next, &fs_devices->devices, dev_list) {
2320027ed2f0SLi Zefan 		if (device->devid > fi_args->max_id)
2321027ed2f0SLi Zefan 			fi_args->max_id = device->devid;
2322475f6387SJan Schmidt 	}
2323475f6387SJan Schmidt 	mutex_unlock(&fs_devices->device_list_mutex);
2324475f6387SJan Schmidt 
2325027ed2f0SLi Zefan 	if (copy_to_user(arg, fi_args, sizeof(*fi_args)))
2326027ed2f0SLi Zefan 		ret = -EFAULT;
2327475f6387SJan Schmidt 
2328027ed2f0SLi Zefan 	kfree(fi_args);
2329027ed2f0SLi Zefan 	return ret;
2330475f6387SJan Schmidt }
2331475f6387SJan Schmidt 
2332475f6387SJan Schmidt static long btrfs_ioctl_dev_info(struct btrfs_root *root, void __user *arg)
2333475f6387SJan Schmidt {
2334475f6387SJan Schmidt 	struct btrfs_ioctl_dev_info_args *di_args;
2335475f6387SJan Schmidt 	struct btrfs_device *dev;
2336475f6387SJan Schmidt 	struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices;
2337475f6387SJan Schmidt 	int ret = 0;
2338475f6387SJan Schmidt 	char *s_uuid = NULL;
2339475f6387SJan Schmidt 	char empty_uuid[BTRFS_UUID_SIZE] = {0};
2340475f6387SJan Schmidt 
2341475f6387SJan Schmidt 	if (!capable(CAP_SYS_ADMIN))
2342475f6387SJan Schmidt 		return -EPERM;
2343475f6387SJan Schmidt 
2344475f6387SJan Schmidt 	di_args = memdup_user(arg, sizeof(*di_args));
2345475f6387SJan Schmidt 	if (IS_ERR(di_args))
2346475f6387SJan Schmidt 		return PTR_ERR(di_args);
2347475f6387SJan Schmidt 
2348475f6387SJan Schmidt 	if (memcmp(empty_uuid, di_args->uuid, BTRFS_UUID_SIZE) != 0)
2349475f6387SJan Schmidt 		s_uuid = di_args->uuid;
2350475f6387SJan Schmidt 
2351475f6387SJan Schmidt 	mutex_lock(&fs_devices->device_list_mutex);
2352aa1b8cd4SStefan Behrens 	dev = btrfs_find_device(root->fs_info, di_args->devid, s_uuid, NULL);
2353475f6387SJan Schmidt 	mutex_unlock(&fs_devices->device_list_mutex);
2354475f6387SJan Schmidt 
2355475f6387SJan Schmidt 	if (!dev) {
2356475f6387SJan Schmidt 		ret = -ENODEV;
2357475f6387SJan Schmidt 		goto out;
2358475f6387SJan Schmidt 	}
2359475f6387SJan Schmidt 
2360475f6387SJan Schmidt 	di_args->devid = dev->devid;
2361475f6387SJan Schmidt 	di_args->bytes_used = dev->bytes_used;
2362475f6387SJan Schmidt 	di_args->total_bytes = dev->total_bytes;
2363475f6387SJan Schmidt 	memcpy(di_args->uuid, dev->uuid, sizeof(di_args->uuid));
2364a27202fbSJim Meyering 	if (dev->name) {
2365606686eeSJosef Bacik 		struct rcu_string *name;
2366606686eeSJosef Bacik 
2367606686eeSJosef Bacik 		rcu_read_lock();
2368606686eeSJosef Bacik 		name = rcu_dereference(dev->name);
2369606686eeSJosef Bacik 		strncpy(di_args->path, name->str, sizeof(di_args->path));
2370606686eeSJosef Bacik 		rcu_read_unlock();
2371a27202fbSJim Meyering 		di_args->path[sizeof(di_args->path) - 1] = 0;
2372a27202fbSJim Meyering 	} else {
237399ba55adSStefan Behrens 		di_args->path[0] = '\0';
2374a27202fbSJim Meyering 	}
2375475f6387SJan Schmidt 
2376475f6387SJan Schmidt out:
2377475f6387SJan Schmidt 	if (ret == 0 && copy_to_user(arg, di_args, sizeof(*di_args)))
2378475f6387SJan Schmidt 		ret = -EFAULT;
2379475f6387SJan Schmidt 
2380475f6387SJan Schmidt 	kfree(di_args);
2381475f6387SJan Schmidt 	return ret;
2382475f6387SJan Schmidt }
2383475f6387SJan Schmidt 
238476dda93cSYan, Zheng static noinline long btrfs_ioctl_clone(struct file *file, unsigned long srcfd,
2385b2950863SChristoph Hellwig 				       u64 off, u64 olen, u64 destoff)
2386f46b5a66SChristoph Hellwig {
2387f46b5a66SChristoph Hellwig 	struct inode *inode = fdentry(file)->d_inode;
2388f46b5a66SChristoph Hellwig 	struct btrfs_root *root = BTRFS_I(inode)->root;
23892903ff01SAl Viro 	struct fd src_file;
2390f46b5a66SChristoph Hellwig 	struct inode *src;
2391f46b5a66SChristoph Hellwig 	struct btrfs_trans_handle *trans;
2392f46b5a66SChristoph Hellwig 	struct btrfs_path *path;
2393f46b5a66SChristoph Hellwig 	struct extent_buffer *leaf;
2394ae01a0abSYan Zheng 	char *buf;
2395ae01a0abSYan Zheng 	struct btrfs_key key;
2396f46b5a66SChristoph Hellwig 	u32 nritems;
2397f46b5a66SChristoph Hellwig 	int slot;
2398ae01a0abSYan Zheng 	int ret;
2399c5c9cd4dSSage Weil 	u64 len = olen;
2400c5c9cd4dSSage Weil 	u64 bs = root->fs_info->sb->s_blocksize;
2401d20f7043SChris Mason 
2402c5c9cd4dSSage Weil 	/*
2403c5c9cd4dSSage Weil 	 * TODO:
2404c5c9cd4dSSage Weil 	 * - split compressed inline extents.  annoying: we need to
2405c5c9cd4dSSage Weil 	 *   decompress into destination's address_space (the file offset
2406c5c9cd4dSSage Weil 	 *   may change, so source mapping won't do), then recompress (or
2407c5c9cd4dSSage Weil 	 *   otherwise reinsert) a subrange.
2408c5c9cd4dSSage Weil 	 * - allow ranges within the same file to be cloned (provided
2409c5c9cd4dSSage Weil 	 *   they don't overlap)?
2410c5c9cd4dSSage Weil 	 */
2411c5c9cd4dSSage Weil 
2412e441d54dSChris Mason 	/* the destination must be opened for writing */
24132ebc3464SDan Rosenberg 	if (!(file->f_mode & FMODE_WRITE) || (file->f_flags & O_APPEND))
2414e441d54dSChris Mason 		return -EINVAL;
2415e441d54dSChris Mason 
2416b83cc969SLi Zefan 	if (btrfs_root_readonly(root))
2417b83cc969SLi Zefan 		return -EROFS;
2418b83cc969SLi Zefan 
2419a561be71SAl Viro 	ret = mnt_want_write_file(file);
2420c146afadSYan Zheng 	if (ret)
2421c146afadSYan Zheng 		return ret;
2422c146afadSYan Zheng 
24232903ff01SAl Viro 	src_file = fdget(srcfd);
24242903ff01SAl Viro 	if (!src_file.file) {
2425ab67b7c1SYan Zheng 		ret = -EBADF;
2426ab67b7c1SYan Zheng 		goto out_drop_write;
2427ab67b7c1SYan Zheng 	}
24285dc64164SDan Rosenberg 
2429362a20c5SDavid Sterba 	ret = -EXDEV;
24302903ff01SAl Viro 	if (src_file.file->f_path.mnt != file->f_path.mnt)
2431362a20c5SDavid Sterba 		goto out_fput;
2432362a20c5SDavid Sterba 
24332903ff01SAl Viro 	src = src_file.file->f_dentry->d_inode;
2434f46b5a66SChristoph Hellwig 
2435c5c9cd4dSSage Weil 	ret = -EINVAL;
2436c5c9cd4dSSage Weil 	if (src == inode)
2437c5c9cd4dSSage Weil 		goto out_fput;
2438c5c9cd4dSSage Weil 
24395dc64164SDan Rosenberg 	/* the src must be open for reading */
24402903ff01SAl Viro 	if (!(src_file.file->f_mode & FMODE_READ))
24415dc64164SDan Rosenberg 		goto out_fput;
24425dc64164SDan Rosenberg 
24430e7b824cSLi Zefan 	/* don't make the dst file partly checksummed */
24440e7b824cSLi Zefan 	if ((BTRFS_I(src)->flags & BTRFS_INODE_NODATASUM) !=
24450e7b824cSLi Zefan 	    (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM))
24460e7b824cSLi Zefan 		goto out_fput;
24470e7b824cSLi Zefan 
2448ae01a0abSYan Zheng 	ret = -EISDIR;
2449ae01a0abSYan Zheng 	if (S_ISDIR(src->i_mode) || S_ISDIR(inode->i_mode))
2450f46b5a66SChristoph Hellwig 		goto out_fput;
2451f46b5a66SChristoph Hellwig 
2452ae01a0abSYan Zheng 	ret = -EXDEV;
2453362a20c5SDavid Sterba 	if (src->i_sb != inode->i_sb)
2454ae01a0abSYan Zheng 		goto out_fput;
2455ae01a0abSYan Zheng 
2456ae01a0abSYan Zheng 	ret = -ENOMEM;
2457ae01a0abSYan Zheng 	buf = vmalloc(btrfs_level_size(root, 0));
2458ae01a0abSYan Zheng 	if (!buf)
2459ae01a0abSYan Zheng 		goto out_fput;
2460ae01a0abSYan Zheng 
2461ae01a0abSYan Zheng 	path = btrfs_alloc_path();
2462ae01a0abSYan Zheng 	if (!path) {
2463ae01a0abSYan Zheng 		vfree(buf);
2464ae01a0abSYan Zheng 		goto out_fput;
2465ae01a0abSYan Zheng 	}
2466ae01a0abSYan Zheng 	path->reada = 2;
2467ae01a0abSYan Zheng 
2468f46b5a66SChristoph Hellwig 	if (inode < src) {
2469fccdae43SSage Weil 		mutex_lock_nested(&inode->i_mutex, I_MUTEX_PARENT);
2470fccdae43SSage Weil 		mutex_lock_nested(&src->i_mutex, I_MUTEX_CHILD);
2471f46b5a66SChristoph Hellwig 	} else {
2472fccdae43SSage Weil 		mutex_lock_nested(&src->i_mutex, I_MUTEX_PARENT);
2473fccdae43SSage Weil 		mutex_lock_nested(&inode->i_mutex, I_MUTEX_CHILD);
2474f46b5a66SChristoph Hellwig 	}
2475f46b5a66SChristoph Hellwig 
2476c5c9cd4dSSage Weil 	/* determine range to clone */
2477c5c9cd4dSSage Weil 	ret = -EINVAL;
24782ebc3464SDan Rosenberg 	if (off + len > src->i_size || off + len < off)
2479f46b5a66SChristoph Hellwig 		goto out_unlock;
2480c5c9cd4dSSage Weil 	if (len == 0)
2481c5c9cd4dSSage Weil 		olen = len = src->i_size - off;
2482c5c9cd4dSSage Weil 	/* if we extend to eof, continue to block boundary */
2483c5c9cd4dSSage Weil 	if (off + len == src->i_size)
24842a6b8daeSLi Zefan 		len = ALIGN(src->i_size, bs) - off;
2485c5c9cd4dSSage Weil 
2486c5c9cd4dSSage Weil 	/* verify the end result is block aligned */
24872a6b8daeSLi Zefan 	if (!IS_ALIGNED(off, bs) || !IS_ALIGNED(off + len, bs) ||
24882a6b8daeSLi Zefan 	    !IS_ALIGNED(destoff, bs))
2489c5c9cd4dSSage Weil 		goto out_unlock;
2490c5c9cd4dSSage Weil 
2491d525e8abSLi Zefan 	if (destoff > inode->i_size) {
2492d525e8abSLi Zefan 		ret = btrfs_cont_expand(inode, inode->i_size, destoff);
2493d525e8abSLi Zefan 		if (ret)
2494d525e8abSLi Zefan 			goto out_unlock;
2495d525e8abSLi Zefan 	}
2496d525e8abSLi Zefan 
249771ef0786SLi Zefan 	/* truncate page cache pages from target inode range */
249871ef0786SLi Zefan 	truncate_inode_pages_range(&inode->i_data, destoff,
249971ef0786SLi Zefan 				   PAGE_CACHE_ALIGN(destoff + len) - 1);
250071ef0786SLi Zefan 
2501f46b5a66SChristoph Hellwig 	/* do any pending delalloc/csum calc on src, one way or
2502f46b5a66SChristoph Hellwig 	   another, and lock file content */
2503f46b5a66SChristoph Hellwig 	while (1) {
250431840ae1SZheng Yan 		struct btrfs_ordered_extent *ordered;
2505aa42ffd9SLiu Bo 		lock_extent(&BTRFS_I(src)->io_tree, off, off + len - 1);
2506aa42ffd9SLiu Bo 		ordered = btrfs_lookup_first_ordered_extent(src, off + len - 1);
25079a019196SSage Weil 		if (!ordered &&
2508aa42ffd9SLiu Bo 		    !test_range_bit(&BTRFS_I(src)->io_tree, off, off + len - 1,
25099a019196SSage Weil 				    EXTENT_DELALLOC, 0, NULL))
2510f46b5a66SChristoph Hellwig 			break;
2511aa42ffd9SLiu Bo 		unlock_extent(&BTRFS_I(src)->io_tree, off, off + len - 1);
2512ae01a0abSYan Zheng 		if (ordered)
2513ae01a0abSYan Zheng 			btrfs_put_ordered_extent(ordered);
25149a019196SSage Weil 		btrfs_wait_ordered_range(src, off, len);
2515f46b5a66SChristoph Hellwig 	}
2516f46b5a66SChristoph Hellwig 
2517c5c9cd4dSSage Weil 	/* clone data */
251833345d01SLi Zefan 	key.objectid = btrfs_ino(src);
2519ae01a0abSYan Zheng 	key.type = BTRFS_EXTENT_DATA_KEY;
2520ae01a0abSYan Zheng 	key.offset = 0;
2521f46b5a66SChristoph Hellwig 
2522f46b5a66SChristoph Hellwig 	while (1) {
2523f46b5a66SChristoph Hellwig 		/*
2524f46b5a66SChristoph Hellwig 		 * note the key will change type as we walk through the
2525f46b5a66SChristoph Hellwig 		 * tree.
2526f46b5a66SChristoph Hellwig 		 */
2527362a20c5SDavid Sterba 		ret = btrfs_search_slot(NULL, BTRFS_I(src)->root, &key, path,
2528362a20c5SDavid Sterba 				0, 0);
2529f46b5a66SChristoph Hellwig 		if (ret < 0)
2530f46b5a66SChristoph Hellwig 			goto out;
2531f46b5a66SChristoph Hellwig 
2532ae01a0abSYan Zheng 		nritems = btrfs_header_nritems(path->nodes[0]);
2533ae01a0abSYan Zheng 		if (path->slots[0] >= nritems) {
2534362a20c5SDavid Sterba 			ret = btrfs_next_leaf(BTRFS_I(src)->root, path);
2535f46b5a66SChristoph Hellwig 			if (ret < 0)
2536f46b5a66SChristoph Hellwig 				goto out;
2537f46b5a66SChristoph Hellwig 			if (ret > 0)
2538f46b5a66SChristoph Hellwig 				break;
2539ae01a0abSYan Zheng 			nritems = btrfs_header_nritems(path->nodes[0]);
2540f46b5a66SChristoph Hellwig 		}
2541f46b5a66SChristoph Hellwig 		leaf = path->nodes[0];
2542f46b5a66SChristoph Hellwig 		slot = path->slots[0];
2543f46b5a66SChristoph Hellwig 
2544ae01a0abSYan Zheng 		btrfs_item_key_to_cpu(leaf, &key, slot);
2545d20f7043SChris Mason 		if (btrfs_key_type(&key) > BTRFS_EXTENT_DATA_KEY ||
254633345d01SLi Zefan 		    key.objectid != btrfs_ino(src))
2547f46b5a66SChristoph Hellwig 			break;
2548f46b5a66SChristoph Hellwig 
2549c5c9cd4dSSage Weil 		if (btrfs_key_type(&key) == BTRFS_EXTENT_DATA_KEY) {
2550c5c9cd4dSSage Weil 			struct btrfs_file_extent_item *extent;
2551c5c9cd4dSSage Weil 			int type;
255231840ae1SZheng Yan 			u32 size;
255331840ae1SZheng Yan 			struct btrfs_key new_key;
2554c5c9cd4dSSage Weil 			u64 disko = 0, diskl = 0;
2555c5c9cd4dSSage Weil 			u64 datao = 0, datal = 0;
2556c5c9cd4dSSage Weil 			u8 comp;
2557b5384d48SSage Weil 			u64 endoff;
255831840ae1SZheng Yan 
255931840ae1SZheng Yan 			size = btrfs_item_size_nr(leaf, slot);
256031840ae1SZheng Yan 			read_extent_buffer(leaf, buf,
256131840ae1SZheng Yan 					   btrfs_item_ptr_offset(leaf, slot),
256231840ae1SZheng Yan 					   size);
2563c5c9cd4dSSage Weil 
2564c5c9cd4dSSage Weil 			extent = btrfs_item_ptr(leaf, slot,
2565c5c9cd4dSSage Weil 						struct btrfs_file_extent_item);
2566c5c9cd4dSSage Weil 			comp = btrfs_file_extent_compression(leaf, extent);
2567c5c9cd4dSSage Weil 			type = btrfs_file_extent_type(leaf, extent);
2568c8a894d7SChris Mason 			if (type == BTRFS_FILE_EXTENT_REG ||
2569c8a894d7SChris Mason 			    type == BTRFS_FILE_EXTENT_PREALLOC) {
2570d397712bSChris Mason 				disko = btrfs_file_extent_disk_bytenr(leaf,
2571d397712bSChris Mason 								      extent);
2572d397712bSChris Mason 				diskl = btrfs_file_extent_disk_num_bytes(leaf,
2573d397712bSChris Mason 								 extent);
2574c5c9cd4dSSage Weil 				datao = btrfs_file_extent_offset(leaf, extent);
2575d397712bSChris Mason 				datal = btrfs_file_extent_num_bytes(leaf,
2576d397712bSChris Mason 								    extent);
2577c5c9cd4dSSage Weil 			} else if (type == BTRFS_FILE_EXTENT_INLINE) {
2578c5c9cd4dSSage Weil 				/* take upper bound, may be compressed */
2579c5c9cd4dSSage Weil 				datal = btrfs_file_extent_ram_bytes(leaf,
2580c5c9cd4dSSage Weil 								    extent);
2581c5c9cd4dSSage Weil 			}
2582b3b4aa74SDavid Sterba 			btrfs_release_path(path);
258331840ae1SZheng Yan 
2584050006a7SSage Weil 			if (key.offset + datal <= off ||
2585aa42ffd9SLiu Bo 			    key.offset >= off + len - 1)
2586c5c9cd4dSSage Weil 				goto next;
2587c5c9cd4dSSage Weil 
258831840ae1SZheng Yan 			memcpy(&new_key, &key, sizeof(new_key));
258933345d01SLi Zefan 			new_key.objectid = btrfs_ino(inode);
25904d728ec7SLi Zefan 			if (off <= key.offset)
2591c5c9cd4dSSage Weil 				new_key.offset = key.offset + destoff - off;
25924d728ec7SLi Zefan 			else
25934d728ec7SLi Zefan 				new_key.offset = destoff;
2594c5c9cd4dSSage Weil 
2595b6f3409bSSage Weil 			/*
2596b6f3409bSSage Weil 			 * 1 - adjusting old extent (we may have to split it)
2597b6f3409bSSage Weil 			 * 1 - add new extent
2598b6f3409bSSage Weil 			 * 1 - inode update
2599b6f3409bSSage Weil 			 */
2600b6f3409bSSage Weil 			trans = btrfs_start_transaction(root, 3);
2601a22285a6SYan, Zheng 			if (IS_ERR(trans)) {
2602a22285a6SYan, Zheng 				ret = PTR_ERR(trans);
2603a22285a6SYan, Zheng 				goto out;
2604a22285a6SYan, Zheng 			}
2605a22285a6SYan, Zheng 
2606c8a894d7SChris Mason 			if (type == BTRFS_FILE_EXTENT_REG ||
2607c8a894d7SChris Mason 			    type == BTRFS_FILE_EXTENT_PREALLOC) {
2608d72c0842SLi Zefan 				/*
2609d72c0842SLi Zefan 				 *    a  | --- range to clone ---|  b
2610d72c0842SLi Zefan 				 * | ------------- extent ------------- |
2611d72c0842SLi Zefan 				 */
2612d72c0842SLi Zefan 
2613d72c0842SLi Zefan 				/* substract range b */
2614d72c0842SLi Zefan 				if (key.offset + datal > off + len)
2615d72c0842SLi Zefan 					datal = off + len - key.offset;
2616d72c0842SLi Zefan 
2617d72c0842SLi Zefan 				/* substract range a */
2618a22285a6SYan, Zheng 				if (off > key.offset) {
2619a22285a6SYan, Zheng 					datao += off - key.offset;
2620a22285a6SYan, Zheng 					datal -= off - key.offset;
2621a22285a6SYan, Zheng 				}
2622a22285a6SYan, Zheng 
26235dc562c5SJosef Bacik 				ret = btrfs_drop_extents(trans, root, inode,
2624a22285a6SYan, Zheng 							 new_key.offset,
2625a22285a6SYan, Zheng 							 new_key.offset + datal,
26262671485dSJosef Bacik 							 1);
262779787eaaSJeff Mahoney 				if (ret) {
262879787eaaSJeff Mahoney 					btrfs_abort_transaction(trans, root,
262979787eaaSJeff Mahoney 								ret);
263079787eaaSJeff Mahoney 					btrfs_end_transaction(trans, root);
263179787eaaSJeff Mahoney 					goto out;
263279787eaaSJeff Mahoney 				}
2633a22285a6SYan, Zheng 
263431840ae1SZheng Yan 				ret = btrfs_insert_empty_item(trans, root, path,
263531840ae1SZheng Yan 							      &new_key, size);
263679787eaaSJeff Mahoney 				if (ret) {
263779787eaaSJeff Mahoney 					btrfs_abort_transaction(trans, root,
263879787eaaSJeff Mahoney 								ret);
263979787eaaSJeff Mahoney 					btrfs_end_transaction(trans, root);
264079787eaaSJeff Mahoney 					goto out;
264179787eaaSJeff Mahoney 				}
264231840ae1SZheng Yan 
264331840ae1SZheng Yan 				leaf = path->nodes[0];
264431840ae1SZheng Yan 				slot = path->slots[0];
264531840ae1SZheng Yan 				write_extent_buffer(leaf, buf,
264631840ae1SZheng Yan 					    btrfs_item_ptr_offset(leaf, slot),
264731840ae1SZheng Yan 					    size);
2648ae01a0abSYan Zheng 
2649f46b5a66SChristoph Hellwig 				extent = btrfs_item_ptr(leaf, slot,
2650f46b5a66SChristoph Hellwig 						struct btrfs_file_extent_item);
2651c5c9cd4dSSage Weil 
2652c5c9cd4dSSage Weil 				/* disko == 0 means it's a hole */
2653c5c9cd4dSSage Weil 				if (!disko)
2654c5c9cd4dSSage Weil 					datao = 0;
2655c5c9cd4dSSage Weil 
2656c5c9cd4dSSage Weil 				btrfs_set_file_extent_offset(leaf, extent,
2657c5c9cd4dSSage Weil 							     datao);
2658c5c9cd4dSSage Weil 				btrfs_set_file_extent_num_bytes(leaf, extent,
2659c5c9cd4dSSage Weil 								datal);
2660c5c9cd4dSSage Weil 				if (disko) {
2661c5c9cd4dSSage Weil 					inode_add_bytes(inode, datal);
2662ae01a0abSYan Zheng 					ret = btrfs_inc_extent_ref(trans, root,
26635d4f98a2SYan Zheng 							disko, diskl, 0,
2664f46b5a66SChristoph Hellwig 							root->root_key.objectid,
266533345d01SLi Zefan 							btrfs_ino(inode),
266666d7e7f0SArne Jansen 							new_key.offset - datao,
266766d7e7f0SArne Jansen 							0);
266879787eaaSJeff Mahoney 					if (ret) {
266979787eaaSJeff Mahoney 						btrfs_abort_transaction(trans,
267079787eaaSJeff Mahoney 									root,
267179787eaaSJeff Mahoney 									ret);
267279787eaaSJeff Mahoney 						btrfs_end_transaction(trans,
267379787eaaSJeff Mahoney 								      root);
267479787eaaSJeff Mahoney 						goto out;
267579787eaaSJeff Mahoney 
267679787eaaSJeff Mahoney 					}
2677f46b5a66SChristoph Hellwig 				}
2678c5c9cd4dSSage Weil 			} else if (type == BTRFS_FILE_EXTENT_INLINE) {
2679c5c9cd4dSSage Weil 				u64 skip = 0;
2680c5c9cd4dSSage Weil 				u64 trim = 0;
2681c5c9cd4dSSage Weil 				if (off > key.offset) {
2682c5c9cd4dSSage Weil 					skip = off - key.offset;
2683c5c9cd4dSSage Weil 					new_key.offset += skip;
268431840ae1SZheng Yan 				}
2685d397712bSChris Mason 
2686c5c9cd4dSSage Weil 				if (key.offset + datal > off + len)
2687c5c9cd4dSSage Weil 					trim = key.offset + datal - (off + len);
2688d397712bSChris Mason 
2689c5c9cd4dSSage Weil 				if (comp && (skip || trim)) {
2690c5c9cd4dSSage Weil 					ret = -EINVAL;
2691a22285a6SYan, Zheng 					btrfs_end_transaction(trans, root);
2692c5c9cd4dSSage Weil 					goto out;
269331840ae1SZheng Yan 				}
2694c5c9cd4dSSage Weil 				size -= skip + trim;
2695c5c9cd4dSSage Weil 				datal -= skip + trim;
2696a22285a6SYan, Zheng 
26975dc562c5SJosef Bacik 				ret = btrfs_drop_extents(trans, root, inode,
2698a22285a6SYan, Zheng 							 new_key.offset,
2699a22285a6SYan, Zheng 							 new_key.offset + datal,
27002671485dSJosef Bacik 							 1);
270179787eaaSJeff Mahoney 				if (ret) {
270279787eaaSJeff Mahoney 					btrfs_abort_transaction(trans, root,
270379787eaaSJeff Mahoney 								ret);
270479787eaaSJeff Mahoney 					btrfs_end_transaction(trans, root);
270579787eaaSJeff Mahoney 					goto out;
270679787eaaSJeff Mahoney 				}
2707a22285a6SYan, Zheng 
2708c5c9cd4dSSage Weil 				ret = btrfs_insert_empty_item(trans, root, path,
2709c5c9cd4dSSage Weil 							      &new_key, size);
271079787eaaSJeff Mahoney 				if (ret) {
271179787eaaSJeff Mahoney 					btrfs_abort_transaction(trans, root,
271279787eaaSJeff Mahoney 								ret);
271379787eaaSJeff Mahoney 					btrfs_end_transaction(trans, root);
271479787eaaSJeff Mahoney 					goto out;
271579787eaaSJeff Mahoney 				}
2716c5c9cd4dSSage Weil 
2717c5c9cd4dSSage Weil 				if (skip) {
2718d397712bSChris Mason 					u32 start =
2719d397712bSChris Mason 					  btrfs_file_extent_calc_inline_size(0);
2720c5c9cd4dSSage Weil 					memmove(buf+start, buf+start+skip,
2721c5c9cd4dSSage Weil 						datal);
2722c5c9cd4dSSage Weil 				}
2723c5c9cd4dSSage Weil 
2724c5c9cd4dSSage Weil 				leaf = path->nodes[0];
2725c5c9cd4dSSage Weil 				slot = path->slots[0];
2726c5c9cd4dSSage Weil 				write_extent_buffer(leaf, buf,
2727c5c9cd4dSSage Weil 					    btrfs_item_ptr_offset(leaf, slot),
2728c5c9cd4dSSage Weil 					    size);
2729c5c9cd4dSSage Weil 				inode_add_bytes(inode, datal);
2730c5c9cd4dSSage Weil 			}
2731c5c9cd4dSSage Weil 
2732c5c9cd4dSSage Weil 			btrfs_mark_buffer_dirty(leaf);
2733b3b4aa74SDavid Sterba 			btrfs_release_path(path);
2734c5c9cd4dSSage Weil 
27350c4d2d95SJosef Bacik 			inode_inc_iversion(inode);
2736a22285a6SYan, Zheng 			inode->i_mtime = inode->i_ctime = CURRENT_TIME;
2737b5384d48SSage Weil 
2738b5384d48SSage Weil 			/*
2739b5384d48SSage Weil 			 * we round up to the block size at eof when
2740b5384d48SSage Weil 			 * determining which extents to clone above,
2741b5384d48SSage Weil 			 * but shouldn't round up the file size
2742b5384d48SSage Weil 			 */
2743b5384d48SSage Weil 			endoff = new_key.offset + datal;
27445f3888ffSLi Zefan 			if (endoff > destoff+olen)
27455f3888ffSLi Zefan 				endoff = destoff+olen;
2746b5384d48SSage Weil 			if (endoff > inode->i_size)
2747b5384d48SSage Weil 				btrfs_i_size_write(inode, endoff);
2748b5384d48SSage Weil 
2749a22285a6SYan, Zheng 			ret = btrfs_update_inode(trans, root, inode);
275079787eaaSJeff Mahoney 			if (ret) {
275179787eaaSJeff Mahoney 				btrfs_abort_transaction(trans, root, ret);
2752a22285a6SYan, Zheng 				btrfs_end_transaction(trans, root);
275379787eaaSJeff Mahoney 				goto out;
275479787eaaSJeff Mahoney 			}
275579787eaaSJeff Mahoney 			ret = btrfs_end_transaction(trans, root);
2756a22285a6SYan, Zheng 		}
2757c5c9cd4dSSage Weil next:
2758b3b4aa74SDavid Sterba 		btrfs_release_path(path);
2759ae01a0abSYan Zheng 		key.offset++;
2760ae01a0abSYan Zheng 	}
2761f46b5a66SChristoph Hellwig 	ret = 0;
2762f46b5a66SChristoph Hellwig out:
2763b3b4aa74SDavid Sterba 	btrfs_release_path(path);
2764aa42ffd9SLiu Bo 	unlock_extent(&BTRFS_I(src)->io_tree, off, off + len - 1);
2765f46b5a66SChristoph Hellwig out_unlock:
2766f46b5a66SChristoph Hellwig 	mutex_unlock(&src->i_mutex);
2767f46b5a66SChristoph Hellwig 	mutex_unlock(&inode->i_mutex);
2768ae01a0abSYan Zheng 	vfree(buf);
2769ae01a0abSYan Zheng 	btrfs_free_path(path);
2770f46b5a66SChristoph Hellwig out_fput:
27712903ff01SAl Viro 	fdput(src_file);
2772ab67b7c1SYan Zheng out_drop_write:
27732a79f17eSAl Viro 	mnt_drop_write_file(file);
2774f46b5a66SChristoph Hellwig 	return ret;
2775f46b5a66SChristoph Hellwig }
2776f46b5a66SChristoph Hellwig 
27777a865e8aSChristoph Hellwig static long btrfs_ioctl_clone_range(struct file *file, void __user *argp)
2778c5c9cd4dSSage Weil {
2779c5c9cd4dSSage Weil 	struct btrfs_ioctl_clone_range_args args;
2780c5c9cd4dSSage Weil 
27817a865e8aSChristoph Hellwig 	if (copy_from_user(&args, argp, sizeof(args)))
2782c5c9cd4dSSage Weil 		return -EFAULT;
2783c5c9cd4dSSage Weil 	return btrfs_ioctl_clone(file, args.src_fd, args.src_offset,
2784c5c9cd4dSSage Weil 				 args.src_length, args.dest_offset);
2785c5c9cd4dSSage Weil }
2786c5c9cd4dSSage Weil 
2787f46b5a66SChristoph Hellwig /*
2788f46b5a66SChristoph Hellwig  * there are many ways the trans_start and trans_end ioctls can lead
2789f46b5a66SChristoph Hellwig  * to deadlocks.  They should only be used by applications that
2790f46b5a66SChristoph Hellwig  * basically own the machine, and have a very in depth understanding
2791f46b5a66SChristoph Hellwig  * of all the possible deadlocks and enospc problems.
2792f46b5a66SChristoph Hellwig  */
2793b2950863SChristoph Hellwig static long btrfs_ioctl_trans_start(struct file *file)
2794f46b5a66SChristoph Hellwig {
2795f46b5a66SChristoph Hellwig 	struct inode *inode = fdentry(file)->d_inode;
2796f46b5a66SChristoph Hellwig 	struct btrfs_root *root = BTRFS_I(inode)->root;
2797f46b5a66SChristoph Hellwig 	struct btrfs_trans_handle *trans;
27981ab86aedSSage Weil 	int ret;
2799f46b5a66SChristoph Hellwig 
28001ab86aedSSage Weil 	ret = -EPERM;
2801df5b5520SChristoph Hellwig 	if (!capable(CAP_SYS_ADMIN))
2802f46b5a66SChristoph Hellwig 		goto out;
28031ab86aedSSage Weil 
28041ab86aedSSage Weil 	ret = -EINPROGRESS;
28051ab86aedSSage Weil 	if (file->private_data)
28061ab86aedSSage Weil 		goto out;
28079ca9ee09SSage Weil 
2808b83cc969SLi Zefan 	ret = -EROFS;
2809b83cc969SLi Zefan 	if (btrfs_root_readonly(root))
2810b83cc969SLi Zefan 		goto out;
2811b83cc969SLi Zefan 
2812a561be71SAl Viro 	ret = mnt_want_write_file(file);
2813c146afadSYan Zheng 	if (ret)
2814c146afadSYan Zheng 		goto out;
2815c146afadSYan Zheng 
2816a4abeea4SJosef Bacik 	atomic_inc(&root->fs_info->open_ioctl_trans);
28179ca9ee09SSage Weil 
2818f46b5a66SChristoph Hellwig 	ret = -ENOMEM;
28197a7eaa40SJosef Bacik 	trans = btrfs_start_ioctl_transaction(root);
2820abd30bb0STsutomu Itoh 	if (IS_ERR(trans))
28211ab86aedSSage Weil 		goto out_drop;
28221ab86aedSSage Weil 
28231ab86aedSSage Weil 	file->private_data = trans;
28241ab86aedSSage Weil 	return 0;
28251ab86aedSSage Weil 
28261ab86aedSSage Weil out_drop:
2827a4abeea4SJosef Bacik 	atomic_dec(&root->fs_info->open_ioctl_trans);
28282a79f17eSAl Viro 	mnt_drop_write_file(file);
2829f46b5a66SChristoph Hellwig out:
2830f46b5a66SChristoph Hellwig 	return ret;
2831f46b5a66SChristoph Hellwig }
2832f46b5a66SChristoph Hellwig 
28336ef5ed0dSJosef Bacik static long btrfs_ioctl_default_subvol(struct file *file, void __user *argp)
28346ef5ed0dSJosef Bacik {
28356ef5ed0dSJosef Bacik 	struct inode *inode = fdentry(file)->d_inode;
28366ef5ed0dSJosef Bacik 	struct btrfs_root *root = BTRFS_I(inode)->root;
28376ef5ed0dSJosef Bacik 	struct btrfs_root *new_root;
28386ef5ed0dSJosef Bacik 	struct btrfs_dir_item *di;
28396ef5ed0dSJosef Bacik 	struct btrfs_trans_handle *trans;
28406ef5ed0dSJosef Bacik 	struct btrfs_path *path;
28416ef5ed0dSJosef Bacik 	struct btrfs_key location;
28426ef5ed0dSJosef Bacik 	struct btrfs_disk_key disk_key;
28436ef5ed0dSJosef Bacik 	u64 objectid = 0;
28446ef5ed0dSJosef Bacik 	u64 dir_id;
28456ef5ed0dSJosef Bacik 
28466ef5ed0dSJosef Bacik 	if (!capable(CAP_SYS_ADMIN))
28476ef5ed0dSJosef Bacik 		return -EPERM;
28486ef5ed0dSJosef Bacik 
28496ef5ed0dSJosef Bacik 	if (copy_from_user(&objectid, argp, sizeof(objectid)))
28506ef5ed0dSJosef Bacik 		return -EFAULT;
28516ef5ed0dSJosef Bacik 
28526ef5ed0dSJosef Bacik 	if (!objectid)
28536ef5ed0dSJosef Bacik 		objectid = root->root_key.objectid;
28546ef5ed0dSJosef Bacik 
28556ef5ed0dSJosef Bacik 	location.objectid = objectid;
28566ef5ed0dSJosef Bacik 	location.type = BTRFS_ROOT_ITEM_KEY;
28576ef5ed0dSJosef Bacik 	location.offset = (u64)-1;
28586ef5ed0dSJosef Bacik 
28596ef5ed0dSJosef Bacik 	new_root = btrfs_read_fs_root_no_name(root->fs_info, &location);
28606ef5ed0dSJosef Bacik 	if (IS_ERR(new_root))
28616ef5ed0dSJosef Bacik 		return PTR_ERR(new_root);
28626ef5ed0dSJosef Bacik 
28636ef5ed0dSJosef Bacik 	if (btrfs_root_refs(&new_root->root_item) == 0)
28646ef5ed0dSJosef Bacik 		return -ENOENT;
28656ef5ed0dSJosef Bacik 
28666ef5ed0dSJosef Bacik 	path = btrfs_alloc_path();
28676ef5ed0dSJosef Bacik 	if (!path)
28686ef5ed0dSJosef Bacik 		return -ENOMEM;
28696ef5ed0dSJosef Bacik 	path->leave_spinning = 1;
28706ef5ed0dSJosef Bacik 
28716ef5ed0dSJosef Bacik 	trans = btrfs_start_transaction(root, 1);
287298d5dc13STsutomu Itoh 	if (IS_ERR(trans)) {
28736ef5ed0dSJosef Bacik 		btrfs_free_path(path);
287498d5dc13STsutomu Itoh 		return PTR_ERR(trans);
28756ef5ed0dSJosef Bacik 	}
28766ef5ed0dSJosef Bacik 
28776c41761fSDavid Sterba 	dir_id = btrfs_super_root_dir(root->fs_info->super_copy);
28786ef5ed0dSJosef Bacik 	di = btrfs_lookup_dir_item(trans, root->fs_info->tree_root, path,
28796ef5ed0dSJosef Bacik 				   dir_id, "default", 7, 1);
2880cf1e99a4SDan Carpenter 	if (IS_ERR_OR_NULL(di)) {
28816ef5ed0dSJosef Bacik 		btrfs_free_path(path);
28826ef5ed0dSJosef Bacik 		btrfs_end_transaction(trans, root);
28836ef5ed0dSJosef Bacik 		printk(KERN_ERR "Umm, you don't have the default dir item, "
28846ef5ed0dSJosef Bacik 		       "this isn't going to work\n");
28856ef5ed0dSJosef Bacik 		return -ENOENT;
28866ef5ed0dSJosef Bacik 	}
28876ef5ed0dSJosef Bacik 
28886ef5ed0dSJosef Bacik 	btrfs_cpu_key_to_disk(&disk_key, &new_root->root_key);
28896ef5ed0dSJosef Bacik 	btrfs_set_dir_item_key(path->nodes[0], di, &disk_key);
28906ef5ed0dSJosef Bacik 	btrfs_mark_buffer_dirty(path->nodes[0]);
28916ef5ed0dSJosef Bacik 	btrfs_free_path(path);
28926ef5ed0dSJosef Bacik 
28932b0ce2c2SMitch Harder 	btrfs_set_fs_incompat(root->fs_info, DEFAULT_SUBVOL);
28946ef5ed0dSJosef Bacik 	btrfs_end_transaction(trans, root);
28956ef5ed0dSJosef Bacik 
28966ef5ed0dSJosef Bacik 	return 0;
28976ef5ed0dSJosef Bacik }
28986ef5ed0dSJosef Bacik 
28995af3e8ccSStefan Behrens void btrfs_get_block_group_info(struct list_head *groups_list,
2900bf5fc093SJosef Bacik 				struct btrfs_ioctl_space_info *space)
2901bf5fc093SJosef Bacik {
2902bf5fc093SJosef Bacik 	struct btrfs_block_group_cache *block_group;
2903bf5fc093SJosef Bacik 
2904bf5fc093SJosef Bacik 	space->total_bytes = 0;
2905bf5fc093SJosef Bacik 	space->used_bytes = 0;
2906bf5fc093SJosef Bacik 	space->flags = 0;
2907bf5fc093SJosef Bacik 	list_for_each_entry(block_group, groups_list, list) {
2908bf5fc093SJosef Bacik 		space->flags = block_group->flags;
2909bf5fc093SJosef Bacik 		space->total_bytes += block_group->key.offset;
2910bf5fc093SJosef Bacik 		space->used_bytes +=
2911bf5fc093SJosef Bacik 			btrfs_block_group_used(&block_group->item);
2912bf5fc093SJosef Bacik 	}
2913bf5fc093SJosef Bacik }
2914bf5fc093SJosef Bacik 
29151406e432SJosef Bacik long btrfs_ioctl_space_info(struct btrfs_root *root, void __user *arg)
29161406e432SJosef Bacik {
29171406e432SJosef Bacik 	struct btrfs_ioctl_space_args space_args;
29181406e432SJosef Bacik 	struct btrfs_ioctl_space_info space;
29191406e432SJosef Bacik 	struct btrfs_ioctl_space_info *dest;
29207fde62bfSChris Mason 	struct btrfs_ioctl_space_info *dest_orig;
292113f2696fSDaniel J Blueman 	struct btrfs_ioctl_space_info __user *user_dest;
29221406e432SJosef Bacik 	struct btrfs_space_info *info;
2923bf5fc093SJosef Bacik 	u64 types[] = {BTRFS_BLOCK_GROUP_DATA,
2924bf5fc093SJosef Bacik 		       BTRFS_BLOCK_GROUP_SYSTEM,
2925bf5fc093SJosef Bacik 		       BTRFS_BLOCK_GROUP_METADATA,
2926bf5fc093SJosef Bacik 		       BTRFS_BLOCK_GROUP_DATA | BTRFS_BLOCK_GROUP_METADATA};
2927bf5fc093SJosef Bacik 	int num_types = 4;
29287fde62bfSChris Mason 	int alloc_size;
29291406e432SJosef Bacik 	int ret = 0;
293051788b1bSDan Rosenberg 	u64 slot_count = 0;
2931bf5fc093SJosef Bacik 	int i, c;
29321406e432SJosef Bacik 
29331406e432SJosef Bacik 	if (copy_from_user(&space_args,
29341406e432SJosef Bacik 			   (struct btrfs_ioctl_space_args __user *)arg,
29351406e432SJosef Bacik 			   sizeof(space_args)))
29361406e432SJosef Bacik 		return -EFAULT;
29371406e432SJosef Bacik 
2938bf5fc093SJosef Bacik 	for (i = 0; i < num_types; i++) {
2939bf5fc093SJosef Bacik 		struct btrfs_space_info *tmp;
2940bf5fc093SJosef Bacik 
2941bf5fc093SJosef Bacik 		info = NULL;
29427fde62bfSChris Mason 		rcu_read_lock();
2943bf5fc093SJosef Bacik 		list_for_each_entry_rcu(tmp, &root->fs_info->space_info,
2944bf5fc093SJosef Bacik 					list) {
2945bf5fc093SJosef Bacik 			if (tmp->flags == types[i]) {
2946bf5fc093SJosef Bacik 				info = tmp;
2947bf5fc093SJosef Bacik 				break;
2948bf5fc093SJosef Bacik 			}
2949bf5fc093SJosef Bacik 		}
29507fde62bfSChris Mason 		rcu_read_unlock();
29511406e432SJosef Bacik 
2952bf5fc093SJosef Bacik 		if (!info)
2953bf5fc093SJosef Bacik 			continue;
2954bf5fc093SJosef Bacik 
2955bf5fc093SJosef Bacik 		down_read(&info->groups_sem);
2956bf5fc093SJosef Bacik 		for (c = 0; c < BTRFS_NR_RAID_TYPES; c++) {
2957bf5fc093SJosef Bacik 			if (!list_empty(&info->block_groups[c]))
2958bf5fc093SJosef Bacik 				slot_count++;
2959bf5fc093SJosef Bacik 		}
2960bf5fc093SJosef Bacik 		up_read(&info->groups_sem);
2961bf5fc093SJosef Bacik 	}
2962bf5fc093SJosef Bacik 
29637fde62bfSChris Mason 	/* space_slots == 0 means they are asking for a count */
29647fde62bfSChris Mason 	if (space_args.space_slots == 0) {
29657fde62bfSChris Mason 		space_args.total_spaces = slot_count;
29667fde62bfSChris Mason 		goto out;
29677fde62bfSChris Mason 	}
2968bf5fc093SJosef Bacik 
296951788b1bSDan Rosenberg 	slot_count = min_t(u64, space_args.space_slots, slot_count);
2970bf5fc093SJosef Bacik 
29717fde62bfSChris Mason 	alloc_size = sizeof(*dest) * slot_count;
2972bf5fc093SJosef Bacik 
29737fde62bfSChris Mason 	/* we generally have at most 6 or so space infos, one for each raid
29747fde62bfSChris Mason 	 * level.  So, a whole page should be more than enough for everyone
29757fde62bfSChris Mason 	 */
29767fde62bfSChris Mason 	if (alloc_size > PAGE_CACHE_SIZE)
29777fde62bfSChris Mason 		return -ENOMEM;
29787fde62bfSChris Mason 
29797fde62bfSChris Mason 	space_args.total_spaces = 0;
29807fde62bfSChris Mason 	dest = kmalloc(alloc_size, GFP_NOFS);
29817fde62bfSChris Mason 	if (!dest)
29827fde62bfSChris Mason 		return -ENOMEM;
29837fde62bfSChris Mason 	dest_orig = dest;
29847fde62bfSChris Mason 
29857fde62bfSChris Mason 	/* now we have a buffer to copy into */
2986bf5fc093SJosef Bacik 	for (i = 0; i < num_types; i++) {
2987bf5fc093SJosef Bacik 		struct btrfs_space_info *tmp;
2988bf5fc093SJosef Bacik 
298951788b1bSDan Rosenberg 		if (!slot_count)
299051788b1bSDan Rosenberg 			break;
299151788b1bSDan Rosenberg 
2992bf5fc093SJosef Bacik 		info = NULL;
29931406e432SJosef Bacik 		rcu_read_lock();
2994bf5fc093SJosef Bacik 		list_for_each_entry_rcu(tmp, &root->fs_info->space_info,
2995bf5fc093SJosef Bacik 					list) {
2996bf5fc093SJosef Bacik 			if (tmp->flags == types[i]) {
2997bf5fc093SJosef Bacik 				info = tmp;
29987fde62bfSChris Mason 				break;
2999bf5fc093SJosef Bacik 			}
3000bf5fc093SJosef Bacik 		}
3001bf5fc093SJosef Bacik 		rcu_read_unlock();
30027fde62bfSChris Mason 
3003bf5fc093SJosef Bacik 		if (!info)
3004bf5fc093SJosef Bacik 			continue;
3005bf5fc093SJosef Bacik 		down_read(&info->groups_sem);
3006bf5fc093SJosef Bacik 		for (c = 0; c < BTRFS_NR_RAID_TYPES; c++) {
3007bf5fc093SJosef Bacik 			if (!list_empty(&info->block_groups[c])) {
30085af3e8ccSStefan Behrens 				btrfs_get_block_group_info(
30095af3e8ccSStefan Behrens 					&info->block_groups[c], &space);
30107fde62bfSChris Mason 				memcpy(dest, &space, sizeof(space));
30111406e432SJosef Bacik 				dest++;
30121406e432SJosef Bacik 				space_args.total_spaces++;
301351788b1bSDan Rosenberg 				slot_count--;
30141406e432SJosef Bacik 			}
301551788b1bSDan Rosenberg 			if (!slot_count)
301651788b1bSDan Rosenberg 				break;
3017bf5fc093SJosef Bacik 		}
3018bf5fc093SJosef Bacik 		up_read(&info->groups_sem);
3019bf5fc093SJosef Bacik 	}
30201406e432SJosef Bacik 
30212eec6c81SDaniel J Blueman 	user_dest = (struct btrfs_ioctl_space_info __user *)
30227fde62bfSChris Mason 		(arg + sizeof(struct btrfs_ioctl_space_args));
30237fde62bfSChris Mason 
30247fde62bfSChris Mason 	if (copy_to_user(user_dest, dest_orig, alloc_size))
30257fde62bfSChris Mason 		ret = -EFAULT;
30267fde62bfSChris Mason 
30277fde62bfSChris Mason 	kfree(dest_orig);
30287fde62bfSChris Mason out:
30297fde62bfSChris Mason 	if (ret == 0 && copy_to_user(arg, &space_args, sizeof(space_args)))
30301406e432SJosef Bacik 		ret = -EFAULT;
30311406e432SJosef Bacik 
30321406e432SJosef Bacik 	return ret;
30331406e432SJosef Bacik }
30341406e432SJosef Bacik 
3035f46b5a66SChristoph Hellwig /*
3036f46b5a66SChristoph Hellwig  * there are many ways the trans_start and trans_end ioctls can lead
3037f46b5a66SChristoph Hellwig  * to deadlocks.  They should only be used by applications that
3038f46b5a66SChristoph Hellwig  * basically own the machine, and have a very in depth understanding
3039f46b5a66SChristoph Hellwig  * of all the possible deadlocks and enospc problems.
3040f46b5a66SChristoph Hellwig  */
3041f46b5a66SChristoph Hellwig long btrfs_ioctl_trans_end(struct file *file)
3042f46b5a66SChristoph Hellwig {
3043f46b5a66SChristoph Hellwig 	struct inode *inode = fdentry(file)->d_inode;
3044f46b5a66SChristoph Hellwig 	struct btrfs_root *root = BTRFS_I(inode)->root;
3045f46b5a66SChristoph Hellwig 	struct btrfs_trans_handle *trans;
3046f46b5a66SChristoph Hellwig 
3047f46b5a66SChristoph Hellwig 	trans = file->private_data;
30481ab86aedSSage Weil 	if (!trans)
30491ab86aedSSage Weil 		return -EINVAL;
3050b214107eSChristoph Hellwig 	file->private_data = NULL;
30519ca9ee09SSage Weil 
30521ab86aedSSage Weil 	btrfs_end_transaction(trans, root);
30531ab86aedSSage Weil 
3054a4abeea4SJosef Bacik 	atomic_dec(&root->fs_info->open_ioctl_trans);
30559ca9ee09SSage Weil 
30562a79f17eSAl Viro 	mnt_drop_write_file(file);
30571ab86aedSSage Weil 	return 0;
3058f46b5a66SChristoph Hellwig }
3059f46b5a66SChristoph Hellwig 
306046204592SSage Weil static noinline long btrfs_ioctl_start_sync(struct file *file, void __user *argp)
306146204592SSage Weil {
306246204592SSage Weil 	struct btrfs_root *root = BTRFS_I(file->f_dentry->d_inode)->root;
306346204592SSage Weil 	struct btrfs_trans_handle *trans;
306446204592SSage Weil 	u64 transid;
3065db5b493aSTsutomu Itoh 	int ret;
306646204592SSage Weil 
306746204592SSage Weil 	trans = btrfs_start_transaction(root, 0);
306898d5dc13STsutomu Itoh 	if (IS_ERR(trans))
306998d5dc13STsutomu Itoh 		return PTR_ERR(trans);
307046204592SSage Weil 	transid = trans->transid;
3071db5b493aSTsutomu Itoh 	ret = btrfs_commit_transaction_async(trans, root, 0);
30728b2b2d3cSTsutomu Itoh 	if (ret) {
30738b2b2d3cSTsutomu Itoh 		btrfs_end_transaction(trans, root);
3074db5b493aSTsutomu Itoh 		return ret;
30758b2b2d3cSTsutomu Itoh 	}
307646204592SSage Weil 
307746204592SSage Weil 	if (argp)
307846204592SSage Weil 		if (copy_to_user(argp, &transid, sizeof(transid)))
307946204592SSage Weil 			return -EFAULT;
308046204592SSage Weil 	return 0;
308146204592SSage Weil }
308246204592SSage Weil 
308346204592SSage Weil static noinline long btrfs_ioctl_wait_sync(struct file *file, void __user *argp)
308446204592SSage Weil {
308546204592SSage Weil 	struct btrfs_root *root = BTRFS_I(file->f_dentry->d_inode)->root;
308646204592SSage Weil 	u64 transid;
308746204592SSage Weil 
308846204592SSage Weil 	if (argp) {
308946204592SSage Weil 		if (copy_from_user(&transid, argp, sizeof(transid)))
309046204592SSage Weil 			return -EFAULT;
309146204592SSage Weil 	} else {
309246204592SSage Weil 		transid = 0;  /* current trans */
309346204592SSage Weil 	}
309446204592SSage Weil 	return btrfs_wait_for_commit(root, transid);
309546204592SSage Weil }
309646204592SSage Weil 
3097475f6387SJan Schmidt static long btrfs_ioctl_scrub(struct btrfs_root *root, void __user *arg)
3098475f6387SJan Schmidt {
3099475f6387SJan Schmidt 	int ret;
3100475f6387SJan Schmidt 	struct btrfs_ioctl_scrub_args *sa;
3101475f6387SJan Schmidt 
3102475f6387SJan Schmidt 	if (!capable(CAP_SYS_ADMIN))
3103475f6387SJan Schmidt 		return -EPERM;
3104475f6387SJan Schmidt 
3105475f6387SJan Schmidt 	sa = memdup_user(arg, sizeof(*sa));
3106475f6387SJan Schmidt 	if (IS_ERR(sa))
3107475f6387SJan Schmidt 		return PTR_ERR(sa);
3108475f6387SJan Schmidt 
3109aa1b8cd4SStefan Behrens 	ret = btrfs_scrub_dev(root->fs_info, sa->devid, sa->start, sa->end,
311063a212abSStefan Behrens 			      &sa->progress, sa->flags & BTRFS_SCRUB_READONLY,
311163a212abSStefan Behrens 			      0);
3112475f6387SJan Schmidt 
3113475f6387SJan Schmidt 	if (copy_to_user(arg, sa, sizeof(*sa)))
3114475f6387SJan Schmidt 		ret = -EFAULT;
3115475f6387SJan Schmidt 
3116475f6387SJan Schmidt 	kfree(sa);
3117475f6387SJan Schmidt 	return ret;
3118475f6387SJan Schmidt }
3119475f6387SJan Schmidt 
3120475f6387SJan Schmidt static long btrfs_ioctl_scrub_cancel(struct btrfs_root *root, void __user *arg)
3121475f6387SJan Schmidt {
3122475f6387SJan Schmidt 	if (!capable(CAP_SYS_ADMIN))
3123475f6387SJan Schmidt 		return -EPERM;
3124475f6387SJan Schmidt 
3125aa1b8cd4SStefan Behrens 	return btrfs_scrub_cancel(root->fs_info);
3126475f6387SJan Schmidt }
3127475f6387SJan Schmidt 
3128475f6387SJan Schmidt static long btrfs_ioctl_scrub_progress(struct btrfs_root *root,
3129475f6387SJan Schmidt 				       void __user *arg)
3130475f6387SJan Schmidt {
3131475f6387SJan Schmidt 	struct btrfs_ioctl_scrub_args *sa;
3132475f6387SJan Schmidt 	int ret;
3133475f6387SJan Schmidt 
3134475f6387SJan Schmidt 	if (!capable(CAP_SYS_ADMIN))
3135475f6387SJan Schmidt 		return -EPERM;
3136475f6387SJan Schmidt 
3137475f6387SJan Schmidt 	sa = memdup_user(arg, sizeof(*sa));
3138475f6387SJan Schmidt 	if (IS_ERR(sa))
3139475f6387SJan Schmidt 		return PTR_ERR(sa);
3140475f6387SJan Schmidt 
3141475f6387SJan Schmidt 	ret = btrfs_scrub_progress(root, sa->devid, &sa->progress);
3142475f6387SJan Schmidt 
3143475f6387SJan Schmidt 	if (copy_to_user(arg, sa, sizeof(*sa)))
3144475f6387SJan Schmidt 		ret = -EFAULT;
3145475f6387SJan Schmidt 
3146475f6387SJan Schmidt 	kfree(sa);
3147475f6387SJan Schmidt 	return ret;
3148475f6387SJan Schmidt }
3149475f6387SJan Schmidt 
3150c11d2c23SStefan Behrens static long btrfs_ioctl_get_dev_stats(struct btrfs_root *root,
3151b27f7c0cSDavid Sterba 				      void __user *arg)
3152c11d2c23SStefan Behrens {
3153c11d2c23SStefan Behrens 	struct btrfs_ioctl_get_dev_stats *sa;
3154c11d2c23SStefan Behrens 	int ret;
3155c11d2c23SStefan Behrens 
3156c11d2c23SStefan Behrens 	sa = memdup_user(arg, sizeof(*sa));
3157c11d2c23SStefan Behrens 	if (IS_ERR(sa))
3158c11d2c23SStefan Behrens 		return PTR_ERR(sa);
3159c11d2c23SStefan Behrens 
3160b27f7c0cSDavid Sterba 	if ((sa->flags & BTRFS_DEV_STATS_RESET) && !capable(CAP_SYS_ADMIN)) {
3161b27f7c0cSDavid Sterba 		kfree(sa);
3162b27f7c0cSDavid Sterba 		return -EPERM;
3163b27f7c0cSDavid Sterba 	}
3164b27f7c0cSDavid Sterba 
3165b27f7c0cSDavid Sterba 	ret = btrfs_get_dev_stats(root, sa);
3166c11d2c23SStefan Behrens 
3167c11d2c23SStefan Behrens 	if (copy_to_user(arg, sa, sizeof(*sa)))
3168c11d2c23SStefan Behrens 		ret = -EFAULT;
3169c11d2c23SStefan Behrens 
3170c11d2c23SStefan Behrens 	kfree(sa);
3171c11d2c23SStefan Behrens 	return ret;
3172c11d2c23SStefan Behrens }
3173c11d2c23SStefan Behrens 
3174d7728c96SJan Schmidt static long btrfs_ioctl_ino_to_path(struct btrfs_root *root, void __user *arg)
3175d7728c96SJan Schmidt {
3176d7728c96SJan Schmidt 	int ret = 0;
3177d7728c96SJan Schmidt 	int i;
3178740c3d22SChris Mason 	u64 rel_ptr;
3179d7728c96SJan Schmidt 	int size;
3180806468f8SChris Mason 	struct btrfs_ioctl_ino_path_args *ipa = NULL;
3181d7728c96SJan Schmidt 	struct inode_fs_paths *ipath = NULL;
3182d7728c96SJan Schmidt 	struct btrfs_path *path;
3183d7728c96SJan Schmidt 
3184d7728c96SJan Schmidt 	if (!capable(CAP_SYS_ADMIN))
3185d7728c96SJan Schmidt 		return -EPERM;
3186d7728c96SJan Schmidt 
3187d7728c96SJan Schmidt 	path = btrfs_alloc_path();
3188d7728c96SJan Schmidt 	if (!path) {
3189d7728c96SJan Schmidt 		ret = -ENOMEM;
3190d7728c96SJan Schmidt 		goto out;
3191d7728c96SJan Schmidt 	}
3192d7728c96SJan Schmidt 
3193d7728c96SJan Schmidt 	ipa = memdup_user(arg, sizeof(*ipa));
3194d7728c96SJan Schmidt 	if (IS_ERR(ipa)) {
3195d7728c96SJan Schmidt 		ret = PTR_ERR(ipa);
3196d7728c96SJan Schmidt 		ipa = NULL;
3197d7728c96SJan Schmidt 		goto out;
3198d7728c96SJan Schmidt 	}
3199d7728c96SJan Schmidt 
3200d7728c96SJan Schmidt 	size = min_t(u32, ipa->size, 4096);
3201d7728c96SJan Schmidt 	ipath = init_ipath(size, root, path);
3202d7728c96SJan Schmidt 	if (IS_ERR(ipath)) {
3203d7728c96SJan Schmidt 		ret = PTR_ERR(ipath);
3204d7728c96SJan Schmidt 		ipath = NULL;
3205d7728c96SJan Schmidt 		goto out;
3206d7728c96SJan Schmidt 	}
3207d7728c96SJan Schmidt 
3208d7728c96SJan Schmidt 	ret = paths_from_inode(ipa->inum, ipath);
3209d7728c96SJan Schmidt 	if (ret < 0)
3210d7728c96SJan Schmidt 		goto out;
3211d7728c96SJan Schmidt 
3212d7728c96SJan Schmidt 	for (i = 0; i < ipath->fspath->elem_cnt; ++i) {
3213745c4d8eSJeff Mahoney 		rel_ptr = ipath->fspath->val[i] -
3214745c4d8eSJeff Mahoney 			  (u64)(unsigned long)ipath->fspath->val;
3215740c3d22SChris Mason 		ipath->fspath->val[i] = rel_ptr;
3216d7728c96SJan Schmidt 	}
3217d7728c96SJan Schmidt 
3218745c4d8eSJeff Mahoney 	ret = copy_to_user((void *)(unsigned long)ipa->fspath,
3219745c4d8eSJeff Mahoney 			   (void *)(unsigned long)ipath->fspath, size);
3220d7728c96SJan Schmidt 	if (ret) {
3221d7728c96SJan Schmidt 		ret = -EFAULT;
3222d7728c96SJan Schmidt 		goto out;
3223d7728c96SJan Schmidt 	}
3224d7728c96SJan Schmidt 
3225d7728c96SJan Schmidt out:
3226d7728c96SJan Schmidt 	btrfs_free_path(path);
3227d7728c96SJan Schmidt 	free_ipath(ipath);
3228d7728c96SJan Schmidt 	kfree(ipa);
3229d7728c96SJan Schmidt 
3230d7728c96SJan Schmidt 	return ret;
3231d7728c96SJan Schmidt }
3232d7728c96SJan Schmidt 
3233d7728c96SJan Schmidt static int build_ino_list(u64 inum, u64 offset, u64 root, void *ctx)
3234d7728c96SJan Schmidt {
3235d7728c96SJan Schmidt 	struct btrfs_data_container *inodes = ctx;
3236d7728c96SJan Schmidt 	const size_t c = 3 * sizeof(u64);
3237d7728c96SJan Schmidt 
3238d7728c96SJan Schmidt 	if (inodes->bytes_left >= c) {
3239d7728c96SJan Schmidt 		inodes->bytes_left -= c;
3240d7728c96SJan Schmidt 		inodes->val[inodes->elem_cnt] = inum;
3241d7728c96SJan Schmidt 		inodes->val[inodes->elem_cnt + 1] = offset;
3242d7728c96SJan Schmidt 		inodes->val[inodes->elem_cnt + 2] = root;
3243d7728c96SJan Schmidt 		inodes->elem_cnt += 3;
3244d7728c96SJan Schmidt 	} else {
3245d7728c96SJan Schmidt 		inodes->bytes_missing += c - inodes->bytes_left;
3246d7728c96SJan Schmidt 		inodes->bytes_left = 0;
3247d7728c96SJan Schmidt 		inodes->elem_missed += 3;
3248d7728c96SJan Schmidt 	}
3249d7728c96SJan Schmidt 
3250d7728c96SJan Schmidt 	return 0;
3251d7728c96SJan Schmidt }
3252d7728c96SJan Schmidt 
3253d7728c96SJan Schmidt static long btrfs_ioctl_logical_to_ino(struct btrfs_root *root,
3254d7728c96SJan Schmidt 					void __user *arg)
3255d7728c96SJan Schmidt {
3256d7728c96SJan Schmidt 	int ret = 0;
3257d7728c96SJan Schmidt 	int size;
3258d7728c96SJan Schmidt 	struct btrfs_ioctl_logical_ino_args *loi;
3259d7728c96SJan Schmidt 	struct btrfs_data_container *inodes = NULL;
3260d7728c96SJan Schmidt 	struct btrfs_path *path = NULL;
3261d7728c96SJan Schmidt 
3262d7728c96SJan Schmidt 	if (!capable(CAP_SYS_ADMIN))
3263d7728c96SJan Schmidt 		return -EPERM;
3264d7728c96SJan Schmidt 
3265d7728c96SJan Schmidt 	loi = memdup_user(arg, sizeof(*loi));
3266d7728c96SJan Schmidt 	if (IS_ERR(loi)) {
3267d7728c96SJan Schmidt 		ret = PTR_ERR(loi);
3268d7728c96SJan Schmidt 		loi = NULL;
3269d7728c96SJan Schmidt 		goto out;
3270d7728c96SJan Schmidt 	}
3271d7728c96SJan Schmidt 
3272d7728c96SJan Schmidt 	path = btrfs_alloc_path();
3273d7728c96SJan Schmidt 	if (!path) {
3274d7728c96SJan Schmidt 		ret = -ENOMEM;
3275d7728c96SJan Schmidt 		goto out;
3276d7728c96SJan Schmidt 	}
3277d7728c96SJan Schmidt 
3278425d17a2SLiu Bo 	size = min_t(u32, loi->size, 64 * 1024);
3279d7728c96SJan Schmidt 	inodes = init_data_container(size);
3280d7728c96SJan Schmidt 	if (IS_ERR(inodes)) {
3281d7728c96SJan Schmidt 		ret = PTR_ERR(inodes);
3282d7728c96SJan Schmidt 		inodes = NULL;
3283d7728c96SJan Schmidt 		goto out;
3284d7728c96SJan Schmidt 	}
3285d7728c96SJan Schmidt 
3286df031f07SLiu Bo 	ret = iterate_inodes_from_logical(loi->logical, root->fs_info, path,
3287df031f07SLiu Bo 					  build_ino_list, inodes);
3288df031f07SLiu Bo 	if (ret == -EINVAL)
3289d7728c96SJan Schmidt 		ret = -ENOENT;
3290d7728c96SJan Schmidt 	if (ret < 0)
3291d7728c96SJan Schmidt 		goto out;
3292d7728c96SJan Schmidt 
3293745c4d8eSJeff Mahoney 	ret = copy_to_user((void *)(unsigned long)loi->inodes,
3294745c4d8eSJeff Mahoney 			   (void *)(unsigned long)inodes, size);
3295d7728c96SJan Schmidt 	if (ret)
3296d7728c96SJan Schmidt 		ret = -EFAULT;
3297d7728c96SJan Schmidt 
3298d7728c96SJan Schmidt out:
3299d7728c96SJan Schmidt 	btrfs_free_path(path);
3300425d17a2SLiu Bo 	vfree(inodes);
3301d7728c96SJan Schmidt 	kfree(loi);
3302d7728c96SJan Schmidt 
3303d7728c96SJan Schmidt 	return ret;
3304d7728c96SJan Schmidt }
3305d7728c96SJan Schmidt 
330619a39dceSIlya Dryomov void update_ioctl_balance_args(struct btrfs_fs_info *fs_info, int lock,
3307c9e9f97bSIlya Dryomov 			       struct btrfs_ioctl_balance_args *bargs)
3308c9e9f97bSIlya Dryomov {
3309c9e9f97bSIlya Dryomov 	struct btrfs_balance_control *bctl = fs_info->balance_ctl;
3310c9e9f97bSIlya Dryomov 
3311c9e9f97bSIlya Dryomov 	bargs->flags = bctl->flags;
3312c9e9f97bSIlya Dryomov 
3313837d5b6eSIlya Dryomov 	if (atomic_read(&fs_info->balance_running))
3314837d5b6eSIlya Dryomov 		bargs->state |= BTRFS_BALANCE_STATE_RUNNING;
3315837d5b6eSIlya Dryomov 	if (atomic_read(&fs_info->balance_pause_req))
3316837d5b6eSIlya Dryomov 		bargs->state |= BTRFS_BALANCE_STATE_PAUSE_REQ;
3317a7e99c69SIlya Dryomov 	if (atomic_read(&fs_info->balance_cancel_req))
3318a7e99c69SIlya Dryomov 		bargs->state |= BTRFS_BALANCE_STATE_CANCEL_REQ;
3319837d5b6eSIlya Dryomov 
3320c9e9f97bSIlya Dryomov 	memcpy(&bargs->data, &bctl->data, sizeof(bargs->data));
3321c9e9f97bSIlya Dryomov 	memcpy(&bargs->meta, &bctl->meta, sizeof(bargs->meta));
3322c9e9f97bSIlya Dryomov 	memcpy(&bargs->sys, &bctl->sys, sizeof(bargs->sys));
332319a39dceSIlya Dryomov 
332419a39dceSIlya Dryomov 	if (lock) {
332519a39dceSIlya Dryomov 		spin_lock(&fs_info->balance_lock);
332619a39dceSIlya Dryomov 		memcpy(&bargs->stat, &bctl->stat, sizeof(bargs->stat));
332719a39dceSIlya Dryomov 		spin_unlock(&fs_info->balance_lock);
332819a39dceSIlya Dryomov 	} else {
332919a39dceSIlya Dryomov 		memcpy(&bargs->stat, &bctl->stat, sizeof(bargs->stat));
333019a39dceSIlya Dryomov 	}
3331c9e9f97bSIlya Dryomov }
3332c9e9f97bSIlya Dryomov 
33339ba1f6e4SLiu Bo static long btrfs_ioctl_balance(struct file *file, void __user *arg)
3334c9e9f97bSIlya Dryomov {
33359ba1f6e4SLiu Bo 	struct btrfs_root *root = BTRFS_I(fdentry(file)->d_inode)->root;
3336c9e9f97bSIlya Dryomov 	struct btrfs_fs_info *fs_info = root->fs_info;
3337c9e9f97bSIlya Dryomov 	struct btrfs_ioctl_balance_args *bargs;
3338c9e9f97bSIlya Dryomov 	struct btrfs_balance_control *bctl;
3339c9e9f97bSIlya Dryomov 	int ret;
33405ac00addSStefan Behrens 	int need_to_clear_lock = 0;
3341c9e9f97bSIlya Dryomov 
3342c9e9f97bSIlya Dryomov 	if (!capable(CAP_SYS_ADMIN))
3343c9e9f97bSIlya Dryomov 		return -EPERM;
3344c9e9f97bSIlya Dryomov 
3345e54bfa31SLiu Bo 	ret = mnt_want_write_file(file);
33469ba1f6e4SLiu Bo 	if (ret)
33479ba1f6e4SLiu Bo 		return ret;
33489ba1f6e4SLiu Bo 
3349c9e9f97bSIlya Dryomov 	mutex_lock(&fs_info->volume_mutex);
3350c9e9f97bSIlya Dryomov 	mutex_lock(&fs_info->balance_mutex);
3351c9e9f97bSIlya Dryomov 
3352c9e9f97bSIlya Dryomov 	if (arg) {
3353c9e9f97bSIlya Dryomov 		bargs = memdup_user(arg, sizeof(*bargs));
3354c9e9f97bSIlya Dryomov 		if (IS_ERR(bargs)) {
3355c9e9f97bSIlya Dryomov 			ret = PTR_ERR(bargs);
3356c9e9f97bSIlya Dryomov 			goto out;
3357c9e9f97bSIlya Dryomov 		}
3358de322263SIlya Dryomov 
3359de322263SIlya Dryomov 		if (bargs->flags & BTRFS_BALANCE_RESUME) {
3360de322263SIlya Dryomov 			if (!fs_info->balance_ctl) {
3361de322263SIlya Dryomov 				ret = -ENOTCONN;
3362de322263SIlya Dryomov 				goto out_bargs;
3363de322263SIlya Dryomov 			}
3364de322263SIlya Dryomov 
3365de322263SIlya Dryomov 			bctl = fs_info->balance_ctl;
3366de322263SIlya Dryomov 			spin_lock(&fs_info->balance_lock);
3367de322263SIlya Dryomov 			bctl->flags |= BTRFS_BALANCE_RESUME;
3368de322263SIlya Dryomov 			spin_unlock(&fs_info->balance_lock);
3369de322263SIlya Dryomov 
3370de322263SIlya Dryomov 			goto do_balance;
3371de322263SIlya Dryomov 		}
3372c9e9f97bSIlya Dryomov 	} else {
3373c9e9f97bSIlya Dryomov 		bargs = NULL;
3374c9e9f97bSIlya Dryomov 	}
3375c9e9f97bSIlya Dryomov 
33765ac00addSStefan Behrens 	if (atomic_xchg(&root->fs_info->mutually_exclusive_operation_running,
33775ac00addSStefan Behrens 			1)) {
33785ac00addSStefan Behrens 		pr_info("btrfs: dev add/delete/balance/replace/resize operation in progress\n");
3379837d5b6eSIlya Dryomov 		ret = -EINPROGRESS;
3380837d5b6eSIlya Dryomov 		goto out_bargs;
3381837d5b6eSIlya Dryomov 	}
33825ac00addSStefan Behrens 	need_to_clear_lock = 1;
3383837d5b6eSIlya Dryomov 
3384c9e9f97bSIlya Dryomov 	bctl = kzalloc(sizeof(*bctl), GFP_NOFS);
3385c9e9f97bSIlya Dryomov 	if (!bctl) {
3386c9e9f97bSIlya Dryomov 		ret = -ENOMEM;
3387c9e9f97bSIlya Dryomov 		goto out_bargs;
3388c9e9f97bSIlya Dryomov 	}
3389c9e9f97bSIlya Dryomov 
3390c9e9f97bSIlya Dryomov 	bctl->fs_info = fs_info;
3391c9e9f97bSIlya Dryomov 	if (arg) {
3392c9e9f97bSIlya Dryomov 		memcpy(&bctl->data, &bargs->data, sizeof(bctl->data));
3393c9e9f97bSIlya Dryomov 		memcpy(&bctl->meta, &bargs->meta, sizeof(bctl->meta));
3394c9e9f97bSIlya Dryomov 		memcpy(&bctl->sys, &bargs->sys, sizeof(bctl->sys));
3395c9e9f97bSIlya Dryomov 
3396c9e9f97bSIlya Dryomov 		bctl->flags = bargs->flags;
3397f43ffb60SIlya Dryomov 	} else {
3398f43ffb60SIlya Dryomov 		/* balance everything - no filters */
3399f43ffb60SIlya Dryomov 		bctl->flags |= BTRFS_BALANCE_TYPE_MASK;
3400c9e9f97bSIlya Dryomov 	}
3401c9e9f97bSIlya Dryomov 
3402de322263SIlya Dryomov do_balance:
3403c9e9f97bSIlya Dryomov 	ret = btrfs_balance(bctl, bargs);
3404c9e9f97bSIlya Dryomov 	/*
3405837d5b6eSIlya Dryomov 	 * bctl is freed in __cancel_balance or in free_fs_info if
3406837d5b6eSIlya Dryomov 	 * restriper was paused all the way until unmount
3407c9e9f97bSIlya Dryomov 	 */
3408c9e9f97bSIlya Dryomov 	if (arg) {
3409c9e9f97bSIlya Dryomov 		if (copy_to_user(arg, bargs, sizeof(*bargs)))
3410c9e9f97bSIlya Dryomov 			ret = -EFAULT;
3411c9e9f97bSIlya Dryomov 	}
3412c9e9f97bSIlya Dryomov 
3413c9e9f97bSIlya Dryomov out_bargs:
3414c9e9f97bSIlya Dryomov 	kfree(bargs);
3415c9e9f97bSIlya Dryomov out:
34165ac00addSStefan Behrens 	if (need_to_clear_lock)
34175ac00addSStefan Behrens 		atomic_set(&root->fs_info->mutually_exclusive_operation_running,
34185ac00addSStefan Behrens 			   0);
3419c9e9f97bSIlya Dryomov 	mutex_unlock(&fs_info->balance_mutex);
3420c9e9f97bSIlya Dryomov 	mutex_unlock(&fs_info->volume_mutex);
3421e54bfa31SLiu Bo 	mnt_drop_write_file(file);
3422c9e9f97bSIlya Dryomov 	return ret;
3423c9e9f97bSIlya Dryomov }
3424c9e9f97bSIlya Dryomov 
3425837d5b6eSIlya Dryomov static long btrfs_ioctl_balance_ctl(struct btrfs_root *root, int cmd)
3426837d5b6eSIlya Dryomov {
3427837d5b6eSIlya Dryomov 	if (!capable(CAP_SYS_ADMIN))
3428837d5b6eSIlya Dryomov 		return -EPERM;
3429837d5b6eSIlya Dryomov 
3430837d5b6eSIlya Dryomov 	switch (cmd) {
3431837d5b6eSIlya Dryomov 	case BTRFS_BALANCE_CTL_PAUSE:
3432837d5b6eSIlya Dryomov 		return btrfs_pause_balance(root->fs_info);
3433a7e99c69SIlya Dryomov 	case BTRFS_BALANCE_CTL_CANCEL:
3434a7e99c69SIlya Dryomov 		return btrfs_cancel_balance(root->fs_info);
3435837d5b6eSIlya Dryomov 	}
3436837d5b6eSIlya Dryomov 
3437837d5b6eSIlya Dryomov 	return -EINVAL;
3438837d5b6eSIlya Dryomov }
3439837d5b6eSIlya Dryomov 
344019a39dceSIlya Dryomov static long btrfs_ioctl_balance_progress(struct btrfs_root *root,
344119a39dceSIlya Dryomov 					 void __user *arg)
344219a39dceSIlya Dryomov {
344319a39dceSIlya Dryomov 	struct btrfs_fs_info *fs_info = root->fs_info;
344419a39dceSIlya Dryomov 	struct btrfs_ioctl_balance_args *bargs;
344519a39dceSIlya Dryomov 	int ret = 0;
344619a39dceSIlya Dryomov 
344719a39dceSIlya Dryomov 	if (!capable(CAP_SYS_ADMIN))
344819a39dceSIlya Dryomov 		return -EPERM;
344919a39dceSIlya Dryomov 
345019a39dceSIlya Dryomov 	mutex_lock(&fs_info->balance_mutex);
345119a39dceSIlya Dryomov 	if (!fs_info->balance_ctl) {
345219a39dceSIlya Dryomov 		ret = -ENOTCONN;
345319a39dceSIlya Dryomov 		goto out;
345419a39dceSIlya Dryomov 	}
345519a39dceSIlya Dryomov 
345619a39dceSIlya Dryomov 	bargs = kzalloc(sizeof(*bargs), GFP_NOFS);
345719a39dceSIlya Dryomov 	if (!bargs) {
345819a39dceSIlya Dryomov 		ret = -ENOMEM;
345919a39dceSIlya Dryomov 		goto out;
346019a39dceSIlya Dryomov 	}
346119a39dceSIlya Dryomov 
346219a39dceSIlya Dryomov 	update_ioctl_balance_args(fs_info, 1, bargs);
346319a39dceSIlya Dryomov 
346419a39dceSIlya Dryomov 	if (copy_to_user(arg, bargs, sizeof(*bargs)))
346519a39dceSIlya Dryomov 		ret = -EFAULT;
346619a39dceSIlya Dryomov 
346719a39dceSIlya Dryomov 	kfree(bargs);
346819a39dceSIlya Dryomov out:
346919a39dceSIlya Dryomov 	mutex_unlock(&fs_info->balance_mutex);
347019a39dceSIlya Dryomov 	return ret;
347119a39dceSIlya Dryomov }
347219a39dceSIlya Dryomov 
34735d13a37bSArne Jansen static long btrfs_ioctl_quota_ctl(struct btrfs_root *root, void __user *arg)
34745d13a37bSArne Jansen {
34755d13a37bSArne Jansen 	struct btrfs_ioctl_quota_ctl_args *sa;
34765d13a37bSArne Jansen 	struct btrfs_trans_handle *trans = NULL;
34775d13a37bSArne Jansen 	int ret;
34785d13a37bSArne Jansen 	int err;
34795d13a37bSArne Jansen 
34805d13a37bSArne Jansen 	if (!capable(CAP_SYS_ADMIN))
34815d13a37bSArne Jansen 		return -EPERM;
34825d13a37bSArne Jansen 
34835d13a37bSArne Jansen 	if (root->fs_info->sb->s_flags & MS_RDONLY)
34845d13a37bSArne Jansen 		return -EROFS;
34855d13a37bSArne Jansen 
34865d13a37bSArne Jansen 	sa = memdup_user(arg, sizeof(*sa));
34875d13a37bSArne Jansen 	if (IS_ERR(sa))
34885d13a37bSArne Jansen 		return PTR_ERR(sa);
34895d13a37bSArne Jansen 
34905d13a37bSArne Jansen 	if (sa->cmd != BTRFS_QUOTA_CTL_RESCAN) {
34915d13a37bSArne Jansen 		trans = btrfs_start_transaction(root, 2);
34925d13a37bSArne Jansen 		if (IS_ERR(trans)) {
34935d13a37bSArne Jansen 			ret = PTR_ERR(trans);
34945d13a37bSArne Jansen 			goto out;
34955d13a37bSArne Jansen 		}
34965d13a37bSArne Jansen 	}
34975d13a37bSArne Jansen 
34985d13a37bSArne Jansen 	switch (sa->cmd) {
34995d13a37bSArne Jansen 	case BTRFS_QUOTA_CTL_ENABLE:
35005d13a37bSArne Jansen 		ret = btrfs_quota_enable(trans, root->fs_info);
35015d13a37bSArne Jansen 		break;
35025d13a37bSArne Jansen 	case BTRFS_QUOTA_CTL_DISABLE:
35035d13a37bSArne Jansen 		ret = btrfs_quota_disable(trans, root->fs_info);
35045d13a37bSArne Jansen 		break;
35055d13a37bSArne Jansen 	case BTRFS_QUOTA_CTL_RESCAN:
35065d13a37bSArne Jansen 		ret = btrfs_quota_rescan(root->fs_info);
35075d13a37bSArne Jansen 		break;
35085d13a37bSArne Jansen 	default:
35095d13a37bSArne Jansen 		ret = -EINVAL;
35105d13a37bSArne Jansen 		break;
35115d13a37bSArne Jansen 	}
35125d13a37bSArne Jansen 
35135d13a37bSArne Jansen 	if (copy_to_user(arg, sa, sizeof(*sa)))
35145d13a37bSArne Jansen 		ret = -EFAULT;
35155d13a37bSArne Jansen 
35165d13a37bSArne Jansen 	if (trans) {
35175d13a37bSArne Jansen 		err = btrfs_commit_transaction(trans, root);
35185d13a37bSArne Jansen 		if (err && !ret)
35195d13a37bSArne Jansen 			ret = err;
35205d13a37bSArne Jansen 	}
35215d13a37bSArne Jansen 
35225d13a37bSArne Jansen out:
35235d13a37bSArne Jansen 	kfree(sa);
35245d13a37bSArne Jansen 	return ret;
35255d13a37bSArne Jansen }
35265d13a37bSArne Jansen 
35275d13a37bSArne Jansen static long btrfs_ioctl_qgroup_assign(struct btrfs_root *root, void __user *arg)
35285d13a37bSArne Jansen {
35295d13a37bSArne Jansen 	struct btrfs_ioctl_qgroup_assign_args *sa;
35305d13a37bSArne Jansen 	struct btrfs_trans_handle *trans;
35315d13a37bSArne Jansen 	int ret;
35325d13a37bSArne Jansen 	int err;
35335d13a37bSArne Jansen 
35345d13a37bSArne Jansen 	if (!capable(CAP_SYS_ADMIN))
35355d13a37bSArne Jansen 		return -EPERM;
35365d13a37bSArne Jansen 
35375d13a37bSArne Jansen 	if (root->fs_info->sb->s_flags & MS_RDONLY)
35385d13a37bSArne Jansen 		return -EROFS;
35395d13a37bSArne Jansen 
35405d13a37bSArne Jansen 	sa = memdup_user(arg, sizeof(*sa));
35415d13a37bSArne Jansen 	if (IS_ERR(sa))
35425d13a37bSArne Jansen 		return PTR_ERR(sa);
35435d13a37bSArne Jansen 
35445d13a37bSArne Jansen 	trans = btrfs_join_transaction(root);
35455d13a37bSArne Jansen 	if (IS_ERR(trans)) {
35465d13a37bSArne Jansen 		ret = PTR_ERR(trans);
35475d13a37bSArne Jansen 		goto out;
35485d13a37bSArne Jansen 	}
35495d13a37bSArne Jansen 
35505d13a37bSArne Jansen 	/* FIXME: check if the IDs really exist */
35515d13a37bSArne Jansen 	if (sa->assign) {
35525d13a37bSArne Jansen 		ret = btrfs_add_qgroup_relation(trans, root->fs_info,
35535d13a37bSArne Jansen 						sa->src, sa->dst);
35545d13a37bSArne Jansen 	} else {
35555d13a37bSArne Jansen 		ret = btrfs_del_qgroup_relation(trans, root->fs_info,
35565d13a37bSArne Jansen 						sa->src, sa->dst);
35575d13a37bSArne Jansen 	}
35585d13a37bSArne Jansen 
35595d13a37bSArne Jansen 	err = btrfs_end_transaction(trans, root);
35605d13a37bSArne Jansen 	if (err && !ret)
35615d13a37bSArne Jansen 		ret = err;
35625d13a37bSArne Jansen 
35635d13a37bSArne Jansen out:
35645d13a37bSArne Jansen 	kfree(sa);
35655d13a37bSArne Jansen 	return ret;
35665d13a37bSArne Jansen }
35675d13a37bSArne Jansen 
35685d13a37bSArne Jansen static long btrfs_ioctl_qgroup_create(struct btrfs_root *root, void __user *arg)
35695d13a37bSArne Jansen {
35705d13a37bSArne Jansen 	struct btrfs_ioctl_qgroup_create_args *sa;
35715d13a37bSArne Jansen 	struct btrfs_trans_handle *trans;
35725d13a37bSArne Jansen 	int ret;
35735d13a37bSArne Jansen 	int err;
35745d13a37bSArne Jansen 
35755d13a37bSArne Jansen 	if (!capable(CAP_SYS_ADMIN))
35765d13a37bSArne Jansen 		return -EPERM;
35775d13a37bSArne Jansen 
35785d13a37bSArne Jansen 	if (root->fs_info->sb->s_flags & MS_RDONLY)
35795d13a37bSArne Jansen 		return -EROFS;
35805d13a37bSArne Jansen 
35815d13a37bSArne Jansen 	sa = memdup_user(arg, sizeof(*sa));
35825d13a37bSArne Jansen 	if (IS_ERR(sa))
35835d13a37bSArne Jansen 		return PTR_ERR(sa);
35845d13a37bSArne Jansen 
35855d13a37bSArne Jansen 	trans = btrfs_join_transaction(root);
35865d13a37bSArne Jansen 	if (IS_ERR(trans)) {
35875d13a37bSArne Jansen 		ret = PTR_ERR(trans);
35885d13a37bSArne Jansen 		goto out;
35895d13a37bSArne Jansen 	}
35905d13a37bSArne Jansen 
35915d13a37bSArne Jansen 	/* FIXME: check if the IDs really exist */
35925d13a37bSArne Jansen 	if (sa->create) {
35935d13a37bSArne Jansen 		ret = btrfs_create_qgroup(trans, root->fs_info, sa->qgroupid,
35945d13a37bSArne Jansen 					  NULL);
35955d13a37bSArne Jansen 	} else {
35965d13a37bSArne Jansen 		ret = btrfs_remove_qgroup(trans, root->fs_info, sa->qgroupid);
35975d13a37bSArne Jansen 	}
35985d13a37bSArne Jansen 
35995d13a37bSArne Jansen 	err = btrfs_end_transaction(trans, root);
36005d13a37bSArne Jansen 	if (err && !ret)
36015d13a37bSArne Jansen 		ret = err;
36025d13a37bSArne Jansen 
36035d13a37bSArne Jansen out:
36045d13a37bSArne Jansen 	kfree(sa);
36055d13a37bSArne Jansen 	return ret;
36065d13a37bSArne Jansen }
36075d13a37bSArne Jansen 
36085d13a37bSArne Jansen static long btrfs_ioctl_qgroup_limit(struct btrfs_root *root, void __user *arg)
36095d13a37bSArne Jansen {
36105d13a37bSArne Jansen 	struct btrfs_ioctl_qgroup_limit_args *sa;
36115d13a37bSArne Jansen 	struct btrfs_trans_handle *trans;
36125d13a37bSArne Jansen 	int ret;
36135d13a37bSArne Jansen 	int err;
36145d13a37bSArne Jansen 	u64 qgroupid;
36155d13a37bSArne Jansen 
36165d13a37bSArne Jansen 	if (!capable(CAP_SYS_ADMIN))
36175d13a37bSArne Jansen 		return -EPERM;
36185d13a37bSArne Jansen 
36195d13a37bSArne Jansen 	if (root->fs_info->sb->s_flags & MS_RDONLY)
36205d13a37bSArne Jansen 		return -EROFS;
36215d13a37bSArne Jansen 
36225d13a37bSArne Jansen 	sa = memdup_user(arg, sizeof(*sa));
36235d13a37bSArne Jansen 	if (IS_ERR(sa))
36245d13a37bSArne Jansen 		return PTR_ERR(sa);
36255d13a37bSArne Jansen 
36265d13a37bSArne Jansen 	trans = btrfs_join_transaction(root);
36275d13a37bSArne Jansen 	if (IS_ERR(trans)) {
36285d13a37bSArne Jansen 		ret = PTR_ERR(trans);
36295d13a37bSArne Jansen 		goto out;
36305d13a37bSArne Jansen 	}
36315d13a37bSArne Jansen 
36325d13a37bSArne Jansen 	qgroupid = sa->qgroupid;
36335d13a37bSArne Jansen 	if (!qgroupid) {
36345d13a37bSArne Jansen 		/* take the current subvol as qgroup */
36355d13a37bSArne Jansen 		qgroupid = root->root_key.objectid;
36365d13a37bSArne Jansen 	}
36375d13a37bSArne Jansen 
36385d13a37bSArne Jansen 	/* FIXME: check if the IDs really exist */
36395d13a37bSArne Jansen 	ret = btrfs_limit_qgroup(trans, root->fs_info, qgroupid, &sa->lim);
36405d13a37bSArne Jansen 
36415d13a37bSArne Jansen 	err = btrfs_end_transaction(trans, root);
36425d13a37bSArne Jansen 	if (err && !ret)
36435d13a37bSArne Jansen 		ret = err;
36445d13a37bSArne Jansen 
36455d13a37bSArne Jansen out:
36465d13a37bSArne Jansen 	kfree(sa);
36475d13a37bSArne Jansen 	return ret;
36485d13a37bSArne Jansen }
36495d13a37bSArne Jansen 
36508ea05e3aSAlexander Block static long btrfs_ioctl_set_received_subvol(struct file *file,
36518ea05e3aSAlexander Block 					    void __user *arg)
36528ea05e3aSAlexander Block {
36538ea05e3aSAlexander Block 	struct btrfs_ioctl_received_subvol_args *sa = NULL;
36548ea05e3aSAlexander Block 	struct inode *inode = fdentry(file)->d_inode;
36558ea05e3aSAlexander Block 	struct btrfs_root *root = BTRFS_I(inode)->root;
36568ea05e3aSAlexander Block 	struct btrfs_root_item *root_item = &root->root_item;
36578ea05e3aSAlexander Block 	struct btrfs_trans_handle *trans;
36588ea05e3aSAlexander Block 	struct timespec ct = CURRENT_TIME;
36598ea05e3aSAlexander Block 	int ret = 0;
36608ea05e3aSAlexander Block 
36618ea05e3aSAlexander Block 	ret = mnt_want_write_file(file);
36628ea05e3aSAlexander Block 	if (ret < 0)
36638ea05e3aSAlexander Block 		return ret;
36648ea05e3aSAlexander Block 
36658ea05e3aSAlexander Block 	down_write(&root->fs_info->subvol_sem);
36668ea05e3aSAlexander Block 
36678ea05e3aSAlexander Block 	if (btrfs_ino(inode) != BTRFS_FIRST_FREE_OBJECTID) {
36688ea05e3aSAlexander Block 		ret = -EINVAL;
36698ea05e3aSAlexander Block 		goto out;
36708ea05e3aSAlexander Block 	}
36718ea05e3aSAlexander Block 
36728ea05e3aSAlexander Block 	if (btrfs_root_readonly(root)) {
36738ea05e3aSAlexander Block 		ret = -EROFS;
36748ea05e3aSAlexander Block 		goto out;
36758ea05e3aSAlexander Block 	}
36768ea05e3aSAlexander Block 
36778ea05e3aSAlexander Block 	if (!inode_owner_or_capable(inode)) {
36788ea05e3aSAlexander Block 		ret = -EACCES;
36798ea05e3aSAlexander Block 		goto out;
36808ea05e3aSAlexander Block 	}
36818ea05e3aSAlexander Block 
36828ea05e3aSAlexander Block 	sa = memdup_user(arg, sizeof(*sa));
36838ea05e3aSAlexander Block 	if (IS_ERR(sa)) {
36848ea05e3aSAlexander Block 		ret = PTR_ERR(sa);
36858ea05e3aSAlexander Block 		sa = NULL;
36868ea05e3aSAlexander Block 		goto out;
36878ea05e3aSAlexander Block 	}
36888ea05e3aSAlexander Block 
36898ea05e3aSAlexander Block 	trans = btrfs_start_transaction(root, 1);
36908ea05e3aSAlexander Block 	if (IS_ERR(trans)) {
36918ea05e3aSAlexander Block 		ret = PTR_ERR(trans);
36928ea05e3aSAlexander Block 		trans = NULL;
36938ea05e3aSAlexander Block 		goto out;
36948ea05e3aSAlexander Block 	}
36958ea05e3aSAlexander Block 
36968ea05e3aSAlexander Block 	sa->rtransid = trans->transid;
36978ea05e3aSAlexander Block 	sa->rtime.sec = ct.tv_sec;
36988ea05e3aSAlexander Block 	sa->rtime.nsec = ct.tv_nsec;
36998ea05e3aSAlexander Block 
37008ea05e3aSAlexander Block 	memcpy(root_item->received_uuid, sa->uuid, BTRFS_UUID_SIZE);
37018ea05e3aSAlexander Block 	btrfs_set_root_stransid(root_item, sa->stransid);
37028ea05e3aSAlexander Block 	btrfs_set_root_rtransid(root_item, sa->rtransid);
37038ea05e3aSAlexander Block 	root_item->stime.sec = cpu_to_le64(sa->stime.sec);
37048ea05e3aSAlexander Block 	root_item->stime.nsec = cpu_to_le32(sa->stime.nsec);
37058ea05e3aSAlexander Block 	root_item->rtime.sec = cpu_to_le64(sa->rtime.sec);
37068ea05e3aSAlexander Block 	root_item->rtime.nsec = cpu_to_le32(sa->rtime.nsec);
37078ea05e3aSAlexander Block 
37088ea05e3aSAlexander Block 	ret = btrfs_update_root(trans, root->fs_info->tree_root,
37098ea05e3aSAlexander Block 				&root->root_key, &root->root_item);
37108ea05e3aSAlexander Block 	if (ret < 0) {
37118ea05e3aSAlexander Block 		btrfs_end_transaction(trans, root);
37128ea05e3aSAlexander Block 		trans = NULL;
37138ea05e3aSAlexander Block 		goto out;
37148ea05e3aSAlexander Block 	} else {
37158ea05e3aSAlexander Block 		ret = btrfs_commit_transaction(trans, root);
37168ea05e3aSAlexander Block 		if (ret < 0)
37178ea05e3aSAlexander Block 			goto out;
37188ea05e3aSAlexander Block 	}
37198ea05e3aSAlexander Block 
37208ea05e3aSAlexander Block 	ret = copy_to_user(arg, sa, sizeof(*sa));
37218ea05e3aSAlexander Block 	if (ret)
37228ea05e3aSAlexander Block 		ret = -EFAULT;
37238ea05e3aSAlexander Block 
37248ea05e3aSAlexander Block out:
37258ea05e3aSAlexander Block 	kfree(sa);
37268ea05e3aSAlexander Block 	up_write(&root->fs_info->subvol_sem);
37278ea05e3aSAlexander Block 	mnt_drop_write_file(file);
37288ea05e3aSAlexander Block 	return ret;
37298ea05e3aSAlexander Block }
37308ea05e3aSAlexander Block 
3731f46b5a66SChristoph Hellwig long btrfs_ioctl(struct file *file, unsigned int
3732f46b5a66SChristoph Hellwig 		cmd, unsigned long arg)
3733f46b5a66SChristoph Hellwig {
3734f46b5a66SChristoph Hellwig 	struct btrfs_root *root = BTRFS_I(fdentry(file)->d_inode)->root;
37354bcabaa3SChristoph Hellwig 	void __user *argp = (void __user *)arg;
3736f46b5a66SChristoph Hellwig 
3737f46b5a66SChristoph Hellwig 	switch (cmd) {
37386cbff00fSChristoph Hellwig 	case FS_IOC_GETFLAGS:
37396cbff00fSChristoph Hellwig 		return btrfs_ioctl_getflags(file, argp);
37406cbff00fSChristoph Hellwig 	case FS_IOC_SETFLAGS:
37416cbff00fSChristoph Hellwig 		return btrfs_ioctl_setflags(file, argp);
37426cbff00fSChristoph Hellwig 	case FS_IOC_GETVERSION:
37436cbff00fSChristoph Hellwig 		return btrfs_ioctl_getversion(file, argp);
3744f7039b1dSLi Dongyang 	case FITRIM:
3745f7039b1dSLi Dongyang 		return btrfs_ioctl_fitrim(file, argp);
3746f46b5a66SChristoph Hellwig 	case BTRFS_IOC_SNAP_CREATE:
3747fa0d2b9bSLi Zefan 		return btrfs_ioctl_snap_create(file, argp, 0);
3748fdfb1e4fSLi Zefan 	case BTRFS_IOC_SNAP_CREATE_V2:
3749fa0d2b9bSLi Zefan 		return btrfs_ioctl_snap_create_v2(file, argp, 0);
37503de4586cSChris Mason 	case BTRFS_IOC_SUBVOL_CREATE:
3751fa0d2b9bSLi Zefan 		return btrfs_ioctl_snap_create(file, argp, 1);
37526f72c7e2SArne Jansen 	case BTRFS_IOC_SUBVOL_CREATE_V2:
37536f72c7e2SArne Jansen 		return btrfs_ioctl_snap_create_v2(file, argp, 1);
375476dda93cSYan, Zheng 	case BTRFS_IOC_SNAP_DESTROY:
375576dda93cSYan, Zheng 		return btrfs_ioctl_snap_destroy(file, argp);
37560caa102dSLi Zefan 	case BTRFS_IOC_SUBVOL_GETFLAGS:
37570caa102dSLi Zefan 		return btrfs_ioctl_subvol_getflags(file, argp);
37580caa102dSLi Zefan 	case BTRFS_IOC_SUBVOL_SETFLAGS:
37590caa102dSLi Zefan 		return btrfs_ioctl_subvol_setflags(file, argp);
37606ef5ed0dSJosef Bacik 	case BTRFS_IOC_DEFAULT_SUBVOL:
37616ef5ed0dSJosef Bacik 		return btrfs_ioctl_default_subvol(file, argp);
3762f46b5a66SChristoph Hellwig 	case BTRFS_IOC_DEFRAG:
37631e701a32SChris Mason 		return btrfs_ioctl_defrag(file, NULL);
37641e701a32SChris Mason 	case BTRFS_IOC_DEFRAG_RANGE:
37651e701a32SChris Mason 		return btrfs_ioctl_defrag(file, argp);
3766f46b5a66SChristoph Hellwig 	case BTRFS_IOC_RESIZE:
37674bcabaa3SChristoph Hellwig 		return btrfs_ioctl_resize(root, argp);
3768f46b5a66SChristoph Hellwig 	case BTRFS_IOC_ADD_DEV:
37694bcabaa3SChristoph Hellwig 		return btrfs_ioctl_add_dev(root, argp);
3770f46b5a66SChristoph Hellwig 	case BTRFS_IOC_RM_DEV:
37714bcabaa3SChristoph Hellwig 		return btrfs_ioctl_rm_dev(root, argp);
3772475f6387SJan Schmidt 	case BTRFS_IOC_FS_INFO:
3773475f6387SJan Schmidt 		return btrfs_ioctl_fs_info(root, argp);
3774475f6387SJan Schmidt 	case BTRFS_IOC_DEV_INFO:
3775475f6387SJan Schmidt 		return btrfs_ioctl_dev_info(root, argp);
3776f46b5a66SChristoph Hellwig 	case BTRFS_IOC_BALANCE:
37779ba1f6e4SLiu Bo 		return btrfs_ioctl_balance(file, NULL);
3778f46b5a66SChristoph Hellwig 	case BTRFS_IOC_CLONE:
3779c5c9cd4dSSage Weil 		return btrfs_ioctl_clone(file, arg, 0, 0, 0);
3780c5c9cd4dSSage Weil 	case BTRFS_IOC_CLONE_RANGE:
37817a865e8aSChristoph Hellwig 		return btrfs_ioctl_clone_range(file, argp);
3782f46b5a66SChristoph Hellwig 	case BTRFS_IOC_TRANS_START:
3783f46b5a66SChristoph Hellwig 		return btrfs_ioctl_trans_start(file);
3784f46b5a66SChristoph Hellwig 	case BTRFS_IOC_TRANS_END:
3785f46b5a66SChristoph Hellwig 		return btrfs_ioctl_trans_end(file);
3786ac8e9819SChris Mason 	case BTRFS_IOC_TREE_SEARCH:
3787ac8e9819SChris Mason 		return btrfs_ioctl_tree_search(file, argp);
3788ac8e9819SChris Mason 	case BTRFS_IOC_INO_LOOKUP:
3789ac8e9819SChris Mason 		return btrfs_ioctl_ino_lookup(file, argp);
3790d7728c96SJan Schmidt 	case BTRFS_IOC_INO_PATHS:
3791d7728c96SJan Schmidt 		return btrfs_ioctl_ino_to_path(root, argp);
3792d7728c96SJan Schmidt 	case BTRFS_IOC_LOGICAL_INO:
3793d7728c96SJan Schmidt 		return btrfs_ioctl_logical_to_ino(root, argp);
37941406e432SJosef Bacik 	case BTRFS_IOC_SPACE_INFO:
37951406e432SJosef Bacik 		return btrfs_ioctl_space_info(root, argp);
3796f46b5a66SChristoph Hellwig 	case BTRFS_IOC_SYNC:
3797f46b5a66SChristoph Hellwig 		btrfs_sync_fs(file->f_dentry->d_sb, 1);
3798f46b5a66SChristoph Hellwig 		return 0;
379946204592SSage Weil 	case BTRFS_IOC_START_SYNC:
380046204592SSage Weil 		return btrfs_ioctl_start_sync(file, argp);
380146204592SSage Weil 	case BTRFS_IOC_WAIT_SYNC:
380246204592SSage Weil 		return btrfs_ioctl_wait_sync(file, argp);
3803475f6387SJan Schmidt 	case BTRFS_IOC_SCRUB:
3804475f6387SJan Schmidt 		return btrfs_ioctl_scrub(root, argp);
3805475f6387SJan Schmidt 	case BTRFS_IOC_SCRUB_CANCEL:
3806475f6387SJan Schmidt 		return btrfs_ioctl_scrub_cancel(root, argp);
3807475f6387SJan Schmidt 	case BTRFS_IOC_SCRUB_PROGRESS:
3808475f6387SJan Schmidt 		return btrfs_ioctl_scrub_progress(root, argp);
3809c9e9f97bSIlya Dryomov 	case BTRFS_IOC_BALANCE_V2:
38109ba1f6e4SLiu Bo 		return btrfs_ioctl_balance(file, argp);
3811837d5b6eSIlya Dryomov 	case BTRFS_IOC_BALANCE_CTL:
3812837d5b6eSIlya Dryomov 		return btrfs_ioctl_balance_ctl(root, arg);
381319a39dceSIlya Dryomov 	case BTRFS_IOC_BALANCE_PROGRESS:
381419a39dceSIlya Dryomov 		return btrfs_ioctl_balance_progress(root, argp);
38158ea05e3aSAlexander Block 	case BTRFS_IOC_SET_RECEIVED_SUBVOL:
38168ea05e3aSAlexander Block 		return btrfs_ioctl_set_received_subvol(file, argp);
381731db9f7cSAlexander Block 	case BTRFS_IOC_SEND:
381831db9f7cSAlexander Block 		return btrfs_ioctl_send(file, argp);
3819c11d2c23SStefan Behrens 	case BTRFS_IOC_GET_DEV_STATS:
3820b27f7c0cSDavid Sterba 		return btrfs_ioctl_get_dev_stats(root, argp);
38215d13a37bSArne Jansen 	case BTRFS_IOC_QUOTA_CTL:
38225d13a37bSArne Jansen 		return btrfs_ioctl_quota_ctl(root, argp);
38235d13a37bSArne Jansen 	case BTRFS_IOC_QGROUP_ASSIGN:
38245d13a37bSArne Jansen 		return btrfs_ioctl_qgroup_assign(root, argp);
38255d13a37bSArne Jansen 	case BTRFS_IOC_QGROUP_CREATE:
38265d13a37bSArne Jansen 		return btrfs_ioctl_qgroup_create(root, argp);
38275d13a37bSArne Jansen 	case BTRFS_IOC_QGROUP_LIMIT:
38285d13a37bSArne Jansen 		return btrfs_ioctl_qgroup_limit(root, argp);
3829f46b5a66SChristoph Hellwig 	}
3830f46b5a66SChristoph Hellwig 
3831f46b5a66SChristoph Hellwig 	return -ENOTTY;
3832f46b5a66SChristoph Hellwig }
3833