xref: /openbmc/linux/fs/btrfs/ioctl.c (revision 51788b1b)
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);
2063612b495STsutomu Itoh 	BUG_ON(IS_ERR(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;
6461a419d85SLi Zefan 	struct btrfs_super_block *disk_super;
647f46b5a66SChristoph Hellwig 	unsigned long last_index;
648f46b5a66SChristoph Hellwig 	unsigned long ra_pages = root->fs_info->bdi.ra_pages;
649f46b5a66SChristoph Hellwig 	unsigned long total_read = 0;
6501a419d85SLi Zefan 	u64 features;
651f46b5a66SChristoph Hellwig 	u64 page_start;
652f46b5a66SChristoph Hellwig 	u64 page_end;
653940100a4SChris Mason 	u64 last_len = 0;
654940100a4SChris Mason 	u64 skip = 0;
655940100a4SChris Mason 	u64 defrag_end = 0;
656f46b5a66SChristoph Hellwig 	unsigned long i;
657f46b5a66SChristoph Hellwig 	int ret;
6581a419d85SLi Zefan 	int compress_type = BTRFS_COMPRESS_ZLIB;
6591a419d85SLi Zefan 
6601a419d85SLi Zefan 	if (range->flags & BTRFS_DEFRAG_RANGE_COMPRESS) {
6611a419d85SLi Zefan 		if (range->compress_type > BTRFS_COMPRESS_TYPES)
6621a419d85SLi Zefan 			return -EINVAL;
6631a419d85SLi Zefan 		if (range->compress_type)
6641a419d85SLi Zefan 			compress_type = range->compress_type;
6651a419d85SLi Zefan 	}
666f46b5a66SChristoph Hellwig 
667940100a4SChris Mason 	if (inode->i_size == 0)
668940100a4SChris Mason 		return 0;
669f46b5a66SChristoph Hellwig 
6701e701a32SChris Mason 	if (range->start + range->len > range->start) {
6711e701a32SChris Mason 		last_index = min_t(u64, inode->i_size - 1,
6721e701a32SChris Mason 			 range->start + range->len - 1) >> PAGE_CACHE_SHIFT;
6731e701a32SChris Mason 	} else {
674940100a4SChris Mason 		last_index = (inode->i_size - 1) >> PAGE_CACHE_SHIFT;
6751e701a32SChris Mason 	}
6761e701a32SChris Mason 
6771e701a32SChris Mason 	i = range->start >> PAGE_CACHE_SHIFT;
678940100a4SChris Mason 	while (i <= last_index) {
679940100a4SChris Mason 		if (!should_defrag_range(inode, (u64)i << PAGE_CACHE_SHIFT,
6801e701a32SChris Mason 					PAGE_CACHE_SIZE,
6811e701a32SChris Mason 					range->extent_thresh,
6821e701a32SChris Mason 					&last_len, &skip,
683940100a4SChris Mason 					&defrag_end)) {
684940100a4SChris Mason 			unsigned long next;
685940100a4SChris Mason 			/*
686940100a4SChris Mason 			 * the should_defrag function tells us how much to skip
687940100a4SChris Mason 			 * bump our counter by the suggested amount
688940100a4SChris Mason 			 */
689940100a4SChris Mason 			next = (skip + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
690940100a4SChris Mason 			i = max(i + 1, next);
691940100a4SChris Mason 			continue;
692940100a4SChris Mason 		}
693940100a4SChris Mason 
694f46b5a66SChristoph Hellwig 		if (total_read % ra_pages == 0) {
695f46b5a66SChristoph Hellwig 			btrfs_force_ra(inode->i_mapping, &file->f_ra, file, i,
696f46b5a66SChristoph Hellwig 				       min(last_index, i + ra_pages - 1));
697f46b5a66SChristoph Hellwig 		}
698f46b5a66SChristoph Hellwig 		total_read++;
699940100a4SChris Mason 		mutex_lock(&inode->i_mutex);
7001e701a32SChris Mason 		if (range->flags & BTRFS_DEFRAG_RANGE_COMPRESS)
7011a419d85SLi Zefan 			BTRFS_I(inode)->force_compress = compress_type;
702940100a4SChris Mason 
7030ca1f7ceSYan, Zheng 		ret  = btrfs_delalloc_reserve_space(inode, PAGE_CACHE_SIZE);
7040ca1f7ceSYan, Zheng 		if (ret)
7050ca1f7ceSYan, Zheng 			goto err_unlock;
7063eaa2885SChris Mason again:
707940100a4SChris Mason 		if (inode->i_size == 0 ||
708940100a4SChris Mason 		    i > ((inode->i_size - 1) >> PAGE_CACHE_SHIFT)) {
709940100a4SChris Mason 			ret = 0;
710940100a4SChris Mason 			goto err_reservations;
711940100a4SChris Mason 		}
712940100a4SChris Mason 
713f46b5a66SChristoph Hellwig 		page = grab_cache_page(inode->i_mapping, i);
7140ca1f7ceSYan, Zheng 		if (!page) {
7150ca1f7ceSYan, Zheng 			ret = -ENOMEM;
716940100a4SChris Mason 			goto err_reservations;
7170ca1f7ceSYan, Zheng 		}
718940100a4SChris Mason 
719f46b5a66SChristoph Hellwig 		if (!PageUptodate(page)) {
720f46b5a66SChristoph Hellwig 			btrfs_readpage(NULL, page);
721f46b5a66SChristoph Hellwig 			lock_page(page);
722f46b5a66SChristoph Hellwig 			if (!PageUptodate(page)) {
723f46b5a66SChristoph Hellwig 				unlock_page(page);
724f46b5a66SChristoph Hellwig 				page_cache_release(page);
7250ca1f7ceSYan, Zheng 				ret = -EIO;
726940100a4SChris Mason 				goto err_reservations;
727f46b5a66SChristoph Hellwig 			}
728f46b5a66SChristoph Hellwig 		}
729f46b5a66SChristoph Hellwig 
730940100a4SChris Mason 		if (page->mapping != inode->i_mapping) {
731940100a4SChris Mason 			unlock_page(page);
732940100a4SChris Mason 			page_cache_release(page);
733940100a4SChris Mason 			goto again;
734940100a4SChris Mason 		}
735940100a4SChris Mason 
736f46b5a66SChristoph Hellwig 		wait_on_page_writeback(page);
737f46b5a66SChristoph Hellwig 
738940100a4SChris Mason 		if (PageDirty(page)) {
7390ca1f7ceSYan, Zheng 			btrfs_delalloc_release_space(inode, PAGE_CACHE_SIZE);
740940100a4SChris Mason 			goto loop_unlock;
741940100a4SChris Mason 		}
742940100a4SChris Mason 
743f46b5a66SChristoph Hellwig 		page_start = (u64)page->index << PAGE_CACHE_SHIFT;
744f46b5a66SChristoph Hellwig 		page_end = page_start + PAGE_CACHE_SIZE - 1;
745f46b5a66SChristoph Hellwig 		lock_extent(io_tree, page_start, page_end, GFP_NOFS);
7463eaa2885SChris Mason 
7473eaa2885SChris Mason 		ordered = btrfs_lookup_ordered_extent(inode, page_start);
7483eaa2885SChris Mason 		if (ordered) {
7493eaa2885SChris Mason 			unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
7503eaa2885SChris Mason 			unlock_page(page);
7513eaa2885SChris Mason 			page_cache_release(page);
7523eaa2885SChris Mason 			btrfs_start_ordered_extent(inode, ordered, 1);
7533eaa2885SChris Mason 			btrfs_put_ordered_extent(ordered);
7543eaa2885SChris Mason 			goto again;
7553eaa2885SChris Mason 		}
7563eaa2885SChris Mason 		set_page_extent_mapped(page);
7573eaa2885SChris Mason 
758f87f057bSChris Mason 		/*
759f87f057bSChris Mason 		 * this makes sure page_mkwrite is called on the
760f87f057bSChris Mason 		 * page if it is dirtied again later
761f87f057bSChris Mason 		 */
762f87f057bSChris Mason 		clear_page_dirty_for_io(page);
763940100a4SChris Mason 		clear_extent_bits(&BTRFS_I(inode)->io_tree, page_start,
764940100a4SChris Mason 				  page_end, EXTENT_DIRTY | EXTENT_DELALLOC |
765940100a4SChris Mason 				  EXTENT_DO_ACCOUNTING, GFP_NOFS);
766f87f057bSChris Mason 
7672ac55d41SJosef Bacik 		btrfs_set_extent_delalloc(inode, page_start, page_end, NULL);
768940100a4SChris Mason 		ClearPageChecked(page);
769f46b5a66SChristoph Hellwig 		set_page_dirty(page);
770a1ed835eSChris Mason 		unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
771940100a4SChris Mason 
772940100a4SChris Mason loop_unlock:
773f46b5a66SChristoph Hellwig 		unlock_page(page);
774f46b5a66SChristoph Hellwig 		page_cache_release(page);
775940100a4SChris Mason 		mutex_unlock(&inode->i_mutex);
776940100a4SChris Mason 
777f46b5a66SChristoph Hellwig 		balance_dirty_pages_ratelimited_nr(inode->i_mapping, 1);
778940100a4SChris Mason 		i++;
779f46b5a66SChristoph Hellwig 	}
780f46b5a66SChristoph Hellwig 
7811e701a32SChris Mason 	if ((range->flags & BTRFS_DEFRAG_RANGE_START_IO))
7821e701a32SChris Mason 		filemap_flush(inode->i_mapping);
7831e701a32SChris Mason 
7841e701a32SChris Mason 	if ((range->flags & BTRFS_DEFRAG_RANGE_COMPRESS)) {
7851e701a32SChris Mason 		/* the filemap_flush will queue IO into the worker threads, but
7861e701a32SChris Mason 		 * we have to make sure the IO is actually started and that
7871e701a32SChris Mason 		 * ordered extents get created before we return
7881e701a32SChris Mason 		 */
7891e701a32SChris Mason 		atomic_inc(&root->fs_info->async_submit_draining);
7901e701a32SChris Mason 		while (atomic_read(&root->fs_info->nr_async_submits) ||
7911e701a32SChris Mason 		      atomic_read(&root->fs_info->async_delalloc_pages)) {
7921e701a32SChris Mason 			wait_event(root->fs_info->async_submit_wait,
7931e701a32SChris Mason 			   (atomic_read(&root->fs_info->nr_async_submits) == 0 &&
7941e701a32SChris Mason 			    atomic_read(&root->fs_info->async_delalloc_pages) == 0));
7951e701a32SChris Mason 		}
7961e701a32SChris Mason 		atomic_dec(&root->fs_info->async_submit_draining);
7971e701a32SChris Mason 
7981e701a32SChris Mason 		mutex_lock(&inode->i_mutex);
799261507a0SLi Zefan 		BTRFS_I(inode)->force_compress = BTRFS_COMPRESS_NONE;
8001e701a32SChris Mason 		mutex_unlock(&inode->i_mutex);
8011e701a32SChris Mason 	}
8021e701a32SChris Mason 
8031a419d85SLi Zefan 	disk_super = &root->fs_info->super_copy;
8041a419d85SLi Zefan 	features = btrfs_super_incompat_flags(disk_super);
8051a419d85SLi Zefan 	if (range->compress_type == BTRFS_COMPRESS_LZO) {
8061a419d85SLi Zefan 		features |= BTRFS_FEATURE_INCOMPAT_COMPRESS_LZO;
8071a419d85SLi Zefan 		btrfs_set_super_incompat_flags(disk_super, features);
8081a419d85SLi Zefan 	}
8091a419d85SLi Zefan 
810f46b5a66SChristoph Hellwig 	return 0;
811940100a4SChris Mason 
812940100a4SChris Mason err_reservations:
8130ca1f7ceSYan, Zheng 	btrfs_delalloc_release_space(inode, PAGE_CACHE_SIZE);
8140ca1f7ceSYan, Zheng err_unlock:
815940100a4SChris Mason 	mutex_unlock(&inode->i_mutex);
816940100a4SChris Mason 	return ret;
817f46b5a66SChristoph Hellwig }
818f46b5a66SChristoph Hellwig 
81976dda93cSYan, Zheng static noinline int btrfs_ioctl_resize(struct btrfs_root *root,
82076dda93cSYan, Zheng 					void __user *arg)
821f46b5a66SChristoph Hellwig {
822f46b5a66SChristoph Hellwig 	u64 new_size;
823f46b5a66SChristoph Hellwig 	u64 old_size;
824f46b5a66SChristoph Hellwig 	u64 devid = 1;
825f46b5a66SChristoph Hellwig 	struct btrfs_ioctl_vol_args *vol_args;
826f46b5a66SChristoph Hellwig 	struct btrfs_trans_handle *trans;
827f46b5a66SChristoph Hellwig 	struct btrfs_device *device = NULL;
828f46b5a66SChristoph Hellwig 	char *sizestr;
829f46b5a66SChristoph Hellwig 	char *devstr = NULL;
830f46b5a66SChristoph Hellwig 	int ret = 0;
831f46b5a66SChristoph Hellwig 	int mod = 0;
832f46b5a66SChristoph Hellwig 
833c146afadSYan Zheng 	if (root->fs_info->sb->s_flags & MS_RDONLY)
834c146afadSYan Zheng 		return -EROFS;
835c146afadSYan Zheng 
836e441d54dSChris Mason 	if (!capable(CAP_SYS_ADMIN))
837e441d54dSChris Mason 		return -EPERM;
838e441d54dSChris Mason 
839dae7b665SLi Zefan 	vol_args = memdup_user(arg, sizeof(*vol_args));
840dae7b665SLi Zefan 	if (IS_ERR(vol_args))
841dae7b665SLi Zefan 		return PTR_ERR(vol_args);
8425516e595SMark Fasheh 
8435516e595SMark Fasheh 	vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
844f46b5a66SChristoph Hellwig 
8457d9eb12cSChris Mason 	mutex_lock(&root->fs_info->volume_mutex);
846f46b5a66SChristoph Hellwig 	sizestr = vol_args->name;
847f46b5a66SChristoph Hellwig 	devstr = strchr(sizestr, ':');
848f46b5a66SChristoph Hellwig 	if (devstr) {
849f46b5a66SChristoph Hellwig 		char *end;
850f46b5a66SChristoph Hellwig 		sizestr = devstr + 1;
851f46b5a66SChristoph Hellwig 		*devstr = '\0';
852f46b5a66SChristoph Hellwig 		devstr = vol_args->name;
853f46b5a66SChristoph Hellwig 		devid = simple_strtoull(devstr, &end, 10);
85421380931SJoel Becker 		printk(KERN_INFO "resizing devid %llu\n",
85521380931SJoel Becker 		       (unsigned long long)devid);
856f46b5a66SChristoph Hellwig 	}
8572b82032cSYan Zheng 	device = btrfs_find_device(root, devid, NULL, NULL);
858f46b5a66SChristoph Hellwig 	if (!device) {
85921380931SJoel Becker 		printk(KERN_INFO "resizer unable to find device %llu\n",
86021380931SJoel Becker 		       (unsigned long long)devid);
861f46b5a66SChristoph Hellwig 		ret = -EINVAL;
862f46b5a66SChristoph Hellwig 		goto out_unlock;
863f46b5a66SChristoph Hellwig 	}
864f46b5a66SChristoph Hellwig 	if (!strcmp(sizestr, "max"))
865f46b5a66SChristoph Hellwig 		new_size = device->bdev->bd_inode->i_size;
866f46b5a66SChristoph Hellwig 	else {
867f46b5a66SChristoph Hellwig 		if (sizestr[0] == '-') {
868f46b5a66SChristoph Hellwig 			mod = -1;
869f46b5a66SChristoph Hellwig 			sizestr++;
870f46b5a66SChristoph Hellwig 		} else if (sizestr[0] == '+') {
871f46b5a66SChristoph Hellwig 			mod = 1;
872f46b5a66SChristoph Hellwig 			sizestr++;
873f46b5a66SChristoph Hellwig 		}
87491748467SAkinobu Mita 		new_size = memparse(sizestr, NULL);
875f46b5a66SChristoph Hellwig 		if (new_size == 0) {
876f46b5a66SChristoph Hellwig 			ret = -EINVAL;
877f46b5a66SChristoph Hellwig 			goto out_unlock;
878f46b5a66SChristoph Hellwig 		}
879f46b5a66SChristoph Hellwig 	}
880f46b5a66SChristoph Hellwig 
881f46b5a66SChristoph Hellwig 	old_size = device->total_bytes;
882f46b5a66SChristoph Hellwig 
883f46b5a66SChristoph Hellwig 	if (mod < 0) {
884f46b5a66SChristoph Hellwig 		if (new_size > old_size) {
885f46b5a66SChristoph Hellwig 			ret = -EINVAL;
886f46b5a66SChristoph Hellwig 			goto out_unlock;
887f46b5a66SChristoph Hellwig 		}
888f46b5a66SChristoph Hellwig 		new_size = old_size - new_size;
889f46b5a66SChristoph Hellwig 	} else if (mod > 0) {
890f46b5a66SChristoph Hellwig 		new_size = old_size + new_size;
891f46b5a66SChristoph Hellwig 	}
892f46b5a66SChristoph Hellwig 
893f46b5a66SChristoph Hellwig 	if (new_size < 256 * 1024 * 1024) {
894f46b5a66SChristoph Hellwig 		ret = -EINVAL;
895f46b5a66SChristoph Hellwig 		goto out_unlock;
896f46b5a66SChristoph Hellwig 	}
897f46b5a66SChristoph Hellwig 	if (new_size > device->bdev->bd_inode->i_size) {
898f46b5a66SChristoph Hellwig 		ret = -EFBIG;
899f46b5a66SChristoph Hellwig 		goto out_unlock;
900f46b5a66SChristoph Hellwig 	}
901f46b5a66SChristoph Hellwig 
902f46b5a66SChristoph Hellwig 	do_div(new_size, root->sectorsize);
903f46b5a66SChristoph Hellwig 	new_size *= root->sectorsize;
904f46b5a66SChristoph Hellwig 
905f46b5a66SChristoph Hellwig 	printk(KERN_INFO "new size for %s is %llu\n",
906f46b5a66SChristoph Hellwig 		device->name, (unsigned long long)new_size);
907f46b5a66SChristoph Hellwig 
908f46b5a66SChristoph Hellwig 	if (new_size > old_size) {
909a22285a6SYan, Zheng 		trans = btrfs_start_transaction(root, 0);
91098d5dc13STsutomu Itoh 		if (IS_ERR(trans)) {
91198d5dc13STsutomu Itoh 			ret = PTR_ERR(trans);
91298d5dc13STsutomu Itoh 			goto out_unlock;
91398d5dc13STsutomu Itoh 		}
914f46b5a66SChristoph Hellwig 		ret = btrfs_grow_device(trans, device, new_size);
915f46b5a66SChristoph Hellwig 		btrfs_commit_transaction(trans, root);
916f46b5a66SChristoph Hellwig 	} else {
917f46b5a66SChristoph Hellwig 		ret = btrfs_shrink_device(device, new_size);
918f46b5a66SChristoph Hellwig 	}
919f46b5a66SChristoph Hellwig 
920f46b5a66SChristoph Hellwig out_unlock:
9217d9eb12cSChris Mason 	mutex_unlock(&root->fs_info->volume_mutex);
922f46b5a66SChristoph Hellwig 	kfree(vol_args);
923f46b5a66SChristoph Hellwig 	return ret;
924f46b5a66SChristoph Hellwig }
925f46b5a66SChristoph Hellwig 
92672fd032eSSage Weil static noinline int btrfs_ioctl_snap_create_transid(struct file *file,
92772fd032eSSage Weil 						    char *name,
92872fd032eSSage Weil 						    unsigned long fd,
92972fd032eSSage Weil 						    int subvol,
930b83cc969SLi Zefan 						    u64 *transid,
931b83cc969SLi Zefan 						    bool readonly)
932f46b5a66SChristoph Hellwig {
933cb8e7090SChristoph Hellwig 	struct btrfs_root *root = BTRFS_I(fdentry(file)->d_inode)->root;
9343de4586cSChris Mason 	struct file *src_file;
935f46b5a66SChristoph Hellwig 	int namelen;
9363de4586cSChris Mason 	int ret = 0;
937f46b5a66SChristoph Hellwig 
938c146afadSYan Zheng 	if (root->fs_info->sb->s_flags & MS_RDONLY)
939c146afadSYan Zheng 		return -EROFS;
940c146afadSYan Zheng 
94172fd032eSSage Weil 	namelen = strlen(name);
94272fd032eSSage Weil 	if (strchr(name, '/')) {
943f46b5a66SChristoph Hellwig 		ret = -EINVAL;
944f46b5a66SChristoph Hellwig 		goto out;
945f46b5a66SChristoph Hellwig 	}
946f46b5a66SChristoph Hellwig 
9473de4586cSChris Mason 	if (subvol) {
94872fd032eSSage Weil 		ret = btrfs_mksubvol(&file->f_path, name, namelen,
949b83cc969SLi Zefan 				     NULL, transid, readonly);
950cb8e7090SChristoph Hellwig 	} else {
9513de4586cSChris Mason 		struct inode *src_inode;
95272fd032eSSage Weil 		src_file = fget(fd);
9533de4586cSChris Mason 		if (!src_file) {
9543de4586cSChris Mason 			ret = -EINVAL;
9553de4586cSChris Mason 			goto out;
9563de4586cSChris Mason 		}
9573de4586cSChris Mason 
9583de4586cSChris Mason 		src_inode = src_file->f_path.dentry->d_inode;
9593de4586cSChris Mason 		if (src_inode->i_sb != file->f_path.dentry->d_inode->i_sb) {
960d397712bSChris Mason 			printk(KERN_INFO "btrfs: Snapshot src from "
961d397712bSChris Mason 			       "another FS\n");
9623de4586cSChris Mason 			ret = -EINVAL;
9633de4586cSChris Mason 			fput(src_file);
9643de4586cSChris Mason 			goto out;
9653de4586cSChris Mason 		}
96672fd032eSSage Weil 		ret = btrfs_mksubvol(&file->f_path, name, namelen,
96772fd032eSSage Weil 				     BTRFS_I(src_inode)->root,
968b83cc969SLi Zefan 				     transid, readonly);
9693de4586cSChris Mason 		fput(src_file);
970cb8e7090SChristoph Hellwig 	}
971f46b5a66SChristoph Hellwig out:
97272fd032eSSage Weil 	return ret;
97372fd032eSSage Weil }
97472fd032eSSage Weil 
97572fd032eSSage Weil static noinline int btrfs_ioctl_snap_create(struct file *file,
976fa0d2b9bSLi Zefan 					    void __user *arg, int subvol)
97772fd032eSSage Weil {
978fa0d2b9bSLi Zefan 	struct btrfs_ioctl_vol_args *vol_args;
97972fd032eSSage Weil 	int ret;
98072fd032eSSage Weil 
981fa0d2b9bSLi Zefan 	vol_args = memdup_user(arg, sizeof(*vol_args));
982fa0d2b9bSLi Zefan 	if (IS_ERR(vol_args))
983fa0d2b9bSLi Zefan 		return PTR_ERR(vol_args);
984fa0d2b9bSLi Zefan 	vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
985fa0d2b9bSLi Zefan 
986fa0d2b9bSLi Zefan 	ret = btrfs_ioctl_snap_create_transid(file, vol_args->name,
987b83cc969SLi Zefan 					      vol_args->fd, subvol,
988b83cc969SLi Zefan 					      NULL, false);
989fa0d2b9bSLi Zefan 
990fa0d2b9bSLi Zefan 	kfree(vol_args);
991fa0d2b9bSLi Zefan 	return ret;
992fa0d2b9bSLi Zefan }
993fa0d2b9bSLi Zefan 
994fa0d2b9bSLi Zefan static noinline int btrfs_ioctl_snap_create_v2(struct file *file,
995fa0d2b9bSLi Zefan 					       void __user *arg, int subvol)
996fa0d2b9bSLi Zefan {
997fa0d2b9bSLi Zefan 	struct btrfs_ioctl_vol_args_v2 *vol_args;
998fa0d2b9bSLi Zefan 	int ret;
999fdfb1e4fSLi Zefan 	u64 transid = 0;
1000fdfb1e4fSLi Zefan 	u64 *ptr = NULL;
1001b83cc969SLi Zefan 	bool readonly = false;
100272fd032eSSage Weil 
1003fa0d2b9bSLi Zefan 	vol_args = memdup_user(arg, sizeof(*vol_args));
1004fa0d2b9bSLi Zefan 	if (IS_ERR(vol_args))
1005fa0d2b9bSLi Zefan 		return PTR_ERR(vol_args);
1006fa0d2b9bSLi Zefan 	vol_args->name[BTRFS_SUBVOL_NAME_MAX] = '\0';
1007fdfb1e4fSLi Zefan 
1008b83cc969SLi Zefan 	if (vol_args->flags &
1009b83cc969SLi Zefan 	    ~(BTRFS_SUBVOL_CREATE_ASYNC | BTRFS_SUBVOL_RDONLY)) {
1010b83cc969SLi Zefan 		ret = -EOPNOTSUPP;
1011fdfb1e4fSLi Zefan 		goto out;
1012fdfb1e4fSLi Zefan 	}
1013fdfb1e4fSLi Zefan 
1014fa0d2b9bSLi Zefan 	if (vol_args->flags & BTRFS_SUBVOL_CREATE_ASYNC)
1015fdfb1e4fSLi Zefan 		ptr = &transid;
1016b83cc969SLi Zefan 	if (vol_args->flags & BTRFS_SUBVOL_RDONLY)
1017b83cc969SLi Zefan 		readonly = true;
101875eaa0e2SSage Weil 
1019fa0d2b9bSLi Zefan 	ret = btrfs_ioctl_snap_create_transid(file, vol_args->name,
1020b83cc969SLi Zefan 					      vol_args->fd, subvol,
1021b83cc969SLi Zefan 					      ptr, readonly);
102275eaa0e2SSage Weil 
1023fdfb1e4fSLi Zefan 	if (ret == 0 && ptr &&
102475eaa0e2SSage Weil 	    copy_to_user(arg +
1025fdfb1e4fSLi Zefan 			 offsetof(struct btrfs_ioctl_vol_args_v2,
1026fdfb1e4fSLi Zefan 				  transid), ptr, sizeof(*ptr)))
102775eaa0e2SSage Weil 		ret = -EFAULT;
1028fdfb1e4fSLi Zefan out:
1029f46b5a66SChristoph Hellwig 	kfree(vol_args);
1030f46b5a66SChristoph Hellwig 	return ret;
1031f46b5a66SChristoph Hellwig }
1032f46b5a66SChristoph Hellwig 
10330caa102dSLi Zefan static noinline int btrfs_ioctl_subvol_getflags(struct file *file,
10340caa102dSLi Zefan 						void __user *arg)
10350caa102dSLi Zefan {
10360caa102dSLi Zefan 	struct inode *inode = fdentry(file)->d_inode;
10370caa102dSLi Zefan 	struct btrfs_root *root = BTRFS_I(inode)->root;
10380caa102dSLi Zefan 	int ret = 0;
10390caa102dSLi Zefan 	u64 flags = 0;
10400caa102dSLi Zefan 
10410caa102dSLi Zefan 	if (inode->i_ino != BTRFS_FIRST_FREE_OBJECTID)
10420caa102dSLi Zefan 		return -EINVAL;
10430caa102dSLi Zefan 
10440caa102dSLi Zefan 	down_read(&root->fs_info->subvol_sem);
10450caa102dSLi Zefan 	if (btrfs_root_readonly(root))
10460caa102dSLi Zefan 		flags |= BTRFS_SUBVOL_RDONLY;
10470caa102dSLi Zefan 	up_read(&root->fs_info->subvol_sem);
10480caa102dSLi Zefan 
10490caa102dSLi Zefan 	if (copy_to_user(arg, &flags, sizeof(flags)))
10500caa102dSLi Zefan 		ret = -EFAULT;
10510caa102dSLi Zefan 
10520caa102dSLi Zefan 	return ret;
10530caa102dSLi Zefan }
10540caa102dSLi Zefan 
10550caa102dSLi Zefan static noinline int btrfs_ioctl_subvol_setflags(struct file *file,
10560caa102dSLi Zefan 					      void __user *arg)
10570caa102dSLi Zefan {
10580caa102dSLi Zefan 	struct inode *inode = fdentry(file)->d_inode;
10590caa102dSLi Zefan 	struct btrfs_root *root = BTRFS_I(inode)->root;
10600caa102dSLi Zefan 	struct btrfs_trans_handle *trans;
10610caa102dSLi Zefan 	u64 root_flags;
10620caa102dSLi Zefan 	u64 flags;
10630caa102dSLi Zefan 	int ret = 0;
10640caa102dSLi Zefan 
10650caa102dSLi Zefan 	if (root->fs_info->sb->s_flags & MS_RDONLY)
10660caa102dSLi Zefan 		return -EROFS;
10670caa102dSLi Zefan 
10680caa102dSLi Zefan 	if (inode->i_ino != BTRFS_FIRST_FREE_OBJECTID)
10690caa102dSLi Zefan 		return -EINVAL;
10700caa102dSLi Zefan 
10710caa102dSLi Zefan 	if (copy_from_user(&flags, arg, sizeof(flags)))
10720caa102dSLi Zefan 		return -EFAULT;
10730caa102dSLi Zefan 
10740caa102dSLi Zefan 	if (flags & ~BTRFS_SUBVOL_CREATE_ASYNC)
10750caa102dSLi Zefan 		return -EINVAL;
10760caa102dSLi Zefan 
10770caa102dSLi Zefan 	if (flags & ~BTRFS_SUBVOL_RDONLY)
10780caa102dSLi Zefan 		return -EOPNOTSUPP;
10790caa102dSLi Zefan 
10800caa102dSLi Zefan 	down_write(&root->fs_info->subvol_sem);
10810caa102dSLi Zefan 
10820caa102dSLi Zefan 	/* nothing to do */
10830caa102dSLi Zefan 	if (!!(flags & BTRFS_SUBVOL_RDONLY) == btrfs_root_readonly(root))
10840caa102dSLi Zefan 		goto out;
10850caa102dSLi Zefan 
10860caa102dSLi Zefan 	root_flags = btrfs_root_flags(&root->root_item);
10870caa102dSLi Zefan 	if (flags & BTRFS_SUBVOL_RDONLY)
10880caa102dSLi Zefan 		btrfs_set_root_flags(&root->root_item,
10890caa102dSLi Zefan 				     root_flags | BTRFS_ROOT_SUBVOL_RDONLY);
10900caa102dSLi Zefan 	else
10910caa102dSLi Zefan 		btrfs_set_root_flags(&root->root_item,
10920caa102dSLi Zefan 				     root_flags & ~BTRFS_ROOT_SUBVOL_RDONLY);
10930caa102dSLi Zefan 
10940caa102dSLi Zefan 	trans = btrfs_start_transaction(root, 1);
10950caa102dSLi Zefan 	if (IS_ERR(trans)) {
10960caa102dSLi Zefan 		ret = PTR_ERR(trans);
10970caa102dSLi Zefan 		goto out_reset;
10980caa102dSLi Zefan 	}
10990caa102dSLi Zefan 
11000caa102dSLi Zefan 	ret = btrfs_update_root(trans, root,
11010caa102dSLi Zefan 				&root->root_key, &root->root_item);
11020caa102dSLi Zefan 
11030caa102dSLi Zefan 	btrfs_commit_transaction(trans, root);
11040caa102dSLi Zefan out_reset:
11050caa102dSLi Zefan 	if (ret)
11060caa102dSLi Zefan 		btrfs_set_root_flags(&root->root_item, root_flags);
11070caa102dSLi Zefan out:
11080caa102dSLi Zefan 	up_write(&root->fs_info->subvol_sem);
11090caa102dSLi Zefan 	return ret;
11100caa102dSLi Zefan }
11110caa102dSLi Zefan 
111276dda93cSYan, Zheng /*
111376dda93cSYan, Zheng  * helper to check if the subvolume references other subvolumes
111476dda93cSYan, Zheng  */
111576dda93cSYan, Zheng static noinline int may_destroy_subvol(struct btrfs_root *root)
111676dda93cSYan, Zheng {
111776dda93cSYan, Zheng 	struct btrfs_path *path;
111876dda93cSYan, Zheng 	struct btrfs_key key;
111976dda93cSYan, Zheng 	int ret;
112076dda93cSYan, Zheng 
112176dda93cSYan, Zheng 	path = btrfs_alloc_path();
112276dda93cSYan, Zheng 	if (!path)
112376dda93cSYan, Zheng 		return -ENOMEM;
112476dda93cSYan, Zheng 
112576dda93cSYan, Zheng 	key.objectid = root->root_key.objectid;
112676dda93cSYan, Zheng 	key.type = BTRFS_ROOT_REF_KEY;
112776dda93cSYan, Zheng 	key.offset = (u64)-1;
112876dda93cSYan, Zheng 
112976dda93cSYan, Zheng 	ret = btrfs_search_slot(NULL, root->fs_info->tree_root,
113076dda93cSYan, Zheng 				&key, path, 0, 0);
113176dda93cSYan, Zheng 	if (ret < 0)
113276dda93cSYan, Zheng 		goto out;
113376dda93cSYan, Zheng 	BUG_ON(ret == 0);
113476dda93cSYan, Zheng 
113576dda93cSYan, Zheng 	ret = 0;
113676dda93cSYan, Zheng 	if (path->slots[0] > 0) {
113776dda93cSYan, Zheng 		path->slots[0]--;
113876dda93cSYan, Zheng 		btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
113976dda93cSYan, Zheng 		if (key.objectid == root->root_key.objectid &&
114076dda93cSYan, Zheng 		    key.type == BTRFS_ROOT_REF_KEY)
114176dda93cSYan, Zheng 			ret = -ENOTEMPTY;
114276dda93cSYan, Zheng 	}
114376dda93cSYan, Zheng out:
114476dda93cSYan, Zheng 	btrfs_free_path(path);
114576dda93cSYan, Zheng 	return ret;
114676dda93cSYan, Zheng }
114776dda93cSYan, Zheng 
1148ac8e9819SChris Mason static noinline int key_in_sk(struct btrfs_key *key,
1149ac8e9819SChris Mason 			      struct btrfs_ioctl_search_key *sk)
1150ac8e9819SChris Mason {
1151abc6e134SChris Mason 	struct btrfs_key test;
1152abc6e134SChris Mason 	int ret;
1153abc6e134SChris Mason 
1154abc6e134SChris Mason 	test.objectid = sk->min_objectid;
1155abc6e134SChris Mason 	test.type = sk->min_type;
1156abc6e134SChris Mason 	test.offset = sk->min_offset;
1157abc6e134SChris Mason 
1158abc6e134SChris Mason 	ret = btrfs_comp_cpu_keys(key, &test);
1159abc6e134SChris Mason 	if (ret < 0)
1160ac8e9819SChris Mason 		return 0;
1161abc6e134SChris Mason 
1162abc6e134SChris Mason 	test.objectid = sk->max_objectid;
1163abc6e134SChris Mason 	test.type = sk->max_type;
1164abc6e134SChris Mason 	test.offset = sk->max_offset;
1165abc6e134SChris Mason 
1166abc6e134SChris Mason 	ret = btrfs_comp_cpu_keys(key, &test);
1167abc6e134SChris Mason 	if (ret > 0)
1168ac8e9819SChris Mason 		return 0;
1169ac8e9819SChris Mason 	return 1;
1170ac8e9819SChris Mason }
1171ac8e9819SChris Mason 
1172ac8e9819SChris Mason static noinline int copy_to_sk(struct btrfs_root *root,
1173ac8e9819SChris Mason 			       struct btrfs_path *path,
1174ac8e9819SChris Mason 			       struct btrfs_key *key,
1175ac8e9819SChris Mason 			       struct btrfs_ioctl_search_key *sk,
1176ac8e9819SChris Mason 			       char *buf,
1177ac8e9819SChris Mason 			       unsigned long *sk_offset,
1178ac8e9819SChris Mason 			       int *num_found)
1179ac8e9819SChris Mason {
1180ac8e9819SChris Mason 	u64 found_transid;
1181ac8e9819SChris Mason 	struct extent_buffer *leaf;
1182ac8e9819SChris Mason 	struct btrfs_ioctl_search_header sh;
1183ac8e9819SChris Mason 	unsigned long item_off;
1184ac8e9819SChris Mason 	unsigned long item_len;
1185ac8e9819SChris Mason 	int nritems;
1186ac8e9819SChris Mason 	int i;
1187ac8e9819SChris Mason 	int slot;
1188ac8e9819SChris Mason 	int found = 0;
1189ac8e9819SChris Mason 	int ret = 0;
1190ac8e9819SChris Mason 
1191ac8e9819SChris Mason 	leaf = path->nodes[0];
1192ac8e9819SChris Mason 	slot = path->slots[0];
1193ac8e9819SChris Mason 	nritems = btrfs_header_nritems(leaf);
1194ac8e9819SChris Mason 
1195ac8e9819SChris Mason 	if (btrfs_header_generation(leaf) > sk->max_transid) {
1196ac8e9819SChris Mason 		i = nritems;
1197ac8e9819SChris Mason 		goto advance_key;
1198ac8e9819SChris Mason 	}
1199ac8e9819SChris Mason 	found_transid = btrfs_header_generation(leaf);
1200ac8e9819SChris Mason 
1201ac8e9819SChris Mason 	for (i = slot; i < nritems; i++) {
1202ac8e9819SChris Mason 		item_off = btrfs_item_ptr_offset(leaf, i);
1203ac8e9819SChris Mason 		item_len = btrfs_item_size_nr(leaf, i);
1204ac8e9819SChris Mason 
1205ac8e9819SChris Mason 		if (item_len > BTRFS_SEARCH_ARGS_BUFSIZE)
1206ac8e9819SChris Mason 			item_len = 0;
1207ac8e9819SChris Mason 
1208ac8e9819SChris Mason 		if (sizeof(sh) + item_len + *sk_offset >
1209ac8e9819SChris Mason 		    BTRFS_SEARCH_ARGS_BUFSIZE) {
1210ac8e9819SChris Mason 			ret = 1;
1211ac8e9819SChris Mason 			goto overflow;
1212ac8e9819SChris Mason 		}
1213ac8e9819SChris Mason 
1214ac8e9819SChris Mason 		btrfs_item_key_to_cpu(leaf, key, i);
1215ac8e9819SChris Mason 		if (!key_in_sk(key, sk))
1216ac8e9819SChris Mason 			continue;
1217ac8e9819SChris Mason 
1218ac8e9819SChris Mason 		sh.objectid = key->objectid;
1219ac8e9819SChris Mason 		sh.offset = key->offset;
1220ac8e9819SChris Mason 		sh.type = key->type;
1221ac8e9819SChris Mason 		sh.len = item_len;
1222ac8e9819SChris Mason 		sh.transid = found_transid;
1223ac8e9819SChris Mason 
1224ac8e9819SChris Mason 		/* copy search result header */
1225ac8e9819SChris Mason 		memcpy(buf + *sk_offset, &sh, sizeof(sh));
1226ac8e9819SChris Mason 		*sk_offset += sizeof(sh);
1227ac8e9819SChris Mason 
1228ac8e9819SChris Mason 		if (item_len) {
1229ac8e9819SChris Mason 			char *p = buf + *sk_offset;
1230ac8e9819SChris Mason 			/* copy the item */
1231ac8e9819SChris Mason 			read_extent_buffer(leaf, p,
1232ac8e9819SChris Mason 					   item_off, item_len);
1233ac8e9819SChris Mason 			*sk_offset += item_len;
1234ac8e9819SChris Mason 		}
123590fdde14SChris Mason 		found++;
1236ac8e9819SChris Mason 
1237ac8e9819SChris Mason 		if (*num_found >= sk->nr_items)
1238ac8e9819SChris Mason 			break;
1239ac8e9819SChris Mason 	}
1240ac8e9819SChris Mason advance_key:
1241ac8e9819SChris Mason 	ret = 0;
1242abc6e134SChris Mason 	if (key->offset < (u64)-1 && key->offset < sk->max_offset)
1243abc6e134SChris Mason 		key->offset++;
1244abc6e134SChris Mason 	else if (key->type < (u8)-1 && key->type < sk->max_type) {
1245abc6e134SChris Mason 		key->offset = 0;
1246abc6e134SChris Mason 		key->type++;
1247abc6e134SChris Mason 	} else if (key->objectid < (u64)-1 && key->objectid < sk->max_objectid) {
1248abc6e134SChris Mason 		key->offset = 0;
1249abc6e134SChris Mason 		key->type = 0;
1250abc6e134SChris Mason 		key->objectid++;
1251abc6e134SChris Mason 	} else
1252abc6e134SChris Mason 		ret = 1;
1253ac8e9819SChris Mason overflow:
1254ac8e9819SChris Mason 	*num_found += found;
1255ac8e9819SChris Mason 	return ret;
1256ac8e9819SChris Mason }
1257ac8e9819SChris Mason 
1258ac8e9819SChris Mason static noinline int search_ioctl(struct inode *inode,
1259ac8e9819SChris Mason 				 struct btrfs_ioctl_search_args *args)
1260ac8e9819SChris Mason {
1261ac8e9819SChris Mason 	struct btrfs_root *root;
1262ac8e9819SChris Mason 	struct btrfs_key key;
1263ac8e9819SChris Mason 	struct btrfs_key max_key;
1264ac8e9819SChris Mason 	struct btrfs_path *path;
1265ac8e9819SChris Mason 	struct btrfs_ioctl_search_key *sk = &args->key;
1266ac8e9819SChris Mason 	struct btrfs_fs_info *info = BTRFS_I(inode)->root->fs_info;
1267ac8e9819SChris Mason 	int ret;
1268ac8e9819SChris Mason 	int num_found = 0;
1269ac8e9819SChris Mason 	unsigned long sk_offset = 0;
1270ac8e9819SChris Mason 
1271ac8e9819SChris Mason 	path = btrfs_alloc_path();
1272ac8e9819SChris Mason 	if (!path)
1273ac8e9819SChris Mason 		return -ENOMEM;
1274ac8e9819SChris Mason 
1275ac8e9819SChris Mason 	if (sk->tree_id == 0) {
1276ac8e9819SChris Mason 		/* search the root of the inode that was passed */
1277ac8e9819SChris Mason 		root = BTRFS_I(inode)->root;
1278ac8e9819SChris Mason 	} else {
1279ac8e9819SChris Mason 		key.objectid = sk->tree_id;
1280ac8e9819SChris Mason 		key.type = BTRFS_ROOT_ITEM_KEY;
1281ac8e9819SChris Mason 		key.offset = (u64)-1;
1282ac8e9819SChris Mason 		root = btrfs_read_fs_root_no_name(info, &key);
1283ac8e9819SChris Mason 		if (IS_ERR(root)) {
1284ac8e9819SChris Mason 			printk(KERN_ERR "could not find root %llu\n",
1285ac8e9819SChris Mason 			       sk->tree_id);
1286ac8e9819SChris Mason 			btrfs_free_path(path);
1287ac8e9819SChris Mason 			return -ENOENT;
1288ac8e9819SChris Mason 		}
1289ac8e9819SChris Mason 	}
1290ac8e9819SChris Mason 
1291ac8e9819SChris Mason 	key.objectid = sk->min_objectid;
1292ac8e9819SChris Mason 	key.type = sk->min_type;
1293ac8e9819SChris Mason 	key.offset = sk->min_offset;
1294ac8e9819SChris Mason 
1295ac8e9819SChris Mason 	max_key.objectid = sk->max_objectid;
1296ac8e9819SChris Mason 	max_key.type = sk->max_type;
1297ac8e9819SChris Mason 	max_key.offset = sk->max_offset;
1298ac8e9819SChris Mason 
1299ac8e9819SChris Mason 	path->keep_locks = 1;
1300ac8e9819SChris Mason 
1301ac8e9819SChris Mason 	while(1) {
1302ac8e9819SChris Mason 		ret = btrfs_search_forward(root, &key, &max_key, path, 0,
1303ac8e9819SChris Mason 					   sk->min_transid);
1304ac8e9819SChris Mason 		if (ret != 0) {
1305ac8e9819SChris Mason 			if (ret > 0)
1306ac8e9819SChris Mason 				ret = 0;
1307ac8e9819SChris Mason 			goto err;
1308ac8e9819SChris Mason 		}
1309ac8e9819SChris Mason 		ret = copy_to_sk(root, path, &key, sk, args->buf,
1310ac8e9819SChris Mason 				 &sk_offset, &num_found);
1311ac8e9819SChris Mason 		btrfs_release_path(root, path);
1312ac8e9819SChris Mason 		if (ret || num_found >= sk->nr_items)
1313ac8e9819SChris Mason 			break;
1314ac8e9819SChris Mason 
1315ac8e9819SChris Mason 	}
1316ac8e9819SChris Mason 	ret = 0;
1317ac8e9819SChris Mason err:
1318ac8e9819SChris Mason 	sk->nr_items = num_found;
1319ac8e9819SChris Mason 	btrfs_free_path(path);
1320ac8e9819SChris Mason 	return ret;
1321ac8e9819SChris Mason }
1322ac8e9819SChris Mason 
1323ac8e9819SChris Mason static noinline int btrfs_ioctl_tree_search(struct file *file,
1324ac8e9819SChris Mason 					   void __user *argp)
1325ac8e9819SChris Mason {
1326ac8e9819SChris Mason 	 struct btrfs_ioctl_search_args *args;
1327ac8e9819SChris Mason 	 struct inode *inode;
1328ac8e9819SChris Mason 	 int ret;
1329ac8e9819SChris Mason 
1330ac8e9819SChris Mason 	if (!capable(CAP_SYS_ADMIN))
1331ac8e9819SChris Mason 		return -EPERM;
1332ac8e9819SChris Mason 
13332354d08fSJulia Lawall 	args = memdup_user(argp, sizeof(*args));
13342354d08fSJulia Lawall 	if (IS_ERR(args))
13352354d08fSJulia Lawall 		return PTR_ERR(args);
1336ac8e9819SChris Mason 
1337ac8e9819SChris Mason 	inode = fdentry(file)->d_inode;
1338ac8e9819SChris Mason 	ret = search_ioctl(inode, args);
1339ac8e9819SChris Mason 	if (ret == 0 && copy_to_user(argp, args, sizeof(*args)))
1340ac8e9819SChris Mason 		ret = -EFAULT;
1341ac8e9819SChris Mason 	kfree(args);
1342ac8e9819SChris Mason 	return ret;
1343ac8e9819SChris Mason }
1344ac8e9819SChris Mason 
134598d377a0STARUISI Hiroaki /*
1346ac8e9819SChris Mason  * Search INODE_REFs to identify path name of 'dirid' directory
1347ac8e9819SChris Mason  * in a 'tree_id' tree. and sets path name to 'name'.
134898d377a0STARUISI Hiroaki  */
134998d377a0STARUISI Hiroaki static noinline int btrfs_search_path_in_tree(struct btrfs_fs_info *info,
135098d377a0STARUISI Hiroaki 				u64 tree_id, u64 dirid, char *name)
135198d377a0STARUISI Hiroaki {
135298d377a0STARUISI Hiroaki 	struct btrfs_root *root;
135398d377a0STARUISI Hiroaki 	struct btrfs_key key;
1354ac8e9819SChris Mason 	char *ptr;
135598d377a0STARUISI Hiroaki 	int ret = -1;
135698d377a0STARUISI Hiroaki 	int slot;
135798d377a0STARUISI Hiroaki 	int len;
135898d377a0STARUISI Hiroaki 	int total_len = 0;
135998d377a0STARUISI Hiroaki 	struct btrfs_inode_ref *iref;
136098d377a0STARUISI Hiroaki 	struct extent_buffer *l;
136198d377a0STARUISI Hiroaki 	struct btrfs_path *path;
136298d377a0STARUISI Hiroaki 
136398d377a0STARUISI Hiroaki 	if (dirid == BTRFS_FIRST_FREE_OBJECTID) {
136498d377a0STARUISI Hiroaki 		name[0]='\0';
136598d377a0STARUISI Hiroaki 		return 0;
136698d377a0STARUISI Hiroaki 	}
136798d377a0STARUISI Hiroaki 
136898d377a0STARUISI Hiroaki 	path = btrfs_alloc_path();
136998d377a0STARUISI Hiroaki 	if (!path)
137098d377a0STARUISI Hiroaki 		return -ENOMEM;
137198d377a0STARUISI Hiroaki 
1372ac8e9819SChris Mason 	ptr = &name[BTRFS_INO_LOOKUP_PATH_MAX];
137398d377a0STARUISI Hiroaki 
137498d377a0STARUISI Hiroaki 	key.objectid = tree_id;
137598d377a0STARUISI Hiroaki 	key.type = BTRFS_ROOT_ITEM_KEY;
137698d377a0STARUISI Hiroaki 	key.offset = (u64)-1;
137798d377a0STARUISI Hiroaki 	root = btrfs_read_fs_root_no_name(info, &key);
137898d377a0STARUISI Hiroaki 	if (IS_ERR(root)) {
137998d377a0STARUISI Hiroaki 		printk(KERN_ERR "could not find root %llu\n", tree_id);
13808ad6fcabSChris Mason 		ret = -ENOENT;
13818ad6fcabSChris Mason 		goto out;
138298d377a0STARUISI Hiroaki 	}
138398d377a0STARUISI Hiroaki 
138498d377a0STARUISI Hiroaki 	key.objectid = dirid;
138598d377a0STARUISI Hiroaki 	key.type = BTRFS_INODE_REF_KEY;
13868ad6fcabSChris Mason 	key.offset = (u64)-1;
138798d377a0STARUISI Hiroaki 
138898d377a0STARUISI Hiroaki 	while(1) {
138998d377a0STARUISI Hiroaki 		ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
139098d377a0STARUISI Hiroaki 		if (ret < 0)
139198d377a0STARUISI Hiroaki 			goto out;
139298d377a0STARUISI Hiroaki 
139398d377a0STARUISI Hiroaki 		l = path->nodes[0];
139498d377a0STARUISI Hiroaki 		slot = path->slots[0];
13958ad6fcabSChris Mason 		if (ret > 0 && slot > 0)
13968ad6fcabSChris Mason 			slot--;
139798d377a0STARUISI Hiroaki 		btrfs_item_key_to_cpu(l, &key, slot);
139898d377a0STARUISI Hiroaki 
139998d377a0STARUISI Hiroaki 		if (ret > 0 && (key.objectid != dirid ||
1400ac8e9819SChris Mason 				key.type != BTRFS_INODE_REF_KEY)) {
1401ac8e9819SChris Mason 			ret = -ENOENT;
140298d377a0STARUISI Hiroaki 			goto out;
1403ac8e9819SChris Mason 		}
140498d377a0STARUISI Hiroaki 
140598d377a0STARUISI Hiroaki 		iref = btrfs_item_ptr(l, slot, struct btrfs_inode_ref);
140698d377a0STARUISI Hiroaki 		len = btrfs_inode_ref_name_len(l, iref);
140798d377a0STARUISI Hiroaki 		ptr -= len + 1;
140898d377a0STARUISI Hiroaki 		total_len += len + 1;
1409ac8e9819SChris Mason 		if (ptr < name)
141098d377a0STARUISI Hiroaki 			goto out;
141198d377a0STARUISI Hiroaki 
141298d377a0STARUISI Hiroaki 		*(ptr + len) = '/';
141398d377a0STARUISI Hiroaki 		read_extent_buffer(l, ptr,(unsigned long)(iref + 1), len);
141498d377a0STARUISI Hiroaki 
141598d377a0STARUISI Hiroaki 		if (key.offset == BTRFS_FIRST_FREE_OBJECTID)
141698d377a0STARUISI Hiroaki 			break;
141798d377a0STARUISI Hiroaki 
141898d377a0STARUISI Hiroaki 		btrfs_release_path(root, path);
141998d377a0STARUISI Hiroaki 		key.objectid = key.offset;
14208ad6fcabSChris Mason 		key.offset = (u64)-1;
142198d377a0STARUISI Hiroaki 		dirid = key.objectid;
142298d377a0STARUISI Hiroaki 
142398d377a0STARUISI Hiroaki 	}
1424ac8e9819SChris Mason 	if (ptr < name)
142598d377a0STARUISI Hiroaki 		goto out;
1426ac8e9819SChris Mason 	memcpy(name, ptr, total_len);
142798d377a0STARUISI Hiroaki 	name[total_len]='\0';
142898d377a0STARUISI Hiroaki 	ret = 0;
142998d377a0STARUISI Hiroaki out:
143098d377a0STARUISI Hiroaki 	btrfs_free_path(path);
1431ac8e9819SChris Mason 	return ret;
1432ac8e9819SChris Mason }
1433ac8e9819SChris Mason 
1434ac8e9819SChris Mason static noinline int btrfs_ioctl_ino_lookup(struct file *file,
1435ac8e9819SChris Mason 					   void __user *argp)
1436ac8e9819SChris Mason {
1437ac8e9819SChris Mason 	 struct btrfs_ioctl_ino_lookup_args *args;
1438ac8e9819SChris Mason 	 struct inode *inode;
1439ac8e9819SChris Mason 	 int ret;
1440ac8e9819SChris Mason 
1441ac8e9819SChris Mason 	if (!capable(CAP_SYS_ADMIN))
1442ac8e9819SChris Mason 		return -EPERM;
1443ac8e9819SChris Mason 
14442354d08fSJulia Lawall 	args = memdup_user(argp, sizeof(*args));
14452354d08fSJulia Lawall 	if (IS_ERR(args))
14462354d08fSJulia Lawall 		return PTR_ERR(args);
1447c2b96929SDan Carpenter 
1448ac8e9819SChris Mason 	inode = fdentry(file)->d_inode;
1449ac8e9819SChris Mason 
14501b53ac4dSChris Mason 	if (args->treeid == 0)
14511b53ac4dSChris Mason 		args->treeid = BTRFS_I(inode)->root->root_key.objectid;
14521b53ac4dSChris Mason 
1453ac8e9819SChris Mason 	ret = btrfs_search_path_in_tree(BTRFS_I(inode)->root->fs_info,
1454ac8e9819SChris Mason 					args->treeid, args->objectid,
1455ac8e9819SChris Mason 					args->name);
1456ac8e9819SChris Mason 
1457ac8e9819SChris Mason 	if (ret == 0 && copy_to_user(argp, args, sizeof(*args)))
1458ac8e9819SChris Mason 		ret = -EFAULT;
1459ac8e9819SChris Mason 
1460ac8e9819SChris Mason 	kfree(args);
146198d377a0STARUISI Hiroaki 	return ret;
146298d377a0STARUISI Hiroaki }
146398d377a0STARUISI Hiroaki 
146476dda93cSYan, Zheng static noinline int btrfs_ioctl_snap_destroy(struct file *file,
146576dda93cSYan, Zheng 					     void __user *arg)
146676dda93cSYan, Zheng {
146776dda93cSYan, Zheng 	struct dentry *parent = fdentry(file);
146876dda93cSYan, Zheng 	struct dentry *dentry;
146976dda93cSYan, Zheng 	struct inode *dir = parent->d_inode;
147076dda93cSYan, Zheng 	struct inode *inode;
147176dda93cSYan, Zheng 	struct btrfs_root *root = BTRFS_I(dir)->root;
147276dda93cSYan, Zheng 	struct btrfs_root *dest = NULL;
147376dda93cSYan, Zheng 	struct btrfs_ioctl_vol_args *vol_args;
147476dda93cSYan, Zheng 	struct btrfs_trans_handle *trans;
147576dda93cSYan, Zheng 	int namelen;
147676dda93cSYan, Zheng 	int ret;
147776dda93cSYan, Zheng 	int err = 0;
147876dda93cSYan, Zheng 
147976dda93cSYan, Zheng 	vol_args = memdup_user(arg, sizeof(*vol_args));
148076dda93cSYan, Zheng 	if (IS_ERR(vol_args))
148176dda93cSYan, Zheng 		return PTR_ERR(vol_args);
148276dda93cSYan, Zheng 
148376dda93cSYan, Zheng 	vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
148476dda93cSYan, Zheng 	namelen = strlen(vol_args->name);
148576dda93cSYan, Zheng 	if (strchr(vol_args->name, '/') ||
148676dda93cSYan, Zheng 	    strncmp(vol_args->name, "..", namelen) == 0) {
148776dda93cSYan, Zheng 		err = -EINVAL;
148876dda93cSYan, Zheng 		goto out;
148976dda93cSYan, Zheng 	}
149076dda93cSYan, Zheng 
149176dda93cSYan, Zheng 	err = mnt_want_write(file->f_path.mnt);
149276dda93cSYan, Zheng 	if (err)
149376dda93cSYan, Zheng 		goto out;
149476dda93cSYan, Zheng 
149576dda93cSYan, Zheng 	mutex_lock_nested(&dir->i_mutex, I_MUTEX_PARENT);
149676dda93cSYan, Zheng 	dentry = lookup_one_len(vol_args->name, parent, namelen);
149776dda93cSYan, Zheng 	if (IS_ERR(dentry)) {
149876dda93cSYan, Zheng 		err = PTR_ERR(dentry);
149976dda93cSYan, Zheng 		goto out_unlock_dir;
150076dda93cSYan, Zheng 	}
150176dda93cSYan, Zheng 
150276dda93cSYan, Zheng 	if (!dentry->d_inode) {
150376dda93cSYan, Zheng 		err = -ENOENT;
150476dda93cSYan, Zheng 		goto out_dput;
150576dda93cSYan, Zheng 	}
150676dda93cSYan, Zheng 
150776dda93cSYan, Zheng 	inode = dentry->d_inode;
15084260f7c7SSage Weil 	dest = BTRFS_I(inode)->root;
15094260f7c7SSage Weil 	if (!capable(CAP_SYS_ADMIN)){
15104260f7c7SSage Weil 		/*
15114260f7c7SSage Weil 		 * Regular user.  Only allow this with a special mount
15124260f7c7SSage Weil 		 * option, when the user has write+exec access to the
15134260f7c7SSage Weil 		 * subvol root, and when rmdir(2) would have been
15144260f7c7SSage Weil 		 * allowed.
15154260f7c7SSage Weil 		 *
15164260f7c7SSage Weil 		 * Note that this is _not_ check that the subvol is
15174260f7c7SSage Weil 		 * empty or doesn't contain data that we wouldn't
15184260f7c7SSage Weil 		 * otherwise be able to delete.
15194260f7c7SSage Weil 		 *
15204260f7c7SSage Weil 		 * Users who want to delete empty subvols should try
15214260f7c7SSage Weil 		 * rmdir(2).
15224260f7c7SSage Weil 		 */
15234260f7c7SSage Weil 		err = -EPERM;
15244260f7c7SSage Weil 		if (!btrfs_test_opt(root, USER_SUBVOL_RM_ALLOWED))
15254260f7c7SSage Weil 			goto out_dput;
15264260f7c7SSage Weil 
15274260f7c7SSage Weil 		/*
15284260f7c7SSage Weil 		 * Do not allow deletion if the parent dir is the same
15294260f7c7SSage Weil 		 * as the dir to be deleted.  That means the ioctl
15304260f7c7SSage Weil 		 * must be called on the dentry referencing the root
15314260f7c7SSage Weil 		 * of the subvol, not a random directory contained
15324260f7c7SSage Weil 		 * within it.
15334260f7c7SSage Weil 		 */
15344260f7c7SSage Weil 		err = -EINVAL;
15354260f7c7SSage Weil 		if (root == dest)
15364260f7c7SSage Weil 			goto out_dput;
15374260f7c7SSage Weil 
15384260f7c7SSage Weil 		err = inode_permission(inode, MAY_WRITE | MAY_EXEC);
15394260f7c7SSage Weil 		if (err)
15404260f7c7SSage Weil 			goto out_dput;
15414260f7c7SSage Weil 
15424260f7c7SSage Weil 		/* check if subvolume may be deleted by a non-root user */
15434260f7c7SSage Weil 		err = btrfs_may_delete(dir, dentry, 1);
15444260f7c7SSage Weil 		if (err)
15454260f7c7SSage Weil 			goto out_dput;
15464260f7c7SSage Weil 	}
15474260f7c7SSage Weil 
154876dda93cSYan, Zheng 	if (inode->i_ino != BTRFS_FIRST_FREE_OBJECTID) {
154976dda93cSYan, Zheng 		err = -EINVAL;
155076dda93cSYan, Zheng 		goto out_dput;
155176dda93cSYan, Zheng 	}
155276dda93cSYan, Zheng 
155376dda93cSYan, Zheng 	mutex_lock(&inode->i_mutex);
155476dda93cSYan, Zheng 	err = d_invalidate(dentry);
155576dda93cSYan, Zheng 	if (err)
155676dda93cSYan, Zheng 		goto out_unlock;
155776dda93cSYan, Zheng 
155876dda93cSYan, Zheng 	down_write(&root->fs_info->subvol_sem);
155976dda93cSYan, Zheng 
156076dda93cSYan, Zheng 	err = may_destroy_subvol(dest);
156176dda93cSYan, Zheng 	if (err)
156276dda93cSYan, Zheng 		goto out_up_write;
156376dda93cSYan, Zheng 
1564a22285a6SYan, Zheng 	trans = btrfs_start_transaction(root, 0);
1565a22285a6SYan, Zheng 	if (IS_ERR(trans)) {
1566a22285a6SYan, Zheng 		err = PTR_ERR(trans);
1567d327099aSDan Carpenter 		goto out_up_write;
1568a22285a6SYan, Zheng 	}
1569a22285a6SYan, Zheng 	trans->block_rsv = &root->fs_info->global_block_rsv;
1570a22285a6SYan, Zheng 
157176dda93cSYan, Zheng 	ret = btrfs_unlink_subvol(trans, root, dir,
157276dda93cSYan, Zheng 				dest->root_key.objectid,
157376dda93cSYan, Zheng 				dentry->d_name.name,
157476dda93cSYan, Zheng 				dentry->d_name.len);
157576dda93cSYan, Zheng 	BUG_ON(ret);
157676dda93cSYan, Zheng 
157776dda93cSYan, Zheng 	btrfs_record_root_in_trans(trans, dest);
157876dda93cSYan, Zheng 
157976dda93cSYan, Zheng 	memset(&dest->root_item.drop_progress, 0,
158076dda93cSYan, Zheng 		sizeof(dest->root_item.drop_progress));
158176dda93cSYan, Zheng 	dest->root_item.drop_level = 0;
158276dda93cSYan, Zheng 	btrfs_set_root_refs(&dest->root_item, 0);
158376dda93cSYan, Zheng 
1584d68fc57bSYan, Zheng 	if (!xchg(&dest->orphan_item_inserted, 1)) {
158576dda93cSYan, Zheng 		ret = btrfs_insert_orphan_item(trans,
158676dda93cSYan, Zheng 					root->fs_info->tree_root,
158776dda93cSYan, Zheng 					dest->root_key.objectid);
158876dda93cSYan, Zheng 		BUG_ON(ret);
1589d68fc57bSYan, Zheng 	}
159076dda93cSYan, Zheng 
1591531cb13fSSage Weil 	ret = btrfs_end_transaction(trans, root);
159276dda93cSYan, Zheng 	BUG_ON(ret);
159376dda93cSYan, Zheng 	inode->i_flags |= S_DEAD;
159476dda93cSYan, Zheng out_up_write:
159576dda93cSYan, Zheng 	up_write(&root->fs_info->subvol_sem);
159676dda93cSYan, Zheng out_unlock:
159776dda93cSYan, Zheng 	mutex_unlock(&inode->i_mutex);
159876dda93cSYan, Zheng 	if (!err) {
1599efefb143SYan, Zheng 		shrink_dcache_sb(root->fs_info->sb);
160076dda93cSYan, Zheng 		btrfs_invalidate_inodes(dest);
160176dda93cSYan, Zheng 		d_delete(dentry);
160276dda93cSYan, Zheng 	}
160376dda93cSYan, Zheng out_dput:
160476dda93cSYan, Zheng 	dput(dentry);
160576dda93cSYan, Zheng out_unlock_dir:
160676dda93cSYan, Zheng 	mutex_unlock(&dir->i_mutex);
160776dda93cSYan, Zheng 	mnt_drop_write(file->f_path.mnt);
160876dda93cSYan, Zheng out:
160976dda93cSYan, Zheng 	kfree(vol_args);
161076dda93cSYan, Zheng 	return err;
161176dda93cSYan, Zheng }
161276dda93cSYan, Zheng 
16131e701a32SChris Mason static int btrfs_ioctl_defrag(struct file *file, void __user *argp)
1614f46b5a66SChristoph Hellwig {
1615f46b5a66SChristoph Hellwig 	struct inode *inode = fdentry(file)->d_inode;
1616f46b5a66SChristoph Hellwig 	struct btrfs_root *root = BTRFS_I(inode)->root;
16171e701a32SChris Mason 	struct btrfs_ioctl_defrag_range_args *range;
1618c146afadSYan Zheng 	int ret;
1619c146afadSYan Zheng 
1620b83cc969SLi Zefan 	if (btrfs_root_readonly(root))
1621b83cc969SLi Zefan 		return -EROFS;
1622b83cc969SLi Zefan 
1623c146afadSYan Zheng 	ret = mnt_want_write(file->f_path.mnt);
1624c146afadSYan Zheng 	if (ret)
1625c146afadSYan Zheng 		return ret;
1626f46b5a66SChristoph Hellwig 
1627f46b5a66SChristoph Hellwig 	switch (inode->i_mode & S_IFMT) {
1628f46b5a66SChristoph Hellwig 	case S_IFDIR:
1629e441d54dSChris Mason 		if (!capable(CAP_SYS_ADMIN)) {
1630e441d54dSChris Mason 			ret = -EPERM;
1631e441d54dSChris Mason 			goto out;
1632e441d54dSChris Mason 		}
16338929ecfaSYan, Zheng 		ret = btrfs_defrag_root(root, 0);
16348929ecfaSYan, Zheng 		if (ret)
16358929ecfaSYan, Zheng 			goto out;
16368929ecfaSYan, Zheng 		ret = btrfs_defrag_root(root->fs_info->extent_root, 0);
1637f46b5a66SChristoph Hellwig 		break;
1638f46b5a66SChristoph Hellwig 	case S_IFREG:
1639e441d54dSChris Mason 		if (!(file->f_mode & FMODE_WRITE)) {
1640e441d54dSChris Mason 			ret = -EINVAL;
1641e441d54dSChris Mason 			goto out;
1642e441d54dSChris Mason 		}
16431e701a32SChris Mason 
16441e701a32SChris Mason 		range = kzalloc(sizeof(*range), GFP_KERNEL);
16451e701a32SChris Mason 		if (!range) {
16461e701a32SChris Mason 			ret = -ENOMEM;
16471e701a32SChris Mason 			goto out;
16481e701a32SChris Mason 		}
16491e701a32SChris Mason 
16501e701a32SChris Mason 		if (argp) {
16511e701a32SChris Mason 			if (copy_from_user(range, argp,
16521e701a32SChris Mason 					   sizeof(*range))) {
16531e701a32SChris Mason 				ret = -EFAULT;
16541e701a32SChris Mason 				kfree(range);
1655683be16eSDan Carpenter 				goto out;
16561e701a32SChris Mason 			}
16571e701a32SChris Mason 			/* compression requires us to start the IO */
16581e701a32SChris Mason 			if ((range->flags & BTRFS_DEFRAG_RANGE_COMPRESS)) {
16591e701a32SChris Mason 				range->flags |= BTRFS_DEFRAG_RANGE_START_IO;
16601e701a32SChris Mason 				range->extent_thresh = (u32)-1;
16611e701a32SChris Mason 			}
16621e701a32SChris Mason 		} else {
16631e701a32SChris Mason 			/* the rest are all set to zero by kzalloc */
16641e701a32SChris Mason 			range->len = (u64)-1;
16651e701a32SChris Mason 		}
16668929ecfaSYan, Zheng 		ret = btrfs_defrag_file(file, range);
16671e701a32SChris Mason 		kfree(range);
1668f46b5a66SChristoph Hellwig 		break;
16698929ecfaSYan, Zheng 	default:
16708929ecfaSYan, Zheng 		ret = -EINVAL;
1671f46b5a66SChristoph Hellwig 	}
1672e441d54dSChris Mason out:
1673ab67b7c1SYan Zheng 	mnt_drop_write(file->f_path.mnt);
1674e441d54dSChris Mason 	return ret;
1675f46b5a66SChristoph Hellwig }
1676f46b5a66SChristoph Hellwig 
1677b2950863SChristoph Hellwig static long btrfs_ioctl_add_dev(struct btrfs_root *root, void __user *arg)
1678f46b5a66SChristoph Hellwig {
1679f46b5a66SChristoph Hellwig 	struct btrfs_ioctl_vol_args *vol_args;
1680f46b5a66SChristoph Hellwig 	int ret;
1681f46b5a66SChristoph Hellwig 
1682e441d54dSChris Mason 	if (!capable(CAP_SYS_ADMIN))
1683e441d54dSChris Mason 		return -EPERM;
1684e441d54dSChris Mason 
1685dae7b665SLi Zefan 	vol_args = memdup_user(arg, sizeof(*vol_args));
1686dae7b665SLi Zefan 	if (IS_ERR(vol_args))
1687dae7b665SLi Zefan 		return PTR_ERR(vol_args);
1688f46b5a66SChristoph Hellwig 
16895516e595SMark Fasheh 	vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
1690f46b5a66SChristoph Hellwig 	ret = btrfs_init_new_device(root, vol_args->name);
1691f46b5a66SChristoph Hellwig 
1692f46b5a66SChristoph Hellwig 	kfree(vol_args);
1693f46b5a66SChristoph Hellwig 	return ret;
1694f46b5a66SChristoph Hellwig }
1695f46b5a66SChristoph Hellwig 
1696b2950863SChristoph Hellwig static long btrfs_ioctl_rm_dev(struct btrfs_root *root, void __user *arg)
1697f46b5a66SChristoph Hellwig {
1698f46b5a66SChristoph Hellwig 	struct btrfs_ioctl_vol_args *vol_args;
1699f46b5a66SChristoph Hellwig 	int ret;
1700f46b5a66SChristoph Hellwig 
1701e441d54dSChris Mason 	if (!capable(CAP_SYS_ADMIN))
1702e441d54dSChris Mason 		return -EPERM;
1703e441d54dSChris Mason 
1704c146afadSYan Zheng 	if (root->fs_info->sb->s_flags & MS_RDONLY)
1705c146afadSYan Zheng 		return -EROFS;
1706c146afadSYan Zheng 
1707dae7b665SLi Zefan 	vol_args = memdup_user(arg, sizeof(*vol_args));
1708dae7b665SLi Zefan 	if (IS_ERR(vol_args))
1709dae7b665SLi Zefan 		return PTR_ERR(vol_args);
1710f46b5a66SChristoph Hellwig 
17115516e595SMark Fasheh 	vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
1712f46b5a66SChristoph Hellwig 	ret = btrfs_rm_device(root, vol_args->name);
1713f46b5a66SChristoph Hellwig 
1714f46b5a66SChristoph Hellwig 	kfree(vol_args);
1715f46b5a66SChristoph Hellwig 	return ret;
1716f46b5a66SChristoph Hellwig }
1717f46b5a66SChristoph Hellwig 
171876dda93cSYan, Zheng static noinline long btrfs_ioctl_clone(struct file *file, unsigned long srcfd,
1719b2950863SChristoph Hellwig 				       u64 off, u64 olen, u64 destoff)
1720f46b5a66SChristoph Hellwig {
1721f46b5a66SChristoph Hellwig 	struct inode *inode = fdentry(file)->d_inode;
1722f46b5a66SChristoph Hellwig 	struct btrfs_root *root = BTRFS_I(inode)->root;
1723f46b5a66SChristoph Hellwig 	struct file *src_file;
1724f46b5a66SChristoph Hellwig 	struct inode *src;
1725f46b5a66SChristoph Hellwig 	struct btrfs_trans_handle *trans;
1726f46b5a66SChristoph Hellwig 	struct btrfs_path *path;
1727f46b5a66SChristoph Hellwig 	struct extent_buffer *leaf;
1728ae01a0abSYan Zheng 	char *buf;
1729ae01a0abSYan Zheng 	struct btrfs_key key;
1730f46b5a66SChristoph Hellwig 	u32 nritems;
1731f46b5a66SChristoph Hellwig 	int slot;
1732ae01a0abSYan Zheng 	int ret;
1733c5c9cd4dSSage Weil 	u64 len = olen;
1734c5c9cd4dSSage Weil 	u64 bs = root->fs_info->sb->s_blocksize;
1735c5c9cd4dSSage Weil 	u64 hint_byte;
1736d20f7043SChris Mason 
1737c5c9cd4dSSage Weil 	/*
1738c5c9cd4dSSage Weil 	 * TODO:
1739c5c9cd4dSSage Weil 	 * - split compressed inline extents.  annoying: we need to
1740c5c9cd4dSSage Weil 	 *   decompress into destination's address_space (the file offset
1741c5c9cd4dSSage Weil 	 *   may change, so source mapping won't do), then recompress (or
1742c5c9cd4dSSage Weil 	 *   otherwise reinsert) a subrange.
1743c5c9cd4dSSage Weil 	 * - allow ranges within the same file to be cloned (provided
1744c5c9cd4dSSage Weil 	 *   they don't overlap)?
1745c5c9cd4dSSage Weil 	 */
1746c5c9cd4dSSage Weil 
1747e441d54dSChris Mason 	/* the destination must be opened for writing */
17482ebc3464SDan Rosenberg 	if (!(file->f_mode & FMODE_WRITE) || (file->f_flags & O_APPEND))
1749e441d54dSChris Mason 		return -EINVAL;
1750e441d54dSChris Mason 
1751b83cc969SLi Zefan 	if (btrfs_root_readonly(root))
1752b83cc969SLi Zefan 		return -EROFS;
1753b83cc969SLi Zefan 
1754c146afadSYan Zheng 	ret = mnt_want_write(file->f_path.mnt);
1755c146afadSYan Zheng 	if (ret)
1756c146afadSYan Zheng 		return ret;
1757c146afadSYan Zheng 
1758c5c9cd4dSSage Weil 	src_file = fget(srcfd);
1759ab67b7c1SYan Zheng 	if (!src_file) {
1760ab67b7c1SYan Zheng 		ret = -EBADF;
1761ab67b7c1SYan Zheng 		goto out_drop_write;
1762ab67b7c1SYan Zheng 	}
17635dc64164SDan Rosenberg 
1764f46b5a66SChristoph Hellwig 	src = src_file->f_dentry->d_inode;
1765f46b5a66SChristoph Hellwig 
1766c5c9cd4dSSage Weil 	ret = -EINVAL;
1767c5c9cd4dSSage Weil 	if (src == inode)
1768c5c9cd4dSSage Weil 		goto out_fput;
1769c5c9cd4dSSage Weil 
17705dc64164SDan Rosenberg 	/* the src must be open for reading */
17715dc64164SDan Rosenberg 	if (!(src_file->f_mode & FMODE_READ))
17725dc64164SDan Rosenberg 		goto out_fput;
17735dc64164SDan Rosenberg 
1774ae01a0abSYan Zheng 	ret = -EISDIR;
1775ae01a0abSYan Zheng 	if (S_ISDIR(src->i_mode) || S_ISDIR(inode->i_mode))
1776f46b5a66SChristoph Hellwig 		goto out_fput;
1777f46b5a66SChristoph Hellwig 
1778ae01a0abSYan Zheng 	ret = -EXDEV;
1779ae01a0abSYan Zheng 	if (src->i_sb != inode->i_sb || BTRFS_I(src)->root != root)
1780ae01a0abSYan Zheng 		goto out_fput;
1781ae01a0abSYan Zheng 
1782ae01a0abSYan Zheng 	ret = -ENOMEM;
1783ae01a0abSYan Zheng 	buf = vmalloc(btrfs_level_size(root, 0));
1784ae01a0abSYan Zheng 	if (!buf)
1785ae01a0abSYan Zheng 		goto out_fput;
1786ae01a0abSYan Zheng 
1787ae01a0abSYan Zheng 	path = btrfs_alloc_path();
1788ae01a0abSYan Zheng 	if (!path) {
1789ae01a0abSYan Zheng 		vfree(buf);
1790ae01a0abSYan Zheng 		goto out_fput;
1791ae01a0abSYan Zheng 	}
1792ae01a0abSYan Zheng 	path->reada = 2;
1793ae01a0abSYan Zheng 
1794f46b5a66SChristoph Hellwig 	if (inode < src) {
1795fccdae43SSage Weil 		mutex_lock_nested(&inode->i_mutex, I_MUTEX_PARENT);
1796fccdae43SSage Weil 		mutex_lock_nested(&src->i_mutex, I_MUTEX_CHILD);
1797f46b5a66SChristoph Hellwig 	} else {
1798fccdae43SSage Weil 		mutex_lock_nested(&src->i_mutex, I_MUTEX_PARENT);
1799fccdae43SSage Weil 		mutex_lock_nested(&inode->i_mutex, I_MUTEX_CHILD);
1800f46b5a66SChristoph Hellwig 	}
1801f46b5a66SChristoph Hellwig 
1802c5c9cd4dSSage Weil 	/* determine range to clone */
1803c5c9cd4dSSage Weil 	ret = -EINVAL;
18042ebc3464SDan Rosenberg 	if (off + len > src->i_size || off + len < off)
1805f46b5a66SChristoph Hellwig 		goto out_unlock;
1806c5c9cd4dSSage Weil 	if (len == 0)
1807c5c9cd4dSSage Weil 		olen = len = src->i_size - off;
1808c5c9cd4dSSage Weil 	/* if we extend to eof, continue to block boundary */
1809c5c9cd4dSSage Weil 	if (off + len == src->i_size)
18102a6b8daeSLi Zefan 		len = ALIGN(src->i_size, bs) - off;
1811c5c9cd4dSSage Weil 
1812c5c9cd4dSSage Weil 	/* verify the end result is block aligned */
18132a6b8daeSLi Zefan 	if (!IS_ALIGNED(off, bs) || !IS_ALIGNED(off + len, bs) ||
18142a6b8daeSLi Zefan 	    !IS_ALIGNED(destoff, bs))
1815c5c9cd4dSSage Weil 		goto out_unlock;
1816c5c9cd4dSSage Weil 
1817f46b5a66SChristoph Hellwig 	/* do any pending delalloc/csum calc on src, one way or
1818f46b5a66SChristoph Hellwig 	   another, and lock file content */
1819f46b5a66SChristoph Hellwig 	while (1) {
182031840ae1SZheng Yan 		struct btrfs_ordered_extent *ordered;
1821c5c9cd4dSSage Weil 		lock_extent(&BTRFS_I(src)->io_tree, off, off+len, GFP_NOFS);
18229a019196SSage Weil 		ordered = btrfs_lookup_first_ordered_extent(src, off+len);
18239a019196SSage Weil 		if (!ordered &&
18249a019196SSage Weil 		    !test_range_bit(&BTRFS_I(src)->io_tree, off, off+len,
18259a019196SSage Weil 				   EXTENT_DELALLOC, 0, NULL))
1826f46b5a66SChristoph Hellwig 			break;
1827c5c9cd4dSSage Weil 		unlock_extent(&BTRFS_I(src)->io_tree, off, off+len, GFP_NOFS);
1828ae01a0abSYan Zheng 		if (ordered)
1829ae01a0abSYan Zheng 			btrfs_put_ordered_extent(ordered);
18309a019196SSage Weil 		btrfs_wait_ordered_range(src, off, len);
1831f46b5a66SChristoph Hellwig 	}
1832f46b5a66SChristoph Hellwig 
1833c5c9cd4dSSage Weil 	/* clone data */
1834f46b5a66SChristoph Hellwig 	key.objectid = src->i_ino;
1835ae01a0abSYan Zheng 	key.type = BTRFS_EXTENT_DATA_KEY;
1836ae01a0abSYan Zheng 	key.offset = 0;
1837f46b5a66SChristoph Hellwig 
1838f46b5a66SChristoph Hellwig 	while (1) {
1839f46b5a66SChristoph Hellwig 		/*
1840f46b5a66SChristoph Hellwig 		 * note the key will change type as we walk through the
1841f46b5a66SChristoph Hellwig 		 * tree.
1842f46b5a66SChristoph Hellwig 		 */
1843a22285a6SYan, Zheng 		ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1844f46b5a66SChristoph Hellwig 		if (ret < 0)
1845f46b5a66SChristoph Hellwig 			goto out;
1846f46b5a66SChristoph Hellwig 
1847ae01a0abSYan Zheng 		nritems = btrfs_header_nritems(path->nodes[0]);
1848ae01a0abSYan Zheng 		if (path->slots[0] >= nritems) {
1849f46b5a66SChristoph Hellwig 			ret = btrfs_next_leaf(root, path);
1850f46b5a66SChristoph Hellwig 			if (ret < 0)
1851f46b5a66SChristoph Hellwig 				goto out;
1852f46b5a66SChristoph Hellwig 			if (ret > 0)
1853f46b5a66SChristoph Hellwig 				break;
1854ae01a0abSYan Zheng 			nritems = btrfs_header_nritems(path->nodes[0]);
1855f46b5a66SChristoph Hellwig 		}
1856f46b5a66SChristoph Hellwig 		leaf = path->nodes[0];
1857f46b5a66SChristoph Hellwig 		slot = path->slots[0];
1858f46b5a66SChristoph Hellwig 
1859ae01a0abSYan Zheng 		btrfs_item_key_to_cpu(leaf, &key, slot);
1860d20f7043SChris Mason 		if (btrfs_key_type(&key) > BTRFS_EXTENT_DATA_KEY ||
1861f46b5a66SChristoph Hellwig 		    key.objectid != src->i_ino)
1862f46b5a66SChristoph Hellwig 			break;
1863f46b5a66SChristoph Hellwig 
1864c5c9cd4dSSage Weil 		if (btrfs_key_type(&key) == BTRFS_EXTENT_DATA_KEY) {
1865c5c9cd4dSSage Weil 			struct btrfs_file_extent_item *extent;
1866c5c9cd4dSSage Weil 			int type;
186731840ae1SZheng Yan 			u32 size;
186831840ae1SZheng Yan 			struct btrfs_key new_key;
1869c5c9cd4dSSage Weil 			u64 disko = 0, diskl = 0;
1870c5c9cd4dSSage Weil 			u64 datao = 0, datal = 0;
1871c5c9cd4dSSage Weil 			u8 comp;
1872b5384d48SSage Weil 			u64 endoff;
187331840ae1SZheng Yan 
187431840ae1SZheng Yan 			size = btrfs_item_size_nr(leaf, slot);
187531840ae1SZheng Yan 			read_extent_buffer(leaf, buf,
187631840ae1SZheng Yan 					   btrfs_item_ptr_offset(leaf, slot),
187731840ae1SZheng Yan 					   size);
1878c5c9cd4dSSage Weil 
1879c5c9cd4dSSage Weil 			extent = btrfs_item_ptr(leaf, slot,
1880c5c9cd4dSSage Weil 						struct btrfs_file_extent_item);
1881c5c9cd4dSSage Weil 			comp = btrfs_file_extent_compression(leaf, extent);
1882c5c9cd4dSSage Weil 			type = btrfs_file_extent_type(leaf, extent);
1883c8a894d7SChris Mason 			if (type == BTRFS_FILE_EXTENT_REG ||
1884c8a894d7SChris Mason 			    type == BTRFS_FILE_EXTENT_PREALLOC) {
1885d397712bSChris Mason 				disko = btrfs_file_extent_disk_bytenr(leaf,
1886d397712bSChris Mason 								      extent);
1887d397712bSChris Mason 				diskl = btrfs_file_extent_disk_num_bytes(leaf,
1888d397712bSChris Mason 								 extent);
1889c5c9cd4dSSage Weil 				datao = btrfs_file_extent_offset(leaf, extent);
1890d397712bSChris Mason 				datal = btrfs_file_extent_num_bytes(leaf,
1891d397712bSChris Mason 								    extent);
1892c5c9cd4dSSage Weil 			} else if (type == BTRFS_FILE_EXTENT_INLINE) {
1893c5c9cd4dSSage Weil 				/* take upper bound, may be compressed */
1894c5c9cd4dSSage Weil 				datal = btrfs_file_extent_ram_bytes(leaf,
1895c5c9cd4dSSage Weil 								    extent);
1896c5c9cd4dSSage Weil 			}
189731840ae1SZheng Yan 			btrfs_release_path(root, path);
189831840ae1SZheng Yan 
1899050006a7SSage Weil 			if (key.offset + datal <= off ||
1900c5c9cd4dSSage Weil 			    key.offset >= off+len)
1901c5c9cd4dSSage Weil 				goto next;
1902c5c9cd4dSSage Weil 
190331840ae1SZheng Yan 			memcpy(&new_key, &key, sizeof(new_key));
190431840ae1SZheng Yan 			new_key.objectid = inode->i_ino;
19054d728ec7SLi Zefan 			if (off <= key.offset)
1906c5c9cd4dSSage Weil 				new_key.offset = key.offset + destoff - off;
19074d728ec7SLi Zefan 			else
19084d728ec7SLi Zefan 				new_key.offset = destoff;
1909c5c9cd4dSSage Weil 
1910a22285a6SYan, Zheng 			trans = btrfs_start_transaction(root, 1);
1911a22285a6SYan, Zheng 			if (IS_ERR(trans)) {
1912a22285a6SYan, Zheng 				ret = PTR_ERR(trans);
1913a22285a6SYan, Zheng 				goto out;
1914a22285a6SYan, Zheng 			}
1915a22285a6SYan, Zheng 
1916c8a894d7SChris Mason 			if (type == BTRFS_FILE_EXTENT_REG ||
1917c8a894d7SChris Mason 			    type == BTRFS_FILE_EXTENT_PREALLOC) {
1918a22285a6SYan, Zheng 				if (off > key.offset) {
1919a22285a6SYan, Zheng 					datao += off - key.offset;
1920a22285a6SYan, Zheng 					datal -= off - key.offset;
1921a22285a6SYan, Zheng 				}
1922a22285a6SYan, Zheng 
1923a22285a6SYan, Zheng 				if (key.offset + datal > off + len)
1924a22285a6SYan, Zheng 					datal = off + len - key.offset;
1925a22285a6SYan, Zheng 
1926a22285a6SYan, Zheng 				ret = btrfs_drop_extents(trans, inode,
1927a22285a6SYan, Zheng 							 new_key.offset,
1928a22285a6SYan, Zheng 							 new_key.offset + datal,
1929a22285a6SYan, Zheng 							 &hint_byte, 1);
1930a22285a6SYan, Zheng 				BUG_ON(ret);
1931a22285a6SYan, Zheng 
193231840ae1SZheng Yan 				ret = btrfs_insert_empty_item(trans, root, path,
193331840ae1SZheng Yan 							      &new_key, size);
1934a22285a6SYan, Zheng 				BUG_ON(ret);
193531840ae1SZheng Yan 
193631840ae1SZheng Yan 				leaf = path->nodes[0];
193731840ae1SZheng Yan 				slot = path->slots[0];
193831840ae1SZheng Yan 				write_extent_buffer(leaf, buf,
193931840ae1SZheng Yan 					    btrfs_item_ptr_offset(leaf, slot),
194031840ae1SZheng Yan 					    size);
1941ae01a0abSYan Zheng 
1942f46b5a66SChristoph Hellwig 				extent = btrfs_item_ptr(leaf, slot,
1943f46b5a66SChristoph Hellwig 						struct btrfs_file_extent_item);
1944c5c9cd4dSSage Weil 
1945c5c9cd4dSSage Weil 				/* disko == 0 means it's a hole */
1946c5c9cd4dSSage Weil 				if (!disko)
1947c5c9cd4dSSage Weil 					datao = 0;
1948c5c9cd4dSSage Weil 
1949c5c9cd4dSSage Weil 				btrfs_set_file_extent_offset(leaf, extent,
1950c5c9cd4dSSage Weil 							     datao);
1951c5c9cd4dSSage Weil 				btrfs_set_file_extent_num_bytes(leaf, extent,
1952c5c9cd4dSSage Weil 								datal);
1953c5c9cd4dSSage Weil 				if (disko) {
1954c5c9cd4dSSage Weil 					inode_add_bytes(inode, datal);
1955ae01a0abSYan Zheng 					ret = btrfs_inc_extent_ref(trans, root,
19565d4f98a2SYan Zheng 							disko, diskl, 0,
1957f46b5a66SChristoph Hellwig 							root->root_key.objectid,
19585d4f98a2SYan Zheng 							inode->i_ino,
19595d4f98a2SYan Zheng 							new_key.offset - datao);
1960ae01a0abSYan Zheng 					BUG_ON(ret);
1961f46b5a66SChristoph Hellwig 				}
1962c5c9cd4dSSage Weil 			} else if (type == BTRFS_FILE_EXTENT_INLINE) {
1963c5c9cd4dSSage Weil 				u64 skip = 0;
1964c5c9cd4dSSage Weil 				u64 trim = 0;
1965c5c9cd4dSSage Weil 				if (off > key.offset) {
1966c5c9cd4dSSage Weil 					skip = off - key.offset;
1967c5c9cd4dSSage Weil 					new_key.offset += skip;
196831840ae1SZheng Yan 				}
1969d397712bSChris Mason 
1970c5c9cd4dSSage Weil 				if (key.offset + datal > off+len)
1971c5c9cd4dSSage Weil 					trim = key.offset + datal - (off+len);
1972d397712bSChris Mason 
1973c5c9cd4dSSage Weil 				if (comp && (skip || trim)) {
1974c5c9cd4dSSage Weil 					ret = -EINVAL;
1975a22285a6SYan, Zheng 					btrfs_end_transaction(trans, root);
1976c5c9cd4dSSage Weil 					goto out;
197731840ae1SZheng Yan 				}
1978c5c9cd4dSSage Weil 				size -= skip + trim;
1979c5c9cd4dSSage Weil 				datal -= skip + trim;
1980a22285a6SYan, Zheng 
1981a22285a6SYan, Zheng 				ret = btrfs_drop_extents(trans, inode,
1982a22285a6SYan, Zheng 							 new_key.offset,
1983a22285a6SYan, Zheng 							 new_key.offset + datal,
1984a22285a6SYan, Zheng 							 &hint_byte, 1);
1985a22285a6SYan, Zheng 				BUG_ON(ret);
1986a22285a6SYan, Zheng 
1987c5c9cd4dSSage Weil 				ret = btrfs_insert_empty_item(trans, root, path,
1988c5c9cd4dSSage Weil 							      &new_key, size);
1989a22285a6SYan, Zheng 				BUG_ON(ret);
1990c5c9cd4dSSage Weil 
1991c5c9cd4dSSage Weil 				if (skip) {
1992d397712bSChris Mason 					u32 start =
1993d397712bSChris Mason 					  btrfs_file_extent_calc_inline_size(0);
1994c5c9cd4dSSage Weil 					memmove(buf+start, buf+start+skip,
1995c5c9cd4dSSage Weil 						datal);
1996c5c9cd4dSSage Weil 				}
1997c5c9cd4dSSage Weil 
1998c5c9cd4dSSage Weil 				leaf = path->nodes[0];
1999c5c9cd4dSSage Weil 				slot = path->slots[0];
2000c5c9cd4dSSage Weil 				write_extent_buffer(leaf, buf,
2001c5c9cd4dSSage Weil 					    btrfs_item_ptr_offset(leaf, slot),
2002c5c9cd4dSSage Weil 					    size);
2003c5c9cd4dSSage Weil 				inode_add_bytes(inode, datal);
2004c5c9cd4dSSage Weil 			}
2005c5c9cd4dSSage Weil 
2006c5c9cd4dSSage Weil 			btrfs_mark_buffer_dirty(leaf);
2007a22285a6SYan, Zheng 			btrfs_release_path(root, path);
2008c5c9cd4dSSage Weil 
2009a22285a6SYan, Zheng 			inode->i_mtime = inode->i_ctime = CURRENT_TIME;
2010b5384d48SSage Weil 
2011b5384d48SSage Weil 			/*
2012b5384d48SSage Weil 			 * we round up to the block size at eof when
2013b5384d48SSage Weil 			 * determining which extents to clone above,
2014b5384d48SSage Weil 			 * but shouldn't round up the file size
2015b5384d48SSage Weil 			 */
2016b5384d48SSage Weil 			endoff = new_key.offset + datal;
20175f3888ffSLi Zefan 			if (endoff > destoff+olen)
20185f3888ffSLi Zefan 				endoff = destoff+olen;
2019b5384d48SSage Weil 			if (endoff > inode->i_size)
2020b5384d48SSage Weil 				btrfs_i_size_write(inode, endoff);
2021b5384d48SSage Weil 
2022a22285a6SYan, Zheng 			BTRFS_I(inode)->flags = BTRFS_I(src)->flags;
2023a22285a6SYan, Zheng 			ret = btrfs_update_inode(trans, root, inode);
2024a22285a6SYan, Zheng 			BUG_ON(ret);
2025a22285a6SYan, Zheng 			btrfs_end_transaction(trans, root);
2026a22285a6SYan, Zheng 		}
2027c5c9cd4dSSage Weil next:
202831840ae1SZheng Yan 		btrfs_release_path(root, path);
2029ae01a0abSYan Zheng 		key.offset++;
2030ae01a0abSYan Zheng 	}
2031f46b5a66SChristoph Hellwig 	ret = 0;
2032f46b5a66SChristoph Hellwig out:
2033ae01a0abSYan Zheng 	btrfs_release_path(root, path);
2034c5c9cd4dSSage Weil 	unlock_extent(&BTRFS_I(src)->io_tree, off, off+len, GFP_NOFS);
2035f46b5a66SChristoph Hellwig out_unlock:
2036f46b5a66SChristoph Hellwig 	mutex_unlock(&src->i_mutex);
2037f46b5a66SChristoph Hellwig 	mutex_unlock(&inode->i_mutex);
2038ae01a0abSYan Zheng 	vfree(buf);
2039ae01a0abSYan Zheng 	btrfs_free_path(path);
2040f46b5a66SChristoph Hellwig out_fput:
2041f46b5a66SChristoph Hellwig 	fput(src_file);
2042ab67b7c1SYan Zheng out_drop_write:
2043ab67b7c1SYan Zheng 	mnt_drop_write(file->f_path.mnt);
2044f46b5a66SChristoph Hellwig 	return ret;
2045f46b5a66SChristoph Hellwig }
2046f46b5a66SChristoph Hellwig 
20477a865e8aSChristoph Hellwig static long btrfs_ioctl_clone_range(struct file *file, void __user *argp)
2048c5c9cd4dSSage Weil {
2049c5c9cd4dSSage Weil 	struct btrfs_ioctl_clone_range_args args;
2050c5c9cd4dSSage Weil 
20517a865e8aSChristoph Hellwig 	if (copy_from_user(&args, argp, sizeof(args)))
2052c5c9cd4dSSage Weil 		return -EFAULT;
2053c5c9cd4dSSage Weil 	return btrfs_ioctl_clone(file, args.src_fd, args.src_offset,
2054c5c9cd4dSSage Weil 				 args.src_length, args.dest_offset);
2055c5c9cd4dSSage Weil }
2056c5c9cd4dSSage Weil 
2057f46b5a66SChristoph Hellwig /*
2058f46b5a66SChristoph Hellwig  * there are many ways the trans_start and trans_end ioctls can lead
2059f46b5a66SChristoph Hellwig  * to deadlocks.  They should only be used by applications that
2060f46b5a66SChristoph Hellwig  * basically own the machine, and have a very in depth understanding
2061f46b5a66SChristoph Hellwig  * of all the possible deadlocks and enospc problems.
2062f46b5a66SChristoph Hellwig  */
2063b2950863SChristoph Hellwig static long btrfs_ioctl_trans_start(struct file *file)
2064f46b5a66SChristoph Hellwig {
2065f46b5a66SChristoph Hellwig 	struct inode *inode = fdentry(file)->d_inode;
2066f46b5a66SChristoph Hellwig 	struct btrfs_root *root = BTRFS_I(inode)->root;
2067f46b5a66SChristoph Hellwig 	struct btrfs_trans_handle *trans;
20681ab86aedSSage Weil 	int ret;
2069f46b5a66SChristoph Hellwig 
20701ab86aedSSage Weil 	ret = -EPERM;
2071df5b5520SChristoph Hellwig 	if (!capable(CAP_SYS_ADMIN))
2072f46b5a66SChristoph Hellwig 		goto out;
20731ab86aedSSage Weil 
20741ab86aedSSage Weil 	ret = -EINPROGRESS;
20751ab86aedSSage Weil 	if (file->private_data)
20761ab86aedSSage Weil 		goto out;
20779ca9ee09SSage Weil 
2078b83cc969SLi Zefan 	ret = -EROFS;
2079b83cc969SLi Zefan 	if (btrfs_root_readonly(root))
2080b83cc969SLi Zefan 		goto out;
2081b83cc969SLi Zefan 
2082c146afadSYan Zheng 	ret = mnt_want_write(file->f_path.mnt);
2083c146afadSYan Zheng 	if (ret)
2084c146afadSYan Zheng 		goto out;
2085c146afadSYan Zheng 
20869ca9ee09SSage Weil 	mutex_lock(&root->fs_info->trans_mutex);
20879ca9ee09SSage Weil 	root->fs_info->open_ioctl_trans++;
20889ca9ee09SSage Weil 	mutex_unlock(&root->fs_info->trans_mutex);
20899ca9ee09SSage Weil 
2090f46b5a66SChristoph Hellwig 	ret = -ENOMEM;
20911ab86aedSSage Weil 	trans = btrfs_start_ioctl_transaction(root, 0);
2092abd30bb0STsutomu Itoh 	if (IS_ERR(trans))
20931ab86aedSSage Weil 		goto out_drop;
20941ab86aedSSage Weil 
20951ab86aedSSage Weil 	file->private_data = trans;
20961ab86aedSSage Weil 	return 0;
20971ab86aedSSage Weil 
20981ab86aedSSage Weil out_drop:
20991ab86aedSSage Weil 	mutex_lock(&root->fs_info->trans_mutex);
21001ab86aedSSage Weil 	root->fs_info->open_ioctl_trans--;
21011ab86aedSSage Weil 	mutex_unlock(&root->fs_info->trans_mutex);
21021ab86aedSSage Weil 	mnt_drop_write(file->f_path.mnt);
2103f46b5a66SChristoph Hellwig out:
2104f46b5a66SChristoph Hellwig 	return ret;
2105f46b5a66SChristoph Hellwig }
2106f46b5a66SChristoph Hellwig 
21076ef5ed0dSJosef Bacik static long btrfs_ioctl_default_subvol(struct file *file, void __user *argp)
21086ef5ed0dSJosef Bacik {
21096ef5ed0dSJosef Bacik 	struct inode *inode = fdentry(file)->d_inode;
21106ef5ed0dSJosef Bacik 	struct btrfs_root *root = BTRFS_I(inode)->root;
21116ef5ed0dSJosef Bacik 	struct btrfs_root *new_root;
21126ef5ed0dSJosef Bacik 	struct btrfs_dir_item *di;
21136ef5ed0dSJosef Bacik 	struct btrfs_trans_handle *trans;
21146ef5ed0dSJosef Bacik 	struct btrfs_path *path;
21156ef5ed0dSJosef Bacik 	struct btrfs_key location;
21166ef5ed0dSJosef Bacik 	struct btrfs_disk_key disk_key;
21176ef5ed0dSJosef Bacik 	struct btrfs_super_block *disk_super;
21186ef5ed0dSJosef Bacik 	u64 features;
21196ef5ed0dSJosef Bacik 	u64 objectid = 0;
21206ef5ed0dSJosef Bacik 	u64 dir_id;
21216ef5ed0dSJosef Bacik 
21226ef5ed0dSJosef Bacik 	if (!capable(CAP_SYS_ADMIN))
21236ef5ed0dSJosef Bacik 		return -EPERM;
21246ef5ed0dSJosef Bacik 
21256ef5ed0dSJosef Bacik 	if (copy_from_user(&objectid, argp, sizeof(objectid)))
21266ef5ed0dSJosef Bacik 		return -EFAULT;
21276ef5ed0dSJosef Bacik 
21286ef5ed0dSJosef Bacik 	if (!objectid)
21296ef5ed0dSJosef Bacik 		objectid = root->root_key.objectid;
21306ef5ed0dSJosef Bacik 
21316ef5ed0dSJosef Bacik 	location.objectid = objectid;
21326ef5ed0dSJosef Bacik 	location.type = BTRFS_ROOT_ITEM_KEY;
21336ef5ed0dSJosef Bacik 	location.offset = (u64)-1;
21346ef5ed0dSJosef Bacik 
21356ef5ed0dSJosef Bacik 	new_root = btrfs_read_fs_root_no_name(root->fs_info, &location);
21366ef5ed0dSJosef Bacik 	if (IS_ERR(new_root))
21376ef5ed0dSJosef Bacik 		return PTR_ERR(new_root);
21386ef5ed0dSJosef Bacik 
21396ef5ed0dSJosef Bacik 	if (btrfs_root_refs(&new_root->root_item) == 0)
21406ef5ed0dSJosef Bacik 		return -ENOENT;
21416ef5ed0dSJosef Bacik 
21426ef5ed0dSJosef Bacik 	path = btrfs_alloc_path();
21436ef5ed0dSJosef Bacik 	if (!path)
21446ef5ed0dSJosef Bacik 		return -ENOMEM;
21456ef5ed0dSJosef Bacik 	path->leave_spinning = 1;
21466ef5ed0dSJosef Bacik 
21476ef5ed0dSJosef Bacik 	trans = btrfs_start_transaction(root, 1);
214898d5dc13STsutomu Itoh 	if (IS_ERR(trans)) {
21496ef5ed0dSJosef Bacik 		btrfs_free_path(path);
215098d5dc13STsutomu Itoh 		return PTR_ERR(trans);
21516ef5ed0dSJosef Bacik 	}
21526ef5ed0dSJosef Bacik 
21536ef5ed0dSJosef Bacik 	dir_id = btrfs_super_root_dir(&root->fs_info->super_copy);
21546ef5ed0dSJosef Bacik 	di = btrfs_lookup_dir_item(trans, root->fs_info->tree_root, path,
21556ef5ed0dSJosef Bacik 				   dir_id, "default", 7, 1);
2156cf1e99a4SDan Carpenter 	if (IS_ERR_OR_NULL(di)) {
21576ef5ed0dSJosef Bacik 		btrfs_free_path(path);
21586ef5ed0dSJosef Bacik 		btrfs_end_transaction(trans, root);
21596ef5ed0dSJosef Bacik 		printk(KERN_ERR "Umm, you don't have the default dir item, "
21606ef5ed0dSJosef Bacik 		       "this isn't going to work\n");
21616ef5ed0dSJosef Bacik 		return -ENOENT;
21626ef5ed0dSJosef Bacik 	}
21636ef5ed0dSJosef Bacik 
21646ef5ed0dSJosef Bacik 	btrfs_cpu_key_to_disk(&disk_key, &new_root->root_key);
21656ef5ed0dSJosef Bacik 	btrfs_set_dir_item_key(path->nodes[0], di, &disk_key);
21666ef5ed0dSJosef Bacik 	btrfs_mark_buffer_dirty(path->nodes[0]);
21676ef5ed0dSJosef Bacik 	btrfs_free_path(path);
21686ef5ed0dSJosef Bacik 
21696ef5ed0dSJosef Bacik 	disk_super = &root->fs_info->super_copy;
21706ef5ed0dSJosef Bacik 	features = btrfs_super_incompat_flags(disk_super);
21716ef5ed0dSJosef Bacik 	if (!(features & BTRFS_FEATURE_INCOMPAT_DEFAULT_SUBVOL)) {
21726ef5ed0dSJosef Bacik 		features |= BTRFS_FEATURE_INCOMPAT_DEFAULT_SUBVOL;
21736ef5ed0dSJosef Bacik 		btrfs_set_super_incompat_flags(disk_super, features);
21746ef5ed0dSJosef Bacik 	}
21756ef5ed0dSJosef Bacik 	btrfs_end_transaction(trans, root);
21766ef5ed0dSJosef Bacik 
21776ef5ed0dSJosef Bacik 	return 0;
21786ef5ed0dSJosef Bacik }
21796ef5ed0dSJosef Bacik 
2180bf5fc093SJosef Bacik static void get_block_group_info(struct list_head *groups_list,
2181bf5fc093SJosef Bacik 				 struct btrfs_ioctl_space_info *space)
2182bf5fc093SJosef Bacik {
2183bf5fc093SJosef Bacik 	struct btrfs_block_group_cache *block_group;
2184bf5fc093SJosef Bacik 
2185bf5fc093SJosef Bacik 	space->total_bytes = 0;
2186bf5fc093SJosef Bacik 	space->used_bytes = 0;
2187bf5fc093SJosef Bacik 	space->flags = 0;
2188bf5fc093SJosef Bacik 	list_for_each_entry(block_group, groups_list, list) {
2189bf5fc093SJosef Bacik 		space->flags = block_group->flags;
2190bf5fc093SJosef Bacik 		space->total_bytes += block_group->key.offset;
2191bf5fc093SJosef Bacik 		space->used_bytes +=
2192bf5fc093SJosef Bacik 			btrfs_block_group_used(&block_group->item);
2193bf5fc093SJosef Bacik 	}
2194bf5fc093SJosef Bacik }
2195bf5fc093SJosef Bacik 
21961406e432SJosef Bacik long btrfs_ioctl_space_info(struct btrfs_root *root, void __user *arg)
21971406e432SJosef Bacik {
21981406e432SJosef Bacik 	struct btrfs_ioctl_space_args space_args;
21991406e432SJosef Bacik 	struct btrfs_ioctl_space_info space;
22001406e432SJosef Bacik 	struct btrfs_ioctl_space_info *dest;
22017fde62bfSChris Mason 	struct btrfs_ioctl_space_info *dest_orig;
22027fde62bfSChris Mason 	struct btrfs_ioctl_space_info *user_dest;
22031406e432SJosef Bacik 	struct btrfs_space_info *info;
2204bf5fc093SJosef Bacik 	u64 types[] = {BTRFS_BLOCK_GROUP_DATA,
2205bf5fc093SJosef Bacik 		       BTRFS_BLOCK_GROUP_SYSTEM,
2206bf5fc093SJosef Bacik 		       BTRFS_BLOCK_GROUP_METADATA,
2207bf5fc093SJosef Bacik 		       BTRFS_BLOCK_GROUP_DATA | BTRFS_BLOCK_GROUP_METADATA};
2208bf5fc093SJosef Bacik 	int num_types = 4;
22097fde62bfSChris Mason 	int alloc_size;
22101406e432SJosef Bacik 	int ret = 0;
221151788b1bSDan Rosenberg 	u64 slot_count = 0;
2212bf5fc093SJosef Bacik 	int i, c;
22131406e432SJosef Bacik 
22141406e432SJosef Bacik 	if (copy_from_user(&space_args,
22151406e432SJosef Bacik 			   (struct btrfs_ioctl_space_args __user *)arg,
22161406e432SJosef Bacik 			   sizeof(space_args)))
22171406e432SJosef Bacik 		return -EFAULT;
22181406e432SJosef Bacik 
2219bf5fc093SJosef Bacik 	for (i = 0; i < num_types; i++) {
2220bf5fc093SJosef Bacik 		struct btrfs_space_info *tmp;
2221bf5fc093SJosef Bacik 
2222bf5fc093SJosef Bacik 		info = NULL;
22237fde62bfSChris Mason 		rcu_read_lock();
2224bf5fc093SJosef Bacik 		list_for_each_entry_rcu(tmp, &root->fs_info->space_info,
2225bf5fc093SJosef Bacik 					list) {
2226bf5fc093SJosef Bacik 			if (tmp->flags == types[i]) {
2227bf5fc093SJosef Bacik 				info = tmp;
2228bf5fc093SJosef Bacik 				break;
2229bf5fc093SJosef Bacik 			}
2230bf5fc093SJosef Bacik 		}
22317fde62bfSChris Mason 		rcu_read_unlock();
22321406e432SJosef Bacik 
2233bf5fc093SJosef Bacik 		if (!info)
2234bf5fc093SJosef Bacik 			continue;
2235bf5fc093SJosef Bacik 
2236bf5fc093SJosef Bacik 		down_read(&info->groups_sem);
2237bf5fc093SJosef Bacik 		for (c = 0; c < BTRFS_NR_RAID_TYPES; c++) {
2238bf5fc093SJosef Bacik 			if (!list_empty(&info->block_groups[c]))
2239bf5fc093SJosef Bacik 				slot_count++;
2240bf5fc093SJosef Bacik 		}
2241bf5fc093SJosef Bacik 		up_read(&info->groups_sem);
2242bf5fc093SJosef Bacik 	}
2243bf5fc093SJosef Bacik 
22447fde62bfSChris Mason 	/* space_slots == 0 means they are asking for a count */
22457fde62bfSChris Mason 	if (space_args.space_slots == 0) {
22467fde62bfSChris Mason 		space_args.total_spaces = slot_count;
22477fde62bfSChris Mason 		goto out;
22487fde62bfSChris Mason 	}
2249bf5fc093SJosef Bacik 
225051788b1bSDan Rosenberg 	slot_count = min_t(u64, space_args.space_slots, slot_count);
2251bf5fc093SJosef Bacik 
22527fde62bfSChris Mason 	alloc_size = sizeof(*dest) * slot_count;
2253bf5fc093SJosef Bacik 
22547fde62bfSChris Mason 	/* we generally have at most 6 or so space infos, one for each raid
22557fde62bfSChris Mason 	 * level.  So, a whole page should be more than enough for everyone
22567fde62bfSChris Mason 	 */
22577fde62bfSChris Mason 	if (alloc_size > PAGE_CACHE_SIZE)
22587fde62bfSChris Mason 		return -ENOMEM;
22597fde62bfSChris Mason 
22607fde62bfSChris Mason 	space_args.total_spaces = 0;
22617fde62bfSChris Mason 	dest = kmalloc(alloc_size, GFP_NOFS);
22627fde62bfSChris Mason 	if (!dest)
22637fde62bfSChris Mason 		return -ENOMEM;
22647fde62bfSChris Mason 	dest_orig = dest;
22657fde62bfSChris Mason 
22667fde62bfSChris Mason 	/* now we have a buffer to copy into */
2267bf5fc093SJosef Bacik 	for (i = 0; i < num_types; i++) {
2268bf5fc093SJosef Bacik 		struct btrfs_space_info *tmp;
2269bf5fc093SJosef Bacik 
227051788b1bSDan Rosenberg 		if (!slot_count)
227151788b1bSDan Rosenberg 			break;
227251788b1bSDan Rosenberg 
2273bf5fc093SJosef Bacik 		info = NULL;
22741406e432SJosef Bacik 		rcu_read_lock();
2275bf5fc093SJosef Bacik 		list_for_each_entry_rcu(tmp, &root->fs_info->space_info,
2276bf5fc093SJosef Bacik 					list) {
2277bf5fc093SJosef Bacik 			if (tmp->flags == types[i]) {
2278bf5fc093SJosef Bacik 				info = tmp;
22797fde62bfSChris Mason 				break;
2280bf5fc093SJosef Bacik 			}
2281bf5fc093SJosef Bacik 		}
2282bf5fc093SJosef Bacik 		rcu_read_unlock();
22837fde62bfSChris Mason 
2284bf5fc093SJosef Bacik 		if (!info)
2285bf5fc093SJosef Bacik 			continue;
2286bf5fc093SJosef Bacik 		down_read(&info->groups_sem);
2287bf5fc093SJosef Bacik 		for (c = 0; c < BTRFS_NR_RAID_TYPES; c++) {
2288bf5fc093SJosef Bacik 			if (!list_empty(&info->block_groups[c])) {
2289bf5fc093SJosef Bacik 				get_block_group_info(&info->block_groups[c],
2290bf5fc093SJosef Bacik 						     &space);
22917fde62bfSChris Mason 				memcpy(dest, &space, sizeof(space));
22921406e432SJosef Bacik 				dest++;
22931406e432SJosef Bacik 				space_args.total_spaces++;
229451788b1bSDan Rosenberg 				slot_count--;
22951406e432SJosef Bacik 			}
229651788b1bSDan Rosenberg 			if (!slot_count)
229751788b1bSDan Rosenberg 				break;
2298bf5fc093SJosef Bacik 		}
2299bf5fc093SJosef Bacik 		up_read(&info->groups_sem);
2300bf5fc093SJosef Bacik 	}
23011406e432SJosef Bacik 
23027fde62bfSChris Mason 	user_dest = (struct btrfs_ioctl_space_info *)
23037fde62bfSChris Mason 		(arg + sizeof(struct btrfs_ioctl_space_args));
23047fde62bfSChris Mason 
23057fde62bfSChris Mason 	if (copy_to_user(user_dest, dest_orig, alloc_size))
23067fde62bfSChris Mason 		ret = -EFAULT;
23077fde62bfSChris Mason 
23087fde62bfSChris Mason 	kfree(dest_orig);
23097fde62bfSChris Mason out:
23107fde62bfSChris Mason 	if (ret == 0 && copy_to_user(arg, &space_args, sizeof(space_args)))
23111406e432SJosef Bacik 		ret = -EFAULT;
23121406e432SJosef Bacik 
23131406e432SJosef Bacik 	return ret;
23141406e432SJosef Bacik }
23151406e432SJosef Bacik 
2316f46b5a66SChristoph Hellwig /*
2317f46b5a66SChristoph Hellwig  * there are many ways the trans_start and trans_end ioctls can lead
2318f46b5a66SChristoph Hellwig  * to deadlocks.  They should only be used by applications that
2319f46b5a66SChristoph Hellwig  * basically own the machine, and have a very in depth understanding
2320f46b5a66SChristoph Hellwig  * of all the possible deadlocks and enospc problems.
2321f46b5a66SChristoph Hellwig  */
2322f46b5a66SChristoph Hellwig long btrfs_ioctl_trans_end(struct file *file)
2323f46b5a66SChristoph Hellwig {
2324f46b5a66SChristoph Hellwig 	struct inode *inode = fdentry(file)->d_inode;
2325f46b5a66SChristoph Hellwig 	struct btrfs_root *root = BTRFS_I(inode)->root;
2326f46b5a66SChristoph Hellwig 	struct btrfs_trans_handle *trans;
2327f46b5a66SChristoph Hellwig 
2328f46b5a66SChristoph Hellwig 	trans = file->private_data;
23291ab86aedSSage Weil 	if (!trans)
23301ab86aedSSage Weil 		return -EINVAL;
2331b214107eSChristoph Hellwig 	file->private_data = NULL;
23329ca9ee09SSage Weil 
23331ab86aedSSage Weil 	btrfs_end_transaction(trans, root);
23341ab86aedSSage Weil 
23359ca9ee09SSage Weil 	mutex_lock(&root->fs_info->trans_mutex);
23369ca9ee09SSage Weil 	root->fs_info->open_ioctl_trans--;
23379ca9ee09SSage Weil 	mutex_unlock(&root->fs_info->trans_mutex);
23389ca9ee09SSage Weil 
2339cfc8ea87SSage Weil 	mnt_drop_write(file->f_path.mnt);
23401ab86aedSSage Weil 	return 0;
2341f46b5a66SChristoph Hellwig }
2342f46b5a66SChristoph Hellwig 
234346204592SSage Weil static noinline long btrfs_ioctl_start_sync(struct file *file, void __user *argp)
234446204592SSage Weil {
234546204592SSage Weil 	struct btrfs_root *root = BTRFS_I(file->f_dentry->d_inode)->root;
234646204592SSage Weil 	struct btrfs_trans_handle *trans;
234746204592SSage Weil 	u64 transid;
234846204592SSage Weil 
234946204592SSage Weil 	trans = btrfs_start_transaction(root, 0);
235098d5dc13STsutomu Itoh 	if (IS_ERR(trans))
235198d5dc13STsutomu Itoh 		return PTR_ERR(trans);
235246204592SSage Weil 	transid = trans->transid;
235346204592SSage Weil 	btrfs_commit_transaction_async(trans, root, 0);
235446204592SSage Weil 
235546204592SSage Weil 	if (argp)
235646204592SSage Weil 		if (copy_to_user(argp, &transid, sizeof(transid)))
235746204592SSage Weil 			return -EFAULT;
235846204592SSage Weil 	return 0;
235946204592SSage Weil }
236046204592SSage Weil 
236146204592SSage Weil static noinline long btrfs_ioctl_wait_sync(struct file *file, void __user *argp)
236246204592SSage Weil {
236346204592SSage Weil 	struct btrfs_root *root = BTRFS_I(file->f_dentry->d_inode)->root;
236446204592SSage Weil 	u64 transid;
236546204592SSage Weil 
236646204592SSage Weil 	if (argp) {
236746204592SSage Weil 		if (copy_from_user(&transid, argp, sizeof(transid)))
236846204592SSage Weil 			return -EFAULT;
236946204592SSage Weil 	} else {
237046204592SSage Weil 		transid = 0;  /* current trans */
237146204592SSage Weil 	}
237246204592SSage Weil 	return btrfs_wait_for_commit(root, transid);
237346204592SSage Weil }
237446204592SSage Weil 
2375f46b5a66SChristoph Hellwig long btrfs_ioctl(struct file *file, unsigned int
2376f46b5a66SChristoph Hellwig 		cmd, unsigned long arg)
2377f46b5a66SChristoph Hellwig {
2378f46b5a66SChristoph Hellwig 	struct btrfs_root *root = BTRFS_I(fdentry(file)->d_inode)->root;
23794bcabaa3SChristoph Hellwig 	void __user *argp = (void __user *)arg;
2380f46b5a66SChristoph Hellwig 
2381f46b5a66SChristoph Hellwig 	switch (cmd) {
23826cbff00fSChristoph Hellwig 	case FS_IOC_GETFLAGS:
23836cbff00fSChristoph Hellwig 		return btrfs_ioctl_getflags(file, argp);
23846cbff00fSChristoph Hellwig 	case FS_IOC_SETFLAGS:
23856cbff00fSChristoph Hellwig 		return btrfs_ioctl_setflags(file, argp);
23866cbff00fSChristoph Hellwig 	case FS_IOC_GETVERSION:
23876cbff00fSChristoph Hellwig 		return btrfs_ioctl_getversion(file, argp);
2388f46b5a66SChristoph Hellwig 	case BTRFS_IOC_SNAP_CREATE:
2389fa0d2b9bSLi Zefan 		return btrfs_ioctl_snap_create(file, argp, 0);
2390fdfb1e4fSLi Zefan 	case BTRFS_IOC_SNAP_CREATE_V2:
2391fa0d2b9bSLi Zefan 		return btrfs_ioctl_snap_create_v2(file, argp, 0);
23923de4586cSChris Mason 	case BTRFS_IOC_SUBVOL_CREATE:
2393fa0d2b9bSLi Zefan 		return btrfs_ioctl_snap_create(file, argp, 1);
239476dda93cSYan, Zheng 	case BTRFS_IOC_SNAP_DESTROY:
239576dda93cSYan, Zheng 		return btrfs_ioctl_snap_destroy(file, argp);
23960caa102dSLi Zefan 	case BTRFS_IOC_SUBVOL_GETFLAGS:
23970caa102dSLi Zefan 		return btrfs_ioctl_subvol_getflags(file, argp);
23980caa102dSLi Zefan 	case BTRFS_IOC_SUBVOL_SETFLAGS:
23990caa102dSLi Zefan 		return btrfs_ioctl_subvol_setflags(file, argp);
24006ef5ed0dSJosef Bacik 	case BTRFS_IOC_DEFAULT_SUBVOL:
24016ef5ed0dSJosef Bacik 		return btrfs_ioctl_default_subvol(file, argp);
2402f46b5a66SChristoph Hellwig 	case BTRFS_IOC_DEFRAG:
24031e701a32SChris Mason 		return btrfs_ioctl_defrag(file, NULL);
24041e701a32SChris Mason 	case BTRFS_IOC_DEFRAG_RANGE:
24051e701a32SChris Mason 		return btrfs_ioctl_defrag(file, argp);
2406f46b5a66SChristoph Hellwig 	case BTRFS_IOC_RESIZE:
24074bcabaa3SChristoph Hellwig 		return btrfs_ioctl_resize(root, argp);
2408f46b5a66SChristoph Hellwig 	case BTRFS_IOC_ADD_DEV:
24094bcabaa3SChristoph Hellwig 		return btrfs_ioctl_add_dev(root, argp);
2410f46b5a66SChristoph Hellwig 	case BTRFS_IOC_RM_DEV:
24114bcabaa3SChristoph Hellwig 		return btrfs_ioctl_rm_dev(root, argp);
2412f46b5a66SChristoph Hellwig 	case BTRFS_IOC_BALANCE:
2413f46b5a66SChristoph Hellwig 		return btrfs_balance(root->fs_info->dev_root);
2414f46b5a66SChristoph Hellwig 	case BTRFS_IOC_CLONE:
2415c5c9cd4dSSage Weil 		return btrfs_ioctl_clone(file, arg, 0, 0, 0);
2416c5c9cd4dSSage Weil 	case BTRFS_IOC_CLONE_RANGE:
24177a865e8aSChristoph Hellwig 		return btrfs_ioctl_clone_range(file, argp);
2418f46b5a66SChristoph Hellwig 	case BTRFS_IOC_TRANS_START:
2419f46b5a66SChristoph Hellwig 		return btrfs_ioctl_trans_start(file);
2420f46b5a66SChristoph Hellwig 	case BTRFS_IOC_TRANS_END:
2421f46b5a66SChristoph Hellwig 		return btrfs_ioctl_trans_end(file);
2422ac8e9819SChris Mason 	case BTRFS_IOC_TREE_SEARCH:
2423ac8e9819SChris Mason 		return btrfs_ioctl_tree_search(file, argp);
2424ac8e9819SChris Mason 	case BTRFS_IOC_INO_LOOKUP:
2425ac8e9819SChris Mason 		return btrfs_ioctl_ino_lookup(file, argp);
24261406e432SJosef Bacik 	case BTRFS_IOC_SPACE_INFO:
24271406e432SJosef Bacik 		return btrfs_ioctl_space_info(root, argp);
2428f46b5a66SChristoph Hellwig 	case BTRFS_IOC_SYNC:
2429f46b5a66SChristoph Hellwig 		btrfs_sync_fs(file->f_dentry->d_sb, 1);
2430f46b5a66SChristoph Hellwig 		return 0;
243146204592SSage Weil 	case BTRFS_IOC_START_SYNC:
243246204592SSage Weil 		return btrfs_ioctl_start_sync(file, argp);
243346204592SSage Weil 	case BTRFS_IOC_WAIT_SYNC:
243446204592SSage Weil 		return btrfs_ioctl_wait_sync(file, argp);
2435f46b5a66SChristoph Hellwig 	}
2436f46b5a66SChristoph Hellwig 
2437f46b5a66SChristoph Hellwig 	return -ENOTTY;
2438f46b5a66SChristoph Hellwig }
2439