xref: /openbmc/linux/fs/btrfs/ioctl.c (revision 79787eaa)
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>
444b4e25f2SChris Mason #include "compat.h"
45f46b5a66SChristoph Hellwig #include "ctree.h"
46f46b5a66SChristoph Hellwig #include "disk-io.h"
47f46b5a66SChristoph Hellwig #include "transaction.h"
48f46b5a66SChristoph Hellwig #include "btrfs_inode.h"
49f46b5a66SChristoph Hellwig #include "ioctl.h"
50f46b5a66SChristoph Hellwig #include "print-tree.h"
51f46b5a66SChristoph Hellwig #include "volumes.h"
52925baeddSChris Mason #include "locking.h"
53581bb050SLi Zefan #include "inode-map.h"
54d7728c96SJan Schmidt #include "backref.h"
55f46b5a66SChristoph Hellwig 
566cbff00fSChristoph Hellwig /* Mask out flags that are inappropriate for the given type of inode. */
576cbff00fSChristoph Hellwig static inline __u32 btrfs_mask_flags(umode_t mode, __u32 flags)
586cbff00fSChristoph Hellwig {
596cbff00fSChristoph Hellwig 	if (S_ISDIR(mode))
606cbff00fSChristoph Hellwig 		return flags;
616cbff00fSChristoph Hellwig 	else if (S_ISREG(mode))
626cbff00fSChristoph Hellwig 		return flags & ~FS_DIRSYNC_FL;
636cbff00fSChristoph Hellwig 	else
646cbff00fSChristoph Hellwig 		return flags & (FS_NODUMP_FL | FS_NOATIME_FL);
656cbff00fSChristoph Hellwig }
66f46b5a66SChristoph Hellwig 
676cbff00fSChristoph Hellwig /*
686cbff00fSChristoph Hellwig  * Export inode flags to the format expected by the FS_IOC_GETFLAGS ioctl.
696cbff00fSChristoph Hellwig  */
706cbff00fSChristoph Hellwig static unsigned int btrfs_flags_to_ioctl(unsigned int flags)
716cbff00fSChristoph Hellwig {
726cbff00fSChristoph Hellwig 	unsigned int iflags = 0;
736cbff00fSChristoph Hellwig 
746cbff00fSChristoph Hellwig 	if (flags & BTRFS_INODE_SYNC)
756cbff00fSChristoph Hellwig 		iflags |= FS_SYNC_FL;
766cbff00fSChristoph Hellwig 	if (flags & BTRFS_INODE_IMMUTABLE)
776cbff00fSChristoph Hellwig 		iflags |= FS_IMMUTABLE_FL;
786cbff00fSChristoph Hellwig 	if (flags & BTRFS_INODE_APPEND)
796cbff00fSChristoph Hellwig 		iflags |= FS_APPEND_FL;
806cbff00fSChristoph Hellwig 	if (flags & BTRFS_INODE_NODUMP)
816cbff00fSChristoph Hellwig 		iflags |= FS_NODUMP_FL;
826cbff00fSChristoph Hellwig 	if (flags & BTRFS_INODE_NOATIME)
836cbff00fSChristoph Hellwig 		iflags |= FS_NOATIME_FL;
846cbff00fSChristoph Hellwig 	if (flags & BTRFS_INODE_DIRSYNC)
856cbff00fSChristoph Hellwig 		iflags |= FS_DIRSYNC_FL;
86d0092bddSLi Zefan 	if (flags & BTRFS_INODE_NODATACOW)
87d0092bddSLi Zefan 		iflags |= FS_NOCOW_FL;
88d0092bddSLi Zefan 
89d0092bddSLi Zefan 	if ((flags & BTRFS_INODE_COMPRESS) && !(flags & BTRFS_INODE_NOCOMPRESS))
90d0092bddSLi Zefan 		iflags |= FS_COMPR_FL;
91d0092bddSLi Zefan 	else if (flags & BTRFS_INODE_NOCOMPRESS)
92d0092bddSLi Zefan 		iflags |= FS_NOCOMP_FL;
936cbff00fSChristoph Hellwig 
946cbff00fSChristoph Hellwig 	return iflags;
956cbff00fSChristoph Hellwig }
966cbff00fSChristoph Hellwig 
976cbff00fSChristoph Hellwig /*
986cbff00fSChristoph Hellwig  * Update inode->i_flags based on the btrfs internal flags.
996cbff00fSChristoph Hellwig  */
1006cbff00fSChristoph Hellwig void btrfs_update_iflags(struct inode *inode)
1016cbff00fSChristoph Hellwig {
1026cbff00fSChristoph Hellwig 	struct btrfs_inode *ip = BTRFS_I(inode);
1036cbff00fSChristoph Hellwig 
1046cbff00fSChristoph Hellwig 	inode->i_flags &= ~(S_SYNC|S_APPEND|S_IMMUTABLE|S_NOATIME|S_DIRSYNC);
1056cbff00fSChristoph Hellwig 
1066cbff00fSChristoph Hellwig 	if (ip->flags & BTRFS_INODE_SYNC)
1076cbff00fSChristoph Hellwig 		inode->i_flags |= S_SYNC;
1086cbff00fSChristoph Hellwig 	if (ip->flags & BTRFS_INODE_IMMUTABLE)
1096cbff00fSChristoph Hellwig 		inode->i_flags |= S_IMMUTABLE;
1106cbff00fSChristoph Hellwig 	if (ip->flags & BTRFS_INODE_APPEND)
1116cbff00fSChristoph Hellwig 		inode->i_flags |= S_APPEND;
1126cbff00fSChristoph Hellwig 	if (ip->flags & BTRFS_INODE_NOATIME)
1136cbff00fSChristoph Hellwig 		inode->i_flags |= S_NOATIME;
1146cbff00fSChristoph Hellwig 	if (ip->flags & BTRFS_INODE_DIRSYNC)
1156cbff00fSChristoph Hellwig 		inode->i_flags |= S_DIRSYNC;
1166cbff00fSChristoph Hellwig }
1176cbff00fSChristoph Hellwig 
1186cbff00fSChristoph Hellwig /*
1196cbff00fSChristoph Hellwig  * Inherit flags from the parent inode.
1206cbff00fSChristoph Hellwig  *
121e27425d6SJosef Bacik  * Currently only the compression flags and the cow flags are inherited.
1226cbff00fSChristoph Hellwig  */
1236cbff00fSChristoph Hellwig void btrfs_inherit_iflags(struct inode *inode, struct inode *dir)
1246cbff00fSChristoph Hellwig {
1250b4dcea5SChris Mason 	unsigned int flags;
1260b4dcea5SChris Mason 
1270b4dcea5SChris Mason 	if (!dir)
1280b4dcea5SChris Mason 		return;
1290b4dcea5SChris Mason 
1300b4dcea5SChris Mason 	flags = BTRFS_I(dir)->flags;
1316cbff00fSChristoph Hellwig 
132e27425d6SJosef Bacik 	if (flags & BTRFS_INODE_NOCOMPRESS) {
133e27425d6SJosef Bacik 		BTRFS_I(inode)->flags &= ~BTRFS_INODE_COMPRESS;
134e27425d6SJosef Bacik 		BTRFS_I(inode)->flags |= BTRFS_INODE_NOCOMPRESS;
135e27425d6SJosef Bacik 	} else if (flags & BTRFS_INODE_COMPRESS) {
136e27425d6SJosef Bacik 		BTRFS_I(inode)->flags &= ~BTRFS_INODE_NOCOMPRESS;
137e27425d6SJosef Bacik 		BTRFS_I(inode)->flags |= BTRFS_INODE_COMPRESS;
138e27425d6SJosef Bacik 	}
1396cbff00fSChristoph Hellwig 
140e27425d6SJosef Bacik 	if (flags & BTRFS_INODE_NODATACOW)
141e27425d6SJosef Bacik 		BTRFS_I(inode)->flags |= BTRFS_INODE_NODATACOW;
142e27425d6SJosef Bacik 
1436cbff00fSChristoph Hellwig 	btrfs_update_iflags(inode);
1446cbff00fSChristoph Hellwig }
1456cbff00fSChristoph Hellwig 
1466cbff00fSChristoph Hellwig static int btrfs_ioctl_getflags(struct file *file, void __user *arg)
1476cbff00fSChristoph Hellwig {
1486cbff00fSChristoph Hellwig 	struct btrfs_inode *ip = BTRFS_I(file->f_path.dentry->d_inode);
1496cbff00fSChristoph Hellwig 	unsigned int flags = btrfs_flags_to_ioctl(ip->flags);
1506cbff00fSChristoph Hellwig 
1516cbff00fSChristoph Hellwig 	if (copy_to_user(arg, &flags, sizeof(flags)))
1526cbff00fSChristoph Hellwig 		return -EFAULT;
1536cbff00fSChristoph Hellwig 	return 0;
1546cbff00fSChristoph Hellwig }
1556cbff00fSChristoph Hellwig 
15675e7cb7fSLiu Bo static int check_flags(unsigned int flags)
15775e7cb7fSLiu Bo {
15875e7cb7fSLiu Bo 	if (flags & ~(FS_IMMUTABLE_FL | FS_APPEND_FL | \
15975e7cb7fSLiu Bo 		      FS_NOATIME_FL | FS_NODUMP_FL | \
16075e7cb7fSLiu Bo 		      FS_SYNC_FL | FS_DIRSYNC_FL | \
161e1e8fb6aSLi Zefan 		      FS_NOCOMP_FL | FS_COMPR_FL |
162e1e8fb6aSLi Zefan 		      FS_NOCOW_FL))
16375e7cb7fSLiu Bo 		return -EOPNOTSUPP;
16475e7cb7fSLiu Bo 
16575e7cb7fSLiu Bo 	if ((flags & FS_NOCOMP_FL) && (flags & FS_COMPR_FL))
16675e7cb7fSLiu Bo 		return -EINVAL;
16775e7cb7fSLiu Bo 
16875e7cb7fSLiu Bo 	return 0;
16975e7cb7fSLiu Bo }
17075e7cb7fSLiu Bo 
1716cbff00fSChristoph Hellwig static int btrfs_ioctl_setflags(struct file *file, void __user *arg)
1726cbff00fSChristoph Hellwig {
1736cbff00fSChristoph Hellwig 	struct inode *inode = file->f_path.dentry->d_inode;
1746cbff00fSChristoph Hellwig 	struct btrfs_inode *ip = BTRFS_I(inode);
1756cbff00fSChristoph Hellwig 	struct btrfs_root *root = ip->root;
1766cbff00fSChristoph Hellwig 	struct btrfs_trans_handle *trans;
1776cbff00fSChristoph Hellwig 	unsigned int flags, oldflags;
1786cbff00fSChristoph Hellwig 	int ret;
179f062abf0SLi Zefan 	u64 ip_oldflags;
180f062abf0SLi Zefan 	unsigned int i_oldflags;
1816cbff00fSChristoph Hellwig 
182b83cc969SLi Zefan 	if (btrfs_root_readonly(root))
183b83cc969SLi Zefan 		return -EROFS;
184b83cc969SLi Zefan 
1856cbff00fSChristoph Hellwig 	if (copy_from_user(&flags, arg, sizeof(flags)))
1866cbff00fSChristoph Hellwig 		return -EFAULT;
1876cbff00fSChristoph Hellwig 
18875e7cb7fSLiu Bo 	ret = check_flags(flags);
18975e7cb7fSLiu Bo 	if (ret)
19075e7cb7fSLiu Bo 		return ret;
1916cbff00fSChristoph Hellwig 
1922e149670SSerge E. Hallyn 	if (!inode_owner_or_capable(inode))
1936cbff00fSChristoph Hellwig 		return -EACCES;
1946cbff00fSChristoph Hellwig 
1956cbff00fSChristoph Hellwig 	mutex_lock(&inode->i_mutex);
1966cbff00fSChristoph Hellwig 
197f062abf0SLi Zefan 	ip_oldflags = ip->flags;
198f062abf0SLi Zefan 	i_oldflags = inode->i_flags;
199f062abf0SLi Zefan 
2006cbff00fSChristoph Hellwig 	flags = btrfs_mask_flags(inode->i_mode, flags);
2016cbff00fSChristoph Hellwig 	oldflags = btrfs_flags_to_ioctl(ip->flags);
2026cbff00fSChristoph Hellwig 	if ((flags ^ oldflags) & (FS_APPEND_FL | FS_IMMUTABLE_FL)) {
2036cbff00fSChristoph Hellwig 		if (!capable(CAP_LINUX_IMMUTABLE)) {
2046cbff00fSChristoph Hellwig 			ret = -EPERM;
2056cbff00fSChristoph Hellwig 			goto out_unlock;
2066cbff00fSChristoph Hellwig 		}
2076cbff00fSChristoph Hellwig 	}
2086cbff00fSChristoph Hellwig 
209a561be71SAl Viro 	ret = mnt_want_write_file(file);
2106cbff00fSChristoph Hellwig 	if (ret)
2116cbff00fSChristoph Hellwig 		goto out_unlock;
2126cbff00fSChristoph Hellwig 
2136cbff00fSChristoph Hellwig 	if (flags & FS_SYNC_FL)
2146cbff00fSChristoph Hellwig 		ip->flags |= BTRFS_INODE_SYNC;
2156cbff00fSChristoph Hellwig 	else
2166cbff00fSChristoph Hellwig 		ip->flags &= ~BTRFS_INODE_SYNC;
2176cbff00fSChristoph Hellwig 	if (flags & FS_IMMUTABLE_FL)
2186cbff00fSChristoph Hellwig 		ip->flags |= BTRFS_INODE_IMMUTABLE;
2196cbff00fSChristoph Hellwig 	else
2206cbff00fSChristoph Hellwig 		ip->flags &= ~BTRFS_INODE_IMMUTABLE;
2216cbff00fSChristoph Hellwig 	if (flags & FS_APPEND_FL)
2226cbff00fSChristoph Hellwig 		ip->flags |= BTRFS_INODE_APPEND;
2236cbff00fSChristoph Hellwig 	else
2246cbff00fSChristoph Hellwig 		ip->flags &= ~BTRFS_INODE_APPEND;
2256cbff00fSChristoph Hellwig 	if (flags & FS_NODUMP_FL)
2266cbff00fSChristoph Hellwig 		ip->flags |= BTRFS_INODE_NODUMP;
2276cbff00fSChristoph Hellwig 	else
2286cbff00fSChristoph Hellwig 		ip->flags &= ~BTRFS_INODE_NODUMP;
2296cbff00fSChristoph Hellwig 	if (flags & FS_NOATIME_FL)
2306cbff00fSChristoph Hellwig 		ip->flags |= BTRFS_INODE_NOATIME;
2316cbff00fSChristoph Hellwig 	else
2326cbff00fSChristoph Hellwig 		ip->flags &= ~BTRFS_INODE_NOATIME;
2336cbff00fSChristoph Hellwig 	if (flags & FS_DIRSYNC_FL)
2346cbff00fSChristoph Hellwig 		ip->flags |= BTRFS_INODE_DIRSYNC;
2356cbff00fSChristoph Hellwig 	else
2366cbff00fSChristoph Hellwig 		ip->flags &= ~BTRFS_INODE_DIRSYNC;
237e1e8fb6aSLi Zefan 	if (flags & FS_NOCOW_FL)
238e1e8fb6aSLi Zefan 		ip->flags |= BTRFS_INODE_NODATACOW;
239e1e8fb6aSLi Zefan 	else
240e1e8fb6aSLi Zefan 		ip->flags &= ~BTRFS_INODE_NODATACOW;
2416cbff00fSChristoph Hellwig 
24275e7cb7fSLiu Bo 	/*
24375e7cb7fSLiu Bo 	 * The COMPRESS flag can only be changed by users, while the NOCOMPRESS
24475e7cb7fSLiu Bo 	 * flag may be changed automatically if compression code won't make
24575e7cb7fSLiu Bo 	 * things smaller.
24675e7cb7fSLiu Bo 	 */
24775e7cb7fSLiu Bo 	if (flags & FS_NOCOMP_FL) {
24875e7cb7fSLiu Bo 		ip->flags &= ~BTRFS_INODE_COMPRESS;
24975e7cb7fSLiu Bo 		ip->flags |= BTRFS_INODE_NOCOMPRESS;
25075e7cb7fSLiu Bo 	} else if (flags & FS_COMPR_FL) {
25175e7cb7fSLiu Bo 		ip->flags |= BTRFS_INODE_COMPRESS;
25275e7cb7fSLiu Bo 		ip->flags &= ~BTRFS_INODE_NOCOMPRESS;
253ebcb904dSLi Zefan 	} else {
254ebcb904dSLi Zefan 		ip->flags &= ~(BTRFS_INODE_COMPRESS | BTRFS_INODE_NOCOMPRESS);
25575e7cb7fSLiu Bo 	}
2566cbff00fSChristoph Hellwig 
2574da6f1a3SLi Zefan 	trans = btrfs_start_transaction(root, 1);
258f062abf0SLi Zefan 	if (IS_ERR(trans)) {
259f062abf0SLi Zefan 		ret = PTR_ERR(trans);
260f062abf0SLi Zefan 		goto out_drop;
261f062abf0SLi Zefan 	}
2626cbff00fSChristoph Hellwig 
263306424ccSLi Zefan 	btrfs_update_iflags(inode);
264306424ccSLi Zefan 	inode->i_ctime = CURRENT_TIME;
2656cbff00fSChristoph Hellwig 	ret = btrfs_update_inode(trans, root, inode);
2666cbff00fSChristoph Hellwig 
2676cbff00fSChristoph Hellwig 	btrfs_end_transaction(trans, root);
268f062abf0SLi Zefan  out_drop:
269f062abf0SLi Zefan 	if (ret) {
270f062abf0SLi Zefan 		ip->flags = ip_oldflags;
271f062abf0SLi Zefan 		inode->i_flags = i_oldflags;
272f062abf0SLi Zefan 	}
2736cbff00fSChristoph Hellwig 
2742a79f17eSAl Viro 	mnt_drop_write_file(file);
2756cbff00fSChristoph Hellwig  out_unlock:
2766cbff00fSChristoph Hellwig 	mutex_unlock(&inode->i_mutex);
2772d4e6f6aSliubo 	return ret;
2786cbff00fSChristoph Hellwig }
2796cbff00fSChristoph Hellwig 
2806cbff00fSChristoph Hellwig static int btrfs_ioctl_getversion(struct file *file, int __user *arg)
2816cbff00fSChristoph Hellwig {
2826cbff00fSChristoph Hellwig 	struct inode *inode = file->f_path.dentry->d_inode;
2836cbff00fSChristoph Hellwig 
2846cbff00fSChristoph Hellwig 	return put_user(inode->i_generation, arg);
2856cbff00fSChristoph Hellwig }
286f46b5a66SChristoph Hellwig 
287f7039b1dSLi Dongyang static noinline int btrfs_ioctl_fitrim(struct file *file, void __user *arg)
288f7039b1dSLi Dongyang {
289815745cfSAl Viro 	struct btrfs_fs_info *fs_info = btrfs_sb(fdentry(file)->d_sb);
290f7039b1dSLi Dongyang 	struct btrfs_device *device;
291f7039b1dSLi Dongyang 	struct request_queue *q;
292f7039b1dSLi Dongyang 	struct fstrim_range range;
293f7039b1dSLi Dongyang 	u64 minlen = ULLONG_MAX;
294f7039b1dSLi Dongyang 	u64 num_devices = 0;
295815745cfSAl Viro 	u64 total_bytes = btrfs_super_total_bytes(fs_info->super_copy);
296f7039b1dSLi Dongyang 	int ret;
297f7039b1dSLi Dongyang 
298f7039b1dSLi Dongyang 	if (!capable(CAP_SYS_ADMIN))
299f7039b1dSLi Dongyang 		return -EPERM;
300f7039b1dSLi Dongyang 
3011f78160cSXiao Guangrong 	rcu_read_lock();
3021f78160cSXiao Guangrong 	list_for_each_entry_rcu(device, &fs_info->fs_devices->devices,
3031f78160cSXiao Guangrong 				dev_list) {
304f7039b1dSLi Dongyang 		if (!device->bdev)
305f7039b1dSLi Dongyang 			continue;
306f7039b1dSLi Dongyang 		q = bdev_get_queue(device->bdev);
307f7039b1dSLi Dongyang 		if (blk_queue_discard(q)) {
308f7039b1dSLi Dongyang 			num_devices++;
309f7039b1dSLi Dongyang 			minlen = min((u64)q->limits.discard_granularity,
310f7039b1dSLi Dongyang 				     minlen);
311f7039b1dSLi Dongyang 		}
312f7039b1dSLi Dongyang 	}
3131f78160cSXiao Guangrong 	rcu_read_unlock();
314f4c697e6SLukas Czerner 
315f7039b1dSLi Dongyang 	if (!num_devices)
316f7039b1dSLi Dongyang 		return -EOPNOTSUPP;
317f7039b1dSLi Dongyang 	if (copy_from_user(&range, arg, sizeof(range)))
318f7039b1dSLi Dongyang 		return -EFAULT;
319f4c697e6SLukas Czerner 	if (range.start > total_bytes)
320f4c697e6SLukas Czerner 		return -EINVAL;
321f7039b1dSLi Dongyang 
322f4c697e6SLukas Czerner 	range.len = min(range.len, total_bytes - range.start);
323f7039b1dSLi Dongyang 	range.minlen = max(range.minlen, minlen);
324815745cfSAl Viro 	ret = btrfs_trim_fs(fs_info->tree_root, &range);
325f7039b1dSLi Dongyang 	if (ret < 0)
326f7039b1dSLi Dongyang 		return ret;
327f7039b1dSLi Dongyang 
328f7039b1dSLi Dongyang 	if (copy_to_user(arg, &range, sizeof(range)))
329f7039b1dSLi Dongyang 		return -EFAULT;
330f7039b1dSLi Dongyang 
331f7039b1dSLi Dongyang 	return 0;
332f7039b1dSLi Dongyang }
333f7039b1dSLi Dongyang 
334cb8e7090SChristoph Hellwig static noinline int create_subvol(struct btrfs_root *root,
335cb8e7090SChristoph Hellwig 				  struct dentry *dentry,
33672fd032eSSage Weil 				  char *name, int namelen,
33772fd032eSSage Weil 				  u64 *async_transid)
338f46b5a66SChristoph Hellwig {
339f46b5a66SChristoph Hellwig 	struct btrfs_trans_handle *trans;
340f46b5a66SChristoph Hellwig 	struct btrfs_key key;
341f46b5a66SChristoph Hellwig 	struct btrfs_root_item root_item;
342f46b5a66SChristoph Hellwig 	struct btrfs_inode_item *inode_item;
343f46b5a66SChristoph Hellwig 	struct extent_buffer *leaf;
34476dda93cSYan, Zheng 	struct btrfs_root *new_root;
3452fbe8c8aSAl Viro 	struct dentry *parent = dentry->d_parent;
3466a912213SJosef Bacik 	struct inode *dir;
347f46b5a66SChristoph Hellwig 	int ret;
348f46b5a66SChristoph Hellwig 	int err;
349f46b5a66SChristoph Hellwig 	u64 objectid;
350f46b5a66SChristoph Hellwig 	u64 new_dirid = BTRFS_FIRST_FREE_OBJECTID;
3513de4586cSChris Mason 	u64 index = 0;
352f46b5a66SChristoph Hellwig 
353581bb050SLi Zefan 	ret = btrfs_find_free_objectid(root->fs_info->tree_root, &objectid);
3542fbe8c8aSAl Viro 	if (ret)
355a22285a6SYan, Zheng 		return ret;
3566a912213SJosef Bacik 
3576a912213SJosef Bacik 	dir = parent->d_inode;
3586a912213SJosef Bacik 
3599ed74f2dSJosef Bacik 	/*
3609ed74f2dSJosef Bacik 	 * 1 - inode item
3619ed74f2dSJosef Bacik 	 * 2 - refs
3629ed74f2dSJosef Bacik 	 * 1 - root item
3639ed74f2dSJosef Bacik 	 * 2 - dir items
3649ed74f2dSJosef Bacik 	 */
365a22285a6SYan, Zheng 	trans = btrfs_start_transaction(root, 6);
3662fbe8c8aSAl Viro 	if (IS_ERR(trans))
367a22285a6SYan, Zheng 		return PTR_ERR(trans);
368f46b5a66SChristoph Hellwig 
3695d4f98a2SYan Zheng 	leaf = btrfs_alloc_free_block(trans, root, root->leafsize,
37066d7e7f0SArne Jansen 				      0, objectid, NULL, 0, 0, 0, 0);
3718e8a1e31SJosef Bacik 	if (IS_ERR(leaf)) {
3728e8a1e31SJosef Bacik 		ret = PTR_ERR(leaf);
3738e8a1e31SJosef Bacik 		goto fail;
3748e8a1e31SJosef Bacik 	}
375f46b5a66SChristoph Hellwig 
3765d4f98a2SYan Zheng 	memset_extent_buffer(leaf, 0, 0, sizeof(struct btrfs_header));
377f46b5a66SChristoph Hellwig 	btrfs_set_header_bytenr(leaf, leaf->start);
378f46b5a66SChristoph Hellwig 	btrfs_set_header_generation(leaf, trans->transid);
3795d4f98a2SYan Zheng 	btrfs_set_header_backref_rev(leaf, BTRFS_MIXED_BACKREF_REV);
380f46b5a66SChristoph Hellwig 	btrfs_set_header_owner(leaf, objectid);
381f46b5a66SChristoph Hellwig 
382f46b5a66SChristoph Hellwig 	write_extent_buffer(leaf, root->fs_info->fsid,
383f46b5a66SChristoph Hellwig 			    (unsigned long)btrfs_header_fsid(leaf),
384f46b5a66SChristoph Hellwig 			    BTRFS_FSID_SIZE);
3855d4f98a2SYan Zheng 	write_extent_buffer(leaf, root->fs_info->chunk_tree_uuid,
3865d4f98a2SYan Zheng 			    (unsigned long)btrfs_header_chunk_tree_uuid(leaf),
3875d4f98a2SYan Zheng 			    BTRFS_UUID_SIZE);
388f46b5a66SChristoph Hellwig 	btrfs_mark_buffer_dirty(leaf);
389f46b5a66SChristoph Hellwig 
390f46b5a66SChristoph Hellwig 	inode_item = &root_item.inode;
391f46b5a66SChristoph Hellwig 	memset(inode_item, 0, sizeof(*inode_item));
392f46b5a66SChristoph Hellwig 	inode_item->generation = cpu_to_le64(1);
393f46b5a66SChristoph Hellwig 	inode_item->size = cpu_to_le64(3);
394f46b5a66SChristoph Hellwig 	inode_item->nlink = cpu_to_le32(1);
395a76a3cd4SYan Zheng 	inode_item->nbytes = cpu_to_le64(root->leafsize);
396f46b5a66SChristoph Hellwig 	inode_item->mode = cpu_to_le32(S_IFDIR | 0755);
397f46b5a66SChristoph Hellwig 
39808fe4db1SLi Zefan 	root_item.flags = 0;
39908fe4db1SLi Zefan 	root_item.byte_limit = 0;
40008fe4db1SLi Zefan 	inode_item->flags = cpu_to_le64(BTRFS_INODE_ROOT_ITEM_INIT);
40108fe4db1SLi Zefan 
402f46b5a66SChristoph Hellwig 	btrfs_set_root_bytenr(&root_item, leaf->start);
40384234f3aSYan Zheng 	btrfs_set_root_generation(&root_item, trans->transid);
404f46b5a66SChristoph Hellwig 	btrfs_set_root_level(&root_item, 0);
405f46b5a66SChristoph Hellwig 	btrfs_set_root_refs(&root_item, 1);
40686b9f2ecSYan, Zheng 	btrfs_set_root_used(&root_item, leaf->len);
40780ff3856SYan Zheng 	btrfs_set_root_last_snapshot(&root_item, 0);
408f46b5a66SChristoph Hellwig 
409f46b5a66SChristoph Hellwig 	memset(&root_item.drop_progress, 0, sizeof(root_item.drop_progress));
410f46b5a66SChristoph Hellwig 	root_item.drop_level = 0;
411f46b5a66SChristoph Hellwig 
412925baeddSChris Mason 	btrfs_tree_unlock(leaf);
413f46b5a66SChristoph Hellwig 	free_extent_buffer(leaf);
414f46b5a66SChristoph Hellwig 	leaf = NULL;
415f46b5a66SChristoph Hellwig 
416f46b5a66SChristoph Hellwig 	btrfs_set_root_dirid(&root_item, new_dirid);
417f46b5a66SChristoph Hellwig 
418f46b5a66SChristoph Hellwig 	key.objectid = objectid;
4195d4f98a2SYan Zheng 	key.offset = 0;
420f46b5a66SChristoph Hellwig 	btrfs_set_key_type(&key, BTRFS_ROOT_ITEM_KEY);
421f46b5a66SChristoph Hellwig 	ret = btrfs_insert_root(trans, root->fs_info->tree_root, &key,
422f46b5a66SChristoph Hellwig 				&root_item);
423f46b5a66SChristoph Hellwig 	if (ret)
424f46b5a66SChristoph Hellwig 		goto fail;
425f46b5a66SChristoph Hellwig 
42676dda93cSYan, Zheng 	key.offset = (u64)-1;
42776dda93cSYan, Zheng 	new_root = btrfs_read_fs_root_no_name(root->fs_info, &key);
42879787eaaSJeff Mahoney 	if (IS_ERR(new_root)) {
42979787eaaSJeff Mahoney 		btrfs_abort_transaction(trans, root, PTR_ERR(new_root));
43079787eaaSJeff Mahoney 		ret = PTR_ERR(new_root);
43179787eaaSJeff Mahoney 		goto fail;
43279787eaaSJeff Mahoney 	}
43376dda93cSYan, Zheng 
43476dda93cSYan, Zheng 	btrfs_record_root_in_trans(trans, new_root);
43576dda93cSYan, Zheng 
436d82a6f1dSJosef Bacik 	ret = btrfs_create_subvol_root(trans, new_root, new_dirid);
437ce598979SMark Fasheh 	if (ret) {
438ce598979SMark Fasheh 		/* We potentially lose an unused inode item here */
43979787eaaSJeff Mahoney 		btrfs_abort_transaction(trans, root, ret);
440ce598979SMark Fasheh 		goto fail;
441ce598979SMark Fasheh 	}
442ce598979SMark Fasheh 
443f46b5a66SChristoph Hellwig 	/*
444f46b5a66SChristoph Hellwig 	 * insert the directory item
445f46b5a66SChristoph Hellwig 	 */
4463de4586cSChris Mason 	ret = btrfs_set_inode_index(dir, &index);
44779787eaaSJeff Mahoney 	if (ret) {
44879787eaaSJeff Mahoney 		btrfs_abort_transaction(trans, root, ret);
44979787eaaSJeff Mahoney 		goto fail;
45079787eaaSJeff Mahoney 	}
4513de4586cSChris Mason 
4523de4586cSChris Mason 	ret = btrfs_insert_dir_item(trans, root,
45316cdcec7SMiao Xie 				    name, namelen, dir, &key,
4543de4586cSChris Mason 				    BTRFS_FT_DIR, index);
45579787eaaSJeff Mahoney 	if (ret) {
45679787eaaSJeff Mahoney 		btrfs_abort_transaction(trans, root, ret);
457f46b5a66SChristoph Hellwig 		goto fail;
45879787eaaSJeff Mahoney 	}
4590660b5afSChris Mason 
46052c26179SYan Zheng 	btrfs_i_size_write(dir, dir->i_size + namelen * 2);
46152c26179SYan Zheng 	ret = btrfs_update_inode(trans, root, dir);
46252c26179SYan Zheng 	BUG_ON(ret);
46352c26179SYan Zheng 
4640660b5afSChris Mason 	ret = btrfs_add_root_ref(trans, root->fs_info->tree_root,
4654df27c4dSYan, Zheng 				 objectid, root->root_key.objectid,
46633345d01SLi Zefan 				 btrfs_ino(dir), index, name, namelen);
46776dda93cSYan, Zheng 
4680660b5afSChris Mason 	BUG_ON(ret);
4690660b5afSChris Mason 
47076dda93cSYan, Zheng 	d_instantiate(dentry, btrfs_lookup_dentry(dir, dentry));
471f46b5a66SChristoph Hellwig fail:
47272fd032eSSage Weil 	if (async_transid) {
47372fd032eSSage Weil 		*async_transid = trans->transid;
47472fd032eSSage Weil 		err = btrfs_commit_transaction_async(trans, root, 1);
47572fd032eSSage Weil 	} else {
47676dda93cSYan, Zheng 		err = btrfs_commit_transaction(trans, root);
47772fd032eSSage Weil 	}
478f46b5a66SChristoph Hellwig 	if (err && !ret)
479f46b5a66SChristoph Hellwig 		ret = err;
480f46b5a66SChristoph Hellwig 	return ret;
481f46b5a66SChristoph Hellwig }
482f46b5a66SChristoph Hellwig 
48372fd032eSSage Weil static int create_snapshot(struct btrfs_root *root, struct dentry *dentry,
484b83cc969SLi Zefan 			   char *name, int namelen, u64 *async_transid,
485b83cc969SLi Zefan 			   bool readonly)
486f46b5a66SChristoph Hellwig {
4872e4bfab9SYan, Zheng 	struct inode *inode;
488f46b5a66SChristoph Hellwig 	struct btrfs_pending_snapshot *pending_snapshot;
489f46b5a66SChristoph Hellwig 	struct btrfs_trans_handle *trans;
4902e4bfab9SYan, Zheng 	int ret;
491f46b5a66SChristoph Hellwig 
492f46b5a66SChristoph Hellwig 	if (!root->ref_cows)
493f46b5a66SChristoph Hellwig 		return -EINVAL;
494f46b5a66SChristoph Hellwig 
495a22285a6SYan, Zheng 	pending_snapshot = kzalloc(sizeof(*pending_snapshot), GFP_NOFS);
496a22285a6SYan, Zheng 	if (!pending_snapshot)
497a22285a6SYan, Zheng 		return -ENOMEM;
498a22285a6SYan, Zheng 
499a22285a6SYan, Zheng 	btrfs_init_block_rsv(&pending_snapshot->block_rsv);
500a22285a6SYan, Zheng 	pending_snapshot->dentry = dentry;
501a22285a6SYan, Zheng 	pending_snapshot->root = root;
502b83cc969SLi Zefan 	pending_snapshot->readonly = readonly;
503a22285a6SYan, Zheng 
504a22285a6SYan, Zheng 	trans = btrfs_start_transaction(root->fs_info->extent_root, 5);
505a22285a6SYan, Zheng 	if (IS_ERR(trans)) {
506a22285a6SYan, Zheng 		ret = PTR_ERR(trans);
507a22285a6SYan, Zheng 		goto fail;
508a22285a6SYan, Zheng 	}
509a22285a6SYan, Zheng 
510a22285a6SYan, Zheng 	ret = btrfs_snap_reserve_metadata(trans, pending_snapshot);
511a22285a6SYan, Zheng 	BUG_ON(ret);
512a22285a6SYan, Zheng 
5138351583eSJosef Bacik 	spin_lock(&root->fs_info->trans_lock);
514a22285a6SYan, Zheng 	list_add(&pending_snapshot->list,
515a22285a6SYan, Zheng 		 &trans->transaction->pending_snapshots);
5168351583eSJosef Bacik 	spin_unlock(&root->fs_info->trans_lock);
51772fd032eSSage Weil 	if (async_transid) {
51872fd032eSSage Weil 		*async_transid = trans->transid;
51972fd032eSSage Weil 		ret = btrfs_commit_transaction_async(trans,
52072fd032eSSage Weil 				     root->fs_info->extent_root, 1);
52172fd032eSSage Weil 	} else {
52272fd032eSSage Weil 		ret = btrfs_commit_transaction(trans,
52372fd032eSSage Weil 					       root->fs_info->extent_root);
52472fd032eSSage Weil 	}
525a22285a6SYan, Zheng 	BUG_ON(ret);
526a22285a6SYan, Zheng 
527a22285a6SYan, Zheng 	ret = pending_snapshot->error;
528f46b5a66SChristoph Hellwig 	if (ret)
5292e4bfab9SYan, Zheng 		goto fail;
530f46b5a66SChristoph Hellwig 
53166b4ffd1SJosef Bacik 	ret = btrfs_orphan_cleanup(pending_snapshot->snap);
53266b4ffd1SJosef Bacik 	if (ret)
53366b4ffd1SJosef Bacik 		goto fail;
534f46b5a66SChristoph Hellwig 
5352fbe8c8aSAl Viro 	inode = btrfs_lookup_dentry(dentry->d_parent->d_inode, dentry);
5362e4bfab9SYan, Zheng 	if (IS_ERR(inode)) {
5372e4bfab9SYan, Zheng 		ret = PTR_ERR(inode);
5382e4bfab9SYan, Zheng 		goto fail;
5392e4bfab9SYan, Zheng 	}
5402e4bfab9SYan, Zheng 	BUG_ON(!inode);
5412e4bfab9SYan, Zheng 	d_instantiate(dentry, inode);
5422e4bfab9SYan, Zheng 	ret = 0;
5432e4bfab9SYan, Zheng fail:
544a22285a6SYan, Zheng 	kfree(pending_snapshot);
545f46b5a66SChristoph Hellwig 	return ret;
546f46b5a66SChristoph Hellwig }
547f46b5a66SChristoph Hellwig 
5484260f7c7SSage Weil /*  copy of check_sticky in fs/namei.c()
5494260f7c7SSage Weil * It's inline, so penalty for filesystems that don't use sticky bit is
5504260f7c7SSage Weil * minimal.
5514260f7c7SSage Weil */
5524260f7c7SSage Weil static inline int btrfs_check_sticky(struct inode *dir, struct inode *inode)
5534260f7c7SSage Weil {
5544260f7c7SSage Weil 	uid_t fsuid = current_fsuid();
5554260f7c7SSage Weil 
5564260f7c7SSage Weil 	if (!(dir->i_mode & S_ISVTX))
5574260f7c7SSage Weil 		return 0;
5584260f7c7SSage Weil 	if (inode->i_uid == fsuid)
5594260f7c7SSage Weil 		return 0;
5604260f7c7SSage Weil 	if (dir->i_uid == fsuid)
5614260f7c7SSage Weil 		return 0;
5624260f7c7SSage Weil 	return !capable(CAP_FOWNER);
5634260f7c7SSage Weil }
5644260f7c7SSage Weil 
5654260f7c7SSage Weil /*  copy of may_delete in fs/namei.c()
5664260f7c7SSage Weil  *	Check whether we can remove a link victim from directory dir, check
5674260f7c7SSage Weil  *  whether the type of victim is right.
5684260f7c7SSage Weil  *  1. We can't do it if dir is read-only (done in permission())
5694260f7c7SSage Weil  *  2. We should have write and exec permissions on dir
5704260f7c7SSage Weil  *  3. We can't remove anything from append-only dir
5714260f7c7SSage Weil  *  4. We can't do anything with immutable dir (done in permission())
5724260f7c7SSage Weil  *  5. If the sticky bit on dir is set we should either
5734260f7c7SSage Weil  *	a. be owner of dir, or
5744260f7c7SSage Weil  *	b. be owner of victim, or
5754260f7c7SSage Weil  *	c. have CAP_FOWNER capability
5764260f7c7SSage Weil  *  6. If the victim is append-only or immutable we can't do antyhing with
5774260f7c7SSage Weil  *     links pointing to it.
5784260f7c7SSage Weil  *  7. If we were asked to remove a directory and victim isn't one - ENOTDIR.
5794260f7c7SSage Weil  *  8. If we were asked to remove a non-directory and victim isn't one - EISDIR.
5804260f7c7SSage Weil  *  9. We can't remove a root or mountpoint.
5814260f7c7SSage Weil  * 10. We don't allow removal of NFS sillyrenamed files; it's handled by
5824260f7c7SSage Weil  *     nfs_async_unlink().
5834260f7c7SSage Weil  */
5844260f7c7SSage Weil 
5854260f7c7SSage Weil static int btrfs_may_delete(struct inode *dir,struct dentry *victim,int isdir)
5864260f7c7SSage Weil {
5874260f7c7SSage Weil 	int error;
5884260f7c7SSage Weil 
5894260f7c7SSage Weil 	if (!victim->d_inode)
5904260f7c7SSage Weil 		return -ENOENT;
5914260f7c7SSage Weil 
5924260f7c7SSage Weil 	BUG_ON(victim->d_parent->d_inode != dir);
5934260f7c7SSage Weil 	audit_inode_child(victim, dir);
5944260f7c7SSage Weil 
5954260f7c7SSage Weil 	error = inode_permission(dir, MAY_WRITE | MAY_EXEC);
5964260f7c7SSage Weil 	if (error)
5974260f7c7SSage Weil 		return error;
5984260f7c7SSage Weil 	if (IS_APPEND(dir))
5994260f7c7SSage Weil 		return -EPERM;
6004260f7c7SSage Weil 	if (btrfs_check_sticky(dir, victim->d_inode)||
6014260f7c7SSage Weil 		IS_APPEND(victim->d_inode)||
6024260f7c7SSage Weil 	    IS_IMMUTABLE(victim->d_inode) || IS_SWAPFILE(victim->d_inode))
6034260f7c7SSage Weil 		return -EPERM;
6044260f7c7SSage Weil 	if (isdir) {
6054260f7c7SSage Weil 		if (!S_ISDIR(victim->d_inode->i_mode))
6064260f7c7SSage Weil 			return -ENOTDIR;
6074260f7c7SSage Weil 		if (IS_ROOT(victim))
6084260f7c7SSage Weil 			return -EBUSY;
6094260f7c7SSage Weil 	} else if (S_ISDIR(victim->d_inode->i_mode))
6104260f7c7SSage Weil 		return -EISDIR;
6114260f7c7SSage Weil 	if (IS_DEADDIR(dir))
6124260f7c7SSage Weil 		return -ENOENT;
6134260f7c7SSage Weil 	if (victim->d_flags & DCACHE_NFSFS_RENAMED)
6144260f7c7SSage Weil 		return -EBUSY;
6154260f7c7SSage Weil 	return 0;
6164260f7c7SSage Weil }
6174260f7c7SSage Weil 
618cb8e7090SChristoph Hellwig /* copy of may_create in fs/namei.c() */
619cb8e7090SChristoph Hellwig static inline int btrfs_may_create(struct inode *dir, struct dentry *child)
620cb8e7090SChristoph Hellwig {
621cb8e7090SChristoph Hellwig 	if (child->d_inode)
622cb8e7090SChristoph Hellwig 		return -EEXIST;
623cb8e7090SChristoph Hellwig 	if (IS_DEADDIR(dir))
624cb8e7090SChristoph Hellwig 		return -ENOENT;
625cb8e7090SChristoph Hellwig 	return inode_permission(dir, MAY_WRITE | MAY_EXEC);
626cb8e7090SChristoph Hellwig }
627cb8e7090SChristoph Hellwig 
628cb8e7090SChristoph Hellwig /*
629cb8e7090SChristoph Hellwig  * Create a new subvolume below @parent.  This is largely modeled after
630cb8e7090SChristoph Hellwig  * sys_mkdirat and vfs_mkdir, but we only do a single component lookup
631cb8e7090SChristoph Hellwig  * inside this filesystem so it's quite a bit simpler.
632cb8e7090SChristoph Hellwig  */
63376dda93cSYan, Zheng static noinline int btrfs_mksubvol(struct path *parent,
63476dda93cSYan, Zheng 				   char *name, int namelen,
63572fd032eSSage Weil 				   struct btrfs_root *snap_src,
636b83cc969SLi Zefan 				   u64 *async_transid, bool readonly)
637cb8e7090SChristoph Hellwig {
63876dda93cSYan, Zheng 	struct inode *dir  = parent->dentry->d_inode;
639cb8e7090SChristoph Hellwig 	struct dentry *dentry;
640cb8e7090SChristoph Hellwig 	int error;
641cb8e7090SChristoph Hellwig 
64276dda93cSYan, Zheng 	mutex_lock_nested(&dir->i_mutex, I_MUTEX_PARENT);
643cb8e7090SChristoph Hellwig 
644cb8e7090SChristoph Hellwig 	dentry = lookup_one_len(name, parent->dentry, namelen);
645cb8e7090SChristoph Hellwig 	error = PTR_ERR(dentry);
646cb8e7090SChristoph Hellwig 	if (IS_ERR(dentry))
647cb8e7090SChristoph Hellwig 		goto out_unlock;
648cb8e7090SChristoph Hellwig 
649cb8e7090SChristoph Hellwig 	error = -EEXIST;
650cb8e7090SChristoph Hellwig 	if (dentry->d_inode)
651cb8e7090SChristoph Hellwig 		goto out_dput;
652cb8e7090SChristoph Hellwig 
653cb8e7090SChristoph Hellwig 	error = mnt_want_write(parent->mnt);
654cb8e7090SChristoph Hellwig 	if (error)
655cb8e7090SChristoph Hellwig 		goto out_dput;
656cb8e7090SChristoph Hellwig 
65776dda93cSYan, Zheng 	error = btrfs_may_create(dir, dentry);
658cb8e7090SChristoph Hellwig 	if (error)
659cb8e7090SChristoph Hellwig 		goto out_drop_write;
660cb8e7090SChristoph Hellwig 
66176dda93cSYan, Zheng 	down_read(&BTRFS_I(dir)->root->fs_info->subvol_sem);
66276dda93cSYan, Zheng 
66376dda93cSYan, Zheng 	if (btrfs_root_refs(&BTRFS_I(dir)->root->root_item) == 0)
66476dda93cSYan, Zheng 		goto out_up_read;
66576dda93cSYan, Zheng 
6663de4586cSChris Mason 	if (snap_src) {
66772fd032eSSage Weil 		error = create_snapshot(snap_src, dentry,
668b83cc969SLi Zefan 					name, namelen, async_transid, readonly);
6693de4586cSChris Mason 	} else {
67076dda93cSYan, Zheng 		error = create_subvol(BTRFS_I(dir)->root, dentry,
67172fd032eSSage Weil 				      name, namelen, async_transid);
6723de4586cSChris Mason 	}
67376dda93cSYan, Zheng 	if (!error)
67476dda93cSYan, Zheng 		fsnotify_mkdir(dir, dentry);
67576dda93cSYan, Zheng out_up_read:
67676dda93cSYan, Zheng 	up_read(&BTRFS_I(dir)->root->fs_info->subvol_sem);
677cb8e7090SChristoph Hellwig out_drop_write:
678cb8e7090SChristoph Hellwig 	mnt_drop_write(parent->mnt);
679cb8e7090SChristoph Hellwig out_dput:
680cb8e7090SChristoph Hellwig 	dput(dentry);
681cb8e7090SChristoph Hellwig out_unlock:
68276dda93cSYan, Zheng 	mutex_unlock(&dir->i_mutex);
683cb8e7090SChristoph Hellwig 	return error;
684cb8e7090SChristoph Hellwig }
685cb8e7090SChristoph Hellwig 
6864cb5300bSChris Mason /*
6874cb5300bSChris Mason  * When we're defragging a range, we don't want to kick it off again
6884cb5300bSChris Mason  * if it is really just waiting for delalloc to send it down.
6894cb5300bSChris Mason  * If we find a nice big extent or delalloc range for the bytes in the
6904cb5300bSChris Mason  * file you want to defrag, we return 0 to let you know to skip this
6914cb5300bSChris Mason  * part of the file
6924cb5300bSChris Mason  */
6934cb5300bSChris Mason static int check_defrag_in_cache(struct inode *inode, u64 offset, int thresh)
6944cb5300bSChris Mason {
6954cb5300bSChris Mason 	struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
6964cb5300bSChris Mason 	struct extent_map *em = NULL;
6974cb5300bSChris Mason 	struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
6984cb5300bSChris Mason 	u64 end;
6994cb5300bSChris Mason 
7004cb5300bSChris Mason 	read_lock(&em_tree->lock);
7014cb5300bSChris Mason 	em = lookup_extent_mapping(em_tree, offset, PAGE_CACHE_SIZE);
7024cb5300bSChris Mason 	read_unlock(&em_tree->lock);
7034cb5300bSChris Mason 
7044cb5300bSChris Mason 	if (em) {
7054cb5300bSChris Mason 		end = extent_map_end(em);
7064cb5300bSChris Mason 		free_extent_map(em);
7074cb5300bSChris Mason 		if (end - offset > thresh)
7084cb5300bSChris Mason 			return 0;
7094cb5300bSChris Mason 	}
7104cb5300bSChris Mason 	/* if we already have a nice delalloc here, just stop */
7114cb5300bSChris Mason 	thresh /= 2;
7124cb5300bSChris Mason 	end = count_range_bits(io_tree, &offset, offset + thresh,
7134cb5300bSChris Mason 			       thresh, EXTENT_DELALLOC, 1);
7144cb5300bSChris Mason 	if (end >= thresh)
7154cb5300bSChris Mason 		return 0;
7164cb5300bSChris Mason 	return 1;
7174cb5300bSChris Mason }
7184cb5300bSChris Mason 
7194cb5300bSChris Mason /*
7204cb5300bSChris Mason  * helper function to walk through a file and find extents
7214cb5300bSChris Mason  * newer than a specific transid, and smaller than thresh.
7224cb5300bSChris Mason  *
7234cb5300bSChris Mason  * This is used by the defragging code to find new and small
7244cb5300bSChris Mason  * extents
7254cb5300bSChris Mason  */
7264cb5300bSChris Mason static int find_new_extents(struct btrfs_root *root,
7274cb5300bSChris Mason 			    struct inode *inode, u64 newer_than,
7284cb5300bSChris Mason 			    u64 *off, int thresh)
7294cb5300bSChris Mason {
7304cb5300bSChris Mason 	struct btrfs_path *path;
7314cb5300bSChris Mason 	struct btrfs_key min_key;
7324cb5300bSChris Mason 	struct btrfs_key max_key;
7334cb5300bSChris Mason 	struct extent_buffer *leaf;
7344cb5300bSChris Mason 	struct btrfs_file_extent_item *extent;
7354cb5300bSChris Mason 	int type;
7364cb5300bSChris Mason 	int ret;
737a4689d2bSDavid Sterba 	u64 ino = btrfs_ino(inode);
7384cb5300bSChris Mason 
7394cb5300bSChris Mason 	path = btrfs_alloc_path();
7404cb5300bSChris Mason 	if (!path)
7414cb5300bSChris Mason 		return -ENOMEM;
7424cb5300bSChris Mason 
743a4689d2bSDavid Sterba 	min_key.objectid = ino;
7444cb5300bSChris Mason 	min_key.type = BTRFS_EXTENT_DATA_KEY;
7454cb5300bSChris Mason 	min_key.offset = *off;
7464cb5300bSChris Mason 
747a4689d2bSDavid Sterba 	max_key.objectid = ino;
7484cb5300bSChris Mason 	max_key.type = (u8)-1;
7494cb5300bSChris Mason 	max_key.offset = (u64)-1;
7504cb5300bSChris Mason 
7514cb5300bSChris Mason 	path->keep_locks = 1;
7524cb5300bSChris Mason 
7534cb5300bSChris Mason 	while(1) {
7544cb5300bSChris Mason 		ret = btrfs_search_forward(root, &min_key, &max_key,
7554cb5300bSChris Mason 					   path, 0, newer_than);
7564cb5300bSChris Mason 		if (ret != 0)
7574cb5300bSChris Mason 			goto none;
758a4689d2bSDavid Sterba 		if (min_key.objectid != ino)
7594cb5300bSChris Mason 			goto none;
7604cb5300bSChris Mason 		if (min_key.type != BTRFS_EXTENT_DATA_KEY)
7614cb5300bSChris Mason 			goto none;
7624cb5300bSChris Mason 
7634cb5300bSChris Mason 		leaf = path->nodes[0];
7644cb5300bSChris Mason 		extent = btrfs_item_ptr(leaf, path->slots[0],
7654cb5300bSChris Mason 					struct btrfs_file_extent_item);
7664cb5300bSChris Mason 
7674cb5300bSChris Mason 		type = btrfs_file_extent_type(leaf, extent);
7684cb5300bSChris Mason 		if (type == BTRFS_FILE_EXTENT_REG &&
7694cb5300bSChris Mason 		    btrfs_file_extent_num_bytes(leaf, extent) < thresh &&
7704cb5300bSChris Mason 		    check_defrag_in_cache(inode, min_key.offset, thresh)) {
7714cb5300bSChris Mason 			*off = min_key.offset;
7724cb5300bSChris Mason 			btrfs_free_path(path);
7734cb5300bSChris Mason 			return 0;
7744cb5300bSChris Mason 		}
7754cb5300bSChris Mason 
7764cb5300bSChris Mason 		if (min_key.offset == (u64)-1)
7774cb5300bSChris Mason 			goto none;
7784cb5300bSChris Mason 
7794cb5300bSChris Mason 		min_key.offset++;
7804cb5300bSChris Mason 		btrfs_release_path(path);
7814cb5300bSChris Mason 	}
7824cb5300bSChris Mason none:
7834cb5300bSChris Mason 	btrfs_free_path(path);
7844cb5300bSChris Mason 	return -ENOENT;
7854cb5300bSChris Mason }
7864cb5300bSChris Mason 
787940100a4SChris Mason static int should_defrag_range(struct inode *inode, u64 start, u64 len,
7881e701a32SChris Mason 			       int thresh, u64 *last_len, u64 *skip,
7891e701a32SChris Mason 			       u64 *defrag_end)
790940100a4SChris Mason {
791940100a4SChris Mason 	struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
792940100a4SChris Mason 	struct extent_map *em = NULL;
793940100a4SChris Mason 	struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
794940100a4SChris Mason 	int ret = 1;
795940100a4SChris Mason 
796940100a4SChris Mason 	/*
797008873eaSLi Zefan 	 * make sure that once we start defragging an extent, we keep on
798940100a4SChris Mason 	 * defragging it
799940100a4SChris Mason 	 */
800940100a4SChris Mason 	if (start < *defrag_end)
801940100a4SChris Mason 		return 1;
802940100a4SChris Mason 
803940100a4SChris Mason 	*skip = 0;
804940100a4SChris Mason 
805940100a4SChris Mason 	/*
806940100a4SChris Mason 	 * hopefully we have this extent in the tree already, try without
807940100a4SChris Mason 	 * the full extent lock
808940100a4SChris Mason 	 */
809940100a4SChris Mason 	read_lock(&em_tree->lock);
810940100a4SChris Mason 	em = lookup_extent_mapping(em_tree, start, len);
811940100a4SChris Mason 	read_unlock(&em_tree->lock);
812940100a4SChris Mason 
813940100a4SChris Mason 	if (!em) {
814940100a4SChris Mason 		/* get the big lock and read metadata off disk */
815d0082371SJeff Mahoney 		lock_extent(io_tree, start, start + len - 1);
816940100a4SChris Mason 		em = btrfs_get_extent(inode, NULL, 0, start, len, 0);
817d0082371SJeff Mahoney 		unlock_extent(io_tree, start, start + len - 1);
818940100a4SChris Mason 
8196cf8bfbfSDan Carpenter 		if (IS_ERR(em))
820940100a4SChris Mason 			return 0;
821940100a4SChris Mason 	}
822940100a4SChris Mason 
823940100a4SChris Mason 	/* this will cover holes, and inline extents */
824940100a4SChris Mason 	if (em->block_start >= EXTENT_MAP_LAST_BYTE)
825940100a4SChris Mason 		ret = 0;
826940100a4SChris Mason 
827940100a4SChris Mason 	/*
828940100a4SChris Mason 	 * we hit a real extent, if it is big don't bother defragging it again
829940100a4SChris Mason 	 */
8301e701a32SChris Mason 	if ((*last_len == 0 || *last_len >= thresh) && em->len >= thresh)
831940100a4SChris Mason 		ret = 0;
832940100a4SChris Mason 
833940100a4SChris Mason 	/*
834940100a4SChris Mason 	 * last_len ends up being a counter of how many bytes we've defragged.
835940100a4SChris Mason 	 * every time we choose not to defrag an extent, we reset *last_len
836940100a4SChris Mason 	 * so that the next tiny extent will force a defrag.
837940100a4SChris Mason 	 *
838940100a4SChris Mason 	 * The end result of this is that tiny extents before a single big
839940100a4SChris Mason 	 * extent will force at least part of that big extent to be defragged.
840940100a4SChris Mason 	 */
841940100a4SChris Mason 	if (ret) {
842940100a4SChris Mason 		*defrag_end = extent_map_end(em);
843940100a4SChris Mason 	} else {
844940100a4SChris Mason 		*last_len = 0;
845940100a4SChris Mason 		*skip = extent_map_end(em);
846940100a4SChris Mason 		*defrag_end = 0;
847940100a4SChris Mason 	}
848940100a4SChris Mason 
849940100a4SChris Mason 	free_extent_map(em);
850940100a4SChris Mason 	return ret;
851940100a4SChris Mason }
852940100a4SChris Mason 
8534cb5300bSChris Mason /*
8544cb5300bSChris Mason  * it doesn't do much good to defrag one or two pages
8554cb5300bSChris Mason  * at a time.  This pulls in a nice chunk of pages
8564cb5300bSChris Mason  * to COW and defrag.
8574cb5300bSChris Mason  *
8584cb5300bSChris Mason  * It also makes sure the delalloc code has enough
8594cb5300bSChris Mason  * dirty data to avoid making new small extents as part
8604cb5300bSChris Mason  * of the defrag
8614cb5300bSChris Mason  *
8624cb5300bSChris Mason  * It's a good idea to start RA on this range
8634cb5300bSChris Mason  * before calling this.
8644cb5300bSChris Mason  */
8654cb5300bSChris Mason static int cluster_pages_for_defrag(struct inode *inode,
8664cb5300bSChris Mason 				    struct page **pages,
8674cb5300bSChris Mason 				    unsigned long start_index,
8684cb5300bSChris Mason 				    int num_pages)
869f46b5a66SChristoph Hellwig {
8704cb5300bSChris Mason 	unsigned long file_end;
8714cb5300bSChris Mason 	u64 isize = i_size_read(inode);
872f46b5a66SChristoph Hellwig 	u64 page_start;
873f46b5a66SChristoph Hellwig 	u64 page_end;
8744cb5300bSChris Mason 	int ret;
8754cb5300bSChris Mason 	int i;
8764cb5300bSChris Mason 	int i_done;
8774cb5300bSChris Mason 	struct btrfs_ordered_extent *ordered;
8784cb5300bSChris Mason 	struct extent_state *cached_state = NULL;
879600a45e1SMiao Xie 	struct extent_io_tree *tree;
8803b16a4e3SJosef Bacik 	gfp_t mask = btrfs_alloc_write_mask(inode->i_mapping);
8814cb5300bSChris Mason 
8824cb5300bSChris Mason 	if (isize == 0)
8834cb5300bSChris Mason 		return 0;
8844cb5300bSChris Mason 	file_end = (isize - 1) >> PAGE_CACHE_SHIFT;
8854cb5300bSChris Mason 
8864cb5300bSChris Mason 	ret = btrfs_delalloc_reserve_space(inode,
8874cb5300bSChris Mason 					   num_pages << PAGE_CACHE_SHIFT);
8884cb5300bSChris Mason 	if (ret)
8894cb5300bSChris Mason 		return ret;
8904cb5300bSChris Mason 	i_done = 0;
891600a45e1SMiao Xie 	tree = &BTRFS_I(inode)->io_tree;
8924cb5300bSChris Mason 
8934cb5300bSChris Mason 	/* step one, lock all the pages */
8944cb5300bSChris Mason 	for (i = 0; i < num_pages; i++) {
8954cb5300bSChris Mason 		struct page *page;
896600a45e1SMiao Xie again:
897a94733d0SJosef Bacik 		page = find_or_create_page(inode->i_mapping,
8983b16a4e3SJosef Bacik 					   start_index + i, mask);
8994cb5300bSChris Mason 		if (!page)
9004cb5300bSChris Mason 			break;
9014cb5300bSChris Mason 
902600a45e1SMiao Xie 		page_start = page_offset(page);
903600a45e1SMiao Xie 		page_end = page_start + PAGE_CACHE_SIZE - 1;
904600a45e1SMiao Xie 		while (1) {
905d0082371SJeff Mahoney 			lock_extent(tree, page_start, page_end);
906600a45e1SMiao Xie 			ordered = btrfs_lookup_ordered_extent(inode,
907600a45e1SMiao Xie 							      page_start);
908d0082371SJeff Mahoney 			unlock_extent(tree, page_start, page_end);
909600a45e1SMiao Xie 			if (!ordered)
910600a45e1SMiao Xie 				break;
911600a45e1SMiao Xie 
912600a45e1SMiao Xie 			unlock_page(page);
913600a45e1SMiao Xie 			btrfs_start_ordered_extent(inode, ordered, 1);
914600a45e1SMiao Xie 			btrfs_put_ordered_extent(ordered);
915600a45e1SMiao Xie 			lock_page(page);
916600a45e1SMiao Xie 		}
917600a45e1SMiao Xie 
9184cb5300bSChris Mason 		if (!PageUptodate(page)) {
9194cb5300bSChris Mason 			btrfs_readpage(NULL, page);
9204cb5300bSChris Mason 			lock_page(page);
9214cb5300bSChris Mason 			if (!PageUptodate(page)) {
9224cb5300bSChris Mason 				unlock_page(page);
9234cb5300bSChris Mason 				page_cache_release(page);
9244cb5300bSChris Mason 				ret = -EIO;
9254cb5300bSChris Mason 				break;
9264cb5300bSChris Mason 			}
9274cb5300bSChris Mason 		}
928600a45e1SMiao Xie 
9294cb5300bSChris Mason 		isize = i_size_read(inode);
9304cb5300bSChris Mason 		file_end = (isize - 1) >> PAGE_CACHE_SHIFT;
931600a45e1SMiao Xie 		if (!isize || page->index > file_end) {
9324cb5300bSChris Mason 			/* whoops, we blew past eof, skip this page */
9334cb5300bSChris Mason 			unlock_page(page);
9344cb5300bSChris Mason 			page_cache_release(page);
9354cb5300bSChris Mason 			break;
9364cb5300bSChris Mason 		}
937600a45e1SMiao Xie 
938600a45e1SMiao Xie 		if (page->mapping != inode->i_mapping) {
939600a45e1SMiao Xie 			unlock_page(page);
940600a45e1SMiao Xie 			page_cache_release(page);
941600a45e1SMiao Xie 			goto again;
942600a45e1SMiao Xie 		}
943600a45e1SMiao Xie 
9444cb5300bSChris Mason 		pages[i] = page;
9454cb5300bSChris Mason 		i_done++;
9464cb5300bSChris Mason 	}
9474cb5300bSChris Mason 	if (!i_done || ret)
9484cb5300bSChris Mason 		goto out;
9494cb5300bSChris Mason 
9504cb5300bSChris Mason 	if (!(inode->i_sb->s_flags & MS_ACTIVE))
9514cb5300bSChris Mason 		goto out;
9524cb5300bSChris Mason 
9534cb5300bSChris Mason 	/*
9544cb5300bSChris Mason 	 * so now we have a nice long stream of locked
9554cb5300bSChris Mason 	 * and up to date pages, lets wait on them
9564cb5300bSChris Mason 	 */
9574cb5300bSChris Mason 	for (i = 0; i < i_done; i++)
9584cb5300bSChris Mason 		wait_on_page_writeback(pages[i]);
9594cb5300bSChris Mason 
9604cb5300bSChris Mason 	page_start = page_offset(pages[0]);
9614cb5300bSChris Mason 	page_end = page_offset(pages[i_done - 1]) + PAGE_CACHE_SIZE;
9624cb5300bSChris Mason 
9634cb5300bSChris Mason 	lock_extent_bits(&BTRFS_I(inode)->io_tree,
964d0082371SJeff Mahoney 			 page_start, page_end - 1, 0, &cached_state);
9654cb5300bSChris Mason 	clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start,
9664cb5300bSChris Mason 			  page_end - 1, EXTENT_DIRTY | EXTENT_DELALLOC |
9674cb5300bSChris Mason 			  EXTENT_DO_ACCOUNTING, 0, 0, &cached_state,
9684cb5300bSChris Mason 			  GFP_NOFS);
9694cb5300bSChris Mason 
9704cb5300bSChris Mason 	if (i_done != num_pages) {
9719e0baf60SJosef Bacik 		spin_lock(&BTRFS_I(inode)->lock);
9729e0baf60SJosef Bacik 		BTRFS_I(inode)->outstanding_extents++;
9739e0baf60SJosef Bacik 		spin_unlock(&BTRFS_I(inode)->lock);
9744cb5300bSChris Mason 		btrfs_delalloc_release_space(inode,
9754cb5300bSChris Mason 				     (num_pages - i_done) << PAGE_CACHE_SHIFT);
9764cb5300bSChris Mason 	}
9774cb5300bSChris Mason 
9784cb5300bSChris Mason 
9794cb5300bSChris Mason 	btrfs_set_extent_delalloc(inode, page_start, page_end - 1,
9804cb5300bSChris Mason 				  &cached_state);
9814cb5300bSChris Mason 
9824cb5300bSChris Mason 	unlock_extent_cached(&BTRFS_I(inode)->io_tree,
9834cb5300bSChris Mason 			     page_start, page_end - 1, &cached_state,
9844cb5300bSChris Mason 			     GFP_NOFS);
9854cb5300bSChris Mason 
9864cb5300bSChris Mason 	for (i = 0; i < i_done; i++) {
9874cb5300bSChris Mason 		clear_page_dirty_for_io(pages[i]);
9884cb5300bSChris Mason 		ClearPageChecked(pages[i]);
9894cb5300bSChris Mason 		set_page_extent_mapped(pages[i]);
9904cb5300bSChris Mason 		set_page_dirty(pages[i]);
9914cb5300bSChris Mason 		unlock_page(pages[i]);
9924cb5300bSChris Mason 		page_cache_release(pages[i]);
9934cb5300bSChris Mason 	}
9944cb5300bSChris Mason 	return i_done;
9954cb5300bSChris Mason out:
9964cb5300bSChris Mason 	for (i = 0; i < i_done; i++) {
9974cb5300bSChris Mason 		unlock_page(pages[i]);
9984cb5300bSChris Mason 		page_cache_release(pages[i]);
9994cb5300bSChris Mason 	}
10004cb5300bSChris Mason 	btrfs_delalloc_release_space(inode, num_pages << PAGE_CACHE_SHIFT);
10014cb5300bSChris Mason 	return ret;
10024cb5300bSChris Mason 
10034cb5300bSChris Mason }
10044cb5300bSChris Mason 
10054cb5300bSChris Mason int btrfs_defrag_file(struct inode *inode, struct file *file,
10064cb5300bSChris Mason 		      struct btrfs_ioctl_defrag_range_args *range,
10074cb5300bSChris Mason 		      u64 newer_than, unsigned long max_to_defrag)
10084cb5300bSChris Mason {
10094cb5300bSChris Mason 	struct btrfs_root *root = BTRFS_I(inode)->root;
10104cb5300bSChris Mason 	struct btrfs_super_block *disk_super;
10114cb5300bSChris Mason 	struct file_ra_state *ra = NULL;
10124cb5300bSChris Mason 	unsigned long last_index;
1013151a31b2SLi Zefan 	u64 isize = i_size_read(inode);
10144cb5300bSChris Mason 	u64 features;
1015940100a4SChris Mason 	u64 last_len = 0;
1016940100a4SChris Mason 	u64 skip = 0;
1017940100a4SChris Mason 	u64 defrag_end = 0;
10184cb5300bSChris Mason 	u64 newer_off = range->start;
1019f46b5a66SChristoph Hellwig 	unsigned long i;
1020008873eaSLi Zefan 	unsigned long ra_index = 0;
1021f46b5a66SChristoph Hellwig 	int ret;
10224cb5300bSChris Mason 	int defrag_count = 0;
10231a419d85SLi Zefan 	int compress_type = BTRFS_COMPRESS_ZLIB;
10244cb5300bSChris Mason 	int extent_thresh = range->extent_thresh;
1025008873eaSLi Zefan 	int max_cluster = (256 * 1024) >> PAGE_CACHE_SHIFT;
1026008873eaSLi Zefan 	int cluster = max_cluster;
10274cb5300bSChris Mason 	u64 new_align = ~((u64)128 * 1024 - 1);
10284cb5300bSChris Mason 	struct page **pages = NULL;
10294cb5300bSChris Mason 
10304cb5300bSChris Mason 	if (extent_thresh == 0)
10314cb5300bSChris Mason 		extent_thresh = 256 * 1024;
10321a419d85SLi Zefan 
10331a419d85SLi Zefan 	if (range->flags & BTRFS_DEFRAG_RANGE_COMPRESS) {
10341a419d85SLi Zefan 		if (range->compress_type > BTRFS_COMPRESS_TYPES)
10351a419d85SLi Zefan 			return -EINVAL;
10361a419d85SLi Zefan 		if (range->compress_type)
10371a419d85SLi Zefan 			compress_type = range->compress_type;
10381a419d85SLi Zefan 	}
1039f46b5a66SChristoph Hellwig 
1040151a31b2SLi Zefan 	if (isize == 0)
1041940100a4SChris Mason 		return 0;
1042f46b5a66SChristoph Hellwig 
10434cb5300bSChris Mason 	/*
10444cb5300bSChris Mason 	 * if we were not given a file, allocate a readahead
10454cb5300bSChris Mason 	 * context
10464cb5300bSChris Mason 	 */
10474cb5300bSChris Mason 	if (!file) {
10484cb5300bSChris Mason 		ra = kzalloc(sizeof(*ra), GFP_NOFS);
10494cb5300bSChris Mason 		if (!ra)
10504cb5300bSChris Mason 			return -ENOMEM;
10514cb5300bSChris Mason 		file_ra_state_init(ra, inode->i_mapping);
10524cb5300bSChris Mason 	} else {
10534cb5300bSChris Mason 		ra = &file->f_ra;
10544cb5300bSChris Mason 	}
10554cb5300bSChris Mason 
1056008873eaSLi Zefan 	pages = kmalloc(sizeof(struct page *) * max_cluster,
10574cb5300bSChris Mason 			GFP_NOFS);
10584cb5300bSChris Mason 	if (!pages) {
10594cb5300bSChris Mason 		ret = -ENOMEM;
10604cb5300bSChris Mason 		goto out_ra;
10614cb5300bSChris Mason 	}
10624cb5300bSChris Mason 
10634cb5300bSChris Mason 	/* find the last page to defrag */
10641e701a32SChris Mason 	if (range->start + range->len > range->start) {
1065151a31b2SLi Zefan 		last_index = min_t(u64, isize - 1,
10661e701a32SChris Mason 			 range->start + range->len - 1) >> PAGE_CACHE_SHIFT;
10671e701a32SChris Mason 	} else {
1068151a31b2SLi Zefan 		last_index = (isize - 1) >> PAGE_CACHE_SHIFT;
10691e701a32SChris Mason 	}
10701e701a32SChris Mason 
10714cb5300bSChris Mason 	if (newer_than) {
10724cb5300bSChris Mason 		ret = find_new_extents(root, inode, newer_than,
10734cb5300bSChris Mason 				       &newer_off, 64 * 1024);
10744cb5300bSChris Mason 		if (!ret) {
10754cb5300bSChris Mason 			range->start = newer_off;
10764cb5300bSChris Mason 			/*
10774cb5300bSChris Mason 			 * we always align our defrag to help keep
10784cb5300bSChris Mason 			 * the extents in the file evenly spaced
10794cb5300bSChris Mason 			 */
10804cb5300bSChris Mason 			i = (newer_off & new_align) >> PAGE_CACHE_SHIFT;
10814cb5300bSChris Mason 		} else
10824cb5300bSChris Mason 			goto out_ra;
10834cb5300bSChris Mason 	} else {
10841e701a32SChris Mason 		i = range->start >> PAGE_CACHE_SHIFT;
10854cb5300bSChris Mason 	}
10864cb5300bSChris Mason 	if (!max_to_defrag)
10877ec31b54SLiu Bo 		max_to_defrag = last_index + 1;
10884cb5300bSChris Mason 
10892a0f7f57SLi Zefan 	/*
10902a0f7f57SLi Zefan 	 * make writeback starts from i, so the defrag range can be
10912a0f7f57SLi Zefan 	 * written sequentially.
10922a0f7f57SLi Zefan 	 */
10932a0f7f57SLi Zefan 	if (i < inode->i_mapping->writeback_index)
10942a0f7f57SLi Zefan 		inode->i_mapping->writeback_index = i;
10952a0f7f57SLi Zefan 
1096f7f43cc8SChris Mason 	while (i <= last_index && defrag_count < max_to_defrag &&
1097f7f43cc8SChris Mason 	       (i < (i_size_read(inode) + PAGE_CACHE_SIZE - 1) >>
1098f7f43cc8SChris Mason 		PAGE_CACHE_SHIFT)) {
10994cb5300bSChris Mason 		/*
11004cb5300bSChris Mason 		 * make sure we stop running if someone unmounts
11014cb5300bSChris Mason 		 * the FS
11024cb5300bSChris Mason 		 */
11034cb5300bSChris Mason 		if (!(inode->i_sb->s_flags & MS_ACTIVE))
11044cb5300bSChris Mason 			break;
11054cb5300bSChris Mason 
11064cb5300bSChris Mason 		if (!newer_than &&
11074cb5300bSChris Mason 		    !should_defrag_range(inode, (u64)i << PAGE_CACHE_SHIFT,
11081e701a32SChris Mason 					PAGE_CACHE_SIZE,
11094cb5300bSChris Mason 					extent_thresh,
11101e701a32SChris Mason 					&last_len, &skip,
1111940100a4SChris Mason 					&defrag_end)) {
1112940100a4SChris Mason 			unsigned long next;
1113940100a4SChris Mason 			/*
1114940100a4SChris Mason 			 * the should_defrag function tells us how much to skip
1115940100a4SChris Mason 			 * bump our counter by the suggested amount
1116940100a4SChris Mason 			 */
1117940100a4SChris Mason 			next = (skip + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
1118940100a4SChris Mason 			i = max(i + 1, next);
1119940100a4SChris Mason 			continue;
1120940100a4SChris Mason 		}
1121008873eaSLi Zefan 
1122008873eaSLi Zefan 		if (!newer_than) {
1123008873eaSLi Zefan 			cluster = (PAGE_CACHE_ALIGN(defrag_end) >>
1124008873eaSLi Zefan 				   PAGE_CACHE_SHIFT) - i;
1125008873eaSLi Zefan 			cluster = min(cluster, max_cluster);
1126008873eaSLi Zefan 		} else {
1127008873eaSLi Zefan 			cluster = max_cluster;
1128008873eaSLi Zefan 		}
1129008873eaSLi Zefan 
11301e701a32SChris Mason 		if (range->flags & BTRFS_DEFRAG_RANGE_COMPRESS)
11311a419d85SLi Zefan 			BTRFS_I(inode)->force_compress = compress_type;
1132940100a4SChris Mason 
1133008873eaSLi Zefan 		if (i + cluster > ra_index) {
1134008873eaSLi Zefan 			ra_index = max(i, ra_index);
1135008873eaSLi Zefan 			btrfs_force_ra(inode->i_mapping, ra, file, ra_index,
1136008873eaSLi Zefan 				       cluster);
1137008873eaSLi Zefan 			ra_index += max_cluster;
1138008873eaSLi Zefan 		}
11394cb5300bSChris Mason 
1140008873eaSLi Zefan 		ret = cluster_pages_for_defrag(inode, pages, i, cluster);
11414cb5300bSChris Mason 		if (ret < 0)
11424cb5300bSChris Mason 			goto out_ra;
11434cb5300bSChris Mason 
11444cb5300bSChris Mason 		defrag_count += ret;
11454cb5300bSChris Mason 		balance_dirty_pages_ratelimited_nr(inode->i_mapping, ret);
11464cb5300bSChris Mason 
11474cb5300bSChris Mason 		if (newer_than) {
11484cb5300bSChris Mason 			if (newer_off == (u64)-1)
11494cb5300bSChris Mason 				break;
11504cb5300bSChris Mason 
11514cb5300bSChris Mason 			newer_off = max(newer_off + 1,
11524cb5300bSChris Mason 					(u64)i << PAGE_CACHE_SHIFT);
11534cb5300bSChris Mason 
11544cb5300bSChris Mason 			ret = find_new_extents(root, inode,
11554cb5300bSChris Mason 					       newer_than, &newer_off,
11564cb5300bSChris Mason 					       64 * 1024);
11574cb5300bSChris Mason 			if (!ret) {
11584cb5300bSChris Mason 				range->start = newer_off;
11594cb5300bSChris Mason 				i = (newer_off & new_align) >> PAGE_CACHE_SHIFT;
11604cb5300bSChris Mason 			} else {
11614cb5300bSChris Mason 				break;
1162940100a4SChris Mason 			}
11634cb5300bSChris Mason 		} else {
1164008873eaSLi Zefan 			if (ret > 0) {
1165cbcc8326SLi Zefan 				i += ret;
1166008873eaSLi Zefan 				last_len += ret << PAGE_CACHE_SHIFT;
1167008873eaSLi Zefan 			} else {
1168940100a4SChris Mason 				i++;
1169008873eaSLi Zefan 				last_len = 0;
1170008873eaSLi Zefan 			}
1171f46b5a66SChristoph Hellwig 		}
11724cb5300bSChris Mason 	}
1173f46b5a66SChristoph Hellwig 
11741e701a32SChris Mason 	if ((range->flags & BTRFS_DEFRAG_RANGE_START_IO))
11751e701a32SChris Mason 		filemap_flush(inode->i_mapping);
11761e701a32SChris Mason 
11771e701a32SChris Mason 	if ((range->flags & BTRFS_DEFRAG_RANGE_COMPRESS)) {
11781e701a32SChris Mason 		/* the filemap_flush will queue IO into the worker threads, but
11791e701a32SChris Mason 		 * we have to make sure the IO is actually started and that
11801e701a32SChris Mason 		 * ordered extents get created before we return
11811e701a32SChris Mason 		 */
11821e701a32SChris Mason 		atomic_inc(&root->fs_info->async_submit_draining);
11831e701a32SChris Mason 		while (atomic_read(&root->fs_info->nr_async_submits) ||
11841e701a32SChris Mason 		      atomic_read(&root->fs_info->async_delalloc_pages)) {
11851e701a32SChris Mason 			wait_event(root->fs_info->async_submit_wait,
11861e701a32SChris Mason 			   (atomic_read(&root->fs_info->nr_async_submits) == 0 &&
11871e701a32SChris Mason 			    atomic_read(&root->fs_info->async_delalloc_pages) == 0));
11881e701a32SChris Mason 		}
11891e701a32SChris Mason 		atomic_dec(&root->fs_info->async_submit_draining);
11901e701a32SChris Mason 
11911e701a32SChris Mason 		mutex_lock(&inode->i_mutex);
1192261507a0SLi Zefan 		BTRFS_I(inode)->force_compress = BTRFS_COMPRESS_NONE;
11931e701a32SChris Mason 		mutex_unlock(&inode->i_mutex);
11941e701a32SChris Mason 	}
11951e701a32SChris Mason 
11966c41761fSDavid Sterba 	disk_super = root->fs_info->super_copy;
11971a419d85SLi Zefan 	features = btrfs_super_incompat_flags(disk_super);
11981a419d85SLi Zefan 	if (range->compress_type == BTRFS_COMPRESS_LZO) {
11991a419d85SLi Zefan 		features |= BTRFS_FEATURE_INCOMPAT_COMPRESS_LZO;
12001a419d85SLi Zefan 		btrfs_set_super_incompat_flags(disk_super, features);
12011a419d85SLi Zefan 	}
12021a419d85SLi Zefan 
120360ccf82fSDiego Calleja 	ret = defrag_count;
1204940100a4SChris Mason 
12054cb5300bSChris Mason out_ra:
12064cb5300bSChris Mason 	if (!file)
12074cb5300bSChris Mason 		kfree(ra);
12084cb5300bSChris Mason 	kfree(pages);
1209940100a4SChris Mason 	return ret;
1210f46b5a66SChristoph Hellwig }
1211f46b5a66SChristoph Hellwig 
121276dda93cSYan, Zheng static noinline int btrfs_ioctl_resize(struct btrfs_root *root,
121376dda93cSYan, Zheng 					void __user *arg)
1214f46b5a66SChristoph Hellwig {
1215f46b5a66SChristoph Hellwig 	u64 new_size;
1216f46b5a66SChristoph Hellwig 	u64 old_size;
1217f46b5a66SChristoph Hellwig 	u64 devid = 1;
1218f46b5a66SChristoph Hellwig 	struct btrfs_ioctl_vol_args *vol_args;
1219f46b5a66SChristoph Hellwig 	struct btrfs_trans_handle *trans;
1220f46b5a66SChristoph Hellwig 	struct btrfs_device *device = NULL;
1221f46b5a66SChristoph Hellwig 	char *sizestr;
1222f46b5a66SChristoph Hellwig 	char *devstr = NULL;
1223f46b5a66SChristoph Hellwig 	int ret = 0;
1224f46b5a66SChristoph Hellwig 	int mod = 0;
1225f46b5a66SChristoph Hellwig 
1226c146afadSYan Zheng 	if (root->fs_info->sb->s_flags & MS_RDONLY)
1227c146afadSYan Zheng 		return -EROFS;
1228c146afadSYan Zheng 
1229e441d54dSChris Mason 	if (!capable(CAP_SYS_ADMIN))
1230e441d54dSChris Mason 		return -EPERM;
1231e441d54dSChris Mason 
1232c9e9f97bSIlya Dryomov 	mutex_lock(&root->fs_info->volume_mutex);
1233c9e9f97bSIlya Dryomov 	if (root->fs_info->balance_ctl) {
1234c9e9f97bSIlya Dryomov 		printk(KERN_INFO "btrfs: balance in progress\n");
1235c9e9f97bSIlya Dryomov 		ret = -EINVAL;
1236c9e9f97bSIlya Dryomov 		goto out;
1237c9e9f97bSIlya Dryomov 	}
1238c9e9f97bSIlya Dryomov 
1239dae7b665SLi Zefan 	vol_args = memdup_user(arg, sizeof(*vol_args));
1240c9e9f97bSIlya Dryomov 	if (IS_ERR(vol_args)) {
1241c9e9f97bSIlya Dryomov 		ret = PTR_ERR(vol_args);
1242c9e9f97bSIlya Dryomov 		goto out;
1243c9e9f97bSIlya Dryomov 	}
12445516e595SMark Fasheh 
12455516e595SMark Fasheh 	vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
1246f46b5a66SChristoph Hellwig 
1247f46b5a66SChristoph Hellwig 	sizestr = vol_args->name;
1248f46b5a66SChristoph Hellwig 	devstr = strchr(sizestr, ':');
1249f46b5a66SChristoph Hellwig 	if (devstr) {
1250f46b5a66SChristoph Hellwig 		char *end;
1251f46b5a66SChristoph Hellwig 		sizestr = devstr + 1;
1252f46b5a66SChristoph Hellwig 		*devstr = '\0';
1253f46b5a66SChristoph Hellwig 		devstr = vol_args->name;
1254f46b5a66SChristoph Hellwig 		devid = simple_strtoull(devstr, &end, 10);
12555bb14682SArnd Hannemann 		printk(KERN_INFO "btrfs: resizing devid %llu\n",
125621380931SJoel Becker 		       (unsigned long long)devid);
1257f46b5a66SChristoph Hellwig 	}
12582b82032cSYan Zheng 	device = btrfs_find_device(root, devid, NULL, NULL);
1259f46b5a66SChristoph Hellwig 	if (!device) {
12605bb14682SArnd Hannemann 		printk(KERN_INFO "btrfs: resizer unable to find device %llu\n",
126121380931SJoel Becker 		       (unsigned long long)devid);
1262f46b5a66SChristoph Hellwig 		ret = -EINVAL;
1263c9e9f97bSIlya Dryomov 		goto out_free;
1264f46b5a66SChristoph Hellwig 	}
1265f46b5a66SChristoph Hellwig 	if (!strcmp(sizestr, "max"))
1266f46b5a66SChristoph Hellwig 		new_size = device->bdev->bd_inode->i_size;
1267f46b5a66SChristoph Hellwig 	else {
1268f46b5a66SChristoph Hellwig 		if (sizestr[0] == '-') {
1269f46b5a66SChristoph Hellwig 			mod = -1;
1270f46b5a66SChristoph Hellwig 			sizestr++;
1271f46b5a66SChristoph Hellwig 		} else if (sizestr[0] == '+') {
1272f46b5a66SChristoph Hellwig 			mod = 1;
1273f46b5a66SChristoph Hellwig 			sizestr++;
1274f46b5a66SChristoph Hellwig 		}
127591748467SAkinobu Mita 		new_size = memparse(sizestr, NULL);
1276f46b5a66SChristoph Hellwig 		if (new_size == 0) {
1277f46b5a66SChristoph Hellwig 			ret = -EINVAL;
1278c9e9f97bSIlya Dryomov 			goto out_free;
1279f46b5a66SChristoph Hellwig 		}
1280f46b5a66SChristoph Hellwig 	}
1281f46b5a66SChristoph Hellwig 
1282f46b5a66SChristoph Hellwig 	old_size = device->total_bytes;
1283f46b5a66SChristoph Hellwig 
1284f46b5a66SChristoph Hellwig 	if (mod < 0) {
1285f46b5a66SChristoph Hellwig 		if (new_size > old_size) {
1286f46b5a66SChristoph Hellwig 			ret = -EINVAL;
1287c9e9f97bSIlya Dryomov 			goto out_free;
1288f46b5a66SChristoph Hellwig 		}
1289f46b5a66SChristoph Hellwig 		new_size = old_size - new_size;
1290f46b5a66SChristoph Hellwig 	} else if (mod > 0) {
1291f46b5a66SChristoph Hellwig 		new_size = old_size + new_size;
1292f46b5a66SChristoph Hellwig 	}
1293f46b5a66SChristoph Hellwig 
1294f46b5a66SChristoph Hellwig 	if (new_size < 256 * 1024 * 1024) {
1295f46b5a66SChristoph Hellwig 		ret = -EINVAL;
1296c9e9f97bSIlya Dryomov 		goto out_free;
1297f46b5a66SChristoph Hellwig 	}
1298f46b5a66SChristoph Hellwig 	if (new_size > device->bdev->bd_inode->i_size) {
1299f46b5a66SChristoph Hellwig 		ret = -EFBIG;
1300c9e9f97bSIlya Dryomov 		goto out_free;
1301f46b5a66SChristoph Hellwig 	}
1302f46b5a66SChristoph Hellwig 
1303f46b5a66SChristoph Hellwig 	do_div(new_size, root->sectorsize);
1304f46b5a66SChristoph Hellwig 	new_size *= root->sectorsize;
1305f46b5a66SChristoph Hellwig 
13065bb14682SArnd Hannemann 	printk(KERN_INFO "btrfs: new size for %s is %llu\n",
1307f46b5a66SChristoph Hellwig 		device->name, (unsigned long long)new_size);
1308f46b5a66SChristoph Hellwig 
1309f46b5a66SChristoph Hellwig 	if (new_size > old_size) {
1310a22285a6SYan, Zheng 		trans = btrfs_start_transaction(root, 0);
131198d5dc13STsutomu Itoh 		if (IS_ERR(trans)) {
131298d5dc13STsutomu Itoh 			ret = PTR_ERR(trans);
1313c9e9f97bSIlya Dryomov 			goto out_free;
131498d5dc13STsutomu Itoh 		}
1315f46b5a66SChristoph Hellwig 		ret = btrfs_grow_device(trans, device, new_size);
1316f46b5a66SChristoph Hellwig 		btrfs_commit_transaction(trans, root);
1317ece7d20eSMike Fleetwood 	} else if (new_size < old_size) {
1318f46b5a66SChristoph Hellwig 		ret = btrfs_shrink_device(device, new_size);
1319f46b5a66SChristoph Hellwig 	}
1320f46b5a66SChristoph Hellwig 
1321c9e9f97bSIlya Dryomov out_free:
1322f46b5a66SChristoph Hellwig 	kfree(vol_args);
1323c9e9f97bSIlya Dryomov out:
1324c9e9f97bSIlya Dryomov 	mutex_unlock(&root->fs_info->volume_mutex);
1325f46b5a66SChristoph Hellwig 	return ret;
1326f46b5a66SChristoph Hellwig }
1327f46b5a66SChristoph Hellwig 
132872fd032eSSage Weil static noinline int btrfs_ioctl_snap_create_transid(struct file *file,
132972fd032eSSage Weil 						    char *name,
133072fd032eSSage Weil 						    unsigned long fd,
133172fd032eSSage Weil 						    int subvol,
1332b83cc969SLi Zefan 						    u64 *transid,
1333b83cc969SLi Zefan 						    bool readonly)
1334f46b5a66SChristoph Hellwig {
1335cb8e7090SChristoph Hellwig 	struct btrfs_root *root = BTRFS_I(fdentry(file)->d_inode)->root;
13363de4586cSChris Mason 	struct file *src_file;
1337f46b5a66SChristoph Hellwig 	int namelen;
13383de4586cSChris Mason 	int ret = 0;
1339f46b5a66SChristoph Hellwig 
1340c146afadSYan Zheng 	if (root->fs_info->sb->s_flags & MS_RDONLY)
1341c146afadSYan Zheng 		return -EROFS;
1342c146afadSYan Zheng 
134372fd032eSSage Weil 	namelen = strlen(name);
134472fd032eSSage Weil 	if (strchr(name, '/')) {
1345f46b5a66SChristoph Hellwig 		ret = -EINVAL;
1346f46b5a66SChristoph Hellwig 		goto out;
1347f46b5a66SChristoph Hellwig 	}
1348f46b5a66SChristoph Hellwig 
134916780cabSChris Mason 	if (name[0] == '.' &&
135016780cabSChris Mason 	   (namelen == 1 || (name[1] == '.' && namelen == 2))) {
135116780cabSChris Mason 		ret = -EEXIST;
135216780cabSChris Mason 		goto out;
135316780cabSChris Mason 	}
135416780cabSChris Mason 
13553de4586cSChris Mason 	if (subvol) {
135672fd032eSSage Weil 		ret = btrfs_mksubvol(&file->f_path, name, namelen,
1357b83cc969SLi Zefan 				     NULL, transid, readonly);
1358cb8e7090SChristoph Hellwig 	} else {
13593de4586cSChris Mason 		struct inode *src_inode;
136072fd032eSSage Weil 		src_file = fget(fd);
13613de4586cSChris Mason 		if (!src_file) {
13623de4586cSChris Mason 			ret = -EINVAL;
13633de4586cSChris Mason 			goto out;
13643de4586cSChris Mason 		}
13653de4586cSChris Mason 
13663de4586cSChris Mason 		src_inode = src_file->f_path.dentry->d_inode;
13673de4586cSChris Mason 		if (src_inode->i_sb != file->f_path.dentry->d_inode->i_sb) {
1368d397712bSChris Mason 			printk(KERN_INFO "btrfs: Snapshot src from "
1369d397712bSChris Mason 			       "another FS\n");
13703de4586cSChris Mason 			ret = -EINVAL;
13713de4586cSChris Mason 			fput(src_file);
13723de4586cSChris Mason 			goto out;
13733de4586cSChris Mason 		}
137472fd032eSSage Weil 		ret = btrfs_mksubvol(&file->f_path, name, namelen,
137572fd032eSSage Weil 				     BTRFS_I(src_inode)->root,
1376b83cc969SLi Zefan 				     transid, readonly);
13773de4586cSChris Mason 		fput(src_file);
1378cb8e7090SChristoph Hellwig 	}
1379f46b5a66SChristoph Hellwig out:
138072fd032eSSage Weil 	return ret;
138172fd032eSSage Weil }
138272fd032eSSage Weil 
138372fd032eSSage Weil static noinline int btrfs_ioctl_snap_create(struct file *file,
1384fa0d2b9bSLi Zefan 					    void __user *arg, int subvol)
138572fd032eSSage Weil {
1386fa0d2b9bSLi Zefan 	struct btrfs_ioctl_vol_args *vol_args;
138772fd032eSSage Weil 	int ret;
138872fd032eSSage Weil 
1389fa0d2b9bSLi Zefan 	vol_args = memdup_user(arg, sizeof(*vol_args));
1390fa0d2b9bSLi Zefan 	if (IS_ERR(vol_args))
1391fa0d2b9bSLi Zefan 		return PTR_ERR(vol_args);
1392fa0d2b9bSLi Zefan 	vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
1393fa0d2b9bSLi Zefan 
1394fa0d2b9bSLi Zefan 	ret = btrfs_ioctl_snap_create_transid(file, vol_args->name,
1395b83cc969SLi Zefan 					      vol_args->fd, subvol,
1396b83cc969SLi Zefan 					      NULL, false);
1397fa0d2b9bSLi Zefan 
1398fa0d2b9bSLi Zefan 	kfree(vol_args);
1399fa0d2b9bSLi Zefan 	return ret;
1400fa0d2b9bSLi Zefan }
1401fa0d2b9bSLi Zefan 
1402fa0d2b9bSLi Zefan static noinline int btrfs_ioctl_snap_create_v2(struct file *file,
1403fa0d2b9bSLi Zefan 					       void __user *arg, int subvol)
1404fa0d2b9bSLi Zefan {
1405fa0d2b9bSLi Zefan 	struct btrfs_ioctl_vol_args_v2 *vol_args;
1406fa0d2b9bSLi Zefan 	int ret;
1407fdfb1e4fSLi Zefan 	u64 transid = 0;
1408fdfb1e4fSLi Zefan 	u64 *ptr = NULL;
1409b83cc969SLi Zefan 	bool readonly = false;
141072fd032eSSage Weil 
1411fa0d2b9bSLi Zefan 	vol_args = memdup_user(arg, sizeof(*vol_args));
1412fa0d2b9bSLi Zefan 	if (IS_ERR(vol_args))
1413fa0d2b9bSLi Zefan 		return PTR_ERR(vol_args);
1414fa0d2b9bSLi Zefan 	vol_args->name[BTRFS_SUBVOL_NAME_MAX] = '\0';
1415fdfb1e4fSLi Zefan 
1416b83cc969SLi Zefan 	if (vol_args->flags &
1417b83cc969SLi Zefan 	    ~(BTRFS_SUBVOL_CREATE_ASYNC | BTRFS_SUBVOL_RDONLY)) {
1418b83cc969SLi Zefan 		ret = -EOPNOTSUPP;
1419fdfb1e4fSLi Zefan 		goto out;
1420fdfb1e4fSLi Zefan 	}
1421fdfb1e4fSLi Zefan 
1422fa0d2b9bSLi Zefan 	if (vol_args->flags & BTRFS_SUBVOL_CREATE_ASYNC)
1423fdfb1e4fSLi Zefan 		ptr = &transid;
1424b83cc969SLi Zefan 	if (vol_args->flags & BTRFS_SUBVOL_RDONLY)
1425b83cc969SLi Zefan 		readonly = true;
142675eaa0e2SSage Weil 
1427fa0d2b9bSLi Zefan 	ret = btrfs_ioctl_snap_create_transid(file, vol_args->name,
1428b83cc969SLi Zefan 					      vol_args->fd, subvol,
1429b83cc969SLi Zefan 					      ptr, readonly);
143075eaa0e2SSage Weil 
1431fdfb1e4fSLi Zefan 	if (ret == 0 && ptr &&
143275eaa0e2SSage Weil 	    copy_to_user(arg +
1433fdfb1e4fSLi Zefan 			 offsetof(struct btrfs_ioctl_vol_args_v2,
1434fdfb1e4fSLi Zefan 				  transid), ptr, sizeof(*ptr)))
143575eaa0e2SSage Weil 		ret = -EFAULT;
1436fdfb1e4fSLi Zefan out:
1437f46b5a66SChristoph Hellwig 	kfree(vol_args);
1438f46b5a66SChristoph Hellwig 	return ret;
1439f46b5a66SChristoph Hellwig }
1440f46b5a66SChristoph Hellwig 
14410caa102dSLi Zefan static noinline int btrfs_ioctl_subvol_getflags(struct file *file,
14420caa102dSLi Zefan 						void __user *arg)
14430caa102dSLi Zefan {
14440caa102dSLi Zefan 	struct inode *inode = fdentry(file)->d_inode;
14450caa102dSLi Zefan 	struct btrfs_root *root = BTRFS_I(inode)->root;
14460caa102dSLi Zefan 	int ret = 0;
14470caa102dSLi Zefan 	u64 flags = 0;
14480caa102dSLi Zefan 
144933345d01SLi Zefan 	if (btrfs_ino(inode) != BTRFS_FIRST_FREE_OBJECTID)
14500caa102dSLi Zefan 		return -EINVAL;
14510caa102dSLi Zefan 
14520caa102dSLi Zefan 	down_read(&root->fs_info->subvol_sem);
14530caa102dSLi Zefan 	if (btrfs_root_readonly(root))
14540caa102dSLi Zefan 		flags |= BTRFS_SUBVOL_RDONLY;
14550caa102dSLi Zefan 	up_read(&root->fs_info->subvol_sem);
14560caa102dSLi Zefan 
14570caa102dSLi Zefan 	if (copy_to_user(arg, &flags, sizeof(flags)))
14580caa102dSLi Zefan 		ret = -EFAULT;
14590caa102dSLi Zefan 
14600caa102dSLi Zefan 	return ret;
14610caa102dSLi Zefan }
14620caa102dSLi Zefan 
14630caa102dSLi Zefan static noinline int btrfs_ioctl_subvol_setflags(struct file *file,
14640caa102dSLi Zefan 					      void __user *arg)
14650caa102dSLi Zefan {
14660caa102dSLi Zefan 	struct inode *inode = fdentry(file)->d_inode;
14670caa102dSLi Zefan 	struct btrfs_root *root = BTRFS_I(inode)->root;
14680caa102dSLi Zefan 	struct btrfs_trans_handle *trans;
14690caa102dSLi Zefan 	u64 root_flags;
14700caa102dSLi Zefan 	u64 flags;
14710caa102dSLi Zefan 	int ret = 0;
14720caa102dSLi Zefan 
14730caa102dSLi Zefan 	if (root->fs_info->sb->s_flags & MS_RDONLY)
14740caa102dSLi Zefan 		return -EROFS;
14750caa102dSLi Zefan 
147633345d01SLi Zefan 	if (btrfs_ino(inode) != BTRFS_FIRST_FREE_OBJECTID)
14770caa102dSLi Zefan 		return -EINVAL;
14780caa102dSLi Zefan 
14790caa102dSLi Zefan 	if (copy_from_user(&flags, arg, sizeof(flags)))
14800caa102dSLi Zefan 		return -EFAULT;
14810caa102dSLi Zefan 
1482b4dc2b8cSLi Zefan 	if (flags & BTRFS_SUBVOL_CREATE_ASYNC)
14830caa102dSLi Zefan 		return -EINVAL;
14840caa102dSLi Zefan 
14850caa102dSLi Zefan 	if (flags & ~BTRFS_SUBVOL_RDONLY)
14860caa102dSLi Zefan 		return -EOPNOTSUPP;
14870caa102dSLi Zefan 
14882e149670SSerge E. Hallyn 	if (!inode_owner_or_capable(inode))
1489b4dc2b8cSLi Zefan 		return -EACCES;
1490b4dc2b8cSLi Zefan 
14910caa102dSLi Zefan 	down_write(&root->fs_info->subvol_sem);
14920caa102dSLi Zefan 
14930caa102dSLi Zefan 	/* nothing to do */
14940caa102dSLi Zefan 	if (!!(flags & BTRFS_SUBVOL_RDONLY) == btrfs_root_readonly(root))
14950caa102dSLi Zefan 		goto out;
14960caa102dSLi Zefan 
14970caa102dSLi Zefan 	root_flags = btrfs_root_flags(&root->root_item);
14980caa102dSLi Zefan 	if (flags & BTRFS_SUBVOL_RDONLY)
14990caa102dSLi Zefan 		btrfs_set_root_flags(&root->root_item,
15000caa102dSLi Zefan 				     root_flags | BTRFS_ROOT_SUBVOL_RDONLY);
15010caa102dSLi Zefan 	else
15020caa102dSLi Zefan 		btrfs_set_root_flags(&root->root_item,
15030caa102dSLi Zefan 				     root_flags & ~BTRFS_ROOT_SUBVOL_RDONLY);
15040caa102dSLi Zefan 
15050caa102dSLi Zefan 	trans = btrfs_start_transaction(root, 1);
15060caa102dSLi Zefan 	if (IS_ERR(trans)) {
15070caa102dSLi Zefan 		ret = PTR_ERR(trans);
15080caa102dSLi Zefan 		goto out_reset;
15090caa102dSLi Zefan 	}
15100caa102dSLi Zefan 
1511b4dc2b8cSLi Zefan 	ret = btrfs_update_root(trans, root->fs_info->tree_root,
15120caa102dSLi Zefan 				&root->root_key, &root->root_item);
15130caa102dSLi Zefan 
15140caa102dSLi Zefan 	btrfs_commit_transaction(trans, root);
15150caa102dSLi Zefan out_reset:
15160caa102dSLi Zefan 	if (ret)
15170caa102dSLi Zefan 		btrfs_set_root_flags(&root->root_item, root_flags);
15180caa102dSLi Zefan out:
15190caa102dSLi Zefan 	up_write(&root->fs_info->subvol_sem);
15200caa102dSLi Zefan 	return ret;
15210caa102dSLi Zefan }
15220caa102dSLi Zefan 
152376dda93cSYan, Zheng /*
152476dda93cSYan, Zheng  * helper to check if the subvolume references other subvolumes
152576dda93cSYan, Zheng  */
152676dda93cSYan, Zheng static noinline int may_destroy_subvol(struct btrfs_root *root)
152776dda93cSYan, Zheng {
152876dda93cSYan, Zheng 	struct btrfs_path *path;
152976dda93cSYan, Zheng 	struct btrfs_key key;
153076dda93cSYan, Zheng 	int ret;
153176dda93cSYan, Zheng 
153276dda93cSYan, Zheng 	path = btrfs_alloc_path();
153376dda93cSYan, Zheng 	if (!path)
153476dda93cSYan, Zheng 		return -ENOMEM;
153576dda93cSYan, Zheng 
153676dda93cSYan, Zheng 	key.objectid = root->root_key.objectid;
153776dda93cSYan, Zheng 	key.type = BTRFS_ROOT_REF_KEY;
153876dda93cSYan, Zheng 	key.offset = (u64)-1;
153976dda93cSYan, Zheng 
154076dda93cSYan, Zheng 	ret = btrfs_search_slot(NULL, root->fs_info->tree_root,
154176dda93cSYan, Zheng 				&key, path, 0, 0);
154276dda93cSYan, Zheng 	if (ret < 0)
154376dda93cSYan, Zheng 		goto out;
154476dda93cSYan, Zheng 	BUG_ON(ret == 0);
154576dda93cSYan, Zheng 
154676dda93cSYan, Zheng 	ret = 0;
154776dda93cSYan, Zheng 	if (path->slots[0] > 0) {
154876dda93cSYan, Zheng 		path->slots[0]--;
154976dda93cSYan, Zheng 		btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
155076dda93cSYan, Zheng 		if (key.objectid == root->root_key.objectid &&
155176dda93cSYan, Zheng 		    key.type == BTRFS_ROOT_REF_KEY)
155276dda93cSYan, Zheng 			ret = -ENOTEMPTY;
155376dda93cSYan, Zheng 	}
155476dda93cSYan, Zheng out:
155576dda93cSYan, Zheng 	btrfs_free_path(path);
155676dda93cSYan, Zheng 	return ret;
155776dda93cSYan, Zheng }
155876dda93cSYan, Zheng 
1559ac8e9819SChris Mason static noinline int key_in_sk(struct btrfs_key *key,
1560ac8e9819SChris Mason 			      struct btrfs_ioctl_search_key *sk)
1561ac8e9819SChris Mason {
1562abc6e134SChris Mason 	struct btrfs_key test;
1563abc6e134SChris Mason 	int ret;
1564abc6e134SChris Mason 
1565abc6e134SChris Mason 	test.objectid = sk->min_objectid;
1566abc6e134SChris Mason 	test.type = sk->min_type;
1567abc6e134SChris Mason 	test.offset = sk->min_offset;
1568abc6e134SChris Mason 
1569abc6e134SChris Mason 	ret = btrfs_comp_cpu_keys(key, &test);
1570abc6e134SChris Mason 	if (ret < 0)
1571ac8e9819SChris Mason 		return 0;
1572abc6e134SChris Mason 
1573abc6e134SChris Mason 	test.objectid = sk->max_objectid;
1574abc6e134SChris Mason 	test.type = sk->max_type;
1575abc6e134SChris Mason 	test.offset = sk->max_offset;
1576abc6e134SChris Mason 
1577abc6e134SChris Mason 	ret = btrfs_comp_cpu_keys(key, &test);
1578abc6e134SChris Mason 	if (ret > 0)
1579ac8e9819SChris Mason 		return 0;
1580ac8e9819SChris Mason 	return 1;
1581ac8e9819SChris Mason }
1582ac8e9819SChris Mason 
1583ac8e9819SChris Mason static noinline int copy_to_sk(struct btrfs_root *root,
1584ac8e9819SChris Mason 			       struct btrfs_path *path,
1585ac8e9819SChris Mason 			       struct btrfs_key *key,
1586ac8e9819SChris Mason 			       struct btrfs_ioctl_search_key *sk,
1587ac8e9819SChris Mason 			       char *buf,
1588ac8e9819SChris Mason 			       unsigned long *sk_offset,
1589ac8e9819SChris Mason 			       int *num_found)
1590ac8e9819SChris Mason {
1591ac8e9819SChris Mason 	u64 found_transid;
1592ac8e9819SChris Mason 	struct extent_buffer *leaf;
1593ac8e9819SChris Mason 	struct btrfs_ioctl_search_header sh;
1594ac8e9819SChris Mason 	unsigned long item_off;
1595ac8e9819SChris Mason 	unsigned long item_len;
1596ac8e9819SChris Mason 	int nritems;
1597ac8e9819SChris Mason 	int i;
1598ac8e9819SChris Mason 	int slot;
1599ac8e9819SChris Mason 	int ret = 0;
1600ac8e9819SChris Mason 
1601ac8e9819SChris Mason 	leaf = path->nodes[0];
1602ac8e9819SChris Mason 	slot = path->slots[0];
1603ac8e9819SChris Mason 	nritems = btrfs_header_nritems(leaf);
1604ac8e9819SChris Mason 
1605ac8e9819SChris Mason 	if (btrfs_header_generation(leaf) > sk->max_transid) {
1606ac8e9819SChris Mason 		i = nritems;
1607ac8e9819SChris Mason 		goto advance_key;
1608ac8e9819SChris Mason 	}
1609ac8e9819SChris Mason 	found_transid = btrfs_header_generation(leaf);
1610ac8e9819SChris Mason 
1611ac8e9819SChris Mason 	for (i = slot; i < nritems; i++) {
1612ac8e9819SChris Mason 		item_off = btrfs_item_ptr_offset(leaf, i);
1613ac8e9819SChris Mason 		item_len = btrfs_item_size_nr(leaf, i);
1614ac8e9819SChris Mason 
1615ac8e9819SChris Mason 		if (item_len > BTRFS_SEARCH_ARGS_BUFSIZE)
1616ac8e9819SChris Mason 			item_len = 0;
1617ac8e9819SChris Mason 
1618ac8e9819SChris Mason 		if (sizeof(sh) + item_len + *sk_offset >
1619ac8e9819SChris Mason 		    BTRFS_SEARCH_ARGS_BUFSIZE) {
1620ac8e9819SChris Mason 			ret = 1;
1621ac8e9819SChris Mason 			goto overflow;
1622ac8e9819SChris Mason 		}
1623ac8e9819SChris Mason 
1624ac8e9819SChris Mason 		btrfs_item_key_to_cpu(leaf, key, i);
1625ac8e9819SChris Mason 		if (!key_in_sk(key, sk))
1626ac8e9819SChris Mason 			continue;
1627ac8e9819SChris Mason 
1628ac8e9819SChris Mason 		sh.objectid = key->objectid;
1629ac8e9819SChris Mason 		sh.offset = key->offset;
1630ac8e9819SChris Mason 		sh.type = key->type;
1631ac8e9819SChris Mason 		sh.len = item_len;
1632ac8e9819SChris Mason 		sh.transid = found_transid;
1633ac8e9819SChris Mason 
1634ac8e9819SChris Mason 		/* copy search result header */
1635ac8e9819SChris Mason 		memcpy(buf + *sk_offset, &sh, sizeof(sh));
1636ac8e9819SChris Mason 		*sk_offset += sizeof(sh);
1637ac8e9819SChris Mason 
1638ac8e9819SChris Mason 		if (item_len) {
1639ac8e9819SChris Mason 			char *p = buf + *sk_offset;
1640ac8e9819SChris Mason 			/* copy the item */
1641ac8e9819SChris Mason 			read_extent_buffer(leaf, p,
1642ac8e9819SChris Mason 					   item_off, item_len);
1643ac8e9819SChris Mason 			*sk_offset += item_len;
1644ac8e9819SChris Mason 		}
1645e2156867SHugo Mills 		(*num_found)++;
1646ac8e9819SChris Mason 
1647ac8e9819SChris Mason 		if (*num_found >= sk->nr_items)
1648ac8e9819SChris Mason 			break;
1649ac8e9819SChris Mason 	}
1650ac8e9819SChris Mason advance_key:
1651ac8e9819SChris Mason 	ret = 0;
1652abc6e134SChris Mason 	if (key->offset < (u64)-1 && key->offset < sk->max_offset)
1653abc6e134SChris Mason 		key->offset++;
1654abc6e134SChris Mason 	else if (key->type < (u8)-1 && key->type < sk->max_type) {
1655abc6e134SChris Mason 		key->offset = 0;
1656abc6e134SChris Mason 		key->type++;
1657abc6e134SChris Mason 	} else if (key->objectid < (u64)-1 && key->objectid < sk->max_objectid) {
1658abc6e134SChris Mason 		key->offset = 0;
1659abc6e134SChris Mason 		key->type = 0;
1660abc6e134SChris Mason 		key->objectid++;
1661abc6e134SChris Mason 	} else
1662abc6e134SChris Mason 		ret = 1;
1663ac8e9819SChris Mason overflow:
1664ac8e9819SChris Mason 	return ret;
1665ac8e9819SChris Mason }
1666ac8e9819SChris Mason 
1667ac8e9819SChris Mason static noinline int search_ioctl(struct inode *inode,
1668ac8e9819SChris Mason 				 struct btrfs_ioctl_search_args *args)
1669ac8e9819SChris Mason {
1670ac8e9819SChris Mason 	struct btrfs_root *root;
1671ac8e9819SChris Mason 	struct btrfs_key key;
1672ac8e9819SChris Mason 	struct btrfs_key max_key;
1673ac8e9819SChris Mason 	struct btrfs_path *path;
1674ac8e9819SChris Mason 	struct btrfs_ioctl_search_key *sk = &args->key;
1675ac8e9819SChris Mason 	struct btrfs_fs_info *info = BTRFS_I(inode)->root->fs_info;
1676ac8e9819SChris Mason 	int ret;
1677ac8e9819SChris Mason 	int num_found = 0;
1678ac8e9819SChris Mason 	unsigned long sk_offset = 0;
1679ac8e9819SChris Mason 
1680ac8e9819SChris Mason 	path = btrfs_alloc_path();
1681ac8e9819SChris Mason 	if (!path)
1682ac8e9819SChris Mason 		return -ENOMEM;
1683ac8e9819SChris Mason 
1684ac8e9819SChris Mason 	if (sk->tree_id == 0) {
1685ac8e9819SChris Mason 		/* search the root of the inode that was passed */
1686ac8e9819SChris Mason 		root = BTRFS_I(inode)->root;
1687ac8e9819SChris Mason 	} else {
1688ac8e9819SChris Mason 		key.objectid = sk->tree_id;
1689ac8e9819SChris Mason 		key.type = BTRFS_ROOT_ITEM_KEY;
1690ac8e9819SChris Mason 		key.offset = (u64)-1;
1691ac8e9819SChris Mason 		root = btrfs_read_fs_root_no_name(info, &key);
1692ac8e9819SChris Mason 		if (IS_ERR(root)) {
1693ac8e9819SChris Mason 			printk(KERN_ERR "could not find root %llu\n",
1694ac8e9819SChris Mason 			       sk->tree_id);
1695ac8e9819SChris Mason 			btrfs_free_path(path);
1696ac8e9819SChris Mason 			return -ENOENT;
1697ac8e9819SChris Mason 		}
1698ac8e9819SChris Mason 	}
1699ac8e9819SChris Mason 
1700ac8e9819SChris Mason 	key.objectid = sk->min_objectid;
1701ac8e9819SChris Mason 	key.type = sk->min_type;
1702ac8e9819SChris Mason 	key.offset = sk->min_offset;
1703ac8e9819SChris Mason 
1704ac8e9819SChris Mason 	max_key.objectid = sk->max_objectid;
1705ac8e9819SChris Mason 	max_key.type = sk->max_type;
1706ac8e9819SChris Mason 	max_key.offset = sk->max_offset;
1707ac8e9819SChris Mason 
1708ac8e9819SChris Mason 	path->keep_locks = 1;
1709ac8e9819SChris Mason 
1710ac8e9819SChris Mason 	while(1) {
1711ac8e9819SChris Mason 		ret = btrfs_search_forward(root, &key, &max_key, path, 0,
1712ac8e9819SChris Mason 					   sk->min_transid);
1713ac8e9819SChris Mason 		if (ret != 0) {
1714ac8e9819SChris Mason 			if (ret > 0)
1715ac8e9819SChris Mason 				ret = 0;
1716ac8e9819SChris Mason 			goto err;
1717ac8e9819SChris Mason 		}
1718ac8e9819SChris Mason 		ret = copy_to_sk(root, path, &key, sk, args->buf,
1719ac8e9819SChris Mason 				 &sk_offset, &num_found);
1720b3b4aa74SDavid Sterba 		btrfs_release_path(path);
1721ac8e9819SChris Mason 		if (ret || num_found >= sk->nr_items)
1722ac8e9819SChris Mason 			break;
1723ac8e9819SChris Mason 
1724ac8e9819SChris Mason 	}
1725ac8e9819SChris Mason 	ret = 0;
1726ac8e9819SChris Mason err:
1727ac8e9819SChris Mason 	sk->nr_items = num_found;
1728ac8e9819SChris Mason 	btrfs_free_path(path);
1729ac8e9819SChris Mason 	return ret;
1730ac8e9819SChris Mason }
1731ac8e9819SChris Mason 
1732ac8e9819SChris Mason static noinline int btrfs_ioctl_tree_search(struct file *file,
1733ac8e9819SChris Mason 					   void __user *argp)
1734ac8e9819SChris Mason {
1735ac8e9819SChris Mason 	 struct btrfs_ioctl_search_args *args;
1736ac8e9819SChris Mason 	 struct inode *inode;
1737ac8e9819SChris Mason 	 int ret;
1738ac8e9819SChris Mason 
1739ac8e9819SChris Mason 	if (!capable(CAP_SYS_ADMIN))
1740ac8e9819SChris Mason 		return -EPERM;
1741ac8e9819SChris Mason 
17422354d08fSJulia Lawall 	args = memdup_user(argp, sizeof(*args));
17432354d08fSJulia Lawall 	if (IS_ERR(args))
17442354d08fSJulia Lawall 		return PTR_ERR(args);
1745ac8e9819SChris Mason 
1746ac8e9819SChris Mason 	inode = fdentry(file)->d_inode;
1747ac8e9819SChris Mason 	ret = search_ioctl(inode, args);
1748ac8e9819SChris Mason 	if (ret == 0 && copy_to_user(argp, args, sizeof(*args)))
1749ac8e9819SChris Mason 		ret = -EFAULT;
1750ac8e9819SChris Mason 	kfree(args);
1751ac8e9819SChris Mason 	return ret;
1752ac8e9819SChris Mason }
1753ac8e9819SChris Mason 
175498d377a0STARUISI Hiroaki /*
1755ac8e9819SChris Mason  * Search INODE_REFs to identify path name of 'dirid' directory
1756ac8e9819SChris Mason  * in a 'tree_id' tree. and sets path name to 'name'.
175798d377a0STARUISI Hiroaki  */
175898d377a0STARUISI Hiroaki static noinline int btrfs_search_path_in_tree(struct btrfs_fs_info *info,
175998d377a0STARUISI Hiroaki 				u64 tree_id, u64 dirid, char *name)
176098d377a0STARUISI Hiroaki {
176198d377a0STARUISI Hiroaki 	struct btrfs_root *root;
176298d377a0STARUISI Hiroaki 	struct btrfs_key key;
1763ac8e9819SChris Mason 	char *ptr;
176498d377a0STARUISI Hiroaki 	int ret = -1;
176598d377a0STARUISI Hiroaki 	int slot;
176698d377a0STARUISI Hiroaki 	int len;
176798d377a0STARUISI Hiroaki 	int total_len = 0;
176898d377a0STARUISI Hiroaki 	struct btrfs_inode_ref *iref;
176998d377a0STARUISI Hiroaki 	struct extent_buffer *l;
177098d377a0STARUISI Hiroaki 	struct btrfs_path *path;
177198d377a0STARUISI Hiroaki 
177298d377a0STARUISI Hiroaki 	if (dirid == BTRFS_FIRST_FREE_OBJECTID) {
177398d377a0STARUISI Hiroaki 		name[0]='\0';
177498d377a0STARUISI Hiroaki 		return 0;
177598d377a0STARUISI Hiroaki 	}
177698d377a0STARUISI Hiroaki 
177798d377a0STARUISI Hiroaki 	path = btrfs_alloc_path();
177898d377a0STARUISI Hiroaki 	if (!path)
177998d377a0STARUISI Hiroaki 		return -ENOMEM;
178098d377a0STARUISI Hiroaki 
1781ac8e9819SChris Mason 	ptr = &name[BTRFS_INO_LOOKUP_PATH_MAX];
178298d377a0STARUISI Hiroaki 
178398d377a0STARUISI Hiroaki 	key.objectid = tree_id;
178498d377a0STARUISI Hiroaki 	key.type = BTRFS_ROOT_ITEM_KEY;
178598d377a0STARUISI Hiroaki 	key.offset = (u64)-1;
178698d377a0STARUISI Hiroaki 	root = btrfs_read_fs_root_no_name(info, &key);
178798d377a0STARUISI Hiroaki 	if (IS_ERR(root)) {
178898d377a0STARUISI Hiroaki 		printk(KERN_ERR "could not find root %llu\n", tree_id);
17898ad6fcabSChris Mason 		ret = -ENOENT;
17908ad6fcabSChris Mason 		goto out;
179198d377a0STARUISI Hiroaki 	}
179298d377a0STARUISI Hiroaki 
179398d377a0STARUISI Hiroaki 	key.objectid = dirid;
179498d377a0STARUISI Hiroaki 	key.type = BTRFS_INODE_REF_KEY;
17958ad6fcabSChris Mason 	key.offset = (u64)-1;
179698d377a0STARUISI Hiroaki 
179798d377a0STARUISI Hiroaki 	while(1) {
179898d377a0STARUISI Hiroaki 		ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
179998d377a0STARUISI Hiroaki 		if (ret < 0)
180098d377a0STARUISI Hiroaki 			goto out;
180198d377a0STARUISI Hiroaki 
180298d377a0STARUISI Hiroaki 		l = path->nodes[0];
180398d377a0STARUISI Hiroaki 		slot = path->slots[0];
18048ad6fcabSChris Mason 		if (ret > 0 && slot > 0)
18058ad6fcabSChris Mason 			slot--;
180698d377a0STARUISI Hiroaki 		btrfs_item_key_to_cpu(l, &key, slot);
180798d377a0STARUISI Hiroaki 
180898d377a0STARUISI Hiroaki 		if (ret > 0 && (key.objectid != dirid ||
1809ac8e9819SChris Mason 				key.type != BTRFS_INODE_REF_KEY)) {
1810ac8e9819SChris Mason 			ret = -ENOENT;
181198d377a0STARUISI Hiroaki 			goto out;
1812ac8e9819SChris Mason 		}
181398d377a0STARUISI Hiroaki 
181498d377a0STARUISI Hiroaki 		iref = btrfs_item_ptr(l, slot, struct btrfs_inode_ref);
181598d377a0STARUISI Hiroaki 		len = btrfs_inode_ref_name_len(l, iref);
181698d377a0STARUISI Hiroaki 		ptr -= len + 1;
181798d377a0STARUISI Hiroaki 		total_len += len + 1;
1818ac8e9819SChris Mason 		if (ptr < name)
181998d377a0STARUISI Hiroaki 			goto out;
182098d377a0STARUISI Hiroaki 
182198d377a0STARUISI Hiroaki 		*(ptr + len) = '/';
182298d377a0STARUISI Hiroaki 		read_extent_buffer(l, ptr,(unsigned long)(iref + 1), len);
182398d377a0STARUISI Hiroaki 
182498d377a0STARUISI Hiroaki 		if (key.offset == BTRFS_FIRST_FREE_OBJECTID)
182598d377a0STARUISI Hiroaki 			break;
182698d377a0STARUISI Hiroaki 
1827b3b4aa74SDavid Sterba 		btrfs_release_path(path);
182898d377a0STARUISI Hiroaki 		key.objectid = key.offset;
18298ad6fcabSChris Mason 		key.offset = (u64)-1;
183098d377a0STARUISI Hiroaki 		dirid = key.objectid;
183198d377a0STARUISI Hiroaki 	}
1832ac8e9819SChris Mason 	if (ptr < name)
183398d377a0STARUISI Hiroaki 		goto out;
183477906a50SLi Zefan 	memmove(name, ptr, total_len);
183598d377a0STARUISI Hiroaki 	name[total_len]='\0';
183698d377a0STARUISI Hiroaki 	ret = 0;
183798d377a0STARUISI Hiroaki out:
183898d377a0STARUISI Hiroaki 	btrfs_free_path(path);
1839ac8e9819SChris Mason 	return ret;
1840ac8e9819SChris Mason }
1841ac8e9819SChris Mason 
1842ac8e9819SChris Mason static noinline int btrfs_ioctl_ino_lookup(struct file *file,
1843ac8e9819SChris Mason 					   void __user *argp)
1844ac8e9819SChris Mason {
1845ac8e9819SChris Mason 	 struct btrfs_ioctl_ino_lookup_args *args;
1846ac8e9819SChris Mason 	 struct inode *inode;
1847ac8e9819SChris Mason 	 int ret;
1848ac8e9819SChris Mason 
1849ac8e9819SChris Mason 	if (!capable(CAP_SYS_ADMIN))
1850ac8e9819SChris Mason 		return -EPERM;
1851ac8e9819SChris Mason 
18522354d08fSJulia Lawall 	args = memdup_user(argp, sizeof(*args));
18532354d08fSJulia Lawall 	if (IS_ERR(args))
18542354d08fSJulia Lawall 		return PTR_ERR(args);
1855c2b96929SDan Carpenter 
1856ac8e9819SChris Mason 	inode = fdentry(file)->d_inode;
1857ac8e9819SChris Mason 
18581b53ac4dSChris Mason 	if (args->treeid == 0)
18591b53ac4dSChris Mason 		args->treeid = BTRFS_I(inode)->root->root_key.objectid;
18601b53ac4dSChris Mason 
1861ac8e9819SChris Mason 	ret = btrfs_search_path_in_tree(BTRFS_I(inode)->root->fs_info,
1862ac8e9819SChris Mason 					args->treeid, args->objectid,
1863ac8e9819SChris Mason 					args->name);
1864ac8e9819SChris Mason 
1865ac8e9819SChris Mason 	if (ret == 0 && copy_to_user(argp, args, sizeof(*args)))
1866ac8e9819SChris Mason 		ret = -EFAULT;
1867ac8e9819SChris Mason 
1868ac8e9819SChris Mason 	kfree(args);
186998d377a0STARUISI Hiroaki 	return ret;
187098d377a0STARUISI Hiroaki }
187198d377a0STARUISI Hiroaki 
187276dda93cSYan, Zheng static noinline int btrfs_ioctl_snap_destroy(struct file *file,
187376dda93cSYan, Zheng 					     void __user *arg)
187476dda93cSYan, Zheng {
187576dda93cSYan, Zheng 	struct dentry *parent = fdentry(file);
187676dda93cSYan, Zheng 	struct dentry *dentry;
187776dda93cSYan, Zheng 	struct inode *dir = parent->d_inode;
187876dda93cSYan, Zheng 	struct inode *inode;
187976dda93cSYan, Zheng 	struct btrfs_root *root = BTRFS_I(dir)->root;
188076dda93cSYan, Zheng 	struct btrfs_root *dest = NULL;
188176dda93cSYan, Zheng 	struct btrfs_ioctl_vol_args *vol_args;
188276dda93cSYan, Zheng 	struct btrfs_trans_handle *trans;
188376dda93cSYan, Zheng 	int namelen;
188476dda93cSYan, Zheng 	int ret;
188576dda93cSYan, Zheng 	int err = 0;
188676dda93cSYan, Zheng 
188776dda93cSYan, Zheng 	vol_args = memdup_user(arg, sizeof(*vol_args));
188876dda93cSYan, Zheng 	if (IS_ERR(vol_args))
188976dda93cSYan, Zheng 		return PTR_ERR(vol_args);
189076dda93cSYan, Zheng 
189176dda93cSYan, Zheng 	vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
189276dda93cSYan, Zheng 	namelen = strlen(vol_args->name);
189376dda93cSYan, Zheng 	if (strchr(vol_args->name, '/') ||
189476dda93cSYan, Zheng 	    strncmp(vol_args->name, "..", namelen) == 0) {
189576dda93cSYan, Zheng 		err = -EINVAL;
189676dda93cSYan, Zheng 		goto out;
189776dda93cSYan, Zheng 	}
189876dda93cSYan, Zheng 
1899a561be71SAl Viro 	err = mnt_want_write_file(file);
190076dda93cSYan, Zheng 	if (err)
190176dda93cSYan, Zheng 		goto out;
190276dda93cSYan, Zheng 
190376dda93cSYan, Zheng 	mutex_lock_nested(&dir->i_mutex, I_MUTEX_PARENT);
190476dda93cSYan, Zheng 	dentry = lookup_one_len(vol_args->name, parent, namelen);
190576dda93cSYan, Zheng 	if (IS_ERR(dentry)) {
190676dda93cSYan, Zheng 		err = PTR_ERR(dentry);
190776dda93cSYan, Zheng 		goto out_unlock_dir;
190876dda93cSYan, Zheng 	}
190976dda93cSYan, Zheng 
191076dda93cSYan, Zheng 	if (!dentry->d_inode) {
191176dda93cSYan, Zheng 		err = -ENOENT;
191276dda93cSYan, Zheng 		goto out_dput;
191376dda93cSYan, Zheng 	}
191476dda93cSYan, Zheng 
191576dda93cSYan, Zheng 	inode = dentry->d_inode;
19164260f7c7SSage Weil 	dest = BTRFS_I(inode)->root;
19174260f7c7SSage Weil 	if (!capable(CAP_SYS_ADMIN)){
19184260f7c7SSage Weil 		/*
19194260f7c7SSage Weil 		 * Regular user.  Only allow this with a special mount
19204260f7c7SSage Weil 		 * option, when the user has write+exec access to the
19214260f7c7SSage Weil 		 * subvol root, and when rmdir(2) would have been
19224260f7c7SSage Weil 		 * allowed.
19234260f7c7SSage Weil 		 *
19244260f7c7SSage Weil 		 * Note that this is _not_ check that the subvol is
19254260f7c7SSage Weil 		 * empty or doesn't contain data that we wouldn't
19264260f7c7SSage Weil 		 * otherwise be able to delete.
19274260f7c7SSage Weil 		 *
19284260f7c7SSage Weil 		 * Users who want to delete empty subvols should try
19294260f7c7SSage Weil 		 * rmdir(2).
19304260f7c7SSage Weil 		 */
19314260f7c7SSage Weil 		err = -EPERM;
19324260f7c7SSage Weil 		if (!btrfs_test_opt(root, USER_SUBVOL_RM_ALLOWED))
19334260f7c7SSage Weil 			goto out_dput;
19344260f7c7SSage Weil 
19354260f7c7SSage Weil 		/*
19364260f7c7SSage Weil 		 * Do not allow deletion if the parent dir is the same
19374260f7c7SSage Weil 		 * as the dir to be deleted.  That means the ioctl
19384260f7c7SSage Weil 		 * must be called on the dentry referencing the root
19394260f7c7SSage Weil 		 * of the subvol, not a random directory contained
19404260f7c7SSage Weil 		 * within it.
19414260f7c7SSage Weil 		 */
19424260f7c7SSage Weil 		err = -EINVAL;
19434260f7c7SSage Weil 		if (root == dest)
19444260f7c7SSage Weil 			goto out_dput;
19454260f7c7SSage Weil 
19464260f7c7SSage Weil 		err = inode_permission(inode, MAY_WRITE | MAY_EXEC);
19474260f7c7SSage Weil 		if (err)
19484260f7c7SSage Weil 			goto out_dput;
19494260f7c7SSage Weil 
19504260f7c7SSage Weil 		/* check if subvolume may be deleted by a non-root user */
19514260f7c7SSage Weil 		err = btrfs_may_delete(dir, dentry, 1);
19524260f7c7SSage Weil 		if (err)
19534260f7c7SSage Weil 			goto out_dput;
19544260f7c7SSage Weil 	}
19554260f7c7SSage Weil 
195633345d01SLi Zefan 	if (btrfs_ino(inode) != BTRFS_FIRST_FREE_OBJECTID) {
195776dda93cSYan, Zheng 		err = -EINVAL;
195876dda93cSYan, Zheng 		goto out_dput;
195976dda93cSYan, Zheng 	}
196076dda93cSYan, Zheng 
196176dda93cSYan, Zheng 	mutex_lock(&inode->i_mutex);
196276dda93cSYan, Zheng 	err = d_invalidate(dentry);
196376dda93cSYan, Zheng 	if (err)
196476dda93cSYan, Zheng 		goto out_unlock;
196576dda93cSYan, Zheng 
196676dda93cSYan, Zheng 	down_write(&root->fs_info->subvol_sem);
196776dda93cSYan, Zheng 
196876dda93cSYan, Zheng 	err = may_destroy_subvol(dest);
196976dda93cSYan, Zheng 	if (err)
197076dda93cSYan, Zheng 		goto out_up_write;
197176dda93cSYan, Zheng 
1972a22285a6SYan, Zheng 	trans = btrfs_start_transaction(root, 0);
1973a22285a6SYan, Zheng 	if (IS_ERR(trans)) {
1974a22285a6SYan, Zheng 		err = PTR_ERR(trans);
1975d327099aSDan Carpenter 		goto out_up_write;
1976a22285a6SYan, Zheng 	}
1977a22285a6SYan, Zheng 	trans->block_rsv = &root->fs_info->global_block_rsv;
1978a22285a6SYan, Zheng 
197976dda93cSYan, Zheng 	ret = btrfs_unlink_subvol(trans, root, dir,
198076dda93cSYan, Zheng 				dest->root_key.objectid,
198176dda93cSYan, Zheng 				dentry->d_name.name,
198276dda93cSYan, Zheng 				dentry->d_name.len);
198379787eaaSJeff Mahoney 	if (ret) {
198479787eaaSJeff Mahoney 		err = ret;
198579787eaaSJeff Mahoney 		btrfs_abort_transaction(trans, root, ret);
198679787eaaSJeff Mahoney 		goto out_end_trans;
198779787eaaSJeff Mahoney 	}
198876dda93cSYan, Zheng 
198976dda93cSYan, Zheng 	btrfs_record_root_in_trans(trans, dest);
199076dda93cSYan, Zheng 
199176dda93cSYan, Zheng 	memset(&dest->root_item.drop_progress, 0,
199276dda93cSYan, Zheng 		sizeof(dest->root_item.drop_progress));
199376dda93cSYan, Zheng 	dest->root_item.drop_level = 0;
199476dda93cSYan, Zheng 	btrfs_set_root_refs(&dest->root_item, 0);
199576dda93cSYan, Zheng 
1996d68fc57bSYan, Zheng 	if (!xchg(&dest->orphan_item_inserted, 1)) {
199776dda93cSYan, Zheng 		ret = btrfs_insert_orphan_item(trans,
199876dda93cSYan, Zheng 					root->fs_info->tree_root,
199976dda93cSYan, Zheng 					dest->root_key.objectid);
200079787eaaSJeff Mahoney 		if (ret) {
200179787eaaSJeff Mahoney 			btrfs_abort_transaction(trans, root, ret);
200279787eaaSJeff Mahoney 			err = ret;
200379787eaaSJeff Mahoney 			goto out_end_trans;
2004d68fc57bSYan, Zheng 		}
200579787eaaSJeff Mahoney 	}
200679787eaaSJeff Mahoney out_end_trans:
2007531cb13fSSage Weil 	ret = btrfs_end_transaction(trans, root);
200879787eaaSJeff Mahoney 	if (ret && !err)
200979787eaaSJeff Mahoney 		err = ret;
201076dda93cSYan, Zheng 	inode->i_flags |= S_DEAD;
201176dda93cSYan, Zheng out_up_write:
201276dda93cSYan, Zheng 	up_write(&root->fs_info->subvol_sem);
201376dda93cSYan, Zheng out_unlock:
201476dda93cSYan, Zheng 	mutex_unlock(&inode->i_mutex);
201576dda93cSYan, Zheng 	if (!err) {
2016efefb143SYan, Zheng 		shrink_dcache_sb(root->fs_info->sb);
201776dda93cSYan, Zheng 		btrfs_invalidate_inodes(dest);
201876dda93cSYan, Zheng 		d_delete(dentry);
201976dda93cSYan, Zheng 	}
202076dda93cSYan, Zheng out_dput:
202176dda93cSYan, Zheng 	dput(dentry);
202276dda93cSYan, Zheng out_unlock_dir:
202376dda93cSYan, Zheng 	mutex_unlock(&dir->i_mutex);
20242a79f17eSAl Viro 	mnt_drop_write_file(file);
202576dda93cSYan, Zheng out:
202676dda93cSYan, Zheng 	kfree(vol_args);
202776dda93cSYan, Zheng 	return err;
202876dda93cSYan, Zheng }
202976dda93cSYan, Zheng 
20301e701a32SChris Mason static int btrfs_ioctl_defrag(struct file *file, void __user *argp)
2031f46b5a66SChristoph Hellwig {
2032f46b5a66SChristoph Hellwig 	struct inode *inode = fdentry(file)->d_inode;
2033f46b5a66SChristoph Hellwig 	struct btrfs_root *root = BTRFS_I(inode)->root;
20341e701a32SChris Mason 	struct btrfs_ioctl_defrag_range_args *range;
2035c146afadSYan Zheng 	int ret;
2036c146afadSYan Zheng 
2037b83cc969SLi Zefan 	if (btrfs_root_readonly(root))
2038b83cc969SLi Zefan 		return -EROFS;
2039b83cc969SLi Zefan 
2040a561be71SAl Viro 	ret = mnt_want_write_file(file);
2041c146afadSYan Zheng 	if (ret)
2042c146afadSYan Zheng 		return ret;
2043f46b5a66SChristoph Hellwig 
2044f46b5a66SChristoph Hellwig 	switch (inode->i_mode & S_IFMT) {
2045f46b5a66SChristoph Hellwig 	case S_IFDIR:
2046e441d54dSChris Mason 		if (!capable(CAP_SYS_ADMIN)) {
2047e441d54dSChris Mason 			ret = -EPERM;
2048e441d54dSChris Mason 			goto out;
2049e441d54dSChris Mason 		}
20508929ecfaSYan, Zheng 		ret = btrfs_defrag_root(root, 0);
20518929ecfaSYan, Zheng 		if (ret)
20528929ecfaSYan, Zheng 			goto out;
20538929ecfaSYan, Zheng 		ret = btrfs_defrag_root(root->fs_info->extent_root, 0);
2054f46b5a66SChristoph Hellwig 		break;
2055f46b5a66SChristoph Hellwig 	case S_IFREG:
2056e441d54dSChris Mason 		if (!(file->f_mode & FMODE_WRITE)) {
2057e441d54dSChris Mason 			ret = -EINVAL;
2058e441d54dSChris Mason 			goto out;
2059e441d54dSChris Mason 		}
20601e701a32SChris Mason 
20611e701a32SChris Mason 		range = kzalloc(sizeof(*range), GFP_KERNEL);
20621e701a32SChris Mason 		if (!range) {
20631e701a32SChris Mason 			ret = -ENOMEM;
20641e701a32SChris Mason 			goto out;
20651e701a32SChris Mason 		}
20661e701a32SChris Mason 
20671e701a32SChris Mason 		if (argp) {
20681e701a32SChris Mason 			if (copy_from_user(range, argp,
20691e701a32SChris Mason 					   sizeof(*range))) {
20701e701a32SChris Mason 				ret = -EFAULT;
20711e701a32SChris Mason 				kfree(range);
2072683be16eSDan Carpenter 				goto out;
20731e701a32SChris Mason 			}
20741e701a32SChris Mason 			/* compression requires us to start the IO */
20751e701a32SChris Mason 			if ((range->flags & BTRFS_DEFRAG_RANGE_COMPRESS)) {
20761e701a32SChris Mason 				range->flags |= BTRFS_DEFRAG_RANGE_START_IO;
20771e701a32SChris Mason 				range->extent_thresh = (u32)-1;
20781e701a32SChris Mason 			}
20791e701a32SChris Mason 		} else {
20801e701a32SChris Mason 			/* the rest are all set to zero by kzalloc */
20811e701a32SChris Mason 			range->len = (u64)-1;
20821e701a32SChris Mason 		}
20834cb5300bSChris Mason 		ret = btrfs_defrag_file(fdentry(file)->d_inode, file,
20844cb5300bSChris Mason 					range, 0, 0);
20854cb5300bSChris Mason 		if (ret > 0)
20864cb5300bSChris Mason 			ret = 0;
20871e701a32SChris Mason 		kfree(range);
2088f46b5a66SChristoph Hellwig 		break;
20898929ecfaSYan, Zheng 	default:
20908929ecfaSYan, Zheng 		ret = -EINVAL;
2091f46b5a66SChristoph Hellwig 	}
2092e441d54dSChris Mason out:
20932a79f17eSAl Viro 	mnt_drop_write_file(file);
2094e441d54dSChris Mason 	return ret;
2095f46b5a66SChristoph Hellwig }
2096f46b5a66SChristoph Hellwig 
2097b2950863SChristoph Hellwig static long btrfs_ioctl_add_dev(struct btrfs_root *root, void __user *arg)
2098f46b5a66SChristoph Hellwig {
2099f46b5a66SChristoph Hellwig 	struct btrfs_ioctl_vol_args *vol_args;
2100f46b5a66SChristoph Hellwig 	int ret;
2101f46b5a66SChristoph Hellwig 
2102e441d54dSChris Mason 	if (!capable(CAP_SYS_ADMIN))
2103e441d54dSChris Mason 		return -EPERM;
2104e441d54dSChris Mason 
2105c9e9f97bSIlya Dryomov 	mutex_lock(&root->fs_info->volume_mutex);
2106c9e9f97bSIlya Dryomov 	if (root->fs_info->balance_ctl) {
2107c9e9f97bSIlya Dryomov 		printk(KERN_INFO "btrfs: balance in progress\n");
2108c9e9f97bSIlya Dryomov 		ret = -EINVAL;
2109c9e9f97bSIlya Dryomov 		goto out;
2110c9e9f97bSIlya Dryomov 	}
2111c9e9f97bSIlya Dryomov 
2112dae7b665SLi Zefan 	vol_args = memdup_user(arg, sizeof(*vol_args));
2113c9e9f97bSIlya Dryomov 	if (IS_ERR(vol_args)) {
2114c9e9f97bSIlya Dryomov 		ret = PTR_ERR(vol_args);
2115c9e9f97bSIlya Dryomov 		goto out;
2116c9e9f97bSIlya Dryomov 	}
2117f46b5a66SChristoph Hellwig 
21185516e595SMark Fasheh 	vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
2119f46b5a66SChristoph Hellwig 	ret = btrfs_init_new_device(root, vol_args->name);
2120f46b5a66SChristoph Hellwig 
2121f46b5a66SChristoph Hellwig 	kfree(vol_args);
2122c9e9f97bSIlya Dryomov out:
2123c9e9f97bSIlya Dryomov 	mutex_unlock(&root->fs_info->volume_mutex);
2124f46b5a66SChristoph Hellwig 	return ret;
2125f46b5a66SChristoph Hellwig }
2126f46b5a66SChristoph Hellwig 
2127b2950863SChristoph Hellwig static long btrfs_ioctl_rm_dev(struct btrfs_root *root, void __user *arg)
2128f46b5a66SChristoph Hellwig {
2129f46b5a66SChristoph Hellwig 	struct btrfs_ioctl_vol_args *vol_args;
2130f46b5a66SChristoph Hellwig 	int ret;
2131f46b5a66SChristoph Hellwig 
2132e441d54dSChris Mason 	if (!capable(CAP_SYS_ADMIN))
2133e441d54dSChris Mason 		return -EPERM;
2134e441d54dSChris Mason 
2135c146afadSYan Zheng 	if (root->fs_info->sb->s_flags & MS_RDONLY)
2136c146afadSYan Zheng 		return -EROFS;
2137c146afadSYan Zheng 
2138c9e9f97bSIlya Dryomov 	mutex_lock(&root->fs_info->volume_mutex);
2139c9e9f97bSIlya Dryomov 	if (root->fs_info->balance_ctl) {
2140c9e9f97bSIlya Dryomov 		printk(KERN_INFO "btrfs: balance in progress\n");
2141c9e9f97bSIlya Dryomov 		ret = -EINVAL;
2142c9e9f97bSIlya Dryomov 		goto out;
2143c9e9f97bSIlya Dryomov 	}
2144c9e9f97bSIlya Dryomov 
2145dae7b665SLi Zefan 	vol_args = memdup_user(arg, sizeof(*vol_args));
2146c9e9f97bSIlya Dryomov 	if (IS_ERR(vol_args)) {
2147c9e9f97bSIlya Dryomov 		ret = PTR_ERR(vol_args);
2148c9e9f97bSIlya Dryomov 		goto out;
2149c9e9f97bSIlya Dryomov 	}
2150f46b5a66SChristoph Hellwig 
21515516e595SMark Fasheh 	vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
2152f46b5a66SChristoph Hellwig 	ret = btrfs_rm_device(root, vol_args->name);
2153f46b5a66SChristoph Hellwig 
2154f46b5a66SChristoph Hellwig 	kfree(vol_args);
2155c9e9f97bSIlya Dryomov out:
2156c9e9f97bSIlya Dryomov 	mutex_unlock(&root->fs_info->volume_mutex);
2157f46b5a66SChristoph Hellwig 	return ret;
2158f46b5a66SChristoph Hellwig }
2159f46b5a66SChristoph Hellwig 
2160475f6387SJan Schmidt static long btrfs_ioctl_fs_info(struct btrfs_root *root, void __user *arg)
2161475f6387SJan Schmidt {
2162027ed2f0SLi Zefan 	struct btrfs_ioctl_fs_info_args *fi_args;
2163475f6387SJan Schmidt 	struct btrfs_device *device;
2164475f6387SJan Schmidt 	struct btrfs_device *next;
2165475f6387SJan Schmidt 	struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices;
2166027ed2f0SLi Zefan 	int ret = 0;
2167475f6387SJan Schmidt 
2168475f6387SJan Schmidt 	if (!capable(CAP_SYS_ADMIN))
2169475f6387SJan Schmidt 		return -EPERM;
2170475f6387SJan Schmidt 
2171027ed2f0SLi Zefan 	fi_args = kzalloc(sizeof(*fi_args), GFP_KERNEL);
2172027ed2f0SLi Zefan 	if (!fi_args)
2173027ed2f0SLi Zefan 		return -ENOMEM;
2174027ed2f0SLi Zefan 
2175027ed2f0SLi Zefan 	fi_args->num_devices = fs_devices->num_devices;
2176027ed2f0SLi Zefan 	memcpy(&fi_args->fsid, root->fs_info->fsid, sizeof(fi_args->fsid));
2177475f6387SJan Schmidt 
2178475f6387SJan Schmidt 	mutex_lock(&fs_devices->device_list_mutex);
2179475f6387SJan Schmidt 	list_for_each_entry_safe(device, next, &fs_devices->devices, dev_list) {
2180027ed2f0SLi Zefan 		if (device->devid > fi_args->max_id)
2181027ed2f0SLi Zefan 			fi_args->max_id = device->devid;
2182475f6387SJan Schmidt 	}
2183475f6387SJan Schmidt 	mutex_unlock(&fs_devices->device_list_mutex);
2184475f6387SJan Schmidt 
2185027ed2f0SLi Zefan 	if (copy_to_user(arg, fi_args, sizeof(*fi_args)))
2186027ed2f0SLi Zefan 		ret = -EFAULT;
2187475f6387SJan Schmidt 
2188027ed2f0SLi Zefan 	kfree(fi_args);
2189027ed2f0SLi Zefan 	return ret;
2190475f6387SJan Schmidt }
2191475f6387SJan Schmidt 
2192475f6387SJan Schmidt static long btrfs_ioctl_dev_info(struct btrfs_root *root, void __user *arg)
2193475f6387SJan Schmidt {
2194475f6387SJan Schmidt 	struct btrfs_ioctl_dev_info_args *di_args;
2195475f6387SJan Schmidt 	struct btrfs_device *dev;
2196475f6387SJan Schmidt 	struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices;
2197475f6387SJan Schmidt 	int ret = 0;
2198475f6387SJan Schmidt 	char *s_uuid = NULL;
2199475f6387SJan Schmidt 	char empty_uuid[BTRFS_UUID_SIZE] = {0};
2200475f6387SJan Schmidt 
2201475f6387SJan Schmidt 	if (!capable(CAP_SYS_ADMIN))
2202475f6387SJan Schmidt 		return -EPERM;
2203475f6387SJan Schmidt 
2204475f6387SJan Schmidt 	di_args = memdup_user(arg, sizeof(*di_args));
2205475f6387SJan Schmidt 	if (IS_ERR(di_args))
2206475f6387SJan Schmidt 		return PTR_ERR(di_args);
2207475f6387SJan Schmidt 
2208475f6387SJan Schmidt 	if (memcmp(empty_uuid, di_args->uuid, BTRFS_UUID_SIZE) != 0)
2209475f6387SJan Schmidt 		s_uuid = di_args->uuid;
2210475f6387SJan Schmidt 
2211475f6387SJan Schmidt 	mutex_lock(&fs_devices->device_list_mutex);
2212475f6387SJan Schmidt 	dev = btrfs_find_device(root, di_args->devid, s_uuid, NULL);
2213475f6387SJan Schmidt 	mutex_unlock(&fs_devices->device_list_mutex);
2214475f6387SJan Schmidt 
2215475f6387SJan Schmidt 	if (!dev) {
2216475f6387SJan Schmidt 		ret = -ENODEV;
2217475f6387SJan Schmidt 		goto out;
2218475f6387SJan Schmidt 	}
2219475f6387SJan Schmidt 
2220475f6387SJan Schmidt 	di_args->devid = dev->devid;
2221475f6387SJan Schmidt 	di_args->bytes_used = dev->bytes_used;
2222475f6387SJan Schmidt 	di_args->total_bytes = dev->total_bytes;
2223475f6387SJan Schmidt 	memcpy(di_args->uuid, dev->uuid, sizeof(di_args->uuid));
2224475f6387SJan Schmidt 	strncpy(di_args->path, dev->name, sizeof(di_args->path));
2225475f6387SJan Schmidt 
2226475f6387SJan Schmidt out:
2227475f6387SJan Schmidt 	if (ret == 0 && copy_to_user(arg, di_args, sizeof(*di_args)))
2228475f6387SJan Schmidt 		ret = -EFAULT;
2229475f6387SJan Schmidt 
2230475f6387SJan Schmidt 	kfree(di_args);
2231475f6387SJan Schmidt 	return ret;
2232475f6387SJan Schmidt }
2233475f6387SJan Schmidt 
223476dda93cSYan, Zheng static noinline long btrfs_ioctl_clone(struct file *file, unsigned long srcfd,
2235b2950863SChristoph Hellwig 				       u64 off, u64 olen, u64 destoff)
2236f46b5a66SChristoph Hellwig {
2237f46b5a66SChristoph Hellwig 	struct inode *inode = fdentry(file)->d_inode;
2238f46b5a66SChristoph Hellwig 	struct btrfs_root *root = BTRFS_I(inode)->root;
2239f46b5a66SChristoph Hellwig 	struct file *src_file;
2240f46b5a66SChristoph Hellwig 	struct inode *src;
2241f46b5a66SChristoph Hellwig 	struct btrfs_trans_handle *trans;
2242f46b5a66SChristoph Hellwig 	struct btrfs_path *path;
2243f46b5a66SChristoph Hellwig 	struct extent_buffer *leaf;
2244ae01a0abSYan Zheng 	char *buf;
2245ae01a0abSYan Zheng 	struct btrfs_key key;
2246f46b5a66SChristoph Hellwig 	u32 nritems;
2247f46b5a66SChristoph Hellwig 	int slot;
2248ae01a0abSYan Zheng 	int ret;
2249c5c9cd4dSSage Weil 	u64 len = olen;
2250c5c9cd4dSSage Weil 	u64 bs = root->fs_info->sb->s_blocksize;
2251c5c9cd4dSSage Weil 	u64 hint_byte;
2252d20f7043SChris Mason 
2253c5c9cd4dSSage Weil 	/*
2254c5c9cd4dSSage Weil 	 * TODO:
2255c5c9cd4dSSage Weil 	 * - split compressed inline extents.  annoying: we need to
2256c5c9cd4dSSage Weil 	 *   decompress into destination's address_space (the file offset
2257c5c9cd4dSSage Weil 	 *   may change, so source mapping won't do), then recompress (or
2258c5c9cd4dSSage Weil 	 *   otherwise reinsert) a subrange.
2259c5c9cd4dSSage Weil 	 * - allow ranges within the same file to be cloned (provided
2260c5c9cd4dSSage Weil 	 *   they don't overlap)?
2261c5c9cd4dSSage Weil 	 */
2262c5c9cd4dSSage Weil 
2263e441d54dSChris Mason 	/* the destination must be opened for writing */
22642ebc3464SDan Rosenberg 	if (!(file->f_mode & FMODE_WRITE) || (file->f_flags & O_APPEND))
2265e441d54dSChris Mason 		return -EINVAL;
2266e441d54dSChris Mason 
2267b83cc969SLi Zefan 	if (btrfs_root_readonly(root))
2268b83cc969SLi Zefan 		return -EROFS;
2269b83cc969SLi Zefan 
2270a561be71SAl Viro 	ret = mnt_want_write_file(file);
2271c146afadSYan Zheng 	if (ret)
2272c146afadSYan Zheng 		return ret;
2273c146afadSYan Zheng 
2274c5c9cd4dSSage Weil 	src_file = fget(srcfd);
2275ab67b7c1SYan Zheng 	if (!src_file) {
2276ab67b7c1SYan Zheng 		ret = -EBADF;
2277ab67b7c1SYan Zheng 		goto out_drop_write;
2278ab67b7c1SYan Zheng 	}
22795dc64164SDan Rosenberg 
2280f46b5a66SChristoph Hellwig 	src = src_file->f_dentry->d_inode;
2281f46b5a66SChristoph Hellwig 
2282c5c9cd4dSSage Weil 	ret = -EINVAL;
2283c5c9cd4dSSage Weil 	if (src == inode)
2284c5c9cd4dSSage Weil 		goto out_fput;
2285c5c9cd4dSSage Weil 
22865dc64164SDan Rosenberg 	/* the src must be open for reading */
22875dc64164SDan Rosenberg 	if (!(src_file->f_mode & FMODE_READ))
22885dc64164SDan Rosenberg 		goto out_fput;
22895dc64164SDan Rosenberg 
22900e7b824cSLi Zefan 	/* don't make the dst file partly checksummed */
22910e7b824cSLi Zefan 	if ((BTRFS_I(src)->flags & BTRFS_INODE_NODATASUM) !=
22920e7b824cSLi Zefan 	    (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM))
22930e7b824cSLi Zefan 		goto out_fput;
22940e7b824cSLi Zefan 
2295ae01a0abSYan Zheng 	ret = -EISDIR;
2296ae01a0abSYan Zheng 	if (S_ISDIR(src->i_mode) || S_ISDIR(inode->i_mode))
2297f46b5a66SChristoph Hellwig 		goto out_fput;
2298f46b5a66SChristoph Hellwig 
2299ae01a0abSYan Zheng 	ret = -EXDEV;
2300ae01a0abSYan Zheng 	if (src->i_sb != inode->i_sb || BTRFS_I(src)->root != root)
2301ae01a0abSYan Zheng 		goto out_fput;
2302ae01a0abSYan Zheng 
2303ae01a0abSYan Zheng 	ret = -ENOMEM;
2304ae01a0abSYan Zheng 	buf = vmalloc(btrfs_level_size(root, 0));
2305ae01a0abSYan Zheng 	if (!buf)
2306ae01a0abSYan Zheng 		goto out_fput;
2307ae01a0abSYan Zheng 
2308ae01a0abSYan Zheng 	path = btrfs_alloc_path();
2309ae01a0abSYan Zheng 	if (!path) {
2310ae01a0abSYan Zheng 		vfree(buf);
2311ae01a0abSYan Zheng 		goto out_fput;
2312ae01a0abSYan Zheng 	}
2313ae01a0abSYan Zheng 	path->reada = 2;
2314ae01a0abSYan Zheng 
2315f46b5a66SChristoph Hellwig 	if (inode < src) {
2316fccdae43SSage Weil 		mutex_lock_nested(&inode->i_mutex, I_MUTEX_PARENT);
2317fccdae43SSage Weil 		mutex_lock_nested(&src->i_mutex, I_MUTEX_CHILD);
2318f46b5a66SChristoph Hellwig 	} else {
2319fccdae43SSage Weil 		mutex_lock_nested(&src->i_mutex, I_MUTEX_PARENT);
2320fccdae43SSage Weil 		mutex_lock_nested(&inode->i_mutex, I_MUTEX_CHILD);
2321f46b5a66SChristoph Hellwig 	}
2322f46b5a66SChristoph Hellwig 
2323c5c9cd4dSSage Weil 	/* determine range to clone */
2324c5c9cd4dSSage Weil 	ret = -EINVAL;
23252ebc3464SDan Rosenberg 	if (off + len > src->i_size || off + len < off)
2326f46b5a66SChristoph Hellwig 		goto out_unlock;
2327c5c9cd4dSSage Weil 	if (len == 0)
2328c5c9cd4dSSage Weil 		olen = len = src->i_size - off;
2329c5c9cd4dSSage Weil 	/* if we extend to eof, continue to block boundary */
2330c5c9cd4dSSage Weil 	if (off + len == src->i_size)
23312a6b8daeSLi Zefan 		len = ALIGN(src->i_size, bs) - off;
2332c5c9cd4dSSage Weil 
2333c5c9cd4dSSage Weil 	/* verify the end result is block aligned */
23342a6b8daeSLi Zefan 	if (!IS_ALIGNED(off, bs) || !IS_ALIGNED(off + len, bs) ||
23352a6b8daeSLi Zefan 	    !IS_ALIGNED(destoff, bs))
2336c5c9cd4dSSage Weil 		goto out_unlock;
2337c5c9cd4dSSage Weil 
2338d525e8abSLi Zefan 	if (destoff > inode->i_size) {
2339d525e8abSLi Zefan 		ret = btrfs_cont_expand(inode, inode->i_size, destoff);
2340d525e8abSLi Zefan 		if (ret)
2341d525e8abSLi Zefan 			goto out_unlock;
2342d525e8abSLi Zefan 	}
2343d525e8abSLi Zefan 
234471ef0786SLi Zefan 	/* truncate page cache pages from target inode range */
234571ef0786SLi Zefan 	truncate_inode_pages_range(&inode->i_data, destoff,
234671ef0786SLi Zefan 				   PAGE_CACHE_ALIGN(destoff + len) - 1);
234771ef0786SLi Zefan 
2348f46b5a66SChristoph Hellwig 	/* do any pending delalloc/csum calc on src, one way or
2349f46b5a66SChristoph Hellwig 	   another, and lock file content */
2350f46b5a66SChristoph Hellwig 	while (1) {
235131840ae1SZheng Yan 		struct btrfs_ordered_extent *ordered;
2352d0082371SJeff Mahoney 		lock_extent(&BTRFS_I(src)->io_tree, off, off+len);
23539a019196SSage Weil 		ordered = btrfs_lookup_first_ordered_extent(src, off+len);
23549a019196SSage Weil 		if (!ordered &&
23559a019196SSage Weil 		    !test_range_bit(&BTRFS_I(src)->io_tree, off, off+len,
23569a019196SSage Weil 				   EXTENT_DELALLOC, 0, NULL))
2357f46b5a66SChristoph Hellwig 			break;
2358d0082371SJeff Mahoney 		unlock_extent(&BTRFS_I(src)->io_tree, off, off+len);
2359ae01a0abSYan Zheng 		if (ordered)
2360ae01a0abSYan Zheng 			btrfs_put_ordered_extent(ordered);
23619a019196SSage Weil 		btrfs_wait_ordered_range(src, off, len);
2362f46b5a66SChristoph Hellwig 	}
2363f46b5a66SChristoph Hellwig 
2364c5c9cd4dSSage Weil 	/* clone data */
236533345d01SLi Zefan 	key.objectid = btrfs_ino(src);
2366ae01a0abSYan Zheng 	key.type = BTRFS_EXTENT_DATA_KEY;
2367ae01a0abSYan Zheng 	key.offset = 0;
2368f46b5a66SChristoph Hellwig 
2369f46b5a66SChristoph Hellwig 	while (1) {
2370f46b5a66SChristoph Hellwig 		/*
2371f46b5a66SChristoph Hellwig 		 * note the key will change type as we walk through the
2372f46b5a66SChristoph Hellwig 		 * tree.
2373f46b5a66SChristoph Hellwig 		 */
2374a22285a6SYan, Zheng 		ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2375f46b5a66SChristoph Hellwig 		if (ret < 0)
2376f46b5a66SChristoph Hellwig 			goto out;
2377f46b5a66SChristoph Hellwig 
2378ae01a0abSYan Zheng 		nritems = btrfs_header_nritems(path->nodes[0]);
2379ae01a0abSYan Zheng 		if (path->slots[0] >= nritems) {
2380f46b5a66SChristoph Hellwig 			ret = btrfs_next_leaf(root, path);
2381f46b5a66SChristoph Hellwig 			if (ret < 0)
2382f46b5a66SChristoph Hellwig 				goto out;
2383f46b5a66SChristoph Hellwig 			if (ret > 0)
2384f46b5a66SChristoph Hellwig 				break;
2385ae01a0abSYan Zheng 			nritems = btrfs_header_nritems(path->nodes[0]);
2386f46b5a66SChristoph Hellwig 		}
2387f46b5a66SChristoph Hellwig 		leaf = path->nodes[0];
2388f46b5a66SChristoph Hellwig 		slot = path->slots[0];
2389f46b5a66SChristoph Hellwig 
2390ae01a0abSYan Zheng 		btrfs_item_key_to_cpu(leaf, &key, slot);
2391d20f7043SChris Mason 		if (btrfs_key_type(&key) > BTRFS_EXTENT_DATA_KEY ||
239233345d01SLi Zefan 		    key.objectid != btrfs_ino(src))
2393f46b5a66SChristoph Hellwig 			break;
2394f46b5a66SChristoph Hellwig 
2395c5c9cd4dSSage Weil 		if (btrfs_key_type(&key) == BTRFS_EXTENT_DATA_KEY) {
2396c5c9cd4dSSage Weil 			struct btrfs_file_extent_item *extent;
2397c5c9cd4dSSage Weil 			int type;
239831840ae1SZheng Yan 			u32 size;
239931840ae1SZheng Yan 			struct btrfs_key new_key;
2400c5c9cd4dSSage Weil 			u64 disko = 0, diskl = 0;
2401c5c9cd4dSSage Weil 			u64 datao = 0, datal = 0;
2402c5c9cd4dSSage Weil 			u8 comp;
2403b5384d48SSage Weil 			u64 endoff;
240431840ae1SZheng Yan 
240531840ae1SZheng Yan 			size = btrfs_item_size_nr(leaf, slot);
240631840ae1SZheng Yan 			read_extent_buffer(leaf, buf,
240731840ae1SZheng Yan 					   btrfs_item_ptr_offset(leaf, slot),
240831840ae1SZheng Yan 					   size);
2409c5c9cd4dSSage Weil 
2410c5c9cd4dSSage Weil 			extent = btrfs_item_ptr(leaf, slot,
2411c5c9cd4dSSage Weil 						struct btrfs_file_extent_item);
2412c5c9cd4dSSage Weil 			comp = btrfs_file_extent_compression(leaf, extent);
2413c5c9cd4dSSage Weil 			type = btrfs_file_extent_type(leaf, extent);
2414c8a894d7SChris Mason 			if (type == BTRFS_FILE_EXTENT_REG ||
2415c8a894d7SChris Mason 			    type == BTRFS_FILE_EXTENT_PREALLOC) {
2416d397712bSChris Mason 				disko = btrfs_file_extent_disk_bytenr(leaf,
2417d397712bSChris Mason 								      extent);
2418d397712bSChris Mason 				diskl = btrfs_file_extent_disk_num_bytes(leaf,
2419d397712bSChris Mason 								 extent);
2420c5c9cd4dSSage Weil 				datao = btrfs_file_extent_offset(leaf, extent);
2421d397712bSChris Mason 				datal = btrfs_file_extent_num_bytes(leaf,
2422d397712bSChris Mason 								    extent);
2423c5c9cd4dSSage Weil 			} else if (type == BTRFS_FILE_EXTENT_INLINE) {
2424c5c9cd4dSSage Weil 				/* take upper bound, may be compressed */
2425c5c9cd4dSSage Weil 				datal = btrfs_file_extent_ram_bytes(leaf,
2426c5c9cd4dSSage Weil 								    extent);
2427c5c9cd4dSSage Weil 			}
2428b3b4aa74SDavid Sterba 			btrfs_release_path(path);
242931840ae1SZheng Yan 
2430050006a7SSage Weil 			if (key.offset + datal <= off ||
2431c5c9cd4dSSage Weil 			    key.offset >= off+len)
2432c5c9cd4dSSage Weil 				goto next;
2433c5c9cd4dSSage Weil 
243431840ae1SZheng Yan 			memcpy(&new_key, &key, sizeof(new_key));
243533345d01SLi Zefan 			new_key.objectid = btrfs_ino(inode);
24364d728ec7SLi Zefan 			if (off <= key.offset)
2437c5c9cd4dSSage Weil 				new_key.offset = key.offset + destoff - off;
24384d728ec7SLi Zefan 			else
24394d728ec7SLi Zefan 				new_key.offset = destoff;
2440c5c9cd4dSSage Weil 
2441b6f3409bSSage Weil 			/*
2442b6f3409bSSage Weil 			 * 1 - adjusting old extent (we may have to split it)
2443b6f3409bSSage Weil 			 * 1 - add new extent
2444b6f3409bSSage Weil 			 * 1 - inode update
2445b6f3409bSSage Weil 			 */
2446b6f3409bSSage Weil 			trans = btrfs_start_transaction(root, 3);
2447a22285a6SYan, Zheng 			if (IS_ERR(trans)) {
2448a22285a6SYan, Zheng 				ret = PTR_ERR(trans);
2449a22285a6SYan, Zheng 				goto out;
2450a22285a6SYan, Zheng 			}
2451a22285a6SYan, Zheng 
2452c8a894d7SChris Mason 			if (type == BTRFS_FILE_EXTENT_REG ||
2453c8a894d7SChris Mason 			    type == BTRFS_FILE_EXTENT_PREALLOC) {
2454d72c0842SLi Zefan 				/*
2455d72c0842SLi Zefan 				 *    a  | --- range to clone ---|  b
2456d72c0842SLi Zefan 				 * | ------------- extent ------------- |
2457d72c0842SLi Zefan 				 */
2458d72c0842SLi Zefan 
2459d72c0842SLi Zefan 				/* substract range b */
2460d72c0842SLi Zefan 				if (key.offset + datal > off + len)
2461d72c0842SLi Zefan 					datal = off + len - key.offset;
2462d72c0842SLi Zefan 
2463d72c0842SLi Zefan 				/* substract range a */
2464a22285a6SYan, Zheng 				if (off > key.offset) {
2465a22285a6SYan, Zheng 					datao += off - key.offset;
2466a22285a6SYan, Zheng 					datal -= off - key.offset;
2467a22285a6SYan, Zheng 				}
2468a22285a6SYan, Zheng 
2469a22285a6SYan, Zheng 				ret = btrfs_drop_extents(trans, inode,
2470a22285a6SYan, Zheng 							 new_key.offset,
2471a22285a6SYan, Zheng 							 new_key.offset + datal,
2472a22285a6SYan, Zheng 							 &hint_byte, 1);
247379787eaaSJeff Mahoney 				if (ret) {
247479787eaaSJeff Mahoney 					btrfs_abort_transaction(trans, root,
247579787eaaSJeff Mahoney 								ret);
247679787eaaSJeff Mahoney 					btrfs_end_transaction(trans, root);
247779787eaaSJeff Mahoney 					goto out;
247879787eaaSJeff Mahoney 				}
2479a22285a6SYan, Zheng 
248031840ae1SZheng Yan 				ret = btrfs_insert_empty_item(trans, root, path,
248131840ae1SZheng Yan 							      &new_key, size);
248279787eaaSJeff Mahoney 				if (ret) {
248379787eaaSJeff Mahoney 					btrfs_abort_transaction(trans, root,
248479787eaaSJeff Mahoney 								ret);
248579787eaaSJeff Mahoney 					btrfs_end_transaction(trans, root);
248679787eaaSJeff Mahoney 					goto out;
248779787eaaSJeff Mahoney 				}
248831840ae1SZheng Yan 
248931840ae1SZheng Yan 				leaf = path->nodes[0];
249031840ae1SZheng Yan 				slot = path->slots[0];
249131840ae1SZheng Yan 				write_extent_buffer(leaf, buf,
249231840ae1SZheng Yan 					    btrfs_item_ptr_offset(leaf, slot),
249331840ae1SZheng Yan 					    size);
2494ae01a0abSYan Zheng 
2495f46b5a66SChristoph Hellwig 				extent = btrfs_item_ptr(leaf, slot,
2496f46b5a66SChristoph Hellwig 						struct btrfs_file_extent_item);
2497c5c9cd4dSSage Weil 
2498c5c9cd4dSSage Weil 				/* disko == 0 means it's a hole */
2499c5c9cd4dSSage Weil 				if (!disko)
2500c5c9cd4dSSage Weil 					datao = 0;
2501c5c9cd4dSSage Weil 
2502c5c9cd4dSSage Weil 				btrfs_set_file_extent_offset(leaf, extent,
2503c5c9cd4dSSage Weil 							     datao);
2504c5c9cd4dSSage Weil 				btrfs_set_file_extent_num_bytes(leaf, extent,
2505c5c9cd4dSSage Weil 								datal);
2506c5c9cd4dSSage Weil 				if (disko) {
2507c5c9cd4dSSage Weil 					inode_add_bytes(inode, datal);
2508ae01a0abSYan Zheng 					ret = btrfs_inc_extent_ref(trans, root,
25095d4f98a2SYan Zheng 							disko, diskl, 0,
2510f46b5a66SChristoph Hellwig 							root->root_key.objectid,
251133345d01SLi Zefan 							btrfs_ino(inode),
251266d7e7f0SArne Jansen 							new_key.offset - datao,
251366d7e7f0SArne Jansen 							0);
251479787eaaSJeff Mahoney 					if (ret) {
251579787eaaSJeff Mahoney 						btrfs_abort_transaction(trans,
251679787eaaSJeff Mahoney 									root,
251779787eaaSJeff Mahoney 									ret);
251879787eaaSJeff Mahoney 						btrfs_end_transaction(trans,
251979787eaaSJeff Mahoney 								      root);
252079787eaaSJeff Mahoney 						goto out;
252179787eaaSJeff Mahoney 
252279787eaaSJeff Mahoney 					}
2523f46b5a66SChristoph Hellwig 				}
2524c5c9cd4dSSage Weil 			} else if (type == BTRFS_FILE_EXTENT_INLINE) {
2525c5c9cd4dSSage Weil 				u64 skip = 0;
2526c5c9cd4dSSage Weil 				u64 trim = 0;
2527c5c9cd4dSSage Weil 				if (off > key.offset) {
2528c5c9cd4dSSage Weil 					skip = off - key.offset;
2529c5c9cd4dSSage Weil 					new_key.offset += skip;
253031840ae1SZheng Yan 				}
2531d397712bSChris Mason 
2532c5c9cd4dSSage Weil 				if (key.offset + datal > off+len)
2533c5c9cd4dSSage Weil 					trim = key.offset + datal - (off+len);
2534d397712bSChris Mason 
2535c5c9cd4dSSage Weil 				if (comp && (skip || trim)) {
2536c5c9cd4dSSage Weil 					ret = -EINVAL;
2537a22285a6SYan, Zheng 					btrfs_end_transaction(trans, root);
2538c5c9cd4dSSage Weil 					goto out;
253931840ae1SZheng Yan 				}
2540c5c9cd4dSSage Weil 				size -= skip + trim;
2541c5c9cd4dSSage Weil 				datal -= skip + trim;
2542a22285a6SYan, Zheng 
2543a22285a6SYan, Zheng 				ret = btrfs_drop_extents(trans, inode,
2544a22285a6SYan, Zheng 							 new_key.offset,
2545a22285a6SYan, Zheng 							 new_key.offset + datal,
2546a22285a6SYan, Zheng 							 &hint_byte, 1);
254779787eaaSJeff Mahoney 				if (ret) {
254879787eaaSJeff Mahoney 					btrfs_abort_transaction(trans, root,
254979787eaaSJeff Mahoney 								ret);
255079787eaaSJeff Mahoney 					btrfs_end_transaction(trans, root);
255179787eaaSJeff Mahoney 					goto out;
255279787eaaSJeff Mahoney 				}
2553a22285a6SYan, Zheng 
2554c5c9cd4dSSage Weil 				ret = btrfs_insert_empty_item(trans, root, path,
2555c5c9cd4dSSage Weil 							      &new_key, size);
255679787eaaSJeff Mahoney 				if (ret) {
255779787eaaSJeff Mahoney 					btrfs_abort_transaction(trans, root,
255879787eaaSJeff Mahoney 								ret);
255979787eaaSJeff Mahoney 					btrfs_end_transaction(trans, root);
256079787eaaSJeff Mahoney 					goto out;
256179787eaaSJeff Mahoney 				}
2562c5c9cd4dSSage Weil 
2563c5c9cd4dSSage Weil 				if (skip) {
2564d397712bSChris Mason 					u32 start =
2565d397712bSChris Mason 					  btrfs_file_extent_calc_inline_size(0);
2566c5c9cd4dSSage Weil 					memmove(buf+start, buf+start+skip,
2567c5c9cd4dSSage Weil 						datal);
2568c5c9cd4dSSage Weil 				}
2569c5c9cd4dSSage Weil 
2570c5c9cd4dSSage Weil 				leaf = path->nodes[0];
2571c5c9cd4dSSage Weil 				slot = path->slots[0];
2572c5c9cd4dSSage Weil 				write_extent_buffer(leaf, buf,
2573c5c9cd4dSSage Weil 					    btrfs_item_ptr_offset(leaf, slot),
2574c5c9cd4dSSage Weil 					    size);
2575c5c9cd4dSSage Weil 				inode_add_bytes(inode, datal);
2576c5c9cd4dSSage Weil 			}
2577c5c9cd4dSSage Weil 
2578c5c9cd4dSSage Weil 			btrfs_mark_buffer_dirty(leaf);
2579b3b4aa74SDavid Sterba 			btrfs_release_path(path);
2580c5c9cd4dSSage Weil 
2581a22285a6SYan, Zheng 			inode->i_mtime = inode->i_ctime = CURRENT_TIME;
2582b5384d48SSage Weil 
2583b5384d48SSage Weil 			/*
2584b5384d48SSage Weil 			 * we round up to the block size at eof when
2585b5384d48SSage Weil 			 * determining which extents to clone above,
2586b5384d48SSage Weil 			 * but shouldn't round up the file size
2587b5384d48SSage Weil 			 */
2588b5384d48SSage Weil 			endoff = new_key.offset + datal;
25895f3888ffSLi Zefan 			if (endoff > destoff+olen)
25905f3888ffSLi Zefan 				endoff = destoff+olen;
2591b5384d48SSage Weil 			if (endoff > inode->i_size)
2592b5384d48SSage Weil 				btrfs_i_size_write(inode, endoff);
2593b5384d48SSage Weil 
2594a22285a6SYan, Zheng 			ret = btrfs_update_inode(trans, root, inode);
259579787eaaSJeff Mahoney 			if (ret) {
259679787eaaSJeff Mahoney 				btrfs_abort_transaction(trans, root, ret);
2597a22285a6SYan, Zheng 				btrfs_end_transaction(trans, root);
259879787eaaSJeff Mahoney 				goto out;
259979787eaaSJeff Mahoney 			}
260079787eaaSJeff Mahoney 			ret = btrfs_end_transaction(trans, root);
2601a22285a6SYan, Zheng 		}
2602c5c9cd4dSSage Weil next:
2603b3b4aa74SDavid Sterba 		btrfs_release_path(path);
2604ae01a0abSYan Zheng 		key.offset++;
2605ae01a0abSYan Zheng 	}
2606f46b5a66SChristoph Hellwig 	ret = 0;
2607f46b5a66SChristoph Hellwig out:
2608b3b4aa74SDavid Sterba 	btrfs_release_path(path);
2609d0082371SJeff Mahoney 	unlock_extent(&BTRFS_I(src)->io_tree, off, off+len);
2610f46b5a66SChristoph Hellwig out_unlock:
2611f46b5a66SChristoph Hellwig 	mutex_unlock(&src->i_mutex);
2612f46b5a66SChristoph Hellwig 	mutex_unlock(&inode->i_mutex);
2613ae01a0abSYan Zheng 	vfree(buf);
2614ae01a0abSYan Zheng 	btrfs_free_path(path);
2615f46b5a66SChristoph Hellwig out_fput:
2616f46b5a66SChristoph Hellwig 	fput(src_file);
2617ab67b7c1SYan Zheng out_drop_write:
26182a79f17eSAl Viro 	mnt_drop_write_file(file);
2619f46b5a66SChristoph Hellwig 	return ret;
2620f46b5a66SChristoph Hellwig }
2621f46b5a66SChristoph Hellwig 
26227a865e8aSChristoph Hellwig static long btrfs_ioctl_clone_range(struct file *file, void __user *argp)
2623c5c9cd4dSSage Weil {
2624c5c9cd4dSSage Weil 	struct btrfs_ioctl_clone_range_args args;
2625c5c9cd4dSSage Weil 
26267a865e8aSChristoph Hellwig 	if (copy_from_user(&args, argp, sizeof(args)))
2627c5c9cd4dSSage Weil 		return -EFAULT;
2628c5c9cd4dSSage Weil 	return btrfs_ioctl_clone(file, args.src_fd, args.src_offset,
2629c5c9cd4dSSage Weil 				 args.src_length, args.dest_offset);
2630c5c9cd4dSSage Weil }
2631c5c9cd4dSSage Weil 
2632f46b5a66SChristoph Hellwig /*
2633f46b5a66SChristoph Hellwig  * there are many ways the trans_start and trans_end ioctls can lead
2634f46b5a66SChristoph Hellwig  * to deadlocks.  They should only be used by applications that
2635f46b5a66SChristoph Hellwig  * basically own the machine, and have a very in depth understanding
2636f46b5a66SChristoph Hellwig  * of all the possible deadlocks and enospc problems.
2637f46b5a66SChristoph Hellwig  */
2638b2950863SChristoph Hellwig static long btrfs_ioctl_trans_start(struct file *file)
2639f46b5a66SChristoph Hellwig {
2640f46b5a66SChristoph Hellwig 	struct inode *inode = fdentry(file)->d_inode;
2641f46b5a66SChristoph Hellwig 	struct btrfs_root *root = BTRFS_I(inode)->root;
2642f46b5a66SChristoph Hellwig 	struct btrfs_trans_handle *trans;
26431ab86aedSSage Weil 	int ret;
2644f46b5a66SChristoph Hellwig 
26451ab86aedSSage Weil 	ret = -EPERM;
2646df5b5520SChristoph Hellwig 	if (!capable(CAP_SYS_ADMIN))
2647f46b5a66SChristoph Hellwig 		goto out;
26481ab86aedSSage Weil 
26491ab86aedSSage Weil 	ret = -EINPROGRESS;
26501ab86aedSSage Weil 	if (file->private_data)
26511ab86aedSSage Weil 		goto out;
26529ca9ee09SSage Weil 
2653b83cc969SLi Zefan 	ret = -EROFS;
2654b83cc969SLi Zefan 	if (btrfs_root_readonly(root))
2655b83cc969SLi Zefan 		goto out;
2656b83cc969SLi Zefan 
2657a561be71SAl Viro 	ret = mnt_want_write_file(file);
2658c146afadSYan Zheng 	if (ret)
2659c146afadSYan Zheng 		goto out;
2660c146afadSYan Zheng 
2661a4abeea4SJosef Bacik 	atomic_inc(&root->fs_info->open_ioctl_trans);
26629ca9ee09SSage Weil 
2663f46b5a66SChristoph Hellwig 	ret = -ENOMEM;
26647a7eaa40SJosef Bacik 	trans = btrfs_start_ioctl_transaction(root);
2665abd30bb0STsutomu Itoh 	if (IS_ERR(trans))
26661ab86aedSSage Weil 		goto out_drop;
26671ab86aedSSage Weil 
26681ab86aedSSage Weil 	file->private_data = trans;
26691ab86aedSSage Weil 	return 0;
26701ab86aedSSage Weil 
26711ab86aedSSage Weil out_drop:
2672a4abeea4SJosef Bacik 	atomic_dec(&root->fs_info->open_ioctl_trans);
26732a79f17eSAl Viro 	mnt_drop_write_file(file);
2674f46b5a66SChristoph Hellwig out:
2675f46b5a66SChristoph Hellwig 	return ret;
2676f46b5a66SChristoph Hellwig }
2677f46b5a66SChristoph Hellwig 
26786ef5ed0dSJosef Bacik static long btrfs_ioctl_default_subvol(struct file *file, void __user *argp)
26796ef5ed0dSJosef Bacik {
26806ef5ed0dSJosef Bacik 	struct inode *inode = fdentry(file)->d_inode;
26816ef5ed0dSJosef Bacik 	struct btrfs_root *root = BTRFS_I(inode)->root;
26826ef5ed0dSJosef Bacik 	struct btrfs_root *new_root;
26836ef5ed0dSJosef Bacik 	struct btrfs_dir_item *di;
26846ef5ed0dSJosef Bacik 	struct btrfs_trans_handle *trans;
26856ef5ed0dSJosef Bacik 	struct btrfs_path *path;
26866ef5ed0dSJosef Bacik 	struct btrfs_key location;
26876ef5ed0dSJosef Bacik 	struct btrfs_disk_key disk_key;
26886ef5ed0dSJosef Bacik 	struct btrfs_super_block *disk_super;
26896ef5ed0dSJosef Bacik 	u64 features;
26906ef5ed0dSJosef Bacik 	u64 objectid = 0;
26916ef5ed0dSJosef Bacik 	u64 dir_id;
26926ef5ed0dSJosef Bacik 
26936ef5ed0dSJosef Bacik 	if (!capable(CAP_SYS_ADMIN))
26946ef5ed0dSJosef Bacik 		return -EPERM;
26956ef5ed0dSJosef Bacik 
26966ef5ed0dSJosef Bacik 	if (copy_from_user(&objectid, argp, sizeof(objectid)))
26976ef5ed0dSJosef Bacik 		return -EFAULT;
26986ef5ed0dSJosef Bacik 
26996ef5ed0dSJosef Bacik 	if (!objectid)
27006ef5ed0dSJosef Bacik 		objectid = root->root_key.objectid;
27016ef5ed0dSJosef Bacik 
27026ef5ed0dSJosef Bacik 	location.objectid = objectid;
27036ef5ed0dSJosef Bacik 	location.type = BTRFS_ROOT_ITEM_KEY;
27046ef5ed0dSJosef Bacik 	location.offset = (u64)-1;
27056ef5ed0dSJosef Bacik 
27066ef5ed0dSJosef Bacik 	new_root = btrfs_read_fs_root_no_name(root->fs_info, &location);
27076ef5ed0dSJosef Bacik 	if (IS_ERR(new_root))
27086ef5ed0dSJosef Bacik 		return PTR_ERR(new_root);
27096ef5ed0dSJosef Bacik 
27106ef5ed0dSJosef Bacik 	if (btrfs_root_refs(&new_root->root_item) == 0)
27116ef5ed0dSJosef Bacik 		return -ENOENT;
27126ef5ed0dSJosef Bacik 
27136ef5ed0dSJosef Bacik 	path = btrfs_alloc_path();
27146ef5ed0dSJosef Bacik 	if (!path)
27156ef5ed0dSJosef Bacik 		return -ENOMEM;
27166ef5ed0dSJosef Bacik 	path->leave_spinning = 1;
27176ef5ed0dSJosef Bacik 
27186ef5ed0dSJosef Bacik 	trans = btrfs_start_transaction(root, 1);
271998d5dc13STsutomu Itoh 	if (IS_ERR(trans)) {
27206ef5ed0dSJosef Bacik 		btrfs_free_path(path);
272198d5dc13STsutomu Itoh 		return PTR_ERR(trans);
27226ef5ed0dSJosef Bacik 	}
27236ef5ed0dSJosef Bacik 
27246c41761fSDavid Sterba 	dir_id = btrfs_super_root_dir(root->fs_info->super_copy);
27256ef5ed0dSJosef Bacik 	di = btrfs_lookup_dir_item(trans, root->fs_info->tree_root, path,
27266ef5ed0dSJosef Bacik 				   dir_id, "default", 7, 1);
2727cf1e99a4SDan Carpenter 	if (IS_ERR_OR_NULL(di)) {
27286ef5ed0dSJosef Bacik 		btrfs_free_path(path);
27296ef5ed0dSJosef Bacik 		btrfs_end_transaction(trans, root);
27306ef5ed0dSJosef Bacik 		printk(KERN_ERR "Umm, you don't have the default dir item, "
27316ef5ed0dSJosef Bacik 		       "this isn't going to work\n");
27326ef5ed0dSJosef Bacik 		return -ENOENT;
27336ef5ed0dSJosef Bacik 	}
27346ef5ed0dSJosef Bacik 
27356ef5ed0dSJosef Bacik 	btrfs_cpu_key_to_disk(&disk_key, &new_root->root_key);
27366ef5ed0dSJosef Bacik 	btrfs_set_dir_item_key(path->nodes[0], di, &disk_key);
27376ef5ed0dSJosef Bacik 	btrfs_mark_buffer_dirty(path->nodes[0]);
27386ef5ed0dSJosef Bacik 	btrfs_free_path(path);
27396ef5ed0dSJosef Bacik 
27406c41761fSDavid Sterba 	disk_super = root->fs_info->super_copy;
27416ef5ed0dSJosef Bacik 	features = btrfs_super_incompat_flags(disk_super);
27426ef5ed0dSJosef Bacik 	if (!(features & BTRFS_FEATURE_INCOMPAT_DEFAULT_SUBVOL)) {
27436ef5ed0dSJosef Bacik 		features |= BTRFS_FEATURE_INCOMPAT_DEFAULT_SUBVOL;
27446ef5ed0dSJosef Bacik 		btrfs_set_super_incompat_flags(disk_super, features);
27456ef5ed0dSJosef Bacik 	}
27466ef5ed0dSJosef Bacik 	btrfs_end_transaction(trans, root);
27476ef5ed0dSJosef Bacik 
27486ef5ed0dSJosef Bacik 	return 0;
27496ef5ed0dSJosef Bacik }
27506ef5ed0dSJosef Bacik 
2751bf5fc093SJosef Bacik static void get_block_group_info(struct list_head *groups_list,
2752bf5fc093SJosef Bacik 				 struct btrfs_ioctl_space_info *space)
2753bf5fc093SJosef Bacik {
2754bf5fc093SJosef Bacik 	struct btrfs_block_group_cache *block_group;
2755bf5fc093SJosef Bacik 
2756bf5fc093SJosef Bacik 	space->total_bytes = 0;
2757bf5fc093SJosef Bacik 	space->used_bytes = 0;
2758bf5fc093SJosef Bacik 	space->flags = 0;
2759bf5fc093SJosef Bacik 	list_for_each_entry(block_group, groups_list, list) {
2760bf5fc093SJosef Bacik 		space->flags = block_group->flags;
2761bf5fc093SJosef Bacik 		space->total_bytes += block_group->key.offset;
2762bf5fc093SJosef Bacik 		space->used_bytes +=
2763bf5fc093SJosef Bacik 			btrfs_block_group_used(&block_group->item);
2764bf5fc093SJosef Bacik 	}
2765bf5fc093SJosef Bacik }
2766bf5fc093SJosef Bacik 
27671406e432SJosef Bacik long btrfs_ioctl_space_info(struct btrfs_root *root, void __user *arg)
27681406e432SJosef Bacik {
27691406e432SJosef Bacik 	struct btrfs_ioctl_space_args space_args;
27701406e432SJosef Bacik 	struct btrfs_ioctl_space_info space;
27711406e432SJosef Bacik 	struct btrfs_ioctl_space_info *dest;
27727fde62bfSChris Mason 	struct btrfs_ioctl_space_info *dest_orig;
277313f2696fSDaniel J Blueman 	struct btrfs_ioctl_space_info __user *user_dest;
27741406e432SJosef Bacik 	struct btrfs_space_info *info;
2775bf5fc093SJosef Bacik 	u64 types[] = {BTRFS_BLOCK_GROUP_DATA,
2776bf5fc093SJosef Bacik 		       BTRFS_BLOCK_GROUP_SYSTEM,
2777bf5fc093SJosef Bacik 		       BTRFS_BLOCK_GROUP_METADATA,
2778bf5fc093SJosef Bacik 		       BTRFS_BLOCK_GROUP_DATA | BTRFS_BLOCK_GROUP_METADATA};
2779bf5fc093SJosef Bacik 	int num_types = 4;
27807fde62bfSChris Mason 	int alloc_size;
27811406e432SJosef Bacik 	int ret = 0;
278251788b1bSDan Rosenberg 	u64 slot_count = 0;
2783bf5fc093SJosef Bacik 	int i, c;
27841406e432SJosef Bacik 
27851406e432SJosef Bacik 	if (copy_from_user(&space_args,
27861406e432SJosef Bacik 			   (struct btrfs_ioctl_space_args __user *)arg,
27871406e432SJosef Bacik 			   sizeof(space_args)))
27881406e432SJosef Bacik 		return -EFAULT;
27891406e432SJosef Bacik 
2790bf5fc093SJosef Bacik 	for (i = 0; i < num_types; i++) {
2791bf5fc093SJosef Bacik 		struct btrfs_space_info *tmp;
2792bf5fc093SJosef Bacik 
2793bf5fc093SJosef Bacik 		info = NULL;
27947fde62bfSChris Mason 		rcu_read_lock();
2795bf5fc093SJosef Bacik 		list_for_each_entry_rcu(tmp, &root->fs_info->space_info,
2796bf5fc093SJosef Bacik 					list) {
2797bf5fc093SJosef Bacik 			if (tmp->flags == types[i]) {
2798bf5fc093SJosef Bacik 				info = tmp;
2799bf5fc093SJosef Bacik 				break;
2800bf5fc093SJosef Bacik 			}
2801bf5fc093SJosef Bacik 		}
28027fde62bfSChris Mason 		rcu_read_unlock();
28031406e432SJosef Bacik 
2804bf5fc093SJosef Bacik 		if (!info)
2805bf5fc093SJosef Bacik 			continue;
2806bf5fc093SJosef Bacik 
2807bf5fc093SJosef Bacik 		down_read(&info->groups_sem);
2808bf5fc093SJosef Bacik 		for (c = 0; c < BTRFS_NR_RAID_TYPES; c++) {
2809bf5fc093SJosef Bacik 			if (!list_empty(&info->block_groups[c]))
2810bf5fc093SJosef Bacik 				slot_count++;
2811bf5fc093SJosef Bacik 		}
2812bf5fc093SJosef Bacik 		up_read(&info->groups_sem);
2813bf5fc093SJosef Bacik 	}
2814bf5fc093SJosef Bacik 
28157fde62bfSChris Mason 	/* space_slots == 0 means they are asking for a count */
28167fde62bfSChris Mason 	if (space_args.space_slots == 0) {
28177fde62bfSChris Mason 		space_args.total_spaces = slot_count;
28187fde62bfSChris Mason 		goto out;
28197fde62bfSChris Mason 	}
2820bf5fc093SJosef Bacik 
282151788b1bSDan Rosenberg 	slot_count = min_t(u64, space_args.space_slots, slot_count);
2822bf5fc093SJosef Bacik 
28237fde62bfSChris Mason 	alloc_size = sizeof(*dest) * slot_count;
2824bf5fc093SJosef Bacik 
28257fde62bfSChris Mason 	/* we generally have at most 6 or so space infos, one for each raid
28267fde62bfSChris Mason 	 * level.  So, a whole page should be more than enough for everyone
28277fde62bfSChris Mason 	 */
28287fde62bfSChris Mason 	if (alloc_size > PAGE_CACHE_SIZE)
28297fde62bfSChris Mason 		return -ENOMEM;
28307fde62bfSChris Mason 
28317fde62bfSChris Mason 	space_args.total_spaces = 0;
28327fde62bfSChris Mason 	dest = kmalloc(alloc_size, GFP_NOFS);
28337fde62bfSChris Mason 	if (!dest)
28347fde62bfSChris Mason 		return -ENOMEM;
28357fde62bfSChris Mason 	dest_orig = dest;
28367fde62bfSChris Mason 
28377fde62bfSChris Mason 	/* now we have a buffer to copy into */
2838bf5fc093SJosef Bacik 	for (i = 0; i < num_types; i++) {
2839bf5fc093SJosef Bacik 		struct btrfs_space_info *tmp;
2840bf5fc093SJosef Bacik 
284151788b1bSDan Rosenberg 		if (!slot_count)
284251788b1bSDan Rosenberg 			break;
284351788b1bSDan Rosenberg 
2844bf5fc093SJosef Bacik 		info = NULL;
28451406e432SJosef Bacik 		rcu_read_lock();
2846bf5fc093SJosef Bacik 		list_for_each_entry_rcu(tmp, &root->fs_info->space_info,
2847bf5fc093SJosef Bacik 					list) {
2848bf5fc093SJosef Bacik 			if (tmp->flags == types[i]) {
2849bf5fc093SJosef Bacik 				info = tmp;
28507fde62bfSChris Mason 				break;
2851bf5fc093SJosef Bacik 			}
2852bf5fc093SJosef Bacik 		}
2853bf5fc093SJosef Bacik 		rcu_read_unlock();
28547fde62bfSChris Mason 
2855bf5fc093SJosef Bacik 		if (!info)
2856bf5fc093SJosef Bacik 			continue;
2857bf5fc093SJosef Bacik 		down_read(&info->groups_sem);
2858bf5fc093SJosef Bacik 		for (c = 0; c < BTRFS_NR_RAID_TYPES; c++) {
2859bf5fc093SJosef Bacik 			if (!list_empty(&info->block_groups[c])) {
2860bf5fc093SJosef Bacik 				get_block_group_info(&info->block_groups[c],
2861bf5fc093SJosef Bacik 						     &space);
28627fde62bfSChris Mason 				memcpy(dest, &space, sizeof(space));
28631406e432SJosef Bacik 				dest++;
28641406e432SJosef Bacik 				space_args.total_spaces++;
286551788b1bSDan Rosenberg 				slot_count--;
28661406e432SJosef Bacik 			}
286751788b1bSDan Rosenberg 			if (!slot_count)
286851788b1bSDan Rosenberg 				break;
2869bf5fc093SJosef Bacik 		}
2870bf5fc093SJosef Bacik 		up_read(&info->groups_sem);
2871bf5fc093SJosef Bacik 	}
28721406e432SJosef Bacik 
28737fde62bfSChris Mason 	user_dest = (struct btrfs_ioctl_space_info *)
28747fde62bfSChris Mason 		(arg + sizeof(struct btrfs_ioctl_space_args));
28757fde62bfSChris Mason 
28767fde62bfSChris Mason 	if (copy_to_user(user_dest, dest_orig, alloc_size))
28777fde62bfSChris Mason 		ret = -EFAULT;
28787fde62bfSChris Mason 
28797fde62bfSChris Mason 	kfree(dest_orig);
28807fde62bfSChris Mason out:
28817fde62bfSChris Mason 	if (ret == 0 && copy_to_user(arg, &space_args, sizeof(space_args)))
28821406e432SJosef Bacik 		ret = -EFAULT;
28831406e432SJosef Bacik 
28841406e432SJosef Bacik 	return ret;
28851406e432SJosef Bacik }
28861406e432SJosef Bacik 
2887f46b5a66SChristoph Hellwig /*
2888f46b5a66SChristoph Hellwig  * there are many ways the trans_start and trans_end ioctls can lead
2889f46b5a66SChristoph Hellwig  * to deadlocks.  They should only be used by applications that
2890f46b5a66SChristoph Hellwig  * basically own the machine, and have a very in depth understanding
2891f46b5a66SChristoph Hellwig  * of all the possible deadlocks and enospc problems.
2892f46b5a66SChristoph Hellwig  */
2893f46b5a66SChristoph Hellwig long btrfs_ioctl_trans_end(struct file *file)
2894f46b5a66SChristoph Hellwig {
2895f46b5a66SChristoph Hellwig 	struct inode *inode = fdentry(file)->d_inode;
2896f46b5a66SChristoph Hellwig 	struct btrfs_root *root = BTRFS_I(inode)->root;
2897f46b5a66SChristoph Hellwig 	struct btrfs_trans_handle *trans;
2898f46b5a66SChristoph Hellwig 
2899f46b5a66SChristoph Hellwig 	trans = file->private_data;
29001ab86aedSSage Weil 	if (!trans)
29011ab86aedSSage Weil 		return -EINVAL;
2902b214107eSChristoph Hellwig 	file->private_data = NULL;
29039ca9ee09SSage Weil 
29041ab86aedSSage Weil 	btrfs_end_transaction(trans, root);
29051ab86aedSSage Weil 
2906a4abeea4SJosef Bacik 	atomic_dec(&root->fs_info->open_ioctl_trans);
29079ca9ee09SSage Weil 
29082a79f17eSAl Viro 	mnt_drop_write_file(file);
29091ab86aedSSage Weil 	return 0;
2910f46b5a66SChristoph Hellwig }
2911f46b5a66SChristoph Hellwig 
291246204592SSage Weil static noinline long btrfs_ioctl_start_sync(struct file *file, void __user *argp)
291346204592SSage Weil {
291446204592SSage Weil 	struct btrfs_root *root = BTRFS_I(file->f_dentry->d_inode)->root;
291546204592SSage Weil 	struct btrfs_trans_handle *trans;
291646204592SSage Weil 	u64 transid;
2917db5b493aSTsutomu Itoh 	int ret;
291846204592SSage Weil 
291946204592SSage Weil 	trans = btrfs_start_transaction(root, 0);
292098d5dc13STsutomu Itoh 	if (IS_ERR(trans))
292198d5dc13STsutomu Itoh 		return PTR_ERR(trans);
292246204592SSage Weil 	transid = trans->transid;
2923db5b493aSTsutomu Itoh 	ret = btrfs_commit_transaction_async(trans, root, 0);
29248b2b2d3cSTsutomu Itoh 	if (ret) {
29258b2b2d3cSTsutomu Itoh 		btrfs_end_transaction(trans, root);
2926db5b493aSTsutomu Itoh 		return ret;
29278b2b2d3cSTsutomu Itoh 	}
292846204592SSage Weil 
292946204592SSage Weil 	if (argp)
293046204592SSage Weil 		if (copy_to_user(argp, &transid, sizeof(transid)))
293146204592SSage Weil 			return -EFAULT;
293246204592SSage Weil 	return 0;
293346204592SSage Weil }
293446204592SSage Weil 
293546204592SSage Weil static noinline long btrfs_ioctl_wait_sync(struct file *file, void __user *argp)
293646204592SSage Weil {
293746204592SSage Weil 	struct btrfs_root *root = BTRFS_I(file->f_dentry->d_inode)->root;
293846204592SSage Weil 	u64 transid;
293946204592SSage Weil 
294046204592SSage Weil 	if (argp) {
294146204592SSage Weil 		if (copy_from_user(&transid, argp, sizeof(transid)))
294246204592SSage Weil 			return -EFAULT;
294346204592SSage Weil 	} else {
294446204592SSage Weil 		transid = 0;  /* current trans */
294546204592SSage Weil 	}
294646204592SSage Weil 	return btrfs_wait_for_commit(root, transid);
294746204592SSage Weil }
294846204592SSage Weil 
2949475f6387SJan Schmidt static long btrfs_ioctl_scrub(struct btrfs_root *root, void __user *arg)
2950475f6387SJan Schmidt {
2951475f6387SJan Schmidt 	int ret;
2952475f6387SJan Schmidt 	struct btrfs_ioctl_scrub_args *sa;
2953475f6387SJan Schmidt 
2954475f6387SJan Schmidt 	if (!capable(CAP_SYS_ADMIN))
2955475f6387SJan Schmidt 		return -EPERM;
2956475f6387SJan Schmidt 
2957475f6387SJan Schmidt 	sa = memdup_user(arg, sizeof(*sa));
2958475f6387SJan Schmidt 	if (IS_ERR(sa))
2959475f6387SJan Schmidt 		return PTR_ERR(sa);
2960475f6387SJan Schmidt 
2961475f6387SJan Schmidt 	ret = btrfs_scrub_dev(root, sa->devid, sa->start, sa->end,
29628628764eSArne Jansen 			      &sa->progress, sa->flags & BTRFS_SCRUB_READONLY);
2963475f6387SJan Schmidt 
2964475f6387SJan Schmidt 	if (copy_to_user(arg, sa, sizeof(*sa)))
2965475f6387SJan Schmidt 		ret = -EFAULT;
2966475f6387SJan Schmidt 
2967475f6387SJan Schmidt 	kfree(sa);
2968475f6387SJan Schmidt 	return ret;
2969475f6387SJan Schmidt }
2970475f6387SJan Schmidt 
2971475f6387SJan Schmidt static long btrfs_ioctl_scrub_cancel(struct btrfs_root *root, void __user *arg)
2972475f6387SJan Schmidt {
2973475f6387SJan Schmidt 	if (!capable(CAP_SYS_ADMIN))
2974475f6387SJan Schmidt 		return -EPERM;
2975475f6387SJan Schmidt 
2976475f6387SJan Schmidt 	return btrfs_scrub_cancel(root);
2977475f6387SJan Schmidt }
2978475f6387SJan Schmidt 
2979475f6387SJan Schmidt static long btrfs_ioctl_scrub_progress(struct btrfs_root *root,
2980475f6387SJan Schmidt 				       void __user *arg)
2981475f6387SJan Schmidt {
2982475f6387SJan Schmidt 	struct btrfs_ioctl_scrub_args *sa;
2983475f6387SJan Schmidt 	int ret;
2984475f6387SJan Schmidt 
2985475f6387SJan Schmidt 	if (!capable(CAP_SYS_ADMIN))
2986475f6387SJan Schmidt 		return -EPERM;
2987475f6387SJan Schmidt 
2988475f6387SJan Schmidt 	sa = memdup_user(arg, sizeof(*sa));
2989475f6387SJan Schmidt 	if (IS_ERR(sa))
2990475f6387SJan Schmidt 		return PTR_ERR(sa);
2991475f6387SJan Schmidt 
2992475f6387SJan Schmidt 	ret = btrfs_scrub_progress(root, sa->devid, &sa->progress);
2993475f6387SJan Schmidt 
2994475f6387SJan Schmidt 	if (copy_to_user(arg, sa, sizeof(*sa)))
2995475f6387SJan Schmidt 		ret = -EFAULT;
2996475f6387SJan Schmidt 
2997475f6387SJan Schmidt 	kfree(sa);
2998475f6387SJan Schmidt 	return ret;
2999475f6387SJan Schmidt }
3000475f6387SJan Schmidt 
3001d7728c96SJan Schmidt static long btrfs_ioctl_ino_to_path(struct btrfs_root *root, void __user *arg)
3002d7728c96SJan Schmidt {
3003d7728c96SJan Schmidt 	int ret = 0;
3004d7728c96SJan Schmidt 	int i;
3005740c3d22SChris Mason 	u64 rel_ptr;
3006d7728c96SJan Schmidt 	int size;
3007806468f8SChris Mason 	struct btrfs_ioctl_ino_path_args *ipa = NULL;
3008d7728c96SJan Schmidt 	struct inode_fs_paths *ipath = NULL;
3009d7728c96SJan Schmidt 	struct btrfs_path *path;
3010d7728c96SJan Schmidt 
3011d7728c96SJan Schmidt 	if (!capable(CAP_SYS_ADMIN))
3012d7728c96SJan Schmidt 		return -EPERM;
3013d7728c96SJan Schmidt 
3014d7728c96SJan Schmidt 	path = btrfs_alloc_path();
3015d7728c96SJan Schmidt 	if (!path) {
3016d7728c96SJan Schmidt 		ret = -ENOMEM;
3017d7728c96SJan Schmidt 		goto out;
3018d7728c96SJan Schmidt 	}
3019d7728c96SJan Schmidt 
3020d7728c96SJan Schmidt 	ipa = memdup_user(arg, sizeof(*ipa));
3021d7728c96SJan Schmidt 	if (IS_ERR(ipa)) {
3022d7728c96SJan Schmidt 		ret = PTR_ERR(ipa);
3023d7728c96SJan Schmidt 		ipa = NULL;
3024d7728c96SJan Schmidt 		goto out;
3025d7728c96SJan Schmidt 	}
3026d7728c96SJan Schmidt 
3027d7728c96SJan Schmidt 	size = min_t(u32, ipa->size, 4096);
3028d7728c96SJan Schmidt 	ipath = init_ipath(size, root, path);
3029d7728c96SJan Schmidt 	if (IS_ERR(ipath)) {
3030d7728c96SJan Schmidt 		ret = PTR_ERR(ipath);
3031d7728c96SJan Schmidt 		ipath = NULL;
3032d7728c96SJan Schmidt 		goto out;
3033d7728c96SJan Schmidt 	}
3034d7728c96SJan Schmidt 
3035d7728c96SJan Schmidt 	ret = paths_from_inode(ipa->inum, ipath);
3036d7728c96SJan Schmidt 	if (ret < 0)
3037d7728c96SJan Schmidt 		goto out;
3038d7728c96SJan Schmidt 
3039d7728c96SJan Schmidt 	for (i = 0; i < ipath->fspath->elem_cnt; ++i) {
3040745c4d8eSJeff Mahoney 		rel_ptr = ipath->fspath->val[i] -
3041745c4d8eSJeff Mahoney 			  (u64)(unsigned long)ipath->fspath->val;
3042740c3d22SChris Mason 		ipath->fspath->val[i] = rel_ptr;
3043d7728c96SJan Schmidt 	}
3044d7728c96SJan Schmidt 
3045745c4d8eSJeff Mahoney 	ret = copy_to_user((void *)(unsigned long)ipa->fspath,
3046745c4d8eSJeff Mahoney 			   (void *)(unsigned long)ipath->fspath, size);
3047d7728c96SJan Schmidt 	if (ret) {
3048d7728c96SJan Schmidt 		ret = -EFAULT;
3049d7728c96SJan Schmidt 		goto out;
3050d7728c96SJan Schmidt 	}
3051d7728c96SJan Schmidt 
3052d7728c96SJan Schmidt out:
3053d7728c96SJan Schmidt 	btrfs_free_path(path);
3054d7728c96SJan Schmidt 	free_ipath(ipath);
3055d7728c96SJan Schmidt 	kfree(ipa);
3056d7728c96SJan Schmidt 
3057d7728c96SJan Schmidt 	return ret;
3058d7728c96SJan Schmidt }
3059d7728c96SJan Schmidt 
3060d7728c96SJan Schmidt static int build_ino_list(u64 inum, u64 offset, u64 root, void *ctx)
3061d7728c96SJan Schmidt {
3062d7728c96SJan Schmidt 	struct btrfs_data_container *inodes = ctx;
3063d7728c96SJan Schmidt 	const size_t c = 3 * sizeof(u64);
3064d7728c96SJan Schmidt 
3065d7728c96SJan Schmidt 	if (inodes->bytes_left >= c) {
3066d7728c96SJan Schmidt 		inodes->bytes_left -= c;
3067d7728c96SJan Schmidt 		inodes->val[inodes->elem_cnt] = inum;
3068d7728c96SJan Schmidt 		inodes->val[inodes->elem_cnt + 1] = offset;
3069d7728c96SJan Schmidt 		inodes->val[inodes->elem_cnt + 2] = root;
3070d7728c96SJan Schmidt 		inodes->elem_cnt += 3;
3071d7728c96SJan Schmidt 	} else {
3072d7728c96SJan Schmidt 		inodes->bytes_missing += c - inodes->bytes_left;
3073d7728c96SJan Schmidt 		inodes->bytes_left = 0;
3074d7728c96SJan Schmidt 		inodes->elem_missed += 3;
3075d7728c96SJan Schmidt 	}
3076d7728c96SJan Schmidt 
3077d7728c96SJan Schmidt 	return 0;
3078d7728c96SJan Schmidt }
3079d7728c96SJan Schmidt 
3080d7728c96SJan Schmidt static long btrfs_ioctl_logical_to_ino(struct btrfs_root *root,
3081d7728c96SJan Schmidt 					void __user *arg)
3082d7728c96SJan Schmidt {
3083d7728c96SJan Schmidt 	int ret = 0;
3084d7728c96SJan Schmidt 	int size;
30854692cf58SJan Schmidt 	u64 extent_item_pos;
3086d7728c96SJan Schmidt 	struct btrfs_ioctl_logical_ino_args *loi;
3087d7728c96SJan Schmidt 	struct btrfs_data_container *inodes = NULL;
3088d7728c96SJan Schmidt 	struct btrfs_path *path = NULL;
3089d7728c96SJan Schmidt 	struct btrfs_key key;
3090d7728c96SJan Schmidt 
3091d7728c96SJan Schmidt 	if (!capable(CAP_SYS_ADMIN))
3092d7728c96SJan Schmidt 		return -EPERM;
3093d7728c96SJan Schmidt 
3094d7728c96SJan Schmidt 	loi = memdup_user(arg, sizeof(*loi));
3095d7728c96SJan Schmidt 	if (IS_ERR(loi)) {
3096d7728c96SJan Schmidt 		ret = PTR_ERR(loi);
3097d7728c96SJan Schmidt 		loi = NULL;
3098d7728c96SJan Schmidt 		goto out;
3099d7728c96SJan Schmidt 	}
3100d7728c96SJan Schmidt 
3101d7728c96SJan Schmidt 	path = btrfs_alloc_path();
3102d7728c96SJan Schmidt 	if (!path) {
3103d7728c96SJan Schmidt 		ret = -ENOMEM;
3104d7728c96SJan Schmidt 		goto out;
3105d7728c96SJan Schmidt 	}
3106d7728c96SJan Schmidt 
3107d7728c96SJan Schmidt 	size = min_t(u32, loi->size, 4096);
3108d7728c96SJan Schmidt 	inodes = init_data_container(size);
3109d7728c96SJan Schmidt 	if (IS_ERR(inodes)) {
3110d7728c96SJan Schmidt 		ret = PTR_ERR(inodes);
3111d7728c96SJan Schmidt 		inodes = NULL;
3112d7728c96SJan Schmidt 		goto out;
3113d7728c96SJan Schmidt 	}
3114d7728c96SJan Schmidt 
3115d7728c96SJan Schmidt 	ret = extent_from_logical(root->fs_info, loi->logical, path, &key);
31164692cf58SJan Schmidt 	btrfs_release_path(path);
3117d7728c96SJan Schmidt 
3118d7728c96SJan Schmidt 	if (ret & BTRFS_EXTENT_FLAG_TREE_BLOCK)
3119d7728c96SJan Schmidt 		ret = -ENOENT;
3120d7728c96SJan Schmidt 	if (ret < 0)
3121d7728c96SJan Schmidt 		goto out;
3122d7728c96SJan Schmidt 
31234692cf58SJan Schmidt 	extent_item_pos = loi->logical - key.objectid;
3124d7728c96SJan Schmidt 	ret = iterate_extent_inodes(root->fs_info, path, key.objectid,
31254692cf58SJan Schmidt 					extent_item_pos, build_ino_list,
31264692cf58SJan Schmidt 					inodes);
3127d7728c96SJan Schmidt 
3128d7728c96SJan Schmidt 	if (ret < 0)
3129d7728c96SJan Schmidt 		goto out;
3130d7728c96SJan Schmidt 
3131745c4d8eSJeff Mahoney 	ret = copy_to_user((void *)(unsigned long)loi->inodes,
3132745c4d8eSJeff Mahoney 			   (void *)(unsigned long)inodes, size);
3133d7728c96SJan Schmidt 	if (ret)
3134d7728c96SJan Schmidt 		ret = -EFAULT;
3135d7728c96SJan Schmidt 
3136d7728c96SJan Schmidt out:
3137d7728c96SJan Schmidt 	btrfs_free_path(path);
3138d7728c96SJan Schmidt 	kfree(inodes);
3139d7728c96SJan Schmidt 	kfree(loi);
3140d7728c96SJan Schmidt 
3141d7728c96SJan Schmidt 	return ret;
3142d7728c96SJan Schmidt }
3143d7728c96SJan Schmidt 
314419a39dceSIlya Dryomov void update_ioctl_balance_args(struct btrfs_fs_info *fs_info, int lock,
3145c9e9f97bSIlya Dryomov 			       struct btrfs_ioctl_balance_args *bargs)
3146c9e9f97bSIlya Dryomov {
3147c9e9f97bSIlya Dryomov 	struct btrfs_balance_control *bctl = fs_info->balance_ctl;
3148c9e9f97bSIlya Dryomov 
3149c9e9f97bSIlya Dryomov 	bargs->flags = bctl->flags;
3150c9e9f97bSIlya Dryomov 
3151837d5b6eSIlya Dryomov 	if (atomic_read(&fs_info->balance_running))
3152837d5b6eSIlya Dryomov 		bargs->state |= BTRFS_BALANCE_STATE_RUNNING;
3153837d5b6eSIlya Dryomov 	if (atomic_read(&fs_info->balance_pause_req))
3154837d5b6eSIlya Dryomov 		bargs->state |= BTRFS_BALANCE_STATE_PAUSE_REQ;
3155a7e99c69SIlya Dryomov 	if (atomic_read(&fs_info->balance_cancel_req))
3156a7e99c69SIlya Dryomov 		bargs->state |= BTRFS_BALANCE_STATE_CANCEL_REQ;
3157837d5b6eSIlya Dryomov 
3158c9e9f97bSIlya Dryomov 	memcpy(&bargs->data, &bctl->data, sizeof(bargs->data));
3159c9e9f97bSIlya Dryomov 	memcpy(&bargs->meta, &bctl->meta, sizeof(bargs->meta));
3160c9e9f97bSIlya Dryomov 	memcpy(&bargs->sys, &bctl->sys, sizeof(bargs->sys));
316119a39dceSIlya Dryomov 
316219a39dceSIlya Dryomov 	if (lock) {
316319a39dceSIlya Dryomov 		spin_lock(&fs_info->balance_lock);
316419a39dceSIlya Dryomov 		memcpy(&bargs->stat, &bctl->stat, sizeof(bargs->stat));
316519a39dceSIlya Dryomov 		spin_unlock(&fs_info->balance_lock);
316619a39dceSIlya Dryomov 	} else {
316719a39dceSIlya Dryomov 		memcpy(&bargs->stat, &bctl->stat, sizeof(bargs->stat));
316819a39dceSIlya Dryomov 	}
3169c9e9f97bSIlya Dryomov }
3170c9e9f97bSIlya Dryomov 
3171c9e9f97bSIlya Dryomov static long btrfs_ioctl_balance(struct btrfs_root *root, void __user *arg)
3172c9e9f97bSIlya Dryomov {
3173c9e9f97bSIlya Dryomov 	struct btrfs_fs_info *fs_info = root->fs_info;
3174c9e9f97bSIlya Dryomov 	struct btrfs_ioctl_balance_args *bargs;
3175c9e9f97bSIlya Dryomov 	struct btrfs_balance_control *bctl;
3176c9e9f97bSIlya Dryomov 	int ret;
3177c9e9f97bSIlya Dryomov 
3178c9e9f97bSIlya Dryomov 	if (!capable(CAP_SYS_ADMIN))
3179c9e9f97bSIlya Dryomov 		return -EPERM;
3180c9e9f97bSIlya Dryomov 
3181c9e9f97bSIlya Dryomov 	if (fs_info->sb->s_flags & MS_RDONLY)
3182c9e9f97bSIlya Dryomov 		return -EROFS;
3183c9e9f97bSIlya Dryomov 
3184c9e9f97bSIlya Dryomov 	mutex_lock(&fs_info->volume_mutex);
3185c9e9f97bSIlya Dryomov 	mutex_lock(&fs_info->balance_mutex);
3186c9e9f97bSIlya Dryomov 
3187c9e9f97bSIlya Dryomov 	if (arg) {
3188c9e9f97bSIlya Dryomov 		bargs = memdup_user(arg, sizeof(*bargs));
3189c9e9f97bSIlya Dryomov 		if (IS_ERR(bargs)) {
3190c9e9f97bSIlya Dryomov 			ret = PTR_ERR(bargs);
3191c9e9f97bSIlya Dryomov 			goto out;
3192c9e9f97bSIlya Dryomov 		}
3193de322263SIlya Dryomov 
3194de322263SIlya Dryomov 		if (bargs->flags & BTRFS_BALANCE_RESUME) {
3195de322263SIlya Dryomov 			if (!fs_info->balance_ctl) {
3196de322263SIlya Dryomov 				ret = -ENOTCONN;
3197de322263SIlya Dryomov 				goto out_bargs;
3198de322263SIlya Dryomov 			}
3199de322263SIlya Dryomov 
3200de322263SIlya Dryomov 			bctl = fs_info->balance_ctl;
3201de322263SIlya Dryomov 			spin_lock(&fs_info->balance_lock);
3202de322263SIlya Dryomov 			bctl->flags |= BTRFS_BALANCE_RESUME;
3203de322263SIlya Dryomov 			spin_unlock(&fs_info->balance_lock);
3204de322263SIlya Dryomov 
3205de322263SIlya Dryomov 			goto do_balance;
3206de322263SIlya Dryomov 		}
3207c9e9f97bSIlya Dryomov 	} else {
3208c9e9f97bSIlya Dryomov 		bargs = NULL;
3209c9e9f97bSIlya Dryomov 	}
3210c9e9f97bSIlya Dryomov 
3211837d5b6eSIlya Dryomov 	if (fs_info->balance_ctl) {
3212837d5b6eSIlya Dryomov 		ret = -EINPROGRESS;
3213837d5b6eSIlya Dryomov 		goto out_bargs;
3214837d5b6eSIlya Dryomov 	}
3215837d5b6eSIlya Dryomov 
3216c9e9f97bSIlya Dryomov 	bctl = kzalloc(sizeof(*bctl), GFP_NOFS);
3217c9e9f97bSIlya Dryomov 	if (!bctl) {
3218c9e9f97bSIlya Dryomov 		ret = -ENOMEM;
3219c9e9f97bSIlya Dryomov 		goto out_bargs;
3220c9e9f97bSIlya Dryomov 	}
3221c9e9f97bSIlya Dryomov 
3222c9e9f97bSIlya Dryomov 	bctl->fs_info = fs_info;
3223c9e9f97bSIlya Dryomov 	if (arg) {
3224c9e9f97bSIlya Dryomov 		memcpy(&bctl->data, &bargs->data, sizeof(bctl->data));
3225c9e9f97bSIlya Dryomov 		memcpy(&bctl->meta, &bargs->meta, sizeof(bctl->meta));
3226c9e9f97bSIlya Dryomov 		memcpy(&bctl->sys, &bargs->sys, sizeof(bctl->sys));
3227c9e9f97bSIlya Dryomov 
3228c9e9f97bSIlya Dryomov 		bctl->flags = bargs->flags;
3229f43ffb60SIlya Dryomov 	} else {
3230f43ffb60SIlya Dryomov 		/* balance everything - no filters */
3231f43ffb60SIlya Dryomov 		bctl->flags |= BTRFS_BALANCE_TYPE_MASK;
3232c9e9f97bSIlya Dryomov 	}
3233c9e9f97bSIlya Dryomov 
3234de322263SIlya Dryomov do_balance:
3235c9e9f97bSIlya Dryomov 	ret = btrfs_balance(bctl, bargs);
3236c9e9f97bSIlya Dryomov 	/*
3237837d5b6eSIlya Dryomov 	 * bctl is freed in __cancel_balance or in free_fs_info if
3238837d5b6eSIlya Dryomov 	 * restriper was paused all the way until unmount
3239c9e9f97bSIlya Dryomov 	 */
3240c9e9f97bSIlya Dryomov 	if (arg) {
3241c9e9f97bSIlya Dryomov 		if (copy_to_user(arg, bargs, sizeof(*bargs)))
3242c9e9f97bSIlya Dryomov 			ret = -EFAULT;
3243c9e9f97bSIlya Dryomov 	}
3244c9e9f97bSIlya Dryomov 
3245c9e9f97bSIlya Dryomov out_bargs:
3246c9e9f97bSIlya Dryomov 	kfree(bargs);
3247c9e9f97bSIlya Dryomov out:
3248c9e9f97bSIlya Dryomov 	mutex_unlock(&fs_info->balance_mutex);
3249c9e9f97bSIlya Dryomov 	mutex_unlock(&fs_info->volume_mutex);
3250c9e9f97bSIlya Dryomov 	return ret;
3251c9e9f97bSIlya Dryomov }
3252c9e9f97bSIlya Dryomov 
3253837d5b6eSIlya Dryomov static long btrfs_ioctl_balance_ctl(struct btrfs_root *root, int cmd)
3254837d5b6eSIlya Dryomov {
3255837d5b6eSIlya Dryomov 	if (!capable(CAP_SYS_ADMIN))
3256837d5b6eSIlya Dryomov 		return -EPERM;
3257837d5b6eSIlya Dryomov 
3258837d5b6eSIlya Dryomov 	switch (cmd) {
3259837d5b6eSIlya Dryomov 	case BTRFS_BALANCE_CTL_PAUSE:
3260837d5b6eSIlya Dryomov 		return btrfs_pause_balance(root->fs_info);
3261a7e99c69SIlya Dryomov 	case BTRFS_BALANCE_CTL_CANCEL:
3262a7e99c69SIlya Dryomov 		return btrfs_cancel_balance(root->fs_info);
3263837d5b6eSIlya Dryomov 	}
3264837d5b6eSIlya Dryomov 
3265837d5b6eSIlya Dryomov 	return -EINVAL;
3266837d5b6eSIlya Dryomov }
3267837d5b6eSIlya Dryomov 
326819a39dceSIlya Dryomov static long btrfs_ioctl_balance_progress(struct btrfs_root *root,
326919a39dceSIlya Dryomov 					 void __user *arg)
327019a39dceSIlya Dryomov {
327119a39dceSIlya Dryomov 	struct btrfs_fs_info *fs_info = root->fs_info;
327219a39dceSIlya Dryomov 	struct btrfs_ioctl_balance_args *bargs;
327319a39dceSIlya Dryomov 	int ret = 0;
327419a39dceSIlya Dryomov 
327519a39dceSIlya Dryomov 	if (!capable(CAP_SYS_ADMIN))
327619a39dceSIlya Dryomov 		return -EPERM;
327719a39dceSIlya Dryomov 
327819a39dceSIlya Dryomov 	mutex_lock(&fs_info->balance_mutex);
327919a39dceSIlya Dryomov 	if (!fs_info->balance_ctl) {
328019a39dceSIlya Dryomov 		ret = -ENOTCONN;
328119a39dceSIlya Dryomov 		goto out;
328219a39dceSIlya Dryomov 	}
328319a39dceSIlya Dryomov 
328419a39dceSIlya Dryomov 	bargs = kzalloc(sizeof(*bargs), GFP_NOFS);
328519a39dceSIlya Dryomov 	if (!bargs) {
328619a39dceSIlya Dryomov 		ret = -ENOMEM;
328719a39dceSIlya Dryomov 		goto out;
328819a39dceSIlya Dryomov 	}
328919a39dceSIlya Dryomov 
329019a39dceSIlya Dryomov 	update_ioctl_balance_args(fs_info, 1, bargs);
329119a39dceSIlya Dryomov 
329219a39dceSIlya Dryomov 	if (copy_to_user(arg, bargs, sizeof(*bargs)))
329319a39dceSIlya Dryomov 		ret = -EFAULT;
329419a39dceSIlya Dryomov 
329519a39dceSIlya Dryomov 	kfree(bargs);
329619a39dceSIlya Dryomov out:
329719a39dceSIlya Dryomov 	mutex_unlock(&fs_info->balance_mutex);
329819a39dceSIlya Dryomov 	return ret;
329919a39dceSIlya Dryomov }
330019a39dceSIlya Dryomov 
3301f46b5a66SChristoph Hellwig long btrfs_ioctl(struct file *file, unsigned int
3302f46b5a66SChristoph Hellwig 		cmd, unsigned long arg)
3303f46b5a66SChristoph Hellwig {
3304f46b5a66SChristoph Hellwig 	struct btrfs_root *root = BTRFS_I(fdentry(file)->d_inode)->root;
33054bcabaa3SChristoph Hellwig 	void __user *argp = (void __user *)arg;
3306f46b5a66SChristoph Hellwig 
3307f46b5a66SChristoph Hellwig 	switch (cmd) {
33086cbff00fSChristoph Hellwig 	case FS_IOC_GETFLAGS:
33096cbff00fSChristoph Hellwig 		return btrfs_ioctl_getflags(file, argp);
33106cbff00fSChristoph Hellwig 	case FS_IOC_SETFLAGS:
33116cbff00fSChristoph Hellwig 		return btrfs_ioctl_setflags(file, argp);
33126cbff00fSChristoph Hellwig 	case FS_IOC_GETVERSION:
33136cbff00fSChristoph Hellwig 		return btrfs_ioctl_getversion(file, argp);
3314f7039b1dSLi Dongyang 	case FITRIM:
3315f7039b1dSLi Dongyang 		return btrfs_ioctl_fitrim(file, argp);
3316f46b5a66SChristoph Hellwig 	case BTRFS_IOC_SNAP_CREATE:
3317fa0d2b9bSLi Zefan 		return btrfs_ioctl_snap_create(file, argp, 0);
3318fdfb1e4fSLi Zefan 	case BTRFS_IOC_SNAP_CREATE_V2:
3319fa0d2b9bSLi Zefan 		return btrfs_ioctl_snap_create_v2(file, argp, 0);
33203de4586cSChris Mason 	case BTRFS_IOC_SUBVOL_CREATE:
3321fa0d2b9bSLi Zefan 		return btrfs_ioctl_snap_create(file, argp, 1);
332276dda93cSYan, Zheng 	case BTRFS_IOC_SNAP_DESTROY:
332376dda93cSYan, Zheng 		return btrfs_ioctl_snap_destroy(file, argp);
33240caa102dSLi Zefan 	case BTRFS_IOC_SUBVOL_GETFLAGS:
33250caa102dSLi Zefan 		return btrfs_ioctl_subvol_getflags(file, argp);
33260caa102dSLi Zefan 	case BTRFS_IOC_SUBVOL_SETFLAGS:
33270caa102dSLi Zefan 		return btrfs_ioctl_subvol_setflags(file, argp);
33286ef5ed0dSJosef Bacik 	case BTRFS_IOC_DEFAULT_SUBVOL:
33296ef5ed0dSJosef Bacik 		return btrfs_ioctl_default_subvol(file, argp);
3330f46b5a66SChristoph Hellwig 	case BTRFS_IOC_DEFRAG:
33311e701a32SChris Mason 		return btrfs_ioctl_defrag(file, NULL);
33321e701a32SChris Mason 	case BTRFS_IOC_DEFRAG_RANGE:
33331e701a32SChris Mason 		return btrfs_ioctl_defrag(file, argp);
3334f46b5a66SChristoph Hellwig 	case BTRFS_IOC_RESIZE:
33354bcabaa3SChristoph Hellwig 		return btrfs_ioctl_resize(root, argp);
3336f46b5a66SChristoph Hellwig 	case BTRFS_IOC_ADD_DEV:
33374bcabaa3SChristoph Hellwig 		return btrfs_ioctl_add_dev(root, argp);
3338f46b5a66SChristoph Hellwig 	case BTRFS_IOC_RM_DEV:
33394bcabaa3SChristoph Hellwig 		return btrfs_ioctl_rm_dev(root, argp);
3340475f6387SJan Schmidt 	case BTRFS_IOC_FS_INFO:
3341475f6387SJan Schmidt 		return btrfs_ioctl_fs_info(root, argp);
3342475f6387SJan Schmidt 	case BTRFS_IOC_DEV_INFO:
3343475f6387SJan Schmidt 		return btrfs_ioctl_dev_info(root, argp);
3344f46b5a66SChristoph Hellwig 	case BTRFS_IOC_BALANCE:
3345c9e9f97bSIlya Dryomov 		return btrfs_ioctl_balance(root, NULL);
3346f46b5a66SChristoph Hellwig 	case BTRFS_IOC_CLONE:
3347c5c9cd4dSSage Weil 		return btrfs_ioctl_clone(file, arg, 0, 0, 0);
3348c5c9cd4dSSage Weil 	case BTRFS_IOC_CLONE_RANGE:
33497a865e8aSChristoph Hellwig 		return btrfs_ioctl_clone_range(file, argp);
3350f46b5a66SChristoph Hellwig 	case BTRFS_IOC_TRANS_START:
3351f46b5a66SChristoph Hellwig 		return btrfs_ioctl_trans_start(file);
3352f46b5a66SChristoph Hellwig 	case BTRFS_IOC_TRANS_END:
3353f46b5a66SChristoph Hellwig 		return btrfs_ioctl_trans_end(file);
3354ac8e9819SChris Mason 	case BTRFS_IOC_TREE_SEARCH:
3355ac8e9819SChris Mason 		return btrfs_ioctl_tree_search(file, argp);
3356ac8e9819SChris Mason 	case BTRFS_IOC_INO_LOOKUP:
3357ac8e9819SChris Mason 		return btrfs_ioctl_ino_lookup(file, argp);
3358d7728c96SJan Schmidt 	case BTRFS_IOC_INO_PATHS:
3359d7728c96SJan Schmidt 		return btrfs_ioctl_ino_to_path(root, argp);
3360d7728c96SJan Schmidt 	case BTRFS_IOC_LOGICAL_INO:
3361d7728c96SJan Schmidt 		return btrfs_ioctl_logical_to_ino(root, argp);
33621406e432SJosef Bacik 	case BTRFS_IOC_SPACE_INFO:
33631406e432SJosef Bacik 		return btrfs_ioctl_space_info(root, argp);
3364f46b5a66SChristoph Hellwig 	case BTRFS_IOC_SYNC:
3365f46b5a66SChristoph Hellwig 		btrfs_sync_fs(file->f_dentry->d_sb, 1);
3366f46b5a66SChristoph Hellwig 		return 0;
336746204592SSage Weil 	case BTRFS_IOC_START_SYNC:
336846204592SSage Weil 		return btrfs_ioctl_start_sync(file, argp);
336946204592SSage Weil 	case BTRFS_IOC_WAIT_SYNC:
337046204592SSage Weil 		return btrfs_ioctl_wait_sync(file, argp);
3371475f6387SJan Schmidt 	case BTRFS_IOC_SCRUB:
3372475f6387SJan Schmidt 		return btrfs_ioctl_scrub(root, argp);
3373475f6387SJan Schmidt 	case BTRFS_IOC_SCRUB_CANCEL:
3374475f6387SJan Schmidt 		return btrfs_ioctl_scrub_cancel(root, argp);
3375475f6387SJan Schmidt 	case BTRFS_IOC_SCRUB_PROGRESS:
3376475f6387SJan Schmidt 		return btrfs_ioctl_scrub_progress(root, argp);
3377c9e9f97bSIlya Dryomov 	case BTRFS_IOC_BALANCE_V2:
3378c9e9f97bSIlya Dryomov 		return btrfs_ioctl_balance(root, argp);
3379837d5b6eSIlya Dryomov 	case BTRFS_IOC_BALANCE_CTL:
3380837d5b6eSIlya Dryomov 		return btrfs_ioctl_balance_ctl(root, arg);
338119a39dceSIlya Dryomov 	case BTRFS_IOC_BALANCE_PROGRESS:
338219a39dceSIlya Dryomov 		return btrfs_ioctl_balance_progress(root, argp);
3383f46b5a66SChristoph Hellwig 	}
3384f46b5a66SChristoph Hellwig 
3385f46b5a66SChristoph Hellwig 	return -ENOTTY;
3386f46b5a66SChristoph Hellwig }
3387