xref: /openbmc/linux/fs/btrfs/ioctl.c (revision 183860f6)
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>
4555e301fdSFilipe Brandenburger #include <linux/btrfs.h>
464b4e25f2SChris Mason #include "compat.h"
47f46b5a66SChristoph Hellwig #include "ctree.h"
48f46b5a66SChristoph Hellwig #include "disk-io.h"
49f46b5a66SChristoph Hellwig #include "transaction.h"
50f46b5a66SChristoph Hellwig #include "btrfs_inode.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"
583f6bcfbdSStefan Behrens #include "dev-replace.h"
59f46b5a66SChristoph Hellwig 
606cbff00fSChristoph Hellwig /* Mask out flags that are inappropriate for the given type of inode. */
616cbff00fSChristoph Hellwig static inline __u32 btrfs_mask_flags(umode_t mode, __u32 flags)
626cbff00fSChristoph Hellwig {
636cbff00fSChristoph Hellwig 	if (S_ISDIR(mode))
646cbff00fSChristoph Hellwig 		return flags;
656cbff00fSChristoph Hellwig 	else if (S_ISREG(mode))
666cbff00fSChristoph Hellwig 		return flags & ~FS_DIRSYNC_FL;
676cbff00fSChristoph Hellwig 	else
686cbff00fSChristoph Hellwig 		return flags & (FS_NODUMP_FL | FS_NOATIME_FL);
696cbff00fSChristoph Hellwig }
70f46b5a66SChristoph Hellwig 
716cbff00fSChristoph Hellwig /*
726cbff00fSChristoph Hellwig  * Export inode flags to the format expected by the FS_IOC_GETFLAGS ioctl.
736cbff00fSChristoph Hellwig  */
746cbff00fSChristoph Hellwig static unsigned int btrfs_flags_to_ioctl(unsigned int flags)
756cbff00fSChristoph Hellwig {
766cbff00fSChristoph Hellwig 	unsigned int iflags = 0;
776cbff00fSChristoph Hellwig 
786cbff00fSChristoph Hellwig 	if (flags & BTRFS_INODE_SYNC)
796cbff00fSChristoph Hellwig 		iflags |= FS_SYNC_FL;
806cbff00fSChristoph Hellwig 	if (flags & BTRFS_INODE_IMMUTABLE)
816cbff00fSChristoph Hellwig 		iflags |= FS_IMMUTABLE_FL;
826cbff00fSChristoph Hellwig 	if (flags & BTRFS_INODE_APPEND)
836cbff00fSChristoph Hellwig 		iflags |= FS_APPEND_FL;
846cbff00fSChristoph Hellwig 	if (flags & BTRFS_INODE_NODUMP)
856cbff00fSChristoph Hellwig 		iflags |= FS_NODUMP_FL;
866cbff00fSChristoph Hellwig 	if (flags & BTRFS_INODE_NOATIME)
876cbff00fSChristoph Hellwig 		iflags |= FS_NOATIME_FL;
886cbff00fSChristoph Hellwig 	if (flags & BTRFS_INODE_DIRSYNC)
896cbff00fSChristoph Hellwig 		iflags |= FS_DIRSYNC_FL;
90d0092bddSLi Zefan 	if (flags & BTRFS_INODE_NODATACOW)
91d0092bddSLi Zefan 		iflags |= FS_NOCOW_FL;
92d0092bddSLi Zefan 
93d0092bddSLi Zefan 	if ((flags & BTRFS_INODE_COMPRESS) && !(flags & BTRFS_INODE_NOCOMPRESS))
94d0092bddSLi Zefan 		iflags |= FS_COMPR_FL;
95d0092bddSLi Zefan 	else if (flags & BTRFS_INODE_NOCOMPRESS)
96d0092bddSLi Zefan 		iflags |= FS_NOCOMP_FL;
976cbff00fSChristoph Hellwig 
986cbff00fSChristoph Hellwig 	return iflags;
996cbff00fSChristoph Hellwig }
1006cbff00fSChristoph Hellwig 
1016cbff00fSChristoph Hellwig /*
1026cbff00fSChristoph Hellwig  * Update inode->i_flags based on the btrfs internal flags.
1036cbff00fSChristoph Hellwig  */
1046cbff00fSChristoph Hellwig void btrfs_update_iflags(struct inode *inode)
1056cbff00fSChristoph Hellwig {
1066cbff00fSChristoph Hellwig 	struct btrfs_inode *ip = BTRFS_I(inode);
1076cbff00fSChristoph Hellwig 
1086cbff00fSChristoph Hellwig 	inode->i_flags &= ~(S_SYNC|S_APPEND|S_IMMUTABLE|S_NOATIME|S_DIRSYNC);
1096cbff00fSChristoph Hellwig 
1106cbff00fSChristoph Hellwig 	if (ip->flags & BTRFS_INODE_SYNC)
1116cbff00fSChristoph Hellwig 		inode->i_flags |= S_SYNC;
1126cbff00fSChristoph Hellwig 	if (ip->flags & BTRFS_INODE_IMMUTABLE)
1136cbff00fSChristoph Hellwig 		inode->i_flags |= S_IMMUTABLE;
1146cbff00fSChristoph Hellwig 	if (ip->flags & BTRFS_INODE_APPEND)
1156cbff00fSChristoph Hellwig 		inode->i_flags |= S_APPEND;
1166cbff00fSChristoph Hellwig 	if (ip->flags & BTRFS_INODE_NOATIME)
1176cbff00fSChristoph Hellwig 		inode->i_flags |= S_NOATIME;
1186cbff00fSChristoph Hellwig 	if (ip->flags & BTRFS_INODE_DIRSYNC)
1196cbff00fSChristoph Hellwig 		inode->i_flags |= S_DIRSYNC;
1206cbff00fSChristoph Hellwig }
1216cbff00fSChristoph Hellwig 
1226cbff00fSChristoph Hellwig /*
1236cbff00fSChristoph Hellwig  * Inherit flags from the parent inode.
1246cbff00fSChristoph Hellwig  *
125e27425d6SJosef Bacik  * Currently only the compression flags and the cow flags are inherited.
1266cbff00fSChristoph Hellwig  */
1276cbff00fSChristoph Hellwig void btrfs_inherit_iflags(struct inode *inode, struct inode *dir)
1286cbff00fSChristoph Hellwig {
1290b4dcea5SChris Mason 	unsigned int flags;
1300b4dcea5SChris Mason 
1310b4dcea5SChris Mason 	if (!dir)
1320b4dcea5SChris Mason 		return;
1330b4dcea5SChris Mason 
1340b4dcea5SChris Mason 	flags = BTRFS_I(dir)->flags;
1356cbff00fSChristoph Hellwig 
136e27425d6SJosef Bacik 	if (flags & BTRFS_INODE_NOCOMPRESS) {
137e27425d6SJosef Bacik 		BTRFS_I(inode)->flags &= ~BTRFS_INODE_COMPRESS;
138e27425d6SJosef Bacik 		BTRFS_I(inode)->flags |= BTRFS_INODE_NOCOMPRESS;
139e27425d6SJosef Bacik 	} else if (flags & BTRFS_INODE_COMPRESS) {
140e27425d6SJosef Bacik 		BTRFS_I(inode)->flags &= ~BTRFS_INODE_NOCOMPRESS;
141e27425d6SJosef Bacik 		BTRFS_I(inode)->flags |= BTRFS_INODE_COMPRESS;
142e27425d6SJosef Bacik 	}
1436cbff00fSChristoph Hellwig 
144213490b3SLiu Bo 	if (flags & BTRFS_INODE_NODATACOW) {
145e27425d6SJosef Bacik 		BTRFS_I(inode)->flags |= BTRFS_INODE_NODATACOW;
146213490b3SLiu Bo 		if (S_ISREG(inode->i_mode))
147213490b3SLiu Bo 			BTRFS_I(inode)->flags |= BTRFS_INODE_NODATASUM;
148213490b3SLiu Bo 	}
149e27425d6SJosef Bacik 
1506cbff00fSChristoph Hellwig 	btrfs_update_iflags(inode);
1516cbff00fSChristoph Hellwig }
1526cbff00fSChristoph Hellwig 
1536cbff00fSChristoph Hellwig static int btrfs_ioctl_getflags(struct file *file, void __user *arg)
1546cbff00fSChristoph Hellwig {
155496ad9aaSAl Viro 	struct btrfs_inode *ip = BTRFS_I(file_inode(file));
1566cbff00fSChristoph Hellwig 	unsigned int flags = btrfs_flags_to_ioctl(ip->flags);
1576cbff00fSChristoph Hellwig 
1586cbff00fSChristoph Hellwig 	if (copy_to_user(arg, &flags, sizeof(flags)))
1596cbff00fSChristoph Hellwig 		return -EFAULT;
1606cbff00fSChristoph Hellwig 	return 0;
1616cbff00fSChristoph Hellwig }
1626cbff00fSChristoph Hellwig 
16375e7cb7fSLiu Bo static int check_flags(unsigned int flags)
16475e7cb7fSLiu Bo {
16575e7cb7fSLiu Bo 	if (flags & ~(FS_IMMUTABLE_FL | FS_APPEND_FL | \
16675e7cb7fSLiu Bo 		      FS_NOATIME_FL | FS_NODUMP_FL | \
16775e7cb7fSLiu Bo 		      FS_SYNC_FL | FS_DIRSYNC_FL | \
168e1e8fb6aSLi Zefan 		      FS_NOCOMP_FL | FS_COMPR_FL |
169e1e8fb6aSLi Zefan 		      FS_NOCOW_FL))
17075e7cb7fSLiu Bo 		return -EOPNOTSUPP;
17175e7cb7fSLiu Bo 
17275e7cb7fSLiu Bo 	if ((flags & FS_NOCOMP_FL) && (flags & FS_COMPR_FL))
17375e7cb7fSLiu Bo 		return -EINVAL;
17475e7cb7fSLiu Bo 
17575e7cb7fSLiu Bo 	return 0;
17675e7cb7fSLiu Bo }
17775e7cb7fSLiu Bo 
1786cbff00fSChristoph Hellwig static int btrfs_ioctl_setflags(struct file *file, void __user *arg)
1796cbff00fSChristoph Hellwig {
180496ad9aaSAl Viro 	struct inode *inode = file_inode(file);
1816cbff00fSChristoph Hellwig 	struct btrfs_inode *ip = BTRFS_I(inode);
1826cbff00fSChristoph Hellwig 	struct btrfs_root *root = ip->root;
1836cbff00fSChristoph Hellwig 	struct btrfs_trans_handle *trans;
1846cbff00fSChristoph Hellwig 	unsigned int flags, oldflags;
1856cbff00fSChristoph Hellwig 	int ret;
186f062abf0SLi Zefan 	u64 ip_oldflags;
187f062abf0SLi Zefan 	unsigned int i_oldflags;
1887e97b8daSDavid Sterba 	umode_t mode;
1896cbff00fSChristoph Hellwig 
190b83cc969SLi Zefan 	if (btrfs_root_readonly(root))
191b83cc969SLi Zefan 		return -EROFS;
192b83cc969SLi Zefan 
1936cbff00fSChristoph Hellwig 	if (copy_from_user(&flags, arg, sizeof(flags)))
1946cbff00fSChristoph Hellwig 		return -EFAULT;
1956cbff00fSChristoph Hellwig 
19675e7cb7fSLiu Bo 	ret = check_flags(flags);
19775e7cb7fSLiu Bo 	if (ret)
19875e7cb7fSLiu Bo 		return ret;
1996cbff00fSChristoph Hellwig 
2002e149670SSerge E. Hallyn 	if (!inode_owner_or_capable(inode))
2016cbff00fSChristoph Hellwig 		return -EACCES;
2026cbff00fSChristoph Hellwig 
203e7848683SJan Kara 	ret = mnt_want_write_file(file);
204e7848683SJan Kara 	if (ret)
205e7848683SJan Kara 		return ret;
206e7848683SJan Kara 
2076cbff00fSChristoph Hellwig 	mutex_lock(&inode->i_mutex);
2086cbff00fSChristoph Hellwig 
209f062abf0SLi Zefan 	ip_oldflags = ip->flags;
210f062abf0SLi Zefan 	i_oldflags = inode->i_flags;
2117e97b8daSDavid Sterba 	mode = inode->i_mode;
212f062abf0SLi Zefan 
2136cbff00fSChristoph Hellwig 	flags = btrfs_mask_flags(inode->i_mode, flags);
2146cbff00fSChristoph Hellwig 	oldflags = btrfs_flags_to_ioctl(ip->flags);
2156cbff00fSChristoph Hellwig 	if ((flags ^ oldflags) & (FS_APPEND_FL | FS_IMMUTABLE_FL)) {
2166cbff00fSChristoph Hellwig 		if (!capable(CAP_LINUX_IMMUTABLE)) {
2176cbff00fSChristoph Hellwig 			ret = -EPERM;
2186cbff00fSChristoph Hellwig 			goto out_unlock;
2196cbff00fSChristoph Hellwig 		}
2206cbff00fSChristoph Hellwig 	}
2216cbff00fSChristoph Hellwig 
2226cbff00fSChristoph Hellwig 	if (flags & FS_SYNC_FL)
2236cbff00fSChristoph Hellwig 		ip->flags |= BTRFS_INODE_SYNC;
2246cbff00fSChristoph Hellwig 	else
2256cbff00fSChristoph Hellwig 		ip->flags &= ~BTRFS_INODE_SYNC;
2266cbff00fSChristoph Hellwig 	if (flags & FS_IMMUTABLE_FL)
2276cbff00fSChristoph Hellwig 		ip->flags |= BTRFS_INODE_IMMUTABLE;
2286cbff00fSChristoph Hellwig 	else
2296cbff00fSChristoph Hellwig 		ip->flags &= ~BTRFS_INODE_IMMUTABLE;
2306cbff00fSChristoph Hellwig 	if (flags & FS_APPEND_FL)
2316cbff00fSChristoph Hellwig 		ip->flags |= BTRFS_INODE_APPEND;
2326cbff00fSChristoph Hellwig 	else
2336cbff00fSChristoph Hellwig 		ip->flags &= ~BTRFS_INODE_APPEND;
2346cbff00fSChristoph Hellwig 	if (flags & FS_NODUMP_FL)
2356cbff00fSChristoph Hellwig 		ip->flags |= BTRFS_INODE_NODUMP;
2366cbff00fSChristoph Hellwig 	else
2376cbff00fSChristoph Hellwig 		ip->flags &= ~BTRFS_INODE_NODUMP;
2386cbff00fSChristoph Hellwig 	if (flags & FS_NOATIME_FL)
2396cbff00fSChristoph Hellwig 		ip->flags |= BTRFS_INODE_NOATIME;
2406cbff00fSChristoph Hellwig 	else
2416cbff00fSChristoph Hellwig 		ip->flags &= ~BTRFS_INODE_NOATIME;
2426cbff00fSChristoph Hellwig 	if (flags & FS_DIRSYNC_FL)
2436cbff00fSChristoph Hellwig 		ip->flags |= BTRFS_INODE_DIRSYNC;
2446cbff00fSChristoph Hellwig 	else
2456cbff00fSChristoph Hellwig 		ip->flags &= ~BTRFS_INODE_DIRSYNC;
2467e97b8daSDavid Sterba 	if (flags & FS_NOCOW_FL) {
2477e97b8daSDavid Sterba 		if (S_ISREG(mode)) {
2487e97b8daSDavid Sterba 			/*
2497e97b8daSDavid Sterba 			 * It's safe to turn csums off here, no extents exist.
2507e97b8daSDavid Sterba 			 * Otherwise we want the flag to reflect the real COW
2517e97b8daSDavid Sterba 			 * status of the file and will not set it.
2527e97b8daSDavid Sterba 			 */
2537e97b8daSDavid Sterba 			if (inode->i_size == 0)
2547e97b8daSDavid Sterba 				ip->flags |= BTRFS_INODE_NODATACOW
2557e97b8daSDavid Sterba 					   | BTRFS_INODE_NODATASUM;
2567e97b8daSDavid Sterba 		} else {
257e1e8fb6aSLi Zefan 			ip->flags |= BTRFS_INODE_NODATACOW;
2587e97b8daSDavid Sterba 		}
2597e97b8daSDavid Sterba 	} else {
2607e97b8daSDavid Sterba 		/*
2617e97b8daSDavid Sterba 		 * Revert back under same assuptions as above
2627e97b8daSDavid Sterba 		 */
2637e97b8daSDavid Sterba 		if (S_ISREG(mode)) {
2647e97b8daSDavid Sterba 			if (inode->i_size == 0)
2657e97b8daSDavid Sterba 				ip->flags &= ~(BTRFS_INODE_NODATACOW
2667e97b8daSDavid Sterba 				             | BTRFS_INODE_NODATASUM);
2677e97b8daSDavid Sterba 		} else {
268e1e8fb6aSLi Zefan 			ip->flags &= ~BTRFS_INODE_NODATACOW;
2697e97b8daSDavid Sterba 		}
2707e97b8daSDavid Sterba 	}
2716cbff00fSChristoph Hellwig 
27275e7cb7fSLiu Bo 	/*
27375e7cb7fSLiu Bo 	 * The COMPRESS flag can only be changed by users, while the NOCOMPRESS
27475e7cb7fSLiu Bo 	 * flag may be changed automatically if compression code won't make
27575e7cb7fSLiu Bo 	 * things smaller.
27675e7cb7fSLiu Bo 	 */
27775e7cb7fSLiu Bo 	if (flags & FS_NOCOMP_FL) {
27875e7cb7fSLiu Bo 		ip->flags &= ~BTRFS_INODE_COMPRESS;
27975e7cb7fSLiu Bo 		ip->flags |= BTRFS_INODE_NOCOMPRESS;
28075e7cb7fSLiu Bo 	} else if (flags & FS_COMPR_FL) {
28175e7cb7fSLiu Bo 		ip->flags |= BTRFS_INODE_COMPRESS;
28275e7cb7fSLiu Bo 		ip->flags &= ~BTRFS_INODE_NOCOMPRESS;
283ebcb904dSLi Zefan 	} else {
284ebcb904dSLi Zefan 		ip->flags &= ~(BTRFS_INODE_COMPRESS | BTRFS_INODE_NOCOMPRESS);
28575e7cb7fSLiu Bo 	}
2866cbff00fSChristoph Hellwig 
2874da6f1a3SLi Zefan 	trans = btrfs_start_transaction(root, 1);
288f062abf0SLi Zefan 	if (IS_ERR(trans)) {
289f062abf0SLi Zefan 		ret = PTR_ERR(trans);
290f062abf0SLi Zefan 		goto out_drop;
291f062abf0SLi Zefan 	}
2926cbff00fSChristoph Hellwig 
293306424ccSLi Zefan 	btrfs_update_iflags(inode);
2940c4d2d95SJosef Bacik 	inode_inc_iversion(inode);
295306424ccSLi Zefan 	inode->i_ctime = CURRENT_TIME;
2966cbff00fSChristoph Hellwig 	ret = btrfs_update_inode(trans, root, inode);
2976cbff00fSChristoph Hellwig 
2986cbff00fSChristoph Hellwig 	btrfs_end_transaction(trans, root);
299f062abf0SLi Zefan  out_drop:
300f062abf0SLi Zefan 	if (ret) {
301f062abf0SLi Zefan 		ip->flags = ip_oldflags;
302f062abf0SLi Zefan 		inode->i_flags = i_oldflags;
303f062abf0SLi Zefan 	}
3046cbff00fSChristoph Hellwig 
3056cbff00fSChristoph Hellwig  out_unlock:
3066cbff00fSChristoph Hellwig 	mutex_unlock(&inode->i_mutex);
307e7848683SJan Kara 	mnt_drop_write_file(file);
3082d4e6f6aSliubo 	return ret;
3096cbff00fSChristoph Hellwig }
3106cbff00fSChristoph Hellwig 
3116cbff00fSChristoph Hellwig static int btrfs_ioctl_getversion(struct file *file, int __user *arg)
3126cbff00fSChristoph Hellwig {
313496ad9aaSAl Viro 	struct inode *inode = file_inode(file);
3146cbff00fSChristoph Hellwig 
3156cbff00fSChristoph Hellwig 	return put_user(inode->i_generation, arg);
3166cbff00fSChristoph Hellwig }
317f46b5a66SChristoph Hellwig 
318f7039b1dSLi Dongyang static noinline int btrfs_ioctl_fitrim(struct file *file, void __user *arg)
319f7039b1dSLi Dongyang {
320815745cfSAl Viro 	struct btrfs_fs_info *fs_info = btrfs_sb(fdentry(file)->d_sb);
321f7039b1dSLi Dongyang 	struct btrfs_device *device;
322f7039b1dSLi Dongyang 	struct request_queue *q;
323f7039b1dSLi Dongyang 	struct fstrim_range range;
324f7039b1dSLi Dongyang 	u64 minlen = ULLONG_MAX;
325f7039b1dSLi Dongyang 	u64 num_devices = 0;
326815745cfSAl Viro 	u64 total_bytes = btrfs_super_total_bytes(fs_info->super_copy);
327f7039b1dSLi Dongyang 	int ret;
328f7039b1dSLi Dongyang 
329f7039b1dSLi Dongyang 	if (!capable(CAP_SYS_ADMIN))
330f7039b1dSLi Dongyang 		return -EPERM;
331f7039b1dSLi Dongyang 
3321f78160cSXiao Guangrong 	rcu_read_lock();
3331f78160cSXiao Guangrong 	list_for_each_entry_rcu(device, &fs_info->fs_devices->devices,
3341f78160cSXiao Guangrong 				dev_list) {
335f7039b1dSLi Dongyang 		if (!device->bdev)
336f7039b1dSLi Dongyang 			continue;
337f7039b1dSLi Dongyang 		q = bdev_get_queue(device->bdev);
338f7039b1dSLi Dongyang 		if (blk_queue_discard(q)) {
339f7039b1dSLi Dongyang 			num_devices++;
340f7039b1dSLi Dongyang 			minlen = min((u64)q->limits.discard_granularity,
341f7039b1dSLi Dongyang 				     minlen);
342f7039b1dSLi Dongyang 		}
343f7039b1dSLi Dongyang 	}
3441f78160cSXiao Guangrong 	rcu_read_unlock();
345f4c697e6SLukas Czerner 
346f7039b1dSLi Dongyang 	if (!num_devices)
347f7039b1dSLi Dongyang 		return -EOPNOTSUPP;
348f7039b1dSLi Dongyang 	if (copy_from_user(&range, arg, sizeof(range)))
349f7039b1dSLi Dongyang 		return -EFAULT;
350e515c18bSLukas Czerner 	if (range.start > total_bytes ||
351e515c18bSLukas Czerner 	    range.len < fs_info->sb->s_blocksize)
352f4c697e6SLukas Czerner 		return -EINVAL;
353f7039b1dSLi Dongyang 
354f4c697e6SLukas Czerner 	range.len = min(range.len, total_bytes - range.start);
355f7039b1dSLi Dongyang 	range.minlen = max(range.minlen, minlen);
356815745cfSAl Viro 	ret = btrfs_trim_fs(fs_info->tree_root, &range);
357f7039b1dSLi Dongyang 	if (ret < 0)
358f7039b1dSLi Dongyang 		return ret;
359f7039b1dSLi Dongyang 
360f7039b1dSLi Dongyang 	if (copy_to_user(arg, &range, sizeof(range)))
361f7039b1dSLi Dongyang 		return -EFAULT;
362f7039b1dSLi Dongyang 
363f7039b1dSLi Dongyang 	return 0;
364f7039b1dSLi Dongyang }
365f7039b1dSLi Dongyang 
366d5c12070SMiao Xie static noinline int create_subvol(struct inode *dir,
367cb8e7090SChristoph Hellwig 				  struct dentry *dentry,
36872fd032eSSage Weil 				  char *name, int namelen,
3696f72c7e2SArne Jansen 				  u64 *async_transid,
3708696c533SMiao Xie 				  struct btrfs_qgroup_inherit *inherit)
371f46b5a66SChristoph Hellwig {
372f46b5a66SChristoph Hellwig 	struct btrfs_trans_handle *trans;
373f46b5a66SChristoph Hellwig 	struct btrfs_key key;
374f46b5a66SChristoph Hellwig 	struct btrfs_root_item root_item;
375f46b5a66SChristoph Hellwig 	struct btrfs_inode_item *inode_item;
376f46b5a66SChristoph Hellwig 	struct extent_buffer *leaf;
377d5c12070SMiao Xie 	struct btrfs_root *root = BTRFS_I(dir)->root;
37876dda93cSYan, Zheng 	struct btrfs_root *new_root;
379d5c12070SMiao Xie 	struct btrfs_block_rsv block_rsv;
3808ea05e3aSAlexander Block 	struct timespec cur_time = CURRENT_TIME;
381f46b5a66SChristoph Hellwig 	int ret;
382f46b5a66SChristoph Hellwig 	int err;
383f46b5a66SChristoph Hellwig 	u64 objectid;
384f46b5a66SChristoph Hellwig 	u64 new_dirid = BTRFS_FIRST_FREE_OBJECTID;
3853de4586cSChris Mason 	u64 index = 0;
386d5c12070SMiao Xie 	u64 qgroup_reserved;
3878ea05e3aSAlexander Block 	uuid_le new_uuid;
388f46b5a66SChristoph Hellwig 
389581bb050SLi Zefan 	ret = btrfs_find_free_objectid(root->fs_info->tree_root, &objectid);
3902fbe8c8aSAl Viro 	if (ret)
391a22285a6SYan, Zheng 		return ret;
3926a912213SJosef Bacik 
393d5c12070SMiao Xie 	btrfs_init_block_rsv(&block_rsv, BTRFS_BLOCK_RSV_TEMP);
3949ed74f2dSJosef Bacik 	/*
395d5c12070SMiao Xie 	 * The same as the snapshot creation, please see the comment
396d5c12070SMiao Xie 	 * of create_snapshot().
3979ed74f2dSJosef Bacik 	 */
398d5c12070SMiao Xie 	ret = btrfs_subvolume_reserve_metadata(root, &block_rsv,
399d5c12070SMiao Xie 					       7, &qgroup_reserved);
400d5c12070SMiao Xie 	if (ret)
401d5c12070SMiao Xie 		return ret;
402f46b5a66SChristoph Hellwig 
403d5c12070SMiao Xie 	trans = btrfs_start_transaction(root, 0);
404d5c12070SMiao Xie 	if (IS_ERR(trans)) {
405d5c12070SMiao Xie 		ret = PTR_ERR(trans);
406d5c12070SMiao Xie 		goto out;
407d5c12070SMiao Xie 	}
408d5c12070SMiao Xie 	trans->block_rsv = &block_rsv;
409d5c12070SMiao Xie 	trans->bytes_reserved = block_rsv.size;
410f46b5a66SChristoph Hellwig 
4118696c533SMiao Xie 	ret = btrfs_qgroup_inherit(trans, root->fs_info, 0, objectid, inherit);
4126f72c7e2SArne Jansen 	if (ret)
4136f72c7e2SArne Jansen 		goto fail;
4146f72c7e2SArne Jansen 
4155d4f98a2SYan Zheng 	leaf = btrfs_alloc_free_block(trans, root, root->leafsize,
4165581a51aSJan Schmidt 				      0, objectid, NULL, 0, 0, 0);
4178e8a1e31SJosef Bacik 	if (IS_ERR(leaf)) {
4188e8a1e31SJosef Bacik 		ret = PTR_ERR(leaf);
4198e8a1e31SJosef Bacik 		goto fail;
4208e8a1e31SJosef Bacik 	}
421f46b5a66SChristoph Hellwig 
4225d4f98a2SYan Zheng 	memset_extent_buffer(leaf, 0, 0, sizeof(struct btrfs_header));
423f46b5a66SChristoph Hellwig 	btrfs_set_header_bytenr(leaf, leaf->start);
424f46b5a66SChristoph Hellwig 	btrfs_set_header_generation(leaf, trans->transid);
4255d4f98a2SYan Zheng 	btrfs_set_header_backref_rev(leaf, BTRFS_MIXED_BACKREF_REV);
426f46b5a66SChristoph Hellwig 	btrfs_set_header_owner(leaf, objectid);
427f46b5a66SChristoph Hellwig 
428f46b5a66SChristoph Hellwig 	write_extent_buffer(leaf, root->fs_info->fsid,
429f46b5a66SChristoph Hellwig 			    (unsigned long)btrfs_header_fsid(leaf),
430f46b5a66SChristoph Hellwig 			    BTRFS_FSID_SIZE);
4315d4f98a2SYan Zheng 	write_extent_buffer(leaf, root->fs_info->chunk_tree_uuid,
4325d4f98a2SYan Zheng 			    (unsigned long)btrfs_header_chunk_tree_uuid(leaf),
4335d4f98a2SYan Zheng 			    BTRFS_UUID_SIZE);
434f46b5a66SChristoph Hellwig 	btrfs_mark_buffer_dirty(leaf);
435f46b5a66SChristoph Hellwig 
4368ea05e3aSAlexander Block 	memset(&root_item, 0, sizeof(root_item));
4378ea05e3aSAlexander Block 
438f46b5a66SChristoph Hellwig 	inode_item = &root_item.inode;
439f46b5a66SChristoph Hellwig 	inode_item->generation = cpu_to_le64(1);
440f46b5a66SChristoph Hellwig 	inode_item->size = cpu_to_le64(3);
441f46b5a66SChristoph Hellwig 	inode_item->nlink = cpu_to_le32(1);
442a76a3cd4SYan Zheng 	inode_item->nbytes = cpu_to_le64(root->leafsize);
443f46b5a66SChristoph Hellwig 	inode_item->mode = cpu_to_le32(S_IFDIR | 0755);
444f46b5a66SChristoph Hellwig 
44508fe4db1SLi Zefan 	root_item.flags = 0;
44608fe4db1SLi Zefan 	root_item.byte_limit = 0;
44708fe4db1SLi Zefan 	inode_item->flags = cpu_to_le64(BTRFS_INODE_ROOT_ITEM_INIT);
44808fe4db1SLi Zefan 
449f46b5a66SChristoph Hellwig 	btrfs_set_root_bytenr(&root_item, leaf->start);
45084234f3aSYan Zheng 	btrfs_set_root_generation(&root_item, trans->transid);
451f46b5a66SChristoph Hellwig 	btrfs_set_root_level(&root_item, 0);
452f46b5a66SChristoph Hellwig 	btrfs_set_root_refs(&root_item, 1);
45386b9f2ecSYan, Zheng 	btrfs_set_root_used(&root_item, leaf->len);
45480ff3856SYan Zheng 	btrfs_set_root_last_snapshot(&root_item, 0);
455f46b5a66SChristoph Hellwig 
4568ea05e3aSAlexander Block 	btrfs_set_root_generation_v2(&root_item,
4578ea05e3aSAlexander Block 			btrfs_root_generation(&root_item));
4588ea05e3aSAlexander Block 	uuid_le_gen(&new_uuid);
4598ea05e3aSAlexander Block 	memcpy(root_item.uuid, new_uuid.b, BTRFS_UUID_SIZE);
4608ea05e3aSAlexander Block 	root_item.otime.sec = cpu_to_le64(cur_time.tv_sec);
461dadd1105SDan Carpenter 	root_item.otime.nsec = cpu_to_le32(cur_time.tv_nsec);
4628ea05e3aSAlexander Block 	root_item.ctime = root_item.otime;
4638ea05e3aSAlexander Block 	btrfs_set_root_ctransid(&root_item, trans->transid);
4648ea05e3aSAlexander Block 	btrfs_set_root_otransid(&root_item, trans->transid);
465f46b5a66SChristoph Hellwig 
466925baeddSChris Mason 	btrfs_tree_unlock(leaf);
467f46b5a66SChristoph Hellwig 	free_extent_buffer(leaf);
468f46b5a66SChristoph Hellwig 	leaf = NULL;
469f46b5a66SChristoph Hellwig 
470f46b5a66SChristoph Hellwig 	btrfs_set_root_dirid(&root_item, new_dirid);
471f46b5a66SChristoph Hellwig 
472f46b5a66SChristoph Hellwig 	key.objectid = objectid;
4735d4f98a2SYan Zheng 	key.offset = 0;
474f46b5a66SChristoph Hellwig 	btrfs_set_key_type(&key, BTRFS_ROOT_ITEM_KEY);
475f46b5a66SChristoph Hellwig 	ret = btrfs_insert_root(trans, root->fs_info->tree_root, &key,
476f46b5a66SChristoph Hellwig 				&root_item);
477f46b5a66SChristoph Hellwig 	if (ret)
478f46b5a66SChristoph Hellwig 		goto fail;
479f46b5a66SChristoph Hellwig 
48076dda93cSYan, Zheng 	key.offset = (u64)-1;
48176dda93cSYan, Zheng 	new_root = btrfs_read_fs_root_no_name(root->fs_info, &key);
48279787eaaSJeff Mahoney 	if (IS_ERR(new_root)) {
48379787eaaSJeff Mahoney 		btrfs_abort_transaction(trans, root, PTR_ERR(new_root));
48479787eaaSJeff Mahoney 		ret = PTR_ERR(new_root);
48579787eaaSJeff Mahoney 		goto fail;
48679787eaaSJeff Mahoney 	}
48776dda93cSYan, Zheng 
48876dda93cSYan, Zheng 	btrfs_record_root_in_trans(trans, new_root);
48976dda93cSYan, Zheng 
490d82a6f1dSJosef Bacik 	ret = btrfs_create_subvol_root(trans, new_root, new_dirid);
491ce598979SMark Fasheh 	if (ret) {
492ce598979SMark Fasheh 		/* We potentially lose an unused inode item here */
49379787eaaSJeff Mahoney 		btrfs_abort_transaction(trans, root, ret);
494ce598979SMark Fasheh 		goto fail;
495ce598979SMark Fasheh 	}
496ce598979SMark Fasheh 
497f46b5a66SChristoph Hellwig 	/*
498f46b5a66SChristoph Hellwig 	 * insert the directory item
499f46b5a66SChristoph Hellwig 	 */
5003de4586cSChris Mason 	ret = btrfs_set_inode_index(dir, &index);
50179787eaaSJeff Mahoney 	if (ret) {
50279787eaaSJeff Mahoney 		btrfs_abort_transaction(trans, root, ret);
50379787eaaSJeff Mahoney 		goto fail;
50479787eaaSJeff Mahoney 	}
5053de4586cSChris Mason 
5063de4586cSChris Mason 	ret = btrfs_insert_dir_item(trans, root,
50716cdcec7SMiao Xie 				    name, namelen, dir, &key,
5083de4586cSChris Mason 				    BTRFS_FT_DIR, index);
50979787eaaSJeff Mahoney 	if (ret) {
51079787eaaSJeff Mahoney 		btrfs_abort_transaction(trans, root, ret);
511f46b5a66SChristoph Hellwig 		goto fail;
51279787eaaSJeff Mahoney 	}
5130660b5afSChris Mason 
51452c26179SYan Zheng 	btrfs_i_size_write(dir, dir->i_size + namelen * 2);
51552c26179SYan Zheng 	ret = btrfs_update_inode(trans, root, dir);
51652c26179SYan Zheng 	BUG_ON(ret);
51752c26179SYan Zheng 
5180660b5afSChris Mason 	ret = btrfs_add_root_ref(trans, root->fs_info->tree_root,
5194df27c4dSYan, Zheng 				 objectid, root->root_key.objectid,
52033345d01SLi Zefan 				 btrfs_ino(dir), index, name, namelen);
52176dda93cSYan, Zheng 
5220660b5afSChris Mason 	BUG_ON(ret);
5230660b5afSChris Mason 
524f46b5a66SChristoph Hellwig fail:
525d5c12070SMiao Xie 	trans->block_rsv = NULL;
526d5c12070SMiao Xie 	trans->bytes_reserved = 0;
52772fd032eSSage Weil 	if (async_transid) {
52872fd032eSSage Weil 		*async_transid = trans->transid;
52972fd032eSSage Weil 		err = btrfs_commit_transaction_async(trans, root, 1);
53000d71c9cSMiao Xie 		if (err)
53100d71c9cSMiao Xie 			err = btrfs_commit_transaction(trans, root);
53272fd032eSSage Weil 	} else {
53376dda93cSYan, Zheng 		err = btrfs_commit_transaction(trans, root);
53472fd032eSSage Weil 	}
535f46b5a66SChristoph Hellwig 	if (err && !ret)
536f46b5a66SChristoph Hellwig 		ret = err;
5371a65e24bSChris Mason 
5381a65e24bSChris Mason 	if (!ret)
5391a65e24bSChris Mason 		d_instantiate(dentry, btrfs_lookup_dentry(dir, dentry));
540d5c12070SMiao Xie out:
541d5c12070SMiao Xie 	btrfs_subvolume_release_metadata(root, &block_rsv, qgroup_reserved);
542f46b5a66SChristoph Hellwig 	return ret;
543f46b5a66SChristoph Hellwig }
544f46b5a66SChristoph Hellwig 
545e9662f70SMiao Xie static int create_snapshot(struct btrfs_root *root, struct inode *dir,
546e9662f70SMiao Xie 			   struct dentry *dentry, char *name, int namelen,
547e9662f70SMiao Xie 			   u64 *async_transid, bool readonly,
548e9662f70SMiao Xie 			   struct btrfs_qgroup_inherit *inherit)
549f46b5a66SChristoph Hellwig {
5502e4bfab9SYan, Zheng 	struct inode *inode;
551f46b5a66SChristoph Hellwig 	struct btrfs_pending_snapshot *pending_snapshot;
552f46b5a66SChristoph Hellwig 	struct btrfs_trans_handle *trans;
5532e4bfab9SYan, Zheng 	int ret;
554f46b5a66SChristoph Hellwig 
555f46b5a66SChristoph Hellwig 	if (!root->ref_cows)
556f46b5a66SChristoph Hellwig 		return -EINVAL;
557f46b5a66SChristoph Hellwig 
5586a03843dSMiao Xie 	ret = btrfs_start_delalloc_inodes(root, 0);
5596a03843dSMiao Xie 	if (ret)
5606a03843dSMiao Xie 		return ret;
5616a03843dSMiao Xie 
5626a03843dSMiao Xie 	btrfs_wait_ordered_extents(root, 0);
5636a03843dSMiao Xie 
564a22285a6SYan, Zheng 	pending_snapshot = kzalloc(sizeof(*pending_snapshot), GFP_NOFS);
565a22285a6SYan, Zheng 	if (!pending_snapshot)
566a22285a6SYan, Zheng 		return -ENOMEM;
567a22285a6SYan, Zheng 
56866d8f3ddSMiao Xie 	btrfs_init_block_rsv(&pending_snapshot->block_rsv,
56966d8f3ddSMiao Xie 			     BTRFS_BLOCK_RSV_TEMP);
570d5c12070SMiao Xie 	/*
571d5c12070SMiao Xie 	 * 1 - parent dir inode
572d5c12070SMiao Xie 	 * 2 - dir entries
573d5c12070SMiao Xie 	 * 1 - root item
574d5c12070SMiao Xie 	 * 2 - root ref/backref
575d5c12070SMiao Xie 	 * 1 - root of snapshot
576d5c12070SMiao Xie 	 */
577d5c12070SMiao Xie 	ret = btrfs_subvolume_reserve_metadata(BTRFS_I(dir)->root,
578d5c12070SMiao Xie 					&pending_snapshot->block_rsv, 7,
579d5c12070SMiao Xie 					&pending_snapshot->qgroup_reserved);
580d5c12070SMiao Xie 	if (ret)
581d5c12070SMiao Xie 		goto out;
582d5c12070SMiao Xie 
583a22285a6SYan, Zheng 	pending_snapshot->dentry = dentry;
584a22285a6SYan, Zheng 	pending_snapshot->root = root;
585b83cc969SLi Zefan 	pending_snapshot->readonly = readonly;
586e9662f70SMiao Xie 	pending_snapshot->dir = dir;
5878696c533SMiao Xie 	pending_snapshot->inherit = inherit;
588a22285a6SYan, Zheng 
589d5c12070SMiao Xie 	trans = btrfs_start_transaction(root, 0);
590a22285a6SYan, Zheng 	if (IS_ERR(trans)) {
591a22285a6SYan, Zheng 		ret = PTR_ERR(trans);
592a22285a6SYan, Zheng 		goto fail;
593a22285a6SYan, Zheng 	}
594a22285a6SYan, Zheng 
5958351583eSJosef Bacik 	spin_lock(&root->fs_info->trans_lock);
596a22285a6SYan, Zheng 	list_add(&pending_snapshot->list,
597a22285a6SYan, Zheng 		 &trans->transaction->pending_snapshots);
5988351583eSJosef Bacik 	spin_unlock(&root->fs_info->trans_lock);
59972fd032eSSage Weil 	if (async_transid) {
60072fd032eSSage Weil 		*async_transid = trans->transid;
60172fd032eSSage Weil 		ret = btrfs_commit_transaction_async(trans,
60272fd032eSSage Weil 				     root->fs_info->extent_root, 1);
60300d71c9cSMiao Xie 		if (ret)
60400d71c9cSMiao Xie 			ret = btrfs_commit_transaction(trans, root);
60572fd032eSSage Weil 	} else {
60672fd032eSSage Weil 		ret = btrfs_commit_transaction(trans,
60772fd032eSSage Weil 					       root->fs_info->extent_root);
60872fd032eSSage Weil 	}
609aec8030aSMiao Xie 	if (ret)
610c37b2b62SJosef Bacik 		goto fail;
611a22285a6SYan, Zheng 
612a22285a6SYan, Zheng 	ret = pending_snapshot->error;
613f46b5a66SChristoph Hellwig 	if (ret)
6142e4bfab9SYan, Zheng 		goto fail;
615f46b5a66SChristoph Hellwig 
61666b4ffd1SJosef Bacik 	ret = btrfs_orphan_cleanup(pending_snapshot->snap);
61766b4ffd1SJosef Bacik 	if (ret)
61866b4ffd1SJosef Bacik 		goto fail;
619f46b5a66SChristoph Hellwig 
6202fbe8c8aSAl Viro 	inode = btrfs_lookup_dentry(dentry->d_parent->d_inode, dentry);
6212e4bfab9SYan, Zheng 	if (IS_ERR(inode)) {
6222e4bfab9SYan, Zheng 		ret = PTR_ERR(inode);
6232e4bfab9SYan, Zheng 		goto fail;
6242e4bfab9SYan, Zheng 	}
6252e4bfab9SYan, Zheng 	BUG_ON(!inode);
6262e4bfab9SYan, Zheng 	d_instantiate(dentry, inode);
6272e4bfab9SYan, Zheng 	ret = 0;
6282e4bfab9SYan, Zheng fail:
629d5c12070SMiao Xie 	btrfs_subvolume_release_metadata(BTRFS_I(dir)->root,
630d5c12070SMiao Xie 					 &pending_snapshot->block_rsv,
631d5c12070SMiao Xie 					 pending_snapshot->qgroup_reserved);
632d5c12070SMiao Xie out:
633a22285a6SYan, Zheng 	kfree(pending_snapshot);
634f46b5a66SChristoph Hellwig 	return ret;
635f46b5a66SChristoph Hellwig }
636f46b5a66SChristoph Hellwig 
6374260f7c7SSage Weil /*  copy of check_sticky in fs/namei.c()
6384260f7c7SSage Weil * It's inline, so penalty for filesystems that don't use sticky bit is
6394260f7c7SSage Weil * minimal.
6404260f7c7SSage Weil */
6414260f7c7SSage Weil static inline int btrfs_check_sticky(struct inode *dir, struct inode *inode)
6424260f7c7SSage Weil {
6432f2f43d3SEric W. Biederman 	kuid_t fsuid = current_fsuid();
6444260f7c7SSage Weil 
6454260f7c7SSage Weil 	if (!(dir->i_mode & S_ISVTX))
6464260f7c7SSage Weil 		return 0;
6472f2f43d3SEric W. Biederman 	if (uid_eq(inode->i_uid, fsuid))
6484260f7c7SSage Weil 		return 0;
6492f2f43d3SEric W. Biederman 	if (uid_eq(dir->i_uid, fsuid))
6504260f7c7SSage Weil 		return 0;
6514260f7c7SSage Weil 	return !capable(CAP_FOWNER);
6524260f7c7SSage Weil }
6534260f7c7SSage Weil 
6544260f7c7SSage Weil /*  copy of may_delete in fs/namei.c()
6554260f7c7SSage Weil  *	Check whether we can remove a link victim from directory dir, check
6564260f7c7SSage Weil  *  whether the type of victim is right.
6574260f7c7SSage Weil  *  1. We can't do it if dir is read-only (done in permission())
6584260f7c7SSage Weil  *  2. We should have write and exec permissions on dir
6594260f7c7SSage Weil  *  3. We can't remove anything from append-only dir
6604260f7c7SSage Weil  *  4. We can't do anything with immutable dir (done in permission())
6614260f7c7SSage Weil  *  5. If the sticky bit on dir is set we should either
6624260f7c7SSage Weil  *	a. be owner of dir, or
6634260f7c7SSage Weil  *	b. be owner of victim, or
6644260f7c7SSage Weil  *	c. have CAP_FOWNER capability
6654260f7c7SSage Weil  *  6. If the victim is append-only or immutable we can't do antyhing with
6664260f7c7SSage Weil  *     links pointing to it.
6674260f7c7SSage Weil  *  7. If we were asked to remove a directory and victim isn't one - ENOTDIR.
6684260f7c7SSage Weil  *  8. If we were asked to remove a non-directory and victim isn't one - EISDIR.
6694260f7c7SSage Weil  *  9. We can't remove a root or mountpoint.
6704260f7c7SSage Weil  * 10. We don't allow removal of NFS sillyrenamed files; it's handled by
6714260f7c7SSage Weil  *     nfs_async_unlink().
6724260f7c7SSage Weil  */
6734260f7c7SSage Weil 
6744260f7c7SSage Weil static int btrfs_may_delete(struct inode *dir,struct dentry *victim,int isdir)
6754260f7c7SSage Weil {
6764260f7c7SSage Weil 	int error;
6774260f7c7SSage Weil 
6784260f7c7SSage Weil 	if (!victim->d_inode)
6794260f7c7SSage Weil 		return -ENOENT;
6804260f7c7SSage Weil 
6814260f7c7SSage Weil 	BUG_ON(victim->d_parent->d_inode != dir);
6824fa6b5ecSJeff Layton 	audit_inode_child(dir, victim, AUDIT_TYPE_CHILD_DELETE);
6834260f7c7SSage Weil 
6844260f7c7SSage Weil 	error = inode_permission(dir, MAY_WRITE | MAY_EXEC);
6854260f7c7SSage Weil 	if (error)
6864260f7c7SSage Weil 		return error;
6874260f7c7SSage Weil 	if (IS_APPEND(dir))
6884260f7c7SSage Weil 		return -EPERM;
6894260f7c7SSage Weil 	if (btrfs_check_sticky(dir, victim->d_inode)||
6904260f7c7SSage Weil 		IS_APPEND(victim->d_inode)||
6914260f7c7SSage Weil 	    IS_IMMUTABLE(victim->d_inode) || IS_SWAPFILE(victim->d_inode))
6924260f7c7SSage Weil 		return -EPERM;
6934260f7c7SSage Weil 	if (isdir) {
6944260f7c7SSage Weil 		if (!S_ISDIR(victim->d_inode->i_mode))
6954260f7c7SSage Weil 			return -ENOTDIR;
6964260f7c7SSage Weil 		if (IS_ROOT(victim))
6974260f7c7SSage Weil 			return -EBUSY;
6984260f7c7SSage Weil 	} else if (S_ISDIR(victim->d_inode->i_mode))
6994260f7c7SSage Weil 		return -EISDIR;
7004260f7c7SSage Weil 	if (IS_DEADDIR(dir))
7014260f7c7SSage Weil 		return -ENOENT;
7024260f7c7SSage Weil 	if (victim->d_flags & DCACHE_NFSFS_RENAMED)
7034260f7c7SSage Weil 		return -EBUSY;
7044260f7c7SSage Weil 	return 0;
7054260f7c7SSage Weil }
7064260f7c7SSage Weil 
707cb8e7090SChristoph Hellwig /* copy of may_create in fs/namei.c() */
708cb8e7090SChristoph Hellwig static inline int btrfs_may_create(struct inode *dir, struct dentry *child)
709cb8e7090SChristoph Hellwig {
710cb8e7090SChristoph Hellwig 	if (child->d_inode)
711cb8e7090SChristoph Hellwig 		return -EEXIST;
712cb8e7090SChristoph Hellwig 	if (IS_DEADDIR(dir))
713cb8e7090SChristoph Hellwig 		return -ENOENT;
714cb8e7090SChristoph Hellwig 	return inode_permission(dir, MAY_WRITE | MAY_EXEC);
715cb8e7090SChristoph Hellwig }
716cb8e7090SChristoph Hellwig 
717cb8e7090SChristoph Hellwig /*
718cb8e7090SChristoph Hellwig  * Create a new subvolume below @parent.  This is largely modeled after
719cb8e7090SChristoph Hellwig  * sys_mkdirat and vfs_mkdir, but we only do a single component lookup
720cb8e7090SChristoph Hellwig  * inside this filesystem so it's quite a bit simpler.
721cb8e7090SChristoph Hellwig  */
72276dda93cSYan, Zheng static noinline int btrfs_mksubvol(struct path *parent,
72376dda93cSYan, Zheng 				   char *name, int namelen,
72472fd032eSSage Weil 				   struct btrfs_root *snap_src,
7256f72c7e2SArne Jansen 				   u64 *async_transid, bool readonly,
7268696c533SMiao Xie 				   struct btrfs_qgroup_inherit *inherit)
727cb8e7090SChristoph Hellwig {
72876dda93cSYan, Zheng 	struct inode *dir  = parent->dentry->d_inode;
729cb8e7090SChristoph Hellwig 	struct dentry *dentry;
730cb8e7090SChristoph Hellwig 	int error;
731cb8e7090SChristoph Hellwig 
7325c50c9b8SDavid Sterba 	error = mutex_lock_killable_nested(&dir->i_mutex, I_MUTEX_PARENT);
7335c50c9b8SDavid Sterba 	if (error == -EINTR)
7345c50c9b8SDavid Sterba 		return error;
735cb8e7090SChristoph Hellwig 
736cb8e7090SChristoph Hellwig 	dentry = lookup_one_len(name, parent->dentry, namelen);
737cb8e7090SChristoph Hellwig 	error = PTR_ERR(dentry);
738cb8e7090SChristoph Hellwig 	if (IS_ERR(dentry))
739cb8e7090SChristoph Hellwig 		goto out_unlock;
740cb8e7090SChristoph Hellwig 
741cb8e7090SChristoph Hellwig 	error = -EEXIST;
742cb8e7090SChristoph Hellwig 	if (dentry->d_inode)
743cb8e7090SChristoph Hellwig 		goto out_dput;
744cb8e7090SChristoph Hellwig 
74576dda93cSYan, Zheng 	error = btrfs_may_create(dir, dentry);
746cb8e7090SChristoph Hellwig 	if (error)
747a874a63eSLiu Bo 		goto out_dput;
748cb8e7090SChristoph Hellwig 
7499c52057cSChris Mason 	/*
7509c52057cSChris Mason 	 * even if this name doesn't exist, we may get hash collisions.
7519c52057cSChris Mason 	 * check for them now when we can safely fail
7529c52057cSChris Mason 	 */
7539c52057cSChris Mason 	error = btrfs_check_dir_item_collision(BTRFS_I(dir)->root,
7549c52057cSChris Mason 					       dir->i_ino, name,
7559c52057cSChris Mason 					       namelen);
7569c52057cSChris Mason 	if (error)
7579c52057cSChris Mason 		goto out_dput;
7589c52057cSChris Mason 
75976dda93cSYan, Zheng 	down_read(&BTRFS_I(dir)->root->fs_info->subvol_sem);
76076dda93cSYan, Zheng 
76176dda93cSYan, Zheng 	if (btrfs_root_refs(&BTRFS_I(dir)->root->root_item) == 0)
76276dda93cSYan, Zheng 		goto out_up_read;
76376dda93cSYan, Zheng 
7643de4586cSChris Mason 	if (snap_src) {
765e9662f70SMiao Xie 		error = create_snapshot(snap_src, dir, dentry, name, namelen,
7666f72c7e2SArne Jansen 					async_transid, readonly, inherit);
7673de4586cSChris Mason 	} else {
768d5c12070SMiao Xie 		error = create_subvol(dir, dentry, name, namelen,
769d5c12070SMiao Xie 				      async_transid, inherit);
7703de4586cSChris Mason 	}
77176dda93cSYan, Zheng 	if (!error)
77276dda93cSYan, Zheng 		fsnotify_mkdir(dir, dentry);
77376dda93cSYan, Zheng out_up_read:
77476dda93cSYan, Zheng 	up_read(&BTRFS_I(dir)->root->fs_info->subvol_sem);
775cb8e7090SChristoph Hellwig out_dput:
776cb8e7090SChristoph Hellwig 	dput(dentry);
777cb8e7090SChristoph Hellwig out_unlock:
77876dda93cSYan, Zheng 	mutex_unlock(&dir->i_mutex);
779cb8e7090SChristoph Hellwig 	return error;
780cb8e7090SChristoph Hellwig }
781cb8e7090SChristoph Hellwig 
7824cb5300bSChris Mason /*
7834cb5300bSChris Mason  * When we're defragging a range, we don't want to kick it off again
7844cb5300bSChris Mason  * if it is really just waiting for delalloc to send it down.
7854cb5300bSChris Mason  * If we find a nice big extent or delalloc range for the bytes in the
7864cb5300bSChris Mason  * file you want to defrag, we return 0 to let you know to skip this
7874cb5300bSChris Mason  * part of the file
7884cb5300bSChris Mason  */
7894cb5300bSChris Mason static int check_defrag_in_cache(struct inode *inode, u64 offset, int thresh)
7904cb5300bSChris Mason {
7914cb5300bSChris Mason 	struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
7924cb5300bSChris Mason 	struct extent_map *em = NULL;
7934cb5300bSChris Mason 	struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
7944cb5300bSChris Mason 	u64 end;
7954cb5300bSChris Mason 
7964cb5300bSChris Mason 	read_lock(&em_tree->lock);
7974cb5300bSChris Mason 	em = lookup_extent_mapping(em_tree, offset, PAGE_CACHE_SIZE);
7984cb5300bSChris Mason 	read_unlock(&em_tree->lock);
7994cb5300bSChris Mason 
8004cb5300bSChris Mason 	if (em) {
8014cb5300bSChris Mason 		end = extent_map_end(em);
8024cb5300bSChris Mason 		free_extent_map(em);
8034cb5300bSChris Mason 		if (end - offset > thresh)
8044cb5300bSChris Mason 			return 0;
8054cb5300bSChris Mason 	}
8064cb5300bSChris Mason 	/* if we already have a nice delalloc here, just stop */
8074cb5300bSChris Mason 	thresh /= 2;
8084cb5300bSChris Mason 	end = count_range_bits(io_tree, &offset, offset + thresh,
8094cb5300bSChris Mason 			       thresh, EXTENT_DELALLOC, 1);
8104cb5300bSChris Mason 	if (end >= thresh)
8114cb5300bSChris Mason 		return 0;
8124cb5300bSChris Mason 	return 1;
8134cb5300bSChris Mason }
8144cb5300bSChris Mason 
8154cb5300bSChris Mason /*
8164cb5300bSChris Mason  * helper function to walk through a file and find extents
8174cb5300bSChris Mason  * newer than a specific transid, and smaller than thresh.
8184cb5300bSChris Mason  *
8194cb5300bSChris Mason  * This is used by the defragging code to find new and small
8204cb5300bSChris Mason  * extents
8214cb5300bSChris Mason  */
8224cb5300bSChris Mason static int find_new_extents(struct btrfs_root *root,
8234cb5300bSChris Mason 			    struct inode *inode, u64 newer_than,
8244cb5300bSChris Mason 			    u64 *off, int thresh)
8254cb5300bSChris Mason {
8264cb5300bSChris Mason 	struct btrfs_path *path;
8274cb5300bSChris Mason 	struct btrfs_key min_key;
8284cb5300bSChris Mason 	struct btrfs_key max_key;
8294cb5300bSChris Mason 	struct extent_buffer *leaf;
8304cb5300bSChris Mason 	struct btrfs_file_extent_item *extent;
8314cb5300bSChris Mason 	int type;
8324cb5300bSChris Mason 	int ret;
833a4689d2bSDavid Sterba 	u64 ino = btrfs_ino(inode);
8344cb5300bSChris Mason 
8354cb5300bSChris Mason 	path = btrfs_alloc_path();
8364cb5300bSChris Mason 	if (!path)
8374cb5300bSChris Mason 		return -ENOMEM;
8384cb5300bSChris Mason 
839a4689d2bSDavid Sterba 	min_key.objectid = ino;
8404cb5300bSChris Mason 	min_key.type = BTRFS_EXTENT_DATA_KEY;
8414cb5300bSChris Mason 	min_key.offset = *off;
8424cb5300bSChris Mason 
843a4689d2bSDavid Sterba 	max_key.objectid = ino;
8444cb5300bSChris Mason 	max_key.type = (u8)-1;
8454cb5300bSChris Mason 	max_key.offset = (u64)-1;
8464cb5300bSChris Mason 
8474cb5300bSChris Mason 	path->keep_locks = 1;
8484cb5300bSChris Mason 
8494cb5300bSChris Mason 	while(1) {
8504cb5300bSChris Mason 		ret = btrfs_search_forward(root, &min_key, &max_key,
851de78b51aSEric Sandeen 					   path, newer_than);
8524cb5300bSChris Mason 		if (ret != 0)
8534cb5300bSChris Mason 			goto none;
854a4689d2bSDavid Sterba 		if (min_key.objectid != ino)
8554cb5300bSChris Mason 			goto none;
8564cb5300bSChris Mason 		if (min_key.type != BTRFS_EXTENT_DATA_KEY)
8574cb5300bSChris Mason 			goto none;
8584cb5300bSChris Mason 
8594cb5300bSChris Mason 		leaf = path->nodes[0];
8604cb5300bSChris Mason 		extent = btrfs_item_ptr(leaf, path->slots[0],
8614cb5300bSChris Mason 					struct btrfs_file_extent_item);
8624cb5300bSChris Mason 
8634cb5300bSChris Mason 		type = btrfs_file_extent_type(leaf, extent);
8644cb5300bSChris Mason 		if (type == BTRFS_FILE_EXTENT_REG &&
8654cb5300bSChris Mason 		    btrfs_file_extent_num_bytes(leaf, extent) < thresh &&
8664cb5300bSChris Mason 		    check_defrag_in_cache(inode, min_key.offset, thresh)) {
8674cb5300bSChris Mason 			*off = min_key.offset;
8684cb5300bSChris Mason 			btrfs_free_path(path);
8694cb5300bSChris Mason 			return 0;
8704cb5300bSChris Mason 		}
8714cb5300bSChris Mason 
8724cb5300bSChris Mason 		if (min_key.offset == (u64)-1)
8734cb5300bSChris Mason 			goto none;
8744cb5300bSChris Mason 
8754cb5300bSChris Mason 		min_key.offset++;
8764cb5300bSChris Mason 		btrfs_release_path(path);
8774cb5300bSChris Mason 	}
8784cb5300bSChris Mason none:
8794cb5300bSChris Mason 	btrfs_free_path(path);
8804cb5300bSChris Mason 	return -ENOENT;
8814cb5300bSChris Mason }
8824cb5300bSChris Mason 
8836c282eb4SLi Zefan static struct extent_map *defrag_lookup_extent(struct inode *inode, u64 start)
88417ce6ef8SLiu Bo {
88517ce6ef8SLiu Bo 	struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
886940100a4SChris Mason 	struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
8876c282eb4SLi Zefan 	struct extent_map *em;
8886c282eb4SLi Zefan 	u64 len = PAGE_CACHE_SIZE;
889940100a4SChris Mason 
890940100a4SChris Mason 	/*
891940100a4SChris Mason 	 * hopefully we have this extent in the tree already, try without
892940100a4SChris Mason 	 * the full extent lock
893940100a4SChris Mason 	 */
894940100a4SChris Mason 	read_lock(&em_tree->lock);
895940100a4SChris Mason 	em = lookup_extent_mapping(em_tree, start, len);
896940100a4SChris Mason 	read_unlock(&em_tree->lock);
897940100a4SChris Mason 
898940100a4SChris Mason 	if (!em) {
899940100a4SChris Mason 		/* get the big lock and read metadata off disk */
900d0082371SJeff Mahoney 		lock_extent(io_tree, start, start + len - 1);
901940100a4SChris Mason 		em = btrfs_get_extent(inode, NULL, 0, start, len, 0);
902d0082371SJeff Mahoney 		unlock_extent(io_tree, start, start + len - 1);
903940100a4SChris Mason 
9046cf8bfbfSDan Carpenter 		if (IS_ERR(em))
9056c282eb4SLi Zefan 			return NULL;
906940100a4SChris Mason 	}
907940100a4SChris Mason 
9086c282eb4SLi Zefan 	return em;
9096c282eb4SLi Zefan }
9106c282eb4SLi Zefan 
9116c282eb4SLi Zefan static bool defrag_check_next_extent(struct inode *inode, struct extent_map *em)
9126c282eb4SLi Zefan {
9136c282eb4SLi Zefan 	struct extent_map *next;
9146c282eb4SLi Zefan 	bool ret = true;
9156c282eb4SLi Zefan 
9166c282eb4SLi Zefan 	/* this is the last extent */
9176c282eb4SLi Zefan 	if (em->start + em->len >= i_size_read(inode))
9186c282eb4SLi Zefan 		return false;
9196c282eb4SLi Zefan 
9206c282eb4SLi Zefan 	next = defrag_lookup_extent(inode, em->start + em->len);
9216c282eb4SLi Zefan 	if (!next || next->block_start >= EXTENT_MAP_LAST_BYTE)
9226c282eb4SLi Zefan 		ret = false;
9236c282eb4SLi Zefan 
9246c282eb4SLi Zefan 	free_extent_map(next);
9256c282eb4SLi Zefan 	return ret;
9266c282eb4SLi Zefan }
9276c282eb4SLi Zefan 
9286c282eb4SLi Zefan static int should_defrag_range(struct inode *inode, u64 start, int thresh,
929a43a2111SAndrew Mahone 			       u64 *last_len, u64 *skip, u64 *defrag_end,
930a43a2111SAndrew Mahone 			       int compress)
9316c282eb4SLi Zefan {
9326c282eb4SLi Zefan 	struct extent_map *em;
9336c282eb4SLi Zefan 	int ret = 1;
9346c282eb4SLi Zefan 	bool next_mergeable = true;
9356c282eb4SLi Zefan 
9366c282eb4SLi Zefan 	/*
9376c282eb4SLi Zefan 	 * make sure that once we start defragging an extent, we keep on
9386c282eb4SLi Zefan 	 * defragging it
9396c282eb4SLi Zefan 	 */
9406c282eb4SLi Zefan 	if (start < *defrag_end)
9416c282eb4SLi Zefan 		return 1;
9426c282eb4SLi Zefan 
9436c282eb4SLi Zefan 	*skip = 0;
9446c282eb4SLi Zefan 
9456c282eb4SLi Zefan 	em = defrag_lookup_extent(inode, start);
9466c282eb4SLi Zefan 	if (!em)
9476c282eb4SLi Zefan 		return 0;
9486c282eb4SLi Zefan 
949940100a4SChris Mason 	/* this will cover holes, and inline extents */
95017ce6ef8SLiu Bo 	if (em->block_start >= EXTENT_MAP_LAST_BYTE) {
951940100a4SChris Mason 		ret = 0;
95217ce6ef8SLiu Bo 		goto out;
95317ce6ef8SLiu Bo 	}
95417ce6ef8SLiu Bo 
9556c282eb4SLi Zefan 	next_mergeable = defrag_check_next_extent(inode, em);
956940100a4SChris Mason 
957940100a4SChris Mason 	/*
9586c282eb4SLi Zefan 	 * we hit a real extent, if it is big or the next extent is not a
9596c282eb4SLi Zefan 	 * real extent, don't bother defragging it
960940100a4SChris Mason 	 */
961a43a2111SAndrew Mahone 	if (!compress && (*last_len == 0 || *last_len >= thresh) &&
9626c282eb4SLi Zefan 	    (em->len >= thresh || !next_mergeable))
963940100a4SChris Mason 		ret = 0;
96417ce6ef8SLiu Bo out:
965940100a4SChris Mason 	/*
966940100a4SChris Mason 	 * last_len ends up being a counter of how many bytes we've defragged.
967940100a4SChris Mason 	 * every time we choose not to defrag an extent, we reset *last_len
968940100a4SChris Mason 	 * so that the next tiny extent will force a defrag.
969940100a4SChris Mason 	 *
970940100a4SChris Mason 	 * The end result of this is that tiny extents before a single big
971940100a4SChris Mason 	 * extent will force at least part of that big extent to be defragged.
972940100a4SChris Mason 	 */
973940100a4SChris Mason 	if (ret) {
974940100a4SChris Mason 		*defrag_end = extent_map_end(em);
975940100a4SChris Mason 	} else {
976940100a4SChris Mason 		*last_len = 0;
977940100a4SChris Mason 		*skip = extent_map_end(em);
978940100a4SChris Mason 		*defrag_end = 0;
979940100a4SChris Mason 	}
980940100a4SChris Mason 
981940100a4SChris Mason 	free_extent_map(em);
982940100a4SChris Mason 	return ret;
983940100a4SChris Mason }
984940100a4SChris Mason 
9854cb5300bSChris Mason /*
9864cb5300bSChris Mason  * it doesn't do much good to defrag one or two pages
9874cb5300bSChris Mason  * at a time.  This pulls in a nice chunk of pages
9884cb5300bSChris Mason  * to COW and defrag.
9894cb5300bSChris Mason  *
9904cb5300bSChris Mason  * It also makes sure the delalloc code has enough
9914cb5300bSChris Mason  * dirty data to avoid making new small extents as part
9924cb5300bSChris Mason  * of the defrag
9934cb5300bSChris Mason  *
9944cb5300bSChris Mason  * It's a good idea to start RA on this range
9954cb5300bSChris Mason  * before calling this.
9964cb5300bSChris Mason  */
9974cb5300bSChris Mason static int cluster_pages_for_defrag(struct inode *inode,
9984cb5300bSChris Mason 				    struct page **pages,
9994cb5300bSChris Mason 				    unsigned long start_index,
10004cb5300bSChris Mason 				    int num_pages)
1001f46b5a66SChristoph Hellwig {
10024cb5300bSChris Mason 	unsigned long file_end;
10034cb5300bSChris Mason 	u64 isize = i_size_read(inode);
1004f46b5a66SChristoph Hellwig 	u64 page_start;
1005f46b5a66SChristoph Hellwig 	u64 page_end;
10061f12bd06SLiu Bo 	u64 page_cnt;
10074cb5300bSChris Mason 	int ret;
10084cb5300bSChris Mason 	int i;
10094cb5300bSChris Mason 	int i_done;
10104cb5300bSChris Mason 	struct btrfs_ordered_extent *ordered;
10114cb5300bSChris Mason 	struct extent_state *cached_state = NULL;
1012600a45e1SMiao Xie 	struct extent_io_tree *tree;
10133b16a4e3SJosef Bacik 	gfp_t mask = btrfs_alloc_write_mask(inode->i_mapping);
10144cb5300bSChris Mason 
10154cb5300bSChris Mason 	file_end = (isize - 1) >> PAGE_CACHE_SHIFT;
10161f12bd06SLiu Bo 	if (!isize || start_index > file_end)
10171f12bd06SLiu Bo 		return 0;
10181f12bd06SLiu Bo 
10191f12bd06SLiu Bo 	page_cnt = min_t(u64, (u64)num_pages, (u64)file_end - start_index + 1);
10204cb5300bSChris Mason 
10214cb5300bSChris Mason 	ret = btrfs_delalloc_reserve_space(inode,
10221f12bd06SLiu Bo 					   page_cnt << PAGE_CACHE_SHIFT);
10234cb5300bSChris Mason 	if (ret)
10244cb5300bSChris Mason 		return ret;
10254cb5300bSChris Mason 	i_done = 0;
1026600a45e1SMiao Xie 	tree = &BTRFS_I(inode)->io_tree;
10274cb5300bSChris Mason 
10284cb5300bSChris Mason 	/* step one, lock all the pages */
10291f12bd06SLiu Bo 	for (i = 0; i < page_cnt; i++) {
10304cb5300bSChris Mason 		struct page *page;
1031600a45e1SMiao Xie again:
1032a94733d0SJosef Bacik 		page = find_or_create_page(inode->i_mapping,
10333b16a4e3SJosef Bacik 					   start_index + i, mask);
10344cb5300bSChris Mason 		if (!page)
10354cb5300bSChris Mason 			break;
10364cb5300bSChris Mason 
1037600a45e1SMiao Xie 		page_start = page_offset(page);
1038600a45e1SMiao Xie 		page_end = page_start + PAGE_CACHE_SIZE - 1;
1039600a45e1SMiao Xie 		while (1) {
1040d0082371SJeff Mahoney 			lock_extent(tree, page_start, page_end);
1041600a45e1SMiao Xie 			ordered = btrfs_lookup_ordered_extent(inode,
1042600a45e1SMiao Xie 							      page_start);
1043d0082371SJeff Mahoney 			unlock_extent(tree, page_start, page_end);
1044600a45e1SMiao Xie 			if (!ordered)
1045600a45e1SMiao Xie 				break;
1046600a45e1SMiao Xie 
1047600a45e1SMiao Xie 			unlock_page(page);
1048600a45e1SMiao Xie 			btrfs_start_ordered_extent(inode, ordered, 1);
1049600a45e1SMiao Xie 			btrfs_put_ordered_extent(ordered);
1050600a45e1SMiao Xie 			lock_page(page);
10511f12bd06SLiu Bo 			/*
10521f12bd06SLiu Bo 			 * we unlocked the page above, so we need check if
10531f12bd06SLiu Bo 			 * it was released or not.
10541f12bd06SLiu Bo 			 */
10551f12bd06SLiu Bo 			if (page->mapping != inode->i_mapping) {
10561f12bd06SLiu Bo 				unlock_page(page);
10571f12bd06SLiu Bo 				page_cache_release(page);
10581f12bd06SLiu Bo 				goto again;
10591f12bd06SLiu Bo 			}
1060600a45e1SMiao Xie 		}
1061600a45e1SMiao Xie 
10624cb5300bSChris Mason 		if (!PageUptodate(page)) {
10634cb5300bSChris Mason 			btrfs_readpage(NULL, page);
10644cb5300bSChris Mason 			lock_page(page);
10654cb5300bSChris Mason 			if (!PageUptodate(page)) {
10664cb5300bSChris Mason 				unlock_page(page);
10674cb5300bSChris Mason 				page_cache_release(page);
10684cb5300bSChris Mason 				ret = -EIO;
10694cb5300bSChris Mason 				break;
10704cb5300bSChris Mason 			}
10714cb5300bSChris Mason 		}
1072600a45e1SMiao Xie 
1073600a45e1SMiao Xie 		if (page->mapping != inode->i_mapping) {
1074600a45e1SMiao Xie 			unlock_page(page);
1075600a45e1SMiao Xie 			page_cache_release(page);
1076600a45e1SMiao Xie 			goto again;
1077600a45e1SMiao Xie 		}
1078600a45e1SMiao Xie 
10794cb5300bSChris Mason 		pages[i] = page;
10804cb5300bSChris Mason 		i_done++;
10814cb5300bSChris Mason 	}
10824cb5300bSChris Mason 	if (!i_done || ret)
10834cb5300bSChris Mason 		goto out;
10844cb5300bSChris Mason 
10854cb5300bSChris Mason 	if (!(inode->i_sb->s_flags & MS_ACTIVE))
10864cb5300bSChris Mason 		goto out;
10874cb5300bSChris Mason 
10884cb5300bSChris Mason 	/*
10894cb5300bSChris Mason 	 * so now we have a nice long stream of locked
10904cb5300bSChris Mason 	 * and up to date pages, lets wait on them
10914cb5300bSChris Mason 	 */
10924cb5300bSChris Mason 	for (i = 0; i < i_done; i++)
10934cb5300bSChris Mason 		wait_on_page_writeback(pages[i]);
10944cb5300bSChris Mason 
10954cb5300bSChris Mason 	page_start = page_offset(pages[0]);
10964cb5300bSChris Mason 	page_end = page_offset(pages[i_done - 1]) + PAGE_CACHE_SIZE;
10974cb5300bSChris Mason 
10984cb5300bSChris Mason 	lock_extent_bits(&BTRFS_I(inode)->io_tree,
1099d0082371SJeff Mahoney 			 page_start, page_end - 1, 0, &cached_state);
11004cb5300bSChris Mason 	clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start,
11014cb5300bSChris Mason 			  page_end - 1, EXTENT_DIRTY | EXTENT_DELALLOC |
11029e8a4a8bSLiu Bo 			  EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG, 0, 0,
11039e8a4a8bSLiu Bo 			  &cached_state, GFP_NOFS);
11044cb5300bSChris Mason 
11051f12bd06SLiu Bo 	if (i_done != page_cnt) {
11069e0baf60SJosef Bacik 		spin_lock(&BTRFS_I(inode)->lock);
11079e0baf60SJosef Bacik 		BTRFS_I(inode)->outstanding_extents++;
11089e0baf60SJosef Bacik 		spin_unlock(&BTRFS_I(inode)->lock);
11094cb5300bSChris Mason 		btrfs_delalloc_release_space(inode,
11101f12bd06SLiu Bo 				     (page_cnt - i_done) << PAGE_CACHE_SHIFT);
11114cb5300bSChris Mason 	}
11124cb5300bSChris Mason 
11134cb5300bSChris Mason 
11149e8a4a8bSLiu Bo 	set_extent_defrag(&BTRFS_I(inode)->io_tree, page_start, page_end - 1,
11159e8a4a8bSLiu Bo 			  &cached_state, GFP_NOFS);
11164cb5300bSChris Mason 
11174cb5300bSChris Mason 	unlock_extent_cached(&BTRFS_I(inode)->io_tree,
11184cb5300bSChris Mason 			     page_start, page_end - 1, &cached_state,
11194cb5300bSChris Mason 			     GFP_NOFS);
11204cb5300bSChris Mason 
11214cb5300bSChris Mason 	for (i = 0; i < i_done; i++) {
11224cb5300bSChris Mason 		clear_page_dirty_for_io(pages[i]);
11234cb5300bSChris Mason 		ClearPageChecked(pages[i]);
11244cb5300bSChris Mason 		set_page_extent_mapped(pages[i]);
11254cb5300bSChris Mason 		set_page_dirty(pages[i]);
11264cb5300bSChris Mason 		unlock_page(pages[i]);
11274cb5300bSChris Mason 		page_cache_release(pages[i]);
11284cb5300bSChris Mason 	}
11294cb5300bSChris Mason 	return i_done;
11304cb5300bSChris Mason out:
11314cb5300bSChris Mason 	for (i = 0; i < i_done; i++) {
11324cb5300bSChris Mason 		unlock_page(pages[i]);
11334cb5300bSChris Mason 		page_cache_release(pages[i]);
11344cb5300bSChris Mason 	}
11351f12bd06SLiu Bo 	btrfs_delalloc_release_space(inode, page_cnt << PAGE_CACHE_SHIFT);
11364cb5300bSChris Mason 	return ret;
11374cb5300bSChris Mason 
11384cb5300bSChris Mason }
11394cb5300bSChris Mason 
11404cb5300bSChris Mason int btrfs_defrag_file(struct inode *inode, struct file *file,
11414cb5300bSChris Mason 		      struct btrfs_ioctl_defrag_range_args *range,
11424cb5300bSChris Mason 		      u64 newer_than, unsigned long max_to_defrag)
11434cb5300bSChris Mason {
11444cb5300bSChris Mason 	struct btrfs_root *root = BTRFS_I(inode)->root;
11454cb5300bSChris Mason 	struct file_ra_state *ra = NULL;
11464cb5300bSChris Mason 	unsigned long last_index;
1147151a31b2SLi Zefan 	u64 isize = i_size_read(inode);
1148940100a4SChris Mason 	u64 last_len = 0;
1149940100a4SChris Mason 	u64 skip = 0;
1150940100a4SChris Mason 	u64 defrag_end = 0;
11514cb5300bSChris Mason 	u64 newer_off = range->start;
1152f46b5a66SChristoph Hellwig 	unsigned long i;
1153008873eaSLi Zefan 	unsigned long ra_index = 0;
1154f46b5a66SChristoph Hellwig 	int ret;
11554cb5300bSChris Mason 	int defrag_count = 0;
11561a419d85SLi Zefan 	int compress_type = BTRFS_COMPRESS_ZLIB;
11574cb5300bSChris Mason 	int extent_thresh = range->extent_thresh;
1158008873eaSLi Zefan 	int max_cluster = (256 * 1024) >> PAGE_CACHE_SHIFT;
1159008873eaSLi Zefan 	int cluster = max_cluster;
11604cb5300bSChris Mason 	u64 new_align = ~((u64)128 * 1024 - 1);
11614cb5300bSChris Mason 	struct page **pages = NULL;
11624cb5300bSChris Mason 
11630abd5b17SLiu Bo 	if (isize == 0)
11640abd5b17SLiu Bo 		return 0;
11650abd5b17SLiu Bo 
11660abd5b17SLiu Bo 	if (range->start >= isize)
11670abd5b17SLiu Bo 		return -EINVAL;
11681a419d85SLi Zefan 
11691a419d85SLi Zefan 	if (range->flags & BTRFS_DEFRAG_RANGE_COMPRESS) {
11701a419d85SLi Zefan 		if (range->compress_type > BTRFS_COMPRESS_TYPES)
11711a419d85SLi Zefan 			return -EINVAL;
11721a419d85SLi Zefan 		if (range->compress_type)
11731a419d85SLi Zefan 			compress_type = range->compress_type;
11741a419d85SLi Zefan 	}
1175f46b5a66SChristoph Hellwig 
11760abd5b17SLiu Bo 	if (extent_thresh == 0)
11770abd5b17SLiu Bo 		extent_thresh = 256 * 1024;
1178f46b5a66SChristoph Hellwig 
11794cb5300bSChris Mason 	/*
11804cb5300bSChris Mason 	 * if we were not given a file, allocate a readahead
11814cb5300bSChris Mason 	 * context
11824cb5300bSChris Mason 	 */
11834cb5300bSChris Mason 	if (!file) {
11844cb5300bSChris Mason 		ra = kzalloc(sizeof(*ra), GFP_NOFS);
11854cb5300bSChris Mason 		if (!ra)
11864cb5300bSChris Mason 			return -ENOMEM;
11874cb5300bSChris Mason 		file_ra_state_init(ra, inode->i_mapping);
11884cb5300bSChris Mason 	} else {
11894cb5300bSChris Mason 		ra = &file->f_ra;
11904cb5300bSChris Mason 	}
11914cb5300bSChris Mason 
1192008873eaSLi Zefan 	pages = kmalloc(sizeof(struct page *) * max_cluster,
11934cb5300bSChris Mason 			GFP_NOFS);
11944cb5300bSChris Mason 	if (!pages) {
11954cb5300bSChris Mason 		ret = -ENOMEM;
11964cb5300bSChris Mason 		goto out_ra;
11974cb5300bSChris Mason 	}
11984cb5300bSChris Mason 
11994cb5300bSChris Mason 	/* find the last page to defrag */
12001e701a32SChris Mason 	if (range->start + range->len > range->start) {
1201151a31b2SLi Zefan 		last_index = min_t(u64, isize - 1,
12021e701a32SChris Mason 			 range->start + range->len - 1) >> PAGE_CACHE_SHIFT;
12031e701a32SChris Mason 	} else {
1204151a31b2SLi Zefan 		last_index = (isize - 1) >> PAGE_CACHE_SHIFT;
12051e701a32SChris Mason 	}
12061e701a32SChris Mason 
12074cb5300bSChris Mason 	if (newer_than) {
12084cb5300bSChris Mason 		ret = find_new_extents(root, inode, newer_than,
12094cb5300bSChris Mason 				       &newer_off, 64 * 1024);
12104cb5300bSChris Mason 		if (!ret) {
12114cb5300bSChris Mason 			range->start = newer_off;
12124cb5300bSChris Mason 			/*
12134cb5300bSChris Mason 			 * we always align our defrag to help keep
12144cb5300bSChris Mason 			 * the extents in the file evenly spaced
12154cb5300bSChris Mason 			 */
12164cb5300bSChris Mason 			i = (newer_off & new_align) >> PAGE_CACHE_SHIFT;
12174cb5300bSChris Mason 		} else
12184cb5300bSChris Mason 			goto out_ra;
12194cb5300bSChris Mason 	} else {
12201e701a32SChris Mason 		i = range->start >> PAGE_CACHE_SHIFT;
12214cb5300bSChris Mason 	}
12224cb5300bSChris Mason 	if (!max_to_defrag)
12237ec31b54SLiu Bo 		max_to_defrag = last_index + 1;
12244cb5300bSChris Mason 
12252a0f7f57SLi Zefan 	/*
12262a0f7f57SLi Zefan 	 * make writeback starts from i, so the defrag range can be
12272a0f7f57SLi Zefan 	 * written sequentially.
12282a0f7f57SLi Zefan 	 */
12292a0f7f57SLi Zefan 	if (i < inode->i_mapping->writeback_index)
12302a0f7f57SLi Zefan 		inode->i_mapping->writeback_index = i;
12312a0f7f57SLi Zefan 
1232f7f43cc8SChris Mason 	while (i <= last_index && defrag_count < max_to_defrag &&
1233f7f43cc8SChris Mason 	       (i < (i_size_read(inode) + PAGE_CACHE_SIZE - 1) >>
1234f7f43cc8SChris Mason 		PAGE_CACHE_SHIFT)) {
12354cb5300bSChris Mason 		/*
12364cb5300bSChris Mason 		 * make sure we stop running if someone unmounts
12374cb5300bSChris Mason 		 * the FS
12384cb5300bSChris Mason 		 */
12394cb5300bSChris Mason 		if (!(inode->i_sb->s_flags & MS_ACTIVE))
12404cb5300bSChris Mason 			break;
12414cb5300bSChris Mason 
1242210549ebSDavid Sterba 		if (btrfs_defrag_cancelled(root->fs_info)) {
1243210549ebSDavid Sterba 			printk(KERN_DEBUG "btrfs: defrag_file cancelled\n");
1244210549ebSDavid Sterba 			ret = -EAGAIN;
1245210549ebSDavid Sterba 			break;
1246210549ebSDavid Sterba 		}
1247210549ebSDavid Sterba 
124866c26892SLiu Bo 		if (!should_defrag_range(inode, (u64)i << PAGE_CACHE_SHIFT,
12496c282eb4SLi Zefan 					 extent_thresh, &last_len, &skip,
1250a43a2111SAndrew Mahone 					 &defrag_end, range->flags &
1251a43a2111SAndrew Mahone 					 BTRFS_DEFRAG_RANGE_COMPRESS)) {
1252940100a4SChris Mason 			unsigned long next;
1253940100a4SChris Mason 			/*
1254940100a4SChris Mason 			 * the should_defrag function tells us how much to skip
1255940100a4SChris Mason 			 * bump our counter by the suggested amount
1256940100a4SChris Mason 			 */
1257940100a4SChris Mason 			next = (skip + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
1258940100a4SChris Mason 			i = max(i + 1, next);
1259940100a4SChris Mason 			continue;
1260940100a4SChris Mason 		}
1261008873eaSLi Zefan 
1262008873eaSLi Zefan 		if (!newer_than) {
1263008873eaSLi Zefan 			cluster = (PAGE_CACHE_ALIGN(defrag_end) >>
1264008873eaSLi Zefan 				   PAGE_CACHE_SHIFT) - i;
1265008873eaSLi Zefan 			cluster = min(cluster, max_cluster);
1266008873eaSLi Zefan 		} else {
1267008873eaSLi Zefan 			cluster = max_cluster;
1268008873eaSLi Zefan 		}
1269008873eaSLi Zefan 
12701e701a32SChris Mason 		if (range->flags & BTRFS_DEFRAG_RANGE_COMPRESS)
12711a419d85SLi Zefan 			BTRFS_I(inode)->force_compress = compress_type;
1272940100a4SChris Mason 
1273008873eaSLi Zefan 		if (i + cluster > ra_index) {
1274008873eaSLi Zefan 			ra_index = max(i, ra_index);
1275008873eaSLi Zefan 			btrfs_force_ra(inode->i_mapping, ra, file, ra_index,
1276008873eaSLi Zefan 				       cluster);
1277008873eaSLi Zefan 			ra_index += max_cluster;
1278008873eaSLi Zefan 		}
12794cb5300bSChris Mason 
1280ecb8bea8SLiu Bo 		mutex_lock(&inode->i_mutex);
1281008873eaSLi Zefan 		ret = cluster_pages_for_defrag(inode, pages, i, cluster);
1282ecb8bea8SLiu Bo 		if (ret < 0) {
1283ecb8bea8SLiu Bo 			mutex_unlock(&inode->i_mutex);
12844cb5300bSChris Mason 			goto out_ra;
1285ecb8bea8SLiu Bo 		}
12864cb5300bSChris Mason 
12874cb5300bSChris Mason 		defrag_count += ret;
1288d0e1d66bSNamjae Jeon 		balance_dirty_pages_ratelimited(inode->i_mapping);
1289ecb8bea8SLiu Bo 		mutex_unlock(&inode->i_mutex);
12904cb5300bSChris Mason 
12914cb5300bSChris Mason 		if (newer_than) {
12924cb5300bSChris Mason 			if (newer_off == (u64)-1)
12934cb5300bSChris Mason 				break;
12944cb5300bSChris Mason 
1295e1f041e1SLiu Bo 			if (ret > 0)
1296e1f041e1SLiu Bo 				i += ret;
1297e1f041e1SLiu Bo 
12984cb5300bSChris Mason 			newer_off = max(newer_off + 1,
12994cb5300bSChris Mason 					(u64)i << PAGE_CACHE_SHIFT);
13004cb5300bSChris Mason 
13014cb5300bSChris Mason 			ret = find_new_extents(root, inode,
13024cb5300bSChris Mason 					       newer_than, &newer_off,
13034cb5300bSChris Mason 					       64 * 1024);
13044cb5300bSChris Mason 			if (!ret) {
13054cb5300bSChris Mason 				range->start = newer_off;
13064cb5300bSChris Mason 				i = (newer_off & new_align) >> PAGE_CACHE_SHIFT;
13074cb5300bSChris Mason 			} else {
13084cb5300bSChris Mason 				break;
1309940100a4SChris Mason 			}
13104cb5300bSChris Mason 		} else {
1311008873eaSLi Zefan 			if (ret > 0) {
1312cbcc8326SLi Zefan 				i += ret;
1313008873eaSLi Zefan 				last_len += ret << PAGE_CACHE_SHIFT;
1314008873eaSLi Zefan 			} else {
1315940100a4SChris Mason 				i++;
1316008873eaSLi Zefan 				last_len = 0;
1317008873eaSLi Zefan 			}
1318f46b5a66SChristoph Hellwig 		}
13194cb5300bSChris Mason 	}
1320f46b5a66SChristoph Hellwig 
13211e701a32SChris Mason 	if ((range->flags & BTRFS_DEFRAG_RANGE_START_IO))
13221e701a32SChris Mason 		filemap_flush(inode->i_mapping);
13231e701a32SChris Mason 
13241e701a32SChris Mason 	if ((range->flags & BTRFS_DEFRAG_RANGE_COMPRESS)) {
13251e701a32SChris Mason 		/* the filemap_flush will queue IO into the worker threads, but
13261e701a32SChris Mason 		 * we have to make sure the IO is actually started and that
13271e701a32SChris Mason 		 * ordered extents get created before we return
13281e701a32SChris Mason 		 */
13291e701a32SChris Mason 		atomic_inc(&root->fs_info->async_submit_draining);
13301e701a32SChris Mason 		while (atomic_read(&root->fs_info->nr_async_submits) ||
13311e701a32SChris Mason 		      atomic_read(&root->fs_info->async_delalloc_pages)) {
13321e701a32SChris Mason 			wait_event(root->fs_info->async_submit_wait,
13331e701a32SChris Mason 			   (atomic_read(&root->fs_info->nr_async_submits) == 0 &&
13341e701a32SChris Mason 			    atomic_read(&root->fs_info->async_delalloc_pages) == 0));
13351e701a32SChris Mason 		}
13361e701a32SChris Mason 		atomic_dec(&root->fs_info->async_submit_draining);
13371e701a32SChris Mason 
13381e701a32SChris Mason 		mutex_lock(&inode->i_mutex);
1339261507a0SLi Zefan 		BTRFS_I(inode)->force_compress = BTRFS_COMPRESS_NONE;
13401e701a32SChris Mason 		mutex_unlock(&inode->i_mutex);
13411e701a32SChris Mason 	}
13421e701a32SChris Mason 
13431a419d85SLi Zefan 	if (range->compress_type == BTRFS_COMPRESS_LZO) {
13442b0ce2c2SMitch Harder 		btrfs_set_fs_incompat(root->fs_info, COMPRESS_LZO);
13451a419d85SLi Zefan 	}
13461a419d85SLi Zefan 
134760ccf82fSDiego Calleja 	ret = defrag_count;
1348940100a4SChris Mason 
13494cb5300bSChris Mason out_ra:
13504cb5300bSChris Mason 	if (!file)
13514cb5300bSChris Mason 		kfree(ra);
13524cb5300bSChris Mason 	kfree(pages);
1353940100a4SChris Mason 	return ret;
1354f46b5a66SChristoph Hellwig }
1355f46b5a66SChristoph Hellwig 
1356198605a8SMiao Xie static noinline int btrfs_ioctl_resize(struct file *file,
135776dda93cSYan, Zheng 					void __user *arg)
1358f46b5a66SChristoph Hellwig {
1359f46b5a66SChristoph Hellwig 	u64 new_size;
1360f46b5a66SChristoph Hellwig 	u64 old_size;
1361f46b5a66SChristoph Hellwig 	u64 devid = 1;
1362496ad9aaSAl Viro 	struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
1363f46b5a66SChristoph Hellwig 	struct btrfs_ioctl_vol_args *vol_args;
1364f46b5a66SChristoph Hellwig 	struct btrfs_trans_handle *trans;
1365f46b5a66SChristoph Hellwig 	struct btrfs_device *device = NULL;
1366f46b5a66SChristoph Hellwig 	char *sizestr;
1367f46b5a66SChristoph Hellwig 	char *devstr = NULL;
1368f46b5a66SChristoph Hellwig 	int ret = 0;
1369f46b5a66SChristoph Hellwig 	int mod = 0;
1370f46b5a66SChristoph Hellwig 
1371e441d54dSChris Mason 	if (!capable(CAP_SYS_ADMIN))
1372e441d54dSChris Mason 		return -EPERM;
1373e441d54dSChris Mason 
1374198605a8SMiao Xie 	ret = mnt_want_write_file(file);
1375198605a8SMiao Xie 	if (ret)
1376198605a8SMiao Xie 		return ret;
1377198605a8SMiao Xie 
13785ac00addSStefan Behrens 	if (atomic_xchg(&root->fs_info->mutually_exclusive_operation_running,
13795ac00addSStefan Behrens 			1)) {
13805ac00addSStefan Behrens 		pr_info("btrfs: dev add/delete/balance/replace/resize operation in progress\n");
138197547676SMiao Xie 		mnt_drop_write_file(file);
13822c0c9da0SIlya Dryomov 		return -EINVAL;
1383c9e9f97bSIlya Dryomov 	}
1384c9e9f97bSIlya Dryomov 
13855ac00addSStefan Behrens 	mutex_lock(&root->fs_info->volume_mutex);
1386dae7b665SLi Zefan 	vol_args = memdup_user(arg, sizeof(*vol_args));
1387c9e9f97bSIlya Dryomov 	if (IS_ERR(vol_args)) {
1388c9e9f97bSIlya Dryomov 		ret = PTR_ERR(vol_args);
1389c9e9f97bSIlya Dryomov 		goto out;
1390c9e9f97bSIlya Dryomov 	}
13915516e595SMark Fasheh 
13925516e595SMark Fasheh 	vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
1393f46b5a66SChristoph Hellwig 
1394f46b5a66SChristoph Hellwig 	sizestr = vol_args->name;
1395f46b5a66SChristoph Hellwig 	devstr = strchr(sizestr, ':');
1396f46b5a66SChristoph Hellwig 	if (devstr) {
1397f46b5a66SChristoph Hellwig 		char *end;
1398f46b5a66SChristoph Hellwig 		sizestr = devstr + 1;
1399f46b5a66SChristoph Hellwig 		*devstr = '\0';
1400f46b5a66SChristoph Hellwig 		devstr = vol_args->name;
1401f46b5a66SChristoph Hellwig 		devid = simple_strtoull(devstr, &end, 10);
1402dfd79829SMiao Xie 		if (!devid) {
1403dfd79829SMiao Xie 			ret = -EINVAL;
1404dfd79829SMiao Xie 			goto out_free;
1405dfd79829SMiao Xie 		}
14065bb14682SArnd Hannemann 		printk(KERN_INFO "btrfs: resizing devid %llu\n",
140721380931SJoel Becker 		       (unsigned long long)devid);
1408f46b5a66SChristoph Hellwig 	}
1409dba60f3fSMiao Xie 
1410aa1b8cd4SStefan Behrens 	device = btrfs_find_device(root->fs_info, devid, NULL, NULL);
1411f46b5a66SChristoph Hellwig 	if (!device) {
14125bb14682SArnd Hannemann 		printk(KERN_INFO "btrfs: resizer unable to find device %llu\n",
141321380931SJoel Becker 		       (unsigned long long)devid);
1414dfd79829SMiao Xie 		ret = -ENODEV;
1415c9e9f97bSIlya Dryomov 		goto out_free;
1416f46b5a66SChristoph Hellwig 	}
1417dba60f3fSMiao Xie 
1418dba60f3fSMiao Xie 	if (!device->writeable) {
14194e42ae1bSLiu Bo 		printk(KERN_INFO "btrfs: resizer unable to apply on "
1420dba60f3fSMiao Xie 		       "readonly device %llu\n",
1421a8c4a33bSChris Mason 		       (unsigned long long)devid);
1422dfd79829SMiao Xie 		ret = -EPERM;
14234e42ae1bSLiu Bo 		goto out_free;
14244e42ae1bSLiu Bo 	}
14254e42ae1bSLiu Bo 
1426f46b5a66SChristoph Hellwig 	if (!strcmp(sizestr, "max"))
1427f46b5a66SChristoph Hellwig 		new_size = device->bdev->bd_inode->i_size;
1428f46b5a66SChristoph Hellwig 	else {
1429f46b5a66SChristoph Hellwig 		if (sizestr[0] == '-') {
1430f46b5a66SChristoph Hellwig 			mod = -1;
1431f46b5a66SChristoph Hellwig 			sizestr++;
1432f46b5a66SChristoph Hellwig 		} else if (sizestr[0] == '+') {
1433f46b5a66SChristoph Hellwig 			mod = 1;
1434f46b5a66SChristoph Hellwig 			sizestr++;
1435f46b5a66SChristoph Hellwig 		}
143691748467SAkinobu Mita 		new_size = memparse(sizestr, NULL);
1437f46b5a66SChristoph Hellwig 		if (new_size == 0) {
1438f46b5a66SChristoph Hellwig 			ret = -EINVAL;
1439c9e9f97bSIlya Dryomov 			goto out_free;
1440f46b5a66SChristoph Hellwig 		}
1441f46b5a66SChristoph Hellwig 	}
1442f46b5a66SChristoph Hellwig 
144363a212abSStefan Behrens 	if (device->is_tgtdev_for_dev_replace) {
1444dfd79829SMiao Xie 		ret = -EPERM;
144563a212abSStefan Behrens 		goto out_free;
144663a212abSStefan Behrens 	}
144763a212abSStefan Behrens 
1448f46b5a66SChristoph Hellwig 	old_size = device->total_bytes;
1449f46b5a66SChristoph Hellwig 
1450f46b5a66SChristoph Hellwig 	if (mod < 0) {
1451f46b5a66SChristoph Hellwig 		if (new_size > old_size) {
1452f46b5a66SChristoph Hellwig 			ret = -EINVAL;
1453c9e9f97bSIlya Dryomov 			goto out_free;
1454f46b5a66SChristoph Hellwig 		}
1455f46b5a66SChristoph Hellwig 		new_size = old_size - new_size;
1456f46b5a66SChristoph Hellwig 	} else if (mod > 0) {
1457f46b5a66SChristoph Hellwig 		new_size = old_size + new_size;
1458f46b5a66SChristoph Hellwig 	}
1459f46b5a66SChristoph Hellwig 
1460f46b5a66SChristoph Hellwig 	if (new_size < 256 * 1024 * 1024) {
1461f46b5a66SChristoph Hellwig 		ret = -EINVAL;
1462c9e9f97bSIlya Dryomov 		goto out_free;
1463f46b5a66SChristoph Hellwig 	}
1464f46b5a66SChristoph Hellwig 	if (new_size > device->bdev->bd_inode->i_size) {
1465f46b5a66SChristoph Hellwig 		ret = -EFBIG;
1466c9e9f97bSIlya Dryomov 		goto out_free;
1467f46b5a66SChristoph Hellwig 	}
1468f46b5a66SChristoph Hellwig 
1469f46b5a66SChristoph Hellwig 	do_div(new_size, root->sectorsize);
1470f46b5a66SChristoph Hellwig 	new_size *= root->sectorsize;
1471f46b5a66SChristoph Hellwig 
1472606686eeSJosef Bacik 	printk_in_rcu(KERN_INFO "btrfs: new size for %s is %llu\n",
1473606686eeSJosef Bacik 		      rcu_str_deref(device->name),
1474606686eeSJosef Bacik 		      (unsigned long long)new_size);
1475f46b5a66SChristoph Hellwig 
1476f46b5a66SChristoph Hellwig 	if (new_size > old_size) {
1477a22285a6SYan, Zheng 		trans = btrfs_start_transaction(root, 0);
147898d5dc13STsutomu Itoh 		if (IS_ERR(trans)) {
147998d5dc13STsutomu Itoh 			ret = PTR_ERR(trans);
1480c9e9f97bSIlya Dryomov 			goto out_free;
148198d5dc13STsutomu Itoh 		}
1482f46b5a66SChristoph Hellwig 		ret = btrfs_grow_device(trans, device, new_size);
1483f46b5a66SChristoph Hellwig 		btrfs_commit_transaction(trans, root);
1484ece7d20eSMike Fleetwood 	} else if (new_size < old_size) {
1485f46b5a66SChristoph Hellwig 		ret = btrfs_shrink_device(device, new_size);
14860253f40eSjeff.liu 	} /* equal, nothing need to do */
1487f46b5a66SChristoph Hellwig 
1488c9e9f97bSIlya Dryomov out_free:
1489f46b5a66SChristoph Hellwig 	kfree(vol_args);
1490c9e9f97bSIlya Dryomov out:
1491c9e9f97bSIlya Dryomov 	mutex_unlock(&root->fs_info->volume_mutex);
14925ac00addSStefan Behrens 	atomic_set(&root->fs_info->mutually_exclusive_operation_running, 0);
149318f39c41SIlya Dryomov 	mnt_drop_write_file(file);
1494f46b5a66SChristoph Hellwig 	return ret;
1495f46b5a66SChristoph Hellwig }
1496f46b5a66SChristoph Hellwig 
149772fd032eSSage Weil static noinline int btrfs_ioctl_snap_create_transid(struct file *file,
14986f72c7e2SArne Jansen 				char *name, unsigned long fd, int subvol,
14996f72c7e2SArne Jansen 				u64 *transid, bool readonly,
15008696c533SMiao Xie 				struct btrfs_qgroup_inherit *inherit)
1501f46b5a66SChristoph Hellwig {
1502f46b5a66SChristoph Hellwig 	int namelen;
15033de4586cSChris Mason 	int ret = 0;
1504f46b5a66SChristoph Hellwig 
1505a874a63eSLiu Bo 	ret = mnt_want_write_file(file);
1506a874a63eSLiu Bo 	if (ret)
1507a874a63eSLiu Bo 		goto out;
1508a874a63eSLiu Bo 
150972fd032eSSage Weil 	namelen = strlen(name);
151072fd032eSSage Weil 	if (strchr(name, '/')) {
1511f46b5a66SChristoph Hellwig 		ret = -EINVAL;
1512a874a63eSLiu Bo 		goto out_drop_write;
1513f46b5a66SChristoph Hellwig 	}
1514f46b5a66SChristoph Hellwig 
151516780cabSChris Mason 	if (name[0] == '.' &&
151616780cabSChris Mason 	   (namelen == 1 || (name[1] == '.' && namelen == 2))) {
151716780cabSChris Mason 		ret = -EEXIST;
1518a874a63eSLiu Bo 		goto out_drop_write;
151916780cabSChris Mason 	}
152016780cabSChris Mason 
15213de4586cSChris Mason 	if (subvol) {
152272fd032eSSage Weil 		ret = btrfs_mksubvol(&file->f_path, name, namelen,
15236f72c7e2SArne Jansen 				     NULL, transid, readonly, inherit);
1524cb8e7090SChristoph Hellwig 	} else {
15252903ff01SAl Viro 		struct fd src = fdget(fd);
15263de4586cSChris Mason 		struct inode *src_inode;
15272903ff01SAl Viro 		if (!src.file) {
15283de4586cSChris Mason 			ret = -EINVAL;
1529a874a63eSLiu Bo 			goto out_drop_write;
15303de4586cSChris Mason 		}
15313de4586cSChris Mason 
1532496ad9aaSAl Viro 		src_inode = file_inode(src.file);
1533496ad9aaSAl Viro 		if (src_inode->i_sb != file_inode(file)->i_sb) {
1534d397712bSChris Mason 			printk(KERN_INFO "btrfs: Snapshot src from "
1535d397712bSChris Mason 			       "another FS\n");
15363de4586cSChris Mason 			ret = -EINVAL;
1537ecd18815SAl Viro 		} else {
153872fd032eSSage Weil 			ret = btrfs_mksubvol(&file->f_path, name, namelen,
153972fd032eSSage Weil 					     BTRFS_I(src_inode)->root,
15406f72c7e2SArne Jansen 					     transid, readonly, inherit);
1541ecd18815SAl Viro 		}
15422903ff01SAl Viro 		fdput(src);
1543cb8e7090SChristoph Hellwig 	}
1544a874a63eSLiu Bo out_drop_write:
1545a874a63eSLiu Bo 	mnt_drop_write_file(file);
1546f46b5a66SChristoph Hellwig out:
154772fd032eSSage Weil 	return ret;
154872fd032eSSage Weil }
154972fd032eSSage Weil 
155072fd032eSSage Weil static noinline int btrfs_ioctl_snap_create(struct file *file,
1551fa0d2b9bSLi Zefan 					    void __user *arg, int subvol)
155272fd032eSSage Weil {
1553fa0d2b9bSLi Zefan 	struct btrfs_ioctl_vol_args *vol_args;
155472fd032eSSage Weil 	int ret;
155572fd032eSSage Weil 
1556fa0d2b9bSLi Zefan 	vol_args = memdup_user(arg, sizeof(*vol_args));
1557fa0d2b9bSLi Zefan 	if (IS_ERR(vol_args))
1558fa0d2b9bSLi Zefan 		return PTR_ERR(vol_args);
1559fa0d2b9bSLi Zefan 	vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
1560fa0d2b9bSLi Zefan 
1561fa0d2b9bSLi Zefan 	ret = btrfs_ioctl_snap_create_transid(file, vol_args->name,
1562b83cc969SLi Zefan 					      vol_args->fd, subvol,
15636f72c7e2SArne Jansen 					      NULL, false, NULL);
1564fa0d2b9bSLi Zefan 
1565fa0d2b9bSLi Zefan 	kfree(vol_args);
1566fa0d2b9bSLi Zefan 	return ret;
1567fa0d2b9bSLi Zefan }
1568fa0d2b9bSLi Zefan 
1569fa0d2b9bSLi Zefan static noinline int btrfs_ioctl_snap_create_v2(struct file *file,
1570fa0d2b9bSLi Zefan 					       void __user *arg, int subvol)
1571fa0d2b9bSLi Zefan {
1572fa0d2b9bSLi Zefan 	struct btrfs_ioctl_vol_args_v2 *vol_args;
1573fa0d2b9bSLi Zefan 	int ret;
1574fdfb1e4fSLi Zefan 	u64 transid = 0;
1575fdfb1e4fSLi Zefan 	u64 *ptr = NULL;
1576b83cc969SLi Zefan 	bool readonly = false;
15776f72c7e2SArne Jansen 	struct btrfs_qgroup_inherit *inherit = NULL;
157872fd032eSSage Weil 
1579fa0d2b9bSLi Zefan 	vol_args = memdup_user(arg, sizeof(*vol_args));
1580fa0d2b9bSLi Zefan 	if (IS_ERR(vol_args))
1581fa0d2b9bSLi Zefan 		return PTR_ERR(vol_args);
1582fa0d2b9bSLi Zefan 	vol_args->name[BTRFS_SUBVOL_NAME_MAX] = '\0';
1583fdfb1e4fSLi Zefan 
1584b83cc969SLi Zefan 	if (vol_args->flags &
15856f72c7e2SArne Jansen 	    ~(BTRFS_SUBVOL_CREATE_ASYNC | BTRFS_SUBVOL_RDONLY |
15866f72c7e2SArne Jansen 	      BTRFS_SUBVOL_QGROUP_INHERIT)) {
1587b83cc969SLi Zefan 		ret = -EOPNOTSUPP;
1588fdfb1e4fSLi Zefan 		goto out;
1589fdfb1e4fSLi Zefan 	}
1590fdfb1e4fSLi Zefan 
1591fa0d2b9bSLi Zefan 	if (vol_args->flags & BTRFS_SUBVOL_CREATE_ASYNC)
1592fdfb1e4fSLi Zefan 		ptr = &transid;
1593b83cc969SLi Zefan 	if (vol_args->flags & BTRFS_SUBVOL_RDONLY)
1594b83cc969SLi Zefan 		readonly = true;
15956f72c7e2SArne Jansen 	if (vol_args->flags & BTRFS_SUBVOL_QGROUP_INHERIT) {
15966f72c7e2SArne Jansen 		if (vol_args->size > PAGE_CACHE_SIZE) {
15976f72c7e2SArne Jansen 			ret = -EINVAL;
15986f72c7e2SArne Jansen 			goto out;
15996f72c7e2SArne Jansen 		}
16006f72c7e2SArne Jansen 		inherit = memdup_user(vol_args->qgroup_inherit, vol_args->size);
16016f72c7e2SArne Jansen 		if (IS_ERR(inherit)) {
16026f72c7e2SArne Jansen 			ret = PTR_ERR(inherit);
16036f72c7e2SArne Jansen 			goto out;
16046f72c7e2SArne Jansen 		}
16056f72c7e2SArne Jansen 	}
160675eaa0e2SSage Weil 
1607fa0d2b9bSLi Zefan 	ret = btrfs_ioctl_snap_create_transid(file, vol_args->name,
16086f72c7e2SArne Jansen 					      vol_args->fd, subvol, ptr,
16098696c533SMiao Xie 					      readonly, inherit);
161075eaa0e2SSage Weil 
1611fdfb1e4fSLi Zefan 	if (ret == 0 && ptr &&
161275eaa0e2SSage Weil 	    copy_to_user(arg +
1613fdfb1e4fSLi Zefan 			 offsetof(struct btrfs_ioctl_vol_args_v2,
1614fdfb1e4fSLi Zefan 				  transid), ptr, sizeof(*ptr)))
161575eaa0e2SSage Weil 		ret = -EFAULT;
1616fdfb1e4fSLi Zefan out:
1617f46b5a66SChristoph Hellwig 	kfree(vol_args);
16186f72c7e2SArne Jansen 	kfree(inherit);
1619f46b5a66SChristoph Hellwig 	return ret;
1620f46b5a66SChristoph Hellwig }
1621f46b5a66SChristoph Hellwig 
16220caa102dSLi Zefan static noinline int btrfs_ioctl_subvol_getflags(struct file *file,
16230caa102dSLi Zefan 						void __user *arg)
16240caa102dSLi Zefan {
1625496ad9aaSAl Viro 	struct inode *inode = file_inode(file);
16260caa102dSLi Zefan 	struct btrfs_root *root = BTRFS_I(inode)->root;
16270caa102dSLi Zefan 	int ret = 0;
16280caa102dSLi Zefan 	u64 flags = 0;
16290caa102dSLi Zefan 
163033345d01SLi Zefan 	if (btrfs_ino(inode) != BTRFS_FIRST_FREE_OBJECTID)
16310caa102dSLi Zefan 		return -EINVAL;
16320caa102dSLi Zefan 
16330caa102dSLi Zefan 	down_read(&root->fs_info->subvol_sem);
16340caa102dSLi Zefan 	if (btrfs_root_readonly(root))
16350caa102dSLi Zefan 		flags |= BTRFS_SUBVOL_RDONLY;
16360caa102dSLi Zefan 	up_read(&root->fs_info->subvol_sem);
16370caa102dSLi Zefan 
16380caa102dSLi Zefan 	if (copy_to_user(arg, &flags, sizeof(flags)))
16390caa102dSLi Zefan 		ret = -EFAULT;
16400caa102dSLi Zefan 
16410caa102dSLi Zefan 	return ret;
16420caa102dSLi Zefan }
16430caa102dSLi Zefan 
16440caa102dSLi Zefan static noinline int btrfs_ioctl_subvol_setflags(struct file *file,
16450caa102dSLi Zefan 					      void __user *arg)
16460caa102dSLi Zefan {
1647496ad9aaSAl Viro 	struct inode *inode = file_inode(file);
16480caa102dSLi Zefan 	struct btrfs_root *root = BTRFS_I(inode)->root;
16490caa102dSLi Zefan 	struct btrfs_trans_handle *trans;
16500caa102dSLi Zefan 	u64 root_flags;
16510caa102dSLi Zefan 	u64 flags;
16520caa102dSLi Zefan 	int ret = 0;
16530caa102dSLi Zefan 
1654b9ca0664SLiu Bo 	ret = mnt_want_write_file(file);
1655b9ca0664SLiu Bo 	if (ret)
1656b9ca0664SLiu Bo 		goto out;
16570caa102dSLi Zefan 
1658b9ca0664SLiu Bo 	if (btrfs_ino(inode) != BTRFS_FIRST_FREE_OBJECTID) {
1659b9ca0664SLiu Bo 		ret = -EINVAL;
1660b9ca0664SLiu Bo 		goto out_drop_write;
1661b9ca0664SLiu Bo 	}
16620caa102dSLi Zefan 
1663b9ca0664SLiu Bo 	if (copy_from_user(&flags, arg, sizeof(flags))) {
1664b9ca0664SLiu Bo 		ret = -EFAULT;
1665b9ca0664SLiu Bo 		goto out_drop_write;
1666b9ca0664SLiu Bo 	}
16670caa102dSLi Zefan 
1668b9ca0664SLiu Bo 	if (flags & BTRFS_SUBVOL_CREATE_ASYNC) {
1669b9ca0664SLiu Bo 		ret = -EINVAL;
1670b9ca0664SLiu Bo 		goto out_drop_write;
1671b9ca0664SLiu Bo 	}
16720caa102dSLi Zefan 
1673b9ca0664SLiu Bo 	if (flags & ~BTRFS_SUBVOL_RDONLY) {
1674b9ca0664SLiu Bo 		ret = -EOPNOTSUPP;
1675b9ca0664SLiu Bo 		goto out_drop_write;
1676b9ca0664SLiu Bo 	}
16770caa102dSLi Zefan 
1678b9ca0664SLiu Bo 	if (!inode_owner_or_capable(inode)) {
1679b9ca0664SLiu Bo 		ret = -EACCES;
1680b9ca0664SLiu Bo 		goto out_drop_write;
1681b9ca0664SLiu Bo 	}
1682b4dc2b8cSLi Zefan 
16830caa102dSLi Zefan 	down_write(&root->fs_info->subvol_sem);
16840caa102dSLi Zefan 
16850caa102dSLi Zefan 	/* nothing to do */
16860caa102dSLi Zefan 	if (!!(flags & BTRFS_SUBVOL_RDONLY) == btrfs_root_readonly(root))
1687b9ca0664SLiu Bo 		goto out_drop_sem;
16880caa102dSLi Zefan 
16890caa102dSLi Zefan 	root_flags = btrfs_root_flags(&root->root_item);
16900caa102dSLi Zefan 	if (flags & BTRFS_SUBVOL_RDONLY)
16910caa102dSLi Zefan 		btrfs_set_root_flags(&root->root_item,
16920caa102dSLi Zefan 				     root_flags | BTRFS_ROOT_SUBVOL_RDONLY);
16930caa102dSLi Zefan 	else
16940caa102dSLi Zefan 		btrfs_set_root_flags(&root->root_item,
16950caa102dSLi Zefan 				     root_flags & ~BTRFS_ROOT_SUBVOL_RDONLY);
16960caa102dSLi Zefan 
16970caa102dSLi Zefan 	trans = btrfs_start_transaction(root, 1);
16980caa102dSLi Zefan 	if (IS_ERR(trans)) {
16990caa102dSLi Zefan 		ret = PTR_ERR(trans);
17000caa102dSLi Zefan 		goto out_reset;
17010caa102dSLi Zefan 	}
17020caa102dSLi Zefan 
1703b4dc2b8cSLi Zefan 	ret = btrfs_update_root(trans, root->fs_info->tree_root,
17040caa102dSLi Zefan 				&root->root_key, &root->root_item);
17050caa102dSLi Zefan 
17060caa102dSLi Zefan 	btrfs_commit_transaction(trans, root);
17070caa102dSLi Zefan out_reset:
17080caa102dSLi Zefan 	if (ret)
17090caa102dSLi Zefan 		btrfs_set_root_flags(&root->root_item, root_flags);
1710b9ca0664SLiu Bo out_drop_sem:
17110caa102dSLi Zefan 	up_write(&root->fs_info->subvol_sem);
1712b9ca0664SLiu Bo out_drop_write:
1713b9ca0664SLiu Bo 	mnt_drop_write_file(file);
1714b9ca0664SLiu Bo out:
17150caa102dSLi Zefan 	return ret;
17160caa102dSLi Zefan }
17170caa102dSLi Zefan 
171876dda93cSYan, Zheng /*
171976dda93cSYan, Zheng  * helper to check if the subvolume references other subvolumes
172076dda93cSYan, Zheng  */
172176dda93cSYan, Zheng static noinline int may_destroy_subvol(struct btrfs_root *root)
172276dda93cSYan, Zheng {
172376dda93cSYan, Zheng 	struct btrfs_path *path;
172476dda93cSYan, Zheng 	struct btrfs_key key;
172576dda93cSYan, Zheng 	int ret;
172676dda93cSYan, Zheng 
172776dda93cSYan, Zheng 	path = btrfs_alloc_path();
172876dda93cSYan, Zheng 	if (!path)
172976dda93cSYan, Zheng 		return -ENOMEM;
173076dda93cSYan, Zheng 
173176dda93cSYan, Zheng 	key.objectid = root->root_key.objectid;
173276dda93cSYan, Zheng 	key.type = BTRFS_ROOT_REF_KEY;
173376dda93cSYan, Zheng 	key.offset = (u64)-1;
173476dda93cSYan, Zheng 
173576dda93cSYan, Zheng 	ret = btrfs_search_slot(NULL, root->fs_info->tree_root,
173676dda93cSYan, Zheng 				&key, path, 0, 0);
173776dda93cSYan, Zheng 	if (ret < 0)
173876dda93cSYan, Zheng 		goto out;
173976dda93cSYan, Zheng 	BUG_ON(ret == 0);
174076dda93cSYan, Zheng 
174176dda93cSYan, Zheng 	ret = 0;
174276dda93cSYan, Zheng 	if (path->slots[0] > 0) {
174376dda93cSYan, Zheng 		path->slots[0]--;
174476dda93cSYan, Zheng 		btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
174576dda93cSYan, Zheng 		if (key.objectid == root->root_key.objectid &&
174676dda93cSYan, Zheng 		    key.type == BTRFS_ROOT_REF_KEY)
174776dda93cSYan, Zheng 			ret = -ENOTEMPTY;
174876dda93cSYan, Zheng 	}
174976dda93cSYan, Zheng out:
175076dda93cSYan, Zheng 	btrfs_free_path(path);
175176dda93cSYan, Zheng 	return ret;
175276dda93cSYan, Zheng }
175376dda93cSYan, Zheng 
1754ac8e9819SChris Mason static noinline int key_in_sk(struct btrfs_key *key,
1755ac8e9819SChris Mason 			      struct btrfs_ioctl_search_key *sk)
1756ac8e9819SChris Mason {
1757abc6e134SChris Mason 	struct btrfs_key test;
1758abc6e134SChris Mason 	int ret;
1759abc6e134SChris Mason 
1760abc6e134SChris Mason 	test.objectid = sk->min_objectid;
1761abc6e134SChris Mason 	test.type = sk->min_type;
1762abc6e134SChris Mason 	test.offset = sk->min_offset;
1763abc6e134SChris Mason 
1764abc6e134SChris Mason 	ret = btrfs_comp_cpu_keys(key, &test);
1765abc6e134SChris Mason 	if (ret < 0)
1766ac8e9819SChris Mason 		return 0;
1767abc6e134SChris Mason 
1768abc6e134SChris Mason 	test.objectid = sk->max_objectid;
1769abc6e134SChris Mason 	test.type = sk->max_type;
1770abc6e134SChris Mason 	test.offset = sk->max_offset;
1771abc6e134SChris Mason 
1772abc6e134SChris Mason 	ret = btrfs_comp_cpu_keys(key, &test);
1773abc6e134SChris Mason 	if (ret > 0)
1774ac8e9819SChris Mason 		return 0;
1775ac8e9819SChris Mason 	return 1;
1776ac8e9819SChris Mason }
1777ac8e9819SChris Mason 
1778ac8e9819SChris Mason static noinline int copy_to_sk(struct btrfs_root *root,
1779ac8e9819SChris Mason 			       struct btrfs_path *path,
1780ac8e9819SChris Mason 			       struct btrfs_key *key,
1781ac8e9819SChris Mason 			       struct btrfs_ioctl_search_key *sk,
1782ac8e9819SChris Mason 			       char *buf,
1783ac8e9819SChris Mason 			       unsigned long *sk_offset,
1784ac8e9819SChris Mason 			       int *num_found)
1785ac8e9819SChris Mason {
1786ac8e9819SChris Mason 	u64 found_transid;
1787ac8e9819SChris Mason 	struct extent_buffer *leaf;
1788ac8e9819SChris Mason 	struct btrfs_ioctl_search_header sh;
1789ac8e9819SChris Mason 	unsigned long item_off;
1790ac8e9819SChris Mason 	unsigned long item_len;
1791ac8e9819SChris Mason 	int nritems;
1792ac8e9819SChris Mason 	int i;
1793ac8e9819SChris Mason 	int slot;
1794ac8e9819SChris Mason 	int ret = 0;
1795ac8e9819SChris Mason 
1796ac8e9819SChris Mason 	leaf = path->nodes[0];
1797ac8e9819SChris Mason 	slot = path->slots[0];
1798ac8e9819SChris Mason 	nritems = btrfs_header_nritems(leaf);
1799ac8e9819SChris Mason 
1800ac8e9819SChris Mason 	if (btrfs_header_generation(leaf) > sk->max_transid) {
1801ac8e9819SChris Mason 		i = nritems;
1802ac8e9819SChris Mason 		goto advance_key;
1803ac8e9819SChris Mason 	}
1804ac8e9819SChris Mason 	found_transid = btrfs_header_generation(leaf);
1805ac8e9819SChris Mason 
1806ac8e9819SChris Mason 	for (i = slot; i < nritems; i++) {
1807ac8e9819SChris Mason 		item_off = btrfs_item_ptr_offset(leaf, i);
1808ac8e9819SChris Mason 		item_len = btrfs_item_size_nr(leaf, i);
1809ac8e9819SChris Mason 
181003b71c6cSGabriel de Perthuis 		btrfs_item_key_to_cpu(leaf, key, i);
181103b71c6cSGabriel de Perthuis 		if (!key_in_sk(key, sk))
181203b71c6cSGabriel de Perthuis 			continue;
181303b71c6cSGabriel de Perthuis 
181403b71c6cSGabriel de Perthuis 		if (sizeof(sh) + item_len > BTRFS_SEARCH_ARGS_BUFSIZE)
1815ac8e9819SChris Mason 			item_len = 0;
1816ac8e9819SChris Mason 
1817ac8e9819SChris Mason 		if (sizeof(sh) + item_len + *sk_offset >
1818ac8e9819SChris Mason 		    BTRFS_SEARCH_ARGS_BUFSIZE) {
1819ac8e9819SChris Mason 			ret = 1;
1820ac8e9819SChris Mason 			goto overflow;
1821ac8e9819SChris Mason 		}
1822ac8e9819SChris Mason 
1823ac8e9819SChris Mason 		sh.objectid = key->objectid;
1824ac8e9819SChris Mason 		sh.offset = key->offset;
1825ac8e9819SChris Mason 		sh.type = key->type;
1826ac8e9819SChris Mason 		sh.len = item_len;
1827ac8e9819SChris Mason 		sh.transid = found_transid;
1828ac8e9819SChris Mason 
1829ac8e9819SChris Mason 		/* copy search result header */
1830ac8e9819SChris Mason 		memcpy(buf + *sk_offset, &sh, sizeof(sh));
1831ac8e9819SChris Mason 		*sk_offset += sizeof(sh);
1832ac8e9819SChris Mason 
1833ac8e9819SChris Mason 		if (item_len) {
1834ac8e9819SChris Mason 			char *p = buf + *sk_offset;
1835ac8e9819SChris Mason 			/* copy the item */
1836ac8e9819SChris Mason 			read_extent_buffer(leaf, p,
1837ac8e9819SChris Mason 					   item_off, item_len);
1838ac8e9819SChris Mason 			*sk_offset += item_len;
1839ac8e9819SChris Mason 		}
1840e2156867SHugo Mills 		(*num_found)++;
1841ac8e9819SChris Mason 
1842ac8e9819SChris Mason 		if (*num_found >= sk->nr_items)
1843ac8e9819SChris Mason 			break;
1844ac8e9819SChris Mason 	}
1845ac8e9819SChris Mason advance_key:
1846ac8e9819SChris Mason 	ret = 0;
1847abc6e134SChris Mason 	if (key->offset < (u64)-1 && key->offset < sk->max_offset)
1848abc6e134SChris Mason 		key->offset++;
1849abc6e134SChris Mason 	else if (key->type < (u8)-1 && key->type < sk->max_type) {
1850abc6e134SChris Mason 		key->offset = 0;
1851abc6e134SChris Mason 		key->type++;
1852abc6e134SChris Mason 	} else if (key->objectid < (u64)-1 && key->objectid < sk->max_objectid) {
1853abc6e134SChris Mason 		key->offset = 0;
1854abc6e134SChris Mason 		key->type = 0;
1855abc6e134SChris Mason 		key->objectid++;
1856abc6e134SChris Mason 	} else
1857abc6e134SChris Mason 		ret = 1;
1858ac8e9819SChris Mason overflow:
1859ac8e9819SChris Mason 	return ret;
1860ac8e9819SChris Mason }
1861ac8e9819SChris Mason 
1862ac8e9819SChris Mason static noinline int search_ioctl(struct inode *inode,
1863ac8e9819SChris Mason 				 struct btrfs_ioctl_search_args *args)
1864ac8e9819SChris Mason {
1865ac8e9819SChris Mason 	struct btrfs_root *root;
1866ac8e9819SChris Mason 	struct btrfs_key key;
1867ac8e9819SChris Mason 	struct btrfs_key max_key;
1868ac8e9819SChris Mason 	struct btrfs_path *path;
1869ac8e9819SChris Mason 	struct btrfs_ioctl_search_key *sk = &args->key;
1870ac8e9819SChris Mason 	struct btrfs_fs_info *info = BTRFS_I(inode)->root->fs_info;
1871ac8e9819SChris Mason 	int ret;
1872ac8e9819SChris Mason 	int num_found = 0;
1873ac8e9819SChris Mason 	unsigned long sk_offset = 0;
1874ac8e9819SChris Mason 
1875ac8e9819SChris Mason 	path = btrfs_alloc_path();
1876ac8e9819SChris Mason 	if (!path)
1877ac8e9819SChris Mason 		return -ENOMEM;
1878ac8e9819SChris Mason 
1879ac8e9819SChris Mason 	if (sk->tree_id == 0) {
1880ac8e9819SChris Mason 		/* search the root of the inode that was passed */
1881ac8e9819SChris Mason 		root = BTRFS_I(inode)->root;
1882ac8e9819SChris Mason 	} else {
1883ac8e9819SChris Mason 		key.objectid = sk->tree_id;
1884ac8e9819SChris Mason 		key.type = BTRFS_ROOT_ITEM_KEY;
1885ac8e9819SChris Mason 		key.offset = (u64)-1;
1886ac8e9819SChris Mason 		root = btrfs_read_fs_root_no_name(info, &key);
1887ac8e9819SChris Mason 		if (IS_ERR(root)) {
1888ac8e9819SChris Mason 			printk(KERN_ERR "could not find root %llu\n",
1889ac8e9819SChris Mason 			       sk->tree_id);
1890ac8e9819SChris Mason 			btrfs_free_path(path);
1891ac8e9819SChris Mason 			return -ENOENT;
1892ac8e9819SChris Mason 		}
1893ac8e9819SChris Mason 	}
1894ac8e9819SChris Mason 
1895ac8e9819SChris Mason 	key.objectid = sk->min_objectid;
1896ac8e9819SChris Mason 	key.type = sk->min_type;
1897ac8e9819SChris Mason 	key.offset = sk->min_offset;
1898ac8e9819SChris Mason 
1899ac8e9819SChris Mason 	max_key.objectid = sk->max_objectid;
1900ac8e9819SChris Mason 	max_key.type = sk->max_type;
1901ac8e9819SChris Mason 	max_key.offset = sk->max_offset;
1902ac8e9819SChris Mason 
1903ac8e9819SChris Mason 	path->keep_locks = 1;
1904ac8e9819SChris Mason 
1905ac8e9819SChris Mason 	while(1) {
1906de78b51aSEric Sandeen 		ret = btrfs_search_forward(root, &key, &max_key, path,
1907ac8e9819SChris Mason 					   sk->min_transid);
1908ac8e9819SChris Mason 		if (ret != 0) {
1909ac8e9819SChris Mason 			if (ret > 0)
1910ac8e9819SChris Mason 				ret = 0;
1911ac8e9819SChris Mason 			goto err;
1912ac8e9819SChris Mason 		}
1913ac8e9819SChris Mason 		ret = copy_to_sk(root, path, &key, sk, args->buf,
1914ac8e9819SChris Mason 				 &sk_offset, &num_found);
1915b3b4aa74SDavid Sterba 		btrfs_release_path(path);
1916ac8e9819SChris Mason 		if (ret || num_found >= sk->nr_items)
1917ac8e9819SChris Mason 			break;
1918ac8e9819SChris Mason 
1919ac8e9819SChris Mason 	}
1920ac8e9819SChris Mason 	ret = 0;
1921ac8e9819SChris Mason err:
1922ac8e9819SChris Mason 	sk->nr_items = num_found;
1923ac8e9819SChris Mason 	btrfs_free_path(path);
1924ac8e9819SChris Mason 	return ret;
1925ac8e9819SChris Mason }
1926ac8e9819SChris Mason 
1927ac8e9819SChris Mason static noinline int btrfs_ioctl_tree_search(struct file *file,
1928ac8e9819SChris Mason 					   void __user *argp)
1929ac8e9819SChris Mason {
1930ac8e9819SChris Mason 	 struct btrfs_ioctl_search_args *args;
1931ac8e9819SChris Mason 	 struct inode *inode;
1932ac8e9819SChris Mason 	 int ret;
1933ac8e9819SChris Mason 
1934ac8e9819SChris Mason 	if (!capable(CAP_SYS_ADMIN))
1935ac8e9819SChris Mason 		return -EPERM;
1936ac8e9819SChris Mason 
19372354d08fSJulia Lawall 	args = memdup_user(argp, sizeof(*args));
19382354d08fSJulia Lawall 	if (IS_ERR(args))
19392354d08fSJulia Lawall 		return PTR_ERR(args);
1940ac8e9819SChris Mason 
1941496ad9aaSAl Viro 	inode = file_inode(file);
1942ac8e9819SChris Mason 	ret = search_ioctl(inode, args);
1943ac8e9819SChris Mason 	if (ret == 0 && copy_to_user(argp, args, sizeof(*args)))
1944ac8e9819SChris Mason 		ret = -EFAULT;
1945ac8e9819SChris Mason 	kfree(args);
1946ac8e9819SChris Mason 	return ret;
1947ac8e9819SChris Mason }
1948ac8e9819SChris Mason 
194998d377a0STARUISI Hiroaki /*
1950ac8e9819SChris Mason  * Search INODE_REFs to identify path name of 'dirid' directory
1951ac8e9819SChris Mason  * in a 'tree_id' tree. and sets path name to 'name'.
195298d377a0STARUISI Hiroaki  */
195398d377a0STARUISI Hiroaki static noinline int btrfs_search_path_in_tree(struct btrfs_fs_info *info,
195498d377a0STARUISI Hiroaki 				u64 tree_id, u64 dirid, char *name)
195598d377a0STARUISI Hiroaki {
195698d377a0STARUISI Hiroaki 	struct btrfs_root *root;
195798d377a0STARUISI Hiroaki 	struct btrfs_key key;
1958ac8e9819SChris Mason 	char *ptr;
195998d377a0STARUISI Hiroaki 	int ret = -1;
196098d377a0STARUISI Hiroaki 	int slot;
196198d377a0STARUISI Hiroaki 	int len;
196298d377a0STARUISI Hiroaki 	int total_len = 0;
196398d377a0STARUISI Hiroaki 	struct btrfs_inode_ref *iref;
196498d377a0STARUISI Hiroaki 	struct extent_buffer *l;
196598d377a0STARUISI Hiroaki 	struct btrfs_path *path;
196698d377a0STARUISI Hiroaki 
196798d377a0STARUISI Hiroaki 	if (dirid == BTRFS_FIRST_FREE_OBJECTID) {
196898d377a0STARUISI Hiroaki 		name[0]='\0';
196998d377a0STARUISI Hiroaki 		return 0;
197098d377a0STARUISI Hiroaki 	}
197198d377a0STARUISI Hiroaki 
197298d377a0STARUISI Hiroaki 	path = btrfs_alloc_path();
197398d377a0STARUISI Hiroaki 	if (!path)
197498d377a0STARUISI Hiroaki 		return -ENOMEM;
197598d377a0STARUISI Hiroaki 
1976ac8e9819SChris Mason 	ptr = &name[BTRFS_INO_LOOKUP_PATH_MAX];
197798d377a0STARUISI Hiroaki 
197898d377a0STARUISI Hiroaki 	key.objectid = tree_id;
197998d377a0STARUISI Hiroaki 	key.type = BTRFS_ROOT_ITEM_KEY;
198098d377a0STARUISI Hiroaki 	key.offset = (u64)-1;
198198d377a0STARUISI Hiroaki 	root = btrfs_read_fs_root_no_name(info, &key);
198298d377a0STARUISI Hiroaki 	if (IS_ERR(root)) {
198398d377a0STARUISI Hiroaki 		printk(KERN_ERR "could not find root %llu\n", tree_id);
19848ad6fcabSChris Mason 		ret = -ENOENT;
19858ad6fcabSChris Mason 		goto out;
198698d377a0STARUISI Hiroaki 	}
198798d377a0STARUISI Hiroaki 
198898d377a0STARUISI Hiroaki 	key.objectid = dirid;
198998d377a0STARUISI Hiroaki 	key.type = BTRFS_INODE_REF_KEY;
19908ad6fcabSChris Mason 	key.offset = (u64)-1;
199198d377a0STARUISI Hiroaki 
199298d377a0STARUISI Hiroaki 	while(1) {
199398d377a0STARUISI Hiroaki 		ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
199498d377a0STARUISI Hiroaki 		if (ret < 0)
199598d377a0STARUISI Hiroaki 			goto out;
199698d377a0STARUISI Hiroaki 
199798d377a0STARUISI Hiroaki 		l = path->nodes[0];
199898d377a0STARUISI Hiroaki 		slot = path->slots[0];
19998ad6fcabSChris Mason 		if (ret > 0 && slot > 0)
20008ad6fcabSChris Mason 			slot--;
200198d377a0STARUISI Hiroaki 		btrfs_item_key_to_cpu(l, &key, slot);
200298d377a0STARUISI Hiroaki 
200398d377a0STARUISI Hiroaki 		if (ret > 0 && (key.objectid != dirid ||
2004ac8e9819SChris Mason 				key.type != BTRFS_INODE_REF_KEY)) {
2005ac8e9819SChris Mason 			ret = -ENOENT;
200698d377a0STARUISI Hiroaki 			goto out;
2007ac8e9819SChris Mason 		}
200898d377a0STARUISI Hiroaki 
200998d377a0STARUISI Hiroaki 		iref = btrfs_item_ptr(l, slot, struct btrfs_inode_ref);
201098d377a0STARUISI Hiroaki 		len = btrfs_inode_ref_name_len(l, iref);
201198d377a0STARUISI Hiroaki 		ptr -= len + 1;
201298d377a0STARUISI Hiroaki 		total_len += len + 1;
2013ac8e9819SChris Mason 		if (ptr < name)
201498d377a0STARUISI Hiroaki 			goto out;
201598d377a0STARUISI Hiroaki 
201698d377a0STARUISI Hiroaki 		*(ptr + len) = '/';
201798d377a0STARUISI Hiroaki 		read_extent_buffer(l, ptr,(unsigned long)(iref + 1), len);
201898d377a0STARUISI Hiroaki 
201998d377a0STARUISI Hiroaki 		if (key.offset == BTRFS_FIRST_FREE_OBJECTID)
202098d377a0STARUISI Hiroaki 			break;
202198d377a0STARUISI Hiroaki 
2022b3b4aa74SDavid Sterba 		btrfs_release_path(path);
202398d377a0STARUISI Hiroaki 		key.objectid = key.offset;
20248ad6fcabSChris Mason 		key.offset = (u64)-1;
202598d377a0STARUISI Hiroaki 		dirid = key.objectid;
202698d377a0STARUISI Hiroaki 	}
2027ac8e9819SChris Mason 	if (ptr < name)
202898d377a0STARUISI Hiroaki 		goto out;
202977906a50SLi Zefan 	memmove(name, ptr, total_len);
203098d377a0STARUISI Hiroaki 	name[total_len]='\0';
203198d377a0STARUISI Hiroaki 	ret = 0;
203298d377a0STARUISI Hiroaki out:
203398d377a0STARUISI Hiroaki 	btrfs_free_path(path);
2034ac8e9819SChris Mason 	return ret;
2035ac8e9819SChris Mason }
2036ac8e9819SChris Mason 
2037ac8e9819SChris Mason static noinline int btrfs_ioctl_ino_lookup(struct file *file,
2038ac8e9819SChris Mason 					   void __user *argp)
2039ac8e9819SChris Mason {
2040ac8e9819SChris Mason 	 struct btrfs_ioctl_ino_lookup_args *args;
2041ac8e9819SChris Mason 	 struct inode *inode;
2042ac8e9819SChris Mason 	 int ret;
2043ac8e9819SChris Mason 
2044ac8e9819SChris Mason 	if (!capable(CAP_SYS_ADMIN))
2045ac8e9819SChris Mason 		return -EPERM;
2046ac8e9819SChris Mason 
20472354d08fSJulia Lawall 	args = memdup_user(argp, sizeof(*args));
20482354d08fSJulia Lawall 	if (IS_ERR(args))
20492354d08fSJulia Lawall 		return PTR_ERR(args);
2050c2b96929SDan Carpenter 
2051496ad9aaSAl Viro 	inode = file_inode(file);
2052ac8e9819SChris Mason 
20531b53ac4dSChris Mason 	if (args->treeid == 0)
20541b53ac4dSChris Mason 		args->treeid = BTRFS_I(inode)->root->root_key.objectid;
20551b53ac4dSChris Mason 
2056ac8e9819SChris Mason 	ret = btrfs_search_path_in_tree(BTRFS_I(inode)->root->fs_info,
2057ac8e9819SChris Mason 					args->treeid, args->objectid,
2058ac8e9819SChris Mason 					args->name);
2059ac8e9819SChris Mason 
2060ac8e9819SChris Mason 	if (ret == 0 && copy_to_user(argp, args, sizeof(*args)))
2061ac8e9819SChris Mason 		ret = -EFAULT;
2062ac8e9819SChris Mason 
2063ac8e9819SChris Mason 	kfree(args);
206498d377a0STARUISI Hiroaki 	return ret;
206598d377a0STARUISI Hiroaki }
206698d377a0STARUISI Hiroaki 
206776dda93cSYan, Zheng static noinline int btrfs_ioctl_snap_destroy(struct file *file,
206876dda93cSYan, Zheng 					     void __user *arg)
206976dda93cSYan, Zheng {
207076dda93cSYan, Zheng 	struct dentry *parent = fdentry(file);
207176dda93cSYan, Zheng 	struct dentry *dentry;
207276dda93cSYan, Zheng 	struct inode *dir = parent->d_inode;
207376dda93cSYan, Zheng 	struct inode *inode;
207476dda93cSYan, Zheng 	struct btrfs_root *root = BTRFS_I(dir)->root;
207576dda93cSYan, Zheng 	struct btrfs_root *dest = NULL;
207676dda93cSYan, Zheng 	struct btrfs_ioctl_vol_args *vol_args;
207776dda93cSYan, Zheng 	struct btrfs_trans_handle *trans;
2078c58aaad2SMiao Xie 	struct btrfs_block_rsv block_rsv;
2079c58aaad2SMiao Xie 	u64 qgroup_reserved;
208076dda93cSYan, Zheng 	int namelen;
208176dda93cSYan, Zheng 	int ret;
208276dda93cSYan, Zheng 	int err = 0;
208376dda93cSYan, Zheng 
208476dda93cSYan, Zheng 	vol_args = memdup_user(arg, sizeof(*vol_args));
208576dda93cSYan, Zheng 	if (IS_ERR(vol_args))
208676dda93cSYan, Zheng 		return PTR_ERR(vol_args);
208776dda93cSYan, Zheng 
208876dda93cSYan, Zheng 	vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
208976dda93cSYan, Zheng 	namelen = strlen(vol_args->name);
209076dda93cSYan, Zheng 	if (strchr(vol_args->name, '/') ||
209176dda93cSYan, Zheng 	    strncmp(vol_args->name, "..", namelen) == 0) {
209276dda93cSYan, Zheng 		err = -EINVAL;
209376dda93cSYan, Zheng 		goto out;
209476dda93cSYan, Zheng 	}
209576dda93cSYan, Zheng 
2096a561be71SAl Viro 	err = mnt_want_write_file(file);
209776dda93cSYan, Zheng 	if (err)
209876dda93cSYan, Zheng 		goto out;
209976dda93cSYan, Zheng 
21005c50c9b8SDavid Sterba 	err = mutex_lock_killable_nested(&dir->i_mutex, I_MUTEX_PARENT);
21015c50c9b8SDavid Sterba 	if (err == -EINTR)
21025c50c9b8SDavid Sterba 		goto out;
210376dda93cSYan, Zheng 	dentry = lookup_one_len(vol_args->name, parent, namelen);
210476dda93cSYan, Zheng 	if (IS_ERR(dentry)) {
210576dda93cSYan, Zheng 		err = PTR_ERR(dentry);
210676dda93cSYan, Zheng 		goto out_unlock_dir;
210776dda93cSYan, Zheng 	}
210876dda93cSYan, Zheng 
210976dda93cSYan, Zheng 	if (!dentry->d_inode) {
211076dda93cSYan, Zheng 		err = -ENOENT;
211176dda93cSYan, Zheng 		goto out_dput;
211276dda93cSYan, Zheng 	}
211376dda93cSYan, Zheng 
211476dda93cSYan, Zheng 	inode = dentry->d_inode;
21154260f7c7SSage Weil 	dest = BTRFS_I(inode)->root;
21164260f7c7SSage Weil 	if (!capable(CAP_SYS_ADMIN)){
21174260f7c7SSage Weil 		/*
21184260f7c7SSage Weil 		 * Regular user.  Only allow this with a special mount
21194260f7c7SSage Weil 		 * option, when the user has write+exec access to the
21204260f7c7SSage Weil 		 * subvol root, and when rmdir(2) would have been
21214260f7c7SSage Weil 		 * allowed.
21224260f7c7SSage Weil 		 *
21234260f7c7SSage Weil 		 * Note that this is _not_ check that the subvol is
21244260f7c7SSage Weil 		 * empty or doesn't contain data that we wouldn't
21254260f7c7SSage Weil 		 * otherwise be able to delete.
21264260f7c7SSage Weil 		 *
21274260f7c7SSage Weil 		 * Users who want to delete empty subvols should try
21284260f7c7SSage Weil 		 * rmdir(2).
21294260f7c7SSage Weil 		 */
21304260f7c7SSage Weil 		err = -EPERM;
21314260f7c7SSage Weil 		if (!btrfs_test_opt(root, USER_SUBVOL_RM_ALLOWED))
21324260f7c7SSage Weil 			goto out_dput;
21334260f7c7SSage Weil 
21344260f7c7SSage Weil 		/*
21354260f7c7SSage Weil 		 * Do not allow deletion if the parent dir is the same
21364260f7c7SSage Weil 		 * as the dir to be deleted.  That means the ioctl
21374260f7c7SSage Weil 		 * must be called on the dentry referencing the root
21384260f7c7SSage Weil 		 * of the subvol, not a random directory contained
21394260f7c7SSage Weil 		 * within it.
21404260f7c7SSage Weil 		 */
21414260f7c7SSage Weil 		err = -EINVAL;
21424260f7c7SSage Weil 		if (root == dest)
21434260f7c7SSage Weil 			goto out_dput;
21444260f7c7SSage Weil 
21454260f7c7SSage Weil 		err = inode_permission(inode, MAY_WRITE | MAY_EXEC);
21464260f7c7SSage Weil 		if (err)
21474260f7c7SSage Weil 			goto out_dput;
21485c39da5bSMiao Xie 	}
21494260f7c7SSage Weil 
21505c39da5bSMiao Xie 	/* check if subvolume may be deleted by a user */
21514260f7c7SSage Weil 	err = btrfs_may_delete(dir, dentry, 1);
21524260f7c7SSage Weil 	if (err)
21534260f7c7SSage Weil 		goto out_dput;
21544260f7c7SSage Weil 
215533345d01SLi Zefan 	if (btrfs_ino(inode) != BTRFS_FIRST_FREE_OBJECTID) {
215676dda93cSYan, Zheng 		err = -EINVAL;
215776dda93cSYan, Zheng 		goto out_dput;
215876dda93cSYan, Zheng 	}
215976dda93cSYan, Zheng 
216076dda93cSYan, Zheng 	mutex_lock(&inode->i_mutex);
216176dda93cSYan, Zheng 	err = d_invalidate(dentry);
216276dda93cSYan, Zheng 	if (err)
216376dda93cSYan, Zheng 		goto out_unlock;
216476dda93cSYan, Zheng 
216576dda93cSYan, Zheng 	down_write(&root->fs_info->subvol_sem);
216676dda93cSYan, Zheng 
216776dda93cSYan, Zheng 	err = may_destroy_subvol(dest);
216876dda93cSYan, Zheng 	if (err)
216976dda93cSYan, Zheng 		goto out_up_write;
217076dda93cSYan, Zheng 
2171c58aaad2SMiao Xie 	btrfs_init_block_rsv(&block_rsv, BTRFS_BLOCK_RSV_TEMP);
2172c58aaad2SMiao Xie 	/*
2173c58aaad2SMiao Xie 	 * One for dir inode, two for dir entries, two for root
2174c58aaad2SMiao Xie 	 * ref/backref.
2175c58aaad2SMiao Xie 	 */
2176c58aaad2SMiao Xie 	err = btrfs_subvolume_reserve_metadata(root, &block_rsv,
2177c58aaad2SMiao Xie 					       5, &qgroup_reserved);
2178c58aaad2SMiao Xie 	if (err)
2179c58aaad2SMiao Xie 		goto out_up_write;
2180c58aaad2SMiao Xie 
2181a22285a6SYan, Zheng 	trans = btrfs_start_transaction(root, 0);
2182a22285a6SYan, Zheng 	if (IS_ERR(trans)) {
2183a22285a6SYan, Zheng 		err = PTR_ERR(trans);
2184c58aaad2SMiao Xie 		goto out_release;
2185a22285a6SYan, Zheng 	}
2186c58aaad2SMiao Xie 	trans->block_rsv = &block_rsv;
2187c58aaad2SMiao Xie 	trans->bytes_reserved = block_rsv.size;
2188a22285a6SYan, Zheng 
218976dda93cSYan, Zheng 	ret = btrfs_unlink_subvol(trans, root, dir,
219076dda93cSYan, Zheng 				dest->root_key.objectid,
219176dda93cSYan, Zheng 				dentry->d_name.name,
219276dda93cSYan, Zheng 				dentry->d_name.len);
219379787eaaSJeff Mahoney 	if (ret) {
219479787eaaSJeff Mahoney 		err = ret;
219579787eaaSJeff Mahoney 		btrfs_abort_transaction(trans, root, ret);
219679787eaaSJeff Mahoney 		goto out_end_trans;
219779787eaaSJeff Mahoney 	}
219876dda93cSYan, Zheng 
219976dda93cSYan, Zheng 	btrfs_record_root_in_trans(trans, dest);
220076dda93cSYan, Zheng 
220176dda93cSYan, Zheng 	memset(&dest->root_item.drop_progress, 0,
220276dda93cSYan, Zheng 		sizeof(dest->root_item.drop_progress));
220376dda93cSYan, Zheng 	dest->root_item.drop_level = 0;
220476dda93cSYan, Zheng 	btrfs_set_root_refs(&dest->root_item, 0);
220576dda93cSYan, Zheng 
2206d68fc57bSYan, Zheng 	if (!xchg(&dest->orphan_item_inserted, 1)) {
220776dda93cSYan, Zheng 		ret = btrfs_insert_orphan_item(trans,
220876dda93cSYan, Zheng 					root->fs_info->tree_root,
220976dda93cSYan, Zheng 					dest->root_key.objectid);
221079787eaaSJeff Mahoney 		if (ret) {
221179787eaaSJeff Mahoney 			btrfs_abort_transaction(trans, root, ret);
221279787eaaSJeff Mahoney 			err = ret;
221379787eaaSJeff Mahoney 			goto out_end_trans;
2214d68fc57bSYan, Zheng 		}
221579787eaaSJeff Mahoney 	}
221679787eaaSJeff Mahoney out_end_trans:
2217c58aaad2SMiao Xie 	trans->block_rsv = NULL;
2218c58aaad2SMiao Xie 	trans->bytes_reserved = 0;
2219531cb13fSSage Weil 	ret = btrfs_end_transaction(trans, root);
222079787eaaSJeff Mahoney 	if (ret && !err)
222179787eaaSJeff Mahoney 		err = ret;
222276dda93cSYan, Zheng 	inode->i_flags |= S_DEAD;
2223c58aaad2SMiao Xie out_release:
2224c58aaad2SMiao Xie 	btrfs_subvolume_release_metadata(root, &block_rsv, qgroup_reserved);
222576dda93cSYan, Zheng out_up_write:
222676dda93cSYan, Zheng 	up_write(&root->fs_info->subvol_sem);
222776dda93cSYan, Zheng out_unlock:
222876dda93cSYan, Zheng 	mutex_unlock(&inode->i_mutex);
222976dda93cSYan, Zheng 	if (!err) {
2230efefb143SYan, Zheng 		shrink_dcache_sb(root->fs_info->sb);
223176dda93cSYan, Zheng 		btrfs_invalidate_inodes(dest);
223276dda93cSYan, Zheng 		d_delete(dentry);
2233fa6ac876SLiu Bo 
2234fa6ac876SLiu Bo 		/* the last ref */
2235fa6ac876SLiu Bo 		if (dest->cache_inode) {
2236fa6ac876SLiu Bo 			iput(dest->cache_inode);
2237fa6ac876SLiu Bo 			dest->cache_inode = NULL;
2238fa6ac876SLiu Bo 		}
223976dda93cSYan, Zheng 	}
224076dda93cSYan, Zheng out_dput:
224176dda93cSYan, Zheng 	dput(dentry);
224276dda93cSYan, Zheng out_unlock_dir:
224376dda93cSYan, Zheng 	mutex_unlock(&dir->i_mutex);
22442a79f17eSAl Viro 	mnt_drop_write_file(file);
224576dda93cSYan, Zheng out:
224676dda93cSYan, Zheng 	kfree(vol_args);
224776dda93cSYan, Zheng 	return err;
224876dda93cSYan, Zheng }
224976dda93cSYan, Zheng 
22501e701a32SChris Mason static int btrfs_ioctl_defrag(struct file *file, void __user *argp)
2251f46b5a66SChristoph Hellwig {
2252496ad9aaSAl Viro 	struct inode *inode = file_inode(file);
2253f46b5a66SChristoph Hellwig 	struct btrfs_root *root = BTRFS_I(inode)->root;
22541e701a32SChris Mason 	struct btrfs_ioctl_defrag_range_args *range;
2255c146afadSYan Zheng 	int ret;
2256c146afadSYan Zheng 
225725122d15SIlya Dryomov 	ret = mnt_want_write_file(file);
225825122d15SIlya Dryomov 	if (ret)
225925122d15SIlya Dryomov 		return ret;
2260b83cc969SLi Zefan 
226125122d15SIlya Dryomov 	if (btrfs_root_readonly(root)) {
226225122d15SIlya Dryomov 		ret = -EROFS;
226325122d15SIlya Dryomov 		goto out;
22645ac00addSStefan Behrens 	}
2265f46b5a66SChristoph Hellwig 
2266f46b5a66SChristoph Hellwig 	switch (inode->i_mode & S_IFMT) {
2267f46b5a66SChristoph Hellwig 	case S_IFDIR:
2268e441d54dSChris Mason 		if (!capable(CAP_SYS_ADMIN)) {
2269e441d54dSChris Mason 			ret = -EPERM;
2270e441d54dSChris Mason 			goto out;
2271e441d54dSChris Mason 		}
2272de78b51aSEric Sandeen 		ret = btrfs_defrag_root(root);
22738929ecfaSYan, Zheng 		if (ret)
22748929ecfaSYan, Zheng 			goto out;
2275de78b51aSEric Sandeen 		ret = btrfs_defrag_root(root->fs_info->extent_root);
2276f46b5a66SChristoph Hellwig 		break;
2277f46b5a66SChristoph Hellwig 	case S_IFREG:
2278e441d54dSChris Mason 		if (!(file->f_mode & FMODE_WRITE)) {
2279e441d54dSChris Mason 			ret = -EINVAL;
2280e441d54dSChris Mason 			goto out;
2281e441d54dSChris Mason 		}
22821e701a32SChris Mason 
22831e701a32SChris Mason 		range = kzalloc(sizeof(*range), GFP_KERNEL);
22841e701a32SChris Mason 		if (!range) {
22851e701a32SChris Mason 			ret = -ENOMEM;
22861e701a32SChris Mason 			goto out;
22871e701a32SChris Mason 		}
22881e701a32SChris Mason 
22891e701a32SChris Mason 		if (argp) {
22901e701a32SChris Mason 			if (copy_from_user(range, argp,
22911e701a32SChris Mason 					   sizeof(*range))) {
22921e701a32SChris Mason 				ret = -EFAULT;
22931e701a32SChris Mason 				kfree(range);
2294683be16eSDan Carpenter 				goto out;
22951e701a32SChris Mason 			}
22961e701a32SChris Mason 			/* compression requires us to start the IO */
22971e701a32SChris Mason 			if ((range->flags & BTRFS_DEFRAG_RANGE_COMPRESS)) {
22981e701a32SChris Mason 				range->flags |= BTRFS_DEFRAG_RANGE_START_IO;
22991e701a32SChris Mason 				range->extent_thresh = (u32)-1;
23001e701a32SChris Mason 			}
23011e701a32SChris Mason 		} else {
23021e701a32SChris Mason 			/* the rest are all set to zero by kzalloc */
23031e701a32SChris Mason 			range->len = (u64)-1;
23041e701a32SChris Mason 		}
2305496ad9aaSAl Viro 		ret = btrfs_defrag_file(file_inode(file), file,
23064cb5300bSChris Mason 					range, 0, 0);
23074cb5300bSChris Mason 		if (ret > 0)
23084cb5300bSChris Mason 			ret = 0;
23091e701a32SChris Mason 		kfree(range);
2310f46b5a66SChristoph Hellwig 		break;
23118929ecfaSYan, Zheng 	default:
23128929ecfaSYan, Zheng 		ret = -EINVAL;
2313f46b5a66SChristoph Hellwig 	}
2314e441d54dSChris Mason out:
231525122d15SIlya Dryomov 	mnt_drop_write_file(file);
2316e441d54dSChris Mason 	return ret;
2317f46b5a66SChristoph Hellwig }
2318f46b5a66SChristoph Hellwig 
2319b2950863SChristoph Hellwig static long btrfs_ioctl_add_dev(struct btrfs_root *root, void __user *arg)
2320f46b5a66SChristoph Hellwig {
2321f46b5a66SChristoph Hellwig 	struct btrfs_ioctl_vol_args *vol_args;
2322f46b5a66SChristoph Hellwig 	int ret;
2323f46b5a66SChristoph Hellwig 
2324e441d54dSChris Mason 	if (!capable(CAP_SYS_ADMIN))
2325e441d54dSChris Mason 		return -EPERM;
2326e441d54dSChris Mason 
23275ac00addSStefan Behrens 	if (atomic_xchg(&root->fs_info->mutually_exclusive_operation_running,
23285ac00addSStefan Behrens 			1)) {
23295ac00addSStefan Behrens 		pr_info("btrfs: dev add/delete/balance/replace/resize operation in progress\n");
23302c0c9da0SIlya Dryomov 		return -EINVAL;
2331c9e9f97bSIlya Dryomov 	}
2332c9e9f97bSIlya Dryomov 
23335ac00addSStefan Behrens 	mutex_lock(&root->fs_info->volume_mutex);
2334dae7b665SLi Zefan 	vol_args = memdup_user(arg, sizeof(*vol_args));
2335c9e9f97bSIlya Dryomov 	if (IS_ERR(vol_args)) {
2336c9e9f97bSIlya Dryomov 		ret = PTR_ERR(vol_args);
2337c9e9f97bSIlya Dryomov 		goto out;
2338c9e9f97bSIlya Dryomov 	}
2339f46b5a66SChristoph Hellwig 
23405516e595SMark Fasheh 	vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
2341f46b5a66SChristoph Hellwig 	ret = btrfs_init_new_device(root, vol_args->name);
2342f46b5a66SChristoph Hellwig 
2343f46b5a66SChristoph Hellwig 	kfree(vol_args);
2344c9e9f97bSIlya Dryomov out:
2345c9e9f97bSIlya Dryomov 	mutex_unlock(&root->fs_info->volume_mutex);
23465ac00addSStefan Behrens 	atomic_set(&root->fs_info->mutually_exclusive_operation_running, 0);
2347f46b5a66SChristoph Hellwig 	return ret;
2348f46b5a66SChristoph Hellwig }
2349f46b5a66SChristoph Hellwig 
2350da24927bSMiao Xie static long btrfs_ioctl_rm_dev(struct file *file, void __user *arg)
2351f46b5a66SChristoph Hellwig {
2352496ad9aaSAl Viro 	struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
2353f46b5a66SChristoph Hellwig 	struct btrfs_ioctl_vol_args *vol_args;
2354f46b5a66SChristoph Hellwig 	int ret;
2355f46b5a66SChristoph Hellwig 
2356e441d54dSChris Mason 	if (!capable(CAP_SYS_ADMIN))
2357e441d54dSChris Mason 		return -EPERM;
2358e441d54dSChris Mason 
2359da24927bSMiao Xie 	ret = mnt_want_write_file(file);
2360da24927bSMiao Xie 	if (ret)
2361da24927bSMiao Xie 		return ret;
2362c146afadSYan Zheng 
2363dae7b665SLi Zefan 	vol_args = memdup_user(arg, sizeof(*vol_args));
2364c9e9f97bSIlya Dryomov 	if (IS_ERR(vol_args)) {
2365c9e9f97bSIlya Dryomov 		ret = PTR_ERR(vol_args);
2366c9e9f97bSIlya Dryomov 		goto out;
2367c9e9f97bSIlya Dryomov 	}
2368f46b5a66SChristoph Hellwig 
23695516e595SMark Fasheh 	vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
2370f46b5a66SChristoph Hellwig 
2371183860f6SAnand Jain 	if (atomic_xchg(&root->fs_info->mutually_exclusive_operation_running,
2372183860f6SAnand Jain 			1)) {
2373183860f6SAnand Jain 		ret = BTRFS_ERROR_DEV_EXCL_RUN_IN_PROGRESS;
2374183860f6SAnand Jain 		goto out;
2375183860f6SAnand Jain 	}
2376183860f6SAnand Jain 
2377183860f6SAnand Jain 	mutex_lock(&root->fs_info->volume_mutex);
2378183860f6SAnand Jain 	ret = btrfs_rm_device(root, vol_args->name);
2379c9e9f97bSIlya Dryomov 	mutex_unlock(&root->fs_info->volume_mutex);
23805ac00addSStefan Behrens 	atomic_set(&root->fs_info->mutually_exclusive_operation_running, 0);
2381183860f6SAnand Jain 
2382183860f6SAnand Jain out:
2383183860f6SAnand Jain 	kfree(vol_args);
23844ac20c70SIlya Dryomov 	mnt_drop_write_file(file);
2385f46b5a66SChristoph Hellwig 	return ret;
2386f46b5a66SChristoph Hellwig }
2387f46b5a66SChristoph Hellwig 
2388475f6387SJan Schmidt static long btrfs_ioctl_fs_info(struct btrfs_root *root, void __user *arg)
2389475f6387SJan Schmidt {
2390027ed2f0SLi Zefan 	struct btrfs_ioctl_fs_info_args *fi_args;
2391475f6387SJan Schmidt 	struct btrfs_device *device;
2392475f6387SJan Schmidt 	struct btrfs_device *next;
2393475f6387SJan Schmidt 	struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices;
2394027ed2f0SLi Zefan 	int ret = 0;
2395475f6387SJan Schmidt 
2396475f6387SJan Schmidt 	if (!capable(CAP_SYS_ADMIN))
2397475f6387SJan Schmidt 		return -EPERM;
2398475f6387SJan Schmidt 
2399027ed2f0SLi Zefan 	fi_args = kzalloc(sizeof(*fi_args), GFP_KERNEL);
2400027ed2f0SLi Zefan 	if (!fi_args)
2401027ed2f0SLi Zefan 		return -ENOMEM;
2402027ed2f0SLi Zefan 
2403027ed2f0SLi Zefan 	fi_args->num_devices = fs_devices->num_devices;
2404027ed2f0SLi Zefan 	memcpy(&fi_args->fsid, root->fs_info->fsid, sizeof(fi_args->fsid));
2405475f6387SJan Schmidt 
2406475f6387SJan Schmidt 	mutex_lock(&fs_devices->device_list_mutex);
2407475f6387SJan Schmidt 	list_for_each_entry_safe(device, next, &fs_devices->devices, dev_list) {
2408027ed2f0SLi Zefan 		if (device->devid > fi_args->max_id)
2409027ed2f0SLi Zefan 			fi_args->max_id = device->devid;
2410475f6387SJan Schmidt 	}
2411475f6387SJan Schmidt 	mutex_unlock(&fs_devices->device_list_mutex);
2412475f6387SJan Schmidt 
2413027ed2f0SLi Zefan 	if (copy_to_user(arg, fi_args, sizeof(*fi_args)))
2414027ed2f0SLi Zefan 		ret = -EFAULT;
2415475f6387SJan Schmidt 
2416027ed2f0SLi Zefan 	kfree(fi_args);
2417027ed2f0SLi Zefan 	return ret;
2418475f6387SJan Schmidt }
2419475f6387SJan Schmidt 
2420475f6387SJan Schmidt static long btrfs_ioctl_dev_info(struct btrfs_root *root, void __user *arg)
2421475f6387SJan Schmidt {
2422475f6387SJan Schmidt 	struct btrfs_ioctl_dev_info_args *di_args;
2423475f6387SJan Schmidt 	struct btrfs_device *dev;
2424475f6387SJan Schmidt 	struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices;
2425475f6387SJan Schmidt 	int ret = 0;
2426475f6387SJan Schmidt 	char *s_uuid = NULL;
2427475f6387SJan Schmidt 	char empty_uuid[BTRFS_UUID_SIZE] = {0};
2428475f6387SJan Schmidt 
2429475f6387SJan Schmidt 	if (!capable(CAP_SYS_ADMIN))
2430475f6387SJan Schmidt 		return -EPERM;
2431475f6387SJan Schmidt 
2432475f6387SJan Schmidt 	di_args = memdup_user(arg, sizeof(*di_args));
2433475f6387SJan Schmidt 	if (IS_ERR(di_args))
2434475f6387SJan Schmidt 		return PTR_ERR(di_args);
2435475f6387SJan Schmidt 
2436475f6387SJan Schmidt 	if (memcmp(empty_uuid, di_args->uuid, BTRFS_UUID_SIZE) != 0)
2437475f6387SJan Schmidt 		s_uuid = di_args->uuid;
2438475f6387SJan Schmidt 
2439475f6387SJan Schmidt 	mutex_lock(&fs_devices->device_list_mutex);
2440aa1b8cd4SStefan Behrens 	dev = btrfs_find_device(root->fs_info, di_args->devid, s_uuid, NULL);
2441475f6387SJan Schmidt 
2442475f6387SJan Schmidt 	if (!dev) {
2443475f6387SJan Schmidt 		ret = -ENODEV;
2444475f6387SJan Schmidt 		goto out;
2445475f6387SJan Schmidt 	}
2446475f6387SJan Schmidt 
2447475f6387SJan Schmidt 	di_args->devid = dev->devid;
2448475f6387SJan Schmidt 	di_args->bytes_used = dev->bytes_used;
2449475f6387SJan Schmidt 	di_args->total_bytes = dev->total_bytes;
2450475f6387SJan Schmidt 	memcpy(di_args->uuid, dev->uuid, sizeof(di_args->uuid));
2451a27202fbSJim Meyering 	if (dev->name) {
2452606686eeSJosef Bacik 		struct rcu_string *name;
2453606686eeSJosef Bacik 
2454606686eeSJosef Bacik 		rcu_read_lock();
2455606686eeSJosef Bacik 		name = rcu_dereference(dev->name);
2456606686eeSJosef Bacik 		strncpy(di_args->path, name->str, sizeof(di_args->path));
2457606686eeSJosef Bacik 		rcu_read_unlock();
2458a27202fbSJim Meyering 		di_args->path[sizeof(di_args->path) - 1] = 0;
2459a27202fbSJim Meyering 	} else {
246099ba55adSStefan Behrens 		di_args->path[0] = '\0';
2461a27202fbSJim Meyering 	}
2462475f6387SJan Schmidt 
2463475f6387SJan Schmidt out:
246455793c0dSDavid Sterba 	mutex_unlock(&fs_devices->device_list_mutex);
2465475f6387SJan Schmidt 	if (ret == 0 && copy_to_user(arg, di_args, sizeof(*di_args)))
2466475f6387SJan Schmidt 		ret = -EFAULT;
2467475f6387SJan Schmidt 
2468475f6387SJan Schmidt 	kfree(di_args);
2469475f6387SJan Schmidt 	return ret;
2470475f6387SJan Schmidt }
2471475f6387SJan Schmidt 
247276dda93cSYan, Zheng static noinline long btrfs_ioctl_clone(struct file *file, unsigned long srcfd,
2473b2950863SChristoph Hellwig 				       u64 off, u64 olen, u64 destoff)
2474f46b5a66SChristoph Hellwig {
2475496ad9aaSAl Viro 	struct inode *inode = file_inode(file);
2476f46b5a66SChristoph Hellwig 	struct btrfs_root *root = BTRFS_I(inode)->root;
24772903ff01SAl Viro 	struct fd src_file;
2478f46b5a66SChristoph Hellwig 	struct inode *src;
2479f46b5a66SChristoph Hellwig 	struct btrfs_trans_handle *trans;
2480f46b5a66SChristoph Hellwig 	struct btrfs_path *path;
2481f46b5a66SChristoph Hellwig 	struct extent_buffer *leaf;
2482ae01a0abSYan Zheng 	char *buf;
2483ae01a0abSYan Zheng 	struct btrfs_key key;
2484f46b5a66SChristoph Hellwig 	u32 nritems;
2485f46b5a66SChristoph Hellwig 	int slot;
2486ae01a0abSYan Zheng 	int ret;
2487c5c9cd4dSSage Weil 	u64 len = olen;
2488c5c9cd4dSSage Weil 	u64 bs = root->fs_info->sb->s_blocksize;
2489d20f7043SChris Mason 
2490c5c9cd4dSSage Weil 	/*
2491c5c9cd4dSSage Weil 	 * TODO:
2492c5c9cd4dSSage Weil 	 * - split compressed inline extents.  annoying: we need to
2493c5c9cd4dSSage Weil 	 *   decompress into destination's address_space (the file offset
2494c5c9cd4dSSage Weil 	 *   may change, so source mapping won't do), then recompress (or
2495c5c9cd4dSSage Weil 	 *   otherwise reinsert) a subrange.
2496c5c9cd4dSSage Weil 	 * - allow ranges within the same file to be cloned (provided
2497c5c9cd4dSSage Weil 	 *   they don't overlap)?
2498c5c9cd4dSSage Weil 	 */
2499c5c9cd4dSSage Weil 
2500e441d54dSChris Mason 	/* the destination must be opened for writing */
25012ebc3464SDan Rosenberg 	if (!(file->f_mode & FMODE_WRITE) || (file->f_flags & O_APPEND))
2502e441d54dSChris Mason 		return -EINVAL;
2503e441d54dSChris Mason 
2504b83cc969SLi Zefan 	if (btrfs_root_readonly(root))
2505b83cc969SLi Zefan 		return -EROFS;
2506b83cc969SLi Zefan 
2507a561be71SAl Viro 	ret = mnt_want_write_file(file);
2508c146afadSYan Zheng 	if (ret)
2509c146afadSYan Zheng 		return ret;
2510c146afadSYan Zheng 
25112903ff01SAl Viro 	src_file = fdget(srcfd);
25122903ff01SAl Viro 	if (!src_file.file) {
2513ab67b7c1SYan Zheng 		ret = -EBADF;
2514ab67b7c1SYan Zheng 		goto out_drop_write;
2515ab67b7c1SYan Zheng 	}
25165dc64164SDan Rosenberg 
2517362a20c5SDavid Sterba 	ret = -EXDEV;
25182903ff01SAl Viro 	if (src_file.file->f_path.mnt != file->f_path.mnt)
2519362a20c5SDavid Sterba 		goto out_fput;
2520362a20c5SDavid Sterba 
2521496ad9aaSAl Viro 	src = file_inode(src_file.file);
2522f46b5a66SChristoph Hellwig 
2523c5c9cd4dSSage Weil 	ret = -EINVAL;
2524c5c9cd4dSSage Weil 	if (src == inode)
2525c5c9cd4dSSage Weil 		goto out_fput;
2526c5c9cd4dSSage Weil 
25275dc64164SDan Rosenberg 	/* the src must be open for reading */
25282903ff01SAl Viro 	if (!(src_file.file->f_mode & FMODE_READ))
25295dc64164SDan Rosenberg 		goto out_fput;
25305dc64164SDan Rosenberg 
25310e7b824cSLi Zefan 	/* don't make the dst file partly checksummed */
25320e7b824cSLi Zefan 	if ((BTRFS_I(src)->flags & BTRFS_INODE_NODATASUM) !=
25330e7b824cSLi Zefan 	    (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM))
25340e7b824cSLi Zefan 		goto out_fput;
25350e7b824cSLi Zefan 
2536ae01a0abSYan Zheng 	ret = -EISDIR;
2537ae01a0abSYan Zheng 	if (S_ISDIR(src->i_mode) || S_ISDIR(inode->i_mode))
2538f46b5a66SChristoph Hellwig 		goto out_fput;
2539f46b5a66SChristoph Hellwig 
2540ae01a0abSYan Zheng 	ret = -EXDEV;
2541362a20c5SDavid Sterba 	if (src->i_sb != inode->i_sb)
2542ae01a0abSYan Zheng 		goto out_fput;
2543ae01a0abSYan Zheng 
2544ae01a0abSYan Zheng 	ret = -ENOMEM;
2545ae01a0abSYan Zheng 	buf = vmalloc(btrfs_level_size(root, 0));
2546ae01a0abSYan Zheng 	if (!buf)
2547ae01a0abSYan Zheng 		goto out_fput;
2548ae01a0abSYan Zheng 
2549ae01a0abSYan Zheng 	path = btrfs_alloc_path();
2550ae01a0abSYan Zheng 	if (!path) {
2551ae01a0abSYan Zheng 		vfree(buf);
2552ae01a0abSYan Zheng 		goto out_fput;
2553ae01a0abSYan Zheng 	}
2554ae01a0abSYan Zheng 	path->reada = 2;
2555ae01a0abSYan Zheng 
2556f46b5a66SChristoph Hellwig 	if (inode < src) {
2557fccdae43SSage Weil 		mutex_lock_nested(&inode->i_mutex, I_MUTEX_PARENT);
2558fccdae43SSage Weil 		mutex_lock_nested(&src->i_mutex, I_MUTEX_CHILD);
2559f46b5a66SChristoph Hellwig 	} else {
2560fccdae43SSage Weil 		mutex_lock_nested(&src->i_mutex, I_MUTEX_PARENT);
2561fccdae43SSage Weil 		mutex_lock_nested(&inode->i_mutex, I_MUTEX_CHILD);
2562f46b5a66SChristoph Hellwig 	}
2563f46b5a66SChristoph Hellwig 
2564c5c9cd4dSSage Weil 	/* determine range to clone */
2565c5c9cd4dSSage Weil 	ret = -EINVAL;
25662ebc3464SDan Rosenberg 	if (off + len > src->i_size || off + len < off)
2567f46b5a66SChristoph Hellwig 		goto out_unlock;
2568c5c9cd4dSSage Weil 	if (len == 0)
2569c5c9cd4dSSage Weil 		olen = len = src->i_size - off;
2570c5c9cd4dSSage Weil 	/* if we extend to eof, continue to block boundary */
2571c5c9cd4dSSage Weil 	if (off + len == src->i_size)
25722a6b8daeSLi Zefan 		len = ALIGN(src->i_size, bs) - off;
2573c5c9cd4dSSage Weil 
2574c5c9cd4dSSage Weil 	/* verify the end result is block aligned */
25752a6b8daeSLi Zefan 	if (!IS_ALIGNED(off, bs) || !IS_ALIGNED(off + len, bs) ||
25762a6b8daeSLi Zefan 	    !IS_ALIGNED(destoff, bs))
2577c5c9cd4dSSage Weil 		goto out_unlock;
2578c5c9cd4dSSage Weil 
2579d525e8abSLi Zefan 	if (destoff > inode->i_size) {
2580d525e8abSLi Zefan 		ret = btrfs_cont_expand(inode, inode->i_size, destoff);
2581d525e8abSLi Zefan 		if (ret)
2582d525e8abSLi Zefan 			goto out_unlock;
2583d525e8abSLi Zefan 	}
2584d525e8abSLi Zefan 
258571ef0786SLi Zefan 	/* truncate page cache pages from target inode range */
258671ef0786SLi Zefan 	truncate_inode_pages_range(&inode->i_data, destoff,
258771ef0786SLi Zefan 				   PAGE_CACHE_ALIGN(destoff + len) - 1);
258871ef0786SLi Zefan 
2589f46b5a66SChristoph Hellwig 	/* do any pending delalloc/csum calc on src, one way or
2590f46b5a66SChristoph Hellwig 	   another, and lock file content */
2591f46b5a66SChristoph Hellwig 	while (1) {
259231840ae1SZheng Yan 		struct btrfs_ordered_extent *ordered;
2593aa42ffd9SLiu Bo 		lock_extent(&BTRFS_I(src)->io_tree, off, off + len - 1);
2594aa42ffd9SLiu Bo 		ordered = btrfs_lookup_first_ordered_extent(src, off + len - 1);
25959a019196SSage Weil 		if (!ordered &&
2596aa42ffd9SLiu Bo 		    !test_range_bit(&BTRFS_I(src)->io_tree, off, off + len - 1,
25979a019196SSage Weil 				    EXTENT_DELALLOC, 0, NULL))
2598f46b5a66SChristoph Hellwig 			break;
2599aa42ffd9SLiu Bo 		unlock_extent(&BTRFS_I(src)->io_tree, off, off + len - 1);
2600ae01a0abSYan Zheng 		if (ordered)
2601ae01a0abSYan Zheng 			btrfs_put_ordered_extent(ordered);
26029a019196SSage Weil 		btrfs_wait_ordered_range(src, off, len);
2603f46b5a66SChristoph Hellwig 	}
2604f46b5a66SChristoph Hellwig 
2605c5c9cd4dSSage Weil 	/* clone data */
260633345d01SLi Zefan 	key.objectid = btrfs_ino(src);
2607ae01a0abSYan Zheng 	key.type = BTRFS_EXTENT_DATA_KEY;
2608ae01a0abSYan Zheng 	key.offset = 0;
2609f46b5a66SChristoph Hellwig 
2610f46b5a66SChristoph Hellwig 	while (1) {
2611f46b5a66SChristoph Hellwig 		/*
2612f46b5a66SChristoph Hellwig 		 * note the key will change type as we walk through the
2613f46b5a66SChristoph Hellwig 		 * tree.
2614f46b5a66SChristoph Hellwig 		 */
2615362a20c5SDavid Sterba 		ret = btrfs_search_slot(NULL, BTRFS_I(src)->root, &key, path,
2616362a20c5SDavid Sterba 				0, 0);
2617f46b5a66SChristoph Hellwig 		if (ret < 0)
2618f46b5a66SChristoph Hellwig 			goto out;
2619f46b5a66SChristoph Hellwig 
2620ae01a0abSYan Zheng 		nritems = btrfs_header_nritems(path->nodes[0]);
2621ae01a0abSYan Zheng 		if (path->slots[0] >= nritems) {
2622362a20c5SDavid Sterba 			ret = btrfs_next_leaf(BTRFS_I(src)->root, path);
2623f46b5a66SChristoph Hellwig 			if (ret < 0)
2624f46b5a66SChristoph Hellwig 				goto out;
2625f46b5a66SChristoph Hellwig 			if (ret > 0)
2626f46b5a66SChristoph Hellwig 				break;
2627ae01a0abSYan Zheng 			nritems = btrfs_header_nritems(path->nodes[0]);
2628f46b5a66SChristoph Hellwig 		}
2629f46b5a66SChristoph Hellwig 		leaf = path->nodes[0];
2630f46b5a66SChristoph Hellwig 		slot = path->slots[0];
2631f46b5a66SChristoph Hellwig 
2632ae01a0abSYan Zheng 		btrfs_item_key_to_cpu(leaf, &key, slot);
2633d20f7043SChris Mason 		if (btrfs_key_type(&key) > BTRFS_EXTENT_DATA_KEY ||
263433345d01SLi Zefan 		    key.objectid != btrfs_ino(src))
2635f46b5a66SChristoph Hellwig 			break;
2636f46b5a66SChristoph Hellwig 
2637c5c9cd4dSSage Weil 		if (btrfs_key_type(&key) == BTRFS_EXTENT_DATA_KEY) {
2638c5c9cd4dSSage Weil 			struct btrfs_file_extent_item *extent;
2639c5c9cd4dSSage Weil 			int type;
264031840ae1SZheng Yan 			u32 size;
264131840ae1SZheng Yan 			struct btrfs_key new_key;
2642c5c9cd4dSSage Weil 			u64 disko = 0, diskl = 0;
2643c5c9cd4dSSage Weil 			u64 datao = 0, datal = 0;
2644c5c9cd4dSSage Weil 			u8 comp;
2645b5384d48SSage Weil 			u64 endoff;
264631840ae1SZheng Yan 
264731840ae1SZheng Yan 			size = btrfs_item_size_nr(leaf, slot);
264831840ae1SZheng Yan 			read_extent_buffer(leaf, buf,
264931840ae1SZheng Yan 					   btrfs_item_ptr_offset(leaf, slot),
265031840ae1SZheng Yan 					   size);
2651c5c9cd4dSSage Weil 
2652c5c9cd4dSSage Weil 			extent = btrfs_item_ptr(leaf, slot,
2653c5c9cd4dSSage Weil 						struct btrfs_file_extent_item);
2654c5c9cd4dSSage Weil 			comp = btrfs_file_extent_compression(leaf, extent);
2655c5c9cd4dSSage Weil 			type = btrfs_file_extent_type(leaf, extent);
2656c8a894d7SChris Mason 			if (type == BTRFS_FILE_EXTENT_REG ||
2657c8a894d7SChris Mason 			    type == BTRFS_FILE_EXTENT_PREALLOC) {
2658d397712bSChris Mason 				disko = btrfs_file_extent_disk_bytenr(leaf,
2659d397712bSChris Mason 								      extent);
2660d397712bSChris Mason 				diskl = btrfs_file_extent_disk_num_bytes(leaf,
2661d397712bSChris Mason 								 extent);
2662c5c9cd4dSSage Weil 				datao = btrfs_file_extent_offset(leaf, extent);
2663d397712bSChris Mason 				datal = btrfs_file_extent_num_bytes(leaf,
2664d397712bSChris Mason 								    extent);
2665c5c9cd4dSSage Weil 			} else if (type == BTRFS_FILE_EXTENT_INLINE) {
2666c5c9cd4dSSage Weil 				/* take upper bound, may be compressed */
2667c5c9cd4dSSage Weil 				datal = btrfs_file_extent_ram_bytes(leaf,
2668c5c9cd4dSSage Weil 								    extent);
2669c5c9cd4dSSage Weil 			}
2670b3b4aa74SDavid Sterba 			btrfs_release_path(path);
267131840ae1SZheng Yan 
2672050006a7SSage Weil 			if (key.offset + datal <= off ||
2673aa42ffd9SLiu Bo 			    key.offset >= off + len - 1)
2674c5c9cd4dSSage Weil 				goto next;
2675c5c9cd4dSSage Weil 
267631840ae1SZheng Yan 			memcpy(&new_key, &key, sizeof(new_key));
267733345d01SLi Zefan 			new_key.objectid = btrfs_ino(inode);
26784d728ec7SLi Zefan 			if (off <= key.offset)
2679c5c9cd4dSSage Weil 				new_key.offset = key.offset + destoff - off;
26804d728ec7SLi Zefan 			else
26814d728ec7SLi Zefan 				new_key.offset = destoff;
2682c5c9cd4dSSage Weil 
2683b6f3409bSSage Weil 			/*
2684b6f3409bSSage Weil 			 * 1 - adjusting old extent (we may have to split it)
2685b6f3409bSSage Weil 			 * 1 - add new extent
2686b6f3409bSSage Weil 			 * 1 - inode update
2687b6f3409bSSage Weil 			 */
2688b6f3409bSSage Weil 			trans = btrfs_start_transaction(root, 3);
2689a22285a6SYan, Zheng 			if (IS_ERR(trans)) {
2690a22285a6SYan, Zheng 				ret = PTR_ERR(trans);
2691a22285a6SYan, Zheng 				goto out;
2692a22285a6SYan, Zheng 			}
2693a22285a6SYan, Zheng 
2694c8a894d7SChris Mason 			if (type == BTRFS_FILE_EXTENT_REG ||
2695c8a894d7SChris Mason 			    type == BTRFS_FILE_EXTENT_PREALLOC) {
2696d72c0842SLi Zefan 				/*
2697d72c0842SLi Zefan 				 *    a  | --- range to clone ---|  b
2698d72c0842SLi Zefan 				 * | ------------- extent ------------- |
2699d72c0842SLi Zefan 				 */
2700d72c0842SLi Zefan 
2701d72c0842SLi Zefan 				/* substract range b */
2702d72c0842SLi Zefan 				if (key.offset + datal > off + len)
2703d72c0842SLi Zefan 					datal = off + len - key.offset;
2704d72c0842SLi Zefan 
2705d72c0842SLi Zefan 				/* substract range a */
2706a22285a6SYan, Zheng 				if (off > key.offset) {
2707a22285a6SYan, Zheng 					datao += off - key.offset;
2708a22285a6SYan, Zheng 					datal -= off - key.offset;
2709a22285a6SYan, Zheng 				}
2710a22285a6SYan, Zheng 
27115dc562c5SJosef Bacik 				ret = btrfs_drop_extents(trans, root, inode,
2712a22285a6SYan, Zheng 							 new_key.offset,
2713a22285a6SYan, Zheng 							 new_key.offset + datal,
27142671485dSJosef Bacik 							 1);
271579787eaaSJeff Mahoney 				if (ret) {
271679787eaaSJeff Mahoney 					btrfs_abort_transaction(trans, root,
271779787eaaSJeff Mahoney 								ret);
271879787eaaSJeff Mahoney 					btrfs_end_transaction(trans, root);
271979787eaaSJeff Mahoney 					goto out;
272079787eaaSJeff Mahoney 				}
2721a22285a6SYan, Zheng 
272231840ae1SZheng Yan 				ret = btrfs_insert_empty_item(trans, root, path,
272331840ae1SZheng Yan 							      &new_key, size);
272479787eaaSJeff Mahoney 				if (ret) {
272579787eaaSJeff Mahoney 					btrfs_abort_transaction(trans, root,
272679787eaaSJeff Mahoney 								ret);
272779787eaaSJeff Mahoney 					btrfs_end_transaction(trans, root);
272879787eaaSJeff Mahoney 					goto out;
272979787eaaSJeff Mahoney 				}
273031840ae1SZheng Yan 
273131840ae1SZheng Yan 				leaf = path->nodes[0];
273231840ae1SZheng Yan 				slot = path->slots[0];
273331840ae1SZheng Yan 				write_extent_buffer(leaf, buf,
273431840ae1SZheng Yan 					    btrfs_item_ptr_offset(leaf, slot),
273531840ae1SZheng Yan 					    size);
2736ae01a0abSYan Zheng 
2737f46b5a66SChristoph Hellwig 				extent = btrfs_item_ptr(leaf, slot,
2738f46b5a66SChristoph Hellwig 						struct btrfs_file_extent_item);
2739c5c9cd4dSSage Weil 
2740c5c9cd4dSSage Weil 				/* disko == 0 means it's a hole */
2741c5c9cd4dSSage Weil 				if (!disko)
2742c5c9cd4dSSage Weil 					datao = 0;
2743c5c9cd4dSSage Weil 
2744c5c9cd4dSSage Weil 				btrfs_set_file_extent_offset(leaf, extent,
2745c5c9cd4dSSage Weil 							     datao);
2746c5c9cd4dSSage Weil 				btrfs_set_file_extent_num_bytes(leaf, extent,
2747c5c9cd4dSSage Weil 								datal);
2748c5c9cd4dSSage Weil 				if (disko) {
2749c5c9cd4dSSage Weil 					inode_add_bytes(inode, datal);
2750ae01a0abSYan Zheng 					ret = btrfs_inc_extent_ref(trans, root,
27515d4f98a2SYan Zheng 							disko, diskl, 0,
2752f46b5a66SChristoph Hellwig 							root->root_key.objectid,
275333345d01SLi Zefan 							btrfs_ino(inode),
275466d7e7f0SArne Jansen 							new_key.offset - datao,
275566d7e7f0SArne Jansen 							0);
275679787eaaSJeff Mahoney 					if (ret) {
275779787eaaSJeff Mahoney 						btrfs_abort_transaction(trans,
275879787eaaSJeff Mahoney 									root,
275979787eaaSJeff Mahoney 									ret);
276079787eaaSJeff Mahoney 						btrfs_end_transaction(trans,
276179787eaaSJeff Mahoney 								      root);
276279787eaaSJeff Mahoney 						goto out;
276379787eaaSJeff Mahoney 
276479787eaaSJeff Mahoney 					}
2765f46b5a66SChristoph Hellwig 				}
2766c5c9cd4dSSage Weil 			} else if (type == BTRFS_FILE_EXTENT_INLINE) {
2767c5c9cd4dSSage Weil 				u64 skip = 0;
2768c5c9cd4dSSage Weil 				u64 trim = 0;
2769c5c9cd4dSSage Weil 				if (off > key.offset) {
2770c5c9cd4dSSage Weil 					skip = off - key.offset;
2771c5c9cd4dSSage Weil 					new_key.offset += skip;
277231840ae1SZheng Yan 				}
2773d397712bSChris Mason 
2774c5c9cd4dSSage Weil 				if (key.offset + datal > off + len)
2775c5c9cd4dSSage Weil 					trim = key.offset + datal - (off + len);
2776d397712bSChris Mason 
2777c5c9cd4dSSage Weil 				if (comp && (skip || trim)) {
2778c5c9cd4dSSage Weil 					ret = -EINVAL;
2779a22285a6SYan, Zheng 					btrfs_end_transaction(trans, root);
2780c5c9cd4dSSage Weil 					goto out;
278131840ae1SZheng Yan 				}
2782c5c9cd4dSSage Weil 				size -= skip + trim;
2783c5c9cd4dSSage Weil 				datal -= skip + trim;
2784a22285a6SYan, Zheng 
27855dc562c5SJosef Bacik 				ret = btrfs_drop_extents(trans, root, inode,
2786a22285a6SYan, Zheng 							 new_key.offset,
2787a22285a6SYan, Zheng 							 new_key.offset + datal,
27882671485dSJosef Bacik 							 1);
278979787eaaSJeff Mahoney 				if (ret) {
279079787eaaSJeff Mahoney 					btrfs_abort_transaction(trans, root,
279179787eaaSJeff Mahoney 								ret);
279279787eaaSJeff Mahoney 					btrfs_end_transaction(trans, root);
279379787eaaSJeff Mahoney 					goto out;
279479787eaaSJeff Mahoney 				}
2795a22285a6SYan, Zheng 
2796c5c9cd4dSSage Weil 				ret = btrfs_insert_empty_item(trans, root, path,
2797c5c9cd4dSSage Weil 							      &new_key, size);
279879787eaaSJeff Mahoney 				if (ret) {
279979787eaaSJeff Mahoney 					btrfs_abort_transaction(trans, root,
280079787eaaSJeff Mahoney 								ret);
280179787eaaSJeff Mahoney 					btrfs_end_transaction(trans, root);
280279787eaaSJeff Mahoney 					goto out;
280379787eaaSJeff Mahoney 				}
2804c5c9cd4dSSage Weil 
2805c5c9cd4dSSage Weil 				if (skip) {
2806d397712bSChris Mason 					u32 start =
2807d397712bSChris Mason 					  btrfs_file_extent_calc_inline_size(0);
2808c5c9cd4dSSage Weil 					memmove(buf+start, buf+start+skip,
2809c5c9cd4dSSage Weil 						datal);
2810c5c9cd4dSSage Weil 				}
2811c5c9cd4dSSage Weil 
2812c5c9cd4dSSage Weil 				leaf = path->nodes[0];
2813c5c9cd4dSSage Weil 				slot = path->slots[0];
2814c5c9cd4dSSage Weil 				write_extent_buffer(leaf, buf,
2815c5c9cd4dSSage Weil 					    btrfs_item_ptr_offset(leaf, slot),
2816c5c9cd4dSSage Weil 					    size);
2817c5c9cd4dSSage Weil 				inode_add_bytes(inode, datal);
2818c5c9cd4dSSage Weil 			}
2819c5c9cd4dSSage Weil 
2820c5c9cd4dSSage Weil 			btrfs_mark_buffer_dirty(leaf);
2821b3b4aa74SDavid Sterba 			btrfs_release_path(path);
2822c5c9cd4dSSage Weil 
28230c4d2d95SJosef Bacik 			inode_inc_iversion(inode);
2824a22285a6SYan, Zheng 			inode->i_mtime = inode->i_ctime = CURRENT_TIME;
2825b5384d48SSage Weil 
2826b5384d48SSage Weil 			/*
2827b5384d48SSage Weil 			 * we round up to the block size at eof when
2828b5384d48SSage Weil 			 * determining which extents to clone above,
2829b5384d48SSage Weil 			 * but shouldn't round up the file size
2830b5384d48SSage Weil 			 */
2831b5384d48SSage Weil 			endoff = new_key.offset + datal;
28325f3888ffSLi Zefan 			if (endoff > destoff+olen)
28335f3888ffSLi Zefan 				endoff = destoff+olen;
2834b5384d48SSage Weil 			if (endoff > inode->i_size)
2835b5384d48SSage Weil 				btrfs_i_size_write(inode, endoff);
2836b5384d48SSage Weil 
2837a22285a6SYan, Zheng 			ret = btrfs_update_inode(trans, root, inode);
283879787eaaSJeff Mahoney 			if (ret) {
283979787eaaSJeff Mahoney 				btrfs_abort_transaction(trans, root, ret);
2840a22285a6SYan, Zheng 				btrfs_end_transaction(trans, root);
284179787eaaSJeff Mahoney 				goto out;
284279787eaaSJeff Mahoney 			}
284379787eaaSJeff Mahoney 			ret = btrfs_end_transaction(trans, root);
2844a22285a6SYan, Zheng 		}
2845c5c9cd4dSSage Weil next:
2846b3b4aa74SDavid Sterba 		btrfs_release_path(path);
2847ae01a0abSYan Zheng 		key.offset++;
2848ae01a0abSYan Zheng 	}
2849f46b5a66SChristoph Hellwig 	ret = 0;
2850f46b5a66SChristoph Hellwig out:
2851b3b4aa74SDavid Sterba 	btrfs_release_path(path);
2852aa42ffd9SLiu Bo 	unlock_extent(&BTRFS_I(src)->io_tree, off, off + len - 1);
2853f46b5a66SChristoph Hellwig out_unlock:
2854f46b5a66SChristoph Hellwig 	mutex_unlock(&src->i_mutex);
2855f46b5a66SChristoph Hellwig 	mutex_unlock(&inode->i_mutex);
2856ae01a0abSYan Zheng 	vfree(buf);
2857ae01a0abSYan Zheng 	btrfs_free_path(path);
2858f46b5a66SChristoph Hellwig out_fput:
28592903ff01SAl Viro 	fdput(src_file);
2860ab67b7c1SYan Zheng out_drop_write:
28612a79f17eSAl Viro 	mnt_drop_write_file(file);
2862f46b5a66SChristoph Hellwig 	return ret;
2863f46b5a66SChristoph Hellwig }
2864f46b5a66SChristoph Hellwig 
28657a865e8aSChristoph Hellwig static long btrfs_ioctl_clone_range(struct file *file, void __user *argp)
2866c5c9cd4dSSage Weil {
2867c5c9cd4dSSage Weil 	struct btrfs_ioctl_clone_range_args args;
2868c5c9cd4dSSage Weil 
28697a865e8aSChristoph Hellwig 	if (copy_from_user(&args, argp, sizeof(args)))
2870c5c9cd4dSSage Weil 		return -EFAULT;
2871c5c9cd4dSSage Weil 	return btrfs_ioctl_clone(file, args.src_fd, args.src_offset,
2872c5c9cd4dSSage Weil 				 args.src_length, args.dest_offset);
2873c5c9cd4dSSage Weil }
2874c5c9cd4dSSage Weil 
2875f46b5a66SChristoph Hellwig /*
2876f46b5a66SChristoph Hellwig  * there are many ways the trans_start and trans_end ioctls can lead
2877f46b5a66SChristoph Hellwig  * to deadlocks.  They should only be used by applications that
2878f46b5a66SChristoph Hellwig  * basically own the machine, and have a very in depth understanding
2879f46b5a66SChristoph Hellwig  * of all the possible deadlocks and enospc problems.
2880f46b5a66SChristoph Hellwig  */
2881b2950863SChristoph Hellwig static long btrfs_ioctl_trans_start(struct file *file)
2882f46b5a66SChristoph Hellwig {
2883496ad9aaSAl Viro 	struct inode *inode = file_inode(file);
2884f46b5a66SChristoph Hellwig 	struct btrfs_root *root = BTRFS_I(inode)->root;
2885f46b5a66SChristoph Hellwig 	struct btrfs_trans_handle *trans;
28861ab86aedSSage Weil 	int ret;
2887f46b5a66SChristoph Hellwig 
28881ab86aedSSage Weil 	ret = -EPERM;
2889df5b5520SChristoph Hellwig 	if (!capable(CAP_SYS_ADMIN))
2890f46b5a66SChristoph Hellwig 		goto out;
28911ab86aedSSage Weil 
28921ab86aedSSage Weil 	ret = -EINPROGRESS;
28931ab86aedSSage Weil 	if (file->private_data)
28941ab86aedSSage Weil 		goto out;
28959ca9ee09SSage Weil 
2896b83cc969SLi Zefan 	ret = -EROFS;
2897b83cc969SLi Zefan 	if (btrfs_root_readonly(root))
2898b83cc969SLi Zefan 		goto out;
2899b83cc969SLi Zefan 
2900a561be71SAl Viro 	ret = mnt_want_write_file(file);
2901c146afadSYan Zheng 	if (ret)
2902c146afadSYan Zheng 		goto out;
2903c146afadSYan Zheng 
2904a4abeea4SJosef Bacik 	atomic_inc(&root->fs_info->open_ioctl_trans);
29059ca9ee09SSage Weil 
2906f46b5a66SChristoph Hellwig 	ret = -ENOMEM;
29077a7eaa40SJosef Bacik 	trans = btrfs_start_ioctl_transaction(root);
2908abd30bb0STsutomu Itoh 	if (IS_ERR(trans))
29091ab86aedSSage Weil 		goto out_drop;
29101ab86aedSSage Weil 
29111ab86aedSSage Weil 	file->private_data = trans;
29121ab86aedSSage Weil 	return 0;
29131ab86aedSSage Weil 
29141ab86aedSSage Weil out_drop:
2915a4abeea4SJosef Bacik 	atomic_dec(&root->fs_info->open_ioctl_trans);
29162a79f17eSAl Viro 	mnt_drop_write_file(file);
2917f46b5a66SChristoph Hellwig out:
2918f46b5a66SChristoph Hellwig 	return ret;
2919f46b5a66SChristoph Hellwig }
2920f46b5a66SChristoph Hellwig 
29216ef5ed0dSJosef Bacik static long btrfs_ioctl_default_subvol(struct file *file, void __user *argp)
29226ef5ed0dSJosef Bacik {
2923496ad9aaSAl Viro 	struct inode *inode = file_inode(file);
29246ef5ed0dSJosef Bacik 	struct btrfs_root *root = BTRFS_I(inode)->root;
29256ef5ed0dSJosef Bacik 	struct btrfs_root *new_root;
29266ef5ed0dSJosef Bacik 	struct btrfs_dir_item *di;
29276ef5ed0dSJosef Bacik 	struct btrfs_trans_handle *trans;
29286ef5ed0dSJosef Bacik 	struct btrfs_path *path;
29296ef5ed0dSJosef Bacik 	struct btrfs_key location;
29306ef5ed0dSJosef Bacik 	struct btrfs_disk_key disk_key;
29316ef5ed0dSJosef Bacik 	u64 objectid = 0;
29326ef5ed0dSJosef Bacik 	u64 dir_id;
29333c04ce01SMiao Xie 	int ret;
29346ef5ed0dSJosef Bacik 
29356ef5ed0dSJosef Bacik 	if (!capable(CAP_SYS_ADMIN))
29366ef5ed0dSJosef Bacik 		return -EPERM;
29376ef5ed0dSJosef Bacik 
29383c04ce01SMiao Xie 	ret = mnt_want_write_file(file);
29393c04ce01SMiao Xie 	if (ret)
29403c04ce01SMiao Xie 		return ret;
29413c04ce01SMiao Xie 
29423c04ce01SMiao Xie 	if (copy_from_user(&objectid, argp, sizeof(objectid))) {
29433c04ce01SMiao Xie 		ret = -EFAULT;
29443c04ce01SMiao Xie 		goto out;
29453c04ce01SMiao Xie 	}
29466ef5ed0dSJosef Bacik 
29476ef5ed0dSJosef Bacik 	if (!objectid)
29486ef5ed0dSJosef Bacik 		objectid = root->root_key.objectid;
29496ef5ed0dSJosef Bacik 
29506ef5ed0dSJosef Bacik 	location.objectid = objectid;
29516ef5ed0dSJosef Bacik 	location.type = BTRFS_ROOT_ITEM_KEY;
29526ef5ed0dSJosef Bacik 	location.offset = (u64)-1;
29536ef5ed0dSJosef Bacik 
29546ef5ed0dSJosef Bacik 	new_root = btrfs_read_fs_root_no_name(root->fs_info, &location);
29553c04ce01SMiao Xie 	if (IS_ERR(new_root)) {
29563c04ce01SMiao Xie 		ret = PTR_ERR(new_root);
29573c04ce01SMiao Xie 		goto out;
29583c04ce01SMiao Xie 	}
29596ef5ed0dSJosef Bacik 
29606ef5ed0dSJosef Bacik 	path = btrfs_alloc_path();
29613c04ce01SMiao Xie 	if (!path) {
29623c04ce01SMiao Xie 		ret = -ENOMEM;
29633c04ce01SMiao Xie 		goto out;
29643c04ce01SMiao Xie 	}
29656ef5ed0dSJosef Bacik 	path->leave_spinning = 1;
29666ef5ed0dSJosef Bacik 
29676ef5ed0dSJosef Bacik 	trans = btrfs_start_transaction(root, 1);
296898d5dc13STsutomu Itoh 	if (IS_ERR(trans)) {
29696ef5ed0dSJosef Bacik 		btrfs_free_path(path);
29703c04ce01SMiao Xie 		ret = PTR_ERR(trans);
29713c04ce01SMiao Xie 		goto out;
29726ef5ed0dSJosef Bacik 	}
29736ef5ed0dSJosef Bacik 
29746c41761fSDavid Sterba 	dir_id = btrfs_super_root_dir(root->fs_info->super_copy);
29756ef5ed0dSJosef Bacik 	di = btrfs_lookup_dir_item(trans, root->fs_info->tree_root, path,
29766ef5ed0dSJosef Bacik 				   dir_id, "default", 7, 1);
2977cf1e99a4SDan Carpenter 	if (IS_ERR_OR_NULL(di)) {
29786ef5ed0dSJosef Bacik 		btrfs_free_path(path);
29796ef5ed0dSJosef Bacik 		btrfs_end_transaction(trans, root);
29806ef5ed0dSJosef Bacik 		printk(KERN_ERR "Umm, you don't have the default dir item, "
29816ef5ed0dSJosef Bacik 		       "this isn't going to work\n");
29823c04ce01SMiao Xie 		ret = -ENOENT;
29833c04ce01SMiao Xie 		goto out;
29846ef5ed0dSJosef Bacik 	}
29856ef5ed0dSJosef Bacik 
29866ef5ed0dSJosef Bacik 	btrfs_cpu_key_to_disk(&disk_key, &new_root->root_key);
29876ef5ed0dSJosef Bacik 	btrfs_set_dir_item_key(path->nodes[0], di, &disk_key);
29886ef5ed0dSJosef Bacik 	btrfs_mark_buffer_dirty(path->nodes[0]);
29896ef5ed0dSJosef Bacik 	btrfs_free_path(path);
29906ef5ed0dSJosef Bacik 
29912b0ce2c2SMitch Harder 	btrfs_set_fs_incompat(root->fs_info, DEFAULT_SUBVOL);
29926ef5ed0dSJosef Bacik 	btrfs_end_transaction(trans, root);
29933c04ce01SMiao Xie out:
29943c04ce01SMiao Xie 	mnt_drop_write_file(file);
29953c04ce01SMiao Xie 	return ret;
29966ef5ed0dSJosef Bacik }
29976ef5ed0dSJosef Bacik 
29985af3e8ccSStefan Behrens void btrfs_get_block_group_info(struct list_head *groups_list,
2999bf5fc093SJosef Bacik 				struct btrfs_ioctl_space_info *space)
3000bf5fc093SJosef Bacik {
3001bf5fc093SJosef Bacik 	struct btrfs_block_group_cache *block_group;
3002bf5fc093SJosef Bacik 
3003bf5fc093SJosef Bacik 	space->total_bytes = 0;
3004bf5fc093SJosef Bacik 	space->used_bytes = 0;
3005bf5fc093SJosef Bacik 	space->flags = 0;
3006bf5fc093SJosef Bacik 	list_for_each_entry(block_group, groups_list, list) {
3007bf5fc093SJosef Bacik 		space->flags = block_group->flags;
3008bf5fc093SJosef Bacik 		space->total_bytes += block_group->key.offset;
3009bf5fc093SJosef Bacik 		space->used_bytes +=
3010bf5fc093SJosef Bacik 			btrfs_block_group_used(&block_group->item);
3011bf5fc093SJosef Bacik 	}
3012bf5fc093SJosef Bacik }
3013bf5fc093SJosef Bacik 
301448a3b636SEric Sandeen static long btrfs_ioctl_space_info(struct btrfs_root *root, void __user *arg)
30151406e432SJosef Bacik {
30161406e432SJosef Bacik 	struct btrfs_ioctl_space_args space_args;
30171406e432SJosef Bacik 	struct btrfs_ioctl_space_info space;
30181406e432SJosef Bacik 	struct btrfs_ioctl_space_info *dest;
30197fde62bfSChris Mason 	struct btrfs_ioctl_space_info *dest_orig;
302013f2696fSDaniel J Blueman 	struct btrfs_ioctl_space_info __user *user_dest;
30211406e432SJosef Bacik 	struct btrfs_space_info *info;
3022bf5fc093SJosef Bacik 	u64 types[] = {BTRFS_BLOCK_GROUP_DATA,
3023bf5fc093SJosef Bacik 		       BTRFS_BLOCK_GROUP_SYSTEM,
3024bf5fc093SJosef Bacik 		       BTRFS_BLOCK_GROUP_METADATA,
3025bf5fc093SJosef Bacik 		       BTRFS_BLOCK_GROUP_DATA | BTRFS_BLOCK_GROUP_METADATA};
3026bf5fc093SJosef Bacik 	int num_types = 4;
30277fde62bfSChris Mason 	int alloc_size;
30281406e432SJosef Bacik 	int ret = 0;
302951788b1bSDan Rosenberg 	u64 slot_count = 0;
3030bf5fc093SJosef Bacik 	int i, c;
30311406e432SJosef Bacik 
30321406e432SJosef Bacik 	if (copy_from_user(&space_args,
30331406e432SJosef Bacik 			   (struct btrfs_ioctl_space_args __user *)arg,
30341406e432SJosef Bacik 			   sizeof(space_args)))
30351406e432SJosef Bacik 		return -EFAULT;
30361406e432SJosef Bacik 
3037bf5fc093SJosef Bacik 	for (i = 0; i < num_types; i++) {
3038bf5fc093SJosef Bacik 		struct btrfs_space_info *tmp;
3039bf5fc093SJosef Bacik 
3040bf5fc093SJosef Bacik 		info = NULL;
30417fde62bfSChris Mason 		rcu_read_lock();
3042bf5fc093SJosef Bacik 		list_for_each_entry_rcu(tmp, &root->fs_info->space_info,
3043bf5fc093SJosef Bacik 					list) {
3044bf5fc093SJosef Bacik 			if (tmp->flags == types[i]) {
3045bf5fc093SJosef Bacik 				info = tmp;
3046bf5fc093SJosef Bacik 				break;
3047bf5fc093SJosef Bacik 			}
3048bf5fc093SJosef Bacik 		}
30497fde62bfSChris Mason 		rcu_read_unlock();
30501406e432SJosef Bacik 
3051bf5fc093SJosef Bacik 		if (!info)
3052bf5fc093SJosef Bacik 			continue;
3053bf5fc093SJosef Bacik 
3054bf5fc093SJosef Bacik 		down_read(&info->groups_sem);
3055bf5fc093SJosef Bacik 		for (c = 0; c < BTRFS_NR_RAID_TYPES; c++) {
3056bf5fc093SJosef Bacik 			if (!list_empty(&info->block_groups[c]))
3057bf5fc093SJosef Bacik 				slot_count++;
3058bf5fc093SJosef Bacik 		}
3059bf5fc093SJosef Bacik 		up_read(&info->groups_sem);
3060bf5fc093SJosef Bacik 	}
3061bf5fc093SJosef Bacik 
30627fde62bfSChris Mason 	/* space_slots == 0 means they are asking for a count */
30637fde62bfSChris Mason 	if (space_args.space_slots == 0) {
30647fde62bfSChris Mason 		space_args.total_spaces = slot_count;
30657fde62bfSChris Mason 		goto out;
30667fde62bfSChris Mason 	}
3067bf5fc093SJosef Bacik 
306851788b1bSDan Rosenberg 	slot_count = min_t(u64, space_args.space_slots, slot_count);
3069bf5fc093SJosef Bacik 
30707fde62bfSChris Mason 	alloc_size = sizeof(*dest) * slot_count;
3071bf5fc093SJosef Bacik 
30727fde62bfSChris Mason 	/* we generally have at most 6 or so space infos, one for each raid
30737fde62bfSChris Mason 	 * level.  So, a whole page should be more than enough for everyone
30747fde62bfSChris Mason 	 */
30757fde62bfSChris Mason 	if (alloc_size > PAGE_CACHE_SIZE)
30767fde62bfSChris Mason 		return -ENOMEM;
30777fde62bfSChris Mason 
30787fde62bfSChris Mason 	space_args.total_spaces = 0;
30797fde62bfSChris Mason 	dest = kmalloc(alloc_size, GFP_NOFS);
30807fde62bfSChris Mason 	if (!dest)
30817fde62bfSChris Mason 		return -ENOMEM;
30827fde62bfSChris Mason 	dest_orig = dest;
30837fde62bfSChris Mason 
30847fde62bfSChris Mason 	/* now we have a buffer to copy into */
3085bf5fc093SJosef Bacik 	for (i = 0; i < num_types; i++) {
3086bf5fc093SJosef Bacik 		struct btrfs_space_info *tmp;
3087bf5fc093SJosef Bacik 
308851788b1bSDan Rosenberg 		if (!slot_count)
308951788b1bSDan Rosenberg 			break;
309051788b1bSDan Rosenberg 
3091bf5fc093SJosef Bacik 		info = NULL;
30921406e432SJosef Bacik 		rcu_read_lock();
3093bf5fc093SJosef Bacik 		list_for_each_entry_rcu(tmp, &root->fs_info->space_info,
3094bf5fc093SJosef Bacik 					list) {
3095bf5fc093SJosef Bacik 			if (tmp->flags == types[i]) {
3096bf5fc093SJosef Bacik 				info = tmp;
30977fde62bfSChris Mason 				break;
3098bf5fc093SJosef Bacik 			}
3099bf5fc093SJosef Bacik 		}
3100bf5fc093SJosef Bacik 		rcu_read_unlock();
31017fde62bfSChris Mason 
3102bf5fc093SJosef Bacik 		if (!info)
3103bf5fc093SJosef Bacik 			continue;
3104bf5fc093SJosef Bacik 		down_read(&info->groups_sem);
3105bf5fc093SJosef Bacik 		for (c = 0; c < BTRFS_NR_RAID_TYPES; c++) {
3106bf5fc093SJosef Bacik 			if (!list_empty(&info->block_groups[c])) {
31075af3e8ccSStefan Behrens 				btrfs_get_block_group_info(
31085af3e8ccSStefan Behrens 					&info->block_groups[c], &space);
31097fde62bfSChris Mason 				memcpy(dest, &space, sizeof(space));
31101406e432SJosef Bacik 				dest++;
31111406e432SJosef Bacik 				space_args.total_spaces++;
311251788b1bSDan Rosenberg 				slot_count--;
31131406e432SJosef Bacik 			}
311451788b1bSDan Rosenberg 			if (!slot_count)
311551788b1bSDan Rosenberg 				break;
3116bf5fc093SJosef Bacik 		}
3117bf5fc093SJosef Bacik 		up_read(&info->groups_sem);
3118bf5fc093SJosef Bacik 	}
31191406e432SJosef Bacik 
31202eec6c81SDaniel J Blueman 	user_dest = (struct btrfs_ioctl_space_info __user *)
31217fde62bfSChris Mason 		(arg + sizeof(struct btrfs_ioctl_space_args));
31227fde62bfSChris Mason 
31237fde62bfSChris Mason 	if (copy_to_user(user_dest, dest_orig, alloc_size))
31247fde62bfSChris Mason 		ret = -EFAULT;
31257fde62bfSChris Mason 
31267fde62bfSChris Mason 	kfree(dest_orig);
31277fde62bfSChris Mason out:
31287fde62bfSChris Mason 	if (ret == 0 && copy_to_user(arg, &space_args, sizeof(space_args)))
31291406e432SJosef Bacik 		ret = -EFAULT;
31301406e432SJosef Bacik 
31311406e432SJosef Bacik 	return ret;
31321406e432SJosef Bacik }
31331406e432SJosef Bacik 
3134f46b5a66SChristoph Hellwig /*
3135f46b5a66SChristoph Hellwig  * there are many ways the trans_start and trans_end ioctls can lead
3136f46b5a66SChristoph Hellwig  * to deadlocks.  They should only be used by applications that
3137f46b5a66SChristoph Hellwig  * basically own the machine, and have a very in depth understanding
3138f46b5a66SChristoph Hellwig  * of all the possible deadlocks and enospc problems.
3139f46b5a66SChristoph Hellwig  */
3140f46b5a66SChristoph Hellwig long btrfs_ioctl_trans_end(struct file *file)
3141f46b5a66SChristoph Hellwig {
3142496ad9aaSAl Viro 	struct inode *inode = file_inode(file);
3143f46b5a66SChristoph Hellwig 	struct btrfs_root *root = BTRFS_I(inode)->root;
3144f46b5a66SChristoph Hellwig 	struct btrfs_trans_handle *trans;
3145f46b5a66SChristoph Hellwig 
3146f46b5a66SChristoph Hellwig 	trans = file->private_data;
31471ab86aedSSage Weil 	if (!trans)
31481ab86aedSSage Weil 		return -EINVAL;
3149b214107eSChristoph Hellwig 	file->private_data = NULL;
31509ca9ee09SSage Weil 
31511ab86aedSSage Weil 	btrfs_end_transaction(trans, root);
31521ab86aedSSage Weil 
3153a4abeea4SJosef Bacik 	atomic_dec(&root->fs_info->open_ioctl_trans);
31549ca9ee09SSage Weil 
31552a79f17eSAl Viro 	mnt_drop_write_file(file);
31561ab86aedSSage Weil 	return 0;
3157f46b5a66SChristoph Hellwig }
3158f46b5a66SChristoph Hellwig 
31599a8c28beSMiao Xie static noinline long btrfs_ioctl_start_sync(struct btrfs_root *root,
31609a8c28beSMiao Xie 					    void __user *argp)
316146204592SSage Weil {
316246204592SSage Weil 	struct btrfs_trans_handle *trans;
316346204592SSage Weil 	u64 transid;
3164db5b493aSTsutomu Itoh 	int ret;
316546204592SSage Weil 
3166d4edf39bSMiao Xie 	trans = btrfs_attach_transaction_barrier(root);
3167ff7c1d33SMiao Xie 	if (IS_ERR(trans)) {
3168ff7c1d33SMiao Xie 		if (PTR_ERR(trans) != -ENOENT)
316998d5dc13STsutomu Itoh 			return PTR_ERR(trans);
3170ff7c1d33SMiao Xie 
3171ff7c1d33SMiao Xie 		/* No running transaction, don't bother */
3172ff7c1d33SMiao Xie 		transid = root->fs_info->last_trans_committed;
3173ff7c1d33SMiao Xie 		goto out;
3174ff7c1d33SMiao Xie 	}
317546204592SSage Weil 	transid = trans->transid;
3176db5b493aSTsutomu Itoh 	ret = btrfs_commit_transaction_async(trans, root, 0);
31778b2b2d3cSTsutomu Itoh 	if (ret) {
31788b2b2d3cSTsutomu Itoh 		btrfs_end_transaction(trans, root);
3179db5b493aSTsutomu Itoh 		return ret;
31808b2b2d3cSTsutomu Itoh 	}
3181ff7c1d33SMiao Xie out:
318246204592SSage Weil 	if (argp)
318346204592SSage Weil 		if (copy_to_user(argp, &transid, sizeof(transid)))
318446204592SSage Weil 			return -EFAULT;
318546204592SSage Weil 	return 0;
318646204592SSage Weil }
318746204592SSage Weil 
31889a8c28beSMiao Xie static noinline long btrfs_ioctl_wait_sync(struct btrfs_root *root,
31899a8c28beSMiao Xie 					   void __user *argp)
319046204592SSage Weil {
319146204592SSage Weil 	u64 transid;
319246204592SSage Weil 
319346204592SSage Weil 	if (argp) {
319446204592SSage Weil 		if (copy_from_user(&transid, argp, sizeof(transid)))
319546204592SSage Weil 			return -EFAULT;
319646204592SSage Weil 	} else {
319746204592SSage Weil 		transid = 0;  /* current trans */
319846204592SSage Weil 	}
319946204592SSage Weil 	return btrfs_wait_for_commit(root, transid);
320046204592SSage Weil }
320146204592SSage Weil 
3202b8e95489SMiao Xie static long btrfs_ioctl_scrub(struct file *file, void __user *arg)
3203475f6387SJan Schmidt {
3204496ad9aaSAl Viro 	struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
3205475f6387SJan Schmidt 	struct btrfs_ioctl_scrub_args *sa;
3206b8e95489SMiao Xie 	int ret;
3207475f6387SJan Schmidt 
3208475f6387SJan Schmidt 	if (!capable(CAP_SYS_ADMIN))
3209475f6387SJan Schmidt 		return -EPERM;
3210475f6387SJan Schmidt 
3211475f6387SJan Schmidt 	sa = memdup_user(arg, sizeof(*sa));
3212475f6387SJan Schmidt 	if (IS_ERR(sa))
3213475f6387SJan Schmidt 		return PTR_ERR(sa);
3214475f6387SJan Schmidt 
3215b8e95489SMiao Xie 	if (!(sa->flags & BTRFS_SCRUB_READONLY)) {
3216b8e95489SMiao Xie 		ret = mnt_want_write_file(file);
3217b8e95489SMiao Xie 		if (ret)
3218b8e95489SMiao Xie 			goto out;
3219b8e95489SMiao Xie 	}
3220b8e95489SMiao Xie 
3221aa1b8cd4SStefan Behrens 	ret = btrfs_scrub_dev(root->fs_info, sa->devid, sa->start, sa->end,
322263a212abSStefan Behrens 			      &sa->progress, sa->flags & BTRFS_SCRUB_READONLY,
322363a212abSStefan Behrens 			      0);
3224475f6387SJan Schmidt 
3225475f6387SJan Schmidt 	if (copy_to_user(arg, sa, sizeof(*sa)))
3226475f6387SJan Schmidt 		ret = -EFAULT;
3227475f6387SJan Schmidt 
3228b8e95489SMiao Xie 	if (!(sa->flags & BTRFS_SCRUB_READONLY))
3229b8e95489SMiao Xie 		mnt_drop_write_file(file);
3230b8e95489SMiao Xie out:
3231475f6387SJan Schmidt 	kfree(sa);
3232475f6387SJan Schmidt 	return ret;
3233475f6387SJan Schmidt }
3234475f6387SJan Schmidt 
3235475f6387SJan Schmidt static long btrfs_ioctl_scrub_cancel(struct btrfs_root *root, void __user *arg)
3236475f6387SJan Schmidt {
3237475f6387SJan Schmidt 	if (!capable(CAP_SYS_ADMIN))
3238475f6387SJan Schmidt 		return -EPERM;
3239475f6387SJan Schmidt 
3240aa1b8cd4SStefan Behrens 	return btrfs_scrub_cancel(root->fs_info);
3241475f6387SJan Schmidt }
3242475f6387SJan Schmidt 
3243475f6387SJan Schmidt static long btrfs_ioctl_scrub_progress(struct btrfs_root *root,
3244475f6387SJan Schmidt 				       void __user *arg)
3245475f6387SJan Schmidt {
3246475f6387SJan Schmidt 	struct btrfs_ioctl_scrub_args *sa;
3247475f6387SJan Schmidt 	int ret;
3248475f6387SJan Schmidt 
3249475f6387SJan Schmidt 	if (!capable(CAP_SYS_ADMIN))
3250475f6387SJan Schmidt 		return -EPERM;
3251475f6387SJan Schmidt 
3252475f6387SJan Schmidt 	sa = memdup_user(arg, sizeof(*sa));
3253475f6387SJan Schmidt 	if (IS_ERR(sa))
3254475f6387SJan Schmidt 		return PTR_ERR(sa);
3255475f6387SJan Schmidt 
3256475f6387SJan Schmidt 	ret = btrfs_scrub_progress(root, sa->devid, &sa->progress);
3257475f6387SJan Schmidt 
3258475f6387SJan Schmidt 	if (copy_to_user(arg, sa, sizeof(*sa)))
3259475f6387SJan Schmidt 		ret = -EFAULT;
3260475f6387SJan Schmidt 
3261475f6387SJan Schmidt 	kfree(sa);
3262475f6387SJan Schmidt 	return ret;
3263475f6387SJan Schmidt }
3264475f6387SJan Schmidt 
3265c11d2c23SStefan Behrens static long btrfs_ioctl_get_dev_stats(struct btrfs_root *root,
3266b27f7c0cSDavid Sterba 				      void __user *arg)
3267c11d2c23SStefan Behrens {
3268c11d2c23SStefan Behrens 	struct btrfs_ioctl_get_dev_stats *sa;
3269c11d2c23SStefan Behrens 	int ret;
3270c11d2c23SStefan Behrens 
3271c11d2c23SStefan Behrens 	sa = memdup_user(arg, sizeof(*sa));
3272c11d2c23SStefan Behrens 	if (IS_ERR(sa))
3273c11d2c23SStefan Behrens 		return PTR_ERR(sa);
3274c11d2c23SStefan Behrens 
3275b27f7c0cSDavid Sterba 	if ((sa->flags & BTRFS_DEV_STATS_RESET) && !capable(CAP_SYS_ADMIN)) {
3276b27f7c0cSDavid Sterba 		kfree(sa);
3277b27f7c0cSDavid Sterba 		return -EPERM;
3278b27f7c0cSDavid Sterba 	}
3279b27f7c0cSDavid Sterba 
3280b27f7c0cSDavid Sterba 	ret = btrfs_get_dev_stats(root, sa);
3281c11d2c23SStefan Behrens 
3282c11d2c23SStefan Behrens 	if (copy_to_user(arg, sa, sizeof(*sa)))
3283c11d2c23SStefan Behrens 		ret = -EFAULT;
3284c11d2c23SStefan Behrens 
3285c11d2c23SStefan Behrens 	kfree(sa);
3286c11d2c23SStefan Behrens 	return ret;
3287c11d2c23SStefan Behrens }
3288c11d2c23SStefan Behrens 
32893f6bcfbdSStefan Behrens static long btrfs_ioctl_dev_replace(struct btrfs_root *root, void __user *arg)
32903f6bcfbdSStefan Behrens {
32913f6bcfbdSStefan Behrens 	struct btrfs_ioctl_dev_replace_args *p;
32923f6bcfbdSStefan Behrens 	int ret;
32933f6bcfbdSStefan Behrens 
32943f6bcfbdSStefan Behrens 	if (!capable(CAP_SYS_ADMIN))
32953f6bcfbdSStefan Behrens 		return -EPERM;
32963f6bcfbdSStefan Behrens 
32973f6bcfbdSStefan Behrens 	p = memdup_user(arg, sizeof(*p));
32983f6bcfbdSStefan Behrens 	if (IS_ERR(p))
32993f6bcfbdSStefan Behrens 		return PTR_ERR(p);
33003f6bcfbdSStefan Behrens 
33013f6bcfbdSStefan Behrens 	switch (p->cmd) {
33023f6bcfbdSStefan Behrens 	case BTRFS_IOCTL_DEV_REPLACE_CMD_START:
33033f6bcfbdSStefan Behrens 		if (atomic_xchg(
33043f6bcfbdSStefan Behrens 			&root->fs_info->mutually_exclusive_operation_running,
33053f6bcfbdSStefan Behrens 			1)) {
33063f6bcfbdSStefan Behrens 			pr_info("btrfs: dev add/delete/balance/replace/resize operation in progress\n");
33073f6bcfbdSStefan Behrens 			ret = -EINPROGRESS;
33083f6bcfbdSStefan Behrens 		} else {
33093f6bcfbdSStefan Behrens 			ret = btrfs_dev_replace_start(root, p);
33103f6bcfbdSStefan Behrens 			atomic_set(
33113f6bcfbdSStefan Behrens 			 &root->fs_info->mutually_exclusive_operation_running,
33123f6bcfbdSStefan Behrens 			 0);
33133f6bcfbdSStefan Behrens 		}
33143f6bcfbdSStefan Behrens 		break;
33153f6bcfbdSStefan Behrens 	case BTRFS_IOCTL_DEV_REPLACE_CMD_STATUS:
33163f6bcfbdSStefan Behrens 		btrfs_dev_replace_status(root->fs_info, p);
33173f6bcfbdSStefan Behrens 		ret = 0;
33183f6bcfbdSStefan Behrens 		break;
33193f6bcfbdSStefan Behrens 	case BTRFS_IOCTL_DEV_REPLACE_CMD_CANCEL:
33203f6bcfbdSStefan Behrens 		ret = btrfs_dev_replace_cancel(root->fs_info, p);
33213f6bcfbdSStefan Behrens 		break;
33223f6bcfbdSStefan Behrens 	default:
33233f6bcfbdSStefan Behrens 		ret = -EINVAL;
33243f6bcfbdSStefan Behrens 		break;
33253f6bcfbdSStefan Behrens 	}
33263f6bcfbdSStefan Behrens 
33273f6bcfbdSStefan Behrens 	if (copy_to_user(arg, p, sizeof(*p)))
33283f6bcfbdSStefan Behrens 		ret = -EFAULT;
33293f6bcfbdSStefan Behrens 
33303f6bcfbdSStefan Behrens 	kfree(p);
33313f6bcfbdSStefan Behrens 	return ret;
33323f6bcfbdSStefan Behrens }
33333f6bcfbdSStefan Behrens 
3334d7728c96SJan Schmidt static long btrfs_ioctl_ino_to_path(struct btrfs_root *root, void __user *arg)
3335d7728c96SJan Schmidt {
3336d7728c96SJan Schmidt 	int ret = 0;
3337d7728c96SJan Schmidt 	int i;
3338740c3d22SChris Mason 	u64 rel_ptr;
3339d7728c96SJan Schmidt 	int size;
3340806468f8SChris Mason 	struct btrfs_ioctl_ino_path_args *ipa = NULL;
3341d7728c96SJan Schmidt 	struct inode_fs_paths *ipath = NULL;
3342d7728c96SJan Schmidt 	struct btrfs_path *path;
3343d7728c96SJan Schmidt 
334482b22ac8SKusanagi Kouichi 	if (!capable(CAP_DAC_READ_SEARCH))
3345d7728c96SJan Schmidt 		return -EPERM;
3346d7728c96SJan Schmidt 
3347d7728c96SJan Schmidt 	path = btrfs_alloc_path();
3348d7728c96SJan Schmidt 	if (!path) {
3349d7728c96SJan Schmidt 		ret = -ENOMEM;
3350d7728c96SJan Schmidt 		goto out;
3351d7728c96SJan Schmidt 	}
3352d7728c96SJan Schmidt 
3353d7728c96SJan Schmidt 	ipa = memdup_user(arg, sizeof(*ipa));
3354d7728c96SJan Schmidt 	if (IS_ERR(ipa)) {
3355d7728c96SJan Schmidt 		ret = PTR_ERR(ipa);
3356d7728c96SJan Schmidt 		ipa = NULL;
3357d7728c96SJan Schmidt 		goto out;
3358d7728c96SJan Schmidt 	}
3359d7728c96SJan Schmidt 
3360d7728c96SJan Schmidt 	size = min_t(u32, ipa->size, 4096);
3361d7728c96SJan Schmidt 	ipath = init_ipath(size, root, path);
3362d7728c96SJan Schmidt 	if (IS_ERR(ipath)) {
3363d7728c96SJan Schmidt 		ret = PTR_ERR(ipath);
3364d7728c96SJan Schmidt 		ipath = NULL;
3365d7728c96SJan Schmidt 		goto out;
3366d7728c96SJan Schmidt 	}
3367d7728c96SJan Schmidt 
3368d7728c96SJan Schmidt 	ret = paths_from_inode(ipa->inum, ipath);
3369d7728c96SJan Schmidt 	if (ret < 0)
3370d7728c96SJan Schmidt 		goto out;
3371d7728c96SJan Schmidt 
3372d7728c96SJan Schmidt 	for (i = 0; i < ipath->fspath->elem_cnt; ++i) {
3373745c4d8eSJeff Mahoney 		rel_ptr = ipath->fspath->val[i] -
3374745c4d8eSJeff Mahoney 			  (u64)(unsigned long)ipath->fspath->val;
3375740c3d22SChris Mason 		ipath->fspath->val[i] = rel_ptr;
3376d7728c96SJan Schmidt 	}
3377d7728c96SJan Schmidt 
3378745c4d8eSJeff Mahoney 	ret = copy_to_user((void *)(unsigned long)ipa->fspath,
3379745c4d8eSJeff Mahoney 			   (void *)(unsigned long)ipath->fspath, size);
3380d7728c96SJan Schmidt 	if (ret) {
3381d7728c96SJan Schmidt 		ret = -EFAULT;
3382d7728c96SJan Schmidt 		goto out;
3383d7728c96SJan Schmidt 	}
3384d7728c96SJan Schmidt 
3385d7728c96SJan Schmidt out:
3386d7728c96SJan Schmidt 	btrfs_free_path(path);
3387d7728c96SJan Schmidt 	free_ipath(ipath);
3388d7728c96SJan Schmidt 	kfree(ipa);
3389d7728c96SJan Schmidt 
3390d7728c96SJan Schmidt 	return ret;
3391d7728c96SJan Schmidt }
3392d7728c96SJan Schmidt 
3393d7728c96SJan Schmidt static int build_ino_list(u64 inum, u64 offset, u64 root, void *ctx)
3394d7728c96SJan Schmidt {
3395d7728c96SJan Schmidt 	struct btrfs_data_container *inodes = ctx;
3396d7728c96SJan Schmidt 	const size_t c = 3 * sizeof(u64);
3397d7728c96SJan Schmidt 
3398d7728c96SJan Schmidt 	if (inodes->bytes_left >= c) {
3399d7728c96SJan Schmidt 		inodes->bytes_left -= c;
3400d7728c96SJan Schmidt 		inodes->val[inodes->elem_cnt] = inum;
3401d7728c96SJan Schmidt 		inodes->val[inodes->elem_cnt + 1] = offset;
3402d7728c96SJan Schmidt 		inodes->val[inodes->elem_cnt + 2] = root;
3403d7728c96SJan Schmidt 		inodes->elem_cnt += 3;
3404d7728c96SJan Schmidt 	} else {
3405d7728c96SJan Schmidt 		inodes->bytes_missing += c - inodes->bytes_left;
3406d7728c96SJan Schmidt 		inodes->bytes_left = 0;
3407d7728c96SJan Schmidt 		inodes->elem_missed += 3;
3408d7728c96SJan Schmidt 	}
3409d7728c96SJan Schmidt 
3410d7728c96SJan Schmidt 	return 0;
3411d7728c96SJan Schmidt }
3412d7728c96SJan Schmidt 
3413d7728c96SJan Schmidt static long btrfs_ioctl_logical_to_ino(struct btrfs_root *root,
3414d7728c96SJan Schmidt 					void __user *arg)
3415d7728c96SJan Schmidt {
3416d7728c96SJan Schmidt 	int ret = 0;
3417d7728c96SJan Schmidt 	int size;
3418d7728c96SJan Schmidt 	struct btrfs_ioctl_logical_ino_args *loi;
3419d7728c96SJan Schmidt 	struct btrfs_data_container *inodes = NULL;
3420d7728c96SJan Schmidt 	struct btrfs_path *path = NULL;
3421d7728c96SJan Schmidt 
3422d7728c96SJan Schmidt 	if (!capable(CAP_SYS_ADMIN))
3423d7728c96SJan Schmidt 		return -EPERM;
3424d7728c96SJan Schmidt 
3425d7728c96SJan Schmidt 	loi = memdup_user(arg, sizeof(*loi));
3426d7728c96SJan Schmidt 	if (IS_ERR(loi)) {
3427d7728c96SJan Schmidt 		ret = PTR_ERR(loi);
3428d7728c96SJan Schmidt 		loi = NULL;
3429d7728c96SJan Schmidt 		goto out;
3430d7728c96SJan Schmidt 	}
3431d7728c96SJan Schmidt 
3432d7728c96SJan Schmidt 	path = btrfs_alloc_path();
3433d7728c96SJan Schmidt 	if (!path) {
3434d7728c96SJan Schmidt 		ret = -ENOMEM;
3435d7728c96SJan Schmidt 		goto out;
3436d7728c96SJan Schmidt 	}
3437d7728c96SJan Schmidt 
3438425d17a2SLiu Bo 	size = min_t(u32, loi->size, 64 * 1024);
3439d7728c96SJan Schmidt 	inodes = init_data_container(size);
3440d7728c96SJan Schmidt 	if (IS_ERR(inodes)) {
3441d7728c96SJan Schmidt 		ret = PTR_ERR(inodes);
3442d7728c96SJan Schmidt 		inodes = NULL;
3443d7728c96SJan Schmidt 		goto out;
3444d7728c96SJan Schmidt 	}
3445d7728c96SJan Schmidt 
3446df031f07SLiu Bo 	ret = iterate_inodes_from_logical(loi->logical, root->fs_info, path,
3447df031f07SLiu Bo 					  build_ino_list, inodes);
3448df031f07SLiu Bo 	if (ret == -EINVAL)
3449d7728c96SJan Schmidt 		ret = -ENOENT;
3450d7728c96SJan Schmidt 	if (ret < 0)
3451d7728c96SJan Schmidt 		goto out;
3452d7728c96SJan Schmidt 
3453745c4d8eSJeff Mahoney 	ret = copy_to_user((void *)(unsigned long)loi->inodes,
3454745c4d8eSJeff Mahoney 			   (void *)(unsigned long)inodes, size);
3455d7728c96SJan Schmidt 	if (ret)
3456d7728c96SJan Schmidt 		ret = -EFAULT;
3457d7728c96SJan Schmidt 
3458d7728c96SJan Schmidt out:
3459d7728c96SJan Schmidt 	btrfs_free_path(path);
3460425d17a2SLiu Bo 	vfree(inodes);
3461d7728c96SJan Schmidt 	kfree(loi);
3462d7728c96SJan Schmidt 
3463d7728c96SJan Schmidt 	return ret;
3464d7728c96SJan Schmidt }
3465d7728c96SJan Schmidt 
346619a39dceSIlya Dryomov void update_ioctl_balance_args(struct btrfs_fs_info *fs_info, int lock,
3467c9e9f97bSIlya Dryomov 			       struct btrfs_ioctl_balance_args *bargs)
3468c9e9f97bSIlya Dryomov {
3469c9e9f97bSIlya Dryomov 	struct btrfs_balance_control *bctl = fs_info->balance_ctl;
3470c9e9f97bSIlya Dryomov 
3471c9e9f97bSIlya Dryomov 	bargs->flags = bctl->flags;
3472c9e9f97bSIlya Dryomov 
3473837d5b6eSIlya Dryomov 	if (atomic_read(&fs_info->balance_running))
3474837d5b6eSIlya Dryomov 		bargs->state |= BTRFS_BALANCE_STATE_RUNNING;
3475837d5b6eSIlya Dryomov 	if (atomic_read(&fs_info->balance_pause_req))
3476837d5b6eSIlya Dryomov 		bargs->state |= BTRFS_BALANCE_STATE_PAUSE_REQ;
3477a7e99c69SIlya Dryomov 	if (atomic_read(&fs_info->balance_cancel_req))
3478a7e99c69SIlya Dryomov 		bargs->state |= BTRFS_BALANCE_STATE_CANCEL_REQ;
3479837d5b6eSIlya Dryomov 
3480c9e9f97bSIlya Dryomov 	memcpy(&bargs->data, &bctl->data, sizeof(bargs->data));
3481c9e9f97bSIlya Dryomov 	memcpy(&bargs->meta, &bctl->meta, sizeof(bargs->meta));
3482c9e9f97bSIlya Dryomov 	memcpy(&bargs->sys, &bctl->sys, sizeof(bargs->sys));
348319a39dceSIlya Dryomov 
348419a39dceSIlya Dryomov 	if (lock) {
348519a39dceSIlya Dryomov 		spin_lock(&fs_info->balance_lock);
348619a39dceSIlya Dryomov 		memcpy(&bargs->stat, &bctl->stat, sizeof(bargs->stat));
348719a39dceSIlya Dryomov 		spin_unlock(&fs_info->balance_lock);
348819a39dceSIlya Dryomov 	} else {
348919a39dceSIlya Dryomov 		memcpy(&bargs->stat, &bctl->stat, sizeof(bargs->stat));
349019a39dceSIlya Dryomov 	}
3491c9e9f97bSIlya Dryomov }
3492c9e9f97bSIlya Dryomov 
34939ba1f6e4SLiu Bo static long btrfs_ioctl_balance(struct file *file, void __user *arg)
3494c9e9f97bSIlya Dryomov {
3495496ad9aaSAl Viro 	struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
3496c9e9f97bSIlya Dryomov 	struct btrfs_fs_info *fs_info = root->fs_info;
3497c9e9f97bSIlya Dryomov 	struct btrfs_ioctl_balance_args *bargs;
3498c9e9f97bSIlya Dryomov 	struct btrfs_balance_control *bctl;
3499ed0fb78fSIlya Dryomov 	bool need_unlock; /* for mut. excl. ops lock */
3500c9e9f97bSIlya Dryomov 	int ret;
3501c9e9f97bSIlya Dryomov 
3502c9e9f97bSIlya Dryomov 	if (!capable(CAP_SYS_ADMIN))
3503c9e9f97bSIlya Dryomov 		return -EPERM;
3504c9e9f97bSIlya Dryomov 
3505e54bfa31SLiu Bo 	ret = mnt_want_write_file(file);
35069ba1f6e4SLiu Bo 	if (ret)
35079ba1f6e4SLiu Bo 		return ret;
35089ba1f6e4SLiu Bo 
3509ed0fb78fSIlya Dryomov again:
3510ed0fb78fSIlya Dryomov 	if (!atomic_xchg(&fs_info->mutually_exclusive_operation_running, 1)) {
3511c9e9f97bSIlya Dryomov 		mutex_lock(&fs_info->volume_mutex);
3512c9e9f97bSIlya Dryomov 		mutex_lock(&fs_info->balance_mutex);
3513ed0fb78fSIlya Dryomov 		need_unlock = true;
3514ed0fb78fSIlya Dryomov 		goto locked;
3515ed0fb78fSIlya Dryomov 	}
3516ed0fb78fSIlya Dryomov 
3517ed0fb78fSIlya Dryomov 	/*
3518ed0fb78fSIlya Dryomov 	 * mut. excl. ops lock is locked.  Three possibilites:
3519ed0fb78fSIlya Dryomov 	 *   (1) some other op is running
3520ed0fb78fSIlya Dryomov 	 *   (2) balance is running
3521ed0fb78fSIlya Dryomov 	 *   (3) balance is paused -- special case (think resume)
3522ed0fb78fSIlya Dryomov 	 */
3523ed0fb78fSIlya Dryomov 	mutex_lock(&fs_info->balance_mutex);
3524ed0fb78fSIlya Dryomov 	if (fs_info->balance_ctl) {
3525ed0fb78fSIlya Dryomov 		/* this is either (2) or (3) */
3526ed0fb78fSIlya Dryomov 		if (!atomic_read(&fs_info->balance_running)) {
3527ed0fb78fSIlya Dryomov 			mutex_unlock(&fs_info->balance_mutex);
3528ed0fb78fSIlya Dryomov 			if (!mutex_trylock(&fs_info->volume_mutex))
3529ed0fb78fSIlya Dryomov 				goto again;
3530ed0fb78fSIlya Dryomov 			mutex_lock(&fs_info->balance_mutex);
3531ed0fb78fSIlya Dryomov 
3532ed0fb78fSIlya Dryomov 			if (fs_info->balance_ctl &&
3533ed0fb78fSIlya Dryomov 			    !atomic_read(&fs_info->balance_running)) {
3534ed0fb78fSIlya Dryomov 				/* this is (3) */
3535ed0fb78fSIlya Dryomov 				need_unlock = false;
3536ed0fb78fSIlya Dryomov 				goto locked;
3537ed0fb78fSIlya Dryomov 			}
3538ed0fb78fSIlya Dryomov 
3539ed0fb78fSIlya Dryomov 			mutex_unlock(&fs_info->balance_mutex);
3540ed0fb78fSIlya Dryomov 			mutex_unlock(&fs_info->volume_mutex);
3541ed0fb78fSIlya Dryomov 			goto again;
3542ed0fb78fSIlya Dryomov 		} else {
3543ed0fb78fSIlya Dryomov 			/* this is (2) */
3544ed0fb78fSIlya Dryomov 			mutex_unlock(&fs_info->balance_mutex);
3545ed0fb78fSIlya Dryomov 			ret = -EINPROGRESS;
3546ed0fb78fSIlya Dryomov 			goto out;
3547ed0fb78fSIlya Dryomov 		}
3548ed0fb78fSIlya Dryomov 	} else {
3549ed0fb78fSIlya Dryomov 		/* this is (1) */
3550ed0fb78fSIlya Dryomov 		mutex_unlock(&fs_info->balance_mutex);
3551ed0fb78fSIlya Dryomov 		pr_info("btrfs: dev add/delete/balance/replace/resize operation in progress\n");
3552ed0fb78fSIlya Dryomov 		ret = -EINVAL;
3553ed0fb78fSIlya Dryomov 		goto out;
3554ed0fb78fSIlya Dryomov 	}
3555ed0fb78fSIlya Dryomov 
3556ed0fb78fSIlya Dryomov locked:
3557ed0fb78fSIlya Dryomov 	BUG_ON(!atomic_read(&fs_info->mutually_exclusive_operation_running));
3558c9e9f97bSIlya Dryomov 
3559c9e9f97bSIlya Dryomov 	if (arg) {
3560c9e9f97bSIlya Dryomov 		bargs = memdup_user(arg, sizeof(*bargs));
3561c9e9f97bSIlya Dryomov 		if (IS_ERR(bargs)) {
3562c9e9f97bSIlya Dryomov 			ret = PTR_ERR(bargs);
3563ed0fb78fSIlya Dryomov 			goto out_unlock;
3564c9e9f97bSIlya Dryomov 		}
3565de322263SIlya Dryomov 
3566de322263SIlya Dryomov 		if (bargs->flags & BTRFS_BALANCE_RESUME) {
3567de322263SIlya Dryomov 			if (!fs_info->balance_ctl) {
3568de322263SIlya Dryomov 				ret = -ENOTCONN;
3569de322263SIlya Dryomov 				goto out_bargs;
3570de322263SIlya Dryomov 			}
3571de322263SIlya Dryomov 
3572de322263SIlya Dryomov 			bctl = fs_info->balance_ctl;
3573de322263SIlya Dryomov 			spin_lock(&fs_info->balance_lock);
3574de322263SIlya Dryomov 			bctl->flags |= BTRFS_BALANCE_RESUME;
3575de322263SIlya Dryomov 			spin_unlock(&fs_info->balance_lock);
3576de322263SIlya Dryomov 
3577de322263SIlya Dryomov 			goto do_balance;
3578de322263SIlya Dryomov 		}
3579c9e9f97bSIlya Dryomov 	} else {
3580c9e9f97bSIlya Dryomov 		bargs = NULL;
3581c9e9f97bSIlya Dryomov 	}
3582c9e9f97bSIlya Dryomov 
3583ed0fb78fSIlya Dryomov 	if (fs_info->balance_ctl) {
3584837d5b6eSIlya Dryomov 		ret = -EINPROGRESS;
3585837d5b6eSIlya Dryomov 		goto out_bargs;
3586837d5b6eSIlya Dryomov 	}
3587837d5b6eSIlya Dryomov 
3588c9e9f97bSIlya Dryomov 	bctl = kzalloc(sizeof(*bctl), GFP_NOFS);
3589c9e9f97bSIlya Dryomov 	if (!bctl) {
3590c9e9f97bSIlya Dryomov 		ret = -ENOMEM;
3591c9e9f97bSIlya Dryomov 		goto out_bargs;
3592c9e9f97bSIlya Dryomov 	}
3593c9e9f97bSIlya Dryomov 
3594c9e9f97bSIlya Dryomov 	bctl->fs_info = fs_info;
3595c9e9f97bSIlya Dryomov 	if (arg) {
3596c9e9f97bSIlya Dryomov 		memcpy(&bctl->data, &bargs->data, sizeof(bctl->data));
3597c9e9f97bSIlya Dryomov 		memcpy(&bctl->meta, &bargs->meta, sizeof(bctl->meta));
3598c9e9f97bSIlya Dryomov 		memcpy(&bctl->sys, &bargs->sys, sizeof(bctl->sys));
3599c9e9f97bSIlya Dryomov 
3600c9e9f97bSIlya Dryomov 		bctl->flags = bargs->flags;
3601f43ffb60SIlya Dryomov 	} else {
3602f43ffb60SIlya Dryomov 		/* balance everything - no filters */
3603f43ffb60SIlya Dryomov 		bctl->flags |= BTRFS_BALANCE_TYPE_MASK;
3604c9e9f97bSIlya Dryomov 	}
3605c9e9f97bSIlya Dryomov 
3606de322263SIlya Dryomov do_balance:
3607c9e9f97bSIlya Dryomov 	/*
3608ed0fb78fSIlya Dryomov 	 * Ownership of bctl and mutually_exclusive_operation_running
3609ed0fb78fSIlya Dryomov 	 * goes to to btrfs_balance.  bctl is freed in __cancel_balance,
3610ed0fb78fSIlya Dryomov 	 * or, if restriper was paused all the way until unmount, in
3611ed0fb78fSIlya Dryomov 	 * free_fs_info.  mutually_exclusive_operation_running is
3612ed0fb78fSIlya Dryomov 	 * cleared in __cancel_balance.
3613c9e9f97bSIlya Dryomov 	 */
3614ed0fb78fSIlya Dryomov 	need_unlock = false;
3615ed0fb78fSIlya Dryomov 
3616ed0fb78fSIlya Dryomov 	ret = btrfs_balance(bctl, bargs);
3617ed0fb78fSIlya Dryomov 
3618c9e9f97bSIlya Dryomov 	if (arg) {
3619c9e9f97bSIlya Dryomov 		if (copy_to_user(arg, bargs, sizeof(*bargs)))
3620c9e9f97bSIlya Dryomov 			ret = -EFAULT;
3621c9e9f97bSIlya Dryomov 	}
3622c9e9f97bSIlya Dryomov 
3623c9e9f97bSIlya Dryomov out_bargs:
3624c9e9f97bSIlya Dryomov 	kfree(bargs);
3625ed0fb78fSIlya Dryomov out_unlock:
3626c9e9f97bSIlya Dryomov 	mutex_unlock(&fs_info->balance_mutex);
3627c9e9f97bSIlya Dryomov 	mutex_unlock(&fs_info->volume_mutex);
3628ed0fb78fSIlya Dryomov 	if (need_unlock)
3629ed0fb78fSIlya Dryomov 		atomic_set(&fs_info->mutually_exclusive_operation_running, 0);
3630ed0fb78fSIlya Dryomov out:
3631e54bfa31SLiu Bo 	mnt_drop_write_file(file);
3632c9e9f97bSIlya Dryomov 	return ret;
3633c9e9f97bSIlya Dryomov }
3634c9e9f97bSIlya Dryomov 
3635837d5b6eSIlya Dryomov static long btrfs_ioctl_balance_ctl(struct btrfs_root *root, int cmd)
3636837d5b6eSIlya Dryomov {
3637837d5b6eSIlya Dryomov 	if (!capable(CAP_SYS_ADMIN))
3638837d5b6eSIlya Dryomov 		return -EPERM;
3639837d5b6eSIlya Dryomov 
3640837d5b6eSIlya Dryomov 	switch (cmd) {
3641837d5b6eSIlya Dryomov 	case BTRFS_BALANCE_CTL_PAUSE:
3642837d5b6eSIlya Dryomov 		return btrfs_pause_balance(root->fs_info);
3643a7e99c69SIlya Dryomov 	case BTRFS_BALANCE_CTL_CANCEL:
3644a7e99c69SIlya Dryomov 		return btrfs_cancel_balance(root->fs_info);
3645837d5b6eSIlya Dryomov 	}
3646837d5b6eSIlya Dryomov 
3647837d5b6eSIlya Dryomov 	return -EINVAL;
3648837d5b6eSIlya Dryomov }
3649837d5b6eSIlya Dryomov 
365019a39dceSIlya Dryomov static long btrfs_ioctl_balance_progress(struct btrfs_root *root,
365119a39dceSIlya Dryomov 					 void __user *arg)
365219a39dceSIlya Dryomov {
365319a39dceSIlya Dryomov 	struct btrfs_fs_info *fs_info = root->fs_info;
365419a39dceSIlya Dryomov 	struct btrfs_ioctl_balance_args *bargs;
365519a39dceSIlya Dryomov 	int ret = 0;
365619a39dceSIlya Dryomov 
365719a39dceSIlya Dryomov 	if (!capable(CAP_SYS_ADMIN))
365819a39dceSIlya Dryomov 		return -EPERM;
365919a39dceSIlya Dryomov 
366019a39dceSIlya Dryomov 	mutex_lock(&fs_info->balance_mutex);
366119a39dceSIlya Dryomov 	if (!fs_info->balance_ctl) {
366219a39dceSIlya Dryomov 		ret = -ENOTCONN;
366319a39dceSIlya Dryomov 		goto out;
366419a39dceSIlya Dryomov 	}
366519a39dceSIlya Dryomov 
366619a39dceSIlya Dryomov 	bargs = kzalloc(sizeof(*bargs), GFP_NOFS);
366719a39dceSIlya Dryomov 	if (!bargs) {
366819a39dceSIlya Dryomov 		ret = -ENOMEM;
366919a39dceSIlya Dryomov 		goto out;
367019a39dceSIlya Dryomov 	}
367119a39dceSIlya Dryomov 
367219a39dceSIlya Dryomov 	update_ioctl_balance_args(fs_info, 1, bargs);
367319a39dceSIlya Dryomov 
367419a39dceSIlya Dryomov 	if (copy_to_user(arg, bargs, sizeof(*bargs)))
367519a39dceSIlya Dryomov 		ret = -EFAULT;
367619a39dceSIlya Dryomov 
367719a39dceSIlya Dryomov 	kfree(bargs);
367819a39dceSIlya Dryomov out:
367919a39dceSIlya Dryomov 	mutex_unlock(&fs_info->balance_mutex);
368019a39dceSIlya Dryomov 	return ret;
368119a39dceSIlya Dryomov }
368219a39dceSIlya Dryomov 
3683905b0ddaSMiao Xie static long btrfs_ioctl_quota_ctl(struct file *file, void __user *arg)
36845d13a37bSArne Jansen {
3685496ad9aaSAl Viro 	struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
36865d13a37bSArne Jansen 	struct btrfs_ioctl_quota_ctl_args *sa;
36875d13a37bSArne Jansen 	struct btrfs_trans_handle *trans = NULL;
36885d13a37bSArne Jansen 	int ret;
36895d13a37bSArne Jansen 	int err;
36905d13a37bSArne Jansen 
36915d13a37bSArne Jansen 	if (!capable(CAP_SYS_ADMIN))
36925d13a37bSArne Jansen 		return -EPERM;
36935d13a37bSArne Jansen 
3694905b0ddaSMiao Xie 	ret = mnt_want_write_file(file);
3695905b0ddaSMiao Xie 	if (ret)
3696905b0ddaSMiao Xie 		return ret;
36975d13a37bSArne Jansen 
36985d13a37bSArne Jansen 	sa = memdup_user(arg, sizeof(*sa));
3699905b0ddaSMiao Xie 	if (IS_ERR(sa)) {
3700905b0ddaSMiao Xie 		ret = PTR_ERR(sa);
3701905b0ddaSMiao Xie 		goto drop_write;
3702905b0ddaSMiao Xie 	}
37035d13a37bSArne Jansen 
37047708f029SWang Shilong 	down_write(&root->fs_info->subvol_sem);
370592f183aaSWang Shilong 	trans = btrfs_start_transaction(root->fs_info->tree_root, 2);
37065d13a37bSArne Jansen 	if (IS_ERR(trans)) {
37075d13a37bSArne Jansen 		ret = PTR_ERR(trans);
37085d13a37bSArne Jansen 		goto out;
37095d13a37bSArne Jansen 	}
37105d13a37bSArne Jansen 
37115d13a37bSArne Jansen 	switch (sa->cmd) {
37125d13a37bSArne Jansen 	case BTRFS_QUOTA_CTL_ENABLE:
37135d13a37bSArne Jansen 		ret = btrfs_quota_enable(trans, root->fs_info);
37145d13a37bSArne Jansen 		break;
37155d13a37bSArne Jansen 	case BTRFS_QUOTA_CTL_DISABLE:
37165d13a37bSArne Jansen 		ret = btrfs_quota_disable(trans, root->fs_info);
37175d13a37bSArne Jansen 		break;
37185d13a37bSArne Jansen 	default:
37195d13a37bSArne Jansen 		ret = -EINVAL;
37205d13a37bSArne Jansen 		break;
37215d13a37bSArne Jansen 	}
37225d13a37bSArne Jansen 
372392f183aaSWang Shilong 	err = btrfs_commit_transaction(trans, root->fs_info->tree_root);
37245d13a37bSArne Jansen 	if (err && !ret)
37255d13a37bSArne Jansen 		ret = err;
37265d13a37bSArne Jansen out:
37275d13a37bSArne Jansen 	kfree(sa);
37287708f029SWang Shilong 	up_write(&root->fs_info->subvol_sem);
3729905b0ddaSMiao Xie drop_write:
3730905b0ddaSMiao Xie 	mnt_drop_write_file(file);
37315d13a37bSArne Jansen 	return ret;
37325d13a37bSArne Jansen }
37335d13a37bSArne Jansen 
3734905b0ddaSMiao Xie static long btrfs_ioctl_qgroup_assign(struct file *file, void __user *arg)
37355d13a37bSArne Jansen {
3736496ad9aaSAl Viro 	struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
37375d13a37bSArne Jansen 	struct btrfs_ioctl_qgroup_assign_args *sa;
37385d13a37bSArne Jansen 	struct btrfs_trans_handle *trans;
37395d13a37bSArne Jansen 	int ret;
37405d13a37bSArne Jansen 	int err;
37415d13a37bSArne Jansen 
37425d13a37bSArne Jansen 	if (!capable(CAP_SYS_ADMIN))
37435d13a37bSArne Jansen 		return -EPERM;
37445d13a37bSArne Jansen 
3745905b0ddaSMiao Xie 	ret = mnt_want_write_file(file);
3746905b0ddaSMiao Xie 	if (ret)
3747905b0ddaSMiao Xie 		return ret;
37485d13a37bSArne Jansen 
37495d13a37bSArne Jansen 	sa = memdup_user(arg, sizeof(*sa));
3750905b0ddaSMiao Xie 	if (IS_ERR(sa)) {
3751905b0ddaSMiao Xie 		ret = PTR_ERR(sa);
3752905b0ddaSMiao Xie 		goto drop_write;
3753905b0ddaSMiao Xie 	}
37545d13a37bSArne Jansen 
37555d13a37bSArne Jansen 	trans = btrfs_join_transaction(root);
37565d13a37bSArne Jansen 	if (IS_ERR(trans)) {
37575d13a37bSArne Jansen 		ret = PTR_ERR(trans);
37585d13a37bSArne Jansen 		goto out;
37595d13a37bSArne Jansen 	}
37605d13a37bSArne Jansen 
37615d13a37bSArne Jansen 	/* FIXME: check if the IDs really exist */
37625d13a37bSArne Jansen 	if (sa->assign) {
37635d13a37bSArne Jansen 		ret = btrfs_add_qgroup_relation(trans, root->fs_info,
37645d13a37bSArne Jansen 						sa->src, sa->dst);
37655d13a37bSArne Jansen 	} else {
37665d13a37bSArne Jansen 		ret = btrfs_del_qgroup_relation(trans, root->fs_info,
37675d13a37bSArne Jansen 						sa->src, sa->dst);
37685d13a37bSArne Jansen 	}
37695d13a37bSArne Jansen 
37705d13a37bSArne Jansen 	err = btrfs_end_transaction(trans, root);
37715d13a37bSArne Jansen 	if (err && !ret)
37725d13a37bSArne Jansen 		ret = err;
37735d13a37bSArne Jansen 
37745d13a37bSArne Jansen out:
37755d13a37bSArne Jansen 	kfree(sa);
3776905b0ddaSMiao Xie drop_write:
3777905b0ddaSMiao Xie 	mnt_drop_write_file(file);
37785d13a37bSArne Jansen 	return ret;
37795d13a37bSArne Jansen }
37805d13a37bSArne Jansen 
3781905b0ddaSMiao Xie static long btrfs_ioctl_qgroup_create(struct file *file, void __user *arg)
37825d13a37bSArne Jansen {
3783496ad9aaSAl Viro 	struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
37845d13a37bSArne Jansen 	struct btrfs_ioctl_qgroup_create_args *sa;
37855d13a37bSArne Jansen 	struct btrfs_trans_handle *trans;
37865d13a37bSArne Jansen 	int ret;
37875d13a37bSArne Jansen 	int err;
37885d13a37bSArne Jansen 
37895d13a37bSArne Jansen 	if (!capable(CAP_SYS_ADMIN))
37905d13a37bSArne Jansen 		return -EPERM;
37915d13a37bSArne Jansen 
3792905b0ddaSMiao Xie 	ret = mnt_want_write_file(file);
3793905b0ddaSMiao Xie 	if (ret)
3794905b0ddaSMiao Xie 		return ret;
37955d13a37bSArne Jansen 
37965d13a37bSArne Jansen 	sa = memdup_user(arg, sizeof(*sa));
3797905b0ddaSMiao Xie 	if (IS_ERR(sa)) {
3798905b0ddaSMiao Xie 		ret = PTR_ERR(sa);
3799905b0ddaSMiao Xie 		goto drop_write;
3800905b0ddaSMiao Xie 	}
38015d13a37bSArne Jansen 
3802d86e56cfSMiao Xie 	if (!sa->qgroupid) {
3803d86e56cfSMiao Xie 		ret = -EINVAL;
3804d86e56cfSMiao Xie 		goto out;
3805d86e56cfSMiao Xie 	}
3806d86e56cfSMiao Xie 
38075d13a37bSArne Jansen 	trans = btrfs_join_transaction(root);
38085d13a37bSArne Jansen 	if (IS_ERR(trans)) {
38095d13a37bSArne Jansen 		ret = PTR_ERR(trans);
38105d13a37bSArne Jansen 		goto out;
38115d13a37bSArne Jansen 	}
38125d13a37bSArne Jansen 
38135d13a37bSArne Jansen 	/* FIXME: check if the IDs really exist */
38145d13a37bSArne Jansen 	if (sa->create) {
38155d13a37bSArne Jansen 		ret = btrfs_create_qgroup(trans, root->fs_info, sa->qgroupid,
38165d13a37bSArne Jansen 					  NULL);
38175d13a37bSArne Jansen 	} else {
38185d13a37bSArne Jansen 		ret = btrfs_remove_qgroup(trans, root->fs_info, sa->qgroupid);
38195d13a37bSArne Jansen 	}
38205d13a37bSArne Jansen 
38215d13a37bSArne Jansen 	err = btrfs_end_transaction(trans, root);
38225d13a37bSArne Jansen 	if (err && !ret)
38235d13a37bSArne Jansen 		ret = err;
38245d13a37bSArne Jansen 
38255d13a37bSArne Jansen out:
38265d13a37bSArne Jansen 	kfree(sa);
3827905b0ddaSMiao Xie drop_write:
3828905b0ddaSMiao Xie 	mnt_drop_write_file(file);
38295d13a37bSArne Jansen 	return ret;
38305d13a37bSArne Jansen }
38315d13a37bSArne Jansen 
3832905b0ddaSMiao Xie static long btrfs_ioctl_qgroup_limit(struct file *file, void __user *arg)
38335d13a37bSArne Jansen {
3834496ad9aaSAl Viro 	struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
38355d13a37bSArne Jansen 	struct btrfs_ioctl_qgroup_limit_args *sa;
38365d13a37bSArne Jansen 	struct btrfs_trans_handle *trans;
38375d13a37bSArne Jansen 	int ret;
38385d13a37bSArne Jansen 	int err;
38395d13a37bSArne Jansen 	u64 qgroupid;
38405d13a37bSArne Jansen 
38415d13a37bSArne Jansen 	if (!capable(CAP_SYS_ADMIN))
38425d13a37bSArne Jansen 		return -EPERM;
38435d13a37bSArne Jansen 
3844905b0ddaSMiao Xie 	ret = mnt_want_write_file(file);
3845905b0ddaSMiao Xie 	if (ret)
3846905b0ddaSMiao Xie 		return ret;
38475d13a37bSArne Jansen 
38485d13a37bSArne Jansen 	sa = memdup_user(arg, sizeof(*sa));
3849905b0ddaSMiao Xie 	if (IS_ERR(sa)) {
3850905b0ddaSMiao Xie 		ret = PTR_ERR(sa);
3851905b0ddaSMiao Xie 		goto drop_write;
3852905b0ddaSMiao Xie 	}
38535d13a37bSArne Jansen 
38545d13a37bSArne Jansen 	trans = btrfs_join_transaction(root);
38555d13a37bSArne Jansen 	if (IS_ERR(trans)) {
38565d13a37bSArne Jansen 		ret = PTR_ERR(trans);
38575d13a37bSArne Jansen 		goto out;
38585d13a37bSArne Jansen 	}
38595d13a37bSArne Jansen 
38605d13a37bSArne Jansen 	qgroupid = sa->qgroupid;
38615d13a37bSArne Jansen 	if (!qgroupid) {
38625d13a37bSArne Jansen 		/* take the current subvol as qgroup */
38635d13a37bSArne Jansen 		qgroupid = root->root_key.objectid;
38645d13a37bSArne Jansen 	}
38655d13a37bSArne Jansen 
38665d13a37bSArne Jansen 	/* FIXME: check if the IDs really exist */
38675d13a37bSArne Jansen 	ret = btrfs_limit_qgroup(trans, root->fs_info, qgroupid, &sa->lim);
38685d13a37bSArne Jansen 
38695d13a37bSArne Jansen 	err = btrfs_end_transaction(trans, root);
38705d13a37bSArne Jansen 	if (err && !ret)
38715d13a37bSArne Jansen 		ret = err;
38725d13a37bSArne Jansen 
38735d13a37bSArne Jansen out:
38745d13a37bSArne Jansen 	kfree(sa);
3875905b0ddaSMiao Xie drop_write:
3876905b0ddaSMiao Xie 	mnt_drop_write_file(file);
38775d13a37bSArne Jansen 	return ret;
38785d13a37bSArne Jansen }
38795d13a37bSArne Jansen 
38802f232036SJan Schmidt static long btrfs_ioctl_quota_rescan(struct file *file, void __user *arg)
38812f232036SJan Schmidt {
38822f232036SJan Schmidt 	struct btrfs_root *root = BTRFS_I(fdentry(file)->d_inode)->root;
38832f232036SJan Schmidt 	struct btrfs_ioctl_quota_rescan_args *qsa;
38842f232036SJan Schmidt 	int ret;
38852f232036SJan Schmidt 
38862f232036SJan Schmidt 	if (!capable(CAP_SYS_ADMIN))
38872f232036SJan Schmidt 		return -EPERM;
38882f232036SJan Schmidt 
38892f232036SJan Schmidt 	ret = mnt_want_write_file(file);
38902f232036SJan Schmidt 	if (ret)
38912f232036SJan Schmidt 		return ret;
38922f232036SJan Schmidt 
38932f232036SJan Schmidt 	qsa = memdup_user(arg, sizeof(*qsa));
38942f232036SJan Schmidt 	if (IS_ERR(qsa)) {
38952f232036SJan Schmidt 		ret = PTR_ERR(qsa);
38962f232036SJan Schmidt 		goto drop_write;
38972f232036SJan Schmidt 	}
38982f232036SJan Schmidt 
38992f232036SJan Schmidt 	if (qsa->flags) {
39002f232036SJan Schmidt 		ret = -EINVAL;
39012f232036SJan Schmidt 		goto out;
39022f232036SJan Schmidt 	}
39032f232036SJan Schmidt 
39042f232036SJan Schmidt 	ret = btrfs_qgroup_rescan(root->fs_info);
39052f232036SJan Schmidt 
39062f232036SJan Schmidt out:
39072f232036SJan Schmidt 	kfree(qsa);
39082f232036SJan Schmidt drop_write:
39092f232036SJan Schmidt 	mnt_drop_write_file(file);
39102f232036SJan Schmidt 	return ret;
39112f232036SJan Schmidt }
39122f232036SJan Schmidt 
39132f232036SJan Schmidt static long btrfs_ioctl_quota_rescan_status(struct file *file, void __user *arg)
39142f232036SJan Schmidt {
39152f232036SJan Schmidt 	struct btrfs_root *root = BTRFS_I(fdentry(file)->d_inode)->root;
39162f232036SJan Schmidt 	struct btrfs_ioctl_quota_rescan_args *qsa;
39172f232036SJan Schmidt 	int ret = 0;
39182f232036SJan Schmidt 
39192f232036SJan Schmidt 	if (!capable(CAP_SYS_ADMIN))
39202f232036SJan Schmidt 		return -EPERM;
39212f232036SJan Schmidt 
39222f232036SJan Schmidt 	qsa = kzalloc(sizeof(*qsa), GFP_NOFS);
39232f232036SJan Schmidt 	if (!qsa)
39242f232036SJan Schmidt 		return -ENOMEM;
39252f232036SJan Schmidt 
39262f232036SJan Schmidt 	if (root->fs_info->qgroup_flags & BTRFS_QGROUP_STATUS_FLAG_RESCAN) {
39272f232036SJan Schmidt 		qsa->flags = 1;
39282f232036SJan Schmidt 		qsa->progress = root->fs_info->qgroup_rescan_progress.objectid;
39292f232036SJan Schmidt 	}
39302f232036SJan Schmidt 
39312f232036SJan Schmidt 	if (copy_to_user(arg, qsa, sizeof(*qsa)))
39322f232036SJan Schmidt 		ret = -EFAULT;
39332f232036SJan Schmidt 
39342f232036SJan Schmidt 	kfree(qsa);
39352f232036SJan Schmidt 	return ret;
39362f232036SJan Schmidt }
39372f232036SJan Schmidt 
393857254b6eSJan Schmidt static long btrfs_ioctl_quota_rescan_wait(struct file *file, void __user *arg)
393957254b6eSJan Schmidt {
394057254b6eSJan Schmidt 	struct btrfs_root *root = BTRFS_I(fdentry(file)->d_inode)->root;
394157254b6eSJan Schmidt 
394257254b6eSJan Schmidt 	if (!capable(CAP_SYS_ADMIN))
394357254b6eSJan Schmidt 		return -EPERM;
394457254b6eSJan Schmidt 
394557254b6eSJan Schmidt 	return btrfs_qgroup_wait_for_completion(root->fs_info);
394657254b6eSJan Schmidt }
394757254b6eSJan Schmidt 
39488ea05e3aSAlexander Block static long btrfs_ioctl_set_received_subvol(struct file *file,
39498ea05e3aSAlexander Block 					    void __user *arg)
39508ea05e3aSAlexander Block {
39518ea05e3aSAlexander Block 	struct btrfs_ioctl_received_subvol_args *sa = NULL;
3952496ad9aaSAl Viro 	struct inode *inode = file_inode(file);
39538ea05e3aSAlexander Block 	struct btrfs_root *root = BTRFS_I(inode)->root;
39548ea05e3aSAlexander Block 	struct btrfs_root_item *root_item = &root->root_item;
39558ea05e3aSAlexander Block 	struct btrfs_trans_handle *trans;
39568ea05e3aSAlexander Block 	struct timespec ct = CURRENT_TIME;
39578ea05e3aSAlexander Block 	int ret = 0;
39588ea05e3aSAlexander Block 
39598ea05e3aSAlexander Block 	ret = mnt_want_write_file(file);
39608ea05e3aSAlexander Block 	if (ret < 0)
39618ea05e3aSAlexander Block 		return ret;
39628ea05e3aSAlexander Block 
39638ea05e3aSAlexander Block 	down_write(&root->fs_info->subvol_sem);
39648ea05e3aSAlexander Block 
39658ea05e3aSAlexander Block 	if (btrfs_ino(inode) != BTRFS_FIRST_FREE_OBJECTID) {
39668ea05e3aSAlexander Block 		ret = -EINVAL;
39678ea05e3aSAlexander Block 		goto out;
39688ea05e3aSAlexander Block 	}
39698ea05e3aSAlexander Block 
39708ea05e3aSAlexander Block 	if (btrfs_root_readonly(root)) {
39718ea05e3aSAlexander Block 		ret = -EROFS;
39728ea05e3aSAlexander Block 		goto out;
39738ea05e3aSAlexander Block 	}
39748ea05e3aSAlexander Block 
39758ea05e3aSAlexander Block 	if (!inode_owner_or_capable(inode)) {
39768ea05e3aSAlexander Block 		ret = -EACCES;
39778ea05e3aSAlexander Block 		goto out;
39788ea05e3aSAlexander Block 	}
39798ea05e3aSAlexander Block 
39808ea05e3aSAlexander Block 	sa = memdup_user(arg, sizeof(*sa));
39818ea05e3aSAlexander Block 	if (IS_ERR(sa)) {
39828ea05e3aSAlexander Block 		ret = PTR_ERR(sa);
39838ea05e3aSAlexander Block 		sa = NULL;
39848ea05e3aSAlexander Block 		goto out;
39858ea05e3aSAlexander Block 	}
39868ea05e3aSAlexander Block 
39878ea05e3aSAlexander Block 	trans = btrfs_start_transaction(root, 1);
39888ea05e3aSAlexander Block 	if (IS_ERR(trans)) {
39898ea05e3aSAlexander Block 		ret = PTR_ERR(trans);
39908ea05e3aSAlexander Block 		trans = NULL;
39918ea05e3aSAlexander Block 		goto out;
39928ea05e3aSAlexander Block 	}
39938ea05e3aSAlexander Block 
39948ea05e3aSAlexander Block 	sa->rtransid = trans->transid;
39958ea05e3aSAlexander Block 	sa->rtime.sec = ct.tv_sec;
39968ea05e3aSAlexander Block 	sa->rtime.nsec = ct.tv_nsec;
39978ea05e3aSAlexander Block 
39988ea05e3aSAlexander Block 	memcpy(root_item->received_uuid, sa->uuid, BTRFS_UUID_SIZE);
39998ea05e3aSAlexander Block 	btrfs_set_root_stransid(root_item, sa->stransid);
40008ea05e3aSAlexander Block 	btrfs_set_root_rtransid(root_item, sa->rtransid);
40018ea05e3aSAlexander Block 	root_item->stime.sec = cpu_to_le64(sa->stime.sec);
40028ea05e3aSAlexander Block 	root_item->stime.nsec = cpu_to_le32(sa->stime.nsec);
40038ea05e3aSAlexander Block 	root_item->rtime.sec = cpu_to_le64(sa->rtime.sec);
40048ea05e3aSAlexander Block 	root_item->rtime.nsec = cpu_to_le32(sa->rtime.nsec);
40058ea05e3aSAlexander Block 
40068ea05e3aSAlexander Block 	ret = btrfs_update_root(trans, root->fs_info->tree_root,
40078ea05e3aSAlexander Block 				&root->root_key, &root->root_item);
40088ea05e3aSAlexander Block 	if (ret < 0) {
40098ea05e3aSAlexander Block 		btrfs_end_transaction(trans, root);
40108ea05e3aSAlexander Block 		trans = NULL;
40118ea05e3aSAlexander Block 		goto out;
40128ea05e3aSAlexander Block 	} else {
40138ea05e3aSAlexander Block 		ret = btrfs_commit_transaction(trans, root);
40148ea05e3aSAlexander Block 		if (ret < 0)
40158ea05e3aSAlexander Block 			goto out;
40168ea05e3aSAlexander Block 	}
40178ea05e3aSAlexander Block 
40188ea05e3aSAlexander Block 	ret = copy_to_user(arg, sa, sizeof(*sa));
40198ea05e3aSAlexander Block 	if (ret)
40208ea05e3aSAlexander Block 		ret = -EFAULT;
40218ea05e3aSAlexander Block 
40228ea05e3aSAlexander Block out:
40238ea05e3aSAlexander Block 	kfree(sa);
40248ea05e3aSAlexander Block 	up_write(&root->fs_info->subvol_sem);
40258ea05e3aSAlexander Block 	mnt_drop_write_file(file);
40268ea05e3aSAlexander Block 	return ret;
40278ea05e3aSAlexander Block }
40288ea05e3aSAlexander Block 
4029867ab667Sjeff.liu static int btrfs_ioctl_get_fslabel(struct file *file, void __user *arg)
4030867ab667Sjeff.liu {
4031867ab667Sjeff.liu 	struct btrfs_root *root = BTRFS_I(fdentry(file)->d_inode)->root;
4032867ab667Sjeff.liu 	const char *label = root->fs_info->super_copy->label;
4033867ab667Sjeff.liu 	size_t len = strnlen(label, BTRFS_LABEL_SIZE);
4034867ab667Sjeff.liu 	int ret;
4035867ab667Sjeff.liu 
4036867ab667Sjeff.liu 	if (len == BTRFS_LABEL_SIZE) {
4037867ab667Sjeff.liu 		pr_warn("btrfs: label is too long, return the first %zu bytes\n",
4038867ab667Sjeff.liu 			--len);
4039867ab667Sjeff.liu 	}
4040867ab667Sjeff.liu 
4041867ab667Sjeff.liu 	mutex_lock(&root->fs_info->volume_mutex);
4042867ab667Sjeff.liu 	ret = copy_to_user(arg, label, len);
4043867ab667Sjeff.liu 	mutex_unlock(&root->fs_info->volume_mutex);
4044867ab667Sjeff.liu 
4045867ab667Sjeff.liu 	return ret ? -EFAULT : 0;
4046867ab667Sjeff.liu }
4047867ab667Sjeff.liu 
4048a8bfd4abSjeff.liu static int btrfs_ioctl_set_fslabel(struct file *file, void __user *arg)
4049a8bfd4abSjeff.liu {
4050a8bfd4abSjeff.liu 	struct btrfs_root *root = BTRFS_I(fdentry(file)->d_inode)->root;
4051a8bfd4abSjeff.liu 	struct btrfs_super_block *super_block = root->fs_info->super_copy;
4052a8bfd4abSjeff.liu 	struct btrfs_trans_handle *trans;
4053a8bfd4abSjeff.liu 	char label[BTRFS_LABEL_SIZE];
4054a8bfd4abSjeff.liu 	int ret;
4055a8bfd4abSjeff.liu 
4056a8bfd4abSjeff.liu 	if (!capable(CAP_SYS_ADMIN))
4057a8bfd4abSjeff.liu 		return -EPERM;
4058a8bfd4abSjeff.liu 
4059a8bfd4abSjeff.liu 	if (copy_from_user(label, arg, sizeof(label)))
4060a8bfd4abSjeff.liu 		return -EFAULT;
4061a8bfd4abSjeff.liu 
4062a8bfd4abSjeff.liu 	if (strnlen(label, BTRFS_LABEL_SIZE) == BTRFS_LABEL_SIZE) {
4063a8bfd4abSjeff.liu 		pr_err("btrfs: unable to set label with more than %d bytes\n",
4064a8bfd4abSjeff.liu 		       BTRFS_LABEL_SIZE - 1);
4065a8bfd4abSjeff.liu 		return -EINVAL;
4066a8bfd4abSjeff.liu 	}
4067a8bfd4abSjeff.liu 
4068a8bfd4abSjeff.liu 	ret = mnt_want_write_file(file);
4069a8bfd4abSjeff.liu 	if (ret)
4070a8bfd4abSjeff.liu 		return ret;
4071a8bfd4abSjeff.liu 
4072a8bfd4abSjeff.liu 	mutex_lock(&root->fs_info->volume_mutex);
4073a8bfd4abSjeff.liu 	trans = btrfs_start_transaction(root, 0);
4074a8bfd4abSjeff.liu 	if (IS_ERR(trans)) {
4075a8bfd4abSjeff.liu 		ret = PTR_ERR(trans);
4076a8bfd4abSjeff.liu 		goto out_unlock;
4077a8bfd4abSjeff.liu 	}
4078a8bfd4abSjeff.liu 
4079a8bfd4abSjeff.liu 	strcpy(super_block->label, label);
4080a8bfd4abSjeff.liu 	ret = btrfs_end_transaction(trans, root);
4081a8bfd4abSjeff.liu 
4082a8bfd4abSjeff.liu out_unlock:
4083a8bfd4abSjeff.liu 	mutex_unlock(&root->fs_info->volume_mutex);
4084a8bfd4abSjeff.liu 	mnt_drop_write_file(file);
4085a8bfd4abSjeff.liu 	return ret;
4086a8bfd4abSjeff.liu }
4087a8bfd4abSjeff.liu 
4088f46b5a66SChristoph Hellwig long btrfs_ioctl(struct file *file, unsigned int
4089f46b5a66SChristoph Hellwig 		cmd, unsigned long arg)
4090f46b5a66SChristoph Hellwig {
4091496ad9aaSAl Viro 	struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
40924bcabaa3SChristoph Hellwig 	void __user *argp = (void __user *)arg;
4093f46b5a66SChristoph Hellwig 
4094f46b5a66SChristoph Hellwig 	switch (cmd) {
40956cbff00fSChristoph Hellwig 	case FS_IOC_GETFLAGS:
40966cbff00fSChristoph Hellwig 		return btrfs_ioctl_getflags(file, argp);
40976cbff00fSChristoph Hellwig 	case FS_IOC_SETFLAGS:
40986cbff00fSChristoph Hellwig 		return btrfs_ioctl_setflags(file, argp);
40996cbff00fSChristoph Hellwig 	case FS_IOC_GETVERSION:
41006cbff00fSChristoph Hellwig 		return btrfs_ioctl_getversion(file, argp);
4101f7039b1dSLi Dongyang 	case FITRIM:
4102f7039b1dSLi Dongyang 		return btrfs_ioctl_fitrim(file, argp);
4103f46b5a66SChristoph Hellwig 	case BTRFS_IOC_SNAP_CREATE:
4104fa0d2b9bSLi Zefan 		return btrfs_ioctl_snap_create(file, argp, 0);
4105fdfb1e4fSLi Zefan 	case BTRFS_IOC_SNAP_CREATE_V2:
4106fa0d2b9bSLi Zefan 		return btrfs_ioctl_snap_create_v2(file, argp, 0);
41073de4586cSChris Mason 	case BTRFS_IOC_SUBVOL_CREATE:
4108fa0d2b9bSLi Zefan 		return btrfs_ioctl_snap_create(file, argp, 1);
41096f72c7e2SArne Jansen 	case BTRFS_IOC_SUBVOL_CREATE_V2:
41106f72c7e2SArne Jansen 		return btrfs_ioctl_snap_create_v2(file, argp, 1);
411176dda93cSYan, Zheng 	case BTRFS_IOC_SNAP_DESTROY:
411276dda93cSYan, Zheng 		return btrfs_ioctl_snap_destroy(file, argp);
41130caa102dSLi Zefan 	case BTRFS_IOC_SUBVOL_GETFLAGS:
41140caa102dSLi Zefan 		return btrfs_ioctl_subvol_getflags(file, argp);
41150caa102dSLi Zefan 	case BTRFS_IOC_SUBVOL_SETFLAGS:
41160caa102dSLi Zefan 		return btrfs_ioctl_subvol_setflags(file, argp);
41176ef5ed0dSJosef Bacik 	case BTRFS_IOC_DEFAULT_SUBVOL:
41186ef5ed0dSJosef Bacik 		return btrfs_ioctl_default_subvol(file, argp);
4119f46b5a66SChristoph Hellwig 	case BTRFS_IOC_DEFRAG:
41201e701a32SChris Mason 		return btrfs_ioctl_defrag(file, NULL);
41211e701a32SChris Mason 	case BTRFS_IOC_DEFRAG_RANGE:
41221e701a32SChris Mason 		return btrfs_ioctl_defrag(file, argp);
4123f46b5a66SChristoph Hellwig 	case BTRFS_IOC_RESIZE:
4124198605a8SMiao Xie 		return btrfs_ioctl_resize(file, argp);
4125f46b5a66SChristoph Hellwig 	case BTRFS_IOC_ADD_DEV:
41264bcabaa3SChristoph Hellwig 		return btrfs_ioctl_add_dev(root, argp);
4127f46b5a66SChristoph Hellwig 	case BTRFS_IOC_RM_DEV:
4128da24927bSMiao Xie 		return btrfs_ioctl_rm_dev(file, argp);
4129475f6387SJan Schmidt 	case BTRFS_IOC_FS_INFO:
4130475f6387SJan Schmidt 		return btrfs_ioctl_fs_info(root, argp);
4131475f6387SJan Schmidt 	case BTRFS_IOC_DEV_INFO:
4132475f6387SJan Schmidt 		return btrfs_ioctl_dev_info(root, argp);
4133f46b5a66SChristoph Hellwig 	case BTRFS_IOC_BALANCE:
41349ba1f6e4SLiu Bo 		return btrfs_ioctl_balance(file, NULL);
4135f46b5a66SChristoph Hellwig 	case BTRFS_IOC_CLONE:
4136c5c9cd4dSSage Weil 		return btrfs_ioctl_clone(file, arg, 0, 0, 0);
4137c5c9cd4dSSage Weil 	case BTRFS_IOC_CLONE_RANGE:
41387a865e8aSChristoph Hellwig 		return btrfs_ioctl_clone_range(file, argp);
4139f46b5a66SChristoph Hellwig 	case BTRFS_IOC_TRANS_START:
4140f46b5a66SChristoph Hellwig 		return btrfs_ioctl_trans_start(file);
4141f46b5a66SChristoph Hellwig 	case BTRFS_IOC_TRANS_END:
4142f46b5a66SChristoph Hellwig 		return btrfs_ioctl_trans_end(file);
4143ac8e9819SChris Mason 	case BTRFS_IOC_TREE_SEARCH:
4144ac8e9819SChris Mason 		return btrfs_ioctl_tree_search(file, argp);
4145ac8e9819SChris Mason 	case BTRFS_IOC_INO_LOOKUP:
4146ac8e9819SChris Mason 		return btrfs_ioctl_ino_lookup(file, argp);
4147d7728c96SJan Schmidt 	case BTRFS_IOC_INO_PATHS:
4148d7728c96SJan Schmidt 		return btrfs_ioctl_ino_to_path(root, argp);
4149d7728c96SJan Schmidt 	case BTRFS_IOC_LOGICAL_INO:
4150d7728c96SJan Schmidt 		return btrfs_ioctl_logical_to_ino(root, argp);
41511406e432SJosef Bacik 	case BTRFS_IOC_SPACE_INFO:
41521406e432SJosef Bacik 		return btrfs_ioctl_space_info(root, argp);
4153f46b5a66SChristoph Hellwig 	case BTRFS_IOC_SYNC:
4154f46b5a66SChristoph Hellwig 		btrfs_sync_fs(file->f_dentry->d_sb, 1);
4155f46b5a66SChristoph Hellwig 		return 0;
415646204592SSage Weil 	case BTRFS_IOC_START_SYNC:
41579a8c28beSMiao Xie 		return btrfs_ioctl_start_sync(root, argp);
415846204592SSage Weil 	case BTRFS_IOC_WAIT_SYNC:
41599a8c28beSMiao Xie 		return btrfs_ioctl_wait_sync(root, argp);
4160475f6387SJan Schmidt 	case BTRFS_IOC_SCRUB:
4161b8e95489SMiao Xie 		return btrfs_ioctl_scrub(file, argp);
4162475f6387SJan Schmidt 	case BTRFS_IOC_SCRUB_CANCEL:
4163475f6387SJan Schmidt 		return btrfs_ioctl_scrub_cancel(root, argp);
4164475f6387SJan Schmidt 	case BTRFS_IOC_SCRUB_PROGRESS:
4165475f6387SJan Schmidt 		return btrfs_ioctl_scrub_progress(root, argp);
4166c9e9f97bSIlya Dryomov 	case BTRFS_IOC_BALANCE_V2:
41679ba1f6e4SLiu Bo 		return btrfs_ioctl_balance(file, argp);
4168837d5b6eSIlya Dryomov 	case BTRFS_IOC_BALANCE_CTL:
4169837d5b6eSIlya Dryomov 		return btrfs_ioctl_balance_ctl(root, arg);
417019a39dceSIlya Dryomov 	case BTRFS_IOC_BALANCE_PROGRESS:
417119a39dceSIlya Dryomov 		return btrfs_ioctl_balance_progress(root, argp);
41728ea05e3aSAlexander Block 	case BTRFS_IOC_SET_RECEIVED_SUBVOL:
41738ea05e3aSAlexander Block 		return btrfs_ioctl_set_received_subvol(file, argp);
417431db9f7cSAlexander Block 	case BTRFS_IOC_SEND:
417531db9f7cSAlexander Block 		return btrfs_ioctl_send(file, argp);
4176c11d2c23SStefan Behrens 	case BTRFS_IOC_GET_DEV_STATS:
4177b27f7c0cSDavid Sterba 		return btrfs_ioctl_get_dev_stats(root, argp);
41785d13a37bSArne Jansen 	case BTRFS_IOC_QUOTA_CTL:
4179905b0ddaSMiao Xie 		return btrfs_ioctl_quota_ctl(file, argp);
41805d13a37bSArne Jansen 	case BTRFS_IOC_QGROUP_ASSIGN:
4181905b0ddaSMiao Xie 		return btrfs_ioctl_qgroup_assign(file, argp);
41825d13a37bSArne Jansen 	case BTRFS_IOC_QGROUP_CREATE:
4183905b0ddaSMiao Xie 		return btrfs_ioctl_qgroup_create(file, argp);
41845d13a37bSArne Jansen 	case BTRFS_IOC_QGROUP_LIMIT:
4185905b0ddaSMiao Xie 		return btrfs_ioctl_qgroup_limit(file, argp);
41862f232036SJan Schmidt 	case BTRFS_IOC_QUOTA_RESCAN:
41872f232036SJan Schmidt 		return btrfs_ioctl_quota_rescan(file, argp);
41882f232036SJan Schmidt 	case BTRFS_IOC_QUOTA_RESCAN_STATUS:
41892f232036SJan Schmidt 		return btrfs_ioctl_quota_rescan_status(file, argp);
419057254b6eSJan Schmidt 	case BTRFS_IOC_QUOTA_RESCAN_WAIT:
419157254b6eSJan Schmidt 		return btrfs_ioctl_quota_rescan_wait(file, argp);
41923f6bcfbdSStefan Behrens 	case BTRFS_IOC_DEV_REPLACE:
41933f6bcfbdSStefan Behrens 		return btrfs_ioctl_dev_replace(root, argp);
4194867ab667Sjeff.liu 	case BTRFS_IOC_GET_FSLABEL:
4195867ab667Sjeff.liu 		return btrfs_ioctl_get_fslabel(file, argp);
4196a8bfd4abSjeff.liu 	case BTRFS_IOC_SET_FSLABEL:
4197a8bfd4abSjeff.liu 		return btrfs_ioctl_set_fslabel(file, argp);
4198f46b5a66SChristoph Hellwig 	}
4199f46b5a66SChristoph Hellwig 
4200f46b5a66SChristoph Hellwig 	return -ENOTTY;
4201f46b5a66SChristoph Hellwig }
4202