xref: /openbmc/linux/fs/btrfs/ioctl.c (revision ecd18815)
1f46b5a66SChristoph Hellwig /*
2f46b5a66SChristoph Hellwig  * Copyright (C) 2007 Oracle.  All rights reserved.
3f46b5a66SChristoph Hellwig  *
4f46b5a66SChristoph Hellwig  * This program is free software; you can redistribute it and/or
5f46b5a66SChristoph Hellwig  * modify it under the terms of the GNU General Public
6f46b5a66SChristoph Hellwig  * License v2 as published by the Free Software Foundation.
7f46b5a66SChristoph Hellwig  *
8f46b5a66SChristoph Hellwig  * This program is distributed in the hope that it will be useful,
9f46b5a66SChristoph Hellwig  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10f46b5a66SChristoph Hellwig  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11f46b5a66SChristoph Hellwig  * General Public License for more details.
12f46b5a66SChristoph Hellwig  *
13f46b5a66SChristoph Hellwig  * You should have received a copy of the GNU General Public
14f46b5a66SChristoph Hellwig  * License along with this program; if not, write to the
15f46b5a66SChristoph Hellwig  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
16f46b5a66SChristoph Hellwig  * Boston, MA 021110-1307, USA.
17f46b5a66SChristoph Hellwig  */
18f46b5a66SChristoph Hellwig 
19f46b5a66SChristoph Hellwig #include <linux/kernel.h>
20f46b5a66SChristoph Hellwig #include <linux/bio.h>
21f46b5a66SChristoph Hellwig #include <linux/buffer_head.h>
22f46b5a66SChristoph Hellwig #include <linux/file.h>
23f46b5a66SChristoph Hellwig #include <linux/fs.h>
24cb8e7090SChristoph Hellwig #include <linux/fsnotify.h>
25f46b5a66SChristoph Hellwig #include <linux/pagemap.h>
26f46b5a66SChristoph Hellwig #include <linux/highmem.h>
27f46b5a66SChristoph Hellwig #include <linux/time.h>
28f46b5a66SChristoph Hellwig #include <linux/init.h>
29f46b5a66SChristoph Hellwig #include <linux/string.h>
30f46b5a66SChristoph Hellwig #include <linux/backing-dev.h>
31cb8e7090SChristoph Hellwig #include <linux/mount.h>
32f46b5a66SChristoph Hellwig #include <linux/mpage.h>
33cb8e7090SChristoph Hellwig #include <linux/namei.h>
34f46b5a66SChristoph Hellwig #include <linux/swap.h>
35f46b5a66SChristoph Hellwig #include <linux/writeback.h>
36f46b5a66SChristoph Hellwig #include <linux/statfs.h>
37f46b5a66SChristoph Hellwig #include <linux/compat.h>
38f46b5a66SChristoph Hellwig #include <linux/bit_spinlock.h>
39cb8e7090SChristoph Hellwig #include <linux/security.h>
40f46b5a66SChristoph Hellwig #include <linux/xattr.h>
417ea394f1SYan Zheng #include <linux/vmalloc.h>
425a0e3ad6STejun Heo #include <linux/slab.h>
43f7039b1dSLi Dongyang #include <linux/blkdev.h>
448ea05e3aSAlexander Block #include <linux/uuid.h>
454b4e25f2SChris Mason #include "compat.h"
46f46b5a66SChristoph Hellwig #include "ctree.h"
47f46b5a66SChristoph Hellwig #include "disk-io.h"
48f46b5a66SChristoph Hellwig #include "transaction.h"
49f46b5a66SChristoph Hellwig #include "btrfs_inode.h"
50f46b5a66SChristoph Hellwig #include "ioctl.h"
51f46b5a66SChristoph Hellwig #include "print-tree.h"
52f46b5a66SChristoph Hellwig #include "volumes.h"
53925baeddSChris Mason #include "locking.h"
54581bb050SLi Zefan #include "inode-map.h"
55d7728c96SJan Schmidt #include "backref.h"
56606686eeSJosef Bacik #include "rcu-string.h"
5731db9f7cSAlexander Block #include "send.h"
58f46b5a66SChristoph Hellwig 
596cbff00fSChristoph Hellwig /* Mask out flags that are inappropriate for the given type of inode. */
606cbff00fSChristoph Hellwig static inline __u32 btrfs_mask_flags(umode_t mode, __u32 flags)
616cbff00fSChristoph Hellwig {
626cbff00fSChristoph Hellwig 	if (S_ISDIR(mode))
636cbff00fSChristoph Hellwig 		return flags;
646cbff00fSChristoph Hellwig 	else if (S_ISREG(mode))
656cbff00fSChristoph Hellwig 		return flags & ~FS_DIRSYNC_FL;
666cbff00fSChristoph Hellwig 	else
676cbff00fSChristoph Hellwig 		return flags & (FS_NODUMP_FL | FS_NOATIME_FL);
686cbff00fSChristoph Hellwig }
69f46b5a66SChristoph Hellwig 
706cbff00fSChristoph Hellwig /*
716cbff00fSChristoph Hellwig  * Export inode flags to the format expected by the FS_IOC_GETFLAGS ioctl.
726cbff00fSChristoph Hellwig  */
736cbff00fSChristoph Hellwig static unsigned int btrfs_flags_to_ioctl(unsigned int flags)
746cbff00fSChristoph Hellwig {
756cbff00fSChristoph Hellwig 	unsigned int iflags = 0;
766cbff00fSChristoph Hellwig 
776cbff00fSChristoph Hellwig 	if (flags & BTRFS_INODE_SYNC)
786cbff00fSChristoph Hellwig 		iflags |= FS_SYNC_FL;
796cbff00fSChristoph Hellwig 	if (flags & BTRFS_INODE_IMMUTABLE)
806cbff00fSChristoph Hellwig 		iflags |= FS_IMMUTABLE_FL;
816cbff00fSChristoph Hellwig 	if (flags & BTRFS_INODE_APPEND)
826cbff00fSChristoph Hellwig 		iflags |= FS_APPEND_FL;
836cbff00fSChristoph Hellwig 	if (flags & BTRFS_INODE_NODUMP)
846cbff00fSChristoph Hellwig 		iflags |= FS_NODUMP_FL;
856cbff00fSChristoph Hellwig 	if (flags & BTRFS_INODE_NOATIME)
866cbff00fSChristoph Hellwig 		iflags |= FS_NOATIME_FL;
876cbff00fSChristoph Hellwig 	if (flags & BTRFS_INODE_DIRSYNC)
886cbff00fSChristoph Hellwig 		iflags |= FS_DIRSYNC_FL;
89d0092bddSLi Zefan 	if (flags & BTRFS_INODE_NODATACOW)
90d0092bddSLi Zefan 		iflags |= FS_NOCOW_FL;
91d0092bddSLi Zefan 
92d0092bddSLi Zefan 	if ((flags & BTRFS_INODE_COMPRESS) && !(flags & BTRFS_INODE_NOCOMPRESS))
93d0092bddSLi Zefan 		iflags |= FS_COMPR_FL;
94d0092bddSLi Zefan 	else if (flags & BTRFS_INODE_NOCOMPRESS)
95d0092bddSLi Zefan 		iflags |= FS_NOCOMP_FL;
966cbff00fSChristoph Hellwig 
976cbff00fSChristoph Hellwig 	return iflags;
986cbff00fSChristoph Hellwig }
996cbff00fSChristoph Hellwig 
1006cbff00fSChristoph Hellwig /*
1016cbff00fSChristoph Hellwig  * Update inode->i_flags based on the btrfs internal flags.
1026cbff00fSChristoph Hellwig  */
1036cbff00fSChristoph Hellwig void btrfs_update_iflags(struct inode *inode)
1046cbff00fSChristoph Hellwig {
1056cbff00fSChristoph Hellwig 	struct btrfs_inode *ip = BTRFS_I(inode);
1066cbff00fSChristoph Hellwig 
1076cbff00fSChristoph Hellwig 	inode->i_flags &= ~(S_SYNC|S_APPEND|S_IMMUTABLE|S_NOATIME|S_DIRSYNC);
1086cbff00fSChristoph Hellwig 
1096cbff00fSChristoph Hellwig 	if (ip->flags & BTRFS_INODE_SYNC)
1106cbff00fSChristoph Hellwig 		inode->i_flags |= S_SYNC;
1116cbff00fSChristoph Hellwig 	if (ip->flags & BTRFS_INODE_IMMUTABLE)
1126cbff00fSChristoph Hellwig 		inode->i_flags |= S_IMMUTABLE;
1136cbff00fSChristoph Hellwig 	if (ip->flags & BTRFS_INODE_APPEND)
1146cbff00fSChristoph Hellwig 		inode->i_flags |= S_APPEND;
1156cbff00fSChristoph Hellwig 	if (ip->flags & BTRFS_INODE_NOATIME)
1166cbff00fSChristoph Hellwig 		inode->i_flags |= S_NOATIME;
1176cbff00fSChristoph Hellwig 	if (ip->flags & BTRFS_INODE_DIRSYNC)
1186cbff00fSChristoph Hellwig 		inode->i_flags |= S_DIRSYNC;
1196cbff00fSChristoph Hellwig }
1206cbff00fSChristoph Hellwig 
1216cbff00fSChristoph Hellwig /*
1226cbff00fSChristoph Hellwig  * Inherit flags from the parent inode.
1236cbff00fSChristoph Hellwig  *
124e27425d6SJosef Bacik  * Currently only the compression flags and the cow flags are inherited.
1256cbff00fSChristoph Hellwig  */
1266cbff00fSChristoph Hellwig void btrfs_inherit_iflags(struct inode *inode, struct inode *dir)
1276cbff00fSChristoph Hellwig {
1280b4dcea5SChris Mason 	unsigned int flags;
1290b4dcea5SChris Mason 
1300b4dcea5SChris Mason 	if (!dir)
1310b4dcea5SChris Mason 		return;
1320b4dcea5SChris Mason 
1330b4dcea5SChris Mason 	flags = BTRFS_I(dir)->flags;
1346cbff00fSChristoph Hellwig 
135e27425d6SJosef Bacik 	if (flags & BTRFS_INODE_NOCOMPRESS) {
136e27425d6SJosef Bacik 		BTRFS_I(inode)->flags &= ~BTRFS_INODE_COMPRESS;
137e27425d6SJosef Bacik 		BTRFS_I(inode)->flags |= BTRFS_INODE_NOCOMPRESS;
138e27425d6SJosef Bacik 	} else if (flags & BTRFS_INODE_COMPRESS) {
139e27425d6SJosef Bacik 		BTRFS_I(inode)->flags &= ~BTRFS_INODE_NOCOMPRESS;
140e27425d6SJosef Bacik 		BTRFS_I(inode)->flags |= BTRFS_INODE_COMPRESS;
141e27425d6SJosef Bacik 	}
1426cbff00fSChristoph Hellwig 
143e27425d6SJosef Bacik 	if (flags & BTRFS_INODE_NODATACOW)
144e27425d6SJosef Bacik 		BTRFS_I(inode)->flags |= BTRFS_INODE_NODATACOW;
145e27425d6SJosef Bacik 
1466cbff00fSChristoph Hellwig 	btrfs_update_iflags(inode);
1476cbff00fSChristoph Hellwig }
1486cbff00fSChristoph Hellwig 
1496cbff00fSChristoph Hellwig static int btrfs_ioctl_getflags(struct file *file, void __user *arg)
1506cbff00fSChristoph Hellwig {
1516cbff00fSChristoph Hellwig 	struct btrfs_inode *ip = BTRFS_I(file->f_path.dentry->d_inode);
1526cbff00fSChristoph Hellwig 	unsigned int flags = btrfs_flags_to_ioctl(ip->flags);
1536cbff00fSChristoph Hellwig 
1546cbff00fSChristoph Hellwig 	if (copy_to_user(arg, &flags, sizeof(flags)))
1556cbff00fSChristoph Hellwig 		return -EFAULT;
1566cbff00fSChristoph Hellwig 	return 0;
1576cbff00fSChristoph Hellwig }
1586cbff00fSChristoph Hellwig 
15975e7cb7fSLiu Bo static int check_flags(unsigned int flags)
16075e7cb7fSLiu Bo {
16175e7cb7fSLiu Bo 	if (flags & ~(FS_IMMUTABLE_FL | FS_APPEND_FL | \
16275e7cb7fSLiu Bo 		      FS_NOATIME_FL | FS_NODUMP_FL | \
16375e7cb7fSLiu Bo 		      FS_SYNC_FL | FS_DIRSYNC_FL | \
164e1e8fb6aSLi Zefan 		      FS_NOCOMP_FL | FS_COMPR_FL |
165e1e8fb6aSLi Zefan 		      FS_NOCOW_FL))
16675e7cb7fSLiu Bo 		return -EOPNOTSUPP;
16775e7cb7fSLiu Bo 
16875e7cb7fSLiu Bo 	if ((flags & FS_NOCOMP_FL) && (flags & FS_COMPR_FL))
16975e7cb7fSLiu Bo 		return -EINVAL;
17075e7cb7fSLiu Bo 
17175e7cb7fSLiu Bo 	return 0;
17275e7cb7fSLiu Bo }
17375e7cb7fSLiu Bo 
1746cbff00fSChristoph Hellwig static int btrfs_ioctl_setflags(struct file *file, void __user *arg)
1756cbff00fSChristoph Hellwig {
1766cbff00fSChristoph Hellwig 	struct inode *inode = file->f_path.dentry->d_inode;
1776cbff00fSChristoph Hellwig 	struct btrfs_inode *ip = BTRFS_I(inode);
1786cbff00fSChristoph Hellwig 	struct btrfs_root *root = ip->root;
1796cbff00fSChristoph Hellwig 	struct btrfs_trans_handle *trans;
1806cbff00fSChristoph Hellwig 	unsigned int flags, oldflags;
1816cbff00fSChristoph Hellwig 	int ret;
182f062abf0SLi Zefan 	u64 ip_oldflags;
183f062abf0SLi Zefan 	unsigned int i_oldflags;
1846cbff00fSChristoph Hellwig 
185b83cc969SLi Zefan 	if (btrfs_root_readonly(root))
186b83cc969SLi Zefan 		return -EROFS;
187b83cc969SLi Zefan 
1886cbff00fSChristoph Hellwig 	if (copy_from_user(&flags, arg, sizeof(flags)))
1896cbff00fSChristoph Hellwig 		return -EFAULT;
1906cbff00fSChristoph Hellwig 
19175e7cb7fSLiu Bo 	ret = check_flags(flags);
19275e7cb7fSLiu Bo 	if (ret)
19375e7cb7fSLiu Bo 		return ret;
1946cbff00fSChristoph Hellwig 
1952e149670SSerge E. Hallyn 	if (!inode_owner_or_capable(inode))
1966cbff00fSChristoph Hellwig 		return -EACCES;
1976cbff00fSChristoph Hellwig 
198e7848683SJan Kara 	ret = mnt_want_write_file(file);
199e7848683SJan Kara 	if (ret)
200e7848683SJan Kara 		return ret;
201e7848683SJan Kara 
2026cbff00fSChristoph Hellwig 	mutex_lock(&inode->i_mutex);
2036cbff00fSChristoph Hellwig 
204f062abf0SLi Zefan 	ip_oldflags = ip->flags;
205f062abf0SLi Zefan 	i_oldflags = inode->i_flags;
206f062abf0SLi Zefan 
2076cbff00fSChristoph Hellwig 	flags = btrfs_mask_flags(inode->i_mode, flags);
2086cbff00fSChristoph Hellwig 	oldflags = btrfs_flags_to_ioctl(ip->flags);
2096cbff00fSChristoph Hellwig 	if ((flags ^ oldflags) & (FS_APPEND_FL | FS_IMMUTABLE_FL)) {
2106cbff00fSChristoph Hellwig 		if (!capable(CAP_LINUX_IMMUTABLE)) {
2116cbff00fSChristoph Hellwig 			ret = -EPERM;
2126cbff00fSChristoph Hellwig 			goto out_unlock;
2136cbff00fSChristoph Hellwig 		}
2146cbff00fSChristoph Hellwig 	}
2156cbff00fSChristoph Hellwig 
2166cbff00fSChristoph Hellwig 	if (flags & FS_SYNC_FL)
2176cbff00fSChristoph Hellwig 		ip->flags |= BTRFS_INODE_SYNC;
2186cbff00fSChristoph Hellwig 	else
2196cbff00fSChristoph Hellwig 		ip->flags &= ~BTRFS_INODE_SYNC;
2206cbff00fSChristoph Hellwig 	if (flags & FS_IMMUTABLE_FL)
2216cbff00fSChristoph Hellwig 		ip->flags |= BTRFS_INODE_IMMUTABLE;
2226cbff00fSChristoph Hellwig 	else
2236cbff00fSChristoph Hellwig 		ip->flags &= ~BTRFS_INODE_IMMUTABLE;
2246cbff00fSChristoph Hellwig 	if (flags & FS_APPEND_FL)
2256cbff00fSChristoph Hellwig 		ip->flags |= BTRFS_INODE_APPEND;
2266cbff00fSChristoph Hellwig 	else
2276cbff00fSChristoph Hellwig 		ip->flags &= ~BTRFS_INODE_APPEND;
2286cbff00fSChristoph Hellwig 	if (flags & FS_NODUMP_FL)
2296cbff00fSChristoph Hellwig 		ip->flags |= BTRFS_INODE_NODUMP;
2306cbff00fSChristoph Hellwig 	else
2316cbff00fSChristoph Hellwig 		ip->flags &= ~BTRFS_INODE_NODUMP;
2326cbff00fSChristoph Hellwig 	if (flags & FS_NOATIME_FL)
2336cbff00fSChristoph Hellwig 		ip->flags |= BTRFS_INODE_NOATIME;
2346cbff00fSChristoph Hellwig 	else
2356cbff00fSChristoph Hellwig 		ip->flags &= ~BTRFS_INODE_NOATIME;
2366cbff00fSChristoph Hellwig 	if (flags & FS_DIRSYNC_FL)
2376cbff00fSChristoph Hellwig 		ip->flags |= BTRFS_INODE_DIRSYNC;
2386cbff00fSChristoph Hellwig 	else
2396cbff00fSChristoph Hellwig 		ip->flags &= ~BTRFS_INODE_DIRSYNC;
240e1e8fb6aSLi Zefan 	if (flags & FS_NOCOW_FL)
241e1e8fb6aSLi Zefan 		ip->flags |= BTRFS_INODE_NODATACOW;
242e1e8fb6aSLi Zefan 	else
243e1e8fb6aSLi Zefan 		ip->flags &= ~BTRFS_INODE_NODATACOW;
2446cbff00fSChristoph Hellwig 
24575e7cb7fSLiu Bo 	/*
24675e7cb7fSLiu Bo 	 * The COMPRESS flag can only be changed by users, while the NOCOMPRESS
24775e7cb7fSLiu Bo 	 * flag may be changed automatically if compression code won't make
24875e7cb7fSLiu Bo 	 * things smaller.
24975e7cb7fSLiu Bo 	 */
25075e7cb7fSLiu Bo 	if (flags & FS_NOCOMP_FL) {
25175e7cb7fSLiu Bo 		ip->flags &= ~BTRFS_INODE_COMPRESS;
25275e7cb7fSLiu Bo 		ip->flags |= BTRFS_INODE_NOCOMPRESS;
25375e7cb7fSLiu Bo 	} else if (flags & FS_COMPR_FL) {
25475e7cb7fSLiu Bo 		ip->flags |= BTRFS_INODE_COMPRESS;
25575e7cb7fSLiu Bo 		ip->flags &= ~BTRFS_INODE_NOCOMPRESS;
256ebcb904dSLi Zefan 	} else {
257ebcb904dSLi Zefan 		ip->flags &= ~(BTRFS_INODE_COMPRESS | BTRFS_INODE_NOCOMPRESS);
25875e7cb7fSLiu Bo 	}
2596cbff00fSChristoph Hellwig 
2604da6f1a3SLi Zefan 	trans = btrfs_start_transaction(root, 1);
261f062abf0SLi Zefan 	if (IS_ERR(trans)) {
262f062abf0SLi Zefan 		ret = PTR_ERR(trans);
263f062abf0SLi Zefan 		goto out_drop;
264f062abf0SLi Zefan 	}
2656cbff00fSChristoph Hellwig 
266306424ccSLi Zefan 	btrfs_update_iflags(inode);
2670c4d2d95SJosef Bacik 	inode_inc_iversion(inode);
268306424ccSLi Zefan 	inode->i_ctime = CURRENT_TIME;
2696cbff00fSChristoph Hellwig 	ret = btrfs_update_inode(trans, root, inode);
2706cbff00fSChristoph Hellwig 
2716cbff00fSChristoph Hellwig 	btrfs_end_transaction(trans, root);
272f062abf0SLi Zefan  out_drop:
273f062abf0SLi Zefan 	if (ret) {
274f062abf0SLi Zefan 		ip->flags = ip_oldflags;
275f062abf0SLi Zefan 		inode->i_flags = i_oldflags;
276f062abf0SLi Zefan 	}
2776cbff00fSChristoph Hellwig 
2786cbff00fSChristoph Hellwig  out_unlock:
2796cbff00fSChristoph Hellwig 	mutex_unlock(&inode->i_mutex);
280e7848683SJan Kara 	mnt_drop_write_file(file);
2812d4e6f6aSliubo 	return ret;
2826cbff00fSChristoph Hellwig }
2836cbff00fSChristoph Hellwig 
2846cbff00fSChristoph Hellwig static int btrfs_ioctl_getversion(struct file *file, int __user *arg)
2856cbff00fSChristoph Hellwig {
2866cbff00fSChristoph Hellwig 	struct inode *inode = file->f_path.dentry->d_inode;
2876cbff00fSChristoph Hellwig 
2886cbff00fSChristoph Hellwig 	return put_user(inode->i_generation, arg);
2896cbff00fSChristoph Hellwig }
290f46b5a66SChristoph Hellwig 
291f7039b1dSLi Dongyang static noinline int btrfs_ioctl_fitrim(struct file *file, void __user *arg)
292f7039b1dSLi Dongyang {
293815745cfSAl Viro 	struct btrfs_fs_info *fs_info = btrfs_sb(fdentry(file)->d_sb);
294f7039b1dSLi Dongyang 	struct btrfs_device *device;
295f7039b1dSLi Dongyang 	struct request_queue *q;
296f7039b1dSLi Dongyang 	struct fstrim_range range;
297f7039b1dSLi Dongyang 	u64 minlen = ULLONG_MAX;
298f7039b1dSLi Dongyang 	u64 num_devices = 0;
299815745cfSAl Viro 	u64 total_bytes = btrfs_super_total_bytes(fs_info->super_copy);
300f7039b1dSLi Dongyang 	int ret;
301f7039b1dSLi Dongyang 
302f7039b1dSLi Dongyang 	if (!capable(CAP_SYS_ADMIN))
303f7039b1dSLi Dongyang 		return -EPERM;
304f7039b1dSLi Dongyang 
3051f78160cSXiao Guangrong 	rcu_read_lock();
3061f78160cSXiao Guangrong 	list_for_each_entry_rcu(device, &fs_info->fs_devices->devices,
3071f78160cSXiao Guangrong 				dev_list) {
308f7039b1dSLi Dongyang 		if (!device->bdev)
309f7039b1dSLi Dongyang 			continue;
310f7039b1dSLi Dongyang 		q = bdev_get_queue(device->bdev);
311f7039b1dSLi Dongyang 		if (blk_queue_discard(q)) {
312f7039b1dSLi Dongyang 			num_devices++;
313f7039b1dSLi Dongyang 			minlen = min((u64)q->limits.discard_granularity,
314f7039b1dSLi Dongyang 				     minlen);
315f7039b1dSLi Dongyang 		}
316f7039b1dSLi Dongyang 	}
3171f78160cSXiao Guangrong 	rcu_read_unlock();
318f4c697e6SLukas Czerner 
319f7039b1dSLi Dongyang 	if (!num_devices)
320f7039b1dSLi Dongyang 		return -EOPNOTSUPP;
321f7039b1dSLi Dongyang 	if (copy_from_user(&range, arg, sizeof(range)))
322f7039b1dSLi Dongyang 		return -EFAULT;
323f4c697e6SLukas Czerner 	if (range.start > total_bytes)
324f4c697e6SLukas Czerner 		return -EINVAL;
325f7039b1dSLi Dongyang 
326f4c697e6SLukas Czerner 	range.len = min(range.len, total_bytes - range.start);
327f7039b1dSLi Dongyang 	range.minlen = max(range.minlen, minlen);
328815745cfSAl Viro 	ret = btrfs_trim_fs(fs_info->tree_root, &range);
329f7039b1dSLi Dongyang 	if (ret < 0)
330f7039b1dSLi Dongyang 		return ret;
331f7039b1dSLi Dongyang 
332f7039b1dSLi Dongyang 	if (copy_to_user(arg, &range, sizeof(range)))
333f7039b1dSLi Dongyang 		return -EFAULT;
334f7039b1dSLi Dongyang 
335f7039b1dSLi Dongyang 	return 0;
336f7039b1dSLi Dongyang }
337f7039b1dSLi Dongyang 
338cb8e7090SChristoph Hellwig static noinline int create_subvol(struct btrfs_root *root,
339cb8e7090SChristoph Hellwig 				  struct dentry *dentry,
34072fd032eSSage Weil 				  char *name, int namelen,
3416f72c7e2SArne Jansen 				  u64 *async_transid,
3426f72c7e2SArne Jansen 				  struct btrfs_qgroup_inherit **inherit)
343f46b5a66SChristoph Hellwig {
344f46b5a66SChristoph Hellwig 	struct btrfs_trans_handle *trans;
345f46b5a66SChristoph Hellwig 	struct btrfs_key key;
346f46b5a66SChristoph Hellwig 	struct btrfs_root_item root_item;
347f46b5a66SChristoph Hellwig 	struct btrfs_inode_item *inode_item;
348f46b5a66SChristoph Hellwig 	struct extent_buffer *leaf;
34976dda93cSYan, Zheng 	struct btrfs_root *new_root;
3502fbe8c8aSAl Viro 	struct dentry *parent = dentry->d_parent;
3516a912213SJosef Bacik 	struct inode *dir;
3528ea05e3aSAlexander Block 	struct timespec cur_time = CURRENT_TIME;
353f46b5a66SChristoph Hellwig 	int ret;
354f46b5a66SChristoph Hellwig 	int err;
355f46b5a66SChristoph Hellwig 	u64 objectid;
356f46b5a66SChristoph Hellwig 	u64 new_dirid = BTRFS_FIRST_FREE_OBJECTID;
3573de4586cSChris Mason 	u64 index = 0;
3588ea05e3aSAlexander Block 	uuid_le new_uuid;
359f46b5a66SChristoph Hellwig 
360581bb050SLi Zefan 	ret = btrfs_find_free_objectid(root->fs_info->tree_root, &objectid);
3612fbe8c8aSAl Viro 	if (ret)
362a22285a6SYan, Zheng 		return ret;
3636a912213SJosef Bacik 
3646a912213SJosef Bacik 	dir = parent->d_inode;
3656a912213SJosef Bacik 
3669ed74f2dSJosef Bacik 	/*
3679ed74f2dSJosef Bacik 	 * 1 - inode item
3689ed74f2dSJosef Bacik 	 * 2 - refs
3699ed74f2dSJosef Bacik 	 * 1 - root item
3709ed74f2dSJosef Bacik 	 * 2 - dir items
3719ed74f2dSJosef Bacik 	 */
372a22285a6SYan, Zheng 	trans = btrfs_start_transaction(root, 6);
3732fbe8c8aSAl Viro 	if (IS_ERR(trans))
374a22285a6SYan, Zheng 		return PTR_ERR(trans);
375f46b5a66SChristoph Hellwig 
3766f72c7e2SArne Jansen 	ret = btrfs_qgroup_inherit(trans, root->fs_info, 0, objectid,
3776f72c7e2SArne Jansen 				   inherit ? *inherit : NULL);
3786f72c7e2SArne Jansen 	if (ret)
3796f72c7e2SArne Jansen 		goto fail;
3806f72c7e2SArne Jansen 
3815d4f98a2SYan Zheng 	leaf = btrfs_alloc_free_block(trans, root, root->leafsize,
3825581a51aSJan Schmidt 				      0, objectid, NULL, 0, 0, 0);
3838e8a1e31SJosef Bacik 	if (IS_ERR(leaf)) {
3848e8a1e31SJosef Bacik 		ret = PTR_ERR(leaf);
3858e8a1e31SJosef Bacik 		goto fail;
3868e8a1e31SJosef Bacik 	}
387f46b5a66SChristoph Hellwig 
3885d4f98a2SYan Zheng 	memset_extent_buffer(leaf, 0, 0, sizeof(struct btrfs_header));
389f46b5a66SChristoph Hellwig 	btrfs_set_header_bytenr(leaf, leaf->start);
390f46b5a66SChristoph Hellwig 	btrfs_set_header_generation(leaf, trans->transid);
3915d4f98a2SYan Zheng 	btrfs_set_header_backref_rev(leaf, BTRFS_MIXED_BACKREF_REV);
392f46b5a66SChristoph Hellwig 	btrfs_set_header_owner(leaf, objectid);
393f46b5a66SChristoph Hellwig 
394f46b5a66SChristoph Hellwig 	write_extent_buffer(leaf, root->fs_info->fsid,
395f46b5a66SChristoph Hellwig 			    (unsigned long)btrfs_header_fsid(leaf),
396f46b5a66SChristoph Hellwig 			    BTRFS_FSID_SIZE);
3975d4f98a2SYan Zheng 	write_extent_buffer(leaf, root->fs_info->chunk_tree_uuid,
3985d4f98a2SYan Zheng 			    (unsigned long)btrfs_header_chunk_tree_uuid(leaf),
3995d4f98a2SYan Zheng 			    BTRFS_UUID_SIZE);
400f46b5a66SChristoph Hellwig 	btrfs_mark_buffer_dirty(leaf);
401f46b5a66SChristoph Hellwig 
4028ea05e3aSAlexander Block 	memset(&root_item, 0, sizeof(root_item));
4038ea05e3aSAlexander Block 
404f46b5a66SChristoph Hellwig 	inode_item = &root_item.inode;
405f46b5a66SChristoph Hellwig 	inode_item->generation = cpu_to_le64(1);
406f46b5a66SChristoph Hellwig 	inode_item->size = cpu_to_le64(3);
407f46b5a66SChristoph Hellwig 	inode_item->nlink = cpu_to_le32(1);
408a76a3cd4SYan Zheng 	inode_item->nbytes = cpu_to_le64(root->leafsize);
409f46b5a66SChristoph Hellwig 	inode_item->mode = cpu_to_le32(S_IFDIR | 0755);
410f46b5a66SChristoph Hellwig 
41108fe4db1SLi Zefan 	root_item.flags = 0;
41208fe4db1SLi Zefan 	root_item.byte_limit = 0;
41308fe4db1SLi Zefan 	inode_item->flags = cpu_to_le64(BTRFS_INODE_ROOT_ITEM_INIT);
41408fe4db1SLi Zefan 
415f46b5a66SChristoph Hellwig 	btrfs_set_root_bytenr(&root_item, leaf->start);
41684234f3aSYan Zheng 	btrfs_set_root_generation(&root_item, trans->transid);
417f46b5a66SChristoph Hellwig 	btrfs_set_root_level(&root_item, 0);
418f46b5a66SChristoph Hellwig 	btrfs_set_root_refs(&root_item, 1);
41986b9f2ecSYan, Zheng 	btrfs_set_root_used(&root_item, leaf->len);
42080ff3856SYan Zheng 	btrfs_set_root_last_snapshot(&root_item, 0);
421f46b5a66SChristoph Hellwig 
4228ea05e3aSAlexander Block 	btrfs_set_root_generation_v2(&root_item,
4238ea05e3aSAlexander Block 			btrfs_root_generation(&root_item));
4248ea05e3aSAlexander Block 	uuid_le_gen(&new_uuid);
4258ea05e3aSAlexander Block 	memcpy(root_item.uuid, new_uuid.b, BTRFS_UUID_SIZE);
4268ea05e3aSAlexander Block 	root_item.otime.sec = cpu_to_le64(cur_time.tv_sec);
427dadd1105SDan Carpenter 	root_item.otime.nsec = cpu_to_le32(cur_time.tv_nsec);
4288ea05e3aSAlexander Block 	root_item.ctime = root_item.otime;
4298ea05e3aSAlexander Block 	btrfs_set_root_ctransid(&root_item, trans->transid);
4308ea05e3aSAlexander Block 	btrfs_set_root_otransid(&root_item, trans->transid);
431f46b5a66SChristoph Hellwig 
432925baeddSChris Mason 	btrfs_tree_unlock(leaf);
433f46b5a66SChristoph Hellwig 	free_extent_buffer(leaf);
434f46b5a66SChristoph Hellwig 	leaf = NULL;
435f46b5a66SChristoph Hellwig 
436f46b5a66SChristoph Hellwig 	btrfs_set_root_dirid(&root_item, new_dirid);
437f46b5a66SChristoph Hellwig 
438f46b5a66SChristoph Hellwig 	key.objectid = objectid;
4395d4f98a2SYan Zheng 	key.offset = 0;
440f46b5a66SChristoph Hellwig 	btrfs_set_key_type(&key, BTRFS_ROOT_ITEM_KEY);
441f46b5a66SChristoph Hellwig 	ret = btrfs_insert_root(trans, root->fs_info->tree_root, &key,
442f46b5a66SChristoph Hellwig 				&root_item);
443f46b5a66SChristoph Hellwig 	if (ret)
444f46b5a66SChristoph Hellwig 		goto fail;
445f46b5a66SChristoph Hellwig 
44676dda93cSYan, Zheng 	key.offset = (u64)-1;
44776dda93cSYan, Zheng 	new_root = btrfs_read_fs_root_no_name(root->fs_info, &key);
44879787eaaSJeff Mahoney 	if (IS_ERR(new_root)) {
44979787eaaSJeff Mahoney 		btrfs_abort_transaction(trans, root, PTR_ERR(new_root));
45079787eaaSJeff Mahoney 		ret = PTR_ERR(new_root);
45179787eaaSJeff Mahoney 		goto fail;
45279787eaaSJeff Mahoney 	}
45376dda93cSYan, Zheng 
45476dda93cSYan, Zheng 	btrfs_record_root_in_trans(trans, new_root);
45576dda93cSYan, Zheng 
456d82a6f1dSJosef Bacik 	ret = btrfs_create_subvol_root(trans, new_root, new_dirid);
457ce598979SMark Fasheh 	if (ret) {
458ce598979SMark Fasheh 		/* We potentially lose an unused inode item here */
45979787eaaSJeff Mahoney 		btrfs_abort_transaction(trans, root, ret);
460ce598979SMark Fasheh 		goto fail;
461ce598979SMark Fasheh 	}
462ce598979SMark Fasheh 
463f46b5a66SChristoph Hellwig 	/*
464f46b5a66SChristoph Hellwig 	 * insert the directory item
465f46b5a66SChristoph Hellwig 	 */
4663de4586cSChris Mason 	ret = btrfs_set_inode_index(dir, &index);
46779787eaaSJeff Mahoney 	if (ret) {
46879787eaaSJeff Mahoney 		btrfs_abort_transaction(trans, root, ret);
46979787eaaSJeff Mahoney 		goto fail;
47079787eaaSJeff Mahoney 	}
4713de4586cSChris Mason 
4723de4586cSChris Mason 	ret = btrfs_insert_dir_item(trans, root,
47316cdcec7SMiao Xie 				    name, namelen, dir, &key,
4743de4586cSChris Mason 				    BTRFS_FT_DIR, index);
47579787eaaSJeff Mahoney 	if (ret) {
47679787eaaSJeff Mahoney 		btrfs_abort_transaction(trans, root, ret);
477f46b5a66SChristoph Hellwig 		goto fail;
47879787eaaSJeff Mahoney 	}
4790660b5afSChris Mason 
48052c26179SYan Zheng 	btrfs_i_size_write(dir, dir->i_size + namelen * 2);
48152c26179SYan Zheng 	ret = btrfs_update_inode(trans, root, dir);
48252c26179SYan Zheng 	BUG_ON(ret);
48352c26179SYan Zheng 
4840660b5afSChris Mason 	ret = btrfs_add_root_ref(trans, root->fs_info->tree_root,
4854df27c4dSYan, Zheng 				 objectid, root->root_key.objectid,
48633345d01SLi Zefan 				 btrfs_ino(dir), index, name, namelen);
48776dda93cSYan, Zheng 
4880660b5afSChris Mason 	BUG_ON(ret);
4890660b5afSChris Mason 
49076dda93cSYan, Zheng 	d_instantiate(dentry, btrfs_lookup_dentry(dir, dentry));
491f46b5a66SChristoph Hellwig fail:
49272fd032eSSage Weil 	if (async_transid) {
49372fd032eSSage Weil 		*async_transid = trans->transid;
49472fd032eSSage Weil 		err = btrfs_commit_transaction_async(trans, root, 1);
49572fd032eSSage Weil 	} else {
49676dda93cSYan, Zheng 		err = btrfs_commit_transaction(trans, root);
49772fd032eSSage Weil 	}
498f46b5a66SChristoph Hellwig 	if (err && !ret)
499f46b5a66SChristoph Hellwig 		ret = err;
500f46b5a66SChristoph Hellwig 	return ret;
501f46b5a66SChristoph Hellwig }
502f46b5a66SChristoph Hellwig 
50372fd032eSSage Weil static int create_snapshot(struct btrfs_root *root, struct dentry *dentry,
504b83cc969SLi Zefan 			   char *name, int namelen, u64 *async_transid,
5056f72c7e2SArne Jansen 			   bool readonly, struct btrfs_qgroup_inherit **inherit)
506f46b5a66SChristoph Hellwig {
5072e4bfab9SYan, Zheng 	struct inode *inode;
508f46b5a66SChristoph Hellwig 	struct btrfs_pending_snapshot *pending_snapshot;
509f46b5a66SChristoph Hellwig 	struct btrfs_trans_handle *trans;
5102e4bfab9SYan, Zheng 	int ret;
511f46b5a66SChristoph Hellwig 
512f46b5a66SChristoph Hellwig 	if (!root->ref_cows)
513f46b5a66SChristoph Hellwig 		return -EINVAL;
514f46b5a66SChristoph Hellwig 
515a22285a6SYan, Zheng 	pending_snapshot = kzalloc(sizeof(*pending_snapshot), GFP_NOFS);
516a22285a6SYan, Zheng 	if (!pending_snapshot)
517a22285a6SYan, Zheng 		return -ENOMEM;
518a22285a6SYan, Zheng 
519a22285a6SYan, Zheng 	btrfs_init_block_rsv(&pending_snapshot->block_rsv);
520a22285a6SYan, Zheng 	pending_snapshot->dentry = dentry;
521a22285a6SYan, Zheng 	pending_snapshot->root = root;
522b83cc969SLi Zefan 	pending_snapshot->readonly = readonly;
5236f72c7e2SArne Jansen 	if (inherit) {
5246f72c7e2SArne Jansen 		pending_snapshot->inherit = *inherit;
5256f72c7e2SArne Jansen 		*inherit = NULL;	/* take responsibility to free it */
5266f72c7e2SArne Jansen 	}
527a22285a6SYan, Zheng 
528a22285a6SYan, Zheng 	trans = btrfs_start_transaction(root->fs_info->extent_root, 5);
529a22285a6SYan, Zheng 	if (IS_ERR(trans)) {
530a22285a6SYan, Zheng 		ret = PTR_ERR(trans);
531a22285a6SYan, Zheng 		goto fail;
532a22285a6SYan, Zheng 	}
533a22285a6SYan, Zheng 
534a22285a6SYan, Zheng 	ret = btrfs_snap_reserve_metadata(trans, pending_snapshot);
535a22285a6SYan, Zheng 	BUG_ON(ret);
536a22285a6SYan, Zheng 
5378351583eSJosef Bacik 	spin_lock(&root->fs_info->trans_lock);
538a22285a6SYan, Zheng 	list_add(&pending_snapshot->list,
539a22285a6SYan, Zheng 		 &trans->transaction->pending_snapshots);
5408351583eSJosef Bacik 	spin_unlock(&root->fs_info->trans_lock);
54172fd032eSSage Weil 	if (async_transid) {
54272fd032eSSage Weil 		*async_transid = trans->transid;
54372fd032eSSage Weil 		ret = btrfs_commit_transaction_async(trans,
54472fd032eSSage Weil 				     root->fs_info->extent_root, 1);
54572fd032eSSage Weil 	} else {
54672fd032eSSage Weil 		ret = btrfs_commit_transaction(trans,
54772fd032eSSage Weil 					       root->fs_info->extent_root);
54872fd032eSSage Weil 	}
549a22285a6SYan, Zheng 	BUG_ON(ret);
550a22285a6SYan, Zheng 
551a22285a6SYan, Zheng 	ret = pending_snapshot->error;
552f46b5a66SChristoph Hellwig 	if (ret)
5532e4bfab9SYan, Zheng 		goto fail;
554f46b5a66SChristoph Hellwig 
55566b4ffd1SJosef Bacik 	ret = btrfs_orphan_cleanup(pending_snapshot->snap);
55666b4ffd1SJosef Bacik 	if (ret)
55766b4ffd1SJosef Bacik 		goto fail;
558f46b5a66SChristoph Hellwig 
5592fbe8c8aSAl Viro 	inode = btrfs_lookup_dentry(dentry->d_parent->d_inode, dentry);
5602e4bfab9SYan, Zheng 	if (IS_ERR(inode)) {
5612e4bfab9SYan, Zheng 		ret = PTR_ERR(inode);
5622e4bfab9SYan, Zheng 		goto fail;
5632e4bfab9SYan, Zheng 	}
5642e4bfab9SYan, Zheng 	BUG_ON(!inode);
5652e4bfab9SYan, Zheng 	d_instantiate(dentry, inode);
5662e4bfab9SYan, Zheng 	ret = 0;
5672e4bfab9SYan, Zheng fail:
568a22285a6SYan, Zheng 	kfree(pending_snapshot);
569f46b5a66SChristoph Hellwig 	return ret;
570f46b5a66SChristoph Hellwig }
571f46b5a66SChristoph Hellwig 
5724260f7c7SSage Weil /*  copy of check_sticky in fs/namei.c()
5734260f7c7SSage Weil * It's inline, so penalty for filesystems that don't use sticky bit is
5744260f7c7SSage Weil * minimal.
5754260f7c7SSage Weil */
5764260f7c7SSage Weil static inline int btrfs_check_sticky(struct inode *dir, struct inode *inode)
5774260f7c7SSage Weil {
5784260f7c7SSage Weil 	uid_t fsuid = current_fsuid();
5794260f7c7SSage Weil 
5804260f7c7SSage Weil 	if (!(dir->i_mode & S_ISVTX))
5814260f7c7SSage Weil 		return 0;
5824260f7c7SSage Weil 	if (inode->i_uid == fsuid)
5834260f7c7SSage Weil 		return 0;
5844260f7c7SSage Weil 	if (dir->i_uid == fsuid)
5854260f7c7SSage Weil 		return 0;
5864260f7c7SSage Weil 	return !capable(CAP_FOWNER);
5874260f7c7SSage Weil }
5884260f7c7SSage Weil 
5894260f7c7SSage Weil /*  copy of may_delete in fs/namei.c()
5904260f7c7SSage Weil  *	Check whether we can remove a link victim from directory dir, check
5914260f7c7SSage Weil  *  whether the type of victim is right.
5924260f7c7SSage Weil  *  1. We can't do it if dir is read-only (done in permission())
5934260f7c7SSage Weil  *  2. We should have write and exec permissions on dir
5944260f7c7SSage Weil  *  3. We can't remove anything from append-only dir
5954260f7c7SSage Weil  *  4. We can't do anything with immutable dir (done in permission())
5964260f7c7SSage Weil  *  5. If the sticky bit on dir is set we should either
5974260f7c7SSage Weil  *	a. be owner of dir, or
5984260f7c7SSage Weil  *	b. be owner of victim, or
5994260f7c7SSage Weil  *	c. have CAP_FOWNER capability
6004260f7c7SSage Weil  *  6. If the victim is append-only or immutable we can't do antyhing with
6014260f7c7SSage Weil  *     links pointing to it.
6024260f7c7SSage Weil  *  7. If we were asked to remove a directory and victim isn't one - ENOTDIR.
6034260f7c7SSage Weil  *  8. If we were asked to remove a non-directory and victim isn't one - EISDIR.
6044260f7c7SSage Weil  *  9. We can't remove a root or mountpoint.
6054260f7c7SSage Weil  * 10. We don't allow removal of NFS sillyrenamed files; it's handled by
6064260f7c7SSage Weil  *     nfs_async_unlink().
6074260f7c7SSage Weil  */
6084260f7c7SSage Weil 
6094260f7c7SSage Weil static int btrfs_may_delete(struct inode *dir,struct dentry *victim,int isdir)
6104260f7c7SSage Weil {
6114260f7c7SSage Weil 	int error;
6124260f7c7SSage Weil 
6134260f7c7SSage Weil 	if (!victim->d_inode)
6144260f7c7SSage Weil 		return -ENOENT;
6154260f7c7SSage Weil 
6164260f7c7SSage Weil 	BUG_ON(victim->d_parent->d_inode != dir);
6174260f7c7SSage Weil 	audit_inode_child(victim, dir);
6184260f7c7SSage Weil 
6194260f7c7SSage Weil 	error = inode_permission(dir, MAY_WRITE | MAY_EXEC);
6204260f7c7SSage Weil 	if (error)
6214260f7c7SSage Weil 		return error;
6224260f7c7SSage Weil 	if (IS_APPEND(dir))
6234260f7c7SSage Weil 		return -EPERM;
6244260f7c7SSage Weil 	if (btrfs_check_sticky(dir, victim->d_inode)||
6254260f7c7SSage Weil 		IS_APPEND(victim->d_inode)||
6264260f7c7SSage Weil 	    IS_IMMUTABLE(victim->d_inode) || IS_SWAPFILE(victim->d_inode))
6274260f7c7SSage Weil 		return -EPERM;
6284260f7c7SSage Weil 	if (isdir) {
6294260f7c7SSage Weil 		if (!S_ISDIR(victim->d_inode->i_mode))
6304260f7c7SSage Weil 			return -ENOTDIR;
6314260f7c7SSage Weil 		if (IS_ROOT(victim))
6324260f7c7SSage Weil 			return -EBUSY;
6334260f7c7SSage Weil 	} else if (S_ISDIR(victim->d_inode->i_mode))
6344260f7c7SSage Weil 		return -EISDIR;
6354260f7c7SSage Weil 	if (IS_DEADDIR(dir))
6364260f7c7SSage Weil 		return -ENOENT;
6374260f7c7SSage Weil 	if (victim->d_flags & DCACHE_NFSFS_RENAMED)
6384260f7c7SSage Weil 		return -EBUSY;
6394260f7c7SSage Weil 	return 0;
6404260f7c7SSage Weil }
6414260f7c7SSage Weil 
642cb8e7090SChristoph Hellwig /* copy of may_create in fs/namei.c() */
643cb8e7090SChristoph Hellwig static inline int btrfs_may_create(struct inode *dir, struct dentry *child)
644cb8e7090SChristoph Hellwig {
645cb8e7090SChristoph Hellwig 	if (child->d_inode)
646cb8e7090SChristoph Hellwig 		return -EEXIST;
647cb8e7090SChristoph Hellwig 	if (IS_DEADDIR(dir))
648cb8e7090SChristoph Hellwig 		return -ENOENT;
649cb8e7090SChristoph Hellwig 	return inode_permission(dir, MAY_WRITE | MAY_EXEC);
650cb8e7090SChristoph Hellwig }
651cb8e7090SChristoph Hellwig 
652cb8e7090SChristoph Hellwig /*
653cb8e7090SChristoph Hellwig  * Create a new subvolume below @parent.  This is largely modeled after
654cb8e7090SChristoph Hellwig  * sys_mkdirat and vfs_mkdir, but we only do a single component lookup
655cb8e7090SChristoph Hellwig  * inside this filesystem so it's quite a bit simpler.
656cb8e7090SChristoph Hellwig  */
65776dda93cSYan, Zheng static noinline int btrfs_mksubvol(struct path *parent,
65876dda93cSYan, Zheng 				   char *name, int namelen,
65972fd032eSSage Weil 				   struct btrfs_root *snap_src,
6606f72c7e2SArne Jansen 				   u64 *async_transid, bool readonly,
6616f72c7e2SArne Jansen 				   struct btrfs_qgroup_inherit **inherit)
662cb8e7090SChristoph Hellwig {
66376dda93cSYan, Zheng 	struct inode *dir  = parent->dentry->d_inode;
664cb8e7090SChristoph Hellwig 	struct dentry *dentry;
665cb8e7090SChristoph Hellwig 	int error;
666cb8e7090SChristoph Hellwig 
66776dda93cSYan, Zheng 	mutex_lock_nested(&dir->i_mutex, I_MUTEX_PARENT);
668cb8e7090SChristoph Hellwig 
669cb8e7090SChristoph Hellwig 	dentry = lookup_one_len(name, parent->dentry, namelen);
670cb8e7090SChristoph Hellwig 	error = PTR_ERR(dentry);
671cb8e7090SChristoph Hellwig 	if (IS_ERR(dentry))
672cb8e7090SChristoph Hellwig 		goto out_unlock;
673cb8e7090SChristoph Hellwig 
674cb8e7090SChristoph Hellwig 	error = -EEXIST;
675cb8e7090SChristoph Hellwig 	if (dentry->d_inode)
676cb8e7090SChristoph Hellwig 		goto out_dput;
677cb8e7090SChristoph Hellwig 
67876dda93cSYan, Zheng 	error = btrfs_may_create(dir, dentry);
679cb8e7090SChristoph Hellwig 	if (error)
680a874a63eSLiu Bo 		goto out_dput;
681cb8e7090SChristoph Hellwig 
68276dda93cSYan, Zheng 	down_read(&BTRFS_I(dir)->root->fs_info->subvol_sem);
68376dda93cSYan, Zheng 
68476dda93cSYan, Zheng 	if (btrfs_root_refs(&BTRFS_I(dir)->root->root_item) == 0)
68576dda93cSYan, Zheng 		goto out_up_read;
68676dda93cSYan, Zheng 
6873de4586cSChris Mason 	if (snap_src) {
6886f72c7e2SArne Jansen 		error = create_snapshot(snap_src, dentry, name, namelen,
6896f72c7e2SArne Jansen 					async_transid, readonly, inherit);
6903de4586cSChris Mason 	} else {
69176dda93cSYan, Zheng 		error = create_subvol(BTRFS_I(dir)->root, dentry,
6926f72c7e2SArne Jansen 				      name, namelen, async_transid, inherit);
6933de4586cSChris Mason 	}
69476dda93cSYan, Zheng 	if (!error)
69576dda93cSYan, Zheng 		fsnotify_mkdir(dir, dentry);
69676dda93cSYan, Zheng out_up_read:
69776dda93cSYan, Zheng 	up_read(&BTRFS_I(dir)->root->fs_info->subvol_sem);
698cb8e7090SChristoph Hellwig out_dput:
699cb8e7090SChristoph Hellwig 	dput(dentry);
700cb8e7090SChristoph Hellwig out_unlock:
70176dda93cSYan, Zheng 	mutex_unlock(&dir->i_mutex);
702cb8e7090SChristoph Hellwig 	return error;
703cb8e7090SChristoph Hellwig }
704cb8e7090SChristoph Hellwig 
7054cb5300bSChris Mason /*
7064cb5300bSChris Mason  * When we're defragging a range, we don't want to kick it off again
7074cb5300bSChris Mason  * if it is really just waiting for delalloc to send it down.
7084cb5300bSChris Mason  * If we find a nice big extent or delalloc range for the bytes in the
7094cb5300bSChris Mason  * file you want to defrag, we return 0 to let you know to skip this
7104cb5300bSChris Mason  * part of the file
7114cb5300bSChris Mason  */
7124cb5300bSChris Mason static int check_defrag_in_cache(struct inode *inode, u64 offset, int thresh)
7134cb5300bSChris Mason {
7144cb5300bSChris Mason 	struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
7154cb5300bSChris Mason 	struct extent_map *em = NULL;
7164cb5300bSChris Mason 	struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
7174cb5300bSChris Mason 	u64 end;
7184cb5300bSChris Mason 
7194cb5300bSChris Mason 	read_lock(&em_tree->lock);
7204cb5300bSChris Mason 	em = lookup_extent_mapping(em_tree, offset, PAGE_CACHE_SIZE);
7214cb5300bSChris Mason 	read_unlock(&em_tree->lock);
7224cb5300bSChris Mason 
7234cb5300bSChris Mason 	if (em) {
7244cb5300bSChris Mason 		end = extent_map_end(em);
7254cb5300bSChris Mason 		free_extent_map(em);
7264cb5300bSChris Mason 		if (end - offset > thresh)
7274cb5300bSChris Mason 			return 0;
7284cb5300bSChris Mason 	}
7294cb5300bSChris Mason 	/* if we already have a nice delalloc here, just stop */
7304cb5300bSChris Mason 	thresh /= 2;
7314cb5300bSChris Mason 	end = count_range_bits(io_tree, &offset, offset + thresh,
7324cb5300bSChris Mason 			       thresh, EXTENT_DELALLOC, 1);
7334cb5300bSChris Mason 	if (end >= thresh)
7344cb5300bSChris Mason 		return 0;
7354cb5300bSChris Mason 	return 1;
7364cb5300bSChris Mason }
7374cb5300bSChris Mason 
7384cb5300bSChris Mason /*
7394cb5300bSChris Mason  * helper function to walk through a file and find extents
7404cb5300bSChris Mason  * newer than a specific transid, and smaller than thresh.
7414cb5300bSChris Mason  *
7424cb5300bSChris Mason  * This is used by the defragging code to find new and small
7434cb5300bSChris Mason  * extents
7444cb5300bSChris Mason  */
7454cb5300bSChris Mason static int find_new_extents(struct btrfs_root *root,
7464cb5300bSChris Mason 			    struct inode *inode, u64 newer_than,
7474cb5300bSChris Mason 			    u64 *off, int thresh)
7484cb5300bSChris Mason {
7494cb5300bSChris Mason 	struct btrfs_path *path;
7504cb5300bSChris Mason 	struct btrfs_key min_key;
7514cb5300bSChris Mason 	struct btrfs_key max_key;
7524cb5300bSChris Mason 	struct extent_buffer *leaf;
7534cb5300bSChris Mason 	struct btrfs_file_extent_item *extent;
7544cb5300bSChris Mason 	int type;
7554cb5300bSChris Mason 	int ret;
756a4689d2bSDavid Sterba 	u64 ino = btrfs_ino(inode);
7574cb5300bSChris Mason 
7584cb5300bSChris Mason 	path = btrfs_alloc_path();
7594cb5300bSChris Mason 	if (!path)
7604cb5300bSChris Mason 		return -ENOMEM;
7614cb5300bSChris Mason 
762a4689d2bSDavid Sterba 	min_key.objectid = ino;
7634cb5300bSChris Mason 	min_key.type = BTRFS_EXTENT_DATA_KEY;
7644cb5300bSChris Mason 	min_key.offset = *off;
7654cb5300bSChris Mason 
766a4689d2bSDavid Sterba 	max_key.objectid = ino;
7674cb5300bSChris Mason 	max_key.type = (u8)-1;
7684cb5300bSChris Mason 	max_key.offset = (u64)-1;
7694cb5300bSChris Mason 
7704cb5300bSChris Mason 	path->keep_locks = 1;
7714cb5300bSChris Mason 
7724cb5300bSChris Mason 	while(1) {
7734cb5300bSChris Mason 		ret = btrfs_search_forward(root, &min_key, &max_key,
7744cb5300bSChris Mason 					   path, 0, newer_than);
7754cb5300bSChris Mason 		if (ret != 0)
7764cb5300bSChris Mason 			goto none;
777a4689d2bSDavid Sterba 		if (min_key.objectid != ino)
7784cb5300bSChris Mason 			goto none;
7794cb5300bSChris Mason 		if (min_key.type != BTRFS_EXTENT_DATA_KEY)
7804cb5300bSChris Mason 			goto none;
7814cb5300bSChris Mason 
7824cb5300bSChris Mason 		leaf = path->nodes[0];
7834cb5300bSChris Mason 		extent = btrfs_item_ptr(leaf, path->slots[0],
7844cb5300bSChris Mason 					struct btrfs_file_extent_item);
7854cb5300bSChris Mason 
7864cb5300bSChris Mason 		type = btrfs_file_extent_type(leaf, extent);
7874cb5300bSChris Mason 		if (type == BTRFS_FILE_EXTENT_REG &&
7884cb5300bSChris Mason 		    btrfs_file_extent_num_bytes(leaf, extent) < thresh &&
7894cb5300bSChris Mason 		    check_defrag_in_cache(inode, min_key.offset, thresh)) {
7904cb5300bSChris Mason 			*off = min_key.offset;
7914cb5300bSChris Mason 			btrfs_free_path(path);
7924cb5300bSChris Mason 			return 0;
7934cb5300bSChris Mason 		}
7944cb5300bSChris Mason 
7954cb5300bSChris Mason 		if (min_key.offset == (u64)-1)
7964cb5300bSChris Mason 			goto none;
7974cb5300bSChris Mason 
7984cb5300bSChris Mason 		min_key.offset++;
7994cb5300bSChris Mason 		btrfs_release_path(path);
8004cb5300bSChris Mason 	}
8014cb5300bSChris Mason none:
8024cb5300bSChris Mason 	btrfs_free_path(path);
8034cb5300bSChris Mason 	return -ENOENT;
8044cb5300bSChris Mason }
8054cb5300bSChris Mason 
8066c282eb4SLi Zefan static struct extent_map *defrag_lookup_extent(struct inode *inode, u64 start)
80717ce6ef8SLiu Bo {
80817ce6ef8SLiu Bo 	struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
809940100a4SChris Mason 	struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
8106c282eb4SLi Zefan 	struct extent_map *em;
8116c282eb4SLi Zefan 	u64 len = PAGE_CACHE_SIZE;
812940100a4SChris Mason 
813940100a4SChris Mason 	/*
814940100a4SChris Mason 	 * hopefully we have this extent in the tree already, try without
815940100a4SChris Mason 	 * the full extent lock
816940100a4SChris Mason 	 */
817940100a4SChris Mason 	read_lock(&em_tree->lock);
818940100a4SChris Mason 	em = lookup_extent_mapping(em_tree, start, len);
819940100a4SChris Mason 	read_unlock(&em_tree->lock);
820940100a4SChris Mason 
821940100a4SChris Mason 	if (!em) {
822940100a4SChris Mason 		/* get the big lock and read metadata off disk */
823d0082371SJeff Mahoney 		lock_extent(io_tree, start, start + len - 1);
824940100a4SChris Mason 		em = btrfs_get_extent(inode, NULL, 0, start, len, 0);
825d0082371SJeff Mahoney 		unlock_extent(io_tree, start, start + len - 1);
826940100a4SChris Mason 
8276cf8bfbfSDan Carpenter 		if (IS_ERR(em))
8286c282eb4SLi Zefan 			return NULL;
829940100a4SChris Mason 	}
830940100a4SChris Mason 
8316c282eb4SLi Zefan 	return em;
8326c282eb4SLi Zefan }
8336c282eb4SLi Zefan 
8346c282eb4SLi Zefan static bool defrag_check_next_extent(struct inode *inode, struct extent_map *em)
8356c282eb4SLi Zefan {
8366c282eb4SLi Zefan 	struct extent_map *next;
8376c282eb4SLi Zefan 	bool ret = true;
8386c282eb4SLi Zefan 
8396c282eb4SLi Zefan 	/* this is the last extent */
8406c282eb4SLi Zefan 	if (em->start + em->len >= i_size_read(inode))
8416c282eb4SLi Zefan 		return false;
8426c282eb4SLi Zefan 
8436c282eb4SLi Zefan 	next = defrag_lookup_extent(inode, em->start + em->len);
8446c282eb4SLi Zefan 	if (!next || next->block_start >= EXTENT_MAP_LAST_BYTE)
8456c282eb4SLi Zefan 		ret = false;
8466c282eb4SLi Zefan 
8476c282eb4SLi Zefan 	free_extent_map(next);
8486c282eb4SLi Zefan 	return ret;
8496c282eb4SLi Zefan }
8506c282eb4SLi Zefan 
8516c282eb4SLi Zefan static int should_defrag_range(struct inode *inode, u64 start, int thresh,
852a43a2111SAndrew Mahone 			       u64 *last_len, u64 *skip, u64 *defrag_end,
853a43a2111SAndrew Mahone 			       int compress)
8546c282eb4SLi Zefan {
8556c282eb4SLi Zefan 	struct extent_map *em;
8566c282eb4SLi Zefan 	int ret = 1;
8576c282eb4SLi Zefan 	bool next_mergeable = true;
8586c282eb4SLi Zefan 
8596c282eb4SLi Zefan 	/*
8606c282eb4SLi Zefan 	 * make sure that once we start defragging an extent, we keep on
8616c282eb4SLi Zefan 	 * defragging it
8626c282eb4SLi Zefan 	 */
8636c282eb4SLi Zefan 	if (start < *defrag_end)
8646c282eb4SLi Zefan 		return 1;
8656c282eb4SLi Zefan 
8666c282eb4SLi Zefan 	*skip = 0;
8676c282eb4SLi Zefan 
8686c282eb4SLi Zefan 	em = defrag_lookup_extent(inode, start);
8696c282eb4SLi Zefan 	if (!em)
8706c282eb4SLi Zefan 		return 0;
8716c282eb4SLi Zefan 
872940100a4SChris Mason 	/* this will cover holes, and inline extents */
87317ce6ef8SLiu Bo 	if (em->block_start >= EXTENT_MAP_LAST_BYTE) {
874940100a4SChris Mason 		ret = 0;
87517ce6ef8SLiu Bo 		goto out;
87617ce6ef8SLiu Bo 	}
87717ce6ef8SLiu Bo 
8786c282eb4SLi Zefan 	next_mergeable = defrag_check_next_extent(inode, em);
879940100a4SChris Mason 
880940100a4SChris Mason 	/*
8816c282eb4SLi Zefan 	 * we hit a real extent, if it is big or the next extent is not a
8826c282eb4SLi Zefan 	 * real extent, don't bother defragging it
883940100a4SChris Mason 	 */
884a43a2111SAndrew Mahone 	if (!compress && (*last_len == 0 || *last_len >= thresh) &&
8856c282eb4SLi Zefan 	    (em->len >= thresh || !next_mergeable))
886940100a4SChris Mason 		ret = 0;
88717ce6ef8SLiu Bo out:
888940100a4SChris Mason 	/*
889940100a4SChris Mason 	 * last_len ends up being a counter of how many bytes we've defragged.
890940100a4SChris Mason 	 * every time we choose not to defrag an extent, we reset *last_len
891940100a4SChris Mason 	 * so that the next tiny extent will force a defrag.
892940100a4SChris Mason 	 *
893940100a4SChris Mason 	 * The end result of this is that tiny extents before a single big
894940100a4SChris Mason 	 * extent will force at least part of that big extent to be defragged.
895940100a4SChris Mason 	 */
896940100a4SChris Mason 	if (ret) {
897940100a4SChris Mason 		*defrag_end = extent_map_end(em);
898940100a4SChris Mason 	} else {
899940100a4SChris Mason 		*last_len = 0;
900940100a4SChris Mason 		*skip = extent_map_end(em);
901940100a4SChris Mason 		*defrag_end = 0;
902940100a4SChris Mason 	}
903940100a4SChris Mason 
904940100a4SChris Mason 	free_extent_map(em);
905940100a4SChris Mason 	return ret;
906940100a4SChris Mason }
907940100a4SChris Mason 
9084cb5300bSChris Mason /*
9094cb5300bSChris Mason  * it doesn't do much good to defrag one or two pages
9104cb5300bSChris Mason  * at a time.  This pulls in a nice chunk of pages
9114cb5300bSChris Mason  * to COW and defrag.
9124cb5300bSChris Mason  *
9134cb5300bSChris Mason  * It also makes sure the delalloc code has enough
9144cb5300bSChris Mason  * dirty data to avoid making new small extents as part
9154cb5300bSChris Mason  * of the defrag
9164cb5300bSChris Mason  *
9174cb5300bSChris Mason  * It's a good idea to start RA on this range
9184cb5300bSChris Mason  * before calling this.
9194cb5300bSChris Mason  */
9204cb5300bSChris Mason static int cluster_pages_for_defrag(struct inode *inode,
9214cb5300bSChris Mason 				    struct page **pages,
9224cb5300bSChris Mason 				    unsigned long start_index,
9234cb5300bSChris Mason 				    int num_pages)
924f46b5a66SChristoph Hellwig {
9254cb5300bSChris Mason 	unsigned long file_end;
9264cb5300bSChris Mason 	u64 isize = i_size_read(inode);
927f46b5a66SChristoph Hellwig 	u64 page_start;
928f46b5a66SChristoph Hellwig 	u64 page_end;
9291f12bd06SLiu Bo 	u64 page_cnt;
9304cb5300bSChris Mason 	int ret;
9314cb5300bSChris Mason 	int i;
9324cb5300bSChris Mason 	int i_done;
9334cb5300bSChris Mason 	struct btrfs_ordered_extent *ordered;
9344cb5300bSChris Mason 	struct extent_state *cached_state = NULL;
935600a45e1SMiao Xie 	struct extent_io_tree *tree;
9363b16a4e3SJosef Bacik 	gfp_t mask = btrfs_alloc_write_mask(inode->i_mapping);
9374cb5300bSChris Mason 
9384cb5300bSChris Mason 	file_end = (isize - 1) >> PAGE_CACHE_SHIFT;
9391f12bd06SLiu Bo 	if (!isize || start_index > file_end)
9401f12bd06SLiu Bo 		return 0;
9411f12bd06SLiu Bo 
9421f12bd06SLiu Bo 	page_cnt = min_t(u64, (u64)num_pages, (u64)file_end - start_index + 1);
9434cb5300bSChris Mason 
9444cb5300bSChris Mason 	ret = btrfs_delalloc_reserve_space(inode,
9451f12bd06SLiu Bo 					   page_cnt << PAGE_CACHE_SHIFT);
9464cb5300bSChris Mason 	if (ret)
9474cb5300bSChris Mason 		return ret;
9484cb5300bSChris Mason 	i_done = 0;
949600a45e1SMiao Xie 	tree = &BTRFS_I(inode)->io_tree;
9504cb5300bSChris Mason 
9514cb5300bSChris Mason 	/* step one, lock all the pages */
9521f12bd06SLiu Bo 	for (i = 0; i < page_cnt; i++) {
9534cb5300bSChris Mason 		struct page *page;
954600a45e1SMiao Xie again:
955a94733d0SJosef Bacik 		page = find_or_create_page(inode->i_mapping,
9563b16a4e3SJosef Bacik 					   start_index + i, mask);
9574cb5300bSChris Mason 		if (!page)
9584cb5300bSChris Mason 			break;
9594cb5300bSChris Mason 
960600a45e1SMiao Xie 		page_start = page_offset(page);
961600a45e1SMiao Xie 		page_end = page_start + PAGE_CACHE_SIZE - 1;
962600a45e1SMiao Xie 		while (1) {
963d0082371SJeff Mahoney 			lock_extent(tree, page_start, page_end);
964600a45e1SMiao Xie 			ordered = btrfs_lookup_ordered_extent(inode,
965600a45e1SMiao Xie 							      page_start);
966d0082371SJeff Mahoney 			unlock_extent(tree, page_start, page_end);
967600a45e1SMiao Xie 			if (!ordered)
968600a45e1SMiao Xie 				break;
969600a45e1SMiao Xie 
970600a45e1SMiao Xie 			unlock_page(page);
971600a45e1SMiao Xie 			btrfs_start_ordered_extent(inode, ordered, 1);
972600a45e1SMiao Xie 			btrfs_put_ordered_extent(ordered);
973600a45e1SMiao Xie 			lock_page(page);
9741f12bd06SLiu Bo 			/*
9751f12bd06SLiu Bo 			 * we unlocked the page above, so we need check if
9761f12bd06SLiu Bo 			 * it was released or not.
9771f12bd06SLiu Bo 			 */
9781f12bd06SLiu Bo 			if (page->mapping != inode->i_mapping) {
9791f12bd06SLiu Bo 				unlock_page(page);
9801f12bd06SLiu Bo 				page_cache_release(page);
9811f12bd06SLiu Bo 				goto again;
9821f12bd06SLiu Bo 			}
983600a45e1SMiao Xie 		}
984600a45e1SMiao Xie 
9854cb5300bSChris Mason 		if (!PageUptodate(page)) {
9864cb5300bSChris Mason 			btrfs_readpage(NULL, page);
9874cb5300bSChris Mason 			lock_page(page);
9884cb5300bSChris Mason 			if (!PageUptodate(page)) {
9894cb5300bSChris Mason 				unlock_page(page);
9904cb5300bSChris Mason 				page_cache_release(page);
9914cb5300bSChris Mason 				ret = -EIO;
9924cb5300bSChris Mason 				break;
9934cb5300bSChris Mason 			}
9944cb5300bSChris Mason 		}
995600a45e1SMiao Xie 
996600a45e1SMiao Xie 		if (page->mapping != inode->i_mapping) {
997600a45e1SMiao Xie 			unlock_page(page);
998600a45e1SMiao Xie 			page_cache_release(page);
999600a45e1SMiao Xie 			goto again;
1000600a45e1SMiao Xie 		}
1001600a45e1SMiao Xie 
10024cb5300bSChris Mason 		pages[i] = page;
10034cb5300bSChris Mason 		i_done++;
10044cb5300bSChris Mason 	}
10054cb5300bSChris Mason 	if (!i_done || ret)
10064cb5300bSChris Mason 		goto out;
10074cb5300bSChris Mason 
10084cb5300bSChris Mason 	if (!(inode->i_sb->s_flags & MS_ACTIVE))
10094cb5300bSChris Mason 		goto out;
10104cb5300bSChris Mason 
10114cb5300bSChris Mason 	/*
10124cb5300bSChris Mason 	 * so now we have a nice long stream of locked
10134cb5300bSChris Mason 	 * and up to date pages, lets wait on them
10144cb5300bSChris Mason 	 */
10154cb5300bSChris Mason 	for (i = 0; i < i_done; i++)
10164cb5300bSChris Mason 		wait_on_page_writeback(pages[i]);
10174cb5300bSChris Mason 
10184cb5300bSChris Mason 	page_start = page_offset(pages[0]);
10194cb5300bSChris Mason 	page_end = page_offset(pages[i_done - 1]) + PAGE_CACHE_SIZE;
10204cb5300bSChris Mason 
10214cb5300bSChris Mason 	lock_extent_bits(&BTRFS_I(inode)->io_tree,
1022d0082371SJeff Mahoney 			 page_start, page_end - 1, 0, &cached_state);
10234cb5300bSChris Mason 	clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start,
10244cb5300bSChris Mason 			  page_end - 1, EXTENT_DIRTY | EXTENT_DELALLOC |
10254cb5300bSChris Mason 			  EXTENT_DO_ACCOUNTING, 0, 0, &cached_state,
10264cb5300bSChris Mason 			  GFP_NOFS);
10274cb5300bSChris Mason 
10281f12bd06SLiu Bo 	if (i_done != page_cnt) {
10299e0baf60SJosef Bacik 		spin_lock(&BTRFS_I(inode)->lock);
10309e0baf60SJosef Bacik 		BTRFS_I(inode)->outstanding_extents++;
10319e0baf60SJosef Bacik 		spin_unlock(&BTRFS_I(inode)->lock);
10324cb5300bSChris Mason 		btrfs_delalloc_release_space(inode,
10331f12bd06SLiu Bo 				     (page_cnt - i_done) << PAGE_CACHE_SHIFT);
10344cb5300bSChris Mason 	}
10354cb5300bSChris Mason 
10364cb5300bSChris Mason 
10374cb5300bSChris Mason 	btrfs_set_extent_delalloc(inode, page_start, page_end - 1,
10384cb5300bSChris Mason 				  &cached_state);
10394cb5300bSChris Mason 
10404cb5300bSChris Mason 	unlock_extent_cached(&BTRFS_I(inode)->io_tree,
10414cb5300bSChris Mason 			     page_start, page_end - 1, &cached_state,
10424cb5300bSChris Mason 			     GFP_NOFS);
10434cb5300bSChris Mason 
10444cb5300bSChris Mason 	for (i = 0; i < i_done; i++) {
10454cb5300bSChris Mason 		clear_page_dirty_for_io(pages[i]);
10464cb5300bSChris Mason 		ClearPageChecked(pages[i]);
10474cb5300bSChris Mason 		set_page_extent_mapped(pages[i]);
10484cb5300bSChris Mason 		set_page_dirty(pages[i]);
10494cb5300bSChris Mason 		unlock_page(pages[i]);
10504cb5300bSChris Mason 		page_cache_release(pages[i]);
10514cb5300bSChris Mason 	}
10524cb5300bSChris Mason 	return i_done;
10534cb5300bSChris Mason out:
10544cb5300bSChris Mason 	for (i = 0; i < i_done; i++) {
10554cb5300bSChris Mason 		unlock_page(pages[i]);
10564cb5300bSChris Mason 		page_cache_release(pages[i]);
10574cb5300bSChris Mason 	}
10581f12bd06SLiu Bo 	btrfs_delalloc_release_space(inode, page_cnt << PAGE_CACHE_SHIFT);
10594cb5300bSChris Mason 	return ret;
10604cb5300bSChris Mason 
10614cb5300bSChris Mason }
10624cb5300bSChris Mason 
10634cb5300bSChris Mason int btrfs_defrag_file(struct inode *inode, struct file *file,
10644cb5300bSChris Mason 		      struct btrfs_ioctl_defrag_range_args *range,
10654cb5300bSChris Mason 		      u64 newer_than, unsigned long max_to_defrag)
10664cb5300bSChris Mason {
10674cb5300bSChris Mason 	struct btrfs_root *root = BTRFS_I(inode)->root;
10684cb5300bSChris Mason 	struct file_ra_state *ra = NULL;
10694cb5300bSChris Mason 	unsigned long last_index;
1070151a31b2SLi Zefan 	u64 isize = i_size_read(inode);
1071940100a4SChris Mason 	u64 last_len = 0;
1072940100a4SChris Mason 	u64 skip = 0;
1073940100a4SChris Mason 	u64 defrag_end = 0;
10744cb5300bSChris Mason 	u64 newer_off = range->start;
1075f46b5a66SChristoph Hellwig 	unsigned long i;
1076008873eaSLi Zefan 	unsigned long ra_index = 0;
1077f46b5a66SChristoph Hellwig 	int ret;
10784cb5300bSChris Mason 	int defrag_count = 0;
10791a419d85SLi Zefan 	int compress_type = BTRFS_COMPRESS_ZLIB;
10804cb5300bSChris Mason 	int extent_thresh = range->extent_thresh;
1081008873eaSLi Zefan 	int max_cluster = (256 * 1024) >> PAGE_CACHE_SHIFT;
1082008873eaSLi Zefan 	int cluster = max_cluster;
10834cb5300bSChris Mason 	u64 new_align = ~((u64)128 * 1024 - 1);
10844cb5300bSChris Mason 	struct page **pages = NULL;
10854cb5300bSChris Mason 
10864cb5300bSChris Mason 	if (extent_thresh == 0)
10874cb5300bSChris Mason 		extent_thresh = 256 * 1024;
10881a419d85SLi Zefan 
10891a419d85SLi Zefan 	if (range->flags & BTRFS_DEFRAG_RANGE_COMPRESS) {
10901a419d85SLi Zefan 		if (range->compress_type > BTRFS_COMPRESS_TYPES)
10911a419d85SLi Zefan 			return -EINVAL;
10921a419d85SLi Zefan 		if (range->compress_type)
10931a419d85SLi Zefan 			compress_type = range->compress_type;
10941a419d85SLi Zefan 	}
1095f46b5a66SChristoph Hellwig 
1096151a31b2SLi Zefan 	if (isize == 0)
1097940100a4SChris Mason 		return 0;
1098f46b5a66SChristoph Hellwig 
10994cb5300bSChris Mason 	/*
11004cb5300bSChris Mason 	 * if we were not given a file, allocate a readahead
11014cb5300bSChris Mason 	 * context
11024cb5300bSChris Mason 	 */
11034cb5300bSChris Mason 	if (!file) {
11044cb5300bSChris Mason 		ra = kzalloc(sizeof(*ra), GFP_NOFS);
11054cb5300bSChris Mason 		if (!ra)
11064cb5300bSChris Mason 			return -ENOMEM;
11074cb5300bSChris Mason 		file_ra_state_init(ra, inode->i_mapping);
11084cb5300bSChris Mason 	} else {
11094cb5300bSChris Mason 		ra = &file->f_ra;
11104cb5300bSChris Mason 	}
11114cb5300bSChris Mason 
1112008873eaSLi Zefan 	pages = kmalloc(sizeof(struct page *) * max_cluster,
11134cb5300bSChris Mason 			GFP_NOFS);
11144cb5300bSChris Mason 	if (!pages) {
11154cb5300bSChris Mason 		ret = -ENOMEM;
11164cb5300bSChris Mason 		goto out_ra;
11174cb5300bSChris Mason 	}
11184cb5300bSChris Mason 
11194cb5300bSChris Mason 	/* find the last page to defrag */
11201e701a32SChris Mason 	if (range->start + range->len > range->start) {
1121151a31b2SLi Zefan 		last_index = min_t(u64, isize - 1,
11221e701a32SChris Mason 			 range->start + range->len - 1) >> PAGE_CACHE_SHIFT;
11231e701a32SChris Mason 	} else {
1124151a31b2SLi Zefan 		last_index = (isize - 1) >> PAGE_CACHE_SHIFT;
11251e701a32SChris Mason 	}
11261e701a32SChris Mason 
11274cb5300bSChris Mason 	if (newer_than) {
11284cb5300bSChris Mason 		ret = find_new_extents(root, inode, newer_than,
11294cb5300bSChris Mason 				       &newer_off, 64 * 1024);
11304cb5300bSChris Mason 		if (!ret) {
11314cb5300bSChris Mason 			range->start = newer_off;
11324cb5300bSChris Mason 			/*
11334cb5300bSChris Mason 			 * we always align our defrag to help keep
11344cb5300bSChris Mason 			 * the extents in the file evenly spaced
11354cb5300bSChris Mason 			 */
11364cb5300bSChris Mason 			i = (newer_off & new_align) >> PAGE_CACHE_SHIFT;
11374cb5300bSChris Mason 		} else
11384cb5300bSChris Mason 			goto out_ra;
11394cb5300bSChris Mason 	} else {
11401e701a32SChris Mason 		i = range->start >> PAGE_CACHE_SHIFT;
11414cb5300bSChris Mason 	}
11424cb5300bSChris Mason 	if (!max_to_defrag)
11437ec31b54SLiu Bo 		max_to_defrag = last_index + 1;
11444cb5300bSChris Mason 
11452a0f7f57SLi Zefan 	/*
11462a0f7f57SLi Zefan 	 * make writeback starts from i, so the defrag range can be
11472a0f7f57SLi Zefan 	 * written sequentially.
11482a0f7f57SLi Zefan 	 */
11492a0f7f57SLi Zefan 	if (i < inode->i_mapping->writeback_index)
11502a0f7f57SLi Zefan 		inode->i_mapping->writeback_index = i;
11512a0f7f57SLi Zefan 
1152f7f43cc8SChris Mason 	while (i <= last_index && defrag_count < max_to_defrag &&
1153f7f43cc8SChris Mason 	       (i < (i_size_read(inode) + PAGE_CACHE_SIZE - 1) >>
1154f7f43cc8SChris Mason 		PAGE_CACHE_SHIFT)) {
11554cb5300bSChris Mason 		/*
11564cb5300bSChris Mason 		 * make sure we stop running if someone unmounts
11574cb5300bSChris Mason 		 * the FS
11584cb5300bSChris Mason 		 */
11594cb5300bSChris Mason 		if (!(inode->i_sb->s_flags & MS_ACTIVE))
11604cb5300bSChris Mason 			break;
11614cb5300bSChris Mason 
116266c26892SLiu Bo 		if (!should_defrag_range(inode, (u64)i << PAGE_CACHE_SHIFT,
11636c282eb4SLi Zefan 					 extent_thresh, &last_len, &skip,
1164a43a2111SAndrew Mahone 					 &defrag_end, range->flags &
1165a43a2111SAndrew Mahone 					 BTRFS_DEFRAG_RANGE_COMPRESS)) {
1166940100a4SChris Mason 			unsigned long next;
1167940100a4SChris Mason 			/*
1168940100a4SChris Mason 			 * the should_defrag function tells us how much to skip
1169940100a4SChris Mason 			 * bump our counter by the suggested amount
1170940100a4SChris Mason 			 */
1171940100a4SChris Mason 			next = (skip + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
1172940100a4SChris Mason 			i = max(i + 1, next);
1173940100a4SChris Mason 			continue;
1174940100a4SChris Mason 		}
1175008873eaSLi Zefan 
1176008873eaSLi Zefan 		if (!newer_than) {
1177008873eaSLi Zefan 			cluster = (PAGE_CACHE_ALIGN(defrag_end) >>
1178008873eaSLi Zefan 				   PAGE_CACHE_SHIFT) - i;
1179008873eaSLi Zefan 			cluster = min(cluster, max_cluster);
1180008873eaSLi Zefan 		} else {
1181008873eaSLi Zefan 			cluster = max_cluster;
1182008873eaSLi Zefan 		}
1183008873eaSLi Zefan 
11841e701a32SChris Mason 		if (range->flags & BTRFS_DEFRAG_RANGE_COMPRESS)
11851a419d85SLi Zefan 			BTRFS_I(inode)->force_compress = compress_type;
1186940100a4SChris Mason 
1187008873eaSLi Zefan 		if (i + cluster > ra_index) {
1188008873eaSLi Zefan 			ra_index = max(i, ra_index);
1189008873eaSLi Zefan 			btrfs_force_ra(inode->i_mapping, ra, file, ra_index,
1190008873eaSLi Zefan 				       cluster);
1191008873eaSLi Zefan 			ra_index += max_cluster;
1192008873eaSLi Zefan 		}
11934cb5300bSChris Mason 
1194ecb8bea8SLiu Bo 		mutex_lock(&inode->i_mutex);
1195008873eaSLi Zefan 		ret = cluster_pages_for_defrag(inode, pages, i, cluster);
1196ecb8bea8SLiu Bo 		if (ret < 0) {
1197ecb8bea8SLiu Bo 			mutex_unlock(&inode->i_mutex);
11984cb5300bSChris Mason 			goto out_ra;
1199ecb8bea8SLiu Bo 		}
12004cb5300bSChris Mason 
12014cb5300bSChris Mason 		defrag_count += ret;
12024cb5300bSChris Mason 		balance_dirty_pages_ratelimited_nr(inode->i_mapping, ret);
1203ecb8bea8SLiu Bo 		mutex_unlock(&inode->i_mutex);
12044cb5300bSChris Mason 
12054cb5300bSChris Mason 		if (newer_than) {
12064cb5300bSChris Mason 			if (newer_off == (u64)-1)
12074cb5300bSChris Mason 				break;
12084cb5300bSChris Mason 
1209e1f041e1SLiu Bo 			if (ret > 0)
1210e1f041e1SLiu Bo 				i += ret;
1211e1f041e1SLiu Bo 
12124cb5300bSChris Mason 			newer_off = max(newer_off + 1,
12134cb5300bSChris Mason 					(u64)i << PAGE_CACHE_SHIFT);
12144cb5300bSChris Mason 
12154cb5300bSChris Mason 			ret = find_new_extents(root, inode,
12164cb5300bSChris Mason 					       newer_than, &newer_off,
12174cb5300bSChris Mason 					       64 * 1024);
12184cb5300bSChris Mason 			if (!ret) {
12194cb5300bSChris Mason 				range->start = newer_off;
12204cb5300bSChris Mason 				i = (newer_off & new_align) >> PAGE_CACHE_SHIFT;
12214cb5300bSChris Mason 			} else {
12224cb5300bSChris Mason 				break;
1223940100a4SChris Mason 			}
12244cb5300bSChris Mason 		} else {
1225008873eaSLi Zefan 			if (ret > 0) {
1226cbcc8326SLi Zefan 				i += ret;
1227008873eaSLi Zefan 				last_len += ret << PAGE_CACHE_SHIFT;
1228008873eaSLi Zefan 			} else {
1229940100a4SChris Mason 				i++;
1230008873eaSLi Zefan 				last_len = 0;
1231008873eaSLi Zefan 			}
1232f46b5a66SChristoph Hellwig 		}
12334cb5300bSChris Mason 	}
1234f46b5a66SChristoph Hellwig 
12351e701a32SChris Mason 	if ((range->flags & BTRFS_DEFRAG_RANGE_START_IO))
12361e701a32SChris Mason 		filemap_flush(inode->i_mapping);
12371e701a32SChris Mason 
12381e701a32SChris Mason 	if ((range->flags & BTRFS_DEFRAG_RANGE_COMPRESS)) {
12391e701a32SChris Mason 		/* the filemap_flush will queue IO into the worker threads, but
12401e701a32SChris Mason 		 * we have to make sure the IO is actually started and that
12411e701a32SChris Mason 		 * ordered extents get created before we return
12421e701a32SChris Mason 		 */
12431e701a32SChris Mason 		atomic_inc(&root->fs_info->async_submit_draining);
12441e701a32SChris Mason 		while (atomic_read(&root->fs_info->nr_async_submits) ||
12451e701a32SChris Mason 		      atomic_read(&root->fs_info->async_delalloc_pages)) {
12461e701a32SChris Mason 			wait_event(root->fs_info->async_submit_wait,
12471e701a32SChris Mason 			   (atomic_read(&root->fs_info->nr_async_submits) == 0 &&
12481e701a32SChris Mason 			    atomic_read(&root->fs_info->async_delalloc_pages) == 0));
12491e701a32SChris Mason 		}
12501e701a32SChris Mason 		atomic_dec(&root->fs_info->async_submit_draining);
12511e701a32SChris Mason 
12521e701a32SChris Mason 		mutex_lock(&inode->i_mutex);
1253261507a0SLi Zefan 		BTRFS_I(inode)->force_compress = BTRFS_COMPRESS_NONE;
12541e701a32SChris Mason 		mutex_unlock(&inode->i_mutex);
12551e701a32SChris Mason 	}
12561e701a32SChris Mason 
12571a419d85SLi Zefan 	if (range->compress_type == BTRFS_COMPRESS_LZO) {
12582b0ce2c2SMitch Harder 		btrfs_set_fs_incompat(root->fs_info, COMPRESS_LZO);
12591a419d85SLi Zefan 	}
12601a419d85SLi Zefan 
126160ccf82fSDiego Calleja 	ret = defrag_count;
1262940100a4SChris Mason 
12634cb5300bSChris Mason out_ra:
12644cb5300bSChris Mason 	if (!file)
12654cb5300bSChris Mason 		kfree(ra);
12664cb5300bSChris Mason 	kfree(pages);
1267940100a4SChris Mason 	return ret;
1268f46b5a66SChristoph Hellwig }
1269f46b5a66SChristoph Hellwig 
127076dda93cSYan, Zheng static noinline int btrfs_ioctl_resize(struct btrfs_root *root,
127176dda93cSYan, Zheng 					void __user *arg)
1272f46b5a66SChristoph Hellwig {
1273f46b5a66SChristoph Hellwig 	u64 new_size;
1274f46b5a66SChristoph Hellwig 	u64 old_size;
1275f46b5a66SChristoph Hellwig 	u64 devid = 1;
1276f46b5a66SChristoph Hellwig 	struct btrfs_ioctl_vol_args *vol_args;
1277f46b5a66SChristoph Hellwig 	struct btrfs_trans_handle *trans;
1278f46b5a66SChristoph Hellwig 	struct btrfs_device *device = NULL;
1279f46b5a66SChristoph Hellwig 	char *sizestr;
1280f46b5a66SChristoph Hellwig 	char *devstr = NULL;
1281f46b5a66SChristoph Hellwig 	int ret = 0;
1282f46b5a66SChristoph Hellwig 	int mod = 0;
1283f46b5a66SChristoph Hellwig 
1284c146afadSYan Zheng 	if (root->fs_info->sb->s_flags & MS_RDONLY)
1285c146afadSYan Zheng 		return -EROFS;
1286c146afadSYan Zheng 
1287e441d54dSChris Mason 	if (!capable(CAP_SYS_ADMIN))
1288e441d54dSChris Mason 		return -EPERM;
1289e441d54dSChris Mason 
1290c9e9f97bSIlya Dryomov 	mutex_lock(&root->fs_info->volume_mutex);
1291c9e9f97bSIlya Dryomov 	if (root->fs_info->balance_ctl) {
1292c9e9f97bSIlya Dryomov 		printk(KERN_INFO "btrfs: balance in progress\n");
1293c9e9f97bSIlya Dryomov 		ret = -EINVAL;
1294c9e9f97bSIlya Dryomov 		goto out;
1295c9e9f97bSIlya Dryomov 	}
1296c9e9f97bSIlya Dryomov 
1297dae7b665SLi Zefan 	vol_args = memdup_user(arg, sizeof(*vol_args));
1298c9e9f97bSIlya Dryomov 	if (IS_ERR(vol_args)) {
1299c9e9f97bSIlya Dryomov 		ret = PTR_ERR(vol_args);
1300c9e9f97bSIlya Dryomov 		goto out;
1301c9e9f97bSIlya Dryomov 	}
13025516e595SMark Fasheh 
13035516e595SMark Fasheh 	vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
1304f46b5a66SChristoph Hellwig 
1305f46b5a66SChristoph Hellwig 	sizestr = vol_args->name;
1306f46b5a66SChristoph Hellwig 	devstr = strchr(sizestr, ':');
1307f46b5a66SChristoph Hellwig 	if (devstr) {
1308f46b5a66SChristoph Hellwig 		char *end;
1309f46b5a66SChristoph Hellwig 		sizestr = devstr + 1;
1310f46b5a66SChristoph Hellwig 		*devstr = '\0';
1311f46b5a66SChristoph Hellwig 		devstr = vol_args->name;
1312f46b5a66SChristoph Hellwig 		devid = simple_strtoull(devstr, &end, 10);
13135bb14682SArnd Hannemann 		printk(KERN_INFO "btrfs: resizing devid %llu\n",
131421380931SJoel Becker 		       (unsigned long long)devid);
1315f46b5a66SChristoph Hellwig 	}
13162b82032cSYan Zheng 	device = btrfs_find_device(root, devid, NULL, NULL);
1317f46b5a66SChristoph Hellwig 	if (!device) {
13185bb14682SArnd Hannemann 		printk(KERN_INFO "btrfs: resizer unable to find device %llu\n",
131921380931SJoel Becker 		       (unsigned long long)devid);
1320f46b5a66SChristoph Hellwig 		ret = -EINVAL;
1321c9e9f97bSIlya Dryomov 		goto out_free;
1322f46b5a66SChristoph Hellwig 	}
13234e42ae1bSLiu Bo 	if (device->fs_devices && device->fs_devices->seeding) {
13244e42ae1bSLiu Bo 		printk(KERN_INFO "btrfs: resizer unable to apply on "
1325a8c4a33bSChris Mason 		       "seeding device %llu\n",
1326a8c4a33bSChris Mason 		       (unsigned long long)devid);
13274e42ae1bSLiu Bo 		ret = -EINVAL;
13284e42ae1bSLiu Bo 		goto out_free;
13294e42ae1bSLiu Bo 	}
13304e42ae1bSLiu Bo 
1331f46b5a66SChristoph Hellwig 	if (!strcmp(sizestr, "max"))
1332f46b5a66SChristoph Hellwig 		new_size = device->bdev->bd_inode->i_size;
1333f46b5a66SChristoph Hellwig 	else {
1334f46b5a66SChristoph Hellwig 		if (sizestr[0] == '-') {
1335f46b5a66SChristoph Hellwig 			mod = -1;
1336f46b5a66SChristoph Hellwig 			sizestr++;
1337f46b5a66SChristoph Hellwig 		} else if (sizestr[0] == '+') {
1338f46b5a66SChristoph Hellwig 			mod = 1;
1339f46b5a66SChristoph Hellwig 			sizestr++;
1340f46b5a66SChristoph Hellwig 		}
134191748467SAkinobu Mita 		new_size = memparse(sizestr, NULL);
1342f46b5a66SChristoph Hellwig 		if (new_size == 0) {
1343f46b5a66SChristoph Hellwig 			ret = -EINVAL;
1344c9e9f97bSIlya Dryomov 			goto out_free;
1345f46b5a66SChristoph Hellwig 		}
1346f46b5a66SChristoph Hellwig 	}
1347f46b5a66SChristoph Hellwig 
1348f46b5a66SChristoph Hellwig 	old_size = device->total_bytes;
1349f46b5a66SChristoph Hellwig 
1350f46b5a66SChristoph Hellwig 	if (mod < 0) {
1351f46b5a66SChristoph Hellwig 		if (new_size > old_size) {
1352f46b5a66SChristoph Hellwig 			ret = -EINVAL;
1353c9e9f97bSIlya Dryomov 			goto out_free;
1354f46b5a66SChristoph Hellwig 		}
1355f46b5a66SChristoph Hellwig 		new_size = old_size - new_size;
1356f46b5a66SChristoph Hellwig 	} else if (mod > 0) {
1357f46b5a66SChristoph Hellwig 		new_size = old_size + new_size;
1358f46b5a66SChristoph Hellwig 	}
1359f46b5a66SChristoph Hellwig 
1360f46b5a66SChristoph Hellwig 	if (new_size < 256 * 1024 * 1024) {
1361f46b5a66SChristoph Hellwig 		ret = -EINVAL;
1362c9e9f97bSIlya Dryomov 		goto out_free;
1363f46b5a66SChristoph Hellwig 	}
1364f46b5a66SChristoph Hellwig 	if (new_size > device->bdev->bd_inode->i_size) {
1365f46b5a66SChristoph Hellwig 		ret = -EFBIG;
1366c9e9f97bSIlya Dryomov 		goto out_free;
1367f46b5a66SChristoph Hellwig 	}
1368f46b5a66SChristoph Hellwig 
1369f46b5a66SChristoph Hellwig 	do_div(new_size, root->sectorsize);
1370f46b5a66SChristoph Hellwig 	new_size *= root->sectorsize;
1371f46b5a66SChristoph Hellwig 
1372606686eeSJosef Bacik 	printk_in_rcu(KERN_INFO "btrfs: new size for %s is %llu\n",
1373606686eeSJosef Bacik 		      rcu_str_deref(device->name),
1374606686eeSJosef Bacik 		      (unsigned long long)new_size);
1375f46b5a66SChristoph Hellwig 
1376f46b5a66SChristoph Hellwig 	if (new_size > old_size) {
1377a22285a6SYan, Zheng 		trans = btrfs_start_transaction(root, 0);
137898d5dc13STsutomu Itoh 		if (IS_ERR(trans)) {
137998d5dc13STsutomu Itoh 			ret = PTR_ERR(trans);
1380c9e9f97bSIlya Dryomov 			goto out_free;
138198d5dc13STsutomu Itoh 		}
1382f46b5a66SChristoph Hellwig 		ret = btrfs_grow_device(trans, device, new_size);
1383f46b5a66SChristoph Hellwig 		btrfs_commit_transaction(trans, root);
1384ece7d20eSMike Fleetwood 	} else if (new_size < old_size) {
1385f46b5a66SChristoph Hellwig 		ret = btrfs_shrink_device(device, new_size);
1386f46b5a66SChristoph Hellwig 	}
1387f46b5a66SChristoph Hellwig 
1388c9e9f97bSIlya Dryomov out_free:
1389f46b5a66SChristoph Hellwig 	kfree(vol_args);
1390c9e9f97bSIlya Dryomov out:
1391c9e9f97bSIlya Dryomov 	mutex_unlock(&root->fs_info->volume_mutex);
1392f46b5a66SChristoph Hellwig 	return ret;
1393f46b5a66SChristoph Hellwig }
1394f46b5a66SChristoph Hellwig 
139572fd032eSSage Weil static noinline int btrfs_ioctl_snap_create_transid(struct file *file,
13966f72c7e2SArne Jansen 				char *name, unsigned long fd, int subvol,
13976f72c7e2SArne Jansen 				u64 *transid, bool readonly,
13986f72c7e2SArne Jansen 				struct btrfs_qgroup_inherit **inherit)
1399f46b5a66SChristoph Hellwig {
14003de4586cSChris Mason 	struct file *src_file;
1401f46b5a66SChristoph Hellwig 	int namelen;
14023de4586cSChris Mason 	int ret = 0;
1403f46b5a66SChristoph Hellwig 
1404a874a63eSLiu Bo 	ret = mnt_want_write_file(file);
1405a874a63eSLiu Bo 	if (ret)
1406a874a63eSLiu Bo 		goto out;
1407a874a63eSLiu Bo 
140872fd032eSSage Weil 	namelen = strlen(name);
140972fd032eSSage Weil 	if (strchr(name, '/')) {
1410f46b5a66SChristoph Hellwig 		ret = -EINVAL;
1411a874a63eSLiu Bo 		goto out_drop_write;
1412f46b5a66SChristoph Hellwig 	}
1413f46b5a66SChristoph Hellwig 
141416780cabSChris Mason 	if (name[0] == '.' &&
141516780cabSChris Mason 	   (namelen == 1 || (name[1] == '.' && namelen == 2))) {
141616780cabSChris Mason 		ret = -EEXIST;
1417a874a63eSLiu Bo 		goto out_drop_write;
141816780cabSChris Mason 	}
141916780cabSChris Mason 
14203de4586cSChris Mason 	if (subvol) {
142172fd032eSSage Weil 		ret = btrfs_mksubvol(&file->f_path, name, namelen,
14226f72c7e2SArne Jansen 				     NULL, transid, readonly, inherit);
1423cb8e7090SChristoph Hellwig 	} else {
14243de4586cSChris Mason 		struct inode *src_inode;
1425ecd18815SAl Viro 		int fput_needed;
1426ecd18815SAl Viro 		src_file = fget_light(fd, &fput_needed);
14273de4586cSChris Mason 		if (!src_file) {
14283de4586cSChris Mason 			ret = -EINVAL;
1429a874a63eSLiu Bo 			goto out_drop_write;
14303de4586cSChris Mason 		}
14313de4586cSChris Mason 
14323de4586cSChris Mason 		src_inode = src_file->f_path.dentry->d_inode;
14333de4586cSChris Mason 		if (src_inode->i_sb != file->f_path.dentry->d_inode->i_sb) {
1434d397712bSChris Mason 			printk(KERN_INFO "btrfs: Snapshot src from "
1435d397712bSChris Mason 			       "another FS\n");
14363de4586cSChris Mason 			ret = -EINVAL;
1437ecd18815SAl Viro 		} else {
143872fd032eSSage Weil 			ret = btrfs_mksubvol(&file->f_path, name, namelen,
143972fd032eSSage Weil 					     BTRFS_I(src_inode)->root,
14406f72c7e2SArne Jansen 					     transid, readonly, inherit);
1441ecd18815SAl Viro 		}
1442ecd18815SAl Viro 		fput_light(src_file, fput_needed);
1443cb8e7090SChristoph Hellwig 	}
1444a874a63eSLiu Bo out_drop_write:
1445a874a63eSLiu Bo 	mnt_drop_write_file(file);
1446f46b5a66SChristoph Hellwig out:
144772fd032eSSage Weil 	return ret;
144872fd032eSSage Weil }
144972fd032eSSage Weil 
145072fd032eSSage Weil static noinline int btrfs_ioctl_snap_create(struct file *file,
1451fa0d2b9bSLi Zefan 					    void __user *arg, int subvol)
145272fd032eSSage Weil {
1453fa0d2b9bSLi Zefan 	struct btrfs_ioctl_vol_args *vol_args;
145472fd032eSSage Weil 	int ret;
145572fd032eSSage Weil 
1456fa0d2b9bSLi Zefan 	vol_args = memdup_user(arg, sizeof(*vol_args));
1457fa0d2b9bSLi Zefan 	if (IS_ERR(vol_args))
1458fa0d2b9bSLi Zefan 		return PTR_ERR(vol_args);
1459fa0d2b9bSLi Zefan 	vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
1460fa0d2b9bSLi Zefan 
1461fa0d2b9bSLi Zefan 	ret = btrfs_ioctl_snap_create_transid(file, vol_args->name,
1462b83cc969SLi Zefan 					      vol_args->fd, subvol,
14636f72c7e2SArne Jansen 					      NULL, false, NULL);
1464fa0d2b9bSLi Zefan 
1465fa0d2b9bSLi Zefan 	kfree(vol_args);
1466fa0d2b9bSLi Zefan 	return ret;
1467fa0d2b9bSLi Zefan }
1468fa0d2b9bSLi Zefan 
1469fa0d2b9bSLi Zefan static noinline int btrfs_ioctl_snap_create_v2(struct file *file,
1470fa0d2b9bSLi Zefan 					       void __user *arg, int subvol)
1471fa0d2b9bSLi Zefan {
1472fa0d2b9bSLi Zefan 	struct btrfs_ioctl_vol_args_v2 *vol_args;
1473fa0d2b9bSLi Zefan 	int ret;
1474fdfb1e4fSLi Zefan 	u64 transid = 0;
1475fdfb1e4fSLi Zefan 	u64 *ptr = NULL;
1476b83cc969SLi Zefan 	bool readonly = false;
14776f72c7e2SArne Jansen 	struct btrfs_qgroup_inherit *inherit = NULL;
147872fd032eSSage Weil 
1479fa0d2b9bSLi Zefan 	vol_args = memdup_user(arg, sizeof(*vol_args));
1480fa0d2b9bSLi Zefan 	if (IS_ERR(vol_args))
1481fa0d2b9bSLi Zefan 		return PTR_ERR(vol_args);
1482fa0d2b9bSLi Zefan 	vol_args->name[BTRFS_SUBVOL_NAME_MAX] = '\0';
1483fdfb1e4fSLi Zefan 
1484b83cc969SLi Zefan 	if (vol_args->flags &
14856f72c7e2SArne Jansen 	    ~(BTRFS_SUBVOL_CREATE_ASYNC | BTRFS_SUBVOL_RDONLY |
14866f72c7e2SArne Jansen 	      BTRFS_SUBVOL_QGROUP_INHERIT)) {
1487b83cc969SLi Zefan 		ret = -EOPNOTSUPP;
1488fdfb1e4fSLi Zefan 		goto out;
1489fdfb1e4fSLi Zefan 	}
1490fdfb1e4fSLi Zefan 
1491fa0d2b9bSLi Zefan 	if (vol_args->flags & BTRFS_SUBVOL_CREATE_ASYNC)
1492fdfb1e4fSLi Zefan 		ptr = &transid;
1493b83cc969SLi Zefan 	if (vol_args->flags & BTRFS_SUBVOL_RDONLY)
1494b83cc969SLi Zefan 		readonly = true;
14956f72c7e2SArne Jansen 	if (vol_args->flags & BTRFS_SUBVOL_QGROUP_INHERIT) {
14966f72c7e2SArne Jansen 		if (vol_args->size > PAGE_CACHE_SIZE) {
14976f72c7e2SArne Jansen 			ret = -EINVAL;
14986f72c7e2SArne Jansen 			goto out;
14996f72c7e2SArne Jansen 		}
15006f72c7e2SArne Jansen 		inherit = memdup_user(vol_args->qgroup_inherit, vol_args->size);
15016f72c7e2SArne Jansen 		if (IS_ERR(inherit)) {
15026f72c7e2SArne Jansen 			ret = PTR_ERR(inherit);
15036f72c7e2SArne Jansen 			goto out;
15046f72c7e2SArne Jansen 		}
15056f72c7e2SArne Jansen 	}
150675eaa0e2SSage Weil 
1507fa0d2b9bSLi Zefan 	ret = btrfs_ioctl_snap_create_transid(file, vol_args->name,
15086f72c7e2SArne Jansen 					      vol_args->fd, subvol, ptr,
15096f72c7e2SArne Jansen 					      readonly, &inherit);
151075eaa0e2SSage Weil 
1511fdfb1e4fSLi Zefan 	if (ret == 0 && ptr &&
151275eaa0e2SSage Weil 	    copy_to_user(arg +
1513fdfb1e4fSLi Zefan 			 offsetof(struct btrfs_ioctl_vol_args_v2,
1514fdfb1e4fSLi Zefan 				  transid), ptr, sizeof(*ptr)))
151575eaa0e2SSage Weil 		ret = -EFAULT;
1516fdfb1e4fSLi Zefan out:
1517f46b5a66SChristoph Hellwig 	kfree(vol_args);
15186f72c7e2SArne Jansen 	kfree(inherit);
1519f46b5a66SChristoph Hellwig 	return ret;
1520f46b5a66SChristoph Hellwig }
1521f46b5a66SChristoph Hellwig 
15220caa102dSLi Zefan static noinline int btrfs_ioctl_subvol_getflags(struct file *file,
15230caa102dSLi Zefan 						void __user *arg)
15240caa102dSLi Zefan {
15250caa102dSLi Zefan 	struct inode *inode = fdentry(file)->d_inode;
15260caa102dSLi Zefan 	struct btrfs_root *root = BTRFS_I(inode)->root;
15270caa102dSLi Zefan 	int ret = 0;
15280caa102dSLi Zefan 	u64 flags = 0;
15290caa102dSLi Zefan 
153033345d01SLi Zefan 	if (btrfs_ino(inode) != BTRFS_FIRST_FREE_OBJECTID)
15310caa102dSLi Zefan 		return -EINVAL;
15320caa102dSLi Zefan 
15330caa102dSLi Zefan 	down_read(&root->fs_info->subvol_sem);
15340caa102dSLi Zefan 	if (btrfs_root_readonly(root))
15350caa102dSLi Zefan 		flags |= BTRFS_SUBVOL_RDONLY;
15360caa102dSLi Zefan 	up_read(&root->fs_info->subvol_sem);
15370caa102dSLi Zefan 
15380caa102dSLi Zefan 	if (copy_to_user(arg, &flags, sizeof(flags)))
15390caa102dSLi Zefan 		ret = -EFAULT;
15400caa102dSLi Zefan 
15410caa102dSLi Zefan 	return ret;
15420caa102dSLi Zefan }
15430caa102dSLi Zefan 
15440caa102dSLi Zefan static noinline int btrfs_ioctl_subvol_setflags(struct file *file,
15450caa102dSLi Zefan 					      void __user *arg)
15460caa102dSLi Zefan {
15470caa102dSLi Zefan 	struct inode *inode = fdentry(file)->d_inode;
15480caa102dSLi Zefan 	struct btrfs_root *root = BTRFS_I(inode)->root;
15490caa102dSLi Zefan 	struct btrfs_trans_handle *trans;
15500caa102dSLi Zefan 	u64 root_flags;
15510caa102dSLi Zefan 	u64 flags;
15520caa102dSLi Zefan 	int ret = 0;
15530caa102dSLi Zefan 
1554b9ca0664SLiu Bo 	ret = mnt_want_write_file(file);
1555b9ca0664SLiu Bo 	if (ret)
1556b9ca0664SLiu Bo 		goto out;
15570caa102dSLi Zefan 
1558b9ca0664SLiu Bo 	if (btrfs_ino(inode) != BTRFS_FIRST_FREE_OBJECTID) {
1559b9ca0664SLiu Bo 		ret = -EINVAL;
1560b9ca0664SLiu Bo 		goto out_drop_write;
1561b9ca0664SLiu Bo 	}
15620caa102dSLi Zefan 
1563b9ca0664SLiu Bo 	if (copy_from_user(&flags, arg, sizeof(flags))) {
1564b9ca0664SLiu Bo 		ret = -EFAULT;
1565b9ca0664SLiu Bo 		goto out_drop_write;
1566b9ca0664SLiu Bo 	}
15670caa102dSLi Zefan 
1568b9ca0664SLiu Bo 	if (flags & BTRFS_SUBVOL_CREATE_ASYNC) {
1569b9ca0664SLiu Bo 		ret = -EINVAL;
1570b9ca0664SLiu Bo 		goto out_drop_write;
1571b9ca0664SLiu Bo 	}
15720caa102dSLi Zefan 
1573b9ca0664SLiu Bo 	if (flags & ~BTRFS_SUBVOL_RDONLY) {
1574b9ca0664SLiu Bo 		ret = -EOPNOTSUPP;
1575b9ca0664SLiu Bo 		goto out_drop_write;
1576b9ca0664SLiu Bo 	}
15770caa102dSLi Zefan 
1578b9ca0664SLiu Bo 	if (!inode_owner_or_capable(inode)) {
1579b9ca0664SLiu Bo 		ret = -EACCES;
1580b9ca0664SLiu Bo 		goto out_drop_write;
1581b9ca0664SLiu Bo 	}
1582b4dc2b8cSLi Zefan 
15830caa102dSLi Zefan 	down_write(&root->fs_info->subvol_sem);
15840caa102dSLi Zefan 
15850caa102dSLi Zefan 	/* nothing to do */
15860caa102dSLi Zefan 	if (!!(flags & BTRFS_SUBVOL_RDONLY) == btrfs_root_readonly(root))
1587b9ca0664SLiu Bo 		goto out_drop_sem;
15880caa102dSLi Zefan 
15890caa102dSLi Zefan 	root_flags = btrfs_root_flags(&root->root_item);
15900caa102dSLi Zefan 	if (flags & BTRFS_SUBVOL_RDONLY)
15910caa102dSLi Zefan 		btrfs_set_root_flags(&root->root_item,
15920caa102dSLi Zefan 				     root_flags | BTRFS_ROOT_SUBVOL_RDONLY);
15930caa102dSLi Zefan 	else
15940caa102dSLi Zefan 		btrfs_set_root_flags(&root->root_item,
15950caa102dSLi Zefan 				     root_flags & ~BTRFS_ROOT_SUBVOL_RDONLY);
15960caa102dSLi Zefan 
15970caa102dSLi Zefan 	trans = btrfs_start_transaction(root, 1);
15980caa102dSLi Zefan 	if (IS_ERR(trans)) {
15990caa102dSLi Zefan 		ret = PTR_ERR(trans);
16000caa102dSLi Zefan 		goto out_reset;
16010caa102dSLi Zefan 	}
16020caa102dSLi Zefan 
1603b4dc2b8cSLi Zefan 	ret = btrfs_update_root(trans, root->fs_info->tree_root,
16040caa102dSLi Zefan 				&root->root_key, &root->root_item);
16050caa102dSLi Zefan 
16060caa102dSLi Zefan 	btrfs_commit_transaction(trans, root);
16070caa102dSLi Zefan out_reset:
16080caa102dSLi Zefan 	if (ret)
16090caa102dSLi Zefan 		btrfs_set_root_flags(&root->root_item, root_flags);
1610b9ca0664SLiu Bo out_drop_sem:
16110caa102dSLi Zefan 	up_write(&root->fs_info->subvol_sem);
1612b9ca0664SLiu Bo out_drop_write:
1613b9ca0664SLiu Bo 	mnt_drop_write_file(file);
1614b9ca0664SLiu Bo out:
16150caa102dSLi Zefan 	return ret;
16160caa102dSLi Zefan }
16170caa102dSLi Zefan 
161876dda93cSYan, Zheng /*
161976dda93cSYan, Zheng  * helper to check if the subvolume references other subvolumes
162076dda93cSYan, Zheng  */
162176dda93cSYan, Zheng static noinline int may_destroy_subvol(struct btrfs_root *root)
162276dda93cSYan, Zheng {
162376dda93cSYan, Zheng 	struct btrfs_path *path;
162476dda93cSYan, Zheng 	struct btrfs_key key;
162576dda93cSYan, Zheng 	int ret;
162676dda93cSYan, Zheng 
162776dda93cSYan, Zheng 	path = btrfs_alloc_path();
162876dda93cSYan, Zheng 	if (!path)
162976dda93cSYan, Zheng 		return -ENOMEM;
163076dda93cSYan, Zheng 
163176dda93cSYan, Zheng 	key.objectid = root->root_key.objectid;
163276dda93cSYan, Zheng 	key.type = BTRFS_ROOT_REF_KEY;
163376dda93cSYan, Zheng 	key.offset = (u64)-1;
163476dda93cSYan, Zheng 
163576dda93cSYan, Zheng 	ret = btrfs_search_slot(NULL, root->fs_info->tree_root,
163676dda93cSYan, Zheng 				&key, path, 0, 0);
163776dda93cSYan, Zheng 	if (ret < 0)
163876dda93cSYan, Zheng 		goto out;
163976dda93cSYan, Zheng 	BUG_ON(ret == 0);
164076dda93cSYan, Zheng 
164176dda93cSYan, Zheng 	ret = 0;
164276dda93cSYan, Zheng 	if (path->slots[0] > 0) {
164376dda93cSYan, Zheng 		path->slots[0]--;
164476dda93cSYan, Zheng 		btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
164576dda93cSYan, Zheng 		if (key.objectid == root->root_key.objectid &&
164676dda93cSYan, Zheng 		    key.type == BTRFS_ROOT_REF_KEY)
164776dda93cSYan, Zheng 			ret = -ENOTEMPTY;
164876dda93cSYan, Zheng 	}
164976dda93cSYan, Zheng out:
165076dda93cSYan, Zheng 	btrfs_free_path(path);
165176dda93cSYan, Zheng 	return ret;
165276dda93cSYan, Zheng }
165376dda93cSYan, Zheng 
1654ac8e9819SChris Mason static noinline int key_in_sk(struct btrfs_key *key,
1655ac8e9819SChris Mason 			      struct btrfs_ioctl_search_key *sk)
1656ac8e9819SChris Mason {
1657abc6e134SChris Mason 	struct btrfs_key test;
1658abc6e134SChris Mason 	int ret;
1659abc6e134SChris Mason 
1660abc6e134SChris Mason 	test.objectid = sk->min_objectid;
1661abc6e134SChris Mason 	test.type = sk->min_type;
1662abc6e134SChris Mason 	test.offset = sk->min_offset;
1663abc6e134SChris Mason 
1664abc6e134SChris Mason 	ret = btrfs_comp_cpu_keys(key, &test);
1665abc6e134SChris Mason 	if (ret < 0)
1666ac8e9819SChris Mason 		return 0;
1667abc6e134SChris Mason 
1668abc6e134SChris Mason 	test.objectid = sk->max_objectid;
1669abc6e134SChris Mason 	test.type = sk->max_type;
1670abc6e134SChris Mason 	test.offset = sk->max_offset;
1671abc6e134SChris Mason 
1672abc6e134SChris Mason 	ret = btrfs_comp_cpu_keys(key, &test);
1673abc6e134SChris Mason 	if (ret > 0)
1674ac8e9819SChris Mason 		return 0;
1675ac8e9819SChris Mason 	return 1;
1676ac8e9819SChris Mason }
1677ac8e9819SChris Mason 
1678ac8e9819SChris Mason static noinline int copy_to_sk(struct btrfs_root *root,
1679ac8e9819SChris Mason 			       struct btrfs_path *path,
1680ac8e9819SChris Mason 			       struct btrfs_key *key,
1681ac8e9819SChris Mason 			       struct btrfs_ioctl_search_key *sk,
1682ac8e9819SChris Mason 			       char *buf,
1683ac8e9819SChris Mason 			       unsigned long *sk_offset,
1684ac8e9819SChris Mason 			       int *num_found)
1685ac8e9819SChris Mason {
1686ac8e9819SChris Mason 	u64 found_transid;
1687ac8e9819SChris Mason 	struct extent_buffer *leaf;
1688ac8e9819SChris Mason 	struct btrfs_ioctl_search_header sh;
1689ac8e9819SChris Mason 	unsigned long item_off;
1690ac8e9819SChris Mason 	unsigned long item_len;
1691ac8e9819SChris Mason 	int nritems;
1692ac8e9819SChris Mason 	int i;
1693ac8e9819SChris Mason 	int slot;
1694ac8e9819SChris Mason 	int ret = 0;
1695ac8e9819SChris Mason 
1696ac8e9819SChris Mason 	leaf = path->nodes[0];
1697ac8e9819SChris Mason 	slot = path->slots[0];
1698ac8e9819SChris Mason 	nritems = btrfs_header_nritems(leaf);
1699ac8e9819SChris Mason 
1700ac8e9819SChris Mason 	if (btrfs_header_generation(leaf) > sk->max_transid) {
1701ac8e9819SChris Mason 		i = nritems;
1702ac8e9819SChris Mason 		goto advance_key;
1703ac8e9819SChris Mason 	}
1704ac8e9819SChris Mason 	found_transid = btrfs_header_generation(leaf);
1705ac8e9819SChris Mason 
1706ac8e9819SChris Mason 	for (i = slot; i < nritems; i++) {
1707ac8e9819SChris Mason 		item_off = btrfs_item_ptr_offset(leaf, i);
1708ac8e9819SChris Mason 		item_len = btrfs_item_size_nr(leaf, i);
1709ac8e9819SChris Mason 
1710ac8e9819SChris Mason 		if (item_len > BTRFS_SEARCH_ARGS_BUFSIZE)
1711ac8e9819SChris Mason 			item_len = 0;
1712ac8e9819SChris Mason 
1713ac8e9819SChris Mason 		if (sizeof(sh) + item_len + *sk_offset >
1714ac8e9819SChris Mason 		    BTRFS_SEARCH_ARGS_BUFSIZE) {
1715ac8e9819SChris Mason 			ret = 1;
1716ac8e9819SChris Mason 			goto overflow;
1717ac8e9819SChris Mason 		}
1718ac8e9819SChris Mason 
1719ac8e9819SChris Mason 		btrfs_item_key_to_cpu(leaf, key, i);
1720ac8e9819SChris Mason 		if (!key_in_sk(key, sk))
1721ac8e9819SChris Mason 			continue;
1722ac8e9819SChris Mason 
1723ac8e9819SChris Mason 		sh.objectid = key->objectid;
1724ac8e9819SChris Mason 		sh.offset = key->offset;
1725ac8e9819SChris Mason 		sh.type = key->type;
1726ac8e9819SChris Mason 		sh.len = item_len;
1727ac8e9819SChris Mason 		sh.transid = found_transid;
1728ac8e9819SChris Mason 
1729ac8e9819SChris Mason 		/* copy search result header */
1730ac8e9819SChris Mason 		memcpy(buf + *sk_offset, &sh, sizeof(sh));
1731ac8e9819SChris Mason 		*sk_offset += sizeof(sh);
1732ac8e9819SChris Mason 
1733ac8e9819SChris Mason 		if (item_len) {
1734ac8e9819SChris Mason 			char *p = buf + *sk_offset;
1735ac8e9819SChris Mason 			/* copy the item */
1736ac8e9819SChris Mason 			read_extent_buffer(leaf, p,
1737ac8e9819SChris Mason 					   item_off, item_len);
1738ac8e9819SChris Mason 			*sk_offset += item_len;
1739ac8e9819SChris Mason 		}
1740e2156867SHugo Mills 		(*num_found)++;
1741ac8e9819SChris Mason 
1742ac8e9819SChris Mason 		if (*num_found >= sk->nr_items)
1743ac8e9819SChris Mason 			break;
1744ac8e9819SChris Mason 	}
1745ac8e9819SChris Mason advance_key:
1746ac8e9819SChris Mason 	ret = 0;
1747abc6e134SChris Mason 	if (key->offset < (u64)-1 && key->offset < sk->max_offset)
1748abc6e134SChris Mason 		key->offset++;
1749abc6e134SChris Mason 	else if (key->type < (u8)-1 && key->type < sk->max_type) {
1750abc6e134SChris Mason 		key->offset = 0;
1751abc6e134SChris Mason 		key->type++;
1752abc6e134SChris Mason 	} else if (key->objectid < (u64)-1 && key->objectid < sk->max_objectid) {
1753abc6e134SChris Mason 		key->offset = 0;
1754abc6e134SChris Mason 		key->type = 0;
1755abc6e134SChris Mason 		key->objectid++;
1756abc6e134SChris Mason 	} else
1757abc6e134SChris Mason 		ret = 1;
1758ac8e9819SChris Mason overflow:
1759ac8e9819SChris Mason 	return ret;
1760ac8e9819SChris Mason }
1761ac8e9819SChris Mason 
1762ac8e9819SChris Mason static noinline int search_ioctl(struct inode *inode,
1763ac8e9819SChris Mason 				 struct btrfs_ioctl_search_args *args)
1764ac8e9819SChris Mason {
1765ac8e9819SChris Mason 	struct btrfs_root *root;
1766ac8e9819SChris Mason 	struct btrfs_key key;
1767ac8e9819SChris Mason 	struct btrfs_key max_key;
1768ac8e9819SChris Mason 	struct btrfs_path *path;
1769ac8e9819SChris Mason 	struct btrfs_ioctl_search_key *sk = &args->key;
1770ac8e9819SChris Mason 	struct btrfs_fs_info *info = BTRFS_I(inode)->root->fs_info;
1771ac8e9819SChris Mason 	int ret;
1772ac8e9819SChris Mason 	int num_found = 0;
1773ac8e9819SChris Mason 	unsigned long sk_offset = 0;
1774ac8e9819SChris Mason 
1775ac8e9819SChris Mason 	path = btrfs_alloc_path();
1776ac8e9819SChris Mason 	if (!path)
1777ac8e9819SChris Mason 		return -ENOMEM;
1778ac8e9819SChris Mason 
1779ac8e9819SChris Mason 	if (sk->tree_id == 0) {
1780ac8e9819SChris Mason 		/* search the root of the inode that was passed */
1781ac8e9819SChris Mason 		root = BTRFS_I(inode)->root;
1782ac8e9819SChris Mason 	} else {
1783ac8e9819SChris Mason 		key.objectid = sk->tree_id;
1784ac8e9819SChris Mason 		key.type = BTRFS_ROOT_ITEM_KEY;
1785ac8e9819SChris Mason 		key.offset = (u64)-1;
1786ac8e9819SChris Mason 		root = btrfs_read_fs_root_no_name(info, &key);
1787ac8e9819SChris Mason 		if (IS_ERR(root)) {
1788ac8e9819SChris Mason 			printk(KERN_ERR "could not find root %llu\n",
1789ac8e9819SChris Mason 			       sk->tree_id);
1790ac8e9819SChris Mason 			btrfs_free_path(path);
1791ac8e9819SChris Mason 			return -ENOENT;
1792ac8e9819SChris Mason 		}
1793ac8e9819SChris Mason 	}
1794ac8e9819SChris Mason 
1795ac8e9819SChris Mason 	key.objectid = sk->min_objectid;
1796ac8e9819SChris Mason 	key.type = sk->min_type;
1797ac8e9819SChris Mason 	key.offset = sk->min_offset;
1798ac8e9819SChris Mason 
1799ac8e9819SChris Mason 	max_key.objectid = sk->max_objectid;
1800ac8e9819SChris Mason 	max_key.type = sk->max_type;
1801ac8e9819SChris Mason 	max_key.offset = sk->max_offset;
1802ac8e9819SChris Mason 
1803ac8e9819SChris Mason 	path->keep_locks = 1;
1804ac8e9819SChris Mason 
1805ac8e9819SChris Mason 	while(1) {
1806ac8e9819SChris Mason 		ret = btrfs_search_forward(root, &key, &max_key, path, 0,
1807ac8e9819SChris Mason 					   sk->min_transid);
1808ac8e9819SChris Mason 		if (ret != 0) {
1809ac8e9819SChris Mason 			if (ret > 0)
1810ac8e9819SChris Mason 				ret = 0;
1811ac8e9819SChris Mason 			goto err;
1812ac8e9819SChris Mason 		}
1813ac8e9819SChris Mason 		ret = copy_to_sk(root, path, &key, sk, args->buf,
1814ac8e9819SChris Mason 				 &sk_offset, &num_found);
1815b3b4aa74SDavid Sterba 		btrfs_release_path(path);
1816ac8e9819SChris Mason 		if (ret || num_found >= sk->nr_items)
1817ac8e9819SChris Mason 			break;
1818ac8e9819SChris Mason 
1819ac8e9819SChris Mason 	}
1820ac8e9819SChris Mason 	ret = 0;
1821ac8e9819SChris Mason err:
1822ac8e9819SChris Mason 	sk->nr_items = num_found;
1823ac8e9819SChris Mason 	btrfs_free_path(path);
1824ac8e9819SChris Mason 	return ret;
1825ac8e9819SChris Mason }
1826ac8e9819SChris Mason 
1827ac8e9819SChris Mason static noinline int btrfs_ioctl_tree_search(struct file *file,
1828ac8e9819SChris Mason 					   void __user *argp)
1829ac8e9819SChris Mason {
1830ac8e9819SChris Mason 	 struct btrfs_ioctl_search_args *args;
1831ac8e9819SChris Mason 	 struct inode *inode;
1832ac8e9819SChris Mason 	 int ret;
1833ac8e9819SChris Mason 
1834ac8e9819SChris Mason 	if (!capable(CAP_SYS_ADMIN))
1835ac8e9819SChris Mason 		return -EPERM;
1836ac8e9819SChris Mason 
18372354d08fSJulia Lawall 	args = memdup_user(argp, sizeof(*args));
18382354d08fSJulia Lawall 	if (IS_ERR(args))
18392354d08fSJulia Lawall 		return PTR_ERR(args);
1840ac8e9819SChris Mason 
1841ac8e9819SChris Mason 	inode = fdentry(file)->d_inode;
1842ac8e9819SChris Mason 	ret = search_ioctl(inode, args);
1843ac8e9819SChris Mason 	if (ret == 0 && copy_to_user(argp, args, sizeof(*args)))
1844ac8e9819SChris Mason 		ret = -EFAULT;
1845ac8e9819SChris Mason 	kfree(args);
1846ac8e9819SChris Mason 	return ret;
1847ac8e9819SChris Mason }
1848ac8e9819SChris Mason 
184998d377a0STARUISI Hiroaki /*
1850ac8e9819SChris Mason  * Search INODE_REFs to identify path name of 'dirid' directory
1851ac8e9819SChris Mason  * in a 'tree_id' tree. and sets path name to 'name'.
185298d377a0STARUISI Hiroaki  */
185398d377a0STARUISI Hiroaki static noinline int btrfs_search_path_in_tree(struct btrfs_fs_info *info,
185498d377a0STARUISI Hiroaki 				u64 tree_id, u64 dirid, char *name)
185598d377a0STARUISI Hiroaki {
185698d377a0STARUISI Hiroaki 	struct btrfs_root *root;
185798d377a0STARUISI Hiroaki 	struct btrfs_key key;
1858ac8e9819SChris Mason 	char *ptr;
185998d377a0STARUISI Hiroaki 	int ret = -1;
186098d377a0STARUISI Hiroaki 	int slot;
186198d377a0STARUISI Hiroaki 	int len;
186298d377a0STARUISI Hiroaki 	int total_len = 0;
186398d377a0STARUISI Hiroaki 	struct btrfs_inode_ref *iref;
186498d377a0STARUISI Hiroaki 	struct extent_buffer *l;
186598d377a0STARUISI Hiroaki 	struct btrfs_path *path;
186698d377a0STARUISI Hiroaki 
186798d377a0STARUISI Hiroaki 	if (dirid == BTRFS_FIRST_FREE_OBJECTID) {
186898d377a0STARUISI Hiroaki 		name[0]='\0';
186998d377a0STARUISI Hiroaki 		return 0;
187098d377a0STARUISI Hiroaki 	}
187198d377a0STARUISI Hiroaki 
187298d377a0STARUISI Hiroaki 	path = btrfs_alloc_path();
187398d377a0STARUISI Hiroaki 	if (!path)
187498d377a0STARUISI Hiroaki 		return -ENOMEM;
187598d377a0STARUISI Hiroaki 
1876ac8e9819SChris Mason 	ptr = &name[BTRFS_INO_LOOKUP_PATH_MAX];
187798d377a0STARUISI Hiroaki 
187898d377a0STARUISI Hiroaki 	key.objectid = tree_id;
187998d377a0STARUISI Hiroaki 	key.type = BTRFS_ROOT_ITEM_KEY;
188098d377a0STARUISI Hiroaki 	key.offset = (u64)-1;
188198d377a0STARUISI Hiroaki 	root = btrfs_read_fs_root_no_name(info, &key);
188298d377a0STARUISI Hiroaki 	if (IS_ERR(root)) {
188398d377a0STARUISI Hiroaki 		printk(KERN_ERR "could not find root %llu\n", tree_id);
18848ad6fcabSChris Mason 		ret = -ENOENT;
18858ad6fcabSChris Mason 		goto out;
188698d377a0STARUISI Hiroaki 	}
188798d377a0STARUISI Hiroaki 
188898d377a0STARUISI Hiroaki 	key.objectid = dirid;
188998d377a0STARUISI Hiroaki 	key.type = BTRFS_INODE_REF_KEY;
18908ad6fcabSChris Mason 	key.offset = (u64)-1;
189198d377a0STARUISI Hiroaki 
189298d377a0STARUISI Hiroaki 	while(1) {
189398d377a0STARUISI Hiroaki 		ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
189498d377a0STARUISI Hiroaki 		if (ret < 0)
189598d377a0STARUISI Hiroaki 			goto out;
189698d377a0STARUISI Hiroaki 
189798d377a0STARUISI Hiroaki 		l = path->nodes[0];
189898d377a0STARUISI Hiroaki 		slot = path->slots[0];
18998ad6fcabSChris Mason 		if (ret > 0 && slot > 0)
19008ad6fcabSChris Mason 			slot--;
190198d377a0STARUISI Hiroaki 		btrfs_item_key_to_cpu(l, &key, slot);
190298d377a0STARUISI Hiroaki 
190398d377a0STARUISI Hiroaki 		if (ret > 0 && (key.objectid != dirid ||
1904ac8e9819SChris Mason 				key.type != BTRFS_INODE_REF_KEY)) {
1905ac8e9819SChris Mason 			ret = -ENOENT;
190698d377a0STARUISI Hiroaki 			goto out;
1907ac8e9819SChris Mason 		}
190898d377a0STARUISI Hiroaki 
190998d377a0STARUISI Hiroaki 		iref = btrfs_item_ptr(l, slot, struct btrfs_inode_ref);
191098d377a0STARUISI Hiroaki 		len = btrfs_inode_ref_name_len(l, iref);
191198d377a0STARUISI Hiroaki 		ptr -= len + 1;
191298d377a0STARUISI Hiroaki 		total_len += len + 1;
1913ac8e9819SChris Mason 		if (ptr < name)
191498d377a0STARUISI Hiroaki 			goto out;
191598d377a0STARUISI Hiroaki 
191698d377a0STARUISI Hiroaki 		*(ptr + len) = '/';
191798d377a0STARUISI Hiroaki 		read_extent_buffer(l, ptr,(unsigned long)(iref + 1), len);
191898d377a0STARUISI Hiroaki 
191998d377a0STARUISI Hiroaki 		if (key.offset == BTRFS_FIRST_FREE_OBJECTID)
192098d377a0STARUISI Hiroaki 			break;
192198d377a0STARUISI Hiroaki 
1922b3b4aa74SDavid Sterba 		btrfs_release_path(path);
192398d377a0STARUISI Hiroaki 		key.objectid = key.offset;
19248ad6fcabSChris Mason 		key.offset = (u64)-1;
192598d377a0STARUISI Hiroaki 		dirid = key.objectid;
192698d377a0STARUISI Hiroaki 	}
1927ac8e9819SChris Mason 	if (ptr < name)
192898d377a0STARUISI Hiroaki 		goto out;
192977906a50SLi Zefan 	memmove(name, ptr, total_len);
193098d377a0STARUISI Hiroaki 	name[total_len]='\0';
193198d377a0STARUISI Hiroaki 	ret = 0;
193298d377a0STARUISI Hiroaki out:
193398d377a0STARUISI Hiroaki 	btrfs_free_path(path);
1934ac8e9819SChris Mason 	return ret;
1935ac8e9819SChris Mason }
1936ac8e9819SChris Mason 
1937ac8e9819SChris Mason static noinline int btrfs_ioctl_ino_lookup(struct file *file,
1938ac8e9819SChris Mason 					   void __user *argp)
1939ac8e9819SChris Mason {
1940ac8e9819SChris Mason 	 struct btrfs_ioctl_ino_lookup_args *args;
1941ac8e9819SChris Mason 	 struct inode *inode;
1942ac8e9819SChris Mason 	 int ret;
1943ac8e9819SChris Mason 
1944ac8e9819SChris Mason 	if (!capable(CAP_SYS_ADMIN))
1945ac8e9819SChris Mason 		return -EPERM;
1946ac8e9819SChris Mason 
19472354d08fSJulia Lawall 	args = memdup_user(argp, sizeof(*args));
19482354d08fSJulia Lawall 	if (IS_ERR(args))
19492354d08fSJulia Lawall 		return PTR_ERR(args);
1950c2b96929SDan Carpenter 
1951ac8e9819SChris Mason 	inode = fdentry(file)->d_inode;
1952ac8e9819SChris Mason 
19531b53ac4dSChris Mason 	if (args->treeid == 0)
19541b53ac4dSChris Mason 		args->treeid = BTRFS_I(inode)->root->root_key.objectid;
19551b53ac4dSChris Mason 
1956ac8e9819SChris Mason 	ret = btrfs_search_path_in_tree(BTRFS_I(inode)->root->fs_info,
1957ac8e9819SChris Mason 					args->treeid, args->objectid,
1958ac8e9819SChris Mason 					args->name);
1959ac8e9819SChris Mason 
1960ac8e9819SChris Mason 	if (ret == 0 && copy_to_user(argp, args, sizeof(*args)))
1961ac8e9819SChris Mason 		ret = -EFAULT;
1962ac8e9819SChris Mason 
1963ac8e9819SChris Mason 	kfree(args);
196498d377a0STARUISI Hiroaki 	return ret;
196598d377a0STARUISI Hiroaki }
196698d377a0STARUISI Hiroaki 
196776dda93cSYan, Zheng static noinline int btrfs_ioctl_snap_destroy(struct file *file,
196876dda93cSYan, Zheng 					     void __user *arg)
196976dda93cSYan, Zheng {
197076dda93cSYan, Zheng 	struct dentry *parent = fdentry(file);
197176dda93cSYan, Zheng 	struct dentry *dentry;
197276dda93cSYan, Zheng 	struct inode *dir = parent->d_inode;
197376dda93cSYan, Zheng 	struct inode *inode;
197476dda93cSYan, Zheng 	struct btrfs_root *root = BTRFS_I(dir)->root;
197576dda93cSYan, Zheng 	struct btrfs_root *dest = NULL;
197676dda93cSYan, Zheng 	struct btrfs_ioctl_vol_args *vol_args;
197776dda93cSYan, Zheng 	struct btrfs_trans_handle *trans;
197876dda93cSYan, Zheng 	int namelen;
197976dda93cSYan, Zheng 	int ret;
198076dda93cSYan, Zheng 	int err = 0;
198176dda93cSYan, Zheng 
198276dda93cSYan, Zheng 	vol_args = memdup_user(arg, sizeof(*vol_args));
198376dda93cSYan, Zheng 	if (IS_ERR(vol_args))
198476dda93cSYan, Zheng 		return PTR_ERR(vol_args);
198576dda93cSYan, Zheng 
198676dda93cSYan, Zheng 	vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
198776dda93cSYan, Zheng 	namelen = strlen(vol_args->name);
198876dda93cSYan, Zheng 	if (strchr(vol_args->name, '/') ||
198976dda93cSYan, Zheng 	    strncmp(vol_args->name, "..", namelen) == 0) {
199076dda93cSYan, Zheng 		err = -EINVAL;
199176dda93cSYan, Zheng 		goto out;
199276dda93cSYan, Zheng 	}
199376dda93cSYan, Zheng 
1994a561be71SAl Viro 	err = mnt_want_write_file(file);
199576dda93cSYan, Zheng 	if (err)
199676dda93cSYan, Zheng 		goto out;
199776dda93cSYan, Zheng 
199876dda93cSYan, Zheng 	mutex_lock_nested(&dir->i_mutex, I_MUTEX_PARENT);
199976dda93cSYan, Zheng 	dentry = lookup_one_len(vol_args->name, parent, namelen);
200076dda93cSYan, Zheng 	if (IS_ERR(dentry)) {
200176dda93cSYan, Zheng 		err = PTR_ERR(dentry);
200276dda93cSYan, Zheng 		goto out_unlock_dir;
200376dda93cSYan, Zheng 	}
200476dda93cSYan, Zheng 
200576dda93cSYan, Zheng 	if (!dentry->d_inode) {
200676dda93cSYan, Zheng 		err = -ENOENT;
200776dda93cSYan, Zheng 		goto out_dput;
200876dda93cSYan, Zheng 	}
200976dda93cSYan, Zheng 
201076dda93cSYan, Zheng 	inode = dentry->d_inode;
20114260f7c7SSage Weil 	dest = BTRFS_I(inode)->root;
20124260f7c7SSage Weil 	if (!capable(CAP_SYS_ADMIN)){
20134260f7c7SSage Weil 		/*
20144260f7c7SSage Weil 		 * Regular user.  Only allow this with a special mount
20154260f7c7SSage Weil 		 * option, when the user has write+exec access to the
20164260f7c7SSage Weil 		 * subvol root, and when rmdir(2) would have been
20174260f7c7SSage Weil 		 * allowed.
20184260f7c7SSage Weil 		 *
20194260f7c7SSage Weil 		 * Note that this is _not_ check that the subvol is
20204260f7c7SSage Weil 		 * empty or doesn't contain data that we wouldn't
20214260f7c7SSage Weil 		 * otherwise be able to delete.
20224260f7c7SSage Weil 		 *
20234260f7c7SSage Weil 		 * Users who want to delete empty subvols should try
20244260f7c7SSage Weil 		 * rmdir(2).
20254260f7c7SSage Weil 		 */
20264260f7c7SSage Weil 		err = -EPERM;
20274260f7c7SSage Weil 		if (!btrfs_test_opt(root, USER_SUBVOL_RM_ALLOWED))
20284260f7c7SSage Weil 			goto out_dput;
20294260f7c7SSage Weil 
20304260f7c7SSage Weil 		/*
20314260f7c7SSage Weil 		 * Do not allow deletion if the parent dir is the same
20324260f7c7SSage Weil 		 * as the dir to be deleted.  That means the ioctl
20334260f7c7SSage Weil 		 * must be called on the dentry referencing the root
20344260f7c7SSage Weil 		 * of the subvol, not a random directory contained
20354260f7c7SSage Weil 		 * within it.
20364260f7c7SSage Weil 		 */
20374260f7c7SSage Weil 		err = -EINVAL;
20384260f7c7SSage Weil 		if (root == dest)
20394260f7c7SSage Weil 			goto out_dput;
20404260f7c7SSage Weil 
20414260f7c7SSage Weil 		err = inode_permission(inode, MAY_WRITE | MAY_EXEC);
20424260f7c7SSage Weil 		if (err)
20434260f7c7SSage Weil 			goto out_dput;
20444260f7c7SSage Weil 
20454260f7c7SSage Weil 		/* check if subvolume may be deleted by a non-root user */
20464260f7c7SSage Weil 		err = btrfs_may_delete(dir, dentry, 1);
20474260f7c7SSage Weil 		if (err)
20484260f7c7SSage Weil 			goto out_dput;
20494260f7c7SSage Weil 	}
20504260f7c7SSage Weil 
205133345d01SLi Zefan 	if (btrfs_ino(inode) != BTRFS_FIRST_FREE_OBJECTID) {
205276dda93cSYan, Zheng 		err = -EINVAL;
205376dda93cSYan, Zheng 		goto out_dput;
205476dda93cSYan, Zheng 	}
205576dda93cSYan, Zheng 
205676dda93cSYan, Zheng 	mutex_lock(&inode->i_mutex);
205776dda93cSYan, Zheng 	err = d_invalidate(dentry);
205876dda93cSYan, Zheng 	if (err)
205976dda93cSYan, Zheng 		goto out_unlock;
206076dda93cSYan, Zheng 
206176dda93cSYan, Zheng 	down_write(&root->fs_info->subvol_sem);
206276dda93cSYan, Zheng 
206376dda93cSYan, Zheng 	err = may_destroy_subvol(dest);
206476dda93cSYan, Zheng 	if (err)
206576dda93cSYan, Zheng 		goto out_up_write;
206676dda93cSYan, Zheng 
2067a22285a6SYan, Zheng 	trans = btrfs_start_transaction(root, 0);
2068a22285a6SYan, Zheng 	if (IS_ERR(trans)) {
2069a22285a6SYan, Zheng 		err = PTR_ERR(trans);
2070d327099aSDan Carpenter 		goto out_up_write;
2071a22285a6SYan, Zheng 	}
2072a22285a6SYan, Zheng 	trans->block_rsv = &root->fs_info->global_block_rsv;
2073a22285a6SYan, Zheng 
207476dda93cSYan, Zheng 	ret = btrfs_unlink_subvol(trans, root, dir,
207576dda93cSYan, Zheng 				dest->root_key.objectid,
207676dda93cSYan, Zheng 				dentry->d_name.name,
207776dda93cSYan, Zheng 				dentry->d_name.len);
207879787eaaSJeff Mahoney 	if (ret) {
207979787eaaSJeff Mahoney 		err = ret;
208079787eaaSJeff Mahoney 		btrfs_abort_transaction(trans, root, ret);
208179787eaaSJeff Mahoney 		goto out_end_trans;
208279787eaaSJeff Mahoney 	}
208376dda93cSYan, Zheng 
208476dda93cSYan, Zheng 	btrfs_record_root_in_trans(trans, dest);
208576dda93cSYan, Zheng 
208676dda93cSYan, Zheng 	memset(&dest->root_item.drop_progress, 0,
208776dda93cSYan, Zheng 		sizeof(dest->root_item.drop_progress));
208876dda93cSYan, Zheng 	dest->root_item.drop_level = 0;
208976dda93cSYan, Zheng 	btrfs_set_root_refs(&dest->root_item, 0);
209076dda93cSYan, Zheng 
2091d68fc57bSYan, Zheng 	if (!xchg(&dest->orphan_item_inserted, 1)) {
209276dda93cSYan, Zheng 		ret = btrfs_insert_orphan_item(trans,
209376dda93cSYan, Zheng 					root->fs_info->tree_root,
209476dda93cSYan, Zheng 					dest->root_key.objectid);
209579787eaaSJeff Mahoney 		if (ret) {
209679787eaaSJeff Mahoney 			btrfs_abort_transaction(trans, root, ret);
209779787eaaSJeff Mahoney 			err = ret;
209879787eaaSJeff Mahoney 			goto out_end_trans;
2099d68fc57bSYan, Zheng 		}
210079787eaaSJeff Mahoney 	}
210179787eaaSJeff Mahoney out_end_trans:
2102531cb13fSSage Weil 	ret = btrfs_end_transaction(trans, root);
210379787eaaSJeff Mahoney 	if (ret && !err)
210479787eaaSJeff Mahoney 		err = ret;
210576dda93cSYan, Zheng 	inode->i_flags |= S_DEAD;
210676dda93cSYan, Zheng out_up_write:
210776dda93cSYan, Zheng 	up_write(&root->fs_info->subvol_sem);
210876dda93cSYan, Zheng out_unlock:
210976dda93cSYan, Zheng 	mutex_unlock(&inode->i_mutex);
211076dda93cSYan, Zheng 	if (!err) {
2111efefb143SYan, Zheng 		shrink_dcache_sb(root->fs_info->sb);
211276dda93cSYan, Zheng 		btrfs_invalidate_inodes(dest);
211376dda93cSYan, Zheng 		d_delete(dentry);
211476dda93cSYan, Zheng 	}
211576dda93cSYan, Zheng out_dput:
211676dda93cSYan, Zheng 	dput(dentry);
211776dda93cSYan, Zheng out_unlock_dir:
211876dda93cSYan, Zheng 	mutex_unlock(&dir->i_mutex);
21192a79f17eSAl Viro 	mnt_drop_write_file(file);
212076dda93cSYan, Zheng out:
212176dda93cSYan, Zheng 	kfree(vol_args);
212276dda93cSYan, Zheng 	return err;
212376dda93cSYan, Zheng }
212476dda93cSYan, Zheng 
21251e701a32SChris Mason static int btrfs_ioctl_defrag(struct file *file, void __user *argp)
2126f46b5a66SChristoph Hellwig {
2127f46b5a66SChristoph Hellwig 	struct inode *inode = fdentry(file)->d_inode;
2128f46b5a66SChristoph Hellwig 	struct btrfs_root *root = BTRFS_I(inode)->root;
21291e701a32SChris Mason 	struct btrfs_ioctl_defrag_range_args *range;
2130c146afadSYan Zheng 	int ret;
2131c146afadSYan Zheng 
2132b83cc969SLi Zefan 	if (btrfs_root_readonly(root))
2133b83cc969SLi Zefan 		return -EROFS;
2134b83cc969SLi Zefan 
2135a561be71SAl Viro 	ret = mnt_want_write_file(file);
2136c146afadSYan Zheng 	if (ret)
2137c146afadSYan Zheng 		return ret;
2138f46b5a66SChristoph Hellwig 
2139f46b5a66SChristoph Hellwig 	switch (inode->i_mode & S_IFMT) {
2140f46b5a66SChristoph Hellwig 	case S_IFDIR:
2141e441d54dSChris Mason 		if (!capable(CAP_SYS_ADMIN)) {
2142e441d54dSChris Mason 			ret = -EPERM;
2143e441d54dSChris Mason 			goto out;
2144e441d54dSChris Mason 		}
21458929ecfaSYan, Zheng 		ret = btrfs_defrag_root(root, 0);
21468929ecfaSYan, Zheng 		if (ret)
21478929ecfaSYan, Zheng 			goto out;
21488929ecfaSYan, Zheng 		ret = btrfs_defrag_root(root->fs_info->extent_root, 0);
2149f46b5a66SChristoph Hellwig 		break;
2150f46b5a66SChristoph Hellwig 	case S_IFREG:
2151e441d54dSChris Mason 		if (!(file->f_mode & FMODE_WRITE)) {
2152e441d54dSChris Mason 			ret = -EINVAL;
2153e441d54dSChris Mason 			goto out;
2154e441d54dSChris Mason 		}
21551e701a32SChris Mason 
21561e701a32SChris Mason 		range = kzalloc(sizeof(*range), GFP_KERNEL);
21571e701a32SChris Mason 		if (!range) {
21581e701a32SChris Mason 			ret = -ENOMEM;
21591e701a32SChris Mason 			goto out;
21601e701a32SChris Mason 		}
21611e701a32SChris Mason 
21621e701a32SChris Mason 		if (argp) {
21631e701a32SChris Mason 			if (copy_from_user(range, argp,
21641e701a32SChris Mason 					   sizeof(*range))) {
21651e701a32SChris Mason 				ret = -EFAULT;
21661e701a32SChris Mason 				kfree(range);
2167683be16eSDan Carpenter 				goto out;
21681e701a32SChris Mason 			}
21691e701a32SChris Mason 			/* compression requires us to start the IO */
21701e701a32SChris Mason 			if ((range->flags & BTRFS_DEFRAG_RANGE_COMPRESS)) {
21711e701a32SChris Mason 				range->flags |= BTRFS_DEFRAG_RANGE_START_IO;
21721e701a32SChris Mason 				range->extent_thresh = (u32)-1;
21731e701a32SChris Mason 			}
21741e701a32SChris Mason 		} else {
21751e701a32SChris Mason 			/* the rest are all set to zero by kzalloc */
21761e701a32SChris Mason 			range->len = (u64)-1;
21771e701a32SChris Mason 		}
21784cb5300bSChris Mason 		ret = btrfs_defrag_file(fdentry(file)->d_inode, file,
21794cb5300bSChris Mason 					range, 0, 0);
21804cb5300bSChris Mason 		if (ret > 0)
21814cb5300bSChris Mason 			ret = 0;
21821e701a32SChris Mason 		kfree(range);
2183f46b5a66SChristoph Hellwig 		break;
21848929ecfaSYan, Zheng 	default:
21858929ecfaSYan, Zheng 		ret = -EINVAL;
2186f46b5a66SChristoph Hellwig 	}
2187e441d54dSChris Mason out:
21882a79f17eSAl Viro 	mnt_drop_write_file(file);
2189e441d54dSChris Mason 	return ret;
2190f46b5a66SChristoph Hellwig }
2191f46b5a66SChristoph Hellwig 
2192b2950863SChristoph Hellwig static long btrfs_ioctl_add_dev(struct btrfs_root *root, void __user *arg)
2193f46b5a66SChristoph Hellwig {
2194f46b5a66SChristoph Hellwig 	struct btrfs_ioctl_vol_args *vol_args;
2195f46b5a66SChristoph Hellwig 	int ret;
2196f46b5a66SChristoph Hellwig 
2197e441d54dSChris Mason 	if (!capable(CAP_SYS_ADMIN))
2198e441d54dSChris Mason 		return -EPERM;
2199e441d54dSChris Mason 
2200c9e9f97bSIlya Dryomov 	mutex_lock(&root->fs_info->volume_mutex);
2201c9e9f97bSIlya Dryomov 	if (root->fs_info->balance_ctl) {
2202c9e9f97bSIlya Dryomov 		printk(KERN_INFO "btrfs: balance in progress\n");
2203c9e9f97bSIlya Dryomov 		ret = -EINVAL;
2204c9e9f97bSIlya Dryomov 		goto out;
2205c9e9f97bSIlya Dryomov 	}
2206c9e9f97bSIlya Dryomov 
2207dae7b665SLi Zefan 	vol_args = memdup_user(arg, sizeof(*vol_args));
2208c9e9f97bSIlya Dryomov 	if (IS_ERR(vol_args)) {
2209c9e9f97bSIlya Dryomov 		ret = PTR_ERR(vol_args);
2210c9e9f97bSIlya Dryomov 		goto out;
2211c9e9f97bSIlya Dryomov 	}
2212f46b5a66SChristoph Hellwig 
22135516e595SMark Fasheh 	vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
2214f46b5a66SChristoph Hellwig 	ret = btrfs_init_new_device(root, vol_args->name);
2215f46b5a66SChristoph Hellwig 
2216f46b5a66SChristoph Hellwig 	kfree(vol_args);
2217c9e9f97bSIlya Dryomov out:
2218c9e9f97bSIlya Dryomov 	mutex_unlock(&root->fs_info->volume_mutex);
2219f46b5a66SChristoph Hellwig 	return ret;
2220f46b5a66SChristoph Hellwig }
2221f46b5a66SChristoph Hellwig 
2222b2950863SChristoph Hellwig static long btrfs_ioctl_rm_dev(struct btrfs_root *root, void __user *arg)
2223f46b5a66SChristoph Hellwig {
2224f46b5a66SChristoph Hellwig 	struct btrfs_ioctl_vol_args *vol_args;
2225f46b5a66SChristoph Hellwig 	int ret;
2226f46b5a66SChristoph Hellwig 
2227e441d54dSChris Mason 	if (!capable(CAP_SYS_ADMIN))
2228e441d54dSChris Mason 		return -EPERM;
2229e441d54dSChris Mason 
2230c146afadSYan Zheng 	if (root->fs_info->sb->s_flags & MS_RDONLY)
2231c146afadSYan Zheng 		return -EROFS;
2232c146afadSYan Zheng 
2233c9e9f97bSIlya Dryomov 	mutex_lock(&root->fs_info->volume_mutex);
2234c9e9f97bSIlya Dryomov 	if (root->fs_info->balance_ctl) {
2235c9e9f97bSIlya Dryomov 		printk(KERN_INFO "btrfs: balance in progress\n");
2236c9e9f97bSIlya Dryomov 		ret = -EINVAL;
2237c9e9f97bSIlya Dryomov 		goto out;
2238c9e9f97bSIlya Dryomov 	}
2239c9e9f97bSIlya Dryomov 
2240dae7b665SLi Zefan 	vol_args = memdup_user(arg, sizeof(*vol_args));
2241c9e9f97bSIlya Dryomov 	if (IS_ERR(vol_args)) {
2242c9e9f97bSIlya Dryomov 		ret = PTR_ERR(vol_args);
2243c9e9f97bSIlya Dryomov 		goto out;
2244c9e9f97bSIlya Dryomov 	}
2245f46b5a66SChristoph Hellwig 
22465516e595SMark Fasheh 	vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
2247f46b5a66SChristoph Hellwig 	ret = btrfs_rm_device(root, vol_args->name);
2248f46b5a66SChristoph Hellwig 
2249f46b5a66SChristoph Hellwig 	kfree(vol_args);
2250c9e9f97bSIlya Dryomov out:
2251c9e9f97bSIlya Dryomov 	mutex_unlock(&root->fs_info->volume_mutex);
2252f46b5a66SChristoph Hellwig 	return ret;
2253f46b5a66SChristoph Hellwig }
2254f46b5a66SChristoph Hellwig 
2255475f6387SJan Schmidt static long btrfs_ioctl_fs_info(struct btrfs_root *root, void __user *arg)
2256475f6387SJan Schmidt {
2257027ed2f0SLi Zefan 	struct btrfs_ioctl_fs_info_args *fi_args;
2258475f6387SJan Schmidt 	struct btrfs_device *device;
2259475f6387SJan Schmidt 	struct btrfs_device *next;
2260475f6387SJan Schmidt 	struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices;
2261027ed2f0SLi Zefan 	int ret = 0;
2262475f6387SJan Schmidt 
2263475f6387SJan Schmidt 	if (!capable(CAP_SYS_ADMIN))
2264475f6387SJan Schmidt 		return -EPERM;
2265475f6387SJan Schmidt 
2266027ed2f0SLi Zefan 	fi_args = kzalloc(sizeof(*fi_args), GFP_KERNEL);
2267027ed2f0SLi Zefan 	if (!fi_args)
2268027ed2f0SLi Zefan 		return -ENOMEM;
2269027ed2f0SLi Zefan 
2270027ed2f0SLi Zefan 	fi_args->num_devices = fs_devices->num_devices;
2271027ed2f0SLi Zefan 	memcpy(&fi_args->fsid, root->fs_info->fsid, sizeof(fi_args->fsid));
2272475f6387SJan Schmidt 
2273475f6387SJan Schmidt 	mutex_lock(&fs_devices->device_list_mutex);
2274475f6387SJan Schmidt 	list_for_each_entry_safe(device, next, &fs_devices->devices, dev_list) {
2275027ed2f0SLi Zefan 		if (device->devid > fi_args->max_id)
2276027ed2f0SLi Zefan 			fi_args->max_id = device->devid;
2277475f6387SJan Schmidt 	}
2278475f6387SJan Schmidt 	mutex_unlock(&fs_devices->device_list_mutex);
2279475f6387SJan Schmidt 
2280027ed2f0SLi Zefan 	if (copy_to_user(arg, fi_args, sizeof(*fi_args)))
2281027ed2f0SLi Zefan 		ret = -EFAULT;
2282475f6387SJan Schmidt 
2283027ed2f0SLi Zefan 	kfree(fi_args);
2284027ed2f0SLi Zefan 	return ret;
2285475f6387SJan Schmidt }
2286475f6387SJan Schmidt 
2287475f6387SJan Schmidt static long btrfs_ioctl_dev_info(struct btrfs_root *root, void __user *arg)
2288475f6387SJan Schmidt {
2289475f6387SJan Schmidt 	struct btrfs_ioctl_dev_info_args *di_args;
2290475f6387SJan Schmidt 	struct btrfs_device *dev;
2291475f6387SJan Schmidt 	struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices;
2292475f6387SJan Schmidt 	int ret = 0;
2293475f6387SJan Schmidt 	char *s_uuid = NULL;
2294475f6387SJan Schmidt 	char empty_uuid[BTRFS_UUID_SIZE] = {0};
2295475f6387SJan Schmidt 
2296475f6387SJan Schmidt 	if (!capable(CAP_SYS_ADMIN))
2297475f6387SJan Schmidt 		return -EPERM;
2298475f6387SJan Schmidt 
2299475f6387SJan Schmidt 	di_args = memdup_user(arg, sizeof(*di_args));
2300475f6387SJan Schmidt 	if (IS_ERR(di_args))
2301475f6387SJan Schmidt 		return PTR_ERR(di_args);
2302475f6387SJan Schmidt 
2303475f6387SJan Schmidt 	if (memcmp(empty_uuid, di_args->uuid, BTRFS_UUID_SIZE) != 0)
2304475f6387SJan Schmidt 		s_uuid = di_args->uuid;
2305475f6387SJan Schmidt 
2306475f6387SJan Schmidt 	mutex_lock(&fs_devices->device_list_mutex);
2307475f6387SJan Schmidt 	dev = btrfs_find_device(root, di_args->devid, s_uuid, NULL);
2308475f6387SJan Schmidt 	mutex_unlock(&fs_devices->device_list_mutex);
2309475f6387SJan Schmidt 
2310475f6387SJan Schmidt 	if (!dev) {
2311475f6387SJan Schmidt 		ret = -ENODEV;
2312475f6387SJan Schmidt 		goto out;
2313475f6387SJan Schmidt 	}
2314475f6387SJan Schmidt 
2315475f6387SJan Schmidt 	di_args->devid = dev->devid;
2316475f6387SJan Schmidt 	di_args->bytes_used = dev->bytes_used;
2317475f6387SJan Schmidt 	di_args->total_bytes = dev->total_bytes;
2318475f6387SJan Schmidt 	memcpy(di_args->uuid, dev->uuid, sizeof(di_args->uuid));
2319a27202fbSJim Meyering 	if (dev->name) {
2320606686eeSJosef Bacik 		struct rcu_string *name;
2321606686eeSJosef Bacik 
2322606686eeSJosef Bacik 		rcu_read_lock();
2323606686eeSJosef Bacik 		name = rcu_dereference(dev->name);
2324606686eeSJosef Bacik 		strncpy(di_args->path, name->str, sizeof(di_args->path));
2325606686eeSJosef Bacik 		rcu_read_unlock();
2326a27202fbSJim Meyering 		di_args->path[sizeof(di_args->path) - 1] = 0;
2327a27202fbSJim Meyering 	} else {
232899ba55adSStefan Behrens 		di_args->path[0] = '\0';
2329a27202fbSJim Meyering 	}
2330475f6387SJan Schmidt 
2331475f6387SJan Schmidt out:
2332475f6387SJan Schmidt 	if (ret == 0 && copy_to_user(arg, di_args, sizeof(*di_args)))
2333475f6387SJan Schmidt 		ret = -EFAULT;
2334475f6387SJan Schmidt 
2335475f6387SJan Schmidt 	kfree(di_args);
2336475f6387SJan Schmidt 	return ret;
2337475f6387SJan Schmidt }
2338475f6387SJan Schmidt 
233976dda93cSYan, Zheng static noinline long btrfs_ioctl_clone(struct file *file, unsigned long srcfd,
2340b2950863SChristoph Hellwig 				       u64 off, u64 olen, u64 destoff)
2341f46b5a66SChristoph Hellwig {
2342f46b5a66SChristoph Hellwig 	struct inode *inode = fdentry(file)->d_inode;
2343f46b5a66SChristoph Hellwig 	struct btrfs_root *root = BTRFS_I(inode)->root;
2344f46b5a66SChristoph Hellwig 	struct file *src_file;
2345f46b5a66SChristoph Hellwig 	struct inode *src;
2346f46b5a66SChristoph Hellwig 	struct btrfs_trans_handle *trans;
2347f46b5a66SChristoph Hellwig 	struct btrfs_path *path;
2348f46b5a66SChristoph Hellwig 	struct extent_buffer *leaf;
2349ae01a0abSYan Zheng 	char *buf;
2350ae01a0abSYan Zheng 	struct btrfs_key key;
2351f46b5a66SChristoph Hellwig 	u32 nritems;
2352f46b5a66SChristoph Hellwig 	int slot;
2353ae01a0abSYan Zheng 	int ret;
2354c5c9cd4dSSage Weil 	u64 len = olen;
2355c5c9cd4dSSage Weil 	u64 bs = root->fs_info->sb->s_blocksize;
2356c5c9cd4dSSage Weil 	u64 hint_byte;
2357d20f7043SChris Mason 
2358c5c9cd4dSSage Weil 	/*
2359c5c9cd4dSSage Weil 	 * TODO:
2360c5c9cd4dSSage Weil 	 * - split compressed inline extents.  annoying: we need to
2361c5c9cd4dSSage Weil 	 *   decompress into destination's address_space (the file offset
2362c5c9cd4dSSage Weil 	 *   may change, so source mapping won't do), then recompress (or
2363c5c9cd4dSSage Weil 	 *   otherwise reinsert) a subrange.
2364c5c9cd4dSSage Weil 	 * - allow ranges within the same file to be cloned (provided
2365c5c9cd4dSSage Weil 	 *   they don't overlap)?
2366c5c9cd4dSSage Weil 	 */
2367c5c9cd4dSSage Weil 
2368e441d54dSChris Mason 	/* the destination must be opened for writing */
23692ebc3464SDan Rosenberg 	if (!(file->f_mode & FMODE_WRITE) || (file->f_flags & O_APPEND))
2370e441d54dSChris Mason 		return -EINVAL;
2371e441d54dSChris Mason 
2372b83cc969SLi Zefan 	if (btrfs_root_readonly(root))
2373b83cc969SLi Zefan 		return -EROFS;
2374b83cc969SLi Zefan 
2375a561be71SAl Viro 	ret = mnt_want_write_file(file);
2376c146afadSYan Zheng 	if (ret)
2377c146afadSYan Zheng 		return ret;
2378c146afadSYan Zheng 
2379c5c9cd4dSSage Weil 	src_file = fget(srcfd);
2380ab67b7c1SYan Zheng 	if (!src_file) {
2381ab67b7c1SYan Zheng 		ret = -EBADF;
2382ab67b7c1SYan Zheng 		goto out_drop_write;
2383ab67b7c1SYan Zheng 	}
23845dc64164SDan Rosenberg 
2385362a20c5SDavid Sterba 	ret = -EXDEV;
2386362a20c5SDavid Sterba 	if (src_file->f_path.mnt != file->f_path.mnt)
2387362a20c5SDavid Sterba 		goto out_fput;
2388362a20c5SDavid Sterba 
2389f46b5a66SChristoph Hellwig 	src = src_file->f_dentry->d_inode;
2390f46b5a66SChristoph Hellwig 
2391c5c9cd4dSSage Weil 	ret = -EINVAL;
2392c5c9cd4dSSage Weil 	if (src == inode)
2393c5c9cd4dSSage Weil 		goto out_fput;
2394c5c9cd4dSSage Weil 
23955dc64164SDan Rosenberg 	/* the src must be open for reading */
23965dc64164SDan Rosenberg 	if (!(src_file->f_mode & FMODE_READ))
23975dc64164SDan Rosenberg 		goto out_fput;
23985dc64164SDan Rosenberg 
23990e7b824cSLi Zefan 	/* don't make the dst file partly checksummed */
24000e7b824cSLi Zefan 	if ((BTRFS_I(src)->flags & BTRFS_INODE_NODATASUM) !=
24010e7b824cSLi Zefan 	    (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM))
24020e7b824cSLi Zefan 		goto out_fput;
24030e7b824cSLi Zefan 
2404ae01a0abSYan Zheng 	ret = -EISDIR;
2405ae01a0abSYan Zheng 	if (S_ISDIR(src->i_mode) || S_ISDIR(inode->i_mode))
2406f46b5a66SChristoph Hellwig 		goto out_fput;
2407f46b5a66SChristoph Hellwig 
2408ae01a0abSYan Zheng 	ret = -EXDEV;
2409362a20c5SDavid Sterba 	if (src->i_sb != inode->i_sb)
2410ae01a0abSYan Zheng 		goto out_fput;
2411ae01a0abSYan Zheng 
2412ae01a0abSYan Zheng 	ret = -ENOMEM;
2413ae01a0abSYan Zheng 	buf = vmalloc(btrfs_level_size(root, 0));
2414ae01a0abSYan Zheng 	if (!buf)
2415ae01a0abSYan Zheng 		goto out_fput;
2416ae01a0abSYan Zheng 
2417ae01a0abSYan Zheng 	path = btrfs_alloc_path();
2418ae01a0abSYan Zheng 	if (!path) {
2419ae01a0abSYan Zheng 		vfree(buf);
2420ae01a0abSYan Zheng 		goto out_fput;
2421ae01a0abSYan Zheng 	}
2422ae01a0abSYan Zheng 	path->reada = 2;
2423ae01a0abSYan Zheng 
2424f46b5a66SChristoph Hellwig 	if (inode < src) {
2425fccdae43SSage Weil 		mutex_lock_nested(&inode->i_mutex, I_MUTEX_PARENT);
2426fccdae43SSage Weil 		mutex_lock_nested(&src->i_mutex, I_MUTEX_CHILD);
2427f46b5a66SChristoph Hellwig 	} else {
2428fccdae43SSage Weil 		mutex_lock_nested(&src->i_mutex, I_MUTEX_PARENT);
2429fccdae43SSage Weil 		mutex_lock_nested(&inode->i_mutex, I_MUTEX_CHILD);
2430f46b5a66SChristoph Hellwig 	}
2431f46b5a66SChristoph Hellwig 
2432c5c9cd4dSSage Weil 	/* determine range to clone */
2433c5c9cd4dSSage Weil 	ret = -EINVAL;
24342ebc3464SDan Rosenberg 	if (off + len > src->i_size || off + len < off)
2435f46b5a66SChristoph Hellwig 		goto out_unlock;
2436c5c9cd4dSSage Weil 	if (len == 0)
2437c5c9cd4dSSage Weil 		olen = len = src->i_size - off;
2438c5c9cd4dSSage Weil 	/* if we extend to eof, continue to block boundary */
2439c5c9cd4dSSage Weil 	if (off + len == src->i_size)
24402a6b8daeSLi Zefan 		len = ALIGN(src->i_size, bs) - off;
2441c5c9cd4dSSage Weil 
2442c5c9cd4dSSage Weil 	/* verify the end result is block aligned */
24432a6b8daeSLi Zefan 	if (!IS_ALIGNED(off, bs) || !IS_ALIGNED(off + len, bs) ||
24442a6b8daeSLi Zefan 	    !IS_ALIGNED(destoff, bs))
2445c5c9cd4dSSage Weil 		goto out_unlock;
2446c5c9cd4dSSage Weil 
2447d525e8abSLi Zefan 	if (destoff > inode->i_size) {
2448d525e8abSLi Zefan 		ret = btrfs_cont_expand(inode, inode->i_size, destoff);
2449d525e8abSLi Zefan 		if (ret)
2450d525e8abSLi Zefan 			goto out_unlock;
2451d525e8abSLi Zefan 	}
2452d525e8abSLi Zefan 
245371ef0786SLi Zefan 	/* truncate page cache pages from target inode range */
245471ef0786SLi Zefan 	truncate_inode_pages_range(&inode->i_data, destoff,
245571ef0786SLi Zefan 				   PAGE_CACHE_ALIGN(destoff + len) - 1);
245671ef0786SLi Zefan 
2457f46b5a66SChristoph Hellwig 	/* do any pending delalloc/csum calc on src, one way or
2458f46b5a66SChristoph Hellwig 	   another, and lock file content */
2459f46b5a66SChristoph Hellwig 	while (1) {
246031840ae1SZheng Yan 		struct btrfs_ordered_extent *ordered;
2461d0082371SJeff Mahoney 		lock_extent(&BTRFS_I(src)->io_tree, off, off+len);
24629a019196SSage Weil 		ordered = btrfs_lookup_first_ordered_extent(src, off+len);
24639a019196SSage Weil 		if (!ordered &&
24649a019196SSage Weil 		    !test_range_bit(&BTRFS_I(src)->io_tree, off, off+len,
24659a019196SSage Weil 				   EXTENT_DELALLOC, 0, NULL))
2466f46b5a66SChristoph Hellwig 			break;
2467d0082371SJeff Mahoney 		unlock_extent(&BTRFS_I(src)->io_tree, off, off+len);
2468ae01a0abSYan Zheng 		if (ordered)
2469ae01a0abSYan Zheng 			btrfs_put_ordered_extent(ordered);
24709a019196SSage Weil 		btrfs_wait_ordered_range(src, off, len);
2471f46b5a66SChristoph Hellwig 	}
2472f46b5a66SChristoph Hellwig 
2473c5c9cd4dSSage Weil 	/* clone data */
247433345d01SLi Zefan 	key.objectid = btrfs_ino(src);
2475ae01a0abSYan Zheng 	key.type = BTRFS_EXTENT_DATA_KEY;
2476ae01a0abSYan Zheng 	key.offset = 0;
2477f46b5a66SChristoph Hellwig 
2478f46b5a66SChristoph Hellwig 	while (1) {
2479f46b5a66SChristoph Hellwig 		/*
2480f46b5a66SChristoph Hellwig 		 * note the key will change type as we walk through the
2481f46b5a66SChristoph Hellwig 		 * tree.
2482f46b5a66SChristoph Hellwig 		 */
2483362a20c5SDavid Sterba 		ret = btrfs_search_slot(NULL, BTRFS_I(src)->root, &key, path,
2484362a20c5SDavid Sterba 				0, 0);
2485f46b5a66SChristoph Hellwig 		if (ret < 0)
2486f46b5a66SChristoph Hellwig 			goto out;
2487f46b5a66SChristoph Hellwig 
2488ae01a0abSYan Zheng 		nritems = btrfs_header_nritems(path->nodes[0]);
2489ae01a0abSYan Zheng 		if (path->slots[0] >= nritems) {
2490362a20c5SDavid Sterba 			ret = btrfs_next_leaf(BTRFS_I(src)->root, path);
2491f46b5a66SChristoph Hellwig 			if (ret < 0)
2492f46b5a66SChristoph Hellwig 				goto out;
2493f46b5a66SChristoph Hellwig 			if (ret > 0)
2494f46b5a66SChristoph Hellwig 				break;
2495ae01a0abSYan Zheng 			nritems = btrfs_header_nritems(path->nodes[0]);
2496f46b5a66SChristoph Hellwig 		}
2497f46b5a66SChristoph Hellwig 		leaf = path->nodes[0];
2498f46b5a66SChristoph Hellwig 		slot = path->slots[0];
2499f46b5a66SChristoph Hellwig 
2500ae01a0abSYan Zheng 		btrfs_item_key_to_cpu(leaf, &key, slot);
2501d20f7043SChris Mason 		if (btrfs_key_type(&key) > BTRFS_EXTENT_DATA_KEY ||
250233345d01SLi Zefan 		    key.objectid != btrfs_ino(src))
2503f46b5a66SChristoph Hellwig 			break;
2504f46b5a66SChristoph Hellwig 
2505c5c9cd4dSSage Weil 		if (btrfs_key_type(&key) == BTRFS_EXTENT_DATA_KEY) {
2506c5c9cd4dSSage Weil 			struct btrfs_file_extent_item *extent;
2507c5c9cd4dSSage Weil 			int type;
250831840ae1SZheng Yan 			u32 size;
250931840ae1SZheng Yan 			struct btrfs_key new_key;
2510c5c9cd4dSSage Weil 			u64 disko = 0, diskl = 0;
2511c5c9cd4dSSage Weil 			u64 datao = 0, datal = 0;
2512c5c9cd4dSSage Weil 			u8 comp;
2513b5384d48SSage Weil 			u64 endoff;
251431840ae1SZheng Yan 
251531840ae1SZheng Yan 			size = btrfs_item_size_nr(leaf, slot);
251631840ae1SZheng Yan 			read_extent_buffer(leaf, buf,
251731840ae1SZheng Yan 					   btrfs_item_ptr_offset(leaf, slot),
251831840ae1SZheng Yan 					   size);
2519c5c9cd4dSSage Weil 
2520c5c9cd4dSSage Weil 			extent = btrfs_item_ptr(leaf, slot,
2521c5c9cd4dSSage Weil 						struct btrfs_file_extent_item);
2522c5c9cd4dSSage Weil 			comp = btrfs_file_extent_compression(leaf, extent);
2523c5c9cd4dSSage Weil 			type = btrfs_file_extent_type(leaf, extent);
2524c8a894d7SChris Mason 			if (type == BTRFS_FILE_EXTENT_REG ||
2525c8a894d7SChris Mason 			    type == BTRFS_FILE_EXTENT_PREALLOC) {
2526d397712bSChris Mason 				disko = btrfs_file_extent_disk_bytenr(leaf,
2527d397712bSChris Mason 								      extent);
2528d397712bSChris Mason 				diskl = btrfs_file_extent_disk_num_bytes(leaf,
2529d397712bSChris Mason 								 extent);
2530c5c9cd4dSSage Weil 				datao = btrfs_file_extent_offset(leaf, extent);
2531d397712bSChris Mason 				datal = btrfs_file_extent_num_bytes(leaf,
2532d397712bSChris Mason 								    extent);
2533c5c9cd4dSSage Weil 			} else if (type == BTRFS_FILE_EXTENT_INLINE) {
2534c5c9cd4dSSage Weil 				/* take upper bound, may be compressed */
2535c5c9cd4dSSage Weil 				datal = btrfs_file_extent_ram_bytes(leaf,
2536c5c9cd4dSSage Weil 								    extent);
2537c5c9cd4dSSage Weil 			}
2538b3b4aa74SDavid Sterba 			btrfs_release_path(path);
253931840ae1SZheng Yan 
2540050006a7SSage Weil 			if (key.offset + datal <= off ||
2541c5c9cd4dSSage Weil 			    key.offset >= off+len)
2542c5c9cd4dSSage Weil 				goto next;
2543c5c9cd4dSSage Weil 
254431840ae1SZheng Yan 			memcpy(&new_key, &key, sizeof(new_key));
254533345d01SLi Zefan 			new_key.objectid = btrfs_ino(inode);
25464d728ec7SLi Zefan 			if (off <= key.offset)
2547c5c9cd4dSSage Weil 				new_key.offset = key.offset + destoff - off;
25484d728ec7SLi Zefan 			else
25494d728ec7SLi Zefan 				new_key.offset = destoff;
2550c5c9cd4dSSage Weil 
2551b6f3409bSSage Weil 			/*
2552b6f3409bSSage Weil 			 * 1 - adjusting old extent (we may have to split it)
2553b6f3409bSSage Weil 			 * 1 - add new extent
2554b6f3409bSSage Weil 			 * 1 - inode update
2555b6f3409bSSage Weil 			 */
2556b6f3409bSSage Weil 			trans = btrfs_start_transaction(root, 3);
2557a22285a6SYan, Zheng 			if (IS_ERR(trans)) {
2558a22285a6SYan, Zheng 				ret = PTR_ERR(trans);
2559a22285a6SYan, Zheng 				goto out;
2560a22285a6SYan, Zheng 			}
2561a22285a6SYan, Zheng 
2562c8a894d7SChris Mason 			if (type == BTRFS_FILE_EXTENT_REG ||
2563c8a894d7SChris Mason 			    type == BTRFS_FILE_EXTENT_PREALLOC) {
2564d72c0842SLi Zefan 				/*
2565d72c0842SLi Zefan 				 *    a  | --- range to clone ---|  b
2566d72c0842SLi Zefan 				 * | ------------- extent ------------- |
2567d72c0842SLi Zefan 				 */
2568d72c0842SLi Zefan 
2569d72c0842SLi Zefan 				/* substract range b */
2570d72c0842SLi Zefan 				if (key.offset + datal > off + len)
2571d72c0842SLi Zefan 					datal = off + len - key.offset;
2572d72c0842SLi Zefan 
2573d72c0842SLi Zefan 				/* substract range a */
2574a22285a6SYan, Zheng 				if (off > key.offset) {
2575a22285a6SYan, Zheng 					datao += off - key.offset;
2576a22285a6SYan, Zheng 					datal -= off - key.offset;
2577a22285a6SYan, Zheng 				}
2578a22285a6SYan, Zheng 
2579a22285a6SYan, Zheng 				ret = btrfs_drop_extents(trans, inode,
2580a22285a6SYan, Zheng 							 new_key.offset,
2581a22285a6SYan, Zheng 							 new_key.offset + datal,
2582a22285a6SYan, Zheng 							 &hint_byte, 1);
258379787eaaSJeff Mahoney 				if (ret) {
258479787eaaSJeff Mahoney 					btrfs_abort_transaction(trans, root,
258579787eaaSJeff Mahoney 								ret);
258679787eaaSJeff Mahoney 					btrfs_end_transaction(trans, root);
258779787eaaSJeff Mahoney 					goto out;
258879787eaaSJeff Mahoney 				}
2589a22285a6SYan, Zheng 
259031840ae1SZheng Yan 				ret = btrfs_insert_empty_item(trans, root, path,
259131840ae1SZheng Yan 							      &new_key, size);
259279787eaaSJeff Mahoney 				if (ret) {
259379787eaaSJeff Mahoney 					btrfs_abort_transaction(trans, root,
259479787eaaSJeff Mahoney 								ret);
259579787eaaSJeff Mahoney 					btrfs_end_transaction(trans, root);
259679787eaaSJeff Mahoney 					goto out;
259779787eaaSJeff Mahoney 				}
259831840ae1SZheng Yan 
259931840ae1SZheng Yan 				leaf = path->nodes[0];
260031840ae1SZheng Yan 				slot = path->slots[0];
260131840ae1SZheng Yan 				write_extent_buffer(leaf, buf,
260231840ae1SZheng Yan 					    btrfs_item_ptr_offset(leaf, slot),
260331840ae1SZheng Yan 					    size);
2604ae01a0abSYan Zheng 
2605f46b5a66SChristoph Hellwig 				extent = btrfs_item_ptr(leaf, slot,
2606f46b5a66SChristoph Hellwig 						struct btrfs_file_extent_item);
2607c5c9cd4dSSage Weil 
2608c5c9cd4dSSage Weil 				/* disko == 0 means it's a hole */
2609c5c9cd4dSSage Weil 				if (!disko)
2610c5c9cd4dSSage Weil 					datao = 0;
2611c5c9cd4dSSage Weil 
2612c5c9cd4dSSage Weil 				btrfs_set_file_extent_offset(leaf, extent,
2613c5c9cd4dSSage Weil 							     datao);
2614c5c9cd4dSSage Weil 				btrfs_set_file_extent_num_bytes(leaf, extent,
2615c5c9cd4dSSage Weil 								datal);
2616c5c9cd4dSSage Weil 				if (disko) {
2617c5c9cd4dSSage Weil 					inode_add_bytes(inode, datal);
2618ae01a0abSYan Zheng 					ret = btrfs_inc_extent_ref(trans, root,
26195d4f98a2SYan Zheng 							disko, diskl, 0,
2620f46b5a66SChristoph Hellwig 							root->root_key.objectid,
262133345d01SLi Zefan 							btrfs_ino(inode),
262266d7e7f0SArne Jansen 							new_key.offset - datao,
262366d7e7f0SArne Jansen 							0);
262479787eaaSJeff Mahoney 					if (ret) {
262579787eaaSJeff Mahoney 						btrfs_abort_transaction(trans,
262679787eaaSJeff Mahoney 									root,
262779787eaaSJeff Mahoney 									ret);
262879787eaaSJeff Mahoney 						btrfs_end_transaction(trans,
262979787eaaSJeff Mahoney 								      root);
263079787eaaSJeff Mahoney 						goto out;
263179787eaaSJeff Mahoney 
263279787eaaSJeff Mahoney 					}
2633f46b5a66SChristoph Hellwig 				}
2634c5c9cd4dSSage Weil 			} else if (type == BTRFS_FILE_EXTENT_INLINE) {
2635c5c9cd4dSSage Weil 				u64 skip = 0;
2636c5c9cd4dSSage Weil 				u64 trim = 0;
2637c5c9cd4dSSage Weil 				if (off > key.offset) {
2638c5c9cd4dSSage Weil 					skip = off - key.offset;
2639c5c9cd4dSSage Weil 					new_key.offset += skip;
264031840ae1SZheng Yan 				}
2641d397712bSChris Mason 
2642c5c9cd4dSSage Weil 				if (key.offset + datal > off+len)
2643c5c9cd4dSSage Weil 					trim = key.offset + datal - (off+len);
2644d397712bSChris Mason 
2645c5c9cd4dSSage Weil 				if (comp && (skip || trim)) {
2646c5c9cd4dSSage Weil 					ret = -EINVAL;
2647a22285a6SYan, Zheng 					btrfs_end_transaction(trans, root);
2648c5c9cd4dSSage Weil 					goto out;
264931840ae1SZheng Yan 				}
2650c5c9cd4dSSage Weil 				size -= skip + trim;
2651c5c9cd4dSSage Weil 				datal -= skip + trim;
2652a22285a6SYan, Zheng 
2653a22285a6SYan, Zheng 				ret = btrfs_drop_extents(trans, inode,
2654a22285a6SYan, Zheng 							 new_key.offset,
2655a22285a6SYan, Zheng 							 new_key.offset + datal,
2656a22285a6SYan, Zheng 							 &hint_byte, 1);
265779787eaaSJeff Mahoney 				if (ret) {
265879787eaaSJeff Mahoney 					btrfs_abort_transaction(trans, root,
265979787eaaSJeff Mahoney 								ret);
266079787eaaSJeff Mahoney 					btrfs_end_transaction(trans, root);
266179787eaaSJeff Mahoney 					goto out;
266279787eaaSJeff Mahoney 				}
2663a22285a6SYan, Zheng 
2664c5c9cd4dSSage Weil 				ret = btrfs_insert_empty_item(trans, root, path,
2665c5c9cd4dSSage Weil 							      &new_key, size);
266679787eaaSJeff Mahoney 				if (ret) {
266779787eaaSJeff Mahoney 					btrfs_abort_transaction(trans, root,
266879787eaaSJeff Mahoney 								ret);
266979787eaaSJeff Mahoney 					btrfs_end_transaction(trans, root);
267079787eaaSJeff Mahoney 					goto out;
267179787eaaSJeff Mahoney 				}
2672c5c9cd4dSSage Weil 
2673c5c9cd4dSSage Weil 				if (skip) {
2674d397712bSChris Mason 					u32 start =
2675d397712bSChris Mason 					  btrfs_file_extent_calc_inline_size(0);
2676c5c9cd4dSSage Weil 					memmove(buf+start, buf+start+skip,
2677c5c9cd4dSSage Weil 						datal);
2678c5c9cd4dSSage Weil 				}
2679c5c9cd4dSSage Weil 
2680c5c9cd4dSSage Weil 				leaf = path->nodes[0];
2681c5c9cd4dSSage Weil 				slot = path->slots[0];
2682c5c9cd4dSSage Weil 				write_extent_buffer(leaf, buf,
2683c5c9cd4dSSage Weil 					    btrfs_item_ptr_offset(leaf, slot),
2684c5c9cd4dSSage Weil 					    size);
2685c5c9cd4dSSage Weil 				inode_add_bytes(inode, datal);
2686c5c9cd4dSSage Weil 			}
2687c5c9cd4dSSage Weil 
2688c5c9cd4dSSage Weil 			btrfs_mark_buffer_dirty(leaf);
2689b3b4aa74SDavid Sterba 			btrfs_release_path(path);
2690c5c9cd4dSSage Weil 
26910c4d2d95SJosef Bacik 			inode_inc_iversion(inode);
2692a22285a6SYan, Zheng 			inode->i_mtime = inode->i_ctime = CURRENT_TIME;
2693b5384d48SSage Weil 
2694b5384d48SSage Weil 			/*
2695b5384d48SSage Weil 			 * we round up to the block size at eof when
2696b5384d48SSage Weil 			 * determining which extents to clone above,
2697b5384d48SSage Weil 			 * but shouldn't round up the file size
2698b5384d48SSage Weil 			 */
2699b5384d48SSage Weil 			endoff = new_key.offset + datal;
27005f3888ffSLi Zefan 			if (endoff > destoff+olen)
27015f3888ffSLi Zefan 				endoff = destoff+olen;
2702b5384d48SSage Weil 			if (endoff > inode->i_size)
2703b5384d48SSage Weil 				btrfs_i_size_write(inode, endoff);
2704b5384d48SSage Weil 
2705a22285a6SYan, Zheng 			ret = btrfs_update_inode(trans, root, inode);
270679787eaaSJeff Mahoney 			if (ret) {
270779787eaaSJeff Mahoney 				btrfs_abort_transaction(trans, root, ret);
2708a22285a6SYan, Zheng 				btrfs_end_transaction(trans, root);
270979787eaaSJeff Mahoney 				goto out;
271079787eaaSJeff Mahoney 			}
271179787eaaSJeff Mahoney 			ret = btrfs_end_transaction(trans, root);
2712a22285a6SYan, Zheng 		}
2713c5c9cd4dSSage Weil next:
2714b3b4aa74SDavid Sterba 		btrfs_release_path(path);
2715ae01a0abSYan Zheng 		key.offset++;
2716ae01a0abSYan Zheng 	}
2717f46b5a66SChristoph Hellwig 	ret = 0;
2718f46b5a66SChristoph Hellwig out:
2719b3b4aa74SDavid Sterba 	btrfs_release_path(path);
2720d0082371SJeff Mahoney 	unlock_extent(&BTRFS_I(src)->io_tree, off, off+len);
2721f46b5a66SChristoph Hellwig out_unlock:
2722f46b5a66SChristoph Hellwig 	mutex_unlock(&src->i_mutex);
2723f46b5a66SChristoph Hellwig 	mutex_unlock(&inode->i_mutex);
2724ae01a0abSYan Zheng 	vfree(buf);
2725ae01a0abSYan Zheng 	btrfs_free_path(path);
2726f46b5a66SChristoph Hellwig out_fput:
2727f46b5a66SChristoph Hellwig 	fput(src_file);
2728ab67b7c1SYan Zheng out_drop_write:
27292a79f17eSAl Viro 	mnt_drop_write_file(file);
2730f46b5a66SChristoph Hellwig 	return ret;
2731f46b5a66SChristoph Hellwig }
2732f46b5a66SChristoph Hellwig 
27337a865e8aSChristoph Hellwig static long btrfs_ioctl_clone_range(struct file *file, void __user *argp)
2734c5c9cd4dSSage Weil {
2735c5c9cd4dSSage Weil 	struct btrfs_ioctl_clone_range_args args;
2736c5c9cd4dSSage Weil 
27377a865e8aSChristoph Hellwig 	if (copy_from_user(&args, argp, sizeof(args)))
2738c5c9cd4dSSage Weil 		return -EFAULT;
2739c5c9cd4dSSage Weil 	return btrfs_ioctl_clone(file, args.src_fd, args.src_offset,
2740c5c9cd4dSSage Weil 				 args.src_length, args.dest_offset);
2741c5c9cd4dSSage Weil }
2742c5c9cd4dSSage Weil 
2743f46b5a66SChristoph Hellwig /*
2744f46b5a66SChristoph Hellwig  * there are many ways the trans_start and trans_end ioctls can lead
2745f46b5a66SChristoph Hellwig  * to deadlocks.  They should only be used by applications that
2746f46b5a66SChristoph Hellwig  * basically own the machine, and have a very in depth understanding
2747f46b5a66SChristoph Hellwig  * of all the possible deadlocks and enospc problems.
2748f46b5a66SChristoph Hellwig  */
2749b2950863SChristoph Hellwig static long btrfs_ioctl_trans_start(struct file *file)
2750f46b5a66SChristoph Hellwig {
2751f46b5a66SChristoph Hellwig 	struct inode *inode = fdentry(file)->d_inode;
2752f46b5a66SChristoph Hellwig 	struct btrfs_root *root = BTRFS_I(inode)->root;
2753f46b5a66SChristoph Hellwig 	struct btrfs_trans_handle *trans;
27541ab86aedSSage Weil 	int ret;
2755f46b5a66SChristoph Hellwig 
27561ab86aedSSage Weil 	ret = -EPERM;
2757df5b5520SChristoph Hellwig 	if (!capable(CAP_SYS_ADMIN))
2758f46b5a66SChristoph Hellwig 		goto out;
27591ab86aedSSage Weil 
27601ab86aedSSage Weil 	ret = -EINPROGRESS;
27611ab86aedSSage Weil 	if (file->private_data)
27621ab86aedSSage Weil 		goto out;
27639ca9ee09SSage Weil 
2764b83cc969SLi Zefan 	ret = -EROFS;
2765b83cc969SLi Zefan 	if (btrfs_root_readonly(root))
2766b83cc969SLi Zefan 		goto out;
2767b83cc969SLi Zefan 
2768a561be71SAl Viro 	ret = mnt_want_write_file(file);
2769c146afadSYan Zheng 	if (ret)
2770c146afadSYan Zheng 		goto out;
2771c146afadSYan Zheng 
2772a4abeea4SJosef Bacik 	atomic_inc(&root->fs_info->open_ioctl_trans);
27739ca9ee09SSage Weil 
2774f46b5a66SChristoph Hellwig 	ret = -ENOMEM;
27757a7eaa40SJosef Bacik 	trans = btrfs_start_ioctl_transaction(root);
2776abd30bb0STsutomu Itoh 	if (IS_ERR(trans))
27771ab86aedSSage Weil 		goto out_drop;
27781ab86aedSSage Weil 
27791ab86aedSSage Weil 	file->private_data = trans;
27801ab86aedSSage Weil 	return 0;
27811ab86aedSSage Weil 
27821ab86aedSSage Weil out_drop:
2783a4abeea4SJosef Bacik 	atomic_dec(&root->fs_info->open_ioctl_trans);
27842a79f17eSAl Viro 	mnt_drop_write_file(file);
2785f46b5a66SChristoph Hellwig out:
2786f46b5a66SChristoph Hellwig 	return ret;
2787f46b5a66SChristoph Hellwig }
2788f46b5a66SChristoph Hellwig 
27896ef5ed0dSJosef Bacik static long btrfs_ioctl_default_subvol(struct file *file, void __user *argp)
27906ef5ed0dSJosef Bacik {
27916ef5ed0dSJosef Bacik 	struct inode *inode = fdentry(file)->d_inode;
27926ef5ed0dSJosef Bacik 	struct btrfs_root *root = BTRFS_I(inode)->root;
27936ef5ed0dSJosef Bacik 	struct btrfs_root *new_root;
27946ef5ed0dSJosef Bacik 	struct btrfs_dir_item *di;
27956ef5ed0dSJosef Bacik 	struct btrfs_trans_handle *trans;
27966ef5ed0dSJosef Bacik 	struct btrfs_path *path;
27976ef5ed0dSJosef Bacik 	struct btrfs_key location;
27986ef5ed0dSJosef Bacik 	struct btrfs_disk_key disk_key;
27996ef5ed0dSJosef Bacik 	u64 objectid = 0;
28006ef5ed0dSJosef Bacik 	u64 dir_id;
28016ef5ed0dSJosef Bacik 
28026ef5ed0dSJosef Bacik 	if (!capable(CAP_SYS_ADMIN))
28036ef5ed0dSJosef Bacik 		return -EPERM;
28046ef5ed0dSJosef Bacik 
28056ef5ed0dSJosef Bacik 	if (copy_from_user(&objectid, argp, sizeof(objectid)))
28066ef5ed0dSJosef Bacik 		return -EFAULT;
28076ef5ed0dSJosef Bacik 
28086ef5ed0dSJosef Bacik 	if (!objectid)
28096ef5ed0dSJosef Bacik 		objectid = root->root_key.objectid;
28106ef5ed0dSJosef Bacik 
28116ef5ed0dSJosef Bacik 	location.objectid = objectid;
28126ef5ed0dSJosef Bacik 	location.type = BTRFS_ROOT_ITEM_KEY;
28136ef5ed0dSJosef Bacik 	location.offset = (u64)-1;
28146ef5ed0dSJosef Bacik 
28156ef5ed0dSJosef Bacik 	new_root = btrfs_read_fs_root_no_name(root->fs_info, &location);
28166ef5ed0dSJosef Bacik 	if (IS_ERR(new_root))
28176ef5ed0dSJosef Bacik 		return PTR_ERR(new_root);
28186ef5ed0dSJosef Bacik 
28196ef5ed0dSJosef Bacik 	if (btrfs_root_refs(&new_root->root_item) == 0)
28206ef5ed0dSJosef Bacik 		return -ENOENT;
28216ef5ed0dSJosef Bacik 
28226ef5ed0dSJosef Bacik 	path = btrfs_alloc_path();
28236ef5ed0dSJosef Bacik 	if (!path)
28246ef5ed0dSJosef Bacik 		return -ENOMEM;
28256ef5ed0dSJosef Bacik 	path->leave_spinning = 1;
28266ef5ed0dSJosef Bacik 
28276ef5ed0dSJosef Bacik 	trans = btrfs_start_transaction(root, 1);
282898d5dc13STsutomu Itoh 	if (IS_ERR(trans)) {
28296ef5ed0dSJosef Bacik 		btrfs_free_path(path);
283098d5dc13STsutomu Itoh 		return PTR_ERR(trans);
28316ef5ed0dSJosef Bacik 	}
28326ef5ed0dSJosef Bacik 
28336c41761fSDavid Sterba 	dir_id = btrfs_super_root_dir(root->fs_info->super_copy);
28346ef5ed0dSJosef Bacik 	di = btrfs_lookup_dir_item(trans, root->fs_info->tree_root, path,
28356ef5ed0dSJosef Bacik 				   dir_id, "default", 7, 1);
2836cf1e99a4SDan Carpenter 	if (IS_ERR_OR_NULL(di)) {
28376ef5ed0dSJosef Bacik 		btrfs_free_path(path);
28386ef5ed0dSJosef Bacik 		btrfs_end_transaction(trans, root);
28396ef5ed0dSJosef Bacik 		printk(KERN_ERR "Umm, you don't have the default dir item, "
28406ef5ed0dSJosef Bacik 		       "this isn't going to work\n");
28416ef5ed0dSJosef Bacik 		return -ENOENT;
28426ef5ed0dSJosef Bacik 	}
28436ef5ed0dSJosef Bacik 
28446ef5ed0dSJosef Bacik 	btrfs_cpu_key_to_disk(&disk_key, &new_root->root_key);
28456ef5ed0dSJosef Bacik 	btrfs_set_dir_item_key(path->nodes[0], di, &disk_key);
28466ef5ed0dSJosef Bacik 	btrfs_mark_buffer_dirty(path->nodes[0]);
28476ef5ed0dSJosef Bacik 	btrfs_free_path(path);
28486ef5ed0dSJosef Bacik 
28492b0ce2c2SMitch Harder 	btrfs_set_fs_incompat(root->fs_info, DEFAULT_SUBVOL);
28506ef5ed0dSJosef Bacik 	btrfs_end_transaction(trans, root);
28516ef5ed0dSJosef Bacik 
28526ef5ed0dSJosef Bacik 	return 0;
28536ef5ed0dSJosef Bacik }
28546ef5ed0dSJosef Bacik 
2855bf5fc093SJosef Bacik static void get_block_group_info(struct list_head *groups_list,
2856bf5fc093SJosef Bacik 				 struct btrfs_ioctl_space_info *space)
2857bf5fc093SJosef Bacik {
2858bf5fc093SJosef Bacik 	struct btrfs_block_group_cache *block_group;
2859bf5fc093SJosef Bacik 
2860bf5fc093SJosef Bacik 	space->total_bytes = 0;
2861bf5fc093SJosef Bacik 	space->used_bytes = 0;
2862bf5fc093SJosef Bacik 	space->flags = 0;
2863bf5fc093SJosef Bacik 	list_for_each_entry(block_group, groups_list, list) {
2864bf5fc093SJosef Bacik 		space->flags = block_group->flags;
2865bf5fc093SJosef Bacik 		space->total_bytes += block_group->key.offset;
2866bf5fc093SJosef Bacik 		space->used_bytes +=
2867bf5fc093SJosef Bacik 			btrfs_block_group_used(&block_group->item);
2868bf5fc093SJosef Bacik 	}
2869bf5fc093SJosef Bacik }
2870bf5fc093SJosef Bacik 
28711406e432SJosef Bacik long btrfs_ioctl_space_info(struct btrfs_root *root, void __user *arg)
28721406e432SJosef Bacik {
28731406e432SJosef Bacik 	struct btrfs_ioctl_space_args space_args;
28741406e432SJosef Bacik 	struct btrfs_ioctl_space_info space;
28751406e432SJosef Bacik 	struct btrfs_ioctl_space_info *dest;
28767fde62bfSChris Mason 	struct btrfs_ioctl_space_info *dest_orig;
287713f2696fSDaniel J Blueman 	struct btrfs_ioctl_space_info __user *user_dest;
28781406e432SJosef Bacik 	struct btrfs_space_info *info;
2879bf5fc093SJosef Bacik 	u64 types[] = {BTRFS_BLOCK_GROUP_DATA,
2880bf5fc093SJosef Bacik 		       BTRFS_BLOCK_GROUP_SYSTEM,
2881bf5fc093SJosef Bacik 		       BTRFS_BLOCK_GROUP_METADATA,
2882bf5fc093SJosef Bacik 		       BTRFS_BLOCK_GROUP_DATA | BTRFS_BLOCK_GROUP_METADATA};
2883bf5fc093SJosef Bacik 	int num_types = 4;
28847fde62bfSChris Mason 	int alloc_size;
28851406e432SJosef Bacik 	int ret = 0;
288651788b1bSDan Rosenberg 	u64 slot_count = 0;
2887bf5fc093SJosef Bacik 	int i, c;
28881406e432SJosef Bacik 
28891406e432SJosef Bacik 	if (copy_from_user(&space_args,
28901406e432SJosef Bacik 			   (struct btrfs_ioctl_space_args __user *)arg,
28911406e432SJosef Bacik 			   sizeof(space_args)))
28921406e432SJosef Bacik 		return -EFAULT;
28931406e432SJosef Bacik 
2894bf5fc093SJosef Bacik 	for (i = 0; i < num_types; i++) {
2895bf5fc093SJosef Bacik 		struct btrfs_space_info *tmp;
2896bf5fc093SJosef Bacik 
2897bf5fc093SJosef Bacik 		info = NULL;
28987fde62bfSChris Mason 		rcu_read_lock();
2899bf5fc093SJosef Bacik 		list_for_each_entry_rcu(tmp, &root->fs_info->space_info,
2900bf5fc093SJosef Bacik 					list) {
2901bf5fc093SJosef Bacik 			if (tmp->flags == types[i]) {
2902bf5fc093SJosef Bacik 				info = tmp;
2903bf5fc093SJosef Bacik 				break;
2904bf5fc093SJosef Bacik 			}
2905bf5fc093SJosef Bacik 		}
29067fde62bfSChris Mason 		rcu_read_unlock();
29071406e432SJosef Bacik 
2908bf5fc093SJosef Bacik 		if (!info)
2909bf5fc093SJosef Bacik 			continue;
2910bf5fc093SJosef Bacik 
2911bf5fc093SJosef Bacik 		down_read(&info->groups_sem);
2912bf5fc093SJosef Bacik 		for (c = 0; c < BTRFS_NR_RAID_TYPES; c++) {
2913bf5fc093SJosef Bacik 			if (!list_empty(&info->block_groups[c]))
2914bf5fc093SJosef Bacik 				slot_count++;
2915bf5fc093SJosef Bacik 		}
2916bf5fc093SJosef Bacik 		up_read(&info->groups_sem);
2917bf5fc093SJosef Bacik 	}
2918bf5fc093SJosef Bacik 
29197fde62bfSChris Mason 	/* space_slots == 0 means they are asking for a count */
29207fde62bfSChris Mason 	if (space_args.space_slots == 0) {
29217fde62bfSChris Mason 		space_args.total_spaces = slot_count;
29227fde62bfSChris Mason 		goto out;
29237fde62bfSChris Mason 	}
2924bf5fc093SJosef Bacik 
292551788b1bSDan Rosenberg 	slot_count = min_t(u64, space_args.space_slots, slot_count);
2926bf5fc093SJosef Bacik 
29277fde62bfSChris Mason 	alloc_size = sizeof(*dest) * slot_count;
2928bf5fc093SJosef Bacik 
29297fde62bfSChris Mason 	/* we generally have at most 6 or so space infos, one for each raid
29307fde62bfSChris Mason 	 * level.  So, a whole page should be more than enough for everyone
29317fde62bfSChris Mason 	 */
29327fde62bfSChris Mason 	if (alloc_size > PAGE_CACHE_SIZE)
29337fde62bfSChris Mason 		return -ENOMEM;
29347fde62bfSChris Mason 
29357fde62bfSChris Mason 	space_args.total_spaces = 0;
29367fde62bfSChris Mason 	dest = kmalloc(alloc_size, GFP_NOFS);
29377fde62bfSChris Mason 	if (!dest)
29387fde62bfSChris Mason 		return -ENOMEM;
29397fde62bfSChris Mason 	dest_orig = dest;
29407fde62bfSChris Mason 
29417fde62bfSChris Mason 	/* now we have a buffer to copy into */
2942bf5fc093SJosef Bacik 	for (i = 0; i < num_types; i++) {
2943bf5fc093SJosef Bacik 		struct btrfs_space_info *tmp;
2944bf5fc093SJosef Bacik 
294551788b1bSDan Rosenberg 		if (!slot_count)
294651788b1bSDan Rosenberg 			break;
294751788b1bSDan Rosenberg 
2948bf5fc093SJosef Bacik 		info = NULL;
29491406e432SJosef Bacik 		rcu_read_lock();
2950bf5fc093SJosef Bacik 		list_for_each_entry_rcu(tmp, &root->fs_info->space_info,
2951bf5fc093SJosef Bacik 					list) {
2952bf5fc093SJosef Bacik 			if (tmp->flags == types[i]) {
2953bf5fc093SJosef Bacik 				info = tmp;
29547fde62bfSChris Mason 				break;
2955bf5fc093SJosef Bacik 			}
2956bf5fc093SJosef Bacik 		}
2957bf5fc093SJosef Bacik 		rcu_read_unlock();
29587fde62bfSChris Mason 
2959bf5fc093SJosef Bacik 		if (!info)
2960bf5fc093SJosef Bacik 			continue;
2961bf5fc093SJosef Bacik 		down_read(&info->groups_sem);
2962bf5fc093SJosef Bacik 		for (c = 0; c < BTRFS_NR_RAID_TYPES; c++) {
2963bf5fc093SJosef Bacik 			if (!list_empty(&info->block_groups[c])) {
2964bf5fc093SJosef Bacik 				get_block_group_info(&info->block_groups[c],
2965bf5fc093SJosef Bacik 						     &space);
29667fde62bfSChris Mason 				memcpy(dest, &space, sizeof(space));
29671406e432SJosef Bacik 				dest++;
29681406e432SJosef Bacik 				space_args.total_spaces++;
296951788b1bSDan Rosenberg 				slot_count--;
29701406e432SJosef Bacik 			}
297151788b1bSDan Rosenberg 			if (!slot_count)
297251788b1bSDan Rosenberg 				break;
2973bf5fc093SJosef Bacik 		}
2974bf5fc093SJosef Bacik 		up_read(&info->groups_sem);
2975bf5fc093SJosef Bacik 	}
29761406e432SJosef Bacik 
29772eec6c81SDaniel J Blueman 	user_dest = (struct btrfs_ioctl_space_info __user *)
29787fde62bfSChris Mason 		(arg + sizeof(struct btrfs_ioctl_space_args));
29797fde62bfSChris Mason 
29807fde62bfSChris Mason 	if (copy_to_user(user_dest, dest_orig, alloc_size))
29817fde62bfSChris Mason 		ret = -EFAULT;
29827fde62bfSChris Mason 
29837fde62bfSChris Mason 	kfree(dest_orig);
29847fde62bfSChris Mason out:
29857fde62bfSChris Mason 	if (ret == 0 && copy_to_user(arg, &space_args, sizeof(space_args)))
29861406e432SJosef Bacik 		ret = -EFAULT;
29871406e432SJosef Bacik 
29881406e432SJosef Bacik 	return ret;
29891406e432SJosef Bacik }
29901406e432SJosef Bacik 
2991f46b5a66SChristoph Hellwig /*
2992f46b5a66SChristoph Hellwig  * there are many ways the trans_start and trans_end ioctls can lead
2993f46b5a66SChristoph Hellwig  * to deadlocks.  They should only be used by applications that
2994f46b5a66SChristoph Hellwig  * basically own the machine, and have a very in depth understanding
2995f46b5a66SChristoph Hellwig  * of all the possible deadlocks and enospc problems.
2996f46b5a66SChristoph Hellwig  */
2997f46b5a66SChristoph Hellwig long btrfs_ioctl_trans_end(struct file *file)
2998f46b5a66SChristoph Hellwig {
2999f46b5a66SChristoph Hellwig 	struct inode *inode = fdentry(file)->d_inode;
3000f46b5a66SChristoph Hellwig 	struct btrfs_root *root = BTRFS_I(inode)->root;
3001f46b5a66SChristoph Hellwig 	struct btrfs_trans_handle *trans;
3002f46b5a66SChristoph Hellwig 
3003f46b5a66SChristoph Hellwig 	trans = file->private_data;
30041ab86aedSSage Weil 	if (!trans)
30051ab86aedSSage Weil 		return -EINVAL;
3006b214107eSChristoph Hellwig 	file->private_data = NULL;
30079ca9ee09SSage Weil 
30081ab86aedSSage Weil 	btrfs_end_transaction(trans, root);
30091ab86aedSSage Weil 
3010a4abeea4SJosef Bacik 	atomic_dec(&root->fs_info->open_ioctl_trans);
30119ca9ee09SSage Weil 
30122a79f17eSAl Viro 	mnt_drop_write_file(file);
30131ab86aedSSage Weil 	return 0;
3014f46b5a66SChristoph Hellwig }
3015f46b5a66SChristoph Hellwig 
301646204592SSage Weil static noinline long btrfs_ioctl_start_sync(struct file *file, void __user *argp)
301746204592SSage Weil {
301846204592SSage Weil 	struct btrfs_root *root = BTRFS_I(file->f_dentry->d_inode)->root;
301946204592SSage Weil 	struct btrfs_trans_handle *trans;
302046204592SSage Weil 	u64 transid;
3021db5b493aSTsutomu Itoh 	int ret;
302246204592SSage Weil 
302346204592SSage Weil 	trans = btrfs_start_transaction(root, 0);
302498d5dc13STsutomu Itoh 	if (IS_ERR(trans))
302598d5dc13STsutomu Itoh 		return PTR_ERR(trans);
302646204592SSage Weil 	transid = trans->transid;
3027db5b493aSTsutomu Itoh 	ret = btrfs_commit_transaction_async(trans, root, 0);
30288b2b2d3cSTsutomu Itoh 	if (ret) {
30298b2b2d3cSTsutomu Itoh 		btrfs_end_transaction(trans, root);
3030db5b493aSTsutomu Itoh 		return ret;
30318b2b2d3cSTsutomu Itoh 	}
303246204592SSage Weil 
303346204592SSage Weil 	if (argp)
303446204592SSage Weil 		if (copy_to_user(argp, &transid, sizeof(transid)))
303546204592SSage Weil 			return -EFAULT;
303646204592SSage Weil 	return 0;
303746204592SSage Weil }
303846204592SSage Weil 
303946204592SSage Weil static noinline long btrfs_ioctl_wait_sync(struct file *file, void __user *argp)
304046204592SSage Weil {
304146204592SSage Weil 	struct btrfs_root *root = BTRFS_I(file->f_dentry->d_inode)->root;
304246204592SSage Weil 	u64 transid;
304346204592SSage Weil 
304446204592SSage Weil 	if (argp) {
304546204592SSage Weil 		if (copy_from_user(&transid, argp, sizeof(transid)))
304646204592SSage Weil 			return -EFAULT;
304746204592SSage Weil 	} else {
304846204592SSage Weil 		transid = 0;  /* current trans */
304946204592SSage Weil 	}
305046204592SSage Weil 	return btrfs_wait_for_commit(root, transid);
305146204592SSage Weil }
305246204592SSage Weil 
3053475f6387SJan Schmidt static long btrfs_ioctl_scrub(struct btrfs_root *root, void __user *arg)
3054475f6387SJan Schmidt {
3055475f6387SJan Schmidt 	int ret;
3056475f6387SJan Schmidt 	struct btrfs_ioctl_scrub_args *sa;
3057475f6387SJan Schmidt 
3058475f6387SJan Schmidt 	if (!capable(CAP_SYS_ADMIN))
3059475f6387SJan Schmidt 		return -EPERM;
3060475f6387SJan Schmidt 
3061475f6387SJan Schmidt 	sa = memdup_user(arg, sizeof(*sa));
3062475f6387SJan Schmidt 	if (IS_ERR(sa))
3063475f6387SJan Schmidt 		return PTR_ERR(sa);
3064475f6387SJan Schmidt 
3065475f6387SJan Schmidt 	ret = btrfs_scrub_dev(root, sa->devid, sa->start, sa->end,
30668628764eSArne Jansen 			      &sa->progress, sa->flags & BTRFS_SCRUB_READONLY);
3067475f6387SJan Schmidt 
3068475f6387SJan Schmidt 	if (copy_to_user(arg, sa, sizeof(*sa)))
3069475f6387SJan Schmidt 		ret = -EFAULT;
3070475f6387SJan Schmidt 
3071475f6387SJan Schmidt 	kfree(sa);
3072475f6387SJan Schmidt 	return ret;
3073475f6387SJan Schmidt }
3074475f6387SJan Schmidt 
3075475f6387SJan Schmidt static long btrfs_ioctl_scrub_cancel(struct btrfs_root *root, void __user *arg)
3076475f6387SJan Schmidt {
3077475f6387SJan Schmidt 	if (!capable(CAP_SYS_ADMIN))
3078475f6387SJan Schmidt 		return -EPERM;
3079475f6387SJan Schmidt 
3080475f6387SJan Schmidt 	return btrfs_scrub_cancel(root);
3081475f6387SJan Schmidt }
3082475f6387SJan Schmidt 
3083475f6387SJan Schmidt static long btrfs_ioctl_scrub_progress(struct btrfs_root *root,
3084475f6387SJan Schmidt 				       void __user *arg)
3085475f6387SJan Schmidt {
3086475f6387SJan Schmidt 	struct btrfs_ioctl_scrub_args *sa;
3087475f6387SJan Schmidt 	int ret;
3088475f6387SJan Schmidt 
3089475f6387SJan Schmidt 	if (!capable(CAP_SYS_ADMIN))
3090475f6387SJan Schmidt 		return -EPERM;
3091475f6387SJan Schmidt 
3092475f6387SJan Schmidt 	sa = memdup_user(arg, sizeof(*sa));
3093475f6387SJan Schmidt 	if (IS_ERR(sa))
3094475f6387SJan Schmidt 		return PTR_ERR(sa);
3095475f6387SJan Schmidt 
3096475f6387SJan Schmidt 	ret = btrfs_scrub_progress(root, sa->devid, &sa->progress);
3097475f6387SJan Schmidt 
3098475f6387SJan Schmidt 	if (copy_to_user(arg, sa, sizeof(*sa)))
3099475f6387SJan Schmidt 		ret = -EFAULT;
3100475f6387SJan Schmidt 
3101475f6387SJan Schmidt 	kfree(sa);
3102475f6387SJan Schmidt 	return ret;
3103475f6387SJan Schmidt }
3104475f6387SJan Schmidt 
3105c11d2c23SStefan Behrens static long btrfs_ioctl_get_dev_stats(struct btrfs_root *root,
3106b27f7c0cSDavid Sterba 				      void __user *arg)
3107c11d2c23SStefan Behrens {
3108c11d2c23SStefan Behrens 	struct btrfs_ioctl_get_dev_stats *sa;
3109c11d2c23SStefan Behrens 	int ret;
3110c11d2c23SStefan Behrens 
3111c11d2c23SStefan Behrens 	sa = memdup_user(arg, sizeof(*sa));
3112c11d2c23SStefan Behrens 	if (IS_ERR(sa))
3113c11d2c23SStefan Behrens 		return PTR_ERR(sa);
3114c11d2c23SStefan Behrens 
3115b27f7c0cSDavid Sterba 	if ((sa->flags & BTRFS_DEV_STATS_RESET) && !capable(CAP_SYS_ADMIN)) {
3116b27f7c0cSDavid Sterba 		kfree(sa);
3117b27f7c0cSDavid Sterba 		return -EPERM;
3118b27f7c0cSDavid Sterba 	}
3119b27f7c0cSDavid Sterba 
3120b27f7c0cSDavid Sterba 	ret = btrfs_get_dev_stats(root, sa);
3121c11d2c23SStefan Behrens 
3122c11d2c23SStefan Behrens 	if (copy_to_user(arg, sa, sizeof(*sa)))
3123c11d2c23SStefan Behrens 		ret = -EFAULT;
3124c11d2c23SStefan Behrens 
3125c11d2c23SStefan Behrens 	kfree(sa);
3126c11d2c23SStefan Behrens 	return ret;
3127c11d2c23SStefan Behrens }
3128c11d2c23SStefan Behrens 
3129d7728c96SJan Schmidt static long btrfs_ioctl_ino_to_path(struct btrfs_root *root, void __user *arg)
3130d7728c96SJan Schmidt {
3131d7728c96SJan Schmidt 	int ret = 0;
3132d7728c96SJan Schmidt 	int i;
3133740c3d22SChris Mason 	u64 rel_ptr;
3134d7728c96SJan Schmidt 	int size;
3135806468f8SChris Mason 	struct btrfs_ioctl_ino_path_args *ipa = NULL;
3136d7728c96SJan Schmidt 	struct inode_fs_paths *ipath = NULL;
3137d7728c96SJan Schmidt 	struct btrfs_path *path;
3138d7728c96SJan Schmidt 
3139d7728c96SJan Schmidt 	if (!capable(CAP_SYS_ADMIN))
3140d7728c96SJan Schmidt 		return -EPERM;
3141d7728c96SJan Schmidt 
3142d7728c96SJan Schmidt 	path = btrfs_alloc_path();
3143d7728c96SJan Schmidt 	if (!path) {
3144d7728c96SJan Schmidt 		ret = -ENOMEM;
3145d7728c96SJan Schmidt 		goto out;
3146d7728c96SJan Schmidt 	}
3147d7728c96SJan Schmidt 
3148d7728c96SJan Schmidt 	ipa = memdup_user(arg, sizeof(*ipa));
3149d7728c96SJan Schmidt 	if (IS_ERR(ipa)) {
3150d7728c96SJan Schmidt 		ret = PTR_ERR(ipa);
3151d7728c96SJan Schmidt 		ipa = NULL;
3152d7728c96SJan Schmidt 		goto out;
3153d7728c96SJan Schmidt 	}
3154d7728c96SJan Schmidt 
3155d7728c96SJan Schmidt 	size = min_t(u32, ipa->size, 4096);
3156d7728c96SJan Schmidt 	ipath = init_ipath(size, root, path);
3157d7728c96SJan Schmidt 	if (IS_ERR(ipath)) {
3158d7728c96SJan Schmidt 		ret = PTR_ERR(ipath);
3159d7728c96SJan Schmidt 		ipath = NULL;
3160d7728c96SJan Schmidt 		goto out;
3161d7728c96SJan Schmidt 	}
3162d7728c96SJan Schmidt 
3163d7728c96SJan Schmidt 	ret = paths_from_inode(ipa->inum, ipath);
3164d7728c96SJan Schmidt 	if (ret < 0)
3165d7728c96SJan Schmidt 		goto out;
3166d7728c96SJan Schmidt 
3167d7728c96SJan Schmidt 	for (i = 0; i < ipath->fspath->elem_cnt; ++i) {
3168745c4d8eSJeff Mahoney 		rel_ptr = ipath->fspath->val[i] -
3169745c4d8eSJeff Mahoney 			  (u64)(unsigned long)ipath->fspath->val;
3170740c3d22SChris Mason 		ipath->fspath->val[i] = rel_ptr;
3171d7728c96SJan Schmidt 	}
3172d7728c96SJan Schmidt 
3173745c4d8eSJeff Mahoney 	ret = copy_to_user((void *)(unsigned long)ipa->fspath,
3174745c4d8eSJeff Mahoney 			   (void *)(unsigned long)ipath->fspath, size);
3175d7728c96SJan Schmidt 	if (ret) {
3176d7728c96SJan Schmidt 		ret = -EFAULT;
3177d7728c96SJan Schmidt 		goto out;
3178d7728c96SJan Schmidt 	}
3179d7728c96SJan Schmidt 
3180d7728c96SJan Schmidt out:
3181d7728c96SJan Schmidt 	btrfs_free_path(path);
3182d7728c96SJan Schmidt 	free_ipath(ipath);
3183d7728c96SJan Schmidt 	kfree(ipa);
3184d7728c96SJan Schmidt 
3185d7728c96SJan Schmidt 	return ret;
3186d7728c96SJan Schmidt }
3187d7728c96SJan Schmidt 
3188d7728c96SJan Schmidt static int build_ino_list(u64 inum, u64 offset, u64 root, void *ctx)
3189d7728c96SJan Schmidt {
3190d7728c96SJan Schmidt 	struct btrfs_data_container *inodes = ctx;
3191d7728c96SJan Schmidt 	const size_t c = 3 * sizeof(u64);
3192d7728c96SJan Schmidt 
3193d7728c96SJan Schmidt 	if (inodes->bytes_left >= c) {
3194d7728c96SJan Schmidt 		inodes->bytes_left -= c;
3195d7728c96SJan Schmidt 		inodes->val[inodes->elem_cnt] = inum;
3196d7728c96SJan Schmidt 		inodes->val[inodes->elem_cnt + 1] = offset;
3197d7728c96SJan Schmidt 		inodes->val[inodes->elem_cnt + 2] = root;
3198d7728c96SJan Schmidt 		inodes->elem_cnt += 3;
3199d7728c96SJan Schmidt 	} else {
3200d7728c96SJan Schmidt 		inodes->bytes_missing += c - inodes->bytes_left;
3201d7728c96SJan Schmidt 		inodes->bytes_left = 0;
3202d7728c96SJan Schmidt 		inodes->elem_missed += 3;
3203d7728c96SJan Schmidt 	}
3204d7728c96SJan Schmidt 
3205d7728c96SJan Schmidt 	return 0;
3206d7728c96SJan Schmidt }
3207d7728c96SJan Schmidt 
3208d7728c96SJan Schmidt static long btrfs_ioctl_logical_to_ino(struct btrfs_root *root,
3209d7728c96SJan Schmidt 					void __user *arg)
3210d7728c96SJan Schmidt {
3211d7728c96SJan Schmidt 	int ret = 0;
3212d7728c96SJan Schmidt 	int size;
32134692cf58SJan Schmidt 	u64 extent_item_pos;
3214d7728c96SJan Schmidt 	struct btrfs_ioctl_logical_ino_args *loi;
3215d7728c96SJan Schmidt 	struct btrfs_data_container *inodes = NULL;
3216d7728c96SJan Schmidt 	struct btrfs_path *path = NULL;
3217d7728c96SJan Schmidt 	struct btrfs_key key;
3218d7728c96SJan Schmidt 
3219d7728c96SJan Schmidt 	if (!capable(CAP_SYS_ADMIN))
3220d7728c96SJan Schmidt 		return -EPERM;
3221d7728c96SJan Schmidt 
3222d7728c96SJan Schmidt 	loi = memdup_user(arg, sizeof(*loi));
3223d7728c96SJan Schmidt 	if (IS_ERR(loi)) {
3224d7728c96SJan Schmidt 		ret = PTR_ERR(loi);
3225d7728c96SJan Schmidt 		loi = NULL;
3226d7728c96SJan Schmidt 		goto out;
3227d7728c96SJan Schmidt 	}
3228d7728c96SJan Schmidt 
3229d7728c96SJan Schmidt 	path = btrfs_alloc_path();
3230d7728c96SJan Schmidt 	if (!path) {
3231d7728c96SJan Schmidt 		ret = -ENOMEM;
3232d7728c96SJan Schmidt 		goto out;
3233d7728c96SJan Schmidt 	}
3234d7728c96SJan Schmidt 
3235d7728c96SJan Schmidt 	size = min_t(u32, loi->size, 4096);
3236d7728c96SJan Schmidt 	inodes = init_data_container(size);
3237d7728c96SJan Schmidt 	if (IS_ERR(inodes)) {
3238d7728c96SJan Schmidt 		ret = PTR_ERR(inodes);
3239d7728c96SJan Schmidt 		inodes = NULL;
3240d7728c96SJan Schmidt 		goto out;
3241d7728c96SJan Schmidt 	}
3242d7728c96SJan Schmidt 
3243d7728c96SJan Schmidt 	ret = extent_from_logical(root->fs_info, loi->logical, path, &key);
32444692cf58SJan Schmidt 	btrfs_release_path(path);
3245d7728c96SJan Schmidt 
3246d7728c96SJan Schmidt 	if (ret & BTRFS_EXTENT_FLAG_TREE_BLOCK)
3247d7728c96SJan Schmidt 		ret = -ENOENT;
3248d7728c96SJan Schmidt 	if (ret < 0)
3249d7728c96SJan Schmidt 		goto out;
3250d7728c96SJan Schmidt 
32514692cf58SJan Schmidt 	extent_item_pos = loi->logical - key.objectid;
32527a3ae2f8SJan Schmidt 	ret = iterate_extent_inodes(root->fs_info, key.objectid,
32537a3ae2f8SJan Schmidt 					extent_item_pos, 0, build_ino_list,
32544692cf58SJan Schmidt 					inodes);
3255d7728c96SJan Schmidt 
3256d7728c96SJan Schmidt 	if (ret < 0)
3257d7728c96SJan Schmidt 		goto out;
3258d7728c96SJan Schmidt 
3259745c4d8eSJeff Mahoney 	ret = copy_to_user((void *)(unsigned long)loi->inodes,
3260745c4d8eSJeff Mahoney 			   (void *)(unsigned long)inodes, size);
3261d7728c96SJan Schmidt 	if (ret)
3262d7728c96SJan Schmidt 		ret = -EFAULT;
3263d7728c96SJan Schmidt 
3264d7728c96SJan Schmidt out:
3265d7728c96SJan Schmidt 	btrfs_free_path(path);
3266d7728c96SJan Schmidt 	kfree(inodes);
3267d7728c96SJan Schmidt 	kfree(loi);
3268d7728c96SJan Schmidt 
3269d7728c96SJan Schmidt 	return ret;
3270d7728c96SJan Schmidt }
3271d7728c96SJan Schmidt 
327219a39dceSIlya Dryomov void update_ioctl_balance_args(struct btrfs_fs_info *fs_info, int lock,
3273c9e9f97bSIlya Dryomov 			       struct btrfs_ioctl_balance_args *bargs)
3274c9e9f97bSIlya Dryomov {
3275c9e9f97bSIlya Dryomov 	struct btrfs_balance_control *bctl = fs_info->balance_ctl;
3276c9e9f97bSIlya Dryomov 
3277c9e9f97bSIlya Dryomov 	bargs->flags = bctl->flags;
3278c9e9f97bSIlya Dryomov 
3279837d5b6eSIlya Dryomov 	if (atomic_read(&fs_info->balance_running))
3280837d5b6eSIlya Dryomov 		bargs->state |= BTRFS_BALANCE_STATE_RUNNING;
3281837d5b6eSIlya Dryomov 	if (atomic_read(&fs_info->balance_pause_req))
3282837d5b6eSIlya Dryomov 		bargs->state |= BTRFS_BALANCE_STATE_PAUSE_REQ;
3283a7e99c69SIlya Dryomov 	if (atomic_read(&fs_info->balance_cancel_req))
3284a7e99c69SIlya Dryomov 		bargs->state |= BTRFS_BALANCE_STATE_CANCEL_REQ;
3285837d5b6eSIlya Dryomov 
3286c9e9f97bSIlya Dryomov 	memcpy(&bargs->data, &bctl->data, sizeof(bargs->data));
3287c9e9f97bSIlya Dryomov 	memcpy(&bargs->meta, &bctl->meta, sizeof(bargs->meta));
3288c9e9f97bSIlya Dryomov 	memcpy(&bargs->sys, &bctl->sys, sizeof(bargs->sys));
328919a39dceSIlya Dryomov 
329019a39dceSIlya Dryomov 	if (lock) {
329119a39dceSIlya Dryomov 		spin_lock(&fs_info->balance_lock);
329219a39dceSIlya Dryomov 		memcpy(&bargs->stat, &bctl->stat, sizeof(bargs->stat));
329319a39dceSIlya Dryomov 		spin_unlock(&fs_info->balance_lock);
329419a39dceSIlya Dryomov 	} else {
329519a39dceSIlya Dryomov 		memcpy(&bargs->stat, &bctl->stat, sizeof(bargs->stat));
329619a39dceSIlya Dryomov 	}
3297c9e9f97bSIlya Dryomov }
3298c9e9f97bSIlya Dryomov 
32999ba1f6e4SLiu Bo static long btrfs_ioctl_balance(struct file *file, void __user *arg)
3300c9e9f97bSIlya Dryomov {
33019ba1f6e4SLiu Bo 	struct btrfs_root *root = BTRFS_I(fdentry(file)->d_inode)->root;
3302c9e9f97bSIlya Dryomov 	struct btrfs_fs_info *fs_info = root->fs_info;
3303c9e9f97bSIlya Dryomov 	struct btrfs_ioctl_balance_args *bargs;
3304c9e9f97bSIlya Dryomov 	struct btrfs_balance_control *bctl;
3305c9e9f97bSIlya Dryomov 	int ret;
3306c9e9f97bSIlya Dryomov 
3307c9e9f97bSIlya Dryomov 	if (!capable(CAP_SYS_ADMIN))
3308c9e9f97bSIlya Dryomov 		return -EPERM;
3309c9e9f97bSIlya Dryomov 
3310e54bfa31SLiu Bo 	ret = mnt_want_write_file(file);
33119ba1f6e4SLiu Bo 	if (ret)
33129ba1f6e4SLiu Bo 		return ret;
33139ba1f6e4SLiu Bo 
3314c9e9f97bSIlya Dryomov 	mutex_lock(&fs_info->volume_mutex);
3315c9e9f97bSIlya Dryomov 	mutex_lock(&fs_info->balance_mutex);
3316c9e9f97bSIlya Dryomov 
3317c9e9f97bSIlya Dryomov 	if (arg) {
3318c9e9f97bSIlya Dryomov 		bargs = memdup_user(arg, sizeof(*bargs));
3319c9e9f97bSIlya Dryomov 		if (IS_ERR(bargs)) {
3320c9e9f97bSIlya Dryomov 			ret = PTR_ERR(bargs);
3321c9e9f97bSIlya Dryomov 			goto out;
3322c9e9f97bSIlya Dryomov 		}
3323de322263SIlya Dryomov 
3324de322263SIlya Dryomov 		if (bargs->flags & BTRFS_BALANCE_RESUME) {
3325de322263SIlya Dryomov 			if (!fs_info->balance_ctl) {
3326de322263SIlya Dryomov 				ret = -ENOTCONN;
3327de322263SIlya Dryomov 				goto out_bargs;
3328de322263SIlya Dryomov 			}
3329de322263SIlya Dryomov 
3330de322263SIlya Dryomov 			bctl = fs_info->balance_ctl;
3331de322263SIlya Dryomov 			spin_lock(&fs_info->balance_lock);
3332de322263SIlya Dryomov 			bctl->flags |= BTRFS_BALANCE_RESUME;
3333de322263SIlya Dryomov 			spin_unlock(&fs_info->balance_lock);
3334de322263SIlya Dryomov 
3335de322263SIlya Dryomov 			goto do_balance;
3336de322263SIlya Dryomov 		}
3337c9e9f97bSIlya Dryomov 	} else {
3338c9e9f97bSIlya Dryomov 		bargs = NULL;
3339c9e9f97bSIlya Dryomov 	}
3340c9e9f97bSIlya Dryomov 
3341837d5b6eSIlya Dryomov 	if (fs_info->balance_ctl) {
3342837d5b6eSIlya Dryomov 		ret = -EINPROGRESS;
3343837d5b6eSIlya Dryomov 		goto out_bargs;
3344837d5b6eSIlya Dryomov 	}
3345837d5b6eSIlya Dryomov 
3346c9e9f97bSIlya Dryomov 	bctl = kzalloc(sizeof(*bctl), GFP_NOFS);
3347c9e9f97bSIlya Dryomov 	if (!bctl) {
3348c9e9f97bSIlya Dryomov 		ret = -ENOMEM;
3349c9e9f97bSIlya Dryomov 		goto out_bargs;
3350c9e9f97bSIlya Dryomov 	}
3351c9e9f97bSIlya Dryomov 
3352c9e9f97bSIlya Dryomov 	bctl->fs_info = fs_info;
3353c9e9f97bSIlya Dryomov 	if (arg) {
3354c9e9f97bSIlya Dryomov 		memcpy(&bctl->data, &bargs->data, sizeof(bctl->data));
3355c9e9f97bSIlya Dryomov 		memcpy(&bctl->meta, &bargs->meta, sizeof(bctl->meta));
3356c9e9f97bSIlya Dryomov 		memcpy(&bctl->sys, &bargs->sys, sizeof(bctl->sys));
3357c9e9f97bSIlya Dryomov 
3358c9e9f97bSIlya Dryomov 		bctl->flags = bargs->flags;
3359f43ffb60SIlya Dryomov 	} else {
3360f43ffb60SIlya Dryomov 		/* balance everything - no filters */
3361f43ffb60SIlya Dryomov 		bctl->flags |= BTRFS_BALANCE_TYPE_MASK;
3362c9e9f97bSIlya Dryomov 	}
3363c9e9f97bSIlya Dryomov 
3364de322263SIlya Dryomov do_balance:
3365c9e9f97bSIlya Dryomov 	ret = btrfs_balance(bctl, bargs);
3366c9e9f97bSIlya Dryomov 	/*
3367837d5b6eSIlya Dryomov 	 * bctl is freed in __cancel_balance or in free_fs_info if
3368837d5b6eSIlya Dryomov 	 * restriper was paused all the way until unmount
3369c9e9f97bSIlya Dryomov 	 */
3370c9e9f97bSIlya Dryomov 	if (arg) {
3371c9e9f97bSIlya Dryomov 		if (copy_to_user(arg, bargs, sizeof(*bargs)))
3372c9e9f97bSIlya Dryomov 			ret = -EFAULT;
3373c9e9f97bSIlya Dryomov 	}
3374c9e9f97bSIlya Dryomov 
3375c9e9f97bSIlya Dryomov out_bargs:
3376c9e9f97bSIlya Dryomov 	kfree(bargs);
3377c9e9f97bSIlya Dryomov out:
3378c9e9f97bSIlya Dryomov 	mutex_unlock(&fs_info->balance_mutex);
3379c9e9f97bSIlya Dryomov 	mutex_unlock(&fs_info->volume_mutex);
3380e54bfa31SLiu Bo 	mnt_drop_write_file(file);
3381c9e9f97bSIlya Dryomov 	return ret;
3382c9e9f97bSIlya Dryomov }
3383c9e9f97bSIlya Dryomov 
3384837d5b6eSIlya Dryomov static long btrfs_ioctl_balance_ctl(struct btrfs_root *root, int cmd)
3385837d5b6eSIlya Dryomov {
3386837d5b6eSIlya Dryomov 	if (!capable(CAP_SYS_ADMIN))
3387837d5b6eSIlya Dryomov 		return -EPERM;
3388837d5b6eSIlya Dryomov 
3389837d5b6eSIlya Dryomov 	switch (cmd) {
3390837d5b6eSIlya Dryomov 	case BTRFS_BALANCE_CTL_PAUSE:
3391837d5b6eSIlya Dryomov 		return btrfs_pause_balance(root->fs_info);
3392a7e99c69SIlya Dryomov 	case BTRFS_BALANCE_CTL_CANCEL:
3393a7e99c69SIlya Dryomov 		return btrfs_cancel_balance(root->fs_info);
3394837d5b6eSIlya Dryomov 	}
3395837d5b6eSIlya Dryomov 
3396837d5b6eSIlya Dryomov 	return -EINVAL;
3397837d5b6eSIlya Dryomov }
3398837d5b6eSIlya Dryomov 
339919a39dceSIlya Dryomov static long btrfs_ioctl_balance_progress(struct btrfs_root *root,
340019a39dceSIlya Dryomov 					 void __user *arg)
340119a39dceSIlya Dryomov {
340219a39dceSIlya Dryomov 	struct btrfs_fs_info *fs_info = root->fs_info;
340319a39dceSIlya Dryomov 	struct btrfs_ioctl_balance_args *bargs;
340419a39dceSIlya Dryomov 	int ret = 0;
340519a39dceSIlya Dryomov 
340619a39dceSIlya Dryomov 	if (!capable(CAP_SYS_ADMIN))
340719a39dceSIlya Dryomov 		return -EPERM;
340819a39dceSIlya Dryomov 
340919a39dceSIlya Dryomov 	mutex_lock(&fs_info->balance_mutex);
341019a39dceSIlya Dryomov 	if (!fs_info->balance_ctl) {
341119a39dceSIlya Dryomov 		ret = -ENOTCONN;
341219a39dceSIlya Dryomov 		goto out;
341319a39dceSIlya Dryomov 	}
341419a39dceSIlya Dryomov 
341519a39dceSIlya Dryomov 	bargs = kzalloc(sizeof(*bargs), GFP_NOFS);
341619a39dceSIlya Dryomov 	if (!bargs) {
341719a39dceSIlya Dryomov 		ret = -ENOMEM;
341819a39dceSIlya Dryomov 		goto out;
341919a39dceSIlya Dryomov 	}
342019a39dceSIlya Dryomov 
342119a39dceSIlya Dryomov 	update_ioctl_balance_args(fs_info, 1, bargs);
342219a39dceSIlya Dryomov 
342319a39dceSIlya Dryomov 	if (copy_to_user(arg, bargs, sizeof(*bargs)))
342419a39dceSIlya Dryomov 		ret = -EFAULT;
342519a39dceSIlya Dryomov 
342619a39dceSIlya Dryomov 	kfree(bargs);
342719a39dceSIlya Dryomov out:
342819a39dceSIlya Dryomov 	mutex_unlock(&fs_info->balance_mutex);
342919a39dceSIlya Dryomov 	return ret;
343019a39dceSIlya Dryomov }
343119a39dceSIlya Dryomov 
34325d13a37bSArne Jansen static long btrfs_ioctl_quota_ctl(struct btrfs_root *root, void __user *arg)
34335d13a37bSArne Jansen {
34345d13a37bSArne Jansen 	struct btrfs_ioctl_quota_ctl_args *sa;
34355d13a37bSArne Jansen 	struct btrfs_trans_handle *trans = NULL;
34365d13a37bSArne Jansen 	int ret;
34375d13a37bSArne Jansen 	int err;
34385d13a37bSArne Jansen 
34395d13a37bSArne Jansen 	if (!capable(CAP_SYS_ADMIN))
34405d13a37bSArne Jansen 		return -EPERM;
34415d13a37bSArne Jansen 
34425d13a37bSArne Jansen 	if (root->fs_info->sb->s_flags & MS_RDONLY)
34435d13a37bSArne Jansen 		return -EROFS;
34445d13a37bSArne Jansen 
34455d13a37bSArne Jansen 	sa = memdup_user(arg, sizeof(*sa));
34465d13a37bSArne Jansen 	if (IS_ERR(sa))
34475d13a37bSArne Jansen 		return PTR_ERR(sa);
34485d13a37bSArne Jansen 
34495d13a37bSArne Jansen 	if (sa->cmd != BTRFS_QUOTA_CTL_RESCAN) {
34505d13a37bSArne Jansen 		trans = btrfs_start_transaction(root, 2);
34515d13a37bSArne Jansen 		if (IS_ERR(trans)) {
34525d13a37bSArne Jansen 			ret = PTR_ERR(trans);
34535d13a37bSArne Jansen 			goto out;
34545d13a37bSArne Jansen 		}
34555d13a37bSArne Jansen 	}
34565d13a37bSArne Jansen 
34575d13a37bSArne Jansen 	switch (sa->cmd) {
34585d13a37bSArne Jansen 	case BTRFS_QUOTA_CTL_ENABLE:
34595d13a37bSArne Jansen 		ret = btrfs_quota_enable(trans, root->fs_info);
34605d13a37bSArne Jansen 		break;
34615d13a37bSArne Jansen 	case BTRFS_QUOTA_CTL_DISABLE:
34625d13a37bSArne Jansen 		ret = btrfs_quota_disable(trans, root->fs_info);
34635d13a37bSArne Jansen 		break;
34645d13a37bSArne Jansen 	case BTRFS_QUOTA_CTL_RESCAN:
34655d13a37bSArne Jansen 		ret = btrfs_quota_rescan(root->fs_info);
34665d13a37bSArne Jansen 		break;
34675d13a37bSArne Jansen 	default:
34685d13a37bSArne Jansen 		ret = -EINVAL;
34695d13a37bSArne Jansen 		break;
34705d13a37bSArne Jansen 	}
34715d13a37bSArne Jansen 
34725d13a37bSArne Jansen 	if (copy_to_user(arg, sa, sizeof(*sa)))
34735d13a37bSArne Jansen 		ret = -EFAULT;
34745d13a37bSArne Jansen 
34755d13a37bSArne Jansen 	if (trans) {
34765d13a37bSArne Jansen 		err = btrfs_commit_transaction(trans, root);
34775d13a37bSArne Jansen 		if (err && !ret)
34785d13a37bSArne Jansen 			ret = err;
34795d13a37bSArne Jansen 	}
34805d13a37bSArne Jansen 
34815d13a37bSArne Jansen out:
34825d13a37bSArne Jansen 	kfree(sa);
34835d13a37bSArne Jansen 	return ret;
34845d13a37bSArne Jansen }
34855d13a37bSArne Jansen 
34865d13a37bSArne Jansen static long btrfs_ioctl_qgroup_assign(struct btrfs_root *root, void __user *arg)
34875d13a37bSArne Jansen {
34885d13a37bSArne Jansen 	struct btrfs_ioctl_qgroup_assign_args *sa;
34895d13a37bSArne Jansen 	struct btrfs_trans_handle *trans;
34905d13a37bSArne Jansen 	int ret;
34915d13a37bSArne Jansen 	int err;
34925d13a37bSArne Jansen 
34935d13a37bSArne Jansen 	if (!capable(CAP_SYS_ADMIN))
34945d13a37bSArne Jansen 		return -EPERM;
34955d13a37bSArne Jansen 
34965d13a37bSArne Jansen 	if (root->fs_info->sb->s_flags & MS_RDONLY)
34975d13a37bSArne Jansen 		return -EROFS;
34985d13a37bSArne Jansen 
34995d13a37bSArne Jansen 	sa = memdup_user(arg, sizeof(*sa));
35005d13a37bSArne Jansen 	if (IS_ERR(sa))
35015d13a37bSArne Jansen 		return PTR_ERR(sa);
35025d13a37bSArne Jansen 
35035d13a37bSArne Jansen 	trans = btrfs_join_transaction(root);
35045d13a37bSArne Jansen 	if (IS_ERR(trans)) {
35055d13a37bSArne Jansen 		ret = PTR_ERR(trans);
35065d13a37bSArne Jansen 		goto out;
35075d13a37bSArne Jansen 	}
35085d13a37bSArne Jansen 
35095d13a37bSArne Jansen 	/* FIXME: check if the IDs really exist */
35105d13a37bSArne Jansen 	if (sa->assign) {
35115d13a37bSArne Jansen 		ret = btrfs_add_qgroup_relation(trans, root->fs_info,
35125d13a37bSArne Jansen 						sa->src, sa->dst);
35135d13a37bSArne Jansen 	} else {
35145d13a37bSArne Jansen 		ret = btrfs_del_qgroup_relation(trans, root->fs_info,
35155d13a37bSArne Jansen 						sa->src, sa->dst);
35165d13a37bSArne Jansen 	}
35175d13a37bSArne Jansen 
35185d13a37bSArne Jansen 	err = btrfs_end_transaction(trans, root);
35195d13a37bSArne Jansen 	if (err && !ret)
35205d13a37bSArne Jansen 		ret = err;
35215d13a37bSArne Jansen 
35225d13a37bSArne Jansen out:
35235d13a37bSArne Jansen 	kfree(sa);
35245d13a37bSArne Jansen 	return ret;
35255d13a37bSArne Jansen }
35265d13a37bSArne Jansen 
35275d13a37bSArne Jansen static long btrfs_ioctl_qgroup_create(struct btrfs_root *root, void __user *arg)
35285d13a37bSArne Jansen {
35295d13a37bSArne Jansen 	struct btrfs_ioctl_qgroup_create_args *sa;
35305d13a37bSArne Jansen 	struct btrfs_trans_handle *trans;
35315d13a37bSArne Jansen 	int ret;
35325d13a37bSArne Jansen 	int err;
35335d13a37bSArne Jansen 
35345d13a37bSArne Jansen 	if (!capable(CAP_SYS_ADMIN))
35355d13a37bSArne Jansen 		return -EPERM;
35365d13a37bSArne Jansen 
35375d13a37bSArne Jansen 	if (root->fs_info->sb->s_flags & MS_RDONLY)
35385d13a37bSArne Jansen 		return -EROFS;
35395d13a37bSArne Jansen 
35405d13a37bSArne Jansen 	sa = memdup_user(arg, sizeof(*sa));
35415d13a37bSArne Jansen 	if (IS_ERR(sa))
35425d13a37bSArne Jansen 		return PTR_ERR(sa);
35435d13a37bSArne Jansen 
35445d13a37bSArne Jansen 	trans = btrfs_join_transaction(root);
35455d13a37bSArne Jansen 	if (IS_ERR(trans)) {
35465d13a37bSArne Jansen 		ret = PTR_ERR(trans);
35475d13a37bSArne Jansen 		goto out;
35485d13a37bSArne Jansen 	}
35495d13a37bSArne Jansen 
35505d13a37bSArne Jansen 	/* FIXME: check if the IDs really exist */
35515d13a37bSArne Jansen 	if (sa->create) {
35525d13a37bSArne Jansen 		ret = btrfs_create_qgroup(trans, root->fs_info, sa->qgroupid,
35535d13a37bSArne Jansen 					  NULL);
35545d13a37bSArne Jansen 	} else {
35555d13a37bSArne Jansen 		ret = btrfs_remove_qgroup(trans, root->fs_info, sa->qgroupid);
35565d13a37bSArne Jansen 	}
35575d13a37bSArne Jansen 
35585d13a37bSArne Jansen 	err = btrfs_end_transaction(trans, root);
35595d13a37bSArne Jansen 	if (err && !ret)
35605d13a37bSArne Jansen 		ret = err;
35615d13a37bSArne Jansen 
35625d13a37bSArne Jansen out:
35635d13a37bSArne Jansen 	kfree(sa);
35645d13a37bSArne Jansen 	return ret;
35655d13a37bSArne Jansen }
35665d13a37bSArne Jansen 
35675d13a37bSArne Jansen static long btrfs_ioctl_qgroup_limit(struct btrfs_root *root, void __user *arg)
35685d13a37bSArne Jansen {
35695d13a37bSArne Jansen 	struct btrfs_ioctl_qgroup_limit_args *sa;
35705d13a37bSArne Jansen 	struct btrfs_trans_handle *trans;
35715d13a37bSArne Jansen 	int ret;
35725d13a37bSArne Jansen 	int err;
35735d13a37bSArne Jansen 	u64 qgroupid;
35745d13a37bSArne Jansen 
35755d13a37bSArne Jansen 	if (!capable(CAP_SYS_ADMIN))
35765d13a37bSArne Jansen 		return -EPERM;
35775d13a37bSArne Jansen 
35785d13a37bSArne Jansen 	if (root->fs_info->sb->s_flags & MS_RDONLY)
35795d13a37bSArne Jansen 		return -EROFS;
35805d13a37bSArne Jansen 
35815d13a37bSArne Jansen 	sa = memdup_user(arg, sizeof(*sa));
35825d13a37bSArne Jansen 	if (IS_ERR(sa))
35835d13a37bSArne Jansen 		return PTR_ERR(sa);
35845d13a37bSArne Jansen 
35855d13a37bSArne Jansen 	trans = btrfs_join_transaction(root);
35865d13a37bSArne Jansen 	if (IS_ERR(trans)) {
35875d13a37bSArne Jansen 		ret = PTR_ERR(trans);
35885d13a37bSArne Jansen 		goto out;
35895d13a37bSArne Jansen 	}
35905d13a37bSArne Jansen 
35915d13a37bSArne Jansen 	qgroupid = sa->qgroupid;
35925d13a37bSArne Jansen 	if (!qgroupid) {
35935d13a37bSArne Jansen 		/* take the current subvol as qgroup */
35945d13a37bSArne Jansen 		qgroupid = root->root_key.objectid;
35955d13a37bSArne Jansen 	}
35965d13a37bSArne Jansen 
35975d13a37bSArne Jansen 	/* FIXME: check if the IDs really exist */
35985d13a37bSArne Jansen 	ret = btrfs_limit_qgroup(trans, root->fs_info, qgroupid, &sa->lim);
35995d13a37bSArne Jansen 
36005d13a37bSArne Jansen 	err = btrfs_end_transaction(trans, root);
36015d13a37bSArne Jansen 	if (err && !ret)
36025d13a37bSArne Jansen 		ret = err;
36035d13a37bSArne Jansen 
36045d13a37bSArne Jansen out:
36055d13a37bSArne Jansen 	kfree(sa);
36065d13a37bSArne Jansen 	return ret;
36075d13a37bSArne Jansen }
36085d13a37bSArne Jansen 
36098ea05e3aSAlexander Block static long btrfs_ioctl_set_received_subvol(struct file *file,
36108ea05e3aSAlexander Block 					    void __user *arg)
36118ea05e3aSAlexander Block {
36128ea05e3aSAlexander Block 	struct btrfs_ioctl_received_subvol_args *sa = NULL;
36138ea05e3aSAlexander Block 	struct inode *inode = fdentry(file)->d_inode;
36148ea05e3aSAlexander Block 	struct btrfs_root *root = BTRFS_I(inode)->root;
36158ea05e3aSAlexander Block 	struct btrfs_root_item *root_item = &root->root_item;
36168ea05e3aSAlexander Block 	struct btrfs_trans_handle *trans;
36178ea05e3aSAlexander Block 	struct timespec ct = CURRENT_TIME;
36188ea05e3aSAlexander Block 	int ret = 0;
36198ea05e3aSAlexander Block 
36208ea05e3aSAlexander Block 	ret = mnt_want_write_file(file);
36218ea05e3aSAlexander Block 	if (ret < 0)
36228ea05e3aSAlexander Block 		return ret;
36238ea05e3aSAlexander Block 
36248ea05e3aSAlexander Block 	down_write(&root->fs_info->subvol_sem);
36258ea05e3aSAlexander Block 
36268ea05e3aSAlexander Block 	if (btrfs_ino(inode) != BTRFS_FIRST_FREE_OBJECTID) {
36278ea05e3aSAlexander Block 		ret = -EINVAL;
36288ea05e3aSAlexander Block 		goto out;
36298ea05e3aSAlexander Block 	}
36308ea05e3aSAlexander Block 
36318ea05e3aSAlexander Block 	if (btrfs_root_readonly(root)) {
36328ea05e3aSAlexander Block 		ret = -EROFS;
36338ea05e3aSAlexander Block 		goto out;
36348ea05e3aSAlexander Block 	}
36358ea05e3aSAlexander Block 
36368ea05e3aSAlexander Block 	if (!inode_owner_or_capable(inode)) {
36378ea05e3aSAlexander Block 		ret = -EACCES;
36388ea05e3aSAlexander Block 		goto out;
36398ea05e3aSAlexander Block 	}
36408ea05e3aSAlexander Block 
36418ea05e3aSAlexander Block 	sa = memdup_user(arg, sizeof(*sa));
36428ea05e3aSAlexander Block 	if (IS_ERR(sa)) {
36438ea05e3aSAlexander Block 		ret = PTR_ERR(sa);
36448ea05e3aSAlexander Block 		sa = NULL;
36458ea05e3aSAlexander Block 		goto out;
36468ea05e3aSAlexander Block 	}
36478ea05e3aSAlexander Block 
36488ea05e3aSAlexander Block 	trans = btrfs_start_transaction(root, 1);
36498ea05e3aSAlexander Block 	if (IS_ERR(trans)) {
36508ea05e3aSAlexander Block 		ret = PTR_ERR(trans);
36518ea05e3aSAlexander Block 		trans = NULL;
36528ea05e3aSAlexander Block 		goto out;
36538ea05e3aSAlexander Block 	}
36548ea05e3aSAlexander Block 
36558ea05e3aSAlexander Block 	sa->rtransid = trans->transid;
36568ea05e3aSAlexander Block 	sa->rtime.sec = ct.tv_sec;
36578ea05e3aSAlexander Block 	sa->rtime.nsec = ct.tv_nsec;
36588ea05e3aSAlexander Block 
36598ea05e3aSAlexander Block 	memcpy(root_item->received_uuid, sa->uuid, BTRFS_UUID_SIZE);
36608ea05e3aSAlexander Block 	btrfs_set_root_stransid(root_item, sa->stransid);
36618ea05e3aSAlexander Block 	btrfs_set_root_rtransid(root_item, sa->rtransid);
36628ea05e3aSAlexander Block 	root_item->stime.sec = cpu_to_le64(sa->stime.sec);
36638ea05e3aSAlexander Block 	root_item->stime.nsec = cpu_to_le32(sa->stime.nsec);
36648ea05e3aSAlexander Block 	root_item->rtime.sec = cpu_to_le64(sa->rtime.sec);
36658ea05e3aSAlexander Block 	root_item->rtime.nsec = cpu_to_le32(sa->rtime.nsec);
36668ea05e3aSAlexander Block 
36678ea05e3aSAlexander Block 	ret = btrfs_update_root(trans, root->fs_info->tree_root,
36688ea05e3aSAlexander Block 				&root->root_key, &root->root_item);
36698ea05e3aSAlexander Block 	if (ret < 0) {
36708ea05e3aSAlexander Block 		btrfs_end_transaction(trans, root);
36718ea05e3aSAlexander Block 		trans = NULL;
36728ea05e3aSAlexander Block 		goto out;
36738ea05e3aSAlexander Block 	} else {
36748ea05e3aSAlexander Block 		ret = btrfs_commit_transaction(trans, root);
36758ea05e3aSAlexander Block 		if (ret < 0)
36768ea05e3aSAlexander Block 			goto out;
36778ea05e3aSAlexander Block 	}
36788ea05e3aSAlexander Block 
36798ea05e3aSAlexander Block 	ret = copy_to_user(arg, sa, sizeof(*sa));
36808ea05e3aSAlexander Block 	if (ret)
36818ea05e3aSAlexander Block 		ret = -EFAULT;
36828ea05e3aSAlexander Block 
36838ea05e3aSAlexander Block out:
36848ea05e3aSAlexander Block 	kfree(sa);
36858ea05e3aSAlexander Block 	up_write(&root->fs_info->subvol_sem);
36868ea05e3aSAlexander Block 	mnt_drop_write_file(file);
36878ea05e3aSAlexander Block 	return ret;
36888ea05e3aSAlexander Block }
36898ea05e3aSAlexander Block 
3690f46b5a66SChristoph Hellwig long btrfs_ioctl(struct file *file, unsigned int
3691f46b5a66SChristoph Hellwig 		cmd, unsigned long arg)
3692f46b5a66SChristoph Hellwig {
3693f46b5a66SChristoph Hellwig 	struct btrfs_root *root = BTRFS_I(fdentry(file)->d_inode)->root;
36944bcabaa3SChristoph Hellwig 	void __user *argp = (void __user *)arg;
3695f46b5a66SChristoph Hellwig 
3696f46b5a66SChristoph Hellwig 	switch (cmd) {
36976cbff00fSChristoph Hellwig 	case FS_IOC_GETFLAGS:
36986cbff00fSChristoph Hellwig 		return btrfs_ioctl_getflags(file, argp);
36996cbff00fSChristoph Hellwig 	case FS_IOC_SETFLAGS:
37006cbff00fSChristoph Hellwig 		return btrfs_ioctl_setflags(file, argp);
37016cbff00fSChristoph Hellwig 	case FS_IOC_GETVERSION:
37026cbff00fSChristoph Hellwig 		return btrfs_ioctl_getversion(file, argp);
3703f7039b1dSLi Dongyang 	case FITRIM:
3704f7039b1dSLi Dongyang 		return btrfs_ioctl_fitrim(file, argp);
3705f46b5a66SChristoph Hellwig 	case BTRFS_IOC_SNAP_CREATE:
3706fa0d2b9bSLi Zefan 		return btrfs_ioctl_snap_create(file, argp, 0);
3707fdfb1e4fSLi Zefan 	case BTRFS_IOC_SNAP_CREATE_V2:
3708fa0d2b9bSLi Zefan 		return btrfs_ioctl_snap_create_v2(file, argp, 0);
37093de4586cSChris Mason 	case BTRFS_IOC_SUBVOL_CREATE:
3710fa0d2b9bSLi Zefan 		return btrfs_ioctl_snap_create(file, argp, 1);
37116f72c7e2SArne Jansen 	case BTRFS_IOC_SUBVOL_CREATE_V2:
37126f72c7e2SArne Jansen 		return btrfs_ioctl_snap_create_v2(file, argp, 1);
371376dda93cSYan, Zheng 	case BTRFS_IOC_SNAP_DESTROY:
371476dda93cSYan, Zheng 		return btrfs_ioctl_snap_destroy(file, argp);
37150caa102dSLi Zefan 	case BTRFS_IOC_SUBVOL_GETFLAGS:
37160caa102dSLi Zefan 		return btrfs_ioctl_subvol_getflags(file, argp);
37170caa102dSLi Zefan 	case BTRFS_IOC_SUBVOL_SETFLAGS:
37180caa102dSLi Zefan 		return btrfs_ioctl_subvol_setflags(file, argp);
37196ef5ed0dSJosef Bacik 	case BTRFS_IOC_DEFAULT_SUBVOL:
37206ef5ed0dSJosef Bacik 		return btrfs_ioctl_default_subvol(file, argp);
3721f46b5a66SChristoph Hellwig 	case BTRFS_IOC_DEFRAG:
37221e701a32SChris Mason 		return btrfs_ioctl_defrag(file, NULL);
37231e701a32SChris Mason 	case BTRFS_IOC_DEFRAG_RANGE:
37241e701a32SChris Mason 		return btrfs_ioctl_defrag(file, argp);
3725f46b5a66SChristoph Hellwig 	case BTRFS_IOC_RESIZE:
37264bcabaa3SChristoph Hellwig 		return btrfs_ioctl_resize(root, argp);
3727f46b5a66SChristoph Hellwig 	case BTRFS_IOC_ADD_DEV:
37284bcabaa3SChristoph Hellwig 		return btrfs_ioctl_add_dev(root, argp);
3729f46b5a66SChristoph Hellwig 	case BTRFS_IOC_RM_DEV:
37304bcabaa3SChristoph Hellwig 		return btrfs_ioctl_rm_dev(root, argp);
3731475f6387SJan Schmidt 	case BTRFS_IOC_FS_INFO:
3732475f6387SJan Schmidt 		return btrfs_ioctl_fs_info(root, argp);
3733475f6387SJan Schmidt 	case BTRFS_IOC_DEV_INFO:
3734475f6387SJan Schmidt 		return btrfs_ioctl_dev_info(root, argp);
3735f46b5a66SChristoph Hellwig 	case BTRFS_IOC_BALANCE:
37369ba1f6e4SLiu Bo 		return btrfs_ioctl_balance(file, NULL);
3737f46b5a66SChristoph Hellwig 	case BTRFS_IOC_CLONE:
3738c5c9cd4dSSage Weil 		return btrfs_ioctl_clone(file, arg, 0, 0, 0);
3739c5c9cd4dSSage Weil 	case BTRFS_IOC_CLONE_RANGE:
37407a865e8aSChristoph Hellwig 		return btrfs_ioctl_clone_range(file, argp);
3741f46b5a66SChristoph Hellwig 	case BTRFS_IOC_TRANS_START:
3742f46b5a66SChristoph Hellwig 		return btrfs_ioctl_trans_start(file);
3743f46b5a66SChristoph Hellwig 	case BTRFS_IOC_TRANS_END:
3744f46b5a66SChristoph Hellwig 		return btrfs_ioctl_trans_end(file);
3745ac8e9819SChris Mason 	case BTRFS_IOC_TREE_SEARCH:
3746ac8e9819SChris Mason 		return btrfs_ioctl_tree_search(file, argp);
3747ac8e9819SChris Mason 	case BTRFS_IOC_INO_LOOKUP:
3748ac8e9819SChris Mason 		return btrfs_ioctl_ino_lookup(file, argp);
3749d7728c96SJan Schmidt 	case BTRFS_IOC_INO_PATHS:
3750d7728c96SJan Schmidt 		return btrfs_ioctl_ino_to_path(root, argp);
3751d7728c96SJan Schmidt 	case BTRFS_IOC_LOGICAL_INO:
3752d7728c96SJan Schmidt 		return btrfs_ioctl_logical_to_ino(root, argp);
37531406e432SJosef Bacik 	case BTRFS_IOC_SPACE_INFO:
37541406e432SJosef Bacik 		return btrfs_ioctl_space_info(root, argp);
3755f46b5a66SChristoph Hellwig 	case BTRFS_IOC_SYNC:
3756f46b5a66SChristoph Hellwig 		btrfs_sync_fs(file->f_dentry->d_sb, 1);
3757f46b5a66SChristoph Hellwig 		return 0;
375846204592SSage Weil 	case BTRFS_IOC_START_SYNC:
375946204592SSage Weil 		return btrfs_ioctl_start_sync(file, argp);
376046204592SSage Weil 	case BTRFS_IOC_WAIT_SYNC:
376146204592SSage Weil 		return btrfs_ioctl_wait_sync(file, argp);
3762475f6387SJan Schmidt 	case BTRFS_IOC_SCRUB:
3763475f6387SJan Schmidt 		return btrfs_ioctl_scrub(root, argp);
3764475f6387SJan Schmidt 	case BTRFS_IOC_SCRUB_CANCEL:
3765475f6387SJan Schmidt 		return btrfs_ioctl_scrub_cancel(root, argp);
3766475f6387SJan Schmidt 	case BTRFS_IOC_SCRUB_PROGRESS:
3767475f6387SJan Schmidt 		return btrfs_ioctl_scrub_progress(root, argp);
3768c9e9f97bSIlya Dryomov 	case BTRFS_IOC_BALANCE_V2:
37699ba1f6e4SLiu Bo 		return btrfs_ioctl_balance(file, argp);
3770837d5b6eSIlya Dryomov 	case BTRFS_IOC_BALANCE_CTL:
3771837d5b6eSIlya Dryomov 		return btrfs_ioctl_balance_ctl(root, arg);
377219a39dceSIlya Dryomov 	case BTRFS_IOC_BALANCE_PROGRESS:
377319a39dceSIlya Dryomov 		return btrfs_ioctl_balance_progress(root, argp);
37748ea05e3aSAlexander Block 	case BTRFS_IOC_SET_RECEIVED_SUBVOL:
37758ea05e3aSAlexander Block 		return btrfs_ioctl_set_received_subvol(file, argp);
377631db9f7cSAlexander Block 	case BTRFS_IOC_SEND:
377731db9f7cSAlexander Block 		return btrfs_ioctl_send(file, argp);
3778c11d2c23SStefan Behrens 	case BTRFS_IOC_GET_DEV_STATS:
3779b27f7c0cSDavid Sterba 		return btrfs_ioctl_get_dev_stats(root, argp);
37805d13a37bSArne Jansen 	case BTRFS_IOC_QUOTA_CTL:
37815d13a37bSArne Jansen 		return btrfs_ioctl_quota_ctl(root, argp);
37825d13a37bSArne Jansen 	case BTRFS_IOC_QGROUP_ASSIGN:
37835d13a37bSArne Jansen 		return btrfs_ioctl_qgroup_assign(root, argp);
37845d13a37bSArne Jansen 	case BTRFS_IOC_QGROUP_CREATE:
37855d13a37bSArne Jansen 		return btrfs_ioctl_qgroup_create(root, argp);
37865d13a37bSArne Jansen 	case BTRFS_IOC_QGROUP_LIMIT:
37875d13a37bSArne Jansen 		return btrfs_ioctl_qgroup_limit(root, argp);
3788f46b5a66SChristoph Hellwig 	}
3789f46b5a66SChristoph Hellwig 
3790f46b5a66SChristoph Hellwig 	return -ENOTTY;
3791f46b5a66SChristoph Hellwig }
3792