xref: /openbmc/linux/fs/btrfs/ioctl.c (revision a696cf35)
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>
46416161dbSMark Fasheh #include <linux/uaccess.h>
474b4e25f2SChris Mason #include "compat.h"
48f46b5a66SChristoph Hellwig #include "ctree.h"
49f46b5a66SChristoph Hellwig #include "disk-io.h"
50f46b5a66SChristoph Hellwig #include "transaction.h"
51f46b5a66SChristoph Hellwig #include "btrfs_inode.h"
52f46b5a66SChristoph Hellwig #include "print-tree.h"
53f46b5a66SChristoph Hellwig #include "volumes.h"
54925baeddSChris Mason #include "locking.h"
55581bb050SLi Zefan #include "inode-map.h"
56d7728c96SJan Schmidt #include "backref.h"
57606686eeSJosef Bacik #include "rcu-string.h"
5831db9f7cSAlexander Block #include "send.h"
593f6bcfbdSStefan Behrens #include "dev-replace.h"
60f46b5a66SChristoph Hellwig 
61416161dbSMark Fasheh static int btrfs_clone(struct inode *src, struct inode *inode,
62416161dbSMark Fasheh 		       u64 off, u64 olen, u64 olen_aligned, u64 destoff);
63416161dbSMark Fasheh 
646cbff00fSChristoph Hellwig /* Mask out flags that are inappropriate for the given type of inode. */
656cbff00fSChristoph Hellwig static inline __u32 btrfs_mask_flags(umode_t mode, __u32 flags)
666cbff00fSChristoph Hellwig {
676cbff00fSChristoph Hellwig 	if (S_ISDIR(mode))
686cbff00fSChristoph Hellwig 		return flags;
696cbff00fSChristoph Hellwig 	else if (S_ISREG(mode))
706cbff00fSChristoph Hellwig 		return flags & ~FS_DIRSYNC_FL;
716cbff00fSChristoph Hellwig 	else
726cbff00fSChristoph Hellwig 		return flags & (FS_NODUMP_FL | FS_NOATIME_FL);
736cbff00fSChristoph Hellwig }
74f46b5a66SChristoph Hellwig 
756cbff00fSChristoph Hellwig /*
766cbff00fSChristoph Hellwig  * Export inode flags to the format expected by the FS_IOC_GETFLAGS ioctl.
776cbff00fSChristoph Hellwig  */
786cbff00fSChristoph Hellwig static unsigned int btrfs_flags_to_ioctl(unsigned int flags)
796cbff00fSChristoph Hellwig {
806cbff00fSChristoph Hellwig 	unsigned int iflags = 0;
816cbff00fSChristoph Hellwig 
826cbff00fSChristoph Hellwig 	if (flags & BTRFS_INODE_SYNC)
836cbff00fSChristoph Hellwig 		iflags |= FS_SYNC_FL;
846cbff00fSChristoph Hellwig 	if (flags & BTRFS_INODE_IMMUTABLE)
856cbff00fSChristoph Hellwig 		iflags |= FS_IMMUTABLE_FL;
866cbff00fSChristoph Hellwig 	if (flags & BTRFS_INODE_APPEND)
876cbff00fSChristoph Hellwig 		iflags |= FS_APPEND_FL;
886cbff00fSChristoph Hellwig 	if (flags & BTRFS_INODE_NODUMP)
896cbff00fSChristoph Hellwig 		iflags |= FS_NODUMP_FL;
906cbff00fSChristoph Hellwig 	if (flags & BTRFS_INODE_NOATIME)
916cbff00fSChristoph Hellwig 		iflags |= FS_NOATIME_FL;
926cbff00fSChristoph Hellwig 	if (flags & BTRFS_INODE_DIRSYNC)
936cbff00fSChristoph Hellwig 		iflags |= FS_DIRSYNC_FL;
94d0092bddSLi Zefan 	if (flags & BTRFS_INODE_NODATACOW)
95d0092bddSLi Zefan 		iflags |= FS_NOCOW_FL;
96d0092bddSLi Zefan 
97d0092bddSLi Zefan 	if ((flags & BTRFS_INODE_COMPRESS) && !(flags & BTRFS_INODE_NOCOMPRESS))
98d0092bddSLi Zefan 		iflags |= FS_COMPR_FL;
99d0092bddSLi Zefan 	else if (flags & BTRFS_INODE_NOCOMPRESS)
100d0092bddSLi Zefan 		iflags |= FS_NOCOMP_FL;
1016cbff00fSChristoph Hellwig 
1026cbff00fSChristoph Hellwig 	return iflags;
1036cbff00fSChristoph Hellwig }
1046cbff00fSChristoph Hellwig 
1056cbff00fSChristoph Hellwig /*
1066cbff00fSChristoph Hellwig  * Update inode->i_flags based on the btrfs internal flags.
1076cbff00fSChristoph Hellwig  */
1086cbff00fSChristoph Hellwig void btrfs_update_iflags(struct inode *inode)
1096cbff00fSChristoph Hellwig {
1106cbff00fSChristoph Hellwig 	struct btrfs_inode *ip = BTRFS_I(inode);
1116cbff00fSChristoph Hellwig 
1126cbff00fSChristoph Hellwig 	inode->i_flags &= ~(S_SYNC|S_APPEND|S_IMMUTABLE|S_NOATIME|S_DIRSYNC);
1136cbff00fSChristoph Hellwig 
1146cbff00fSChristoph Hellwig 	if (ip->flags & BTRFS_INODE_SYNC)
1156cbff00fSChristoph Hellwig 		inode->i_flags |= S_SYNC;
1166cbff00fSChristoph Hellwig 	if (ip->flags & BTRFS_INODE_IMMUTABLE)
1176cbff00fSChristoph Hellwig 		inode->i_flags |= S_IMMUTABLE;
1186cbff00fSChristoph Hellwig 	if (ip->flags & BTRFS_INODE_APPEND)
1196cbff00fSChristoph Hellwig 		inode->i_flags |= S_APPEND;
1206cbff00fSChristoph Hellwig 	if (ip->flags & BTRFS_INODE_NOATIME)
1216cbff00fSChristoph Hellwig 		inode->i_flags |= S_NOATIME;
1226cbff00fSChristoph Hellwig 	if (ip->flags & BTRFS_INODE_DIRSYNC)
1236cbff00fSChristoph Hellwig 		inode->i_flags |= S_DIRSYNC;
1246cbff00fSChristoph Hellwig }
1256cbff00fSChristoph Hellwig 
1266cbff00fSChristoph Hellwig /*
1276cbff00fSChristoph Hellwig  * Inherit flags from the parent inode.
1286cbff00fSChristoph Hellwig  *
129e27425d6SJosef Bacik  * Currently only the compression flags and the cow flags are inherited.
1306cbff00fSChristoph Hellwig  */
1316cbff00fSChristoph Hellwig void btrfs_inherit_iflags(struct inode *inode, struct inode *dir)
1326cbff00fSChristoph Hellwig {
1330b4dcea5SChris Mason 	unsigned int flags;
1340b4dcea5SChris Mason 
1350b4dcea5SChris Mason 	if (!dir)
1360b4dcea5SChris Mason 		return;
1370b4dcea5SChris Mason 
1380b4dcea5SChris Mason 	flags = BTRFS_I(dir)->flags;
1396cbff00fSChristoph Hellwig 
140e27425d6SJosef Bacik 	if (flags & BTRFS_INODE_NOCOMPRESS) {
141e27425d6SJosef Bacik 		BTRFS_I(inode)->flags &= ~BTRFS_INODE_COMPRESS;
142e27425d6SJosef Bacik 		BTRFS_I(inode)->flags |= BTRFS_INODE_NOCOMPRESS;
143e27425d6SJosef Bacik 	} else if (flags & BTRFS_INODE_COMPRESS) {
144e27425d6SJosef Bacik 		BTRFS_I(inode)->flags &= ~BTRFS_INODE_NOCOMPRESS;
145e27425d6SJosef Bacik 		BTRFS_I(inode)->flags |= BTRFS_INODE_COMPRESS;
146e27425d6SJosef Bacik 	}
1476cbff00fSChristoph Hellwig 
148213490b3SLiu Bo 	if (flags & BTRFS_INODE_NODATACOW) {
149e27425d6SJosef Bacik 		BTRFS_I(inode)->flags |= BTRFS_INODE_NODATACOW;
150213490b3SLiu Bo 		if (S_ISREG(inode->i_mode))
151213490b3SLiu Bo 			BTRFS_I(inode)->flags |= BTRFS_INODE_NODATASUM;
152213490b3SLiu Bo 	}
153e27425d6SJosef Bacik 
1546cbff00fSChristoph Hellwig 	btrfs_update_iflags(inode);
1556cbff00fSChristoph Hellwig }
1566cbff00fSChristoph Hellwig 
1576cbff00fSChristoph Hellwig static int btrfs_ioctl_getflags(struct file *file, void __user *arg)
1586cbff00fSChristoph Hellwig {
159496ad9aaSAl Viro 	struct btrfs_inode *ip = BTRFS_I(file_inode(file));
1606cbff00fSChristoph Hellwig 	unsigned int flags = btrfs_flags_to_ioctl(ip->flags);
1616cbff00fSChristoph Hellwig 
1626cbff00fSChristoph Hellwig 	if (copy_to_user(arg, &flags, sizeof(flags)))
1636cbff00fSChristoph Hellwig 		return -EFAULT;
1646cbff00fSChristoph Hellwig 	return 0;
1656cbff00fSChristoph Hellwig }
1666cbff00fSChristoph Hellwig 
16775e7cb7fSLiu Bo static int check_flags(unsigned int flags)
16875e7cb7fSLiu Bo {
16975e7cb7fSLiu Bo 	if (flags & ~(FS_IMMUTABLE_FL | FS_APPEND_FL | \
17075e7cb7fSLiu Bo 		      FS_NOATIME_FL | FS_NODUMP_FL | \
17175e7cb7fSLiu Bo 		      FS_SYNC_FL | FS_DIRSYNC_FL | \
172e1e8fb6aSLi Zefan 		      FS_NOCOMP_FL | FS_COMPR_FL |
173e1e8fb6aSLi Zefan 		      FS_NOCOW_FL))
17475e7cb7fSLiu Bo 		return -EOPNOTSUPP;
17575e7cb7fSLiu Bo 
17675e7cb7fSLiu Bo 	if ((flags & FS_NOCOMP_FL) && (flags & FS_COMPR_FL))
17775e7cb7fSLiu Bo 		return -EINVAL;
17875e7cb7fSLiu Bo 
17975e7cb7fSLiu Bo 	return 0;
18075e7cb7fSLiu Bo }
18175e7cb7fSLiu Bo 
1826cbff00fSChristoph Hellwig static int btrfs_ioctl_setflags(struct file *file, void __user *arg)
1836cbff00fSChristoph Hellwig {
184496ad9aaSAl Viro 	struct inode *inode = file_inode(file);
1856cbff00fSChristoph Hellwig 	struct btrfs_inode *ip = BTRFS_I(inode);
1866cbff00fSChristoph Hellwig 	struct btrfs_root *root = ip->root;
1876cbff00fSChristoph Hellwig 	struct btrfs_trans_handle *trans;
1886cbff00fSChristoph Hellwig 	unsigned int flags, oldflags;
1896cbff00fSChristoph Hellwig 	int ret;
190f062abf0SLi Zefan 	u64 ip_oldflags;
191f062abf0SLi Zefan 	unsigned int i_oldflags;
1927e97b8daSDavid Sterba 	umode_t mode;
1936cbff00fSChristoph Hellwig 
194b83cc969SLi Zefan 	if (btrfs_root_readonly(root))
195b83cc969SLi Zefan 		return -EROFS;
196b83cc969SLi Zefan 
1976cbff00fSChristoph Hellwig 	if (copy_from_user(&flags, arg, sizeof(flags)))
1986cbff00fSChristoph Hellwig 		return -EFAULT;
1996cbff00fSChristoph Hellwig 
20075e7cb7fSLiu Bo 	ret = check_flags(flags);
20175e7cb7fSLiu Bo 	if (ret)
20275e7cb7fSLiu Bo 		return ret;
2036cbff00fSChristoph Hellwig 
2042e149670SSerge E. Hallyn 	if (!inode_owner_or_capable(inode))
2056cbff00fSChristoph Hellwig 		return -EACCES;
2066cbff00fSChristoph Hellwig 
207e7848683SJan Kara 	ret = mnt_want_write_file(file);
208e7848683SJan Kara 	if (ret)
209e7848683SJan Kara 		return ret;
210e7848683SJan Kara 
2116cbff00fSChristoph Hellwig 	mutex_lock(&inode->i_mutex);
2126cbff00fSChristoph Hellwig 
213f062abf0SLi Zefan 	ip_oldflags = ip->flags;
214f062abf0SLi Zefan 	i_oldflags = inode->i_flags;
2157e97b8daSDavid Sterba 	mode = inode->i_mode;
216f062abf0SLi Zefan 
2176cbff00fSChristoph Hellwig 	flags = btrfs_mask_flags(inode->i_mode, flags);
2186cbff00fSChristoph Hellwig 	oldflags = btrfs_flags_to_ioctl(ip->flags);
2196cbff00fSChristoph Hellwig 	if ((flags ^ oldflags) & (FS_APPEND_FL | FS_IMMUTABLE_FL)) {
2206cbff00fSChristoph Hellwig 		if (!capable(CAP_LINUX_IMMUTABLE)) {
2216cbff00fSChristoph Hellwig 			ret = -EPERM;
2226cbff00fSChristoph Hellwig 			goto out_unlock;
2236cbff00fSChristoph Hellwig 		}
2246cbff00fSChristoph Hellwig 	}
2256cbff00fSChristoph Hellwig 
2266cbff00fSChristoph Hellwig 	if (flags & FS_SYNC_FL)
2276cbff00fSChristoph Hellwig 		ip->flags |= BTRFS_INODE_SYNC;
2286cbff00fSChristoph Hellwig 	else
2296cbff00fSChristoph Hellwig 		ip->flags &= ~BTRFS_INODE_SYNC;
2306cbff00fSChristoph Hellwig 	if (flags & FS_IMMUTABLE_FL)
2316cbff00fSChristoph Hellwig 		ip->flags |= BTRFS_INODE_IMMUTABLE;
2326cbff00fSChristoph Hellwig 	else
2336cbff00fSChristoph Hellwig 		ip->flags &= ~BTRFS_INODE_IMMUTABLE;
2346cbff00fSChristoph Hellwig 	if (flags & FS_APPEND_FL)
2356cbff00fSChristoph Hellwig 		ip->flags |= BTRFS_INODE_APPEND;
2366cbff00fSChristoph Hellwig 	else
2376cbff00fSChristoph Hellwig 		ip->flags &= ~BTRFS_INODE_APPEND;
2386cbff00fSChristoph Hellwig 	if (flags & FS_NODUMP_FL)
2396cbff00fSChristoph Hellwig 		ip->flags |= BTRFS_INODE_NODUMP;
2406cbff00fSChristoph Hellwig 	else
2416cbff00fSChristoph Hellwig 		ip->flags &= ~BTRFS_INODE_NODUMP;
2426cbff00fSChristoph Hellwig 	if (flags & FS_NOATIME_FL)
2436cbff00fSChristoph Hellwig 		ip->flags |= BTRFS_INODE_NOATIME;
2446cbff00fSChristoph Hellwig 	else
2456cbff00fSChristoph Hellwig 		ip->flags &= ~BTRFS_INODE_NOATIME;
2466cbff00fSChristoph Hellwig 	if (flags & FS_DIRSYNC_FL)
2476cbff00fSChristoph Hellwig 		ip->flags |= BTRFS_INODE_DIRSYNC;
2486cbff00fSChristoph Hellwig 	else
2496cbff00fSChristoph Hellwig 		ip->flags &= ~BTRFS_INODE_DIRSYNC;
2507e97b8daSDavid Sterba 	if (flags & FS_NOCOW_FL) {
2517e97b8daSDavid Sterba 		if (S_ISREG(mode)) {
2527e97b8daSDavid Sterba 			/*
2537e97b8daSDavid Sterba 			 * It's safe to turn csums off here, no extents exist.
2547e97b8daSDavid Sterba 			 * Otherwise we want the flag to reflect the real COW
2557e97b8daSDavid Sterba 			 * status of the file and will not set it.
2567e97b8daSDavid Sterba 			 */
2577e97b8daSDavid Sterba 			if (inode->i_size == 0)
2587e97b8daSDavid Sterba 				ip->flags |= BTRFS_INODE_NODATACOW
2597e97b8daSDavid Sterba 					   | BTRFS_INODE_NODATASUM;
2607e97b8daSDavid Sterba 		} else {
261e1e8fb6aSLi Zefan 			ip->flags |= BTRFS_INODE_NODATACOW;
2627e97b8daSDavid Sterba 		}
2637e97b8daSDavid Sterba 	} else {
2647e97b8daSDavid Sterba 		/*
2657e97b8daSDavid Sterba 		 * Revert back under same assuptions as above
2667e97b8daSDavid Sterba 		 */
2677e97b8daSDavid Sterba 		if (S_ISREG(mode)) {
2687e97b8daSDavid Sterba 			if (inode->i_size == 0)
2697e97b8daSDavid Sterba 				ip->flags &= ~(BTRFS_INODE_NODATACOW
2707e97b8daSDavid Sterba 				             | BTRFS_INODE_NODATASUM);
2717e97b8daSDavid Sterba 		} else {
272e1e8fb6aSLi Zefan 			ip->flags &= ~BTRFS_INODE_NODATACOW;
2737e97b8daSDavid Sterba 		}
2747e97b8daSDavid Sterba 	}
2756cbff00fSChristoph Hellwig 
27675e7cb7fSLiu Bo 	/*
27775e7cb7fSLiu Bo 	 * The COMPRESS flag can only be changed by users, while the NOCOMPRESS
27875e7cb7fSLiu Bo 	 * flag may be changed automatically if compression code won't make
27975e7cb7fSLiu Bo 	 * things smaller.
28075e7cb7fSLiu Bo 	 */
28175e7cb7fSLiu Bo 	if (flags & FS_NOCOMP_FL) {
28275e7cb7fSLiu Bo 		ip->flags &= ~BTRFS_INODE_COMPRESS;
28375e7cb7fSLiu Bo 		ip->flags |= BTRFS_INODE_NOCOMPRESS;
28475e7cb7fSLiu Bo 	} else if (flags & FS_COMPR_FL) {
28575e7cb7fSLiu Bo 		ip->flags |= BTRFS_INODE_COMPRESS;
28675e7cb7fSLiu Bo 		ip->flags &= ~BTRFS_INODE_NOCOMPRESS;
287ebcb904dSLi Zefan 	} else {
288ebcb904dSLi Zefan 		ip->flags &= ~(BTRFS_INODE_COMPRESS | BTRFS_INODE_NOCOMPRESS);
28975e7cb7fSLiu Bo 	}
2906cbff00fSChristoph Hellwig 
2914da6f1a3SLi Zefan 	trans = btrfs_start_transaction(root, 1);
292f062abf0SLi Zefan 	if (IS_ERR(trans)) {
293f062abf0SLi Zefan 		ret = PTR_ERR(trans);
294f062abf0SLi Zefan 		goto out_drop;
295f062abf0SLi Zefan 	}
2966cbff00fSChristoph Hellwig 
297306424ccSLi Zefan 	btrfs_update_iflags(inode);
2980c4d2d95SJosef Bacik 	inode_inc_iversion(inode);
299306424ccSLi Zefan 	inode->i_ctime = CURRENT_TIME;
3006cbff00fSChristoph Hellwig 	ret = btrfs_update_inode(trans, root, inode);
3016cbff00fSChristoph Hellwig 
3026cbff00fSChristoph Hellwig 	btrfs_end_transaction(trans, root);
303f062abf0SLi Zefan  out_drop:
304f062abf0SLi Zefan 	if (ret) {
305f062abf0SLi Zefan 		ip->flags = ip_oldflags;
306f062abf0SLi Zefan 		inode->i_flags = i_oldflags;
307f062abf0SLi Zefan 	}
3086cbff00fSChristoph Hellwig 
3096cbff00fSChristoph Hellwig  out_unlock:
3106cbff00fSChristoph Hellwig 	mutex_unlock(&inode->i_mutex);
311e7848683SJan Kara 	mnt_drop_write_file(file);
3122d4e6f6aSliubo 	return ret;
3136cbff00fSChristoph Hellwig }
3146cbff00fSChristoph Hellwig 
3156cbff00fSChristoph Hellwig static int btrfs_ioctl_getversion(struct file *file, int __user *arg)
3166cbff00fSChristoph Hellwig {
317496ad9aaSAl Viro 	struct inode *inode = file_inode(file);
3186cbff00fSChristoph Hellwig 
3196cbff00fSChristoph Hellwig 	return put_user(inode->i_generation, arg);
3206cbff00fSChristoph Hellwig }
321f46b5a66SChristoph Hellwig 
322f7039b1dSLi Dongyang static noinline int btrfs_ioctl_fitrim(struct file *file, void __user *arg)
323f7039b1dSLi Dongyang {
324815745cfSAl Viro 	struct btrfs_fs_info *fs_info = btrfs_sb(fdentry(file)->d_sb);
325f7039b1dSLi Dongyang 	struct btrfs_device *device;
326f7039b1dSLi Dongyang 	struct request_queue *q;
327f7039b1dSLi Dongyang 	struct fstrim_range range;
328f7039b1dSLi Dongyang 	u64 minlen = ULLONG_MAX;
329f7039b1dSLi Dongyang 	u64 num_devices = 0;
330815745cfSAl Viro 	u64 total_bytes = btrfs_super_total_bytes(fs_info->super_copy);
331f7039b1dSLi Dongyang 	int ret;
332f7039b1dSLi Dongyang 
333f7039b1dSLi Dongyang 	if (!capable(CAP_SYS_ADMIN))
334f7039b1dSLi Dongyang 		return -EPERM;
335f7039b1dSLi Dongyang 
3361f78160cSXiao Guangrong 	rcu_read_lock();
3371f78160cSXiao Guangrong 	list_for_each_entry_rcu(device, &fs_info->fs_devices->devices,
3381f78160cSXiao Guangrong 				dev_list) {
339f7039b1dSLi Dongyang 		if (!device->bdev)
340f7039b1dSLi Dongyang 			continue;
341f7039b1dSLi Dongyang 		q = bdev_get_queue(device->bdev);
342f7039b1dSLi Dongyang 		if (blk_queue_discard(q)) {
343f7039b1dSLi Dongyang 			num_devices++;
344f7039b1dSLi Dongyang 			minlen = min((u64)q->limits.discard_granularity,
345f7039b1dSLi Dongyang 				     minlen);
346f7039b1dSLi Dongyang 		}
347f7039b1dSLi Dongyang 	}
3481f78160cSXiao Guangrong 	rcu_read_unlock();
349f4c697e6SLukas Czerner 
350f7039b1dSLi Dongyang 	if (!num_devices)
351f7039b1dSLi Dongyang 		return -EOPNOTSUPP;
352f7039b1dSLi Dongyang 	if (copy_from_user(&range, arg, sizeof(range)))
353f7039b1dSLi Dongyang 		return -EFAULT;
354e515c18bSLukas Czerner 	if (range.start > total_bytes ||
355e515c18bSLukas Czerner 	    range.len < fs_info->sb->s_blocksize)
356f4c697e6SLukas Czerner 		return -EINVAL;
357f7039b1dSLi Dongyang 
358f4c697e6SLukas Czerner 	range.len = min(range.len, total_bytes - range.start);
359f7039b1dSLi Dongyang 	range.minlen = max(range.minlen, minlen);
360815745cfSAl Viro 	ret = btrfs_trim_fs(fs_info->tree_root, &range);
361f7039b1dSLi Dongyang 	if (ret < 0)
362f7039b1dSLi Dongyang 		return ret;
363f7039b1dSLi Dongyang 
364f7039b1dSLi Dongyang 	if (copy_to_user(arg, &range, sizeof(range)))
365f7039b1dSLi Dongyang 		return -EFAULT;
366f7039b1dSLi Dongyang 
367f7039b1dSLi Dongyang 	return 0;
368f7039b1dSLi Dongyang }
369f7039b1dSLi Dongyang 
370d5c12070SMiao Xie static noinline int create_subvol(struct inode *dir,
371cb8e7090SChristoph Hellwig 				  struct dentry *dentry,
37272fd032eSSage Weil 				  char *name, int namelen,
3736f72c7e2SArne Jansen 				  u64 *async_transid,
3748696c533SMiao Xie 				  struct btrfs_qgroup_inherit *inherit)
375f46b5a66SChristoph Hellwig {
376f46b5a66SChristoph Hellwig 	struct btrfs_trans_handle *trans;
377f46b5a66SChristoph Hellwig 	struct btrfs_key key;
378f46b5a66SChristoph Hellwig 	struct btrfs_root_item root_item;
379f46b5a66SChristoph Hellwig 	struct btrfs_inode_item *inode_item;
380f46b5a66SChristoph Hellwig 	struct extent_buffer *leaf;
381d5c12070SMiao Xie 	struct btrfs_root *root = BTRFS_I(dir)->root;
38276dda93cSYan, Zheng 	struct btrfs_root *new_root;
383d5c12070SMiao Xie 	struct btrfs_block_rsv block_rsv;
3848ea05e3aSAlexander Block 	struct timespec cur_time = CURRENT_TIME;
385f46b5a66SChristoph Hellwig 	int ret;
386f46b5a66SChristoph Hellwig 	int err;
387f46b5a66SChristoph Hellwig 	u64 objectid;
388f46b5a66SChristoph Hellwig 	u64 new_dirid = BTRFS_FIRST_FREE_OBJECTID;
3893de4586cSChris Mason 	u64 index = 0;
390d5c12070SMiao Xie 	u64 qgroup_reserved;
3918ea05e3aSAlexander Block 	uuid_le new_uuid;
392f46b5a66SChristoph Hellwig 
393581bb050SLi Zefan 	ret = btrfs_find_free_objectid(root->fs_info->tree_root, &objectid);
3942fbe8c8aSAl Viro 	if (ret)
395a22285a6SYan, Zheng 		return ret;
3966a912213SJosef Bacik 
397d5c12070SMiao Xie 	btrfs_init_block_rsv(&block_rsv, BTRFS_BLOCK_RSV_TEMP);
3989ed74f2dSJosef Bacik 	/*
399d5c12070SMiao Xie 	 * The same as the snapshot creation, please see the comment
400d5c12070SMiao Xie 	 * of create_snapshot().
4019ed74f2dSJosef Bacik 	 */
402d5c12070SMiao Xie 	ret = btrfs_subvolume_reserve_metadata(root, &block_rsv,
403ee3441b4SJeff Mahoney 					       7, &qgroup_reserved, false);
404d5c12070SMiao Xie 	if (ret)
405d5c12070SMiao Xie 		return ret;
406f46b5a66SChristoph Hellwig 
407d5c12070SMiao Xie 	trans = btrfs_start_transaction(root, 0);
408d5c12070SMiao Xie 	if (IS_ERR(trans)) {
409d5c12070SMiao Xie 		ret = PTR_ERR(trans);
410d5c12070SMiao Xie 		goto out;
411d5c12070SMiao Xie 	}
412d5c12070SMiao Xie 	trans->block_rsv = &block_rsv;
413d5c12070SMiao Xie 	trans->bytes_reserved = block_rsv.size;
414f46b5a66SChristoph Hellwig 
4158696c533SMiao Xie 	ret = btrfs_qgroup_inherit(trans, root->fs_info, 0, objectid, inherit);
4166f72c7e2SArne Jansen 	if (ret)
4176f72c7e2SArne Jansen 		goto fail;
4186f72c7e2SArne Jansen 
4195d4f98a2SYan Zheng 	leaf = btrfs_alloc_free_block(trans, root, root->leafsize,
4205581a51aSJan Schmidt 				      0, objectid, NULL, 0, 0, 0);
4218e8a1e31SJosef Bacik 	if (IS_ERR(leaf)) {
4228e8a1e31SJosef Bacik 		ret = PTR_ERR(leaf);
4238e8a1e31SJosef Bacik 		goto fail;
4248e8a1e31SJosef Bacik 	}
425f46b5a66SChristoph Hellwig 
4265d4f98a2SYan Zheng 	memset_extent_buffer(leaf, 0, 0, sizeof(struct btrfs_header));
427f46b5a66SChristoph Hellwig 	btrfs_set_header_bytenr(leaf, leaf->start);
428f46b5a66SChristoph Hellwig 	btrfs_set_header_generation(leaf, trans->transid);
4295d4f98a2SYan Zheng 	btrfs_set_header_backref_rev(leaf, BTRFS_MIXED_BACKREF_REV);
430f46b5a66SChristoph Hellwig 	btrfs_set_header_owner(leaf, objectid);
431f46b5a66SChristoph Hellwig 
432f46b5a66SChristoph Hellwig 	write_extent_buffer(leaf, root->fs_info->fsid,
433f46b5a66SChristoph Hellwig 			    (unsigned long)btrfs_header_fsid(leaf),
434f46b5a66SChristoph Hellwig 			    BTRFS_FSID_SIZE);
4355d4f98a2SYan Zheng 	write_extent_buffer(leaf, root->fs_info->chunk_tree_uuid,
4365d4f98a2SYan Zheng 			    (unsigned long)btrfs_header_chunk_tree_uuid(leaf),
4375d4f98a2SYan Zheng 			    BTRFS_UUID_SIZE);
438f46b5a66SChristoph Hellwig 	btrfs_mark_buffer_dirty(leaf);
439f46b5a66SChristoph Hellwig 
4408ea05e3aSAlexander Block 	memset(&root_item, 0, sizeof(root_item));
4418ea05e3aSAlexander Block 
442f46b5a66SChristoph Hellwig 	inode_item = &root_item.inode;
4433cae210fSQu Wenruo 	btrfs_set_stack_inode_generation(inode_item, 1);
4443cae210fSQu Wenruo 	btrfs_set_stack_inode_size(inode_item, 3);
4453cae210fSQu Wenruo 	btrfs_set_stack_inode_nlink(inode_item, 1);
4463cae210fSQu Wenruo 	btrfs_set_stack_inode_nbytes(inode_item, root->leafsize);
4473cae210fSQu Wenruo 	btrfs_set_stack_inode_mode(inode_item, S_IFDIR | 0755);
448f46b5a66SChristoph Hellwig 
4493cae210fSQu Wenruo 	btrfs_set_root_flags(&root_item, 0);
4503cae210fSQu Wenruo 	btrfs_set_root_limit(&root_item, 0);
4513cae210fSQu Wenruo 	btrfs_set_stack_inode_flags(inode_item, BTRFS_INODE_ROOT_ITEM_INIT);
45208fe4db1SLi Zefan 
453f46b5a66SChristoph Hellwig 	btrfs_set_root_bytenr(&root_item, leaf->start);
45484234f3aSYan Zheng 	btrfs_set_root_generation(&root_item, trans->transid);
455f46b5a66SChristoph Hellwig 	btrfs_set_root_level(&root_item, 0);
456f46b5a66SChristoph Hellwig 	btrfs_set_root_refs(&root_item, 1);
45786b9f2ecSYan, Zheng 	btrfs_set_root_used(&root_item, leaf->len);
45880ff3856SYan Zheng 	btrfs_set_root_last_snapshot(&root_item, 0);
459f46b5a66SChristoph Hellwig 
4608ea05e3aSAlexander Block 	btrfs_set_root_generation_v2(&root_item,
4618ea05e3aSAlexander Block 			btrfs_root_generation(&root_item));
4628ea05e3aSAlexander Block 	uuid_le_gen(&new_uuid);
4638ea05e3aSAlexander Block 	memcpy(root_item.uuid, new_uuid.b, BTRFS_UUID_SIZE);
4643cae210fSQu Wenruo 	btrfs_set_stack_timespec_sec(&root_item.otime, cur_time.tv_sec);
4653cae210fSQu Wenruo 	btrfs_set_stack_timespec_nsec(&root_item.otime, cur_time.tv_nsec);
4668ea05e3aSAlexander Block 	root_item.ctime = root_item.otime;
4678ea05e3aSAlexander Block 	btrfs_set_root_ctransid(&root_item, trans->transid);
4688ea05e3aSAlexander Block 	btrfs_set_root_otransid(&root_item, trans->transid);
469f46b5a66SChristoph Hellwig 
470925baeddSChris Mason 	btrfs_tree_unlock(leaf);
471f46b5a66SChristoph Hellwig 	free_extent_buffer(leaf);
472f46b5a66SChristoph Hellwig 	leaf = NULL;
473f46b5a66SChristoph Hellwig 
474f46b5a66SChristoph Hellwig 	btrfs_set_root_dirid(&root_item, new_dirid);
475f46b5a66SChristoph Hellwig 
476f46b5a66SChristoph Hellwig 	key.objectid = objectid;
4775d4f98a2SYan Zheng 	key.offset = 0;
478f46b5a66SChristoph Hellwig 	btrfs_set_key_type(&key, BTRFS_ROOT_ITEM_KEY);
479f46b5a66SChristoph Hellwig 	ret = btrfs_insert_root(trans, root->fs_info->tree_root, &key,
480f46b5a66SChristoph Hellwig 				&root_item);
481f46b5a66SChristoph Hellwig 	if (ret)
482f46b5a66SChristoph Hellwig 		goto fail;
483f46b5a66SChristoph Hellwig 
48476dda93cSYan, Zheng 	key.offset = (u64)-1;
48576dda93cSYan, Zheng 	new_root = btrfs_read_fs_root_no_name(root->fs_info, &key);
48679787eaaSJeff Mahoney 	if (IS_ERR(new_root)) {
48779787eaaSJeff Mahoney 		btrfs_abort_transaction(trans, root, PTR_ERR(new_root));
48879787eaaSJeff Mahoney 		ret = PTR_ERR(new_root);
48979787eaaSJeff Mahoney 		goto fail;
49079787eaaSJeff Mahoney 	}
49176dda93cSYan, Zheng 
49276dda93cSYan, Zheng 	btrfs_record_root_in_trans(trans, new_root);
49376dda93cSYan, Zheng 
494d82a6f1dSJosef Bacik 	ret = btrfs_create_subvol_root(trans, new_root, new_dirid);
495ce598979SMark Fasheh 	if (ret) {
496ce598979SMark Fasheh 		/* We potentially lose an unused inode item here */
49779787eaaSJeff Mahoney 		btrfs_abort_transaction(trans, root, ret);
498ce598979SMark Fasheh 		goto fail;
499ce598979SMark Fasheh 	}
500ce598979SMark Fasheh 
501f46b5a66SChristoph Hellwig 	/*
502f46b5a66SChristoph Hellwig 	 * insert the directory item
503f46b5a66SChristoph Hellwig 	 */
5043de4586cSChris Mason 	ret = btrfs_set_inode_index(dir, &index);
50579787eaaSJeff Mahoney 	if (ret) {
50679787eaaSJeff Mahoney 		btrfs_abort_transaction(trans, root, ret);
50779787eaaSJeff Mahoney 		goto fail;
50879787eaaSJeff Mahoney 	}
5093de4586cSChris Mason 
5103de4586cSChris Mason 	ret = btrfs_insert_dir_item(trans, root,
51116cdcec7SMiao Xie 				    name, namelen, dir, &key,
5123de4586cSChris Mason 				    BTRFS_FT_DIR, index);
51379787eaaSJeff Mahoney 	if (ret) {
51479787eaaSJeff Mahoney 		btrfs_abort_transaction(trans, root, ret);
515f46b5a66SChristoph Hellwig 		goto fail;
51679787eaaSJeff Mahoney 	}
5170660b5afSChris Mason 
51852c26179SYan Zheng 	btrfs_i_size_write(dir, dir->i_size + namelen * 2);
51952c26179SYan Zheng 	ret = btrfs_update_inode(trans, root, dir);
52052c26179SYan Zheng 	BUG_ON(ret);
52152c26179SYan Zheng 
5220660b5afSChris Mason 	ret = btrfs_add_root_ref(trans, root->fs_info->tree_root,
5234df27c4dSYan, Zheng 				 objectid, root->root_key.objectid,
52433345d01SLi Zefan 				 btrfs_ino(dir), index, name, namelen);
52576dda93cSYan, Zheng 
5260660b5afSChris Mason 	BUG_ON(ret);
5270660b5afSChris Mason 
528f46b5a66SChristoph Hellwig fail:
529d5c12070SMiao Xie 	trans->block_rsv = NULL;
530d5c12070SMiao Xie 	trans->bytes_reserved = 0;
53172fd032eSSage Weil 	if (async_transid) {
53272fd032eSSage Weil 		*async_transid = trans->transid;
53372fd032eSSage Weil 		err = btrfs_commit_transaction_async(trans, root, 1);
53400d71c9cSMiao Xie 		if (err)
53500d71c9cSMiao Xie 			err = btrfs_commit_transaction(trans, root);
53672fd032eSSage Weil 	} else {
53776dda93cSYan, Zheng 		err = btrfs_commit_transaction(trans, root);
53872fd032eSSage Weil 	}
539f46b5a66SChristoph Hellwig 	if (err && !ret)
540f46b5a66SChristoph Hellwig 		ret = err;
5411a65e24bSChris Mason 
5421a65e24bSChris Mason 	if (!ret)
5431a65e24bSChris Mason 		d_instantiate(dentry, btrfs_lookup_dentry(dir, dentry));
544d5c12070SMiao Xie out:
545d5c12070SMiao Xie 	btrfs_subvolume_release_metadata(root, &block_rsv, qgroup_reserved);
546f46b5a66SChristoph Hellwig 	return ret;
547f46b5a66SChristoph Hellwig }
548f46b5a66SChristoph Hellwig 
549e9662f70SMiao Xie static int create_snapshot(struct btrfs_root *root, struct inode *dir,
550e9662f70SMiao Xie 			   struct dentry *dentry, char *name, int namelen,
551e9662f70SMiao Xie 			   u64 *async_transid, bool readonly,
552e9662f70SMiao Xie 			   struct btrfs_qgroup_inherit *inherit)
553f46b5a66SChristoph Hellwig {
5542e4bfab9SYan, Zheng 	struct inode *inode;
555f46b5a66SChristoph Hellwig 	struct btrfs_pending_snapshot *pending_snapshot;
556f46b5a66SChristoph Hellwig 	struct btrfs_trans_handle *trans;
5572e4bfab9SYan, Zheng 	int ret;
558f46b5a66SChristoph Hellwig 
559f46b5a66SChristoph Hellwig 	if (!root->ref_cows)
560f46b5a66SChristoph Hellwig 		return -EINVAL;
561f46b5a66SChristoph Hellwig 
5626a03843dSMiao Xie 	ret = btrfs_start_delalloc_inodes(root, 0);
5636a03843dSMiao Xie 	if (ret)
5646a03843dSMiao Xie 		return ret;
5656a03843dSMiao Xie 
5666a03843dSMiao Xie 	btrfs_wait_ordered_extents(root, 0);
5676a03843dSMiao Xie 
568a22285a6SYan, Zheng 	pending_snapshot = kzalloc(sizeof(*pending_snapshot), GFP_NOFS);
569a22285a6SYan, Zheng 	if (!pending_snapshot)
570a22285a6SYan, Zheng 		return -ENOMEM;
571a22285a6SYan, Zheng 
57266d8f3ddSMiao Xie 	btrfs_init_block_rsv(&pending_snapshot->block_rsv,
57366d8f3ddSMiao Xie 			     BTRFS_BLOCK_RSV_TEMP);
574d5c12070SMiao Xie 	/*
575d5c12070SMiao Xie 	 * 1 - parent dir inode
576d5c12070SMiao Xie 	 * 2 - dir entries
577d5c12070SMiao Xie 	 * 1 - root item
578d5c12070SMiao Xie 	 * 2 - root ref/backref
579d5c12070SMiao Xie 	 * 1 - root of snapshot
580d5c12070SMiao Xie 	 */
581d5c12070SMiao Xie 	ret = btrfs_subvolume_reserve_metadata(BTRFS_I(dir)->root,
582d5c12070SMiao Xie 					&pending_snapshot->block_rsv, 7,
583ee3441b4SJeff Mahoney 					&pending_snapshot->qgroup_reserved,
584ee3441b4SJeff Mahoney 					false);
585d5c12070SMiao Xie 	if (ret)
586d5c12070SMiao Xie 		goto out;
587d5c12070SMiao Xie 
588a22285a6SYan, Zheng 	pending_snapshot->dentry = dentry;
589a22285a6SYan, Zheng 	pending_snapshot->root = root;
590b83cc969SLi Zefan 	pending_snapshot->readonly = readonly;
591e9662f70SMiao Xie 	pending_snapshot->dir = dir;
5928696c533SMiao Xie 	pending_snapshot->inherit = inherit;
593a22285a6SYan, Zheng 
594d5c12070SMiao Xie 	trans = btrfs_start_transaction(root, 0);
595a22285a6SYan, Zheng 	if (IS_ERR(trans)) {
596a22285a6SYan, Zheng 		ret = PTR_ERR(trans);
597a22285a6SYan, Zheng 		goto fail;
598a22285a6SYan, Zheng 	}
599a22285a6SYan, Zheng 
6008351583eSJosef Bacik 	spin_lock(&root->fs_info->trans_lock);
601a22285a6SYan, Zheng 	list_add(&pending_snapshot->list,
602a22285a6SYan, Zheng 		 &trans->transaction->pending_snapshots);
6038351583eSJosef Bacik 	spin_unlock(&root->fs_info->trans_lock);
60472fd032eSSage Weil 	if (async_transid) {
60572fd032eSSage Weil 		*async_transid = trans->transid;
60672fd032eSSage Weil 		ret = btrfs_commit_transaction_async(trans,
60772fd032eSSage Weil 				     root->fs_info->extent_root, 1);
60800d71c9cSMiao Xie 		if (ret)
60900d71c9cSMiao Xie 			ret = btrfs_commit_transaction(trans, root);
61072fd032eSSage Weil 	} else {
61172fd032eSSage Weil 		ret = btrfs_commit_transaction(trans,
61272fd032eSSage Weil 					       root->fs_info->extent_root);
61372fd032eSSage Weil 	}
614aec8030aSMiao Xie 	if (ret)
615c37b2b62SJosef Bacik 		goto fail;
616a22285a6SYan, Zheng 
617a22285a6SYan, Zheng 	ret = pending_snapshot->error;
618f46b5a66SChristoph Hellwig 	if (ret)
6192e4bfab9SYan, Zheng 		goto fail;
620f46b5a66SChristoph Hellwig 
62166b4ffd1SJosef Bacik 	ret = btrfs_orphan_cleanup(pending_snapshot->snap);
62266b4ffd1SJosef Bacik 	if (ret)
62366b4ffd1SJosef Bacik 		goto fail;
624f46b5a66SChristoph Hellwig 
6252fbe8c8aSAl Viro 	inode = btrfs_lookup_dentry(dentry->d_parent->d_inode, dentry);
6262e4bfab9SYan, Zheng 	if (IS_ERR(inode)) {
6272e4bfab9SYan, Zheng 		ret = PTR_ERR(inode);
6282e4bfab9SYan, Zheng 		goto fail;
6292e4bfab9SYan, Zheng 	}
6302e4bfab9SYan, Zheng 	BUG_ON(!inode);
6312e4bfab9SYan, Zheng 	d_instantiate(dentry, inode);
6322e4bfab9SYan, Zheng 	ret = 0;
6332e4bfab9SYan, Zheng fail:
634d5c12070SMiao Xie 	btrfs_subvolume_release_metadata(BTRFS_I(dir)->root,
635d5c12070SMiao Xie 					 &pending_snapshot->block_rsv,
636d5c12070SMiao Xie 					 pending_snapshot->qgroup_reserved);
637d5c12070SMiao Xie out:
638a22285a6SYan, Zheng 	kfree(pending_snapshot);
639f46b5a66SChristoph Hellwig 	return ret;
640f46b5a66SChristoph Hellwig }
641f46b5a66SChristoph Hellwig 
6424260f7c7SSage Weil /*  copy of check_sticky in fs/namei.c()
6434260f7c7SSage Weil * It's inline, so penalty for filesystems that don't use sticky bit is
6444260f7c7SSage Weil * minimal.
6454260f7c7SSage Weil */
6464260f7c7SSage Weil static inline int btrfs_check_sticky(struct inode *dir, struct inode *inode)
6474260f7c7SSage Weil {
6482f2f43d3SEric W. Biederman 	kuid_t fsuid = current_fsuid();
6494260f7c7SSage Weil 
6504260f7c7SSage Weil 	if (!(dir->i_mode & S_ISVTX))
6514260f7c7SSage Weil 		return 0;
6522f2f43d3SEric W. Biederman 	if (uid_eq(inode->i_uid, fsuid))
6534260f7c7SSage Weil 		return 0;
6542f2f43d3SEric W. Biederman 	if (uid_eq(dir->i_uid, fsuid))
6554260f7c7SSage Weil 		return 0;
6564260f7c7SSage Weil 	return !capable(CAP_FOWNER);
6574260f7c7SSage Weil }
6584260f7c7SSage Weil 
6594260f7c7SSage Weil /*  copy of may_delete in fs/namei.c()
6604260f7c7SSage Weil  *	Check whether we can remove a link victim from directory dir, check
6614260f7c7SSage Weil  *  whether the type of victim is right.
6624260f7c7SSage Weil  *  1. We can't do it if dir is read-only (done in permission())
6634260f7c7SSage Weil  *  2. We should have write and exec permissions on dir
6644260f7c7SSage Weil  *  3. We can't remove anything from append-only dir
6654260f7c7SSage Weil  *  4. We can't do anything with immutable dir (done in permission())
6664260f7c7SSage Weil  *  5. If the sticky bit on dir is set we should either
6674260f7c7SSage Weil  *	a. be owner of dir, or
6684260f7c7SSage Weil  *	b. be owner of victim, or
6694260f7c7SSage Weil  *	c. have CAP_FOWNER capability
6704260f7c7SSage Weil  *  6. If the victim is append-only or immutable we can't do antyhing with
6714260f7c7SSage Weil  *     links pointing to it.
6724260f7c7SSage Weil  *  7. If we were asked to remove a directory and victim isn't one - ENOTDIR.
6734260f7c7SSage Weil  *  8. If we were asked to remove a non-directory and victim isn't one - EISDIR.
6744260f7c7SSage Weil  *  9. We can't remove a root or mountpoint.
6754260f7c7SSage Weil  * 10. We don't allow removal of NFS sillyrenamed files; it's handled by
6764260f7c7SSage Weil  *     nfs_async_unlink().
6774260f7c7SSage Weil  */
6784260f7c7SSage Weil 
6794260f7c7SSage Weil static int btrfs_may_delete(struct inode *dir,struct dentry *victim,int isdir)
6804260f7c7SSage Weil {
6814260f7c7SSage Weil 	int error;
6824260f7c7SSage Weil 
6834260f7c7SSage Weil 	if (!victim->d_inode)
6844260f7c7SSage Weil 		return -ENOENT;
6854260f7c7SSage Weil 
6864260f7c7SSage Weil 	BUG_ON(victim->d_parent->d_inode != dir);
6874fa6b5ecSJeff Layton 	audit_inode_child(dir, victim, AUDIT_TYPE_CHILD_DELETE);
6884260f7c7SSage Weil 
6894260f7c7SSage Weil 	error = inode_permission(dir, MAY_WRITE | MAY_EXEC);
6904260f7c7SSage Weil 	if (error)
6914260f7c7SSage Weil 		return error;
6924260f7c7SSage Weil 	if (IS_APPEND(dir))
6934260f7c7SSage Weil 		return -EPERM;
6944260f7c7SSage Weil 	if (btrfs_check_sticky(dir, victim->d_inode)||
6954260f7c7SSage Weil 		IS_APPEND(victim->d_inode)||
6964260f7c7SSage Weil 	    IS_IMMUTABLE(victim->d_inode) || IS_SWAPFILE(victim->d_inode))
6974260f7c7SSage Weil 		return -EPERM;
6984260f7c7SSage Weil 	if (isdir) {
6994260f7c7SSage Weil 		if (!S_ISDIR(victim->d_inode->i_mode))
7004260f7c7SSage Weil 			return -ENOTDIR;
7014260f7c7SSage Weil 		if (IS_ROOT(victim))
7024260f7c7SSage Weil 			return -EBUSY;
7034260f7c7SSage Weil 	} else if (S_ISDIR(victim->d_inode->i_mode))
7044260f7c7SSage Weil 		return -EISDIR;
7054260f7c7SSage Weil 	if (IS_DEADDIR(dir))
7064260f7c7SSage Weil 		return -ENOENT;
7074260f7c7SSage Weil 	if (victim->d_flags & DCACHE_NFSFS_RENAMED)
7084260f7c7SSage Weil 		return -EBUSY;
7094260f7c7SSage Weil 	return 0;
7104260f7c7SSage Weil }
7114260f7c7SSage Weil 
712cb8e7090SChristoph Hellwig /* copy of may_create in fs/namei.c() */
713cb8e7090SChristoph Hellwig static inline int btrfs_may_create(struct inode *dir, struct dentry *child)
714cb8e7090SChristoph Hellwig {
715cb8e7090SChristoph Hellwig 	if (child->d_inode)
716cb8e7090SChristoph Hellwig 		return -EEXIST;
717cb8e7090SChristoph Hellwig 	if (IS_DEADDIR(dir))
718cb8e7090SChristoph Hellwig 		return -ENOENT;
719cb8e7090SChristoph Hellwig 	return inode_permission(dir, MAY_WRITE | MAY_EXEC);
720cb8e7090SChristoph Hellwig }
721cb8e7090SChristoph Hellwig 
722cb8e7090SChristoph Hellwig /*
723cb8e7090SChristoph Hellwig  * Create a new subvolume below @parent.  This is largely modeled after
724cb8e7090SChristoph Hellwig  * sys_mkdirat and vfs_mkdir, but we only do a single component lookup
725cb8e7090SChristoph Hellwig  * inside this filesystem so it's quite a bit simpler.
726cb8e7090SChristoph Hellwig  */
72776dda93cSYan, Zheng static noinline int btrfs_mksubvol(struct path *parent,
72876dda93cSYan, Zheng 				   char *name, int namelen,
72972fd032eSSage Weil 				   struct btrfs_root *snap_src,
7306f72c7e2SArne Jansen 				   u64 *async_transid, bool readonly,
7318696c533SMiao Xie 				   struct btrfs_qgroup_inherit *inherit)
732cb8e7090SChristoph Hellwig {
73376dda93cSYan, Zheng 	struct inode *dir  = parent->dentry->d_inode;
734cb8e7090SChristoph Hellwig 	struct dentry *dentry;
735cb8e7090SChristoph Hellwig 	int error;
736cb8e7090SChristoph Hellwig 
7375c50c9b8SDavid Sterba 	error = mutex_lock_killable_nested(&dir->i_mutex, I_MUTEX_PARENT);
7385c50c9b8SDavid Sterba 	if (error == -EINTR)
7395c50c9b8SDavid Sterba 		return error;
740cb8e7090SChristoph Hellwig 
741cb8e7090SChristoph Hellwig 	dentry = lookup_one_len(name, parent->dentry, namelen);
742cb8e7090SChristoph Hellwig 	error = PTR_ERR(dentry);
743cb8e7090SChristoph Hellwig 	if (IS_ERR(dentry))
744cb8e7090SChristoph Hellwig 		goto out_unlock;
745cb8e7090SChristoph Hellwig 
746cb8e7090SChristoph Hellwig 	error = -EEXIST;
747cb8e7090SChristoph Hellwig 	if (dentry->d_inode)
748cb8e7090SChristoph Hellwig 		goto out_dput;
749cb8e7090SChristoph Hellwig 
75076dda93cSYan, Zheng 	error = btrfs_may_create(dir, dentry);
751cb8e7090SChristoph Hellwig 	if (error)
752a874a63eSLiu Bo 		goto out_dput;
753cb8e7090SChristoph Hellwig 
7549c52057cSChris Mason 	/*
7559c52057cSChris Mason 	 * even if this name doesn't exist, we may get hash collisions.
7569c52057cSChris Mason 	 * check for them now when we can safely fail
7579c52057cSChris Mason 	 */
7589c52057cSChris Mason 	error = btrfs_check_dir_item_collision(BTRFS_I(dir)->root,
7599c52057cSChris Mason 					       dir->i_ino, name,
7609c52057cSChris Mason 					       namelen);
7619c52057cSChris Mason 	if (error)
7629c52057cSChris Mason 		goto out_dput;
7639c52057cSChris Mason 
76476dda93cSYan, Zheng 	down_read(&BTRFS_I(dir)->root->fs_info->subvol_sem);
76576dda93cSYan, Zheng 
76676dda93cSYan, Zheng 	if (btrfs_root_refs(&BTRFS_I(dir)->root->root_item) == 0)
76776dda93cSYan, Zheng 		goto out_up_read;
76876dda93cSYan, Zheng 
7693de4586cSChris Mason 	if (snap_src) {
770e9662f70SMiao Xie 		error = create_snapshot(snap_src, dir, dentry, name, namelen,
7716f72c7e2SArne Jansen 					async_transid, readonly, inherit);
7723de4586cSChris Mason 	} else {
773d5c12070SMiao Xie 		error = create_subvol(dir, dentry, name, namelen,
774d5c12070SMiao Xie 				      async_transid, inherit);
7753de4586cSChris Mason 	}
77676dda93cSYan, Zheng 	if (!error)
77776dda93cSYan, Zheng 		fsnotify_mkdir(dir, dentry);
77876dda93cSYan, Zheng out_up_read:
77976dda93cSYan, Zheng 	up_read(&BTRFS_I(dir)->root->fs_info->subvol_sem);
780cb8e7090SChristoph Hellwig out_dput:
781cb8e7090SChristoph Hellwig 	dput(dentry);
782cb8e7090SChristoph Hellwig out_unlock:
78376dda93cSYan, Zheng 	mutex_unlock(&dir->i_mutex);
784cb8e7090SChristoph Hellwig 	return error;
785cb8e7090SChristoph Hellwig }
786cb8e7090SChristoph Hellwig 
7874cb5300bSChris Mason /*
7884cb5300bSChris Mason  * When we're defragging a range, we don't want to kick it off again
7894cb5300bSChris Mason  * if it is really just waiting for delalloc to send it down.
7904cb5300bSChris Mason  * If we find a nice big extent or delalloc range for the bytes in the
7914cb5300bSChris Mason  * file you want to defrag, we return 0 to let you know to skip this
7924cb5300bSChris Mason  * part of the file
7934cb5300bSChris Mason  */
7944cb5300bSChris Mason static int check_defrag_in_cache(struct inode *inode, u64 offset, int thresh)
7954cb5300bSChris Mason {
7964cb5300bSChris Mason 	struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
7974cb5300bSChris Mason 	struct extent_map *em = NULL;
7984cb5300bSChris Mason 	struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
7994cb5300bSChris Mason 	u64 end;
8004cb5300bSChris Mason 
8014cb5300bSChris Mason 	read_lock(&em_tree->lock);
8024cb5300bSChris Mason 	em = lookup_extent_mapping(em_tree, offset, PAGE_CACHE_SIZE);
8034cb5300bSChris Mason 	read_unlock(&em_tree->lock);
8044cb5300bSChris Mason 
8054cb5300bSChris Mason 	if (em) {
8064cb5300bSChris Mason 		end = extent_map_end(em);
8074cb5300bSChris Mason 		free_extent_map(em);
8084cb5300bSChris Mason 		if (end - offset > thresh)
8094cb5300bSChris Mason 			return 0;
8104cb5300bSChris Mason 	}
8114cb5300bSChris Mason 	/* if we already have a nice delalloc here, just stop */
8124cb5300bSChris Mason 	thresh /= 2;
8134cb5300bSChris Mason 	end = count_range_bits(io_tree, &offset, offset + thresh,
8144cb5300bSChris Mason 			       thresh, EXTENT_DELALLOC, 1);
8154cb5300bSChris Mason 	if (end >= thresh)
8164cb5300bSChris Mason 		return 0;
8174cb5300bSChris Mason 	return 1;
8184cb5300bSChris Mason }
8194cb5300bSChris Mason 
8204cb5300bSChris Mason /*
8214cb5300bSChris Mason  * helper function to walk through a file and find extents
8224cb5300bSChris Mason  * newer than a specific transid, and smaller than thresh.
8234cb5300bSChris Mason  *
8244cb5300bSChris Mason  * This is used by the defragging code to find new and small
8254cb5300bSChris Mason  * extents
8264cb5300bSChris Mason  */
8274cb5300bSChris Mason static int find_new_extents(struct btrfs_root *root,
8284cb5300bSChris Mason 			    struct inode *inode, u64 newer_than,
8294cb5300bSChris Mason 			    u64 *off, int thresh)
8304cb5300bSChris Mason {
8314cb5300bSChris Mason 	struct btrfs_path *path;
8324cb5300bSChris Mason 	struct btrfs_key min_key;
8334cb5300bSChris Mason 	struct btrfs_key max_key;
8344cb5300bSChris Mason 	struct extent_buffer *leaf;
8354cb5300bSChris Mason 	struct btrfs_file_extent_item *extent;
8364cb5300bSChris Mason 	int type;
8374cb5300bSChris Mason 	int ret;
838a4689d2bSDavid Sterba 	u64 ino = btrfs_ino(inode);
8394cb5300bSChris Mason 
8404cb5300bSChris Mason 	path = btrfs_alloc_path();
8414cb5300bSChris Mason 	if (!path)
8424cb5300bSChris Mason 		return -ENOMEM;
8434cb5300bSChris Mason 
844a4689d2bSDavid Sterba 	min_key.objectid = ino;
8454cb5300bSChris Mason 	min_key.type = BTRFS_EXTENT_DATA_KEY;
8464cb5300bSChris Mason 	min_key.offset = *off;
8474cb5300bSChris Mason 
848a4689d2bSDavid Sterba 	max_key.objectid = ino;
8494cb5300bSChris Mason 	max_key.type = (u8)-1;
8504cb5300bSChris Mason 	max_key.offset = (u64)-1;
8514cb5300bSChris Mason 
8524cb5300bSChris Mason 	path->keep_locks = 1;
8534cb5300bSChris Mason 
8544cb5300bSChris Mason 	while(1) {
8554cb5300bSChris Mason 		ret = btrfs_search_forward(root, &min_key, &max_key,
856de78b51aSEric Sandeen 					   path, newer_than);
8574cb5300bSChris Mason 		if (ret != 0)
8584cb5300bSChris Mason 			goto none;
859a4689d2bSDavid Sterba 		if (min_key.objectid != ino)
8604cb5300bSChris Mason 			goto none;
8614cb5300bSChris Mason 		if (min_key.type != BTRFS_EXTENT_DATA_KEY)
8624cb5300bSChris Mason 			goto none;
8634cb5300bSChris Mason 
8644cb5300bSChris Mason 		leaf = path->nodes[0];
8654cb5300bSChris Mason 		extent = btrfs_item_ptr(leaf, path->slots[0],
8664cb5300bSChris Mason 					struct btrfs_file_extent_item);
8674cb5300bSChris Mason 
8684cb5300bSChris Mason 		type = btrfs_file_extent_type(leaf, extent);
8694cb5300bSChris Mason 		if (type == BTRFS_FILE_EXTENT_REG &&
8704cb5300bSChris Mason 		    btrfs_file_extent_num_bytes(leaf, extent) < thresh &&
8714cb5300bSChris Mason 		    check_defrag_in_cache(inode, min_key.offset, thresh)) {
8724cb5300bSChris Mason 			*off = min_key.offset;
8734cb5300bSChris Mason 			btrfs_free_path(path);
8744cb5300bSChris Mason 			return 0;
8754cb5300bSChris Mason 		}
8764cb5300bSChris Mason 
8774cb5300bSChris Mason 		if (min_key.offset == (u64)-1)
8784cb5300bSChris Mason 			goto none;
8794cb5300bSChris Mason 
8804cb5300bSChris Mason 		min_key.offset++;
8814cb5300bSChris Mason 		btrfs_release_path(path);
8824cb5300bSChris Mason 	}
8834cb5300bSChris Mason none:
8844cb5300bSChris Mason 	btrfs_free_path(path);
8854cb5300bSChris Mason 	return -ENOENT;
8864cb5300bSChris Mason }
8874cb5300bSChris Mason 
8886c282eb4SLi Zefan static struct extent_map *defrag_lookup_extent(struct inode *inode, u64 start)
88917ce6ef8SLiu Bo {
89017ce6ef8SLiu Bo 	struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
891940100a4SChris Mason 	struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
8926c282eb4SLi Zefan 	struct extent_map *em;
8936c282eb4SLi Zefan 	u64 len = PAGE_CACHE_SIZE;
894940100a4SChris Mason 
895940100a4SChris Mason 	/*
896940100a4SChris Mason 	 * hopefully we have this extent in the tree already, try without
897940100a4SChris Mason 	 * the full extent lock
898940100a4SChris Mason 	 */
899940100a4SChris Mason 	read_lock(&em_tree->lock);
900940100a4SChris Mason 	em = lookup_extent_mapping(em_tree, start, len);
901940100a4SChris Mason 	read_unlock(&em_tree->lock);
902940100a4SChris Mason 
903940100a4SChris Mason 	if (!em) {
904940100a4SChris Mason 		/* get the big lock and read metadata off disk */
905d0082371SJeff Mahoney 		lock_extent(io_tree, start, start + len - 1);
906940100a4SChris Mason 		em = btrfs_get_extent(inode, NULL, 0, start, len, 0);
907d0082371SJeff Mahoney 		unlock_extent(io_tree, start, start + len - 1);
908940100a4SChris Mason 
9096cf8bfbfSDan Carpenter 		if (IS_ERR(em))
9106c282eb4SLi Zefan 			return NULL;
911940100a4SChris Mason 	}
912940100a4SChris Mason 
9136c282eb4SLi Zefan 	return em;
9146c282eb4SLi Zefan }
9156c282eb4SLi Zefan 
9166c282eb4SLi Zefan static bool defrag_check_next_extent(struct inode *inode, struct extent_map *em)
9176c282eb4SLi Zefan {
9186c282eb4SLi Zefan 	struct extent_map *next;
9196c282eb4SLi Zefan 	bool ret = true;
9206c282eb4SLi Zefan 
9216c282eb4SLi Zefan 	/* this is the last extent */
9226c282eb4SLi Zefan 	if (em->start + em->len >= i_size_read(inode))
9236c282eb4SLi Zefan 		return false;
9246c282eb4SLi Zefan 
9256c282eb4SLi Zefan 	next = defrag_lookup_extent(inode, em->start + em->len);
9266c282eb4SLi Zefan 	if (!next || next->block_start >= EXTENT_MAP_LAST_BYTE)
9276c282eb4SLi Zefan 		ret = false;
9286c282eb4SLi Zefan 
9296c282eb4SLi Zefan 	free_extent_map(next);
9306c282eb4SLi Zefan 	return ret;
9316c282eb4SLi Zefan }
9326c282eb4SLi Zefan 
9336c282eb4SLi Zefan static int should_defrag_range(struct inode *inode, u64 start, int thresh,
934a43a2111SAndrew Mahone 			       u64 *last_len, u64 *skip, u64 *defrag_end,
935a43a2111SAndrew Mahone 			       int compress)
9366c282eb4SLi Zefan {
9376c282eb4SLi Zefan 	struct extent_map *em;
9386c282eb4SLi Zefan 	int ret = 1;
9396c282eb4SLi Zefan 	bool next_mergeable = true;
9406c282eb4SLi Zefan 
9416c282eb4SLi Zefan 	/*
9426c282eb4SLi Zefan 	 * make sure that once we start defragging an extent, we keep on
9436c282eb4SLi Zefan 	 * defragging it
9446c282eb4SLi Zefan 	 */
9456c282eb4SLi Zefan 	if (start < *defrag_end)
9466c282eb4SLi Zefan 		return 1;
9476c282eb4SLi Zefan 
9486c282eb4SLi Zefan 	*skip = 0;
9496c282eb4SLi Zefan 
9506c282eb4SLi Zefan 	em = defrag_lookup_extent(inode, start);
9516c282eb4SLi Zefan 	if (!em)
9526c282eb4SLi Zefan 		return 0;
9536c282eb4SLi Zefan 
954940100a4SChris Mason 	/* this will cover holes, and inline extents */
95517ce6ef8SLiu Bo 	if (em->block_start >= EXTENT_MAP_LAST_BYTE) {
956940100a4SChris Mason 		ret = 0;
95717ce6ef8SLiu Bo 		goto out;
95817ce6ef8SLiu Bo 	}
95917ce6ef8SLiu Bo 
9606c282eb4SLi Zefan 	next_mergeable = defrag_check_next_extent(inode, em);
961940100a4SChris Mason 
962940100a4SChris Mason 	/*
9636c282eb4SLi Zefan 	 * we hit a real extent, if it is big or the next extent is not a
9646c282eb4SLi Zefan 	 * real extent, don't bother defragging it
965940100a4SChris Mason 	 */
966a43a2111SAndrew Mahone 	if (!compress && (*last_len == 0 || *last_len >= thresh) &&
9676c282eb4SLi Zefan 	    (em->len >= thresh || !next_mergeable))
968940100a4SChris Mason 		ret = 0;
96917ce6ef8SLiu Bo out:
970940100a4SChris Mason 	/*
971940100a4SChris Mason 	 * last_len ends up being a counter of how many bytes we've defragged.
972940100a4SChris Mason 	 * every time we choose not to defrag an extent, we reset *last_len
973940100a4SChris Mason 	 * so that the next tiny extent will force a defrag.
974940100a4SChris Mason 	 *
975940100a4SChris Mason 	 * The end result of this is that tiny extents before a single big
976940100a4SChris Mason 	 * extent will force at least part of that big extent to be defragged.
977940100a4SChris Mason 	 */
978940100a4SChris Mason 	if (ret) {
979940100a4SChris Mason 		*defrag_end = extent_map_end(em);
980940100a4SChris Mason 	} else {
981940100a4SChris Mason 		*last_len = 0;
982940100a4SChris Mason 		*skip = extent_map_end(em);
983940100a4SChris Mason 		*defrag_end = 0;
984940100a4SChris Mason 	}
985940100a4SChris Mason 
986940100a4SChris Mason 	free_extent_map(em);
987940100a4SChris Mason 	return ret;
988940100a4SChris Mason }
989940100a4SChris Mason 
9904cb5300bSChris Mason /*
9914cb5300bSChris Mason  * it doesn't do much good to defrag one or two pages
9924cb5300bSChris Mason  * at a time.  This pulls in a nice chunk of pages
9934cb5300bSChris Mason  * to COW and defrag.
9944cb5300bSChris Mason  *
9954cb5300bSChris Mason  * It also makes sure the delalloc code has enough
9964cb5300bSChris Mason  * dirty data to avoid making new small extents as part
9974cb5300bSChris Mason  * of the defrag
9984cb5300bSChris Mason  *
9994cb5300bSChris Mason  * It's a good idea to start RA on this range
10004cb5300bSChris Mason  * before calling this.
10014cb5300bSChris Mason  */
10024cb5300bSChris Mason static int cluster_pages_for_defrag(struct inode *inode,
10034cb5300bSChris Mason 				    struct page **pages,
10044cb5300bSChris Mason 				    unsigned long start_index,
10054cb5300bSChris Mason 				    int num_pages)
1006f46b5a66SChristoph Hellwig {
10074cb5300bSChris Mason 	unsigned long file_end;
10084cb5300bSChris Mason 	u64 isize = i_size_read(inode);
1009f46b5a66SChristoph Hellwig 	u64 page_start;
1010f46b5a66SChristoph Hellwig 	u64 page_end;
10111f12bd06SLiu Bo 	u64 page_cnt;
10124cb5300bSChris Mason 	int ret;
10134cb5300bSChris Mason 	int i;
10144cb5300bSChris Mason 	int i_done;
10154cb5300bSChris Mason 	struct btrfs_ordered_extent *ordered;
10164cb5300bSChris Mason 	struct extent_state *cached_state = NULL;
1017600a45e1SMiao Xie 	struct extent_io_tree *tree;
10183b16a4e3SJosef Bacik 	gfp_t mask = btrfs_alloc_write_mask(inode->i_mapping);
10194cb5300bSChris Mason 
10204cb5300bSChris Mason 	file_end = (isize - 1) >> PAGE_CACHE_SHIFT;
10211f12bd06SLiu Bo 	if (!isize || start_index > file_end)
10221f12bd06SLiu Bo 		return 0;
10231f12bd06SLiu Bo 
10241f12bd06SLiu Bo 	page_cnt = min_t(u64, (u64)num_pages, (u64)file_end - start_index + 1);
10254cb5300bSChris Mason 
10264cb5300bSChris Mason 	ret = btrfs_delalloc_reserve_space(inode,
10271f12bd06SLiu Bo 					   page_cnt << PAGE_CACHE_SHIFT);
10284cb5300bSChris Mason 	if (ret)
10294cb5300bSChris Mason 		return ret;
10304cb5300bSChris Mason 	i_done = 0;
1031600a45e1SMiao Xie 	tree = &BTRFS_I(inode)->io_tree;
10324cb5300bSChris Mason 
10334cb5300bSChris Mason 	/* step one, lock all the pages */
10341f12bd06SLiu Bo 	for (i = 0; i < page_cnt; i++) {
10354cb5300bSChris Mason 		struct page *page;
1036600a45e1SMiao Xie again:
1037a94733d0SJosef Bacik 		page = find_or_create_page(inode->i_mapping,
10383b16a4e3SJosef Bacik 					   start_index + i, mask);
10394cb5300bSChris Mason 		if (!page)
10404cb5300bSChris Mason 			break;
10414cb5300bSChris Mason 
1042600a45e1SMiao Xie 		page_start = page_offset(page);
1043600a45e1SMiao Xie 		page_end = page_start + PAGE_CACHE_SIZE - 1;
1044600a45e1SMiao Xie 		while (1) {
1045d0082371SJeff Mahoney 			lock_extent(tree, page_start, page_end);
1046600a45e1SMiao Xie 			ordered = btrfs_lookup_ordered_extent(inode,
1047600a45e1SMiao Xie 							      page_start);
1048d0082371SJeff Mahoney 			unlock_extent(tree, page_start, page_end);
1049600a45e1SMiao Xie 			if (!ordered)
1050600a45e1SMiao Xie 				break;
1051600a45e1SMiao Xie 
1052600a45e1SMiao Xie 			unlock_page(page);
1053600a45e1SMiao Xie 			btrfs_start_ordered_extent(inode, ordered, 1);
1054600a45e1SMiao Xie 			btrfs_put_ordered_extent(ordered);
1055600a45e1SMiao Xie 			lock_page(page);
10561f12bd06SLiu Bo 			/*
10571f12bd06SLiu Bo 			 * we unlocked the page above, so we need check if
10581f12bd06SLiu Bo 			 * it was released or not.
10591f12bd06SLiu Bo 			 */
10601f12bd06SLiu Bo 			if (page->mapping != inode->i_mapping) {
10611f12bd06SLiu Bo 				unlock_page(page);
10621f12bd06SLiu Bo 				page_cache_release(page);
10631f12bd06SLiu Bo 				goto again;
10641f12bd06SLiu Bo 			}
1065600a45e1SMiao Xie 		}
1066600a45e1SMiao Xie 
10674cb5300bSChris Mason 		if (!PageUptodate(page)) {
10684cb5300bSChris Mason 			btrfs_readpage(NULL, page);
10694cb5300bSChris Mason 			lock_page(page);
10704cb5300bSChris Mason 			if (!PageUptodate(page)) {
10714cb5300bSChris Mason 				unlock_page(page);
10724cb5300bSChris Mason 				page_cache_release(page);
10734cb5300bSChris Mason 				ret = -EIO;
10744cb5300bSChris Mason 				break;
10754cb5300bSChris Mason 			}
10764cb5300bSChris Mason 		}
1077600a45e1SMiao Xie 
1078600a45e1SMiao Xie 		if (page->mapping != inode->i_mapping) {
1079600a45e1SMiao Xie 			unlock_page(page);
1080600a45e1SMiao Xie 			page_cache_release(page);
1081600a45e1SMiao Xie 			goto again;
1082600a45e1SMiao Xie 		}
1083600a45e1SMiao Xie 
10844cb5300bSChris Mason 		pages[i] = page;
10854cb5300bSChris Mason 		i_done++;
10864cb5300bSChris Mason 	}
10874cb5300bSChris Mason 	if (!i_done || ret)
10884cb5300bSChris Mason 		goto out;
10894cb5300bSChris Mason 
10904cb5300bSChris Mason 	if (!(inode->i_sb->s_flags & MS_ACTIVE))
10914cb5300bSChris Mason 		goto out;
10924cb5300bSChris Mason 
10934cb5300bSChris Mason 	/*
10944cb5300bSChris Mason 	 * so now we have a nice long stream of locked
10954cb5300bSChris Mason 	 * and up to date pages, lets wait on them
10964cb5300bSChris Mason 	 */
10974cb5300bSChris Mason 	for (i = 0; i < i_done; i++)
10984cb5300bSChris Mason 		wait_on_page_writeback(pages[i]);
10994cb5300bSChris Mason 
11004cb5300bSChris Mason 	page_start = page_offset(pages[0]);
11014cb5300bSChris Mason 	page_end = page_offset(pages[i_done - 1]) + PAGE_CACHE_SIZE;
11024cb5300bSChris Mason 
11034cb5300bSChris Mason 	lock_extent_bits(&BTRFS_I(inode)->io_tree,
1104d0082371SJeff Mahoney 			 page_start, page_end - 1, 0, &cached_state);
11054cb5300bSChris Mason 	clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start,
11064cb5300bSChris Mason 			  page_end - 1, EXTENT_DIRTY | EXTENT_DELALLOC |
11079e8a4a8bSLiu Bo 			  EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG, 0, 0,
11089e8a4a8bSLiu Bo 			  &cached_state, GFP_NOFS);
11094cb5300bSChris Mason 
11101f12bd06SLiu Bo 	if (i_done != page_cnt) {
11119e0baf60SJosef Bacik 		spin_lock(&BTRFS_I(inode)->lock);
11129e0baf60SJosef Bacik 		BTRFS_I(inode)->outstanding_extents++;
11139e0baf60SJosef Bacik 		spin_unlock(&BTRFS_I(inode)->lock);
11144cb5300bSChris Mason 		btrfs_delalloc_release_space(inode,
11151f12bd06SLiu Bo 				     (page_cnt - i_done) << PAGE_CACHE_SHIFT);
11164cb5300bSChris Mason 	}
11174cb5300bSChris Mason 
11184cb5300bSChris Mason 
11199e8a4a8bSLiu Bo 	set_extent_defrag(&BTRFS_I(inode)->io_tree, page_start, page_end - 1,
11209e8a4a8bSLiu Bo 			  &cached_state, GFP_NOFS);
11214cb5300bSChris Mason 
11224cb5300bSChris Mason 	unlock_extent_cached(&BTRFS_I(inode)->io_tree,
11234cb5300bSChris Mason 			     page_start, page_end - 1, &cached_state,
11244cb5300bSChris Mason 			     GFP_NOFS);
11254cb5300bSChris Mason 
11264cb5300bSChris Mason 	for (i = 0; i < i_done; i++) {
11274cb5300bSChris Mason 		clear_page_dirty_for_io(pages[i]);
11284cb5300bSChris Mason 		ClearPageChecked(pages[i]);
11294cb5300bSChris Mason 		set_page_extent_mapped(pages[i]);
11304cb5300bSChris Mason 		set_page_dirty(pages[i]);
11314cb5300bSChris Mason 		unlock_page(pages[i]);
11324cb5300bSChris Mason 		page_cache_release(pages[i]);
11334cb5300bSChris Mason 	}
11344cb5300bSChris Mason 	return i_done;
11354cb5300bSChris Mason out:
11364cb5300bSChris Mason 	for (i = 0; i < i_done; i++) {
11374cb5300bSChris Mason 		unlock_page(pages[i]);
11384cb5300bSChris Mason 		page_cache_release(pages[i]);
11394cb5300bSChris Mason 	}
11401f12bd06SLiu Bo 	btrfs_delalloc_release_space(inode, page_cnt << PAGE_CACHE_SHIFT);
11414cb5300bSChris Mason 	return ret;
11424cb5300bSChris Mason 
11434cb5300bSChris Mason }
11444cb5300bSChris Mason 
11454cb5300bSChris Mason int btrfs_defrag_file(struct inode *inode, struct file *file,
11464cb5300bSChris Mason 		      struct btrfs_ioctl_defrag_range_args *range,
11474cb5300bSChris Mason 		      u64 newer_than, unsigned long max_to_defrag)
11484cb5300bSChris Mason {
11494cb5300bSChris Mason 	struct btrfs_root *root = BTRFS_I(inode)->root;
11504cb5300bSChris Mason 	struct file_ra_state *ra = NULL;
11514cb5300bSChris Mason 	unsigned long last_index;
1152151a31b2SLi Zefan 	u64 isize = i_size_read(inode);
1153940100a4SChris Mason 	u64 last_len = 0;
1154940100a4SChris Mason 	u64 skip = 0;
1155940100a4SChris Mason 	u64 defrag_end = 0;
11564cb5300bSChris Mason 	u64 newer_off = range->start;
1157f46b5a66SChristoph Hellwig 	unsigned long i;
1158008873eaSLi Zefan 	unsigned long ra_index = 0;
1159f46b5a66SChristoph Hellwig 	int ret;
11604cb5300bSChris Mason 	int defrag_count = 0;
11611a419d85SLi Zefan 	int compress_type = BTRFS_COMPRESS_ZLIB;
11624cb5300bSChris Mason 	int extent_thresh = range->extent_thresh;
1163008873eaSLi Zefan 	int max_cluster = (256 * 1024) >> PAGE_CACHE_SHIFT;
1164008873eaSLi Zefan 	int cluster = max_cluster;
11654cb5300bSChris Mason 	u64 new_align = ~((u64)128 * 1024 - 1);
11664cb5300bSChris Mason 	struct page **pages = NULL;
11674cb5300bSChris Mason 
11680abd5b17SLiu Bo 	if (isize == 0)
11690abd5b17SLiu Bo 		return 0;
11700abd5b17SLiu Bo 
11710abd5b17SLiu Bo 	if (range->start >= isize)
11720abd5b17SLiu Bo 		return -EINVAL;
11731a419d85SLi Zefan 
11741a419d85SLi Zefan 	if (range->flags & BTRFS_DEFRAG_RANGE_COMPRESS) {
11751a419d85SLi Zefan 		if (range->compress_type > BTRFS_COMPRESS_TYPES)
11761a419d85SLi Zefan 			return -EINVAL;
11771a419d85SLi Zefan 		if (range->compress_type)
11781a419d85SLi Zefan 			compress_type = range->compress_type;
11791a419d85SLi Zefan 	}
1180f46b5a66SChristoph Hellwig 
11810abd5b17SLiu Bo 	if (extent_thresh == 0)
11820abd5b17SLiu Bo 		extent_thresh = 256 * 1024;
1183f46b5a66SChristoph Hellwig 
11844cb5300bSChris Mason 	/*
11854cb5300bSChris Mason 	 * if we were not given a file, allocate a readahead
11864cb5300bSChris Mason 	 * context
11874cb5300bSChris Mason 	 */
11884cb5300bSChris Mason 	if (!file) {
11894cb5300bSChris Mason 		ra = kzalloc(sizeof(*ra), GFP_NOFS);
11904cb5300bSChris Mason 		if (!ra)
11914cb5300bSChris Mason 			return -ENOMEM;
11924cb5300bSChris Mason 		file_ra_state_init(ra, inode->i_mapping);
11934cb5300bSChris Mason 	} else {
11944cb5300bSChris Mason 		ra = &file->f_ra;
11954cb5300bSChris Mason 	}
11964cb5300bSChris Mason 
1197008873eaSLi Zefan 	pages = kmalloc(sizeof(struct page *) * max_cluster,
11984cb5300bSChris Mason 			GFP_NOFS);
11994cb5300bSChris Mason 	if (!pages) {
12004cb5300bSChris Mason 		ret = -ENOMEM;
12014cb5300bSChris Mason 		goto out_ra;
12024cb5300bSChris Mason 	}
12034cb5300bSChris Mason 
12044cb5300bSChris Mason 	/* find the last page to defrag */
12051e701a32SChris Mason 	if (range->start + range->len > range->start) {
1206151a31b2SLi Zefan 		last_index = min_t(u64, isize - 1,
12071e701a32SChris Mason 			 range->start + range->len - 1) >> PAGE_CACHE_SHIFT;
12081e701a32SChris Mason 	} else {
1209151a31b2SLi Zefan 		last_index = (isize - 1) >> PAGE_CACHE_SHIFT;
12101e701a32SChris Mason 	}
12111e701a32SChris Mason 
12124cb5300bSChris Mason 	if (newer_than) {
12134cb5300bSChris Mason 		ret = find_new_extents(root, inode, newer_than,
12144cb5300bSChris Mason 				       &newer_off, 64 * 1024);
12154cb5300bSChris Mason 		if (!ret) {
12164cb5300bSChris Mason 			range->start = newer_off;
12174cb5300bSChris Mason 			/*
12184cb5300bSChris Mason 			 * we always align our defrag to help keep
12194cb5300bSChris Mason 			 * the extents in the file evenly spaced
12204cb5300bSChris Mason 			 */
12214cb5300bSChris Mason 			i = (newer_off & new_align) >> PAGE_CACHE_SHIFT;
12224cb5300bSChris Mason 		} else
12234cb5300bSChris Mason 			goto out_ra;
12244cb5300bSChris Mason 	} else {
12251e701a32SChris Mason 		i = range->start >> PAGE_CACHE_SHIFT;
12264cb5300bSChris Mason 	}
12274cb5300bSChris Mason 	if (!max_to_defrag)
12287ec31b54SLiu Bo 		max_to_defrag = last_index + 1;
12294cb5300bSChris Mason 
12302a0f7f57SLi Zefan 	/*
12312a0f7f57SLi Zefan 	 * make writeback starts from i, so the defrag range can be
12322a0f7f57SLi Zefan 	 * written sequentially.
12332a0f7f57SLi Zefan 	 */
12342a0f7f57SLi Zefan 	if (i < inode->i_mapping->writeback_index)
12352a0f7f57SLi Zefan 		inode->i_mapping->writeback_index = i;
12362a0f7f57SLi Zefan 
1237f7f43cc8SChris Mason 	while (i <= last_index && defrag_count < max_to_defrag &&
1238f7f43cc8SChris Mason 	       (i < (i_size_read(inode) + PAGE_CACHE_SIZE - 1) >>
1239f7f43cc8SChris Mason 		PAGE_CACHE_SHIFT)) {
12404cb5300bSChris Mason 		/*
12414cb5300bSChris Mason 		 * make sure we stop running if someone unmounts
12424cb5300bSChris Mason 		 * the FS
12434cb5300bSChris Mason 		 */
12444cb5300bSChris Mason 		if (!(inode->i_sb->s_flags & MS_ACTIVE))
12454cb5300bSChris Mason 			break;
12464cb5300bSChris Mason 
1247210549ebSDavid Sterba 		if (btrfs_defrag_cancelled(root->fs_info)) {
1248210549ebSDavid Sterba 			printk(KERN_DEBUG "btrfs: defrag_file cancelled\n");
1249210549ebSDavid Sterba 			ret = -EAGAIN;
1250210549ebSDavid Sterba 			break;
1251210549ebSDavid Sterba 		}
1252210549ebSDavid Sterba 
125366c26892SLiu Bo 		if (!should_defrag_range(inode, (u64)i << PAGE_CACHE_SHIFT,
12546c282eb4SLi Zefan 					 extent_thresh, &last_len, &skip,
1255a43a2111SAndrew Mahone 					 &defrag_end, range->flags &
1256a43a2111SAndrew Mahone 					 BTRFS_DEFRAG_RANGE_COMPRESS)) {
1257940100a4SChris Mason 			unsigned long next;
1258940100a4SChris Mason 			/*
1259940100a4SChris Mason 			 * the should_defrag function tells us how much to skip
1260940100a4SChris Mason 			 * bump our counter by the suggested amount
1261940100a4SChris Mason 			 */
1262940100a4SChris Mason 			next = (skip + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
1263940100a4SChris Mason 			i = max(i + 1, next);
1264940100a4SChris Mason 			continue;
1265940100a4SChris Mason 		}
1266008873eaSLi Zefan 
1267008873eaSLi Zefan 		if (!newer_than) {
1268008873eaSLi Zefan 			cluster = (PAGE_CACHE_ALIGN(defrag_end) >>
1269008873eaSLi Zefan 				   PAGE_CACHE_SHIFT) - i;
1270008873eaSLi Zefan 			cluster = min(cluster, max_cluster);
1271008873eaSLi Zefan 		} else {
1272008873eaSLi Zefan 			cluster = max_cluster;
1273008873eaSLi Zefan 		}
1274008873eaSLi Zefan 
12751e701a32SChris Mason 		if (range->flags & BTRFS_DEFRAG_RANGE_COMPRESS)
12761a419d85SLi Zefan 			BTRFS_I(inode)->force_compress = compress_type;
1277940100a4SChris Mason 
1278008873eaSLi Zefan 		if (i + cluster > ra_index) {
1279008873eaSLi Zefan 			ra_index = max(i, ra_index);
1280008873eaSLi Zefan 			btrfs_force_ra(inode->i_mapping, ra, file, ra_index,
1281008873eaSLi Zefan 				       cluster);
1282008873eaSLi Zefan 			ra_index += max_cluster;
1283008873eaSLi Zefan 		}
12844cb5300bSChris Mason 
1285ecb8bea8SLiu Bo 		mutex_lock(&inode->i_mutex);
1286008873eaSLi Zefan 		ret = cluster_pages_for_defrag(inode, pages, i, cluster);
1287ecb8bea8SLiu Bo 		if (ret < 0) {
1288ecb8bea8SLiu Bo 			mutex_unlock(&inode->i_mutex);
12894cb5300bSChris Mason 			goto out_ra;
1290ecb8bea8SLiu Bo 		}
12914cb5300bSChris Mason 
12924cb5300bSChris Mason 		defrag_count += ret;
1293d0e1d66bSNamjae Jeon 		balance_dirty_pages_ratelimited(inode->i_mapping);
1294ecb8bea8SLiu Bo 		mutex_unlock(&inode->i_mutex);
12954cb5300bSChris Mason 
12964cb5300bSChris Mason 		if (newer_than) {
12974cb5300bSChris Mason 			if (newer_off == (u64)-1)
12984cb5300bSChris Mason 				break;
12994cb5300bSChris Mason 
1300e1f041e1SLiu Bo 			if (ret > 0)
1301e1f041e1SLiu Bo 				i += ret;
1302e1f041e1SLiu Bo 
13034cb5300bSChris Mason 			newer_off = max(newer_off + 1,
13044cb5300bSChris Mason 					(u64)i << PAGE_CACHE_SHIFT);
13054cb5300bSChris Mason 
13064cb5300bSChris Mason 			ret = find_new_extents(root, inode,
13074cb5300bSChris Mason 					       newer_than, &newer_off,
13084cb5300bSChris Mason 					       64 * 1024);
13094cb5300bSChris Mason 			if (!ret) {
13104cb5300bSChris Mason 				range->start = newer_off;
13114cb5300bSChris Mason 				i = (newer_off & new_align) >> PAGE_CACHE_SHIFT;
13124cb5300bSChris Mason 			} else {
13134cb5300bSChris Mason 				break;
1314940100a4SChris Mason 			}
13154cb5300bSChris Mason 		} else {
1316008873eaSLi Zefan 			if (ret > 0) {
1317cbcc8326SLi Zefan 				i += ret;
1318008873eaSLi Zefan 				last_len += ret << PAGE_CACHE_SHIFT;
1319008873eaSLi Zefan 			} else {
1320940100a4SChris Mason 				i++;
1321008873eaSLi Zefan 				last_len = 0;
1322008873eaSLi Zefan 			}
1323f46b5a66SChristoph Hellwig 		}
13244cb5300bSChris Mason 	}
1325f46b5a66SChristoph Hellwig 
13261e701a32SChris Mason 	if ((range->flags & BTRFS_DEFRAG_RANGE_START_IO))
13271e701a32SChris Mason 		filemap_flush(inode->i_mapping);
13281e701a32SChris Mason 
13291e701a32SChris Mason 	if ((range->flags & BTRFS_DEFRAG_RANGE_COMPRESS)) {
13301e701a32SChris Mason 		/* the filemap_flush will queue IO into the worker threads, but
13311e701a32SChris Mason 		 * we have to make sure the IO is actually started and that
13321e701a32SChris Mason 		 * ordered extents get created before we return
13331e701a32SChris Mason 		 */
13341e701a32SChris Mason 		atomic_inc(&root->fs_info->async_submit_draining);
13351e701a32SChris Mason 		while (atomic_read(&root->fs_info->nr_async_submits) ||
13361e701a32SChris Mason 		      atomic_read(&root->fs_info->async_delalloc_pages)) {
13371e701a32SChris Mason 			wait_event(root->fs_info->async_submit_wait,
13381e701a32SChris Mason 			   (atomic_read(&root->fs_info->nr_async_submits) == 0 &&
13391e701a32SChris Mason 			    atomic_read(&root->fs_info->async_delalloc_pages) == 0));
13401e701a32SChris Mason 		}
13411e701a32SChris Mason 		atomic_dec(&root->fs_info->async_submit_draining);
13421e701a32SChris Mason 
13431e701a32SChris Mason 		mutex_lock(&inode->i_mutex);
1344261507a0SLi Zefan 		BTRFS_I(inode)->force_compress = BTRFS_COMPRESS_NONE;
13451e701a32SChris Mason 		mutex_unlock(&inode->i_mutex);
13461e701a32SChris Mason 	}
13471e701a32SChris Mason 
13481a419d85SLi Zefan 	if (range->compress_type == BTRFS_COMPRESS_LZO) {
13492b0ce2c2SMitch Harder 		btrfs_set_fs_incompat(root->fs_info, COMPRESS_LZO);
13501a419d85SLi Zefan 	}
13511a419d85SLi Zefan 
135260ccf82fSDiego Calleja 	ret = defrag_count;
1353940100a4SChris Mason 
13544cb5300bSChris Mason out_ra:
13554cb5300bSChris Mason 	if (!file)
13564cb5300bSChris Mason 		kfree(ra);
13574cb5300bSChris Mason 	kfree(pages);
1358940100a4SChris Mason 	return ret;
1359f46b5a66SChristoph Hellwig }
1360f46b5a66SChristoph Hellwig 
1361198605a8SMiao Xie static noinline int btrfs_ioctl_resize(struct file *file,
136276dda93cSYan, Zheng 					void __user *arg)
1363f46b5a66SChristoph Hellwig {
1364f46b5a66SChristoph Hellwig 	u64 new_size;
1365f46b5a66SChristoph Hellwig 	u64 old_size;
1366f46b5a66SChristoph Hellwig 	u64 devid = 1;
1367496ad9aaSAl Viro 	struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
1368f46b5a66SChristoph Hellwig 	struct btrfs_ioctl_vol_args *vol_args;
1369f46b5a66SChristoph Hellwig 	struct btrfs_trans_handle *trans;
1370f46b5a66SChristoph Hellwig 	struct btrfs_device *device = NULL;
1371f46b5a66SChristoph Hellwig 	char *sizestr;
1372f46b5a66SChristoph Hellwig 	char *devstr = NULL;
1373f46b5a66SChristoph Hellwig 	int ret = 0;
1374f46b5a66SChristoph Hellwig 	int mod = 0;
1375f46b5a66SChristoph Hellwig 
1376e441d54dSChris Mason 	if (!capable(CAP_SYS_ADMIN))
1377e441d54dSChris Mason 		return -EPERM;
1378e441d54dSChris Mason 
1379198605a8SMiao Xie 	ret = mnt_want_write_file(file);
1380198605a8SMiao Xie 	if (ret)
1381198605a8SMiao Xie 		return ret;
1382198605a8SMiao Xie 
13835ac00addSStefan Behrens 	if (atomic_xchg(&root->fs_info->mutually_exclusive_operation_running,
13845ac00addSStefan Behrens 			1)) {
13855ac00addSStefan Behrens 		pr_info("btrfs: dev add/delete/balance/replace/resize operation in progress\n");
138697547676SMiao Xie 		mnt_drop_write_file(file);
13872c0c9da0SIlya Dryomov 		return -EINVAL;
1388c9e9f97bSIlya Dryomov 	}
1389c9e9f97bSIlya Dryomov 
13905ac00addSStefan Behrens 	mutex_lock(&root->fs_info->volume_mutex);
1391dae7b665SLi Zefan 	vol_args = memdup_user(arg, sizeof(*vol_args));
1392c9e9f97bSIlya Dryomov 	if (IS_ERR(vol_args)) {
1393c9e9f97bSIlya Dryomov 		ret = PTR_ERR(vol_args);
1394c9e9f97bSIlya Dryomov 		goto out;
1395c9e9f97bSIlya Dryomov 	}
13965516e595SMark Fasheh 
13975516e595SMark Fasheh 	vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
1398f46b5a66SChristoph Hellwig 
1399f46b5a66SChristoph Hellwig 	sizestr = vol_args->name;
1400f46b5a66SChristoph Hellwig 	devstr = strchr(sizestr, ':');
1401f46b5a66SChristoph Hellwig 	if (devstr) {
1402f46b5a66SChristoph Hellwig 		char *end;
1403f46b5a66SChristoph Hellwig 		sizestr = devstr + 1;
1404f46b5a66SChristoph Hellwig 		*devstr = '\0';
1405f46b5a66SChristoph Hellwig 		devstr = vol_args->name;
1406f46b5a66SChristoph Hellwig 		devid = simple_strtoull(devstr, &end, 10);
1407dfd79829SMiao Xie 		if (!devid) {
1408dfd79829SMiao Xie 			ret = -EINVAL;
1409dfd79829SMiao Xie 			goto out_free;
1410dfd79829SMiao Xie 		}
14115bb14682SArnd Hannemann 		printk(KERN_INFO "btrfs: resizing devid %llu\n",
141221380931SJoel Becker 		       (unsigned long long)devid);
1413f46b5a66SChristoph Hellwig 	}
1414dba60f3fSMiao Xie 
1415aa1b8cd4SStefan Behrens 	device = btrfs_find_device(root->fs_info, devid, NULL, NULL);
1416f46b5a66SChristoph Hellwig 	if (!device) {
14175bb14682SArnd Hannemann 		printk(KERN_INFO "btrfs: resizer unable to find device %llu\n",
141821380931SJoel Becker 		       (unsigned long long)devid);
1419dfd79829SMiao Xie 		ret = -ENODEV;
1420c9e9f97bSIlya Dryomov 		goto out_free;
1421f46b5a66SChristoph Hellwig 	}
1422dba60f3fSMiao Xie 
1423dba60f3fSMiao Xie 	if (!device->writeable) {
14244e42ae1bSLiu Bo 		printk(KERN_INFO "btrfs: resizer unable to apply on "
1425dba60f3fSMiao Xie 		       "readonly device %llu\n",
1426a8c4a33bSChris Mason 		       (unsigned long long)devid);
1427dfd79829SMiao Xie 		ret = -EPERM;
14284e42ae1bSLiu Bo 		goto out_free;
14294e42ae1bSLiu Bo 	}
14304e42ae1bSLiu Bo 
1431f46b5a66SChristoph Hellwig 	if (!strcmp(sizestr, "max"))
1432f46b5a66SChristoph Hellwig 		new_size = device->bdev->bd_inode->i_size;
1433f46b5a66SChristoph Hellwig 	else {
1434f46b5a66SChristoph Hellwig 		if (sizestr[0] == '-') {
1435f46b5a66SChristoph Hellwig 			mod = -1;
1436f46b5a66SChristoph Hellwig 			sizestr++;
1437f46b5a66SChristoph Hellwig 		} else if (sizestr[0] == '+') {
1438f46b5a66SChristoph Hellwig 			mod = 1;
1439f46b5a66SChristoph Hellwig 			sizestr++;
1440f46b5a66SChristoph Hellwig 		}
144191748467SAkinobu Mita 		new_size = memparse(sizestr, NULL);
1442f46b5a66SChristoph Hellwig 		if (new_size == 0) {
1443f46b5a66SChristoph Hellwig 			ret = -EINVAL;
1444c9e9f97bSIlya Dryomov 			goto out_free;
1445f46b5a66SChristoph Hellwig 		}
1446f46b5a66SChristoph Hellwig 	}
1447f46b5a66SChristoph Hellwig 
144863a212abSStefan Behrens 	if (device->is_tgtdev_for_dev_replace) {
1449dfd79829SMiao Xie 		ret = -EPERM;
145063a212abSStefan Behrens 		goto out_free;
145163a212abSStefan Behrens 	}
145263a212abSStefan Behrens 
1453f46b5a66SChristoph Hellwig 	old_size = device->total_bytes;
1454f46b5a66SChristoph Hellwig 
1455f46b5a66SChristoph Hellwig 	if (mod < 0) {
1456f46b5a66SChristoph Hellwig 		if (new_size > old_size) {
1457f46b5a66SChristoph Hellwig 			ret = -EINVAL;
1458c9e9f97bSIlya Dryomov 			goto out_free;
1459f46b5a66SChristoph Hellwig 		}
1460f46b5a66SChristoph Hellwig 		new_size = old_size - new_size;
1461f46b5a66SChristoph Hellwig 	} else if (mod > 0) {
1462f46b5a66SChristoph Hellwig 		new_size = old_size + new_size;
1463f46b5a66SChristoph Hellwig 	}
1464f46b5a66SChristoph Hellwig 
1465f46b5a66SChristoph Hellwig 	if (new_size < 256 * 1024 * 1024) {
1466f46b5a66SChristoph Hellwig 		ret = -EINVAL;
1467c9e9f97bSIlya Dryomov 		goto out_free;
1468f46b5a66SChristoph Hellwig 	}
1469f46b5a66SChristoph Hellwig 	if (new_size > device->bdev->bd_inode->i_size) {
1470f46b5a66SChristoph Hellwig 		ret = -EFBIG;
1471c9e9f97bSIlya Dryomov 		goto out_free;
1472f46b5a66SChristoph Hellwig 	}
1473f46b5a66SChristoph Hellwig 
1474f46b5a66SChristoph Hellwig 	do_div(new_size, root->sectorsize);
1475f46b5a66SChristoph Hellwig 	new_size *= root->sectorsize;
1476f46b5a66SChristoph Hellwig 
1477606686eeSJosef Bacik 	printk_in_rcu(KERN_INFO "btrfs: new size for %s is %llu\n",
1478606686eeSJosef Bacik 		      rcu_str_deref(device->name),
1479606686eeSJosef Bacik 		      (unsigned long long)new_size);
1480f46b5a66SChristoph Hellwig 
1481f46b5a66SChristoph Hellwig 	if (new_size > old_size) {
1482a22285a6SYan, Zheng 		trans = btrfs_start_transaction(root, 0);
148398d5dc13STsutomu Itoh 		if (IS_ERR(trans)) {
148498d5dc13STsutomu Itoh 			ret = PTR_ERR(trans);
1485c9e9f97bSIlya Dryomov 			goto out_free;
148698d5dc13STsutomu Itoh 		}
1487f46b5a66SChristoph Hellwig 		ret = btrfs_grow_device(trans, device, new_size);
1488f46b5a66SChristoph Hellwig 		btrfs_commit_transaction(trans, root);
1489ece7d20eSMike Fleetwood 	} else if (new_size < old_size) {
1490f46b5a66SChristoph Hellwig 		ret = btrfs_shrink_device(device, new_size);
14910253f40eSjeff.liu 	} /* equal, nothing need to do */
1492f46b5a66SChristoph Hellwig 
1493c9e9f97bSIlya Dryomov out_free:
1494f46b5a66SChristoph Hellwig 	kfree(vol_args);
1495c9e9f97bSIlya Dryomov out:
1496c9e9f97bSIlya Dryomov 	mutex_unlock(&root->fs_info->volume_mutex);
14975ac00addSStefan Behrens 	atomic_set(&root->fs_info->mutually_exclusive_operation_running, 0);
149818f39c41SIlya Dryomov 	mnt_drop_write_file(file);
1499f46b5a66SChristoph Hellwig 	return ret;
1500f46b5a66SChristoph Hellwig }
1501f46b5a66SChristoph Hellwig 
150272fd032eSSage Weil static noinline int btrfs_ioctl_snap_create_transid(struct file *file,
15036f72c7e2SArne Jansen 				char *name, unsigned long fd, int subvol,
15046f72c7e2SArne Jansen 				u64 *transid, bool readonly,
15058696c533SMiao Xie 				struct btrfs_qgroup_inherit *inherit)
1506f46b5a66SChristoph Hellwig {
1507f46b5a66SChristoph Hellwig 	int namelen;
15083de4586cSChris Mason 	int ret = 0;
1509f46b5a66SChristoph Hellwig 
1510a874a63eSLiu Bo 	ret = mnt_want_write_file(file);
1511a874a63eSLiu Bo 	if (ret)
1512a874a63eSLiu Bo 		goto out;
1513a874a63eSLiu Bo 
151472fd032eSSage Weil 	namelen = strlen(name);
151572fd032eSSage Weil 	if (strchr(name, '/')) {
1516f46b5a66SChristoph Hellwig 		ret = -EINVAL;
1517a874a63eSLiu Bo 		goto out_drop_write;
1518f46b5a66SChristoph Hellwig 	}
1519f46b5a66SChristoph Hellwig 
152016780cabSChris Mason 	if (name[0] == '.' &&
152116780cabSChris Mason 	   (namelen == 1 || (name[1] == '.' && namelen == 2))) {
152216780cabSChris Mason 		ret = -EEXIST;
1523a874a63eSLiu Bo 		goto out_drop_write;
152416780cabSChris Mason 	}
152516780cabSChris Mason 
15263de4586cSChris Mason 	if (subvol) {
152772fd032eSSage Weil 		ret = btrfs_mksubvol(&file->f_path, name, namelen,
15286f72c7e2SArne Jansen 				     NULL, transid, readonly, inherit);
1529cb8e7090SChristoph Hellwig 	} else {
15302903ff01SAl Viro 		struct fd src = fdget(fd);
15313de4586cSChris Mason 		struct inode *src_inode;
15322903ff01SAl Viro 		if (!src.file) {
15333de4586cSChris Mason 			ret = -EINVAL;
1534a874a63eSLiu Bo 			goto out_drop_write;
15353de4586cSChris Mason 		}
15363de4586cSChris Mason 
1537496ad9aaSAl Viro 		src_inode = file_inode(src.file);
1538496ad9aaSAl Viro 		if (src_inode->i_sb != file_inode(file)->i_sb) {
1539d397712bSChris Mason 			printk(KERN_INFO "btrfs: Snapshot src from "
1540d397712bSChris Mason 			       "another FS\n");
15413de4586cSChris Mason 			ret = -EINVAL;
1542ecd18815SAl Viro 		} else {
154372fd032eSSage Weil 			ret = btrfs_mksubvol(&file->f_path, name, namelen,
154472fd032eSSage Weil 					     BTRFS_I(src_inode)->root,
15456f72c7e2SArne Jansen 					     transid, readonly, inherit);
1546ecd18815SAl Viro 		}
15472903ff01SAl Viro 		fdput(src);
1548cb8e7090SChristoph Hellwig 	}
1549a874a63eSLiu Bo out_drop_write:
1550a874a63eSLiu Bo 	mnt_drop_write_file(file);
1551f46b5a66SChristoph Hellwig out:
155272fd032eSSage Weil 	return ret;
155372fd032eSSage Weil }
155472fd032eSSage Weil 
155572fd032eSSage Weil static noinline int btrfs_ioctl_snap_create(struct file *file,
1556fa0d2b9bSLi Zefan 					    void __user *arg, int subvol)
155772fd032eSSage Weil {
1558fa0d2b9bSLi Zefan 	struct btrfs_ioctl_vol_args *vol_args;
155972fd032eSSage Weil 	int ret;
156072fd032eSSage Weil 
1561fa0d2b9bSLi Zefan 	vol_args = memdup_user(arg, sizeof(*vol_args));
1562fa0d2b9bSLi Zefan 	if (IS_ERR(vol_args))
1563fa0d2b9bSLi Zefan 		return PTR_ERR(vol_args);
1564fa0d2b9bSLi Zefan 	vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
1565fa0d2b9bSLi Zefan 
1566fa0d2b9bSLi Zefan 	ret = btrfs_ioctl_snap_create_transid(file, vol_args->name,
1567b83cc969SLi Zefan 					      vol_args->fd, subvol,
15686f72c7e2SArne Jansen 					      NULL, false, NULL);
1569fa0d2b9bSLi Zefan 
1570fa0d2b9bSLi Zefan 	kfree(vol_args);
1571fa0d2b9bSLi Zefan 	return ret;
1572fa0d2b9bSLi Zefan }
1573fa0d2b9bSLi Zefan 
1574fa0d2b9bSLi Zefan static noinline int btrfs_ioctl_snap_create_v2(struct file *file,
1575fa0d2b9bSLi Zefan 					       void __user *arg, int subvol)
1576fa0d2b9bSLi Zefan {
1577fa0d2b9bSLi Zefan 	struct btrfs_ioctl_vol_args_v2 *vol_args;
1578fa0d2b9bSLi Zefan 	int ret;
1579fdfb1e4fSLi Zefan 	u64 transid = 0;
1580fdfb1e4fSLi Zefan 	u64 *ptr = NULL;
1581b83cc969SLi Zefan 	bool readonly = false;
15826f72c7e2SArne Jansen 	struct btrfs_qgroup_inherit *inherit = NULL;
158372fd032eSSage Weil 
1584fa0d2b9bSLi Zefan 	vol_args = memdup_user(arg, sizeof(*vol_args));
1585fa0d2b9bSLi Zefan 	if (IS_ERR(vol_args))
1586fa0d2b9bSLi Zefan 		return PTR_ERR(vol_args);
1587fa0d2b9bSLi Zefan 	vol_args->name[BTRFS_SUBVOL_NAME_MAX] = '\0';
1588fdfb1e4fSLi Zefan 
1589b83cc969SLi Zefan 	if (vol_args->flags &
15906f72c7e2SArne Jansen 	    ~(BTRFS_SUBVOL_CREATE_ASYNC | BTRFS_SUBVOL_RDONLY |
15916f72c7e2SArne Jansen 	      BTRFS_SUBVOL_QGROUP_INHERIT)) {
1592b83cc969SLi Zefan 		ret = -EOPNOTSUPP;
1593fdfb1e4fSLi Zefan 		goto out;
1594fdfb1e4fSLi Zefan 	}
1595fdfb1e4fSLi Zefan 
1596fa0d2b9bSLi Zefan 	if (vol_args->flags & BTRFS_SUBVOL_CREATE_ASYNC)
1597fdfb1e4fSLi Zefan 		ptr = &transid;
1598b83cc969SLi Zefan 	if (vol_args->flags & BTRFS_SUBVOL_RDONLY)
1599b83cc969SLi Zefan 		readonly = true;
16006f72c7e2SArne Jansen 	if (vol_args->flags & BTRFS_SUBVOL_QGROUP_INHERIT) {
16016f72c7e2SArne Jansen 		if (vol_args->size > PAGE_CACHE_SIZE) {
16026f72c7e2SArne Jansen 			ret = -EINVAL;
16036f72c7e2SArne Jansen 			goto out;
16046f72c7e2SArne Jansen 		}
16056f72c7e2SArne Jansen 		inherit = memdup_user(vol_args->qgroup_inherit, vol_args->size);
16066f72c7e2SArne Jansen 		if (IS_ERR(inherit)) {
16076f72c7e2SArne Jansen 			ret = PTR_ERR(inherit);
16086f72c7e2SArne Jansen 			goto out;
16096f72c7e2SArne Jansen 		}
16106f72c7e2SArne Jansen 	}
161175eaa0e2SSage Weil 
1612fa0d2b9bSLi Zefan 	ret = btrfs_ioctl_snap_create_transid(file, vol_args->name,
16136f72c7e2SArne Jansen 					      vol_args->fd, subvol, ptr,
16148696c533SMiao Xie 					      readonly, inherit);
161575eaa0e2SSage Weil 
1616fdfb1e4fSLi Zefan 	if (ret == 0 && ptr &&
161775eaa0e2SSage Weil 	    copy_to_user(arg +
1618fdfb1e4fSLi Zefan 			 offsetof(struct btrfs_ioctl_vol_args_v2,
1619fdfb1e4fSLi Zefan 				  transid), ptr, sizeof(*ptr)))
162075eaa0e2SSage Weil 		ret = -EFAULT;
1621fdfb1e4fSLi Zefan out:
1622f46b5a66SChristoph Hellwig 	kfree(vol_args);
16236f72c7e2SArne Jansen 	kfree(inherit);
1624f46b5a66SChristoph Hellwig 	return ret;
1625f46b5a66SChristoph Hellwig }
1626f46b5a66SChristoph Hellwig 
16270caa102dSLi Zefan static noinline int btrfs_ioctl_subvol_getflags(struct file *file,
16280caa102dSLi Zefan 						void __user *arg)
16290caa102dSLi Zefan {
1630496ad9aaSAl Viro 	struct inode *inode = file_inode(file);
16310caa102dSLi Zefan 	struct btrfs_root *root = BTRFS_I(inode)->root;
16320caa102dSLi Zefan 	int ret = 0;
16330caa102dSLi Zefan 	u64 flags = 0;
16340caa102dSLi Zefan 
163533345d01SLi Zefan 	if (btrfs_ino(inode) != BTRFS_FIRST_FREE_OBJECTID)
16360caa102dSLi Zefan 		return -EINVAL;
16370caa102dSLi Zefan 
16380caa102dSLi Zefan 	down_read(&root->fs_info->subvol_sem);
16390caa102dSLi Zefan 	if (btrfs_root_readonly(root))
16400caa102dSLi Zefan 		flags |= BTRFS_SUBVOL_RDONLY;
16410caa102dSLi Zefan 	up_read(&root->fs_info->subvol_sem);
16420caa102dSLi Zefan 
16430caa102dSLi Zefan 	if (copy_to_user(arg, &flags, sizeof(flags)))
16440caa102dSLi Zefan 		ret = -EFAULT;
16450caa102dSLi Zefan 
16460caa102dSLi Zefan 	return ret;
16470caa102dSLi Zefan }
16480caa102dSLi Zefan 
16490caa102dSLi Zefan static noinline int btrfs_ioctl_subvol_setflags(struct file *file,
16500caa102dSLi Zefan 					      void __user *arg)
16510caa102dSLi Zefan {
1652496ad9aaSAl Viro 	struct inode *inode = file_inode(file);
16530caa102dSLi Zefan 	struct btrfs_root *root = BTRFS_I(inode)->root;
16540caa102dSLi Zefan 	struct btrfs_trans_handle *trans;
16550caa102dSLi Zefan 	u64 root_flags;
16560caa102dSLi Zefan 	u64 flags;
16570caa102dSLi Zefan 	int ret = 0;
16580caa102dSLi Zefan 
1659b9ca0664SLiu Bo 	ret = mnt_want_write_file(file);
1660b9ca0664SLiu Bo 	if (ret)
1661b9ca0664SLiu Bo 		goto out;
16620caa102dSLi Zefan 
1663b9ca0664SLiu Bo 	if (btrfs_ino(inode) != BTRFS_FIRST_FREE_OBJECTID) {
1664b9ca0664SLiu Bo 		ret = -EINVAL;
1665b9ca0664SLiu Bo 		goto out_drop_write;
1666b9ca0664SLiu Bo 	}
16670caa102dSLi Zefan 
1668b9ca0664SLiu Bo 	if (copy_from_user(&flags, arg, sizeof(flags))) {
1669b9ca0664SLiu Bo 		ret = -EFAULT;
1670b9ca0664SLiu Bo 		goto out_drop_write;
1671b9ca0664SLiu Bo 	}
16720caa102dSLi Zefan 
1673b9ca0664SLiu Bo 	if (flags & BTRFS_SUBVOL_CREATE_ASYNC) {
1674b9ca0664SLiu Bo 		ret = -EINVAL;
1675b9ca0664SLiu Bo 		goto out_drop_write;
1676b9ca0664SLiu Bo 	}
16770caa102dSLi Zefan 
1678b9ca0664SLiu Bo 	if (flags & ~BTRFS_SUBVOL_RDONLY) {
1679b9ca0664SLiu Bo 		ret = -EOPNOTSUPP;
1680b9ca0664SLiu Bo 		goto out_drop_write;
1681b9ca0664SLiu Bo 	}
16820caa102dSLi Zefan 
1683b9ca0664SLiu Bo 	if (!inode_owner_or_capable(inode)) {
1684b9ca0664SLiu Bo 		ret = -EACCES;
1685b9ca0664SLiu Bo 		goto out_drop_write;
1686b9ca0664SLiu Bo 	}
1687b4dc2b8cSLi Zefan 
16880caa102dSLi Zefan 	down_write(&root->fs_info->subvol_sem);
16890caa102dSLi Zefan 
16900caa102dSLi Zefan 	/* nothing to do */
16910caa102dSLi Zefan 	if (!!(flags & BTRFS_SUBVOL_RDONLY) == btrfs_root_readonly(root))
1692b9ca0664SLiu Bo 		goto out_drop_sem;
16930caa102dSLi Zefan 
16940caa102dSLi Zefan 	root_flags = btrfs_root_flags(&root->root_item);
16950caa102dSLi Zefan 	if (flags & BTRFS_SUBVOL_RDONLY)
16960caa102dSLi Zefan 		btrfs_set_root_flags(&root->root_item,
16970caa102dSLi Zefan 				     root_flags | BTRFS_ROOT_SUBVOL_RDONLY);
16980caa102dSLi Zefan 	else
16990caa102dSLi Zefan 		btrfs_set_root_flags(&root->root_item,
17000caa102dSLi Zefan 				     root_flags & ~BTRFS_ROOT_SUBVOL_RDONLY);
17010caa102dSLi Zefan 
17020caa102dSLi Zefan 	trans = btrfs_start_transaction(root, 1);
17030caa102dSLi Zefan 	if (IS_ERR(trans)) {
17040caa102dSLi Zefan 		ret = PTR_ERR(trans);
17050caa102dSLi Zefan 		goto out_reset;
17060caa102dSLi Zefan 	}
17070caa102dSLi Zefan 
1708b4dc2b8cSLi Zefan 	ret = btrfs_update_root(trans, root->fs_info->tree_root,
17090caa102dSLi Zefan 				&root->root_key, &root->root_item);
17100caa102dSLi Zefan 
17110caa102dSLi Zefan 	btrfs_commit_transaction(trans, root);
17120caa102dSLi Zefan out_reset:
17130caa102dSLi Zefan 	if (ret)
17140caa102dSLi Zefan 		btrfs_set_root_flags(&root->root_item, root_flags);
1715b9ca0664SLiu Bo out_drop_sem:
17160caa102dSLi Zefan 	up_write(&root->fs_info->subvol_sem);
1717b9ca0664SLiu Bo out_drop_write:
1718b9ca0664SLiu Bo 	mnt_drop_write_file(file);
1719b9ca0664SLiu Bo out:
17200caa102dSLi Zefan 	return ret;
17210caa102dSLi Zefan }
17220caa102dSLi Zefan 
172376dda93cSYan, Zheng /*
172476dda93cSYan, Zheng  * helper to check if the subvolume references other subvolumes
172576dda93cSYan, Zheng  */
172676dda93cSYan, Zheng static noinline int may_destroy_subvol(struct btrfs_root *root)
172776dda93cSYan, Zheng {
172876dda93cSYan, Zheng 	struct btrfs_path *path;
1729175a2b87SJosef Bacik 	struct btrfs_dir_item *di;
173076dda93cSYan, Zheng 	struct btrfs_key key;
1731175a2b87SJosef Bacik 	u64 dir_id;
173276dda93cSYan, Zheng 	int ret;
173376dda93cSYan, Zheng 
173476dda93cSYan, Zheng 	path = btrfs_alloc_path();
173576dda93cSYan, Zheng 	if (!path)
173676dda93cSYan, Zheng 		return -ENOMEM;
173776dda93cSYan, Zheng 
1738175a2b87SJosef Bacik 	/* Make sure this root isn't set as the default subvol */
1739175a2b87SJosef Bacik 	dir_id = btrfs_super_root_dir(root->fs_info->super_copy);
1740175a2b87SJosef Bacik 	di = btrfs_lookup_dir_item(NULL, root->fs_info->tree_root, path,
1741175a2b87SJosef Bacik 				   dir_id, "default", 7, 0);
1742175a2b87SJosef Bacik 	if (di && !IS_ERR(di)) {
1743175a2b87SJosef Bacik 		btrfs_dir_item_key_to_cpu(path->nodes[0], di, &key);
1744175a2b87SJosef Bacik 		if (key.objectid == root->root_key.objectid) {
1745175a2b87SJosef Bacik 			ret = -ENOTEMPTY;
1746175a2b87SJosef Bacik 			goto out;
1747175a2b87SJosef Bacik 		}
1748175a2b87SJosef Bacik 		btrfs_release_path(path);
1749175a2b87SJosef Bacik 	}
1750175a2b87SJosef Bacik 
175176dda93cSYan, Zheng 	key.objectid = root->root_key.objectid;
175276dda93cSYan, Zheng 	key.type = BTRFS_ROOT_REF_KEY;
175376dda93cSYan, Zheng 	key.offset = (u64)-1;
175476dda93cSYan, Zheng 
175576dda93cSYan, Zheng 	ret = btrfs_search_slot(NULL, root->fs_info->tree_root,
175676dda93cSYan, Zheng 				&key, path, 0, 0);
175776dda93cSYan, Zheng 	if (ret < 0)
175876dda93cSYan, Zheng 		goto out;
175976dda93cSYan, Zheng 	BUG_ON(ret == 0);
176076dda93cSYan, Zheng 
176176dda93cSYan, Zheng 	ret = 0;
176276dda93cSYan, Zheng 	if (path->slots[0] > 0) {
176376dda93cSYan, Zheng 		path->slots[0]--;
176476dda93cSYan, Zheng 		btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
176576dda93cSYan, Zheng 		if (key.objectid == root->root_key.objectid &&
176676dda93cSYan, Zheng 		    key.type == BTRFS_ROOT_REF_KEY)
176776dda93cSYan, Zheng 			ret = -ENOTEMPTY;
176876dda93cSYan, Zheng 	}
176976dda93cSYan, Zheng out:
177076dda93cSYan, Zheng 	btrfs_free_path(path);
177176dda93cSYan, Zheng 	return ret;
177276dda93cSYan, Zheng }
177376dda93cSYan, Zheng 
1774ac8e9819SChris Mason static noinline int key_in_sk(struct btrfs_key *key,
1775ac8e9819SChris Mason 			      struct btrfs_ioctl_search_key *sk)
1776ac8e9819SChris Mason {
1777abc6e134SChris Mason 	struct btrfs_key test;
1778abc6e134SChris Mason 	int ret;
1779abc6e134SChris Mason 
1780abc6e134SChris Mason 	test.objectid = sk->min_objectid;
1781abc6e134SChris Mason 	test.type = sk->min_type;
1782abc6e134SChris Mason 	test.offset = sk->min_offset;
1783abc6e134SChris Mason 
1784abc6e134SChris Mason 	ret = btrfs_comp_cpu_keys(key, &test);
1785abc6e134SChris Mason 	if (ret < 0)
1786ac8e9819SChris Mason 		return 0;
1787abc6e134SChris Mason 
1788abc6e134SChris Mason 	test.objectid = sk->max_objectid;
1789abc6e134SChris Mason 	test.type = sk->max_type;
1790abc6e134SChris Mason 	test.offset = sk->max_offset;
1791abc6e134SChris Mason 
1792abc6e134SChris Mason 	ret = btrfs_comp_cpu_keys(key, &test);
1793abc6e134SChris Mason 	if (ret > 0)
1794ac8e9819SChris Mason 		return 0;
1795ac8e9819SChris Mason 	return 1;
1796ac8e9819SChris Mason }
1797ac8e9819SChris Mason 
1798ac8e9819SChris Mason static noinline int copy_to_sk(struct btrfs_root *root,
1799ac8e9819SChris Mason 			       struct btrfs_path *path,
1800ac8e9819SChris Mason 			       struct btrfs_key *key,
1801ac8e9819SChris Mason 			       struct btrfs_ioctl_search_key *sk,
1802ac8e9819SChris Mason 			       char *buf,
1803ac8e9819SChris Mason 			       unsigned long *sk_offset,
1804ac8e9819SChris Mason 			       int *num_found)
1805ac8e9819SChris Mason {
1806ac8e9819SChris Mason 	u64 found_transid;
1807ac8e9819SChris Mason 	struct extent_buffer *leaf;
1808ac8e9819SChris Mason 	struct btrfs_ioctl_search_header sh;
1809ac8e9819SChris Mason 	unsigned long item_off;
1810ac8e9819SChris Mason 	unsigned long item_len;
1811ac8e9819SChris Mason 	int nritems;
1812ac8e9819SChris Mason 	int i;
1813ac8e9819SChris Mason 	int slot;
1814ac8e9819SChris Mason 	int ret = 0;
1815ac8e9819SChris Mason 
1816ac8e9819SChris Mason 	leaf = path->nodes[0];
1817ac8e9819SChris Mason 	slot = path->slots[0];
1818ac8e9819SChris Mason 	nritems = btrfs_header_nritems(leaf);
1819ac8e9819SChris Mason 
1820ac8e9819SChris Mason 	if (btrfs_header_generation(leaf) > sk->max_transid) {
1821ac8e9819SChris Mason 		i = nritems;
1822ac8e9819SChris Mason 		goto advance_key;
1823ac8e9819SChris Mason 	}
1824ac8e9819SChris Mason 	found_transid = btrfs_header_generation(leaf);
1825ac8e9819SChris Mason 
1826ac8e9819SChris Mason 	for (i = slot; i < nritems; i++) {
1827ac8e9819SChris Mason 		item_off = btrfs_item_ptr_offset(leaf, i);
1828ac8e9819SChris Mason 		item_len = btrfs_item_size_nr(leaf, i);
1829ac8e9819SChris Mason 
183003b71c6cSGabriel de Perthuis 		btrfs_item_key_to_cpu(leaf, key, i);
183103b71c6cSGabriel de Perthuis 		if (!key_in_sk(key, sk))
183203b71c6cSGabriel de Perthuis 			continue;
183303b71c6cSGabriel de Perthuis 
183403b71c6cSGabriel de Perthuis 		if (sizeof(sh) + item_len > BTRFS_SEARCH_ARGS_BUFSIZE)
1835ac8e9819SChris Mason 			item_len = 0;
1836ac8e9819SChris Mason 
1837ac8e9819SChris Mason 		if (sizeof(sh) + item_len + *sk_offset >
1838ac8e9819SChris Mason 		    BTRFS_SEARCH_ARGS_BUFSIZE) {
1839ac8e9819SChris Mason 			ret = 1;
1840ac8e9819SChris Mason 			goto overflow;
1841ac8e9819SChris Mason 		}
1842ac8e9819SChris Mason 
1843ac8e9819SChris Mason 		sh.objectid = key->objectid;
1844ac8e9819SChris Mason 		sh.offset = key->offset;
1845ac8e9819SChris Mason 		sh.type = key->type;
1846ac8e9819SChris Mason 		sh.len = item_len;
1847ac8e9819SChris Mason 		sh.transid = found_transid;
1848ac8e9819SChris Mason 
1849ac8e9819SChris Mason 		/* copy search result header */
1850ac8e9819SChris Mason 		memcpy(buf + *sk_offset, &sh, sizeof(sh));
1851ac8e9819SChris Mason 		*sk_offset += sizeof(sh);
1852ac8e9819SChris Mason 
1853ac8e9819SChris Mason 		if (item_len) {
1854ac8e9819SChris Mason 			char *p = buf + *sk_offset;
1855ac8e9819SChris Mason 			/* copy the item */
1856ac8e9819SChris Mason 			read_extent_buffer(leaf, p,
1857ac8e9819SChris Mason 					   item_off, item_len);
1858ac8e9819SChris Mason 			*sk_offset += item_len;
1859ac8e9819SChris Mason 		}
1860e2156867SHugo Mills 		(*num_found)++;
1861ac8e9819SChris Mason 
1862ac8e9819SChris Mason 		if (*num_found >= sk->nr_items)
1863ac8e9819SChris Mason 			break;
1864ac8e9819SChris Mason 	}
1865ac8e9819SChris Mason advance_key:
1866ac8e9819SChris Mason 	ret = 0;
1867abc6e134SChris Mason 	if (key->offset < (u64)-1 && key->offset < sk->max_offset)
1868abc6e134SChris Mason 		key->offset++;
1869abc6e134SChris Mason 	else if (key->type < (u8)-1 && key->type < sk->max_type) {
1870abc6e134SChris Mason 		key->offset = 0;
1871abc6e134SChris Mason 		key->type++;
1872abc6e134SChris Mason 	} else if (key->objectid < (u64)-1 && key->objectid < sk->max_objectid) {
1873abc6e134SChris Mason 		key->offset = 0;
1874abc6e134SChris Mason 		key->type = 0;
1875abc6e134SChris Mason 		key->objectid++;
1876abc6e134SChris Mason 	} else
1877abc6e134SChris Mason 		ret = 1;
1878ac8e9819SChris Mason overflow:
1879ac8e9819SChris Mason 	return ret;
1880ac8e9819SChris Mason }
1881ac8e9819SChris Mason 
1882ac8e9819SChris Mason static noinline int search_ioctl(struct inode *inode,
1883ac8e9819SChris Mason 				 struct btrfs_ioctl_search_args *args)
1884ac8e9819SChris Mason {
1885ac8e9819SChris Mason 	struct btrfs_root *root;
1886ac8e9819SChris Mason 	struct btrfs_key key;
1887ac8e9819SChris Mason 	struct btrfs_key max_key;
1888ac8e9819SChris Mason 	struct btrfs_path *path;
1889ac8e9819SChris Mason 	struct btrfs_ioctl_search_key *sk = &args->key;
1890ac8e9819SChris Mason 	struct btrfs_fs_info *info = BTRFS_I(inode)->root->fs_info;
1891ac8e9819SChris Mason 	int ret;
1892ac8e9819SChris Mason 	int num_found = 0;
1893ac8e9819SChris Mason 	unsigned long sk_offset = 0;
1894ac8e9819SChris Mason 
1895ac8e9819SChris Mason 	path = btrfs_alloc_path();
1896ac8e9819SChris Mason 	if (!path)
1897ac8e9819SChris Mason 		return -ENOMEM;
1898ac8e9819SChris Mason 
1899ac8e9819SChris Mason 	if (sk->tree_id == 0) {
1900ac8e9819SChris Mason 		/* search the root of the inode that was passed */
1901ac8e9819SChris Mason 		root = BTRFS_I(inode)->root;
1902ac8e9819SChris Mason 	} else {
1903ac8e9819SChris Mason 		key.objectid = sk->tree_id;
1904ac8e9819SChris Mason 		key.type = BTRFS_ROOT_ITEM_KEY;
1905ac8e9819SChris Mason 		key.offset = (u64)-1;
1906ac8e9819SChris Mason 		root = btrfs_read_fs_root_no_name(info, &key);
1907ac8e9819SChris Mason 		if (IS_ERR(root)) {
1908ac8e9819SChris Mason 			printk(KERN_ERR "could not find root %llu\n",
1909ac8e9819SChris Mason 			       sk->tree_id);
1910ac8e9819SChris Mason 			btrfs_free_path(path);
1911ac8e9819SChris Mason 			return -ENOENT;
1912ac8e9819SChris Mason 		}
1913ac8e9819SChris Mason 	}
1914ac8e9819SChris Mason 
1915ac8e9819SChris Mason 	key.objectid = sk->min_objectid;
1916ac8e9819SChris Mason 	key.type = sk->min_type;
1917ac8e9819SChris Mason 	key.offset = sk->min_offset;
1918ac8e9819SChris Mason 
1919ac8e9819SChris Mason 	max_key.objectid = sk->max_objectid;
1920ac8e9819SChris Mason 	max_key.type = sk->max_type;
1921ac8e9819SChris Mason 	max_key.offset = sk->max_offset;
1922ac8e9819SChris Mason 
1923ac8e9819SChris Mason 	path->keep_locks = 1;
1924ac8e9819SChris Mason 
1925ac8e9819SChris Mason 	while(1) {
1926de78b51aSEric Sandeen 		ret = btrfs_search_forward(root, &key, &max_key, path,
1927ac8e9819SChris Mason 					   sk->min_transid);
1928ac8e9819SChris Mason 		if (ret != 0) {
1929ac8e9819SChris Mason 			if (ret > 0)
1930ac8e9819SChris Mason 				ret = 0;
1931ac8e9819SChris Mason 			goto err;
1932ac8e9819SChris Mason 		}
1933ac8e9819SChris Mason 		ret = copy_to_sk(root, path, &key, sk, args->buf,
1934ac8e9819SChris Mason 				 &sk_offset, &num_found);
1935b3b4aa74SDavid Sterba 		btrfs_release_path(path);
1936ac8e9819SChris Mason 		if (ret || num_found >= sk->nr_items)
1937ac8e9819SChris Mason 			break;
1938ac8e9819SChris Mason 
1939ac8e9819SChris Mason 	}
1940ac8e9819SChris Mason 	ret = 0;
1941ac8e9819SChris Mason err:
1942ac8e9819SChris Mason 	sk->nr_items = num_found;
1943ac8e9819SChris Mason 	btrfs_free_path(path);
1944ac8e9819SChris Mason 	return ret;
1945ac8e9819SChris Mason }
1946ac8e9819SChris Mason 
1947ac8e9819SChris Mason static noinline int btrfs_ioctl_tree_search(struct file *file,
1948ac8e9819SChris Mason 					   void __user *argp)
1949ac8e9819SChris Mason {
1950ac8e9819SChris Mason 	 struct btrfs_ioctl_search_args *args;
1951ac8e9819SChris Mason 	 struct inode *inode;
1952ac8e9819SChris Mason 	 int ret;
1953ac8e9819SChris Mason 
1954ac8e9819SChris Mason 	if (!capable(CAP_SYS_ADMIN))
1955ac8e9819SChris Mason 		return -EPERM;
1956ac8e9819SChris Mason 
19572354d08fSJulia Lawall 	args = memdup_user(argp, sizeof(*args));
19582354d08fSJulia Lawall 	if (IS_ERR(args))
19592354d08fSJulia Lawall 		return PTR_ERR(args);
1960ac8e9819SChris Mason 
1961496ad9aaSAl Viro 	inode = file_inode(file);
1962ac8e9819SChris Mason 	ret = search_ioctl(inode, args);
1963ac8e9819SChris Mason 	if (ret == 0 && copy_to_user(argp, args, sizeof(*args)))
1964ac8e9819SChris Mason 		ret = -EFAULT;
1965ac8e9819SChris Mason 	kfree(args);
1966ac8e9819SChris Mason 	return ret;
1967ac8e9819SChris Mason }
1968ac8e9819SChris Mason 
196998d377a0STARUISI Hiroaki /*
1970ac8e9819SChris Mason  * Search INODE_REFs to identify path name of 'dirid' directory
1971ac8e9819SChris Mason  * in a 'tree_id' tree. and sets path name to 'name'.
197298d377a0STARUISI Hiroaki  */
197398d377a0STARUISI Hiroaki static noinline int btrfs_search_path_in_tree(struct btrfs_fs_info *info,
197498d377a0STARUISI Hiroaki 				u64 tree_id, u64 dirid, char *name)
197598d377a0STARUISI Hiroaki {
197698d377a0STARUISI Hiroaki 	struct btrfs_root *root;
197798d377a0STARUISI Hiroaki 	struct btrfs_key key;
1978ac8e9819SChris Mason 	char *ptr;
197998d377a0STARUISI Hiroaki 	int ret = -1;
198098d377a0STARUISI Hiroaki 	int slot;
198198d377a0STARUISI Hiroaki 	int len;
198298d377a0STARUISI Hiroaki 	int total_len = 0;
198398d377a0STARUISI Hiroaki 	struct btrfs_inode_ref *iref;
198498d377a0STARUISI Hiroaki 	struct extent_buffer *l;
198598d377a0STARUISI Hiroaki 	struct btrfs_path *path;
198698d377a0STARUISI Hiroaki 
198798d377a0STARUISI Hiroaki 	if (dirid == BTRFS_FIRST_FREE_OBJECTID) {
198898d377a0STARUISI Hiroaki 		name[0]='\0';
198998d377a0STARUISI Hiroaki 		return 0;
199098d377a0STARUISI Hiroaki 	}
199198d377a0STARUISI Hiroaki 
199298d377a0STARUISI Hiroaki 	path = btrfs_alloc_path();
199398d377a0STARUISI Hiroaki 	if (!path)
199498d377a0STARUISI Hiroaki 		return -ENOMEM;
199598d377a0STARUISI Hiroaki 
1996ac8e9819SChris Mason 	ptr = &name[BTRFS_INO_LOOKUP_PATH_MAX];
199798d377a0STARUISI Hiroaki 
199898d377a0STARUISI Hiroaki 	key.objectid = tree_id;
199998d377a0STARUISI Hiroaki 	key.type = BTRFS_ROOT_ITEM_KEY;
200098d377a0STARUISI Hiroaki 	key.offset = (u64)-1;
200198d377a0STARUISI Hiroaki 	root = btrfs_read_fs_root_no_name(info, &key);
200298d377a0STARUISI Hiroaki 	if (IS_ERR(root)) {
200398d377a0STARUISI Hiroaki 		printk(KERN_ERR "could not find root %llu\n", tree_id);
20048ad6fcabSChris Mason 		ret = -ENOENT;
20058ad6fcabSChris Mason 		goto out;
200698d377a0STARUISI Hiroaki 	}
200798d377a0STARUISI Hiroaki 
200898d377a0STARUISI Hiroaki 	key.objectid = dirid;
200998d377a0STARUISI Hiroaki 	key.type = BTRFS_INODE_REF_KEY;
20108ad6fcabSChris Mason 	key.offset = (u64)-1;
201198d377a0STARUISI Hiroaki 
201298d377a0STARUISI Hiroaki 	while(1) {
201398d377a0STARUISI Hiroaki 		ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
201498d377a0STARUISI Hiroaki 		if (ret < 0)
201598d377a0STARUISI Hiroaki 			goto out;
201698d377a0STARUISI Hiroaki 
201798d377a0STARUISI Hiroaki 		l = path->nodes[0];
201898d377a0STARUISI Hiroaki 		slot = path->slots[0];
20198ad6fcabSChris Mason 		if (ret > 0 && slot > 0)
20208ad6fcabSChris Mason 			slot--;
202198d377a0STARUISI Hiroaki 		btrfs_item_key_to_cpu(l, &key, slot);
202298d377a0STARUISI Hiroaki 
202398d377a0STARUISI Hiroaki 		if (ret > 0 && (key.objectid != dirid ||
2024ac8e9819SChris Mason 				key.type != BTRFS_INODE_REF_KEY)) {
2025ac8e9819SChris Mason 			ret = -ENOENT;
202698d377a0STARUISI Hiroaki 			goto out;
2027ac8e9819SChris Mason 		}
202898d377a0STARUISI Hiroaki 
202998d377a0STARUISI Hiroaki 		iref = btrfs_item_ptr(l, slot, struct btrfs_inode_ref);
203098d377a0STARUISI Hiroaki 		len = btrfs_inode_ref_name_len(l, iref);
203198d377a0STARUISI Hiroaki 		ptr -= len + 1;
203298d377a0STARUISI Hiroaki 		total_len += len + 1;
2033*a696cf35SFilipe David Borba Manana 		if (ptr < name) {
2034*a696cf35SFilipe David Borba Manana 			ret = -ENAMETOOLONG;
203598d377a0STARUISI Hiroaki 			goto out;
2036*a696cf35SFilipe David Borba Manana 		}
203798d377a0STARUISI Hiroaki 
203898d377a0STARUISI Hiroaki 		*(ptr + len) = '/';
203998d377a0STARUISI Hiroaki 		read_extent_buffer(l, ptr,(unsigned long)(iref + 1), len);
204098d377a0STARUISI Hiroaki 
204198d377a0STARUISI Hiroaki 		if (key.offset == BTRFS_FIRST_FREE_OBJECTID)
204298d377a0STARUISI Hiroaki 			break;
204398d377a0STARUISI Hiroaki 
2044b3b4aa74SDavid Sterba 		btrfs_release_path(path);
204598d377a0STARUISI Hiroaki 		key.objectid = key.offset;
20468ad6fcabSChris Mason 		key.offset = (u64)-1;
204798d377a0STARUISI Hiroaki 		dirid = key.objectid;
204898d377a0STARUISI Hiroaki 	}
204977906a50SLi Zefan 	memmove(name, ptr, total_len);
205098d377a0STARUISI Hiroaki 	name[total_len]='\0';
205198d377a0STARUISI Hiroaki 	ret = 0;
205298d377a0STARUISI Hiroaki out:
205398d377a0STARUISI Hiroaki 	btrfs_free_path(path);
2054ac8e9819SChris Mason 	return ret;
2055ac8e9819SChris Mason }
2056ac8e9819SChris Mason 
2057ac8e9819SChris Mason static noinline int btrfs_ioctl_ino_lookup(struct file *file,
2058ac8e9819SChris Mason 					   void __user *argp)
2059ac8e9819SChris Mason {
2060ac8e9819SChris Mason 	 struct btrfs_ioctl_ino_lookup_args *args;
2061ac8e9819SChris Mason 	 struct inode *inode;
2062ac8e9819SChris Mason 	 int ret;
2063ac8e9819SChris Mason 
2064ac8e9819SChris Mason 	if (!capable(CAP_SYS_ADMIN))
2065ac8e9819SChris Mason 		return -EPERM;
2066ac8e9819SChris Mason 
20672354d08fSJulia Lawall 	args = memdup_user(argp, sizeof(*args));
20682354d08fSJulia Lawall 	if (IS_ERR(args))
20692354d08fSJulia Lawall 		return PTR_ERR(args);
2070c2b96929SDan Carpenter 
2071496ad9aaSAl Viro 	inode = file_inode(file);
2072ac8e9819SChris Mason 
20731b53ac4dSChris Mason 	if (args->treeid == 0)
20741b53ac4dSChris Mason 		args->treeid = BTRFS_I(inode)->root->root_key.objectid;
20751b53ac4dSChris Mason 
2076ac8e9819SChris Mason 	ret = btrfs_search_path_in_tree(BTRFS_I(inode)->root->fs_info,
2077ac8e9819SChris Mason 					args->treeid, args->objectid,
2078ac8e9819SChris Mason 					args->name);
2079ac8e9819SChris Mason 
2080ac8e9819SChris Mason 	if (ret == 0 && copy_to_user(argp, args, sizeof(*args)))
2081ac8e9819SChris Mason 		ret = -EFAULT;
2082ac8e9819SChris Mason 
2083ac8e9819SChris Mason 	kfree(args);
208498d377a0STARUISI Hiroaki 	return ret;
208598d377a0STARUISI Hiroaki }
208698d377a0STARUISI Hiroaki 
208776dda93cSYan, Zheng static noinline int btrfs_ioctl_snap_destroy(struct file *file,
208876dda93cSYan, Zheng 					     void __user *arg)
208976dda93cSYan, Zheng {
209076dda93cSYan, Zheng 	struct dentry *parent = fdentry(file);
209176dda93cSYan, Zheng 	struct dentry *dentry;
209276dda93cSYan, Zheng 	struct inode *dir = parent->d_inode;
209376dda93cSYan, Zheng 	struct inode *inode;
209476dda93cSYan, Zheng 	struct btrfs_root *root = BTRFS_I(dir)->root;
209576dda93cSYan, Zheng 	struct btrfs_root *dest = NULL;
209676dda93cSYan, Zheng 	struct btrfs_ioctl_vol_args *vol_args;
209776dda93cSYan, Zheng 	struct btrfs_trans_handle *trans;
2098c58aaad2SMiao Xie 	struct btrfs_block_rsv block_rsv;
2099c58aaad2SMiao Xie 	u64 qgroup_reserved;
210076dda93cSYan, Zheng 	int namelen;
210176dda93cSYan, Zheng 	int ret;
210276dda93cSYan, Zheng 	int err = 0;
210376dda93cSYan, Zheng 
210476dda93cSYan, Zheng 	vol_args = memdup_user(arg, sizeof(*vol_args));
210576dda93cSYan, Zheng 	if (IS_ERR(vol_args))
210676dda93cSYan, Zheng 		return PTR_ERR(vol_args);
210776dda93cSYan, Zheng 
210876dda93cSYan, Zheng 	vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
210976dda93cSYan, Zheng 	namelen = strlen(vol_args->name);
211076dda93cSYan, Zheng 	if (strchr(vol_args->name, '/') ||
211176dda93cSYan, Zheng 	    strncmp(vol_args->name, "..", namelen) == 0) {
211276dda93cSYan, Zheng 		err = -EINVAL;
211376dda93cSYan, Zheng 		goto out;
211476dda93cSYan, Zheng 	}
211576dda93cSYan, Zheng 
2116a561be71SAl Viro 	err = mnt_want_write_file(file);
211776dda93cSYan, Zheng 	if (err)
211876dda93cSYan, Zheng 		goto out;
211976dda93cSYan, Zheng 
21205c50c9b8SDavid Sterba 	err = mutex_lock_killable_nested(&dir->i_mutex, I_MUTEX_PARENT);
21215c50c9b8SDavid Sterba 	if (err == -EINTR)
21225c50c9b8SDavid Sterba 		goto out;
212376dda93cSYan, Zheng 	dentry = lookup_one_len(vol_args->name, parent, namelen);
212476dda93cSYan, Zheng 	if (IS_ERR(dentry)) {
212576dda93cSYan, Zheng 		err = PTR_ERR(dentry);
212676dda93cSYan, Zheng 		goto out_unlock_dir;
212776dda93cSYan, Zheng 	}
212876dda93cSYan, Zheng 
212976dda93cSYan, Zheng 	if (!dentry->d_inode) {
213076dda93cSYan, Zheng 		err = -ENOENT;
213176dda93cSYan, Zheng 		goto out_dput;
213276dda93cSYan, Zheng 	}
213376dda93cSYan, Zheng 
213476dda93cSYan, Zheng 	inode = dentry->d_inode;
21354260f7c7SSage Weil 	dest = BTRFS_I(inode)->root;
21364260f7c7SSage Weil 	if (!capable(CAP_SYS_ADMIN)){
21374260f7c7SSage Weil 		/*
21384260f7c7SSage Weil 		 * Regular user.  Only allow this with a special mount
21394260f7c7SSage Weil 		 * option, when the user has write+exec access to the
21404260f7c7SSage Weil 		 * subvol root, and when rmdir(2) would have been
21414260f7c7SSage Weil 		 * allowed.
21424260f7c7SSage Weil 		 *
21434260f7c7SSage Weil 		 * Note that this is _not_ check that the subvol is
21444260f7c7SSage Weil 		 * empty or doesn't contain data that we wouldn't
21454260f7c7SSage Weil 		 * otherwise be able to delete.
21464260f7c7SSage Weil 		 *
21474260f7c7SSage Weil 		 * Users who want to delete empty subvols should try
21484260f7c7SSage Weil 		 * rmdir(2).
21494260f7c7SSage Weil 		 */
21504260f7c7SSage Weil 		err = -EPERM;
21514260f7c7SSage Weil 		if (!btrfs_test_opt(root, USER_SUBVOL_RM_ALLOWED))
21524260f7c7SSage Weil 			goto out_dput;
21534260f7c7SSage Weil 
21544260f7c7SSage Weil 		/*
21554260f7c7SSage Weil 		 * Do not allow deletion if the parent dir is the same
21564260f7c7SSage Weil 		 * as the dir to be deleted.  That means the ioctl
21574260f7c7SSage Weil 		 * must be called on the dentry referencing the root
21584260f7c7SSage Weil 		 * of the subvol, not a random directory contained
21594260f7c7SSage Weil 		 * within it.
21604260f7c7SSage Weil 		 */
21614260f7c7SSage Weil 		err = -EINVAL;
21624260f7c7SSage Weil 		if (root == dest)
21634260f7c7SSage Weil 			goto out_dput;
21644260f7c7SSage Weil 
21654260f7c7SSage Weil 		err = inode_permission(inode, MAY_WRITE | MAY_EXEC);
21664260f7c7SSage Weil 		if (err)
21674260f7c7SSage Weil 			goto out_dput;
21685c39da5bSMiao Xie 	}
21694260f7c7SSage Weil 
21705c39da5bSMiao Xie 	/* check if subvolume may be deleted by a user */
21714260f7c7SSage Weil 	err = btrfs_may_delete(dir, dentry, 1);
21724260f7c7SSage Weil 	if (err)
21734260f7c7SSage Weil 		goto out_dput;
21744260f7c7SSage Weil 
217533345d01SLi Zefan 	if (btrfs_ino(inode) != BTRFS_FIRST_FREE_OBJECTID) {
217676dda93cSYan, Zheng 		err = -EINVAL;
217776dda93cSYan, Zheng 		goto out_dput;
217876dda93cSYan, Zheng 	}
217976dda93cSYan, Zheng 
218076dda93cSYan, Zheng 	mutex_lock(&inode->i_mutex);
218176dda93cSYan, Zheng 	err = d_invalidate(dentry);
218276dda93cSYan, Zheng 	if (err)
218376dda93cSYan, Zheng 		goto out_unlock;
218476dda93cSYan, Zheng 
218576dda93cSYan, Zheng 	down_write(&root->fs_info->subvol_sem);
218676dda93cSYan, Zheng 
218776dda93cSYan, Zheng 	err = may_destroy_subvol(dest);
218876dda93cSYan, Zheng 	if (err)
218976dda93cSYan, Zheng 		goto out_up_write;
219076dda93cSYan, Zheng 
2191c58aaad2SMiao Xie 	btrfs_init_block_rsv(&block_rsv, BTRFS_BLOCK_RSV_TEMP);
2192c58aaad2SMiao Xie 	/*
2193c58aaad2SMiao Xie 	 * One for dir inode, two for dir entries, two for root
2194c58aaad2SMiao Xie 	 * ref/backref.
2195c58aaad2SMiao Xie 	 */
2196c58aaad2SMiao Xie 	err = btrfs_subvolume_reserve_metadata(root, &block_rsv,
2197ee3441b4SJeff Mahoney 					       5, &qgroup_reserved, true);
2198c58aaad2SMiao Xie 	if (err)
2199c58aaad2SMiao Xie 		goto out_up_write;
2200c58aaad2SMiao Xie 
2201a22285a6SYan, Zheng 	trans = btrfs_start_transaction(root, 0);
2202a22285a6SYan, Zheng 	if (IS_ERR(trans)) {
2203a22285a6SYan, Zheng 		err = PTR_ERR(trans);
2204c58aaad2SMiao Xie 		goto out_release;
2205a22285a6SYan, Zheng 	}
2206c58aaad2SMiao Xie 	trans->block_rsv = &block_rsv;
2207c58aaad2SMiao Xie 	trans->bytes_reserved = block_rsv.size;
2208a22285a6SYan, Zheng 
220976dda93cSYan, Zheng 	ret = btrfs_unlink_subvol(trans, root, dir,
221076dda93cSYan, Zheng 				dest->root_key.objectid,
221176dda93cSYan, Zheng 				dentry->d_name.name,
221276dda93cSYan, Zheng 				dentry->d_name.len);
221379787eaaSJeff Mahoney 	if (ret) {
221479787eaaSJeff Mahoney 		err = ret;
221579787eaaSJeff Mahoney 		btrfs_abort_transaction(trans, root, ret);
221679787eaaSJeff Mahoney 		goto out_end_trans;
221779787eaaSJeff Mahoney 	}
221876dda93cSYan, Zheng 
221976dda93cSYan, Zheng 	btrfs_record_root_in_trans(trans, dest);
222076dda93cSYan, Zheng 
222176dda93cSYan, Zheng 	memset(&dest->root_item.drop_progress, 0,
222276dda93cSYan, Zheng 		sizeof(dest->root_item.drop_progress));
222376dda93cSYan, Zheng 	dest->root_item.drop_level = 0;
222476dda93cSYan, Zheng 	btrfs_set_root_refs(&dest->root_item, 0);
222576dda93cSYan, Zheng 
2226d68fc57bSYan, Zheng 	if (!xchg(&dest->orphan_item_inserted, 1)) {
222776dda93cSYan, Zheng 		ret = btrfs_insert_orphan_item(trans,
222876dda93cSYan, Zheng 					root->fs_info->tree_root,
222976dda93cSYan, Zheng 					dest->root_key.objectid);
223079787eaaSJeff Mahoney 		if (ret) {
223179787eaaSJeff Mahoney 			btrfs_abort_transaction(trans, root, ret);
223279787eaaSJeff Mahoney 			err = ret;
223379787eaaSJeff Mahoney 			goto out_end_trans;
2234d68fc57bSYan, Zheng 		}
223579787eaaSJeff Mahoney 	}
223679787eaaSJeff Mahoney out_end_trans:
2237c58aaad2SMiao Xie 	trans->block_rsv = NULL;
2238c58aaad2SMiao Xie 	trans->bytes_reserved = 0;
2239531cb13fSSage Weil 	ret = btrfs_end_transaction(trans, root);
224079787eaaSJeff Mahoney 	if (ret && !err)
224179787eaaSJeff Mahoney 		err = ret;
224276dda93cSYan, Zheng 	inode->i_flags |= S_DEAD;
2243c58aaad2SMiao Xie out_release:
2244c58aaad2SMiao Xie 	btrfs_subvolume_release_metadata(root, &block_rsv, qgroup_reserved);
224576dda93cSYan, Zheng out_up_write:
224676dda93cSYan, Zheng 	up_write(&root->fs_info->subvol_sem);
224776dda93cSYan, Zheng out_unlock:
224876dda93cSYan, Zheng 	mutex_unlock(&inode->i_mutex);
224976dda93cSYan, Zheng 	if (!err) {
2250efefb143SYan, Zheng 		shrink_dcache_sb(root->fs_info->sb);
225176dda93cSYan, Zheng 		btrfs_invalidate_inodes(dest);
225276dda93cSYan, Zheng 		d_delete(dentry);
2253fa6ac876SLiu Bo 
2254fa6ac876SLiu Bo 		/* the last ref */
2255fa6ac876SLiu Bo 		if (dest->cache_inode) {
2256fa6ac876SLiu Bo 			iput(dest->cache_inode);
2257fa6ac876SLiu Bo 			dest->cache_inode = NULL;
2258fa6ac876SLiu Bo 		}
225976dda93cSYan, Zheng 	}
226076dda93cSYan, Zheng out_dput:
226176dda93cSYan, Zheng 	dput(dentry);
226276dda93cSYan, Zheng out_unlock_dir:
226376dda93cSYan, Zheng 	mutex_unlock(&dir->i_mutex);
22642a79f17eSAl Viro 	mnt_drop_write_file(file);
226576dda93cSYan, Zheng out:
226676dda93cSYan, Zheng 	kfree(vol_args);
226776dda93cSYan, Zheng 	return err;
226876dda93cSYan, Zheng }
226976dda93cSYan, Zheng 
22701e701a32SChris Mason static int btrfs_ioctl_defrag(struct file *file, void __user *argp)
2271f46b5a66SChristoph Hellwig {
2272496ad9aaSAl Viro 	struct inode *inode = file_inode(file);
2273f46b5a66SChristoph Hellwig 	struct btrfs_root *root = BTRFS_I(inode)->root;
22741e701a32SChris Mason 	struct btrfs_ioctl_defrag_range_args *range;
2275c146afadSYan Zheng 	int ret;
2276c146afadSYan Zheng 
227725122d15SIlya Dryomov 	ret = mnt_want_write_file(file);
227825122d15SIlya Dryomov 	if (ret)
227925122d15SIlya Dryomov 		return ret;
2280b83cc969SLi Zefan 
228125122d15SIlya Dryomov 	if (btrfs_root_readonly(root)) {
228225122d15SIlya Dryomov 		ret = -EROFS;
228325122d15SIlya Dryomov 		goto out;
22845ac00addSStefan Behrens 	}
2285f46b5a66SChristoph Hellwig 
2286f46b5a66SChristoph Hellwig 	switch (inode->i_mode & S_IFMT) {
2287f46b5a66SChristoph Hellwig 	case S_IFDIR:
2288e441d54dSChris Mason 		if (!capable(CAP_SYS_ADMIN)) {
2289e441d54dSChris Mason 			ret = -EPERM;
2290e441d54dSChris Mason 			goto out;
2291e441d54dSChris Mason 		}
2292de78b51aSEric Sandeen 		ret = btrfs_defrag_root(root);
22938929ecfaSYan, Zheng 		if (ret)
22948929ecfaSYan, Zheng 			goto out;
2295de78b51aSEric Sandeen 		ret = btrfs_defrag_root(root->fs_info->extent_root);
2296f46b5a66SChristoph Hellwig 		break;
2297f46b5a66SChristoph Hellwig 	case S_IFREG:
2298e441d54dSChris Mason 		if (!(file->f_mode & FMODE_WRITE)) {
2299e441d54dSChris Mason 			ret = -EINVAL;
2300e441d54dSChris Mason 			goto out;
2301e441d54dSChris Mason 		}
23021e701a32SChris Mason 
23031e701a32SChris Mason 		range = kzalloc(sizeof(*range), GFP_KERNEL);
23041e701a32SChris Mason 		if (!range) {
23051e701a32SChris Mason 			ret = -ENOMEM;
23061e701a32SChris Mason 			goto out;
23071e701a32SChris Mason 		}
23081e701a32SChris Mason 
23091e701a32SChris Mason 		if (argp) {
23101e701a32SChris Mason 			if (copy_from_user(range, argp,
23111e701a32SChris Mason 					   sizeof(*range))) {
23121e701a32SChris Mason 				ret = -EFAULT;
23131e701a32SChris Mason 				kfree(range);
2314683be16eSDan Carpenter 				goto out;
23151e701a32SChris Mason 			}
23161e701a32SChris Mason 			/* compression requires us to start the IO */
23171e701a32SChris Mason 			if ((range->flags & BTRFS_DEFRAG_RANGE_COMPRESS)) {
23181e701a32SChris Mason 				range->flags |= BTRFS_DEFRAG_RANGE_START_IO;
23191e701a32SChris Mason 				range->extent_thresh = (u32)-1;
23201e701a32SChris Mason 			}
23211e701a32SChris Mason 		} else {
23221e701a32SChris Mason 			/* the rest are all set to zero by kzalloc */
23231e701a32SChris Mason 			range->len = (u64)-1;
23241e701a32SChris Mason 		}
2325496ad9aaSAl Viro 		ret = btrfs_defrag_file(file_inode(file), file,
23264cb5300bSChris Mason 					range, 0, 0);
23274cb5300bSChris Mason 		if (ret > 0)
23284cb5300bSChris Mason 			ret = 0;
23291e701a32SChris Mason 		kfree(range);
2330f46b5a66SChristoph Hellwig 		break;
23318929ecfaSYan, Zheng 	default:
23328929ecfaSYan, Zheng 		ret = -EINVAL;
2333f46b5a66SChristoph Hellwig 	}
2334e441d54dSChris Mason out:
233525122d15SIlya Dryomov 	mnt_drop_write_file(file);
2336e441d54dSChris Mason 	return ret;
2337f46b5a66SChristoph Hellwig }
2338f46b5a66SChristoph Hellwig 
2339b2950863SChristoph Hellwig static long btrfs_ioctl_add_dev(struct btrfs_root *root, void __user *arg)
2340f46b5a66SChristoph Hellwig {
2341f46b5a66SChristoph Hellwig 	struct btrfs_ioctl_vol_args *vol_args;
2342f46b5a66SChristoph Hellwig 	int ret;
2343f46b5a66SChristoph Hellwig 
2344e441d54dSChris Mason 	if (!capable(CAP_SYS_ADMIN))
2345e441d54dSChris Mason 		return -EPERM;
2346e441d54dSChris Mason 
23475ac00addSStefan Behrens 	if (atomic_xchg(&root->fs_info->mutually_exclusive_operation_running,
23485ac00addSStefan Behrens 			1)) {
23495ac00addSStefan Behrens 		pr_info("btrfs: dev add/delete/balance/replace/resize operation in progress\n");
23502c0c9da0SIlya Dryomov 		return -EINVAL;
2351c9e9f97bSIlya Dryomov 	}
2352c9e9f97bSIlya Dryomov 
23535ac00addSStefan Behrens 	mutex_lock(&root->fs_info->volume_mutex);
2354dae7b665SLi Zefan 	vol_args = memdup_user(arg, sizeof(*vol_args));
2355c9e9f97bSIlya Dryomov 	if (IS_ERR(vol_args)) {
2356c9e9f97bSIlya Dryomov 		ret = PTR_ERR(vol_args);
2357c9e9f97bSIlya Dryomov 		goto out;
2358c9e9f97bSIlya Dryomov 	}
2359f46b5a66SChristoph Hellwig 
23605516e595SMark Fasheh 	vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
2361f46b5a66SChristoph Hellwig 	ret = btrfs_init_new_device(root, vol_args->name);
2362f46b5a66SChristoph Hellwig 
2363f46b5a66SChristoph Hellwig 	kfree(vol_args);
2364c9e9f97bSIlya Dryomov out:
2365c9e9f97bSIlya Dryomov 	mutex_unlock(&root->fs_info->volume_mutex);
23665ac00addSStefan Behrens 	atomic_set(&root->fs_info->mutually_exclusive_operation_running, 0);
2367f46b5a66SChristoph Hellwig 	return ret;
2368f46b5a66SChristoph Hellwig }
2369f46b5a66SChristoph Hellwig 
2370da24927bSMiao Xie static long btrfs_ioctl_rm_dev(struct file *file, void __user *arg)
2371f46b5a66SChristoph Hellwig {
2372496ad9aaSAl Viro 	struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
2373f46b5a66SChristoph Hellwig 	struct btrfs_ioctl_vol_args *vol_args;
2374f46b5a66SChristoph Hellwig 	int ret;
2375f46b5a66SChristoph Hellwig 
2376e441d54dSChris Mason 	if (!capable(CAP_SYS_ADMIN))
2377e441d54dSChris Mason 		return -EPERM;
2378e441d54dSChris Mason 
2379da24927bSMiao Xie 	ret = mnt_want_write_file(file);
2380da24927bSMiao Xie 	if (ret)
2381da24927bSMiao Xie 		return ret;
2382c146afadSYan Zheng 
2383dae7b665SLi Zefan 	vol_args = memdup_user(arg, sizeof(*vol_args));
2384c9e9f97bSIlya Dryomov 	if (IS_ERR(vol_args)) {
2385c9e9f97bSIlya Dryomov 		ret = PTR_ERR(vol_args);
2386c9e9f97bSIlya Dryomov 		goto out;
2387c9e9f97bSIlya Dryomov 	}
2388f46b5a66SChristoph Hellwig 
23895516e595SMark Fasheh 	vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
2390f46b5a66SChristoph Hellwig 
2391183860f6SAnand Jain 	if (atomic_xchg(&root->fs_info->mutually_exclusive_operation_running,
2392183860f6SAnand Jain 			1)) {
2393183860f6SAnand Jain 		ret = BTRFS_ERROR_DEV_EXCL_RUN_IN_PROGRESS;
2394183860f6SAnand Jain 		goto out;
2395183860f6SAnand Jain 	}
2396183860f6SAnand Jain 
2397183860f6SAnand Jain 	mutex_lock(&root->fs_info->volume_mutex);
2398183860f6SAnand Jain 	ret = btrfs_rm_device(root, vol_args->name);
2399c9e9f97bSIlya Dryomov 	mutex_unlock(&root->fs_info->volume_mutex);
24005ac00addSStefan Behrens 	atomic_set(&root->fs_info->mutually_exclusive_operation_running, 0);
2401183860f6SAnand Jain 
2402183860f6SAnand Jain out:
2403183860f6SAnand Jain 	kfree(vol_args);
24044ac20c70SIlya Dryomov 	mnt_drop_write_file(file);
2405f46b5a66SChristoph Hellwig 	return ret;
2406f46b5a66SChristoph Hellwig }
2407f46b5a66SChristoph Hellwig 
2408475f6387SJan Schmidt static long btrfs_ioctl_fs_info(struct btrfs_root *root, void __user *arg)
2409475f6387SJan Schmidt {
2410027ed2f0SLi Zefan 	struct btrfs_ioctl_fs_info_args *fi_args;
2411475f6387SJan Schmidt 	struct btrfs_device *device;
2412475f6387SJan Schmidt 	struct btrfs_device *next;
2413475f6387SJan Schmidt 	struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices;
2414027ed2f0SLi Zefan 	int ret = 0;
2415475f6387SJan Schmidt 
2416475f6387SJan Schmidt 	if (!capable(CAP_SYS_ADMIN))
2417475f6387SJan Schmidt 		return -EPERM;
2418475f6387SJan Schmidt 
2419027ed2f0SLi Zefan 	fi_args = kzalloc(sizeof(*fi_args), GFP_KERNEL);
2420027ed2f0SLi Zefan 	if (!fi_args)
2421027ed2f0SLi Zefan 		return -ENOMEM;
2422027ed2f0SLi Zefan 
2423027ed2f0SLi Zefan 	fi_args->num_devices = fs_devices->num_devices;
2424027ed2f0SLi Zefan 	memcpy(&fi_args->fsid, root->fs_info->fsid, sizeof(fi_args->fsid));
2425475f6387SJan Schmidt 
2426475f6387SJan Schmidt 	mutex_lock(&fs_devices->device_list_mutex);
2427475f6387SJan Schmidt 	list_for_each_entry_safe(device, next, &fs_devices->devices, dev_list) {
2428027ed2f0SLi Zefan 		if (device->devid > fi_args->max_id)
2429027ed2f0SLi Zefan 			fi_args->max_id = device->devid;
2430475f6387SJan Schmidt 	}
2431475f6387SJan Schmidt 	mutex_unlock(&fs_devices->device_list_mutex);
2432475f6387SJan Schmidt 
2433027ed2f0SLi Zefan 	if (copy_to_user(arg, fi_args, sizeof(*fi_args)))
2434027ed2f0SLi Zefan 		ret = -EFAULT;
2435475f6387SJan Schmidt 
2436027ed2f0SLi Zefan 	kfree(fi_args);
2437027ed2f0SLi Zefan 	return ret;
2438475f6387SJan Schmidt }
2439475f6387SJan Schmidt 
2440475f6387SJan Schmidt static long btrfs_ioctl_dev_info(struct btrfs_root *root, void __user *arg)
2441475f6387SJan Schmidt {
2442475f6387SJan Schmidt 	struct btrfs_ioctl_dev_info_args *di_args;
2443475f6387SJan Schmidt 	struct btrfs_device *dev;
2444475f6387SJan Schmidt 	struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices;
2445475f6387SJan Schmidt 	int ret = 0;
2446475f6387SJan Schmidt 	char *s_uuid = NULL;
2447475f6387SJan Schmidt 	char empty_uuid[BTRFS_UUID_SIZE] = {0};
2448475f6387SJan Schmidt 
2449475f6387SJan Schmidt 	if (!capable(CAP_SYS_ADMIN))
2450475f6387SJan Schmidt 		return -EPERM;
2451475f6387SJan Schmidt 
2452475f6387SJan Schmidt 	di_args = memdup_user(arg, sizeof(*di_args));
2453475f6387SJan Schmidt 	if (IS_ERR(di_args))
2454475f6387SJan Schmidt 		return PTR_ERR(di_args);
2455475f6387SJan Schmidt 
2456475f6387SJan Schmidt 	if (memcmp(empty_uuid, di_args->uuid, BTRFS_UUID_SIZE) != 0)
2457475f6387SJan Schmidt 		s_uuid = di_args->uuid;
2458475f6387SJan Schmidt 
2459475f6387SJan Schmidt 	mutex_lock(&fs_devices->device_list_mutex);
2460aa1b8cd4SStefan Behrens 	dev = btrfs_find_device(root->fs_info, di_args->devid, s_uuid, NULL);
2461475f6387SJan Schmidt 
2462475f6387SJan Schmidt 	if (!dev) {
2463475f6387SJan Schmidt 		ret = -ENODEV;
2464475f6387SJan Schmidt 		goto out;
2465475f6387SJan Schmidt 	}
2466475f6387SJan Schmidt 
2467475f6387SJan Schmidt 	di_args->devid = dev->devid;
2468475f6387SJan Schmidt 	di_args->bytes_used = dev->bytes_used;
2469475f6387SJan Schmidt 	di_args->total_bytes = dev->total_bytes;
2470475f6387SJan Schmidt 	memcpy(di_args->uuid, dev->uuid, sizeof(di_args->uuid));
2471a27202fbSJim Meyering 	if (dev->name) {
2472606686eeSJosef Bacik 		struct rcu_string *name;
2473606686eeSJosef Bacik 
2474606686eeSJosef Bacik 		rcu_read_lock();
2475606686eeSJosef Bacik 		name = rcu_dereference(dev->name);
2476606686eeSJosef Bacik 		strncpy(di_args->path, name->str, sizeof(di_args->path));
2477606686eeSJosef Bacik 		rcu_read_unlock();
2478a27202fbSJim Meyering 		di_args->path[sizeof(di_args->path) - 1] = 0;
2479a27202fbSJim Meyering 	} else {
248099ba55adSStefan Behrens 		di_args->path[0] = '\0';
2481a27202fbSJim Meyering 	}
2482475f6387SJan Schmidt 
2483475f6387SJan Schmidt out:
248455793c0dSDavid Sterba 	mutex_unlock(&fs_devices->device_list_mutex);
2485475f6387SJan Schmidt 	if (ret == 0 && copy_to_user(arg, di_args, sizeof(*di_args)))
2486475f6387SJan Schmidt 		ret = -EFAULT;
2487475f6387SJan Schmidt 
2488475f6387SJan Schmidt 	kfree(di_args);
2489475f6387SJan Schmidt 	return ret;
2490475f6387SJan Schmidt }
2491475f6387SJan Schmidt 
2492416161dbSMark Fasheh static struct page *extent_same_get_page(struct inode *inode, u64 off)
2493416161dbSMark Fasheh {
2494416161dbSMark Fasheh 	struct page *page;
2495416161dbSMark Fasheh 	pgoff_t index;
2496416161dbSMark Fasheh 	struct extent_io_tree *tree = &BTRFS_I(inode)->io_tree;
2497416161dbSMark Fasheh 
2498416161dbSMark Fasheh 	index = off >> PAGE_CACHE_SHIFT;
2499416161dbSMark Fasheh 
2500416161dbSMark Fasheh 	page = grab_cache_page(inode->i_mapping, index);
2501416161dbSMark Fasheh 	if (!page)
2502416161dbSMark Fasheh 		return NULL;
2503416161dbSMark Fasheh 
2504416161dbSMark Fasheh 	if (!PageUptodate(page)) {
2505416161dbSMark Fasheh 		if (extent_read_full_page_nolock(tree, page, btrfs_get_extent,
2506416161dbSMark Fasheh 						 0))
2507416161dbSMark Fasheh 			return NULL;
2508416161dbSMark Fasheh 		lock_page(page);
2509416161dbSMark Fasheh 		if (!PageUptodate(page)) {
2510416161dbSMark Fasheh 			unlock_page(page);
2511416161dbSMark Fasheh 			page_cache_release(page);
2512416161dbSMark Fasheh 			return NULL;
2513416161dbSMark Fasheh 		}
2514416161dbSMark Fasheh 	}
2515416161dbSMark Fasheh 	unlock_page(page);
2516416161dbSMark Fasheh 
2517416161dbSMark Fasheh 	return page;
2518416161dbSMark Fasheh }
2519416161dbSMark Fasheh 
252077fe20dcSMark Fasheh static inline void lock_extent_range(struct inode *inode, u64 off, u64 len)
252177fe20dcSMark Fasheh {
252277fe20dcSMark Fasheh 	/* do any pending delalloc/csum calc on src, one way or
252377fe20dcSMark Fasheh 	   another, and lock file content */
252477fe20dcSMark Fasheh 	while (1) {
252577fe20dcSMark Fasheh 		struct btrfs_ordered_extent *ordered;
252677fe20dcSMark Fasheh 		lock_extent(&BTRFS_I(inode)->io_tree, off, off + len - 1);
252777fe20dcSMark Fasheh 		ordered = btrfs_lookup_first_ordered_extent(inode,
252877fe20dcSMark Fasheh 							    off + len - 1);
252977fe20dcSMark Fasheh 		if (!ordered &&
253077fe20dcSMark Fasheh 		    !test_range_bit(&BTRFS_I(inode)->io_tree, off,
253177fe20dcSMark Fasheh 				    off + len - 1, EXTENT_DELALLOC, 0, NULL))
253277fe20dcSMark Fasheh 			break;
253377fe20dcSMark Fasheh 		unlock_extent(&BTRFS_I(inode)->io_tree, off, off + len - 1);
253477fe20dcSMark Fasheh 		if (ordered)
253577fe20dcSMark Fasheh 			btrfs_put_ordered_extent(ordered);
253677fe20dcSMark Fasheh 		btrfs_wait_ordered_range(inode, off, len);
253777fe20dcSMark Fasheh 	}
253877fe20dcSMark Fasheh }
253977fe20dcSMark Fasheh 
2540416161dbSMark Fasheh static void btrfs_double_unlock(struct inode *inode1, u64 loff1,
2541416161dbSMark Fasheh 				struct inode *inode2, u64 loff2, u64 len)
2542416161dbSMark Fasheh {
2543416161dbSMark Fasheh 	unlock_extent(&BTRFS_I(inode1)->io_tree, loff1, loff1 + len - 1);
2544416161dbSMark Fasheh 	unlock_extent(&BTRFS_I(inode2)->io_tree, loff2, loff2 + len - 1);
2545416161dbSMark Fasheh 
2546416161dbSMark Fasheh 	mutex_unlock(&inode1->i_mutex);
2547416161dbSMark Fasheh 	mutex_unlock(&inode2->i_mutex);
2548416161dbSMark Fasheh }
2549416161dbSMark Fasheh 
2550416161dbSMark Fasheh static void btrfs_double_lock(struct inode *inode1, u64 loff1,
2551416161dbSMark Fasheh 			      struct inode *inode2, u64 loff2, u64 len)
2552416161dbSMark Fasheh {
2553416161dbSMark Fasheh 	if (inode1 < inode2) {
2554416161dbSMark Fasheh 		swap(inode1, inode2);
2555416161dbSMark Fasheh 		swap(loff1, loff2);
2556416161dbSMark Fasheh 	}
2557416161dbSMark Fasheh 
2558416161dbSMark Fasheh 	mutex_lock_nested(&inode1->i_mutex, I_MUTEX_PARENT);
2559416161dbSMark Fasheh 	lock_extent_range(inode1, loff1, len);
2560416161dbSMark Fasheh 	if (inode1 != inode2) {
2561416161dbSMark Fasheh 		mutex_lock_nested(&inode2->i_mutex, I_MUTEX_CHILD);
2562416161dbSMark Fasheh 		lock_extent_range(inode2, loff2, len);
2563416161dbSMark Fasheh 	}
2564416161dbSMark Fasheh }
2565416161dbSMark Fasheh 
2566416161dbSMark Fasheh static int btrfs_cmp_data(struct inode *src, u64 loff, struct inode *dst,
2567416161dbSMark Fasheh 			  u64 dst_loff, u64 len)
2568416161dbSMark Fasheh {
2569416161dbSMark Fasheh 	int ret = 0;
2570416161dbSMark Fasheh 	struct page *src_page, *dst_page;
2571416161dbSMark Fasheh 	unsigned int cmp_len = PAGE_CACHE_SIZE;
2572416161dbSMark Fasheh 	void *addr, *dst_addr;
2573416161dbSMark Fasheh 
2574416161dbSMark Fasheh 	while (len) {
2575416161dbSMark Fasheh 		if (len < PAGE_CACHE_SIZE)
2576416161dbSMark Fasheh 			cmp_len = len;
2577416161dbSMark Fasheh 
2578416161dbSMark Fasheh 		src_page = extent_same_get_page(src, loff);
2579416161dbSMark Fasheh 		if (!src_page)
2580416161dbSMark Fasheh 			return -EINVAL;
2581416161dbSMark Fasheh 		dst_page = extent_same_get_page(dst, dst_loff);
2582416161dbSMark Fasheh 		if (!dst_page) {
2583416161dbSMark Fasheh 			page_cache_release(src_page);
2584416161dbSMark Fasheh 			return -EINVAL;
2585416161dbSMark Fasheh 		}
2586416161dbSMark Fasheh 		addr = kmap_atomic(src_page);
2587416161dbSMark Fasheh 		dst_addr = kmap_atomic(dst_page);
2588416161dbSMark Fasheh 
2589416161dbSMark Fasheh 		flush_dcache_page(src_page);
2590416161dbSMark Fasheh 		flush_dcache_page(dst_page);
2591416161dbSMark Fasheh 
2592416161dbSMark Fasheh 		if (memcmp(addr, dst_addr, cmp_len))
2593416161dbSMark Fasheh 			ret = BTRFS_SAME_DATA_DIFFERS;
2594416161dbSMark Fasheh 
2595416161dbSMark Fasheh 		kunmap_atomic(addr);
2596416161dbSMark Fasheh 		kunmap_atomic(dst_addr);
2597416161dbSMark Fasheh 		page_cache_release(src_page);
2598416161dbSMark Fasheh 		page_cache_release(dst_page);
2599416161dbSMark Fasheh 
2600416161dbSMark Fasheh 		if (ret)
2601416161dbSMark Fasheh 			break;
2602416161dbSMark Fasheh 
2603416161dbSMark Fasheh 		loff += cmp_len;
2604416161dbSMark Fasheh 		dst_loff += cmp_len;
2605416161dbSMark Fasheh 		len -= cmp_len;
2606416161dbSMark Fasheh 	}
2607416161dbSMark Fasheh 
2608416161dbSMark Fasheh 	return ret;
2609416161dbSMark Fasheh }
2610416161dbSMark Fasheh 
2611416161dbSMark Fasheh static int extent_same_check_offsets(struct inode *inode, u64 off, u64 len)
2612416161dbSMark Fasheh {
2613416161dbSMark Fasheh 	u64 bs = BTRFS_I(inode)->root->fs_info->sb->s_blocksize;
2614416161dbSMark Fasheh 
2615416161dbSMark Fasheh 	if (off + len > inode->i_size || off + len < off)
2616416161dbSMark Fasheh 		return -EINVAL;
2617416161dbSMark Fasheh 	/* Check that we are block aligned - btrfs_clone() requires this */
2618416161dbSMark Fasheh 	if (!IS_ALIGNED(off, bs) || !IS_ALIGNED(off + len, bs))
2619416161dbSMark Fasheh 		return -EINVAL;
2620416161dbSMark Fasheh 
2621416161dbSMark Fasheh 	return 0;
2622416161dbSMark Fasheh }
2623416161dbSMark Fasheh 
2624416161dbSMark Fasheh static int btrfs_extent_same(struct inode *src, u64 loff, u64 len,
2625416161dbSMark Fasheh 			     struct inode *dst, u64 dst_loff)
2626416161dbSMark Fasheh {
2627416161dbSMark Fasheh 	int ret;
2628416161dbSMark Fasheh 
2629416161dbSMark Fasheh 	/*
2630416161dbSMark Fasheh 	 * btrfs_clone() can't handle extents in the same file
2631416161dbSMark Fasheh 	 * yet. Once that works, we can drop this check and replace it
2632416161dbSMark Fasheh 	 * with a check for the same inode, but overlapping extents.
2633416161dbSMark Fasheh 	 */
2634416161dbSMark Fasheh 	if (src == dst)
2635416161dbSMark Fasheh 		return -EINVAL;
2636416161dbSMark Fasheh 
2637416161dbSMark Fasheh 	btrfs_double_lock(src, loff, dst, dst_loff, len);
2638416161dbSMark Fasheh 
2639416161dbSMark Fasheh 	ret = extent_same_check_offsets(src, loff, len);
2640416161dbSMark Fasheh 	if (ret)
2641416161dbSMark Fasheh 		goto out_unlock;
2642416161dbSMark Fasheh 
2643416161dbSMark Fasheh 	ret = extent_same_check_offsets(dst, dst_loff, len);
2644416161dbSMark Fasheh 	if (ret)
2645416161dbSMark Fasheh 		goto out_unlock;
2646416161dbSMark Fasheh 
2647416161dbSMark Fasheh 	/* don't make the dst file partly checksummed */
2648416161dbSMark Fasheh 	if ((BTRFS_I(src)->flags & BTRFS_INODE_NODATASUM) !=
2649416161dbSMark Fasheh 	    (BTRFS_I(dst)->flags & BTRFS_INODE_NODATASUM)) {
2650416161dbSMark Fasheh 		ret = -EINVAL;
2651416161dbSMark Fasheh 		goto out_unlock;
2652416161dbSMark Fasheh 	}
2653416161dbSMark Fasheh 
2654416161dbSMark Fasheh 	ret = btrfs_cmp_data(src, loff, dst, dst_loff, len);
2655416161dbSMark Fasheh 	if (ret == 0)
2656416161dbSMark Fasheh 		ret = btrfs_clone(src, dst, loff, len, len, dst_loff);
2657416161dbSMark Fasheh 
2658416161dbSMark Fasheh out_unlock:
2659416161dbSMark Fasheh 	btrfs_double_unlock(src, loff, dst, dst_loff, len);
2660416161dbSMark Fasheh 
2661416161dbSMark Fasheh 	return ret;
2662416161dbSMark Fasheh }
2663416161dbSMark Fasheh 
2664416161dbSMark Fasheh #define BTRFS_MAX_DEDUPE_LEN	(16 * 1024 * 1024)
2665416161dbSMark Fasheh 
2666416161dbSMark Fasheh static long btrfs_ioctl_file_extent_same(struct file *file,
2667416161dbSMark Fasheh 					 void __user *argp)
2668416161dbSMark Fasheh {
2669416161dbSMark Fasheh 	struct btrfs_ioctl_same_args *args = argp;
2670416161dbSMark Fasheh 	struct btrfs_ioctl_same_args same;
2671416161dbSMark Fasheh 	struct btrfs_ioctl_same_extent_info info;
2672416161dbSMark Fasheh 	struct inode *src = file->f_dentry->d_inode;
2673416161dbSMark Fasheh 	struct file *dst_file = NULL;
2674416161dbSMark Fasheh 	struct inode *dst;
2675416161dbSMark Fasheh 	u64 off;
2676416161dbSMark Fasheh 	u64 len;
2677416161dbSMark Fasheh 	int i;
2678416161dbSMark Fasheh 	int ret;
2679416161dbSMark Fasheh 	u64 bs = BTRFS_I(src)->root->fs_info->sb->s_blocksize;
2680416161dbSMark Fasheh 	bool is_admin = capable(CAP_SYS_ADMIN);
2681416161dbSMark Fasheh 
2682416161dbSMark Fasheh 	if (!(file->f_mode & FMODE_READ))
2683416161dbSMark Fasheh 		return -EINVAL;
2684416161dbSMark Fasheh 
2685416161dbSMark Fasheh 	ret = mnt_want_write_file(file);
2686416161dbSMark Fasheh 	if (ret)
2687416161dbSMark Fasheh 		return ret;
2688416161dbSMark Fasheh 
2689416161dbSMark Fasheh 	if (copy_from_user(&same,
2690416161dbSMark Fasheh 			   (struct btrfs_ioctl_same_args __user *)argp,
2691416161dbSMark Fasheh 			   sizeof(same))) {
2692416161dbSMark Fasheh 		ret = -EFAULT;
2693416161dbSMark Fasheh 		goto out;
2694416161dbSMark Fasheh 	}
2695416161dbSMark Fasheh 
2696416161dbSMark Fasheh 	off = same.logical_offset;
2697416161dbSMark Fasheh 	len = same.length;
2698416161dbSMark Fasheh 
2699416161dbSMark Fasheh 	/*
2700416161dbSMark Fasheh 	 * Limit the total length we will dedupe for each operation.
2701416161dbSMark Fasheh 	 * This is intended to bound the total time spent in this
2702416161dbSMark Fasheh 	 * ioctl to something sane.
2703416161dbSMark Fasheh 	 */
2704416161dbSMark Fasheh 	if (len > BTRFS_MAX_DEDUPE_LEN)
2705416161dbSMark Fasheh 		len = BTRFS_MAX_DEDUPE_LEN;
2706416161dbSMark Fasheh 
2707416161dbSMark Fasheh 	if (WARN_ON_ONCE(bs < PAGE_CACHE_SIZE)) {
2708416161dbSMark Fasheh 		/*
2709416161dbSMark Fasheh 		 * Btrfs does not support blocksize < page_size. As a
2710416161dbSMark Fasheh 		 * result, btrfs_cmp_data() won't correctly handle
2711416161dbSMark Fasheh 		 * this situation without an update.
2712416161dbSMark Fasheh 		 */
2713416161dbSMark Fasheh 		ret = -EINVAL;
2714416161dbSMark Fasheh 		goto out;
2715416161dbSMark Fasheh 	}
2716416161dbSMark Fasheh 
2717416161dbSMark Fasheh 	ret = -EISDIR;
2718416161dbSMark Fasheh 	if (S_ISDIR(src->i_mode))
2719416161dbSMark Fasheh 		goto out;
2720416161dbSMark Fasheh 
2721416161dbSMark Fasheh 	ret = -EACCES;
2722416161dbSMark Fasheh 	if (!S_ISREG(src->i_mode))
2723416161dbSMark Fasheh 		goto out;
2724416161dbSMark Fasheh 
2725416161dbSMark Fasheh 	ret = 0;
2726416161dbSMark Fasheh 	for (i = 0; i < same.dest_count; i++) {
2727416161dbSMark Fasheh 		if (copy_from_user(&info, &args->info[i], sizeof(info))) {
2728416161dbSMark Fasheh 			ret = -EFAULT;
2729416161dbSMark Fasheh 			goto out;
2730416161dbSMark Fasheh 		}
2731416161dbSMark Fasheh 
2732416161dbSMark Fasheh 		info.bytes_deduped = 0;
2733416161dbSMark Fasheh 
2734416161dbSMark Fasheh 		dst_file = fget(info.fd);
2735416161dbSMark Fasheh 		if (!dst_file) {
2736416161dbSMark Fasheh 			info.status = -EBADF;
2737416161dbSMark Fasheh 			goto next;
2738416161dbSMark Fasheh 		}
2739416161dbSMark Fasheh 
2740416161dbSMark Fasheh 		if (!(is_admin || (dst_file->f_mode & FMODE_WRITE))) {
2741416161dbSMark Fasheh 			info.status = -EINVAL;
2742416161dbSMark Fasheh 			goto next;
2743416161dbSMark Fasheh 		}
2744416161dbSMark Fasheh 
2745416161dbSMark Fasheh 		info.status = -EXDEV;
2746416161dbSMark Fasheh 		if (file->f_path.mnt != dst_file->f_path.mnt)
2747416161dbSMark Fasheh 			goto next;
2748416161dbSMark Fasheh 
2749416161dbSMark Fasheh 		dst = dst_file->f_dentry->d_inode;
2750416161dbSMark Fasheh 		if (src->i_sb != dst->i_sb)
2751416161dbSMark Fasheh 			goto next;
2752416161dbSMark Fasheh 
2753416161dbSMark Fasheh 		if (S_ISDIR(dst->i_mode)) {
2754416161dbSMark Fasheh 			info.status = -EISDIR;
2755416161dbSMark Fasheh 			goto next;
2756416161dbSMark Fasheh 		}
2757416161dbSMark Fasheh 
2758416161dbSMark Fasheh 		if (!S_ISREG(dst->i_mode)) {
2759416161dbSMark Fasheh 			info.status = -EACCES;
2760416161dbSMark Fasheh 			goto next;
2761416161dbSMark Fasheh 		}
2762416161dbSMark Fasheh 
2763416161dbSMark Fasheh 		info.status = btrfs_extent_same(src, off, len, dst,
2764416161dbSMark Fasheh 						info.logical_offset);
2765416161dbSMark Fasheh 		if (info.status == 0)
2766416161dbSMark Fasheh 			info.bytes_deduped += len;
2767416161dbSMark Fasheh 
2768416161dbSMark Fasheh next:
2769416161dbSMark Fasheh 		if (dst_file)
2770416161dbSMark Fasheh 			fput(dst_file);
2771416161dbSMark Fasheh 
2772416161dbSMark Fasheh 		if (__put_user_unaligned(info.status, &args->info[i].status) ||
2773416161dbSMark Fasheh 		    __put_user_unaligned(info.bytes_deduped,
2774416161dbSMark Fasheh 					 &args->info[i].bytes_deduped)) {
2775416161dbSMark Fasheh 			ret = -EFAULT;
2776416161dbSMark Fasheh 			goto out;
2777416161dbSMark Fasheh 		}
2778416161dbSMark Fasheh 	}
2779416161dbSMark Fasheh 
2780416161dbSMark Fasheh out:
2781416161dbSMark Fasheh 	mnt_drop_write_file(file);
2782416161dbSMark Fasheh 	return ret;
2783416161dbSMark Fasheh }
2784416161dbSMark Fasheh 
278532b7c687SMark Fasheh /**
278632b7c687SMark Fasheh  * btrfs_clone() - clone a range from inode file to another
278732b7c687SMark Fasheh  *
278832b7c687SMark Fasheh  * @src: Inode to clone from
278932b7c687SMark Fasheh  * @inode: Inode to clone to
279032b7c687SMark Fasheh  * @off: Offset within source to start clone from
279132b7c687SMark Fasheh  * @olen: Original length, passed by user, of range to clone
279232b7c687SMark Fasheh  * @olen_aligned: Block-aligned value of olen, extent_same uses
279332b7c687SMark Fasheh  *               identical values here
279432b7c687SMark Fasheh  * @destoff: Offset within @inode to start clone
279532b7c687SMark Fasheh  */
279632b7c687SMark Fasheh static int btrfs_clone(struct inode *src, struct inode *inode,
279732b7c687SMark Fasheh 		       u64 off, u64 olen, u64 olen_aligned, u64 destoff)
2798f46b5a66SChristoph Hellwig {
2799f46b5a66SChristoph Hellwig 	struct btrfs_root *root = BTRFS_I(inode)->root;
280032b7c687SMark Fasheh 	struct btrfs_path *path = NULL;
2801f46b5a66SChristoph Hellwig 	struct extent_buffer *leaf;
280232b7c687SMark Fasheh 	struct btrfs_trans_handle *trans;
280332b7c687SMark Fasheh 	char *buf = NULL;
2804ae01a0abSYan Zheng 	struct btrfs_key key;
2805f46b5a66SChristoph Hellwig 	u32 nritems;
2806f46b5a66SChristoph Hellwig 	int slot;
2807ae01a0abSYan Zheng 	int ret;
280832b7c687SMark Fasheh 	u64 len = olen_aligned;
2809ae01a0abSYan Zheng 
2810ae01a0abSYan Zheng 	ret = -ENOMEM;
2811ae01a0abSYan Zheng 	buf = vmalloc(btrfs_level_size(root, 0));
2812ae01a0abSYan Zheng 	if (!buf)
281332b7c687SMark Fasheh 		return ret;
2814ae01a0abSYan Zheng 
2815ae01a0abSYan Zheng 	path = btrfs_alloc_path();
2816ae01a0abSYan Zheng 	if (!path) {
2817ae01a0abSYan Zheng 		vfree(buf);
281832b7c687SMark Fasheh 		return ret;
2819ae01a0abSYan Zheng 	}
282032b7c687SMark Fasheh 
2821ae01a0abSYan Zheng 	path->reada = 2;
2822c5c9cd4dSSage Weil 	/* clone data */
282333345d01SLi Zefan 	key.objectid = btrfs_ino(src);
2824ae01a0abSYan Zheng 	key.type = BTRFS_EXTENT_DATA_KEY;
2825ae01a0abSYan Zheng 	key.offset = 0;
2826f46b5a66SChristoph Hellwig 
2827f46b5a66SChristoph Hellwig 	while (1) {
2828f46b5a66SChristoph Hellwig 		/*
2829f46b5a66SChristoph Hellwig 		 * note the key will change type as we walk through the
2830f46b5a66SChristoph Hellwig 		 * tree.
2831f46b5a66SChristoph Hellwig 		 */
2832362a20c5SDavid Sterba 		ret = btrfs_search_slot(NULL, BTRFS_I(src)->root, &key, path,
2833362a20c5SDavid Sterba 				0, 0);
2834f46b5a66SChristoph Hellwig 		if (ret < 0)
2835f46b5a66SChristoph Hellwig 			goto out;
2836f46b5a66SChristoph Hellwig 
2837ae01a0abSYan Zheng 		nritems = btrfs_header_nritems(path->nodes[0]);
2838ae01a0abSYan Zheng 		if (path->slots[0] >= nritems) {
2839362a20c5SDavid Sterba 			ret = btrfs_next_leaf(BTRFS_I(src)->root, path);
2840f46b5a66SChristoph Hellwig 			if (ret < 0)
2841f46b5a66SChristoph Hellwig 				goto out;
2842f46b5a66SChristoph Hellwig 			if (ret > 0)
2843f46b5a66SChristoph Hellwig 				break;
2844ae01a0abSYan Zheng 			nritems = btrfs_header_nritems(path->nodes[0]);
2845f46b5a66SChristoph Hellwig 		}
2846f46b5a66SChristoph Hellwig 		leaf = path->nodes[0];
2847f46b5a66SChristoph Hellwig 		slot = path->slots[0];
2848f46b5a66SChristoph Hellwig 
2849ae01a0abSYan Zheng 		btrfs_item_key_to_cpu(leaf, &key, slot);
2850d20f7043SChris Mason 		if (btrfs_key_type(&key) > BTRFS_EXTENT_DATA_KEY ||
285133345d01SLi Zefan 		    key.objectid != btrfs_ino(src))
2852f46b5a66SChristoph Hellwig 			break;
2853f46b5a66SChristoph Hellwig 
2854c5c9cd4dSSage Weil 		if (btrfs_key_type(&key) == BTRFS_EXTENT_DATA_KEY) {
2855c5c9cd4dSSage Weil 			struct btrfs_file_extent_item *extent;
2856c5c9cd4dSSage Weil 			int type;
285731840ae1SZheng Yan 			u32 size;
285831840ae1SZheng Yan 			struct btrfs_key new_key;
2859c5c9cd4dSSage Weil 			u64 disko = 0, diskl = 0;
2860c5c9cd4dSSage Weil 			u64 datao = 0, datal = 0;
2861c5c9cd4dSSage Weil 			u8 comp;
2862b5384d48SSage Weil 			u64 endoff;
286331840ae1SZheng Yan 
286431840ae1SZheng Yan 			size = btrfs_item_size_nr(leaf, slot);
286531840ae1SZheng Yan 			read_extent_buffer(leaf, buf,
286631840ae1SZheng Yan 					   btrfs_item_ptr_offset(leaf, slot),
286731840ae1SZheng Yan 					   size);
2868c5c9cd4dSSage Weil 
2869c5c9cd4dSSage Weil 			extent = btrfs_item_ptr(leaf, slot,
2870c5c9cd4dSSage Weil 						struct btrfs_file_extent_item);
2871c5c9cd4dSSage Weil 			comp = btrfs_file_extent_compression(leaf, extent);
2872c5c9cd4dSSage Weil 			type = btrfs_file_extent_type(leaf, extent);
2873c8a894d7SChris Mason 			if (type == BTRFS_FILE_EXTENT_REG ||
2874c8a894d7SChris Mason 			    type == BTRFS_FILE_EXTENT_PREALLOC) {
2875d397712bSChris Mason 				disko = btrfs_file_extent_disk_bytenr(leaf,
2876d397712bSChris Mason 								      extent);
2877d397712bSChris Mason 				diskl = btrfs_file_extent_disk_num_bytes(leaf,
2878d397712bSChris Mason 								 extent);
2879c5c9cd4dSSage Weil 				datao = btrfs_file_extent_offset(leaf, extent);
2880d397712bSChris Mason 				datal = btrfs_file_extent_num_bytes(leaf,
2881d397712bSChris Mason 								    extent);
2882c5c9cd4dSSage Weil 			} else if (type == BTRFS_FILE_EXTENT_INLINE) {
2883c5c9cd4dSSage Weil 				/* take upper bound, may be compressed */
2884c5c9cd4dSSage Weil 				datal = btrfs_file_extent_ram_bytes(leaf,
2885c5c9cd4dSSage Weil 								    extent);
2886c5c9cd4dSSage Weil 			}
2887b3b4aa74SDavid Sterba 			btrfs_release_path(path);
288831840ae1SZheng Yan 
2889050006a7SSage Weil 			if (key.offset + datal <= off ||
2890aa42ffd9SLiu Bo 			    key.offset >= off + len - 1)
2891c5c9cd4dSSage Weil 				goto next;
2892c5c9cd4dSSage Weil 
289331840ae1SZheng Yan 			memcpy(&new_key, &key, sizeof(new_key));
289433345d01SLi Zefan 			new_key.objectid = btrfs_ino(inode);
28954d728ec7SLi Zefan 			if (off <= key.offset)
2896c5c9cd4dSSage Weil 				new_key.offset = key.offset + destoff - off;
28974d728ec7SLi Zefan 			else
28984d728ec7SLi Zefan 				new_key.offset = destoff;
2899c5c9cd4dSSage Weil 
2900b6f3409bSSage Weil 			/*
2901b6f3409bSSage Weil 			 * 1 - adjusting old extent (we may have to split it)
2902b6f3409bSSage Weil 			 * 1 - add new extent
2903b6f3409bSSage Weil 			 * 1 - inode update
2904b6f3409bSSage Weil 			 */
2905b6f3409bSSage Weil 			trans = btrfs_start_transaction(root, 3);
2906a22285a6SYan, Zheng 			if (IS_ERR(trans)) {
2907a22285a6SYan, Zheng 				ret = PTR_ERR(trans);
2908a22285a6SYan, Zheng 				goto out;
2909a22285a6SYan, Zheng 			}
2910a22285a6SYan, Zheng 
2911c8a894d7SChris Mason 			if (type == BTRFS_FILE_EXTENT_REG ||
2912c8a894d7SChris Mason 			    type == BTRFS_FILE_EXTENT_PREALLOC) {
2913d72c0842SLi Zefan 				/*
2914d72c0842SLi Zefan 				 *    a  | --- range to clone ---|  b
2915d72c0842SLi Zefan 				 * | ------------- extent ------------- |
2916d72c0842SLi Zefan 				 */
2917d72c0842SLi Zefan 
2918d72c0842SLi Zefan 				/* substract range b */
2919d72c0842SLi Zefan 				if (key.offset + datal > off + len)
2920d72c0842SLi Zefan 					datal = off + len - key.offset;
2921d72c0842SLi Zefan 
2922d72c0842SLi Zefan 				/* substract range a */
2923a22285a6SYan, Zheng 				if (off > key.offset) {
2924a22285a6SYan, Zheng 					datao += off - key.offset;
2925a22285a6SYan, Zheng 					datal -= off - key.offset;
2926a22285a6SYan, Zheng 				}
2927a22285a6SYan, Zheng 
29285dc562c5SJosef Bacik 				ret = btrfs_drop_extents(trans, root, inode,
2929a22285a6SYan, Zheng 							 new_key.offset,
2930a22285a6SYan, Zheng 							 new_key.offset + datal,
29312671485dSJosef Bacik 							 1);
293279787eaaSJeff Mahoney 				if (ret) {
293379787eaaSJeff Mahoney 					btrfs_abort_transaction(trans, root,
293479787eaaSJeff Mahoney 								ret);
293579787eaaSJeff Mahoney 					btrfs_end_transaction(trans, root);
293679787eaaSJeff Mahoney 					goto out;
293779787eaaSJeff Mahoney 				}
2938a22285a6SYan, Zheng 
293931840ae1SZheng Yan 				ret = btrfs_insert_empty_item(trans, root, path,
294031840ae1SZheng Yan 							      &new_key, size);
294179787eaaSJeff Mahoney 				if (ret) {
294279787eaaSJeff Mahoney 					btrfs_abort_transaction(trans, root,
294379787eaaSJeff Mahoney 								ret);
294479787eaaSJeff Mahoney 					btrfs_end_transaction(trans, root);
294579787eaaSJeff Mahoney 					goto out;
294679787eaaSJeff Mahoney 				}
294731840ae1SZheng Yan 
294831840ae1SZheng Yan 				leaf = path->nodes[0];
294931840ae1SZheng Yan 				slot = path->slots[0];
295031840ae1SZheng Yan 				write_extent_buffer(leaf, buf,
295131840ae1SZheng Yan 					    btrfs_item_ptr_offset(leaf, slot),
295231840ae1SZheng Yan 					    size);
2953ae01a0abSYan Zheng 
2954f46b5a66SChristoph Hellwig 				extent = btrfs_item_ptr(leaf, slot,
2955f46b5a66SChristoph Hellwig 						struct btrfs_file_extent_item);
2956c5c9cd4dSSage Weil 
2957c5c9cd4dSSage Weil 				/* disko == 0 means it's a hole */
2958c5c9cd4dSSage Weil 				if (!disko)
2959c5c9cd4dSSage Weil 					datao = 0;
2960c5c9cd4dSSage Weil 
2961c5c9cd4dSSage Weil 				btrfs_set_file_extent_offset(leaf, extent,
2962c5c9cd4dSSage Weil 							     datao);
2963c5c9cd4dSSage Weil 				btrfs_set_file_extent_num_bytes(leaf, extent,
2964c5c9cd4dSSage Weil 								datal);
2965c5c9cd4dSSage Weil 				if (disko) {
2966c5c9cd4dSSage Weil 					inode_add_bytes(inode, datal);
2967ae01a0abSYan Zheng 					ret = btrfs_inc_extent_ref(trans, root,
29685d4f98a2SYan Zheng 							disko, diskl, 0,
2969f46b5a66SChristoph Hellwig 							root->root_key.objectid,
297033345d01SLi Zefan 							btrfs_ino(inode),
297166d7e7f0SArne Jansen 							new_key.offset - datao,
297266d7e7f0SArne Jansen 							0);
297379787eaaSJeff Mahoney 					if (ret) {
297479787eaaSJeff Mahoney 						btrfs_abort_transaction(trans,
297579787eaaSJeff Mahoney 									root,
297679787eaaSJeff Mahoney 									ret);
297779787eaaSJeff Mahoney 						btrfs_end_transaction(trans,
297879787eaaSJeff Mahoney 								      root);
297979787eaaSJeff Mahoney 						goto out;
298079787eaaSJeff Mahoney 
298179787eaaSJeff Mahoney 					}
2982f46b5a66SChristoph Hellwig 				}
2983c5c9cd4dSSage Weil 			} else if (type == BTRFS_FILE_EXTENT_INLINE) {
2984c5c9cd4dSSage Weil 				u64 skip = 0;
2985c5c9cd4dSSage Weil 				u64 trim = 0;
2986c5c9cd4dSSage Weil 				if (off > key.offset) {
2987c5c9cd4dSSage Weil 					skip = off - key.offset;
2988c5c9cd4dSSage Weil 					new_key.offset += skip;
298931840ae1SZheng Yan 				}
2990d397712bSChris Mason 
2991c5c9cd4dSSage Weil 				if (key.offset + datal > off + len)
2992c5c9cd4dSSage Weil 					trim = key.offset + datal - (off + len);
2993d397712bSChris Mason 
2994c5c9cd4dSSage Weil 				if (comp && (skip || trim)) {
2995c5c9cd4dSSage Weil 					ret = -EINVAL;
2996a22285a6SYan, Zheng 					btrfs_end_transaction(trans, root);
2997c5c9cd4dSSage Weil 					goto out;
299831840ae1SZheng Yan 				}
2999c5c9cd4dSSage Weil 				size -= skip + trim;
3000c5c9cd4dSSage Weil 				datal -= skip + trim;
3001a22285a6SYan, Zheng 
30025dc562c5SJosef Bacik 				ret = btrfs_drop_extents(trans, root, inode,
3003a22285a6SYan, Zheng 							 new_key.offset,
3004a22285a6SYan, Zheng 							 new_key.offset + datal,
30052671485dSJosef Bacik 							 1);
300679787eaaSJeff Mahoney 				if (ret) {
300779787eaaSJeff Mahoney 					btrfs_abort_transaction(trans, root,
300879787eaaSJeff Mahoney 								ret);
300979787eaaSJeff Mahoney 					btrfs_end_transaction(trans, root);
301079787eaaSJeff Mahoney 					goto out;
301179787eaaSJeff Mahoney 				}
3012a22285a6SYan, Zheng 
3013c5c9cd4dSSage Weil 				ret = btrfs_insert_empty_item(trans, root, path,
3014c5c9cd4dSSage Weil 							      &new_key, size);
301579787eaaSJeff Mahoney 				if (ret) {
301679787eaaSJeff Mahoney 					btrfs_abort_transaction(trans, root,
301779787eaaSJeff Mahoney 								ret);
301879787eaaSJeff Mahoney 					btrfs_end_transaction(trans, root);
301979787eaaSJeff Mahoney 					goto out;
302079787eaaSJeff Mahoney 				}
3021c5c9cd4dSSage Weil 
3022c5c9cd4dSSage Weil 				if (skip) {
3023d397712bSChris Mason 					u32 start =
3024d397712bSChris Mason 					  btrfs_file_extent_calc_inline_size(0);
3025c5c9cd4dSSage Weil 					memmove(buf+start, buf+start+skip,
3026c5c9cd4dSSage Weil 						datal);
3027c5c9cd4dSSage Weil 				}
3028c5c9cd4dSSage Weil 
3029c5c9cd4dSSage Weil 				leaf = path->nodes[0];
3030c5c9cd4dSSage Weil 				slot = path->slots[0];
3031c5c9cd4dSSage Weil 				write_extent_buffer(leaf, buf,
3032c5c9cd4dSSage Weil 					    btrfs_item_ptr_offset(leaf, slot),
3033c5c9cd4dSSage Weil 					    size);
3034c5c9cd4dSSage Weil 				inode_add_bytes(inode, datal);
3035c5c9cd4dSSage Weil 			}
3036c5c9cd4dSSage Weil 
3037c5c9cd4dSSage Weil 			btrfs_mark_buffer_dirty(leaf);
3038b3b4aa74SDavid Sterba 			btrfs_release_path(path);
3039c5c9cd4dSSage Weil 
30400c4d2d95SJosef Bacik 			inode_inc_iversion(inode);
3041a22285a6SYan, Zheng 			inode->i_mtime = inode->i_ctime = CURRENT_TIME;
3042b5384d48SSage Weil 
3043b5384d48SSage Weil 			/*
3044b5384d48SSage Weil 			 * we round up to the block size at eof when
3045b5384d48SSage Weil 			 * determining which extents to clone above,
3046b5384d48SSage Weil 			 * but shouldn't round up the file size
3047b5384d48SSage Weil 			 */
3048b5384d48SSage Weil 			endoff = new_key.offset + datal;
30495f3888ffSLi Zefan 			if (endoff > destoff+olen)
30505f3888ffSLi Zefan 				endoff = destoff+olen;
3051b5384d48SSage Weil 			if (endoff > inode->i_size)
3052b5384d48SSage Weil 				btrfs_i_size_write(inode, endoff);
3053b5384d48SSage Weil 
3054a22285a6SYan, Zheng 			ret = btrfs_update_inode(trans, root, inode);
305579787eaaSJeff Mahoney 			if (ret) {
305679787eaaSJeff Mahoney 				btrfs_abort_transaction(trans, root, ret);
3057a22285a6SYan, Zheng 				btrfs_end_transaction(trans, root);
305879787eaaSJeff Mahoney 				goto out;
305979787eaaSJeff Mahoney 			}
306079787eaaSJeff Mahoney 			ret = btrfs_end_transaction(trans, root);
3061a22285a6SYan, Zheng 		}
3062c5c9cd4dSSage Weil next:
3063b3b4aa74SDavid Sterba 		btrfs_release_path(path);
3064ae01a0abSYan Zheng 		key.offset++;
3065ae01a0abSYan Zheng 	}
3066f46b5a66SChristoph Hellwig 	ret = 0;
306732b7c687SMark Fasheh 
3068f46b5a66SChristoph Hellwig out:
3069b3b4aa74SDavid Sterba 	btrfs_release_path(path);
307032b7c687SMark Fasheh 	btrfs_free_path(path);
307132b7c687SMark Fasheh 	vfree(buf);
307232b7c687SMark Fasheh 	return ret;
307332b7c687SMark Fasheh }
307432b7c687SMark Fasheh 
307532b7c687SMark Fasheh static noinline long btrfs_ioctl_clone(struct file *file, unsigned long srcfd,
307632b7c687SMark Fasheh 				       u64 off, u64 olen, u64 destoff)
307732b7c687SMark Fasheh {
307832b7c687SMark Fasheh 	struct inode *inode = fdentry(file)->d_inode;
307932b7c687SMark Fasheh 	struct btrfs_root *root = BTRFS_I(inode)->root;
308032b7c687SMark Fasheh 	struct fd src_file;
308132b7c687SMark Fasheh 	struct inode *src;
308232b7c687SMark Fasheh 	int ret;
308332b7c687SMark Fasheh 	u64 len = olen;
308432b7c687SMark Fasheh 	u64 bs = root->fs_info->sb->s_blocksize;
308532b7c687SMark Fasheh 	int same_inode = 0;
308632b7c687SMark Fasheh 
308732b7c687SMark Fasheh 	/*
308832b7c687SMark Fasheh 	 * TODO:
308932b7c687SMark Fasheh 	 * - split compressed inline extents.  annoying: we need to
309032b7c687SMark Fasheh 	 *   decompress into destination's address_space (the file offset
309132b7c687SMark Fasheh 	 *   may change, so source mapping won't do), then recompress (or
309232b7c687SMark Fasheh 	 *   otherwise reinsert) a subrange.
309332b7c687SMark Fasheh 	 * - allow ranges within the same file to be cloned (provided
309432b7c687SMark Fasheh 	 *   they don't overlap)?
309532b7c687SMark Fasheh 	 */
309632b7c687SMark Fasheh 
309732b7c687SMark Fasheh 	/* the destination must be opened for writing */
309832b7c687SMark Fasheh 	if (!(file->f_mode & FMODE_WRITE) || (file->f_flags & O_APPEND))
309932b7c687SMark Fasheh 		return -EINVAL;
310032b7c687SMark Fasheh 
310132b7c687SMark Fasheh 	if (btrfs_root_readonly(root))
310232b7c687SMark Fasheh 		return -EROFS;
310332b7c687SMark Fasheh 
310432b7c687SMark Fasheh 	ret = mnt_want_write_file(file);
310532b7c687SMark Fasheh 	if (ret)
310632b7c687SMark Fasheh 		return ret;
310732b7c687SMark Fasheh 
310832b7c687SMark Fasheh 	src_file = fdget(srcfd);
310932b7c687SMark Fasheh 	if (!src_file.file) {
311032b7c687SMark Fasheh 		ret = -EBADF;
311132b7c687SMark Fasheh 		goto out_drop_write;
311232b7c687SMark Fasheh 	}
311332b7c687SMark Fasheh 
311432b7c687SMark Fasheh 	ret = -EXDEV;
311532b7c687SMark Fasheh 	if (src_file.file->f_path.mnt != file->f_path.mnt)
311632b7c687SMark Fasheh 		goto out_fput;
311732b7c687SMark Fasheh 
311832b7c687SMark Fasheh 	src = file_inode(src_file.file);
311932b7c687SMark Fasheh 
312032b7c687SMark Fasheh 	ret = -EINVAL;
312132b7c687SMark Fasheh 	if (src == inode)
312232b7c687SMark Fasheh 		same_inode = 1;
312332b7c687SMark Fasheh 
312432b7c687SMark Fasheh 	/* the src must be open for reading */
312532b7c687SMark Fasheh 	if (!(src_file.file->f_mode & FMODE_READ))
312632b7c687SMark Fasheh 		goto out_fput;
312732b7c687SMark Fasheh 
312832b7c687SMark Fasheh 	/* don't make the dst file partly checksummed */
312932b7c687SMark Fasheh 	if ((BTRFS_I(src)->flags & BTRFS_INODE_NODATASUM) !=
313032b7c687SMark Fasheh 	    (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM))
313132b7c687SMark Fasheh 		goto out_fput;
313232b7c687SMark Fasheh 
313332b7c687SMark Fasheh 	ret = -EISDIR;
313432b7c687SMark Fasheh 	if (S_ISDIR(src->i_mode) || S_ISDIR(inode->i_mode))
313532b7c687SMark Fasheh 		goto out_fput;
313632b7c687SMark Fasheh 
313732b7c687SMark Fasheh 	ret = -EXDEV;
313832b7c687SMark Fasheh 	if (src->i_sb != inode->i_sb)
313932b7c687SMark Fasheh 		goto out_fput;
314032b7c687SMark Fasheh 
314132b7c687SMark Fasheh 	if (!same_inode) {
314232b7c687SMark Fasheh 		if (inode < src) {
314332b7c687SMark Fasheh 			mutex_lock_nested(&inode->i_mutex, I_MUTEX_PARENT);
314432b7c687SMark Fasheh 			mutex_lock_nested(&src->i_mutex, I_MUTEX_CHILD);
314532b7c687SMark Fasheh 		} else {
314632b7c687SMark Fasheh 			mutex_lock_nested(&src->i_mutex, I_MUTEX_PARENT);
314732b7c687SMark Fasheh 			mutex_lock_nested(&inode->i_mutex, I_MUTEX_CHILD);
314832b7c687SMark Fasheh 		}
314932b7c687SMark Fasheh 	} else {
315032b7c687SMark Fasheh 		mutex_lock(&src->i_mutex);
315132b7c687SMark Fasheh 	}
315232b7c687SMark Fasheh 
315332b7c687SMark Fasheh 	/* determine range to clone */
315432b7c687SMark Fasheh 	ret = -EINVAL;
315532b7c687SMark Fasheh 	if (off + len > src->i_size || off + len < off)
315632b7c687SMark Fasheh 		goto out_unlock;
315732b7c687SMark Fasheh 	if (len == 0)
315832b7c687SMark Fasheh 		olen = len = src->i_size - off;
315932b7c687SMark Fasheh 	/* if we extend to eof, continue to block boundary */
316032b7c687SMark Fasheh 	if (off + len == src->i_size)
316132b7c687SMark Fasheh 		len = ALIGN(src->i_size, bs) - off;
316232b7c687SMark Fasheh 
316332b7c687SMark Fasheh 	/* verify the end result is block aligned */
316432b7c687SMark Fasheh 	if (!IS_ALIGNED(off, bs) || !IS_ALIGNED(off + len, bs) ||
316532b7c687SMark Fasheh 	    !IS_ALIGNED(destoff, bs))
316632b7c687SMark Fasheh 		goto out_unlock;
316732b7c687SMark Fasheh 
316832b7c687SMark Fasheh 	/* verify if ranges are overlapped within the same file */
316932b7c687SMark Fasheh 	if (same_inode) {
317032b7c687SMark Fasheh 		if (destoff + len > off && destoff < off + len)
317132b7c687SMark Fasheh 			goto out_unlock;
317232b7c687SMark Fasheh 	}
317332b7c687SMark Fasheh 
317432b7c687SMark Fasheh 	if (destoff > inode->i_size) {
317532b7c687SMark Fasheh 		ret = btrfs_cont_expand(inode, inode->i_size, destoff);
317632b7c687SMark Fasheh 		if (ret)
317732b7c687SMark Fasheh 			goto out_unlock;
317832b7c687SMark Fasheh 	}
317932b7c687SMark Fasheh 
318032b7c687SMark Fasheh 	/* truncate page cache pages from target inode range */
318132b7c687SMark Fasheh 	truncate_inode_pages_range(&inode->i_data, destoff,
318232b7c687SMark Fasheh 				   PAGE_CACHE_ALIGN(destoff + len) - 1);
318332b7c687SMark Fasheh 
318432b7c687SMark Fasheh 	lock_extent_range(src, off, len);
318532b7c687SMark Fasheh 
318632b7c687SMark Fasheh 	ret = btrfs_clone(src, inode, off, olen, len, destoff);
318732b7c687SMark Fasheh 
3188aa42ffd9SLiu Bo 	unlock_extent(&BTRFS_I(src)->io_tree, off, off + len - 1);
3189f46b5a66SChristoph Hellwig out_unlock:
3190f46b5a66SChristoph Hellwig 	mutex_unlock(&src->i_mutex);
3191a96fbc72SLiu Bo 	if (!same_inode)
3192f46b5a66SChristoph Hellwig 		mutex_unlock(&inode->i_mutex);
3193f46b5a66SChristoph Hellwig out_fput:
31942903ff01SAl Viro 	fdput(src_file);
3195ab67b7c1SYan Zheng out_drop_write:
31962a79f17eSAl Viro 	mnt_drop_write_file(file);
3197f46b5a66SChristoph Hellwig 	return ret;
3198f46b5a66SChristoph Hellwig }
3199f46b5a66SChristoph Hellwig 
32007a865e8aSChristoph Hellwig static long btrfs_ioctl_clone_range(struct file *file, void __user *argp)
3201c5c9cd4dSSage Weil {
3202c5c9cd4dSSage Weil 	struct btrfs_ioctl_clone_range_args args;
3203c5c9cd4dSSage Weil 
32047a865e8aSChristoph Hellwig 	if (copy_from_user(&args, argp, sizeof(args)))
3205c5c9cd4dSSage Weil 		return -EFAULT;
3206c5c9cd4dSSage Weil 	return btrfs_ioctl_clone(file, args.src_fd, args.src_offset,
3207c5c9cd4dSSage Weil 				 args.src_length, args.dest_offset);
3208c5c9cd4dSSage Weil }
3209c5c9cd4dSSage Weil 
3210f46b5a66SChristoph Hellwig /*
3211f46b5a66SChristoph Hellwig  * there are many ways the trans_start and trans_end ioctls can lead
3212f46b5a66SChristoph Hellwig  * to deadlocks.  They should only be used by applications that
3213f46b5a66SChristoph Hellwig  * basically own the machine, and have a very in depth understanding
3214f46b5a66SChristoph Hellwig  * of all the possible deadlocks and enospc problems.
3215f46b5a66SChristoph Hellwig  */
3216b2950863SChristoph Hellwig static long btrfs_ioctl_trans_start(struct file *file)
3217f46b5a66SChristoph Hellwig {
3218496ad9aaSAl Viro 	struct inode *inode = file_inode(file);
3219f46b5a66SChristoph Hellwig 	struct btrfs_root *root = BTRFS_I(inode)->root;
3220f46b5a66SChristoph Hellwig 	struct btrfs_trans_handle *trans;
32211ab86aedSSage Weil 	int ret;
3222f46b5a66SChristoph Hellwig 
32231ab86aedSSage Weil 	ret = -EPERM;
3224df5b5520SChristoph Hellwig 	if (!capable(CAP_SYS_ADMIN))
3225f46b5a66SChristoph Hellwig 		goto out;
32261ab86aedSSage Weil 
32271ab86aedSSage Weil 	ret = -EINPROGRESS;
32281ab86aedSSage Weil 	if (file->private_data)
32291ab86aedSSage Weil 		goto out;
32309ca9ee09SSage Weil 
3231b83cc969SLi Zefan 	ret = -EROFS;
3232b83cc969SLi Zefan 	if (btrfs_root_readonly(root))
3233b83cc969SLi Zefan 		goto out;
3234b83cc969SLi Zefan 
3235a561be71SAl Viro 	ret = mnt_want_write_file(file);
3236c146afadSYan Zheng 	if (ret)
3237c146afadSYan Zheng 		goto out;
3238c146afadSYan Zheng 
3239a4abeea4SJosef Bacik 	atomic_inc(&root->fs_info->open_ioctl_trans);
32409ca9ee09SSage Weil 
3241f46b5a66SChristoph Hellwig 	ret = -ENOMEM;
32427a7eaa40SJosef Bacik 	trans = btrfs_start_ioctl_transaction(root);
3243abd30bb0STsutomu Itoh 	if (IS_ERR(trans))
32441ab86aedSSage Weil 		goto out_drop;
32451ab86aedSSage Weil 
32461ab86aedSSage Weil 	file->private_data = trans;
32471ab86aedSSage Weil 	return 0;
32481ab86aedSSage Weil 
32491ab86aedSSage Weil out_drop:
3250a4abeea4SJosef Bacik 	atomic_dec(&root->fs_info->open_ioctl_trans);
32512a79f17eSAl Viro 	mnt_drop_write_file(file);
3252f46b5a66SChristoph Hellwig out:
3253f46b5a66SChristoph Hellwig 	return ret;
3254f46b5a66SChristoph Hellwig }
3255f46b5a66SChristoph Hellwig 
32566ef5ed0dSJosef Bacik static long btrfs_ioctl_default_subvol(struct file *file, void __user *argp)
32576ef5ed0dSJosef Bacik {
3258496ad9aaSAl Viro 	struct inode *inode = file_inode(file);
32596ef5ed0dSJosef Bacik 	struct btrfs_root *root = BTRFS_I(inode)->root;
32606ef5ed0dSJosef Bacik 	struct btrfs_root *new_root;
32616ef5ed0dSJosef Bacik 	struct btrfs_dir_item *di;
32626ef5ed0dSJosef Bacik 	struct btrfs_trans_handle *trans;
32636ef5ed0dSJosef Bacik 	struct btrfs_path *path;
32646ef5ed0dSJosef Bacik 	struct btrfs_key location;
32656ef5ed0dSJosef Bacik 	struct btrfs_disk_key disk_key;
32666ef5ed0dSJosef Bacik 	u64 objectid = 0;
32676ef5ed0dSJosef Bacik 	u64 dir_id;
32683c04ce01SMiao Xie 	int ret;
32696ef5ed0dSJosef Bacik 
32706ef5ed0dSJosef Bacik 	if (!capable(CAP_SYS_ADMIN))
32716ef5ed0dSJosef Bacik 		return -EPERM;
32726ef5ed0dSJosef Bacik 
32733c04ce01SMiao Xie 	ret = mnt_want_write_file(file);
32743c04ce01SMiao Xie 	if (ret)
32753c04ce01SMiao Xie 		return ret;
32763c04ce01SMiao Xie 
32773c04ce01SMiao Xie 	if (copy_from_user(&objectid, argp, sizeof(objectid))) {
32783c04ce01SMiao Xie 		ret = -EFAULT;
32793c04ce01SMiao Xie 		goto out;
32803c04ce01SMiao Xie 	}
32816ef5ed0dSJosef Bacik 
32826ef5ed0dSJosef Bacik 	if (!objectid)
32836ef5ed0dSJosef Bacik 		objectid = root->root_key.objectid;
32846ef5ed0dSJosef Bacik 
32856ef5ed0dSJosef Bacik 	location.objectid = objectid;
32866ef5ed0dSJosef Bacik 	location.type = BTRFS_ROOT_ITEM_KEY;
32876ef5ed0dSJosef Bacik 	location.offset = (u64)-1;
32886ef5ed0dSJosef Bacik 
32896ef5ed0dSJosef Bacik 	new_root = btrfs_read_fs_root_no_name(root->fs_info, &location);
32903c04ce01SMiao Xie 	if (IS_ERR(new_root)) {
32913c04ce01SMiao Xie 		ret = PTR_ERR(new_root);
32923c04ce01SMiao Xie 		goto out;
32933c04ce01SMiao Xie 	}
32946ef5ed0dSJosef Bacik 
32956ef5ed0dSJosef Bacik 	path = btrfs_alloc_path();
32963c04ce01SMiao Xie 	if (!path) {
32973c04ce01SMiao Xie 		ret = -ENOMEM;
32983c04ce01SMiao Xie 		goto out;
32993c04ce01SMiao Xie 	}
33006ef5ed0dSJosef Bacik 	path->leave_spinning = 1;
33016ef5ed0dSJosef Bacik 
33026ef5ed0dSJosef Bacik 	trans = btrfs_start_transaction(root, 1);
330398d5dc13STsutomu Itoh 	if (IS_ERR(trans)) {
33046ef5ed0dSJosef Bacik 		btrfs_free_path(path);
33053c04ce01SMiao Xie 		ret = PTR_ERR(trans);
33063c04ce01SMiao Xie 		goto out;
33076ef5ed0dSJosef Bacik 	}
33086ef5ed0dSJosef Bacik 
33096c41761fSDavid Sterba 	dir_id = btrfs_super_root_dir(root->fs_info->super_copy);
33106ef5ed0dSJosef Bacik 	di = btrfs_lookup_dir_item(trans, root->fs_info->tree_root, path,
33116ef5ed0dSJosef Bacik 				   dir_id, "default", 7, 1);
3312cf1e99a4SDan Carpenter 	if (IS_ERR_OR_NULL(di)) {
33136ef5ed0dSJosef Bacik 		btrfs_free_path(path);
33146ef5ed0dSJosef Bacik 		btrfs_end_transaction(trans, root);
33156ef5ed0dSJosef Bacik 		printk(KERN_ERR "Umm, you don't have the default dir item, "
33166ef5ed0dSJosef Bacik 		       "this isn't going to work\n");
33173c04ce01SMiao Xie 		ret = -ENOENT;
33183c04ce01SMiao Xie 		goto out;
33196ef5ed0dSJosef Bacik 	}
33206ef5ed0dSJosef Bacik 
33216ef5ed0dSJosef Bacik 	btrfs_cpu_key_to_disk(&disk_key, &new_root->root_key);
33226ef5ed0dSJosef Bacik 	btrfs_set_dir_item_key(path->nodes[0], di, &disk_key);
33236ef5ed0dSJosef Bacik 	btrfs_mark_buffer_dirty(path->nodes[0]);
33246ef5ed0dSJosef Bacik 	btrfs_free_path(path);
33256ef5ed0dSJosef Bacik 
33262b0ce2c2SMitch Harder 	btrfs_set_fs_incompat(root->fs_info, DEFAULT_SUBVOL);
33276ef5ed0dSJosef Bacik 	btrfs_end_transaction(trans, root);
33283c04ce01SMiao Xie out:
33293c04ce01SMiao Xie 	mnt_drop_write_file(file);
33303c04ce01SMiao Xie 	return ret;
33316ef5ed0dSJosef Bacik }
33326ef5ed0dSJosef Bacik 
33335af3e8ccSStefan Behrens void btrfs_get_block_group_info(struct list_head *groups_list,
3334bf5fc093SJosef Bacik 				struct btrfs_ioctl_space_info *space)
3335bf5fc093SJosef Bacik {
3336bf5fc093SJosef Bacik 	struct btrfs_block_group_cache *block_group;
3337bf5fc093SJosef Bacik 
3338bf5fc093SJosef Bacik 	space->total_bytes = 0;
3339bf5fc093SJosef Bacik 	space->used_bytes = 0;
3340bf5fc093SJosef Bacik 	space->flags = 0;
3341bf5fc093SJosef Bacik 	list_for_each_entry(block_group, groups_list, list) {
3342bf5fc093SJosef Bacik 		space->flags = block_group->flags;
3343bf5fc093SJosef Bacik 		space->total_bytes += block_group->key.offset;
3344bf5fc093SJosef Bacik 		space->used_bytes +=
3345bf5fc093SJosef Bacik 			btrfs_block_group_used(&block_group->item);
3346bf5fc093SJosef Bacik 	}
3347bf5fc093SJosef Bacik }
3348bf5fc093SJosef Bacik 
334948a3b636SEric Sandeen static long btrfs_ioctl_space_info(struct btrfs_root *root, void __user *arg)
33501406e432SJosef Bacik {
33511406e432SJosef Bacik 	struct btrfs_ioctl_space_args space_args;
33521406e432SJosef Bacik 	struct btrfs_ioctl_space_info space;
33531406e432SJosef Bacik 	struct btrfs_ioctl_space_info *dest;
33547fde62bfSChris Mason 	struct btrfs_ioctl_space_info *dest_orig;
335513f2696fSDaniel J Blueman 	struct btrfs_ioctl_space_info __user *user_dest;
33561406e432SJosef Bacik 	struct btrfs_space_info *info;
3357bf5fc093SJosef Bacik 	u64 types[] = {BTRFS_BLOCK_GROUP_DATA,
3358bf5fc093SJosef Bacik 		       BTRFS_BLOCK_GROUP_SYSTEM,
3359bf5fc093SJosef Bacik 		       BTRFS_BLOCK_GROUP_METADATA,
3360bf5fc093SJosef Bacik 		       BTRFS_BLOCK_GROUP_DATA | BTRFS_BLOCK_GROUP_METADATA};
3361bf5fc093SJosef Bacik 	int num_types = 4;
33627fde62bfSChris Mason 	int alloc_size;
33631406e432SJosef Bacik 	int ret = 0;
336451788b1bSDan Rosenberg 	u64 slot_count = 0;
3365bf5fc093SJosef Bacik 	int i, c;
33661406e432SJosef Bacik 
33671406e432SJosef Bacik 	if (copy_from_user(&space_args,
33681406e432SJosef Bacik 			   (struct btrfs_ioctl_space_args __user *)arg,
33691406e432SJosef Bacik 			   sizeof(space_args)))
33701406e432SJosef Bacik 		return -EFAULT;
33711406e432SJosef Bacik 
3372bf5fc093SJosef Bacik 	for (i = 0; i < num_types; i++) {
3373bf5fc093SJosef Bacik 		struct btrfs_space_info *tmp;
3374bf5fc093SJosef Bacik 
3375bf5fc093SJosef Bacik 		info = NULL;
33767fde62bfSChris Mason 		rcu_read_lock();
3377bf5fc093SJosef Bacik 		list_for_each_entry_rcu(tmp, &root->fs_info->space_info,
3378bf5fc093SJosef Bacik 					list) {
3379bf5fc093SJosef Bacik 			if (tmp->flags == types[i]) {
3380bf5fc093SJosef Bacik 				info = tmp;
3381bf5fc093SJosef Bacik 				break;
3382bf5fc093SJosef Bacik 			}
3383bf5fc093SJosef Bacik 		}
33847fde62bfSChris Mason 		rcu_read_unlock();
33851406e432SJosef Bacik 
3386bf5fc093SJosef Bacik 		if (!info)
3387bf5fc093SJosef Bacik 			continue;
3388bf5fc093SJosef Bacik 
3389bf5fc093SJosef Bacik 		down_read(&info->groups_sem);
3390bf5fc093SJosef Bacik 		for (c = 0; c < BTRFS_NR_RAID_TYPES; c++) {
3391bf5fc093SJosef Bacik 			if (!list_empty(&info->block_groups[c]))
3392bf5fc093SJosef Bacik 				slot_count++;
3393bf5fc093SJosef Bacik 		}
3394bf5fc093SJosef Bacik 		up_read(&info->groups_sem);
3395bf5fc093SJosef Bacik 	}
3396bf5fc093SJosef Bacik 
33977fde62bfSChris Mason 	/* space_slots == 0 means they are asking for a count */
33987fde62bfSChris Mason 	if (space_args.space_slots == 0) {
33997fde62bfSChris Mason 		space_args.total_spaces = slot_count;
34007fde62bfSChris Mason 		goto out;
34017fde62bfSChris Mason 	}
3402bf5fc093SJosef Bacik 
340351788b1bSDan Rosenberg 	slot_count = min_t(u64, space_args.space_slots, slot_count);
3404bf5fc093SJosef Bacik 
34057fde62bfSChris Mason 	alloc_size = sizeof(*dest) * slot_count;
3406bf5fc093SJosef Bacik 
34077fde62bfSChris Mason 	/* we generally have at most 6 or so space infos, one for each raid
34087fde62bfSChris Mason 	 * level.  So, a whole page should be more than enough for everyone
34097fde62bfSChris Mason 	 */
34107fde62bfSChris Mason 	if (alloc_size > PAGE_CACHE_SIZE)
34117fde62bfSChris Mason 		return -ENOMEM;
34127fde62bfSChris Mason 
34137fde62bfSChris Mason 	space_args.total_spaces = 0;
34147fde62bfSChris Mason 	dest = kmalloc(alloc_size, GFP_NOFS);
34157fde62bfSChris Mason 	if (!dest)
34167fde62bfSChris Mason 		return -ENOMEM;
34177fde62bfSChris Mason 	dest_orig = dest;
34187fde62bfSChris Mason 
34197fde62bfSChris Mason 	/* now we have a buffer to copy into */
3420bf5fc093SJosef Bacik 	for (i = 0; i < num_types; i++) {
3421bf5fc093SJosef Bacik 		struct btrfs_space_info *tmp;
3422bf5fc093SJosef Bacik 
342351788b1bSDan Rosenberg 		if (!slot_count)
342451788b1bSDan Rosenberg 			break;
342551788b1bSDan Rosenberg 
3426bf5fc093SJosef Bacik 		info = NULL;
34271406e432SJosef Bacik 		rcu_read_lock();
3428bf5fc093SJosef Bacik 		list_for_each_entry_rcu(tmp, &root->fs_info->space_info,
3429bf5fc093SJosef Bacik 					list) {
3430bf5fc093SJosef Bacik 			if (tmp->flags == types[i]) {
3431bf5fc093SJosef Bacik 				info = tmp;
34327fde62bfSChris Mason 				break;
3433bf5fc093SJosef Bacik 			}
3434bf5fc093SJosef Bacik 		}
3435bf5fc093SJosef Bacik 		rcu_read_unlock();
34367fde62bfSChris Mason 
3437bf5fc093SJosef Bacik 		if (!info)
3438bf5fc093SJosef Bacik 			continue;
3439bf5fc093SJosef Bacik 		down_read(&info->groups_sem);
3440bf5fc093SJosef Bacik 		for (c = 0; c < BTRFS_NR_RAID_TYPES; c++) {
3441bf5fc093SJosef Bacik 			if (!list_empty(&info->block_groups[c])) {
34425af3e8ccSStefan Behrens 				btrfs_get_block_group_info(
34435af3e8ccSStefan Behrens 					&info->block_groups[c], &space);
34447fde62bfSChris Mason 				memcpy(dest, &space, sizeof(space));
34451406e432SJosef Bacik 				dest++;
34461406e432SJosef Bacik 				space_args.total_spaces++;
344751788b1bSDan Rosenberg 				slot_count--;
34481406e432SJosef Bacik 			}
344951788b1bSDan Rosenberg 			if (!slot_count)
345051788b1bSDan Rosenberg 				break;
3451bf5fc093SJosef Bacik 		}
3452bf5fc093SJosef Bacik 		up_read(&info->groups_sem);
3453bf5fc093SJosef Bacik 	}
34541406e432SJosef Bacik 
34552eec6c81SDaniel J Blueman 	user_dest = (struct btrfs_ioctl_space_info __user *)
34567fde62bfSChris Mason 		(arg + sizeof(struct btrfs_ioctl_space_args));
34577fde62bfSChris Mason 
34587fde62bfSChris Mason 	if (copy_to_user(user_dest, dest_orig, alloc_size))
34597fde62bfSChris Mason 		ret = -EFAULT;
34607fde62bfSChris Mason 
34617fde62bfSChris Mason 	kfree(dest_orig);
34627fde62bfSChris Mason out:
34637fde62bfSChris Mason 	if (ret == 0 && copy_to_user(arg, &space_args, sizeof(space_args)))
34641406e432SJosef Bacik 		ret = -EFAULT;
34651406e432SJosef Bacik 
34661406e432SJosef Bacik 	return ret;
34671406e432SJosef Bacik }
34681406e432SJosef Bacik 
3469f46b5a66SChristoph Hellwig /*
3470f46b5a66SChristoph Hellwig  * there are many ways the trans_start and trans_end ioctls can lead
3471f46b5a66SChristoph Hellwig  * to deadlocks.  They should only be used by applications that
3472f46b5a66SChristoph Hellwig  * basically own the machine, and have a very in depth understanding
3473f46b5a66SChristoph Hellwig  * of all the possible deadlocks and enospc problems.
3474f46b5a66SChristoph Hellwig  */
3475f46b5a66SChristoph Hellwig long btrfs_ioctl_trans_end(struct file *file)
3476f46b5a66SChristoph Hellwig {
3477496ad9aaSAl Viro 	struct inode *inode = file_inode(file);
3478f46b5a66SChristoph Hellwig 	struct btrfs_root *root = BTRFS_I(inode)->root;
3479f46b5a66SChristoph Hellwig 	struct btrfs_trans_handle *trans;
3480f46b5a66SChristoph Hellwig 
3481f46b5a66SChristoph Hellwig 	trans = file->private_data;
34821ab86aedSSage Weil 	if (!trans)
34831ab86aedSSage Weil 		return -EINVAL;
3484b214107eSChristoph Hellwig 	file->private_data = NULL;
34859ca9ee09SSage Weil 
34861ab86aedSSage Weil 	btrfs_end_transaction(trans, root);
34871ab86aedSSage Weil 
3488a4abeea4SJosef Bacik 	atomic_dec(&root->fs_info->open_ioctl_trans);
34899ca9ee09SSage Weil 
34902a79f17eSAl Viro 	mnt_drop_write_file(file);
34911ab86aedSSage Weil 	return 0;
3492f46b5a66SChristoph Hellwig }
3493f46b5a66SChristoph Hellwig 
34949a8c28beSMiao Xie static noinline long btrfs_ioctl_start_sync(struct btrfs_root *root,
34959a8c28beSMiao Xie 					    void __user *argp)
349646204592SSage Weil {
349746204592SSage Weil 	struct btrfs_trans_handle *trans;
349846204592SSage Weil 	u64 transid;
3499db5b493aSTsutomu Itoh 	int ret;
350046204592SSage Weil 
3501d4edf39bSMiao Xie 	trans = btrfs_attach_transaction_barrier(root);
3502ff7c1d33SMiao Xie 	if (IS_ERR(trans)) {
3503ff7c1d33SMiao Xie 		if (PTR_ERR(trans) != -ENOENT)
350498d5dc13STsutomu Itoh 			return PTR_ERR(trans);
3505ff7c1d33SMiao Xie 
3506ff7c1d33SMiao Xie 		/* No running transaction, don't bother */
3507ff7c1d33SMiao Xie 		transid = root->fs_info->last_trans_committed;
3508ff7c1d33SMiao Xie 		goto out;
3509ff7c1d33SMiao Xie 	}
351046204592SSage Weil 	transid = trans->transid;
3511db5b493aSTsutomu Itoh 	ret = btrfs_commit_transaction_async(trans, root, 0);
35128b2b2d3cSTsutomu Itoh 	if (ret) {
35138b2b2d3cSTsutomu Itoh 		btrfs_end_transaction(trans, root);
3514db5b493aSTsutomu Itoh 		return ret;
35158b2b2d3cSTsutomu Itoh 	}
3516ff7c1d33SMiao Xie out:
351746204592SSage Weil 	if (argp)
351846204592SSage Weil 		if (copy_to_user(argp, &transid, sizeof(transid)))
351946204592SSage Weil 			return -EFAULT;
352046204592SSage Weil 	return 0;
352146204592SSage Weil }
352246204592SSage Weil 
35239a8c28beSMiao Xie static noinline long btrfs_ioctl_wait_sync(struct btrfs_root *root,
35249a8c28beSMiao Xie 					   void __user *argp)
352546204592SSage Weil {
352646204592SSage Weil 	u64 transid;
352746204592SSage Weil 
352846204592SSage Weil 	if (argp) {
352946204592SSage Weil 		if (copy_from_user(&transid, argp, sizeof(transid)))
353046204592SSage Weil 			return -EFAULT;
353146204592SSage Weil 	} else {
353246204592SSage Weil 		transid = 0;  /* current trans */
353346204592SSage Weil 	}
353446204592SSage Weil 	return btrfs_wait_for_commit(root, transid);
353546204592SSage Weil }
353646204592SSage Weil 
3537b8e95489SMiao Xie static long btrfs_ioctl_scrub(struct file *file, void __user *arg)
3538475f6387SJan Schmidt {
3539496ad9aaSAl Viro 	struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
3540475f6387SJan Schmidt 	struct btrfs_ioctl_scrub_args *sa;
3541b8e95489SMiao Xie 	int ret;
3542475f6387SJan Schmidt 
3543475f6387SJan Schmidt 	if (!capable(CAP_SYS_ADMIN))
3544475f6387SJan Schmidt 		return -EPERM;
3545475f6387SJan Schmidt 
3546475f6387SJan Schmidt 	sa = memdup_user(arg, sizeof(*sa));
3547475f6387SJan Schmidt 	if (IS_ERR(sa))
3548475f6387SJan Schmidt 		return PTR_ERR(sa);
3549475f6387SJan Schmidt 
3550b8e95489SMiao Xie 	if (!(sa->flags & BTRFS_SCRUB_READONLY)) {
3551b8e95489SMiao Xie 		ret = mnt_want_write_file(file);
3552b8e95489SMiao Xie 		if (ret)
3553b8e95489SMiao Xie 			goto out;
3554b8e95489SMiao Xie 	}
3555b8e95489SMiao Xie 
3556aa1b8cd4SStefan Behrens 	ret = btrfs_scrub_dev(root->fs_info, sa->devid, sa->start, sa->end,
355763a212abSStefan Behrens 			      &sa->progress, sa->flags & BTRFS_SCRUB_READONLY,
355863a212abSStefan Behrens 			      0);
3559475f6387SJan Schmidt 
3560475f6387SJan Schmidt 	if (copy_to_user(arg, sa, sizeof(*sa)))
3561475f6387SJan Schmidt 		ret = -EFAULT;
3562475f6387SJan Schmidt 
3563b8e95489SMiao Xie 	if (!(sa->flags & BTRFS_SCRUB_READONLY))
3564b8e95489SMiao Xie 		mnt_drop_write_file(file);
3565b8e95489SMiao Xie out:
3566475f6387SJan Schmidt 	kfree(sa);
3567475f6387SJan Schmidt 	return ret;
3568475f6387SJan Schmidt }
3569475f6387SJan Schmidt 
3570475f6387SJan Schmidt static long btrfs_ioctl_scrub_cancel(struct btrfs_root *root, void __user *arg)
3571475f6387SJan Schmidt {
3572475f6387SJan Schmidt 	if (!capable(CAP_SYS_ADMIN))
3573475f6387SJan Schmidt 		return -EPERM;
3574475f6387SJan Schmidt 
3575aa1b8cd4SStefan Behrens 	return btrfs_scrub_cancel(root->fs_info);
3576475f6387SJan Schmidt }
3577475f6387SJan Schmidt 
3578475f6387SJan Schmidt static long btrfs_ioctl_scrub_progress(struct btrfs_root *root,
3579475f6387SJan Schmidt 				       void __user *arg)
3580475f6387SJan Schmidt {
3581475f6387SJan Schmidt 	struct btrfs_ioctl_scrub_args *sa;
3582475f6387SJan Schmidt 	int ret;
3583475f6387SJan Schmidt 
3584475f6387SJan Schmidt 	if (!capable(CAP_SYS_ADMIN))
3585475f6387SJan Schmidt 		return -EPERM;
3586475f6387SJan Schmidt 
3587475f6387SJan Schmidt 	sa = memdup_user(arg, sizeof(*sa));
3588475f6387SJan Schmidt 	if (IS_ERR(sa))
3589475f6387SJan Schmidt 		return PTR_ERR(sa);
3590475f6387SJan Schmidt 
3591475f6387SJan Schmidt 	ret = btrfs_scrub_progress(root, sa->devid, &sa->progress);
3592475f6387SJan Schmidt 
3593475f6387SJan Schmidt 	if (copy_to_user(arg, sa, sizeof(*sa)))
3594475f6387SJan Schmidt 		ret = -EFAULT;
3595475f6387SJan Schmidt 
3596475f6387SJan Schmidt 	kfree(sa);
3597475f6387SJan Schmidt 	return ret;
3598475f6387SJan Schmidt }
3599475f6387SJan Schmidt 
3600c11d2c23SStefan Behrens static long btrfs_ioctl_get_dev_stats(struct btrfs_root *root,
3601b27f7c0cSDavid Sterba 				      void __user *arg)
3602c11d2c23SStefan Behrens {
3603c11d2c23SStefan Behrens 	struct btrfs_ioctl_get_dev_stats *sa;
3604c11d2c23SStefan Behrens 	int ret;
3605c11d2c23SStefan Behrens 
3606c11d2c23SStefan Behrens 	sa = memdup_user(arg, sizeof(*sa));
3607c11d2c23SStefan Behrens 	if (IS_ERR(sa))
3608c11d2c23SStefan Behrens 		return PTR_ERR(sa);
3609c11d2c23SStefan Behrens 
3610b27f7c0cSDavid Sterba 	if ((sa->flags & BTRFS_DEV_STATS_RESET) && !capable(CAP_SYS_ADMIN)) {
3611b27f7c0cSDavid Sterba 		kfree(sa);
3612b27f7c0cSDavid Sterba 		return -EPERM;
3613b27f7c0cSDavid Sterba 	}
3614b27f7c0cSDavid Sterba 
3615b27f7c0cSDavid Sterba 	ret = btrfs_get_dev_stats(root, sa);
3616c11d2c23SStefan Behrens 
3617c11d2c23SStefan Behrens 	if (copy_to_user(arg, sa, sizeof(*sa)))
3618c11d2c23SStefan Behrens 		ret = -EFAULT;
3619c11d2c23SStefan Behrens 
3620c11d2c23SStefan Behrens 	kfree(sa);
3621c11d2c23SStefan Behrens 	return ret;
3622c11d2c23SStefan Behrens }
3623c11d2c23SStefan Behrens 
36243f6bcfbdSStefan Behrens static long btrfs_ioctl_dev_replace(struct btrfs_root *root, void __user *arg)
36253f6bcfbdSStefan Behrens {
36263f6bcfbdSStefan Behrens 	struct btrfs_ioctl_dev_replace_args *p;
36273f6bcfbdSStefan Behrens 	int ret;
36283f6bcfbdSStefan Behrens 
36293f6bcfbdSStefan Behrens 	if (!capable(CAP_SYS_ADMIN))
36303f6bcfbdSStefan Behrens 		return -EPERM;
36313f6bcfbdSStefan Behrens 
36323f6bcfbdSStefan Behrens 	p = memdup_user(arg, sizeof(*p));
36333f6bcfbdSStefan Behrens 	if (IS_ERR(p))
36343f6bcfbdSStefan Behrens 		return PTR_ERR(p);
36353f6bcfbdSStefan Behrens 
36363f6bcfbdSStefan Behrens 	switch (p->cmd) {
36373f6bcfbdSStefan Behrens 	case BTRFS_IOCTL_DEV_REPLACE_CMD_START:
36383f6bcfbdSStefan Behrens 		if (atomic_xchg(
36393f6bcfbdSStefan Behrens 			&root->fs_info->mutually_exclusive_operation_running,
36403f6bcfbdSStefan Behrens 			1)) {
36413f6bcfbdSStefan Behrens 			pr_info("btrfs: dev add/delete/balance/replace/resize operation in progress\n");
36423f6bcfbdSStefan Behrens 			ret = -EINPROGRESS;
36433f6bcfbdSStefan Behrens 		} else {
36443f6bcfbdSStefan Behrens 			ret = btrfs_dev_replace_start(root, p);
36453f6bcfbdSStefan Behrens 			atomic_set(
36463f6bcfbdSStefan Behrens 			 &root->fs_info->mutually_exclusive_operation_running,
36473f6bcfbdSStefan Behrens 			 0);
36483f6bcfbdSStefan Behrens 		}
36493f6bcfbdSStefan Behrens 		break;
36503f6bcfbdSStefan Behrens 	case BTRFS_IOCTL_DEV_REPLACE_CMD_STATUS:
36513f6bcfbdSStefan Behrens 		btrfs_dev_replace_status(root->fs_info, p);
36523f6bcfbdSStefan Behrens 		ret = 0;
36533f6bcfbdSStefan Behrens 		break;
36543f6bcfbdSStefan Behrens 	case BTRFS_IOCTL_DEV_REPLACE_CMD_CANCEL:
36553f6bcfbdSStefan Behrens 		ret = btrfs_dev_replace_cancel(root->fs_info, p);
36563f6bcfbdSStefan Behrens 		break;
36573f6bcfbdSStefan Behrens 	default:
36583f6bcfbdSStefan Behrens 		ret = -EINVAL;
36593f6bcfbdSStefan Behrens 		break;
36603f6bcfbdSStefan Behrens 	}
36613f6bcfbdSStefan Behrens 
36623f6bcfbdSStefan Behrens 	if (copy_to_user(arg, p, sizeof(*p)))
36633f6bcfbdSStefan Behrens 		ret = -EFAULT;
36643f6bcfbdSStefan Behrens 
36653f6bcfbdSStefan Behrens 	kfree(p);
36663f6bcfbdSStefan Behrens 	return ret;
36673f6bcfbdSStefan Behrens }
36683f6bcfbdSStefan Behrens 
3669d7728c96SJan Schmidt static long btrfs_ioctl_ino_to_path(struct btrfs_root *root, void __user *arg)
3670d7728c96SJan Schmidt {
3671d7728c96SJan Schmidt 	int ret = 0;
3672d7728c96SJan Schmidt 	int i;
3673740c3d22SChris Mason 	u64 rel_ptr;
3674d7728c96SJan Schmidt 	int size;
3675806468f8SChris Mason 	struct btrfs_ioctl_ino_path_args *ipa = NULL;
3676d7728c96SJan Schmidt 	struct inode_fs_paths *ipath = NULL;
3677d7728c96SJan Schmidt 	struct btrfs_path *path;
3678d7728c96SJan Schmidt 
367982b22ac8SKusanagi Kouichi 	if (!capable(CAP_DAC_READ_SEARCH))
3680d7728c96SJan Schmidt 		return -EPERM;
3681d7728c96SJan Schmidt 
3682d7728c96SJan Schmidt 	path = btrfs_alloc_path();
3683d7728c96SJan Schmidt 	if (!path) {
3684d7728c96SJan Schmidt 		ret = -ENOMEM;
3685d7728c96SJan Schmidt 		goto out;
3686d7728c96SJan Schmidt 	}
3687d7728c96SJan Schmidt 
3688d7728c96SJan Schmidt 	ipa = memdup_user(arg, sizeof(*ipa));
3689d7728c96SJan Schmidt 	if (IS_ERR(ipa)) {
3690d7728c96SJan Schmidt 		ret = PTR_ERR(ipa);
3691d7728c96SJan Schmidt 		ipa = NULL;
3692d7728c96SJan Schmidt 		goto out;
3693d7728c96SJan Schmidt 	}
3694d7728c96SJan Schmidt 
3695d7728c96SJan Schmidt 	size = min_t(u32, ipa->size, 4096);
3696d7728c96SJan Schmidt 	ipath = init_ipath(size, root, path);
3697d7728c96SJan Schmidt 	if (IS_ERR(ipath)) {
3698d7728c96SJan Schmidt 		ret = PTR_ERR(ipath);
3699d7728c96SJan Schmidt 		ipath = NULL;
3700d7728c96SJan Schmidt 		goto out;
3701d7728c96SJan Schmidt 	}
3702d7728c96SJan Schmidt 
3703d7728c96SJan Schmidt 	ret = paths_from_inode(ipa->inum, ipath);
3704d7728c96SJan Schmidt 	if (ret < 0)
3705d7728c96SJan Schmidt 		goto out;
3706d7728c96SJan Schmidt 
3707d7728c96SJan Schmidt 	for (i = 0; i < ipath->fspath->elem_cnt; ++i) {
3708745c4d8eSJeff Mahoney 		rel_ptr = ipath->fspath->val[i] -
3709745c4d8eSJeff Mahoney 			  (u64)(unsigned long)ipath->fspath->val;
3710740c3d22SChris Mason 		ipath->fspath->val[i] = rel_ptr;
3711d7728c96SJan Schmidt 	}
3712d7728c96SJan Schmidt 
3713745c4d8eSJeff Mahoney 	ret = copy_to_user((void *)(unsigned long)ipa->fspath,
3714745c4d8eSJeff Mahoney 			   (void *)(unsigned long)ipath->fspath, size);
3715d7728c96SJan Schmidt 	if (ret) {
3716d7728c96SJan Schmidt 		ret = -EFAULT;
3717d7728c96SJan Schmidt 		goto out;
3718d7728c96SJan Schmidt 	}
3719d7728c96SJan Schmidt 
3720d7728c96SJan Schmidt out:
3721d7728c96SJan Schmidt 	btrfs_free_path(path);
3722d7728c96SJan Schmidt 	free_ipath(ipath);
3723d7728c96SJan Schmidt 	kfree(ipa);
3724d7728c96SJan Schmidt 
3725d7728c96SJan Schmidt 	return ret;
3726d7728c96SJan Schmidt }
3727d7728c96SJan Schmidt 
3728d7728c96SJan Schmidt static int build_ino_list(u64 inum, u64 offset, u64 root, void *ctx)
3729d7728c96SJan Schmidt {
3730d7728c96SJan Schmidt 	struct btrfs_data_container *inodes = ctx;
3731d7728c96SJan Schmidt 	const size_t c = 3 * sizeof(u64);
3732d7728c96SJan Schmidt 
3733d7728c96SJan Schmidt 	if (inodes->bytes_left >= c) {
3734d7728c96SJan Schmidt 		inodes->bytes_left -= c;
3735d7728c96SJan Schmidt 		inodes->val[inodes->elem_cnt] = inum;
3736d7728c96SJan Schmidt 		inodes->val[inodes->elem_cnt + 1] = offset;
3737d7728c96SJan Schmidt 		inodes->val[inodes->elem_cnt + 2] = root;
3738d7728c96SJan Schmidt 		inodes->elem_cnt += 3;
3739d7728c96SJan Schmidt 	} else {
3740d7728c96SJan Schmidt 		inodes->bytes_missing += c - inodes->bytes_left;
3741d7728c96SJan Schmidt 		inodes->bytes_left = 0;
3742d7728c96SJan Schmidt 		inodes->elem_missed += 3;
3743d7728c96SJan Schmidt 	}
3744d7728c96SJan Schmidt 
3745d7728c96SJan Schmidt 	return 0;
3746d7728c96SJan Schmidt }
3747d7728c96SJan Schmidt 
3748d7728c96SJan Schmidt static long btrfs_ioctl_logical_to_ino(struct btrfs_root *root,
3749d7728c96SJan Schmidt 					void __user *arg)
3750d7728c96SJan Schmidt {
3751d7728c96SJan Schmidt 	int ret = 0;
3752d7728c96SJan Schmidt 	int size;
3753d7728c96SJan Schmidt 	struct btrfs_ioctl_logical_ino_args *loi;
3754d7728c96SJan Schmidt 	struct btrfs_data_container *inodes = NULL;
3755d7728c96SJan Schmidt 	struct btrfs_path *path = NULL;
3756d7728c96SJan Schmidt 
3757d7728c96SJan Schmidt 	if (!capable(CAP_SYS_ADMIN))
3758d7728c96SJan Schmidt 		return -EPERM;
3759d7728c96SJan Schmidt 
3760d7728c96SJan Schmidt 	loi = memdup_user(arg, sizeof(*loi));
3761d7728c96SJan Schmidt 	if (IS_ERR(loi)) {
3762d7728c96SJan Schmidt 		ret = PTR_ERR(loi);
3763d7728c96SJan Schmidt 		loi = NULL;
3764d7728c96SJan Schmidt 		goto out;
3765d7728c96SJan Schmidt 	}
3766d7728c96SJan Schmidt 
3767d7728c96SJan Schmidt 	path = btrfs_alloc_path();
3768d7728c96SJan Schmidt 	if (!path) {
3769d7728c96SJan Schmidt 		ret = -ENOMEM;
3770d7728c96SJan Schmidt 		goto out;
3771d7728c96SJan Schmidt 	}
3772d7728c96SJan Schmidt 
3773425d17a2SLiu Bo 	size = min_t(u32, loi->size, 64 * 1024);
3774d7728c96SJan Schmidt 	inodes = init_data_container(size);
3775d7728c96SJan Schmidt 	if (IS_ERR(inodes)) {
3776d7728c96SJan Schmidt 		ret = PTR_ERR(inodes);
3777d7728c96SJan Schmidt 		inodes = NULL;
3778d7728c96SJan Schmidt 		goto out;
3779d7728c96SJan Schmidt 	}
3780d7728c96SJan Schmidt 
3781df031f07SLiu Bo 	ret = iterate_inodes_from_logical(loi->logical, root->fs_info, path,
3782df031f07SLiu Bo 					  build_ino_list, inodes);
3783df031f07SLiu Bo 	if (ret == -EINVAL)
3784d7728c96SJan Schmidt 		ret = -ENOENT;
3785d7728c96SJan Schmidt 	if (ret < 0)
3786d7728c96SJan Schmidt 		goto out;
3787d7728c96SJan Schmidt 
3788745c4d8eSJeff Mahoney 	ret = copy_to_user((void *)(unsigned long)loi->inodes,
3789745c4d8eSJeff Mahoney 			   (void *)(unsigned long)inodes, size);
3790d7728c96SJan Schmidt 	if (ret)
3791d7728c96SJan Schmidt 		ret = -EFAULT;
3792d7728c96SJan Schmidt 
3793d7728c96SJan Schmidt out:
3794d7728c96SJan Schmidt 	btrfs_free_path(path);
3795425d17a2SLiu Bo 	vfree(inodes);
3796d7728c96SJan Schmidt 	kfree(loi);
3797d7728c96SJan Schmidt 
3798d7728c96SJan Schmidt 	return ret;
3799d7728c96SJan Schmidt }
3800d7728c96SJan Schmidt 
380119a39dceSIlya Dryomov void update_ioctl_balance_args(struct btrfs_fs_info *fs_info, int lock,
3802c9e9f97bSIlya Dryomov 			       struct btrfs_ioctl_balance_args *bargs)
3803c9e9f97bSIlya Dryomov {
3804c9e9f97bSIlya Dryomov 	struct btrfs_balance_control *bctl = fs_info->balance_ctl;
3805c9e9f97bSIlya Dryomov 
3806c9e9f97bSIlya Dryomov 	bargs->flags = bctl->flags;
3807c9e9f97bSIlya Dryomov 
3808837d5b6eSIlya Dryomov 	if (atomic_read(&fs_info->balance_running))
3809837d5b6eSIlya Dryomov 		bargs->state |= BTRFS_BALANCE_STATE_RUNNING;
3810837d5b6eSIlya Dryomov 	if (atomic_read(&fs_info->balance_pause_req))
3811837d5b6eSIlya Dryomov 		bargs->state |= BTRFS_BALANCE_STATE_PAUSE_REQ;
3812a7e99c69SIlya Dryomov 	if (atomic_read(&fs_info->balance_cancel_req))
3813a7e99c69SIlya Dryomov 		bargs->state |= BTRFS_BALANCE_STATE_CANCEL_REQ;
3814837d5b6eSIlya Dryomov 
3815c9e9f97bSIlya Dryomov 	memcpy(&bargs->data, &bctl->data, sizeof(bargs->data));
3816c9e9f97bSIlya Dryomov 	memcpy(&bargs->meta, &bctl->meta, sizeof(bargs->meta));
3817c9e9f97bSIlya Dryomov 	memcpy(&bargs->sys, &bctl->sys, sizeof(bargs->sys));
381819a39dceSIlya Dryomov 
381919a39dceSIlya Dryomov 	if (lock) {
382019a39dceSIlya Dryomov 		spin_lock(&fs_info->balance_lock);
382119a39dceSIlya Dryomov 		memcpy(&bargs->stat, &bctl->stat, sizeof(bargs->stat));
382219a39dceSIlya Dryomov 		spin_unlock(&fs_info->balance_lock);
382319a39dceSIlya Dryomov 	} else {
382419a39dceSIlya Dryomov 		memcpy(&bargs->stat, &bctl->stat, sizeof(bargs->stat));
382519a39dceSIlya Dryomov 	}
3826c9e9f97bSIlya Dryomov }
3827c9e9f97bSIlya Dryomov 
38289ba1f6e4SLiu Bo static long btrfs_ioctl_balance(struct file *file, void __user *arg)
3829c9e9f97bSIlya Dryomov {
3830496ad9aaSAl Viro 	struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
3831c9e9f97bSIlya Dryomov 	struct btrfs_fs_info *fs_info = root->fs_info;
3832c9e9f97bSIlya Dryomov 	struct btrfs_ioctl_balance_args *bargs;
3833c9e9f97bSIlya Dryomov 	struct btrfs_balance_control *bctl;
3834ed0fb78fSIlya Dryomov 	bool need_unlock; /* for mut. excl. ops lock */
3835c9e9f97bSIlya Dryomov 	int ret;
3836c9e9f97bSIlya Dryomov 
3837c9e9f97bSIlya Dryomov 	if (!capable(CAP_SYS_ADMIN))
3838c9e9f97bSIlya Dryomov 		return -EPERM;
3839c9e9f97bSIlya Dryomov 
3840e54bfa31SLiu Bo 	ret = mnt_want_write_file(file);
38419ba1f6e4SLiu Bo 	if (ret)
38429ba1f6e4SLiu Bo 		return ret;
38439ba1f6e4SLiu Bo 
3844ed0fb78fSIlya Dryomov again:
3845ed0fb78fSIlya Dryomov 	if (!atomic_xchg(&fs_info->mutually_exclusive_operation_running, 1)) {
3846c9e9f97bSIlya Dryomov 		mutex_lock(&fs_info->volume_mutex);
3847c9e9f97bSIlya Dryomov 		mutex_lock(&fs_info->balance_mutex);
3848ed0fb78fSIlya Dryomov 		need_unlock = true;
3849ed0fb78fSIlya Dryomov 		goto locked;
3850ed0fb78fSIlya Dryomov 	}
3851ed0fb78fSIlya Dryomov 
3852ed0fb78fSIlya Dryomov 	/*
3853ed0fb78fSIlya Dryomov 	 * mut. excl. ops lock is locked.  Three possibilites:
3854ed0fb78fSIlya Dryomov 	 *   (1) some other op is running
3855ed0fb78fSIlya Dryomov 	 *   (2) balance is running
3856ed0fb78fSIlya Dryomov 	 *   (3) balance is paused -- special case (think resume)
3857ed0fb78fSIlya Dryomov 	 */
3858ed0fb78fSIlya Dryomov 	mutex_lock(&fs_info->balance_mutex);
3859ed0fb78fSIlya Dryomov 	if (fs_info->balance_ctl) {
3860ed0fb78fSIlya Dryomov 		/* this is either (2) or (3) */
3861ed0fb78fSIlya Dryomov 		if (!atomic_read(&fs_info->balance_running)) {
3862ed0fb78fSIlya Dryomov 			mutex_unlock(&fs_info->balance_mutex);
3863ed0fb78fSIlya Dryomov 			if (!mutex_trylock(&fs_info->volume_mutex))
3864ed0fb78fSIlya Dryomov 				goto again;
3865ed0fb78fSIlya Dryomov 			mutex_lock(&fs_info->balance_mutex);
3866ed0fb78fSIlya Dryomov 
3867ed0fb78fSIlya Dryomov 			if (fs_info->balance_ctl &&
3868ed0fb78fSIlya Dryomov 			    !atomic_read(&fs_info->balance_running)) {
3869ed0fb78fSIlya Dryomov 				/* this is (3) */
3870ed0fb78fSIlya Dryomov 				need_unlock = false;
3871ed0fb78fSIlya Dryomov 				goto locked;
3872ed0fb78fSIlya Dryomov 			}
3873ed0fb78fSIlya Dryomov 
3874ed0fb78fSIlya Dryomov 			mutex_unlock(&fs_info->balance_mutex);
3875ed0fb78fSIlya Dryomov 			mutex_unlock(&fs_info->volume_mutex);
3876ed0fb78fSIlya Dryomov 			goto again;
3877ed0fb78fSIlya Dryomov 		} else {
3878ed0fb78fSIlya Dryomov 			/* this is (2) */
3879ed0fb78fSIlya Dryomov 			mutex_unlock(&fs_info->balance_mutex);
3880ed0fb78fSIlya Dryomov 			ret = -EINPROGRESS;
3881ed0fb78fSIlya Dryomov 			goto out;
3882ed0fb78fSIlya Dryomov 		}
3883ed0fb78fSIlya Dryomov 	} else {
3884ed0fb78fSIlya Dryomov 		/* this is (1) */
3885ed0fb78fSIlya Dryomov 		mutex_unlock(&fs_info->balance_mutex);
3886ed0fb78fSIlya Dryomov 		pr_info("btrfs: dev add/delete/balance/replace/resize operation in progress\n");
3887ed0fb78fSIlya Dryomov 		ret = -EINVAL;
3888ed0fb78fSIlya Dryomov 		goto out;
3889ed0fb78fSIlya Dryomov 	}
3890ed0fb78fSIlya Dryomov 
3891ed0fb78fSIlya Dryomov locked:
3892ed0fb78fSIlya Dryomov 	BUG_ON(!atomic_read(&fs_info->mutually_exclusive_operation_running));
3893c9e9f97bSIlya Dryomov 
3894c9e9f97bSIlya Dryomov 	if (arg) {
3895c9e9f97bSIlya Dryomov 		bargs = memdup_user(arg, sizeof(*bargs));
3896c9e9f97bSIlya Dryomov 		if (IS_ERR(bargs)) {
3897c9e9f97bSIlya Dryomov 			ret = PTR_ERR(bargs);
3898ed0fb78fSIlya Dryomov 			goto out_unlock;
3899c9e9f97bSIlya Dryomov 		}
3900de322263SIlya Dryomov 
3901de322263SIlya Dryomov 		if (bargs->flags & BTRFS_BALANCE_RESUME) {
3902de322263SIlya Dryomov 			if (!fs_info->balance_ctl) {
3903de322263SIlya Dryomov 				ret = -ENOTCONN;
3904de322263SIlya Dryomov 				goto out_bargs;
3905de322263SIlya Dryomov 			}
3906de322263SIlya Dryomov 
3907de322263SIlya Dryomov 			bctl = fs_info->balance_ctl;
3908de322263SIlya Dryomov 			spin_lock(&fs_info->balance_lock);
3909de322263SIlya Dryomov 			bctl->flags |= BTRFS_BALANCE_RESUME;
3910de322263SIlya Dryomov 			spin_unlock(&fs_info->balance_lock);
3911de322263SIlya Dryomov 
3912de322263SIlya Dryomov 			goto do_balance;
3913de322263SIlya Dryomov 		}
3914c9e9f97bSIlya Dryomov 	} else {
3915c9e9f97bSIlya Dryomov 		bargs = NULL;
3916c9e9f97bSIlya Dryomov 	}
3917c9e9f97bSIlya Dryomov 
3918ed0fb78fSIlya Dryomov 	if (fs_info->balance_ctl) {
3919837d5b6eSIlya Dryomov 		ret = -EINPROGRESS;
3920837d5b6eSIlya Dryomov 		goto out_bargs;
3921837d5b6eSIlya Dryomov 	}
3922837d5b6eSIlya Dryomov 
3923c9e9f97bSIlya Dryomov 	bctl = kzalloc(sizeof(*bctl), GFP_NOFS);
3924c9e9f97bSIlya Dryomov 	if (!bctl) {
3925c9e9f97bSIlya Dryomov 		ret = -ENOMEM;
3926c9e9f97bSIlya Dryomov 		goto out_bargs;
3927c9e9f97bSIlya Dryomov 	}
3928c9e9f97bSIlya Dryomov 
3929c9e9f97bSIlya Dryomov 	bctl->fs_info = fs_info;
3930c9e9f97bSIlya Dryomov 	if (arg) {
3931c9e9f97bSIlya Dryomov 		memcpy(&bctl->data, &bargs->data, sizeof(bctl->data));
3932c9e9f97bSIlya Dryomov 		memcpy(&bctl->meta, &bargs->meta, sizeof(bctl->meta));
3933c9e9f97bSIlya Dryomov 		memcpy(&bctl->sys, &bargs->sys, sizeof(bctl->sys));
3934c9e9f97bSIlya Dryomov 
3935c9e9f97bSIlya Dryomov 		bctl->flags = bargs->flags;
3936f43ffb60SIlya Dryomov 	} else {
3937f43ffb60SIlya Dryomov 		/* balance everything - no filters */
3938f43ffb60SIlya Dryomov 		bctl->flags |= BTRFS_BALANCE_TYPE_MASK;
3939c9e9f97bSIlya Dryomov 	}
3940c9e9f97bSIlya Dryomov 
3941de322263SIlya Dryomov do_balance:
3942c9e9f97bSIlya Dryomov 	/*
3943ed0fb78fSIlya Dryomov 	 * Ownership of bctl and mutually_exclusive_operation_running
3944ed0fb78fSIlya Dryomov 	 * goes to to btrfs_balance.  bctl is freed in __cancel_balance,
3945ed0fb78fSIlya Dryomov 	 * or, if restriper was paused all the way until unmount, in
3946ed0fb78fSIlya Dryomov 	 * free_fs_info.  mutually_exclusive_operation_running is
3947ed0fb78fSIlya Dryomov 	 * cleared in __cancel_balance.
3948c9e9f97bSIlya Dryomov 	 */
3949ed0fb78fSIlya Dryomov 	need_unlock = false;
3950ed0fb78fSIlya Dryomov 
3951ed0fb78fSIlya Dryomov 	ret = btrfs_balance(bctl, bargs);
3952ed0fb78fSIlya Dryomov 
3953c9e9f97bSIlya Dryomov 	if (arg) {
3954c9e9f97bSIlya Dryomov 		if (copy_to_user(arg, bargs, sizeof(*bargs)))
3955c9e9f97bSIlya Dryomov 			ret = -EFAULT;
3956c9e9f97bSIlya Dryomov 	}
3957c9e9f97bSIlya Dryomov 
3958c9e9f97bSIlya Dryomov out_bargs:
3959c9e9f97bSIlya Dryomov 	kfree(bargs);
3960ed0fb78fSIlya Dryomov out_unlock:
3961c9e9f97bSIlya Dryomov 	mutex_unlock(&fs_info->balance_mutex);
3962c9e9f97bSIlya Dryomov 	mutex_unlock(&fs_info->volume_mutex);
3963ed0fb78fSIlya Dryomov 	if (need_unlock)
3964ed0fb78fSIlya Dryomov 		atomic_set(&fs_info->mutually_exclusive_operation_running, 0);
3965ed0fb78fSIlya Dryomov out:
3966e54bfa31SLiu Bo 	mnt_drop_write_file(file);
3967c9e9f97bSIlya Dryomov 	return ret;
3968c9e9f97bSIlya Dryomov }
3969c9e9f97bSIlya Dryomov 
3970837d5b6eSIlya Dryomov static long btrfs_ioctl_balance_ctl(struct btrfs_root *root, int cmd)
3971837d5b6eSIlya Dryomov {
3972837d5b6eSIlya Dryomov 	if (!capable(CAP_SYS_ADMIN))
3973837d5b6eSIlya Dryomov 		return -EPERM;
3974837d5b6eSIlya Dryomov 
3975837d5b6eSIlya Dryomov 	switch (cmd) {
3976837d5b6eSIlya Dryomov 	case BTRFS_BALANCE_CTL_PAUSE:
3977837d5b6eSIlya Dryomov 		return btrfs_pause_balance(root->fs_info);
3978a7e99c69SIlya Dryomov 	case BTRFS_BALANCE_CTL_CANCEL:
3979a7e99c69SIlya Dryomov 		return btrfs_cancel_balance(root->fs_info);
3980837d5b6eSIlya Dryomov 	}
3981837d5b6eSIlya Dryomov 
3982837d5b6eSIlya Dryomov 	return -EINVAL;
3983837d5b6eSIlya Dryomov }
3984837d5b6eSIlya Dryomov 
398519a39dceSIlya Dryomov static long btrfs_ioctl_balance_progress(struct btrfs_root *root,
398619a39dceSIlya Dryomov 					 void __user *arg)
398719a39dceSIlya Dryomov {
398819a39dceSIlya Dryomov 	struct btrfs_fs_info *fs_info = root->fs_info;
398919a39dceSIlya Dryomov 	struct btrfs_ioctl_balance_args *bargs;
399019a39dceSIlya Dryomov 	int ret = 0;
399119a39dceSIlya Dryomov 
399219a39dceSIlya Dryomov 	if (!capable(CAP_SYS_ADMIN))
399319a39dceSIlya Dryomov 		return -EPERM;
399419a39dceSIlya Dryomov 
399519a39dceSIlya Dryomov 	mutex_lock(&fs_info->balance_mutex);
399619a39dceSIlya Dryomov 	if (!fs_info->balance_ctl) {
399719a39dceSIlya Dryomov 		ret = -ENOTCONN;
399819a39dceSIlya Dryomov 		goto out;
399919a39dceSIlya Dryomov 	}
400019a39dceSIlya Dryomov 
400119a39dceSIlya Dryomov 	bargs = kzalloc(sizeof(*bargs), GFP_NOFS);
400219a39dceSIlya Dryomov 	if (!bargs) {
400319a39dceSIlya Dryomov 		ret = -ENOMEM;
400419a39dceSIlya Dryomov 		goto out;
400519a39dceSIlya Dryomov 	}
400619a39dceSIlya Dryomov 
400719a39dceSIlya Dryomov 	update_ioctl_balance_args(fs_info, 1, bargs);
400819a39dceSIlya Dryomov 
400919a39dceSIlya Dryomov 	if (copy_to_user(arg, bargs, sizeof(*bargs)))
401019a39dceSIlya Dryomov 		ret = -EFAULT;
401119a39dceSIlya Dryomov 
401219a39dceSIlya Dryomov 	kfree(bargs);
401319a39dceSIlya Dryomov out:
401419a39dceSIlya Dryomov 	mutex_unlock(&fs_info->balance_mutex);
401519a39dceSIlya Dryomov 	return ret;
401619a39dceSIlya Dryomov }
401719a39dceSIlya Dryomov 
4018905b0ddaSMiao Xie static long btrfs_ioctl_quota_ctl(struct file *file, void __user *arg)
40195d13a37bSArne Jansen {
4020496ad9aaSAl Viro 	struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
40215d13a37bSArne Jansen 	struct btrfs_ioctl_quota_ctl_args *sa;
40225d13a37bSArne Jansen 	struct btrfs_trans_handle *trans = NULL;
40235d13a37bSArne Jansen 	int ret;
40245d13a37bSArne Jansen 	int err;
40255d13a37bSArne Jansen 
40265d13a37bSArne Jansen 	if (!capable(CAP_SYS_ADMIN))
40275d13a37bSArne Jansen 		return -EPERM;
40285d13a37bSArne Jansen 
4029905b0ddaSMiao Xie 	ret = mnt_want_write_file(file);
4030905b0ddaSMiao Xie 	if (ret)
4031905b0ddaSMiao Xie 		return ret;
40325d13a37bSArne Jansen 
40335d13a37bSArne Jansen 	sa = memdup_user(arg, sizeof(*sa));
4034905b0ddaSMiao Xie 	if (IS_ERR(sa)) {
4035905b0ddaSMiao Xie 		ret = PTR_ERR(sa);
4036905b0ddaSMiao Xie 		goto drop_write;
4037905b0ddaSMiao Xie 	}
40385d13a37bSArne Jansen 
40397708f029SWang Shilong 	down_write(&root->fs_info->subvol_sem);
404092f183aaSWang Shilong 	trans = btrfs_start_transaction(root->fs_info->tree_root, 2);
40415d13a37bSArne Jansen 	if (IS_ERR(trans)) {
40425d13a37bSArne Jansen 		ret = PTR_ERR(trans);
40435d13a37bSArne Jansen 		goto out;
40445d13a37bSArne Jansen 	}
40455d13a37bSArne Jansen 
40465d13a37bSArne Jansen 	switch (sa->cmd) {
40475d13a37bSArne Jansen 	case BTRFS_QUOTA_CTL_ENABLE:
40485d13a37bSArne Jansen 		ret = btrfs_quota_enable(trans, root->fs_info);
40495d13a37bSArne Jansen 		break;
40505d13a37bSArne Jansen 	case BTRFS_QUOTA_CTL_DISABLE:
40515d13a37bSArne Jansen 		ret = btrfs_quota_disable(trans, root->fs_info);
40525d13a37bSArne Jansen 		break;
40535d13a37bSArne Jansen 	default:
40545d13a37bSArne Jansen 		ret = -EINVAL;
40555d13a37bSArne Jansen 		break;
40565d13a37bSArne Jansen 	}
40575d13a37bSArne Jansen 
405892f183aaSWang Shilong 	err = btrfs_commit_transaction(trans, root->fs_info->tree_root);
40595d13a37bSArne Jansen 	if (err && !ret)
40605d13a37bSArne Jansen 		ret = err;
40615d13a37bSArne Jansen out:
40625d13a37bSArne Jansen 	kfree(sa);
40637708f029SWang Shilong 	up_write(&root->fs_info->subvol_sem);
4064905b0ddaSMiao Xie drop_write:
4065905b0ddaSMiao Xie 	mnt_drop_write_file(file);
40665d13a37bSArne Jansen 	return ret;
40675d13a37bSArne Jansen }
40685d13a37bSArne Jansen 
4069905b0ddaSMiao Xie static long btrfs_ioctl_qgroup_assign(struct file *file, void __user *arg)
40705d13a37bSArne Jansen {
4071496ad9aaSAl Viro 	struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
40725d13a37bSArne Jansen 	struct btrfs_ioctl_qgroup_assign_args *sa;
40735d13a37bSArne Jansen 	struct btrfs_trans_handle *trans;
40745d13a37bSArne Jansen 	int ret;
40755d13a37bSArne Jansen 	int err;
40765d13a37bSArne Jansen 
40775d13a37bSArne Jansen 	if (!capable(CAP_SYS_ADMIN))
40785d13a37bSArne Jansen 		return -EPERM;
40795d13a37bSArne Jansen 
4080905b0ddaSMiao Xie 	ret = mnt_want_write_file(file);
4081905b0ddaSMiao Xie 	if (ret)
4082905b0ddaSMiao Xie 		return ret;
40835d13a37bSArne Jansen 
40845d13a37bSArne Jansen 	sa = memdup_user(arg, sizeof(*sa));
4085905b0ddaSMiao Xie 	if (IS_ERR(sa)) {
4086905b0ddaSMiao Xie 		ret = PTR_ERR(sa);
4087905b0ddaSMiao Xie 		goto drop_write;
4088905b0ddaSMiao Xie 	}
40895d13a37bSArne Jansen 
40905d13a37bSArne Jansen 	trans = btrfs_join_transaction(root);
40915d13a37bSArne Jansen 	if (IS_ERR(trans)) {
40925d13a37bSArne Jansen 		ret = PTR_ERR(trans);
40935d13a37bSArne Jansen 		goto out;
40945d13a37bSArne Jansen 	}
40955d13a37bSArne Jansen 
40965d13a37bSArne Jansen 	/* FIXME: check if the IDs really exist */
40975d13a37bSArne Jansen 	if (sa->assign) {
40985d13a37bSArne Jansen 		ret = btrfs_add_qgroup_relation(trans, root->fs_info,
40995d13a37bSArne Jansen 						sa->src, sa->dst);
41005d13a37bSArne Jansen 	} else {
41015d13a37bSArne Jansen 		ret = btrfs_del_qgroup_relation(trans, root->fs_info,
41025d13a37bSArne Jansen 						sa->src, sa->dst);
41035d13a37bSArne Jansen 	}
41045d13a37bSArne Jansen 
41055d13a37bSArne Jansen 	err = btrfs_end_transaction(trans, root);
41065d13a37bSArne Jansen 	if (err && !ret)
41075d13a37bSArne Jansen 		ret = err;
41085d13a37bSArne Jansen 
41095d13a37bSArne Jansen out:
41105d13a37bSArne Jansen 	kfree(sa);
4111905b0ddaSMiao Xie drop_write:
4112905b0ddaSMiao Xie 	mnt_drop_write_file(file);
41135d13a37bSArne Jansen 	return ret;
41145d13a37bSArne Jansen }
41155d13a37bSArne Jansen 
4116905b0ddaSMiao Xie static long btrfs_ioctl_qgroup_create(struct file *file, void __user *arg)
41175d13a37bSArne Jansen {
4118496ad9aaSAl Viro 	struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
41195d13a37bSArne Jansen 	struct btrfs_ioctl_qgroup_create_args *sa;
41205d13a37bSArne Jansen 	struct btrfs_trans_handle *trans;
41215d13a37bSArne Jansen 	int ret;
41225d13a37bSArne Jansen 	int err;
41235d13a37bSArne Jansen 
41245d13a37bSArne Jansen 	if (!capable(CAP_SYS_ADMIN))
41255d13a37bSArne Jansen 		return -EPERM;
41265d13a37bSArne Jansen 
4127905b0ddaSMiao Xie 	ret = mnt_want_write_file(file);
4128905b0ddaSMiao Xie 	if (ret)
4129905b0ddaSMiao Xie 		return ret;
41305d13a37bSArne Jansen 
41315d13a37bSArne Jansen 	sa = memdup_user(arg, sizeof(*sa));
4132905b0ddaSMiao Xie 	if (IS_ERR(sa)) {
4133905b0ddaSMiao Xie 		ret = PTR_ERR(sa);
4134905b0ddaSMiao Xie 		goto drop_write;
4135905b0ddaSMiao Xie 	}
41365d13a37bSArne Jansen 
4137d86e56cfSMiao Xie 	if (!sa->qgroupid) {
4138d86e56cfSMiao Xie 		ret = -EINVAL;
4139d86e56cfSMiao Xie 		goto out;
4140d86e56cfSMiao Xie 	}
4141d86e56cfSMiao Xie 
41425d13a37bSArne Jansen 	trans = btrfs_join_transaction(root);
41435d13a37bSArne Jansen 	if (IS_ERR(trans)) {
41445d13a37bSArne Jansen 		ret = PTR_ERR(trans);
41455d13a37bSArne Jansen 		goto out;
41465d13a37bSArne Jansen 	}
41475d13a37bSArne Jansen 
41485d13a37bSArne Jansen 	/* FIXME: check if the IDs really exist */
41495d13a37bSArne Jansen 	if (sa->create) {
41505d13a37bSArne Jansen 		ret = btrfs_create_qgroup(trans, root->fs_info, sa->qgroupid,
41515d13a37bSArne Jansen 					  NULL);
41525d13a37bSArne Jansen 	} else {
41535d13a37bSArne Jansen 		ret = btrfs_remove_qgroup(trans, root->fs_info, sa->qgroupid);
41545d13a37bSArne Jansen 	}
41555d13a37bSArne Jansen 
41565d13a37bSArne Jansen 	err = btrfs_end_transaction(trans, root);
41575d13a37bSArne Jansen 	if (err && !ret)
41585d13a37bSArne Jansen 		ret = err;
41595d13a37bSArne Jansen 
41605d13a37bSArne Jansen out:
41615d13a37bSArne Jansen 	kfree(sa);
4162905b0ddaSMiao Xie drop_write:
4163905b0ddaSMiao Xie 	mnt_drop_write_file(file);
41645d13a37bSArne Jansen 	return ret;
41655d13a37bSArne Jansen }
41665d13a37bSArne Jansen 
4167905b0ddaSMiao Xie static long btrfs_ioctl_qgroup_limit(struct file *file, void __user *arg)
41685d13a37bSArne Jansen {
4169496ad9aaSAl Viro 	struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
41705d13a37bSArne Jansen 	struct btrfs_ioctl_qgroup_limit_args *sa;
41715d13a37bSArne Jansen 	struct btrfs_trans_handle *trans;
41725d13a37bSArne Jansen 	int ret;
41735d13a37bSArne Jansen 	int err;
41745d13a37bSArne Jansen 	u64 qgroupid;
41755d13a37bSArne Jansen 
41765d13a37bSArne Jansen 	if (!capable(CAP_SYS_ADMIN))
41775d13a37bSArne Jansen 		return -EPERM;
41785d13a37bSArne Jansen 
4179905b0ddaSMiao Xie 	ret = mnt_want_write_file(file);
4180905b0ddaSMiao Xie 	if (ret)
4181905b0ddaSMiao Xie 		return ret;
41825d13a37bSArne Jansen 
41835d13a37bSArne Jansen 	sa = memdup_user(arg, sizeof(*sa));
4184905b0ddaSMiao Xie 	if (IS_ERR(sa)) {
4185905b0ddaSMiao Xie 		ret = PTR_ERR(sa);
4186905b0ddaSMiao Xie 		goto drop_write;
4187905b0ddaSMiao Xie 	}
41885d13a37bSArne Jansen 
41895d13a37bSArne Jansen 	trans = btrfs_join_transaction(root);
41905d13a37bSArne Jansen 	if (IS_ERR(trans)) {
41915d13a37bSArne Jansen 		ret = PTR_ERR(trans);
41925d13a37bSArne Jansen 		goto out;
41935d13a37bSArne Jansen 	}
41945d13a37bSArne Jansen 
41955d13a37bSArne Jansen 	qgroupid = sa->qgroupid;
41965d13a37bSArne Jansen 	if (!qgroupid) {
41975d13a37bSArne Jansen 		/* take the current subvol as qgroup */
41985d13a37bSArne Jansen 		qgroupid = root->root_key.objectid;
41995d13a37bSArne Jansen 	}
42005d13a37bSArne Jansen 
42015d13a37bSArne Jansen 	/* FIXME: check if the IDs really exist */
42025d13a37bSArne Jansen 	ret = btrfs_limit_qgroup(trans, root->fs_info, qgroupid, &sa->lim);
42035d13a37bSArne Jansen 
42045d13a37bSArne Jansen 	err = btrfs_end_transaction(trans, root);
42055d13a37bSArne Jansen 	if (err && !ret)
42065d13a37bSArne Jansen 		ret = err;
42075d13a37bSArne Jansen 
42085d13a37bSArne Jansen out:
42095d13a37bSArne Jansen 	kfree(sa);
4210905b0ddaSMiao Xie drop_write:
4211905b0ddaSMiao Xie 	mnt_drop_write_file(file);
42125d13a37bSArne Jansen 	return ret;
42135d13a37bSArne Jansen }
42145d13a37bSArne Jansen 
42152f232036SJan Schmidt static long btrfs_ioctl_quota_rescan(struct file *file, void __user *arg)
42162f232036SJan Schmidt {
42176d0379ecSAl Viro 	struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
42182f232036SJan Schmidt 	struct btrfs_ioctl_quota_rescan_args *qsa;
42192f232036SJan Schmidt 	int ret;
42202f232036SJan Schmidt 
42212f232036SJan Schmidt 	if (!capable(CAP_SYS_ADMIN))
42222f232036SJan Schmidt 		return -EPERM;
42232f232036SJan Schmidt 
42242f232036SJan Schmidt 	ret = mnt_want_write_file(file);
42252f232036SJan Schmidt 	if (ret)
42262f232036SJan Schmidt 		return ret;
42272f232036SJan Schmidt 
42282f232036SJan Schmidt 	qsa = memdup_user(arg, sizeof(*qsa));
42292f232036SJan Schmidt 	if (IS_ERR(qsa)) {
42302f232036SJan Schmidt 		ret = PTR_ERR(qsa);
42312f232036SJan Schmidt 		goto drop_write;
42322f232036SJan Schmidt 	}
42332f232036SJan Schmidt 
42342f232036SJan Schmidt 	if (qsa->flags) {
42352f232036SJan Schmidt 		ret = -EINVAL;
42362f232036SJan Schmidt 		goto out;
42372f232036SJan Schmidt 	}
42382f232036SJan Schmidt 
42392f232036SJan Schmidt 	ret = btrfs_qgroup_rescan(root->fs_info);
42402f232036SJan Schmidt 
42412f232036SJan Schmidt out:
42422f232036SJan Schmidt 	kfree(qsa);
42432f232036SJan Schmidt drop_write:
42442f232036SJan Schmidt 	mnt_drop_write_file(file);
42452f232036SJan Schmidt 	return ret;
42462f232036SJan Schmidt }
42472f232036SJan Schmidt 
42482f232036SJan Schmidt static long btrfs_ioctl_quota_rescan_status(struct file *file, void __user *arg)
42492f232036SJan Schmidt {
42506d0379ecSAl Viro 	struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
42512f232036SJan Schmidt 	struct btrfs_ioctl_quota_rescan_args *qsa;
42522f232036SJan Schmidt 	int ret = 0;
42532f232036SJan Schmidt 
42542f232036SJan Schmidt 	if (!capable(CAP_SYS_ADMIN))
42552f232036SJan Schmidt 		return -EPERM;
42562f232036SJan Schmidt 
42572f232036SJan Schmidt 	qsa = kzalloc(sizeof(*qsa), GFP_NOFS);
42582f232036SJan Schmidt 	if (!qsa)
42592f232036SJan Schmidt 		return -ENOMEM;
42602f232036SJan Schmidt 
42612f232036SJan Schmidt 	if (root->fs_info->qgroup_flags & BTRFS_QGROUP_STATUS_FLAG_RESCAN) {
42622f232036SJan Schmidt 		qsa->flags = 1;
42632f232036SJan Schmidt 		qsa->progress = root->fs_info->qgroup_rescan_progress.objectid;
42642f232036SJan Schmidt 	}
42652f232036SJan Schmidt 
42662f232036SJan Schmidt 	if (copy_to_user(arg, qsa, sizeof(*qsa)))
42672f232036SJan Schmidt 		ret = -EFAULT;
42682f232036SJan Schmidt 
42692f232036SJan Schmidt 	kfree(qsa);
42702f232036SJan Schmidt 	return ret;
42712f232036SJan Schmidt }
42722f232036SJan Schmidt 
427357254b6eSJan Schmidt static long btrfs_ioctl_quota_rescan_wait(struct file *file, void __user *arg)
427457254b6eSJan Schmidt {
427557254b6eSJan Schmidt 	struct btrfs_root *root = BTRFS_I(fdentry(file)->d_inode)->root;
427657254b6eSJan Schmidt 
427757254b6eSJan Schmidt 	if (!capable(CAP_SYS_ADMIN))
427857254b6eSJan Schmidt 		return -EPERM;
427957254b6eSJan Schmidt 
428057254b6eSJan Schmidt 	return btrfs_qgroup_wait_for_completion(root->fs_info);
428157254b6eSJan Schmidt }
428257254b6eSJan Schmidt 
42838ea05e3aSAlexander Block static long btrfs_ioctl_set_received_subvol(struct file *file,
42848ea05e3aSAlexander Block 					    void __user *arg)
42858ea05e3aSAlexander Block {
42868ea05e3aSAlexander Block 	struct btrfs_ioctl_received_subvol_args *sa = NULL;
4287496ad9aaSAl Viro 	struct inode *inode = file_inode(file);
42888ea05e3aSAlexander Block 	struct btrfs_root *root = BTRFS_I(inode)->root;
42898ea05e3aSAlexander Block 	struct btrfs_root_item *root_item = &root->root_item;
42908ea05e3aSAlexander Block 	struct btrfs_trans_handle *trans;
42918ea05e3aSAlexander Block 	struct timespec ct = CURRENT_TIME;
42928ea05e3aSAlexander Block 	int ret = 0;
42938ea05e3aSAlexander Block 
42948ea05e3aSAlexander Block 	ret = mnt_want_write_file(file);
42958ea05e3aSAlexander Block 	if (ret < 0)
42968ea05e3aSAlexander Block 		return ret;
42978ea05e3aSAlexander Block 
42988ea05e3aSAlexander Block 	down_write(&root->fs_info->subvol_sem);
42998ea05e3aSAlexander Block 
43008ea05e3aSAlexander Block 	if (btrfs_ino(inode) != BTRFS_FIRST_FREE_OBJECTID) {
43018ea05e3aSAlexander Block 		ret = -EINVAL;
43028ea05e3aSAlexander Block 		goto out;
43038ea05e3aSAlexander Block 	}
43048ea05e3aSAlexander Block 
43058ea05e3aSAlexander Block 	if (btrfs_root_readonly(root)) {
43068ea05e3aSAlexander Block 		ret = -EROFS;
43078ea05e3aSAlexander Block 		goto out;
43088ea05e3aSAlexander Block 	}
43098ea05e3aSAlexander Block 
43108ea05e3aSAlexander Block 	if (!inode_owner_or_capable(inode)) {
43118ea05e3aSAlexander Block 		ret = -EACCES;
43128ea05e3aSAlexander Block 		goto out;
43138ea05e3aSAlexander Block 	}
43148ea05e3aSAlexander Block 
43158ea05e3aSAlexander Block 	sa = memdup_user(arg, sizeof(*sa));
43168ea05e3aSAlexander Block 	if (IS_ERR(sa)) {
43178ea05e3aSAlexander Block 		ret = PTR_ERR(sa);
43188ea05e3aSAlexander Block 		sa = NULL;
43198ea05e3aSAlexander Block 		goto out;
43208ea05e3aSAlexander Block 	}
43218ea05e3aSAlexander Block 
43228ea05e3aSAlexander Block 	trans = btrfs_start_transaction(root, 1);
43238ea05e3aSAlexander Block 	if (IS_ERR(trans)) {
43248ea05e3aSAlexander Block 		ret = PTR_ERR(trans);
43258ea05e3aSAlexander Block 		trans = NULL;
43268ea05e3aSAlexander Block 		goto out;
43278ea05e3aSAlexander Block 	}
43288ea05e3aSAlexander Block 
43298ea05e3aSAlexander Block 	sa->rtransid = trans->transid;
43308ea05e3aSAlexander Block 	sa->rtime.sec = ct.tv_sec;
43318ea05e3aSAlexander Block 	sa->rtime.nsec = ct.tv_nsec;
43328ea05e3aSAlexander Block 
43338ea05e3aSAlexander Block 	memcpy(root_item->received_uuid, sa->uuid, BTRFS_UUID_SIZE);
43348ea05e3aSAlexander Block 	btrfs_set_root_stransid(root_item, sa->stransid);
43358ea05e3aSAlexander Block 	btrfs_set_root_rtransid(root_item, sa->rtransid);
43363cae210fSQu Wenruo 	btrfs_set_stack_timespec_sec(&root_item->stime, sa->stime.sec);
43373cae210fSQu Wenruo 	btrfs_set_stack_timespec_nsec(&root_item->stime, sa->stime.nsec);
43383cae210fSQu Wenruo 	btrfs_set_stack_timespec_sec(&root_item->rtime, sa->rtime.sec);
43393cae210fSQu Wenruo 	btrfs_set_stack_timespec_nsec(&root_item->rtime, sa->rtime.nsec);
43408ea05e3aSAlexander Block 
43418ea05e3aSAlexander Block 	ret = btrfs_update_root(trans, root->fs_info->tree_root,
43428ea05e3aSAlexander Block 				&root->root_key, &root->root_item);
43438ea05e3aSAlexander Block 	if (ret < 0) {
43448ea05e3aSAlexander Block 		btrfs_end_transaction(trans, root);
43458ea05e3aSAlexander Block 		trans = NULL;
43468ea05e3aSAlexander Block 		goto out;
43478ea05e3aSAlexander Block 	} else {
43488ea05e3aSAlexander Block 		ret = btrfs_commit_transaction(trans, root);
43498ea05e3aSAlexander Block 		if (ret < 0)
43508ea05e3aSAlexander Block 			goto out;
43518ea05e3aSAlexander Block 	}
43528ea05e3aSAlexander Block 
43538ea05e3aSAlexander Block 	ret = copy_to_user(arg, sa, sizeof(*sa));
43548ea05e3aSAlexander Block 	if (ret)
43558ea05e3aSAlexander Block 		ret = -EFAULT;
43568ea05e3aSAlexander Block 
43578ea05e3aSAlexander Block out:
43588ea05e3aSAlexander Block 	kfree(sa);
43598ea05e3aSAlexander Block 	up_write(&root->fs_info->subvol_sem);
43608ea05e3aSAlexander Block 	mnt_drop_write_file(file);
43618ea05e3aSAlexander Block 	return ret;
43628ea05e3aSAlexander Block }
43638ea05e3aSAlexander Block 
4364867ab667Sjeff.liu static int btrfs_ioctl_get_fslabel(struct file *file, void __user *arg)
4365867ab667Sjeff.liu {
43666d0379ecSAl Viro 	struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
4367a1b83ac5SAnand Jain 	size_t len;
4368867ab667Sjeff.liu 	int ret;
4369a1b83ac5SAnand Jain 	char label[BTRFS_LABEL_SIZE];
4370a1b83ac5SAnand Jain 
4371a1b83ac5SAnand Jain 	spin_lock(&root->fs_info->super_lock);
4372a1b83ac5SAnand Jain 	memcpy(label, root->fs_info->super_copy->label, BTRFS_LABEL_SIZE);
4373a1b83ac5SAnand Jain 	spin_unlock(&root->fs_info->super_lock);
4374a1b83ac5SAnand Jain 
4375a1b83ac5SAnand Jain 	len = strnlen(label, BTRFS_LABEL_SIZE);
4376867ab667Sjeff.liu 
4377867ab667Sjeff.liu 	if (len == BTRFS_LABEL_SIZE) {
4378867ab667Sjeff.liu 		pr_warn("btrfs: label is too long, return the first %zu bytes\n",
4379867ab667Sjeff.liu 			--len);
4380867ab667Sjeff.liu 	}
4381867ab667Sjeff.liu 
4382867ab667Sjeff.liu 	ret = copy_to_user(arg, label, len);
4383867ab667Sjeff.liu 
4384867ab667Sjeff.liu 	return ret ? -EFAULT : 0;
4385867ab667Sjeff.liu }
4386867ab667Sjeff.liu 
4387a8bfd4abSjeff.liu static int btrfs_ioctl_set_fslabel(struct file *file, void __user *arg)
4388a8bfd4abSjeff.liu {
43896d0379ecSAl Viro 	struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
4390a8bfd4abSjeff.liu 	struct btrfs_super_block *super_block = root->fs_info->super_copy;
4391a8bfd4abSjeff.liu 	struct btrfs_trans_handle *trans;
4392a8bfd4abSjeff.liu 	char label[BTRFS_LABEL_SIZE];
4393a8bfd4abSjeff.liu 	int ret;
4394a8bfd4abSjeff.liu 
4395a8bfd4abSjeff.liu 	if (!capable(CAP_SYS_ADMIN))
4396a8bfd4abSjeff.liu 		return -EPERM;
4397a8bfd4abSjeff.liu 
4398a8bfd4abSjeff.liu 	if (copy_from_user(label, arg, sizeof(label)))
4399a8bfd4abSjeff.liu 		return -EFAULT;
4400a8bfd4abSjeff.liu 
4401a8bfd4abSjeff.liu 	if (strnlen(label, BTRFS_LABEL_SIZE) == BTRFS_LABEL_SIZE) {
4402a8bfd4abSjeff.liu 		pr_err("btrfs: unable to set label with more than %d bytes\n",
4403a8bfd4abSjeff.liu 		       BTRFS_LABEL_SIZE - 1);
4404a8bfd4abSjeff.liu 		return -EINVAL;
4405a8bfd4abSjeff.liu 	}
4406a8bfd4abSjeff.liu 
4407a8bfd4abSjeff.liu 	ret = mnt_want_write_file(file);
4408a8bfd4abSjeff.liu 	if (ret)
4409a8bfd4abSjeff.liu 		return ret;
4410a8bfd4abSjeff.liu 
4411a8bfd4abSjeff.liu 	trans = btrfs_start_transaction(root, 0);
4412a8bfd4abSjeff.liu 	if (IS_ERR(trans)) {
4413a8bfd4abSjeff.liu 		ret = PTR_ERR(trans);
4414a8bfd4abSjeff.liu 		goto out_unlock;
4415a8bfd4abSjeff.liu 	}
4416a8bfd4abSjeff.liu 
4417a1b83ac5SAnand Jain 	spin_lock(&root->fs_info->super_lock);
4418a8bfd4abSjeff.liu 	strcpy(super_block->label, label);
4419a1b83ac5SAnand Jain 	spin_unlock(&root->fs_info->super_lock);
4420a8bfd4abSjeff.liu 	ret = btrfs_end_transaction(trans, root);
4421a8bfd4abSjeff.liu 
4422a8bfd4abSjeff.liu out_unlock:
4423a8bfd4abSjeff.liu 	mnt_drop_write_file(file);
4424a8bfd4abSjeff.liu 	return ret;
4425a8bfd4abSjeff.liu }
4426a8bfd4abSjeff.liu 
4427f46b5a66SChristoph Hellwig long btrfs_ioctl(struct file *file, unsigned int
4428f46b5a66SChristoph Hellwig 		cmd, unsigned long arg)
4429f46b5a66SChristoph Hellwig {
4430496ad9aaSAl Viro 	struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
44314bcabaa3SChristoph Hellwig 	void __user *argp = (void __user *)arg;
4432f46b5a66SChristoph Hellwig 
4433f46b5a66SChristoph Hellwig 	switch (cmd) {
44346cbff00fSChristoph Hellwig 	case FS_IOC_GETFLAGS:
44356cbff00fSChristoph Hellwig 		return btrfs_ioctl_getflags(file, argp);
44366cbff00fSChristoph Hellwig 	case FS_IOC_SETFLAGS:
44376cbff00fSChristoph Hellwig 		return btrfs_ioctl_setflags(file, argp);
44386cbff00fSChristoph Hellwig 	case FS_IOC_GETVERSION:
44396cbff00fSChristoph Hellwig 		return btrfs_ioctl_getversion(file, argp);
4440f7039b1dSLi Dongyang 	case FITRIM:
4441f7039b1dSLi Dongyang 		return btrfs_ioctl_fitrim(file, argp);
4442f46b5a66SChristoph Hellwig 	case BTRFS_IOC_SNAP_CREATE:
4443fa0d2b9bSLi Zefan 		return btrfs_ioctl_snap_create(file, argp, 0);
4444fdfb1e4fSLi Zefan 	case BTRFS_IOC_SNAP_CREATE_V2:
4445fa0d2b9bSLi Zefan 		return btrfs_ioctl_snap_create_v2(file, argp, 0);
44463de4586cSChris Mason 	case BTRFS_IOC_SUBVOL_CREATE:
4447fa0d2b9bSLi Zefan 		return btrfs_ioctl_snap_create(file, argp, 1);
44486f72c7e2SArne Jansen 	case BTRFS_IOC_SUBVOL_CREATE_V2:
44496f72c7e2SArne Jansen 		return btrfs_ioctl_snap_create_v2(file, argp, 1);
445076dda93cSYan, Zheng 	case BTRFS_IOC_SNAP_DESTROY:
445176dda93cSYan, Zheng 		return btrfs_ioctl_snap_destroy(file, argp);
44520caa102dSLi Zefan 	case BTRFS_IOC_SUBVOL_GETFLAGS:
44530caa102dSLi Zefan 		return btrfs_ioctl_subvol_getflags(file, argp);
44540caa102dSLi Zefan 	case BTRFS_IOC_SUBVOL_SETFLAGS:
44550caa102dSLi Zefan 		return btrfs_ioctl_subvol_setflags(file, argp);
44566ef5ed0dSJosef Bacik 	case BTRFS_IOC_DEFAULT_SUBVOL:
44576ef5ed0dSJosef Bacik 		return btrfs_ioctl_default_subvol(file, argp);
4458f46b5a66SChristoph Hellwig 	case BTRFS_IOC_DEFRAG:
44591e701a32SChris Mason 		return btrfs_ioctl_defrag(file, NULL);
44601e701a32SChris Mason 	case BTRFS_IOC_DEFRAG_RANGE:
44611e701a32SChris Mason 		return btrfs_ioctl_defrag(file, argp);
4462f46b5a66SChristoph Hellwig 	case BTRFS_IOC_RESIZE:
4463198605a8SMiao Xie 		return btrfs_ioctl_resize(file, argp);
4464f46b5a66SChristoph Hellwig 	case BTRFS_IOC_ADD_DEV:
44654bcabaa3SChristoph Hellwig 		return btrfs_ioctl_add_dev(root, argp);
4466f46b5a66SChristoph Hellwig 	case BTRFS_IOC_RM_DEV:
4467da24927bSMiao Xie 		return btrfs_ioctl_rm_dev(file, argp);
4468475f6387SJan Schmidt 	case BTRFS_IOC_FS_INFO:
4469475f6387SJan Schmidt 		return btrfs_ioctl_fs_info(root, argp);
4470475f6387SJan Schmidt 	case BTRFS_IOC_DEV_INFO:
4471475f6387SJan Schmidt 		return btrfs_ioctl_dev_info(root, argp);
4472f46b5a66SChristoph Hellwig 	case BTRFS_IOC_BALANCE:
44739ba1f6e4SLiu Bo 		return btrfs_ioctl_balance(file, NULL);
4474f46b5a66SChristoph Hellwig 	case BTRFS_IOC_CLONE:
4475c5c9cd4dSSage Weil 		return btrfs_ioctl_clone(file, arg, 0, 0, 0);
4476c5c9cd4dSSage Weil 	case BTRFS_IOC_CLONE_RANGE:
44777a865e8aSChristoph Hellwig 		return btrfs_ioctl_clone_range(file, argp);
4478f46b5a66SChristoph Hellwig 	case BTRFS_IOC_TRANS_START:
4479f46b5a66SChristoph Hellwig 		return btrfs_ioctl_trans_start(file);
4480f46b5a66SChristoph Hellwig 	case BTRFS_IOC_TRANS_END:
4481f46b5a66SChristoph Hellwig 		return btrfs_ioctl_trans_end(file);
4482ac8e9819SChris Mason 	case BTRFS_IOC_TREE_SEARCH:
4483ac8e9819SChris Mason 		return btrfs_ioctl_tree_search(file, argp);
4484ac8e9819SChris Mason 	case BTRFS_IOC_INO_LOOKUP:
4485ac8e9819SChris Mason 		return btrfs_ioctl_ino_lookup(file, argp);
4486d7728c96SJan Schmidt 	case BTRFS_IOC_INO_PATHS:
4487d7728c96SJan Schmidt 		return btrfs_ioctl_ino_to_path(root, argp);
4488d7728c96SJan Schmidt 	case BTRFS_IOC_LOGICAL_INO:
4489d7728c96SJan Schmidt 		return btrfs_ioctl_logical_to_ino(root, argp);
44901406e432SJosef Bacik 	case BTRFS_IOC_SPACE_INFO:
44911406e432SJosef Bacik 		return btrfs_ioctl_space_info(root, argp);
4492f46b5a66SChristoph Hellwig 	case BTRFS_IOC_SYNC:
4493f46b5a66SChristoph Hellwig 		btrfs_sync_fs(file->f_dentry->d_sb, 1);
4494f46b5a66SChristoph Hellwig 		return 0;
449546204592SSage Weil 	case BTRFS_IOC_START_SYNC:
44969a8c28beSMiao Xie 		return btrfs_ioctl_start_sync(root, argp);
449746204592SSage Weil 	case BTRFS_IOC_WAIT_SYNC:
44989a8c28beSMiao Xie 		return btrfs_ioctl_wait_sync(root, argp);
4499475f6387SJan Schmidt 	case BTRFS_IOC_SCRUB:
4500b8e95489SMiao Xie 		return btrfs_ioctl_scrub(file, argp);
4501475f6387SJan Schmidt 	case BTRFS_IOC_SCRUB_CANCEL:
4502475f6387SJan Schmidt 		return btrfs_ioctl_scrub_cancel(root, argp);
4503475f6387SJan Schmidt 	case BTRFS_IOC_SCRUB_PROGRESS:
4504475f6387SJan Schmidt 		return btrfs_ioctl_scrub_progress(root, argp);
4505c9e9f97bSIlya Dryomov 	case BTRFS_IOC_BALANCE_V2:
45069ba1f6e4SLiu Bo 		return btrfs_ioctl_balance(file, argp);
4507837d5b6eSIlya Dryomov 	case BTRFS_IOC_BALANCE_CTL:
4508837d5b6eSIlya Dryomov 		return btrfs_ioctl_balance_ctl(root, arg);
450919a39dceSIlya Dryomov 	case BTRFS_IOC_BALANCE_PROGRESS:
451019a39dceSIlya Dryomov 		return btrfs_ioctl_balance_progress(root, argp);
45118ea05e3aSAlexander Block 	case BTRFS_IOC_SET_RECEIVED_SUBVOL:
45128ea05e3aSAlexander Block 		return btrfs_ioctl_set_received_subvol(file, argp);
451331db9f7cSAlexander Block 	case BTRFS_IOC_SEND:
451431db9f7cSAlexander Block 		return btrfs_ioctl_send(file, argp);
4515c11d2c23SStefan Behrens 	case BTRFS_IOC_GET_DEV_STATS:
4516b27f7c0cSDavid Sterba 		return btrfs_ioctl_get_dev_stats(root, argp);
45175d13a37bSArne Jansen 	case BTRFS_IOC_QUOTA_CTL:
4518905b0ddaSMiao Xie 		return btrfs_ioctl_quota_ctl(file, argp);
45195d13a37bSArne Jansen 	case BTRFS_IOC_QGROUP_ASSIGN:
4520905b0ddaSMiao Xie 		return btrfs_ioctl_qgroup_assign(file, argp);
45215d13a37bSArne Jansen 	case BTRFS_IOC_QGROUP_CREATE:
4522905b0ddaSMiao Xie 		return btrfs_ioctl_qgroup_create(file, argp);
45235d13a37bSArne Jansen 	case BTRFS_IOC_QGROUP_LIMIT:
4524905b0ddaSMiao Xie 		return btrfs_ioctl_qgroup_limit(file, argp);
45252f232036SJan Schmidt 	case BTRFS_IOC_QUOTA_RESCAN:
45262f232036SJan Schmidt 		return btrfs_ioctl_quota_rescan(file, argp);
45272f232036SJan Schmidt 	case BTRFS_IOC_QUOTA_RESCAN_STATUS:
45282f232036SJan Schmidt 		return btrfs_ioctl_quota_rescan_status(file, argp);
452957254b6eSJan Schmidt 	case BTRFS_IOC_QUOTA_RESCAN_WAIT:
453057254b6eSJan Schmidt 		return btrfs_ioctl_quota_rescan_wait(file, argp);
45313f6bcfbdSStefan Behrens 	case BTRFS_IOC_DEV_REPLACE:
45323f6bcfbdSStefan Behrens 		return btrfs_ioctl_dev_replace(root, argp);
4533867ab667Sjeff.liu 	case BTRFS_IOC_GET_FSLABEL:
4534867ab667Sjeff.liu 		return btrfs_ioctl_get_fslabel(file, argp);
4535a8bfd4abSjeff.liu 	case BTRFS_IOC_SET_FSLABEL:
4536a8bfd4abSjeff.liu 		return btrfs_ioctl_set_fslabel(file, argp);
4537416161dbSMark Fasheh 	case BTRFS_IOC_FILE_EXTENT_SAME:
4538416161dbSMark Fasheh 		return btrfs_ioctl_file_extent_same(file, argp);
4539f46b5a66SChristoph Hellwig 	}
4540f46b5a66SChristoph Hellwig 
4541f46b5a66SChristoph Hellwig 	return -ENOTTY;
4542f46b5a66SChristoph Hellwig }
4543