xref: /openbmc/linux/fs/btrfs/ioctl.c (revision 4cb5300b)
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"
54f46b5a66SChristoph Hellwig 
556cbff00fSChristoph Hellwig /* Mask out flags that are inappropriate for the given type of inode. */
566cbff00fSChristoph Hellwig static inline __u32 btrfs_mask_flags(umode_t mode, __u32 flags)
576cbff00fSChristoph Hellwig {
586cbff00fSChristoph Hellwig 	if (S_ISDIR(mode))
596cbff00fSChristoph Hellwig 		return flags;
606cbff00fSChristoph Hellwig 	else if (S_ISREG(mode))
616cbff00fSChristoph Hellwig 		return flags & ~FS_DIRSYNC_FL;
626cbff00fSChristoph Hellwig 	else
636cbff00fSChristoph Hellwig 		return flags & (FS_NODUMP_FL | FS_NOATIME_FL);
646cbff00fSChristoph Hellwig }
65f46b5a66SChristoph Hellwig 
666cbff00fSChristoph Hellwig /*
676cbff00fSChristoph Hellwig  * Export inode flags to the format expected by the FS_IOC_GETFLAGS ioctl.
686cbff00fSChristoph Hellwig  */
696cbff00fSChristoph Hellwig static unsigned int btrfs_flags_to_ioctl(unsigned int flags)
706cbff00fSChristoph Hellwig {
716cbff00fSChristoph Hellwig 	unsigned int iflags = 0;
726cbff00fSChristoph Hellwig 
736cbff00fSChristoph Hellwig 	if (flags & BTRFS_INODE_SYNC)
746cbff00fSChristoph Hellwig 		iflags |= FS_SYNC_FL;
756cbff00fSChristoph Hellwig 	if (flags & BTRFS_INODE_IMMUTABLE)
766cbff00fSChristoph Hellwig 		iflags |= FS_IMMUTABLE_FL;
776cbff00fSChristoph Hellwig 	if (flags & BTRFS_INODE_APPEND)
786cbff00fSChristoph Hellwig 		iflags |= FS_APPEND_FL;
796cbff00fSChristoph Hellwig 	if (flags & BTRFS_INODE_NODUMP)
806cbff00fSChristoph Hellwig 		iflags |= FS_NODUMP_FL;
816cbff00fSChristoph Hellwig 	if (flags & BTRFS_INODE_NOATIME)
826cbff00fSChristoph Hellwig 		iflags |= FS_NOATIME_FL;
836cbff00fSChristoph Hellwig 	if (flags & BTRFS_INODE_DIRSYNC)
846cbff00fSChristoph Hellwig 		iflags |= FS_DIRSYNC_FL;
85d0092bddSLi Zefan 	if (flags & BTRFS_INODE_NODATACOW)
86d0092bddSLi Zefan 		iflags |= FS_NOCOW_FL;
87d0092bddSLi Zefan 
88d0092bddSLi Zefan 	if ((flags & BTRFS_INODE_COMPRESS) && !(flags & BTRFS_INODE_NOCOMPRESS))
89d0092bddSLi Zefan 		iflags |= FS_COMPR_FL;
90d0092bddSLi Zefan 	else if (flags & BTRFS_INODE_NOCOMPRESS)
91d0092bddSLi Zefan 		iflags |= FS_NOCOMP_FL;
926cbff00fSChristoph Hellwig 
936cbff00fSChristoph Hellwig 	return iflags;
946cbff00fSChristoph Hellwig }
956cbff00fSChristoph Hellwig 
966cbff00fSChristoph Hellwig /*
976cbff00fSChristoph Hellwig  * Update inode->i_flags based on the btrfs internal flags.
986cbff00fSChristoph Hellwig  */
996cbff00fSChristoph Hellwig void btrfs_update_iflags(struct inode *inode)
1006cbff00fSChristoph Hellwig {
1016cbff00fSChristoph Hellwig 	struct btrfs_inode *ip = BTRFS_I(inode);
1026cbff00fSChristoph Hellwig 
1036cbff00fSChristoph Hellwig 	inode->i_flags &= ~(S_SYNC|S_APPEND|S_IMMUTABLE|S_NOATIME|S_DIRSYNC);
1046cbff00fSChristoph Hellwig 
1056cbff00fSChristoph Hellwig 	if (ip->flags & BTRFS_INODE_SYNC)
1066cbff00fSChristoph Hellwig 		inode->i_flags |= S_SYNC;
1076cbff00fSChristoph Hellwig 	if (ip->flags & BTRFS_INODE_IMMUTABLE)
1086cbff00fSChristoph Hellwig 		inode->i_flags |= S_IMMUTABLE;
1096cbff00fSChristoph Hellwig 	if (ip->flags & BTRFS_INODE_APPEND)
1106cbff00fSChristoph Hellwig 		inode->i_flags |= S_APPEND;
1116cbff00fSChristoph Hellwig 	if (ip->flags & BTRFS_INODE_NOATIME)
1126cbff00fSChristoph Hellwig 		inode->i_flags |= S_NOATIME;
1136cbff00fSChristoph Hellwig 	if (ip->flags & BTRFS_INODE_DIRSYNC)
1146cbff00fSChristoph Hellwig 		inode->i_flags |= S_DIRSYNC;
1156cbff00fSChristoph Hellwig }
1166cbff00fSChristoph Hellwig 
1176cbff00fSChristoph Hellwig /*
1186cbff00fSChristoph Hellwig  * Inherit flags from the parent inode.
1196cbff00fSChristoph Hellwig  *
1206cbff00fSChristoph Hellwig  * Unlike extN we don't have any flags we don't want to inherit currently.
1216cbff00fSChristoph Hellwig  */
1226cbff00fSChristoph Hellwig void btrfs_inherit_iflags(struct inode *inode, struct inode *dir)
1236cbff00fSChristoph Hellwig {
1240b4dcea5SChris Mason 	unsigned int flags;
1250b4dcea5SChris Mason 
1260b4dcea5SChris Mason 	if (!dir)
1270b4dcea5SChris Mason 		return;
1280b4dcea5SChris Mason 
1290b4dcea5SChris Mason 	flags = BTRFS_I(dir)->flags;
1306cbff00fSChristoph Hellwig 
1316cbff00fSChristoph Hellwig 	if (S_ISREG(inode->i_mode))
1326cbff00fSChristoph Hellwig 		flags &= ~BTRFS_INODE_DIRSYNC;
1336cbff00fSChristoph Hellwig 	else if (!S_ISDIR(inode->i_mode))
1346cbff00fSChristoph Hellwig 		flags &= (BTRFS_INODE_NODUMP | BTRFS_INODE_NOATIME);
1356cbff00fSChristoph Hellwig 
1366cbff00fSChristoph Hellwig 	BTRFS_I(inode)->flags = flags;
1376cbff00fSChristoph Hellwig 	btrfs_update_iflags(inode);
1386cbff00fSChristoph Hellwig }
1396cbff00fSChristoph Hellwig 
1406cbff00fSChristoph Hellwig static int btrfs_ioctl_getflags(struct file *file, void __user *arg)
1416cbff00fSChristoph Hellwig {
1426cbff00fSChristoph Hellwig 	struct btrfs_inode *ip = BTRFS_I(file->f_path.dentry->d_inode);
1436cbff00fSChristoph Hellwig 	unsigned int flags = btrfs_flags_to_ioctl(ip->flags);
1446cbff00fSChristoph Hellwig 
1456cbff00fSChristoph Hellwig 	if (copy_to_user(arg, &flags, sizeof(flags)))
1466cbff00fSChristoph Hellwig 		return -EFAULT;
1476cbff00fSChristoph Hellwig 	return 0;
1486cbff00fSChristoph Hellwig }
1496cbff00fSChristoph Hellwig 
15075e7cb7fSLiu Bo static int check_flags(unsigned int flags)
15175e7cb7fSLiu Bo {
15275e7cb7fSLiu Bo 	if (flags & ~(FS_IMMUTABLE_FL | FS_APPEND_FL | \
15375e7cb7fSLiu Bo 		      FS_NOATIME_FL | FS_NODUMP_FL | \
15475e7cb7fSLiu Bo 		      FS_SYNC_FL | FS_DIRSYNC_FL | \
155e1e8fb6aSLi Zefan 		      FS_NOCOMP_FL | FS_COMPR_FL |
156e1e8fb6aSLi Zefan 		      FS_NOCOW_FL))
15775e7cb7fSLiu Bo 		return -EOPNOTSUPP;
15875e7cb7fSLiu Bo 
15975e7cb7fSLiu Bo 	if ((flags & FS_NOCOMP_FL) && (flags & FS_COMPR_FL))
16075e7cb7fSLiu Bo 		return -EINVAL;
16175e7cb7fSLiu Bo 
16275e7cb7fSLiu Bo 	return 0;
16375e7cb7fSLiu Bo }
16475e7cb7fSLiu Bo 
1656cbff00fSChristoph Hellwig static int btrfs_ioctl_setflags(struct file *file, void __user *arg)
1666cbff00fSChristoph Hellwig {
1676cbff00fSChristoph Hellwig 	struct inode *inode = file->f_path.dentry->d_inode;
1686cbff00fSChristoph Hellwig 	struct btrfs_inode *ip = BTRFS_I(inode);
1696cbff00fSChristoph Hellwig 	struct btrfs_root *root = ip->root;
1706cbff00fSChristoph Hellwig 	struct btrfs_trans_handle *trans;
1716cbff00fSChristoph Hellwig 	unsigned int flags, oldflags;
1726cbff00fSChristoph Hellwig 	int ret;
1736cbff00fSChristoph Hellwig 
174b83cc969SLi Zefan 	if (btrfs_root_readonly(root))
175b83cc969SLi Zefan 		return -EROFS;
176b83cc969SLi Zefan 
1776cbff00fSChristoph Hellwig 	if (copy_from_user(&flags, arg, sizeof(flags)))
1786cbff00fSChristoph Hellwig 		return -EFAULT;
1796cbff00fSChristoph Hellwig 
18075e7cb7fSLiu Bo 	ret = check_flags(flags);
18175e7cb7fSLiu Bo 	if (ret)
18275e7cb7fSLiu Bo 		return ret;
1836cbff00fSChristoph Hellwig 
1842e149670SSerge E. Hallyn 	if (!inode_owner_or_capable(inode))
1856cbff00fSChristoph Hellwig 		return -EACCES;
1866cbff00fSChristoph Hellwig 
1876cbff00fSChristoph Hellwig 	mutex_lock(&inode->i_mutex);
1886cbff00fSChristoph Hellwig 
1896cbff00fSChristoph Hellwig 	flags = btrfs_mask_flags(inode->i_mode, flags);
1906cbff00fSChristoph Hellwig 	oldflags = btrfs_flags_to_ioctl(ip->flags);
1916cbff00fSChristoph Hellwig 	if ((flags ^ oldflags) & (FS_APPEND_FL | FS_IMMUTABLE_FL)) {
1926cbff00fSChristoph Hellwig 		if (!capable(CAP_LINUX_IMMUTABLE)) {
1936cbff00fSChristoph Hellwig 			ret = -EPERM;
1946cbff00fSChristoph Hellwig 			goto out_unlock;
1956cbff00fSChristoph Hellwig 		}
1966cbff00fSChristoph Hellwig 	}
1976cbff00fSChristoph Hellwig 
1986cbff00fSChristoph Hellwig 	ret = mnt_want_write(file->f_path.mnt);
1996cbff00fSChristoph Hellwig 	if (ret)
2006cbff00fSChristoph Hellwig 		goto out_unlock;
2016cbff00fSChristoph Hellwig 
2026cbff00fSChristoph Hellwig 	if (flags & FS_SYNC_FL)
2036cbff00fSChristoph Hellwig 		ip->flags |= BTRFS_INODE_SYNC;
2046cbff00fSChristoph Hellwig 	else
2056cbff00fSChristoph Hellwig 		ip->flags &= ~BTRFS_INODE_SYNC;
2066cbff00fSChristoph Hellwig 	if (flags & FS_IMMUTABLE_FL)
2076cbff00fSChristoph Hellwig 		ip->flags |= BTRFS_INODE_IMMUTABLE;
2086cbff00fSChristoph Hellwig 	else
2096cbff00fSChristoph Hellwig 		ip->flags &= ~BTRFS_INODE_IMMUTABLE;
2106cbff00fSChristoph Hellwig 	if (flags & FS_APPEND_FL)
2116cbff00fSChristoph Hellwig 		ip->flags |= BTRFS_INODE_APPEND;
2126cbff00fSChristoph Hellwig 	else
2136cbff00fSChristoph Hellwig 		ip->flags &= ~BTRFS_INODE_APPEND;
2146cbff00fSChristoph Hellwig 	if (flags & FS_NODUMP_FL)
2156cbff00fSChristoph Hellwig 		ip->flags |= BTRFS_INODE_NODUMP;
2166cbff00fSChristoph Hellwig 	else
2176cbff00fSChristoph Hellwig 		ip->flags &= ~BTRFS_INODE_NODUMP;
2186cbff00fSChristoph Hellwig 	if (flags & FS_NOATIME_FL)
2196cbff00fSChristoph Hellwig 		ip->flags |= BTRFS_INODE_NOATIME;
2206cbff00fSChristoph Hellwig 	else
2216cbff00fSChristoph Hellwig 		ip->flags &= ~BTRFS_INODE_NOATIME;
2226cbff00fSChristoph Hellwig 	if (flags & FS_DIRSYNC_FL)
2236cbff00fSChristoph Hellwig 		ip->flags |= BTRFS_INODE_DIRSYNC;
2246cbff00fSChristoph Hellwig 	else
2256cbff00fSChristoph Hellwig 		ip->flags &= ~BTRFS_INODE_DIRSYNC;
226e1e8fb6aSLi Zefan 	if (flags & FS_NOCOW_FL)
227e1e8fb6aSLi Zefan 		ip->flags |= BTRFS_INODE_NODATACOW;
228e1e8fb6aSLi Zefan 	else
229e1e8fb6aSLi Zefan 		ip->flags &= ~BTRFS_INODE_NODATACOW;
2306cbff00fSChristoph Hellwig 
23175e7cb7fSLiu Bo 	/*
23275e7cb7fSLiu Bo 	 * The COMPRESS flag can only be changed by users, while the NOCOMPRESS
23375e7cb7fSLiu Bo 	 * flag may be changed automatically if compression code won't make
23475e7cb7fSLiu Bo 	 * things smaller.
23575e7cb7fSLiu Bo 	 */
23675e7cb7fSLiu Bo 	if (flags & FS_NOCOMP_FL) {
23775e7cb7fSLiu Bo 		ip->flags &= ~BTRFS_INODE_COMPRESS;
23875e7cb7fSLiu Bo 		ip->flags |= BTRFS_INODE_NOCOMPRESS;
23975e7cb7fSLiu Bo 	} else if (flags & FS_COMPR_FL) {
24075e7cb7fSLiu Bo 		ip->flags |= BTRFS_INODE_COMPRESS;
24175e7cb7fSLiu Bo 		ip->flags &= ~BTRFS_INODE_NOCOMPRESS;
242ebcb904dSLi Zefan 	} else {
243ebcb904dSLi Zefan 		ip->flags &= ~(BTRFS_INODE_COMPRESS | BTRFS_INODE_NOCOMPRESS);
24475e7cb7fSLiu Bo 	}
2456cbff00fSChristoph Hellwig 
2466cbff00fSChristoph Hellwig 	trans = btrfs_join_transaction(root, 1);
2473612b495STsutomu Itoh 	BUG_ON(IS_ERR(trans));
2486cbff00fSChristoph Hellwig 
2496cbff00fSChristoph Hellwig 	ret = btrfs_update_inode(trans, root, inode);
2506cbff00fSChristoph Hellwig 	BUG_ON(ret);
2516cbff00fSChristoph Hellwig 
2526cbff00fSChristoph Hellwig 	btrfs_update_iflags(inode);
2536cbff00fSChristoph Hellwig 	inode->i_ctime = CURRENT_TIME;
2546cbff00fSChristoph Hellwig 	btrfs_end_transaction(trans, root);
2556cbff00fSChristoph Hellwig 
2566cbff00fSChristoph Hellwig 	mnt_drop_write(file->f_path.mnt);
2572d4e6f6aSliubo 
2582d4e6f6aSliubo 	ret = 0;
2596cbff00fSChristoph Hellwig  out_unlock:
2606cbff00fSChristoph Hellwig 	mutex_unlock(&inode->i_mutex);
2612d4e6f6aSliubo 	return ret;
2626cbff00fSChristoph Hellwig }
2636cbff00fSChristoph Hellwig 
2646cbff00fSChristoph Hellwig static int btrfs_ioctl_getversion(struct file *file, int __user *arg)
2656cbff00fSChristoph Hellwig {
2666cbff00fSChristoph Hellwig 	struct inode *inode = file->f_path.dentry->d_inode;
2676cbff00fSChristoph Hellwig 
2686cbff00fSChristoph Hellwig 	return put_user(inode->i_generation, arg);
2696cbff00fSChristoph Hellwig }
270f46b5a66SChristoph Hellwig 
271f7039b1dSLi Dongyang static noinline int btrfs_ioctl_fitrim(struct file *file, void __user *arg)
272f7039b1dSLi Dongyang {
273f7039b1dSLi Dongyang 	struct btrfs_root *root = fdentry(file)->d_sb->s_fs_info;
274f7039b1dSLi Dongyang 	struct btrfs_fs_info *fs_info = root->fs_info;
275f7039b1dSLi Dongyang 	struct btrfs_device *device;
276f7039b1dSLi Dongyang 	struct request_queue *q;
277f7039b1dSLi Dongyang 	struct fstrim_range range;
278f7039b1dSLi Dongyang 	u64 minlen = ULLONG_MAX;
279f7039b1dSLi Dongyang 	u64 num_devices = 0;
280f7039b1dSLi Dongyang 	int ret;
281f7039b1dSLi Dongyang 
282f7039b1dSLi Dongyang 	if (!capable(CAP_SYS_ADMIN))
283f7039b1dSLi Dongyang 		return -EPERM;
284f7039b1dSLi Dongyang 
2851f78160cSXiao Guangrong 	rcu_read_lock();
2861f78160cSXiao Guangrong 	list_for_each_entry_rcu(device, &fs_info->fs_devices->devices,
2871f78160cSXiao Guangrong 				dev_list) {
288f7039b1dSLi Dongyang 		if (!device->bdev)
289f7039b1dSLi Dongyang 			continue;
290f7039b1dSLi Dongyang 		q = bdev_get_queue(device->bdev);
291f7039b1dSLi Dongyang 		if (blk_queue_discard(q)) {
292f7039b1dSLi Dongyang 			num_devices++;
293f7039b1dSLi Dongyang 			minlen = min((u64)q->limits.discard_granularity,
294f7039b1dSLi Dongyang 				     minlen);
295f7039b1dSLi Dongyang 		}
296f7039b1dSLi Dongyang 	}
2971f78160cSXiao Guangrong 	rcu_read_unlock();
298f7039b1dSLi Dongyang 	if (!num_devices)
299f7039b1dSLi Dongyang 		return -EOPNOTSUPP;
300f7039b1dSLi Dongyang 
301f7039b1dSLi Dongyang 	if (copy_from_user(&range, arg, sizeof(range)))
302f7039b1dSLi Dongyang 		return -EFAULT;
303f7039b1dSLi Dongyang 
304f7039b1dSLi Dongyang 	range.minlen = max(range.minlen, minlen);
305f7039b1dSLi Dongyang 	ret = btrfs_trim_fs(root, &range);
306f7039b1dSLi Dongyang 	if (ret < 0)
307f7039b1dSLi Dongyang 		return ret;
308f7039b1dSLi Dongyang 
309f7039b1dSLi Dongyang 	if (copy_to_user(arg, &range, sizeof(range)))
310f7039b1dSLi Dongyang 		return -EFAULT;
311f7039b1dSLi Dongyang 
312f7039b1dSLi Dongyang 	return 0;
313f7039b1dSLi Dongyang }
314f7039b1dSLi Dongyang 
315cb8e7090SChristoph Hellwig static noinline int create_subvol(struct btrfs_root *root,
316cb8e7090SChristoph Hellwig 				  struct dentry *dentry,
31772fd032eSSage Weil 				  char *name, int namelen,
31872fd032eSSage Weil 				  u64 *async_transid)
319f46b5a66SChristoph Hellwig {
320f46b5a66SChristoph Hellwig 	struct btrfs_trans_handle *trans;
321f46b5a66SChristoph Hellwig 	struct btrfs_key key;
322f46b5a66SChristoph Hellwig 	struct btrfs_root_item root_item;
323f46b5a66SChristoph Hellwig 	struct btrfs_inode_item *inode_item;
324f46b5a66SChristoph Hellwig 	struct extent_buffer *leaf;
32576dda93cSYan, Zheng 	struct btrfs_root *new_root;
3266a912213SJosef Bacik 	struct dentry *parent = dget_parent(dentry);
3276a912213SJosef Bacik 	struct inode *dir;
328f46b5a66SChristoph Hellwig 	int ret;
329f46b5a66SChristoph Hellwig 	int err;
330f46b5a66SChristoph Hellwig 	u64 objectid;
331f46b5a66SChristoph Hellwig 	u64 new_dirid = BTRFS_FIRST_FREE_OBJECTID;
3323de4586cSChris Mason 	u64 index = 0;
333f46b5a66SChristoph Hellwig 
334581bb050SLi Zefan 	ret = btrfs_find_free_objectid(root->fs_info->tree_root, &objectid);
3356a912213SJosef Bacik 	if (ret) {
3366a912213SJosef Bacik 		dput(parent);
337a22285a6SYan, Zheng 		return ret;
3386a912213SJosef Bacik 	}
3396a912213SJosef Bacik 
3406a912213SJosef Bacik 	dir = parent->d_inode;
3416a912213SJosef Bacik 
3429ed74f2dSJosef Bacik 	/*
3439ed74f2dSJosef Bacik 	 * 1 - inode item
3449ed74f2dSJosef Bacik 	 * 2 - refs
3459ed74f2dSJosef Bacik 	 * 1 - root item
3469ed74f2dSJosef Bacik 	 * 2 - dir items
3479ed74f2dSJosef Bacik 	 */
348a22285a6SYan, Zheng 	trans = btrfs_start_transaction(root, 6);
3496a912213SJosef Bacik 	if (IS_ERR(trans)) {
3506a912213SJosef Bacik 		dput(parent);
351a22285a6SYan, Zheng 		return PTR_ERR(trans);
3526a912213SJosef Bacik 	}
353f46b5a66SChristoph Hellwig 
3545d4f98a2SYan Zheng 	leaf = btrfs_alloc_free_block(trans, root, root->leafsize,
3555d4f98a2SYan Zheng 				      0, objectid, NULL, 0, 0, 0);
3568e8a1e31SJosef Bacik 	if (IS_ERR(leaf)) {
3578e8a1e31SJosef Bacik 		ret = PTR_ERR(leaf);
3588e8a1e31SJosef Bacik 		goto fail;
3598e8a1e31SJosef Bacik 	}
360f46b5a66SChristoph Hellwig 
3615d4f98a2SYan Zheng 	memset_extent_buffer(leaf, 0, 0, sizeof(struct btrfs_header));
362f46b5a66SChristoph Hellwig 	btrfs_set_header_bytenr(leaf, leaf->start);
363f46b5a66SChristoph Hellwig 	btrfs_set_header_generation(leaf, trans->transid);
3645d4f98a2SYan Zheng 	btrfs_set_header_backref_rev(leaf, BTRFS_MIXED_BACKREF_REV);
365f46b5a66SChristoph Hellwig 	btrfs_set_header_owner(leaf, objectid);
366f46b5a66SChristoph Hellwig 
367f46b5a66SChristoph Hellwig 	write_extent_buffer(leaf, root->fs_info->fsid,
368f46b5a66SChristoph Hellwig 			    (unsigned long)btrfs_header_fsid(leaf),
369f46b5a66SChristoph Hellwig 			    BTRFS_FSID_SIZE);
3705d4f98a2SYan Zheng 	write_extent_buffer(leaf, root->fs_info->chunk_tree_uuid,
3715d4f98a2SYan Zheng 			    (unsigned long)btrfs_header_chunk_tree_uuid(leaf),
3725d4f98a2SYan Zheng 			    BTRFS_UUID_SIZE);
373f46b5a66SChristoph Hellwig 	btrfs_mark_buffer_dirty(leaf);
374f46b5a66SChristoph Hellwig 
375f46b5a66SChristoph Hellwig 	inode_item = &root_item.inode;
376f46b5a66SChristoph Hellwig 	memset(inode_item, 0, sizeof(*inode_item));
377f46b5a66SChristoph Hellwig 	inode_item->generation = cpu_to_le64(1);
378f46b5a66SChristoph Hellwig 	inode_item->size = cpu_to_le64(3);
379f46b5a66SChristoph Hellwig 	inode_item->nlink = cpu_to_le32(1);
380a76a3cd4SYan Zheng 	inode_item->nbytes = cpu_to_le64(root->leafsize);
381f46b5a66SChristoph Hellwig 	inode_item->mode = cpu_to_le32(S_IFDIR | 0755);
382f46b5a66SChristoph Hellwig 
38308fe4db1SLi Zefan 	root_item.flags = 0;
38408fe4db1SLi Zefan 	root_item.byte_limit = 0;
38508fe4db1SLi Zefan 	inode_item->flags = cpu_to_le64(BTRFS_INODE_ROOT_ITEM_INIT);
38608fe4db1SLi Zefan 
387f46b5a66SChristoph Hellwig 	btrfs_set_root_bytenr(&root_item, leaf->start);
38884234f3aSYan Zheng 	btrfs_set_root_generation(&root_item, trans->transid);
389f46b5a66SChristoph Hellwig 	btrfs_set_root_level(&root_item, 0);
390f46b5a66SChristoph Hellwig 	btrfs_set_root_refs(&root_item, 1);
39186b9f2ecSYan, Zheng 	btrfs_set_root_used(&root_item, leaf->len);
39280ff3856SYan Zheng 	btrfs_set_root_last_snapshot(&root_item, 0);
393f46b5a66SChristoph Hellwig 
394f46b5a66SChristoph Hellwig 	memset(&root_item.drop_progress, 0, sizeof(root_item.drop_progress));
395f46b5a66SChristoph Hellwig 	root_item.drop_level = 0;
396f46b5a66SChristoph Hellwig 
397925baeddSChris Mason 	btrfs_tree_unlock(leaf);
398f46b5a66SChristoph Hellwig 	free_extent_buffer(leaf);
399f46b5a66SChristoph Hellwig 	leaf = NULL;
400f46b5a66SChristoph Hellwig 
401f46b5a66SChristoph Hellwig 	btrfs_set_root_dirid(&root_item, new_dirid);
402f46b5a66SChristoph Hellwig 
403f46b5a66SChristoph Hellwig 	key.objectid = objectid;
4045d4f98a2SYan Zheng 	key.offset = 0;
405f46b5a66SChristoph Hellwig 	btrfs_set_key_type(&key, BTRFS_ROOT_ITEM_KEY);
406f46b5a66SChristoph Hellwig 	ret = btrfs_insert_root(trans, root->fs_info->tree_root, &key,
407f46b5a66SChristoph Hellwig 				&root_item);
408f46b5a66SChristoph Hellwig 	if (ret)
409f46b5a66SChristoph Hellwig 		goto fail;
410f46b5a66SChristoph Hellwig 
41176dda93cSYan, Zheng 	key.offset = (u64)-1;
41276dda93cSYan, Zheng 	new_root = btrfs_read_fs_root_no_name(root->fs_info, &key);
41376dda93cSYan, Zheng 	BUG_ON(IS_ERR(new_root));
41476dda93cSYan, Zheng 
41576dda93cSYan, Zheng 	btrfs_record_root_in_trans(trans, new_root);
41676dda93cSYan, Zheng 
41776dda93cSYan, Zheng 	ret = btrfs_create_subvol_root(trans, new_root, new_dirid,
41876dda93cSYan, Zheng 				       BTRFS_I(dir)->block_group);
419f46b5a66SChristoph Hellwig 	/*
420f46b5a66SChristoph Hellwig 	 * insert the directory item
421f46b5a66SChristoph Hellwig 	 */
4223de4586cSChris Mason 	ret = btrfs_set_inode_index(dir, &index);
4233de4586cSChris Mason 	BUG_ON(ret);
4243de4586cSChris Mason 
4253de4586cSChris Mason 	ret = btrfs_insert_dir_item(trans, root,
42616cdcec7SMiao Xie 				    name, namelen, dir, &key,
4273de4586cSChris Mason 				    BTRFS_FT_DIR, index);
428f46b5a66SChristoph Hellwig 	if (ret)
429f46b5a66SChristoph Hellwig 		goto fail;
4300660b5afSChris Mason 
43152c26179SYan Zheng 	btrfs_i_size_write(dir, dir->i_size + namelen * 2);
43252c26179SYan Zheng 	ret = btrfs_update_inode(trans, root, dir);
43352c26179SYan Zheng 	BUG_ON(ret);
43452c26179SYan Zheng 
4350660b5afSChris Mason 	ret = btrfs_add_root_ref(trans, root->fs_info->tree_root,
4364df27c4dSYan, Zheng 				 objectid, root->root_key.objectid,
43733345d01SLi Zefan 				 btrfs_ino(dir), index, name, namelen);
43876dda93cSYan, Zheng 
4390660b5afSChris Mason 	BUG_ON(ret);
4400660b5afSChris Mason 
44176dda93cSYan, Zheng 	d_instantiate(dentry, btrfs_lookup_dentry(dir, dentry));
442f46b5a66SChristoph Hellwig fail:
4436a912213SJosef Bacik 	dput(parent);
44472fd032eSSage Weil 	if (async_transid) {
44572fd032eSSage Weil 		*async_transid = trans->transid;
44672fd032eSSage Weil 		err = btrfs_commit_transaction_async(trans, root, 1);
44772fd032eSSage Weil 	} else {
44876dda93cSYan, Zheng 		err = btrfs_commit_transaction(trans, root);
44972fd032eSSage Weil 	}
450f46b5a66SChristoph Hellwig 	if (err && !ret)
451f46b5a66SChristoph Hellwig 		ret = err;
452f46b5a66SChristoph Hellwig 	return ret;
453f46b5a66SChristoph Hellwig }
454f46b5a66SChristoph Hellwig 
45572fd032eSSage Weil static int create_snapshot(struct btrfs_root *root, struct dentry *dentry,
456b83cc969SLi Zefan 			   char *name, int namelen, u64 *async_transid,
457b83cc969SLi Zefan 			   bool readonly)
458f46b5a66SChristoph Hellwig {
4592e4bfab9SYan, Zheng 	struct inode *inode;
4606a912213SJosef Bacik 	struct dentry *parent;
461f46b5a66SChristoph Hellwig 	struct btrfs_pending_snapshot *pending_snapshot;
462f46b5a66SChristoph Hellwig 	struct btrfs_trans_handle *trans;
4632e4bfab9SYan, Zheng 	int ret;
464f46b5a66SChristoph Hellwig 
465f46b5a66SChristoph Hellwig 	if (!root->ref_cows)
466f46b5a66SChristoph Hellwig 		return -EINVAL;
467f46b5a66SChristoph Hellwig 
468a22285a6SYan, Zheng 	pending_snapshot = kzalloc(sizeof(*pending_snapshot), GFP_NOFS);
469a22285a6SYan, Zheng 	if (!pending_snapshot)
470a22285a6SYan, Zheng 		return -ENOMEM;
471a22285a6SYan, Zheng 
472a22285a6SYan, Zheng 	btrfs_init_block_rsv(&pending_snapshot->block_rsv);
473a22285a6SYan, Zheng 	pending_snapshot->dentry = dentry;
474a22285a6SYan, Zheng 	pending_snapshot->root = root;
475b83cc969SLi Zefan 	pending_snapshot->readonly = readonly;
476a22285a6SYan, Zheng 
477a22285a6SYan, Zheng 	trans = btrfs_start_transaction(root->fs_info->extent_root, 5);
478a22285a6SYan, Zheng 	if (IS_ERR(trans)) {
479a22285a6SYan, Zheng 		ret = PTR_ERR(trans);
480a22285a6SYan, Zheng 		goto fail;
481a22285a6SYan, Zheng 	}
482a22285a6SYan, Zheng 
483a22285a6SYan, Zheng 	ret = btrfs_snap_reserve_metadata(trans, pending_snapshot);
484a22285a6SYan, Zheng 	BUG_ON(ret);
485a22285a6SYan, Zheng 
486a22285a6SYan, Zheng 	list_add(&pending_snapshot->list,
487a22285a6SYan, Zheng 		 &trans->transaction->pending_snapshots);
48872fd032eSSage Weil 	if (async_transid) {
48972fd032eSSage Weil 		*async_transid = trans->transid;
49072fd032eSSage Weil 		ret = btrfs_commit_transaction_async(trans,
49172fd032eSSage Weil 				     root->fs_info->extent_root, 1);
49272fd032eSSage Weil 	} else {
49372fd032eSSage Weil 		ret = btrfs_commit_transaction(trans,
49472fd032eSSage Weil 					       root->fs_info->extent_root);
49572fd032eSSage Weil 	}
496a22285a6SYan, Zheng 	BUG_ON(ret);
497a22285a6SYan, Zheng 
498a22285a6SYan, Zheng 	ret = pending_snapshot->error;
499f46b5a66SChristoph Hellwig 	if (ret)
5002e4bfab9SYan, Zheng 		goto fail;
501f46b5a66SChristoph Hellwig 
50266b4ffd1SJosef Bacik 	ret = btrfs_orphan_cleanup(pending_snapshot->snap);
50366b4ffd1SJosef Bacik 	if (ret)
50466b4ffd1SJosef Bacik 		goto fail;
505f46b5a66SChristoph Hellwig 
5066a912213SJosef Bacik 	parent = dget_parent(dentry);
5076a912213SJosef Bacik 	inode = btrfs_lookup_dentry(parent->d_inode, dentry);
5086a912213SJosef Bacik 	dput(parent);
5092e4bfab9SYan, Zheng 	if (IS_ERR(inode)) {
5102e4bfab9SYan, Zheng 		ret = PTR_ERR(inode);
5112e4bfab9SYan, Zheng 		goto fail;
5122e4bfab9SYan, Zheng 	}
5132e4bfab9SYan, Zheng 	BUG_ON(!inode);
5142e4bfab9SYan, Zheng 	d_instantiate(dentry, inode);
5152e4bfab9SYan, Zheng 	ret = 0;
5162e4bfab9SYan, Zheng fail:
517a22285a6SYan, Zheng 	kfree(pending_snapshot);
518f46b5a66SChristoph Hellwig 	return ret;
519f46b5a66SChristoph Hellwig }
520f46b5a66SChristoph Hellwig 
5214260f7c7SSage Weil /*  copy of check_sticky in fs/namei.c()
5224260f7c7SSage Weil * It's inline, so penalty for filesystems that don't use sticky bit is
5234260f7c7SSage Weil * minimal.
5244260f7c7SSage Weil */
5254260f7c7SSage Weil static inline int btrfs_check_sticky(struct inode *dir, struct inode *inode)
5264260f7c7SSage Weil {
5274260f7c7SSage Weil 	uid_t fsuid = current_fsuid();
5284260f7c7SSage Weil 
5294260f7c7SSage Weil 	if (!(dir->i_mode & S_ISVTX))
5304260f7c7SSage Weil 		return 0;
5314260f7c7SSage Weil 	if (inode->i_uid == fsuid)
5324260f7c7SSage Weil 		return 0;
5334260f7c7SSage Weil 	if (dir->i_uid == fsuid)
5344260f7c7SSage Weil 		return 0;
5354260f7c7SSage Weil 	return !capable(CAP_FOWNER);
5364260f7c7SSage Weil }
5374260f7c7SSage Weil 
5384260f7c7SSage Weil /*  copy of may_delete in fs/namei.c()
5394260f7c7SSage Weil  *	Check whether we can remove a link victim from directory dir, check
5404260f7c7SSage Weil  *  whether the type of victim is right.
5414260f7c7SSage Weil  *  1. We can't do it if dir is read-only (done in permission())
5424260f7c7SSage Weil  *  2. We should have write and exec permissions on dir
5434260f7c7SSage Weil  *  3. We can't remove anything from append-only dir
5444260f7c7SSage Weil  *  4. We can't do anything with immutable dir (done in permission())
5454260f7c7SSage Weil  *  5. If the sticky bit on dir is set we should either
5464260f7c7SSage Weil  *	a. be owner of dir, or
5474260f7c7SSage Weil  *	b. be owner of victim, or
5484260f7c7SSage Weil  *	c. have CAP_FOWNER capability
5494260f7c7SSage Weil  *  6. If the victim is append-only or immutable we can't do antyhing with
5504260f7c7SSage Weil  *     links pointing to it.
5514260f7c7SSage Weil  *  7. If we were asked to remove a directory and victim isn't one - ENOTDIR.
5524260f7c7SSage Weil  *  8. If we were asked to remove a non-directory and victim isn't one - EISDIR.
5534260f7c7SSage Weil  *  9. We can't remove a root or mountpoint.
5544260f7c7SSage Weil  * 10. We don't allow removal of NFS sillyrenamed files; it's handled by
5554260f7c7SSage Weil  *     nfs_async_unlink().
5564260f7c7SSage Weil  */
5574260f7c7SSage Weil 
5584260f7c7SSage Weil static int btrfs_may_delete(struct inode *dir,struct dentry *victim,int isdir)
5594260f7c7SSage Weil {
5604260f7c7SSage Weil 	int error;
5614260f7c7SSage Weil 
5624260f7c7SSage Weil 	if (!victim->d_inode)
5634260f7c7SSage Weil 		return -ENOENT;
5644260f7c7SSage Weil 
5654260f7c7SSage Weil 	BUG_ON(victim->d_parent->d_inode != dir);
5664260f7c7SSage Weil 	audit_inode_child(victim, dir);
5674260f7c7SSage Weil 
5684260f7c7SSage Weil 	error = inode_permission(dir, MAY_WRITE | MAY_EXEC);
5694260f7c7SSage Weil 	if (error)
5704260f7c7SSage Weil 		return error;
5714260f7c7SSage Weil 	if (IS_APPEND(dir))
5724260f7c7SSage Weil 		return -EPERM;
5734260f7c7SSage Weil 	if (btrfs_check_sticky(dir, victim->d_inode)||
5744260f7c7SSage Weil 		IS_APPEND(victim->d_inode)||
5754260f7c7SSage Weil 	    IS_IMMUTABLE(victim->d_inode) || IS_SWAPFILE(victim->d_inode))
5764260f7c7SSage Weil 		return -EPERM;
5774260f7c7SSage Weil 	if (isdir) {
5784260f7c7SSage Weil 		if (!S_ISDIR(victim->d_inode->i_mode))
5794260f7c7SSage Weil 			return -ENOTDIR;
5804260f7c7SSage Weil 		if (IS_ROOT(victim))
5814260f7c7SSage Weil 			return -EBUSY;
5824260f7c7SSage Weil 	} else if (S_ISDIR(victim->d_inode->i_mode))
5834260f7c7SSage Weil 		return -EISDIR;
5844260f7c7SSage Weil 	if (IS_DEADDIR(dir))
5854260f7c7SSage Weil 		return -ENOENT;
5864260f7c7SSage Weil 	if (victim->d_flags & DCACHE_NFSFS_RENAMED)
5874260f7c7SSage Weil 		return -EBUSY;
5884260f7c7SSage Weil 	return 0;
5894260f7c7SSage Weil }
5904260f7c7SSage Weil 
591cb8e7090SChristoph Hellwig /* copy of may_create in fs/namei.c() */
592cb8e7090SChristoph Hellwig static inline int btrfs_may_create(struct inode *dir, struct dentry *child)
593cb8e7090SChristoph Hellwig {
594cb8e7090SChristoph Hellwig 	if (child->d_inode)
595cb8e7090SChristoph Hellwig 		return -EEXIST;
596cb8e7090SChristoph Hellwig 	if (IS_DEADDIR(dir))
597cb8e7090SChristoph Hellwig 		return -ENOENT;
598cb8e7090SChristoph Hellwig 	return inode_permission(dir, MAY_WRITE | MAY_EXEC);
599cb8e7090SChristoph Hellwig }
600cb8e7090SChristoph Hellwig 
601cb8e7090SChristoph Hellwig /*
602cb8e7090SChristoph Hellwig  * Create a new subvolume below @parent.  This is largely modeled after
603cb8e7090SChristoph Hellwig  * sys_mkdirat and vfs_mkdir, but we only do a single component lookup
604cb8e7090SChristoph Hellwig  * inside this filesystem so it's quite a bit simpler.
605cb8e7090SChristoph Hellwig  */
60676dda93cSYan, Zheng static noinline int btrfs_mksubvol(struct path *parent,
60776dda93cSYan, Zheng 				   char *name, int namelen,
60872fd032eSSage Weil 				   struct btrfs_root *snap_src,
609b83cc969SLi Zefan 				   u64 *async_transid, bool readonly)
610cb8e7090SChristoph Hellwig {
61176dda93cSYan, Zheng 	struct inode *dir  = parent->dentry->d_inode;
612cb8e7090SChristoph Hellwig 	struct dentry *dentry;
613cb8e7090SChristoph Hellwig 	int error;
614cb8e7090SChristoph Hellwig 
61576dda93cSYan, Zheng 	mutex_lock_nested(&dir->i_mutex, I_MUTEX_PARENT);
616cb8e7090SChristoph Hellwig 
617cb8e7090SChristoph Hellwig 	dentry = lookup_one_len(name, parent->dentry, namelen);
618cb8e7090SChristoph Hellwig 	error = PTR_ERR(dentry);
619cb8e7090SChristoph Hellwig 	if (IS_ERR(dentry))
620cb8e7090SChristoph Hellwig 		goto out_unlock;
621cb8e7090SChristoph Hellwig 
622cb8e7090SChristoph Hellwig 	error = -EEXIST;
623cb8e7090SChristoph Hellwig 	if (dentry->d_inode)
624cb8e7090SChristoph Hellwig 		goto out_dput;
625cb8e7090SChristoph Hellwig 
626cb8e7090SChristoph Hellwig 	error = mnt_want_write(parent->mnt);
627cb8e7090SChristoph Hellwig 	if (error)
628cb8e7090SChristoph Hellwig 		goto out_dput;
629cb8e7090SChristoph Hellwig 
63076dda93cSYan, Zheng 	error = btrfs_may_create(dir, dentry);
631cb8e7090SChristoph Hellwig 	if (error)
632cb8e7090SChristoph Hellwig 		goto out_drop_write;
633cb8e7090SChristoph Hellwig 
63476dda93cSYan, Zheng 	down_read(&BTRFS_I(dir)->root->fs_info->subvol_sem);
63576dda93cSYan, Zheng 
63676dda93cSYan, Zheng 	if (btrfs_root_refs(&BTRFS_I(dir)->root->root_item) == 0)
63776dda93cSYan, Zheng 		goto out_up_read;
63876dda93cSYan, Zheng 
6393de4586cSChris Mason 	if (snap_src) {
64072fd032eSSage Weil 		error = create_snapshot(snap_src, dentry,
641b83cc969SLi Zefan 					name, namelen, async_transid, readonly);
6423de4586cSChris Mason 	} else {
64376dda93cSYan, Zheng 		error = create_subvol(BTRFS_I(dir)->root, dentry,
64472fd032eSSage Weil 				      name, namelen, async_transid);
6453de4586cSChris Mason 	}
64676dda93cSYan, Zheng 	if (!error)
64776dda93cSYan, Zheng 		fsnotify_mkdir(dir, dentry);
64876dda93cSYan, Zheng out_up_read:
64976dda93cSYan, Zheng 	up_read(&BTRFS_I(dir)->root->fs_info->subvol_sem);
650cb8e7090SChristoph Hellwig out_drop_write:
651cb8e7090SChristoph Hellwig 	mnt_drop_write(parent->mnt);
652cb8e7090SChristoph Hellwig out_dput:
653cb8e7090SChristoph Hellwig 	dput(dentry);
654cb8e7090SChristoph Hellwig out_unlock:
65576dda93cSYan, Zheng 	mutex_unlock(&dir->i_mutex);
656cb8e7090SChristoph Hellwig 	return error;
657cb8e7090SChristoph Hellwig }
658cb8e7090SChristoph Hellwig 
6594cb5300bSChris Mason /*
6604cb5300bSChris Mason  * When we're defragging a range, we don't want to kick it off again
6614cb5300bSChris Mason  * if it is really just waiting for delalloc to send it down.
6624cb5300bSChris Mason  * If we find a nice big extent or delalloc range for the bytes in the
6634cb5300bSChris Mason  * file you want to defrag, we return 0 to let you know to skip this
6644cb5300bSChris Mason  * part of the file
6654cb5300bSChris Mason  */
6664cb5300bSChris Mason static int check_defrag_in_cache(struct inode *inode, u64 offset, int thresh)
6674cb5300bSChris Mason {
6684cb5300bSChris Mason 	struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
6694cb5300bSChris Mason 	struct extent_map *em = NULL;
6704cb5300bSChris Mason 	struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
6714cb5300bSChris Mason 	u64 end;
6724cb5300bSChris Mason 
6734cb5300bSChris Mason 	read_lock(&em_tree->lock);
6744cb5300bSChris Mason 	em = lookup_extent_mapping(em_tree, offset, PAGE_CACHE_SIZE);
6754cb5300bSChris Mason 	read_unlock(&em_tree->lock);
6764cb5300bSChris Mason 
6774cb5300bSChris Mason 	if (em) {
6784cb5300bSChris Mason 		end = extent_map_end(em);
6794cb5300bSChris Mason 		free_extent_map(em);
6804cb5300bSChris Mason 		if (end - offset > thresh)
6814cb5300bSChris Mason 			return 0;
6824cb5300bSChris Mason 	}
6834cb5300bSChris Mason 	/* if we already have a nice delalloc here, just stop */
6844cb5300bSChris Mason 	thresh /= 2;
6854cb5300bSChris Mason 	end = count_range_bits(io_tree, &offset, offset + thresh,
6864cb5300bSChris Mason 			       thresh, EXTENT_DELALLOC, 1);
6874cb5300bSChris Mason 	if (end >= thresh)
6884cb5300bSChris Mason 		return 0;
6894cb5300bSChris Mason 	return 1;
6904cb5300bSChris Mason }
6914cb5300bSChris Mason 
6924cb5300bSChris Mason /*
6934cb5300bSChris Mason  * helper function to walk through a file and find extents
6944cb5300bSChris Mason  * newer than a specific transid, and smaller than thresh.
6954cb5300bSChris Mason  *
6964cb5300bSChris Mason  * This is used by the defragging code to find new and small
6974cb5300bSChris Mason  * extents
6984cb5300bSChris Mason  */
6994cb5300bSChris Mason static int find_new_extents(struct btrfs_root *root,
7004cb5300bSChris Mason 			    struct inode *inode, u64 newer_than,
7014cb5300bSChris Mason 			    u64 *off, int thresh)
7024cb5300bSChris Mason {
7034cb5300bSChris Mason 	struct btrfs_path *path;
7044cb5300bSChris Mason 	struct btrfs_key min_key;
7054cb5300bSChris Mason 	struct btrfs_key max_key;
7064cb5300bSChris Mason 	struct extent_buffer *leaf;
7074cb5300bSChris Mason 	struct btrfs_file_extent_item *extent;
7084cb5300bSChris Mason 	int type;
7094cb5300bSChris Mason 	int ret;
7104cb5300bSChris Mason 
7114cb5300bSChris Mason 	path = btrfs_alloc_path();
7124cb5300bSChris Mason 	if (!path)
7134cb5300bSChris Mason 		return -ENOMEM;
7144cb5300bSChris Mason 
7154cb5300bSChris Mason 	min_key.objectid = inode->i_ino;
7164cb5300bSChris Mason 	min_key.type = BTRFS_EXTENT_DATA_KEY;
7174cb5300bSChris Mason 	min_key.offset = *off;
7184cb5300bSChris Mason 
7194cb5300bSChris Mason 	max_key.objectid = inode->i_ino;
7204cb5300bSChris Mason 	max_key.type = (u8)-1;
7214cb5300bSChris Mason 	max_key.offset = (u64)-1;
7224cb5300bSChris Mason 
7234cb5300bSChris Mason 	path->keep_locks = 1;
7244cb5300bSChris Mason 
7254cb5300bSChris Mason 	while(1) {
7264cb5300bSChris Mason 		ret = btrfs_search_forward(root, &min_key, &max_key,
7274cb5300bSChris Mason 					   path, 0, newer_than);
7284cb5300bSChris Mason 		if (ret != 0)
7294cb5300bSChris Mason 			goto none;
7304cb5300bSChris Mason 		if (min_key.objectid != inode->i_ino)
7314cb5300bSChris Mason 			goto none;
7324cb5300bSChris Mason 		if (min_key.type != BTRFS_EXTENT_DATA_KEY)
7334cb5300bSChris Mason 			goto none;
7344cb5300bSChris Mason 
7354cb5300bSChris Mason 		leaf = path->nodes[0];
7364cb5300bSChris Mason 		extent = btrfs_item_ptr(leaf, path->slots[0],
7374cb5300bSChris Mason 					struct btrfs_file_extent_item);
7384cb5300bSChris Mason 
7394cb5300bSChris Mason 		type = btrfs_file_extent_type(leaf, extent);
7404cb5300bSChris Mason 		if (type == BTRFS_FILE_EXTENT_REG &&
7414cb5300bSChris Mason 		    btrfs_file_extent_num_bytes(leaf, extent) < thresh &&
7424cb5300bSChris Mason 		    check_defrag_in_cache(inode, min_key.offset, thresh)) {
7434cb5300bSChris Mason 			*off = min_key.offset;
7444cb5300bSChris Mason 			btrfs_free_path(path);
7454cb5300bSChris Mason 			return 0;
7464cb5300bSChris Mason 		}
7474cb5300bSChris Mason 
7484cb5300bSChris Mason 		if (min_key.offset == (u64)-1)
7494cb5300bSChris Mason 			goto none;
7504cb5300bSChris Mason 
7514cb5300bSChris Mason 		min_key.offset++;
7524cb5300bSChris Mason 		btrfs_release_path(path);
7534cb5300bSChris Mason 	}
7544cb5300bSChris Mason none:
7554cb5300bSChris Mason 	btrfs_free_path(path);
7564cb5300bSChris Mason 	return -ENOENT;
7574cb5300bSChris Mason }
7584cb5300bSChris Mason 
759940100a4SChris Mason static int should_defrag_range(struct inode *inode, u64 start, u64 len,
7601e701a32SChris Mason 			       int thresh, u64 *last_len, u64 *skip,
7611e701a32SChris Mason 			       u64 *defrag_end)
762940100a4SChris Mason {
763940100a4SChris Mason 	struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
764940100a4SChris Mason 	struct extent_map *em = NULL;
765940100a4SChris Mason 	struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
766940100a4SChris Mason 	int ret = 1;
767940100a4SChris Mason 
768940100a4SChris Mason 	/*
769940100a4SChris Mason 	 * make sure that once we start defragging and extent, we keep on
770940100a4SChris Mason 	 * defragging it
771940100a4SChris Mason 	 */
772940100a4SChris Mason 	if (start < *defrag_end)
773940100a4SChris Mason 		return 1;
774940100a4SChris Mason 
775940100a4SChris Mason 	*skip = 0;
776940100a4SChris Mason 
777940100a4SChris Mason 	/*
778940100a4SChris Mason 	 * hopefully we have this extent in the tree already, try without
779940100a4SChris Mason 	 * the full extent lock
780940100a4SChris Mason 	 */
781940100a4SChris Mason 	read_lock(&em_tree->lock);
782940100a4SChris Mason 	em = lookup_extent_mapping(em_tree, start, len);
783940100a4SChris Mason 	read_unlock(&em_tree->lock);
784940100a4SChris Mason 
785940100a4SChris Mason 	if (!em) {
786940100a4SChris Mason 		/* get the big lock and read metadata off disk */
787940100a4SChris Mason 		lock_extent(io_tree, start, start + len - 1, GFP_NOFS);
788940100a4SChris Mason 		em = btrfs_get_extent(inode, NULL, 0, start, len, 0);
789940100a4SChris Mason 		unlock_extent(io_tree, start, start + len - 1, GFP_NOFS);
790940100a4SChris Mason 
7916cf8bfbfSDan Carpenter 		if (IS_ERR(em))
792940100a4SChris Mason 			return 0;
793940100a4SChris Mason 	}
794940100a4SChris Mason 
795940100a4SChris Mason 	/* this will cover holes, and inline extents */
796940100a4SChris Mason 	if (em->block_start >= EXTENT_MAP_LAST_BYTE)
797940100a4SChris Mason 		ret = 0;
798940100a4SChris Mason 
799940100a4SChris Mason 	/*
800940100a4SChris Mason 	 * we hit a real extent, if it is big don't bother defragging it again
801940100a4SChris Mason 	 */
8021e701a32SChris Mason 	if ((*last_len == 0 || *last_len >= thresh) && em->len >= thresh)
803940100a4SChris Mason 		ret = 0;
804940100a4SChris Mason 
805940100a4SChris Mason 	/*
806940100a4SChris Mason 	 * last_len ends up being a counter of how many bytes we've defragged.
807940100a4SChris Mason 	 * every time we choose not to defrag an extent, we reset *last_len
808940100a4SChris Mason 	 * so that the next tiny extent will force a defrag.
809940100a4SChris Mason 	 *
810940100a4SChris Mason 	 * The end result of this is that tiny extents before a single big
811940100a4SChris Mason 	 * extent will force at least part of that big extent to be defragged.
812940100a4SChris Mason 	 */
813940100a4SChris Mason 	if (ret) {
814940100a4SChris Mason 		*last_len += len;
815940100a4SChris Mason 		*defrag_end = extent_map_end(em);
816940100a4SChris Mason 	} else {
817940100a4SChris Mason 		*last_len = 0;
818940100a4SChris Mason 		*skip = extent_map_end(em);
819940100a4SChris Mason 		*defrag_end = 0;
820940100a4SChris Mason 	}
821940100a4SChris Mason 
822940100a4SChris Mason 	free_extent_map(em);
823940100a4SChris Mason 	return ret;
824940100a4SChris Mason }
825940100a4SChris Mason 
8264cb5300bSChris Mason /*
8274cb5300bSChris Mason  * it doesn't do much good to defrag one or two pages
8284cb5300bSChris Mason  * at a time.  This pulls in a nice chunk of pages
8294cb5300bSChris Mason  * to COW and defrag.
8304cb5300bSChris Mason  *
8314cb5300bSChris Mason  * It also makes sure the delalloc code has enough
8324cb5300bSChris Mason  * dirty data to avoid making new small extents as part
8334cb5300bSChris Mason  * of the defrag
8344cb5300bSChris Mason  *
8354cb5300bSChris Mason  * It's a good idea to start RA on this range
8364cb5300bSChris Mason  * before calling this.
8374cb5300bSChris Mason  */
8384cb5300bSChris Mason static int cluster_pages_for_defrag(struct inode *inode,
8394cb5300bSChris Mason 				    struct page **pages,
8404cb5300bSChris Mason 				    unsigned long start_index,
8414cb5300bSChris Mason 				    int num_pages)
842f46b5a66SChristoph Hellwig {
8434cb5300bSChris Mason 	unsigned long file_end;
8444cb5300bSChris Mason 	u64 isize = i_size_read(inode);
845f46b5a66SChristoph Hellwig 	u64 page_start;
846f46b5a66SChristoph Hellwig 	u64 page_end;
8474cb5300bSChris Mason 	int ret;
8484cb5300bSChris Mason 	int i;
8494cb5300bSChris Mason 	int i_done;
8504cb5300bSChris Mason 	struct btrfs_ordered_extent *ordered;
8514cb5300bSChris Mason 	struct extent_state *cached_state = NULL;
8524cb5300bSChris Mason 
8534cb5300bSChris Mason 	if (isize == 0)
8544cb5300bSChris Mason 		return 0;
8554cb5300bSChris Mason 	file_end = (isize - 1) >> PAGE_CACHE_SHIFT;
8564cb5300bSChris Mason 
8574cb5300bSChris Mason 	ret = btrfs_delalloc_reserve_space(inode,
8584cb5300bSChris Mason 					   num_pages << PAGE_CACHE_SHIFT);
8594cb5300bSChris Mason 	if (ret)
8604cb5300bSChris Mason 		return ret;
8614cb5300bSChris Mason again:
8624cb5300bSChris Mason 	ret = 0;
8634cb5300bSChris Mason 	i_done = 0;
8644cb5300bSChris Mason 
8654cb5300bSChris Mason 	/* step one, lock all the pages */
8664cb5300bSChris Mason 	for (i = 0; i < num_pages; i++) {
8674cb5300bSChris Mason 		struct page *page;
8684cb5300bSChris Mason 		page = grab_cache_page(inode->i_mapping,
8694cb5300bSChris Mason 					    start_index + i);
8704cb5300bSChris Mason 		if (!page)
8714cb5300bSChris Mason 			break;
8724cb5300bSChris Mason 
8734cb5300bSChris Mason 		if (!PageUptodate(page)) {
8744cb5300bSChris Mason 			btrfs_readpage(NULL, page);
8754cb5300bSChris Mason 			lock_page(page);
8764cb5300bSChris Mason 			if (!PageUptodate(page)) {
8774cb5300bSChris Mason 				unlock_page(page);
8784cb5300bSChris Mason 				page_cache_release(page);
8794cb5300bSChris Mason 				ret = -EIO;
8804cb5300bSChris Mason 				break;
8814cb5300bSChris Mason 			}
8824cb5300bSChris Mason 		}
8834cb5300bSChris Mason 		isize = i_size_read(inode);
8844cb5300bSChris Mason 		file_end = (isize - 1) >> PAGE_CACHE_SHIFT;
8854cb5300bSChris Mason 		if (!isize || page->index > file_end ||
8864cb5300bSChris Mason 		    page->mapping != inode->i_mapping) {
8874cb5300bSChris Mason 			/* whoops, we blew past eof, skip this page */
8884cb5300bSChris Mason 			unlock_page(page);
8894cb5300bSChris Mason 			page_cache_release(page);
8904cb5300bSChris Mason 			break;
8914cb5300bSChris Mason 		}
8924cb5300bSChris Mason 		pages[i] = page;
8934cb5300bSChris Mason 		i_done++;
8944cb5300bSChris Mason 	}
8954cb5300bSChris Mason 	if (!i_done || ret)
8964cb5300bSChris Mason 		goto out;
8974cb5300bSChris Mason 
8984cb5300bSChris Mason 	if (!(inode->i_sb->s_flags & MS_ACTIVE))
8994cb5300bSChris Mason 		goto out;
9004cb5300bSChris Mason 
9014cb5300bSChris Mason 	/*
9024cb5300bSChris Mason 	 * so now we have a nice long stream of locked
9034cb5300bSChris Mason 	 * and up to date pages, lets wait on them
9044cb5300bSChris Mason 	 */
9054cb5300bSChris Mason 	for (i = 0; i < i_done; i++)
9064cb5300bSChris Mason 		wait_on_page_writeback(pages[i]);
9074cb5300bSChris Mason 
9084cb5300bSChris Mason 	page_start = page_offset(pages[0]);
9094cb5300bSChris Mason 	page_end = page_offset(pages[i_done - 1]) + PAGE_CACHE_SIZE;
9104cb5300bSChris Mason 
9114cb5300bSChris Mason 	lock_extent_bits(&BTRFS_I(inode)->io_tree,
9124cb5300bSChris Mason 			 page_start, page_end - 1, 0, &cached_state,
9134cb5300bSChris Mason 			 GFP_NOFS);
9144cb5300bSChris Mason 	ordered = btrfs_lookup_first_ordered_extent(inode, page_end - 1);
9154cb5300bSChris Mason 	if (ordered &&
9164cb5300bSChris Mason 	    ordered->file_offset + ordered->len > page_start &&
9174cb5300bSChris Mason 	    ordered->file_offset < page_end) {
9184cb5300bSChris Mason 		btrfs_put_ordered_extent(ordered);
9194cb5300bSChris Mason 		unlock_extent_cached(&BTRFS_I(inode)->io_tree,
9204cb5300bSChris Mason 				     page_start, page_end - 1,
9214cb5300bSChris Mason 				     &cached_state, GFP_NOFS);
9224cb5300bSChris Mason 		for (i = 0; i < i_done; i++) {
9234cb5300bSChris Mason 			unlock_page(pages[i]);
9244cb5300bSChris Mason 			page_cache_release(pages[i]);
9254cb5300bSChris Mason 		}
9264cb5300bSChris Mason 		btrfs_wait_ordered_range(inode, page_start,
9274cb5300bSChris Mason 					 page_end - page_start);
9284cb5300bSChris Mason 		goto again;
9294cb5300bSChris Mason 	}
9304cb5300bSChris Mason 	if (ordered)
9314cb5300bSChris Mason 		btrfs_put_ordered_extent(ordered);
9324cb5300bSChris Mason 
9334cb5300bSChris Mason 	clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start,
9344cb5300bSChris Mason 			  page_end - 1, EXTENT_DIRTY | EXTENT_DELALLOC |
9354cb5300bSChris Mason 			  EXTENT_DO_ACCOUNTING, 0, 0, &cached_state,
9364cb5300bSChris Mason 			  GFP_NOFS);
9374cb5300bSChris Mason 
9384cb5300bSChris Mason 	if (i_done != num_pages) {
9394cb5300bSChris Mason 		atomic_inc(&BTRFS_I(inode)->outstanding_extents);
9404cb5300bSChris Mason 		btrfs_delalloc_release_space(inode,
9414cb5300bSChris Mason 				     (num_pages - i_done) << PAGE_CACHE_SHIFT);
9424cb5300bSChris Mason 	}
9434cb5300bSChris Mason 
9444cb5300bSChris Mason 
9454cb5300bSChris Mason 	btrfs_set_extent_delalloc(inode, page_start, page_end - 1,
9464cb5300bSChris Mason 				  &cached_state);
9474cb5300bSChris Mason 
9484cb5300bSChris Mason 	unlock_extent_cached(&BTRFS_I(inode)->io_tree,
9494cb5300bSChris Mason 			     page_start, page_end - 1, &cached_state,
9504cb5300bSChris Mason 			     GFP_NOFS);
9514cb5300bSChris Mason 
9524cb5300bSChris Mason 	for (i = 0; i < i_done; i++) {
9534cb5300bSChris Mason 		clear_page_dirty_for_io(pages[i]);
9544cb5300bSChris Mason 		ClearPageChecked(pages[i]);
9554cb5300bSChris Mason 		set_page_extent_mapped(pages[i]);
9564cb5300bSChris Mason 		set_page_dirty(pages[i]);
9574cb5300bSChris Mason 		unlock_page(pages[i]);
9584cb5300bSChris Mason 		page_cache_release(pages[i]);
9594cb5300bSChris Mason 	}
9604cb5300bSChris Mason 	return i_done;
9614cb5300bSChris Mason out:
9624cb5300bSChris Mason 	for (i = 0; i < i_done; i++) {
9634cb5300bSChris Mason 		unlock_page(pages[i]);
9644cb5300bSChris Mason 		page_cache_release(pages[i]);
9654cb5300bSChris Mason 	}
9664cb5300bSChris Mason 	btrfs_delalloc_release_space(inode, num_pages << PAGE_CACHE_SHIFT);
9674cb5300bSChris Mason 	return ret;
9684cb5300bSChris Mason 
9694cb5300bSChris Mason }
9704cb5300bSChris Mason 
9714cb5300bSChris Mason int btrfs_defrag_file(struct inode *inode, struct file *file,
9724cb5300bSChris Mason 		      struct btrfs_ioctl_defrag_range_args *range,
9734cb5300bSChris Mason 		      u64 newer_than, unsigned long max_to_defrag)
9744cb5300bSChris Mason {
9754cb5300bSChris Mason 	struct btrfs_root *root = BTRFS_I(inode)->root;
9764cb5300bSChris Mason 	struct btrfs_super_block *disk_super;
9774cb5300bSChris Mason 	struct file_ra_state *ra = NULL;
9784cb5300bSChris Mason 	unsigned long last_index;
9794cb5300bSChris Mason 	u64 features;
980940100a4SChris Mason 	u64 last_len = 0;
981940100a4SChris Mason 	u64 skip = 0;
982940100a4SChris Mason 	u64 defrag_end = 0;
9834cb5300bSChris Mason 	u64 newer_off = range->start;
9844cb5300bSChris Mason 	int newer_left = 0;
985f46b5a66SChristoph Hellwig 	unsigned long i;
986f46b5a66SChristoph Hellwig 	int ret;
9874cb5300bSChris Mason 	int defrag_count = 0;
9881a419d85SLi Zefan 	int compress_type = BTRFS_COMPRESS_ZLIB;
9894cb5300bSChris Mason 	int extent_thresh = range->extent_thresh;
9904cb5300bSChris Mason 	int newer_cluster = (256 * 1024) >> PAGE_CACHE_SHIFT;
9914cb5300bSChris Mason 	u64 new_align = ~((u64)128 * 1024 - 1);
9924cb5300bSChris Mason 	struct page **pages = NULL;
9934cb5300bSChris Mason 
9944cb5300bSChris Mason 	if (extent_thresh == 0)
9954cb5300bSChris Mason 		extent_thresh = 256 * 1024;
9961a419d85SLi Zefan 
9971a419d85SLi Zefan 	if (range->flags & BTRFS_DEFRAG_RANGE_COMPRESS) {
9981a419d85SLi Zefan 		if (range->compress_type > BTRFS_COMPRESS_TYPES)
9991a419d85SLi Zefan 			return -EINVAL;
10001a419d85SLi Zefan 		if (range->compress_type)
10011a419d85SLi Zefan 			compress_type = range->compress_type;
10021a419d85SLi Zefan 	}
1003f46b5a66SChristoph Hellwig 
1004940100a4SChris Mason 	if (inode->i_size == 0)
1005940100a4SChris Mason 		return 0;
1006f46b5a66SChristoph Hellwig 
10074cb5300bSChris Mason 	/*
10084cb5300bSChris Mason 	 * if we were not given a file, allocate a readahead
10094cb5300bSChris Mason 	 * context
10104cb5300bSChris Mason 	 */
10114cb5300bSChris Mason 	if (!file) {
10124cb5300bSChris Mason 		ra = kzalloc(sizeof(*ra), GFP_NOFS);
10134cb5300bSChris Mason 		if (!ra)
10144cb5300bSChris Mason 			return -ENOMEM;
10154cb5300bSChris Mason 		file_ra_state_init(ra, inode->i_mapping);
10164cb5300bSChris Mason 	} else {
10174cb5300bSChris Mason 		ra = &file->f_ra;
10184cb5300bSChris Mason 	}
10194cb5300bSChris Mason 
10204cb5300bSChris Mason 	pages = kmalloc(sizeof(struct page *) * newer_cluster,
10214cb5300bSChris Mason 			GFP_NOFS);
10224cb5300bSChris Mason 	if (!pages) {
10234cb5300bSChris Mason 		ret = -ENOMEM;
10244cb5300bSChris Mason 		goto out_ra;
10254cb5300bSChris Mason 	}
10264cb5300bSChris Mason 
10274cb5300bSChris Mason 	/* find the last page to defrag */
10281e701a32SChris Mason 	if (range->start + range->len > range->start) {
10291e701a32SChris Mason 		last_index = min_t(u64, inode->i_size - 1,
10301e701a32SChris Mason 			 range->start + range->len - 1) >> PAGE_CACHE_SHIFT;
10311e701a32SChris Mason 	} else {
1032940100a4SChris Mason 		last_index = (inode->i_size - 1) >> PAGE_CACHE_SHIFT;
10331e701a32SChris Mason 	}
10341e701a32SChris Mason 
10354cb5300bSChris Mason 	if (newer_than) {
10364cb5300bSChris Mason 		ret = find_new_extents(root, inode, newer_than,
10374cb5300bSChris Mason 				       &newer_off, 64 * 1024);
10384cb5300bSChris Mason 		if (!ret) {
10394cb5300bSChris Mason 			range->start = newer_off;
10404cb5300bSChris Mason 			/*
10414cb5300bSChris Mason 			 * we always align our defrag to help keep
10424cb5300bSChris Mason 			 * the extents in the file evenly spaced
10434cb5300bSChris Mason 			 */
10444cb5300bSChris Mason 			i = (newer_off & new_align) >> PAGE_CACHE_SHIFT;
10454cb5300bSChris Mason 			newer_left = newer_cluster;
10464cb5300bSChris Mason 		} else
10474cb5300bSChris Mason 			goto out_ra;
10484cb5300bSChris Mason 	} else {
10491e701a32SChris Mason 		i = range->start >> PAGE_CACHE_SHIFT;
10504cb5300bSChris Mason 	}
10514cb5300bSChris Mason 	if (!max_to_defrag)
10524cb5300bSChris Mason 		max_to_defrag = last_index - 1;
10534cb5300bSChris Mason 
10544cb5300bSChris Mason 	while (i <= last_index && defrag_count < max_to_defrag) {
10554cb5300bSChris Mason 		/*
10564cb5300bSChris Mason 		 * make sure we stop running if someone unmounts
10574cb5300bSChris Mason 		 * the FS
10584cb5300bSChris Mason 		 */
10594cb5300bSChris Mason 		if (!(inode->i_sb->s_flags & MS_ACTIVE))
10604cb5300bSChris Mason 			break;
10614cb5300bSChris Mason 
10624cb5300bSChris Mason 		if (!newer_than &&
10634cb5300bSChris Mason 		    !should_defrag_range(inode, (u64)i << PAGE_CACHE_SHIFT,
10641e701a32SChris Mason 					PAGE_CACHE_SIZE,
10654cb5300bSChris Mason 					extent_thresh,
10661e701a32SChris Mason 					&last_len, &skip,
1067940100a4SChris Mason 					&defrag_end)) {
1068940100a4SChris Mason 			unsigned long next;
1069940100a4SChris Mason 			/*
1070940100a4SChris Mason 			 * the should_defrag function tells us how much to skip
1071940100a4SChris Mason 			 * bump our counter by the suggested amount
1072940100a4SChris Mason 			 */
1073940100a4SChris Mason 			next = (skip + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
1074940100a4SChris Mason 			i = max(i + 1, next);
1075940100a4SChris Mason 			continue;
1076940100a4SChris Mason 		}
10771e701a32SChris Mason 		if (range->flags & BTRFS_DEFRAG_RANGE_COMPRESS)
10781a419d85SLi Zefan 			BTRFS_I(inode)->force_compress = compress_type;
1079940100a4SChris Mason 
10804cb5300bSChris Mason 		btrfs_force_ra(inode->i_mapping, ra, file, i, newer_cluster);
10814cb5300bSChris Mason 
10824cb5300bSChris Mason 		ret = cluster_pages_for_defrag(inode, pages, i, newer_cluster);
10834cb5300bSChris Mason 		if (ret < 0)
10844cb5300bSChris Mason 			goto out_ra;
10854cb5300bSChris Mason 
10864cb5300bSChris Mason 		defrag_count += ret;
10874cb5300bSChris Mason 		balance_dirty_pages_ratelimited_nr(inode->i_mapping, ret);
10884cb5300bSChris Mason 		i += ret;
10894cb5300bSChris Mason 
10904cb5300bSChris Mason 		if (newer_than) {
10914cb5300bSChris Mason 			if (newer_off == (u64)-1)
10924cb5300bSChris Mason 				break;
10934cb5300bSChris Mason 
10944cb5300bSChris Mason 			newer_off = max(newer_off + 1,
10954cb5300bSChris Mason 					(u64)i << PAGE_CACHE_SHIFT);
10964cb5300bSChris Mason 
10974cb5300bSChris Mason 			ret = find_new_extents(root, inode,
10984cb5300bSChris Mason 					       newer_than, &newer_off,
10994cb5300bSChris Mason 					       64 * 1024);
11004cb5300bSChris Mason 			if (!ret) {
11014cb5300bSChris Mason 				range->start = newer_off;
11024cb5300bSChris Mason 				i = (newer_off & new_align) >> PAGE_CACHE_SHIFT;
11034cb5300bSChris Mason 				newer_left = newer_cluster;
11044cb5300bSChris Mason 			} else {
11054cb5300bSChris Mason 				break;
1106940100a4SChris Mason 			}
11074cb5300bSChris Mason 		} else {
1108940100a4SChris Mason 			i++;
1109f46b5a66SChristoph Hellwig 		}
11104cb5300bSChris Mason 	}
1111f46b5a66SChristoph Hellwig 
11121e701a32SChris Mason 	if ((range->flags & BTRFS_DEFRAG_RANGE_START_IO))
11131e701a32SChris Mason 		filemap_flush(inode->i_mapping);
11141e701a32SChris Mason 
11151e701a32SChris Mason 	if ((range->flags & BTRFS_DEFRAG_RANGE_COMPRESS)) {
11161e701a32SChris Mason 		/* the filemap_flush will queue IO into the worker threads, but
11171e701a32SChris Mason 		 * we have to make sure the IO is actually started and that
11181e701a32SChris Mason 		 * ordered extents get created before we return
11191e701a32SChris Mason 		 */
11201e701a32SChris Mason 		atomic_inc(&root->fs_info->async_submit_draining);
11211e701a32SChris Mason 		while (atomic_read(&root->fs_info->nr_async_submits) ||
11221e701a32SChris Mason 		      atomic_read(&root->fs_info->async_delalloc_pages)) {
11231e701a32SChris Mason 			wait_event(root->fs_info->async_submit_wait,
11241e701a32SChris Mason 			   (atomic_read(&root->fs_info->nr_async_submits) == 0 &&
11251e701a32SChris Mason 			    atomic_read(&root->fs_info->async_delalloc_pages) == 0));
11261e701a32SChris Mason 		}
11271e701a32SChris Mason 		atomic_dec(&root->fs_info->async_submit_draining);
11281e701a32SChris Mason 
11291e701a32SChris Mason 		mutex_lock(&inode->i_mutex);
1130261507a0SLi Zefan 		BTRFS_I(inode)->force_compress = BTRFS_COMPRESS_NONE;
11311e701a32SChris Mason 		mutex_unlock(&inode->i_mutex);
11321e701a32SChris Mason 	}
11331e701a32SChris Mason 
11341a419d85SLi Zefan 	disk_super = &root->fs_info->super_copy;
11351a419d85SLi Zefan 	features = btrfs_super_incompat_flags(disk_super);
11361a419d85SLi Zefan 	if (range->compress_type == BTRFS_COMPRESS_LZO) {
11371a419d85SLi Zefan 		features |= BTRFS_FEATURE_INCOMPAT_COMPRESS_LZO;
11381a419d85SLi Zefan 		btrfs_set_super_incompat_flags(disk_super, features);
11391a419d85SLi Zefan 	}
11401a419d85SLi Zefan 
11414cb5300bSChris Mason 	if (!file)
11424cb5300bSChris Mason 		kfree(ra);
11434cb5300bSChris Mason 	return defrag_count;
1144940100a4SChris Mason 
11454cb5300bSChris Mason out_ra:
11464cb5300bSChris Mason 	if (!file)
11474cb5300bSChris Mason 		kfree(ra);
11484cb5300bSChris Mason 	kfree(pages);
1149940100a4SChris Mason 	return ret;
1150f46b5a66SChristoph Hellwig }
1151f46b5a66SChristoph Hellwig 
115276dda93cSYan, Zheng static noinline int btrfs_ioctl_resize(struct btrfs_root *root,
115376dda93cSYan, Zheng 					void __user *arg)
1154f46b5a66SChristoph Hellwig {
1155f46b5a66SChristoph Hellwig 	u64 new_size;
1156f46b5a66SChristoph Hellwig 	u64 old_size;
1157f46b5a66SChristoph Hellwig 	u64 devid = 1;
1158f46b5a66SChristoph Hellwig 	struct btrfs_ioctl_vol_args *vol_args;
1159f46b5a66SChristoph Hellwig 	struct btrfs_trans_handle *trans;
1160f46b5a66SChristoph Hellwig 	struct btrfs_device *device = NULL;
1161f46b5a66SChristoph Hellwig 	char *sizestr;
1162f46b5a66SChristoph Hellwig 	char *devstr = NULL;
1163f46b5a66SChristoph Hellwig 	int ret = 0;
1164f46b5a66SChristoph Hellwig 	int mod = 0;
1165f46b5a66SChristoph Hellwig 
1166c146afadSYan Zheng 	if (root->fs_info->sb->s_flags & MS_RDONLY)
1167c146afadSYan Zheng 		return -EROFS;
1168c146afadSYan Zheng 
1169e441d54dSChris Mason 	if (!capable(CAP_SYS_ADMIN))
1170e441d54dSChris Mason 		return -EPERM;
1171e441d54dSChris Mason 
1172dae7b665SLi Zefan 	vol_args = memdup_user(arg, sizeof(*vol_args));
1173dae7b665SLi Zefan 	if (IS_ERR(vol_args))
1174dae7b665SLi Zefan 		return PTR_ERR(vol_args);
11755516e595SMark Fasheh 
11765516e595SMark Fasheh 	vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
1177f46b5a66SChristoph Hellwig 
11787d9eb12cSChris Mason 	mutex_lock(&root->fs_info->volume_mutex);
1179f46b5a66SChristoph Hellwig 	sizestr = vol_args->name;
1180f46b5a66SChristoph Hellwig 	devstr = strchr(sizestr, ':');
1181f46b5a66SChristoph Hellwig 	if (devstr) {
1182f46b5a66SChristoph Hellwig 		char *end;
1183f46b5a66SChristoph Hellwig 		sizestr = devstr + 1;
1184f46b5a66SChristoph Hellwig 		*devstr = '\0';
1185f46b5a66SChristoph Hellwig 		devstr = vol_args->name;
1186f46b5a66SChristoph Hellwig 		devid = simple_strtoull(devstr, &end, 10);
118721380931SJoel Becker 		printk(KERN_INFO "resizing devid %llu\n",
118821380931SJoel Becker 		       (unsigned long long)devid);
1189f46b5a66SChristoph Hellwig 	}
11902b82032cSYan Zheng 	device = btrfs_find_device(root, devid, NULL, NULL);
1191f46b5a66SChristoph Hellwig 	if (!device) {
119221380931SJoel Becker 		printk(KERN_INFO "resizer unable to find device %llu\n",
119321380931SJoel Becker 		       (unsigned long long)devid);
1194f46b5a66SChristoph Hellwig 		ret = -EINVAL;
1195f46b5a66SChristoph Hellwig 		goto out_unlock;
1196f46b5a66SChristoph Hellwig 	}
1197f46b5a66SChristoph Hellwig 	if (!strcmp(sizestr, "max"))
1198f46b5a66SChristoph Hellwig 		new_size = device->bdev->bd_inode->i_size;
1199f46b5a66SChristoph Hellwig 	else {
1200f46b5a66SChristoph Hellwig 		if (sizestr[0] == '-') {
1201f46b5a66SChristoph Hellwig 			mod = -1;
1202f46b5a66SChristoph Hellwig 			sizestr++;
1203f46b5a66SChristoph Hellwig 		} else if (sizestr[0] == '+') {
1204f46b5a66SChristoph Hellwig 			mod = 1;
1205f46b5a66SChristoph Hellwig 			sizestr++;
1206f46b5a66SChristoph Hellwig 		}
120791748467SAkinobu Mita 		new_size = memparse(sizestr, NULL);
1208f46b5a66SChristoph Hellwig 		if (new_size == 0) {
1209f46b5a66SChristoph Hellwig 			ret = -EINVAL;
1210f46b5a66SChristoph Hellwig 			goto out_unlock;
1211f46b5a66SChristoph Hellwig 		}
1212f46b5a66SChristoph Hellwig 	}
1213f46b5a66SChristoph Hellwig 
1214f46b5a66SChristoph Hellwig 	old_size = device->total_bytes;
1215f46b5a66SChristoph Hellwig 
1216f46b5a66SChristoph Hellwig 	if (mod < 0) {
1217f46b5a66SChristoph Hellwig 		if (new_size > old_size) {
1218f46b5a66SChristoph Hellwig 			ret = -EINVAL;
1219f46b5a66SChristoph Hellwig 			goto out_unlock;
1220f46b5a66SChristoph Hellwig 		}
1221f46b5a66SChristoph Hellwig 		new_size = old_size - new_size;
1222f46b5a66SChristoph Hellwig 	} else if (mod > 0) {
1223f46b5a66SChristoph Hellwig 		new_size = old_size + new_size;
1224f46b5a66SChristoph Hellwig 	}
1225f46b5a66SChristoph Hellwig 
1226f46b5a66SChristoph Hellwig 	if (new_size < 256 * 1024 * 1024) {
1227f46b5a66SChristoph Hellwig 		ret = -EINVAL;
1228f46b5a66SChristoph Hellwig 		goto out_unlock;
1229f46b5a66SChristoph Hellwig 	}
1230f46b5a66SChristoph Hellwig 	if (new_size > device->bdev->bd_inode->i_size) {
1231f46b5a66SChristoph Hellwig 		ret = -EFBIG;
1232f46b5a66SChristoph Hellwig 		goto out_unlock;
1233f46b5a66SChristoph Hellwig 	}
1234f46b5a66SChristoph Hellwig 
1235f46b5a66SChristoph Hellwig 	do_div(new_size, root->sectorsize);
1236f46b5a66SChristoph Hellwig 	new_size *= root->sectorsize;
1237f46b5a66SChristoph Hellwig 
1238f46b5a66SChristoph Hellwig 	printk(KERN_INFO "new size for %s is %llu\n",
1239f46b5a66SChristoph Hellwig 		device->name, (unsigned long long)new_size);
1240f46b5a66SChristoph Hellwig 
1241f46b5a66SChristoph Hellwig 	if (new_size > old_size) {
1242a22285a6SYan, Zheng 		trans = btrfs_start_transaction(root, 0);
124398d5dc13STsutomu Itoh 		if (IS_ERR(trans)) {
124498d5dc13STsutomu Itoh 			ret = PTR_ERR(trans);
124598d5dc13STsutomu Itoh 			goto out_unlock;
124698d5dc13STsutomu Itoh 		}
1247f46b5a66SChristoph Hellwig 		ret = btrfs_grow_device(trans, device, new_size);
1248f46b5a66SChristoph Hellwig 		btrfs_commit_transaction(trans, root);
1249f46b5a66SChristoph Hellwig 	} else {
1250f46b5a66SChristoph Hellwig 		ret = btrfs_shrink_device(device, new_size);
1251f46b5a66SChristoph Hellwig 	}
1252f46b5a66SChristoph Hellwig 
1253f46b5a66SChristoph Hellwig out_unlock:
12547d9eb12cSChris Mason 	mutex_unlock(&root->fs_info->volume_mutex);
1255f46b5a66SChristoph Hellwig 	kfree(vol_args);
1256f46b5a66SChristoph Hellwig 	return ret;
1257f46b5a66SChristoph Hellwig }
1258f46b5a66SChristoph Hellwig 
125972fd032eSSage Weil static noinline int btrfs_ioctl_snap_create_transid(struct file *file,
126072fd032eSSage Weil 						    char *name,
126172fd032eSSage Weil 						    unsigned long fd,
126272fd032eSSage Weil 						    int subvol,
1263b83cc969SLi Zefan 						    u64 *transid,
1264b83cc969SLi Zefan 						    bool readonly)
1265f46b5a66SChristoph Hellwig {
1266cb8e7090SChristoph Hellwig 	struct btrfs_root *root = BTRFS_I(fdentry(file)->d_inode)->root;
12673de4586cSChris Mason 	struct file *src_file;
1268f46b5a66SChristoph Hellwig 	int namelen;
12693de4586cSChris Mason 	int ret = 0;
1270f46b5a66SChristoph Hellwig 
1271c146afadSYan Zheng 	if (root->fs_info->sb->s_flags & MS_RDONLY)
1272c146afadSYan Zheng 		return -EROFS;
1273c146afadSYan Zheng 
127472fd032eSSage Weil 	namelen = strlen(name);
127572fd032eSSage Weil 	if (strchr(name, '/')) {
1276f46b5a66SChristoph Hellwig 		ret = -EINVAL;
1277f46b5a66SChristoph Hellwig 		goto out;
1278f46b5a66SChristoph Hellwig 	}
1279f46b5a66SChristoph Hellwig 
12803de4586cSChris Mason 	if (subvol) {
128172fd032eSSage Weil 		ret = btrfs_mksubvol(&file->f_path, name, namelen,
1282b83cc969SLi Zefan 				     NULL, transid, readonly);
1283cb8e7090SChristoph Hellwig 	} else {
12843de4586cSChris Mason 		struct inode *src_inode;
128572fd032eSSage Weil 		src_file = fget(fd);
12863de4586cSChris Mason 		if (!src_file) {
12873de4586cSChris Mason 			ret = -EINVAL;
12883de4586cSChris Mason 			goto out;
12893de4586cSChris Mason 		}
12903de4586cSChris Mason 
12913de4586cSChris Mason 		src_inode = src_file->f_path.dentry->d_inode;
12923de4586cSChris Mason 		if (src_inode->i_sb != file->f_path.dentry->d_inode->i_sb) {
1293d397712bSChris Mason 			printk(KERN_INFO "btrfs: Snapshot src from "
1294d397712bSChris Mason 			       "another FS\n");
12953de4586cSChris Mason 			ret = -EINVAL;
12963de4586cSChris Mason 			fput(src_file);
12973de4586cSChris Mason 			goto out;
12983de4586cSChris Mason 		}
129972fd032eSSage Weil 		ret = btrfs_mksubvol(&file->f_path, name, namelen,
130072fd032eSSage Weil 				     BTRFS_I(src_inode)->root,
1301b83cc969SLi Zefan 				     transid, readonly);
13023de4586cSChris Mason 		fput(src_file);
1303cb8e7090SChristoph Hellwig 	}
1304f46b5a66SChristoph Hellwig out:
130572fd032eSSage Weil 	return ret;
130672fd032eSSage Weil }
130772fd032eSSage Weil 
130872fd032eSSage Weil static noinline int btrfs_ioctl_snap_create(struct file *file,
1309fa0d2b9bSLi Zefan 					    void __user *arg, int subvol)
131072fd032eSSage Weil {
1311fa0d2b9bSLi Zefan 	struct btrfs_ioctl_vol_args *vol_args;
131272fd032eSSage Weil 	int ret;
131372fd032eSSage Weil 
1314fa0d2b9bSLi Zefan 	vol_args = memdup_user(arg, sizeof(*vol_args));
1315fa0d2b9bSLi Zefan 	if (IS_ERR(vol_args))
1316fa0d2b9bSLi Zefan 		return PTR_ERR(vol_args);
1317fa0d2b9bSLi Zefan 	vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
1318fa0d2b9bSLi Zefan 
1319fa0d2b9bSLi Zefan 	ret = btrfs_ioctl_snap_create_transid(file, vol_args->name,
1320b83cc969SLi Zefan 					      vol_args->fd, subvol,
1321b83cc969SLi Zefan 					      NULL, false);
1322fa0d2b9bSLi Zefan 
1323fa0d2b9bSLi Zefan 	kfree(vol_args);
1324fa0d2b9bSLi Zefan 	return ret;
1325fa0d2b9bSLi Zefan }
1326fa0d2b9bSLi Zefan 
1327fa0d2b9bSLi Zefan static noinline int btrfs_ioctl_snap_create_v2(struct file *file,
1328fa0d2b9bSLi Zefan 					       void __user *arg, int subvol)
1329fa0d2b9bSLi Zefan {
1330fa0d2b9bSLi Zefan 	struct btrfs_ioctl_vol_args_v2 *vol_args;
1331fa0d2b9bSLi Zefan 	int ret;
1332fdfb1e4fSLi Zefan 	u64 transid = 0;
1333fdfb1e4fSLi Zefan 	u64 *ptr = NULL;
1334b83cc969SLi Zefan 	bool readonly = false;
133572fd032eSSage Weil 
1336fa0d2b9bSLi Zefan 	vol_args = memdup_user(arg, sizeof(*vol_args));
1337fa0d2b9bSLi Zefan 	if (IS_ERR(vol_args))
1338fa0d2b9bSLi Zefan 		return PTR_ERR(vol_args);
1339fa0d2b9bSLi Zefan 	vol_args->name[BTRFS_SUBVOL_NAME_MAX] = '\0';
1340fdfb1e4fSLi Zefan 
1341b83cc969SLi Zefan 	if (vol_args->flags &
1342b83cc969SLi Zefan 	    ~(BTRFS_SUBVOL_CREATE_ASYNC | BTRFS_SUBVOL_RDONLY)) {
1343b83cc969SLi Zefan 		ret = -EOPNOTSUPP;
1344fdfb1e4fSLi Zefan 		goto out;
1345fdfb1e4fSLi Zefan 	}
1346fdfb1e4fSLi Zefan 
1347fa0d2b9bSLi Zefan 	if (vol_args->flags & BTRFS_SUBVOL_CREATE_ASYNC)
1348fdfb1e4fSLi Zefan 		ptr = &transid;
1349b83cc969SLi Zefan 	if (vol_args->flags & BTRFS_SUBVOL_RDONLY)
1350b83cc969SLi Zefan 		readonly = true;
135175eaa0e2SSage Weil 
1352fa0d2b9bSLi Zefan 	ret = btrfs_ioctl_snap_create_transid(file, vol_args->name,
1353b83cc969SLi Zefan 					      vol_args->fd, subvol,
1354b83cc969SLi Zefan 					      ptr, readonly);
135575eaa0e2SSage Weil 
1356fdfb1e4fSLi Zefan 	if (ret == 0 && ptr &&
135775eaa0e2SSage Weil 	    copy_to_user(arg +
1358fdfb1e4fSLi Zefan 			 offsetof(struct btrfs_ioctl_vol_args_v2,
1359fdfb1e4fSLi Zefan 				  transid), ptr, sizeof(*ptr)))
136075eaa0e2SSage Weil 		ret = -EFAULT;
1361fdfb1e4fSLi Zefan out:
1362f46b5a66SChristoph Hellwig 	kfree(vol_args);
1363f46b5a66SChristoph Hellwig 	return ret;
1364f46b5a66SChristoph Hellwig }
1365f46b5a66SChristoph Hellwig 
13660caa102dSLi Zefan static noinline int btrfs_ioctl_subvol_getflags(struct file *file,
13670caa102dSLi Zefan 						void __user *arg)
13680caa102dSLi Zefan {
13690caa102dSLi Zefan 	struct inode *inode = fdentry(file)->d_inode;
13700caa102dSLi Zefan 	struct btrfs_root *root = BTRFS_I(inode)->root;
13710caa102dSLi Zefan 	int ret = 0;
13720caa102dSLi Zefan 	u64 flags = 0;
13730caa102dSLi Zefan 
137433345d01SLi Zefan 	if (btrfs_ino(inode) != BTRFS_FIRST_FREE_OBJECTID)
13750caa102dSLi Zefan 		return -EINVAL;
13760caa102dSLi Zefan 
13770caa102dSLi Zefan 	down_read(&root->fs_info->subvol_sem);
13780caa102dSLi Zefan 	if (btrfs_root_readonly(root))
13790caa102dSLi Zefan 		flags |= BTRFS_SUBVOL_RDONLY;
13800caa102dSLi Zefan 	up_read(&root->fs_info->subvol_sem);
13810caa102dSLi Zefan 
13820caa102dSLi Zefan 	if (copy_to_user(arg, &flags, sizeof(flags)))
13830caa102dSLi Zefan 		ret = -EFAULT;
13840caa102dSLi Zefan 
13850caa102dSLi Zefan 	return ret;
13860caa102dSLi Zefan }
13870caa102dSLi Zefan 
13880caa102dSLi Zefan static noinline int btrfs_ioctl_subvol_setflags(struct file *file,
13890caa102dSLi Zefan 					      void __user *arg)
13900caa102dSLi Zefan {
13910caa102dSLi Zefan 	struct inode *inode = fdentry(file)->d_inode;
13920caa102dSLi Zefan 	struct btrfs_root *root = BTRFS_I(inode)->root;
13930caa102dSLi Zefan 	struct btrfs_trans_handle *trans;
13940caa102dSLi Zefan 	u64 root_flags;
13950caa102dSLi Zefan 	u64 flags;
13960caa102dSLi Zefan 	int ret = 0;
13970caa102dSLi Zefan 
13980caa102dSLi Zefan 	if (root->fs_info->sb->s_flags & MS_RDONLY)
13990caa102dSLi Zefan 		return -EROFS;
14000caa102dSLi Zefan 
140133345d01SLi Zefan 	if (btrfs_ino(inode) != BTRFS_FIRST_FREE_OBJECTID)
14020caa102dSLi Zefan 		return -EINVAL;
14030caa102dSLi Zefan 
14040caa102dSLi Zefan 	if (copy_from_user(&flags, arg, sizeof(flags)))
14050caa102dSLi Zefan 		return -EFAULT;
14060caa102dSLi Zefan 
1407b4dc2b8cSLi Zefan 	if (flags & BTRFS_SUBVOL_CREATE_ASYNC)
14080caa102dSLi Zefan 		return -EINVAL;
14090caa102dSLi Zefan 
14100caa102dSLi Zefan 	if (flags & ~BTRFS_SUBVOL_RDONLY)
14110caa102dSLi Zefan 		return -EOPNOTSUPP;
14120caa102dSLi Zefan 
14132e149670SSerge E. Hallyn 	if (!inode_owner_or_capable(inode))
1414b4dc2b8cSLi Zefan 		return -EACCES;
1415b4dc2b8cSLi Zefan 
14160caa102dSLi Zefan 	down_write(&root->fs_info->subvol_sem);
14170caa102dSLi Zefan 
14180caa102dSLi Zefan 	/* nothing to do */
14190caa102dSLi Zefan 	if (!!(flags & BTRFS_SUBVOL_RDONLY) == btrfs_root_readonly(root))
14200caa102dSLi Zefan 		goto out;
14210caa102dSLi Zefan 
14220caa102dSLi Zefan 	root_flags = btrfs_root_flags(&root->root_item);
14230caa102dSLi Zefan 	if (flags & BTRFS_SUBVOL_RDONLY)
14240caa102dSLi Zefan 		btrfs_set_root_flags(&root->root_item,
14250caa102dSLi Zefan 				     root_flags | BTRFS_ROOT_SUBVOL_RDONLY);
14260caa102dSLi Zefan 	else
14270caa102dSLi Zefan 		btrfs_set_root_flags(&root->root_item,
14280caa102dSLi Zefan 				     root_flags & ~BTRFS_ROOT_SUBVOL_RDONLY);
14290caa102dSLi Zefan 
14300caa102dSLi Zefan 	trans = btrfs_start_transaction(root, 1);
14310caa102dSLi Zefan 	if (IS_ERR(trans)) {
14320caa102dSLi Zefan 		ret = PTR_ERR(trans);
14330caa102dSLi Zefan 		goto out_reset;
14340caa102dSLi Zefan 	}
14350caa102dSLi Zefan 
1436b4dc2b8cSLi Zefan 	ret = btrfs_update_root(trans, root->fs_info->tree_root,
14370caa102dSLi Zefan 				&root->root_key, &root->root_item);
14380caa102dSLi Zefan 
14390caa102dSLi Zefan 	btrfs_commit_transaction(trans, root);
14400caa102dSLi Zefan out_reset:
14410caa102dSLi Zefan 	if (ret)
14420caa102dSLi Zefan 		btrfs_set_root_flags(&root->root_item, root_flags);
14430caa102dSLi Zefan out:
14440caa102dSLi Zefan 	up_write(&root->fs_info->subvol_sem);
14450caa102dSLi Zefan 	return ret;
14460caa102dSLi Zefan }
14470caa102dSLi Zefan 
144876dda93cSYan, Zheng /*
144976dda93cSYan, Zheng  * helper to check if the subvolume references other subvolumes
145076dda93cSYan, Zheng  */
145176dda93cSYan, Zheng static noinline int may_destroy_subvol(struct btrfs_root *root)
145276dda93cSYan, Zheng {
145376dda93cSYan, Zheng 	struct btrfs_path *path;
145476dda93cSYan, Zheng 	struct btrfs_key key;
145576dda93cSYan, Zheng 	int ret;
145676dda93cSYan, Zheng 
145776dda93cSYan, Zheng 	path = btrfs_alloc_path();
145876dda93cSYan, Zheng 	if (!path)
145976dda93cSYan, Zheng 		return -ENOMEM;
146076dda93cSYan, Zheng 
146176dda93cSYan, Zheng 	key.objectid = root->root_key.objectid;
146276dda93cSYan, Zheng 	key.type = BTRFS_ROOT_REF_KEY;
146376dda93cSYan, Zheng 	key.offset = (u64)-1;
146476dda93cSYan, Zheng 
146576dda93cSYan, Zheng 	ret = btrfs_search_slot(NULL, root->fs_info->tree_root,
146676dda93cSYan, Zheng 				&key, path, 0, 0);
146776dda93cSYan, Zheng 	if (ret < 0)
146876dda93cSYan, Zheng 		goto out;
146976dda93cSYan, Zheng 	BUG_ON(ret == 0);
147076dda93cSYan, Zheng 
147176dda93cSYan, Zheng 	ret = 0;
147276dda93cSYan, Zheng 	if (path->slots[0] > 0) {
147376dda93cSYan, Zheng 		path->slots[0]--;
147476dda93cSYan, Zheng 		btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
147576dda93cSYan, Zheng 		if (key.objectid == root->root_key.objectid &&
147676dda93cSYan, Zheng 		    key.type == BTRFS_ROOT_REF_KEY)
147776dda93cSYan, Zheng 			ret = -ENOTEMPTY;
147876dda93cSYan, Zheng 	}
147976dda93cSYan, Zheng out:
148076dda93cSYan, Zheng 	btrfs_free_path(path);
148176dda93cSYan, Zheng 	return ret;
148276dda93cSYan, Zheng }
148376dda93cSYan, Zheng 
1484ac8e9819SChris Mason static noinline int key_in_sk(struct btrfs_key *key,
1485ac8e9819SChris Mason 			      struct btrfs_ioctl_search_key *sk)
1486ac8e9819SChris Mason {
1487abc6e134SChris Mason 	struct btrfs_key test;
1488abc6e134SChris Mason 	int ret;
1489abc6e134SChris Mason 
1490abc6e134SChris Mason 	test.objectid = sk->min_objectid;
1491abc6e134SChris Mason 	test.type = sk->min_type;
1492abc6e134SChris Mason 	test.offset = sk->min_offset;
1493abc6e134SChris Mason 
1494abc6e134SChris Mason 	ret = btrfs_comp_cpu_keys(key, &test);
1495abc6e134SChris Mason 	if (ret < 0)
1496ac8e9819SChris Mason 		return 0;
1497abc6e134SChris Mason 
1498abc6e134SChris Mason 	test.objectid = sk->max_objectid;
1499abc6e134SChris Mason 	test.type = sk->max_type;
1500abc6e134SChris Mason 	test.offset = sk->max_offset;
1501abc6e134SChris Mason 
1502abc6e134SChris Mason 	ret = btrfs_comp_cpu_keys(key, &test);
1503abc6e134SChris Mason 	if (ret > 0)
1504ac8e9819SChris Mason 		return 0;
1505ac8e9819SChris Mason 	return 1;
1506ac8e9819SChris Mason }
1507ac8e9819SChris Mason 
1508ac8e9819SChris Mason static noinline int copy_to_sk(struct btrfs_root *root,
1509ac8e9819SChris Mason 			       struct btrfs_path *path,
1510ac8e9819SChris Mason 			       struct btrfs_key *key,
1511ac8e9819SChris Mason 			       struct btrfs_ioctl_search_key *sk,
1512ac8e9819SChris Mason 			       char *buf,
1513ac8e9819SChris Mason 			       unsigned long *sk_offset,
1514ac8e9819SChris Mason 			       int *num_found)
1515ac8e9819SChris Mason {
1516ac8e9819SChris Mason 	u64 found_transid;
1517ac8e9819SChris Mason 	struct extent_buffer *leaf;
1518ac8e9819SChris Mason 	struct btrfs_ioctl_search_header sh;
1519ac8e9819SChris Mason 	unsigned long item_off;
1520ac8e9819SChris Mason 	unsigned long item_len;
1521ac8e9819SChris Mason 	int nritems;
1522ac8e9819SChris Mason 	int i;
1523ac8e9819SChris Mason 	int slot;
1524ac8e9819SChris Mason 	int ret = 0;
1525ac8e9819SChris Mason 
1526ac8e9819SChris Mason 	leaf = path->nodes[0];
1527ac8e9819SChris Mason 	slot = path->slots[0];
1528ac8e9819SChris Mason 	nritems = btrfs_header_nritems(leaf);
1529ac8e9819SChris Mason 
1530ac8e9819SChris Mason 	if (btrfs_header_generation(leaf) > sk->max_transid) {
1531ac8e9819SChris Mason 		i = nritems;
1532ac8e9819SChris Mason 		goto advance_key;
1533ac8e9819SChris Mason 	}
1534ac8e9819SChris Mason 	found_transid = btrfs_header_generation(leaf);
1535ac8e9819SChris Mason 
1536ac8e9819SChris Mason 	for (i = slot; i < nritems; i++) {
1537ac8e9819SChris Mason 		item_off = btrfs_item_ptr_offset(leaf, i);
1538ac8e9819SChris Mason 		item_len = btrfs_item_size_nr(leaf, i);
1539ac8e9819SChris Mason 
1540ac8e9819SChris Mason 		if (item_len > BTRFS_SEARCH_ARGS_BUFSIZE)
1541ac8e9819SChris Mason 			item_len = 0;
1542ac8e9819SChris Mason 
1543ac8e9819SChris Mason 		if (sizeof(sh) + item_len + *sk_offset >
1544ac8e9819SChris Mason 		    BTRFS_SEARCH_ARGS_BUFSIZE) {
1545ac8e9819SChris Mason 			ret = 1;
1546ac8e9819SChris Mason 			goto overflow;
1547ac8e9819SChris Mason 		}
1548ac8e9819SChris Mason 
1549ac8e9819SChris Mason 		btrfs_item_key_to_cpu(leaf, key, i);
1550ac8e9819SChris Mason 		if (!key_in_sk(key, sk))
1551ac8e9819SChris Mason 			continue;
1552ac8e9819SChris Mason 
1553ac8e9819SChris Mason 		sh.objectid = key->objectid;
1554ac8e9819SChris Mason 		sh.offset = key->offset;
1555ac8e9819SChris Mason 		sh.type = key->type;
1556ac8e9819SChris Mason 		sh.len = item_len;
1557ac8e9819SChris Mason 		sh.transid = found_transid;
1558ac8e9819SChris Mason 
1559ac8e9819SChris Mason 		/* copy search result header */
1560ac8e9819SChris Mason 		memcpy(buf + *sk_offset, &sh, sizeof(sh));
1561ac8e9819SChris Mason 		*sk_offset += sizeof(sh);
1562ac8e9819SChris Mason 
1563ac8e9819SChris Mason 		if (item_len) {
1564ac8e9819SChris Mason 			char *p = buf + *sk_offset;
1565ac8e9819SChris Mason 			/* copy the item */
1566ac8e9819SChris Mason 			read_extent_buffer(leaf, p,
1567ac8e9819SChris Mason 					   item_off, item_len);
1568ac8e9819SChris Mason 			*sk_offset += item_len;
1569ac8e9819SChris Mason 		}
1570e2156867SHugo Mills 		(*num_found)++;
1571ac8e9819SChris Mason 
1572ac8e9819SChris Mason 		if (*num_found >= sk->nr_items)
1573ac8e9819SChris Mason 			break;
1574ac8e9819SChris Mason 	}
1575ac8e9819SChris Mason advance_key:
1576ac8e9819SChris Mason 	ret = 0;
1577abc6e134SChris Mason 	if (key->offset < (u64)-1 && key->offset < sk->max_offset)
1578abc6e134SChris Mason 		key->offset++;
1579abc6e134SChris Mason 	else if (key->type < (u8)-1 && key->type < sk->max_type) {
1580abc6e134SChris Mason 		key->offset = 0;
1581abc6e134SChris Mason 		key->type++;
1582abc6e134SChris Mason 	} else if (key->objectid < (u64)-1 && key->objectid < sk->max_objectid) {
1583abc6e134SChris Mason 		key->offset = 0;
1584abc6e134SChris Mason 		key->type = 0;
1585abc6e134SChris Mason 		key->objectid++;
1586abc6e134SChris Mason 	} else
1587abc6e134SChris Mason 		ret = 1;
1588ac8e9819SChris Mason overflow:
1589ac8e9819SChris Mason 	return ret;
1590ac8e9819SChris Mason }
1591ac8e9819SChris Mason 
1592ac8e9819SChris Mason static noinline int search_ioctl(struct inode *inode,
1593ac8e9819SChris Mason 				 struct btrfs_ioctl_search_args *args)
1594ac8e9819SChris Mason {
1595ac8e9819SChris Mason 	struct btrfs_root *root;
1596ac8e9819SChris Mason 	struct btrfs_key key;
1597ac8e9819SChris Mason 	struct btrfs_key max_key;
1598ac8e9819SChris Mason 	struct btrfs_path *path;
1599ac8e9819SChris Mason 	struct btrfs_ioctl_search_key *sk = &args->key;
1600ac8e9819SChris Mason 	struct btrfs_fs_info *info = BTRFS_I(inode)->root->fs_info;
1601ac8e9819SChris Mason 	int ret;
1602ac8e9819SChris Mason 	int num_found = 0;
1603ac8e9819SChris Mason 	unsigned long sk_offset = 0;
1604ac8e9819SChris Mason 
1605ac8e9819SChris Mason 	path = btrfs_alloc_path();
1606ac8e9819SChris Mason 	if (!path)
1607ac8e9819SChris Mason 		return -ENOMEM;
1608ac8e9819SChris Mason 
1609ac8e9819SChris Mason 	if (sk->tree_id == 0) {
1610ac8e9819SChris Mason 		/* search the root of the inode that was passed */
1611ac8e9819SChris Mason 		root = BTRFS_I(inode)->root;
1612ac8e9819SChris Mason 	} else {
1613ac8e9819SChris Mason 		key.objectid = sk->tree_id;
1614ac8e9819SChris Mason 		key.type = BTRFS_ROOT_ITEM_KEY;
1615ac8e9819SChris Mason 		key.offset = (u64)-1;
1616ac8e9819SChris Mason 		root = btrfs_read_fs_root_no_name(info, &key);
1617ac8e9819SChris Mason 		if (IS_ERR(root)) {
1618ac8e9819SChris Mason 			printk(KERN_ERR "could not find root %llu\n",
1619ac8e9819SChris Mason 			       sk->tree_id);
1620ac8e9819SChris Mason 			btrfs_free_path(path);
1621ac8e9819SChris Mason 			return -ENOENT;
1622ac8e9819SChris Mason 		}
1623ac8e9819SChris Mason 	}
1624ac8e9819SChris Mason 
1625ac8e9819SChris Mason 	key.objectid = sk->min_objectid;
1626ac8e9819SChris Mason 	key.type = sk->min_type;
1627ac8e9819SChris Mason 	key.offset = sk->min_offset;
1628ac8e9819SChris Mason 
1629ac8e9819SChris Mason 	max_key.objectid = sk->max_objectid;
1630ac8e9819SChris Mason 	max_key.type = sk->max_type;
1631ac8e9819SChris Mason 	max_key.offset = sk->max_offset;
1632ac8e9819SChris Mason 
1633ac8e9819SChris Mason 	path->keep_locks = 1;
1634ac8e9819SChris Mason 
1635ac8e9819SChris Mason 	while(1) {
1636ac8e9819SChris Mason 		ret = btrfs_search_forward(root, &key, &max_key, path, 0,
1637ac8e9819SChris Mason 					   sk->min_transid);
1638ac8e9819SChris Mason 		if (ret != 0) {
1639ac8e9819SChris Mason 			if (ret > 0)
1640ac8e9819SChris Mason 				ret = 0;
1641ac8e9819SChris Mason 			goto err;
1642ac8e9819SChris Mason 		}
1643ac8e9819SChris Mason 		ret = copy_to_sk(root, path, &key, sk, args->buf,
1644ac8e9819SChris Mason 				 &sk_offset, &num_found);
1645b3b4aa74SDavid Sterba 		btrfs_release_path(path);
1646ac8e9819SChris Mason 		if (ret || num_found >= sk->nr_items)
1647ac8e9819SChris Mason 			break;
1648ac8e9819SChris Mason 
1649ac8e9819SChris Mason 	}
1650ac8e9819SChris Mason 	ret = 0;
1651ac8e9819SChris Mason err:
1652ac8e9819SChris Mason 	sk->nr_items = num_found;
1653ac8e9819SChris Mason 	btrfs_free_path(path);
1654ac8e9819SChris Mason 	return ret;
1655ac8e9819SChris Mason }
1656ac8e9819SChris Mason 
1657ac8e9819SChris Mason static noinline int btrfs_ioctl_tree_search(struct file *file,
1658ac8e9819SChris Mason 					   void __user *argp)
1659ac8e9819SChris Mason {
1660ac8e9819SChris Mason 	 struct btrfs_ioctl_search_args *args;
1661ac8e9819SChris Mason 	 struct inode *inode;
1662ac8e9819SChris Mason 	 int ret;
1663ac8e9819SChris Mason 
1664ac8e9819SChris Mason 	if (!capable(CAP_SYS_ADMIN))
1665ac8e9819SChris Mason 		return -EPERM;
1666ac8e9819SChris Mason 
16672354d08fSJulia Lawall 	args = memdup_user(argp, sizeof(*args));
16682354d08fSJulia Lawall 	if (IS_ERR(args))
16692354d08fSJulia Lawall 		return PTR_ERR(args);
1670ac8e9819SChris Mason 
1671ac8e9819SChris Mason 	inode = fdentry(file)->d_inode;
1672ac8e9819SChris Mason 	ret = search_ioctl(inode, args);
1673ac8e9819SChris Mason 	if (ret == 0 && copy_to_user(argp, args, sizeof(*args)))
1674ac8e9819SChris Mason 		ret = -EFAULT;
1675ac8e9819SChris Mason 	kfree(args);
1676ac8e9819SChris Mason 	return ret;
1677ac8e9819SChris Mason }
1678ac8e9819SChris Mason 
167998d377a0STARUISI Hiroaki /*
1680ac8e9819SChris Mason  * Search INODE_REFs to identify path name of 'dirid' directory
1681ac8e9819SChris Mason  * in a 'tree_id' tree. and sets path name to 'name'.
168298d377a0STARUISI Hiroaki  */
168398d377a0STARUISI Hiroaki static noinline int btrfs_search_path_in_tree(struct btrfs_fs_info *info,
168498d377a0STARUISI Hiroaki 				u64 tree_id, u64 dirid, char *name)
168598d377a0STARUISI Hiroaki {
168698d377a0STARUISI Hiroaki 	struct btrfs_root *root;
168798d377a0STARUISI Hiroaki 	struct btrfs_key key;
1688ac8e9819SChris Mason 	char *ptr;
168998d377a0STARUISI Hiroaki 	int ret = -1;
169098d377a0STARUISI Hiroaki 	int slot;
169198d377a0STARUISI Hiroaki 	int len;
169298d377a0STARUISI Hiroaki 	int total_len = 0;
169398d377a0STARUISI Hiroaki 	struct btrfs_inode_ref *iref;
169498d377a0STARUISI Hiroaki 	struct extent_buffer *l;
169598d377a0STARUISI Hiroaki 	struct btrfs_path *path;
169698d377a0STARUISI Hiroaki 
169798d377a0STARUISI Hiroaki 	if (dirid == BTRFS_FIRST_FREE_OBJECTID) {
169898d377a0STARUISI Hiroaki 		name[0]='\0';
169998d377a0STARUISI Hiroaki 		return 0;
170098d377a0STARUISI Hiroaki 	}
170198d377a0STARUISI Hiroaki 
170298d377a0STARUISI Hiroaki 	path = btrfs_alloc_path();
170398d377a0STARUISI Hiroaki 	if (!path)
170498d377a0STARUISI Hiroaki 		return -ENOMEM;
170598d377a0STARUISI Hiroaki 
1706ac8e9819SChris Mason 	ptr = &name[BTRFS_INO_LOOKUP_PATH_MAX];
170798d377a0STARUISI Hiroaki 
170898d377a0STARUISI Hiroaki 	key.objectid = tree_id;
170998d377a0STARUISI Hiroaki 	key.type = BTRFS_ROOT_ITEM_KEY;
171098d377a0STARUISI Hiroaki 	key.offset = (u64)-1;
171198d377a0STARUISI Hiroaki 	root = btrfs_read_fs_root_no_name(info, &key);
171298d377a0STARUISI Hiroaki 	if (IS_ERR(root)) {
171398d377a0STARUISI Hiroaki 		printk(KERN_ERR "could not find root %llu\n", tree_id);
17148ad6fcabSChris Mason 		ret = -ENOENT;
17158ad6fcabSChris Mason 		goto out;
171698d377a0STARUISI Hiroaki 	}
171798d377a0STARUISI Hiroaki 
171898d377a0STARUISI Hiroaki 	key.objectid = dirid;
171998d377a0STARUISI Hiroaki 	key.type = BTRFS_INODE_REF_KEY;
17208ad6fcabSChris Mason 	key.offset = (u64)-1;
172198d377a0STARUISI Hiroaki 
172298d377a0STARUISI Hiroaki 	while(1) {
172398d377a0STARUISI Hiroaki 		ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
172498d377a0STARUISI Hiroaki 		if (ret < 0)
172598d377a0STARUISI Hiroaki 			goto out;
172698d377a0STARUISI Hiroaki 
172798d377a0STARUISI Hiroaki 		l = path->nodes[0];
172898d377a0STARUISI Hiroaki 		slot = path->slots[0];
17298ad6fcabSChris Mason 		if (ret > 0 && slot > 0)
17308ad6fcabSChris Mason 			slot--;
173198d377a0STARUISI Hiroaki 		btrfs_item_key_to_cpu(l, &key, slot);
173298d377a0STARUISI Hiroaki 
173398d377a0STARUISI Hiroaki 		if (ret > 0 && (key.objectid != dirid ||
1734ac8e9819SChris Mason 				key.type != BTRFS_INODE_REF_KEY)) {
1735ac8e9819SChris Mason 			ret = -ENOENT;
173698d377a0STARUISI Hiroaki 			goto out;
1737ac8e9819SChris Mason 		}
173898d377a0STARUISI Hiroaki 
173998d377a0STARUISI Hiroaki 		iref = btrfs_item_ptr(l, slot, struct btrfs_inode_ref);
174098d377a0STARUISI Hiroaki 		len = btrfs_inode_ref_name_len(l, iref);
174198d377a0STARUISI Hiroaki 		ptr -= len + 1;
174298d377a0STARUISI Hiroaki 		total_len += len + 1;
1743ac8e9819SChris Mason 		if (ptr < name)
174498d377a0STARUISI Hiroaki 			goto out;
174598d377a0STARUISI Hiroaki 
174698d377a0STARUISI Hiroaki 		*(ptr + len) = '/';
174798d377a0STARUISI Hiroaki 		read_extent_buffer(l, ptr,(unsigned long)(iref + 1), len);
174898d377a0STARUISI Hiroaki 
174998d377a0STARUISI Hiroaki 		if (key.offset == BTRFS_FIRST_FREE_OBJECTID)
175098d377a0STARUISI Hiroaki 			break;
175198d377a0STARUISI Hiroaki 
1752b3b4aa74SDavid Sterba 		btrfs_release_path(path);
175398d377a0STARUISI Hiroaki 		key.objectid = key.offset;
17548ad6fcabSChris Mason 		key.offset = (u64)-1;
175598d377a0STARUISI Hiroaki 		dirid = key.objectid;
175698d377a0STARUISI Hiroaki 
175798d377a0STARUISI Hiroaki 	}
1758ac8e9819SChris Mason 	if (ptr < name)
175998d377a0STARUISI Hiroaki 		goto out;
1760ac8e9819SChris Mason 	memcpy(name, ptr, total_len);
176198d377a0STARUISI Hiroaki 	name[total_len]='\0';
176298d377a0STARUISI Hiroaki 	ret = 0;
176398d377a0STARUISI Hiroaki out:
176498d377a0STARUISI Hiroaki 	btrfs_free_path(path);
1765ac8e9819SChris Mason 	return ret;
1766ac8e9819SChris Mason }
1767ac8e9819SChris Mason 
1768ac8e9819SChris Mason static noinline int btrfs_ioctl_ino_lookup(struct file *file,
1769ac8e9819SChris Mason 					   void __user *argp)
1770ac8e9819SChris Mason {
1771ac8e9819SChris Mason 	 struct btrfs_ioctl_ino_lookup_args *args;
1772ac8e9819SChris Mason 	 struct inode *inode;
1773ac8e9819SChris Mason 	 int ret;
1774ac8e9819SChris Mason 
1775ac8e9819SChris Mason 	if (!capable(CAP_SYS_ADMIN))
1776ac8e9819SChris Mason 		return -EPERM;
1777ac8e9819SChris Mason 
17782354d08fSJulia Lawall 	args = memdup_user(argp, sizeof(*args));
17792354d08fSJulia Lawall 	if (IS_ERR(args))
17802354d08fSJulia Lawall 		return PTR_ERR(args);
1781c2b96929SDan Carpenter 
1782ac8e9819SChris Mason 	inode = fdentry(file)->d_inode;
1783ac8e9819SChris Mason 
17841b53ac4dSChris Mason 	if (args->treeid == 0)
17851b53ac4dSChris Mason 		args->treeid = BTRFS_I(inode)->root->root_key.objectid;
17861b53ac4dSChris Mason 
1787ac8e9819SChris Mason 	ret = btrfs_search_path_in_tree(BTRFS_I(inode)->root->fs_info,
1788ac8e9819SChris Mason 					args->treeid, args->objectid,
1789ac8e9819SChris Mason 					args->name);
1790ac8e9819SChris Mason 
1791ac8e9819SChris Mason 	if (ret == 0 && copy_to_user(argp, args, sizeof(*args)))
1792ac8e9819SChris Mason 		ret = -EFAULT;
1793ac8e9819SChris Mason 
1794ac8e9819SChris Mason 	kfree(args);
179598d377a0STARUISI Hiroaki 	return ret;
179698d377a0STARUISI Hiroaki }
179798d377a0STARUISI Hiroaki 
179876dda93cSYan, Zheng static noinline int btrfs_ioctl_snap_destroy(struct file *file,
179976dda93cSYan, Zheng 					     void __user *arg)
180076dda93cSYan, Zheng {
180176dda93cSYan, Zheng 	struct dentry *parent = fdentry(file);
180276dda93cSYan, Zheng 	struct dentry *dentry;
180376dda93cSYan, Zheng 	struct inode *dir = parent->d_inode;
180476dda93cSYan, Zheng 	struct inode *inode;
180576dda93cSYan, Zheng 	struct btrfs_root *root = BTRFS_I(dir)->root;
180676dda93cSYan, Zheng 	struct btrfs_root *dest = NULL;
180776dda93cSYan, Zheng 	struct btrfs_ioctl_vol_args *vol_args;
180876dda93cSYan, Zheng 	struct btrfs_trans_handle *trans;
180976dda93cSYan, Zheng 	int namelen;
181076dda93cSYan, Zheng 	int ret;
181176dda93cSYan, Zheng 	int err = 0;
181276dda93cSYan, Zheng 
181376dda93cSYan, Zheng 	vol_args = memdup_user(arg, sizeof(*vol_args));
181476dda93cSYan, Zheng 	if (IS_ERR(vol_args))
181576dda93cSYan, Zheng 		return PTR_ERR(vol_args);
181676dda93cSYan, Zheng 
181776dda93cSYan, Zheng 	vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
181876dda93cSYan, Zheng 	namelen = strlen(vol_args->name);
181976dda93cSYan, Zheng 	if (strchr(vol_args->name, '/') ||
182076dda93cSYan, Zheng 	    strncmp(vol_args->name, "..", namelen) == 0) {
182176dda93cSYan, Zheng 		err = -EINVAL;
182276dda93cSYan, Zheng 		goto out;
182376dda93cSYan, Zheng 	}
182476dda93cSYan, Zheng 
182576dda93cSYan, Zheng 	err = mnt_want_write(file->f_path.mnt);
182676dda93cSYan, Zheng 	if (err)
182776dda93cSYan, Zheng 		goto out;
182876dda93cSYan, Zheng 
182976dda93cSYan, Zheng 	mutex_lock_nested(&dir->i_mutex, I_MUTEX_PARENT);
183076dda93cSYan, Zheng 	dentry = lookup_one_len(vol_args->name, parent, namelen);
183176dda93cSYan, Zheng 	if (IS_ERR(dentry)) {
183276dda93cSYan, Zheng 		err = PTR_ERR(dentry);
183376dda93cSYan, Zheng 		goto out_unlock_dir;
183476dda93cSYan, Zheng 	}
183576dda93cSYan, Zheng 
183676dda93cSYan, Zheng 	if (!dentry->d_inode) {
183776dda93cSYan, Zheng 		err = -ENOENT;
183876dda93cSYan, Zheng 		goto out_dput;
183976dda93cSYan, Zheng 	}
184076dda93cSYan, Zheng 
184176dda93cSYan, Zheng 	inode = dentry->d_inode;
18424260f7c7SSage Weil 	dest = BTRFS_I(inode)->root;
18434260f7c7SSage Weil 	if (!capable(CAP_SYS_ADMIN)){
18444260f7c7SSage Weil 		/*
18454260f7c7SSage Weil 		 * Regular user.  Only allow this with a special mount
18464260f7c7SSage Weil 		 * option, when the user has write+exec access to the
18474260f7c7SSage Weil 		 * subvol root, and when rmdir(2) would have been
18484260f7c7SSage Weil 		 * allowed.
18494260f7c7SSage Weil 		 *
18504260f7c7SSage Weil 		 * Note that this is _not_ check that the subvol is
18514260f7c7SSage Weil 		 * empty or doesn't contain data that we wouldn't
18524260f7c7SSage Weil 		 * otherwise be able to delete.
18534260f7c7SSage Weil 		 *
18544260f7c7SSage Weil 		 * Users who want to delete empty subvols should try
18554260f7c7SSage Weil 		 * rmdir(2).
18564260f7c7SSage Weil 		 */
18574260f7c7SSage Weil 		err = -EPERM;
18584260f7c7SSage Weil 		if (!btrfs_test_opt(root, USER_SUBVOL_RM_ALLOWED))
18594260f7c7SSage Weil 			goto out_dput;
18604260f7c7SSage Weil 
18614260f7c7SSage Weil 		/*
18624260f7c7SSage Weil 		 * Do not allow deletion if the parent dir is the same
18634260f7c7SSage Weil 		 * as the dir to be deleted.  That means the ioctl
18644260f7c7SSage Weil 		 * must be called on the dentry referencing the root
18654260f7c7SSage Weil 		 * of the subvol, not a random directory contained
18664260f7c7SSage Weil 		 * within it.
18674260f7c7SSage Weil 		 */
18684260f7c7SSage Weil 		err = -EINVAL;
18694260f7c7SSage Weil 		if (root == dest)
18704260f7c7SSage Weil 			goto out_dput;
18714260f7c7SSage Weil 
18724260f7c7SSage Weil 		err = inode_permission(inode, MAY_WRITE | MAY_EXEC);
18734260f7c7SSage Weil 		if (err)
18744260f7c7SSage Weil 			goto out_dput;
18754260f7c7SSage Weil 
18764260f7c7SSage Weil 		/* check if subvolume may be deleted by a non-root user */
18774260f7c7SSage Weil 		err = btrfs_may_delete(dir, dentry, 1);
18784260f7c7SSage Weil 		if (err)
18794260f7c7SSage Weil 			goto out_dput;
18804260f7c7SSage Weil 	}
18814260f7c7SSage Weil 
188233345d01SLi Zefan 	if (btrfs_ino(inode) != BTRFS_FIRST_FREE_OBJECTID) {
188376dda93cSYan, Zheng 		err = -EINVAL;
188476dda93cSYan, Zheng 		goto out_dput;
188576dda93cSYan, Zheng 	}
188676dda93cSYan, Zheng 
188776dda93cSYan, Zheng 	mutex_lock(&inode->i_mutex);
188876dda93cSYan, Zheng 	err = d_invalidate(dentry);
188976dda93cSYan, Zheng 	if (err)
189076dda93cSYan, Zheng 		goto out_unlock;
189176dda93cSYan, Zheng 
189276dda93cSYan, Zheng 	down_write(&root->fs_info->subvol_sem);
189376dda93cSYan, Zheng 
189476dda93cSYan, Zheng 	err = may_destroy_subvol(dest);
189576dda93cSYan, Zheng 	if (err)
189676dda93cSYan, Zheng 		goto out_up_write;
189776dda93cSYan, Zheng 
1898a22285a6SYan, Zheng 	trans = btrfs_start_transaction(root, 0);
1899a22285a6SYan, Zheng 	if (IS_ERR(trans)) {
1900a22285a6SYan, Zheng 		err = PTR_ERR(trans);
1901d327099aSDan Carpenter 		goto out_up_write;
1902a22285a6SYan, Zheng 	}
1903a22285a6SYan, Zheng 	trans->block_rsv = &root->fs_info->global_block_rsv;
1904a22285a6SYan, Zheng 
190576dda93cSYan, Zheng 	ret = btrfs_unlink_subvol(trans, root, dir,
190676dda93cSYan, Zheng 				dest->root_key.objectid,
190776dda93cSYan, Zheng 				dentry->d_name.name,
190876dda93cSYan, Zheng 				dentry->d_name.len);
190976dda93cSYan, Zheng 	BUG_ON(ret);
191076dda93cSYan, Zheng 
191176dda93cSYan, Zheng 	btrfs_record_root_in_trans(trans, dest);
191276dda93cSYan, Zheng 
191376dda93cSYan, Zheng 	memset(&dest->root_item.drop_progress, 0,
191476dda93cSYan, Zheng 		sizeof(dest->root_item.drop_progress));
191576dda93cSYan, Zheng 	dest->root_item.drop_level = 0;
191676dda93cSYan, Zheng 	btrfs_set_root_refs(&dest->root_item, 0);
191776dda93cSYan, Zheng 
1918d68fc57bSYan, Zheng 	if (!xchg(&dest->orphan_item_inserted, 1)) {
191976dda93cSYan, Zheng 		ret = btrfs_insert_orphan_item(trans,
192076dda93cSYan, Zheng 					root->fs_info->tree_root,
192176dda93cSYan, Zheng 					dest->root_key.objectid);
192276dda93cSYan, Zheng 		BUG_ON(ret);
1923d68fc57bSYan, Zheng 	}
192476dda93cSYan, Zheng 
1925531cb13fSSage Weil 	ret = btrfs_end_transaction(trans, root);
192676dda93cSYan, Zheng 	BUG_ON(ret);
192776dda93cSYan, Zheng 	inode->i_flags |= S_DEAD;
192876dda93cSYan, Zheng out_up_write:
192976dda93cSYan, Zheng 	up_write(&root->fs_info->subvol_sem);
193076dda93cSYan, Zheng out_unlock:
193176dda93cSYan, Zheng 	mutex_unlock(&inode->i_mutex);
193276dda93cSYan, Zheng 	if (!err) {
1933efefb143SYan, Zheng 		shrink_dcache_sb(root->fs_info->sb);
193476dda93cSYan, Zheng 		btrfs_invalidate_inodes(dest);
193576dda93cSYan, Zheng 		d_delete(dentry);
193676dda93cSYan, Zheng 	}
193776dda93cSYan, Zheng out_dput:
193876dda93cSYan, Zheng 	dput(dentry);
193976dda93cSYan, Zheng out_unlock_dir:
194076dda93cSYan, Zheng 	mutex_unlock(&dir->i_mutex);
194176dda93cSYan, Zheng 	mnt_drop_write(file->f_path.mnt);
194276dda93cSYan, Zheng out:
194376dda93cSYan, Zheng 	kfree(vol_args);
194476dda93cSYan, Zheng 	return err;
194576dda93cSYan, Zheng }
194676dda93cSYan, Zheng 
19471e701a32SChris Mason static int btrfs_ioctl_defrag(struct file *file, void __user *argp)
1948f46b5a66SChristoph Hellwig {
1949f46b5a66SChristoph Hellwig 	struct inode *inode = fdentry(file)->d_inode;
1950f46b5a66SChristoph Hellwig 	struct btrfs_root *root = BTRFS_I(inode)->root;
19511e701a32SChris Mason 	struct btrfs_ioctl_defrag_range_args *range;
1952c146afadSYan Zheng 	int ret;
1953c146afadSYan Zheng 
1954b83cc969SLi Zefan 	if (btrfs_root_readonly(root))
1955b83cc969SLi Zefan 		return -EROFS;
1956b83cc969SLi Zefan 
1957c146afadSYan Zheng 	ret = mnt_want_write(file->f_path.mnt);
1958c146afadSYan Zheng 	if (ret)
1959c146afadSYan Zheng 		return ret;
1960f46b5a66SChristoph Hellwig 
1961f46b5a66SChristoph Hellwig 	switch (inode->i_mode & S_IFMT) {
1962f46b5a66SChristoph Hellwig 	case S_IFDIR:
1963e441d54dSChris Mason 		if (!capable(CAP_SYS_ADMIN)) {
1964e441d54dSChris Mason 			ret = -EPERM;
1965e441d54dSChris Mason 			goto out;
1966e441d54dSChris Mason 		}
19678929ecfaSYan, Zheng 		ret = btrfs_defrag_root(root, 0);
19688929ecfaSYan, Zheng 		if (ret)
19698929ecfaSYan, Zheng 			goto out;
19708929ecfaSYan, Zheng 		ret = btrfs_defrag_root(root->fs_info->extent_root, 0);
1971f46b5a66SChristoph Hellwig 		break;
1972f46b5a66SChristoph Hellwig 	case S_IFREG:
1973e441d54dSChris Mason 		if (!(file->f_mode & FMODE_WRITE)) {
1974e441d54dSChris Mason 			ret = -EINVAL;
1975e441d54dSChris Mason 			goto out;
1976e441d54dSChris Mason 		}
19771e701a32SChris Mason 
19781e701a32SChris Mason 		range = kzalloc(sizeof(*range), GFP_KERNEL);
19791e701a32SChris Mason 		if (!range) {
19801e701a32SChris Mason 			ret = -ENOMEM;
19811e701a32SChris Mason 			goto out;
19821e701a32SChris Mason 		}
19831e701a32SChris Mason 
19841e701a32SChris Mason 		if (argp) {
19851e701a32SChris Mason 			if (copy_from_user(range, argp,
19861e701a32SChris Mason 					   sizeof(*range))) {
19871e701a32SChris Mason 				ret = -EFAULT;
19881e701a32SChris Mason 				kfree(range);
1989683be16eSDan Carpenter 				goto out;
19901e701a32SChris Mason 			}
19911e701a32SChris Mason 			/* compression requires us to start the IO */
19921e701a32SChris Mason 			if ((range->flags & BTRFS_DEFRAG_RANGE_COMPRESS)) {
19931e701a32SChris Mason 				range->flags |= BTRFS_DEFRAG_RANGE_START_IO;
19941e701a32SChris Mason 				range->extent_thresh = (u32)-1;
19951e701a32SChris Mason 			}
19961e701a32SChris Mason 		} else {
19971e701a32SChris Mason 			/* the rest are all set to zero by kzalloc */
19981e701a32SChris Mason 			range->len = (u64)-1;
19991e701a32SChris Mason 		}
20004cb5300bSChris Mason 		ret = btrfs_defrag_file(fdentry(file)->d_inode, file,
20014cb5300bSChris Mason 					range, 0, 0);
20024cb5300bSChris Mason 		if (ret > 0)
20034cb5300bSChris Mason 			ret = 0;
20041e701a32SChris Mason 		kfree(range);
2005f46b5a66SChristoph Hellwig 		break;
20068929ecfaSYan, Zheng 	default:
20078929ecfaSYan, Zheng 		ret = -EINVAL;
2008f46b5a66SChristoph Hellwig 	}
2009e441d54dSChris Mason out:
2010ab67b7c1SYan Zheng 	mnt_drop_write(file->f_path.mnt);
2011e441d54dSChris Mason 	return ret;
2012f46b5a66SChristoph Hellwig }
2013f46b5a66SChristoph Hellwig 
2014b2950863SChristoph Hellwig static long btrfs_ioctl_add_dev(struct btrfs_root *root, void __user *arg)
2015f46b5a66SChristoph Hellwig {
2016f46b5a66SChristoph Hellwig 	struct btrfs_ioctl_vol_args *vol_args;
2017f46b5a66SChristoph Hellwig 	int ret;
2018f46b5a66SChristoph Hellwig 
2019e441d54dSChris Mason 	if (!capable(CAP_SYS_ADMIN))
2020e441d54dSChris Mason 		return -EPERM;
2021e441d54dSChris Mason 
2022dae7b665SLi Zefan 	vol_args = memdup_user(arg, sizeof(*vol_args));
2023dae7b665SLi Zefan 	if (IS_ERR(vol_args))
2024dae7b665SLi Zefan 		return PTR_ERR(vol_args);
2025f46b5a66SChristoph Hellwig 
20265516e595SMark Fasheh 	vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
2027f46b5a66SChristoph Hellwig 	ret = btrfs_init_new_device(root, vol_args->name);
2028f46b5a66SChristoph Hellwig 
2029f46b5a66SChristoph Hellwig 	kfree(vol_args);
2030f46b5a66SChristoph Hellwig 	return ret;
2031f46b5a66SChristoph Hellwig }
2032f46b5a66SChristoph Hellwig 
2033b2950863SChristoph Hellwig static long btrfs_ioctl_rm_dev(struct btrfs_root *root, void __user *arg)
2034f46b5a66SChristoph Hellwig {
2035f46b5a66SChristoph Hellwig 	struct btrfs_ioctl_vol_args *vol_args;
2036f46b5a66SChristoph Hellwig 	int ret;
2037f46b5a66SChristoph Hellwig 
2038e441d54dSChris Mason 	if (!capable(CAP_SYS_ADMIN))
2039e441d54dSChris Mason 		return -EPERM;
2040e441d54dSChris Mason 
2041c146afadSYan Zheng 	if (root->fs_info->sb->s_flags & MS_RDONLY)
2042c146afadSYan Zheng 		return -EROFS;
2043c146afadSYan Zheng 
2044dae7b665SLi Zefan 	vol_args = memdup_user(arg, sizeof(*vol_args));
2045dae7b665SLi Zefan 	if (IS_ERR(vol_args))
2046dae7b665SLi Zefan 		return PTR_ERR(vol_args);
2047f46b5a66SChristoph Hellwig 
20485516e595SMark Fasheh 	vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
2049f46b5a66SChristoph Hellwig 	ret = btrfs_rm_device(root, vol_args->name);
2050f46b5a66SChristoph Hellwig 
2051f46b5a66SChristoph Hellwig 	kfree(vol_args);
2052f46b5a66SChristoph Hellwig 	return ret;
2053f46b5a66SChristoph Hellwig }
2054f46b5a66SChristoph Hellwig 
2055475f6387SJan Schmidt static long btrfs_ioctl_fs_info(struct btrfs_root *root, void __user *arg)
2056475f6387SJan Schmidt {
2057475f6387SJan Schmidt 	struct btrfs_ioctl_fs_info_args fi_args;
2058475f6387SJan Schmidt 	struct btrfs_device *device;
2059475f6387SJan Schmidt 	struct btrfs_device *next;
2060475f6387SJan Schmidt 	struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices;
2061475f6387SJan Schmidt 
2062475f6387SJan Schmidt 	if (!capable(CAP_SYS_ADMIN))
2063475f6387SJan Schmidt 		return -EPERM;
2064475f6387SJan Schmidt 
2065475f6387SJan Schmidt 	fi_args.num_devices = fs_devices->num_devices;
2066475f6387SJan Schmidt 	fi_args.max_id = 0;
2067475f6387SJan Schmidt 	memcpy(&fi_args.fsid, root->fs_info->fsid, sizeof(fi_args.fsid));
2068475f6387SJan Schmidt 
2069475f6387SJan Schmidt 	mutex_lock(&fs_devices->device_list_mutex);
2070475f6387SJan Schmidt 	list_for_each_entry_safe(device, next, &fs_devices->devices, dev_list) {
2071475f6387SJan Schmidt 		if (device->devid > fi_args.max_id)
2072475f6387SJan Schmidt 			fi_args.max_id = device->devid;
2073475f6387SJan Schmidt 	}
2074475f6387SJan Schmidt 	mutex_unlock(&fs_devices->device_list_mutex);
2075475f6387SJan Schmidt 
2076475f6387SJan Schmidt 	if (copy_to_user(arg, &fi_args, sizeof(fi_args)))
2077475f6387SJan Schmidt 		return -EFAULT;
2078475f6387SJan Schmidt 
2079475f6387SJan Schmidt 	return 0;
2080475f6387SJan Schmidt }
2081475f6387SJan Schmidt 
2082475f6387SJan Schmidt static long btrfs_ioctl_dev_info(struct btrfs_root *root, void __user *arg)
2083475f6387SJan Schmidt {
2084475f6387SJan Schmidt 	struct btrfs_ioctl_dev_info_args *di_args;
2085475f6387SJan Schmidt 	struct btrfs_device *dev;
2086475f6387SJan Schmidt 	struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices;
2087475f6387SJan Schmidt 	int ret = 0;
2088475f6387SJan Schmidt 	char *s_uuid = NULL;
2089475f6387SJan Schmidt 	char empty_uuid[BTRFS_UUID_SIZE] = {0};
2090475f6387SJan Schmidt 
2091475f6387SJan Schmidt 	if (!capable(CAP_SYS_ADMIN))
2092475f6387SJan Schmidt 		return -EPERM;
2093475f6387SJan Schmidt 
2094475f6387SJan Schmidt 	di_args = memdup_user(arg, sizeof(*di_args));
2095475f6387SJan Schmidt 	if (IS_ERR(di_args))
2096475f6387SJan Schmidt 		return PTR_ERR(di_args);
2097475f6387SJan Schmidt 
2098475f6387SJan Schmidt 	if (memcmp(empty_uuid, di_args->uuid, BTRFS_UUID_SIZE) != 0)
2099475f6387SJan Schmidt 		s_uuid = di_args->uuid;
2100475f6387SJan Schmidt 
2101475f6387SJan Schmidt 	mutex_lock(&fs_devices->device_list_mutex);
2102475f6387SJan Schmidt 	dev = btrfs_find_device(root, di_args->devid, s_uuid, NULL);
2103475f6387SJan Schmidt 	mutex_unlock(&fs_devices->device_list_mutex);
2104475f6387SJan Schmidt 
2105475f6387SJan Schmidt 	if (!dev) {
2106475f6387SJan Schmidt 		ret = -ENODEV;
2107475f6387SJan Schmidt 		goto out;
2108475f6387SJan Schmidt 	}
2109475f6387SJan Schmidt 
2110475f6387SJan Schmidt 	di_args->devid = dev->devid;
2111475f6387SJan Schmidt 	di_args->bytes_used = dev->bytes_used;
2112475f6387SJan Schmidt 	di_args->total_bytes = dev->total_bytes;
2113475f6387SJan Schmidt 	memcpy(di_args->uuid, dev->uuid, sizeof(di_args->uuid));
2114475f6387SJan Schmidt 	strncpy(di_args->path, dev->name, sizeof(di_args->path));
2115475f6387SJan Schmidt 
2116475f6387SJan Schmidt out:
2117475f6387SJan Schmidt 	if (ret == 0 && copy_to_user(arg, di_args, sizeof(*di_args)))
2118475f6387SJan Schmidt 		ret = -EFAULT;
2119475f6387SJan Schmidt 
2120475f6387SJan Schmidt 	kfree(di_args);
2121475f6387SJan Schmidt 	return ret;
2122475f6387SJan Schmidt }
2123475f6387SJan Schmidt 
212476dda93cSYan, Zheng static noinline long btrfs_ioctl_clone(struct file *file, unsigned long srcfd,
2125b2950863SChristoph Hellwig 				       u64 off, u64 olen, u64 destoff)
2126f46b5a66SChristoph Hellwig {
2127f46b5a66SChristoph Hellwig 	struct inode *inode = fdentry(file)->d_inode;
2128f46b5a66SChristoph Hellwig 	struct btrfs_root *root = BTRFS_I(inode)->root;
2129f46b5a66SChristoph Hellwig 	struct file *src_file;
2130f46b5a66SChristoph Hellwig 	struct inode *src;
2131f46b5a66SChristoph Hellwig 	struct btrfs_trans_handle *trans;
2132f46b5a66SChristoph Hellwig 	struct btrfs_path *path;
2133f46b5a66SChristoph Hellwig 	struct extent_buffer *leaf;
2134ae01a0abSYan Zheng 	char *buf;
2135ae01a0abSYan Zheng 	struct btrfs_key key;
2136f46b5a66SChristoph Hellwig 	u32 nritems;
2137f46b5a66SChristoph Hellwig 	int slot;
2138ae01a0abSYan Zheng 	int ret;
2139c5c9cd4dSSage Weil 	u64 len = olen;
2140c5c9cd4dSSage Weil 	u64 bs = root->fs_info->sb->s_blocksize;
2141c5c9cd4dSSage Weil 	u64 hint_byte;
2142d20f7043SChris Mason 
2143c5c9cd4dSSage Weil 	/*
2144c5c9cd4dSSage Weil 	 * TODO:
2145c5c9cd4dSSage Weil 	 * - split compressed inline extents.  annoying: we need to
2146c5c9cd4dSSage Weil 	 *   decompress into destination's address_space (the file offset
2147c5c9cd4dSSage Weil 	 *   may change, so source mapping won't do), then recompress (or
2148c5c9cd4dSSage Weil 	 *   otherwise reinsert) a subrange.
2149c5c9cd4dSSage Weil 	 * - allow ranges within the same file to be cloned (provided
2150c5c9cd4dSSage Weil 	 *   they don't overlap)?
2151c5c9cd4dSSage Weil 	 */
2152c5c9cd4dSSage Weil 
2153e441d54dSChris Mason 	/* the destination must be opened for writing */
21542ebc3464SDan Rosenberg 	if (!(file->f_mode & FMODE_WRITE) || (file->f_flags & O_APPEND))
2155e441d54dSChris Mason 		return -EINVAL;
2156e441d54dSChris Mason 
2157b83cc969SLi Zefan 	if (btrfs_root_readonly(root))
2158b83cc969SLi Zefan 		return -EROFS;
2159b83cc969SLi Zefan 
2160c146afadSYan Zheng 	ret = mnt_want_write(file->f_path.mnt);
2161c146afadSYan Zheng 	if (ret)
2162c146afadSYan Zheng 		return ret;
2163c146afadSYan Zheng 
2164c5c9cd4dSSage Weil 	src_file = fget(srcfd);
2165ab67b7c1SYan Zheng 	if (!src_file) {
2166ab67b7c1SYan Zheng 		ret = -EBADF;
2167ab67b7c1SYan Zheng 		goto out_drop_write;
2168ab67b7c1SYan Zheng 	}
21695dc64164SDan Rosenberg 
2170f46b5a66SChristoph Hellwig 	src = src_file->f_dentry->d_inode;
2171f46b5a66SChristoph Hellwig 
2172c5c9cd4dSSage Weil 	ret = -EINVAL;
2173c5c9cd4dSSage Weil 	if (src == inode)
2174c5c9cd4dSSage Weil 		goto out_fput;
2175c5c9cd4dSSage Weil 
21765dc64164SDan Rosenberg 	/* the src must be open for reading */
21775dc64164SDan Rosenberg 	if (!(src_file->f_mode & FMODE_READ))
21785dc64164SDan Rosenberg 		goto out_fput;
21795dc64164SDan Rosenberg 
2180ae01a0abSYan Zheng 	ret = -EISDIR;
2181ae01a0abSYan Zheng 	if (S_ISDIR(src->i_mode) || S_ISDIR(inode->i_mode))
2182f46b5a66SChristoph Hellwig 		goto out_fput;
2183f46b5a66SChristoph Hellwig 
2184ae01a0abSYan Zheng 	ret = -EXDEV;
2185ae01a0abSYan Zheng 	if (src->i_sb != inode->i_sb || BTRFS_I(src)->root != root)
2186ae01a0abSYan Zheng 		goto out_fput;
2187ae01a0abSYan Zheng 
2188ae01a0abSYan Zheng 	ret = -ENOMEM;
2189ae01a0abSYan Zheng 	buf = vmalloc(btrfs_level_size(root, 0));
2190ae01a0abSYan Zheng 	if (!buf)
2191ae01a0abSYan Zheng 		goto out_fput;
2192ae01a0abSYan Zheng 
2193ae01a0abSYan Zheng 	path = btrfs_alloc_path();
2194ae01a0abSYan Zheng 	if (!path) {
2195ae01a0abSYan Zheng 		vfree(buf);
2196ae01a0abSYan Zheng 		goto out_fput;
2197ae01a0abSYan Zheng 	}
2198ae01a0abSYan Zheng 	path->reada = 2;
2199ae01a0abSYan Zheng 
2200f46b5a66SChristoph Hellwig 	if (inode < src) {
2201fccdae43SSage Weil 		mutex_lock_nested(&inode->i_mutex, I_MUTEX_PARENT);
2202fccdae43SSage Weil 		mutex_lock_nested(&src->i_mutex, I_MUTEX_CHILD);
2203f46b5a66SChristoph Hellwig 	} else {
2204fccdae43SSage Weil 		mutex_lock_nested(&src->i_mutex, I_MUTEX_PARENT);
2205fccdae43SSage Weil 		mutex_lock_nested(&inode->i_mutex, I_MUTEX_CHILD);
2206f46b5a66SChristoph Hellwig 	}
2207f46b5a66SChristoph Hellwig 
2208c5c9cd4dSSage Weil 	/* determine range to clone */
2209c5c9cd4dSSage Weil 	ret = -EINVAL;
22102ebc3464SDan Rosenberg 	if (off + len > src->i_size || off + len < off)
2211f46b5a66SChristoph Hellwig 		goto out_unlock;
2212c5c9cd4dSSage Weil 	if (len == 0)
2213c5c9cd4dSSage Weil 		olen = len = src->i_size - off;
2214c5c9cd4dSSage Weil 	/* if we extend to eof, continue to block boundary */
2215c5c9cd4dSSage Weil 	if (off + len == src->i_size)
22162a6b8daeSLi Zefan 		len = ALIGN(src->i_size, bs) - off;
2217c5c9cd4dSSage Weil 
2218c5c9cd4dSSage Weil 	/* verify the end result is block aligned */
22192a6b8daeSLi Zefan 	if (!IS_ALIGNED(off, bs) || !IS_ALIGNED(off + len, bs) ||
22202a6b8daeSLi Zefan 	    !IS_ALIGNED(destoff, bs))
2221c5c9cd4dSSage Weil 		goto out_unlock;
2222c5c9cd4dSSage Weil 
2223f46b5a66SChristoph Hellwig 	/* do any pending delalloc/csum calc on src, one way or
2224f46b5a66SChristoph Hellwig 	   another, and lock file content */
2225f46b5a66SChristoph Hellwig 	while (1) {
222631840ae1SZheng Yan 		struct btrfs_ordered_extent *ordered;
2227c5c9cd4dSSage Weil 		lock_extent(&BTRFS_I(src)->io_tree, off, off+len, GFP_NOFS);
22289a019196SSage Weil 		ordered = btrfs_lookup_first_ordered_extent(src, off+len);
22299a019196SSage Weil 		if (!ordered &&
22309a019196SSage Weil 		    !test_range_bit(&BTRFS_I(src)->io_tree, off, off+len,
22319a019196SSage Weil 				   EXTENT_DELALLOC, 0, NULL))
2232f46b5a66SChristoph Hellwig 			break;
2233c5c9cd4dSSage Weil 		unlock_extent(&BTRFS_I(src)->io_tree, off, off+len, GFP_NOFS);
2234ae01a0abSYan Zheng 		if (ordered)
2235ae01a0abSYan Zheng 			btrfs_put_ordered_extent(ordered);
22369a019196SSage Weil 		btrfs_wait_ordered_range(src, off, len);
2237f46b5a66SChristoph Hellwig 	}
2238f46b5a66SChristoph Hellwig 
2239c5c9cd4dSSage Weil 	/* clone data */
224033345d01SLi Zefan 	key.objectid = btrfs_ino(src);
2241ae01a0abSYan Zheng 	key.type = BTRFS_EXTENT_DATA_KEY;
2242ae01a0abSYan Zheng 	key.offset = 0;
2243f46b5a66SChristoph Hellwig 
2244f46b5a66SChristoph Hellwig 	while (1) {
2245f46b5a66SChristoph Hellwig 		/*
2246f46b5a66SChristoph Hellwig 		 * note the key will change type as we walk through the
2247f46b5a66SChristoph Hellwig 		 * tree.
2248f46b5a66SChristoph Hellwig 		 */
2249a22285a6SYan, Zheng 		ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2250f46b5a66SChristoph Hellwig 		if (ret < 0)
2251f46b5a66SChristoph Hellwig 			goto out;
2252f46b5a66SChristoph Hellwig 
2253ae01a0abSYan Zheng 		nritems = btrfs_header_nritems(path->nodes[0]);
2254ae01a0abSYan Zheng 		if (path->slots[0] >= nritems) {
2255f46b5a66SChristoph Hellwig 			ret = btrfs_next_leaf(root, path);
2256f46b5a66SChristoph Hellwig 			if (ret < 0)
2257f46b5a66SChristoph Hellwig 				goto out;
2258f46b5a66SChristoph Hellwig 			if (ret > 0)
2259f46b5a66SChristoph Hellwig 				break;
2260ae01a0abSYan Zheng 			nritems = btrfs_header_nritems(path->nodes[0]);
2261f46b5a66SChristoph Hellwig 		}
2262f46b5a66SChristoph Hellwig 		leaf = path->nodes[0];
2263f46b5a66SChristoph Hellwig 		slot = path->slots[0];
2264f46b5a66SChristoph Hellwig 
2265ae01a0abSYan Zheng 		btrfs_item_key_to_cpu(leaf, &key, slot);
2266d20f7043SChris Mason 		if (btrfs_key_type(&key) > BTRFS_EXTENT_DATA_KEY ||
226733345d01SLi Zefan 		    key.objectid != btrfs_ino(src))
2268f46b5a66SChristoph Hellwig 			break;
2269f46b5a66SChristoph Hellwig 
2270c5c9cd4dSSage Weil 		if (btrfs_key_type(&key) == BTRFS_EXTENT_DATA_KEY) {
2271c5c9cd4dSSage Weil 			struct btrfs_file_extent_item *extent;
2272c5c9cd4dSSage Weil 			int type;
227331840ae1SZheng Yan 			u32 size;
227431840ae1SZheng Yan 			struct btrfs_key new_key;
2275c5c9cd4dSSage Weil 			u64 disko = 0, diskl = 0;
2276c5c9cd4dSSage Weil 			u64 datao = 0, datal = 0;
2277c5c9cd4dSSage Weil 			u8 comp;
2278b5384d48SSage Weil 			u64 endoff;
227931840ae1SZheng Yan 
228031840ae1SZheng Yan 			size = btrfs_item_size_nr(leaf, slot);
228131840ae1SZheng Yan 			read_extent_buffer(leaf, buf,
228231840ae1SZheng Yan 					   btrfs_item_ptr_offset(leaf, slot),
228331840ae1SZheng Yan 					   size);
2284c5c9cd4dSSage Weil 
2285c5c9cd4dSSage Weil 			extent = btrfs_item_ptr(leaf, slot,
2286c5c9cd4dSSage Weil 						struct btrfs_file_extent_item);
2287c5c9cd4dSSage Weil 			comp = btrfs_file_extent_compression(leaf, extent);
2288c5c9cd4dSSage Weil 			type = btrfs_file_extent_type(leaf, extent);
2289c8a894d7SChris Mason 			if (type == BTRFS_FILE_EXTENT_REG ||
2290c8a894d7SChris Mason 			    type == BTRFS_FILE_EXTENT_PREALLOC) {
2291d397712bSChris Mason 				disko = btrfs_file_extent_disk_bytenr(leaf,
2292d397712bSChris Mason 								      extent);
2293d397712bSChris Mason 				diskl = btrfs_file_extent_disk_num_bytes(leaf,
2294d397712bSChris Mason 								 extent);
2295c5c9cd4dSSage Weil 				datao = btrfs_file_extent_offset(leaf, extent);
2296d397712bSChris Mason 				datal = btrfs_file_extent_num_bytes(leaf,
2297d397712bSChris Mason 								    extent);
2298c5c9cd4dSSage Weil 			} else if (type == BTRFS_FILE_EXTENT_INLINE) {
2299c5c9cd4dSSage Weil 				/* take upper bound, may be compressed */
2300c5c9cd4dSSage Weil 				datal = btrfs_file_extent_ram_bytes(leaf,
2301c5c9cd4dSSage Weil 								    extent);
2302c5c9cd4dSSage Weil 			}
2303b3b4aa74SDavid Sterba 			btrfs_release_path(path);
230431840ae1SZheng Yan 
2305050006a7SSage Weil 			if (key.offset + datal <= off ||
2306c5c9cd4dSSage Weil 			    key.offset >= off+len)
2307c5c9cd4dSSage Weil 				goto next;
2308c5c9cd4dSSage Weil 
230931840ae1SZheng Yan 			memcpy(&new_key, &key, sizeof(new_key));
231033345d01SLi Zefan 			new_key.objectid = btrfs_ino(inode);
23114d728ec7SLi Zefan 			if (off <= key.offset)
2312c5c9cd4dSSage Weil 				new_key.offset = key.offset + destoff - off;
23134d728ec7SLi Zefan 			else
23144d728ec7SLi Zefan 				new_key.offset = destoff;
2315c5c9cd4dSSage Weil 
2316a22285a6SYan, Zheng 			trans = btrfs_start_transaction(root, 1);
2317a22285a6SYan, Zheng 			if (IS_ERR(trans)) {
2318a22285a6SYan, Zheng 				ret = PTR_ERR(trans);
2319a22285a6SYan, Zheng 				goto out;
2320a22285a6SYan, Zheng 			}
2321a22285a6SYan, Zheng 
2322c8a894d7SChris Mason 			if (type == BTRFS_FILE_EXTENT_REG ||
2323c8a894d7SChris Mason 			    type == BTRFS_FILE_EXTENT_PREALLOC) {
2324a22285a6SYan, Zheng 				if (off > key.offset) {
2325a22285a6SYan, Zheng 					datao += off - key.offset;
2326a22285a6SYan, Zheng 					datal -= off - key.offset;
2327a22285a6SYan, Zheng 				}
2328a22285a6SYan, Zheng 
2329a22285a6SYan, Zheng 				if (key.offset + datal > off + len)
2330a22285a6SYan, Zheng 					datal = off + len - key.offset;
2331a22285a6SYan, Zheng 
2332a22285a6SYan, Zheng 				ret = btrfs_drop_extents(trans, inode,
2333a22285a6SYan, Zheng 							 new_key.offset,
2334a22285a6SYan, Zheng 							 new_key.offset + datal,
2335a22285a6SYan, Zheng 							 &hint_byte, 1);
2336a22285a6SYan, Zheng 				BUG_ON(ret);
2337a22285a6SYan, Zheng 
233831840ae1SZheng Yan 				ret = btrfs_insert_empty_item(trans, root, path,
233931840ae1SZheng Yan 							      &new_key, size);
2340a22285a6SYan, Zheng 				BUG_ON(ret);
234131840ae1SZheng Yan 
234231840ae1SZheng Yan 				leaf = path->nodes[0];
234331840ae1SZheng Yan 				slot = path->slots[0];
234431840ae1SZheng Yan 				write_extent_buffer(leaf, buf,
234531840ae1SZheng Yan 					    btrfs_item_ptr_offset(leaf, slot),
234631840ae1SZheng Yan 					    size);
2347ae01a0abSYan Zheng 
2348f46b5a66SChristoph Hellwig 				extent = btrfs_item_ptr(leaf, slot,
2349f46b5a66SChristoph Hellwig 						struct btrfs_file_extent_item);
2350c5c9cd4dSSage Weil 
2351c5c9cd4dSSage Weil 				/* disko == 0 means it's a hole */
2352c5c9cd4dSSage Weil 				if (!disko)
2353c5c9cd4dSSage Weil 					datao = 0;
2354c5c9cd4dSSage Weil 
2355c5c9cd4dSSage Weil 				btrfs_set_file_extent_offset(leaf, extent,
2356c5c9cd4dSSage Weil 							     datao);
2357c5c9cd4dSSage Weil 				btrfs_set_file_extent_num_bytes(leaf, extent,
2358c5c9cd4dSSage Weil 								datal);
2359c5c9cd4dSSage Weil 				if (disko) {
2360c5c9cd4dSSage Weil 					inode_add_bytes(inode, datal);
2361ae01a0abSYan Zheng 					ret = btrfs_inc_extent_ref(trans, root,
23625d4f98a2SYan Zheng 							disko, diskl, 0,
2363f46b5a66SChristoph Hellwig 							root->root_key.objectid,
236433345d01SLi Zefan 							btrfs_ino(inode),
23655d4f98a2SYan Zheng 							new_key.offset - datao);
2366ae01a0abSYan Zheng 					BUG_ON(ret);
2367f46b5a66SChristoph Hellwig 				}
2368c5c9cd4dSSage Weil 			} else if (type == BTRFS_FILE_EXTENT_INLINE) {
2369c5c9cd4dSSage Weil 				u64 skip = 0;
2370c5c9cd4dSSage Weil 				u64 trim = 0;
2371c5c9cd4dSSage Weil 				if (off > key.offset) {
2372c5c9cd4dSSage Weil 					skip = off - key.offset;
2373c5c9cd4dSSage Weil 					new_key.offset += skip;
237431840ae1SZheng Yan 				}
2375d397712bSChris Mason 
2376c5c9cd4dSSage Weil 				if (key.offset + datal > off+len)
2377c5c9cd4dSSage Weil 					trim = key.offset + datal - (off+len);
2378d397712bSChris Mason 
2379c5c9cd4dSSage Weil 				if (comp && (skip || trim)) {
2380c5c9cd4dSSage Weil 					ret = -EINVAL;
2381a22285a6SYan, Zheng 					btrfs_end_transaction(trans, root);
2382c5c9cd4dSSage Weil 					goto out;
238331840ae1SZheng Yan 				}
2384c5c9cd4dSSage Weil 				size -= skip + trim;
2385c5c9cd4dSSage Weil 				datal -= skip + trim;
2386a22285a6SYan, Zheng 
2387a22285a6SYan, Zheng 				ret = btrfs_drop_extents(trans, inode,
2388a22285a6SYan, Zheng 							 new_key.offset,
2389a22285a6SYan, Zheng 							 new_key.offset + datal,
2390a22285a6SYan, Zheng 							 &hint_byte, 1);
2391a22285a6SYan, Zheng 				BUG_ON(ret);
2392a22285a6SYan, Zheng 
2393c5c9cd4dSSage Weil 				ret = btrfs_insert_empty_item(trans, root, path,
2394c5c9cd4dSSage Weil 							      &new_key, size);
2395a22285a6SYan, Zheng 				BUG_ON(ret);
2396c5c9cd4dSSage Weil 
2397c5c9cd4dSSage Weil 				if (skip) {
2398d397712bSChris Mason 					u32 start =
2399d397712bSChris Mason 					  btrfs_file_extent_calc_inline_size(0);
2400c5c9cd4dSSage Weil 					memmove(buf+start, buf+start+skip,
2401c5c9cd4dSSage Weil 						datal);
2402c5c9cd4dSSage Weil 				}
2403c5c9cd4dSSage Weil 
2404c5c9cd4dSSage Weil 				leaf = path->nodes[0];
2405c5c9cd4dSSage Weil 				slot = path->slots[0];
2406c5c9cd4dSSage Weil 				write_extent_buffer(leaf, buf,
2407c5c9cd4dSSage Weil 					    btrfs_item_ptr_offset(leaf, slot),
2408c5c9cd4dSSage Weil 					    size);
2409c5c9cd4dSSage Weil 				inode_add_bytes(inode, datal);
2410c5c9cd4dSSage Weil 			}
2411c5c9cd4dSSage Weil 
2412c5c9cd4dSSage Weil 			btrfs_mark_buffer_dirty(leaf);
2413b3b4aa74SDavid Sterba 			btrfs_release_path(path);
2414c5c9cd4dSSage Weil 
2415a22285a6SYan, Zheng 			inode->i_mtime = inode->i_ctime = CURRENT_TIME;
2416b5384d48SSage Weil 
2417b5384d48SSage Weil 			/*
2418b5384d48SSage Weil 			 * we round up to the block size at eof when
2419b5384d48SSage Weil 			 * determining which extents to clone above,
2420b5384d48SSage Weil 			 * but shouldn't round up the file size
2421b5384d48SSage Weil 			 */
2422b5384d48SSage Weil 			endoff = new_key.offset + datal;
24235f3888ffSLi Zefan 			if (endoff > destoff+olen)
24245f3888ffSLi Zefan 				endoff = destoff+olen;
2425b5384d48SSage Weil 			if (endoff > inode->i_size)
2426b5384d48SSage Weil 				btrfs_i_size_write(inode, endoff);
2427b5384d48SSage Weil 
2428a22285a6SYan, Zheng 			BTRFS_I(inode)->flags = BTRFS_I(src)->flags;
2429a22285a6SYan, Zheng 			ret = btrfs_update_inode(trans, root, inode);
2430a22285a6SYan, Zheng 			BUG_ON(ret);
2431a22285a6SYan, Zheng 			btrfs_end_transaction(trans, root);
2432a22285a6SYan, Zheng 		}
2433c5c9cd4dSSage Weil next:
2434b3b4aa74SDavid Sterba 		btrfs_release_path(path);
2435ae01a0abSYan Zheng 		key.offset++;
2436ae01a0abSYan Zheng 	}
2437f46b5a66SChristoph Hellwig 	ret = 0;
2438f46b5a66SChristoph Hellwig out:
2439b3b4aa74SDavid Sterba 	btrfs_release_path(path);
2440c5c9cd4dSSage Weil 	unlock_extent(&BTRFS_I(src)->io_tree, off, off+len, GFP_NOFS);
2441f46b5a66SChristoph Hellwig out_unlock:
2442f46b5a66SChristoph Hellwig 	mutex_unlock(&src->i_mutex);
2443f46b5a66SChristoph Hellwig 	mutex_unlock(&inode->i_mutex);
2444ae01a0abSYan Zheng 	vfree(buf);
2445ae01a0abSYan Zheng 	btrfs_free_path(path);
2446f46b5a66SChristoph Hellwig out_fput:
2447f46b5a66SChristoph Hellwig 	fput(src_file);
2448ab67b7c1SYan Zheng out_drop_write:
2449ab67b7c1SYan Zheng 	mnt_drop_write(file->f_path.mnt);
2450f46b5a66SChristoph Hellwig 	return ret;
2451f46b5a66SChristoph Hellwig }
2452f46b5a66SChristoph Hellwig 
24537a865e8aSChristoph Hellwig static long btrfs_ioctl_clone_range(struct file *file, void __user *argp)
2454c5c9cd4dSSage Weil {
2455c5c9cd4dSSage Weil 	struct btrfs_ioctl_clone_range_args args;
2456c5c9cd4dSSage Weil 
24577a865e8aSChristoph Hellwig 	if (copy_from_user(&args, argp, sizeof(args)))
2458c5c9cd4dSSage Weil 		return -EFAULT;
2459c5c9cd4dSSage Weil 	return btrfs_ioctl_clone(file, args.src_fd, args.src_offset,
2460c5c9cd4dSSage Weil 				 args.src_length, args.dest_offset);
2461c5c9cd4dSSage Weil }
2462c5c9cd4dSSage Weil 
2463f46b5a66SChristoph Hellwig /*
2464f46b5a66SChristoph Hellwig  * there are many ways the trans_start and trans_end ioctls can lead
2465f46b5a66SChristoph Hellwig  * to deadlocks.  They should only be used by applications that
2466f46b5a66SChristoph Hellwig  * basically own the machine, and have a very in depth understanding
2467f46b5a66SChristoph Hellwig  * of all the possible deadlocks and enospc problems.
2468f46b5a66SChristoph Hellwig  */
2469b2950863SChristoph Hellwig static long btrfs_ioctl_trans_start(struct file *file)
2470f46b5a66SChristoph Hellwig {
2471f46b5a66SChristoph Hellwig 	struct inode *inode = fdentry(file)->d_inode;
2472f46b5a66SChristoph Hellwig 	struct btrfs_root *root = BTRFS_I(inode)->root;
2473f46b5a66SChristoph Hellwig 	struct btrfs_trans_handle *trans;
24741ab86aedSSage Weil 	int ret;
2475f46b5a66SChristoph Hellwig 
24761ab86aedSSage Weil 	ret = -EPERM;
2477df5b5520SChristoph Hellwig 	if (!capable(CAP_SYS_ADMIN))
2478f46b5a66SChristoph Hellwig 		goto out;
24791ab86aedSSage Weil 
24801ab86aedSSage Weil 	ret = -EINPROGRESS;
24811ab86aedSSage Weil 	if (file->private_data)
24821ab86aedSSage Weil 		goto out;
24839ca9ee09SSage Weil 
2484b83cc969SLi Zefan 	ret = -EROFS;
2485b83cc969SLi Zefan 	if (btrfs_root_readonly(root))
2486b83cc969SLi Zefan 		goto out;
2487b83cc969SLi Zefan 
2488c146afadSYan Zheng 	ret = mnt_want_write(file->f_path.mnt);
2489c146afadSYan Zheng 	if (ret)
2490c146afadSYan Zheng 		goto out;
2491c146afadSYan Zheng 
24929ca9ee09SSage Weil 	mutex_lock(&root->fs_info->trans_mutex);
24939ca9ee09SSage Weil 	root->fs_info->open_ioctl_trans++;
24949ca9ee09SSage Weil 	mutex_unlock(&root->fs_info->trans_mutex);
24959ca9ee09SSage Weil 
2496f46b5a66SChristoph Hellwig 	ret = -ENOMEM;
24971ab86aedSSage Weil 	trans = btrfs_start_ioctl_transaction(root, 0);
2498abd30bb0STsutomu Itoh 	if (IS_ERR(trans))
24991ab86aedSSage Weil 		goto out_drop;
25001ab86aedSSage Weil 
25011ab86aedSSage Weil 	file->private_data = trans;
25021ab86aedSSage Weil 	return 0;
25031ab86aedSSage Weil 
25041ab86aedSSage Weil out_drop:
25051ab86aedSSage Weil 	mutex_lock(&root->fs_info->trans_mutex);
25061ab86aedSSage Weil 	root->fs_info->open_ioctl_trans--;
25071ab86aedSSage Weil 	mutex_unlock(&root->fs_info->trans_mutex);
25081ab86aedSSage Weil 	mnt_drop_write(file->f_path.mnt);
2509f46b5a66SChristoph Hellwig out:
2510f46b5a66SChristoph Hellwig 	return ret;
2511f46b5a66SChristoph Hellwig }
2512f46b5a66SChristoph Hellwig 
25136ef5ed0dSJosef Bacik static long btrfs_ioctl_default_subvol(struct file *file, void __user *argp)
25146ef5ed0dSJosef Bacik {
25156ef5ed0dSJosef Bacik 	struct inode *inode = fdentry(file)->d_inode;
25166ef5ed0dSJosef Bacik 	struct btrfs_root *root = BTRFS_I(inode)->root;
25176ef5ed0dSJosef Bacik 	struct btrfs_root *new_root;
25186ef5ed0dSJosef Bacik 	struct btrfs_dir_item *di;
25196ef5ed0dSJosef Bacik 	struct btrfs_trans_handle *trans;
25206ef5ed0dSJosef Bacik 	struct btrfs_path *path;
25216ef5ed0dSJosef Bacik 	struct btrfs_key location;
25226ef5ed0dSJosef Bacik 	struct btrfs_disk_key disk_key;
25236ef5ed0dSJosef Bacik 	struct btrfs_super_block *disk_super;
25246ef5ed0dSJosef Bacik 	u64 features;
25256ef5ed0dSJosef Bacik 	u64 objectid = 0;
25266ef5ed0dSJosef Bacik 	u64 dir_id;
25276ef5ed0dSJosef Bacik 
25286ef5ed0dSJosef Bacik 	if (!capable(CAP_SYS_ADMIN))
25296ef5ed0dSJosef Bacik 		return -EPERM;
25306ef5ed0dSJosef Bacik 
25316ef5ed0dSJosef Bacik 	if (copy_from_user(&objectid, argp, sizeof(objectid)))
25326ef5ed0dSJosef Bacik 		return -EFAULT;
25336ef5ed0dSJosef Bacik 
25346ef5ed0dSJosef Bacik 	if (!objectid)
25356ef5ed0dSJosef Bacik 		objectid = root->root_key.objectid;
25366ef5ed0dSJosef Bacik 
25376ef5ed0dSJosef Bacik 	location.objectid = objectid;
25386ef5ed0dSJosef Bacik 	location.type = BTRFS_ROOT_ITEM_KEY;
25396ef5ed0dSJosef Bacik 	location.offset = (u64)-1;
25406ef5ed0dSJosef Bacik 
25416ef5ed0dSJosef Bacik 	new_root = btrfs_read_fs_root_no_name(root->fs_info, &location);
25426ef5ed0dSJosef Bacik 	if (IS_ERR(new_root))
25436ef5ed0dSJosef Bacik 		return PTR_ERR(new_root);
25446ef5ed0dSJosef Bacik 
25456ef5ed0dSJosef Bacik 	if (btrfs_root_refs(&new_root->root_item) == 0)
25466ef5ed0dSJosef Bacik 		return -ENOENT;
25476ef5ed0dSJosef Bacik 
25486ef5ed0dSJosef Bacik 	path = btrfs_alloc_path();
25496ef5ed0dSJosef Bacik 	if (!path)
25506ef5ed0dSJosef Bacik 		return -ENOMEM;
25516ef5ed0dSJosef Bacik 	path->leave_spinning = 1;
25526ef5ed0dSJosef Bacik 
25536ef5ed0dSJosef Bacik 	trans = btrfs_start_transaction(root, 1);
255498d5dc13STsutomu Itoh 	if (IS_ERR(trans)) {
25556ef5ed0dSJosef Bacik 		btrfs_free_path(path);
255698d5dc13STsutomu Itoh 		return PTR_ERR(trans);
25576ef5ed0dSJosef Bacik 	}
25586ef5ed0dSJosef Bacik 
25596ef5ed0dSJosef Bacik 	dir_id = btrfs_super_root_dir(&root->fs_info->super_copy);
25606ef5ed0dSJosef Bacik 	di = btrfs_lookup_dir_item(trans, root->fs_info->tree_root, path,
25616ef5ed0dSJosef Bacik 				   dir_id, "default", 7, 1);
2562cf1e99a4SDan Carpenter 	if (IS_ERR_OR_NULL(di)) {
25636ef5ed0dSJosef Bacik 		btrfs_free_path(path);
25646ef5ed0dSJosef Bacik 		btrfs_end_transaction(trans, root);
25656ef5ed0dSJosef Bacik 		printk(KERN_ERR "Umm, you don't have the default dir item, "
25666ef5ed0dSJosef Bacik 		       "this isn't going to work\n");
25676ef5ed0dSJosef Bacik 		return -ENOENT;
25686ef5ed0dSJosef Bacik 	}
25696ef5ed0dSJosef Bacik 
25706ef5ed0dSJosef Bacik 	btrfs_cpu_key_to_disk(&disk_key, &new_root->root_key);
25716ef5ed0dSJosef Bacik 	btrfs_set_dir_item_key(path->nodes[0], di, &disk_key);
25726ef5ed0dSJosef Bacik 	btrfs_mark_buffer_dirty(path->nodes[0]);
25736ef5ed0dSJosef Bacik 	btrfs_free_path(path);
25746ef5ed0dSJosef Bacik 
25756ef5ed0dSJosef Bacik 	disk_super = &root->fs_info->super_copy;
25766ef5ed0dSJosef Bacik 	features = btrfs_super_incompat_flags(disk_super);
25776ef5ed0dSJosef Bacik 	if (!(features & BTRFS_FEATURE_INCOMPAT_DEFAULT_SUBVOL)) {
25786ef5ed0dSJosef Bacik 		features |= BTRFS_FEATURE_INCOMPAT_DEFAULT_SUBVOL;
25796ef5ed0dSJosef Bacik 		btrfs_set_super_incompat_flags(disk_super, features);
25806ef5ed0dSJosef Bacik 	}
25816ef5ed0dSJosef Bacik 	btrfs_end_transaction(trans, root);
25826ef5ed0dSJosef Bacik 
25836ef5ed0dSJosef Bacik 	return 0;
25846ef5ed0dSJosef Bacik }
25856ef5ed0dSJosef Bacik 
2586bf5fc093SJosef Bacik static void get_block_group_info(struct list_head *groups_list,
2587bf5fc093SJosef Bacik 				 struct btrfs_ioctl_space_info *space)
2588bf5fc093SJosef Bacik {
2589bf5fc093SJosef Bacik 	struct btrfs_block_group_cache *block_group;
2590bf5fc093SJosef Bacik 
2591bf5fc093SJosef Bacik 	space->total_bytes = 0;
2592bf5fc093SJosef Bacik 	space->used_bytes = 0;
2593bf5fc093SJosef Bacik 	space->flags = 0;
2594bf5fc093SJosef Bacik 	list_for_each_entry(block_group, groups_list, list) {
2595bf5fc093SJosef Bacik 		space->flags = block_group->flags;
2596bf5fc093SJosef Bacik 		space->total_bytes += block_group->key.offset;
2597bf5fc093SJosef Bacik 		space->used_bytes +=
2598bf5fc093SJosef Bacik 			btrfs_block_group_used(&block_group->item);
2599bf5fc093SJosef Bacik 	}
2600bf5fc093SJosef Bacik }
2601bf5fc093SJosef Bacik 
26021406e432SJosef Bacik long btrfs_ioctl_space_info(struct btrfs_root *root, void __user *arg)
26031406e432SJosef Bacik {
26041406e432SJosef Bacik 	struct btrfs_ioctl_space_args space_args;
26051406e432SJosef Bacik 	struct btrfs_ioctl_space_info space;
26061406e432SJosef Bacik 	struct btrfs_ioctl_space_info *dest;
26077fde62bfSChris Mason 	struct btrfs_ioctl_space_info *dest_orig;
260813f2696fSDaniel J Blueman 	struct btrfs_ioctl_space_info __user *user_dest;
26091406e432SJosef Bacik 	struct btrfs_space_info *info;
2610bf5fc093SJosef Bacik 	u64 types[] = {BTRFS_BLOCK_GROUP_DATA,
2611bf5fc093SJosef Bacik 		       BTRFS_BLOCK_GROUP_SYSTEM,
2612bf5fc093SJosef Bacik 		       BTRFS_BLOCK_GROUP_METADATA,
2613bf5fc093SJosef Bacik 		       BTRFS_BLOCK_GROUP_DATA | BTRFS_BLOCK_GROUP_METADATA};
2614bf5fc093SJosef Bacik 	int num_types = 4;
26157fde62bfSChris Mason 	int alloc_size;
26161406e432SJosef Bacik 	int ret = 0;
261751788b1bSDan Rosenberg 	u64 slot_count = 0;
2618bf5fc093SJosef Bacik 	int i, c;
26191406e432SJosef Bacik 
26201406e432SJosef Bacik 	if (copy_from_user(&space_args,
26211406e432SJosef Bacik 			   (struct btrfs_ioctl_space_args __user *)arg,
26221406e432SJosef Bacik 			   sizeof(space_args)))
26231406e432SJosef Bacik 		return -EFAULT;
26241406e432SJosef Bacik 
2625bf5fc093SJosef Bacik 	for (i = 0; i < num_types; i++) {
2626bf5fc093SJosef Bacik 		struct btrfs_space_info *tmp;
2627bf5fc093SJosef Bacik 
2628bf5fc093SJosef Bacik 		info = NULL;
26297fde62bfSChris Mason 		rcu_read_lock();
2630bf5fc093SJosef Bacik 		list_for_each_entry_rcu(tmp, &root->fs_info->space_info,
2631bf5fc093SJosef Bacik 					list) {
2632bf5fc093SJosef Bacik 			if (tmp->flags == types[i]) {
2633bf5fc093SJosef Bacik 				info = tmp;
2634bf5fc093SJosef Bacik 				break;
2635bf5fc093SJosef Bacik 			}
2636bf5fc093SJosef Bacik 		}
26377fde62bfSChris Mason 		rcu_read_unlock();
26381406e432SJosef Bacik 
2639bf5fc093SJosef Bacik 		if (!info)
2640bf5fc093SJosef Bacik 			continue;
2641bf5fc093SJosef Bacik 
2642bf5fc093SJosef Bacik 		down_read(&info->groups_sem);
2643bf5fc093SJosef Bacik 		for (c = 0; c < BTRFS_NR_RAID_TYPES; c++) {
2644bf5fc093SJosef Bacik 			if (!list_empty(&info->block_groups[c]))
2645bf5fc093SJosef Bacik 				slot_count++;
2646bf5fc093SJosef Bacik 		}
2647bf5fc093SJosef Bacik 		up_read(&info->groups_sem);
2648bf5fc093SJosef Bacik 	}
2649bf5fc093SJosef Bacik 
26507fde62bfSChris Mason 	/* space_slots == 0 means they are asking for a count */
26517fde62bfSChris Mason 	if (space_args.space_slots == 0) {
26527fde62bfSChris Mason 		space_args.total_spaces = slot_count;
26537fde62bfSChris Mason 		goto out;
26547fde62bfSChris Mason 	}
2655bf5fc093SJosef Bacik 
265651788b1bSDan Rosenberg 	slot_count = min_t(u64, space_args.space_slots, slot_count);
2657bf5fc093SJosef Bacik 
26587fde62bfSChris Mason 	alloc_size = sizeof(*dest) * slot_count;
2659bf5fc093SJosef Bacik 
26607fde62bfSChris Mason 	/* we generally have at most 6 or so space infos, one for each raid
26617fde62bfSChris Mason 	 * level.  So, a whole page should be more than enough for everyone
26627fde62bfSChris Mason 	 */
26637fde62bfSChris Mason 	if (alloc_size > PAGE_CACHE_SIZE)
26647fde62bfSChris Mason 		return -ENOMEM;
26657fde62bfSChris Mason 
26667fde62bfSChris Mason 	space_args.total_spaces = 0;
26677fde62bfSChris Mason 	dest = kmalloc(alloc_size, GFP_NOFS);
26687fde62bfSChris Mason 	if (!dest)
26697fde62bfSChris Mason 		return -ENOMEM;
26707fde62bfSChris Mason 	dest_orig = dest;
26717fde62bfSChris Mason 
26727fde62bfSChris Mason 	/* now we have a buffer to copy into */
2673bf5fc093SJosef Bacik 	for (i = 0; i < num_types; i++) {
2674bf5fc093SJosef Bacik 		struct btrfs_space_info *tmp;
2675bf5fc093SJosef Bacik 
267651788b1bSDan Rosenberg 		if (!slot_count)
267751788b1bSDan Rosenberg 			break;
267851788b1bSDan Rosenberg 
2679bf5fc093SJosef Bacik 		info = NULL;
26801406e432SJosef Bacik 		rcu_read_lock();
2681bf5fc093SJosef Bacik 		list_for_each_entry_rcu(tmp, &root->fs_info->space_info,
2682bf5fc093SJosef Bacik 					list) {
2683bf5fc093SJosef Bacik 			if (tmp->flags == types[i]) {
2684bf5fc093SJosef Bacik 				info = tmp;
26857fde62bfSChris Mason 				break;
2686bf5fc093SJosef Bacik 			}
2687bf5fc093SJosef Bacik 		}
2688bf5fc093SJosef Bacik 		rcu_read_unlock();
26897fde62bfSChris Mason 
2690bf5fc093SJosef Bacik 		if (!info)
2691bf5fc093SJosef Bacik 			continue;
2692bf5fc093SJosef Bacik 		down_read(&info->groups_sem);
2693bf5fc093SJosef Bacik 		for (c = 0; c < BTRFS_NR_RAID_TYPES; c++) {
2694bf5fc093SJosef Bacik 			if (!list_empty(&info->block_groups[c])) {
2695bf5fc093SJosef Bacik 				get_block_group_info(&info->block_groups[c],
2696bf5fc093SJosef Bacik 						     &space);
26977fde62bfSChris Mason 				memcpy(dest, &space, sizeof(space));
26981406e432SJosef Bacik 				dest++;
26991406e432SJosef Bacik 				space_args.total_spaces++;
270051788b1bSDan Rosenberg 				slot_count--;
27011406e432SJosef Bacik 			}
270251788b1bSDan Rosenberg 			if (!slot_count)
270351788b1bSDan Rosenberg 				break;
2704bf5fc093SJosef Bacik 		}
2705bf5fc093SJosef Bacik 		up_read(&info->groups_sem);
2706bf5fc093SJosef Bacik 	}
27071406e432SJosef Bacik 
27087fde62bfSChris Mason 	user_dest = (struct btrfs_ioctl_space_info *)
27097fde62bfSChris Mason 		(arg + sizeof(struct btrfs_ioctl_space_args));
27107fde62bfSChris Mason 
27117fde62bfSChris Mason 	if (copy_to_user(user_dest, dest_orig, alloc_size))
27127fde62bfSChris Mason 		ret = -EFAULT;
27137fde62bfSChris Mason 
27147fde62bfSChris Mason 	kfree(dest_orig);
27157fde62bfSChris Mason out:
27167fde62bfSChris Mason 	if (ret == 0 && copy_to_user(arg, &space_args, sizeof(space_args)))
27171406e432SJosef Bacik 		ret = -EFAULT;
27181406e432SJosef Bacik 
27191406e432SJosef Bacik 	return ret;
27201406e432SJosef Bacik }
27211406e432SJosef Bacik 
2722f46b5a66SChristoph Hellwig /*
2723f46b5a66SChristoph Hellwig  * there are many ways the trans_start and trans_end ioctls can lead
2724f46b5a66SChristoph Hellwig  * to deadlocks.  They should only be used by applications that
2725f46b5a66SChristoph Hellwig  * basically own the machine, and have a very in depth understanding
2726f46b5a66SChristoph Hellwig  * of all the possible deadlocks and enospc problems.
2727f46b5a66SChristoph Hellwig  */
2728f46b5a66SChristoph Hellwig long btrfs_ioctl_trans_end(struct file *file)
2729f46b5a66SChristoph Hellwig {
2730f46b5a66SChristoph Hellwig 	struct inode *inode = fdentry(file)->d_inode;
2731f46b5a66SChristoph Hellwig 	struct btrfs_root *root = BTRFS_I(inode)->root;
2732f46b5a66SChristoph Hellwig 	struct btrfs_trans_handle *trans;
2733f46b5a66SChristoph Hellwig 
2734f46b5a66SChristoph Hellwig 	trans = file->private_data;
27351ab86aedSSage Weil 	if (!trans)
27361ab86aedSSage Weil 		return -EINVAL;
2737b214107eSChristoph Hellwig 	file->private_data = NULL;
27389ca9ee09SSage Weil 
27391ab86aedSSage Weil 	btrfs_end_transaction(trans, root);
27401ab86aedSSage Weil 
27419ca9ee09SSage Weil 	mutex_lock(&root->fs_info->trans_mutex);
27429ca9ee09SSage Weil 	root->fs_info->open_ioctl_trans--;
27439ca9ee09SSage Weil 	mutex_unlock(&root->fs_info->trans_mutex);
27449ca9ee09SSage Weil 
2745cfc8ea87SSage Weil 	mnt_drop_write(file->f_path.mnt);
27461ab86aedSSage Weil 	return 0;
2747f46b5a66SChristoph Hellwig }
2748f46b5a66SChristoph Hellwig 
274946204592SSage Weil static noinline long btrfs_ioctl_start_sync(struct file *file, void __user *argp)
275046204592SSage Weil {
275146204592SSage Weil 	struct btrfs_root *root = BTRFS_I(file->f_dentry->d_inode)->root;
275246204592SSage Weil 	struct btrfs_trans_handle *trans;
275346204592SSage Weil 	u64 transid;
2754db5b493aSTsutomu Itoh 	int ret;
275546204592SSage Weil 
275646204592SSage Weil 	trans = btrfs_start_transaction(root, 0);
275798d5dc13STsutomu Itoh 	if (IS_ERR(trans))
275898d5dc13STsutomu Itoh 		return PTR_ERR(trans);
275946204592SSage Weil 	transid = trans->transid;
2760db5b493aSTsutomu Itoh 	ret = btrfs_commit_transaction_async(trans, root, 0);
27618b2b2d3cSTsutomu Itoh 	if (ret) {
27628b2b2d3cSTsutomu Itoh 		btrfs_end_transaction(trans, root);
2763db5b493aSTsutomu Itoh 		return ret;
27648b2b2d3cSTsutomu Itoh 	}
276546204592SSage Weil 
276646204592SSage Weil 	if (argp)
276746204592SSage Weil 		if (copy_to_user(argp, &transid, sizeof(transid)))
276846204592SSage Weil 			return -EFAULT;
276946204592SSage Weil 	return 0;
277046204592SSage Weil }
277146204592SSage Weil 
277246204592SSage Weil static noinline long btrfs_ioctl_wait_sync(struct file *file, void __user *argp)
277346204592SSage Weil {
277446204592SSage Weil 	struct btrfs_root *root = BTRFS_I(file->f_dentry->d_inode)->root;
277546204592SSage Weil 	u64 transid;
277646204592SSage Weil 
277746204592SSage Weil 	if (argp) {
277846204592SSage Weil 		if (copy_from_user(&transid, argp, sizeof(transid)))
277946204592SSage Weil 			return -EFAULT;
278046204592SSage Weil 	} else {
278146204592SSage Weil 		transid = 0;  /* current trans */
278246204592SSage Weil 	}
278346204592SSage Weil 	return btrfs_wait_for_commit(root, transid);
278446204592SSage Weil }
278546204592SSage Weil 
2786475f6387SJan Schmidt static long btrfs_ioctl_scrub(struct btrfs_root *root, void __user *arg)
2787475f6387SJan Schmidt {
2788475f6387SJan Schmidt 	int ret;
2789475f6387SJan Schmidt 	struct btrfs_ioctl_scrub_args *sa;
2790475f6387SJan Schmidt 
2791475f6387SJan Schmidt 	if (!capable(CAP_SYS_ADMIN))
2792475f6387SJan Schmidt 		return -EPERM;
2793475f6387SJan Schmidt 
2794475f6387SJan Schmidt 	sa = memdup_user(arg, sizeof(*sa));
2795475f6387SJan Schmidt 	if (IS_ERR(sa))
2796475f6387SJan Schmidt 		return PTR_ERR(sa);
2797475f6387SJan Schmidt 
2798475f6387SJan Schmidt 	ret = btrfs_scrub_dev(root, sa->devid, sa->start, sa->end,
27998628764eSArne Jansen 			      &sa->progress, sa->flags & BTRFS_SCRUB_READONLY);
2800475f6387SJan Schmidt 
2801475f6387SJan Schmidt 	if (copy_to_user(arg, sa, sizeof(*sa)))
2802475f6387SJan Schmidt 		ret = -EFAULT;
2803475f6387SJan Schmidt 
2804475f6387SJan Schmidt 	kfree(sa);
2805475f6387SJan Schmidt 	return ret;
2806475f6387SJan Schmidt }
2807475f6387SJan Schmidt 
2808475f6387SJan Schmidt static long btrfs_ioctl_scrub_cancel(struct btrfs_root *root, void __user *arg)
2809475f6387SJan Schmidt {
2810475f6387SJan Schmidt 	if (!capable(CAP_SYS_ADMIN))
2811475f6387SJan Schmidt 		return -EPERM;
2812475f6387SJan Schmidt 
2813475f6387SJan Schmidt 	return btrfs_scrub_cancel(root);
2814475f6387SJan Schmidt }
2815475f6387SJan Schmidt 
2816475f6387SJan Schmidt static long btrfs_ioctl_scrub_progress(struct btrfs_root *root,
2817475f6387SJan Schmidt 				       void __user *arg)
2818475f6387SJan Schmidt {
2819475f6387SJan Schmidt 	struct btrfs_ioctl_scrub_args *sa;
2820475f6387SJan Schmidt 	int ret;
2821475f6387SJan Schmidt 
2822475f6387SJan Schmidt 	if (!capable(CAP_SYS_ADMIN))
2823475f6387SJan Schmidt 		return -EPERM;
2824475f6387SJan Schmidt 
2825475f6387SJan Schmidt 	sa = memdup_user(arg, sizeof(*sa));
2826475f6387SJan Schmidt 	if (IS_ERR(sa))
2827475f6387SJan Schmidt 		return PTR_ERR(sa);
2828475f6387SJan Schmidt 
2829475f6387SJan Schmidt 	ret = btrfs_scrub_progress(root, sa->devid, &sa->progress);
2830475f6387SJan Schmidt 
2831475f6387SJan Schmidt 	if (copy_to_user(arg, sa, sizeof(*sa)))
2832475f6387SJan Schmidt 		ret = -EFAULT;
2833475f6387SJan Schmidt 
2834475f6387SJan Schmidt 	kfree(sa);
2835475f6387SJan Schmidt 	return ret;
2836475f6387SJan Schmidt }
2837475f6387SJan Schmidt 
2838f46b5a66SChristoph Hellwig long btrfs_ioctl(struct file *file, unsigned int
2839f46b5a66SChristoph Hellwig 		cmd, unsigned long arg)
2840f46b5a66SChristoph Hellwig {
2841f46b5a66SChristoph Hellwig 	struct btrfs_root *root = BTRFS_I(fdentry(file)->d_inode)->root;
28424bcabaa3SChristoph Hellwig 	void __user *argp = (void __user *)arg;
2843f46b5a66SChristoph Hellwig 
2844f46b5a66SChristoph Hellwig 	switch (cmd) {
28456cbff00fSChristoph Hellwig 	case FS_IOC_GETFLAGS:
28466cbff00fSChristoph Hellwig 		return btrfs_ioctl_getflags(file, argp);
28476cbff00fSChristoph Hellwig 	case FS_IOC_SETFLAGS:
28486cbff00fSChristoph Hellwig 		return btrfs_ioctl_setflags(file, argp);
28496cbff00fSChristoph Hellwig 	case FS_IOC_GETVERSION:
28506cbff00fSChristoph Hellwig 		return btrfs_ioctl_getversion(file, argp);
2851f7039b1dSLi Dongyang 	case FITRIM:
2852f7039b1dSLi Dongyang 		return btrfs_ioctl_fitrim(file, argp);
2853f46b5a66SChristoph Hellwig 	case BTRFS_IOC_SNAP_CREATE:
2854fa0d2b9bSLi Zefan 		return btrfs_ioctl_snap_create(file, argp, 0);
2855fdfb1e4fSLi Zefan 	case BTRFS_IOC_SNAP_CREATE_V2:
2856fa0d2b9bSLi Zefan 		return btrfs_ioctl_snap_create_v2(file, argp, 0);
28573de4586cSChris Mason 	case BTRFS_IOC_SUBVOL_CREATE:
2858fa0d2b9bSLi Zefan 		return btrfs_ioctl_snap_create(file, argp, 1);
285976dda93cSYan, Zheng 	case BTRFS_IOC_SNAP_DESTROY:
286076dda93cSYan, Zheng 		return btrfs_ioctl_snap_destroy(file, argp);
28610caa102dSLi Zefan 	case BTRFS_IOC_SUBVOL_GETFLAGS:
28620caa102dSLi Zefan 		return btrfs_ioctl_subvol_getflags(file, argp);
28630caa102dSLi Zefan 	case BTRFS_IOC_SUBVOL_SETFLAGS:
28640caa102dSLi Zefan 		return btrfs_ioctl_subvol_setflags(file, argp);
28656ef5ed0dSJosef Bacik 	case BTRFS_IOC_DEFAULT_SUBVOL:
28666ef5ed0dSJosef Bacik 		return btrfs_ioctl_default_subvol(file, argp);
2867f46b5a66SChristoph Hellwig 	case BTRFS_IOC_DEFRAG:
28681e701a32SChris Mason 		return btrfs_ioctl_defrag(file, NULL);
28691e701a32SChris Mason 	case BTRFS_IOC_DEFRAG_RANGE:
28701e701a32SChris Mason 		return btrfs_ioctl_defrag(file, argp);
2871f46b5a66SChristoph Hellwig 	case BTRFS_IOC_RESIZE:
28724bcabaa3SChristoph Hellwig 		return btrfs_ioctl_resize(root, argp);
2873f46b5a66SChristoph Hellwig 	case BTRFS_IOC_ADD_DEV:
28744bcabaa3SChristoph Hellwig 		return btrfs_ioctl_add_dev(root, argp);
2875f46b5a66SChristoph Hellwig 	case BTRFS_IOC_RM_DEV:
28764bcabaa3SChristoph Hellwig 		return btrfs_ioctl_rm_dev(root, argp);
2877475f6387SJan Schmidt 	case BTRFS_IOC_FS_INFO:
2878475f6387SJan Schmidt 		return btrfs_ioctl_fs_info(root, argp);
2879475f6387SJan Schmidt 	case BTRFS_IOC_DEV_INFO:
2880475f6387SJan Schmidt 		return btrfs_ioctl_dev_info(root, argp);
2881f46b5a66SChristoph Hellwig 	case BTRFS_IOC_BALANCE:
2882f46b5a66SChristoph Hellwig 		return btrfs_balance(root->fs_info->dev_root);
2883f46b5a66SChristoph Hellwig 	case BTRFS_IOC_CLONE:
2884c5c9cd4dSSage Weil 		return btrfs_ioctl_clone(file, arg, 0, 0, 0);
2885c5c9cd4dSSage Weil 	case BTRFS_IOC_CLONE_RANGE:
28867a865e8aSChristoph Hellwig 		return btrfs_ioctl_clone_range(file, argp);
2887f46b5a66SChristoph Hellwig 	case BTRFS_IOC_TRANS_START:
2888f46b5a66SChristoph Hellwig 		return btrfs_ioctl_trans_start(file);
2889f46b5a66SChristoph Hellwig 	case BTRFS_IOC_TRANS_END:
2890f46b5a66SChristoph Hellwig 		return btrfs_ioctl_trans_end(file);
2891ac8e9819SChris Mason 	case BTRFS_IOC_TREE_SEARCH:
2892ac8e9819SChris Mason 		return btrfs_ioctl_tree_search(file, argp);
2893ac8e9819SChris Mason 	case BTRFS_IOC_INO_LOOKUP:
2894ac8e9819SChris Mason 		return btrfs_ioctl_ino_lookup(file, argp);
28951406e432SJosef Bacik 	case BTRFS_IOC_SPACE_INFO:
28961406e432SJosef Bacik 		return btrfs_ioctl_space_info(root, argp);
2897f46b5a66SChristoph Hellwig 	case BTRFS_IOC_SYNC:
2898f46b5a66SChristoph Hellwig 		btrfs_sync_fs(file->f_dentry->d_sb, 1);
2899f46b5a66SChristoph Hellwig 		return 0;
290046204592SSage Weil 	case BTRFS_IOC_START_SYNC:
290146204592SSage Weil 		return btrfs_ioctl_start_sync(file, argp);
290246204592SSage Weil 	case BTRFS_IOC_WAIT_SYNC:
290346204592SSage Weil 		return btrfs_ioctl_wait_sync(file, argp);
2904475f6387SJan Schmidt 	case BTRFS_IOC_SCRUB:
2905475f6387SJan Schmidt 		return btrfs_ioctl_scrub(root, argp);
2906475f6387SJan Schmidt 	case BTRFS_IOC_SCRUB_CANCEL:
2907475f6387SJan Schmidt 		return btrfs_ioctl_scrub_cancel(root, argp);
2908475f6387SJan Schmidt 	case BTRFS_IOC_SCRUB_PROGRESS:
2909475f6387SJan Schmidt 		return btrfs_ioctl_scrub_progress(root, argp);
2910f46b5a66SChristoph Hellwig 	}
2911f46b5a66SChristoph Hellwig 
2912f46b5a66SChristoph Hellwig 	return -ENOTTY;
2913f46b5a66SChristoph Hellwig }
2914