xref: /openbmc/linux/fs/btrfs/ioctl.c (revision eb8052e0)
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"
593b02a68aSJeff Mahoney #include "sysfs.h"
60f46b5a66SChristoph Hellwig 
61416161dbSMark Fasheh static int btrfs_clone(struct inode *src, struct inode *inode,
62416161dbSMark Fasheh 		       u64 off, u64 olen, u64 olen_aligned, u64 destoff);
63416161dbSMark Fasheh 
646cbff00fSChristoph Hellwig /* Mask out flags that are inappropriate for the given type of inode. */
656cbff00fSChristoph Hellwig static inline __u32 btrfs_mask_flags(umode_t mode, __u32 flags)
666cbff00fSChristoph Hellwig {
676cbff00fSChristoph Hellwig 	if (S_ISDIR(mode))
686cbff00fSChristoph Hellwig 		return flags;
696cbff00fSChristoph Hellwig 	else if (S_ISREG(mode))
706cbff00fSChristoph Hellwig 		return flags & ~FS_DIRSYNC_FL;
716cbff00fSChristoph Hellwig 	else
726cbff00fSChristoph Hellwig 		return flags & (FS_NODUMP_FL | FS_NOATIME_FL);
736cbff00fSChristoph Hellwig }
74f46b5a66SChristoph Hellwig 
756cbff00fSChristoph Hellwig /*
766cbff00fSChristoph Hellwig  * Export inode flags to the format expected by the FS_IOC_GETFLAGS ioctl.
776cbff00fSChristoph Hellwig  */
786cbff00fSChristoph Hellwig static unsigned int btrfs_flags_to_ioctl(unsigned int flags)
796cbff00fSChristoph Hellwig {
806cbff00fSChristoph Hellwig 	unsigned int iflags = 0;
816cbff00fSChristoph Hellwig 
826cbff00fSChristoph Hellwig 	if (flags & BTRFS_INODE_SYNC)
836cbff00fSChristoph Hellwig 		iflags |= FS_SYNC_FL;
846cbff00fSChristoph Hellwig 	if (flags & BTRFS_INODE_IMMUTABLE)
856cbff00fSChristoph Hellwig 		iflags |= FS_IMMUTABLE_FL;
866cbff00fSChristoph Hellwig 	if (flags & BTRFS_INODE_APPEND)
876cbff00fSChristoph Hellwig 		iflags |= FS_APPEND_FL;
886cbff00fSChristoph Hellwig 	if (flags & BTRFS_INODE_NODUMP)
896cbff00fSChristoph Hellwig 		iflags |= FS_NODUMP_FL;
906cbff00fSChristoph Hellwig 	if (flags & BTRFS_INODE_NOATIME)
916cbff00fSChristoph Hellwig 		iflags |= FS_NOATIME_FL;
926cbff00fSChristoph Hellwig 	if (flags & BTRFS_INODE_DIRSYNC)
936cbff00fSChristoph Hellwig 		iflags |= FS_DIRSYNC_FL;
94d0092bddSLi Zefan 	if (flags & BTRFS_INODE_NODATACOW)
95d0092bddSLi Zefan 		iflags |= FS_NOCOW_FL;
96d0092bddSLi Zefan 
97d0092bddSLi Zefan 	if ((flags & BTRFS_INODE_COMPRESS) && !(flags & BTRFS_INODE_NOCOMPRESS))
98d0092bddSLi Zefan 		iflags |= FS_COMPR_FL;
99d0092bddSLi Zefan 	else if (flags & BTRFS_INODE_NOCOMPRESS)
100d0092bddSLi Zefan 		iflags |= FS_NOCOMP_FL;
1016cbff00fSChristoph Hellwig 
1026cbff00fSChristoph Hellwig 	return iflags;
1036cbff00fSChristoph Hellwig }
1046cbff00fSChristoph Hellwig 
1056cbff00fSChristoph Hellwig /*
1066cbff00fSChristoph Hellwig  * Update inode->i_flags based on the btrfs internal flags.
1076cbff00fSChristoph Hellwig  */
1086cbff00fSChristoph Hellwig void btrfs_update_iflags(struct inode *inode)
1096cbff00fSChristoph Hellwig {
1106cbff00fSChristoph Hellwig 	struct btrfs_inode *ip = BTRFS_I(inode);
1116cbff00fSChristoph Hellwig 
1126cbff00fSChristoph Hellwig 	inode->i_flags &= ~(S_SYNC|S_APPEND|S_IMMUTABLE|S_NOATIME|S_DIRSYNC);
1136cbff00fSChristoph Hellwig 
1146cbff00fSChristoph Hellwig 	if (ip->flags & BTRFS_INODE_SYNC)
1156cbff00fSChristoph Hellwig 		inode->i_flags |= S_SYNC;
1166cbff00fSChristoph Hellwig 	if (ip->flags & BTRFS_INODE_IMMUTABLE)
1176cbff00fSChristoph Hellwig 		inode->i_flags |= S_IMMUTABLE;
1186cbff00fSChristoph Hellwig 	if (ip->flags & BTRFS_INODE_APPEND)
1196cbff00fSChristoph Hellwig 		inode->i_flags |= S_APPEND;
1206cbff00fSChristoph Hellwig 	if (ip->flags & BTRFS_INODE_NOATIME)
1216cbff00fSChristoph Hellwig 		inode->i_flags |= S_NOATIME;
1226cbff00fSChristoph Hellwig 	if (ip->flags & BTRFS_INODE_DIRSYNC)
1236cbff00fSChristoph Hellwig 		inode->i_flags |= S_DIRSYNC;
1246cbff00fSChristoph Hellwig }
1256cbff00fSChristoph Hellwig 
1266cbff00fSChristoph Hellwig /*
1276cbff00fSChristoph Hellwig  * Inherit flags from the parent inode.
1286cbff00fSChristoph Hellwig  *
129e27425d6SJosef Bacik  * Currently only the compression flags and the cow flags are inherited.
1306cbff00fSChristoph Hellwig  */
1316cbff00fSChristoph Hellwig void btrfs_inherit_iflags(struct inode *inode, struct inode *dir)
1326cbff00fSChristoph Hellwig {
1330b4dcea5SChris Mason 	unsigned int flags;
1340b4dcea5SChris Mason 
1350b4dcea5SChris Mason 	if (!dir)
1360b4dcea5SChris Mason 		return;
1370b4dcea5SChris Mason 
1380b4dcea5SChris Mason 	flags = BTRFS_I(dir)->flags;
1396cbff00fSChristoph Hellwig 
140e27425d6SJosef Bacik 	if (flags & BTRFS_INODE_NOCOMPRESS) {
141e27425d6SJosef Bacik 		BTRFS_I(inode)->flags &= ~BTRFS_INODE_COMPRESS;
142e27425d6SJosef Bacik 		BTRFS_I(inode)->flags |= BTRFS_INODE_NOCOMPRESS;
143e27425d6SJosef Bacik 	} else if (flags & BTRFS_INODE_COMPRESS) {
144e27425d6SJosef Bacik 		BTRFS_I(inode)->flags &= ~BTRFS_INODE_NOCOMPRESS;
145e27425d6SJosef Bacik 		BTRFS_I(inode)->flags |= BTRFS_INODE_COMPRESS;
146e27425d6SJosef Bacik 	}
1476cbff00fSChristoph Hellwig 
148213490b3SLiu Bo 	if (flags & BTRFS_INODE_NODATACOW) {
149e27425d6SJosef Bacik 		BTRFS_I(inode)->flags |= BTRFS_INODE_NODATACOW;
150213490b3SLiu Bo 		if (S_ISREG(inode->i_mode))
151213490b3SLiu Bo 			BTRFS_I(inode)->flags |= BTRFS_INODE_NODATASUM;
152213490b3SLiu Bo 	}
153e27425d6SJosef Bacik 
1546cbff00fSChristoph Hellwig 	btrfs_update_iflags(inode);
1556cbff00fSChristoph Hellwig }
1566cbff00fSChristoph Hellwig 
1576cbff00fSChristoph Hellwig static int btrfs_ioctl_getflags(struct file *file, void __user *arg)
1586cbff00fSChristoph Hellwig {
159496ad9aaSAl Viro 	struct btrfs_inode *ip = BTRFS_I(file_inode(file));
1606cbff00fSChristoph Hellwig 	unsigned int flags = btrfs_flags_to_ioctl(ip->flags);
1616cbff00fSChristoph Hellwig 
1626cbff00fSChristoph Hellwig 	if (copy_to_user(arg, &flags, sizeof(flags)))
1636cbff00fSChristoph Hellwig 		return -EFAULT;
1646cbff00fSChristoph Hellwig 	return 0;
1656cbff00fSChristoph Hellwig }
1666cbff00fSChristoph Hellwig 
16775e7cb7fSLiu Bo static int check_flags(unsigned int flags)
16875e7cb7fSLiu Bo {
16975e7cb7fSLiu Bo 	if (flags & ~(FS_IMMUTABLE_FL | FS_APPEND_FL | \
17075e7cb7fSLiu Bo 		      FS_NOATIME_FL | FS_NODUMP_FL | \
17175e7cb7fSLiu Bo 		      FS_SYNC_FL | FS_DIRSYNC_FL | \
172e1e8fb6aSLi Zefan 		      FS_NOCOMP_FL | FS_COMPR_FL |
173e1e8fb6aSLi Zefan 		      FS_NOCOW_FL))
17475e7cb7fSLiu Bo 		return -EOPNOTSUPP;
17575e7cb7fSLiu Bo 
17675e7cb7fSLiu Bo 	if ((flags & FS_NOCOMP_FL) && (flags & FS_COMPR_FL))
17775e7cb7fSLiu Bo 		return -EINVAL;
17875e7cb7fSLiu Bo 
17975e7cb7fSLiu Bo 	return 0;
18075e7cb7fSLiu Bo }
18175e7cb7fSLiu Bo 
1826cbff00fSChristoph Hellwig static int btrfs_ioctl_setflags(struct file *file, void __user *arg)
1836cbff00fSChristoph Hellwig {
184496ad9aaSAl Viro 	struct inode *inode = file_inode(file);
1856cbff00fSChristoph Hellwig 	struct btrfs_inode *ip = BTRFS_I(inode);
1866cbff00fSChristoph Hellwig 	struct btrfs_root *root = ip->root;
1876cbff00fSChristoph Hellwig 	struct btrfs_trans_handle *trans;
1886cbff00fSChristoph Hellwig 	unsigned int flags, oldflags;
1896cbff00fSChristoph Hellwig 	int ret;
190f062abf0SLi Zefan 	u64 ip_oldflags;
191f062abf0SLi Zefan 	unsigned int i_oldflags;
1927e97b8daSDavid Sterba 	umode_t mode;
1936cbff00fSChristoph Hellwig 
194b83cc969SLi Zefan 	if (btrfs_root_readonly(root))
195b83cc969SLi Zefan 		return -EROFS;
196b83cc969SLi Zefan 
1976cbff00fSChristoph Hellwig 	if (copy_from_user(&flags, arg, sizeof(flags)))
1986cbff00fSChristoph Hellwig 		return -EFAULT;
1996cbff00fSChristoph Hellwig 
20075e7cb7fSLiu Bo 	ret = check_flags(flags);
20175e7cb7fSLiu Bo 	if (ret)
20275e7cb7fSLiu Bo 		return ret;
2036cbff00fSChristoph Hellwig 
2042e149670SSerge E. Hallyn 	if (!inode_owner_or_capable(inode))
2056cbff00fSChristoph Hellwig 		return -EACCES;
2066cbff00fSChristoph Hellwig 
207e7848683SJan Kara 	ret = mnt_want_write_file(file);
208e7848683SJan Kara 	if (ret)
209e7848683SJan Kara 		return ret;
210e7848683SJan Kara 
2116cbff00fSChristoph Hellwig 	mutex_lock(&inode->i_mutex);
2126cbff00fSChristoph Hellwig 
213f062abf0SLi Zefan 	ip_oldflags = ip->flags;
214f062abf0SLi Zefan 	i_oldflags = inode->i_flags;
2157e97b8daSDavid Sterba 	mode = inode->i_mode;
216f062abf0SLi Zefan 
2176cbff00fSChristoph Hellwig 	flags = btrfs_mask_flags(inode->i_mode, flags);
2186cbff00fSChristoph Hellwig 	oldflags = btrfs_flags_to_ioctl(ip->flags);
2196cbff00fSChristoph Hellwig 	if ((flags ^ oldflags) & (FS_APPEND_FL | FS_IMMUTABLE_FL)) {
2206cbff00fSChristoph Hellwig 		if (!capable(CAP_LINUX_IMMUTABLE)) {
2216cbff00fSChristoph Hellwig 			ret = -EPERM;
2226cbff00fSChristoph Hellwig 			goto out_unlock;
2236cbff00fSChristoph Hellwig 		}
2246cbff00fSChristoph Hellwig 	}
2256cbff00fSChristoph Hellwig 
2266cbff00fSChristoph Hellwig 	if (flags & FS_SYNC_FL)
2276cbff00fSChristoph Hellwig 		ip->flags |= BTRFS_INODE_SYNC;
2286cbff00fSChristoph Hellwig 	else
2296cbff00fSChristoph Hellwig 		ip->flags &= ~BTRFS_INODE_SYNC;
2306cbff00fSChristoph Hellwig 	if (flags & FS_IMMUTABLE_FL)
2316cbff00fSChristoph Hellwig 		ip->flags |= BTRFS_INODE_IMMUTABLE;
2326cbff00fSChristoph Hellwig 	else
2336cbff00fSChristoph Hellwig 		ip->flags &= ~BTRFS_INODE_IMMUTABLE;
2346cbff00fSChristoph Hellwig 	if (flags & FS_APPEND_FL)
2356cbff00fSChristoph Hellwig 		ip->flags |= BTRFS_INODE_APPEND;
2366cbff00fSChristoph Hellwig 	else
2376cbff00fSChristoph Hellwig 		ip->flags &= ~BTRFS_INODE_APPEND;
2386cbff00fSChristoph Hellwig 	if (flags & FS_NODUMP_FL)
2396cbff00fSChristoph Hellwig 		ip->flags |= BTRFS_INODE_NODUMP;
2406cbff00fSChristoph Hellwig 	else
2416cbff00fSChristoph Hellwig 		ip->flags &= ~BTRFS_INODE_NODUMP;
2426cbff00fSChristoph Hellwig 	if (flags & FS_NOATIME_FL)
2436cbff00fSChristoph Hellwig 		ip->flags |= BTRFS_INODE_NOATIME;
2446cbff00fSChristoph Hellwig 	else
2456cbff00fSChristoph Hellwig 		ip->flags &= ~BTRFS_INODE_NOATIME;
2466cbff00fSChristoph Hellwig 	if (flags & FS_DIRSYNC_FL)
2476cbff00fSChristoph Hellwig 		ip->flags |= BTRFS_INODE_DIRSYNC;
2486cbff00fSChristoph Hellwig 	else
2496cbff00fSChristoph Hellwig 		ip->flags &= ~BTRFS_INODE_DIRSYNC;
2507e97b8daSDavid Sterba 	if (flags & FS_NOCOW_FL) {
2517e97b8daSDavid Sterba 		if (S_ISREG(mode)) {
2527e97b8daSDavid Sterba 			/*
2537e97b8daSDavid Sterba 			 * It's safe to turn csums off here, no extents exist.
2547e97b8daSDavid Sterba 			 * Otherwise we want the flag to reflect the real COW
2557e97b8daSDavid Sterba 			 * status of the file and will not set it.
2567e97b8daSDavid Sterba 			 */
2577e97b8daSDavid Sterba 			if (inode->i_size == 0)
2587e97b8daSDavid Sterba 				ip->flags |= BTRFS_INODE_NODATACOW
2597e97b8daSDavid Sterba 					   | BTRFS_INODE_NODATASUM;
2607e97b8daSDavid Sterba 		} else {
261e1e8fb6aSLi Zefan 			ip->flags |= BTRFS_INODE_NODATACOW;
2627e97b8daSDavid Sterba 		}
2637e97b8daSDavid Sterba 	} else {
2647e97b8daSDavid Sterba 		/*
2657e97b8daSDavid Sterba 		 * Revert back under same assuptions as above
2667e97b8daSDavid Sterba 		 */
2677e97b8daSDavid Sterba 		if (S_ISREG(mode)) {
2687e97b8daSDavid Sterba 			if (inode->i_size == 0)
2697e97b8daSDavid Sterba 				ip->flags &= ~(BTRFS_INODE_NODATACOW
2707e97b8daSDavid Sterba 				             | BTRFS_INODE_NODATASUM);
2717e97b8daSDavid Sterba 		} else {
272e1e8fb6aSLi Zefan 			ip->flags &= ~BTRFS_INODE_NODATACOW;
2737e97b8daSDavid Sterba 		}
2747e97b8daSDavid Sterba 	}
2756cbff00fSChristoph Hellwig 
27675e7cb7fSLiu Bo 	/*
27775e7cb7fSLiu Bo 	 * The COMPRESS flag can only be changed by users, while the NOCOMPRESS
27875e7cb7fSLiu Bo 	 * flag may be changed automatically if compression code won't make
27975e7cb7fSLiu Bo 	 * things smaller.
28075e7cb7fSLiu Bo 	 */
28175e7cb7fSLiu Bo 	if (flags & FS_NOCOMP_FL) {
28275e7cb7fSLiu Bo 		ip->flags &= ~BTRFS_INODE_COMPRESS;
28375e7cb7fSLiu Bo 		ip->flags |= BTRFS_INODE_NOCOMPRESS;
28475e7cb7fSLiu Bo 	} else if (flags & FS_COMPR_FL) {
28575e7cb7fSLiu Bo 		ip->flags |= BTRFS_INODE_COMPRESS;
28675e7cb7fSLiu Bo 		ip->flags &= ~BTRFS_INODE_NOCOMPRESS;
287ebcb904dSLi Zefan 	} else {
288ebcb904dSLi Zefan 		ip->flags &= ~(BTRFS_INODE_COMPRESS | BTRFS_INODE_NOCOMPRESS);
28975e7cb7fSLiu Bo 	}
2906cbff00fSChristoph Hellwig 
2914da6f1a3SLi Zefan 	trans = btrfs_start_transaction(root, 1);
292f062abf0SLi Zefan 	if (IS_ERR(trans)) {
293f062abf0SLi Zefan 		ret = PTR_ERR(trans);
294f062abf0SLi Zefan 		goto out_drop;
295f062abf0SLi Zefan 	}
2966cbff00fSChristoph Hellwig 
297306424ccSLi Zefan 	btrfs_update_iflags(inode);
2980c4d2d95SJosef Bacik 	inode_inc_iversion(inode);
299306424ccSLi Zefan 	inode->i_ctime = CURRENT_TIME;
3006cbff00fSChristoph Hellwig 	ret = btrfs_update_inode(trans, root, inode);
3016cbff00fSChristoph Hellwig 
3026cbff00fSChristoph Hellwig 	btrfs_end_transaction(trans, root);
303f062abf0SLi Zefan  out_drop:
304f062abf0SLi Zefan 	if (ret) {
305f062abf0SLi Zefan 		ip->flags = ip_oldflags;
306f062abf0SLi Zefan 		inode->i_flags = i_oldflags;
307f062abf0SLi Zefan 	}
3086cbff00fSChristoph Hellwig 
3096cbff00fSChristoph Hellwig  out_unlock:
3106cbff00fSChristoph Hellwig 	mutex_unlock(&inode->i_mutex);
311e7848683SJan Kara 	mnt_drop_write_file(file);
3122d4e6f6aSliubo 	return ret;
3136cbff00fSChristoph Hellwig }
3146cbff00fSChristoph Hellwig 
3156cbff00fSChristoph Hellwig static int btrfs_ioctl_getversion(struct file *file, int __user *arg)
3166cbff00fSChristoph Hellwig {
317496ad9aaSAl Viro 	struct inode *inode = file_inode(file);
3186cbff00fSChristoph Hellwig 
3196cbff00fSChristoph Hellwig 	return put_user(inode->i_generation, arg);
3206cbff00fSChristoph Hellwig }
321f46b5a66SChristoph Hellwig 
322f7039b1dSLi Dongyang static noinline int btrfs_ioctl_fitrim(struct file *file, void __user *arg)
323f7039b1dSLi Dongyang {
32454563d41SAl Viro 	struct btrfs_fs_info *fs_info = btrfs_sb(file_inode(file)->i_sb);
325f7039b1dSLi Dongyang 	struct btrfs_device *device;
326f7039b1dSLi Dongyang 	struct request_queue *q;
327f7039b1dSLi Dongyang 	struct fstrim_range range;
328f7039b1dSLi Dongyang 	u64 minlen = ULLONG_MAX;
329f7039b1dSLi Dongyang 	u64 num_devices = 0;
330815745cfSAl Viro 	u64 total_bytes = btrfs_super_total_bytes(fs_info->super_copy);
331f7039b1dSLi Dongyang 	int ret;
332f7039b1dSLi Dongyang 
333f7039b1dSLi Dongyang 	if (!capable(CAP_SYS_ADMIN))
334f7039b1dSLi Dongyang 		return -EPERM;
335f7039b1dSLi Dongyang 
3361f78160cSXiao Guangrong 	rcu_read_lock();
3371f78160cSXiao Guangrong 	list_for_each_entry_rcu(device, &fs_info->fs_devices->devices,
3381f78160cSXiao Guangrong 				dev_list) {
339f7039b1dSLi Dongyang 		if (!device->bdev)
340f7039b1dSLi Dongyang 			continue;
341f7039b1dSLi Dongyang 		q = bdev_get_queue(device->bdev);
342f7039b1dSLi Dongyang 		if (blk_queue_discard(q)) {
343f7039b1dSLi Dongyang 			num_devices++;
344f7039b1dSLi Dongyang 			minlen = min((u64)q->limits.discard_granularity,
345f7039b1dSLi Dongyang 				     minlen);
346f7039b1dSLi Dongyang 		}
347f7039b1dSLi Dongyang 	}
3481f78160cSXiao Guangrong 	rcu_read_unlock();
349f4c697e6SLukas Czerner 
350f7039b1dSLi Dongyang 	if (!num_devices)
351f7039b1dSLi Dongyang 		return -EOPNOTSUPP;
352f7039b1dSLi Dongyang 	if (copy_from_user(&range, arg, sizeof(range)))
353f7039b1dSLi Dongyang 		return -EFAULT;
354e515c18bSLukas Czerner 	if (range.start > total_bytes ||
355e515c18bSLukas Czerner 	    range.len < fs_info->sb->s_blocksize)
356f4c697e6SLukas Czerner 		return -EINVAL;
357f7039b1dSLi Dongyang 
358f4c697e6SLukas Czerner 	range.len = min(range.len, total_bytes - range.start);
359f7039b1dSLi Dongyang 	range.minlen = max(range.minlen, minlen);
360815745cfSAl Viro 	ret = btrfs_trim_fs(fs_info->tree_root, &range);
361f7039b1dSLi Dongyang 	if (ret < 0)
362f7039b1dSLi Dongyang 		return ret;
363f7039b1dSLi Dongyang 
364f7039b1dSLi Dongyang 	if (copy_to_user(arg, &range, sizeof(range)))
365f7039b1dSLi Dongyang 		return -EFAULT;
366f7039b1dSLi Dongyang 
367f7039b1dSLi Dongyang 	return 0;
368f7039b1dSLi Dongyang }
369f7039b1dSLi Dongyang 
370dd5f9615SStefan Behrens int btrfs_is_empty_uuid(u8 *uuid)
371dd5f9615SStefan Behrens {
37246e0f66aSChris Mason 	int i;
37346e0f66aSChris Mason 
37446e0f66aSChris Mason 	for (i = 0; i < BTRFS_UUID_SIZE; i++) {
37546e0f66aSChris Mason 		if (uuid[i])
37646e0f66aSChris Mason 			return 0;
37746e0f66aSChris Mason 	}
37846e0f66aSChris Mason 	return 1;
379dd5f9615SStefan Behrens }
380dd5f9615SStefan Behrens 
381d5c12070SMiao Xie static noinline int create_subvol(struct inode *dir,
382cb8e7090SChristoph Hellwig 				  struct dentry *dentry,
38372fd032eSSage Weil 				  char *name, int namelen,
3846f72c7e2SArne Jansen 				  u64 *async_transid,
3858696c533SMiao Xie 				  struct btrfs_qgroup_inherit *inherit)
386f46b5a66SChristoph Hellwig {
387f46b5a66SChristoph Hellwig 	struct btrfs_trans_handle *trans;
388f46b5a66SChristoph Hellwig 	struct btrfs_key key;
389f46b5a66SChristoph Hellwig 	struct btrfs_root_item root_item;
390f46b5a66SChristoph Hellwig 	struct btrfs_inode_item *inode_item;
391f46b5a66SChristoph Hellwig 	struct extent_buffer *leaf;
392d5c12070SMiao Xie 	struct btrfs_root *root = BTRFS_I(dir)->root;
39376dda93cSYan, Zheng 	struct btrfs_root *new_root;
394d5c12070SMiao Xie 	struct btrfs_block_rsv block_rsv;
3958ea05e3aSAlexander Block 	struct timespec cur_time = CURRENT_TIME;
3965662344bSTsutomu Itoh 	struct inode *inode;
397f46b5a66SChristoph Hellwig 	int ret;
398f46b5a66SChristoph Hellwig 	int err;
399f46b5a66SChristoph Hellwig 	u64 objectid;
400f46b5a66SChristoph Hellwig 	u64 new_dirid = BTRFS_FIRST_FREE_OBJECTID;
4013de4586cSChris Mason 	u64 index = 0;
402d5c12070SMiao Xie 	u64 qgroup_reserved;
4038ea05e3aSAlexander Block 	uuid_le new_uuid;
404f46b5a66SChristoph Hellwig 
405581bb050SLi Zefan 	ret = btrfs_find_free_objectid(root->fs_info->tree_root, &objectid);
4062fbe8c8aSAl Viro 	if (ret)
407a22285a6SYan, Zheng 		return ret;
4086a912213SJosef Bacik 
409d5c12070SMiao Xie 	btrfs_init_block_rsv(&block_rsv, BTRFS_BLOCK_RSV_TEMP);
4109ed74f2dSJosef Bacik 	/*
411d5c12070SMiao Xie 	 * The same as the snapshot creation, please see the comment
412d5c12070SMiao Xie 	 * of create_snapshot().
4139ed74f2dSJosef Bacik 	 */
414d5c12070SMiao Xie 	ret = btrfs_subvolume_reserve_metadata(root, &block_rsv,
415dd5f9615SStefan Behrens 					       8, &qgroup_reserved, false);
416d5c12070SMiao Xie 	if (ret)
417d5c12070SMiao Xie 		return ret;
418f46b5a66SChristoph Hellwig 
419d5c12070SMiao Xie 	trans = btrfs_start_transaction(root, 0);
420d5c12070SMiao Xie 	if (IS_ERR(trans)) {
421d5c12070SMiao Xie 		ret = PTR_ERR(trans);
422d5c12070SMiao Xie 		goto out;
423d5c12070SMiao Xie 	}
424d5c12070SMiao Xie 	trans->block_rsv = &block_rsv;
425d5c12070SMiao Xie 	trans->bytes_reserved = block_rsv.size;
426f46b5a66SChristoph Hellwig 
4278696c533SMiao Xie 	ret = btrfs_qgroup_inherit(trans, root->fs_info, 0, objectid, inherit);
4286f72c7e2SArne Jansen 	if (ret)
4296f72c7e2SArne Jansen 		goto fail;
4306f72c7e2SArne Jansen 
4315d4f98a2SYan Zheng 	leaf = btrfs_alloc_free_block(trans, root, root->leafsize,
4325581a51aSJan Schmidt 				      0, objectid, NULL, 0, 0, 0);
4338e8a1e31SJosef Bacik 	if (IS_ERR(leaf)) {
4348e8a1e31SJosef Bacik 		ret = PTR_ERR(leaf);
4358e8a1e31SJosef Bacik 		goto fail;
4368e8a1e31SJosef Bacik 	}
437f46b5a66SChristoph Hellwig 
4385d4f98a2SYan Zheng 	memset_extent_buffer(leaf, 0, 0, sizeof(struct btrfs_header));
439f46b5a66SChristoph Hellwig 	btrfs_set_header_bytenr(leaf, leaf->start);
440f46b5a66SChristoph Hellwig 	btrfs_set_header_generation(leaf, trans->transid);
4415d4f98a2SYan Zheng 	btrfs_set_header_backref_rev(leaf, BTRFS_MIXED_BACKREF_REV);
442f46b5a66SChristoph Hellwig 	btrfs_set_header_owner(leaf, objectid);
443f46b5a66SChristoph Hellwig 
4440a4e5586SRoss Kirk 	write_extent_buffer(leaf, root->fs_info->fsid, btrfs_header_fsid(),
445f46b5a66SChristoph Hellwig 			    BTRFS_FSID_SIZE);
4465d4f98a2SYan Zheng 	write_extent_buffer(leaf, root->fs_info->chunk_tree_uuid,
447b308bc2fSGeert Uytterhoeven 			    btrfs_header_chunk_tree_uuid(leaf),
4485d4f98a2SYan Zheng 			    BTRFS_UUID_SIZE);
449f46b5a66SChristoph Hellwig 	btrfs_mark_buffer_dirty(leaf);
450f46b5a66SChristoph Hellwig 
4518ea05e3aSAlexander Block 	memset(&root_item, 0, sizeof(root_item));
4528ea05e3aSAlexander Block 
453f46b5a66SChristoph Hellwig 	inode_item = &root_item.inode;
4543cae210fSQu Wenruo 	btrfs_set_stack_inode_generation(inode_item, 1);
4553cae210fSQu Wenruo 	btrfs_set_stack_inode_size(inode_item, 3);
4563cae210fSQu Wenruo 	btrfs_set_stack_inode_nlink(inode_item, 1);
4573cae210fSQu Wenruo 	btrfs_set_stack_inode_nbytes(inode_item, root->leafsize);
4583cae210fSQu Wenruo 	btrfs_set_stack_inode_mode(inode_item, S_IFDIR | 0755);
459f46b5a66SChristoph Hellwig 
4603cae210fSQu Wenruo 	btrfs_set_root_flags(&root_item, 0);
4613cae210fSQu Wenruo 	btrfs_set_root_limit(&root_item, 0);
4623cae210fSQu Wenruo 	btrfs_set_stack_inode_flags(inode_item, BTRFS_INODE_ROOT_ITEM_INIT);
46308fe4db1SLi Zefan 
464f46b5a66SChristoph Hellwig 	btrfs_set_root_bytenr(&root_item, leaf->start);
46584234f3aSYan Zheng 	btrfs_set_root_generation(&root_item, trans->transid);
466f46b5a66SChristoph Hellwig 	btrfs_set_root_level(&root_item, 0);
467f46b5a66SChristoph Hellwig 	btrfs_set_root_refs(&root_item, 1);
46886b9f2ecSYan, Zheng 	btrfs_set_root_used(&root_item, leaf->len);
46980ff3856SYan Zheng 	btrfs_set_root_last_snapshot(&root_item, 0);
470f46b5a66SChristoph Hellwig 
4718ea05e3aSAlexander Block 	btrfs_set_root_generation_v2(&root_item,
4728ea05e3aSAlexander Block 			btrfs_root_generation(&root_item));
4738ea05e3aSAlexander Block 	uuid_le_gen(&new_uuid);
4748ea05e3aSAlexander Block 	memcpy(root_item.uuid, new_uuid.b, BTRFS_UUID_SIZE);
4753cae210fSQu Wenruo 	btrfs_set_stack_timespec_sec(&root_item.otime, cur_time.tv_sec);
4763cae210fSQu Wenruo 	btrfs_set_stack_timespec_nsec(&root_item.otime, cur_time.tv_nsec);
4778ea05e3aSAlexander Block 	root_item.ctime = root_item.otime;
4788ea05e3aSAlexander Block 	btrfs_set_root_ctransid(&root_item, trans->transid);
4798ea05e3aSAlexander Block 	btrfs_set_root_otransid(&root_item, trans->transid);
480f46b5a66SChristoph Hellwig 
481925baeddSChris Mason 	btrfs_tree_unlock(leaf);
482f46b5a66SChristoph Hellwig 	free_extent_buffer(leaf);
483f46b5a66SChristoph Hellwig 	leaf = NULL;
484f46b5a66SChristoph Hellwig 
485f46b5a66SChristoph Hellwig 	btrfs_set_root_dirid(&root_item, new_dirid);
486f46b5a66SChristoph Hellwig 
487f46b5a66SChristoph Hellwig 	key.objectid = objectid;
4885d4f98a2SYan Zheng 	key.offset = 0;
489f46b5a66SChristoph Hellwig 	btrfs_set_key_type(&key, BTRFS_ROOT_ITEM_KEY);
490f46b5a66SChristoph Hellwig 	ret = btrfs_insert_root(trans, root->fs_info->tree_root, &key,
491f46b5a66SChristoph Hellwig 				&root_item);
492f46b5a66SChristoph Hellwig 	if (ret)
493f46b5a66SChristoph Hellwig 		goto fail;
494f46b5a66SChristoph Hellwig 
49576dda93cSYan, Zheng 	key.offset = (u64)-1;
49676dda93cSYan, Zheng 	new_root = btrfs_read_fs_root_no_name(root->fs_info, &key);
49779787eaaSJeff Mahoney 	if (IS_ERR(new_root)) {
49879787eaaSJeff Mahoney 		btrfs_abort_transaction(trans, root, PTR_ERR(new_root));
49979787eaaSJeff Mahoney 		ret = PTR_ERR(new_root);
50079787eaaSJeff Mahoney 		goto fail;
50179787eaaSJeff Mahoney 	}
50276dda93cSYan, Zheng 
50376dda93cSYan, Zheng 	btrfs_record_root_in_trans(trans, new_root);
50476dda93cSYan, Zheng 
505d82a6f1dSJosef Bacik 	ret = btrfs_create_subvol_root(trans, new_root, new_dirid);
506ce598979SMark Fasheh 	if (ret) {
507ce598979SMark Fasheh 		/* We potentially lose an unused inode item here */
50879787eaaSJeff Mahoney 		btrfs_abort_transaction(trans, root, ret);
509ce598979SMark Fasheh 		goto fail;
510ce598979SMark Fasheh 	}
511ce598979SMark Fasheh 
512f46b5a66SChristoph Hellwig 	/*
513f46b5a66SChristoph Hellwig 	 * insert the directory item
514f46b5a66SChristoph Hellwig 	 */
5153de4586cSChris Mason 	ret = btrfs_set_inode_index(dir, &index);
51679787eaaSJeff Mahoney 	if (ret) {
51779787eaaSJeff Mahoney 		btrfs_abort_transaction(trans, root, ret);
51879787eaaSJeff Mahoney 		goto fail;
51979787eaaSJeff Mahoney 	}
5203de4586cSChris Mason 
5213de4586cSChris Mason 	ret = btrfs_insert_dir_item(trans, root,
52216cdcec7SMiao Xie 				    name, namelen, dir, &key,
5233de4586cSChris Mason 				    BTRFS_FT_DIR, index);
52479787eaaSJeff Mahoney 	if (ret) {
52579787eaaSJeff Mahoney 		btrfs_abort_transaction(trans, root, ret);
526f46b5a66SChristoph Hellwig 		goto fail;
52779787eaaSJeff Mahoney 	}
5280660b5afSChris Mason 
52952c26179SYan Zheng 	btrfs_i_size_write(dir, dir->i_size + namelen * 2);
53052c26179SYan Zheng 	ret = btrfs_update_inode(trans, root, dir);
53152c26179SYan Zheng 	BUG_ON(ret);
53252c26179SYan Zheng 
5330660b5afSChris Mason 	ret = btrfs_add_root_ref(trans, root->fs_info->tree_root,
5344df27c4dSYan, Zheng 				 objectid, root->root_key.objectid,
53533345d01SLi Zefan 				 btrfs_ino(dir), index, name, namelen);
5360660b5afSChris Mason 	BUG_ON(ret);
5370660b5afSChris Mason 
538dd5f9615SStefan Behrens 	ret = btrfs_uuid_tree_add(trans, root->fs_info->uuid_root,
539dd5f9615SStefan Behrens 				  root_item.uuid, BTRFS_UUID_KEY_SUBVOL,
540dd5f9615SStefan Behrens 				  objectid);
541dd5f9615SStefan Behrens 	if (ret)
542dd5f9615SStefan Behrens 		btrfs_abort_transaction(trans, root, ret);
543dd5f9615SStefan Behrens 
544f46b5a66SChristoph Hellwig fail:
545d5c12070SMiao Xie 	trans->block_rsv = NULL;
546d5c12070SMiao Xie 	trans->bytes_reserved = 0;
54772fd032eSSage Weil 	if (async_transid) {
54872fd032eSSage Weil 		*async_transid = trans->transid;
54972fd032eSSage Weil 		err = btrfs_commit_transaction_async(trans, root, 1);
55000d71c9cSMiao Xie 		if (err)
55100d71c9cSMiao Xie 			err = btrfs_commit_transaction(trans, root);
55272fd032eSSage Weil 	} else {
55376dda93cSYan, Zheng 		err = btrfs_commit_transaction(trans, root);
55472fd032eSSage Weil 	}
555f46b5a66SChristoph Hellwig 	if (err && !ret)
556f46b5a66SChristoph Hellwig 		ret = err;
5571a65e24bSChris Mason 
5585662344bSTsutomu Itoh 	if (!ret) {
5595662344bSTsutomu Itoh 		inode = btrfs_lookup_dentry(dir, dentry);
5605662344bSTsutomu Itoh 		if (IS_ERR(inode)) {
5615662344bSTsutomu Itoh 			ret = PTR_ERR(inode);
5625662344bSTsutomu Itoh 			goto out;
5635662344bSTsutomu Itoh 		}
5645662344bSTsutomu Itoh 		d_instantiate(dentry, inode);
5655662344bSTsutomu Itoh 	}
566d5c12070SMiao Xie out:
567d5c12070SMiao Xie 	btrfs_subvolume_release_metadata(root, &block_rsv, qgroup_reserved);
568f46b5a66SChristoph Hellwig 	return ret;
569f46b5a66SChristoph Hellwig }
570f46b5a66SChristoph Hellwig 
571e9662f70SMiao Xie static int create_snapshot(struct btrfs_root *root, struct inode *dir,
572e9662f70SMiao Xie 			   struct dentry *dentry, char *name, int namelen,
573e9662f70SMiao Xie 			   u64 *async_transid, bool readonly,
574e9662f70SMiao Xie 			   struct btrfs_qgroup_inherit *inherit)
575f46b5a66SChristoph Hellwig {
5762e4bfab9SYan, Zheng 	struct inode *inode;
577f46b5a66SChristoph Hellwig 	struct btrfs_pending_snapshot *pending_snapshot;
578f46b5a66SChristoph Hellwig 	struct btrfs_trans_handle *trans;
5792e4bfab9SYan, Zheng 	int ret;
580f46b5a66SChristoph Hellwig 
581f46b5a66SChristoph Hellwig 	if (!root->ref_cows)
582f46b5a66SChristoph Hellwig 		return -EINVAL;
583f46b5a66SChristoph Hellwig 
5846a03843dSMiao Xie 	ret = btrfs_start_delalloc_inodes(root, 0);
5856a03843dSMiao Xie 	if (ret)
5866a03843dSMiao Xie 		return ret;
5876a03843dSMiao Xie 
588b0244199SMiao Xie 	btrfs_wait_ordered_extents(root, -1);
5896a03843dSMiao Xie 
590a22285a6SYan, Zheng 	pending_snapshot = kzalloc(sizeof(*pending_snapshot), GFP_NOFS);
591a22285a6SYan, Zheng 	if (!pending_snapshot)
592a22285a6SYan, Zheng 		return -ENOMEM;
593a22285a6SYan, Zheng 
59466d8f3ddSMiao Xie 	btrfs_init_block_rsv(&pending_snapshot->block_rsv,
59566d8f3ddSMiao Xie 			     BTRFS_BLOCK_RSV_TEMP);
596d5c12070SMiao Xie 	/*
597d5c12070SMiao Xie 	 * 1 - parent dir inode
598d5c12070SMiao Xie 	 * 2 - dir entries
599d5c12070SMiao Xie 	 * 1 - root item
600d5c12070SMiao Xie 	 * 2 - root ref/backref
601d5c12070SMiao Xie 	 * 1 - root of snapshot
602dd5f9615SStefan Behrens 	 * 1 - UUID item
603d5c12070SMiao Xie 	 */
604d5c12070SMiao Xie 	ret = btrfs_subvolume_reserve_metadata(BTRFS_I(dir)->root,
605dd5f9615SStefan Behrens 					&pending_snapshot->block_rsv, 8,
606ee3441b4SJeff Mahoney 					&pending_snapshot->qgroup_reserved,
607ee3441b4SJeff Mahoney 					false);
608d5c12070SMiao Xie 	if (ret)
609d5c12070SMiao Xie 		goto out;
610d5c12070SMiao Xie 
611a22285a6SYan, Zheng 	pending_snapshot->dentry = dentry;
612a22285a6SYan, Zheng 	pending_snapshot->root = root;
613b83cc969SLi Zefan 	pending_snapshot->readonly = readonly;
614e9662f70SMiao Xie 	pending_snapshot->dir = dir;
6158696c533SMiao Xie 	pending_snapshot->inherit = inherit;
616a22285a6SYan, Zheng 
617d5c12070SMiao Xie 	trans = btrfs_start_transaction(root, 0);
618a22285a6SYan, Zheng 	if (IS_ERR(trans)) {
619a22285a6SYan, Zheng 		ret = PTR_ERR(trans);
620a22285a6SYan, Zheng 		goto fail;
621a22285a6SYan, Zheng 	}
622a22285a6SYan, Zheng 
6238351583eSJosef Bacik 	spin_lock(&root->fs_info->trans_lock);
624a22285a6SYan, Zheng 	list_add(&pending_snapshot->list,
625a22285a6SYan, Zheng 		 &trans->transaction->pending_snapshots);
6268351583eSJosef Bacik 	spin_unlock(&root->fs_info->trans_lock);
62772fd032eSSage Weil 	if (async_transid) {
62872fd032eSSage Weil 		*async_transid = trans->transid;
62972fd032eSSage Weil 		ret = btrfs_commit_transaction_async(trans,
63072fd032eSSage Weil 				     root->fs_info->extent_root, 1);
63100d71c9cSMiao Xie 		if (ret)
63200d71c9cSMiao Xie 			ret = btrfs_commit_transaction(trans, root);
63372fd032eSSage Weil 	} else {
63472fd032eSSage Weil 		ret = btrfs_commit_transaction(trans,
63572fd032eSSage Weil 					       root->fs_info->extent_root);
63672fd032eSSage Weil 	}
637aec8030aSMiao Xie 	if (ret)
638c37b2b62SJosef Bacik 		goto fail;
639a22285a6SYan, Zheng 
640a22285a6SYan, Zheng 	ret = pending_snapshot->error;
641f46b5a66SChristoph Hellwig 	if (ret)
6422e4bfab9SYan, Zheng 		goto fail;
643f46b5a66SChristoph Hellwig 
64466b4ffd1SJosef Bacik 	ret = btrfs_orphan_cleanup(pending_snapshot->snap);
64566b4ffd1SJosef Bacik 	if (ret)
64666b4ffd1SJosef Bacik 		goto fail;
647f46b5a66SChristoph Hellwig 
6482fbe8c8aSAl Viro 	inode = btrfs_lookup_dentry(dentry->d_parent->d_inode, dentry);
6492e4bfab9SYan, Zheng 	if (IS_ERR(inode)) {
6502e4bfab9SYan, Zheng 		ret = PTR_ERR(inode);
6512e4bfab9SYan, Zheng 		goto fail;
6522e4bfab9SYan, Zheng 	}
6535662344bSTsutomu Itoh 
6542e4bfab9SYan, Zheng 	d_instantiate(dentry, inode);
6552e4bfab9SYan, Zheng 	ret = 0;
6562e4bfab9SYan, Zheng fail:
657d5c12070SMiao Xie 	btrfs_subvolume_release_metadata(BTRFS_I(dir)->root,
658d5c12070SMiao Xie 					 &pending_snapshot->block_rsv,
659d5c12070SMiao Xie 					 pending_snapshot->qgroup_reserved);
660d5c12070SMiao Xie out:
661a22285a6SYan, Zheng 	kfree(pending_snapshot);
662f46b5a66SChristoph Hellwig 	return ret;
663f46b5a66SChristoph Hellwig }
664f46b5a66SChristoph Hellwig 
6654260f7c7SSage Weil /*  copy of check_sticky in fs/namei.c()
6664260f7c7SSage Weil * It's inline, so penalty for filesystems that don't use sticky bit is
6674260f7c7SSage Weil * minimal.
6684260f7c7SSage Weil */
6694260f7c7SSage Weil static inline int btrfs_check_sticky(struct inode *dir, struct inode *inode)
6704260f7c7SSage Weil {
6712f2f43d3SEric W. Biederman 	kuid_t fsuid = current_fsuid();
6724260f7c7SSage Weil 
6734260f7c7SSage Weil 	if (!(dir->i_mode & S_ISVTX))
6744260f7c7SSage Weil 		return 0;
6752f2f43d3SEric W. Biederman 	if (uid_eq(inode->i_uid, fsuid))
6764260f7c7SSage Weil 		return 0;
6772f2f43d3SEric W. Biederman 	if (uid_eq(dir->i_uid, fsuid))
6784260f7c7SSage Weil 		return 0;
6794260f7c7SSage Weil 	return !capable(CAP_FOWNER);
6804260f7c7SSage Weil }
6814260f7c7SSage Weil 
6824260f7c7SSage Weil /*  copy of may_delete in fs/namei.c()
6834260f7c7SSage Weil  *	Check whether we can remove a link victim from directory dir, check
6844260f7c7SSage Weil  *  whether the type of victim is right.
6854260f7c7SSage Weil  *  1. We can't do it if dir is read-only (done in permission())
6864260f7c7SSage Weil  *  2. We should have write and exec permissions on dir
6874260f7c7SSage Weil  *  3. We can't remove anything from append-only dir
6884260f7c7SSage Weil  *  4. We can't do anything with immutable dir (done in permission())
6894260f7c7SSage Weil  *  5. If the sticky bit on dir is set we should either
6904260f7c7SSage Weil  *	a. be owner of dir, or
6914260f7c7SSage Weil  *	b. be owner of victim, or
6924260f7c7SSage Weil  *	c. have CAP_FOWNER capability
6934260f7c7SSage Weil  *  6. If the victim is append-only or immutable we can't do antyhing with
6944260f7c7SSage Weil  *     links pointing to it.
6954260f7c7SSage Weil  *  7. If we were asked to remove a directory and victim isn't one - ENOTDIR.
6964260f7c7SSage Weil  *  8. If we were asked to remove a non-directory and victim isn't one - EISDIR.
6974260f7c7SSage Weil  *  9. We can't remove a root or mountpoint.
6984260f7c7SSage Weil  * 10. We don't allow removal of NFS sillyrenamed files; it's handled by
6994260f7c7SSage Weil  *     nfs_async_unlink().
7004260f7c7SSage Weil  */
7014260f7c7SSage Weil 
7024260f7c7SSage Weil static int btrfs_may_delete(struct inode *dir, struct dentry *victim, int isdir)
7034260f7c7SSage Weil {
7044260f7c7SSage Weil 	int error;
7054260f7c7SSage Weil 
7064260f7c7SSage Weil 	if (!victim->d_inode)
7074260f7c7SSage Weil 		return -ENOENT;
7084260f7c7SSage Weil 
7094260f7c7SSage Weil 	BUG_ON(victim->d_parent->d_inode != dir);
7104fa6b5ecSJeff Layton 	audit_inode_child(dir, victim, AUDIT_TYPE_CHILD_DELETE);
7114260f7c7SSage Weil 
7124260f7c7SSage Weil 	error = inode_permission(dir, MAY_WRITE | MAY_EXEC);
7134260f7c7SSage Weil 	if (error)
7144260f7c7SSage Weil 		return error;
7154260f7c7SSage Weil 	if (IS_APPEND(dir))
7164260f7c7SSage Weil 		return -EPERM;
7174260f7c7SSage Weil 	if (btrfs_check_sticky(dir, victim->d_inode)||
7184260f7c7SSage Weil 		IS_APPEND(victim->d_inode)||
7194260f7c7SSage Weil 	    IS_IMMUTABLE(victim->d_inode) || IS_SWAPFILE(victim->d_inode))
7204260f7c7SSage Weil 		return -EPERM;
7214260f7c7SSage Weil 	if (isdir) {
7224260f7c7SSage Weil 		if (!S_ISDIR(victim->d_inode->i_mode))
7234260f7c7SSage Weil 			return -ENOTDIR;
7244260f7c7SSage Weil 		if (IS_ROOT(victim))
7254260f7c7SSage Weil 			return -EBUSY;
7264260f7c7SSage Weil 	} else if (S_ISDIR(victim->d_inode->i_mode))
7274260f7c7SSage Weil 		return -EISDIR;
7284260f7c7SSage Weil 	if (IS_DEADDIR(dir))
7294260f7c7SSage Weil 		return -ENOENT;
7304260f7c7SSage Weil 	if (victim->d_flags & DCACHE_NFSFS_RENAMED)
7314260f7c7SSage Weil 		return -EBUSY;
7324260f7c7SSage Weil 	return 0;
7334260f7c7SSage Weil }
7344260f7c7SSage Weil 
735cb8e7090SChristoph Hellwig /* copy of may_create in fs/namei.c() */
736cb8e7090SChristoph Hellwig static inline int btrfs_may_create(struct inode *dir, struct dentry *child)
737cb8e7090SChristoph Hellwig {
738cb8e7090SChristoph Hellwig 	if (child->d_inode)
739cb8e7090SChristoph Hellwig 		return -EEXIST;
740cb8e7090SChristoph Hellwig 	if (IS_DEADDIR(dir))
741cb8e7090SChristoph Hellwig 		return -ENOENT;
742cb8e7090SChristoph Hellwig 	return inode_permission(dir, MAY_WRITE | MAY_EXEC);
743cb8e7090SChristoph Hellwig }
744cb8e7090SChristoph Hellwig 
745cb8e7090SChristoph Hellwig /*
746cb8e7090SChristoph Hellwig  * Create a new subvolume below @parent.  This is largely modeled after
747cb8e7090SChristoph Hellwig  * sys_mkdirat and vfs_mkdir, but we only do a single component lookup
748cb8e7090SChristoph Hellwig  * inside this filesystem so it's quite a bit simpler.
749cb8e7090SChristoph Hellwig  */
75076dda93cSYan, Zheng static noinline int btrfs_mksubvol(struct path *parent,
75176dda93cSYan, Zheng 				   char *name, int namelen,
75272fd032eSSage Weil 				   struct btrfs_root *snap_src,
7536f72c7e2SArne Jansen 				   u64 *async_transid, bool readonly,
7548696c533SMiao Xie 				   struct btrfs_qgroup_inherit *inherit)
755cb8e7090SChristoph Hellwig {
75676dda93cSYan, Zheng 	struct inode *dir  = parent->dentry->d_inode;
757cb8e7090SChristoph Hellwig 	struct dentry *dentry;
758cb8e7090SChristoph Hellwig 	int error;
759cb8e7090SChristoph Hellwig 
7605c50c9b8SDavid Sterba 	error = mutex_lock_killable_nested(&dir->i_mutex, I_MUTEX_PARENT);
7615c50c9b8SDavid Sterba 	if (error == -EINTR)
7625c50c9b8SDavid Sterba 		return error;
763cb8e7090SChristoph Hellwig 
764cb8e7090SChristoph Hellwig 	dentry = lookup_one_len(name, parent->dentry, namelen);
765cb8e7090SChristoph Hellwig 	error = PTR_ERR(dentry);
766cb8e7090SChristoph Hellwig 	if (IS_ERR(dentry))
767cb8e7090SChristoph Hellwig 		goto out_unlock;
768cb8e7090SChristoph Hellwig 
769cb8e7090SChristoph Hellwig 	error = -EEXIST;
770cb8e7090SChristoph Hellwig 	if (dentry->d_inode)
771cb8e7090SChristoph Hellwig 		goto out_dput;
772cb8e7090SChristoph Hellwig 
77376dda93cSYan, Zheng 	error = btrfs_may_create(dir, dentry);
774cb8e7090SChristoph Hellwig 	if (error)
775a874a63eSLiu Bo 		goto out_dput;
776cb8e7090SChristoph Hellwig 
7779c52057cSChris Mason 	/*
7789c52057cSChris Mason 	 * even if this name doesn't exist, we may get hash collisions.
7799c52057cSChris Mason 	 * check for them now when we can safely fail
7809c52057cSChris Mason 	 */
7819c52057cSChris Mason 	error = btrfs_check_dir_item_collision(BTRFS_I(dir)->root,
7829c52057cSChris Mason 					       dir->i_ino, name,
7839c52057cSChris Mason 					       namelen);
7849c52057cSChris Mason 	if (error)
7859c52057cSChris Mason 		goto out_dput;
7869c52057cSChris Mason 
78776dda93cSYan, Zheng 	down_read(&BTRFS_I(dir)->root->fs_info->subvol_sem);
78876dda93cSYan, Zheng 
78976dda93cSYan, Zheng 	if (btrfs_root_refs(&BTRFS_I(dir)->root->root_item) == 0)
79076dda93cSYan, Zheng 		goto out_up_read;
79176dda93cSYan, Zheng 
7923de4586cSChris Mason 	if (snap_src) {
793e9662f70SMiao Xie 		error = create_snapshot(snap_src, dir, dentry, name, namelen,
7946f72c7e2SArne Jansen 					async_transid, readonly, inherit);
7953de4586cSChris Mason 	} else {
796d5c12070SMiao Xie 		error = create_subvol(dir, dentry, name, namelen,
797d5c12070SMiao Xie 				      async_transid, inherit);
7983de4586cSChris Mason 	}
79976dda93cSYan, Zheng 	if (!error)
80076dda93cSYan, Zheng 		fsnotify_mkdir(dir, dentry);
80176dda93cSYan, Zheng out_up_read:
80276dda93cSYan, Zheng 	up_read(&BTRFS_I(dir)->root->fs_info->subvol_sem);
803cb8e7090SChristoph Hellwig out_dput:
804cb8e7090SChristoph Hellwig 	dput(dentry);
805cb8e7090SChristoph Hellwig out_unlock:
80676dda93cSYan, Zheng 	mutex_unlock(&dir->i_mutex);
807cb8e7090SChristoph Hellwig 	return error;
808cb8e7090SChristoph Hellwig }
809cb8e7090SChristoph Hellwig 
8104cb5300bSChris Mason /*
8114cb5300bSChris Mason  * When we're defragging a range, we don't want to kick it off again
8124cb5300bSChris Mason  * if it is really just waiting for delalloc to send it down.
8134cb5300bSChris Mason  * If we find a nice big extent or delalloc range for the bytes in the
8144cb5300bSChris Mason  * file you want to defrag, we return 0 to let you know to skip this
8154cb5300bSChris Mason  * part of the file
8164cb5300bSChris Mason  */
8174cb5300bSChris Mason static int check_defrag_in_cache(struct inode *inode, u64 offset, int thresh)
8184cb5300bSChris Mason {
8194cb5300bSChris Mason 	struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
8204cb5300bSChris Mason 	struct extent_map *em = NULL;
8214cb5300bSChris Mason 	struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
8224cb5300bSChris Mason 	u64 end;
8234cb5300bSChris Mason 
8244cb5300bSChris Mason 	read_lock(&em_tree->lock);
8254cb5300bSChris Mason 	em = lookup_extent_mapping(em_tree, offset, PAGE_CACHE_SIZE);
8264cb5300bSChris Mason 	read_unlock(&em_tree->lock);
8274cb5300bSChris Mason 
8284cb5300bSChris Mason 	if (em) {
8294cb5300bSChris Mason 		end = extent_map_end(em);
8304cb5300bSChris Mason 		free_extent_map(em);
8314cb5300bSChris Mason 		if (end - offset > thresh)
8324cb5300bSChris Mason 			return 0;
8334cb5300bSChris Mason 	}
8344cb5300bSChris Mason 	/* if we already have a nice delalloc here, just stop */
8354cb5300bSChris Mason 	thresh /= 2;
8364cb5300bSChris Mason 	end = count_range_bits(io_tree, &offset, offset + thresh,
8374cb5300bSChris Mason 			       thresh, EXTENT_DELALLOC, 1);
8384cb5300bSChris Mason 	if (end >= thresh)
8394cb5300bSChris Mason 		return 0;
8404cb5300bSChris Mason 	return 1;
8414cb5300bSChris Mason }
8424cb5300bSChris Mason 
8434cb5300bSChris Mason /*
8444cb5300bSChris Mason  * helper function to walk through a file and find extents
8454cb5300bSChris Mason  * newer than a specific transid, and smaller than thresh.
8464cb5300bSChris Mason  *
8474cb5300bSChris Mason  * This is used by the defragging code to find new and small
8484cb5300bSChris Mason  * extents
8494cb5300bSChris Mason  */
8504cb5300bSChris Mason static int find_new_extents(struct btrfs_root *root,
8514cb5300bSChris Mason 			    struct inode *inode, u64 newer_than,
8524cb5300bSChris Mason 			    u64 *off, int thresh)
8534cb5300bSChris Mason {
8544cb5300bSChris Mason 	struct btrfs_path *path;
8554cb5300bSChris Mason 	struct btrfs_key min_key;
8564cb5300bSChris Mason 	struct extent_buffer *leaf;
8574cb5300bSChris Mason 	struct btrfs_file_extent_item *extent;
8584cb5300bSChris Mason 	int type;
8594cb5300bSChris Mason 	int ret;
860a4689d2bSDavid Sterba 	u64 ino = btrfs_ino(inode);
8614cb5300bSChris Mason 
8624cb5300bSChris Mason 	path = btrfs_alloc_path();
8634cb5300bSChris Mason 	if (!path)
8644cb5300bSChris Mason 		return -ENOMEM;
8654cb5300bSChris Mason 
866a4689d2bSDavid Sterba 	min_key.objectid = ino;
8674cb5300bSChris Mason 	min_key.type = BTRFS_EXTENT_DATA_KEY;
8684cb5300bSChris Mason 	min_key.offset = *off;
8694cb5300bSChris Mason 
8704cb5300bSChris Mason 	path->keep_locks = 1;
8714cb5300bSChris Mason 
8724cb5300bSChris Mason 	while (1) {
8736174d3cbSFilipe David Borba Manana 		ret = btrfs_search_forward(root, &min_key, path, newer_than);
8744cb5300bSChris Mason 		if (ret != 0)
8754cb5300bSChris Mason 			goto none;
876a4689d2bSDavid Sterba 		if (min_key.objectid != ino)
8774cb5300bSChris Mason 			goto none;
8784cb5300bSChris Mason 		if (min_key.type != BTRFS_EXTENT_DATA_KEY)
8794cb5300bSChris Mason 			goto none;
8804cb5300bSChris Mason 
8814cb5300bSChris Mason 		leaf = path->nodes[0];
8824cb5300bSChris Mason 		extent = btrfs_item_ptr(leaf, path->slots[0],
8834cb5300bSChris Mason 					struct btrfs_file_extent_item);
8844cb5300bSChris Mason 
8854cb5300bSChris Mason 		type = btrfs_file_extent_type(leaf, extent);
8864cb5300bSChris Mason 		if (type == BTRFS_FILE_EXTENT_REG &&
8874cb5300bSChris Mason 		    btrfs_file_extent_num_bytes(leaf, extent) < thresh &&
8884cb5300bSChris Mason 		    check_defrag_in_cache(inode, min_key.offset, thresh)) {
8894cb5300bSChris Mason 			*off = min_key.offset;
8904cb5300bSChris Mason 			btrfs_free_path(path);
8914cb5300bSChris Mason 			return 0;
8924cb5300bSChris Mason 		}
8934cb5300bSChris Mason 
8944cb5300bSChris Mason 		if (min_key.offset == (u64)-1)
8954cb5300bSChris Mason 			goto none;
8964cb5300bSChris Mason 
8974cb5300bSChris Mason 		min_key.offset++;
8984cb5300bSChris Mason 		btrfs_release_path(path);
8994cb5300bSChris Mason 	}
9004cb5300bSChris Mason none:
9014cb5300bSChris Mason 	btrfs_free_path(path);
9024cb5300bSChris Mason 	return -ENOENT;
9034cb5300bSChris Mason }
9044cb5300bSChris Mason 
9056c282eb4SLi Zefan static struct extent_map *defrag_lookup_extent(struct inode *inode, u64 start)
90617ce6ef8SLiu Bo {
90717ce6ef8SLiu Bo 	struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
908940100a4SChris Mason 	struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
9096c282eb4SLi Zefan 	struct extent_map *em;
9106c282eb4SLi Zefan 	u64 len = PAGE_CACHE_SIZE;
911940100a4SChris Mason 
912940100a4SChris Mason 	/*
913940100a4SChris Mason 	 * hopefully we have this extent in the tree already, try without
914940100a4SChris Mason 	 * the full extent lock
915940100a4SChris Mason 	 */
916940100a4SChris Mason 	read_lock(&em_tree->lock);
917940100a4SChris Mason 	em = lookup_extent_mapping(em_tree, start, len);
918940100a4SChris Mason 	read_unlock(&em_tree->lock);
919940100a4SChris Mason 
920940100a4SChris Mason 	if (!em) {
921940100a4SChris Mason 		/* get the big lock and read metadata off disk */
922d0082371SJeff Mahoney 		lock_extent(io_tree, start, start + len - 1);
923940100a4SChris Mason 		em = btrfs_get_extent(inode, NULL, 0, start, len, 0);
924d0082371SJeff Mahoney 		unlock_extent(io_tree, start, start + len - 1);
925940100a4SChris Mason 
9266cf8bfbfSDan Carpenter 		if (IS_ERR(em))
9276c282eb4SLi Zefan 			return NULL;
928940100a4SChris Mason 	}
929940100a4SChris Mason 
9306c282eb4SLi Zefan 	return em;
9316c282eb4SLi Zefan }
9326c282eb4SLi Zefan 
9336c282eb4SLi Zefan static bool defrag_check_next_extent(struct inode *inode, struct extent_map *em)
9346c282eb4SLi Zefan {
9356c282eb4SLi Zefan 	struct extent_map *next;
9366c282eb4SLi Zefan 	bool ret = true;
9376c282eb4SLi Zefan 
9386c282eb4SLi Zefan 	/* this is the last extent */
9396c282eb4SLi Zefan 	if (em->start + em->len >= i_size_read(inode))
9406c282eb4SLi Zefan 		return false;
9416c282eb4SLi Zefan 
9426c282eb4SLi Zefan 	next = defrag_lookup_extent(inode, em->start + em->len);
9436c282eb4SLi Zefan 	if (!next || next->block_start >= EXTENT_MAP_LAST_BYTE)
9446c282eb4SLi Zefan 		ret = false;
9456c282eb4SLi Zefan 
9466c282eb4SLi Zefan 	free_extent_map(next);
9476c282eb4SLi Zefan 	return ret;
9486c282eb4SLi Zefan }
9496c282eb4SLi Zefan 
9506c282eb4SLi Zefan static int should_defrag_range(struct inode *inode, u64 start, int thresh,
951a43a2111SAndrew Mahone 			       u64 *last_len, u64 *skip, u64 *defrag_end,
952a43a2111SAndrew Mahone 			       int compress)
9536c282eb4SLi Zefan {
9546c282eb4SLi Zefan 	struct extent_map *em;
9556c282eb4SLi Zefan 	int ret = 1;
9566c282eb4SLi Zefan 	bool next_mergeable = true;
9576c282eb4SLi Zefan 
9586c282eb4SLi Zefan 	/*
9596c282eb4SLi Zefan 	 * make sure that once we start defragging an extent, we keep on
9606c282eb4SLi Zefan 	 * defragging it
9616c282eb4SLi Zefan 	 */
9626c282eb4SLi Zefan 	if (start < *defrag_end)
9636c282eb4SLi Zefan 		return 1;
9646c282eb4SLi Zefan 
9656c282eb4SLi Zefan 	*skip = 0;
9666c282eb4SLi Zefan 
9676c282eb4SLi Zefan 	em = defrag_lookup_extent(inode, start);
9686c282eb4SLi Zefan 	if (!em)
9696c282eb4SLi Zefan 		return 0;
9706c282eb4SLi Zefan 
971940100a4SChris Mason 	/* this will cover holes, and inline extents */
97217ce6ef8SLiu Bo 	if (em->block_start >= EXTENT_MAP_LAST_BYTE) {
973940100a4SChris Mason 		ret = 0;
97417ce6ef8SLiu Bo 		goto out;
97517ce6ef8SLiu Bo 	}
97617ce6ef8SLiu Bo 
9776c282eb4SLi Zefan 	next_mergeable = defrag_check_next_extent(inode, em);
978940100a4SChris Mason 
979940100a4SChris Mason 	/*
9806c282eb4SLi Zefan 	 * we hit a real extent, if it is big or the next extent is not a
9816c282eb4SLi Zefan 	 * real extent, don't bother defragging it
982940100a4SChris Mason 	 */
983a43a2111SAndrew Mahone 	if (!compress && (*last_len == 0 || *last_len >= thresh) &&
9846c282eb4SLi Zefan 	    (em->len >= thresh || !next_mergeable))
985940100a4SChris Mason 		ret = 0;
98617ce6ef8SLiu Bo out:
987940100a4SChris Mason 	/*
988940100a4SChris Mason 	 * last_len ends up being a counter of how many bytes we've defragged.
989940100a4SChris Mason 	 * every time we choose not to defrag an extent, we reset *last_len
990940100a4SChris Mason 	 * so that the next tiny extent will force a defrag.
991940100a4SChris Mason 	 *
992940100a4SChris Mason 	 * The end result of this is that tiny extents before a single big
993940100a4SChris Mason 	 * extent will force at least part of that big extent to be defragged.
994940100a4SChris Mason 	 */
995940100a4SChris Mason 	if (ret) {
996940100a4SChris Mason 		*defrag_end = extent_map_end(em);
997940100a4SChris Mason 	} else {
998940100a4SChris Mason 		*last_len = 0;
999940100a4SChris Mason 		*skip = extent_map_end(em);
1000940100a4SChris Mason 		*defrag_end = 0;
1001940100a4SChris Mason 	}
1002940100a4SChris Mason 
1003940100a4SChris Mason 	free_extent_map(em);
1004940100a4SChris Mason 	return ret;
1005940100a4SChris Mason }
1006940100a4SChris Mason 
10074cb5300bSChris Mason /*
10084cb5300bSChris Mason  * it doesn't do much good to defrag one or two pages
10094cb5300bSChris Mason  * at a time.  This pulls in a nice chunk of pages
10104cb5300bSChris Mason  * to COW and defrag.
10114cb5300bSChris Mason  *
10124cb5300bSChris Mason  * It also makes sure the delalloc code has enough
10134cb5300bSChris Mason  * dirty data to avoid making new small extents as part
10144cb5300bSChris Mason  * of the defrag
10154cb5300bSChris Mason  *
10164cb5300bSChris Mason  * It's a good idea to start RA on this range
10174cb5300bSChris Mason  * before calling this.
10184cb5300bSChris Mason  */
10194cb5300bSChris Mason static int cluster_pages_for_defrag(struct inode *inode,
10204cb5300bSChris Mason 				    struct page **pages,
10214cb5300bSChris Mason 				    unsigned long start_index,
10224cb5300bSChris Mason 				    int num_pages)
1023f46b5a66SChristoph Hellwig {
10244cb5300bSChris Mason 	unsigned long file_end;
10254cb5300bSChris Mason 	u64 isize = i_size_read(inode);
1026f46b5a66SChristoph Hellwig 	u64 page_start;
1027f46b5a66SChristoph Hellwig 	u64 page_end;
10281f12bd06SLiu Bo 	u64 page_cnt;
10294cb5300bSChris Mason 	int ret;
10304cb5300bSChris Mason 	int i;
10314cb5300bSChris Mason 	int i_done;
10324cb5300bSChris Mason 	struct btrfs_ordered_extent *ordered;
10334cb5300bSChris Mason 	struct extent_state *cached_state = NULL;
1034600a45e1SMiao Xie 	struct extent_io_tree *tree;
10353b16a4e3SJosef Bacik 	gfp_t mask = btrfs_alloc_write_mask(inode->i_mapping);
10364cb5300bSChris Mason 
10374cb5300bSChris Mason 	file_end = (isize - 1) >> PAGE_CACHE_SHIFT;
10381f12bd06SLiu Bo 	if (!isize || start_index > file_end)
10391f12bd06SLiu Bo 		return 0;
10401f12bd06SLiu Bo 
10411f12bd06SLiu Bo 	page_cnt = min_t(u64, (u64)num_pages, (u64)file_end - start_index + 1);
10424cb5300bSChris Mason 
10434cb5300bSChris Mason 	ret = btrfs_delalloc_reserve_space(inode,
10441f12bd06SLiu Bo 					   page_cnt << PAGE_CACHE_SHIFT);
10454cb5300bSChris Mason 	if (ret)
10464cb5300bSChris Mason 		return ret;
10474cb5300bSChris Mason 	i_done = 0;
1048600a45e1SMiao Xie 	tree = &BTRFS_I(inode)->io_tree;
10494cb5300bSChris Mason 
10504cb5300bSChris Mason 	/* step one, lock all the pages */
10511f12bd06SLiu Bo 	for (i = 0; i < page_cnt; i++) {
10524cb5300bSChris Mason 		struct page *page;
1053600a45e1SMiao Xie again:
1054a94733d0SJosef Bacik 		page = find_or_create_page(inode->i_mapping,
10553b16a4e3SJosef Bacik 					   start_index + i, mask);
10564cb5300bSChris Mason 		if (!page)
10574cb5300bSChris Mason 			break;
10584cb5300bSChris Mason 
1059600a45e1SMiao Xie 		page_start = page_offset(page);
1060600a45e1SMiao Xie 		page_end = page_start + PAGE_CACHE_SIZE - 1;
1061600a45e1SMiao Xie 		while (1) {
1062d0082371SJeff Mahoney 			lock_extent(tree, page_start, page_end);
1063600a45e1SMiao Xie 			ordered = btrfs_lookup_ordered_extent(inode,
1064600a45e1SMiao Xie 							      page_start);
1065d0082371SJeff Mahoney 			unlock_extent(tree, page_start, page_end);
1066600a45e1SMiao Xie 			if (!ordered)
1067600a45e1SMiao Xie 				break;
1068600a45e1SMiao Xie 
1069600a45e1SMiao Xie 			unlock_page(page);
1070600a45e1SMiao Xie 			btrfs_start_ordered_extent(inode, ordered, 1);
1071600a45e1SMiao Xie 			btrfs_put_ordered_extent(ordered);
1072600a45e1SMiao Xie 			lock_page(page);
10731f12bd06SLiu Bo 			/*
10741f12bd06SLiu Bo 			 * we unlocked the page above, so we need check if
10751f12bd06SLiu Bo 			 * it was released or not.
10761f12bd06SLiu Bo 			 */
10771f12bd06SLiu Bo 			if (page->mapping != inode->i_mapping) {
10781f12bd06SLiu Bo 				unlock_page(page);
10791f12bd06SLiu Bo 				page_cache_release(page);
10801f12bd06SLiu Bo 				goto again;
10811f12bd06SLiu Bo 			}
1082600a45e1SMiao Xie 		}
1083600a45e1SMiao Xie 
10844cb5300bSChris Mason 		if (!PageUptodate(page)) {
10854cb5300bSChris Mason 			btrfs_readpage(NULL, page);
10864cb5300bSChris Mason 			lock_page(page);
10874cb5300bSChris Mason 			if (!PageUptodate(page)) {
10884cb5300bSChris Mason 				unlock_page(page);
10894cb5300bSChris Mason 				page_cache_release(page);
10904cb5300bSChris Mason 				ret = -EIO;
10914cb5300bSChris Mason 				break;
10924cb5300bSChris Mason 			}
10934cb5300bSChris Mason 		}
1094600a45e1SMiao Xie 
1095600a45e1SMiao Xie 		if (page->mapping != inode->i_mapping) {
1096600a45e1SMiao Xie 			unlock_page(page);
1097600a45e1SMiao Xie 			page_cache_release(page);
1098600a45e1SMiao Xie 			goto again;
1099600a45e1SMiao Xie 		}
1100600a45e1SMiao Xie 
11014cb5300bSChris Mason 		pages[i] = page;
11024cb5300bSChris Mason 		i_done++;
11034cb5300bSChris Mason 	}
11044cb5300bSChris Mason 	if (!i_done || ret)
11054cb5300bSChris Mason 		goto out;
11064cb5300bSChris Mason 
11074cb5300bSChris Mason 	if (!(inode->i_sb->s_flags & MS_ACTIVE))
11084cb5300bSChris Mason 		goto out;
11094cb5300bSChris Mason 
11104cb5300bSChris Mason 	/*
11114cb5300bSChris Mason 	 * so now we have a nice long stream of locked
11124cb5300bSChris Mason 	 * and up to date pages, lets wait on them
11134cb5300bSChris Mason 	 */
11144cb5300bSChris Mason 	for (i = 0; i < i_done; i++)
11154cb5300bSChris Mason 		wait_on_page_writeback(pages[i]);
11164cb5300bSChris Mason 
11174cb5300bSChris Mason 	page_start = page_offset(pages[0]);
11184cb5300bSChris Mason 	page_end = page_offset(pages[i_done - 1]) + PAGE_CACHE_SIZE;
11194cb5300bSChris Mason 
11204cb5300bSChris Mason 	lock_extent_bits(&BTRFS_I(inode)->io_tree,
1121d0082371SJeff Mahoney 			 page_start, page_end - 1, 0, &cached_state);
11224cb5300bSChris Mason 	clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start,
11234cb5300bSChris Mason 			  page_end - 1, EXTENT_DIRTY | EXTENT_DELALLOC |
11249e8a4a8bSLiu Bo 			  EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG, 0, 0,
11259e8a4a8bSLiu Bo 			  &cached_state, GFP_NOFS);
11264cb5300bSChris Mason 
11271f12bd06SLiu Bo 	if (i_done != page_cnt) {
11289e0baf60SJosef Bacik 		spin_lock(&BTRFS_I(inode)->lock);
11299e0baf60SJosef Bacik 		BTRFS_I(inode)->outstanding_extents++;
11309e0baf60SJosef Bacik 		spin_unlock(&BTRFS_I(inode)->lock);
11314cb5300bSChris Mason 		btrfs_delalloc_release_space(inode,
11321f12bd06SLiu Bo 				     (page_cnt - i_done) << PAGE_CACHE_SHIFT);
11334cb5300bSChris Mason 	}
11344cb5300bSChris Mason 
11354cb5300bSChris Mason 
11369e8a4a8bSLiu Bo 	set_extent_defrag(&BTRFS_I(inode)->io_tree, page_start, page_end - 1,
11379e8a4a8bSLiu Bo 			  &cached_state, GFP_NOFS);
11384cb5300bSChris Mason 
11394cb5300bSChris Mason 	unlock_extent_cached(&BTRFS_I(inode)->io_tree,
11404cb5300bSChris Mason 			     page_start, page_end - 1, &cached_state,
11414cb5300bSChris Mason 			     GFP_NOFS);
11424cb5300bSChris Mason 
11434cb5300bSChris Mason 	for (i = 0; i < i_done; i++) {
11444cb5300bSChris Mason 		clear_page_dirty_for_io(pages[i]);
11454cb5300bSChris Mason 		ClearPageChecked(pages[i]);
11464cb5300bSChris Mason 		set_page_extent_mapped(pages[i]);
11474cb5300bSChris Mason 		set_page_dirty(pages[i]);
11484cb5300bSChris Mason 		unlock_page(pages[i]);
11494cb5300bSChris Mason 		page_cache_release(pages[i]);
11504cb5300bSChris Mason 	}
11514cb5300bSChris Mason 	return i_done;
11524cb5300bSChris Mason out:
11534cb5300bSChris Mason 	for (i = 0; i < i_done; i++) {
11544cb5300bSChris Mason 		unlock_page(pages[i]);
11554cb5300bSChris Mason 		page_cache_release(pages[i]);
11564cb5300bSChris Mason 	}
11571f12bd06SLiu Bo 	btrfs_delalloc_release_space(inode, page_cnt << PAGE_CACHE_SHIFT);
11584cb5300bSChris Mason 	return ret;
11594cb5300bSChris Mason 
11604cb5300bSChris Mason }
11614cb5300bSChris Mason 
11624cb5300bSChris Mason int btrfs_defrag_file(struct inode *inode, struct file *file,
11634cb5300bSChris Mason 		      struct btrfs_ioctl_defrag_range_args *range,
11644cb5300bSChris Mason 		      u64 newer_than, unsigned long max_to_defrag)
11654cb5300bSChris Mason {
11664cb5300bSChris Mason 	struct btrfs_root *root = BTRFS_I(inode)->root;
11674cb5300bSChris Mason 	struct file_ra_state *ra = NULL;
11684cb5300bSChris Mason 	unsigned long last_index;
1169151a31b2SLi Zefan 	u64 isize = i_size_read(inode);
1170940100a4SChris Mason 	u64 last_len = 0;
1171940100a4SChris Mason 	u64 skip = 0;
1172940100a4SChris Mason 	u64 defrag_end = 0;
11734cb5300bSChris Mason 	u64 newer_off = range->start;
1174f46b5a66SChristoph Hellwig 	unsigned long i;
1175008873eaSLi Zefan 	unsigned long ra_index = 0;
1176f46b5a66SChristoph Hellwig 	int ret;
11774cb5300bSChris Mason 	int defrag_count = 0;
11781a419d85SLi Zefan 	int compress_type = BTRFS_COMPRESS_ZLIB;
11794cb5300bSChris Mason 	int extent_thresh = range->extent_thresh;
1180008873eaSLi Zefan 	int max_cluster = (256 * 1024) >> PAGE_CACHE_SHIFT;
1181008873eaSLi Zefan 	int cluster = max_cluster;
11824cb5300bSChris Mason 	u64 new_align = ~((u64)128 * 1024 - 1);
11834cb5300bSChris Mason 	struct page **pages = NULL;
11844cb5300bSChris Mason 
11850abd5b17SLiu Bo 	if (isize == 0)
11860abd5b17SLiu Bo 		return 0;
11870abd5b17SLiu Bo 
11880abd5b17SLiu Bo 	if (range->start >= isize)
11890abd5b17SLiu Bo 		return -EINVAL;
11901a419d85SLi Zefan 
11911a419d85SLi Zefan 	if (range->flags & BTRFS_DEFRAG_RANGE_COMPRESS) {
11921a419d85SLi Zefan 		if (range->compress_type > BTRFS_COMPRESS_TYPES)
11931a419d85SLi Zefan 			return -EINVAL;
11941a419d85SLi Zefan 		if (range->compress_type)
11951a419d85SLi Zefan 			compress_type = range->compress_type;
11961a419d85SLi Zefan 	}
1197f46b5a66SChristoph Hellwig 
11980abd5b17SLiu Bo 	if (extent_thresh == 0)
11990abd5b17SLiu Bo 		extent_thresh = 256 * 1024;
1200f46b5a66SChristoph Hellwig 
12014cb5300bSChris Mason 	/*
12024cb5300bSChris Mason 	 * if we were not given a file, allocate a readahead
12034cb5300bSChris Mason 	 * context
12044cb5300bSChris Mason 	 */
12054cb5300bSChris Mason 	if (!file) {
12064cb5300bSChris Mason 		ra = kzalloc(sizeof(*ra), GFP_NOFS);
12074cb5300bSChris Mason 		if (!ra)
12084cb5300bSChris Mason 			return -ENOMEM;
12094cb5300bSChris Mason 		file_ra_state_init(ra, inode->i_mapping);
12104cb5300bSChris Mason 	} else {
12114cb5300bSChris Mason 		ra = &file->f_ra;
12124cb5300bSChris Mason 	}
12134cb5300bSChris Mason 
1214d9b0d9baSDulshani Gunawardhana 	pages = kmalloc_array(max_cluster, sizeof(struct page *),
12154cb5300bSChris Mason 			GFP_NOFS);
12164cb5300bSChris Mason 	if (!pages) {
12174cb5300bSChris Mason 		ret = -ENOMEM;
12184cb5300bSChris Mason 		goto out_ra;
12194cb5300bSChris Mason 	}
12204cb5300bSChris Mason 
12214cb5300bSChris Mason 	/* find the last page to defrag */
12221e701a32SChris Mason 	if (range->start + range->len > range->start) {
1223151a31b2SLi Zefan 		last_index = min_t(u64, isize - 1,
12241e701a32SChris Mason 			 range->start + range->len - 1) >> PAGE_CACHE_SHIFT;
12251e701a32SChris Mason 	} else {
1226151a31b2SLi Zefan 		last_index = (isize - 1) >> PAGE_CACHE_SHIFT;
12271e701a32SChris Mason 	}
12281e701a32SChris Mason 
12294cb5300bSChris Mason 	if (newer_than) {
12304cb5300bSChris Mason 		ret = find_new_extents(root, inode, newer_than,
12314cb5300bSChris Mason 				       &newer_off, 64 * 1024);
12324cb5300bSChris Mason 		if (!ret) {
12334cb5300bSChris Mason 			range->start = newer_off;
12344cb5300bSChris Mason 			/*
12354cb5300bSChris Mason 			 * we always align our defrag to help keep
12364cb5300bSChris Mason 			 * the extents in the file evenly spaced
12374cb5300bSChris Mason 			 */
12384cb5300bSChris Mason 			i = (newer_off & new_align) >> PAGE_CACHE_SHIFT;
12394cb5300bSChris Mason 		} else
12404cb5300bSChris Mason 			goto out_ra;
12414cb5300bSChris Mason 	} else {
12421e701a32SChris Mason 		i = range->start >> PAGE_CACHE_SHIFT;
12434cb5300bSChris Mason 	}
12444cb5300bSChris Mason 	if (!max_to_defrag)
12457ec31b54SLiu Bo 		max_to_defrag = last_index + 1;
12464cb5300bSChris Mason 
12472a0f7f57SLi Zefan 	/*
12482a0f7f57SLi Zefan 	 * make writeback starts from i, so the defrag range can be
12492a0f7f57SLi Zefan 	 * written sequentially.
12502a0f7f57SLi Zefan 	 */
12512a0f7f57SLi Zefan 	if (i < inode->i_mapping->writeback_index)
12522a0f7f57SLi Zefan 		inode->i_mapping->writeback_index = i;
12532a0f7f57SLi Zefan 
1254f7f43cc8SChris Mason 	while (i <= last_index && defrag_count < max_to_defrag &&
1255f7f43cc8SChris Mason 	       (i < (i_size_read(inode) + PAGE_CACHE_SIZE - 1) >>
1256f7f43cc8SChris Mason 		PAGE_CACHE_SHIFT)) {
12574cb5300bSChris Mason 		/*
12584cb5300bSChris Mason 		 * make sure we stop running if someone unmounts
12594cb5300bSChris Mason 		 * the FS
12604cb5300bSChris Mason 		 */
12614cb5300bSChris Mason 		if (!(inode->i_sb->s_flags & MS_ACTIVE))
12624cb5300bSChris Mason 			break;
12634cb5300bSChris Mason 
1264210549ebSDavid Sterba 		if (btrfs_defrag_cancelled(root->fs_info)) {
1265efe120a0SFrank Holton 			printk(KERN_DEBUG "BTRFS: defrag_file cancelled\n");
1266210549ebSDavid Sterba 			ret = -EAGAIN;
1267210549ebSDavid Sterba 			break;
1268210549ebSDavid Sterba 		}
1269210549ebSDavid Sterba 
127066c26892SLiu Bo 		if (!should_defrag_range(inode, (u64)i << PAGE_CACHE_SHIFT,
12716c282eb4SLi Zefan 					 extent_thresh, &last_len, &skip,
1272a43a2111SAndrew Mahone 					 &defrag_end, range->flags &
1273a43a2111SAndrew Mahone 					 BTRFS_DEFRAG_RANGE_COMPRESS)) {
1274940100a4SChris Mason 			unsigned long next;
1275940100a4SChris Mason 			/*
1276940100a4SChris Mason 			 * the should_defrag function tells us how much to skip
1277940100a4SChris Mason 			 * bump our counter by the suggested amount
1278940100a4SChris Mason 			 */
1279940100a4SChris Mason 			next = (skip + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
1280940100a4SChris Mason 			i = max(i + 1, next);
1281940100a4SChris Mason 			continue;
1282940100a4SChris Mason 		}
1283008873eaSLi Zefan 
1284008873eaSLi Zefan 		if (!newer_than) {
1285008873eaSLi Zefan 			cluster = (PAGE_CACHE_ALIGN(defrag_end) >>
1286008873eaSLi Zefan 				   PAGE_CACHE_SHIFT) - i;
1287008873eaSLi Zefan 			cluster = min(cluster, max_cluster);
1288008873eaSLi Zefan 		} else {
1289008873eaSLi Zefan 			cluster = max_cluster;
1290008873eaSLi Zefan 		}
1291008873eaSLi Zefan 
1292008873eaSLi Zefan 		if (i + cluster > ra_index) {
1293008873eaSLi Zefan 			ra_index = max(i, ra_index);
1294008873eaSLi Zefan 			btrfs_force_ra(inode->i_mapping, ra, file, ra_index,
1295008873eaSLi Zefan 				       cluster);
1296008873eaSLi Zefan 			ra_index += max_cluster;
1297008873eaSLi Zefan 		}
12984cb5300bSChris Mason 
1299ecb8bea8SLiu Bo 		mutex_lock(&inode->i_mutex);
1300633085c7SFilipe David Borba Manana 		if (range->flags & BTRFS_DEFRAG_RANGE_COMPRESS)
1301633085c7SFilipe David Borba Manana 			BTRFS_I(inode)->force_compress = compress_type;
1302008873eaSLi Zefan 		ret = cluster_pages_for_defrag(inode, pages, i, cluster);
1303ecb8bea8SLiu Bo 		if (ret < 0) {
1304ecb8bea8SLiu Bo 			mutex_unlock(&inode->i_mutex);
13054cb5300bSChris Mason 			goto out_ra;
1306ecb8bea8SLiu Bo 		}
13074cb5300bSChris Mason 
13084cb5300bSChris Mason 		defrag_count += ret;
1309d0e1d66bSNamjae Jeon 		balance_dirty_pages_ratelimited(inode->i_mapping);
1310ecb8bea8SLiu Bo 		mutex_unlock(&inode->i_mutex);
13114cb5300bSChris Mason 
13124cb5300bSChris Mason 		if (newer_than) {
13134cb5300bSChris Mason 			if (newer_off == (u64)-1)
13144cb5300bSChris Mason 				break;
13154cb5300bSChris Mason 
1316e1f041e1SLiu Bo 			if (ret > 0)
1317e1f041e1SLiu Bo 				i += ret;
1318e1f041e1SLiu Bo 
13194cb5300bSChris Mason 			newer_off = max(newer_off + 1,
13204cb5300bSChris Mason 					(u64)i << PAGE_CACHE_SHIFT);
13214cb5300bSChris Mason 
13224cb5300bSChris Mason 			ret = find_new_extents(root, inode,
13234cb5300bSChris Mason 					       newer_than, &newer_off,
13244cb5300bSChris Mason 					       64 * 1024);
13254cb5300bSChris Mason 			if (!ret) {
13264cb5300bSChris Mason 				range->start = newer_off;
13274cb5300bSChris Mason 				i = (newer_off & new_align) >> PAGE_CACHE_SHIFT;
13284cb5300bSChris Mason 			} else {
13294cb5300bSChris Mason 				break;
1330940100a4SChris Mason 			}
13314cb5300bSChris Mason 		} else {
1332008873eaSLi Zefan 			if (ret > 0) {
1333cbcc8326SLi Zefan 				i += ret;
1334008873eaSLi Zefan 				last_len += ret << PAGE_CACHE_SHIFT;
1335008873eaSLi Zefan 			} else {
1336940100a4SChris Mason 				i++;
1337008873eaSLi Zefan 				last_len = 0;
1338008873eaSLi Zefan 			}
1339f46b5a66SChristoph Hellwig 		}
13404cb5300bSChris Mason 	}
1341f46b5a66SChristoph Hellwig 
13421e701a32SChris Mason 	if ((range->flags & BTRFS_DEFRAG_RANGE_START_IO))
13431e701a32SChris Mason 		filemap_flush(inode->i_mapping);
13441e701a32SChris Mason 
13451e701a32SChris Mason 	if ((range->flags & BTRFS_DEFRAG_RANGE_COMPRESS)) {
13461e701a32SChris Mason 		/* the filemap_flush will queue IO into the worker threads, but
13471e701a32SChris Mason 		 * we have to make sure the IO is actually started and that
13481e701a32SChris Mason 		 * ordered extents get created before we return
13491e701a32SChris Mason 		 */
13501e701a32SChris Mason 		atomic_inc(&root->fs_info->async_submit_draining);
13511e701a32SChris Mason 		while (atomic_read(&root->fs_info->nr_async_submits) ||
13521e701a32SChris Mason 		      atomic_read(&root->fs_info->async_delalloc_pages)) {
13531e701a32SChris Mason 			wait_event(root->fs_info->async_submit_wait,
13541e701a32SChris Mason 			   (atomic_read(&root->fs_info->nr_async_submits) == 0 &&
13551e701a32SChris Mason 			    atomic_read(&root->fs_info->async_delalloc_pages) == 0));
13561e701a32SChris Mason 		}
13571e701a32SChris Mason 		atomic_dec(&root->fs_info->async_submit_draining);
13581e701a32SChris Mason 	}
13591e701a32SChris Mason 
13601a419d85SLi Zefan 	if (range->compress_type == BTRFS_COMPRESS_LZO) {
13612b0ce2c2SMitch Harder 		btrfs_set_fs_incompat(root->fs_info, COMPRESS_LZO);
13621a419d85SLi Zefan 	}
13631a419d85SLi Zefan 
136460ccf82fSDiego Calleja 	ret = defrag_count;
1365940100a4SChris Mason 
13664cb5300bSChris Mason out_ra:
1367633085c7SFilipe David Borba Manana 	if (range->flags & BTRFS_DEFRAG_RANGE_COMPRESS) {
1368633085c7SFilipe David Borba Manana 		mutex_lock(&inode->i_mutex);
1369633085c7SFilipe David Borba Manana 		BTRFS_I(inode)->force_compress = BTRFS_COMPRESS_NONE;
1370633085c7SFilipe David Borba Manana 		mutex_unlock(&inode->i_mutex);
1371633085c7SFilipe David Borba Manana 	}
13724cb5300bSChris Mason 	if (!file)
13734cb5300bSChris Mason 		kfree(ra);
13744cb5300bSChris Mason 	kfree(pages);
1375940100a4SChris Mason 	return ret;
1376f46b5a66SChristoph Hellwig }
1377f46b5a66SChristoph Hellwig 
1378198605a8SMiao Xie static noinline int btrfs_ioctl_resize(struct file *file,
137976dda93cSYan, Zheng 					void __user *arg)
1380f46b5a66SChristoph Hellwig {
1381f46b5a66SChristoph Hellwig 	u64 new_size;
1382f46b5a66SChristoph Hellwig 	u64 old_size;
1383f46b5a66SChristoph Hellwig 	u64 devid = 1;
1384496ad9aaSAl Viro 	struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
1385f46b5a66SChristoph Hellwig 	struct btrfs_ioctl_vol_args *vol_args;
1386f46b5a66SChristoph Hellwig 	struct btrfs_trans_handle *trans;
1387f46b5a66SChristoph Hellwig 	struct btrfs_device *device = NULL;
1388f46b5a66SChristoph Hellwig 	char *sizestr;
1389f46b5a66SChristoph Hellwig 	char *devstr = NULL;
1390f46b5a66SChristoph Hellwig 	int ret = 0;
1391f46b5a66SChristoph Hellwig 	int mod = 0;
1392f46b5a66SChristoph Hellwig 
1393e441d54dSChris Mason 	if (!capable(CAP_SYS_ADMIN))
1394e441d54dSChris Mason 		return -EPERM;
1395e441d54dSChris Mason 
1396198605a8SMiao Xie 	ret = mnt_want_write_file(file);
1397198605a8SMiao Xie 	if (ret)
1398198605a8SMiao Xie 		return ret;
1399198605a8SMiao Xie 
14005ac00addSStefan Behrens 	if (atomic_xchg(&root->fs_info->mutually_exclusive_operation_running,
14015ac00addSStefan Behrens 			1)) {
140297547676SMiao Xie 		mnt_drop_write_file(file);
1403e57138b3SAnand Jain 		return BTRFS_ERROR_DEV_EXCL_RUN_IN_PROGRESS;
1404c9e9f97bSIlya Dryomov 	}
1405c9e9f97bSIlya Dryomov 
14065ac00addSStefan Behrens 	mutex_lock(&root->fs_info->volume_mutex);
1407dae7b665SLi Zefan 	vol_args = memdup_user(arg, sizeof(*vol_args));
1408c9e9f97bSIlya Dryomov 	if (IS_ERR(vol_args)) {
1409c9e9f97bSIlya Dryomov 		ret = PTR_ERR(vol_args);
1410c9e9f97bSIlya Dryomov 		goto out;
1411c9e9f97bSIlya Dryomov 	}
14125516e595SMark Fasheh 
14135516e595SMark Fasheh 	vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
1414f46b5a66SChristoph Hellwig 
1415f46b5a66SChristoph Hellwig 	sizestr = vol_args->name;
1416f46b5a66SChristoph Hellwig 	devstr = strchr(sizestr, ':');
1417f46b5a66SChristoph Hellwig 	if (devstr) {
1418f46b5a66SChristoph Hellwig 		char *end;
1419f46b5a66SChristoph Hellwig 		sizestr = devstr + 1;
1420f46b5a66SChristoph Hellwig 		*devstr = '\0';
1421f46b5a66SChristoph Hellwig 		devstr = vol_args->name;
1422f46b5a66SChristoph Hellwig 		devid = simple_strtoull(devstr, &end, 10);
1423dfd79829SMiao Xie 		if (!devid) {
1424dfd79829SMiao Xie 			ret = -EINVAL;
1425dfd79829SMiao Xie 			goto out_free;
1426dfd79829SMiao Xie 		}
1427efe120a0SFrank Holton 		btrfs_info(root->fs_info, "resizing devid %llu", devid);
1428f46b5a66SChristoph Hellwig 	}
1429dba60f3fSMiao Xie 
1430aa1b8cd4SStefan Behrens 	device = btrfs_find_device(root->fs_info, devid, NULL, NULL);
1431f46b5a66SChristoph Hellwig 	if (!device) {
1432efe120a0SFrank Holton 		btrfs_info(root->fs_info, "resizer unable to find device %llu",
1433c1c9ff7cSGeert Uytterhoeven 		       devid);
1434dfd79829SMiao Xie 		ret = -ENODEV;
1435c9e9f97bSIlya Dryomov 		goto out_free;
1436f46b5a66SChristoph Hellwig 	}
1437dba60f3fSMiao Xie 
1438dba60f3fSMiao Xie 	if (!device->writeable) {
1439efe120a0SFrank Holton 		btrfs_info(root->fs_info,
1440efe120a0SFrank Holton 			   "resizer unable to apply on readonly device %llu",
1441c1c9ff7cSGeert Uytterhoeven 		       devid);
1442dfd79829SMiao Xie 		ret = -EPERM;
14434e42ae1bSLiu Bo 		goto out_free;
14444e42ae1bSLiu Bo 	}
14454e42ae1bSLiu Bo 
1446f46b5a66SChristoph Hellwig 	if (!strcmp(sizestr, "max"))
1447f46b5a66SChristoph Hellwig 		new_size = device->bdev->bd_inode->i_size;
1448f46b5a66SChristoph Hellwig 	else {
1449f46b5a66SChristoph Hellwig 		if (sizestr[0] == '-') {
1450f46b5a66SChristoph Hellwig 			mod = -1;
1451f46b5a66SChristoph Hellwig 			sizestr++;
1452f46b5a66SChristoph Hellwig 		} else if (sizestr[0] == '+') {
1453f46b5a66SChristoph Hellwig 			mod = 1;
1454f46b5a66SChristoph Hellwig 			sizestr++;
1455f46b5a66SChristoph Hellwig 		}
145691748467SAkinobu Mita 		new_size = memparse(sizestr, NULL);
1457f46b5a66SChristoph Hellwig 		if (new_size == 0) {
1458f46b5a66SChristoph Hellwig 			ret = -EINVAL;
1459c9e9f97bSIlya Dryomov 			goto out_free;
1460f46b5a66SChristoph Hellwig 		}
1461f46b5a66SChristoph Hellwig 	}
1462f46b5a66SChristoph Hellwig 
146363a212abSStefan Behrens 	if (device->is_tgtdev_for_dev_replace) {
1464dfd79829SMiao Xie 		ret = -EPERM;
146563a212abSStefan Behrens 		goto out_free;
146663a212abSStefan Behrens 	}
146763a212abSStefan Behrens 
1468f46b5a66SChristoph Hellwig 	old_size = device->total_bytes;
1469f46b5a66SChristoph Hellwig 
1470f46b5a66SChristoph Hellwig 	if (mod < 0) {
1471f46b5a66SChristoph Hellwig 		if (new_size > old_size) {
1472f46b5a66SChristoph Hellwig 			ret = -EINVAL;
1473c9e9f97bSIlya Dryomov 			goto out_free;
1474f46b5a66SChristoph Hellwig 		}
1475f46b5a66SChristoph Hellwig 		new_size = old_size - new_size;
1476f46b5a66SChristoph Hellwig 	} else if (mod > 0) {
1477*eb8052e0SWenliang Fan 		if (new_size > ULLONG_MAX - old_size) {
1478*eb8052e0SWenliang Fan 			ret = -EINVAL;
1479*eb8052e0SWenliang Fan 			goto out_free;
1480*eb8052e0SWenliang Fan 		}
1481f46b5a66SChristoph Hellwig 		new_size = old_size + new_size;
1482f46b5a66SChristoph Hellwig 	}
1483f46b5a66SChristoph Hellwig 
1484f46b5a66SChristoph Hellwig 	if (new_size < 256 * 1024 * 1024) {
1485f46b5a66SChristoph Hellwig 		ret = -EINVAL;
1486c9e9f97bSIlya Dryomov 		goto out_free;
1487f46b5a66SChristoph Hellwig 	}
1488f46b5a66SChristoph Hellwig 	if (new_size > device->bdev->bd_inode->i_size) {
1489f46b5a66SChristoph Hellwig 		ret = -EFBIG;
1490c9e9f97bSIlya Dryomov 		goto out_free;
1491f46b5a66SChristoph Hellwig 	}
1492f46b5a66SChristoph Hellwig 
1493f46b5a66SChristoph Hellwig 	do_div(new_size, root->sectorsize);
1494f46b5a66SChristoph Hellwig 	new_size *= root->sectorsize;
1495f46b5a66SChristoph Hellwig 
1496efe120a0SFrank Holton 	printk_in_rcu(KERN_INFO "BTRFS: new size for %s is %llu\n",
1497c1c9ff7cSGeert Uytterhoeven 		      rcu_str_deref(device->name), new_size);
1498f46b5a66SChristoph Hellwig 
1499f46b5a66SChristoph Hellwig 	if (new_size > old_size) {
1500a22285a6SYan, Zheng 		trans = btrfs_start_transaction(root, 0);
150198d5dc13STsutomu Itoh 		if (IS_ERR(trans)) {
150298d5dc13STsutomu Itoh 			ret = PTR_ERR(trans);
1503c9e9f97bSIlya Dryomov 			goto out_free;
150498d5dc13STsutomu Itoh 		}
1505f46b5a66SChristoph Hellwig 		ret = btrfs_grow_device(trans, device, new_size);
1506f46b5a66SChristoph Hellwig 		btrfs_commit_transaction(trans, root);
1507ece7d20eSMike Fleetwood 	} else if (new_size < old_size) {
1508f46b5a66SChristoph Hellwig 		ret = btrfs_shrink_device(device, new_size);
15090253f40eSjeff.liu 	} /* equal, nothing need to do */
1510f46b5a66SChristoph Hellwig 
1511c9e9f97bSIlya Dryomov out_free:
1512f46b5a66SChristoph Hellwig 	kfree(vol_args);
1513c9e9f97bSIlya Dryomov out:
1514c9e9f97bSIlya Dryomov 	mutex_unlock(&root->fs_info->volume_mutex);
15155ac00addSStefan Behrens 	atomic_set(&root->fs_info->mutually_exclusive_operation_running, 0);
151618f39c41SIlya Dryomov 	mnt_drop_write_file(file);
1517f46b5a66SChristoph Hellwig 	return ret;
1518f46b5a66SChristoph Hellwig }
1519f46b5a66SChristoph Hellwig 
152072fd032eSSage Weil static noinline int btrfs_ioctl_snap_create_transid(struct file *file,
15216f72c7e2SArne Jansen 				char *name, unsigned long fd, int subvol,
15226f72c7e2SArne Jansen 				u64 *transid, bool readonly,
15238696c533SMiao Xie 				struct btrfs_qgroup_inherit *inherit)
1524f46b5a66SChristoph Hellwig {
1525f46b5a66SChristoph Hellwig 	int namelen;
15263de4586cSChris Mason 	int ret = 0;
1527f46b5a66SChristoph Hellwig 
1528a874a63eSLiu Bo 	ret = mnt_want_write_file(file);
1529a874a63eSLiu Bo 	if (ret)
1530a874a63eSLiu Bo 		goto out;
1531a874a63eSLiu Bo 
153272fd032eSSage Weil 	namelen = strlen(name);
153372fd032eSSage Weil 	if (strchr(name, '/')) {
1534f46b5a66SChristoph Hellwig 		ret = -EINVAL;
1535a874a63eSLiu Bo 		goto out_drop_write;
1536f46b5a66SChristoph Hellwig 	}
1537f46b5a66SChristoph Hellwig 
153816780cabSChris Mason 	if (name[0] == '.' &&
153916780cabSChris Mason 	   (namelen == 1 || (name[1] == '.' && namelen == 2))) {
154016780cabSChris Mason 		ret = -EEXIST;
1541a874a63eSLiu Bo 		goto out_drop_write;
154216780cabSChris Mason 	}
154316780cabSChris Mason 
15443de4586cSChris Mason 	if (subvol) {
154572fd032eSSage Weil 		ret = btrfs_mksubvol(&file->f_path, name, namelen,
15466f72c7e2SArne Jansen 				     NULL, transid, readonly, inherit);
1547cb8e7090SChristoph Hellwig 	} else {
15482903ff01SAl Viro 		struct fd src = fdget(fd);
15493de4586cSChris Mason 		struct inode *src_inode;
15502903ff01SAl Viro 		if (!src.file) {
15513de4586cSChris Mason 			ret = -EINVAL;
1552a874a63eSLiu Bo 			goto out_drop_write;
15533de4586cSChris Mason 		}
15543de4586cSChris Mason 
1555496ad9aaSAl Viro 		src_inode = file_inode(src.file);
1556496ad9aaSAl Viro 		if (src_inode->i_sb != file_inode(file)->i_sb) {
1557efe120a0SFrank Holton 			btrfs_info(BTRFS_I(src_inode)->root->fs_info,
1558efe120a0SFrank Holton 				   "Snapshot src from another FS");
15593de4586cSChris Mason 			ret = -EINVAL;
1560ecd18815SAl Viro 		} else {
156172fd032eSSage Weil 			ret = btrfs_mksubvol(&file->f_path, name, namelen,
156272fd032eSSage Weil 					     BTRFS_I(src_inode)->root,
15636f72c7e2SArne Jansen 					     transid, readonly, inherit);
1564ecd18815SAl Viro 		}
15652903ff01SAl Viro 		fdput(src);
1566cb8e7090SChristoph Hellwig 	}
1567a874a63eSLiu Bo out_drop_write:
1568a874a63eSLiu Bo 	mnt_drop_write_file(file);
1569f46b5a66SChristoph Hellwig out:
157072fd032eSSage Weil 	return ret;
157172fd032eSSage Weil }
157272fd032eSSage Weil 
157372fd032eSSage Weil static noinline int btrfs_ioctl_snap_create(struct file *file,
1574fa0d2b9bSLi Zefan 					    void __user *arg, int subvol)
157572fd032eSSage Weil {
1576fa0d2b9bSLi Zefan 	struct btrfs_ioctl_vol_args *vol_args;
157772fd032eSSage Weil 	int ret;
157872fd032eSSage Weil 
1579fa0d2b9bSLi Zefan 	vol_args = memdup_user(arg, sizeof(*vol_args));
1580fa0d2b9bSLi Zefan 	if (IS_ERR(vol_args))
1581fa0d2b9bSLi Zefan 		return PTR_ERR(vol_args);
1582fa0d2b9bSLi Zefan 	vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
1583fa0d2b9bSLi Zefan 
1584fa0d2b9bSLi Zefan 	ret = btrfs_ioctl_snap_create_transid(file, vol_args->name,
1585b83cc969SLi Zefan 					      vol_args->fd, subvol,
15866f72c7e2SArne Jansen 					      NULL, false, NULL);
1587fa0d2b9bSLi Zefan 
1588fa0d2b9bSLi Zefan 	kfree(vol_args);
1589fa0d2b9bSLi Zefan 	return ret;
1590fa0d2b9bSLi Zefan }
1591fa0d2b9bSLi Zefan 
1592fa0d2b9bSLi Zefan static noinline int btrfs_ioctl_snap_create_v2(struct file *file,
1593fa0d2b9bSLi Zefan 					       void __user *arg, int subvol)
1594fa0d2b9bSLi Zefan {
1595fa0d2b9bSLi Zefan 	struct btrfs_ioctl_vol_args_v2 *vol_args;
1596fa0d2b9bSLi Zefan 	int ret;
1597fdfb1e4fSLi Zefan 	u64 transid = 0;
1598fdfb1e4fSLi Zefan 	u64 *ptr = NULL;
1599b83cc969SLi Zefan 	bool readonly = false;
16006f72c7e2SArne Jansen 	struct btrfs_qgroup_inherit *inherit = NULL;
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_SUBVOL_NAME_MAX] = '\0';
1606fdfb1e4fSLi Zefan 
1607b83cc969SLi Zefan 	if (vol_args->flags &
16086f72c7e2SArne Jansen 	    ~(BTRFS_SUBVOL_CREATE_ASYNC | BTRFS_SUBVOL_RDONLY |
16096f72c7e2SArne Jansen 	      BTRFS_SUBVOL_QGROUP_INHERIT)) {
1610b83cc969SLi Zefan 		ret = -EOPNOTSUPP;
1611fdfb1e4fSLi Zefan 		goto out;
1612fdfb1e4fSLi Zefan 	}
1613fdfb1e4fSLi Zefan 
1614fa0d2b9bSLi Zefan 	if (vol_args->flags & BTRFS_SUBVOL_CREATE_ASYNC)
1615fdfb1e4fSLi Zefan 		ptr = &transid;
1616b83cc969SLi Zefan 	if (vol_args->flags & BTRFS_SUBVOL_RDONLY)
1617b83cc969SLi Zefan 		readonly = true;
16186f72c7e2SArne Jansen 	if (vol_args->flags & BTRFS_SUBVOL_QGROUP_INHERIT) {
16196f72c7e2SArne Jansen 		if (vol_args->size > PAGE_CACHE_SIZE) {
16206f72c7e2SArne Jansen 			ret = -EINVAL;
16216f72c7e2SArne Jansen 			goto out;
16226f72c7e2SArne Jansen 		}
16236f72c7e2SArne Jansen 		inherit = memdup_user(vol_args->qgroup_inherit, vol_args->size);
16246f72c7e2SArne Jansen 		if (IS_ERR(inherit)) {
16256f72c7e2SArne Jansen 			ret = PTR_ERR(inherit);
16266f72c7e2SArne Jansen 			goto out;
16276f72c7e2SArne Jansen 		}
16286f72c7e2SArne Jansen 	}
162975eaa0e2SSage Weil 
1630fa0d2b9bSLi Zefan 	ret = btrfs_ioctl_snap_create_transid(file, vol_args->name,
16316f72c7e2SArne Jansen 					      vol_args->fd, subvol, ptr,
16328696c533SMiao Xie 					      readonly, inherit);
163375eaa0e2SSage Weil 
1634fdfb1e4fSLi Zefan 	if (ret == 0 && ptr &&
163575eaa0e2SSage Weil 	    copy_to_user(arg +
1636fdfb1e4fSLi Zefan 			 offsetof(struct btrfs_ioctl_vol_args_v2,
1637fdfb1e4fSLi Zefan 				  transid), ptr, sizeof(*ptr)))
163875eaa0e2SSage Weil 		ret = -EFAULT;
1639fdfb1e4fSLi Zefan out:
1640f46b5a66SChristoph Hellwig 	kfree(vol_args);
16416f72c7e2SArne Jansen 	kfree(inherit);
1642f46b5a66SChristoph Hellwig 	return ret;
1643f46b5a66SChristoph Hellwig }
1644f46b5a66SChristoph Hellwig 
16450caa102dSLi Zefan static noinline int btrfs_ioctl_subvol_getflags(struct file *file,
16460caa102dSLi Zefan 						void __user *arg)
16470caa102dSLi Zefan {
1648496ad9aaSAl Viro 	struct inode *inode = file_inode(file);
16490caa102dSLi Zefan 	struct btrfs_root *root = BTRFS_I(inode)->root;
16500caa102dSLi Zefan 	int ret = 0;
16510caa102dSLi Zefan 	u64 flags = 0;
16520caa102dSLi Zefan 
165333345d01SLi Zefan 	if (btrfs_ino(inode) != BTRFS_FIRST_FREE_OBJECTID)
16540caa102dSLi Zefan 		return -EINVAL;
16550caa102dSLi Zefan 
16560caa102dSLi Zefan 	down_read(&root->fs_info->subvol_sem);
16570caa102dSLi Zefan 	if (btrfs_root_readonly(root))
16580caa102dSLi Zefan 		flags |= BTRFS_SUBVOL_RDONLY;
16590caa102dSLi Zefan 	up_read(&root->fs_info->subvol_sem);
16600caa102dSLi Zefan 
16610caa102dSLi Zefan 	if (copy_to_user(arg, &flags, sizeof(flags)))
16620caa102dSLi Zefan 		ret = -EFAULT;
16630caa102dSLi Zefan 
16640caa102dSLi Zefan 	return ret;
16650caa102dSLi Zefan }
16660caa102dSLi Zefan 
16670caa102dSLi Zefan static noinline int btrfs_ioctl_subvol_setflags(struct file *file,
16680caa102dSLi Zefan 					      void __user *arg)
16690caa102dSLi Zefan {
1670496ad9aaSAl Viro 	struct inode *inode = file_inode(file);
16710caa102dSLi Zefan 	struct btrfs_root *root = BTRFS_I(inode)->root;
16720caa102dSLi Zefan 	struct btrfs_trans_handle *trans;
16730caa102dSLi Zefan 	u64 root_flags;
16740caa102dSLi Zefan 	u64 flags;
16750caa102dSLi Zefan 	int ret = 0;
16760caa102dSLi Zefan 
1677b9ca0664SLiu Bo 	ret = mnt_want_write_file(file);
1678b9ca0664SLiu Bo 	if (ret)
1679b9ca0664SLiu Bo 		goto out;
16800caa102dSLi Zefan 
1681b9ca0664SLiu Bo 	if (btrfs_ino(inode) != BTRFS_FIRST_FREE_OBJECTID) {
1682b9ca0664SLiu Bo 		ret = -EINVAL;
1683b9ca0664SLiu Bo 		goto out_drop_write;
1684b9ca0664SLiu Bo 	}
16850caa102dSLi Zefan 
1686b9ca0664SLiu Bo 	if (copy_from_user(&flags, arg, sizeof(flags))) {
1687b9ca0664SLiu Bo 		ret = -EFAULT;
1688b9ca0664SLiu Bo 		goto out_drop_write;
1689b9ca0664SLiu Bo 	}
16900caa102dSLi Zefan 
1691b9ca0664SLiu Bo 	if (flags & BTRFS_SUBVOL_CREATE_ASYNC) {
1692b9ca0664SLiu Bo 		ret = -EINVAL;
1693b9ca0664SLiu Bo 		goto out_drop_write;
1694b9ca0664SLiu Bo 	}
16950caa102dSLi Zefan 
1696b9ca0664SLiu Bo 	if (flags & ~BTRFS_SUBVOL_RDONLY) {
1697b9ca0664SLiu Bo 		ret = -EOPNOTSUPP;
1698b9ca0664SLiu Bo 		goto out_drop_write;
1699b9ca0664SLiu Bo 	}
17000caa102dSLi Zefan 
1701b9ca0664SLiu Bo 	if (!inode_owner_or_capable(inode)) {
1702b9ca0664SLiu Bo 		ret = -EACCES;
1703b9ca0664SLiu Bo 		goto out_drop_write;
1704b9ca0664SLiu Bo 	}
1705b4dc2b8cSLi Zefan 
17060caa102dSLi Zefan 	down_write(&root->fs_info->subvol_sem);
17070caa102dSLi Zefan 
17080caa102dSLi Zefan 	/* nothing to do */
17090caa102dSLi Zefan 	if (!!(flags & BTRFS_SUBVOL_RDONLY) == btrfs_root_readonly(root))
1710b9ca0664SLiu Bo 		goto out_drop_sem;
17110caa102dSLi Zefan 
17120caa102dSLi Zefan 	root_flags = btrfs_root_flags(&root->root_item);
17132c686537SDavid Sterba 	if (flags & BTRFS_SUBVOL_RDONLY) {
17140caa102dSLi Zefan 		btrfs_set_root_flags(&root->root_item,
17150caa102dSLi Zefan 				     root_flags | BTRFS_ROOT_SUBVOL_RDONLY);
17162c686537SDavid Sterba 	} else {
17172c686537SDavid Sterba 		/*
17182c686537SDavid Sterba 		 * Block RO -> RW transition if this subvolume is involved in
17192c686537SDavid Sterba 		 * send
17202c686537SDavid Sterba 		 */
17212c686537SDavid Sterba 		spin_lock(&root->root_item_lock);
17222c686537SDavid Sterba 		if (root->send_in_progress == 0) {
17230caa102dSLi Zefan 			btrfs_set_root_flags(&root->root_item,
17240caa102dSLi Zefan 				     root_flags & ~BTRFS_ROOT_SUBVOL_RDONLY);
17252c686537SDavid Sterba 			spin_unlock(&root->root_item_lock);
17262c686537SDavid Sterba 		} else {
17272c686537SDavid Sterba 			spin_unlock(&root->root_item_lock);
17282c686537SDavid Sterba 			btrfs_warn(root->fs_info,
17292c686537SDavid Sterba 			"Attempt to set subvolume %llu read-write during send",
17302c686537SDavid Sterba 					root->root_key.objectid);
17312c686537SDavid Sterba 			ret = -EPERM;
17322c686537SDavid Sterba 			goto out_drop_sem;
17332c686537SDavid Sterba 		}
17342c686537SDavid Sterba 	}
17350caa102dSLi Zefan 
17360caa102dSLi Zefan 	trans = btrfs_start_transaction(root, 1);
17370caa102dSLi Zefan 	if (IS_ERR(trans)) {
17380caa102dSLi Zefan 		ret = PTR_ERR(trans);
17390caa102dSLi Zefan 		goto out_reset;
17400caa102dSLi Zefan 	}
17410caa102dSLi Zefan 
1742b4dc2b8cSLi Zefan 	ret = btrfs_update_root(trans, root->fs_info->tree_root,
17430caa102dSLi Zefan 				&root->root_key, &root->root_item);
17440caa102dSLi Zefan 
17450caa102dSLi Zefan 	btrfs_commit_transaction(trans, root);
17460caa102dSLi Zefan out_reset:
17470caa102dSLi Zefan 	if (ret)
17480caa102dSLi Zefan 		btrfs_set_root_flags(&root->root_item, root_flags);
1749b9ca0664SLiu Bo out_drop_sem:
17500caa102dSLi Zefan 	up_write(&root->fs_info->subvol_sem);
1751b9ca0664SLiu Bo out_drop_write:
1752b9ca0664SLiu Bo 	mnt_drop_write_file(file);
1753b9ca0664SLiu Bo out:
17540caa102dSLi Zefan 	return ret;
17550caa102dSLi Zefan }
17560caa102dSLi Zefan 
175776dda93cSYan, Zheng /*
175876dda93cSYan, Zheng  * helper to check if the subvolume references other subvolumes
175976dda93cSYan, Zheng  */
176076dda93cSYan, Zheng static noinline int may_destroy_subvol(struct btrfs_root *root)
176176dda93cSYan, Zheng {
176276dda93cSYan, Zheng 	struct btrfs_path *path;
1763175a2b87SJosef Bacik 	struct btrfs_dir_item *di;
176476dda93cSYan, Zheng 	struct btrfs_key key;
1765175a2b87SJosef Bacik 	u64 dir_id;
176676dda93cSYan, Zheng 	int ret;
176776dda93cSYan, Zheng 
176876dda93cSYan, Zheng 	path = btrfs_alloc_path();
176976dda93cSYan, Zheng 	if (!path)
177076dda93cSYan, Zheng 		return -ENOMEM;
177176dda93cSYan, Zheng 
1772175a2b87SJosef Bacik 	/* Make sure this root isn't set as the default subvol */
1773175a2b87SJosef Bacik 	dir_id = btrfs_super_root_dir(root->fs_info->super_copy);
1774175a2b87SJosef Bacik 	di = btrfs_lookup_dir_item(NULL, root->fs_info->tree_root, path,
1775175a2b87SJosef Bacik 				   dir_id, "default", 7, 0);
1776175a2b87SJosef Bacik 	if (di && !IS_ERR(di)) {
1777175a2b87SJosef Bacik 		btrfs_dir_item_key_to_cpu(path->nodes[0], di, &key);
1778175a2b87SJosef Bacik 		if (key.objectid == root->root_key.objectid) {
1779175a2b87SJosef Bacik 			ret = -ENOTEMPTY;
1780175a2b87SJosef Bacik 			goto out;
1781175a2b87SJosef Bacik 		}
1782175a2b87SJosef Bacik 		btrfs_release_path(path);
1783175a2b87SJosef Bacik 	}
1784175a2b87SJosef Bacik 
178576dda93cSYan, Zheng 	key.objectid = root->root_key.objectid;
178676dda93cSYan, Zheng 	key.type = BTRFS_ROOT_REF_KEY;
178776dda93cSYan, Zheng 	key.offset = (u64)-1;
178876dda93cSYan, Zheng 
178976dda93cSYan, Zheng 	ret = btrfs_search_slot(NULL, root->fs_info->tree_root,
179076dda93cSYan, Zheng 				&key, path, 0, 0);
179176dda93cSYan, Zheng 	if (ret < 0)
179276dda93cSYan, Zheng 		goto out;
179376dda93cSYan, Zheng 	BUG_ON(ret == 0);
179476dda93cSYan, Zheng 
179576dda93cSYan, Zheng 	ret = 0;
179676dda93cSYan, Zheng 	if (path->slots[0] > 0) {
179776dda93cSYan, Zheng 		path->slots[0]--;
179876dda93cSYan, Zheng 		btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
179976dda93cSYan, Zheng 		if (key.objectid == root->root_key.objectid &&
180076dda93cSYan, Zheng 		    key.type == BTRFS_ROOT_REF_KEY)
180176dda93cSYan, Zheng 			ret = -ENOTEMPTY;
180276dda93cSYan, Zheng 	}
180376dda93cSYan, Zheng out:
180476dda93cSYan, Zheng 	btrfs_free_path(path);
180576dda93cSYan, Zheng 	return ret;
180676dda93cSYan, Zheng }
180776dda93cSYan, Zheng 
1808ac8e9819SChris Mason static noinline int key_in_sk(struct btrfs_key *key,
1809ac8e9819SChris Mason 			      struct btrfs_ioctl_search_key *sk)
1810ac8e9819SChris Mason {
1811abc6e134SChris Mason 	struct btrfs_key test;
1812abc6e134SChris Mason 	int ret;
1813abc6e134SChris Mason 
1814abc6e134SChris Mason 	test.objectid = sk->min_objectid;
1815abc6e134SChris Mason 	test.type = sk->min_type;
1816abc6e134SChris Mason 	test.offset = sk->min_offset;
1817abc6e134SChris Mason 
1818abc6e134SChris Mason 	ret = btrfs_comp_cpu_keys(key, &test);
1819abc6e134SChris Mason 	if (ret < 0)
1820ac8e9819SChris Mason 		return 0;
1821abc6e134SChris Mason 
1822abc6e134SChris Mason 	test.objectid = sk->max_objectid;
1823abc6e134SChris Mason 	test.type = sk->max_type;
1824abc6e134SChris Mason 	test.offset = sk->max_offset;
1825abc6e134SChris Mason 
1826abc6e134SChris Mason 	ret = btrfs_comp_cpu_keys(key, &test);
1827abc6e134SChris Mason 	if (ret > 0)
1828ac8e9819SChris Mason 		return 0;
1829ac8e9819SChris Mason 	return 1;
1830ac8e9819SChris Mason }
1831ac8e9819SChris Mason 
1832ac8e9819SChris Mason static noinline int copy_to_sk(struct btrfs_root *root,
1833ac8e9819SChris Mason 			       struct btrfs_path *path,
1834ac8e9819SChris Mason 			       struct btrfs_key *key,
1835ac8e9819SChris Mason 			       struct btrfs_ioctl_search_key *sk,
1836ac8e9819SChris Mason 			       char *buf,
1837ac8e9819SChris Mason 			       unsigned long *sk_offset,
1838ac8e9819SChris Mason 			       int *num_found)
1839ac8e9819SChris Mason {
1840ac8e9819SChris Mason 	u64 found_transid;
1841ac8e9819SChris Mason 	struct extent_buffer *leaf;
1842ac8e9819SChris Mason 	struct btrfs_ioctl_search_header sh;
1843ac8e9819SChris Mason 	unsigned long item_off;
1844ac8e9819SChris Mason 	unsigned long item_len;
1845ac8e9819SChris Mason 	int nritems;
1846ac8e9819SChris Mason 	int i;
1847ac8e9819SChris Mason 	int slot;
1848ac8e9819SChris Mason 	int ret = 0;
1849ac8e9819SChris Mason 
1850ac8e9819SChris Mason 	leaf = path->nodes[0];
1851ac8e9819SChris Mason 	slot = path->slots[0];
1852ac8e9819SChris Mason 	nritems = btrfs_header_nritems(leaf);
1853ac8e9819SChris Mason 
1854ac8e9819SChris Mason 	if (btrfs_header_generation(leaf) > sk->max_transid) {
1855ac8e9819SChris Mason 		i = nritems;
1856ac8e9819SChris Mason 		goto advance_key;
1857ac8e9819SChris Mason 	}
1858ac8e9819SChris Mason 	found_transid = btrfs_header_generation(leaf);
1859ac8e9819SChris Mason 
1860ac8e9819SChris Mason 	for (i = slot; i < nritems; i++) {
1861ac8e9819SChris Mason 		item_off = btrfs_item_ptr_offset(leaf, i);
1862ac8e9819SChris Mason 		item_len = btrfs_item_size_nr(leaf, i);
1863ac8e9819SChris Mason 
186403b71c6cSGabriel de Perthuis 		btrfs_item_key_to_cpu(leaf, key, i);
186503b71c6cSGabriel de Perthuis 		if (!key_in_sk(key, sk))
186603b71c6cSGabriel de Perthuis 			continue;
186703b71c6cSGabriel de Perthuis 
186803b71c6cSGabriel de Perthuis 		if (sizeof(sh) + item_len > BTRFS_SEARCH_ARGS_BUFSIZE)
1869ac8e9819SChris Mason 			item_len = 0;
1870ac8e9819SChris Mason 
1871ac8e9819SChris Mason 		if (sizeof(sh) + item_len + *sk_offset >
1872ac8e9819SChris Mason 		    BTRFS_SEARCH_ARGS_BUFSIZE) {
1873ac8e9819SChris Mason 			ret = 1;
1874ac8e9819SChris Mason 			goto overflow;
1875ac8e9819SChris Mason 		}
1876ac8e9819SChris Mason 
1877ac8e9819SChris Mason 		sh.objectid = key->objectid;
1878ac8e9819SChris Mason 		sh.offset = key->offset;
1879ac8e9819SChris Mason 		sh.type = key->type;
1880ac8e9819SChris Mason 		sh.len = item_len;
1881ac8e9819SChris Mason 		sh.transid = found_transid;
1882ac8e9819SChris Mason 
1883ac8e9819SChris Mason 		/* copy search result header */
1884ac8e9819SChris Mason 		memcpy(buf + *sk_offset, &sh, sizeof(sh));
1885ac8e9819SChris Mason 		*sk_offset += sizeof(sh);
1886ac8e9819SChris Mason 
1887ac8e9819SChris Mason 		if (item_len) {
1888ac8e9819SChris Mason 			char *p = buf + *sk_offset;
1889ac8e9819SChris Mason 			/* copy the item */
1890ac8e9819SChris Mason 			read_extent_buffer(leaf, p,
1891ac8e9819SChris Mason 					   item_off, item_len);
1892ac8e9819SChris Mason 			*sk_offset += item_len;
1893ac8e9819SChris Mason 		}
1894e2156867SHugo Mills 		(*num_found)++;
1895ac8e9819SChris Mason 
1896ac8e9819SChris Mason 		if (*num_found >= sk->nr_items)
1897ac8e9819SChris Mason 			break;
1898ac8e9819SChris Mason 	}
1899ac8e9819SChris Mason advance_key:
1900ac8e9819SChris Mason 	ret = 0;
1901abc6e134SChris Mason 	if (key->offset < (u64)-1 && key->offset < sk->max_offset)
1902abc6e134SChris Mason 		key->offset++;
1903abc6e134SChris Mason 	else if (key->type < (u8)-1 && key->type < sk->max_type) {
1904abc6e134SChris Mason 		key->offset = 0;
1905abc6e134SChris Mason 		key->type++;
1906abc6e134SChris Mason 	} else if (key->objectid < (u64)-1 && key->objectid < sk->max_objectid) {
1907abc6e134SChris Mason 		key->offset = 0;
1908abc6e134SChris Mason 		key->type = 0;
1909abc6e134SChris Mason 		key->objectid++;
1910abc6e134SChris Mason 	} else
1911abc6e134SChris Mason 		ret = 1;
1912ac8e9819SChris Mason overflow:
1913ac8e9819SChris Mason 	return ret;
1914ac8e9819SChris Mason }
1915ac8e9819SChris Mason 
1916ac8e9819SChris Mason static noinline int search_ioctl(struct inode *inode,
1917ac8e9819SChris Mason 				 struct btrfs_ioctl_search_args *args)
1918ac8e9819SChris Mason {
1919ac8e9819SChris Mason 	struct btrfs_root *root;
1920ac8e9819SChris Mason 	struct btrfs_key key;
1921ac8e9819SChris Mason 	struct btrfs_path *path;
1922ac8e9819SChris Mason 	struct btrfs_ioctl_search_key *sk = &args->key;
1923ac8e9819SChris Mason 	struct btrfs_fs_info *info = BTRFS_I(inode)->root->fs_info;
1924ac8e9819SChris Mason 	int ret;
1925ac8e9819SChris Mason 	int num_found = 0;
1926ac8e9819SChris Mason 	unsigned long sk_offset = 0;
1927ac8e9819SChris Mason 
1928ac8e9819SChris Mason 	path = btrfs_alloc_path();
1929ac8e9819SChris Mason 	if (!path)
1930ac8e9819SChris Mason 		return -ENOMEM;
1931ac8e9819SChris Mason 
1932ac8e9819SChris Mason 	if (sk->tree_id == 0) {
1933ac8e9819SChris Mason 		/* search the root of the inode that was passed */
1934ac8e9819SChris Mason 		root = BTRFS_I(inode)->root;
1935ac8e9819SChris Mason 	} else {
1936ac8e9819SChris Mason 		key.objectid = sk->tree_id;
1937ac8e9819SChris Mason 		key.type = BTRFS_ROOT_ITEM_KEY;
1938ac8e9819SChris Mason 		key.offset = (u64)-1;
1939ac8e9819SChris Mason 		root = btrfs_read_fs_root_no_name(info, &key);
1940ac8e9819SChris Mason 		if (IS_ERR(root)) {
1941efe120a0SFrank Holton 			printk(KERN_ERR "BTRFS: could not find root %llu\n",
1942ac8e9819SChris Mason 			       sk->tree_id);
1943ac8e9819SChris Mason 			btrfs_free_path(path);
1944ac8e9819SChris Mason 			return -ENOENT;
1945ac8e9819SChris Mason 		}
1946ac8e9819SChris Mason 	}
1947ac8e9819SChris Mason 
1948ac8e9819SChris Mason 	key.objectid = sk->min_objectid;
1949ac8e9819SChris Mason 	key.type = sk->min_type;
1950ac8e9819SChris Mason 	key.offset = sk->min_offset;
1951ac8e9819SChris Mason 
1952ac8e9819SChris Mason 	path->keep_locks = 1;
1953ac8e9819SChris Mason 
1954ac8e9819SChris Mason 	while (1) {
19556174d3cbSFilipe David Borba Manana 		ret = btrfs_search_forward(root, &key, path, sk->min_transid);
1956ac8e9819SChris Mason 		if (ret != 0) {
1957ac8e9819SChris Mason 			if (ret > 0)
1958ac8e9819SChris Mason 				ret = 0;
1959ac8e9819SChris Mason 			goto err;
1960ac8e9819SChris Mason 		}
1961ac8e9819SChris Mason 		ret = copy_to_sk(root, path, &key, sk, args->buf,
1962ac8e9819SChris Mason 				 &sk_offset, &num_found);
1963b3b4aa74SDavid Sterba 		btrfs_release_path(path);
1964ac8e9819SChris Mason 		if (ret || num_found >= sk->nr_items)
1965ac8e9819SChris Mason 			break;
1966ac8e9819SChris Mason 
1967ac8e9819SChris Mason 	}
1968ac8e9819SChris Mason 	ret = 0;
1969ac8e9819SChris Mason err:
1970ac8e9819SChris Mason 	sk->nr_items = num_found;
1971ac8e9819SChris Mason 	btrfs_free_path(path);
1972ac8e9819SChris Mason 	return ret;
1973ac8e9819SChris Mason }
1974ac8e9819SChris Mason 
1975ac8e9819SChris Mason static noinline int btrfs_ioctl_tree_search(struct file *file,
1976ac8e9819SChris Mason 					   void __user *argp)
1977ac8e9819SChris Mason {
1978ac8e9819SChris Mason 	 struct btrfs_ioctl_search_args *args;
1979ac8e9819SChris Mason 	 struct inode *inode;
1980ac8e9819SChris Mason 	 int ret;
1981ac8e9819SChris Mason 
1982ac8e9819SChris Mason 	if (!capable(CAP_SYS_ADMIN))
1983ac8e9819SChris Mason 		return -EPERM;
1984ac8e9819SChris Mason 
19852354d08fSJulia Lawall 	args = memdup_user(argp, sizeof(*args));
19862354d08fSJulia Lawall 	if (IS_ERR(args))
19872354d08fSJulia Lawall 		return PTR_ERR(args);
1988ac8e9819SChris Mason 
1989496ad9aaSAl Viro 	inode = file_inode(file);
1990ac8e9819SChris Mason 	ret = search_ioctl(inode, args);
1991ac8e9819SChris Mason 	if (ret == 0 && copy_to_user(argp, args, sizeof(*args)))
1992ac8e9819SChris Mason 		ret = -EFAULT;
1993ac8e9819SChris Mason 	kfree(args);
1994ac8e9819SChris Mason 	return ret;
1995ac8e9819SChris Mason }
1996ac8e9819SChris Mason 
199798d377a0STARUISI Hiroaki /*
1998ac8e9819SChris Mason  * Search INODE_REFs to identify path name of 'dirid' directory
1999ac8e9819SChris Mason  * in a 'tree_id' tree. and sets path name to 'name'.
200098d377a0STARUISI Hiroaki  */
200198d377a0STARUISI Hiroaki static noinline int btrfs_search_path_in_tree(struct btrfs_fs_info *info,
200298d377a0STARUISI Hiroaki 				u64 tree_id, u64 dirid, char *name)
200398d377a0STARUISI Hiroaki {
200498d377a0STARUISI Hiroaki 	struct btrfs_root *root;
200598d377a0STARUISI Hiroaki 	struct btrfs_key key;
2006ac8e9819SChris Mason 	char *ptr;
200798d377a0STARUISI Hiroaki 	int ret = -1;
200898d377a0STARUISI Hiroaki 	int slot;
200998d377a0STARUISI Hiroaki 	int len;
201098d377a0STARUISI Hiroaki 	int total_len = 0;
201198d377a0STARUISI Hiroaki 	struct btrfs_inode_ref *iref;
201298d377a0STARUISI Hiroaki 	struct extent_buffer *l;
201398d377a0STARUISI Hiroaki 	struct btrfs_path *path;
201498d377a0STARUISI Hiroaki 
201598d377a0STARUISI Hiroaki 	if (dirid == BTRFS_FIRST_FREE_OBJECTID) {
201698d377a0STARUISI Hiroaki 		name[0]='\0';
201798d377a0STARUISI Hiroaki 		return 0;
201898d377a0STARUISI Hiroaki 	}
201998d377a0STARUISI Hiroaki 
202098d377a0STARUISI Hiroaki 	path = btrfs_alloc_path();
202198d377a0STARUISI Hiroaki 	if (!path)
202298d377a0STARUISI Hiroaki 		return -ENOMEM;
202398d377a0STARUISI Hiroaki 
2024ac8e9819SChris Mason 	ptr = &name[BTRFS_INO_LOOKUP_PATH_MAX];
202598d377a0STARUISI Hiroaki 
202698d377a0STARUISI Hiroaki 	key.objectid = tree_id;
202798d377a0STARUISI Hiroaki 	key.type = BTRFS_ROOT_ITEM_KEY;
202898d377a0STARUISI Hiroaki 	key.offset = (u64)-1;
202998d377a0STARUISI Hiroaki 	root = btrfs_read_fs_root_no_name(info, &key);
203098d377a0STARUISI Hiroaki 	if (IS_ERR(root)) {
2031efe120a0SFrank Holton 		printk(KERN_ERR "BTRFS: could not find root %llu\n", tree_id);
20328ad6fcabSChris Mason 		ret = -ENOENT;
20338ad6fcabSChris Mason 		goto out;
203498d377a0STARUISI Hiroaki 	}
203598d377a0STARUISI Hiroaki 
203698d377a0STARUISI Hiroaki 	key.objectid = dirid;
203798d377a0STARUISI Hiroaki 	key.type = BTRFS_INODE_REF_KEY;
20388ad6fcabSChris Mason 	key.offset = (u64)-1;
203998d377a0STARUISI Hiroaki 
204098d377a0STARUISI Hiroaki 	while (1) {
204198d377a0STARUISI Hiroaki 		ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
204298d377a0STARUISI Hiroaki 		if (ret < 0)
204398d377a0STARUISI Hiroaki 			goto out;
204418674c6cSFilipe David Borba Manana 		else if (ret > 0) {
204518674c6cSFilipe David Borba Manana 			ret = btrfs_previous_item(root, path, dirid,
204618674c6cSFilipe David Borba Manana 						  BTRFS_INODE_REF_KEY);
204718674c6cSFilipe David Borba Manana 			if (ret < 0)
204818674c6cSFilipe David Borba Manana 				goto out;
204918674c6cSFilipe David Borba Manana 			else if (ret > 0) {
2050ac8e9819SChris Mason 				ret = -ENOENT;
205198d377a0STARUISI Hiroaki 				goto out;
2052ac8e9819SChris Mason 			}
205318674c6cSFilipe David Borba Manana 		}
205418674c6cSFilipe David Borba Manana 
205518674c6cSFilipe David Borba Manana 		l = path->nodes[0];
205618674c6cSFilipe David Borba Manana 		slot = path->slots[0];
205718674c6cSFilipe David Borba Manana 		btrfs_item_key_to_cpu(l, &key, slot);
205898d377a0STARUISI Hiroaki 
205998d377a0STARUISI Hiroaki 		iref = btrfs_item_ptr(l, slot, struct btrfs_inode_ref);
206098d377a0STARUISI Hiroaki 		len = btrfs_inode_ref_name_len(l, iref);
206198d377a0STARUISI Hiroaki 		ptr -= len + 1;
206298d377a0STARUISI Hiroaki 		total_len += len + 1;
2063a696cf35SFilipe David Borba Manana 		if (ptr < name) {
2064a696cf35SFilipe David Borba Manana 			ret = -ENAMETOOLONG;
206598d377a0STARUISI Hiroaki 			goto out;
2066a696cf35SFilipe David Borba Manana 		}
206798d377a0STARUISI Hiroaki 
206898d377a0STARUISI Hiroaki 		*(ptr + len) = '/';
206998d377a0STARUISI Hiroaki 		read_extent_buffer(l, ptr, (unsigned long)(iref + 1), len);
207098d377a0STARUISI Hiroaki 
207198d377a0STARUISI Hiroaki 		if (key.offset == BTRFS_FIRST_FREE_OBJECTID)
207298d377a0STARUISI Hiroaki 			break;
207398d377a0STARUISI Hiroaki 
2074b3b4aa74SDavid Sterba 		btrfs_release_path(path);
207598d377a0STARUISI Hiroaki 		key.objectid = key.offset;
20768ad6fcabSChris Mason 		key.offset = (u64)-1;
207798d377a0STARUISI Hiroaki 		dirid = key.objectid;
207898d377a0STARUISI Hiroaki 	}
207977906a50SLi Zefan 	memmove(name, ptr, total_len);
208098d377a0STARUISI Hiroaki 	name[total_len] = '\0';
208198d377a0STARUISI Hiroaki 	ret = 0;
208298d377a0STARUISI Hiroaki out:
208398d377a0STARUISI Hiroaki 	btrfs_free_path(path);
2084ac8e9819SChris Mason 	return ret;
2085ac8e9819SChris Mason }
2086ac8e9819SChris Mason 
2087ac8e9819SChris Mason static noinline int btrfs_ioctl_ino_lookup(struct file *file,
2088ac8e9819SChris Mason 					   void __user *argp)
2089ac8e9819SChris Mason {
2090ac8e9819SChris Mason 	 struct btrfs_ioctl_ino_lookup_args *args;
2091ac8e9819SChris Mason 	 struct inode *inode;
2092ac8e9819SChris Mason 	 int ret;
2093ac8e9819SChris Mason 
2094ac8e9819SChris Mason 	if (!capable(CAP_SYS_ADMIN))
2095ac8e9819SChris Mason 		return -EPERM;
2096ac8e9819SChris Mason 
20972354d08fSJulia Lawall 	args = memdup_user(argp, sizeof(*args));
20982354d08fSJulia Lawall 	if (IS_ERR(args))
20992354d08fSJulia Lawall 		return PTR_ERR(args);
2100c2b96929SDan Carpenter 
2101496ad9aaSAl Viro 	inode = file_inode(file);
2102ac8e9819SChris Mason 
21031b53ac4dSChris Mason 	if (args->treeid == 0)
21041b53ac4dSChris Mason 		args->treeid = BTRFS_I(inode)->root->root_key.objectid;
21051b53ac4dSChris Mason 
2106ac8e9819SChris Mason 	ret = btrfs_search_path_in_tree(BTRFS_I(inode)->root->fs_info,
2107ac8e9819SChris Mason 					args->treeid, args->objectid,
2108ac8e9819SChris Mason 					args->name);
2109ac8e9819SChris Mason 
2110ac8e9819SChris Mason 	if (ret == 0 && copy_to_user(argp, args, sizeof(*args)))
2111ac8e9819SChris Mason 		ret = -EFAULT;
2112ac8e9819SChris Mason 
2113ac8e9819SChris Mason 	kfree(args);
211498d377a0STARUISI Hiroaki 	return ret;
211598d377a0STARUISI Hiroaki }
211698d377a0STARUISI Hiroaki 
211776dda93cSYan, Zheng static noinline int btrfs_ioctl_snap_destroy(struct file *file,
211876dda93cSYan, Zheng 					     void __user *arg)
211976dda93cSYan, Zheng {
212054563d41SAl Viro 	struct dentry *parent = file->f_path.dentry;
212176dda93cSYan, Zheng 	struct dentry *dentry;
212276dda93cSYan, Zheng 	struct inode *dir = parent->d_inode;
212376dda93cSYan, Zheng 	struct inode *inode;
212476dda93cSYan, Zheng 	struct btrfs_root *root = BTRFS_I(dir)->root;
212576dda93cSYan, Zheng 	struct btrfs_root *dest = NULL;
212676dda93cSYan, Zheng 	struct btrfs_ioctl_vol_args *vol_args;
212776dda93cSYan, Zheng 	struct btrfs_trans_handle *trans;
2128c58aaad2SMiao Xie 	struct btrfs_block_rsv block_rsv;
2129c58aaad2SMiao Xie 	u64 qgroup_reserved;
213076dda93cSYan, Zheng 	int namelen;
213176dda93cSYan, Zheng 	int ret;
213276dda93cSYan, Zheng 	int err = 0;
213376dda93cSYan, Zheng 
213476dda93cSYan, Zheng 	vol_args = memdup_user(arg, sizeof(*vol_args));
213576dda93cSYan, Zheng 	if (IS_ERR(vol_args))
213676dda93cSYan, Zheng 		return PTR_ERR(vol_args);
213776dda93cSYan, Zheng 
213876dda93cSYan, Zheng 	vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
213976dda93cSYan, Zheng 	namelen = strlen(vol_args->name);
214076dda93cSYan, Zheng 	if (strchr(vol_args->name, '/') ||
214176dda93cSYan, Zheng 	    strncmp(vol_args->name, "..", namelen) == 0) {
214276dda93cSYan, Zheng 		err = -EINVAL;
214376dda93cSYan, Zheng 		goto out;
214476dda93cSYan, Zheng 	}
214576dda93cSYan, Zheng 
2146a561be71SAl Viro 	err = mnt_want_write_file(file);
214776dda93cSYan, Zheng 	if (err)
214876dda93cSYan, Zheng 		goto out;
214976dda93cSYan, Zheng 
21505c50c9b8SDavid Sterba 	err = mutex_lock_killable_nested(&dir->i_mutex, I_MUTEX_PARENT);
21515c50c9b8SDavid Sterba 	if (err == -EINTR)
2152e43f998eSDavid Sterba 		goto out_drop_write;
215376dda93cSYan, Zheng 	dentry = lookup_one_len(vol_args->name, parent, namelen);
215476dda93cSYan, Zheng 	if (IS_ERR(dentry)) {
215576dda93cSYan, Zheng 		err = PTR_ERR(dentry);
215676dda93cSYan, Zheng 		goto out_unlock_dir;
215776dda93cSYan, Zheng 	}
215876dda93cSYan, Zheng 
215976dda93cSYan, Zheng 	if (!dentry->d_inode) {
216076dda93cSYan, Zheng 		err = -ENOENT;
216176dda93cSYan, Zheng 		goto out_dput;
216276dda93cSYan, Zheng 	}
216376dda93cSYan, Zheng 
216476dda93cSYan, Zheng 	inode = dentry->d_inode;
21654260f7c7SSage Weil 	dest = BTRFS_I(inode)->root;
21664260f7c7SSage Weil 	if (!capable(CAP_SYS_ADMIN)) {
21674260f7c7SSage Weil 		/*
21684260f7c7SSage Weil 		 * Regular user.  Only allow this with a special mount
21694260f7c7SSage Weil 		 * option, when the user has write+exec access to the
21704260f7c7SSage Weil 		 * subvol root, and when rmdir(2) would have been
21714260f7c7SSage Weil 		 * allowed.
21724260f7c7SSage Weil 		 *
21734260f7c7SSage Weil 		 * Note that this is _not_ check that the subvol is
21744260f7c7SSage Weil 		 * empty or doesn't contain data that we wouldn't
21754260f7c7SSage Weil 		 * otherwise be able to delete.
21764260f7c7SSage Weil 		 *
21774260f7c7SSage Weil 		 * Users who want to delete empty subvols should try
21784260f7c7SSage Weil 		 * rmdir(2).
21794260f7c7SSage Weil 		 */
21804260f7c7SSage Weil 		err = -EPERM;
21814260f7c7SSage Weil 		if (!btrfs_test_opt(root, USER_SUBVOL_RM_ALLOWED))
21824260f7c7SSage Weil 			goto out_dput;
21834260f7c7SSage Weil 
21844260f7c7SSage Weil 		/*
21854260f7c7SSage Weil 		 * Do not allow deletion if the parent dir is the same
21864260f7c7SSage Weil 		 * as the dir to be deleted.  That means the ioctl
21874260f7c7SSage Weil 		 * must be called on the dentry referencing the root
21884260f7c7SSage Weil 		 * of the subvol, not a random directory contained
21894260f7c7SSage Weil 		 * within it.
21904260f7c7SSage Weil 		 */
21914260f7c7SSage Weil 		err = -EINVAL;
21924260f7c7SSage Weil 		if (root == dest)
21934260f7c7SSage Weil 			goto out_dput;
21944260f7c7SSage Weil 
21954260f7c7SSage Weil 		err = inode_permission(inode, MAY_WRITE | MAY_EXEC);
21964260f7c7SSage Weil 		if (err)
21974260f7c7SSage Weil 			goto out_dput;
21985c39da5bSMiao Xie 	}
21994260f7c7SSage Weil 
22005c39da5bSMiao Xie 	/* check if subvolume may be deleted by a user */
22014260f7c7SSage Weil 	err = btrfs_may_delete(dir, dentry, 1);
22024260f7c7SSage Weil 	if (err)
22034260f7c7SSage Weil 		goto out_dput;
22044260f7c7SSage Weil 
220533345d01SLi Zefan 	if (btrfs_ino(inode) != BTRFS_FIRST_FREE_OBJECTID) {
220676dda93cSYan, Zheng 		err = -EINVAL;
220776dda93cSYan, Zheng 		goto out_dput;
220876dda93cSYan, Zheng 	}
220976dda93cSYan, Zheng 
221076dda93cSYan, Zheng 	mutex_lock(&inode->i_mutex);
221176dda93cSYan, Zheng 	err = d_invalidate(dentry);
221276dda93cSYan, Zheng 	if (err)
221376dda93cSYan, Zheng 		goto out_unlock;
221476dda93cSYan, Zheng 
221576dda93cSYan, Zheng 	down_write(&root->fs_info->subvol_sem);
221676dda93cSYan, Zheng 
221776dda93cSYan, Zheng 	err = may_destroy_subvol(dest);
221876dda93cSYan, Zheng 	if (err)
221976dda93cSYan, Zheng 		goto out_up_write;
222076dda93cSYan, Zheng 
2221c58aaad2SMiao Xie 	btrfs_init_block_rsv(&block_rsv, BTRFS_BLOCK_RSV_TEMP);
2222c58aaad2SMiao Xie 	/*
2223c58aaad2SMiao Xie 	 * One for dir inode, two for dir entries, two for root
2224c58aaad2SMiao Xie 	 * ref/backref.
2225c58aaad2SMiao Xie 	 */
2226c58aaad2SMiao Xie 	err = btrfs_subvolume_reserve_metadata(root, &block_rsv,
2227ee3441b4SJeff Mahoney 					       5, &qgroup_reserved, true);
2228c58aaad2SMiao Xie 	if (err)
2229c58aaad2SMiao Xie 		goto out_up_write;
2230c58aaad2SMiao Xie 
2231a22285a6SYan, Zheng 	trans = btrfs_start_transaction(root, 0);
2232a22285a6SYan, Zheng 	if (IS_ERR(trans)) {
2233a22285a6SYan, Zheng 		err = PTR_ERR(trans);
2234c58aaad2SMiao Xie 		goto out_release;
2235a22285a6SYan, Zheng 	}
2236c58aaad2SMiao Xie 	trans->block_rsv = &block_rsv;
2237c58aaad2SMiao Xie 	trans->bytes_reserved = block_rsv.size;
2238a22285a6SYan, Zheng 
223976dda93cSYan, Zheng 	ret = btrfs_unlink_subvol(trans, root, dir,
224076dda93cSYan, Zheng 				dest->root_key.objectid,
224176dda93cSYan, Zheng 				dentry->d_name.name,
224276dda93cSYan, Zheng 				dentry->d_name.len);
224379787eaaSJeff Mahoney 	if (ret) {
224479787eaaSJeff Mahoney 		err = ret;
224579787eaaSJeff Mahoney 		btrfs_abort_transaction(trans, root, ret);
224679787eaaSJeff Mahoney 		goto out_end_trans;
224779787eaaSJeff Mahoney 	}
224876dda93cSYan, Zheng 
224976dda93cSYan, Zheng 	btrfs_record_root_in_trans(trans, dest);
225076dda93cSYan, Zheng 
225176dda93cSYan, Zheng 	memset(&dest->root_item.drop_progress, 0,
225276dda93cSYan, Zheng 		sizeof(dest->root_item.drop_progress));
225376dda93cSYan, Zheng 	dest->root_item.drop_level = 0;
225476dda93cSYan, Zheng 	btrfs_set_root_refs(&dest->root_item, 0);
225576dda93cSYan, Zheng 
2256d68fc57bSYan, Zheng 	if (!xchg(&dest->orphan_item_inserted, 1)) {
225776dda93cSYan, Zheng 		ret = btrfs_insert_orphan_item(trans,
225876dda93cSYan, Zheng 					root->fs_info->tree_root,
225976dda93cSYan, Zheng 					dest->root_key.objectid);
226079787eaaSJeff Mahoney 		if (ret) {
226179787eaaSJeff Mahoney 			btrfs_abort_transaction(trans, root, ret);
226279787eaaSJeff Mahoney 			err = ret;
226379787eaaSJeff Mahoney 			goto out_end_trans;
2264d68fc57bSYan, Zheng 		}
226579787eaaSJeff Mahoney 	}
2266dd5f9615SStefan Behrens 
2267dd5f9615SStefan Behrens 	ret = btrfs_uuid_tree_rem(trans, root->fs_info->uuid_root,
2268dd5f9615SStefan Behrens 				  dest->root_item.uuid, BTRFS_UUID_KEY_SUBVOL,
2269dd5f9615SStefan Behrens 				  dest->root_key.objectid);
2270dd5f9615SStefan Behrens 	if (ret && ret != -ENOENT) {
2271dd5f9615SStefan Behrens 		btrfs_abort_transaction(trans, root, ret);
2272dd5f9615SStefan Behrens 		err = ret;
2273dd5f9615SStefan Behrens 		goto out_end_trans;
2274dd5f9615SStefan Behrens 	}
2275dd5f9615SStefan Behrens 	if (!btrfs_is_empty_uuid(dest->root_item.received_uuid)) {
2276dd5f9615SStefan Behrens 		ret = btrfs_uuid_tree_rem(trans, root->fs_info->uuid_root,
2277dd5f9615SStefan Behrens 					  dest->root_item.received_uuid,
2278dd5f9615SStefan Behrens 					  BTRFS_UUID_KEY_RECEIVED_SUBVOL,
2279dd5f9615SStefan Behrens 					  dest->root_key.objectid);
2280dd5f9615SStefan Behrens 		if (ret && ret != -ENOENT) {
2281dd5f9615SStefan Behrens 			btrfs_abort_transaction(trans, root, ret);
2282dd5f9615SStefan Behrens 			err = ret;
2283dd5f9615SStefan Behrens 			goto out_end_trans;
2284dd5f9615SStefan Behrens 		}
2285dd5f9615SStefan Behrens 	}
2286dd5f9615SStefan Behrens 
228779787eaaSJeff Mahoney out_end_trans:
2288c58aaad2SMiao Xie 	trans->block_rsv = NULL;
2289c58aaad2SMiao Xie 	trans->bytes_reserved = 0;
2290531cb13fSSage Weil 	ret = btrfs_end_transaction(trans, root);
229179787eaaSJeff Mahoney 	if (ret && !err)
229279787eaaSJeff Mahoney 		err = ret;
229376dda93cSYan, Zheng 	inode->i_flags |= S_DEAD;
2294c58aaad2SMiao Xie out_release:
2295c58aaad2SMiao Xie 	btrfs_subvolume_release_metadata(root, &block_rsv, qgroup_reserved);
229676dda93cSYan, Zheng out_up_write:
229776dda93cSYan, Zheng 	up_write(&root->fs_info->subvol_sem);
229876dda93cSYan, Zheng out_unlock:
229976dda93cSYan, Zheng 	mutex_unlock(&inode->i_mutex);
230076dda93cSYan, Zheng 	if (!err) {
2301efefb143SYan, Zheng 		shrink_dcache_sb(root->fs_info->sb);
230276dda93cSYan, Zheng 		btrfs_invalidate_inodes(dest);
230376dda93cSYan, Zheng 		d_delete(dentry);
2304fa6ac876SLiu Bo 
2305fa6ac876SLiu Bo 		/* the last ref */
2306fa6ac876SLiu Bo 		if (dest->cache_inode) {
2307fa6ac876SLiu Bo 			iput(dest->cache_inode);
2308fa6ac876SLiu Bo 			dest->cache_inode = NULL;
2309fa6ac876SLiu Bo 		}
231076dda93cSYan, Zheng 	}
231176dda93cSYan, Zheng out_dput:
231276dda93cSYan, Zheng 	dput(dentry);
231376dda93cSYan, Zheng out_unlock_dir:
231476dda93cSYan, Zheng 	mutex_unlock(&dir->i_mutex);
2315e43f998eSDavid Sterba out_drop_write:
23162a79f17eSAl Viro 	mnt_drop_write_file(file);
231776dda93cSYan, Zheng out:
231876dda93cSYan, Zheng 	kfree(vol_args);
231976dda93cSYan, Zheng 	return err;
232076dda93cSYan, Zheng }
232176dda93cSYan, Zheng 
23221e701a32SChris Mason static int btrfs_ioctl_defrag(struct file *file, void __user *argp)
2323f46b5a66SChristoph Hellwig {
2324496ad9aaSAl Viro 	struct inode *inode = file_inode(file);
2325f46b5a66SChristoph Hellwig 	struct btrfs_root *root = BTRFS_I(inode)->root;
23261e701a32SChris Mason 	struct btrfs_ioctl_defrag_range_args *range;
2327c146afadSYan Zheng 	int ret;
2328c146afadSYan Zheng 
232925122d15SIlya Dryomov 	ret = mnt_want_write_file(file);
233025122d15SIlya Dryomov 	if (ret)
233125122d15SIlya Dryomov 		return ret;
2332b83cc969SLi Zefan 
233325122d15SIlya Dryomov 	if (btrfs_root_readonly(root)) {
233425122d15SIlya Dryomov 		ret = -EROFS;
233525122d15SIlya Dryomov 		goto out;
23365ac00addSStefan Behrens 	}
2337f46b5a66SChristoph Hellwig 
2338f46b5a66SChristoph Hellwig 	switch (inode->i_mode & S_IFMT) {
2339f46b5a66SChristoph Hellwig 	case S_IFDIR:
2340e441d54dSChris Mason 		if (!capable(CAP_SYS_ADMIN)) {
2341e441d54dSChris Mason 			ret = -EPERM;
2342e441d54dSChris Mason 			goto out;
2343e441d54dSChris Mason 		}
2344de78b51aSEric Sandeen 		ret = btrfs_defrag_root(root);
23458929ecfaSYan, Zheng 		if (ret)
23468929ecfaSYan, Zheng 			goto out;
2347de78b51aSEric Sandeen 		ret = btrfs_defrag_root(root->fs_info->extent_root);
2348f46b5a66SChristoph Hellwig 		break;
2349f46b5a66SChristoph Hellwig 	case S_IFREG:
2350e441d54dSChris Mason 		if (!(file->f_mode & FMODE_WRITE)) {
2351e441d54dSChris Mason 			ret = -EINVAL;
2352e441d54dSChris Mason 			goto out;
2353e441d54dSChris Mason 		}
23541e701a32SChris Mason 
23551e701a32SChris Mason 		range = kzalloc(sizeof(*range), GFP_KERNEL);
23561e701a32SChris Mason 		if (!range) {
23571e701a32SChris Mason 			ret = -ENOMEM;
23581e701a32SChris Mason 			goto out;
23591e701a32SChris Mason 		}
23601e701a32SChris Mason 
23611e701a32SChris Mason 		if (argp) {
23621e701a32SChris Mason 			if (copy_from_user(range, argp,
23631e701a32SChris Mason 					   sizeof(*range))) {
23641e701a32SChris Mason 				ret = -EFAULT;
23651e701a32SChris Mason 				kfree(range);
2366683be16eSDan Carpenter 				goto out;
23671e701a32SChris Mason 			}
23681e701a32SChris Mason 			/* compression requires us to start the IO */
23691e701a32SChris Mason 			if ((range->flags & BTRFS_DEFRAG_RANGE_COMPRESS)) {
23701e701a32SChris Mason 				range->flags |= BTRFS_DEFRAG_RANGE_START_IO;
23711e701a32SChris Mason 				range->extent_thresh = (u32)-1;
23721e701a32SChris Mason 			}
23731e701a32SChris Mason 		} else {
23741e701a32SChris Mason 			/* the rest are all set to zero by kzalloc */
23751e701a32SChris Mason 			range->len = (u64)-1;
23761e701a32SChris Mason 		}
2377496ad9aaSAl Viro 		ret = btrfs_defrag_file(file_inode(file), file,
23784cb5300bSChris Mason 					range, 0, 0);
23794cb5300bSChris Mason 		if (ret > 0)
23804cb5300bSChris Mason 			ret = 0;
23811e701a32SChris Mason 		kfree(range);
2382f46b5a66SChristoph Hellwig 		break;
23838929ecfaSYan, Zheng 	default:
23848929ecfaSYan, Zheng 		ret = -EINVAL;
2385f46b5a66SChristoph Hellwig 	}
2386e441d54dSChris Mason out:
238725122d15SIlya Dryomov 	mnt_drop_write_file(file);
2388e441d54dSChris Mason 	return ret;
2389f46b5a66SChristoph Hellwig }
2390f46b5a66SChristoph Hellwig 
2391b2950863SChristoph Hellwig static long btrfs_ioctl_add_dev(struct btrfs_root *root, void __user *arg)
2392f46b5a66SChristoph Hellwig {
2393f46b5a66SChristoph Hellwig 	struct btrfs_ioctl_vol_args *vol_args;
2394f46b5a66SChristoph Hellwig 	int ret;
2395f46b5a66SChristoph Hellwig 
2396e441d54dSChris Mason 	if (!capable(CAP_SYS_ADMIN))
2397e441d54dSChris Mason 		return -EPERM;
2398e441d54dSChris Mason 
23995ac00addSStefan Behrens 	if (atomic_xchg(&root->fs_info->mutually_exclusive_operation_running,
24005ac00addSStefan Behrens 			1)) {
2401e57138b3SAnand Jain 		return BTRFS_ERROR_DEV_EXCL_RUN_IN_PROGRESS;
2402c9e9f97bSIlya Dryomov 	}
2403c9e9f97bSIlya Dryomov 
24045ac00addSStefan Behrens 	mutex_lock(&root->fs_info->volume_mutex);
2405dae7b665SLi Zefan 	vol_args = memdup_user(arg, sizeof(*vol_args));
2406c9e9f97bSIlya Dryomov 	if (IS_ERR(vol_args)) {
2407c9e9f97bSIlya Dryomov 		ret = PTR_ERR(vol_args);
2408c9e9f97bSIlya Dryomov 		goto out;
2409c9e9f97bSIlya Dryomov 	}
2410f46b5a66SChristoph Hellwig 
24115516e595SMark Fasheh 	vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
2412f46b5a66SChristoph Hellwig 	ret = btrfs_init_new_device(root, vol_args->name);
2413f46b5a66SChristoph Hellwig 
2414f46b5a66SChristoph Hellwig 	kfree(vol_args);
2415c9e9f97bSIlya Dryomov out:
2416c9e9f97bSIlya Dryomov 	mutex_unlock(&root->fs_info->volume_mutex);
24175ac00addSStefan Behrens 	atomic_set(&root->fs_info->mutually_exclusive_operation_running, 0);
2418f46b5a66SChristoph Hellwig 	return ret;
2419f46b5a66SChristoph Hellwig }
2420f46b5a66SChristoph Hellwig 
2421da24927bSMiao Xie static long btrfs_ioctl_rm_dev(struct file *file, void __user *arg)
2422f46b5a66SChristoph Hellwig {
2423496ad9aaSAl Viro 	struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
2424f46b5a66SChristoph Hellwig 	struct btrfs_ioctl_vol_args *vol_args;
2425f46b5a66SChristoph Hellwig 	int ret;
2426f46b5a66SChristoph Hellwig 
2427e441d54dSChris Mason 	if (!capable(CAP_SYS_ADMIN))
2428e441d54dSChris Mason 		return -EPERM;
2429e441d54dSChris Mason 
2430da24927bSMiao Xie 	ret = mnt_want_write_file(file);
2431da24927bSMiao Xie 	if (ret)
2432da24927bSMiao Xie 		return ret;
2433c146afadSYan Zheng 
2434dae7b665SLi Zefan 	vol_args = memdup_user(arg, sizeof(*vol_args));
2435c9e9f97bSIlya Dryomov 	if (IS_ERR(vol_args)) {
2436c9e9f97bSIlya Dryomov 		ret = PTR_ERR(vol_args);
2437c9e9f97bSIlya Dryomov 		goto out;
2438c9e9f97bSIlya Dryomov 	}
2439f46b5a66SChristoph Hellwig 
24405516e595SMark Fasheh 	vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
2441f46b5a66SChristoph Hellwig 
2442183860f6SAnand Jain 	if (atomic_xchg(&root->fs_info->mutually_exclusive_operation_running,
2443183860f6SAnand Jain 			1)) {
2444183860f6SAnand Jain 		ret = BTRFS_ERROR_DEV_EXCL_RUN_IN_PROGRESS;
2445183860f6SAnand Jain 		goto out;
2446183860f6SAnand Jain 	}
2447183860f6SAnand Jain 
2448183860f6SAnand Jain 	mutex_lock(&root->fs_info->volume_mutex);
2449183860f6SAnand Jain 	ret = btrfs_rm_device(root, vol_args->name);
2450c9e9f97bSIlya Dryomov 	mutex_unlock(&root->fs_info->volume_mutex);
24515ac00addSStefan Behrens 	atomic_set(&root->fs_info->mutually_exclusive_operation_running, 0);
2452183860f6SAnand Jain 
2453183860f6SAnand Jain out:
2454183860f6SAnand Jain 	kfree(vol_args);
24554ac20c70SIlya Dryomov 	mnt_drop_write_file(file);
2456f46b5a66SChristoph Hellwig 	return ret;
2457f46b5a66SChristoph Hellwig }
2458f46b5a66SChristoph Hellwig 
2459475f6387SJan Schmidt static long btrfs_ioctl_fs_info(struct btrfs_root *root, void __user *arg)
2460475f6387SJan Schmidt {
2461027ed2f0SLi Zefan 	struct btrfs_ioctl_fs_info_args *fi_args;
2462475f6387SJan Schmidt 	struct btrfs_device *device;
2463475f6387SJan Schmidt 	struct btrfs_device *next;
2464475f6387SJan Schmidt 	struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices;
2465027ed2f0SLi Zefan 	int ret = 0;
2466475f6387SJan Schmidt 
2467475f6387SJan Schmidt 	if (!capable(CAP_SYS_ADMIN))
2468475f6387SJan Schmidt 		return -EPERM;
2469475f6387SJan Schmidt 
2470027ed2f0SLi Zefan 	fi_args = kzalloc(sizeof(*fi_args), GFP_KERNEL);
2471027ed2f0SLi Zefan 	if (!fi_args)
2472027ed2f0SLi Zefan 		return -ENOMEM;
2473027ed2f0SLi Zefan 
2474f7171750SFilipe David Borba Manana 	mutex_lock(&fs_devices->device_list_mutex);
2475027ed2f0SLi Zefan 	fi_args->num_devices = fs_devices->num_devices;
2476027ed2f0SLi Zefan 	memcpy(&fi_args->fsid, root->fs_info->fsid, sizeof(fi_args->fsid));
2477475f6387SJan Schmidt 
2478475f6387SJan Schmidt 	list_for_each_entry_safe(device, next, &fs_devices->devices, dev_list) {
2479027ed2f0SLi Zefan 		if (device->devid > fi_args->max_id)
2480027ed2f0SLi Zefan 			fi_args->max_id = device->devid;
2481475f6387SJan Schmidt 	}
2482475f6387SJan Schmidt 	mutex_unlock(&fs_devices->device_list_mutex);
2483475f6387SJan Schmidt 
2484027ed2f0SLi Zefan 	if (copy_to_user(arg, fi_args, sizeof(*fi_args)))
2485027ed2f0SLi Zefan 		ret = -EFAULT;
2486475f6387SJan Schmidt 
2487027ed2f0SLi Zefan 	kfree(fi_args);
2488027ed2f0SLi Zefan 	return ret;
2489475f6387SJan Schmidt }
2490475f6387SJan Schmidt 
2491475f6387SJan Schmidt static long btrfs_ioctl_dev_info(struct btrfs_root *root, void __user *arg)
2492475f6387SJan Schmidt {
2493475f6387SJan Schmidt 	struct btrfs_ioctl_dev_info_args *di_args;
2494475f6387SJan Schmidt 	struct btrfs_device *dev;
2495475f6387SJan Schmidt 	struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices;
2496475f6387SJan Schmidt 	int ret = 0;
2497475f6387SJan Schmidt 	char *s_uuid = NULL;
2498475f6387SJan Schmidt 
2499475f6387SJan Schmidt 	if (!capable(CAP_SYS_ADMIN))
2500475f6387SJan Schmidt 		return -EPERM;
2501475f6387SJan Schmidt 
2502475f6387SJan Schmidt 	di_args = memdup_user(arg, sizeof(*di_args));
2503475f6387SJan Schmidt 	if (IS_ERR(di_args))
2504475f6387SJan Schmidt 		return PTR_ERR(di_args);
2505475f6387SJan Schmidt 
2506dd5f9615SStefan Behrens 	if (!btrfs_is_empty_uuid(di_args->uuid))
2507475f6387SJan Schmidt 		s_uuid = di_args->uuid;
2508475f6387SJan Schmidt 
2509475f6387SJan Schmidt 	mutex_lock(&fs_devices->device_list_mutex);
2510aa1b8cd4SStefan Behrens 	dev = btrfs_find_device(root->fs_info, di_args->devid, s_uuid, NULL);
2511475f6387SJan Schmidt 
2512475f6387SJan Schmidt 	if (!dev) {
2513475f6387SJan Schmidt 		ret = -ENODEV;
2514475f6387SJan Schmidt 		goto out;
2515475f6387SJan Schmidt 	}
2516475f6387SJan Schmidt 
2517475f6387SJan Schmidt 	di_args->devid = dev->devid;
2518475f6387SJan Schmidt 	di_args->bytes_used = dev->bytes_used;
2519475f6387SJan Schmidt 	di_args->total_bytes = dev->total_bytes;
2520475f6387SJan Schmidt 	memcpy(di_args->uuid, dev->uuid, sizeof(di_args->uuid));
2521a27202fbSJim Meyering 	if (dev->name) {
2522606686eeSJosef Bacik 		struct rcu_string *name;
2523606686eeSJosef Bacik 
2524606686eeSJosef Bacik 		rcu_read_lock();
2525606686eeSJosef Bacik 		name = rcu_dereference(dev->name);
2526606686eeSJosef Bacik 		strncpy(di_args->path, name->str, sizeof(di_args->path));
2527606686eeSJosef Bacik 		rcu_read_unlock();
2528a27202fbSJim Meyering 		di_args->path[sizeof(di_args->path) - 1] = 0;
2529a27202fbSJim Meyering 	} else {
253099ba55adSStefan Behrens 		di_args->path[0] = '\0';
2531a27202fbSJim Meyering 	}
2532475f6387SJan Schmidt 
2533475f6387SJan Schmidt out:
253455793c0dSDavid Sterba 	mutex_unlock(&fs_devices->device_list_mutex);
2535475f6387SJan Schmidt 	if (ret == 0 && copy_to_user(arg, di_args, sizeof(*di_args)))
2536475f6387SJan Schmidt 		ret = -EFAULT;
2537475f6387SJan Schmidt 
2538475f6387SJan Schmidt 	kfree(di_args);
2539475f6387SJan Schmidt 	return ret;
2540475f6387SJan Schmidt }
2541475f6387SJan Schmidt 
2542416161dbSMark Fasheh static struct page *extent_same_get_page(struct inode *inode, u64 off)
2543416161dbSMark Fasheh {
2544416161dbSMark Fasheh 	struct page *page;
2545416161dbSMark Fasheh 	pgoff_t index;
2546416161dbSMark Fasheh 	struct extent_io_tree *tree = &BTRFS_I(inode)->io_tree;
2547416161dbSMark Fasheh 
2548416161dbSMark Fasheh 	index = off >> PAGE_CACHE_SHIFT;
2549416161dbSMark Fasheh 
2550416161dbSMark Fasheh 	page = grab_cache_page(inode->i_mapping, index);
2551416161dbSMark Fasheh 	if (!page)
2552416161dbSMark Fasheh 		return NULL;
2553416161dbSMark Fasheh 
2554416161dbSMark Fasheh 	if (!PageUptodate(page)) {
2555416161dbSMark Fasheh 		if (extent_read_full_page_nolock(tree, page, btrfs_get_extent,
2556416161dbSMark Fasheh 						 0))
2557416161dbSMark Fasheh 			return NULL;
2558416161dbSMark Fasheh 		lock_page(page);
2559416161dbSMark Fasheh 		if (!PageUptodate(page)) {
2560416161dbSMark Fasheh 			unlock_page(page);
2561416161dbSMark Fasheh 			page_cache_release(page);
2562416161dbSMark Fasheh 			return NULL;
2563416161dbSMark Fasheh 		}
2564416161dbSMark Fasheh 	}
2565416161dbSMark Fasheh 	unlock_page(page);
2566416161dbSMark Fasheh 
2567416161dbSMark Fasheh 	return page;
2568416161dbSMark Fasheh }
2569416161dbSMark Fasheh 
257077fe20dcSMark Fasheh static inline void lock_extent_range(struct inode *inode, u64 off, u64 len)
257177fe20dcSMark Fasheh {
257277fe20dcSMark Fasheh 	/* do any pending delalloc/csum calc on src, one way or
257377fe20dcSMark Fasheh 	   another, and lock file content */
257477fe20dcSMark Fasheh 	while (1) {
257577fe20dcSMark Fasheh 		struct btrfs_ordered_extent *ordered;
257677fe20dcSMark Fasheh 		lock_extent(&BTRFS_I(inode)->io_tree, off, off + len - 1);
257777fe20dcSMark Fasheh 		ordered = btrfs_lookup_first_ordered_extent(inode,
257877fe20dcSMark Fasheh 							    off + len - 1);
257977fe20dcSMark Fasheh 		if (!ordered &&
258077fe20dcSMark Fasheh 		    !test_range_bit(&BTRFS_I(inode)->io_tree, off,
258177fe20dcSMark Fasheh 				    off + len - 1, EXTENT_DELALLOC, 0, NULL))
258277fe20dcSMark Fasheh 			break;
258377fe20dcSMark Fasheh 		unlock_extent(&BTRFS_I(inode)->io_tree, off, off + len - 1);
258477fe20dcSMark Fasheh 		if (ordered)
258577fe20dcSMark Fasheh 			btrfs_put_ordered_extent(ordered);
258677fe20dcSMark Fasheh 		btrfs_wait_ordered_range(inode, off, len);
258777fe20dcSMark Fasheh 	}
258877fe20dcSMark Fasheh }
258977fe20dcSMark Fasheh 
2590416161dbSMark Fasheh static void btrfs_double_unlock(struct inode *inode1, u64 loff1,
2591416161dbSMark Fasheh 				struct inode *inode2, u64 loff2, u64 len)
2592416161dbSMark Fasheh {
2593416161dbSMark Fasheh 	unlock_extent(&BTRFS_I(inode1)->io_tree, loff1, loff1 + len - 1);
2594416161dbSMark Fasheh 	unlock_extent(&BTRFS_I(inode2)->io_tree, loff2, loff2 + len - 1);
2595416161dbSMark Fasheh 
2596416161dbSMark Fasheh 	mutex_unlock(&inode1->i_mutex);
2597416161dbSMark Fasheh 	mutex_unlock(&inode2->i_mutex);
2598416161dbSMark Fasheh }
2599416161dbSMark Fasheh 
2600416161dbSMark Fasheh static void btrfs_double_lock(struct inode *inode1, u64 loff1,
2601416161dbSMark Fasheh 			      struct inode *inode2, u64 loff2, u64 len)
2602416161dbSMark Fasheh {
2603416161dbSMark Fasheh 	if (inode1 < inode2) {
2604416161dbSMark Fasheh 		swap(inode1, inode2);
2605416161dbSMark Fasheh 		swap(loff1, loff2);
2606416161dbSMark Fasheh 	}
2607416161dbSMark Fasheh 
2608416161dbSMark Fasheh 	mutex_lock_nested(&inode1->i_mutex, I_MUTEX_PARENT);
2609416161dbSMark Fasheh 	lock_extent_range(inode1, loff1, len);
2610416161dbSMark Fasheh 	if (inode1 != inode2) {
2611416161dbSMark Fasheh 		mutex_lock_nested(&inode2->i_mutex, I_MUTEX_CHILD);
2612416161dbSMark Fasheh 		lock_extent_range(inode2, loff2, len);
2613416161dbSMark Fasheh 	}
2614416161dbSMark Fasheh }
2615416161dbSMark Fasheh 
2616416161dbSMark Fasheh static int btrfs_cmp_data(struct inode *src, u64 loff, struct inode *dst,
2617416161dbSMark Fasheh 			  u64 dst_loff, u64 len)
2618416161dbSMark Fasheh {
2619416161dbSMark Fasheh 	int ret = 0;
2620416161dbSMark Fasheh 	struct page *src_page, *dst_page;
2621416161dbSMark Fasheh 	unsigned int cmp_len = PAGE_CACHE_SIZE;
2622416161dbSMark Fasheh 	void *addr, *dst_addr;
2623416161dbSMark Fasheh 
2624416161dbSMark Fasheh 	while (len) {
2625416161dbSMark Fasheh 		if (len < PAGE_CACHE_SIZE)
2626416161dbSMark Fasheh 			cmp_len = len;
2627416161dbSMark Fasheh 
2628416161dbSMark Fasheh 		src_page = extent_same_get_page(src, loff);
2629416161dbSMark Fasheh 		if (!src_page)
2630416161dbSMark Fasheh 			return -EINVAL;
2631416161dbSMark Fasheh 		dst_page = extent_same_get_page(dst, dst_loff);
2632416161dbSMark Fasheh 		if (!dst_page) {
2633416161dbSMark Fasheh 			page_cache_release(src_page);
2634416161dbSMark Fasheh 			return -EINVAL;
2635416161dbSMark Fasheh 		}
2636416161dbSMark Fasheh 		addr = kmap_atomic(src_page);
2637416161dbSMark Fasheh 		dst_addr = kmap_atomic(dst_page);
2638416161dbSMark Fasheh 
2639416161dbSMark Fasheh 		flush_dcache_page(src_page);
2640416161dbSMark Fasheh 		flush_dcache_page(dst_page);
2641416161dbSMark Fasheh 
2642416161dbSMark Fasheh 		if (memcmp(addr, dst_addr, cmp_len))
2643416161dbSMark Fasheh 			ret = BTRFS_SAME_DATA_DIFFERS;
2644416161dbSMark Fasheh 
2645416161dbSMark Fasheh 		kunmap_atomic(addr);
2646416161dbSMark Fasheh 		kunmap_atomic(dst_addr);
2647416161dbSMark Fasheh 		page_cache_release(src_page);
2648416161dbSMark Fasheh 		page_cache_release(dst_page);
2649416161dbSMark Fasheh 
2650416161dbSMark Fasheh 		if (ret)
2651416161dbSMark Fasheh 			break;
2652416161dbSMark Fasheh 
2653416161dbSMark Fasheh 		loff += cmp_len;
2654416161dbSMark Fasheh 		dst_loff += cmp_len;
2655416161dbSMark Fasheh 		len -= cmp_len;
2656416161dbSMark Fasheh 	}
2657416161dbSMark Fasheh 
2658416161dbSMark Fasheh 	return ret;
2659416161dbSMark Fasheh }
2660416161dbSMark Fasheh 
2661416161dbSMark Fasheh static int extent_same_check_offsets(struct inode *inode, u64 off, u64 len)
2662416161dbSMark Fasheh {
2663416161dbSMark Fasheh 	u64 bs = BTRFS_I(inode)->root->fs_info->sb->s_blocksize;
2664416161dbSMark Fasheh 
2665416161dbSMark Fasheh 	if (off + len > inode->i_size || off + len < off)
2666416161dbSMark Fasheh 		return -EINVAL;
2667416161dbSMark Fasheh 	/* Check that we are block aligned - btrfs_clone() requires this */
2668416161dbSMark Fasheh 	if (!IS_ALIGNED(off, bs) || !IS_ALIGNED(off + len, bs))
2669416161dbSMark Fasheh 		return -EINVAL;
2670416161dbSMark Fasheh 
2671416161dbSMark Fasheh 	return 0;
2672416161dbSMark Fasheh }
2673416161dbSMark Fasheh 
2674416161dbSMark Fasheh static int btrfs_extent_same(struct inode *src, u64 loff, u64 len,
2675416161dbSMark Fasheh 			     struct inode *dst, u64 dst_loff)
2676416161dbSMark Fasheh {
2677416161dbSMark Fasheh 	int ret;
2678416161dbSMark Fasheh 
2679416161dbSMark Fasheh 	/*
2680416161dbSMark Fasheh 	 * btrfs_clone() can't handle extents in the same file
2681416161dbSMark Fasheh 	 * yet. Once that works, we can drop this check and replace it
2682416161dbSMark Fasheh 	 * with a check for the same inode, but overlapping extents.
2683416161dbSMark Fasheh 	 */
2684416161dbSMark Fasheh 	if (src == dst)
2685416161dbSMark Fasheh 		return -EINVAL;
2686416161dbSMark Fasheh 
2687416161dbSMark Fasheh 	btrfs_double_lock(src, loff, dst, dst_loff, len);
2688416161dbSMark Fasheh 
2689416161dbSMark Fasheh 	ret = extent_same_check_offsets(src, loff, len);
2690416161dbSMark Fasheh 	if (ret)
2691416161dbSMark Fasheh 		goto out_unlock;
2692416161dbSMark Fasheh 
2693416161dbSMark Fasheh 	ret = extent_same_check_offsets(dst, dst_loff, len);
2694416161dbSMark Fasheh 	if (ret)
2695416161dbSMark Fasheh 		goto out_unlock;
2696416161dbSMark Fasheh 
2697416161dbSMark Fasheh 	/* don't make the dst file partly checksummed */
2698416161dbSMark Fasheh 	if ((BTRFS_I(src)->flags & BTRFS_INODE_NODATASUM) !=
2699416161dbSMark Fasheh 	    (BTRFS_I(dst)->flags & BTRFS_INODE_NODATASUM)) {
2700416161dbSMark Fasheh 		ret = -EINVAL;
2701416161dbSMark Fasheh 		goto out_unlock;
2702416161dbSMark Fasheh 	}
2703416161dbSMark Fasheh 
2704416161dbSMark Fasheh 	ret = btrfs_cmp_data(src, loff, dst, dst_loff, len);
2705416161dbSMark Fasheh 	if (ret == 0)
2706416161dbSMark Fasheh 		ret = btrfs_clone(src, dst, loff, len, len, dst_loff);
2707416161dbSMark Fasheh 
2708416161dbSMark Fasheh out_unlock:
2709416161dbSMark Fasheh 	btrfs_double_unlock(src, loff, dst, dst_loff, len);
2710416161dbSMark Fasheh 
2711416161dbSMark Fasheh 	return ret;
2712416161dbSMark Fasheh }
2713416161dbSMark Fasheh 
2714416161dbSMark Fasheh #define BTRFS_MAX_DEDUPE_LEN	(16 * 1024 * 1024)
2715416161dbSMark Fasheh 
2716416161dbSMark Fasheh static long btrfs_ioctl_file_extent_same(struct file *file,
2717416161dbSMark Fasheh 					 void __user *argp)
2718416161dbSMark Fasheh {
2719cbf8b8caSMark Fasheh 	struct btrfs_ioctl_same_args tmp;
2720cbf8b8caSMark Fasheh 	struct btrfs_ioctl_same_args *same;
2721cbf8b8caSMark Fasheh 	struct btrfs_ioctl_same_extent_info *info;
2722416161dbSMark Fasheh 	struct inode *src = file->f_dentry->d_inode;
2723416161dbSMark Fasheh 	struct file *dst_file = NULL;
2724416161dbSMark Fasheh 	struct inode *dst;
2725416161dbSMark Fasheh 	u64 off;
2726416161dbSMark Fasheh 	u64 len;
2727416161dbSMark Fasheh 	int i;
2728416161dbSMark Fasheh 	int ret;
2729cbf8b8caSMark Fasheh 	unsigned long size;
2730416161dbSMark Fasheh 	u64 bs = BTRFS_I(src)->root->fs_info->sb->s_blocksize;
2731416161dbSMark Fasheh 	bool is_admin = capable(CAP_SYS_ADMIN);
2732416161dbSMark Fasheh 
2733416161dbSMark Fasheh 	if (!(file->f_mode & FMODE_READ))
2734416161dbSMark Fasheh 		return -EINVAL;
2735416161dbSMark Fasheh 
2736416161dbSMark Fasheh 	ret = mnt_want_write_file(file);
2737416161dbSMark Fasheh 	if (ret)
2738416161dbSMark Fasheh 		return ret;
2739416161dbSMark Fasheh 
2740cbf8b8caSMark Fasheh 	if (copy_from_user(&tmp,
2741416161dbSMark Fasheh 			   (struct btrfs_ioctl_same_args __user *)argp,
2742cbf8b8caSMark Fasheh 			   sizeof(tmp))) {
2743416161dbSMark Fasheh 		ret = -EFAULT;
2744416161dbSMark Fasheh 		goto out;
2745416161dbSMark Fasheh 	}
2746416161dbSMark Fasheh 
2747cbf8b8caSMark Fasheh 	size = sizeof(tmp) +
2748cbf8b8caSMark Fasheh 		tmp.dest_count * sizeof(struct btrfs_ioctl_same_extent_info);
2749cbf8b8caSMark Fasheh 
2750229eed43SGeyslan G. Bem 	same = memdup_user((struct btrfs_ioctl_same_args __user *)argp, size);
2751cbf8b8caSMark Fasheh 
2752229eed43SGeyslan G. Bem 	if (IS_ERR(same)) {
2753229eed43SGeyslan G. Bem 		ret = PTR_ERR(same);
2754cbf8b8caSMark Fasheh 		goto out;
2755cbf8b8caSMark Fasheh 	}
2756cbf8b8caSMark Fasheh 
2757cbf8b8caSMark Fasheh 	off = same->logical_offset;
2758cbf8b8caSMark Fasheh 	len = same->length;
2759416161dbSMark Fasheh 
2760416161dbSMark Fasheh 	/*
2761416161dbSMark Fasheh 	 * Limit the total length we will dedupe for each operation.
2762416161dbSMark Fasheh 	 * This is intended to bound the total time spent in this
2763416161dbSMark Fasheh 	 * ioctl to something sane.
2764416161dbSMark Fasheh 	 */
2765416161dbSMark Fasheh 	if (len > BTRFS_MAX_DEDUPE_LEN)
2766416161dbSMark Fasheh 		len = BTRFS_MAX_DEDUPE_LEN;
2767416161dbSMark Fasheh 
2768416161dbSMark Fasheh 	if (WARN_ON_ONCE(bs < PAGE_CACHE_SIZE)) {
2769416161dbSMark Fasheh 		/*
2770416161dbSMark Fasheh 		 * Btrfs does not support blocksize < page_size. As a
2771416161dbSMark Fasheh 		 * result, btrfs_cmp_data() won't correctly handle
2772416161dbSMark Fasheh 		 * this situation without an update.
2773416161dbSMark Fasheh 		 */
2774416161dbSMark Fasheh 		ret = -EINVAL;
2775416161dbSMark Fasheh 		goto out;
2776416161dbSMark Fasheh 	}
2777416161dbSMark Fasheh 
2778416161dbSMark Fasheh 	ret = -EISDIR;
2779416161dbSMark Fasheh 	if (S_ISDIR(src->i_mode))
2780416161dbSMark Fasheh 		goto out;
2781416161dbSMark Fasheh 
2782416161dbSMark Fasheh 	ret = -EACCES;
2783416161dbSMark Fasheh 	if (!S_ISREG(src->i_mode))
2784416161dbSMark Fasheh 		goto out;
2785416161dbSMark Fasheh 
2786cbf8b8caSMark Fasheh 	/* pre-format output fields to sane values */
2787cbf8b8caSMark Fasheh 	for (i = 0; i < same->dest_count; i++) {
2788cbf8b8caSMark Fasheh 		same->info[i].bytes_deduped = 0ULL;
2789cbf8b8caSMark Fasheh 		same->info[i].status = 0;
2790416161dbSMark Fasheh 	}
2791416161dbSMark Fasheh 
2792cbf8b8caSMark Fasheh 	ret = 0;
2793cbf8b8caSMark Fasheh 	for (i = 0; i < same->dest_count; i++) {
2794cbf8b8caSMark Fasheh 		info = &same->info[i];
2795416161dbSMark Fasheh 
2796cbf8b8caSMark Fasheh 		dst_file = fget(info->fd);
2797416161dbSMark Fasheh 		if (!dst_file) {
2798cbf8b8caSMark Fasheh 			info->status = -EBADF;
2799416161dbSMark Fasheh 			goto next;
2800416161dbSMark Fasheh 		}
2801416161dbSMark Fasheh 
2802416161dbSMark Fasheh 		if (!(is_admin || (dst_file->f_mode & FMODE_WRITE))) {
2803cbf8b8caSMark Fasheh 			info->status = -EINVAL;
2804416161dbSMark Fasheh 			goto next;
2805416161dbSMark Fasheh 		}
2806416161dbSMark Fasheh 
2807cbf8b8caSMark Fasheh 		info->status = -EXDEV;
2808416161dbSMark Fasheh 		if (file->f_path.mnt != dst_file->f_path.mnt)
2809416161dbSMark Fasheh 			goto next;
2810416161dbSMark Fasheh 
2811416161dbSMark Fasheh 		dst = dst_file->f_dentry->d_inode;
2812416161dbSMark Fasheh 		if (src->i_sb != dst->i_sb)
2813416161dbSMark Fasheh 			goto next;
2814416161dbSMark Fasheh 
2815416161dbSMark Fasheh 		if (S_ISDIR(dst->i_mode)) {
2816cbf8b8caSMark Fasheh 			info->status = -EISDIR;
2817416161dbSMark Fasheh 			goto next;
2818416161dbSMark Fasheh 		}
2819416161dbSMark Fasheh 
2820416161dbSMark Fasheh 		if (!S_ISREG(dst->i_mode)) {
2821cbf8b8caSMark Fasheh 			info->status = -EACCES;
2822416161dbSMark Fasheh 			goto next;
2823416161dbSMark Fasheh 		}
2824416161dbSMark Fasheh 
2825cbf8b8caSMark Fasheh 		info->status = btrfs_extent_same(src, off, len, dst,
2826cbf8b8caSMark Fasheh 						info->logical_offset);
2827cbf8b8caSMark Fasheh 		if (info->status == 0)
2828cbf8b8caSMark Fasheh 			info->bytes_deduped += len;
2829416161dbSMark Fasheh 
2830416161dbSMark Fasheh next:
2831416161dbSMark Fasheh 		if (dst_file)
2832416161dbSMark Fasheh 			fput(dst_file);
2833cbf8b8caSMark Fasheh 	}
2834416161dbSMark Fasheh 
2835cbf8b8caSMark Fasheh 	ret = copy_to_user(argp, same, size);
2836cbf8b8caSMark Fasheh 	if (ret)
2837416161dbSMark Fasheh 		ret = -EFAULT;
2838416161dbSMark Fasheh 
2839416161dbSMark Fasheh out:
2840416161dbSMark Fasheh 	mnt_drop_write_file(file);
2841416161dbSMark Fasheh 	return ret;
2842416161dbSMark Fasheh }
2843416161dbSMark Fasheh 
284432b7c687SMark Fasheh /**
284532b7c687SMark Fasheh  * btrfs_clone() - clone a range from inode file to another
284632b7c687SMark Fasheh  *
284732b7c687SMark Fasheh  * @src: Inode to clone from
284832b7c687SMark Fasheh  * @inode: Inode to clone to
284932b7c687SMark Fasheh  * @off: Offset within source to start clone from
285032b7c687SMark Fasheh  * @olen: Original length, passed by user, of range to clone
285132b7c687SMark Fasheh  * @olen_aligned: Block-aligned value of olen, extent_same uses
285232b7c687SMark Fasheh  *               identical values here
285332b7c687SMark Fasheh  * @destoff: Offset within @inode to start clone
285432b7c687SMark Fasheh  */
285532b7c687SMark Fasheh static int btrfs_clone(struct inode *src, struct inode *inode,
285632b7c687SMark Fasheh 		       u64 off, u64 olen, u64 olen_aligned, u64 destoff)
2857f46b5a66SChristoph Hellwig {
2858f46b5a66SChristoph Hellwig 	struct btrfs_root *root = BTRFS_I(inode)->root;
285932b7c687SMark Fasheh 	struct btrfs_path *path = NULL;
2860f46b5a66SChristoph Hellwig 	struct extent_buffer *leaf;
286132b7c687SMark Fasheh 	struct btrfs_trans_handle *trans;
286232b7c687SMark Fasheh 	char *buf = NULL;
2863ae01a0abSYan Zheng 	struct btrfs_key key;
2864f46b5a66SChristoph Hellwig 	u32 nritems;
2865f46b5a66SChristoph Hellwig 	int slot;
2866ae01a0abSYan Zheng 	int ret;
286732b7c687SMark Fasheh 	u64 len = olen_aligned;
2868ae01a0abSYan Zheng 
2869ae01a0abSYan Zheng 	ret = -ENOMEM;
2870ae01a0abSYan Zheng 	buf = vmalloc(btrfs_level_size(root, 0));
2871ae01a0abSYan Zheng 	if (!buf)
287232b7c687SMark Fasheh 		return ret;
2873ae01a0abSYan Zheng 
2874ae01a0abSYan Zheng 	path = btrfs_alloc_path();
2875ae01a0abSYan Zheng 	if (!path) {
2876ae01a0abSYan Zheng 		vfree(buf);
287732b7c687SMark Fasheh 		return ret;
2878ae01a0abSYan Zheng 	}
287932b7c687SMark Fasheh 
2880ae01a0abSYan Zheng 	path->reada = 2;
2881c5c9cd4dSSage Weil 	/* clone data */
288233345d01SLi Zefan 	key.objectid = btrfs_ino(src);
2883ae01a0abSYan Zheng 	key.type = BTRFS_EXTENT_DATA_KEY;
2884ae01a0abSYan Zheng 	key.offset = 0;
2885f46b5a66SChristoph Hellwig 
2886f46b5a66SChristoph Hellwig 	while (1) {
2887f46b5a66SChristoph Hellwig 		/*
2888f46b5a66SChristoph Hellwig 		 * note the key will change type as we walk through the
2889f46b5a66SChristoph Hellwig 		 * tree.
2890f46b5a66SChristoph Hellwig 		 */
2891362a20c5SDavid Sterba 		ret = btrfs_search_slot(NULL, BTRFS_I(src)->root, &key, path,
2892362a20c5SDavid Sterba 				0, 0);
2893f46b5a66SChristoph Hellwig 		if (ret < 0)
2894f46b5a66SChristoph Hellwig 			goto out;
2895f46b5a66SChristoph Hellwig 
2896ae01a0abSYan Zheng 		nritems = btrfs_header_nritems(path->nodes[0]);
2897ae01a0abSYan Zheng 		if (path->slots[0] >= nritems) {
2898362a20c5SDavid Sterba 			ret = btrfs_next_leaf(BTRFS_I(src)->root, path);
2899f46b5a66SChristoph Hellwig 			if (ret < 0)
2900f46b5a66SChristoph Hellwig 				goto out;
2901f46b5a66SChristoph Hellwig 			if (ret > 0)
2902f46b5a66SChristoph Hellwig 				break;
2903ae01a0abSYan Zheng 			nritems = btrfs_header_nritems(path->nodes[0]);
2904f46b5a66SChristoph Hellwig 		}
2905f46b5a66SChristoph Hellwig 		leaf = path->nodes[0];
2906f46b5a66SChristoph Hellwig 		slot = path->slots[0];
2907f46b5a66SChristoph Hellwig 
2908ae01a0abSYan Zheng 		btrfs_item_key_to_cpu(leaf, &key, slot);
2909d20f7043SChris Mason 		if (btrfs_key_type(&key) > BTRFS_EXTENT_DATA_KEY ||
291033345d01SLi Zefan 		    key.objectid != btrfs_ino(src))
2911f46b5a66SChristoph Hellwig 			break;
2912f46b5a66SChristoph Hellwig 
2913c5c9cd4dSSage Weil 		if (btrfs_key_type(&key) == BTRFS_EXTENT_DATA_KEY) {
2914c5c9cd4dSSage Weil 			struct btrfs_file_extent_item *extent;
2915c5c9cd4dSSage Weil 			int type;
291631840ae1SZheng Yan 			u32 size;
291731840ae1SZheng Yan 			struct btrfs_key new_key;
2918c5c9cd4dSSage Weil 			u64 disko = 0, diskl = 0;
2919c5c9cd4dSSage Weil 			u64 datao = 0, datal = 0;
2920c5c9cd4dSSage Weil 			u8 comp;
2921b5384d48SSage Weil 			u64 endoff;
292231840ae1SZheng Yan 
292331840ae1SZheng Yan 			size = btrfs_item_size_nr(leaf, slot);
292431840ae1SZheng Yan 			read_extent_buffer(leaf, buf,
292531840ae1SZheng Yan 					   btrfs_item_ptr_offset(leaf, slot),
292631840ae1SZheng Yan 					   size);
2927c5c9cd4dSSage Weil 
2928c5c9cd4dSSage Weil 			extent = btrfs_item_ptr(leaf, slot,
2929c5c9cd4dSSage Weil 						struct btrfs_file_extent_item);
2930c5c9cd4dSSage Weil 			comp = btrfs_file_extent_compression(leaf, extent);
2931c5c9cd4dSSage Weil 			type = btrfs_file_extent_type(leaf, extent);
2932c8a894d7SChris Mason 			if (type == BTRFS_FILE_EXTENT_REG ||
2933c8a894d7SChris Mason 			    type == BTRFS_FILE_EXTENT_PREALLOC) {
2934d397712bSChris Mason 				disko = btrfs_file_extent_disk_bytenr(leaf,
2935d397712bSChris Mason 								      extent);
2936d397712bSChris Mason 				diskl = btrfs_file_extent_disk_num_bytes(leaf,
2937d397712bSChris Mason 								 extent);
2938c5c9cd4dSSage Weil 				datao = btrfs_file_extent_offset(leaf, extent);
2939d397712bSChris Mason 				datal = btrfs_file_extent_num_bytes(leaf,
2940d397712bSChris Mason 								    extent);
2941c5c9cd4dSSage Weil 			} else if (type == BTRFS_FILE_EXTENT_INLINE) {
2942c5c9cd4dSSage Weil 				/* take upper bound, may be compressed */
2943c5c9cd4dSSage Weil 				datal = btrfs_file_extent_ram_bytes(leaf,
2944c5c9cd4dSSage Weil 								    extent);
2945c5c9cd4dSSage Weil 			}
2946b3b4aa74SDavid Sterba 			btrfs_release_path(path);
294731840ae1SZheng Yan 
2948050006a7SSage Weil 			if (key.offset + datal <= off ||
2949aa42ffd9SLiu Bo 			    key.offset >= off + len - 1)
2950c5c9cd4dSSage Weil 				goto next;
2951c5c9cd4dSSage Weil 
295231840ae1SZheng Yan 			memcpy(&new_key, &key, sizeof(new_key));
295333345d01SLi Zefan 			new_key.objectid = btrfs_ino(inode);
29544d728ec7SLi Zefan 			if (off <= key.offset)
2955c5c9cd4dSSage Weil 				new_key.offset = key.offset + destoff - off;
29564d728ec7SLi Zefan 			else
29574d728ec7SLi Zefan 				new_key.offset = destoff;
2958c5c9cd4dSSage Weil 
2959b6f3409bSSage Weil 			/*
2960b6f3409bSSage Weil 			 * 1 - adjusting old extent (we may have to split it)
2961b6f3409bSSage Weil 			 * 1 - add new extent
2962b6f3409bSSage Weil 			 * 1 - inode update
2963b6f3409bSSage Weil 			 */
2964b6f3409bSSage Weil 			trans = btrfs_start_transaction(root, 3);
2965a22285a6SYan, Zheng 			if (IS_ERR(trans)) {
2966a22285a6SYan, Zheng 				ret = PTR_ERR(trans);
2967a22285a6SYan, Zheng 				goto out;
2968a22285a6SYan, Zheng 			}
2969a22285a6SYan, Zheng 
2970c8a894d7SChris Mason 			if (type == BTRFS_FILE_EXTENT_REG ||
2971c8a894d7SChris Mason 			    type == BTRFS_FILE_EXTENT_PREALLOC) {
2972d72c0842SLi Zefan 				/*
2973d72c0842SLi Zefan 				 *    a  | --- range to clone ---|  b
2974d72c0842SLi Zefan 				 * | ------------- extent ------------- |
2975d72c0842SLi Zefan 				 */
2976d72c0842SLi Zefan 
2977d72c0842SLi Zefan 				/* substract range b */
2978d72c0842SLi Zefan 				if (key.offset + datal > off + len)
2979d72c0842SLi Zefan 					datal = off + len - key.offset;
2980d72c0842SLi Zefan 
2981d72c0842SLi Zefan 				/* substract range a */
2982a22285a6SYan, Zheng 				if (off > key.offset) {
2983a22285a6SYan, Zheng 					datao += off - key.offset;
2984a22285a6SYan, Zheng 					datal -= off - key.offset;
2985a22285a6SYan, Zheng 				}
2986a22285a6SYan, Zheng 
29875dc562c5SJosef Bacik 				ret = btrfs_drop_extents(trans, root, inode,
2988a22285a6SYan, Zheng 							 new_key.offset,
2989a22285a6SYan, Zheng 							 new_key.offset + datal,
29902671485dSJosef Bacik 							 1);
299179787eaaSJeff Mahoney 				if (ret) {
299279787eaaSJeff Mahoney 					btrfs_abort_transaction(trans, root,
299379787eaaSJeff Mahoney 								ret);
299479787eaaSJeff Mahoney 					btrfs_end_transaction(trans, root);
299579787eaaSJeff Mahoney 					goto out;
299679787eaaSJeff Mahoney 				}
2997a22285a6SYan, Zheng 
299831840ae1SZheng Yan 				ret = btrfs_insert_empty_item(trans, root, path,
299931840ae1SZheng Yan 							      &new_key, size);
300079787eaaSJeff Mahoney 				if (ret) {
300179787eaaSJeff Mahoney 					btrfs_abort_transaction(trans, root,
300279787eaaSJeff Mahoney 								ret);
300379787eaaSJeff Mahoney 					btrfs_end_transaction(trans, root);
300479787eaaSJeff Mahoney 					goto out;
300579787eaaSJeff Mahoney 				}
300631840ae1SZheng Yan 
300731840ae1SZheng Yan 				leaf = path->nodes[0];
300831840ae1SZheng Yan 				slot = path->slots[0];
300931840ae1SZheng Yan 				write_extent_buffer(leaf, buf,
301031840ae1SZheng Yan 					    btrfs_item_ptr_offset(leaf, slot),
301131840ae1SZheng Yan 					    size);
3012ae01a0abSYan Zheng 
3013f46b5a66SChristoph Hellwig 				extent = btrfs_item_ptr(leaf, slot,
3014f46b5a66SChristoph Hellwig 						struct btrfs_file_extent_item);
3015c5c9cd4dSSage Weil 
3016c5c9cd4dSSage Weil 				/* disko == 0 means it's a hole */
3017c5c9cd4dSSage Weil 				if (!disko)
3018c5c9cd4dSSage Weil 					datao = 0;
3019c5c9cd4dSSage Weil 
3020c5c9cd4dSSage Weil 				btrfs_set_file_extent_offset(leaf, extent,
3021c5c9cd4dSSage Weil 							     datao);
3022c5c9cd4dSSage Weil 				btrfs_set_file_extent_num_bytes(leaf, extent,
3023c5c9cd4dSSage Weil 								datal);
3024c5c9cd4dSSage Weil 				if (disko) {
3025c5c9cd4dSSage Weil 					inode_add_bytes(inode, datal);
3026ae01a0abSYan Zheng 					ret = btrfs_inc_extent_ref(trans, root,
30275d4f98a2SYan Zheng 							disko, diskl, 0,
3028f46b5a66SChristoph Hellwig 							root->root_key.objectid,
302933345d01SLi Zefan 							btrfs_ino(inode),
303066d7e7f0SArne Jansen 							new_key.offset - datao,
303166d7e7f0SArne Jansen 							0);
303279787eaaSJeff Mahoney 					if (ret) {
303379787eaaSJeff Mahoney 						btrfs_abort_transaction(trans,
303479787eaaSJeff Mahoney 									root,
303579787eaaSJeff Mahoney 									ret);
303679787eaaSJeff Mahoney 						btrfs_end_transaction(trans,
303779787eaaSJeff Mahoney 								      root);
303879787eaaSJeff Mahoney 						goto out;
303979787eaaSJeff Mahoney 
304079787eaaSJeff Mahoney 					}
3041f46b5a66SChristoph Hellwig 				}
3042c5c9cd4dSSage Weil 			} else if (type == BTRFS_FILE_EXTENT_INLINE) {
3043c5c9cd4dSSage Weil 				u64 skip = 0;
3044c5c9cd4dSSage Weil 				u64 trim = 0;
3045c5c9cd4dSSage Weil 				if (off > key.offset) {
3046c5c9cd4dSSage Weil 					skip = off - key.offset;
3047c5c9cd4dSSage Weil 					new_key.offset += skip;
304831840ae1SZheng Yan 				}
3049d397712bSChris Mason 
3050c5c9cd4dSSage Weil 				if (key.offset + datal > off + len)
3051c5c9cd4dSSage Weil 					trim = key.offset + datal - (off + len);
3052d397712bSChris Mason 
3053c5c9cd4dSSage Weil 				if (comp && (skip || trim)) {
3054c5c9cd4dSSage Weil 					ret = -EINVAL;
3055a22285a6SYan, Zheng 					btrfs_end_transaction(trans, root);
3056c5c9cd4dSSage Weil 					goto out;
305731840ae1SZheng Yan 				}
3058c5c9cd4dSSage Weil 				size -= skip + trim;
3059c5c9cd4dSSage Weil 				datal -= skip + trim;
3060a22285a6SYan, Zheng 
30615dc562c5SJosef Bacik 				ret = btrfs_drop_extents(trans, root, inode,
3062a22285a6SYan, Zheng 							 new_key.offset,
3063a22285a6SYan, Zheng 							 new_key.offset + datal,
30642671485dSJosef Bacik 							 1);
306579787eaaSJeff Mahoney 				if (ret) {
306679787eaaSJeff Mahoney 					btrfs_abort_transaction(trans, root,
306779787eaaSJeff Mahoney 								ret);
306879787eaaSJeff Mahoney 					btrfs_end_transaction(trans, root);
306979787eaaSJeff Mahoney 					goto out;
307079787eaaSJeff Mahoney 				}
3071a22285a6SYan, Zheng 
3072c5c9cd4dSSage Weil 				ret = btrfs_insert_empty_item(trans, root, path,
3073c5c9cd4dSSage Weil 							      &new_key, size);
307479787eaaSJeff Mahoney 				if (ret) {
307579787eaaSJeff Mahoney 					btrfs_abort_transaction(trans, root,
307679787eaaSJeff Mahoney 								ret);
307779787eaaSJeff Mahoney 					btrfs_end_transaction(trans, root);
307879787eaaSJeff Mahoney 					goto out;
307979787eaaSJeff Mahoney 				}
3080c5c9cd4dSSage Weil 
3081c5c9cd4dSSage Weil 				if (skip) {
3082d397712bSChris Mason 					u32 start =
3083d397712bSChris Mason 					  btrfs_file_extent_calc_inline_size(0);
3084c5c9cd4dSSage Weil 					memmove(buf+start, buf+start+skip,
3085c5c9cd4dSSage Weil 						datal);
3086c5c9cd4dSSage Weil 				}
3087c5c9cd4dSSage Weil 
3088c5c9cd4dSSage Weil 				leaf = path->nodes[0];
3089c5c9cd4dSSage Weil 				slot = path->slots[0];
3090c5c9cd4dSSage Weil 				write_extent_buffer(leaf, buf,
3091c5c9cd4dSSage Weil 					    btrfs_item_ptr_offset(leaf, slot),
3092c5c9cd4dSSage Weil 					    size);
3093c5c9cd4dSSage Weil 				inode_add_bytes(inode, datal);
3094c5c9cd4dSSage Weil 			}
3095c5c9cd4dSSage Weil 
3096c5c9cd4dSSage Weil 			btrfs_mark_buffer_dirty(leaf);
3097b3b4aa74SDavid Sterba 			btrfs_release_path(path);
3098c5c9cd4dSSage Weil 
30990c4d2d95SJosef Bacik 			inode_inc_iversion(inode);
3100a22285a6SYan, Zheng 			inode->i_mtime = inode->i_ctime = CURRENT_TIME;
3101b5384d48SSage Weil 
3102b5384d48SSage Weil 			/*
3103b5384d48SSage Weil 			 * we round up to the block size at eof when
3104b5384d48SSage Weil 			 * determining which extents to clone above,
3105b5384d48SSage Weil 			 * but shouldn't round up the file size
3106b5384d48SSage Weil 			 */
3107b5384d48SSage Weil 			endoff = new_key.offset + datal;
31085f3888ffSLi Zefan 			if (endoff > destoff+olen)
31095f3888ffSLi Zefan 				endoff = destoff+olen;
3110b5384d48SSage Weil 			if (endoff > inode->i_size)
3111b5384d48SSage Weil 				btrfs_i_size_write(inode, endoff);
3112b5384d48SSage Weil 
3113a22285a6SYan, Zheng 			ret = btrfs_update_inode(trans, root, inode);
311479787eaaSJeff Mahoney 			if (ret) {
311579787eaaSJeff Mahoney 				btrfs_abort_transaction(trans, root, ret);
3116a22285a6SYan, Zheng 				btrfs_end_transaction(trans, root);
311779787eaaSJeff Mahoney 				goto out;
311879787eaaSJeff Mahoney 			}
311979787eaaSJeff Mahoney 			ret = btrfs_end_transaction(trans, root);
3120a22285a6SYan, Zheng 		}
3121c5c9cd4dSSage Weil next:
3122b3b4aa74SDavid Sterba 		btrfs_release_path(path);
3123ae01a0abSYan Zheng 		key.offset++;
3124ae01a0abSYan Zheng 	}
3125f46b5a66SChristoph Hellwig 	ret = 0;
312632b7c687SMark Fasheh 
3127f46b5a66SChristoph Hellwig out:
3128b3b4aa74SDavid Sterba 	btrfs_release_path(path);
312932b7c687SMark Fasheh 	btrfs_free_path(path);
313032b7c687SMark Fasheh 	vfree(buf);
313132b7c687SMark Fasheh 	return ret;
313232b7c687SMark Fasheh }
313332b7c687SMark Fasheh 
313432b7c687SMark Fasheh static noinline long btrfs_ioctl_clone(struct file *file, unsigned long srcfd,
313532b7c687SMark Fasheh 				       u64 off, u64 olen, u64 destoff)
313632b7c687SMark Fasheh {
313754563d41SAl Viro 	struct inode *inode = file_inode(file);
313832b7c687SMark Fasheh 	struct btrfs_root *root = BTRFS_I(inode)->root;
313932b7c687SMark Fasheh 	struct fd src_file;
314032b7c687SMark Fasheh 	struct inode *src;
314132b7c687SMark Fasheh 	int ret;
314232b7c687SMark Fasheh 	u64 len = olen;
314332b7c687SMark Fasheh 	u64 bs = root->fs_info->sb->s_blocksize;
314432b7c687SMark Fasheh 	int same_inode = 0;
314532b7c687SMark Fasheh 
314632b7c687SMark Fasheh 	/*
314732b7c687SMark Fasheh 	 * TODO:
314832b7c687SMark Fasheh 	 * - split compressed inline extents.  annoying: we need to
314932b7c687SMark Fasheh 	 *   decompress into destination's address_space (the file offset
315032b7c687SMark Fasheh 	 *   may change, so source mapping won't do), then recompress (or
315132b7c687SMark Fasheh 	 *   otherwise reinsert) a subrange.
315232b7c687SMark Fasheh 	 * - allow ranges within the same file to be cloned (provided
315332b7c687SMark Fasheh 	 *   they don't overlap)?
315432b7c687SMark Fasheh 	 */
315532b7c687SMark Fasheh 
315632b7c687SMark Fasheh 	/* the destination must be opened for writing */
315732b7c687SMark Fasheh 	if (!(file->f_mode & FMODE_WRITE) || (file->f_flags & O_APPEND))
315832b7c687SMark Fasheh 		return -EINVAL;
315932b7c687SMark Fasheh 
316032b7c687SMark Fasheh 	if (btrfs_root_readonly(root))
316132b7c687SMark Fasheh 		return -EROFS;
316232b7c687SMark Fasheh 
316332b7c687SMark Fasheh 	ret = mnt_want_write_file(file);
316432b7c687SMark Fasheh 	if (ret)
316532b7c687SMark Fasheh 		return ret;
316632b7c687SMark Fasheh 
316732b7c687SMark Fasheh 	src_file = fdget(srcfd);
316832b7c687SMark Fasheh 	if (!src_file.file) {
316932b7c687SMark Fasheh 		ret = -EBADF;
317032b7c687SMark Fasheh 		goto out_drop_write;
317132b7c687SMark Fasheh 	}
317232b7c687SMark Fasheh 
317332b7c687SMark Fasheh 	ret = -EXDEV;
317432b7c687SMark Fasheh 	if (src_file.file->f_path.mnt != file->f_path.mnt)
317532b7c687SMark Fasheh 		goto out_fput;
317632b7c687SMark Fasheh 
317732b7c687SMark Fasheh 	src = file_inode(src_file.file);
317832b7c687SMark Fasheh 
317932b7c687SMark Fasheh 	ret = -EINVAL;
318032b7c687SMark Fasheh 	if (src == inode)
318132b7c687SMark Fasheh 		same_inode = 1;
318232b7c687SMark Fasheh 
318332b7c687SMark Fasheh 	/* the src must be open for reading */
318432b7c687SMark Fasheh 	if (!(src_file.file->f_mode & FMODE_READ))
318532b7c687SMark Fasheh 		goto out_fput;
318632b7c687SMark Fasheh 
318732b7c687SMark Fasheh 	/* don't make the dst file partly checksummed */
318832b7c687SMark Fasheh 	if ((BTRFS_I(src)->flags & BTRFS_INODE_NODATASUM) !=
318932b7c687SMark Fasheh 	    (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM))
319032b7c687SMark Fasheh 		goto out_fput;
319132b7c687SMark Fasheh 
319232b7c687SMark Fasheh 	ret = -EISDIR;
319332b7c687SMark Fasheh 	if (S_ISDIR(src->i_mode) || S_ISDIR(inode->i_mode))
319432b7c687SMark Fasheh 		goto out_fput;
319532b7c687SMark Fasheh 
319632b7c687SMark Fasheh 	ret = -EXDEV;
319732b7c687SMark Fasheh 	if (src->i_sb != inode->i_sb)
319832b7c687SMark Fasheh 		goto out_fput;
319932b7c687SMark Fasheh 
320032b7c687SMark Fasheh 	if (!same_inode) {
320132b7c687SMark Fasheh 		if (inode < src) {
320232b7c687SMark Fasheh 			mutex_lock_nested(&inode->i_mutex, I_MUTEX_PARENT);
320332b7c687SMark Fasheh 			mutex_lock_nested(&src->i_mutex, I_MUTEX_CHILD);
320432b7c687SMark Fasheh 		} else {
320532b7c687SMark Fasheh 			mutex_lock_nested(&src->i_mutex, I_MUTEX_PARENT);
320632b7c687SMark Fasheh 			mutex_lock_nested(&inode->i_mutex, I_MUTEX_CHILD);
320732b7c687SMark Fasheh 		}
320832b7c687SMark Fasheh 	} else {
320932b7c687SMark Fasheh 		mutex_lock(&src->i_mutex);
321032b7c687SMark Fasheh 	}
321132b7c687SMark Fasheh 
321232b7c687SMark Fasheh 	/* determine range to clone */
321332b7c687SMark Fasheh 	ret = -EINVAL;
321432b7c687SMark Fasheh 	if (off + len > src->i_size || off + len < off)
321532b7c687SMark Fasheh 		goto out_unlock;
321632b7c687SMark Fasheh 	if (len == 0)
321732b7c687SMark Fasheh 		olen = len = src->i_size - off;
321832b7c687SMark Fasheh 	/* if we extend to eof, continue to block boundary */
321932b7c687SMark Fasheh 	if (off + len == src->i_size)
322032b7c687SMark Fasheh 		len = ALIGN(src->i_size, bs) - off;
322132b7c687SMark Fasheh 
322232b7c687SMark Fasheh 	/* verify the end result is block aligned */
322332b7c687SMark Fasheh 	if (!IS_ALIGNED(off, bs) || !IS_ALIGNED(off + len, bs) ||
322432b7c687SMark Fasheh 	    !IS_ALIGNED(destoff, bs))
322532b7c687SMark Fasheh 		goto out_unlock;
322632b7c687SMark Fasheh 
322732b7c687SMark Fasheh 	/* verify if ranges are overlapped within the same file */
322832b7c687SMark Fasheh 	if (same_inode) {
322932b7c687SMark Fasheh 		if (destoff + len > off && destoff < off + len)
323032b7c687SMark Fasheh 			goto out_unlock;
323132b7c687SMark Fasheh 	}
323232b7c687SMark Fasheh 
323332b7c687SMark Fasheh 	if (destoff > inode->i_size) {
323432b7c687SMark Fasheh 		ret = btrfs_cont_expand(inode, inode->i_size, destoff);
323532b7c687SMark Fasheh 		if (ret)
323632b7c687SMark Fasheh 			goto out_unlock;
323732b7c687SMark Fasheh 	}
323832b7c687SMark Fasheh 
323932b7c687SMark Fasheh 	/* truncate page cache pages from target inode range */
324032b7c687SMark Fasheh 	truncate_inode_pages_range(&inode->i_data, destoff,
324132b7c687SMark Fasheh 				   PAGE_CACHE_ALIGN(destoff + len) - 1);
324232b7c687SMark Fasheh 
324332b7c687SMark Fasheh 	lock_extent_range(src, off, len);
324432b7c687SMark Fasheh 
324532b7c687SMark Fasheh 	ret = btrfs_clone(src, inode, off, olen, len, destoff);
324632b7c687SMark Fasheh 
3247aa42ffd9SLiu Bo 	unlock_extent(&BTRFS_I(src)->io_tree, off, off + len - 1);
3248f46b5a66SChristoph Hellwig out_unlock:
3249f46b5a66SChristoph Hellwig 	mutex_unlock(&src->i_mutex);
3250a96fbc72SLiu Bo 	if (!same_inode)
3251f46b5a66SChristoph Hellwig 		mutex_unlock(&inode->i_mutex);
3252f46b5a66SChristoph Hellwig out_fput:
32532903ff01SAl Viro 	fdput(src_file);
3254ab67b7c1SYan Zheng out_drop_write:
32552a79f17eSAl Viro 	mnt_drop_write_file(file);
3256f46b5a66SChristoph Hellwig 	return ret;
3257f46b5a66SChristoph Hellwig }
3258f46b5a66SChristoph Hellwig 
32597a865e8aSChristoph Hellwig static long btrfs_ioctl_clone_range(struct file *file, void __user *argp)
3260c5c9cd4dSSage Weil {
3261c5c9cd4dSSage Weil 	struct btrfs_ioctl_clone_range_args args;
3262c5c9cd4dSSage Weil 
32637a865e8aSChristoph Hellwig 	if (copy_from_user(&args, argp, sizeof(args)))
3264c5c9cd4dSSage Weil 		return -EFAULT;
3265c5c9cd4dSSage Weil 	return btrfs_ioctl_clone(file, args.src_fd, args.src_offset,
3266c5c9cd4dSSage Weil 				 args.src_length, args.dest_offset);
3267c5c9cd4dSSage Weil }
3268c5c9cd4dSSage Weil 
3269f46b5a66SChristoph Hellwig /*
3270f46b5a66SChristoph Hellwig  * there are many ways the trans_start and trans_end ioctls can lead
3271f46b5a66SChristoph Hellwig  * to deadlocks.  They should only be used by applications that
3272f46b5a66SChristoph Hellwig  * basically own the machine, and have a very in depth understanding
3273f46b5a66SChristoph Hellwig  * of all the possible deadlocks and enospc problems.
3274f46b5a66SChristoph Hellwig  */
3275b2950863SChristoph Hellwig static long btrfs_ioctl_trans_start(struct file *file)
3276f46b5a66SChristoph Hellwig {
3277496ad9aaSAl Viro 	struct inode *inode = file_inode(file);
3278f46b5a66SChristoph Hellwig 	struct btrfs_root *root = BTRFS_I(inode)->root;
3279f46b5a66SChristoph Hellwig 	struct btrfs_trans_handle *trans;
32801ab86aedSSage Weil 	int ret;
3281f46b5a66SChristoph Hellwig 
32821ab86aedSSage Weil 	ret = -EPERM;
3283df5b5520SChristoph Hellwig 	if (!capable(CAP_SYS_ADMIN))
3284f46b5a66SChristoph Hellwig 		goto out;
32851ab86aedSSage Weil 
32861ab86aedSSage Weil 	ret = -EINPROGRESS;
32871ab86aedSSage Weil 	if (file->private_data)
32881ab86aedSSage Weil 		goto out;
32899ca9ee09SSage Weil 
3290b83cc969SLi Zefan 	ret = -EROFS;
3291b83cc969SLi Zefan 	if (btrfs_root_readonly(root))
3292b83cc969SLi Zefan 		goto out;
3293b83cc969SLi Zefan 
3294a561be71SAl Viro 	ret = mnt_want_write_file(file);
3295c146afadSYan Zheng 	if (ret)
3296c146afadSYan Zheng 		goto out;
3297c146afadSYan Zheng 
3298a4abeea4SJosef Bacik 	atomic_inc(&root->fs_info->open_ioctl_trans);
32999ca9ee09SSage Weil 
3300f46b5a66SChristoph Hellwig 	ret = -ENOMEM;
33017a7eaa40SJosef Bacik 	trans = btrfs_start_ioctl_transaction(root);
3302abd30bb0STsutomu Itoh 	if (IS_ERR(trans))
33031ab86aedSSage Weil 		goto out_drop;
33041ab86aedSSage Weil 
33051ab86aedSSage Weil 	file->private_data = trans;
33061ab86aedSSage Weil 	return 0;
33071ab86aedSSage Weil 
33081ab86aedSSage Weil out_drop:
3309a4abeea4SJosef Bacik 	atomic_dec(&root->fs_info->open_ioctl_trans);
33102a79f17eSAl Viro 	mnt_drop_write_file(file);
3311f46b5a66SChristoph Hellwig out:
3312f46b5a66SChristoph Hellwig 	return ret;
3313f46b5a66SChristoph Hellwig }
3314f46b5a66SChristoph Hellwig 
33156ef5ed0dSJosef Bacik static long btrfs_ioctl_default_subvol(struct file *file, void __user *argp)
33166ef5ed0dSJosef Bacik {
3317496ad9aaSAl Viro 	struct inode *inode = file_inode(file);
33186ef5ed0dSJosef Bacik 	struct btrfs_root *root = BTRFS_I(inode)->root;
33196ef5ed0dSJosef Bacik 	struct btrfs_root *new_root;
33206ef5ed0dSJosef Bacik 	struct btrfs_dir_item *di;
33216ef5ed0dSJosef Bacik 	struct btrfs_trans_handle *trans;
33226ef5ed0dSJosef Bacik 	struct btrfs_path *path;
33236ef5ed0dSJosef Bacik 	struct btrfs_key location;
33246ef5ed0dSJosef Bacik 	struct btrfs_disk_key disk_key;
33256ef5ed0dSJosef Bacik 	u64 objectid = 0;
33266ef5ed0dSJosef Bacik 	u64 dir_id;
33273c04ce01SMiao Xie 	int ret;
33286ef5ed0dSJosef Bacik 
33296ef5ed0dSJosef Bacik 	if (!capable(CAP_SYS_ADMIN))
33306ef5ed0dSJosef Bacik 		return -EPERM;
33316ef5ed0dSJosef Bacik 
33323c04ce01SMiao Xie 	ret = mnt_want_write_file(file);
33333c04ce01SMiao Xie 	if (ret)
33343c04ce01SMiao Xie 		return ret;
33353c04ce01SMiao Xie 
33363c04ce01SMiao Xie 	if (copy_from_user(&objectid, argp, sizeof(objectid))) {
33373c04ce01SMiao Xie 		ret = -EFAULT;
33383c04ce01SMiao Xie 		goto out;
33393c04ce01SMiao Xie 	}
33406ef5ed0dSJosef Bacik 
33416ef5ed0dSJosef Bacik 	if (!objectid)
33421cecf579Schandan 		objectid = BTRFS_FS_TREE_OBJECTID;
33436ef5ed0dSJosef Bacik 
33446ef5ed0dSJosef Bacik 	location.objectid = objectid;
33456ef5ed0dSJosef Bacik 	location.type = BTRFS_ROOT_ITEM_KEY;
33466ef5ed0dSJosef Bacik 	location.offset = (u64)-1;
33476ef5ed0dSJosef Bacik 
33486ef5ed0dSJosef Bacik 	new_root = btrfs_read_fs_root_no_name(root->fs_info, &location);
33493c04ce01SMiao Xie 	if (IS_ERR(new_root)) {
33503c04ce01SMiao Xie 		ret = PTR_ERR(new_root);
33513c04ce01SMiao Xie 		goto out;
33523c04ce01SMiao Xie 	}
33536ef5ed0dSJosef Bacik 
33546ef5ed0dSJosef Bacik 	path = btrfs_alloc_path();
33553c04ce01SMiao Xie 	if (!path) {
33563c04ce01SMiao Xie 		ret = -ENOMEM;
33573c04ce01SMiao Xie 		goto out;
33583c04ce01SMiao Xie 	}
33596ef5ed0dSJosef Bacik 	path->leave_spinning = 1;
33606ef5ed0dSJosef Bacik 
33616ef5ed0dSJosef Bacik 	trans = btrfs_start_transaction(root, 1);
336298d5dc13STsutomu Itoh 	if (IS_ERR(trans)) {
33636ef5ed0dSJosef Bacik 		btrfs_free_path(path);
33643c04ce01SMiao Xie 		ret = PTR_ERR(trans);
33653c04ce01SMiao Xie 		goto out;
33666ef5ed0dSJosef Bacik 	}
33676ef5ed0dSJosef Bacik 
33686c41761fSDavid Sterba 	dir_id = btrfs_super_root_dir(root->fs_info->super_copy);
33696ef5ed0dSJosef Bacik 	di = btrfs_lookup_dir_item(trans, root->fs_info->tree_root, path,
33706ef5ed0dSJosef Bacik 				   dir_id, "default", 7, 1);
3371cf1e99a4SDan Carpenter 	if (IS_ERR_OR_NULL(di)) {
33726ef5ed0dSJosef Bacik 		btrfs_free_path(path);
33736ef5ed0dSJosef Bacik 		btrfs_end_transaction(trans, root);
3374efe120a0SFrank Holton 		btrfs_err(new_root->fs_info, "Umm, you don't have the default dir"
3375efe120a0SFrank Holton 			   "item, this isn't going to work");
33763c04ce01SMiao Xie 		ret = -ENOENT;
33773c04ce01SMiao Xie 		goto out;
33786ef5ed0dSJosef Bacik 	}
33796ef5ed0dSJosef Bacik 
33806ef5ed0dSJosef Bacik 	btrfs_cpu_key_to_disk(&disk_key, &new_root->root_key);
33816ef5ed0dSJosef Bacik 	btrfs_set_dir_item_key(path->nodes[0], di, &disk_key);
33826ef5ed0dSJosef Bacik 	btrfs_mark_buffer_dirty(path->nodes[0]);
33836ef5ed0dSJosef Bacik 	btrfs_free_path(path);
33846ef5ed0dSJosef Bacik 
33852b0ce2c2SMitch Harder 	btrfs_set_fs_incompat(root->fs_info, DEFAULT_SUBVOL);
33866ef5ed0dSJosef Bacik 	btrfs_end_transaction(trans, root);
33873c04ce01SMiao Xie out:
33883c04ce01SMiao Xie 	mnt_drop_write_file(file);
33893c04ce01SMiao Xie 	return ret;
33906ef5ed0dSJosef Bacik }
33916ef5ed0dSJosef Bacik 
33925af3e8ccSStefan Behrens void btrfs_get_block_group_info(struct list_head *groups_list,
3393bf5fc093SJosef Bacik 				struct btrfs_ioctl_space_info *space)
3394bf5fc093SJosef Bacik {
3395bf5fc093SJosef Bacik 	struct btrfs_block_group_cache *block_group;
3396bf5fc093SJosef Bacik 
3397bf5fc093SJosef Bacik 	space->total_bytes = 0;
3398bf5fc093SJosef Bacik 	space->used_bytes = 0;
3399bf5fc093SJosef Bacik 	space->flags = 0;
3400bf5fc093SJosef Bacik 	list_for_each_entry(block_group, groups_list, list) {
3401bf5fc093SJosef Bacik 		space->flags = block_group->flags;
3402bf5fc093SJosef Bacik 		space->total_bytes += block_group->key.offset;
3403bf5fc093SJosef Bacik 		space->used_bytes +=
3404bf5fc093SJosef Bacik 			btrfs_block_group_used(&block_group->item);
3405bf5fc093SJosef Bacik 	}
3406bf5fc093SJosef Bacik }
3407bf5fc093SJosef Bacik 
340848a3b636SEric Sandeen static long btrfs_ioctl_space_info(struct btrfs_root *root, void __user *arg)
34091406e432SJosef Bacik {
34101406e432SJosef Bacik 	struct btrfs_ioctl_space_args space_args;
34111406e432SJosef Bacik 	struct btrfs_ioctl_space_info space;
34121406e432SJosef Bacik 	struct btrfs_ioctl_space_info *dest;
34137fde62bfSChris Mason 	struct btrfs_ioctl_space_info *dest_orig;
341413f2696fSDaniel J Blueman 	struct btrfs_ioctl_space_info __user *user_dest;
34151406e432SJosef Bacik 	struct btrfs_space_info *info;
3416bf5fc093SJosef Bacik 	u64 types[] = {BTRFS_BLOCK_GROUP_DATA,
3417bf5fc093SJosef Bacik 		       BTRFS_BLOCK_GROUP_SYSTEM,
3418bf5fc093SJosef Bacik 		       BTRFS_BLOCK_GROUP_METADATA,
3419bf5fc093SJosef Bacik 		       BTRFS_BLOCK_GROUP_DATA | BTRFS_BLOCK_GROUP_METADATA};
3420bf5fc093SJosef Bacik 	int num_types = 4;
34217fde62bfSChris Mason 	int alloc_size;
34221406e432SJosef Bacik 	int ret = 0;
342351788b1bSDan Rosenberg 	u64 slot_count = 0;
3424bf5fc093SJosef Bacik 	int i, c;
34251406e432SJosef Bacik 
34261406e432SJosef Bacik 	if (copy_from_user(&space_args,
34271406e432SJosef Bacik 			   (struct btrfs_ioctl_space_args __user *)arg,
34281406e432SJosef Bacik 			   sizeof(space_args)))
34291406e432SJosef Bacik 		return -EFAULT;
34301406e432SJosef Bacik 
3431bf5fc093SJosef Bacik 	for (i = 0; i < num_types; i++) {
3432bf5fc093SJosef Bacik 		struct btrfs_space_info *tmp;
3433bf5fc093SJosef Bacik 
3434bf5fc093SJosef Bacik 		info = NULL;
34357fde62bfSChris Mason 		rcu_read_lock();
3436bf5fc093SJosef Bacik 		list_for_each_entry_rcu(tmp, &root->fs_info->space_info,
3437bf5fc093SJosef Bacik 					list) {
3438bf5fc093SJosef Bacik 			if (tmp->flags == types[i]) {
3439bf5fc093SJosef Bacik 				info = tmp;
3440bf5fc093SJosef Bacik 				break;
3441bf5fc093SJosef Bacik 			}
3442bf5fc093SJosef Bacik 		}
34437fde62bfSChris Mason 		rcu_read_unlock();
34441406e432SJosef Bacik 
3445bf5fc093SJosef Bacik 		if (!info)
3446bf5fc093SJosef Bacik 			continue;
3447bf5fc093SJosef Bacik 
3448bf5fc093SJosef Bacik 		down_read(&info->groups_sem);
3449bf5fc093SJosef Bacik 		for (c = 0; c < BTRFS_NR_RAID_TYPES; c++) {
3450bf5fc093SJosef Bacik 			if (!list_empty(&info->block_groups[c]))
3451bf5fc093SJosef Bacik 				slot_count++;
3452bf5fc093SJosef Bacik 		}
3453bf5fc093SJosef Bacik 		up_read(&info->groups_sem);
3454bf5fc093SJosef Bacik 	}
3455bf5fc093SJosef Bacik 
34567fde62bfSChris Mason 	/* space_slots == 0 means they are asking for a count */
34577fde62bfSChris Mason 	if (space_args.space_slots == 0) {
34587fde62bfSChris Mason 		space_args.total_spaces = slot_count;
34597fde62bfSChris Mason 		goto out;
34607fde62bfSChris Mason 	}
3461bf5fc093SJosef Bacik 
346251788b1bSDan Rosenberg 	slot_count = min_t(u64, space_args.space_slots, slot_count);
3463bf5fc093SJosef Bacik 
34647fde62bfSChris Mason 	alloc_size = sizeof(*dest) * slot_count;
3465bf5fc093SJosef Bacik 
34667fde62bfSChris Mason 	/* we generally have at most 6 or so space infos, one for each raid
34677fde62bfSChris Mason 	 * level.  So, a whole page should be more than enough for everyone
34687fde62bfSChris Mason 	 */
34697fde62bfSChris Mason 	if (alloc_size > PAGE_CACHE_SIZE)
34707fde62bfSChris Mason 		return -ENOMEM;
34717fde62bfSChris Mason 
34727fde62bfSChris Mason 	space_args.total_spaces = 0;
34737fde62bfSChris Mason 	dest = kmalloc(alloc_size, GFP_NOFS);
34747fde62bfSChris Mason 	if (!dest)
34757fde62bfSChris Mason 		return -ENOMEM;
34767fde62bfSChris Mason 	dest_orig = dest;
34777fde62bfSChris Mason 
34787fde62bfSChris Mason 	/* now we have a buffer to copy into */
3479bf5fc093SJosef Bacik 	for (i = 0; i < num_types; i++) {
3480bf5fc093SJosef Bacik 		struct btrfs_space_info *tmp;
3481bf5fc093SJosef Bacik 
348251788b1bSDan Rosenberg 		if (!slot_count)
348351788b1bSDan Rosenberg 			break;
348451788b1bSDan Rosenberg 
3485bf5fc093SJosef Bacik 		info = NULL;
34861406e432SJosef Bacik 		rcu_read_lock();
3487bf5fc093SJosef Bacik 		list_for_each_entry_rcu(tmp, &root->fs_info->space_info,
3488bf5fc093SJosef Bacik 					list) {
3489bf5fc093SJosef Bacik 			if (tmp->flags == types[i]) {
3490bf5fc093SJosef Bacik 				info = tmp;
34917fde62bfSChris Mason 				break;
3492bf5fc093SJosef Bacik 			}
3493bf5fc093SJosef Bacik 		}
3494bf5fc093SJosef Bacik 		rcu_read_unlock();
34957fde62bfSChris Mason 
3496bf5fc093SJosef Bacik 		if (!info)
3497bf5fc093SJosef Bacik 			continue;
3498bf5fc093SJosef Bacik 		down_read(&info->groups_sem);
3499bf5fc093SJosef Bacik 		for (c = 0; c < BTRFS_NR_RAID_TYPES; c++) {
3500bf5fc093SJosef Bacik 			if (!list_empty(&info->block_groups[c])) {
35015af3e8ccSStefan Behrens 				btrfs_get_block_group_info(
35025af3e8ccSStefan Behrens 					&info->block_groups[c], &space);
35037fde62bfSChris Mason 				memcpy(dest, &space, sizeof(space));
35041406e432SJosef Bacik 				dest++;
35051406e432SJosef Bacik 				space_args.total_spaces++;
350651788b1bSDan Rosenberg 				slot_count--;
35071406e432SJosef Bacik 			}
350851788b1bSDan Rosenberg 			if (!slot_count)
350951788b1bSDan Rosenberg 				break;
3510bf5fc093SJosef Bacik 		}
3511bf5fc093SJosef Bacik 		up_read(&info->groups_sem);
3512bf5fc093SJosef Bacik 	}
35131406e432SJosef Bacik 
35142eec6c81SDaniel J Blueman 	user_dest = (struct btrfs_ioctl_space_info __user *)
35157fde62bfSChris Mason 		(arg + sizeof(struct btrfs_ioctl_space_args));
35167fde62bfSChris Mason 
35177fde62bfSChris Mason 	if (copy_to_user(user_dest, dest_orig, alloc_size))
35187fde62bfSChris Mason 		ret = -EFAULT;
35197fde62bfSChris Mason 
35207fde62bfSChris Mason 	kfree(dest_orig);
35217fde62bfSChris Mason out:
35227fde62bfSChris Mason 	if (ret == 0 && copy_to_user(arg, &space_args, sizeof(space_args)))
35231406e432SJosef Bacik 		ret = -EFAULT;
35241406e432SJosef Bacik 
35251406e432SJosef Bacik 	return ret;
35261406e432SJosef Bacik }
35271406e432SJosef Bacik 
352801e219e8SJeff Mahoney static long btrfs_ioctl_global_rsv(struct btrfs_root *root, void __user *arg)
352901e219e8SJeff Mahoney {
353001e219e8SJeff Mahoney 	struct btrfs_block_rsv *block_rsv = &root->fs_info->global_block_rsv;
353101e219e8SJeff Mahoney 	u64 reserved;
353201e219e8SJeff Mahoney 
353301e219e8SJeff Mahoney 	spin_lock(&block_rsv->lock);
353401e219e8SJeff Mahoney 	reserved = block_rsv->reserved;
353501e219e8SJeff Mahoney 	spin_unlock(&block_rsv->lock);
353601e219e8SJeff Mahoney 
353701e219e8SJeff Mahoney 	if (arg && copy_to_user(arg, &reserved, sizeof(reserved)))
353801e219e8SJeff Mahoney 		return -EFAULT;
353901e219e8SJeff Mahoney 	return 0;
354001e219e8SJeff Mahoney }
354101e219e8SJeff Mahoney 
3542f46b5a66SChristoph Hellwig /*
3543f46b5a66SChristoph Hellwig  * there are many ways the trans_start and trans_end ioctls can lead
3544f46b5a66SChristoph Hellwig  * to deadlocks.  They should only be used by applications that
3545f46b5a66SChristoph Hellwig  * basically own the machine, and have a very in depth understanding
3546f46b5a66SChristoph Hellwig  * of all the possible deadlocks and enospc problems.
3547f46b5a66SChristoph Hellwig  */
3548f46b5a66SChristoph Hellwig long btrfs_ioctl_trans_end(struct file *file)
3549f46b5a66SChristoph Hellwig {
3550496ad9aaSAl Viro 	struct inode *inode = file_inode(file);
3551f46b5a66SChristoph Hellwig 	struct btrfs_root *root = BTRFS_I(inode)->root;
3552f46b5a66SChristoph Hellwig 	struct btrfs_trans_handle *trans;
3553f46b5a66SChristoph Hellwig 
3554f46b5a66SChristoph Hellwig 	trans = file->private_data;
35551ab86aedSSage Weil 	if (!trans)
35561ab86aedSSage Weil 		return -EINVAL;
3557b214107eSChristoph Hellwig 	file->private_data = NULL;
35589ca9ee09SSage Weil 
35591ab86aedSSage Weil 	btrfs_end_transaction(trans, root);
35601ab86aedSSage Weil 
3561a4abeea4SJosef Bacik 	atomic_dec(&root->fs_info->open_ioctl_trans);
35629ca9ee09SSage Weil 
35632a79f17eSAl Viro 	mnt_drop_write_file(file);
35641ab86aedSSage Weil 	return 0;
3565f46b5a66SChristoph Hellwig }
3566f46b5a66SChristoph Hellwig 
35679a8c28beSMiao Xie static noinline long btrfs_ioctl_start_sync(struct btrfs_root *root,
35689a8c28beSMiao Xie 					    void __user *argp)
356946204592SSage Weil {
357046204592SSage Weil 	struct btrfs_trans_handle *trans;
357146204592SSage Weil 	u64 transid;
3572db5b493aSTsutomu Itoh 	int ret;
357346204592SSage Weil 
3574d4edf39bSMiao Xie 	trans = btrfs_attach_transaction_barrier(root);
3575ff7c1d33SMiao Xie 	if (IS_ERR(trans)) {
3576ff7c1d33SMiao Xie 		if (PTR_ERR(trans) != -ENOENT)
357798d5dc13STsutomu Itoh 			return PTR_ERR(trans);
3578ff7c1d33SMiao Xie 
3579ff7c1d33SMiao Xie 		/* No running transaction, don't bother */
3580ff7c1d33SMiao Xie 		transid = root->fs_info->last_trans_committed;
3581ff7c1d33SMiao Xie 		goto out;
3582ff7c1d33SMiao Xie 	}
358346204592SSage Weil 	transid = trans->transid;
3584db5b493aSTsutomu Itoh 	ret = btrfs_commit_transaction_async(trans, root, 0);
35858b2b2d3cSTsutomu Itoh 	if (ret) {
35868b2b2d3cSTsutomu Itoh 		btrfs_end_transaction(trans, root);
3587db5b493aSTsutomu Itoh 		return ret;
35888b2b2d3cSTsutomu Itoh 	}
3589ff7c1d33SMiao Xie out:
359046204592SSage Weil 	if (argp)
359146204592SSage Weil 		if (copy_to_user(argp, &transid, sizeof(transid)))
359246204592SSage Weil 			return -EFAULT;
359346204592SSage Weil 	return 0;
359446204592SSage Weil }
359546204592SSage Weil 
35969a8c28beSMiao Xie static noinline long btrfs_ioctl_wait_sync(struct btrfs_root *root,
35979a8c28beSMiao Xie 					   void __user *argp)
359846204592SSage Weil {
359946204592SSage Weil 	u64 transid;
360046204592SSage Weil 
360146204592SSage Weil 	if (argp) {
360246204592SSage Weil 		if (copy_from_user(&transid, argp, sizeof(transid)))
360346204592SSage Weil 			return -EFAULT;
360446204592SSage Weil 	} else {
360546204592SSage Weil 		transid = 0;  /* current trans */
360646204592SSage Weil 	}
360746204592SSage Weil 	return btrfs_wait_for_commit(root, transid);
360846204592SSage Weil }
360946204592SSage Weil 
3610b8e95489SMiao Xie static long btrfs_ioctl_scrub(struct file *file, void __user *arg)
3611475f6387SJan Schmidt {
3612496ad9aaSAl Viro 	struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
3613475f6387SJan Schmidt 	struct btrfs_ioctl_scrub_args *sa;
3614b8e95489SMiao Xie 	int ret;
3615475f6387SJan Schmidt 
3616475f6387SJan Schmidt 	if (!capable(CAP_SYS_ADMIN))
3617475f6387SJan Schmidt 		return -EPERM;
3618475f6387SJan Schmidt 
3619475f6387SJan Schmidt 	sa = memdup_user(arg, sizeof(*sa));
3620475f6387SJan Schmidt 	if (IS_ERR(sa))
3621475f6387SJan Schmidt 		return PTR_ERR(sa);
3622475f6387SJan Schmidt 
3623b8e95489SMiao Xie 	if (!(sa->flags & BTRFS_SCRUB_READONLY)) {
3624b8e95489SMiao Xie 		ret = mnt_want_write_file(file);
3625b8e95489SMiao Xie 		if (ret)
3626b8e95489SMiao Xie 			goto out;
3627b8e95489SMiao Xie 	}
3628b8e95489SMiao Xie 
3629aa1b8cd4SStefan Behrens 	ret = btrfs_scrub_dev(root->fs_info, sa->devid, sa->start, sa->end,
363063a212abSStefan Behrens 			      &sa->progress, sa->flags & BTRFS_SCRUB_READONLY,
363163a212abSStefan Behrens 			      0);
3632475f6387SJan Schmidt 
3633475f6387SJan Schmidt 	if (copy_to_user(arg, sa, sizeof(*sa)))
3634475f6387SJan Schmidt 		ret = -EFAULT;
3635475f6387SJan Schmidt 
3636b8e95489SMiao Xie 	if (!(sa->flags & BTRFS_SCRUB_READONLY))
3637b8e95489SMiao Xie 		mnt_drop_write_file(file);
3638b8e95489SMiao Xie out:
3639475f6387SJan Schmidt 	kfree(sa);
3640475f6387SJan Schmidt 	return ret;
3641475f6387SJan Schmidt }
3642475f6387SJan Schmidt 
3643475f6387SJan Schmidt static long btrfs_ioctl_scrub_cancel(struct btrfs_root *root, void __user *arg)
3644475f6387SJan Schmidt {
3645475f6387SJan Schmidt 	if (!capable(CAP_SYS_ADMIN))
3646475f6387SJan Schmidt 		return -EPERM;
3647475f6387SJan Schmidt 
3648aa1b8cd4SStefan Behrens 	return btrfs_scrub_cancel(root->fs_info);
3649475f6387SJan Schmidt }
3650475f6387SJan Schmidt 
3651475f6387SJan Schmidt static long btrfs_ioctl_scrub_progress(struct btrfs_root *root,
3652475f6387SJan Schmidt 				       void __user *arg)
3653475f6387SJan Schmidt {
3654475f6387SJan Schmidt 	struct btrfs_ioctl_scrub_args *sa;
3655475f6387SJan Schmidt 	int ret;
3656475f6387SJan Schmidt 
3657475f6387SJan Schmidt 	if (!capable(CAP_SYS_ADMIN))
3658475f6387SJan Schmidt 		return -EPERM;
3659475f6387SJan Schmidt 
3660475f6387SJan Schmidt 	sa = memdup_user(arg, sizeof(*sa));
3661475f6387SJan Schmidt 	if (IS_ERR(sa))
3662475f6387SJan Schmidt 		return PTR_ERR(sa);
3663475f6387SJan Schmidt 
3664475f6387SJan Schmidt 	ret = btrfs_scrub_progress(root, sa->devid, &sa->progress);
3665475f6387SJan Schmidt 
3666475f6387SJan Schmidt 	if (copy_to_user(arg, sa, sizeof(*sa)))
3667475f6387SJan Schmidt 		ret = -EFAULT;
3668475f6387SJan Schmidt 
3669475f6387SJan Schmidt 	kfree(sa);
3670475f6387SJan Schmidt 	return ret;
3671475f6387SJan Schmidt }
3672475f6387SJan Schmidt 
3673c11d2c23SStefan Behrens static long btrfs_ioctl_get_dev_stats(struct btrfs_root *root,
3674b27f7c0cSDavid Sterba 				      void __user *arg)
3675c11d2c23SStefan Behrens {
3676c11d2c23SStefan Behrens 	struct btrfs_ioctl_get_dev_stats *sa;
3677c11d2c23SStefan Behrens 	int ret;
3678c11d2c23SStefan Behrens 
3679c11d2c23SStefan Behrens 	sa = memdup_user(arg, sizeof(*sa));
3680c11d2c23SStefan Behrens 	if (IS_ERR(sa))
3681c11d2c23SStefan Behrens 		return PTR_ERR(sa);
3682c11d2c23SStefan Behrens 
3683b27f7c0cSDavid Sterba 	if ((sa->flags & BTRFS_DEV_STATS_RESET) && !capable(CAP_SYS_ADMIN)) {
3684b27f7c0cSDavid Sterba 		kfree(sa);
3685b27f7c0cSDavid Sterba 		return -EPERM;
3686b27f7c0cSDavid Sterba 	}
3687b27f7c0cSDavid Sterba 
3688b27f7c0cSDavid Sterba 	ret = btrfs_get_dev_stats(root, sa);
3689c11d2c23SStefan Behrens 
3690c11d2c23SStefan Behrens 	if (copy_to_user(arg, sa, sizeof(*sa)))
3691c11d2c23SStefan Behrens 		ret = -EFAULT;
3692c11d2c23SStefan Behrens 
3693c11d2c23SStefan Behrens 	kfree(sa);
3694c11d2c23SStefan Behrens 	return ret;
3695c11d2c23SStefan Behrens }
3696c11d2c23SStefan Behrens 
36973f6bcfbdSStefan Behrens static long btrfs_ioctl_dev_replace(struct btrfs_root *root, void __user *arg)
36983f6bcfbdSStefan Behrens {
36993f6bcfbdSStefan Behrens 	struct btrfs_ioctl_dev_replace_args *p;
37003f6bcfbdSStefan Behrens 	int ret;
37013f6bcfbdSStefan Behrens 
37023f6bcfbdSStefan Behrens 	if (!capable(CAP_SYS_ADMIN))
37033f6bcfbdSStefan Behrens 		return -EPERM;
37043f6bcfbdSStefan Behrens 
37053f6bcfbdSStefan Behrens 	p = memdup_user(arg, sizeof(*p));
37063f6bcfbdSStefan Behrens 	if (IS_ERR(p))
37073f6bcfbdSStefan Behrens 		return PTR_ERR(p);
37083f6bcfbdSStefan Behrens 
37093f6bcfbdSStefan Behrens 	switch (p->cmd) {
37103f6bcfbdSStefan Behrens 	case BTRFS_IOCTL_DEV_REPLACE_CMD_START:
3711adfa97cbSIlya Dryomov 		if (root->fs_info->sb->s_flags & MS_RDONLY) {
3712adfa97cbSIlya Dryomov 			ret = -EROFS;
3713adfa97cbSIlya Dryomov 			goto out;
3714adfa97cbSIlya Dryomov 		}
37153f6bcfbdSStefan Behrens 		if (atomic_xchg(
37163f6bcfbdSStefan Behrens 			&root->fs_info->mutually_exclusive_operation_running,
37173f6bcfbdSStefan Behrens 			1)) {
3718e57138b3SAnand Jain 			ret = BTRFS_ERROR_DEV_EXCL_RUN_IN_PROGRESS;
37193f6bcfbdSStefan Behrens 		} else {
37203f6bcfbdSStefan Behrens 			ret = btrfs_dev_replace_start(root, p);
37213f6bcfbdSStefan Behrens 			atomic_set(
37223f6bcfbdSStefan Behrens 			 &root->fs_info->mutually_exclusive_operation_running,
37233f6bcfbdSStefan Behrens 			 0);
37243f6bcfbdSStefan Behrens 		}
37253f6bcfbdSStefan Behrens 		break;
37263f6bcfbdSStefan Behrens 	case BTRFS_IOCTL_DEV_REPLACE_CMD_STATUS:
37273f6bcfbdSStefan Behrens 		btrfs_dev_replace_status(root->fs_info, p);
37283f6bcfbdSStefan Behrens 		ret = 0;
37293f6bcfbdSStefan Behrens 		break;
37303f6bcfbdSStefan Behrens 	case BTRFS_IOCTL_DEV_REPLACE_CMD_CANCEL:
37313f6bcfbdSStefan Behrens 		ret = btrfs_dev_replace_cancel(root->fs_info, p);
37323f6bcfbdSStefan Behrens 		break;
37333f6bcfbdSStefan Behrens 	default:
37343f6bcfbdSStefan Behrens 		ret = -EINVAL;
37353f6bcfbdSStefan Behrens 		break;
37363f6bcfbdSStefan Behrens 	}
37373f6bcfbdSStefan Behrens 
37383f6bcfbdSStefan Behrens 	if (copy_to_user(arg, p, sizeof(*p)))
37393f6bcfbdSStefan Behrens 		ret = -EFAULT;
3740adfa97cbSIlya Dryomov out:
37413f6bcfbdSStefan Behrens 	kfree(p);
37423f6bcfbdSStefan Behrens 	return ret;
37433f6bcfbdSStefan Behrens }
37443f6bcfbdSStefan Behrens 
3745d7728c96SJan Schmidt static long btrfs_ioctl_ino_to_path(struct btrfs_root *root, void __user *arg)
3746d7728c96SJan Schmidt {
3747d7728c96SJan Schmidt 	int ret = 0;
3748d7728c96SJan Schmidt 	int i;
3749740c3d22SChris Mason 	u64 rel_ptr;
3750d7728c96SJan Schmidt 	int size;
3751806468f8SChris Mason 	struct btrfs_ioctl_ino_path_args *ipa = NULL;
3752d7728c96SJan Schmidt 	struct inode_fs_paths *ipath = NULL;
3753d7728c96SJan Schmidt 	struct btrfs_path *path;
3754d7728c96SJan Schmidt 
375582b22ac8SKusanagi Kouichi 	if (!capable(CAP_DAC_READ_SEARCH))
3756d7728c96SJan Schmidt 		return -EPERM;
3757d7728c96SJan Schmidt 
3758d7728c96SJan Schmidt 	path = btrfs_alloc_path();
3759d7728c96SJan Schmidt 	if (!path) {
3760d7728c96SJan Schmidt 		ret = -ENOMEM;
3761d7728c96SJan Schmidt 		goto out;
3762d7728c96SJan Schmidt 	}
3763d7728c96SJan Schmidt 
3764d7728c96SJan Schmidt 	ipa = memdup_user(arg, sizeof(*ipa));
3765d7728c96SJan Schmidt 	if (IS_ERR(ipa)) {
3766d7728c96SJan Schmidt 		ret = PTR_ERR(ipa);
3767d7728c96SJan Schmidt 		ipa = NULL;
3768d7728c96SJan Schmidt 		goto out;
3769d7728c96SJan Schmidt 	}
3770d7728c96SJan Schmidt 
3771d7728c96SJan Schmidt 	size = min_t(u32, ipa->size, 4096);
3772d7728c96SJan Schmidt 	ipath = init_ipath(size, root, path);
3773d7728c96SJan Schmidt 	if (IS_ERR(ipath)) {
3774d7728c96SJan Schmidt 		ret = PTR_ERR(ipath);
3775d7728c96SJan Schmidt 		ipath = NULL;
3776d7728c96SJan Schmidt 		goto out;
3777d7728c96SJan Schmidt 	}
3778d7728c96SJan Schmidt 
3779d7728c96SJan Schmidt 	ret = paths_from_inode(ipa->inum, ipath);
3780d7728c96SJan Schmidt 	if (ret < 0)
3781d7728c96SJan Schmidt 		goto out;
3782d7728c96SJan Schmidt 
3783d7728c96SJan Schmidt 	for (i = 0; i < ipath->fspath->elem_cnt; ++i) {
3784745c4d8eSJeff Mahoney 		rel_ptr = ipath->fspath->val[i] -
3785745c4d8eSJeff Mahoney 			  (u64)(unsigned long)ipath->fspath->val;
3786740c3d22SChris Mason 		ipath->fspath->val[i] = rel_ptr;
3787d7728c96SJan Schmidt 	}
3788d7728c96SJan Schmidt 
3789745c4d8eSJeff Mahoney 	ret = copy_to_user((void *)(unsigned long)ipa->fspath,
3790745c4d8eSJeff Mahoney 			   (void *)(unsigned long)ipath->fspath, size);
3791d7728c96SJan Schmidt 	if (ret) {
3792d7728c96SJan Schmidt 		ret = -EFAULT;
3793d7728c96SJan Schmidt 		goto out;
3794d7728c96SJan Schmidt 	}
3795d7728c96SJan Schmidt 
3796d7728c96SJan Schmidt out:
3797d7728c96SJan Schmidt 	btrfs_free_path(path);
3798d7728c96SJan Schmidt 	free_ipath(ipath);
3799d7728c96SJan Schmidt 	kfree(ipa);
3800d7728c96SJan Schmidt 
3801d7728c96SJan Schmidt 	return ret;
3802d7728c96SJan Schmidt }
3803d7728c96SJan Schmidt 
3804d7728c96SJan Schmidt static int build_ino_list(u64 inum, u64 offset, u64 root, void *ctx)
3805d7728c96SJan Schmidt {
3806d7728c96SJan Schmidt 	struct btrfs_data_container *inodes = ctx;
3807d7728c96SJan Schmidt 	const size_t c = 3 * sizeof(u64);
3808d7728c96SJan Schmidt 
3809d7728c96SJan Schmidt 	if (inodes->bytes_left >= c) {
3810d7728c96SJan Schmidt 		inodes->bytes_left -= c;
3811d7728c96SJan Schmidt 		inodes->val[inodes->elem_cnt] = inum;
3812d7728c96SJan Schmidt 		inodes->val[inodes->elem_cnt + 1] = offset;
3813d7728c96SJan Schmidt 		inodes->val[inodes->elem_cnt + 2] = root;
3814d7728c96SJan Schmidt 		inodes->elem_cnt += 3;
3815d7728c96SJan Schmidt 	} else {
3816d7728c96SJan Schmidt 		inodes->bytes_missing += c - inodes->bytes_left;
3817d7728c96SJan Schmidt 		inodes->bytes_left = 0;
3818d7728c96SJan Schmidt 		inodes->elem_missed += 3;
3819d7728c96SJan Schmidt 	}
3820d7728c96SJan Schmidt 
3821d7728c96SJan Schmidt 	return 0;
3822d7728c96SJan Schmidt }
3823d7728c96SJan Schmidt 
3824d7728c96SJan Schmidt static long btrfs_ioctl_logical_to_ino(struct btrfs_root *root,
3825d7728c96SJan Schmidt 					void __user *arg)
3826d7728c96SJan Schmidt {
3827d7728c96SJan Schmidt 	int ret = 0;
3828d7728c96SJan Schmidt 	int size;
3829d7728c96SJan Schmidt 	struct btrfs_ioctl_logical_ino_args *loi;
3830d7728c96SJan Schmidt 	struct btrfs_data_container *inodes = NULL;
3831d7728c96SJan Schmidt 	struct btrfs_path *path = NULL;
3832d7728c96SJan Schmidt 
3833d7728c96SJan Schmidt 	if (!capable(CAP_SYS_ADMIN))
3834d7728c96SJan Schmidt 		return -EPERM;
3835d7728c96SJan Schmidt 
3836d7728c96SJan Schmidt 	loi = memdup_user(arg, sizeof(*loi));
3837d7728c96SJan Schmidt 	if (IS_ERR(loi)) {
3838d7728c96SJan Schmidt 		ret = PTR_ERR(loi);
3839d7728c96SJan Schmidt 		loi = NULL;
3840d7728c96SJan Schmidt 		goto out;
3841d7728c96SJan Schmidt 	}
3842d7728c96SJan Schmidt 
3843d7728c96SJan Schmidt 	path = btrfs_alloc_path();
3844d7728c96SJan Schmidt 	if (!path) {
3845d7728c96SJan Schmidt 		ret = -ENOMEM;
3846d7728c96SJan Schmidt 		goto out;
3847d7728c96SJan Schmidt 	}
3848d7728c96SJan Schmidt 
3849425d17a2SLiu Bo 	size = min_t(u32, loi->size, 64 * 1024);
3850d7728c96SJan Schmidt 	inodes = init_data_container(size);
3851d7728c96SJan Schmidt 	if (IS_ERR(inodes)) {
3852d7728c96SJan Schmidt 		ret = PTR_ERR(inodes);
3853d7728c96SJan Schmidt 		inodes = NULL;
3854d7728c96SJan Schmidt 		goto out;
3855d7728c96SJan Schmidt 	}
3856d7728c96SJan Schmidt 
3857df031f07SLiu Bo 	ret = iterate_inodes_from_logical(loi->logical, root->fs_info, path,
3858df031f07SLiu Bo 					  build_ino_list, inodes);
3859df031f07SLiu Bo 	if (ret == -EINVAL)
3860d7728c96SJan Schmidt 		ret = -ENOENT;
3861d7728c96SJan Schmidt 	if (ret < 0)
3862d7728c96SJan Schmidt 		goto out;
3863d7728c96SJan Schmidt 
3864745c4d8eSJeff Mahoney 	ret = copy_to_user((void *)(unsigned long)loi->inodes,
3865745c4d8eSJeff Mahoney 			   (void *)(unsigned long)inodes, size);
3866d7728c96SJan Schmidt 	if (ret)
3867d7728c96SJan Schmidt 		ret = -EFAULT;
3868d7728c96SJan Schmidt 
3869d7728c96SJan Schmidt out:
3870d7728c96SJan Schmidt 	btrfs_free_path(path);
3871425d17a2SLiu Bo 	vfree(inodes);
3872d7728c96SJan Schmidt 	kfree(loi);
3873d7728c96SJan Schmidt 
3874d7728c96SJan Schmidt 	return ret;
3875d7728c96SJan Schmidt }
3876d7728c96SJan Schmidt 
387719a39dceSIlya Dryomov void update_ioctl_balance_args(struct btrfs_fs_info *fs_info, int lock,
3878c9e9f97bSIlya Dryomov 			       struct btrfs_ioctl_balance_args *bargs)
3879c9e9f97bSIlya Dryomov {
3880c9e9f97bSIlya Dryomov 	struct btrfs_balance_control *bctl = fs_info->balance_ctl;
3881c9e9f97bSIlya Dryomov 
3882c9e9f97bSIlya Dryomov 	bargs->flags = bctl->flags;
3883c9e9f97bSIlya Dryomov 
3884837d5b6eSIlya Dryomov 	if (atomic_read(&fs_info->balance_running))
3885837d5b6eSIlya Dryomov 		bargs->state |= BTRFS_BALANCE_STATE_RUNNING;
3886837d5b6eSIlya Dryomov 	if (atomic_read(&fs_info->balance_pause_req))
3887837d5b6eSIlya Dryomov 		bargs->state |= BTRFS_BALANCE_STATE_PAUSE_REQ;
3888a7e99c69SIlya Dryomov 	if (atomic_read(&fs_info->balance_cancel_req))
3889a7e99c69SIlya Dryomov 		bargs->state |= BTRFS_BALANCE_STATE_CANCEL_REQ;
3890837d5b6eSIlya Dryomov 
3891c9e9f97bSIlya Dryomov 	memcpy(&bargs->data, &bctl->data, sizeof(bargs->data));
3892c9e9f97bSIlya Dryomov 	memcpy(&bargs->meta, &bctl->meta, sizeof(bargs->meta));
3893c9e9f97bSIlya Dryomov 	memcpy(&bargs->sys, &bctl->sys, sizeof(bargs->sys));
389419a39dceSIlya Dryomov 
389519a39dceSIlya Dryomov 	if (lock) {
389619a39dceSIlya Dryomov 		spin_lock(&fs_info->balance_lock);
389719a39dceSIlya Dryomov 		memcpy(&bargs->stat, &bctl->stat, sizeof(bargs->stat));
389819a39dceSIlya Dryomov 		spin_unlock(&fs_info->balance_lock);
389919a39dceSIlya Dryomov 	} else {
390019a39dceSIlya Dryomov 		memcpy(&bargs->stat, &bctl->stat, sizeof(bargs->stat));
390119a39dceSIlya Dryomov 	}
3902c9e9f97bSIlya Dryomov }
3903c9e9f97bSIlya Dryomov 
39049ba1f6e4SLiu Bo static long btrfs_ioctl_balance(struct file *file, void __user *arg)
3905c9e9f97bSIlya Dryomov {
3906496ad9aaSAl Viro 	struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
3907c9e9f97bSIlya Dryomov 	struct btrfs_fs_info *fs_info = root->fs_info;
3908c9e9f97bSIlya Dryomov 	struct btrfs_ioctl_balance_args *bargs;
3909c9e9f97bSIlya Dryomov 	struct btrfs_balance_control *bctl;
3910ed0fb78fSIlya Dryomov 	bool need_unlock; /* for mut. excl. ops lock */
3911c9e9f97bSIlya Dryomov 	int ret;
3912c9e9f97bSIlya Dryomov 
3913c9e9f97bSIlya Dryomov 	if (!capable(CAP_SYS_ADMIN))
3914c9e9f97bSIlya Dryomov 		return -EPERM;
3915c9e9f97bSIlya Dryomov 
3916e54bfa31SLiu Bo 	ret = mnt_want_write_file(file);
39179ba1f6e4SLiu Bo 	if (ret)
39189ba1f6e4SLiu Bo 		return ret;
39199ba1f6e4SLiu Bo 
3920ed0fb78fSIlya Dryomov again:
3921ed0fb78fSIlya Dryomov 	if (!atomic_xchg(&fs_info->mutually_exclusive_operation_running, 1)) {
3922c9e9f97bSIlya Dryomov 		mutex_lock(&fs_info->volume_mutex);
3923c9e9f97bSIlya Dryomov 		mutex_lock(&fs_info->balance_mutex);
3924ed0fb78fSIlya Dryomov 		need_unlock = true;
3925ed0fb78fSIlya Dryomov 		goto locked;
3926ed0fb78fSIlya Dryomov 	}
3927ed0fb78fSIlya Dryomov 
3928ed0fb78fSIlya Dryomov 	/*
3929ed0fb78fSIlya Dryomov 	 * mut. excl. ops lock is locked.  Three possibilites:
3930ed0fb78fSIlya Dryomov 	 *   (1) some other op is running
3931ed0fb78fSIlya Dryomov 	 *   (2) balance is running
3932ed0fb78fSIlya Dryomov 	 *   (3) balance is paused -- special case (think resume)
3933ed0fb78fSIlya Dryomov 	 */
3934ed0fb78fSIlya Dryomov 	mutex_lock(&fs_info->balance_mutex);
3935ed0fb78fSIlya Dryomov 	if (fs_info->balance_ctl) {
3936ed0fb78fSIlya Dryomov 		/* this is either (2) or (3) */
3937ed0fb78fSIlya Dryomov 		if (!atomic_read(&fs_info->balance_running)) {
3938ed0fb78fSIlya Dryomov 			mutex_unlock(&fs_info->balance_mutex);
3939ed0fb78fSIlya Dryomov 			if (!mutex_trylock(&fs_info->volume_mutex))
3940ed0fb78fSIlya Dryomov 				goto again;
3941ed0fb78fSIlya Dryomov 			mutex_lock(&fs_info->balance_mutex);
3942ed0fb78fSIlya Dryomov 
3943ed0fb78fSIlya Dryomov 			if (fs_info->balance_ctl &&
3944ed0fb78fSIlya Dryomov 			    !atomic_read(&fs_info->balance_running)) {
3945ed0fb78fSIlya Dryomov 				/* this is (3) */
3946ed0fb78fSIlya Dryomov 				need_unlock = false;
3947ed0fb78fSIlya Dryomov 				goto locked;
3948ed0fb78fSIlya Dryomov 			}
3949ed0fb78fSIlya Dryomov 
3950ed0fb78fSIlya Dryomov 			mutex_unlock(&fs_info->balance_mutex);
3951ed0fb78fSIlya Dryomov 			mutex_unlock(&fs_info->volume_mutex);
3952ed0fb78fSIlya Dryomov 			goto again;
3953ed0fb78fSIlya Dryomov 		} else {
3954ed0fb78fSIlya Dryomov 			/* this is (2) */
3955ed0fb78fSIlya Dryomov 			mutex_unlock(&fs_info->balance_mutex);
3956ed0fb78fSIlya Dryomov 			ret = -EINPROGRESS;
3957ed0fb78fSIlya Dryomov 			goto out;
3958ed0fb78fSIlya Dryomov 		}
3959ed0fb78fSIlya Dryomov 	} else {
3960ed0fb78fSIlya Dryomov 		/* this is (1) */
3961ed0fb78fSIlya Dryomov 		mutex_unlock(&fs_info->balance_mutex);
3962e57138b3SAnand Jain 		ret = BTRFS_ERROR_DEV_EXCL_RUN_IN_PROGRESS;
3963ed0fb78fSIlya Dryomov 		goto out;
3964ed0fb78fSIlya Dryomov 	}
3965ed0fb78fSIlya Dryomov 
3966ed0fb78fSIlya Dryomov locked:
3967ed0fb78fSIlya Dryomov 	BUG_ON(!atomic_read(&fs_info->mutually_exclusive_operation_running));
3968c9e9f97bSIlya Dryomov 
3969c9e9f97bSIlya Dryomov 	if (arg) {
3970c9e9f97bSIlya Dryomov 		bargs = memdup_user(arg, sizeof(*bargs));
3971c9e9f97bSIlya Dryomov 		if (IS_ERR(bargs)) {
3972c9e9f97bSIlya Dryomov 			ret = PTR_ERR(bargs);
3973ed0fb78fSIlya Dryomov 			goto out_unlock;
3974c9e9f97bSIlya Dryomov 		}
3975de322263SIlya Dryomov 
3976de322263SIlya Dryomov 		if (bargs->flags & BTRFS_BALANCE_RESUME) {
3977de322263SIlya Dryomov 			if (!fs_info->balance_ctl) {
3978de322263SIlya Dryomov 				ret = -ENOTCONN;
3979de322263SIlya Dryomov 				goto out_bargs;
3980de322263SIlya Dryomov 			}
3981de322263SIlya Dryomov 
3982de322263SIlya Dryomov 			bctl = fs_info->balance_ctl;
3983de322263SIlya Dryomov 			spin_lock(&fs_info->balance_lock);
3984de322263SIlya Dryomov 			bctl->flags |= BTRFS_BALANCE_RESUME;
3985de322263SIlya Dryomov 			spin_unlock(&fs_info->balance_lock);
3986de322263SIlya Dryomov 
3987de322263SIlya Dryomov 			goto do_balance;
3988de322263SIlya Dryomov 		}
3989c9e9f97bSIlya Dryomov 	} else {
3990c9e9f97bSIlya Dryomov 		bargs = NULL;
3991c9e9f97bSIlya Dryomov 	}
3992c9e9f97bSIlya Dryomov 
3993ed0fb78fSIlya Dryomov 	if (fs_info->balance_ctl) {
3994837d5b6eSIlya Dryomov 		ret = -EINPROGRESS;
3995837d5b6eSIlya Dryomov 		goto out_bargs;
3996837d5b6eSIlya Dryomov 	}
3997837d5b6eSIlya Dryomov 
3998c9e9f97bSIlya Dryomov 	bctl = kzalloc(sizeof(*bctl), GFP_NOFS);
3999c9e9f97bSIlya Dryomov 	if (!bctl) {
4000c9e9f97bSIlya Dryomov 		ret = -ENOMEM;
4001c9e9f97bSIlya Dryomov 		goto out_bargs;
4002c9e9f97bSIlya Dryomov 	}
4003c9e9f97bSIlya Dryomov 
4004c9e9f97bSIlya Dryomov 	bctl->fs_info = fs_info;
4005c9e9f97bSIlya Dryomov 	if (arg) {
4006c9e9f97bSIlya Dryomov 		memcpy(&bctl->data, &bargs->data, sizeof(bctl->data));
4007c9e9f97bSIlya Dryomov 		memcpy(&bctl->meta, &bargs->meta, sizeof(bctl->meta));
4008c9e9f97bSIlya Dryomov 		memcpy(&bctl->sys, &bargs->sys, sizeof(bctl->sys));
4009c9e9f97bSIlya Dryomov 
4010c9e9f97bSIlya Dryomov 		bctl->flags = bargs->flags;
4011f43ffb60SIlya Dryomov 	} else {
4012f43ffb60SIlya Dryomov 		/* balance everything - no filters */
4013f43ffb60SIlya Dryomov 		bctl->flags |= BTRFS_BALANCE_TYPE_MASK;
4014c9e9f97bSIlya Dryomov 	}
4015c9e9f97bSIlya Dryomov 
4016de322263SIlya Dryomov do_balance:
4017c9e9f97bSIlya Dryomov 	/*
4018ed0fb78fSIlya Dryomov 	 * Ownership of bctl and mutually_exclusive_operation_running
4019ed0fb78fSIlya Dryomov 	 * goes to to btrfs_balance.  bctl is freed in __cancel_balance,
4020ed0fb78fSIlya Dryomov 	 * or, if restriper was paused all the way until unmount, in
4021ed0fb78fSIlya Dryomov 	 * free_fs_info.  mutually_exclusive_operation_running is
4022ed0fb78fSIlya Dryomov 	 * cleared in __cancel_balance.
4023c9e9f97bSIlya Dryomov 	 */
4024ed0fb78fSIlya Dryomov 	need_unlock = false;
4025ed0fb78fSIlya Dryomov 
4026ed0fb78fSIlya Dryomov 	ret = btrfs_balance(bctl, bargs);
4027ed0fb78fSIlya Dryomov 
4028c9e9f97bSIlya Dryomov 	if (arg) {
4029c9e9f97bSIlya Dryomov 		if (copy_to_user(arg, bargs, sizeof(*bargs)))
4030c9e9f97bSIlya Dryomov 			ret = -EFAULT;
4031c9e9f97bSIlya Dryomov 	}
4032c9e9f97bSIlya Dryomov 
4033c9e9f97bSIlya Dryomov out_bargs:
4034c9e9f97bSIlya Dryomov 	kfree(bargs);
4035ed0fb78fSIlya Dryomov out_unlock:
4036c9e9f97bSIlya Dryomov 	mutex_unlock(&fs_info->balance_mutex);
4037c9e9f97bSIlya Dryomov 	mutex_unlock(&fs_info->volume_mutex);
4038ed0fb78fSIlya Dryomov 	if (need_unlock)
4039ed0fb78fSIlya Dryomov 		atomic_set(&fs_info->mutually_exclusive_operation_running, 0);
4040ed0fb78fSIlya Dryomov out:
4041e54bfa31SLiu Bo 	mnt_drop_write_file(file);
4042c9e9f97bSIlya Dryomov 	return ret;
4043c9e9f97bSIlya Dryomov }
4044c9e9f97bSIlya Dryomov 
4045837d5b6eSIlya Dryomov static long btrfs_ioctl_balance_ctl(struct btrfs_root *root, int cmd)
4046837d5b6eSIlya Dryomov {
4047837d5b6eSIlya Dryomov 	if (!capable(CAP_SYS_ADMIN))
4048837d5b6eSIlya Dryomov 		return -EPERM;
4049837d5b6eSIlya Dryomov 
4050837d5b6eSIlya Dryomov 	switch (cmd) {
4051837d5b6eSIlya Dryomov 	case BTRFS_BALANCE_CTL_PAUSE:
4052837d5b6eSIlya Dryomov 		return btrfs_pause_balance(root->fs_info);
4053a7e99c69SIlya Dryomov 	case BTRFS_BALANCE_CTL_CANCEL:
4054a7e99c69SIlya Dryomov 		return btrfs_cancel_balance(root->fs_info);
4055837d5b6eSIlya Dryomov 	}
4056837d5b6eSIlya Dryomov 
4057837d5b6eSIlya Dryomov 	return -EINVAL;
4058837d5b6eSIlya Dryomov }
4059837d5b6eSIlya Dryomov 
406019a39dceSIlya Dryomov static long btrfs_ioctl_balance_progress(struct btrfs_root *root,
406119a39dceSIlya Dryomov 					 void __user *arg)
406219a39dceSIlya Dryomov {
406319a39dceSIlya Dryomov 	struct btrfs_fs_info *fs_info = root->fs_info;
406419a39dceSIlya Dryomov 	struct btrfs_ioctl_balance_args *bargs;
406519a39dceSIlya Dryomov 	int ret = 0;
406619a39dceSIlya Dryomov 
406719a39dceSIlya Dryomov 	if (!capable(CAP_SYS_ADMIN))
406819a39dceSIlya Dryomov 		return -EPERM;
406919a39dceSIlya Dryomov 
407019a39dceSIlya Dryomov 	mutex_lock(&fs_info->balance_mutex);
407119a39dceSIlya Dryomov 	if (!fs_info->balance_ctl) {
407219a39dceSIlya Dryomov 		ret = -ENOTCONN;
407319a39dceSIlya Dryomov 		goto out;
407419a39dceSIlya Dryomov 	}
407519a39dceSIlya Dryomov 
407619a39dceSIlya Dryomov 	bargs = kzalloc(sizeof(*bargs), GFP_NOFS);
407719a39dceSIlya Dryomov 	if (!bargs) {
407819a39dceSIlya Dryomov 		ret = -ENOMEM;
407919a39dceSIlya Dryomov 		goto out;
408019a39dceSIlya Dryomov 	}
408119a39dceSIlya Dryomov 
408219a39dceSIlya Dryomov 	update_ioctl_balance_args(fs_info, 1, bargs);
408319a39dceSIlya Dryomov 
408419a39dceSIlya Dryomov 	if (copy_to_user(arg, bargs, sizeof(*bargs)))
408519a39dceSIlya Dryomov 		ret = -EFAULT;
408619a39dceSIlya Dryomov 
408719a39dceSIlya Dryomov 	kfree(bargs);
408819a39dceSIlya Dryomov out:
408919a39dceSIlya Dryomov 	mutex_unlock(&fs_info->balance_mutex);
409019a39dceSIlya Dryomov 	return ret;
409119a39dceSIlya Dryomov }
409219a39dceSIlya Dryomov 
4093905b0ddaSMiao Xie static long btrfs_ioctl_quota_ctl(struct file *file, void __user *arg)
40945d13a37bSArne Jansen {
4095496ad9aaSAl Viro 	struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
40965d13a37bSArne Jansen 	struct btrfs_ioctl_quota_ctl_args *sa;
40975d13a37bSArne Jansen 	struct btrfs_trans_handle *trans = NULL;
40985d13a37bSArne Jansen 	int ret;
40995d13a37bSArne Jansen 	int err;
41005d13a37bSArne Jansen 
41015d13a37bSArne Jansen 	if (!capable(CAP_SYS_ADMIN))
41025d13a37bSArne Jansen 		return -EPERM;
41035d13a37bSArne Jansen 
4104905b0ddaSMiao Xie 	ret = mnt_want_write_file(file);
4105905b0ddaSMiao Xie 	if (ret)
4106905b0ddaSMiao Xie 		return ret;
41075d13a37bSArne Jansen 
41085d13a37bSArne Jansen 	sa = memdup_user(arg, sizeof(*sa));
4109905b0ddaSMiao Xie 	if (IS_ERR(sa)) {
4110905b0ddaSMiao Xie 		ret = PTR_ERR(sa);
4111905b0ddaSMiao Xie 		goto drop_write;
4112905b0ddaSMiao Xie 	}
41135d13a37bSArne Jansen 
41147708f029SWang Shilong 	down_write(&root->fs_info->subvol_sem);
411592f183aaSWang Shilong 	trans = btrfs_start_transaction(root->fs_info->tree_root, 2);
41165d13a37bSArne Jansen 	if (IS_ERR(trans)) {
41175d13a37bSArne Jansen 		ret = PTR_ERR(trans);
41185d13a37bSArne Jansen 		goto out;
41195d13a37bSArne Jansen 	}
41205d13a37bSArne Jansen 
41215d13a37bSArne Jansen 	switch (sa->cmd) {
41225d13a37bSArne Jansen 	case BTRFS_QUOTA_CTL_ENABLE:
41235d13a37bSArne Jansen 		ret = btrfs_quota_enable(trans, root->fs_info);
41245d13a37bSArne Jansen 		break;
41255d13a37bSArne Jansen 	case BTRFS_QUOTA_CTL_DISABLE:
41265d13a37bSArne Jansen 		ret = btrfs_quota_disable(trans, root->fs_info);
41275d13a37bSArne Jansen 		break;
41285d13a37bSArne Jansen 	default:
41295d13a37bSArne Jansen 		ret = -EINVAL;
41305d13a37bSArne Jansen 		break;
41315d13a37bSArne Jansen 	}
41325d13a37bSArne Jansen 
413392f183aaSWang Shilong 	err = btrfs_commit_transaction(trans, root->fs_info->tree_root);
41345d13a37bSArne Jansen 	if (err && !ret)
41355d13a37bSArne Jansen 		ret = err;
41365d13a37bSArne Jansen out:
41375d13a37bSArne Jansen 	kfree(sa);
41387708f029SWang Shilong 	up_write(&root->fs_info->subvol_sem);
4139905b0ddaSMiao Xie drop_write:
4140905b0ddaSMiao Xie 	mnt_drop_write_file(file);
41415d13a37bSArne Jansen 	return ret;
41425d13a37bSArne Jansen }
41435d13a37bSArne Jansen 
4144905b0ddaSMiao Xie static long btrfs_ioctl_qgroup_assign(struct file *file, void __user *arg)
41455d13a37bSArne Jansen {
4146496ad9aaSAl Viro 	struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
41475d13a37bSArne Jansen 	struct btrfs_ioctl_qgroup_assign_args *sa;
41485d13a37bSArne Jansen 	struct btrfs_trans_handle *trans;
41495d13a37bSArne Jansen 	int ret;
41505d13a37bSArne Jansen 	int err;
41515d13a37bSArne Jansen 
41525d13a37bSArne Jansen 	if (!capable(CAP_SYS_ADMIN))
41535d13a37bSArne Jansen 		return -EPERM;
41545d13a37bSArne Jansen 
4155905b0ddaSMiao Xie 	ret = mnt_want_write_file(file);
4156905b0ddaSMiao Xie 	if (ret)
4157905b0ddaSMiao Xie 		return ret;
41585d13a37bSArne Jansen 
41595d13a37bSArne Jansen 	sa = memdup_user(arg, sizeof(*sa));
4160905b0ddaSMiao Xie 	if (IS_ERR(sa)) {
4161905b0ddaSMiao Xie 		ret = PTR_ERR(sa);
4162905b0ddaSMiao Xie 		goto drop_write;
4163905b0ddaSMiao Xie 	}
41645d13a37bSArne Jansen 
41655d13a37bSArne Jansen 	trans = btrfs_join_transaction(root);
41665d13a37bSArne Jansen 	if (IS_ERR(trans)) {
41675d13a37bSArne Jansen 		ret = PTR_ERR(trans);
41685d13a37bSArne Jansen 		goto out;
41695d13a37bSArne Jansen 	}
41705d13a37bSArne Jansen 
41715d13a37bSArne Jansen 	/* FIXME: check if the IDs really exist */
41725d13a37bSArne Jansen 	if (sa->assign) {
41735d13a37bSArne Jansen 		ret = btrfs_add_qgroup_relation(trans, root->fs_info,
41745d13a37bSArne Jansen 						sa->src, sa->dst);
41755d13a37bSArne Jansen 	} else {
41765d13a37bSArne Jansen 		ret = btrfs_del_qgroup_relation(trans, root->fs_info,
41775d13a37bSArne Jansen 						sa->src, sa->dst);
41785d13a37bSArne Jansen 	}
41795d13a37bSArne Jansen 
41805d13a37bSArne Jansen 	err = btrfs_end_transaction(trans, root);
41815d13a37bSArne Jansen 	if (err && !ret)
41825d13a37bSArne Jansen 		ret = err;
41835d13a37bSArne Jansen 
41845d13a37bSArne Jansen out:
41855d13a37bSArne Jansen 	kfree(sa);
4186905b0ddaSMiao Xie drop_write:
4187905b0ddaSMiao Xie 	mnt_drop_write_file(file);
41885d13a37bSArne Jansen 	return ret;
41895d13a37bSArne Jansen }
41905d13a37bSArne Jansen 
4191905b0ddaSMiao Xie static long btrfs_ioctl_qgroup_create(struct file *file, void __user *arg)
41925d13a37bSArne Jansen {
4193496ad9aaSAl Viro 	struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
41945d13a37bSArne Jansen 	struct btrfs_ioctl_qgroup_create_args *sa;
41955d13a37bSArne Jansen 	struct btrfs_trans_handle *trans;
41965d13a37bSArne Jansen 	int ret;
41975d13a37bSArne Jansen 	int err;
41985d13a37bSArne Jansen 
41995d13a37bSArne Jansen 	if (!capable(CAP_SYS_ADMIN))
42005d13a37bSArne Jansen 		return -EPERM;
42015d13a37bSArne Jansen 
4202905b0ddaSMiao Xie 	ret = mnt_want_write_file(file);
4203905b0ddaSMiao Xie 	if (ret)
4204905b0ddaSMiao Xie 		return ret;
42055d13a37bSArne Jansen 
42065d13a37bSArne Jansen 	sa = memdup_user(arg, sizeof(*sa));
4207905b0ddaSMiao Xie 	if (IS_ERR(sa)) {
4208905b0ddaSMiao Xie 		ret = PTR_ERR(sa);
4209905b0ddaSMiao Xie 		goto drop_write;
4210905b0ddaSMiao Xie 	}
42115d13a37bSArne Jansen 
4212d86e56cfSMiao Xie 	if (!sa->qgroupid) {
4213d86e56cfSMiao Xie 		ret = -EINVAL;
4214d86e56cfSMiao Xie 		goto out;
4215d86e56cfSMiao Xie 	}
4216d86e56cfSMiao Xie 
42175d13a37bSArne Jansen 	trans = btrfs_join_transaction(root);
42185d13a37bSArne Jansen 	if (IS_ERR(trans)) {
42195d13a37bSArne Jansen 		ret = PTR_ERR(trans);
42205d13a37bSArne Jansen 		goto out;
42215d13a37bSArne Jansen 	}
42225d13a37bSArne Jansen 
42235d13a37bSArne Jansen 	/* FIXME: check if the IDs really exist */
42245d13a37bSArne Jansen 	if (sa->create) {
42255d13a37bSArne Jansen 		ret = btrfs_create_qgroup(trans, root->fs_info, sa->qgroupid,
42265d13a37bSArne Jansen 					  NULL);
42275d13a37bSArne Jansen 	} else {
42285d13a37bSArne Jansen 		ret = btrfs_remove_qgroup(trans, root->fs_info, sa->qgroupid);
42295d13a37bSArne Jansen 	}
42305d13a37bSArne Jansen 
42315d13a37bSArne Jansen 	err = btrfs_end_transaction(trans, root);
42325d13a37bSArne Jansen 	if (err && !ret)
42335d13a37bSArne Jansen 		ret = err;
42345d13a37bSArne Jansen 
42355d13a37bSArne Jansen out:
42365d13a37bSArne Jansen 	kfree(sa);
4237905b0ddaSMiao Xie drop_write:
4238905b0ddaSMiao Xie 	mnt_drop_write_file(file);
42395d13a37bSArne Jansen 	return ret;
42405d13a37bSArne Jansen }
42415d13a37bSArne Jansen 
4242905b0ddaSMiao Xie static long btrfs_ioctl_qgroup_limit(struct file *file, void __user *arg)
42435d13a37bSArne Jansen {
4244496ad9aaSAl Viro 	struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
42455d13a37bSArne Jansen 	struct btrfs_ioctl_qgroup_limit_args *sa;
42465d13a37bSArne Jansen 	struct btrfs_trans_handle *trans;
42475d13a37bSArne Jansen 	int ret;
42485d13a37bSArne Jansen 	int err;
42495d13a37bSArne Jansen 	u64 qgroupid;
42505d13a37bSArne Jansen 
42515d13a37bSArne Jansen 	if (!capable(CAP_SYS_ADMIN))
42525d13a37bSArne Jansen 		return -EPERM;
42535d13a37bSArne Jansen 
4254905b0ddaSMiao Xie 	ret = mnt_want_write_file(file);
4255905b0ddaSMiao Xie 	if (ret)
4256905b0ddaSMiao Xie 		return ret;
42575d13a37bSArne Jansen 
42585d13a37bSArne Jansen 	sa = memdup_user(arg, sizeof(*sa));
4259905b0ddaSMiao Xie 	if (IS_ERR(sa)) {
4260905b0ddaSMiao Xie 		ret = PTR_ERR(sa);
4261905b0ddaSMiao Xie 		goto drop_write;
4262905b0ddaSMiao Xie 	}
42635d13a37bSArne Jansen 
42645d13a37bSArne Jansen 	trans = btrfs_join_transaction(root);
42655d13a37bSArne Jansen 	if (IS_ERR(trans)) {
42665d13a37bSArne Jansen 		ret = PTR_ERR(trans);
42675d13a37bSArne Jansen 		goto out;
42685d13a37bSArne Jansen 	}
42695d13a37bSArne Jansen 
42705d13a37bSArne Jansen 	qgroupid = sa->qgroupid;
42715d13a37bSArne Jansen 	if (!qgroupid) {
42725d13a37bSArne Jansen 		/* take the current subvol as qgroup */
42735d13a37bSArne Jansen 		qgroupid = root->root_key.objectid;
42745d13a37bSArne Jansen 	}
42755d13a37bSArne Jansen 
42765d13a37bSArne Jansen 	/* FIXME: check if the IDs really exist */
42775d13a37bSArne Jansen 	ret = btrfs_limit_qgroup(trans, root->fs_info, qgroupid, &sa->lim);
42785d13a37bSArne Jansen 
42795d13a37bSArne Jansen 	err = btrfs_end_transaction(trans, root);
42805d13a37bSArne Jansen 	if (err && !ret)
42815d13a37bSArne Jansen 		ret = err;
42825d13a37bSArne Jansen 
42835d13a37bSArne Jansen out:
42845d13a37bSArne Jansen 	kfree(sa);
4285905b0ddaSMiao Xie drop_write:
4286905b0ddaSMiao Xie 	mnt_drop_write_file(file);
42875d13a37bSArne Jansen 	return ret;
42885d13a37bSArne Jansen }
42895d13a37bSArne Jansen 
42902f232036SJan Schmidt static long btrfs_ioctl_quota_rescan(struct file *file, void __user *arg)
42912f232036SJan Schmidt {
42926d0379ecSAl Viro 	struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
42932f232036SJan Schmidt 	struct btrfs_ioctl_quota_rescan_args *qsa;
42942f232036SJan Schmidt 	int ret;
42952f232036SJan Schmidt 
42962f232036SJan Schmidt 	if (!capable(CAP_SYS_ADMIN))
42972f232036SJan Schmidt 		return -EPERM;
42982f232036SJan Schmidt 
42992f232036SJan Schmidt 	ret = mnt_want_write_file(file);
43002f232036SJan Schmidt 	if (ret)
43012f232036SJan Schmidt 		return ret;
43022f232036SJan Schmidt 
43032f232036SJan Schmidt 	qsa = memdup_user(arg, sizeof(*qsa));
43042f232036SJan Schmidt 	if (IS_ERR(qsa)) {
43052f232036SJan Schmidt 		ret = PTR_ERR(qsa);
43062f232036SJan Schmidt 		goto drop_write;
43072f232036SJan Schmidt 	}
43082f232036SJan Schmidt 
43092f232036SJan Schmidt 	if (qsa->flags) {
43102f232036SJan Schmidt 		ret = -EINVAL;
43112f232036SJan Schmidt 		goto out;
43122f232036SJan Schmidt 	}
43132f232036SJan Schmidt 
43142f232036SJan Schmidt 	ret = btrfs_qgroup_rescan(root->fs_info);
43152f232036SJan Schmidt 
43162f232036SJan Schmidt out:
43172f232036SJan Schmidt 	kfree(qsa);
43182f232036SJan Schmidt drop_write:
43192f232036SJan Schmidt 	mnt_drop_write_file(file);
43202f232036SJan Schmidt 	return ret;
43212f232036SJan Schmidt }
43222f232036SJan Schmidt 
43232f232036SJan Schmidt static long btrfs_ioctl_quota_rescan_status(struct file *file, void __user *arg)
43242f232036SJan Schmidt {
43256d0379ecSAl Viro 	struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
43262f232036SJan Schmidt 	struct btrfs_ioctl_quota_rescan_args *qsa;
43272f232036SJan Schmidt 	int ret = 0;
43282f232036SJan Schmidt 
43292f232036SJan Schmidt 	if (!capable(CAP_SYS_ADMIN))
43302f232036SJan Schmidt 		return -EPERM;
43312f232036SJan Schmidt 
43322f232036SJan Schmidt 	qsa = kzalloc(sizeof(*qsa), GFP_NOFS);
43332f232036SJan Schmidt 	if (!qsa)
43342f232036SJan Schmidt 		return -ENOMEM;
43352f232036SJan Schmidt 
43362f232036SJan Schmidt 	if (root->fs_info->qgroup_flags & BTRFS_QGROUP_STATUS_FLAG_RESCAN) {
43372f232036SJan Schmidt 		qsa->flags = 1;
43382f232036SJan Schmidt 		qsa->progress = root->fs_info->qgroup_rescan_progress.objectid;
43392f232036SJan Schmidt 	}
43402f232036SJan Schmidt 
43412f232036SJan Schmidt 	if (copy_to_user(arg, qsa, sizeof(*qsa)))
43422f232036SJan Schmidt 		ret = -EFAULT;
43432f232036SJan Schmidt 
43442f232036SJan Schmidt 	kfree(qsa);
43452f232036SJan Schmidt 	return ret;
43462f232036SJan Schmidt }
43472f232036SJan Schmidt 
434857254b6eSJan Schmidt static long btrfs_ioctl_quota_rescan_wait(struct file *file, void __user *arg)
434957254b6eSJan Schmidt {
435054563d41SAl Viro 	struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
435157254b6eSJan Schmidt 
435257254b6eSJan Schmidt 	if (!capable(CAP_SYS_ADMIN))
435357254b6eSJan Schmidt 		return -EPERM;
435457254b6eSJan Schmidt 
435557254b6eSJan Schmidt 	return btrfs_qgroup_wait_for_completion(root->fs_info);
435657254b6eSJan Schmidt }
435757254b6eSJan Schmidt 
43588ea05e3aSAlexander Block static long btrfs_ioctl_set_received_subvol(struct file *file,
43598ea05e3aSAlexander Block 					    void __user *arg)
43608ea05e3aSAlexander Block {
43618ea05e3aSAlexander Block 	struct btrfs_ioctl_received_subvol_args *sa = NULL;
4362496ad9aaSAl Viro 	struct inode *inode = file_inode(file);
43638ea05e3aSAlexander Block 	struct btrfs_root *root = BTRFS_I(inode)->root;
43648ea05e3aSAlexander Block 	struct btrfs_root_item *root_item = &root->root_item;
43658ea05e3aSAlexander Block 	struct btrfs_trans_handle *trans;
43668ea05e3aSAlexander Block 	struct timespec ct = CURRENT_TIME;
43678ea05e3aSAlexander Block 	int ret = 0;
4368dd5f9615SStefan Behrens 	int received_uuid_changed;
43698ea05e3aSAlexander Block 
43708ea05e3aSAlexander Block 	ret = mnt_want_write_file(file);
43718ea05e3aSAlexander Block 	if (ret < 0)
43728ea05e3aSAlexander Block 		return ret;
43738ea05e3aSAlexander Block 
43748ea05e3aSAlexander Block 	down_write(&root->fs_info->subvol_sem);
43758ea05e3aSAlexander Block 
43768ea05e3aSAlexander Block 	if (btrfs_ino(inode) != BTRFS_FIRST_FREE_OBJECTID) {
43778ea05e3aSAlexander Block 		ret = -EINVAL;
43788ea05e3aSAlexander Block 		goto out;
43798ea05e3aSAlexander Block 	}
43808ea05e3aSAlexander Block 
43818ea05e3aSAlexander Block 	if (btrfs_root_readonly(root)) {
43828ea05e3aSAlexander Block 		ret = -EROFS;
43838ea05e3aSAlexander Block 		goto out;
43848ea05e3aSAlexander Block 	}
43858ea05e3aSAlexander Block 
43868ea05e3aSAlexander Block 	if (!inode_owner_or_capable(inode)) {
43878ea05e3aSAlexander Block 		ret = -EACCES;
43888ea05e3aSAlexander Block 		goto out;
43898ea05e3aSAlexander Block 	}
43908ea05e3aSAlexander Block 
43918ea05e3aSAlexander Block 	sa = memdup_user(arg, sizeof(*sa));
43928ea05e3aSAlexander Block 	if (IS_ERR(sa)) {
43938ea05e3aSAlexander Block 		ret = PTR_ERR(sa);
43948ea05e3aSAlexander Block 		sa = NULL;
43958ea05e3aSAlexander Block 		goto out;
43968ea05e3aSAlexander Block 	}
43978ea05e3aSAlexander Block 
4398dd5f9615SStefan Behrens 	/*
4399dd5f9615SStefan Behrens 	 * 1 - root item
4400dd5f9615SStefan Behrens 	 * 2 - uuid items (received uuid + subvol uuid)
4401dd5f9615SStefan Behrens 	 */
4402dd5f9615SStefan Behrens 	trans = btrfs_start_transaction(root, 3);
44038ea05e3aSAlexander Block 	if (IS_ERR(trans)) {
44048ea05e3aSAlexander Block 		ret = PTR_ERR(trans);
44058ea05e3aSAlexander Block 		trans = NULL;
44068ea05e3aSAlexander Block 		goto out;
44078ea05e3aSAlexander Block 	}
44088ea05e3aSAlexander Block 
44098ea05e3aSAlexander Block 	sa->rtransid = trans->transid;
44108ea05e3aSAlexander Block 	sa->rtime.sec = ct.tv_sec;
44118ea05e3aSAlexander Block 	sa->rtime.nsec = ct.tv_nsec;
44128ea05e3aSAlexander Block 
4413dd5f9615SStefan Behrens 	received_uuid_changed = memcmp(root_item->received_uuid, sa->uuid,
4414dd5f9615SStefan Behrens 				       BTRFS_UUID_SIZE);
4415dd5f9615SStefan Behrens 	if (received_uuid_changed &&
4416dd5f9615SStefan Behrens 	    !btrfs_is_empty_uuid(root_item->received_uuid))
4417dd5f9615SStefan Behrens 		btrfs_uuid_tree_rem(trans, root->fs_info->uuid_root,
4418dd5f9615SStefan Behrens 				    root_item->received_uuid,
4419dd5f9615SStefan Behrens 				    BTRFS_UUID_KEY_RECEIVED_SUBVOL,
4420dd5f9615SStefan Behrens 				    root->root_key.objectid);
44218ea05e3aSAlexander Block 	memcpy(root_item->received_uuid, sa->uuid, BTRFS_UUID_SIZE);
44228ea05e3aSAlexander Block 	btrfs_set_root_stransid(root_item, sa->stransid);
44238ea05e3aSAlexander Block 	btrfs_set_root_rtransid(root_item, sa->rtransid);
44243cae210fSQu Wenruo 	btrfs_set_stack_timespec_sec(&root_item->stime, sa->stime.sec);
44253cae210fSQu Wenruo 	btrfs_set_stack_timespec_nsec(&root_item->stime, sa->stime.nsec);
44263cae210fSQu Wenruo 	btrfs_set_stack_timespec_sec(&root_item->rtime, sa->rtime.sec);
44273cae210fSQu Wenruo 	btrfs_set_stack_timespec_nsec(&root_item->rtime, sa->rtime.nsec);
44288ea05e3aSAlexander Block 
44298ea05e3aSAlexander Block 	ret = btrfs_update_root(trans, root->fs_info->tree_root,
44308ea05e3aSAlexander Block 				&root->root_key, &root->root_item);
44318ea05e3aSAlexander Block 	if (ret < 0) {
44328ea05e3aSAlexander Block 		btrfs_end_transaction(trans, root);
44338ea05e3aSAlexander Block 		goto out;
4434dd5f9615SStefan Behrens 	}
4435dd5f9615SStefan Behrens 	if (received_uuid_changed && !btrfs_is_empty_uuid(sa->uuid)) {
4436dd5f9615SStefan Behrens 		ret = btrfs_uuid_tree_add(trans, root->fs_info->uuid_root,
4437dd5f9615SStefan Behrens 					  sa->uuid,
4438dd5f9615SStefan Behrens 					  BTRFS_UUID_KEY_RECEIVED_SUBVOL,
4439dd5f9615SStefan Behrens 					  root->root_key.objectid);
4440dd5f9615SStefan Behrens 		if (ret < 0 && ret != -EEXIST) {
4441dd5f9615SStefan Behrens 			btrfs_abort_transaction(trans, root, ret);
4442dd5f9615SStefan Behrens 			goto out;
4443dd5f9615SStefan Behrens 		}
4444dd5f9615SStefan Behrens 	}
44458ea05e3aSAlexander Block 	ret = btrfs_commit_transaction(trans, root);
4446dd5f9615SStefan Behrens 	if (ret < 0) {
4447dd5f9615SStefan Behrens 		btrfs_abort_transaction(trans, root, ret);
44488ea05e3aSAlexander Block 		goto out;
44498ea05e3aSAlexander Block 	}
44508ea05e3aSAlexander Block 
44518ea05e3aSAlexander Block 	ret = copy_to_user(arg, sa, sizeof(*sa));
44528ea05e3aSAlexander Block 	if (ret)
44538ea05e3aSAlexander Block 		ret = -EFAULT;
44548ea05e3aSAlexander Block 
44558ea05e3aSAlexander Block out:
44568ea05e3aSAlexander Block 	kfree(sa);
44578ea05e3aSAlexander Block 	up_write(&root->fs_info->subvol_sem);
44588ea05e3aSAlexander Block 	mnt_drop_write_file(file);
44598ea05e3aSAlexander Block 	return ret;
44608ea05e3aSAlexander Block }
44618ea05e3aSAlexander Block 
4462867ab667Sjeff.liu static int btrfs_ioctl_get_fslabel(struct file *file, void __user *arg)
4463867ab667Sjeff.liu {
44646d0379ecSAl Viro 	struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
4465a1b83ac5SAnand Jain 	size_t len;
4466867ab667Sjeff.liu 	int ret;
4467a1b83ac5SAnand Jain 	char label[BTRFS_LABEL_SIZE];
4468a1b83ac5SAnand Jain 
4469a1b83ac5SAnand Jain 	spin_lock(&root->fs_info->super_lock);
4470a1b83ac5SAnand Jain 	memcpy(label, root->fs_info->super_copy->label, BTRFS_LABEL_SIZE);
4471a1b83ac5SAnand Jain 	spin_unlock(&root->fs_info->super_lock);
4472a1b83ac5SAnand Jain 
4473a1b83ac5SAnand Jain 	len = strnlen(label, BTRFS_LABEL_SIZE);
4474867ab667Sjeff.liu 
4475867ab667Sjeff.liu 	if (len == BTRFS_LABEL_SIZE) {
4476efe120a0SFrank Holton 		btrfs_warn(root->fs_info,
4477efe120a0SFrank Holton 			"label is too long, return the first %zu bytes", --len);
4478867ab667Sjeff.liu 	}
4479867ab667Sjeff.liu 
4480867ab667Sjeff.liu 	ret = copy_to_user(arg, label, len);
4481867ab667Sjeff.liu 
4482867ab667Sjeff.liu 	return ret ? -EFAULT : 0;
4483867ab667Sjeff.liu }
4484867ab667Sjeff.liu 
4485a8bfd4abSjeff.liu static int btrfs_ioctl_set_fslabel(struct file *file, void __user *arg)
4486a8bfd4abSjeff.liu {
44876d0379ecSAl Viro 	struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
4488a8bfd4abSjeff.liu 	struct btrfs_super_block *super_block = root->fs_info->super_copy;
4489a8bfd4abSjeff.liu 	struct btrfs_trans_handle *trans;
4490a8bfd4abSjeff.liu 	char label[BTRFS_LABEL_SIZE];
4491a8bfd4abSjeff.liu 	int ret;
4492a8bfd4abSjeff.liu 
4493a8bfd4abSjeff.liu 	if (!capable(CAP_SYS_ADMIN))
4494a8bfd4abSjeff.liu 		return -EPERM;
4495a8bfd4abSjeff.liu 
4496a8bfd4abSjeff.liu 	if (copy_from_user(label, arg, sizeof(label)))
4497a8bfd4abSjeff.liu 		return -EFAULT;
4498a8bfd4abSjeff.liu 
4499a8bfd4abSjeff.liu 	if (strnlen(label, BTRFS_LABEL_SIZE) == BTRFS_LABEL_SIZE) {
4500efe120a0SFrank Holton 		btrfs_err(root->fs_info, "unable to set label with more than %d bytes",
4501a8bfd4abSjeff.liu 		       BTRFS_LABEL_SIZE - 1);
4502a8bfd4abSjeff.liu 		return -EINVAL;
4503a8bfd4abSjeff.liu 	}
4504a8bfd4abSjeff.liu 
4505a8bfd4abSjeff.liu 	ret = mnt_want_write_file(file);
4506a8bfd4abSjeff.liu 	if (ret)
4507a8bfd4abSjeff.liu 		return ret;
4508a8bfd4abSjeff.liu 
4509a8bfd4abSjeff.liu 	trans = btrfs_start_transaction(root, 0);
4510a8bfd4abSjeff.liu 	if (IS_ERR(trans)) {
4511a8bfd4abSjeff.liu 		ret = PTR_ERR(trans);
4512a8bfd4abSjeff.liu 		goto out_unlock;
4513a8bfd4abSjeff.liu 	}
4514a8bfd4abSjeff.liu 
4515a1b83ac5SAnand Jain 	spin_lock(&root->fs_info->super_lock);
4516a8bfd4abSjeff.liu 	strcpy(super_block->label, label);
4517a1b83ac5SAnand Jain 	spin_unlock(&root->fs_info->super_lock);
4518a8bfd4abSjeff.liu 	ret = btrfs_end_transaction(trans, root);
4519a8bfd4abSjeff.liu 
4520a8bfd4abSjeff.liu out_unlock:
4521a8bfd4abSjeff.liu 	mnt_drop_write_file(file);
4522a8bfd4abSjeff.liu 	return ret;
4523a8bfd4abSjeff.liu }
4524a8bfd4abSjeff.liu 
45252eaa055fSJeff Mahoney #define INIT_FEATURE_FLAGS(suffix) \
45262eaa055fSJeff Mahoney 	{ .compat_flags = BTRFS_FEATURE_COMPAT_##suffix, \
45272eaa055fSJeff Mahoney 	  .compat_ro_flags = BTRFS_FEATURE_COMPAT_RO_##suffix, \
45282eaa055fSJeff Mahoney 	  .incompat_flags = BTRFS_FEATURE_INCOMPAT_##suffix }
45292eaa055fSJeff Mahoney 
45302eaa055fSJeff Mahoney static int btrfs_ioctl_get_supported_features(struct file *file,
45312eaa055fSJeff Mahoney 					      void __user *arg)
45322eaa055fSJeff Mahoney {
45332eaa055fSJeff Mahoney 	static struct btrfs_ioctl_feature_flags features[3] = {
45342eaa055fSJeff Mahoney 		INIT_FEATURE_FLAGS(SUPP),
45352eaa055fSJeff Mahoney 		INIT_FEATURE_FLAGS(SAFE_SET),
45362eaa055fSJeff Mahoney 		INIT_FEATURE_FLAGS(SAFE_CLEAR)
45372eaa055fSJeff Mahoney 	};
45382eaa055fSJeff Mahoney 
45392eaa055fSJeff Mahoney 	if (copy_to_user(arg, &features, sizeof(features)))
45402eaa055fSJeff Mahoney 		return -EFAULT;
45412eaa055fSJeff Mahoney 
45422eaa055fSJeff Mahoney 	return 0;
45432eaa055fSJeff Mahoney }
45442eaa055fSJeff Mahoney 
45452eaa055fSJeff Mahoney static int btrfs_ioctl_get_features(struct file *file, void __user *arg)
45462eaa055fSJeff Mahoney {
45472eaa055fSJeff Mahoney 	struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
45482eaa055fSJeff Mahoney 	struct btrfs_super_block *super_block = root->fs_info->super_copy;
45492eaa055fSJeff Mahoney 	struct btrfs_ioctl_feature_flags features;
45502eaa055fSJeff Mahoney 
45512eaa055fSJeff Mahoney 	features.compat_flags = btrfs_super_compat_flags(super_block);
45522eaa055fSJeff Mahoney 	features.compat_ro_flags = btrfs_super_compat_ro_flags(super_block);
45532eaa055fSJeff Mahoney 	features.incompat_flags = btrfs_super_incompat_flags(super_block);
45542eaa055fSJeff Mahoney 
45552eaa055fSJeff Mahoney 	if (copy_to_user(arg, &features, sizeof(features)))
45562eaa055fSJeff Mahoney 		return -EFAULT;
45572eaa055fSJeff Mahoney 
45582eaa055fSJeff Mahoney 	return 0;
45592eaa055fSJeff Mahoney }
45602eaa055fSJeff Mahoney 
45613b02a68aSJeff Mahoney static int check_feature_bits(struct btrfs_root *root,
45623b02a68aSJeff Mahoney 			      enum btrfs_feature_set set,
45632eaa055fSJeff Mahoney 			      u64 change_mask, u64 flags, u64 supported_flags,
45642eaa055fSJeff Mahoney 			      u64 safe_set, u64 safe_clear)
45652eaa055fSJeff Mahoney {
45663b02a68aSJeff Mahoney 	const char *type = btrfs_feature_set_names[set];
45673b02a68aSJeff Mahoney 	char *names;
45682eaa055fSJeff Mahoney 	u64 disallowed, unsupported;
45692eaa055fSJeff Mahoney 	u64 set_mask = flags & change_mask;
45702eaa055fSJeff Mahoney 	u64 clear_mask = ~flags & change_mask;
45712eaa055fSJeff Mahoney 
45722eaa055fSJeff Mahoney 	unsupported = set_mask & ~supported_flags;
45732eaa055fSJeff Mahoney 	if (unsupported) {
45743b02a68aSJeff Mahoney 		names = btrfs_printable_features(set, unsupported);
45753b02a68aSJeff Mahoney 		if (names) {
45763b02a68aSJeff Mahoney 			btrfs_warn(root->fs_info,
45773b02a68aSJeff Mahoney 			   "this kernel does not support the %s feature bit%s",
45783b02a68aSJeff Mahoney 			   names, strchr(names, ',') ? "s" : "");
45793b02a68aSJeff Mahoney 			kfree(names);
45803b02a68aSJeff Mahoney 		} else
45812eaa055fSJeff Mahoney 			btrfs_warn(root->fs_info,
45822eaa055fSJeff Mahoney 			   "this kernel does not support %s bits 0x%llx",
45832eaa055fSJeff Mahoney 			   type, unsupported);
45842eaa055fSJeff Mahoney 		return -EOPNOTSUPP;
45852eaa055fSJeff Mahoney 	}
45862eaa055fSJeff Mahoney 
45872eaa055fSJeff Mahoney 	disallowed = set_mask & ~safe_set;
45882eaa055fSJeff Mahoney 	if (disallowed) {
45893b02a68aSJeff Mahoney 		names = btrfs_printable_features(set, disallowed);
45903b02a68aSJeff Mahoney 		if (names) {
45913b02a68aSJeff Mahoney 			btrfs_warn(root->fs_info,
45923b02a68aSJeff Mahoney 			   "can't set the %s feature bit%s while mounted",
45933b02a68aSJeff Mahoney 			   names, strchr(names, ',') ? "s" : "");
45943b02a68aSJeff Mahoney 			kfree(names);
45953b02a68aSJeff Mahoney 		} else
45962eaa055fSJeff Mahoney 			btrfs_warn(root->fs_info,
45972eaa055fSJeff Mahoney 			   "can't set %s bits 0x%llx while mounted",
45982eaa055fSJeff Mahoney 			   type, disallowed);
45992eaa055fSJeff Mahoney 		return -EPERM;
46002eaa055fSJeff Mahoney 	}
46012eaa055fSJeff Mahoney 
46022eaa055fSJeff Mahoney 	disallowed = clear_mask & ~safe_clear;
46032eaa055fSJeff Mahoney 	if (disallowed) {
46043b02a68aSJeff Mahoney 		names = btrfs_printable_features(set, disallowed);
46053b02a68aSJeff Mahoney 		if (names) {
46063b02a68aSJeff Mahoney 			btrfs_warn(root->fs_info,
46073b02a68aSJeff Mahoney 			   "can't clear the %s feature bit%s while mounted",
46083b02a68aSJeff Mahoney 			   names, strchr(names, ',') ? "s" : "");
46093b02a68aSJeff Mahoney 			kfree(names);
46103b02a68aSJeff Mahoney 		} else
46112eaa055fSJeff Mahoney 			btrfs_warn(root->fs_info,
46122eaa055fSJeff Mahoney 			   "can't clear %s bits 0x%llx while mounted",
46132eaa055fSJeff Mahoney 			   type, disallowed);
46142eaa055fSJeff Mahoney 		return -EPERM;
46152eaa055fSJeff Mahoney 	}
46162eaa055fSJeff Mahoney 
46172eaa055fSJeff Mahoney 	return 0;
46182eaa055fSJeff Mahoney }
46192eaa055fSJeff Mahoney 
46202eaa055fSJeff Mahoney #define check_feature(root, change_mask, flags, mask_base)	\
46213b02a68aSJeff Mahoney check_feature_bits(root, FEAT_##mask_base, change_mask, flags,	\
46222eaa055fSJeff Mahoney 		   BTRFS_FEATURE_ ## mask_base ## _SUPP,	\
46232eaa055fSJeff Mahoney 		   BTRFS_FEATURE_ ## mask_base ## _SAFE_SET,	\
46242eaa055fSJeff Mahoney 		   BTRFS_FEATURE_ ## mask_base ## _SAFE_CLEAR)
46252eaa055fSJeff Mahoney 
46262eaa055fSJeff Mahoney static int btrfs_ioctl_set_features(struct file *file, void __user *arg)
46272eaa055fSJeff Mahoney {
46282eaa055fSJeff Mahoney 	struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
46292eaa055fSJeff Mahoney 	struct btrfs_super_block *super_block = root->fs_info->super_copy;
46302eaa055fSJeff Mahoney 	struct btrfs_ioctl_feature_flags flags[2];
46312eaa055fSJeff Mahoney 	struct btrfs_trans_handle *trans;
46322eaa055fSJeff Mahoney 	u64 newflags;
46332eaa055fSJeff Mahoney 	int ret;
46342eaa055fSJeff Mahoney 
46352eaa055fSJeff Mahoney 	if (!capable(CAP_SYS_ADMIN))
46362eaa055fSJeff Mahoney 		return -EPERM;
46372eaa055fSJeff Mahoney 
46382eaa055fSJeff Mahoney 	if (copy_from_user(flags, arg, sizeof(flags)))
46392eaa055fSJeff Mahoney 		return -EFAULT;
46402eaa055fSJeff Mahoney 
46412eaa055fSJeff Mahoney 	/* Nothing to do */
46422eaa055fSJeff Mahoney 	if (!flags[0].compat_flags && !flags[0].compat_ro_flags &&
46432eaa055fSJeff Mahoney 	    !flags[0].incompat_flags)
46442eaa055fSJeff Mahoney 		return 0;
46452eaa055fSJeff Mahoney 
46462eaa055fSJeff Mahoney 	ret = check_feature(root, flags[0].compat_flags,
46472eaa055fSJeff Mahoney 			    flags[1].compat_flags, COMPAT);
46482eaa055fSJeff Mahoney 	if (ret)
46492eaa055fSJeff Mahoney 		return ret;
46502eaa055fSJeff Mahoney 
46512eaa055fSJeff Mahoney 	ret = check_feature(root, flags[0].compat_ro_flags,
46522eaa055fSJeff Mahoney 			    flags[1].compat_ro_flags, COMPAT_RO);
46532eaa055fSJeff Mahoney 	if (ret)
46542eaa055fSJeff Mahoney 		return ret;
46552eaa055fSJeff Mahoney 
46562eaa055fSJeff Mahoney 	ret = check_feature(root, flags[0].incompat_flags,
46572eaa055fSJeff Mahoney 			    flags[1].incompat_flags, INCOMPAT);
46582eaa055fSJeff Mahoney 	if (ret)
46592eaa055fSJeff Mahoney 		return ret;
46602eaa055fSJeff Mahoney 
46612eaa055fSJeff Mahoney 	trans = btrfs_start_transaction(root, 1);
46622eaa055fSJeff Mahoney 	if (IS_ERR(trans))
46632eaa055fSJeff Mahoney 		return PTR_ERR(trans);
46642eaa055fSJeff Mahoney 
46652eaa055fSJeff Mahoney 	spin_lock(&root->fs_info->super_lock);
46662eaa055fSJeff Mahoney 	newflags = btrfs_super_compat_flags(super_block);
46672eaa055fSJeff Mahoney 	newflags |= flags[0].compat_flags & flags[1].compat_flags;
46682eaa055fSJeff Mahoney 	newflags &= ~(flags[0].compat_flags & ~flags[1].compat_flags);
46692eaa055fSJeff Mahoney 	btrfs_set_super_compat_flags(super_block, newflags);
46702eaa055fSJeff Mahoney 
46712eaa055fSJeff Mahoney 	newflags = btrfs_super_compat_ro_flags(super_block);
46722eaa055fSJeff Mahoney 	newflags |= flags[0].compat_ro_flags & flags[1].compat_ro_flags;
46732eaa055fSJeff Mahoney 	newflags &= ~(flags[0].compat_ro_flags & ~flags[1].compat_ro_flags);
46742eaa055fSJeff Mahoney 	btrfs_set_super_compat_ro_flags(super_block, newflags);
46752eaa055fSJeff Mahoney 
46762eaa055fSJeff Mahoney 	newflags = btrfs_super_incompat_flags(super_block);
46772eaa055fSJeff Mahoney 	newflags |= flags[0].incompat_flags & flags[1].incompat_flags;
46782eaa055fSJeff Mahoney 	newflags &= ~(flags[0].incompat_flags & ~flags[1].incompat_flags);
46792eaa055fSJeff Mahoney 	btrfs_set_super_incompat_flags(super_block, newflags);
46802eaa055fSJeff Mahoney 	spin_unlock(&root->fs_info->super_lock);
46812eaa055fSJeff Mahoney 
46822eaa055fSJeff Mahoney 	return btrfs_end_transaction(trans, root);
46832eaa055fSJeff Mahoney }
46842eaa055fSJeff Mahoney 
4685f46b5a66SChristoph Hellwig long btrfs_ioctl(struct file *file, unsigned int
4686f46b5a66SChristoph Hellwig 		cmd, unsigned long arg)
4687f46b5a66SChristoph Hellwig {
4688496ad9aaSAl Viro 	struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
46894bcabaa3SChristoph Hellwig 	void __user *argp = (void __user *)arg;
4690f46b5a66SChristoph Hellwig 
4691f46b5a66SChristoph Hellwig 	switch (cmd) {
46926cbff00fSChristoph Hellwig 	case FS_IOC_GETFLAGS:
46936cbff00fSChristoph Hellwig 		return btrfs_ioctl_getflags(file, argp);
46946cbff00fSChristoph Hellwig 	case FS_IOC_SETFLAGS:
46956cbff00fSChristoph Hellwig 		return btrfs_ioctl_setflags(file, argp);
46966cbff00fSChristoph Hellwig 	case FS_IOC_GETVERSION:
46976cbff00fSChristoph Hellwig 		return btrfs_ioctl_getversion(file, argp);
4698f7039b1dSLi Dongyang 	case FITRIM:
4699f7039b1dSLi Dongyang 		return btrfs_ioctl_fitrim(file, argp);
4700f46b5a66SChristoph Hellwig 	case BTRFS_IOC_SNAP_CREATE:
4701fa0d2b9bSLi Zefan 		return btrfs_ioctl_snap_create(file, argp, 0);
4702fdfb1e4fSLi Zefan 	case BTRFS_IOC_SNAP_CREATE_V2:
4703fa0d2b9bSLi Zefan 		return btrfs_ioctl_snap_create_v2(file, argp, 0);
47043de4586cSChris Mason 	case BTRFS_IOC_SUBVOL_CREATE:
4705fa0d2b9bSLi Zefan 		return btrfs_ioctl_snap_create(file, argp, 1);
47066f72c7e2SArne Jansen 	case BTRFS_IOC_SUBVOL_CREATE_V2:
47076f72c7e2SArne Jansen 		return btrfs_ioctl_snap_create_v2(file, argp, 1);
470876dda93cSYan, Zheng 	case BTRFS_IOC_SNAP_DESTROY:
470976dda93cSYan, Zheng 		return btrfs_ioctl_snap_destroy(file, argp);
47100caa102dSLi Zefan 	case BTRFS_IOC_SUBVOL_GETFLAGS:
47110caa102dSLi Zefan 		return btrfs_ioctl_subvol_getflags(file, argp);
47120caa102dSLi Zefan 	case BTRFS_IOC_SUBVOL_SETFLAGS:
47130caa102dSLi Zefan 		return btrfs_ioctl_subvol_setflags(file, argp);
47146ef5ed0dSJosef Bacik 	case BTRFS_IOC_DEFAULT_SUBVOL:
47156ef5ed0dSJosef Bacik 		return btrfs_ioctl_default_subvol(file, argp);
4716f46b5a66SChristoph Hellwig 	case BTRFS_IOC_DEFRAG:
47171e701a32SChris Mason 		return btrfs_ioctl_defrag(file, NULL);
47181e701a32SChris Mason 	case BTRFS_IOC_DEFRAG_RANGE:
47191e701a32SChris Mason 		return btrfs_ioctl_defrag(file, argp);
4720f46b5a66SChristoph Hellwig 	case BTRFS_IOC_RESIZE:
4721198605a8SMiao Xie 		return btrfs_ioctl_resize(file, argp);
4722f46b5a66SChristoph Hellwig 	case BTRFS_IOC_ADD_DEV:
47234bcabaa3SChristoph Hellwig 		return btrfs_ioctl_add_dev(root, argp);
4724f46b5a66SChristoph Hellwig 	case BTRFS_IOC_RM_DEV:
4725da24927bSMiao Xie 		return btrfs_ioctl_rm_dev(file, argp);
4726475f6387SJan Schmidt 	case BTRFS_IOC_FS_INFO:
4727475f6387SJan Schmidt 		return btrfs_ioctl_fs_info(root, argp);
4728475f6387SJan Schmidt 	case BTRFS_IOC_DEV_INFO:
4729475f6387SJan Schmidt 		return btrfs_ioctl_dev_info(root, argp);
4730f46b5a66SChristoph Hellwig 	case BTRFS_IOC_BALANCE:
47319ba1f6e4SLiu Bo 		return btrfs_ioctl_balance(file, NULL);
4732f46b5a66SChristoph Hellwig 	case BTRFS_IOC_CLONE:
4733c5c9cd4dSSage Weil 		return btrfs_ioctl_clone(file, arg, 0, 0, 0);
4734c5c9cd4dSSage Weil 	case BTRFS_IOC_CLONE_RANGE:
47357a865e8aSChristoph Hellwig 		return btrfs_ioctl_clone_range(file, argp);
4736f46b5a66SChristoph Hellwig 	case BTRFS_IOC_TRANS_START:
4737f46b5a66SChristoph Hellwig 		return btrfs_ioctl_trans_start(file);
4738f46b5a66SChristoph Hellwig 	case BTRFS_IOC_TRANS_END:
4739f46b5a66SChristoph Hellwig 		return btrfs_ioctl_trans_end(file);
4740ac8e9819SChris Mason 	case BTRFS_IOC_TREE_SEARCH:
4741ac8e9819SChris Mason 		return btrfs_ioctl_tree_search(file, argp);
4742ac8e9819SChris Mason 	case BTRFS_IOC_INO_LOOKUP:
4743ac8e9819SChris Mason 		return btrfs_ioctl_ino_lookup(file, argp);
4744d7728c96SJan Schmidt 	case BTRFS_IOC_INO_PATHS:
4745d7728c96SJan Schmidt 		return btrfs_ioctl_ino_to_path(root, argp);
4746d7728c96SJan Schmidt 	case BTRFS_IOC_LOGICAL_INO:
4747d7728c96SJan Schmidt 		return btrfs_ioctl_logical_to_ino(root, argp);
47481406e432SJosef Bacik 	case BTRFS_IOC_SPACE_INFO:
47491406e432SJosef Bacik 		return btrfs_ioctl_space_info(root, argp);
475001e219e8SJeff Mahoney 	case BTRFS_IOC_GLOBAL_RSV:
475101e219e8SJeff Mahoney 		return btrfs_ioctl_global_rsv(root, argp);
47529b199859SFilipe David Borba Manana 	case BTRFS_IOC_SYNC: {
47539b199859SFilipe David Borba Manana 		int ret;
47549b199859SFilipe David Borba Manana 
475591aef86fSMiao Xie 		ret = btrfs_start_delalloc_roots(root->fs_info, 0);
47569b199859SFilipe David Borba Manana 		if (ret)
47579b199859SFilipe David Borba Manana 			return ret;
47589b199859SFilipe David Borba Manana 		ret = btrfs_sync_fs(file->f_dentry->d_sb, 1);
47599b199859SFilipe David Borba Manana 		return ret;
47609b199859SFilipe David Borba Manana 	}
476146204592SSage Weil 	case BTRFS_IOC_START_SYNC:
47629a8c28beSMiao Xie 		return btrfs_ioctl_start_sync(root, argp);
476346204592SSage Weil 	case BTRFS_IOC_WAIT_SYNC:
47649a8c28beSMiao Xie 		return btrfs_ioctl_wait_sync(root, argp);
4765475f6387SJan Schmidt 	case BTRFS_IOC_SCRUB:
4766b8e95489SMiao Xie 		return btrfs_ioctl_scrub(file, argp);
4767475f6387SJan Schmidt 	case BTRFS_IOC_SCRUB_CANCEL:
4768475f6387SJan Schmidt 		return btrfs_ioctl_scrub_cancel(root, argp);
4769475f6387SJan Schmidt 	case BTRFS_IOC_SCRUB_PROGRESS:
4770475f6387SJan Schmidt 		return btrfs_ioctl_scrub_progress(root, argp);
4771c9e9f97bSIlya Dryomov 	case BTRFS_IOC_BALANCE_V2:
47729ba1f6e4SLiu Bo 		return btrfs_ioctl_balance(file, argp);
4773837d5b6eSIlya Dryomov 	case BTRFS_IOC_BALANCE_CTL:
4774837d5b6eSIlya Dryomov 		return btrfs_ioctl_balance_ctl(root, arg);
477519a39dceSIlya Dryomov 	case BTRFS_IOC_BALANCE_PROGRESS:
477619a39dceSIlya Dryomov 		return btrfs_ioctl_balance_progress(root, argp);
47778ea05e3aSAlexander Block 	case BTRFS_IOC_SET_RECEIVED_SUBVOL:
47788ea05e3aSAlexander Block 		return btrfs_ioctl_set_received_subvol(file, argp);
477931db9f7cSAlexander Block 	case BTRFS_IOC_SEND:
478031db9f7cSAlexander Block 		return btrfs_ioctl_send(file, argp);
4781c11d2c23SStefan Behrens 	case BTRFS_IOC_GET_DEV_STATS:
4782b27f7c0cSDavid Sterba 		return btrfs_ioctl_get_dev_stats(root, argp);
47835d13a37bSArne Jansen 	case BTRFS_IOC_QUOTA_CTL:
4784905b0ddaSMiao Xie 		return btrfs_ioctl_quota_ctl(file, argp);
47855d13a37bSArne Jansen 	case BTRFS_IOC_QGROUP_ASSIGN:
4786905b0ddaSMiao Xie 		return btrfs_ioctl_qgroup_assign(file, argp);
47875d13a37bSArne Jansen 	case BTRFS_IOC_QGROUP_CREATE:
4788905b0ddaSMiao Xie 		return btrfs_ioctl_qgroup_create(file, argp);
47895d13a37bSArne Jansen 	case BTRFS_IOC_QGROUP_LIMIT:
4790905b0ddaSMiao Xie 		return btrfs_ioctl_qgroup_limit(file, argp);
47912f232036SJan Schmidt 	case BTRFS_IOC_QUOTA_RESCAN:
47922f232036SJan Schmidt 		return btrfs_ioctl_quota_rescan(file, argp);
47932f232036SJan Schmidt 	case BTRFS_IOC_QUOTA_RESCAN_STATUS:
47942f232036SJan Schmidt 		return btrfs_ioctl_quota_rescan_status(file, argp);
479557254b6eSJan Schmidt 	case BTRFS_IOC_QUOTA_RESCAN_WAIT:
479657254b6eSJan Schmidt 		return btrfs_ioctl_quota_rescan_wait(file, argp);
47973f6bcfbdSStefan Behrens 	case BTRFS_IOC_DEV_REPLACE:
47983f6bcfbdSStefan Behrens 		return btrfs_ioctl_dev_replace(root, argp);
4799867ab667Sjeff.liu 	case BTRFS_IOC_GET_FSLABEL:
4800867ab667Sjeff.liu 		return btrfs_ioctl_get_fslabel(file, argp);
4801a8bfd4abSjeff.liu 	case BTRFS_IOC_SET_FSLABEL:
4802a8bfd4abSjeff.liu 		return btrfs_ioctl_set_fslabel(file, argp);
4803416161dbSMark Fasheh 	case BTRFS_IOC_FILE_EXTENT_SAME:
4804416161dbSMark Fasheh 		return btrfs_ioctl_file_extent_same(file, argp);
48052eaa055fSJeff Mahoney 	case BTRFS_IOC_GET_SUPPORTED_FEATURES:
48062eaa055fSJeff Mahoney 		return btrfs_ioctl_get_supported_features(file, argp);
48072eaa055fSJeff Mahoney 	case BTRFS_IOC_GET_FEATURES:
48082eaa055fSJeff Mahoney 		return btrfs_ioctl_get_features(file, argp);
48092eaa055fSJeff Mahoney 	case BTRFS_IOC_SET_FEATURES:
48102eaa055fSJeff Mahoney 		return btrfs_ioctl_set_features(file, argp);
4811f46b5a66SChristoph Hellwig 	}
4812f46b5a66SChristoph Hellwig 
4813f46b5a66SChristoph Hellwig 	return -ENOTTY;
4814f46b5a66SChristoph Hellwig }
4815