xref: /openbmc/linux/fs/btrfs/ioctl.c (revision 3b02a68a)
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;
396f46b5a66SChristoph Hellwig 	int ret;
397f46b5a66SChristoph Hellwig 	int err;
398f46b5a66SChristoph Hellwig 	u64 objectid;
399f46b5a66SChristoph Hellwig 	u64 new_dirid = BTRFS_FIRST_FREE_OBJECTID;
4003de4586cSChris Mason 	u64 index = 0;
401d5c12070SMiao Xie 	u64 qgroup_reserved;
4028ea05e3aSAlexander Block 	uuid_le new_uuid;
403f46b5a66SChristoph Hellwig 
404581bb050SLi Zefan 	ret = btrfs_find_free_objectid(root->fs_info->tree_root, &objectid);
4052fbe8c8aSAl Viro 	if (ret)
406a22285a6SYan, Zheng 		return ret;
4076a912213SJosef Bacik 
408d5c12070SMiao Xie 	btrfs_init_block_rsv(&block_rsv, BTRFS_BLOCK_RSV_TEMP);
4099ed74f2dSJosef Bacik 	/*
410d5c12070SMiao Xie 	 * The same as the snapshot creation, please see the comment
411d5c12070SMiao Xie 	 * of create_snapshot().
4129ed74f2dSJosef Bacik 	 */
413d5c12070SMiao Xie 	ret = btrfs_subvolume_reserve_metadata(root, &block_rsv,
414dd5f9615SStefan Behrens 					       8, &qgroup_reserved, false);
415d5c12070SMiao Xie 	if (ret)
416d5c12070SMiao Xie 		return ret;
417f46b5a66SChristoph Hellwig 
418d5c12070SMiao Xie 	trans = btrfs_start_transaction(root, 0);
419d5c12070SMiao Xie 	if (IS_ERR(trans)) {
420d5c12070SMiao Xie 		ret = PTR_ERR(trans);
421d5c12070SMiao Xie 		goto out;
422d5c12070SMiao Xie 	}
423d5c12070SMiao Xie 	trans->block_rsv = &block_rsv;
424d5c12070SMiao Xie 	trans->bytes_reserved = block_rsv.size;
425f46b5a66SChristoph Hellwig 
4268696c533SMiao Xie 	ret = btrfs_qgroup_inherit(trans, root->fs_info, 0, objectid, inherit);
4276f72c7e2SArne Jansen 	if (ret)
4286f72c7e2SArne Jansen 		goto fail;
4296f72c7e2SArne Jansen 
4305d4f98a2SYan Zheng 	leaf = btrfs_alloc_free_block(trans, root, root->leafsize,
4315581a51aSJan Schmidt 				      0, objectid, NULL, 0, 0, 0);
4328e8a1e31SJosef Bacik 	if (IS_ERR(leaf)) {
4338e8a1e31SJosef Bacik 		ret = PTR_ERR(leaf);
4348e8a1e31SJosef Bacik 		goto fail;
4358e8a1e31SJosef Bacik 	}
436f46b5a66SChristoph Hellwig 
4375d4f98a2SYan Zheng 	memset_extent_buffer(leaf, 0, 0, sizeof(struct btrfs_header));
438f46b5a66SChristoph Hellwig 	btrfs_set_header_bytenr(leaf, leaf->start);
439f46b5a66SChristoph Hellwig 	btrfs_set_header_generation(leaf, trans->transid);
4405d4f98a2SYan Zheng 	btrfs_set_header_backref_rev(leaf, BTRFS_MIXED_BACKREF_REV);
441f46b5a66SChristoph Hellwig 	btrfs_set_header_owner(leaf, objectid);
442f46b5a66SChristoph Hellwig 
4430a4e5586SRoss Kirk 	write_extent_buffer(leaf, root->fs_info->fsid, btrfs_header_fsid(),
444f46b5a66SChristoph Hellwig 			    BTRFS_FSID_SIZE);
4455d4f98a2SYan Zheng 	write_extent_buffer(leaf, root->fs_info->chunk_tree_uuid,
446b308bc2fSGeert Uytterhoeven 			    btrfs_header_chunk_tree_uuid(leaf),
4475d4f98a2SYan Zheng 			    BTRFS_UUID_SIZE);
448f46b5a66SChristoph Hellwig 	btrfs_mark_buffer_dirty(leaf);
449f46b5a66SChristoph Hellwig 
4508ea05e3aSAlexander Block 	memset(&root_item, 0, sizeof(root_item));
4518ea05e3aSAlexander Block 
452f46b5a66SChristoph Hellwig 	inode_item = &root_item.inode;
4533cae210fSQu Wenruo 	btrfs_set_stack_inode_generation(inode_item, 1);
4543cae210fSQu Wenruo 	btrfs_set_stack_inode_size(inode_item, 3);
4553cae210fSQu Wenruo 	btrfs_set_stack_inode_nlink(inode_item, 1);
4563cae210fSQu Wenruo 	btrfs_set_stack_inode_nbytes(inode_item, root->leafsize);
4573cae210fSQu Wenruo 	btrfs_set_stack_inode_mode(inode_item, S_IFDIR | 0755);
458f46b5a66SChristoph Hellwig 
4593cae210fSQu Wenruo 	btrfs_set_root_flags(&root_item, 0);
4603cae210fSQu Wenruo 	btrfs_set_root_limit(&root_item, 0);
4613cae210fSQu Wenruo 	btrfs_set_stack_inode_flags(inode_item, BTRFS_INODE_ROOT_ITEM_INIT);
46208fe4db1SLi Zefan 
463f46b5a66SChristoph Hellwig 	btrfs_set_root_bytenr(&root_item, leaf->start);
46484234f3aSYan Zheng 	btrfs_set_root_generation(&root_item, trans->transid);
465f46b5a66SChristoph Hellwig 	btrfs_set_root_level(&root_item, 0);
466f46b5a66SChristoph Hellwig 	btrfs_set_root_refs(&root_item, 1);
46786b9f2ecSYan, Zheng 	btrfs_set_root_used(&root_item, leaf->len);
46880ff3856SYan Zheng 	btrfs_set_root_last_snapshot(&root_item, 0);
469f46b5a66SChristoph Hellwig 
4708ea05e3aSAlexander Block 	btrfs_set_root_generation_v2(&root_item,
4718ea05e3aSAlexander Block 			btrfs_root_generation(&root_item));
4728ea05e3aSAlexander Block 	uuid_le_gen(&new_uuid);
4738ea05e3aSAlexander Block 	memcpy(root_item.uuid, new_uuid.b, BTRFS_UUID_SIZE);
4743cae210fSQu Wenruo 	btrfs_set_stack_timespec_sec(&root_item.otime, cur_time.tv_sec);
4753cae210fSQu Wenruo 	btrfs_set_stack_timespec_nsec(&root_item.otime, cur_time.tv_nsec);
4768ea05e3aSAlexander Block 	root_item.ctime = root_item.otime;
4778ea05e3aSAlexander Block 	btrfs_set_root_ctransid(&root_item, trans->transid);
4788ea05e3aSAlexander Block 	btrfs_set_root_otransid(&root_item, trans->transid);
479f46b5a66SChristoph Hellwig 
480925baeddSChris Mason 	btrfs_tree_unlock(leaf);
481f46b5a66SChristoph Hellwig 	free_extent_buffer(leaf);
482f46b5a66SChristoph Hellwig 	leaf = NULL;
483f46b5a66SChristoph Hellwig 
484f46b5a66SChristoph Hellwig 	btrfs_set_root_dirid(&root_item, new_dirid);
485f46b5a66SChristoph Hellwig 
486f46b5a66SChristoph Hellwig 	key.objectid = objectid;
4875d4f98a2SYan Zheng 	key.offset = 0;
488f46b5a66SChristoph Hellwig 	btrfs_set_key_type(&key, BTRFS_ROOT_ITEM_KEY);
489f46b5a66SChristoph Hellwig 	ret = btrfs_insert_root(trans, root->fs_info->tree_root, &key,
490f46b5a66SChristoph Hellwig 				&root_item);
491f46b5a66SChristoph Hellwig 	if (ret)
492f46b5a66SChristoph Hellwig 		goto fail;
493f46b5a66SChristoph Hellwig 
49476dda93cSYan, Zheng 	key.offset = (u64)-1;
49576dda93cSYan, Zheng 	new_root = btrfs_read_fs_root_no_name(root->fs_info, &key);
49679787eaaSJeff Mahoney 	if (IS_ERR(new_root)) {
49779787eaaSJeff Mahoney 		btrfs_abort_transaction(trans, root, PTR_ERR(new_root));
49879787eaaSJeff Mahoney 		ret = PTR_ERR(new_root);
49979787eaaSJeff Mahoney 		goto fail;
50079787eaaSJeff Mahoney 	}
50176dda93cSYan, Zheng 
50276dda93cSYan, Zheng 	btrfs_record_root_in_trans(trans, new_root);
50376dda93cSYan, Zheng 
504d82a6f1dSJosef Bacik 	ret = btrfs_create_subvol_root(trans, new_root, new_dirid);
505ce598979SMark Fasheh 	if (ret) {
506ce598979SMark Fasheh 		/* We potentially lose an unused inode item here */
50779787eaaSJeff Mahoney 		btrfs_abort_transaction(trans, root, ret);
508ce598979SMark Fasheh 		goto fail;
509ce598979SMark Fasheh 	}
510ce598979SMark Fasheh 
511f46b5a66SChristoph Hellwig 	/*
512f46b5a66SChristoph Hellwig 	 * insert the directory item
513f46b5a66SChristoph Hellwig 	 */
5143de4586cSChris Mason 	ret = btrfs_set_inode_index(dir, &index);
51579787eaaSJeff Mahoney 	if (ret) {
51679787eaaSJeff Mahoney 		btrfs_abort_transaction(trans, root, ret);
51779787eaaSJeff Mahoney 		goto fail;
51879787eaaSJeff Mahoney 	}
5193de4586cSChris Mason 
5203de4586cSChris Mason 	ret = btrfs_insert_dir_item(trans, root,
52116cdcec7SMiao Xie 				    name, namelen, dir, &key,
5223de4586cSChris Mason 				    BTRFS_FT_DIR, index);
52379787eaaSJeff Mahoney 	if (ret) {
52479787eaaSJeff Mahoney 		btrfs_abort_transaction(trans, root, ret);
525f46b5a66SChristoph Hellwig 		goto fail;
52679787eaaSJeff Mahoney 	}
5270660b5afSChris Mason 
52852c26179SYan Zheng 	btrfs_i_size_write(dir, dir->i_size + namelen * 2);
52952c26179SYan Zheng 	ret = btrfs_update_inode(trans, root, dir);
53052c26179SYan Zheng 	BUG_ON(ret);
53152c26179SYan Zheng 
5320660b5afSChris Mason 	ret = btrfs_add_root_ref(trans, root->fs_info->tree_root,
5334df27c4dSYan, Zheng 				 objectid, root->root_key.objectid,
53433345d01SLi Zefan 				 btrfs_ino(dir), index, name, namelen);
5350660b5afSChris Mason 	BUG_ON(ret);
5360660b5afSChris Mason 
537dd5f9615SStefan Behrens 	ret = btrfs_uuid_tree_add(trans, root->fs_info->uuid_root,
538dd5f9615SStefan Behrens 				  root_item.uuid, BTRFS_UUID_KEY_SUBVOL,
539dd5f9615SStefan Behrens 				  objectid);
540dd5f9615SStefan Behrens 	if (ret)
541dd5f9615SStefan Behrens 		btrfs_abort_transaction(trans, root, ret);
542dd5f9615SStefan Behrens 
543f46b5a66SChristoph Hellwig fail:
544d5c12070SMiao Xie 	trans->block_rsv = NULL;
545d5c12070SMiao Xie 	trans->bytes_reserved = 0;
54672fd032eSSage Weil 	if (async_transid) {
54772fd032eSSage Weil 		*async_transid = trans->transid;
54872fd032eSSage Weil 		err = btrfs_commit_transaction_async(trans, root, 1);
54900d71c9cSMiao Xie 		if (err)
55000d71c9cSMiao Xie 			err = btrfs_commit_transaction(trans, root);
55172fd032eSSage Weil 	} else {
55276dda93cSYan, Zheng 		err = btrfs_commit_transaction(trans, root);
55372fd032eSSage Weil 	}
554f46b5a66SChristoph Hellwig 	if (err && !ret)
555f46b5a66SChristoph Hellwig 		ret = err;
5561a65e24bSChris Mason 
5571a65e24bSChris Mason 	if (!ret)
5581a65e24bSChris Mason 		d_instantiate(dentry, btrfs_lookup_dentry(dir, dentry));
559d5c12070SMiao Xie out:
560d5c12070SMiao Xie 	btrfs_subvolume_release_metadata(root, &block_rsv, qgroup_reserved);
561f46b5a66SChristoph Hellwig 	return ret;
562f46b5a66SChristoph Hellwig }
563f46b5a66SChristoph Hellwig 
564e9662f70SMiao Xie static int create_snapshot(struct btrfs_root *root, struct inode *dir,
565e9662f70SMiao Xie 			   struct dentry *dentry, char *name, int namelen,
566e9662f70SMiao Xie 			   u64 *async_transid, bool readonly,
567e9662f70SMiao Xie 			   struct btrfs_qgroup_inherit *inherit)
568f46b5a66SChristoph Hellwig {
5692e4bfab9SYan, Zheng 	struct inode *inode;
570f46b5a66SChristoph Hellwig 	struct btrfs_pending_snapshot *pending_snapshot;
571f46b5a66SChristoph Hellwig 	struct btrfs_trans_handle *trans;
5722e4bfab9SYan, Zheng 	int ret;
573f46b5a66SChristoph Hellwig 
574f46b5a66SChristoph Hellwig 	if (!root->ref_cows)
575f46b5a66SChristoph Hellwig 		return -EINVAL;
576f46b5a66SChristoph Hellwig 
5776a03843dSMiao Xie 	ret = btrfs_start_delalloc_inodes(root, 0);
5786a03843dSMiao Xie 	if (ret)
5796a03843dSMiao Xie 		return ret;
5806a03843dSMiao Xie 
581b0244199SMiao Xie 	btrfs_wait_ordered_extents(root, -1);
5826a03843dSMiao Xie 
583a22285a6SYan, Zheng 	pending_snapshot = kzalloc(sizeof(*pending_snapshot), GFP_NOFS);
584a22285a6SYan, Zheng 	if (!pending_snapshot)
585a22285a6SYan, Zheng 		return -ENOMEM;
586a22285a6SYan, Zheng 
58766d8f3ddSMiao Xie 	btrfs_init_block_rsv(&pending_snapshot->block_rsv,
58866d8f3ddSMiao Xie 			     BTRFS_BLOCK_RSV_TEMP);
589d5c12070SMiao Xie 	/*
590d5c12070SMiao Xie 	 * 1 - parent dir inode
591d5c12070SMiao Xie 	 * 2 - dir entries
592d5c12070SMiao Xie 	 * 1 - root item
593d5c12070SMiao Xie 	 * 2 - root ref/backref
594d5c12070SMiao Xie 	 * 1 - root of snapshot
595dd5f9615SStefan Behrens 	 * 1 - UUID item
596d5c12070SMiao Xie 	 */
597d5c12070SMiao Xie 	ret = btrfs_subvolume_reserve_metadata(BTRFS_I(dir)->root,
598dd5f9615SStefan Behrens 					&pending_snapshot->block_rsv, 8,
599ee3441b4SJeff Mahoney 					&pending_snapshot->qgroup_reserved,
600ee3441b4SJeff Mahoney 					false);
601d5c12070SMiao Xie 	if (ret)
602d5c12070SMiao Xie 		goto out;
603d5c12070SMiao Xie 
604a22285a6SYan, Zheng 	pending_snapshot->dentry = dentry;
605a22285a6SYan, Zheng 	pending_snapshot->root = root;
606b83cc969SLi Zefan 	pending_snapshot->readonly = readonly;
607e9662f70SMiao Xie 	pending_snapshot->dir = dir;
6088696c533SMiao Xie 	pending_snapshot->inherit = inherit;
609a22285a6SYan, Zheng 
610d5c12070SMiao Xie 	trans = btrfs_start_transaction(root, 0);
611a22285a6SYan, Zheng 	if (IS_ERR(trans)) {
612a22285a6SYan, Zheng 		ret = PTR_ERR(trans);
613a22285a6SYan, Zheng 		goto fail;
614a22285a6SYan, Zheng 	}
615a22285a6SYan, Zheng 
6168351583eSJosef Bacik 	spin_lock(&root->fs_info->trans_lock);
617a22285a6SYan, Zheng 	list_add(&pending_snapshot->list,
618a22285a6SYan, Zheng 		 &trans->transaction->pending_snapshots);
6198351583eSJosef Bacik 	spin_unlock(&root->fs_info->trans_lock);
62072fd032eSSage Weil 	if (async_transid) {
62172fd032eSSage Weil 		*async_transid = trans->transid;
62272fd032eSSage Weil 		ret = btrfs_commit_transaction_async(trans,
62372fd032eSSage Weil 				     root->fs_info->extent_root, 1);
62400d71c9cSMiao Xie 		if (ret)
62500d71c9cSMiao Xie 			ret = btrfs_commit_transaction(trans, root);
62672fd032eSSage Weil 	} else {
62772fd032eSSage Weil 		ret = btrfs_commit_transaction(trans,
62872fd032eSSage Weil 					       root->fs_info->extent_root);
62972fd032eSSage Weil 	}
630aec8030aSMiao Xie 	if (ret)
631c37b2b62SJosef Bacik 		goto fail;
632a22285a6SYan, Zheng 
633a22285a6SYan, Zheng 	ret = pending_snapshot->error;
634f46b5a66SChristoph Hellwig 	if (ret)
6352e4bfab9SYan, Zheng 		goto fail;
636f46b5a66SChristoph Hellwig 
63766b4ffd1SJosef Bacik 	ret = btrfs_orphan_cleanup(pending_snapshot->snap);
63866b4ffd1SJosef Bacik 	if (ret)
63966b4ffd1SJosef Bacik 		goto fail;
640f46b5a66SChristoph Hellwig 
6412fbe8c8aSAl Viro 	inode = btrfs_lookup_dentry(dentry->d_parent->d_inode, dentry);
6422e4bfab9SYan, Zheng 	if (IS_ERR(inode)) {
6432e4bfab9SYan, Zheng 		ret = PTR_ERR(inode);
6442e4bfab9SYan, Zheng 		goto fail;
6452e4bfab9SYan, Zheng 	}
6462e4bfab9SYan, Zheng 	BUG_ON(!inode);
6472e4bfab9SYan, Zheng 	d_instantiate(dentry, inode);
6482e4bfab9SYan, Zheng 	ret = 0;
6492e4bfab9SYan, Zheng fail:
650d5c12070SMiao Xie 	btrfs_subvolume_release_metadata(BTRFS_I(dir)->root,
651d5c12070SMiao Xie 					 &pending_snapshot->block_rsv,
652d5c12070SMiao Xie 					 pending_snapshot->qgroup_reserved);
653d5c12070SMiao Xie out:
654a22285a6SYan, Zheng 	kfree(pending_snapshot);
655f46b5a66SChristoph Hellwig 	return ret;
656f46b5a66SChristoph Hellwig }
657f46b5a66SChristoph Hellwig 
6584260f7c7SSage Weil /*  copy of check_sticky in fs/namei.c()
6594260f7c7SSage Weil * It's inline, so penalty for filesystems that don't use sticky bit is
6604260f7c7SSage Weil * minimal.
6614260f7c7SSage Weil */
6624260f7c7SSage Weil static inline int btrfs_check_sticky(struct inode *dir, struct inode *inode)
6634260f7c7SSage Weil {
6642f2f43d3SEric W. Biederman 	kuid_t fsuid = current_fsuid();
6654260f7c7SSage Weil 
6664260f7c7SSage Weil 	if (!(dir->i_mode & S_ISVTX))
6674260f7c7SSage Weil 		return 0;
6682f2f43d3SEric W. Biederman 	if (uid_eq(inode->i_uid, fsuid))
6694260f7c7SSage Weil 		return 0;
6702f2f43d3SEric W. Biederman 	if (uid_eq(dir->i_uid, fsuid))
6714260f7c7SSage Weil 		return 0;
6724260f7c7SSage Weil 	return !capable(CAP_FOWNER);
6734260f7c7SSage Weil }
6744260f7c7SSage Weil 
6754260f7c7SSage Weil /*  copy of may_delete in fs/namei.c()
6764260f7c7SSage Weil  *	Check whether we can remove a link victim from directory dir, check
6774260f7c7SSage Weil  *  whether the type of victim is right.
6784260f7c7SSage Weil  *  1. We can't do it if dir is read-only (done in permission())
6794260f7c7SSage Weil  *  2. We should have write and exec permissions on dir
6804260f7c7SSage Weil  *  3. We can't remove anything from append-only dir
6814260f7c7SSage Weil  *  4. We can't do anything with immutable dir (done in permission())
6824260f7c7SSage Weil  *  5. If the sticky bit on dir is set we should either
6834260f7c7SSage Weil  *	a. be owner of dir, or
6844260f7c7SSage Weil  *	b. be owner of victim, or
6854260f7c7SSage Weil  *	c. have CAP_FOWNER capability
6864260f7c7SSage Weil  *  6. If the victim is append-only or immutable we can't do antyhing with
6874260f7c7SSage Weil  *     links pointing to it.
6884260f7c7SSage Weil  *  7. If we were asked to remove a directory and victim isn't one - ENOTDIR.
6894260f7c7SSage Weil  *  8. If we were asked to remove a non-directory and victim isn't one - EISDIR.
6904260f7c7SSage Weil  *  9. We can't remove a root or mountpoint.
6914260f7c7SSage Weil  * 10. We don't allow removal of NFS sillyrenamed files; it's handled by
6924260f7c7SSage Weil  *     nfs_async_unlink().
6934260f7c7SSage Weil  */
6944260f7c7SSage Weil 
6954260f7c7SSage Weil static int btrfs_may_delete(struct inode *dir, struct dentry *victim, int isdir)
6964260f7c7SSage Weil {
6974260f7c7SSage Weil 	int error;
6984260f7c7SSage Weil 
6994260f7c7SSage Weil 	if (!victim->d_inode)
7004260f7c7SSage Weil 		return -ENOENT;
7014260f7c7SSage Weil 
7024260f7c7SSage Weil 	BUG_ON(victim->d_parent->d_inode != dir);
7034fa6b5ecSJeff Layton 	audit_inode_child(dir, victim, AUDIT_TYPE_CHILD_DELETE);
7044260f7c7SSage Weil 
7054260f7c7SSage Weil 	error = inode_permission(dir, MAY_WRITE | MAY_EXEC);
7064260f7c7SSage Weil 	if (error)
7074260f7c7SSage Weil 		return error;
7084260f7c7SSage Weil 	if (IS_APPEND(dir))
7094260f7c7SSage Weil 		return -EPERM;
7104260f7c7SSage Weil 	if (btrfs_check_sticky(dir, victim->d_inode)||
7114260f7c7SSage Weil 		IS_APPEND(victim->d_inode)||
7124260f7c7SSage Weil 	    IS_IMMUTABLE(victim->d_inode) || IS_SWAPFILE(victim->d_inode))
7134260f7c7SSage Weil 		return -EPERM;
7144260f7c7SSage Weil 	if (isdir) {
7154260f7c7SSage Weil 		if (!S_ISDIR(victim->d_inode->i_mode))
7164260f7c7SSage Weil 			return -ENOTDIR;
7174260f7c7SSage Weil 		if (IS_ROOT(victim))
7184260f7c7SSage Weil 			return -EBUSY;
7194260f7c7SSage Weil 	} else if (S_ISDIR(victim->d_inode->i_mode))
7204260f7c7SSage Weil 		return -EISDIR;
7214260f7c7SSage Weil 	if (IS_DEADDIR(dir))
7224260f7c7SSage Weil 		return -ENOENT;
7234260f7c7SSage Weil 	if (victim->d_flags & DCACHE_NFSFS_RENAMED)
7244260f7c7SSage Weil 		return -EBUSY;
7254260f7c7SSage Weil 	return 0;
7264260f7c7SSage Weil }
7274260f7c7SSage Weil 
728cb8e7090SChristoph Hellwig /* copy of may_create in fs/namei.c() */
729cb8e7090SChristoph Hellwig static inline int btrfs_may_create(struct inode *dir, struct dentry *child)
730cb8e7090SChristoph Hellwig {
731cb8e7090SChristoph Hellwig 	if (child->d_inode)
732cb8e7090SChristoph Hellwig 		return -EEXIST;
733cb8e7090SChristoph Hellwig 	if (IS_DEADDIR(dir))
734cb8e7090SChristoph Hellwig 		return -ENOENT;
735cb8e7090SChristoph Hellwig 	return inode_permission(dir, MAY_WRITE | MAY_EXEC);
736cb8e7090SChristoph Hellwig }
737cb8e7090SChristoph Hellwig 
738cb8e7090SChristoph Hellwig /*
739cb8e7090SChristoph Hellwig  * Create a new subvolume below @parent.  This is largely modeled after
740cb8e7090SChristoph Hellwig  * sys_mkdirat and vfs_mkdir, but we only do a single component lookup
741cb8e7090SChristoph Hellwig  * inside this filesystem so it's quite a bit simpler.
742cb8e7090SChristoph Hellwig  */
74376dda93cSYan, Zheng static noinline int btrfs_mksubvol(struct path *parent,
74476dda93cSYan, Zheng 				   char *name, int namelen,
74572fd032eSSage Weil 				   struct btrfs_root *snap_src,
7466f72c7e2SArne Jansen 				   u64 *async_transid, bool readonly,
7478696c533SMiao Xie 				   struct btrfs_qgroup_inherit *inherit)
748cb8e7090SChristoph Hellwig {
74976dda93cSYan, Zheng 	struct inode *dir  = parent->dentry->d_inode;
750cb8e7090SChristoph Hellwig 	struct dentry *dentry;
751cb8e7090SChristoph Hellwig 	int error;
752cb8e7090SChristoph Hellwig 
7535c50c9b8SDavid Sterba 	error = mutex_lock_killable_nested(&dir->i_mutex, I_MUTEX_PARENT);
7545c50c9b8SDavid Sterba 	if (error == -EINTR)
7555c50c9b8SDavid Sterba 		return error;
756cb8e7090SChristoph Hellwig 
757cb8e7090SChristoph Hellwig 	dentry = lookup_one_len(name, parent->dentry, namelen);
758cb8e7090SChristoph Hellwig 	error = PTR_ERR(dentry);
759cb8e7090SChristoph Hellwig 	if (IS_ERR(dentry))
760cb8e7090SChristoph Hellwig 		goto out_unlock;
761cb8e7090SChristoph Hellwig 
762cb8e7090SChristoph Hellwig 	error = -EEXIST;
763cb8e7090SChristoph Hellwig 	if (dentry->d_inode)
764cb8e7090SChristoph Hellwig 		goto out_dput;
765cb8e7090SChristoph Hellwig 
76676dda93cSYan, Zheng 	error = btrfs_may_create(dir, dentry);
767cb8e7090SChristoph Hellwig 	if (error)
768a874a63eSLiu Bo 		goto out_dput;
769cb8e7090SChristoph Hellwig 
7709c52057cSChris Mason 	/*
7719c52057cSChris Mason 	 * even if this name doesn't exist, we may get hash collisions.
7729c52057cSChris Mason 	 * check for them now when we can safely fail
7739c52057cSChris Mason 	 */
7749c52057cSChris Mason 	error = btrfs_check_dir_item_collision(BTRFS_I(dir)->root,
7759c52057cSChris Mason 					       dir->i_ino, name,
7769c52057cSChris Mason 					       namelen);
7779c52057cSChris Mason 	if (error)
7789c52057cSChris Mason 		goto out_dput;
7799c52057cSChris Mason 
78076dda93cSYan, Zheng 	down_read(&BTRFS_I(dir)->root->fs_info->subvol_sem);
78176dda93cSYan, Zheng 
78276dda93cSYan, Zheng 	if (btrfs_root_refs(&BTRFS_I(dir)->root->root_item) == 0)
78376dda93cSYan, Zheng 		goto out_up_read;
78476dda93cSYan, Zheng 
7853de4586cSChris Mason 	if (snap_src) {
786e9662f70SMiao Xie 		error = create_snapshot(snap_src, dir, dentry, name, namelen,
7876f72c7e2SArne Jansen 					async_transid, readonly, inherit);
7883de4586cSChris Mason 	} else {
789d5c12070SMiao Xie 		error = create_subvol(dir, dentry, name, namelen,
790d5c12070SMiao Xie 				      async_transid, inherit);
7913de4586cSChris Mason 	}
79276dda93cSYan, Zheng 	if (!error)
79376dda93cSYan, Zheng 		fsnotify_mkdir(dir, dentry);
79476dda93cSYan, Zheng out_up_read:
79576dda93cSYan, Zheng 	up_read(&BTRFS_I(dir)->root->fs_info->subvol_sem);
796cb8e7090SChristoph Hellwig out_dput:
797cb8e7090SChristoph Hellwig 	dput(dentry);
798cb8e7090SChristoph Hellwig out_unlock:
79976dda93cSYan, Zheng 	mutex_unlock(&dir->i_mutex);
800cb8e7090SChristoph Hellwig 	return error;
801cb8e7090SChristoph Hellwig }
802cb8e7090SChristoph Hellwig 
8034cb5300bSChris Mason /*
8044cb5300bSChris Mason  * When we're defragging a range, we don't want to kick it off again
8054cb5300bSChris Mason  * if it is really just waiting for delalloc to send it down.
8064cb5300bSChris Mason  * If we find a nice big extent or delalloc range for the bytes in the
8074cb5300bSChris Mason  * file you want to defrag, we return 0 to let you know to skip this
8084cb5300bSChris Mason  * part of the file
8094cb5300bSChris Mason  */
8104cb5300bSChris Mason static int check_defrag_in_cache(struct inode *inode, u64 offset, int thresh)
8114cb5300bSChris Mason {
8124cb5300bSChris Mason 	struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
8134cb5300bSChris Mason 	struct extent_map *em = NULL;
8144cb5300bSChris Mason 	struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
8154cb5300bSChris Mason 	u64 end;
8164cb5300bSChris Mason 
8174cb5300bSChris Mason 	read_lock(&em_tree->lock);
8184cb5300bSChris Mason 	em = lookup_extent_mapping(em_tree, offset, PAGE_CACHE_SIZE);
8194cb5300bSChris Mason 	read_unlock(&em_tree->lock);
8204cb5300bSChris Mason 
8214cb5300bSChris Mason 	if (em) {
8224cb5300bSChris Mason 		end = extent_map_end(em);
8234cb5300bSChris Mason 		free_extent_map(em);
8244cb5300bSChris Mason 		if (end - offset > thresh)
8254cb5300bSChris Mason 			return 0;
8264cb5300bSChris Mason 	}
8274cb5300bSChris Mason 	/* if we already have a nice delalloc here, just stop */
8284cb5300bSChris Mason 	thresh /= 2;
8294cb5300bSChris Mason 	end = count_range_bits(io_tree, &offset, offset + thresh,
8304cb5300bSChris Mason 			       thresh, EXTENT_DELALLOC, 1);
8314cb5300bSChris Mason 	if (end >= thresh)
8324cb5300bSChris Mason 		return 0;
8334cb5300bSChris Mason 	return 1;
8344cb5300bSChris Mason }
8354cb5300bSChris Mason 
8364cb5300bSChris Mason /*
8374cb5300bSChris Mason  * helper function to walk through a file and find extents
8384cb5300bSChris Mason  * newer than a specific transid, and smaller than thresh.
8394cb5300bSChris Mason  *
8404cb5300bSChris Mason  * This is used by the defragging code to find new and small
8414cb5300bSChris Mason  * extents
8424cb5300bSChris Mason  */
8434cb5300bSChris Mason static int find_new_extents(struct btrfs_root *root,
8444cb5300bSChris Mason 			    struct inode *inode, u64 newer_than,
8454cb5300bSChris Mason 			    u64 *off, int thresh)
8464cb5300bSChris Mason {
8474cb5300bSChris Mason 	struct btrfs_path *path;
8484cb5300bSChris Mason 	struct btrfs_key min_key;
8494cb5300bSChris Mason 	struct extent_buffer *leaf;
8504cb5300bSChris Mason 	struct btrfs_file_extent_item *extent;
8514cb5300bSChris Mason 	int type;
8524cb5300bSChris Mason 	int ret;
853a4689d2bSDavid Sterba 	u64 ino = btrfs_ino(inode);
8544cb5300bSChris Mason 
8554cb5300bSChris Mason 	path = btrfs_alloc_path();
8564cb5300bSChris Mason 	if (!path)
8574cb5300bSChris Mason 		return -ENOMEM;
8584cb5300bSChris Mason 
859a4689d2bSDavid Sterba 	min_key.objectid = ino;
8604cb5300bSChris Mason 	min_key.type = BTRFS_EXTENT_DATA_KEY;
8614cb5300bSChris Mason 	min_key.offset = *off;
8624cb5300bSChris Mason 
8634cb5300bSChris Mason 	path->keep_locks = 1;
8644cb5300bSChris Mason 
8654cb5300bSChris Mason 	while (1) {
8666174d3cbSFilipe David Borba Manana 		ret = btrfs_search_forward(root, &min_key, path, newer_than);
8674cb5300bSChris Mason 		if (ret != 0)
8684cb5300bSChris Mason 			goto none;
869a4689d2bSDavid Sterba 		if (min_key.objectid != ino)
8704cb5300bSChris Mason 			goto none;
8714cb5300bSChris Mason 		if (min_key.type != BTRFS_EXTENT_DATA_KEY)
8724cb5300bSChris Mason 			goto none;
8734cb5300bSChris Mason 
8744cb5300bSChris Mason 		leaf = path->nodes[0];
8754cb5300bSChris Mason 		extent = btrfs_item_ptr(leaf, path->slots[0],
8764cb5300bSChris Mason 					struct btrfs_file_extent_item);
8774cb5300bSChris Mason 
8784cb5300bSChris Mason 		type = btrfs_file_extent_type(leaf, extent);
8794cb5300bSChris Mason 		if (type == BTRFS_FILE_EXTENT_REG &&
8804cb5300bSChris Mason 		    btrfs_file_extent_num_bytes(leaf, extent) < thresh &&
8814cb5300bSChris Mason 		    check_defrag_in_cache(inode, min_key.offset, thresh)) {
8824cb5300bSChris Mason 			*off = min_key.offset;
8834cb5300bSChris Mason 			btrfs_free_path(path);
8844cb5300bSChris Mason 			return 0;
8854cb5300bSChris Mason 		}
8864cb5300bSChris Mason 
8874cb5300bSChris Mason 		if (min_key.offset == (u64)-1)
8884cb5300bSChris Mason 			goto none;
8894cb5300bSChris Mason 
8904cb5300bSChris Mason 		min_key.offset++;
8914cb5300bSChris Mason 		btrfs_release_path(path);
8924cb5300bSChris Mason 	}
8934cb5300bSChris Mason none:
8944cb5300bSChris Mason 	btrfs_free_path(path);
8954cb5300bSChris Mason 	return -ENOENT;
8964cb5300bSChris Mason }
8974cb5300bSChris Mason 
8986c282eb4SLi Zefan static struct extent_map *defrag_lookup_extent(struct inode *inode, u64 start)
89917ce6ef8SLiu Bo {
90017ce6ef8SLiu Bo 	struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
901940100a4SChris Mason 	struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
9026c282eb4SLi Zefan 	struct extent_map *em;
9036c282eb4SLi Zefan 	u64 len = PAGE_CACHE_SIZE;
904940100a4SChris Mason 
905940100a4SChris Mason 	/*
906940100a4SChris Mason 	 * hopefully we have this extent in the tree already, try without
907940100a4SChris Mason 	 * the full extent lock
908940100a4SChris Mason 	 */
909940100a4SChris Mason 	read_lock(&em_tree->lock);
910940100a4SChris Mason 	em = lookup_extent_mapping(em_tree, start, len);
911940100a4SChris Mason 	read_unlock(&em_tree->lock);
912940100a4SChris Mason 
913940100a4SChris Mason 	if (!em) {
914940100a4SChris Mason 		/* get the big lock and read metadata off disk */
915d0082371SJeff Mahoney 		lock_extent(io_tree, start, start + len - 1);
916940100a4SChris Mason 		em = btrfs_get_extent(inode, NULL, 0, start, len, 0);
917d0082371SJeff Mahoney 		unlock_extent(io_tree, start, start + len - 1);
918940100a4SChris Mason 
9196cf8bfbfSDan Carpenter 		if (IS_ERR(em))
9206c282eb4SLi Zefan 			return NULL;
921940100a4SChris Mason 	}
922940100a4SChris Mason 
9236c282eb4SLi Zefan 	return em;
9246c282eb4SLi Zefan }
9256c282eb4SLi Zefan 
9266c282eb4SLi Zefan static bool defrag_check_next_extent(struct inode *inode, struct extent_map *em)
9276c282eb4SLi Zefan {
9286c282eb4SLi Zefan 	struct extent_map *next;
9296c282eb4SLi Zefan 	bool ret = true;
9306c282eb4SLi Zefan 
9316c282eb4SLi Zefan 	/* this is the last extent */
9326c282eb4SLi Zefan 	if (em->start + em->len >= i_size_read(inode))
9336c282eb4SLi Zefan 		return false;
9346c282eb4SLi Zefan 
9356c282eb4SLi Zefan 	next = defrag_lookup_extent(inode, em->start + em->len);
9366c282eb4SLi Zefan 	if (!next || next->block_start >= EXTENT_MAP_LAST_BYTE)
9376c282eb4SLi Zefan 		ret = false;
9386c282eb4SLi Zefan 
9396c282eb4SLi Zefan 	free_extent_map(next);
9406c282eb4SLi Zefan 	return ret;
9416c282eb4SLi Zefan }
9426c282eb4SLi Zefan 
9436c282eb4SLi Zefan static int should_defrag_range(struct inode *inode, u64 start, int thresh,
944a43a2111SAndrew Mahone 			       u64 *last_len, u64 *skip, u64 *defrag_end,
945a43a2111SAndrew Mahone 			       int compress)
9466c282eb4SLi Zefan {
9476c282eb4SLi Zefan 	struct extent_map *em;
9486c282eb4SLi Zefan 	int ret = 1;
9496c282eb4SLi Zefan 	bool next_mergeable = true;
9506c282eb4SLi Zefan 
9516c282eb4SLi Zefan 	/*
9526c282eb4SLi Zefan 	 * make sure that once we start defragging an extent, we keep on
9536c282eb4SLi Zefan 	 * defragging it
9546c282eb4SLi Zefan 	 */
9556c282eb4SLi Zefan 	if (start < *defrag_end)
9566c282eb4SLi Zefan 		return 1;
9576c282eb4SLi Zefan 
9586c282eb4SLi Zefan 	*skip = 0;
9596c282eb4SLi Zefan 
9606c282eb4SLi Zefan 	em = defrag_lookup_extent(inode, start);
9616c282eb4SLi Zefan 	if (!em)
9626c282eb4SLi Zefan 		return 0;
9636c282eb4SLi Zefan 
964940100a4SChris Mason 	/* this will cover holes, and inline extents */
96517ce6ef8SLiu Bo 	if (em->block_start >= EXTENT_MAP_LAST_BYTE) {
966940100a4SChris Mason 		ret = 0;
96717ce6ef8SLiu Bo 		goto out;
96817ce6ef8SLiu Bo 	}
96917ce6ef8SLiu Bo 
9706c282eb4SLi Zefan 	next_mergeable = defrag_check_next_extent(inode, em);
971940100a4SChris Mason 
972940100a4SChris Mason 	/*
9736c282eb4SLi Zefan 	 * we hit a real extent, if it is big or the next extent is not a
9746c282eb4SLi Zefan 	 * real extent, don't bother defragging it
975940100a4SChris Mason 	 */
976a43a2111SAndrew Mahone 	if (!compress && (*last_len == 0 || *last_len >= thresh) &&
9776c282eb4SLi Zefan 	    (em->len >= thresh || !next_mergeable))
978940100a4SChris Mason 		ret = 0;
97917ce6ef8SLiu Bo out:
980940100a4SChris Mason 	/*
981940100a4SChris Mason 	 * last_len ends up being a counter of how many bytes we've defragged.
982940100a4SChris Mason 	 * every time we choose not to defrag an extent, we reset *last_len
983940100a4SChris Mason 	 * so that the next tiny extent will force a defrag.
984940100a4SChris Mason 	 *
985940100a4SChris Mason 	 * The end result of this is that tiny extents before a single big
986940100a4SChris Mason 	 * extent will force at least part of that big extent to be defragged.
987940100a4SChris Mason 	 */
988940100a4SChris Mason 	if (ret) {
989940100a4SChris Mason 		*defrag_end = extent_map_end(em);
990940100a4SChris Mason 	} else {
991940100a4SChris Mason 		*last_len = 0;
992940100a4SChris Mason 		*skip = extent_map_end(em);
993940100a4SChris Mason 		*defrag_end = 0;
994940100a4SChris Mason 	}
995940100a4SChris Mason 
996940100a4SChris Mason 	free_extent_map(em);
997940100a4SChris Mason 	return ret;
998940100a4SChris Mason }
999940100a4SChris Mason 
10004cb5300bSChris Mason /*
10014cb5300bSChris Mason  * it doesn't do much good to defrag one or two pages
10024cb5300bSChris Mason  * at a time.  This pulls in a nice chunk of pages
10034cb5300bSChris Mason  * to COW and defrag.
10044cb5300bSChris Mason  *
10054cb5300bSChris Mason  * It also makes sure the delalloc code has enough
10064cb5300bSChris Mason  * dirty data to avoid making new small extents as part
10074cb5300bSChris Mason  * of the defrag
10084cb5300bSChris Mason  *
10094cb5300bSChris Mason  * It's a good idea to start RA on this range
10104cb5300bSChris Mason  * before calling this.
10114cb5300bSChris Mason  */
10124cb5300bSChris Mason static int cluster_pages_for_defrag(struct inode *inode,
10134cb5300bSChris Mason 				    struct page **pages,
10144cb5300bSChris Mason 				    unsigned long start_index,
10154cb5300bSChris Mason 				    int num_pages)
1016f46b5a66SChristoph Hellwig {
10174cb5300bSChris Mason 	unsigned long file_end;
10184cb5300bSChris Mason 	u64 isize = i_size_read(inode);
1019f46b5a66SChristoph Hellwig 	u64 page_start;
1020f46b5a66SChristoph Hellwig 	u64 page_end;
10211f12bd06SLiu Bo 	u64 page_cnt;
10224cb5300bSChris Mason 	int ret;
10234cb5300bSChris Mason 	int i;
10244cb5300bSChris Mason 	int i_done;
10254cb5300bSChris Mason 	struct btrfs_ordered_extent *ordered;
10264cb5300bSChris Mason 	struct extent_state *cached_state = NULL;
1027600a45e1SMiao Xie 	struct extent_io_tree *tree;
10283b16a4e3SJosef Bacik 	gfp_t mask = btrfs_alloc_write_mask(inode->i_mapping);
10294cb5300bSChris Mason 
10304cb5300bSChris Mason 	file_end = (isize - 1) >> PAGE_CACHE_SHIFT;
10311f12bd06SLiu Bo 	if (!isize || start_index > file_end)
10321f12bd06SLiu Bo 		return 0;
10331f12bd06SLiu Bo 
10341f12bd06SLiu Bo 	page_cnt = min_t(u64, (u64)num_pages, (u64)file_end - start_index + 1);
10354cb5300bSChris Mason 
10364cb5300bSChris Mason 	ret = btrfs_delalloc_reserve_space(inode,
10371f12bd06SLiu Bo 					   page_cnt << PAGE_CACHE_SHIFT);
10384cb5300bSChris Mason 	if (ret)
10394cb5300bSChris Mason 		return ret;
10404cb5300bSChris Mason 	i_done = 0;
1041600a45e1SMiao Xie 	tree = &BTRFS_I(inode)->io_tree;
10424cb5300bSChris Mason 
10434cb5300bSChris Mason 	/* step one, lock all the pages */
10441f12bd06SLiu Bo 	for (i = 0; i < page_cnt; i++) {
10454cb5300bSChris Mason 		struct page *page;
1046600a45e1SMiao Xie again:
1047a94733d0SJosef Bacik 		page = find_or_create_page(inode->i_mapping,
10483b16a4e3SJosef Bacik 					   start_index + i, mask);
10494cb5300bSChris Mason 		if (!page)
10504cb5300bSChris Mason 			break;
10514cb5300bSChris Mason 
1052600a45e1SMiao Xie 		page_start = page_offset(page);
1053600a45e1SMiao Xie 		page_end = page_start + PAGE_CACHE_SIZE - 1;
1054600a45e1SMiao Xie 		while (1) {
1055d0082371SJeff Mahoney 			lock_extent(tree, page_start, page_end);
1056600a45e1SMiao Xie 			ordered = btrfs_lookup_ordered_extent(inode,
1057600a45e1SMiao Xie 							      page_start);
1058d0082371SJeff Mahoney 			unlock_extent(tree, page_start, page_end);
1059600a45e1SMiao Xie 			if (!ordered)
1060600a45e1SMiao Xie 				break;
1061600a45e1SMiao Xie 
1062600a45e1SMiao Xie 			unlock_page(page);
1063600a45e1SMiao Xie 			btrfs_start_ordered_extent(inode, ordered, 1);
1064600a45e1SMiao Xie 			btrfs_put_ordered_extent(ordered);
1065600a45e1SMiao Xie 			lock_page(page);
10661f12bd06SLiu Bo 			/*
10671f12bd06SLiu Bo 			 * we unlocked the page above, so we need check if
10681f12bd06SLiu Bo 			 * it was released or not.
10691f12bd06SLiu Bo 			 */
10701f12bd06SLiu Bo 			if (page->mapping != inode->i_mapping) {
10711f12bd06SLiu Bo 				unlock_page(page);
10721f12bd06SLiu Bo 				page_cache_release(page);
10731f12bd06SLiu Bo 				goto again;
10741f12bd06SLiu Bo 			}
1075600a45e1SMiao Xie 		}
1076600a45e1SMiao Xie 
10774cb5300bSChris Mason 		if (!PageUptodate(page)) {
10784cb5300bSChris Mason 			btrfs_readpage(NULL, page);
10794cb5300bSChris Mason 			lock_page(page);
10804cb5300bSChris Mason 			if (!PageUptodate(page)) {
10814cb5300bSChris Mason 				unlock_page(page);
10824cb5300bSChris Mason 				page_cache_release(page);
10834cb5300bSChris Mason 				ret = -EIO;
10844cb5300bSChris Mason 				break;
10854cb5300bSChris Mason 			}
10864cb5300bSChris Mason 		}
1087600a45e1SMiao Xie 
1088600a45e1SMiao Xie 		if (page->mapping != inode->i_mapping) {
1089600a45e1SMiao Xie 			unlock_page(page);
1090600a45e1SMiao Xie 			page_cache_release(page);
1091600a45e1SMiao Xie 			goto again;
1092600a45e1SMiao Xie 		}
1093600a45e1SMiao Xie 
10944cb5300bSChris Mason 		pages[i] = page;
10954cb5300bSChris Mason 		i_done++;
10964cb5300bSChris Mason 	}
10974cb5300bSChris Mason 	if (!i_done || ret)
10984cb5300bSChris Mason 		goto out;
10994cb5300bSChris Mason 
11004cb5300bSChris Mason 	if (!(inode->i_sb->s_flags & MS_ACTIVE))
11014cb5300bSChris Mason 		goto out;
11024cb5300bSChris Mason 
11034cb5300bSChris Mason 	/*
11044cb5300bSChris Mason 	 * so now we have a nice long stream of locked
11054cb5300bSChris Mason 	 * and up to date pages, lets wait on them
11064cb5300bSChris Mason 	 */
11074cb5300bSChris Mason 	for (i = 0; i < i_done; i++)
11084cb5300bSChris Mason 		wait_on_page_writeback(pages[i]);
11094cb5300bSChris Mason 
11104cb5300bSChris Mason 	page_start = page_offset(pages[0]);
11114cb5300bSChris Mason 	page_end = page_offset(pages[i_done - 1]) + PAGE_CACHE_SIZE;
11124cb5300bSChris Mason 
11134cb5300bSChris Mason 	lock_extent_bits(&BTRFS_I(inode)->io_tree,
1114d0082371SJeff Mahoney 			 page_start, page_end - 1, 0, &cached_state);
11154cb5300bSChris Mason 	clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start,
11164cb5300bSChris Mason 			  page_end - 1, EXTENT_DIRTY | EXTENT_DELALLOC |
11179e8a4a8bSLiu Bo 			  EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG, 0, 0,
11189e8a4a8bSLiu Bo 			  &cached_state, GFP_NOFS);
11194cb5300bSChris Mason 
11201f12bd06SLiu Bo 	if (i_done != page_cnt) {
11219e0baf60SJosef Bacik 		spin_lock(&BTRFS_I(inode)->lock);
11229e0baf60SJosef Bacik 		BTRFS_I(inode)->outstanding_extents++;
11239e0baf60SJosef Bacik 		spin_unlock(&BTRFS_I(inode)->lock);
11244cb5300bSChris Mason 		btrfs_delalloc_release_space(inode,
11251f12bd06SLiu Bo 				     (page_cnt - i_done) << PAGE_CACHE_SHIFT);
11264cb5300bSChris Mason 	}
11274cb5300bSChris Mason 
11284cb5300bSChris Mason 
11299e8a4a8bSLiu Bo 	set_extent_defrag(&BTRFS_I(inode)->io_tree, page_start, page_end - 1,
11309e8a4a8bSLiu Bo 			  &cached_state, GFP_NOFS);
11314cb5300bSChris Mason 
11324cb5300bSChris Mason 	unlock_extent_cached(&BTRFS_I(inode)->io_tree,
11334cb5300bSChris Mason 			     page_start, page_end - 1, &cached_state,
11344cb5300bSChris Mason 			     GFP_NOFS);
11354cb5300bSChris Mason 
11364cb5300bSChris Mason 	for (i = 0; i < i_done; i++) {
11374cb5300bSChris Mason 		clear_page_dirty_for_io(pages[i]);
11384cb5300bSChris Mason 		ClearPageChecked(pages[i]);
11394cb5300bSChris Mason 		set_page_extent_mapped(pages[i]);
11404cb5300bSChris Mason 		set_page_dirty(pages[i]);
11414cb5300bSChris Mason 		unlock_page(pages[i]);
11424cb5300bSChris Mason 		page_cache_release(pages[i]);
11434cb5300bSChris Mason 	}
11444cb5300bSChris Mason 	return i_done;
11454cb5300bSChris Mason out:
11464cb5300bSChris Mason 	for (i = 0; i < i_done; i++) {
11474cb5300bSChris Mason 		unlock_page(pages[i]);
11484cb5300bSChris Mason 		page_cache_release(pages[i]);
11494cb5300bSChris Mason 	}
11501f12bd06SLiu Bo 	btrfs_delalloc_release_space(inode, page_cnt << PAGE_CACHE_SHIFT);
11514cb5300bSChris Mason 	return ret;
11524cb5300bSChris Mason 
11534cb5300bSChris Mason }
11544cb5300bSChris Mason 
11554cb5300bSChris Mason int btrfs_defrag_file(struct inode *inode, struct file *file,
11564cb5300bSChris Mason 		      struct btrfs_ioctl_defrag_range_args *range,
11574cb5300bSChris Mason 		      u64 newer_than, unsigned long max_to_defrag)
11584cb5300bSChris Mason {
11594cb5300bSChris Mason 	struct btrfs_root *root = BTRFS_I(inode)->root;
11604cb5300bSChris Mason 	struct file_ra_state *ra = NULL;
11614cb5300bSChris Mason 	unsigned long last_index;
1162151a31b2SLi Zefan 	u64 isize = i_size_read(inode);
1163940100a4SChris Mason 	u64 last_len = 0;
1164940100a4SChris Mason 	u64 skip = 0;
1165940100a4SChris Mason 	u64 defrag_end = 0;
11664cb5300bSChris Mason 	u64 newer_off = range->start;
1167f46b5a66SChristoph Hellwig 	unsigned long i;
1168008873eaSLi Zefan 	unsigned long ra_index = 0;
1169f46b5a66SChristoph Hellwig 	int ret;
11704cb5300bSChris Mason 	int defrag_count = 0;
11711a419d85SLi Zefan 	int compress_type = BTRFS_COMPRESS_ZLIB;
11724cb5300bSChris Mason 	int extent_thresh = range->extent_thresh;
1173008873eaSLi Zefan 	int max_cluster = (256 * 1024) >> PAGE_CACHE_SHIFT;
1174008873eaSLi Zefan 	int cluster = max_cluster;
11754cb5300bSChris Mason 	u64 new_align = ~((u64)128 * 1024 - 1);
11764cb5300bSChris Mason 	struct page **pages = NULL;
11774cb5300bSChris Mason 
11780abd5b17SLiu Bo 	if (isize == 0)
11790abd5b17SLiu Bo 		return 0;
11800abd5b17SLiu Bo 
11810abd5b17SLiu Bo 	if (range->start >= isize)
11820abd5b17SLiu Bo 		return -EINVAL;
11831a419d85SLi Zefan 
11841a419d85SLi Zefan 	if (range->flags & BTRFS_DEFRAG_RANGE_COMPRESS) {
11851a419d85SLi Zefan 		if (range->compress_type > BTRFS_COMPRESS_TYPES)
11861a419d85SLi Zefan 			return -EINVAL;
11871a419d85SLi Zefan 		if (range->compress_type)
11881a419d85SLi Zefan 			compress_type = range->compress_type;
11891a419d85SLi Zefan 	}
1190f46b5a66SChristoph Hellwig 
11910abd5b17SLiu Bo 	if (extent_thresh == 0)
11920abd5b17SLiu Bo 		extent_thresh = 256 * 1024;
1193f46b5a66SChristoph Hellwig 
11944cb5300bSChris Mason 	/*
11954cb5300bSChris Mason 	 * if we were not given a file, allocate a readahead
11964cb5300bSChris Mason 	 * context
11974cb5300bSChris Mason 	 */
11984cb5300bSChris Mason 	if (!file) {
11994cb5300bSChris Mason 		ra = kzalloc(sizeof(*ra), GFP_NOFS);
12004cb5300bSChris Mason 		if (!ra)
12014cb5300bSChris Mason 			return -ENOMEM;
12024cb5300bSChris Mason 		file_ra_state_init(ra, inode->i_mapping);
12034cb5300bSChris Mason 	} else {
12044cb5300bSChris Mason 		ra = &file->f_ra;
12054cb5300bSChris Mason 	}
12064cb5300bSChris Mason 
1207d9b0d9baSDulshani Gunawardhana 	pages = kmalloc_array(max_cluster, sizeof(struct page *),
12084cb5300bSChris Mason 			GFP_NOFS);
12094cb5300bSChris Mason 	if (!pages) {
12104cb5300bSChris Mason 		ret = -ENOMEM;
12114cb5300bSChris Mason 		goto out_ra;
12124cb5300bSChris Mason 	}
12134cb5300bSChris Mason 
12144cb5300bSChris Mason 	/* find the last page to defrag */
12151e701a32SChris Mason 	if (range->start + range->len > range->start) {
1216151a31b2SLi Zefan 		last_index = min_t(u64, isize - 1,
12171e701a32SChris Mason 			 range->start + range->len - 1) >> PAGE_CACHE_SHIFT;
12181e701a32SChris Mason 	} else {
1219151a31b2SLi Zefan 		last_index = (isize - 1) >> PAGE_CACHE_SHIFT;
12201e701a32SChris Mason 	}
12211e701a32SChris Mason 
12224cb5300bSChris Mason 	if (newer_than) {
12234cb5300bSChris Mason 		ret = find_new_extents(root, inode, newer_than,
12244cb5300bSChris Mason 				       &newer_off, 64 * 1024);
12254cb5300bSChris Mason 		if (!ret) {
12264cb5300bSChris Mason 			range->start = newer_off;
12274cb5300bSChris Mason 			/*
12284cb5300bSChris Mason 			 * we always align our defrag to help keep
12294cb5300bSChris Mason 			 * the extents in the file evenly spaced
12304cb5300bSChris Mason 			 */
12314cb5300bSChris Mason 			i = (newer_off & new_align) >> PAGE_CACHE_SHIFT;
12324cb5300bSChris Mason 		} else
12334cb5300bSChris Mason 			goto out_ra;
12344cb5300bSChris Mason 	} else {
12351e701a32SChris Mason 		i = range->start >> PAGE_CACHE_SHIFT;
12364cb5300bSChris Mason 	}
12374cb5300bSChris Mason 	if (!max_to_defrag)
12387ec31b54SLiu Bo 		max_to_defrag = last_index + 1;
12394cb5300bSChris Mason 
12402a0f7f57SLi Zefan 	/*
12412a0f7f57SLi Zefan 	 * make writeback starts from i, so the defrag range can be
12422a0f7f57SLi Zefan 	 * written sequentially.
12432a0f7f57SLi Zefan 	 */
12442a0f7f57SLi Zefan 	if (i < inode->i_mapping->writeback_index)
12452a0f7f57SLi Zefan 		inode->i_mapping->writeback_index = i;
12462a0f7f57SLi Zefan 
1247f7f43cc8SChris Mason 	while (i <= last_index && defrag_count < max_to_defrag &&
1248f7f43cc8SChris Mason 	       (i < (i_size_read(inode) + PAGE_CACHE_SIZE - 1) >>
1249f7f43cc8SChris Mason 		PAGE_CACHE_SHIFT)) {
12504cb5300bSChris Mason 		/*
12514cb5300bSChris Mason 		 * make sure we stop running if someone unmounts
12524cb5300bSChris Mason 		 * the FS
12534cb5300bSChris Mason 		 */
12544cb5300bSChris Mason 		if (!(inode->i_sb->s_flags & MS_ACTIVE))
12554cb5300bSChris Mason 			break;
12564cb5300bSChris Mason 
1257210549ebSDavid Sterba 		if (btrfs_defrag_cancelled(root->fs_info)) {
1258210549ebSDavid Sterba 			printk(KERN_DEBUG "btrfs: defrag_file cancelled\n");
1259210549ebSDavid Sterba 			ret = -EAGAIN;
1260210549ebSDavid Sterba 			break;
1261210549ebSDavid Sterba 		}
1262210549ebSDavid Sterba 
126366c26892SLiu Bo 		if (!should_defrag_range(inode, (u64)i << PAGE_CACHE_SHIFT,
12646c282eb4SLi Zefan 					 extent_thresh, &last_len, &skip,
1265a43a2111SAndrew Mahone 					 &defrag_end, range->flags &
1266a43a2111SAndrew Mahone 					 BTRFS_DEFRAG_RANGE_COMPRESS)) {
1267940100a4SChris Mason 			unsigned long next;
1268940100a4SChris Mason 			/*
1269940100a4SChris Mason 			 * the should_defrag function tells us how much to skip
1270940100a4SChris Mason 			 * bump our counter by the suggested amount
1271940100a4SChris Mason 			 */
1272940100a4SChris Mason 			next = (skip + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
1273940100a4SChris Mason 			i = max(i + 1, next);
1274940100a4SChris Mason 			continue;
1275940100a4SChris Mason 		}
1276008873eaSLi Zefan 
1277008873eaSLi Zefan 		if (!newer_than) {
1278008873eaSLi Zefan 			cluster = (PAGE_CACHE_ALIGN(defrag_end) >>
1279008873eaSLi Zefan 				   PAGE_CACHE_SHIFT) - i;
1280008873eaSLi Zefan 			cluster = min(cluster, max_cluster);
1281008873eaSLi Zefan 		} else {
1282008873eaSLi Zefan 			cluster = max_cluster;
1283008873eaSLi Zefan 		}
1284008873eaSLi Zefan 
1285008873eaSLi Zefan 		if (i + cluster > ra_index) {
1286008873eaSLi Zefan 			ra_index = max(i, ra_index);
1287008873eaSLi Zefan 			btrfs_force_ra(inode->i_mapping, ra, file, ra_index,
1288008873eaSLi Zefan 				       cluster);
1289008873eaSLi Zefan 			ra_index += max_cluster;
1290008873eaSLi Zefan 		}
12914cb5300bSChris Mason 
1292ecb8bea8SLiu Bo 		mutex_lock(&inode->i_mutex);
1293633085c7SFilipe David Borba Manana 		if (range->flags & BTRFS_DEFRAG_RANGE_COMPRESS)
1294633085c7SFilipe David Borba Manana 			BTRFS_I(inode)->force_compress = compress_type;
1295008873eaSLi Zefan 		ret = cluster_pages_for_defrag(inode, pages, i, cluster);
1296ecb8bea8SLiu Bo 		if (ret < 0) {
1297ecb8bea8SLiu Bo 			mutex_unlock(&inode->i_mutex);
12984cb5300bSChris Mason 			goto out_ra;
1299ecb8bea8SLiu Bo 		}
13004cb5300bSChris Mason 
13014cb5300bSChris Mason 		defrag_count += ret;
1302d0e1d66bSNamjae Jeon 		balance_dirty_pages_ratelimited(inode->i_mapping);
1303ecb8bea8SLiu Bo 		mutex_unlock(&inode->i_mutex);
13044cb5300bSChris Mason 
13054cb5300bSChris Mason 		if (newer_than) {
13064cb5300bSChris Mason 			if (newer_off == (u64)-1)
13074cb5300bSChris Mason 				break;
13084cb5300bSChris Mason 
1309e1f041e1SLiu Bo 			if (ret > 0)
1310e1f041e1SLiu Bo 				i += ret;
1311e1f041e1SLiu Bo 
13124cb5300bSChris Mason 			newer_off = max(newer_off + 1,
13134cb5300bSChris Mason 					(u64)i << PAGE_CACHE_SHIFT);
13144cb5300bSChris Mason 
13154cb5300bSChris Mason 			ret = find_new_extents(root, inode,
13164cb5300bSChris Mason 					       newer_than, &newer_off,
13174cb5300bSChris Mason 					       64 * 1024);
13184cb5300bSChris Mason 			if (!ret) {
13194cb5300bSChris Mason 				range->start = newer_off;
13204cb5300bSChris Mason 				i = (newer_off & new_align) >> PAGE_CACHE_SHIFT;
13214cb5300bSChris Mason 			} else {
13224cb5300bSChris Mason 				break;
1323940100a4SChris Mason 			}
13244cb5300bSChris Mason 		} else {
1325008873eaSLi Zefan 			if (ret > 0) {
1326cbcc8326SLi Zefan 				i += ret;
1327008873eaSLi Zefan 				last_len += ret << PAGE_CACHE_SHIFT;
1328008873eaSLi Zefan 			} else {
1329940100a4SChris Mason 				i++;
1330008873eaSLi Zefan 				last_len = 0;
1331008873eaSLi Zefan 			}
1332f46b5a66SChristoph Hellwig 		}
13334cb5300bSChris Mason 	}
1334f46b5a66SChristoph Hellwig 
13351e701a32SChris Mason 	if ((range->flags & BTRFS_DEFRAG_RANGE_START_IO))
13361e701a32SChris Mason 		filemap_flush(inode->i_mapping);
13371e701a32SChris Mason 
13381e701a32SChris Mason 	if ((range->flags & BTRFS_DEFRAG_RANGE_COMPRESS)) {
13391e701a32SChris Mason 		/* the filemap_flush will queue IO into the worker threads, but
13401e701a32SChris Mason 		 * we have to make sure the IO is actually started and that
13411e701a32SChris Mason 		 * ordered extents get created before we return
13421e701a32SChris Mason 		 */
13431e701a32SChris Mason 		atomic_inc(&root->fs_info->async_submit_draining);
13441e701a32SChris Mason 		while (atomic_read(&root->fs_info->nr_async_submits) ||
13451e701a32SChris Mason 		      atomic_read(&root->fs_info->async_delalloc_pages)) {
13461e701a32SChris Mason 			wait_event(root->fs_info->async_submit_wait,
13471e701a32SChris Mason 			   (atomic_read(&root->fs_info->nr_async_submits) == 0 &&
13481e701a32SChris Mason 			    atomic_read(&root->fs_info->async_delalloc_pages) == 0));
13491e701a32SChris Mason 		}
13501e701a32SChris Mason 		atomic_dec(&root->fs_info->async_submit_draining);
13511e701a32SChris Mason 	}
13521e701a32SChris Mason 
13531a419d85SLi Zefan 	if (range->compress_type == BTRFS_COMPRESS_LZO) {
13542b0ce2c2SMitch Harder 		btrfs_set_fs_incompat(root->fs_info, COMPRESS_LZO);
13551a419d85SLi Zefan 	}
13561a419d85SLi Zefan 
135760ccf82fSDiego Calleja 	ret = defrag_count;
1358940100a4SChris Mason 
13594cb5300bSChris Mason out_ra:
1360633085c7SFilipe David Borba Manana 	if (range->flags & BTRFS_DEFRAG_RANGE_COMPRESS) {
1361633085c7SFilipe David Borba Manana 		mutex_lock(&inode->i_mutex);
1362633085c7SFilipe David Borba Manana 		BTRFS_I(inode)->force_compress = BTRFS_COMPRESS_NONE;
1363633085c7SFilipe David Borba Manana 		mutex_unlock(&inode->i_mutex);
1364633085c7SFilipe David Borba Manana 	}
13654cb5300bSChris Mason 	if (!file)
13664cb5300bSChris Mason 		kfree(ra);
13674cb5300bSChris Mason 	kfree(pages);
1368940100a4SChris Mason 	return ret;
1369f46b5a66SChristoph Hellwig }
1370f46b5a66SChristoph Hellwig 
1371198605a8SMiao Xie static noinline int btrfs_ioctl_resize(struct file *file,
137276dda93cSYan, Zheng 					void __user *arg)
1373f46b5a66SChristoph Hellwig {
1374f46b5a66SChristoph Hellwig 	u64 new_size;
1375f46b5a66SChristoph Hellwig 	u64 old_size;
1376f46b5a66SChristoph Hellwig 	u64 devid = 1;
1377496ad9aaSAl Viro 	struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
1378f46b5a66SChristoph Hellwig 	struct btrfs_ioctl_vol_args *vol_args;
1379f46b5a66SChristoph Hellwig 	struct btrfs_trans_handle *trans;
1380f46b5a66SChristoph Hellwig 	struct btrfs_device *device = NULL;
1381f46b5a66SChristoph Hellwig 	char *sizestr;
1382f46b5a66SChristoph Hellwig 	char *devstr = NULL;
1383f46b5a66SChristoph Hellwig 	int ret = 0;
1384f46b5a66SChristoph Hellwig 	int mod = 0;
1385f46b5a66SChristoph Hellwig 
1386e441d54dSChris Mason 	if (!capable(CAP_SYS_ADMIN))
1387e441d54dSChris Mason 		return -EPERM;
1388e441d54dSChris Mason 
1389198605a8SMiao Xie 	ret = mnt_want_write_file(file);
1390198605a8SMiao Xie 	if (ret)
1391198605a8SMiao Xie 		return ret;
1392198605a8SMiao Xie 
13935ac00addSStefan Behrens 	if (atomic_xchg(&root->fs_info->mutually_exclusive_operation_running,
13945ac00addSStefan Behrens 			1)) {
139597547676SMiao Xie 		mnt_drop_write_file(file);
1396e57138b3SAnand Jain 		return BTRFS_ERROR_DEV_EXCL_RUN_IN_PROGRESS;
1397c9e9f97bSIlya Dryomov 	}
1398c9e9f97bSIlya Dryomov 
13995ac00addSStefan Behrens 	mutex_lock(&root->fs_info->volume_mutex);
1400dae7b665SLi Zefan 	vol_args = memdup_user(arg, sizeof(*vol_args));
1401c9e9f97bSIlya Dryomov 	if (IS_ERR(vol_args)) {
1402c9e9f97bSIlya Dryomov 		ret = PTR_ERR(vol_args);
1403c9e9f97bSIlya Dryomov 		goto out;
1404c9e9f97bSIlya Dryomov 	}
14055516e595SMark Fasheh 
14065516e595SMark Fasheh 	vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
1407f46b5a66SChristoph Hellwig 
1408f46b5a66SChristoph Hellwig 	sizestr = vol_args->name;
1409f46b5a66SChristoph Hellwig 	devstr = strchr(sizestr, ':');
1410f46b5a66SChristoph Hellwig 	if (devstr) {
1411f46b5a66SChristoph Hellwig 		char *end;
1412f46b5a66SChristoph Hellwig 		sizestr = devstr + 1;
1413f46b5a66SChristoph Hellwig 		*devstr = '\0';
1414f46b5a66SChristoph Hellwig 		devstr = vol_args->name;
1415f46b5a66SChristoph Hellwig 		devid = simple_strtoull(devstr, &end, 10);
1416dfd79829SMiao Xie 		if (!devid) {
1417dfd79829SMiao Xie 			ret = -EINVAL;
1418dfd79829SMiao Xie 			goto out_free;
1419dfd79829SMiao Xie 		}
1420c1c9ff7cSGeert Uytterhoeven 		printk(KERN_INFO "btrfs: resizing devid %llu\n", devid);
1421f46b5a66SChristoph Hellwig 	}
1422dba60f3fSMiao Xie 
1423aa1b8cd4SStefan Behrens 	device = btrfs_find_device(root->fs_info, devid, NULL, NULL);
1424f46b5a66SChristoph Hellwig 	if (!device) {
14255bb14682SArnd Hannemann 		printk(KERN_INFO "btrfs: resizer unable to find device %llu\n",
1426c1c9ff7cSGeert Uytterhoeven 		       devid);
1427dfd79829SMiao Xie 		ret = -ENODEV;
1428c9e9f97bSIlya Dryomov 		goto out_free;
1429f46b5a66SChristoph Hellwig 	}
1430dba60f3fSMiao Xie 
1431dba60f3fSMiao Xie 	if (!device->writeable) {
14324e42ae1bSLiu Bo 		printk(KERN_INFO "btrfs: resizer unable to apply on "
1433dba60f3fSMiao Xie 		       "readonly device %llu\n",
1434c1c9ff7cSGeert Uytterhoeven 		       devid);
1435dfd79829SMiao Xie 		ret = -EPERM;
14364e42ae1bSLiu Bo 		goto out_free;
14374e42ae1bSLiu Bo 	}
14384e42ae1bSLiu Bo 
1439f46b5a66SChristoph Hellwig 	if (!strcmp(sizestr, "max"))
1440f46b5a66SChristoph Hellwig 		new_size = device->bdev->bd_inode->i_size;
1441f46b5a66SChristoph Hellwig 	else {
1442f46b5a66SChristoph Hellwig 		if (sizestr[0] == '-') {
1443f46b5a66SChristoph Hellwig 			mod = -1;
1444f46b5a66SChristoph Hellwig 			sizestr++;
1445f46b5a66SChristoph Hellwig 		} else if (sizestr[0] == '+') {
1446f46b5a66SChristoph Hellwig 			mod = 1;
1447f46b5a66SChristoph Hellwig 			sizestr++;
1448f46b5a66SChristoph Hellwig 		}
144991748467SAkinobu Mita 		new_size = memparse(sizestr, NULL);
1450f46b5a66SChristoph Hellwig 		if (new_size == 0) {
1451f46b5a66SChristoph Hellwig 			ret = -EINVAL;
1452c9e9f97bSIlya Dryomov 			goto out_free;
1453f46b5a66SChristoph Hellwig 		}
1454f46b5a66SChristoph Hellwig 	}
1455f46b5a66SChristoph Hellwig 
145663a212abSStefan Behrens 	if (device->is_tgtdev_for_dev_replace) {
1457dfd79829SMiao Xie 		ret = -EPERM;
145863a212abSStefan Behrens 		goto out_free;
145963a212abSStefan Behrens 	}
146063a212abSStefan Behrens 
1461f46b5a66SChristoph Hellwig 	old_size = device->total_bytes;
1462f46b5a66SChristoph Hellwig 
1463f46b5a66SChristoph Hellwig 	if (mod < 0) {
1464f46b5a66SChristoph Hellwig 		if (new_size > old_size) {
1465f46b5a66SChristoph Hellwig 			ret = -EINVAL;
1466c9e9f97bSIlya Dryomov 			goto out_free;
1467f46b5a66SChristoph Hellwig 		}
1468f46b5a66SChristoph Hellwig 		new_size = old_size - new_size;
1469f46b5a66SChristoph Hellwig 	} else if (mod > 0) {
1470f46b5a66SChristoph Hellwig 		new_size = old_size + new_size;
1471f46b5a66SChristoph Hellwig 	}
1472f46b5a66SChristoph Hellwig 
1473f46b5a66SChristoph Hellwig 	if (new_size < 256 * 1024 * 1024) {
1474f46b5a66SChristoph Hellwig 		ret = -EINVAL;
1475c9e9f97bSIlya Dryomov 		goto out_free;
1476f46b5a66SChristoph Hellwig 	}
1477f46b5a66SChristoph Hellwig 	if (new_size > device->bdev->bd_inode->i_size) {
1478f46b5a66SChristoph Hellwig 		ret = -EFBIG;
1479c9e9f97bSIlya Dryomov 		goto out_free;
1480f46b5a66SChristoph Hellwig 	}
1481f46b5a66SChristoph Hellwig 
1482f46b5a66SChristoph Hellwig 	do_div(new_size, root->sectorsize);
1483f46b5a66SChristoph Hellwig 	new_size *= root->sectorsize;
1484f46b5a66SChristoph Hellwig 
1485606686eeSJosef Bacik 	printk_in_rcu(KERN_INFO "btrfs: new size for %s is %llu\n",
1486c1c9ff7cSGeert Uytterhoeven 		      rcu_str_deref(device->name), new_size);
1487f46b5a66SChristoph Hellwig 
1488f46b5a66SChristoph Hellwig 	if (new_size > old_size) {
1489a22285a6SYan, Zheng 		trans = btrfs_start_transaction(root, 0);
149098d5dc13STsutomu Itoh 		if (IS_ERR(trans)) {
149198d5dc13STsutomu Itoh 			ret = PTR_ERR(trans);
1492c9e9f97bSIlya Dryomov 			goto out_free;
149398d5dc13STsutomu Itoh 		}
1494f46b5a66SChristoph Hellwig 		ret = btrfs_grow_device(trans, device, new_size);
1495f46b5a66SChristoph Hellwig 		btrfs_commit_transaction(trans, root);
1496ece7d20eSMike Fleetwood 	} else if (new_size < old_size) {
1497f46b5a66SChristoph Hellwig 		ret = btrfs_shrink_device(device, new_size);
14980253f40eSjeff.liu 	} /* equal, nothing need to do */
1499f46b5a66SChristoph Hellwig 
1500c9e9f97bSIlya Dryomov out_free:
1501f46b5a66SChristoph Hellwig 	kfree(vol_args);
1502c9e9f97bSIlya Dryomov out:
1503c9e9f97bSIlya Dryomov 	mutex_unlock(&root->fs_info->volume_mutex);
15045ac00addSStefan Behrens 	atomic_set(&root->fs_info->mutually_exclusive_operation_running, 0);
150518f39c41SIlya Dryomov 	mnt_drop_write_file(file);
1506f46b5a66SChristoph Hellwig 	return ret;
1507f46b5a66SChristoph Hellwig }
1508f46b5a66SChristoph Hellwig 
150972fd032eSSage Weil static noinline int btrfs_ioctl_snap_create_transid(struct file *file,
15106f72c7e2SArne Jansen 				char *name, unsigned long fd, int subvol,
15116f72c7e2SArne Jansen 				u64 *transid, bool readonly,
15128696c533SMiao Xie 				struct btrfs_qgroup_inherit *inherit)
1513f46b5a66SChristoph Hellwig {
1514f46b5a66SChristoph Hellwig 	int namelen;
15153de4586cSChris Mason 	int ret = 0;
1516f46b5a66SChristoph Hellwig 
1517a874a63eSLiu Bo 	ret = mnt_want_write_file(file);
1518a874a63eSLiu Bo 	if (ret)
1519a874a63eSLiu Bo 		goto out;
1520a874a63eSLiu Bo 
152172fd032eSSage Weil 	namelen = strlen(name);
152272fd032eSSage Weil 	if (strchr(name, '/')) {
1523f46b5a66SChristoph Hellwig 		ret = -EINVAL;
1524a874a63eSLiu Bo 		goto out_drop_write;
1525f46b5a66SChristoph Hellwig 	}
1526f46b5a66SChristoph Hellwig 
152716780cabSChris Mason 	if (name[0] == '.' &&
152816780cabSChris Mason 	   (namelen == 1 || (name[1] == '.' && namelen == 2))) {
152916780cabSChris Mason 		ret = -EEXIST;
1530a874a63eSLiu Bo 		goto out_drop_write;
153116780cabSChris Mason 	}
153216780cabSChris Mason 
15333de4586cSChris Mason 	if (subvol) {
153472fd032eSSage Weil 		ret = btrfs_mksubvol(&file->f_path, name, namelen,
15356f72c7e2SArne Jansen 				     NULL, transid, readonly, inherit);
1536cb8e7090SChristoph Hellwig 	} else {
15372903ff01SAl Viro 		struct fd src = fdget(fd);
15383de4586cSChris Mason 		struct inode *src_inode;
15392903ff01SAl Viro 		if (!src.file) {
15403de4586cSChris Mason 			ret = -EINVAL;
1541a874a63eSLiu Bo 			goto out_drop_write;
15423de4586cSChris Mason 		}
15433de4586cSChris Mason 
1544496ad9aaSAl Viro 		src_inode = file_inode(src.file);
1545496ad9aaSAl Viro 		if (src_inode->i_sb != file_inode(file)->i_sb) {
1546d397712bSChris Mason 			printk(KERN_INFO "btrfs: Snapshot src from "
1547d397712bSChris Mason 			       "another FS\n");
15483de4586cSChris Mason 			ret = -EINVAL;
1549ecd18815SAl Viro 		} else {
155072fd032eSSage Weil 			ret = btrfs_mksubvol(&file->f_path, name, namelen,
155172fd032eSSage Weil 					     BTRFS_I(src_inode)->root,
15526f72c7e2SArne Jansen 					     transid, readonly, inherit);
1553ecd18815SAl Viro 		}
15542903ff01SAl Viro 		fdput(src);
1555cb8e7090SChristoph Hellwig 	}
1556a874a63eSLiu Bo out_drop_write:
1557a874a63eSLiu Bo 	mnt_drop_write_file(file);
1558f46b5a66SChristoph Hellwig out:
155972fd032eSSage Weil 	return ret;
156072fd032eSSage Weil }
156172fd032eSSage Weil 
156272fd032eSSage Weil static noinline int btrfs_ioctl_snap_create(struct file *file,
1563fa0d2b9bSLi Zefan 					    void __user *arg, int subvol)
156472fd032eSSage Weil {
1565fa0d2b9bSLi Zefan 	struct btrfs_ioctl_vol_args *vol_args;
156672fd032eSSage Weil 	int ret;
156772fd032eSSage Weil 
1568fa0d2b9bSLi Zefan 	vol_args = memdup_user(arg, sizeof(*vol_args));
1569fa0d2b9bSLi Zefan 	if (IS_ERR(vol_args))
1570fa0d2b9bSLi Zefan 		return PTR_ERR(vol_args);
1571fa0d2b9bSLi Zefan 	vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
1572fa0d2b9bSLi Zefan 
1573fa0d2b9bSLi Zefan 	ret = btrfs_ioctl_snap_create_transid(file, vol_args->name,
1574b83cc969SLi Zefan 					      vol_args->fd, subvol,
15756f72c7e2SArne Jansen 					      NULL, false, NULL);
1576fa0d2b9bSLi Zefan 
1577fa0d2b9bSLi Zefan 	kfree(vol_args);
1578fa0d2b9bSLi Zefan 	return ret;
1579fa0d2b9bSLi Zefan }
1580fa0d2b9bSLi Zefan 
1581fa0d2b9bSLi Zefan static noinline int btrfs_ioctl_snap_create_v2(struct file *file,
1582fa0d2b9bSLi Zefan 					       void __user *arg, int subvol)
1583fa0d2b9bSLi Zefan {
1584fa0d2b9bSLi Zefan 	struct btrfs_ioctl_vol_args_v2 *vol_args;
1585fa0d2b9bSLi Zefan 	int ret;
1586fdfb1e4fSLi Zefan 	u64 transid = 0;
1587fdfb1e4fSLi Zefan 	u64 *ptr = NULL;
1588b83cc969SLi Zefan 	bool readonly = false;
15896f72c7e2SArne Jansen 	struct btrfs_qgroup_inherit *inherit = NULL;
159072fd032eSSage Weil 
1591fa0d2b9bSLi Zefan 	vol_args = memdup_user(arg, sizeof(*vol_args));
1592fa0d2b9bSLi Zefan 	if (IS_ERR(vol_args))
1593fa0d2b9bSLi Zefan 		return PTR_ERR(vol_args);
1594fa0d2b9bSLi Zefan 	vol_args->name[BTRFS_SUBVOL_NAME_MAX] = '\0';
1595fdfb1e4fSLi Zefan 
1596b83cc969SLi Zefan 	if (vol_args->flags &
15976f72c7e2SArne Jansen 	    ~(BTRFS_SUBVOL_CREATE_ASYNC | BTRFS_SUBVOL_RDONLY |
15986f72c7e2SArne Jansen 	      BTRFS_SUBVOL_QGROUP_INHERIT)) {
1599b83cc969SLi Zefan 		ret = -EOPNOTSUPP;
1600fdfb1e4fSLi Zefan 		goto out;
1601fdfb1e4fSLi Zefan 	}
1602fdfb1e4fSLi Zefan 
1603fa0d2b9bSLi Zefan 	if (vol_args->flags & BTRFS_SUBVOL_CREATE_ASYNC)
1604fdfb1e4fSLi Zefan 		ptr = &transid;
1605b83cc969SLi Zefan 	if (vol_args->flags & BTRFS_SUBVOL_RDONLY)
1606b83cc969SLi Zefan 		readonly = true;
16076f72c7e2SArne Jansen 	if (vol_args->flags & BTRFS_SUBVOL_QGROUP_INHERIT) {
16086f72c7e2SArne Jansen 		if (vol_args->size > PAGE_CACHE_SIZE) {
16096f72c7e2SArne Jansen 			ret = -EINVAL;
16106f72c7e2SArne Jansen 			goto out;
16116f72c7e2SArne Jansen 		}
16126f72c7e2SArne Jansen 		inherit = memdup_user(vol_args->qgroup_inherit, vol_args->size);
16136f72c7e2SArne Jansen 		if (IS_ERR(inherit)) {
16146f72c7e2SArne Jansen 			ret = PTR_ERR(inherit);
16156f72c7e2SArne Jansen 			goto out;
16166f72c7e2SArne Jansen 		}
16176f72c7e2SArne Jansen 	}
161875eaa0e2SSage Weil 
1619fa0d2b9bSLi Zefan 	ret = btrfs_ioctl_snap_create_transid(file, vol_args->name,
16206f72c7e2SArne Jansen 					      vol_args->fd, subvol, ptr,
16218696c533SMiao Xie 					      readonly, inherit);
162275eaa0e2SSage Weil 
1623fdfb1e4fSLi Zefan 	if (ret == 0 && ptr &&
162475eaa0e2SSage Weil 	    copy_to_user(arg +
1625fdfb1e4fSLi Zefan 			 offsetof(struct btrfs_ioctl_vol_args_v2,
1626fdfb1e4fSLi Zefan 				  transid), ptr, sizeof(*ptr)))
162775eaa0e2SSage Weil 		ret = -EFAULT;
1628fdfb1e4fSLi Zefan out:
1629f46b5a66SChristoph Hellwig 	kfree(vol_args);
16306f72c7e2SArne Jansen 	kfree(inherit);
1631f46b5a66SChristoph Hellwig 	return ret;
1632f46b5a66SChristoph Hellwig }
1633f46b5a66SChristoph Hellwig 
16340caa102dSLi Zefan static noinline int btrfs_ioctl_subvol_getflags(struct file *file,
16350caa102dSLi Zefan 						void __user *arg)
16360caa102dSLi Zefan {
1637496ad9aaSAl Viro 	struct inode *inode = file_inode(file);
16380caa102dSLi Zefan 	struct btrfs_root *root = BTRFS_I(inode)->root;
16390caa102dSLi Zefan 	int ret = 0;
16400caa102dSLi Zefan 	u64 flags = 0;
16410caa102dSLi Zefan 
164233345d01SLi Zefan 	if (btrfs_ino(inode) != BTRFS_FIRST_FREE_OBJECTID)
16430caa102dSLi Zefan 		return -EINVAL;
16440caa102dSLi Zefan 
16450caa102dSLi Zefan 	down_read(&root->fs_info->subvol_sem);
16460caa102dSLi Zefan 	if (btrfs_root_readonly(root))
16470caa102dSLi Zefan 		flags |= BTRFS_SUBVOL_RDONLY;
16480caa102dSLi Zefan 	up_read(&root->fs_info->subvol_sem);
16490caa102dSLi Zefan 
16500caa102dSLi Zefan 	if (copy_to_user(arg, &flags, sizeof(flags)))
16510caa102dSLi Zefan 		ret = -EFAULT;
16520caa102dSLi Zefan 
16530caa102dSLi Zefan 	return ret;
16540caa102dSLi Zefan }
16550caa102dSLi Zefan 
16560caa102dSLi Zefan static noinline int btrfs_ioctl_subvol_setflags(struct file *file,
16570caa102dSLi Zefan 					      void __user *arg)
16580caa102dSLi Zefan {
1659496ad9aaSAl Viro 	struct inode *inode = file_inode(file);
16600caa102dSLi Zefan 	struct btrfs_root *root = BTRFS_I(inode)->root;
16610caa102dSLi Zefan 	struct btrfs_trans_handle *trans;
16620caa102dSLi Zefan 	u64 root_flags;
16630caa102dSLi Zefan 	u64 flags;
16640caa102dSLi Zefan 	int ret = 0;
16650caa102dSLi Zefan 
1666b9ca0664SLiu Bo 	ret = mnt_want_write_file(file);
1667b9ca0664SLiu Bo 	if (ret)
1668b9ca0664SLiu Bo 		goto out;
16690caa102dSLi Zefan 
1670b9ca0664SLiu Bo 	if (btrfs_ino(inode) != BTRFS_FIRST_FREE_OBJECTID) {
1671b9ca0664SLiu Bo 		ret = -EINVAL;
1672b9ca0664SLiu Bo 		goto out_drop_write;
1673b9ca0664SLiu Bo 	}
16740caa102dSLi Zefan 
1675b9ca0664SLiu Bo 	if (copy_from_user(&flags, arg, sizeof(flags))) {
1676b9ca0664SLiu Bo 		ret = -EFAULT;
1677b9ca0664SLiu Bo 		goto out_drop_write;
1678b9ca0664SLiu Bo 	}
16790caa102dSLi Zefan 
1680b9ca0664SLiu Bo 	if (flags & BTRFS_SUBVOL_CREATE_ASYNC) {
1681b9ca0664SLiu Bo 		ret = -EINVAL;
1682b9ca0664SLiu Bo 		goto out_drop_write;
1683b9ca0664SLiu Bo 	}
16840caa102dSLi Zefan 
1685b9ca0664SLiu Bo 	if (flags & ~BTRFS_SUBVOL_RDONLY) {
1686b9ca0664SLiu Bo 		ret = -EOPNOTSUPP;
1687b9ca0664SLiu Bo 		goto out_drop_write;
1688b9ca0664SLiu Bo 	}
16890caa102dSLi Zefan 
1690b9ca0664SLiu Bo 	if (!inode_owner_or_capable(inode)) {
1691b9ca0664SLiu Bo 		ret = -EACCES;
1692b9ca0664SLiu Bo 		goto out_drop_write;
1693b9ca0664SLiu Bo 	}
1694b4dc2b8cSLi Zefan 
16950caa102dSLi Zefan 	down_write(&root->fs_info->subvol_sem);
16960caa102dSLi Zefan 
16970caa102dSLi Zefan 	/* nothing to do */
16980caa102dSLi Zefan 	if (!!(flags & BTRFS_SUBVOL_RDONLY) == btrfs_root_readonly(root))
1699b9ca0664SLiu Bo 		goto out_drop_sem;
17000caa102dSLi Zefan 
17010caa102dSLi Zefan 	root_flags = btrfs_root_flags(&root->root_item);
17020caa102dSLi Zefan 	if (flags & BTRFS_SUBVOL_RDONLY)
17030caa102dSLi Zefan 		btrfs_set_root_flags(&root->root_item,
17040caa102dSLi Zefan 				     root_flags | BTRFS_ROOT_SUBVOL_RDONLY);
17050caa102dSLi Zefan 	else
17060caa102dSLi Zefan 		btrfs_set_root_flags(&root->root_item,
17070caa102dSLi Zefan 				     root_flags & ~BTRFS_ROOT_SUBVOL_RDONLY);
17080caa102dSLi Zefan 
17090caa102dSLi Zefan 	trans = btrfs_start_transaction(root, 1);
17100caa102dSLi Zefan 	if (IS_ERR(trans)) {
17110caa102dSLi Zefan 		ret = PTR_ERR(trans);
17120caa102dSLi Zefan 		goto out_reset;
17130caa102dSLi Zefan 	}
17140caa102dSLi Zefan 
1715b4dc2b8cSLi Zefan 	ret = btrfs_update_root(trans, root->fs_info->tree_root,
17160caa102dSLi Zefan 				&root->root_key, &root->root_item);
17170caa102dSLi Zefan 
17180caa102dSLi Zefan 	btrfs_commit_transaction(trans, root);
17190caa102dSLi Zefan out_reset:
17200caa102dSLi Zefan 	if (ret)
17210caa102dSLi Zefan 		btrfs_set_root_flags(&root->root_item, root_flags);
1722b9ca0664SLiu Bo out_drop_sem:
17230caa102dSLi Zefan 	up_write(&root->fs_info->subvol_sem);
1724b9ca0664SLiu Bo out_drop_write:
1725b9ca0664SLiu Bo 	mnt_drop_write_file(file);
1726b9ca0664SLiu Bo out:
17270caa102dSLi Zefan 	return ret;
17280caa102dSLi Zefan }
17290caa102dSLi Zefan 
173076dda93cSYan, Zheng /*
173176dda93cSYan, Zheng  * helper to check if the subvolume references other subvolumes
173276dda93cSYan, Zheng  */
173376dda93cSYan, Zheng static noinline int may_destroy_subvol(struct btrfs_root *root)
173476dda93cSYan, Zheng {
173576dda93cSYan, Zheng 	struct btrfs_path *path;
1736175a2b87SJosef Bacik 	struct btrfs_dir_item *di;
173776dda93cSYan, Zheng 	struct btrfs_key key;
1738175a2b87SJosef Bacik 	u64 dir_id;
173976dda93cSYan, Zheng 	int ret;
174076dda93cSYan, Zheng 
174176dda93cSYan, Zheng 	path = btrfs_alloc_path();
174276dda93cSYan, Zheng 	if (!path)
174376dda93cSYan, Zheng 		return -ENOMEM;
174476dda93cSYan, Zheng 
1745175a2b87SJosef Bacik 	/* Make sure this root isn't set as the default subvol */
1746175a2b87SJosef Bacik 	dir_id = btrfs_super_root_dir(root->fs_info->super_copy);
1747175a2b87SJosef Bacik 	di = btrfs_lookup_dir_item(NULL, root->fs_info->tree_root, path,
1748175a2b87SJosef Bacik 				   dir_id, "default", 7, 0);
1749175a2b87SJosef Bacik 	if (di && !IS_ERR(di)) {
1750175a2b87SJosef Bacik 		btrfs_dir_item_key_to_cpu(path->nodes[0], di, &key);
1751175a2b87SJosef Bacik 		if (key.objectid == root->root_key.objectid) {
1752175a2b87SJosef Bacik 			ret = -ENOTEMPTY;
1753175a2b87SJosef Bacik 			goto out;
1754175a2b87SJosef Bacik 		}
1755175a2b87SJosef Bacik 		btrfs_release_path(path);
1756175a2b87SJosef Bacik 	}
1757175a2b87SJosef Bacik 
175876dda93cSYan, Zheng 	key.objectid = root->root_key.objectid;
175976dda93cSYan, Zheng 	key.type = BTRFS_ROOT_REF_KEY;
176076dda93cSYan, Zheng 	key.offset = (u64)-1;
176176dda93cSYan, Zheng 
176276dda93cSYan, Zheng 	ret = btrfs_search_slot(NULL, root->fs_info->tree_root,
176376dda93cSYan, Zheng 				&key, path, 0, 0);
176476dda93cSYan, Zheng 	if (ret < 0)
176576dda93cSYan, Zheng 		goto out;
176676dda93cSYan, Zheng 	BUG_ON(ret == 0);
176776dda93cSYan, Zheng 
176876dda93cSYan, Zheng 	ret = 0;
176976dda93cSYan, Zheng 	if (path->slots[0] > 0) {
177076dda93cSYan, Zheng 		path->slots[0]--;
177176dda93cSYan, Zheng 		btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
177276dda93cSYan, Zheng 		if (key.objectid == root->root_key.objectid &&
177376dda93cSYan, Zheng 		    key.type == BTRFS_ROOT_REF_KEY)
177476dda93cSYan, Zheng 			ret = -ENOTEMPTY;
177576dda93cSYan, Zheng 	}
177676dda93cSYan, Zheng out:
177776dda93cSYan, Zheng 	btrfs_free_path(path);
177876dda93cSYan, Zheng 	return ret;
177976dda93cSYan, Zheng }
178076dda93cSYan, Zheng 
1781ac8e9819SChris Mason static noinline int key_in_sk(struct btrfs_key *key,
1782ac8e9819SChris Mason 			      struct btrfs_ioctl_search_key *sk)
1783ac8e9819SChris Mason {
1784abc6e134SChris Mason 	struct btrfs_key test;
1785abc6e134SChris Mason 	int ret;
1786abc6e134SChris Mason 
1787abc6e134SChris Mason 	test.objectid = sk->min_objectid;
1788abc6e134SChris Mason 	test.type = sk->min_type;
1789abc6e134SChris Mason 	test.offset = sk->min_offset;
1790abc6e134SChris Mason 
1791abc6e134SChris Mason 	ret = btrfs_comp_cpu_keys(key, &test);
1792abc6e134SChris Mason 	if (ret < 0)
1793ac8e9819SChris Mason 		return 0;
1794abc6e134SChris Mason 
1795abc6e134SChris Mason 	test.objectid = sk->max_objectid;
1796abc6e134SChris Mason 	test.type = sk->max_type;
1797abc6e134SChris Mason 	test.offset = sk->max_offset;
1798abc6e134SChris Mason 
1799abc6e134SChris Mason 	ret = btrfs_comp_cpu_keys(key, &test);
1800abc6e134SChris Mason 	if (ret > 0)
1801ac8e9819SChris Mason 		return 0;
1802ac8e9819SChris Mason 	return 1;
1803ac8e9819SChris Mason }
1804ac8e9819SChris Mason 
1805ac8e9819SChris Mason static noinline int copy_to_sk(struct btrfs_root *root,
1806ac8e9819SChris Mason 			       struct btrfs_path *path,
1807ac8e9819SChris Mason 			       struct btrfs_key *key,
1808ac8e9819SChris Mason 			       struct btrfs_ioctl_search_key *sk,
1809ac8e9819SChris Mason 			       char *buf,
1810ac8e9819SChris Mason 			       unsigned long *sk_offset,
1811ac8e9819SChris Mason 			       int *num_found)
1812ac8e9819SChris Mason {
1813ac8e9819SChris Mason 	u64 found_transid;
1814ac8e9819SChris Mason 	struct extent_buffer *leaf;
1815ac8e9819SChris Mason 	struct btrfs_ioctl_search_header sh;
1816ac8e9819SChris Mason 	unsigned long item_off;
1817ac8e9819SChris Mason 	unsigned long item_len;
1818ac8e9819SChris Mason 	int nritems;
1819ac8e9819SChris Mason 	int i;
1820ac8e9819SChris Mason 	int slot;
1821ac8e9819SChris Mason 	int ret = 0;
1822ac8e9819SChris Mason 
1823ac8e9819SChris Mason 	leaf = path->nodes[0];
1824ac8e9819SChris Mason 	slot = path->slots[0];
1825ac8e9819SChris Mason 	nritems = btrfs_header_nritems(leaf);
1826ac8e9819SChris Mason 
1827ac8e9819SChris Mason 	if (btrfs_header_generation(leaf) > sk->max_transid) {
1828ac8e9819SChris Mason 		i = nritems;
1829ac8e9819SChris Mason 		goto advance_key;
1830ac8e9819SChris Mason 	}
1831ac8e9819SChris Mason 	found_transid = btrfs_header_generation(leaf);
1832ac8e9819SChris Mason 
1833ac8e9819SChris Mason 	for (i = slot; i < nritems; i++) {
1834ac8e9819SChris Mason 		item_off = btrfs_item_ptr_offset(leaf, i);
1835ac8e9819SChris Mason 		item_len = btrfs_item_size_nr(leaf, i);
1836ac8e9819SChris Mason 
183703b71c6cSGabriel de Perthuis 		btrfs_item_key_to_cpu(leaf, key, i);
183803b71c6cSGabriel de Perthuis 		if (!key_in_sk(key, sk))
183903b71c6cSGabriel de Perthuis 			continue;
184003b71c6cSGabriel de Perthuis 
184103b71c6cSGabriel de Perthuis 		if (sizeof(sh) + item_len > BTRFS_SEARCH_ARGS_BUFSIZE)
1842ac8e9819SChris Mason 			item_len = 0;
1843ac8e9819SChris Mason 
1844ac8e9819SChris Mason 		if (sizeof(sh) + item_len + *sk_offset >
1845ac8e9819SChris Mason 		    BTRFS_SEARCH_ARGS_BUFSIZE) {
1846ac8e9819SChris Mason 			ret = 1;
1847ac8e9819SChris Mason 			goto overflow;
1848ac8e9819SChris Mason 		}
1849ac8e9819SChris Mason 
1850ac8e9819SChris Mason 		sh.objectid = key->objectid;
1851ac8e9819SChris Mason 		sh.offset = key->offset;
1852ac8e9819SChris Mason 		sh.type = key->type;
1853ac8e9819SChris Mason 		sh.len = item_len;
1854ac8e9819SChris Mason 		sh.transid = found_transid;
1855ac8e9819SChris Mason 
1856ac8e9819SChris Mason 		/* copy search result header */
1857ac8e9819SChris Mason 		memcpy(buf + *sk_offset, &sh, sizeof(sh));
1858ac8e9819SChris Mason 		*sk_offset += sizeof(sh);
1859ac8e9819SChris Mason 
1860ac8e9819SChris Mason 		if (item_len) {
1861ac8e9819SChris Mason 			char *p = buf + *sk_offset;
1862ac8e9819SChris Mason 			/* copy the item */
1863ac8e9819SChris Mason 			read_extent_buffer(leaf, p,
1864ac8e9819SChris Mason 					   item_off, item_len);
1865ac8e9819SChris Mason 			*sk_offset += item_len;
1866ac8e9819SChris Mason 		}
1867e2156867SHugo Mills 		(*num_found)++;
1868ac8e9819SChris Mason 
1869ac8e9819SChris Mason 		if (*num_found >= sk->nr_items)
1870ac8e9819SChris Mason 			break;
1871ac8e9819SChris Mason 	}
1872ac8e9819SChris Mason advance_key:
1873ac8e9819SChris Mason 	ret = 0;
1874abc6e134SChris Mason 	if (key->offset < (u64)-1 && key->offset < sk->max_offset)
1875abc6e134SChris Mason 		key->offset++;
1876abc6e134SChris Mason 	else if (key->type < (u8)-1 && key->type < sk->max_type) {
1877abc6e134SChris Mason 		key->offset = 0;
1878abc6e134SChris Mason 		key->type++;
1879abc6e134SChris Mason 	} else if (key->objectid < (u64)-1 && key->objectid < sk->max_objectid) {
1880abc6e134SChris Mason 		key->offset = 0;
1881abc6e134SChris Mason 		key->type = 0;
1882abc6e134SChris Mason 		key->objectid++;
1883abc6e134SChris Mason 	} else
1884abc6e134SChris Mason 		ret = 1;
1885ac8e9819SChris Mason overflow:
1886ac8e9819SChris Mason 	return ret;
1887ac8e9819SChris Mason }
1888ac8e9819SChris Mason 
1889ac8e9819SChris Mason static noinline int search_ioctl(struct inode *inode,
1890ac8e9819SChris Mason 				 struct btrfs_ioctl_search_args *args)
1891ac8e9819SChris Mason {
1892ac8e9819SChris Mason 	struct btrfs_root *root;
1893ac8e9819SChris Mason 	struct btrfs_key key;
1894ac8e9819SChris Mason 	struct btrfs_path *path;
1895ac8e9819SChris Mason 	struct btrfs_ioctl_search_key *sk = &args->key;
1896ac8e9819SChris Mason 	struct btrfs_fs_info *info = BTRFS_I(inode)->root->fs_info;
1897ac8e9819SChris Mason 	int ret;
1898ac8e9819SChris Mason 	int num_found = 0;
1899ac8e9819SChris Mason 	unsigned long sk_offset = 0;
1900ac8e9819SChris Mason 
1901ac8e9819SChris Mason 	path = btrfs_alloc_path();
1902ac8e9819SChris Mason 	if (!path)
1903ac8e9819SChris Mason 		return -ENOMEM;
1904ac8e9819SChris Mason 
1905ac8e9819SChris Mason 	if (sk->tree_id == 0) {
1906ac8e9819SChris Mason 		/* search the root of the inode that was passed */
1907ac8e9819SChris Mason 		root = BTRFS_I(inode)->root;
1908ac8e9819SChris Mason 	} else {
1909ac8e9819SChris Mason 		key.objectid = sk->tree_id;
1910ac8e9819SChris Mason 		key.type = BTRFS_ROOT_ITEM_KEY;
1911ac8e9819SChris Mason 		key.offset = (u64)-1;
1912ac8e9819SChris Mason 		root = btrfs_read_fs_root_no_name(info, &key);
1913ac8e9819SChris Mason 		if (IS_ERR(root)) {
1914ac8e9819SChris Mason 			printk(KERN_ERR "could not find root %llu\n",
1915ac8e9819SChris Mason 			       sk->tree_id);
1916ac8e9819SChris Mason 			btrfs_free_path(path);
1917ac8e9819SChris Mason 			return -ENOENT;
1918ac8e9819SChris Mason 		}
1919ac8e9819SChris Mason 	}
1920ac8e9819SChris Mason 
1921ac8e9819SChris Mason 	key.objectid = sk->min_objectid;
1922ac8e9819SChris Mason 	key.type = sk->min_type;
1923ac8e9819SChris Mason 	key.offset = sk->min_offset;
1924ac8e9819SChris Mason 
1925ac8e9819SChris Mason 	path->keep_locks = 1;
1926ac8e9819SChris Mason 
1927ac8e9819SChris Mason 	while (1) {
19286174d3cbSFilipe David Borba Manana 		ret = btrfs_search_forward(root, &key, path, sk->min_transid);
1929ac8e9819SChris Mason 		if (ret != 0) {
1930ac8e9819SChris Mason 			if (ret > 0)
1931ac8e9819SChris Mason 				ret = 0;
1932ac8e9819SChris Mason 			goto err;
1933ac8e9819SChris Mason 		}
1934ac8e9819SChris Mason 		ret = copy_to_sk(root, path, &key, sk, args->buf,
1935ac8e9819SChris Mason 				 &sk_offset, &num_found);
1936b3b4aa74SDavid Sterba 		btrfs_release_path(path);
1937ac8e9819SChris Mason 		if (ret || num_found >= sk->nr_items)
1938ac8e9819SChris Mason 			break;
1939ac8e9819SChris Mason 
1940ac8e9819SChris Mason 	}
1941ac8e9819SChris Mason 	ret = 0;
1942ac8e9819SChris Mason err:
1943ac8e9819SChris Mason 	sk->nr_items = num_found;
1944ac8e9819SChris Mason 	btrfs_free_path(path);
1945ac8e9819SChris Mason 	return ret;
1946ac8e9819SChris Mason }
1947ac8e9819SChris Mason 
1948ac8e9819SChris Mason static noinline int btrfs_ioctl_tree_search(struct file *file,
1949ac8e9819SChris Mason 					   void __user *argp)
1950ac8e9819SChris Mason {
1951ac8e9819SChris Mason 	 struct btrfs_ioctl_search_args *args;
1952ac8e9819SChris Mason 	 struct inode *inode;
1953ac8e9819SChris Mason 	 int ret;
1954ac8e9819SChris Mason 
1955ac8e9819SChris Mason 	if (!capable(CAP_SYS_ADMIN))
1956ac8e9819SChris Mason 		return -EPERM;
1957ac8e9819SChris Mason 
19582354d08fSJulia Lawall 	args = memdup_user(argp, sizeof(*args));
19592354d08fSJulia Lawall 	if (IS_ERR(args))
19602354d08fSJulia Lawall 		return PTR_ERR(args);
1961ac8e9819SChris Mason 
1962496ad9aaSAl Viro 	inode = file_inode(file);
1963ac8e9819SChris Mason 	ret = search_ioctl(inode, args);
1964ac8e9819SChris Mason 	if (ret == 0 && copy_to_user(argp, args, sizeof(*args)))
1965ac8e9819SChris Mason 		ret = -EFAULT;
1966ac8e9819SChris Mason 	kfree(args);
1967ac8e9819SChris Mason 	return ret;
1968ac8e9819SChris Mason }
1969ac8e9819SChris Mason 
197098d377a0STARUISI Hiroaki /*
1971ac8e9819SChris Mason  * Search INODE_REFs to identify path name of 'dirid' directory
1972ac8e9819SChris Mason  * in a 'tree_id' tree. and sets path name to 'name'.
197398d377a0STARUISI Hiroaki  */
197498d377a0STARUISI Hiroaki static noinline int btrfs_search_path_in_tree(struct btrfs_fs_info *info,
197598d377a0STARUISI Hiroaki 				u64 tree_id, u64 dirid, char *name)
197698d377a0STARUISI Hiroaki {
197798d377a0STARUISI Hiroaki 	struct btrfs_root *root;
197898d377a0STARUISI Hiroaki 	struct btrfs_key key;
1979ac8e9819SChris Mason 	char *ptr;
198098d377a0STARUISI Hiroaki 	int ret = -1;
198198d377a0STARUISI Hiroaki 	int slot;
198298d377a0STARUISI Hiroaki 	int len;
198398d377a0STARUISI Hiroaki 	int total_len = 0;
198498d377a0STARUISI Hiroaki 	struct btrfs_inode_ref *iref;
198598d377a0STARUISI Hiroaki 	struct extent_buffer *l;
198698d377a0STARUISI Hiroaki 	struct btrfs_path *path;
198798d377a0STARUISI Hiroaki 
198898d377a0STARUISI Hiroaki 	if (dirid == BTRFS_FIRST_FREE_OBJECTID) {
198998d377a0STARUISI Hiroaki 		name[0]='\0';
199098d377a0STARUISI Hiroaki 		return 0;
199198d377a0STARUISI Hiroaki 	}
199298d377a0STARUISI Hiroaki 
199398d377a0STARUISI Hiroaki 	path = btrfs_alloc_path();
199498d377a0STARUISI Hiroaki 	if (!path)
199598d377a0STARUISI Hiroaki 		return -ENOMEM;
199698d377a0STARUISI Hiroaki 
1997ac8e9819SChris Mason 	ptr = &name[BTRFS_INO_LOOKUP_PATH_MAX];
199898d377a0STARUISI Hiroaki 
199998d377a0STARUISI Hiroaki 	key.objectid = tree_id;
200098d377a0STARUISI Hiroaki 	key.type = BTRFS_ROOT_ITEM_KEY;
200198d377a0STARUISI Hiroaki 	key.offset = (u64)-1;
200298d377a0STARUISI Hiroaki 	root = btrfs_read_fs_root_no_name(info, &key);
200398d377a0STARUISI Hiroaki 	if (IS_ERR(root)) {
200498d377a0STARUISI Hiroaki 		printk(KERN_ERR "could not find root %llu\n", tree_id);
20058ad6fcabSChris Mason 		ret = -ENOENT;
20068ad6fcabSChris Mason 		goto out;
200798d377a0STARUISI Hiroaki 	}
200898d377a0STARUISI Hiroaki 
200998d377a0STARUISI Hiroaki 	key.objectid = dirid;
201098d377a0STARUISI Hiroaki 	key.type = BTRFS_INODE_REF_KEY;
20118ad6fcabSChris Mason 	key.offset = (u64)-1;
201298d377a0STARUISI Hiroaki 
201398d377a0STARUISI Hiroaki 	while (1) {
201498d377a0STARUISI Hiroaki 		ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
201598d377a0STARUISI Hiroaki 		if (ret < 0)
201698d377a0STARUISI Hiroaki 			goto out;
201718674c6cSFilipe David Borba Manana 		else if (ret > 0) {
201818674c6cSFilipe David Borba Manana 			ret = btrfs_previous_item(root, path, dirid,
201918674c6cSFilipe David Borba Manana 						  BTRFS_INODE_REF_KEY);
202018674c6cSFilipe David Borba Manana 			if (ret < 0)
202118674c6cSFilipe David Borba Manana 				goto out;
202218674c6cSFilipe David Borba Manana 			else if (ret > 0) {
2023ac8e9819SChris Mason 				ret = -ENOENT;
202498d377a0STARUISI Hiroaki 				goto out;
2025ac8e9819SChris Mason 			}
202618674c6cSFilipe David Borba Manana 		}
202718674c6cSFilipe David Borba Manana 
202818674c6cSFilipe David Borba Manana 		l = path->nodes[0];
202918674c6cSFilipe David Borba Manana 		slot = path->slots[0];
203018674c6cSFilipe David Borba Manana 		btrfs_item_key_to_cpu(l, &key, slot);
203198d377a0STARUISI Hiroaki 
203298d377a0STARUISI Hiroaki 		iref = btrfs_item_ptr(l, slot, struct btrfs_inode_ref);
203398d377a0STARUISI Hiroaki 		len = btrfs_inode_ref_name_len(l, iref);
203498d377a0STARUISI Hiroaki 		ptr -= len + 1;
203598d377a0STARUISI Hiroaki 		total_len += len + 1;
2036a696cf35SFilipe David Borba Manana 		if (ptr < name) {
2037a696cf35SFilipe David Borba Manana 			ret = -ENAMETOOLONG;
203898d377a0STARUISI Hiroaki 			goto out;
2039a696cf35SFilipe David Borba Manana 		}
204098d377a0STARUISI Hiroaki 
204198d377a0STARUISI Hiroaki 		*(ptr + len) = '/';
204298d377a0STARUISI Hiroaki 		read_extent_buffer(l, ptr, (unsigned long)(iref + 1), len);
204398d377a0STARUISI Hiroaki 
204498d377a0STARUISI Hiroaki 		if (key.offset == BTRFS_FIRST_FREE_OBJECTID)
204598d377a0STARUISI Hiroaki 			break;
204698d377a0STARUISI Hiroaki 
2047b3b4aa74SDavid Sterba 		btrfs_release_path(path);
204898d377a0STARUISI Hiroaki 		key.objectid = key.offset;
20498ad6fcabSChris Mason 		key.offset = (u64)-1;
205098d377a0STARUISI Hiroaki 		dirid = key.objectid;
205198d377a0STARUISI Hiroaki 	}
205277906a50SLi Zefan 	memmove(name, ptr, total_len);
205398d377a0STARUISI Hiroaki 	name[total_len] = '\0';
205498d377a0STARUISI Hiroaki 	ret = 0;
205598d377a0STARUISI Hiroaki out:
205698d377a0STARUISI Hiroaki 	btrfs_free_path(path);
2057ac8e9819SChris Mason 	return ret;
2058ac8e9819SChris Mason }
2059ac8e9819SChris Mason 
2060ac8e9819SChris Mason static noinline int btrfs_ioctl_ino_lookup(struct file *file,
2061ac8e9819SChris Mason 					   void __user *argp)
2062ac8e9819SChris Mason {
2063ac8e9819SChris Mason 	 struct btrfs_ioctl_ino_lookup_args *args;
2064ac8e9819SChris Mason 	 struct inode *inode;
2065ac8e9819SChris Mason 	 int ret;
2066ac8e9819SChris Mason 
2067ac8e9819SChris Mason 	if (!capable(CAP_SYS_ADMIN))
2068ac8e9819SChris Mason 		return -EPERM;
2069ac8e9819SChris Mason 
20702354d08fSJulia Lawall 	args = memdup_user(argp, sizeof(*args));
20712354d08fSJulia Lawall 	if (IS_ERR(args))
20722354d08fSJulia Lawall 		return PTR_ERR(args);
2073c2b96929SDan Carpenter 
2074496ad9aaSAl Viro 	inode = file_inode(file);
2075ac8e9819SChris Mason 
20761b53ac4dSChris Mason 	if (args->treeid == 0)
20771b53ac4dSChris Mason 		args->treeid = BTRFS_I(inode)->root->root_key.objectid;
20781b53ac4dSChris Mason 
2079ac8e9819SChris Mason 	ret = btrfs_search_path_in_tree(BTRFS_I(inode)->root->fs_info,
2080ac8e9819SChris Mason 					args->treeid, args->objectid,
2081ac8e9819SChris Mason 					args->name);
2082ac8e9819SChris Mason 
2083ac8e9819SChris Mason 	if (ret == 0 && copy_to_user(argp, args, sizeof(*args)))
2084ac8e9819SChris Mason 		ret = -EFAULT;
2085ac8e9819SChris Mason 
2086ac8e9819SChris Mason 	kfree(args);
208798d377a0STARUISI Hiroaki 	return ret;
208898d377a0STARUISI Hiroaki }
208998d377a0STARUISI Hiroaki 
209076dda93cSYan, Zheng static noinline int btrfs_ioctl_snap_destroy(struct file *file,
209176dda93cSYan, Zheng 					     void __user *arg)
209276dda93cSYan, Zheng {
209354563d41SAl Viro 	struct dentry *parent = file->f_path.dentry;
209476dda93cSYan, Zheng 	struct dentry *dentry;
209576dda93cSYan, Zheng 	struct inode *dir = parent->d_inode;
209676dda93cSYan, Zheng 	struct inode *inode;
209776dda93cSYan, Zheng 	struct btrfs_root *root = BTRFS_I(dir)->root;
209876dda93cSYan, Zheng 	struct btrfs_root *dest = NULL;
209976dda93cSYan, Zheng 	struct btrfs_ioctl_vol_args *vol_args;
210076dda93cSYan, Zheng 	struct btrfs_trans_handle *trans;
2101c58aaad2SMiao Xie 	struct btrfs_block_rsv block_rsv;
2102c58aaad2SMiao Xie 	u64 qgroup_reserved;
210376dda93cSYan, Zheng 	int namelen;
210476dda93cSYan, Zheng 	int ret;
210576dda93cSYan, Zheng 	int err = 0;
210676dda93cSYan, Zheng 
210776dda93cSYan, Zheng 	vol_args = memdup_user(arg, sizeof(*vol_args));
210876dda93cSYan, Zheng 	if (IS_ERR(vol_args))
210976dda93cSYan, Zheng 		return PTR_ERR(vol_args);
211076dda93cSYan, Zheng 
211176dda93cSYan, Zheng 	vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
211276dda93cSYan, Zheng 	namelen = strlen(vol_args->name);
211376dda93cSYan, Zheng 	if (strchr(vol_args->name, '/') ||
211476dda93cSYan, Zheng 	    strncmp(vol_args->name, "..", namelen) == 0) {
211576dda93cSYan, Zheng 		err = -EINVAL;
211676dda93cSYan, Zheng 		goto out;
211776dda93cSYan, Zheng 	}
211876dda93cSYan, Zheng 
2119a561be71SAl Viro 	err = mnt_want_write_file(file);
212076dda93cSYan, Zheng 	if (err)
212176dda93cSYan, Zheng 		goto out;
212276dda93cSYan, Zheng 
21235c50c9b8SDavid Sterba 	err = mutex_lock_killable_nested(&dir->i_mutex, I_MUTEX_PARENT);
21245c50c9b8SDavid Sterba 	if (err == -EINTR)
2125e43f998eSDavid Sterba 		goto out_drop_write;
212676dda93cSYan, Zheng 	dentry = lookup_one_len(vol_args->name, parent, namelen);
212776dda93cSYan, Zheng 	if (IS_ERR(dentry)) {
212876dda93cSYan, Zheng 		err = PTR_ERR(dentry);
212976dda93cSYan, Zheng 		goto out_unlock_dir;
213076dda93cSYan, Zheng 	}
213176dda93cSYan, Zheng 
213276dda93cSYan, Zheng 	if (!dentry->d_inode) {
213376dda93cSYan, Zheng 		err = -ENOENT;
213476dda93cSYan, Zheng 		goto out_dput;
213576dda93cSYan, Zheng 	}
213676dda93cSYan, Zheng 
213776dda93cSYan, Zheng 	inode = dentry->d_inode;
21384260f7c7SSage Weil 	dest = BTRFS_I(inode)->root;
21394260f7c7SSage Weil 	if (!capable(CAP_SYS_ADMIN)) {
21404260f7c7SSage Weil 		/*
21414260f7c7SSage Weil 		 * Regular user.  Only allow this with a special mount
21424260f7c7SSage Weil 		 * option, when the user has write+exec access to the
21434260f7c7SSage Weil 		 * subvol root, and when rmdir(2) would have been
21444260f7c7SSage Weil 		 * allowed.
21454260f7c7SSage Weil 		 *
21464260f7c7SSage Weil 		 * Note that this is _not_ check that the subvol is
21474260f7c7SSage Weil 		 * empty or doesn't contain data that we wouldn't
21484260f7c7SSage Weil 		 * otherwise be able to delete.
21494260f7c7SSage Weil 		 *
21504260f7c7SSage Weil 		 * Users who want to delete empty subvols should try
21514260f7c7SSage Weil 		 * rmdir(2).
21524260f7c7SSage Weil 		 */
21534260f7c7SSage Weil 		err = -EPERM;
21544260f7c7SSage Weil 		if (!btrfs_test_opt(root, USER_SUBVOL_RM_ALLOWED))
21554260f7c7SSage Weil 			goto out_dput;
21564260f7c7SSage Weil 
21574260f7c7SSage Weil 		/*
21584260f7c7SSage Weil 		 * Do not allow deletion if the parent dir is the same
21594260f7c7SSage Weil 		 * as the dir to be deleted.  That means the ioctl
21604260f7c7SSage Weil 		 * must be called on the dentry referencing the root
21614260f7c7SSage Weil 		 * of the subvol, not a random directory contained
21624260f7c7SSage Weil 		 * within it.
21634260f7c7SSage Weil 		 */
21644260f7c7SSage Weil 		err = -EINVAL;
21654260f7c7SSage Weil 		if (root == dest)
21664260f7c7SSage Weil 			goto out_dput;
21674260f7c7SSage Weil 
21684260f7c7SSage Weil 		err = inode_permission(inode, MAY_WRITE | MAY_EXEC);
21694260f7c7SSage Weil 		if (err)
21704260f7c7SSage Weil 			goto out_dput;
21715c39da5bSMiao Xie 	}
21724260f7c7SSage Weil 
21735c39da5bSMiao Xie 	/* check if subvolume may be deleted by a user */
21744260f7c7SSage Weil 	err = btrfs_may_delete(dir, dentry, 1);
21754260f7c7SSage Weil 	if (err)
21764260f7c7SSage Weil 		goto out_dput;
21774260f7c7SSage Weil 
217833345d01SLi Zefan 	if (btrfs_ino(inode) != BTRFS_FIRST_FREE_OBJECTID) {
217976dda93cSYan, Zheng 		err = -EINVAL;
218076dda93cSYan, Zheng 		goto out_dput;
218176dda93cSYan, Zheng 	}
218276dda93cSYan, Zheng 
218376dda93cSYan, Zheng 	mutex_lock(&inode->i_mutex);
218476dda93cSYan, Zheng 	err = d_invalidate(dentry);
218576dda93cSYan, Zheng 	if (err)
218676dda93cSYan, Zheng 		goto out_unlock;
218776dda93cSYan, Zheng 
218876dda93cSYan, Zheng 	down_write(&root->fs_info->subvol_sem);
218976dda93cSYan, Zheng 
219076dda93cSYan, Zheng 	err = may_destroy_subvol(dest);
219176dda93cSYan, Zheng 	if (err)
219276dda93cSYan, Zheng 		goto out_up_write;
219376dda93cSYan, Zheng 
2194c58aaad2SMiao Xie 	btrfs_init_block_rsv(&block_rsv, BTRFS_BLOCK_RSV_TEMP);
2195c58aaad2SMiao Xie 	/*
2196c58aaad2SMiao Xie 	 * One for dir inode, two for dir entries, two for root
2197c58aaad2SMiao Xie 	 * ref/backref.
2198c58aaad2SMiao Xie 	 */
2199c58aaad2SMiao Xie 	err = btrfs_subvolume_reserve_metadata(root, &block_rsv,
2200ee3441b4SJeff Mahoney 					       5, &qgroup_reserved, true);
2201c58aaad2SMiao Xie 	if (err)
2202c58aaad2SMiao Xie 		goto out_up_write;
2203c58aaad2SMiao Xie 
2204a22285a6SYan, Zheng 	trans = btrfs_start_transaction(root, 0);
2205a22285a6SYan, Zheng 	if (IS_ERR(trans)) {
2206a22285a6SYan, Zheng 		err = PTR_ERR(trans);
2207c58aaad2SMiao Xie 		goto out_release;
2208a22285a6SYan, Zheng 	}
2209c58aaad2SMiao Xie 	trans->block_rsv = &block_rsv;
2210c58aaad2SMiao Xie 	trans->bytes_reserved = block_rsv.size;
2211a22285a6SYan, Zheng 
221276dda93cSYan, Zheng 	ret = btrfs_unlink_subvol(trans, root, dir,
221376dda93cSYan, Zheng 				dest->root_key.objectid,
221476dda93cSYan, Zheng 				dentry->d_name.name,
221576dda93cSYan, Zheng 				dentry->d_name.len);
221679787eaaSJeff Mahoney 	if (ret) {
221779787eaaSJeff Mahoney 		err = ret;
221879787eaaSJeff Mahoney 		btrfs_abort_transaction(trans, root, ret);
221979787eaaSJeff Mahoney 		goto out_end_trans;
222079787eaaSJeff Mahoney 	}
222176dda93cSYan, Zheng 
222276dda93cSYan, Zheng 	btrfs_record_root_in_trans(trans, dest);
222376dda93cSYan, Zheng 
222476dda93cSYan, Zheng 	memset(&dest->root_item.drop_progress, 0,
222576dda93cSYan, Zheng 		sizeof(dest->root_item.drop_progress));
222676dda93cSYan, Zheng 	dest->root_item.drop_level = 0;
222776dda93cSYan, Zheng 	btrfs_set_root_refs(&dest->root_item, 0);
222876dda93cSYan, Zheng 
2229d68fc57bSYan, Zheng 	if (!xchg(&dest->orphan_item_inserted, 1)) {
223076dda93cSYan, Zheng 		ret = btrfs_insert_orphan_item(trans,
223176dda93cSYan, Zheng 					root->fs_info->tree_root,
223276dda93cSYan, Zheng 					dest->root_key.objectid);
223379787eaaSJeff Mahoney 		if (ret) {
223479787eaaSJeff Mahoney 			btrfs_abort_transaction(trans, root, ret);
223579787eaaSJeff Mahoney 			err = ret;
223679787eaaSJeff Mahoney 			goto out_end_trans;
2237d68fc57bSYan, Zheng 		}
223879787eaaSJeff Mahoney 	}
2239dd5f9615SStefan Behrens 
2240dd5f9615SStefan Behrens 	ret = btrfs_uuid_tree_rem(trans, root->fs_info->uuid_root,
2241dd5f9615SStefan Behrens 				  dest->root_item.uuid, BTRFS_UUID_KEY_SUBVOL,
2242dd5f9615SStefan Behrens 				  dest->root_key.objectid);
2243dd5f9615SStefan Behrens 	if (ret && ret != -ENOENT) {
2244dd5f9615SStefan Behrens 		btrfs_abort_transaction(trans, root, ret);
2245dd5f9615SStefan Behrens 		err = ret;
2246dd5f9615SStefan Behrens 		goto out_end_trans;
2247dd5f9615SStefan Behrens 	}
2248dd5f9615SStefan Behrens 	if (!btrfs_is_empty_uuid(dest->root_item.received_uuid)) {
2249dd5f9615SStefan Behrens 		ret = btrfs_uuid_tree_rem(trans, root->fs_info->uuid_root,
2250dd5f9615SStefan Behrens 					  dest->root_item.received_uuid,
2251dd5f9615SStefan Behrens 					  BTRFS_UUID_KEY_RECEIVED_SUBVOL,
2252dd5f9615SStefan Behrens 					  dest->root_key.objectid);
2253dd5f9615SStefan Behrens 		if (ret && ret != -ENOENT) {
2254dd5f9615SStefan Behrens 			btrfs_abort_transaction(trans, root, ret);
2255dd5f9615SStefan Behrens 			err = ret;
2256dd5f9615SStefan Behrens 			goto out_end_trans;
2257dd5f9615SStefan Behrens 		}
2258dd5f9615SStefan Behrens 	}
2259dd5f9615SStefan Behrens 
226079787eaaSJeff Mahoney out_end_trans:
2261c58aaad2SMiao Xie 	trans->block_rsv = NULL;
2262c58aaad2SMiao Xie 	trans->bytes_reserved = 0;
2263531cb13fSSage Weil 	ret = btrfs_end_transaction(trans, root);
226479787eaaSJeff Mahoney 	if (ret && !err)
226579787eaaSJeff Mahoney 		err = ret;
226676dda93cSYan, Zheng 	inode->i_flags |= S_DEAD;
2267c58aaad2SMiao Xie out_release:
2268c58aaad2SMiao Xie 	btrfs_subvolume_release_metadata(root, &block_rsv, qgroup_reserved);
226976dda93cSYan, Zheng out_up_write:
227076dda93cSYan, Zheng 	up_write(&root->fs_info->subvol_sem);
227176dda93cSYan, Zheng out_unlock:
227276dda93cSYan, Zheng 	mutex_unlock(&inode->i_mutex);
227376dda93cSYan, Zheng 	if (!err) {
2274efefb143SYan, Zheng 		shrink_dcache_sb(root->fs_info->sb);
227576dda93cSYan, Zheng 		btrfs_invalidate_inodes(dest);
227676dda93cSYan, Zheng 		d_delete(dentry);
2277fa6ac876SLiu Bo 
2278fa6ac876SLiu Bo 		/* the last ref */
2279fa6ac876SLiu Bo 		if (dest->cache_inode) {
2280fa6ac876SLiu Bo 			iput(dest->cache_inode);
2281fa6ac876SLiu Bo 			dest->cache_inode = NULL;
2282fa6ac876SLiu Bo 		}
228376dda93cSYan, Zheng 	}
228476dda93cSYan, Zheng out_dput:
228576dda93cSYan, Zheng 	dput(dentry);
228676dda93cSYan, Zheng out_unlock_dir:
228776dda93cSYan, Zheng 	mutex_unlock(&dir->i_mutex);
2288e43f998eSDavid Sterba out_drop_write:
22892a79f17eSAl Viro 	mnt_drop_write_file(file);
229076dda93cSYan, Zheng out:
229176dda93cSYan, Zheng 	kfree(vol_args);
229276dda93cSYan, Zheng 	return err;
229376dda93cSYan, Zheng }
229476dda93cSYan, Zheng 
22951e701a32SChris Mason static int btrfs_ioctl_defrag(struct file *file, void __user *argp)
2296f46b5a66SChristoph Hellwig {
2297496ad9aaSAl Viro 	struct inode *inode = file_inode(file);
2298f46b5a66SChristoph Hellwig 	struct btrfs_root *root = BTRFS_I(inode)->root;
22991e701a32SChris Mason 	struct btrfs_ioctl_defrag_range_args *range;
2300c146afadSYan Zheng 	int ret;
2301c146afadSYan Zheng 
230225122d15SIlya Dryomov 	ret = mnt_want_write_file(file);
230325122d15SIlya Dryomov 	if (ret)
230425122d15SIlya Dryomov 		return ret;
2305b83cc969SLi Zefan 
230625122d15SIlya Dryomov 	if (btrfs_root_readonly(root)) {
230725122d15SIlya Dryomov 		ret = -EROFS;
230825122d15SIlya Dryomov 		goto out;
23095ac00addSStefan Behrens 	}
2310f46b5a66SChristoph Hellwig 
2311f46b5a66SChristoph Hellwig 	switch (inode->i_mode & S_IFMT) {
2312f46b5a66SChristoph Hellwig 	case S_IFDIR:
2313e441d54dSChris Mason 		if (!capable(CAP_SYS_ADMIN)) {
2314e441d54dSChris Mason 			ret = -EPERM;
2315e441d54dSChris Mason 			goto out;
2316e441d54dSChris Mason 		}
2317de78b51aSEric Sandeen 		ret = btrfs_defrag_root(root);
23188929ecfaSYan, Zheng 		if (ret)
23198929ecfaSYan, Zheng 			goto out;
2320de78b51aSEric Sandeen 		ret = btrfs_defrag_root(root->fs_info->extent_root);
2321f46b5a66SChristoph Hellwig 		break;
2322f46b5a66SChristoph Hellwig 	case S_IFREG:
2323e441d54dSChris Mason 		if (!(file->f_mode & FMODE_WRITE)) {
2324e441d54dSChris Mason 			ret = -EINVAL;
2325e441d54dSChris Mason 			goto out;
2326e441d54dSChris Mason 		}
23271e701a32SChris Mason 
23281e701a32SChris Mason 		range = kzalloc(sizeof(*range), GFP_KERNEL);
23291e701a32SChris Mason 		if (!range) {
23301e701a32SChris Mason 			ret = -ENOMEM;
23311e701a32SChris Mason 			goto out;
23321e701a32SChris Mason 		}
23331e701a32SChris Mason 
23341e701a32SChris Mason 		if (argp) {
23351e701a32SChris Mason 			if (copy_from_user(range, argp,
23361e701a32SChris Mason 					   sizeof(*range))) {
23371e701a32SChris Mason 				ret = -EFAULT;
23381e701a32SChris Mason 				kfree(range);
2339683be16eSDan Carpenter 				goto out;
23401e701a32SChris Mason 			}
23411e701a32SChris Mason 			/* compression requires us to start the IO */
23421e701a32SChris Mason 			if ((range->flags & BTRFS_DEFRAG_RANGE_COMPRESS)) {
23431e701a32SChris Mason 				range->flags |= BTRFS_DEFRAG_RANGE_START_IO;
23441e701a32SChris Mason 				range->extent_thresh = (u32)-1;
23451e701a32SChris Mason 			}
23461e701a32SChris Mason 		} else {
23471e701a32SChris Mason 			/* the rest are all set to zero by kzalloc */
23481e701a32SChris Mason 			range->len = (u64)-1;
23491e701a32SChris Mason 		}
2350496ad9aaSAl Viro 		ret = btrfs_defrag_file(file_inode(file), file,
23514cb5300bSChris Mason 					range, 0, 0);
23524cb5300bSChris Mason 		if (ret > 0)
23534cb5300bSChris Mason 			ret = 0;
23541e701a32SChris Mason 		kfree(range);
2355f46b5a66SChristoph Hellwig 		break;
23568929ecfaSYan, Zheng 	default:
23578929ecfaSYan, Zheng 		ret = -EINVAL;
2358f46b5a66SChristoph Hellwig 	}
2359e441d54dSChris Mason out:
236025122d15SIlya Dryomov 	mnt_drop_write_file(file);
2361e441d54dSChris Mason 	return ret;
2362f46b5a66SChristoph Hellwig }
2363f46b5a66SChristoph Hellwig 
2364b2950863SChristoph Hellwig static long btrfs_ioctl_add_dev(struct btrfs_root *root, void __user *arg)
2365f46b5a66SChristoph Hellwig {
2366f46b5a66SChristoph Hellwig 	struct btrfs_ioctl_vol_args *vol_args;
2367f46b5a66SChristoph Hellwig 	int ret;
2368f46b5a66SChristoph Hellwig 
2369e441d54dSChris Mason 	if (!capable(CAP_SYS_ADMIN))
2370e441d54dSChris Mason 		return -EPERM;
2371e441d54dSChris Mason 
23725ac00addSStefan Behrens 	if (atomic_xchg(&root->fs_info->mutually_exclusive_operation_running,
23735ac00addSStefan Behrens 			1)) {
2374e57138b3SAnand Jain 		return BTRFS_ERROR_DEV_EXCL_RUN_IN_PROGRESS;
2375c9e9f97bSIlya Dryomov 	}
2376c9e9f97bSIlya Dryomov 
23775ac00addSStefan Behrens 	mutex_lock(&root->fs_info->volume_mutex);
2378dae7b665SLi Zefan 	vol_args = memdup_user(arg, sizeof(*vol_args));
2379c9e9f97bSIlya Dryomov 	if (IS_ERR(vol_args)) {
2380c9e9f97bSIlya Dryomov 		ret = PTR_ERR(vol_args);
2381c9e9f97bSIlya Dryomov 		goto out;
2382c9e9f97bSIlya Dryomov 	}
2383f46b5a66SChristoph Hellwig 
23845516e595SMark Fasheh 	vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
2385f46b5a66SChristoph Hellwig 	ret = btrfs_init_new_device(root, vol_args->name);
2386f46b5a66SChristoph Hellwig 
2387f46b5a66SChristoph Hellwig 	kfree(vol_args);
2388c9e9f97bSIlya Dryomov out:
2389c9e9f97bSIlya Dryomov 	mutex_unlock(&root->fs_info->volume_mutex);
23905ac00addSStefan Behrens 	atomic_set(&root->fs_info->mutually_exclusive_operation_running, 0);
2391f46b5a66SChristoph Hellwig 	return ret;
2392f46b5a66SChristoph Hellwig }
2393f46b5a66SChristoph Hellwig 
2394da24927bSMiao Xie static long btrfs_ioctl_rm_dev(struct file *file, void __user *arg)
2395f46b5a66SChristoph Hellwig {
2396496ad9aaSAl Viro 	struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
2397f46b5a66SChristoph Hellwig 	struct btrfs_ioctl_vol_args *vol_args;
2398f46b5a66SChristoph Hellwig 	int ret;
2399f46b5a66SChristoph Hellwig 
2400e441d54dSChris Mason 	if (!capable(CAP_SYS_ADMIN))
2401e441d54dSChris Mason 		return -EPERM;
2402e441d54dSChris Mason 
2403da24927bSMiao Xie 	ret = mnt_want_write_file(file);
2404da24927bSMiao Xie 	if (ret)
2405da24927bSMiao Xie 		return ret;
2406c146afadSYan Zheng 
2407dae7b665SLi Zefan 	vol_args = memdup_user(arg, sizeof(*vol_args));
2408c9e9f97bSIlya Dryomov 	if (IS_ERR(vol_args)) {
2409c9e9f97bSIlya Dryomov 		ret = PTR_ERR(vol_args);
2410c9e9f97bSIlya Dryomov 		goto out;
2411c9e9f97bSIlya Dryomov 	}
2412f46b5a66SChristoph Hellwig 
24135516e595SMark Fasheh 	vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
2414f46b5a66SChristoph Hellwig 
2415183860f6SAnand Jain 	if (atomic_xchg(&root->fs_info->mutually_exclusive_operation_running,
2416183860f6SAnand Jain 			1)) {
2417183860f6SAnand Jain 		ret = BTRFS_ERROR_DEV_EXCL_RUN_IN_PROGRESS;
2418183860f6SAnand Jain 		goto out;
2419183860f6SAnand Jain 	}
2420183860f6SAnand Jain 
2421183860f6SAnand Jain 	mutex_lock(&root->fs_info->volume_mutex);
2422183860f6SAnand Jain 	ret = btrfs_rm_device(root, vol_args->name);
2423c9e9f97bSIlya Dryomov 	mutex_unlock(&root->fs_info->volume_mutex);
24245ac00addSStefan Behrens 	atomic_set(&root->fs_info->mutually_exclusive_operation_running, 0);
2425183860f6SAnand Jain 
2426183860f6SAnand Jain out:
2427183860f6SAnand Jain 	kfree(vol_args);
24284ac20c70SIlya Dryomov 	mnt_drop_write_file(file);
2429f46b5a66SChristoph Hellwig 	return ret;
2430f46b5a66SChristoph Hellwig }
2431f46b5a66SChristoph Hellwig 
2432475f6387SJan Schmidt static long btrfs_ioctl_fs_info(struct btrfs_root *root, void __user *arg)
2433475f6387SJan Schmidt {
2434027ed2f0SLi Zefan 	struct btrfs_ioctl_fs_info_args *fi_args;
2435475f6387SJan Schmidt 	struct btrfs_device *device;
2436475f6387SJan Schmidt 	struct btrfs_device *next;
2437475f6387SJan Schmidt 	struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices;
2438027ed2f0SLi Zefan 	int ret = 0;
2439475f6387SJan Schmidt 
2440475f6387SJan Schmidt 	if (!capable(CAP_SYS_ADMIN))
2441475f6387SJan Schmidt 		return -EPERM;
2442475f6387SJan Schmidt 
2443027ed2f0SLi Zefan 	fi_args = kzalloc(sizeof(*fi_args), GFP_KERNEL);
2444027ed2f0SLi Zefan 	if (!fi_args)
2445027ed2f0SLi Zefan 		return -ENOMEM;
2446027ed2f0SLi Zefan 
2447f7171750SFilipe David Borba Manana 	mutex_lock(&fs_devices->device_list_mutex);
2448027ed2f0SLi Zefan 	fi_args->num_devices = fs_devices->num_devices;
2449027ed2f0SLi Zefan 	memcpy(&fi_args->fsid, root->fs_info->fsid, sizeof(fi_args->fsid));
2450475f6387SJan Schmidt 
2451475f6387SJan Schmidt 	list_for_each_entry_safe(device, next, &fs_devices->devices, dev_list) {
2452027ed2f0SLi Zefan 		if (device->devid > fi_args->max_id)
2453027ed2f0SLi Zefan 			fi_args->max_id = device->devid;
2454475f6387SJan Schmidt 	}
2455475f6387SJan Schmidt 	mutex_unlock(&fs_devices->device_list_mutex);
2456475f6387SJan Schmidt 
2457027ed2f0SLi Zefan 	if (copy_to_user(arg, fi_args, sizeof(*fi_args)))
2458027ed2f0SLi Zefan 		ret = -EFAULT;
2459475f6387SJan Schmidt 
2460027ed2f0SLi Zefan 	kfree(fi_args);
2461027ed2f0SLi Zefan 	return ret;
2462475f6387SJan Schmidt }
2463475f6387SJan Schmidt 
2464475f6387SJan Schmidt static long btrfs_ioctl_dev_info(struct btrfs_root *root, void __user *arg)
2465475f6387SJan Schmidt {
2466475f6387SJan Schmidt 	struct btrfs_ioctl_dev_info_args *di_args;
2467475f6387SJan Schmidt 	struct btrfs_device *dev;
2468475f6387SJan Schmidt 	struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices;
2469475f6387SJan Schmidt 	int ret = 0;
2470475f6387SJan Schmidt 	char *s_uuid = NULL;
2471475f6387SJan Schmidt 
2472475f6387SJan Schmidt 	if (!capable(CAP_SYS_ADMIN))
2473475f6387SJan Schmidt 		return -EPERM;
2474475f6387SJan Schmidt 
2475475f6387SJan Schmidt 	di_args = memdup_user(arg, sizeof(*di_args));
2476475f6387SJan Schmidt 	if (IS_ERR(di_args))
2477475f6387SJan Schmidt 		return PTR_ERR(di_args);
2478475f6387SJan Schmidt 
2479dd5f9615SStefan Behrens 	if (!btrfs_is_empty_uuid(di_args->uuid))
2480475f6387SJan Schmidt 		s_uuid = di_args->uuid;
2481475f6387SJan Schmidt 
2482475f6387SJan Schmidt 	mutex_lock(&fs_devices->device_list_mutex);
2483aa1b8cd4SStefan Behrens 	dev = btrfs_find_device(root->fs_info, di_args->devid, s_uuid, NULL);
2484475f6387SJan Schmidt 
2485475f6387SJan Schmidt 	if (!dev) {
2486475f6387SJan Schmidt 		ret = -ENODEV;
2487475f6387SJan Schmidt 		goto out;
2488475f6387SJan Schmidt 	}
2489475f6387SJan Schmidt 
2490475f6387SJan Schmidt 	di_args->devid = dev->devid;
2491475f6387SJan Schmidt 	di_args->bytes_used = dev->bytes_used;
2492475f6387SJan Schmidt 	di_args->total_bytes = dev->total_bytes;
2493475f6387SJan Schmidt 	memcpy(di_args->uuid, dev->uuid, sizeof(di_args->uuid));
2494a27202fbSJim Meyering 	if (dev->name) {
2495606686eeSJosef Bacik 		struct rcu_string *name;
2496606686eeSJosef Bacik 
2497606686eeSJosef Bacik 		rcu_read_lock();
2498606686eeSJosef Bacik 		name = rcu_dereference(dev->name);
2499606686eeSJosef Bacik 		strncpy(di_args->path, name->str, sizeof(di_args->path));
2500606686eeSJosef Bacik 		rcu_read_unlock();
2501a27202fbSJim Meyering 		di_args->path[sizeof(di_args->path) - 1] = 0;
2502a27202fbSJim Meyering 	} else {
250399ba55adSStefan Behrens 		di_args->path[0] = '\0';
2504a27202fbSJim Meyering 	}
2505475f6387SJan Schmidt 
2506475f6387SJan Schmidt out:
250755793c0dSDavid Sterba 	mutex_unlock(&fs_devices->device_list_mutex);
2508475f6387SJan Schmidt 	if (ret == 0 && copy_to_user(arg, di_args, sizeof(*di_args)))
2509475f6387SJan Schmidt 		ret = -EFAULT;
2510475f6387SJan Schmidt 
2511475f6387SJan Schmidt 	kfree(di_args);
2512475f6387SJan Schmidt 	return ret;
2513475f6387SJan Schmidt }
2514475f6387SJan Schmidt 
2515416161dbSMark Fasheh static struct page *extent_same_get_page(struct inode *inode, u64 off)
2516416161dbSMark Fasheh {
2517416161dbSMark Fasheh 	struct page *page;
2518416161dbSMark Fasheh 	pgoff_t index;
2519416161dbSMark Fasheh 	struct extent_io_tree *tree = &BTRFS_I(inode)->io_tree;
2520416161dbSMark Fasheh 
2521416161dbSMark Fasheh 	index = off >> PAGE_CACHE_SHIFT;
2522416161dbSMark Fasheh 
2523416161dbSMark Fasheh 	page = grab_cache_page(inode->i_mapping, index);
2524416161dbSMark Fasheh 	if (!page)
2525416161dbSMark Fasheh 		return NULL;
2526416161dbSMark Fasheh 
2527416161dbSMark Fasheh 	if (!PageUptodate(page)) {
2528416161dbSMark Fasheh 		if (extent_read_full_page_nolock(tree, page, btrfs_get_extent,
2529416161dbSMark Fasheh 						 0))
2530416161dbSMark Fasheh 			return NULL;
2531416161dbSMark Fasheh 		lock_page(page);
2532416161dbSMark Fasheh 		if (!PageUptodate(page)) {
2533416161dbSMark Fasheh 			unlock_page(page);
2534416161dbSMark Fasheh 			page_cache_release(page);
2535416161dbSMark Fasheh 			return NULL;
2536416161dbSMark Fasheh 		}
2537416161dbSMark Fasheh 	}
2538416161dbSMark Fasheh 	unlock_page(page);
2539416161dbSMark Fasheh 
2540416161dbSMark Fasheh 	return page;
2541416161dbSMark Fasheh }
2542416161dbSMark Fasheh 
254377fe20dcSMark Fasheh static inline void lock_extent_range(struct inode *inode, u64 off, u64 len)
254477fe20dcSMark Fasheh {
254577fe20dcSMark Fasheh 	/* do any pending delalloc/csum calc on src, one way or
254677fe20dcSMark Fasheh 	   another, and lock file content */
254777fe20dcSMark Fasheh 	while (1) {
254877fe20dcSMark Fasheh 		struct btrfs_ordered_extent *ordered;
254977fe20dcSMark Fasheh 		lock_extent(&BTRFS_I(inode)->io_tree, off, off + len - 1);
255077fe20dcSMark Fasheh 		ordered = btrfs_lookup_first_ordered_extent(inode,
255177fe20dcSMark Fasheh 							    off + len - 1);
255277fe20dcSMark Fasheh 		if (!ordered &&
255377fe20dcSMark Fasheh 		    !test_range_bit(&BTRFS_I(inode)->io_tree, off,
255477fe20dcSMark Fasheh 				    off + len - 1, EXTENT_DELALLOC, 0, NULL))
255577fe20dcSMark Fasheh 			break;
255677fe20dcSMark Fasheh 		unlock_extent(&BTRFS_I(inode)->io_tree, off, off + len - 1);
255777fe20dcSMark Fasheh 		if (ordered)
255877fe20dcSMark Fasheh 			btrfs_put_ordered_extent(ordered);
255977fe20dcSMark Fasheh 		btrfs_wait_ordered_range(inode, off, len);
256077fe20dcSMark Fasheh 	}
256177fe20dcSMark Fasheh }
256277fe20dcSMark Fasheh 
2563416161dbSMark Fasheh static void btrfs_double_unlock(struct inode *inode1, u64 loff1,
2564416161dbSMark Fasheh 				struct inode *inode2, u64 loff2, u64 len)
2565416161dbSMark Fasheh {
2566416161dbSMark Fasheh 	unlock_extent(&BTRFS_I(inode1)->io_tree, loff1, loff1 + len - 1);
2567416161dbSMark Fasheh 	unlock_extent(&BTRFS_I(inode2)->io_tree, loff2, loff2 + len - 1);
2568416161dbSMark Fasheh 
2569416161dbSMark Fasheh 	mutex_unlock(&inode1->i_mutex);
2570416161dbSMark Fasheh 	mutex_unlock(&inode2->i_mutex);
2571416161dbSMark Fasheh }
2572416161dbSMark Fasheh 
2573416161dbSMark Fasheh static void btrfs_double_lock(struct inode *inode1, u64 loff1,
2574416161dbSMark Fasheh 			      struct inode *inode2, u64 loff2, u64 len)
2575416161dbSMark Fasheh {
2576416161dbSMark Fasheh 	if (inode1 < inode2) {
2577416161dbSMark Fasheh 		swap(inode1, inode2);
2578416161dbSMark Fasheh 		swap(loff1, loff2);
2579416161dbSMark Fasheh 	}
2580416161dbSMark Fasheh 
2581416161dbSMark Fasheh 	mutex_lock_nested(&inode1->i_mutex, I_MUTEX_PARENT);
2582416161dbSMark Fasheh 	lock_extent_range(inode1, loff1, len);
2583416161dbSMark Fasheh 	if (inode1 != inode2) {
2584416161dbSMark Fasheh 		mutex_lock_nested(&inode2->i_mutex, I_MUTEX_CHILD);
2585416161dbSMark Fasheh 		lock_extent_range(inode2, loff2, len);
2586416161dbSMark Fasheh 	}
2587416161dbSMark Fasheh }
2588416161dbSMark Fasheh 
2589416161dbSMark Fasheh static int btrfs_cmp_data(struct inode *src, u64 loff, struct inode *dst,
2590416161dbSMark Fasheh 			  u64 dst_loff, u64 len)
2591416161dbSMark Fasheh {
2592416161dbSMark Fasheh 	int ret = 0;
2593416161dbSMark Fasheh 	struct page *src_page, *dst_page;
2594416161dbSMark Fasheh 	unsigned int cmp_len = PAGE_CACHE_SIZE;
2595416161dbSMark Fasheh 	void *addr, *dst_addr;
2596416161dbSMark Fasheh 
2597416161dbSMark Fasheh 	while (len) {
2598416161dbSMark Fasheh 		if (len < PAGE_CACHE_SIZE)
2599416161dbSMark Fasheh 			cmp_len = len;
2600416161dbSMark Fasheh 
2601416161dbSMark Fasheh 		src_page = extent_same_get_page(src, loff);
2602416161dbSMark Fasheh 		if (!src_page)
2603416161dbSMark Fasheh 			return -EINVAL;
2604416161dbSMark Fasheh 		dst_page = extent_same_get_page(dst, dst_loff);
2605416161dbSMark Fasheh 		if (!dst_page) {
2606416161dbSMark Fasheh 			page_cache_release(src_page);
2607416161dbSMark Fasheh 			return -EINVAL;
2608416161dbSMark Fasheh 		}
2609416161dbSMark Fasheh 		addr = kmap_atomic(src_page);
2610416161dbSMark Fasheh 		dst_addr = kmap_atomic(dst_page);
2611416161dbSMark Fasheh 
2612416161dbSMark Fasheh 		flush_dcache_page(src_page);
2613416161dbSMark Fasheh 		flush_dcache_page(dst_page);
2614416161dbSMark Fasheh 
2615416161dbSMark Fasheh 		if (memcmp(addr, dst_addr, cmp_len))
2616416161dbSMark Fasheh 			ret = BTRFS_SAME_DATA_DIFFERS;
2617416161dbSMark Fasheh 
2618416161dbSMark Fasheh 		kunmap_atomic(addr);
2619416161dbSMark Fasheh 		kunmap_atomic(dst_addr);
2620416161dbSMark Fasheh 		page_cache_release(src_page);
2621416161dbSMark Fasheh 		page_cache_release(dst_page);
2622416161dbSMark Fasheh 
2623416161dbSMark Fasheh 		if (ret)
2624416161dbSMark Fasheh 			break;
2625416161dbSMark Fasheh 
2626416161dbSMark Fasheh 		loff += cmp_len;
2627416161dbSMark Fasheh 		dst_loff += cmp_len;
2628416161dbSMark Fasheh 		len -= cmp_len;
2629416161dbSMark Fasheh 	}
2630416161dbSMark Fasheh 
2631416161dbSMark Fasheh 	return ret;
2632416161dbSMark Fasheh }
2633416161dbSMark Fasheh 
2634416161dbSMark Fasheh static int extent_same_check_offsets(struct inode *inode, u64 off, u64 len)
2635416161dbSMark Fasheh {
2636416161dbSMark Fasheh 	u64 bs = BTRFS_I(inode)->root->fs_info->sb->s_blocksize;
2637416161dbSMark Fasheh 
2638416161dbSMark Fasheh 	if (off + len > inode->i_size || off + len < off)
2639416161dbSMark Fasheh 		return -EINVAL;
2640416161dbSMark Fasheh 	/* Check that we are block aligned - btrfs_clone() requires this */
2641416161dbSMark Fasheh 	if (!IS_ALIGNED(off, bs) || !IS_ALIGNED(off + len, bs))
2642416161dbSMark Fasheh 		return -EINVAL;
2643416161dbSMark Fasheh 
2644416161dbSMark Fasheh 	return 0;
2645416161dbSMark Fasheh }
2646416161dbSMark Fasheh 
2647416161dbSMark Fasheh static int btrfs_extent_same(struct inode *src, u64 loff, u64 len,
2648416161dbSMark Fasheh 			     struct inode *dst, u64 dst_loff)
2649416161dbSMark Fasheh {
2650416161dbSMark Fasheh 	int ret;
2651416161dbSMark Fasheh 
2652416161dbSMark Fasheh 	/*
2653416161dbSMark Fasheh 	 * btrfs_clone() can't handle extents in the same file
2654416161dbSMark Fasheh 	 * yet. Once that works, we can drop this check and replace it
2655416161dbSMark Fasheh 	 * with a check for the same inode, but overlapping extents.
2656416161dbSMark Fasheh 	 */
2657416161dbSMark Fasheh 	if (src == dst)
2658416161dbSMark Fasheh 		return -EINVAL;
2659416161dbSMark Fasheh 
2660416161dbSMark Fasheh 	btrfs_double_lock(src, loff, dst, dst_loff, len);
2661416161dbSMark Fasheh 
2662416161dbSMark Fasheh 	ret = extent_same_check_offsets(src, loff, len);
2663416161dbSMark Fasheh 	if (ret)
2664416161dbSMark Fasheh 		goto out_unlock;
2665416161dbSMark Fasheh 
2666416161dbSMark Fasheh 	ret = extent_same_check_offsets(dst, dst_loff, len);
2667416161dbSMark Fasheh 	if (ret)
2668416161dbSMark Fasheh 		goto out_unlock;
2669416161dbSMark Fasheh 
2670416161dbSMark Fasheh 	/* don't make the dst file partly checksummed */
2671416161dbSMark Fasheh 	if ((BTRFS_I(src)->flags & BTRFS_INODE_NODATASUM) !=
2672416161dbSMark Fasheh 	    (BTRFS_I(dst)->flags & BTRFS_INODE_NODATASUM)) {
2673416161dbSMark Fasheh 		ret = -EINVAL;
2674416161dbSMark Fasheh 		goto out_unlock;
2675416161dbSMark Fasheh 	}
2676416161dbSMark Fasheh 
2677416161dbSMark Fasheh 	ret = btrfs_cmp_data(src, loff, dst, dst_loff, len);
2678416161dbSMark Fasheh 	if (ret == 0)
2679416161dbSMark Fasheh 		ret = btrfs_clone(src, dst, loff, len, len, dst_loff);
2680416161dbSMark Fasheh 
2681416161dbSMark Fasheh out_unlock:
2682416161dbSMark Fasheh 	btrfs_double_unlock(src, loff, dst, dst_loff, len);
2683416161dbSMark Fasheh 
2684416161dbSMark Fasheh 	return ret;
2685416161dbSMark Fasheh }
2686416161dbSMark Fasheh 
2687416161dbSMark Fasheh #define BTRFS_MAX_DEDUPE_LEN	(16 * 1024 * 1024)
2688416161dbSMark Fasheh 
2689416161dbSMark Fasheh static long btrfs_ioctl_file_extent_same(struct file *file,
2690416161dbSMark Fasheh 					 void __user *argp)
2691416161dbSMark Fasheh {
2692cbf8b8caSMark Fasheh 	struct btrfs_ioctl_same_args tmp;
2693cbf8b8caSMark Fasheh 	struct btrfs_ioctl_same_args *same;
2694cbf8b8caSMark Fasheh 	struct btrfs_ioctl_same_extent_info *info;
2695416161dbSMark Fasheh 	struct inode *src = file->f_dentry->d_inode;
2696416161dbSMark Fasheh 	struct file *dst_file = NULL;
2697416161dbSMark Fasheh 	struct inode *dst;
2698416161dbSMark Fasheh 	u64 off;
2699416161dbSMark Fasheh 	u64 len;
2700416161dbSMark Fasheh 	int i;
2701416161dbSMark Fasheh 	int ret;
2702cbf8b8caSMark Fasheh 	unsigned long size;
2703416161dbSMark Fasheh 	u64 bs = BTRFS_I(src)->root->fs_info->sb->s_blocksize;
2704416161dbSMark Fasheh 	bool is_admin = capable(CAP_SYS_ADMIN);
2705416161dbSMark Fasheh 
2706416161dbSMark Fasheh 	if (!(file->f_mode & FMODE_READ))
2707416161dbSMark Fasheh 		return -EINVAL;
2708416161dbSMark Fasheh 
2709416161dbSMark Fasheh 	ret = mnt_want_write_file(file);
2710416161dbSMark Fasheh 	if (ret)
2711416161dbSMark Fasheh 		return ret;
2712416161dbSMark Fasheh 
2713cbf8b8caSMark Fasheh 	if (copy_from_user(&tmp,
2714416161dbSMark Fasheh 			   (struct btrfs_ioctl_same_args __user *)argp,
2715cbf8b8caSMark Fasheh 			   sizeof(tmp))) {
2716416161dbSMark Fasheh 		ret = -EFAULT;
2717416161dbSMark Fasheh 		goto out;
2718416161dbSMark Fasheh 	}
2719416161dbSMark Fasheh 
2720cbf8b8caSMark Fasheh 	size = sizeof(tmp) +
2721cbf8b8caSMark Fasheh 		tmp.dest_count * sizeof(struct btrfs_ioctl_same_extent_info);
2722cbf8b8caSMark Fasheh 
2723229eed43SGeyslan G. Bem 	same = memdup_user((struct btrfs_ioctl_same_args __user *)argp, size);
2724cbf8b8caSMark Fasheh 
2725229eed43SGeyslan G. Bem 	if (IS_ERR(same)) {
2726229eed43SGeyslan G. Bem 		ret = PTR_ERR(same);
2727cbf8b8caSMark Fasheh 		goto out;
2728cbf8b8caSMark Fasheh 	}
2729cbf8b8caSMark Fasheh 
2730cbf8b8caSMark Fasheh 	off = same->logical_offset;
2731cbf8b8caSMark Fasheh 	len = same->length;
2732416161dbSMark Fasheh 
2733416161dbSMark Fasheh 	/*
2734416161dbSMark Fasheh 	 * Limit the total length we will dedupe for each operation.
2735416161dbSMark Fasheh 	 * This is intended to bound the total time spent in this
2736416161dbSMark Fasheh 	 * ioctl to something sane.
2737416161dbSMark Fasheh 	 */
2738416161dbSMark Fasheh 	if (len > BTRFS_MAX_DEDUPE_LEN)
2739416161dbSMark Fasheh 		len = BTRFS_MAX_DEDUPE_LEN;
2740416161dbSMark Fasheh 
2741416161dbSMark Fasheh 	if (WARN_ON_ONCE(bs < PAGE_CACHE_SIZE)) {
2742416161dbSMark Fasheh 		/*
2743416161dbSMark Fasheh 		 * Btrfs does not support blocksize < page_size. As a
2744416161dbSMark Fasheh 		 * result, btrfs_cmp_data() won't correctly handle
2745416161dbSMark Fasheh 		 * this situation without an update.
2746416161dbSMark Fasheh 		 */
2747416161dbSMark Fasheh 		ret = -EINVAL;
2748416161dbSMark Fasheh 		goto out;
2749416161dbSMark Fasheh 	}
2750416161dbSMark Fasheh 
2751416161dbSMark Fasheh 	ret = -EISDIR;
2752416161dbSMark Fasheh 	if (S_ISDIR(src->i_mode))
2753416161dbSMark Fasheh 		goto out;
2754416161dbSMark Fasheh 
2755416161dbSMark Fasheh 	ret = -EACCES;
2756416161dbSMark Fasheh 	if (!S_ISREG(src->i_mode))
2757416161dbSMark Fasheh 		goto out;
2758416161dbSMark Fasheh 
2759cbf8b8caSMark Fasheh 	/* pre-format output fields to sane values */
2760cbf8b8caSMark Fasheh 	for (i = 0; i < same->dest_count; i++) {
2761cbf8b8caSMark Fasheh 		same->info[i].bytes_deduped = 0ULL;
2762cbf8b8caSMark Fasheh 		same->info[i].status = 0;
2763416161dbSMark Fasheh 	}
2764416161dbSMark Fasheh 
2765cbf8b8caSMark Fasheh 	ret = 0;
2766cbf8b8caSMark Fasheh 	for (i = 0; i < same->dest_count; i++) {
2767cbf8b8caSMark Fasheh 		info = &same->info[i];
2768416161dbSMark Fasheh 
2769cbf8b8caSMark Fasheh 		dst_file = fget(info->fd);
2770416161dbSMark Fasheh 		if (!dst_file) {
2771cbf8b8caSMark Fasheh 			info->status = -EBADF;
2772416161dbSMark Fasheh 			goto next;
2773416161dbSMark Fasheh 		}
2774416161dbSMark Fasheh 
2775416161dbSMark Fasheh 		if (!(is_admin || (dst_file->f_mode & FMODE_WRITE))) {
2776cbf8b8caSMark Fasheh 			info->status = -EINVAL;
2777416161dbSMark Fasheh 			goto next;
2778416161dbSMark Fasheh 		}
2779416161dbSMark Fasheh 
2780cbf8b8caSMark Fasheh 		info->status = -EXDEV;
2781416161dbSMark Fasheh 		if (file->f_path.mnt != dst_file->f_path.mnt)
2782416161dbSMark Fasheh 			goto next;
2783416161dbSMark Fasheh 
2784416161dbSMark Fasheh 		dst = dst_file->f_dentry->d_inode;
2785416161dbSMark Fasheh 		if (src->i_sb != dst->i_sb)
2786416161dbSMark Fasheh 			goto next;
2787416161dbSMark Fasheh 
2788416161dbSMark Fasheh 		if (S_ISDIR(dst->i_mode)) {
2789cbf8b8caSMark Fasheh 			info->status = -EISDIR;
2790416161dbSMark Fasheh 			goto next;
2791416161dbSMark Fasheh 		}
2792416161dbSMark Fasheh 
2793416161dbSMark Fasheh 		if (!S_ISREG(dst->i_mode)) {
2794cbf8b8caSMark Fasheh 			info->status = -EACCES;
2795416161dbSMark Fasheh 			goto next;
2796416161dbSMark Fasheh 		}
2797416161dbSMark Fasheh 
2798cbf8b8caSMark Fasheh 		info->status = btrfs_extent_same(src, off, len, dst,
2799cbf8b8caSMark Fasheh 						info->logical_offset);
2800cbf8b8caSMark Fasheh 		if (info->status == 0)
2801cbf8b8caSMark Fasheh 			info->bytes_deduped += len;
2802416161dbSMark Fasheh 
2803416161dbSMark Fasheh next:
2804416161dbSMark Fasheh 		if (dst_file)
2805416161dbSMark Fasheh 			fput(dst_file);
2806cbf8b8caSMark Fasheh 	}
2807416161dbSMark Fasheh 
2808cbf8b8caSMark Fasheh 	ret = copy_to_user(argp, same, size);
2809cbf8b8caSMark Fasheh 	if (ret)
2810416161dbSMark Fasheh 		ret = -EFAULT;
2811416161dbSMark Fasheh 
2812416161dbSMark Fasheh out:
2813416161dbSMark Fasheh 	mnt_drop_write_file(file);
2814416161dbSMark Fasheh 	return ret;
2815416161dbSMark Fasheh }
2816416161dbSMark Fasheh 
281732b7c687SMark Fasheh /**
281832b7c687SMark Fasheh  * btrfs_clone() - clone a range from inode file to another
281932b7c687SMark Fasheh  *
282032b7c687SMark Fasheh  * @src: Inode to clone from
282132b7c687SMark Fasheh  * @inode: Inode to clone to
282232b7c687SMark Fasheh  * @off: Offset within source to start clone from
282332b7c687SMark Fasheh  * @olen: Original length, passed by user, of range to clone
282432b7c687SMark Fasheh  * @olen_aligned: Block-aligned value of olen, extent_same uses
282532b7c687SMark Fasheh  *               identical values here
282632b7c687SMark Fasheh  * @destoff: Offset within @inode to start clone
282732b7c687SMark Fasheh  */
282832b7c687SMark Fasheh static int btrfs_clone(struct inode *src, struct inode *inode,
282932b7c687SMark Fasheh 		       u64 off, u64 olen, u64 olen_aligned, u64 destoff)
2830f46b5a66SChristoph Hellwig {
2831f46b5a66SChristoph Hellwig 	struct btrfs_root *root = BTRFS_I(inode)->root;
283232b7c687SMark Fasheh 	struct btrfs_path *path = NULL;
2833f46b5a66SChristoph Hellwig 	struct extent_buffer *leaf;
283432b7c687SMark Fasheh 	struct btrfs_trans_handle *trans;
283532b7c687SMark Fasheh 	char *buf = NULL;
2836ae01a0abSYan Zheng 	struct btrfs_key key;
2837f46b5a66SChristoph Hellwig 	u32 nritems;
2838f46b5a66SChristoph Hellwig 	int slot;
2839ae01a0abSYan Zheng 	int ret;
284032b7c687SMark Fasheh 	u64 len = olen_aligned;
2841ae01a0abSYan Zheng 
2842ae01a0abSYan Zheng 	ret = -ENOMEM;
2843ae01a0abSYan Zheng 	buf = vmalloc(btrfs_level_size(root, 0));
2844ae01a0abSYan Zheng 	if (!buf)
284532b7c687SMark Fasheh 		return ret;
2846ae01a0abSYan Zheng 
2847ae01a0abSYan Zheng 	path = btrfs_alloc_path();
2848ae01a0abSYan Zheng 	if (!path) {
2849ae01a0abSYan Zheng 		vfree(buf);
285032b7c687SMark Fasheh 		return ret;
2851ae01a0abSYan Zheng 	}
285232b7c687SMark Fasheh 
2853ae01a0abSYan Zheng 	path->reada = 2;
2854c5c9cd4dSSage Weil 	/* clone data */
285533345d01SLi Zefan 	key.objectid = btrfs_ino(src);
2856ae01a0abSYan Zheng 	key.type = BTRFS_EXTENT_DATA_KEY;
2857ae01a0abSYan Zheng 	key.offset = 0;
2858f46b5a66SChristoph Hellwig 
2859f46b5a66SChristoph Hellwig 	while (1) {
2860f46b5a66SChristoph Hellwig 		/*
2861f46b5a66SChristoph Hellwig 		 * note the key will change type as we walk through the
2862f46b5a66SChristoph Hellwig 		 * tree.
2863f46b5a66SChristoph Hellwig 		 */
2864362a20c5SDavid Sterba 		ret = btrfs_search_slot(NULL, BTRFS_I(src)->root, &key, path,
2865362a20c5SDavid Sterba 				0, 0);
2866f46b5a66SChristoph Hellwig 		if (ret < 0)
2867f46b5a66SChristoph Hellwig 			goto out;
2868f46b5a66SChristoph Hellwig 
2869ae01a0abSYan Zheng 		nritems = btrfs_header_nritems(path->nodes[0]);
2870ae01a0abSYan Zheng 		if (path->slots[0] >= nritems) {
2871362a20c5SDavid Sterba 			ret = btrfs_next_leaf(BTRFS_I(src)->root, path);
2872f46b5a66SChristoph Hellwig 			if (ret < 0)
2873f46b5a66SChristoph Hellwig 				goto out;
2874f46b5a66SChristoph Hellwig 			if (ret > 0)
2875f46b5a66SChristoph Hellwig 				break;
2876ae01a0abSYan Zheng 			nritems = btrfs_header_nritems(path->nodes[0]);
2877f46b5a66SChristoph Hellwig 		}
2878f46b5a66SChristoph Hellwig 		leaf = path->nodes[0];
2879f46b5a66SChristoph Hellwig 		slot = path->slots[0];
2880f46b5a66SChristoph Hellwig 
2881ae01a0abSYan Zheng 		btrfs_item_key_to_cpu(leaf, &key, slot);
2882d20f7043SChris Mason 		if (btrfs_key_type(&key) > BTRFS_EXTENT_DATA_KEY ||
288333345d01SLi Zefan 		    key.objectid != btrfs_ino(src))
2884f46b5a66SChristoph Hellwig 			break;
2885f46b5a66SChristoph Hellwig 
2886c5c9cd4dSSage Weil 		if (btrfs_key_type(&key) == BTRFS_EXTENT_DATA_KEY) {
2887c5c9cd4dSSage Weil 			struct btrfs_file_extent_item *extent;
2888c5c9cd4dSSage Weil 			int type;
288931840ae1SZheng Yan 			u32 size;
289031840ae1SZheng Yan 			struct btrfs_key new_key;
2891c5c9cd4dSSage Weil 			u64 disko = 0, diskl = 0;
2892c5c9cd4dSSage Weil 			u64 datao = 0, datal = 0;
2893c5c9cd4dSSage Weil 			u8 comp;
2894b5384d48SSage Weil 			u64 endoff;
289531840ae1SZheng Yan 
289631840ae1SZheng Yan 			size = btrfs_item_size_nr(leaf, slot);
289731840ae1SZheng Yan 			read_extent_buffer(leaf, buf,
289831840ae1SZheng Yan 					   btrfs_item_ptr_offset(leaf, slot),
289931840ae1SZheng Yan 					   size);
2900c5c9cd4dSSage Weil 
2901c5c9cd4dSSage Weil 			extent = btrfs_item_ptr(leaf, slot,
2902c5c9cd4dSSage Weil 						struct btrfs_file_extent_item);
2903c5c9cd4dSSage Weil 			comp = btrfs_file_extent_compression(leaf, extent);
2904c5c9cd4dSSage Weil 			type = btrfs_file_extent_type(leaf, extent);
2905c8a894d7SChris Mason 			if (type == BTRFS_FILE_EXTENT_REG ||
2906c8a894d7SChris Mason 			    type == BTRFS_FILE_EXTENT_PREALLOC) {
2907d397712bSChris Mason 				disko = btrfs_file_extent_disk_bytenr(leaf,
2908d397712bSChris Mason 								      extent);
2909d397712bSChris Mason 				diskl = btrfs_file_extent_disk_num_bytes(leaf,
2910d397712bSChris Mason 								 extent);
2911c5c9cd4dSSage Weil 				datao = btrfs_file_extent_offset(leaf, extent);
2912d397712bSChris Mason 				datal = btrfs_file_extent_num_bytes(leaf,
2913d397712bSChris Mason 								    extent);
2914c5c9cd4dSSage Weil 			} else if (type == BTRFS_FILE_EXTENT_INLINE) {
2915c5c9cd4dSSage Weil 				/* take upper bound, may be compressed */
2916c5c9cd4dSSage Weil 				datal = btrfs_file_extent_ram_bytes(leaf,
2917c5c9cd4dSSage Weil 								    extent);
2918c5c9cd4dSSage Weil 			}
2919b3b4aa74SDavid Sterba 			btrfs_release_path(path);
292031840ae1SZheng Yan 
2921050006a7SSage Weil 			if (key.offset + datal <= off ||
2922aa42ffd9SLiu Bo 			    key.offset >= off + len - 1)
2923c5c9cd4dSSage Weil 				goto next;
2924c5c9cd4dSSage Weil 
292531840ae1SZheng Yan 			memcpy(&new_key, &key, sizeof(new_key));
292633345d01SLi Zefan 			new_key.objectid = btrfs_ino(inode);
29274d728ec7SLi Zefan 			if (off <= key.offset)
2928c5c9cd4dSSage Weil 				new_key.offset = key.offset + destoff - off;
29294d728ec7SLi Zefan 			else
29304d728ec7SLi Zefan 				new_key.offset = destoff;
2931c5c9cd4dSSage Weil 
2932b6f3409bSSage Weil 			/*
2933b6f3409bSSage Weil 			 * 1 - adjusting old extent (we may have to split it)
2934b6f3409bSSage Weil 			 * 1 - add new extent
2935b6f3409bSSage Weil 			 * 1 - inode update
2936b6f3409bSSage Weil 			 */
2937b6f3409bSSage Weil 			trans = btrfs_start_transaction(root, 3);
2938a22285a6SYan, Zheng 			if (IS_ERR(trans)) {
2939a22285a6SYan, Zheng 				ret = PTR_ERR(trans);
2940a22285a6SYan, Zheng 				goto out;
2941a22285a6SYan, Zheng 			}
2942a22285a6SYan, Zheng 
2943c8a894d7SChris Mason 			if (type == BTRFS_FILE_EXTENT_REG ||
2944c8a894d7SChris Mason 			    type == BTRFS_FILE_EXTENT_PREALLOC) {
2945d72c0842SLi Zefan 				/*
2946d72c0842SLi Zefan 				 *    a  | --- range to clone ---|  b
2947d72c0842SLi Zefan 				 * | ------------- extent ------------- |
2948d72c0842SLi Zefan 				 */
2949d72c0842SLi Zefan 
2950d72c0842SLi Zefan 				/* substract range b */
2951d72c0842SLi Zefan 				if (key.offset + datal > off + len)
2952d72c0842SLi Zefan 					datal = off + len - key.offset;
2953d72c0842SLi Zefan 
2954d72c0842SLi Zefan 				/* substract range a */
2955a22285a6SYan, Zheng 				if (off > key.offset) {
2956a22285a6SYan, Zheng 					datao += off - key.offset;
2957a22285a6SYan, Zheng 					datal -= off - key.offset;
2958a22285a6SYan, Zheng 				}
2959a22285a6SYan, Zheng 
29605dc562c5SJosef Bacik 				ret = btrfs_drop_extents(trans, root, inode,
2961a22285a6SYan, Zheng 							 new_key.offset,
2962a22285a6SYan, Zheng 							 new_key.offset + datal,
29632671485dSJosef Bacik 							 1);
296479787eaaSJeff Mahoney 				if (ret) {
296579787eaaSJeff Mahoney 					btrfs_abort_transaction(trans, root,
296679787eaaSJeff Mahoney 								ret);
296779787eaaSJeff Mahoney 					btrfs_end_transaction(trans, root);
296879787eaaSJeff Mahoney 					goto out;
296979787eaaSJeff Mahoney 				}
2970a22285a6SYan, Zheng 
297131840ae1SZheng Yan 				ret = btrfs_insert_empty_item(trans, root, path,
297231840ae1SZheng Yan 							      &new_key, size);
297379787eaaSJeff Mahoney 				if (ret) {
297479787eaaSJeff Mahoney 					btrfs_abort_transaction(trans, root,
297579787eaaSJeff Mahoney 								ret);
297679787eaaSJeff Mahoney 					btrfs_end_transaction(trans, root);
297779787eaaSJeff Mahoney 					goto out;
297879787eaaSJeff Mahoney 				}
297931840ae1SZheng Yan 
298031840ae1SZheng Yan 				leaf = path->nodes[0];
298131840ae1SZheng Yan 				slot = path->slots[0];
298231840ae1SZheng Yan 				write_extent_buffer(leaf, buf,
298331840ae1SZheng Yan 					    btrfs_item_ptr_offset(leaf, slot),
298431840ae1SZheng Yan 					    size);
2985ae01a0abSYan Zheng 
2986f46b5a66SChristoph Hellwig 				extent = btrfs_item_ptr(leaf, slot,
2987f46b5a66SChristoph Hellwig 						struct btrfs_file_extent_item);
2988c5c9cd4dSSage Weil 
2989c5c9cd4dSSage Weil 				/* disko == 0 means it's a hole */
2990c5c9cd4dSSage Weil 				if (!disko)
2991c5c9cd4dSSage Weil 					datao = 0;
2992c5c9cd4dSSage Weil 
2993c5c9cd4dSSage Weil 				btrfs_set_file_extent_offset(leaf, extent,
2994c5c9cd4dSSage Weil 							     datao);
2995c5c9cd4dSSage Weil 				btrfs_set_file_extent_num_bytes(leaf, extent,
2996c5c9cd4dSSage Weil 								datal);
2997c5c9cd4dSSage Weil 				if (disko) {
2998c5c9cd4dSSage Weil 					inode_add_bytes(inode, datal);
2999ae01a0abSYan Zheng 					ret = btrfs_inc_extent_ref(trans, root,
30005d4f98a2SYan Zheng 							disko, diskl, 0,
3001f46b5a66SChristoph Hellwig 							root->root_key.objectid,
300233345d01SLi Zefan 							btrfs_ino(inode),
300366d7e7f0SArne Jansen 							new_key.offset - datao,
300466d7e7f0SArne Jansen 							0);
300579787eaaSJeff Mahoney 					if (ret) {
300679787eaaSJeff Mahoney 						btrfs_abort_transaction(trans,
300779787eaaSJeff Mahoney 									root,
300879787eaaSJeff Mahoney 									ret);
300979787eaaSJeff Mahoney 						btrfs_end_transaction(trans,
301079787eaaSJeff Mahoney 								      root);
301179787eaaSJeff Mahoney 						goto out;
301279787eaaSJeff Mahoney 
301379787eaaSJeff Mahoney 					}
3014f46b5a66SChristoph Hellwig 				}
3015c5c9cd4dSSage Weil 			} else if (type == BTRFS_FILE_EXTENT_INLINE) {
3016c5c9cd4dSSage Weil 				u64 skip = 0;
3017c5c9cd4dSSage Weil 				u64 trim = 0;
3018c5c9cd4dSSage Weil 				if (off > key.offset) {
3019c5c9cd4dSSage Weil 					skip = off - key.offset;
3020c5c9cd4dSSage Weil 					new_key.offset += skip;
302131840ae1SZheng Yan 				}
3022d397712bSChris Mason 
3023c5c9cd4dSSage Weil 				if (key.offset + datal > off + len)
3024c5c9cd4dSSage Weil 					trim = key.offset + datal - (off + len);
3025d397712bSChris Mason 
3026c5c9cd4dSSage Weil 				if (comp && (skip || trim)) {
3027c5c9cd4dSSage Weil 					ret = -EINVAL;
3028a22285a6SYan, Zheng 					btrfs_end_transaction(trans, root);
3029c5c9cd4dSSage Weil 					goto out;
303031840ae1SZheng Yan 				}
3031c5c9cd4dSSage Weil 				size -= skip + trim;
3032c5c9cd4dSSage Weil 				datal -= skip + trim;
3033a22285a6SYan, Zheng 
30345dc562c5SJosef Bacik 				ret = btrfs_drop_extents(trans, root, inode,
3035a22285a6SYan, Zheng 							 new_key.offset,
3036a22285a6SYan, Zheng 							 new_key.offset + datal,
30372671485dSJosef Bacik 							 1);
303879787eaaSJeff Mahoney 				if (ret) {
303979787eaaSJeff Mahoney 					btrfs_abort_transaction(trans, root,
304079787eaaSJeff Mahoney 								ret);
304179787eaaSJeff Mahoney 					btrfs_end_transaction(trans, root);
304279787eaaSJeff Mahoney 					goto out;
304379787eaaSJeff Mahoney 				}
3044a22285a6SYan, Zheng 
3045c5c9cd4dSSage Weil 				ret = btrfs_insert_empty_item(trans, root, path,
3046c5c9cd4dSSage Weil 							      &new_key, size);
304779787eaaSJeff Mahoney 				if (ret) {
304879787eaaSJeff Mahoney 					btrfs_abort_transaction(trans, root,
304979787eaaSJeff Mahoney 								ret);
305079787eaaSJeff Mahoney 					btrfs_end_transaction(trans, root);
305179787eaaSJeff Mahoney 					goto out;
305279787eaaSJeff Mahoney 				}
3053c5c9cd4dSSage Weil 
3054c5c9cd4dSSage Weil 				if (skip) {
3055d397712bSChris Mason 					u32 start =
3056d397712bSChris Mason 					  btrfs_file_extent_calc_inline_size(0);
3057c5c9cd4dSSage Weil 					memmove(buf+start, buf+start+skip,
3058c5c9cd4dSSage Weil 						datal);
3059c5c9cd4dSSage Weil 				}
3060c5c9cd4dSSage Weil 
3061c5c9cd4dSSage Weil 				leaf = path->nodes[0];
3062c5c9cd4dSSage Weil 				slot = path->slots[0];
3063c5c9cd4dSSage Weil 				write_extent_buffer(leaf, buf,
3064c5c9cd4dSSage Weil 					    btrfs_item_ptr_offset(leaf, slot),
3065c5c9cd4dSSage Weil 					    size);
3066c5c9cd4dSSage Weil 				inode_add_bytes(inode, datal);
3067c5c9cd4dSSage Weil 			}
3068c5c9cd4dSSage Weil 
3069c5c9cd4dSSage Weil 			btrfs_mark_buffer_dirty(leaf);
3070b3b4aa74SDavid Sterba 			btrfs_release_path(path);
3071c5c9cd4dSSage Weil 
30720c4d2d95SJosef Bacik 			inode_inc_iversion(inode);
3073a22285a6SYan, Zheng 			inode->i_mtime = inode->i_ctime = CURRENT_TIME;
3074b5384d48SSage Weil 
3075b5384d48SSage Weil 			/*
3076b5384d48SSage Weil 			 * we round up to the block size at eof when
3077b5384d48SSage Weil 			 * determining which extents to clone above,
3078b5384d48SSage Weil 			 * but shouldn't round up the file size
3079b5384d48SSage Weil 			 */
3080b5384d48SSage Weil 			endoff = new_key.offset + datal;
30815f3888ffSLi Zefan 			if (endoff > destoff+olen)
30825f3888ffSLi Zefan 				endoff = destoff+olen;
3083b5384d48SSage Weil 			if (endoff > inode->i_size)
3084b5384d48SSage Weil 				btrfs_i_size_write(inode, endoff);
3085b5384d48SSage Weil 
3086a22285a6SYan, Zheng 			ret = btrfs_update_inode(trans, root, inode);
308779787eaaSJeff Mahoney 			if (ret) {
308879787eaaSJeff Mahoney 				btrfs_abort_transaction(trans, root, ret);
3089a22285a6SYan, Zheng 				btrfs_end_transaction(trans, root);
309079787eaaSJeff Mahoney 				goto out;
309179787eaaSJeff Mahoney 			}
309279787eaaSJeff Mahoney 			ret = btrfs_end_transaction(trans, root);
3093a22285a6SYan, Zheng 		}
3094c5c9cd4dSSage Weil next:
3095b3b4aa74SDavid Sterba 		btrfs_release_path(path);
3096ae01a0abSYan Zheng 		key.offset++;
3097ae01a0abSYan Zheng 	}
3098f46b5a66SChristoph Hellwig 	ret = 0;
309932b7c687SMark Fasheh 
3100f46b5a66SChristoph Hellwig out:
3101b3b4aa74SDavid Sterba 	btrfs_release_path(path);
310232b7c687SMark Fasheh 	btrfs_free_path(path);
310332b7c687SMark Fasheh 	vfree(buf);
310432b7c687SMark Fasheh 	return ret;
310532b7c687SMark Fasheh }
310632b7c687SMark Fasheh 
310732b7c687SMark Fasheh static noinline long btrfs_ioctl_clone(struct file *file, unsigned long srcfd,
310832b7c687SMark Fasheh 				       u64 off, u64 olen, u64 destoff)
310932b7c687SMark Fasheh {
311054563d41SAl Viro 	struct inode *inode = file_inode(file);
311132b7c687SMark Fasheh 	struct btrfs_root *root = BTRFS_I(inode)->root;
311232b7c687SMark Fasheh 	struct fd src_file;
311332b7c687SMark Fasheh 	struct inode *src;
311432b7c687SMark Fasheh 	int ret;
311532b7c687SMark Fasheh 	u64 len = olen;
311632b7c687SMark Fasheh 	u64 bs = root->fs_info->sb->s_blocksize;
311732b7c687SMark Fasheh 	int same_inode = 0;
311832b7c687SMark Fasheh 
311932b7c687SMark Fasheh 	/*
312032b7c687SMark Fasheh 	 * TODO:
312132b7c687SMark Fasheh 	 * - split compressed inline extents.  annoying: we need to
312232b7c687SMark Fasheh 	 *   decompress into destination's address_space (the file offset
312332b7c687SMark Fasheh 	 *   may change, so source mapping won't do), then recompress (or
312432b7c687SMark Fasheh 	 *   otherwise reinsert) a subrange.
312532b7c687SMark Fasheh 	 * - allow ranges within the same file to be cloned (provided
312632b7c687SMark Fasheh 	 *   they don't overlap)?
312732b7c687SMark Fasheh 	 */
312832b7c687SMark Fasheh 
312932b7c687SMark Fasheh 	/* the destination must be opened for writing */
313032b7c687SMark Fasheh 	if (!(file->f_mode & FMODE_WRITE) || (file->f_flags & O_APPEND))
313132b7c687SMark Fasheh 		return -EINVAL;
313232b7c687SMark Fasheh 
313332b7c687SMark Fasheh 	if (btrfs_root_readonly(root))
313432b7c687SMark Fasheh 		return -EROFS;
313532b7c687SMark Fasheh 
313632b7c687SMark Fasheh 	ret = mnt_want_write_file(file);
313732b7c687SMark Fasheh 	if (ret)
313832b7c687SMark Fasheh 		return ret;
313932b7c687SMark Fasheh 
314032b7c687SMark Fasheh 	src_file = fdget(srcfd);
314132b7c687SMark Fasheh 	if (!src_file.file) {
314232b7c687SMark Fasheh 		ret = -EBADF;
314332b7c687SMark Fasheh 		goto out_drop_write;
314432b7c687SMark Fasheh 	}
314532b7c687SMark Fasheh 
314632b7c687SMark Fasheh 	ret = -EXDEV;
314732b7c687SMark Fasheh 	if (src_file.file->f_path.mnt != file->f_path.mnt)
314832b7c687SMark Fasheh 		goto out_fput;
314932b7c687SMark Fasheh 
315032b7c687SMark Fasheh 	src = file_inode(src_file.file);
315132b7c687SMark Fasheh 
315232b7c687SMark Fasheh 	ret = -EINVAL;
315332b7c687SMark Fasheh 	if (src == inode)
315432b7c687SMark Fasheh 		same_inode = 1;
315532b7c687SMark Fasheh 
315632b7c687SMark Fasheh 	/* the src must be open for reading */
315732b7c687SMark Fasheh 	if (!(src_file.file->f_mode & FMODE_READ))
315832b7c687SMark Fasheh 		goto out_fput;
315932b7c687SMark Fasheh 
316032b7c687SMark Fasheh 	/* don't make the dst file partly checksummed */
316132b7c687SMark Fasheh 	if ((BTRFS_I(src)->flags & BTRFS_INODE_NODATASUM) !=
316232b7c687SMark Fasheh 	    (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM))
316332b7c687SMark Fasheh 		goto out_fput;
316432b7c687SMark Fasheh 
316532b7c687SMark Fasheh 	ret = -EISDIR;
316632b7c687SMark Fasheh 	if (S_ISDIR(src->i_mode) || S_ISDIR(inode->i_mode))
316732b7c687SMark Fasheh 		goto out_fput;
316832b7c687SMark Fasheh 
316932b7c687SMark Fasheh 	ret = -EXDEV;
317032b7c687SMark Fasheh 	if (src->i_sb != inode->i_sb)
317132b7c687SMark Fasheh 		goto out_fput;
317232b7c687SMark Fasheh 
317332b7c687SMark Fasheh 	if (!same_inode) {
317432b7c687SMark Fasheh 		if (inode < src) {
317532b7c687SMark Fasheh 			mutex_lock_nested(&inode->i_mutex, I_MUTEX_PARENT);
317632b7c687SMark Fasheh 			mutex_lock_nested(&src->i_mutex, I_MUTEX_CHILD);
317732b7c687SMark Fasheh 		} else {
317832b7c687SMark Fasheh 			mutex_lock_nested(&src->i_mutex, I_MUTEX_PARENT);
317932b7c687SMark Fasheh 			mutex_lock_nested(&inode->i_mutex, I_MUTEX_CHILD);
318032b7c687SMark Fasheh 		}
318132b7c687SMark Fasheh 	} else {
318232b7c687SMark Fasheh 		mutex_lock(&src->i_mutex);
318332b7c687SMark Fasheh 	}
318432b7c687SMark Fasheh 
318532b7c687SMark Fasheh 	/* determine range to clone */
318632b7c687SMark Fasheh 	ret = -EINVAL;
318732b7c687SMark Fasheh 	if (off + len > src->i_size || off + len < off)
318832b7c687SMark Fasheh 		goto out_unlock;
318932b7c687SMark Fasheh 	if (len == 0)
319032b7c687SMark Fasheh 		olen = len = src->i_size - off;
319132b7c687SMark Fasheh 	/* if we extend to eof, continue to block boundary */
319232b7c687SMark Fasheh 	if (off + len == src->i_size)
319332b7c687SMark Fasheh 		len = ALIGN(src->i_size, bs) - off;
319432b7c687SMark Fasheh 
319532b7c687SMark Fasheh 	/* verify the end result is block aligned */
319632b7c687SMark Fasheh 	if (!IS_ALIGNED(off, bs) || !IS_ALIGNED(off + len, bs) ||
319732b7c687SMark Fasheh 	    !IS_ALIGNED(destoff, bs))
319832b7c687SMark Fasheh 		goto out_unlock;
319932b7c687SMark Fasheh 
320032b7c687SMark Fasheh 	/* verify if ranges are overlapped within the same file */
320132b7c687SMark Fasheh 	if (same_inode) {
320232b7c687SMark Fasheh 		if (destoff + len > off && destoff < off + len)
320332b7c687SMark Fasheh 			goto out_unlock;
320432b7c687SMark Fasheh 	}
320532b7c687SMark Fasheh 
320632b7c687SMark Fasheh 	if (destoff > inode->i_size) {
320732b7c687SMark Fasheh 		ret = btrfs_cont_expand(inode, inode->i_size, destoff);
320832b7c687SMark Fasheh 		if (ret)
320932b7c687SMark Fasheh 			goto out_unlock;
321032b7c687SMark Fasheh 	}
321132b7c687SMark Fasheh 
321232b7c687SMark Fasheh 	/* truncate page cache pages from target inode range */
321332b7c687SMark Fasheh 	truncate_inode_pages_range(&inode->i_data, destoff,
321432b7c687SMark Fasheh 				   PAGE_CACHE_ALIGN(destoff + len) - 1);
321532b7c687SMark Fasheh 
321632b7c687SMark Fasheh 	lock_extent_range(src, off, len);
321732b7c687SMark Fasheh 
321832b7c687SMark Fasheh 	ret = btrfs_clone(src, inode, off, olen, len, destoff);
321932b7c687SMark Fasheh 
3220aa42ffd9SLiu Bo 	unlock_extent(&BTRFS_I(src)->io_tree, off, off + len - 1);
3221f46b5a66SChristoph Hellwig out_unlock:
3222f46b5a66SChristoph Hellwig 	mutex_unlock(&src->i_mutex);
3223a96fbc72SLiu Bo 	if (!same_inode)
3224f46b5a66SChristoph Hellwig 		mutex_unlock(&inode->i_mutex);
3225f46b5a66SChristoph Hellwig out_fput:
32262903ff01SAl Viro 	fdput(src_file);
3227ab67b7c1SYan Zheng out_drop_write:
32282a79f17eSAl Viro 	mnt_drop_write_file(file);
3229f46b5a66SChristoph Hellwig 	return ret;
3230f46b5a66SChristoph Hellwig }
3231f46b5a66SChristoph Hellwig 
32327a865e8aSChristoph Hellwig static long btrfs_ioctl_clone_range(struct file *file, void __user *argp)
3233c5c9cd4dSSage Weil {
3234c5c9cd4dSSage Weil 	struct btrfs_ioctl_clone_range_args args;
3235c5c9cd4dSSage Weil 
32367a865e8aSChristoph Hellwig 	if (copy_from_user(&args, argp, sizeof(args)))
3237c5c9cd4dSSage Weil 		return -EFAULT;
3238c5c9cd4dSSage Weil 	return btrfs_ioctl_clone(file, args.src_fd, args.src_offset,
3239c5c9cd4dSSage Weil 				 args.src_length, args.dest_offset);
3240c5c9cd4dSSage Weil }
3241c5c9cd4dSSage Weil 
3242f46b5a66SChristoph Hellwig /*
3243f46b5a66SChristoph Hellwig  * there are many ways the trans_start and trans_end ioctls can lead
3244f46b5a66SChristoph Hellwig  * to deadlocks.  They should only be used by applications that
3245f46b5a66SChristoph Hellwig  * basically own the machine, and have a very in depth understanding
3246f46b5a66SChristoph Hellwig  * of all the possible deadlocks and enospc problems.
3247f46b5a66SChristoph Hellwig  */
3248b2950863SChristoph Hellwig static long btrfs_ioctl_trans_start(struct file *file)
3249f46b5a66SChristoph Hellwig {
3250496ad9aaSAl Viro 	struct inode *inode = file_inode(file);
3251f46b5a66SChristoph Hellwig 	struct btrfs_root *root = BTRFS_I(inode)->root;
3252f46b5a66SChristoph Hellwig 	struct btrfs_trans_handle *trans;
32531ab86aedSSage Weil 	int ret;
3254f46b5a66SChristoph Hellwig 
32551ab86aedSSage Weil 	ret = -EPERM;
3256df5b5520SChristoph Hellwig 	if (!capable(CAP_SYS_ADMIN))
3257f46b5a66SChristoph Hellwig 		goto out;
32581ab86aedSSage Weil 
32591ab86aedSSage Weil 	ret = -EINPROGRESS;
32601ab86aedSSage Weil 	if (file->private_data)
32611ab86aedSSage Weil 		goto out;
32629ca9ee09SSage Weil 
3263b83cc969SLi Zefan 	ret = -EROFS;
3264b83cc969SLi Zefan 	if (btrfs_root_readonly(root))
3265b83cc969SLi Zefan 		goto out;
3266b83cc969SLi Zefan 
3267a561be71SAl Viro 	ret = mnt_want_write_file(file);
3268c146afadSYan Zheng 	if (ret)
3269c146afadSYan Zheng 		goto out;
3270c146afadSYan Zheng 
3271a4abeea4SJosef Bacik 	atomic_inc(&root->fs_info->open_ioctl_trans);
32729ca9ee09SSage Weil 
3273f46b5a66SChristoph Hellwig 	ret = -ENOMEM;
32747a7eaa40SJosef Bacik 	trans = btrfs_start_ioctl_transaction(root);
3275abd30bb0STsutomu Itoh 	if (IS_ERR(trans))
32761ab86aedSSage Weil 		goto out_drop;
32771ab86aedSSage Weil 
32781ab86aedSSage Weil 	file->private_data = trans;
32791ab86aedSSage Weil 	return 0;
32801ab86aedSSage Weil 
32811ab86aedSSage Weil out_drop:
3282a4abeea4SJosef Bacik 	atomic_dec(&root->fs_info->open_ioctl_trans);
32832a79f17eSAl Viro 	mnt_drop_write_file(file);
3284f46b5a66SChristoph Hellwig out:
3285f46b5a66SChristoph Hellwig 	return ret;
3286f46b5a66SChristoph Hellwig }
3287f46b5a66SChristoph Hellwig 
32886ef5ed0dSJosef Bacik static long btrfs_ioctl_default_subvol(struct file *file, void __user *argp)
32896ef5ed0dSJosef Bacik {
3290496ad9aaSAl Viro 	struct inode *inode = file_inode(file);
32916ef5ed0dSJosef Bacik 	struct btrfs_root *root = BTRFS_I(inode)->root;
32926ef5ed0dSJosef Bacik 	struct btrfs_root *new_root;
32936ef5ed0dSJosef Bacik 	struct btrfs_dir_item *di;
32946ef5ed0dSJosef Bacik 	struct btrfs_trans_handle *trans;
32956ef5ed0dSJosef Bacik 	struct btrfs_path *path;
32966ef5ed0dSJosef Bacik 	struct btrfs_key location;
32976ef5ed0dSJosef Bacik 	struct btrfs_disk_key disk_key;
32986ef5ed0dSJosef Bacik 	u64 objectid = 0;
32996ef5ed0dSJosef Bacik 	u64 dir_id;
33003c04ce01SMiao Xie 	int ret;
33016ef5ed0dSJosef Bacik 
33026ef5ed0dSJosef Bacik 	if (!capable(CAP_SYS_ADMIN))
33036ef5ed0dSJosef Bacik 		return -EPERM;
33046ef5ed0dSJosef Bacik 
33053c04ce01SMiao Xie 	ret = mnt_want_write_file(file);
33063c04ce01SMiao Xie 	if (ret)
33073c04ce01SMiao Xie 		return ret;
33083c04ce01SMiao Xie 
33093c04ce01SMiao Xie 	if (copy_from_user(&objectid, argp, sizeof(objectid))) {
33103c04ce01SMiao Xie 		ret = -EFAULT;
33113c04ce01SMiao Xie 		goto out;
33123c04ce01SMiao Xie 	}
33136ef5ed0dSJosef Bacik 
33146ef5ed0dSJosef Bacik 	if (!objectid)
33151cecf579Schandan 		objectid = BTRFS_FS_TREE_OBJECTID;
33166ef5ed0dSJosef Bacik 
33176ef5ed0dSJosef Bacik 	location.objectid = objectid;
33186ef5ed0dSJosef Bacik 	location.type = BTRFS_ROOT_ITEM_KEY;
33196ef5ed0dSJosef Bacik 	location.offset = (u64)-1;
33206ef5ed0dSJosef Bacik 
33216ef5ed0dSJosef Bacik 	new_root = btrfs_read_fs_root_no_name(root->fs_info, &location);
33223c04ce01SMiao Xie 	if (IS_ERR(new_root)) {
33233c04ce01SMiao Xie 		ret = PTR_ERR(new_root);
33243c04ce01SMiao Xie 		goto out;
33253c04ce01SMiao Xie 	}
33266ef5ed0dSJosef Bacik 
33276ef5ed0dSJosef Bacik 	path = btrfs_alloc_path();
33283c04ce01SMiao Xie 	if (!path) {
33293c04ce01SMiao Xie 		ret = -ENOMEM;
33303c04ce01SMiao Xie 		goto out;
33313c04ce01SMiao Xie 	}
33326ef5ed0dSJosef Bacik 	path->leave_spinning = 1;
33336ef5ed0dSJosef Bacik 
33346ef5ed0dSJosef Bacik 	trans = btrfs_start_transaction(root, 1);
333598d5dc13STsutomu Itoh 	if (IS_ERR(trans)) {
33366ef5ed0dSJosef Bacik 		btrfs_free_path(path);
33373c04ce01SMiao Xie 		ret = PTR_ERR(trans);
33383c04ce01SMiao Xie 		goto out;
33396ef5ed0dSJosef Bacik 	}
33406ef5ed0dSJosef Bacik 
33416c41761fSDavid Sterba 	dir_id = btrfs_super_root_dir(root->fs_info->super_copy);
33426ef5ed0dSJosef Bacik 	di = btrfs_lookup_dir_item(trans, root->fs_info->tree_root, path,
33436ef5ed0dSJosef Bacik 				   dir_id, "default", 7, 1);
3344cf1e99a4SDan Carpenter 	if (IS_ERR_OR_NULL(di)) {
33456ef5ed0dSJosef Bacik 		btrfs_free_path(path);
33466ef5ed0dSJosef Bacik 		btrfs_end_transaction(trans, root);
33476ef5ed0dSJosef Bacik 		printk(KERN_ERR "Umm, you don't have the default dir item, "
33486ef5ed0dSJosef Bacik 		       "this isn't going to work\n");
33493c04ce01SMiao Xie 		ret = -ENOENT;
33503c04ce01SMiao Xie 		goto out;
33516ef5ed0dSJosef Bacik 	}
33526ef5ed0dSJosef Bacik 
33536ef5ed0dSJosef Bacik 	btrfs_cpu_key_to_disk(&disk_key, &new_root->root_key);
33546ef5ed0dSJosef Bacik 	btrfs_set_dir_item_key(path->nodes[0], di, &disk_key);
33556ef5ed0dSJosef Bacik 	btrfs_mark_buffer_dirty(path->nodes[0]);
33566ef5ed0dSJosef Bacik 	btrfs_free_path(path);
33576ef5ed0dSJosef Bacik 
33582b0ce2c2SMitch Harder 	btrfs_set_fs_incompat(root->fs_info, DEFAULT_SUBVOL);
33596ef5ed0dSJosef Bacik 	btrfs_end_transaction(trans, root);
33603c04ce01SMiao Xie out:
33613c04ce01SMiao Xie 	mnt_drop_write_file(file);
33623c04ce01SMiao Xie 	return ret;
33636ef5ed0dSJosef Bacik }
33646ef5ed0dSJosef Bacik 
33655af3e8ccSStefan Behrens void btrfs_get_block_group_info(struct list_head *groups_list,
3366bf5fc093SJosef Bacik 				struct btrfs_ioctl_space_info *space)
3367bf5fc093SJosef Bacik {
3368bf5fc093SJosef Bacik 	struct btrfs_block_group_cache *block_group;
3369bf5fc093SJosef Bacik 
3370bf5fc093SJosef Bacik 	space->total_bytes = 0;
3371bf5fc093SJosef Bacik 	space->used_bytes = 0;
3372bf5fc093SJosef Bacik 	space->flags = 0;
3373bf5fc093SJosef Bacik 	list_for_each_entry(block_group, groups_list, list) {
3374bf5fc093SJosef Bacik 		space->flags = block_group->flags;
3375bf5fc093SJosef Bacik 		space->total_bytes += block_group->key.offset;
3376bf5fc093SJosef Bacik 		space->used_bytes +=
3377bf5fc093SJosef Bacik 			btrfs_block_group_used(&block_group->item);
3378bf5fc093SJosef Bacik 	}
3379bf5fc093SJosef Bacik }
3380bf5fc093SJosef Bacik 
338148a3b636SEric Sandeen static long btrfs_ioctl_space_info(struct btrfs_root *root, void __user *arg)
33821406e432SJosef Bacik {
33831406e432SJosef Bacik 	struct btrfs_ioctl_space_args space_args;
33841406e432SJosef Bacik 	struct btrfs_ioctl_space_info space;
33851406e432SJosef Bacik 	struct btrfs_ioctl_space_info *dest;
33867fde62bfSChris Mason 	struct btrfs_ioctl_space_info *dest_orig;
338713f2696fSDaniel J Blueman 	struct btrfs_ioctl_space_info __user *user_dest;
33881406e432SJosef Bacik 	struct btrfs_space_info *info;
3389bf5fc093SJosef Bacik 	u64 types[] = {BTRFS_BLOCK_GROUP_DATA,
3390bf5fc093SJosef Bacik 		       BTRFS_BLOCK_GROUP_SYSTEM,
3391bf5fc093SJosef Bacik 		       BTRFS_BLOCK_GROUP_METADATA,
3392bf5fc093SJosef Bacik 		       BTRFS_BLOCK_GROUP_DATA | BTRFS_BLOCK_GROUP_METADATA};
3393bf5fc093SJosef Bacik 	int num_types = 4;
33947fde62bfSChris Mason 	int alloc_size;
33951406e432SJosef Bacik 	int ret = 0;
339651788b1bSDan Rosenberg 	u64 slot_count = 0;
3397bf5fc093SJosef Bacik 	int i, c;
33981406e432SJosef Bacik 
33991406e432SJosef Bacik 	if (copy_from_user(&space_args,
34001406e432SJosef Bacik 			   (struct btrfs_ioctl_space_args __user *)arg,
34011406e432SJosef Bacik 			   sizeof(space_args)))
34021406e432SJosef Bacik 		return -EFAULT;
34031406e432SJosef Bacik 
3404bf5fc093SJosef Bacik 	for (i = 0; i < num_types; i++) {
3405bf5fc093SJosef Bacik 		struct btrfs_space_info *tmp;
3406bf5fc093SJosef Bacik 
3407bf5fc093SJosef Bacik 		info = NULL;
34087fde62bfSChris Mason 		rcu_read_lock();
3409bf5fc093SJosef Bacik 		list_for_each_entry_rcu(tmp, &root->fs_info->space_info,
3410bf5fc093SJosef Bacik 					list) {
3411bf5fc093SJosef Bacik 			if (tmp->flags == types[i]) {
3412bf5fc093SJosef Bacik 				info = tmp;
3413bf5fc093SJosef Bacik 				break;
3414bf5fc093SJosef Bacik 			}
3415bf5fc093SJosef Bacik 		}
34167fde62bfSChris Mason 		rcu_read_unlock();
34171406e432SJosef Bacik 
3418bf5fc093SJosef Bacik 		if (!info)
3419bf5fc093SJosef Bacik 			continue;
3420bf5fc093SJosef Bacik 
3421bf5fc093SJosef Bacik 		down_read(&info->groups_sem);
3422bf5fc093SJosef Bacik 		for (c = 0; c < BTRFS_NR_RAID_TYPES; c++) {
3423bf5fc093SJosef Bacik 			if (!list_empty(&info->block_groups[c]))
3424bf5fc093SJosef Bacik 				slot_count++;
3425bf5fc093SJosef Bacik 		}
3426bf5fc093SJosef Bacik 		up_read(&info->groups_sem);
3427bf5fc093SJosef Bacik 	}
3428bf5fc093SJosef Bacik 
34297fde62bfSChris Mason 	/* space_slots == 0 means they are asking for a count */
34307fde62bfSChris Mason 	if (space_args.space_slots == 0) {
34317fde62bfSChris Mason 		space_args.total_spaces = slot_count;
34327fde62bfSChris Mason 		goto out;
34337fde62bfSChris Mason 	}
3434bf5fc093SJosef Bacik 
343551788b1bSDan Rosenberg 	slot_count = min_t(u64, space_args.space_slots, slot_count);
3436bf5fc093SJosef Bacik 
34377fde62bfSChris Mason 	alloc_size = sizeof(*dest) * slot_count;
3438bf5fc093SJosef Bacik 
34397fde62bfSChris Mason 	/* we generally have at most 6 or so space infos, one for each raid
34407fde62bfSChris Mason 	 * level.  So, a whole page should be more than enough for everyone
34417fde62bfSChris Mason 	 */
34427fde62bfSChris Mason 	if (alloc_size > PAGE_CACHE_SIZE)
34437fde62bfSChris Mason 		return -ENOMEM;
34447fde62bfSChris Mason 
34457fde62bfSChris Mason 	space_args.total_spaces = 0;
34467fde62bfSChris Mason 	dest = kmalloc(alloc_size, GFP_NOFS);
34477fde62bfSChris Mason 	if (!dest)
34487fde62bfSChris Mason 		return -ENOMEM;
34497fde62bfSChris Mason 	dest_orig = dest;
34507fde62bfSChris Mason 
34517fde62bfSChris Mason 	/* now we have a buffer to copy into */
3452bf5fc093SJosef Bacik 	for (i = 0; i < num_types; i++) {
3453bf5fc093SJosef Bacik 		struct btrfs_space_info *tmp;
3454bf5fc093SJosef Bacik 
345551788b1bSDan Rosenberg 		if (!slot_count)
345651788b1bSDan Rosenberg 			break;
345751788b1bSDan Rosenberg 
3458bf5fc093SJosef Bacik 		info = NULL;
34591406e432SJosef Bacik 		rcu_read_lock();
3460bf5fc093SJosef Bacik 		list_for_each_entry_rcu(tmp, &root->fs_info->space_info,
3461bf5fc093SJosef Bacik 					list) {
3462bf5fc093SJosef Bacik 			if (tmp->flags == types[i]) {
3463bf5fc093SJosef Bacik 				info = tmp;
34647fde62bfSChris Mason 				break;
3465bf5fc093SJosef Bacik 			}
3466bf5fc093SJosef Bacik 		}
3467bf5fc093SJosef Bacik 		rcu_read_unlock();
34687fde62bfSChris Mason 
3469bf5fc093SJosef Bacik 		if (!info)
3470bf5fc093SJosef Bacik 			continue;
3471bf5fc093SJosef Bacik 		down_read(&info->groups_sem);
3472bf5fc093SJosef Bacik 		for (c = 0; c < BTRFS_NR_RAID_TYPES; c++) {
3473bf5fc093SJosef Bacik 			if (!list_empty(&info->block_groups[c])) {
34745af3e8ccSStefan Behrens 				btrfs_get_block_group_info(
34755af3e8ccSStefan Behrens 					&info->block_groups[c], &space);
34767fde62bfSChris Mason 				memcpy(dest, &space, sizeof(space));
34771406e432SJosef Bacik 				dest++;
34781406e432SJosef Bacik 				space_args.total_spaces++;
347951788b1bSDan Rosenberg 				slot_count--;
34801406e432SJosef Bacik 			}
348151788b1bSDan Rosenberg 			if (!slot_count)
348251788b1bSDan Rosenberg 				break;
3483bf5fc093SJosef Bacik 		}
3484bf5fc093SJosef Bacik 		up_read(&info->groups_sem);
3485bf5fc093SJosef Bacik 	}
34861406e432SJosef Bacik 
34872eec6c81SDaniel J Blueman 	user_dest = (struct btrfs_ioctl_space_info __user *)
34887fde62bfSChris Mason 		(arg + sizeof(struct btrfs_ioctl_space_args));
34897fde62bfSChris Mason 
34907fde62bfSChris Mason 	if (copy_to_user(user_dest, dest_orig, alloc_size))
34917fde62bfSChris Mason 		ret = -EFAULT;
34927fde62bfSChris Mason 
34937fde62bfSChris Mason 	kfree(dest_orig);
34947fde62bfSChris Mason out:
34957fde62bfSChris Mason 	if (ret == 0 && copy_to_user(arg, &space_args, sizeof(space_args)))
34961406e432SJosef Bacik 		ret = -EFAULT;
34971406e432SJosef Bacik 
34981406e432SJosef Bacik 	return ret;
34991406e432SJosef Bacik }
35001406e432SJosef Bacik 
3501f46b5a66SChristoph Hellwig /*
3502f46b5a66SChristoph Hellwig  * there are many ways the trans_start and trans_end ioctls can lead
3503f46b5a66SChristoph Hellwig  * to deadlocks.  They should only be used by applications that
3504f46b5a66SChristoph Hellwig  * basically own the machine, and have a very in depth understanding
3505f46b5a66SChristoph Hellwig  * of all the possible deadlocks and enospc problems.
3506f46b5a66SChristoph Hellwig  */
3507f46b5a66SChristoph Hellwig long btrfs_ioctl_trans_end(struct file *file)
3508f46b5a66SChristoph Hellwig {
3509496ad9aaSAl Viro 	struct inode *inode = file_inode(file);
3510f46b5a66SChristoph Hellwig 	struct btrfs_root *root = BTRFS_I(inode)->root;
3511f46b5a66SChristoph Hellwig 	struct btrfs_trans_handle *trans;
3512f46b5a66SChristoph Hellwig 
3513f46b5a66SChristoph Hellwig 	trans = file->private_data;
35141ab86aedSSage Weil 	if (!trans)
35151ab86aedSSage Weil 		return -EINVAL;
3516b214107eSChristoph Hellwig 	file->private_data = NULL;
35179ca9ee09SSage Weil 
35181ab86aedSSage Weil 	btrfs_end_transaction(trans, root);
35191ab86aedSSage Weil 
3520a4abeea4SJosef Bacik 	atomic_dec(&root->fs_info->open_ioctl_trans);
35219ca9ee09SSage Weil 
35222a79f17eSAl Viro 	mnt_drop_write_file(file);
35231ab86aedSSage Weil 	return 0;
3524f46b5a66SChristoph Hellwig }
3525f46b5a66SChristoph Hellwig 
35269a8c28beSMiao Xie static noinline long btrfs_ioctl_start_sync(struct btrfs_root *root,
35279a8c28beSMiao Xie 					    void __user *argp)
352846204592SSage Weil {
352946204592SSage Weil 	struct btrfs_trans_handle *trans;
353046204592SSage Weil 	u64 transid;
3531db5b493aSTsutomu Itoh 	int ret;
353246204592SSage Weil 
3533d4edf39bSMiao Xie 	trans = btrfs_attach_transaction_barrier(root);
3534ff7c1d33SMiao Xie 	if (IS_ERR(trans)) {
3535ff7c1d33SMiao Xie 		if (PTR_ERR(trans) != -ENOENT)
353698d5dc13STsutomu Itoh 			return PTR_ERR(trans);
3537ff7c1d33SMiao Xie 
3538ff7c1d33SMiao Xie 		/* No running transaction, don't bother */
3539ff7c1d33SMiao Xie 		transid = root->fs_info->last_trans_committed;
3540ff7c1d33SMiao Xie 		goto out;
3541ff7c1d33SMiao Xie 	}
354246204592SSage Weil 	transid = trans->transid;
3543db5b493aSTsutomu Itoh 	ret = btrfs_commit_transaction_async(trans, root, 0);
35448b2b2d3cSTsutomu Itoh 	if (ret) {
35458b2b2d3cSTsutomu Itoh 		btrfs_end_transaction(trans, root);
3546db5b493aSTsutomu Itoh 		return ret;
35478b2b2d3cSTsutomu Itoh 	}
3548ff7c1d33SMiao Xie out:
354946204592SSage Weil 	if (argp)
355046204592SSage Weil 		if (copy_to_user(argp, &transid, sizeof(transid)))
355146204592SSage Weil 			return -EFAULT;
355246204592SSage Weil 	return 0;
355346204592SSage Weil }
355446204592SSage Weil 
35559a8c28beSMiao Xie static noinline long btrfs_ioctl_wait_sync(struct btrfs_root *root,
35569a8c28beSMiao Xie 					   void __user *argp)
355746204592SSage Weil {
355846204592SSage Weil 	u64 transid;
355946204592SSage Weil 
356046204592SSage Weil 	if (argp) {
356146204592SSage Weil 		if (copy_from_user(&transid, argp, sizeof(transid)))
356246204592SSage Weil 			return -EFAULT;
356346204592SSage Weil 	} else {
356446204592SSage Weil 		transid = 0;  /* current trans */
356546204592SSage Weil 	}
356646204592SSage Weil 	return btrfs_wait_for_commit(root, transid);
356746204592SSage Weil }
356846204592SSage Weil 
3569b8e95489SMiao Xie static long btrfs_ioctl_scrub(struct file *file, void __user *arg)
3570475f6387SJan Schmidt {
3571496ad9aaSAl Viro 	struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
3572475f6387SJan Schmidt 	struct btrfs_ioctl_scrub_args *sa;
3573b8e95489SMiao Xie 	int ret;
3574475f6387SJan Schmidt 
3575475f6387SJan Schmidt 	if (!capable(CAP_SYS_ADMIN))
3576475f6387SJan Schmidt 		return -EPERM;
3577475f6387SJan Schmidt 
3578475f6387SJan Schmidt 	sa = memdup_user(arg, sizeof(*sa));
3579475f6387SJan Schmidt 	if (IS_ERR(sa))
3580475f6387SJan Schmidt 		return PTR_ERR(sa);
3581475f6387SJan Schmidt 
3582b8e95489SMiao Xie 	if (!(sa->flags & BTRFS_SCRUB_READONLY)) {
3583b8e95489SMiao Xie 		ret = mnt_want_write_file(file);
3584b8e95489SMiao Xie 		if (ret)
3585b8e95489SMiao Xie 			goto out;
3586b8e95489SMiao Xie 	}
3587b8e95489SMiao Xie 
3588aa1b8cd4SStefan Behrens 	ret = btrfs_scrub_dev(root->fs_info, sa->devid, sa->start, sa->end,
358963a212abSStefan Behrens 			      &sa->progress, sa->flags & BTRFS_SCRUB_READONLY,
359063a212abSStefan Behrens 			      0);
3591475f6387SJan Schmidt 
3592475f6387SJan Schmidt 	if (copy_to_user(arg, sa, sizeof(*sa)))
3593475f6387SJan Schmidt 		ret = -EFAULT;
3594475f6387SJan Schmidt 
3595b8e95489SMiao Xie 	if (!(sa->flags & BTRFS_SCRUB_READONLY))
3596b8e95489SMiao Xie 		mnt_drop_write_file(file);
3597b8e95489SMiao Xie out:
3598475f6387SJan Schmidt 	kfree(sa);
3599475f6387SJan Schmidt 	return ret;
3600475f6387SJan Schmidt }
3601475f6387SJan Schmidt 
3602475f6387SJan Schmidt static long btrfs_ioctl_scrub_cancel(struct btrfs_root *root, void __user *arg)
3603475f6387SJan Schmidt {
3604475f6387SJan Schmidt 	if (!capable(CAP_SYS_ADMIN))
3605475f6387SJan Schmidt 		return -EPERM;
3606475f6387SJan Schmidt 
3607aa1b8cd4SStefan Behrens 	return btrfs_scrub_cancel(root->fs_info);
3608475f6387SJan Schmidt }
3609475f6387SJan Schmidt 
3610475f6387SJan Schmidt static long btrfs_ioctl_scrub_progress(struct btrfs_root *root,
3611475f6387SJan Schmidt 				       void __user *arg)
3612475f6387SJan Schmidt {
3613475f6387SJan Schmidt 	struct btrfs_ioctl_scrub_args *sa;
3614475f6387SJan Schmidt 	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 
3623475f6387SJan Schmidt 	ret = btrfs_scrub_progress(root, sa->devid, &sa->progress);
3624475f6387SJan Schmidt 
3625475f6387SJan Schmidt 	if (copy_to_user(arg, sa, sizeof(*sa)))
3626475f6387SJan Schmidt 		ret = -EFAULT;
3627475f6387SJan Schmidt 
3628475f6387SJan Schmidt 	kfree(sa);
3629475f6387SJan Schmidt 	return ret;
3630475f6387SJan Schmidt }
3631475f6387SJan Schmidt 
3632c11d2c23SStefan Behrens static long btrfs_ioctl_get_dev_stats(struct btrfs_root *root,
3633b27f7c0cSDavid Sterba 				      void __user *arg)
3634c11d2c23SStefan Behrens {
3635c11d2c23SStefan Behrens 	struct btrfs_ioctl_get_dev_stats *sa;
3636c11d2c23SStefan Behrens 	int ret;
3637c11d2c23SStefan Behrens 
3638c11d2c23SStefan Behrens 	sa = memdup_user(arg, sizeof(*sa));
3639c11d2c23SStefan Behrens 	if (IS_ERR(sa))
3640c11d2c23SStefan Behrens 		return PTR_ERR(sa);
3641c11d2c23SStefan Behrens 
3642b27f7c0cSDavid Sterba 	if ((sa->flags & BTRFS_DEV_STATS_RESET) && !capable(CAP_SYS_ADMIN)) {
3643b27f7c0cSDavid Sterba 		kfree(sa);
3644b27f7c0cSDavid Sterba 		return -EPERM;
3645b27f7c0cSDavid Sterba 	}
3646b27f7c0cSDavid Sterba 
3647b27f7c0cSDavid Sterba 	ret = btrfs_get_dev_stats(root, sa);
3648c11d2c23SStefan Behrens 
3649c11d2c23SStefan Behrens 	if (copy_to_user(arg, sa, sizeof(*sa)))
3650c11d2c23SStefan Behrens 		ret = -EFAULT;
3651c11d2c23SStefan Behrens 
3652c11d2c23SStefan Behrens 	kfree(sa);
3653c11d2c23SStefan Behrens 	return ret;
3654c11d2c23SStefan Behrens }
3655c11d2c23SStefan Behrens 
36563f6bcfbdSStefan Behrens static long btrfs_ioctl_dev_replace(struct btrfs_root *root, void __user *arg)
36573f6bcfbdSStefan Behrens {
36583f6bcfbdSStefan Behrens 	struct btrfs_ioctl_dev_replace_args *p;
36593f6bcfbdSStefan Behrens 	int ret;
36603f6bcfbdSStefan Behrens 
36613f6bcfbdSStefan Behrens 	if (!capable(CAP_SYS_ADMIN))
36623f6bcfbdSStefan Behrens 		return -EPERM;
36633f6bcfbdSStefan Behrens 
36643f6bcfbdSStefan Behrens 	p = memdup_user(arg, sizeof(*p));
36653f6bcfbdSStefan Behrens 	if (IS_ERR(p))
36663f6bcfbdSStefan Behrens 		return PTR_ERR(p);
36673f6bcfbdSStefan Behrens 
36683f6bcfbdSStefan Behrens 	switch (p->cmd) {
36693f6bcfbdSStefan Behrens 	case BTRFS_IOCTL_DEV_REPLACE_CMD_START:
3670adfa97cbSIlya Dryomov 		if (root->fs_info->sb->s_flags & MS_RDONLY) {
3671adfa97cbSIlya Dryomov 			ret = -EROFS;
3672adfa97cbSIlya Dryomov 			goto out;
3673adfa97cbSIlya Dryomov 		}
36743f6bcfbdSStefan Behrens 		if (atomic_xchg(
36753f6bcfbdSStefan Behrens 			&root->fs_info->mutually_exclusive_operation_running,
36763f6bcfbdSStefan Behrens 			1)) {
3677e57138b3SAnand Jain 			ret = BTRFS_ERROR_DEV_EXCL_RUN_IN_PROGRESS;
36783f6bcfbdSStefan Behrens 		} else {
36793f6bcfbdSStefan Behrens 			ret = btrfs_dev_replace_start(root, p);
36803f6bcfbdSStefan Behrens 			atomic_set(
36813f6bcfbdSStefan Behrens 			 &root->fs_info->mutually_exclusive_operation_running,
36823f6bcfbdSStefan Behrens 			 0);
36833f6bcfbdSStefan Behrens 		}
36843f6bcfbdSStefan Behrens 		break;
36853f6bcfbdSStefan Behrens 	case BTRFS_IOCTL_DEV_REPLACE_CMD_STATUS:
36863f6bcfbdSStefan Behrens 		btrfs_dev_replace_status(root->fs_info, p);
36873f6bcfbdSStefan Behrens 		ret = 0;
36883f6bcfbdSStefan Behrens 		break;
36893f6bcfbdSStefan Behrens 	case BTRFS_IOCTL_DEV_REPLACE_CMD_CANCEL:
36903f6bcfbdSStefan Behrens 		ret = btrfs_dev_replace_cancel(root->fs_info, p);
36913f6bcfbdSStefan Behrens 		break;
36923f6bcfbdSStefan Behrens 	default:
36933f6bcfbdSStefan Behrens 		ret = -EINVAL;
36943f6bcfbdSStefan Behrens 		break;
36953f6bcfbdSStefan Behrens 	}
36963f6bcfbdSStefan Behrens 
36973f6bcfbdSStefan Behrens 	if (copy_to_user(arg, p, sizeof(*p)))
36983f6bcfbdSStefan Behrens 		ret = -EFAULT;
3699adfa97cbSIlya Dryomov out:
37003f6bcfbdSStefan Behrens 	kfree(p);
37013f6bcfbdSStefan Behrens 	return ret;
37023f6bcfbdSStefan Behrens }
37033f6bcfbdSStefan Behrens 
3704d7728c96SJan Schmidt static long btrfs_ioctl_ino_to_path(struct btrfs_root *root, void __user *arg)
3705d7728c96SJan Schmidt {
3706d7728c96SJan Schmidt 	int ret = 0;
3707d7728c96SJan Schmidt 	int i;
3708740c3d22SChris Mason 	u64 rel_ptr;
3709d7728c96SJan Schmidt 	int size;
3710806468f8SChris Mason 	struct btrfs_ioctl_ino_path_args *ipa = NULL;
3711d7728c96SJan Schmidt 	struct inode_fs_paths *ipath = NULL;
3712d7728c96SJan Schmidt 	struct btrfs_path *path;
3713d7728c96SJan Schmidt 
371482b22ac8SKusanagi Kouichi 	if (!capable(CAP_DAC_READ_SEARCH))
3715d7728c96SJan Schmidt 		return -EPERM;
3716d7728c96SJan Schmidt 
3717d7728c96SJan Schmidt 	path = btrfs_alloc_path();
3718d7728c96SJan Schmidt 	if (!path) {
3719d7728c96SJan Schmidt 		ret = -ENOMEM;
3720d7728c96SJan Schmidt 		goto out;
3721d7728c96SJan Schmidt 	}
3722d7728c96SJan Schmidt 
3723d7728c96SJan Schmidt 	ipa = memdup_user(arg, sizeof(*ipa));
3724d7728c96SJan Schmidt 	if (IS_ERR(ipa)) {
3725d7728c96SJan Schmidt 		ret = PTR_ERR(ipa);
3726d7728c96SJan Schmidt 		ipa = NULL;
3727d7728c96SJan Schmidt 		goto out;
3728d7728c96SJan Schmidt 	}
3729d7728c96SJan Schmidt 
3730d7728c96SJan Schmidt 	size = min_t(u32, ipa->size, 4096);
3731d7728c96SJan Schmidt 	ipath = init_ipath(size, root, path);
3732d7728c96SJan Schmidt 	if (IS_ERR(ipath)) {
3733d7728c96SJan Schmidt 		ret = PTR_ERR(ipath);
3734d7728c96SJan Schmidt 		ipath = NULL;
3735d7728c96SJan Schmidt 		goto out;
3736d7728c96SJan Schmidt 	}
3737d7728c96SJan Schmidt 
3738d7728c96SJan Schmidt 	ret = paths_from_inode(ipa->inum, ipath);
3739d7728c96SJan Schmidt 	if (ret < 0)
3740d7728c96SJan Schmidt 		goto out;
3741d7728c96SJan Schmidt 
3742d7728c96SJan Schmidt 	for (i = 0; i < ipath->fspath->elem_cnt; ++i) {
3743745c4d8eSJeff Mahoney 		rel_ptr = ipath->fspath->val[i] -
3744745c4d8eSJeff Mahoney 			  (u64)(unsigned long)ipath->fspath->val;
3745740c3d22SChris Mason 		ipath->fspath->val[i] = rel_ptr;
3746d7728c96SJan Schmidt 	}
3747d7728c96SJan Schmidt 
3748745c4d8eSJeff Mahoney 	ret = copy_to_user((void *)(unsigned long)ipa->fspath,
3749745c4d8eSJeff Mahoney 			   (void *)(unsigned long)ipath->fspath, size);
3750d7728c96SJan Schmidt 	if (ret) {
3751d7728c96SJan Schmidt 		ret = -EFAULT;
3752d7728c96SJan Schmidt 		goto out;
3753d7728c96SJan Schmidt 	}
3754d7728c96SJan Schmidt 
3755d7728c96SJan Schmidt out:
3756d7728c96SJan Schmidt 	btrfs_free_path(path);
3757d7728c96SJan Schmidt 	free_ipath(ipath);
3758d7728c96SJan Schmidt 	kfree(ipa);
3759d7728c96SJan Schmidt 
3760d7728c96SJan Schmidt 	return ret;
3761d7728c96SJan Schmidt }
3762d7728c96SJan Schmidt 
3763d7728c96SJan Schmidt static int build_ino_list(u64 inum, u64 offset, u64 root, void *ctx)
3764d7728c96SJan Schmidt {
3765d7728c96SJan Schmidt 	struct btrfs_data_container *inodes = ctx;
3766d7728c96SJan Schmidt 	const size_t c = 3 * sizeof(u64);
3767d7728c96SJan Schmidt 
3768d7728c96SJan Schmidt 	if (inodes->bytes_left >= c) {
3769d7728c96SJan Schmidt 		inodes->bytes_left -= c;
3770d7728c96SJan Schmidt 		inodes->val[inodes->elem_cnt] = inum;
3771d7728c96SJan Schmidt 		inodes->val[inodes->elem_cnt + 1] = offset;
3772d7728c96SJan Schmidt 		inodes->val[inodes->elem_cnt + 2] = root;
3773d7728c96SJan Schmidt 		inodes->elem_cnt += 3;
3774d7728c96SJan Schmidt 	} else {
3775d7728c96SJan Schmidt 		inodes->bytes_missing += c - inodes->bytes_left;
3776d7728c96SJan Schmidt 		inodes->bytes_left = 0;
3777d7728c96SJan Schmidt 		inodes->elem_missed += 3;
3778d7728c96SJan Schmidt 	}
3779d7728c96SJan Schmidt 
3780d7728c96SJan Schmidt 	return 0;
3781d7728c96SJan Schmidt }
3782d7728c96SJan Schmidt 
3783d7728c96SJan Schmidt static long btrfs_ioctl_logical_to_ino(struct btrfs_root *root,
3784d7728c96SJan Schmidt 					void __user *arg)
3785d7728c96SJan Schmidt {
3786d7728c96SJan Schmidt 	int ret = 0;
3787d7728c96SJan Schmidt 	int size;
3788d7728c96SJan Schmidt 	struct btrfs_ioctl_logical_ino_args *loi;
3789d7728c96SJan Schmidt 	struct btrfs_data_container *inodes = NULL;
3790d7728c96SJan Schmidt 	struct btrfs_path *path = NULL;
3791d7728c96SJan Schmidt 
3792d7728c96SJan Schmidt 	if (!capable(CAP_SYS_ADMIN))
3793d7728c96SJan Schmidt 		return -EPERM;
3794d7728c96SJan Schmidt 
3795d7728c96SJan Schmidt 	loi = memdup_user(arg, sizeof(*loi));
3796d7728c96SJan Schmidt 	if (IS_ERR(loi)) {
3797d7728c96SJan Schmidt 		ret = PTR_ERR(loi);
3798d7728c96SJan Schmidt 		loi = NULL;
3799d7728c96SJan Schmidt 		goto out;
3800d7728c96SJan Schmidt 	}
3801d7728c96SJan Schmidt 
3802d7728c96SJan Schmidt 	path = btrfs_alloc_path();
3803d7728c96SJan Schmidt 	if (!path) {
3804d7728c96SJan Schmidt 		ret = -ENOMEM;
3805d7728c96SJan Schmidt 		goto out;
3806d7728c96SJan Schmidt 	}
3807d7728c96SJan Schmidt 
3808425d17a2SLiu Bo 	size = min_t(u32, loi->size, 64 * 1024);
3809d7728c96SJan Schmidt 	inodes = init_data_container(size);
3810d7728c96SJan Schmidt 	if (IS_ERR(inodes)) {
3811d7728c96SJan Schmidt 		ret = PTR_ERR(inodes);
3812d7728c96SJan Schmidt 		inodes = NULL;
3813d7728c96SJan Schmidt 		goto out;
3814d7728c96SJan Schmidt 	}
3815d7728c96SJan Schmidt 
3816df031f07SLiu Bo 	ret = iterate_inodes_from_logical(loi->logical, root->fs_info, path,
3817df031f07SLiu Bo 					  build_ino_list, inodes);
3818df031f07SLiu Bo 	if (ret == -EINVAL)
3819d7728c96SJan Schmidt 		ret = -ENOENT;
3820d7728c96SJan Schmidt 	if (ret < 0)
3821d7728c96SJan Schmidt 		goto out;
3822d7728c96SJan Schmidt 
3823745c4d8eSJeff Mahoney 	ret = copy_to_user((void *)(unsigned long)loi->inodes,
3824745c4d8eSJeff Mahoney 			   (void *)(unsigned long)inodes, size);
3825d7728c96SJan Schmidt 	if (ret)
3826d7728c96SJan Schmidt 		ret = -EFAULT;
3827d7728c96SJan Schmidt 
3828d7728c96SJan Schmidt out:
3829d7728c96SJan Schmidt 	btrfs_free_path(path);
3830425d17a2SLiu Bo 	vfree(inodes);
3831d7728c96SJan Schmidt 	kfree(loi);
3832d7728c96SJan Schmidt 
3833d7728c96SJan Schmidt 	return ret;
3834d7728c96SJan Schmidt }
3835d7728c96SJan Schmidt 
383619a39dceSIlya Dryomov void update_ioctl_balance_args(struct btrfs_fs_info *fs_info, int lock,
3837c9e9f97bSIlya Dryomov 			       struct btrfs_ioctl_balance_args *bargs)
3838c9e9f97bSIlya Dryomov {
3839c9e9f97bSIlya Dryomov 	struct btrfs_balance_control *bctl = fs_info->balance_ctl;
3840c9e9f97bSIlya Dryomov 
3841c9e9f97bSIlya Dryomov 	bargs->flags = bctl->flags;
3842c9e9f97bSIlya Dryomov 
3843837d5b6eSIlya Dryomov 	if (atomic_read(&fs_info->balance_running))
3844837d5b6eSIlya Dryomov 		bargs->state |= BTRFS_BALANCE_STATE_RUNNING;
3845837d5b6eSIlya Dryomov 	if (atomic_read(&fs_info->balance_pause_req))
3846837d5b6eSIlya Dryomov 		bargs->state |= BTRFS_BALANCE_STATE_PAUSE_REQ;
3847a7e99c69SIlya Dryomov 	if (atomic_read(&fs_info->balance_cancel_req))
3848a7e99c69SIlya Dryomov 		bargs->state |= BTRFS_BALANCE_STATE_CANCEL_REQ;
3849837d5b6eSIlya Dryomov 
3850c9e9f97bSIlya Dryomov 	memcpy(&bargs->data, &bctl->data, sizeof(bargs->data));
3851c9e9f97bSIlya Dryomov 	memcpy(&bargs->meta, &bctl->meta, sizeof(bargs->meta));
3852c9e9f97bSIlya Dryomov 	memcpy(&bargs->sys, &bctl->sys, sizeof(bargs->sys));
385319a39dceSIlya Dryomov 
385419a39dceSIlya Dryomov 	if (lock) {
385519a39dceSIlya Dryomov 		spin_lock(&fs_info->balance_lock);
385619a39dceSIlya Dryomov 		memcpy(&bargs->stat, &bctl->stat, sizeof(bargs->stat));
385719a39dceSIlya Dryomov 		spin_unlock(&fs_info->balance_lock);
385819a39dceSIlya Dryomov 	} else {
385919a39dceSIlya Dryomov 		memcpy(&bargs->stat, &bctl->stat, sizeof(bargs->stat));
386019a39dceSIlya Dryomov 	}
3861c9e9f97bSIlya Dryomov }
3862c9e9f97bSIlya Dryomov 
38639ba1f6e4SLiu Bo static long btrfs_ioctl_balance(struct file *file, void __user *arg)
3864c9e9f97bSIlya Dryomov {
3865496ad9aaSAl Viro 	struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
3866c9e9f97bSIlya Dryomov 	struct btrfs_fs_info *fs_info = root->fs_info;
3867c9e9f97bSIlya Dryomov 	struct btrfs_ioctl_balance_args *bargs;
3868c9e9f97bSIlya Dryomov 	struct btrfs_balance_control *bctl;
3869ed0fb78fSIlya Dryomov 	bool need_unlock; /* for mut. excl. ops lock */
3870c9e9f97bSIlya Dryomov 	int ret;
3871c9e9f97bSIlya Dryomov 
3872c9e9f97bSIlya Dryomov 	if (!capable(CAP_SYS_ADMIN))
3873c9e9f97bSIlya Dryomov 		return -EPERM;
3874c9e9f97bSIlya Dryomov 
3875e54bfa31SLiu Bo 	ret = mnt_want_write_file(file);
38769ba1f6e4SLiu Bo 	if (ret)
38779ba1f6e4SLiu Bo 		return ret;
38789ba1f6e4SLiu Bo 
3879ed0fb78fSIlya Dryomov again:
3880ed0fb78fSIlya Dryomov 	if (!atomic_xchg(&fs_info->mutually_exclusive_operation_running, 1)) {
3881c9e9f97bSIlya Dryomov 		mutex_lock(&fs_info->volume_mutex);
3882c9e9f97bSIlya Dryomov 		mutex_lock(&fs_info->balance_mutex);
3883ed0fb78fSIlya Dryomov 		need_unlock = true;
3884ed0fb78fSIlya Dryomov 		goto locked;
3885ed0fb78fSIlya Dryomov 	}
3886ed0fb78fSIlya Dryomov 
3887ed0fb78fSIlya Dryomov 	/*
3888ed0fb78fSIlya Dryomov 	 * mut. excl. ops lock is locked.  Three possibilites:
3889ed0fb78fSIlya Dryomov 	 *   (1) some other op is running
3890ed0fb78fSIlya Dryomov 	 *   (2) balance is running
3891ed0fb78fSIlya Dryomov 	 *   (3) balance is paused -- special case (think resume)
3892ed0fb78fSIlya Dryomov 	 */
3893ed0fb78fSIlya Dryomov 	mutex_lock(&fs_info->balance_mutex);
3894ed0fb78fSIlya Dryomov 	if (fs_info->balance_ctl) {
3895ed0fb78fSIlya Dryomov 		/* this is either (2) or (3) */
3896ed0fb78fSIlya Dryomov 		if (!atomic_read(&fs_info->balance_running)) {
3897ed0fb78fSIlya Dryomov 			mutex_unlock(&fs_info->balance_mutex);
3898ed0fb78fSIlya Dryomov 			if (!mutex_trylock(&fs_info->volume_mutex))
3899ed0fb78fSIlya Dryomov 				goto again;
3900ed0fb78fSIlya Dryomov 			mutex_lock(&fs_info->balance_mutex);
3901ed0fb78fSIlya Dryomov 
3902ed0fb78fSIlya Dryomov 			if (fs_info->balance_ctl &&
3903ed0fb78fSIlya Dryomov 			    !atomic_read(&fs_info->balance_running)) {
3904ed0fb78fSIlya Dryomov 				/* this is (3) */
3905ed0fb78fSIlya Dryomov 				need_unlock = false;
3906ed0fb78fSIlya Dryomov 				goto locked;
3907ed0fb78fSIlya Dryomov 			}
3908ed0fb78fSIlya Dryomov 
3909ed0fb78fSIlya Dryomov 			mutex_unlock(&fs_info->balance_mutex);
3910ed0fb78fSIlya Dryomov 			mutex_unlock(&fs_info->volume_mutex);
3911ed0fb78fSIlya Dryomov 			goto again;
3912ed0fb78fSIlya Dryomov 		} else {
3913ed0fb78fSIlya Dryomov 			/* this is (2) */
3914ed0fb78fSIlya Dryomov 			mutex_unlock(&fs_info->balance_mutex);
3915ed0fb78fSIlya Dryomov 			ret = -EINPROGRESS;
3916ed0fb78fSIlya Dryomov 			goto out;
3917ed0fb78fSIlya Dryomov 		}
3918ed0fb78fSIlya Dryomov 	} else {
3919ed0fb78fSIlya Dryomov 		/* this is (1) */
3920ed0fb78fSIlya Dryomov 		mutex_unlock(&fs_info->balance_mutex);
3921e57138b3SAnand Jain 		ret = BTRFS_ERROR_DEV_EXCL_RUN_IN_PROGRESS;
3922ed0fb78fSIlya Dryomov 		goto out;
3923ed0fb78fSIlya Dryomov 	}
3924ed0fb78fSIlya Dryomov 
3925ed0fb78fSIlya Dryomov locked:
3926ed0fb78fSIlya Dryomov 	BUG_ON(!atomic_read(&fs_info->mutually_exclusive_operation_running));
3927c9e9f97bSIlya Dryomov 
3928c9e9f97bSIlya Dryomov 	if (arg) {
3929c9e9f97bSIlya Dryomov 		bargs = memdup_user(arg, sizeof(*bargs));
3930c9e9f97bSIlya Dryomov 		if (IS_ERR(bargs)) {
3931c9e9f97bSIlya Dryomov 			ret = PTR_ERR(bargs);
3932ed0fb78fSIlya Dryomov 			goto out_unlock;
3933c9e9f97bSIlya Dryomov 		}
3934de322263SIlya Dryomov 
3935de322263SIlya Dryomov 		if (bargs->flags & BTRFS_BALANCE_RESUME) {
3936de322263SIlya Dryomov 			if (!fs_info->balance_ctl) {
3937de322263SIlya Dryomov 				ret = -ENOTCONN;
3938de322263SIlya Dryomov 				goto out_bargs;
3939de322263SIlya Dryomov 			}
3940de322263SIlya Dryomov 
3941de322263SIlya Dryomov 			bctl = fs_info->balance_ctl;
3942de322263SIlya Dryomov 			spin_lock(&fs_info->balance_lock);
3943de322263SIlya Dryomov 			bctl->flags |= BTRFS_BALANCE_RESUME;
3944de322263SIlya Dryomov 			spin_unlock(&fs_info->balance_lock);
3945de322263SIlya Dryomov 
3946de322263SIlya Dryomov 			goto do_balance;
3947de322263SIlya Dryomov 		}
3948c9e9f97bSIlya Dryomov 	} else {
3949c9e9f97bSIlya Dryomov 		bargs = NULL;
3950c9e9f97bSIlya Dryomov 	}
3951c9e9f97bSIlya Dryomov 
3952ed0fb78fSIlya Dryomov 	if (fs_info->balance_ctl) {
3953837d5b6eSIlya Dryomov 		ret = -EINPROGRESS;
3954837d5b6eSIlya Dryomov 		goto out_bargs;
3955837d5b6eSIlya Dryomov 	}
3956837d5b6eSIlya Dryomov 
3957c9e9f97bSIlya Dryomov 	bctl = kzalloc(sizeof(*bctl), GFP_NOFS);
3958c9e9f97bSIlya Dryomov 	if (!bctl) {
3959c9e9f97bSIlya Dryomov 		ret = -ENOMEM;
3960c9e9f97bSIlya Dryomov 		goto out_bargs;
3961c9e9f97bSIlya Dryomov 	}
3962c9e9f97bSIlya Dryomov 
3963c9e9f97bSIlya Dryomov 	bctl->fs_info = fs_info;
3964c9e9f97bSIlya Dryomov 	if (arg) {
3965c9e9f97bSIlya Dryomov 		memcpy(&bctl->data, &bargs->data, sizeof(bctl->data));
3966c9e9f97bSIlya Dryomov 		memcpy(&bctl->meta, &bargs->meta, sizeof(bctl->meta));
3967c9e9f97bSIlya Dryomov 		memcpy(&bctl->sys, &bargs->sys, sizeof(bctl->sys));
3968c9e9f97bSIlya Dryomov 
3969c9e9f97bSIlya Dryomov 		bctl->flags = bargs->flags;
3970f43ffb60SIlya Dryomov 	} else {
3971f43ffb60SIlya Dryomov 		/* balance everything - no filters */
3972f43ffb60SIlya Dryomov 		bctl->flags |= BTRFS_BALANCE_TYPE_MASK;
3973c9e9f97bSIlya Dryomov 	}
3974c9e9f97bSIlya Dryomov 
3975de322263SIlya Dryomov do_balance:
3976c9e9f97bSIlya Dryomov 	/*
3977ed0fb78fSIlya Dryomov 	 * Ownership of bctl and mutually_exclusive_operation_running
3978ed0fb78fSIlya Dryomov 	 * goes to to btrfs_balance.  bctl is freed in __cancel_balance,
3979ed0fb78fSIlya Dryomov 	 * or, if restriper was paused all the way until unmount, in
3980ed0fb78fSIlya Dryomov 	 * free_fs_info.  mutually_exclusive_operation_running is
3981ed0fb78fSIlya Dryomov 	 * cleared in __cancel_balance.
3982c9e9f97bSIlya Dryomov 	 */
3983ed0fb78fSIlya Dryomov 	need_unlock = false;
3984ed0fb78fSIlya Dryomov 
3985ed0fb78fSIlya Dryomov 	ret = btrfs_balance(bctl, bargs);
3986ed0fb78fSIlya Dryomov 
3987c9e9f97bSIlya Dryomov 	if (arg) {
3988c9e9f97bSIlya Dryomov 		if (copy_to_user(arg, bargs, sizeof(*bargs)))
3989c9e9f97bSIlya Dryomov 			ret = -EFAULT;
3990c9e9f97bSIlya Dryomov 	}
3991c9e9f97bSIlya Dryomov 
3992c9e9f97bSIlya Dryomov out_bargs:
3993c9e9f97bSIlya Dryomov 	kfree(bargs);
3994ed0fb78fSIlya Dryomov out_unlock:
3995c9e9f97bSIlya Dryomov 	mutex_unlock(&fs_info->balance_mutex);
3996c9e9f97bSIlya Dryomov 	mutex_unlock(&fs_info->volume_mutex);
3997ed0fb78fSIlya Dryomov 	if (need_unlock)
3998ed0fb78fSIlya Dryomov 		atomic_set(&fs_info->mutually_exclusive_operation_running, 0);
3999ed0fb78fSIlya Dryomov out:
4000e54bfa31SLiu Bo 	mnt_drop_write_file(file);
4001c9e9f97bSIlya Dryomov 	return ret;
4002c9e9f97bSIlya Dryomov }
4003c9e9f97bSIlya Dryomov 
4004837d5b6eSIlya Dryomov static long btrfs_ioctl_balance_ctl(struct btrfs_root *root, int cmd)
4005837d5b6eSIlya Dryomov {
4006837d5b6eSIlya Dryomov 	if (!capable(CAP_SYS_ADMIN))
4007837d5b6eSIlya Dryomov 		return -EPERM;
4008837d5b6eSIlya Dryomov 
4009837d5b6eSIlya Dryomov 	switch (cmd) {
4010837d5b6eSIlya Dryomov 	case BTRFS_BALANCE_CTL_PAUSE:
4011837d5b6eSIlya Dryomov 		return btrfs_pause_balance(root->fs_info);
4012a7e99c69SIlya Dryomov 	case BTRFS_BALANCE_CTL_CANCEL:
4013a7e99c69SIlya Dryomov 		return btrfs_cancel_balance(root->fs_info);
4014837d5b6eSIlya Dryomov 	}
4015837d5b6eSIlya Dryomov 
4016837d5b6eSIlya Dryomov 	return -EINVAL;
4017837d5b6eSIlya Dryomov }
4018837d5b6eSIlya Dryomov 
401919a39dceSIlya Dryomov static long btrfs_ioctl_balance_progress(struct btrfs_root *root,
402019a39dceSIlya Dryomov 					 void __user *arg)
402119a39dceSIlya Dryomov {
402219a39dceSIlya Dryomov 	struct btrfs_fs_info *fs_info = root->fs_info;
402319a39dceSIlya Dryomov 	struct btrfs_ioctl_balance_args *bargs;
402419a39dceSIlya Dryomov 	int ret = 0;
402519a39dceSIlya Dryomov 
402619a39dceSIlya Dryomov 	if (!capable(CAP_SYS_ADMIN))
402719a39dceSIlya Dryomov 		return -EPERM;
402819a39dceSIlya Dryomov 
402919a39dceSIlya Dryomov 	mutex_lock(&fs_info->balance_mutex);
403019a39dceSIlya Dryomov 	if (!fs_info->balance_ctl) {
403119a39dceSIlya Dryomov 		ret = -ENOTCONN;
403219a39dceSIlya Dryomov 		goto out;
403319a39dceSIlya Dryomov 	}
403419a39dceSIlya Dryomov 
403519a39dceSIlya Dryomov 	bargs = kzalloc(sizeof(*bargs), GFP_NOFS);
403619a39dceSIlya Dryomov 	if (!bargs) {
403719a39dceSIlya Dryomov 		ret = -ENOMEM;
403819a39dceSIlya Dryomov 		goto out;
403919a39dceSIlya Dryomov 	}
404019a39dceSIlya Dryomov 
404119a39dceSIlya Dryomov 	update_ioctl_balance_args(fs_info, 1, bargs);
404219a39dceSIlya Dryomov 
404319a39dceSIlya Dryomov 	if (copy_to_user(arg, bargs, sizeof(*bargs)))
404419a39dceSIlya Dryomov 		ret = -EFAULT;
404519a39dceSIlya Dryomov 
404619a39dceSIlya Dryomov 	kfree(bargs);
404719a39dceSIlya Dryomov out:
404819a39dceSIlya Dryomov 	mutex_unlock(&fs_info->balance_mutex);
404919a39dceSIlya Dryomov 	return ret;
405019a39dceSIlya Dryomov }
405119a39dceSIlya Dryomov 
4052905b0ddaSMiao Xie static long btrfs_ioctl_quota_ctl(struct file *file, void __user *arg)
40535d13a37bSArne Jansen {
4054496ad9aaSAl Viro 	struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
40555d13a37bSArne Jansen 	struct btrfs_ioctl_quota_ctl_args *sa;
40565d13a37bSArne Jansen 	struct btrfs_trans_handle *trans = NULL;
40575d13a37bSArne Jansen 	int ret;
40585d13a37bSArne Jansen 	int err;
40595d13a37bSArne Jansen 
40605d13a37bSArne Jansen 	if (!capable(CAP_SYS_ADMIN))
40615d13a37bSArne Jansen 		return -EPERM;
40625d13a37bSArne Jansen 
4063905b0ddaSMiao Xie 	ret = mnt_want_write_file(file);
4064905b0ddaSMiao Xie 	if (ret)
4065905b0ddaSMiao Xie 		return ret;
40665d13a37bSArne Jansen 
40675d13a37bSArne Jansen 	sa = memdup_user(arg, sizeof(*sa));
4068905b0ddaSMiao Xie 	if (IS_ERR(sa)) {
4069905b0ddaSMiao Xie 		ret = PTR_ERR(sa);
4070905b0ddaSMiao Xie 		goto drop_write;
4071905b0ddaSMiao Xie 	}
40725d13a37bSArne Jansen 
40737708f029SWang Shilong 	down_write(&root->fs_info->subvol_sem);
407492f183aaSWang Shilong 	trans = btrfs_start_transaction(root->fs_info->tree_root, 2);
40755d13a37bSArne Jansen 	if (IS_ERR(trans)) {
40765d13a37bSArne Jansen 		ret = PTR_ERR(trans);
40775d13a37bSArne Jansen 		goto out;
40785d13a37bSArne Jansen 	}
40795d13a37bSArne Jansen 
40805d13a37bSArne Jansen 	switch (sa->cmd) {
40815d13a37bSArne Jansen 	case BTRFS_QUOTA_CTL_ENABLE:
40825d13a37bSArne Jansen 		ret = btrfs_quota_enable(trans, root->fs_info);
40835d13a37bSArne Jansen 		break;
40845d13a37bSArne Jansen 	case BTRFS_QUOTA_CTL_DISABLE:
40855d13a37bSArne Jansen 		ret = btrfs_quota_disable(trans, root->fs_info);
40865d13a37bSArne Jansen 		break;
40875d13a37bSArne Jansen 	default:
40885d13a37bSArne Jansen 		ret = -EINVAL;
40895d13a37bSArne Jansen 		break;
40905d13a37bSArne Jansen 	}
40915d13a37bSArne Jansen 
409292f183aaSWang Shilong 	err = btrfs_commit_transaction(trans, root->fs_info->tree_root);
40935d13a37bSArne Jansen 	if (err && !ret)
40945d13a37bSArne Jansen 		ret = err;
40955d13a37bSArne Jansen out:
40965d13a37bSArne Jansen 	kfree(sa);
40977708f029SWang Shilong 	up_write(&root->fs_info->subvol_sem);
4098905b0ddaSMiao Xie drop_write:
4099905b0ddaSMiao Xie 	mnt_drop_write_file(file);
41005d13a37bSArne Jansen 	return ret;
41015d13a37bSArne Jansen }
41025d13a37bSArne Jansen 
4103905b0ddaSMiao Xie static long btrfs_ioctl_qgroup_assign(struct file *file, void __user *arg)
41045d13a37bSArne Jansen {
4105496ad9aaSAl Viro 	struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
41065d13a37bSArne Jansen 	struct btrfs_ioctl_qgroup_assign_args *sa;
41075d13a37bSArne Jansen 	struct btrfs_trans_handle *trans;
41085d13a37bSArne Jansen 	int ret;
41095d13a37bSArne Jansen 	int err;
41105d13a37bSArne Jansen 
41115d13a37bSArne Jansen 	if (!capable(CAP_SYS_ADMIN))
41125d13a37bSArne Jansen 		return -EPERM;
41135d13a37bSArne Jansen 
4114905b0ddaSMiao Xie 	ret = mnt_want_write_file(file);
4115905b0ddaSMiao Xie 	if (ret)
4116905b0ddaSMiao Xie 		return ret;
41175d13a37bSArne Jansen 
41185d13a37bSArne Jansen 	sa = memdup_user(arg, sizeof(*sa));
4119905b0ddaSMiao Xie 	if (IS_ERR(sa)) {
4120905b0ddaSMiao Xie 		ret = PTR_ERR(sa);
4121905b0ddaSMiao Xie 		goto drop_write;
4122905b0ddaSMiao Xie 	}
41235d13a37bSArne Jansen 
41245d13a37bSArne Jansen 	trans = btrfs_join_transaction(root);
41255d13a37bSArne Jansen 	if (IS_ERR(trans)) {
41265d13a37bSArne Jansen 		ret = PTR_ERR(trans);
41275d13a37bSArne Jansen 		goto out;
41285d13a37bSArne Jansen 	}
41295d13a37bSArne Jansen 
41305d13a37bSArne Jansen 	/* FIXME: check if the IDs really exist */
41315d13a37bSArne Jansen 	if (sa->assign) {
41325d13a37bSArne Jansen 		ret = btrfs_add_qgroup_relation(trans, root->fs_info,
41335d13a37bSArne Jansen 						sa->src, sa->dst);
41345d13a37bSArne Jansen 	} else {
41355d13a37bSArne Jansen 		ret = btrfs_del_qgroup_relation(trans, root->fs_info,
41365d13a37bSArne Jansen 						sa->src, sa->dst);
41375d13a37bSArne Jansen 	}
41385d13a37bSArne Jansen 
41395d13a37bSArne Jansen 	err = btrfs_end_transaction(trans, root);
41405d13a37bSArne Jansen 	if (err && !ret)
41415d13a37bSArne Jansen 		ret = err;
41425d13a37bSArne Jansen 
41435d13a37bSArne Jansen out:
41445d13a37bSArne Jansen 	kfree(sa);
4145905b0ddaSMiao Xie drop_write:
4146905b0ddaSMiao Xie 	mnt_drop_write_file(file);
41475d13a37bSArne Jansen 	return ret;
41485d13a37bSArne Jansen }
41495d13a37bSArne Jansen 
4150905b0ddaSMiao Xie static long btrfs_ioctl_qgroup_create(struct file *file, void __user *arg)
41515d13a37bSArne Jansen {
4152496ad9aaSAl Viro 	struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
41535d13a37bSArne Jansen 	struct btrfs_ioctl_qgroup_create_args *sa;
41545d13a37bSArne Jansen 	struct btrfs_trans_handle *trans;
41555d13a37bSArne Jansen 	int ret;
41565d13a37bSArne Jansen 	int err;
41575d13a37bSArne Jansen 
41585d13a37bSArne Jansen 	if (!capable(CAP_SYS_ADMIN))
41595d13a37bSArne Jansen 		return -EPERM;
41605d13a37bSArne Jansen 
4161905b0ddaSMiao Xie 	ret = mnt_want_write_file(file);
4162905b0ddaSMiao Xie 	if (ret)
4163905b0ddaSMiao Xie 		return ret;
41645d13a37bSArne Jansen 
41655d13a37bSArne Jansen 	sa = memdup_user(arg, sizeof(*sa));
4166905b0ddaSMiao Xie 	if (IS_ERR(sa)) {
4167905b0ddaSMiao Xie 		ret = PTR_ERR(sa);
4168905b0ddaSMiao Xie 		goto drop_write;
4169905b0ddaSMiao Xie 	}
41705d13a37bSArne Jansen 
4171d86e56cfSMiao Xie 	if (!sa->qgroupid) {
4172d86e56cfSMiao Xie 		ret = -EINVAL;
4173d86e56cfSMiao Xie 		goto out;
4174d86e56cfSMiao Xie 	}
4175d86e56cfSMiao Xie 
41765d13a37bSArne Jansen 	trans = btrfs_join_transaction(root);
41775d13a37bSArne Jansen 	if (IS_ERR(trans)) {
41785d13a37bSArne Jansen 		ret = PTR_ERR(trans);
41795d13a37bSArne Jansen 		goto out;
41805d13a37bSArne Jansen 	}
41815d13a37bSArne Jansen 
41825d13a37bSArne Jansen 	/* FIXME: check if the IDs really exist */
41835d13a37bSArne Jansen 	if (sa->create) {
41845d13a37bSArne Jansen 		ret = btrfs_create_qgroup(trans, root->fs_info, sa->qgroupid,
41855d13a37bSArne Jansen 					  NULL);
41865d13a37bSArne Jansen 	} else {
41875d13a37bSArne Jansen 		ret = btrfs_remove_qgroup(trans, root->fs_info, sa->qgroupid);
41885d13a37bSArne Jansen 	}
41895d13a37bSArne Jansen 
41905d13a37bSArne Jansen 	err = btrfs_end_transaction(trans, root);
41915d13a37bSArne Jansen 	if (err && !ret)
41925d13a37bSArne Jansen 		ret = err;
41935d13a37bSArne Jansen 
41945d13a37bSArne Jansen out:
41955d13a37bSArne Jansen 	kfree(sa);
4196905b0ddaSMiao Xie drop_write:
4197905b0ddaSMiao Xie 	mnt_drop_write_file(file);
41985d13a37bSArne Jansen 	return ret;
41995d13a37bSArne Jansen }
42005d13a37bSArne Jansen 
4201905b0ddaSMiao Xie static long btrfs_ioctl_qgroup_limit(struct file *file, void __user *arg)
42025d13a37bSArne Jansen {
4203496ad9aaSAl Viro 	struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
42045d13a37bSArne Jansen 	struct btrfs_ioctl_qgroup_limit_args *sa;
42055d13a37bSArne Jansen 	struct btrfs_trans_handle *trans;
42065d13a37bSArne Jansen 	int ret;
42075d13a37bSArne Jansen 	int err;
42085d13a37bSArne Jansen 	u64 qgroupid;
42095d13a37bSArne Jansen 
42105d13a37bSArne Jansen 	if (!capable(CAP_SYS_ADMIN))
42115d13a37bSArne Jansen 		return -EPERM;
42125d13a37bSArne Jansen 
4213905b0ddaSMiao Xie 	ret = mnt_want_write_file(file);
4214905b0ddaSMiao Xie 	if (ret)
4215905b0ddaSMiao Xie 		return ret;
42165d13a37bSArne Jansen 
42175d13a37bSArne Jansen 	sa = memdup_user(arg, sizeof(*sa));
4218905b0ddaSMiao Xie 	if (IS_ERR(sa)) {
4219905b0ddaSMiao Xie 		ret = PTR_ERR(sa);
4220905b0ddaSMiao Xie 		goto drop_write;
4221905b0ddaSMiao Xie 	}
42225d13a37bSArne Jansen 
42235d13a37bSArne Jansen 	trans = btrfs_join_transaction(root);
42245d13a37bSArne Jansen 	if (IS_ERR(trans)) {
42255d13a37bSArne Jansen 		ret = PTR_ERR(trans);
42265d13a37bSArne Jansen 		goto out;
42275d13a37bSArne Jansen 	}
42285d13a37bSArne Jansen 
42295d13a37bSArne Jansen 	qgroupid = sa->qgroupid;
42305d13a37bSArne Jansen 	if (!qgroupid) {
42315d13a37bSArne Jansen 		/* take the current subvol as qgroup */
42325d13a37bSArne Jansen 		qgroupid = root->root_key.objectid;
42335d13a37bSArne Jansen 	}
42345d13a37bSArne Jansen 
42355d13a37bSArne Jansen 	/* FIXME: check if the IDs really exist */
42365d13a37bSArne Jansen 	ret = btrfs_limit_qgroup(trans, root->fs_info, qgroupid, &sa->lim);
42375d13a37bSArne Jansen 
42385d13a37bSArne Jansen 	err = btrfs_end_transaction(trans, root);
42395d13a37bSArne Jansen 	if (err && !ret)
42405d13a37bSArne Jansen 		ret = err;
42415d13a37bSArne Jansen 
42425d13a37bSArne Jansen out:
42435d13a37bSArne Jansen 	kfree(sa);
4244905b0ddaSMiao Xie drop_write:
4245905b0ddaSMiao Xie 	mnt_drop_write_file(file);
42465d13a37bSArne Jansen 	return ret;
42475d13a37bSArne Jansen }
42485d13a37bSArne Jansen 
42492f232036SJan Schmidt static long btrfs_ioctl_quota_rescan(struct file *file, void __user *arg)
42502f232036SJan Schmidt {
42516d0379ecSAl Viro 	struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
42522f232036SJan Schmidt 	struct btrfs_ioctl_quota_rescan_args *qsa;
42532f232036SJan Schmidt 	int ret;
42542f232036SJan Schmidt 
42552f232036SJan Schmidt 	if (!capable(CAP_SYS_ADMIN))
42562f232036SJan Schmidt 		return -EPERM;
42572f232036SJan Schmidt 
42582f232036SJan Schmidt 	ret = mnt_want_write_file(file);
42592f232036SJan Schmidt 	if (ret)
42602f232036SJan Schmidt 		return ret;
42612f232036SJan Schmidt 
42622f232036SJan Schmidt 	qsa = memdup_user(arg, sizeof(*qsa));
42632f232036SJan Schmidt 	if (IS_ERR(qsa)) {
42642f232036SJan Schmidt 		ret = PTR_ERR(qsa);
42652f232036SJan Schmidt 		goto drop_write;
42662f232036SJan Schmidt 	}
42672f232036SJan Schmidt 
42682f232036SJan Schmidt 	if (qsa->flags) {
42692f232036SJan Schmidt 		ret = -EINVAL;
42702f232036SJan Schmidt 		goto out;
42712f232036SJan Schmidt 	}
42722f232036SJan Schmidt 
42732f232036SJan Schmidt 	ret = btrfs_qgroup_rescan(root->fs_info);
42742f232036SJan Schmidt 
42752f232036SJan Schmidt out:
42762f232036SJan Schmidt 	kfree(qsa);
42772f232036SJan Schmidt drop_write:
42782f232036SJan Schmidt 	mnt_drop_write_file(file);
42792f232036SJan Schmidt 	return ret;
42802f232036SJan Schmidt }
42812f232036SJan Schmidt 
42822f232036SJan Schmidt static long btrfs_ioctl_quota_rescan_status(struct file *file, void __user *arg)
42832f232036SJan Schmidt {
42846d0379ecSAl Viro 	struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
42852f232036SJan Schmidt 	struct btrfs_ioctl_quota_rescan_args *qsa;
42862f232036SJan Schmidt 	int ret = 0;
42872f232036SJan Schmidt 
42882f232036SJan Schmidt 	if (!capable(CAP_SYS_ADMIN))
42892f232036SJan Schmidt 		return -EPERM;
42902f232036SJan Schmidt 
42912f232036SJan Schmidt 	qsa = kzalloc(sizeof(*qsa), GFP_NOFS);
42922f232036SJan Schmidt 	if (!qsa)
42932f232036SJan Schmidt 		return -ENOMEM;
42942f232036SJan Schmidt 
42952f232036SJan Schmidt 	if (root->fs_info->qgroup_flags & BTRFS_QGROUP_STATUS_FLAG_RESCAN) {
42962f232036SJan Schmidt 		qsa->flags = 1;
42972f232036SJan Schmidt 		qsa->progress = root->fs_info->qgroup_rescan_progress.objectid;
42982f232036SJan Schmidt 	}
42992f232036SJan Schmidt 
43002f232036SJan Schmidt 	if (copy_to_user(arg, qsa, sizeof(*qsa)))
43012f232036SJan Schmidt 		ret = -EFAULT;
43022f232036SJan Schmidt 
43032f232036SJan Schmidt 	kfree(qsa);
43042f232036SJan Schmidt 	return ret;
43052f232036SJan Schmidt }
43062f232036SJan Schmidt 
430757254b6eSJan Schmidt static long btrfs_ioctl_quota_rescan_wait(struct file *file, void __user *arg)
430857254b6eSJan Schmidt {
430954563d41SAl Viro 	struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
431057254b6eSJan Schmidt 
431157254b6eSJan Schmidt 	if (!capable(CAP_SYS_ADMIN))
431257254b6eSJan Schmidt 		return -EPERM;
431357254b6eSJan Schmidt 
431457254b6eSJan Schmidt 	return btrfs_qgroup_wait_for_completion(root->fs_info);
431557254b6eSJan Schmidt }
431657254b6eSJan Schmidt 
43178ea05e3aSAlexander Block static long btrfs_ioctl_set_received_subvol(struct file *file,
43188ea05e3aSAlexander Block 					    void __user *arg)
43198ea05e3aSAlexander Block {
43208ea05e3aSAlexander Block 	struct btrfs_ioctl_received_subvol_args *sa = NULL;
4321496ad9aaSAl Viro 	struct inode *inode = file_inode(file);
43228ea05e3aSAlexander Block 	struct btrfs_root *root = BTRFS_I(inode)->root;
43238ea05e3aSAlexander Block 	struct btrfs_root_item *root_item = &root->root_item;
43248ea05e3aSAlexander Block 	struct btrfs_trans_handle *trans;
43258ea05e3aSAlexander Block 	struct timespec ct = CURRENT_TIME;
43268ea05e3aSAlexander Block 	int ret = 0;
4327dd5f9615SStefan Behrens 	int received_uuid_changed;
43288ea05e3aSAlexander Block 
43298ea05e3aSAlexander Block 	ret = mnt_want_write_file(file);
43308ea05e3aSAlexander Block 	if (ret < 0)
43318ea05e3aSAlexander Block 		return ret;
43328ea05e3aSAlexander Block 
43338ea05e3aSAlexander Block 	down_write(&root->fs_info->subvol_sem);
43348ea05e3aSAlexander Block 
43358ea05e3aSAlexander Block 	if (btrfs_ino(inode) != BTRFS_FIRST_FREE_OBJECTID) {
43368ea05e3aSAlexander Block 		ret = -EINVAL;
43378ea05e3aSAlexander Block 		goto out;
43388ea05e3aSAlexander Block 	}
43398ea05e3aSAlexander Block 
43408ea05e3aSAlexander Block 	if (btrfs_root_readonly(root)) {
43418ea05e3aSAlexander Block 		ret = -EROFS;
43428ea05e3aSAlexander Block 		goto out;
43438ea05e3aSAlexander Block 	}
43448ea05e3aSAlexander Block 
43458ea05e3aSAlexander Block 	if (!inode_owner_or_capable(inode)) {
43468ea05e3aSAlexander Block 		ret = -EACCES;
43478ea05e3aSAlexander Block 		goto out;
43488ea05e3aSAlexander Block 	}
43498ea05e3aSAlexander Block 
43508ea05e3aSAlexander Block 	sa = memdup_user(arg, sizeof(*sa));
43518ea05e3aSAlexander Block 	if (IS_ERR(sa)) {
43528ea05e3aSAlexander Block 		ret = PTR_ERR(sa);
43538ea05e3aSAlexander Block 		sa = NULL;
43548ea05e3aSAlexander Block 		goto out;
43558ea05e3aSAlexander Block 	}
43568ea05e3aSAlexander Block 
4357dd5f9615SStefan Behrens 	/*
4358dd5f9615SStefan Behrens 	 * 1 - root item
4359dd5f9615SStefan Behrens 	 * 2 - uuid items (received uuid + subvol uuid)
4360dd5f9615SStefan Behrens 	 */
4361dd5f9615SStefan Behrens 	trans = btrfs_start_transaction(root, 3);
43628ea05e3aSAlexander Block 	if (IS_ERR(trans)) {
43638ea05e3aSAlexander Block 		ret = PTR_ERR(trans);
43648ea05e3aSAlexander Block 		trans = NULL;
43658ea05e3aSAlexander Block 		goto out;
43668ea05e3aSAlexander Block 	}
43678ea05e3aSAlexander Block 
43688ea05e3aSAlexander Block 	sa->rtransid = trans->transid;
43698ea05e3aSAlexander Block 	sa->rtime.sec = ct.tv_sec;
43708ea05e3aSAlexander Block 	sa->rtime.nsec = ct.tv_nsec;
43718ea05e3aSAlexander Block 
4372dd5f9615SStefan Behrens 	received_uuid_changed = memcmp(root_item->received_uuid, sa->uuid,
4373dd5f9615SStefan Behrens 				       BTRFS_UUID_SIZE);
4374dd5f9615SStefan Behrens 	if (received_uuid_changed &&
4375dd5f9615SStefan Behrens 	    !btrfs_is_empty_uuid(root_item->received_uuid))
4376dd5f9615SStefan Behrens 		btrfs_uuid_tree_rem(trans, root->fs_info->uuid_root,
4377dd5f9615SStefan Behrens 				    root_item->received_uuid,
4378dd5f9615SStefan Behrens 				    BTRFS_UUID_KEY_RECEIVED_SUBVOL,
4379dd5f9615SStefan Behrens 				    root->root_key.objectid);
43808ea05e3aSAlexander Block 	memcpy(root_item->received_uuid, sa->uuid, BTRFS_UUID_SIZE);
43818ea05e3aSAlexander Block 	btrfs_set_root_stransid(root_item, sa->stransid);
43828ea05e3aSAlexander Block 	btrfs_set_root_rtransid(root_item, sa->rtransid);
43833cae210fSQu Wenruo 	btrfs_set_stack_timespec_sec(&root_item->stime, sa->stime.sec);
43843cae210fSQu Wenruo 	btrfs_set_stack_timespec_nsec(&root_item->stime, sa->stime.nsec);
43853cae210fSQu Wenruo 	btrfs_set_stack_timespec_sec(&root_item->rtime, sa->rtime.sec);
43863cae210fSQu Wenruo 	btrfs_set_stack_timespec_nsec(&root_item->rtime, sa->rtime.nsec);
43878ea05e3aSAlexander Block 
43888ea05e3aSAlexander Block 	ret = btrfs_update_root(trans, root->fs_info->tree_root,
43898ea05e3aSAlexander Block 				&root->root_key, &root->root_item);
43908ea05e3aSAlexander Block 	if (ret < 0) {
43918ea05e3aSAlexander Block 		btrfs_end_transaction(trans, root);
43928ea05e3aSAlexander Block 		goto out;
4393dd5f9615SStefan Behrens 	}
4394dd5f9615SStefan Behrens 	if (received_uuid_changed && !btrfs_is_empty_uuid(sa->uuid)) {
4395dd5f9615SStefan Behrens 		ret = btrfs_uuid_tree_add(trans, root->fs_info->uuid_root,
4396dd5f9615SStefan Behrens 					  sa->uuid,
4397dd5f9615SStefan Behrens 					  BTRFS_UUID_KEY_RECEIVED_SUBVOL,
4398dd5f9615SStefan Behrens 					  root->root_key.objectid);
4399dd5f9615SStefan Behrens 		if (ret < 0 && ret != -EEXIST) {
4400dd5f9615SStefan Behrens 			btrfs_abort_transaction(trans, root, ret);
4401dd5f9615SStefan Behrens 			goto out;
4402dd5f9615SStefan Behrens 		}
4403dd5f9615SStefan Behrens 	}
44048ea05e3aSAlexander Block 	ret = btrfs_commit_transaction(trans, root);
4405dd5f9615SStefan Behrens 	if (ret < 0) {
4406dd5f9615SStefan Behrens 		btrfs_abort_transaction(trans, root, ret);
44078ea05e3aSAlexander Block 		goto out;
44088ea05e3aSAlexander Block 	}
44098ea05e3aSAlexander Block 
44108ea05e3aSAlexander Block 	ret = copy_to_user(arg, sa, sizeof(*sa));
44118ea05e3aSAlexander Block 	if (ret)
44128ea05e3aSAlexander Block 		ret = -EFAULT;
44138ea05e3aSAlexander Block 
44148ea05e3aSAlexander Block out:
44158ea05e3aSAlexander Block 	kfree(sa);
44168ea05e3aSAlexander Block 	up_write(&root->fs_info->subvol_sem);
44178ea05e3aSAlexander Block 	mnt_drop_write_file(file);
44188ea05e3aSAlexander Block 	return ret;
44198ea05e3aSAlexander Block }
44208ea05e3aSAlexander Block 
4421867ab667Sjeff.liu static int btrfs_ioctl_get_fslabel(struct file *file, void __user *arg)
4422867ab667Sjeff.liu {
44236d0379ecSAl Viro 	struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
4424a1b83ac5SAnand Jain 	size_t len;
4425867ab667Sjeff.liu 	int ret;
4426a1b83ac5SAnand Jain 	char label[BTRFS_LABEL_SIZE];
4427a1b83ac5SAnand Jain 
4428a1b83ac5SAnand Jain 	spin_lock(&root->fs_info->super_lock);
4429a1b83ac5SAnand Jain 	memcpy(label, root->fs_info->super_copy->label, BTRFS_LABEL_SIZE);
4430a1b83ac5SAnand Jain 	spin_unlock(&root->fs_info->super_lock);
4431a1b83ac5SAnand Jain 
4432a1b83ac5SAnand Jain 	len = strnlen(label, BTRFS_LABEL_SIZE);
4433867ab667Sjeff.liu 
4434867ab667Sjeff.liu 	if (len == BTRFS_LABEL_SIZE) {
4435867ab667Sjeff.liu 		pr_warn("btrfs: label is too long, return the first %zu bytes\n",
4436867ab667Sjeff.liu 			--len);
4437867ab667Sjeff.liu 	}
4438867ab667Sjeff.liu 
4439867ab667Sjeff.liu 	ret = copy_to_user(arg, label, len);
4440867ab667Sjeff.liu 
4441867ab667Sjeff.liu 	return ret ? -EFAULT : 0;
4442867ab667Sjeff.liu }
4443867ab667Sjeff.liu 
4444a8bfd4abSjeff.liu static int btrfs_ioctl_set_fslabel(struct file *file, void __user *arg)
4445a8bfd4abSjeff.liu {
44466d0379ecSAl Viro 	struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
4447a8bfd4abSjeff.liu 	struct btrfs_super_block *super_block = root->fs_info->super_copy;
4448a8bfd4abSjeff.liu 	struct btrfs_trans_handle *trans;
4449a8bfd4abSjeff.liu 	char label[BTRFS_LABEL_SIZE];
4450a8bfd4abSjeff.liu 	int ret;
4451a8bfd4abSjeff.liu 
4452a8bfd4abSjeff.liu 	if (!capable(CAP_SYS_ADMIN))
4453a8bfd4abSjeff.liu 		return -EPERM;
4454a8bfd4abSjeff.liu 
4455a8bfd4abSjeff.liu 	if (copy_from_user(label, arg, sizeof(label)))
4456a8bfd4abSjeff.liu 		return -EFAULT;
4457a8bfd4abSjeff.liu 
4458a8bfd4abSjeff.liu 	if (strnlen(label, BTRFS_LABEL_SIZE) == BTRFS_LABEL_SIZE) {
4459a8bfd4abSjeff.liu 		pr_err("btrfs: unable to set label with more than %d bytes\n",
4460a8bfd4abSjeff.liu 		       BTRFS_LABEL_SIZE - 1);
4461a8bfd4abSjeff.liu 		return -EINVAL;
4462a8bfd4abSjeff.liu 	}
4463a8bfd4abSjeff.liu 
4464a8bfd4abSjeff.liu 	ret = mnt_want_write_file(file);
4465a8bfd4abSjeff.liu 	if (ret)
4466a8bfd4abSjeff.liu 		return ret;
4467a8bfd4abSjeff.liu 
4468a8bfd4abSjeff.liu 	trans = btrfs_start_transaction(root, 0);
4469a8bfd4abSjeff.liu 	if (IS_ERR(trans)) {
4470a8bfd4abSjeff.liu 		ret = PTR_ERR(trans);
4471a8bfd4abSjeff.liu 		goto out_unlock;
4472a8bfd4abSjeff.liu 	}
4473a8bfd4abSjeff.liu 
4474a1b83ac5SAnand Jain 	spin_lock(&root->fs_info->super_lock);
4475a8bfd4abSjeff.liu 	strcpy(super_block->label, label);
4476a1b83ac5SAnand Jain 	spin_unlock(&root->fs_info->super_lock);
4477a8bfd4abSjeff.liu 	ret = btrfs_end_transaction(trans, root);
4478a8bfd4abSjeff.liu 
4479a8bfd4abSjeff.liu out_unlock:
4480a8bfd4abSjeff.liu 	mnt_drop_write_file(file);
4481a8bfd4abSjeff.liu 	return ret;
4482a8bfd4abSjeff.liu }
4483a8bfd4abSjeff.liu 
44842eaa055fSJeff Mahoney #define INIT_FEATURE_FLAGS(suffix) \
44852eaa055fSJeff Mahoney 	{ .compat_flags = BTRFS_FEATURE_COMPAT_##suffix, \
44862eaa055fSJeff Mahoney 	  .compat_ro_flags = BTRFS_FEATURE_COMPAT_RO_##suffix, \
44872eaa055fSJeff Mahoney 	  .incompat_flags = BTRFS_FEATURE_INCOMPAT_##suffix }
44882eaa055fSJeff Mahoney 
44892eaa055fSJeff Mahoney static int btrfs_ioctl_get_supported_features(struct file *file,
44902eaa055fSJeff Mahoney 					      void __user *arg)
44912eaa055fSJeff Mahoney {
44922eaa055fSJeff Mahoney 	static struct btrfs_ioctl_feature_flags features[3] = {
44932eaa055fSJeff Mahoney 		INIT_FEATURE_FLAGS(SUPP),
44942eaa055fSJeff Mahoney 		INIT_FEATURE_FLAGS(SAFE_SET),
44952eaa055fSJeff Mahoney 		INIT_FEATURE_FLAGS(SAFE_CLEAR)
44962eaa055fSJeff Mahoney 	};
44972eaa055fSJeff Mahoney 
44982eaa055fSJeff Mahoney 	if (copy_to_user(arg, &features, sizeof(features)))
44992eaa055fSJeff Mahoney 		return -EFAULT;
45002eaa055fSJeff Mahoney 
45012eaa055fSJeff Mahoney 	return 0;
45022eaa055fSJeff Mahoney }
45032eaa055fSJeff Mahoney 
45042eaa055fSJeff Mahoney static int btrfs_ioctl_get_features(struct file *file, void __user *arg)
45052eaa055fSJeff Mahoney {
45062eaa055fSJeff Mahoney 	struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
45072eaa055fSJeff Mahoney 	struct btrfs_super_block *super_block = root->fs_info->super_copy;
45082eaa055fSJeff Mahoney 	struct btrfs_ioctl_feature_flags features;
45092eaa055fSJeff Mahoney 
45102eaa055fSJeff Mahoney 	features.compat_flags = btrfs_super_compat_flags(super_block);
45112eaa055fSJeff Mahoney 	features.compat_ro_flags = btrfs_super_compat_ro_flags(super_block);
45122eaa055fSJeff Mahoney 	features.incompat_flags = btrfs_super_incompat_flags(super_block);
45132eaa055fSJeff Mahoney 
45142eaa055fSJeff Mahoney 	if (copy_to_user(arg, &features, sizeof(features)))
45152eaa055fSJeff Mahoney 		return -EFAULT;
45162eaa055fSJeff Mahoney 
45172eaa055fSJeff Mahoney 	return 0;
45182eaa055fSJeff Mahoney }
45192eaa055fSJeff Mahoney 
45203b02a68aSJeff Mahoney static int check_feature_bits(struct btrfs_root *root,
45213b02a68aSJeff Mahoney 			      enum btrfs_feature_set set,
45222eaa055fSJeff Mahoney 			      u64 change_mask, u64 flags, u64 supported_flags,
45232eaa055fSJeff Mahoney 			      u64 safe_set, u64 safe_clear)
45242eaa055fSJeff Mahoney {
45253b02a68aSJeff Mahoney 	const char *type = btrfs_feature_set_names[set];
45263b02a68aSJeff Mahoney 	char *names;
45272eaa055fSJeff Mahoney 	u64 disallowed, unsupported;
45282eaa055fSJeff Mahoney 	u64 set_mask = flags & change_mask;
45292eaa055fSJeff Mahoney 	u64 clear_mask = ~flags & change_mask;
45302eaa055fSJeff Mahoney 
45312eaa055fSJeff Mahoney 	unsupported = set_mask & ~supported_flags;
45322eaa055fSJeff Mahoney 	if (unsupported) {
45333b02a68aSJeff Mahoney 		names = btrfs_printable_features(set, unsupported);
45343b02a68aSJeff Mahoney 		if (names) {
45353b02a68aSJeff Mahoney 			btrfs_warn(root->fs_info,
45363b02a68aSJeff Mahoney 			   "this kernel does not support the %s feature bit%s",
45373b02a68aSJeff Mahoney 			   names, strchr(names, ',') ? "s" : "");
45383b02a68aSJeff Mahoney 			kfree(names);
45393b02a68aSJeff Mahoney 		} else
45402eaa055fSJeff Mahoney 			btrfs_warn(root->fs_info,
45412eaa055fSJeff Mahoney 			   "this kernel does not support %s bits 0x%llx",
45422eaa055fSJeff Mahoney 			   type, unsupported);
45432eaa055fSJeff Mahoney 		return -EOPNOTSUPP;
45442eaa055fSJeff Mahoney 	}
45452eaa055fSJeff Mahoney 
45462eaa055fSJeff Mahoney 	disallowed = set_mask & ~safe_set;
45472eaa055fSJeff Mahoney 	if (disallowed) {
45483b02a68aSJeff Mahoney 		names = btrfs_printable_features(set, disallowed);
45493b02a68aSJeff Mahoney 		if (names) {
45503b02a68aSJeff Mahoney 			btrfs_warn(root->fs_info,
45513b02a68aSJeff Mahoney 			   "can't set the %s feature bit%s while mounted",
45523b02a68aSJeff Mahoney 			   names, strchr(names, ',') ? "s" : "");
45533b02a68aSJeff Mahoney 			kfree(names);
45543b02a68aSJeff Mahoney 		} else
45552eaa055fSJeff Mahoney 			btrfs_warn(root->fs_info,
45562eaa055fSJeff Mahoney 			   "can't set %s bits 0x%llx while mounted",
45572eaa055fSJeff Mahoney 			   type, disallowed);
45582eaa055fSJeff Mahoney 		return -EPERM;
45592eaa055fSJeff Mahoney 	}
45602eaa055fSJeff Mahoney 
45612eaa055fSJeff Mahoney 	disallowed = clear_mask & ~safe_clear;
45622eaa055fSJeff Mahoney 	if (disallowed) {
45633b02a68aSJeff Mahoney 		names = btrfs_printable_features(set, disallowed);
45643b02a68aSJeff Mahoney 		if (names) {
45653b02a68aSJeff Mahoney 			btrfs_warn(root->fs_info,
45663b02a68aSJeff Mahoney 			   "can't clear the %s feature bit%s while mounted",
45673b02a68aSJeff Mahoney 			   names, strchr(names, ',') ? "s" : "");
45683b02a68aSJeff Mahoney 			kfree(names);
45693b02a68aSJeff Mahoney 		} else
45702eaa055fSJeff Mahoney 			btrfs_warn(root->fs_info,
45712eaa055fSJeff Mahoney 			   "can't clear %s bits 0x%llx while mounted",
45722eaa055fSJeff Mahoney 			   type, disallowed);
45732eaa055fSJeff Mahoney 		return -EPERM;
45742eaa055fSJeff Mahoney 	}
45752eaa055fSJeff Mahoney 
45762eaa055fSJeff Mahoney 	return 0;
45772eaa055fSJeff Mahoney }
45782eaa055fSJeff Mahoney 
45792eaa055fSJeff Mahoney #define check_feature(root, change_mask, flags, mask_base)	\
45803b02a68aSJeff Mahoney check_feature_bits(root, FEAT_##mask_base, change_mask, flags,	\
45812eaa055fSJeff Mahoney 		   BTRFS_FEATURE_ ## mask_base ## _SUPP,	\
45822eaa055fSJeff Mahoney 		   BTRFS_FEATURE_ ## mask_base ## _SAFE_SET,	\
45832eaa055fSJeff Mahoney 		   BTRFS_FEATURE_ ## mask_base ## _SAFE_CLEAR)
45842eaa055fSJeff Mahoney 
45852eaa055fSJeff Mahoney static int btrfs_ioctl_set_features(struct file *file, void __user *arg)
45862eaa055fSJeff Mahoney {
45872eaa055fSJeff Mahoney 	struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
45882eaa055fSJeff Mahoney 	struct btrfs_super_block *super_block = root->fs_info->super_copy;
45892eaa055fSJeff Mahoney 	struct btrfs_ioctl_feature_flags flags[2];
45902eaa055fSJeff Mahoney 	struct btrfs_trans_handle *trans;
45912eaa055fSJeff Mahoney 	u64 newflags;
45922eaa055fSJeff Mahoney 	int ret;
45932eaa055fSJeff Mahoney 
45942eaa055fSJeff Mahoney 	if (!capable(CAP_SYS_ADMIN))
45952eaa055fSJeff Mahoney 		return -EPERM;
45962eaa055fSJeff Mahoney 
45972eaa055fSJeff Mahoney 	if (copy_from_user(flags, arg, sizeof(flags)))
45982eaa055fSJeff Mahoney 		return -EFAULT;
45992eaa055fSJeff Mahoney 
46002eaa055fSJeff Mahoney 	/* Nothing to do */
46012eaa055fSJeff Mahoney 	if (!flags[0].compat_flags && !flags[0].compat_ro_flags &&
46022eaa055fSJeff Mahoney 	    !flags[0].incompat_flags)
46032eaa055fSJeff Mahoney 		return 0;
46042eaa055fSJeff Mahoney 
46052eaa055fSJeff Mahoney 	ret = check_feature(root, flags[0].compat_flags,
46062eaa055fSJeff Mahoney 			    flags[1].compat_flags, COMPAT);
46072eaa055fSJeff Mahoney 	if (ret)
46082eaa055fSJeff Mahoney 		return ret;
46092eaa055fSJeff Mahoney 
46102eaa055fSJeff Mahoney 	ret = check_feature(root, flags[0].compat_ro_flags,
46112eaa055fSJeff Mahoney 			    flags[1].compat_ro_flags, COMPAT_RO);
46122eaa055fSJeff Mahoney 	if (ret)
46132eaa055fSJeff Mahoney 		return ret;
46142eaa055fSJeff Mahoney 
46152eaa055fSJeff Mahoney 	ret = check_feature(root, flags[0].incompat_flags,
46162eaa055fSJeff Mahoney 			    flags[1].incompat_flags, INCOMPAT);
46172eaa055fSJeff Mahoney 	if (ret)
46182eaa055fSJeff Mahoney 		return ret;
46192eaa055fSJeff Mahoney 
46202eaa055fSJeff Mahoney 	trans = btrfs_start_transaction(root, 1);
46212eaa055fSJeff Mahoney 	if (IS_ERR(trans))
46222eaa055fSJeff Mahoney 		return PTR_ERR(trans);
46232eaa055fSJeff Mahoney 
46242eaa055fSJeff Mahoney 	spin_lock(&root->fs_info->super_lock);
46252eaa055fSJeff Mahoney 	newflags = btrfs_super_compat_flags(super_block);
46262eaa055fSJeff Mahoney 	newflags |= flags[0].compat_flags & flags[1].compat_flags;
46272eaa055fSJeff Mahoney 	newflags &= ~(flags[0].compat_flags & ~flags[1].compat_flags);
46282eaa055fSJeff Mahoney 	btrfs_set_super_compat_flags(super_block, newflags);
46292eaa055fSJeff Mahoney 
46302eaa055fSJeff Mahoney 	newflags = btrfs_super_compat_ro_flags(super_block);
46312eaa055fSJeff Mahoney 	newflags |= flags[0].compat_ro_flags & flags[1].compat_ro_flags;
46322eaa055fSJeff Mahoney 	newflags &= ~(flags[0].compat_ro_flags & ~flags[1].compat_ro_flags);
46332eaa055fSJeff Mahoney 	btrfs_set_super_compat_ro_flags(super_block, newflags);
46342eaa055fSJeff Mahoney 
46352eaa055fSJeff Mahoney 	newflags = btrfs_super_incompat_flags(super_block);
46362eaa055fSJeff Mahoney 	newflags |= flags[0].incompat_flags & flags[1].incompat_flags;
46372eaa055fSJeff Mahoney 	newflags &= ~(flags[0].incompat_flags & ~flags[1].incompat_flags);
46382eaa055fSJeff Mahoney 	btrfs_set_super_incompat_flags(super_block, newflags);
46392eaa055fSJeff Mahoney 	spin_unlock(&root->fs_info->super_lock);
46402eaa055fSJeff Mahoney 
46412eaa055fSJeff Mahoney 	return btrfs_end_transaction(trans, root);
46422eaa055fSJeff Mahoney }
46432eaa055fSJeff Mahoney 
4644f46b5a66SChristoph Hellwig long btrfs_ioctl(struct file *file, unsigned int
4645f46b5a66SChristoph Hellwig 		cmd, unsigned long arg)
4646f46b5a66SChristoph Hellwig {
4647496ad9aaSAl Viro 	struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
46484bcabaa3SChristoph Hellwig 	void __user *argp = (void __user *)arg;
4649f46b5a66SChristoph Hellwig 
4650f46b5a66SChristoph Hellwig 	switch (cmd) {
46516cbff00fSChristoph Hellwig 	case FS_IOC_GETFLAGS:
46526cbff00fSChristoph Hellwig 		return btrfs_ioctl_getflags(file, argp);
46536cbff00fSChristoph Hellwig 	case FS_IOC_SETFLAGS:
46546cbff00fSChristoph Hellwig 		return btrfs_ioctl_setflags(file, argp);
46556cbff00fSChristoph Hellwig 	case FS_IOC_GETVERSION:
46566cbff00fSChristoph Hellwig 		return btrfs_ioctl_getversion(file, argp);
4657f7039b1dSLi Dongyang 	case FITRIM:
4658f7039b1dSLi Dongyang 		return btrfs_ioctl_fitrim(file, argp);
4659f46b5a66SChristoph Hellwig 	case BTRFS_IOC_SNAP_CREATE:
4660fa0d2b9bSLi Zefan 		return btrfs_ioctl_snap_create(file, argp, 0);
4661fdfb1e4fSLi Zefan 	case BTRFS_IOC_SNAP_CREATE_V2:
4662fa0d2b9bSLi Zefan 		return btrfs_ioctl_snap_create_v2(file, argp, 0);
46633de4586cSChris Mason 	case BTRFS_IOC_SUBVOL_CREATE:
4664fa0d2b9bSLi Zefan 		return btrfs_ioctl_snap_create(file, argp, 1);
46656f72c7e2SArne Jansen 	case BTRFS_IOC_SUBVOL_CREATE_V2:
46666f72c7e2SArne Jansen 		return btrfs_ioctl_snap_create_v2(file, argp, 1);
466776dda93cSYan, Zheng 	case BTRFS_IOC_SNAP_DESTROY:
466876dda93cSYan, Zheng 		return btrfs_ioctl_snap_destroy(file, argp);
46690caa102dSLi Zefan 	case BTRFS_IOC_SUBVOL_GETFLAGS:
46700caa102dSLi Zefan 		return btrfs_ioctl_subvol_getflags(file, argp);
46710caa102dSLi Zefan 	case BTRFS_IOC_SUBVOL_SETFLAGS:
46720caa102dSLi Zefan 		return btrfs_ioctl_subvol_setflags(file, argp);
46736ef5ed0dSJosef Bacik 	case BTRFS_IOC_DEFAULT_SUBVOL:
46746ef5ed0dSJosef Bacik 		return btrfs_ioctl_default_subvol(file, argp);
4675f46b5a66SChristoph Hellwig 	case BTRFS_IOC_DEFRAG:
46761e701a32SChris Mason 		return btrfs_ioctl_defrag(file, NULL);
46771e701a32SChris Mason 	case BTRFS_IOC_DEFRAG_RANGE:
46781e701a32SChris Mason 		return btrfs_ioctl_defrag(file, argp);
4679f46b5a66SChristoph Hellwig 	case BTRFS_IOC_RESIZE:
4680198605a8SMiao Xie 		return btrfs_ioctl_resize(file, argp);
4681f46b5a66SChristoph Hellwig 	case BTRFS_IOC_ADD_DEV:
46824bcabaa3SChristoph Hellwig 		return btrfs_ioctl_add_dev(root, argp);
4683f46b5a66SChristoph Hellwig 	case BTRFS_IOC_RM_DEV:
4684da24927bSMiao Xie 		return btrfs_ioctl_rm_dev(file, argp);
4685475f6387SJan Schmidt 	case BTRFS_IOC_FS_INFO:
4686475f6387SJan Schmidt 		return btrfs_ioctl_fs_info(root, argp);
4687475f6387SJan Schmidt 	case BTRFS_IOC_DEV_INFO:
4688475f6387SJan Schmidt 		return btrfs_ioctl_dev_info(root, argp);
4689f46b5a66SChristoph Hellwig 	case BTRFS_IOC_BALANCE:
46909ba1f6e4SLiu Bo 		return btrfs_ioctl_balance(file, NULL);
4691f46b5a66SChristoph Hellwig 	case BTRFS_IOC_CLONE:
4692c5c9cd4dSSage Weil 		return btrfs_ioctl_clone(file, arg, 0, 0, 0);
4693c5c9cd4dSSage Weil 	case BTRFS_IOC_CLONE_RANGE:
46947a865e8aSChristoph Hellwig 		return btrfs_ioctl_clone_range(file, argp);
4695f46b5a66SChristoph Hellwig 	case BTRFS_IOC_TRANS_START:
4696f46b5a66SChristoph Hellwig 		return btrfs_ioctl_trans_start(file);
4697f46b5a66SChristoph Hellwig 	case BTRFS_IOC_TRANS_END:
4698f46b5a66SChristoph Hellwig 		return btrfs_ioctl_trans_end(file);
4699ac8e9819SChris Mason 	case BTRFS_IOC_TREE_SEARCH:
4700ac8e9819SChris Mason 		return btrfs_ioctl_tree_search(file, argp);
4701ac8e9819SChris Mason 	case BTRFS_IOC_INO_LOOKUP:
4702ac8e9819SChris Mason 		return btrfs_ioctl_ino_lookup(file, argp);
4703d7728c96SJan Schmidt 	case BTRFS_IOC_INO_PATHS:
4704d7728c96SJan Schmidt 		return btrfs_ioctl_ino_to_path(root, argp);
4705d7728c96SJan Schmidt 	case BTRFS_IOC_LOGICAL_INO:
4706d7728c96SJan Schmidt 		return btrfs_ioctl_logical_to_ino(root, argp);
47071406e432SJosef Bacik 	case BTRFS_IOC_SPACE_INFO:
47081406e432SJosef Bacik 		return btrfs_ioctl_space_info(root, argp);
47099b199859SFilipe David Borba Manana 	case BTRFS_IOC_SYNC: {
47109b199859SFilipe David Borba Manana 		int ret;
47119b199859SFilipe David Borba Manana 
471291aef86fSMiao Xie 		ret = btrfs_start_delalloc_roots(root->fs_info, 0);
47139b199859SFilipe David Borba Manana 		if (ret)
47149b199859SFilipe David Borba Manana 			return ret;
47159b199859SFilipe David Borba Manana 		ret = btrfs_sync_fs(file->f_dentry->d_sb, 1);
47169b199859SFilipe David Borba Manana 		return ret;
47179b199859SFilipe David Borba Manana 	}
471846204592SSage Weil 	case BTRFS_IOC_START_SYNC:
47199a8c28beSMiao Xie 		return btrfs_ioctl_start_sync(root, argp);
472046204592SSage Weil 	case BTRFS_IOC_WAIT_SYNC:
47219a8c28beSMiao Xie 		return btrfs_ioctl_wait_sync(root, argp);
4722475f6387SJan Schmidt 	case BTRFS_IOC_SCRUB:
4723b8e95489SMiao Xie 		return btrfs_ioctl_scrub(file, argp);
4724475f6387SJan Schmidt 	case BTRFS_IOC_SCRUB_CANCEL:
4725475f6387SJan Schmidt 		return btrfs_ioctl_scrub_cancel(root, argp);
4726475f6387SJan Schmidt 	case BTRFS_IOC_SCRUB_PROGRESS:
4727475f6387SJan Schmidt 		return btrfs_ioctl_scrub_progress(root, argp);
4728c9e9f97bSIlya Dryomov 	case BTRFS_IOC_BALANCE_V2:
47299ba1f6e4SLiu Bo 		return btrfs_ioctl_balance(file, argp);
4730837d5b6eSIlya Dryomov 	case BTRFS_IOC_BALANCE_CTL:
4731837d5b6eSIlya Dryomov 		return btrfs_ioctl_balance_ctl(root, arg);
473219a39dceSIlya Dryomov 	case BTRFS_IOC_BALANCE_PROGRESS:
473319a39dceSIlya Dryomov 		return btrfs_ioctl_balance_progress(root, argp);
47348ea05e3aSAlexander Block 	case BTRFS_IOC_SET_RECEIVED_SUBVOL:
47358ea05e3aSAlexander Block 		return btrfs_ioctl_set_received_subvol(file, argp);
473631db9f7cSAlexander Block 	case BTRFS_IOC_SEND:
473731db9f7cSAlexander Block 		return btrfs_ioctl_send(file, argp);
4738c11d2c23SStefan Behrens 	case BTRFS_IOC_GET_DEV_STATS:
4739b27f7c0cSDavid Sterba 		return btrfs_ioctl_get_dev_stats(root, argp);
47405d13a37bSArne Jansen 	case BTRFS_IOC_QUOTA_CTL:
4741905b0ddaSMiao Xie 		return btrfs_ioctl_quota_ctl(file, argp);
47425d13a37bSArne Jansen 	case BTRFS_IOC_QGROUP_ASSIGN:
4743905b0ddaSMiao Xie 		return btrfs_ioctl_qgroup_assign(file, argp);
47445d13a37bSArne Jansen 	case BTRFS_IOC_QGROUP_CREATE:
4745905b0ddaSMiao Xie 		return btrfs_ioctl_qgroup_create(file, argp);
47465d13a37bSArne Jansen 	case BTRFS_IOC_QGROUP_LIMIT:
4747905b0ddaSMiao Xie 		return btrfs_ioctl_qgroup_limit(file, argp);
47482f232036SJan Schmidt 	case BTRFS_IOC_QUOTA_RESCAN:
47492f232036SJan Schmidt 		return btrfs_ioctl_quota_rescan(file, argp);
47502f232036SJan Schmidt 	case BTRFS_IOC_QUOTA_RESCAN_STATUS:
47512f232036SJan Schmidt 		return btrfs_ioctl_quota_rescan_status(file, argp);
475257254b6eSJan Schmidt 	case BTRFS_IOC_QUOTA_RESCAN_WAIT:
475357254b6eSJan Schmidt 		return btrfs_ioctl_quota_rescan_wait(file, argp);
47543f6bcfbdSStefan Behrens 	case BTRFS_IOC_DEV_REPLACE:
47553f6bcfbdSStefan Behrens 		return btrfs_ioctl_dev_replace(root, argp);
4756867ab667Sjeff.liu 	case BTRFS_IOC_GET_FSLABEL:
4757867ab667Sjeff.liu 		return btrfs_ioctl_get_fslabel(file, argp);
4758a8bfd4abSjeff.liu 	case BTRFS_IOC_SET_FSLABEL:
4759a8bfd4abSjeff.liu 		return btrfs_ioctl_set_fslabel(file, argp);
4760416161dbSMark Fasheh 	case BTRFS_IOC_FILE_EXTENT_SAME:
4761416161dbSMark Fasheh 		return btrfs_ioctl_file_extent_same(file, argp);
47622eaa055fSJeff Mahoney 	case BTRFS_IOC_GET_SUPPORTED_FEATURES:
47632eaa055fSJeff Mahoney 		return btrfs_ioctl_get_supported_features(file, argp);
47642eaa055fSJeff Mahoney 	case BTRFS_IOC_GET_FEATURES:
47652eaa055fSJeff Mahoney 		return btrfs_ioctl_get_features(file, argp);
47662eaa055fSJeff Mahoney 	case BTRFS_IOC_SET_FEATURES:
47672eaa055fSJeff Mahoney 		return btrfs_ioctl_set_features(file, argp);
4768f46b5a66SChristoph Hellwig 	}
4769f46b5a66SChristoph Hellwig 
4770f46b5a66SChristoph Hellwig 	return -ENOTTY;
4771f46b5a66SChristoph Hellwig }
4772