xref: /openbmc/linux/fs/btrfs/ioctl.c (revision 0caa102d)
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>
434b4e25f2SChris Mason #include "compat.h"
44f46b5a66SChristoph Hellwig #include "ctree.h"
45f46b5a66SChristoph Hellwig #include "disk-io.h"
46f46b5a66SChristoph Hellwig #include "transaction.h"
47f46b5a66SChristoph Hellwig #include "btrfs_inode.h"
48f46b5a66SChristoph Hellwig #include "ioctl.h"
49f46b5a66SChristoph Hellwig #include "print-tree.h"
50f46b5a66SChristoph Hellwig #include "volumes.h"
51925baeddSChris Mason #include "locking.h"
52f46b5a66SChristoph Hellwig 
536cbff00fSChristoph Hellwig /* Mask out flags that are inappropriate for the given type of inode. */
546cbff00fSChristoph Hellwig static inline __u32 btrfs_mask_flags(umode_t mode, __u32 flags)
556cbff00fSChristoph Hellwig {
566cbff00fSChristoph Hellwig 	if (S_ISDIR(mode))
576cbff00fSChristoph Hellwig 		return flags;
586cbff00fSChristoph Hellwig 	else if (S_ISREG(mode))
596cbff00fSChristoph Hellwig 		return flags & ~FS_DIRSYNC_FL;
606cbff00fSChristoph Hellwig 	else
616cbff00fSChristoph Hellwig 		return flags & (FS_NODUMP_FL | FS_NOATIME_FL);
626cbff00fSChristoph Hellwig }
63f46b5a66SChristoph Hellwig 
646cbff00fSChristoph Hellwig /*
656cbff00fSChristoph Hellwig  * Export inode flags to the format expected by the FS_IOC_GETFLAGS ioctl.
666cbff00fSChristoph Hellwig  */
676cbff00fSChristoph Hellwig static unsigned int btrfs_flags_to_ioctl(unsigned int flags)
686cbff00fSChristoph Hellwig {
696cbff00fSChristoph Hellwig 	unsigned int iflags = 0;
706cbff00fSChristoph Hellwig 
716cbff00fSChristoph Hellwig 	if (flags & BTRFS_INODE_SYNC)
726cbff00fSChristoph Hellwig 		iflags |= FS_SYNC_FL;
736cbff00fSChristoph Hellwig 	if (flags & BTRFS_INODE_IMMUTABLE)
746cbff00fSChristoph Hellwig 		iflags |= FS_IMMUTABLE_FL;
756cbff00fSChristoph Hellwig 	if (flags & BTRFS_INODE_APPEND)
766cbff00fSChristoph Hellwig 		iflags |= FS_APPEND_FL;
776cbff00fSChristoph Hellwig 	if (flags & BTRFS_INODE_NODUMP)
786cbff00fSChristoph Hellwig 		iflags |= FS_NODUMP_FL;
796cbff00fSChristoph Hellwig 	if (flags & BTRFS_INODE_NOATIME)
806cbff00fSChristoph Hellwig 		iflags |= FS_NOATIME_FL;
816cbff00fSChristoph Hellwig 	if (flags & BTRFS_INODE_DIRSYNC)
826cbff00fSChristoph Hellwig 		iflags |= FS_DIRSYNC_FL;
836cbff00fSChristoph Hellwig 
846cbff00fSChristoph Hellwig 	return iflags;
856cbff00fSChristoph Hellwig }
866cbff00fSChristoph Hellwig 
876cbff00fSChristoph Hellwig /*
886cbff00fSChristoph Hellwig  * Update inode->i_flags based on the btrfs internal flags.
896cbff00fSChristoph Hellwig  */
906cbff00fSChristoph Hellwig void btrfs_update_iflags(struct inode *inode)
916cbff00fSChristoph Hellwig {
926cbff00fSChristoph Hellwig 	struct btrfs_inode *ip = BTRFS_I(inode);
936cbff00fSChristoph Hellwig 
946cbff00fSChristoph Hellwig 	inode->i_flags &= ~(S_SYNC|S_APPEND|S_IMMUTABLE|S_NOATIME|S_DIRSYNC);
956cbff00fSChristoph Hellwig 
966cbff00fSChristoph Hellwig 	if (ip->flags & BTRFS_INODE_SYNC)
976cbff00fSChristoph Hellwig 		inode->i_flags |= S_SYNC;
986cbff00fSChristoph Hellwig 	if (ip->flags & BTRFS_INODE_IMMUTABLE)
996cbff00fSChristoph Hellwig 		inode->i_flags |= S_IMMUTABLE;
1006cbff00fSChristoph Hellwig 	if (ip->flags & BTRFS_INODE_APPEND)
1016cbff00fSChristoph Hellwig 		inode->i_flags |= S_APPEND;
1026cbff00fSChristoph Hellwig 	if (ip->flags & BTRFS_INODE_NOATIME)
1036cbff00fSChristoph Hellwig 		inode->i_flags |= S_NOATIME;
1046cbff00fSChristoph Hellwig 	if (ip->flags & BTRFS_INODE_DIRSYNC)
1056cbff00fSChristoph Hellwig 		inode->i_flags |= S_DIRSYNC;
1066cbff00fSChristoph Hellwig }
1076cbff00fSChristoph Hellwig 
1086cbff00fSChristoph Hellwig /*
1096cbff00fSChristoph Hellwig  * Inherit flags from the parent inode.
1106cbff00fSChristoph Hellwig  *
1116cbff00fSChristoph Hellwig  * Unlike extN we don't have any flags we don't want to inherit currently.
1126cbff00fSChristoph Hellwig  */
1136cbff00fSChristoph Hellwig void btrfs_inherit_iflags(struct inode *inode, struct inode *dir)
1146cbff00fSChristoph Hellwig {
1150b4dcea5SChris Mason 	unsigned int flags;
1160b4dcea5SChris Mason 
1170b4dcea5SChris Mason 	if (!dir)
1180b4dcea5SChris Mason 		return;
1190b4dcea5SChris Mason 
1200b4dcea5SChris Mason 	flags = BTRFS_I(dir)->flags;
1216cbff00fSChristoph Hellwig 
1226cbff00fSChristoph Hellwig 	if (S_ISREG(inode->i_mode))
1236cbff00fSChristoph Hellwig 		flags &= ~BTRFS_INODE_DIRSYNC;
1246cbff00fSChristoph Hellwig 	else if (!S_ISDIR(inode->i_mode))
1256cbff00fSChristoph Hellwig 		flags &= (BTRFS_INODE_NODUMP | BTRFS_INODE_NOATIME);
1266cbff00fSChristoph Hellwig 
1276cbff00fSChristoph Hellwig 	BTRFS_I(inode)->flags = flags;
1286cbff00fSChristoph Hellwig 	btrfs_update_iflags(inode);
1296cbff00fSChristoph Hellwig }
1306cbff00fSChristoph Hellwig 
1316cbff00fSChristoph Hellwig static int btrfs_ioctl_getflags(struct file *file, void __user *arg)
1326cbff00fSChristoph Hellwig {
1336cbff00fSChristoph Hellwig 	struct btrfs_inode *ip = BTRFS_I(file->f_path.dentry->d_inode);
1346cbff00fSChristoph Hellwig 	unsigned int flags = btrfs_flags_to_ioctl(ip->flags);
1356cbff00fSChristoph Hellwig 
1366cbff00fSChristoph Hellwig 	if (copy_to_user(arg, &flags, sizeof(flags)))
1376cbff00fSChristoph Hellwig 		return -EFAULT;
1386cbff00fSChristoph Hellwig 	return 0;
1396cbff00fSChristoph Hellwig }
1406cbff00fSChristoph Hellwig 
1416cbff00fSChristoph Hellwig static int btrfs_ioctl_setflags(struct file *file, void __user *arg)
1426cbff00fSChristoph Hellwig {
1436cbff00fSChristoph Hellwig 	struct inode *inode = file->f_path.dentry->d_inode;
1446cbff00fSChristoph Hellwig 	struct btrfs_inode *ip = BTRFS_I(inode);
1456cbff00fSChristoph Hellwig 	struct btrfs_root *root = ip->root;
1466cbff00fSChristoph Hellwig 	struct btrfs_trans_handle *trans;
1476cbff00fSChristoph Hellwig 	unsigned int flags, oldflags;
1486cbff00fSChristoph Hellwig 	int ret;
1496cbff00fSChristoph Hellwig 
150b83cc969SLi Zefan 	if (btrfs_root_readonly(root))
151b83cc969SLi Zefan 		return -EROFS;
152b83cc969SLi Zefan 
1536cbff00fSChristoph Hellwig 	if (copy_from_user(&flags, arg, sizeof(flags)))
1546cbff00fSChristoph Hellwig 		return -EFAULT;
1556cbff00fSChristoph Hellwig 
1566cbff00fSChristoph Hellwig 	if (flags & ~(FS_IMMUTABLE_FL | FS_APPEND_FL | \
1576cbff00fSChristoph Hellwig 		      FS_NOATIME_FL | FS_NODUMP_FL | \
1586cbff00fSChristoph Hellwig 		      FS_SYNC_FL | FS_DIRSYNC_FL))
1596cbff00fSChristoph Hellwig 		return -EOPNOTSUPP;
1606cbff00fSChristoph Hellwig 
1616cbff00fSChristoph Hellwig 	if (!is_owner_or_cap(inode))
1626cbff00fSChristoph Hellwig 		return -EACCES;
1636cbff00fSChristoph Hellwig 
1646cbff00fSChristoph Hellwig 	mutex_lock(&inode->i_mutex);
1656cbff00fSChristoph Hellwig 
1666cbff00fSChristoph Hellwig 	flags = btrfs_mask_flags(inode->i_mode, flags);
1676cbff00fSChristoph Hellwig 	oldflags = btrfs_flags_to_ioctl(ip->flags);
1686cbff00fSChristoph Hellwig 	if ((flags ^ oldflags) & (FS_APPEND_FL | FS_IMMUTABLE_FL)) {
1696cbff00fSChristoph Hellwig 		if (!capable(CAP_LINUX_IMMUTABLE)) {
1706cbff00fSChristoph Hellwig 			ret = -EPERM;
1716cbff00fSChristoph Hellwig 			goto out_unlock;
1726cbff00fSChristoph Hellwig 		}
1736cbff00fSChristoph Hellwig 	}
1746cbff00fSChristoph Hellwig 
1756cbff00fSChristoph Hellwig 	ret = mnt_want_write(file->f_path.mnt);
1766cbff00fSChristoph Hellwig 	if (ret)
1776cbff00fSChristoph Hellwig 		goto out_unlock;
1786cbff00fSChristoph Hellwig 
1796cbff00fSChristoph Hellwig 	if (flags & FS_SYNC_FL)
1806cbff00fSChristoph Hellwig 		ip->flags |= BTRFS_INODE_SYNC;
1816cbff00fSChristoph Hellwig 	else
1826cbff00fSChristoph Hellwig 		ip->flags &= ~BTRFS_INODE_SYNC;
1836cbff00fSChristoph Hellwig 	if (flags & FS_IMMUTABLE_FL)
1846cbff00fSChristoph Hellwig 		ip->flags |= BTRFS_INODE_IMMUTABLE;
1856cbff00fSChristoph Hellwig 	else
1866cbff00fSChristoph Hellwig 		ip->flags &= ~BTRFS_INODE_IMMUTABLE;
1876cbff00fSChristoph Hellwig 	if (flags & FS_APPEND_FL)
1886cbff00fSChristoph Hellwig 		ip->flags |= BTRFS_INODE_APPEND;
1896cbff00fSChristoph Hellwig 	else
1906cbff00fSChristoph Hellwig 		ip->flags &= ~BTRFS_INODE_APPEND;
1916cbff00fSChristoph Hellwig 	if (flags & FS_NODUMP_FL)
1926cbff00fSChristoph Hellwig 		ip->flags |= BTRFS_INODE_NODUMP;
1936cbff00fSChristoph Hellwig 	else
1946cbff00fSChristoph Hellwig 		ip->flags &= ~BTRFS_INODE_NODUMP;
1956cbff00fSChristoph Hellwig 	if (flags & FS_NOATIME_FL)
1966cbff00fSChristoph Hellwig 		ip->flags |= BTRFS_INODE_NOATIME;
1976cbff00fSChristoph Hellwig 	else
1986cbff00fSChristoph Hellwig 		ip->flags &= ~BTRFS_INODE_NOATIME;
1996cbff00fSChristoph Hellwig 	if (flags & FS_DIRSYNC_FL)
2006cbff00fSChristoph Hellwig 		ip->flags |= BTRFS_INODE_DIRSYNC;
2016cbff00fSChristoph Hellwig 	else
2026cbff00fSChristoph Hellwig 		ip->flags &= ~BTRFS_INODE_DIRSYNC;
2036cbff00fSChristoph Hellwig 
2046cbff00fSChristoph Hellwig 
2056cbff00fSChristoph Hellwig 	trans = btrfs_join_transaction(root, 1);
2066cbff00fSChristoph Hellwig 	BUG_ON(!trans);
2076cbff00fSChristoph Hellwig 
2086cbff00fSChristoph Hellwig 	ret = btrfs_update_inode(trans, root, inode);
2096cbff00fSChristoph Hellwig 	BUG_ON(ret);
2106cbff00fSChristoph Hellwig 
2116cbff00fSChristoph Hellwig 	btrfs_update_iflags(inode);
2126cbff00fSChristoph Hellwig 	inode->i_ctime = CURRENT_TIME;
2136cbff00fSChristoph Hellwig 	btrfs_end_transaction(trans, root);
2146cbff00fSChristoph Hellwig 
2156cbff00fSChristoph Hellwig 	mnt_drop_write(file->f_path.mnt);
2166cbff00fSChristoph Hellwig  out_unlock:
2176cbff00fSChristoph Hellwig 	mutex_unlock(&inode->i_mutex);
2186cbff00fSChristoph Hellwig 	return 0;
2196cbff00fSChristoph Hellwig }
2206cbff00fSChristoph Hellwig 
2216cbff00fSChristoph Hellwig static int btrfs_ioctl_getversion(struct file *file, int __user *arg)
2226cbff00fSChristoph Hellwig {
2236cbff00fSChristoph Hellwig 	struct inode *inode = file->f_path.dentry->d_inode;
2246cbff00fSChristoph Hellwig 
2256cbff00fSChristoph Hellwig 	return put_user(inode->i_generation, arg);
2266cbff00fSChristoph Hellwig }
227f46b5a66SChristoph Hellwig 
228cb8e7090SChristoph Hellwig static noinline int create_subvol(struct btrfs_root *root,
229cb8e7090SChristoph Hellwig 				  struct dentry *dentry,
23072fd032eSSage Weil 				  char *name, int namelen,
23172fd032eSSage Weil 				  u64 *async_transid)
232f46b5a66SChristoph Hellwig {
233f46b5a66SChristoph Hellwig 	struct btrfs_trans_handle *trans;
234f46b5a66SChristoph Hellwig 	struct btrfs_key key;
235f46b5a66SChristoph Hellwig 	struct btrfs_root_item root_item;
236f46b5a66SChristoph Hellwig 	struct btrfs_inode_item *inode_item;
237f46b5a66SChristoph Hellwig 	struct extent_buffer *leaf;
23876dda93cSYan, Zheng 	struct btrfs_root *new_root;
2396a912213SJosef Bacik 	struct dentry *parent = dget_parent(dentry);
2406a912213SJosef Bacik 	struct inode *dir;
241f46b5a66SChristoph Hellwig 	int ret;
242f46b5a66SChristoph Hellwig 	int err;
243f46b5a66SChristoph Hellwig 	u64 objectid;
244f46b5a66SChristoph Hellwig 	u64 new_dirid = BTRFS_FIRST_FREE_OBJECTID;
2453de4586cSChris Mason 	u64 index = 0;
246f46b5a66SChristoph Hellwig 
247a22285a6SYan, Zheng 	ret = btrfs_find_free_objectid(NULL, root->fs_info->tree_root,
248a22285a6SYan, Zheng 				       0, &objectid);
2496a912213SJosef Bacik 	if (ret) {
2506a912213SJosef Bacik 		dput(parent);
251a22285a6SYan, Zheng 		return ret;
2526a912213SJosef Bacik 	}
2536a912213SJosef Bacik 
2546a912213SJosef Bacik 	dir = parent->d_inode;
2556a912213SJosef Bacik 
2569ed74f2dSJosef Bacik 	/*
2579ed74f2dSJosef Bacik 	 * 1 - inode item
2589ed74f2dSJosef Bacik 	 * 2 - refs
2599ed74f2dSJosef Bacik 	 * 1 - root item
2609ed74f2dSJosef Bacik 	 * 2 - dir items
2619ed74f2dSJosef Bacik 	 */
262a22285a6SYan, Zheng 	trans = btrfs_start_transaction(root, 6);
2636a912213SJosef Bacik 	if (IS_ERR(trans)) {
2646a912213SJosef Bacik 		dput(parent);
265a22285a6SYan, Zheng 		return PTR_ERR(trans);
2666a912213SJosef Bacik 	}
267f46b5a66SChristoph Hellwig 
2685d4f98a2SYan Zheng 	leaf = btrfs_alloc_free_block(trans, root, root->leafsize,
2695d4f98a2SYan Zheng 				      0, objectid, NULL, 0, 0, 0);
2708e8a1e31SJosef Bacik 	if (IS_ERR(leaf)) {
2718e8a1e31SJosef Bacik 		ret = PTR_ERR(leaf);
2728e8a1e31SJosef Bacik 		goto fail;
2738e8a1e31SJosef Bacik 	}
274f46b5a66SChristoph Hellwig 
2755d4f98a2SYan Zheng 	memset_extent_buffer(leaf, 0, 0, sizeof(struct btrfs_header));
276f46b5a66SChristoph Hellwig 	btrfs_set_header_bytenr(leaf, leaf->start);
277f46b5a66SChristoph Hellwig 	btrfs_set_header_generation(leaf, trans->transid);
2785d4f98a2SYan Zheng 	btrfs_set_header_backref_rev(leaf, BTRFS_MIXED_BACKREF_REV);
279f46b5a66SChristoph Hellwig 	btrfs_set_header_owner(leaf, objectid);
280f46b5a66SChristoph Hellwig 
281f46b5a66SChristoph Hellwig 	write_extent_buffer(leaf, root->fs_info->fsid,
282f46b5a66SChristoph Hellwig 			    (unsigned long)btrfs_header_fsid(leaf),
283f46b5a66SChristoph Hellwig 			    BTRFS_FSID_SIZE);
2845d4f98a2SYan Zheng 	write_extent_buffer(leaf, root->fs_info->chunk_tree_uuid,
2855d4f98a2SYan Zheng 			    (unsigned long)btrfs_header_chunk_tree_uuid(leaf),
2865d4f98a2SYan Zheng 			    BTRFS_UUID_SIZE);
287f46b5a66SChristoph Hellwig 	btrfs_mark_buffer_dirty(leaf);
288f46b5a66SChristoph Hellwig 
289f46b5a66SChristoph Hellwig 	inode_item = &root_item.inode;
290f46b5a66SChristoph Hellwig 	memset(inode_item, 0, sizeof(*inode_item));
291f46b5a66SChristoph Hellwig 	inode_item->generation = cpu_to_le64(1);
292f46b5a66SChristoph Hellwig 	inode_item->size = cpu_to_le64(3);
293f46b5a66SChristoph Hellwig 	inode_item->nlink = cpu_to_le32(1);
294a76a3cd4SYan Zheng 	inode_item->nbytes = cpu_to_le64(root->leafsize);
295f46b5a66SChristoph Hellwig 	inode_item->mode = cpu_to_le32(S_IFDIR | 0755);
296f46b5a66SChristoph Hellwig 
297f46b5a66SChristoph Hellwig 	btrfs_set_root_bytenr(&root_item, leaf->start);
29884234f3aSYan Zheng 	btrfs_set_root_generation(&root_item, trans->transid);
299f46b5a66SChristoph Hellwig 	btrfs_set_root_level(&root_item, 0);
300f46b5a66SChristoph Hellwig 	btrfs_set_root_refs(&root_item, 1);
30186b9f2ecSYan, Zheng 	btrfs_set_root_used(&root_item, leaf->len);
30280ff3856SYan Zheng 	btrfs_set_root_last_snapshot(&root_item, 0);
303f46b5a66SChristoph Hellwig 
304f46b5a66SChristoph Hellwig 	memset(&root_item.drop_progress, 0, sizeof(root_item.drop_progress));
305f46b5a66SChristoph Hellwig 	root_item.drop_level = 0;
306f46b5a66SChristoph Hellwig 
307925baeddSChris Mason 	btrfs_tree_unlock(leaf);
308f46b5a66SChristoph Hellwig 	free_extent_buffer(leaf);
309f46b5a66SChristoph Hellwig 	leaf = NULL;
310f46b5a66SChristoph Hellwig 
311f46b5a66SChristoph Hellwig 	btrfs_set_root_dirid(&root_item, new_dirid);
312f46b5a66SChristoph Hellwig 
313f46b5a66SChristoph Hellwig 	key.objectid = objectid;
3145d4f98a2SYan Zheng 	key.offset = 0;
315f46b5a66SChristoph Hellwig 	btrfs_set_key_type(&key, BTRFS_ROOT_ITEM_KEY);
316f46b5a66SChristoph Hellwig 	ret = btrfs_insert_root(trans, root->fs_info->tree_root, &key,
317f46b5a66SChristoph Hellwig 				&root_item);
318f46b5a66SChristoph Hellwig 	if (ret)
319f46b5a66SChristoph Hellwig 		goto fail;
320f46b5a66SChristoph Hellwig 
32176dda93cSYan, Zheng 	key.offset = (u64)-1;
32276dda93cSYan, Zheng 	new_root = btrfs_read_fs_root_no_name(root->fs_info, &key);
32376dda93cSYan, Zheng 	BUG_ON(IS_ERR(new_root));
32476dda93cSYan, Zheng 
32576dda93cSYan, Zheng 	btrfs_record_root_in_trans(trans, new_root);
32676dda93cSYan, Zheng 
32776dda93cSYan, Zheng 	ret = btrfs_create_subvol_root(trans, new_root, new_dirid,
32876dda93cSYan, Zheng 				       BTRFS_I(dir)->block_group);
329f46b5a66SChristoph Hellwig 	/*
330f46b5a66SChristoph Hellwig 	 * insert the directory item
331f46b5a66SChristoph Hellwig 	 */
3323de4586cSChris Mason 	ret = btrfs_set_inode_index(dir, &index);
3333de4586cSChris Mason 	BUG_ON(ret);
3343de4586cSChris Mason 
3353de4586cSChris Mason 	ret = btrfs_insert_dir_item(trans, root,
336f46b5a66SChristoph Hellwig 				    name, namelen, dir->i_ino, &key,
3373de4586cSChris Mason 				    BTRFS_FT_DIR, index);
338f46b5a66SChristoph Hellwig 	if (ret)
339f46b5a66SChristoph Hellwig 		goto fail;
3400660b5afSChris Mason 
34152c26179SYan Zheng 	btrfs_i_size_write(dir, dir->i_size + namelen * 2);
34252c26179SYan Zheng 	ret = btrfs_update_inode(trans, root, dir);
34352c26179SYan Zheng 	BUG_ON(ret);
34452c26179SYan Zheng 
3450660b5afSChris Mason 	ret = btrfs_add_root_ref(trans, root->fs_info->tree_root,
3464df27c4dSYan, Zheng 				 objectid, root->root_key.objectid,
3470660b5afSChris Mason 				 dir->i_ino, index, name, namelen);
34876dda93cSYan, Zheng 
3490660b5afSChris Mason 	BUG_ON(ret);
3500660b5afSChris Mason 
35176dda93cSYan, Zheng 	d_instantiate(dentry, btrfs_lookup_dentry(dir, dentry));
352f46b5a66SChristoph Hellwig fail:
3536a912213SJosef Bacik 	dput(parent);
35472fd032eSSage Weil 	if (async_transid) {
35572fd032eSSage Weil 		*async_transid = trans->transid;
35672fd032eSSage Weil 		err = btrfs_commit_transaction_async(trans, root, 1);
35772fd032eSSage Weil 	} else {
35876dda93cSYan, Zheng 		err = btrfs_commit_transaction(trans, root);
35972fd032eSSage Weil 	}
360f46b5a66SChristoph Hellwig 	if (err && !ret)
361f46b5a66SChristoph Hellwig 		ret = err;
362f46b5a66SChristoph Hellwig 	return ret;
363f46b5a66SChristoph Hellwig }
364f46b5a66SChristoph Hellwig 
36572fd032eSSage Weil static int create_snapshot(struct btrfs_root *root, struct dentry *dentry,
366b83cc969SLi Zefan 			   char *name, int namelen, u64 *async_transid,
367b83cc969SLi Zefan 			   bool readonly)
368f46b5a66SChristoph Hellwig {
3692e4bfab9SYan, Zheng 	struct inode *inode;
3706a912213SJosef Bacik 	struct dentry *parent;
371f46b5a66SChristoph Hellwig 	struct btrfs_pending_snapshot *pending_snapshot;
372f46b5a66SChristoph Hellwig 	struct btrfs_trans_handle *trans;
3732e4bfab9SYan, Zheng 	int ret;
374f46b5a66SChristoph Hellwig 
375f46b5a66SChristoph Hellwig 	if (!root->ref_cows)
376f46b5a66SChristoph Hellwig 		return -EINVAL;
377f46b5a66SChristoph Hellwig 
378a22285a6SYan, Zheng 	pending_snapshot = kzalloc(sizeof(*pending_snapshot), GFP_NOFS);
379a22285a6SYan, Zheng 	if (!pending_snapshot)
380a22285a6SYan, Zheng 		return -ENOMEM;
381a22285a6SYan, Zheng 
382a22285a6SYan, Zheng 	btrfs_init_block_rsv(&pending_snapshot->block_rsv);
383a22285a6SYan, Zheng 	pending_snapshot->dentry = dentry;
384a22285a6SYan, Zheng 	pending_snapshot->root = root;
385b83cc969SLi Zefan 	pending_snapshot->readonly = readonly;
386a22285a6SYan, Zheng 
387a22285a6SYan, Zheng 	trans = btrfs_start_transaction(root->fs_info->extent_root, 5);
388a22285a6SYan, Zheng 	if (IS_ERR(trans)) {
389a22285a6SYan, Zheng 		ret = PTR_ERR(trans);
390a22285a6SYan, Zheng 		goto fail;
391a22285a6SYan, Zheng 	}
392a22285a6SYan, Zheng 
393a22285a6SYan, Zheng 	ret = btrfs_snap_reserve_metadata(trans, pending_snapshot);
394a22285a6SYan, Zheng 	BUG_ON(ret);
395a22285a6SYan, Zheng 
396a22285a6SYan, Zheng 	list_add(&pending_snapshot->list,
397a22285a6SYan, Zheng 		 &trans->transaction->pending_snapshots);
39872fd032eSSage Weil 	if (async_transid) {
39972fd032eSSage Weil 		*async_transid = trans->transid;
40072fd032eSSage Weil 		ret = btrfs_commit_transaction_async(trans,
40172fd032eSSage Weil 				     root->fs_info->extent_root, 1);
40272fd032eSSage Weil 	} else {
40372fd032eSSage Weil 		ret = btrfs_commit_transaction(trans,
40472fd032eSSage Weil 					       root->fs_info->extent_root);
40572fd032eSSage Weil 	}
406a22285a6SYan, Zheng 	BUG_ON(ret);
407a22285a6SYan, Zheng 
408a22285a6SYan, Zheng 	ret = pending_snapshot->error;
409f46b5a66SChristoph Hellwig 	if (ret)
4102e4bfab9SYan, Zheng 		goto fail;
411f46b5a66SChristoph Hellwig 
412a22285a6SYan, Zheng 	btrfs_orphan_cleanup(pending_snapshot->snap);
413f46b5a66SChristoph Hellwig 
4146a912213SJosef Bacik 	parent = dget_parent(dentry);
4156a912213SJosef Bacik 	inode = btrfs_lookup_dentry(parent->d_inode, dentry);
4166a912213SJosef Bacik 	dput(parent);
4172e4bfab9SYan, Zheng 	if (IS_ERR(inode)) {
4182e4bfab9SYan, Zheng 		ret = PTR_ERR(inode);
4192e4bfab9SYan, Zheng 		goto fail;
4202e4bfab9SYan, Zheng 	}
4212e4bfab9SYan, Zheng 	BUG_ON(!inode);
4222e4bfab9SYan, Zheng 	d_instantiate(dentry, inode);
4232e4bfab9SYan, Zheng 	ret = 0;
4242e4bfab9SYan, Zheng fail:
425a22285a6SYan, Zheng 	kfree(pending_snapshot);
426f46b5a66SChristoph Hellwig 	return ret;
427f46b5a66SChristoph Hellwig }
428f46b5a66SChristoph Hellwig 
4294260f7c7SSage Weil /*  copy of check_sticky in fs/namei.c()
4304260f7c7SSage Weil * It's inline, so penalty for filesystems that don't use sticky bit is
4314260f7c7SSage Weil * minimal.
4324260f7c7SSage Weil */
4334260f7c7SSage Weil static inline int btrfs_check_sticky(struct inode *dir, struct inode *inode)
4344260f7c7SSage Weil {
4354260f7c7SSage Weil 	uid_t fsuid = current_fsuid();
4364260f7c7SSage Weil 
4374260f7c7SSage Weil 	if (!(dir->i_mode & S_ISVTX))
4384260f7c7SSage Weil 		return 0;
4394260f7c7SSage Weil 	if (inode->i_uid == fsuid)
4404260f7c7SSage Weil 		return 0;
4414260f7c7SSage Weil 	if (dir->i_uid == fsuid)
4424260f7c7SSage Weil 		return 0;
4434260f7c7SSage Weil 	return !capable(CAP_FOWNER);
4444260f7c7SSage Weil }
4454260f7c7SSage Weil 
4464260f7c7SSage Weil /*  copy of may_delete in fs/namei.c()
4474260f7c7SSage Weil  *	Check whether we can remove a link victim from directory dir, check
4484260f7c7SSage Weil  *  whether the type of victim is right.
4494260f7c7SSage Weil  *  1. We can't do it if dir is read-only (done in permission())
4504260f7c7SSage Weil  *  2. We should have write and exec permissions on dir
4514260f7c7SSage Weil  *  3. We can't remove anything from append-only dir
4524260f7c7SSage Weil  *  4. We can't do anything with immutable dir (done in permission())
4534260f7c7SSage Weil  *  5. If the sticky bit on dir is set we should either
4544260f7c7SSage Weil  *	a. be owner of dir, or
4554260f7c7SSage Weil  *	b. be owner of victim, or
4564260f7c7SSage Weil  *	c. have CAP_FOWNER capability
4574260f7c7SSage Weil  *  6. If the victim is append-only or immutable we can't do antyhing with
4584260f7c7SSage Weil  *     links pointing to it.
4594260f7c7SSage Weil  *  7. If we were asked to remove a directory and victim isn't one - ENOTDIR.
4604260f7c7SSage Weil  *  8. If we were asked to remove a non-directory and victim isn't one - EISDIR.
4614260f7c7SSage Weil  *  9. We can't remove a root or mountpoint.
4624260f7c7SSage Weil  * 10. We don't allow removal of NFS sillyrenamed files; it's handled by
4634260f7c7SSage Weil  *     nfs_async_unlink().
4644260f7c7SSage Weil  */
4654260f7c7SSage Weil 
4664260f7c7SSage Weil static int btrfs_may_delete(struct inode *dir,struct dentry *victim,int isdir)
4674260f7c7SSage Weil {
4684260f7c7SSage Weil 	int error;
4694260f7c7SSage Weil 
4704260f7c7SSage Weil 	if (!victim->d_inode)
4714260f7c7SSage Weil 		return -ENOENT;
4724260f7c7SSage Weil 
4734260f7c7SSage Weil 	BUG_ON(victim->d_parent->d_inode != dir);
4744260f7c7SSage Weil 	audit_inode_child(victim, dir);
4754260f7c7SSage Weil 
4764260f7c7SSage Weil 	error = inode_permission(dir, MAY_WRITE | MAY_EXEC);
4774260f7c7SSage Weil 	if (error)
4784260f7c7SSage Weil 		return error;
4794260f7c7SSage Weil 	if (IS_APPEND(dir))
4804260f7c7SSage Weil 		return -EPERM;
4814260f7c7SSage Weil 	if (btrfs_check_sticky(dir, victim->d_inode)||
4824260f7c7SSage Weil 		IS_APPEND(victim->d_inode)||
4834260f7c7SSage Weil 	    IS_IMMUTABLE(victim->d_inode) || IS_SWAPFILE(victim->d_inode))
4844260f7c7SSage Weil 		return -EPERM;
4854260f7c7SSage Weil 	if (isdir) {
4864260f7c7SSage Weil 		if (!S_ISDIR(victim->d_inode->i_mode))
4874260f7c7SSage Weil 			return -ENOTDIR;
4884260f7c7SSage Weil 		if (IS_ROOT(victim))
4894260f7c7SSage Weil 			return -EBUSY;
4904260f7c7SSage Weil 	} else if (S_ISDIR(victim->d_inode->i_mode))
4914260f7c7SSage Weil 		return -EISDIR;
4924260f7c7SSage Weil 	if (IS_DEADDIR(dir))
4934260f7c7SSage Weil 		return -ENOENT;
4944260f7c7SSage Weil 	if (victim->d_flags & DCACHE_NFSFS_RENAMED)
4954260f7c7SSage Weil 		return -EBUSY;
4964260f7c7SSage Weil 	return 0;
4974260f7c7SSage Weil }
4984260f7c7SSage Weil 
499cb8e7090SChristoph Hellwig /* copy of may_create in fs/namei.c() */
500cb8e7090SChristoph Hellwig static inline int btrfs_may_create(struct inode *dir, struct dentry *child)
501cb8e7090SChristoph Hellwig {
502cb8e7090SChristoph Hellwig 	if (child->d_inode)
503cb8e7090SChristoph Hellwig 		return -EEXIST;
504cb8e7090SChristoph Hellwig 	if (IS_DEADDIR(dir))
505cb8e7090SChristoph Hellwig 		return -ENOENT;
506cb8e7090SChristoph Hellwig 	return inode_permission(dir, MAY_WRITE | MAY_EXEC);
507cb8e7090SChristoph Hellwig }
508cb8e7090SChristoph Hellwig 
509cb8e7090SChristoph Hellwig /*
510cb8e7090SChristoph Hellwig  * Create a new subvolume below @parent.  This is largely modeled after
511cb8e7090SChristoph Hellwig  * sys_mkdirat and vfs_mkdir, but we only do a single component lookup
512cb8e7090SChristoph Hellwig  * inside this filesystem so it's quite a bit simpler.
513cb8e7090SChristoph Hellwig  */
51476dda93cSYan, Zheng static noinline int btrfs_mksubvol(struct path *parent,
51576dda93cSYan, Zheng 				   char *name, int namelen,
51672fd032eSSage Weil 				   struct btrfs_root *snap_src,
517b83cc969SLi Zefan 				   u64 *async_transid, bool readonly)
518cb8e7090SChristoph Hellwig {
51976dda93cSYan, Zheng 	struct inode *dir  = parent->dentry->d_inode;
520cb8e7090SChristoph Hellwig 	struct dentry *dentry;
521cb8e7090SChristoph Hellwig 	int error;
522cb8e7090SChristoph Hellwig 
52376dda93cSYan, Zheng 	mutex_lock_nested(&dir->i_mutex, I_MUTEX_PARENT);
524cb8e7090SChristoph Hellwig 
525cb8e7090SChristoph Hellwig 	dentry = lookup_one_len(name, parent->dentry, namelen);
526cb8e7090SChristoph Hellwig 	error = PTR_ERR(dentry);
527cb8e7090SChristoph Hellwig 	if (IS_ERR(dentry))
528cb8e7090SChristoph Hellwig 		goto out_unlock;
529cb8e7090SChristoph Hellwig 
530cb8e7090SChristoph Hellwig 	error = -EEXIST;
531cb8e7090SChristoph Hellwig 	if (dentry->d_inode)
532cb8e7090SChristoph Hellwig 		goto out_dput;
533cb8e7090SChristoph Hellwig 
534cb8e7090SChristoph Hellwig 	error = mnt_want_write(parent->mnt);
535cb8e7090SChristoph Hellwig 	if (error)
536cb8e7090SChristoph Hellwig 		goto out_dput;
537cb8e7090SChristoph Hellwig 
53876dda93cSYan, Zheng 	error = btrfs_may_create(dir, dentry);
539cb8e7090SChristoph Hellwig 	if (error)
540cb8e7090SChristoph Hellwig 		goto out_drop_write;
541cb8e7090SChristoph Hellwig 
54276dda93cSYan, Zheng 	down_read(&BTRFS_I(dir)->root->fs_info->subvol_sem);
54376dda93cSYan, Zheng 
54476dda93cSYan, Zheng 	if (btrfs_root_refs(&BTRFS_I(dir)->root->root_item) == 0)
54576dda93cSYan, Zheng 		goto out_up_read;
54676dda93cSYan, Zheng 
5473de4586cSChris Mason 	if (snap_src) {
54872fd032eSSage Weil 		error = create_snapshot(snap_src, dentry,
549b83cc969SLi Zefan 					name, namelen, async_transid, readonly);
5503de4586cSChris Mason 	} else {
55176dda93cSYan, Zheng 		error = create_subvol(BTRFS_I(dir)->root, dentry,
55272fd032eSSage Weil 				      name, namelen, async_transid);
5533de4586cSChris Mason 	}
55476dda93cSYan, Zheng 	if (!error)
55576dda93cSYan, Zheng 		fsnotify_mkdir(dir, dentry);
55676dda93cSYan, Zheng out_up_read:
55776dda93cSYan, Zheng 	up_read(&BTRFS_I(dir)->root->fs_info->subvol_sem);
558cb8e7090SChristoph Hellwig out_drop_write:
559cb8e7090SChristoph Hellwig 	mnt_drop_write(parent->mnt);
560cb8e7090SChristoph Hellwig out_dput:
561cb8e7090SChristoph Hellwig 	dput(dentry);
562cb8e7090SChristoph Hellwig out_unlock:
56376dda93cSYan, Zheng 	mutex_unlock(&dir->i_mutex);
564cb8e7090SChristoph Hellwig 	return error;
565cb8e7090SChristoph Hellwig }
566cb8e7090SChristoph Hellwig 
567940100a4SChris Mason static int should_defrag_range(struct inode *inode, u64 start, u64 len,
5681e701a32SChris Mason 			       int thresh, u64 *last_len, u64 *skip,
5691e701a32SChris Mason 			       u64 *defrag_end)
570940100a4SChris Mason {
571940100a4SChris Mason 	struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
572940100a4SChris Mason 	struct extent_map *em = NULL;
573940100a4SChris Mason 	struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
574940100a4SChris Mason 	int ret = 1;
575940100a4SChris Mason 
5761e701a32SChris Mason 
5771e701a32SChris Mason 	if (thresh == 0)
5781e701a32SChris Mason 		thresh = 256 * 1024;
5791e701a32SChris Mason 
580940100a4SChris Mason 	/*
581940100a4SChris Mason 	 * make sure that once we start defragging and extent, we keep on
582940100a4SChris Mason 	 * defragging it
583940100a4SChris Mason 	 */
584940100a4SChris Mason 	if (start < *defrag_end)
585940100a4SChris Mason 		return 1;
586940100a4SChris Mason 
587940100a4SChris Mason 	*skip = 0;
588940100a4SChris Mason 
589940100a4SChris Mason 	/*
590940100a4SChris Mason 	 * hopefully we have this extent in the tree already, try without
591940100a4SChris Mason 	 * the full extent lock
592940100a4SChris Mason 	 */
593940100a4SChris Mason 	read_lock(&em_tree->lock);
594940100a4SChris Mason 	em = lookup_extent_mapping(em_tree, start, len);
595940100a4SChris Mason 	read_unlock(&em_tree->lock);
596940100a4SChris Mason 
597940100a4SChris Mason 	if (!em) {
598940100a4SChris Mason 		/* get the big lock and read metadata off disk */
599940100a4SChris Mason 		lock_extent(io_tree, start, start + len - 1, GFP_NOFS);
600940100a4SChris Mason 		em = btrfs_get_extent(inode, NULL, 0, start, len, 0);
601940100a4SChris Mason 		unlock_extent(io_tree, start, start + len - 1, GFP_NOFS);
602940100a4SChris Mason 
6036cf8bfbfSDan Carpenter 		if (IS_ERR(em))
604940100a4SChris Mason 			return 0;
605940100a4SChris Mason 	}
606940100a4SChris Mason 
607940100a4SChris Mason 	/* this will cover holes, and inline extents */
608940100a4SChris Mason 	if (em->block_start >= EXTENT_MAP_LAST_BYTE)
609940100a4SChris Mason 		ret = 0;
610940100a4SChris Mason 
611940100a4SChris Mason 	/*
612940100a4SChris Mason 	 * we hit a real extent, if it is big don't bother defragging it again
613940100a4SChris Mason 	 */
6141e701a32SChris Mason 	if ((*last_len == 0 || *last_len >= thresh) && em->len >= thresh)
615940100a4SChris Mason 		ret = 0;
616940100a4SChris Mason 
617940100a4SChris Mason 	/*
618940100a4SChris Mason 	 * last_len ends up being a counter of how many bytes we've defragged.
619940100a4SChris Mason 	 * every time we choose not to defrag an extent, we reset *last_len
620940100a4SChris Mason 	 * so that the next tiny extent will force a defrag.
621940100a4SChris Mason 	 *
622940100a4SChris Mason 	 * The end result of this is that tiny extents before a single big
623940100a4SChris Mason 	 * extent will force at least part of that big extent to be defragged.
624940100a4SChris Mason 	 */
625940100a4SChris Mason 	if (ret) {
626940100a4SChris Mason 		*last_len += len;
627940100a4SChris Mason 		*defrag_end = extent_map_end(em);
628940100a4SChris Mason 	} else {
629940100a4SChris Mason 		*last_len = 0;
630940100a4SChris Mason 		*skip = extent_map_end(em);
631940100a4SChris Mason 		*defrag_end = 0;
632940100a4SChris Mason 	}
633940100a4SChris Mason 
634940100a4SChris Mason 	free_extent_map(em);
635940100a4SChris Mason 	return ret;
636940100a4SChris Mason }
637940100a4SChris Mason 
6381e701a32SChris Mason static int btrfs_defrag_file(struct file *file,
6391e701a32SChris Mason 			     struct btrfs_ioctl_defrag_range_args *range)
640f46b5a66SChristoph Hellwig {
641f46b5a66SChristoph Hellwig 	struct inode *inode = fdentry(file)->d_inode;
642f46b5a66SChristoph Hellwig 	struct btrfs_root *root = BTRFS_I(inode)->root;
643f46b5a66SChristoph Hellwig 	struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
6443eaa2885SChris Mason 	struct btrfs_ordered_extent *ordered;
645f46b5a66SChristoph Hellwig 	struct page *page;
646f46b5a66SChristoph Hellwig 	unsigned long last_index;
647f46b5a66SChristoph Hellwig 	unsigned long ra_pages = root->fs_info->bdi.ra_pages;
648f46b5a66SChristoph Hellwig 	unsigned long total_read = 0;
649f46b5a66SChristoph Hellwig 	u64 page_start;
650f46b5a66SChristoph Hellwig 	u64 page_end;
651940100a4SChris Mason 	u64 last_len = 0;
652940100a4SChris Mason 	u64 skip = 0;
653940100a4SChris Mason 	u64 defrag_end = 0;
654f46b5a66SChristoph Hellwig 	unsigned long i;
655f46b5a66SChristoph Hellwig 	int ret;
656f46b5a66SChristoph Hellwig 
657940100a4SChris Mason 	if (inode->i_size == 0)
658940100a4SChris Mason 		return 0;
659f46b5a66SChristoph Hellwig 
6601e701a32SChris Mason 	if (range->start + range->len > range->start) {
6611e701a32SChris Mason 		last_index = min_t(u64, inode->i_size - 1,
6621e701a32SChris Mason 			 range->start + range->len - 1) >> PAGE_CACHE_SHIFT;
6631e701a32SChris Mason 	} else {
664940100a4SChris Mason 		last_index = (inode->i_size - 1) >> PAGE_CACHE_SHIFT;
6651e701a32SChris Mason 	}
6661e701a32SChris Mason 
6671e701a32SChris Mason 	i = range->start >> PAGE_CACHE_SHIFT;
668940100a4SChris Mason 	while (i <= last_index) {
669940100a4SChris Mason 		if (!should_defrag_range(inode, (u64)i << PAGE_CACHE_SHIFT,
6701e701a32SChris Mason 					PAGE_CACHE_SIZE,
6711e701a32SChris Mason 					range->extent_thresh,
6721e701a32SChris Mason 					&last_len, &skip,
673940100a4SChris Mason 					&defrag_end)) {
674940100a4SChris Mason 			unsigned long next;
675940100a4SChris Mason 			/*
676940100a4SChris Mason 			 * the should_defrag function tells us how much to skip
677940100a4SChris Mason 			 * bump our counter by the suggested amount
678940100a4SChris Mason 			 */
679940100a4SChris Mason 			next = (skip + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
680940100a4SChris Mason 			i = max(i + 1, next);
681940100a4SChris Mason 			continue;
682940100a4SChris Mason 		}
683940100a4SChris Mason 
684f46b5a66SChristoph Hellwig 		if (total_read % ra_pages == 0) {
685f46b5a66SChristoph Hellwig 			btrfs_force_ra(inode->i_mapping, &file->f_ra, file, i,
686f46b5a66SChristoph Hellwig 				       min(last_index, i + ra_pages - 1));
687f46b5a66SChristoph Hellwig 		}
688f46b5a66SChristoph Hellwig 		total_read++;
689940100a4SChris Mason 		mutex_lock(&inode->i_mutex);
6901e701a32SChris Mason 		if (range->flags & BTRFS_DEFRAG_RANGE_COMPRESS)
6911e701a32SChris Mason 			BTRFS_I(inode)->force_compress = 1;
692940100a4SChris Mason 
6930ca1f7ceSYan, Zheng 		ret  = btrfs_delalloc_reserve_space(inode, PAGE_CACHE_SIZE);
6940ca1f7ceSYan, Zheng 		if (ret)
6950ca1f7ceSYan, Zheng 			goto err_unlock;
6963eaa2885SChris Mason again:
697940100a4SChris Mason 		if (inode->i_size == 0 ||
698940100a4SChris Mason 		    i > ((inode->i_size - 1) >> PAGE_CACHE_SHIFT)) {
699940100a4SChris Mason 			ret = 0;
700940100a4SChris Mason 			goto err_reservations;
701940100a4SChris Mason 		}
702940100a4SChris Mason 
703f46b5a66SChristoph Hellwig 		page = grab_cache_page(inode->i_mapping, i);
7040ca1f7ceSYan, Zheng 		if (!page) {
7050ca1f7ceSYan, Zheng 			ret = -ENOMEM;
706940100a4SChris Mason 			goto err_reservations;
7070ca1f7ceSYan, Zheng 		}
708940100a4SChris Mason 
709f46b5a66SChristoph Hellwig 		if (!PageUptodate(page)) {
710f46b5a66SChristoph Hellwig 			btrfs_readpage(NULL, page);
711f46b5a66SChristoph Hellwig 			lock_page(page);
712f46b5a66SChristoph Hellwig 			if (!PageUptodate(page)) {
713f46b5a66SChristoph Hellwig 				unlock_page(page);
714f46b5a66SChristoph Hellwig 				page_cache_release(page);
7150ca1f7ceSYan, Zheng 				ret = -EIO;
716940100a4SChris Mason 				goto err_reservations;
717f46b5a66SChristoph Hellwig 			}
718f46b5a66SChristoph Hellwig 		}
719f46b5a66SChristoph Hellwig 
720940100a4SChris Mason 		if (page->mapping != inode->i_mapping) {
721940100a4SChris Mason 			unlock_page(page);
722940100a4SChris Mason 			page_cache_release(page);
723940100a4SChris Mason 			goto again;
724940100a4SChris Mason 		}
725940100a4SChris Mason 
726f46b5a66SChristoph Hellwig 		wait_on_page_writeback(page);
727f46b5a66SChristoph Hellwig 
728940100a4SChris Mason 		if (PageDirty(page)) {
7290ca1f7ceSYan, Zheng 			btrfs_delalloc_release_space(inode, PAGE_CACHE_SIZE);
730940100a4SChris Mason 			goto loop_unlock;
731940100a4SChris Mason 		}
732940100a4SChris Mason 
733f46b5a66SChristoph Hellwig 		page_start = (u64)page->index << PAGE_CACHE_SHIFT;
734f46b5a66SChristoph Hellwig 		page_end = page_start + PAGE_CACHE_SIZE - 1;
735f46b5a66SChristoph Hellwig 		lock_extent(io_tree, page_start, page_end, GFP_NOFS);
7363eaa2885SChris Mason 
7373eaa2885SChris Mason 		ordered = btrfs_lookup_ordered_extent(inode, page_start);
7383eaa2885SChris Mason 		if (ordered) {
7393eaa2885SChris Mason 			unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
7403eaa2885SChris Mason 			unlock_page(page);
7413eaa2885SChris Mason 			page_cache_release(page);
7423eaa2885SChris Mason 			btrfs_start_ordered_extent(inode, ordered, 1);
7433eaa2885SChris Mason 			btrfs_put_ordered_extent(ordered);
7443eaa2885SChris Mason 			goto again;
7453eaa2885SChris Mason 		}
7463eaa2885SChris Mason 		set_page_extent_mapped(page);
7473eaa2885SChris Mason 
748f87f057bSChris Mason 		/*
749f87f057bSChris Mason 		 * this makes sure page_mkwrite is called on the
750f87f057bSChris Mason 		 * page if it is dirtied again later
751f87f057bSChris Mason 		 */
752f87f057bSChris Mason 		clear_page_dirty_for_io(page);
753940100a4SChris Mason 		clear_extent_bits(&BTRFS_I(inode)->io_tree, page_start,
754940100a4SChris Mason 				  page_end, EXTENT_DIRTY | EXTENT_DELALLOC |
755940100a4SChris Mason 				  EXTENT_DO_ACCOUNTING, GFP_NOFS);
756f87f057bSChris Mason 
7572ac55d41SJosef Bacik 		btrfs_set_extent_delalloc(inode, page_start, page_end, NULL);
758940100a4SChris Mason 		ClearPageChecked(page);
759f46b5a66SChristoph Hellwig 		set_page_dirty(page);
760a1ed835eSChris Mason 		unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
761940100a4SChris Mason 
762940100a4SChris Mason loop_unlock:
763f46b5a66SChristoph Hellwig 		unlock_page(page);
764f46b5a66SChristoph Hellwig 		page_cache_release(page);
765940100a4SChris Mason 		mutex_unlock(&inode->i_mutex);
766940100a4SChris Mason 
767f46b5a66SChristoph Hellwig 		balance_dirty_pages_ratelimited_nr(inode->i_mapping, 1);
768940100a4SChris Mason 		i++;
769f46b5a66SChristoph Hellwig 	}
770f46b5a66SChristoph Hellwig 
7711e701a32SChris Mason 	if ((range->flags & BTRFS_DEFRAG_RANGE_START_IO))
7721e701a32SChris Mason 		filemap_flush(inode->i_mapping);
7731e701a32SChris Mason 
7741e701a32SChris Mason 	if ((range->flags & BTRFS_DEFRAG_RANGE_COMPRESS)) {
7751e701a32SChris Mason 		/* the filemap_flush will queue IO into the worker threads, but
7761e701a32SChris Mason 		 * we have to make sure the IO is actually started and that
7771e701a32SChris Mason 		 * ordered extents get created before we return
7781e701a32SChris Mason 		 */
7791e701a32SChris Mason 		atomic_inc(&root->fs_info->async_submit_draining);
7801e701a32SChris Mason 		while (atomic_read(&root->fs_info->nr_async_submits) ||
7811e701a32SChris Mason 		      atomic_read(&root->fs_info->async_delalloc_pages)) {
7821e701a32SChris Mason 			wait_event(root->fs_info->async_submit_wait,
7831e701a32SChris Mason 			   (atomic_read(&root->fs_info->nr_async_submits) == 0 &&
7841e701a32SChris Mason 			    atomic_read(&root->fs_info->async_delalloc_pages) == 0));
7851e701a32SChris Mason 		}
7861e701a32SChris Mason 		atomic_dec(&root->fs_info->async_submit_draining);
7871e701a32SChris Mason 
7881e701a32SChris Mason 		mutex_lock(&inode->i_mutex);
7891e701a32SChris Mason 		BTRFS_I(inode)->force_compress = 0;
7901e701a32SChris Mason 		mutex_unlock(&inode->i_mutex);
7911e701a32SChris Mason 	}
7921e701a32SChris Mason 
793f46b5a66SChristoph Hellwig 	return 0;
794940100a4SChris Mason 
795940100a4SChris Mason err_reservations:
7960ca1f7ceSYan, Zheng 	btrfs_delalloc_release_space(inode, PAGE_CACHE_SIZE);
7970ca1f7ceSYan, Zheng err_unlock:
798940100a4SChris Mason 	mutex_unlock(&inode->i_mutex);
799940100a4SChris Mason 	return ret;
800f46b5a66SChristoph Hellwig }
801f46b5a66SChristoph Hellwig 
80276dda93cSYan, Zheng static noinline int btrfs_ioctl_resize(struct btrfs_root *root,
80376dda93cSYan, Zheng 					void __user *arg)
804f46b5a66SChristoph Hellwig {
805f46b5a66SChristoph Hellwig 	u64 new_size;
806f46b5a66SChristoph Hellwig 	u64 old_size;
807f46b5a66SChristoph Hellwig 	u64 devid = 1;
808f46b5a66SChristoph Hellwig 	struct btrfs_ioctl_vol_args *vol_args;
809f46b5a66SChristoph Hellwig 	struct btrfs_trans_handle *trans;
810f46b5a66SChristoph Hellwig 	struct btrfs_device *device = NULL;
811f46b5a66SChristoph Hellwig 	char *sizestr;
812f46b5a66SChristoph Hellwig 	char *devstr = NULL;
813f46b5a66SChristoph Hellwig 	int ret = 0;
814f46b5a66SChristoph Hellwig 	int mod = 0;
815f46b5a66SChristoph Hellwig 
816c146afadSYan Zheng 	if (root->fs_info->sb->s_flags & MS_RDONLY)
817c146afadSYan Zheng 		return -EROFS;
818c146afadSYan Zheng 
819e441d54dSChris Mason 	if (!capable(CAP_SYS_ADMIN))
820e441d54dSChris Mason 		return -EPERM;
821e441d54dSChris Mason 
822dae7b665SLi Zefan 	vol_args = memdup_user(arg, sizeof(*vol_args));
823dae7b665SLi Zefan 	if (IS_ERR(vol_args))
824dae7b665SLi Zefan 		return PTR_ERR(vol_args);
8255516e595SMark Fasheh 
8265516e595SMark Fasheh 	vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
827f46b5a66SChristoph Hellwig 
8287d9eb12cSChris Mason 	mutex_lock(&root->fs_info->volume_mutex);
829f46b5a66SChristoph Hellwig 	sizestr = vol_args->name;
830f46b5a66SChristoph Hellwig 	devstr = strchr(sizestr, ':');
831f46b5a66SChristoph Hellwig 	if (devstr) {
832f46b5a66SChristoph Hellwig 		char *end;
833f46b5a66SChristoph Hellwig 		sizestr = devstr + 1;
834f46b5a66SChristoph Hellwig 		*devstr = '\0';
835f46b5a66SChristoph Hellwig 		devstr = vol_args->name;
836f46b5a66SChristoph Hellwig 		devid = simple_strtoull(devstr, &end, 10);
83721380931SJoel Becker 		printk(KERN_INFO "resizing devid %llu\n",
83821380931SJoel Becker 		       (unsigned long long)devid);
839f46b5a66SChristoph Hellwig 	}
8402b82032cSYan Zheng 	device = btrfs_find_device(root, devid, NULL, NULL);
841f46b5a66SChristoph Hellwig 	if (!device) {
84221380931SJoel Becker 		printk(KERN_INFO "resizer unable to find device %llu\n",
84321380931SJoel Becker 		       (unsigned long long)devid);
844f46b5a66SChristoph Hellwig 		ret = -EINVAL;
845f46b5a66SChristoph Hellwig 		goto out_unlock;
846f46b5a66SChristoph Hellwig 	}
847f46b5a66SChristoph Hellwig 	if (!strcmp(sizestr, "max"))
848f46b5a66SChristoph Hellwig 		new_size = device->bdev->bd_inode->i_size;
849f46b5a66SChristoph Hellwig 	else {
850f46b5a66SChristoph Hellwig 		if (sizestr[0] == '-') {
851f46b5a66SChristoph Hellwig 			mod = -1;
852f46b5a66SChristoph Hellwig 			sizestr++;
853f46b5a66SChristoph Hellwig 		} else if (sizestr[0] == '+') {
854f46b5a66SChristoph Hellwig 			mod = 1;
855f46b5a66SChristoph Hellwig 			sizestr++;
856f46b5a66SChristoph Hellwig 		}
85791748467SAkinobu Mita 		new_size = memparse(sizestr, NULL);
858f46b5a66SChristoph Hellwig 		if (new_size == 0) {
859f46b5a66SChristoph Hellwig 			ret = -EINVAL;
860f46b5a66SChristoph Hellwig 			goto out_unlock;
861f46b5a66SChristoph Hellwig 		}
862f46b5a66SChristoph Hellwig 	}
863f46b5a66SChristoph Hellwig 
864f46b5a66SChristoph Hellwig 	old_size = device->total_bytes;
865f46b5a66SChristoph Hellwig 
866f46b5a66SChristoph Hellwig 	if (mod < 0) {
867f46b5a66SChristoph Hellwig 		if (new_size > old_size) {
868f46b5a66SChristoph Hellwig 			ret = -EINVAL;
869f46b5a66SChristoph Hellwig 			goto out_unlock;
870f46b5a66SChristoph Hellwig 		}
871f46b5a66SChristoph Hellwig 		new_size = old_size - new_size;
872f46b5a66SChristoph Hellwig 	} else if (mod > 0) {
873f46b5a66SChristoph Hellwig 		new_size = old_size + new_size;
874f46b5a66SChristoph Hellwig 	}
875f46b5a66SChristoph Hellwig 
876f46b5a66SChristoph Hellwig 	if (new_size < 256 * 1024 * 1024) {
877f46b5a66SChristoph Hellwig 		ret = -EINVAL;
878f46b5a66SChristoph Hellwig 		goto out_unlock;
879f46b5a66SChristoph Hellwig 	}
880f46b5a66SChristoph Hellwig 	if (new_size > device->bdev->bd_inode->i_size) {
881f46b5a66SChristoph Hellwig 		ret = -EFBIG;
882f46b5a66SChristoph Hellwig 		goto out_unlock;
883f46b5a66SChristoph Hellwig 	}
884f46b5a66SChristoph Hellwig 
885f46b5a66SChristoph Hellwig 	do_div(new_size, root->sectorsize);
886f46b5a66SChristoph Hellwig 	new_size *= root->sectorsize;
887f46b5a66SChristoph Hellwig 
888f46b5a66SChristoph Hellwig 	printk(KERN_INFO "new size for %s is %llu\n",
889f46b5a66SChristoph Hellwig 		device->name, (unsigned long long)new_size);
890f46b5a66SChristoph Hellwig 
891f46b5a66SChristoph Hellwig 	if (new_size > old_size) {
892a22285a6SYan, Zheng 		trans = btrfs_start_transaction(root, 0);
893f46b5a66SChristoph Hellwig 		ret = btrfs_grow_device(trans, device, new_size);
894f46b5a66SChristoph Hellwig 		btrfs_commit_transaction(trans, root);
895f46b5a66SChristoph Hellwig 	} else {
896f46b5a66SChristoph Hellwig 		ret = btrfs_shrink_device(device, new_size);
897f46b5a66SChristoph Hellwig 	}
898f46b5a66SChristoph Hellwig 
899f46b5a66SChristoph Hellwig out_unlock:
9007d9eb12cSChris Mason 	mutex_unlock(&root->fs_info->volume_mutex);
901f46b5a66SChristoph Hellwig 	kfree(vol_args);
902f46b5a66SChristoph Hellwig 	return ret;
903f46b5a66SChristoph Hellwig }
904f46b5a66SChristoph Hellwig 
90572fd032eSSage Weil static noinline int btrfs_ioctl_snap_create_transid(struct file *file,
90672fd032eSSage Weil 						    char *name,
90772fd032eSSage Weil 						    unsigned long fd,
90872fd032eSSage Weil 						    int subvol,
909b83cc969SLi Zefan 						    u64 *transid,
910b83cc969SLi Zefan 						    bool readonly)
911f46b5a66SChristoph Hellwig {
912cb8e7090SChristoph Hellwig 	struct btrfs_root *root = BTRFS_I(fdentry(file)->d_inode)->root;
9133de4586cSChris Mason 	struct file *src_file;
914f46b5a66SChristoph Hellwig 	int namelen;
9153de4586cSChris Mason 	int ret = 0;
916f46b5a66SChristoph Hellwig 
917c146afadSYan Zheng 	if (root->fs_info->sb->s_flags & MS_RDONLY)
918c146afadSYan Zheng 		return -EROFS;
919c146afadSYan Zheng 
92072fd032eSSage Weil 	namelen = strlen(name);
92172fd032eSSage Weil 	if (strchr(name, '/')) {
922f46b5a66SChristoph Hellwig 		ret = -EINVAL;
923f46b5a66SChristoph Hellwig 		goto out;
924f46b5a66SChristoph Hellwig 	}
925f46b5a66SChristoph Hellwig 
9263de4586cSChris Mason 	if (subvol) {
92772fd032eSSage Weil 		ret = btrfs_mksubvol(&file->f_path, name, namelen,
928b83cc969SLi Zefan 				     NULL, transid, readonly);
929cb8e7090SChristoph Hellwig 	} else {
9303de4586cSChris Mason 		struct inode *src_inode;
93172fd032eSSage Weil 		src_file = fget(fd);
9323de4586cSChris Mason 		if (!src_file) {
9333de4586cSChris Mason 			ret = -EINVAL;
9343de4586cSChris Mason 			goto out;
9353de4586cSChris Mason 		}
9363de4586cSChris Mason 
9373de4586cSChris Mason 		src_inode = src_file->f_path.dentry->d_inode;
9383de4586cSChris Mason 		if (src_inode->i_sb != file->f_path.dentry->d_inode->i_sb) {
939d397712bSChris Mason 			printk(KERN_INFO "btrfs: Snapshot src from "
940d397712bSChris Mason 			       "another FS\n");
9413de4586cSChris Mason 			ret = -EINVAL;
9423de4586cSChris Mason 			fput(src_file);
9433de4586cSChris Mason 			goto out;
9443de4586cSChris Mason 		}
94572fd032eSSage Weil 		ret = btrfs_mksubvol(&file->f_path, name, namelen,
94672fd032eSSage Weil 				     BTRFS_I(src_inode)->root,
947b83cc969SLi Zefan 				     transid, readonly);
9483de4586cSChris Mason 		fput(src_file);
949cb8e7090SChristoph Hellwig 	}
950f46b5a66SChristoph Hellwig out:
95172fd032eSSage Weil 	return ret;
95272fd032eSSage Weil }
95372fd032eSSage Weil 
95472fd032eSSage Weil static noinline int btrfs_ioctl_snap_create(struct file *file,
955fa0d2b9bSLi Zefan 					    void __user *arg, int subvol)
95672fd032eSSage Weil {
957fa0d2b9bSLi Zefan 	struct btrfs_ioctl_vol_args *vol_args;
95872fd032eSSage Weil 	int ret;
95972fd032eSSage Weil 
960fa0d2b9bSLi Zefan 	vol_args = memdup_user(arg, sizeof(*vol_args));
961fa0d2b9bSLi Zefan 	if (IS_ERR(vol_args))
962fa0d2b9bSLi Zefan 		return PTR_ERR(vol_args);
963fa0d2b9bSLi Zefan 	vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
964fa0d2b9bSLi Zefan 
965fa0d2b9bSLi Zefan 	ret = btrfs_ioctl_snap_create_transid(file, vol_args->name,
966b83cc969SLi Zefan 					      vol_args->fd, subvol,
967b83cc969SLi Zefan 					      NULL, false);
968fa0d2b9bSLi Zefan 
969fa0d2b9bSLi Zefan 	kfree(vol_args);
970fa0d2b9bSLi Zefan 	return ret;
971fa0d2b9bSLi Zefan }
972fa0d2b9bSLi Zefan 
973fa0d2b9bSLi Zefan static noinline int btrfs_ioctl_snap_create_v2(struct file *file,
974fa0d2b9bSLi Zefan 					       void __user *arg, int subvol)
975fa0d2b9bSLi Zefan {
976fa0d2b9bSLi Zefan 	struct btrfs_ioctl_vol_args_v2 *vol_args;
977fa0d2b9bSLi Zefan 	int ret;
978fdfb1e4fSLi Zefan 	u64 transid = 0;
979fdfb1e4fSLi Zefan 	u64 *ptr = NULL;
980b83cc969SLi Zefan 	bool readonly = false;
98172fd032eSSage Weil 
982fa0d2b9bSLi Zefan 	vol_args = memdup_user(arg, sizeof(*vol_args));
983fa0d2b9bSLi Zefan 	if (IS_ERR(vol_args))
984fa0d2b9bSLi Zefan 		return PTR_ERR(vol_args);
985fa0d2b9bSLi Zefan 	vol_args->name[BTRFS_SUBVOL_NAME_MAX] = '\0';
986fdfb1e4fSLi Zefan 
987b83cc969SLi Zefan 	if (vol_args->flags &
988b83cc969SLi Zefan 	    ~(BTRFS_SUBVOL_CREATE_ASYNC | BTRFS_SUBVOL_RDONLY)) {
989b83cc969SLi Zefan 		ret = -EOPNOTSUPP;
990fdfb1e4fSLi Zefan 		goto out;
991fdfb1e4fSLi Zefan 	}
992fdfb1e4fSLi Zefan 
993fa0d2b9bSLi Zefan 	if (vol_args->flags & BTRFS_SUBVOL_CREATE_ASYNC)
994fdfb1e4fSLi Zefan 		ptr = &transid;
995b83cc969SLi Zefan 	if (vol_args->flags & BTRFS_SUBVOL_RDONLY)
996b83cc969SLi Zefan 		readonly = true;
99775eaa0e2SSage Weil 
998fa0d2b9bSLi Zefan 	ret = btrfs_ioctl_snap_create_transid(file, vol_args->name,
999b83cc969SLi Zefan 					      vol_args->fd, subvol,
1000b83cc969SLi Zefan 					      ptr, readonly);
100175eaa0e2SSage Weil 
1002fdfb1e4fSLi Zefan 	if (ret == 0 && ptr &&
100375eaa0e2SSage Weil 	    copy_to_user(arg +
1004fdfb1e4fSLi Zefan 			 offsetof(struct btrfs_ioctl_vol_args_v2,
1005fdfb1e4fSLi Zefan 				  transid), ptr, sizeof(*ptr)))
100675eaa0e2SSage Weil 		ret = -EFAULT;
1007fdfb1e4fSLi Zefan out:
1008f46b5a66SChristoph Hellwig 	kfree(vol_args);
1009f46b5a66SChristoph Hellwig 	return ret;
1010f46b5a66SChristoph Hellwig }
1011f46b5a66SChristoph Hellwig 
1012*0caa102dSLi Zefan static noinline int btrfs_ioctl_subvol_getflags(struct file *file,
1013*0caa102dSLi Zefan 						void __user *arg)
1014*0caa102dSLi Zefan {
1015*0caa102dSLi Zefan 	struct inode *inode = fdentry(file)->d_inode;
1016*0caa102dSLi Zefan 	struct btrfs_root *root = BTRFS_I(inode)->root;
1017*0caa102dSLi Zefan 	int ret = 0;
1018*0caa102dSLi Zefan 	u64 flags = 0;
1019*0caa102dSLi Zefan 
1020*0caa102dSLi Zefan 	if (inode->i_ino != BTRFS_FIRST_FREE_OBJECTID)
1021*0caa102dSLi Zefan 		return -EINVAL;
1022*0caa102dSLi Zefan 
1023*0caa102dSLi Zefan 	down_read(&root->fs_info->subvol_sem);
1024*0caa102dSLi Zefan 	if (btrfs_root_readonly(root))
1025*0caa102dSLi Zefan 		flags |= BTRFS_SUBVOL_RDONLY;
1026*0caa102dSLi Zefan 	up_read(&root->fs_info->subvol_sem);
1027*0caa102dSLi Zefan 
1028*0caa102dSLi Zefan 	if (copy_to_user(arg, &flags, sizeof(flags)))
1029*0caa102dSLi Zefan 		ret = -EFAULT;
1030*0caa102dSLi Zefan 
1031*0caa102dSLi Zefan 	return ret;
1032*0caa102dSLi Zefan }
1033*0caa102dSLi Zefan 
1034*0caa102dSLi Zefan static noinline int btrfs_ioctl_subvol_setflags(struct file *file,
1035*0caa102dSLi Zefan 					      void __user *arg)
1036*0caa102dSLi Zefan {
1037*0caa102dSLi Zefan 	struct inode *inode = fdentry(file)->d_inode;
1038*0caa102dSLi Zefan 	struct btrfs_root *root = BTRFS_I(inode)->root;
1039*0caa102dSLi Zefan 	struct btrfs_trans_handle *trans;
1040*0caa102dSLi Zefan 	u64 root_flags;
1041*0caa102dSLi Zefan 	u64 flags;
1042*0caa102dSLi Zefan 	int ret = 0;
1043*0caa102dSLi Zefan 
1044*0caa102dSLi Zefan 	if (root->fs_info->sb->s_flags & MS_RDONLY)
1045*0caa102dSLi Zefan 		return -EROFS;
1046*0caa102dSLi Zefan 
1047*0caa102dSLi Zefan 	if (inode->i_ino != BTRFS_FIRST_FREE_OBJECTID)
1048*0caa102dSLi Zefan 		return -EINVAL;
1049*0caa102dSLi Zefan 
1050*0caa102dSLi Zefan 	if (copy_from_user(&flags, arg, sizeof(flags)))
1051*0caa102dSLi Zefan 		return -EFAULT;
1052*0caa102dSLi Zefan 
1053*0caa102dSLi Zefan 	if (flags & ~BTRFS_SUBVOL_CREATE_ASYNC)
1054*0caa102dSLi Zefan 		return -EINVAL;
1055*0caa102dSLi Zefan 
1056*0caa102dSLi Zefan 	if (flags & ~BTRFS_SUBVOL_RDONLY)
1057*0caa102dSLi Zefan 		return -EOPNOTSUPP;
1058*0caa102dSLi Zefan 
1059*0caa102dSLi Zefan 	down_write(&root->fs_info->subvol_sem);
1060*0caa102dSLi Zefan 
1061*0caa102dSLi Zefan 	/* nothing to do */
1062*0caa102dSLi Zefan 	if (!!(flags & BTRFS_SUBVOL_RDONLY) == btrfs_root_readonly(root))
1063*0caa102dSLi Zefan 		goto out;
1064*0caa102dSLi Zefan 
1065*0caa102dSLi Zefan 	root_flags = btrfs_root_flags(&root->root_item);
1066*0caa102dSLi Zefan 	if (flags & BTRFS_SUBVOL_RDONLY)
1067*0caa102dSLi Zefan 		btrfs_set_root_flags(&root->root_item,
1068*0caa102dSLi Zefan 				     root_flags | BTRFS_ROOT_SUBVOL_RDONLY);
1069*0caa102dSLi Zefan 	else
1070*0caa102dSLi Zefan 		btrfs_set_root_flags(&root->root_item,
1071*0caa102dSLi Zefan 				     root_flags & ~BTRFS_ROOT_SUBVOL_RDONLY);
1072*0caa102dSLi Zefan 
1073*0caa102dSLi Zefan 	trans = btrfs_start_transaction(root, 1);
1074*0caa102dSLi Zefan 	if (IS_ERR(trans)) {
1075*0caa102dSLi Zefan 		ret = PTR_ERR(trans);
1076*0caa102dSLi Zefan 		goto out_reset;
1077*0caa102dSLi Zefan 	}
1078*0caa102dSLi Zefan 
1079*0caa102dSLi Zefan 	ret = btrfs_update_root(trans, root,
1080*0caa102dSLi Zefan 				&root->root_key, &root->root_item);
1081*0caa102dSLi Zefan 
1082*0caa102dSLi Zefan 	btrfs_commit_transaction(trans, root);
1083*0caa102dSLi Zefan out_reset:
1084*0caa102dSLi Zefan 	if (ret)
1085*0caa102dSLi Zefan 		btrfs_set_root_flags(&root->root_item, root_flags);
1086*0caa102dSLi Zefan out:
1087*0caa102dSLi Zefan 	up_write(&root->fs_info->subvol_sem);
1088*0caa102dSLi Zefan 	return ret;
1089*0caa102dSLi Zefan }
1090*0caa102dSLi Zefan 
109176dda93cSYan, Zheng /*
109276dda93cSYan, Zheng  * helper to check if the subvolume references other subvolumes
109376dda93cSYan, Zheng  */
109476dda93cSYan, Zheng static noinline int may_destroy_subvol(struct btrfs_root *root)
109576dda93cSYan, Zheng {
109676dda93cSYan, Zheng 	struct btrfs_path *path;
109776dda93cSYan, Zheng 	struct btrfs_key key;
109876dda93cSYan, Zheng 	int ret;
109976dda93cSYan, Zheng 
110076dda93cSYan, Zheng 	path = btrfs_alloc_path();
110176dda93cSYan, Zheng 	if (!path)
110276dda93cSYan, Zheng 		return -ENOMEM;
110376dda93cSYan, Zheng 
110476dda93cSYan, Zheng 	key.objectid = root->root_key.objectid;
110576dda93cSYan, Zheng 	key.type = BTRFS_ROOT_REF_KEY;
110676dda93cSYan, Zheng 	key.offset = (u64)-1;
110776dda93cSYan, Zheng 
110876dda93cSYan, Zheng 	ret = btrfs_search_slot(NULL, root->fs_info->tree_root,
110976dda93cSYan, Zheng 				&key, path, 0, 0);
111076dda93cSYan, Zheng 	if (ret < 0)
111176dda93cSYan, Zheng 		goto out;
111276dda93cSYan, Zheng 	BUG_ON(ret == 0);
111376dda93cSYan, Zheng 
111476dda93cSYan, Zheng 	ret = 0;
111576dda93cSYan, Zheng 	if (path->slots[0] > 0) {
111676dda93cSYan, Zheng 		path->slots[0]--;
111776dda93cSYan, Zheng 		btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
111876dda93cSYan, Zheng 		if (key.objectid == root->root_key.objectid &&
111976dda93cSYan, Zheng 		    key.type == BTRFS_ROOT_REF_KEY)
112076dda93cSYan, Zheng 			ret = -ENOTEMPTY;
112176dda93cSYan, Zheng 	}
112276dda93cSYan, Zheng out:
112376dda93cSYan, Zheng 	btrfs_free_path(path);
112476dda93cSYan, Zheng 	return ret;
112576dda93cSYan, Zheng }
112676dda93cSYan, Zheng 
1127ac8e9819SChris Mason static noinline int key_in_sk(struct btrfs_key *key,
1128ac8e9819SChris Mason 			      struct btrfs_ioctl_search_key *sk)
1129ac8e9819SChris Mason {
1130abc6e134SChris Mason 	struct btrfs_key test;
1131abc6e134SChris Mason 	int ret;
1132abc6e134SChris Mason 
1133abc6e134SChris Mason 	test.objectid = sk->min_objectid;
1134abc6e134SChris Mason 	test.type = sk->min_type;
1135abc6e134SChris Mason 	test.offset = sk->min_offset;
1136abc6e134SChris Mason 
1137abc6e134SChris Mason 	ret = btrfs_comp_cpu_keys(key, &test);
1138abc6e134SChris Mason 	if (ret < 0)
1139ac8e9819SChris Mason 		return 0;
1140abc6e134SChris Mason 
1141abc6e134SChris Mason 	test.objectid = sk->max_objectid;
1142abc6e134SChris Mason 	test.type = sk->max_type;
1143abc6e134SChris Mason 	test.offset = sk->max_offset;
1144abc6e134SChris Mason 
1145abc6e134SChris Mason 	ret = btrfs_comp_cpu_keys(key, &test);
1146abc6e134SChris Mason 	if (ret > 0)
1147ac8e9819SChris Mason 		return 0;
1148ac8e9819SChris Mason 	return 1;
1149ac8e9819SChris Mason }
1150ac8e9819SChris Mason 
1151ac8e9819SChris Mason static noinline int copy_to_sk(struct btrfs_root *root,
1152ac8e9819SChris Mason 			       struct btrfs_path *path,
1153ac8e9819SChris Mason 			       struct btrfs_key *key,
1154ac8e9819SChris Mason 			       struct btrfs_ioctl_search_key *sk,
1155ac8e9819SChris Mason 			       char *buf,
1156ac8e9819SChris Mason 			       unsigned long *sk_offset,
1157ac8e9819SChris Mason 			       int *num_found)
1158ac8e9819SChris Mason {
1159ac8e9819SChris Mason 	u64 found_transid;
1160ac8e9819SChris Mason 	struct extent_buffer *leaf;
1161ac8e9819SChris Mason 	struct btrfs_ioctl_search_header sh;
1162ac8e9819SChris Mason 	unsigned long item_off;
1163ac8e9819SChris Mason 	unsigned long item_len;
1164ac8e9819SChris Mason 	int nritems;
1165ac8e9819SChris Mason 	int i;
1166ac8e9819SChris Mason 	int slot;
1167ac8e9819SChris Mason 	int found = 0;
1168ac8e9819SChris Mason 	int ret = 0;
1169ac8e9819SChris Mason 
1170ac8e9819SChris Mason 	leaf = path->nodes[0];
1171ac8e9819SChris Mason 	slot = path->slots[0];
1172ac8e9819SChris Mason 	nritems = btrfs_header_nritems(leaf);
1173ac8e9819SChris Mason 
1174ac8e9819SChris Mason 	if (btrfs_header_generation(leaf) > sk->max_transid) {
1175ac8e9819SChris Mason 		i = nritems;
1176ac8e9819SChris Mason 		goto advance_key;
1177ac8e9819SChris Mason 	}
1178ac8e9819SChris Mason 	found_transid = btrfs_header_generation(leaf);
1179ac8e9819SChris Mason 
1180ac8e9819SChris Mason 	for (i = slot; i < nritems; i++) {
1181ac8e9819SChris Mason 		item_off = btrfs_item_ptr_offset(leaf, i);
1182ac8e9819SChris Mason 		item_len = btrfs_item_size_nr(leaf, i);
1183ac8e9819SChris Mason 
1184ac8e9819SChris Mason 		if (item_len > BTRFS_SEARCH_ARGS_BUFSIZE)
1185ac8e9819SChris Mason 			item_len = 0;
1186ac8e9819SChris Mason 
1187ac8e9819SChris Mason 		if (sizeof(sh) + item_len + *sk_offset >
1188ac8e9819SChris Mason 		    BTRFS_SEARCH_ARGS_BUFSIZE) {
1189ac8e9819SChris Mason 			ret = 1;
1190ac8e9819SChris Mason 			goto overflow;
1191ac8e9819SChris Mason 		}
1192ac8e9819SChris Mason 
1193ac8e9819SChris Mason 		btrfs_item_key_to_cpu(leaf, key, i);
1194ac8e9819SChris Mason 		if (!key_in_sk(key, sk))
1195ac8e9819SChris Mason 			continue;
1196ac8e9819SChris Mason 
1197ac8e9819SChris Mason 		sh.objectid = key->objectid;
1198ac8e9819SChris Mason 		sh.offset = key->offset;
1199ac8e9819SChris Mason 		sh.type = key->type;
1200ac8e9819SChris Mason 		sh.len = item_len;
1201ac8e9819SChris Mason 		sh.transid = found_transid;
1202ac8e9819SChris Mason 
1203ac8e9819SChris Mason 		/* copy search result header */
1204ac8e9819SChris Mason 		memcpy(buf + *sk_offset, &sh, sizeof(sh));
1205ac8e9819SChris Mason 		*sk_offset += sizeof(sh);
1206ac8e9819SChris Mason 
1207ac8e9819SChris Mason 		if (item_len) {
1208ac8e9819SChris Mason 			char *p = buf + *sk_offset;
1209ac8e9819SChris Mason 			/* copy the item */
1210ac8e9819SChris Mason 			read_extent_buffer(leaf, p,
1211ac8e9819SChris Mason 					   item_off, item_len);
1212ac8e9819SChris Mason 			*sk_offset += item_len;
1213ac8e9819SChris Mason 		}
121490fdde14SChris Mason 		found++;
1215ac8e9819SChris Mason 
1216ac8e9819SChris Mason 		if (*num_found >= sk->nr_items)
1217ac8e9819SChris Mason 			break;
1218ac8e9819SChris Mason 	}
1219ac8e9819SChris Mason advance_key:
1220ac8e9819SChris Mason 	ret = 0;
1221abc6e134SChris Mason 	if (key->offset < (u64)-1 && key->offset < sk->max_offset)
1222abc6e134SChris Mason 		key->offset++;
1223abc6e134SChris Mason 	else if (key->type < (u8)-1 && key->type < sk->max_type) {
1224abc6e134SChris Mason 		key->offset = 0;
1225abc6e134SChris Mason 		key->type++;
1226abc6e134SChris Mason 	} else if (key->objectid < (u64)-1 && key->objectid < sk->max_objectid) {
1227abc6e134SChris Mason 		key->offset = 0;
1228abc6e134SChris Mason 		key->type = 0;
1229abc6e134SChris Mason 		key->objectid++;
1230abc6e134SChris Mason 	} else
1231abc6e134SChris Mason 		ret = 1;
1232ac8e9819SChris Mason overflow:
1233ac8e9819SChris Mason 	*num_found += found;
1234ac8e9819SChris Mason 	return ret;
1235ac8e9819SChris Mason }
1236ac8e9819SChris Mason 
1237ac8e9819SChris Mason static noinline int search_ioctl(struct inode *inode,
1238ac8e9819SChris Mason 				 struct btrfs_ioctl_search_args *args)
1239ac8e9819SChris Mason {
1240ac8e9819SChris Mason 	struct btrfs_root *root;
1241ac8e9819SChris Mason 	struct btrfs_key key;
1242ac8e9819SChris Mason 	struct btrfs_key max_key;
1243ac8e9819SChris Mason 	struct btrfs_path *path;
1244ac8e9819SChris Mason 	struct btrfs_ioctl_search_key *sk = &args->key;
1245ac8e9819SChris Mason 	struct btrfs_fs_info *info = BTRFS_I(inode)->root->fs_info;
1246ac8e9819SChris Mason 	int ret;
1247ac8e9819SChris Mason 	int num_found = 0;
1248ac8e9819SChris Mason 	unsigned long sk_offset = 0;
1249ac8e9819SChris Mason 
1250ac8e9819SChris Mason 	path = btrfs_alloc_path();
1251ac8e9819SChris Mason 	if (!path)
1252ac8e9819SChris Mason 		return -ENOMEM;
1253ac8e9819SChris Mason 
1254ac8e9819SChris Mason 	if (sk->tree_id == 0) {
1255ac8e9819SChris Mason 		/* search the root of the inode that was passed */
1256ac8e9819SChris Mason 		root = BTRFS_I(inode)->root;
1257ac8e9819SChris Mason 	} else {
1258ac8e9819SChris Mason 		key.objectid = sk->tree_id;
1259ac8e9819SChris Mason 		key.type = BTRFS_ROOT_ITEM_KEY;
1260ac8e9819SChris Mason 		key.offset = (u64)-1;
1261ac8e9819SChris Mason 		root = btrfs_read_fs_root_no_name(info, &key);
1262ac8e9819SChris Mason 		if (IS_ERR(root)) {
1263ac8e9819SChris Mason 			printk(KERN_ERR "could not find root %llu\n",
1264ac8e9819SChris Mason 			       sk->tree_id);
1265ac8e9819SChris Mason 			btrfs_free_path(path);
1266ac8e9819SChris Mason 			return -ENOENT;
1267ac8e9819SChris Mason 		}
1268ac8e9819SChris Mason 	}
1269ac8e9819SChris Mason 
1270ac8e9819SChris Mason 	key.objectid = sk->min_objectid;
1271ac8e9819SChris Mason 	key.type = sk->min_type;
1272ac8e9819SChris Mason 	key.offset = sk->min_offset;
1273ac8e9819SChris Mason 
1274ac8e9819SChris Mason 	max_key.objectid = sk->max_objectid;
1275ac8e9819SChris Mason 	max_key.type = sk->max_type;
1276ac8e9819SChris Mason 	max_key.offset = sk->max_offset;
1277ac8e9819SChris Mason 
1278ac8e9819SChris Mason 	path->keep_locks = 1;
1279ac8e9819SChris Mason 
1280ac8e9819SChris Mason 	while(1) {
1281ac8e9819SChris Mason 		ret = btrfs_search_forward(root, &key, &max_key, path, 0,
1282ac8e9819SChris Mason 					   sk->min_transid);
1283ac8e9819SChris Mason 		if (ret != 0) {
1284ac8e9819SChris Mason 			if (ret > 0)
1285ac8e9819SChris Mason 				ret = 0;
1286ac8e9819SChris Mason 			goto err;
1287ac8e9819SChris Mason 		}
1288ac8e9819SChris Mason 		ret = copy_to_sk(root, path, &key, sk, args->buf,
1289ac8e9819SChris Mason 				 &sk_offset, &num_found);
1290ac8e9819SChris Mason 		btrfs_release_path(root, path);
1291ac8e9819SChris Mason 		if (ret || num_found >= sk->nr_items)
1292ac8e9819SChris Mason 			break;
1293ac8e9819SChris Mason 
1294ac8e9819SChris Mason 	}
1295ac8e9819SChris Mason 	ret = 0;
1296ac8e9819SChris Mason err:
1297ac8e9819SChris Mason 	sk->nr_items = num_found;
1298ac8e9819SChris Mason 	btrfs_free_path(path);
1299ac8e9819SChris Mason 	return ret;
1300ac8e9819SChris Mason }
1301ac8e9819SChris Mason 
1302ac8e9819SChris Mason static noinline int btrfs_ioctl_tree_search(struct file *file,
1303ac8e9819SChris Mason 					   void __user *argp)
1304ac8e9819SChris Mason {
1305ac8e9819SChris Mason 	 struct btrfs_ioctl_search_args *args;
1306ac8e9819SChris Mason 	 struct inode *inode;
1307ac8e9819SChris Mason 	 int ret;
1308ac8e9819SChris Mason 
1309ac8e9819SChris Mason 	if (!capable(CAP_SYS_ADMIN))
1310ac8e9819SChris Mason 		return -EPERM;
1311ac8e9819SChris Mason 
13122354d08fSJulia Lawall 	args = memdup_user(argp, sizeof(*args));
13132354d08fSJulia Lawall 	if (IS_ERR(args))
13142354d08fSJulia Lawall 		return PTR_ERR(args);
1315ac8e9819SChris Mason 
1316ac8e9819SChris Mason 	inode = fdentry(file)->d_inode;
1317ac8e9819SChris Mason 	ret = search_ioctl(inode, args);
1318ac8e9819SChris Mason 	if (ret == 0 && copy_to_user(argp, args, sizeof(*args)))
1319ac8e9819SChris Mason 		ret = -EFAULT;
1320ac8e9819SChris Mason 	kfree(args);
1321ac8e9819SChris Mason 	return ret;
1322ac8e9819SChris Mason }
1323ac8e9819SChris Mason 
132498d377a0STARUISI Hiroaki /*
1325ac8e9819SChris Mason  * Search INODE_REFs to identify path name of 'dirid' directory
1326ac8e9819SChris Mason  * in a 'tree_id' tree. and sets path name to 'name'.
132798d377a0STARUISI Hiroaki  */
132898d377a0STARUISI Hiroaki static noinline int btrfs_search_path_in_tree(struct btrfs_fs_info *info,
132998d377a0STARUISI Hiroaki 				u64 tree_id, u64 dirid, char *name)
133098d377a0STARUISI Hiroaki {
133198d377a0STARUISI Hiroaki 	struct btrfs_root *root;
133298d377a0STARUISI Hiroaki 	struct btrfs_key key;
1333ac8e9819SChris Mason 	char *ptr;
133498d377a0STARUISI Hiroaki 	int ret = -1;
133598d377a0STARUISI Hiroaki 	int slot;
133698d377a0STARUISI Hiroaki 	int len;
133798d377a0STARUISI Hiroaki 	int total_len = 0;
133898d377a0STARUISI Hiroaki 	struct btrfs_inode_ref *iref;
133998d377a0STARUISI Hiroaki 	struct extent_buffer *l;
134098d377a0STARUISI Hiroaki 	struct btrfs_path *path;
134198d377a0STARUISI Hiroaki 
134298d377a0STARUISI Hiroaki 	if (dirid == BTRFS_FIRST_FREE_OBJECTID) {
134398d377a0STARUISI Hiroaki 		name[0]='\0';
134498d377a0STARUISI Hiroaki 		return 0;
134598d377a0STARUISI Hiroaki 	}
134698d377a0STARUISI Hiroaki 
134798d377a0STARUISI Hiroaki 	path = btrfs_alloc_path();
134898d377a0STARUISI Hiroaki 	if (!path)
134998d377a0STARUISI Hiroaki 		return -ENOMEM;
135098d377a0STARUISI Hiroaki 
1351ac8e9819SChris Mason 	ptr = &name[BTRFS_INO_LOOKUP_PATH_MAX];
135298d377a0STARUISI Hiroaki 
135398d377a0STARUISI Hiroaki 	key.objectid = tree_id;
135498d377a0STARUISI Hiroaki 	key.type = BTRFS_ROOT_ITEM_KEY;
135598d377a0STARUISI Hiroaki 	key.offset = (u64)-1;
135698d377a0STARUISI Hiroaki 	root = btrfs_read_fs_root_no_name(info, &key);
135798d377a0STARUISI Hiroaki 	if (IS_ERR(root)) {
135898d377a0STARUISI Hiroaki 		printk(KERN_ERR "could not find root %llu\n", tree_id);
13598ad6fcabSChris Mason 		ret = -ENOENT;
13608ad6fcabSChris Mason 		goto out;
136198d377a0STARUISI Hiroaki 	}
136298d377a0STARUISI Hiroaki 
136398d377a0STARUISI Hiroaki 	key.objectid = dirid;
136498d377a0STARUISI Hiroaki 	key.type = BTRFS_INODE_REF_KEY;
13658ad6fcabSChris Mason 	key.offset = (u64)-1;
136698d377a0STARUISI Hiroaki 
136798d377a0STARUISI Hiroaki 	while(1) {
136898d377a0STARUISI Hiroaki 		ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
136998d377a0STARUISI Hiroaki 		if (ret < 0)
137098d377a0STARUISI Hiroaki 			goto out;
137198d377a0STARUISI Hiroaki 
137298d377a0STARUISI Hiroaki 		l = path->nodes[0];
137398d377a0STARUISI Hiroaki 		slot = path->slots[0];
13748ad6fcabSChris Mason 		if (ret > 0 && slot > 0)
13758ad6fcabSChris Mason 			slot--;
137698d377a0STARUISI Hiroaki 		btrfs_item_key_to_cpu(l, &key, slot);
137798d377a0STARUISI Hiroaki 
137898d377a0STARUISI Hiroaki 		if (ret > 0 && (key.objectid != dirid ||
1379ac8e9819SChris Mason 				key.type != BTRFS_INODE_REF_KEY)) {
1380ac8e9819SChris Mason 			ret = -ENOENT;
138198d377a0STARUISI Hiroaki 			goto out;
1382ac8e9819SChris Mason 		}
138398d377a0STARUISI Hiroaki 
138498d377a0STARUISI Hiroaki 		iref = btrfs_item_ptr(l, slot, struct btrfs_inode_ref);
138598d377a0STARUISI Hiroaki 		len = btrfs_inode_ref_name_len(l, iref);
138698d377a0STARUISI Hiroaki 		ptr -= len + 1;
138798d377a0STARUISI Hiroaki 		total_len += len + 1;
1388ac8e9819SChris Mason 		if (ptr < name)
138998d377a0STARUISI Hiroaki 			goto out;
139098d377a0STARUISI Hiroaki 
139198d377a0STARUISI Hiroaki 		*(ptr + len) = '/';
139298d377a0STARUISI Hiroaki 		read_extent_buffer(l, ptr,(unsigned long)(iref + 1), len);
139398d377a0STARUISI Hiroaki 
139498d377a0STARUISI Hiroaki 		if (key.offset == BTRFS_FIRST_FREE_OBJECTID)
139598d377a0STARUISI Hiroaki 			break;
139698d377a0STARUISI Hiroaki 
139798d377a0STARUISI Hiroaki 		btrfs_release_path(root, path);
139898d377a0STARUISI Hiroaki 		key.objectid = key.offset;
13998ad6fcabSChris Mason 		key.offset = (u64)-1;
140098d377a0STARUISI Hiroaki 		dirid = key.objectid;
140198d377a0STARUISI Hiroaki 
140298d377a0STARUISI Hiroaki 	}
1403ac8e9819SChris Mason 	if (ptr < name)
140498d377a0STARUISI Hiroaki 		goto out;
1405ac8e9819SChris Mason 	memcpy(name, ptr, total_len);
140698d377a0STARUISI Hiroaki 	name[total_len]='\0';
140798d377a0STARUISI Hiroaki 	ret = 0;
140898d377a0STARUISI Hiroaki out:
140998d377a0STARUISI Hiroaki 	btrfs_free_path(path);
1410ac8e9819SChris Mason 	return ret;
1411ac8e9819SChris Mason }
1412ac8e9819SChris Mason 
1413ac8e9819SChris Mason static noinline int btrfs_ioctl_ino_lookup(struct file *file,
1414ac8e9819SChris Mason 					   void __user *argp)
1415ac8e9819SChris Mason {
1416ac8e9819SChris Mason 	 struct btrfs_ioctl_ino_lookup_args *args;
1417ac8e9819SChris Mason 	 struct inode *inode;
1418ac8e9819SChris Mason 	 int ret;
1419ac8e9819SChris Mason 
1420ac8e9819SChris Mason 	if (!capable(CAP_SYS_ADMIN))
1421ac8e9819SChris Mason 		return -EPERM;
1422ac8e9819SChris Mason 
14232354d08fSJulia Lawall 	args = memdup_user(argp, sizeof(*args));
14242354d08fSJulia Lawall 	if (IS_ERR(args))
14252354d08fSJulia Lawall 		return PTR_ERR(args);
1426c2b96929SDan Carpenter 
1427ac8e9819SChris Mason 	inode = fdentry(file)->d_inode;
1428ac8e9819SChris Mason 
14291b53ac4dSChris Mason 	if (args->treeid == 0)
14301b53ac4dSChris Mason 		args->treeid = BTRFS_I(inode)->root->root_key.objectid;
14311b53ac4dSChris Mason 
1432ac8e9819SChris Mason 	ret = btrfs_search_path_in_tree(BTRFS_I(inode)->root->fs_info,
1433ac8e9819SChris Mason 					args->treeid, args->objectid,
1434ac8e9819SChris Mason 					args->name);
1435ac8e9819SChris Mason 
1436ac8e9819SChris Mason 	if (ret == 0 && copy_to_user(argp, args, sizeof(*args)))
1437ac8e9819SChris Mason 		ret = -EFAULT;
1438ac8e9819SChris Mason 
1439ac8e9819SChris Mason 	kfree(args);
144098d377a0STARUISI Hiroaki 	return ret;
144198d377a0STARUISI Hiroaki }
144298d377a0STARUISI Hiroaki 
144376dda93cSYan, Zheng static noinline int btrfs_ioctl_snap_destroy(struct file *file,
144476dda93cSYan, Zheng 					     void __user *arg)
144576dda93cSYan, Zheng {
144676dda93cSYan, Zheng 	struct dentry *parent = fdentry(file);
144776dda93cSYan, Zheng 	struct dentry *dentry;
144876dda93cSYan, Zheng 	struct inode *dir = parent->d_inode;
144976dda93cSYan, Zheng 	struct inode *inode;
145076dda93cSYan, Zheng 	struct btrfs_root *root = BTRFS_I(dir)->root;
145176dda93cSYan, Zheng 	struct btrfs_root *dest = NULL;
145276dda93cSYan, Zheng 	struct btrfs_ioctl_vol_args *vol_args;
145376dda93cSYan, Zheng 	struct btrfs_trans_handle *trans;
145476dda93cSYan, Zheng 	int namelen;
145576dda93cSYan, Zheng 	int ret;
145676dda93cSYan, Zheng 	int err = 0;
145776dda93cSYan, Zheng 
145876dda93cSYan, Zheng 	vol_args = memdup_user(arg, sizeof(*vol_args));
145976dda93cSYan, Zheng 	if (IS_ERR(vol_args))
146076dda93cSYan, Zheng 		return PTR_ERR(vol_args);
146176dda93cSYan, Zheng 
146276dda93cSYan, Zheng 	vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
146376dda93cSYan, Zheng 	namelen = strlen(vol_args->name);
146476dda93cSYan, Zheng 	if (strchr(vol_args->name, '/') ||
146576dda93cSYan, Zheng 	    strncmp(vol_args->name, "..", namelen) == 0) {
146676dda93cSYan, Zheng 		err = -EINVAL;
146776dda93cSYan, Zheng 		goto out;
146876dda93cSYan, Zheng 	}
146976dda93cSYan, Zheng 
147076dda93cSYan, Zheng 	err = mnt_want_write(file->f_path.mnt);
147176dda93cSYan, Zheng 	if (err)
147276dda93cSYan, Zheng 		goto out;
147376dda93cSYan, Zheng 
147476dda93cSYan, Zheng 	mutex_lock_nested(&dir->i_mutex, I_MUTEX_PARENT);
147576dda93cSYan, Zheng 	dentry = lookup_one_len(vol_args->name, parent, namelen);
147676dda93cSYan, Zheng 	if (IS_ERR(dentry)) {
147776dda93cSYan, Zheng 		err = PTR_ERR(dentry);
147876dda93cSYan, Zheng 		goto out_unlock_dir;
147976dda93cSYan, Zheng 	}
148076dda93cSYan, Zheng 
148176dda93cSYan, Zheng 	if (!dentry->d_inode) {
148276dda93cSYan, Zheng 		err = -ENOENT;
148376dda93cSYan, Zheng 		goto out_dput;
148476dda93cSYan, Zheng 	}
148576dda93cSYan, Zheng 
148676dda93cSYan, Zheng 	inode = dentry->d_inode;
14874260f7c7SSage Weil 	dest = BTRFS_I(inode)->root;
14884260f7c7SSage Weil 	if (!capable(CAP_SYS_ADMIN)){
14894260f7c7SSage Weil 		/*
14904260f7c7SSage Weil 		 * Regular user.  Only allow this with a special mount
14914260f7c7SSage Weil 		 * option, when the user has write+exec access to the
14924260f7c7SSage Weil 		 * subvol root, and when rmdir(2) would have been
14934260f7c7SSage Weil 		 * allowed.
14944260f7c7SSage Weil 		 *
14954260f7c7SSage Weil 		 * Note that this is _not_ check that the subvol is
14964260f7c7SSage Weil 		 * empty or doesn't contain data that we wouldn't
14974260f7c7SSage Weil 		 * otherwise be able to delete.
14984260f7c7SSage Weil 		 *
14994260f7c7SSage Weil 		 * Users who want to delete empty subvols should try
15004260f7c7SSage Weil 		 * rmdir(2).
15014260f7c7SSage Weil 		 */
15024260f7c7SSage Weil 		err = -EPERM;
15034260f7c7SSage Weil 		if (!btrfs_test_opt(root, USER_SUBVOL_RM_ALLOWED))
15044260f7c7SSage Weil 			goto out_dput;
15054260f7c7SSage Weil 
15064260f7c7SSage Weil 		/*
15074260f7c7SSage Weil 		 * Do not allow deletion if the parent dir is the same
15084260f7c7SSage Weil 		 * as the dir to be deleted.  That means the ioctl
15094260f7c7SSage Weil 		 * must be called on the dentry referencing the root
15104260f7c7SSage Weil 		 * of the subvol, not a random directory contained
15114260f7c7SSage Weil 		 * within it.
15124260f7c7SSage Weil 		 */
15134260f7c7SSage Weil 		err = -EINVAL;
15144260f7c7SSage Weil 		if (root == dest)
15154260f7c7SSage Weil 			goto out_dput;
15164260f7c7SSage Weil 
15174260f7c7SSage Weil 		err = inode_permission(inode, MAY_WRITE | MAY_EXEC);
15184260f7c7SSage Weil 		if (err)
15194260f7c7SSage Weil 			goto out_dput;
15204260f7c7SSage Weil 
15214260f7c7SSage Weil 		/* check if subvolume may be deleted by a non-root user */
15224260f7c7SSage Weil 		err = btrfs_may_delete(dir, dentry, 1);
15234260f7c7SSage Weil 		if (err)
15244260f7c7SSage Weil 			goto out_dput;
15254260f7c7SSage Weil 	}
15264260f7c7SSage Weil 
152776dda93cSYan, Zheng 	if (inode->i_ino != BTRFS_FIRST_FREE_OBJECTID) {
152876dda93cSYan, Zheng 		err = -EINVAL;
152976dda93cSYan, Zheng 		goto out_dput;
153076dda93cSYan, Zheng 	}
153176dda93cSYan, Zheng 
153276dda93cSYan, Zheng 	mutex_lock(&inode->i_mutex);
153376dda93cSYan, Zheng 	err = d_invalidate(dentry);
153476dda93cSYan, Zheng 	if (err)
153576dda93cSYan, Zheng 		goto out_unlock;
153676dda93cSYan, Zheng 
153776dda93cSYan, Zheng 	down_write(&root->fs_info->subvol_sem);
153876dda93cSYan, Zheng 
153976dda93cSYan, Zheng 	err = may_destroy_subvol(dest);
154076dda93cSYan, Zheng 	if (err)
154176dda93cSYan, Zheng 		goto out_up_write;
154276dda93cSYan, Zheng 
1543a22285a6SYan, Zheng 	trans = btrfs_start_transaction(root, 0);
1544a22285a6SYan, Zheng 	if (IS_ERR(trans)) {
1545a22285a6SYan, Zheng 		err = PTR_ERR(trans);
1546d327099aSDan Carpenter 		goto out_up_write;
1547a22285a6SYan, Zheng 	}
1548a22285a6SYan, Zheng 	trans->block_rsv = &root->fs_info->global_block_rsv;
1549a22285a6SYan, Zheng 
155076dda93cSYan, Zheng 	ret = btrfs_unlink_subvol(trans, root, dir,
155176dda93cSYan, Zheng 				dest->root_key.objectid,
155276dda93cSYan, Zheng 				dentry->d_name.name,
155376dda93cSYan, Zheng 				dentry->d_name.len);
155476dda93cSYan, Zheng 	BUG_ON(ret);
155576dda93cSYan, Zheng 
155676dda93cSYan, Zheng 	btrfs_record_root_in_trans(trans, dest);
155776dda93cSYan, Zheng 
155876dda93cSYan, Zheng 	memset(&dest->root_item.drop_progress, 0,
155976dda93cSYan, Zheng 		sizeof(dest->root_item.drop_progress));
156076dda93cSYan, Zheng 	dest->root_item.drop_level = 0;
156176dda93cSYan, Zheng 	btrfs_set_root_refs(&dest->root_item, 0);
156276dda93cSYan, Zheng 
1563d68fc57bSYan, Zheng 	if (!xchg(&dest->orphan_item_inserted, 1)) {
156476dda93cSYan, Zheng 		ret = btrfs_insert_orphan_item(trans,
156576dda93cSYan, Zheng 					root->fs_info->tree_root,
156676dda93cSYan, Zheng 					dest->root_key.objectid);
156776dda93cSYan, Zheng 		BUG_ON(ret);
1568d68fc57bSYan, Zheng 	}
156976dda93cSYan, Zheng 
1570531cb13fSSage Weil 	ret = btrfs_end_transaction(trans, root);
157176dda93cSYan, Zheng 	BUG_ON(ret);
157276dda93cSYan, Zheng 	inode->i_flags |= S_DEAD;
157376dda93cSYan, Zheng out_up_write:
157476dda93cSYan, Zheng 	up_write(&root->fs_info->subvol_sem);
157576dda93cSYan, Zheng out_unlock:
157676dda93cSYan, Zheng 	mutex_unlock(&inode->i_mutex);
157776dda93cSYan, Zheng 	if (!err) {
1578efefb143SYan, Zheng 		shrink_dcache_sb(root->fs_info->sb);
157976dda93cSYan, Zheng 		btrfs_invalidate_inodes(dest);
158076dda93cSYan, Zheng 		d_delete(dentry);
158176dda93cSYan, Zheng 	}
158276dda93cSYan, Zheng out_dput:
158376dda93cSYan, Zheng 	dput(dentry);
158476dda93cSYan, Zheng out_unlock_dir:
158576dda93cSYan, Zheng 	mutex_unlock(&dir->i_mutex);
158676dda93cSYan, Zheng 	mnt_drop_write(file->f_path.mnt);
158776dda93cSYan, Zheng out:
158876dda93cSYan, Zheng 	kfree(vol_args);
158976dda93cSYan, Zheng 	return err;
159076dda93cSYan, Zheng }
159176dda93cSYan, Zheng 
15921e701a32SChris Mason static int btrfs_ioctl_defrag(struct file *file, void __user *argp)
1593f46b5a66SChristoph Hellwig {
1594f46b5a66SChristoph Hellwig 	struct inode *inode = fdentry(file)->d_inode;
1595f46b5a66SChristoph Hellwig 	struct btrfs_root *root = BTRFS_I(inode)->root;
15961e701a32SChris Mason 	struct btrfs_ioctl_defrag_range_args *range;
1597c146afadSYan Zheng 	int ret;
1598c146afadSYan Zheng 
1599b83cc969SLi Zefan 	if (btrfs_root_readonly(root))
1600b83cc969SLi Zefan 		return -EROFS;
1601b83cc969SLi Zefan 
1602c146afadSYan Zheng 	ret = mnt_want_write(file->f_path.mnt);
1603c146afadSYan Zheng 	if (ret)
1604c146afadSYan Zheng 		return ret;
1605f46b5a66SChristoph Hellwig 
1606f46b5a66SChristoph Hellwig 	switch (inode->i_mode & S_IFMT) {
1607f46b5a66SChristoph Hellwig 	case S_IFDIR:
1608e441d54dSChris Mason 		if (!capable(CAP_SYS_ADMIN)) {
1609e441d54dSChris Mason 			ret = -EPERM;
1610e441d54dSChris Mason 			goto out;
1611e441d54dSChris Mason 		}
16128929ecfaSYan, Zheng 		ret = btrfs_defrag_root(root, 0);
16138929ecfaSYan, Zheng 		if (ret)
16148929ecfaSYan, Zheng 			goto out;
16158929ecfaSYan, Zheng 		ret = btrfs_defrag_root(root->fs_info->extent_root, 0);
1616f46b5a66SChristoph Hellwig 		break;
1617f46b5a66SChristoph Hellwig 	case S_IFREG:
1618e441d54dSChris Mason 		if (!(file->f_mode & FMODE_WRITE)) {
1619e441d54dSChris Mason 			ret = -EINVAL;
1620e441d54dSChris Mason 			goto out;
1621e441d54dSChris Mason 		}
16221e701a32SChris Mason 
16231e701a32SChris Mason 		range = kzalloc(sizeof(*range), GFP_KERNEL);
16241e701a32SChris Mason 		if (!range) {
16251e701a32SChris Mason 			ret = -ENOMEM;
16261e701a32SChris Mason 			goto out;
16271e701a32SChris Mason 		}
16281e701a32SChris Mason 
16291e701a32SChris Mason 		if (argp) {
16301e701a32SChris Mason 			if (copy_from_user(range, argp,
16311e701a32SChris Mason 					   sizeof(*range))) {
16321e701a32SChris Mason 				ret = -EFAULT;
16331e701a32SChris Mason 				kfree(range);
1634683be16eSDan Carpenter 				goto out;
16351e701a32SChris Mason 			}
16361e701a32SChris Mason 			/* compression requires us to start the IO */
16371e701a32SChris Mason 			if ((range->flags & BTRFS_DEFRAG_RANGE_COMPRESS)) {
16381e701a32SChris Mason 				range->flags |= BTRFS_DEFRAG_RANGE_START_IO;
16391e701a32SChris Mason 				range->extent_thresh = (u32)-1;
16401e701a32SChris Mason 			}
16411e701a32SChris Mason 		} else {
16421e701a32SChris Mason 			/* the rest are all set to zero by kzalloc */
16431e701a32SChris Mason 			range->len = (u64)-1;
16441e701a32SChris Mason 		}
16458929ecfaSYan, Zheng 		ret = btrfs_defrag_file(file, range);
16461e701a32SChris Mason 		kfree(range);
1647f46b5a66SChristoph Hellwig 		break;
16488929ecfaSYan, Zheng 	default:
16498929ecfaSYan, Zheng 		ret = -EINVAL;
1650f46b5a66SChristoph Hellwig 	}
1651e441d54dSChris Mason out:
1652ab67b7c1SYan Zheng 	mnt_drop_write(file->f_path.mnt);
1653e441d54dSChris Mason 	return ret;
1654f46b5a66SChristoph Hellwig }
1655f46b5a66SChristoph Hellwig 
1656b2950863SChristoph Hellwig static long btrfs_ioctl_add_dev(struct btrfs_root *root, void __user *arg)
1657f46b5a66SChristoph Hellwig {
1658f46b5a66SChristoph Hellwig 	struct btrfs_ioctl_vol_args *vol_args;
1659f46b5a66SChristoph Hellwig 	int ret;
1660f46b5a66SChristoph Hellwig 
1661e441d54dSChris Mason 	if (!capable(CAP_SYS_ADMIN))
1662e441d54dSChris Mason 		return -EPERM;
1663e441d54dSChris Mason 
1664dae7b665SLi Zefan 	vol_args = memdup_user(arg, sizeof(*vol_args));
1665dae7b665SLi Zefan 	if (IS_ERR(vol_args))
1666dae7b665SLi Zefan 		return PTR_ERR(vol_args);
1667f46b5a66SChristoph Hellwig 
16685516e595SMark Fasheh 	vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
1669f46b5a66SChristoph Hellwig 	ret = btrfs_init_new_device(root, vol_args->name);
1670f46b5a66SChristoph Hellwig 
1671f46b5a66SChristoph Hellwig 	kfree(vol_args);
1672f46b5a66SChristoph Hellwig 	return ret;
1673f46b5a66SChristoph Hellwig }
1674f46b5a66SChristoph Hellwig 
1675b2950863SChristoph Hellwig static long btrfs_ioctl_rm_dev(struct btrfs_root *root, void __user *arg)
1676f46b5a66SChristoph Hellwig {
1677f46b5a66SChristoph Hellwig 	struct btrfs_ioctl_vol_args *vol_args;
1678f46b5a66SChristoph Hellwig 	int ret;
1679f46b5a66SChristoph Hellwig 
1680e441d54dSChris Mason 	if (!capable(CAP_SYS_ADMIN))
1681e441d54dSChris Mason 		return -EPERM;
1682e441d54dSChris Mason 
1683c146afadSYan Zheng 	if (root->fs_info->sb->s_flags & MS_RDONLY)
1684c146afadSYan Zheng 		return -EROFS;
1685c146afadSYan Zheng 
1686dae7b665SLi Zefan 	vol_args = memdup_user(arg, sizeof(*vol_args));
1687dae7b665SLi Zefan 	if (IS_ERR(vol_args))
1688dae7b665SLi Zefan 		return PTR_ERR(vol_args);
1689f46b5a66SChristoph Hellwig 
16905516e595SMark Fasheh 	vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
1691f46b5a66SChristoph Hellwig 	ret = btrfs_rm_device(root, vol_args->name);
1692f46b5a66SChristoph Hellwig 
1693f46b5a66SChristoph Hellwig 	kfree(vol_args);
1694f46b5a66SChristoph Hellwig 	return ret;
1695f46b5a66SChristoph Hellwig }
1696f46b5a66SChristoph Hellwig 
169776dda93cSYan, Zheng static noinline long btrfs_ioctl_clone(struct file *file, unsigned long srcfd,
1698b2950863SChristoph Hellwig 				       u64 off, u64 olen, u64 destoff)
1699f46b5a66SChristoph Hellwig {
1700f46b5a66SChristoph Hellwig 	struct inode *inode = fdentry(file)->d_inode;
1701f46b5a66SChristoph Hellwig 	struct btrfs_root *root = BTRFS_I(inode)->root;
1702f46b5a66SChristoph Hellwig 	struct file *src_file;
1703f46b5a66SChristoph Hellwig 	struct inode *src;
1704f46b5a66SChristoph Hellwig 	struct btrfs_trans_handle *trans;
1705f46b5a66SChristoph Hellwig 	struct btrfs_path *path;
1706f46b5a66SChristoph Hellwig 	struct extent_buffer *leaf;
1707ae01a0abSYan Zheng 	char *buf;
1708ae01a0abSYan Zheng 	struct btrfs_key key;
1709f46b5a66SChristoph Hellwig 	u32 nritems;
1710f46b5a66SChristoph Hellwig 	int slot;
1711ae01a0abSYan Zheng 	int ret;
1712c5c9cd4dSSage Weil 	u64 len = olen;
1713c5c9cd4dSSage Weil 	u64 bs = root->fs_info->sb->s_blocksize;
1714c5c9cd4dSSage Weil 	u64 hint_byte;
1715d20f7043SChris Mason 
1716c5c9cd4dSSage Weil 	/*
1717c5c9cd4dSSage Weil 	 * TODO:
1718c5c9cd4dSSage Weil 	 * - split compressed inline extents.  annoying: we need to
1719c5c9cd4dSSage Weil 	 *   decompress into destination's address_space (the file offset
1720c5c9cd4dSSage Weil 	 *   may change, so source mapping won't do), then recompress (or
1721c5c9cd4dSSage Weil 	 *   otherwise reinsert) a subrange.
1722c5c9cd4dSSage Weil 	 * - allow ranges within the same file to be cloned (provided
1723c5c9cd4dSSage Weil 	 *   they don't overlap)?
1724c5c9cd4dSSage Weil 	 */
1725c5c9cd4dSSage Weil 
1726e441d54dSChris Mason 	/* the destination must be opened for writing */
17272ebc3464SDan Rosenberg 	if (!(file->f_mode & FMODE_WRITE) || (file->f_flags & O_APPEND))
1728e441d54dSChris Mason 		return -EINVAL;
1729e441d54dSChris Mason 
1730b83cc969SLi Zefan 	if (btrfs_root_readonly(root))
1731b83cc969SLi Zefan 		return -EROFS;
1732b83cc969SLi Zefan 
1733c146afadSYan Zheng 	ret = mnt_want_write(file->f_path.mnt);
1734c146afadSYan Zheng 	if (ret)
1735c146afadSYan Zheng 		return ret;
1736c146afadSYan Zheng 
1737c5c9cd4dSSage Weil 	src_file = fget(srcfd);
1738ab67b7c1SYan Zheng 	if (!src_file) {
1739ab67b7c1SYan Zheng 		ret = -EBADF;
1740ab67b7c1SYan Zheng 		goto out_drop_write;
1741ab67b7c1SYan Zheng 	}
17425dc64164SDan Rosenberg 
1743f46b5a66SChristoph Hellwig 	src = src_file->f_dentry->d_inode;
1744f46b5a66SChristoph Hellwig 
1745c5c9cd4dSSage Weil 	ret = -EINVAL;
1746c5c9cd4dSSage Weil 	if (src == inode)
1747c5c9cd4dSSage Weil 		goto out_fput;
1748c5c9cd4dSSage Weil 
17495dc64164SDan Rosenberg 	/* the src must be open for reading */
17505dc64164SDan Rosenberg 	if (!(src_file->f_mode & FMODE_READ))
17515dc64164SDan Rosenberg 		goto out_fput;
17525dc64164SDan Rosenberg 
1753ae01a0abSYan Zheng 	ret = -EISDIR;
1754ae01a0abSYan Zheng 	if (S_ISDIR(src->i_mode) || S_ISDIR(inode->i_mode))
1755f46b5a66SChristoph Hellwig 		goto out_fput;
1756f46b5a66SChristoph Hellwig 
1757ae01a0abSYan Zheng 	ret = -EXDEV;
1758ae01a0abSYan Zheng 	if (src->i_sb != inode->i_sb || BTRFS_I(src)->root != root)
1759ae01a0abSYan Zheng 		goto out_fput;
1760ae01a0abSYan Zheng 
1761ae01a0abSYan Zheng 	ret = -ENOMEM;
1762ae01a0abSYan Zheng 	buf = vmalloc(btrfs_level_size(root, 0));
1763ae01a0abSYan Zheng 	if (!buf)
1764ae01a0abSYan Zheng 		goto out_fput;
1765ae01a0abSYan Zheng 
1766ae01a0abSYan Zheng 	path = btrfs_alloc_path();
1767ae01a0abSYan Zheng 	if (!path) {
1768ae01a0abSYan Zheng 		vfree(buf);
1769ae01a0abSYan Zheng 		goto out_fput;
1770ae01a0abSYan Zheng 	}
1771ae01a0abSYan Zheng 	path->reada = 2;
1772ae01a0abSYan Zheng 
1773f46b5a66SChristoph Hellwig 	if (inode < src) {
1774fccdae43SSage Weil 		mutex_lock_nested(&inode->i_mutex, I_MUTEX_PARENT);
1775fccdae43SSage Weil 		mutex_lock_nested(&src->i_mutex, I_MUTEX_CHILD);
1776f46b5a66SChristoph Hellwig 	} else {
1777fccdae43SSage Weil 		mutex_lock_nested(&src->i_mutex, I_MUTEX_PARENT);
1778fccdae43SSage Weil 		mutex_lock_nested(&inode->i_mutex, I_MUTEX_CHILD);
1779f46b5a66SChristoph Hellwig 	}
1780f46b5a66SChristoph Hellwig 
1781c5c9cd4dSSage Weil 	/* determine range to clone */
1782c5c9cd4dSSage Weil 	ret = -EINVAL;
17832ebc3464SDan Rosenberg 	if (off + len > src->i_size || off + len < off)
1784f46b5a66SChristoph Hellwig 		goto out_unlock;
1785c5c9cd4dSSage Weil 	if (len == 0)
1786c5c9cd4dSSage Weil 		olen = len = src->i_size - off;
1787c5c9cd4dSSage Weil 	/* if we extend to eof, continue to block boundary */
1788c5c9cd4dSSage Weil 	if (off + len == src->i_size)
17892a6b8daeSLi Zefan 		len = ALIGN(src->i_size, bs) - off;
1790c5c9cd4dSSage Weil 
1791c5c9cd4dSSage Weil 	/* verify the end result is block aligned */
17922a6b8daeSLi Zefan 	if (!IS_ALIGNED(off, bs) || !IS_ALIGNED(off + len, bs) ||
17932a6b8daeSLi Zefan 	    !IS_ALIGNED(destoff, bs))
1794c5c9cd4dSSage Weil 		goto out_unlock;
1795c5c9cd4dSSage Weil 
1796f46b5a66SChristoph Hellwig 	/* do any pending delalloc/csum calc on src, one way or
1797f46b5a66SChristoph Hellwig 	   another, and lock file content */
1798f46b5a66SChristoph Hellwig 	while (1) {
179931840ae1SZheng Yan 		struct btrfs_ordered_extent *ordered;
1800c5c9cd4dSSage Weil 		lock_extent(&BTRFS_I(src)->io_tree, off, off+len, GFP_NOFS);
18019a019196SSage Weil 		ordered = btrfs_lookup_first_ordered_extent(src, off+len);
18029a019196SSage Weil 		if (!ordered &&
18039a019196SSage Weil 		    !test_range_bit(&BTRFS_I(src)->io_tree, off, off+len,
18049a019196SSage Weil 				   EXTENT_DELALLOC, 0, NULL))
1805f46b5a66SChristoph Hellwig 			break;
1806c5c9cd4dSSage Weil 		unlock_extent(&BTRFS_I(src)->io_tree, off, off+len, GFP_NOFS);
1807ae01a0abSYan Zheng 		if (ordered)
1808ae01a0abSYan Zheng 			btrfs_put_ordered_extent(ordered);
18099a019196SSage Weil 		btrfs_wait_ordered_range(src, off, len);
1810f46b5a66SChristoph Hellwig 	}
1811f46b5a66SChristoph Hellwig 
1812c5c9cd4dSSage Weil 	/* clone data */
1813f46b5a66SChristoph Hellwig 	key.objectid = src->i_ino;
1814ae01a0abSYan Zheng 	key.type = BTRFS_EXTENT_DATA_KEY;
1815ae01a0abSYan Zheng 	key.offset = 0;
1816f46b5a66SChristoph Hellwig 
1817f46b5a66SChristoph Hellwig 	while (1) {
1818f46b5a66SChristoph Hellwig 		/*
1819f46b5a66SChristoph Hellwig 		 * note the key will change type as we walk through the
1820f46b5a66SChristoph Hellwig 		 * tree.
1821f46b5a66SChristoph Hellwig 		 */
1822a22285a6SYan, Zheng 		ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1823f46b5a66SChristoph Hellwig 		if (ret < 0)
1824f46b5a66SChristoph Hellwig 			goto out;
1825f46b5a66SChristoph Hellwig 
1826ae01a0abSYan Zheng 		nritems = btrfs_header_nritems(path->nodes[0]);
1827ae01a0abSYan Zheng 		if (path->slots[0] >= nritems) {
1828f46b5a66SChristoph Hellwig 			ret = btrfs_next_leaf(root, path);
1829f46b5a66SChristoph Hellwig 			if (ret < 0)
1830f46b5a66SChristoph Hellwig 				goto out;
1831f46b5a66SChristoph Hellwig 			if (ret > 0)
1832f46b5a66SChristoph Hellwig 				break;
1833ae01a0abSYan Zheng 			nritems = btrfs_header_nritems(path->nodes[0]);
1834f46b5a66SChristoph Hellwig 		}
1835f46b5a66SChristoph Hellwig 		leaf = path->nodes[0];
1836f46b5a66SChristoph Hellwig 		slot = path->slots[0];
1837f46b5a66SChristoph Hellwig 
1838ae01a0abSYan Zheng 		btrfs_item_key_to_cpu(leaf, &key, slot);
1839d20f7043SChris Mason 		if (btrfs_key_type(&key) > BTRFS_EXTENT_DATA_KEY ||
1840f46b5a66SChristoph Hellwig 		    key.objectid != src->i_ino)
1841f46b5a66SChristoph Hellwig 			break;
1842f46b5a66SChristoph Hellwig 
1843c5c9cd4dSSage Weil 		if (btrfs_key_type(&key) == BTRFS_EXTENT_DATA_KEY) {
1844c5c9cd4dSSage Weil 			struct btrfs_file_extent_item *extent;
1845c5c9cd4dSSage Weil 			int type;
184631840ae1SZheng Yan 			u32 size;
184731840ae1SZheng Yan 			struct btrfs_key new_key;
1848c5c9cd4dSSage Weil 			u64 disko = 0, diskl = 0;
1849c5c9cd4dSSage Weil 			u64 datao = 0, datal = 0;
1850c5c9cd4dSSage Weil 			u8 comp;
1851b5384d48SSage Weil 			u64 endoff;
185231840ae1SZheng Yan 
185331840ae1SZheng Yan 			size = btrfs_item_size_nr(leaf, slot);
185431840ae1SZheng Yan 			read_extent_buffer(leaf, buf,
185531840ae1SZheng Yan 					   btrfs_item_ptr_offset(leaf, slot),
185631840ae1SZheng Yan 					   size);
1857c5c9cd4dSSage Weil 
1858c5c9cd4dSSage Weil 			extent = btrfs_item_ptr(leaf, slot,
1859c5c9cd4dSSage Weil 						struct btrfs_file_extent_item);
1860c5c9cd4dSSage Weil 			comp = btrfs_file_extent_compression(leaf, extent);
1861c5c9cd4dSSage Weil 			type = btrfs_file_extent_type(leaf, extent);
1862c8a894d7SChris Mason 			if (type == BTRFS_FILE_EXTENT_REG ||
1863c8a894d7SChris Mason 			    type == BTRFS_FILE_EXTENT_PREALLOC) {
1864d397712bSChris Mason 				disko = btrfs_file_extent_disk_bytenr(leaf,
1865d397712bSChris Mason 								      extent);
1866d397712bSChris Mason 				diskl = btrfs_file_extent_disk_num_bytes(leaf,
1867d397712bSChris Mason 								 extent);
1868c5c9cd4dSSage Weil 				datao = btrfs_file_extent_offset(leaf, extent);
1869d397712bSChris Mason 				datal = btrfs_file_extent_num_bytes(leaf,
1870d397712bSChris Mason 								    extent);
1871c5c9cd4dSSage Weil 			} else if (type == BTRFS_FILE_EXTENT_INLINE) {
1872c5c9cd4dSSage Weil 				/* take upper bound, may be compressed */
1873c5c9cd4dSSage Weil 				datal = btrfs_file_extent_ram_bytes(leaf,
1874c5c9cd4dSSage Weil 								    extent);
1875c5c9cd4dSSage Weil 			}
187631840ae1SZheng Yan 			btrfs_release_path(root, path);
187731840ae1SZheng Yan 
1878050006a7SSage Weil 			if (key.offset + datal <= off ||
1879c5c9cd4dSSage Weil 			    key.offset >= off+len)
1880c5c9cd4dSSage Weil 				goto next;
1881c5c9cd4dSSage Weil 
188231840ae1SZheng Yan 			memcpy(&new_key, &key, sizeof(new_key));
188331840ae1SZheng Yan 			new_key.objectid = inode->i_ino;
1884c5c9cd4dSSage Weil 			new_key.offset = key.offset + destoff - off;
1885c5c9cd4dSSage Weil 
1886a22285a6SYan, Zheng 			trans = btrfs_start_transaction(root, 1);
1887a22285a6SYan, Zheng 			if (IS_ERR(trans)) {
1888a22285a6SYan, Zheng 				ret = PTR_ERR(trans);
1889a22285a6SYan, Zheng 				goto out;
1890a22285a6SYan, Zheng 			}
1891a22285a6SYan, Zheng 
1892c8a894d7SChris Mason 			if (type == BTRFS_FILE_EXTENT_REG ||
1893c8a894d7SChris Mason 			    type == BTRFS_FILE_EXTENT_PREALLOC) {
1894a22285a6SYan, Zheng 				if (off > key.offset) {
1895a22285a6SYan, Zheng 					datao += off - key.offset;
1896a22285a6SYan, Zheng 					datal -= off - key.offset;
1897a22285a6SYan, Zheng 				}
1898a22285a6SYan, Zheng 
1899a22285a6SYan, Zheng 				if (key.offset + datal > off + len)
1900a22285a6SYan, Zheng 					datal = off + len - key.offset;
1901a22285a6SYan, Zheng 
1902a22285a6SYan, Zheng 				ret = btrfs_drop_extents(trans, inode,
1903a22285a6SYan, Zheng 							 new_key.offset,
1904a22285a6SYan, Zheng 							 new_key.offset + datal,
1905a22285a6SYan, Zheng 							 &hint_byte, 1);
1906a22285a6SYan, Zheng 				BUG_ON(ret);
1907a22285a6SYan, Zheng 
190831840ae1SZheng Yan 				ret = btrfs_insert_empty_item(trans, root, path,
190931840ae1SZheng Yan 							      &new_key, size);
1910a22285a6SYan, Zheng 				BUG_ON(ret);
191131840ae1SZheng Yan 
191231840ae1SZheng Yan 				leaf = path->nodes[0];
191331840ae1SZheng Yan 				slot = path->slots[0];
191431840ae1SZheng Yan 				write_extent_buffer(leaf, buf,
191531840ae1SZheng Yan 					    btrfs_item_ptr_offset(leaf, slot),
191631840ae1SZheng Yan 					    size);
1917ae01a0abSYan Zheng 
1918f46b5a66SChristoph Hellwig 				extent = btrfs_item_ptr(leaf, slot,
1919f46b5a66SChristoph Hellwig 						struct btrfs_file_extent_item);
1920c5c9cd4dSSage Weil 
1921c5c9cd4dSSage Weil 				/* disko == 0 means it's a hole */
1922c5c9cd4dSSage Weil 				if (!disko)
1923c5c9cd4dSSage Weil 					datao = 0;
1924c5c9cd4dSSage Weil 
1925c5c9cd4dSSage Weil 				btrfs_set_file_extent_offset(leaf, extent,
1926c5c9cd4dSSage Weil 							     datao);
1927c5c9cd4dSSage Weil 				btrfs_set_file_extent_num_bytes(leaf, extent,
1928c5c9cd4dSSage Weil 								datal);
1929c5c9cd4dSSage Weil 				if (disko) {
1930c5c9cd4dSSage Weil 					inode_add_bytes(inode, datal);
1931ae01a0abSYan Zheng 					ret = btrfs_inc_extent_ref(trans, root,
19325d4f98a2SYan Zheng 							disko, diskl, 0,
1933f46b5a66SChristoph Hellwig 							root->root_key.objectid,
19345d4f98a2SYan Zheng 							inode->i_ino,
19355d4f98a2SYan Zheng 							new_key.offset - datao);
1936ae01a0abSYan Zheng 					BUG_ON(ret);
1937f46b5a66SChristoph Hellwig 				}
1938c5c9cd4dSSage Weil 			} else if (type == BTRFS_FILE_EXTENT_INLINE) {
1939c5c9cd4dSSage Weil 				u64 skip = 0;
1940c5c9cd4dSSage Weil 				u64 trim = 0;
1941c5c9cd4dSSage Weil 				if (off > key.offset) {
1942c5c9cd4dSSage Weil 					skip = off - key.offset;
1943c5c9cd4dSSage Weil 					new_key.offset += skip;
194431840ae1SZheng Yan 				}
1945d397712bSChris Mason 
1946c5c9cd4dSSage Weil 				if (key.offset + datal > off+len)
1947c5c9cd4dSSage Weil 					trim = key.offset + datal - (off+len);
1948d397712bSChris Mason 
1949c5c9cd4dSSage Weil 				if (comp && (skip || trim)) {
1950c5c9cd4dSSage Weil 					ret = -EINVAL;
1951a22285a6SYan, Zheng 					btrfs_end_transaction(trans, root);
1952c5c9cd4dSSage Weil 					goto out;
195331840ae1SZheng Yan 				}
1954c5c9cd4dSSage Weil 				size -= skip + trim;
1955c5c9cd4dSSage Weil 				datal -= skip + trim;
1956a22285a6SYan, Zheng 
1957a22285a6SYan, Zheng 				ret = btrfs_drop_extents(trans, inode,
1958a22285a6SYan, Zheng 							 new_key.offset,
1959a22285a6SYan, Zheng 							 new_key.offset + datal,
1960a22285a6SYan, Zheng 							 &hint_byte, 1);
1961a22285a6SYan, Zheng 				BUG_ON(ret);
1962a22285a6SYan, Zheng 
1963c5c9cd4dSSage Weil 				ret = btrfs_insert_empty_item(trans, root, path,
1964c5c9cd4dSSage Weil 							      &new_key, size);
1965a22285a6SYan, Zheng 				BUG_ON(ret);
1966c5c9cd4dSSage Weil 
1967c5c9cd4dSSage Weil 				if (skip) {
1968d397712bSChris Mason 					u32 start =
1969d397712bSChris Mason 					  btrfs_file_extent_calc_inline_size(0);
1970c5c9cd4dSSage Weil 					memmove(buf+start, buf+start+skip,
1971c5c9cd4dSSage Weil 						datal);
1972c5c9cd4dSSage Weil 				}
1973c5c9cd4dSSage Weil 
1974c5c9cd4dSSage Weil 				leaf = path->nodes[0];
1975c5c9cd4dSSage Weil 				slot = path->slots[0];
1976c5c9cd4dSSage Weil 				write_extent_buffer(leaf, buf,
1977c5c9cd4dSSage Weil 					    btrfs_item_ptr_offset(leaf, slot),
1978c5c9cd4dSSage Weil 					    size);
1979c5c9cd4dSSage Weil 				inode_add_bytes(inode, datal);
1980c5c9cd4dSSage Weil 			}
1981c5c9cd4dSSage Weil 
1982c5c9cd4dSSage Weil 			btrfs_mark_buffer_dirty(leaf);
1983a22285a6SYan, Zheng 			btrfs_release_path(root, path);
1984c5c9cd4dSSage Weil 
1985a22285a6SYan, Zheng 			inode->i_mtime = inode->i_ctime = CURRENT_TIME;
1986b5384d48SSage Weil 
1987b5384d48SSage Weil 			/*
1988b5384d48SSage Weil 			 * we round up to the block size at eof when
1989b5384d48SSage Weil 			 * determining which extents to clone above,
1990b5384d48SSage Weil 			 * but shouldn't round up the file size
1991b5384d48SSage Weil 			 */
1992b5384d48SSage Weil 			endoff = new_key.offset + datal;
19935f3888ffSLi Zefan 			if (endoff > destoff+olen)
19945f3888ffSLi Zefan 				endoff = destoff+olen;
1995b5384d48SSage Weil 			if (endoff > inode->i_size)
1996b5384d48SSage Weil 				btrfs_i_size_write(inode, endoff);
1997b5384d48SSage Weil 
1998a22285a6SYan, Zheng 			BTRFS_I(inode)->flags = BTRFS_I(src)->flags;
1999a22285a6SYan, Zheng 			ret = btrfs_update_inode(trans, root, inode);
2000a22285a6SYan, Zheng 			BUG_ON(ret);
2001a22285a6SYan, Zheng 			btrfs_end_transaction(trans, root);
2002a22285a6SYan, Zheng 		}
2003c5c9cd4dSSage Weil next:
200431840ae1SZheng Yan 		btrfs_release_path(root, path);
2005ae01a0abSYan Zheng 		key.offset++;
2006ae01a0abSYan Zheng 	}
2007f46b5a66SChristoph Hellwig 	ret = 0;
2008f46b5a66SChristoph Hellwig out:
2009ae01a0abSYan Zheng 	btrfs_release_path(root, path);
2010c5c9cd4dSSage Weil 	unlock_extent(&BTRFS_I(src)->io_tree, off, off+len, GFP_NOFS);
2011f46b5a66SChristoph Hellwig out_unlock:
2012f46b5a66SChristoph Hellwig 	mutex_unlock(&src->i_mutex);
2013f46b5a66SChristoph Hellwig 	mutex_unlock(&inode->i_mutex);
2014ae01a0abSYan Zheng 	vfree(buf);
2015ae01a0abSYan Zheng 	btrfs_free_path(path);
2016f46b5a66SChristoph Hellwig out_fput:
2017f46b5a66SChristoph Hellwig 	fput(src_file);
2018ab67b7c1SYan Zheng out_drop_write:
2019ab67b7c1SYan Zheng 	mnt_drop_write(file->f_path.mnt);
2020f46b5a66SChristoph Hellwig 	return ret;
2021f46b5a66SChristoph Hellwig }
2022f46b5a66SChristoph Hellwig 
20237a865e8aSChristoph Hellwig static long btrfs_ioctl_clone_range(struct file *file, void __user *argp)
2024c5c9cd4dSSage Weil {
2025c5c9cd4dSSage Weil 	struct btrfs_ioctl_clone_range_args args;
2026c5c9cd4dSSage Weil 
20277a865e8aSChristoph Hellwig 	if (copy_from_user(&args, argp, sizeof(args)))
2028c5c9cd4dSSage Weil 		return -EFAULT;
2029c5c9cd4dSSage Weil 	return btrfs_ioctl_clone(file, args.src_fd, args.src_offset,
2030c5c9cd4dSSage Weil 				 args.src_length, args.dest_offset);
2031c5c9cd4dSSage Weil }
2032c5c9cd4dSSage Weil 
2033f46b5a66SChristoph Hellwig /*
2034f46b5a66SChristoph Hellwig  * there are many ways the trans_start and trans_end ioctls can lead
2035f46b5a66SChristoph Hellwig  * to deadlocks.  They should only be used by applications that
2036f46b5a66SChristoph Hellwig  * basically own the machine, and have a very in depth understanding
2037f46b5a66SChristoph Hellwig  * of all the possible deadlocks and enospc problems.
2038f46b5a66SChristoph Hellwig  */
2039b2950863SChristoph Hellwig static long btrfs_ioctl_trans_start(struct file *file)
2040f46b5a66SChristoph Hellwig {
2041f46b5a66SChristoph Hellwig 	struct inode *inode = fdentry(file)->d_inode;
2042f46b5a66SChristoph Hellwig 	struct btrfs_root *root = BTRFS_I(inode)->root;
2043f46b5a66SChristoph Hellwig 	struct btrfs_trans_handle *trans;
20441ab86aedSSage Weil 	int ret;
2045f46b5a66SChristoph Hellwig 
20461ab86aedSSage Weil 	ret = -EPERM;
2047df5b5520SChristoph Hellwig 	if (!capable(CAP_SYS_ADMIN))
2048f46b5a66SChristoph Hellwig 		goto out;
20491ab86aedSSage Weil 
20501ab86aedSSage Weil 	ret = -EINPROGRESS;
20511ab86aedSSage Weil 	if (file->private_data)
20521ab86aedSSage Weil 		goto out;
20539ca9ee09SSage Weil 
2054b83cc969SLi Zefan 	ret = -EROFS;
2055b83cc969SLi Zefan 	if (btrfs_root_readonly(root))
2056b83cc969SLi Zefan 		goto out;
2057b83cc969SLi Zefan 
2058c146afadSYan Zheng 	ret = mnt_want_write(file->f_path.mnt);
2059c146afadSYan Zheng 	if (ret)
2060c146afadSYan Zheng 		goto out;
2061c146afadSYan Zheng 
20629ca9ee09SSage Weil 	mutex_lock(&root->fs_info->trans_mutex);
20639ca9ee09SSage Weil 	root->fs_info->open_ioctl_trans++;
20649ca9ee09SSage Weil 	mutex_unlock(&root->fs_info->trans_mutex);
20659ca9ee09SSage Weil 
2066f46b5a66SChristoph Hellwig 	ret = -ENOMEM;
20671ab86aedSSage Weil 	trans = btrfs_start_ioctl_transaction(root, 0);
20681ab86aedSSage Weil 	if (!trans)
20691ab86aedSSage Weil 		goto out_drop;
20701ab86aedSSage Weil 
20711ab86aedSSage Weil 	file->private_data = trans;
20721ab86aedSSage Weil 	return 0;
20731ab86aedSSage Weil 
20741ab86aedSSage Weil out_drop:
20751ab86aedSSage Weil 	mutex_lock(&root->fs_info->trans_mutex);
20761ab86aedSSage Weil 	root->fs_info->open_ioctl_trans--;
20771ab86aedSSage Weil 	mutex_unlock(&root->fs_info->trans_mutex);
20781ab86aedSSage Weil 	mnt_drop_write(file->f_path.mnt);
2079f46b5a66SChristoph Hellwig out:
2080f46b5a66SChristoph Hellwig 	return ret;
2081f46b5a66SChristoph Hellwig }
2082f46b5a66SChristoph Hellwig 
20836ef5ed0dSJosef Bacik static long btrfs_ioctl_default_subvol(struct file *file, void __user *argp)
20846ef5ed0dSJosef Bacik {
20856ef5ed0dSJosef Bacik 	struct inode *inode = fdentry(file)->d_inode;
20866ef5ed0dSJosef Bacik 	struct btrfs_root *root = BTRFS_I(inode)->root;
20876ef5ed0dSJosef Bacik 	struct btrfs_root *new_root;
20886ef5ed0dSJosef Bacik 	struct btrfs_dir_item *di;
20896ef5ed0dSJosef Bacik 	struct btrfs_trans_handle *trans;
20906ef5ed0dSJosef Bacik 	struct btrfs_path *path;
20916ef5ed0dSJosef Bacik 	struct btrfs_key location;
20926ef5ed0dSJosef Bacik 	struct btrfs_disk_key disk_key;
20936ef5ed0dSJosef Bacik 	struct btrfs_super_block *disk_super;
20946ef5ed0dSJosef Bacik 	u64 features;
20956ef5ed0dSJosef Bacik 	u64 objectid = 0;
20966ef5ed0dSJosef Bacik 	u64 dir_id;
20976ef5ed0dSJosef Bacik 
20986ef5ed0dSJosef Bacik 	if (!capable(CAP_SYS_ADMIN))
20996ef5ed0dSJosef Bacik 		return -EPERM;
21006ef5ed0dSJosef Bacik 
21016ef5ed0dSJosef Bacik 	if (copy_from_user(&objectid, argp, sizeof(objectid)))
21026ef5ed0dSJosef Bacik 		return -EFAULT;
21036ef5ed0dSJosef Bacik 
21046ef5ed0dSJosef Bacik 	if (!objectid)
21056ef5ed0dSJosef Bacik 		objectid = root->root_key.objectid;
21066ef5ed0dSJosef Bacik 
21076ef5ed0dSJosef Bacik 	location.objectid = objectid;
21086ef5ed0dSJosef Bacik 	location.type = BTRFS_ROOT_ITEM_KEY;
21096ef5ed0dSJosef Bacik 	location.offset = (u64)-1;
21106ef5ed0dSJosef Bacik 
21116ef5ed0dSJosef Bacik 	new_root = btrfs_read_fs_root_no_name(root->fs_info, &location);
21126ef5ed0dSJosef Bacik 	if (IS_ERR(new_root))
21136ef5ed0dSJosef Bacik 		return PTR_ERR(new_root);
21146ef5ed0dSJosef Bacik 
21156ef5ed0dSJosef Bacik 	if (btrfs_root_refs(&new_root->root_item) == 0)
21166ef5ed0dSJosef Bacik 		return -ENOENT;
21176ef5ed0dSJosef Bacik 
21186ef5ed0dSJosef Bacik 	path = btrfs_alloc_path();
21196ef5ed0dSJosef Bacik 	if (!path)
21206ef5ed0dSJosef Bacik 		return -ENOMEM;
21216ef5ed0dSJosef Bacik 	path->leave_spinning = 1;
21226ef5ed0dSJosef Bacik 
21236ef5ed0dSJosef Bacik 	trans = btrfs_start_transaction(root, 1);
21246ef5ed0dSJosef Bacik 	if (!trans) {
21256ef5ed0dSJosef Bacik 		btrfs_free_path(path);
21266ef5ed0dSJosef Bacik 		return -ENOMEM;
21276ef5ed0dSJosef Bacik 	}
21286ef5ed0dSJosef Bacik 
21296ef5ed0dSJosef Bacik 	dir_id = btrfs_super_root_dir(&root->fs_info->super_copy);
21306ef5ed0dSJosef Bacik 	di = btrfs_lookup_dir_item(trans, root->fs_info->tree_root, path,
21316ef5ed0dSJosef Bacik 				   dir_id, "default", 7, 1);
2132cf1e99a4SDan Carpenter 	if (IS_ERR_OR_NULL(di)) {
21336ef5ed0dSJosef Bacik 		btrfs_free_path(path);
21346ef5ed0dSJosef Bacik 		btrfs_end_transaction(trans, root);
21356ef5ed0dSJosef Bacik 		printk(KERN_ERR "Umm, you don't have the default dir item, "
21366ef5ed0dSJosef Bacik 		       "this isn't going to work\n");
21376ef5ed0dSJosef Bacik 		return -ENOENT;
21386ef5ed0dSJosef Bacik 	}
21396ef5ed0dSJosef Bacik 
21406ef5ed0dSJosef Bacik 	btrfs_cpu_key_to_disk(&disk_key, &new_root->root_key);
21416ef5ed0dSJosef Bacik 	btrfs_set_dir_item_key(path->nodes[0], di, &disk_key);
21426ef5ed0dSJosef Bacik 	btrfs_mark_buffer_dirty(path->nodes[0]);
21436ef5ed0dSJosef Bacik 	btrfs_free_path(path);
21446ef5ed0dSJosef Bacik 
21456ef5ed0dSJosef Bacik 	disk_super = &root->fs_info->super_copy;
21466ef5ed0dSJosef Bacik 	features = btrfs_super_incompat_flags(disk_super);
21476ef5ed0dSJosef Bacik 	if (!(features & BTRFS_FEATURE_INCOMPAT_DEFAULT_SUBVOL)) {
21486ef5ed0dSJosef Bacik 		features |= BTRFS_FEATURE_INCOMPAT_DEFAULT_SUBVOL;
21496ef5ed0dSJosef Bacik 		btrfs_set_super_incompat_flags(disk_super, features);
21506ef5ed0dSJosef Bacik 	}
21516ef5ed0dSJosef Bacik 	btrfs_end_transaction(trans, root);
21526ef5ed0dSJosef Bacik 
21536ef5ed0dSJosef Bacik 	return 0;
21546ef5ed0dSJosef Bacik }
21556ef5ed0dSJosef Bacik 
2156bf5fc093SJosef Bacik static void get_block_group_info(struct list_head *groups_list,
2157bf5fc093SJosef Bacik 				 struct btrfs_ioctl_space_info *space)
2158bf5fc093SJosef Bacik {
2159bf5fc093SJosef Bacik 	struct btrfs_block_group_cache *block_group;
2160bf5fc093SJosef Bacik 
2161bf5fc093SJosef Bacik 	space->total_bytes = 0;
2162bf5fc093SJosef Bacik 	space->used_bytes = 0;
2163bf5fc093SJosef Bacik 	space->flags = 0;
2164bf5fc093SJosef Bacik 	list_for_each_entry(block_group, groups_list, list) {
2165bf5fc093SJosef Bacik 		space->flags = block_group->flags;
2166bf5fc093SJosef Bacik 		space->total_bytes += block_group->key.offset;
2167bf5fc093SJosef Bacik 		space->used_bytes +=
2168bf5fc093SJosef Bacik 			btrfs_block_group_used(&block_group->item);
2169bf5fc093SJosef Bacik 	}
2170bf5fc093SJosef Bacik }
2171bf5fc093SJosef Bacik 
21721406e432SJosef Bacik long btrfs_ioctl_space_info(struct btrfs_root *root, void __user *arg)
21731406e432SJosef Bacik {
21741406e432SJosef Bacik 	struct btrfs_ioctl_space_args space_args;
21751406e432SJosef Bacik 	struct btrfs_ioctl_space_info space;
21761406e432SJosef Bacik 	struct btrfs_ioctl_space_info *dest;
21777fde62bfSChris Mason 	struct btrfs_ioctl_space_info *dest_orig;
21787fde62bfSChris Mason 	struct btrfs_ioctl_space_info *user_dest;
21791406e432SJosef Bacik 	struct btrfs_space_info *info;
2180bf5fc093SJosef Bacik 	u64 types[] = {BTRFS_BLOCK_GROUP_DATA,
2181bf5fc093SJosef Bacik 		       BTRFS_BLOCK_GROUP_SYSTEM,
2182bf5fc093SJosef Bacik 		       BTRFS_BLOCK_GROUP_METADATA,
2183bf5fc093SJosef Bacik 		       BTRFS_BLOCK_GROUP_DATA | BTRFS_BLOCK_GROUP_METADATA};
2184bf5fc093SJosef Bacik 	int num_types = 4;
21857fde62bfSChris Mason 	int alloc_size;
21861406e432SJosef Bacik 	int ret = 0;
21877fde62bfSChris Mason 	int slot_count = 0;
2188bf5fc093SJosef Bacik 	int i, c;
21891406e432SJosef Bacik 
21901406e432SJosef Bacik 	if (copy_from_user(&space_args,
21911406e432SJosef Bacik 			   (struct btrfs_ioctl_space_args __user *)arg,
21921406e432SJosef Bacik 			   sizeof(space_args)))
21931406e432SJosef Bacik 		return -EFAULT;
21941406e432SJosef Bacik 
2195bf5fc093SJosef Bacik 	for (i = 0; i < num_types; i++) {
2196bf5fc093SJosef Bacik 		struct btrfs_space_info *tmp;
2197bf5fc093SJosef Bacik 
2198bf5fc093SJosef Bacik 		info = NULL;
21997fde62bfSChris Mason 		rcu_read_lock();
2200bf5fc093SJosef Bacik 		list_for_each_entry_rcu(tmp, &root->fs_info->space_info,
2201bf5fc093SJosef Bacik 					list) {
2202bf5fc093SJosef Bacik 			if (tmp->flags == types[i]) {
2203bf5fc093SJosef Bacik 				info = tmp;
2204bf5fc093SJosef Bacik 				break;
2205bf5fc093SJosef Bacik 			}
2206bf5fc093SJosef Bacik 		}
22077fde62bfSChris Mason 		rcu_read_unlock();
22081406e432SJosef Bacik 
2209bf5fc093SJosef Bacik 		if (!info)
2210bf5fc093SJosef Bacik 			continue;
2211bf5fc093SJosef Bacik 
2212bf5fc093SJosef Bacik 		down_read(&info->groups_sem);
2213bf5fc093SJosef Bacik 		for (c = 0; c < BTRFS_NR_RAID_TYPES; c++) {
2214bf5fc093SJosef Bacik 			if (!list_empty(&info->block_groups[c]))
2215bf5fc093SJosef Bacik 				slot_count++;
2216bf5fc093SJosef Bacik 		}
2217bf5fc093SJosef Bacik 		up_read(&info->groups_sem);
2218bf5fc093SJosef Bacik 	}
2219bf5fc093SJosef Bacik 
22207fde62bfSChris Mason 	/* space_slots == 0 means they are asking for a count */
22217fde62bfSChris Mason 	if (space_args.space_slots == 0) {
22227fde62bfSChris Mason 		space_args.total_spaces = slot_count;
22237fde62bfSChris Mason 		goto out;
22247fde62bfSChris Mason 	}
2225bf5fc093SJosef Bacik 
2226bf5fc093SJosef Bacik 	slot_count = min_t(int, space_args.space_slots, slot_count);
2227bf5fc093SJosef Bacik 
22287fde62bfSChris Mason 	alloc_size = sizeof(*dest) * slot_count;
2229bf5fc093SJosef Bacik 
22307fde62bfSChris Mason 	/* we generally have at most 6 or so space infos, one for each raid
22317fde62bfSChris Mason 	 * level.  So, a whole page should be more than enough for everyone
22327fde62bfSChris Mason 	 */
22337fde62bfSChris Mason 	if (alloc_size > PAGE_CACHE_SIZE)
22347fde62bfSChris Mason 		return -ENOMEM;
22357fde62bfSChris Mason 
22367fde62bfSChris Mason 	space_args.total_spaces = 0;
22377fde62bfSChris Mason 	dest = kmalloc(alloc_size, GFP_NOFS);
22387fde62bfSChris Mason 	if (!dest)
22397fde62bfSChris Mason 		return -ENOMEM;
22407fde62bfSChris Mason 	dest_orig = dest;
22417fde62bfSChris Mason 
22427fde62bfSChris Mason 	/* now we have a buffer to copy into */
2243bf5fc093SJosef Bacik 	for (i = 0; i < num_types; i++) {
2244bf5fc093SJosef Bacik 		struct btrfs_space_info *tmp;
2245bf5fc093SJosef Bacik 
2246bf5fc093SJosef Bacik 		info = NULL;
22471406e432SJosef Bacik 		rcu_read_lock();
2248bf5fc093SJosef Bacik 		list_for_each_entry_rcu(tmp, &root->fs_info->space_info,
2249bf5fc093SJosef Bacik 					list) {
2250bf5fc093SJosef Bacik 			if (tmp->flags == types[i]) {
2251bf5fc093SJosef Bacik 				info = tmp;
22527fde62bfSChris Mason 				break;
2253bf5fc093SJosef Bacik 			}
2254bf5fc093SJosef Bacik 		}
2255bf5fc093SJosef Bacik 		rcu_read_unlock();
22567fde62bfSChris Mason 
2257bf5fc093SJosef Bacik 		if (!info)
2258bf5fc093SJosef Bacik 			continue;
2259bf5fc093SJosef Bacik 		down_read(&info->groups_sem);
2260bf5fc093SJosef Bacik 		for (c = 0; c < BTRFS_NR_RAID_TYPES; c++) {
2261bf5fc093SJosef Bacik 			if (!list_empty(&info->block_groups[c])) {
2262bf5fc093SJosef Bacik 				get_block_group_info(&info->block_groups[c],
2263bf5fc093SJosef Bacik 						     &space);
22647fde62bfSChris Mason 				memcpy(dest, &space, sizeof(space));
22651406e432SJosef Bacik 				dest++;
22661406e432SJosef Bacik 				space_args.total_spaces++;
22671406e432SJosef Bacik 			}
2268bf5fc093SJosef Bacik 		}
2269bf5fc093SJosef Bacik 		up_read(&info->groups_sem);
2270bf5fc093SJosef Bacik 	}
22711406e432SJosef Bacik 
22727fde62bfSChris Mason 	user_dest = (struct btrfs_ioctl_space_info *)
22737fde62bfSChris Mason 		(arg + sizeof(struct btrfs_ioctl_space_args));
22747fde62bfSChris Mason 
22757fde62bfSChris Mason 	if (copy_to_user(user_dest, dest_orig, alloc_size))
22767fde62bfSChris Mason 		ret = -EFAULT;
22777fde62bfSChris Mason 
22787fde62bfSChris Mason 	kfree(dest_orig);
22797fde62bfSChris Mason out:
22807fde62bfSChris Mason 	if (ret == 0 && copy_to_user(arg, &space_args, sizeof(space_args)))
22811406e432SJosef Bacik 		ret = -EFAULT;
22821406e432SJosef Bacik 
22831406e432SJosef Bacik 	return ret;
22841406e432SJosef Bacik }
22851406e432SJosef Bacik 
2286f46b5a66SChristoph Hellwig /*
2287f46b5a66SChristoph Hellwig  * there are many ways the trans_start and trans_end ioctls can lead
2288f46b5a66SChristoph Hellwig  * to deadlocks.  They should only be used by applications that
2289f46b5a66SChristoph Hellwig  * basically own the machine, and have a very in depth understanding
2290f46b5a66SChristoph Hellwig  * of all the possible deadlocks and enospc problems.
2291f46b5a66SChristoph Hellwig  */
2292f46b5a66SChristoph Hellwig long btrfs_ioctl_trans_end(struct file *file)
2293f46b5a66SChristoph Hellwig {
2294f46b5a66SChristoph Hellwig 	struct inode *inode = fdentry(file)->d_inode;
2295f46b5a66SChristoph Hellwig 	struct btrfs_root *root = BTRFS_I(inode)->root;
2296f46b5a66SChristoph Hellwig 	struct btrfs_trans_handle *trans;
2297f46b5a66SChristoph Hellwig 
2298f46b5a66SChristoph Hellwig 	trans = file->private_data;
22991ab86aedSSage Weil 	if (!trans)
23001ab86aedSSage Weil 		return -EINVAL;
2301b214107eSChristoph Hellwig 	file->private_data = NULL;
23029ca9ee09SSage Weil 
23031ab86aedSSage Weil 	btrfs_end_transaction(trans, root);
23041ab86aedSSage Weil 
23059ca9ee09SSage Weil 	mutex_lock(&root->fs_info->trans_mutex);
23069ca9ee09SSage Weil 	root->fs_info->open_ioctl_trans--;
23079ca9ee09SSage Weil 	mutex_unlock(&root->fs_info->trans_mutex);
23089ca9ee09SSage Weil 
2309cfc8ea87SSage Weil 	mnt_drop_write(file->f_path.mnt);
23101ab86aedSSage Weil 	return 0;
2311f46b5a66SChristoph Hellwig }
2312f46b5a66SChristoph Hellwig 
231346204592SSage Weil static noinline long btrfs_ioctl_start_sync(struct file *file, void __user *argp)
231446204592SSage Weil {
231546204592SSage Weil 	struct btrfs_root *root = BTRFS_I(file->f_dentry->d_inode)->root;
231646204592SSage Weil 	struct btrfs_trans_handle *trans;
231746204592SSage Weil 	u64 transid;
231846204592SSage Weil 
231946204592SSage Weil 	trans = btrfs_start_transaction(root, 0);
232046204592SSage Weil 	transid = trans->transid;
232146204592SSage Weil 	btrfs_commit_transaction_async(trans, root, 0);
232246204592SSage Weil 
232346204592SSage Weil 	if (argp)
232446204592SSage Weil 		if (copy_to_user(argp, &transid, sizeof(transid)))
232546204592SSage Weil 			return -EFAULT;
232646204592SSage Weil 	return 0;
232746204592SSage Weil }
232846204592SSage Weil 
232946204592SSage Weil static noinline long btrfs_ioctl_wait_sync(struct file *file, void __user *argp)
233046204592SSage Weil {
233146204592SSage Weil 	struct btrfs_root *root = BTRFS_I(file->f_dentry->d_inode)->root;
233246204592SSage Weil 	u64 transid;
233346204592SSage Weil 
233446204592SSage Weil 	if (argp) {
233546204592SSage Weil 		if (copy_from_user(&transid, argp, sizeof(transid)))
233646204592SSage Weil 			return -EFAULT;
233746204592SSage Weil 	} else {
233846204592SSage Weil 		transid = 0;  /* current trans */
233946204592SSage Weil 	}
234046204592SSage Weil 	return btrfs_wait_for_commit(root, transid);
234146204592SSage Weil }
234246204592SSage Weil 
2343f46b5a66SChristoph Hellwig long btrfs_ioctl(struct file *file, unsigned int
2344f46b5a66SChristoph Hellwig 		cmd, unsigned long arg)
2345f46b5a66SChristoph Hellwig {
2346f46b5a66SChristoph Hellwig 	struct btrfs_root *root = BTRFS_I(fdentry(file)->d_inode)->root;
23474bcabaa3SChristoph Hellwig 	void __user *argp = (void __user *)arg;
2348f46b5a66SChristoph Hellwig 
2349f46b5a66SChristoph Hellwig 	switch (cmd) {
23506cbff00fSChristoph Hellwig 	case FS_IOC_GETFLAGS:
23516cbff00fSChristoph Hellwig 		return btrfs_ioctl_getflags(file, argp);
23526cbff00fSChristoph Hellwig 	case FS_IOC_SETFLAGS:
23536cbff00fSChristoph Hellwig 		return btrfs_ioctl_setflags(file, argp);
23546cbff00fSChristoph Hellwig 	case FS_IOC_GETVERSION:
23556cbff00fSChristoph Hellwig 		return btrfs_ioctl_getversion(file, argp);
2356f46b5a66SChristoph Hellwig 	case BTRFS_IOC_SNAP_CREATE:
2357fa0d2b9bSLi Zefan 		return btrfs_ioctl_snap_create(file, argp, 0);
2358fdfb1e4fSLi Zefan 	case BTRFS_IOC_SNAP_CREATE_V2:
2359fa0d2b9bSLi Zefan 		return btrfs_ioctl_snap_create_v2(file, argp, 0);
23603de4586cSChris Mason 	case BTRFS_IOC_SUBVOL_CREATE:
2361fa0d2b9bSLi Zefan 		return btrfs_ioctl_snap_create(file, argp, 1);
236276dda93cSYan, Zheng 	case BTRFS_IOC_SNAP_DESTROY:
236376dda93cSYan, Zheng 		return btrfs_ioctl_snap_destroy(file, argp);
2364*0caa102dSLi Zefan 	case BTRFS_IOC_SUBVOL_GETFLAGS:
2365*0caa102dSLi Zefan 		return btrfs_ioctl_subvol_getflags(file, argp);
2366*0caa102dSLi Zefan 	case BTRFS_IOC_SUBVOL_SETFLAGS:
2367*0caa102dSLi Zefan 		return btrfs_ioctl_subvol_setflags(file, argp);
23686ef5ed0dSJosef Bacik 	case BTRFS_IOC_DEFAULT_SUBVOL:
23696ef5ed0dSJosef Bacik 		return btrfs_ioctl_default_subvol(file, argp);
2370f46b5a66SChristoph Hellwig 	case BTRFS_IOC_DEFRAG:
23711e701a32SChris Mason 		return btrfs_ioctl_defrag(file, NULL);
23721e701a32SChris Mason 	case BTRFS_IOC_DEFRAG_RANGE:
23731e701a32SChris Mason 		return btrfs_ioctl_defrag(file, argp);
2374f46b5a66SChristoph Hellwig 	case BTRFS_IOC_RESIZE:
23754bcabaa3SChristoph Hellwig 		return btrfs_ioctl_resize(root, argp);
2376f46b5a66SChristoph Hellwig 	case BTRFS_IOC_ADD_DEV:
23774bcabaa3SChristoph Hellwig 		return btrfs_ioctl_add_dev(root, argp);
2378f46b5a66SChristoph Hellwig 	case BTRFS_IOC_RM_DEV:
23794bcabaa3SChristoph Hellwig 		return btrfs_ioctl_rm_dev(root, argp);
2380f46b5a66SChristoph Hellwig 	case BTRFS_IOC_BALANCE:
2381f46b5a66SChristoph Hellwig 		return btrfs_balance(root->fs_info->dev_root);
2382f46b5a66SChristoph Hellwig 	case BTRFS_IOC_CLONE:
2383c5c9cd4dSSage Weil 		return btrfs_ioctl_clone(file, arg, 0, 0, 0);
2384c5c9cd4dSSage Weil 	case BTRFS_IOC_CLONE_RANGE:
23857a865e8aSChristoph Hellwig 		return btrfs_ioctl_clone_range(file, argp);
2386f46b5a66SChristoph Hellwig 	case BTRFS_IOC_TRANS_START:
2387f46b5a66SChristoph Hellwig 		return btrfs_ioctl_trans_start(file);
2388f46b5a66SChristoph Hellwig 	case BTRFS_IOC_TRANS_END:
2389f46b5a66SChristoph Hellwig 		return btrfs_ioctl_trans_end(file);
2390ac8e9819SChris Mason 	case BTRFS_IOC_TREE_SEARCH:
2391ac8e9819SChris Mason 		return btrfs_ioctl_tree_search(file, argp);
2392ac8e9819SChris Mason 	case BTRFS_IOC_INO_LOOKUP:
2393ac8e9819SChris Mason 		return btrfs_ioctl_ino_lookup(file, argp);
23941406e432SJosef Bacik 	case BTRFS_IOC_SPACE_INFO:
23951406e432SJosef Bacik 		return btrfs_ioctl_space_info(root, argp);
2396f46b5a66SChristoph Hellwig 	case BTRFS_IOC_SYNC:
2397f46b5a66SChristoph Hellwig 		btrfs_sync_fs(file->f_dentry->d_sb, 1);
2398f46b5a66SChristoph Hellwig 		return 0;
239946204592SSage Weil 	case BTRFS_IOC_START_SYNC:
240046204592SSage Weil 		return btrfs_ioctl_start_sync(file, argp);
240146204592SSage Weil 	case BTRFS_IOC_WAIT_SYNC:
240246204592SSage Weil 		return btrfs_ioctl_wait_sync(file, argp);
2403f46b5a66SChristoph Hellwig 	}
2404f46b5a66SChristoph Hellwig 
2405f46b5a66SChristoph Hellwig 	return -ENOTTY;
2406f46b5a66SChristoph Hellwig }
2407