xref: /openbmc/linux/fs/btrfs/ioctl.c (revision 4bcabaa3)
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/smp_lock.h>
31f46b5a66SChristoph Hellwig #include <linux/backing-dev.h>
32cb8e7090SChristoph Hellwig #include <linux/mount.h>
33f46b5a66SChristoph Hellwig #include <linux/mpage.h>
34cb8e7090SChristoph Hellwig #include <linux/namei.h>
35f46b5a66SChristoph Hellwig #include <linux/swap.h>
36f46b5a66SChristoph Hellwig #include <linux/writeback.h>
37f46b5a66SChristoph Hellwig #include <linux/statfs.h>
38f46b5a66SChristoph Hellwig #include <linux/compat.h>
39f46b5a66SChristoph Hellwig #include <linux/bit_spinlock.h>
40cb8e7090SChristoph Hellwig #include <linux/security.h>
41f46b5a66SChristoph Hellwig #include <linux/version.h>
42f46b5a66SChristoph Hellwig #include <linux/xattr.h>
437ea394f1SYan Zheng #include <linux/vmalloc.h>
444b4e25f2SChris Mason #include "compat.h"
45f46b5a66SChristoph Hellwig #include "ctree.h"
46f46b5a66SChristoph Hellwig #include "disk-io.h"
47f46b5a66SChristoph Hellwig #include "transaction.h"
48f46b5a66SChristoph Hellwig #include "btrfs_inode.h"
49f46b5a66SChristoph Hellwig #include "ioctl.h"
50f46b5a66SChristoph Hellwig #include "print-tree.h"
51f46b5a66SChristoph Hellwig #include "volumes.h"
52925baeddSChris Mason #include "locking.h"
53f46b5a66SChristoph Hellwig 
54f46b5a66SChristoph Hellwig 
55f46b5a66SChristoph Hellwig 
56cb8e7090SChristoph Hellwig static noinline int create_subvol(struct btrfs_root *root,
57cb8e7090SChristoph Hellwig 				  struct dentry *dentry,
58cb8e7090SChristoph Hellwig 				  char *name, int namelen)
59f46b5a66SChristoph Hellwig {
60f46b5a66SChristoph Hellwig 	struct btrfs_trans_handle *trans;
61f46b5a66SChristoph Hellwig 	struct btrfs_key key;
62f46b5a66SChristoph Hellwig 	struct btrfs_root_item root_item;
63f46b5a66SChristoph Hellwig 	struct btrfs_inode_item *inode_item;
64f46b5a66SChristoph Hellwig 	struct extent_buffer *leaf;
65f46b5a66SChristoph Hellwig 	struct btrfs_root *new_root = root;
66f46b5a66SChristoph Hellwig 	struct inode *dir;
67f46b5a66SChristoph Hellwig 	int ret;
68f46b5a66SChristoph Hellwig 	int err;
69f46b5a66SChristoph Hellwig 	u64 objectid;
70f46b5a66SChristoph Hellwig 	u64 new_dirid = BTRFS_FIRST_FREE_OBJECTID;
713de4586cSChris Mason 	u64 index = 0;
72f46b5a66SChristoph Hellwig 	unsigned long nr = 1;
73f46b5a66SChristoph Hellwig 
74f46b5a66SChristoph Hellwig 	ret = btrfs_check_free_space(root, 1, 0);
75f46b5a66SChristoph Hellwig 	if (ret)
76f46b5a66SChristoph Hellwig 		goto fail_commit;
77f46b5a66SChristoph Hellwig 
78f46b5a66SChristoph Hellwig 	trans = btrfs_start_transaction(root, 1);
79f46b5a66SChristoph Hellwig 	BUG_ON(!trans);
80f46b5a66SChristoph Hellwig 
81f46b5a66SChristoph Hellwig 	ret = btrfs_find_free_objectid(trans, root->fs_info->tree_root,
82f46b5a66SChristoph Hellwig 				       0, &objectid);
83f46b5a66SChristoph Hellwig 	if (ret)
84f46b5a66SChristoph Hellwig 		goto fail;
85f46b5a66SChristoph Hellwig 
8631840ae1SZheng Yan 	leaf = btrfs_alloc_free_block(trans, root, root->leafsize, 0,
8731840ae1SZheng Yan 				      objectid, trans->transid, 0, 0, 0);
888e8a1e31SJosef Bacik 	if (IS_ERR(leaf)) {
898e8a1e31SJosef Bacik 		ret = PTR_ERR(leaf);
908e8a1e31SJosef Bacik 		goto fail;
918e8a1e31SJosef Bacik 	}
92f46b5a66SChristoph Hellwig 
93f46b5a66SChristoph Hellwig 	btrfs_set_header_nritems(leaf, 0);
94f46b5a66SChristoph Hellwig 	btrfs_set_header_level(leaf, 0);
95f46b5a66SChristoph Hellwig 	btrfs_set_header_bytenr(leaf, leaf->start);
96f46b5a66SChristoph Hellwig 	btrfs_set_header_generation(leaf, trans->transid);
97f46b5a66SChristoph Hellwig 	btrfs_set_header_owner(leaf, objectid);
98f46b5a66SChristoph Hellwig 
99f46b5a66SChristoph Hellwig 	write_extent_buffer(leaf, root->fs_info->fsid,
100f46b5a66SChristoph Hellwig 			    (unsigned long)btrfs_header_fsid(leaf),
101f46b5a66SChristoph Hellwig 			    BTRFS_FSID_SIZE);
102f46b5a66SChristoph Hellwig 	btrfs_mark_buffer_dirty(leaf);
103f46b5a66SChristoph Hellwig 
104f46b5a66SChristoph Hellwig 	inode_item = &root_item.inode;
105f46b5a66SChristoph Hellwig 	memset(inode_item, 0, sizeof(*inode_item));
106f46b5a66SChristoph Hellwig 	inode_item->generation = cpu_to_le64(1);
107f46b5a66SChristoph Hellwig 	inode_item->size = cpu_to_le64(3);
108f46b5a66SChristoph Hellwig 	inode_item->nlink = cpu_to_le32(1);
109a76a3cd4SYan Zheng 	inode_item->nbytes = cpu_to_le64(root->leafsize);
110f46b5a66SChristoph Hellwig 	inode_item->mode = cpu_to_le32(S_IFDIR | 0755);
111f46b5a66SChristoph Hellwig 
112f46b5a66SChristoph Hellwig 	btrfs_set_root_bytenr(&root_item, leaf->start);
11384234f3aSYan Zheng 	btrfs_set_root_generation(&root_item, trans->transid);
114f46b5a66SChristoph Hellwig 	btrfs_set_root_level(&root_item, 0);
115f46b5a66SChristoph Hellwig 	btrfs_set_root_refs(&root_item, 1);
116f46b5a66SChristoph Hellwig 	btrfs_set_root_used(&root_item, 0);
11780ff3856SYan Zheng 	btrfs_set_root_last_snapshot(&root_item, 0);
118f46b5a66SChristoph Hellwig 
119f46b5a66SChristoph Hellwig 	memset(&root_item.drop_progress, 0, sizeof(root_item.drop_progress));
120f46b5a66SChristoph Hellwig 	root_item.drop_level = 0;
121f46b5a66SChristoph Hellwig 
122925baeddSChris Mason 	btrfs_tree_unlock(leaf);
123f46b5a66SChristoph Hellwig 	free_extent_buffer(leaf);
124f46b5a66SChristoph Hellwig 	leaf = NULL;
125f46b5a66SChristoph Hellwig 
126f46b5a66SChristoph Hellwig 	btrfs_set_root_dirid(&root_item, new_dirid);
127f46b5a66SChristoph Hellwig 
128f46b5a66SChristoph Hellwig 	key.objectid = objectid;
129f46b5a66SChristoph Hellwig 	key.offset = 1;
130f46b5a66SChristoph Hellwig 	btrfs_set_key_type(&key, BTRFS_ROOT_ITEM_KEY);
131f46b5a66SChristoph Hellwig 	ret = btrfs_insert_root(trans, root->fs_info->tree_root, &key,
132f46b5a66SChristoph Hellwig 				&root_item);
133f46b5a66SChristoph Hellwig 	if (ret)
134f46b5a66SChristoph Hellwig 		goto fail;
135f46b5a66SChristoph Hellwig 
136f46b5a66SChristoph Hellwig 	/*
137f46b5a66SChristoph Hellwig 	 * insert the directory item
138f46b5a66SChristoph Hellwig 	 */
139f46b5a66SChristoph Hellwig 	key.offset = (u64)-1;
1403de4586cSChris Mason 	dir = dentry->d_parent->d_inode;
1413de4586cSChris Mason 	ret = btrfs_set_inode_index(dir, &index);
1423de4586cSChris Mason 	BUG_ON(ret);
1433de4586cSChris Mason 
1443de4586cSChris Mason 	ret = btrfs_insert_dir_item(trans, root,
145f46b5a66SChristoph Hellwig 				    name, namelen, dir->i_ino, &key,
1463de4586cSChris Mason 				    BTRFS_FT_DIR, index);
147f46b5a66SChristoph Hellwig 	if (ret)
148f46b5a66SChristoph Hellwig 		goto fail;
1490660b5afSChris Mason 
1500660b5afSChris Mason 	/* add the backref first */
1510660b5afSChris Mason 	ret = btrfs_add_root_ref(trans, root->fs_info->tree_root,
1520660b5afSChris Mason 				 objectid, BTRFS_ROOT_BACKREF_KEY,
1530660b5afSChris Mason 				 root->root_key.objectid,
1540660b5afSChris Mason 				 dir->i_ino, index, name, namelen);
1550660b5afSChris Mason 
1560660b5afSChris Mason 	BUG_ON(ret);
1570660b5afSChris Mason 
1580660b5afSChris Mason 	/* now add the forward ref */
1590660b5afSChris Mason 	ret = btrfs_add_root_ref(trans, root->fs_info->tree_root,
1600660b5afSChris Mason 				 root->root_key.objectid, BTRFS_ROOT_REF_KEY,
1610660b5afSChris Mason 				 objectid,
1620660b5afSChris Mason 				 dir->i_ino, index, name, namelen);
1630660b5afSChris Mason 
1640660b5afSChris Mason 	BUG_ON(ret);
1650660b5afSChris Mason 
166f46b5a66SChristoph Hellwig 	ret = btrfs_commit_transaction(trans, root);
167f46b5a66SChristoph Hellwig 	if (ret)
168f46b5a66SChristoph Hellwig 		goto fail_commit;
169f46b5a66SChristoph Hellwig 
1703de4586cSChris Mason 	new_root = btrfs_read_fs_root_no_name(root->fs_info, &key);
171f46b5a66SChristoph Hellwig 	BUG_ON(!new_root);
172f46b5a66SChristoph Hellwig 
173f46b5a66SChristoph Hellwig 	trans = btrfs_start_transaction(new_root, 1);
174f46b5a66SChristoph Hellwig 	BUG_ON(!trans);
175f46b5a66SChristoph Hellwig 
176cb8e7090SChristoph Hellwig 	ret = btrfs_create_subvol_root(new_root, dentry, trans, new_dirid,
177f46b5a66SChristoph Hellwig 				       BTRFS_I(dir)->block_group);
178f46b5a66SChristoph Hellwig 	if (ret)
179f46b5a66SChristoph Hellwig 		goto fail;
180f46b5a66SChristoph Hellwig 
181f46b5a66SChristoph Hellwig fail:
182f46b5a66SChristoph Hellwig 	nr = trans->blocks_used;
183f46b5a66SChristoph Hellwig 	err = btrfs_commit_transaction(trans, new_root);
184f46b5a66SChristoph Hellwig 	if (err && !ret)
185f46b5a66SChristoph Hellwig 		ret = err;
186f46b5a66SChristoph Hellwig fail_commit:
187f46b5a66SChristoph Hellwig 	btrfs_btree_balance_dirty(root, nr);
188f46b5a66SChristoph Hellwig 	return ret;
189f46b5a66SChristoph Hellwig }
190f46b5a66SChristoph Hellwig 
1913de4586cSChris Mason static int create_snapshot(struct btrfs_root *root, struct dentry *dentry,
1923de4586cSChris Mason 			   char *name, int namelen)
193f46b5a66SChristoph Hellwig {
194f46b5a66SChristoph Hellwig 	struct btrfs_pending_snapshot *pending_snapshot;
195f46b5a66SChristoph Hellwig 	struct btrfs_trans_handle *trans;
1963de4586cSChris Mason 	int ret = 0;
197f46b5a66SChristoph Hellwig 	int err;
198f46b5a66SChristoph Hellwig 	unsigned long nr = 0;
199f46b5a66SChristoph Hellwig 
200f46b5a66SChristoph Hellwig 	if (!root->ref_cows)
201f46b5a66SChristoph Hellwig 		return -EINVAL;
202f46b5a66SChristoph Hellwig 
203f46b5a66SChristoph Hellwig 	ret = btrfs_check_free_space(root, 1, 0);
204f46b5a66SChristoph Hellwig 	if (ret)
205f46b5a66SChristoph Hellwig 		goto fail_unlock;
206f46b5a66SChristoph Hellwig 
2073de4586cSChris Mason 	pending_snapshot = kzalloc(sizeof(*pending_snapshot), GFP_NOFS);
208f46b5a66SChristoph Hellwig 	if (!pending_snapshot) {
209f46b5a66SChristoph Hellwig 		ret = -ENOMEM;
210f46b5a66SChristoph Hellwig 		goto fail_unlock;
211f46b5a66SChristoph Hellwig 	}
212f46b5a66SChristoph Hellwig 	pending_snapshot->name = kmalloc(namelen + 1, GFP_NOFS);
213f46b5a66SChristoph Hellwig 	if (!pending_snapshot->name) {
214f46b5a66SChristoph Hellwig 		ret = -ENOMEM;
215f46b5a66SChristoph Hellwig 		kfree(pending_snapshot);
216f46b5a66SChristoph Hellwig 		goto fail_unlock;
217f46b5a66SChristoph Hellwig 	}
218f46b5a66SChristoph Hellwig 	memcpy(pending_snapshot->name, name, namelen);
219f46b5a66SChristoph Hellwig 	pending_snapshot->name[namelen] = '\0';
2203de4586cSChris Mason 	pending_snapshot->dentry = dentry;
221f46b5a66SChristoph Hellwig 	trans = btrfs_start_transaction(root, 1);
222f46b5a66SChristoph Hellwig 	BUG_ON(!trans);
223f46b5a66SChristoph Hellwig 	pending_snapshot->root = root;
224f46b5a66SChristoph Hellwig 	list_add(&pending_snapshot->list,
225f46b5a66SChristoph Hellwig 		 &trans->transaction->pending_snapshots);
226f46b5a66SChristoph Hellwig 	err = btrfs_commit_transaction(trans, root);
227f46b5a66SChristoph Hellwig 
228f46b5a66SChristoph Hellwig fail_unlock:
229f46b5a66SChristoph Hellwig 	btrfs_btree_balance_dirty(root, nr);
230f46b5a66SChristoph Hellwig 	return ret;
231f46b5a66SChristoph Hellwig }
232f46b5a66SChristoph Hellwig 
233cb8e7090SChristoph Hellwig /* copy of may_create in fs/namei.c() */
234cb8e7090SChristoph Hellwig static inline int btrfs_may_create(struct inode *dir, struct dentry *child)
235cb8e7090SChristoph Hellwig {
236cb8e7090SChristoph Hellwig 	if (child->d_inode)
237cb8e7090SChristoph Hellwig 		return -EEXIST;
238cb8e7090SChristoph Hellwig 	if (IS_DEADDIR(dir))
239cb8e7090SChristoph Hellwig 		return -ENOENT;
240cb8e7090SChristoph Hellwig 	return inode_permission(dir, MAY_WRITE | MAY_EXEC);
241cb8e7090SChristoph Hellwig }
242cb8e7090SChristoph Hellwig 
243cb8e7090SChristoph Hellwig /*
244cb8e7090SChristoph Hellwig  * Create a new subvolume below @parent.  This is largely modeled after
245cb8e7090SChristoph Hellwig  * sys_mkdirat and vfs_mkdir, but we only do a single component lookup
246cb8e7090SChristoph Hellwig  * inside this filesystem so it's quite a bit simpler.
247cb8e7090SChristoph Hellwig  */
248cb8e7090SChristoph Hellwig static noinline int btrfs_mksubvol(struct path *parent, char *name,
2493de4586cSChris Mason 				   int mode, int namelen,
2503de4586cSChris Mason 				   struct btrfs_root *snap_src)
251cb8e7090SChristoph Hellwig {
252cb8e7090SChristoph Hellwig 	struct dentry *dentry;
253cb8e7090SChristoph Hellwig 	int error;
254cb8e7090SChristoph Hellwig 
255cb8e7090SChristoph Hellwig 	mutex_lock_nested(&parent->dentry->d_inode->i_mutex, I_MUTEX_PARENT);
256cb8e7090SChristoph Hellwig 
257cb8e7090SChristoph Hellwig 	dentry = lookup_one_len(name, parent->dentry, namelen);
258cb8e7090SChristoph Hellwig 	error = PTR_ERR(dentry);
259cb8e7090SChristoph Hellwig 	if (IS_ERR(dentry))
260cb8e7090SChristoph Hellwig 		goto out_unlock;
261cb8e7090SChristoph Hellwig 
262cb8e7090SChristoph Hellwig 	error = -EEXIST;
263cb8e7090SChristoph Hellwig 	if (dentry->d_inode)
264cb8e7090SChristoph Hellwig 		goto out_dput;
265cb8e7090SChristoph Hellwig 
266cb8e7090SChristoph Hellwig 	if (!IS_POSIXACL(parent->dentry->d_inode))
267cb8e7090SChristoph Hellwig 		mode &= ~current->fs->umask;
2683de4586cSChris Mason 
269cb8e7090SChristoph Hellwig 	error = mnt_want_write(parent->mnt);
270cb8e7090SChristoph Hellwig 	if (error)
271cb8e7090SChristoph Hellwig 		goto out_dput;
272cb8e7090SChristoph Hellwig 
273cb8e7090SChristoph Hellwig 	error = btrfs_may_create(parent->dentry->d_inode, dentry);
274cb8e7090SChristoph Hellwig 	if (error)
275cb8e7090SChristoph Hellwig 		goto out_drop_write;
276cb8e7090SChristoph Hellwig 
277cb8e7090SChristoph Hellwig 	/*
278cb8e7090SChristoph Hellwig 	 * Actually perform the low-level subvolume creation after all
279cb8e7090SChristoph Hellwig 	 * this VFS fuzz.
280cb8e7090SChristoph Hellwig 	 *
281cb8e7090SChristoph Hellwig 	 * Eventually we want to pass in an inode under which we create this
282cb8e7090SChristoph Hellwig 	 * subvolume, but for now all are under the filesystem root.
283cb8e7090SChristoph Hellwig 	 *
284cb8e7090SChristoph Hellwig 	 * Also we should pass on the mode eventually to allow creating new
285cb8e7090SChristoph Hellwig 	 * subvolume with specific mode bits.
286cb8e7090SChristoph Hellwig 	 */
2873de4586cSChris Mason 	if (snap_src) {
288ea9e8b11SChris Mason 		struct dentry *dir = dentry->d_parent;
289ea9e8b11SChris Mason 		struct dentry *test = dir->d_parent;
290ea9e8b11SChris Mason 		struct btrfs_path *path = btrfs_alloc_path();
291ea9e8b11SChris Mason 		int ret;
292ea9e8b11SChris Mason 		u64 test_oid;
293ea9e8b11SChris Mason 		u64 parent_oid = BTRFS_I(dir->d_inode)->root->root_key.objectid;
294ea9e8b11SChris Mason 
295ea9e8b11SChris Mason 		test_oid = snap_src->root_key.objectid;
296ea9e8b11SChris Mason 
297ea9e8b11SChris Mason 		ret = btrfs_find_root_ref(snap_src->fs_info->tree_root,
298ea9e8b11SChris Mason 					  path, parent_oid, test_oid);
299ea9e8b11SChris Mason 		if (ret == 0)
300ea9e8b11SChris Mason 			goto create;
301ea9e8b11SChris Mason 		btrfs_release_path(snap_src->fs_info->tree_root, path);
302ea9e8b11SChris Mason 
303ea9e8b11SChris Mason 		/* we need to make sure we aren't creating a directory loop
304ea9e8b11SChris Mason 		 * by taking a snapshot of something that has our current
305ea9e8b11SChris Mason 		 * subvol in its directory tree.  So, this loops through
306ea9e8b11SChris Mason 		 * the dentries and checks the forward refs for each subvolume
307ea9e8b11SChris Mason 		 * to see if is references the subvolume where we are
308ea9e8b11SChris Mason 		 * placing this new snapshot.
309ea9e8b11SChris Mason 		 */
310ea9e8b11SChris Mason 		while(1) {
311ea9e8b11SChris Mason 			if (!test ||
312ea9e8b11SChris Mason 			    dir == snap_src->fs_info->sb->s_root ||
313ea9e8b11SChris Mason 			    test == snap_src->fs_info->sb->s_root ||
314ea9e8b11SChris Mason 			    test->d_inode->i_sb != snap_src->fs_info->sb) {
315ea9e8b11SChris Mason 				break;
316ea9e8b11SChris Mason 			}
317ea9e8b11SChris Mason 			if (S_ISLNK(test->d_inode->i_mode)) {
318ea9e8b11SChris Mason 				printk("Symlink in snapshot path, failed\n");
319ea9e8b11SChris Mason 				error = -EMLINK;
320ea9e8b11SChris Mason 				btrfs_free_path(path);
321ea9e8b11SChris Mason 				goto out_drop_write;
322ea9e8b11SChris Mason 			}
323ea9e8b11SChris Mason 			test_oid =
324ea9e8b11SChris Mason 				BTRFS_I(test->d_inode)->root->root_key.objectid;
325ea9e8b11SChris Mason 			ret = btrfs_find_root_ref(snap_src->fs_info->tree_root,
326ea9e8b11SChris Mason 				  path, test_oid, parent_oid);
327ea9e8b11SChris Mason 			if (ret == 0) {
328ea9e8b11SChris Mason 				printk("Snapshot creation failed, looping\n");
329ea9e8b11SChris Mason 				error = -EMLINK;
330ea9e8b11SChris Mason 				btrfs_free_path(path);
331ea9e8b11SChris Mason 				goto out_drop_write;
332ea9e8b11SChris Mason 			}
333ea9e8b11SChris Mason 			btrfs_release_path(snap_src->fs_info->tree_root, path);
334ea9e8b11SChris Mason 			test = test->d_parent;
335ea9e8b11SChris Mason 		}
336ea9e8b11SChris Mason create:
337ea9e8b11SChris Mason 		btrfs_free_path(path);
3383de4586cSChris Mason 		error = create_snapshot(snap_src, dentry, name, namelen);
3393de4586cSChris Mason 	} else {
3403de4586cSChris Mason 		error = create_subvol(BTRFS_I(parent->dentry->d_inode)->root,
3413de4586cSChris Mason 				      dentry, name, namelen);
3423de4586cSChris Mason 	}
343cb8e7090SChristoph Hellwig 	if (error)
344cb8e7090SChristoph Hellwig 		goto out_drop_write;
345cb8e7090SChristoph Hellwig 
346cb8e7090SChristoph Hellwig 	fsnotify_mkdir(parent->dentry->d_inode, dentry);
347cb8e7090SChristoph Hellwig out_drop_write:
348cb8e7090SChristoph Hellwig 	mnt_drop_write(parent->mnt);
349cb8e7090SChristoph Hellwig out_dput:
350cb8e7090SChristoph Hellwig 	dput(dentry);
351cb8e7090SChristoph Hellwig out_unlock:
352cb8e7090SChristoph Hellwig 	mutex_unlock(&parent->dentry->d_inode->i_mutex);
353cb8e7090SChristoph Hellwig 	return error;
354cb8e7090SChristoph Hellwig }
355cb8e7090SChristoph Hellwig 
356cb8e7090SChristoph Hellwig 
357b2950863SChristoph Hellwig static int btrfs_defrag_file(struct file *file)
358f46b5a66SChristoph Hellwig {
359f46b5a66SChristoph Hellwig 	struct inode *inode = fdentry(file)->d_inode;
360f46b5a66SChristoph Hellwig 	struct btrfs_root *root = BTRFS_I(inode)->root;
361f46b5a66SChristoph Hellwig 	struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
3623eaa2885SChris Mason 	struct btrfs_ordered_extent *ordered;
363f46b5a66SChristoph Hellwig 	struct page *page;
364f46b5a66SChristoph Hellwig 	unsigned long last_index;
365f46b5a66SChristoph Hellwig 	unsigned long ra_pages = root->fs_info->bdi.ra_pages;
366f46b5a66SChristoph Hellwig 	unsigned long total_read = 0;
367f46b5a66SChristoph Hellwig 	u64 page_start;
368f46b5a66SChristoph Hellwig 	u64 page_end;
369f46b5a66SChristoph Hellwig 	unsigned long i;
370f46b5a66SChristoph Hellwig 	int ret;
371f46b5a66SChristoph Hellwig 
372f46b5a66SChristoph Hellwig 	ret = btrfs_check_free_space(root, inode->i_size, 0);
373f46b5a66SChristoph Hellwig 	if (ret)
374f46b5a66SChristoph Hellwig 		return -ENOSPC;
375f46b5a66SChristoph Hellwig 
376f46b5a66SChristoph Hellwig 	mutex_lock(&inode->i_mutex);
377f46b5a66SChristoph Hellwig 	last_index = inode->i_size >> PAGE_CACHE_SHIFT;
378f46b5a66SChristoph Hellwig 	for (i = 0; i <= last_index; i++) {
379f46b5a66SChristoph Hellwig 		if (total_read % ra_pages == 0) {
380f46b5a66SChristoph Hellwig 			btrfs_force_ra(inode->i_mapping, &file->f_ra, file, i,
381f46b5a66SChristoph Hellwig 				       min(last_index, i + ra_pages - 1));
382f46b5a66SChristoph Hellwig 		}
383f46b5a66SChristoph Hellwig 		total_read++;
3843eaa2885SChris Mason again:
385f46b5a66SChristoph Hellwig 		page = grab_cache_page(inode->i_mapping, i);
386f46b5a66SChristoph Hellwig 		if (!page)
387f46b5a66SChristoph Hellwig 			goto out_unlock;
388f46b5a66SChristoph Hellwig 		if (!PageUptodate(page)) {
389f46b5a66SChristoph Hellwig 			btrfs_readpage(NULL, page);
390f46b5a66SChristoph Hellwig 			lock_page(page);
391f46b5a66SChristoph Hellwig 			if (!PageUptodate(page)) {
392f46b5a66SChristoph Hellwig 				unlock_page(page);
393f46b5a66SChristoph Hellwig 				page_cache_release(page);
394f46b5a66SChristoph Hellwig 				goto out_unlock;
395f46b5a66SChristoph Hellwig 			}
396f46b5a66SChristoph Hellwig 		}
397f46b5a66SChristoph Hellwig 
398f46b5a66SChristoph Hellwig 		wait_on_page_writeback(page);
399f46b5a66SChristoph Hellwig 
400f46b5a66SChristoph Hellwig 		page_start = (u64)page->index << PAGE_CACHE_SHIFT;
401f46b5a66SChristoph Hellwig 		page_end = page_start + PAGE_CACHE_SIZE - 1;
402f46b5a66SChristoph Hellwig 		lock_extent(io_tree, page_start, page_end, GFP_NOFS);
4033eaa2885SChris Mason 
4043eaa2885SChris Mason 		ordered = btrfs_lookup_ordered_extent(inode, page_start);
4053eaa2885SChris Mason 		if (ordered) {
4063eaa2885SChris Mason 			unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
4073eaa2885SChris Mason 			unlock_page(page);
4083eaa2885SChris Mason 			page_cache_release(page);
4093eaa2885SChris Mason 			btrfs_start_ordered_extent(inode, ordered, 1);
4103eaa2885SChris Mason 			btrfs_put_ordered_extent(ordered);
4113eaa2885SChris Mason 			goto again;
4123eaa2885SChris Mason 		}
4133eaa2885SChris Mason 		set_page_extent_mapped(page);
4143eaa2885SChris Mason 
415f87f057bSChris Mason 		/*
416f87f057bSChris Mason 		 * this makes sure page_mkwrite is called on the
417f87f057bSChris Mason 		 * page if it is dirtied again later
418f87f057bSChris Mason 		 */
419f87f057bSChris Mason 		clear_page_dirty_for_io(page);
420f87f057bSChris Mason 
421ea8c2819SChris Mason 		btrfs_set_extent_delalloc(inode, page_start, page_end);
422f46b5a66SChristoph Hellwig 
423f46b5a66SChristoph Hellwig 		unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
424f46b5a66SChristoph Hellwig 		set_page_dirty(page);
425f46b5a66SChristoph Hellwig 		unlock_page(page);
426f46b5a66SChristoph Hellwig 		page_cache_release(page);
427f46b5a66SChristoph Hellwig 		balance_dirty_pages_ratelimited_nr(inode->i_mapping, 1);
428f46b5a66SChristoph Hellwig 	}
429f46b5a66SChristoph Hellwig 
430f46b5a66SChristoph Hellwig out_unlock:
431f46b5a66SChristoph Hellwig 	mutex_unlock(&inode->i_mutex);
432f46b5a66SChristoph Hellwig 	return 0;
433f46b5a66SChristoph Hellwig }
434f46b5a66SChristoph Hellwig 
435f46b5a66SChristoph Hellwig /*
436f46b5a66SChristoph Hellwig  * Called inside transaction, so use GFP_NOFS
437f46b5a66SChristoph Hellwig  */
438f46b5a66SChristoph Hellwig 
439f46b5a66SChristoph Hellwig static int btrfs_ioctl_resize(struct btrfs_root *root, void __user *arg)
440f46b5a66SChristoph Hellwig {
441f46b5a66SChristoph Hellwig 	u64 new_size;
442f46b5a66SChristoph Hellwig 	u64 old_size;
443f46b5a66SChristoph Hellwig 	u64 devid = 1;
444f46b5a66SChristoph Hellwig 	struct btrfs_ioctl_vol_args *vol_args;
445f46b5a66SChristoph Hellwig 	struct btrfs_trans_handle *trans;
446f46b5a66SChristoph Hellwig 	struct btrfs_device *device = NULL;
447f46b5a66SChristoph Hellwig 	char *sizestr;
448f46b5a66SChristoph Hellwig 	char *devstr = NULL;
449f46b5a66SChristoph Hellwig 	int ret = 0;
450f46b5a66SChristoph Hellwig 	int namelen;
451f46b5a66SChristoph Hellwig 	int mod = 0;
452f46b5a66SChristoph Hellwig 
453c146afadSYan Zheng 	if (root->fs_info->sb->s_flags & MS_RDONLY)
454c146afadSYan Zheng 		return -EROFS;
455c146afadSYan Zheng 
456f46b5a66SChristoph Hellwig 	vol_args = kmalloc(sizeof(*vol_args), GFP_NOFS);
457f46b5a66SChristoph Hellwig 
458f46b5a66SChristoph Hellwig 	if (!vol_args)
459f46b5a66SChristoph Hellwig 		return -ENOMEM;
460f46b5a66SChristoph Hellwig 
461f46b5a66SChristoph Hellwig 	if (copy_from_user(vol_args, arg, sizeof(*vol_args))) {
462f46b5a66SChristoph Hellwig 		ret = -EFAULT;
463f46b5a66SChristoph Hellwig 		goto out;
464f46b5a66SChristoph Hellwig 	}
4655516e595SMark Fasheh 
4665516e595SMark Fasheh 	vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
467f46b5a66SChristoph Hellwig 	namelen = strlen(vol_args->name);
468f46b5a66SChristoph Hellwig 
4697d9eb12cSChris Mason 	mutex_lock(&root->fs_info->volume_mutex);
470f46b5a66SChristoph Hellwig 	sizestr = vol_args->name;
471f46b5a66SChristoph Hellwig 	devstr = strchr(sizestr, ':');
472f46b5a66SChristoph Hellwig 	if (devstr) {
473f46b5a66SChristoph Hellwig 		char *end;
474f46b5a66SChristoph Hellwig 		sizestr = devstr + 1;
475f46b5a66SChristoph Hellwig 		*devstr = '\0';
476f46b5a66SChristoph Hellwig 		devstr = vol_args->name;
477f46b5a66SChristoph Hellwig 		devid = simple_strtoull(devstr, &end, 10);
478f46b5a66SChristoph Hellwig 		printk(KERN_INFO "resizing devid %llu\n", devid);
479f46b5a66SChristoph Hellwig 	}
4802b82032cSYan Zheng 	device = btrfs_find_device(root, devid, NULL, NULL);
481f46b5a66SChristoph Hellwig 	if (!device) {
482f46b5a66SChristoph Hellwig 		printk(KERN_INFO "resizer unable to find device %llu\n", devid);
483f46b5a66SChristoph Hellwig 		ret = -EINVAL;
484f46b5a66SChristoph Hellwig 		goto out_unlock;
485f46b5a66SChristoph Hellwig 	}
486f46b5a66SChristoph Hellwig 	if (!strcmp(sizestr, "max"))
487f46b5a66SChristoph Hellwig 		new_size = device->bdev->bd_inode->i_size;
488f46b5a66SChristoph Hellwig 	else {
489f46b5a66SChristoph Hellwig 		if (sizestr[0] == '-') {
490f46b5a66SChristoph Hellwig 			mod = -1;
491f46b5a66SChristoph Hellwig 			sizestr++;
492f46b5a66SChristoph Hellwig 		} else if (sizestr[0] == '+') {
493f46b5a66SChristoph Hellwig 			mod = 1;
494f46b5a66SChristoph Hellwig 			sizestr++;
495f46b5a66SChristoph Hellwig 		}
496f46b5a66SChristoph Hellwig 		new_size = btrfs_parse_size(sizestr);
497f46b5a66SChristoph Hellwig 		if (new_size == 0) {
498f46b5a66SChristoph Hellwig 			ret = -EINVAL;
499f46b5a66SChristoph Hellwig 			goto out_unlock;
500f46b5a66SChristoph Hellwig 		}
501f46b5a66SChristoph Hellwig 	}
502f46b5a66SChristoph Hellwig 
503f46b5a66SChristoph Hellwig 	old_size = device->total_bytes;
504f46b5a66SChristoph Hellwig 
505f46b5a66SChristoph Hellwig 	if (mod < 0) {
506f46b5a66SChristoph Hellwig 		if (new_size > old_size) {
507f46b5a66SChristoph Hellwig 			ret = -EINVAL;
508f46b5a66SChristoph Hellwig 			goto out_unlock;
509f46b5a66SChristoph Hellwig 		}
510f46b5a66SChristoph Hellwig 		new_size = old_size - new_size;
511f46b5a66SChristoph Hellwig 	} else if (mod > 0) {
512f46b5a66SChristoph Hellwig 		new_size = old_size + new_size;
513f46b5a66SChristoph Hellwig 	}
514f46b5a66SChristoph Hellwig 
515f46b5a66SChristoph Hellwig 	if (new_size < 256 * 1024 * 1024) {
516f46b5a66SChristoph Hellwig 		ret = -EINVAL;
517f46b5a66SChristoph Hellwig 		goto out_unlock;
518f46b5a66SChristoph Hellwig 	}
519f46b5a66SChristoph Hellwig 	if (new_size > device->bdev->bd_inode->i_size) {
520f46b5a66SChristoph Hellwig 		ret = -EFBIG;
521f46b5a66SChristoph Hellwig 		goto out_unlock;
522f46b5a66SChristoph Hellwig 	}
523f46b5a66SChristoph Hellwig 
524f46b5a66SChristoph Hellwig 	do_div(new_size, root->sectorsize);
525f46b5a66SChristoph Hellwig 	new_size *= root->sectorsize;
526f46b5a66SChristoph Hellwig 
527f46b5a66SChristoph Hellwig 	printk(KERN_INFO "new size for %s is %llu\n",
528f46b5a66SChristoph Hellwig 		device->name, (unsigned long long)new_size);
529f46b5a66SChristoph Hellwig 
530f46b5a66SChristoph Hellwig 	if (new_size > old_size) {
531f46b5a66SChristoph Hellwig 		trans = btrfs_start_transaction(root, 1);
532f46b5a66SChristoph Hellwig 		ret = btrfs_grow_device(trans, device, new_size);
533f46b5a66SChristoph Hellwig 		btrfs_commit_transaction(trans, root);
534f46b5a66SChristoph Hellwig 	} else {
535f46b5a66SChristoph Hellwig 		ret = btrfs_shrink_device(device, new_size);
536f46b5a66SChristoph Hellwig 	}
537f46b5a66SChristoph Hellwig 
538f46b5a66SChristoph Hellwig out_unlock:
5397d9eb12cSChris Mason 	mutex_unlock(&root->fs_info->volume_mutex);
540f46b5a66SChristoph Hellwig out:
541f46b5a66SChristoph Hellwig 	kfree(vol_args);
542f46b5a66SChristoph Hellwig 	return ret;
543f46b5a66SChristoph Hellwig }
544f46b5a66SChristoph Hellwig 
545cb8e7090SChristoph Hellwig static noinline int btrfs_ioctl_snap_create(struct file *file,
5463de4586cSChris Mason 					    void __user *arg, int subvol)
547f46b5a66SChristoph Hellwig {
548cb8e7090SChristoph Hellwig 	struct btrfs_root *root = BTRFS_I(fdentry(file)->d_inode)->root;
549f46b5a66SChristoph Hellwig 	struct btrfs_ioctl_vol_args *vol_args;
550f46b5a66SChristoph Hellwig 	struct btrfs_dir_item *di;
551f46b5a66SChristoph Hellwig 	struct btrfs_path *path;
5523de4586cSChris Mason 	struct file *src_file;
553f46b5a66SChristoph Hellwig 	u64 root_dirid;
554f46b5a66SChristoph Hellwig 	int namelen;
5553de4586cSChris Mason 	int ret = 0;
556f46b5a66SChristoph Hellwig 
557c146afadSYan Zheng 	if (root->fs_info->sb->s_flags & MS_RDONLY)
558c146afadSYan Zheng 		return -EROFS;
559c146afadSYan Zheng 
560f46b5a66SChristoph Hellwig 	vol_args = kmalloc(sizeof(*vol_args), GFP_NOFS);
561f46b5a66SChristoph Hellwig 
562f46b5a66SChristoph Hellwig 	if (!vol_args)
563f46b5a66SChristoph Hellwig 		return -ENOMEM;
564f46b5a66SChristoph Hellwig 
565f46b5a66SChristoph Hellwig 	if (copy_from_user(vol_args, arg, sizeof(*vol_args))) {
566f46b5a66SChristoph Hellwig 		ret = -EFAULT;
567f46b5a66SChristoph Hellwig 		goto out;
568f46b5a66SChristoph Hellwig 	}
569f46b5a66SChristoph Hellwig 
5705516e595SMark Fasheh 	vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
571f46b5a66SChristoph Hellwig 	namelen = strlen(vol_args->name);
572f46b5a66SChristoph Hellwig 	if (strchr(vol_args->name, '/')) {
573f46b5a66SChristoph Hellwig 		ret = -EINVAL;
574f46b5a66SChristoph Hellwig 		goto out;
575f46b5a66SChristoph Hellwig 	}
576f46b5a66SChristoph Hellwig 
577f46b5a66SChristoph Hellwig 	path = btrfs_alloc_path();
578f46b5a66SChristoph Hellwig 	if (!path) {
579f46b5a66SChristoph Hellwig 		ret = -ENOMEM;
580f46b5a66SChristoph Hellwig 		goto out;
581f46b5a66SChristoph Hellwig 	}
582f46b5a66SChristoph Hellwig 
583f46b5a66SChristoph Hellwig 	root_dirid = root->fs_info->sb->s_root->d_inode->i_ino,
584f46b5a66SChristoph Hellwig 	di = btrfs_lookup_dir_item(NULL, root->fs_info->tree_root,
585f46b5a66SChristoph Hellwig 			    path, root_dirid,
586f46b5a66SChristoph Hellwig 			    vol_args->name, namelen, 0);
587f46b5a66SChristoph Hellwig 	btrfs_free_path(path);
588f46b5a66SChristoph Hellwig 
589f46b5a66SChristoph Hellwig 	if (di && !IS_ERR(di)) {
590f46b5a66SChristoph Hellwig 		ret = -EEXIST;
591f46b5a66SChristoph Hellwig 		goto out;
592f46b5a66SChristoph Hellwig 	}
593f46b5a66SChristoph Hellwig 
594f46b5a66SChristoph Hellwig 	if (IS_ERR(di)) {
595f46b5a66SChristoph Hellwig 		ret = PTR_ERR(di);
596f46b5a66SChristoph Hellwig 		goto out;
597f46b5a66SChristoph Hellwig 	}
598f46b5a66SChristoph Hellwig 
5993de4586cSChris Mason 	if (subvol) {
600cb8e7090SChristoph Hellwig 		ret = btrfs_mksubvol(&file->f_path, vol_args->name,
601cb8e7090SChristoph Hellwig 				     file->f_path.dentry->d_inode->i_mode,
6023de4586cSChris Mason 				     namelen, NULL);
603cb8e7090SChristoph Hellwig 	} else {
6043de4586cSChris Mason 		struct inode *src_inode;
6053de4586cSChris Mason 		src_file = fget(vol_args->fd);
6063de4586cSChris Mason 		if (!src_file) {
6073de4586cSChris Mason 			ret = -EINVAL;
6083de4586cSChris Mason 			goto out;
6093de4586cSChris Mason 		}
6103de4586cSChris Mason 
6113de4586cSChris Mason 		src_inode = src_file->f_path.dentry->d_inode;
6123de4586cSChris Mason 		if (src_inode->i_sb != file->f_path.dentry->d_inode->i_sb) {
6133de4586cSChris Mason 			printk("btrfs: Snapshot src from another FS\n");
6143de4586cSChris Mason 			ret = -EINVAL;
6153de4586cSChris Mason 			fput(src_file);
6163de4586cSChris Mason 			goto out;
6173de4586cSChris Mason 		}
6183de4586cSChris Mason 		ret = btrfs_mksubvol(&file->f_path, vol_args->name,
6193de4586cSChris Mason 			     file->f_path.dentry->d_inode->i_mode,
6203de4586cSChris Mason 			     namelen, BTRFS_I(src_inode)->root);
6213de4586cSChris Mason 		fput(src_file);
622cb8e7090SChristoph Hellwig 	}
623cb8e7090SChristoph Hellwig 
624f46b5a66SChristoph Hellwig out:
625f46b5a66SChristoph Hellwig 	kfree(vol_args);
626f46b5a66SChristoph Hellwig 	return ret;
627f46b5a66SChristoph Hellwig }
628f46b5a66SChristoph Hellwig 
629f46b5a66SChristoph Hellwig static int btrfs_ioctl_defrag(struct file *file)
630f46b5a66SChristoph Hellwig {
631f46b5a66SChristoph Hellwig 	struct inode *inode = fdentry(file)->d_inode;
632f46b5a66SChristoph Hellwig 	struct btrfs_root *root = BTRFS_I(inode)->root;
633c146afadSYan Zheng 	int ret;
634c146afadSYan Zheng 
635c146afadSYan Zheng 	ret = mnt_want_write(file->f_path.mnt);
636c146afadSYan Zheng 	if (ret)
637c146afadSYan Zheng 		return ret;
638f46b5a66SChristoph Hellwig 
639f46b5a66SChristoph Hellwig 	switch (inode->i_mode & S_IFMT) {
640f46b5a66SChristoph Hellwig 	case S_IFDIR:
641f46b5a66SChristoph Hellwig 		btrfs_defrag_root(root, 0);
642f46b5a66SChristoph Hellwig 		btrfs_defrag_root(root->fs_info->extent_root, 0);
643f46b5a66SChristoph Hellwig 		break;
644f46b5a66SChristoph Hellwig 	case S_IFREG:
645f46b5a66SChristoph Hellwig 		btrfs_defrag_file(file);
646f46b5a66SChristoph Hellwig 		break;
647f46b5a66SChristoph Hellwig 	}
648f46b5a66SChristoph Hellwig 
649f46b5a66SChristoph Hellwig 	return 0;
650f46b5a66SChristoph Hellwig }
651f46b5a66SChristoph Hellwig 
652b2950863SChristoph Hellwig static long btrfs_ioctl_add_dev(struct btrfs_root *root, void __user *arg)
653f46b5a66SChristoph Hellwig {
654f46b5a66SChristoph Hellwig 	struct btrfs_ioctl_vol_args *vol_args;
655f46b5a66SChristoph Hellwig 	int ret;
656f46b5a66SChristoph Hellwig 
657f46b5a66SChristoph Hellwig 	vol_args = kmalloc(sizeof(*vol_args), GFP_NOFS);
658f46b5a66SChristoph Hellwig 
659f46b5a66SChristoph Hellwig 	if (!vol_args)
660f46b5a66SChristoph Hellwig 		return -ENOMEM;
661f46b5a66SChristoph Hellwig 
662f46b5a66SChristoph Hellwig 	if (copy_from_user(vol_args, arg, sizeof(*vol_args))) {
663f46b5a66SChristoph Hellwig 		ret = -EFAULT;
664f46b5a66SChristoph Hellwig 		goto out;
665f46b5a66SChristoph Hellwig 	}
6665516e595SMark Fasheh 	vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
667f46b5a66SChristoph Hellwig 	ret = btrfs_init_new_device(root, vol_args->name);
668f46b5a66SChristoph Hellwig 
669f46b5a66SChristoph Hellwig out:
670f46b5a66SChristoph Hellwig 	kfree(vol_args);
671f46b5a66SChristoph Hellwig 	return ret;
672f46b5a66SChristoph Hellwig }
673f46b5a66SChristoph Hellwig 
674b2950863SChristoph Hellwig static long btrfs_ioctl_rm_dev(struct btrfs_root *root, void __user *arg)
675f46b5a66SChristoph Hellwig {
676f46b5a66SChristoph Hellwig 	struct btrfs_ioctl_vol_args *vol_args;
677f46b5a66SChristoph Hellwig 	int ret;
678f46b5a66SChristoph Hellwig 
679c146afadSYan Zheng 	if (root->fs_info->sb->s_flags & MS_RDONLY)
680c146afadSYan Zheng 		return -EROFS;
681c146afadSYan Zheng 
682f46b5a66SChristoph Hellwig 	vol_args = kmalloc(sizeof(*vol_args), GFP_NOFS);
683f46b5a66SChristoph Hellwig 
684f46b5a66SChristoph Hellwig 	if (!vol_args)
685f46b5a66SChristoph Hellwig 		return -ENOMEM;
686f46b5a66SChristoph Hellwig 
687f46b5a66SChristoph Hellwig 	if (copy_from_user(vol_args, arg, sizeof(*vol_args))) {
688f46b5a66SChristoph Hellwig 		ret = -EFAULT;
689f46b5a66SChristoph Hellwig 		goto out;
690f46b5a66SChristoph Hellwig 	}
6915516e595SMark Fasheh 	vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
692f46b5a66SChristoph Hellwig 	ret = btrfs_rm_device(root, vol_args->name);
693f46b5a66SChristoph Hellwig 
694f46b5a66SChristoph Hellwig out:
695f46b5a66SChristoph Hellwig 	kfree(vol_args);
696f46b5a66SChristoph Hellwig 	return ret;
697f46b5a66SChristoph Hellwig }
698f46b5a66SChristoph Hellwig 
699b2950863SChristoph Hellwig static long btrfs_ioctl_clone(struct file *file, unsigned long srcfd,
700b2950863SChristoph Hellwig 		u64 off, u64 olen, u64 destoff)
701f46b5a66SChristoph Hellwig {
702f46b5a66SChristoph Hellwig 	struct inode *inode = fdentry(file)->d_inode;
703f46b5a66SChristoph Hellwig 	struct btrfs_root *root = BTRFS_I(inode)->root;
704f46b5a66SChristoph Hellwig 	struct file *src_file;
705f46b5a66SChristoph Hellwig 	struct inode *src;
706f46b5a66SChristoph Hellwig 	struct btrfs_trans_handle *trans;
707f46b5a66SChristoph Hellwig 	struct btrfs_path *path;
708f46b5a66SChristoph Hellwig 	struct extent_buffer *leaf;
709ae01a0abSYan Zheng 	char *buf;
710ae01a0abSYan Zheng 	struct btrfs_key key;
711f46b5a66SChristoph Hellwig 	u32 nritems;
712f46b5a66SChristoph Hellwig 	int slot;
713ae01a0abSYan Zheng 	int ret;
714c5c9cd4dSSage Weil 	u64 len = olen;
715c5c9cd4dSSage Weil 	u64 bs = root->fs_info->sb->s_blocksize;
716c5c9cd4dSSage Weil 	u64 hint_byte;
717f46b5a66SChristoph Hellwig 
718c5c9cd4dSSage Weil 	/*
719c5c9cd4dSSage Weil 	 * TODO:
720c5c9cd4dSSage Weil 	 * - split compressed inline extents.  annoying: we need to
721c5c9cd4dSSage Weil 	 *   decompress into destination's address_space (the file offset
722c5c9cd4dSSage Weil 	 *   may change, so source mapping won't do), then recompress (or
723c5c9cd4dSSage Weil 	 *   otherwise reinsert) a subrange.
724c5c9cd4dSSage Weil 	 * - allow ranges within the same file to be cloned (provided
725c5c9cd4dSSage Weil 	 *   they don't overlap)?
726c5c9cd4dSSage Weil 	 */
727c5c9cd4dSSage Weil 
728c146afadSYan Zheng 	ret = mnt_want_write(file->f_path.mnt);
729c146afadSYan Zheng 	if (ret)
730c146afadSYan Zheng 		return ret;
731c146afadSYan Zheng 
732c5c9cd4dSSage Weil 	src_file = fget(srcfd);
733f46b5a66SChristoph Hellwig 	if (!src_file)
734f46b5a66SChristoph Hellwig 		return -EBADF;
735f46b5a66SChristoph Hellwig 	src = src_file->f_dentry->d_inode;
736f46b5a66SChristoph Hellwig 
737c5c9cd4dSSage Weil 	ret = -EINVAL;
738c5c9cd4dSSage Weil 	if (src == inode)
739c5c9cd4dSSage Weil 		goto out_fput;
740c5c9cd4dSSage Weil 
741ae01a0abSYan Zheng 	ret = -EISDIR;
742ae01a0abSYan Zheng 	if (S_ISDIR(src->i_mode) || S_ISDIR(inode->i_mode))
743f46b5a66SChristoph Hellwig 		goto out_fput;
744f46b5a66SChristoph Hellwig 
745ae01a0abSYan Zheng 	ret = -EXDEV;
746ae01a0abSYan Zheng 	if (src->i_sb != inode->i_sb || BTRFS_I(src)->root != root)
747ae01a0abSYan Zheng 		goto out_fput;
748ae01a0abSYan Zheng 
749ae01a0abSYan Zheng 	ret = -ENOMEM;
750ae01a0abSYan Zheng 	buf = vmalloc(btrfs_level_size(root, 0));
751ae01a0abSYan Zheng 	if (!buf)
752ae01a0abSYan Zheng 		goto out_fput;
753ae01a0abSYan Zheng 
754ae01a0abSYan Zheng 	path = btrfs_alloc_path();
755ae01a0abSYan Zheng 	if (!path) {
756ae01a0abSYan Zheng 		vfree(buf);
757ae01a0abSYan Zheng 		goto out_fput;
758ae01a0abSYan Zheng 	}
759ae01a0abSYan Zheng 	path->reada = 2;
760ae01a0abSYan Zheng 
761f46b5a66SChristoph Hellwig 	if (inode < src) {
762f46b5a66SChristoph Hellwig 		mutex_lock(&inode->i_mutex);
763f46b5a66SChristoph Hellwig 		mutex_lock(&src->i_mutex);
764f46b5a66SChristoph Hellwig 	} else {
765f46b5a66SChristoph Hellwig 		mutex_lock(&src->i_mutex);
766f46b5a66SChristoph Hellwig 		mutex_lock(&inode->i_mutex);
767f46b5a66SChristoph Hellwig 	}
768f46b5a66SChristoph Hellwig 
769c5c9cd4dSSage Weil 	/* determine range to clone */
770c5c9cd4dSSage Weil 	ret = -EINVAL;
771c5c9cd4dSSage Weil 	if (off >= src->i_size || off + len > src->i_size)
772f46b5a66SChristoph Hellwig 		goto out_unlock;
773c5c9cd4dSSage Weil 	if (len == 0)
774c5c9cd4dSSage Weil 		olen = len = src->i_size - off;
775c5c9cd4dSSage Weil 	/* if we extend to eof, continue to block boundary */
776c5c9cd4dSSage Weil 	if (off + len == src->i_size)
777c5c9cd4dSSage Weil 		len = ((src->i_size + bs-1) & ~(bs-1))
778c5c9cd4dSSage Weil 			- off;
779c5c9cd4dSSage Weil 
780c5c9cd4dSSage Weil 	/* verify the end result is block aligned */
781c5c9cd4dSSage Weil 	if ((off & (bs-1)) ||
782c5c9cd4dSSage Weil 	    ((off + len) & (bs-1)))
783c5c9cd4dSSage Weil 		goto out_unlock;
784c5c9cd4dSSage Weil 
785c5c9cd4dSSage Weil 	printk("final src extent is %llu~%llu\n", off, len);
786c5c9cd4dSSage Weil 	printk("final dst extent is %llu~%llu\n", destoff, len);
787f46b5a66SChristoph Hellwig 
788f46b5a66SChristoph Hellwig 	/* do any pending delalloc/csum calc on src, one way or
789f46b5a66SChristoph Hellwig 	   another, and lock file content */
790f46b5a66SChristoph Hellwig 	while (1) {
79131840ae1SZheng Yan 		struct btrfs_ordered_extent *ordered;
792c5c9cd4dSSage Weil 		lock_extent(&BTRFS_I(src)->io_tree, off, off+len, GFP_NOFS);
793c5c9cd4dSSage Weil 		ordered = btrfs_lookup_first_ordered_extent(inode, off+len);
794ae01a0abSYan Zheng 		if (BTRFS_I(src)->delalloc_bytes == 0 && !ordered)
795f46b5a66SChristoph Hellwig 			break;
796c5c9cd4dSSage Weil 		unlock_extent(&BTRFS_I(src)->io_tree, off, off+len, GFP_NOFS);
797ae01a0abSYan Zheng 		if (ordered)
798ae01a0abSYan Zheng 			btrfs_put_ordered_extent(ordered);
799c5c9cd4dSSage Weil 		btrfs_wait_ordered_range(src, off, off+len);
800f46b5a66SChristoph Hellwig 	}
801f46b5a66SChristoph Hellwig 
802ae01a0abSYan Zheng 	trans = btrfs_start_transaction(root, 1);
803ae01a0abSYan Zheng 	BUG_ON(!trans);
804ae01a0abSYan Zheng 
805c5c9cd4dSSage Weil 	/* punch hole in destination first */
806c5c9cd4dSSage Weil 	btrfs_drop_extents(trans, root, inode, off, off+len, 0, &hint_byte);
807c5c9cd4dSSage Weil 
808c5c9cd4dSSage Weil 	/* clone data */
809f46b5a66SChristoph Hellwig 	key.objectid = src->i_ino;
810ae01a0abSYan Zheng 	key.type = BTRFS_EXTENT_DATA_KEY;
811ae01a0abSYan Zheng 	key.offset = 0;
812f46b5a66SChristoph Hellwig 
813f46b5a66SChristoph Hellwig 	while (1) {
814f46b5a66SChristoph Hellwig 		/*
815f46b5a66SChristoph Hellwig 		 * note the key will change type as we walk through the
816f46b5a66SChristoph Hellwig 		 * tree.
817f46b5a66SChristoph Hellwig 		 */
818f46b5a66SChristoph Hellwig 		ret = btrfs_search_slot(trans, root, &key, path, 0, 0);
819f46b5a66SChristoph Hellwig 		if (ret < 0)
820f46b5a66SChristoph Hellwig 			goto out;
821f46b5a66SChristoph Hellwig 
822ae01a0abSYan Zheng 		nritems = btrfs_header_nritems(path->nodes[0]);
823ae01a0abSYan Zheng 		if (path->slots[0] >= nritems) {
824f46b5a66SChristoph Hellwig 			ret = btrfs_next_leaf(root, path);
825f46b5a66SChristoph Hellwig 			if (ret < 0)
826f46b5a66SChristoph Hellwig 				goto out;
827f46b5a66SChristoph Hellwig 			if (ret > 0)
828f46b5a66SChristoph Hellwig 				break;
829ae01a0abSYan Zheng 			nritems = btrfs_header_nritems(path->nodes[0]);
830f46b5a66SChristoph Hellwig 		}
831f46b5a66SChristoph Hellwig 		leaf = path->nodes[0];
832f46b5a66SChristoph Hellwig 		slot = path->slots[0];
833f46b5a66SChristoph Hellwig 
834ae01a0abSYan Zheng 		btrfs_item_key_to_cpu(leaf, &key, slot);
835f46b5a66SChristoph Hellwig 		if (btrfs_key_type(&key) > BTRFS_CSUM_ITEM_KEY ||
836f46b5a66SChristoph Hellwig 		    key.objectid != src->i_ino)
837f46b5a66SChristoph Hellwig 			break;
838f46b5a66SChristoph Hellwig 
839c5c9cd4dSSage Weil 		if (btrfs_key_type(&key) == BTRFS_EXTENT_DATA_KEY) {
840c5c9cd4dSSage Weil 			struct btrfs_file_extent_item *extent;
841c5c9cd4dSSage Weil 			int type;
84231840ae1SZheng Yan 			u32 size;
84331840ae1SZheng Yan 			struct btrfs_key new_key;
844c5c9cd4dSSage Weil 			u64 disko = 0, diskl = 0;
845c5c9cd4dSSage Weil 			u64 datao = 0, datal = 0;
846c5c9cd4dSSage Weil 			u8 comp;
84731840ae1SZheng Yan 
84831840ae1SZheng Yan 			size = btrfs_item_size_nr(leaf, slot);
84931840ae1SZheng Yan 			read_extent_buffer(leaf, buf,
85031840ae1SZheng Yan 					   btrfs_item_ptr_offset(leaf, slot),
85131840ae1SZheng Yan 					   size);
852c5c9cd4dSSage Weil 
853c5c9cd4dSSage Weil 			extent = btrfs_item_ptr(leaf, slot,
854c5c9cd4dSSage Weil 						struct btrfs_file_extent_item);
855c5c9cd4dSSage Weil 			comp = btrfs_file_extent_compression(leaf, extent);
856c5c9cd4dSSage Weil 			type = btrfs_file_extent_type(leaf, extent);
857c5c9cd4dSSage Weil 			if (type == BTRFS_FILE_EXTENT_REG) {
858c5c9cd4dSSage Weil 				disko = btrfs_file_extent_disk_bytenr(leaf, extent);
859c5c9cd4dSSage Weil 				diskl = btrfs_file_extent_disk_num_bytes(leaf, extent);
860c5c9cd4dSSage Weil 				datao = btrfs_file_extent_offset(leaf, extent);
861c5c9cd4dSSage Weil 				datal = btrfs_file_extent_num_bytes(leaf, extent);
862c5c9cd4dSSage Weil 			} else if (type == BTRFS_FILE_EXTENT_INLINE) {
863c5c9cd4dSSage Weil 				/* take upper bound, may be compressed */
864c5c9cd4dSSage Weil 				datal = btrfs_file_extent_ram_bytes(leaf,
865c5c9cd4dSSage Weil 								    extent);
866c5c9cd4dSSage Weil 			}
86731840ae1SZheng Yan 			btrfs_release_path(root, path);
86831840ae1SZheng Yan 
869c5c9cd4dSSage Weil 			if (key.offset + datal < off ||
870c5c9cd4dSSage Weil 			    key.offset >= off+len)
871c5c9cd4dSSage Weil 				goto next;
872c5c9cd4dSSage Weil 
87331840ae1SZheng Yan 			memcpy(&new_key, &key, sizeof(new_key));
87431840ae1SZheng Yan 			new_key.objectid = inode->i_ino;
875c5c9cd4dSSage Weil 			new_key.offset = key.offset + destoff - off;
876c5c9cd4dSSage Weil 
877c5c9cd4dSSage Weil 			if (type == BTRFS_FILE_EXTENT_REG) {
87831840ae1SZheng Yan 				ret = btrfs_insert_empty_item(trans, root, path,
87931840ae1SZheng Yan 							      &new_key, size);
88031840ae1SZheng Yan 				if (ret)
88131840ae1SZheng Yan 					goto out;
88231840ae1SZheng Yan 
88331840ae1SZheng Yan 				leaf = path->nodes[0];
88431840ae1SZheng Yan 				slot = path->slots[0];
88531840ae1SZheng Yan 				write_extent_buffer(leaf, buf,
88631840ae1SZheng Yan 					    btrfs_item_ptr_offset(leaf, slot),
88731840ae1SZheng Yan 					    size);
888ae01a0abSYan Zheng 
889f46b5a66SChristoph Hellwig 				extent = btrfs_item_ptr(leaf, slot,
890f46b5a66SChristoph Hellwig 						struct btrfs_file_extent_item);
891c5c9cd4dSSage Weil 				printk("  orig disk %llu~%llu data %llu~%llu\n",
892c5c9cd4dSSage Weil 				       disko, diskl, datao, datal);
893c5c9cd4dSSage Weil 
894c5c9cd4dSSage Weil 				if (off > key.offset) {
895c5c9cd4dSSage Weil 					datao += off - key.offset;
896c5c9cd4dSSage Weil 					datal -= off - key.offset;
897c5c9cd4dSSage Weil 				}
898c5c9cd4dSSage Weil 				if (key.offset + datao + datal + key.offset >
899c5c9cd4dSSage Weil 				    off + len)
900c5c9cd4dSSage Weil 					datal = off + len - key.offset - datao;
901c5c9cd4dSSage Weil 				/* disko == 0 means it's a hole */
902c5c9cd4dSSage Weil 				if (!disko)
903c5c9cd4dSSage Weil 					datao = 0;
904c5c9cd4dSSage Weil 				printk(" final disk %llu~%llu data %llu~%llu\n",
905c5c9cd4dSSage Weil 				       disko, diskl, datao, datal);
906c5c9cd4dSSage Weil 
907c5c9cd4dSSage Weil 				btrfs_set_file_extent_offset(leaf, extent,
908c5c9cd4dSSage Weil 							     datao);
909c5c9cd4dSSage Weil 				btrfs_set_file_extent_num_bytes(leaf, extent,
910c5c9cd4dSSage Weil 								datal);
911c5c9cd4dSSage Weil 				if (disko) {
912c5c9cd4dSSage Weil 					inode_add_bytes(inode, datal);
913ae01a0abSYan Zheng 					ret = btrfs_inc_extent_ref(trans, root,
914c5c9cd4dSSage Weil 						   disko, diskl, leaf->start,
915f46b5a66SChristoph Hellwig 						   root->root_key.objectid,
916f46b5a66SChristoph Hellwig 						   trans->transid,
9173bb1a1bcSYan Zheng 						   inode->i_ino);
918ae01a0abSYan Zheng 					BUG_ON(ret);
919f46b5a66SChristoph Hellwig 				}
920c5c9cd4dSSage Weil 			} else if (type == BTRFS_FILE_EXTENT_INLINE) {
921c5c9cd4dSSage Weil 				u64 skip = 0;
922c5c9cd4dSSage Weil 				u64 trim = 0;
923c5c9cd4dSSage Weil 				if (off > key.offset) {
924c5c9cd4dSSage Weil 					skip = off - key.offset;
925c5c9cd4dSSage Weil 					new_key.offset += skip;
92631840ae1SZheng Yan 				}
927c5c9cd4dSSage Weil 				if (key.offset + datal > off+len)
928c5c9cd4dSSage Weil 					trim = key.offset + datal - (off+len);
929c5c9cd4dSSage Weil 				printk("len %lld skip %lld trim %lld\n",
930c5c9cd4dSSage Weil 				       datal, skip, trim);
931c5c9cd4dSSage Weil 				if (comp && (skip || trim)) {
932c5c9cd4dSSage Weil 					printk("btrfs clone_range can't split compressed inline extents yet\n");
933c5c9cd4dSSage Weil 					ret = -EINVAL;
934c5c9cd4dSSage Weil 					goto out;
93531840ae1SZheng Yan 				}
936c5c9cd4dSSage Weil 				size -= skip + trim;
937c5c9cd4dSSage Weil 				datal -= skip + trim;
938c5c9cd4dSSage Weil 				ret = btrfs_insert_empty_item(trans, root, path,
939c5c9cd4dSSage Weil 							      &new_key, size);
940c5c9cd4dSSage Weil 				if (ret)
941c5c9cd4dSSage Weil 					goto out;
942c5c9cd4dSSage Weil 
943c5c9cd4dSSage Weil 				if (skip) {
944c5c9cd4dSSage Weil 					u32 start = btrfs_file_extent_calc_inline_size(0);
945c5c9cd4dSSage Weil 					memmove(buf+start, buf+start+skip,
946c5c9cd4dSSage Weil 						datal);
947c5c9cd4dSSage Weil 				}
948c5c9cd4dSSage Weil 
949c5c9cd4dSSage Weil 				leaf = path->nodes[0];
950c5c9cd4dSSage Weil 				slot = path->slots[0];
951c5c9cd4dSSage Weil 				write_extent_buffer(leaf, buf,
952c5c9cd4dSSage Weil 					    btrfs_item_ptr_offset(leaf, slot),
953c5c9cd4dSSage Weil 					    size);
954c5c9cd4dSSage Weil 				inode_add_bytes(inode, datal);
955c5c9cd4dSSage Weil 			}
956c5c9cd4dSSage Weil 
957c5c9cd4dSSage Weil 			btrfs_mark_buffer_dirty(leaf);
958c5c9cd4dSSage Weil 		}
959c5c9cd4dSSage Weil 
960c5c9cd4dSSage Weil 		if (btrfs_key_type(&key) == BTRFS_CSUM_ITEM_KEY) {
961c5c9cd4dSSage Weil 			u32 size;
962c5c9cd4dSSage Weil 			struct btrfs_key new_key;
963c5c9cd4dSSage Weil 			u64 coverslen;
964c5c9cd4dSSage Weil 			int coff, clen;
965c5c9cd4dSSage Weil 
966c5c9cd4dSSage Weil 			size = btrfs_item_size_nr(leaf, slot);
967c5c9cd4dSSage Weil 			coverslen = (size / BTRFS_CRC32_SIZE) <<
968c5c9cd4dSSage Weil 				root->fs_info->sb->s_blocksize_bits;
969c5c9cd4dSSage Weil 			printk("csums for %llu~%llu\n",
970c5c9cd4dSSage Weil 			       key.offset, coverslen);
971c5c9cd4dSSage Weil 			if (key.offset + coverslen < off ||
972c5c9cd4dSSage Weil 			    key.offset >= off+len)
973c5c9cd4dSSage Weil 				goto next;
974c5c9cd4dSSage Weil 
975c5c9cd4dSSage Weil 			read_extent_buffer(leaf, buf,
976c5c9cd4dSSage Weil 					   btrfs_item_ptr_offset(leaf, slot),
977c5c9cd4dSSage Weil 					   size);
978c5c9cd4dSSage Weil 			btrfs_release_path(root, path);
979c5c9cd4dSSage Weil 
980c5c9cd4dSSage Weil 			coff = 0;
981c5c9cd4dSSage Weil 			if (off > key.offset)
982c5c9cd4dSSage Weil 				coff = ((off - key.offset) >>
983c5c9cd4dSSage Weil 					root->fs_info->sb->s_blocksize_bits) *
984c5c9cd4dSSage Weil 					BTRFS_CRC32_SIZE;
985c5c9cd4dSSage Weil 			clen = size - coff;
986c5c9cd4dSSage Weil 			if (key.offset + coverslen > off+len)
987c5c9cd4dSSage Weil 				clen -= ((key.offset+coverslen-off-len) >>
988c5c9cd4dSSage Weil 					 root->fs_info->sb->s_blocksize_bits) *
989c5c9cd4dSSage Weil 					BTRFS_CRC32_SIZE;
990c5c9cd4dSSage Weil 			printk(" will dup %d~%d of %d\n",
991c5c9cd4dSSage Weil 			       coff, clen, size);
992c5c9cd4dSSage Weil 
993c5c9cd4dSSage Weil 			memcpy(&new_key, &key, sizeof(new_key));
994c5c9cd4dSSage Weil 			new_key.objectid = inode->i_ino;
995c5c9cd4dSSage Weil 			new_key.offset = key.offset + destoff - off;
996c5c9cd4dSSage Weil 
997c5c9cd4dSSage Weil 			ret = btrfs_insert_empty_item(trans, root, path,
998c5c9cd4dSSage Weil 						      &new_key, clen);
999c5c9cd4dSSage Weil 			if (ret)
1000c5c9cd4dSSage Weil 				goto out;
1001c5c9cd4dSSage Weil 
1002c5c9cd4dSSage Weil 			leaf = path->nodes[0];
1003c5c9cd4dSSage Weil 			slot = path->slots[0];
1004c5c9cd4dSSage Weil 			write_extent_buffer(leaf, buf + coff,
1005c5c9cd4dSSage Weil 					    btrfs_item_ptr_offset(leaf, slot),
1006c5c9cd4dSSage Weil 					    clen);
1007c5c9cd4dSSage Weil 			btrfs_mark_buffer_dirty(leaf);
1008c5c9cd4dSSage Weil 		}
1009c5c9cd4dSSage Weil 
1010c5c9cd4dSSage Weil 	next:
101131840ae1SZheng Yan 		btrfs_release_path(root, path);
1012ae01a0abSYan Zheng 		key.offset++;
1013ae01a0abSYan Zheng 	}
1014f46b5a66SChristoph Hellwig 	ret = 0;
1015f46b5a66SChristoph Hellwig out:
1016ae01a0abSYan Zheng 	btrfs_release_path(root, path);
1017ae01a0abSYan Zheng 	if (ret == 0) {
1018ae01a0abSYan Zheng 		inode->i_mtime = inode->i_ctime = CURRENT_TIME;
1019c5c9cd4dSSage Weil 		if (destoff + olen > inode->i_size)
1020c5c9cd4dSSage Weil 			btrfs_i_size_write(inode, destoff + olen);
1021ae01a0abSYan Zheng 		BTRFS_I(inode)->flags = BTRFS_I(src)->flags;
1022ae01a0abSYan Zheng 		ret = btrfs_update_inode(trans, root, inode);
1023ae01a0abSYan Zheng 	}
1024f46b5a66SChristoph Hellwig 	btrfs_end_transaction(trans, root);
1025c5c9cd4dSSage Weil 	unlock_extent(&BTRFS_I(src)->io_tree, off, off+len, GFP_NOFS);
1026ae01a0abSYan Zheng 	if (ret)
1027ae01a0abSYan Zheng 		vmtruncate(inode, 0);
1028f46b5a66SChristoph Hellwig out_unlock:
1029f46b5a66SChristoph Hellwig 	mutex_unlock(&src->i_mutex);
1030f46b5a66SChristoph Hellwig 	mutex_unlock(&inode->i_mutex);
1031ae01a0abSYan Zheng 	vfree(buf);
1032ae01a0abSYan Zheng 	btrfs_free_path(path);
1033f46b5a66SChristoph Hellwig out_fput:
1034f46b5a66SChristoph Hellwig 	fput(src_file);
1035f46b5a66SChristoph Hellwig 	return ret;
1036f46b5a66SChristoph Hellwig }
1037f46b5a66SChristoph Hellwig 
1038b2950863SChristoph Hellwig static long btrfs_ioctl_clone_range(struct file *file, unsigned long argptr)
1039c5c9cd4dSSage Weil {
1040c5c9cd4dSSage Weil 	struct btrfs_ioctl_clone_range_args args;
1041c5c9cd4dSSage Weil 
1042c5c9cd4dSSage Weil 	if (copy_from_user(&args, (void *)argptr, sizeof(args)))
1043c5c9cd4dSSage Weil 		return -EFAULT;
1044c5c9cd4dSSage Weil 	return btrfs_ioctl_clone(file, args.src_fd, args.src_offset,
1045c5c9cd4dSSage Weil 				 args.src_length, args.dest_offset);
1046c5c9cd4dSSage Weil }
1047c5c9cd4dSSage Weil 
1048f46b5a66SChristoph Hellwig /*
1049f46b5a66SChristoph Hellwig  * there are many ways the trans_start and trans_end ioctls can lead
1050f46b5a66SChristoph Hellwig  * to deadlocks.  They should only be used by applications that
1051f46b5a66SChristoph Hellwig  * basically own the machine, and have a very in depth understanding
1052f46b5a66SChristoph Hellwig  * of all the possible deadlocks and enospc problems.
1053f46b5a66SChristoph Hellwig  */
1054b2950863SChristoph Hellwig static long btrfs_ioctl_trans_start(struct file *file)
1055f46b5a66SChristoph Hellwig {
1056f46b5a66SChristoph Hellwig 	struct inode *inode = fdentry(file)->d_inode;
1057f46b5a66SChristoph Hellwig 	struct btrfs_root *root = BTRFS_I(inode)->root;
1058f46b5a66SChristoph Hellwig 	struct btrfs_trans_handle *trans;
1059f46b5a66SChristoph Hellwig 	int ret = 0;
1060f46b5a66SChristoph Hellwig 
1061df5b5520SChristoph Hellwig 	if (!capable(CAP_SYS_ADMIN))
1062df5b5520SChristoph Hellwig 		return -EPERM;
1063df5b5520SChristoph Hellwig 
1064f46b5a66SChristoph Hellwig 	if (file->private_data) {
1065f46b5a66SChristoph Hellwig 		ret = -EINPROGRESS;
1066f46b5a66SChristoph Hellwig 		goto out;
1067f46b5a66SChristoph Hellwig 	}
10689ca9ee09SSage Weil 
1069c146afadSYan Zheng 	ret = mnt_want_write(file->f_path.mnt);
1070c146afadSYan Zheng 	if (ret)
1071c146afadSYan Zheng 		goto out;
1072c146afadSYan Zheng 
10739ca9ee09SSage Weil 	mutex_lock(&root->fs_info->trans_mutex);
10749ca9ee09SSage Weil 	root->fs_info->open_ioctl_trans++;
10759ca9ee09SSage Weil 	mutex_unlock(&root->fs_info->trans_mutex);
10769ca9ee09SSage Weil 
10779ca9ee09SSage Weil 	trans = btrfs_start_ioctl_transaction(root, 0);
1078f46b5a66SChristoph Hellwig 	if (trans)
1079f46b5a66SChristoph Hellwig 		file->private_data = trans;
1080f46b5a66SChristoph Hellwig 	else
1081f46b5a66SChristoph Hellwig 		ret = -ENOMEM;
1082f46b5a66SChristoph Hellwig 	/*printk(KERN_INFO "btrfs_ioctl_trans_start on %p\n", file);*/
1083f46b5a66SChristoph Hellwig out:
1084f46b5a66SChristoph Hellwig 	return ret;
1085f46b5a66SChristoph Hellwig }
1086f46b5a66SChristoph Hellwig 
1087f46b5a66SChristoph Hellwig /*
1088f46b5a66SChristoph Hellwig  * there are many ways the trans_start and trans_end ioctls can lead
1089f46b5a66SChristoph Hellwig  * to deadlocks.  They should only be used by applications that
1090f46b5a66SChristoph Hellwig  * basically own the machine, and have a very in depth understanding
1091f46b5a66SChristoph Hellwig  * of all the possible deadlocks and enospc problems.
1092f46b5a66SChristoph Hellwig  */
1093f46b5a66SChristoph Hellwig long btrfs_ioctl_trans_end(struct file *file)
1094f46b5a66SChristoph Hellwig {
1095f46b5a66SChristoph Hellwig 	struct inode *inode = fdentry(file)->d_inode;
1096f46b5a66SChristoph Hellwig 	struct btrfs_root *root = BTRFS_I(inode)->root;
1097f46b5a66SChristoph Hellwig 	struct btrfs_trans_handle *trans;
1098f46b5a66SChristoph Hellwig 	int ret = 0;
1099f46b5a66SChristoph Hellwig 
1100f46b5a66SChristoph Hellwig 	trans = file->private_data;
1101f46b5a66SChristoph Hellwig 	if (!trans) {
1102f46b5a66SChristoph Hellwig 		ret = -EINVAL;
1103f46b5a66SChristoph Hellwig 		goto out;
1104f46b5a66SChristoph Hellwig 	}
1105f46b5a66SChristoph Hellwig 	btrfs_end_transaction(trans, root);
1106b214107eSChristoph Hellwig 	file->private_data = NULL;
11079ca9ee09SSage Weil 
11089ca9ee09SSage Weil 	mutex_lock(&root->fs_info->trans_mutex);
11099ca9ee09SSage Weil 	root->fs_info->open_ioctl_trans--;
11109ca9ee09SSage Weil 	mutex_unlock(&root->fs_info->trans_mutex);
11119ca9ee09SSage Weil 
1112f46b5a66SChristoph Hellwig out:
1113f46b5a66SChristoph Hellwig 	return ret;
1114f46b5a66SChristoph Hellwig }
1115f46b5a66SChristoph Hellwig 
1116f46b5a66SChristoph Hellwig long btrfs_ioctl(struct file *file, unsigned int
1117f46b5a66SChristoph Hellwig 		cmd, unsigned long arg)
1118f46b5a66SChristoph Hellwig {
1119f46b5a66SChristoph Hellwig 	struct btrfs_root *root = BTRFS_I(fdentry(file)->d_inode)->root;
1120*4bcabaa3SChristoph Hellwig 	void __user *argp = (void __user *)arg;
1121f46b5a66SChristoph Hellwig 
1122f46b5a66SChristoph Hellwig 	switch (cmd) {
1123f46b5a66SChristoph Hellwig 	case BTRFS_IOC_SNAP_CREATE:
1124*4bcabaa3SChristoph Hellwig 		return btrfs_ioctl_snap_create(file, argp, 0);
11253de4586cSChris Mason 	case BTRFS_IOC_SUBVOL_CREATE:
1126*4bcabaa3SChristoph Hellwig 		return btrfs_ioctl_snap_create(file, argp, 1);
1127f46b5a66SChristoph Hellwig 	case BTRFS_IOC_DEFRAG:
1128f46b5a66SChristoph Hellwig 		return btrfs_ioctl_defrag(file);
1129f46b5a66SChristoph Hellwig 	case BTRFS_IOC_RESIZE:
1130*4bcabaa3SChristoph Hellwig 		return btrfs_ioctl_resize(root, argp);
1131f46b5a66SChristoph Hellwig 	case BTRFS_IOC_ADD_DEV:
1132*4bcabaa3SChristoph Hellwig 		return btrfs_ioctl_add_dev(root, argp);
1133f46b5a66SChristoph Hellwig 	case BTRFS_IOC_RM_DEV:
1134*4bcabaa3SChristoph Hellwig 		return btrfs_ioctl_rm_dev(root, argp);
1135f46b5a66SChristoph Hellwig 	case BTRFS_IOC_BALANCE:
1136f46b5a66SChristoph Hellwig 		return btrfs_balance(root->fs_info->dev_root);
1137f46b5a66SChristoph Hellwig 	case BTRFS_IOC_CLONE:
1138c5c9cd4dSSage Weil 		return btrfs_ioctl_clone(file, arg, 0, 0, 0);
1139c5c9cd4dSSage Weil 	case BTRFS_IOC_CLONE_RANGE:
1140c5c9cd4dSSage Weil 		return btrfs_ioctl_clone_range(file, arg);
1141f46b5a66SChristoph Hellwig 	case BTRFS_IOC_TRANS_START:
1142f46b5a66SChristoph Hellwig 		return btrfs_ioctl_trans_start(file);
1143f46b5a66SChristoph Hellwig 	case BTRFS_IOC_TRANS_END:
1144f46b5a66SChristoph Hellwig 		return btrfs_ioctl_trans_end(file);
1145f46b5a66SChristoph Hellwig 	case BTRFS_IOC_SYNC:
1146f46b5a66SChristoph Hellwig 		btrfs_sync_fs(file->f_dentry->d_sb, 1);
1147f46b5a66SChristoph Hellwig 		return 0;
1148f46b5a66SChristoph Hellwig 	}
1149f46b5a66SChristoph Hellwig 
1150f46b5a66SChristoph Hellwig 	return -ENOTTY;
1151f46b5a66SChristoph Hellwig }
1152