xref: /openbmc/linux/fs/btrfs/ioctl.c (revision e54bfa31)
1f46b5a66SChristoph Hellwig /*
2f46b5a66SChristoph Hellwig  * Copyright (C) 2007 Oracle.  All rights reserved.
3f46b5a66SChristoph Hellwig  *
4f46b5a66SChristoph Hellwig  * This program is free software; you can redistribute it and/or
5f46b5a66SChristoph Hellwig  * modify it under the terms of the GNU General Public
6f46b5a66SChristoph Hellwig  * License v2 as published by the Free Software Foundation.
7f46b5a66SChristoph Hellwig  *
8f46b5a66SChristoph Hellwig  * This program is distributed in the hope that it will be useful,
9f46b5a66SChristoph Hellwig  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10f46b5a66SChristoph Hellwig  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11f46b5a66SChristoph Hellwig  * General Public License for more details.
12f46b5a66SChristoph Hellwig  *
13f46b5a66SChristoph Hellwig  * You should have received a copy of the GNU General Public
14f46b5a66SChristoph Hellwig  * License along with this program; if not, write to the
15f46b5a66SChristoph Hellwig  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
16f46b5a66SChristoph Hellwig  * Boston, MA 021110-1307, USA.
17f46b5a66SChristoph Hellwig  */
18f46b5a66SChristoph Hellwig 
19f46b5a66SChristoph Hellwig #include <linux/kernel.h>
20f46b5a66SChristoph Hellwig #include <linux/bio.h>
21f46b5a66SChristoph Hellwig #include <linux/buffer_head.h>
22f46b5a66SChristoph Hellwig #include <linux/file.h>
23f46b5a66SChristoph Hellwig #include <linux/fs.h>
24cb8e7090SChristoph Hellwig #include <linux/fsnotify.h>
25f46b5a66SChristoph Hellwig #include <linux/pagemap.h>
26f46b5a66SChristoph Hellwig #include <linux/highmem.h>
27f46b5a66SChristoph Hellwig #include <linux/time.h>
28f46b5a66SChristoph Hellwig #include <linux/init.h>
29f46b5a66SChristoph Hellwig #include <linux/string.h>
30f46b5a66SChristoph Hellwig #include <linux/backing-dev.h>
31cb8e7090SChristoph Hellwig #include <linux/mount.h>
32f46b5a66SChristoph Hellwig #include <linux/mpage.h>
33cb8e7090SChristoph Hellwig #include <linux/namei.h>
34f46b5a66SChristoph Hellwig #include <linux/swap.h>
35f46b5a66SChristoph Hellwig #include <linux/writeback.h>
36f46b5a66SChristoph Hellwig #include <linux/statfs.h>
37f46b5a66SChristoph Hellwig #include <linux/compat.h>
38f46b5a66SChristoph Hellwig #include <linux/bit_spinlock.h>
39cb8e7090SChristoph Hellwig #include <linux/security.h>
40f46b5a66SChristoph Hellwig #include <linux/xattr.h>
417ea394f1SYan Zheng #include <linux/vmalloc.h>
425a0e3ad6STejun Heo #include <linux/slab.h>
43f7039b1dSLi Dongyang #include <linux/blkdev.h>
444b4e25f2SChris Mason #include "compat.h"
45f46b5a66SChristoph Hellwig #include "ctree.h"
46f46b5a66SChristoph Hellwig #include "disk-io.h"
47f46b5a66SChristoph Hellwig #include "transaction.h"
48f46b5a66SChristoph Hellwig #include "btrfs_inode.h"
49f46b5a66SChristoph Hellwig #include "ioctl.h"
50f46b5a66SChristoph Hellwig #include "print-tree.h"
51f46b5a66SChristoph Hellwig #include "volumes.h"
52925baeddSChris Mason #include "locking.h"
53581bb050SLi Zefan #include "inode-map.h"
54d7728c96SJan Schmidt #include "backref.h"
55606686eeSJosef Bacik #include "rcu-string.h"
56f46b5a66SChristoph Hellwig 
576cbff00fSChristoph Hellwig /* Mask out flags that are inappropriate for the given type of inode. */
586cbff00fSChristoph Hellwig static inline __u32 btrfs_mask_flags(umode_t mode, __u32 flags)
596cbff00fSChristoph Hellwig {
606cbff00fSChristoph Hellwig 	if (S_ISDIR(mode))
616cbff00fSChristoph Hellwig 		return flags;
626cbff00fSChristoph Hellwig 	else if (S_ISREG(mode))
636cbff00fSChristoph Hellwig 		return flags & ~FS_DIRSYNC_FL;
646cbff00fSChristoph Hellwig 	else
656cbff00fSChristoph Hellwig 		return flags & (FS_NODUMP_FL | FS_NOATIME_FL);
666cbff00fSChristoph Hellwig }
67f46b5a66SChristoph Hellwig 
686cbff00fSChristoph Hellwig /*
696cbff00fSChristoph Hellwig  * Export inode flags to the format expected by the FS_IOC_GETFLAGS ioctl.
706cbff00fSChristoph Hellwig  */
716cbff00fSChristoph Hellwig static unsigned int btrfs_flags_to_ioctl(unsigned int flags)
726cbff00fSChristoph Hellwig {
736cbff00fSChristoph Hellwig 	unsigned int iflags = 0;
746cbff00fSChristoph Hellwig 
756cbff00fSChristoph Hellwig 	if (flags & BTRFS_INODE_SYNC)
766cbff00fSChristoph Hellwig 		iflags |= FS_SYNC_FL;
776cbff00fSChristoph Hellwig 	if (flags & BTRFS_INODE_IMMUTABLE)
786cbff00fSChristoph Hellwig 		iflags |= FS_IMMUTABLE_FL;
796cbff00fSChristoph Hellwig 	if (flags & BTRFS_INODE_APPEND)
806cbff00fSChristoph Hellwig 		iflags |= FS_APPEND_FL;
816cbff00fSChristoph Hellwig 	if (flags & BTRFS_INODE_NODUMP)
826cbff00fSChristoph Hellwig 		iflags |= FS_NODUMP_FL;
836cbff00fSChristoph Hellwig 	if (flags & BTRFS_INODE_NOATIME)
846cbff00fSChristoph Hellwig 		iflags |= FS_NOATIME_FL;
856cbff00fSChristoph Hellwig 	if (flags & BTRFS_INODE_DIRSYNC)
866cbff00fSChristoph Hellwig 		iflags |= FS_DIRSYNC_FL;
87d0092bddSLi Zefan 	if (flags & BTRFS_INODE_NODATACOW)
88d0092bddSLi Zefan 		iflags |= FS_NOCOW_FL;
89d0092bddSLi Zefan 
90d0092bddSLi Zefan 	if ((flags & BTRFS_INODE_COMPRESS) && !(flags & BTRFS_INODE_NOCOMPRESS))
91d0092bddSLi Zefan 		iflags |= FS_COMPR_FL;
92d0092bddSLi Zefan 	else if (flags & BTRFS_INODE_NOCOMPRESS)
93d0092bddSLi Zefan 		iflags |= FS_NOCOMP_FL;
946cbff00fSChristoph Hellwig 
956cbff00fSChristoph Hellwig 	return iflags;
966cbff00fSChristoph Hellwig }
976cbff00fSChristoph Hellwig 
986cbff00fSChristoph Hellwig /*
996cbff00fSChristoph Hellwig  * Update inode->i_flags based on the btrfs internal flags.
1006cbff00fSChristoph Hellwig  */
1016cbff00fSChristoph Hellwig void btrfs_update_iflags(struct inode *inode)
1026cbff00fSChristoph Hellwig {
1036cbff00fSChristoph Hellwig 	struct btrfs_inode *ip = BTRFS_I(inode);
1046cbff00fSChristoph Hellwig 
1056cbff00fSChristoph Hellwig 	inode->i_flags &= ~(S_SYNC|S_APPEND|S_IMMUTABLE|S_NOATIME|S_DIRSYNC);
1066cbff00fSChristoph Hellwig 
1076cbff00fSChristoph Hellwig 	if (ip->flags & BTRFS_INODE_SYNC)
1086cbff00fSChristoph Hellwig 		inode->i_flags |= S_SYNC;
1096cbff00fSChristoph Hellwig 	if (ip->flags & BTRFS_INODE_IMMUTABLE)
1106cbff00fSChristoph Hellwig 		inode->i_flags |= S_IMMUTABLE;
1116cbff00fSChristoph Hellwig 	if (ip->flags & BTRFS_INODE_APPEND)
1126cbff00fSChristoph Hellwig 		inode->i_flags |= S_APPEND;
1136cbff00fSChristoph Hellwig 	if (ip->flags & BTRFS_INODE_NOATIME)
1146cbff00fSChristoph Hellwig 		inode->i_flags |= S_NOATIME;
1156cbff00fSChristoph Hellwig 	if (ip->flags & BTRFS_INODE_DIRSYNC)
1166cbff00fSChristoph Hellwig 		inode->i_flags |= S_DIRSYNC;
1176cbff00fSChristoph Hellwig }
1186cbff00fSChristoph Hellwig 
1196cbff00fSChristoph Hellwig /*
1206cbff00fSChristoph Hellwig  * Inherit flags from the parent inode.
1216cbff00fSChristoph Hellwig  *
122e27425d6SJosef Bacik  * Currently only the compression flags and the cow flags are inherited.
1236cbff00fSChristoph Hellwig  */
1246cbff00fSChristoph Hellwig void btrfs_inherit_iflags(struct inode *inode, struct inode *dir)
1256cbff00fSChristoph Hellwig {
1260b4dcea5SChris Mason 	unsigned int flags;
1270b4dcea5SChris Mason 
1280b4dcea5SChris Mason 	if (!dir)
1290b4dcea5SChris Mason 		return;
1300b4dcea5SChris Mason 
1310b4dcea5SChris Mason 	flags = BTRFS_I(dir)->flags;
1326cbff00fSChristoph Hellwig 
133e27425d6SJosef Bacik 	if (flags & BTRFS_INODE_NOCOMPRESS) {
134e27425d6SJosef Bacik 		BTRFS_I(inode)->flags &= ~BTRFS_INODE_COMPRESS;
135e27425d6SJosef Bacik 		BTRFS_I(inode)->flags |= BTRFS_INODE_NOCOMPRESS;
136e27425d6SJosef Bacik 	} else if (flags & BTRFS_INODE_COMPRESS) {
137e27425d6SJosef Bacik 		BTRFS_I(inode)->flags &= ~BTRFS_INODE_NOCOMPRESS;
138e27425d6SJosef Bacik 		BTRFS_I(inode)->flags |= BTRFS_INODE_COMPRESS;
139e27425d6SJosef Bacik 	}
1406cbff00fSChristoph Hellwig 
141e27425d6SJosef Bacik 	if (flags & BTRFS_INODE_NODATACOW)
142e27425d6SJosef Bacik 		BTRFS_I(inode)->flags |= BTRFS_INODE_NODATACOW;
143e27425d6SJosef Bacik 
1446cbff00fSChristoph Hellwig 	btrfs_update_iflags(inode);
1456cbff00fSChristoph Hellwig }
1466cbff00fSChristoph Hellwig 
1476cbff00fSChristoph Hellwig static int btrfs_ioctl_getflags(struct file *file, void __user *arg)
1486cbff00fSChristoph Hellwig {
1496cbff00fSChristoph Hellwig 	struct btrfs_inode *ip = BTRFS_I(file->f_path.dentry->d_inode);
1506cbff00fSChristoph Hellwig 	unsigned int flags = btrfs_flags_to_ioctl(ip->flags);
1516cbff00fSChristoph Hellwig 
1526cbff00fSChristoph Hellwig 	if (copy_to_user(arg, &flags, sizeof(flags)))
1536cbff00fSChristoph Hellwig 		return -EFAULT;
1546cbff00fSChristoph Hellwig 	return 0;
1556cbff00fSChristoph Hellwig }
1566cbff00fSChristoph Hellwig 
15775e7cb7fSLiu Bo static int check_flags(unsigned int flags)
15875e7cb7fSLiu Bo {
15975e7cb7fSLiu Bo 	if (flags & ~(FS_IMMUTABLE_FL | FS_APPEND_FL | \
16075e7cb7fSLiu Bo 		      FS_NOATIME_FL | FS_NODUMP_FL | \
16175e7cb7fSLiu Bo 		      FS_SYNC_FL | FS_DIRSYNC_FL | \
162e1e8fb6aSLi Zefan 		      FS_NOCOMP_FL | FS_COMPR_FL |
163e1e8fb6aSLi Zefan 		      FS_NOCOW_FL))
16475e7cb7fSLiu Bo 		return -EOPNOTSUPP;
16575e7cb7fSLiu Bo 
16675e7cb7fSLiu Bo 	if ((flags & FS_NOCOMP_FL) && (flags & FS_COMPR_FL))
16775e7cb7fSLiu Bo 		return -EINVAL;
16875e7cb7fSLiu Bo 
16975e7cb7fSLiu Bo 	return 0;
17075e7cb7fSLiu Bo }
17175e7cb7fSLiu Bo 
1726cbff00fSChristoph Hellwig static int btrfs_ioctl_setflags(struct file *file, void __user *arg)
1736cbff00fSChristoph Hellwig {
1746cbff00fSChristoph Hellwig 	struct inode *inode = file->f_path.dentry->d_inode;
1756cbff00fSChristoph Hellwig 	struct btrfs_inode *ip = BTRFS_I(inode);
1766cbff00fSChristoph Hellwig 	struct btrfs_root *root = ip->root;
1776cbff00fSChristoph Hellwig 	struct btrfs_trans_handle *trans;
1786cbff00fSChristoph Hellwig 	unsigned int flags, oldflags;
1796cbff00fSChristoph Hellwig 	int ret;
180f062abf0SLi Zefan 	u64 ip_oldflags;
181f062abf0SLi Zefan 	unsigned int i_oldflags;
1826cbff00fSChristoph Hellwig 
183b83cc969SLi Zefan 	if (btrfs_root_readonly(root))
184b83cc969SLi Zefan 		return -EROFS;
185b83cc969SLi Zefan 
1866cbff00fSChristoph Hellwig 	if (copy_from_user(&flags, arg, sizeof(flags)))
1876cbff00fSChristoph Hellwig 		return -EFAULT;
1886cbff00fSChristoph Hellwig 
18975e7cb7fSLiu Bo 	ret = check_flags(flags);
19075e7cb7fSLiu Bo 	if (ret)
19175e7cb7fSLiu Bo 		return ret;
1926cbff00fSChristoph Hellwig 
1932e149670SSerge E. Hallyn 	if (!inode_owner_or_capable(inode))
1946cbff00fSChristoph Hellwig 		return -EACCES;
1956cbff00fSChristoph Hellwig 
1966cbff00fSChristoph Hellwig 	mutex_lock(&inode->i_mutex);
1976cbff00fSChristoph Hellwig 
198f062abf0SLi Zefan 	ip_oldflags = ip->flags;
199f062abf0SLi Zefan 	i_oldflags = inode->i_flags;
200f062abf0SLi Zefan 
2016cbff00fSChristoph Hellwig 	flags = btrfs_mask_flags(inode->i_mode, flags);
2026cbff00fSChristoph Hellwig 	oldflags = btrfs_flags_to_ioctl(ip->flags);
2036cbff00fSChristoph Hellwig 	if ((flags ^ oldflags) & (FS_APPEND_FL | FS_IMMUTABLE_FL)) {
2046cbff00fSChristoph Hellwig 		if (!capable(CAP_LINUX_IMMUTABLE)) {
2056cbff00fSChristoph Hellwig 			ret = -EPERM;
2066cbff00fSChristoph Hellwig 			goto out_unlock;
2076cbff00fSChristoph Hellwig 		}
2086cbff00fSChristoph Hellwig 	}
2096cbff00fSChristoph Hellwig 
210a561be71SAl Viro 	ret = mnt_want_write_file(file);
2116cbff00fSChristoph Hellwig 	if (ret)
2126cbff00fSChristoph Hellwig 		goto out_unlock;
2136cbff00fSChristoph Hellwig 
2146cbff00fSChristoph Hellwig 	if (flags & FS_SYNC_FL)
2156cbff00fSChristoph Hellwig 		ip->flags |= BTRFS_INODE_SYNC;
2166cbff00fSChristoph Hellwig 	else
2176cbff00fSChristoph Hellwig 		ip->flags &= ~BTRFS_INODE_SYNC;
2186cbff00fSChristoph Hellwig 	if (flags & FS_IMMUTABLE_FL)
2196cbff00fSChristoph Hellwig 		ip->flags |= BTRFS_INODE_IMMUTABLE;
2206cbff00fSChristoph Hellwig 	else
2216cbff00fSChristoph Hellwig 		ip->flags &= ~BTRFS_INODE_IMMUTABLE;
2226cbff00fSChristoph Hellwig 	if (flags & FS_APPEND_FL)
2236cbff00fSChristoph Hellwig 		ip->flags |= BTRFS_INODE_APPEND;
2246cbff00fSChristoph Hellwig 	else
2256cbff00fSChristoph Hellwig 		ip->flags &= ~BTRFS_INODE_APPEND;
2266cbff00fSChristoph Hellwig 	if (flags & FS_NODUMP_FL)
2276cbff00fSChristoph Hellwig 		ip->flags |= BTRFS_INODE_NODUMP;
2286cbff00fSChristoph Hellwig 	else
2296cbff00fSChristoph Hellwig 		ip->flags &= ~BTRFS_INODE_NODUMP;
2306cbff00fSChristoph Hellwig 	if (flags & FS_NOATIME_FL)
2316cbff00fSChristoph Hellwig 		ip->flags |= BTRFS_INODE_NOATIME;
2326cbff00fSChristoph Hellwig 	else
2336cbff00fSChristoph Hellwig 		ip->flags &= ~BTRFS_INODE_NOATIME;
2346cbff00fSChristoph Hellwig 	if (flags & FS_DIRSYNC_FL)
2356cbff00fSChristoph Hellwig 		ip->flags |= BTRFS_INODE_DIRSYNC;
2366cbff00fSChristoph Hellwig 	else
2376cbff00fSChristoph Hellwig 		ip->flags &= ~BTRFS_INODE_DIRSYNC;
238e1e8fb6aSLi Zefan 	if (flags & FS_NOCOW_FL)
239e1e8fb6aSLi Zefan 		ip->flags |= BTRFS_INODE_NODATACOW;
240e1e8fb6aSLi Zefan 	else
241e1e8fb6aSLi Zefan 		ip->flags &= ~BTRFS_INODE_NODATACOW;
2426cbff00fSChristoph Hellwig 
24375e7cb7fSLiu Bo 	/*
24475e7cb7fSLiu Bo 	 * The COMPRESS flag can only be changed by users, while the NOCOMPRESS
24575e7cb7fSLiu Bo 	 * flag may be changed automatically if compression code won't make
24675e7cb7fSLiu Bo 	 * things smaller.
24775e7cb7fSLiu Bo 	 */
24875e7cb7fSLiu Bo 	if (flags & FS_NOCOMP_FL) {
24975e7cb7fSLiu Bo 		ip->flags &= ~BTRFS_INODE_COMPRESS;
25075e7cb7fSLiu Bo 		ip->flags |= BTRFS_INODE_NOCOMPRESS;
25175e7cb7fSLiu Bo 	} else if (flags & FS_COMPR_FL) {
25275e7cb7fSLiu Bo 		ip->flags |= BTRFS_INODE_COMPRESS;
25375e7cb7fSLiu Bo 		ip->flags &= ~BTRFS_INODE_NOCOMPRESS;
254ebcb904dSLi Zefan 	} else {
255ebcb904dSLi Zefan 		ip->flags &= ~(BTRFS_INODE_COMPRESS | BTRFS_INODE_NOCOMPRESS);
25675e7cb7fSLiu Bo 	}
2576cbff00fSChristoph Hellwig 
2584da6f1a3SLi Zefan 	trans = btrfs_start_transaction(root, 1);
259f062abf0SLi Zefan 	if (IS_ERR(trans)) {
260f062abf0SLi Zefan 		ret = PTR_ERR(trans);
261f062abf0SLi Zefan 		goto out_drop;
262f062abf0SLi Zefan 	}
2636cbff00fSChristoph Hellwig 
264306424ccSLi Zefan 	btrfs_update_iflags(inode);
2650c4d2d95SJosef Bacik 	inode_inc_iversion(inode);
266306424ccSLi Zefan 	inode->i_ctime = CURRENT_TIME;
2676cbff00fSChristoph Hellwig 	ret = btrfs_update_inode(trans, root, inode);
2686cbff00fSChristoph Hellwig 
2696cbff00fSChristoph Hellwig 	btrfs_end_transaction(trans, root);
270f062abf0SLi Zefan  out_drop:
271f062abf0SLi Zefan 	if (ret) {
272f062abf0SLi Zefan 		ip->flags = ip_oldflags;
273f062abf0SLi Zefan 		inode->i_flags = i_oldflags;
274f062abf0SLi Zefan 	}
2756cbff00fSChristoph Hellwig 
2762a79f17eSAl Viro 	mnt_drop_write_file(file);
2776cbff00fSChristoph Hellwig  out_unlock:
2786cbff00fSChristoph Hellwig 	mutex_unlock(&inode->i_mutex);
2792d4e6f6aSliubo 	return ret;
2806cbff00fSChristoph Hellwig }
2816cbff00fSChristoph Hellwig 
2826cbff00fSChristoph Hellwig static int btrfs_ioctl_getversion(struct file *file, int __user *arg)
2836cbff00fSChristoph Hellwig {
2846cbff00fSChristoph Hellwig 	struct inode *inode = file->f_path.dentry->d_inode;
2856cbff00fSChristoph Hellwig 
2866cbff00fSChristoph Hellwig 	return put_user(inode->i_generation, arg);
2876cbff00fSChristoph Hellwig }
288f46b5a66SChristoph Hellwig 
289f7039b1dSLi Dongyang static noinline int btrfs_ioctl_fitrim(struct file *file, void __user *arg)
290f7039b1dSLi Dongyang {
291815745cfSAl Viro 	struct btrfs_fs_info *fs_info = btrfs_sb(fdentry(file)->d_sb);
292f7039b1dSLi Dongyang 	struct btrfs_device *device;
293f7039b1dSLi Dongyang 	struct request_queue *q;
294f7039b1dSLi Dongyang 	struct fstrim_range range;
295f7039b1dSLi Dongyang 	u64 minlen = ULLONG_MAX;
296f7039b1dSLi Dongyang 	u64 num_devices = 0;
297815745cfSAl Viro 	u64 total_bytes = btrfs_super_total_bytes(fs_info->super_copy);
298f7039b1dSLi Dongyang 	int ret;
299f7039b1dSLi Dongyang 
300f7039b1dSLi Dongyang 	if (!capable(CAP_SYS_ADMIN))
301f7039b1dSLi Dongyang 		return -EPERM;
302f7039b1dSLi Dongyang 
3031f78160cSXiao Guangrong 	rcu_read_lock();
3041f78160cSXiao Guangrong 	list_for_each_entry_rcu(device, &fs_info->fs_devices->devices,
3051f78160cSXiao Guangrong 				dev_list) {
306f7039b1dSLi Dongyang 		if (!device->bdev)
307f7039b1dSLi Dongyang 			continue;
308f7039b1dSLi Dongyang 		q = bdev_get_queue(device->bdev);
309f7039b1dSLi Dongyang 		if (blk_queue_discard(q)) {
310f7039b1dSLi Dongyang 			num_devices++;
311f7039b1dSLi Dongyang 			minlen = min((u64)q->limits.discard_granularity,
312f7039b1dSLi Dongyang 				     minlen);
313f7039b1dSLi Dongyang 		}
314f7039b1dSLi Dongyang 	}
3151f78160cSXiao Guangrong 	rcu_read_unlock();
316f4c697e6SLukas Czerner 
317f7039b1dSLi Dongyang 	if (!num_devices)
318f7039b1dSLi Dongyang 		return -EOPNOTSUPP;
319f7039b1dSLi Dongyang 	if (copy_from_user(&range, arg, sizeof(range)))
320f7039b1dSLi Dongyang 		return -EFAULT;
321f4c697e6SLukas Czerner 	if (range.start > total_bytes)
322f4c697e6SLukas Czerner 		return -EINVAL;
323f7039b1dSLi Dongyang 
324f4c697e6SLukas Czerner 	range.len = min(range.len, total_bytes - range.start);
325f7039b1dSLi Dongyang 	range.minlen = max(range.minlen, minlen);
326815745cfSAl Viro 	ret = btrfs_trim_fs(fs_info->tree_root, &range);
327f7039b1dSLi Dongyang 	if (ret < 0)
328f7039b1dSLi Dongyang 		return ret;
329f7039b1dSLi Dongyang 
330f7039b1dSLi Dongyang 	if (copy_to_user(arg, &range, sizeof(range)))
331f7039b1dSLi Dongyang 		return -EFAULT;
332f7039b1dSLi Dongyang 
333f7039b1dSLi Dongyang 	return 0;
334f7039b1dSLi Dongyang }
335f7039b1dSLi Dongyang 
336cb8e7090SChristoph Hellwig static noinline int create_subvol(struct btrfs_root *root,
337cb8e7090SChristoph Hellwig 				  struct dentry *dentry,
33872fd032eSSage Weil 				  char *name, int namelen,
33972fd032eSSage Weil 				  u64 *async_transid)
340f46b5a66SChristoph Hellwig {
341f46b5a66SChristoph Hellwig 	struct btrfs_trans_handle *trans;
342f46b5a66SChristoph Hellwig 	struct btrfs_key key;
343f46b5a66SChristoph Hellwig 	struct btrfs_root_item root_item;
344f46b5a66SChristoph Hellwig 	struct btrfs_inode_item *inode_item;
345f46b5a66SChristoph Hellwig 	struct extent_buffer *leaf;
34676dda93cSYan, Zheng 	struct btrfs_root *new_root;
3472fbe8c8aSAl Viro 	struct dentry *parent = dentry->d_parent;
3486a912213SJosef Bacik 	struct inode *dir;
349f46b5a66SChristoph Hellwig 	int ret;
350f46b5a66SChristoph Hellwig 	int err;
351f46b5a66SChristoph Hellwig 	u64 objectid;
352f46b5a66SChristoph Hellwig 	u64 new_dirid = BTRFS_FIRST_FREE_OBJECTID;
3533de4586cSChris Mason 	u64 index = 0;
354f46b5a66SChristoph Hellwig 
355581bb050SLi Zefan 	ret = btrfs_find_free_objectid(root->fs_info->tree_root, &objectid);
3562fbe8c8aSAl Viro 	if (ret)
357a22285a6SYan, Zheng 		return ret;
3586a912213SJosef Bacik 
3596a912213SJosef Bacik 	dir = parent->d_inode;
3606a912213SJosef Bacik 
3619ed74f2dSJosef Bacik 	/*
3629ed74f2dSJosef Bacik 	 * 1 - inode item
3639ed74f2dSJosef Bacik 	 * 2 - refs
3649ed74f2dSJosef Bacik 	 * 1 - root item
3659ed74f2dSJosef Bacik 	 * 2 - dir items
3669ed74f2dSJosef Bacik 	 */
367a22285a6SYan, Zheng 	trans = btrfs_start_transaction(root, 6);
3682fbe8c8aSAl Viro 	if (IS_ERR(trans))
369a22285a6SYan, Zheng 		return PTR_ERR(trans);
370f46b5a66SChristoph Hellwig 
3715d4f98a2SYan Zheng 	leaf = btrfs_alloc_free_block(trans, root, root->leafsize,
3725581a51aSJan Schmidt 				      0, objectid, NULL, 0, 0, 0);
3738e8a1e31SJosef Bacik 	if (IS_ERR(leaf)) {
3748e8a1e31SJosef Bacik 		ret = PTR_ERR(leaf);
3758e8a1e31SJosef Bacik 		goto fail;
3768e8a1e31SJosef Bacik 	}
377f46b5a66SChristoph Hellwig 
3785d4f98a2SYan Zheng 	memset_extent_buffer(leaf, 0, 0, sizeof(struct btrfs_header));
379f46b5a66SChristoph Hellwig 	btrfs_set_header_bytenr(leaf, leaf->start);
380f46b5a66SChristoph Hellwig 	btrfs_set_header_generation(leaf, trans->transid);
3815d4f98a2SYan Zheng 	btrfs_set_header_backref_rev(leaf, BTRFS_MIXED_BACKREF_REV);
382f46b5a66SChristoph Hellwig 	btrfs_set_header_owner(leaf, objectid);
383f46b5a66SChristoph Hellwig 
384f46b5a66SChristoph Hellwig 	write_extent_buffer(leaf, root->fs_info->fsid,
385f46b5a66SChristoph Hellwig 			    (unsigned long)btrfs_header_fsid(leaf),
386f46b5a66SChristoph Hellwig 			    BTRFS_FSID_SIZE);
3875d4f98a2SYan Zheng 	write_extent_buffer(leaf, root->fs_info->chunk_tree_uuid,
3885d4f98a2SYan Zheng 			    (unsigned long)btrfs_header_chunk_tree_uuid(leaf),
3895d4f98a2SYan Zheng 			    BTRFS_UUID_SIZE);
390f46b5a66SChristoph Hellwig 	btrfs_mark_buffer_dirty(leaf);
391f46b5a66SChristoph Hellwig 
392f46b5a66SChristoph Hellwig 	inode_item = &root_item.inode;
393f46b5a66SChristoph Hellwig 	memset(inode_item, 0, sizeof(*inode_item));
394f46b5a66SChristoph Hellwig 	inode_item->generation = cpu_to_le64(1);
395f46b5a66SChristoph Hellwig 	inode_item->size = cpu_to_le64(3);
396f46b5a66SChristoph Hellwig 	inode_item->nlink = cpu_to_le32(1);
397a76a3cd4SYan Zheng 	inode_item->nbytes = cpu_to_le64(root->leafsize);
398f46b5a66SChristoph Hellwig 	inode_item->mode = cpu_to_le32(S_IFDIR | 0755);
399f46b5a66SChristoph Hellwig 
40008fe4db1SLi Zefan 	root_item.flags = 0;
40108fe4db1SLi Zefan 	root_item.byte_limit = 0;
40208fe4db1SLi Zefan 	inode_item->flags = cpu_to_le64(BTRFS_INODE_ROOT_ITEM_INIT);
40308fe4db1SLi Zefan 
404f46b5a66SChristoph Hellwig 	btrfs_set_root_bytenr(&root_item, leaf->start);
40584234f3aSYan Zheng 	btrfs_set_root_generation(&root_item, trans->transid);
406f46b5a66SChristoph Hellwig 	btrfs_set_root_level(&root_item, 0);
407f46b5a66SChristoph Hellwig 	btrfs_set_root_refs(&root_item, 1);
40886b9f2ecSYan, Zheng 	btrfs_set_root_used(&root_item, leaf->len);
40980ff3856SYan Zheng 	btrfs_set_root_last_snapshot(&root_item, 0);
410f46b5a66SChristoph Hellwig 
411f46b5a66SChristoph Hellwig 	memset(&root_item.drop_progress, 0, sizeof(root_item.drop_progress));
412f46b5a66SChristoph Hellwig 	root_item.drop_level = 0;
413f46b5a66SChristoph Hellwig 
414925baeddSChris Mason 	btrfs_tree_unlock(leaf);
415f46b5a66SChristoph Hellwig 	free_extent_buffer(leaf);
416f46b5a66SChristoph Hellwig 	leaf = NULL;
417f46b5a66SChristoph Hellwig 
418f46b5a66SChristoph Hellwig 	btrfs_set_root_dirid(&root_item, new_dirid);
419f46b5a66SChristoph Hellwig 
420f46b5a66SChristoph Hellwig 	key.objectid = objectid;
4215d4f98a2SYan Zheng 	key.offset = 0;
422f46b5a66SChristoph Hellwig 	btrfs_set_key_type(&key, BTRFS_ROOT_ITEM_KEY);
423f46b5a66SChristoph Hellwig 	ret = btrfs_insert_root(trans, root->fs_info->tree_root, &key,
424f46b5a66SChristoph Hellwig 				&root_item);
425f46b5a66SChristoph Hellwig 	if (ret)
426f46b5a66SChristoph Hellwig 		goto fail;
427f46b5a66SChristoph Hellwig 
42876dda93cSYan, Zheng 	key.offset = (u64)-1;
42976dda93cSYan, Zheng 	new_root = btrfs_read_fs_root_no_name(root->fs_info, &key);
43079787eaaSJeff Mahoney 	if (IS_ERR(new_root)) {
43179787eaaSJeff Mahoney 		btrfs_abort_transaction(trans, root, PTR_ERR(new_root));
43279787eaaSJeff Mahoney 		ret = PTR_ERR(new_root);
43379787eaaSJeff Mahoney 		goto fail;
43479787eaaSJeff Mahoney 	}
43576dda93cSYan, Zheng 
43676dda93cSYan, Zheng 	btrfs_record_root_in_trans(trans, new_root);
43776dda93cSYan, Zheng 
438d82a6f1dSJosef Bacik 	ret = btrfs_create_subvol_root(trans, new_root, new_dirid);
439ce598979SMark Fasheh 	if (ret) {
440ce598979SMark Fasheh 		/* We potentially lose an unused inode item here */
44179787eaaSJeff Mahoney 		btrfs_abort_transaction(trans, root, ret);
442ce598979SMark Fasheh 		goto fail;
443ce598979SMark Fasheh 	}
444ce598979SMark Fasheh 
445f46b5a66SChristoph Hellwig 	/*
446f46b5a66SChristoph Hellwig 	 * insert the directory item
447f46b5a66SChristoph Hellwig 	 */
4483de4586cSChris Mason 	ret = btrfs_set_inode_index(dir, &index);
44979787eaaSJeff Mahoney 	if (ret) {
45079787eaaSJeff Mahoney 		btrfs_abort_transaction(trans, root, ret);
45179787eaaSJeff Mahoney 		goto fail;
45279787eaaSJeff Mahoney 	}
4533de4586cSChris Mason 
4543de4586cSChris Mason 	ret = btrfs_insert_dir_item(trans, root,
45516cdcec7SMiao Xie 				    name, namelen, dir, &key,
4563de4586cSChris Mason 				    BTRFS_FT_DIR, index);
45779787eaaSJeff Mahoney 	if (ret) {
45879787eaaSJeff Mahoney 		btrfs_abort_transaction(trans, root, ret);
459f46b5a66SChristoph Hellwig 		goto fail;
46079787eaaSJeff Mahoney 	}
4610660b5afSChris Mason 
46252c26179SYan Zheng 	btrfs_i_size_write(dir, dir->i_size + namelen * 2);
46352c26179SYan Zheng 	ret = btrfs_update_inode(trans, root, dir);
46452c26179SYan Zheng 	BUG_ON(ret);
46552c26179SYan Zheng 
4660660b5afSChris Mason 	ret = btrfs_add_root_ref(trans, root->fs_info->tree_root,
4674df27c4dSYan, Zheng 				 objectid, root->root_key.objectid,
46833345d01SLi Zefan 				 btrfs_ino(dir), index, name, namelen);
46976dda93cSYan, Zheng 
4700660b5afSChris Mason 	BUG_ON(ret);
4710660b5afSChris Mason 
47276dda93cSYan, Zheng 	d_instantiate(dentry, btrfs_lookup_dentry(dir, dentry));
473f46b5a66SChristoph Hellwig fail:
47472fd032eSSage Weil 	if (async_transid) {
47572fd032eSSage Weil 		*async_transid = trans->transid;
47672fd032eSSage Weil 		err = btrfs_commit_transaction_async(trans, root, 1);
47772fd032eSSage Weil 	} else {
47876dda93cSYan, Zheng 		err = btrfs_commit_transaction(trans, root);
47972fd032eSSage Weil 	}
480f46b5a66SChristoph Hellwig 	if (err && !ret)
481f46b5a66SChristoph Hellwig 		ret = err;
482f46b5a66SChristoph Hellwig 	return ret;
483f46b5a66SChristoph Hellwig }
484f46b5a66SChristoph Hellwig 
48572fd032eSSage Weil static int create_snapshot(struct btrfs_root *root, struct dentry *dentry,
486b83cc969SLi Zefan 			   char *name, int namelen, u64 *async_transid,
487b83cc969SLi Zefan 			   bool readonly)
488f46b5a66SChristoph Hellwig {
4892e4bfab9SYan, Zheng 	struct inode *inode;
490f46b5a66SChristoph Hellwig 	struct btrfs_pending_snapshot *pending_snapshot;
491f46b5a66SChristoph Hellwig 	struct btrfs_trans_handle *trans;
4922e4bfab9SYan, Zheng 	int ret;
493f46b5a66SChristoph Hellwig 
494f46b5a66SChristoph Hellwig 	if (!root->ref_cows)
495f46b5a66SChristoph Hellwig 		return -EINVAL;
496f46b5a66SChristoph Hellwig 
497a22285a6SYan, Zheng 	pending_snapshot = kzalloc(sizeof(*pending_snapshot), GFP_NOFS);
498a22285a6SYan, Zheng 	if (!pending_snapshot)
499a22285a6SYan, Zheng 		return -ENOMEM;
500a22285a6SYan, Zheng 
501a22285a6SYan, Zheng 	btrfs_init_block_rsv(&pending_snapshot->block_rsv);
502a22285a6SYan, Zheng 	pending_snapshot->dentry = dentry;
503a22285a6SYan, Zheng 	pending_snapshot->root = root;
504b83cc969SLi Zefan 	pending_snapshot->readonly = readonly;
505a22285a6SYan, Zheng 
506a22285a6SYan, Zheng 	trans = btrfs_start_transaction(root->fs_info->extent_root, 5);
507a22285a6SYan, Zheng 	if (IS_ERR(trans)) {
508a22285a6SYan, Zheng 		ret = PTR_ERR(trans);
509a22285a6SYan, Zheng 		goto fail;
510a22285a6SYan, Zheng 	}
511a22285a6SYan, Zheng 
512a22285a6SYan, Zheng 	ret = btrfs_snap_reserve_metadata(trans, pending_snapshot);
513a22285a6SYan, Zheng 	BUG_ON(ret);
514a22285a6SYan, Zheng 
5158351583eSJosef Bacik 	spin_lock(&root->fs_info->trans_lock);
516a22285a6SYan, Zheng 	list_add(&pending_snapshot->list,
517a22285a6SYan, Zheng 		 &trans->transaction->pending_snapshots);
5188351583eSJosef Bacik 	spin_unlock(&root->fs_info->trans_lock);
51972fd032eSSage Weil 	if (async_transid) {
52072fd032eSSage Weil 		*async_transid = trans->transid;
52172fd032eSSage Weil 		ret = btrfs_commit_transaction_async(trans,
52272fd032eSSage Weil 				     root->fs_info->extent_root, 1);
52372fd032eSSage Weil 	} else {
52472fd032eSSage Weil 		ret = btrfs_commit_transaction(trans,
52572fd032eSSage Weil 					       root->fs_info->extent_root);
52672fd032eSSage Weil 	}
527a22285a6SYan, Zheng 	BUG_ON(ret);
528a22285a6SYan, Zheng 
529a22285a6SYan, Zheng 	ret = pending_snapshot->error;
530f46b5a66SChristoph Hellwig 	if (ret)
5312e4bfab9SYan, Zheng 		goto fail;
532f46b5a66SChristoph Hellwig 
53366b4ffd1SJosef Bacik 	ret = btrfs_orphan_cleanup(pending_snapshot->snap);
53466b4ffd1SJosef Bacik 	if (ret)
53566b4ffd1SJosef Bacik 		goto fail;
536f46b5a66SChristoph Hellwig 
5372fbe8c8aSAl Viro 	inode = btrfs_lookup_dentry(dentry->d_parent->d_inode, dentry);
5382e4bfab9SYan, Zheng 	if (IS_ERR(inode)) {
5392e4bfab9SYan, Zheng 		ret = PTR_ERR(inode);
5402e4bfab9SYan, Zheng 		goto fail;
5412e4bfab9SYan, Zheng 	}
5422e4bfab9SYan, Zheng 	BUG_ON(!inode);
5432e4bfab9SYan, Zheng 	d_instantiate(dentry, inode);
5442e4bfab9SYan, Zheng 	ret = 0;
5452e4bfab9SYan, Zheng fail:
546a22285a6SYan, Zheng 	kfree(pending_snapshot);
547f46b5a66SChristoph Hellwig 	return ret;
548f46b5a66SChristoph Hellwig }
549f46b5a66SChristoph Hellwig 
5504260f7c7SSage Weil /*  copy of check_sticky in fs/namei.c()
5514260f7c7SSage Weil * It's inline, so penalty for filesystems that don't use sticky bit is
5524260f7c7SSage Weil * minimal.
5534260f7c7SSage Weil */
5544260f7c7SSage Weil static inline int btrfs_check_sticky(struct inode *dir, struct inode *inode)
5554260f7c7SSage Weil {
5564260f7c7SSage Weil 	uid_t fsuid = current_fsuid();
5574260f7c7SSage Weil 
5584260f7c7SSage Weil 	if (!(dir->i_mode & S_ISVTX))
5594260f7c7SSage Weil 		return 0;
5604260f7c7SSage Weil 	if (inode->i_uid == fsuid)
5614260f7c7SSage Weil 		return 0;
5624260f7c7SSage Weil 	if (dir->i_uid == fsuid)
5634260f7c7SSage Weil 		return 0;
5644260f7c7SSage Weil 	return !capable(CAP_FOWNER);
5654260f7c7SSage Weil }
5664260f7c7SSage Weil 
5674260f7c7SSage Weil /*  copy of may_delete in fs/namei.c()
5684260f7c7SSage Weil  *	Check whether we can remove a link victim from directory dir, check
5694260f7c7SSage Weil  *  whether the type of victim is right.
5704260f7c7SSage Weil  *  1. We can't do it if dir is read-only (done in permission())
5714260f7c7SSage Weil  *  2. We should have write and exec permissions on dir
5724260f7c7SSage Weil  *  3. We can't remove anything from append-only dir
5734260f7c7SSage Weil  *  4. We can't do anything with immutable dir (done in permission())
5744260f7c7SSage Weil  *  5. If the sticky bit on dir is set we should either
5754260f7c7SSage Weil  *	a. be owner of dir, or
5764260f7c7SSage Weil  *	b. be owner of victim, or
5774260f7c7SSage Weil  *	c. have CAP_FOWNER capability
5784260f7c7SSage Weil  *  6. If the victim is append-only or immutable we can't do antyhing with
5794260f7c7SSage Weil  *     links pointing to it.
5804260f7c7SSage Weil  *  7. If we were asked to remove a directory and victim isn't one - ENOTDIR.
5814260f7c7SSage Weil  *  8. If we were asked to remove a non-directory and victim isn't one - EISDIR.
5824260f7c7SSage Weil  *  9. We can't remove a root or mountpoint.
5834260f7c7SSage Weil  * 10. We don't allow removal of NFS sillyrenamed files; it's handled by
5844260f7c7SSage Weil  *     nfs_async_unlink().
5854260f7c7SSage Weil  */
5864260f7c7SSage Weil 
5874260f7c7SSage Weil static int btrfs_may_delete(struct inode *dir,struct dentry *victim,int isdir)
5884260f7c7SSage Weil {
5894260f7c7SSage Weil 	int error;
5904260f7c7SSage Weil 
5914260f7c7SSage Weil 	if (!victim->d_inode)
5924260f7c7SSage Weil 		return -ENOENT;
5934260f7c7SSage Weil 
5944260f7c7SSage Weil 	BUG_ON(victim->d_parent->d_inode != dir);
5954260f7c7SSage Weil 	audit_inode_child(victim, dir);
5964260f7c7SSage Weil 
5974260f7c7SSage Weil 	error = inode_permission(dir, MAY_WRITE | MAY_EXEC);
5984260f7c7SSage Weil 	if (error)
5994260f7c7SSage Weil 		return error;
6004260f7c7SSage Weil 	if (IS_APPEND(dir))
6014260f7c7SSage Weil 		return -EPERM;
6024260f7c7SSage Weil 	if (btrfs_check_sticky(dir, victim->d_inode)||
6034260f7c7SSage Weil 		IS_APPEND(victim->d_inode)||
6044260f7c7SSage Weil 	    IS_IMMUTABLE(victim->d_inode) || IS_SWAPFILE(victim->d_inode))
6054260f7c7SSage Weil 		return -EPERM;
6064260f7c7SSage Weil 	if (isdir) {
6074260f7c7SSage Weil 		if (!S_ISDIR(victim->d_inode->i_mode))
6084260f7c7SSage Weil 			return -ENOTDIR;
6094260f7c7SSage Weil 		if (IS_ROOT(victim))
6104260f7c7SSage Weil 			return -EBUSY;
6114260f7c7SSage Weil 	} else if (S_ISDIR(victim->d_inode->i_mode))
6124260f7c7SSage Weil 		return -EISDIR;
6134260f7c7SSage Weil 	if (IS_DEADDIR(dir))
6144260f7c7SSage Weil 		return -ENOENT;
6154260f7c7SSage Weil 	if (victim->d_flags & DCACHE_NFSFS_RENAMED)
6164260f7c7SSage Weil 		return -EBUSY;
6174260f7c7SSage Weil 	return 0;
6184260f7c7SSage Weil }
6194260f7c7SSage Weil 
620cb8e7090SChristoph Hellwig /* copy of may_create in fs/namei.c() */
621cb8e7090SChristoph Hellwig static inline int btrfs_may_create(struct inode *dir, struct dentry *child)
622cb8e7090SChristoph Hellwig {
623cb8e7090SChristoph Hellwig 	if (child->d_inode)
624cb8e7090SChristoph Hellwig 		return -EEXIST;
625cb8e7090SChristoph Hellwig 	if (IS_DEADDIR(dir))
626cb8e7090SChristoph Hellwig 		return -ENOENT;
627cb8e7090SChristoph Hellwig 	return inode_permission(dir, MAY_WRITE | MAY_EXEC);
628cb8e7090SChristoph Hellwig }
629cb8e7090SChristoph Hellwig 
630cb8e7090SChristoph Hellwig /*
631cb8e7090SChristoph Hellwig  * Create a new subvolume below @parent.  This is largely modeled after
632cb8e7090SChristoph Hellwig  * sys_mkdirat and vfs_mkdir, but we only do a single component lookup
633cb8e7090SChristoph Hellwig  * inside this filesystem so it's quite a bit simpler.
634cb8e7090SChristoph Hellwig  */
63576dda93cSYan, Zheng static noinline int btrfs_mksubvol(struct path *parent,
63676dda93cSYan, Zheng 				   char *name, int namelen,
63772fd032eSSage Weil 				   struct btrfs_root *snap_src,
638b83cc969SLi Zefan 				   u64 *async_transid, bool readonly)
639cb8e7090SChristoph Hellwig {
64076dda93cSYan, Zheng 	struct inode *dir  = parent->dentry->d_inode;
641cb8e7090SChristoph Hellwig 	struct dentry *dentry;
642cb8e7090SChristoph Hellwig 	int error;
643cb8e7090SChristoph Hellwig 
64476dda93cSYan, Zheng 	mutex_lock_nested(&dir->i_mutex, I_MUTEX_PARENT);
645cb8e7090SChristoph Hellwig 
646cb8e7090SChristoph Hellwig 	dentry = lookup_one_len(name, parent->dentry, namelen);
647cb8e7090SChristoph Hellwig 	error = PTR_ERR(dentry);
648cb8e7090SChristoph Hellwig 	if (IS_ERR(dentry))
649cb8e7090SChristoph Hellwig 		goto out_unlock;
650cb8e7090SChristoph Hellwig 
651cb8e7090SChristoph Hellwig 	error = -EEXIST;
652cb8e7090SChristoph Hellwig 	if (dentry->d_inode)
653cb8e7090SChristoph Hellwig 		goto out_dput;
654cb8e7090SChristoph Hellwig 
65576dda93cSYan, Zheng 	error = btrfs_may_create(dir, dentry);
656cb8e7090SChristoph Hellwig 	if (error)
657a874a63eSLiu Bo 		goto out_dput;
658cb8e7090SChristoph Hellwig 
65976dda93cSYan, Zheng 	down_read(&BTRFS_I(dir)->root->fs_info->subvol_sem);
66076dda93cSYan, Zheng 
66176dda93cSYan, Zheng 	if (btrfs_root_refs(&BTRFS_I(dir)->root->root_item) == 0)
66276dda93cSYan, Zheng 		goto out_up_read;
66376dda93cSYan, Zheng 
6643de4586cSChris Mason 	if (snap_src) {
66572fd032eSSage Weil 		error = create_snapshot(snap_src, dentry,
666b83cc969SLi Zefan 					name, namelen, async_transid, readonly);
6673de4586cSChris Mason 	} else {
66876dda93cSYan, Zheng 		error = create_subvol(BTRFS_I(dir)->root, dentry,
66972fd032eSSage Weil 				      name, namelen, async_transid);
6703de4586cSChris Mason 	}
67176dda93cSYan, Zheng 	if (!error)
67276dda93cSYan, Zheng 		fsnotify_mkdir(dir, dentry);
67376dda93cSYan, Zheng out_up_read:
67476dda93cSYan, Zheng 	up_read(&BTRFS_I(dir)->root->fs_info->subvol_sem);
675cb8e7090SChristoph Hellwig out_dput:
676cb8e7090SChristoph Hellwig 	dput(dentry);
677cb8e7090SChristoph Hellwig out_unlock:
67876dda93cSYan, Zheng 	mutex_unlock(&dir->i_mutex);
679cb8e7090SChristoph Hellwig 	return error;
680cb8e7090SChristoph Hellwig }
681cb8e7090SChristoph Hellwig 
6824cb5300bSChris Mason /*
6834cb5300bSChris Mason  * When we're defragging a range, we don't want to kick it off again
6844cb5300bSChris Mason  * if it is really just waiting for delalloc to send it down.
6854cb5300bSChris Mason  * If we find a nice big extent or delalloc range for the bytes in the
6864cb5300bSChris Mason  * file you want to defrag, we return 0 to let you know to skip this
6874cb5300bSChris Mason  * part of the file
6884cb5300bSChris Mason  */
6894cb5300bSChris Mason static int check_defrag_in_cache(struct inode *inode, u64 offset, int thresh)
6904cb5300bSChris Mason {
6914cb5300bSChris Mason 	struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
6924cb5300bSChris Mason 	struct extent_map *em = NULL;
6934cb5300bSChris Mason 	struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
6944cb5300bSChris Mason 	u64 end;
6954cb5300bSChris Mason 
6964cb5300bSChris Mason 	read_lock(&em_tree->lock);
6974cb5300bSChris Mason 	em = lookup_extent_mapping(em_tree, offset, PAGE_CACHE_SIZE);
6984cb5300bSChris Mason 	read_unlock(&em_tree->lock);
6994cb5300bSChris Mason 
7004cb5300bSChris Mason 	if (em) {
7014cb5300bSChris Mason 		end = extent_map_end(em);
7024cb5300bSChris Mason 		free_extent_map(em);
7034cb5300bSChris Mason 		if (end - offset > thresh)
7044cb5300bSChris Mason 			return 0;
7054cb5300bSChris Mason 	}
7064cb5300bSChris Mason 	/* if we already have a nice delalloc here, just stop */
7074cb5300bSChris Mason 	thresh /= 2;
7084cb5300bSChris Mason 	end = count_range_bits(io_tree, &offset, offset + thresh,
7094cb5300bSChris Mason 			       thresh, EXTENT_DELALLOC, 1);
7104cb5300bSChris Mason 	if (end >= thresh)
7114cb5300bSChris Mason 		return 0;
7124cb5300bSChris Mason 	return 1;
7134cb5300bSChris Mason }
7144cb5300bSChris Mason 
7154cb5300bSChris Mason /*
7164cb5300bSChris Mason  * helper function to walk through a file and find extents
7174cb5300bSChris Mason  * newer than a specific transid, and smaller than thresh.
7184cb5300bSChris Mason  *
7194cb5300bSChris Mason  * This is used by the defragging code to find new and small
7204cb5300bSChris Mason  * extents
7214cb5300bSChris Mason  */
7224cb5300bSChris Mason static int find_new_extents(struct btrfs_root *root,
7234cb5300bSChris Mason 			    struct inode *inode, u64 newer_than,
7244cb5300bSChris Mason 			    u64 *off, int thresh)
7254cb5300bSChris Mason {
7264cb5300bSChris Mason 	struct btrfs_path *path;
7274cb5300bSChris Mason 	struct btrfs_key min_key;
7284cb5300bSChris Mason 	struct btrfs_key max_key;
7294cb5300bSChris Mason 	struct extent_buffer *leaf;
7304cb5300bSChris Mason 	struct btrfs_file_extent_item *extent;
7314cb5300bSChris Mason 	int type;
7324cb5300bSChris Mason 	int ret;
733a4689d2bSDavid Sterba 	u64 ino = btrfs_ino(inode);
7344cb5300bSChris Mason 
7354cb5300bSChris Mason 	path = btrfs_alloc_path();
7364cb5300bSChris Mason 	if (!path)
7374cb5300bSChris Mason 		return -ENOMEM;
7384cb5300bSChris Mason 
739a4689d2bSDavid Sterba 	min_key.objectid = ino;
7404cb5300bSChris Mason 	min_key.type = BTRFS_EXTENT_DATA_KEY;
7414cb5300bSChris Mason 	min_key.offset = *off;
7424cb5300bSChris Mason 
743a4689d2bSDavid Sterba 	max_key.objectid = ino;
7444cb5300bSChris Mason 	max_key.type = (u8)-1;
7454cb5300bSChris Mason 	max_key.offset = (u64)-1;
7464cb5300bSChris Mason 
7474cb5300bSChris Mason 	path->keep_locks = 1;
7484cb5300bSChris Mason 
7494cb5300bSChris Mason 	while(1) {
7504cb5300bSChris Mason 		ret = btrfs_search_forward(root, &min_key, &max_key,
7514cb5300bSChris Mason 					   path, 0, newer_than);
7524cb5300bSChris Mason 		if (ret != 0)
7534cb5300bSChris Mason 			goto none;
754a4689d2bSDavid Sterba 		if (min_key.objectid != ino)
7554cb5300bSChris Mason 			goto none;
7564cb5300bSChris Mason 		if (min_key.type != BTRFS_EXTENT_DATA_KEY)
7574cb5300bSChris Mason 			goto none;
7584cb5300bSChris Mason 
7594cb5300bSChris Mason 		leaf = path->nodes[0];
7604cb5300bSChris Mason 		extent = btrfs_item_ptr(leaf, path->slots[0],
7614cb5300bSChris Mason 					struct btrfs_file_extent_item);
7624cb5300bSChris Mason 
7634cb5300bSChris Mason 		type = btrfs_file_extent_type(leaf, extent);
7644cb5300bSChris Mason 		if (type == BTRFS_FILE_EXTENT_REG &&
7654cb5300bSChris Mason 		    btrfs_file_extent_num_bytes(leaf, extent) < thresh &&
7664cb5300bSChris Mason 		    check_defrag_in_cache(inode, min_key.offset, thresh)) {
7674cb5300bSChris Mason 			*off = min_key.offset;
7684cb5300bSChris Mason 			btrfs_free_path(path);
7694cb5300bSChris Mason 			return 0;
7704cb5300bSChris Mason 		}
7714cb5300bSChris Mason 
7724cb5300bSChris Mason 		if (min_key.offset == (u64)-1)
7734cb5300bSChris Mason 			goto none;
7744cb5300bSChris Mason 
7754cb5300bSChris Mason 		min_key.offset++;
7764cb5300bSChris Mason 		btrfs_release_path(path);
7774cb5300bSChris Mason 	}
7784cb5300bSChris Mason none:
7794cb5300bSChris Mason 	btrfs_free_path(path);
7804cb5300bSChris Mason 	return -ENOENT;
7814cb5300bSChris Mason }
7824cb5300bSChris Mason 
7836c282eb4SLi Zefan static struct extent_map *defrag_lookup_extent(struct inode *inode, u64 start)
78417ce6ef8SLiu Bo {
78517ce6ef8SLiu Bo 	struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
786940100a4SChris Mason 	struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
7876c282eb4SLi Zefan 	struct extent_map *em;
7886c282eb4SLi Zefan 	u64 len = PAGE_CACHE_SIZE;
789940100a4SChris Mason 
790940100a4SChris Mason 	/*
791940100a4SChris Mason 	 * hopefully we have this extent in the tree already, try without
792940100a4SChris Mason 	 * the full extent lock
793940100a4SChris Mason 	 */
794940100a4SChris Mason 	read_lock(&em_tree->lock);
795940100a4SChris Mason 	em = lookup_extent_mapping(em_tree, start, len);
796940100a4SChris Mason 	read_unlock(&em_tree->lock);
797940100a4SChris Mason 
798940100a4SChris Mason 	if (!em) {
799940100a4SChris Mason 		/* get the big lock and read metadata off disk */
800d0082371SJeff Mahoney 		lock_extent(io_tree, start, start + len - 1);
801940100a4SChris Mason 		em = btrfs_get_extent(inode, NULL, 0, start, len, 0);
802d0082371SJeff Mahoney 		unlock_extent(io_tree, start, start + len - 1);
803940100a4SChris Mason 
8046cf8bfbfSDan Carpenter 		if (IS_ERR(em))
8056c282eb4SLi Zefan 			return NULL;
806940100a4SChris Mason 	}
807940100a4SChris Mason 
8086c282eb4SLi Zefan 	return em;
8096c282eb4SLi Zefan }
8106c282eb4SLi Zefan 
8116c282eb4SLi Zefan static bool defrag_check_next_extent(struct inode *inode, struct extent_map *em)
8126c282eb4SLi Zefan {
8136c282eb4SLi Zefan 	struct extent_map *next;
8146c282eb4SLi Zefan 	bool ret = true;
8156c282eb4SLi Zefan 
8166c282eb4SLi Zefan 	/* this is the last extent */
8176c282eb4SLi Zefan 	if (em->start + em->len >= i_size_read(inode))
8186c282eb4SLi Zefan 		return false;
8196c282eb4SLi Zefan 
8206c282eb4SLi Zefan 	next = defrag_lookup_extent(inode, em->start + em->len);
8216c282eb4SLi Zefan 	if (!next || next->block_start >= EXTENT_MAP_LAST_BYTE)
8226c282eb4SLi Zefan 		ret = false;
8236c282eb4SLi Zefan 
8246c282eb4SLi Zefan 	free_extent_map(next);
8256c282eb4SLi Zefan 	return ret;
8266c282eb4SLi Zefan }
8276c282eb4SLi Zefan 
8286c282eb4SLi Zefan static int should_defrag_range(struct inode *inode, u64 start, int thresh,
829a43a2111SAndrew Mahone 			       u64 *last_len, u64 *skip, u64 *defrag_end,
830a43a2111SAndrew Mahone 			       int compress)
8316c282eb4SLi Zefan {
8326c282eb4SLi Zefan 	struct extent_map *em;
8336c282eb4SLi Zefan 	int ret = 1;
8346c282eb4SLi Zefan 	bool next_mergeable = true;
8356c282eb4SLi Zefan 
8366c282eb4SLi Zefan 	/*
8376c282eb4SLi Zefan 	 * make sure that once we start defragging an extent, we keep on
8386c282eb4SLi Zefan 	 * defragging it
8396c282eb4SLi Zefan 	 */
8406c282eb4SLi Zefan 	if (start < *defrag_end)
8416c282eb4SLi Zefan 		return 1;
8426c282eb4SLi Zefan 
8436c282eb4SLi Zefan 	*skip = 0;
8446c282eb4SLi Zefan 
8456c282eb4SLi Zefan 	em = defrag_lookup_extent(inode, start);
8466c282eb4SLi Zefan 	if (!em)
8476c282eb4SLi Zefan 		return 0;
8486c282eb4SLi Zefan 
849940100a4SChris Mason 	/* this will cover holes, and inline extents */
85017ce6ef8SLiu Bo 	if (em->block_start >= EXTENT_MAP_LAST_BYTE) {
851940100a4SChris Mason 		ret = 0;
85217ce6ef8SLiu Bo 		goto out;
85317ce6ef8SLiu Bo 	}
85417ce6ef8SLiu Bo 
8556c282eb4SLi Zefan 	next_mergeable = defrag_check_next_extent(inode, em);
856940100a4SChris Mason 
857940100a4SChris Mason 	/*
8586c282eb4SLi Zefan 	 * we hit a real extent, if it is big or the next extent is not a
8596c282eb4SLi Zefan 	 * real extent, don't bother defragging it
860940100a4SChris Mason 	 */
861a43a2111SAndrew Mahone 	if (!compress && (*last_len == 0 || *last_len >= thresh) &&
8626c282eb4SLi Zefan 	    (em->len >= thresh || !next_mergeable))
863940100a4SChris Mason 		ret = 0;
86417ce6ef8SLiu Bo out:
865940100a4SChris Mason 	/*
866940100a4SChris Mason 	 * last_len ends up being a counter of how many bytes we've defragged.
867940100a4SChris Mason 	 * every time we choose not to defrag an extent, we reset *last_len
868940100a4SChris Mason 	 * so that the next tiny extent will force a defrag.
869940100a4SChris Mason 	 *
870940100a4SChris Mason 	 * The end result of this is that tiny extents before a single big
871940100a4SChris Mason 	 * extent will force at least part of that big extent to be defragged.
872940100a4SChris Mason 	 */
873940100a4SChris Mason 	if (ret) {
874940100a4SChris Mason 		*defrag_end = extent_map_end(em);
875940100a4SChris Mason 	} else {
876940100a4SChris Mason 		*last_len = 0;
877940100a4SChris Mason 		*skip = extent_map_end(em);
878940100a4SChris Mason 		*defrag_end = 0;
879940100a4SChris Mason 	}
880940100a4SChris Mason 
881940100a4SChris Mason 	free_extent_map(em);
882940100a4SChris Mason 	return ret;
883940100a4SChris Mason }
884940100a4SChris Mason 
8854cb5300bSChris Mason /*
8864cb5300bSChris Mason  * it doesn't do much good to defrag one or two pages
8874cb5300bSChris Mason  * at a time.  This pulls in a nice chunk of pages
8884cb5300bSChris Mason  * to COW and defrag.
8894cb5300bSChris Mason  *
8904cb5300bSChris Mason  * It also makes sure the delalloc code has enough
8914cb5300bSChris Mason  * dirty data to avoid making new small extents as part
8924cb5300bSChris Mason  * of the defrag
8934cb5300bSChris Mason  *
8944cb5300bSChris Mason  * It's a good idea to start RA on this range
8954cb5300bSChris Mason  * before calling this.
8964cb5300bSChris Mason  */
8974cb5300bSChris Mason static int cluster_pages_for_defrag(struct inode *inode,
8984cb5300bSChris Mason 				    struct page **pages,
8994cb5300bSChris Mason 				    unsigned long start_index,
9004cb5300bSChris Mason 				    int num_pages)
901f46b5a66SChristoph Hellwig {
9024cb5300bSChris Mason 	unsigned long file_end;
9034cb5300bSChris Mason 	u64 isize = i_size_read(inode);
904f46b5a66SChristoph Hellwig 	u64 page_start;
905f46b5a66SChristoph Hellwig 	u64 page_end;
9061f12bd06SLiu Bo 	u64 page_cnt;
9074cb5300bSChris Mason 	int ret;
9084cb5300bSChris Mason 	int i;
9094cb5300bSChris Mason 	int i_done;
9104cb5300bSChris Mason 	struct btrfs_ordered_extent *ordered;
9114cb5300bSChris Mason 	struct extent_state *cached_state = NULL;
912600a45e1SMiao Xie 	struct extent_io_tree *tree;
9133b16a4e3SJosef Bacik 	gfp_t mask = btrfs_alloc_write_mask(inode->i_mapping);
9144cb5300bSChris Mason 
9154cb5300bSChris Mason 	file_end = (isize - 1) >> PAGE_CACHE_SHIFT;
9161f12bd06SLiu Bo 	if (!isize || start_index > file_end)
9171f12bd06SLiu Bo 		return 0;
9181f12bd06SLiu Bo 
9191f12bd06SLiu Bo 	page_cnt = min_t(u64, (u64)num_pages, (u64)file_end - start_index + 1);
9204cb5300bSChris Mason 
9214cb5300bSChris Mason 	ret = btrfs_delalloc_reserve_space(inode,
9221f12bd06SLiu Bo 					   page_cnt << PAGE_CACHE_SHIFT);
9234cb5300bSChris Mason 	if (ret)
9244cb5300bSChris Mason 		return ret;
9254cb5300bSChris Mason 	i_done = 0;
926600a45e1SMiao Xie 	tree = &BTRFS_I(inode)->io_tree;
9274cb5300bSChris Mason 
9284cb5300bSChris Mason 	/* step one, lock all the pages */
9291f12bd06SLiu Bo 	for (i = 0; i < page_cnt; i++) {
9304cb5300bSChris Mason 		struct page *page;
931600a45e1SMiao Xie again:
932a94733d0SJosef Bacik 		page = find_or_create_page(inode->i_mapping,
9333b16a4e3SJosef Bacik 					   start_index + i, mask);
9344cb5300bSChris Mason 		if (!page)
9354cb5300bSChris Mason 			break;
9364cb5300bSChris Mason 
937600a45e1SMiao Xie 		page_start = page_offset(page);
938600a45e1SMiao Xie 		page_end = page_start + PAGE_CACHE_SIZE - 1;
939600a45e1SMiao Xie 		while (1) {
940d0082371SJeff Mahoney 			lock_extent(tree, page_start, page_end);
941600a45e1SMiao Xie 			ordered = btrfs_lookup_ordered_extent(inode,
942600a45e1SMiao Xie 							      page_start);
943d0082371SJeff Mahoney 			unlock_extent(tree, page_start, page_end);
944600a45e1SMiao Xie 			if (!ordered)
945600a45e1SMiao Xie 				break;
946600a45e1SMiao Xie 
947600a45e1SMiao Xie 			unlock_page(page);
948600a45e1SMiao Xie 			btrfs_start_ordered_extent(inode, ordered, 1);
949600a45e1SMiao Xie 			btrfs_put_ordered_extent(ordered);
950600a45e1SMiao Xie 			lock_page(page);
9511f12bd06SLiu Bo 			/*
9521f12bd06SLiu Bo 			 * we unlocked the page above, so we need check if
9531f12bd06SLiu Bo 			 * it was released or not.
9541f12bd06SLiu Bo 			 */
9551f12bd06SLiu Bo 			if (page->mapping != inode->i_mapping) {
9561f12bd06SLiu Bo 				unlock_page(page);
9571f12bd06SLiu Bo 				page_cache_release(page);
9581f12bd06SLiu Bo 				goto again;
9591f12bd06SLiu Bo 			}
960600a45e1SMiao Xie 		}
961600a45e1SMiao Xie 
9624cb5300bSChris Mason 		if (!PageUptodate(page)) {
9634cb5300bSChris Mason 			btrfs_readpage(NULL, page);
9644cb5300bSChris Mason 			lock_page(page);
9654cb5300bSChris Mason 			if (!PageUptodate(page)) {
9664cb5300bSChris Mason 				unlock_page(page);
9674cb5300bSChris Mason 				page_cache_release(page);
9684cb5300bSChris Mason 				ret = -EIO;
9694cb5300bSChris Mason 				break;
9704cb5300bSChris Mason 			}
9714cb5300bSChris Mason 		}
972600a45e1SMiao Xie 
973600a45e1SMiao Xie 		if (page->mapping != inode->i_mapping) {
974600a45e1SMiao Xie 			unlock_page(page);
975600a45e1SMiao Xie 			page_cache_release(page);
976600a45e1SMiao Xie 			goto again;
977600a45e1SMiao Xie 		}
978600a45e1SMiao Xie 
9794cb5300bSChris Mason 		pages[i] = page;
9804cb5300bSChris Mason 		i_done++;
9814cb5300bSChris Mason 	}
9824cb5300bSChris Mason 	if (!i_done || ret)
9834cb5300bSChris Mason 		goto out;
9844cb5300bSChris Mason 
9854cb5300bSChris Mason 	if (!(inode->i_sb->s_flags & MS_ACTIVE))
9864cb5300bSChris Mason 		goto out;
9874cb5300bSChris Mason 
9884cb5300bSChris Mason 	/*
9894cb5300bSChris Mason 	 * so now we have a nice long stream of locked
9904cb5300bSChris Mason 	 * and up to date pages, lets wait on them
9914cb5300bSChris Mason 	 */
9924cb5300bSChris Mason 	for (i = 0; i < i_done; i++)
9934cb5300bSChris Mason 		wait_on_page_writeback(pages[i]);
9944cb5300bSChris Mason 
9954cb5300bSChris Mason 	page_start = page_offset(pages[0]);
9964cb5300bSChris Mason 	page_end = page_offset(pages[i_done - 1]) + PAGE_CACHE_SIZE;
9974cb5300bSChris Mason 
9984cb5300bSChris Mason 	lock_extent_bits(&BTRFS_I(inode)->io_tree,
999d0082371SJeff Mahoney 			 page_start, page_end - 1, 0, &cached_state);
10004cb5300bSChris Mason 	clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start,
10014cb5300bSChris Mason 			  page_end - 1, EXTENT_DIRTY | EXTENT_DELALLOC |
10024cb5300bSChris Mason 			  EXTENT_DO_ACCOUNTING, 0, 0, &cached_state,
10034cb5300bSChris Mason 			  GFP_NOFS);
10044cb5300bSChris Mason 
10051f12bd06SLiu Bo 	if (i_done != page_cnt) {
10069e0baf60SJosef Bacik 		spin_lock(&BTRFS_I(inode)->lock);
10079e0baf60SJosef Bacik 		BTRFS_I(inode)->outstanding_extents++;
10089e0baf60SJosef Bacik 		spin_unlock(&BTRFS_I(inode)->lock);
10094cb5300bSChris Mason 		btrfs_delalloc_release_space(inode,
10101f12bd06SLiu Bo 				     (page_cnt - i_done) << PAGE_CACHE_SHIFT);
10114cb5300bSChris Mason 	}
10124cb5300bSChris Mason 
10134cb5300bSChris Mason 
10144cb5300bSChris Mason 	btrfs_set_extent_delalloc(inode, page_start, page_end - 1,
10154cb5300bSChris Mason 				  &cached_state);
10164cb5300bSChris Mason 
10174cb5300bSChris Mason 	unlock_extent_cached(&BTRFS_I(inode)->io_tree,
10184cb5300bSChris Mason 			     page_start, page_end - 1, &cached_state,
10194cb5300bSChris Mason 			     GFP_NOFS);
10204cb5300bSChris Mason 
10214cb5300bSChris Mason 	for (i = 0; i < i_done; i++) {
10224cb5300bSChris Mason 		clear_page_dirty_for_io(pages[i]);
10234cb5300bSChris Mason 		ClearPageChecked(pages[i]);
10244cb5300bSChris Mason 		set_page_extent_mapped(pages[i]);
10254cb5300bSChris Mason 		set_page_dirty(pages[i]);
10264cb5300bSChris Mason 		unlock_page(pages[i]);
10274cb5300bSChris Mason 		page_cache_release(pages[i]);
10284cb5300bSChris Mason 	}
10294cb5300bSChris Mason 	return i_done;
10304cb5300bSChris Mason out:
10314cb5300bSChris Mason 	for (i = 0; i < i_done; i++) {
10324cb5300bSChris Mason 		unlock_page(pages[i]);
10334cb5300bSChris Mason 		page_cache_release(pages[i]);
10344cb5300bSChris Mason 	}
10351f12bd06SLiu Bo 	btrfs_delalloc_release_space(inode, page_cnt << PAGE_CACHE_SHIFT);
10364cb5300bSChris Mason 	return ret;
10374cb5300bSChris Mason 
10384cb5300bSChris Mason }
10394cb5300bSChris Mason 
10404cb5300bSChris Mason int btrfs_defrag_file(struct inode *inode, struct file *file,
10414cb5300bSChris Mason 		      struct btrfs_ioctl_defrag_range_args *range,
10424cb5300bSChris Mason 		      u64 newer_than, unsigned long max_to_defrag)
10434cb5300bSChris Mason {
10444cb5300bSChris Mason 	struct btrfs_root *root = BTRFS_I(inode)->root;
10454cb5300bSChris Mason 	struct btrfs_super_block *disk_super;
10464cb5300bSChris Mason 	struct file_ra_state *ra = NULL;
10474cb5300bSChris Mason 	unsigned long last_index;
1048151a31b2SLi Zefan 	u64 isize = i_size_read(inode);
10494cb5300bSChris Mason 	u64 features;
1050940100a4SChris Mason 	u64 last_len = 0;
1051940100a4SChris Mason 	u64 skip = 0;
1052940100a4SChris Mason 	u64 defrag_end = 0;
10534cb5300bSChris Mason 	u64 newer_off = range->start;
1054f46b5a66SChristoph Hellwig 	unsigned long i;
1055008873eaSLi Zefan 	unsigned long ra_index = 0;
1056f46b5a66SChristoph Hellwig 	int ret;
10574cb5300bSChris Mason 	int defrag_count = 0;
10581a419d85SLi Zefan 	int compress_type = BTRFS_COMPRESS_ZLIB;
10594cb5300bSChris Mason 	int extent_thresh = range->extent_thresh;
1060008873eaSLi Zefan 	int max_cluster = (256 * 1024) >> PAGE_CACHE_SHIFT;
1061008873eaSLi Zefan 	int cluster = max_cluster;
10624cb5300bSChris Mason 	u64 new_align = ~((u64)128 * 1024 - 1);
10634cb5300bSChris Mason 	struct page **pages = NULL;
10644cb5300bSChris Mason 
10654cb5300bSChris Mason 	if (extent_thresh == 0)
10664cb5300bSChris Mason 		extent_thresh = 256 * 1024;
10671a419d85SLi Zefan 
10681a419d85SLi Zefan 	if (range->flags & BTRFS_DEFRAG_RANGE_COMPRESS) {
10691a419d85SLi Zefan 		if (range->compress_type > BTRFS_COMPRESS_TYPES)
10701a419d85SLi Zefan 			return -EINVAL;
10711a419d85SLi Zefan 		if (range->compress_type)
10721a419d85SLi Zefan 			compress_type = range->compress_type;
10731a419d85SLi Zefan 	}
1074f46b5a66SChristoph Hellwig 
1075151a31b2SLi Zefan 	if (isize == 0)
1076940100a4SChris Mason 		return 0;
1077f46b5a66SChristoph Hellwig 
10784cb5300bSChris Mason 	/*
10794cb5300bSChris Mason 	 * if we were not given a file, allocate a readahead
10804cb5300bSChris Mason 	 * context
10814cb5300bSChris Mason 	 */
10824cb5300bSChris Mason 	if (!file) {
10834cb5300bSChris Mason 		ra = kzalloc(sizeof(*ra), GFP_NOFS);
10844cb5300bSChris Mason 		if (!ra)
10854cb5300bSChris Mason 			return -ENOMEM;
10864cb5300bSChris Mason 		file_ra_state_init(ra, inode->i_mapping);
10874cb5300bSChris Mason 	} else {
10884cb5300bSChris Mason 		ra = &file->f_ra;
10894cb5300bSChris Mason 	}
10904cb5300bSChris Mason 
1091008873eaSLi Zefan 	pages = kmalloc(sizeof(struct page *) * max_cluster,
10924cb5300bSChris Mason 			GFP_NOFS);
10934cb5300bSChris Mason 	if (!pages) {
10944cb5300bSChris Mason 		ret = -ENOMEM;
10954cb5300bSChris Mason 		goto out_ra;
10964cb5300bSChris Mason 	}
10974cb5300bSChris Mason 
10984cb5300bSChris Mason 	/* find the last page to defrag */
10991e701a32SChris Mason 	if (range->start + range->len > range->start) {
1100151a31b2SLi Zefan 		last_index = min_t(u64, isize - 1,
11011e701a32SChris Mason 			 range->start + range->len - 1) >> PAGE_CACHE_SHIFT;
11021e701a32SChris Mason 	} else {
1103151a31b2SLi Zefan 		last_index = (isize - 1) >> PAGE_CACHE_SHIFT;
11041e701a32SChris Mason 	}
11051e701a32SChris Mason 
11064cb5300bSChris Mason 	if (newer_than) {
11074cb5300bSChris Mason 		ret = find_new_extents(root, inode, newer_than,
11084cb5300bSChris Mason 				       &newer_off, 64 * 1024);
11094cb5300bSChris Mason 		if (!ret) {
11104cb5300bSChris Mason 			range->start = newer_off;
11114cb5300bSChris Mason 			/*
11124cb5300bSChris Mason 			 * we always align our defrag to help keep
11134cb5300bSChris Mason 			 * the extents in the file evenly spaced
11144cb5300bSChris Mason 			 */
11154cb5300bSChris Mason 			i = (newer_off & new_align) >> PAGE_CACHE_SHIFT;
11164cb5300bSChris Mason 		} else
11174cb5300bSChris Mason 			goto out_ra;
11184cb5300bSChris Mason 	} else {
11191e701a32SChris Mason 		i = range->start >> PAGE_CACHE_SHIFT;
11204cb5300bSChris Mason 	}
11214cb5300bSChris Mason 	if (!max_to_defrag)
11227ec31b54SLiu Bo 		max_to_defrag = last_index + 1;
11234cb5300bSChris Mason 
11242a0f7f57SLi Zefan 	/*
11252a0f7f57SLi Zefan 	 * make writeback starts from i, so the defrag range can be
11262a0f7f57SLi Zefan 	 * written sequentially.
11272a0f7f57SLi Zefan 	 */
11282a0f7f57SLi Zefan 	if (i < inode->i_mapping->writeback_index)
11292a0f7f57SLi Zefan 		inode->i_mapping->writeback_index = i;
11302a0f7f57SLi Zefan 
1131f7f43cc8SChris Mason 	while (i <= last_index && defrag_count < max_to_defrag &&
1132f7f43cc8SChris Mason 	       (i < (i_size_read(inode) + PAGE_CACHE_SIZE - 1) >>
1133f7f43cc8SChris Mason 		PAGE_CACHE_SHIFT)) {
11344cb5300bSChris Mason 		/*
11354cb5300bSChris Mason 		 * make sure we stop running if someone unmounts
11364cb5300bSChris Mason 		 * the FS
11374cb5300bSChris Mason 		 */
11384cb5300bSChris Mason 		if (!(inode->i_sb->s_flags & MS_ACTIVE))
11394cb5300bSChris Mason 			break;
11404cb5300bSChris Mason 
114166c26892SLiu Bo 		if (!should_defrag_range(inode, (u64)i << PAGE_CACHE_SHIFT,
11426c282eb4SLi Zefan 					 extent_thresh, &last_len, &skip,
1143a43a2111SAndrew Mahone 					 &defrag_end, range->flags &
1144a43a2111SAndrew Mahone 					 BTRFS_DEFRAG_RANGE_COMPRESS)) {
1145940100a4SChris Mason 			unsigned long next;
1146940100a4SChris Mason 			/*
1147940100a4SChris Mason 			 * the should_defrag function tells us how much to skip
1148940100a4SChris Mason 			 * bump our counter by the suggested amount
1149940100a4SChris Mason 			 */
1150940100a4SChris Mason 			next = (skip + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
1151940100a4SChris Mason 			i = max(i + 1, next);
1152940100a4SChris Mason 			continue;
1153940100a4SChris Mason 		}
1154008873eaSLi Zefan 
1155008873eaSLi Zefan 		if (!newer_than) {
1156008873eaSLi Zefan 			cluster = (PAGE_CACHE_ALIGN(defrag_end) >>
1157008873eaSLi Zefan 				   PAGE_CACHE_SHIFT) - i;
1158008873eaSLi Zefan 			cluster = min(cluster, max_cluster);
1159008873eaSLi Zefan 		} else {
1160008873eaSLi Zefan 			cluster = max_cluster;
1161008873eaSLi Zefan 		}
1162008873eaSLi Zefan 
11631e701a32SChris Mason 		if (range->flags & BTRFS_DEFRAG_RANGE_COMPRESS)
11641a419d85SLi Zefan 			BTRFS_I(inode)->force_compress = compress_type;
1165940100a4SChris Mason 
1166008873eaSLi Zefan 		if (i + cluster > ra_index) {
1167008873eaSLi Zefan 			ra_index = max(i, ra_index);
1168008873eaSLi Zefan 			btrfs_force_ra(inode->i_mapping, ra, file, ra_index,
1169008873eaSLi Zefan 				       cluster);
1170008873eaSLi Zefan 			ra_index += max_cluster;
1171008873eaSLi Zefan 		}
11724cb5300bSChris Mason 
1173ecb8bea8SLiu Bo 		mutex_lock(&inode->i_mutex);
1174008873eaSLi Zefan 		ret = cluster_pages_for_defrag(inode, pages, i, cluster);
1175ecb8bea8SLiu Bo 		if (ret < 0) {
1176ecb8bea8SLiu Bo 			mutex_unlock(&inode->i_mutex);
11774cb5300bSChris Mason 			goto out_ra;
1178ecb8bea8SLiu Bo 		}
11794cb5300bSChris Mason 
11804cb5300bSChris Mason 		defrag_count += ret;
11814cb5300bSChris Mason 		balance_dirty_pages_ratelimited_nr(inode->i_mapping, ret);
1182ecb8bea8SLiu Bo 		mutex_unlock(&inode->i_mutex);
11834cb5300bSChris Mason 
11844cb5300bSChris Mason 		if (newer_than) {
11854cb5300bSChris Mason 			if (newer_off == (u64)-1)
11864cb5300bSChris Mason 				break;
11874cb5300bSChris Mason 
1188e1f041e1SLiu Bo 			if (ret > 0)
1189e1f041e1SLiu Bo 				i += ret;
1190e1f041e1SLiu Bo 
11914cb5300bSChris Mason 			newer_off = max(newer_off + 1,
11924cb5300bSChris Mason 					(u64)i << PAGE_CACHE_SHIFT);
11934cb5300bSChris Mason 
11944cb5300bSChris Mason 			ret = find_new_extents(root, inode,
11954cb5300bSChris Mason 					       newer_than, &newer_off,
11964cb5300bSChris Mason 					       64 * 1024);
11974cb5300bSChris Mason 			if (!ret) {
11984cb5300bSChris Mason 				range->start = newer_off;
11994cb5300bSChris Mason 				i = (newer_off & new_align) >> PAGE_CACHE_SHIFT;
12004cb5300bSChris Mason 			} else {
12014cb5300bSChris Mason 				break;
1202940100a4SChris Mason 			}
12034cb5300bSChris Mason 		} else {
1204008873eaSLi Zefan 			if (ret > 0) {
1205cbcc8326SLi Zefan 				i += ret;
1206008873eaSLi Zefan 				last_len += ret << PAGE_CACHE_SHIFT;
1207008873eaSLi Zefan 			} else {
1208940100a4SChris Mason 				i++;
1209008873eaSLi Zefan 				last_len = 0;
1210008873eaSLi Zefan 			}
1211f46b5a66SChristoph Hellwig 		}
12124cb5300bSChris Mason 	}
1213f46b5a66SChristoph Hellwig 
12141e701a32SChris Mason 	if ((range->flags & BTRFS_DEFRAG_RANGE_START_IO))
12151e701a32SChris Mason 		filemap_flush(inode->i_mapping);
12161e701a32SChris Mason 
12171e701a32SChris Mason 	if ((range->flags & BTRFS_DEFRAG_RANGE_COMPRESS)) {
12181e701a32SChris Mason 		/* the filemap_flush will queue IO into the worker threads, but
12191e701a32SChris Mason 		 * we have to make sure the IO is actually started and that
12201e701a32SChris Mason 		 * ordered extents get created before we return
12211e701a32SChris Mason 		 */
12221e701a32SChris Mason 		atomic_inc(&root->fs_info->async_submit_draining);
12231e701a32SChris Mason 		while (atomic_read(&root->fs_info->nr_async_submits) ||
12241e701a32SChris Mason 		      atomic_read(&root->fs_info->async_delalloc_pages)) {
12251e701a32SChris Mason 			wait_event(root->fs_info->async_submit_wait,
12261e701a32SChris Mason 			   (atomic_read(&root->fs_info->nr_async_submits) == 0 &&
12271e701a32SChris Mason 			    atomic_read(&root->fs_info->async_delalloc_pages) == 0));
12281e701a32SChris Mason 		}
12291e701a32SChris Mason 		atomic_dec(&root->fs_info->async_submit_draining);
12301e701a32SChris Mason 
12311e701a32SChris Mason 		mutex_lock(&inode->i_mutex);
1232261507a0SLi Zefan 		BTRFS_I(inode)->force_compress = BTRFS_COMPRESS_NONE;
12331e701a32SChris Mason 		mutex_unlock(&inode->i_mutex);
12341e701a32SChris Mason 	}
12351e701a32SChris Mason 
12366c41761fSDavid Sterba 	disk_super = root->fs_info->super_copy;
12371a419d85SLi Zefan 	features = btrfs_super_incompat_flags(disk_super);
12381a419d85SLi Zefan 	if (range->compress_type == BTRFS_COMPRESS_LZO) {
12391a419d85SLi Zefan 		features |= BTRFS_FEATURE_INCOMPAT_COMPRESS_LZO;
12401a419d85SLi Zefan 		btrfs_set_super_incompat_flags(disk_super, features);
12411a419d85SLi Zefan 	}
12421a419d85SLi Zefan 
124360ccf82fSDiego Calleja 	ret = defrag_count;
1244940100a4SChris Mason 
12454cb5300bSChris Mason out_ra:
12464cb5300bSChris Mason 	if (!file)
12474cb5300bSChris Mason 		kfree(ra);
12484cb5300bSChris Mason 	kfree(pages);
1249940100a4SChris Mason 	return ret;
1250f46b5a66SChristoph Hellwig }
1251f46b5a66SChristoph Hellwig 
125276dda93cSYan, Zheng static noinline int btrfs_ioctl_resize(struct btrfs_root *root,
125376dda93cSYan, Zheng 					void __user *arg)
1254f46b5a66SChristoph Hellwig {
1255f46b5a66SChristoph Hellwig 	u64 new_size;
1256f46b5a66SChristoph Hellwig 	u64 old_size;
1257f46b5a66SChristoph Hellwig 	u64 devid = 1;
1258f46b5a66SChristoph Hellwig 	struct btrfs_ioctl_vol_args *vol_args;
1259f46b5a66SChristoph Hellwig 	struct btrfs_trans_handle *trans;
1260f46b5a66SChristoph Hellwig 	struct btrfs_device *device = NULL;
1261f46b5a66SChristoph Hellwig 	char *sizestr;
1262f46b5a66SChristoph Hellwig 	char *devstr = NULL;
1263f46b5a66SChristoph Hellwig 	int ret = 0;
1264f46b5a66SChristoph Hellwig 	int mod = 0;
1265f46b5a66SChristoph Hellwig 
1266c146afadSYan Zheng 	if (root->fs_info->sb->s_flags & MS_RDONLY)
1267c146afadSYan Zheng 		return -EROFS;
1268c146afadSYan Zheng 
1269e441d54dSChris Mason 	if (!capable(CAP_SYS_ADMIN))
1270e441d54dSChris Mason 		return -EPERM;
1271e441d54dSChris Mason 
1272c9e9f97bSIlya Dryomov 	mutex_lock(&root->fs_info->volume_mutex);
1273c9e9f97bSIlya Dryomov 	if (root->fs_info->balance_ctl) {
1274c9e9f97bSIlya Dryomov 		printk(KERN_INFO "btrfs: balance in progress\n");
1275c9e9f97bSIlya Dryomov 		ret = -EINVAL;
1276c9e9f97bSIlya Dryomov 		goto out;
1277c9e9f97bSIlya Dryomov 	}
1278c9e9f97bSIlya Dryomov 
1279dae7b665SLi Zefan 	vol_args = memdup_user(arg, sizeof(*vol_args));
1280c9e9f97bSIlya Dryomov 	if (IS_ERR(vol_args)) {
1281c9e9f97bSIlya Dryomov 		ret = PTR_ERR(vol_args);
1282c9e9f97bSIlya Dryomov 		goto out;
1283c9e9f97bSIlya Dryomov 	}
12845516e595SMark Fasheh 
12855516e595SMark Fasheh 	vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
1286f46b5a66SChristoph Hellwig 
1287f46b5a66SChristoph Hellwig 	sizestr = vol_args->name;
1288f46b5a66SChristoph Hellwig 	devstr = strchr(sizestr, ':');
1289f46b5a66SChristoph Hellwig 	if (devstr) {
1290f46b5a66SChristoph Hellwig 		char *end;
1291f46b5a66SChristoph Hellwig 		sizestr = devstr + 1;
1292f46b5a66SChristoph Hellwig 		*devstr = '\0';
1293f46b5a66SChristoph Hellwig 		devstr = vol_args->name;
1294f46b5a66SChristoph Hellwig 		devid = simple_strtoull(devstr, &end, 10);
12955bb14682SArnd Hannemann 		printk(KERN_INFO "btrfs: resizing devid %llu\n",
129621380931SJoel Becker 		       (unsigned long long)devid);
1297f46b5a66SChristoph Hellwig 	}
12982b82032cSYan Zheng 	device = btrfs_find_device(root, devid, NULL, NULL);
1299f46b5a66SChristoph Hellwig 	if (!device) {
13005bb14682SArnd Hannemann 		printk(KERN_INFO "btrfs: resizer unable to find device %llu\n",
130121380931SJoel Becker 		       (unsigned long long)devid);
1302f46b5a66SChristoph Hellwig 		ret = -EINVAL;
1303c9e9f97bSIlya Dryomov 		goto out_free;
1304f46b5a66SChristoph Hellwig 	}
13054e42ae1bSLiu Bo 	if (device->fs_devices && device->fs_devices->seeding) {
13064e42ae1bSLiu Bo 		printk(KERN_INFO "btrfs: resizer unable to apply on "
1307a8c4a33bSChris Mason 		       "seeding device %llu\n",
1308a8c4a33bSChris Mason 		       (unsigned long long)devid);
13094e42ae1bSLiu Bo 		ret = -EINVAL;
13104e42ae1bSLiu Bo 		goto out_free;
13114e42ae1bSLiu Bo 	}
13124e42ae1bSLiu Bo 
1313f46b5a66SChristoph Hellwig 	if (!strcmp(sizestr, "max"))
1314f46b5a66SChristoph Hellwig 		new_size = device->bdev->bd_inode->i_size;
1315f46b5a66SChristoph Hellwig 	else {
1316f46b5a66SChristoph Hellwig 		if (sizestr[0] == '-') {
1317f46b5a66SChristoph Hellwig 			mod = -1;
1318f46b5a66SChristoph Hellwig 			sizestr++;
1319f46b5a66SChristoph Hellwig 		} else if (sizestr[0] == '+') {
1320f46b5a66SChristoph Hellwig 			mod = 1;
1321f46b5a66SChristoph Hellwig 			sizestr++;
1322f46b5a66SChristoph Hellwig 		}
132391748467SAkinobu Mita 		new_size = memparse(sizestr, NULL);
1324f46b5a66SChristoph Hellwig 		if (new_size == 0) {
1325f46b5a66SChristoph Hellwig 			ret = -EINVAL;
1326c9e9f97bSIlya Dryomov 			goto out_free;
1327f46b5a66SChristoph Hellwig 		}
1328f46b5a66SChristoph Hellwig 	}
1329f46b5a66SChristoph Hellwig 
1330f46b5a66SChristoph Hellwig 	old_size = device->total_bytes;
1331f46b5a66SChristoph Hellwig 
1332f46b5a66SChristoph Hellwig 	if (mod < 0) {
1333f46b5a66SChristoph Hellwig 		if (new_size > old_size) {
1334f46b5a66SChristoph Hellwig 			ret = -EINVAL;
1335c9e9f97bSIlya Dryomov 			goto out_free;
1336f46b5a66SChristoph Hellwig 		}
1337f46b5a66SChristoph Hellwig 		new_size = old_size - new_size;
1338f46b5a66SChristoph Hellwig 	} else if (mod > 0) {
1339f46b5a66SChristoph Hellwig 		new_size = old_size + new_size;
1340f46b5a66SChristoph Hellwig 	}
1341f46b5a66SChristoph Hellwig 
1342f46b5a66SChristoph Hellwig 	if (new_size < 256 * 1024 * 1024) {
1343f46b5a66SChristoph Hellwig 		ret = -EINVAL;
1344c9e9f97bSIlya Dryomov 		goto out_free;
1345f46b5a66SChristoph Hellwig 	}
1346f46b5a66SChristoph Hellwig 	if (new_size > device->bdev->bd_inode->i_size) {
1347f46b5a66SChristoph Hellwig 		ret = -EFBIG;
1348c9e9f97bSIlya Dryomov 		goto out_free;
1349f46b5a66SChristoph Hellwig 	}
1350f46b5a66SChristoph Hellwig 
1351f46b5a66SChristoph Hellwig 	do_div(new_size, root->sectorsize);
1352f46b5a66SChristoph Hellwig 	new_size *= root->sectorsize;
1353f46b5a66SChristoph Hellwig 
1354606686eeSJosef Bacik 	printk_in_rcu(KERN_INFO "btrfs: new size for %s is %llu\n",
1355606686eeSJosef Bacik 		      rcu_str_deref(device->name),
1356606686eeSJosef Bacik 		      (unsigned long long)new_size);
1357f46b5a66SChristoph Hellwig 
1358f46b5a66SChristoph Hellwig 	if (new_size > old_size) {
1359a22285a6SYan, Zheng 		trans = btrfs_start_transaction(root, 0);
136098d5dc13STsutomu Itoh 		if (IS_ERR(trans)) {
136198d5dc13STsutomu Itoh 			ret = PTR_ERR(trans);
1362c9e9f97bSIlya Dryomov 			goto out_free;
136398d5dc13STsutomu Itoh 		}
1364f46b5a66SChristoph Hellwig 		ret = btrfs_grow_device(trans, device, new_size);
1365f46b5a66SChristoph Hellwig 		btrfs_commit_transaction(trans, root);
1366ece7d20eSMike Fleetwood 	} else if (new_size < old_size) {
1367f46b5a66SChristoph Hellwig 		ret = btrfs_shrink_device(device, new_size);
1368f46b5a66SChristoph Hellwig 	}
1369f46b5a66SChristoph Hellwig 
1370c9e9f97bSIlya Dryomov out_free:
1371f46b5a66SChristoph Hellwig 	kfree(vol_args);
1372c9e9f97bSIlya Dryomov out:
1373c9e9f97bSIlya Dryomov 	mutex_unlock(&root->fs_info->volume_mutex);
1374f46b5a66SChristoph Hellwig 	return ret;
1375f46b5a66SChristoph Hellwig }
1376f46b5a66SChristoph Hellwig 
137772fd032eSSage Weil static noinline int btrfs_ioctl_snap_create_transid(struct file *file,
137872fd032eSSage Weil 						    char *name,
137972fd032eSSage Weil 						    unsigned long fd,
138072fd032eSSage Weil 						    int subvol,
1381b83cc969SLi Zefan 						    u64 *transid,
1382b83cc969SLi Zefan 						    bool readonly)
1383f46b5a66SChristoph Hellwig {
13843de4586cSChris Mason 	struct file *src_file;
1385f46b5a66SChristoph Hellwig 	int namelen;
13863de4586cSChris Mason 	int ret = 0;
1387f46b5a66SChristoph Hellwig 
1388a874a63eSLiu Bo 	ret = mnt_want_write_file(file);
1389a874a63eSLiu Bo 	if (ret)
1390a874a63eSLiu Bo 		goto out;
1391a874a63eSLiu Bo 
139272fd032eSSage Weil 	namelen = strlen(name);
139372fd032eSSage Weil 	if (strchr(name, '/')) {
1394f46b5a66SChristoph Hellwig 		ret = -EINVAL;
1395a874a63eSLiu Bo 		goto out_drop_write;
1396f46b5a66SChristoph Hellwig 	}
1397f46b5a66SChristoph Hellwig 
139816780cabSChris Mason 	if (name[0] == '.' &&
139916780cabSChris Mason 	   (namelen == 1 || (name[1] == '.' && namelen == 2))) {
140016780cabSChris Mason 		ret = -EEXIST;
1401a874a63eSLiu Bo 		goto out_drop_write;
140216780cabSChris Mason 	}
140316780cabSChris Mason 
14043de4586cSChris Mason 	if (subvol) {
140572fd032eSSage Weil 		ret = btrfs_mksubvol(&file->f_path, name, namelen,
1406b83cc969SLi Zefan 				     NULL, transid, readonly);
1407cb8e7090SChristoph Hellwig 	} else {
14083de4586cSChris Mason 		struct inode *src_inode;
140972fd032eSSage Weil 		src_file = fget(fd);
14103de4586cSChris Mason 		if (!src_file) {
14113de4586cSChris Mason 			ret = -EINVAL;
1412a874a63eSLiu Bo 			goto out_drop_write;
14133de4586cSChris Mason 		}
14143de4586cSChris Mason 
14153de4586cSChris Mason 		src_inode = src_file->f_path.dentry->d_inode;
14163de4586cSChris Mason 		if (src_inode->i_sb != file->f_path.dentry->d_inode->i_sb) {
1417d397712bSChris Mason 			printk(KERN_INFO "btrfs: Snapshot src from "
1418d397712bSChris Mason 			       "another FS\n");
14193de4586cSChris Mason 			ret = -EINVAL;
14203de4586cSChris Mason 			fput(src_file);
1421a874a63eSLiu Bo 			goto out_drop_write;
14223de4586cSChris Mason 		}
142372fd032eSSage Weil 		ret = btrfs_mksubvol(&file->f_path, name, namelen,
142472fd032eSSage Weil 				     BTRFS_I(src_inode)->root,
1425b83cc969SLi Zefan 				     transid, readonly);
14263de4586cSChris Mason 		fput(src_file);
1427cb8e7090SChristoph Hellwig 	}
1428a874a63eSLiu Bo out_drop_write:
1429a874a63eSLiu Bo 	mnt_drop_write_file(file);
1430f46b5a66SChristoph Hellwig out:
143172fd032eSSage Weil 	return ret;
143272fd032eSSage Weil }
143372fd032eSSage Weil 
143472fd032eSSage Weil static noinline int btrfs_ioctl_snap_create(struct file *file,
1435fa0d2b9bSLi Zefan 					    void __user *arg, int subvol)
143672fd032eSSage Weil {
1437fa0d2b9bSLi Zefan 	struct btrfs_ioctl_vol_args *vol_args;
143872fd032eSSage Weil 	int ret;
143972fd032eSSage Weil 
1440fa0d2b9bSLi Zefan 	vol_args = memdup_user(arg, sizeof(*vol_args));
1441fa0d2b9bSLi Zefan 	if (IS_ERR(vol_args))
1442fa0d2b9bSLi Zefan 		return PTR_ERR(vol_args);
1443fa0d2b9bSLi Zefan 	vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
1444fa0d2b9bSLi Zefan 
1445fa0d2b9bSLi Zefan 	ret = btrfs_ioctl_snap_create_transid(file, vol_args->name,
1446b83cc969SLi Zefan 					      vol_args->fd, subvol,
1447b83cc969SLi Zefan 					      NULL, false);
1448fa0d2b9bSLi Zefan 
1449fa0d2b9bSLi Zefan 	kfree(vol_args);
1450fa0d2b9bSLi Zefan 	return ret;
1451fa0d2b9bSLi Zefan }
1452fa0d2b9bSLi Zefan 
1453fa0d2b9bSLi Zefan static noinline int btrfs_ioctl_snap_create_v2(struct file *file,
1454fa0d2b9bSLi Zefan 					       void __user *arg, int subvol)
1455fa0d2b9bSLi Zefan {
1456fa0d2b9bSLi Zefan 	struct btrfs_ioctl_vol_args_v2 *vol_args;
1457fa0d2b9bSLi Zefan 	int ret;
1458fdfb1e4fSLi Zefan 	u64 transid = 0;
1459fdfb1e4fSLi Zefan 	u64 *ptr = NULL;
1460b83cc969SLi Zefan 	bool readonly = false;
146172fd032eSSage Weil 
1462fa0d2b9bSLi Zefan 	vol_args = memdup_user(arg, sizeof(*vol_args));
1463fa0d2b9bSLi Zefan 	if (IS_ERR(vol_args))
1464fa0d2b9bSLi Zefan 		return PTR_ERR(vol_args);
1465fa0d2b9bSLi Zefan 	vol_args->name[BTRFS_SUBVOL_NAME_MAX] = '\0';
1466fdfb1e4fSLi Zefan 
1467b83cc969SLi Zefan 	if (vol_args->flags &
1468b83cc969SLi Zefan 	    ~(BTRFS_SUBVOL_CREATE_ASYNC | BTRFS_SUBVOL_RDONLY)) {
1469b83cc969SLi Zefan 		ret = -EOPNOTSUPP;
1470fdfb1e4fSLi Zefan 		goto out;
1471fdfb1e4fSLi Zefan 	}
1472fdfb1e4fSLi Zefan 
1473fa0d2b9bSLi Zefan 	if (vol_args->flags & BTRFS_SUBVOL_CREATE_ASYNC)
1474fdfb1e4fSLi Zefan 		ptr = &transid;
1475b83cc969SLi Zefan 	if (vol_args->flags & BTRFS_SUBVOL_RDONLY)
1476b83cc969SLi Zefan 		readonly = true;
147775eaa0e2SSage Weil 
1478fa0d2b9bSLi Zefan 	ret = btrfs_ioctl_snap_create_transid(file, vol_args->name,
1479b83cc969SLi Zefan 					      vol_args->fd, subvol,
1480b83cc969SLi Zefan 					      ptr, readonly);
148175eaa0e2SSage Weil 
1482fdfb1e4fSLi Zefan 	if (ret == 0 && ptr &&
148375eaa0e2SSage Weil 	    copy_to_user(arg +
1484fdfb1e4fSLi Zefan 			 offsetof(struct btrfs_ioctl_vol_args_v2,
1485fdfb1e4fSLi Zefan 				  transid), ptr, sizeof(*ptr)))
148675eaa0e2SSage Weil 		ret = -EFAULT;
1487fdfb1e4fSLi Zefan out:
1488f46b5a66SChristoph Hellwig 	kfree(vol_args);
1489f46b5a66SChristoph Hellwig 	return ret;
1490f46b5a66SChristoph Hellwig }
1491f46b5a66SChristoph Hellwig 
14920caa102dSLi Zefan static noinline int btrfs_ioctl_subvol_getflags(struct file *file,
14930caa102dSLi Zefan 						void __user *arg)
14940caa102dSLi Zefan {
14950caa102dSLi Zefan 	struct inode *inode = fdentry(file)->d_inode;
14960caa102dSLi Zefan 	struct btrfs_root *root = BTRFS_I(inode)->root;
14970caa102dSLi Zefan 	int ret = 0;
14980caa102dSLi Zefan 	u64 flags = 0;
14990caa102dSLi Zefan 
150033345d01SLi Zefan 	if (btrfs_ino(inode) != BTRFS_FIRST_FREE_OBJECTID)
15010caa102dSLi Zefan 		return -EINVAL;
15020caa102dSLi Zefan 
15030caa102dSLi Zefan 	down_read(&root->fs_info->subvol_sem);
15040caa102dSLi Zefan 	if (btrfs_root_readonly(root))
15050caa102dSLi Zefan 		flags |= BTRFS_SUBVOL_RDONLY;
15060caa102dSLi Zefan 	up_read(&root->fs_info->subvol_sem);
15070caa102dSLi Zefan 
15080caa102dSLi Zefan 	if (copy_to_user(arg, &flags, sizeof(flags)))
15090caa102dSLi Zefan 		ret = -EFAULT;
15100caa102dSLi Zefan 
15110caa102dSLi Zefan 	return ret;
15120caa102dSLi Zefan }
15130caa102dSLi Zefan 
15140caa102dSLi Zefan static noinline int btrfs_ioctl_subvol_setflags(struct file *file,
15150caa102dSLi Zefan 					      void __user *arg)
15160caa102dSLi Zefan {
15170caa102dSLi Zefan 	struct inode *inode = fdentry(file)->d_inode;
15180caa102dSLi Zefan 	struct btrfs_root *root = BTRFS_I(inode)->root;
15190caa102dSLi Zefan 	struct btrfs_trans_handle *trans;
15200caa102dSLi Zefan 	u64 root_flags;
15210caa102dSLi Zefan 	u64 flags;
15220caa102dSLi Zefan 	int ret = 0;
15230caa102dSLi Zefan 
15240caa102dSLi Zefan 	if (root->fs_info->sb->s_flags & MS_RDONLY)
15250caa102dSLi Zefan 		return -EROFS;
15260caa102dSLi Zefan 
152733345d01SLi Zefan 	if (btrfs_ino(inode) != BTRFS_FIRST_FREE_OBJECTID)
15280caa102dSLi Zefan 		return -EINVAL;
15290caa102dSLi Zefan 
15300caa102dSLi Zefan 	if (copy_from_user(&flags, arg, sizeof(flags)))
15310caa102dSLi Zefan 		return -EFAULT;
15320caa102dSLi Zefan 
1533b4dc2b8cSLi Zefan 	if (flags & BTRFS_SUBVOL_CREATE_ASYNC)
15340caa102dSLi Zefan 		return -EINVAL;
15350caa102dSLi Zefan 
15360caa102dSLi Zefan 	if (flags & ~BTRFS_SUBVOL_RDONLY)
15370caa102dSLi Zefan 		return -EOPNOTSUPP;
15380caa102dSLi Zefan 
15392e149670SSerge E. Hallyn 	if (!inode_owner_or_capable(inode))
1540b4dc2b8cSLi Zefan 		return -EACCES;
1541b4dc2b8cSLi Zefan 
15420caa102dSLi Zefan 	down_write(&root->fs_info->subvol_sem);
15430caa102dSLi Zefan 
15440caa102dSLi Zefan 	/* nothing to do */
15450caa102dSLi Zefan 	if (!!(flags & BTRFS_SUBVOL_RDONLY) == btrfs_root_readonly(root))
15460caa102dSLi Zefan 		goto out;
15470caa102dSLi Zefan 
15480caa102dSLi Zefan 	root_flags = btrfs_root_flags(&root->root_item);
15490caa102dSLi Zefan 	if (flags & BTRFS_SUBVOL_RDONLY)
15500caa102dSLi Zefan 		btrfs_set_root_flags(&root->root_item,
15510caa102dSLi Zefan 				     root_flags | BTRFS_ROOT_SUBVOL_RDONLY);
15520caa102dSLi Zefan 	else
15530caa102dSLi Zefan 		btrfs_set_root_flags(&root->root_item,
15540caa102dSLi Zefan 				     root_flags & ~BTRFS_ROOT_SUBVOL_RDONLY);
15550caa102dSLi Zefan 
15560caa102dSLi Zefan 	trans = btrfs_start_transaction(root, 1);
15570caa102dSLi Zefan 	if (IS_ERR(trans)) {
15580caa102dSLi Zefan 		ret = PTR_ERR(trans);
15590caa102dSLi Zefan 		goto out_reset;
15600caa102dSLi Zefan 	}
15610caa102dSLi Zefan 
1562b4dc2b8cSLi Zefan 	ret = btrfs_update_root(trans, root->fs_info->tree_root,
15630caa102dSLi Zefan 				&root->root_key, &root->root_item);
15640caa102dSLi Zefan 
15650caa102dSLi Zefan 	btrfs_commit_transaction(trans, root);
15660caa102dSLi Zefan out_reset:
15670caa102dSLi Zefan 	if (ret)
15680caa102dSLi Zefan 		btrfs_set_root_flags(&root->root_item, root_flags);
15690caa102dSLi Zefan out:
15700caa102dSLi Zefan 	up_write(&root->fs_info->subvol_sem);
15710caa102dSLi Zefan 	return ret;
15720caa102dSLi Zefan }
15730caa102dSLi Zefan 
157476dda93cSYan, Zheng /*
157576dda93cSYan, Zheng  * helper to check if the subvolume references other subvolumes
157676dda93cSYan, Zheng  */
157776dda93cSYan, Zheng static noinline int may_destroy_subvol(struct btrfs_root *root)
157876dda93cSYan, Zheng {
157976dda93cSYan, Zheng 	struct btrfs_path *path;
158076dda93cSYan, Zheng 	struct btrfs_key key;
158176dda93cSYan, Zheng 	int ret;
158276dda93cSYan, Zheng 
158376dda93cSYan, Zheng 	path = btrfs_alloc_path();
158476dda93cSYan, Zheng 	if (!path)
158576dda93cSYan, Zheng 		return -ENOMEM;
158676dda93cSYan, Zheng 
158776dda93cSYan, Zheng 	key.objectid = root->root_key.objectid;
158876dda93cSYan, Zheng 	key.type = BTRFS_ROOT_REF_KEY;
158976dda93cSYan, Zheng 	key.offset = (u64)-1;
159076dda93cSYan, Zheng 
159176dda93cSYan, Zheng 	ret = btrfs_search_slot(NULL, root->fs_info->tree_root,
159276dda93cSYan, Zheng 				&key, path, 0, 0);
159376dda93cSYan, Zheng 	if (ret < 0)
159476dda93cSYan, Zheng 		goto out;
159576dda93cSYan, Zheng 	BUG_ON(ret == 0);
159676dda93cSYan, Zheng 
159776dda93cSYan, Zheng 	ret = 0;
159876dda93cSYan, Zheng 	if (path->slots[0] > 0) {
159976dda93cSYan, Zheng 		path->slots[0]--;
160076dda93cSYan, Zheng 		btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
160176dda93cSYan, Zheng 		if (key.objectid == root->root_key.objectid &&
160276dda93cSYan, Zheng 		    key.type == BTRFS_ROOT_REF_KEY)
160376dda93cSYan, Zheng 			ret = -ENOTEMPTY;
160476dda93cSYan, Zheng 	}
160576dda93cSYan, Zheng out:
160676dda93cSYan, Zheng 	btrfs_free_path(path);
160776dda93cSYan, Zheng 	return ret;
160876dda93cSYan, Zheng }
160976dda93cSYan, Zheng 
1610ac8e9819SChris Mason static noinline int key_in_sk(struct btrfs_key *key,
1611ac8e9819SChris Mason 			      struct btrfs_ioctl_search_key *sk)
1612ac8e9819SChris Mason {
1613abc6e134SChris Mason 	struct btrfs_key test;
1614abc6e134SChris Mason 	int ret;
1615abc6e134SChris Mason 
1616abc6e134SChris Mason 	test.objectid = sk->min_objectid;
1617abc6e134SChris Mason 	test.type = sk->min_type;
1618abc6e134SChris Mason 	test.offset = sk->min_offset;
1619abc6e134SChris Mason 
1620abc6e134SChris Mason 	ret = btrfs_comp_cpu_keys(key, &test);
1621abc6e134SChris Mason 	if (ret < 0)
1622ac8e9819SChris Mason 		return 0;
1623abc6e134SChris Mason 
1624abc6e134SChris Mason 	test.objectid = sk->max_objectid;
1625abc6e134SChris Mason 	test.type = sk->max_type;
1626abc6e134SChris Mason 	test.offset = sk->max_offset;
1627abc6e134SChris Mason 
1628abc6e134SChris Mason 	ret = btrfs_comp_cpu_keys(key, &test);
1629abc6e134SChris Mason 	if (ret > 0)
1630ac8e9819SChris Mason 		return 0;
1631ac8e9819SChris Mason 	return 1;
1632ac8e9819SChris Mason }
1633ac8e9819SChris Mason 
1634ac8e9819SChris Mason static noinline int copy_to_sk(struct btrfs_root *root,
1635ac8e9819SChris Mason 			       struct btrfs_path *path,
1636ac8e9819SChris Mason 			       struct btrfs_key *key,
1637ac8e9819SChris Mason 			       struct btrfs_ioctl_search_key *sk,
1638ac8e9819SChris Mason 			       char *buf,
1639ac8e9819SChris Mason 			       unsigned long *sk_offset,
1640ac8e9819SChris Mason 			       int *num_found)
1641ac8e9819SChris Mason {
1642ac8e9819SChris Mason 	u64 found_transid;
1643ac8e9819SChris Mason 	struct extent_buffer *leaf;
1644ac8e9819SChris Mason 	struct btrfs_ioctl_search_header sh;
1645ac8e9819SChris Mason 	unsigned long item_off;
1646ac8e9819SChris Mason 	unsigned long item_len;
1647ac8e9819SChris Mason 	int nritems;
1648ac8e9819SChris Mason 	int i;
1649ac8e9819SChris Mason 	int slot;
1650ac8e9819SChris Mason 	int ret = 0;
1651ac8e9819SChris Mason 
1652ac8e9819SChris Mason 	leaf = path->nodes[0];
1653ac8e9819SChris Mason 	slot = path->slots[0];
1654ac8e9819SChris Mason 	nritems = btrfs_header_nritems(leaf);
1655ac8e9819SChris Mason 
1656ac8e9819SChris Mason 	if (btrfs_header_generation(leaf) > sk->max_transid) {
1657ac8e9819SChris Mason 		i = nritems;
1658ac8e9819SChris Mason 		goto advance_key;
1659ac8e9819SChris Mason 	}
1660ac8e9819SChris Mason 	found_transid = btrfs_header_generation(leaf);
1661ac8e9819SChris Mason 
1662ac8e9819SChris Mason 	for (i = slot; i < nritems; i++) {
1663ac8e9819SChris Mason 		item_off = btrfs_item_ptr_offset(leaf, i);
1664ac8e9819SChris Mason 		item_len = btrfs_item_size_nr(leaf, i);
1665ac8e9819SChris Mason 
1666ac8e9819SChris Mason 		if (item_len > BTRFS_SEARCH_ARGS_BUFSIZE)
1667ac8e9819SChris Mason 			item_len = 0;
1668ac8e9819SChris Mason 
1669ac8e9819SChris Mason 		if (sizeof(sh) + item_len + *sk_offset >
1670ac8e9819SChris Mason 		    BTRFS_SEARCH_ARGS_BUFSIZE) {
1671ac8e9819SChris Mason 			ret = 1;
1672ac8e9819SChris Mason 			goto overflow;
1673ac8e9819SChris Mason 		}
1674ac8e9819SChris Mason 
1675ac8e9819SChris Mason 		btrfs_item_key_to_cpu(leaf, key, i);
1676ac8e9819SChris Mason 		if (!key_in_sk(key, sk))
1677ac8e9819SChris Mason 			continue;
1678ac8e9819SChris Mason 
1679ac8e9819SChris Mason 		sh.objectid = key->objectid;
1680ac8e9819SChris Mason 		sh.offset = key->offset;
1681ac8e9819SChris Mason 		sh.type = key->type;
1682ac8e9819SChris Mason 		sh.len = item_len;
1683ac8e9819SChris Mason 		sh.transid = found_transid;
1684ac8e9819SChris Mason 
1685ac8e9819SChris Mason 		/* copy search result header */
1686ac8e9819SChris Mason 		memcpy(buf + *sk_offset, &sh, sizeof(sh));
1687ac8e9819SChris Mason 		*sk_offset += sizeof(sh);
1688ac8e9819SChris Mason 
1689ac8e9819SChris Mason 		if (item_len) {
1690ac8e9819SChris Mason 			char *p = buf + *sk_offset;
1691ac8e9819SChris Mason 			/* copy the item */
1692ac8e9819SChris Mason 			read_extent_buffer(leaf, p,
1693ac8e9819SChris Mason 					   item_off, item_len);
1694ac8e9819SChris Mason 			*sk_offset += item_len;
1695ac8e9819SChris Mason 		}
1696e2156867SHugo Mills 		(*num_found)++;
1697ac8e9819SChris Mason 
1698ac8e9819SChris Mason 		if (*num_found >= sk->nr_items)
1699ac8e9819SChris Mason 			break;
1700ac8e9819SChris Mason 	}
1701ac8e9819SChris Mason advance_key:
1702ac8e9819SChris Mason 	ret = 0;
1703abc6e134SChris Mason 	if (key->offset < (u64)-1 && key->offset < sk->max_offset)
1704abc6e134SChris Mason 		key->offset++;
1705abc6e134SChris Mason 	else if (key->type < (u8)-1 && key->type < sk->max_type) {
1706abc6e134SChris Mason 		key->offset = 0;
1707abc6e134SChris Mason 		key->type++;
1708abc6e134SChris Mason 	} else if (key->objectid < (u64)-1 && key->objectid < sk->max_objectid) {
1709abc6e134SChris Mason 		key->offset = 0;
1710abc6e134SChris Mason 		key->type = 0;
1711abc6e134SChris Mason 		key->objectid++;
1712abc6e134SChris Mason 	} else
1713abc6e134SChris Mason 		ret = 1;
1714ac8e9819SChris Mason overflow:
1715ac8e9819SChris Mason 	return ret;
1716ac8e9819SChris Mason }
1717ac8e9819SChris Mason 
1718ac8e9819SChris Mason static noinline int search_ioctl(struct inode *inode,
1719ac8e9819SChris Mason 				 struct btrfs_ioctl_search_args *args)
1720ac8e9819SChris Mason {
1721ac8e9819SChris Mason 	struct btrfs_root *root;
1722ac8e9819SChris Mason 	struct btrfs_key key;
1723ac8e9819SChris Mason 	struct btrfs_key max_key;
1724ac8e9819SChris Mason 	struct btrfs_path *path;
1725ac8e9819SChris Mason 	struct btrfs_ioctl_search_key *sk = &args->key;
1726ac8e9819SChris Mason 	struct btrfs_fs_info *info = BTRFS_I(inode)->root->fs_info;
1727ac8e9819SChris Mason 	int ret;
1728ac8e9819SChris Mason 	int num_found = 0;
1729ac8e9819SChris Mason 	unsigned long sk_offset = 0;
1730ac8e9819SChris Mason 
1731ac8e9819SChris Mason 	path = btrfs_alloc_path();
1732ac8e9819SChris Mason 	if (!path)
1733ac8e9819SChris Mason 		return -ENOMEM;
1734ac8e9819SChris Mason 
1735ac8e9819SChris Mason 	if (sk->tree_id == 0) {
1736ac8e9819SChris Mason 		/* search the root of the inode that was passed */
1737ac8e9819SChris Mason 		root = BTRFS_I(inode)->root;
1738ac8e9819SChris Mason 	} else {
1739ac8e9819SChris Mason 		key.objectid = sk->tree_id;
1740ac8e9819SChris Mason 		key.type = BTRFS_ROOT_ITEM_KEY;
1741ac8e9819SChris Mason 		key.offset = (u64)-1;
1742ac8e9819SChris Mason 		root = btrfs_read_fs_root_no_name(info, &key);
1743ac8e9819SChris Mason 		if (IS_ERR(root)) {
1744ac8e9819SChris Mason 			printk(KERN_ERR "could not find root %llu\n",
1745ac8e9819SChris Mason 			       sk->tree_id);
1746ac8e9819SChris Mason 			btrfs_free_path(path);
1747ac8e9819SChris Mason 			return -ENOENT;
1748ac8e9819SChris Mason 		}
1749ac8e9819SChris Mason 	}
1750ac8e9819SChris Mason 
1751ac8e9819SChris Mason 	key.objectid = sk->min_objectid;
1752ac8e9819SChris Mason 	key.type = sk->min_type;
1753ac8e9819SChris Mason 	key.offset = sk->min_offset;
1754ac8e9819SChris Mason 
1755ac8e9819SChris Mason 	max_key.objectid = sk->max_objectid;
1756ac8e9819SChris Mason 	max_key.type = sk->max_type;
1757ac8e9819SChris Mason 	max_key.offset = sk->max_offset;
1758ac8e9819SChris Mason 
1759ac8e9819SChris Mason 	path->keep_locks = 1;
1760ac8e9819SChris Mason 
1761ac8e9819SChris Mason 	while(1) {
1762ac8e9819SChris Mason 		ret = btrfs_search_forward(root, &key, &max_key, path, 0,
1763ac8e9819SChris Mason 					   sk->min_transid);
1764ac8e9819SChris Mason 		if (ret != 0) {
1765ac8e9819SChris Mason 			if (ret > 0)
1766ac8e9819SChris Mason 				ret = 0;
1767ac8e9819SChris Mason 			goto err;
1768ac8e9819SChris Mason 		}
1769ac8e9819SChris Mason 		ret = copy_to_sk(root, path, &key, sk, args->buf,
1770ac8e9819SChris Mason 				 &sk_offset, &num_found);
1771b3b4aa74SDavid Sterba 		btrfs_release_path(path);
1772ac8e9819SChris Mason 		if (ret || num_found >= sk->nr_items)
1773ac8e9819SChris Mason 			break;
1774ac8e9819SChris Mason 
1775ac8e9819SChris Mason 	}
1776ac8e9819SChris Mason 	ret = 0;
1777ac8e9819SChris Mason err:
1778ac8e9819SChris Mason 	sk->nr_items = num_found;
1779ac8e9819SChris Mason 	btrfs_free_path(path);
1780ac8e9819SChris Mason 	return ret;
1781ac8e9819SChris Mason }
1782ac8e9819SChris Mason 
1783ac8e9819SChris Mason static noinline int btrfs_ioctl_tree_search(struct file *file,
1784ac8e9819SChris Mason 					   void __user *argp)
1785ac8e9819SChris Mason {
1786ac8e9819SChris Mason 	 struct btrfs_ioctl_search_args *args;
1787ac8e9819SChris Mason 	 struct inode *inode;
1788ac8e9819SChris Mason 	 int ret;
1789ac8e9819SChris Mason 
1790ac8e9819SChris Mason 	if (!capable(CAP_SYS_ADMIN))
1791ac8e9819SChris Mason 		return -EPERM;
1792ac8e9819SChris Mason 
17932354d08fSJulia Lawall 	args = memdup_user(argp, sizeof(*args));
17942354d08fSJulia Lawall 	if (IS_ERR(args))
17952354d08fSJulia Lawall 		return PTR_ERR(args);
1796ac8e9819SChris Mason 
1797ac8e9819SChris Mason 	inode = fdentry(file)->d_inode;
1798ac8e9819SChris Mason 	ret = search_ioctl(inode, args);
1799ac8e9819SChris Mason 	if (ret == 0 && copy_to_user(argp, args, sizeof(*args)))
1800ac8e9819SChris Mason 		ret = -EFAULT;
1801ac8e9819SChris Mason 	kfree(args);
1802ac8e9819SChris Mason 	return ret;
1803ac8e9819SChris Mason }
1804ac8e9819SChris Mason 
180598d377a0STARUISI Hiroaki /*
1806ac8e9819SChris Mason  * Search INODE_REFs to identify path name of 'dirid' directory
1807ac8e9819SChris Mason  * in a 'tree_id' tree. and sets path name to 'name'.
180898d377a0STARUISI Hiroaki  */
180998d377a0STARUISI Hiroaki static noinline int btrfs_search_path_in_tree(struct btrfs_fs_info *info,
181098d377a0STARUISI Hiroaki 				u64 tree_id, u64 dirid, char *name)
181198d377a0STARUISI Hiroaki {
181298d377a0STARUISI Hiroaki 	struct btrfs_root *root;
181398d377a0STARUISI Hiroaki 	struct btrfs_key key;
1814ac8e9819SChris Mason 	char *ptr;
181598d377a0STARUISI Hiroaki 	int ret = -1;
181698d377a0STARUISI Hiroaki 	int slot;
181798d377a0STARUISI Hiroaki 	int len;
181898d377a0STARUISI Hiroaki 	int total_len = 0;
181998d377a0STARUISI Hiroaki 	struct btrfs_inode_ref *iref;
182098d377a0STARUISI Hiroaki 	struct extent_buffer *l;
182198d377a0STARUISI Hiroaki 	struct btrfs_path *path;
182298d377a0STARUISI Hiroaki 
182398d377a0STARUISI Hiroaki 	if (dirid == BTRFS_FIRST_FREE_OBJECTID) {
182498d377a0STARUISI Hiroaki 		name[0]='\0';
182598d377a0STARUISI Hiroaki 		return 0;
182698d377a0STARUISI Hiroaki 	}
182798d377a0STARUISI Hiroaki 
182898d377a0STARUISI Hiroaki 	path = btrfs_alloc_path();
182998d377a0STARUISI Hiroaki 	if (!path)
183098d377a0STARUISI Hiroaki 		return -ENOMEM;
183198d377a0STARUISI Hiroaki 
1832ac8e9819SChris Mason 	ptr = &name[BTRFS_INO_LOOKUP_PATH_MAX];
183398d377a0STARUISI Hiroaki 
183498d377a0STARUISI Hiroaki 	key.objectid = tree_id;
183598d377a0STARUISI Hiroaki 	key.type = BTRFS_ROOT_ITEM_KEY;
183698d377a0STARUISI Hiroaki 	key.offset = (u64)-1;
183798d377a0STARUISI Hiroaki 	root = btrfs_read_fs_root_no_name(info, &key);
183898d377a0STARUISI Hiroaki 	if (IS_ERR(root)) {
183998d377a0STARUISI Hiroaki 		printk(KERN_ERR "could not find root %llu\n", tree_id);
18408ad6fcabSChris Mason 		ret = -ENOENT;
18418ad6fcabSChris Mason 		goto out;
184298d377a0STARUISI Hiroaki 	}
184398d377a0STARUISI Hiroaki 
184498d377a0STARUISI Hiroaki 	key.objectid = dirid;
184598d377a0STARUISI Hiroaki 	key.type = BTRFS_INODE_REF_KEY;
18468ad6fcabSChris Mason 	key.offset = (u64)-1;
184798d377a0STARUISI Hiroaki 
184898d377a0STARUISI Hiroaki 	while(1) {
184998d377a0STARUISI Hiroaki 		ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
185098d377a0STARUISI Hiroaki 		if (ret < 0)
185198d377a0STARUISI Hiroaki 			goto out;
185298d377a0STARUISI Hiroaki 
185398d377a0STARUISI Hiroaki 		l = path->nodes[0];
185498d377a0STARUISI Hiroaki 		slot = path->slots[0];
18558ad6fcabSChris Mason 		if (ret > 0 && slot > 0)
18568ad6fcabSChris Mason 			slot--;
185798d377a0STARUISI Hiroaki 		btrfs_item_key_to_cpu(l, &key, slot);
185898d377a0STARUISI Hiroaki 
185998d377a0STARUISI Hiroaki 		if (ret > 0 && (key.objectid != dirid ||
1860ac8e9819SChris Mason 				key.type != BTRFS_INODE_REF_KEY)) {
1861ac8e9819SChris Mason 			ret = -ENOENT;
186298d377a0STARUISI Hiroaki 			goto out;
1863ac8e9819SChris Mason 		}
186498d377a0STARUISI Hiroaki 
186598d377a0STARUISI Hiroaki 		iref = btrfs_item_ptr(l, slot, struct btrfs_inode_ref);
186698d377a0STARUISI Hiroaki 		len = btrfs_inode_ref_name_len(l, iref);
186798d377a0STARUISI Hiroaki 		ptr -= len + 1;
186898d377a0STARUISI Hiroaki 		total_len += len + 1;
1869ac8e9819SChris Mason 		if (ptr < name)
187098d377a0STARUISI Hiroaki 			goto out;
187198d377a0STARUISI Hiroaki 
187298d377a0STARUISI Hiroaki 		*(ptr + len) = '/';
187398d377a0STARUISI Hiroaki 		read_extent_buffer(l, ptr,(unsigned long)(iref + 1), len);
187498d377a0STARUISI Hiroaki 
187598d377a0STARUISI Hiroaki 		if (key.offset == BTRFS_FIRST_FREE_OBJECTID)
187698d377a0STARUISI Hiroaki 			break;
187798d377a0STARUISI Hiroaki 
1878b3b4aa74SDavid Sterba 		btrfs_release_path(path);
187998d377a0STARUISI Hiroaki 		key.objectid = key.offset;
18808ad6fcabSChris Mason 		key.offset = (u64)-1;
188198d377a0STARUISI Hiroaki 		dirid = key.objectid;
188298d377a0STARUISI Hiroaki 	}
1883ac8e9819SChris Mason 	if (ptr < name)
188498d377a0STARUISI Hiroaki 		goto out;
188577906a50SLi Zefan 	memmove(name, ptr, total_len);
188698d377a0STARUISI Hiroaki 	name[total_len]='\0';
188798d377a0STARUISI Hiroaki 	ret = 0;
188898d377a0STARUISI Hiroaki out:
188998d377a0STARUISI Hiroaki 	btrfs_free_path(path);
1890ac8e9819SChris Mason 	return ret;
1891ac8e9819SChris Mason }
1892ac8e9819SChris Mason 
1893ac8e9819SChris Mason static noinline int btrfs_ioctl_ino_lookup(struct file *file,
1894ac8e9819SChris Mason 					   void __user *argp)
1895ac8e9819SChris Mason {
1896ac8e9819SChris Mason 	 struct btrfs_ioctl_ino_lookup_args *args;
1897ac8e9819SChris Mason 	 struct inode *inode;
1898ac8e9819SChris Mason 	 int ret;
1899ac8e9819SChris Mason 
1900ac8e9819SChris Mason 	if (!capable(CAP_SYS_ADMIN))
1901ac8e9819SChris Mason 		return -EPERM;
1902ac8e9819SChris Mason 
19032354d08fSJulia Lawall 	args = memdup_user(argp, sizeof(*args));
19042354d08fSJulia Lawall 	if (IS_ERR(args))
19052354d08fSJulia Lawall 		return PTR_ERR(args);
1906c2b96929SDan Carpenter 
1907ac8e9819SChris Mason 	inode = fdentry(file)->d_inode;
1908ac8e9819SChris Mason 
19091b53ac4dSChris Mason 	if (args->treeid == 0)
19101b53ac4dSChris Mason 		args->treeid = BTRFS_I(inode)->root->root_key.objectid;
19111b53ac4dSChris Mason 
1912ac8e9819SChris Mason 	ret = btrfs_search_path_in_tree(BTRFS_I(inode)->root->fs_info,
1913ac8e9819SChris Mason 					args->treeid, args->objectid,
1914ac8e9819SChris Mason 					args->name);
1915ac8e9819SChris Mason 
1916ac8e9819SChris Mason 	if (ret == 0 && copy_to_user(argp, args, sizeof(*args)))
1917ac8e9819SChris Mason 		ret = -EFAULT;
1918ac8e9819SChris Mason 
1919ac8e9819SChris Mason 	kfree(args);
192098d377a0STARUISI Hiroaki 	return ret;
192198d377a0STARUISI Hiroaki }
192298d377a0STARUISI Hiroaki 
192376dda93cSYan, Zheng static noinline int btrfs_ioctl_snap_destroy(struct file *file,
192476dda93cSYan, Zheng 					     void __user *arg)
192576dda93cSYan, Zheng {
192676dda93cSYan, Zheng 	struct dentry *parent = fdentry(file);
192776dda93cSYan, Zheng 	struct dentry *dentry;
192876dda93cSYan, Zheng 	struct inode *dir = parent->d_inode;
192976dda93cSYan, Zheng 	struct inode *inode;
193076dda93cSYan, Zheng 	struct btrfs_root *root = BTRFS_I(dir)->root;
193176dda93cSYan, Zheng 	struct btrfs_root *dest = NULL;
193276dda93cSYan, Zheng 	struct btrfs_ioctl_vol_args *vol_args;
193376dda93cSYan, Zheng 	struct btrfs_trans_handle *trans;
193476dda93cSYan, Zheng 	int namelen;
193576dda93cSYan, Zheng 	int ret;
193676dda93cSYan, Zheng 	int err = 0;
193776dda93cSYan, Zheng 
193876dda93cSYan, Zheng 	vol_args = memdup_user(arg, sizeof(*vol_args));
193976dda93cSYan, Zheng 	if (IS_ERR(vol_args))
194076dda93cSYan, Zheng 		return PTR_ERR(vol_args);
194176dda93cSYan, Zheng 
194276dda93cSYan, Zheng 	vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
194376dda93cSYan, Zheng 	namelen = strlen(vol_args->name);
194476dda93cSYan, Zheng 	if (strchr(vol_args->name, '/') ||
194576dda93cSYan, Zheng 	    strncmp(vol_args->name, "..", namelen) == 0) {
194676dda93cSYan, Zheng 		err = -EINVAL;
194776dda93cSYan, Zheng 		goto out;
194876dda93cSYan, Zheng 	}
194976dda93cSYan, Zheng 
1950a561be71SAl Viro 	err = mnt_want_write_file(file);
195176dda93cSYan, Zheng 	if (err)
195276dda93cSYan, Zheng 		goto out;
195376dda93cSYan, Zheng 
195476dda93cSYan, Zheng 	mutex_lock_nested(&dir->i_mutex, I_MUTEX_PARENT);
195576dda93cSYan, Zheng 	dentry = lookup_one_len(vol_args->name, parent, namelen);
195676dda93cSYan, Zheng 	if (IS_ERR(dentry)) {
195776dda93cSYan, Zheng 		err = PTR_ERR(dentry);
195876dda93cSYan, Zheng 		goto out_unlock_dir;
195976dda93cSYan, Zheng 	}
196076dda93cSYan, Zheng 
196176dda93cSYan, Zheng 	if (!dentry->d_inode) {
196276dda93cSYan, Zheng 		err = -ENOENT;
196376dda93cSYan, Zheng 		goto out_dput;
196476dda93cSYan, Zheng 	}
196576dda93cSYan, Zheng 
196676dda93cSYan, Zheng 	inode = dentry->d_inode;
19674260f7c7SSage Weil 	dest = BTRFS_I(inode)->root;
19684260f7c7SSage Weil 	if (!capable(CAP_SYS_ADMIN)){
19694260f7c7SSage Weil 		/*
19704260f7c7SSage Weil 		 * Regular user.  Only allow this with a special mount
19714260f7c7SSage Weil 		 * option, when the user has write+exec access to the
19724260f7c7SSage Weil 		 * subvol root, and when rmdir(2) would have been
19734260f7c7SSage Weil 		 * allowed.
19744260f7c7SSage Weil 		 *
19754260f7c7SSage Weil 		 * Note that this is _not_ check that the subvol is
19764260f7c7SSage Weil 		 * empty or doesn't contain data that we wouldn't
19774260f7c7SSage Weil 		 * otherwise be able to delete.
19784260f7c7SSage Weil 		 *
19794260f7c7SSage Weil 		 * Users who want to delete empty subvols should try
19804260f7c7SSage Weil 		 * rmdir(2).
19814260f7c7SSage Weil 		 */
19824260f7c7SSage Weil 		err = -EPERM;
19834260f7c7SSage Weil 		if (!btrfs_test_opt(root, USER_SUBVOL_RM_ALLOWED))
19844260f7c7SSage Weil 			goto out_dput;
19854260f7c7SSage Weil 
19864260f7c7SSage Weil 		/*
19874260f7c7SSage Weil 		 * Do not allow deletion if the parent dir is the same
19884260f7c7SSage Weil 		 * as the dir to be deleted.  That means the ioctl
19894260f7c7SSage Weil 		 * must be called on the dentry referencing the root
19904260f7c7SSage Weil 		 * of the subvol, not a random directory contained
19914260f7c7SSage Weil 		 * within it.
19924260f7c7SSage Weil 		 */
19934260f7c7SSage Weil 		err = -EINVAL;
19944260f7c7SSage Weil 		if (root == dest)
19954260f7c7SSage Weil 			goto out_dput;
19964260f7c7SSage Weil 
19974260f7c7SSage Weil 		err = inode_permission(inode, MAY_WRITE | MAY_EXEC);
19984260f7c7SSage Weil 		if (err)
19994260f7c7SSage Weil 			goto out_dput;
20004260f7c7SSage Weil 
20014260f7c7SSage Weil 		/* check if subvolume may be deleted by a non-root user */
20024260f7c7SSage Weil 		err = btrfs_may_delete(dir, dentry, 1);
20034260f7c7SSage Weil 		if (err)
20044260f7c7SSage Weil 			goto out_dput;
20054260f7c7SSage Weil 	}
20064260f7c7SSage Weil 
200733345d01SLi Zefan 	if (btrfs_ino(inode) != BTRFS_FIRST_FREE_OBJECTID) {
200876dda93cSYan, Zheng 		err = -EINVAL;
200976dda93cSYan, Zheng 		goto out_dput;
201076dda93cSYan, Zheng 	}
201176dda93cSYan, Zheng 
201276dda93cSYan, Zheng 	mutex_lock(&inode->i_mutex);
201376dda93cSYan, Zheng 	err = d_invalidate(dentry);
201476dda93cSYan, Zheng 	if (err)
201576dda93cSYan, Zheng 		goto out_unlock;
201676dda93cSYan, Zheng 
201776dda93cSYan, Zheng 	down_write(&root->fs_info->subvol_sem);
201876dda93cSYan, Zheng 
201976dda93cSYan, Zheng 	err = may_destroy_subvol(dest);
202076dda93cSYan, Zheng 	if (err)
202176dda93cSYan, Zheng 		goto out_up_write;
202276dda93cSYan, Zheng 
2023a22285a6SYan, Zheng 	trans = btrfs_start_transaction(root, 0);
2024a22285a6SYan, Zheng 	if (IS_ERR(trans)) {
2025a22285a6SYan, Zheng 		err = PTR_ERR(trans);
2026d327099aSDan Carpenter 		goto out_up_write;
2027a22285a6SYan, Zheng 	}
2028a22285a6SYan, Zheng 	trans->block_rsv = &root->fs_info->global_block_rsv;
2029a22285a6SYan, Zheng 
203076dda93cSYan, Zheng 	ret = btrfs_unlink_subvol(trans, root, dir,
203176dda93cSYan, Zheng 				dest->root_key.objectid,
203276dda93cSYan, Zheng 				dentry->d_name.name,
203376dda93cSYan, Zheng 				dentry->d_name.len);
203479787eaaSJeff Mahoney 	if (ret) {
203579787eaaSJeff Mahoney 		err = ret;
203679787eaaSJeff Mahoney 		btrfs_abort_transaction(trans, root, ret);
203779787eaaSJeff Mahoney 		goto out_end_trans;
203879787eaaSJeff Mahoney 	}
203976dda93cSYan, Zheng 
204076dda93cSYan, Zheng 	btrfs_record_root_in_trans(trans, dest);
204176dda93cSYan, Zheng 
204276dda93cSYan, Zheng 	memset(&dest->root_item.drop_progress, 0,
204376dda93cSYan, Zheng 		sizeof(dest->root_item.drop_progress));
204476dda93cSYan, Zheng 	dest->root_item.drop_level = 0;
204576dda93cSYan, Zheng 	btrfs_set_root_refs(&dest->root_item, 0);
204676dda93cSYan, Zheng 
2047d68fc57bSYan, Zheng 	if (!xchg(&dest->orphan_item_inserted, 1)) {
204876dda93cSYan, Zheng 		ret = btrfs_insert_orphan_item(trans,
204976dda93cSYan, Zheng 					root->fs_info->tree_root,
205076dda93cSYan, Zheng 					dest->root_key.objectid);
205179787eaaSJeff Mahoney 		if (ret) {
205279787eaaSJeff Mahoney 			btrfs_abort_transaction(trans, root, ret);
205379787eaaSJeff Mahoney 			err = ret;
205479787eaaSJeff Mahoney 			goto out_end_trans;
2055d68fc57bSYan, Zheng 		}
205679787eaaSJeff Mahoney 	}
205779787eaaSJeff Mahoney out_end_trans:
2058531cb13fSSage Weil 	ret = btrfs_end_transaction(trans, root);
205979787eaaSJeff Mahoney 	if (ret && !err)
206079787eaaSJeff Mahoney 		err = ret;
206176dda93cSYan, Zheng 	inode->i_flags |= S_DEAD;
206276dda93cSYan, Zheng out_up_write:
206376dda93cSYan, Zheng 	up_write(&root->fs_info->subvol_sem);
206476dda93cSYan, Zheng out_unlock:
206576dda93cSYan, Zheng 	mutex_unlock(&inode->i_mutex);
206676dda93cSYan, Zheng 	if (!err) {
2067efefb143SYan, Zheng 		shrink_dcache_sb(root->fs_info->sb);
206876dda93cSYan, Zheng 		btrfs_invalidate_inodes(dest);
206976dda93cSYan, Zheng 		d_delete(dentry);
207076dda93cSYan, Zheng 	}
207176dda93cSYan, Zheng out_dput:
207276dda93cSYan, Zheng 	dput(dentry);
207376dda93cSYan, Zheng out_unlock_dir:
207476dda93cSYan, Zheng 	mutex_unlock(&dir->i_mutex);
20752a79f17eSAl Viro 	mnt_drop_write_file(file);
207676dda93cSYan, Zheng out:
207776dda93cSYan, Zheng 	kfree(vol_args);
207876dda93cSYan, Zheng 	return err;
207976dda93cSYan, Zheng }
208076dda93cSYan, Zheng 
20811e701a32SChris Mason static int btrfs_ioctl_defrag(struct file *file, void __user *argp)
2082f46b5a66SChristoph Hellwig {
2083f46b5a66SChristoph Hellwig 	struct inode *inode = fdentry(file)->d_inode;
2084f46b5a66SChristoph Hellwig 	struct btrfs_root *root = BTRFS_I(inode)->root;
20851e701a32SChris Mason 	struct btrfs_ioctl_defrag_range_args *range;
2086c146afadSYan Zheng 	int ret;
2087c146afadSYan Zheng 
2088b83cc969SLi Zefan 	if (btrfs_root_readonly(root))
2089b83cc969SLi Zefan 		return -EROFS;
2090b83cc969SLi Zefan 
2091a561be71SAl Viro 	ret = mnt_want_write_file(file);
2092c146afadSYan Zheng 	if (ret)
2093c146afadSYan Zheng 		return ret;
2094f46b5a66SChristoph Hellwig 
2095f46b5a66SChristoph Hellwig 	switch (inode->i_mode & S_IFMT) {
2096f46b5a66SChristoph Hellwig 	case S_IFDIR:
2097e441d54dSChris Mason 		if (!capable(CAP_SYS_ADMIN)) {
2098e441d54dSChris Mason 			ret = -EPERM;
2099e441d54dSChris Mason 			goto out;
2100e441d54dSChris Mason 		}
21018929ecfaSYan, Zheng 		ret = btrfs_defrag_root(root, 0);
21028929ecfaSYan, Zheng 		if (ret)
21038929ecfaSYan, Zheng 			goto out;
21048929ecfaSYan, Zheng 		ret = btrfs_defrag_root(root->fs_info->extent_root, 0);
2105f46b5a66SChristoph Hellwig 		break;
2106f46b5a66SChristoph Hellwig 	case S_IFREG:
2107e441d54dSChris Mason 		if (!(file->f_mode & FMODE_WRITE)) {
2108e441d54dSChris Mason 			ret = -EINVAL;
2109e441d54dSChris Mason 			goto out;
2110e441d54dSChris Mason 		}
21111e701a32SChris Mason 
21121e701a32SChris Mason 		range = kzalloc(sizeof(*range), GFP_KERNEL);
21131e701a32SChris Mason 		if (!range) {
21141e701a32SChris Mason 			ret = -ENOMEM;
21151e701a32SChris Mason 			goto out;
21161e701a32SChris Mason 		}
21171e701a32SChris Mason 
21181e701a32SChris Mason 		if (argp) {
21191e701a32SChris Mason 			if (copy_from_user(range, argp,
21201e701a32SChris Mason 					   sizeof(*range))) {
21211e701a32SChris Mason 				ret = -EFAULT;
21221e701a32SChris Mason 				kfree(range);
2123683be16eSDan Carpenter 				goto out;
21241e701a32SChris Mason 			}
21251e701a32SChris Mason 			/* compression requires us to start the IO */
21261e701a32SChris Mason 			if ((range->flags & BTRFS_DEFRAG_RANGE_COMPRESS)) {
21271e701a32SChris Mason 				range->flags |= BTRFS_DEFRAG_RANGE_START_IO;
21281e701a32SChris Mason 				range->extent_thresh = (u32)-1;
21291e701a32SChris Mason 			}
21301e701a32SChris Mason 		} else {
21311e701a32SChris Mason 			/* the rest are all set to zero by kzalloc */
21321e701a32SChris Mason 			range->len = (u64)-1;
21331e701a32SChris Mason 		}
21344cb5300bSChris Mason 		ret = btrfs_defrag_file(fdentry(file)->d_inode, file,
21354cb5300bSChris Mason 					range, 0, 0);
21364cb5300bSChris Mason 		if (ret > 0)
21374cb5300bSChris Mason 			ret = 0;
21381e701a32SChris Mason 		kfree(range);
2139f46b5a66SChristoph Hellwig 		break;
21408929ecfaSYan, Zheng 	default:
21418929ecfaSYan, Zheng 		ret = -EINVAL;
2142f46b5a66SChristoph Hellwig 	}
2143e441d54dSChris Mason out:
21442a79f17eSAl Viro 	mnt_drop_write_file(file);
2145e441d54dSChris Mason 	return ret;
2146f46b5a66SChristoph Hellwig }
2147f46b5a66SChristoph Hellwig 
2148b2950863SChristoph Hellwig static long btrfs_ioctl_add_dev(struct btrfs_root *root, void __user *arg)
2149f46b5a66SChristoph Hellwig {
2150f46b5a66SChristoph Hellwig 	struct btrfs_ioctl_vol_args *vol_args;
2151f46b5a66SChristoph Hellwig 	int ret;
2152f46b5a66SChristoph Hellwig 
2153e441d54dSChris Mason 	if (!capable(CAP_SYS_ADMIN))
2154e441d54dSChris Mason 		return -EPERM;
2155e441d54dSChris Mason 
2156c9e9f97bSIlya Dryomov 	mutex_lock(&root->fs_info->volume_mutex);
2157c9e9f97bSIlya Dryomov 	if (root->fs_info->balance_ctl) {
2158c9e9f97bSIlya Dryomov 		printk(KERN_INFO "btrfs: balance in progress\n");
2159c9e9f97bSIlya Dryomov 		ret = -EINVAL;
2160c9e9f97bSIlya Dryomov 		goto out;
2161c9e9f97bSIlya Dryomov 	}
2162c9e9f97bSIlya Dryomov 
2163dae7b665SLi Zefan 	vol_args = memdup_user(arg, sizeof(*vol_args));
2164c9e9f97bSIlya Dryomov 	if (IS_ERR(vol_args)) {
2165c9e9f97bSIlya Dryomov 		ret = PTR_ERR(vol_args);
2166c9e9f97bSIlya Dryomov 		goto out;
2167c9e9f97bSIlya Dryomov 	}
2168f46b5a66SChristoph Hellwig 
21695516e595SMark Fasheh 	vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
2170f46b5a66SChristoph Hellwig 	ret = btrfs_init_new_device(root, vol_args->name);
2171f46b5a66SChristoph Hellwig 
2172f46b5a66SChristoph Hellwig 	kfree(vol_args);
2173c9e9f97bSIlya Dryomov out:
2174c9e9f97bSIlya Dryomov 	mutex_unlock(&root->fs_info->volume_mutex);
2175f46b5a66SChristoph Hellwig 	return ret;
2176f46b5a66SChristoph Hellwig }
2177f46b5a66SChristoph Hellwig 
2178b2950863SChristoph Hellwig static long btrfs_ioctl_rm_dev(struct btrfs_root *root, void __user *arg)
2179f46b5a66SChristoph Hellwig {
2180f46b5a66SChristoph Hellwig 	struct btrfs_ioctl_vol_args *vol_args;
2181f46b5a66SChristoph Hellwig 	int ret;
2182f46b5a66SChristoph Hellwig 
2183e441d54dSChris Mason 	if (!capable(CAP_SYS_ADMIN))
2184e441d54dSChris Mason 		return -EPERM;
2185e441d54dSChris Mason 
2186c146afadSYan Zheng 	if (root->fs_info->sb->s_flags & MS_RDONLY)
2187c146afadSYan Zheng 		return -EROFS;
2188c146afadSYan Zheng 
2189c9e9f97bSIlya Dryomov 	mutex_lock(&root->fs_info->volume_mutex);
2190c9e9f97bSIlya Dryomov 	if (root->fs_info->balance_ctl) {
2191c9e9f97bSIlya Dryomov 		printk(KERN_INFO "btrfs: balance in progress\n");
2192c9e9f97bSIlya Dryomov 		ret = -EINVAL;
2193c9e9f97bSIlya Dryomov 		goto out;
2194c9e9f97bSIlya Dryomov 	}
2195c9e9f97bSIlya Dryomov 
2196dae7b665SLi Zefan 	vol_args = memdup_user(arg, sizeof(*vol_args));
2197c9e9f97bSIlya Dryomov 	if (IS_ERR(vol_args)) {
2198c9e9f97bSIlya Dryomov 		ret = PTR_ERR(vol_args);
2199c9e9f97bSIlya Dryomov 		goto out;
2200c9e9f97bSIlya Dryomov 	}
2201f46b5a66SChristoph Hellwig 
22025516e595SMark Fasheh 	vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
2203f46b5a66SChristoph Hellwig 	ret = btrfs_rm_device(root, vol_args->name);
2204f46b5a66SChristoph Hellwig 
2205f46b5a66SChristoph Hellwig 	kfree(vol_args);
2206c9e9f97bSIlya Dryomov out:
2207c9e9f97bSIlya Dryomov 	mutex_unlock(&root->fs_info->volume_mutex);
2208f46b5a66SChristoph Hellwig 	return ret;
2209f46b5a66SChristoph Hellwig }
2210f46b5a66SChristoph Hellwig 
2211475f6387SJan Schmidt static long btrfs_ioctl_fs_info(struct btrfs_root *root, void __user *arg)
2212475f6387SJan Schmidt {
2213027ed2f0SLi Zefan 	struct btrfs_ioctl_fs_info_args *fi_args;
2214475f6387SJan Schmidt 	struct btrfs_device *device;
2215475f6387SJan Schmidt 	struct btrfs_device *next;
2216475f6387SJan Schmidt 	struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices;
2217027ed2f0SLi Zefan 	int ret = 0;
2218475f6387SJan Schmidt 
2219475f6387SJan Schmidt 	if (!capable(CAP_SYS_ADMIN))
2220475f6387SJan Schmidt 		return -EPERM;
2221475f6387SJan Schmidt 
2222027ed2f0SLi Zefan 	fi_args = kzalloc(sizeof(*fi_args), GFP_KERNEL);
2223027ed2f0SLi Zefan 	if (!fi_args)
2224027ed2f0SLi Zefan 		return -ENOMEM;
2225027ed2f0SLi Zefan 
2226027ed2f0SLi Zefan 	fi_args->num_devices = fs_devices->num_devices;
2227027ed2f0SLi Zefan 	memcpy(&fi_args->fsid, root->fs_info->fsid, sizeof(fi_args->fsid));
2228475f6387SJan Schmidt 
2229475f6387SJan Schmidt 	mutex_lock(&fs_devices->device_list_mutex);
2230475f6387SJan Schmidt 	list_for_each_entry_safe(device, next, &fs_devices->devices, dev_list) {
2231027ed2f0SLi Zefan 		if (device->devid > fi_args->max_id)
2232027ed2f0SLi Zefan 			fi_args->max_id = device->devid;
2233475f6387SJan Schmidt 	}
2234475f6387SJan Schmidt 	mutex_unlock(&fs_devices->device_list_mutex);
2235475f6387SJan Schmidt 
2236027ed2f0SLi Zefan 	if (copy_to_user(arg, fi_args, sizeof(*fi_args)))
2237027ed2f0SLi Zefan 		ret = -EFAULT;
2238475f6387SJan Schmidt 
2239027ed2f0SLi Zefan 	kfree(fi_args);
2240027ed2f0SLi Zefan 	return ret;
2241475f6387SJan Schmidt }
2242475f6387SJan Schmidt 
2243475f6387SJan Schmidt static long btrfs_ioctl_dev_info(struct btrfs_root *root, void __user *arg)
2244475f6387SJan Schmidt {
2245475f6387SJan Schmidt 	struct btrfs_ioctl_dev_info_args *di_args;
2246475f6387SJan Schmidt 	struct btrfs_device *dev;
2247475f6387SJan Schmidt 	struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices;
2248475f6387SJan Schmidt 	int ret = 0;
2249475f6387SJan Schmidt 	char *s_uuid = NULL;
2250475f6387SJan Schmidt 	char empty_uuid[BTRFS_UUID_SIZE] = {0};
2251475f6387SJan Schmidt 
2252475f6387SJan Schmidt 	if (!capable(CAP_SYS_ADMIN))
2253475f6387SJan Schmidt 		return -EPERM;
2254475f6387SJan Schmidt 
2255475f6387SJan Schmidt 	di_args = memdup_user(arg, sizeof(*di_args));
2256475f6387SJan Schmidt 	if (IS_ERR(di_args))
2257475f6387SJan Schmidt 		return PTR_ERR(di_args);
2258475f6387SJan Schmidt 
2259475f6387SJan Schmidt 	if (memcmp(empty_uuid, di_args->uuid, BTRFS_UUID_SIZE) != 0)
2260475f6387SJan Schmidt 		s_uuid = di_args->uuid;
2261475f6387SJan Schmidt 
2262475f6387SJan Schmidt 	mutex_lock(&fs_devices->device_list_mutex);
2263475f6387SJan Schmidt 	dev = btrfs_find_device(root, di_args->devid, s_uuid, NULL);
2264475f6387SJan Schmidt 	mutex_unlock(&fs_devices->device_list_mutex);
2265475f6387SJan Schmidt 
2266475f6387SJan Schmidt 	if (!dev) {
2267475f6387SJan Schmidt 		ret = -ENODEV;
2268475f6387SJan Schmidt 		goto out;
2269475f6387SJan Schmidt 	}
2270475f6387SJan Schmidt 
2271475f6387SJan Schmidt 	di_args->devid = dev->devid;
2272475f6387SJan Schmidt 	di_args->bytes_used = dev->bytes_used;
2273475f6387SJan Schmidt 	di_args->total_bytes = dev->total_bytes;
2274475f6387SJan Schmidt 	memcpy(di_args->uuid, dev->uuid, sizeof(di_args->uuid));
2275a27202fbSJim Meyering 	if (dev->name) {
2276606686eeSJosef Bacik 		struct rcu_string *name;
2277606686eeSJosef Bacik 
2278606686eeSJosef Bacik 		rcu_read_lock();
2279606686eeSJosef Bacik 		name = rcu_dereference(dev->name);
2280606686eeSJosef Bacik 		strncpy(di_args->path, name->str, sizeof(di_args->path));
2281606686eeSJosef Bacik 		rcu_read_unlock();
2282a27202fbSJim Meyering 		di_args->path[sizeof(di_args->path) - 1] = 0;
2283a27202fbSJim Meyering 	} else {
228499ba55adSStefan Behrens 		di_args->path[0] = '\0';
2285a27202fbSJim Meyering 	}
2286475f6387SJan Schmidt 
2287475f6387SJan Schmidt out:
2288475f6387SJan Schmidt 	if (ret == 0 && copy_to_user(arg, di_args, sizeof(*di_args)))
2289475f6387SJan Schmidt 		ret = -EFAULT;
2290475f6387SJan Schmidt 
2291475f6387SJan Schmidt 	kfree(di_args);
2292475f6387SJan Schmidt 	return ret;
2293475f6387SJan Schmidt }
2294475f6387SJan Schmidt 
229576dda93cSYan, Zheng static noinline long btrfs_ioctl_clone(struct file *file, unsigned long srcfd,
2296b2950863SChristoph Hellwig 				       u64 off, u64 olen, u64 destoff)
2297f46b5a66SChristoph Hellwig {
2298f46b5a66SChristoph Hellwig 	struct inode *inode = fdentry(file)->d_inode;
2299f46b5a66SChristoph Hellwig 	struct btrfs_root *root = BTRFS_I(inode)->root;
2300f46b5a66SChristoph Hellwig 	struct file *src_file;
2301f46b5a66SChristoph Hellwig 	struct inode *src;
2302f46b5a66SChristoph Hellwig 	struct btrfs_trans_handle *trans;
2303f46b5a66SChristoph Hellwig 	struct btrfs_path *path;
2304f46b5a66SChristoph Hellwig 	struct extent_buffer *leaf;
2305ae01a0abSYan Zheng 	char *buf;
2306ae01a0abSYan Zheng 	struct btrfs_key key;
2307f46b5a66SChristoph Hellwig 	u32 nritems;
2308f46b5a66SChristoph Hellwig 	int slot;
2309ae01a0abSYan Zheng 	int ret;
2310c5c9cd4dSSage Weil 	u64 len = olen;
2311c5c9cd4dSSage Weil 	u64 bs = root->fs_info->sb->s_blocksize;
2312c5c9cd4dSSage Weil 	u64 hint_byte;
2313d20f7043SChris Mason 
2314c5c9cd4dSSage Weil 	/*
2315c5c9cd4dSSage Weil 	 * TODO:
2316c5c9cd4dSSage Weil 	 * - split compressed inline extents.  annoying: we need to
2317c5c9cd4dSSage Weil 	 *   decompress into destination's address_space (the file offset
2318c5c9cd4dSSage Weil 	 *   may change, so source mapping won't do), then recompress (or
2319c5c9cd4dSSage Weil 	 *   otherwise reinsert) a subrange.
2320c5c9cd4dSSage Weil 	 * - allow ranges within the same file to be cloned (provided
2321c5c9cd4dSSage Weil 	 *   they don't overlap)?
2322c5c9cd4dSSage Weil 	 */
2323c5c9cd4dSSage Weil 
2324e441d54dSChris Mason 	/* the destination must be opened for writing */
23252ebc3464SDan Rosenberg 	if (!(file->f_mode & FMODE_WRITE) || (file->f_flags & O_APPEND))
2326e441d54dSChris Mason 		return -EINVAL;
2327e441d54dSChris Mason 
2328b83cc969SLi Zefan 	if (btrfs_root_readonly(root))
2329b83cc969SLi Zefan 		return -EROFS;
2330b83cc969SLi Zefan 
2331a561be71SAl Viro 	ret = mnt_want_write_file(file);
2332c146afadSYan Zheng 	if (ret)
2333c146afadSYan Zheng 		return ret;
2334c146afadSYan Zheng 
2335c5c9cd4dSSage Weil 	src_file = fget(srcfd);
2336ab67b7c1SYan Zheng 	if (!src_file) {
2337ab67b7c1SYan Zheng 		ret = -EBADF;
2338ab67b7c1SYan Zheng 		goto out_drop_write;
2339ab67b7c1SYan Zheng 	}
23405dc64164SDan Rosenberg 
2341f46b5a66SChristoph Hellwig 	src = src_file->f_dentry->d_inode;
2342f46b5a66SChristoph Hellwig 
2343c5c9cd4dSSage Weil 	ret = -EINVAL;
2344c5c9cd4dSSage Weil 	if (src == inode)
2345c5c9cd4dSSage Weil 		goto out_fput;
2346c5c9cd4dSSage Weil 
23475dc64164SDan Rosenberg 	/* the src must be open for reading */
23485dc64164SDan Rosenberg 	if (!(src_file->f_mode & FMODE_READ))
23495dc64164SDan Rosenberg 		goto out_fput;
23505dc64164SDan Rosenberg 
23510e7b824cSLi Zefan 	/* don't make the dst file partly checksummed */
23520e7b824cSLi Zefan 	if ((BTRFS_I(src)->flags & BTRFS_INODE_NODATASUM) !=
23530e7b824cSLi Zefan 	    (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM))
23540e7b824cSLi Zefan 		goto out_fput;
23550e7b824cSLi Zefan 
2356ae01a0abSYan Zheng 	ret = -EISDIR;
2357ae01a0abSYan Zheng 	if (S_ISDIR(src->i_mode) || S_ISDIR(inode->i_mode))
2358f46b5a66SChristoph Hellwig 		goto out_fput;
2359f46b5a66SChristoph Hellwig 
2360ae01a0abSYan Zheng 	ret = -EXDEV;
2361ae01a0abSYan Zheng 	if (src->i_sb != inode->i_sb || BTRFS_I(src)->root != root)
2362ae01a0abSYan Zheng 		goto out_fput;
2363ae01a0abSYan Zheng 
2364ae01a0abSYan Zheng 	ret = -ENOMEM;
2365ae01a0abSYan Zheng 	buf = vmalloc(btrfs_level_size(root, 0));
2366ae01a0abSYan Zheng 	if (!buf)
2367ae01a0abSYan Zheng 		goto out_fput;
2368ae01a0abSYan Zheng 
2369ae01a0abSYan Zheng 	path = btrfs_alloc_path();
2370ae01a0abSYan Zheng 	if (!path) {
2371ae01a0abSYan Zheng 		vfree(buf);
2372ae01a0abSYan Zheng 		goto out_fput;
2373ae01a0abSYan Zheng 	}
2374ae01a0abSYan Zheng 	path->reada = 2;
2375ae01a0abSYan Zheng 
2376f46b5a66SChristoph Hellwig 	if (inode < src) {
2377fccdae43SSage Weil 		mutex_lock_nested(&inode->i_mutex, I_MUTEX_PARENT);
2378fccdae43SSage Weil 		mutex_lock_nested(&src->i_mutex, I_MUTEX_CHILD);
2379f46b5a66SChristoph Hellwig 	} else {
2380fccdae43SSage Weil 		mutex_lock_nested(&src->i_mutex, I_MUTEX_PARENT);
2381fccdae43SSage Weil 		mutex_lock_nested(&inode->i_mutex, I_MUTEX_CHILD);
2382f46b5a66SChristoph Hellwig 	}
2383f46b5a66SChristoph Hellwig 
2384c5c9cd4dSSage Weil 	/* determine range to clone */
2385c5c9cd4dSSage Weil 	ret = -EINVAL;
23862ebc3464SDan Rosenberg 	if (off + len > src->i_size || off + len < off)
2387f46b5a66SChristoph Hellwig 		goto out_unlock;
2388c5c9cd4dSSage Weil 	if (len == 0)
2389c5c9cd4dSSage Weil 		olen = len = src->i_size - off;
2390c5c9cd4dSSage Weil 	/* if we extend to eof, continue to block boundary */
2391c5c9cd4dSSage Weil 	if (off + len == src->i_size)
23922a6b8daeSLi Zefan 		len = ALIGN(src->i_size, bs) - off;
2393c5c9cd4dSSage Weil 
2394c5c9cd4dSSage Weil 	/* verify the end result is block aligned */
23952a6b8daeSLi Zefan 	if (!IS_ALIGNED(off, bs) || !IS_ALIGNED(off + len, bs) ||
23962a6b8daeSLi Zefan 	    !IS_ALIGNED(destoff, bs))
2397c5c9cd4dSSage Weil 		goto out_unlock;
2398c5c9cd4dSSage Weil 
2399d525e8abSLi Zefan 	if (destoff > inode->i_size) {
2400d525e8abSLi Zefan 		ret = btrfs_cont_expand(inode, inode->i_size, destoff);
2401d525e8abSLi Zefan 		if (ret)
2402d525e8abSLi Zefan 			goto out_unlock;
2403d525e8abSLi Zefan 	}
2404d525e8abSLi Zefan 
240571ef0786SLi Zefan 	/* truncate page cache pages from target inode range */
240671ef0786SLi Zefan 	truncate_inode_pages_range(&inode->i_data, destoff,
240771ef0786SLi Zefan 				   PAGE_CACHE_ALIGN(destoff + len) - 1);
240871ef0786SLi Zefan 
2409f46b5a66SChristoph Hellwig 	/* do any pending delalloc/csum calc on src, one way or
2410f46b5a66SChristoph Hellwig 	   another, and lock file content */
2411f46b5a66SChristoph Hellwig 	while (1) {
241231840ae1SZheng Yan 		struct btrfs_ordered_extent *ordered;
2413d0082371SJeff Mahoney 		lock_extent(&BTRFS_I(src)->io_tree, off, off+len);
24149a019196SSage Weil 		ordered = btrfs_lookup_first_ordered_extent(src, off+len);
24159a019196SSage Weil 		if (!ordered &&
24169a019196SSage Weil 		    !test_range_bit(&BTRFS_I(src)->io_tree, off, off+len,
24179a019196SSage Weil 				   EXTENT_DELALLOC, 0, NULL))
2418f46b5a66SChristoph Hellwig 			break;
2419d0082371SJeff Mahoney 		unlock_extent(&BTRFS_I(src)->io_tree, off, off+len);
2420ae01a0abSYan Zheng 		if (ordered)
2421ae01a0abSYan Zheng 			btrfs_put_ordered_extent(ordered);
24229a019196SSage Weil 		btrfs_wait_ordered_range(src, off, len);
2423f46b5a66SChristoph Hellwig 	}
2424f46b5a66SChristoph Hellwig 
2425c5c9cd4dSSage Weil 	/* clone data */
242633345d01SLi Zefan 	key.objectid = btrfs_ino(src);
2427ae01a0abSYan Zheng 	key.type = BTRFS_EXTENT_DATA_KEY;
2428ae01a0abSYan Zheng 	key.offset = 0;
2429f46b5a66SChristoph Hellwig 
2430f46b5a66SChristoph Hellwig 	while (1) {
2431f46b5a66SChristoph Hellwig 		/*
2432f46b5a66SChristoph Hellwig 		 * note the key will change type as we walk through the
2433f46b5a66SChristoph Hellwig 		 * tree.
2434f46b5a66SChristoph Hellwig 		 */
2435a22285a6SYan, Zheng 		ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2436f46b5a66SChristoph Hellwig 		if (ret < 0)
2437f46b5a66SChristoph Hellwig 			goto out;
2438f46b5a66SChristoph Hellwig 
2439ae01a0abSYan Zheng 		nritems = btrfs_header_nritems(path->nodes[0]);
2440ae01a0abSYan Zheng 		if (path->slots[0] >= nritems) {
2441f46b5a66SChristoph Hellwig 			ret = btrfs_next_leaf(root, path);
2442f46b5a66SChristoph Hellwig 			if (ret < 0)
2443f46b5a66SChristoph Hellwig 				goto out;
2444f46b5a66SChristoph Hellwig 			if (ret > 0)
2445f46b5a66SChristoph Hellwig 				break;
2446ae01a0abSYan Zheng 			nritems = btrfs_header_nritems(path->nodes[0]);
2447f46b5a66SChristoph Hellwig 		}
2448f46b5a66SChristoph Hellwig 		leaf = path->nodes[0];
2449f46b5a66SChristoph Hellwig 		slot = path->slots[0];
2450f46b5a66SChristoph Hellwig 
2451ae01a0abSYan Zheng 		btrfs_item_key_to_cpu(leaf, &key, slot);
2452d20f7043SChris Mason 		if (btrfs_key_type(&key) > BTRFS_EXTENT_DATA_KEY ||
245333345d01SLi Zefan 		    key.objectid != btrfs_ino(src))
2454f46b5a66SChristoph Hellwig 			break;
2455f46b5a66SChristoph Hellwig 
2456c5c9cd4dSSage Weil 		if (btrfs_key_type(&key) == BTRFS_EXTENT_DATA_KEY) {
2457c5c9cd4dSSage Weil 			struct btrfs_file_extent_item *extent;
2458c5c9cd4dSSage Weil 			int type;
245931840ae1SZheng Yan 			u32 size;
246031840ae1SZheng Yan 			struct btrfs_key new_key;
2461c5c9cd4dSSage Weil 			u64 disko = 0, diskl = 0;
2462c5c9cd4dSSage Weil 			u64 datao = 0, datal = 0;
2463c5c9cd4dSSage Weil 			u8 comp;
2464b5384d48SSage Weil 			u64 endoff;
246531840ae1SZheng Yan 
246631840ae1SZheng Yan 			size = btrfs_item_size_nr(leaf, slot);
246731840ae1SZheng Yan 			read_extent_buffer(leaf, buf,
246831840ae1SZheng Yan 					   btrfs_item_ptr_offset(leaf, slot),
246931840ae1SZheng Yan 					   size);
2470c5c9cd4dSSage Weil 
2471c5c9cd4dSSage Weil 			extent = btrfs_item_ptr(leaf, slot,
2472c5c9cd4dSSage Weil 						struct btrfs_file_extent_item);
2473c5c9cd4dSSage Weil 			comp = btrfs_file_extent_compression(leaf, extent);
2474c5c9cd4dSSage Weil 			type = btrfs_file_extent_type(leaf, extent);
2475c8a894d7SChris Mason 			if (type == BTRFS_FILE_EXTENT_REG ||
2476c8a894d7SChris Mason 			    type == BTRFS_FILE_EXTENT_PREALLOC) {
2477d397712bSChris Mason 				disko = btrfs_file_extent_disk_bytenr(leaf,
2478d397712bSChris Mason 								      extent);
2479d397712bSChris Mason 				diskl = btrfs_file_extent_disk_num_bytes(leaf,
2480d397712bSChris Mason 								 extent);
2481c5c9cd4dSSage Weil 				datao = btrfs_file_extent_offset(leaf, extent);
2482d397712bSChris Mason 				datal = btrfs_file_extent_num_bytes(leaf,
2483d397712bSChris Mason 								    extent);
2484c5c9cd4dSSage Weil 			} else if (type == BTRFS_FILE_EXTENT_INLINE) {
2485c5c9cd4dSSage Weil 				/* take upper bound, may be compressed */
2486c5c9cd4dSSage Weil 				datal = btrfs_file_extent_ram_bytes(leaf,
2487c5c9cd4dSSage Weil 								    extent);
2488c5c9cd4dSSage Weil 			}
2489b3b4aa74SDavid Sterba 			btrfs_release_path(path);
249031840ae1SZheng Yan 
2491050006a7SSage Weil 			if (key.offset + datal <= off ||
2492c5c9cd4dSSage Weil 			    key.offset >= off+len)
2493c5c9cd4dSSage Weil 				goto next;
2494c5c9cd4dSSage Weil 
249531840ae1SZheng Yan 			memcpy(&new_key, &key, sizeof(new_key));
249633345d01SLi Zefan 			new_key.objectid = btrfs_ino(inode);
24974d728ec7SLi Zefan 			if (off <= key.offset)
2498c5c9cd4dSSage Weil 				new_key.offset = key.offset + destoff - off;
24994d728ec7SLi Zefan 			else
25004d728ec7SLi Zefan 				new_key.offset = destoff;
2501c5c9cd4dSSage Weil 
2502b6f3409bSSage Weil 			/*
2503b6f3409bSSage Weil 			 * 1 - adjusting old extent (we may have to split it)
2504b6f3409bSSage Weil 			 * 1 - add new extent
2505b6f3409bSSage Weil 			 * 1 - inode update
2506b6f3409bSSage Weil 			 */
2507b6f3409bSSage Weil 			trans = btrfs_start_transaction(root, 3);
2508a22285a6SYan, Zheng 			if (IS_ERR(trans)) {
2509a22285a6SYan, Zheng 				ret = PTR_ERR(trans);
2510a22285a6SYan, Zheng 				goto out;
2511a22285a6SYan, Zheng 			}
2512a22285a6SYan, Zheng 
2513c8a894d7SChris Mason 			if (type == BTRFS_FILE_EXTENT_REG ||
2514c8a894d7SChris Mason 			    type == BTRFS_FILE_EXTENT_PREALLOC) {
2515d72c0842SLi Zefan 				/*
2516d72c0842SLi Zefan 				 *    a  | --- range to clone ---|  b
2517d72c0842SLi Zefan 				 * | ------------- extent ------------- |
2518d72c0842SLi Zefan 				 */
2519d72c0842SLi Zefan 
2520d72c0842SLi Zefan 				/* substract range b */
2521d72c0842SLi Zefan 				if (key.offset + datal > off + len)
2522d72c0842SLi Zefan 					datal = off + len - key.offset;
2523d72c0842SLi Zefan 
2524d72c0842SLi Zefan 				/* substract range a */
2525a22285a6SYan, Zheng 				if (off > key.offset) {
2526a22285a6SYan, Zheng 					datao += off - key.offset;
2527a22285a6SYan, Zheng 					datal -= off - key.offset;
2528a22285a6SYan, Zheng 				}
2529a22285a6SYan, Zheng 
2530a22285a6SYan, Zheng 				ret = btrfs_drop_extents(trans, inode,
2531a22285a6SYan, Zheng 							 new_key.offset,
2532a22285a6SYan, Zheng 							 new_key.offset + datal,
2533a22285a6SYan, Zheng 							 &hint_byte, 1);
253479787eaaSJeff Mahoney 				if (ret) {
253579787eaaSJeff Mahoney 					btrfs_abort_transaction(trans, root,
253679787eaaSJeff Mahoney 								ret);
253779787eaaSJeff Mahoney 					btrfs_end_transaction(trans, root);
253879787eaaSJeff Mahoney 					goto out;
253979787eaaSJeff Mahoney 				}
2540a22285a6SYan, Zheng 
254131840ae1SZheng Yan 				ret = btrfs_insert_empty_item(trans, root, path,
254231840ae1SZheng Yan 							      &new_key, size);
254379787eaaSJeff Mahoney 				if (ret) {
254479787eaaSJeff Mahoney 					btrfs_abort_transaction(trans, root,
254579787eaaSJeff Mahoney 								ret);
254679787eaaSJeff Mahoney 					btrfs_end_transaction(trans, root);
254779787eaaSJeff Mahoney 					goto out;
254879787eaaSJeff Mahoney 				}
254931840ae1SZheng Yan 
255031840ae1SZheng Yan 				leaf = path->nodes[0];
255131840ae1SZheng Yan 				slot = path->slots[0];
255231840ae1SZheng Yan 				write_extent_buffer(leaf, buf,
255331840ae1SZheng Yan 					    btrfs_item_ptr_offset(leaf, slot),
255431840ae1SZheng Yan 					    size);
2555ae01a0abSYan Zheng 
2556f46b5a66SChristoph Hellwig 				extent = btrfs_item_ptr(leaf, slot,
2557f46b5a66SChristoph Hellwig 						struct btrfs_file_extent_item);
2558c5c9cd4dSSage Weil 
2559c5c9cd4dSSage Weil 				/* disko == 0 means it's a hole */
2560c5c9cd4dSSage Weil 				if (!disko)
2561c5c9cd4dSSage Weil 					datao = 0;
2562c5c9cd4dSSage Weil 
2563c5c9cd4dSSage Weil 				btrfs_set_file_extent_offset(leaf, extent,
2564c5c9cd4dSSage Weil 							     datao);
2565c5c9cd4dSSage Weil 				btrfs_set_file_extent_num_bytes(leaf, extent,
2566c5c9cd4dSSage Weil 								datal);
2567c5c9cd4dSSage Weil 				if (disko) {
2568c5c9cd4dSSage Weil 					inode_add_bytes(inode, datal);
2569ae01a0abSYan Zheng 					ret = btrfs_inc_extent_ref(trans, root,
25705d4f98a2SYan Zheng 							disko, diskl, 0,
2571f46b5a66SChristoph Hellwig 							root->root_key.objectid,
257233345d01SLi Zefan 							btrfs_ino(inode),
257366d7e7f0SArne Jansen 							new_key.offset - datao,
257466d7e7f0SArne Jansen 							0);
257579787eaaSJeff Mahoney 					if (ret) {
257679787eaaSJeff Mahoney 						btrfs_abort_transaction(trans,
257779787eaaSJeff Mahoney 									root,
257879787eaaSJeff Mahoney 									ret);
257979787eaaSJeff Mahoney 						btrfs_end_transaction(trans,
258079787eaaSJeff Mahoney 								      root);
258179787eaaSJeff Mahoney 						goto out;
258279787eaaSJeff Mahoney 
258379787eaaSJeff Mahoney 					}
2584f46b5a66SChristoph Hellwig 				}
2585c5c9cd4dSSage Weil 			} else if (type == BTRFS_FILE_EXTENT_INLINE) {
2586c5c9cd4dSSage Weil 				u64 skip = 0;
2587c5c9cd4dSSage Weil 				u64 trim = 0;
2588c5c9cd4dSSage Weil 				if (off > key.offset) {
2589c5c9cd4dSSage Weil 					skip = off - key.offset;
2590c5c9cd4dSSage Weil 					new_key.offset += skip;
259131840ae1SZheng Yan 				}
2592d397712bSChris Mason 
2593c5c9cd4dSSage Weil 				if (key.offset + datal > off+len)
2594c5c9cd4dSSage Weil 					trim = key.offset + datal - (off+len);
2595d397712bSChris Mason 
2596c5c9cd4dSSage Weil 				if (comp && (skip || trim)) {
2597c5c9cd4dSSage Weil 					ret = -EINVAL;
2598a22285a6SYan, Zheng 					btrfs_end_transaction(trans, root);
2599c5c9cd4dSSage Weil 					goto out;
260031840ae1SZheng Yan 				}
2601c5c9cd4dSSage Weil 				size -= skip + trim;
2602c5c9cd4dSSage Weil 				datal -= skip + trim;
2603a22285a6SYan, Zheng 
2604a22285a6SYan, Zheng 				ret = btrfs_drop_extents(trans, inode,
2605a22285a6SYan, Zheng 							 new_key.offset,
2606a22285a6SYan, Zheng 							 new_key.offset + datal,
2607a22285a6SYan, Zheng 							 &hint_byte, 1);
260879787eaaSJeff Mahoney 				if (ret) {
260979787eaaSJeff Mahoney 					btrfs_abort_transaction(trans, root,
261079787eaaSJeff Mahoney 								ret);
261179787eaaSJeff Mahoney 					btrfs_end_transaction(trans, root);
261279787eaaSJeff Mahoney 					goto out;
261379787eaaSJeff Mahoney 				}
2614a22285a6SYan, Zheng 
2615c5c9cd4dSSage Weil 				ret = btrfs_insert_empty_item(trans, root, path,
2616c5c9cd4dSSage Weil 							      &new_key, size);
261779787eaaSJeff Mahoney 				if (ret) {
261879787eaaSJeff Mahoney 					btrfs_abort_transaction(trans, root,
261979787eaaSJeff Mahoney 								ret);
262079787eaaSJeff Mahoney 					btrfs_end_transaction(trans, root);
262179787eaaSJeff Mahoney 					goto out;
262279787eaaSJeff Mahoney 				}
2623c5c9cd4dSSage Weil 
2624c5c9cd4dSSage Weil 				if (skip) {
2625d397712bSChris Mason 					u32 start =
2626d397712bSChris Mason 					  btrfs_file_extent_calc_inline_size(0);
2627c5c9cd4dSSage Weil 					memmove(buf+start, buf+start+skip,
2628c5c9cd4dSSage Weil 						datal);
2629c5c9cd4dSSage Weil 				}
2630c5c9cd4dSSage Weil 
2631c5c9cd4dSSage Weil 				leaf = path->nodes[0];
2632c5c9cd4dSSage Weil 				slot = path->slots[0];
2633c5c9cd4dSSage Weil 				write_extent_buffer(leaf, buf,
2634c5c9cd4dSSage Weil 					    btrfs_item_ptr_offset(leaf, slot),
2635c5c9cd4dSSage Weil 					    size);
2636c5c9cd4dSSage Weil 				inode_add_bytes(inode, datal);
2637c5c9cd4dSSage Weil 			}
2638c5c9cd4dSSage Weil 
2639c5c9cd4dSSage Weil 			btrfs_mark_buffer_dirty(leaf);
2640b3b4aa74SDavid Sterba 			btrfs_release_path(path);
2641c5c9cd4dSSage Weil 
26420c4d2d95SJosef Bacik 			inode_inc_iversion(inode);
2643a22285a6SYan, Zheng 			inode->i_mtime = inode->i_ctime = CURRENT_TIME;
2644b5384d48SSage Weil 
2645b5384d48SSage Weil 			/*
2646b5384d48SSage Weil 			 * we round up to the block size at eof when
2647b5384d48SSage Weil 			 * determining which extents to clone above,
2648b5384d48SSage Weil 			 * but shouldn't round up the file size
2649b5384d48SSage Weil 			 */
2650b5384d48SSage Weil 			endoff = new_key.offset + datal;
26515f3888ffSLi Zefan 			if (endoff > destoff+olen)
26525f3888ffSLi Zefan 				endoff = destoff+olen;
2653b5384d48SSage Weil 			if (endoff > inode->i_size)
2654b5384d48SSage Weil 				btrfs_i_size_write(inode, endoff);
2655b5384d48SSage Weil 
2656a22285a6SYan, Zheng 			ret = btrfs_update_inode(trans, root, inode);
265779787eaaSJeff Mahoney 			if (ret) {
265879787eaaSJeff Mahoney 				btrfs_abort_transaction(trans, root, ret);
2659a22285a6SYan, Zheng 				btrfs_end_transaction(trans, root);
266079787eaaSJeff Mahoney 				goto out;
266179787eaaSJeff Mahoney 			}
266279787eaaSJeff Mahoney 			ret = btrfs_end_transaction(trans, root);
2663a22285a6SYan, Zheng 		}
2664c5c9cd4dSSage Weil next:
2665b3b4aa74SDavid Sterba 		btrfs_release_path(path);
2666ae01a0abSYan Zheng 		key.offset++;
2667ae01a0abSYan Zheng 	}
2668f46b5a66SChristoph Hellwig 	ret = 0;
2669f46b5a66SChristoph Hellwig out:
2670b3b4aa74SDavid Sterba 	btrfs_release_path(path);
2671d0082371SJeff Mahoney 	unlock_extent(&BTRFS_I(src)->io_tree, off, off+len);
2672f46b5a66SChristoph Hellwig out_unlock:
2673f46b5a66SChristoph Hellwig 	mutex_unlock(&src->i_mutex);
2674f46b5a66SChristoph Hellwig 	mutex_unlock(&inode->i_mutex);
2675ae01a0abSYan Zheng 	vfree(buf);
2676ae01a0abSYan Zheng 	btrfs_free_path(path);
2677f46b5a66SChristoph Hellwig out_fput:
2678f46b5a66SChristoph Hellwig 	fput(src_file);
2679ab67b7c1SYan Zheng out_drop_write:
26802a79f17eSAl Viro 	mnt_drop_write_file(file);
2681f46b5a66SChristoph Hellwig 	return ret;
2682f46b5a66SChristoph Hellwig }
2683f46b5a66SChristoph Hellwig 
26847a865e8aSChristoph Hellwig static long btrfs_ioctl_clone_range(struct file *file, void __user *argp)
2685c5c9cd4dSSage Weil {
2686c5c9cd4dSSage Weil 	struct btrfs_ioctl_clone_range_args args;
2687c5c9cd4dSSage Weil 
26887a865e8aSChristoph Hellwig 	if (copy_from_user(&args, argp, sizeof(args)))
2689c5c9cd4dSSage Weil 		return -EFAULT;
2690c5c9cd4dSSage Weil 	return btrfs_ioctl_clone(file, args.src_fd, args.src_offset,
2691c5c9cd4dSSage Weil 				 args.src_length, args.dest_offset);
2692c5c9cd4dSSage Weil }
2693c5c9cd4dSSage Weil 
2694f46b5a66SChristoph Hellwig /*
2695f46b5a66SChristoph Hellwig  * there are many ways the trans_start and trans_end ioctls can lead
2696f46b5a66SChristoph Hellwig  * to deadlocks.  They should only be used by applications that
2697f46b5a66SChristoph Hellwig  * basically own the machine, and have a very in depth understanding
2698f46b5a66SChristoph Hellwig  * of all the possible deadlocks and enospc problems.
2699f46b5a66SChristoph Hellwig  */
2700b2950863SChristoph Hellwig static long btrfs_ioctl_trans_start(struct file *file)
2701f46b5a66SChristoph Hellwig {
2702f46b5a66SChristoph Hellwig 	struct inode *inode = fdentry(file)->d_inode;
2703f46b5a66SChristoph Hellwig 	struct btrfs_root *root = BTRFS_I(inode)->root;
2704f46b5a66SChristoph Hellwig 	struct btrfs_trans_handle *trans;
27051ab86aedSSage Weil 	int ret;
2706f46b5a66SChristoph Hellwig 
27071ab86aedSSage Weil 	ret = -EPERM;
2708df5b5520SChristoph Hellwig 	if (!capable(CAP_SYS_ADMIN))
2709f46b5a66SChristoph Hellwig 		goto out;
27101ab86aedSSage Weil 
27111ab86aedSSage Weil 	ret = -EINPROGRESS;
27121ab86aedSSage Weil 	if (file->private_data)
27131ab86aedSSage Weil 		goto out;
27149ca9ee09SSage Weil 
2715b83cc969SLi Zefan 	ret = -EROFS;
2716b83cc969SLi Zefan 	if (btrfs_root_readonly(root))
2717b83cc969SLi Zefan 		goto out;
2718b83cc969SLi Zefan 
2719a561be71SAl Viro 	ret = mnt_want_write_file(file);
2720c146afadSYan Zheng 	if (ret)
2721c146afadSYan Zheng 		goto out;
2722c146afadSYan Zheng 
2723a4abeea4SJosef Bacik 	atomic_inc(&root->fs_info->open_ioctl_trans);
27249ca9ee09SSage Weil 
2725f46b5a66SChristoph Hellwig 	ret = -ENOMEM;
27267a7eaa40SJosef Bacik 	trans = btrfs_start_ioctl_transaction(root);
2727abd30bb0STsutomu Itoh 	if (IS_ERR(trans))
27281ab86aedSSage Weil 		goto out_drop;
27291ab86aedSSage Weil 
27301ab86aedSSage Weil 	file->private_data = trans;
27311ab86aedSSage Weil 	return 0;
27321ab86aedSSage Weil 
27331ab86aedSSage Weil out_drop:
2734a4abeea4SJosef Bacik 	atomic_dec(&root->fs_info->open_ioctl_trans);
27352a79f17eSAl Viro 	mnt_drop_write_file(file);
2736f46b5a66SChristoph Hellwig out:
2737f46b5a66SChristoph Hellwig 	return ret;
2738f46b5a66SChristoph Hellwig }
2739f46b5a66SChristoph Hellwig 
27406ef5ed0dSJosef Bacik static long btrfs_ioctl_default_subvol(struct file *file, void __user *argp)
27416ef5ed0dSJosef Bacik {
27426ef5ed0dSJosef Bacik 	struct inode *inode = fdentry(file)->d_inode;
27436ef5ed0dSJosef Bacik 	struct btrfs_root *root = BTRFS_I(inode)->root;
27446ef5ed0dSJosef Bacik 	struct btrfs_root *new_root;
27456ef5ed0dSJosef Bacik 	struct btrfs_dir_item *di;
27466ef5ed0dSJosef Bacik 	struct btrfs_trans_handle *trans;
27476ef5ed0dSJosef Bacik 	struct btrfs_path *path;
27486ef5ed0dSJosef Bacik 	struct btrfs_key location;
27496ef5ed0dSJosef Bacik 	struct btrfs_disk_key disk_key;
27506ef5ed0dSJosef Bacik 	struct btrfs_super_block *disk_super;
27516ef5ed0dSJosef Bacik 	u64 features;
27526ef5ed0dSJosef Bacik 	u64 objectid = 0;
27536ef5ed0dSJosef Bacik 	u64 dir_id;
27546ef5ed0dSJosef Bacik 
27556ef5ed0dSJosef Bacik 	if (!capable(CAP_SYS_ADMIN))
27566ef5ed0dSJosef Bacik 		return -EPERM;
27576ef5ed0dSJosef Bacik 
27586ef5ed0dSJosef Bacik 	if (copy_from_user(&objectid, argp, sizeof(objectid)))
27596ef5ed0dSJosef Bacik 		return -EFAULT;
27606ef5ed0dSJosef Bacik 
27616ef5ed0dSJosef Bacik 	if (!objectid)
27626ef5ed0dSJosef Bacik 		objectid = root->root_key.objectid;
27636ef5ed0dSJosef Bacik 
27646ef5ed0dSJosef Bacik 	location.objectid = objectid;
27656ef5ed0dSJosef Bacik 	location.type = BTRFS_ROOT_ITEM_KEY;
27666ef5ed0dSJosef Bacik 	location.offset = (u64)-1;
27676ef5ed0dSJosef Bacik 
27686ef5ed0dSJosef Bacik 	new_root = btrfs_read_fs_root_no_name(root->fs_info, &location);
27696ef5ed0dSJosef Bacik 	if (IS_ERR(new_root))
27706ef5ed0dSJosef Bacik 		return PTR_ERR(new_root);
27716ef5ed0dSJosef Bacik 
27726ef5ed0dSJosef Bacik 	if (btrfs_root_refs(&new_root->root_item) == 0)
27736ef5ed0dSJosef Bacik 		return -ENOENT;
27746ef5ed0dSJosef Bacik 
27756ef5ed0dSJosef Bacik 	path = btrfs_alloc_path();
27766ef5ed0dSJosef Bacik 	if (!path)
27776ef5ed0dSJosef Bacik 		return -ENOMEM;
27786ef5ed0dSJosef Bacik 	path->leave_spinning = 1;
27796ef5ed0dSJosef Bacik 
27806ef5ed0dSJosef Bacik 	trans = btrfs_start_transaction(root, 1);
278198d5dc13STsutomu Itoh 	if (IS_ERR(trans)) {
27826ef5ed0dSJosef Bacik 		btrfs_free_path(path);
278398d5dc13STsutomu Itoh 		return PTR_ERR(trans);
27846ef5ed0dSJosef Bacik 	}
27856ef5ed0dSJosef Bacik 
27866c41761fSDavid Sterba 	dir_id = btrfs_super_root_dir(root->fs_info->super_copy);
27876ef5ed0dSJosef Bacik 	di = btrfs_lookup_dir_item(trans, root->fs_info->tree_root, path,
27886ef5ed0dSJosef Bacik 				   dir_id, "default", 7, 1);
2789cf1e99a4SDan Carpenter 	if (IS_ERR_OR_NULL(di)) {
27906ef5ed0dSJosef Bacik 		btrfs_free_path(path);
27916ef5ed0dSJosef Bacik 		btrfs_end_transaction(trans, root);
27926ef5ed0dSJosef Bacik 		printk(KERN_ERR "Umm, you don't have the default dir item, "
27936ef5ed0dSJosef Bacik 		       "this isn't going to work\n");
27946ef5ed0dSJosef Bacik 		return -ENOENT;
27956ef5ed0dSJosef Bacik 	}
27966ef5ed0dSJosef Bacik 
27976ef5ed0dSJosef Bacik 	btrfs_cpu_key_to_disk(&disk_key, &new_root->root_key);
27986ef5ed0dSJosef Bacik 	btrfs_set_dir_item_key(path->nodes[0], di, &disk_key);
27996ef5ed0dSJosef Bacik 	btrfs_mark_buffer_dirty(path->nodes[0]);
28006ef5ed0dSJosef Bacik 	btrfs_free_path(path);
28016ef5ed0dSJosef Bacik 
28026c41761fSDavid Sterba 	disk_super = root->fs_info->super_copy;
28036ef5ed0dSJosef Bacik 	features = btrfs_super_incompat_flags(disk_super);
28046ef5ed0dSJosef Bacik 	if (!(features & BTRFS_FEATURE_INCOMPAT_DEFAULT_SUBVOL)) {
28056ef5ed0dSJosef Bacik 		features |= BTRFS_FEATURE_INCOMPAT_DEFAULT_SUBVOL;
28066ef5ed0dSJosef Bacik 		btrfs_set_super_incompat_flags(disk_super, features);
28076ef5ed0dSJosef Bacik 	}
28086ef5ed0dSJosef Bacik 	btrfs_end_transaction(trans, root);
28096ef5ed0dSJosef Bacik 
28106ef5ed0dSJosef Bacik 	return 0;
28116ef5ed0dSJosef Bacik }
28126ef5ed0dSJosef Bacik 
2813bf5fc093SJosef Bacik static void get_block_group_info(struct list_head *groups_list,
2814bf5fc093SJosef Bacik 				 struct btrfs_ioctl_space_info *space)
2815bf5fc093SJosef Bacik {
2816bf5fc093SJosef Bacik 	struct btrfs_block_group_cache *block_group;
2817bf5fc093SJosef Bacik 
2818bf5fc093SJosef Bacik 	space->total_bytes = 0;
2819bf5fc093SJosef Bacik 	space->used_bytes = 0;
2820bf5fc093SJosef Bacik 	space->flags = 0;
2821bf5fc093SJosef Bacik 	list_for_each_entry(block_group, groups_list, list) {
2822bf5fc093SJosef Bacik 		space->flags = block_group->flags;
2823bf5fc093SJosef Bacik 		space->total_bytes += block_group->key.offset;
2824bf5fc093SJosef Bacik 		space->used_bytes +=
2825bf5fc093SJosef Bacik 			btrfs_block_group_used(&block_group->item);
2826bf5fc093SJosef Bacik 	}
2827bf5fc093SJosef Bacik }
2828bf5fc093SJosef Bacik 
28291406e432SJosef Bacik long btrfs_ioctl_space_info(struct btrfs_root *root, void __user *arg)
28301406e432SJosef Bacik {
28311406e432SJosef Bacik 	struct btrfs_ioctl_space_args space_args;
28321406e432SJosef Bacik 	struct btrfs_ioctl_space_info space;
28331406e432SJosef Bacik 	struct btrfs_ioctl_space_info *dest;
28347fde62bfSChris Mason 	struct btrfs_ioctl_space_info *dest_orig;
283513f2696fSDaniel J Blueman 	struct btrfs_ioctl_space_info __user *user_dest;
28361406e432SJosef Bacik 	struct btrfs_space_info *info;
2837bf5fc093SJosef Bacik 	u64 types[] = {BTRFS_BLOCK_GROUP_DATA,
2838bf5fc093SJosef Bacik 		       BTRFS_BLOCK_GROUP_SYSTEM,
2839bf5fc093SJosef Bacik 		       BTRFS_BLOCK_GROUP_METADATA,
2840bf5fc093SJosef Bacik 		       BTRFS_BLOCK_GROUP_DATA | BTRFS_BLOCK_GROUP_METADATA};
2841bf5fc093SJosef Bacik 	int num_types = 4;
28427fde62bfSChris Mason 	int alloc_size;
28431406e432SJosef Bacik 	int ret = 0;
284451788b1bSDan Rosenberg 	u64 slot_count = 0;
2845bf5fc093SJosef Bacik 	int i, c;
28461406e432SJosef Bacik 
28471406e432SJosef Bacik 	if (copy_from_user(&space_args,
28481406e432SJosef Bacik 			   (struct btrfs_ioctl_space_args __user *)arg,
28491406e432SJosef Bacik 			   sizeof(space_args)))
28501406e432SJosef Bacik 		return -EFAULT;
28511406e432SJosef Bacik 
2852bf5fc093SJosef Bacik 	for (i = 0; i < num_types; i++) {
2853bf5fc093SJosef Bacik 		struct btrfs_space_info *tmp;
2854bf5fc093SJosef Bacik 
2855bf5fc093SJosef Bacik 		info = NULL;
28567fde62bfSChris Mason 		rcu_read_lock();
2857bf5fc093SJosef Bacik 		list_for_each_entry_rcu(tmp, &root->fs_info->space_info,
2858bf5fc093SJosef Bacik 					list) {
2859bf5fc093SJosef Bacik 			if (tmp->flags == types[i]) {
2860bf5fc093SJosef Bacik 				info = tmp;
2861bf5fc093SJosef Bacik 				break;
2862bf5fc093SJosef Bacik 			}
2863bf5fc093SJosef Bacik 		}
28647fde62bfSChris Mason 		rcu_read_unlock();
28651406e432SJosef Bacik 
2866bf5fc093SJosef Bacik 		if (!info)
2867bf5fc093SJosef Bacik 			continue;
2868bf5fc093SJosef Bacik 
2869bf5fc093SJosef Bacik 		down_read(&info->groups_sem);
2870bf5fc093SJosef Bacik 		for (c = 0; c < BTRFS_NR_RAID_TYPES; c++) {
2871bf5fc093SJosef Bacik 			if (!list_empty(&info->block_groups[c]))
2872bf5fc093SJosef Bacik 				slot_count++;
2873bf5fc093SJosef Bacik 		}
2874bf5fc093SJosef Bacik 		up_read(&info->groups_sem);
2875bf5fc093SJosef Bacik 	}
2876bf5fc093SJosef Bacik 
28777fde62bfSChris Mason 	/* space_slots == 0 means they are asking for a count */
28787fde62bfSChris Mason 	if (space_args.space_slots == 0) {
28797fde62bfSChris Mason 		space_args.total_spaces = slot_count;
28807fde62bfSChris Mason 		goto out;
28817fde62bfSChris Mason 	}
2882bf5fc093SJosef Bacik 
288351788b1bSDan Rosenberg 	slot_count = min_t(u64, space_args.space_slots, slot_count);
2884bf5fc093SJosef Bacik 
28857fde62bfSChris Mason 	alloc_size = sizeof(*dest) * slot_count;
2886bf5fc093SJosef Bacik 
28877fde62bfSChris Mason 	/* we generally have at most 6 or so space infos, one for each raid
28887fde62bfSChris Mason 	 * level.  So, a whole page should be more than enough for everyone
28897fde62bfSChris Mason 	 */
28907fde62bfSChris Mason 	if (alloc_size > PAGE_CACHE_SIZE)
28917fde62bfSChris Mason 		return -ENOMEM;
28927fde62bfSChris Mason 
28937fde62bfSChris Mason 	space_args.total_spaces = 0;
28947fde62bfSChris Mason 	dest = kmalloc(alloc_size, GFP_NOFS);
28957fde62bfSChris Mason 	if (!dest)
28967fde62bfSChris Mason 		return -ENOMEM;
28977fde62bfSChris Mason 	dest_orig = dest;
28987fde62bfSChris Mason 
28997fde62bfSChris Mason 	/* now we have a buffer to copy into */
2900bf5fc093SJosef Bacik 	for (i = 0; i < num_types; i++) {
2901bf5fc093SJosef Bacik 		struct btrfs_space_info *tmp;
2902bf5fc093SJosef Bacik 
290351788b1bSDan Rosenberg 		if (!slot_count)
290451788b1bSDan Rosenberg 			break;
290551788b1bSDan Rosenberg 
2906bf5fc093SJosef Bacik 		info = NULL;
29071406e432SJosef Bacik 		rcu_read_lock();
2908bf5fc093SJosef Bacik 		list_for_each_entry_rcu(tmp, &root->fs_info->space_info,
2909bf5fc093SJosef Bacik 					list) {
2910bf5fc093SJosef Bacik 			if (tmp->flags == types[i]) {
2911bf5fc093SJosef Bacik 				info = tmp;
29127fde62bfSChris Mason 				break;
2913bf5fc093SJosef Bacik 			}
2914bf5fc093SJosef Bacik 		}
2915bf5fc093SJosef Bacik 		rcu_read_unlock();
29167fde62bfSChris Mason 
2917bf5fc093SJosef Bacik 		if (!info)
2918bf5fc093SJosef Bacik 			continue;
2919bf5fc093SJosef Bacik 		down_read(&info->groups_sem);
2920bf5fc093SJosef Bacik 		for (c = 0; c < BTRFS_NR_RAID_TYPES; c++) {
2921bf5fc093SJosef Bacik 			if (!list_empty(&info->block_groups[c])) {
2922bf5fc093SJosef Bacik 				get_block_group_info(&info->block_groups[c],
2923bf5fc093SJosef Bacik 						     &space);
29247fde62bfSChris Mason 				memcpy(dest, &space, sizeof(space));
29251406e432SJosef Bacik 				dest++;
29261406e432SJosef Bacik 				space_args.total_spaces++;
292751788b1bSDan Rosenberg 				slot_count--;
29281406e432SJosef Bacik 			}
292951788b1bSDan Rosenberg 			if (!slot_count)
293051788b1bSDan Rosenberg 				break;
2931bf5fc093SJosef Bacik 		}
2932bf5fc093SJosef Bacik 		up_read(&info->groups_sem);
2933bf5fc093SJosef Bacik 	}
29341406e432SJosef Bacik 
29352eec6c81SDaniel J Blueman 	user_dest = (struct btrfs_ioctl_space_info __user *)
29367fde62bfSChris Mason 		(arg + sizeof(struct btrfs_ioctl_space_args));
29377fde62bfSChris Mason 
29387fde62bfSChris Mason 	if (copy_to_user(user_dest, dest_orig, alloc_size))
29397fde62bfSChris Mason 		ret = -EFAULT;
29407fde62bfSChris Mason 
29417fde62bfSChris Mason 	kfree(dest_orig);
29427fde62bfSChris Mason out:
29437fde62bfSChris Mason 	if (ret == 0 && copy_to_user(arg, &space_args, sizeof(space_args)))
29441406e432SJosef Bacik 		ret = -EFAULT;
29451406e432SJosef Bacik 
29461406e432SJosef Bacik 	return ret;
29471406e432SJosef Bacik }
29481406e432SJosef Bacik 
2949f46b5a66SChristoph Hellwig /*
2950f46b5a66SChristoph Hellwig  * there are many ways the trans_start and trans_end ioctls can lead
2951f46b5a66SChristoph Hellwig  * to deadlocks.  They should only be used by applications that
2952f46b5a66SChristoph Hellwig  * basically own the machine, and have a very in depth understanding
2953f46b5a66SChristoph Hellwig  * of all the possible deadlocks and enospc problems.
2954f46b5a66SChristoph Hellwig  */
2955f46b5a66SChristoph Hellwig long btrfs_ioctl_trans_end(struct file *file)
2956f46b5a66SChristoph Hellwig {
2957f46b5a66SChristoph Hellwig 	struct inode *inode = fdentry(file)->d_inode;
2958f46b5a66SChristoph Hellwig 	struct btrfs_root *root = BTRFS_I(inode)->root;
2959f46b5a66SChristoph Hellwig 	struct btrfs_trans_handle *trans;
2960f46b5a66SChristoph Hellwig 
2961f46b5a66SChristoph Hellwig 	trans = file->private_data;
29621ab86aedSSage Weil 	if (!trans)
29631ab86aedSSage Weil 		return -EINVAL;
2964b214107eSChristoph Hellwig 	file->private_data = NULL;
29659ca9ee09SSage Weil 
29661ab86aedSSage Weil 	btrfs_end_transaction(trans, root);
29671ab86aedSSage Weil 
2968a4abeea4SJosef Bacik 	atomic_dec(&root->fs_info->open_ioctl_trans);
29699ca9ee09SSage Weil 
29702a79f17eSAl Viro 	mnt_drop_write_file(file);
29711ab86aedSSage Weil 	return 0;
2972f46b5a66SChristoph Hellwig }
2973f46b5a66SChristoph Hellwig 
297446204592SSage Weil static noinline long btrfs_ioctl_start_sync(struct file *file, void __user *argp)
297546204592SSage Weil {
297646204592SSage Weil 	struct btrfs_root *root = BTRFS_I(file->f_dentry->d_inode)->root;
297746204592SSage Weil 	struct btrfs_trans_handle *trans;
297846204592SSage Weil 	u64 transid;
2979db5b493aSTsutomu Itoh 	int ret;
298046204592SSage Weil 
298146204592SSage Weil 	trans = btrfs_start_transaction(root, 0);
298298d5dc13STsutomu Itoh 	if (IS_ERR(trans))
298398d5dc13STsutomu Itoh 		return PTR_ERR(trans);
298446204592SSage Weil 	transid = trans->transid;
2985db5b493aSTsutomu Itoh 	ret = btrfs_commit_transaction_async(trans, root, 0);
29868b2b2d3cSTsutomu Itoh 	if (ret) {
29878b2b2d3cSTsutomu Itoh 		btrfs_end_transaction(trans, root);
2988db5b493aSTsutomu Itoh 		return ret;
29898b2b2d3cSTsutomu Itoh 	}
299046204592SSage Weil 
299146204592SSage Weil 	if (argp)
299246204592SSage Weil 		if (copy_to_user(argp, &transid, sizeof(transid)))
299346204592SSage Weil 			return -EFAULT;
299446204592SSage Weil 	return 0;
299546204592SSage Weil }
299646204592SSage Weil 
299746204592SSage Weil static noinline long btrfs_ioctl_wait_sync(struct file *file, void __user *argp)
299846204592SSage Weil {
299946204592SSage Weil 	struct btrfs_root *root = BTRFS_I(file->f_dentry->d_inode)->root;
300046204592SSage Weil 	u64 transid;
300146204592SSage Weil 
300246204592SSage Weil 	if (argp) {
300346204592SSage Weil 		if (copy_from_user(&transid, argp, sizeof(transid)))
300446204592SSage Weil 			return -EFAULT;
300546204592SSage Weil 	} else {
300646204592SSage Weil 		transid = 0;  /* current trans */
300746204592SSage Weil 	}
300846204592SSage Weil 	return btrfs_wait_for_commit(root, transid);
300946204592SSage Weil }
301046204592SSage Weil 
3011475f6387SJan Schmidt static long btrfs_ioctl_scrub(struct btrfs_root *root, void __user *arg)
3012475f6387SJan Schmidt {
3013475f6387SJan Schmidt 	int ret;
3014475f6387SJan Schmidt 	struct btrfs_ioctl_scrub_args *sa;
3015475f6387SJan Schmidt 
3016475f6387SJan Schmidt 	if (!capable(CAP_SYS_ADMIN))
3017475f6387SJan Schmidt 		return -EPERM;
3018475f6387SJan Schmidt 
3019475f6387SJan Schmidt 	sa = memdup_user(arg, sizeof(*sa));
3020475f6387SJan Schmidt 	if (IS_ERR(sa))
3021475f6387SJan Schmidt 		return PTR_ERR(sa);
3022475f6387SJan Schmidt 
3023475f6387SJan Schmidt 	ret = btrfs_scrub_dev(root, sa->devid, sa->start, sa->end,
30248628764eSArne Jansen 			      &sa->progress, sa->flags & BTRFS_SCRUB_READONLY);
3025475f6387SJan Schmidt 
3026475f6387SJan Schmidt 	if (copy_to_user(arg, sa, sizeof(*sa)))
3027475f6387SJan Schmidt 		ret = -EFAULT;
3028475f6387SJan Schmidt 
3029475f6387SJan Schmidt 	kfree(sa);
3030475f6387SJan Schmidt 	return ret;
3031475f6387SJan Schmidt }
3032475f6387SJan Schmidt 
3033475f6387SJan Schmidt static long btrfs_ioctl_scrub_cancel(struct btrfs_root *root, void __user *arg)
3034475f6387SJan Schmidt {
3035475f6387SJan Schmidt 	if (!capable(CAP_SYS_ADMIN))
3036475f6387SJan Schmidt 		return -EPERM;
3037475f6387SJan Schmidt 
3038475f6387SJan Schmidt 	return btrfs_scrub_cancel(root);
3039475f6387SJan Schmidt }
3040475f6387SJan Schmidt 
3041475f6387SJan Schmidt static long btrfs_ioctl_scrub_progress(struct btrfs_root *root,
3042475f6387SJan Schmidt 				       void __user *arg)
3043475f6387SJan Schmidt {
3044475f6387SJan Schmidt 	struct btrfs_ioctl_scrub_args *sa;
3045475f6387SJan Schmidt 	int ret;
3046475f6387SJan Schmidt 
3047475f6387SJan Schmidt 	if (!capable(CAP_SYS_ADMIN))
3048475f6387SJan Schmidt 		return -EPERM;
3049475f6387SJan Schmidt 
3050475f6387SJan Schmidt 	sa = memdup_user(arg, sizeof(*sa));
3051475f6387SJan Schmidt 	if (IS_ERR(sa))
3052475f6387SJan Schmidt 		return PTR_ERR(sa);
3053475f6387SJan Schmidt 
3054475f6387SJan Schmidt 	ret = btrfs_scrub_progress(root, sa->devid, &sa->progress);
3055475f6387SJan Schmidt 
3056475f6387SJan Schmidt 	if (copy_to_user(arg, sa, sizeof(*sa)))
3057475f6387SJan Schmidt 		ret = -EFAULT;
3058475f6387SJan Schmidt 
3059475f6387SJan Schmidt 	kfree(sa);
3060475f6387SJan Schmidt 	return ret;
3061475f6387SJan Schmidt }
3062475f6387SJan Schmidt 
3063c11d2c23SStefan Behrens static long btrfs_ioctl_get_dev_stats(struct btrfs_root *root,
3064b27f7c0cSDavid Sterba 				      void __user *arg)
3065c11d2c23SStefan Behrens {
3066c11d2c23SStefan Behrens 	struct btrfs_ioctl_get_dev_stats *sa;
3067c11d2c23SStefan Behrens 	int ret;
3068c11d2c23SStefan Behrens 
3069c11d2c23SStefan Behrens 	sa = memdup_user(arg, sizeof(*sa));
3070c11d2c23SStefan Behrens 	if (IS_ERR(sa))
3071c11d2c23SStefan Behrens 		return PTR_ERR(sa);
3072c11d2c23SStefan Behrens 
3073b27f7c0cSDavid Sterba 	if ((sa->flags & BTRFS_DEV_STATS_RESET) && !capable(CAP_SYS_ADMIN)) {
3074b27f7c0cSDavid Sterba 		kfree(sa);
3075b27f7c0cSDavid Sterba 		return -EPERM;
3076b27f7c0cSDavid Sterba 	}
3077b27f7c0cSDavid Sterba 
3078b27f7c0cSDavid Sterba 	ret = btrfs_get_dev_stats(root, sa);
3079c11d2c23SStefan Behrens 
3080c11d2c23SStefan Behrens 	if (copy_to_user(arg, sa, sizeof(*sa)))
3081c11d2c23SStefan Behrens 		ret = -EFAULT;
3082c11d2c23SStefan Behrens 
3083c11d2c23SStefan Behrens 	kfree(sa);
3084c11d2c23SStefan Behrens 	return ret;
3085c11d2c23SStefan Behrens }
3086c11d2c23SStefan Behrens 
3087d7728c96SJan Schmidt static long btrfs_ioctl_ino_to_path(struct btrfs_root *root, void __user *arg)
3088d7728c96SJan Schmidt {
3089d7728c96SJan Schmidt 	int ret = 0;
3090d7728c96SJan Schmidt 	int i;
3091740c3d22SChris Mason 	u64 rel_ptr;
3092d7728c96SJan Schmidt 	int size;
3093806468f8SChris Mason 	struct btrfs_ioctl_ino_path_args *ipa = NULL;
3094d7728c96SJan Schmidt 	struct inode_fs_paths *ipath = NULL;
3095d7728c96SJan Schmidt 	struct btrfs_path *path;
3096d7728c96SJan Schmidt 
3097d7728c96SJan Schmidt 	if (!capable(CAP_SYS_ADMIN))
3098d7728c96SJan Schmidt 		return -EPERM;
3099d7728c96SJan Schmidt 
3100d7728c96SJan Schmidt 	path = btrfs_alloc_path();
3101d7728c96SJan Schmidt 	if (!path) {
3102d7728c96SJan Schmidt 		ret = -ENOMEM;
3103d7728c96SJan Schmidt 		goto out;
3104d7728c96SJan Schmidt 	}
3105d7728c96SJan Schmidt 
3106d7728c96SJan Schmidt 	ipa = memdup_user(arg, sizeof(*ipa));
3107d7728c96SJan Schmidt 	if (IS_ERR(ipa)) {
3108d7728c96SJan Schmidt 		ret = PTR_ERR(ipa);
3109d7728c96SJan Schmidt 		ipa = NULL;
3110d7728c96SJan Schmidt 		goto out;
3111d7728c96SJan Schmidt 	}
3112d7728c96SJan Schmidt 
3113d7728c96SJan Schmidt 	size = min_t(u32, ipa->size, 4096);
3114d7728c96SJan Schmidt 	ipath = init_ipath(size, root, path);
3115d7728c96SJan Schmidt 	if (IS_ERR(ipath)) {
3116d7728c96SJan Schmidt 		ret = PTR_ERR(ipath);
3117d7728c96SJan Schmidt 		ipath = NULL;
3118d7728c96SJan Schmidt 		goto out;
3119d7728c96SJan Schmidt 	}
3120d7728c96SJan Schmidt 
3121d7728c96SJan Schmidt 	ret = paths_from_inode(ipa->inum, ipath);
3122d7728c96SJan Schmidt 	if (ret < 0)
3123d7728c96SJan Schmidt 		goto out;
3124d7728c96SJan Schmidt 
3125d7728c96SJan Schmidt 	for (i = 0; i < ipath->fspath->elem_cnt; ++i) {
3126745c4d8eSJeff Mahoney 		rel_ptr = ipath->fspath->val[i] -
3127745c4d8eSJeff Mahoney 			  (u64)(unsigned long)ipath->fspath->val;
3128740c3d22SChris Mason 		ipath->fspath->val[i] = rel_ptr;
3129d7728c96SJan Schmidt 	}
3130d7728c96SJan Schmidt 
3131745c4d8eSJeff Mahoney 	ret = copy_to_user((void *)(unsigned long)ipa->fspath,
3132745c4d8eSJeff Mahoney 			   (void *)(unsigned long)ipath->fspath, size);
3133d7728c96SJan Schmidt 	if (ret) {
3134d7728c96SJan Schmidt 		ret = -EFAULT;
3135d7728c96SJan Schmidt 		goto out;
3136d7728c96SJan Schmidt 	}
3137d7728c96SJan Schmidt 
3138d7728c96SJan Schmidt out:
3139d7728c96SJan Schmidt 	btrfs_free_path(path);
3140d7728c96SJan Schmidt 	free_ipath(ipath);
3141d7728c96SJan Schmidt 	kfree(ipa);
3142d7728c96SJan Schmidt 
3143d7728c96SJan Schmidt 	return ret;
3144d7728c96SJan Schmidt }
3145d7728c96SJan Schmidt 
3146d7728c96SJan Schmidt static int build_ino_list(u64 inum, u64 offset, u64 root, void *ctx)
3147d7728c96SJan Schmidt {
3148d7728c96SJan Schmidt 	struct btrfs_data_container *inodes = ctx;
3149d7728c96SJan Schmidt 	const size_t c = 3 * sizeof(u64);
3150d7728c96SJan Schmidt 
3151d7728c96SJan Schmidt 	if (inodes->bytes_left >= c) {
3152d7728c96SJan Schmidt 		inodes->bytes_left -= c;
3153d7728c96SJan Schmidt 		inodes->val[inodes->elem_cnt] = inum;
3154d7728c96SJan Schmidt 		inodes->val[inodes->elem_cnt + 1] = offset;
3155d7728c96SJan Schmidt 		inodes->val[inodes->elem_cnt + 2] = root;
3156d7728c96SJan Schmidt 		inodes->elem_cnt += 3;
3157d7728c96SJan Schmidt 	} else {
3158d7728c96SJan Schmidt 		inodes->bytes_missing += c - inodes->bytes_left;
3159d7728c96SJan Schmidt 		inodes->bytes_left = 0;
3160d7728c96SJan Schmidt 		inodes->elem_missed += 3;
3161d7728c96SJan Schmidt 	}
3162d7728c96SJan Schmidt 
3163d7728c96SJan Schmidt 	return 0;
3164d7728c96SJan Schmidt }
3165d7728c96SJan Schmidt 
3166d7728c96SJan Schmidt static long btrfs_ioctl_logical_to_ino(struct btrfs_root *root,
3167d7728c96SJan Schmidt 					void __user *arg)
3168d7728c96SJan Schmidt {
3169d7728c96SJan Schmidt 	int ret = 0;
3170d7728c96SJan Schmidt 	int size;
31714692cf58SJan Schmidt 	u64 extent_item_pos;
3172d7728c96SJan Schmidt 	struct btrfs_ioctl_logical_ino_args *loi;
3173d7728c96SJan Schmidt 	struct btrfs_data_container *inodes = NULL;
3174d7728c96SJan Schmidt 	struct btrfs_path *path = NULL;
3175d7728c96SJan Schmidt 	struct btrfs_key key;
3176d7728c96SJan Schmidt 
3177d7728c96SJan Schmidt 	if (!capable(CAP_SYS_ADMIN))
3178d7728c96SJan Schmidt 		return -EPERM;
3179d7728c96SJan Schmidt 
3180d7728c96SJan Schmidt 	loi = memdup_user(arg, sizeof(*loi));
3181d7728c96SJan Schmidt 	if (IS_ERR(loi)) {
3182d7728c96SJan Schmidt 		ret = PTR_ERR(loi);
3183d7728c96SJan Schmidt 		loi = NULL;
3184d7728c96SJan Schmidt 		goto out;
3185d7728c96SJan Schmidt 	}
3186d7728c96SJan Schmidt 
3187d7728c96SJan Schmidt 	path = btrfs_alloc_path();
3188d7728c96SJan Schmidt 	if (!path) {
3189d7728c96SJan Schmidt 		ret = -ENOMEM;
3190d7728c96SJan Schmidt 		goto out;
3191d7728c96SJan Schmidt 	}
3192d7728c96SJan Schmidt 
3193d7728c96SJan Schmidt 	size = min_t(u32, loi->size, 4096);
3194d7728c96SJan Schmidt 	inodes = init_data_container(size);
3195d7728c96SJan Schmidt 	if (IS_ERR(inodes)) {
3196d7728c96SJan Schmidt 		ret = PTR_ERR(inodes);
3197d7728c96SJan Schmidt 		inodes = NULL;
3198d7728c96SJan Schmidt 		goto out;
3199d7728c96SJan Schmidt 	}
3200d7728c96SJan Schmidt 
3201d7728c96SJan Schmidt 	ret = extent_from_logical(root->fs_info, loi->logical, path, &key);
32024692cf58SJan Schmidt 	btrfs_release_path(path);
3203d7728c96SJan Schmidt 
3204d7728c96SJan Schmidt 	if (ret & BTRFS_EXTENT_FLAG_TREE_BLOCK)
3205d7728c96SJan Schmidt 		ret = -ENOENT;
3206d7728c96SJan Schmidt 	if (ret < 0)
3207d7728c96SJan Schmidt 		goto out;
3208d7728c96SJan Schmidt 
32094692cf58SJan Schmidt 	extent_item_pos = loi->logical - key.objectid;
32107a3ae2f8SJan Schmidt 	ret = iterate_extent_inodes(root->fs_info, key.objectid,
32117a3ae2f8SJan Schmidt 					extent_item_pos, 0, build_ino_list,
32124692cf58SJan Schmidt 					inodes);
3213d7728c96SJan Schmidt 
3214d7728c96SJan Schmidt 	if (ret < 0)
3215d7728c96SJan Schmidt 		goto out;
3216d7728c96SJan Schmidt 
3217745c4d8eSJeff Mahoney 	ret = copy_to_user((void *)(unsigned long)loi->inodes,
3218745c4d8eSJeff Mahoney 			   (void *)(unsigned long)inodes, size);
3219d7728c96SJan Schmidt 	if (ret)
3220d7728c96SJan Schmidt 		ret = -EFAULT;
3221d7728c96SJan Schmidt 
3222d7728c96SJan Schmidt out:
3223d7728c96SJan Schmidt 	btrfs_free_path(path);
3224d7728c96SJan Schmidt 	kfree(inodes);
3225d7728c96SJan Schmidt 	kfree(loi);
3226d7728c96SJan Schmidt 
3227d7728c96SJan Schmidt 	return ret;
3228d7728c96SJan Schmidt }
3229d7728c96SJan Schmidt 
323019a39dceSIlya Dryomov void update_ioctl_balance_args(struct btrfs_fs_info *fs_info, int lock,
3231c9e9f97bSIlya Dryomov 			       struct btrfs_ioctl_balance_args *bargs)
3232c9e9f97bSIlya Dryomov {
3233c9e9f97bSIlya Dryomov 	struct btrfs_balance_control *bctl = fs_info->balance_ctl;
3234c9e9f97bSIlya Dryomov 
3235c9e9f97bSIlya Dryomov 	bargs->flags = bctl->flags;
3236c9e9f97bSIlya Dryomov 
3237837d5b6eSIlya Dryomov 	if (atomic_read(&fs_info->balance_running))
3238837d5b6eSIlya Dryomov 		bargs->state |= BTRFS_BALANCE_STATE_RUNNING;
3239837d5b6eSIlya Dryomov 	if (atomic_read(&fs_info->balance_pause_req))
3240837d5b6eSIlya Dryomov 		bargs->state |= BTRFS_BALANCE_STATE_PAUSE_REQ;
3241a7e99c69SIlya Dryomov 	if (atomic_read(&fs_info->balance_cancel_req))
3242a7e99c69SIlya Dryomov 		bargs->state |= BTRFS_BALANCE_STATE_CANCEL_REQ;
3243837d5b6eSIlya Dryomov 
3244c9e9f97bSIlya Dryomov 	memcpy(&bargs->data, &bctl->data, sizeof(bargs->data));
3245c9e9f97bSIlya Dryomov 	memcpy(&bargs->meta, &bctl->meta, sizeof(bargs->meta));
3246c9e9f97bSIlya Dryomov 	memcpy(&bargs->sys, &bctl->sys, sizeof(bargs->sys));
324719a39dceSIlya Dryomov 
324819a39dceSIlya Dryomov 	if (lock) {
324919a39dceSIlya Dryomov 		spin_lock(&fs_info->balance_lock);
325019a39dceSIlya Dryomov 		memcpy(&bargs->stat, &bctl->stat, sizeof(bargs->stat));
325119a39dceSIlya Dryomov 		spin_unlock(&fs_info->balance_lock);
325219a39dceSIlya Dryomov 	} else {
325319a39dceSIlya Dryomov 		memcpy(&bargs->stat, &bctl->stat, sizeof(bargs->stat));
325419a39dceSIlya Dryomov 	}
3255c9e9f97bSIlya Dryomov }
3256c9e9f97bSIlya Dryomov 
32579ba1f6e4SLiu Bo static long btrfs_ioctl_balance(struct file *file, void __user *arg)
3258c9e9f97bSIlya Dryomov {
32599ba1f6e4SLiu Bo 	struct btrfs_root *root = BTRFS_I(fdentry(file)->d_inode)->root;
3260c9e9f97bSIlya Dryomov 	struct btrfs_fs_info *fs_info = root->fs_info;
3261c9e9f97bSIlya Dryomov 	struct btrfs_ioctl_balance_args *bargs;
3262c9e9f97bSIlya Dryomov 	struct btrfs_balance_control *bctl;
3263c9e9f97bSIlya Dryomov 	int ret;
3264c9e9f97bSIlya Dryomov 
3265c9e9f97bSIlya Dryomov 	if (!capable(CAP_SYS_ADMIN))
3266c9e9f97bSIlya Dryomov 		return -EPERM;
3267c9e9f97bSIlya Dryomov 
3268*e54bfa31SLiu Bo 	ret = mnt_want_write_file(file);
32699ba1f6e4SLiu Bo 	if (ret)
32709ba1f6e4SLiu Bo 		return ret;
32719ba1f6e4SLiu Bo 
3272c9e9f97bSIlya Dryomov 	mutex_lock(&fs_info->volume_mutex);
3273c9e9f97bSIlya Dryomov 	mutex_lock(&fs_info->balance_mutex);
3274c9e9f97bSIlya Dryomov 
3275c9e9f97bSIlya Dryomov 	if (arg) {
3276c9e9f97bSIlya Dryomov 		bargs = memdup_user(arg, sizeof(*bargs));
3277c9e9f97bSIlya Dryomov 		if (IS_ERR(bargs)) {
3278c9e9f97bSIlya Dryomov 			ret = PTR_ERR(bargs);
3279c9e9f97bSIlya Dryomov 			goto out;
3280c9e9f97bSIlya Dryomov 		}
3281de322263SIlya Dryomov 
3282de322263SIlya Dryomov 		if (bargs->flags & BTRFS_BALANCE_RESUME) {
3283de322263SIlya Dryomov 			if (!fs_info->balance_ctl) {
3284de322263SIlya Dryomov 				ret = -ENOTCONN;
3285de322263SIlya Dryomov 				goto out_bargs;
3286de322263SIlya Dryomov 			}
3287de322263SIlya Dryomov 
3288de322263SIlya Dryomov 			bctl = fs_info->balance_ctl;
3289de322263SIlya Dryomov 			spin_lock(&fs_info->balance_lock);
3290de322263SIlya Dryomov 			bctl->flags |= BTRFS_BALANCE_RESUME;
3291de322263SIlya Dryomov 			spin_unlock(&fs_info->balance_lock);
3292de322263SIlya Dryomov 
3293de322263SIlya Dryomov 			goto do_balance;
3294de322263SIlya Dryomov 		}
3295c9e9f97bSIlya Dryomov 	} else {
3296c9e9f97bSIlya Dryomov 		bargs = NULL;
3297c9e9f97bSIlya Dryomov 	}
3298c9e9f97bSIlya Dryomov 
3299837d5b6eSIlya Dryomov 	if (fs_info->balance_ctl) {
3300837d5b6eSIlya Dryomov 		ret = -EINPROGRESS;
3301837d5b6eSIlya Dryomov 		goto out_bargs;
3302837d5b6eSIlya Dryomov 	}
3303837d5b6eSIlya Dryomov 
3304c9e9f97bSIlya Dryomov 	bctl = kzalloc(sizeof(*bctl), GFP_NOFS);
3305c9e9f97bSIlya Dryomov 	if (!bctl) {
3306c9e9f97bSIlya Dryomov 		ret = -ENOMEM;
3307c9e9f97bSIlya Dryomov 		goto out_bargs;
3308c9e9f97bSIlya Dryomov 	}
3309c9e9f97bSIlya Dryomov 
3310c9e9f97bSIlya Dryomov 	bctl->fs_info = fs_info;
3311c9e9f97bSIlya Dryomov 	if (arg) {
3312c9e9f97bSIlya Dryomov 		memcpy(&bctl->data, &bargs->data, sizeof(bctl->data));
3313c9e9f97bSIlya Dryomov 		memcpy(&bctl->meta, &bargs->meta, sizeof(bctl->meta));
3314c9e9f97bSIlya Dryomov 		memcpy(&bctl->sys, &bargs->sys, sizeof(bctl->sys));
3315c9e9f97bSIlya Dryomov 
3316c9e9f97bSIlya Dryomov 		bctl->flags = bargs->flags;
3317f43ffb60SIlya Dryomov 	} else {
3318f43ffb60SIlya Dryomov 		/* balance everything - no filters */
3319f43ffb60SIlya Dryomov 		bctl->flags |= BTRFS_BALANCE_TYPE_MASK;
3320c9e9f97bSIlya Dryomov 	}
3321c9e9f97bSIlya Dryomov 
3322de322263SIlya Dryomov do_balance:
3323c9e9f97bSIlya Dryomov 	ret = btrfs_balance(bctl, bargs);
3324c9e9f97bSIlya Dryomov 	/*
3325837d5b6eSIlya Dryomov 	 * bctl is freed in __cancel_balance or in free_fs_info if
3326837d5b6eSIlya Dryomov 	 * restriper was paused all the way until unmount
3327c9e9f97bSIlya Dryomov 	 */
3328c9e9f97bSIlya Dryomov 	if (arg) {
3329c9e9f97bSIlya Dryomov 		if (copy_to_user(arg, bargs, sizeof(*bargs)))
3330c9e9f97bSIlya Dryomov 			ret = -EFAULT;
3331c9e9f97bSIlya Dryomov 	}
3332c9e9f97bSIlya Dryomov 
3333c9e9f97bSIlya Dryomov out_bargs:
3334c9e9f97bSIlya Dryomov 	kfree(bargs);
3335c9e9f97bSIlya Dryomov out:
3336c9e9f97bSIlya Dryomov 	mutex_unlock(&fs_info->balance_mutex);
3337c9e9f97bSIlya Dryomov 	mutex_unlock(&fs_info->volume_mutex);
3338*e54bfa31SLiu Bo 	mnt_drop_write_file(file);
3339c9e9f97bSIlya Dryomov 	return ret;
3340c9e9f97bSIlya Dryomov }
3341c9e9f97bSIlya Dryomov 
3342837d5b6eSIlya Dryomov static long btrfs_ioctl_balance_ctl(struct btrfs_root *root, int cmd)
3343837d5b6eSIlya Dryomov {
3344837d5b6eSIlya Dryomov 	if (!capable(CAP_SYS_ADMIN))
3345837d5b6eSIlya Dryomov 		return -EPERM;
3346837d5b6eSIlya Dryomov 
3347837d5b6eSIlya Dryomov 	switch (cmd) {
3348837d5b6eSIlya Dryomov 	case BTRFS_BALANCE_CTL_PAUSE:
3349837d5b6eSIlya Dryomov 		return btrfs_pause_balance(root->fs_info);
3350a7e99c69SIlya Dryomov 	case BTRFS_BALANCE_CTL_CANCEL:
3351a7e99c69SIlya Dryomov 		return btrfs_cancel_balance(root->fs_info);
3352837d5b6eSIlya Dryomov 	}
3353837d5b6eSIlya Dryomov 
3354837d5b6eSIlya Dryomov 	return -EINVAL;
3355837d5b6eSIlya Dryomov }
3356837d5b6eSIlya Dryomov 
335719a39dceSIlya Dryomov static long btrfs_ioctl_balance_progress(struct btrfs_root *root,
335819a39dceSIlya Dryomov 					 void __user *arg)
335919a39dceSIlya Dryomov {
336019a39dceSIlya Dryomov 	struct btrfs_fs_info *fs_info = root->fs_info;
336119a39dceSIlya Dryomov 	struct btrfs_ioctl_balance_args *bargs;
336219a39dceSIlya Dryomov 	int ret = 0;
336319a39dceSIlya Dryomov 
336419a39dceSIlya Dryomov 	if (!capable(CAP_SYS_ADMIN))
336519a39dceSIlya Dryomov 		return -EPERM;
336619a39dceSIlya Dryomov 
336719a39dceSIlya Dryomov 	mutex_lock(&fs_info->balance_mutex);
336819a39dceSIlya Dryomov 	if (!fs_info->balance_ctl) {
336919a39dceSIlya Dryomov 		ret = -ENOTCONN;
337019a39dceSIlya Dryomov 		goto out;
337119a39dceSIlya Dryomov 	}
337219a39dceSIlya Dryomov 
337319a39dceSIlya Dryomov 	bargs = kzalloc(sizeof(*bargs), GFP_NOFS);
337419a39dceSIlya Dryomov 	if (!bargs) {
337519a39dceSIlya Dryomov 		ret = -ENOMEM;
337619a39dceSIlya Dryomov 		goto out;
337719a39dceSIlya Dryomov 	}
337819a39dceSIlya Dryomov 
337919a39dceSIlya Dryomov 	update_ioctl_balance_args(fs_info, 1, bargs);
338019a39dceSIlya Dryomov 
338119a39dceSIlya Dryomov 	if (copy_to_user(arg, bargs, sizeof(*bargs)))
338219a39dceSIlya Dryomov 		ret = -EFAULT;
338319a39dceSIlya Dryomov 
338419a39dceSIlya Dryomov 	kfree(bargs);
338519a39dceSIlya Dryomov out:
338619a39dceSIlya Dryomov 	mutex_unlock(&fs_info->balance_mutex);
338719a39dceSIlya Dryomov 	return ret;
338819a39dceSIlya Dryomov }
338919a39dceSIlya Dryomov 
3390f46b5a66SChristoph Hellwig long btrfs_ioctl(struct file *file, unsigned int
3391f46b5a66SChristoph Hellwig 		cmd, unsigned long arg)
3392f46b5a66SChristoph Hellwig {
3393f46b5a66SChristoph Hellwig 	struct btrfs_root *root = BTRFS_I(fdentry(file)->d_inode)->root;
33944bcabaa3SChristoph Hellwig 	void __user *argp = (void __user *)arg;
3395f46b5a66SChristoph Hellwig 
3396f46b5a66SChristoph Hellwig 	switch (cmd) {
33976cbff00fSChristoph Hellwig 	case FS_IOC_GETFLAGS:
33986cbff00fSChristoph Hellwig 		return btrfs_ioctl_getflags(file, argp);
33996cbff00fSChristoph Hellwig 	case FS_IOC_SETFLAGS:
34006cbff00fSChristoph Hellwig 		return btrfs_ioctl_setflags(file, argp);
34016cbff00fSChristoph Hellwig 	case FS_IOC_GETVERSION:
34026cbff00fSChristoph Hellwig 		return btrfs_ioctl_getversion(file, argp);
3403f7039b1dSLi Dongyang 	case FITRIM:
3404f7039b1dSLi Dongyang 		return btrfs_ioctl_fitrim(file, argp);
3405f46b5a66SChristoph Hellwig 	case BTRFS_IOC_SNAP_CREATE:
3406fa0d2b9bSLi Zefan 		return btrfs_ioctl_snap_create(file, argp, 0);
3407fdfb1e4fSLi Zefan 	case BTRFS_IOC_SNAP_CREATE_V2:
3408fa0d2b9bSLi Zefan 		return btrfs_ioctl_snap_create_v2(file, argp, 0);
34093de4586cSChris Mason 	case BTRFS_IOC_SUBVOL_CREATE:
3410fa0d2b9bSLi Zefan 		return btrfs_ioctl_snap_create(file, argp, 1);
341176dda93cSYan, Zheng 	case BTRFS_IOC_SNAP_DESTROY:
341276dda93cSYan, Zheng 		return btrfs_ioctl_snap_destroy(file, argp);
34130caa102dSLi Zefan 	case BTRFS_IOC_SUBVOL_GETFLAGS:
34140caa102dSLi Zefan 		return btrfs_ioctl_subvol_getflags(file, argp);
34150caa102dSLi Zefan 	case BTRFS_IOC_SUBVOL_SETFLAGS:
34160caa102dSLi Zefan 		return btrfs_ioctl_subvol_setflags(file, argp);
34176ef5ed0dSJosef Bacik 	case BTRFS_IOC_DEFAULT_SUBVOL:
34186ef5ed0dSJosef Bacik 		return btrfs_ioctl_default_subvol(file, argp);
3419f46b5a66SChristoph Hellwig 	case BTRFS_IOC_DEFRAG:
34201e701a32SChris Mason 		return btrfs_ioctl_defrag(file, NULL);
34211e701a32SChris Mason 	case BTRFS_IOC_DEFRAG_RANGE:
34221e701a32SChris Mason 		return btrfs_ioctl_defrag(file, argp);
3423f46b5a66SChristoph Hellwig 	case BTRFS_IOC_RESIZE:
34244bcabaa3SChristoph Hellwig 		return btrfs_ioctl_resize(root, argp);
3425f46b5a66SChristoph Hellwig 	case BTRFS_IOC_ADD_DEV:
34264bcabaa3SChristoph Hellwig 		return btrfs_ioctl_add_dev(root, argp);
3427f46b5a66SChristoph Hellwig 	case BTRFS_IOC_RM_DEV:
34284bcabaa3SChristoph Hellwig 		return btrfs_ioctl_rm_dev(root, argp);
3429475f6387SJan Schmidt 	case BTRFS_IOC_FS_INFO:
3430475f6387SJan Schmidt 		return btrfs_ioctl_fs_info(root, argp);
3431475f6387SJan Schmidt 	case BTRFS_IOC_DEV_INFO:
3432475f6387SJan Schmidt 		return btrfs_ioctl_dev_info(root, argp);
3433f46b5a66SChristoph Hellwig 	case BTRFS_IOC_BALANCE:
34349ba1f6e4SLiu Bo 		return btrfs_ioctl_balance(file, NULL);
3435f46b5a66SChristoph Hellwig 	case BTRFS_IOC_CLONE:
3436c5c9cd4dSSage Weil 		return btrfs_ioctl_clone(file, arg, 0, 0, 0);
3437c5c9cd4dSSage Weil 	case BTRFS_IOC_CLONE_RANGE:
34387a865e8aSChristoph Hellwig 		return btrfs_ioctl_clone_range(file, argp);
3439f46b5a66SChristoph Hellwig 	case BTRFS_IOC_TRANS_START:
3440f46b5a66SChristoph Hellwig 		return btrfs_ioctl_trans_start(file);
3441f46b5a66SChristoph Hellwig 	case BTRFS_IOC_TRANS_END:
3442f46b5a66SChristoph Hellwig 		return btrfs_ioctl_trans_end(file);
3443ac8e9819SChris Mason 	case BTRFS_IOC_TREE_SEARCH:
3444ac8e9819SChris Mason 		return btrfs_ioctl_tree_search(file, argp);
3445ac8e9819SChris Mason 	case BTRFS_IOC_INO_LOOKUP:
3446ac8e9819SChris Mason 		return btrfs_ioctl_ino_lookup(file, argp);
3447d7728c96SJan Schmidt 	case BTRFS_IOC_INO_PATHS:
3448d7728c96SJan Schmidt 		return btrfs_ioctl_ino_to_path(root, argp);
3449d7728c96SJan Schmidt 	case BTRFS_IOC_LOGICAL_INO:
3450d7728c96SJan Schmidt 		return btrfs_ioctl_logical_to_ino(root, argp);
34511406e432SJosef Bacik 	case BTRFS_IOC_SPACE_INFO:
34521406e432SJosef Bacik 		return btrfs_ioctl_space_info(root, argp);
3453f46b5a66SChristoph Hellwig 	case BTRFS_IOC_SYNC:
3454f46b5a66SChristoph Hellwig 		btrfs_sync_fs(file->f_dentry->d_sb, 1);
3455f46b5a66SChristoph Hellwig 		return 0;
345646204592SSage Weil 	case BTRFS_IOC_START_SYNC:
345746204592SSage Weil 		return btrfs_ioctl_start_sync(file, argp);
345846204592SSage Weil 	case BTRFS_IOC_WAIT_SYNC:
345946204592SSage Weil 		return btrfs_ioctl_wait_sync(file, argp);
3460475f6387SJan Schmidt 	case BTRFS_IOC_SCRUB:
3461475f6387SJan Schmidt 		return btrfs_ioctl_scrub(root, argp);
3462475f6387SJan Schmidt 	case BTRFS_IOC_SCRUB_CANCEL:
3463475f6387SJan Schmidt 		return btrfs_ioctl_scrub_cancel(root, argp);
3464475f6387SJan Schmidt 	case BTRFS_IOC_SCRUB_PROGRESS:
3465475f6387SJan Schmidt 		return btrfs_ioctl_scrub_progress(root, argp);
3466c9e9f97bSIlya Dryomov 	case BTRFS_IOC_BALANCE_V2:
34679ba1f6e4SLiu Bo 		return btrfs_ioctl_balance(file, argp);
3468837d5b6eSIlya Dryomov 	case BTRFS_IOC_BALANCE_CTL:
3469837d5b6eSIlya Dryomov 		return btrfs_ioctl_balance_ctl(root, arg);
347019a39dceSIlya Dryomov 	case BTRFS_IOC_BALANCE_PROGRESS:
347119a39dceSIlya Dryomov 		return btrfs_ioctl_balance_progress(root, argp);
3472c11d2c23SStefan Behrens 	case BTRFS_IOC_GET_DEV_STATS:
3473b27f7c0cSDavid Sterba 		return btrfs_ioctl_get_dev_stats(root, argp);
3474f46b5a66SChristoph Hellwig 	}
3475f46b5a66SChristoph Hellwig 
3476f46b5a66SChristoph Hellwig 	return -ENOTTY;
3477f46b5a66SChristoph Hellwig }
3478