xref: /openbmc/linux/fs/btrfs/ioctl.c (revision 8051aa1a)
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>
47f46b5a66SChristoph Hellwig #include "ctree.h"
48f46b5a66SChristoph Hellwig #include "disk-io.h"
49f46b5a66SChristoph Hellwig #include "transaction.h"
50f46b5a66SChristoph Hellwig #include "btrfs_inode.h"
51f46b5a66SChristoph Hellwig #include "print-tree.h"
52f46b5a66SChristoph Hellwig #include "volumes.h"
53925baeddSChris Mason #include "locking.h"
54581bb050SLi Zefan #include "inode-map.h"
55d7728c96SJan Schmidt #include "backref.h"
56606686eeSJosef Bacik #include "rcu-string.h"
5731db9f7cSAlexander Block #include "send.h"
583f6bcfbdSStefan Behrens #include "dev-replace.h"
5963541927SFilipe David Borba Manana #include "props.h"
603b02a68aSJeff Mahoney #include "sysfs.h"
61f46b5a66SChristoph Hellwig 
62416161dbSMark Fasheh static int btrfs_clone(struct inode *src, struct inode *inode,
63416161dbSMark Fasheh 		       u64 off, u64 olen, u64 olen_aligned, u64 destoff);
64416161dbSMark Fasheh 
656cbff00fSChristoph Hellwig /* Mask out flags that are inappropriate for the given type of inode. */
666cbff00fSChristoph Hellwig static inline __u32 btrfs_mask_flags(umode_t mode, __u32 flags)
676cbff00fSChristoph Hellwig {
686cbff00fSChristoph Hellwig 	if (S_ISDIR(mode))
696cbff00fSChristoph Hellwig 		return flags;
706cbff00fSChristoph Hellwig 	else if (S_ISREG(mode))
716cbff00fSChristoph Hellwig 		return flags & ~FS_DIRSYNC_FL;
726cbff00fSChristoph Hellwig 	else
736cbff00fSChristoph Hellwig 		return flags & (FS_NODUMP_FL | FS_NOATIME_FL);
746cbff00fSChristoph Hellwig }
75f46b5a66SChristoph Hellwig 
766cbff00fSChristoph Hellwig /*
776cbff00fSChristoph Hellwig  * Export inode flags to the format expected by the FS_IOC_GETFLAGS ioctl.
786cbff00fSChristoph Hellwig  */
796cbff00fSChristoph Hellwig static unsigned int btrfs_flags_to_ioctl(unsigned int flags)
806cbff00fSChristoph Hellwig {
816cbff00fSChristoph Hellwig 	unsigned int iflags = 0;
826cbff00fSChristoph Hellwig 
836cbff00fSChristoph Hellwig 	if (flags & BTRFS_INODE_SYNC)
846cbff00fSChristoph Hellwig 		iflags |= FS_SYNC_FL;
856cbff00fSChristoph Hellwig 	if (flags & BTRFS_INODE_IMMUTABLE)
866cbff00fSChristoph Hellwig 		iflags |= FS_IMMUTABLE_FL;
876cbff00fSChristoph Hellwig 	if (flags & BTRFS_INODE_APPEND)
886cbff00fSChristoph Hellwig 		iflags |= FS_APPEND_FL;
896cbff00fSChristoph Hellwig 	if (flags & BTRFS_INODE_NODUMP)
906cbff00fSChristoph Hellwig 		iflags |= FS_NODUMP_FL;
916cbff00fSChristoph Hellwig 	if (flags & BTRFS_INODE_NOATIME)
926cbff00fSChristoph Hellwig 		iflags |= FS_NOATIME_FL;
936cbff00fSChristoph Hellwig 	if (flags & BTRFS_INODE_DIRSYNC)
946cbff00fSChristoph Hellwig 		iflags |= FS_DIRSYNC_FL;
95d0092bddSLi Zefan 	if (flags & BTRFS_INODE_NODATACOW)
96d0092bddSLi Zefan 		iflags |= FS_NOCOW_FL;
97d0092bddSLi Zefan 
98d0092bddSLi Zefan 	if ((flags & BTRFS_INODE_COMPRESS) && !(flags & BTRFS_INODE_NOCOMPRESS))
99d0092bddSLi Zefan 		iflags |= FS_COMPR_FL;
100d0092bddSLi Zefan 	else if (flags & BTRFS_INODE_NOCOMPRESS)
101d0092bddSLi Zefan 		iflags |= FS_NOCOMP_FL;
1026cbff00fSChristoph Hellwig 
1036cbff00fSChristoph Hellwig 	return iflags;
1046cbff00fSChristoph Hellwig }
1056cbff00fSChristoph Hellwig 
1066cbff00fSChristoph Hellwig /*
1076cbff00fSChristoph Hellwig  * Update inode->i_flags based on the btrfs internal flags.
1086cbff00fSChristoph Hellwig  */
1096cbff00fSChristoph Hellwig void btrfs_update_iflags(struct inode *inode)
1106cbff00fSChristoph Hellwig {
1116cbff00fSChristoph Hellwig 	struct btrfs_inode *ip = BTRFS_I(inode);
1126cbff00fSChristoph Hellwig 
1136cbff00fSChristoph Hellwig 	inode->i_flags &= ~(S_SYNC|S_APPEND|S_IMMUTABLE|S_NOATIME|S_DIRSYNC);
1146cbff00fSChristoph Hellwig 
1156cbff00fSChristoph Hellwig 	if (ip->flags & BTRFS_INODE_SYNC)
1166cbff00fSChristoph Hellwig 		inode->i_flags |= S_SYNC;
1176cbff00fSChristoph Hellwig 	if (ip->flags & BTRFS_INODE_IMMUTABLE)
1186cbff00fSChristoph Hellwig 		inode->i_flags |= S_IMMUTABLE;
1196cbff00fSChristoph Hellwig 	if (ip->flags & BTRFS_INODE_APPEND)
1206cbff00fSChristoph Hellwig 		inode->i_flags |= S_APPEND;
1216cbff00fSChristoph Hellwig 	if (ip->flags & BTRFS_INODE_NOATIME)
1226cbff00fSChristoph Hellwig 		inode->i_flags |= S_NOATIME;
1236cbff00fSChristoph Hellwig 	if (ip->flags & BTRFS_INODE_DIRSYNC)
1246cbff00fSChristoph Hellwig 		inode->i_flags |= S_DIRSYNC;
1256cbff00fSChristoph Hellwig }
1266cbff00fSChristoph Hellwig 
1276cbff00fSChristoph Hellwig /*
1286cbff00fSChristoph Hellwig  * Inherit flags from the parent inode.
1296cbff00fSChristoph Hellwig  *
130e27425d6SJosef Bacik  * Currently only the compression flags and the cow flags are inherited.
1316cbff00fSChristoph Hellwig  */
1326cbff00fSChristoph Hellwig void btrfs_inherit_iflags(struct inode *inode, struct inode *dir)
1336cbff00fSChristoph Hellwig {
1340b4dcea5SChris Mason 	unsigned int flags;
1350b4dcea5SChris Mason 
1360b4dcea5SChris Mason 	if (!dir)
1370b4dcea5SChris Mason 		return;
1380b4dcea5SChris Mason 
1390b4dcea5SChris Mason 	flags = BTRFS_I(dir)->flags;
1406cbff00fSChristoph Hellwig 
141e27425d6SJosef Bacik 	if (flags & BTRFS_INODE_NOCOMPRESS) {
142e27425d6SJosef Bacik 		BTRFS_I(inode)->flags &= ~BTRFS_INODE_COMPRESS;
143e27425d6SJosef Bacik 		BTRFS_I(inode)->flags |= BTRFS_INODE_NOCOMPRESS;
144e27425d6SJosef Bacik 	} else if (flags & BTRFS_INODE_COMPRESS) {
145e27425d6SJosef Bacik 		BTRFS_I(inode)->flags &= ~BTRFS_INODE_NOCOMPRESS;
146e27425d6SJosef Bacik 		BTRFS_I(inode)->flags |= BTRFS_INODE_COMPRESS;
147e27425d6SJosef Bacik 	}
1486cbff00fSChristoph Hellwig 
149213490b3SLiu Bo 	if (flags & BTRFS_INODE_NODATACOW) {
150e27425d6SJosef Bacik 		BTRFS_I(inode)->flags |= BTRFS_INODE_NODATACOW;
151213490b3SLiu Bo 		if (S_ISREG(inode->i_mode))
152213490b3SLiu Bo 			BTRFS_I(inode)->flags |= BTRFS_INODE_NODATASUM;
153213490b3SLiu Bo 	}
154e27425d6SJosef Bacik 
1556cbff00fSChristoph Hellwig 	btrfs_update_iflags(inode);
1566cbff00fSChristoph Hellwig }
1576cbff00fSChristoph Hellwig 
1586cbff00fSChristoph Hellwig static int btrfs_ioctl_getflags(struct file *file, void __user *arg)
1596cbff00fSChristoph Hellwig {
160496ad9aaSAl Viro 	struct btrfs_inode *ip = BTRFS_I(file_inode(file));
1616cbff00fSChristoph Hellwig 	unsigned int flags = btrfs_flags_to_ioctl(ip->flags);
1626cbff00fSChristoph Hellwig 
1636cbff00fSChristoph Hellwig 	if (copy_to_user(arg, &flags, sizeof(flags)))
1646cbff00fSChristoph Hellwig 		return -EFAULT;
1656cbff00fSChristoph Hellwig 	return 0;
1666cbff00fSChristoph Hellwig }
1676cbff00fSChristoph Hellwig 
16875e7cb7fSLiu Bo static int check_flags(unsigned int flags)
16975e7cb7fSLiu Bo {
17075e7cb7fSLiu Bo 	if (flags & ~(FS_IMMUTABLE_FL | FS_APPEND_FL | \
17175e7cb7fSLiu Bo 		      FS_NOATIME_FL | FS_NODUMP_FL | \
17275e7cb7fSLiu Bo 		      FS_SYNC_FL | FS_DIRSYNC_FL | \
173e1e8fb6aSLi Zefan 		      FS_NOCOMP_FL | FS_COMPR_FL |
174e1e8fb6aSLi Zefan 		      FS_NOCOW_FL))
17575e7cb7fSLiu Bo 		return -EOPNOTSUPP;
17675e7cb7fSLiu Bo 
17775e7cb7fSLiu Bo 	if ((flags & FS_NOCOMP_FL) && (flags & FS_COMPR_FL))
17875e7cb7fSLiu Bo 		return -EINVAL;
17975e7cb7fSLiu Bo 
18075e7cb7fSLiu Bo 	return 0;
18175e7cb7fSLiu Bo }
18275e7cb7fSLiu Bo 
1836cbff00fSChristoph Hellwig static int btrfs_ioctl_setflags(struct file *file, void __user *arg)
1846cbff00fSChristoph Hellwig {
185496ad9aaSAl Viro 	struct inode *inode = file_inode(file);
1866cbff00fSChristoph Hellwig 	struct btrfs_inode *ip = BTRFS_I(inode);
1876cbff00fSChristoph Hellwig 	struct btrfs_root *root = ip->root;
1886cbff00fSChristoph Hellwig 	struct btrfs_trans_handle *trans;
1896cbff00fSChristoph Hellwig 	unsigned int flags, oldflags;
1906cbff00fSChristoph Hellwig 	int ret;
191f062abf0SLi Zefan 	u64 ip_oldflags;
192f062abf0SLi Zefan 	unsigned int i_oldflags;
1937e97b8daSDavid Sterba 	umode_t mode;
1946cbff00fSChristoph Hellwig 
195bd60ea0fSDavid Sterba 	if (!inode_owner_or_capable(inode))
196bd60ea0fSDavid Sterba 		return -EPERM;
197bd60ea0fSDavid Sterba 
198b83cc969SLi Zefan 	if (btrfs_root_readonly(root))
199b83cc969SLi Zefan 		return -EROFS;
200b83cc969SLi Zefan 
2016cbff00fSChristoph Hellwig 	if (copy_from_user(&flags, arg, sizeof(flags)))
2026cbff00fSChristoph Hellwig 		return -EFAULT;
2036cbff00fSChristoph Hellwig 
20475e7cb7fSLiu Bo 	ret = check_flags(flags);
20575e7cb7fSLiu Bo 	if (ret)
20675e7cb7fSLiu Bo 		return ret;
2076cbff00fSChristoph Hellwig 
208e7848683SJan Kara 	ret = mnt_want_write_file(file);
209e7848683SJan Kara 	if (ret)
210e7848683SJan Kara 		return ret;
211e7848683SJan Kara 
2126cbff00fSChristoph Hellwig 	mutex_lock(&inode->i_mutex);
2136cbff00fSChristoph Hellwig 
214f062abf0SLi Zefan 	ip_oldflags = ip->flags;
215f062abf0SLi Zefan 	i_oldflags = inode->i_flags;
2167e97b8daSDavid Sterba 	mode = inode->i_mode;
217f062abf0SLi Zefan 
2186cbff00fSChristoph Hellwig 	flags = btrfs_mask_flags(inode->i_mode, flags);
2196cbff00fSChristoph Hellwig 	oldflags = btrfs_flags_to_ioctl(ip->flags);
2206cbff00fSChristoph Hellwig 	if ((flags ^ oldflags) & (FS_APPEND_FL | FS_IMMUTABLE_FL)) {
2216cbff00fSChristoph Hellwig 		if (!capable(CAP_LINUX_IMMUTABLE)) {
2226cbff00fSChristoph Hellwig 			ret = -EPERM;
2236cbff00fSChristoph Hellwig 			goto out_unlock;
2246cbff00fSChristoph Hellwig 		}
2256cbff00fSChristoph Hellwig 	}
2266cbff00fSChristoph Hellwig 
2276cbff00fSChristoph Hellwig 	if (flags & FS_SYNC_FL)
2286cbff00fSChristoph Hellwig 		ip->flags |= BTRFS_INODE_SYNC;
2296cbff00fSChristoph Hellwig 	else
2306cbff00fSChristoph Hellwig 		ip->flags &= ~BTRFS_INODE_SYNC;
2316cbff00fSChristoph Hellwig 	if (flags & FS_IMMUTABLE_FL)
2326cbff00fSChristoph Hellwig 		ip->flags |= BTRFS_INODE_IMMUTABLE;
2336cbff00fSChristoph Hellwig 	else
2346cbff00fSChristoph Hellwig 		ip->flags &= ~BTRFS_INODE_IMMUTABLE;
2356cbff00fSChristoph Hellwig 	if (flags & FS_APPEND_FL)
2366cbff00fSChristoph Hellwig 		ip->flags |= BTRFS_INODE_APPEND;
2376cbff00fSChristoph Hellwig 	else
2386cbff00fSChristoph Hellwig 		ip->flags &= ~BTRFS_INODE_APPEND;
2396cbff00fSChristoph Hellwig 	if (flags & FS_NODUMP_FL)
2406cbff00fSChristoph Hellwig 		ip->flags |= BTRFS_INODE_NODUMP;
2416cbff00fSChristoph Hellwig 	else
2426cbff00fSChristoph Hellwig 		ip->flags &= ~BTRFS_INODE_NODUMP;
2436cbff00fSChristoph Hellwig 	if (flags & FS_NOATIME_FL)
2446cbff00fSChristoph Hellwig 		ip->flags |= BTRFS_INODE_NOATIME;
2456cbff00fSChristoph Hellwig 	else
2466cbff00fSChristoph Hellwig 		ip->flags &= ~BTRFS_INODE_NOATIME;
2476cbff00fSChristoph Hellwig 	if (flags & FS_DIRSYNC_FL)
2486cbff00fSChristoph Hellwig 		ip->flags |= BTRFS_INODE_DIRSYNC;
2496cbff00fSChristoph Hellwig 	else
2506cbff00fSChristoph Hellwig 		ip->flags &= ~BTRFS_INODE_DIRSYNC;
2517e97b8daSDavid Sterba 	if (flags & FS_NOCOW_FL) {
2527e97b8daSDavid Sterba 		if (S_ISREG(mode)) {
2537e97b8daSDavid Sterba 			/*
2547e97b8daSDavid Sterba 			 * It's safe to turn csums off here, no extents exist.
2557e97b8daSDavid Sterba 			 * Otherwise we want the flag to reflect the real COW
2567e97b8daSDavid Sterba 			 * status of the file and will not set it.
2577e97b8daSDavid Sterba 			 */
2587e97b8daSDavid Sterba 			if (inode->i_size == 0)
2597e97b8daSDavid Sterba 				ip->flags |= BTRFS_INODE_NODATACOW
2607e97b8daSDavid Sterba 					   | BTRFS_INODE_NODATASUM;
2617e97b8daSDavid Sterba 		} else {
262e1e8fb6aSLi Zefan 			ip->flags |= BTRFS_INODE_NODATACOW;
2637e97b8daSDavid Sterba 		}
2647e97b8daSDavid Sterba 	} else {
2657e97b8daSDavid Sterba 		/*
2667e97b8daSDavid Sterba 		 * Revert back under same assuptions as above
2677e97b8daSDavid Sterba 		 */
2687e97b8daSDavid Sterba 		if (S_ISREG(mode)) {
2697e97b8daSDavid Sterba 			if (inode->i_size == 0)
2707e97b8daSDavid Sterba 				ip->flags &= ~(BTRFS_INODE_NODATACOW
2717e97b8daSDavid Sterba 				             | BTRFS_INODE_NODATASUM);
2727e97b8daSDavid Sterba 		} else {
273e1e8fb6aSLi Zefan 			ip->flags &= ~BTRFS_INODE_NODATACOW;
2747e97b8daSDavid Sterba 		}
2757e97b8daSDavid Sterba 	}
2766cbff00fSChristoph Hellwig 
27775e7cb7fSLiu Bo 	/*
27875e7cb7fSLiu Bo 	 * The COMPRESS flag can only be changed by users, while the NOCOMPRESS
27975e7cb7fSLiu Bo 	 * flag may be changed automatically if compression code won't make
28075e7cb7fSLiu Bo 	 * things smaller.
28175e7cb7fSLiu Bo 	 */
28275e7cb7fSLiu Bo 	if (flags & FS_NOCOMP_FL) {
28375e7cb7fSLiu Bo 		ip->flags &= ~BTRFS_INODE_COMPRESS;
28475e7cb7fSLiu Bo 		ip->flags |= BTRFS_INODE_NOCOMPRESS;
28563541927SFilipe David Borba Manana 
28663541927SFilipe David Borba Manana 		ret = btrfs_set_prop(inode, "btrfs.compression", NULL, 0, 0);
28763541927SFilipe David Borba Manana 		if (ret && ret != -ENODATA)
28863541927SFilipe David Borba Manana 			goto out_drop;
28975e7cb7fSLiu Bo 	} else if (flags & FS_COMPR_FL) {
29063541927SFilipe David Borba Manana 		const char *comp;
29163541927SFilipe David Borba Manana 
29275e7cb7fSLiu Bo 		ip->flags |= BTRFS_INODE_COMPRESS;
29375e7cb7fSLiu Bo 		ip->flags &= ~BTRFS_INODE_NOCOMPRESS;
29463541927SFilipe David Borba Manana 
29563541927SFilipe David Borba Manana 		if (root->fs_info->compress_type == BTRFS_COMPRESS_LZO)
29663541927SFilipe David Borba Manana 			comp = "lzo";
29763541927SFilipe David Borba Manana 		else
29863541927SFilipe David Borba Manana 			comp = "zlib";
29963541927SFilipe David Borba Manana 		ret = btrfs_set_prop(inode, "btrfs.compression",
30063541927SFilipe David Borba Manana 				     comp, strlen(comp), 0);
30163541927SFilipe David Borba Manana 		if (ret)
30263541927SFilipe David Borba Manana 			goto out_drop;
30363541927SFilipe David Borba Manana 
304ebcb904dSLi Zefan 	} else {
305ebcb904dSLi Zefan 		ip->flags &= ~(BTRFS_INODE_COMPRESS | BTRFS_INODE_NOCOMPRESS);
30675e7cb7fSLiu Bo 	}
3076cbff00fSChristoph Hellwig 
3084da6f1a3SLi Zefan 	trans = btrfs_start_transaction(root, 1);
309f062abf0SLi Zefan 	if (IS_ERR(trans)) {
310f062abf0SLi Zefan 		ret = PTR_ERR(trans);
311f062abf0SLi Zefan 		goto out_drop;
312f062abf0SLi Zefan 	}
3136cbff00fSChristoph Hellwig 
314306424ccSLi Zefan 	btrfs_update_iflags(inode);
3150c4d2d95SJosef Bacik 	inode_inc_iversion(inode);
316306424ccSLi Zefan 	inode->i_ctime = CURRENT_TIME;
3176cbff00fSChristoph Hellwig 	ret = btrfs_update_inode(trans, root, inode);
3186cbff00fSChristoph Hellwig 
3196cbff00fSChristoph Hellwig 	btrfs_end_transaction(trans, root);
320f062abf0SLi Zefan  out_drop:
321f062abf0SLi Zefan 	if (ret) {
322f062abf0SLi Zefan 		ip->flags = ip_oldflags;
323f062abf0SLi Zefan 		inode->i_flags = i_oldflags;
324f062abf0SLi Zefan 	}
3256cbff00fSChristoph Hellwig 
3266cbff00fSChristoph Hellwig  out_unlock:
3276cbff00fSChristoph Hellwig 	mutex_unlock(&inode->i_mutex);
328e7848683SJan Kara 	mnt_drop_write_file(file);
3292d4e6f6aSliubo 	return ret;
3306cbff00fSChristoph Hellwig }
3316cbff00fSChristoph Hellwig 
3326cbff00fSChristoph Hellwig static int btrfs_ioctl_getversion(struct file *file, int __user *arg)
3336cbff00fSChristoph Hellwig {
334496ad9aaSAl Viro 	struct inode *inode = file_inode(file);
3356cbff00fSChristoph Hellwig 
3366cbff00fSChristoph Hellwig 	return put_user(inode->i_generation, arg);
3376cbff00fSChristoph Hellwig }
338f46b5a66SChristoph Hellwig 
339f7039b1dSLi Dongyang static noinline int btrfs_ioctl_fitrim(struct file *file, void __user *arg)
340f7039b1dSLi Dongyang {
34154563d41SAl Viro 	struct btrfs_fs_info *fs_info = btrfs_sb(file_inode(file)->i_sb);
342f7039b1dSLi Dongyang 	struct btrfs_device *device;
343f7039b1dSLi Dongyang 	struct request_queue *q;
344f7039b1dSLi Dongyang 	struct fstrim_range range;
345f7039b1dSLi Dongyang 	u64 minlen = ULLONG_MAX;
346f7039b1dSLi Dongyang 	u64 num_devices = 0;
347815745cfSAl Viro 	u64 total_bytes = btrfs_super_total_bytes(fs_info->super_copy);
348f7039b1dSLi Dongyang 	int ret;
349f7039b1dSLi Dongyang 
350f7039b1dSLi Dongyang 	if (!capable(CAP_SYS_ADMIN))
351f7039b1dSLi Dongyang 		return -EPERM;
352f7039b1dSLi Dongyang 
3531f78160cSXiao Guangrong 	rcu_read_lock();
3541f78160cSXiao Guangrong 	list_for_each_entry_rcu(device, &fs_info->fs_devices->devices,
3551f78160cSXiao Guangrong 				dev_list) {
356f7039b1dSLi Dongyang 		if (!device->bdev)
357f7039b1dSLi Dongyang 			continue;
358f7039b1dSLi Dongyang 		q = bdev_get_queue(device->bdev);
359f7039b1dSLi Dongyang 		if (blk_queue_discard(q)) {
360f7039b1dSLi Dongyang 			num_devices++;
361f7039b1dSLi Dongyang 			minlen = min((u64)q->limits.discard_granularity,
362f7039b1dSLi Dongyang 				     minlen);
363f7039b1dSLi Dongyang 		}
364f7039b1dSLi Dongyang 	}
3651f78160cSXiao Guangrong 	rcu_read_unlock();
366f4c697e6SLukas Czerner 
367f7039b1dSLi Dongyang 	if (!num_devices)
368f7039b1dSLi Dongyang 		return -EOPNOTSUPP;
369f7039b1dSLi Dongyang 	if (copy_from_user(&range, arg, sizeof(range)))
370f7039b1dSLi Dongyang 		return -EFAULT;
371e515c18bSLukas Czerner 	if (range.start > total_bytes ||
372e515c18bSLukas Czerner 	    range.len < fs_info->sb->s_blocksize)
373f4c697e6SLukas Czerner 		return -EINVAL;
374f7039b1dSLi Dongyang 
375f4c697e6SLukas Czerner 	range.len = min(range.len, total_bytes - range.start);
376f7039b1dSLi Dongyang 	range.minlen = max(range.minlen, minlen);
377815745cfSAl Viro 	ret = btrfs_trim_fs(fs_info->tree_root, &range);
378f7039b1dSLi Dongyang 	if (ret < 0)
379f7039b1dSLi Dongyang 		return ret;
380f7039b1dSLi Dongyang 
381f7039b1dSLi Dongyang 	if (copy_to_user(arg, &range, sizeof(range)))
382f7039b1dSLi Dongyang 		return -EFAULT;
383f7039b1dSLi Dongyang 
384f7039b1dSLi Dongyang 	return 0;
385f7039b1dSLi Dongyang }
386f7039b1dSLi Dongyang 
387dd5f9615SStefan Behrens int btrfs_is_empty_uuid(u8 *uuid)
388dd5f9615SStefan Behrens {
38946e0f66aSChris Mason 	int i;
39046e0f66aSChris Mason 
39146e0f66aSChris Mason 	for (i = 0; i < BTRFS_UUID_SIZE; i++) {
39246e0f66aSChris Mason 		if (uuid[i])
39346e0f66aSChris Mason 			return 0;
39446e0f66aSChris Mason 	}
39546e0f66aSChris Mason 	return 1;
396dd5f9615SStefan Behrens }
397dd5f9615SStefan Behrens 
398d5c12070SMiao Xie static noinline int create_subvol(struct inode *dir,
399cb8e7090SChristoph Hellwig 				  struct dentry *dentry,
40072fd032eSSage Weil 				  char *name, int namelen,
4016f72c7e2SArne Jansen 				  u64 *async_transid,
4028696c533SMiao Xie 				  struct btrfs_qgroup_inherit *inherit)
403f46b5a66SChristoph Hellwig {
404f46b5a66SChristoph Hellwig 	struct btrfs_trans_handle *trans;
405f46b5a66SChristoph Hellwig 	struct btrfs_key key;
406f46b5a66SChristoph Hellwig 	struct btrfs_root_item root_item;
407f46b5a66SChristoph Hellwig 	struct btrfs_inode_item *inode_item;
408f46b5a66SChristoph Hellwig 	struct extent_buffer *leaf;
409d5c12070SMiao Xie 	struct btrfs_root *root = BTRFS_I(dir)->root;
41076dda93cSYan, Zheng 	struct btrfs_root *new_root;
411d5c12070SMiao Xie 	struct btrfs_block_rsv block_rsv;
4128ea05e3aSAlexander Block 	struct timespec cur_time = CURRENT_TIME;
4135662344bSTsutomu Itoh 	struct inode *inode;
414f46b5a66SChristoph Hellwig 	int ret;
415f46b5a66SChristoph Hellwig 	int err;
416f46b5a66SChristoph Hellwig 	u64 objectid;
417f46b5a66SChristoph Hellwig 	u64 new_dirid = BTRFS_FIRST_FREE_OBJECTID;
4183de4586cSChris Mason 	u64 index = 0;
419d5c12070SMiao Xie 	u64 qgroup_reserved;
4208ea05e3aSAlexander Block 	uuid_le new_uuid;
421f46b5a66SChristoph Hellwig 
422581bb050SLi Zefan 	ret = btrfs_find_free_objectid(root->fs_info->tree_root, &objectid);
4232fbe8c8aSAl Viro 	if (ret)
424a22285a6SYan, Zheng 		return ret;
4256a912213SJosef Bacik 
426d5c12070SMiao Xie 	btrfs_init_block_rsv(&block_rsv, BTRFS_BLOCK_RSV_TEMP);
4279ed74f2dSJosef Bacik 	/*
428d5c12070SMiao Xie 	 * The same as the snapshot creation, please see the comment
429d5c12070SMiao Xie 	 * of create_snapshot().
4309ed74f2dSJosef Bacik 	 */
431d5c12070SMiao Xie 	ret = btrfs_subvolume_reserve_metadata(root, &block_rsv,
432dd5f9615SStefan Behrens 					       8, &qgroup_reserved, false);
433d5c12070SMiao Xie 	if (ret)
434d5c12070SMiao Xie 		return ret;
435f46b5a66SChristoph Hellwig 
436d5c12070SMiao Xie 	trans = btrfs_start_transaction(root, 0);
437d5c12070SMiao Xie 	if (IS_ERR(trans)) {
438d5c12070SMiao Xie 		ret = PTR_ERR(trans);
439de6e8200SLiu Bo 		btrfs_subvolume_release_metadata(root, &block_rsv,
440de6e8200SLiu Bo 						 qgroup_reserved);
441de6e8200SLiu Bo 		return ret;
442d5c12070SMiao Xie 	}
443d5c12070SMiao Xie 	trans->block_rsv = &block_rsv;
444d5c12070SMiao Xie 	trans->bytes_reserved = block_rsv.size;
445f46b5a66SChristoph Hellwig 
4468696c533SMiao Xie 	ret = btrfs_qgroup_inherit(trans, root->fs_info, 0, objectid, inherit);
4476f72c7e2SArne Jansen 	if (ret)
4486f72c7e2SArne Jansen 		goto fail;
4496f72c7e2SArne Jansen 
4505d4f98a2SYan Zheng 	leaf = btrfs_alloc_free_block(trans, root, root->leafsize,
4515581a51aSJan Schmidt 				      0, objectid, NULL, 0, 0, 0);
4528e8a1e31SJosef Bacik 	if (IS_ERR(leaf)) {
4538e8a1e31SJosef Bacik 		ret = PTR_ERR(leaf);
4548e8a1e31SJosef Bacik 		goto fail;
4558e8a1e31SJosef Bacik 	}
456f46b5a66SChristoph Hellwig 
4575d4f98a2SYan Zheng 	memset_extent_buffer(leaf, 0, 0, sizeof(struct btrfs_header));
458f46b5a66SChristoph Hellwig 	btrfs_set_header_bytenr(leaf, leaf->start);
459f46b5a66SChristoph Hellwig 	btrfs_set_header_generation(leaf, trans->transid);
4605d4f98a2SYan Zheng 	btrfs_set_header_backref_rev(leaf, BTRFS_MIXED_BACKREF_REV);
461f46b5a66SChristoph Hellwig 	btrfs_set_header_owner(leaf, objectid);
462f46b5a66SChristoph Hellwig 
4630a4e5586SRoss Kirk 	write_extent_buffer(leaf, root->fs_info->fsid, btrfs_header_fsid(),
464f46b5a66SChristoph Hellwig 			    BTRFS_FSID_SIZE);
4655d4f98a2SYan Zheng 	write_extent_buffer(leaf, root->fs_info->chunk_tree_uuid,
466b308bc2fSGeert Uytterhoeven 			    btrfs_header_chunk_tree_uuid(leaf),
4675d4f98a2SYan Zheng 			    BTRFS_UUID_SIZE);
468f46b5a66SChristoph Hellwig 	btrfs_mark_buffer_dirty(leaf);
469f46b5a66SChristoph Hellwig 
4708ea05e3aSAlexander Block 	memset(&root_item, 0, sizeof(root_item));
4718ea05e3aSAlexander Block 
472f46b5a66SChristoph Hellwig 	inode_item = &root_item.inode;
4733cae210fSQu Wenruo 	btrfs_set_stack_inode_generation(inode_item, 1);
4743cae210fSQu Wenruo 	btrfs_set_stack_inode_size(inode_item, 3);
4753cae210fSQu Wenruo 	btrfs_set_stack_inode_nlink(inode_item, 1);
4763cae210fSQu Wenruo 	btrfs_set_stack_inode_nbytes(inode_item, root->leafsize);
4773cae210fSQu Wenruo 	btrfs_set_stack_inode_mode(inode_item, S_IFDIR | 0755);
478f46b5a66SChristoph Hellwig 
4793cae210fSQu Wenruo 	btrfs_set_root_flags(&root_item, 0);
4803cae210fSQu Wenruo 	btrfs_set_root_limit(&root_item, 0);
4813cae210fSQu Wenruo 	btrfs_set_stack_inode_flags(inode_item, BTRFS_INODE_ROOT_ITEM_INIT);
48208fe4db1SLi Zefan 
483f46b5a66SChristoph Hellwig 	btrfs_set_root_bytenr(&root_item, leaf->start);
48484234f3aSYan Zheng 	btrfs_set_root_generation(&root_item, trans->transid);
485f46b5a66SChristoph Hellwig 	btrfs_set_root_level(&root_item, 0);
486f46b5a66SChristoph Hellwig 	btrfs_set_root_refs(&root_item, 1);
48786b9f2ecSYan, Zheng 	btrfs_set_root_used(&root_item, leaf->len);
48880ff3856SYan Zheng 	btrfs_set_root_last_snapshot(&root_item, 0);
489f46b5a66SChristoph Hellwig 
4908ea05e3aSAlexander Block 	btrfs_set_root_generation_v2(&root_item,
4918ea05e3aSAlexander Block 			btrfs_root_generation(&root_item));
4928ea05e3aSAlexander Block 	uuid_le_gen(&new_uuid);
4938ea05e3aSAlexander Block 	memcpy(root_item.uuid, new_uuid.b, BTRFS_UUID_SIZE);
4943cae210fSQu Wenruo 	btrfs_set_stack_timespec_sec(&root_item.otime, cur_time.tv_sec);
4953cae210fSQu Wenruo 	btrfs_set_stack_timespec_nsec(&root_item.otime, cur_time.tv_nsec);
4968ea05e3aSAlexander Block 	root_item.ctime = root_item.otime;
4978ea05e3aSAlexander Block 	btrfs_set_root_ctransid(&root_item, trans->transid);
4988ea05e3aSAlexander Block 	btrfs_set_root_otransid(&root_item, trans->transid);
499f46b5a66SChristoph Hellwig 
500925baeddSChris Mason 	btrfs_tree_unlock(leaf);
501f46b5a66SChristoph Hellwig 	free_extent_buffer(leaf);
502f46b5a66SChristoph Hellwig 	leaf = NULL;
503f46b5a66SChristoph Hellwig 
504f46b5a66SChristoph Hellwig 	btrfs_set_root_dirid(&root_item, new_dirid);
505f46b5a66SChristoph Hellwig 
506f46b5a66SChristoph Hellwig 	key.objectid = objectid;
5075d4f98a2SYan Zheng 	key.offset = 0;
508f46b5a66SChristoph Hellwig 	btrfs_set_key_type(&key, BTRFS_ROOT_ITEM_KEY);
509f46b5a66SChristoph Hellwig 	ret = btrfs_insert_root(trans, root->fs_info->tree_root, &key,
510f46b5a66SChristoph Hellwig 				&root_item);
511f46b5a66SChristoph Hellwig 	if (ret)
512f46b5a66SChristoph Hellwig 		goto fail;
513f46b5a66SChristoph Hellwig 
51476dda93cSYan, Zheng 	key.offset = (u64)-1;
51576dda93cSYan, Zheng 	new_root = btrfs_read_fs_root_no_name(root->fs_info, &key);
51679787eaaSJeff Mahoney 	if (IS_ERR(new_root)) {
51779787eaaSJeff Mahoney 		btrfs_abort_transaction(trans, root, PTR_ERR(new_root));
51879787eaaSJeff Mahoney 		ret = PTR_ERR(new_root);
51979787eaaSJeff Mahoney 		goto fail;
52079787eaaSJeff Mahoney 	}
52176dda93cSYan, Zheng 
52276dda93cSYan, Zheng 	btrfs_record_root_in_trans(trans, new_root);
52376dda93cSYan, Zheng 
52463541927SFilipe David Borba Manana 	ret = btrfs_create_subvol_root(trans, new_root, root, new_dirid);
525ce598979SMark Fasheh 	if (ret) {
526ce598979SMark Fasheh 		/* We potentially lose an unused inode item here */
52779787eaaSJeff Mahoney 		btrfs_abort_transaction(trans, root, ret);
528ce598979SMark Fasheh 		goto fail;
529ce598979SMark Fasheh 	}
530ce598979SMark Fasheh 
531f46b5a66SChristoph Hellwig 	/*
532f46b5a66SChristoph Hellwig 	 * insert the directory item
533f46b5a66SChristoph Hellwig 	 */
5343de4586cSChris Mason 	ret = btrfs_set_inode_index(dir, &index);
53579787eaaSJeff Mahoney 	if (ret) {
53679787eaaSJeff Mahoney 		btrfs_abort_transaction(trans, root, ret);
53779787eaaSJeff Mahoney 		goto fail;
53879787eaaSJeff Mahoney 	}
5393de4586cSChris Mason 
5403de4586cSChris Mason 	ret = btrfs_insert_dir_item(trans, root,
54116cdcec7SMiao Xie 				    name, namelen, dir, &key,
5423de4586cSChris Mason 				    BTRFS_FT_DIR, index);
54379787eaaSJeff Mahoney 	if (ret) {
54479787eaaSJeff Mahoney 		btrfs_abort_transaction(trans, root, ret);
545f46b5a66SChristoph Hellwig 		goto fail;
54679787eaaSJeff Mahoney 	}
5470660b5afSChris Mason 
54852c26179SYan Zheng 	btrfs_i_size_write(dir, dir->i_size + namelen * 2);
54952c26179SYan Zheng 	ret = btrfs_update_inode(trans, root, dir);
55052c26179SYan Zheng 	BUG_ON(ret);
55152c26179SYan Zheng 
5520660b5afSChris Mason 	ret = btrfs_add_root_ref(trans, root->fs_info->tree_root,
5534df27c4dSYan, Zheng 				 objectid, root->root_key.objectid,
55433345d01SLi Zefan 				 btrfs_ino(dir), index, name, namelen);
5550660b5afSChris Mason 	BUG_ON(ret);
5560660b5afSChris Mason 
557dd5f9615SStefan Behrens 	ret = btrfs_uuid_tree_add(trans, root->fs_info->uuid_root,
558dd5f9615SStefan Behrens 				  root_item.uuid, BTRFS_UUID_KEY_SUBVOL,
559dd5f9615SStefan Behrens 				  objectid);
560dd5f9615SStefan Behrens 	if (ret)
561dd5f9615SStefan Behrens 		btrfs_abort_transaction(trans, root, ret);
562dd5f9615SStefan Behrens 
563f46b5a66SChristoph Hellwig fail:
564d5c12070SMiao Xie 	trans->block_rsv = NULL;
565d5c12070SMiao Xie 	trans->bytes_reserved = 0;
566de6e8200SLiu Bo 	btrfs_subvolume_release_metadata(root, &block_rsv, qgroup_reserved);
567de6e8200SLiu Bo 
56872fd032eSSage Weil 	if (async_transid) {
56972fd032eSSage Weil 		*async_transid = trans->transid;
57072fd032eSSage Weil 		err = btrfs_commit_transaction_async(trans, root, 1);
57100d71c9cSMiao Xie 		if (err)
57200d71c9cSMiao Xie 			err = btrfs_commit_transaction(trans, root);
57372fd032eSSage Weil 	} else {
57476dda93cSYan, Zheng 		err = btrfs_commit_transaction(trans, root);
57572fd032eSSage Weil 	}
576f46b5a66SChristoph Hellwig 	if (err && !ret)
577f46b5a66SChristoph Hellwig 		ret = err;
5781a65e24bSChris Mason 
5795662344bSTsutomu Itoh 	if (!ret) {
5805662344bSTsutomu Itoh 		inode = btrfs_lookup_dentry(dir, dentry);
581de6e8200SLiu Bo 		if (IS_ERR(inode))
582de6e8200SLiu Bo 			return PTR_ERR(inode);
5835662344bSTsutomu Itoh 		d_instantiate(dentry, inode);
5845662344bSTsutomu Itoh 	}
585f46b5a66SChristoph Hellwig 	return ret;
586f46b5a66SChristoph Hellwig }
587f46b5a66SChristoph Hellwig 
588e9662f70SMiao Xie static int create_snapshot(struct btrfs_root *root, struct inode *dir,
589e9662f70SMiao Xie 			   struct dentry *dentry, char *name, int namelen,
590e9662f70SMiao Xie 			   u64 *async_transid, bool readonly,
591e9662f70SMiao Xie 			   struct btrfs_qgroup_inherit *inherit)
592f46b5a66SChristoph Hellwig {
5932e4bfab9SYan, Zheng 	struct inode *inode;
594f46b5a66SChristoph Hellwig 	struct btrfs_pending_snapshot *pending_snapshot;
595f46b5a66SChristoph Hellwig 	struct btrfs_trans_handle *trans;
5962e4bfab9SYan, Zheng 	int ret;
597f46b5a66SChristoph Hellwig 
598f46b5a66SChristoph Hellwig 	if (!root->ref_cows)
599f46b5a66SChristoph Hellwig 		return -EINVAL;
600f46b5a66SChristoph Hellwig 
6016a03843dSMiao Xie 	ret = btrfs_start_delalloc_inodes(root, 0);
6026a03843dSMiao Xie 	if (ret)
6036a03843dSMiao Xie 		return ret;
6046a03843dSMiao Xie 
605b0244199SMiao Xie 	btrfs_wait_ordered_extents(root, -1);
6066a03843dSMiao Xie 
607a22285a6SYan, Zheng 	pending_snapshot = kzalloc(sizeof(*pending_snapshot), GFP_NOFS);
608a22285a6SYan, Zheng 	if (!pending_snapshot)
609a22285a6SYan, Zheng 		return -ENOMEM;
610a22285a6SYan, Zheng 
61166d8f3ddSMiao Xie 	btrfs_init_block_rsv(&pending_snapshot->block_rsv,
61266d8f3ddSMiao Xie 			     BTRFS_BLOCK_RSV_TEMP);
613d5c12070SMiao Xie 	/*
614d5c12070SMiao Xie 	 * 1 - parent dir inode
615d5c12070SMiao Xie 	 * 2 - dir entries
616d5c12070SMiao Xie 	 * 1 - root item
617d5c12070SMiao Xie 	 * 2 - root ref/backref
618d5c12070SMiao Xie 	 * 1 - root of snapshot
619dd5f9615SStefan Behrens 	 * 1 - UUID item
620d5c12070SMiao Xie 	 */
621d5c12070SMiao Xie 	ret = btrfs_subvolume_reserve_metadata(BTRFS_I(dir)->root,
622dd5f9615SStefan Behrens 					&pending_snapshot->block_rsv, 8,
623ee3441b4SJeff Mahoney 					&pending_snapshot->qgroup_reserved,
624ee3441b4SJeff Mahoney 					false);
625d5c12070SMiao Xie 	if (ret)
626d5c12070SMiao Xie 		goto out;
627d5c12070SMiao Xie 
628a22285a6SYan, Zheng 	pending_snapshot->dentry = dentry;
629a22285a6SYan, Zheng 	pending_snapshot->root = root;
630b83cc969SLi Zefan 	pending_snapshot->readonly = readonly;
631e9662f70SMiao Xie 	pending_snapshot->dir = dir;
6328696c533SMiao Xie 	pending_snapshot->inherit = inherit;
633a22285a6SYan, Zheng 
634d5c12070SMiao Xie 	trans = btrfs_start_transaction(root, 0);
635a22285a6SYan, Zheng 	if (IS_ERR(trans)) {
636a22285a6SYan, Zheng 		ret = PTR_ERR(trans);
637a22285a6SYan, Zheng 		goto fail;
638a22285a6SYan, Zheng 	}
639a22285a6SYan, Zheng 
6408351583eSJosef Bacik 	spin_lock(&root->fs_info->trans_lock);
641a22285a6SYan, Zheng 	list_add(&pending_snapshot->list,
642a22285a6SYan, Zheng 		 &trans->transaction->pending_snapshots);
6438351583eSJosef Bacik 	spin_unlock(&root->fs_info->trans_lock);
64472fd032eSSage Weil 	if (async_transid) {
64572fd032eSSage Weil 		*async_transid = trans->transid;
64672fd032eSSage Weil 		ret = btrfs_commit_transaction_async(trans,
64772fd032eSSage Weil 				     root->fs_info->extent_root, 1);
64800d71c9cSMiao Xie 		if (ret)
64900d71c9cSMiao Xie 			ret = btrfs_commit_transaction(trans, root);
65072fd032eSSage Weil 	} else {
65172fd032eSSage Weil 		ret = btrfs_commit_transaction(trans,
65272fd032eSSage Weil 					       root->fs_info->extent_root);
65372fd032eSSage Weil 	}
654aec8030aSMiao Xie 	if (ret)
655c37b2b62SJosef Bacik 		goto fail;
656a22285a6SYan, Zheng 
657a22285a6SYan, Zheng 	ret = pending_snapshot->error;
658f46b5a66SChristoph Hellwig 	if (ret)
6592e4bfab9SYan, Zheng 		goto fail;
660f46b5a66SChristoph Hellwig 
66166b4ffd1SJosef Bacik 	ret = btrfs_orphan_cleanup(pending_snapshot->snap);
66266b4ffd1SJosef Bacik 	if (ret)
66366b4ffd1SJosef Bacik 		goto fail;
664f46b5a66SChristoph Hellwig 
6652fbe8c8aSAl Viro 	inode = btrfs_lookup_dentry(dentry->d_parent->d_inode, dentry);
6662e4bfab9SYan, Zheng 	if (IS_ERR(inode)) {
6672e4bfab9SYan, Zheng 		ret = PTR_ERR(inode);
6682e4bfab9SYan, Zheng 		goto fail;
6692e4bfab9SYan, Zheng 	}
6705662344bSTsutomu Itoh 
6712e4bfab9SYan, Zheng 	d_instantiate(dentry, inode);
6722e4bfab9SYan, Zheng 	ret = 0;
6732e4bfab9SYan, Zheng fail:
674d5c12070SMiao Xie 	btrfs_subvolume_release_metadata(BTRFS_I(dir)->root,
675d5c12070SMiao Xie 					 &pending_snapshot->block_rsv,
676d5c12070SMiao Xie 					 pending_snapshot->qgroup_reserved);
677d5c12070SMiao Xie out:
678a22285a6SYan, Zheng 	kfree(pending_snapshot);
679f46b5a66SChristoph Hellwig 	return ret;
680f46b5a66SChristoph Hellwig }
681f46b5a66SChristoph Hellwig 
6824260f7c7SSage Weil /*  copy of check_sticky in fs/namei.c()
6834260f7c7SSage Weil * It's inline, so penalty for filesystems that don't use sticky bit is
6844260f7c7SSage Weil * minimal.
6854260f7c7SSage Weil */
6864260f7c7SSage Weil static inline int btrfs_check_sticky(struct inode *dir, struct inode *inode)
6874260f7c7SSage Weil {
6882f2f43d3SEric W. Biederman 	kuid_t fsuid = current_fsuid();
6894260f7c7SSage Weil 
6904260f7c7SSage Weil 	if (!(dir->i_mode & S_ISVTX))
6914260f7c7SSage Weil 		return 0;
6922f2f43d3SEric W. Biederman 	if (uid_eq(inode->i_uid, fsuid))
6934260f7c7SSage Weil 		return 0;
6942f2f43d3SEric W. Biederman 	if (uid_eq(dir->i_uid, fsuid))
6954260f7c7SSage Weil 		return 0;
6964260f7c7SSage Weil 	return !capable(CAP_FOWNER);
6974260f7c7SSage Weil }
6984260f7c7SSage Weil 
6994260f7c7SSage Weil /*  copy of may_delete in fs/namei.c()
7004260f7c7SSage Weil  *	Check whether we can remove a link victim from directory dir, check
7014260f7c7SSage Weil  *  whether the type of victim is right.
7024260f7c7SSage Weil  *  1. We can't do it if dir is read-only (done in permission())
7034260f7c7SSage Weil  *  2. We should have write and exec permissions on dir
7044260f7c7SSage Weil  *  3. We can't remove anything from append-only dir
7054260f7c7SSage Weil  *  4. We can't do anything with immutable dir (done in permission())
7064260f7c7SSage Weil  *  5. If the sticky bit on dir is set we should either
7074260f7c7SSage Weil  *	a. be owner of dir, or
7084260f7c7SSage Weil  *	b. be owner of victim, or
7094260f7c7SSage Weil  *	c. have CAP_FOWNER capability
7104260f7c7SSage Weil  *  6. If the victim is append-only or immutable we can't do antyhing with
7114260f7c7SSage Weil  *     links pointing to it.
7124260f7c7SSage Weil  *  7. If we were asked to remove a directory and victim isn't one - ENOTDIR.
7134260f7c7SSage Weil  *  8. If we were asked to remove a non-directory and victim isn't one - EISDIR.
7144260f7c7SSage Weil  *  9. We can't remove a root or mountpoint.
7154260f7c7SSage Weil  * 10. We don't allow removal of NFS sillyrenamed files; it's handled by
7164260f7c7SSage Weil  *     nfs_async_unlink().
7174260f7c7SSage Weil  */
7184260f7c7SSage Weil 
7194260f7c7SSage Weil static int btrfs_may_delete(struct inode *dir, struct dentry *victim, int isdir)
7204260f7c7SSage Weil {
7214260f7c7SSage Weil 	int error;
7224260f7c7SSage Weil 
7234260f7c7SSage Weil 	if (!victim->d_inode)
7244260f7c7SSage Weil 		return -ENOENT;
7254260f7c7SSage Weil 
7264260f7c7SSage Weil 	BUG_ON(victim->d_parent->d_inode != dir);
7274fa6b5ecSJeff Layton 	audit_inode_child(dir, victim, AUDIT_TYPE_CHILD_DELETE);
7284260f7c7SSage Weil 
7294260f7c7SSage Weil 	error = inode_permission(dir, MAY_WRITE | MAY_EXEC);
7304260f7c7SSage Weil 	if (error)
7314260f7c7SSage Weil 		return error;
7324260f7c7SSage Weil 	if (IS_APPEND(dir))
7334260f7c7SSage Weil 		return -EPERM;
7344260f7c7SSage Weil 	if (btrfs_check_sticky(dir, victim->d_inode)||
7354260f7c7SSage Weil 		IS_APPEND(victim->d_inode)||
7364260f7c7SSage Weil 	    IS_IMMUTABLE(victim->d_inode) || IS_SWAPFILE(victim->d_inode))
7374260f7c7SSage Weil 		return -EPERM;
7384260f7c7SSage Weil 	if (isdir) {
7394260f7c7SSage Weil 		if (!S_ISDIR(victim->d_inode->i_mode))
7404260f7c7SSage Weil 			return -ENOTDIR;
7414260f7c7SSage Weil 		if (IS_ROOT(victim))
7424260f7c7SSage Weil 			return -EBUSY;
7434260f7c7SSage Weil 	} else if (S_ISDIR(victim->d_inode->i_mode))
7444260f7c7SSage Weil 		return -EISDIR;
7454260f7c7SSage Weil 	if (IS_DEADDIR(dir))
7464260f7c7SSage Weil 		return -ENOENT;
7474260f7c7SSage Weil 	if (victim->d_flags & DCACHE_NFSFS_RENAMED)
7484260f7c7SSage Weil 		return -EBUSY;
7494260f7c7SSage Weil 	return 0;
7504260f7c7SSage Weil }
7514260f7c7SSage Weil 
752cb8e7090SChristoph Hellwig /* copy of may_create in fs/namei.c() */
753cb8e7090SChristoph Hellwig static inline int btrfs_may_create(struct inode *dir, struct dentry *child)
754cb8e7090SChristoph Hellwig {
755cb8e7090SChristoph Hellwig 	if (child->d_inode)
756cb8e7090SChristoph Hellwig 		return -EEXIST;
757cb8e7090SChristoph Hellwig 	if (IS_DEADDIR(dir))
758cb8e7090SChristoph Hellwig 		return -ENOENT;
759cb8e7090SChristoph Hellwig 	return inode_permission(dir, MAY_WRITE | MAY_EXEC);
760cb8e7090SChristoph Hellwig }
761cb8e7090SChristoph Hellwig 
762cb8e7090SChristoph Hellwig /*
763cb8e7090SChristoph Hellwig  * Create a new subvolume below @parent.  This is largely modeled after
764cb8e7090SChristoph Hellwig  * sys_mkdirat and vfs_mkdir, but we only do a single component lookup
765cb8e7090SChristoph Hellwig  * inside this filesystem so it's quite a bit simpler.
766cb8e7090SChristoph Hellwig  */
76776dda93cSYan, Zheng static noinline int btrfs_mksubvol(struct path *parent,
76876dda93cSYan, Zheng 				   char *name, int namelen,
76972fd032eSSage Weil 				   struct btrfs_root *snap_src,
7706f72c7e2SArne Jansen 				   u64 *async_transid, bool readonly,
7718696c533SMiao Xie 				   struct btrfs_qgroup_inherit *inherit)
772cb8e7090SChristoph Hellwig {
77376dda93cSYan, Zheng 	struct inode *dir  = parent->dentry->d_inode;
774cb8e7090SChristoph Hellwig 	struct dentry *dentry;
775cb8e7090SChristoph Hellwig 	int error;
776cb8e7090SChristoph Hellwig 
7775c50c9b8SDavid Sterba 	error = mutex_lock_killable_nested(&dir->i_mutex, I_MUTEX_PARENT);
7785c50c9b8SDavid Sterba 	if (error == -EINTR)
7795c50c9b8SDavid Sterba 		return error;
780cb8e7090SChristoph Hellwig 
781cb8e7090SChristoph Hellwig 	dentry = lookup_one_len(name, parent->dentry, namelen);
782cb8e7090SChristoph Hellwig 	error = PTR_ERR(dentry);
783cb8e7090SChristoph Hellwig 	if (IS_ERR(dentry))
784cb8e7090SChristoph Hellwig 		goto out_unlock;
785cb8e7090SChristoph Hellwig 
786cb8e7090SChristoph Hellwig 	error = -EEXIST;
787cb8e7090SChristoph Hellwig 	if (dentry->d_inode)
788cb8e7090SChristoph Hellwig 		goto out_dput;
789cb8e7090SChristoph Hellwig 
79076dda93cSYan, Zheng 	error = btrfs_may_create(dir, dentry);
791cb8e7090SChristoph Hellwig 	if (error)
792a874a63eSLiu Bo 		goto out_dput;
793cb8e7090SChristoph Hellwig 
7949c52057cSChris Mason 	/*
7959c52057cSChris Mason 	 * even if this name doesn't exist, we may get hash collisions.
7969c52057cSChris Mason 	 * check for them now when we can safely fail
7979c52057cSChris Mason 	 */
7989c52057cSChris Mason 	error = btrfs_check_dir_item_collision(BTRFS_I(dir)->root,
7999c52057cSChris Mason 					       dir->i_ino, name,
8009c52057cSChris Mason 					       namelen);
8019c52057cSChris Mason 	if (error)
8029c52057cSChris Mason 		goto out_dput;
8039c52057cSChris Mason 
80476dda93cSYan, Zheng 	down_read(&BTRFS_I(dir)->root->fs_info->subvol_sem);
80576dda93cSYan, Zheng 
80676dda93cSYan, Zheng 	if (btrfs_root_refs(&BTRFS_I(dir)->root->root_item) == 0)
80776dda93cSYan, Zheng 		goto out_up_read;
80876dda93cSYan, Zheng 
8093de4586cSChris Mason 	if (snap_src) {
810e9662f70SMiao Xie 		error = create_snapshot(snap_src, dir, dentry, name, namelen,
8116f72c7e2SArne Jansen 					async_transid, readonly, inherit);
8123de4586cSChris Mason 	} else {
813d5c12070SMiao Xie 		error = create_subvol(dir, dentry, name, namelen,
814d5c12070SMiao Xie 				      async_transid, inherit);
8153de4586cSChris Mason 	}
81676dda93cSYan, Zheng 	if (!error)
81776dda93cSYan, Zheng 		fsnotify_mkdir(dir, dentry);
81876dda93cSYan, Zheng out_up_read:
81976dda93cSYan, Zheng 	up_read(&BTRFS_I(dir)->root->fs_info->subvol_sem);
820cb8e7090SChristoph Hellwig out_dput:
821cb8e7090SChristoph Hellwig 	dput(dentry);
822cb8e7090SChristoph Hellwig out_unlock:
82376dda93cSYan, Zheng 	mutex_unlock(&dir->i_mutex);
824cb8e7090SChristoph Hellwig 	return error;
825cb8e7090SChristoph Hellwig }
826cb8e7090SChristoph Hellwig 
8274cb5300bSChris Mason /*
8284cb5300bSChris Mason  * When we're defragging a range, we don't want to kick it off again
8294cb5300bSChris Mason  * if it is really just waiting for delalloc to send it down.
8304cb5300bSChris Mason  * If we find a nice big extent or delalloc range for the bytes in the
8314cb5300bSChris Mason  * file you want to defrag, we return 0 to let you know to skip this
8324cb5300bSChris Mason  * part of the file
8334cb5300bSChris Mason  */
8344cb5300bSChris Mason static int check_defrag_in_cache(struct inode *inode, u64 offset, int thresh)
8354cb5300bSChris Mason {
8364cb5300bSChris Mason 	struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
8374cb5300bSChris Mason 	struct extent_map *em = NULL;
8384cb5300bSChris Mason 	struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
8394cb5300bSChris Mason 	u64 end;
8404cb5300bSChris Mason 
8414cb5300bSChris Mason 	read_lock(&em_tree->lock);
8424cb5300bSChris Mason 	em = lookup_extent_mapping(em_tree, offset, PAGE_CACHE_SIZE);
8434cb5300bSChris Mason 	read_unlock(&em_tree->lock);
8444cb5300bSChris Mason 
8454cb5300bSChris Mason 	if (em) {
8464cb5300bSChris Mason 		end = extent_map_end(em);
8474cb5300bSChris Mason 		free_extent_map(em);
8484cb5300bSChris Mason 		if (end - offset > thresh)
8494cb5300bSChris Mason 			return 0;
8504cb5300bSChris Mason 	}
8514cb5300bSChris Mason 	/* if we already have a nice delalloc here, just stop */
8524cb5300bSChris Mason 	thresh /= 2;
8534cb5300bSChris Mason 	end = count_range_bits(io_tree, &offset, offset + thresh,
8544cb5300bSChris Mason 			       thresh, EXTENT_DELALLOC, 1);
8554cb5300bSChris Mason 	if (end >= thresh)
8564cb5300bSChris Mason 		return 0;
8574cb5300bSChris Mason 	return 1;
8584cb5300bSChris Mason }
8594cb5300bSChris Mason 
8604cb5300bSChris Mason /*
8614cb5300bSChris Mason  * helper function to walk through a file and find extents
8624cb5300bSChris Mason  * newer than a specific transid, and smaller than thresh.
8634cb5300bSChris Mason  *
8644cb5300bSChris Mason  * This is used by the defragging code to find new and small
8654cb5300bSChris Mason  * extents
8664cb5300bSChris Mason  */
8674cb5300bSChris Mason static int find_new_extents(struct btrfs_root *root,
8684cb5300bSChris Mason 			    struct inode *inode, u64 newer_than,
8694cb5300bSChris Mason 			    u64 *off, int thresh)
8704cb5300bSChris Mason {
8714cb5300bSChris Mason 	struct btrfs_path *path;
8724cb5300bSChris Mason 	struct btrfs_key min_key;
8734cb5300bSChris Mason 	struct extent_buffer *leaf;
8744cb5300bSChris Mason 	struct btrfs_file_extent_item *extent;
8754cb5300bSChris Mason 	int type;
8764cb5300bSChris Mason 	int ret;
877a4689d2bSDavid Sterba 	u64 ino = btrfs_ino(inode);
8784cb5300bSChris Mason 
8794cb5300bSChris Mason 	path = btrfs_alloc_path();
8804cb5300bSChris Mason 	if (!path)
8814cb5300bSChris Mason 		return -ENOMEM;
8824cb5300bSChris Mason 
883a4689d2bSDavid Sterba 	min_key.objectid = ino;
8844cb5300bSChris Mason 	min_key.type = BTRFS_EXTENT_DATA_KEY;
8854cb5300bSChris Mason 	min_key.offset = *off;
8864cb5300bSChris Mason 
8874cb5300bSChris Mason 	path->keep_locks = 1;
8884cb5300bSChris Mason 
8894cb5300bSChris Mason 	while (1) {
8906174d3cbSFilipe David Borba Manana 		ret = btrfs_search_forward(root, &min_key, path, newer_than);
8914cb5300bSChris Mason 		if (ret != 0)
8924cb5300bSChris Mason 			goto none;
893a4689d2bSDavid Sterba 		if (min_key.objectid != ino)
8944cb5300bSChris Mason 			goto none;
8954cb5300bSChris Mason 		if (min_key.type != BTRFS_EXTENT_DATA_KEY)
8964cb5300bSChris Mason 			goto none;
8974cb5300bSChris Mason 
8984cb5300bSChris Mason 		leaf = path->nodes[0];
8994cb5300bSChris Mason 		extent = btrfs_item_ptr(leaf, path->slots[0],
9004cb5300bSChris Mason 					struct btrfs_file_extent_item);
9014cb5300bSChris Mason 
9024cb5300bSChris Mason 		type = btrfs_file_extent_type(leaf, extent);
9034cb5300bSChris Mason 		if (type == BTRFS_FILE_EXTENT_REG &&
9044cb5300bSChris Mason 		    btrfs_file_extent_num_bytes(leaf, extent) < thresh &&
9054cb5300bSChris Mason 		    check_defrag_in_cache(inode, min_key.offset, thresh)) {
9064cb5300bSChris Mason 			*off = min_key.offset;
9074cb5300bSChris Mason 			btrfs_free_path(path);
9084cb5300bSChris Mason 			return 0;
9094cb5300bSChris Mason 		}
9104cb5300bSChris Mason 
9114cb5300bSChris Mason 		if (min_key.offset == (u64)-1)
9124cb5300bSChris Mason 			goto none;
9134cb5300bSChris Mason 
9144cb5300bSChris Mason 		min_key.offset++;
9154cb5300bSChris Mason 		btrfs_release_path(path);
9164cb5300bSChris Mason 	}
9174cb5300bSChris Mason none:
9184cb5300bSChris Mason 	btrfs_free_path(path);
9194cb5300bSChris Mason 	return -ENOENT;
9204cb5300bSChris Mason }
9214cb5300bSChris Mason 
9226c282eb4SLi Zefan static struct extent_map *defrag_lookup_extent(struct inode *inode, u64 start)
92317ce6ef8SLiu Bo {
92417ce6ef8SLiu Bo 	struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
925940100a4SChris Mason 	struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
9266c282eb4SLi Zefan 	struct extent_map *em;
9276c282eb4SLi Zefan 	u64 len = PAGE_CACHE_SIZE;
928940100a4SChris Mason 
929940100a4SChris Mason 	/*
930940100a4SChris Mason 	 * hopefully we have this extent in the tree already, try without
931940100a4SChris Mason 	 * the full extent lock
932940100a4SChris Mason 	 */
933940100a4SChris Mason 	read_lock(&em_tree->lock);
934940100a4SChris Mason 	em = lookup_extent_mapping(em_tree, start, len);
935940100a4SChris Mason 	read_unlock(&em_tree->lock);
936940100a4SChris Mason 
937940100a4SChris Mason 	if (!em) {
938940100a4SChris Mason 		/* get the big lock and read metadata off disk */
939d0082371SJeff Mahoney 		lock_extent(io_tree, start, start + len - 1);
940940100a4SChris Mason 		em = btrfs_get_extent(inode, NULL, 0, start, len, 0);
941d0082371SJeff Mahoney 		unlock_extent(io_tree, start, start + len - 1);
942940100a4SChris Mason 
9436cf8bfbfSDan Carpenter 		if (IS_ERR(em))
9446c282eb4SLi Zefan 			return NULL;
945940100a4SChris Mason 	}
946940100a4SChris Mason 
9476c282eb4SLi Zefan 	return em;
9486c282eb4SLi Zefan }
9496c282eb4SLi Zefan 
9506c282eb4SLi Zefan static bool defrag_check_next_extent(struct inode *inode, struct extent_map *em)
9516c282eb4SLi Zefan {
9526c282eb4SLi Zefan 	struct extent_map *next;
9536c282eb4SLi Zefan 	bool ret = true;
9546c282eb4SLi Zefan 
9556c282eb4SLi Zefan 	/* this is the last extent */
9566c282eb4SLi Zefan 	if (em->start + em->len >= i_size_read(inode))
9576c282eb4SLi Zefan 		return false;
9586c282eb4SLi Zefan 
9596c282eb4SLi Zefan 	next = defrag_lookup_extent(inode, em->start + em->len);
9606c282eb4SLi Zefan 	if (!next || next->block_start >= EXTENT_MAP_LAST_BYTE)
9616c282eb4SLi Zefan 		ret = false;
9626c282eb4SLi Zefan 
9636c282eb4SLi Zefan 	free_extent_map(next);
9646c282eb4SLi Zefan 	return ret;
9656c282eb4SLi Zefan }
9666c282eb4SLi Zefan 
9676c282eb4SLi Zefan static int should_defrag_range(struct inode *inode, u64 start, int thresh,
968a43a2111SAndrew Mahone 			       u64 *last_len, u64 *skip, u64 *defrag_end,
969a43a2111SAndrew Mahone 			       int compress)
9706c282eb4SLi Zefan {
9716c282eb4SLi Zefan 	struct extent_map *em;
9726c282eb4SLi Zefan 	int ret = 1;
9736c282eb4SLi Zefan 	bool next_mergeable = true;
9746c282eb4SLi Zefan 
9756c282eb4SLi Zefan 	/*
9766c282eb4SLi Zefan 	 * make sure that once we start defragging an extent, we keep on
9776c282eb4SLi Zefan 	 * defragging it
9786c282eb4SLi Zefan 	 */
9796c282eb4SLi Zefan 	if (start < *defrag_end)
9806c282eb4SLi Zefan 		return 1;
9816c282eb4SLi Zefan 
9826c282eb4SLi Zefan 	*skip = 0;
9836c282eb4SLi Zefan 
9846c282eb4SLi Zefan 	em = defrag_lookup_extent(inode, start);
9856c282eb4SLi Zefan 	if (!em)
9866c282eb4SLi Zefan 		return 0;
9876c282eb4SLi Zefan 
988940100a4SChris Mason 	/* this will cover holes, and inline extents */
98917ce6ef8SLiu Bo 	if (em->block_start >= EXTENT_MAP_LAST_BYTE) {
990940100a4SChris Mason 		ret = 0;
99117ce6ef8SLiu Bo 		goto out;
99217ce6ef8SLiu Bo 	}
99317ce6ef8SLiu Bo 
9946c282eb4SLi Zefan 	next_mergeable = defrag_check_next_extent(inode, em);
995940100a4SChris Mason 
996940100a4SChris Mason 	/*
9976c282eb4SLi Zefan 	 * we hit a real extent, if it is big or the next extent is not a
9986c282eb4SLi Zefan 	 * real extent, don't bother defragging it
999940100a4SChris Mason 	 */
1000a43a2111SAndrew Mahone 	if (!compress && (*last_len == 0 || *last_len >= thresh) &&
10016c282eb4SLi Zefan 	    (em->len >= thresh || !next_mergeable))
1002940100a4SChris Mason 		ret = 0;
100317ce6ef8SLiu Bo out:
1004940100a4SChris Mason 	/*
1005940100a4SChris Mason 	 * last_len ends up being a counter of how many bytes we've defragged.
1006940100a4SChris Mason 	 * every time we choose not to defrag an extent, we reset *last_len
1007940100a4SChris Mason 	 * so that the next tiny extent will force a defrag.
1008940100a4SChris Mason 	 *
1009940100a4SChris Mason 	 * The end result of this is that tiny extents before a single big
1010940100a4SChris Mason 	 * extent will force at least part of that big extent to be defragged.
1011940100a4SChris Mason 	 */
1012940100a4SChris Mason 	if (ret) {
1013940100a4SChris Mason 		*defrag_end = extent_map_end(em);
1014940100a4SChris Mason 	} else {
1015940100a4SChris Mason 		*last_len = 0;
1016940100a4SChris Mason 		*skip = extent_map_end(em);
1017940100a4SChris Mason 		*defrag_end = 0;
1018940100a4SChris Mason 	}
1019940100a4SChris Mason 
1020940100a4SChris Mason 	free_extent_map(em);
1021940100a4SChris Mason 	return ret;
1022940100a4SChris Mason }
1023940100a4SChris Mason 
10244cb5300bSChris Mason /*
10254cb5300bSChris Mason  * it doesn't do much good to defrag one or two pages
10264cb5300bSChris Mason  * at a time.  This pulls in a nice chunk of pages
10274cb5300bSChris Mason  * to COW and defrag.
10284cb5300bSChris Mason  *
10294cb5300bSChris Mason  * It also makes sure the delalloc code has enough
10304cb5300bSChris Mason  * dirty data to avoid making new small extents as part
10314cb5300bSChris Mason  * of the defrag
10324cb5300bSChris Mason  *
10334cb5300bSChris Mason  * It's a good idea to start RA on this range
10344cb5300bSChris Mason  * before calling this.
10354cb5300bSChris Mason  */
10364cb5300bSChris Mason static int cluster_pages_for_defrag(struct inode *inode,
10374cb5300bSChris Mason 				    struct page **pages,
10384cb5300bSChris Mason 				    unsigned long start_index,
1039c41570c9SJustin Maggard 				    unsigned long num_pages)
1040f46b5a66SChristoph Hellwig {
10414cb5300bSChris Mason 	unsigned long file_end;
10424cb5300bSChris Mason 	u64 isize = i_size_read(inode);
1043f46b5a66SChristoph Hellwig 	u64 page_start;
1044f46b5a66SChristoph Hellwig 	u64 page_end;
10451f12bd06SLiu Bo 	u64 page_cnt;
10464cb5300bSChris Mason 	int ret;
10474cb5300bSChris Mason 	int i;
10484cb5300bSChris Mason 	int i_done;
10494cb5300bSChris Mason 	struct btrfs_ordered_extent *ordered;
10504cb5300bSChris Mason 	struct extent_state *cached_state = NULL;
1051600a45e1SMiao Xie 	struct extent_io_tree *tree;
10523b16a4e3SJosef Bacik 	gfp_t mask = btrfs_alloc_write_mask(inode->i_mapping);
10534cb5300bSChris Mason 
10544cb5300bSChris Mason 	file_end = (isize - 1) >> PAGE_CACHE_SHIFT;
10551f12bd06SLiu Bo 	if (!isize || start_index > file_end)
10561f12bd06SLiu Bo 		return 0;
10571f12bd06SLiu Bo 
10581f12bd06SLiu Bo 	page_cnt = min_t(u64, (u64)num_pages, (u64)file_end - start_index + 1);
10594cb5300bSChris Mason 
10604cb5300bSChris Mason 	ret = btrfs_delalloc_reserve_space(inode,
10611f12bd06SLiu Bo 					   page_cnt << PAGE_CACHE_SHIFT);
10624cb5300bSChris Mason 	if (ret)
10634cb5300bSChris Mason 		return ret;
10644cb5300bSChris Mason 	i_done = 0;
1065600a45e1SMiao Xie 	tree = &BTRFS_I(inode)->io_tree;
10664cb5300bSChris Mason 
10674cb5300bSChris Mason 	/* step one, lock all the pages */
10681f12bd06SLiu Bo 	for (i = 0; i < page_cnt; i++) {
10694cb5300bSChris Mason 		struct page *page;
1070600a45e1SMiao Xie again:
1071a94733d0SJosef Bacik 		page = find_or_create_page(inode->i_mapping,
10723b16a4e3SJosef Bacik 					   start_index + i, mask);
10734cb5300bSChris Mason 		if (!page)
10744cb5300bSChris Mason 			break;
10754cb5300bSChris Mason 
1076600a45e1SMiao Xie 		page_start = page_offset(page);
1077600a45e1SMiao Xie 		page_end = page_start + PAGE_CACHE_SIZE - 1;
1078600a45e1SMiao Xie 		while (1) {
1079d0082371SJeff Mahoney 			lock_extent(tree, page_start, page_end);
1080600a45e1SMiao Xie 			ordered = btrfs_lookup_ordered_extent(inode,
1081600a45e1SMiao Xie 							      page_start);
1082d0082371SJeff Mahoney 			unlock_extent(tree, page_start, page_end);
1083600a45e1SMiao Xie 			if (!ordered)
1084600a45e1SMiao Xie 				break;
1085600a45e1SMiao Xie 
1086600a45e1SMiao Xie 			unlock_page(page);
1087600a45e1SMiao Xie 			btrfs_start_ordered_extent(inode, ordered, 1);
1088600a45e1SMiao Xie 			btrfs_put_ordered_extent(ordered);
1089600a45e1SMiao Xie 			lock_page(page);
10901f12bd06SLiu Bo 			/*
10911f12bd06SLiu Bo 			 * we unlocked the page above, so we need check if
10921f12bd06SLiu Bo 			 * it was released or not.
10931f12bd06SLiu Bo 			 */
10941f12bd06SLiu Bo 			if (page->mapping != inode->i_mapping) {
10951f12bd06SLiu Bo 				unlock_page(page);
10961f12bd06SLiu Bo 				page_cache_release(page);
10971f12bd06SLiu Bo 				goto again;
10981f12bd06SLiu Bo 			}
1099600a45e1SMiao Xie 		}
1100600a45e1SMiao Xie 
11014cb5300bSChris Mason 		if (!PageUptodate(page)) {
11024cb5300bSChris Mason 			btrfs_readpage(NULL, page);
11034cb5300bSChris Mason 			lock_page(page);
11044cb5300bSChris Mason 			if (!PageUptodate(page)) {
11054cb5300bSChris Mason 				unlock_page(page);
11064cb5300bSChris Mason 				page_cache_release(page);
11074cb5300bSChris Mason 				ret = -EIO;
11084cb5300bSChris Mason 				break;
11094cb5300bSChris Mason 			}
11104cb5300bSChris Mason 		}
1111600a45e1SMiao Xie 
1112600a45e1SMiao Xie 		if (page->mapping != inode->i_mapping) {
1113600a45e1SMiao Xie 			unlock_page(page);
1114600a45e1SMiao Xie 			page_cache_release(page);
1115600a45e1SMiao Xie 			goto again;
1116600a45e1SMiao Xie 		}
1117600a45e1SMiao Xie 
11184cb5300bSChris Mason 		pages[i] = page;
11194cb5300bSChris Mason 		i_done++;
11204cb5300bSChris Mason 	}
11214cb5300bSChris Mason 	if (!i_done || ret)
11224cb5300bSChris Mason 		goto out;
11234cb5300bSChris Mason 
11244cb5300bSChris Mason 	if (!(inode->i_sb->s_flags & MS_ACTIVE))
11254cb5300bSChris Mason 		goto out;
11264cb5300bSChris Mason 
11274cb5300bSChris Mason 	/*
11284cb5300bSChris Mason 	 * so now we have a nice long stream of locked
11294cb5300bSChris Mason 	 * and up to date pages, lets wait on them
11304cb5300bSChris Mason 	 */
11314cb5300bSChris Mason 	for (i = 0; i < i_done; i++)
11324cb5300bSChris Mason 		wait_on_page_writeback(pages[i]);
11334cb5300bSChris Mason 
11344cb5300bSChris Mason 	page_start = page_offset(pages[0]);
11354cb5300bSChris Mason 	page_end = page_offset(pages[i_done - 1]) + PAGE_CACHE_SIZE;
11364cb5300bSChris Mason 
11374cb5300bSChris Mason 	lock_extent_bits(&BTRFS_I(inode)->io_tree,
1138d0082371SJeff Mahoney 			 page_start, page_end - 1, 0, &cached_state);
11394cb5300bSChris Mason 	clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start,
11404cb5300bSChris Mason 			  page_end - 1, EXTENT_DIRTY | EXTENT_DELALLOC |
11419e8a4a8bSLiu Bo 			  EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG, 0, 0,
11429e8a4a8bSLiu Bo 			  &cached_state, GFP_NOFS);
11434cb5300bSChris Mason 
11441f12bd06SLiu Bo 	if (i_done != page_cnt) {
11459e0baf60SJosef Bacik 		spin_lock(&BTRFS_I(inode)->lock);
11469e0baf60SJosef Bacik 		BTRFS_I(inode)->outstanding_extents++;
11479e0baf60SJosef Bacik 		spin_unlock(&BTRFS_I(inode)->lock);
11484cb5300bSChris Mason 		btrfs_delalloc_release_space(inode,
11491f12bd06SLiu Bo 				     (page_cnt - i_done) << PAGE_CACHE_SHIFT);
11504cb5300bSChris Mason 	}
11514cb5300bSChris Mason 
11524cb5300bSChris Mason 
11539e8a4a8bSLiu Bo 	set_extent_defrag(&BTRFS_I(inode)->io_tree, page_start, page_end - 1,
11549e8a4a8bSLiu Bo 			  &cached_state, GFP_NOFS);
11554cb5300bSChris Mason 
11564cb5300bSChris Mason 	unlock_extent_cached(&BTRFS_I(inode)->io_tree,
11574cb5300bSChris Mason 			     page_start, page_end - 1, &cached_state,
11584cb5300bSChris Mason 			     GFP_NOFS);
11594cb5300bSChris Mason 
11604cb5300bSChris Mason 	for (i = 0; i < i_done; i++) {
11614cb5300bSChris Mason 		clear_page_dirty_for_io(pages[i]);
11624cb5300bSChris Mason 		ClearPageChecked(pages[i]);
11634cb5300bSChris Mason 		set_page_extent_mapped(pages[i]);
11644cb5300bSChris Mason 		set_page_dirty(pages[i]);
11654cb5300bSChris Mason 		unlock_page(pages[i]);
11664cb5300bSChris Mason 		page_cache_release(pages[i]);
11674cb5300bSChris Mason 	}
11684cb5300bSChris Mason 	return i_done;
11694cb5300bSChris Mason out:
11704cb5300bSChris Mason 	for (i = 0; i < i_done; i++) {
11714cb5300bSChris Mason 		unlock_page(pages[i]);
11724cb5300bSChris Mason 		page_cache_release(pages[i]);
11734cb5300bSChris Mason 	}
11741f12bd06SLiu Bo 	btrfs_delalloc_release_space(inode, page_cnt << PAGE_CACHE_SHIFT);
11754cb5300bSChris Mason 	return ret;
11764cb5300bSChris Mason 
11774cb5300bSChris Mason }
11784cb5300bSChris Mason 
11794cb5300bSChris Mason int btrfs_defrag_file(struct inode *inode, struct file *file,
11804cb5300bSChris Mason 		      struct btrfs_ioctl_defrag_range_args *range,
11814cb5300bSChris Mason 		      u64 newer_than, unsigned long max_to_defrag)
11824cb5300bSChris Mason {
11834cb5300bSChris Mason 	struct btrfs_root *root = BTRFS_I(inode)->root;
11844cb5300bSChris Mason 	struct file_ra_state *ra = NULL;
11854cb5300bSChris Mason 	unsigned long last_index;
1186151a31b2SLi Zefan 	u64 isize = i_size_read(inode);
1187940100a4SChris Mason 	u64 last_len = 0;
1188940100a4SChris Mason 	u64 skip = 0;
1189940100a4SChris Mason 	u64 defrag_end = 0;
11904cb5300bSChris Mason 	u64 newer_off = range->start;
1191f46b5a66SChristoph Hellwig 	unsigned long i;
1192008873eaSLi Zefan 	unsigned long ra_index = 0;
1193f46b5a66SChristoph Hellwig 	int ret;
11944cb5300bSChris Mason 	int defrag_count = 0;
11951a419d85SLi Zefan 	int compress_type = BTRFS_COMPRESS_ZLIB;
11964cb5300bSChris Mason 	int extent_thresh = range->extent_thresh;
1197c41570c9SJustin Maggard 	unsigned long max_cluster = (256 * 1024) >> PAGE_CACHE_SHIFT;
1198c41570c9SJustin Maggard 	unsigned long cluster = max_cluster;
11994cb5300bSChris Mason 	u64 new_align = ~((u64)128 * 1024 - 1);
12004cb5300bSChris Mason 	struct page **pages = NULL;
12014cb5300bSChris Mason 
12020abd5b17SLiu Bo 	if (isize == 0)
12030abd5b17SLiu Bo 		return 0;
12040abd5b17SLiu Bo 
12050abd5b17SLiu Bo 	if (range->start >= isize)
12060abd5b17SLiu Bo 		return -EINVAL;
12071a419d85SLi Zefan 
12081a419d85SLi Zefan 	if (range->flags & BTRFS_DEFRAG_RANGE_COMPRESS) {
12091a419d85SLi Zefan 		if (range->compress_type > BTRFS_COMPRESS_TYPES)
12101a419d85SLi Zefan 			return -EINVAL;
12111a419d85SLi Zefan 		if (range->compress_type)
12121a419d85SLi Zefan 			compress_type = range->compress_type;
12131a419d85SLi Zefan 	}
1214f46b5a66SChristoph Hellwig 
12150abd5b17SLiu Bo 	if (extent_thresh == 0)
12160abd5b17SLiu Bo 		extent_thresh = 256 * 1024;
1217f46b5a66SChristoph Hellwig 
12184cb5300bSChris Mason 	/*
12194cb5300bSChris Mason 	 * if we were not given a file, allocate a readahead
12204cb5300bSChris Mason 	 * context
12214cb5300bSChris Mason 	 */
12224cb5300bSChris Mason 	if (!file) {
12234cb5300bSChris Mason 		ra = kzalloc(sizeof(*ra), GFP_NOFS);
12244cb5300bSChris Mason 		if (!ra)
12254cb5300bSChris Mason 			return -ENOMEM;
12264cb5300bSChris Mason 		file_ra_state_init(ra, inode->i_mapping);
12274cb5300bSChris Mason 	} else {
12284cb5300bSChris Mason 		ra = &file->f_ra;
12294cb5300bSChris Mason 	}
12304cb5300bSChris Mason 
1231d9b0d9baSDulshani Gunawardhana 	pages = kmalloc_array(max_cluster, sizeof(struct page *),
12324cb5300bSChris Mason 			GFP_NOFS);
12334cb5300bSChris Mason 	if (!pages) {
12344cb5300bSChris Mason 		ret = -ENOMEM;
12354cb5300bSChris Mason 		goto out_ra;
12364cb5300bSChris Mason 	}
12374cb5300bSChris Mason 
12384cb5300bSChris Mason 	/* find the last page to defrag */
12391e701a32SChris Mason 	if (range->start + range->len > range->start) {
1240151a31b2SLi Zefan 		last_index = min_t(u64, isize - 1,
12411e701a32SChris Mason 			 range->start + range->len - 1) >> PAGE_CACHE_SHIFT;
12421e701a32SChris Mason 	} else {
1243151a31b2SLi Zefan 		last_index = (isize - 1) >> PAGE_CACHE_SHIFT;
12441e701a32SChris Mason 	}
12451e701a32SChris Mason 
12464cb5300bSChris Mason 	if (newer_than) {
12474cb5300bSChris Mason 		ret = find_new_extents(root, inode, newer_than,
12484cb5300bSChris Mason 				       &newer_off, 64 * 1024);
12494cb5300bSChris Mason 		if (!ret) {
12504cb5300bSChris Mason 			range->start = newer_off;
12514cb5300bSChris Mason 			/*
12524cb5300bSChris Mason 			 * we always align our defrag to help keep
12534cb5300bSChris Mason 			 * the extents in the file evenly spaced
12544cb5300bSChris Mason 			 */
12554cb5300bSChris Mason 			i = (newer_off & new_align) >> PAGE_CACHE_SHIFT;
12564cb5300bSChris Mason 		} else
12574cb5300bSChris Mason 			goto out_ra;
12584cb5300bSChris Mason 	} else {
12591e701a32SChris Mason 		i = range->start >> PAGE_CACHE_SHIFT;
12604cb5300bSChris Mason 	}
12614cb5300bSChris Mason 	if (!max_to_defrag)
12627ec31b54SLiu Bo 		max_to_defrag = last_index + 1;
12634cb5300bSChris Mason 
12642a0f7f57SLi Zefan 	/*
12652a0f7f57SLi Zefan 	 * make writeback starts from i, so the defrag range can be
12662a0f7f57SLi Zefan 	 * written sequentially.
12672a0f7f57SLi Zefan 	 */
12682a0f7f57SLi Zefan 	if (i < inode->i_mapping->writeback_index)
12692a0f7f57SLi Zefan 		inode->i_mapping->writeback_index = i;
12702a0f7f57SLi Zefan 
1271f7f43cc8SChris Mason 	while (i <= last_index && defrag_count < max_to_defrag &&
1272f7f43cc8SChris Mason 	       (i < (i_size_read(inode) + PAGE_CACHE_SIZE - 1) >>
1273f7f43cc8SChris Mason 		PAGE_CACHE_SHIFT)) {
12744cb5300bSChris Mason 		/*
12754cb5300bSChris Mason 		 * make sure we stop running if someone unmounts
12764cb5300bSChris Mason 		 * the FS
12774cb5300bSChris Mason 		 */
12784cb5300bSChris Mason 		if (!(inode->i_sb->s_flags & MS_ACTIVE))
12794cb5300bSChris Mason 			break;
12804cb5300bSChris Mason 
1281210549ebSDavid Sterba 		if (btrfs_defrag_cancelled(root->fs_info)) {
1282efe120a0SFrank Holton 			printk(KERN_DEBUG "BTRFS: defrag_file cancelled\n");
1283210549ebSDavid Sterba 			ret = -EAGAIN;
1284210549ebSDavid Sterba 			break;
1285210549ebSDavid Sterba 		}
1286210549ebSDavid Sterba 
128766c26892SLiu Bo 		if (!should_defrag_range(inode, (u64)i << PAGE_CACHE_SHIFT,
12886c282eb4SLi Zefan 					 extent_thresh, &last_len, &skip,
1289a43a2111SAndrew Mahone 					 &defrag_end, range->flags &
1290a43a2111SAndrew Mahone 					 BTRFS_DEFRAG_RANGE_COMPRESS)) {
1291940100a4SChris Mason 			unsigned long next;
1292940100a4SChris Mason 			/*
1293940100a4SChris Mason 			 * the should_defrag function tells us how much to skip
1294940100a4SChris Mason 			 * bump our counter by the suggested amount
1295940100a4SChris Mason 			 */
1296940100a4SChris Mason 			next = (skip + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
1297940100a4SChris Mason 			i = max(i + 1, next);
1298940100a4SChris Mason 			continue;
1299940100a4SChris Mason 		}
1300008873eaSLi Zefan 
1301008873eaSLi Zefan 		if (!newer_than) {
1302008873eaSLi Zefan 			cluster = (PAGE_CACHE_ALIGN(defrag_end) >>
1303008873eaSLi Zefan 				   PAGE_CACHE_SHIFT) - i;
1304008873eaSLi Zefan 			cluster = min(cluster, max_cluster);
1305008873eaSLi Zefan 		} else {
1306008873eaSLi Zefan 			cluster = max_cluster;
1307008873eaSLi Zefan 		}
1308008873eaSLi Zefan 
1309008873eaSLi Zefan 		if (i + cluster > ra_index) {
1310008873eaSLi Zefan 			ra_index = max(i, ra_index);
1311008873eaSLi Zefan 			btrfs_force_ra(inode->i_mapping, ra, file, ra_index,
1312008873eaSLi Zefan 				       cluster);
1313008873eaSLi Zefan 			ra_index += max_cluster;
1314008873eaSLi Zefan 		}
13154cb5300bSChris Mason 
1316ecb8bea8SLiu Bo 		mutex_lock(&inode->i_mutex);
1317633085c7SFilipe David Borba Manana 		if (range->flags & BTRFS_DEFRAG_RANGE_COMPRESS)
1318633085c7SFilipe David Borba Manana 			BTRFS_I(inode)->force_compress = compress_type;
1319008873eaSLi Zefan 		ret = cluster_pages_for_defrag(inode, pages, i, cluster);
1320ecb8bea8SLiu Bo 		if (ret < 0) {
1321ecb8bea8SLiu Bo 			mutex_unlock(&inode->i_mutex);
13224cb5300bSChris Mason 			goto out_ra;
1323ecb8bea8SLiu Bo 		}
13244cb5300bSChris Mason 
13254cb5300bSChris Mason 		defrag_count += ret;
1326d0e1d66bSNamjae Jeon 		balance_dirty_pages_ratelimited(inode->i_mapping);
1327ecb8bea8SLiu Bo 		mutex_unlock(&inode->i_mutex);
13284cb5300bSChris Mason 
13294cb5300bSChris Mason 		if (newer_than) {
13304cb5300bSChris Mason 			if (newer_off == (u64)-1)
13314cb5300bSChris Mason 				break;
13324cb5300bSChris Mason 
1333e1f041e1SLiu Bo 			if (ret > 0)
1334e1f041e1SLiu Bo 				i += ret;
1335e1f041e1SLiu Bo 
13364cb5300bSChris Mason 			newer_off = max(newer_off + 1,
13374cb5300bSChris Mason 					(u64)i << PAGE_CACHE_SHIFT);
13384cb5300bSChris Mason 
13394cb5300bSChris Mason 			ret = find_new_extents(root, inode,
13404cb5300bSChris Mason 					       newer_than, &newer_off,
13414cb5300bSChris Mason 					       64 * 1024);
13424cb5300bSChris Mason 			if (!ret) {
13434cb5300bSChris Mason 				range->start = newer_off;
13444cb5300bSChris Mason 				i = (newer_off & new_align) >> PAGE_CACHE_SHIFT;
13454cb5300bSChris Mason 			} else {
13464cb5300bSChris Mason 				break;
1347940100a4SChris Mason 			}
13484cb5300bSChris Mason 		} else {
1349008873eaSLi Zefan 			if (ret > 0) {
1350cbcc8326SLi Zefan 				i += ret;
1351008873eaSLi Zefan 				last_len += ret << PAGE_CACHE_SHIFT;
1352008873eaSLi Zefan 			} else {
1353940100a4SChris Mason 				i++;
1354008873eaSLi Zefan 				last_len = 0;
1355008873eaSLi Zefan 			}
1356f46b5a66SChristoph Hellwig 		}
13574cb5300bSChris Mason 	}
1358f46b5a66SChristoph Hellwig 
13591e701a32SChris Mason 	if ((range->flags & BTRFS_DEFRAG_RANGE_START_IO))
13601e701a32SChris Mason 		filemap_flush(inode->i_mapping);
13611e701a32SChris Mason 
13621e701a32SChris Mason 	if ((range->flags & BTRFS_DEFRAG_RANGE_COMPRESS)) {
13631e701a32SChris Mason 		/* the filemap_flush will queue IO into the worker threads, but
13641e701a32SChris Mason 		 * we have to make sure the IO is actually started and that
13651e701a32SChris Mason 		 * ordered extents get created before we return
13661e701a32SChris Mason 		 */
13671e701a32SChris Mason 		atomic_inc(&root->fs_info->async_submit_draining);
13681e701a32SChris Mason 		while (atomic_read(&root->fs_info->nr_async_submits) ||
13691e701a32SChris Mason 		      atomic_read(&root->fs_info->async_delalloc_pages)) {
13701e701a32SChris Mason 			wait_event(root->fs_info->async_submit_wait,
13711e701a32SChris Mason 			   (atomic_read(&root->fs_info->nr_async_submits) == 0 &&
13721e701a32SChris Mason 			    atomic_read(&root->fs_info->async_delalloc_pages) == 0));
13731e701a32SChris Mason 		}
13741e701a32SChris Mason 		atomic_dec(&root->fs_info->async_submit_draining);
13751e701a32SChris Mason 	}
13761e701a32SChris Mason 
13771a419d85SLi Zefan 	if (range->compress_type == BTRFS_COMPRESS_LZO) {
13782b0ce2c2SMitch Harder 		btrfs_set_fs_incompat(root->fs_info, COMPRESS_LZO);
13791a419d85SLi Zefan 	}
13801a419d85SLi Zefan 
138160ccf82fSDiego Calleja 	ret = defrag_count;
1382940100a4SChris Mason 
13834cb5300bSChris Mason out_ra:
1384633085c7SFilipe David Borba Manana 	if (range->flags & BTRFS_DEFRAG_RANGE_COMPRESS) {
1385633085c7SFilipe David Borba Manana 		mutex_lock(&inode->i_mutex);
1386633085c7SFilipe David Borba Manana 		BTRFS_I(inode)->force_compress = BTRFS_COMPRESS_NONE;
1387633085c7SFilipe David Borba Manana 		mutex_unlock(&inode->i_mutex);
1388633085c7SFilipe David Borba Manana 	}
13894cb5300bSChris Mason 	if (!file)
13904cb5300bSChris Mason 		kfree(ra);
13914cb5300bSChris Mason 	kfree(pages);
1392940100a4SChris Mason 	return ret;
1393f46b5a66SChristoph Hellwig }
1394f46b5a66SChristoph Hellwig 
1395198605a8SMiao Xie static noinline int btrfs_ioctl_resize(struct file *file,
139676dda93cSYan, Zheng 					void __user *arg)
1397f46b5a66SChristoph Hellwig {
1398f46b5a66SChristoph Hellwig 	u64 new_size;
1399f46b5a66SChristoph Hellwig 	u64 old_size;
1400f46b5a66SChristoph Hellwig 	u64 devid = 1;
1401496ad9aaSAl Viro 	struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
1402f46b5a66SChristoph Hellwig 	struct btrfs_ioctl_vol_args *vol_args;
1403f46b5a66SChristoph Hellwig 	struct btrfs_trans_handle *trans;
1404f46b5a66SChristoph Hellwig 	struct btrfs_device *device = NULL;
1405f46b5a66SChristoph Hellwig 	char *sizestr;
1406f46b5a66SChristoph Hellwig 	char *devstr = NULL;
1407f46b5a66SChristoph Hellwig 	int ret = 0;
1408f46b5a66SChristoph Hellwig 	int mod = 0;
1409f46b5a66SChristoph Hellwig 
1410e441d54dSChris Mason 	if (!capable(CAP_SYS_ADMIN))
1411e441d54dSChris Mason 		return -EPERM;
1412e441d54dSChris Mason 
1413198605a8SMiao Xie 	ret = mnt_want_write_file(file);
1414198605a8SMiao Xie 	if (ret)
1415198605a8SMiao Xie 		return ret;
1416198605a8SMiao Xie 
14175ac00addSStefan Behrens 	if (atomic_xchg(&root->fs_info->mutually_exclusive_operation_running,
14185ac00addSStefan Behrens 			1)) {
141997547676SMiao Xie 		mnt_drop_write_file(file);
1420e57138b3SAnand Jain 		return BTRFS_ERROR_DEV_EXCL_RUN_IN_PROGRESS;
1421c9e9f97bSIlya Dryomov 	}
1422c9e9f97bSIlya Dryomov 
14235ac00addSStefan Behrens 	mutex_lock(&root->fs_info->volume_mutex);
1424dae7b665SLi Zefan 	vol_args = memdup_user(arg, sizeof(*vol_args));
1425c9e9f97bSIlya Dryomov 	if (IS_ERR(vol_args)) {
1426c9e9f97bSIlya Dryomov 		ret = PTR_ERR(vol_args);
1427c9e9f97bSIlya Dryomov 		goto out;
1428c9e9f97bSIlya Dryomov 	}
14295516e595SMark Fasheh 
14305516e595SMark Fasheh 	vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
1431f46b5a66SChristoph Hellwig 
1432f46b5a66SChristoph Hellwig 	sizestr = vol_args->name;
1433f46b5a66SChristoph Hellwig 	devstr = strchr(sizestr, ':');
1434f46b5a66SChristoph Hellwig 	if (devstr) {
1435f46b5a66SChristoph Hellwig 		char *end;
1436f46b5a66SChristoph Hellwig 		sizestr = devstr + 1;
1437f46b5a66SChristoph Hellwig 		*devstr = '\0';
1438f46b5a66SChristoph Hellwig 		devstr = vol_args->name;
1439f46b5a66SChristoph Hellwig 		devid = simple_strtoull(devstr, &end, 10);
1440dfd79829SMiao Xie 		if (!devid) {
1441dfd79829SMiao Xie 			ret = -EINVAL;
1442dfd79829SMiao Xie 			goto out_free;
1443dfd79829SMiao Xie 		}
1444efe120a0SFrank Holton 		btrfs_info(root->fs_info, "resizing devid %llu", devid);
1445f46b5a66SChristoph Hellwig 	}
1446dba60f3fSMiao Xie 
1447aa1b8cd4SStefan Behrens 	device = btrfs_find_device(root->fs_info, devid, NULL, NULL);
1448f46b5a66SChristoph Hellwig 	if (!device) {
1449efe120a0SFrank Holton 		btrfs_info(root->fs_info, "resizer unable to find device %llu",
1450c1c9ff7cSGeert Uytterhoeven 		       devid);
1451dfd79829SMiao Xie 		ret = -ENODEV;
1452c9e9f97bSIlya Dryomov 		goto out_free;
1453f46b5a66SChristoph Hellwig 	}
1454dba60f3fSMiao Xie 
1455dba60f3fSMiao Xie 	if (!device->writeable) {
1456efe120a0SFrank Holton 		btrfs_info(root->fs_info,
1457efe120a0SFrank Holton 			   "resizer unable to apply on readonly device %llu",
1458c1c9ff7cSGeert Uytterhoeven 		       devid);
1459dfd79829SMiao Xie 		ret = -EPERM;
14604e42ae1bSLiu Bo 		goto out_free;
14614e42ae1bSLiu Bo 	}
14624e42ae1bSLiu Bo 
1463f46b5a66SChristoph Hellwig 	if (!strcmp(sizestr, "max"))
1464f46b5a66SChristoph Hellwig 		new_size = device->bdev->bd_inode->i_size;
1465f46b5a66SChristoph Hellwig 	else {
1466f46b5a66SChristoph Hellwig 		if (sizestr[0] == '-') {
1467f46b5a66SChristoph Hellwig 			mod = -1;
1468f46b5a66SChristoph Hellwig 			sizestr++;
1469f46b5a66SChristoph Hellwig 		} else if (sizestr[0] == '+') {
1470f46b5a66SChristoph Hellwig 			mod = 1;
1471f46b5a66SChristoph Hellwig 			sizestr++;
1472f46b5a66SChristoph Hellwig 		}
147391748467SAkinobu Mita 		new_size = memparse(sizestr, NULL);
1474f46b5a66SChristoph Hellwig 		if (new_size == 0) {
1475f46b5a66SChristoph Hellwig 			ret = -EINVAL;
1476c9e9f97bSIlya Dryomov 			goto out_free;
1477f46b5a66SChristoph Hellwig 		}
1478f46b5a66SChristoph Hellwig 	}
1479f46b5a66SChristoph Hellwig 
148063a212abSStefan Behrens 	if (device->is_tgtdev_for_dev_replace) {
1481dfd79829SMiao Xie 		ret = -EPERM;
148263a212abSStefan Behrens 		goto out_free;
148363a212abSStefan Behrens 	}
148463a212abSStefan Behrens 
1485f46b5a66SChristoph Hellwig 	old_size = device->total_bytes;
1486f46b5a66SChristoph Hellwig 
1487f46b5a66SChristoph Hellwig 	if (mod < 0) {
1488f46b5a66SChristoph Hellwig 		if (new_size > old_size) {
1489f46b5a66SChristoph Hellwig 			ret = -EINVAL;
1490c9e9f97bSIlya Dryomov 			goto out_free;
1491f46b5a66SChristoph Hellwig 		}
1492f46b5a66SChristoph Hellwig 		new_size = old_size - new_size;
1493f46b5a66SChristoph Hellwig 	} else if (mod > 0) {
1494eb8052e0SWenliang Fan 		if (new_size > ULLONG_MAX - old_size) {
1495eb8052e0SWenliang Fan 			ret = -EINVAL;
1496eb8052e0SWenliang Fan 			goto out_free;
1497eb8052e0SWenliang Fan 		}
1498f46b5a66SChristoph Hellwig 		new_size = old_size + new_size;
1499f46b5a66SChristoph Hellwig 	}
1500f46b5a66SChristoph Hellwig 
1501f46b5a66SChristoph Hellwig 	if (new_size < 256 * 1024 * 1024) {
1502f46b5a66SChristoph Hellwig 		ret = -EINVAL;
1503c9e9f97bSIlya Dryomov 		goto out_free;
1504f46b5a66SChristoph Hellwig 	}
1505f46b5a66SChristoph Hellwig 	if (new_size > device->bdev->bd_inode->i_size) {
1506f46b5a66SChristoph Hellwig 		ret = -EFBIG;
1507c9e9f97bSIlya Dryomov 		goto out_free;
1508f46b5a66SChristoph Hellwig 	}
1509f46b5a66SChristoph Hellwig 
1510f46b5a66SChristoph Hellwig 	do_div(new_size, root->sectorsize);
1511f46b5a66SChristoph Hellwig 	new_size *= root->sectorsize;
1512f46b5a66SChristoph Hellwig 
1513efe120a0SFrank Holton 	printk_in_rcu(KERN_INFO "BTRFS: new size for %s is %llu\n",
1514c1c9ff7cSGeert Uytterhoeven 		      rcu_str_deref(device->name), new_size);
1515f46b5a66SChristoph Hellwig 
1516f46b5a66SChristoph Hellwig 	if (new_size > old_size) {
1517a22285a6SYan, Zheng 		trans = btrfs_start_transaction(root, 0);
151898d5dc13STsutomu Itoh 		if (IS_ERR(trans)) {
151998d5dc13STsutomu Itoh 			ret = PTR_ERR(trans);
1520c9e9f97bSIlya Dryomov 			goto out_free;
152198d5dc13STsutomu Itoh 		}
1522f46b5a66SChristoph Hellwig 		ret = btrfs_grow_device(trans, device, new_size);
1523f46b5a66SChristoph Hellwig 		btrfs_commit_transaction(trans, root);
1524ece7d20eSMike Fleetwood 	} else if (new_size < old_size) {
1525f46b5a66SChristoph Hellwig 		ret = btrfs_shrink_device(device, new_size);
15260253f40eSjeff.liu 	} /* equal, nothing need to do */
1527f46b5a66SChristoph Hellwig 
1528c9e9f97bSIlya Dryomov out_free:
1529f46b5a66SChristoph Hellwig 	kfree(vol_args);
1530c9e9f97bSIlya Dryomov out:
1531c9e9f97bSIlya Dryomov 	mutex_unlock(&root->fs_info->volume_mutex);
15325ac00addSStefan Behrens 	atomic_set(&root->fs_info->mutually_exclusive_operation_running, 0);
153318f39c41SIlya Dryomov 	mnt_drop_write_file(file);
1534f46b5a66SChristoph Hellwig 	return ret;
1535f46b5a66SChristoph Hellwig }
1536f46b5a66SChristoph Hellwig 
153772fd032eSSage Weil static noinline int btrfs_ioctl_snap_create_transid(struct file *file,
15386f72c7e2SArne Jansen 				char *name, unsigned long fd, int subvol,
15396f72c7e2SArne Jansen 				u64 *transid, bool readonly,
15408696c533SMiao Xie 				struct btrfs_qgroup_inherit *inherit)
1541f46b5a66SChristoph Hellwig {
1542f46b5a66SChristoph Hellwig 	int namelen;
15433de4586cSChris Mason 	int ret = 0;
1544f46b5a66SChristoph Hellwig 
1545a874a63eSLiu Bo 	ret = mnt_want_write_file(file);
1546a874a63eSLiu Bo 	if (ret)
1547a874a63eSLiu Bo 		goto out;
1548a874a63eSLiu Bo 
154972fd032eSSage Weil 	namelen = strlen(name);
155072fd032eSSage Weil 	if (strchr(name, '/')) {
1551f46b5a66SChristoph Hellwig 		ret = -EINVAL;
1552a874a63eSLiu Bo 		goto out_drop_write;
1553f46b5a66SChristoph Hellwig 	}
1554f46b5a66SChristoph Hellwig 
155516780cabSChris Mason 	if (name[0] == '.' &&
155616780cabSChris Mason 	   (namelen == 1 || (name[1] == '.' && namelen == 2))) {
155716780cabSChris Mason 		ret = -EEXIST;
1558a874a63eSLiu Bo 		goto out_drop_write;
155916780cabSChris Mason 	}
156016780cabSChris Mason 
15613de4586cSChris Mason 	if (subvol) {
156272fd032eSSage Weil 		ret = btrfs_mksubvol(&file->f_path, name, namelen,
15636f72c7e2SArne Jansen 				     NULL, transid, readonly, inherit);
1564cb8e7090SChristoph Hellwig 	} else {
15652903ff01SAl Viro 		struct fd src = fdget(fd);
15663de4586cSChris Mason 		struct inode *src_inode;
15672903ff01SAl Viro 		if (!src.file) {
15683de4586cSChris Mason 			ret = -EINVAL;
1569a874a63eSLiu Bo 			goto out_drop_write;
15703de4586cSChris Mason 		}
15713de4586cSChris Mason 
1572496ad9aaSAl Viro 		src_inode = file_inode(src.file);
1573496ad9aaSAl Viro 		if (src_inode->i_sb != file_inode(file)->i_sb) {
1574efe120a0SFrank Holton 			btrfs_info(BTRFS_I(src_inode)->root->fs_info,
1575efe120a0SFrank Holton 				   "Snapshot src from another FS");
15763de4586cSChris Mason 			ret = -EINVAL;
1577d0242061SDavid Sterba 		} else if (!inode_owner_or_capable(src_inode)) {
1578d0242061SDavid Sterba 			/*
1579d0242061SDavid Sterba 			 * Subvolume creation is not restricted, but snapshots
1580d0242061SDavid Sterba 			 * are limited to own subvolumes only
1581d0242061SDavid Sterba 			 */
1582d0242061SDavid Sterba 			ret = -EPERM;
1583ecd18815SAl Viro 		} else {
158472fd032eSSage Weil 			ret = btrfs_mksubvol(&file->f_path, name, namelen,
158572fd032eSSage Weil 					     BTRFS_I(src_inode)->root,
15866f72c7e2SArne Jansen 					     transid, readonly, inherit);
1587ecd18815SAl Viro 		}
15882903ff01SAl Viro 		fdput(src);
1589cb8e7090SChristoph Hellwig 	}
1590a874a63eSLiu Bo out_drop_write:
1591a874a63eSLiu Bo 	mnt_drop_write_file(file);
1592f46b5a66SChristoph Hellwig out:
159372fd032eSSage Weil 	return ret;
159472fd032eSSage Weil }
159572fd032eSSage Weil 
159672fd032eSSage Weil static noinline int btrfs_ioctl_snap_create(struct file *file,
1597fa0d2b9bSLi Zefan 					    void __user *arg, int subvol)
159872fd032eSSage Weil {
1599fa0d2b9bSLi Zefan 	struct btrfs_ioctl_vol_args *vol_args;
160072fd032eSSage Weil 	int ret;
160172fd032eSSage Weil 
1602fa0d2b9bSLi Zefan 	vol_args = memdup_user(arg, sizeof(*vol_args));
1603fa0d2b9bSLi Zefan 	if (IS_ERR(vol_args))
1604fa0d2b9bSLi Zefan 		return PTR_ERR(vol_args);
1605fa0d2b9bSLi Zefan 	vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
1606fa0d2b9bSLi Zefan 
1607fa0d2b9bSLi Zefan 	ret = btrfs_ioctl_snap_create_transid(file, vol_args->name,
1608b83cc969SLi Zefan 					      vol_args->fd, subvol,
16096f72c7e2SArne Jansen 					      NULL, false, NULL);
1610fa0d2b9bSLi Zefan 
1611fa0d2b9bSLi Zefan 	kfree(vol_args);
1612fa0d2b9bSLi Zefan 	return ret;
1613fa0d2b9bSLi Zefan }
1614fa0d2b9bSLi Zefan 
1615fa0d2b9bSLi Zefan static noinline int btrfs_ioctl_snap_create_v2(struct file *file,
1616fa0d2b9bSLi Zefan 					       void __user *arg, int subvol)
1617fa0d2b9bSLi Zefan {
1618fa0d2b9bSLi Zefan 	struct btrfs_ioctl_vol_args_v2 *vol_args;
1619fa0d2b9bSLi Zefan 	int ret;
1620fdfb1e4fSLi Zefan 	u64 transid = 0;
1621fdfb1e4fSLi Zefan 	u64 *ptr = NULL;
1622b83cc969SLi Zefan 	bool readonly = false;
16236f72c7e2SArne Jansen 	struct btrfs_qgroup_inherit *inherit = NULL;
162472fd032eSSage Weil 
1625fa0d2b9bSLi Zefan 	vol_args = memdup_user(arg, sizeof(*vol_args));
1626fa0d2b9bSLi Zefan 	if (IS_ERR(vol_args))
1627fa0d2b9bSLi Zefan 		return PTR_ERR(vol_args);
1628fa0d2b9bSLi Zefan 	vol_args->name[BTRFS_SUBVOL_NAME_MAX] = '\0';
1629fdfb1e4fSLi Zefan 
1630b83cc969SLi Zefan 	if (vol_args->flags &
16316f72c7e2SArne Jansen 	    ~(BTRFS_SUBVOL_CREATE_ASYNC | BTRFS_SUBVOL_RDONLY |
16326f72c7e2SArne Jansen 	      BTRFS_SUBVOL_QGROUP_INHERIT)) {
1633b83cc969SLi Zefan 		ret = -EOPNOTSUPP;
1634fdfb1e4fSLi Zefan 		goto out;
1635fdfb1e4fSLi Zefan 	}
1636fdfb1e4fSLi Zefan 
1637fa0d2b9bSLi Zefan 	if (vol_args->flags & BTRFS_SUBVOL_CREATE_ASYNC)
1638fdfb1e4fSLi Zefan 		ptr = &transid;
1639b83cc969SLi Zefan 	if (vol_args->flags & BTRFS_SUBVOL_RDONLY)
1640b83cc969SLi Zefan 		readonly = true;
16416f72c7e2SArne Jansen 	if (vol_args->flags & BTRFS_SUBVOL_QGROUP_INHERIT) {
16426f72c7e2SArne Jansen 		if (vol_args->size > PAGE_CACHE_SIZE) {
16436f72c7e2SArne Jansen 			ret = -EINVAL;
16446f72c7e2SArne Jansen 			goto out;
16456f72c7e2SArne Jansen 		}
16466f72c7e2SArne Jansen 		inherit = memdup_user(vol_args->qgroup_inherit, vol_args->size);
16476f72c7e2SArne Jansen 		if (IS_ERR(inherit)) {
16486f72c7e2SArne Jansen 			ret = PTR_ERR(inherit);
16496f72c7e2SArne Jansen 			goto out;
16506f72c7e2SArne Jansen 		}
16516f72c7e2SArne Jansen 	}
165275eaa0e2SSage Weil 
1653fa0d2b9bSLi Zefan 	ret = btrfs_ioctl_snap_create_transid(file, vol_args->name,
16546f72c7e2SArne Jansen 					      vol_args->fd, subvol, ptr,
16558696c533SMiao Xie 					      readonly, inherit);
165675eaa0e2SSage Weil 
1657fdfb1e4fSLi Zefan 	if (ret == 0 && ptr &&
165875eaa0e2SSage Weil 	    copy_to_user(arg +
1659fdfb1e4fSLi Zefan 			 offsetof(struct btrfs_ioctl_vol_args_v2,
1660fdfb1e4fSLi Zefan 				  transid), ptr, sizeof(*ptr)))
166175eaa0e2SSage Weil 		ret = -EFAULT;
1662fdfb1e4fSLi Zefan out:
1663f46b5a66SChristoph Hellwig 	kfree(vol_args);
16646f72c7e2SArne Jansen 	kfree(inherit);
1665f46b5a66SChristoph Hellwig 	return ret;
1666f46b5a66SChristoph Hellwig }
1667f46b5a66SChristoph Hellwig 
16680caa102dSLi Zefan static noinline int btrfs_ioctl_subvol_getflags(struct file *file,
16690caa102dSLi Zefan 						void __user *arg)
16700caa102dSLi Zefan {
1671496ad9aaSAl Viro 	struct inode *inode = file_inode(file);
16720caa102dSLi Zefan 	struct btrfs_root *root = BTRFS_I(inode)->root;
16730caa102dSLi Zefan 	int ret = 0;
16740caa102dSLi Zefan 	u64 flags = 0;
16750caa102dSLi Zefan 
167633345d01SLi Zefan 	if (btrfs_ino(inode) != BTRFS_FIRST_FREE_OBJECTID)
16770caa102dSLi Zefan 		return -EINVAL;
16780caa102dSLi Zefan 
16790caa102dSLi Zefan 	down_read(&root->fs_info->subvol_sem);
16800caa102dSLi Zefan 	if (btrfs_root_readonly(root))
16810caa102dSLi Zefan 		flags |= BTRFS_SUBVOL_RDONLY;
16820caa102dSLi Zefan 	up_read(&root->fs_info->subvol_sem);
16830caa102dSLi Zefan 
16840caa102dSLi Zefan 	if (copy_to_user(arg, &flags, sizeof(flags)))
16850caa102dSLi Zefan 		ret = -EFAULT;
16860caa102dSLi Zefan 
16870caa102dSLi Zefan 	return ret;
16880caa102dSLi Zefan }
16890caa102dSLi Zefan 
16900caa102dSLi Zefan static noinline int btrfs_ioctl_subvol_setflags(struct file *file,
16910caa102dSLi Zefan 					      void __user *arg)
16920caa102dSLi Zefan {
1693496ad9aaSAl Viro 	struct inode *inode = file_inode(file);
16940caa102dSLi Zefan 	struct btrfs_root *root = BTRFS_I(inode)->root;
16950caa102dSLi Zefan 	struct btrfs_trans_handle *trans;
16960caa102dSLi Zefan 	u64 root_flags;
16970caa102dSLi Zefan 	u64 flags;
16980caa102dSLi Zefan 	int ret = 0;
16990caa102dSLi Zefan 
1700bd60ea0fSDavid Sterba 	if (!inode_owner_or_capable(inode))
1701bd60ea0fSDavid Sterba 		return -EPERM;
1702bd60ea0fSDavid Sterba 
1703b9ca0664SLiu Bo 	ret = mnt_want_write_file(file);
1704b9ca0664SLiu Bo 	if (ret)
1705b9ca0664SLiu Bo 		goto out;
17060caa102dSLi Zefan 
1707b9ca0664SLiu Bo 	if (btrfs_ino(inode) != BTRFS_FIRST_FREE_OBJECTID) {
1708b9ca0664SLiu Bo 		ret = -EINVAL;
1709b9ca0664SLiu Bo 		goto out_drop_write;
1710b9ca0664SLiu Bo 	}
17110caa102dSLi Zefan 
1712b9ca0664SLiu Bo 	if (copy_from_user(&flags, arg, sizeof(flags))) {
1713b9ca0664SLiu Bo 		ret = -EFAULT;
1714b9ca0664SLiu Bo 		goto out_drop_write;
1715b9ca0664SLiu Bo 	}
17160caa102dSLi Zefan 
1717b9ca0664SLiu Bo 	if (flags & BTRFS_SUBVOL_CREATE_ASYNC) {
1718b9ca0664SLiu Bo 		ret = -EINVAL;
1719b9ca0664SLiu Bo 		goto out_drop_write;
1720b9ca0664SLiu Bo 	}
17210caa102dSLi Zefan 
1722b9ca0664SLiu Bo 	if (flags & ~BTRFS_SUBVOL_RDONLY) {
1723b9ca0664SLiu Bo 		ret = -EOPNOTSUPP;
1724b9ca0664SLiu Bo 		goto out_drop_write;
1725b9ca0664SLiu Bo 	}
17260caa102dSLi Zefan 
17270caa102dSLi Zefan 	down_write(&root->fs_info->subvol_sem);
17280caa102dSLi Zefan 
17290caa102dSLi Zefan 	/* nothing to do */
17300caa102dSLi Zefan 	if (!!(flags & BTRFS_SUBVOL_RDONLY) == btrfs_root_readonly(root))
1731b9ca0664SLiu Bo 		goto out_drop_sem;
17320caa102dSLi Zefan 
17330caa102dSLi Zefan 	root_flags = btrfs_root_flags(&root->root_item);
17342c686537SDavid Sterba 	if (flags & BTRFS_SUBVOL_RDONLY) {
17350caa102dSLi Zefan 		btrfs_set_root_flags(&root->root_item,
17360caa102dSLi Zefan 				     root_flags | BTRFS_ROOT_SUBVOL_RDONLY);
17372c686537SDavid Sterba 	} else {
17382c686537SDavid Sterba 		/*
17392c686537SDavid Sterba 		 * Block RO -> RW transition if this subvolume is involved in
17402c686537SDavid Sterba 		 * send
17412c686537SDavid Sterba 		 */
17422c686537SDavid Sterba 		spin_lock(&root->root_item_lock);
17432c686537SDavid Sterba 		if (root->send_in_progress == 0) {
17440caa102dSLi Zefan 			btrfs_set_root_flags(&root->root_item,
17450caa102dSLi Zefan 				     root_flags & ~BTRFS_ROOT_SUBVOL_RDONLY);
17462c686537SDavid Sterba 			spin_unlock(&root->root_item_lock);
17472c686537SDavid Sterba 		} else {
17482c686537SDavid Sterba 			spin_unlock(&root->root_item_lock);
17492c686537SDavid Sterba 			btrfs_warn(root->fs_info,
17502c686537SDavid Sterba 			"Attempt to set subvolume %llu read-write during send",
17512c686537SDavid Sterba 					root->root_key.objectid);
17522c686537SDavid Sterba 			ret = -EPERM;
17532c686537SDavid Sterba 			goto out_drop_sem;
17542c686537SDavid Sterba 		}
17552c686537SDavid Sterba 	}
17560caa102dSLi Zefan 
17570caa102dSLi Zefan 	trans = btrfs_start_transaction(root, 1);
17580caa102dSLi Zefan 	if (IS_ERR(trans)) {
17590caa102dSLi Zefan 		ret = PTR_ERR(trans);
17600caa102dSLi Zefan 		goto out_reset;
17610caa102dSLi Zefan 	}
17620caa102dSLi Zefan 
1763b4dc2b8cSLi Zefan 	ret = btrfs_update_root(trans, root->fs_info->tree_root,
17640caa102dSLi Zefan 				&root->root_key, &root->root_item);
17650caa102dSLi Zefan 
17660caa102dSLi Zefan 	btrfs_commit_transaction(trans, root);
17670caa102dSLi Zefan out_reset:
17680caa102dSLi Zefan 	if (ret)
17690caa102dSLi Zefan 		btrfs_set_root_flags(&root->root_item, root_flags);
1770b9ca0664SLiu Bo out_drop_sem:
17710caa102dSLi Zefan 	up_write(&root->fs_info->subvol_sem);
1772b9ca0664SLiu Bo out_drop_write:
1773b9ca0664SLiu Bo 	mnt_drop_write_file(file);
1774b9ca0664SLiu Bo out:
17750caa102dSLi Zefan 	return ret;
17760caa102dSLi Zefan }
17770caa102dSLi Zefan 
177876dda93cSYan, Zheng /*
177976dda93cSYan, Zheng  * helper to check if the subvolume references other subvolumes
178076dda93cSYan, Zheng  */
178176dda93cSYan, Zheng static noinline int may_destroy_subvol(struct btrfs_root *root)
178276dda93cSYan, Zheng {
178376dda93cSYan, Zheng 	struct btrfs_path *path;
1784175a2b87SJosef Bacik 	struct btrfs_dir_item *di;
178576dda93cSYan, Zheng 	struct btrfs_key key;
1786175a2b87SJosef Bacik 	u64 dir_id;
178776dda93cSYan, Zheng 	int ret;
178876dda93cSYan, Zheng 
178976dda93cSYan, Zheng 	path = btrfs_alloc_path();
179076dda93cSYan, Zheng 	if (!path)
179176dda93cSYan, Zheng 		return -ENOMEM;
179276dda93cSYan, Zheng 
1793175a2b87SJosef Bacik 	/* Make sure this root isn't set as the default subvol */
1794175a2b87SJosef Bacik 	dir_id = btrfs_super_root_dir(root->fs_info->super_copy);
1795175a2b87SJosef Bacik 	di = btrfs_lookup_dir_item(NULL, root->fs_info->tree_root, path,
1796175a2b87SJosef Bacik 				   dir_id, "default", 7, 0);
1797175a2b87SJosef Bacik 	if (di && !IS_ERR(di)) {
1798175a2b87SJosef Bacik 		btrfs_dir_item_key_to_cpu(path->nodes[0], di, &key);
1799175a2b87SJosef Bacik 		if (key.objectid == root->root_key.objectid) {
1800175a2b87SJosef Bacik 			ret = -ENOTEMPTY;
1801175a2b87SJosef Bacik 			goto out;
1802175a2b87SJosef Bacik 		}
1803175a2b87SJosef Bacik 		btrfs_release_path(path);
1804175a2b87SJosef Bacik 	}
1805175a2b87SJosef Bacik 
180676dda93cSYan, Zheng 	key.objectid = root->root_key.objectid;
180776dda93cSYan, Zheng 	key.type = BTRFS_ROOT_REF_KEY;
180876dda93cSYan, Zheng 	key.offset = (u64)-1;
180976dda93cSYan, Zheng 
181076dda93cSYan, Zheng 	ret = btrfs_search_slot(NULL, root->fs_info->tree_root,
181176dda93cSYan, Zheng 				&key, path, 0, 0);
181276dda93cSYan, Zheng 	if (ret < 0)
181376dda93cSYan, Zheng 		goto out;
181476dda93cSYan, Zheng 	BUG_ON(ret == 0);
181576dda93cSYan, Zheng 
181676dda93cSYan, Zheng 	ret = 0;
181776dda93cSYan, Zheng 	if (path->slots[0] > 0) {
181876dda93cSYan, Zheng 		path->slots[0]--;
181976dda93cSYan, Zheng 		btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
182076dda93cSYan, Zheng 		if (key.objectid == root->root_key.objectid &&
182176dda93cSYan, Zheng 		    key.type == BTRFS_ROOT_REF_KEY)
182276dda93cSYan, Zheng 			ret = -ENOTEMPTY;
182376dda93cSYan, Zheng 	}
182476dda93cSYan, Zheng out:
182576dda93cSYan, Zheng 	btrfs_free_path(path);
182676dda93cSYan, Zheng 	return ret;
182776dda93cSYan, Zheng }
182876dda93cSYan, Zheng 
1829ac8e9819SChris Mason static noinline int key_in_sk(struct btrfs_key *key,
1830ac8e9819SChris Mason 			      struct btrfs_ioctl_search_key *sk)
1831ac8e9819SChris Mason {
1832abc6e134SChris Mason 	struct btrfs_key test;
1833abc6e134SChris Mason 	int ret;
1834abc6e134SChris Mason 
1835abc6e134SChris Mason 	test.objectid = sk->min_objectid;
1836abc6e134SChris Mason 	test.type = sk->min_type;
1837abc6e134SChris Mason 	test.offset = sk->min_offset;
1838abc6e134SChris Mason 
1839abc6e134SChris Mason 	ret = btrfs_comp_cpu_keys(key, &test);
1840abc6e134SChris Mason 	if (ret < 0)
1841ac8e9819SChris Mason 		return 0;
1842abc6e134SChris Mason 
1843abc6e134SChris Mason 	test.objectid = sk->max_objectid;
1844abc6e134SChris Mason 	test.type = sk->max_type;
1845abc6e134SChris Mason 	test.offset = sk->max_offset;
1846abc6e134SChris Mason 
1847abc6e134SChris Mason 	ret = btrfs_comp_cpu_keys(key, &test);
1848abc6e134SChris Mason 	if (ret > 0)
1849ac8e9819SChris Mason 		return 0;
1850ac8e9819SChris Mason 	return 1;
1851ac8e9819SChris Mason }
1852ac8e9819SChris Mason 
1853ac8e9819SChris Mason static noinline int copy_to_sk(struct btrfs_root *root,
1854ac8e9819SChris Mason 			       struct btrfs_path *path,
1855ac8e9819SChris Mason 			       struct btrfs_key *key,
1856ac8e9819SChris Mason 			       struct btrfs_ioctl_search_key *sk,
1857ac8e9819SChris Mason 			       char *buf,
1858ac8e9819SChris Mason 			       unsigned long *sk_offset,
1859ac8e9819SChris Mason 			       int *num_found)
1860ac8e9819SChris Mason {
1861ac8e9819SChris Mason 	u64 found_transid;
1862ac8e9819SChris Mason 	struct extent_buffer *leaf;
1863ac8e9819SChris Mason 	struct btrfs_ioctl_search_header sh;
1864ac8e9819SChris Mason 	unsigned long item_off;
1865ac8e9819SChris Mason 	unsigned long item_len;
1866ac8e9819SChris Mason 	int nritems;
1867ac8e9819SChris Mason 	int i;
1868ac8e9819SChris Mason 	int slot;
1869ac8e9819SChris Mason 	int ret = 0;
1870ac8e9819SChris Mason 
1871ac8e9819SChris Mason 	leaf = path->nodes[0];
1872ac8e9819SChris Mason 	slot = path->slots[0];
1873ac8e9819SChris Mason 	nritems = btrfs_header_nritems(leaf);
1874ac8e9819SChris Mason 
1875ac8e9819SChris Mason 	if (btrfs_header_generation(leaf) > sk->max_transid) {
1876ac8e9819SChris Mason 		i = nritems;
1877ac8e9819SChris Mason 		goto advance_key;
1878ac8e9819SChris Mason 	}
1879ac8e9819SChris Mason 	found_transid = btrfs_header_generation(leaf);
1880ac8e9819SChris Mason 
1881ac8e9819SChris Mason 	for (i = slot; i < nritems; i++) {
1882ac8e9819SChris Mason 		item_off = btrfs_item_ptr_offset(leaf, i);
1883ac8e9819SChris Mason 		item_len = btrfs_item_size_nr(leaf, i);
1884ac8e9819SChris Mason 
188503b71c6cSGabriel de Perthuis 		btrfs_item_key_to_cpu(leaf, key, i);
188603b71c6cSGabriel de Perthuis 		if (!key_in_sk(key, sk))
188703b71c6cSGabriel de Perthuis 			continue;
188803b71c6cSGabriel de Perthuis 
188903b71c6cSGabriel de Perthuis 		if (sizeof(sh) + item_len > BTRFS_SEARCH_ARGS_BUFSIZE)
1890ac8e9819SChris Mason 			item_len = 0;
1891ac8e9819SChris Mason 
1892ac8e9819SChris Mason 		if (sizeof(sh) + item_len + *sk_offset >
1893ac8e9819SChris Mason 		    BTRFS_SEARCH_ARGS_BUFSIZE) {
1894ac8e9819SChris Mason 			ret = 1;
1895ac8e9819SChris Mason 			goto overflow;
1896ac8e9819SChris Mason 		}
1897ac8e9819SChris Mason 
1898ac8e9819SChris Mason 		sh.objectid = key->objectid;
1899ac8e9819SChris Mason 		sh.offset = key->offset;
1900ac8e9819SChris Mason 		sh.type = key->type;
1901ac8e9819SChris Mason 		sh.len = item_len;
1902ac8e9819SChris Mason 		sh.transid = found_transid;
1903ac8e9819SChris Mason 
1904ac8e9819SChris Mason 		/* copy search result header */
1905ac8e9819SChris Mason 		memcpy(buf + *sk_offset, &sh, sizeof(sh));
1906ac8e9819SChris Mason 		*sk_offset += sizeof(sh);
1907ac8e9819SChris Mason 
1908ac8e9819SChris Mason 		if (item_len) {
1909ac8e9819SChris Mason 			char *p = buf + *sk_offset;
1910ac8e9819SChris Mason 			/* copy the item */
1911ac8e9819SChris Mason 			read_extent_buffer(leaf, p,
1912ac8e9819SChris Mason 					   item_off, item_len);
1913ac8e9819SChris Mason 			*sk_offset += item_len;
1914ac8e9819SChris Mason 		}
1915e2156867SHugo Mills 		(*num_found)++;
1916ac8e9819SChris Mason 
1917ac8e9819SChris Mason 		if (*num_found >= sk->nr_items)
1918ac8e9819SChris Mason 			break;
1919ac8e9819SChris Mason 	}
1920ac8e9819SChris Mason advance_key:
1921ac8e9819SChris Mason 	ret = 0;
1922abc6e134SChris Mason 	if (key->offset < (u64)-1 && key->offset < sk->max_offset)
1923abc6e134SChris Mason 		key->offset++;
1924abc6e134SChris Mason 	else if (key->type < (u8)-1 && key->type < sk->max_type) {
1925abc6e134SChris Mason 		key->offset = 0;
1926abc6e134SChris Mason 		key->type++;
1927abc6e134SChris Mason 	} else if (key->objectid < (u64)-1 && key->objectid < sk->max_objectid) {
1928abc6e134SChris Mason 		key->offset = 0;
1929abc6e134SChris Mason 		key->type = 0;
1930abc6e134SChris Mason 		key->objectid++;
1931abc6e134SChris Mason 	} else
1932abc6e134SChris Mason 		ret = 1;
1933ac8e9819SChris Mason overflow:
1934ac8e9819SChris Mason 	return ret;
1935ac8e9819SChris Mason }
1936ac8e9819SChris Mason 
1937ac8e9819SChris Mason static noinline int search_ioctl(struct inode *inode,
1938ac8e9819SChris Mason 				 struct btrfs_ioctl_search_args *args)
1939ac8e9819SChris Mason {
1940ac8e9819SChris Mason 	struct btrfs_root *root;
1941ac8e9819SChris Mason 	struct btrfs_key key;
1942ac8e9819SChris Mason 	struct btrfs_path *path;
1943ac8e9819SChris Mason 	struct btrfs_ioctl_search_key *sk = &args->key;
1944ac8e9819SChris Mason 	struct btrfs_fs_info *info = BTRFS_I(inode)->root->fs_info;
1945ac8e9819SChris Mason 	int ret;
1946ac8e9819SChris Mason 	int num_found = 0;
1947ac8e9819SChris Mason 	unsigned long sk_offset = 0;
1948ac8e9819SChris Mason 
1949ac8e9819SChris Mason 	path = btrfs_alloc_path();
1950ac8e9819SChris Mason 	if (!path)
1951ac8e9819SChris Mason 		return -ENOMEM;
1952ac8e9819SChris Mason 
1953ac8e9819SChris Mason 	if (sk->tree_id == 0) {
1954ac8e9819SChris Mason 		/* search the root of the inode that was passed */
1955ac8e9819SChris Mason 		root = BTRFS_I(inode)->root;
1956ac8e9819SChris Mason 	} else {
1957ac8e9819SChris Mason 		key.objectid = sk->tree_id;
1958ac8e9819SChris Mason 		key.type = BTRFS_ROOT_ITEM_KEY;
1959ac8e9819SChris Mason 		key.offset = (u64)-1;
1960ac8e9819SChris Mason 		root = btrfs_read_fs_root_no_name(info, &key);
1961ac8e9819SChris Mason 		if (IS_ERR(root)) {
1962efe120a0SFrank Holton 			printk(KERN_ERR "BTRFS: could not find root %llu\n",
1963ac8e9819SChris Mason 			       sk->tree_id);
1964ac8e9819SChris Mason 			btrfs_free_path(path);
1965ac8e9819SChris Mason 			return -ENOENT;
1966ac8e9819SChris Mason 		}
1967ac8e9819SChris Mason 	}
1968ac8e9819SChris Mason 
1969ac8e9819SChris Mason 	key.objectid = sk->min_objectid;
1970ac8e9819SChris Mason 	key.type = sk->min_type;
1971ac8e9819SChris Mason 	key.offset = sk->min_offset;
1972ac8e9819SChris Mason 
1973ac8e9819SChris Mason 	path->keep_locks = 1;
1974ac8e9819SChris Mason 
1975ac8e9819SChris Mason 	while (1) {
19766174d3cbSFilipe David Borba Manana 		ret = btrfs_search_forward(root, &key, path, sk->min_transid);
1977ac8e9819SChris Mason 		if (ret != 0) {
1978ac8e9819SChris Mason 			if (ret > 0)
1979ac8e9819SChris Mason 				ret = 0;
1980ac8e9819SChris Mason 			goto err;
1981ac8e9819SChris Mason 		}
1982ac8e9819SChris Mason 		ret = copy_to_sk(root, path, &key, sk, args->buf,
1983ac8e9819SChris Mason 				 &sk_offset, &num_found);
1984b3b4aa74SDavid Sterba 		btrfs_release_path(path);
1985ac8e9819SChris Mason 		if (ret || num_found >= sk->nr_items)
1986ac8e9819SChris Mason 			break;
1987ac8e9819SChris Mason 
1988ac8e9819SChris Mason 	}
1989ac8e9819SChris Mason 	ret = 0;
1990ac8e9819SChris Mason err:
1991ac8e9819SChris Mason 	sk->nr_items = num_found;
1992ac8e9819SChris Mason 	btrfs_free_path(path);
1993ac8e9819SChris Mason 	return ret;
1994ac8e9819SChris Mason }
1995ac8e9819SChris Mason 
1996ac8e9819SChris Mason static noinline int btrfs_ioctl_tree_search(struct file *file,
1997ac8e9819SChris Mason 					   void __user *argp)
1998ac8e9819SChris Mason {
1999ac8e9819SChris Mason 	 struct btrfs_ioctl_search_args *args;
2000ac8e9819SChris Mason 	 struct inode *inode;
2001ac8e9819SChris Mason 	 int ret;
2002ac8e9819SChris Mason 
2003ac8e9819SChris Mason 	if (!capable(CAP_SYS_ADMIN))
2004ac8e9819SChris Mason 		return -EPERM;
2005ac8e9819SChris Mason 
20062354d08fSJulia Lawall 	args = memdup_user(argp, sizeof(*args));
20072354d08fSJulia Lawall 	if (IS_ERR(args))
20082354d08fSJulia Lawall 		return PTR_ERR(args);
2009ac8e9819SChris Mason 
2010496ad9aaSAl Viro 	inode = file_inode(file);
2011ac8e9819SChris Mason 	ret = search_ioctl(inode, args);
2012ac8e9819SChris Mason 	if (ret == 0 && copy_to_user(argp, args, sizeof(*args)))
2013ac8e9819SChris Mason 		ret = -EFAULT;
2014ac8e9819SChris Mason 	kfree(args);
2015ac8e9819SChris Mason 	return ret;
2016ac8e9819SChris Mason }
2017ac8e9819SChris Mason 
201898d377a0STARUISI Hiroaki /*
2019ac8e9819SChris Mason  * Search INODE_REFs to identify path name of 'dirid' directory
2020ac8e9819SChris Mason  * in a 'tree_id' tree. and sets path name to 'name'.
202198d377a0STARUISI Hiroaki  */
202298d377a0STARUISI Hiroaki static noinline int btrfs_search_path_in_tree(struct btrfs_fs_info *info,
202398d377a0STARUISI Hiroaki 				u64 tree_id, u64 dirid, char *name)
202498d377a0STARUISI Hiroaki {
202598d377a0STARUISI Hiroaki 	struct btrfs_root *root;
202698d377a0STARUISI Hiroaki 	struct btrfs_key key;
2027ac8e9819SChris Mason 	char *ptr;
202898d377a0STARUISI Hiroaki 	int ret = -1;
202998d377a0STARUISI Hiroaki 	int slot;
203098d377a0STARUISI Hiroaki 	int len;
203198d377a0STARUISI Hiroaki 	int total_len = 0;
203298d377a0STARUISI Hiroaki 	struct btrfs_inode_ref *iref;
203398d377a0STARUISI Hiroaki 	struct extent_buffer *l;
203498d377a0STARUISI Hiroaki 	struct btrfs_path *path;
203598d377a0STARUISI Hiroaki 
203698d377a0STARUISI Hiroaki 	if (dirid == BTRFS_FIRST_FREE_OBJECTID) {
203798d377a0STARUISI Hiroaki 		name[0]='\0';
203898d377a0STARUISI Hiroaki 		return 0;
203998d377a0STARUISI Hiroaki 	}
204098d377a0STARUISI Hiroaki 
204198d377a0STARUISI Hiroaki 	path = btrfs_alloc_path();
204298d377a0STARUISI Hiroaki 	if (!path)
204398d377a0STARUISI Hiroaki 		return -ENOMEM;
204498d377a0STARUISI Hiroaki 
2045ac8e9819SChris Mason 	ptr = &name[BTRFS_INO_LOOKUP_PATH_MAX];
204698d377a0STARUISI Hiroaki 
204798d377a0STARUISI Hiroaki 	key.objectid = tree_id;
204898d377a0STARUISI Hiroaki 	key.type = BTRFS_ROOT_ITEM_KEY;
204998d377a0STARUISI Hiroaki 	key.offset = (u64)-1;
205098d377a0STARUISI Hiroaki 	root = btrfs_read_fs_root_no_name(info, &key);
205198d377a0STARUISI Hiroaki 	if (IS_ERR(root)) {
2052efe120a0SFrank Holton 		printk(KERN_ERR "BTRFS: could not find root %llu\n", tree_id);
20538ad6fcabSChris Mason 		ret = -ENOENT;
20548ad6fcabSChris Mason 		goto out;
205598d377a0STARUISI Hiroaki 	}
205698d377a0STARUISI Hiroaki 
205798d377a0STARUISI Hiroaki 	key.objectid = dirid;
205898d377a0STARUISI Hiroaki 	key.type = BTRFS_INODE_REF_KEY;
20598ad6fcabSChris Mason 	key.offset = (u64)-1;
206098d377a0STARUISI Hiroaki 
206198d377a0STARUISI Hiroaki 	while (1) {
206298d377a0STARUISI Hiroaki 		ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
206398d377a0STARUISI Hiroaki 		if (ret < 0)
206498d377a0STARUISI Hiroaki 			goto out;
206518674c6cSFilipe David Borba Manana 		else if (ret > 0) {
206618674c6cSFilipe David Borba Manana 			ret = btrfs_previous_item(root, path, dirid,
206718674c6cSFilipe David Borba Manana 						  BTRFS_INODE_REF_KEY);
206818674c6cSFilipe David Borba Manana 			if (ret < 0)
206918674c6cSFilipe David Borba Manana 				goto out;
207018674c6cSFilipe David Borba Manana 			else if (ret > 0) {
2071ac8e9819SChris Mason 				ret = -ENOENT;
207298d377a0STARUISI Hiroaki 				goto out;
2073ac8e9819SChris Mason 			}
207418674c6cSFilipe David Borba Manana 		}
207518674c6cSFilipe David Borba Manana 
207618674c6cSFilipe David Borba Manana 		l = path->nodes[0];
207718674c6cSFilipe David Borba Manana 		slot = path->slots[0];
207818674c6cSFilipe David Borba Manana 		btrfs_item_key_to_cpu(l, &key, slot);
207998d377a0STARUISI Hiroaki 
208098d377a0STARUISI Hiroaki 		iref = btrfs_item_ptr(l, slot, struct btrfs_inode_ref);
208198d377a0STARUISI Hiroaki 		len = btrfs_inode_ref_name_len(l, iref);
208298d377a0STARUISI Hiroaki 		ptr -= len + 1;
208398d377a0STARUISI Hiroaki 		total_len += len + 1;
2084a696cf35SFilipe David Borba Manana 		if (ptr < name) {
2085a696cf35SFilipe David Borba Manana 			ret = -ENAMETOOLONG;
208698d377a0STARUISI Hiroaki 			goto out;
2087a696cf35SFilipe David Borba Manana 		}
208898d377a0STARUISI Hiroaki 
208998d377a0STARUISI Hiroaki 		*(ptr + len) = '/';
209098d377a0STARUISI Hiroaki 		read_extent_buffer(l, ptr, (unsigned long)(iref + 1), len);
209198d377a0STARUISI Hiroaki 
209298d377a0STARUISI Hiroaki 		if (key.offset == BTRFS_FIRST_FREE_OBJECTID)
209398d377a0STARUISI Hiroaki 			break;
209498d377a0STARUISI Hiroaki 
2095b3b4aa74SDavid Sterba 		btrfs_release_path(path);
209698d377a0STARUISI Hiroaki 		key.objectid = key.offset;
20978ad6fcabSChris Mason 		key.offset = (u64)-1;
209898d377a0STARUISI Hiroaki 		dirid = key.objectid;
209998d377a0STARUISI Hiroaki 	}
210077906a50SLi Zefan 	memmove(name, ptr, total_len);
210198d377a0STARUISI Hiroaki 	name[total_len] = '\0';
210298d377a0STARUISI Hiroaki 	ret = 0;
210398d377a0STARUISI Hiroaki out:
210498d377a0STARUISI Hiroaki 	btrfs_free_path(path);
2105ac8e9819SChris Mason 	return ret;
2106ac8e9819SChris Mason }
2107ac8e9819SChris Mason 
2108ac8e9819SChris Mason static noinline int btrfs_ioctl_ino_lookup(struct file *file,
2109ac8e9819SChris Mason 					   void __user *argp)
2110ac8e9819SChris Mason {
2111ac8e9819SChris Mason 	 struct btrfs_ioctl_ino_lookup_args *args;
2112ac8e9819SChris Mason 	 struct inode *inode;
2113ac8e9819SChris Mason 	 int ret;
2114ac8e9819SChris Mason 
2115ac8e9819SChris Mason 	if (!capable(CAP_SYS_ADMIN))
2116ac8e9819SChris Mason 		return -EPERM;
2117ac8e9819SChris Mason 
21182354d08fSJulia Lawall 	args = memdup_user(argp, sizeof(*args));
21192354d08fSJulia Lawall 	if (IS_ERR(args))
21202354d08fSJulia Lawall 		return PTR_ERR(args);
2121c2b96929SDan Carpenter 
2122496ad9aaSAl Viro 	inode = file_inode(file);
2123ac8e9819SChris Mason 
21241b53ac4dSChris Mason 	if (args->treeid == 0)
21251b53ac4dSChris Mason 		args->treeid = BTRFS_I(inode)->root->root_key.objectid;
21261b53ac4dSChris Mason 
2127ac8e9819SChris Mason 	ret = btrfs_search_path_in_tree(BTRFS_I(inode)->root->fs_info,
2128ac8e9819SChris Mason 					args->treeid, args->objectid,
2129ac8e9819SChris Mason 					args->name);
2130ac8e9819SChris Mason 
2131ac8e9819SChris Mason 	if (ret == 0 && copy_to_user(argp, args, sizeof(*args)))
2132ac8e9819SChris Mason 		ret = -EFAULT;
2133ac8e9819SChris Mason 
2134ac8e9819SChris Mason 	kfree(args);
213598d377a0STARUISI Hiroaki 	return ret;
213698d377a0STARUISI Hiroaki }
213798d377a0STARUISI Hiroaki 
213876dda93cSYan, Zheng static noinline int btrfs_ioctl_snap_destroy(struct file *file,
213976dda93cSYan, Zheng 					     void __user *arg)
214076dda93cSYan, Zheng {
214154563d41SAl Viro 	struct dentry *parent = file->f_path.dentry;
214276dda93cSYan, Zheng 	struct dentry *dentry;
214376dda93cSYan, Zheng 	struct inode *dir = parent->d_inode;
214476dda93cSYan, Zheng 	struct inode *inode;
214576dda93cSYan, Zheng 	struct btrfs_root *root = BTRFS_I(dir)->root;
214676dda93cSYan, Zheng 	struct btrfs_root *dest = NULL;
214776dda93cSYan, Zheng 	struct btrfs_ioctl_vol_args *vol_args;
214876dda93cSYan, Zheng 	struct btrfs_trans_handle *trans;
2149c58aaad2SMiao Xie 	struct btrfs_block_rsv block_rsv;
2150c58aaad2SMiao Xie 	u64 qgroup_reserved;
215176dda93cSYan, Zheng 	int namelen;
215276dda93cSYan, Zheng 	int ret;
215376dda93cSYan, Zheng 	int err = 0;
215476dda93cSYan, Zheng 
215576dda93cSYan, Zheng 	vol_args = memdup_user(arg, sizeof(*vol_args));
215676dda93cSYan, Zheng 	if (IS_ERR(vol_args))
215776dda93cSYan, Zheng 		return PTR_ERR(vol_args);
215876dda93cSYan, Zheng 
215976dda93cSYan, Zheng 	vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
216076dda93cSYan, Zheng 	namelen = strlen(vol_args->name);
216176dda93cSYan, Zheng 	if (strchr(vol_args->name, '/') ||
216276dda93cSYan, Zheng 	    strncmp(vol_args->name, "..", namelen) == 0) {
216376dda93cSYan, Zheng 		err = -EINVAL;
216476dda93cSYan, Zheng 		goto out;
216576dda93cSYan, Zheng 	}
216676dda93cSYan, Zheng 
2167a561be71SAl Viro 	err = mnt_want_write_file(file);
216876dda93cSYan, Zheng 	if (err)
216976dda93cSYan, Zheng 		goto out;
217076dda93cSYan, Zheng 
21715c50c9b8SDavid Sterba 	err = mutex_lock_killable_nested(&dir->i_mutex, I_MUTEX_PARENT);
21725c50c9b8SDavid Sterba 	if (err == -EINTR)
2173e43f998eSDavid Sterba 		goto out_drop_write;
217476dda93cSYan, Zheng 	dentry = lookup_one_len(vol_args->name, parent, namelen);
217576dda93cSYan, Zheng 	if (IS_ERR(dentry)) {
217676dda93cSYan, Zheng 		err = PTR_ERR(dentry);
217776dda93cSYan, Zheng 		goto out_unlock_dir;
217876dda93cSYan, Zheng 	}
217976dda93cSYan, Zheng 
218076dda93cSYan, Zheng 	if (!dentry->d_inode) {
218176dda93cSYan, Zheng 		err = -ENOENT;
218276dda93cSYan, Zheng 		goto out_dput;
218376dda93cSYan, Zheng 	}
218476dda93cSYan, Zheng 
218576dda93cSYan, Zheng 	inode = dentry->d_inode;
21864260f7c7SSage Weil 	dest = BTRFS_I(inode)->root;
21874260f7c7SSage Weil 	if (!capable(CAP_SYS_ADMIN)) {
21884260f7c7SSage Weil 		/*
21894260f7c7SSage Weil 		 * Regular user.  Only allow this with a special mount
21904260f7c7SSage Weil 		 * option, when the user has write+exec access to the
21914260f7c7SSage Weil 		 * subvol root, and when rmdir(2) would have been
21924260f7c7SSage Weil 		 * allowed.
21934260f7c7SSage Weil 		 *
21944260f7c7SSage Weil 		 * Note that this is _not_ check that the subvol is
21954260f7c7SSage Weil 		 * empty or doesn't contain data that we wouldn't
21964260f7c7SSage Weil 		 * otherwise be able to delete.
21974260f7c7SSage Weil 		 *
21984260f7c7SSage Weil 		 * Users who want to delete empty subvols should try
21994260f7c7SSage Weil 		 * rmdir(2).
22004260f7c7SSage Weil 		 */
22014260f7c7SSage Weil 		err = -EPERM;
22024260f7c7SSage Weil 		if (!btrfs_test_opt(root, USER_SUBVOL_RM_ALLOWED))
22034260f7c7SSage Weil 			goto out_dput;
22044260f7c7SSage Weil 
22054260f7c7SSage Weil 		/*
22064260f7c7SSage Weil 		 * Do not allow deletion if the parent dir is the same
22074260f7c7SSage Weil 		 * as the dir to be deleted.  That means the ioctl
22084260f7c7SSage Weil 		 * must be called on the dentry referencing the root
22094260f7c7SSage Weil 		 * of the subvol, not a random directory contained
22104260f7c7SSage Weil 		 * within it.
22114260f7c7SSage Weil 		 */
22124260f7c7SSage Weil 		err = -EINVAL;
22134260f7c7SSage Weil 		if (root == dest)
22144260f7c7SSage Weil 			goto out_dput;
22154260f7c7SSage Weil 
22164260f7c7SSage Weil 		err = inode_permission(inode, MAY_WRITE | MAY_EXEC);
22174260f7c7SSage Weil 		if (err)
22184260f7c7SSage Weil 			goto out_dput;
22195c39da5bSMiao Xie 	}
22204260f7c7SSage Weil 
22215c39da5bSMiao Xie 	/* check if subvolume may be deleted by a user */
22224260f7c7SSage Weil 	err = btrfs_may_delete(dir, dentry, 1);
22234260f7c7SSage Weil 	if (err)
22244260f7c7SSage Weil 		goto out_dput;
22254260f7c7SSage Weil 
222633345d01SLi Zefan 	if (btrfs_ino(inode) != BTRFS_FIRST_FREE_OBJECTID) {
222776dda93cSYan, Zheng 		err = -EINVAL;
222876dda93cSYan, Zheng 		goto out_dput;
222976dda93cSYan, Zheng 	}
223076dda93cSYan, Zheng 
223176dda93cSYan, Zheng 	mutex_lock(&inode->i_mutex);
223276dda93cSYan, Zheng 	err = d_invalidate(dentry);
223376dda93cSYan, Zheng 	if (err)
223476dda93cSYan, Zheng 		goto out_unlock;
223576dda93cSYan, Zheng 
223676dda93cSYan, Zheng 	down_write(&root->fs_info->subvol_sem);
223776dda93cSYan, Zheng 
223876dda93cSYan, Zheng 	err = may_destroy_subvol(dest);
223976dda93cSYan, Zheng 	if (err)
224076dda93cSYan, Zheng 		goto out_up_write;
224176dda93cSYan, Zheng 
2242c58aaad2SMiao Xie 	btrfs_init_block_rsv(&block_rsv, BTRFS_BLOCK_RSV_TEMP);
2243c58aaad2SMiao Xie 	/*
2244c58aaad2SMiao Xie 	 * One for dir inode, two for dir entries, two for root
2245c58aaad2SMiao Xie 	 * ref/backref.
2246c58aaad2SMiao Xie 	 */
2247c58aaad2SMiao Xie 	err = btrfs_subvolume_reserve_metadata(root, &block_rsv,
2248ee3441b4SJeff Mahoney 					       5, &qgroup_reserved, true);
2249c58aaad2SMiao Xie 	if (err)
2250c58aaad2SMiao Xie 		goto out_up_write;
2251c58aaad2SMiao Xie 
2252a22285a6SYan, Zheng 	trans = btrfs_start_transaction(root, 0);
2253a22285a6SYan, Zheng 	if (IS_ERR(trans)) {
2254a22285a6SYan, Zheng 		err = PTR_ERR(trans);
2255c58aaad2SMiao Xie 		goto out_release;
2256a22285a6SYan, Zheng 	}
2257c58aaad2SMiao Xie 	trans->block_rsv = &block_rsv;
2258c58aaad2SMiao Xie 	trans->bytes_reserved = block_rsv.size;
2259a22285a6SYan, Zheng 
226076dda93cSYan, Zheng 	ret = btrfs_unlink_subvol(trans, root, dir,
226176dda93cSYan, Zheng 				dest->root_key.objectid,
226276dda93cSYan, Zheng 				dentry->d_name.name,
226376dda93cSYan, Zheng 				dentry->d_name.len);
226479787eaaSJeff Mahoney 	if (ret) {
226579787eaaSJeff Mahoney 		err = ret;
226679787eaaSJeff Mahoney 		btrfs_abort_transaction(trans, root, ret);
226779787eaaSJeff Mahoney 		goto out_end_trans;
226879787eaaSJeff Mahoney 	}
226976dda93cSYan, Zheng 
227076dda93cSYan, Zheng 	btrfs_record_root_in_trans(trans, dest);
227176dda93cSYan, Zheng 
227276dda93cSYan, Zheng 	memset(&dest->root_item.drop_progress, 0,
227376dda93cSYan, Zheng 		sizeof(dest->root_item.drop_progress));
227476dda93cSYan, Zheng 	dest->root_item.drop_level = 0;
227576dda93cSYan, Zheng 	btrfs_set_root_refs(&dest->root_item, 0);
227676dda93cSYan, Zheng 
2277d68fc57bSYan, Zheng 	if (!xchg(&dest->orphan_item_inserted, 1)) {
227876dda93cSYan, Zheng 		ret = btrfs_insert_orphan_item(trans,
227976dda93cSYan, Zheng 					root->fs_info->tree_root,
228076dda93cSYan, Zheng 					dest->root_key.objectid);
228179787eaaSJeff Mahoney 		if (ret) {
228279787eaaSJeff Mahoney 			btrfs_abort_transaction(trans, root, ret);
228379787eaaSJeff Mahoney 			err = ret;
228479787eaaSJeff Mahoney 			goto out_end_trans;
2285d68fc57bSYan, Zheng 		}
228679787eaaSJeff Mahoney 	}
2287dd5f9615SStefan Behrens 
2288dd5f9615SStefan Behrens 	ret = btrfs_uuid_tree_rem(trans, root->fs_info->uuid_root,
2289dd5f9615SStefan Behrens 				  dest->root_item.uuid, BTRFS_UUID_KEY_SUBVOL,
2290dd5f9615SStefan Behrens 				  dest->root_key.objectid);
2291dd5f9615SStefan Behrens 	if (ret && ret != -ENOENT) {
2292dd5f9615SStefan Behrens 		btrfs_abort_transaction(trans, root, ret);
2293dd5f9615SStefan Behrens 		err = ret;
2294dd5f9615SStefan Behrens 		goto out_end_trans;
2295dd5f9615SStefan Behrens 	}
2296dd5f9615SStefan Behrens 	if (!btrfs_is_empty_uuid(dest->root_item.received_uuid)) {
2297dd5f9615SStefan Behrens 		ret = btrfs_uuid_tree_rem(trans, root->fs_info->uuid_root,
2298dd5f9615SStefan Behrens 					  dest->root_item.received_uuid,
2299dd5f9615SStefan Behrens 					  BTRFS_UUID_KEY_RECEIVED_SUBVOL,
2300dd5f9615SStefan Behrens 					  dest->root_key.objectid);
2301dd5f9615SStefan Behrens 		if (ret && ret != -ENOENT) {
2302dd5f9615SStefan Behrens 			btrfs_abort_transaction(trans, root, ret);
2303dd5f9615SStefan Behrens 			err = ret;
2304dd5f9615SStefan Behrens 			goto out_end_trans;
2305dd5f9615SStefan Behrens 		}
2306dd5f9615SStefan Behrens 	}
2307dd5f9615SStefan Behrens 
230879787eaaSJeff Mahoney out_end_trans:
2309c58aaad2SMiao Xie 	trans->block_rsv = NULL;
2310c58aaad2SMiao Xie 	trans->bytes_reserved = 0;
2311531cb13fSSage Weil 	ret = btrfs_end_transaction(trans, root);
231279787eaaSJeff Mahoney 	if (ret && !err)
231379787eaaSJeff Mahoney 		err = ret;
231476dda93cSYan, Zheng 	inode->i_flags |= S_DEAD;
2315c58aaad2SMiao Xie out_release:
2316c58aaad2SMiao Xie 	btrfs_subvolume_release_metadata(root, &block_rsv, qgroup_reserved);
231776dda93cSYan, Zheng out_up_write:
231876dda93cSYan, Zheng 	up_write(&root->fs_info->subvol_sem);
231976dda93cSYan, Zheng out_unlock:
232076dda93cSYan, Zheng 	mutex_unlock(&inode->i_mutex);
232176dda93cSYan, Zheng 	if (!err) {
2322efefb143SYan, Zheng 		shrink_dcache_sb(root->fs_info->sb);
232376dda93cSYan, Zheng 		btrfs_invalidate_inodes(dest);
232476dda93cSYan, Zheng 		d_delete(dentry);
2325fa6ac876SLiu Bo 
2326fa6ac876SLiu Bo 		/* the last ref */
2327fa6ac876SLiu Bo 		if (dest->cache_inode) {
2328fa6ac876SLiu Bo 			iput(dest->cache_inode);
2329fa6ac876SLiu Bo 			dest->cache_inode = NULL;
2330fa6ac876SLiu Bo 		}
233176dda93cSYan, Zheng 	}
233276dda93cSYan, Zheng out_dput:
233376dda93cSYan, Zheng 	dput(dentry);
233476dda93cSYan, Zheng out_unlock_dir:
233576dda93cSYan, Zheng 	mutex_unlock(&dir->i_mutex);
2336e43f998eSDavid Sterba out_drop_write:
23372a79f17eSAl Viro 	mnt_drop_write_file(file);
233876dda93cSYan, Zheng out:
233976dda93cSYan, Zheng 	kfree(vol_args);
234076dda93cSYan, Zheng 	return err;
234176dda93cSYan, Zheng }
234276dda93cSYan, Zheng 
23431e701a32SChris Mason static int btrfs_ioctl_defrag(struct file *file, void __user *argp)
2344f46b5a66SChristoph Hellwig {
2345496ad9aaSAl Viro 	struct inode *inode = file_inode(file);
2346f46b5a66SChristoph Hellwig 	struct btrfs_root *root = BTRFS_I(inode)->root;
23471e701a32SChris Mason 	struct btrfs_ioctl_defrag_range_args *range;
2348c146afadSYan Zheng 	int ret;
2349c146afadSYan Zheng 
235025122d15SIlya Dryomov 	ret = mnt_want_write_file(file);
235125122d15SIlya Dryomov 	if (ret)
235225122d15SIlya Dryomov 		return ret;
2353b83cc969SLi Zefan 
235425122d15SIlya Dryomov 	if (btrfs_root_readonly(root)) {
235525122d15SIlya Dryomov 		ret = -EROFS;
235625122d15SIlya Dryomov 		goto out;
23575ac00addSStefan Behrens 	}
2358f46b5a66SChristoph Hellwig 
2359f46b5a66SChristoph Hellwig 	switch (inode->i_mode & S_IFMT) {
2360f46b5a66SChristoph Hellwig 	case S_IFDIR:
2361e441d54dSChris Mason 		if (!capable(CAP_SYS_ADMIN)) {
2362e441d54dSChris Mason 			ret = -EPERM;
2363e441d54dSChris Mason 			goto out;
2364e441d54dSChris Mason 		}
2365de78b51aSEric Sandeen 		ret = btrfs_defrag_root(root);
23668929ecfaSYan, Zheng 		if (ret)
23678929ecfaSYan, Zheng 			goto out;
2368de78b51aSEric Sandeen 		ret = btrfs_defrag_root(root->fs_info->extent_root);
2369f46b5a66SChristoph Hellwig 		break;
2370f46b5a66SChristoph Hellwig 	case S_IFREG:
2371e441d54dSChris Mason 		if (!(file->f_mode & FMODE_WRITE)) {
2372e441d54dSChris Mason 			ret = -EINVAL;
2373e441d54dSChris Mason 			goto out;
2374e441d54dSChris Mason 		}
23751e701a32SChris Mason 
23761e701a32SChris Mason 		range = kzalloc(sizeof(*range), GFP_KERNEL);
23771e701a32SChris Mason 		if (!range) {
23781e701a32SChris Mason 			ret = -ENOMEM;
23791e701a32SChris Mason 			goto out;
23801e701a32SChris Mason 		}
23811e701a32SChris Mason 
23821e701a32SChris Mason 		if (argp) {
23831e701a32SChris Mason 			if (copy_from_user(range, argp,
23841e701a32SChris Mason 					   sizeof(*range))) {
23851e701a32SChris Mason 				ret = -EFAULT;
23861e701a32SChris Mason 				kfree(range);
2387683be16eSDan Carpenter 				goto out;
23881e701a32SChris Mason 			}
23891e701a32SChris Mason 			/* compression requires us to start the IO */
23901e701a32SChris Mason 			if ((range->flags & BTRFS_DEFRAG_RANGE_COMPRESS)) {
23911e701a32SChris Mason 				range->flags |= BTRFS_DEFRAG_RANGE_START_IO;
23921e701a32SChris Mason 				range->extent_thresh = (u32)-1;
23931e701a32SChris Mason 			}
23941e701a32SChris Mason 		} else {
23951e701a32SChris Mason 			/* the rest are all set to zero by kzalloc */
23961e701a32SChris Mason 			range->len = (u64)-1;
23971e701a32SChris Mason 		}
2398496ad9aaSAl Viro 		ret = btrfs_defrag_file(file_inode(file), file,
23994cb5300bSChris Mason 					range, 0, 0);
24004cb5300bSChris Mason 		if (ret > 0)
24014cb5300bSChris Mason 			ret = 0;
24021e701a32SChris Mason 		kfree(range);
2403f46b5a66SChristoph Hellwig 		break;
24048929ecfaSYan, Zheng 	default:
24058929ecfaSYan, Zheng 		ret = -EINVAL;
2406f46b5a66SChristoph Hellwig 	}
2407e441d54dSChris Mason out:
240825122d15SIlya Dryomov 	mnt_drop_write_file(file);
2409e441d54dSChris Mason 	return ret;
2410f46b5a66SChristoph Hellwig }
2411f46b5a66SChristoph Hellwig 
2412b2950863SChristoph Hellwig static long btrfs_ioctl_add_dev(struct btrfs_root *root, void __user *arg)
2413f46b5a66SChristoph Hellwig {
2414f46b5a66SChristoph Hellwig 	struct btrfs_ioctl_vol_args *vol_args;
2415f46b5a66SChristoph Hellwig 	int ret;
2416f46b5a66SChristoph Hellwig 
2417e441d54dSChris Mason 	if (!capable(CAP_SYS_ADMIN))
2418e441d54dSChris Mason 		return -EPERM;
2419e441d54dSChris Mason 
24205ac00addSStefan Behrens 	if (atomic_xchg(&root->fs_info->mutually_exclusive_operation_running,
24215ac00addSStefan Behrens 			1)) {
2422e57138b3SAnand Jain 		return BTRFS_ERROR_DEV_EXCL_RUN_IN_PROGRESS;
2423c9e9f97bSIlya Dryomov 	}
2424c9e9f97bSIlya Dryomov 
24255ac00addSStefan Behrens 	mutex_lock(&root->fs_info->volume_mutex);
2426dae7b665SLi Zefan 	vol_args = memdup_user(arg, sizeof(*vol_args));
2427c9e9f97bSIlya Dryomov 	if (IS_ERR(vol_args)) {
2428c9e9f97bSIlya Dryomov 		ret = PTR_ERR(vol_args);
2429c9e9f97bSIlya Dryomov 		goto out;
2430c9e9f97bSIlya Dryomov 	}
2431f46b5a66SChristoph Hellwig 
24325516e595SMark Fasheh 	vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
2433f46b5a66SChristoph Hellwig 	ret = btrfs_init_new_device(root, vol_args->name);
2434f46b5a66SChristoph Hellwig 
2435f46b5a66SChristoph Hellwig 	kfree(vol_args);
2436c9e9f97bSIlya Dryomov out:
2437c9e9f97bSIlya Dryomov 	mutex_unlock(&root->fs_info->volume_mutex);
24385ac00addSStefan Behrens 	atomic_set(&root->fs_info->mutually_exclusive_operation_running, 0);
2439f46b5a66SChristoph Hellwig 	return ret;
2440f46b5a66SChristoph Hellwig }
2441f46b5a66SChristoph Hellwig 
2442da24927bSMiao Xie static long btrfs_ioctl_rm_dev(struct file *file, void __user *arg)
2443f46b5a66SChristoph Hellwig {
2444496ad9aaSAl Viro 	struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
2445f46b5a66SChristoph Hellwig 	struct btrfs_ioctl_vol_args *vol_args;
2446f46b5a66SChristoph Hellwig 	int ret;
2447f46b5a66SChristoph Hellwig 
2448e441d54dSChris Mason 	if (!capable(CAP_SYS_ADMIN))
2449e441d54dSChris Mason 		return -EPERM;
2450e441d54dSChris Mason 
2451da24927bSMiao Xie 	ret = mnt_want_write_file(file);
2452da24927bSMiao Xie 	if (ret)
2453da24927bSMiao Xie 		return ret;
2454c146afadSYan Zheng 
2455dae7b665SLi Zefan 	vol_args = memdup_user(arg, sizeof(*vol_args));
2456c9e9f97bSIlya Dryomov 	if (IS_ERR(vol_args)) {
2457c9e9f97bSIlya Dryomov 		ret = PTR_ERR(vol_args);
2458c9e9f97bSIlya Dryomov 		goto out;
2459c9e9f97bSIlya Dryomov 	}
2460f46b5a66SChristoph Hellwig 
24615516e595SMark Fasheh 	vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
2462f46b5a66SChristoph Hellwig 
2463183860f6SAnand Jain 	if (atomic_xchg(&root->fs_info->mutually_exclusive_operation_running,
2464183860f6SAnand Jain 			1)) {
2465183860f6SAnand Jain 		ret = BTRFS_ERROR_DEV_EXCL_RUN_IN_PROGRESS;
2466183860f6SAnand Jain 		goto out;
2467183860f6SAnand Jain 	}
2468183860f6SAnand Jain 
2469183860f6SAnand Jain 	mutex_lock(&root->fs_info->volume_mutex);
2470183860f6SAnand Jain 	ret = btrfs_rm_device(root, vol_args->name);
2471c9e9f97bSIlya Dryomov 	mutex_unlock(&root->fs_info->volume_mutex);
24725ac00addSStefan Behrens 	atomic_set(&root->fs_info->mutually_exclusive_operation_running, 0);
2473183860f6SAnand Jain 
2474183860f6SAnand Jain out:
2475183860f6SAnand Jain 	kfree(vol_args);
24764ac20c70SIlya Dryomov 	mnt_drop_write_file(file);
2477f46b5a66SChristoph Hellwig 	return ret;
2478f46b5a66SChristoph Hellwig }
2479f46b5a66SChristoph Hellwig 
2480475f6387SJan Schmidt static long btrfs_ioctl_fs_info(struct btrfs_root *root, void __user *arg)
2481475f6387SJan Schmidt {
2482027ed2f0SLi Zefan 	struct btrfs_ioctl_fs_info_args *fi_args;
2483475f6387SJan Schmidt 	struct btrfs_device *device;
2484475f6387SJan Schmidt 	struct btrfs_device *next;
2485475f6387SJan Schmidt 	struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices;
2486027ed2f0SLi Zefan 	int ret = 0;
2487475f6387SJan Schmidt 
2488475f6387SJan Schmidt 	if (!capable(CAP_SYS_ADMIN))
2489475f6387SJan Schmidt 		return -EPERM;
2490475f6387SJan Schmidt 
2491027ed2f0SLi Zefan 	fi_args = kzalloc(sizeof(*fi_args), GFP_KERNEL);
2492027ed2f0SLi Zefan 	if (!fi_args)
2493027ed2f0SLi Zefan 		return -ENOMEM;
2494027ed2f0SLi Zefan 
2495f7171750SFilipe David Borba Manana 	mutex_lock(&fs_devices->device_list_mutex);
2496027ed2f0SLi Zefan 	fi_args->num_devices = fs_devices->num_devices;
2497027ed2f0SLi Zefan 	memcpy(&fi_args->fsid, root->fs_info->fsid, sizeof(fi_args->fsid));
2498475f6387SJan Schmidt 
2499475f6387SJan Schmidt 	list_for_each_entry_safe(device, next, &fs_devices->devices, dev_list) {
2500027ed2f0SLi Zefan 		if (device->devid > fi_args->max_id)
2501027ed2f0SLi Zefan 			fi_args->max_id = device->devid;
2502475f6387SJan Schmidt 	}
2503475f6387SJan Schmidt 	mutex_unlock(&fs_devices->device_list_mutex);
2504475f6387SJan Schmidt 
2505027ed2f0SLi Zefan 	if (copy_to_user(arg, fi_args, sizeof(*fi_args)))
2506027ed2f0SLi Zefan 		ret = -EFAULT;
2507475f6387SJan Schmidt 
2508027ed2f0SLi Zefan 	kfree(fi_args);
2509027ed2f0SLi Zefan 	return ret;
2510475f6387SJan Schmidt }
2511475f6387SJan Schmidt 
2512475f6387SJan Schmidt static long btrfs_ioctl_dev_info(struct btrfs_root *root, void __user *arg)
2513475f6387SJan Schmidt {
2514475f6387SJan Schmidt 	struct btrfs_ioctl_dev_info_args *di_args;
2515475f6387SJan Schmidt 	struct btrfs_device *dev;
2516475f6387SJan Schmidt 	struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices;
2517475f6387SJan Schmidt 	int ret = 0;
2518475f6387SJan Schmidt 	char *s_uuid = NULL;
2519475f6387SJan Schmidt 
2520475f6387SJan Schmidt 	if (!capable(CAP_SYS_ADMIN))
2521475f6387SJan Schmidt 		return -EPERM;
2522475f6387SJan Schmidt 
2523475f6387SJan Schmidt 	di_args = memdup_user(arg, sizeof(*di_args));
2524475f6387SJan Schmidt 	if (IS_ERR(di_args))
2525475f6387SJan Schmidt 		return PTR_ERR(di_args);
2526475f6387SJan Schmidt 
2527dd5f9615SStefan Behrens 	if (!btrfs_is_empty_uuid(di_args->uuid))
2528475f6387SJan Schmidt 		s_uuid = di_args->uuid;
2529475f6387SJan Schmidt 
2530475f6387SJan Schmidt 	mutex_lock(&fs_devices->device_list_mutex);
2531aa1b8cd4SStefan Behrens 	dev = btrfs_find_device(root->fs_info, di_args->devid, s_uuid, NULL);
2532475f6387SJan Schmidt 
2533475f6387SJan Schmidt 	if (!dev) {
2534475f6387SJan Schmidt 		ret = -ENODEV;
2535475f6387SJan Schmidt 		goto out;
2536475f6387SJan Schmidt 	}
2537475f6387SJan Schmidt 
2538475f6387SJan Schmidt 	di_args->devid = dev->devid;
2539475f6387SJan Schmidt 	di_args->bytes_used = dev->bytes_used;
2540475f6387SJan Schmidt 	di_args->total_bytes = dev->total_bytes;
2541475f6387SJan Schmidt 	memcpy(di_args->uuid, dev->uuid, sizeof(di_args->uuid));
2542a27202fbSJim Meyering 	if (dev->name) {
2543606686eeSJosef Bacik 		struct rcu_string *name;
2544606686eeSJosef Bacik 
2545606686eeSJosef Bacik 		rcu_read_lock();
2546606686eeSJosef Bacik 		name = rcu_dereference(dev->name);
2547606686eeSJosef Bacik 		strncpy(di_args->path, name->str, sizeof(di_args->path));
2548606686eeSJosef Bacik 		rcu_read_unlock();
2549a27202fbSJim Meyering 		di_args->path[sizeof(di_args->path) - 1] = 0;
2550a27202fbSJim Meyering 	} else {
255199ba55adSStefan Behrens 		di_args->path[0] = '\0';
2552a27202fbSJim Meyering 	}
2553475f6387SJan Schmidt 
2554475f6387SJan Schmidt out:
255555793c0dSDavid Sterba 	mutex_unlock(&fs_devices->device_list_mutex);
2556475f6387SJan Schmidt 	if (ret == 0 && copy_to_user(arg, di_args, sizeof(*di_args)))
2557475f6387SJan Schmidt 		ret = -EFAULT;
2558475f6387SJan Schmidt 
2559475f6387SJan Schmidt 	kfree(di_args);
2560475f6387SJan Schmidt 	return ret;
2561475f6387SJan Schmidt }
2562475f6387SJan Schmidt 
2563416161dbSMark Fasheh static struct page *extent_same_get_page(struct inode *inode, u64 off)
2564416161dbSMark Fasheh {
2565416161dbSMark Fasheh 	struct page *page;
2566416161dbSMark Fasheh 	pgoff_t index;
2567416161dbSMark Fasheh 	struct extent_io_tree *tree = &BTRFS_I(inode)->io_tree;
2568416161dbSMark Fasheh 
2569416161dbSMark Fasheh 	index = off >> PAGE_CACHE_SHIFT;
2570416161dbSMark Fasheh 
2571416161dbSMark Fasheh 	page = grab_cache_page(inode->i_mapping, index);
2572416161dbSMark Fasheh 	if (!page)
2573416161dbSMark Fasheh 		return NULL;
2574416161dbSMark Fasheh 
2575416161dbSMark Fasheh 	if (!PageUptodate(page)) {
2576416161dbSMark Fasheh 		if (extent_read_full_page_nolock(tree, page, btrfs_get_extent,
2577416161dbSMark Fasheh 						 0))
2578416161dbSMark Fasheh 			return NULL;
2579416161dbSMark Fasheh 		lock_page(page);
2580416161dbSMark Fasheh 		if (!PageUptodate(page)) {
2581416161dbSMark Fasheh 			unlock_page(page);
2582416161dbSMark Fasheh 			page_cache_release(page);
2583416161dbSMark Fasheh 			return NULL;
2584416161dbSMark Fasheh 		}
2585416161dbSMark Fasheh 	}
2586416161dbSMark Fasheh 	unlock_page(page);
2587416161dbSMark Fasheh 
2588416161dbSMark Fasheh 	return page;
2589416161dbSMark Fasheh }
2590416161dbSMark Fasheh 
259177fe20dcSMark Fasheh static inline void lock_extent_range(struct inode *inode, u64 off, u64 len)
259277fe20dcSMark Fasheh {
259377fe20dcSMark Fasheh 	/* do any pending delalloc/csum calc on src, one way or
259477fe20dcSMark Fasheh 	   another, and lock file content */
259577fe20dcSMark Fasheh 	while (1) {
259677fe20dcSMark Fasheh 		struct btrfs_ordered_extent *ordered;
259777fe20dcSMark Fasheh 		lock_extent(&BTRFS_I(inode)->io_tree, off, off + len - 1);
259877fe20dcSMark Fasheh 		ordered = btrfs_lookup_first_ordered_extent(inode,
259977fe20dcSMark Fasheh 							    off + len - 1);
260077fe20dcSMark Fasheh 		if (!ordered &&
260177fe20dcSMark Fasheh 		    !test_range_bit(&BTRFS_I(inode)->io_tree, off,
260277fe20dcSMark Fasheh 				    off + len - 1, EXTENT_DELALLOC, 0, NULL))
260377fe20dcSMark Fasheh 			break;
260477fe20dcSMark Fasheh 		unlock_extent(&BTRFS_I(inode)->io_tree, off, off + len - 1);
260577fe20dcSMark Fasheh 		if (ordered)
260677fe20dcSMark Fasheh 			btrfs_put_ordered_extent(ordered);
260777fe20dcSMark Fasheh 		btrfs_wait_ordered_range(inode, off, len);
260877fe20dcSMark Fasheh 	}
260977fe20dcSMark Fasheh }
261077fe20dcSMark Fasheh 
2611416161dbSMark Fasheh static void btrfs_double_unlock(struct inode *inode1, u64 loff1,
2612416161dbSMark Fasheh 				struct inode *inode2, u64 loff2, u64 len)
2613416161dbSMark Fasheh {
2614416161dbSMark Fasheh 	unlock_extent(&BTRFS_I(inode1)->io_tree, loff1, loff1 + len - 1);
2615416161dbSMark Fasheh 	unlock_extent(&BTRFS_I(inode2)->io_tree, loff2, loff2 + len - 1);
2616416161dbSMark Fasheh 
2617416161dbSMark Fasheh 	mutex_unlock(&inode1->i_mutex);
2618416161dbSMark Fasheh 	mutex_unlock(&inode2->i_mutex);
2619416161dbSMark Fasheh }
2620416161dbSMark Fasheh 
2621416161dbSMark Fasheh static void btrfs_double_lock(struct inode *inode1, u64 loff1,
2622416161dbSMark Fasheh 			      struct inode *inode2, u64 loff2, u64 len)
2623416161dbSMark Fasheh {
2624416161dbSMark Fasheh 	if (inode1 < inode2) {
2625416161dbSMark Fasheh 		swap(inode1, inode2);
2626416161dbSMark Fasheh 		swap(loff1, loff2);
2627416161dbSMark Fasheh 	}
2628416161dbSMark Fasheh 
2629416161dbSMark Fasheh 	mutex_lock_nested(&inode1->i_mutex, I_MUTEX_PARENT);
2630416161dbSMark Fasheh 	lock_extent_range(inode1, loff1, len);
2631416161dbSMark Fasheh 	if (inode1 != inode2) {
2632416161dbSMark Fasheh 		mutex_lock_nested(&inode2->i_mutex, I_MUTEX_CHILD);
2633416161dbSMark Fasheh 		lock_extent_range(inode2, loff2, len);
2634416161dbSMark Fasheh 	}
2635416161dbSMark Fasheh }
2636416161dbSMark Fasheh 
2637416161dbSMark Fasheh static int btrfs_cmp_data(struct inode *src, u64 loff, struct inode *dst,
2638416161dbSMark Fasheh 			  u64 dst_loff, u64 len)
2639416161dbSMark Fasheh {
2640416161dbSMark Fasheh 	int ret = 0;
2641416161dbSMark Fasheh 	struct page *src_page, *dst_page;
2642416161dbSMark Fasheh 	unsigned int cmp_len = PAGE_CACHE_SIZE;
2643416161dbSMark Fasheh 	void *addr, *dst_addr;
2644416161dbSMark Fasheh 
2645416161dbSMark Fasheh 	while (len) {
2646416161dbSMark Fasheh 		if (len < PAGE_CACHE_SIZE)
2647416161dbSMark Fasheh 			cmp_len = len;
2648416161dbSMark Fasheh 
2649416161dbSMark Fasheh 		src_page = extent_same_get_page(src, loff);
2650416161dbSMark Fasheh 		if (!src_page)
2651416161dbSMark Fasheh 			return -EINVAL;
2652416161dbSMark Fasheh 		dst_page = extent_same_get_page(dst, dst_loff);
2653416161dbSMark Fasheh 		if (!dst_page) {
2654416161dbSMark Fasheh 			page_cache_release(src_page);
2655416161dbSMark Fasheh 			return -EINVAL;
2656416161dbSMark Fasheh 		}
2657416161dbSMark Fasheh 		addr = kmap_atomic(src_page);
2658416161dbSMark Fasheh 		dst_addr = kmap_atomic(dst_page);
2659416161dbSMark Fasheh 
2660416161dbSMark Fasheh 		flush_dcache_page(src_page);
2661416161dbSMark Fasheh 		flush_dcache_page(dst_page);
2662416161dbSMark Fasheh 
2663416161dbSMark Fasheh 		if (memcmp(addr, dst_addr, cmp_len))
2664416161dbSMark Fasheh 			ret = BTRFS_SAME_DATA_DIFFERS;
2665416161dbSMark Fasheh 
2666416161dbSMark Fasheh 		kunmap_atomic(addr);
2667416161dbSMark Fasheh 		kunmap_atomic(dst_addr);
2668416161dbSMark Fasheh 		page_cache_release(src_page);
2669416161dbSMark Fasheh 		page_cache_release(dst_page);
2670416161dbSMark Fasheh 
2671416161dbSMark Fasheh 		if (ret)
2672416161dbSMark Fasheh 			break;
2673416161dbSMark Fasheh 
2674416161dbSMark Fasheh 		loff += cmp_len;
2675416161dbSMark Fasheh 		dst_loff += cmp_len;
2676416161dbSMark Fasheh 		len -= cmp_len;
2677416161dbSMark Fasheh 	}
2678416161dbSMark Fasheh 
2679416161dbSMark Fasheh 	return ret;
2680416161dbSMark Fasheh }
2681416161dbSMark Fasheh 
2682416161dbSMark Fasheh static int extent_same_check_offsets(struct inode *inode, u64 off, u64 len)
2683416161dbSMark Fasheh {
2684416161dbSMark Fasheh 	u64 bs = BTRFS_I(inode)->root->fs_info->sb->s_blocksize;
2685416161dbSMark Fasheh 
2686416161dbSMark Fasheh 	if (off + len > inode->i_size || off + len < off)
2687416161dbSMark Fasheh 		return -EINVAL;
2688416161dbSMark Fasheh 	/* Check that we are block aligned - btrfs_clone() requires this */
2689416161dbSMark Fasheh 	if (!IS_ALIGNED(off, bs) || !IS_ALIGNED(off + len, bs))
2690416161dbSMark Fasheh 		return -EINVAL;
2691416161dbSMark Fasheh 
2692416161dbSMark Fasheh 	return 0;
2693416161dbSMark Fasheh }
2694416161dbSMark Fasheh 
2695416161dbSMark Fasheh static int btrfs_extent_same(struct inode *src, u64 loff, u64 len,
2696416161dbSMark Fasheh 			     struct inode *dst, u64 dst_loff)
2697416161dbSMark Fasheh {
2698416161dbSMark Fasheh 	int ret;
2699416161dbSMark Fasheh 
2700416161dbSMark Fasheh 	/*
2701416161dbSMark Fasheh 	 * btrfs_clone() can't handle extents in the same file
2702416161dbSMark Fasheh 	 * yet. Once that works, we can drop this check and replace it
2703416161dbSMark Fasheh 	 * with a check for the same inode, but overlapping extents.
2704416161dbSMark Fasheh 	 */
2705416161dbSMark Fasheh 	if (src == dst)
2706416161dbSMark Fasheh 		return -EINVAL;
2707416161dbSMark Fasheh 
2708416161dbSMark Fasheh 	btrfs_double_lock(src, loff, dst, dst_loff, len);
2709416161dbSMark Fasheh 
2710416161dbSMark Fasheh 	ret = extent_same_check_offsets(src, loff, len);
2711416161dbSMark Fasheh 	if (ret)
2712416161dbSMark Fasheh 		goto out_unlock;
2713416161dbSMark Fasheh 
2714416161dbSMark Fasheh 	ret = extent_same_check_offsets(dst, dst_loff, len);
2715416161dbSMark Fasheh 	if (ret)
2716416161dbSMark Fasheh 		goto out_unlock;
2717416161dbSMark Fasheh 
2718416161dbSMark Fasheh 	/* don't make the dst file partly checksummed */
2719416161dbSMark Fasheh 	if ((BTRFS_I(src)->flags & BTRFS_INODE_NODATASUM) !=
2720416161dbSMark Fasheh 	    (BTRFS_I(dst)->flags & BTRFS_INODE_NODATASUM)) {
2721416161dbSMark Fasheh 		ret = -EINVAL;
2722416161dbSMark Fasheh 		goto out_unlock;
2723416161dbSMark Fasheh 	}
2724416161dbSMark Fasheh 
2725416161dbSMark Fasheh 	ret = btrfs_cmp_data(src, loff, dst, dst_loff, len);
2726416161dbSMark Fasheh 	if (ret == 0)
2727416161dbSMark Fasheh 		ret = btrfs_clone(src, dst, loff, len, len, dst_loff);
2728416161dbSMark Fasheh 
2729416161dbSMark Fasheh out_unlock:
2730416161dbSMark Fasheh 	btrfs_double_unlock(src, loff, dst, dst_loff, len);
2731416161dbSMark Fasheh 
2732416161dbSMark Fasheh 	return ret;
2733416161dbSMark Fasheh }
2734416161dbSMark Fasheh 
2735416161dbSMark Fasheh #define BTRFS_MAX_DEDUPE_LEN	(16 * 1024 * 1024)
2736416161dbSMark Fasheh 
2737416161dbSMark Fasheh static long btrfs_ioctl_file_extent_same(struct file *file,
2738416161dbSMark Fasheh 					 void __user *argp)
2739416161dbSMark Fasheh {
2740cbf8b8caSMark Fasheh 	struct btrfs_ioctl_same_args tmp;
2741cbf8b8caSMark Fasheh 	struct btrfs_ioctl_same_args *same;
2742cbf8b8caSMark Fasheh 	struct btrfs_ioctl_same_extent_info *info;
2743416161dbSMark Fasheh 	struct inode *src = file->f_dentry->d_inode;
2744416161dbSMark Fasheh 	struct file *dst_file = NULL;
2745416161dbSMark Fasheh 	struct inode *dst;
2746416161dbSMark Fasheh 	u64 off;
2747416161dbSMark Fasheh 	u64 len;
2748416161dbSMark Fasheh 	int i;
2749416161dbSMark Fasheh 	int ret;
2750cbf8b8caSMark Fasheh 	unsigned long size;
2751416161dbSMark Fasheh 	u64 bs = BTRFS_I(src)->root->fs_info->sb->s_blocksize;
2752416161dbSMark Fasheh 	bool is_admin = capable(CAP_SYS_ADMIN);
2753416161dbSMark Fasheh 
2754416161dbSMark Fasheh 	if (!(file->f_mode & FMODE_READ))
2755416161dbSMark Fasheh 		return -EINVAL;
2756416161dbSMark Fasheh 
2757416161dbSMark Fasheh 	ret = mnt_want_write_file(file);
2758416161dbSMark Fasheh 	if (ret)
2759416161dbSMark Fasheh 		return ret;
2760416161dbSMark Fasheh 
2761cbf8b8caSMark Fasheh 	if (copy_from_user(&tmp,
2762416161dbSMark Fasheh 			   (struct btrfs_ioctl_same_args __user *)argp,
2763cbf8b8caSMark Fasheh 			   sizeof(tmp))) {
2764416161dbSMark Fasheh 		ret = -EFAULT;
2765416161dbSMark Fasheh 		goto out;
2766416161dbSMark Fasheh 	}
2767416161dbSMark Fasheh 
2768cbf8b8caSMark Fasheh 	size = sizeof(tmp) +
2769cbf8b8caSMark Fasheh 		tmp.dest_count * sizeof(struct btrfs_ioctl_same_extent_info);
2770cbf8b8caSMark Fasheh 
2771229eed43SGeyslan G. Bem 	same = memdup_user((struct btrfs_ioctl_same_args __user *)argp, size);
2772cbf8b8caSMark Fasheh 
2773229eed43SGeyslan G. Bem 	if (IS_ERR(same)) {
2774229eed43SGeyslan G. Bem 		ret = PTR_ERR(same);
2775cbf8b8caSMark Fasheh 		goto out;
2776cbf8b8caSMark Fasheh 	}
2777cbf8b8caSMark Fasheh 
2778cbf8b8caSMark Fasheh 	off = same->logical_offset;
2779cbf8b8caSMark Fasheh 	len = same->length;
2780416161dbSMark Fasheh 
2781416161dbSMark Fasheh 	/*
2782416161dbSMark Fasheh 	 * Limit the total length we will dedupe for each operation.
2783416161dbSMark Fasheh 	 * This is intended to bound the total time spent in this
2784416161dbSMark Fasheh 	 * ioctl to something sane.
2785416161dbSMark Fasheh 	 */
2786416161dbSMark Fasheh 	if (len > BTRFS_MAX_DEDUPE_LEN)
2787416161dbSMark Fasheh 		len = BTRFS_MAX_DEDUPE_LEN;
2788416161dbSMark Fasheh 
2789416161dbSMark Fasheh 	if (WARN_ON_ONCE(bs < PAGE_CACHE_SIZE)) {
2790416161dbSMark Fasheh 		/*
2791416161dbSMark Fasheh 		 * Btrfs does not support blocksize < page_size. As a
2792416161dbSMark Fasheh 		 * result, btrfs_cmp_data() won't correctly handle
2793416161dbSMark Fasheh 		 * this situation without an update.
2794416161dbSMark Fasheh 		 */
2795416161dbSMark Fasheh 		ret = -EINVAL;
2796416161dbSMark Fasheh 		goto out;
2797416161dbSMark Fasheh 	}
2798416161dbSMark Fasheh 
2799416161dbSMark Fasheh 	ret = -EISDIR;
2800416161dbSMark Fasheh 	if (S_ISDIR(src->i_mode))
2801416161dbSMark Fasheh 		goto out;
2802416161dbSMark Fasheh 
2803416161dbSMark Fasheh 	ret = -EACCES;
2804416161dbSMark Fasheh 	if (!S_ISREG(src->i_mode))
2805416161dbSMark Fasheh 		goto out;
2806416161dbSMark Fasheh 
2807cbf8b8caSMark Fasheh 	/* pre-format output fields to sane values */
2808cbf8b8caSMark Fasheh 	for (i = 0; i < same->dest_count; i++) {
2809cbf8b8caSMark Fasheh 		same->info[i].bytes_deduped = 0ULL;
2810cbf8b8caSMark Fasheh 		same->info[i].status = 0;
2811416161dbSMark Fasheh 	}
2812416161dbSMark Fasheh 
2813cbf8b8caSMark Fasheh 	ret = 0;
2814cbf8b8caSMark Fasheh 	for (i = 0; i < same->dest_count; i++) {
2815cbf8b8caSMark Fasheh 		info = &same->info[i];
2816416161dbSMark Fasheh 
2817cbf8b8caSMark Fasheh 		dst_file = fget(info->fd);
2818416161dbSMark Fasheh 		if (!dst_file) {
2819cbf8b8caSMark Fasheh 			info->status = -EBADF;
2820416161dbSMark Fasheh 			goto next;
2821416161dbSMark Fasheh 		}
2822416161dbSMark Fasheh 
2823416161dbSMark Fasheh 		if (!(is_admin || (dst_file->f_mode & FMODE_WRITE))) {
2824cbf8b8caSMark Fasheh 			info->status = -EINVAL;
2825416161dbSMark Fasheh 			goto next;
2826416161dbSMark Fasheh 		}
2827416161dbSMark Fasheh 
2828cbf8b8caSMark Fasheh 		info->status = -EXDEV;
2829416161dbSMark Fasheh 		if (file->f_path.mnt != dst_file->f_path.mnt)
2830416161dbSMark Fasheh 			goto next;
2831416161dbSMark Fasheh 
2832416161dbSMark Fasheh 		dst = dst_file->f_dentry->d_inode;
2833416161dbSMark Fasheh 		if (src->i_sb != dst->i_sb)
2834416161dbSMark Fasheh 			goto next;
2835416161dbSMark Fasheh 
2836416161dbSMark Fasheh 		if (S_ISDIR(dst->i_mode)) {
2837cbf8b8caSMark Fasheh 			info->status = -EISDIR;
2838416161dbSMark Fasheh 			goto next;
2839416161dbSMark Fasheh 		}
2840416161dbSMark Fasheh 
2841416161dbSMark Fasheh 		if (!S_ISREG(dst->i_mode)) {
2842cbf8b8caSMark Fasheh 			info->status = -EACCES;
2843416161dbSMark Fasheh 			goto next;
2844416161dbSMark Fasheh 		}
2845416161dbSMark Fasheh 
2846cbf8b8caSMark Fasheh 		info->status = btrfs_extent_same(src, off, len, dst,
2847cbf8b8caSMark Fasheh 						info->logical_offset);
2848cbf8b8caSMark Fasheh 		if (info->status == 0)
2849cbf8b8caSMark Fasheh 			info->bytes_deduped += len;
2850416161dbSMark Fasheh 
2851416161dbSMark Fasheh next:
2852416161dbSMark Fasheh 		if (dst_file)
2853416161dbSMark Fasheh 			fput(dst_file);
2854cbf8b8caSMark Fasheh 	}
2855416161dbSMark Fasheh 
2856cbf8b8caSMark Fasheh 	ret = copy_to_user(argp, same, size);
2857cbf8b8caSMark Fasheh 	if (ret)
2858416161dbSMark Fasheh 		ret = -EFAULT;
2859416161dbSMark Fasheh 
2860416161dbSMark Fasheh out:
2861416161dbSMark Fasheh 	mnt_drop_write_file(file);
2862416161dbSMark Fasheh 	return ret;
2863416161dbSMark Fasheh }
2864416161dbSMark Fasheh 
286532b7c687SMark Fasheh /**
286632b7c687SMark Fasheh  * btrfs_clone() - clone a range from inode file to another
286732b7c687SMark Fasheh  *
286832b7c687SMark Fasheh  * @src: Inode to clone from
286932b7c687SMark Fasheh  * @inode: Inode to clone to
287032b7c687SMark Fasheh  * @off: Offset within source to start clone from
287132b7c687SMark Fasheh  * @olen: Original length, passed by user, of range to clone
287232b7c687SMark Fasheh  * @olen_aligned: Block-aligned value of olen, extent_same uses
287332b7c687SMark Fasheh  *               identical values here
287432b7c687SMark Fasheh  * @destoff: Offset within @inode to start clone
287532b7c687SMark Fasheh  */
287632b7c687SMark Fasheh static int btrfs_clone(struct inode *src, struct inode *inode,
287732b7c687SMark Fasheh 		       u64 off, u64 olen, u64 olen_aligned, u64 destoff)
2878f46b5a66SChristoph Hellwig {
2879f46b5a66SChristoph Hellwig 	struct btrfs_root *root = BTRFS_I(inode)->root;
288032b7c687SMark Fasheh 	struct btrfs_path *path = NULL;
2881f46b5a66SChristoph Hellwig 	struct extent_buffer *leaf;
288232b7c687SMark Fasheh 	struct btrfs_trans_handle *trans;
288332b7c687SMark Fasheh 	char *buf = NULL;
2884ae01a0abSYan Zheng 	struct btrfs_key key;
2885f46b5a66SChristoph Hellwig 	u32 nritems;
2886f46b5a66SChristoph Hellwig 	int slot;
2887ae01a0abSYan Zheng 	int ret;
288832b7c687SMark Fasheh 	u64 len = olen_aligned;
2889ae01a0abSYan Zheng 
2890ae01a0abSYan Zheng 	ret = -ENOMEM;
2891ae01a0abSYan Zheng 	buf = vmalloc(btrfs_level_size(root, 0));
2892ae01a0abSYan Zheng 	if (!buf)
289332b7c687SMark Fasheh 		return ret;
2894ae01a0abSYan Zheng 
2895ae01a0abSYan Zheng 	path = btrfs_alloc_path();
2896ae01a0abSYan Zheng 	if (!path) {
2897ae01a0abSYan Zheng 		vfree(buf);
289832b7c687SMark Fasheh 		return ret;
2899ae01a0abSYan Zheng 	}
290032b7c687SMark Fasheh 
2901ae01a0abSYan Zheng 	path->reada = 2;
2902c5c9cd4dSSage Weil 	/* clone data */
290333345d01SLi Zefan 	key.objectid = btrfs_ino(src);
2904ae01a0abSYan Zheng 	key.type = BTRFS_EXTENT_DATA_KEY;
2905ae01a0abSYan Zheng 	key.offset = 0;
2906f46b5a66SChristoph Hellwig 
2907f46b5a66SChristoph Hellwig 	while (1) {
2908f46b5a66SChristoph Hellwig 		/*
2909f46b5a66SChristoph Hellwig 		 * note the key will change type as we walk through the
2910f46b5a66SChristoph Hellwig 		 * tree.
2911f46b5a66SChristoph Hellwig 		 */
2912e4355f34SFilipe David Borba Manana 		path->leave_spinning = 1;
2913362a20c5SDavid Sterba 		ret = btrfs_search_slot(NULL, BTRFS_I(src)->root, &key, path,
2914362a20c5SDavid Sterba 				0, 0);
2915f46b5a66SChristoph Hellwig 		if (ret < 0)
2916f46b5a66SChristoph Hellwig 			goto out;
2917f46b5a66SChristoph Hellwig 
2918ae01a0abSYan Zheng 		nritems = btrfs_header_nritems(path->nodes[0]);
2919e4355f34SFilipe David Borba Manana process_slot:
2920ae01a0abSYan Zheng 		if (path->slots[0] >= nritems) {
2921362a20c5SDavid Sterba 			ret = btrfs_next_leaf(BTRFS_I(src)->root, path);
2922f46b5a66SChristoph Hellwig 			if (ret < 0)
2923f46b5a66SChristoph Hellwig 				goto out;
2924f46b5a66SChristoph Hellwig 			if (ret > 0)
2925f46b5a66SChristoph Hellwig 				break;
2926ae01a0abSYan Zheng 			nritems = btrfs_header_nritems(path->nodes[0]);
2927f46b5a66SChristoph Hellwig 		}
2928f46b5a66SChristoph Hellwig 		leaf = path->nodes[0];
2929f46b5a66SChristoph Hellwig 		slot = path->slots[0];
2930f46b5a66SChristoph Hellwig 
2931ae01a0abSYan Zheng 		btrfs_item_key_to_cpu(leaf, &key, slot);
2932d20f7043SChris Mason 		if (btrfs_key_type(&key) > BTRFS_EXTENT_DATA_KEY ||
293333345d01SLi Zefan 		    key.objectid != btrfs_ino(src))
2934f46b5a66SChristoph Hellwig 			break;
2935f46b5a66SChristoph Hellwig 
2936c5c9cd4dSSage Weil 		if (btrfs_key_type(&key) == BTRFS_EXTENT_DATA_KEY) {
2937c5c9cd4dSSage Weil 			struct btrfs_file_extent_item *extent;
2938c5c9cd4dSSage Weil 			int type;
293931840ae1SZheng Yan 			u32 size;
294031840ae1SZheng Yan 			struct btrfs_key new_key;
2941c5c9cd4dSSage Weil 			u64 disko = 0, diskl = 0;
2942c5c9cd4dSSage Weil 			u64 datao = 0, datal = 0;
2943c5c9cd4dSSage Weil 			u8 comp;
2944b5384d48SSage Weil 			u64 endoff;
294531840ae1SZheng Yan 
2946c5c9cd4dSSage Weil 			extent = btrfs_item_ptr(leaf, slot,
2947c5c9cd4dSSage Weil 						struct btrfs_file_extent_item);
2948c5c9cd4dSSage Weil 			comp = btrfs_file_extent_compression(leaf, extent);
2949c5c9cd4dSSage Weil 			type = btrfs_file_extent_type(leaf, extent);
2950c8a894d7SChris Mason 			if (type == BTRFS_FILE_EXTENT_REG ||
2951c8a894d7SChris Mason 			    type == BTRFS_FILE_EXTENT_PREALLOC) {
2952d397712bSChris Mason 				disko = btrfs_file_extent_disk_bytenr(leaf,
2953d397712bSChris Mason 								      extent);
2954d397712bSChris Mason 				diskl = btrfs_file_extent_disk_num_bytes(leaf,
2955d397712bSChris Mason 								 extent);
2956c5c9cd4dSSage Weil 				datao = btrfs_file_extent_offset(leaf, extent);
2957d397712bSChris Mason 				datal = btrfs_file_extent_num_bytes(leaf,
2958d397712bSChris Mason 								    extent);
2959c5c9cd4dSSage Weil 			} else if (type == BTRFS_FILE_EXTENT_INLINE) {
2960c5c9cd4dSSage Weil 				/* take upper bound, may be compressed */
2961c5c9cd4dSSage Weil 				datal = btrfs_file_extent_ram_bytes(leaf,
2962c5c9cd4dSSage Weil 								    extent);
2963c5c9cd4dSSage Weil 			}
296431840ae1SZheng Yan 
2965050006a7SSage Weil 			if (key.offset + datal <= off ||
2966e4355f34SFilipe David Borba Manana 			    key.offset >= off + len - 1) {
2967e4355f34SFilipe David Borba Manana 				path->slots[0]++;
2968e4355f34SFilipe David Borba Manana 				goto process_slot;
2969e4355f34SFilipe David Borba Manana 			}
2970e4355f34SFilipe David Borba Manana 
2971e4355f34SFilipe David Borba Manana 			size = btrfs_item_size_nr(leaf, slot);
2972e4355f34SFilipe David Borba Manana 			read_extent_buffer(leaf, buf,
2973e4355f34SFilipe David Borba Manana 					   btrfs_item_ptr_offset(leaf, slot),
2974e4355f34SFilipe David Borba Manana 					   size);
2975e4355f34SFilipe David Borba Manana 
2976e4355f34SFilipe David Borba Manana 			btrfs_release_path(path);
2977e4355f34SFilipe David Borba Manana 			path->leave_spinning = 0;
2978c5c9cd4dSSage Weil 
297931840ae1SZheng Yan 			memcpy(&new_key, &key, sizeof(new_key));
298033345d01SLi Zefan 			new_key.objectid = btrfs_ino(inode);
29814d728ec7SLi Zefan 			if (off <= key.offset)
2982c5c9cd4dSSage Weil 				new_key.offset = key.offset + destoff - off;
29834d728ec7SLi Zefan 			else
29844d728ec7SLi Zefan 				new_key.offset = destoff;
2985c5c9cd4dSSage Weil 
2986b6f3409bSSage Weil 			/*
2987b6f3409bSSage Weil 			 * 1 - adjusting old extent (we may have to split it)
2988b6f3409bSSage Weil 			 * 1 - add new extent
2989b6f3409bSSage Weil 			 * 1 - inode update
2990b6f3409bSSage Weil 			 */
2991b6f3409bSSage Weil 			trans = btrfs_start_transaction(root, 3);
2992a22285a6SYan, Zheng 			if (IS_ERR(trans)) {
2993a22285a6SYan, Zheng 				ret = PTR_ERR(trans);
2994a22285a6SYan, Zheng 				goto out;
2995a22285a6SYan, Zheng 			}
2996a22285a6SYan, Zheng 
2997c8a894d7SChris Mason 			if (type == BTRFS_FILE_EXTENT_REG ||
2998c8a894d7SChris Mason 			    type == BTRFS_FILE_EXTENT_PREALLOC) {
2999d72c0842SLi Zefan 				/*
3000d72c0842SLi Zefan 				 *    a  | --- range to clone ---|  b
3001d72c0842SLi Zefan 				 * | ------------- extent ------------- |
3002d72c0842SLi Zefan 				 */
3003d72c0842SLi Zefan 
3004d72c0842SLi Zefan 				/* substract range b */
3005d72c0842SLi Zefan 				if (key.offset + datal > off + len)
3006d72c0842SLi Zefan 					datal = off + len - key.offset;
3007d72c0842SLi Zefan 
3008d72c0842SLi Zefan 				/* substract range a */
3009a22285a6SYan, Zheng 				if (off > key.offset) {
3010a22285a6SYan, Zheng 					datao += off - key.offset;
3011a22285a6SYan, Zheng 					datal -= off - key.offset;
3012a22285a6SYan, Zheng 				}
3013a22285a6SYan, Zheng 
30145dc562c5SJosef Bacik 				ret = btrfs_drop_extents(trans, root, inode,
3015a22285a6SYan, Zheng 							 new_key.offset,
3016a22285a6SYan, Zheng 							 new_key.offset + datal,
30172671485dSJosef Bacik 							 1);
301879787eaaSJeff Mahoney 				if (ret) {
301979787eaaSJeff Mahoney 					btrfs_abort_transaction(trans, root,
302079787eaaSJeff Mahoney 								ret);
302179787eaaSJeff Mahoney 					btrfs_end_transaction(trans, root);
302279787eaaSJeff Mahoney 					goto out;
302379787eaaSJeff Mahoney 				}
3024a22285a6SYan, Zheng 
302531840ae1SZheng Yan 				ret = btrfs_insert_empty_item(trans, root, path,
302631840ae1SZheng Yan 							      &new_key, size);
302779787eaaSJeff Mahoney 				if (ret) {
302879787eaaSJeff Mahoney 					btrfs_abort_transaction(trans, root,
302979787eaaSJeff Mahoney 								ret);
303079787eaaSJeff Mahoney 					btrfs_end_transaction(trans, root);
303179787eaaSJeff Mahoney 					goto out;
303279787eaaSJeff Mahoney 				}
303331840ae1SZheng Yan 
303431840ae1SZheng Yan 				leaf = path->nodes[0];
303531840ae1SZheng Yan 				slot = path->slots[0];
303631840ae1SZheng Yan 				write_extent_buffer(leaf, buf,
303731840ae1SZheng Yan 					    btrfs_item_ptr_offset(leaf, slot),
303831840ae1SZheng Yan 					    size);
3039ae01a0abSYan Zheng 
3040f46b5a66SChristoph Hellwig 				extent = btrfs_item_ptr(leaf, slot,
3041f46b5a66SChristoph Hellwig 						struct btrfs_file_extent_item);
3042c5c9cd4dSSage Weil 
3043c5c9cd4dSSage Weil 				/* disko == 0 means it's a hole */
3044c5c9cd4dSSage Weil 				if (!disko)
3045c5c9cd4dSSage Weil 					datao = 0;
3046c5c9cd4dSSage Weil 
3047c5c9cd4dSSage Weil 				btrfs_set_file_extent_offset(leaf, extent,
3048c5c9cd4dSSage Weil 							     datao);
3049c5c9cd4dSSage Weil 				btrfs_set_file_extent_num_bytes(leaf, extent,
3050c5c9cd4dSSage Weil 								datal);
3051c5c9cd4dSSage Weil 				if (disko) {
3052c5c9cd4dSSage Weil 					inode_add_bytes(inode, datal);
3053ae01a0abSYan Zheng 					ret = btrfs_inc_extent_ref(trans, root,
30545d4f98a2SYan Zheng 							disko, diskl, 0,
3055f46b5a66SChristoph Hellwig 							root->root_key.objectid,
305633345d01SLi Zefan 							btrfs_ino(inode),
305766d7e7f0SArne Jansen 							new_key.offset - datao,
305866d7e7f0SArne Jansen 							0);
305979787eaaSJeff Mahoney 					if (ret) {
306079787eaaSJeff Mahoney 						btrfs_abort_transaction(trans,
306179787eaaSJeff Mahoney 									root,
306279787eaaSJeff Mahoney 									ret);
306379787eaaSJeff Mahoney 						btrfs_end_transaction(trans,
306479787eaaSJeff Mahoney 								      root);
306579787eaaSJeff Mahoney 						goto out;
306679787eaaSJeff Mahoney 
306779787eaaSJeff Mahoney 					}
3068f46b5a66SChristoph Hellwig 				}
3069c5c9cd4dSSage Weil 			} else if (type == BTRFS_FILE_EXTENT_INLINE) {
3070c5c9cd4dSSage Weil 				u64 skip = 0;
3071c5c9cd4dSSage Weil 				u64 trim = 0;
3072c5c9cd4dSSage Weil 				if (off > key.offset) {
3073c5c9cd4dSSage Weil 					skip = off - key.offset;
3074c5c9cd4dSSage Weil 					new_key.offset += skip;
307531840ae1SZheng Yan 				}
3076d397712bSChris Mason 
3077c5c9cd4dSSage Weil 				if (key.offset + datal > off + len)
3078c5c9cd4dSSage Weil 					trim = key.offset + datal - (off + len);
3079d397712bSChris Mason 
3080c5c9cd4dSSage Weil 				if (comp && (skip || trim)) {
3081c5c9cd4dSSage Weil 					ret = -EINVAL;
3082a22285a6SYan, Zheng 					btrfs_end_transaction(trans, root);
3083c5c9cd4dSSage Weil 					goto out;
308431840ae1SZheng Yan 				}
3085c5c9cd4dSSage Weil 				size -= skip + trim;
3086c5c9cd4dSSage Weil 				datal -= skip + trim;
3087a22285a6SYan, Zheng 
30885dc562c5SJosef Bacik 				ret = btrfs_drop_extents(trans, root, inode,
3089a22285a6SYan, Zheng 							 new_key.offset,
3090a22285a6SYan, Zheng 							 new_key.offset + datal,
30912671485dSJosef Bacik 							 1);
309279787eaaSJeff Mahoney 				if (ret) {
309379787eaaSJeff Mahoney 					btrfs_abort_transaction(trans, root,
309479787eaaSJeff Mahoney 								ret);
309579787eaaSJeff Mahoney 					btrfs_end_transaction(trans, root);
309679787eaaSJeff Mahoney 					goto out;
309779787eaaSJeff Mahoney 				}
3098a22285a6SYan, Zheng 
3099c5c9cd4dSSage Weil 				ret = btrfs_insert_empty_item(trans, root, path,
3100c5c9cd4dSSage Weil 							      &new_key, size);
310179787eaaSJeff Mahoney 				if (ret) {
310279787eaaSJeff Mahoney 					btrfs_abort_transaction(trans, root,
310379787eaaSJeff Mahoney 								ret);
310479787eaaSJeff Mahoney 					btrfs_end_transaction(trans, root);
310579787eaaSJeff Mahoney 					goto out;
310679787eaaSJeff Mahoney 				}
3107c5c9cd4dSSage Weil 
3108c5c9cd4dSSage Weil 				if (skip) {
3109d397712bSChris Mason 					u32 start =
3110d397712bSChris Mason 					  btrfs_file_extent_calc_inline_size(0);
3111c5c9cd4dSSage Weil 					memmove(buf+start, buf+start+skip,
3112c5c9cd4dSSage Weil 						datal);
3113c5c9cd4dSSage Weil 				}
3114c5c9cd4dSSage Weil 
3115c5c9cd4dSSage Weil 				leaf = path->nodes[0];
3116c5c9cd4dSSage Weil 				slot = path->slots[0];
3117c5c9cd4dSSage Weil 				write_extent_buffer(leaf, buf,
3118c5c9cd4dSSage Weil 					    btrfs_item_ptr_offset(leaf, slot),
3119c5c9cd4dSSage Weil 					    size);
3120c5c9cd4dSSage Weil 				inode_add_bytes(inode, datal);
3121c5c9cd4dSSage Weil 			}
3122c5c9cd4dSSage Weil 
3123c5c9cd4dSSage Weil 			btrfs_mark_buffer_dirty(leaf);
3124b3b4aa74SDavid Sterba 			btrfs_release_path(path);
3125c5c9cd4dSSage Weil 
31260c4d2d95SJosef Bacik 			inode_inc_iversion(inode);
3127a22285a6SYan, Zheng 			inode->i_mtime = inode->i_ctime = CURRENT_TIME;
3128b5384d48SSage Weil 
3129b5384d48SSage Weil 			/*
3130b5384d48SSage Weil 			 * we round up to the block size at eof when
3131b5384d48SSage Weil 			 * determining which extents to clone above,
3132b5384d48SSage Weil 			 * but shouldn't round up the file size
3133b5384d48SSage Weil 			 */
3134b5384d48SSage Weil 			endoff = new_key.offset + datal;
31355f3888ffSLi Zefan 			if (endoff > destoff+olen)
31365f3888ffSLi Zefan 				endoff = destoff+olen;
3137b5384d48SSage Weil 			if (endoff > inode->i_size)
3138b5384d48SSage Weil 				btrfs_i_size_write(inode, endoff);
3139b5384d48SSage Weil 
3140a22285a6SYan, Zheng 			ret = btrfs_update_inode(trans, root, inode);
314179787eaaSJeff Mahoney 			if (ret) {
314279787eaaSJeff Mahoney 				btrfs_abort_transaction(trans, root, ret);
3143a22285a6SYan, Zheng 				btrfs_end_transaction(trans, root);
314479787eaaSJeff Mahoney 				goto out;
314579787eaaSJeff Mahoney 			}
314679787eaaSJeff Mahoney 			ret = btrfs_end_transaction(trans, root);
3147a22285a6SYan, Zheng 		}
3148b3b4aa74SDavid Sterba 		btrfs_release_path(path);
3149ae01a0abSYan Zheng 		key.offset++;
3150ae01a0abSYan Zheng 	}
3151f46b5a66SChristoph Hellwig 	ret = 0;
315232b7c687SMark Fasheh 
3153f46b5a66SChristoph Hellwig out:
3154b3b4aa74SDavid Sterba 	btrfs_release_path(path);
315532b7c687SMark Fasheh 	btrfs_free_path(path);
315632b7c687SMark Fasheh 	vfree(buf);
315732b7c687SMark Fasheh 	return ret;
315832b7c687SMark Fasheh }
315932b7c687SMark Fasheh 
316032b7c687SMark Fasheh static noinline long btrfs_ioctl_clone(struct file *file, unsigned long srcfd,
316132b7c687SMark Fasheh 				       u64 off, u64 olen, u64 destoff)
316232b7c687SMark Fasheh {
316354563d41SAl Viro 	struct inode *inode = file_inode(file);
316432b7c687SMark Fasheh 	struct btrfs_root *root = BTRFS_I(inode)->root;
316532b7c687SMark Fasheh 	struct fd src_file;
316632b7c687SMark Fasheh 	struct inode *src;
316732b7c687SMark Fasheh 	int ret;
316832b7c687SMark Fasheh 	u64 len = olen;
316932b7c687SMark Fasheh 	u64 bs = root->fs_info->sb->s_blocksize;
317032b7c687SMark Fasheh 	int same_inode = 0;
317132b7c687SMark Fasheh 
317232b7c687SMark Fasheh 	/*
317332b7c687SMark Fasheh 	 * TODO:
317432b7c687SMark Fasheh 	 * - split compressed inline extents.  annoying: we need to
317532b7c687SMark Fasheh 	 *   decompress into destination's address_space (the file offset
317632b7c687SMark Fasheh 	 *   may change, so source mapping won't do), then recompress (or
317732b7c687SMark Fasheh 	 *   otherwise reinsert) a subrange.
317832b7c687SMark Fasheh 	 * - allow ranges within the same file to be cloned (provided
317932b7c687SMark Fasheh 	 *   they don't overlap)?
318032b7c687SMark Fasheh 	 */
318132b7c687SMark Fasheh 
318232b7c687SMark Fasheh 	/* the destination must be opened for writing */
318332b7c687SMark Fasheh 	if (!(file->f_mode & FMODE_WRITE) || (file->f_flags & O_APPEND))
318432b7c687SMark Fasheh 		return -EINVAL;
318532b7c687SMark Fasheh 
318632b7c687SMark Fasheh 	if (btrfs_root_readonly(root))
318732b7c687SMark Fasheh 		return -EROFS;
318832b7c687SMark Fasheh 
318932b7c687SMark Fasheh 	ret = mnt_want_write_file(file);
319032b7c687SMark Fasheh 	if (ret)
319132b7c687SMark Fasheh 		return ret;
319232b7c687SMark Fasheh 
319332b7c687SMark Fasheh 	src_file = fdget(srcfd);
319432b7c687SMark Fasheh 	if (!src_file.file) {
319532b7c687SMark Fasheh 		ret = -EBADF;
319632b7c687SMark Fasheh 		goto out_drop_write;
319732b7c687SMark Fasheh 	}
319832b7c687SMark Fasheh 
319932b7c687SMark Fasheh 	ret = -EXDEV;
320032b7c687SMark Fasheh 	if (src_file.file->f_path.mnt != file->f_path.mnt)
320132b7c687SMark Fasheh 		goto out_fput;
320232b7c687SMark Fasheh 
320332b7c687SMark Fasheh 	src = file_inode(src_file.file);
320432b7c687SMark Fasheh 
320532b7c687SMark Fasheh 	ret = -EINVAL;
320632b7c687SMark Fasheh 	if (src == inode)
320732b7c687SMark Fasheh 		same_inode = 1;
320832b7c687SMark Fasheh 
320932b7c687SMark Fasheh 	/* the src must be open for reading */
321032b7c687SMark Fasheh 	if (!(src_file.file->f_mode & FMODE_READ))
321132b7c687SMark Fasheh 		goto out_fput;
321232b7c687SMark Fasheh 
321332b7c687SMark Fasheh 	/* don't make the dst file partly checksummed */
321432b7c687SMark Fasheh 	if ((BTRFS_I(src)->flags & BTRFS_INODE_NODATASUM) !=
321532b7c687SMark Fasheh 	    (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM))
321632b7c687SMark Fasheh 		goto out_fput;
321732b7c687SMark Fasheh 
321832b7c687SMark Fasheh 	ret = -EISDIR;
321932b7c687SMark Fasheh 	if (S_ISDIR(src->i_mode) || S_ISDIR(inode->i_mode))
322032b7c687SMark Fasheh 		goto out_fput;
322132b7c687SMark Fasheh 
322232b7c687SMark Fasheh 	ret = -EXDEV;
322332b7c687SMark Fasheh 	if (src->i_sb != inode->i_sb)
322432b7c687SMark Fasheh 		goto out_fput;
322532b7c687SMark Fasheh 
322632b7c687SMark Fasheh 	if (!same_inode) {
322732b7c687SMark Fasheh 		if (inode < src) {
322832b7c687SMark Fasheh 			mutex_lock_nested(&inode->i_mutex, I_MUTEX_PARENT);
322932b7c687SMark Fasheh 			mutex_lock_nested(&src->i_mutex, I_MUTEX_CHILD);
323032b7c687SMark Fasheh 		} else {
323132b7c687SMark Fasheh 			mutex_lock_nested(&src->i_mutex, I_MUTEX_PARENT);
323232b7c687SMark Fasheh 			mutex_lock_nested(&inode->i_mutex, I_MUTEX_CHILD);
323332b7c687SMark Fasheh 		}
323432b7c687SMark Fasheh 	} else {
323532b7c687SMark Fasheh 		mutex_lock(&src->i_mutex);
323632b7c687SMark Fasheh 	}
323732b7c687SMark Fasheh 
323832b7c687SMark Fasheh 	/* determine range to clone */
323932b7c687SMark Fasheh 	ret = -EINVAL;
324032b7c687SMark Fasheh 	if (off + len > src->i_size || off + len < off)
324132b7c687SMark Fasheh 		goto out_unlock;
324232b7c687SMark Fasheh 	if (len == 0)
324332b7c687SMark Fasheh 		olen = len = src->i_size - off;
324432b7c687SMark Fasheh 	/* if we extend to eof, continue to block boundary */
324532b7c687SMark Fasheh 	if (off + len == src->i_size)
324632b7c687SMark Fasheh 		len = ALIGN(src->i_size, bs) - off;
324732b7c687SMark Fasheh 
324832b7c687SMark Fasheh 	/* verify the end result is block aligned */
324932b7c687SMark Fasheh 	if (!IS_ALIGNED(off, bs) || !IS_ALIGNED(off + len, bs) ||
325032b7c687SMark Fasheh 	    !IS_ALIGNED(destoff, bs))
325132b7c687SMark Fasheh 		goto out_unlock;
325232b7c687SMark Fasheh 
325332b7c687SMark Fasheh 	/* verify if ranges are overlapped within the same file */
325432b7c687SMark Fasheh 	if (same_inode) {
325532b7c687SMark Fasheh 		if (destoff + len > off && destoff < off + len)
325632b7c687SMark Fasheh 			goto out_unlock;
325732b7c687SMark Fasheh 	}
325832b7c687SMark Fasheh 
325932b7c687SMark Fasheh 	if (destoff > inode->i_size) {
326032b7c687SMark Fasheh 		ret = btrfs_cont_expand(inode, inode->i_size, destoff);
326132b7c687SMark Fasheh 		if (ret)
326232b7c687SMark Fasheh 			goto out_unlock;
326332b7c687SMark Fasheh 	}
326432b7c687SMark Fasheh 
326532b7c687SMark Fasheh 	/* truncate page cache pages from target inode range */
326632b7c687SMark Fasheh 	truncate_inode_pages_range(&inode->i_data, destoff,
326732b7c687SMark Fasheh 				   PAGE_CACHE_ALIGN(destoff + len) - 1);
326832b7c687SMark Fasheh 
326932b7c687SMark Fasheh 	lock_extent_range(src, off, len);
327032b7c687SMark Fasheh 
327132b7c687SMark Fasheh 	ret = btrfs_clone(src, inode, off, olen, len, destoff);
327232b7c687SMark Fasheh 
3273aa42ffd9SLiu Bo 	unlock_extent(&BTRFS_I(src)->io_tree, off, off + len - 1);
3274f46b5a66SChristoph Hellwig out_unlock:
3275c57c2b3eSFilipe David Borba Manana 	if (!same_inode) {
3276c57c2b3eSFilipe David Borba Manana 		if (inode < src) {
3277f46b5a66SChristoph Hellwig 			mutex_unlock(&src->i_mutex);
3278f46b5a66SChristoph Hellwig 			mutex_unlock(&inode->i_mutex);
3279c57c2b3eSFilipe David Borba Manana 		} else {
3280c57c2b3eSFilipe David Borba Manana 			mutex_unlock(&inode->i_mutex);
3281c57c2b3eSFilipe David Borba Manana 			mutex_unlock(&src->i_mutex);
3282c57c2b3eSFilipe David Borba Manana 		}
3283c57c2b3eSFilipe David Borba Manana 	} else {
3284c57c2b3eSFilipe David Borba Manana 		mutex_unlock(&src->i_mutex);
3285c57c2b3eSFilipe David Borba Manana 	}
3286f46b5a66SChristoph Hellwig out_fput:
32872903ff01SAl Viro 	fdput(src_file);
3288ab67b7c1SYan Zheng out_drop_write:
32892a79f17eSAl Viro 	mnt_drop_write_file(file);
3290f46b5a66SChristoph Hellwig 	return ret;
3291f46b5a66SChristoph Hellwig }
3292f46b5a66SChristoph Hellwig 
32937a865e8aSChristoph Hellwig static long btrfs_ioctl_clone_range(struct file *file, void __user *argp)
3294c5c9cd4dSSage Weil {
3295c5c9cd4dSSage Weil 	struct btrfs_ioctl_clone_range_args args;
3296c5c9cd4dSSage Weil 
32977a865e8aSChristoph Hellwig 	if (copy_from_user(&args, argp, sizeof(args)))
3298c5c9cd4dSSage Weil 		return -EFAULT;
3299c5c9cd4dSSage Weil 	return btrfs_ioctl_clone(file, args.src_fd, args.src_offset,
3300c5c9cd4dSSage Weil 				 args.src_length, args.dest_offset);
3301c5c9cd4dSSage Weil }
3302c5c9cd4dSSage Weil 
3303f46b5a66SChristoph Hellwig /*
3304f46b5a66SChristoph Hellwig  * there are many ways the trans_start and trans_end ioctls can lead
3305f46b5a66SChristoph Hellwig  * to deadlocks.  They should only be used by applications that
3306f46b5a66SChristoph Hellwig  * basically own the machine, and have a very in depth understanding
3307f46b5a66SChristoph Hellwig  * of all the possible deadlocks and enospc problems.
3308f46b5a66SChristoph Hellwig  */
3309b2950863SChristoph Hellwig static long btrfs_ioctl_trans_start(struct file *file)
3310f46b5a66SChristoph Hellwig {
3311496ad9aaSAl Viro 	struct inode *inode = file_inode(file);
3312f46b5a66SChristoph Hellwig 	struct btrfs_root *root = BTRFS_I(inode)->root;
3313f46b5a66SChristoph Hellwig 	struct btrfs_trans_handle *trans;
33141ab86aedSSage Weil 	int ret;
3315f46b5a66SChristoph Hellwig 
33161ab86aedSSage Weil 	ret = -EPERM;
3317df5b5520SChristoph Hellwig 	if (!capable(CAP_SYS_ADMIN))
3318f46b5a66SChristoph Hellwig 		goto out;
33191ab86aedSSage Weil 
33201ab86aedSSage Weil 	ret = -EINPROGRESS;
33211ab86aedSSage Weil 	if (file->private_data)
33221ab86aedSSage Weil 		goto out;
33239ca9ee09SSage Weil 
3324b83cc969SLi Zefan 	ret = -EROFS;
3325b83cc969SLi Zefan 	if (btrfs_root_readonly(root))
3326b83cc969SLi Zefan 		goto out;
3327b83cc969SLi Zefan 
3328a561be71SAl Viro 	ret = mnt_want_write_file(file);
3329c146afadSYan Zheng 	if (ret)
3330c146afadSYan Zheng 		goto out;
3331c146afadSYan Zheng 
3332a4abeea4SJosef Bacik 	atomic_inc(&root->fs_info->open_ioctl_trans);
33339ca9ee09SSage Weil 
3334f46b5a66SChristoph Hellwig 	ret = -ENOMEM;
33357a7eaa40SJosef Bacik 	trans = btrfs_start_ioctl_transaction(root);
3336abd30bb0STsutomu Itoh 	if (IS_ERR(trans))
33371ab86aedSSage Weil 		goto out_drop;
33381ab86aedSSage Weil 
33391ab86aedSSage Weil 	file->private_data = trans;
33401ab86aedSSage Weil 	return 0;
33411ab86aedSSage Weil 
33421ab86aedSSage Weil out_drop:
3343a4abeea4SJosef Bacik 	atomic_dec(&root->fs_info->open_ioctl_trans);
33442a79f17eSAl Viro 	mnt_drop_write_file(file);
3345f46b5a66SChristoph Hellwig out:
3346f46b5a66SChristoph Hellwig 	return ret;
3347f46b5a66SChristoph Hellwig }
3348f46b5a66SChristoph Hellwig 
33496ef5ed0dSJosef Bacik static long btrfs_ioctl_default_subvol(struct file *file, void __user *argp)
33506ef5ed0dSJosef Bacik {
3351496ad9aaSAl Viro 	struct inode *inode = file_inode(file);
33526ef5ed0dSJosef Bacik 	struct btrfs_root *root = BTRFS_I(inode)->root;
33536ef5ed0dSJosef Bacik 	struct btrfs_root *new_root;
33546ef5ed0dSJosef Bacik 	struct btrfs_dir_item *di;
33556ef5ed0dSJosef Bacik 	struct btrfs_trans_handle *trans;
33566ef5ed0dSJosef Bacik 	struct btrfs_path *path;
33576ef5ed0dSJosef Bacik 	struct btrfs_key location;
33586ef5ed0dSJosef Bacik 	struct btrfs_disk_key disk_key;
33596ef5ed0dSJosef Bacik 	u64 objectid = 0;
33606ef5ed0dSJosef Bacik 	u64 dir_id;
33613c04ce01SMiao Xie 	int ret;
33626ef5ed0dSJosef Bacik 
33636ef5ed0dSJosef Bacik 	if (!capable(CAP_SYS_ADMIN))
33646ef5ed0dSJosef Bacik 		return -EPERM;
33656ef5ed0dSJosef Bacik 
33663c04ce01SMiao Xie 	ret = mnt_want_write_file(file);
33673c04ce01SMiao Xie 	if (ret)
33683c04ce01SMiao Xie 		return ret;
33693c04ce01SMiao Xie 
33703c04ce01SMiao Xie 	if (copy_from_user(&objectid, argp, sizeof(objectid))) {
33713c04ce01SMiao Xie 		ret = -EFAULT;
33723c04ce01SMiao Xie 		goto out;
33733c04ce01SMiao Xie 	}
33746ef5ed0dSJosef Bacik 
33756ef5ed0dSJosef Bacik 	if (!objectid)
33761cecf579Schandan 		objectid = BTRFS_FS_TREE_OBJECTID;
33776ef5ed0dSJosef Bacik 
33786ef5ed0dSJosef Bacik 	location.objectid = objectid;
33796ef5ed0dSJosef Bacik 	location.type = BTRFS_ROOT_ITEM_KEY;
33806ef5ed0dSJosef Bacik 	location.offset = (u64)-1;
33816ef5ed0dSJosef Bacik 
33826ef5ed0dSJosef Bacik 	new_root = btrfs_read_fs_root_no_name(root->fs_info, &location);
33833c04ce01SMiao Xie 	if (IS_ERR(new_root)) {
33843c04ce01SMiao Xie 		ret = PTR_ERR(new_root);
33853c04ce01SMiao Xie 		goto out;
33863c04ce01SMiao Xie 	}
33876ef5ed0dSJosef Bacik 
33886ef5ed0dSJosef Bacik 	path = btrfs_alloc_path();
33893c04ce01SMiao Xie 	if (!path) {
33903c04ce01SMiao Xie 		ret = -ENOMEM;
33913c04ce01SMiao Xie 		goto out;
33923c04ce01SMiao Xie 	}
33936ef5ed0dSJosef Bacik 	path->leave_spinning = 1;
33946ef5ed0dSJosef Bacik 
33956ef5ed0dSJosef Bacik 	trans = btrfs_start_transaction(root, 1);
339698d5dc13STsutomu Itoh 	if (IS_ERR(trans)) {
33976ef5ed0dSJosef Bacik 		btrfs_free_path(path);
33983c04ce01SMiao Xie 		ret = PTR_ERR(trans);
33993c04ce01SMiao Xie 		goto out;
34006ef5ed0dSJosef Bacik 	}
34016ef5ed0dSJosef Bacik 
34026c41761fSDavid Sterba 	dir_id = btrfs_super_root_dir(root->fs_info->super_copy);
34036ef5ed0dSJosef Bacik 	di = btrfs_lookup_dir_item(trans, root->fs_info->tree_root, path,
34046ef5ed0dSJosef Bacik 				   dir_id, "default", 7, 1);
3405cf1e99a4SDan Carpenter 	if (IS_ERR_OR_NULL(di)) {
34066ef5ed0dSJosef Bacik 		btrfs_free_path(path);
34076ef5ed0dSJosef Bacik 		btrfs_end_transaction(trans, root);
3408efe120a0SFrank Holton 		btrfs_err(new_root->fs_info, "Umm, you don't have the default dir"
3409efe120a0SFrank Holton 			   "item, this isn't going to work");
34103c04ce01SMiao Xie 		ret = -ENOENT;
34113c04ce01SMiao Xie 		goto out;
34126ef5ed0dSJosef Bacik 	}
34136ef5ed0dSJosef Bacik 
34146ef5ed0dSJosef Bacik 	btrfs_cpu_key_to_disk(&disk_key, &new_root->root_key);
34156ef5ed0dSJosef Bacik 	btrfs_set_dir_item_key(path->nodes[0], di, &disk_key);
34166ef5ed0dSJosef Bacik 	btrfs_mark_buffer_dirty(path->nodes[0]);
34176ef5ed0dSJosef Bacik 	btrfs_free_path(path);
34186ef5ed0dSJosef Bacik 
34192b0ce2c2SMitch Harder 	btrfs_set_fs_incompat(root->fs_info, DEFAULT_SUBVOL);
34206ef5ed0dSJosef Bacik 	btrfs_end_transaction(trans, root);
34213c04ce01SMiao Xie out:
34223c04ce01SMiao Xie 	mnt_drop_write_file(file);
34233c04ce01SMiao Xie 	return ret;
34246ef5ed0dSJosef Bacik }
34256ef5ed0dSJosef Bacik 
34265af3e8ccSStefan Behrens void btrfs_get_block_group_info(struct list_head *groups_list,
3427bf5fc093SJosef Bacik 				struct btrfs_ioctl_space_info *space)
3428bf5fc093SJosef Bacik {
3429bf5fc093SJosef Bacik 	struct btrfs_block_group_cache *block_group;
3430bf5fc093SJosef Bacik 
3431bf5fc093SJosef Bacik 	space->total_bytes = 0;
3432bf5fc093SJosef Bacik 	space->used_bytes = 0;
3433bf5fc093SJosef Bacik 	space->flags = 0;
3434bf5fc093SJosef Bacik 	list_for_each_entry(block_group, groups_list, list) {
3435bf5fc093SJosef Bacik 		space->flags = block_group->flags;
3436bf5fc093SJosef Bacik 		space->total_bytes += block_group->key.offset;
3437bf5fc093SJosef Bacik 		space->used_bytes +=
3438bf5fc093SJosef Bacik 			btrfs_block_group_used(&block_group->item);
3439bf5fc093SJosef Bacik 	}
3440bf5fc093SJosef Bacik }
3441bf5fc093SJosef Bacik 
344248a3b636SEric Sandeen static long btrfs_ioctl_space_info(struct btrfs_root *root, void __user *arg)
34431406e432SJosef Bacik {
34441406e432SJosef Bacik 	struct btrfs_ioctl_space_args space_args;
34451406e432SJosef Bacik 	struct btrfs_ioctl_space_info space;
34461406e432SJosef Bacik 	struct btrfs_ioctl_space_info *dest;
34477fde62bfSChris Mason 	struct btrfs_ioctl_space_info *dest_orig;
344813f2696fSDaniel J Blueman 	struct btrfs_ioctl_space_info __user *user_dest;
34491406e432SJosef Bacik 	struct btrfs_space_info *info;
3450bf5fc093SJosef Bacik 	u64 types[] = {BTRFS_BLOCK_GROUP_DATA,
3451bf5fc093SJosef Bacik 		       BTRFS_BLOCK_GROUP_SYSTEM,
3452bf5fc093SJosef Bacik 		       BTRFS_BLOCK_GROUP_METADATA,
3453bf5fc093SJosef Bacik 		       BTRFS_BLOCK_GROUP_DATA | BTRFS_BLOCK_GROUP_METADATA};
3454bf5fc093SJosef Bacik 	int num_types = 4;
34557fde62bfSChris Mason 	int alloc_size;
34561406e432SJosef Bacik 	int ret = 0;
345751788b1bSDan Rosenberg 	u64 slot_count = 0;
3458bf5fc093SJosef Bacik 	int i, c;
34591406e432SJosef Bacik 
34601406e432SJosef Bacik 	if (copy_from_user(&space_args,
34611406e432SJosef Bacik 			   (struct btrfs_ioctl_space_args __user *)arg,
34621406e432SJosef Bacik 			   sizeof(space_args)))
34631406e432SJosef Bacik 		return -EFAULT;
34641406e432SJosef Bacik 
3465bf5fc093SJosef Bacik 	for (i = 0; i < num_types; i++) {
3466bf5fc093SJosef Bacik 		struct btrfs_space_info *tmp;
3467bf5fc093SJosef Bacik 
3468bf5fc093SJosef Bacik 		info = NULL;
34697fde62bfSChris Mason 		rcu_read_lock();
3470bf5fc093SJosef Bacik 		list_for_each_entry_rcu(tmp, &root->fs_info->space_info,
3471bf5fc093SJosef Bacik 					list) {
3472bf5fc093SJosef Bacik 			if (tmp->flags == types[i]) {
3473bf5fc093SJosef Bacik 				info = tmp;
3474bf5fc093SJosef Bacik 				break;
3475bf5fc093SJosef Bacik 			}
3476bf5fc093SJosef Bacik 		}
34777fde62bfSChris Mason 		rcu_read_unlock();
34781406e432SJosef Bacik 
3479bf5fc093SJosef Bacik 		if (!info)
3480bf5fc093SJosef Bacik 			continue;
3481bf5fc093SJosef Bacik 
3482bf5fc093SJosef Bacik 		down_read(&info->groups_sem);
3483bf5fc093SJosef Bacik 		for (c = 0; c < BTRFS_NR_RAID_TYPES; c++) {
3484bf5fc093SJosef Bacik 			if (!list_empty(&info->block_groups[c]))
3485bf5fc093SJosef Bacik 				slot_count++;
3486bf5fc093SJosef Bacik 		}
3487bf5fc093SJosef Bacik 		up_read(&info->groups_sem);
3488bf5fc093SJosef Bacik 	}
3489bf5fc093SJosef Bacik 
34907fde62bfSChris Mason 	/* space_slots == 0 means they are asking for a count */
34917fde62bfSChris Mason 	if (space_args.space_slots == 0) {
34927fde62bfSChris Mason 		space_args.total_spaces = slot_count;
34937fde62bfSChris Mason 		goto out;
34947fde62bfSChris Mason 	}
3495bf5fc093SJosef Bacik 
349651788b1bSDan Rosenberg 	slot_count = min_t(u64, space_args.space_slots, slot_count);
3497bf5fc093SJosef Bacik 
34987fde62bfSChris Mason 	alloc_size = sizeof(*dest) * slot_count;
3499bf5fc093SJosef Bacik 
35007fde62bfSChris Mason 	/* we generally have at most 6 or so space infos, one for each raid
35017fde62bfSChris Mason 	 * level.  So, a whole page should be more than enough for everyone
35027fde62bfSChris Mason 	 */
35037fde62bfSChris Mason 	if (alloc_size > PAGE_CACHE_SIZE)
35047fde62bfSChris Mason 		return -ENOMEM;
35057fde62bfSChris Mason 
35067fde62bfSChris Mason 	space_args.total_spaces = 0;
35077fde62bfSChris Mason 	dest = kmalloc(alloc_size, GFP_NOFS);
35087fde62bfSChris Mason 	if (!dest)
35097fde62bfSChris Mason 		return -ENOMEM;
35107fde62bfSChris Mason 	dest_orig = dest;
35117fde62bfSChris Mason 
35127fde62bfSChris Mason 	/* now we have a buffer to copy into */
3513bf5fc093SJosef Bacik 	for (i = 0; i < num_types; i++) {
3514bf5fc093SJosef Bacik 		struct btrfs_space_info *tmp;
3515bf5fc093SJosef Bacik 
351651788b1bSDan Rosenberg 		if (!slot_count)
351751788b1bSDan Rosenberg 			break;
351851788b1bSDan Rosenberg 
3519bf5fc093SJosef Bacik 		info = NULL;
35201406e432SJosef Bacik 		rcu_read_lock();
3521bf5fc093SJosef Bacik 		list_for_each_entry_rcu(tmp, &root->fs_info->space_info,
3522bf5fc093SJosef Bacik 					list) {
3523bf5fc093SJosef Bacik 			if (tmp->flags == types[i]) {
3524bf5fc093SJosef Bacik 				info = tmp;
35257fde62bfSChris Mason 				break;
3526bf5fc093SJosef Bacik 			}
3527bf5fc093SJosef Bacik 		}
3528bf5fc093SJosef Bacik 		rcu_read_unlock();
35297fde62bfSChris Mason 
3530bf5fc093SJosef Bacik 		if (!info)
3531bf5fc093SJosef Bacik 			continue;
3532bf5fc093SJosef Bacik 		down_read(&info->groups_sem);
3533bf5fc093SJosef Bacik 		for (c = 0; c < BTRFS_NR_RAID_TYPES; c++) {
3534bf5fc093SJosef Bacik 			if (!list_empty(&info->block_groups[c])) {
35355af3e8ccSStefan Behrens 				btrfs_get_block_group_info(
35365af3e8ccSStefan Behrens 					&info->block_groups[c], &space);
35377fde62bfSChris Mason 				memcpy(dest, &space, sizeof(space));
35381406e432SJosef Bacik 				dest++;
35391406e432SJosef Bacik 				space_args.total_spaces++;
354051788b1bSDan Rosenberg 				slot_count--;
35411406e432SJosef Bacik 			}
354251788b1bSDan Rosenberg 			if (!slot_count)
354351788b1bSDan Rosenberg 				break;
3544bf5fc093SJosef Bacik 		}
3545bf5fc093SJosef Bacik 		up_read(&info->groups_sem);
3546bf5fc093SJosef Bacik 	}
35471406e432SJosef Bacik 
35482eec6c81SDaniel J Blueman 	user_dest = (struct btrfs_ioctl_space_info __user *)
35497fde62bfSChris Mason 		(arg + sizeof(struct btrfs_ioctl_space_args));
35507fde62bfSChris Mason 
35517fde62bfSChris Mason 	if (copy_to_user(user_dest, dest_orig, alloc_size))
35527fde62bfSChris Mason 		ret = -EFAULT;
35537fde62bfSChris Mason 
35547fde62bfSChris Mason 	kfree(dest_orig);
35557fde62bfSChris Mason out:
35567fde62bfSChris Mason 	if (ret == 0 && copy_to_user(arg, &space_args, sizeof(space_args)))
35571406e432SJosef Bacik 		ret = -EFAULT;
35581406e432SJosef Bacik 
35591406e432SJosef Bacik 	return ret;
35601406e432SJosef Bacik }
35611406e432SJosef Bacik 
356201e219e8SJeff Mahoney static long btrfs_ioctl_global_rsv(struct btrfs_root *root, void __user *arg)
356301e219e8SJeff Mahoney {
356401e219e8SJeff Mahoney 	struct btrfs_block_rsv *block_rsv = &root->fs_info->global_block_rsv;
356501e219e8SJeff Mahoney 	u64 reserved;
356601e219e8SJeff Mahoney 
356701e219e8SJeff Mahoney 	spin_lock(&block_rsv->lock);
356801e219e8SJeff Mahoney 	reserved = block_rsv->reserved;
356901e219e8SJeff Mahoney 	spin_unlock(&block_rsv->lock);
357001e219e8SJeff Mahoney 
357101e219e8SJeff Mahoney 	if (arg && copy_to_user(arg, &reserved, sizeof(reserved)))
357201e219e8SJeff Mahoney 		return -EFAULT;
357301e219e8SJeff Mahoney 	return 0;
357401e219e8SJeff Mahoney }
357501e219e8SJeff Mahoney 
3576f46b5a66SChristoph Hellwig /*
3577f46b5a66SChristoph Hellwig  * there are many ways the trans_start and trans_end ioctls can lead
3578f46b5a66SChristoph Hellwig  * to deadlocks.  They should only be used by applications that
3579f46b5a66SChristoph Hellwig  * basically own the machine, and have a very in depth understanding
3580f46b5a66SChristoph Hellwig  * of all the possible deadlocks and enospc problems.
3581f46b5a66SChristoph Hellwig  */
3582f46b5a66SChristoph Hellwig long btrfs_ioctl_trans_end(struct file *file)
3583f46b5a66SChristoph Hellwig {
3584496ad9aaSAl Viro 	struct inode *inode = file_inode(file);
3585f46b5a66SChristoph Hellwig 	struct btrfs_root *root = BTRFS_I(inode)->root;
3586f46b5a66SChristoph Hellwig 	struct btrfs_trans_handle *trans;
3587f46b5a66SChristoph Hellwig 
3588f46b5a66SChristoph Hellwig 	trans = file->private_data;
35891ab86aedSSage Weil 	if (!trans)
35901ab86aedSSage Weil 		return -EINVAL;
3591b214107eSChristoph Hellwig 	file->private_data = NULL;
35929ca9ee09SSage Weil 
35931ab86aedSSage Weil 	btrfs_end_transaction(trans, root);
35941ab86aedSSage Weil 
3595a4abeea4SJosef Bacik 	atomic_dec(&root->fs_info->open_ioctl_trans);
35969ca9ee09SSage Weil 
35972a79f17eSAl Viro 	mnt_drop_write_file(file);
35981ab86aedSSage Weil 	return 0;
3599f46b5a66SChristoph Hellwig }
3600f46b5a66SChristoph Hellwig 
36019a8c28beSMiao Xie static noinline long btrfs_ioctl_start_sync(struct btrfs_root *root,
36029a8c28beSMiao Xie 					    void __user *argp)
360346204592SSage Weil {
360446204592SSage Weil 	struct btrfs_trans_handle *trans;
360546204592SSage Weil 	u64 transid;
3606db5b493aSTsutomu Itoh 	int ret;
360746204592SSage Weil 
3608d4edf39bSMiao Xie 	trans = btrfs_attach_transaction_barrier(root);
3609ff7c1d33SMiao Xie 	if (IS_ERR(trans)) {
3610ff7c1d33SMiao Xie 		if (PTR_ERR(trans) != -ENOENT)
361198d5dc13STsutomu Itoh 			return PTR_ERR(trans);
3612ff7c1d33SMiao Xie 
3613ff7c1d33SMiao Xie 		/* No running transaction, don't bother */
3614ff7c1d33SMiao Xie 		transid = root->fs_info->last_trans_committed;
3615ff7c1d33SMiao Xie 		goto out;
3616ff7c1d33SMiao Xie 	}
361746204592SSage Weil 	transid = trans->transid;
3618db5b493aSTsutomu Itoh 	ret = btrfs_commit_transaction_async(trans, root, 0);
36198b2b2d3cSTsutomu Itoh 	if (ret) {
36208b2b2d3cSTsutomu Itoh 		btrfs_end_transaction(trans, root);
3621db5b493aSTsutomu Itoh 		return ret;
36228b2b2d3cSTsutomu Itoh 	}
3623ff7c1d33SMiao Xie out:
362446204592SSage Weil 	if (argp)
362546204592SSage Weil 		if (copy_to_user(argp, &transid, sizeof(transid)))
362646204592SSage Weil 			return -EFAULT;
362746204592SSage Weil 	return 0;
362846204592SSage Weil }
362946204592SSage Weil 
36309a8c28beSMiao Xie static noinline long btrfs_ioctl_wait_sync(struct btrfs_root *root,
36319a8c28beSMiao Xie 					   void __user *argp)
363246204592SSage Weil {
363346204592SSage Weil 	u64 transid;
363446204592SSage Weil 
363546204592SSage Weil 	if (argp) {
363646204592SSage Weil 		if (copy_from_user(&transid, argp, sizeof(transid)))
363746204592SSage Weil 			return -EFAULT;
363846204592SSage Weil 	} else {
363946204592SSage Weil 		transid = 0;  /* current trans */
364046204592SSage Weil 	}
364146204592SSage Weil 	return btrfs_wait_for_commit(root, transid);
364246204592SSage Weil }
364346204592SSage Weil 
3644b8e95489SMiao Xie static long btrfs_ioctl_scrub(struct file *file, void __user *arg)
3645475f6387SJan Schmidt {
3646496ad9aaSAl Viro 	struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
3647475f6387SJan Schmidt 	struct btrfs_ioctl_scrub_args *sa;
3648b8e95489SMiao Xie 	int ret;
3649475f6387SJan Schmidt 
3650475f6387SJan Schmidt 	if (!capable(CAP_SYS_ADMIN))
3651475f6387SJan Schmidt 		return -EPERM;
3652475f6387SJan Schmidt 
3653475f6387SJan Schmidt 	sa = memdup_user(arg, sizeof(*sa));
3654475f6387SJan Schmidt 	if (IS_ERR(sa))
3655475f6387SJan Schmidt 		return PTR_ERR(sa);
3656475f6387SJan Schmidt 
3657b8e95489SMiao Xie 	if (!(sa->flags & BTRFS_SCRUB_READONLY)) {
3658b8e95489SMiao Xie 		ret = mnt_want_write_file(file);
3659b8e95489SMiao Xie 		if (ret)
3660b8e95489SMiao Xie 			goto out;
3661b8e95489SMiao Xie 	}
3662b8e95489SMiao Xie 
3663aa1b8cd4SStefan Behrens 	ret = btrfs_scrub_dev(root->fs_info, sa->devid, sa->start, sa->end,
366463a212abSStefan Behrens 			      &sa->progress, sa->flags & BTRFS_SCRUB_READONLY,
366563a212abSStefan Behrens 			      0);
3666475f6387SJan Schmidt 
3667475f6387SJan Schmidt 	if (copy_to_user(arg, sa, sizeof(*sa)))
3668475f6387SJan Schmidt 		ret = -EFAULT;
3669475f6387SJan Schmidt 
3670b8e95489SMiao Xie 	if (!(sa->flags & BTRFS_SCRUB_READONLY))
3671b8e95489SMiao Xie 		mnt_drop_write_file(file);
3672b8e95489SMiao Xie out:
3673475f6387SJan Schmidt 	kfree(sa);
3674475f6387SJan Schmidt 	return ret;
3675475f6387SJan Schmidt }
3676475f6387SJan Schmidt 
3677475f6387SJan Schmidt static long btrfs_ioctl_scrub_cancel(struct btrfs_root *root, void __user *arg)
3678475f6387SJan Schmidt {
3679475f6387SJan Schmidt 	if (!capable(CAP_SYS_ADMIN))
3680475f6387SJan Schmidt 		return -EPERM;
3681475f6387SJan Schmidt 
3682aa1b8cd4SStefan Behrens 	return btrfs_scrub_cancel(root->fs_info);
3683475f6387SJan Schmidt }
3684475f6387SJan Schmidt 
3685475f6387SJan Schmidt static long btrfs_ioctl_scrub_progress(struct btrfs_root *root,
3686475f6387SJan Schmidt 				       void __user *arg)
3687475f6387SJan Schmidt {
3688475f6387SJan Schmidt 	struct btrfs_ioctl_scrub_args *sa;
3689475f6387SJan Schmidt 	int ret;
3690475f6387SJan Schmidt 
3691475f6387SJan Schmidt 	if (!capable(CAP_SYS_ADMIN))
3692475f6387SJan Schmidt 		return -EPERM;
3693475f6387SJan Schmidt 
3694475f6387SJan Schmidt 	sa = memdup_user(arg, sizeof(*sa));
3695475f6387SJan Schmidt 	if (IS_ERR(sa))
3696475f6387SJan Schmidt 		return PTR_ERR(sa);
3697475f6387SJan Schmidt 
3698475f6387SJan Schmidt 	ret = btrfs_scrub_progress(root, sa->devid, &sa->progress);
3699475f6387SJan Schmidt 
3700475f6387SJan Schmidt 	if (copy_to_user(arg, sa, sizeof(*sa)))
3701475f6387SJan Schmidt 		ret = -EFAULT;
3702475f6387SJan Schmidt 
3703475f6387SJan Schmidt 	kfree(sa);
3704475f6387SJan Schmidt 	return ret;
3705475f6387SJan Schmidt }
3706475f6387SJan Schmidt 
3707c11d2c23SStefan Behrens static long btrfs_ioctl_get_dev_stats(struct btrfs_root *root,
3708b27f7c0cSDavid Sterba 				      void __user *arg)
3709c11d2c23SStefan Behrens {
3710c11d2c23SStefan Behrens 	struct btrfs_ioctl_get_dev_stats *sa;
3711c11d2c23SStefan Behrens 	int ret;
3712c11d2c23SStefan Behrens 
3713c11d2c23SStefan Behrens 	sa = memdup_user(arg, sizeof(*sa));
3714c11d2c23SStefan Behrens 	if (IS_ERR(sa))
3715c11d2c23SStefan Behrens 		return PTR_ERR(sa);
3716c11d2c23SStefan Behrens 
3717b27f7c0cSDavid Sterba 	if ((sa->flags & BTRFS_DEV_STATS_RESET) && !capable(CAP_SYS_ADMIN)) {
3718b27f7c0cSDavid Sterba 		kfree(sa);
3719b27f7c0cSDavid Sterba 		return -EPERM;
3720b27f7c0cSDavid Sterba 	}
3721b27f7c0cSDavid Sterba 
3722b27f7c0cSDavid Sterba 	ret = btrfs_get_dev_stats(root, sa);
3723c11d2c23SStefan Behrens 
3724c11d2c23SStefan Behrens 	if (copy_to_user(arg, sa, sizeof(*sa)))
3725c11d2c23SStefan Behrens 		ret = -EFAULT;
3726c11d2c23SStefan Behrens 
3727c11d2c23SStefan Behrens 	kfree(sa);
3728c11d2c23SStefan Behrens 	return ret;
3729c11d2c23SStefan Behrens }
3730c11d2c23SStefan Behrens 
37313f6bcfbdSStefan Behrens static long btrfs_ioctl_dev_replace(struct btrfs_root *root, void __user *arg)
37323f6bcfbdSStefan Behrens {
37333f6bcfbdSStefan Behrens 	struct btrfs_ioctl_dev_replace_args *p;
37343f6bcfbdSStefan Behrens 	int ret;
37353f6bcfbdSStefan Behrens 
37363f6bcfbdSStefan Behrens 	if (!capable(CAP_SYS_ADMIN))
37373f6bcfbdSStefan Behrens 		return -EPERM;
37383f6bcfbdSStefan Behrens 
37393f6bcfbdSStefan Behrens 	p = memdup_user(arg, sizeof(*p));
37403f6bcfbdSStefan Behrens 	if (IS_ERR(p))
37413f6bcfbdSStefan Behrens 		return PTR_ERR(p);
37423f6bcfbdSStefan Behrens 
37433f6bcfbdSStefan Behrens 	switch (p->cmd) {
37443f6bcfbdSStefan Behrens 	case BTRFS_IOCTL_DEV_REPLACE_CMD_START:
3745adfa97cbSIlya Dryomov 		if (root->fs_info->sb->s_flags & MS_RDONLY) {
3746adfa97cbSIlya Dryomov 			ret = -EROFS;
3747adfa97cbSIlya Dryomov 			goto out;
3748adfa97cbSIlya Dryomov 		}
37493f6bcfbdSStefan Behrens 		if (atomic_xchg(
37503f6bcfbdSStefan Behrens 			&root->fs_info->mutually_exclusive_operation_running,
37513f6bcfbdSStefan Behrens 			1)) {
3752e57138b3SAnand Jain 			ret = BTRFS_ERROR_DEV_EXCL_RUN_IN_PROGRESS;
37533f6bcfbdSStefan Behrens 		} else {
37543f6bcfbdSStefan Behrens 			ret = btrfs_dev_replace_start(root, p);
37553f6bcfbdSStefan Behrens 			atomic_set(
37563f6bcfbdSStefan Behrens 			 &root->fs_info->mutually_exclusive_operation_running,
37573f6bcfbdSStefan Behrens 			 0);
37583f6bcfbdSStefan Behrens 		}
37593f6bcfbdSStefan Behrens 		break;
37603f6bcfbdSStefan Behrens 	case BTRFS_IOCTL_DEV_REPLACE_CMD_STATUS:
37613f6bcfbdSStefan Behrens 		btrfs_dev_replace_status(root->fs_info, p);
37623f6bcfbdSStefan Behrens 		ret = 0;
37633f6bcfbdSStefan Behrens 		break;
37643f6bcfbdSStefan Behrens 	case BTRFS_IOCTL_DEV_REPLACE_CMD_CANCEL:
37653f6bcfbdSStefan Behrens 		ret = btrfs_dev_replace_cancel(root->fs_info, p);
37663f6bcfbdSStefan Behrens 		break;
37673f6bcfbdSStefan Behrens 	default:
37683f6bcfbdSStefan Behrens 		ret = -EINVAL;
37693f6bcfbdSStefan Behrens 		break;
37703f6bcfbdSStefan Behrens 	}
37713f6bcfbdSStefan Behrens 
37723f6bcfbdSStefan Behrens 	if (copy_to_user(arg, p, sizeof(*p)))
37733f6bcfbdSStefan Behrens 		ret = -EFAULT;
3774adfa97cbSIlya Dryomov out:
37753f6bcfbdSStefan Behrens 	kfree(p);
37763f6bcfbdSStefan Behrens 	return ret;
37773f6bcfbdSStefan Behrens }
37783f6bcfbdSStefan Behrens 
3779d7728c96SJan Schmidt static long btrfs_ioctl_ino_to_path(struct btrfs_root *root, void __user *arg)
3780d7728c96SJan Schmidt {
3781d7728c96SJan Schmidt 	int ret = 0;
3782d7728c96SJan Schmidt 	int i;
3783740c3d22SChris Mason 	u64 rel_ptr;
3784d7728c96SJan Schmidt 	int size;
3785806468f8SChris Mason 	struct btrfs_ioctl_ino_path_args *ipa = NULL;
3786d7728c96SJan Schmidt 	struct inode_fs_paths *ipath = NULL;
3787d7728c96SJan Schmidt 	struct btrfs_path *path;
3788d7728c96SJan Schmidt 
378982b22ac8SKusanagi Kouichi 	if (!capable(CAP_DAC_READ_SEARCH))
3790d7728c96SJan Schmidt 		return -EPERM;
3791d7728c96SJan Schmidt 
3792d7728c96SJan Schmidt 	path = btrfs_alloc_path();
3793d7728c96SJan Schmidt 	if (!path) {
3794d7728c96SJan Schmidt 		ret = -ENOMEM;
3795d7728c96SJan Schmidt 		goto out;
3796d7728c96SJan Schmidt 	}
3797d7728c96SJan Schmidt 
3798d7728c96SJan Schmidt 	ipa = memdup_user(arg, sizeof(*ipa));
3799d7728c96SJan Schmidt 	if (IS_ERR(ipa)) {
3800d7728c96SJan Schmidt 		ret = PTR_ERR(ipa);
3801d7728c96SJan Schmidt 		ipa = NULL;
3802d7728c96SJan Schmidt 		goto out;
3803d7728c96SJan Schmidt 	}
3804d7728c96SJan Schmidt 
3805d7728c96SJan Schmidt 	size = min_t(u32, ipa->size, 4096);
3806d7728c96SJan Schmidt 	ipath = init_ipath(size, root, path);
3807d7728c96SJan Schmidt 	if (IS_ERR(ipath)) {
3808d7728c96SJan Schmidt 		ret = PTR_ERR(ipath);
3809d7728c96SJan Schmidt 		ipath = NULL;
3810d7728c96SJan Schmidt 		goto out;
3811d7728c96SJan Schmidt 	}
3812d7728c96SJan Schmidt 
3813d7728c96SJan Schmidt 	ret = paths_from_inode(ipa->inum, ipath);
3814d7728c96SJan Schmidt 	if (ret < 0)
3815d7728c96SJan Schmidt 		goto out;
3816d7728c96SJan Schmidt 
3817d7728c96SJan Schmidt 	for (i = 0; i < ipath->fspath->elem_cnt; ++i) {
3818745c4d8eSJeff Mahoney 		rel_ptr = ipath->fspath->val[i] -
3819745c4d8eSJeff Mahoney 			  (u64)(unsigned long)ipath->fspath->val;
3820740c3d22SChris Mason 		ipath->fspath->val[i] = rel_ptr;
3821d7728c96SJan Schmidt 	}
3822d7728c96SJan Schmidt 
3823745c4d8eSJeff Mahoney 	ret = copy_to_user((void *)(unsigned long)ipa->fspath,
3824745c4d8eSJeff Mahoney 			   (void *)(unsigned long)ipath->fspath, size);
3825d7728c96SJan Schmidt 	if (ret) {
3826d7728c96SJan Schmidt 		ret = -EFAULT;
3827d7728c96SJan Schmidt 		goto out;
3828d7728c96SJan Schmidt 	}
3829d7728c96SJan Schmidt 
3830d7728c96SJan Schmidt out:
3831d7728c96SJan Schmidt 	btrfs_free_path(path);
3832d7728c96SJan Schmidt 	free_ipath(ipath);
3833d7728c96SJan Schmidt 	kfree(ipa);
3834d7728c96SJan Schmidt 
3835d7728c96SJan Schmidt 	return ret;
3836d7728c96SJan Schmidt }
3837d7728c96SJan Schmidt 
3838d7728c96SJan Schmidt static int build_ino_list(u64 inum, u64 offset, u64 root, void *ctx)
3839d7728c96SJan Schmidt {
3840d7728c96SJan Schmidt 	struct btrfs_data_container *inodes = ctx;
3841d7728c96SJan Schmidt 	const size_t c = 3 * sizeof(u64);
3842d7728c96SJan Schmidt 
3843d7728c96SJan Schmidt 	if (inodes->bytes_left >= c) {
3844d7728c96SJan Schmidt 		inodes->bytes_left -= c;
3845d7728c96SJan Schmidt 		inodes->val[inodes->elem_cnt] = inum;
3846d7728c96SJan Schmidt 		inodes->val[inodes->elem_cnt + 1] = offset;
3847d7728c96SJan Schmidt 		inodes->val[inodes->elem_cnt + 2] = root;
3848d7728c96SJan Schmidt 		inodes->elem_cnt += 3;
3849d7728c96SJan Schmidt 	} else {
3850d7728c96SJan Schmidt 		inodes->bytes_missing += c - inodes->bytes_left;
3851d7728c96SJan Schmidt 		inodes->bytes_left = 0;
3852d7728c96SJan Schmidt 		inodes->elem_missed += 3;
3853d7728c96SJan Schmidt 	}
3854d7728c96SJan Schmidt 
3855d7728c96SJan Schmidt 	return 0;
3856d7728c96SJan Schmidt }
3857d7728c96SJan Schmidt 
3858d7728c96SJan Schmidt static long btrfs_ioctl_logical_to_ino(struct btrfs_root *root,
3859d7728c96SJan Schmidt 					void __user *arg)
3860d7728c96SJan Schmidt {
3861d7728c96SJan Schmidt 	int ret = 0;
3862d7728c96SJan Schmidt 	int size;
3863d7728c96SJan Schmidt 	struct btrfs_ioctl_logical_ino_args *loi;
3864d7728c96SJan Schmidt 	struct btrfs_data_container *inodes = NULL;
3865d7728c96SJan Schmidt 	struct btrfs_path *path = NULL;
3866d7728c96SJan Schmidt 
3867d7728c96SJan Schmidt 	if (!capable(CAP_SYS_ADMIN))
3868d7728c96SJan Schmidt 		return -EPERM;
3869d7728c96SJan Schmidt 
3870d7728c96SJan Schmidt 	loi = memdup_user(arg, sizeof(*loi));
3871d7728c96SJan Schmidt 	if (IS_ERR(loi)) {
3872d7728c96SJan Schmidt 		ret = PTR_ERR(loi);
3873d7728c96SJan Schmidt 		loi = NULL;
3874d7728c96SJan Schmidt 		goto out;
3875d7728c96SJan Schmidt 	}
3876d7728c96SJan Schmidt 
3877d7728c96SJan Schmidt 	path = btrfs_alloc_path();
3878d7728c96SJan Schmidt 	if (!path) {
3879d7728c96SJan Schmidt 		ret = -ENOMEM;
3880d7728c96SJan Schmidt 		goto out;
3881d7728c96SJan Schmidt 	}
3882d7728c96SJan Schmidt 
3883425d17a2SLiu Bo 	size = min_t(u32, loi->size, 64 * 1024);
3884d7728c96SJan Schmidt 	inodes = init_data_container(size);
3885d7728c96SJan Schmidt 	if (IS_ERR(inodes)) {
3886d7728c96SJan Schmidt 		ret = PTR_ERR(inodes);
3887d7728c96SJan Schmidt 		inodes = NULL;
3888d7728c96SJan Schmidt 		goto out;
3889d7728c96SJan Schmidt 	}
3890d7728c96SJan Schmidt 
3891df031f07SLiu Bo 	ret = iterate_inodes_from_logical(loi->logical, root->fs_info, path,
3892df031f07SLiu Bo 					  build_ino_list, inodes);
3893df031f07SLiu Bo 	if (ret == -EINVAL)
3894d7728c96SJan Schmidt 		ret = -ENOENT;
3895d7728c96SJan Schmidt 	if (ret < 0)
3896d7728c96SJan Schmidt 		goto out;
3897d7728c96SJan Schmidt 
3898745c4d8eSJeff Mahoney 	ret = copy_to_user((void *)(unsigned long)loi->inodes,
3899745c4d8eSJeff Mahoney 			   (void *)(unsigned long)inodes, size);
3900d7728c96SJan Schmidt 	if (ret)
3901d7728c96SJan Schmidt 		ret = -EFAULT;
3902d7728c96SJan Schmidt 
3903d7728c96SJan Schmidt out:
3904d7728c96SJan Schmidt 	btrfs_free_path(path);
3905425d17a2SLiu Bo 	vfree(inodes);
3906d7728c96SJan Schmidt 	kfree(loi);
3907d7728c96SJan Schmidt 
3908d7728c96SJan Schmidt 	return ret;
3909d7728c96SJan Schmidt }
3910d7728c96SJan Schmidt 
391119a39dceSIlya Dryomov void update_ioctl_balance_args(struct btrfs_fs_info *fs_info, int lock,
3912c9e9f97bSIlya Dryomov 			       struct btrfs_ioctl_balance_args *bargs)
3913c9e9f97bSIlya Dryomov {
3914c9e9f97bSIlya Dryomov 	struct btrfs_balance_control *bctl = fs_info->balance_ctl;
3915c9e9f97bSIlya Dryomov 
3916c9e9f97bSIlya Dryomov 	bargs->flags = bctl->flags;
3917c9e9f97bSIlya Dryomov 
3918837d5b6eSIlya Dryomov 	if (atomic_read(&fs_info->balance_running))
3919837d5b6eSIlya Dryomov 		bargs->state |= BTRFS_BALANCE_STATE_RUNNING;
3920837d5b6eSIlya Dryomov 	if (atomic_read(&fs_info->balance_pause_req))
3921837d5b6eSIlya Dryomov 		bargs->state |= BTRFS_BALANCE_STATE_PAUSE_REQ;
3922a7e99c69SIlya Dryomov 	if (atomic_read(&fs_info->balance_cancel_req))
3923a7e99c69SIlya Dryomov 		bargs->state |= BTRFS_BALANCE_STATE_CANCEL_REQ;
3924837d5b6eSIlya Dryomov 
3925c9e9f97bSIlya Dryomov 	memcpy(&bargs->data, &bctl->data, sizeof(bargs->data));
3926c9e9f97bSIlya Dryomov 	memcpy(&bargs->meta, &bctl->meta, sizeof(bargs->meta));
3927c9e9f97bSIlya Dryomov 	memcpy(&bargs->sys, &bctl->sys, sizeof(bargs->sys));
392819a39dceSIlya Dryomov 
392919a39dceSIlya Dryomov 	if (lock) {
393019a39dceSIlya Dryomov 		spin_lock(&fs_info->balance_lock);
393119a39dceSIlya Dryomov 		memcpy(&bargs->stat, &bctl->stat, sizeof(bargs->stat));
393219a39dceSIlya Dryomov 		spin_unlock(&fs_info->balance_lock);
393319a39dceSIlya Dryomov 	} else {
393419a39dceSIlya Dryomov 		memcpy(&bargs->stat, &bctl->stat, sizeof(bargs->stat));
393519a39dceSIlya Dryomov 	}
3936c9e9f97bSIlya Dryomov }
3937c9e9f97bSIlya Dryomov 
39389ba1f6e4SLiu Bo static long btrfs_ioctl_balance(struct file *file, void __user *arg)
3939c9e9f97bSIlya Dryomov {
3940496ad9aaSAl Viro 	struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
3941c9e9f97bSIlya Dryomov 	struct btrfs_fs_info *fs_info = root->fs_info;
3942c9e9f97bSIlya Dryomov 	struct btrfs_ioctl_balance_args *bargs;
3943c9e9f97bSIlya Dryomov 	struct btrfs_balance_control *bctl;
3944ed0fb78fSIlya Dryomov 	bool need_unlock; /* for mut. excl. ops lock */
3945c9e9f97bSIlya Dryomov 	int ret;
3946c9e9f97bSIlya Dryomov 
3947c9e9f97bSIlya Dryomov 	if (!capable(CAP_SYS_ADMIN))
3948c9e9f97bSIlya Dryomov 		return -EPERM;
3949c9e9f97bSIlya Dryomov 
3950e54bfa31SLiu Bo 	ret = mnt_want_write_file(file);
39519ba1f6e4SLiu Bo 	if (ret)
39529ba1f6e4SLiu Bo 		return ret;
39539ba1f6e4SLiu Bo 
3954ed0fb78fSIlya Dryomov again:
3955ed0fb78fSIlya Dryomov 	if (!atomic_xchg(&fs_info->mutually_exclusive_operation_running, 1)) {
3956c9e9f97bSIlya Dryomov 		mutex_lock(&fs_info->volume_mutex);
3957c9e9f97bSIlya Dryomov 		mutex_lock(&fs_info->balance_mutex);
3958ed0fb78fSIlya Dryomov 		need_unlock = true;
3959ed0fb78fSIlya Dryomov 		goto locked;
3960ed0fb78fSIlya Dryomov 	}
3961ed0fb78fSIlya Dryomov 
3962ed0fb78fSIlya Dryomov 	/*
3963ed0fb78fSIlya Dryomov 	 * mut. excl. ops lock is locked.  Three possibilites:
3964ed0fb78fSIlya Dryomov 	 *   (1) some other op is running
3965ed0fb78fSIlya Dryomov 	 *   (2) balance is running
3966ed0fb78fSIlya Dryomov 	 *   (3) balance is paused -- special case (think resume)
3967ed0fb78fSIlya Dryomov 	 */
3968ed0fb78fSIlya Dryomov 	mutex_lock(&fs_info->balance_mutex);
3969ed0fb78fSIlya Dryomov 	if (fs_info->balance_ctl) {
3970ed0fb78fSIlya Dryomov 		/* this is either (2) or (3) */
3971ed0fb78fSIlya Dryomov 		if (!atomic_read(&fs_info->balance_running)) {
3972ed0fb78fSIlya Dryomov 			mutex_unlock(&fs_info->balance_mutex);
3973ed0fb78fSIlya Dryomov 			if (!mutex_trylock(&fs_info->volume_mutex))
3974ed0fb78fSIlya Dryomov 				goto again;
3975ed0fb78fSIlya Dryomov 			mutex_lock(&fs_info->balance_mutex);
3976ed0fb78fSIlya Dryomov 
3977ed0fb78fSIlya Dryomov 			if (fs_info->balance_ctl &&
3978ed0fb78fSIlya Dryomov 			    !atomic_read(&fs_info->balance_running)) {
3979ed0fb78fSIlya Dryomov 				/* this is (3) */
3980ed0fb78fSIlya Dryomov 				need_unlock = false;
3981ed0fb78fSIlya Dryomov 				goto locked;
3982ed0fb78fSIlya Dryomov 			}
3983ed0fb78fSIlya Dryomov 
3984ed0fb78fSIlya Dryomov 			mutex_unlock(&fs_info->balance_mutex);
3985ed0fb78fSIlya Dryomov 			mutex_unlock(&fs_info->volume_mutex);
3986ed0fb78fSIlya Dryomov 			goto again;
3987ed0fb78fSIlya Dryomov 		} else {
3988ed0fb78fSIlya Dryomov 			/* this is (2) */
3989ed0fb78fSIlya Dryomov 			mutex_unlock(&fs_info->balance_mutex);
3990ed0fb78fSIlya Dryomov 			ret = -EINPROGRESS;
3991ed0fb78fSIlya Dryomov 			goto out;
3992ed0fb78fSIlya Dryomov 		}
3993ed0fb78fSIlya Dryomov 	} else {
3994ed0fb78fSIlya Dryomov 		/* this is (1) */
3995ed0fb78fSIlya Dryomov 		mutex_unlock(&fs_info->balance_mutex);
3996e57138b3SAnand Jain 		ret = BTRFS_ERROR_DEV_EXCL_RUN_IN_PROGRESS;
3997ed0fb78fSIlya Dryomov 		goto out;
3998ed0fb78fSIlya Dryomov 	}
3999ed0fb78fSIlya Dryomov 
4000ed0fb78fSIlya Dryomov locked:
4001ed0fb78fSIlya Dryomov 	BUG_ON(!atomic_read(&fs_info->mutually_exclusive_operation_running));
4002c9e9f97bSIlya Dryomov 
4003c9e9f97bSIlya Dryomov 	if (arg) {
4004c9e9f97bSIlya Dryomov 		bargs = memdup_user(arg, sizeof(*bargs));
4005c9e9f97bSIlya Dryomov 		if (IS_ERR(bargs)) {
4006c9e9f97bSIlya Dryomov 			ret = PTR_ERR(bargs);
4007ed0fb78fSIlya Dryomov 			goto out_unlock;
4008c9e9f97bSIlya Dryomov 		}
4009de322263SIlya Dryomov 
4010de322263SIlya Dryomov 		if (bargs->flags & BTRFS_BALANCE_RESUME) {
4011de322263SIlya Dryomov 			if (!fs_info->balance_ctl) {
4012de322263SIlya Dryomov 				ret = -ENOTCONN;
4013de322263SIlya Dryomov 				goto out_bargs;
4014de322263SIlya Dryomov 			}
4015de322263SIlya Dryomov 
4016de322263SIlya Dryomov 			bctl = fs_info->balance_ctl;
4017de322263SIlya Dryomov 			spin_lock(&fs_info->balance_lock);
4018de322263SIlya Dryomov 			bctl->flags |= BTRFS_BALANCE_RESUME;
4019de322263SIlya Dryomov 			spin_unlock(&fs_info->balance_lock);
4020de322263SIlya Dryomov 
4021de322263SIlya Dryomov 			goto do_balance;
4022de322263SIlya Dryomov 		}
4023c9e9f97bSIlya Dryomov 	} else {
4024c9e9f97bSIlya Dryomov 		bargs = NULL;
4025c9e9f97bSIlya Dryomov 	}
4026c9e9f97bSIlya Dryomov 
4027ed0fb78fSIlya Dryomov 	if (fs_info->balance_ctl) {
4028837d5b6eSIlya Dryomov 		ret = -EINPROGRESS;
4029837d5b6eSIlya Dryomov 		goto out_bargs;
4030837d5b6eSIlya Dryomov 	}
4031837d5b6eSIlya Dryomov 
4032c9e9f97bSIlya Dryomov 	bctl = kzalloc(sizeof(*bctl), GFP_NOFS);
4033c9e9f97bSIlya Dryomov 	if (!bctl) {
4034c9e9f97bSIlya Dryomov 		ret = -ENOMEM;
4035c9e9f97bSIlya Dryomov 		goto out_bargs;
4036c9e9f97bSIlya Dryomov 	}
4037c9e9f97bSIlya Dryomov 
4038c9e9f97bSIlya Dryomov 	bctl->fs_info = fs_info;
4039c9e9f97bSIlya Dryomov 	if (arg) {
4040c9e9f97bSIlya Dryomov 		memcpy(&bctl->data, &bargs->data, sizeof(bctl->data));
4041c9e9f97bSIlya Dryomov 		memcpy(&bctl->meta, &bargs->meta, sizeof(bctl->meta));
4042c9e9f97bSIlya Dryomov 		memcpy(&bctl->sys, &bargs->sys, sizeof(bctl->sys));
4043c9e9f97bSIlya Dryomov 
4044c9e9f97bSIlya Dryomov 		bctl->flags = bargs->flags;
4045f43ffb60SIlya Dryomov 	} else {
4046f43ffb60SIlya Dryomov 		/* balance everything - no filters */
4047f43ffb60SIlya Dryomov 		bctl->flags |= BTRFS_BALANCE_TYPE_MASK;
4048c9e9f97bSIlya Dryomov 	}
4049c9e9f97bSIlya Dryomov 
4050de322263SIlya Dryomov do_balance:
4051c9e9f97bSIlya Dryomov 	/*
4052ed0fb78fSIlya Dryomov 	 * Ownership of bctl and mutually_exclusive_operation_running
4053ed0fb78fSIlya Dryomov 	 * goes to to btrfs_balance.  bctl is freed in __cancel_balance,
4054ed0fb78fSIlya Dryomov 	 * or, if restriper was paused all the way until unmount, in
4055ed0fb78fSIlya Dryomov 	 * free_fs_info.  mutually_exclusive_operation_running is
4056ed0fb78fSIlya Dryomov 	 * cleared in __cancel_balance.
4057c9e9f97bSIlya Dryomov 	 */
4058ed0fb78fSIlya Dryomov 	need_unlock = false;
4059ed0fb78fSIlya Dryomov 
4060ed0fb78fSIlya Dryomov 	ret = btrfs_balance(bctl, bargs);
4061ed0fb78fSIlya Dryomov 
4062c9e9f97bSIlya Dryomov 	if (arg) {
4063c9e9f97bSIlya Dryomov 		if (copy_to_user(arg, bargs, sizeof(*bargs)))
4064c9e9f97bSIlya Dryomov 			ret = -EFAULT;
4065c9e9f97bSIlya Dryomov 	}
4066c9e9f97bSIlya Dryomov 
4067c9e9f97bSIlya Dryomov out_bargs:
4068c9e9f97bSIlya Dryomov 	kfree(bargs);
4069ed0fb78fSIlya Dryomov out_unlock:
4070c9e9f97bSIlya Dryomov 	mutex_unlock(&fs_info->balance_mutex);
4071c9e9f97bSIlya Dryomov 	mutex_unlock(&fs_info->volume_mutex);
4072ed0fb78fSIlya Dryomov 	if (need_unlock)
4073ed0fb78fSIlya Dryomov 		atomic_set(&fs_info->mutually_exclusive_operation_running, 0);
4074ed0fb78fSIlya Dryomov out:
4075e54bfa31SLiu Bo 	mnt_drop_write_file(file);
4076c9e9f97bSIlya Dryomov 	return ret;
4077c9e9f97bSIlya Dryomov }
4078c9e9f97bSIlya Dryomov 
4079837d5b6eSIlya Dryomov static long btrfs_ioctl_balance_ctl(struct btrfs_root *root, int cmd)
4080837d5b6eSIlya Dryomov {
4081837d5b6eSIlya Dryomov 	if (!capable(CAP_SYS_ADMIN))
4082837d5b6eSIlya Dryomov 		return -EPERM;
4083837d5b6eSIlya Dryomov 
4084837d5b6eSIlya Dryomov 	switch (cmd) {
4085837d5b6eSIlya Dryomov 	case BTRFS_BALANCE_CTL_PAUSE:
4086837d5b6eSIlya Dryomov 		return btrfs_pause_balance(root->fs_info);
4087a7e99c69SIlya Dryomov 	case BTRFS_BALANCE_CTL_CANCEL:
4088a7e99c69SIlya Dryomov 		return btrfs_cancel_balance(root->fs_info);
4089837d5b6eSIlya Dryomov 	}
4090837d5b6eSIlya Dryomov 
4091837d5b6eSIlya Dryomov 	return -EINVAL;
4092837d5b6eSIlya Dryomov }
4093837d5b6eSIlya Dryomov 
409419a39dceSIlya Dryomov static long btrfs_ioctl_balance_progress(struct btrfs_root *root,
409519a39dceSIlya Dryomov 					 void __user *arg)
409619a39dceSIlya Dryomov {
409719a39dceSIlya Dryomov 	struct btrfs_fs_info *fs_info = root->fs_info;
409819a39dceSIlya Dryomov 	struct btrfs_ioctl_balance_args *bargs;
409919a39dceSIlya Dryomov 	int ret = 0;
410019a39dceSIlya Dryomov 
410119a39dceSIlya Dryomov 	if (!capable(CAP_SYS_ADMIN))
410219a39dceSIlya Dryomov 		return -EPERM;
410319a39dceSIlya Dryomov 
410419a39dceSIlya Dryomov 	mutex_lock(&fs_info->balance_mutex);
410519a39dceSIlya Dryomov 	if (!fs_info->balance_ctl) {
410619a39dceSIlya Dryomov 		ret = -ENOTCONN;
410719a39dceSIlya Dryomov 		goto out;
410819a39dceSIlya Dryomov 	}
410919a39dceSIlya Dryomov 
411019a39dceSIlya Dryomov 	bargs = kzalloc(sizeof(*bargs), GFP_NOFS);
411119a39dceSIlya Dryomov 	if (!bargs) {
411219a39dceSIlya Dryomov 		ret = -ENOMEM;
411319a39dceSIlya Dryomov 		goto out;
411419a39dceSIlya Dryomov 	}
411519a39dceSIlya Dryomov 
411619a39dceSIlya Dryomov 	update_ioctl_balance_args(fs_info, 1, bargs);
411719a39dceSIlya Dryomov 
411819a39dceSIlya Dryomov 	if (copy_to_user(arg, bargs, sizeof(*bargs)))
411919a39dceSIlya Dryomov 		ret = -EFAULT;
412019a39dceSIlya Dryomov 
412119a39dceSIlya Dryomov 	kfree(bargs);
412219a39dceSIlya Dryomov out:
412319a39dceSIlya Dryomov 	mutex_unlock(&fs_info->balance_mutex);
412419a39dceSIlya Dryomov 	return ret;
412519a39dceSIlya Dryomov }
412619a39dceSIlya Dryomov 
4127905b0ddaSMiao Xie static long btrfs_ioctl_quota_ctl(struct file *file, void __user *arg)
41285d13a37bSArne Jansen {
4129496ad9aaSAl Viro 	struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
41305d13a37bSArne Jansen 	struct btrfs_ioctl_quota_ctl_args *sa;
41315d13a37bSArne Jansen 	struct btrfs_trans_handle *trans = NULL;
41325d13a37bSArne Jansen 	int ret;
41335d13a37bSArne Jansen 	int err;
41345d13a37bSArne Jansen 
41355d13a37bSArne Jansen 	if (!capable(CAP_SYS_ADMIN))
41365d13a37bSArne Jansen 		return -EPERM;
41375d13a37bSArne Jansen 
4138905b0ddaSMiao Xie 	ret = mnt_want_write_file(file);
4139905b0ddaSMiao Xie 	if (ret)
4140905b0ddaSMiao Xie 		return ret;
41415d13a37bSArne Jansen 
41425d13a37bSArne Jansen 	sa = memdup_user(arg, sizeof(*sa));
4143905b0ddaSMiao Xie 	if (IS_ERR(sa)) {
4144905b0ddaSMiao Xie 		ret = PTR_ERR(sa);
4145905b0ddaSMiao Xie 		goto drop_write;
4146905b0ddaSMiao Xie 	}
41475d13a37bSArne Jansen 
41487708f029SWang Shilong 	down_write(&root->fs_info->subvol_sem);
414992f183aaSWang Shilong 	trans = btrfs_start_transaction(root->fs_info->tree_root, 2);
41505d13a37bSArne Jansen 	if (IS_ERR(trans)) {
41515d13a37bSArne Jansen 		ret = PTR_ERR(trans);
41525d13a37bSArne Jansen 		goto out;
41535d13a37bSArne Jansen 	}
41545d13a37bSArne Jansen 
41555d13a37bSArne Jansen 	switch (sa->cmd) {
41565d13a37bSArne Jansen 	case BTRFS_QUOTA_CTL_ENABLE:
41575d13a37bSArne Jansen 		ret = btrfs_quota_enable(trans, root->fs_info);
41585d13a37bSArne Jansen 		break;
41595d13a37bSArne Jansen 	case BTRFS_QUOTA_CTL_DISABLE:
41605d13a37bSArne Jansen 		ret = btrfs_quota_disable(trans, root->fs_info);
41615d13a37bSArne Jansen 		break;
41625d13a37bSArne Jansen 	default:
41635d13a37bSArne Jansen 		ret = -EINVAL;
41645d13a37bSArne Jansen 		break;
41655d13a37bSArne Jansen 	}
41665d13a37bSArne Jansen 
416792f183aaSWang Shilong 	err = btrfs_commit_transaction(trans, root->fs_info->tree_root);
41685d13a37bSArne Jansen 	if (err && !ret)
41695d13a37bSArne Jansen 		ret = err;
41705d13a37bSArne Jansen out:
41715d13a37bSArne Jansen 	kfree(sa);
41727708f029SWang Shilong 	up_write(&root->fs_info->subvol_sem);
4173905b0ddaSMiao Xie drop_write:
4174905b0ddaSMiao Xie 	mnt_drop_write_file(file);
41755d13a37bSArne Jansen 	return ret;
41765d13a37bSArne Jansen }
41775d13a37bSArne Jansen 
4178905b0ddaSMiao Xie static long btrfs_ioctl_qgroup_assign(struct file *file, void __user *arg)
41795d13a37bSArne Jansen {
4180496ad9aaSAl Viro 	struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
41815d13a37bSArne Jansen 	struct btrfs_ioctl_qgroup_assign_args *sa;
41825d13a37bSArne Jansen 	struct btrfs_trans_handle *trans;
41835d13a37bSArne Jansen 	int ret;
41845d13a37bSArne Jansen 	int err;
41855d13a37bSArne Jansen 
41865d13a37bSArne Jansen 	if (!capable(CAP_SYS_ADMIN))
41875d13a37bSArne Jansen 		return -EPERM;
41885d13a37bSArne Jansen 
4189905b0ddaSMiao Xie 	ret = mnt_want_write_file(file);
4190905b0ddaSMiao Xie 	if (ret)
4191905b0ddaSMiao Xie 		return ret;
41925d13a37bSArne Jansen 
41935d13a37bSArne Jansen 	sa = memdup_user(arg, sizeof(*sa));
4194905b0ddaSMiao Xie 	if (IS_ERR(sa)) {
4195905b0ddaSMiao Xie 		ret = PTR_ERR(sa);
4196905b0ddaSMiao Xie 		goto drop_write;
4197905b0ddaSMiao Xie 	}
41985d13a37bSArne Jansen 
41995d13a37bSArne Jansen 	trans = btrfs_join_transaction(root);
42005d13a37bSArne Jansen 	if (IS_ERR(trans)) {
42015d13a37bSArne Jansen 		ret = PTR_ERR(trans);
42025d13a37bSArne Jansen 		goto out;
42035d13a37bSArne Jansen 	}
42045d13a37bSArne Jansen 
42055d13a37bSArne Jansen 	/* FIXME: check if the IDs really exist */
42065d13a37bSArne Jansen 	if (sa->assign) {
42075d13a37bSArne Jansen 		ret = btrfs_add_qgroup_relation(trans, root->fs_info,
42085d13a37bSArne Jansen 						sa->src, sa->dst);
42095d13a37bSArne Jansen 	} else {
42105d13a37bSArne Jansen 		ret = btrfs_del_qgroup_relation(trans, root->fs_info,
42115d13a37bSArne Jansen 						sa->src, sa->dst);
42125d13a37bSArne Jansen 	}
42135d13a37bSArne Jansen 
42145d13a37bSArne Jansen 	err = btrfs_end_transaction(trans, root);
42155d13a37bSArne Jansen 	if (err && !ret)
42165d13a37bSArne Jansen 		ret = err;
42175d13a37bSArne Jansen 
42185d13a37bSArne Jansen out:
42195d13a37bSArne Jansen 	kfree(sa);
4220905b0ddaSMiao Xie drop_write:
4221905b0ddaSMiao Xie 	mnt_drop_write_file(file);
42225d13a37bSArne Jansen 	return ret;
42235d13a37bSArne Jansen }
42245d13a37bSArne Jansen 
4225905b0ddaSMiao Xie static long btrfs_ioctl_qgroup_create(struct file *file, void __user *arg)
42265d13a37bSArne Jansen {
4227496ad9aaSAl Viro 	struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
42285d13a37bSArne Jansen 	struct btrfs_ioctl_qgroup_create_args *sa;
42295d13a37bSArne Jansen 	struct btrfs_trans_handle *trans;
42305d13a37bSArne Jansen 	int ret;
42315d13a37bSArne Jansen 	int err;
42325d13a37bSArne Jansen 
42335d13a37bSArne Jansen 	if (!capable(CAP_SYS_ADMIN))
42345d13a37bSArne Jansen 		return -EPERM;
42355d13a37bSArne Jansen 
4236905b0ddaSMiao Xie 	ret = mnt_want_write_file(file);
4237905b0ddaSMiao Xie 	if (ret)
4238905b0ddaSMiao Xie 		return ret;
42395d13a37bSArne Jansen 
42405d13a37bSArne Jansen 	sa = memdup_user(arg, sizeof(*sa));
4241905b0ddaSMiao Xie 	if (IS_ERR(sa)) {
4242905b0ddaSMiao Xie 		ret = PTR_ERR(sa);
4243905b0ddaSMiao Xie 		goto drop_write;
4244905b0ddaSMiao Xie 	}
42455d13a37bSArne Jansen 
4246d86e56cfSMiao Xie 	if (!sa->qgroupid) {
4247d86e56cfSMiao Xie 		ret = -EINVAL;
4248d86e56cfSMiao Xie 		goto out;
4249d86e56cfSMiao Xie 	}
4250d86e56cfSMiao Xie 
42515d13a37bSArne Jansen 	trans = btrfs_join_transaction(root);
42525d13a37bSArne Jansen 	if (IS_ERR(trans)) {
42535d13a37bSArne Jansen 		ret = PTR_ERR(trans);
42545d13a37bSArne Jansen 		goto out;
42555d13a37bSArne Jansen 	}
42565d13a37bSArne Jansen 
42575d13a37bSArne Jansen 	/* FIXME: check if the IDs really exist */
42585d13a37bSArne Jansen 	if (sa->create) {
42595d13a37bSArne Jansen 		ret = btrfs_create_qgroup(trans, root->fs_info, sa->qgroupid,
42605d13a37bSArne Jansen 					  NULL);
42615d13a37bSArne Jansen 	} else {
42625d13a37bSArne Jansen 		ret = btrfs_remove_qgroup(trans, root->fs_info, sa->qgroupid);
42635d13a37bSArne Jansen 	}
42645d13a37bSArne Jansen 
42655d13a37bSArne Jansen 	err = btrfs_end_transaction(trans, root);
42665d13a37bSArne Jansen 	if (err && !ret)
42675d13a37bSArne Jansen 		ret = err;
42685d13a37bSArne Jansen 
42695d13a37bSArne Jansen out:
42705d13a37bSArne Jansen 	kfree(sa);
4271905b0ddaSMiao Xie drop_write:
4272905b0ddaSMiao Xie 	mnt_drop_write_file(file);
42735d13a37bSArne Jansen 	return ret;
42745d13a37bSArne Jansen }
42755d13a37bSArne Jansen 
4276905b0ddaSMiao Xie static long btrfs_ioctl_qgroup_limit(struct file *file, void __user *arg)
42775d13a37bSArne Jansen {
4278496ad9aaSAl Viro 	struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
42795d13a37bSArne Jansen 	struct btrfs_ioctl_qgroup_limit_args *sa;
42805d13a37bSArne Jansen 	struct btrfs_trans_handle *trans;
42815d13a37bSArne Jansen 	int ret;
42825d13a37bSArne Jansen 	int err;
42835d13a37bSArne Jansen 	u64 qgroupid;
42845d13a37bSArne Jansen 
42855d13a37bSArne Jansen 	if (!capable(CAP_SYS_ADMIN))
42865d13a37bSArne Jansen 		return -EPERM;
42875d13a37bSArne Jansen 
4288905b0ddaSMiao Xie 	ret = mnt_want_write_file(file);
4289905b0ddaSMiao Xie 	if (ret)
4290905b0ddaSMiao Xie 		return ret;
42915d13a37bSArne Jansen 
42925d13a37bSArne Jansen 	sa = memdup_user(arg, sizeof(*sa));
4293905b0ddaSMiao Xie 	if (IS_ERR(sa)) {
4294905b0ddaSMiao Xie 		ret = PTR_ERR(sa);
4295905b0ddaSMiao Xie 		goto drop_write;
4296905b0ddaSMiao Xie 	}
42975d13a37bSArne Jansen 
42985d13a37bSArne Jansen 	trans = btrfs_join_transaction(root);
42995d13a37bSArne Jansen 	if (IS_ERR(trans)) {
43005d13a37bSArne Jansen 		ret = PTR_ERR(trans);
43015d13a37bSArne Jansen 		goto out;
43025d13a37bSArne Jansen 	}
43035d13a37bSArne Jansen 
43045d13a37bSArne Jansen 	qgroupid = sa->qgroupid;
43055d13a37bSArne Jansen 	if (!qgroupid) {
43065d13a37bSArne Jansen 		/* take the current subvol as qgroup */
43075d13a37bSArne Jansen 		qgroupid = root->root_key.objectid;
43085d13a37bSArne Jansen 	}
43095d13a37bSArne Jansen 
43105d13a37bSArne Jansen 	/* FIXME: check if the IDs really exist */
43115d13a37bSArne Jansen 	ret = btrfs_limit_qgroup(trans, root->fs_info, qgroupid, &sa->lim);
43125d13a37bSArne Jansen 
43135d13a37bSArne Jansen 	err = btrfs_end_transaction(trans, root);
43145d13a37bSArne Jansen 	if (err && !ret)
43155d13a37bSArne Jansen 		ret = err;
43165d13a37bSArne Jansen 
43175d13a37bSArne Jansen out:
43185d13a37bSArne Jansen 	kfree(sa);
4319905b0ddaSMiao Xie drop_write:
4320905b0ddaSMiao Xie 	mnt_drop_write_file(file);
43215d13a37bSArne Jansen 	return ret;
43225d13a37bSArne Jansen }
43235d13a37bSArne Jansen 
43242f232036SJan Schmidt static long btrfs_ioctl_quota_rescan(struct file *file, void __user *arg)
43252f232036SJan Schmidt {
43266d0379ecSAl Viro 	struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
43272f232036SJan Schmidt 	struct btrfs_ioctl_quota_rescan_args *qsa;
43282f232036SJan Schmidt 	int ret;
43292f232036SJan Schmidt 
43302f232036SJan Schmidt 	if (!capable(CAP_SYS_ADMIN))
43312f232036SJan Schmidt 		return -EPERM;
43322f232036SJan Schmidt 
43332f232036SJan Schmidt 	ret = mnt_want_write_file(file);
43342f232036SJan Schmidt 	if (ret)
43352f232036SJan Schmidt 		return ret;
43362f232036SJan Schmidt 
43372f232036SJan Schmidt 	qsa = memdup_user(arg, sizeof(*qsa));
43382f232036SJan Schmidt 	if (IS_ERR(qsa)) {
43392f232036SJan Schmidt 		ret = PTR_ERR(qsa);
43402f232036SJan Schmidt 		goto drop_write;
43412f232036SJan Schmidt 	}
43422f232036SJan Schmidt 
43432f232036SJan Schmidt 	if (qsa->flags) {
43442f232036SJan Schmidt 		ret = -EINVAL;
43452f232036SJan Schmidt 		goto out;
43462f232036SJan Schmidt 	}
43472f232036SJan Schmidt 
43482f232036SJan Schmidt 	ret = btrfs_qgroup_rescan(root->fs_info);
43492f232036SJan Schmidt 
43502f232036SJan Schmidt out:
43512f232036SJan Schmidt 	kfree(qsa);
43522f232036SJan Schmidt drop_write:
43532f232036SJan Schmidt 	mnt_drop_write_file(file);
43542f232036SJan Schmidt 	return ret;
43552f232036SJan Schmidt }
43562f232036SJan Schmidt 
43572f232036SJan Schmidt static long btrfs_ioctl_quota_rescan_status(struct file *file, void __user *arg)
43582f232036SJan Schmidt {
43596d0379ecSAl Viro 	struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
43602f232036SJan Schmidt 	struct btrfs_ioctl_quota_rescan_args *qsa;
43612f232036SJan Schmidt 	int ret = 0;
43622f232036SJan Schmidt 
43632f232036SJan Schmidt 	if (!capable(CAP_SYS_ADMIN))
43642f232036SJan Schmidt 		return -EPERM;
43652f232036SJan Schmidt 
43662f232036SJan Schmidt 	qsa = kzalloc(sizeof(*qsa), GFP_NOFS);
43672f232036SJan Schmidt 	if (!qsa)
43682f232036SJan Schmidt 		return -ENOMEM;
43692f232036SJan Schmidt 
43702f232036SJan Schmidt 	if (root->fs_info->qgroup_flags & BTRFS_QGROUP_STATUS_FLAG_RESCAN) {
43712f232036SJan Schmidt 		qsa->flags = 1;
43722f232036SJan Schmidt 		qsa->progress = root->fs_info->qgroup_rescan_progress.objectid;
43732f232036SJan Schmidt 	}
43742f232036SJan Schmidt 
43752f232036SJan Schmidt 	if (copy_to_user(arg, qsa, sizeof(*qsa)))
43762f232036SJan Schmidt 		ret = -EFAULT;
43772f232036SJan Schmidt 
43782f232036SJan Schmidt 	kfree(qsa);
43792f232036SJan Schmidt 	return ret;
43802f232036SJan Schmidt }
43812f232036SJan Schmidt 
438257254b6eSJan Schmidt static long btrfs_ioctl_quota_rescan_wait(struct file *file, void __user *arg)
438357254b6eSJan Schmidt {
438454563d41SAl Viro 	struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
438557254b6eSJan Schmidt 
438657254b6eSJan Schmidt 	if (!capable(CAP_SYS_ADMIN))
438757254b6eSJan Schmidt 		return -EPERM;
438857254b6eSJan Schmidt 
438957254b6eSJan Schmidt 	return btrfs_qgroup_wait_for_completion(root->fs_info);
439057254b6eSJan Schmidt }
439157254b6eSJan Schmidt 
43928ea05e3aSAlexander Block static long btrfs_ioctl_set_received_subvol(struct file *file,
43938ea05e3aSAlexander Block 					    void __user *arg)
43948ea05e3aSAlexander Block {
43958ea05e3aSAlexander Block 	struct btrfs_ioctl_received_subvol_args *sa = NULL;
4396496ad9aaSAl Viro 	struct inode *inode = file_inode(file);
43978ea05e3aSAlexander Block 	struct btrfs_root *root = BTRFS_I(inode)->root;
43988ea05e3aSAlexander Block 	struct btrfs_root_item *root_item = &root->root_item;
43998ea05e3aSAlexander Block 	struct btrfs_trans_handle *trans;
44008ea05e3aSAlexander Block 	struct timespec ct = CURRENT_TIME;
44018ea05e3aSAlexander Block 	int ret = 0;
4402dd5f9615SStefan Behrens 	int received_uuid_changed;
44038ea05e3aSAlexander Block 
4404bd60ea0fSDavid Sterba 	if (!inode_owner_or_capable(inode))
4405bd60ea0fSDavid Sterba 		return -EPERM;
4406bd60ea0fSDavid Sterba 
44078ea05e3aSAlexander Block 	ret = mnt_want_write_file(file);
44088ea05e3aSAlexander Block 	if (ret < 0)
44098ea05e3aSAlexander Block 		return ret;
44108ea05e3aSAlexander Block 
44118ea05e3aSAlexander Block 	down_write(&root->fs_info->subvol_sem);
44128ea05e3aSAlexander Block 
44138ea05e3aSAlexander Block 	if (btrfs_ino(inode) != BTRFS_FIRST_FREE_OBJECTID) {
44148ea05e3aSAlexander Block 		ret = -EINVAL;
44158ea05e3aSAlexander Block 		goto out;
44168ea05e3aSAlexander Block 	}
44178ea05e3aSAlexander Block 
44188ea05e3aSAlexander Block 	if (btrfs_root_readonly(root)) {
44198ea05e3aSAlexander Block 		ret = -EROFS;
44208ea05e3aSAlexander Block 		goto out;
44218ea05e3aSAlexander Block 	}
44228ea05e3aSAlexander Block 
44238ea05e3aSAlexander Block 	sa = memdup_user(arg, sizeof(*sa));
44248ea05e3aSAlexander Block 	if (IS_ERR(sa)) {
44258ea05e3aSAlexander Block 		ret = PTR_ERR(sa);
44268ea05e3aSAlexander Block 		sa = NULL;
44278ea05e3aSAlexander Block 		goto out;
44288ea05e3aSAlexander Block 	}
44298ea05e3aSAlexander Block 
4430dd5f9615SStefan Behrens 	/*
4431dd5f9615SStefan Behrens 	 * 1 - root item
4432dd5f9615SStefan Behrens 	 * 2 - uuid items (received uuid + subvol uuid)
4433dd5f9615SStefan Behrens 	 */
4434dd5f9615SStefan Behrens 	trans = btrfs_start_transaction(root, 3);
44358ea05e3aSAlexander Block 	if (IS_ERR(trans)) {
44368ea05e3aSAlexander Block 		ret = PTR_ERR(trans);
44378ea05e3aSAlexander Block 		trans = NULL;
44388ea05e3aSAlexander Block 		goto out;
44398ea05e3aSAlexander Block 	}
44408ea05e3aSAlexander Block 
44418ea05e3aSAlexander Block 	sa->rtransid = trans->transid;
44428ea05e3aSAlexander Block 	sa->rtime.sec = ct.tv_sec;
44438ea05e3aSAlexander Block 	sa->rtime.nsec = ct.tv_nsec;
44448ea05e3aSAlexander Block 
4445dd5f9615SStefan Behrens 	received_uuid_changed = memcmp(root_item->received_uuid, sa->uuid,
4446dd5f9615SStefan Behrens 				       BTRFS_UUID_SIZE);
4447dd5f9615SStefan Behrens 	if (received_uuid_changed &&
4448dd5f9615SStefan Behrens 	    !btrfs_is_empty_uuid(root_item->received_uuid))
4449dd5f9615SStefan Behrens 		btrfs_uuid_tree_rem(trans, root->fs_info->uuid_root,
4450dd5f9615SStefan Behrens 				    root_item->received_uuid,
4451dd5f9615SStefan Behrens 				    BTRFS_UUID_KEY_RECEIVED_SUBVOL,
4452dd5f9615SStefan Behrens 				    root->root_key.objectid);
44538ea05e3aSAlexander Block 	memcpy(root_item->received_uuid, sa->uuid, BTRFS_UUID_SIZE);
44548ea05e3aSAlexander Block 	btrfs_set_root_stransid(root_item, sa->stransid);
44558ea05e3aSAlexander Block 	btrfs_set_root_rtransid(root_item, sa->rtransid);
44563cae210fSQu Wenruo 	btrfs_set_stack_timespec_sec(&root_item->stime, sa->stime.sec);
44573cae210fSQu Wenruo 	btrfs_set_stack_timespec_nsec(&root_item->stime, sa->stime.nsec);
44583cae210fSQu Wenruo 	btrfs_set_stack_timespec_sec(&root_item->rtime, sa->rtime.sec);
44593cae210fSQu Wenruo 	btrfs_set_stack_timespec_nsec(&root_item->rtime, sa->rtime.nsec);
44608ea05e3aSAlexander Block 
44618ea05e3aSAlexander Block 	ret = btrfs_update_root(trans, root->fs_info->tree_root,
44628ea05e3aSAlexander Block 				&root->root_key, &root->root_item);
44638ea05e3aSAlexander Block 	if (ret < 0) {
44648ea05e3aSAlexander Block 		btrfs_end_transaction(trans, root);
44658ea05e3aSAlexander Block 		goto out;
4466dd5f9615SStefan Behrens 	}
4467dd5f9615SStefan Behrens 	if (received_uuid_changed && !btrfs_is_empty_uuid(sa->uuid)) {
4468dd5f9615SStefan Behrens 		ret = btrfs_uuid_tree_add(trans, root->fs_info->uuid_root,
4469dd5f9615SStefan Behrens 					  sa->uuid,
4470dd5f9615SStefan Behrens 					  BTRFS_UUID_KEY_RECEIVED_SUBVOL,
4471dd5f9615SStefan Behrens 					  root->root_key.objectid);
4472dd5f9615SStefan Behrens 		if (ret < 0 && ret != -EEXIST) {
4473dd5f9615SStefan Behrens 			btrfs_abort_transaction(trans, root, ret);
4474dd5f9615SStefan Behrens 			goto out;
4475dd5f9615SStefan Behrens 		}
4476dd5f9615SStefan Behrens 	}
44778ea05e3aSAlexander Block 	ret = btrfs_commit_transaction(trans, root);
4478dd5f9615SStefan Behrens 	if (ret < 0) {
4479dd5f9615SStefan Behrens 		btrfs_abort_transaction(trans, root, ret);
44808ea05e3aSAlexander Block 		goto out;
44818ea05e3aSAlexander Block 	}
44828ea05e3aSAlexander Block 
44838ea05e3aSAlexander Block 	ret = copy_to_user(arg, sa, sizeof(*sa));
44848ea05e3aSAlexander Block 	if (ret)
44858ea05e3aSAlexander Block 		ret = -EFAULT;
44868ea05e3aSAlexander Block 
44878ea05e3aSAlexander Block out:
44888ea05e3aSAlexander Block 	kfree(sa);
44898ea05e3aSAlexander Block 	up_write(&root->fs_info->subvol_sem);
44908ea05e3aSAlexander Block 	mnt_drop_write_file(file);
44918ea05e3aSAlexander Block 	return ret;
44928ea05e3aSAlexander Block }
44938ea05e3aSAlexander Block 
4494867ab667Sjeff.liu static int btrfs_ioctl_get_fslabel(struct file *file, void __user *arg)
4495867ab667Sjeff.liu {
44966d0379ecSAl Viro 	struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
4497a1b83ac5SAnand Jain 	size_t len;
4498867ab667Sjeff.liu 	int ret;
4499a1b83ac5SAnand Jain 	char label[BTRFS_LABEL_SIZE];
4500a1b83ac5SAnand Jain 
4501a1b83ac5SAnand Jain 	spin_lock(&root->fs_info->super_lock);
4502a1b83ac5SAnand Jain 	memcpy(label, root->fs_info->super_copy->label, BTRFS_LABEL_SIZE);
4503a1b83ac5SAnand Jain 	spin_unlock(&root->fs_info->super_lock);
4504a1b83ac5SAnand Jain 
4505a1b83ac5SAnand Jain 	len = strnlen(label, BTRFS_LABEL_SIZE);
4506867ab667Sjeff.liu 
4507867ab667Sjeff.liu 	if (len == BTRFS_LABEL_SIZE) {
4508efe120a0SFrank Holton 		btrfs_warn(root->fs_info,
4509efe120a0SFrank Holton 			"label is too long, return the first %zu bytes", --len);
4510867ab667Sjeff.liu 	}
4511867ab667Sjeff.liu 
4512867ab667Sjeff.liu 	ret = copy_to_user(arg, label, len);
4513867ab667Sjeff.liu 
4514867ab667Sjeff.liu 	return ret ? -EFAULT : 0;
4515867ab667Sjeff.liu }
4516867ab667Sjeff.liu 
4517a8bfd4abSjeff.liu static int btrfs_ioctl_set_fslabel(struct file *file, void __user *arg)
4518a8bfd4abSjeff.liu {
45196d0379ecSAl Viro 	struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
4520a8bfd4abSjeff.liu 	struct btrfs_super_block *super_block = root->fs_info->super_copy;
4521a8bfd4abSjeff.liu 	struct btrfs_trans_handle *trans;
4522a8bfd4abSjeff.liu 	char label[BTRFS_LABEL_SIZE];
4523a8bfd4abSjeff.liu 	int ret;
4524a8bfd4abSjeff.liu 
4525a8bfd4abSjeff.liu 	if (!capable(CAP_SYS_ADMIN))
4526a8bfd4abSjeff.liu 		return -EPERM;
4527a8bfd4abSjeff.liu 
4528a8bfd4abSjeff.liu 	if (copy_from_user(label, arg, sizeof(label)))
4529a8bfd4abSjeff.liu 		return -EFAULT;
4530a8bfd4abSjeff.liu 
4531a8bfd4abSjeff.liu 	if (strnlen(label, BTRFS_LABEL_SIZE) == BTRFS_LABEL_SIZE) {
4532efe120a0SFrank Holton 		btrfs_err(root->fs_info, "unable to set label with more than %d bytes",
4533a8bfd4abSjeff.liu 		       BTRFS_LABEL_SIZE - 1);
4534a8bfd4abSjeff.liu 		return -EINVAL;
4535a8bfd4abSjeff.liu 	}
4536a8bfd4abSjeff.liu 
4537a8bfd4abSjeff.liu 	ret = mnt_want_write_file(file);
4538a8bfd4abSjeff.liu 	if (ret)
4539a8bfd4abSjeff.liu 		return ret;
4540a8bfd4abSjeff.liu 
4541a8bfd4abSjeff.liu 	trans = btrfs_start_transaction(root, 0);
4542a8bfd4abSjeff.liu 	if (IS_ERR(trans)) {
4543a8bfd4abSjeff.liu 		ret = PTR_ERR(trans);
4544a8bfd4abSjeff.liu 		goto out_unlock;
4545a8bfd4abSjeff.liu 	}
4546a8bfd4abSjeff.liu 
4547a1b83ac5SAnand Jain 	spin_lock(&root->fs_info->super_lock);
4548a8bfd4abSjeff.liu 	strcpy(super_block->label, label);
4549a1b83ac5SAnand Jain 	spin_unlock(&root->fs_info->super_lock);
4550d0270acaSJeff Mahoney 	ret = btrfs_commit_transaction(trans, root);
4551a8bfd4abSjeff.liu 
4552a8bfd4abSjeff.liu out_unlock:
4553a8bfd4abSjeff.liu 	mnt_drop_write_file(file);
4554a8bfd4abSjeff.liu 	return ret;
4555a8bfd4abSjeff.liu }
4556a8bfd4abSjeff.liu 
45572eaa055fSJeff Mahoney #define INIT_FEATURE_FLAGS(suffix) \
45582eaa055fSJeff Mahoney 	{ .compat_flags = BTRFS_FEATURE_COMPAT_##suffix, \
45592eaa055fSJeff Mahoney 	  .compat_ro_flags = BTRFS_FEATURE_COMPAT_RO_##suffix, \
45602eaa055fSJeff Mahoney 	  .incompat_flags = BTRFS_FEATURE_INCOMPAT_##suffix }
45612eaa055fSJeff Mahoney 
45622eaa055fSJeff Mahoney static int btrfs_ioctl_get_supported_features(struct file *file,
45632eaa055fSJeff Mahoney 					      void __user *arg)
45642eaa055fSJeff Mahoney {
45652eaa055fSJeff Mahoney 	static struct btrfs_ioctl_feature_flags features[3] = {
45662eaa055fSJeff Mahoney 		INIT_FEATURE_FLAGS(SUPP),
45672eaa055fSJeff Mahoney 		INIT_FEATURE_FLAGS(SAFE_SET),
45682eaa055fSJeff Mahoney 		INIT_FEATURE_FLAGS(SAFE_CLEAR)
45692eaa055fSJeff Mahoney 	};
45702eaa055fSJeff Mahoney 
45712eaa055fSJeff Mahoney 	if (copy_to_user(arg, &features, sizeof(features)))
45722eaa055fSJeff Mahoney 		return -EFAULT;
45732eaa055fSJeff Mahoney 
45742eaa055fSJeff Mahoney 	return 0;
45752eaa055fSJeff Mahoney }
45762eaa055fSJeff Mahoney 
45772eaa055fSJeff Mahoney static int btrfs_ioctl_get_features(struct file *file, void __user *arg)
45782eaa055fSJeff Mahoney {
45792eaa055fSJeff Mahoney 	struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
45802eaa055fSJeff Mahoney 	struct btrfs_super_block *super_block = root->fs_info->super_copy;
45812eaa055fSJeff Mahoney 	struct btrfs_ioctl_feature_flags features;
45822eaa055fSJeff Mahoney 
45832eaa055fSJeff Mahoney 	features.compat_flags = btrfs_super_compat_flags(super_block);
45842eaa055fSJeff Mahoney 	features.compat_ro_flags = btrfs_super_compat_ro_flags(super_block);
45852eaa055fSJeff Mahoney 	features.incompat_flags = btrfs_super_incompat_flags(super_block);
45862eaa055fSJeff Mahoney 
45872eaa055fSJeff Mahoney 	if (copy_to_user(arg, &features, sizeof(features)))
45882eaa055fSJeff Mahoney 		return -EFAULT;
45892eaa055fSJeff Mahoney 
45902eaa055fSJeff Mahoney 	return 0;
45912eaa055fSJeff Mahoney }
45922eaa055fSJeff Mahoney 
45933b02a68aSJeff Mahoney static int check_feature_bits(struct btrfs_root *root,
45943b02a68aSJeff Mahoney 			      enum btrfs_feature_set set,
45952eaa055fSJeff Mahoney 			      u64 change_mask, u64 flags, u64 supported_flags,
45962eaa055fSJeff Mahoney 			      u64 safe_set, u64 safe_clear)
45972eaa055fSJeff Mahoney {
45983b02a68aSJeff Mahoney 	const char *type = btrfs_feature_set_names[set];
45993b02a68aSJeff Mahoney 	char *names;
46002eaa055fSJeff Mahoney 	u64 disallowed, unsupported;
46012eaa055fSJeff Mahoney 	u64 set_mask = flags & change_mask;
46022eaa055fSJeff Mahoney 	u64 clear_mask = ~flags & change_mask;
46032eaa055fSJeff Mahoney 
46042eaa055fSJeff Mahoney 	unsupported = set_mask & ~supported_flags;
46052eaa055fSJeff Mahoney 	if (unsupported) {
46063b02a68aSJeff Mahoney 		names = btrfs_printable_features(set, unsupported);
46073b02a68aSJeff Mahoney 		if (names) {
46083b02a68aSJeff Mahoney 			btrfs_warn(root->fs_info,
46093b02a68aSJeff Mahoney 			   "this kernel does not support the %s feature bit%s",
46103b02a68aSJeff Mahoney 			   names, strchr(names, ',') ? "s" : "");
46113b02a68aSJeff Mahoney 			kfree(names);
46123b02a68aSJeff Mahoney 		} else
46132eaa055fSJeff Mahoney 			btrfs_warn(root->fs_info,
46142eaa055fSJeff Mahoney 			   "this kernel does not support %s bits 0x%llx",
46152eaa055fSJeff Mahoney 			   type, unsupported);
46162eaa055fSJeff Mahoney 		return -EOPNOTSUPP;
46172eaa055fSJeff Mahoney 	}
46182eaa055fSJeff Mahoney 
46192eaa055fSJeff Mahoney 	disallowed = set_mask & ~safe_set;
46202eaa055fSJeff Mahoney 	if (disallowed) {
46213b02a68aSJeff Mahoney 		names = btrfs_printable_features(set, disallowed);
46223b02a68aSJeff Mahoney 		if (names) {
46233b02a68aSJeff Mahoney 			btrfs_warn(root->fs_info,
46243b02a68aSJeff Mahoney 			   "can't set the %s feature bit%s while mounted",
46253b02a68aSJeff Mahoney 			   names, strchr(names, ',') ? "s" : "");
46263b02a68aSJeff Mahoney 			kfree(names);
46273b02a68aSJeff Mahoney 		} else
46282eaa055fSJeff Mahoney 			btrfs_warn(root->fs_info,
46292eaa055fSJeff Mahoney 			   "can't set %s bits 0x%llx while mounted",
46302eaa055fSJeff Mahoney 			   type, disallowed);
46312eaa055fSJeff Mahoney 		return -EPERM;
46322eaa055fSJeff Mahoney 	}
46332eaa055fSJeff Mahoney 
46342eaa055fSJeff Mahoney 	disallowed = clear_mask & ~safe_clear;
46352eaa055fSJeff Mahoney 	if (disallowed) {
46363b02a68aSJeff Mahoney 		names = btrfs_printable_features(set, disallowed);
46373b02a68aSJeff Mahoney 		if (names) {
46383b02a68aSJeff Mahoney 			btrfs_warn(root->fs_info,
46393b02a68aSJeff Mahoney 			   "can't clear the %s feature bit%s while mounted",
46403b02a68aSJeff Mahoney 			   names, strchr(names, ',') ? "s" : "");
46413b02a68aSJeff Mahoney 			kfree(names);
46423b02a68aSJeff Mahoney 		} else
46432eaa055fSJeff Mahoney 			btrfs_warn(root->fs_info,
46442eaa055fSJeff Mahoney 			   "can't clear %s bits 0x%llx while mounted",
46452eaa055fSJeff Mahoney 			   type, disallowed);
46462eaa055fSJeff Mahoney 		return -EPERM;
46472eaa055fSJeff Mahoney 	}
46482eaa055fSJeff Mahoney 
46492eaa055fSJeff Mahoney 	return 0;
46502eaa055fSJeff Mahoney }
46512eaa055fSJeff Mahoney 
46522eaa055fSJeff Mahoney #define check_feature(root, change_mask, flags, mask_base)	\
46533b02a68aSJeff Mahoney check_feature_bits(root, FEAT_##mask_base, change_mask, flags,	\
46542eaa055fSJeff Mahoney 		   BTRFS_FEATURE_ ## mask_base ## _SUPP,	\
46552eaa055fSJeff Mahoney 		   BTRFS_FEATURE_ ## mask_base ## _SAFE_SET,	\
46562eaa055fSJeff Mahoney 		   BTRFS_FEATURE_ ## mask_base ## _SAFE_CLEAR)
46572eaa055fSJeff Mahoney 
46582eaa055fSJeff Mahoney static int btrfs_ioctl_set_features(struct file *file, void __user *arg)
46592eaa055fSJeff Mahoney {
46602eaa055fSJeff Mahoney 	struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
46612eaa055fSJeff Mahoney 	struct btrfs_super_block *super_block = root->fs_info->super_copy;
46622eaa055fSJeff Mahoney 	struct btrfs_ioctl_feature_flags flags[2];
46632eaa055fSJeff Mahoney 	struct btrfs_trans_handle *trans;
46642eaa055fSJeff Mahoney 	u64 newflags;
46652eaa055fSJeff Mahoney 	int ret;
46662eaa055fSJeff Mahoney 
46672eaa055fSJeff Mahoney 	if (!capable(CAP_SYS_ADMIN))
46682eaa055fSJeff Mahoney 		return -EPERM;
46692eaa055fSJeff Mahoney 
46702eaa055fSJeff Mahoney 	if (copy_from_user(flags, arg, sizeof(flags)))
46712eaa055fSJeff Mahoney 		return -EFAULT;
46722eaa055fSJeff Mahoney 
46732eaa055fSJeff Mahoney 	/* Nothing to do */
46742eaa055fSJeff Mahoney 	if (!flags[0].compat_flags && !flags[0].compat_ro_flags &&
46752eaa055fSJeff Mahoney 	    !flags[0].incompat_flags)
46762eaa055fSJeff Mahoney 		return 0;
46772eaa055fSJeff Mahoney 
46782eaa055fSJeff Mahoney 	ret = check_feature(root, flags[0].compat_flags,
46792eaa055fSJeff Mahoney 			    flags[1].compat_flags, COMPAT);
46802eaa055fSJeff Mahoney 	if (ret)
46812eaa055fSJeff Mahoney 		return ret;
46822eaa055fSJeff Mahoney 
46832eaa055fSJeff Mahoney 	ret = check_feature(root, flags[0].compat_ro_flags,
46842eaa055fSJeff Mahoney 			    flags[1].compat_ro_flags, COMPAT_RO);
46852eaa055fSJeff Mahoney 	if (ret)
46862eaa055fSJeff Mahoney 		return ret;
46872eaa055fSJeff Mahoney 
46882eaa055fSJeff Mahoney 	ret = check_feature(root, flags[0].incompat_flags,
46892eaa055fSJeff Mahoney 			    flags[1].incompat_flags, INCOMPAT);
46902eaa055fSJeff Mahoney 	if (ret)
46912eaa055fSJeff Mahoney 		return ret;
46922eaa055fSJeff Mahoney 
46938051aa1aSDavid Sterba 	trans = btrfs_start_transaction(root, 0);
46942eaa055fSJeff Mahoney 	if (IS_ERR(trans))
46952eaa055fSJeff Mahoney 		return PTR_ERR(trans);
46962eaa055fSJeff Mahoney 
46972eaa055fSJeff Mahoney 	spin_lock(&root->fs_info->super_lock);
46982eaa055fSJeff Mahoney 	newflags = btrfs_super_compat_flags(super_block);
46992eaa055fSJeff Mahoney 	newflags |= flags[0].compat_flags & flags[1].compat_flags;
47002eaa055fSJeff Mahoney 	newflags &= ~(flags[0].compat_flags & ~flags[1].compat_flags);
47012eaa055fSJeff Mahoney 	btrfs_set_super_compat_flags(super_block, newflags);
47022eaa055fSJeff Mahoney 
47032eaa055fSJeff Mahoney 	newflags = btrfs_super_compat_ro_flags(super_block);
47042eaa055fSJeff Mahoney 	newflags |= flags[0].compat_ro_flags & flags[1].compat_ro_flags;
47052eaa055fSJeff Mahoney 	newflags &= ~(flags[0].compat_ro_flags & ~flags[1].compat_ro_flags);
47062eaa055fSJeff Mahoney 	btrfs_set_super_compat_ro_flags(super_block, newflags);
47072eaa055fSJeff Mahoney 
47082eaa055fSJeff Mahoney 	newflags = btrfs_super_incompat_flags(super_block);
47092eaa055fSJeff Mahoney 	newflags |= flags[0].incompat_flags & flags[1].incompat_flags;
47102eaa055fSJeff Mahoney 	newflags &= ~(flags[0].incompat_flags & ~flags[1].incompat_flags);
47112eaa055fSJeff Mahoney 	btrfs_set_super_incompat_flags(super_block, newflags);
47122eaa055fSJeff Mahoney 	spin_unlock(&root->fs_info->super_lock);
47132eaa055fSJeff Mahoney 
4714d0270acaSJeff Mahoney 	return btrfs_commit_transaction(trans, root);
47152eaa055fSJeff Mahoney }
47162eaa055fSJeff Mahoney 
4717f46b5a66SChristoph Hellwig long btrfs_ioctl(struct file *file, unsigned int
4718f46b5a66SChristoph Hellwig 		cmd, unsigned long arg)
4719f46b5a66SChristoph Hellwig {
4720496ad9aaSAl Viro 	struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
47214bcabaa3SChristoph Hellwig 	void __user *argp = (void __user *)arg;
4722f46b5a66SChristoph Hellwig 
4723f46b5a66SChristoph Hellwig 	switch (cmd) {
47246cbff00fSChristoph Hellwig 	case FS_IOC_GETFLAGS:
47256cbff00fSChristoph Hellwig 		return btrfs_ioctl_getflags(file, argp);
47266cbff00fSChristoph Hellwig 	case FS_IOC_SETFLAGS:
47276cbff00fSChristoph Hellwig 		return btrfs_ioctl_setflags(file, argp);
47286cbff00fSChristoph Hellwig 	case FS_IOC_GETVERSION:
47296cbff00fSChristoph Hellwig 		return btrfs_ioctl_getversion(file, argp);
4730f7039b1dSLi Dongyang 	case FITRIM:
4731f7039b1dSLi Dongyang 		return btrfs_ioctl_fitrim(file, argp);
4732f46b5a66SChristoph Hellwig 	case BTRFS_IOC_SNAP_CREATE:
4733fa0d2b9bSLi Zefan 		return btrfs_ioctl_snap_create(file, argp, 0);
4734fdfb1e4fSLi Zefan 	case BTRFS_IOC_SNAP_CREATE_V2:
4735fa0d2b9bSLi Zefan 		return btrfs_ioctl_snap_create_v2(file, argp, 0);
47363de4586cSChris Mason 	case BTRFS_IOC_SUBVOL_CREATE:
4737fa0d2b9bSLi Zefan 		return btrfs_ioctl_snap_create(file, argp, 1);
47386f72c7e2SArne Jansen 	case BTRFS_IOC_SUBVOL_CREATE_V2:
47396f72c7e2SArne Jansen 		return btrfs_ioctl_snap_create_v2(file, argp, 1);
474076dda93cSYan, Zheng 	case BTRFS_IOC_SNAP_DESTROY:
474176dda93cSYan, Zheng 		return btrfs_ioctl_snap_destroy(file, argp);
47420caa102dSLi Zefan 	case BTRFS_IOC_SUBVOL_GETFLAGS:
47430caa102dSLi Zefan 		return btrfs_ioctl_subvol_getflags(file, argp);
47440caa102dSLi Zefan 	case BTRFS_IOC_SUBVOL_SETFLAGS:
47450caa102dSLi Zefan 		return btrfs_ioctl_subvol_setflags(file, argp);
47466ef5ed0dSJosef Bacik 	case BTRFS_IOC_DEFAULT_SUBVOL:
47476ef5ed0dSJosef Bacik 		return btrfs_ioctl_default_subvol(file, argp);
4748f46b5a66SChristoph Hellwig 	case BTRFS_IOC_DEFRAG:
47491e701a32SChris Mason 		return btrfs_ioctl_defrag(file, NULL);
47501e701a32SChris Mason 	case BTRFS_IOC_DEFRAG_RANGE:
47511e701a32SChris Mason 		return btrfs_ioctl_defrag(file, argp);
4752f46b5a66SChristoph Hellwig 	case BTRFS_IOC_RESIZE:
4753198605a8SMiao Xie 		return btrfs_ioctl_resize(file, argp);
4754f46b5a66SChristoph Hellwig 	case BTRFS_IOC_ADD_DEV:
47554bcabaa3SChristoph Hellwig 		return btrfs_ioctl_add_dev(root, argp);
4756f46b5a66SChristoph Hellwig 	case BTRFS_IOC_RM_DEV:
4757da24927bSMiao Xie 		return btrfs_ioctl_rm_dev(file, argp);
4758475f6387SJan Schmidt 	case BTRFS_IOC_FS_INFO:
4759475f6387SJan Schmidt 		return btrfs_ioctl_fs_info(root, argp);
4760475f6387SJan Schmidt 	case BTRFS_IOC_DEV_INFO:
4761475f6387SJan Schmidt 		return btrfs_ioctl_dev_info(root, argp);
4762f46b5a66SChristoph Hellwig 	case BTRFS_IOC_BALANCE:
47639ba1f6e4SLiu Bo 		return btrfs_ioctl_balance(file, NULL);
4764f46b5a66SChristoph Hellwig 	case BTRFS_IOC_CLONE:
4765c5c9cd4dSSage Weil 		return btrfs_ioctl_clone(file, arg, 0, 0, 0);
4766c5c9cd4dSSage Weil 	case BTRFS_IOC_CLONE_RANGE:
47677a865e8aSChristoph Hellwig 		return btrfs_ioctl_clone_range(file, argp);
4768f46b5a66SChristoph Hellwig 	case BTRFS_IOC_TRANS_START:
4769f46b5a66SChristoph Hellwig 		return btrfs_ioctl_trans_start(file);
4770f46b5a66SChristoph Hellwig 	case BTRFS_IOC_TRANS_END:
4771f46b5a66SChristoph Hellwig 		return btrfs_ioctl_trans_end(file);
4772ac8e9819SChris Mason 	case BTRFS_IOC_TREE_SEARCH:
4773ac8e9819SChris Mason 		return btrfs_ioctl_tree_search(file, argp);
4774ac8e9819SChris Mason 	case BTRFS_IOC_INO_LOOKUP:
4775ac8e9819SChris Mason 		return btrfs_ioctl_ino_lookup(file, argp);
4776d7728c96SJan Schmidt 	case BTRFS_IOC_INO_PATHS:
4777d7728c96SJan Schmidt 		return btrfs_ioctl_ino_to_path(root, argp);
4778d7728c96SJan Schmidt 	case BTRFS_IOC_LOGICAL_INO:
4779d7728c96SJan Schmidt 		return btrfs_ioctl_logical_to_ino(root, argp);
47801406e432SJosef Bacik 	case BTRFS_IOC_SPACE_INFO:
47811406e432SJosef Bacik 		return btrfs_ioctl_space_info(root, argp);
478201e219e8SJeff Mahoney 	case BTRFS_IOC_GLOBAL_RSV:
478301e219e8SJeff Mahoney 		return btrfs_ioctl_global_rsv(root, argp);
47849b199859SFilipe David Borba Manana 	case BTRFS_IOC_SYNC: {
47859b199859SFilipe David Borba Manana 		int ret;
47869b199859SFilipe David Borba Manana 
478791aef86fSMiao Xie 		ret = btrfs_start_delalloc_roots(root->fs_info, 0);
47889b199859SFilipe David Borba Manana 		if (ret)
47899b199859SFilipe David Borba Manana 			return ret;
47909b199859SFilipe David Borba Manana 		ret = btrfs_sync_fs(file->f_dentry->d_sb, 1);
47919b199859SFilipe David Borba Manana 		return ret;
47929b199859SFilipe David Borba Manana 	}
479346204592SSage Weil 	case BTRFS_IOC_START_SYNC:
47949a8c28beSMiao Xie 		return btrfs_ioctl_start_sync(root, argp);
479546204592SSage Weil 	case BTRFS_IOC_WAIT_SYNC:
47969a8c28beSMiao Xie 		return btrfs_ioctl_wait_sync(root, argp);
4797475f6387SJan Schmidt 	case BTRFS_IOC_SCRUB:
4798b8e95489SMiao Xie 		return btrfs_ioctl_scrub(file, argp);
4799475f6387SJan Schmidt 	case BTRFS_IOC_SCRUB_CANCEL:
4800475f6387SJan Schmidt 		return btrfs_ioctl_scrub_cancel(root, argp);
4801475f6387SJan Schmidt 	case BTRFS_IOC_SCRUB_PROGRESS:
4802475f6387SJan Schmidt 		return btrfs_ioctl_scrub_progress(root, argp);
4803c9e9f97bSIlya Dryomov 	case BTRFS_IOC_BALANCE_V2:
48049ba1f6e4SLiu Bo 		return btrfs_ioctl_balance(file, argp);
4805837d5b6eSIlya Dryomov 	case BTRFS_IOC_BALANCE_CTL:
4806837d5b6eSIlya Dryomov 		return btrfs_ioctl_balance_ctl(root, arg);
480719a39dceSIlya Dryomov 	case BTRFS_IOC_BALANCE_PROGRESS:
480819a39dceSIlya Dryomov 		return btrfs_ioctl_balance_progress(root, argp);
48098ea05e3aSAlexander Block 	case BTRFS_IOC_SET_RECEIVED_SUBVOL:
48108ea05e3aSAlexander Block 		return btrfs_ioctl_set_received_subvol(file, argp);
481131db9f7cSAlexander Block 	case BTRFS_IOC_SEND:
481231db9f7cSAlexander Block 		return btrfs_ioctl_send(file, argp);
4813c11d2c23SStefan Behrens 	case BTRFS_IOC_GET_DEV_STATS:
4814b27f7c0cSDavid Sterba 		return btrfs_ioctl_get_dev_stats(root, argp);
48155d13a37bSArne Jansen 	case BTRFS_IOC_QUOTA_CTL:
4816905b0ddaSMiao Xie 		return btrfs_ioctl_quota_ctl(file, argp);
48175d13a37bSArne Jansen 	case BTRFS_IOC_QGROUP_ASSIGN:
4818905b0ddaSMiao Xie 		return btrfs_ioctl_qgroup_assign(file, argp);
48195d13a37bSArne Jansen 	case BTRFS_IOC_QGROUP_CREATE:
4820905b0ddaSMiao Xie 		return btrfs_ioctl_qgroup_create(file, argp);
48215d13a37bSArne Jansen 	case BTRFS_IOC_QGROUP_LIMIT:
4822905b0ddaSMiao Xie 		return btrfs_ioctl_qgroup_limit(file, argp);
48232f232036SJan Schmidt 	case BTRFS_IOC_QUOTA_RESCAN:
48242f232036SJan Schmidt 		return btrfs_ioctl_quota_rescan(file, argp);
48252f232036SJan Schmidt 	case BTRFS_IOC_QUOTA_RESCAN_STATUS:
48262f232036SJan Schmidt 		return btrfs_ioctl_quota_rescan_status(file, argp);
482757254b6eSJan Schmidt 	case BTRFS_IOC_QUOTA_RESCAN_WAIT:
482857254b6eSJan Schmidt 		return btrfs_ioctl_quota_rescan_wait(file, argp);
48293f6bcfbdSStefan Behrens 	case BTRFS_IOC_DEV_REPLACE:
48303f6bcfbdSStefan Behrens 		return btrfs_ioctl_dev_replace(root, argp);
4831867ab667Sjeff.liu 	case BTRFS_IOC_GET_FSLABEL:
4832867ab667Sjeff.liu 		return btrfs_ioctl_get_fslabel(file, argp);
4833a8bfd4abSjeff.liu 	case BTRFS_IOC_SET_FSLABEL:
4834a8bfd4abSjeff.liu 		return btrfs_ioctl_set_fslabel(file, argp);
4835416161dbSMark Fasheh 	case BTRFS_IOC_FILE_EXTENT_SAME:
4836416161dbSMark Fasheh 		return btrfs_ioctl_file_extent_same(file, argp);
48372eaa055fSJeff Mahoney 	case BTRFS_IOC_GET_SUPPORTED_FEATURES:
48382eaa055fSJeff Mahoney 		return btrfs_ioctl_get_supported_features(file, argp);
48392eaa055fSJeff Mahoney 	case BTRFS_IOC_GET_FEATURES:
48402eaa055fSJeff Mahoney 		return btrfs_ioctl_get_features(file, argp);
48412eaa055fSJeff Mahoney 	case BTRFS_IOC_SET_FEATURES:
48422eaa055fSJeff Mahoney 		return btrfs_ioctl_set_features(file, argp);
4843f46b5a66SChristoph Hellwig 	}
4844f46b5a66SChristoph Hellwig 
4845f46b5a66SChristoph Hellwig 	return -ENOTTY;
4846f46b5a66SChristoph Hellwig }
4847