xref: /openbmc/linux/fs/btrfs/ioctl.c (revision 8ea05e3a)
1f46b5a66SChristoph Hellwig /*
2f46b5a66SChristoph Hellwig  * Copyright (C) 2007 Oracle.  All rights reserved.
3f46b5a66SChristoph Hellwig  *
4f46b5a66SChristoph Hellwig  * This program is free software; you can redistribute it and/or
5f46b5a66SChristoph Hellwig  * modify it under the terms of the GNU General Public
6f46b5a66SChristoph Hellwig  * License v2 as published by the Free Software Foundation.
7f46b5a66SChristoph Hellwig  *
8f46b5a66SChristoph Hellwig  * This program is distributed in the hope that it will be useful,
9f46b5a66SChristoph Hellwig  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10f46b5a66SChristoph Hellwig  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11f46b5a66SChristoph Hellwig  * General Public License for more details.
12f46b5a66SChristoph Hellwig  *
13f46b5a66SChristoph Hellwig  * You should have received a copy of the GNU General Public
14f46b5a66SChristoph Hellwig  * License along with this program; if not, write to the
15f46b5a66SChristoph Hellwig  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
16f46b5a66SChristoph Hellwig  * Boston, MA 021110-1307, USA.
17f46b5a66SChristoph Hellwig  */
18f46b5a66SChristoph Hellwig 
19f46b5a66SChristoph Hellwig #include <linux/kernel.h>
20f46b5a66SChristoph Hellwig #include <linux/bio.h>
21f46b5a66SChristoph Hellwig #include <linux/buffer_head.h>
22f46b5a66SChristoph Hellwig #include <linux/file.h>
23f46b5a66SChristoph Hellwig #include <linux/fs.h>
24cb8e7090SChristoph Hellwig #include <linux/fsnotify.h>
25f46b5a66SChristoph Hellwig #include <linux/pagemap.h>
26f46b5a66SChristoph Hellwig #include <linux/highmem.h>
27f46b5a66SChristoph Hellwig #include <linux/time.h>
28f46b5a66SChristoph Hellwig #include <linux/init.h>
29f46b5a66SChristoph Hellwig #include <linux/string.h>
30f46b5a66SChristoph Hellwig #include <linux/backing-dev.h>
31cb8e7090SChristoph Hellwig #include <linux/mount.h>
32f46b5a66SChristoph Hellwig #include <linux/mpage.h>
33cb8e7090SChristoph Hellwig #include <linux/namei.h>
34f46b5a66SChristoph Hellwig #include <linux/swap.h>
35f46b5a66SChristoph Hellwig #include <linux/writeback.h>
36f46b5a66SChristoph Hellwig #include <linux/statfs.h>
37f46b5a66SChristoph Hellwig #include <linux/compat.h>
38f46b5a66SChristoph Hellwig #include <linux/bit_spinlock.h>
39cb8e7090SChristoph Hellwig #include <linux/security.h>
40f46b5a66SChristoph Hellwig #include <linux/xattr.h>
417ea394f1SYan Zheng #include <linux/vmalloc.h>
425a0e3ad6STejun Heo #include <linux/slab.h>
43f7039b1dSLi Dongyang #include <linux/blkdev.h>
448ea05e3aSAlexander Block #include <linux/uuid.h>
454b4e25f2SChris Mason #include "compat.h"
46f46b5a66SChristoph Hellwig #include "ctree.h"
47f46b5a66SChristoph Hellwig #include "disk-io.h"
48f46b5a66SChristoph Hellwig #include "transaction.h"
49f46b5a66SChristoph Hellwig #include "btrfs_inode.h"
50f46b5a66SChristoph Hellwig #include "ioctl.h"
51f46b5a66SChristoph Hellwig #include "print-tree.h"
52f46b5a66SChristoph Hellwig #include "volumes.h"
53925baeddSChris Mason #include "locking.h"
54581bb050SLi Zefan #include "inode-map.h"
55d7728c96SJan Schmidt #include "backref.h"
56606686eeSJosef Bacik #include "rcu-string.h"
57f46b5a66SChristoph Hellwig 
586cbff00fSChristoph Hellwig /* Mask out flags that are inappropriate for the given type of inode. */
596cbff00fSChristoph Hellwig static inline __u32 btrfs_mask_flags(umode_t mode, __u32 flags)
606cbff00fSChristoph Hellwig {
616cbff00fSChristoph Hellwig 	if (S_ISDIR(mode))
626cbff00fSChristoph Hellwig 		return flags;
636cbff00fSChristoph Hellwig 	else if (S_ISREG(mode))
646cbff00fSChristoph Hellwig 		return flags & ~FS_DIRSYNC_FL;
656cbff00fSChristoph Hellwig 	else
666cbff00fSChristoph Hellwig 		return flags & (FS_NODUMP_FL | FS_NOATIME_FL);
676cbff00fSChristoph Hellwig }
68f46b5a66SChristoph Hellwig 
696cbff00fSChristoph Hellwig /*
706cbff00fSChristoph Hellwig  * Export inode flags to the format expected by the FS_IOC_GETFLAGS ioctl.
716cbff00fSChristoph Hellwig  */
726cbff00fSChristoph Hellwig static unsigned int btrfs_flags_to_ioctl(unsigned int flags)
736cbff00fSChristoph Hellwig {
746cbff00fSChristoph Hellwig 	unsigned int iflags = 0;
756cbff00fSChristoph Hellwig 
766cbff00fSChristoph Hellwig 	if (flags & BTRFS_INODE_SYNC)
776cbff00fSChristoph Hellwig 		iflags |= FS_SYNC_FL;
786cbff00fSChristoph Hellwig 	if (flags & BTRFS_INODE_IMMUTABLE)
796cbff00fSChristoph Hellwig 		iflags |= FS_IMMUTABLE_FL;
806cbff00fSChristoph Hellwig 	if (flags & BTRFS_INODE_APPEND)
816cbff00fSChristoph Hellwig 		iflags |= FS_APPEND_FL;
826cbff00fSChristoph Hellwig 	if (flags & BTRFS_INODE_NODUMP)
836cbff00fSChristoph Hellwig 		iflags |= FS_NODUMP_FL;
846cbff00fSChristoph Hellwig 	if (flags & BTRFS_INODE_NOATIME)
856cbff00fSChristoph Hellwig 		iflags |= FS_NOATIME_FL;
866cbff00fSChristoph Hellwig 	if (flags & BTRFS_INODE_DIRSYNC)
876cbff00fSChristoph Hellwig 		iflags |= FS_DIRSYNC_FL;
88d0092bddSLi Zefan 	if (flags & BTRFS_INODE_NODATACOW)
89d0092bddSLi Zefan 		iflags |= FS_NOCOW_FL;
90d0092bddSLi Zefan 
91d0092bddSLi Zefan 	if ((flags & BTRFS_INODE_COMPRESS) && !(flags & BTRFS_INODE_NOCOMPRESS))
92d0092bddSLi Zefan 		iflags |= FS_COMPR_FL;
93d0092bddSLi Zefan 	else if (flags & BTRFS_INODE_NOCOMPRESS)
94d0092bddSLi Zefan 		iflags |= FS_NOCOMP_FL;
956cbff00fSChristoph Hellwig 
966cbff00fSChristoph Hellwig 	return iflags;
976cbff00fSChristoph Hellwig }
986cbff00fSChristoph Hellwig 
996cbff00fSChristoph Hellwig /*
1006cbff00fSChristoph Hellwig  * Update inode->i_flags based on the btrfs internal flags.
1016cbff00fSChristoph Hellwig  */
1026cbff00fSChristoph Hellwig void btrfs_update_iflags(struct inode *inode)
1036cbff00fSChristoph Hellwig {
1046cbff00fSChristoph Hellwig 	struct btrfs_inode *ip = BTRFS_I(inode);
1056cbff00fSChristoph Hellwig 
1066cbff00fSChristoph Hellwig 	inode->i_flags &= ~(S_SYNC|S_APPEND|S_IMMUTABLE|S_NOATIME|S_DIRSYNC);
1076cbff00fSChristoph Hellwig 
1086cbff00fSChristoph Hellwig 	if (ip->flags & BTRFS_INODE_SYNC)
1096cbff00fSChristoph Hellwig 		inode->i_flags |= S_SYNC;
1106cbff00fSChristoph Hellwig 	if (ip->flags & BTRFS_INODE_IMMUTABLE)
1116cbff00fSChristoph Hellwig 		inode->i_flags |= S_IMMUTABLE;
1126cbff00fSChristoph Hellwig 	if (ip->flags & BTRFS_INODE_APPEND)
1136cbff00fSChristoph Hellwig 		inode->i_flags |= S_APPEND;
1146cbff00fSChristoph Hellwig 	if (ip->flags & BTRFS_INODE_NOATIME)
1156cbff00fSChristoph Hellwig 		inode->i_flags |= S_NOATIME;
1166cbff00fSChristoph Hellwig 	if (ip->flags & BTRFS_INODE_DIRSYNC)
1176cbff00fSChristoph Hellwig 		inode->i_flags |= S_DIRSYNC;
1186cbff00fSChristoph Hellwig }
1196cbff00fSChristoph Hellwig 
1206cbff00fSChristoph Hellwig /*
1216cbff00fSChristoph Hellwig  * Inherit flags from the parent inode.
1226cbff00fSChristoph Hellwig  *
123e27425d6SJosef Bacik  * Currently only the compression flags and the cow flags are inherited.
1246cbff00fSChristoph Hellwig  */
1256cbff00fSChristoph Hellwig void btrfs_inherit_iflags(struct inode *inode, struct inode *dir)
1266cbff00fSChristoph Hellwig {
1270b4dcea5SChris Mason 	unsigned int flags;
1280b4dcea5SChris Mason 
1290b4dcea5SChris Mason 	if (!dir)
1300b4dcea5SChris Mason 		return;
1310b4dcea5SChris Mason 
1320b4dcea5SChris Mason 	flags = BTRFS_I(dir)->flags;
1336cbff00fSChristoph Hellwig 
134e27425d6SJosef Bacik 	if (flags & BTRFS_INODE_NOCOMPRESS) {
135e27425d6SJosef Bacik 		BTRFS_I(inode)->flags &= ~BTRFS_INODE_COMPRESS;
136e27425d6SJosef Bacik 		BTRFS_I(inode)->flags |= BTRFS_INODE_NOCOMPRESS;
137e27425d6SJosef Bacik 	} else if (flags & BTRFS_INODE_COMPRESS) {
138e27425d6SJosef Bacik 		BTRFS_I(inode)->flags &= ~BTRFS_INODE_NOCOMPRESS;
139e27425d6SJosef Bacik 		BTRFS_I(inode)->flags |= BTRFS_INODE_COMPRESS;
140e27425d6SJosef Bacik 	}
1416cbff00fSChristoph Hellwig 
142e27425d6SJosef Bacik 	if (flags & BTRFS_INODE_NODATACOW)
143e27425d6SJosef Bacik 		BTRFS_I(inode)->flags |= BTRFS_INODE_NODATACOW;
144e27425d6SJosef Bacik 
1456cbff00fSChristoph Hellwig 	btrfs_update_iflags(inode);
1466cbff00fSChristoph Hellwig }
1476cbff00fSChristoph Hellwig 
1486cbff00fSChristoph Hellwig static int btrfs_ioctl_getflags(struct file *file, void __user *arg)
1496cbff00fSChristoph Hellwig {
1506cbff00fSChristoph Hellwig 	struct btrfs_inode *ip = BTRFS_I(file->f_path.dentry->d_inode);
1516cbff00fSChristoph Hellwig 	unsigned int flags = btrfs_flags_to_ioctl(ip->flags);
1526cbff00fSChristoph Hellwig 
1536cbff00fSChristoph Hellwig 	if (copy_to_user(arg, &flags, sizeof(flags)))
1546cbff00fSChristoph Hellwig 		return -EFAULT;
1556cbff00fSChristoph Hellwig 	return 0;
1566cbff00fSChristoph Hellwig }
1576cbff00fSChristoph Hellwig 
15875e7cb7fSLiu Bo static int check_flags(unsigned int flags)
15975e7cb7fSLiu Bo {
16075e7cb7fSLiu Bo 	if (flags & ~(FS_IMMUTABLE_FL | FS_APPEND_FL | \
16175e7cb7fSLiu Bo 		      FS_NOATIME_FL | FS_NODUMP_FL | \
16275e7cb7fSLiu Bo 		      FS_SYNC_FL | FS_DIRSYNC_FL | \
163e1e8fb6aSLi Zefan 		      FS_NOCOMP_FL | FS_COMPR_FL |
164e1e8fb6aSLi Zefan 		      FS_NOCOW_FL))
16575e7cb7fSLiu Bo 		return -EOPNOTSUPP;
16675e7cb7fSLiu Bo 
16775e7cb7fSLiu Bo 	if ((flags & FS_NOCOMP_FL) && (flags & FS_COMPR_FL))
16875e7cb7fSLiu Bo 		return -EINVAL;
16975e7cb7fSLiu Bo 
17075e7cb7fSLiu Bo 	return 0;
17175e7cb7fSLiu Bo }
17275e7cb7fSLiu Bo 
1736cbff00fSChristoph Hellwig static int btrfs_ioctl_setflags(struct file *file, void __user *arg)
1746cbff00fSChristoph Hellwig {
1756cbff00fSChristoph Hellwig 	struct inode *inode = file->f_path.dentry->d_inode;
1766cbff00fSChristoph Hellwig 	struct btrfs_inode *ip = BTRFS_I(inode);
1776cbff00fSChristoph Hellwig 	struct btrfs_root *root = ip->root;
1786cbff00fSChristoph Hellwig 	struct btrfs_trans_handle *trans;
1796cbff00fSChristoph Hellwig 	unsigned int flags, oldflags;
1806cbff00fSChristoph Hellwig 	int ret;
181f062abf0SLi Zefan 	u64 ip_oldflags;
182f062abf0SLi Zefan 	unsigned int i_oldflags;
1836cbff00fSChristoph Hellwig 
184b83cc969SLi Zefan 	if (btrfs_root_readonly(root))
185b83cc969SLi Zefan 		return -EROFS;
186b83cc969SLi Zefan 
1876cbff00fSChristoph Hellwig 	if (copy_from_user(&flags, arg, sizeof(flags)))
1886cbff00fSChristoph Hellwig 		return -EFAULT;
1896cbff00fSChristoph Hellwig 
19075e7cb7fSLiu Bo 	ret = check_flags(flags);
19175e7cb7fSLiu Bo 	if (ret)
19275e7cb7fSLiu Bo 		return ret;
1936cbff00fSChristoph Hellwig 
1942e149670SSerge E. Hallyn 	if (!inode_owner_or_capable(inode))
1956cbff00fSChristoph Hellwig 		return -EACCES;
1966cbff00fSChristoph Hellwig 
1976cbff00fSChristoph Hellwig 	mutex_lock(&inode->i_mutex);
1986cbff00fSChristoph Hellwig 
199f062abf0SLi Zefan 	ip_oldflags = ip->flags;
200f062abf0SLi Zefan 	i_oldflags = inode->i_flags;
201f062abf0SLi Zefan 
2026cbff00fSChristoph Hellwig 	flags = btrfs_mask_flags(inode->i_mode, flags);
2036cbff00fSChristoph Hellwig 	oldflags = btrfs_flags_to_ioctl(ip->flags);
2046cbff00fSChristoph Hellwig 	if ((flags ^ oldflags) & (FS_APPEND_FL | FS_IMMUTABLE_FL)) {
2056cbff00fSChristoph Hellwig 		if (!capable(CAP_LINUX_IMMUTABLE)) {
2066cbff00fSChristoph Hellwig 			ret = -EPERM;
2076cbff00fSChristoph Hellwig 			goto out_unlock;
2086cbff00fSChristoph Hellwig 		}
2096cbff00fSChristoph Hellwig 	}
2106cbff00fSChristoph Hellwig 
211a561be71SAl Viro 	ret = mnt_want_write_file(file);
2126cbff00fSChristoph Hellwig 	if (ret)
2136cbff00fSChristoph Hellwig 		goto out_unlock;
2146cbff00fSChristoph Hellwig 
2156cbff00fSChristoph Hellwig 	if (flags & FS_SYNC_FL)
2166cbff00fSChristoph Hellwig 		ip->flags |= BTRFS_INODE_SYNC;
2176cbff00fSChristoph Hellwig 	else
2186cbff00fSChristoph Hellwig 		ip->flags &= ~BTRFS_INODE_SYNC;
2196cbff00fSChristoph Hellwig 	if (flags & FS_IMMUTABLE_FL)
2206cbff00fSChristoph Hellwig 		ip->flags |= BTRFS_INODE_IMMUTABLE;
2216cbff00fSChristoph Hellwig 	else
2226cbff00fSChristoph Hellwig 		ip->flags &= ~BTRFS_INODE_IMMUTABLE;
2236cbff00fSChristoph Hellwig 	if (flags & FS_APPEND_FL)
2246cbff00fSChristoph Hellwig 		ip->flags |= BTRFS_INODE_APPEND;
2256cbff00fSChristoph Hellwig 	else
2266cbff00fSChristoph Hellwig 		ip->flags &= ~BTRFS_INODE_APPEND;
2276cbff00fSChristoph Hellwig 	if (flags & FS_NODUMP_FL)
2286cbff00fSChristoph Hellwig 		ip->flags |= BTRFS_INODE_NODUMP;
2296cbff00fSChristoph Hellwig 	else
2306cbff00fSChristoph Hellwig 		ip->flags &= ~BTRFS_INODE_NODUMP;
2316cbff00fSChristoph Hellwig 	if (flags & FS_NOATIME_FL)
2326cbff00fSChristoph Hellwig 		ip->flags |= BTRFS_INODE_NOATIME;
2336cbff00fSChristoph Hellwig 	else
2346cbff00fSChristoph Hellwig 		ip->flags &= ~BTRFS_INODE_NOATIME;
2356cbff00fSChristoph Hellwig 	if (flags & FS_DIRSYNC_FL)
2366cbff00fSChristoph Hellwig 		ip->flags |= BTRFS_INODE_DIRSYNC;
2376cbff00fSChristoph Hellwig 	else
2386cbff00fSChristoph Hellwig 		ip->flags &= ~BTRFS_INODE_DIRSYNC;
239e1e8fb6aSLi Zefan 	if (flags & FS_NOCOW_FL)
240e1e8fb6aSLi Zefan 		ip->flags |= BTRFS_INODE_NODATACOW;
241e1e8fb6aSLi Zefan 	else
242e1e8fb6aSLi Zefan 		ip->flags &= ~BTRFS_INODE_NODATACOW;
2436cbff00fSChristoph Hellwig 
24475e7cb7fSLiu Bo 	/*
24575e7cb7fSLiu Bo 	 * The COMPRESS flag can only be changed by users, while the NOCOMPRESS
24675e7cb7fSLiu Bo 	 * flag may be changed automatically if compression code won't make
24775e7cb7fSLiu Bo 	 * things smaller.
24875e7cb7fSLiu Bo 	 */
24975e7cb7fSLiu Bo 	if (flags & FS_NOCOMP_FL) {
25075e7cb7fSLiu Bo 		ip->flags &= ~BTRFS_INODE_COMPRESS;
25175e7cb7fSLiu Bo 		ip->flags |= BTRFS_INODE_NOCOMPRESS;
25275e7cb7fSLiu Bo 	} else if (flags & FS_COMPR_FL) {
25375e7cb7fSLiu Bo 		ip->flags |= BTRFS_INODE_COMPRESS;
25475e7cb7fSLiu Bo 		ip->flags &= ~BTRFS_INODE_NOCOMPRESS;
255ebcb904dSLi Zefan 	} else {
256ebcb904dSLi Zefan 		ip->flags &= ~(BTRFS_INODE_COMPRESS | BTRFS_INODE_NOCOMPRESS);
25775e7cb7fSLiu Bo 	}
2586cbff00fSChristoph Hellwig 
2594da6f1a3SLi Zefan 	trans = btrfs_start_transaction(root, 1);
260f062abf0SLi Zefan 	if (IS_ERR(trans)) {
261f062abf0SLi Zefan 		ret = PTR_ERR(trans);
262f062abf0SLi Zefan 		goto out_drop;
263f062abf0SLi Zefan 	}
2646cbff00fSChristoph Hellwig 
265306424ccSLi Zefan 	btrfs_update_iflags(inode);
2660c4d2d95SJosef Bacik 	inode_inc_iversion(inode);
267306424ccSLi Zefan 	inode->i_ctime = CURRENT_TIME;
2686cbff00fSChristoph Hellwig 	ret = btrfs_update_inode(trans, root, inode);
2696cbff00fSChristoph Hellwig 
2706cbff00fSChristoph Hellwig 	btrfs_end_transaction(trans, root);
271f062abf0SLi Zefan  out_drop:
272f062abf0SLi Zefan 	if (ret) {
273f062abf0SLi Zefan 		ip->flags = ip_oldflags;
274f062abf0SLi Zefan 		inode->i_flags = i_oldflags;
275f062abf0SLi Zefan 	}
2766cbff00fSChristoph Hellwig 
2772a79f17eSAl Viro 	mnt_drop_write_file(file);
2786cbff00fSChristoph Hellwig  out_unlock:
2796cbff00fSChristoph Hellwig 	mutex_unlock(&inode->i_mutex);
2802d4e6f6aSliubo 	return ret;
2816cbff00fSChristoph Hellwig }
2826cbff00fSChristoph Hellwig 
2836cbff00fSChristoph Hellwig static int btrfs_ioctl_getversion(struct file *file, int __user *arg)
2846cbff00fSChristoph Hellwig {
2856cbff00fSChristoph Hellwig 	struct inode *inode = file->f_path.dentry->d_inode;
2866cbff00fSChristoph Hellwig 
2876cbff00fSChristoph Hellwig 	return put_user(inode->i_generation, arg);
2886cbff00fSChristoph Hellwig }
289f46b5a66SChristoph Hellwig 
290f7039b1dSLi Dongyang static noinline int btrfs_ioctl_fitrim(struct file *file, void __user *arg)
291f7039b1dSLi Dongyang {
292815745cfSAl Viro 	struct btrfs_fs_info *fs_info = btrfs_sb(fdentry(file)->d_sb);
293f7039b1dSLi Dongyang 	struct btrfs_device *device;
294f7039b1dSLi Dongyang 	struct request_queue *q;
295f7039b1dSLi Dongyang 	struct fstrim_range range;
296f7039b1dSLi Dongyang 	u64 minlen = ULLONG_MAX;
297f7039b1dSLi Dongyang 	u64 num_devices = 0;
298815745cfSAl Viro 	u64 total_bytes = btrfs_super_total_bytes(fs_info->super_copy);
299f7039b1dSLi Dongyang 	int ret;
300f7039b1dSLi Dongyang 
301f7039b1dSLi Dongyang 	if (!capable(CAP_SYS_ADMIN))
302f7039b1dSLi Dongyang 		return -EPERM;
303f7039b1dSLi Dongyang 
3041f78160cSXiao Guangrong 	rcu_read_lock();
3051f78160cSXiao Guangrong 	list_for_each_entry_rcu(device, &fs_info->fs_devices->devices,
3061f78160cSXiao Guangrong 				dev_list) {
307f7039b1dSLi Dongyang 		if (!device->bdev)
308f7039b1dSLi Dongyang 			continue;
309f7039b1dSLi Dongyang 		q = bdev_get_queue(device->bdev);
310f7039b1dSLi Dongyang 		if (blk_queue_discard(q)) {
311f7039b1dSLi Dongyang 			num_devices++;
312f7039b1dSLi Dongyang 			minlen = min((u64)q->limits.discard_granularity,
313f7039b1dSLi Dongyang 				     minlen);
314f7039b1dSLi Dongyang 		}
315f7039b1dSLi Dongyang 	}
3161f78160cSXiao Guangrong 	rcu_read_unlock();
317f4c697e6SLukas Czerner 
318f7039b1dSLi Dongyang 	if (!num_devices)
319f7039b1dSLi Dongyang 		return -EOPNOTSUPP;
320f7039b1dSLi Dongyang 	if (copy_from_user(&range, arg, sizeof(range)))
321f7039b1dSLi Dongyang 		return -EFAULT;
322f4c697e6SLukas Czerner 	if (range.start > total_bytes)
323f4c697e6SLukas Czerner 		return -EINVAL;
324f7039b1dSLi Dongyang 
325f4c697e6SLukas Czerner 	range.len = min(range.len, total_bytes - range.start);
326f7039b1dSLi Dongyang 	range.minlen = max(range.minlen, minlen);
327815745cfSAl Viro 	ret = btrfs_trim_fs(fs_info->tree_root, &range);
328f7039b1dSLi Dongyang 	if (ret < 0)
329f7039b1dSLi Dongyang 		return ret;
330f7039b1dSLi Dongyang 
331f7039b1dSLi Dongyang 	if (copy_to_user(arg, &range, sizeof(range)))
332f7039b1dSLi Dongyang 		return -EFAULT;
333f7039b1dSLi Dongyang 
334f7039b1dSLi Dongyang 	return 0;
335f7039b1dSLi Dongyang }
336f7039b1dSLi Dongyang 
337cb8e7090SChristoph Hellwig static noinline int create_subvol(struct btrfs_root *root,
338cb8e7090SChristoph Hellwig 				  struct dentry *dentry,
33972fd032eSSage Weil 				  char *name, int namelen,
34072fd032eSSage Weil 				  u64 *async_transid)
341f46b5a66SChristoph Hellwig {
342f46b5a66SChristoph Hellwig 	struct btrfs_trans_handle *trans;
343f46b5a66SChristoph Hellwig 	struct btrfs_key key;
344f46b5a66SChristoph Hellwig 	struct btrfs_root_item root_item;
345f46b5a66SChristoph Hellwig 	struct btrfs_inode_item *inode_item;
346f46b5a66SChristoph Hellwig 	struct extent_buffer *leaf;
34776dda93cSYan, Zheng 	struct btrfs_root *new_root;
3482fbe8c8aSAl Viro 	struct dentry *parent = dentry->d_parent;
3496a912213SJosef Bacik 	struct inode *dir;
3508ea05e3aSAlexander Block 	struct timespec cur_time = CURRENT_TIME;
351f46b5a66SChristoph Hellwig 	int ret;
352f46b5a66SChristoph Hellwig 	int err;
353f46b5a66SChristoph Hellwig 	u64 objectid;
354f46b5a66SChristoph Hellwig 	u64 new_dirid = BTRFS_FIRST_FREE_OBJECTID;
3553de4586cSChris Mason 	u64 index = 0;
3568ea05e3aSAlexander Block 	uuid_le new_uuid;
357f46b5a66SChristoph Hellwig 
358581bb050SLi Zefan 	ret = btrfs_find_free_objectid(root->fs_info->tree_root, &objectid);
3592fbe8c8aSAl Viro 	if (ret)
360a22285a6SYan, Zheng 		return ret;
3616a912213SJosef Bacik 
3626a912213SJosef Bacik 	dir = parent->d_inode;
3636a912213SJosef Bacik 
3649ed74f2dSJosef Bacik 	/*
3659ed74f2dSJosef Bacik 	 * 1 - inode item
3669ed74f2dSJosef Bacik 	 * 2 - refs
3679ed74f2dSJosef Bacik 	 * 1 - root item
3689ed74f2dSJosef Bacik 	 * 2 - dir items
3699ed74f2dSJosef Bacik 	 */
370a22285a6SYan, Zheng 	trans = btrfs_start_transaction(root, 6);
3712fbe8c8aSAl Viro 	if (IS_ERR(trans))
372a22285a6SYan, Zheng 		return PTR_ERR(trans);
373f46b5a66SChristoph Hellwig 
3745d4f98a2SYan Zheng 	leaf = btrfs_alloc_free_block(trans, root, root->leafsize,
3755581a51aSJan Schmidt 				      0, objectid, NULL, 0, 0, 0);
3768e8a1e31SJosef Bacik 	if (IS_ERR(leaf)) {
3778e8a1e31SJosef Bacik 		ret = PTR_ERR(leaf);
3788e8a1e31SJosef Bacik 		goto fail;
3798e8a1e31SJosef Bacik 	}
380f46b5a66SChristoph Hellwig 
3815d4f98a2SYan Zheng 	memset_extent_buffer(leaf, 0, 0, sizeof(struct btrfs_header));
382f46b5a66SChristoph Hellwig 	btrfs_set_header_bytenr(leaf, leaf->start);
383f46b5a66SChristoph Hellwig 	btrfs_set_header_generation(leaf, trans->transid);
3845d4f98a2SYan Zheng 	btrfs_set_header_backref_rev(leaf, BTRFS_MIXED_BACKREF_REV);
385f46b5a66SChristoph Hellwig 	btrfs_set_header_owner(leaf, objectid);
386f46b5a66SChristoph Hellwig 
387f46b5a66SChristoph Hellwig 	write_extent_buffer(leaf, root->fs_info->fsid,
388f46b5a66SChristoph Hellwig 			    (unsigned long)btrfs_header_fsid(leaf),
389f46b5a66SChristoph Hellwig 			    BTRFS_FSID_SIZE);
3905d4f98a2SYan Zheng 	write_extent_buffer(leaf, root->fs_info->chunk_tree_uuid,
3915d4f98a2SYan Zheng 			    (unsigned long)btrfs_header_chunk_tree_uuid(leaf),
3925d4f98a2SYan Zheng 			    BTRFS_UUID_SIZE);
393f46b5a66SChristoph Hellwig 	btrfs_mark_buffer_dirty(leaf);
394f46b5a66SChristoph Hellwig 
3958ea05e3aSAlexander Block 	memset(&root_item, 0, sizeof(root_item));
3968ea05e3aSAlexander Block 
397f46b5a66SChristoph Hellwig 	inode_item = &root_item.inode;
398f46b5a66SChristoph Hellwig 	inode_item->generation = cpu_to_le64(1);
399f46b5a66SChristoph Hellwig 	inode_item->size = cpu_to_le64(3);
400f46b5a66SChristoph Hellwig 	inode_item->nlink = cpu_to_le32(1);
401a76a3cd4SYan Zheng 	inode_item->nbytes = cpu_to_le64(root->leafsize);
402f46b5a66SChristoph Hellwig 	inode_item->mode = cpu_to_le32(S_IFDIR | 0755);
403f46b5a66SChristoph Hellwig 
40408fe4db1SLi Zefan 	root_item.flags = 0;
40508fe4db1SLi Zefan 	root_item.byte_limit = 0;
40608fe4db1SLi Zefan 	inode_item->flags = cpu_to_le64(BTRFS_INODE_ROOT_ITEM_INIT);
40708fe4db1SLi Zefan 
408f46b5a66SChristoph Hellwig 	btrfs_set_root_bytenr(&root_item, leaf->start);
40984234f3aSYan Zheng 	btrfs_set_root_generation(&root_item, trans->transid);
410f46b5a66SChristoph Hellwig 	btrfs_set_root_level(&root_item, 0);
411f46b5a66SChristoph Hellwig 	btrfs_set_root_refs(&root_item, 1);
41286b9f2ecSYan, Zheng 	btrfs_set_root_used(&root_item, leaf->len);
41380ff3856SYan Zheng 	btrfs_set_root_last_snapshot(&root_item, 0);
414f46b5a66SChristoph Hellwig 
4158ea05e3aSAlexander Block 	btrfs_set_root_generation_v2(&root_item,
4168ea05e3aSAlexander Block 			btrfs_root_generation(&root_item));
4178ea05e3aSAlexander Block 	uuid_le_gen(&new_uuid);
4188ea05e3aSAlexander Block 	memcpy(root_item.uuid, new_uuid.b, BTRFS_UUID_SIZE);
4198ea05e3aSAlexander Block 	root_item.otime.sec = cpu_to_le64(cur_time.tv_sec);
4208ea05e3aSAlexander Block 	root_item.otime.nsec = cpu_to_le64(cur_time.tv_nsec);
4218ea05e3aSAlexander Block 	root_item.ctime = root_item.otime;
4228ea05e3aSAlexander Block 	btrfs_set_root_ctransid(&root_item, trans->transid);
4238ea05e3aSAlexander Block 	btrfs_set_root_otransid(&root_item, trans->transid);
424f46b5a66SChristoph Hellwig 
425925baeddSChris Mason 	btrfs_tree_unlock(leaf);
426f46b5a66SChristoph Hellwig 	free_extent_buffer(leaf);
427f46b5a66SChristoph Hellwig 	leaf = NULL;
428f46b5a66SChristoph Hellwig 
429f46b5a66SChristoph Hellwig 	btrfs_set_root_dirid(&root_item, new_dirid);
430f46b5a66SChristoph Hellwig 
431f46b5a66SChristoph Hellwig 	key.objectid = objectid;
4325d4f98a2SYan Zheng 	key.offset = 0;
433f46b5a66SChristoph Hellwig 	btrfs_set_key_type(&key, BTRFS_ROOT_ITEM_KEY);
434f46b5a66SChristoph Hellwig 	ret = btrfs_insert_root(trans, root->fs_info->tree_root, &key,
435f46b5a66SChristoph Hellwig 				&root_item);
436f46b5a66SChristoph Hellwig 	if (ret)
437f46b5a66SChristoph Hellwig 		goto fail;
438f46b5a66SChristoph Hellwig 
43976dda93cSYan, Zheng 	key.offset = (u64)-1;
44076dda93cSYan, Zheng 	new_root = btrfs_read_fs_root_no_name(root->fs_info, &key);
44179787eaaSJeff Mahoney 	if (IS_ERR(new_root)) {
44279787eaaSJeff Mahoney 		btrfs_abort_transaction(trans, root, PTR_ERR(new_root));
44379787eaaSJeff Mahoney 		ret = PTR_ERR(new_root);
44479787eaaSJeff Mahoney 		goto fail;
44579787eaaSJeff Mahoney 	}
44676dda93cSYan, Zheng 
44776dda93cSYan, Zheng 	btrfs_record_root_in_trans(trans, new_root);
44876dda93cSYan, Zheng 
449d82a6f1dSJosef Bacik 	ret = btrfs_create_subvol_root(trans, new_root, new_dirid);
450ce598979SMark Fasheh 	if (ret) {
451ce598979SMark Fasheh 		/* We potentially lose an unused inode item here */
45279787eaaSJeff Mahoney 		btrfs_abort_transaction(trans, root, ret);
453ce598979SMark Fasheh 		goto fail;
454ce598979SMark Fasheh 	}
455ce598979SMark Fasheh 
456f46b5a66SChristoph Hellwig 	/*
457f46b5a66SChristoph Hellwig 	 * insert the directory item
458f46b5a66SChristoph Hellwig 	 */
4593de4586cSChris Mason 	ret = btrfs_set_inode_index(dir, &index);
46079787eaaSJeff Mahoney 	if (ret) {
46179787eaaSJeff Mahoney 		btrfs_abort_transaction(trans, root, ret);
46279787eaaSJeff Mahoney 		goto fail;
46379787eaaSJeff Mahoney 	}
4643de4586cSChris Mason 
4653de4586cSChris Mason 	ret = btrfs_insert_dir_item(trans, root,
46616cdcec7SMiao Xie 				    name, namelen, dir, &key,
4673de4586cSChris Mason 				    BTRFS_FT_DIR, index);
46879787eaaSJeff Mahoney 	if (ret) {
46979787eaaSJeff Mahoney 		btrfs_abort_transaction(trans, root, ret);
470f46b5a66SChristoph Hellwig 		goto fail;
47179787eaaSJeff Mahoney 	}
4720660b5afSChris Mason 
47352c26179SYan Zheng 	btrfs_i_size_write(dir, dir->i_size + namelen * 2);
47452c26179SYan Zheng 	ret = btrfs_update_inode(trans, root, dir);
47552c26179SYan Zheng 	BUG_ON(ret);
47652c26179SYan Zheng 
4770660b5afSChris Mason 	ret = btrfs_add_root_ref(trans, root->fs_info->tree_root,
4784df27c4dSYan, Zheng 				 objectid, root->root_key.objectid,
47933345d01SLi Zefan 				 btrfs_ino(dir), index, name, namelen);
48076dda93cSYan, Zheng 
4810660b5afSChris Mason 	BUG_ON(ret);
4820660b5afSChris Mason 
48376dda93cSYan, Zheng 	d_instantiate(dentry, btrfs_lookup_dentry(dir, dentry));
484f46b5a66SChristoph Hellwig fail:
48572fd032eSSage Weil 	if (async_transid) {
48672fd032eSSage Weil 		*async_transid = trans->transid;
48772fd032eSSage Weil 		err = btrfs_commit_transaction_async(trans, root, 1);
48872fd032eSSage Weil 	} else {
48976dda93cSYan, Zheng 		err = btrfs_commit_transaction(trans, root);
49072fd032eSSage Weil 	}
491f46b5a66SChristoph Hellwig 	if (err && !ret)
492f46b5a66SChristoph Hellwig 		ret = err;
493f46b5a66SChristoph Hellwig 	return ret;
494f46b5a66SChristoph Hellwig }
495f46b5a66SChristoph Hellwig 
49672fd032eSSage Weil static int create_snapshot(struct btrfs_root *root, struct dentry *dentry,
497b83cc969SLi Zefan 			   char *name, int namelen, u64 *async_transid,
498b83cc969SLi Zefan 			   bool readonly)
499f46b5a66SChristoph Hellwig {
5002e4bfab9SYan, Zheng 	struct inode *inode;
501f46b5a66SChristoph Hellwig 	struct btrfs_pending_snapshot *pending_snapshot;
502f46b5a66SChristoph Hellwig 	struct btrfs_trans_handle *trans;
5032e4bfab9SYan, Zheng 	int ret;
504f46b5a66SChristoph Hellwig 
505f46b5a66SChristoph Hellwig 	if (!root->ref_cows)
506f46b5a66SChristoph Hellwig 		return -EINVAL;
507f46b5a66SChristoph Hellwig 
508a22285a6SYan, Zheng 	pending_snapshot = kzalloc(sizeof(*pending_snapshot), GFP_NOFS);
509a22285a6SYan, Zheng 	if (!pending_snapshot)
510a22285a6SYan, Zheng 		return -ENOMEM;
511a22285a6SYan, Zheng 
512a22285a6SYan, Zheng 	btrfs_init_block_rsv(&pending_snapshot->block_rsv);
513a22285a6SYan, Zheng 	pending_snapshot->dentry = dentry;
514a22285a6SYan, Zheng 	pending_snapshot->root = root;
515b83cc969SLi Zefan 	pending_snapshot->readonly = readonly;
516a22285a6SYan, Zheng 
517a22285a6SYan, Zheng 	trans = btrfs_start_transaction(root->fs_info->extent_root, 5);
518a22285a6SYan, Zheng 	if (IS_ERR(trans)) {
519a22285a6SYan, Zheng 		ret = PTR_ERR(trans);
520a22285a6SYan, Zheng 		goto fail;
521a22285a6SYan, Zheng 	}
522a22285a6SYan, Zheng 
523a22285a6SYan, Zheng 	ret = btrfs_snap_reserve_metadata(trans, pending_snapshot);
524a22285a6SYan, Zheng 	BUG_ON(ret);
525a22285a6SYan, Zheng 
5268351583eSJosef Bacik 	spin_lock(&root->fs_info->trans_lock);
527a22285a6SYan, Zheng 	list_add(&pending_snapshot->list,
528a22285a6SYan, Zheng 		 &trans->transaction->pending_snapshots);
5298351583eSJosef Bacik 	spin_unlock(&root->fs_info->trans_lock);
53072fd032eSSage Weil 	if (async_transid) {
53172fd032eSSage Weil 		*async_transid = trans->transid;
53272fd032eSSage Weil 		ret = btrfs_commit_transaction_async(trans,
53372fd032eSSage Weil 				     root->fs_info->extent_root, 1);
53472fd032eSSage Weil 	} else {
53572fd032eSSage Weil 		ret = btrfs_commit_transaction(trans,
53672fd032eSSage Weil 					       root->fs_info->extent_root);
53772fd032eSSage Weil 	}
538a22285a6SYan, Zheng 	BUG_ON(ret);
539a22285a6SYan, Zheng 
540a22285a6SYan, Zheng 	ret = pending_snapshot->error;
541f46b5a66SChristoph Hellwig 	if (ret)
5422e4bfab9SYan, Zheng 		goto fail;
543f46b5a66SChristoph Hellwig 
54466b4ffd1SJosef Bacik 	ret = btrfs_orphan_cleanup(pending_snapshot->snap);
54566b4ffd1SJosef Bacik 	if (ret)
54666b4ffd1SJosef Bacik 		goto fail;
547f46b5a66SChristoph Hellwig 
5482fbe8c8aSAl Viro 	inode = btrfs_lookup_dentry(dentry->d_parent->d_inode, dentry);
5492e4bfab9SYan, Zheng 	if (IS_ERR(inode)) {
5502e4bfab9SYan, Zheng 		ret = PTR_ERR(inode);
5512e4bfab9SYan, Zheng 		goto fail;
5522e4bfab9SYan, Zheng 	}
5532e4bfab9SYan, Zheng 	BUG_ON(!inode);
5542e4bfab9SYan, Zheng 	d_instantiate(dentry, inode);
5552e4bfab9SYan, Zheng 	ret = 0;
5562e4bfab9SYan, Zheng fail:
557a22285a6SYan, Zheng 	kfree(pending_snapshot);
558f46b5a66SChristoph Hellwig 	return ret;
559f46b5a66SChristoph Hellwig }
560f46b5a66SChristoph Hellwig 
5614260f7c7SSage Weil /*  copy of check_sticky in fs/namei.c()
5624260f7c7SSage Weil * It's inline, so penalty for filesystems that don't use sticky bit is
5634260f7c7SSage Weil * minimal.
5644260f7c7SSage Weil */
5654260f7c7SSage Weil static inline int btrfs_check_sticky(struct inode *dir, struct inode *inode)
5664260f7c7SSage Weil {
5674260f7c7SSage Weil 	uid_t fsuid = current_fsuid();
5684260f7c7SSage Weil 
5694260f7c7SSage Weil 	if (!(dir->i_mode & S_ISVTX))
5704260f7c7SSage Weil 		return 0;
5714260f7c7SSage Weil 	if (inode->i_uid == fsuid)
5724260f7c7SSage Weil 		return 0;
5734260f7c7SSage Weil 	if (dir->i_uid == fsuid)
5744260f7c7SSage Weil 		return 0;
5754260f7c7SSage Weil 	return !capable(CAP_FOWNER);
5764260f7c7SSage Weil }
5774260f7c7SSage Weil 
5784260f7c7SSage Weil /*  copy of may_delete in fs/namei.c()
5794260f7c7SSage Weil  *	Check whether we can remove a link victim from directory dir, check
5804260f7c7SSage Weil  *  whether the type of victim is right.
5814260f7c7SSage Weil  *  1. We can't do it if dir is read-only (done in permission())
5824260f7c7SSage Weil  *  2. We should have write and exec permissions on dir
5834260f7c7SSage Weil  *  3. We can't remove anything from append-only dir
5844260f7c7SSage Weil  *  4. We can't do anything with immutable dir (done in permission())
5854260f7c7SSage Weil  *  5. If the sticky bit on dir is set we should either
5864260f7c7SSage Weil  *	a. be owner of dir, or
5874260f7c7SSage Weil  *	b. be owner of victim, or
5884260f7c7SSage Weil  *	c. have CAP_FOWNER capability
5894260f7c7SSage Weil  *  6. If the victim is append-only or immutable we can't do antyhing with
5904260f7c7SSage Weil  *     links pointing to it.
5914260f7c7SSage Weil  *  7. If we were asked to remove a directory and victim isn't one - ENOTDIR.
5924260f7c7SSage Weil  *  8. If we were asked to remove a non-directory and victim isn't one - EISDIR.
5934260f7c7SSage Weil  *  9. We can't remove a root or mountpoint.
5944260f7c7SSage Weil  * 10. We don't allow removal of NFS sillyrenamed files; it's handled by
5954260f7c7SSage Weil  *     nfs_async_unlink().
5964260f7c7SSage Weil  */
5974260f7c7SSage Weil 
5984260f7c7SSage Weil static int btrfs_may_delete(struct inode *dir,struct dentry *victim,int isdir)
5994260f7c7SSage Weil {
6004260f7c7SSage Weil 	int error;
6014260f7c7SSage Weil 
6024260f7c7SSage Weil 	if (!victim->d_inode)
6034260f7c7SSage Weil 		return -ENOENT;
6044260f7c7SSage Weil 
6054260f7c7SSage Weil 	BUG_ON(victim->d_parent->d_inode != dir);
6064260f7c7SSage Weil 	audit_inode_child(victim, dir);
6074260f7c7SSage Weil 
6084260f7c7SSage Weil 	error = inode_permission(dir, MAY_WRITE | MAY_EXEC);
6094260f7c7SSage Weil 	if (error)
6104260f7c7SSage Weil 		return error;
6114260f7c7SSage Weil 	if (IS_APPEND(dir))
6124260f7c7SSage Weil 		return -EPERM;
6134260f7c7SSage Weil 	if (btrfs_check_sticky(dir, victim->d_inode)||
6144260f7c7SSage Weil 		IS_APPEND(victim->d_inode)||
6154260f7c7SSage Weil 	    IS_IMMUTABLE(victim->d_inode) || IS_SWAPFILE(victim->d_inode))
6164260f7c7SSage Weil 		return -EPERM;
6174260f7c7SSage Weil 	if (isdir) {
6184260f7c7SSage Weil 		if (!S_ISDIR(victim->d_inode->i_mode))
6194260f7c7SSage Weil 			return -ENOTDIR;
6204260f7c7SSage Weil 		if (IS_ROOT(victim))
6214260f7c7SSage Weil 			return -EBUSY;
6224260f7c7SSage Weil 	} else if (S_ISDIR(victim->d_inode->i_mode))
6234260f7c7SSage Weil 		return -EISDIR;
6244260f7c7SSage Weil 	if (IS_DEADDIR(dir))
6254260f7c7SSage Weil 		return -ENOENT;
6264260f7c7SSage Weil 	if (victim->d_flags & DCACHE_NFSFS_RENAMED)
6274260f7c7SSage Weil 		return -EBUSY;
6284260f7c7SSage Weil 	return 0;
6294260f7c7SSage Weil }
6304260f7c7SSage Weil 
631cb8e7090SChristoph Hellwig /* copy of may_create in fs/namei.c() */
632cb8e7090SChristoph Hellwig static inline int btrfs_may_create(struct inode *dir, struct dentry *child)
633cb8e7090SChristoph Hellwig {
634cb8e7090SChristoph Hellwig 	if (child->d_inode)
635cb8e7090SChristoph Hellwig 		return -EEXIST;
636cb8e7090SChristoph Hellwig 	if (IS_DEADDIR(dir))
637cb8e7090SChristoph Hellwig 		return -ENOENT;
638cb8e7090SChristoph Hellwig 	return inode_permission(dir, MAY_WRITE | MAY_EXEC);
639cb8e7090SChristoph Hellwig }
640cb8e7090SChristoph Hellwig 
641cb8e7090SChristoph Hellwig /*
642cb8e7090SChristoph Hellwig  * Create a new subvolume below @parent.  This is largely modeled after
643cb8e7090SChristoph Hellwig  * sys_mkdirat and vfs_mkdir, but we only do a single component lookup
644cb8e7090SChristoph Hellwig  * inside this filesystem so it's quite a bit simpler.
645cb8e7090SChristoph Hellwig  */
64676dda93cSYan, Zheng static noinline int btrfs_mksubvol(struct path *parent,
64776dda93cSYan, Zheng 				   char *name, int namelen,
64872fd032eSSage Weil 				   struct btrfs_root *snap_src,
649b83cc969SLi Zefan 				   u64 *async_transid, bool readonly)
650cb8e7090SChristoph Hellwig {
65176dda93cSYan, Zheng 	struct inode *dir  = parent->dentry->d_inode;
652cb8e7090SChristoph Hellwig 	struct dentry *dentry;
653cb8e7090SChristoph Hellwig 	int error;
654cb8e7090SChristoph Hellwig 
65576dda93cSYan, Zheng 	mutex_lock_nested(&dir->i_mutex, I_MUTEX_PARENT);
656cb8e7090SChristoph Hellwig 
657cb8e7090SChristoph Hellwig 	dentry = lookup_one_len(name, parent->dentry, namelen);
658cb8e7090SChristoph Hellwig 	error = PTR_ERR(dentry);
659cb8e7090SChristoph Hellwig 	if (IS_ERR(dentry))
660cb8e7090SChristoph Hellwig 		goto out_unlock;
661cb8e7090SChristoph Hellwig 
662cb8e7090SChristoph Hellwig 	error = -EEXIST;
663cb8e7090SChristoph Hellwig 	if (dentry->d_inode)
664cb8e7090SChristoph Hellwig 		goto out_dput;
665cb8e7090SChristoph Hellwig 
666cb8e7090SChristoph Hellwig 	error = mnt_want_write(parent->mnt);
667cb8e7090SChristoph Hellwig 	if (error)
668cb8e7090SChristoph Hellwig 		goto out_dput;
669cb8e7090SChristoph Hellwig 
67076dda93cSYan, Zheng 	error = btrfs_may_create(dir, dentry);
671cb8e7090SChristoph Hellwig 	if (error)
672cb8e7090SChristoph Hellwig 		goto out_drop_write;
673cb8e7090SChristoph Hellwig 
67476dda93cSYan, Zheng 	down_read(&BTRFS_I(dir)->root->fs_info->subvol_sem);
67576dda93cSYan, Zheng 
67676dda93cSYan, Zheng 	if (btrfs_root_refs(&BTRFS_I(dir)->root->root_item) == 0)
67776dda93cSYan, Zheng 		goto out_up_read;
67876dda93cSYan, Zheng 
6793de4586cSChris Mason 	if (snap_src) {
68072fd032eSSage Weil 		error = create_snapshot(snap_src, dentry,
681b83cc969SLi Zefan 					name, namelen, async_transid, readonly);
6823de4586cSChris Mason 	} else {
68376dda93cSYan, Zheng 		error = create_subvol(BTRFS_I(dir)->root, dentry,
68472fd032eSSage Weil 				      name, namelen, async_transid);
6853de4586cSChris Mason 	}
68676dda93cSYan, Zheng 	if (!error)
68776dda93cSYan, Zheng 		fsnotify_mkdir(dir, dentry);
68876dda93cSYan, Zheng out_up_read:
68976dda93cSYan, Zheng 	up_read(&BTRFS_I(dir)->root->fs_info->subvol_sem);
690cb8e7090SChristoph Hellwig out_drop_write:
691cb8e7090SChristoph Hellwig 	mnt_drop_write(parent->mnt);
692cb8e7090SChristoph Hellwig out_dput:
693cb8e7090SChristoph Hellwig 	dput(dentry);
694cb8e7090SChristoph Hellwig out_unlock:
69576dda93cSYan, Zheng 	mutex_unlock(&dir->i_mutex);
696cb8e7090SChristoph Hellwig 	return error;
697cb8e7090SChristoph Hellwig }
698cb8e7090SChristoph Hellwig 
6994cb5300bSChris Mason /*
7004cb5300bSChris Mason  * When we're defragging a range, we don't want to kick it off again
7014cb5300bSChris Mason  * if it is really just waiting for delalloc to send it down.
7024cb5300bSChris Mason  * If we find a nice big extent or delalloc range for the bytes in the
7034cb5300bSChris Mason  * file you want to defrag, we return 0 to let you know to skip this
7044cb5300bSChris Mason  * part of the file
7054cb5300bSChris Mason  */
7064cb5300bSChris Mason static int check_defrag_in_cache(struct inode *inode, u64 offset, int thresh)
7074cb5300bSChris Mason {
7084cb5300bSChris Mason 	struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
7094cb5300bSChris Mason 	struct extent_map *em = NULL;
7104cb5300bSChris Mason 	struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
7114cb5300bSChris Mason 	u64 end;
7124cb5300bSChris Mason 
7134cb5300bSChris Mason 	read_lock(&em_tree->lock);
7144cb5300bSChris Mason 	em = lookup_extent_mapping(em_tree, offset, PAGE_CACHE_SIZE);
7154cb5300bSChris Mason 	read_unlock(&em_tree->lock);
7164cb5300bSChris Mason 
7174cb5300bSChris Mason 	if (em) {
7184cb5300bSChris Mason 		end = extent_map_end(em);
7194cb5300bSChris Mason 		free_extent_map(em);
7204cb5300bSChris Mason 		if (end - offset > thresh)
7214cb5300bSChris Mason 			return 0;
7224cb5300bSChris Mason 	}
7234cb5300bSChris Mason 	/* if we already have a nice delalloc here, just stop */
7244cb5300bSChris Mason 	thresh /= 2;
7254cb5300bSChris Mason 	end = count_range_bits(io_tree, &offset, offset + thresh,
7264cb5300bSChris Mason 			       thresh, EXTENT_DELALLOC, 1);
7274cb5300bSChris Mason 	if (end >= thresh)
7284cb5300bSChris Mason 		return 0;
7294cb5300bSChris Mason 	return 1;
7304cb5300bSChris Mason }
7314cb5300bSChris Mason 
7324cb5300bSChris Mason /*
7334cb5300bSChris Mason  * helper function to walk through a file and find extents
7344cb5300bSChris Mason  * newer than a specific transid, and smaller than thresh.
7354cb5300bSChris Mason  *
7364cb5300bSChris Mason  * This is used by the defragging code to find new and small
7374cb5300bSChris Mason  * extents
7384cb5300bSChris Mason  */
7394cb5300bSChris Mason static int find_new_extents(struct btrfs_root *root,
7404cb5300bSChris Mason 			    struct inode *inode, u64 newer_than,
7414cb5300bSChris Mason 			    u64 *off, int thresh)
7424cb5300bSChris Mason {
7434cb5300bSChris Mason 	struct btrfs_path *path;
7444cb5300bSChris Mason 	struct btrfs_key min_key;
7454cb5300bSChris Mason 	struct btrfs_key max_key;
7464cb5300bSChris Mason 	struct extent_buffer *leaf;
7474cb5300bSChris Mason 	struct btrfs_file_extent_item *extent;
7484cb5300bSChris Mason 	int type;
7494cb5300bSChris Mason 	int ret;
750a4689d2bSDavid Sterba 	u64 ino = btrfs_ino(inode);
7514cb5300bSChris Mason 
7524cb5300bSChris Mason 	path = btrfs_alloc_path();
7534cb5300bSChris Mason 	if (!path)
7544cb5300bSChris Mason 		return -ENOMEM;
7554cb5300bSChris Mason 
756a4689d2bSDavid Sterba 	min_key.objectid = ino;
7574cb5300bSChris Mason 	min_key.type = BTRFS_EXTENT_DATA_KEY;
7584cb5300bSChris Mason 	min_key.offset = *off;
7594cb5300bSChris Mason 
760a4689d2bSDavid Sterba 	max_key.objectid = ino;
7614cb5300bSChris Mason 	max_key.type = (u8)-1;
7624cb5300bSChris Mason 	max_key.offset = (u64)-1;
7634cb5300bSChris Mason 
7644cb5300bSChris Mason 	path->keep_locks = 1;
7654cb5300bSChris Mason 
7664cb5300bSChris Mason 	while(1) {
7674cb5300bSChris Mason 		ret = btrfs_search_forward(root, &min_key, &max_key,
7684cb5300bSChris Mason 					   path, 0, newer_than);
7694cb5300bSChris Mason 		if (ret != 0)
7704cb5300bSChris Mason 			goto none;
771a4689d2bSDavid Sterba 		if (min_key.objectid != ino)
7724cb5300bSChris Mason 			goto none;
7734cb5300bSChris Mason 		if (min_key.type != BTRFS_EXTENT_DATA_KEY)
7744cb5300bSChris Mason 			goto none;
7754cb5300bSChris Mason 
7764cb5300bSChris Mason 		leaf = path->nodes[0];
7774cb5300bSChris Mason 		extent = btrfs_item_ptr(leaf, path->slots[0],
7784cb5300bSChris Mason 					struct btrfs_file_extent_item);
7794cb5300bSChris Mason 
7804cb5300bSChris Mason 		type = btrfs_file_extent_type(leaf, extent);
7814cb5300bSChris Mason 		if (type == BTRFS_FILE_EXTENT_REG &&
7824cb5300bSChris Mason 		    btrfs_file_extent_num_bytes(leaf, extent) < thresh &&
7834cb5300bSChris Mason 		    check_defrag_in_cache(inode, min_key.offset, thresh)) {
7844cb5300bSChris Mason 			*off = min_key.offset;
7854cb5300bSChris Mason 			btrfs_free_path(path);
7864cb5300bSChris Mason 			return 0;
7874cb5300bSChris Mason 		}
7884cb5300bSChris Mason 
7894cb5300bSChris Mason 		if (min_key.offset == (u64)-1)
7904cb5300bSChris Mason 			goto none;
7914cb5300bSChris Mason 
7924cb5300bSChris Mason 		min_key.offset++;
7934cb5300bSChris Mason 		btrfs_release_path(path);
7944cb5300bSChris Mason 	}
7954cb5300bSChris Mason none:
7964cb5300bSChris Mason 	btrfs_free_path(path);
7974cb5300bSChris Mason 	return -ENOENT;
7984cb5300bSChris Mason }
7994cb5300bSChris Mason 
8006c282eb4SLi Zefan static struct extent_map *defrag_lookup_extent(struct inode *inode, u64 start)
80117ce6ef8SLiu Bo {
80217ce6ef8SLiu Bo 	struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
803940100a4SChris Mason 	struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
8046c282eb4SLi Zefan 	struct extent_map *em;
8056c282eb4SLi Zefan 	u64 len = PAGE_CACHE_SIZE;
806940100a4SChris Mason 
807940100a4SChris Mason 	/*
808940100a4SChris Mason 	 * hopefully we have this extent in the tree already, try without
809940100a4SChris Mason 	 * the full extent lock
810940100a4SChris Mason 	 */
811940100a4SChris Mason 	read_lock(&em_tree->lock);
812940100a4SChris Mason 	em = lookup_extent_mapping(em_tree, start, len);
813940100a4SChris Mason 	read_unlock(&em_tree->lock);
814940100a4SChris Mason 
815940100a4SChris Mason 	if (!em) {
816940100a4SChris Mason 		/* get the big lock and read metadata off disk */
817d0082371SJeff Mahoney 		lock_extent(io_tree, start, start + len - 1);
818940100a4SChris Mason 		em = btrfs_get_extent(inode, NULL, 0, start, len, 0);
819d0082371SJeff Mahoney 		unlock_extent(io_tree, start, start + len - 1);
820940100a4SChris Mason 
8216cf8bfbfSDan Carpenter 		if (IS_ERR(em))
8226c282eb4SLi Zefan 			return NULL;
823940100a4SChris Mason 	}
824940100a4SChris Mason 
8256c282eb4SLi Zefan 	return em;
8266c282eb4SLi Zefan }
8276c282eb4SLi Zefan 
8286c282eb4SLi Zefan static bool defrag_check_next_extent(struct inode *inode, struct extent_map *em)
8296c282eb4SLi Zefan {
8306c282eb4SLi Zefan 	struct extent_map *next;
8316c282eb4SLi Zefan 	bool ret = true;
8326c282eb4SLi Zefan 
8336c282eb4SLi Zefan 	/* this is the last extent */
8346c282eb4SLi Zefan 	if (em->start + em->len >= i_size_read(inode))
8356c282eb4SLi Zefan 		return false;
8366c282eb4SLi Zefan 
8376c282eb4SLi Zefan 	next = defrag_lookup_extent(inode, em->start + em->len);
8386c282eb4SLi Zefan 	if (!next || next->block_start >= EXTENT_MAP_LAST_BYTE)
8396c282eb4SLi Zefan 		ret = false;
8406c282eb4SLi Zefan 
8416c282eb4SLi Zefan 	free_extent_map(next);
8426c282eb4SLi Zefan 	return ret;
8436c282eb4SLi Zefan }
8446c282eb4SLi Zefan 
8456c282eb4SLi Zefan static int should_defrag_range(struct inode *inode, u64 start, int thresh,
8466c282eb4SLi Zefan 			       u64 *last_len, u64 *skip, u64 *defrag_end)
8476c282eb4SLi Zefan {
8486c282eb4SLi Zefan 	struct extent_map *em;
8496c282eb4SLi Zefan 	int ret = 1;
8506c282eb4SLi Zefan 	bool next_mergeable = true;
8516c282eb4SLi Zefan 
8526c282eb4SLi Zefan 	/*
8536c282eb4SLi Zefan 	 * make sure that once we start defragging an extent, we keep on
8546c282eb4SLi Zefan 	 * defragging it
8556c282eb4SLi Zefan 	 */
8566c282eb4SLi Zefan 	if (start < *defrag_end)
8576c282eb4SLi Zefan 		return 1;
8586c282eb4SLi Zefan 
8596c282eb4SLi Zefan 	*skip = 0;
8606c282eb4SLi Zefan 
8616c282eb4SLi Zefan 	em = defrag_lookup_extent(inode, start);
8626c282eb4SLi Zefan 	if (!em)
8636c282eb4SLi Zefan 		return 0;
8646c282eb4SLi Zefan 
865940100a4SChris Mason 	/* this will cover holes, and inline extents */
86617ce6ef8SLiu Bo 	if (em->block_start >= EXTENT_MAP_LAST_BYTE) {
867940100a4SChris Mason 		ret = 0;
86817ce6ef8SLiu Bo 		goto out;
86917ce6ef8SLiu Bo 	}
87017ce6ef8SLiu Bo 
8716c282eb4SLi Zefan 	next_mergeable = defrag_check_next_extent(inode, em);
872940100a4SChris Mason 
873940100a4SChris Mason 	/*
8746c282eb4SLi Zefan 	 * we hit a real extent, if it is big or the next extent is not a
8756c282eb4SLi Zefan 	 * real extent, don't bother defragging it
876940100a4SChris Mason 	 */
8776c282eb4SLi Zefan 	if ((*last_len == 0 || *last_len >= thresh) &&
8786c282eb4SLi Zefan 	    (em->len >= thresh || !next_mergeable))
879940100a4SChris Mason 		ret = 0;
88017ce6ef8SLiu Bo out:
881940100a4SChris Mason 	/*
882940100a4SChris Mason 	 * last_len ends up being a counter of how many bytes we've defragged.
883940100a4SChris Mason 	 * every time we choose not to defrag an extent, we reset *last_len
884940100a4SChris Mason 	 * so that the next tiny extent will force a defrag.
885940100a4SChris Mason 	 *
886940100a4SChris Mason 	 * The end result of this is that tiny extents before a single big
887940100a4SChris Mason 	 * extent will force at least part of that big extent to be defragged.
888940100a4SChris Mason 	 */
889940100a4SChris Mason 	if (ret) {
890940100a4SChris Mason 		*defrag_end = extent_map_end(em);
891940100a4SChris Mason 	} else {
892940100a4SChris Mason 		*last_len = 0;
893940100a4SChris Mason 		*skip = extent_map_end(em);
894940100a4SChris Mason 		*defrag_end = 0;
895940100a4SChris Mason 	}
896940100a4SChris Mason 
897940100a4SChris Mason 	free_extent_map(em);
898940100a4SChris Mason 	return ret;
899940100a4SChris Mason }
900940100a4SChris Mason 
9014cb5300bSChris Mason /*
9024cb5300bSChris Mason  * it doesn't do much good to defrag one or two pages
9034cb5300bSChris Mason  * at a time.  This pulls in a nice chunk of pages
9044cb5300bSChris Mason  * to COW and defrag.
9054cb5300bSChris Mason  *
9064cb5300bSChris Mason  * It also makes sure the delalloc code has enough
9074cb5300bSChris Mason  * dirty data to avoid making new small extents as part
9084cb5300bSChris Mason  * of the defrag
9094cb5300bSChris Mason  *
9104cb5300bSChris Mason  * It's a good idea to start RA on this range
9114cb5300bSChris Mason  * before calling this.
9124cb5300bSChris Mason  */
9134cb5300bSChris Mason static int cluster_pages_for_defrag(struct inode *inode,
9144cb5300bSChris Mason 				    struct page **pages,
9154cb5300bSChris Mason 				    unsigned long start_index,
9164cb5300bSChris Mason 				    int num_pages)
917f46b5a66SChristoph Hellwig {
9184cb5300bSChris Mason 	unsigned long file_end;
9194cb5300bSChris Mason 	u64 isize = i_size_read(inode);
920f46b5a66SChristoph Hellwig 	u64 page_start;
921f46b5a66SChristoph Hellwig 	u64 page_end;
9221f12bd06SLiu Bo 	u64 page_cnt;
9234cb5300bSChris Mason 	int ret;
9244cb5300bSChris Mason 	int i;
9254cb5300bSChris Mason 	int i_done;
9264cb5300bSChris Mason 	struct btrfs_ordered_extent *ordered;
9274cb5300bSChris Mason 	struct extent_state *cached_state = NULL;
928600a45e1SMiao Xie 	struct extent_io_tree *tree;
9293b16a4e3SJosef Bacik 	gfp_t mask = btrfs_alloc_write_mask(inode->i_mapping);
9304cb5300bSChris Mason 
9314cb5300bSChris Mason 	file_end = (isize - 1) >> PAGE_CACHE_SHIFT;
9321f12bd06SLiu Bo 	if (!isize || start_index > file_end)
9331f12bd06SLiu Bo 		return 0;
9341f12bd06SLiu Bo 
9351f12bd06SLiu Bo 	page_cnt = min_t(u64, (u64)num_pages, (u64)file_end - start_index + 1);
9364cb5300bSChris Mason 
9374cb5300bSChris Mason 	ret = btrfs_delalloc_reserve_space(inode,
9381f12bd06SLiu Bo 					   page_cnt << PAGE_CACHE_SHIFT);
9394cb5300bSChris Mason 	if (ret)
9404cb5300bSChris Mason 		return ret;
9414cb5300bSChris Mason 	i_done = 0;
942600a45e1SMiao Xie 	tree = &BTRFS_I(inode)->io_tree;
9434cb5300bSChris Mason 
9444cb5300bSChris Mason 	/* step one, lock all the pages */
9451f12bd06SLiu Bo 	for (i = 0; i < page_cnt; i++) {
9464cb5300bSChris Mason 		struct page *page;
947600a45e1SMiao Xie again:
948a94733d0SJosef Bacik 		page = find_or_create_page(inode->i_mapping,
9493b16a4e3SJosef Bacik 					   start_index + i, mask);
9504cb5300bSChris Mason 		if (!page)
9514cb5300bSChris Mason 			break;
9524cb5300bSChris Mason 
953600a45e1SMiao Xie 		page_start = page_offset(page);
954600a45e1SMiao Xie 		page_end = page_start + PAGE_CACHE_SIZE - 1;
955600a45e1SMiao Xie 		while (1) {
956d0082371SJeff Mahoney 			lock_extent(tree, page_start, page_end);
957600a45e1SMiao Xie 			ordered = btrfs_lookup_ordered_extent(inode,
958600a45e1SMiao Xie 							      page_start);
959d0082371SJeff Mahoney 			unlock_extent(tree, page_start, page_end);
960600a45e1SMiao Xie 			if (!ordered)
961600a45e1SMiao Xie 				break;
962600a45e1SMiao Xie 
963600a45e1SMiao Xie 			unlock_page(page);
964600a45e1SMiao Xie 			btrfs_start_ordered_extent(inode, ordered, 1);
965600a45e1SMiao Xie 			btrfs_put_ordered_extent(ordered);
966600a45e1SMiao Xie 			lock_page(page);
9671f12bd06SLiu Bo 			/*
9681f12bd06SLiu Bo 			 * we unlocked the page above, so we need check if
9691f12bd06SLiu Bo 			 * it was released or not.
9701f12bd06SLiu Bo 			 */
9711f12bd06SLiu Bo 			if (page->mapping != inode->i_mapping) {
9721f12bd06SLiu Bo 				unlock_page(page);
9731f12bd06SLiu Bo 				page_cache_release(page);
9741f12bd06SLiu Bo 				goto again;
9751f12bd06SLiu Bo 			}
976600a45e1SMiao Xie 		}
977600a45e1SMiao Xie 
9784cb5300bSChris Mason 		if (!PageUptodate(page)) {
9794cb5300bSChris Mason 			btrfs_readpage(NULL, page);
9804cb5300bSChris Mason 			lock_page(page);
9814cb5300bSChris Mason 			if (!PageUptodate(page)) {
9824cb5300bSChris Mason 				unlock_page(page);
9834cb5300bSChris Mason 				page_cache_release(page);
9844cb5300bSChris Mason 				ret = -EIO;
9854cb5300bSChris Mason 				break;
9864cb5300bSChris Mason 			}
9874cb5300bSChris Mason 		}
988600a45e1SMiao Xie 
989600a45e1SMiao Xie 		if (page->mapping != inode->i_mapping) {
990600a45e1SMiao Xie 			unlock_page(page);
991600a45e1SMiao Xie 			page_cache_release(page);
992600a45e1SMiao Xie 			goto again;
993600a45e1SMiao Xie 		}
994600a45e1SMiao Xie 
9954cb5300bSChris Mason 		pages[i] = page;
9964cb5300bSChris Mason 		i_done++;
9974cb5300bSChris Mason 	}
9984cb5300bSChris Mason 	if (!i_done || ret)
9994cb5300bSChris Mason 		goto out;
10004cb5300bSChris Mason 
10014cb5300bSChris Mason 	if (!(inode->i_sb->s_flags & MS_ACTIVE))
10024cb5300bSChris Mason 		goto out;
10034cb5300bSChris Mason 
10044cb5300bSChris Mason 	/*
10054cb5300bSChris Mason 	 * so now we have a nice long stream of locked
10064cb5300bSChris Mason 	 * and up to date pages, lets wait on them
10074cb5300bSChris Mason 	 */
10084cb5300bSChris Mason 	for (i = 0; i < i_done; i++)
10094cb5300bSChris Mason 		wait_on_page_writeback(pages[i]);
10104cb5300bSChris Mason 
10114cb5300bSChris Mason 	page_start = page_offset(pages[0]);
10124cb5300bSChris Mason 	page_end = page_offset(pages[i_done - 1]) + PAGE_CACHE_SIZE;
10134cb5300bSChris Mason 
10144cb5300bSChris Mason 	lock_extent_bits(&BTRFS_I(inode)->io_tree,
1015d0082371SJeff Mahoney 			 page_start, page_end - 1, 0, &cached_state);
10164cb5300bSChris Mason 	clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start,
10174cb5300bSChris Mason 			  page_end - 1, EXTENT_DIRTY | EXTENT_DELALLOC |
10184cb5300bSChris Mason 			  EXTENT_DO_ACCOUNTING, 0, 0, &cached_state,
10194cb5300bSChris Mason 			  GFP_NOFS);
10204cb5300bSChris Mason 
10211f12bd06SLiu Bo 	if (i_done != page_cnt) {
10229e0baf60SJosef Bacik 		spin_lock(&BTRFS_I(inode)->lock);
10239e0baf60SJosef Bacik 		BTRFS_I(inode)->outstanding_extents++;
10249e0baf60SJosef Bacik 		spin_unlock(&BTRFS_I(inode)->lock);
10254cb5300bSChris Mason 		btrfs_delalloc_release_space(inode,
10261f12bd06SLiu Bo 				     (page_cnt - i_done) << PAGE_CACHE_SHIFT);
10274cb5300bSChris Mason 	}
10284cb5300bSChris Mason 
10294cb5300bSChris Mason 
10304cb5300bSChris Mason 	btrfs_set_extent_delalloc(inode, page_start, page_end - 1,
10314cb5300bSChris Mason 				  &cached_state);
10324cb5300bSChris Mason 
10334cb5300bSChris Mason 	unlock_extent_cached(&BTRFS_I(inode)->io_tree,
10344cb5300bSChris Mason 			     page_start, page_end - 1, &cached_state,
10354cb5300bSChris Mason 			     GFP_NOFS);
10364cb5300bSChris Mason 
10374cb5300bSChris Mason 	for (i = 0; i < i_done; i++) {
10384cb5300bSChris Mason 		clear_page_dirty_for_io(pages[i]);
10394cb5300bSChris Mason 		ClearPageChecked(pages[i]);
10404cb5300bSChris Mason 		set_page_extent_mapped(pages[i]);
10414cb5300bSChris Mason 		set_page_dirty(pages[i]);
10424cb5300bSChris Mason 		unlock_page(pages[i]);
10434cb5300bSChris Mason 		page_cache_release(pages[i]);
10444cb5300bSChris Mason 	}
10454cb5300bSChris Mason 	return i_done;
10464cb5300bSChris Mason out:
10474cb5300bSChris Mason 	for (i = 0; i < i_done; i++) {
10484cb5300bSChris Mason 		unlock_page(pages[i]);
10494cb5300bSChris Mason 		page_cache_release(pages[i]);
10504cb5300bSChris Mason 	}
10511f12bd06SLiu Bo 	btrfs_delalloc_release_space(inode, page_cnt << PAGE_CACHE_SHIFT);
10524cb5300bSChris Mason 	return ret;
10534cb5300bSChris Mason 
10544cb5300bSChris Mason }
10554cb5300bSChris Mason 
10564cb5300bSChris Mason int btrfs_defrag_file(struct inode *inode, struct file *file,
10574cb5300bSChris Mason 		      struct btrfs_ioctl_defrag_range_args *range,
10584cb5300bSChris Mason 		      u64 newer_than, unsigned long max_to_defrag)
10594cb5300bSChris Mason {
10604cb5300bSChris Mason 	struct btrfs_root *root = BTRFS_I(inode)->root;
10614cb5300bSChris Mason 	struct btrfs_super_block *disk_super;
10624cb5300bSChris Mason 	struct file_ra_state *ra = NULL;
10634cb5300bSChris Mason 	unsigned long last_index;
1064151a31b2SLi Zefan 	u64 isize = i_size_read(inode);
10654cb5300bSChris Mason 	u64 features;
1066940100a4SChris Mason 	u64 last_len = 0;
1067940100a4SChris Mason 	u64 skip = 0;
1068940100a4SChris Mason 	u64 defrag_end = 0;
10694cb5300bSChris Mason 	u64 newer_off = range->start;
1070f46b5a66SChristoph Hellwig 	unsigned long i;
1071008873eaSLi Zefan 	unsigned long ra_index = 0;
1072f46b5a66SChristoph Hellwig 	int ret;
10734cb5300bSChris Mason 	int defrag_count = 0;
10741a419d85SLi Zefan 	int compress_type = BTRFS_COMPRESS_ZLIB;
10754cb5300bSChris Mason 	int extent_thresh = range->extent_thresh;
1076008873eaSLi Zefan 	int max_cluster = (256 * 1024) >> PAGE_CACHE_SHIFT;
1077008873eaSLi Zefan 	int cluster = max_cluster;
10784cb5300bSChris Mason 	u64 new_align = ~((u64)128 * 1024 - 1);
10794cb5300bSChris Mason 	struct page **pages = NULL;
10804cb5300bSChris Mason 
10814cb5300bSChris Mason 	if (extent_thresh == 0)
10824cb5300bSChris Mason 		extent_thresh = 256 * 1024;
10831a419d85SLi Zefan 
10841a419d85SLi Zefan 	if (range->flags & BTRFS_DEFRAG_RANGE_COMPRESS) {
10851a419d85SLi Zefan 		if (range->compress_type > BTRFS_COMPRESS_TYPES)
10861a419d85SLi Zefan 			return -EINVAL;
10871a419d85SLi Zefan 		if (range->compress_type)
10881a419d85SLi Zefan 			compress_type = range->compress_type;
10891a419d85SLi Zefan 	}
1090f46b5a66SChristoph Hellwig 
1091151a31b2SLi Zefan 	if (isize == 0)
1092940100a4SChris Mason 		return 0;
1093f46b5a66SChristoph Hellwig 
10944cb5300bSChris Mason 	/*
10954cb5300bSChris Mason 	 * if we were not given a file, allocate a readahead
10964cb5300bSChris Mason 	 * context
10974cb5300bSChris Mason 	 */
10984cb5300bSChris Mason 	if (!file) {
10994cb5300bSChris Mason 		ra = kzalloc(sizeof(*ra), GFP_NOFS);
11004cb5300bSChris Mason 		if (!ra)
11014cb5300bSChris Mason 			return -ENOMEM;
11024cb5300bSChris Mason 		file_ra_state_init(ra, inode->i_mapping);
11034cb5300bSChris Mason 	} else {
11044cb5300bSChris Mason 		ra = &file->f_ra;
11054cb5300bSChris Mason 	}
11064cb5300bSChris Mason 
1107008873eaSLi Zefan 	pages = kmalloc(sizeof(struct page *) * max_cluster,
11084cb5300bSChris Mason 			GFP_NOFS);
11094cb5300bSChris Mason 	if (!pages) {
11104cb5300bSChris Mason 		ret = -ENOMEM;
11114cb5300bSChris Mason 		goto out_ra;
11124cb5300bSChris Mason 	}
11134cb5300bSChris Mason 
11144cb5300bSChris Mason 	/* find the last page to defrag */
11151e701a32SChris Mason 	if (range->start + range->len > range->start) {
1116151a31b2SLi Zefan 		last_index = min_t(u64, isize - 1,
11171e701a32SChris Mason 			 range->start + range->len - 1) >> PAGE_CACHE_SHIFT;
11181e701a32SChris Mason 	} else {
1119151a31b2SLi Zefan 		last_index = (isize - 1) >> PAGE_CACHE_SHIFT;
11201e701a32SChris Mason 	}
11211e701a32SChris Mason 
11224cb5300bSChris Mason 	if (newer_than) {
11234cb5300bSChris Mason 		ret = find_new_extents(root, inode, newer_than,
11244cb5300bSChris Mason 				       &newer_off, 64 * 1024);
11254cb5300bSChris Mason 		if (!ret) {
11264cb5300bSChris Mason 			range->start = newer_off;
11274cb5300bSChris Mason 			/*
11284cb5300bSChris Mason 			 * we always align our defrag to help keep
11294cb5300bSChris Mason 			 * the extents in the file evenly spaced
11304cb5300bSChris Mason 			 */
11314cb5300bSChris Mason 			i = (newer_off & new_align) >> PAGE_CACHE_SHIFT;
11324cb5300bSChris Mason 		} else
11334cb5300bSChris Mason 			goto out_ra;
11344cb5300bSChris Mason 	} else {
11351e701a32SChris Mason 		i = range->start >> PAGE_CACHE_SHIFT;
11364cb5300bSChris Mason 	}
11374cb5300bSChris Mason 	if (!max_to_defrag)
11387ec31b54SLiu Bo 		max_to_defrag = last_index + 1;
11394cb5300bSChris Mason 
11402a0f7f57SLi Zefan 	/*
11412a0f7f57SLi Zefan 	 * make writeback starts from i, so the defrag range can be
11422a0f7f57SLi Zefan 	 * written sequentially.
11432a0f7f57SLi Zefan 	 */
11442a0f7f57SLi Zefan 	if (i < inode->i_mapping->writeback_index)
11452a0f7f57SLi Zefan 		inode->i_mapping->writeback_index = i;
11462a0f7f57SLi Zefan 
1147f7f43cc8SChris Mason 	while (i <= last_index && defrag_count < max_to_defrag &&
1148f7f43cc8SChris Mason 	       (i < (i_size_read(inode) + PAGE_CACHE_SIZE - 1) >>
1149f7f43cc8SChris Mason 		PAGE_CACHE_SHIFT)) {
11504cb5300bSChris Mason 		/*
11514cb5300bSChris Mason 		 * make sure we stop running if someone unmounts
11524cb5300bSChris Mason 		 * the FS
11534cb5300bSChris Mason 		 */
11544cb5300bSChris Mason 		if (!(inode->i_sb->s_flags & MS_ACTIVE))
11554cb5300bSChris Mason 			break;
11564cb5300bSChris Mason 
115766c26892SLiu Bo 		if (!should_defrag_range(inode, (u64)i << PAGE_CACHE_SHIFT,
11586c282eb4SLi Zefan 					 extent_thresh, &last_len, &skip,
11596c282eb4SLi Zefan 					 &defrag_end)) {
1160940100a4SChris Mason 			unsigned long next;
1161940100a4SChris Mason 			/*
1162940100a4SChris Mason 			 * the should_defrag function tells us how much to skip
1163940100a4SChris Mason 			 * bump our counter by the suggested amount
1164940100a4SChris Mason 			 */
1165940100a4SChris Mason 			next = (skip + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
1166940100a4SChris Mason 			i = max(i + 1, next);
1167940100a4SChris Mason 			continue;
1168940100a4SChris Mason 		}
1169008873eaSLi Zefan 
1170008873eaSLi Zefan 		if (!newer_than) {
1171008873eaSLi Zefan 			cluster = (PAGE_CACHE_ALIGN(defrag_end) >>
1172008873eaSLi Zefan 				   PAGE_CACHE_SHIFT) - i;
1173008873eaSLi Zefan 			cluster = min(cluster, max_cluster);
1174008873eaSLi Zefan 		} else {
1175008873eaSLi Zefan 			cluster = max_cluster;
1176008873eaSLi Zefan 		}
1177008873eaSLi Zefan 
11781e701a32SChris Mason 		if (range->flags & BTRFS_DEFRAG_RANGE_COMPRESS)
11791a419d85SLi Zefan 			BTRFS_I(inode)->force_compress = compress_type;
1180940100a4SChris Mason 
1181008873eaSLi Zefan 		if (i + cluster > ra_index) {
1182008873eaSLi Zefan 			ra_index = max(i, ra_index);
1183008873eaSLi Zefan 			btrfs_force_ra(inode->i_mapping, ra, file, ra_index,
1184008873eaSLi Zefan 				       cluster);
1185008873eaSLi Zefan 			ra_index += max_cluster;
1186008873eaSLi Zefan 		}
11874cb5300bSChris Mason 
1188ecb8bea8SLiu Bo 		mutex_lock(&inode->i_mutex);
1189008873eaSLi Zefan 		ret = cluster_pages_for_defrag(inode, pages, i, cluster);
1190ecb8bea8SLiu Bo 		if (ret < 0) {
1191ecb8bea8SLiu Bo 			mutex_unlock(&inode->i_mutex);
11924cb5300bSChris Mason 			goto out_ra;
1193ecb8bea8SLiu Bo 		}
11944cb5300bSChris Mason 
11954cb5300bSChris Mason 		defrag_count += ret;
11964cb5300bSChris Mason 		balance_dirty_pages_ratelimited_nr(inode->i_mapping, ret);
1197ecb8bea8SLiu Bo 		mutex_unlock(&inode->i_mutex);
11984cb5300bSChris Mason 
11994cb5300bSChris Mason 		if (newer_than) {
12004cb5300bSChris Mason 			if (newer_off == (u64)-1)
12014cb5300bSChris Mason 				break;
12024cb5300bSChris Mason 
1203e1f041e1SLiu Bo 			if (ret > 0)
1204e1f041e1SLiu Bo 				i += ret;
1205e1f041e1SLiu Bo 
12064cb5300bSChris Mason 			newer_off = max(newer_off + 1,
12074cb5300bSChris Mason 					(u64)i << PAGE_CACHE_SHIFT);
12084cb5300bSChris Mason 
12094cb5300bSChris Mason 			ret = find_new_extents(root, inode,
12104cb5300bSChris Mason 					       newer_than, &newer_off,
12114cb5300bSChris Mason 					       64 * 1024);
12124cb5300bSChris Mason 			if (!ret) {
12134cb5300bSChris Mason 				range->start = newer_off;
12144cb5300bSChris Mason 				i = (newer_off & new_align) >> PAGE_CACHE_SHIFT;
12154cb5300bSChris Mason 			} else {
12164cb5300bSChris Mason 				break;
1217940100a4SChris Mason 			}
12184cb5300bSChris Mason 		} else {
1219008873eaSLi Zefan 			if (ret > 0) {
1220cbcc8326SLi Zefan 				i += ret;
1221008873eaSLi Zefan 				last_len += ret << PAGE_CACHE_SHIFT;
1222008873eaSLi Zefan 			} else {
1223940100a4SChris Mason 				i++;
1224008873eaSLi Zefan 				last_len = 0;
1225008873eaSLi Zefan 			}
1226f46b5a66SChristoph Hellwig 		}
12274cb5300bSChris Mason 	}
1228f46b5a66SChristoph Hellwig 
12291e701a32SChris Mason 	if ((range->flags & BTRFS_DEFRAG_RANGE_START_IO))
12301e701a32SChris Mason 		filemap_flush(inode->i_mapping);
12311e701a32SChris Mason 
12321e701a32SChris Mason 	if ((range->flags & BTRFS_DEFRAG_RANGE_COMPRESS)) {
12331e701a32SChris Mason 		/* the filemap_flush will queue IO into the worker threads, but
12341e701a32SChris Mason 		 * we have to make sure the IO is actually started and that
12351e701a32SChris Mason 		 * ordered extents get created before we return
12361e701a32SChris Mason 		 */
12371e701a32SChris Mason 		atomic_inc(&root->fs_info->async_submit_draining);
12381e701a32SChris Mason 		while (atomic_read(&root->fs_info->nr_async_submits) ||
12391e701a32SChris Mason 		      atomic_read(&root->fs_info->async_delalloc_pages)) {
12401e701a32SChris Mason 			wait_event(root->fs_info->async_submit_wait,
12411e701a32SChris Mason 			   (atomic_read(&root->fs_info->nr_async_submits) == 0 &&
12421e701a32SChris Mason 			    atomic_read(&root->fs_info->async_delalloc_pages) == 0));
12431e701a32SChris Mason 		}
12441e701a32SChris Mason 		atomic_dec(&root->fs_info->async_submit_draining);
12451e701a32SChris Mason 
12461e701a32SChris Mason 		mutex_lock(&inode->i_mutex);
1247261507a0SLi Zefan 		BTRFS_I(inode)->force_compress = BTRFS_COMPRESS_NONE;
12481e701a32SChris Mason 		mutex_unlock(&inode->i_mutex);
12491e701a32SChris Mason 	}
12501e701a32SChris Mason 
12516c41761fSDavid Sterba 	disk_super = root->fs_info->super_copy;
12521a419d85SLi Zefan 	features = btrfs_super_incompat_flags(disk_super);
12531a419d85SLi Zefan 	if (range->compress_type == BTRFS_COMPRESS_LZO) {
12541a419d85SLi Zefan 		features |= BTRFS_FEATURE_INCOMPAT_COMPRESS_LZO;
12551a419d85SLi Zefan 		btrfs_set_super_incompat_flags(disk_super, features);
12561a419d85SLi Zefan 	}
12571a419d85SLi Zefan 
125860ccf82fSDiego Calleja 	ret = defrag_count;
1259940100a4SChris Mason 
12604cb5300bSChris Mason out_ra:
12614cb5300bSChris Mason 	if (!file)
12624cb5300bSChris Mason 		kfree(ra);
12634cb5300bSChris Mason 	kfree(pages);
1264940100a4SChris Mason 	return ret;
1265f46b5a66SChristoph Hellwig }
1266f46b5a66SChristoph Hellwig 
126776dda93cSYan, Zheng static noinline int btrfs_ioctl_resize(struct btrfs_root *root,
126876dda93cSYan, Zheng 					void __user *arg)
1269f46b5a66SChristoph Hellwig {
1270f46b5a66SChristoph Hellwig 	u64 new_size;
1271f46b5a66SChristoph Hellwig 	u64 old_size;
1272f46b5a66SChristoph Hellwig 	u64 devid = 1;
1273f46b5a66SChristoph Hellwig 	struct btrfs_ioctl_vol_args *vol_args;
1274f46b5a66SChristoph Hellwig 	struct btrfs_trans_handle *trans;
1275f46b5a66SChristoph Hellwig 	struct btrfs_device *device = NULL;
1276f46b5a66SChristoph Hellwig 	char *sizestr;
1277f46b5a66SChristoph Hellwig 	char *devstr = NULL;
1278f46b5a66SChristoph Hellwig 	int ret = 0;
1279f46b5a66SChristoph Hellwig 	int mod = 0;
1280f46b5a66SChristoph Hellwig 
1281c146afadSYan Zheng 	if (root->fs_info->sb->s_flags & MS_RDONLY)
1282c146afadSYan Zheng 		return -EROFS;
1283c146afadSYan Zheng 
1284e441d54dSChris Mason 	if (!capable(CAP_SYS_ADMIN))
1285e441d54dSChris Mason 		return -EPERM;
1286e441d54dSChris Mason 
1287c9e9f97bSIlya Dryomov 	mutex_lock(&root->fs_info->volume_mutex);
1288c9e9f97bSIlya Dryomov 	if (root->fs_info->balance_ctl) {
1289c9e9f97bSIlya Dryomov 		printk(KERN_INFO "btrfs: balance in progress\n");
1290c9e9f97bSIlya Dryomov 		ret = -EINVAL;
1291c9e9f97bSIlya Dryomov 		goto out;
1292c9e9f97bSIlya Dryomov 	}
1293c9e9f97bSIlya Dryomov 
1294dae7b665SLi Zefan 	vol_args = memdup_user(arg, sizeof(*vol_args));
1295c9e9f97bSIlya Dryomov 	if (IS_ERR(vol_args)) {
1296c9e9f97bSIlya Dryomov 		ret = PTR_ERR(vol_args);
1297c9e9f97bSIlya Dryomov 		goto out;
1298c9e9f97bSIlya Dryomov 	}
12995516e595SMark Fasheh 
13005516e595SMark Fasheh 	vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
1301f46b5a66SChristoph Hellwig 
1302f46b5a66SChristoph Hellwig 	sizestr = vol_args->name;
1303f46b5a66SChristoph Hellwig 	devstr = strchr(sizestr, ':');
1304f46b5a66SChristoph Hellwig 	if (devstr) {
1305f46b5a66SChristoph Hellwig 		char *end;
1306f46b5a66SChristoph Hellwig 		sizestr = devstr + 1;
1307f46b5a66SChristoph Hellwig 		*devstr = '\0';
1308f46b5a66SChristoph Hellwig 		devstr = vol_args->name;
1309f46b5a66SChristoph Hellwig 		devid = simple_strtoull(devstr, &end, 10);
13105bb14682SArnd Hannemann 		printk(KERN_INFO "btrfs: resizing devid %llu\n",
131121380931SJoel Becker 		       (unsigned long long)devid);
1312f46b5a66SChristoph Hellwig 	}
13132b82032cSYan Zheng 	device = btrfs_find_device(root, devid, NULL, NULL);
1314f46b5a66SChristoph Hellwig 	if (!device) {
13155bb14682SArnd Hannemann 		printk(KERN_INFO "btrfs: resizer unable to find device %llu\n",
131621380931SJoel Becker 		       (unsigned long long)devid);
1317f46b5a66SChristoph Hellwig 		ret = -EINVAL;
1318c9e9f97bSIlya Dryomov 		goto out_free;
1319f46b5a66SChristoph Hellwig 	}
13204e42ae1bSLiu Bo 	if (device->fs_devices && device->fs_devices->seeding) {
13214e42ae1bSLiu Bo 		printk(KERN_INFO "btrfs: resizer unable to apply on "
1322a8c4a33bSChris Mason 		       "seeding device %llu\n",
1323a8c4a33bSChris Mason 		       (unsigned long long)devid);
13244e42ae1bSLiu Bo 		ret = -EINVAL;
13254e42ae1bSLiu Bo 		goto out_free;
13264e42ae1bSLiu Bo 	}
13274e42ae1bSLiu Bo 
1328f46b5a66SChristoph Hellwig 	if (!strcmp(sizestr, "max"))
1329f46b5a66SChristoph Hellwig 		new_size = device->bdev->bd_inode->i_size;
1330f46b5a66SChristoph Hellwig 	else {
1331f46b5a66SChristoph Hellwig 		if (sizestr[0] == '-') {
1332f46b5a66SChristoph Hellwig 			mod = -1;
1333f46b5a66SChristoph Hellwig 			sizestr++;
1334f46b5a66SChristoph Hellwig 		} else if (sizestr[0] == '+') {
1335f46b5a66SChristoph Hellwig 			mod = 1;
1336f46b5a66SChristoph Hellwig 			sizestr++;
1337f46b5a66SChristoph Hellwig 		}
133891748467SAkinobu Mita 		new_size = memparse(sizestr, NULL);
1339f46b5a66SChristoph Hellwig 		if (new_size == 0) {
1340f46b5a66SChristoph Hellwig 			ret = -EINVAL;
1341c9e9f97bSIlya Dryomov 			goto out_free;
1342f46b5a66SChristoph Hellwig 		}
1343f46b5a66SChristoph Hellwig 	}
1344f46b5a66SChristoph Hellwig 
1345f46b5a66SChristoph Hellwig 	old_size = device->total_bytes;
1346f46b5a66SChristoph Hellwig 
1347f46b5a66SChristoph Hellwig 	if (mod < 0) {
1348f46b5a66SChristoph Hellwig 		if (new_size > old_size) {
1349f46b5a66SChristoph Hellwig 			ret = -EINVAL;
1350c9e9f97bSIlya Dryomov 			goto out_free;
1351f46b5a66SChristoph Hellwig 		}
1352f46b5a66SChristoph Hellwig 		new_size = old_size - new_size;
1353f46b5a66SChristoph Hellwig 	} else if (mod > 0) {
1354f46b5a66SChristoph Hellwig 		new_size = old_size + new_size;
1355f46b5a66SChristoph Hellwig 	}
1356f46b5a66SChristoph Hellwig 
1357f46b5a66SChristoph Hellwig 	if (new_size < 256 * 1024 * 1024) {
1358f46b5a66SChristoph Hellwig 		ret = -EINVAL;
1359c9e9f97bSIlya Dryomov 		goto out_free;
1360f46b5a66SChristoph Hellwig 	}
1361f46b5a66SChristoph Hellwig 	if (new_size > device->bdev->bd_inode->i_size) {
1362f46b5a66SChristoph Hellwig 		ret = -EFBIG;
1363c9e9f97bSIlya Dryomov 		goto out_free;
1364f46b5a66SChristoph Hellwig 	}
1365f46b5a66SChristoph Hellwig 
1366f46b5a66SChristoph Hellwig 	do_div(new_size, root->sectorsize);
1367f46b5a66SChristoph Hellwig 	new_size *= root->sectorsize;
1368f46b5a66SChristoph Hellwig 
1369606686eeSJosef Bacik 	printk_in_rcu(KERN_INFO "btrfs: new size for %s is %llu\n",
1370606686eeSJosef Bacik 		      rcu_str_deref(device->name),
1371606686eeSJosef Bacik 		      (unsigned long long)new_size);
1372f46b5a66SChristoph Hellwig 
1373f46b5a66SChristoph Hellwig 	if (new_size > old_size) {
1374a22285a6SYan, Zheng 		trans = btrfs_start_transaction(root, 0);
137598d5dc13STsutomu Itoh 		if (IS_ERR(trans)) {
137698d5dc13STsutomu Itoh 			ret = PTR_ERR(trans);
1377c9e9f97bSIlya Dryomov 			goto out_free;
137898d5dc13STsutomu Itoh 		}
1379f46b5a66SChristoph Hellwig 		ret = btrfs_grow_device(trans, device, new_size);
1380f46b5a66SChristoph Hellwig 		btrfs_commit_transaction(trans, root);
1381ece7d20eSMike Fleetwood 	} else if (new_size < old_size) {
1382f46b5a66SChristoph Hellwig 		ret = btrfs_shrink_device(device, new_size);
1383f46b5a66SChristoph Hellwig 	}
1384f46b5a66SChristoph Hellwig 
1385c9e9f97bSIlya Dryomov out_free:
1386f46b5a66SChristoph Hellwig 	kfree(vol_args);
1387c9e9f97bSIlya Dryomov out:
1388c9e9f97bSIlya Dryomov 	mutex_unlock(&root->fs_info->volume_mutex);
1389f46b5a66SChristoph Hellwig 	return ret;
1390f46b5a66SChristoph Hellwig }
1391f46b5a66SChristoph Hellwig 
139272fd032eSSage Weil static noinline int btrfs_ioctl_snap_create_transid(struct file *file,
139372fd032eSSage Weil 						    char *name,
139472fd032eSSage Weil 						    unsigned long fd,
139572fd032eSSage Weil 						    int subvol,
1396b83cc969SLi Zefan 						    u64 *transid,
1397b83cc969SLi Zefan 						    bool readonly)
1398f46b5a66SChristoph Hellwig {
1399cb8e7090SChristoph Hellwig 	struct btrfs_root *root = BTRFS_I(fdentry(file)->d_inode)->root;
14003de4586cSChris Mason 	struct file *src_file;
1401f46b5a66SChristoph Hellwig 	int namelen;
14023de4586cSChris Mason 	int ret = 0;
1403f46b5a66SChristoph Hellwig 
1404c146afadSYan Zheng 	if (root->fs_info->sb->s_flags & MS_RDONLY)
1405c146afadSYan Zheng 		return -EROFS;
1406c146afadSYan Zheng 
140772fd032eSSage Weil 	namelen = strlen(name);
140872fd032eSSage Weil 	if (strchr(name, '/')) {
1409f46b5a66SChristoph Hellwig 		ret = -EINVAL;
1410f46b5a66SChristoph Hellwig 		goto out;
1411f46b5a66SChristoph Hellwig 	}
1412f46b5a66SChristoph Hellwig 
141316780cabSChris Mason 	if (name[0] == '.' &&
141416780cabSChris Mason 	   (namelen == 1 || (name[1] == '.' && namelen == 2))) {
141516780cabSChris Mason 		ret = -EEXIST;
141616780cabSChris Mason 		goto out;
141716780cabSChris Mason 	}
141816780cabSChris Mason 
14193de4586cSChris Mason 	if (subvol) {
142072fd032eSSage Weil 		ret = btrfs_mksubvol(&file->f_path, name, namelen,
1421b83cc969SLi Zefan 				     NULL, transid, readonly);
1422cb8e7090SChristoph Hellwig 	} else {
14233de4586cSChris Mason 		struct inode *src_inode;
142472fd032eSSage Weil 		src_file = fget(fd);
14253de4586cSChris Mason 		if (!src_file) {
14263de4586cSChris Mason 			ret = -EINVAL;
14273de4586cSChris Mason 			goto out;
14283de4586cSChris Mason 		}
14293de4586cSChris Mason 
14303de4586cSChris Mason 		src_inode = src_file->f_path.dentry->d_inode;
14313de4586cSChris Mason 		if (src_inode->i_sb != file->f_path.dentry->d_inode->i_sb) {
1432d397712bSChris Mason 			printk(KERN_INFO "btrfs: Snapshot src from "
1433d397712bSChris Mason 			       "another FS\n");
14343de4586cSChris Mason 			ret = -EINVAL;
14353de4586cSChris Mason 			fput(src_file);
14363de4586cSChris Mason 			goto out;
14373de4586cSChris Mason 		}
143872fd032eSSage Weil 		ret = btrfs_mksubvol(&file->f_path, name, namelen,
143972fd032eSSage Weil 				     BTRFS_I(src_inode)->root,
1440b83cc969SLi Zefan 				     transid, readonly);
14413de4586cSChris Mason 		fput(src_file);
1442cb8e7090SChristoph Hellwig 	}
1443f46b5a66SChristoph Hellwig out:
144472fd032eSSage Weil 	return ret;
144572fd032eSSage Weil }
144672fd032eSSage Weil 
144772fd032eSSage Weil static noinline int btrfs_ioctl_snap_create(struct file *file,
1448fa0d2b9bSLi Zefan 					    void __user *arg, int subvol)
144972fd032eSSage Weil {
1450fa0d2b9bSLi Zefan 	struct btrfs_ioctl_vol_args *vol_args;
145172fd032eSSage Weil 	int ret;
145272fd032eSSage Weil 
1453fa0d2b9bSLi Zefan 	vol_args = memdup_user(arg, sizeof(*vol_args));
1454fa0d2b9bSLi Zefan 	if (IS_ERR(vol_args))
1455fa0d2b9bSLi Zefan 		return PTR_ERR(vol_args);
1456fa0d2b9bSLi Zefan 	vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
1457fa0d2b9bSLi Zefan 
1458fa0d2b9bSLi Zefan 	ret = btrfs_ioctl_snap_create_transid(file, vol_args->name,
1459b83cc969SLi Zefan 					      vol_args->fd, subvol,
1460b83cc969SLi Zefan 					      NULL, false);
1461fa0d2b9bSLi Zefan 
1462fa0d2b9bSLi Zefan 	kfree(vol_args);
1463fa0d2b9bSLi Zefan 	return ret;
1464fa0d2b9bSLi Zefan }
1465fa0d2b9bSLi Zefan 
1466fa0d2b9bSLi Zefan static noinline int btrfs_ioctl_snap_create_v2(struct file *file,
1467fa0d2b9bSLi Zefan 					       void __user *arg, int subvol)
1468fa0d2b9bSLi Zefan {
1469fa0d2b9bSLi Zefan 	struct btrfs_ioctl_vol_args_v2 *vol_args;
1470fa0d2b9bSLi Zefan 	int ret;
1471fdfb1e4fSLi Zefan 	u64 transid = 0;
1472fdfb1e4fSLi Zefan 	u64 *ptr = NULL;
1473b83cc969SLi Zefan 	bool readonly = false;
147472fd032eSSage Weil 
1475fa0d2b9bSLi Zefan 	vol_args = memdup_user(arg, sizeof(*vol_args));
1476fa0d2b9bSLi Zefan 	if (IS_ERR(vol_args))
1477fa0d2b9bSLi Zefan 		return PTR_ERR(vol_args);
1478fa0d2b9bSLi Zefan 	vol_args->name[BTRFS_SUBVOL_NAME_MAX] = '\0';
1479fdfb1e4fSLi Zefan 
1480b83cc969SLi Zefan 	if (vol_args->flags &
1481b83cc969SLi Zefan 	    ~(BTRFS_SUBVOL_CREATE_ASYNC | BTRFS_SUBVOL_RDONLY)) {
1482b83cc969SLi Zefan 		ret = -EOPNOTSUPP;
1483fdfb1e4fSLi Zefan 		goto out;
1484fdfb1e4fSLi Zefan 	}
1485fdfb1e4fSLi Zefan 
1486fa0d2b9bSLi Zefan 	if (vol_args->flags & BTRFS_SUBVOL_CREATE_ASYNC)
1487fdfb1e4fSLi Zefan 		ptr = &transid;
1488b83cc969SLi Zefan 	if (vol_args->flags & BTRFS_SUBVOL_RDONLY)
1489b83cc969SLi Zefan 		readonly = true;
149075eaa0e2SSage Weil 
1491fa0d2b9bSLi Zefan 	ret = btrfs_ioctl_snap_create_transid(file, vol_args->name,
1492b83cc969SLi Zefan 					      vol_args->fd, subvol,
1493b83cc969SLi Zefan 					      ptr, readonly);
149475eaa0e2SSage Weil 
1495fdfb1e4fSLi Zefan 	if (ret == 0 && ptr &&
149675eaa0e2SSage Weil 	    copy_to_user(arg +
1497fdfb1e4fSLi Zefan 			 offsetof(struct btrfs_ioctl_vol_args_v2,
1498fdfb1e4fSLi Zefan 				  transid), ptr, sizeof(*ptr)))
149975eaa0e2SSage Weil 		ret = -EFAULT;
1500fdfb1e4fSLi Zefan out:
1501f46b5a66SChristoph Hellwig 	kfree(vol_args);
1502f46b5a66SChristoph Hellwig 	return ret;
1503f46b5a66SChristoph Hellwig }
1504f46b5a66SChristoph Hellwig 
15050caa102dSLi Zefan static noinline int btrfs_ioctl_subvol_getflags(struct file *file,
15060caa102dSLi Zefan 						void __user *arg)
15070caa102dSLi Zefan {
15080caa102dSLi Zefan 	struct inode *inode = fdentry(file)->d_inode;
15090caa102dSLi Zefan 	struct btrfs_root *root = BTRFS_I(inode)->root;
15100caa102dSLi Zefan 	int ret = 0;
15110caa102dSLi Zefan 	u64 flags = 0;
15120caa102dSLi Zefan 
151333345d01SLi Zefan 	if (btrfs_ino(inode) != BTRFS_FIRST_FREE_OBJECTID)
15140caa102dSLi Zefan 		return -EINVAL;
15150caa102dSLi Zefan 
15160caa102dSLi Zefan 	down_read(&root->fs_info->subvol_sem);
15170caa102dSLi Zefan 	if (btrfs_root_readonly(root))
15180caa102dSLi Zefan 		flags |= BTRFS_SUBVOL_RDONLY;
15190caa102dSLi Zefan 	up_read(&root->fs_info->subvol_sem);
15200caa102dSLi Zefan 
15210caa102dSLi Zefan 	if (copy_to_user(arg, &flags, sizeof(flags)))
15220caa102dSLi Zefan 		ret = -EFAULT;
15230caa102dSLi Zefan 
15240caa102dSLi Zefan 	return ret;
15250caa102dSLi Zefan }
15260caa102dSLi Zefan 
15270caa102dSLi Zefan static noinline int btrfs_ioctl_subvol_setflags(struct file *file,
15280caa102dSLi Zefan 					      void __user *arg)
15290caa102dSLi Zefan {
15300caa102dSLi Zefan 	struct inode *inode = fdentry(file)->d_inode;
15310caa102dSLi Zefan 	struct btrfs_root *root = BTRFS_I(inode)->root;
15320caa102dSLi Zefan 	struct btrfs_trans_handle *trans;
15330caa102dSLi Zefan 	u64 root_flags;
15340caa102dSLi Zefan 	u64 flags;
15350caa102dSLi Zefan 	int ret = 0;
15360caa102dSLi Zefan 
15370caa102dSLi Zefan 	if (root->fs_info->sb->s_flags & MS_RDONLY)
15380caa102dSLi Zefan 		return -EROFS;
15390caa102dSLi Zefan 
154033345d01SLi Zefan 	if (btrfs_ino(inode) != BTRFS_FIRST_FREE_OBJECTID)
15410caa102dSLi Zefan 		return -EINVAL;
15420caa102dSLi Zefan 
15430caa102dSLi Zefan 	if (copy_from_user(&flags, arg, sizeof(flags)))
15440caa102dSLi Zefan 		return -EFAULT;
15450caa102dSLi Zefan 
1546b4dc2b8cSLi Zefan 	if (flags & BTRFS_SUBVOL_CREATE_ASYNC)
15470caa102dSLi Zefan 		return -EINVAL;
15480caa102dSLi Zefan 
15490caa102dSLi Zefan 	if (flags & ~BTRFS_SUBVOL_RDONLY)
15500caa102dSLi Zefan 		return -EOPNOTSUPP;
15510caa102dSLi Zefan 
15522e149670SSerge E. Hallyn 	if (!inode_owner_or_capable(inode))
1553b4dc2b8cSLi Zefan 		return -EACCES;
1554b4dc2b8cSLi Zefan 
15550caa102dSLi Zefan 	down_write(&root->fs_info->subvol_sem);
15560caa102dSLi Zefan 
15570caa102dSLi Zefan 	/* nothing to do */
15580caa102dSLi Zefan 	if (!!(flags & BTRFS_SUBVOL_RDONLY) == btrfs_root_readonly(root))
15590caa102dSLi Zefan 		goto out;
15600caa102dSLi Zefan 
15610caa102dSLi Zefan 	root_flags = btrfs_root_flags(&root->root_item);
15620caa102dSLi Zefan 	if (flags & BTRFS_SUBVOL_RDONLY)
15630caa102dSLi Zefan 		btrfs_set_root_flags(&root->root_item,
15640caa102dSLi Zefan 				     root_flags | BTRFS_ROOT_SUBVOL_RDONLY);
15650caa102dSLi Zefan 	else
15660caa102dSLi Zefan 		btrfs_set_root_flags(&root->root_item,
15670caa102dSLi Zefan 				     root_flags & ~BTRFS_ROOT_SUBVOL_RDONLY);
15680caa102dSLi Zefan 
15690caa102dSLi Zefan 	trans = btrfs_start_transaction(root, 1);
15700caa102dSLi Zefan 	if (IS_ERR(trans)) {
15710caa102dSLi Zefan 		ret = PTR_ERR(trans);
15720caa102dSLi Zefan 		goto out_reset;
15730caa102dSLi Zefan 	}
15740caa102dSLi Zefan 
1575b4dc2b8cSLi Zefan 	ret = btrfs_update_root(trans, root->fs_info->tree_root,
15760caa102dSLi Zefan 				&root->root_key, &root->root_item);
15770caa102dSLi Zefan 
15780caa102dSLi Zefan 	btrfs_commit_transaction(trans, root);
15790caa102dSLi Zefan out_reset:
15800caa102dSLi Zefan 	if (ret)
15810caa102dSLi Zefan 		btrfs_set_root_flags(&root->root_item, root_flags);
15820caa102dSLi Zefan out:
15830caa102dSLi Zefan 	up_write(&root->fs_info->subvol_sem);
15840caa102dSLi Zefan 	return ret;
15850caa102dSLi Zefan }
15860caa102dSLi Zefan 
158776dda93cSYan, Zheng /*
158876dda93cSYan, Zheng  * helper to check if the subvolume references other subvolumes
158976dda93cSYan, Zheng  */
159076dda93cSYan, Zheng static noinline int may_destroy_subvol(struct btrfs_root *root)
159176dda93cSYan, Zheng {
159276dda93cSYan, Zheng 	struct btrfs_path *path;
159376dda93cSYan, Zheng 	struct btrfs_key key;
159476dda93cSYan, Zheng 	int ret;
159576dda93cSYan, Zheng 
159676dda93cSYan, Zheng 	path = btrfs_alloc_path();
159776dda93cSYan, Zheng 	if (!path)
159876dda93cSYan, Zheng 		return -ENOMEM;
159976dda93cSYan, Zheng 
160076dda93cSYan, Zheng 	key.objectid = root->root_key.objectid;
160176dda93cSYan, Zheng 	key.type = BTRFS_ROOT_REF_KEY;
160276dda93cSYan, Zheng 	key.offset = (u64)-1;
160376dda93cSYan, Zheng 
160476dda93cSYan, Zheng 	ret = btrfs_search_slot(NULL, root->fs_info->tree_root,
160576dda93cSYan, Zheng 				&key, path, 0, 0);
160676dda93cSYan, Zheng 	if (ret < 0)
160776dda93cSYan, Zheng 		goto out;
160876dda93cSYan, Zheng 	BUG_ON(ret == 0);
160976dda93cSYan, Zheng 
161076dda93cSYan, Zheng 	ret = 0;
161176dda93cSYan, Zheng 	if (path->slots[0] > 0) {
161276dda93cSYan, Zheng 		path->slots[0]--;
161376dda93cSYan, Zheng 		btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
161476dda93cSYan, Zheng 		if (key.objectid == root->root_key.objectid &&
161576dda93cSYan, Zheng 		    key.type == BTRFS_ROOT_REF_KEY)
161676dda93cSYan, Zheng 			ret = -ENOTEMPTY;
161776dda93cSYan, Zheng 	}
161876dda93cSYan, Zheng out:
161976dda93cSYan, Zheng 	btrfs_free_path(path);
162076dda93cSYan, Zheng 	return ret;
162176dda93cSYan, Zheng }
162276dda93cSYan, Zheng 
1623ac8e9819SChris Mason static noinline int key_in_sk(struct btrfs_key *key,
1624ac8e9819SChris Mason 			      struct btrfs_ioctl_search_key *sk)
1625ac8e9819SChris Mason {
1626abc6e134SChris Mason 	struct btrfs_key test;
1627abc6e134SChris Mason 	int ret;
1628abc6e134SChris Mason 
1629abc6e134SChris Mason 	test.objectid = sk->min_objectid;
1630abc6e134SChris Mason 	test.type = sk->min_type;
1631abc6e134SChris Mason 	test.offset = sk->min_offset;
1632abc6e134SChris Mason 
1633abc6e134SChris Mason 	ret = btrfs_comp_cpu_keys(key, &test);
1634abc6e134SChris Mason 	if (ret < 0)
1635ac8e9819SChris Mason 		return 0;
1636abc6e134SChris Mason 
1637abc6e134SChris Mason 	test.objectid = sk->max_objectid;
1638abc6e134SChris Mason 	test.type = sk->max_type;
1639abc6e134SChris Mason 	test.offset = sk->max_offset;
1640abc6e134SChris Mason 
1641abc6e134SChris Mason 	ret = btrfs_comp_cpu_keys(key, &test);
1642abc6e134SChris Mason 	if (ret > 0)
1643ac8e9819SChris Mason 		return 0;
1644ac8e9819SChris Mason 	return 1;
1645ac8e9819SChris Mason }
1646ac8e9819SChris Mason 
1647ac8e9819SChris Mason static noinline int copy_to_sk(struct btrfs_root *root,
1648ac8e9819SChris Mason 			       struct btrfs_path *path,
1649ac8e9819SChris Mason 			       struct btrfs_key *key,
1650ac8e9819SChris Mason 			       struct btrfs_ioctl_search_key *sk,
1651ac8e9819SChris Mason 			       char *buf,
1652ac8e9819SChris Mason 			       unsigned long *sk_offset,
1653ac8e9819SChris Mason 			       int *num_found)
1654ac8e9819SChris Mason {
1655ac8e9819SChris Mason 	u64 found_transid;
1656ac8e9819SChris Mason 	struct extent_buffer *leaf;
1657ac8e9819SChris Mason 	struct btrfs_ioctl_search_header sh;
1658ac8e9819SChris Mason 	unsigned long item_off;
1659ac8e9819SChris Mason 	unsigned long item_len;
1660ac8e9819SChris Mason 	int nritems;
1661ac8e9819SChris Mason 	int i;
1662ac8e9819SChris Mason 	int slot;
1663ac8e9819SChris Mason 	int ret = 0;
1664ac8e9819SChris Mason 
1665ac8e9819SChris Mason 	leaf = path->nodes[0];
1666ac8e9819SChris Mason 	slot = path->slots[0];
1667ac8e9819SChris Mason 	nritems = btrfs_header_nritems(leaf);
1668ac8e9819SChris Mason 
1669ac8e9819SChris Mason 	if (btrfs_header_generation(leaf) > sk->max_transid) {
1670ac8e9819SChris Mason 		i = nritems;
1671ac8e9819SChris Mason 		goto advance_key;
1672ac8e9819SChris Mason 	}
1673ac8e9819SChris Mason 	found_transid = btrfs_header_generation(leaf);
1674ac8e9819SChris Mason 
1675ac8e9819SChris Mason 	for (i = slot; i < nritems; i++) {
1676ac8e9819SChris Mason 		item_off = btrfs_item_ptr_offset(leaf, i);
1677ac8e9819SChris Mason 		item_len = btrfs_item_size_nr(leaf, i);
1678ac8e9819SChris Mason 
1679ac8e9819SChris Mason 		if (item_len > BTRFS_SEARCH_ARGS_BUFSIZE)
1680ac8e9819SChris Mason 			item_len = 0;
1681ac8e9819SChris Mason 
1682ac8e9819SChris Mason 		if (sizeof(sh) + item_len + *sk_offset >
1683ac8e9819SChris Mason 		    BTRFS_SEARCH_ARGS_BUFSIZE) {
1684ac8e9819SChris Mason 			ret = 1;
1685ac8e9819SChris Mason 			goto overflow;
1686ac8e9819SChris Mason 		}
1687ac8e9819SChris Mason 
1688ac8e9819SChris Mason 		btrfs_item_key_to_cpu(leaf, key, i);
1689ac8e9819SChris Mason 		if (!key_in_sk(key, sk))
1690ac8e9819SChris Mason 			continue;
1691ac8e9819SChris Mason 
1692ac8e9819SChris Mason 		sh.objectid = key->objectid;
1693ac8e9819SChris Mason 		sh.offset = key->offset;
1694ac8e9819SChris Mason 		sh.type = key->type;
1695ac8e9819SChris Mason 		sh.len = item_len;
1696ac8e9819SChris Mason 		sh.transid = found_transid;
1697ac8e9819SChris Mason 
1698ac8e9819SChris Mason 		/* copy search result header */
1699ac8e9819SChris Mason 		memcpy(buf + *sk_offset, &sh, sizeof(sh));
1700ac8e9819SChris Mason 		*sk_offset += sizeof(sh);
1701ac8e9819SChris Mason 
1702ac8e9819SChris Mason 		if (item_len) {
1703ac8e9819SChris Mason 			char *p = buf + *sk_offset;
1704ac8e9819SChris Mason 			/* copy the item */
1705ac8e9819SChris Mason 			read_extent_buffer(leaf, p,
1706ac8e9819SChris Mason 					   item_off, item_len);
1707ac8e9819SChris Mason 			*sk_offset += item_len;
1708ac8e9819SChris Mason 		}
1709e2156867SHugo Mills 		(*num_found)++;
1710ac8e9819SChris Mason 
1711ac8e9819SChris Mason 		if (*num_found >= sk->nr_items)
1712ac8e9819SChris Mason 			break;
1713ac8e9819SChris Mason 	}
1714ac8e9819SChris Mason advance_key:
1715ac8e9819SChris Mason 	ret = 0;
1716abc6e134SChris Mason 	if (key->offset < (u64)-1 && key->offset < sk->max_offset)
1717abc6e134SChris Mason 		key->offset++;
1718abc6e134SChris Mason 	else if (key->type < (u8)-1 && key->type < sk->max_type) {
1719abc6e134SChris Mason 		key->offset = 0;
1720abc6e134SChris Mason 		key->type++;
1721abc6e134SChris Mason 	} else if (key->objectid < (u64)-1 && key->objectid < sk->max_objectid) {
1722abc6e134SChris Mason 		key->offset = 0;
1723abc6e134SChris Mason 		key->type = 0;
1724abc6e134SChris Mason 		key->objectid++;
1725abc6e134SChris Mason 	} else
1726abc6e134SChris Mason 		ret = 1;
1727ac8e9819SChris Mason overflow:
1728ac8e9819SChris Mason 	return ret;
1729ac8e9819SChris Mason }
1730ac8e9819SChris Mason 
1731ac8e9819SChris Mason static noinline int search_ioctl(struct inode *inode,
1732ac8e9819SChris Mason 				 struct btrfs_ioctl_search_args *args)
1733ac8e9819SChris Mason {
1734ac8e9819SChris Mason 	struct btrfs_root *root;
1735ac8e9819SChris Mason 	struct btrfs_key key;
1736ac8e9819SChris Mason 	struct btrfs_key max_key;
1737ac8e9819SChris Mason 	struct btrfs_path *path;
1738ac8e9819SChris Mason 	struct btrfs_ioctl_search_key *sk = &args->key;
1739ac8e9819SChris Mason 	struct btrfs_fs_info *info = BTRFS_I(inode)->root->fs_info;
1740ac8e9819SChris Mason 	int ret;
1741ac8e9819SChris Mason 	int num_found = 0;
1742ac8e9819SChris Mason 	unsigned long sk_offset = 0;
1743ac8e9819SChris Mason 
1744ac8e9819SChris Mason 	path = btrfs_alloc_path();
1745ac8e9819SChris Mason 	if (!path)
1746ac8e9819SChris Mason 		return -ENOMEM;
1747ac8e9819SChris Mason 
1748ac8e9819SChris Mason 	if (sk->tree_id == 0) {
1749ac8e9819SChris Mason 		/* search the root of the inode that was passed */
1750ac8e9819SChris Mason 		root = BTRFS_I(inode)->root;
1751ac8e9819SChris Mason 	} else {
1752ac8e9819SChris Mason 		key.objectid = sk->tree_id;
1753ac8e9819SChris Mason 		key.type = BTRFS_ROOT_ITEM_KEY;
1754ac8e9819SChris Mason 		key.offset = (u64)-1;
1755ac8e9819SChris Mason 		root = btrfs_read_fs_root_no_name(info, &key);
1756ac8e9819SChris Mason 		if (IS_ERR(root)) {
1757ac8e9819SChris Mason 			printk(KERN_ERR "could not find root %llu\n",
1758ac8e9819SChris Mason 			       sk->tree_id);
1759ac8e9819SChris Mason 			btrfs_free_path(path);
1760ac8e9819SChris Mason 			return -ENOENT;
1761ac8e9819SChris Mason 		}
1762ac8e9819SChris Mason 	}
1763ac8e9819SChris Mason 
1764ac8e9819SChris Mason 	key.objectid = sk->min_objectid;
1765ac8e9819SChris Mason 	key.type = sk->min_type;
1766ac8e9819SChris Mason 	key.offset = sk->min_offset;
1767ac8e9819SChris Mason 
1768ac8e9819SChris Mason 	max_key.objectid = sk->max_objectid;
1769ac8e9819SChris Mason 	max_key.type = sk->max_type;
1770ac8e9819SChris Mason 	max_key.offset = sk->max_offset;
1771ac8e9819SChris Mason 
1772ac8e9819SChris Mason 	path->keep_locks = 1;
1773ac8e9819SChris Mason 
1774ac8e9819SChris Mason 	while(1) {
1775ac8e9819SChris Mason 		ret = btrfs_search_forward(root, &key, &max_key, path, 0,
1776ac8e9819SChris Mason 					   sk->min_transid);
1777ac8e9819SChris Mason 		if (ret != 0) {
1778ac8e9819SChris Mason 			if (ret > 0)
1779ac8e9819SChris Mason 				ret = 0;
1780ac8e9819SChris Mason 			goto err;
1781ac8e9819SChris Mason 		}
1782ac8e9819SChris Mason 		ret = copy_to_sk(root, path, &key, sk, args->buf,
1783ac8e9819SChris Mason 				 &sk_offset, &num_found);
1784b3b4aa74SDavid Sterba 		btrfs_release_path(path);
1785ac8e9819SChris Mason 		if (ret || num_found >= sk->nr_items)
1786ac8e9819SChris Mason 			break;
1787ac8e9819SChris Mason 
1788ac8e9819SChris Mason 	}
1789ac8e9819SChris Mason 	ret = 0;
1790ac8e9819SChris Mason err:
1791ac8e9819SChris Mason 	sk->nr_items = num_found;
1792ac8e9819SChris Mason 	btrfs_free_path(path);
1793ac8e9819SChris Mason 	return ret;
1794ac8e9819SChris Mason }
1795ac8e9819SChris Mason 
1796ac8e9819SChris Mason static noinline int btrfs_ioctl_tree_search(struct file *file,
1797ac8e9819SChris Mason 					   void __user *argp)
1798ac8e9819SChris Mason {
1799ac8e9819SChris Mason 	 struct btrfs_ioctl_search_args *args;
1800ac8e9819SChris Mason 	 struct inode *inode;
1801ac8e9819SChris Mason 	 int ret;
1802ac8e9819SChris Mason 
1803ac8e9819SChris Mason 	if (!capable(CAP_SYS_ADMIN))
1804ac8e9819SChris Mason 		return -EPERM;
1805ac8e9819SChris Mason 
18062354d08fSJulia Lawall 	args = memdup_user(argp, sizeof(*args));
18072354d08fSJulia Lawall 	if (IS_ERR(args))
18082354d08fSJulia Lawall 		return PTR_ERR(args);
1809ac8e9819SChris Mason 
1810ac8e9819SChris Mason 	inode = fdentry(file)->d_inode;
1811ac8e9819SChris Mason 	ret = search_ioctl(inode, args);
1812ac8e9819SChris Mason 	if (ret == 0 && copy_to_user(argp, args, sizeof(*args)))
1813ac8e9819SChris Mason 		ret = -EFAULT;
1814ac8e9819SChris Mason 	kfree(args);
1815ac8e9819SChris Mason 	return ret;
1816ac8e9819SChris Mason }
1817ac8e9819SChris Mason 
181898d377a0STARUISI Hiroaki /*
1819ac8e9819SChris Mason  * Search INODE_REFs to identify path name of 'dirid' directory
1820ac8e9819SChris Mason  * in a 'tree_id' tree. and sets path name to 'name'.
182198d377a0STARUISI Hiroaki  */
182298d377a0STARUISI Hiroaki static noinline int btrfs_search_path_in_tree(struct btrfs_fs_info *info,
182398d377a0STARUISI Hiroaki 				u64 tree_id, u64 dirid, char *name)
182498d377a0STARUISI Hiroaki {
182598d377a0STARUISI Hiroaki 	struct btrfs_root *root;
182698d377a0STARUISI Hiroaki 	struct btrfs_key key;
1827ac8e9819SChris Mason 	char *ptr;
182898d377a0STARUISI Hiroaki 	int ret = -1;
182998d377a0STARUISI Hiroaki 	int slot;
183098d377a0STARUISI Hiroaki 	int len;
183198d377a0STARUISI Hiroaki 	int total_len = 0;
183298d377a0STARUISI Hiroaki 	struct btrfs_inode_ref *iref;
183398d377a0STARUISI Hiroaki 	struct extent_buffer *l;
183498d377a0STARUISI Hiroaki 	struct btrfs_path *path;
183598d377a0STARUISI Hiroaki 
183698d377a0STARUISI Hiroaki 	if (dirid == BTRFS_FIRST_FREE_OBJECTID) {
183798d377a0STARUISI Hiroaki 		name[0]='\0';
183898d377a0STARUISI Hiroaki 		return 0;
183998d377a0STARUISI Hiroaki 	}
184098d377a0STARUISI Hiroaki 
184198d377a0STARUISI Hiroaki 	path = btrfs_alloc_path();
184298d377a0STARUISI Hiroaki 	if (!path)
184398d377a0STARUISI Hiroaki 		return -ENOMEM;
184498d377a0STARUISI Hiroaki 
1845ac8e9819SChris Mason 	ptr = &name[BTRFS_INO_LOOKUP_PATH_MAX];
184698d377a0STARUISI Hiroaki 
184798d377a0STARUISI Hiroaki 	key.objectid = tree_id;
184898d377a0STARUISI Hiroaki 	key.type = BTRFS_ROOT_ITEM_KEY;
184998d377a0STARUISI Hiroaki 	key.offset = (u64)-1;
185098d377a0STARUISI Hiroaki 	root = btrfs_read_fs_root_no_name(info, &key);
185198d377a0STARUISI Hiroaki 	if (IS_ERR(root)) {
185298d377a0STARUISI Hiroaki 		printk(KERN_ERR "could not find root %llu\n", tree_id);
18538ad6fcabSChris Mason 		ret = -ENOENT;
18548ad6fcabSChris Mason 		goto out;
185598d377a0STARUISI Hiroaki 	}
185698d377a0STARUISI Hiroaki 
185798d377a0STARUISI Hiroaki 	key.objectid = dirid;
185898d377a0STARUISI Hiroaki 	key.type = BTRFS_INODE_REF_KEY;
18598ad6fcabSChris Mason 	key.offset = (u64)-1;
186098d377a0STARUISI Hiroaki 
186198d377a0STARUISI Hiroaki 	while(1) {
186298d377a0STARUISI Hiroaki 		ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
186398d377a0STARUISI Hiroaki 		if (ret < 0)
186498d377a0STARUISI Hiroaki 			goto out;
186598d377a0STARUISI Hiroaki 
186698d377a0STARUISI Hiroaki 		l = path->nodes[0];
186798d377a0STARUISI Hiroaki 		slot = path->slots[0];
18688ad6fcabSChris Mason 		if (ret > 0 && slot > 0)
18698ad6fcabSChris Mason 			slot--;
187098d377a0STARUISI Hiroaki 		btrfs_item_key_to_cpu(l, &key, slot);
187198d377a0STARUISI Hiroaki 
187298d377a0STARUISI Hiroaki 		if (ret > 0 && (key.objectid != dirid ||
1873ac8e9819SChris Mason 				key.type != BTRFS_INODE_REF_KEY)) {
1874ac8e9819SChris Mason 			ret = -ENOENT;
187598d377a0STARUISI Hiroaki 			goto out;
1876ac8e9819SChris Mason 		}
187798d377a0STARUISI Hiroaki 
187898d377a0STARUISI Hiroaki 		iref = btrfs_item_ptr(l, slot, struct btrfs_inode_ref);
187998d377a0STARUISI Hiroaki 		len = btrfs_inode_ref_name_len(l, iref);
188098d377a0STARUISI Hiroaki 		ptr -= len + 1;
188198d377a0STARUISI Hiroaki 		total_len += len + 1;
1882ac8e9819SChris Mason 		if (ptr < name)
188398d377a0STARUISI Hiroaki 			goto out;
188498d377a0STARUISI Hiroaki 
188598d377a0STARUISI Hiroaki 		*(ptr + len) = '/';
188698d377a0STARUISI Hiroaki 		read_extent_buffer(l, ptr,(unsigned long)(iref + 1), len);
188798d377a0STARUISI Hiroaki 
188898d377a0STARUISI Hiroaki 		if (key.offset == BTRFS_FIRST_FREE_OBJECTID)
188998d377a0STARUISI Hiroaki 			break;
189098d377a0STARUISI Hiroaki 
1891b3b4aa74SDavid Sterba 		btrfs_release_path(path);
189298d377a0STARUISI Hiroaki 		key.objectid = key.offset;
18938ad6fcabSChris Mason 		key.offset = (u64)-1;
189498d377a0STARUISI Hiroaki 		dirid = key.objectid;
189598d377a0STARUISI Hiroaki 	}
1896ac8e9819SChris Mason 	if (ptr < name)
189798d377a0STARUISI Hiroaki 		goto out;
189877906a50SLi Zefan 	memmove(name, ptr, total_len);
189998d377a0STARUISI Hiroaki 	name[total_len]='\0';
190098d377a0STARUISI Hiroaki 	ret = 0;
190198d377a0STARUISI Hiroaki out:
190298d377a0STARUISI Hiroaki 	btrfs_free_path(path);
1903ac8e9819SChris Mason 	return ret;
1904ac8e9819SChris Mason }
1905ac8e9819SChris Mason 
1906ac8e9819SChris Mason static noinline int btrfs_ioctl_ino_lookup(struct file *file,
1907ac8e9819SChris Mason 					   void __user *argp)
1908ac8e9819SChris Mason {
1909ac8e9819SChris Mason 	 struct btrfs_ioctl_ino_lookup_args *args;
1910ac8e9819SChris Mason 	 struct inode *inode;
1911ac8e9819SChris Mason 	 int ret;
1912ac8e9819SChris Mason 
1913ac8e9819SChris Mason 	if (!capable(CAP_SYS_ADMIN))
1914ac8e9819SChris Mason 		return -EPERM;
1915ac8e9819SChris Mason 
19162354d08fSJulia Lawall 	args = memdup_user(argp, sizeof(*args));
19172354d08fSJulia Lawall 	if (IS_ERR(args))
19182354d08fSJulia Lawall 		return PTR_ERR(args);
1919c2b96929SDan Carpenter 
1920ac8e9819SChris Mason 	inode = fdentry(file)->d_inode;
1921ac8e9819SChris Mason 
19221b53ac4dSChris Mason 	if (args->treeid == 0)
19231b53ac4dSChris Mason 		args->treeid = BTRFS_I(inode)->root->root_key.objectid;
19241b53ac4dSChris Mason 
1925ac8e9819SChris Mason 	ret = btrfs_search_path_in_tree(BTRFS_I(inode)->root->fs_info,
1926ac8e9819SChris Mason 					args->treeid, args->objectid,
1927ac8e9819SChris Mason 					args->name);
1928ac8e9819SChris Mason 
1929ac8e9819SChris Mason 	if (ret == 0 && copy_to_user(argp, args, sizeof(*args)))
1930ac8e9819SChris Mason 		ret = -EFAULT;
1931ac8e9819SChris Mason 
1932ac8e9819SChris Mason 	kfree(args);
193398d377a0STARUISI Hiroaki 	return ret;
193498d377a0STARUISI Hiroaki }
193598d377a0STARUISI Hiroaki 
193676dda93cSYan, Zheng static noinline int btrfs_ioctl_snap_destroy(struct file *file,
193776dda93cSYan, Zheng 					     void __user *arg)
193876dda93cSYan, Zheng {
193976dda93cSYan, Zheng 	struct dentry *parent = fdentry(file);
194076dda93cSYan, Zheng 	struct dentry *dentry;
194176dda93cSYan, Zheng 	struct inode *dir = parent->d_inode;
194276dda93cSYan, Zheng 	struct inode *inode;
194376dda93cSYan, Zheng 	struct btrfs_root *root = BTRFS_I(dir)->root;
194476dda93cSYan, Zheng 	struct btrfs_root *dest = NULL;
194576dda93cSYan, Zheng 	struct btrfs_ioctl_vol_args *vol_args;
194676dda93cSYan, Zheng 	struct btrfs_trans_handle *trans;
194776dda93cSYan, Zheng 	int namelen;
194876dda93cSYan, Zheng 	int ret;
194976dda93cSYan, Zheng 	int err = 0;
195076dda93cSYan, Zheng 
195176dda93cSYan, Zheng 	vol_args = memdup_user(arg, sizeof(*vol_args));
195276dda93cSYan, Zheng 	if (IS_ERR(vol_args))
195376dda93cSYan, Zheng 		return PTR_ERR(vol_args);
195476dda93cSYan, Zheng 
195576dda93cSYan, Zheng 	vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
195676dda93cSYan, Zheng 	namelen = strlen(vol_args->name);
195776dda93cSYan, Zheng 	if (strchr(vol_args->name, '/') ||
195876dda93cSYan, Zheng 	    strncmp(vol_args->name, "..", namelen) == 0) {
195976dda93cSYan, Zheng 		err = -EINVAL;
196076dda93cSYan, Zheng 		goto out;
196176dda93cSYan, Zheng 	}
196276dda93cSYan, Zheng 
1963a561be71SAl Viro 	err = mnt_want_write_file(file);
196476dda93cSYan, Zheng 	if (err)
196576dda93cSYan, Zheng 		goto out;
196676dda93cSYan, Zheng 
196776dda93cSYan, Zheng 	mutex_lock_nested(&dir->i_mutex, I_MUTEX_PARENT);
196876dda93cSYan, Zheng 	dentry = lookup_one_len(vol_args->name, parent, namelen);
196976dda93cSYan, Zheng 	if (IS_ERR(dentry)) {
197076dda93cSYan, Zheng 		err = PTR_ERR(dentry);
197176dda93cSYan, Zheng 		goto out_unlock_dir;
197276dda93cSYan, Zheng 	}
197376dda93cSYan, Zheng 
197476dda93cSYan, Zheng 	if (!dentry->d_inode) {
197576dda93cSYan, Zheng 		err = -ENOENT;
197676dda93cSYan, Zheng 		goto out_dput;
197776dda93cSYan, Zheng 	}
197876dda93cSYan, Zheng 
197976dda93cSYan, Zheng 	inode = dentry->d_inode;
19804260f7c7SSage Weil 	dest = BTRFS_I(inode)->root;
19814260f7c7SSage Weil 	if (!capable(CAP_SYS_ADMIN)){
19824260f7c7SSage Weil 		/*
19834260f7c7SSage Weil 		 * Regular user.  Only allow this with a special mount
19844260f7c7SSage Weil 		 * option, when the user has write+exec access to the
19854260f7c7SSage Weil 		 * subvol root, and when rmdir(2) would have been
19864260f7c7SSage Weil 		 * allowed.
19874260f7c7SSage Weil 		 *
19884260f7c7SSage Weil 		 * Note that this is _not_ check that the subvol is
19894260f7c7SSage Weil 		 * empty or doesn't contain data that we wouldn't
19904260f7c7SSage Weil 		 * otherwise be able to delete.
19914260f7c7SSage Weil 		 *
19924260f7c7SSage Weil 		 * Users who want to delete empty subvols should try
19934260f7c7SSage Weil 		 * rmdir(2).
19944260f7c7SSage Weil 		 */
19954260f7c7SSage Weil 		err = -EPERM;
19964260f7c7SSage Weil 		if (!btrfs_test_opt(root, USER_SUBVOL_RM_ALLOWED))
19974260f7c7SSage Weil 			goto out_dput;
19984260f7c7SSage Weil 
19994260f7c7SSage Weil 		/*
20004260f7c7SSage Weil 		 * Do not allow deletion if the parent dir is the same
20014260f7c7SSage Weil 		 * as the dir to be deleted.  That means the ioctl
20024260f7c7SSage Weil 		 * must be called on the dentry referencing the root
20034260f7c7SSage Weil 		 * of the subvol, not a random directory contained
20044260f7c7SSage Weil 		 * within it.
20054260f7c7SSage Weil 		 */
20064260f7c7SSage Weil 		err = -EINVAL;
20074260f7c7SSage Weil 		if (root == dest)
20084260f7c7SSage Weil 			goto out_dput;
20094260f7c7SSage Weil 
20104260f7c7SSage Weil 		err = inode_permission(inode, MAY_WRITE | MAY_EXEC);
20114260f7c7SSage Weil 		if (err)
20124260f7c7SSage Weil 			goto out_dput;
20134260f7c7SSage Weil 
20144260f7c7SSage Weil 		/* check if subvolume may be deleted by a non-root user */
20154260f7c7SSage Weil 		err = btrfs_may_delete(dir, dentry, 1);
20164260f7c7SSage Weil 		if (err)
20174260f7c7SSage Weil 			goto out_dput;
20184260f7c7SSage Weil 	}
20194260f7c7SSage Weil 
202033345d01SLi Zefan 	if (btrfs_ino(inode) != BTRFS_FIRST_FREE_OBJECTID) {
202176dda93cSYan, Zheng 		err = -EINVAL;
202276dda93cSYan, Zheng 		goto out_dput;
202376dda93cSYan, Zheng 	}
202476dda93cSYan, Zheng 
202576dda93cSYan, Zheng 	mutex_lock(&inode->i_mutex);
202676dda93cSYan, Zheng 	err = d_invalidate(dentry);
202776dda93cSYan, Zheng 	if (err)
202876dda93cSYan, Zheng 		goto out_unlock;
202976dda93cSYan, Zheng 
203076dda93cSYan, Zheng 	down_write(&root->fs_info->subvol_sem);
203176dda93cSYan, Zheng 
203276dda93cSYan, Zheng 	err = may_destroy_subvol(dest);
203376dda93cSYan, Zheng 	if (err)
203476dda93cSYan, Zheng 		goto out_up_write;
203576dda93cSYan, Zheng 
2036a22285a6SYan, Zheng 	trans = btrfs_start_transaction(root, 0);
2037a22285a6SYan, Zheng 	if (IS_ERR(trans)) {
2038a22285a6SYan, Zheng 		err = PTR_ERR(trans);
2039d327099aSDan Carpenter 		goto out_up_write;
2040a22285a6SYan, Zheng 	}
2041a22285a6SYan, Zheng 	trans->block_rsv = &root->fs_info->global_block_rsv;
2042a22285a6SYan, Zheng 
204376dda93cSYan, Zheng 	ret = btrfs_unlink_subvol(trans, root, dir,
204476dda93cSYan, Zheng 				dest->root_key.objectid,
204576dda93cSYan, Zheng 				dentry->d_name.name,
204676dda93cSYan, Zheng 				dentry->d_name.len);
204779787eaaSJeff Mahoney 	if (ret) {
204879787eaaSJeff Mahoney 		err = ret;
204979787eaaSJeff Mahoney 		btrfs_abort_transaction(trans, root, ret);
205079787eaaSJeff Mahoney 		goto out_end_trans;
205179787eaaSJeff Mahoney 	}
205276dda93cSYan, Zheng 
205376dda93cSYan, Zheng 	btrfs_record_root_in_trans(trans, dest);
205476dda93cSYan, Zheng 
205576dda93cSYan, Zheng 	memset(&dest->root_item.drop_progress, 0,
205676dda93cSYan, Zheng 		sizeof(dest->root_item.drop_progress));
205776dda93cSYan, Zheng 	dest->root_item.drop_level = 0;
205876dda93cSYan, Zheng 	btrfs_set_root_refs(&dest->root_item, 0);
205976dda93cSYan, Zheng 
2060d68fc57bSYan, Zheng 	if (!xchg(&dest->orphan_item_inserted, 1)) {
206176dda93cSYan, Zheng 		ret = btrfs_insert_orphan_item(trans,
206276dda93cSYan, Zheng 					root->fs_info->tree_root,
206376dda93cSYan, Zheng 					dest->root_key.objectid);
206479787eaaSJeff Mahoney 		if (ret) {
206579787eaaSJeff Mahoney 			btrfs_abort_transaction(trans, root, ret);
206679787eaaSJeff Mahoney 			err = ret;
206779787eaaSJeff Mahoney 			goto out_end_trans;
2068d68fc57bSYan, Zheng 		}
206979787eaaSJeff Mahoney 	}
207079787eaaSJeff Mahoney out_end_trans:
2071531cb13fSSage Weil 	ret = btrfs_end_transaction(trans, root);
207279787eaaSJeff Mahoney 	if (ret && !err)
207379787eaaSJeff Mahoney 		err = ret;
207476dda93cSYan, Zheng 	inode->i_flags |= S_DEAD;
207576dda93cSYan, Zheng out_up_write:
207676dda93cSYan, Zheng 	up_write(&root->fs_info->subvol_sem);
207776dda93cSYan, Zheng out_unlock:
207876dda93cSYan, Zheng 	mutex_unlock(&inode->i_mutex);
207976dda93cSYan, Zheng 	if (!err) {
2080efefb143SYan, Zheng 		shrink_dcache_sb(root->fs_info->sb);
208176dda93cSYan, Zheng 		btrfs_invalidate_inodes(dest);
208276dda93cSYan, Zheng 		d_delete(dentry);
208376dda93cSYan, Zheng 	}
208476dda93cSYan, Zheng out_dput:
208576dda93cSYan, Zheng 	dput(dentry);
208676dda93cSYan, Zheng out_unlock_dir:
208776dda93cSYan, Zheng 	mutex_unlock(&dir->i_mutex);
20882a79f17eSAl Viro 	mnt_drop_write_file(file);
208976dda93cSYan, Zheng out:
209076dda93cSYan, Zheng 	kfree(vol_args);
209176dda93cSYan, Zheng 	return err;
209276dda93cSYan, Zheng }
209376dda93cSYan, Zheng 
20941e701a32SChris Mason static int btrfs_ioctl_defrag(struct file *file, void __user *argp)
2095f46b5a66SChristoph Hellwig {
2096f46b5a66SChristoph Hellwig 	struct inode *inode = fdentry(file)->d_inode;
2097f46b5a66SChristoph Hellwig 	struct btrfs_root *root = BTRFS_I(inode)->root;
20981e701a32SChris Mason 	struct btrfs_ioctl_defrag_range_args *range;
2099c146afadSYan Zheng 	int ret;
2100c146afadSYan Zheng 
2101b83cc969SLi Zefan 	if (btrfs_root_readonly(root))
2102b83cc969SLi Zefan 		return -EROFS;
2103b83cc969SLi Zefan 
2104a561be71SAl Viro 	ret = mnt_want_write_file(file);
2105c146afadSYan Zheng 	if (ret)
2106c146afadSYan Zheng 		return ret;
2107f46b5a66SChristoph Hellwig 
2108f46b5a66SChristoph Hellwig 	switch (inode->i_mode & S_IFMT) {
2109f46b5a66SChristoph Hellwig 	case S_IFDIR:
2110e441d54dSChris Mason 		if (!capable(CAP_SYS_ADMIN)) {
2111e441d54dSChris Mason 			ret = -EPERM;
2112e441d54dSChris Mason 			goto out;
2113e441d54dSChris Mason 		}
21148929ecfaSYan, Zheng 		ret = btrfs_defrag_root(root, 0);
21158929ecfaSYan, Zheng 		if (ret)
21168929ecfaSYan, Zheng 			goto out;
21178929ecfaSYan, Zheng 		ret = btrfs_defrag_root(root->fs_info->extent_root, 0);
2118f46b5a66SChristoph Hellwig 		break;
2119f46b5a66SChristoph Hellwig 	case S_IFREG:
2120e441d54dSChris Mason 		if (!(file->f_mode & FMODE_WRITE)) {
2121e441d54dSChris Mason 			ret = -EINVAL;
2122e441d54dSChris Mason 			goto out;
2123e441d54dSChris Mason 		}
21241e701a32SChris Mason 
21251e701a32SChris Mason 		range = kzalloc(sizeof(*range), GFP_KERNEL);
21261e701a32SChris Mason 		if (!range) {
21271e701a32SChris Mason 			ret = -ENOMEM;
21281e701a32SChris Mason 			goto out;
21291e701a32SChris Mason 		}
21301e701a32SChris Mason 
21311e701a32SChris Mason 		if (argp) {
21321e701a32SChris Mason 			if (copy_from_user(range, argp,
21331e701a32SChris Mason 					   sizeof(*range))) {
21341e701a32SChris Mason 				ret = -EFAULT;
21351e701a32SChris Mason 				kfree(range);
2136683be16eSDan Carpenter 				goto out;
21371e701a32SChris Mason 			}
21381e701a32SChris Mason 			/* compression requires us to start the IO */
21391e701a32SChris Mason 			if ((range->flags & BTRFS_DEFRAG_RANGE_COMPRESS)) {
21401e701a32SChris Mason 				range->flags |= BTRFS_DEFRAG_RANGE_START_IO;
21411e701a32SChris Mason 				range->extent_thresh = (u32)-1;
21421e701a32SChris Mason 			}
21431e701a32SChris Mason 		} else {
21441e701a32SChris Mason 			/* the rest are all set to zero by kzalloc */
21451e701a32SChris Mason 			range->len = (u64)-1;
21461e701a32SChris Mason 		}
21474cb5300bSChris Mason 		ret = btrfs_defrag_file(fdentry(file)->d_inode, file,
21484cb5300bSChris Mason 					range, 0, 0);
21494cb5300bSChris Mason 		if (ret > 0)
21504cb5300bSChris Mason 			ret = 0;
21511e701a32SChris Mason 		kfree(range);
2152f46b5a66SChristoph Hellwig 		break;
21538929ecfaSYan, Zheng 	default:
21548929ecfaSYan, Zheng 		ret = -EINVAL;
2155f46b5a66SChristoph Hellwig 	}
2156e441d54dSChris Mason out:
21572a79f17eSAl Viro 	mnt_drop_write_file(file);
2158e441d54dSChris Mason 	return ret;
2159f46b5a66SChristoph Hellwig }
2160f46b5a66SChristoph Hellwig 
2161b2950863SChristoph Hellwig static long btrfs_ioctl_add_dev(struct btrfs_root *root, void __user *arg)
2162f46b5a66SChristoph Hellwig {
2163f46b5a66SChristoph Hellwig 	struct btrfs_ioctl_vol_args *vol_args;
2164f46b5a66SChristoph Hellwig 	int ret;
2165f46b5a66SChristoph Hellwig 
2166e441d54dSChris Mason 	if (!capable(CAP_SYS_ADMIN))
2167e441d54dSChris Mason 		return -EPERM;
2168e441d54dSChris Mason 
2169c9e9f97bSIlya Dryomov 	mutex_lock(&root->fs_info->volume_mutex);
2170c9e9f97bSIlya Dryomov 	if (root->fs_info->balance_ctl) {
2171c9e9f97bSIlya Dryomov 		printk(KERN_INFO "btrfs: balance in progress\n");
2172c9e9f97bSIlya Dryomov 		ret = -EINVAL;
2173c9e9f97bSIlya Dryomov 		goto out;
2174c9e9f97bSIlya Dryomov 	}
2175c9e9f97bSIlya Dryomov 
2176dae7b665SLi Zefan 	vol_args = memdup_user(arg, sizeof(*vol_args));
2177c9e9f97bSIlya Dryomov 	if (IS_ERR(vol_args)) {
2178c9e9f97bSIlya Dryomov 		ret = PTR_ERR(vol_args);
2179c9e9f97bSIlya Dryomov 		goto out;
2180c9e9f97bSIlya Dryomov 	}
2181f46b5a66SChristoph Hellwig 
21825516e595SMark Fasheh 	vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
2183f46b5a66SChristoph Hellwig 	ret = btrfs_init_new_device(root, vol_args->name);
2184f46b5a66SChristoph Hellwig 
2185f46b5a66SChristoph Hellwig 	kfree(vol_args);
2186c9e9f97bSIlya Dryomov out:
2187c9e9f97bSIlya Dryomov 	mutex_unlock(&root->fs_info->volume_mutex);
2188f46b5a66SChristoph Hellwig 	return ret;
2189f46b5a66SChristoph Hellwig }
2190f46b5a66SChristoph Hellwig 
2191b2950863SChristoph Hellwig static long btrfs_ioctl_rm_dev(struct btrfs_root *root, void __user *arg)
2192f46b5a66SChristoph Hellwig {
2193f46b5a66SChristoph Hellwig 	struct btrfs_ioctl_vol_args *vol_args;
2194f46b5a66SChristoph Hellwig 	int ret;
2195f46b5a66SChristoph Hellwig 
2196e441d54dSChris Mason 	if (!capable(CAP_SYS_ADMIN))
2197e441d54dSChris Mason 		return -EPERM;
2198e441d54dSChris Mason 
2199c146afadSYan Zheng 	if (root->fs_info->sb->s_flags & MS_RDONLY)
2200c146afadSYan Zheng 		return -EROFS;
2201c146afadSYan Zheng 
2202c9e9f97bSIlya Dryomov 	mutex_lock(&root->fs_info->volume_mutex);
2203c9e9f97bSIlya Dryomov 	if (root->fs_info->balance_ctl) {
2204c9e9f97bSIlya Dryomov 		printk(KERN_INFO "btrfs: balance in progress\n");
2205c9e9f97bSIlya Dryomov 		ret = -EINVAL;
2206c9e9f97bSIlya Dryomov 		goto out;
2207c9e9f97bSIlya Dryomov 	}
2208c9e9f97bSIlya Dryomov 
2209dae7b665SLi Zefan 	vol_args = memdup_user(arg, sizeof(*vol_args));
2210c9e9f97bSIlya Dryomov 	if (IS_ERR(vol_args)) {
2211c9e9f97bSIlya Dryomov 		ret = PTR_ERR(vol_args);
2212c9e9f97bSIlya Dryomov 		goto out;
2213c9e9f97bSIlya Dryomov 	}
2214f46b5a66SChristoph Hellwig 
22155516e595SMark Fasheh 	vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
2216f46b5a66SChristoph Hellwig 	ret = btrfs_rm_device(root, vol_args->name);
2217f46b5a66SChristoph Hellwig 
2218f46b5a66SChristoph Hellwig 	kfree(vol_args);
2219c9e9f97bSIlya Dryomov out:
2220c9e9f97bSIlya Dryomov 	mutex_unlock(&root->fs_info->volume_mutex);
2221f46b5a66SChristoph Hellwig 	return ret;
2222f46b5a66SChristoph Hellwig }
2223f46b5a66SChristoph Hellwig 
2224475f6387SJan Schmidt static long btrfs_ioctl_fs_info(struct btrfs_root *root, void __user *arg)
2225475f6387SJan Schmidt {
2226027ed2f0SLi Zefan 	struct btrfs_ioctl_fs_info_args *fi_args;
2227475f6387SJan Schmidt 	struct btrfs_device *device;
2228475f6387SJan Schmidt 	struct btrfs_device *next;
2229475f6387SJan Schmidt 	struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices;
2230027ed2f0SLi Zefan 	int ret = 0;
2231475f6387SJan Schmidt 
2232475f6387SJan Schmidt 	if (!capable(CAP_SYS_ADMIN))
2233475f6387SJan Schmidt 		return -EPERM;
2234475f6387SJan Schmidt 
2235027ed2f0SLi Zefan 	fi_args = kzalloc(sizeof(*fi_args), GFP_KERNEL);
2236027ed2f0SLi Zefan 	if (!fi_args)
2237027ed2f0SLi Zefan 		return -ENOMEM;
2238027ed2f0SLi Zefan 
2239027ed2f0SLi Zefan 	fi_args->num_devices = fs_devices->num_devices;
2240027ed2f0SLi Zefan 	memcpy(&fi_args->fsid, root->fs_info->fsid, sizeof(fi_args->fsid));
2241475f6387SJan Schmidt 
2242475f6387SJan Schmidt 	mutex_lock(&fs_devices->device_list_mutex);
2243475f6387SJan Schmidt 	list_for_each_entry_safe(device, next, &fs_devices->devices, dev_list) {
2244027ed2f0SLi Zefan 		if (device->devid > fi_args->max_id)
2245027ed2f0SLi Zefan 			fi_args->max_id = device->devid;
2246475f6387SJan Schmidt 	}
2247475f6387SJan Schmidt 	mutex_unlock(&fs_devices->device_list_mutex);
2248475f6387SJan Schmidt 
2249027ed2f0SLi Zefan 	if (copy_to_user(arg, fi_args, sizeof(*fi_args)))
2250027ed2f0SLi Zefan 		ret = -EFAULT;
2251475f6387SJan Schmidt 
2252027ed2f0SLi Zefan 	kfree(fi_args);
2253027ed2f0SLi Zefan 	return ret;
2254475f6387SJan Schmidt }
2255475f6387SJan Schmidt 
2256475f6387SJan Schmidt static long btrfs_ioctl_dev_info(struct btrfs_root *root, void __user *arg)
2257475f6387SJan Schmidt {
2258475f6387SJan Schmidt 	struct btrfs_ioctl_dev_info_args *di_args;
2259475f6387SJan Schmidt 	struct btrfs_device *dev;
2260475f6387SJan Schmidt 	struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices;
2261475f6387SJan Schmidt 	int ret = 0;
2262475f6387SJan Schmidt 	char *s_uuid = NULL;
2263475f6387SJan Schmidt 	char empty_uuid[BTRFS_UUID_SIZE] = {0};
2264475f6387SJan Schmidt 
2265475f6387SJan Schmidt 	if (!capable(CAP_SYS_ADMIN))
2266475f6387SJan Schmidt 		return -EPERM;
2267475f6387SJan Schmidt 
2268475f6387SJan Schmidt 	di_args = memdup_user(arg, sizeof(*di_args));
2269475f6387SJan Schmidt 	if (IS_ERR(di_args))
2270475f6387SJan Schmidt 		return PTR_ERR(di_args);
2271475f6387SJan Schmidt 
2272475f6387SJan Schmidt 	if (memcmp(empty_uuid, di_args->uuid, BTRFS_UUID_SIZE) != 0)
2273475f6387SJan Schmidt 		s_uuid = di_args->uuid;
2274475f6387SJan Schmidt 
2275475f6387SJan Schmidt 	mutex_lock(&fs_devices->device_list_mutex);
2276475f6387SJan Schmidt 	dev = btrfs_find_device(root, di_args->devid, s_uuid, NULL);
2277475f6387SJan Schmidt 	mutex_unlock(&fs_devices->device_list_mutex);
2278475f6387SJan Schmidt 
2279475f6387SJan Schmidt 	if (!dev) {
2280475f6387SJan Schmidt 		ret = -ENODEV;
2281475f6387SJan Schmidt 		goto out;
2282475f6387SJan Schmidt 	}
2283475f6387SJan Schmidt 
2284475f6387SJan Schmidt 	di_args->devid = dev->devid;
2285475f6387SJan Schmidt 	di_args->bytes_used = dev->bytes_used;
2286475f6387SJan Schmidt 	di_args->total_bytes = dev->total_bytes;
2287475f6387SJan Schmidt 	memcpy(di_args->uuid, dev->uuid, sizeof(di_args->uuid));
2288a27202fbSJim Meyering 	if (dev->name) {
2289606686eeSJosef Bacik 		struct rcu_string *name;
2290606686eeSJosef Bacik 
2291606686eeSJosef Bacik 		rcu_read_lock();
2292606686eeSJosef Bacik 		name = rcu_dereference(dev->name);
2293606686eeSJosef Bacik 		strncpy(di_args->path, name->str, sizeof(di_args->path));
2294606686eeSJosef Bacik 		rcu_read_unlock();
2295a27202fbSJim Meyering 		di_args->path[sizeof(di_args->path) - 1] = 0;
2296a27202fbSJim Meyering 	} else {
229799ba55adSStefan Behrens 		di_args->path[0] = '\0';
2298a27202fbSJim Meyering 	}
2299475f6387SJan Schmidt 
2300475f6387SJan Schmidt out:
2301475f6387SJan Schmidt 	if (ret == 0 && copy_to_user(arg, di_args, sizeof(*di_args)))
2302475f6387SJan Schmidt 		ret = -EFAULT;
2303475f6387SJan Schmidt 
2304475f6387SJan Schmidt 	kfree(di_args);
2305475f6387SJan Schmidt 	return ret;
2306475f6387SJan Schmidt }
2307475f6387SJan Schmidt 
230876dda93cSYan, Zheng static noinline long btrfs_ioctl_clone(struct file *file, unsigned long srcfd,
2309b2950863SChristoph Hellwig 				       u64 off, u64 olen, u64 destoff)
2310f46b5a66SChristoph Hellwig {
2311f46b5a66SChristoph Hellwig 	struct inode *inode = fdentry(file)->d_inode;
2312f46b5a66SChristoph Hellwig 	struct btrfs_root *root = BTRFS_I(inode)->root;
2313f46b5a66SChristoph Hellwig 	struct file *src_file;
2314f46b5a66SChristoph Hellwig 	struct inode *src;
2315f46b5a66SChristoph Hellwig 	struct btrfs_trans_handle *trans;
2316f46b5a66SChristoph Hellwig 	struct btrfs_path *path;
2317f46b5a66SChristoph Hellwig 	struct extent_buffer *leaf;
2318ae01a0abSYan Zheng 	char *buf;
2319ae01a0abSYan Zheng 	struct btrfs_key key;
2320f46b5a66SChristoph Hellwig 	u32 nritems;
2321f46b5a66SChristoph Hellwig 	int slot;
2322ae01a0abSYan Zheng 	int ret;
2323c5c9cd4dSSage Weil 	u64 len = olen;
2324c5c9cd4dSSage Weil 	u64 bs = root->fs_info->sb->s_blocksize;
2325c5c9cd4dSSage Weil 	u64 hint_byte;
2326d20f7043SChris Mason 
2327c5c9cd4dSSage Weil 	/*
2328c5c9cd4dSSage Weil 	 * TODO:
2329c5c9cd4dSSage Weil 	 * - split compressed inline extents.  annoying: we need to
2330c5c9cd4dSSage Weil 	 *   decompress into destination's address_space (the file offset
2331c5c9cd4dSSage Weil 	 *   may change, so source mapping won't do), then recompress (or
2332c5c9cd4dSSage Weil 	 *   otherwise reinsert) a subrange.
2333c5c9cd4dSSage Weil 	 * - allow ranges within the same file to be cloned (provided
2334c5c9cd4dSSage Weil 	 *   they don't overlap)?
2335c5c9cd4dSSage Weil 	 */
2336c5c9cd4dSSage Weil 
2337e441d54dSChris Mason 	/* the destination must be opened for writing */
23382ebc3464SDan Rosenberg 	if (!(file->f_mode & FMODE_WRITE) || (file->f_flags & O_APPEND))
2339e441d54dSChris Mason 		return -EINVAL;
2340e441d54dSChris Mason 
2341b83cc969SLi Zefan 	if (btrfs_root_readonly(root))
2342b83cc969SLi Zefan 		return -EROFS;
2343b83cc969SLi Zefan 
2344a561be71SAl Viro 	ret = mnt_want_write_file(file);
2345c146afadSYan Zheng 	if (ret)
2346c146afadSYan Zheng 		return ret;
2347c146afadSYan Zheng 
2348c5c9cd4dSSage Weil 	src_file = fget(srcfd);
2349ab67b7c1SYan Zheng 	if (!src_file) {
2350ab67b7c1SYan Zheng 		ret = -EBADF;
2351ab67b7c1SYan Zheng 		goto out_drop_write;
2352ab67b7c1SYan Zheng 	}
23535dc64164SDan Rosenberg 
2354362a20c5SDavid Sterba 	ret = -EXDEV;
2355362a20c5SDavid Sterba 	if (src_file->f_path.mnt != file->f_path.mnt)
2356362a20c5SDavid Sterba 		goto out_fput;
2357362a20c5SDavid Sterba 
2358f46b5a66SChristoph Hellwig 	src = src_file->f_dentry->d_inode;
2359f46b5a66SChristoph Hellwig 
2360c5c9cd4dSSage Weil 	ret = -EINVAL;
2361c5c9cd4dSSage Weil 	if (src == inode)
2362c5c9cd4dSSage Weil 		goto out_fput;
2363c5c9cd4dSSage Weil 
23645dc64164SDan Rosenberg 	/* the src must be open for reading */
23655dc64164SDan Rosenberg 	if (!(src_file->f_mode & FMODE_READ))
23665dc64164SDan Rosenberg 		goto out_fput;
23675dc64164SDan Rosenberg 
23680e7b824cSLi Zefan 	/* don't make the dst file partly checksummed */
23690e7b824cSLi Zefan 	if ((BTRFS_I(src)->flags & BTRFS_INODE_NODATASUM) !=
23700e7b824cSLi Zefan 	    (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM))
23710e7b824cSLi Zefan 		goto out_fput;
23720e7b824cSLi Zefan 
2373ae01a0abSYan Zheng 	ret = -EISDIR;
2374ae01a0abSYan Zheng 	if (S_ISDIR(src->i_mode) || S_ISDIR(inode->i_mode))
2375f46b5a66SChristoph Hellwig 		goto out_fput;
2376f46b5a66SChristoph Hellwig 
2377ae01a0abSYan Zheng 	ret = -EXDEV;
2378362a20c5SDavid Sterba 	if (src->i_sb != inode->i_sb)
2379ae01a0abSYan Zheng 		goto out_fput;
2380ae01a0abSYan Zheng 
2381ae01a0abSYan Zheng 	ret = -ENOMEM;
2382ae01a0abSYan Zheng 	buf = vmalloc(btrfs_level_size(root, 0));
2383ae01a0abSYan Zheng 	if (!buf)
2384ae01a0abSYan Zheng 		goto out_fput;
2385ae01a0abSYan Zheng 
2386ae01a0abSYan Zheng 	path = btrfs_alloc_path();
2387ae01a0abSYan Zheng 	if (!path) {
2388ae01a0abSYan Zheng 		vfree(buf);
2389ae01a0abSYan Zheng 		goto out_fput;
2390ae01a0abSYan Zheng 	}
2391ae01a0abSYan Zheng 	path->reada = 2;
2392ae01a0abSYan Zheng 
2393f46b5a66SChristoph Hellwig 	if (inode < src) {
2394fccdae43SSage Weil 		mutex_lock_nested(&inode->i_mutex, I_MUTEX_PARENT);
2395fccdae43SSage Weil 		mutex_lock_nested(&src->i_mutex, I_MUTEX_CHILD);
2396f46b5a66SChristoph Hellwig 	} else {
2397fccdae43SSage Weil 		mutex_lock_nested(&src->i_mutex, I_MUTEX_PARENT);
2398fccdae43SSage Weil 		mutex_lock_nested(&inode->i_mutex, I_MUTEX_CHILD);
2399f46b5a66SChristoph Hellwig 	}
2400f46b5a66SChristoph Hellwig 
2401c5c9cd4dSSage Weil 	/* determine range to clone */
2402c5c9cd4dSSage Weil 	ret = -EINVAL;
24032ebc3464SDan Rosenberg 	if (off + len > src->i_size || off + len < off)
2404f46b5a66SChristoph Hellwig 		goto out_unlock;
2405c5c9cd4dSSage Weil 	if (len == 0)
2406c5c9cd4dSSage Weil 		olen = len = src->i_size - off;
2407c5c9cd4dSSage Weil 	/* if we extend to eof, continue to block boundary */
2408c5c9cd4dSSage Weil 	if (off + len == src->i_size)
24092a6b8daeSLi Zefan 		len = ALIGN(src->i_size, bs) - off;
2410c5c9cd4dSSage Weil 
2411c5c9cd4dSSage Weil 	/* verify the end result is block aligned */
24122a6b8daeSLi Zefan 	if (!IS_ALIGNED(off, bs) || !IS_ALIGNED(off + len, bs) ||
24132a6b8daeSLi Zefan 	    !IS_ALIGNED(destoff, bs))
2414c5c9cd4dSSage Weil 		goto out_unlock;
2415c5c9cd4dSSage Weil 
2416d525e8abSLi Zefan 	if (destoff > inode->i_size) {
2417d525e8abSLi Zefan 		ret = btrfs_cont_expand(inode, inode->i_size, destoff);
2418d525e8abSLi Zefan 		if (ret)
2419d525e8abSLi Zefan 			goto out_unlock;
2420d525e8abSLi Zefan 	}
2421d525e8abSLi Zefan 
242271ef0786SLi Zefan 	/* truncate page cache pages from target inode range */
242371ef0786SLi Zefan 	truncate_inode_pages_range(&inode->i_data, destoff,
242471ef0786SLi Zefan 				   PAGE_CACHE_ALIGN(destoff + len) - 1);
242571ef0786SLi Zefan 
2426f46b5a66SChristoph Hellwig 	/* do any pending delalloc/csum calc on src, one way or
2427f46b5a66SChristoph Hellwig 	   another, and lock file content */
2428f46b5a66SChristoph Hellwig 	while (1) {
242931840ae1SZheng Yan 		struct btrfs_ordered_extent *ordered;
2430d0082371SJeff Mahoney 		lock_extent(&BTRFS_I(src)->io_tree, off, off+len);
24319a019196SSage Weil 		ordered = btrfs_lookup_first_ordered_extent(src, off+len);
24329a019196SSage Weil 		if (!ordered &&
24339a019196SSage Weil 		    !test_range_bit(&BTRFS_I(src)->io_tree, off, off+len,
24349a019196SSage Weil 				   EXTENT_DELALLOC, 0, NULL))
2435f46b5a66SChristoph Hellwig 			break;
2436d0082371SJeff Mahoney 		unlock_extent(&BTRFS_I(src)->io_tree, off, off+len);
2437ae01a0abSYan Zheng 		if (ordered)
2438ae01a0abSYan Zheng 			btrfs_put_ordered_extent(ordered);
24399a019196SSage Weil 		btrfs_wait_ordered_range(src, off, len);
2440f46b5a66SChristoph Hellwig 	}
2441f46b5a66SChristoph Hellwig 
2442c5c9cd4dSSage Weil 	/* clone data */
244333345d01SLi Zefan 	key.objectid = btrfs_ino(src);
2444ae01a0abSYan Zheng 	key.type = BTRFS_EXTENT_DATA_KEY;
2445ae01a0abSYan Zheng 	key.offset = 0;
2446f46b5a66SChristoph Hellwig 
2447f46b5a66SChristoph Hellwig 	while (1) {
2448f46b5a66SChristoph Hellwig 		/*
2449f46b5a66SChristoph Hellwig 		 * note the key will change type as we walk through the
2450f46b5a66SChristoph Hellwig 		 * tree.
2451f46b5a66SChristoph Hellwig 		 */
2452362a20c5SDavid Sterba 		ret = btrfs_search_slot(NULL, BTRFS_I(src)->root, &key, path,
2453362a20c5SDavid Sterba 				0, 0);
2454f46b5a66SChristoph Hellwig 		if (ret < 0)
2455f46b5a66SChristoph Hellwig 			goto out;
2456f46b5a66SChristoph Hellwig 
2457ae01a0abSYan Zheng 		nritems = btrfs_header_nritems(path->nodes[0]);
2458ae01a0abSYan Zheng 		if (path->slots[0] >= nritems) {
2459362a20c5SDavid Sterba 			ret = btrfs_next_leaf(BTRFS_I(src)->root, path);
2460f46b5a66SChristoph Hellwig 			if (ret < 0)
2461f46b5a66SChristoph Hellwig 				goto out;
2462f46b5a66SChristoph Hellwig 			if (ret > 0)
2463f46b5a66SChristoph Hellwig 				break;
2464ae01a0abSYan Zheng 			nritems = btrfs_header_nritems(path->nodes[0]);
2465f46b5a66SChristoph Hellwig 		}
2466f46b5a66SChristoph Hellwig 		leaf = path->nodes[0];
2467f46b5a66SChristoph Hellwig 		slot = path->slots[0];
2468f46b5a66SChristoph Hellwig 
2469ae01a0abSYan Zheng 		btrfs_item_key_to_cpu(leaf, &key, slot);
2470d20f7043SChris Mason 		if (btrfs_key_type(&key) > BTRFS_EXTENT_DATA_KEY ||
247133345d01SLi Zefan 		    key.objectid != btrfs_ino(src))
2472f46b5a66SChristoph Hellwig 			break;
2473f46b5a66SChristoph Hellwig 
2474c5c9cd4dSSage Weil 		if (btrfs_key_type(&key) == BTRFS_EXTENT_DATA_KEY) {
2475c5c9cd4dSSage Weil 			struct btrfs_file_extent_item *extent;
2476c5c9cd4dSSage Weil 			int type;
247731840ae1SZheng Yan 			u32 size;
247831840ae1SZheng Yan 			struct btrfs_key new_key;
2479c5c9cd4dSSage Weil 			u64 disko = 0, diskl = 0;
2480c5c9cd4dSSage Weil 			u64 datao = 0, datal = 0;
2481c5c9cd4dSSage Weil 			u8 comp;
2482b5384d48SSage Weil 			u64 endoff;
248331840ae1SZheng Yan 
248431840ae1SZheng Yan 			size = btrfs_item_size_nr(leaf, slot);
248531840ae1SZheng Yan 			read_extent_buffer(leaf, buf,
248631840ae1SZheng Yan 					   btrfs_item_ptr_offset(leaf, slot),
248731840ae1SZheng Yan 					   size);
2488c5c9cd4dSSage Weil 
2489c5c9cd4dSSage Weil 			extent = btrfs_item_ptr(leaf, slot,
2490c5c9cd4dSSage Weil 						struct btrfs_file_extent_item);
2491c5c9cd4dSSage Weil 			comp = btrfs_file_extent_compression(leaf, extent);
2492c5c9cd4dSSage Weil 			type = btrfs_file_extent_type(leaf, extent);
2493c8a894d7SChris Mason 			if (type == BTRFS_FILE_EXTENT_REG ||
2494c8a894d7SChris Mason 			    type == BTRFS_FILE_EXTENT_PREALLOC) {
2495d397712bSChris Mason 				disko = btrfs_file_extent_disk_bytenr(leaf,
2496d397712bSChris Mason 								      extent);
2497d397712bSChris Mason 				diskl = btrfs_file_extent_disk_num_bytes(leaf,
2498d397712bSChris Mason 								 extent);
2499c5c9cd4dSSage Weil 				datao = btrfs_file_extent_offset(leaf, extent);
2500d397712bSChris Mason 				datal = btrfs_file_extent_num_bytes(leaf,
2501d397712bSChris Mason 								    extent);
2502c5c9cd4dSSage Weil 			} else if (type == BTRFS_FILE_EXTENT_INLINE) {
2503c5c9cd4dSSage Weil 				/* take upper bound, may be compressed */
2504c5c9cd4dSSage Weil 				datal = btrfs_file_extent_ram_bytes(leaf,
2505c5c9cd4dSSage Weil 								    extent);
2506c5c9cd4dSSage Weil 			}
2507b3b4aa74SDavid Sterba 			btrfs_release_path(path);
250831840ae1SZheng Yan 
2509050006a7SSage Weil 			if (key.offset + datal <= off ||
2510c5c9cd4dSSage Weil 			    key.offset >= off+len)
2511c5c9cd4dSSage Weil 				goto next;
2512c5c9cd4dSSage Weil 
251331840ae1SZheng Yan 			memcpy(&new_key, &key, sizeof(new_key));
251433345d01SLi Zefan 			new_key.objectid = btrfs_ino(inode);
25154d728ec7SLi Zefan 			if (off <= key.offset)
2516c5c9cd4dSSage Weil 				new_key.offset = key.offset + destoff - off;
25174d728ec7SLi Zefan 			else
25184d728ec7SLi Zefan 				new_key.offset = destoff;
2519c5c9cd4dSSage Weil 
2520b6f3409bSSage Weil 			/*
2521b6f3409bSSage Weil 			 * 1 - adjusting old extent (we may have to split it)
2522b6f3409bSSage Weil 			 * 1 - add new extent
2523b6f3409bSSage Weil 			 * 1 - inode update
2524b6f3409bSSage Weil 			 */
2525b6f3409bSSage Weil 			trans = btrfs_start_transaction(root, 3);
2526a22285a6SYan, Zheng 			if (IS_ERR(trans)) {
2527a22285a6SYan, Zheng 				ret = PTR_ERR(trans);
2528a22285a6SYan, Zheng 				goto out;
2529a22285a6SYan, Zheng 			}
2530a22285a6SYan, Zheng 
2531c8a894d7SChris Mason 			if (type == BTRFS_FILE_EXTENT_REG ||
2532c8a894d7SChris Mason 			    type == BTRFS_FILE_EXTENT_PREALLOC) {
2533d72c0842SLi Zefan 				/*
2534d72c0842SLi Zefan 				 *    a  | --- range to clone ---|  b
2535d72c0842SLi Zefan 				 * | ------------- extent ------------- |
2536d72c0842SLi Zefan 				 */
2537d72c0842SLi Zefan 
2538d72c0842SLi Zefan 				/* substract range b */
2539d72c0842SLi Zefan 				if (key.offset + datal > off + len)
2540d72c0842SLi Zefan 					datal = off + len - key.offset;
2541d72c0842SLi Zefan 
2542d72c0842SLi Zefan 				/* substract range a */
2543a22285a6SYan, Zheng 				if (off > key.offset) {
2544a22285a6SYan, Zheng 					datao += off - key.offset;
2545a22285a6SYan, Zheng 					datal -= off - key.offset;
2546a22285a6SYan, Zheng 				}
2547a22285a6SYan, Zheng 
2548a22285a6SYan, Zheng 				ret = btrfs_drop_extents(trans, inode,
2549a22285a6SYan, Zheng 							 new_key.offset,
2550a22285a6SYan, Zheng 							 new_key.offset + datal,
2551a22285a6SYan, Zheng 							 &hint_byte, 1);
255279787eaaSJeff Mahoney 				if (ret) {
255379787eaaSJeff Mahoney 					btrfs_abort_transaction(trans, root,
255479787eaaSJeff Mahoney 								ret);
255579787eaaSJeff Mahoney 					btrfs_end_transaction(trans, root);
255679787eaaSJeff Mahoney 					goto out;
255779787eaaSJeff Mahoney 				}
2558a22285a6SYan, Zheng 
255931840ae1SZheng Yan 				ret = btrfs_insert_empty_item(trans, root, path,
256031840ae1SZheng Yan 							      &new_key, size);
256179787eaaSJeff Mahoney 				if (ret) {
256279787eaaSJeff Mahoney 					btrfs_abort_transaction(trans, root,
256379787eaaSJeff Mahoney 								ret);
256479787eaaSJeff Mahoney 					btrfs_end_transaction(trans, root);
256579787eaaSJeff Mahoney 					goto out;
256679787eaaSJeff Mahoney 				}
256731840ae1SZheng Yan 
256831840ae1SZheng Yan 				leaf = path->nodes[0];
256931840ae1SZheng Yan 				slot = path->slots[0];
257031840ae1SZheng Yan 				write_extent_buffer(leaf, buf,
257131840ae1SZheng Yan 					    btrfs_item_ptr_offset(leaf, slot),
257231840ae1SZheng Yan 					    size);
2573ae01a0abSYan Zheng 
2574f46b5a66SChristoph Hellwig 				extent = btrfs_item_ptr(leaf, slot,
2575f46b5a66SChristoph Hellwig 						struct btrfs_file_extent_item);
2576c5c9cd4dSSage Weil 
2577c5c9cd4dSSage Weil 				/* disko == 0 means it's a hole */
2578c5c9cd4dSSage Weil 				if (!disko)
2579c5c9cd4dSSage Weil 					datao = 0;
2580c5c9cd4dSSage Weil 
2581c5c9cd4dSSage Weil 				btrfs_set_file_extent_offset(leaf, extent,
2582c5c9cd4dSSage Weil 							     datao);
2583c5c9cd4dSSage Weil 				btrfs_set_file_extent_num_bytes(leaf, extent,
2584c5c9cd4dSSage Weil 								datal);
2585c5c9cd4dSSage Weil 				if (disko) {
2586c5c9cd4dSSage Weil 					inode_add_bytes(inode, datal);
2587ae01a0abSYan Zheng 					ret = btrfs_inc_extent_ref(trans, root,
25885d4f98a2SYan Zheng 							disko, diskl, 0,
2589f46b5a66SChristoph Hellwig 							root->root_key.objectid,
259033345d01SLi Zefan 							btrfs_ino(inode),
259166d7e7f0SArne Jansen 							new_key.offset - datao,
259266d7e7f0SArne Jansen 							0);
259379787eaaSJeff Mahoney 					if (ret) {
259479787eaaSJeff Mahoney 						btrfs_abort_transaction(trans,
259579787eaaSJeff Mahoney 									root,
259679787eaaSJeff Mahoney 									ret);
259779787eaaSJeff Mahoney 						btrfs_end_transaction(trans,
259879787eaaSJeff Mahoney 								      root);
259979787eaaSJeff Mahoney 						goto out;
260079787eaaSJeff Mahoney 
260179787eaaSJeff Mahoney 					}
2602f46b5a66SChristoph Hellwig 				}
2603c5c9cd4dSSage Weil 			} else if (type == BTRFS_FILE_EXTENT_INLINE) {
2604c5c9cd4dSSage Weil 				u64 skip = 0;
2605c5c9cd4dSSage Weil 				u64 trim = 0;
2606c5c9cd4dSSage Weil 				if (off > key.offset) {
2607c5c9cd4dSSage Weil 					skip = off - key.offset;
2608c5c9cd4dSSage Weil 					new_key.offset += skip;
260931840ae1SZheng Yan 				}
2610d397712bSChris Mason 
2611c5c9cd4dSSage Weil 				if (key.offset + datal > off+len)
2612c5c9cd4dSSage Weil 					trim = key.offset + datal - (off+len);
2613d397712bSChris Mason 
2614c5c9cd4dSSage Weil 				if (comp && (skip || trim)) {
2615c5c9cd4dSSage Weil 					ret = -EINVAL;
2616a22285a6SYan, Zheng 					btrfs_end_transaction(trans, root);
2617c5c9cd4dSSage Weil 					goto out;
261831840ae1SZheng Yan 				}
2619c5c9cd4dSSage Weil 				size -= skip + trim;
2620c5c9cd4dSSage Weil 				datal -= skip + trim;
2621a22285a6SYan, Zheng 
2622a22285a6SYan, Zheng 				ret = btrfs_drop_extents(trans, inode,
2623a22285a6SYan, Zheng 							 new_key.offset,
2624a22285a6SYan, Zheng 							 new_key.offset + datal,
2625a22285a6SYan, Zheng 							 &hint_byte, 1);
262679787eaaSJeff Mahoney 				if (ret) {
262779787eaaSJeff Mahoney 					btrfs_abort_transaction(trans, root,
262879787eaaSJeff Mahoney 								ret);
262979787eaaSJeff Mahoney 					btrfs_end_transaction(trans, root);
263079787eaaSJeff Mahoney 					goto out;
263179787eaaSJeff Mahoney 				}
2632a22285a6SYan, Zheng 
2633c5c9cd4dSSage Weil 				ret = btrfs_insert_empty_item(trans, root, path,
2634c5c9cd4dSSage Weil 							      &new_key, size);
263579787eaaSJeff Mahoney 				if (ret) {
263679787eaaSJeff Mahoney 					btrfs_abort_transaction(trans, root,
263779787eaaSJeff Mahoney 								ret);
263879787eaaSJeff Mahoney 					btrfs_end_transaction(trans, root);
263979787eaaSJeff Mahoney 					goto out;
264079787eaaSJeff Mahoney 				}
2641c5c9cd4dSSage Weil 
2642c5c9cd4dSSage Weil 				if (skip) {
2643d397712bSChris Mason 					u32 start =
2644d397712bSChris Mason 					  btrfs_file_extent_calc_inline_size(0);
2645c5c9cd4dSSage Weil 					memmove(buf+start, buf+start+skip,
2646c5c9cd4dSSage Weil 						datal);
2647c5c9cd4dSSage Weil 				}
2648c5c9cd4dSSage Weil 
2649c5c9cd4dSSage Weil 				leaf = path->nodes[0];
2650c5c9cd4dSSage Weil 				slot = path->slots[0];
2651c5c9cd4dSSage Weil 				write_extent_buffer(leaf, buf,
2652c5c9cd4dSSage Weil 					    btrfs_item_ptr_offset(leaf, slot),
2653c5c9cd4dSSage Weil 					    size);
2654c5c9cd4dSSage Weil 				inode_add_bytes(inode, datal);
2655c5c9cd4dSSage Weil 			}
2656c5c9cd4dSSage Weil 
2657c5c9cd4dSSage Weil 			btrfs_mark_buffer_dirty(leaf);
2658b3b4aa74SDavid Sterba 			btrfs_release_path(path);
2659c5c9cd4dSSage Weil 
26600c4d2d95SJosef Bacik 			inode_inc_iversion(inode);
2661a22285a6SYan, Zheng 			inode->i_mtime = inode->i_ctime = CURRENT_TIME;
2662b5384d48SSage Weil 
2663b5384d48SSage Weil 			/*
2664b5384d48SSage Weil 			 * we round up to the block size at eof when
2665b5384d48SSage Weil 			 * determining which extents to clone above,
2666b5384d48SSage Weil 			 * but shouldn't round up the file size
2667b5384d48SSage Weil 			 */
2668b5384d48SSage Weil 			endoff = new_key.offset + datal;
26695f3888ffSLi Zefan 			if (endoff > destoff+olen)
26705f3888ffSLi Zefan 				endoff = destoff+olen;
2671b5384d48SSage Weil 			if (endoff > inode->i_size)
2672b5384d48SSage Weil 				btrfs_i_size_write(inode, endoff);
2673b5384d48SSage Weil 
2674a22285a6SYan, Zheng 			ret = btrfs_update_inode(trans, root, inode);
267579787eaaSJeff Mahoney 			if (ret) {
267679787eaaSJeff Mahoney 				btrfs_abort_transaction(trans, root, ret);
2677a22285a6SYan, Zheng 				btrfs_end_transaction(trans, root);
267879787eaaSJeff Mahoney 				goto out;
267979787eaaSJeff Mahoney 			}
268079787eaaSJeff Mahoney 			ret = btrfs_end_transaction(trans, root);
2681a22285a6SYan, Zheng 		}
2682c5c9cd4dSSage Weil next:
2683b3b4aa74SDavid Sterba 		btrfs_release_path(path);
2684ae01a0abSYan Zheng 		key.offset++;
2685ae01a0abSYan Zheng 	}
2686f46b5a66SChristoph Hellwig 	ret = 0;
2687f46b5a66SChristoph Hellwig out:
2688b3b4aa74SDavid Sterba 	btrfs_release_path(path);
2689d0082371SJeff Mahoney 	unlock_extent(&BTRFS_I(src)->io_tree, off, off+len);
2690f46b5a66SChristoph Hellwig out_unlock:
2691f46b5a66SChristoph Hellwig 	mutex_unlock(&src->i_mutex);
2692f46b5a66SChristoph Hellwig 	mutex_unlock(&inode->i_mutex);
2693ae01a0abSYan Zheng 	vfree(buf);
2694ae01a0abSYan Zheng 	btrfs_free_path(path);
2695f46b5a66SChristoph Hellwig out_fput:
2696f46b5a66SChristoph Hellwig 	fput(src_file);
2697ab67b7c1SYan Zheng out_drop_write:
26982a79f17eSAl Viro 	mnt_drop_write_file(file);
2699f46b5a66SChristoph Hellwig 	return ret;
2700f46b5a66SChristoph Hellwig }
2701f46b5a66SChristoph Hellwig 
27027a865e8aSChristoph Hellwig static long btrfs_ioctl_clone_range(struct file *file, void __user *argp)
2703c5c9cd4dSSage Weil {
2704c5c9cd4dSSage Weil 	struct btrfs_ioctl_clone_range_args args;
2705c5c9cd4dSSage Weil 
27067a865e8aSChristoph Hellwig 	if (copy_from_user(&args, argp, sizeof(args)))
2707c5c9cd4dSSage Weil 		return -EFAULT;
2708c5c9cd4dSSage Weil 	return btrfs_ioctl_clone(file, args.src_fd, args.src_offset,
2709c5c9cd4dSSage Weil 				 args.src_length, args.dest_offset);
2710c5c9cd4dSSage Weil }
2711c5c9cd4dSSage Weil 
2712f46b5a66SChristoph Hellwig /*
2713f46b5a66SChristoph Hellwig  * there are many ways the trans_start and trans_end ioctls can lead
2714f46b5a66SChristoph Hellwig  * to deadlocks.  They should only be used by applications that
2715f46b5a66SChristoph Hellwig  * basically own the machine, and have a very in depth understanding
2716f46b5a66SChristoph Hellwig  * of all the possible deadlocks and enospc problems.
2717f46b5a66SChristoph Hellwig  */
2718b2950863SChristoph Hellwig static long btrfs_ioctl_trans_start(struct file *file)
2719f46b5a66SChristoph Hellwig {
2720f46b5a66SChristoph Hellwig 	struct inode *inode = fdentry(file)->d_inode;
2721f46b5a66SChristoph Hellwig 	struct btrfs_root *root = BTRFS_I(inode)->root;
2722f46b5a66SChristoph Hellwig 	struct btrfs_trans_handle *trans;
27231ab86aedSSage Weil 	int ret;
2724f46b5a66SChristoph Hellwig 
27251ab86aedSSage Weil 	ret = -EPERM;
2726df5b5520SChristoph Hellwig 	if (!capable(CAP_SYS_ADMIN))
2727f46b5a66SChristoph Hellwig 		goto out;
27281ab86aedSSage Weil 
27291ab86aedSSage Weil 	ret = -EINPROGRESS;
27301ab86aedSSage Weil 	if (file->private_data)
27311ab86aedSSage Weil 		goto out;
27329ca9ee09SSage Weil 
2733b83cc969SLi Zefan 	ret = -EROFS;
2734b83cc969SLi Zefan 	if (btrfs_root_readonly(root))
2735b83cc969SLi Zefan 		goto out;
2736b83cc969SLi Zefan 
2737a561be71SAl Viro 	ret = mnt_want_write_file(file);
2738c146afadSYan Zheng 	if (ret)
2739c146afadSYan Zheng 		goto out;
2740c146afadSYan Zheng 
2741a4abeea4SJosef Bacik 	atomic_inc(&root->fs_info->open_ioctl_trans);
27429ca9ee09SSage Weil 
2743f46b5a66SChristoph Hellwig 	ret = -ENOMEM;
27447a7eaa40SJosef Bacik 	trans = btrfs_start_ioctl_transaction(root);
2745abd30bb0STsutomu Itoh 	if (IS_ERR(trans))
27461ab86aedSSage Weil 		goto out_drop;
27471ab86aedSSage Weil 
27481ab86aedSSage Weil 	file->private_data = trans;
27491ab86aedSSage Weil 	return 0;
27501ab86aedSSage Weil 
27511ab86aedSSage Weil out_drop:
2752a4abeea4SJosef Bacik 	atomic_dec(&root->fs_info->open_ioctl_trans);
27532a79f17eSAl Viro 	mnt_drop_write_file(file);
2754f46b5a66SChristoph Hellwig out:
2755f46b5a66SChristoph Hellwig 	return ret;
2756f46b5a66SChristoph Hellwig }
2757f46b5a66SChristoph Hellwig 
27586ef5ed0dSJosef Bacik static long btrfs_ioctl_default_subvol(struct file *file, void __user *argp)
27596ef5ed0dSJosef Bacik {
27606ef5ed0dSJosef Bacik 	struct inode *inode = fdentry(file)->d_inode;
27616ef5ed0dSJosef Bacik 	struct btrfs_root *root = BTRFS_I(inode)->root;
27626ef5ed0dSJosef Bacik 	struct btrfs_root *new_root;
27636ef5ed0dSJosef Bacik 	struct btrfs_dir_item *di;
27646ef5ed0dSJosef Bacik 	struct btrfs_trans_handle *trans;
27656ef5ed0dSJosef Bacik 	struct btrfs_path *path;
27666ef5ed0dSJosef Bacik 	struct btrfs_key location;
27676ef5ed0dSJosef Bacik 	struct btrfs_disk_key disk_key;
27686ef5ed0dSJosef Bacik 	struct btrfs_super_block *disk_super;
27696ef5ed0dSJosef Bacik 	u64 features;
27706ef5ed0dSJosef Bacik 	u64 objectid = 0;
27716ef5ed0dSJosef Bacik 	u64 dir_id;
27726ef5ed0dSJosef Bacik 
27736ef5ed0dSJosef Bacik 	if (!capable(CAP_SYS_ADMIN))
27746ef5ed0dSJosef Bacik 		return -EPERM;
27756ef5ed0dSJosef Bacik 
27766ef5ed0dSJosef Bacik 	if (copy_from_user(&objectid, argp, sizeof(objectid)))
27776ef5ed0dSJosef Bacik 		return -EFAULT;
27786ef5ed0dSJosef Bacik 
27796ef5ed0dSJosef Bacik 	if (!objectid)
27806ef5ed0dSJosef Bacik 		objectid = root->root_key.objectid;
27816ef5ed0dSJosef Bacik 
27826ef5ed0dSJosef Bacik 	location.objectid = objectid;
27836ef5ed0dSJosef Bacik 	location.type = BTRFS_ROOT_ITEM_KEY;
27846ef5ed0dSJosef Bacik 	location.offset = (u64)-1;
27856ef5ed0dSJosef Bacik 
27866ef5ed0dSJosef Bacik 	new_root = btrfs_read_fs_root_no_name(root->fs_info, &location);
27876ef5ed0dSJosef Bacik 	if (IS_ERR(new_root))
27886ef5ed0dSJosef Bacik 		return PTR_ERR(new_root);
27896ef5ed0dSJosef Bacik 
27906ef5ed0dSJosef Bacik 	if (btrfs_root_refs(&new_root->root_item) == 0)
27916ef5ed0dSJosef Bacik 		return -ENOENT;
27926ef5ed0dSJosef Bacik 
27936ef5ed0dSJosef Bacik 	path = btrfs_alloc_path();
27946ef5ed0dSJosef Bacik 	if (!path)
27956ef5ed0dSJosef Bacik 		return -ENOMEM;
27966ef5ed0dSJosef Bacik 	path->leave_spinning = 1;
27976ef5ed0dSJosef Bacik 
27986ef5ed0dSJosef Bacik 	trans = btrfs_start_transaction(root, 1);
279998d5dc13STsutomu Itoh 	if (IS_ERR(trans)) {
28006ef5ed0dSJosef Bacik 		btrfs_free_path(path);
280198d5dc13STsutomu Itoh 		return PTR_ERR(trans);
28026ef5ed0dSJosef Bacik 	}
28036ef5ed0dSJosef Bacik 
28046c41761fSDavid Sterba 	dir_id = btrfs_super_root_dir(root->fs_info->super_copy);
28056ef5ed0dSJosef Bacik 	di = btrfs_lookup_dir_item(trans, root->fs_info->tree_root, path,
28066ef5ed0dSJosef Bacik 				   dir_id, "default", 7, 1);
2807cf1e99a4SDan Carpenter 	if (IS_ERR_OR_NULL(di)) {
28086ef5ed0dSJosef Bacik 		btrfs_free_path(path);
28096ef5ed0dSJosef Bacik 		btrfs_end_transaction(trans, root);
28106ef5ed0dSJosef Bacik 		printk(KERN_ERR "Umm, you don't have the default dir item, "
28116ef5ed0dSJosef Bacik 		       "this isn't going to work\n");
28126ef5ed0dSJosef Bacik 		return -ENOENT;
28136ef5ed0dSJosef Bacik 	}
28146ef5ed0dSJosef Bacik 
28156ef5ed0dSJosef Bacik 	btrfs_cpu_key_to_disk(&disk_key, &new_root->root_key);
28166ef5ed0dSJosef Bacik 	btrfs_set_dir_item_key(path->nodes[0], di, &disk_key);
28176ef5ed0dSJosef Bacik 	btrfs_mark_buffer_dirty(path->nodes[0]);
28186ef5ed0dSJosef Bacik 	btrfs_free_path(path);
28196ef5ed0dSJosef Bacik 
28206c41761fSDavid Sterba 	disk_super = root->fs_info->super_copy;
28216ef5ed0dSJosef Bacik 	features = btrfs_super_incompat_flags(disk_super);
28226ef5ed0dSJosef Bacik 	if (!(features & BTRFS_FEATURE_INCOMPAT_DEFAULT_SUBVOL)) {
28236ef5ed0dSJosef Bacik 		features |= BTRFS_FEATURE_INCOMPAT_DEFAULT_SUBVOL;
28246ef5ed0dSJosef Bacik 		btrfs_set_super_incompat_flags(disk_super, features);
28256ef5ed0dSJosef Bacik 	}
28266ef5ed0dSJosef Bacik 	btrfs_end_transaction(trans, root);
28276ef5ed0dSJosef Bacik 
28286ef5ed0dSJosef Bacik 	return 0;
28296ef5ed0dSJosef Bacik }
28306ef5ed0dSJosef Bacik 
2831bf5fc093SJosef Bacik static void get_block_group_info(struct list_head *groups_list,
2832bf5fc093SJosef Bacik 				 struct btrfs_ioctl_space_info *space)
2833bf5fc093SJosef Bacik {
2834bf5fc093SJosef Bacik 	struct btrfs_block_group_cache *block_group;
2835bf5fc093SJosef Bacik 
2836bf5fc093SJosef Bacik 	space->total_bytes = 0;
2837bf5fc093SJosef Bacik 	space->used_bytes = 0;
2838bf5fc093SJosef Bacik 	space->flags = 0;
2839bf5fc093SJosef Bacik 	list_for_each_entry(block_group, groups_list, list) {
2840bf5fc093SJosef Bacik 		space->flags = block_group->flags;
2841bf5fc093SJosef Bacik 		space->total_bytes += block_group->key.offset;
2842bf5fc093SJosef Bacik 		space->used_bytes +=
2843bf5fc093SJosef Bacik 			btrfs_block_group_used(&block_group->item);
2844bf5fc093SJosef Bacik 	}
2845bf5fc093SJosef Bacik }
2846bf5fc093SJosef Bacik 
28471406e432SJosef Bacik long btrfs_ioctl_space_info(struct btrfs_root *root, void __user *arg)
28481406e432SJosef Bacik {
28491406e432SJosef Bacik 	struct btrfs_ioctl_space_args space_args;
28501406e432SJosef Bacik 	struct btrfs_ioctl_space_info space;
28511406e432SJosef Bacik 	struct btrfs_ioctl_space_info *dest;
28527fde62bfSChris Mason 	struct btrfs_ioctl_space_info *dest_orig;
285313f2696fSDaniel J Blueman 	struct btrfs_ioctl_space_info __user *user_dest;
28541406e432SJosef Bacik 	struct btrfs_space_info *info;
2855bf5fc093SJosef Bacik 	u64 types[] = {BTRFS_BLOCK_GROUP_DATA,
2856bf5fc093SJosef Bacik 		       BTRFS_BLOCK_GROUP_SYSTEM,
2857bf5fc093SJosef Bacik 		       BTRFS_BLOCK_GROUP_METADATA,
2858bf5fc093SJosef Bacik 		       BTRFS_BLOCK_GROUP_DATA | BTRFS_BLOCK_GROUP_METADATA};
2859bf5fc093SJosef Bacik 	int num_types = 4;
28607fde62bfSChris Mason 	int alloc_size;
28611406e432SJosef Bacik 	int ret = 0;
286251788b1bSDan Rosenberg 	u64 slot_count = 0;
2863bf5fc093SJosef Bacik 	int i, c;
28641406e432SJosef Bacik 
28651406e432SJosef Bacik 	if (copy_from_user(&space_args,
28661406e432SJosef Bacik 			   (struct btrfs_ioctl_space_args __user *)arg,
28671406e432SJosef Bacik 			   sizeof(space_args)))
28681406e432SJosef Bacik 		return -EFAULT;
28691406e432SJosef Bacik 
2870bf5fc093SJosef Bacik 	for (i = 0; i < num_types; i++) {
2871bf5fc093SJosef Bacik 		struct btrfs_space_info *tmp;
2872bf5fc093SJosef Bacik 
2873bf5fc093SJosef Bacik 		info = NULL;
28747fde62bfSChris Mason 		rcu_read_lock();
2875bf5fc093SJosef Bacik 		list_for_each_entry_rcu(tmp, &root->fs_info->space_info,
2876bf5fc093SJosef Bacik 					list) {
2877bf5fc093SJosef Bacik 			if (tmp->flags == types[i]) {
2878bf5fc093SJosef Bacik 				info = tmp;
2879bf5fc093SJosef Bacik 				break;
2880bf5fc093SJosef Bacik 			}
2881bf5fc093SJosef Bacik 		}
28827fde62bfSChris Mason 		rcu_read_unlock();
28831406e432SJosef Bacik 
2884bf5fc093SJosef Bacik 		if (!info)
2885bf5fc093SJosef Bacik 			continue;
2886bf5fc093SJosef Bacik 
2887bf5fc093SJosef Bacik 		down_read(&info->groups_sem);
2888bf5fc093SJosef Bacik 		for (c = 0; c < BTRFS_NR_RAID_TYPES; c++) {
2889bf5fc093SJosef Bacik 			if (!list_empty(&info->block_groups[c]))
2890bf5fc093SJosef Bacik 				slot_count++;
2891bf5fc093SJosef Bacik 		}
2892bf5fc093SJosef Bacik 		up_read(&info->groups_sem);
2893bf5fc093SJosef Bacik 	}
2894bf5fc093SJosef Bacik 
28957fde62bfSChris Mason 	/* space_slots == 0 means they are asking for a count */
28967fde62bfSChris Mason 	if (space_args.space_slots == 0) {
28977fde62bfSChris Mason 		space_args.total_spaces = slot_count;
28987fde62bfSChris Mason 		goto out;
28997fde62bfSChris Mason 	}
2900bf5fc093SJosef Bacik 
290151788b1bSDan Rosenberg 	slot_count = min_t(u64, space_args.space_slots, slot_count);
2902bf5fc093SJosef Bacik 
29037fde62bfSChris Mason 	alloc_size = sizeof(*dest) * slot_count;
2904bf5fc093SJosef Bacik 
29057fde62bfSChris Mason 	/* we generally have at most 6 or so space infos, one for each raid
29067fde62bfSChris Mason 	 * level.  So, a whole page should be more than enough for everyone
29077fde62bfSChris Mason 	 */
29087fde62bfSChris Mason 	if (alloc_size > PAGE_CACHE_SIZE)
29097fde62bfSChris Mason 		return -ENOMEM;
29107fde62bfSChris Mason 
29117fde62bfSChris Mason 	space_args.total_spaces = 0;
29127fde62bfSChris Mason 	dest = kmalloc(alloc_size, GFP_NOFS);
29137fde62bfSChris Mason 	if (!dest)
29147fde62bfSChris Mason 		return -ENOMEM;
29157fde62bfSChris Mason 	dest_orig = dest;
29167fde62bfSChris Mason 
29177fde62bfSChris Mason 	/* now we have a buffer to copy into */
2918bf5fc093SJosef Bacik 	for (i = 0; i < num_types; i++) {
2919bf5fc093SJosef Bacik 		struct btrfs_space_info *tmp;
2920bf5fc093SJosef Bacik 
292151788b1bSDan Rosenberg 		if (!slot_count)
292251788b1bSDan Rosenberg 			break;
292351788b1bSDan Rosenberg 
2924bf5fc093SJosef Bacik 		info = NULL;
29251406e432SJosef Bacik 		rcu_read_lock();
2926bf5fc093SJosef Bacik 		list_for_each_entry_rcu(tmp, &root->fs_info->space_info,
2927bf5fc093SJosef Bacik 					list) {
2928bf5fc093SJosef Bacik 			if (tmp->flags == types[i]) {
2929bf5fc093SJosef Bacik 				info = tmp;
29307fde62bfSChris Mason 				break;
2931bf5fc093SJosef Bacik 			}
2932bf5fc093SJosef Bacik 		}
2933bf5fc093SJosef Bacik 		rcu_read_unlock();
29347fde62bfSChris Mason 
2935bf5fc093SJosef Bacik 		if (!info)
2936bf5fc093SJosef Bacik 			continue;
2937bf5fc093SJosef Bacik 		down_read(&info->groups_sem);
2938bf5fc093SJosef Bacik 		for (c = 0; c < BTRFS_NR_RAID_TYPES; c++) {
2939bf5fc093SJosef Bacik 			if (!list_empty(&info->block_groups[c])) {
2940bf5fc093SJosef Bacik 				get_block_group_info(&info->block_groups[c],
2941bf5fc093SJosef Bacik 						     &space);
29427fde62bfSChris Mason 				memcpy(dest, &space, sizeof(space));
29431406e432SJosef Bacik 				dest++;
29441406e432SJosef Bacik 				space_args.total_spaces++;
294551788b1bSDan Rosenberg 				slot_count--;
29461406e432SJosef Bacik 			}
294751788b1bSDan Rosenberg 			if (!slot_count)
294851788b1bSDan Rosenberg 				break;
2949bf5fc093SJosef Bacik 		}
2950bf5fc093SJosef Bacik 		up_read(&info->groups_sem);
2951bf5fc093SJosef Bacik 	}
29521406e432SJosef Bacik 
29532eec6c81SDaniel J Blueman 	user_dest = (struct btrfs_ioctl_space_info __user *)
29547fde62bfSChris Mason 		(arg + sizeof(struct btrfs_ioctl_space_args));
29557fde62bfSChris Mason 
29567fde62bfSChris Mason 	if (copy_to_user(user_dest, dest_orig, alloc_size))
29577fde62bfSChris Mason 		ret = -EFAULT;
29587fde62bfSChris Mason 
29597fde62bfSChris Mason 	kfree(dest_orig);
29607fde62bfSChris Mason out:
29617fde62bfSChris Mason 	if (ret == 0 && copy_to_user(arg, &space_args, sizeof(space_args)))
29621406e432SJosef Bacik 		ret = -EFAULT;
29631406e432SJosef Bacik 
29641406e432SJosef Bacik 	return ret;
29651406e432SJosef Bacik }
29661406e432SJosef Bacik 
2967f46b5a66SChristoph Hellwig /*
2968f46b5a66SChristoph Hellwig  * there are many ways the trans_start and trans_end ioctls can lead
2969f46b5a66SChristoph Hellwig  * to deadlocks.  They should only be used by applications that
2970f46b5a66SChristoph Hellwig  * basically own the machine, and have a very in depth understanding
2971f46b5a66SChristoph Hellwig  * of all the possible deadlocks and enospc problems.
2972f46b5a66SChristoph Hellwig  */
2973f46b5a66SChristoph Hellwig long btrfs_ioctl_trans_end(struct file *file)
2974f46b5a66SChristoph Hellwig {
2975f46b5a66SChristoph Hellwig 	struct inode *inode = fdentry(file)->d_inode;
2976f46b5a66SChristoph Hellwig 	struct btrfs_root *root = BTRFS_I(inode)->root;
2977f46b5a66SChristoph Hellwig 	struct btrfs_trans_handle *trans;
2978f46b5a66SChristoph Hellwig 
2979f46b5a66SChristoph Hellwig 	trans = file->private_data;
29801ab86aedSSage Weil 	if (!trans)
29811ab86aedSSage Weil 		return -EINVAL;
2982b214107eSChristoph Hellwig 	file->private_data = NULL;
29839ca9ee09SSage Weil 
29841ab86aedSSage Weil 	btrfs_end_transaction(trans, root);
29851ab86aedSSage Weil 
2986a4abeea4SJosef Bacik 	atomic_dec(&root->fs_info->open_ioctl_trans);
29879ca9ee09SSage Weil 
29882a79f17eSAl Viro 	mnt_drop_write_file(file);
29891ab86aedSSage Weil 	return 0;
2990f46b5a66SChristoph Hellwig }
2991f46b5a66SChristoph Hellwig 
299246204592SSage Weil static noinline long btrfs_ioctl_start_sync(struct file *file, void __user *argp)
299346204592SSage Weil {
299446204592SSage Weil 	struct btrfs_root *root = BTRFS_I(file->f_dentry->d_inode)->root;
299546204592SSage Weil 	struct btrfs_trans_handle *trans;
299646204592SSage Weil 	u64 transid;
2997db5b493aSTsutomu Itoh 	int ret;
299846204592SSage Weil 
299946204592SSage Weil 	trans = btrfs_start_transaction(root, 0);
300098d5dc13STsutomu Itoh 	if (IS_ERR(trans))
300198d5dc13STsutomu Itoh 		return PTR_ERR(trans);
300246204592SSage Weil 	transid = trans->transid;
3003db5b493aSTsutomu Itoh 	ret = btrfs_commit_transaction_async(trans, root, 0);
30048b2b2d3cSTsutomu Itoh 	if (ret) {
30058b2b2d3cSTsutomu Itoh 		btrfs_end_transaction(trans, root);
3006db5b493aSTsutomu Itoh 		return ret;
30078b2b2d3cSTsutomu Itoh 	}
300846204592SSage Weil 
300946204592SSage Weil 	if (argp)
301046204592SSage Weil 		if (copy_to_user(argp, &transid, sizeof(transid)))
301146204592SSage Weil 			return -EFAULT;
301246204592SSage Weil 	return 0;
301346204592SSage Weil }
301446204592SSage Weil 
301546204592SSage Weil static noinline long btrfs_ioctl_wait_sync(struct file *file, void __user *argp)
301646204592SSage Weil {
301746204592SSage Weil 	struct btrfs_root *root = BTRFS_I(file->f_dentry->d_inode)->root;
301846204592SSage Weil 	u64 transid;
301946204592SSage Weil 
302046204592SSage Weil 	if (argp) {
302146204592SSage Weil 		if (copy_from_user(&transid, argp, sizeof(transid)))
302246204592SSage Weil 			return -EFAULT;
302346204592SSage Weil 	} else {
302446204592SSage Weil 		transid = 0;  /* current trans */
302546204592SSage Weil 	}
302646204592SSage Weil 	return btrfs_wait_for_commit(root, transid);
302746204592SSage Weil }
302846204592SSage Weil 
3029475f6387SJan Schmidt static long btrfs_ioctl_scrub(struct btrfs_root *root, void __user *arg)
3030475f6387SJan Schmidt {
3031475f6387SJan Schmidt 	int ret;
3032475f6387SJan Schmidt 	struct btrfs_ioctl_scrub_args *sa;
3033475f6387SJan Schmidt 
3034475f6387SJan Schmidt 	if (!capable(CAP_SYS_ADMIN))
3035475f6387SJan Schmidt 		return -EPERM;
3036475f6387SJan Schmidt 
3037475f6387SJan Schmidt 	sa = memdup_user(arg, sizeof(*sa));
3038475f6387SJan Schmidt 	if (IS_ERR(sa))
3039475f6387SJan Schmidt 		return PTR_ERR(sa);
3040475f6387SJan Schmidt 
3041475f6387SJan Schmidt 	ret = btrfs_scrub_dev(root, sa->devid, sa->start, sa->end,
30428628764eSArne Jansen 			      &sa->progress, sa->flags & BTRFS_SCRUB_READONLY);
3043475f6387SJan Schmidt 
3044475f6387SJan Schmidt 	if (copy_to_user(arg, sa, sizeof(*sa)))
3045475f6387SJan Schmidt 		ret = -EFAULT;
3046475f6387SJan Schmidt 
3047475f6387SJan Schmidt 	kfree(sa);
3048475f6387SJan Schmidt 	return ret;
3049475f6387SJan Schmidt }
3050475f6387SJan Schmidt 
3051475f6387SJan Schmidt static long btrfs_ioctl_scrub_cancel(struct btrfs_root *root, void __user *arg)
3052475f6387SJan Schmidt {
3053475f6387SJan Schmidt 	if (!capable(CAP_SYS_ADMIN))
3054475f6387SJan Schmidt 		return -EPERM;
3055475f6387SJan Schmidt 
3056475f6387SJan Schmidt 	return btrfs_scrub_cancel(root);
3057475f6387SJan Schmidt }
3058475f6387SJan Schmidt 
3059475f6387SJan Schmidt static long btrfs_ioctl_scrub_progress(struct btrfs_root *root,
3060475f6387SJan Schmidt 				       void __user *arg)
3061475f6387SJan Schmidt {
3062475f6387SJan Schmidt 	struct btrfs_ioctl_scrub_args *sa;
3063475f6387SJan Schmidt 	int ret;
3064475f6387SJan Schmidt 
3065475f6387SJan Schmidt 	if (!capable(CAP_SYS_ADMIN))
3066475f6387SJan Schmidt 		return -EPERM;
3067475f6387SJan Schmidt 
3068475f6387SJan Schmidt 	sa = memdup_user(arg, sizeof(*sa));
3069475f6387SJan Schmidt 	if (IS_ERR(sa))
3070475f6387SJan Schmidt 		return PTR_ERR(sa);
3071475f6387SJan Schmidt 
3072475f6387SJan Schmidt 	ret = btrfs_scrub_progress(root, sa->devid, &sa->progress);
3073475f6387SJan Schmidt 
3074475f6387SJan Schmidt 	if (copy_to_user(arg, sa, sizeof(*sa)))
3075475f6387SJan Schmidt 		ret = -EFAULT;
3076475f6387SJan Schmidt 
3077475f6387SJan Schmidt 	kfree(sa);
3078475f6387SJan Schmidt 	return ret;
3079475f6387SJan Schmidt }
3080475f6387SJan Schmidt 
3081c11d2c23SStefan Behrens static long btrfs_ioctl_get_dev_stats(struct btrfs_root *root,
3082c11d2c23SStefan Behrens 				      void __user *arg, int reset_after_read)
3083c11d2c23SStefan Behrens {
3084c11d2c23SStefan Behrens 	struct btrfs_ioctl_get_dev_stats *sa;
3085c11d2c23SStefan Behrens 	int ret;
3086c11d2c23SStefan Behrens 
3087c11d2c23SStefan Behrens 	if (reset_after_read && !capable(CAP_SYS_ADMIN))
3088c11d2c23SStefan Behrens 		return -EPERM;
3089c11d2c23SStefan Behrens 
3090c11d2c23SStefan Behrens 	sa = memdup_user(arg, sizeof(*sa));
3091c11d2c23SStefan Behrens 	if (IS_ERR(sa))
3092c11d2c23SStefan Behrens 		return PTR_ERR(sa);
3093c11d2c23SStefan Behrens 
3094c11d2c23SStefan Behrens 	ret = btrfs_get_dev_stats(root, sa, reset_after_read);
3095c11d2c23SStefan Behrens 
3096c11d2c23SStefan Behrens 	if (copy_to_user(arg, sa, sizeof(*sa)))
3097c11d2c23SStefan Behrens 		ret = -EFAULT;
3098c11d2c23SStefan Behrens 
3099c11d2c23SStefan Behrens 	kfree(sa);
3100c11d2c23SStefan Behrens 	return ret;
3101c11d2c23SStefan Behrens }
3102c11d2c23SStefan Behrens 
3103d7728c96SJan Schmidt static long btrfs_ioctl_ino_to_path(struct btrfs_root *root, void __user *arg)
3104d7728c96SJan Schmidt {
3105d7728c96SJan Schmidt 	int ret = 0;
3106d7728c96SJan Schmidt 	int i;
3107740c3d22SChris Mason 	u64 rel_ptr;
3108d7728c96SJan Schmidt 	int size;
3109806468f8SChris Mason 	struct btrfs_ioctl_ino_path_args *ipa = NULL;
3110d7728c96SJan Schmidt 	struct inode_fs_paths *ipath = NULL;
3111d7728c96SJan Schmidt 	struct btrfs_path *path;
3112d7728c96SJan Schmidt 
3113d7728c96SJan Schmidt 	if (!capable(CAP_SYS_ADMIN))
3114d7728c96SJan Schmidt 		return -EPERM;
3115d7728c96SJan Schmidt 
3116d7728c96SJan Schmidt 	path = btrfs_alloc_path();
3117d7728c96SJan Schmidt 	if (!path) {
3118d7728c96SJan Schmidt 		ret = -ENOMEM;
3119d7728c96SJan Schmidt 		goto out;
3120d7728c96SJan Schmidt 	}
3121d7728c96SJan Schmidt 
3122d7728c96SJan Schmidt 	ipa = memdup_user(arg, sizeof(*ipa));
3123d7728c96SJan Schmidt 	if (IS_ERR(ipa)) {
3124d7728c96SJan Schmidt 		ret = PTR_ERR(ipa);
3125d7728c96SJan Schmidt 		ipa = NULL;
3126d7728c96SJan Schmidt 		goto out;
3127d7728c96SJan Schmidt 	}
3128d7728c96SJan Schmidt 
3129d7728c96SJan Schmidt 	size = min_t(u32, ipa->size, 4096);
3130d7728c96SJan Schmidt 	ipath = init_ipath(size, root, path);
3131d7728c96SJan Schmidt 	if (IS_ERR(ipath)) {
3132d7728c96SJan Schmidt 		ret = PTR_ERR(ipath);
3133d7728c96SJan Schmidt 		ipath = NULL;
3134d7728c96SJan Schmidt 		goto out;
3135d7728c96SJan Schmidt 	}
3136d7728c96SJan Schmidt 
3137d7728c96SJan Schmidt 	ret = paths_from_inode(ipa->inum, ipath);
3138d7728c96SJan Schmidt 	if (ret < 0)
3139d7728c96SJan Schmidt 		goto out;
3140d7728c96SJan Schmidt 
3141d7728c96SJan Schmidt 	for (i = 0; i < ipath->fspath->elem_cnt; ++i) {
3142745c4d8eSJeff Mahoney 		rel_ptr = ipath->fspath->val[i] -
3143745c4d8eSJeff Mahoney 			  (u64)(unsigned long)ipath->fspath->val;
3144740c3d22SChris Mason 		ipath->fspath->val[i] = rel_ptr;
3145d7728c96SJan Schmidt 	}
3146d7728c96SJan Schmidt 
3147745c4d8eSJeff Mahoney 	ret = copy_to_user((void *)(unsigned long)ipa->fspath,
3148745c4d8eSJeff Mahoney 			   (void *)(unsigned long)ipath->fspath, size);
3149d7728c96SJan Schmidt 	if (ret) {
3150d7728c96SJan Schmidt 		ret = -EFAULT;
3151d7728c96SJan Schmidt 		goto out;
3152d7728c96SJan Schmidt 	}
3153d7728c96SJan Schmidt 
3154d7728c96SJan Schmidt out:
3155d7728c96SJan Schmidt 	btrfs_free_path(path);
3156d7728c96SJan Schmidt 	free_ipath(ipath);
3157d7728c96SJan Schmidt 	kfree(ipa);
3158d7728c96SJan Schmidt 
3159d7728c96SJan Schmidt 	return ret;
3160d7728c96SJan Schmidt }
3161d7728c96SJan Schmidt 
3162d7728c96SJan Schmidt static int build_ino_list(u64 inum, u64 offset, u64 root, void *ctx)
3163d7728c96SJan Schmidt {
3164d7728c96SJan Schmidt 	struct btrfs_data_container *inodes = ctx;
3165d7728c96SJan Schmidt 	const size_t c = 3 * sizeof(u64);
3166d7728c96SJan Schmidt 
3167d7728c96SJan Schmidt 	if (inodes->bytes_left >= c) {
3168d7728c96SJan Schmidt 		inodes->bytes_left -= c;
3169d7728c96SJan Schmidt 		inodes->val[inodes->elem_cnt] = inum;
3170d7728c96SJan Schmidt 		inodes->val[inodes->elem_cnt + 1] = offset;
3171d7728c96SJan Schmidt 		inodes->val[inodes->elem_cnt + 2] = root;
3172d7728c96SJan Schmidt 		inodes->elem_cnt += 3;
3173d7728c96SJan Schmidt 	} else {
3174d7728c96SJan Schmidt 		inodes->bytes_missing += c - inodes->bytes_left;
3175d7728c96SJan Schmidt 		inodes->bytes_left = 0;
3176d7728c96SJan Schmidt 		inodes->elem_missed += 3;
3177d7728c96SJan Schmidt 	}
3178d7728c96SJan Schmidt 
3179d7728c96SJan Schmidt 	return 0;
3180d7728c96SJan Schmidt }
3181d7728c96SJan Schmidt 
3182d7728c96SJan Schmidt static long btrfs_ioctl_logical_to_ino(struct btrfs_root *root,
3183d7728c96SJan Schmidt 					void __user *arg)
3184d7728c96SJan Schmidt {
3185d7728c96SJan Schmidt 	int ret = 0;
3186d7728c96SJan Schmidt 	int size;
31874692cf58SJan Schmidt 	u64 extent_item_pos;
3188d7728c96SJan Schmidt 	struct btrfs_ioctl_logical_ino_args *loi;
3189d7728c96SJan Schmidt 	struct btrfs_data_container *inodes = NULL;
3190d7728c96SJan Schmidt 	struct btrfs_path *path = NULL;
3191d7728c96SJan Schmidt 	struct btrfs_key key;
3192d7728c96SJan Schmidt 
3193d7728c96SJan Schmidt 	if (!capable(CAP_SYS_ADMIN))
3194d7728c96SJan Schmidt 		return -EPERM;
3195d7728c96SJan Schmidt 
3196d7728c96SJan Schmidt 	loi = memdup_user(arg, sizeof(*loi));
3197d7728c96SJan Schmidt 	if (IS_ERR(loi)) {
3198d7728c96SJan Schmidt 		ret = PTR_ERR(loi);
3199d7728c96SJan Schmidt 		loi = NULL;
3200d7728c96SJan Schmidt 		goto out;
3201d7728c96SJan Schmidt 	}
3202d7728c96SJan Schmidt 
3203d7728c96SJan Schmidt 	path = btrfs_alloc_path();
3204d7728c96SJan Schmidt 	if (!path) {
3205d7728c96SJan Schmidt 		ret = -ENOMEM;
3206d7728c96SJan Schmidt 		goto out;
3207d7728c96SJan Schmidt 	}
3208d7728c96SJan Schmidt 
3209d7728c96SJan Schmidt 	size = min_t(u32, loi->size, 4096);
3210d7728c96SJan Schmidt 	inodes = init_data_container(size);
3211d7728c96SJan Schmidt 	if (IS_ERR(inodes)) {
3212d7728c96SJan Schmidt 		ret = PTR_ERR(inodes);
3213d7728c96SJan Schmidt 		inodes = NULL;
3214d7728c96SJan Schmidt 		goto out;
3215d7728c96SJan Schmidt 	}
3216d7728c96SJan Schmidt 
3217d7728c96SJan Schmidt 	ret = extent_from_logical(root->fs_info, loi->logical, path, &key);
32184692cf58SJan Schmidt 	btrfs_release_path(path);
3219d7728c96SJan Schmidt 
3220d7728c96SJan Schmidt 	if (ret & BTRFS_EXTENT_FLAG_TREE_BLOCK)
3221d7728c96SJan Schmidt 		ret = -ENOENT;
3222d7728c96SJan Schmidt 	if (ret < 0)
3223d7728c96SJan Schmidt 		goto out;
3224d7728c96SJan Schmidt 
32254692cf58SJan Schmidt 	extent_item_pos = loi->logical - key.objectid;
32267a3ae2f8SJan Schmidt 	ret = iterate_extent_inodes(root->fs_info, key.objectid,
32277a3ae2f8SJan Schmidt 					extent_item_pos, 0, build_ino_list,
32284692cf58SJan Schmidt 					inodes);
3229d7728c96SJan Schmidt 
3230d7728c96SJan Schmidt 	if (ret < 0)
3231d7728c96SJan Schmidt 		goto out;
3232d7728c96SJan Schmidt 
3233745c4d8eSJeff Mahoney 	ret = copy_to_user((void *)(unsigned long)loi->inodes,
3234745c4d8eSJeff Mahoney 			   (void *)(unsigned long)inodes, size);
3235d7728c96SJan Schmidt 	if (ret)
3236d7728c96SJan Schmidt 		ret = -EFAULT;
3237d7728c96SJan Schmidt 
3238d7728c96SJan Schmidt out:
3239d7728c96SJan Schmidt 	btrfs_free_path(path);
3240d7728c96SJan Schmidt 	kfree(inodes);
3241d7728c96SJan Schmidt 	kfree(loi);
3242d7728c96SJan Schmidt 
3243d7728c96SJan Schmidt 	return ret;
3244d7728c96SJan Schmidt }
3245d7728c96SJan Schmidt 
324619a39dceSIlya Dryomov void update_ioctl_balance_args(struct btrfs_fs_info *fs_info, int lock,
3247c9e9f97bSIlya Dryomov 			       struct btrfs_ioctl_balance_args *bargs)
3248c9e9f97bSIlya Dryomov {
3249c9e9f97bSIlya Dryomov 	struct btrfs_balance_control *bctl = fs_info->balance_ctl;
3250c9e9f97bSIlya Dryomov 
3251c9e9f97bSIlya Dryomov 	bargs->flags = bctl->flags;
3252c9e9f97bSIlya Dryomov 
3253837d5b6eSIlya Dryomov 	if (atomic_read(&fs_info->balance_running))
3254837d5b6eSIlya Dryomov 		bargs->state |= BTRFS_BALANCE_STATE_RUNNING;
3255837d5b6eSIlya Dryomov 	if (atomic_read(&fs_info->balance_pause_req))
3256837d5b6eSIlya Dryomov 		bargs->state |= BTRFS_BALANCE_STATE_PAUSE_REQ;
3257a7e99c69SIlya Dryomov 	if (atomic_read(&fs_info->balance_cancel_req))
3258a7e99c69SIlya Dryomov 		bargs->state |= BTRFS_BALANCE_STATE_CANCEL_REQ;
3259837d5b6eSIlya Dryomov 
3260c9e9f97bSIlya Dryomov 	memcpy(&bargs->data, &bctl->data, sizeof(bargs->data));
3261c9e9f97bSIlya Dryomov 	memcpy(&bargs->meta, &bctl->meta, sizeof(bargs->meta));
3262c9e9f97bSIlya Dryomov 	memcpy(&bargs->sys, &bctl->sys, sizeof(bargs->sys));
326319a39dceSIlya Dryomov 
326419a39dceSIlya Dryomov 	if (lock) {
326519a39dceSIlya Dryomov 		spin_lock(&fs_info->balance_lock);
326619a39dceSIlya Dryomov 		memcpy(&bargs->stat, &bctl->stat, sizeof(bargs->stat));
326719a39dceSIlya Dryomov 		spin_unlock(&fs_info->balance_lock);
326819a39dceSIlya Dryomov 	} else {
326919a39dceSIlya Dryomov 		memcpy(&bargs->stat, &bctl->stat, sizeof(bargs->stat));
327019a39dceSIlya Dryomov 	}
3271c9e9f97bSIlya Dryomov }
3272c9e9f97bSIlya Dryomov 
32739ba1f6e4SLiu Bo static long btrfs_ioctl_balance(struct file *file, void __user *arg)
3274c9e9f97bSIlya Dryomov {
32759ba1f6e4SLiu Bo 	struct btrfs_root *root = BTRFS_I(fdentry(file)->d_inode)->root;
3276c9e9f97bSIlya Dryomov 	struct btrfs_fs_info *fs_info = root->fs_info;
3277c9e9f97bSIlya Dryomov 	struct btrfs_ioctl_balance_args *bargs;
3278c9e9f97bSIlya Dryomov 	struct btrfs_balance_control *bctl;
3279c9e9f97bSIlya Dryomov 	int ret;
3280c9e9f97bSIlya Dryomov 
3281c9e9f97bSIlya Dryomov 	if (!capable(CAP_SYS_ADMIN))
3282c9e9f97bSIlya Dryomov 		return -EPERM;
3283c9e9f97bSIlya Dryomov 
3284c9e9f97bSIlya Dryomov 	if (fs_info->sb->s_flags & MS_RDONLY)
3285c9e9f97bSIlya Dryomov 		return -EROFS;
3286c9e9f97bSIlya Dryomov 
32879ba1f6e4SLiu Bo 	ret = mnt_want_write(file->f_path.mnt);
32889ba1f6e4SLiu Bo 	if (ret)
32899ba1f6e4SLiu Bo 		return ret;
32909ba1f6e4SLiu Bo 
3291c9e9f97bSIlya Dryomov 	mutex_lock(&fs_info->volume_mutex);
3292c9e9f97bSIlya Dryomov 	mutex_lock(&fs_info->balance_mutex);
3293c9e9f97bSIlya Dryomov 
3294c9e9f97bSIlya Dryomov 	if (arg) {
3295c9e9f97bSIlya Dryomov 		bargs = memdup_user(arg, sizeof(*bargs));
3296c9e9f97bSIlya Dryomov 		if (IS_ERR(bargs)) {
3297c9e9f97bSIlya Dryomov 			ret = PTR_ERR(bargs);
3298c9e9f97bSIlya Dryomov 			goto out;
3299c9e9f97bSIlya Dryomov 		}
3300de322263SIlya Dryomov 
3301de322263SIlya Dryomov 		if (bargs->flags & BTRFS_BALANCE_RESUME) {
3302de322263SIlya Dryomov 			if (!fs_info->balance_ctl) {
3303de322263SIlya Dryomov 				ret = -ENOTCONN;
3304de322263SIlya Dryomov 				goto out_bargs;
3305de322263SIlya Dryomov 			}
3306de322263SIlya Dryomov 
3307de322263SIlya Dryomov 			bctl = fs_info->balance_ctl;
3308de322263SIlya Dryomov 			spin_lock(&fs_info->balance_lock);
3309de322263SIlya Dryomov 			bctl->flags |= BTRFS_BALANCE_RESUME;
3310de322263SIlya Dryomov 			spin_unlock(&fs_info->balance_lock);
3311de322263SIlya Dryomov 
3312de322263SIlya Dryomov 			goto do_balance;
3313de322263SIlya Dryomov 		}
3314c9e9f97bSIlya Dryomov 	} else {
3315c9e9f97bSIlya Dryomov 		bargs = NULL;
3316c9e9f97bSIlya Dryomov 	}
3317c9e9f97bSIlya Dryomov 
3318837d5b6eSIlya Dryomov 	if (fs_info->balance_ctl) {
3319837d5b6eSIlya Dryomov 		ret = -EINPROGRESS;
3320837d5b6eSIlya Dryomov 		goto out_bargs;
3321837d5b6eSIlya Dryomov 	}
3322837d5b6eSIlya Dryomov 
3323c9e9f97bSIlya Dryomov 	bctl = kzalloc(sizeof(*bctl), GFP_NOFS);
3324c9e9f97bSIlya Dryomov 	if (!bctl) {
3325c9e9f97bSIlya Dryomov 		ret = -ENOMEM;
3326c9e9f97bSIlya Dryomov 		goto out_bargs;
3327c9e9f97bSIlya Dryomov 	}
3328c9e9f97bSIlya Dryomov 
3329c9e9f97bSIlya Dryomov 	bctl->fs_info = fs_info;
3330c9e9f97bSIlya Dryomov 	if (arg) {
3331c9e9f97bSIlya Dryomov 		memcpy(&bctl->data, &bargs->data, sizeof(bctl->data));
3332c9e9f97bSIlya Dryomov 		memcpy(&bctl->meta, &bargs->meta, sizeof(bctl->meta));
3333c9e9f97bSIlya Dryomov 		memcpy(&bctl->sys, &bargs->sys, sizeof(bctl->sys));
3334c9e9f97bSIlya Dryomov 
3335c9e9f97bSIlya Dryomov 		bctl->flags = bargs->flags;
3336f43ffb60SIlya Dryomov 	} else {
3337f43ffb60SIlya Dryomov 		/* balance everything - no filters */
3338f43ffb60SIlya Dryomov 		bctl->flags |= BTRFS_BALANCE_TYPE_MASK;
3339c9e9f97bSIlya Dryomov 	}
3340c9e9f97bSIlya Dryomov 
3341de322263SIlya Dryomov do_balance:
3342c9e9f97bSIlya Dryomov 	ret = btrfs_balance(bctl, bargs);
3343c9e9f97bSIlya Dryomov 	/*
3344837d5b6eSIlya Dryomov 	 * bctl is freed in __cancel_balance or in free_fs_info if
3345837d5b6eSIlya Dryomov 	 * restriper was paused all the way until unmount
3346c9e9f97bSIlya Dryomov 	 */
3347c9e9f97bSIlya Dryomov 	if (arg) {
3348c9e9f97bSIlya Dryomov 		if (copy_to_user(arg, bargs, sizeof(*bargs)))
3349c9e9f97bSIlya Dryomov 			ret = -EFAULT;
3350c9e9f97bSIlya Dryomov 	}
3351c9e9f97bSIlya Dryomov 
3352c9e9f97bSIlya Dryomov out_bargs:
3353c9e9f97bSIlya Dryomov 	kfree(bargs);
3354c9e9f97bSIlya Dryomov out:
3355c9e9f97bSIlya Dryomov 	mutex_unlock(&fs_info->balance_mutex);
3356c9e9f97bSIlya Dryomov 	mutex_unlock(&fs_info->volume_mutex);
33579ba1f6e4SLiu Bo 	mnt_drop_write(file->f_path.mnt);
3358c9e9f97bSIlya Dryomov 	return ret;
3359c9e9f97bSIlya Dryomov }
3360c9e9f97bSIlya Dryomov 
3361837d5b6eSIlya Dryomov static long btrfs_ioctl_balance_ctl(struct btrfs_root *root, int cmd)
3362837d5b6eSIlya Dryomov {
3363837d5b6eSIlya Dryomov 	if (!capable(CAP_SYS_ADMIN))
3364837d5b6eSIlya Dryomov 		return -EPERM;
3365837d5b6eSIlya Dryomov 
3366837d5b6eSIlya Dryomov 	switch (cmd) {
3367837d5b6eSIlya Dryomov 	case BTRFS_BALANCE_CTL_PAUSE:
3368837d5b6eSIlya Dryomov 		return btrfs_pause_balance(root->fs_info);
3369a7e99c69SIlya Dryomov 	case BTRFS_BALANCE_CTL_CANCEL:
3370a7e99c69SIlya Dryomov 		return btrfs_cancel_balance(root->fs_info);
3371837d5b6eSIlya Dryomov 	}
3372837d5b6eSIlya Dryomov 
3373837d5b6eSIlya Dryomov 	return -EINVAL;
3374837d5b6eSIlya Dryomov }
3375837d5b6eSIlya Dryomov 
337619a39dceSIlya Dryomov static long btrfs_ioctl_balance_progress(struct btrfs_root *root,
337719a39dceSIlya Dryomov 					 void __user *arg)
337819a39dceSIlya Dryomov {
337919a39dceSIlya Dryomov 	struct btrfs_fs_info *fs_info = root->fs_info;
338019a39dceSIlya Dryomov 	struct btrfs_ioctl_balance_args *bargs;
338119a39dceSIlya Dryomov 	int ret = 0;
338219a39dceSIlya Dryomov 
338319a39dceSIlya Dryomov 	if (!capable(CAP_SYS_ADMIN))
338419a39dceSIlya Dryomov 		return -EPERM;
338519a39dceSIlya Dryomov 
338619a39dceSIlya Dryomov 	mutex_lock(&fs_info->balance_mutex);
338719a39dceSIlya Dryomov 	if (!fs_info->balance_ctl) {
338819a39dceSIlya Dryomov 		ret = -ENOTCONN;
338919a39dceSIlya Dryomov 		goto out;
339019a39dceSIlya Dryomov 	}
339119a39dceSIlya Dryomov 
339219a39dceSIlya Dryomov 	bargs = kzalloc(sizeof(*bargs), GFP_NOFS);
339319a39dceSIlya Dryomov 	if (!bargs) {
339419a39dceSIlya Dryomov 		ret = -ENOMEM;
339519a39dceSIlya Dryomov 		goto out;
339619a39dceSIlya Dryomov 	}
339719a39dceSIlya Dryomov 
339819a39dceSIlya Dryomov 	update_ioctl_balance_args(fs_info, 1, bargs);
339919a39dceSIlya Dryomov 
340019a39dceSIlya Dryomov 	if (copy_to_user(arg, bargs, sizeof(*bargs)))
340119a39dceSIlya Dryomov 		ret = -EFAULT;
340219a39dceSIlya Dryomov 
340319a39dceSIlya Dryomov 	kfree(bargs);
340419a39dceSIlya Dryomov out:
340519a39dceSIlya Dryomov 	mutex_unlock(&fs_info->balance_mutex);
340619a39dceSIlya Dryomov 	return ret;
340719a39dceSIlya Dryomov }
340819a39dceSIlya Dryomov 
34098ea05e3aSAlexander Block static long btrfs_ioctl_set_received_subvol(struct file *file,
34108ea05e3aSAlexander Block 					    void __user *arg)
34118ea05e3aSAlexander Block {
34128ea05e3aSAlexander Block 	struct btrfs_ioctl_received_subvol_args *sa = NULL;
34138ea05e3aSAlexander Block 	struct inode *inode = fdentry(file)->d_inode;
34148ea05e3aSAlexander Block 	struct btrfs_root *root = BTRFS_I(inode)->root;
34158ea05e3aSAlexander Block 	struct btrfs_root_item *root_item = &root->root_item;
34168ea05e3aSAlexander Block 	struct btrfs_trans_handle *trans;
34178ea05e3aSAlexander Block 	struct timespec ct = CURRENT_TIME;
34188ea05e3aSAlexander Block 	int ret = 0;
34198ea05e3aSAlexander Block 
34208ea05e3aSAlexander Block 	ret = mnt_want_write_file(file);
34218ea05e3aSAlexander Block 	if (ret < 0)
34228ea05e3aSAlexander Block 		return ret;
34238ea05e3aSAlexander Block 
34248ea05e3aSAlexander Block 	down_write(&root->fs_info->subvol_sem);
34258ea05e3aSAlexander Block 
34268ea05e3aSAlexander Block 	if (btrfs_ino(inode) != BTRFS_FIRST_FREE_OBJECTID) {
34278ea05e3aSAlexander Block 		ret = -EINVAL;
34288ea05e3aSAlexander Block 		goto out;
34298ea05e3aSAlexander Block 	}
34308ea05e3aSAlexander Block 
34318ea05e3aSAlexander Block 	if (btrfs_root_readonly(root)) {
34328ea05e3aSAlexander Block 		ret = -EROFS;
34338ea05e3aSAlexander Block 		goto out;
34348ea05e3aSAlexander Block 	}
34358ea05e3aSAlexander Block 
34368ea05e3aSAlexander Block 	if (!inode_owner_or_capable(inode)) {
34378ea05e3aSAlexander Block 		ret = -EACCES;
34388ea05e3aSAlexander Block 		goto out;
34398ea05e3aSAlexander Block 	}
34408ea05e3aSAlexander Block 
34418ea05e3aSAlexander Block 	sa = memdup_user(arg, sizeof(*sa));
34428ea05e3aSAlexander Block 	if (IS_ERR(sa)) {
34438ea05e3aSAlexander Block 		ret = PTR_ERR(sa);
34448ea05e3aSAlexander Block 		sa = NULL;
34458ea05e3aSAlexander Block 		goto out;
34468ea05e3aSAlexander Block 	}
34478ea05e3aSAlexander Block 
34488ea05e3aSAlexander Block 	trans = btrfs_start_transaction(root, 1);
34498ea05e3aSAlexander Block 	if (IS_ERR(trans)) {
34508ea05e3aSAlexander Block 		ret = PTR_ERR(trans);
34518ea05e3aSAlexander Block 		trans = NULL;
34528ea05e3aSAlexander Block 		goto out;
34538ea05e3aSAlexander Block 	}
34548ea05e3aSAlexander Block 
34558ea05e3aSAlexander Block 	sa->rtransid = trans->transid;
34568ea05e3aSAlexander Block 	sa->rtime.sec = ct.tv_sec;
34578ea05e3aSAlexander Block 	sa->rtime.nsec = ct.tv_nsec;
34588ea05e3aSAlexander Block 
34598ea05e3aSAlexander Block 	memcpy(root_item->received_uuid, sa->uuid, BTRFS_UUID_SIZE);
34608ea05e3aSAlexander Block 	btrfs_set_root_stransid(root_item, sa->stransid);
34618ea05e3aSAlexander Block 	btrfs_set_root_rtransid(root_item, sa->rtransid);
34628ea05e3aSAlexander Block 	root_item->stime.sec = cpu_to_le64(sa->stime.sec);
34638ea05e3aSAlexander Block 	root_item->stime.nsec = cpu_to_le32(sa->stime.nsec);
34648ea05e3aSAlexander Block 	root_item->rtime.sec = cpu_to_le64(sa->rtime.sec);
34658ea05e3aSAlexander Block 	root_item->rtime.nsec = cpu_to_le32(sa->rtime.nsec);
34668ea05e3aSAlexander Block 
34678ea05e3aSAlexander Block 	ret = btrfs_update_root(trans, root->fs_info->tree_root,
34688ea05e3aSAlexander Block 				&root->root_key, &root->root_item);
34698ea05e3aSAlexander Block 	if (ret < 0) {
34708ea05e3aSAlexander Block 		btrfs_end_transaction(trans, root);
34718ea05e3aSAlexander Block 		trans = NULL;
34728ea05e3aSAlexander Block 		goto out;
34738ea05e3aSAlexander Block 	} else {
34748ea05e3aSAlexander Block 		ret = btrfs_commit_transaction(trans, root);
34758ea05e3aSAlexander Block 		if (ret < 0)
34768ea05e3aSAlexander Block 			goto out;
34778ea05e3aSAlexander Block 	}
34788ea05e3aSAlexander Block 
34798ea05e3aSAlexander Block 	ret = copy_to_user(arg, sa, sizeof(*sa));
34808ea05e3aSAlexander Block 	if (ret)
34818ea05e3aSAlexander Block 		ret = -EFAULT;
34828ea05e3aSAlexander Block 
34838ea05e3aSAlexander Block out:
34848ea05e3aSAlexander Block 	kfree(sa);
34858ea05e3aSAlexander Block 	up_write(&root->fs_info->subvol_sem);
34868ea05e3aSAlexander Block 	mnt_drop_write_file(file);
34878ea05e3aSAlexander Block 	return ret;
34888ea05e3aSAlexander Block }
34898ea05e3aSAlexander Block 
3490f46b5a66SChristoph Hellwig long btrfs_ioctl(struct file *file, unsigned int
3491f46b5a66SChristoph Hellwig 		cmd, unsigned long arg)
3492f46b5a66SChristoph Hellwig {
3493f46b5a66SChristoph Hellwig 	struct btrfs_root *root = BTRFS_I(fdentry(file)->d_inode)->root;
34944bcabaa3SChristoph Hellwig 	void __user *argp = (void __user *)arg;
3495f46b5a66SChristoph Hellwig 
3496f46b5a66SChristoph Hellwig 	switch (cmd) {
34976cbff00fSChristoph Hellwig 	case FS_IOC_GETFLAGS:
34986cbff00fSChristoph Hellwig 		return btrfs_ioctl_getflags(file, argp);
34996cbff00fSChristoph Hellwig 	case FS_IOC_SETFLAGS:
35006cbff00fSChristoph Hellwig 		return btrfs_ioctl_setflags(file, argp);
35016cbff00fSChristoph Hellwig 	case FS_IOC_GETVERSION:
35026cbff00fSChristoph Hellwig 		return btrfs_ioctl_getversion(file, argp);
3503f7039b1dSLi Dongyang 	case FITRIM:
3504f7039b1dSLi Dongyang 		return btrfs_ioctl_fitrim(file, argp);
3505f46b5a66SChristoph Hellwig 	case BTRFS_IOC_SNAP_CREATE:
3506fa0d2b9bSLi Zefan 		return btrfs_ioctl_snap_create(file, argp, 0);
3507fdfb1e4fSLi Zefan 	case BTRFS_IOC_SNAP_CREATE_V2:
3508fa0d2b9bSLi Zefan 		return btrfs_ioctl_snap_create_v2(file, argp, 0);
35093de4586cSChris Mason 	case BTRFS_IOC_SUBVOL_CREATE:
3510fa0d2b9bSLi Zefan 		return btrfs_ioctl_snap_create(file, argp, 1);
351176dda93cSYan, Zheng 	case BTRFS_IOC_SNAP_DESTROY:
351276dda93cSYan, Zheng 		return btrfs_ioctl_snap_destroy(file, argp);
35130caa102dSLi Zefan 	case BTRFS_IOC_SUBVOL_GETFLAGS:
35140caa102dSLi Zefan 		return btrfs_ioctl_subvol_getflags(file, argp);
35150caa102dSLi Zefan 	case BTRFS_IOC_SUBVOL_SETFLAGS:
35160caa102dSLi Zefan 		return btrfs_ioctl_subvol_setflags(file, argp);
35176ef5ed0dSJosef Bacik 	case BTRFS_IOC_DEFAULT_SUBVOL:
35186ef5ed0dSJosef Bacik 		return btrfs_ioctl_default_subvol(file, argp);
3519f46b5a66SChristoph Hellwig 	case BTRFS_IOC_DEFRAG:
35201e701a32SChris Mason 		return btrfs_ioctl_defrag(file, NULL);
35211e701a32SChris Mason 	case BTRFS_IOC_DEFRAG_RANGE:
35221e701a32SChris Mason 		return btrfs_ioctl_defrag(file, argp);
3523f46b5a66SChristoph Hellwig 	case BTRFS_IOC_RESIZE:
35244bcabaa3SChristoph Hellwig 		return btrfs_ioctl_resize(root, argp);
3525f46b5a66SChristoph Hellwig 	case BTRFS_IOC_ADD_DEV:
35264bcabaa3SChristoph Hellwig 		return btrfs_ioctl_add_dev(root, argp);
3527f46b5a66SChristoph Hellwig 	case BTRFS_IOC_RM_DEV:
35284bcabaa3SChristoph Hellwig 		return btrfs_ioctl_rm_dev(root, argp);
3529475f6387SJan Schmidt 	case BTRFS_IOC_FS_INFO:
3530475f6387SJan Schmidt 		return btrfs_ioctl_fs_info(root, argp);
3531475f6387SJan Schmidt 	case BTRFS_IOC_DEV_INFO:
3532475f6387SJan Schmidt 		return btrfs_ioctl_dev_info(root, argp);
3533f46b5a66SChristoph Hellwig 	case BTRFS_IOC_BALANCE:
35349ba1f6e4SLiu Bo 		return btrfs_ioctl_balance(file, NULL);
3535f46b5a66SChristoph Hellwig 	case BTRFS_IOC_CLONE:
3536c5c9cd4dSSage Weil 		return btrfs_ioctl_clone(file, arg, 0, 0, 0);
3537c5c9cd4dSSage Weil 	case BTRFS_IOC_CLONE_RANGE:
35387a865e8aSChristoph Hellwig 		return btrfs_ioctl_clone_range(file, argp);
3539f46b5a66SChristoph Hellwig 	case BTRFS_IOC_TRANS_START:
3540f46b5a66SChristoph Hellwig 		return btrfs_ioctl_trans_start(file);
3541f46b5a66SChristoph Hellwig 	case BTRFS_IOC_TRANS_END:
3542f46b5a66SChristoph Hellwig 		return btrfs_ioctl_trans_end(file);
3543ac8e9819SChris Mason 	case BTRFS_IOC_TREE_SEARCH:
3544ac8e9819SChris Mason 		return btrfs_ioctl_tree_search(file, argp);
3545ac8e9819SChris Mason 	case BTRFS_IOC_INO_LOOKUP:
3546ac8e9819SChris Mason 		return btrfs_ioctl_ino_lookup(file, argp);
3547d7728c96SJan Schmidt 	case BTRFS_IOC_INO_PATHS:
3548d7728c96SJan Schmidt 		return btrfs_ioctl_ino_to_path(root, argp);
3549d7728c96SJan Schmidt 	case BTRFS_IOC_LOGICAL_INO:
3550d7728c96SJan Schmidt 		return btrfs_ioctl_logical_to_ino(root, argp);
35511406e432SJosef Bacik 	case BTRFS_IOC_SPACE_INFO:
35521406e432SJosef Bacik 		return btrfs_ioctl_space_info(root, argp);
3553f46b5a66SChristoph Hellwig 	case BTRFS_IOC_SYNC:
3554f46b5a66SChristoph Hellwig 		btrfs_sync_fs(file->f_dentry->d_sb, 1);
3555f46b5a66SChristoph Hellwig 		return 0;
355646204592SSage Weil 	case BTRFS_IOC_START_SYNC:
355746204592SSage Weil 		return btrfs_ioctl_start_sync(file, argp);
355846204592SSage Weil 	case BTRFS_IOC_WAIT_SYNC:
355946204592SSage Weil 		return btrfs_ioctl_wait_sync(file, argp);
3560475f6387SJan Schmidt 	case BTRFS_IOC_SCRUB:
3561475f6387SJan Schmidt 		return btrfs_ioctl_scrub(root, argp);
3562475f6387SJan Schmidt 	case BTRFS_IOC_SCRUB_CANCEL:
3563475f6387SJan Schmidt 		return btrfs_ioctl_scrub_cancel(root, argp);
3564475f6387SJan Schmidt 	case BTRFS_IOC_SCRUB_PROGRESS:
3565475f6387SJan Schmidt 		return btrfs_ioctl_scrub_progress(root, argp);
3566c9e9f97bSIlya Dryomov 	case BTRFS_IOC_BALANCE_V2:
35679ba1f6e4SLiu Bo 		return btrfs_ioctl_balance(file, argp);
3568837d5b6eSIlya Dryomov 	case BTRFS_IOC_BALANCE_CTL:
3569837d5b6eSIlya Dryomov 		return btrfs_ioctl_balance_ctl(root, arg);
357019a39dceSIlya Dryomov 	case BTRFS_IOC_BALANCE_PROGRESS:
357119a39dceSIlya Dryomov 		return btrfs_ioctl_balance_progress(root, argp);
35728ea05e3aSAlexander Block 	case BTRFS_IOC_SET_RECEIVED_SUBVOL:
35738ea05e3aSAlexander Block 		return btrfs_ioctl_set_received_subvol(file, argp);
3574c11d2c23SStefan Behrens 	case BTRFS_IOC_GET_DEV_STATS:
3575c11d2c23SStefan Behrens 		return btrfs_ioctl_get_dev_stats(root, argp, 0);
3576c11d2c23SStefan Behrens 	case BTRFS_IOC_GET_AND_RESET_DEV_STATS:
3577c11d2c23SStefan Behrens 		return btrfs_ioctl_get_dev_stats(root, argp, 1);
3578f46b5a66SChristoph Hellwig 	}
3579f46b5a66SChristoph Hellwig 
3580f46b5a66SChristoph Hellwig 	return -ENOTTY;
3581f46b5a66SChristoph Hellwig }
3582