xref: /openbmc/linux/fs/btrfs/ioctl.c (revision 9785dbdf265ddc47d5c88267d89a97648c0dc14b)
1 /*
2  * Copyright (C) 2007 Oracle.  All rights reserved.
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public
6  * License v2 as published by the Free Software Foundation.
7  *
8  * This program is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11  * General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public
14  * License along with this program; if not, write to the
15  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
16  * Boston, MA 021110-1307, USA.
17  */
18 
19 #include <linux/kernel.h>
20 #include <linux/bio.h>
21 #include <linux/buffer_head.h>
22 #include <linux/file.h>
23 #include <linux/fs.h>
24 #include <linux/fsnotify.h>
25 #include <linux/pagemap.h>
26 #include <linux/highmem.h>
27 #include <linux/time.h>
28 #include <linux/init.h>
29 #include <linux/string.h>
30 #include <linux/backing-dev.h>
31 #include <linux/mount.h>
32 #include <linux/mpage.h>
33 #include <linux/namei.h>
34 #include <linux/swap.h>
35 #include <linux/writeback.h>
36 #include <linux/statfs.h>
37 #include <linux/compat.h>
38 #include <linux/bit_spinlock.h>
39 #include <linux/security.h>
40 #include <linux/xattr.h>
41 #include <linux/vmalloc.h>
42 #include <linux/slab.h>
43 #include <linux/blkdev.h>
44 #include "compat.h"
45 #include "ctree.h"
46 #include "disk-io.h"
47 #include "transaction.h"
48 #include "btrfs_inode.h"
49 #include "ioctl.h"
50 #include "print-tree.h"
51 #include "volumes.h"
52 #include "locking.h"
53 #include "inode-map.h"
54 #include "backref.h"
55 
56 /* Mask out flags that are inappropriate for the given type of inode. */
57 static inline __u32 btrfs_mask_flags(umode_t mode, __u32 flags)
58 {
59 	if (S_ISDIR(mode))
60 		return flags;
61 	else if (S_ISREG(mode))
62 		return flags & ~FS_DIRSYNC_FL;
63 	else
64 		return flags & (FS_NODUMP_FL | FS_NOATIME_FL);
65 }
66 
67 /*
68  * Export inode flags to the format expected by the FS_IOC_GETFLAGS ioctl.
69  */
70 static unsigned int btrfs_flags_to_ioctl(unsigned int flags)
71 {
72 	unsigned int iflags = 0;
73 
74 	if (flags & BTRFS_INODE_SYNC)
75 		iflags |= FS_SYNC_FL;
76 	if (flags & BTRFS_INODE_IMMUTABLE)
77 		iflags |= FS_IMMUTABLE_FL;
78 	if (flags & BTRFS_INODE_APPEND)
79 		iflags |= FS_APPEND_FL;
80 	if (flags & BTRFS_INODE_NODUMP)
81 		iflags |= FS_NODUMP_FL;
82 	if (flags & BTRFS_INODE_NOATIME)
83 		iflags |= FS_NOATIME_FL;
84 	if (flags & BTRFS_INODE_DIRSYNC)
85 		iflags |= FS_DIRSYNC_FL;
86 	if (flags & BTRFS_INODE_NODATACOW)
87 		iflags |= FS_NOCOW_FL;
88 
89 	if ((flags & BTRFS_INODE_COMPRESS) && !(flags & BTRFS_INODE_NOCOMPRESS))
90 		iflags |= FS_COMPR_FL;
91 	else if (flags & BTRFS_INODE_NOCOMPRESS)
92 		iflags |= FS_NOCOMP_FL;
93 
94 	return iflags;
95 }
96 
97 /*
98  * Update inode->i_flags based on the btrfs internal flags.
99  */
100 void btrfs_update_iflags(struct inode *inode)
101 {
102 	struct btrfs_inode *ip = BTRFS_I(inode);
103 
104 	inode->i_flags &= ~(S_SYNC|S_APPEND|S_IMMUTABLE|S_NOATIME|S_DIRSYNC);
105 
106 	if (ip->flags & BTRFS_INODE_SYNC)
107 		inode->i_flags |= S_SYNC;
108 	if (ip->flags & BTRFS_INODE_IMMUTABLE)
109 		inode->i_flags |= S_IMMUTABLE;
110 	if (ip->flags & BTRFS_INODE_APPEND)
111 		inode->i_flags |= S_APPEND;
112 	if (ip->flags & BTRFS_INODE_NOATIME)
113 		inode->i_flags |= S_NOATIME;
114 	if (ip->flags & BTRFS_INODE_DIRSYNC)
115 		inode->i_flags |= S_DIRSYNC;
116 }
117 
118 /*
119  * Inherit flags from the parent inode.
120  *
121  * Currently only the compression flags and the cow flags are inherited.
122  */
123 void btrfs_inherit_iflags(struct inode *inode, struct inode *dir)
124 {
125 	unsigned int flags;
126 
127 	if (!dir)
128 		return;
129 
130 	flags = BTRFS_I(dir)->flags;
131 
132 	if (flags & BTRFS_INODE_NOCOMPRESS) {
133 		BTRFS_I(inode)->flags &= ~BTRFS_INODE_COMPRESS;
134 		BTRFS_I(inode)->flags |= BTRFS_INODE_NOCOMPRESS;
135 	} else if (flags & BTRFS_INODE_COMPRESS) {
136 		BTRFS_I(inode)->flags &= ~BTRFS_INODE_NOCOMPRESS;
137 		BTRFS_I(inode)->flags |= BTRFS_INODE_COMPRESS;
138 	}
139 
140 	if (flags & BTRFS_INODE_NODATACOW)
141 		BTRFS_I(inode)->flags |= BTRFS_INODE_NODATACOW;
142 
143 	btrfs_update_iflags(inode);
144 }
145 
146 static int btrfs_ioctl_getflags(struct file *file, void __user *arg)
147 {
148 	struct btrfs_inode *ip = BTRFS_I(file->f_path.dentry->d_inode);
149 	unsigned int flags = btrfs_flags_to_ioctl(ip->flags);
150 
151 	if (copy_to_user(arg, &flags, sizeof(flags)))
152 		return -EFAULT;
153 	return 0;
154 }
155 
156 static int check_flags(unsigned int flags)
157 {
158 	if (flags & ~(FS_IMMUTABLE_FL | FS_APPEND_FL | \
159 		      FS_NOATIME_FL | FS_NODUMP_FL | \
160 		      FS_SYNC_FL | FS_DIRSYNC_FL | \
161 		      FS_NOCOMP_FL | FS_COMPR_FL |
162 		      FS_NOCOW_FL))
163 		return -EOPNOTSUPP;
164 
165 	if ((flags & FS_NOCOMP_FL) && (flags & FS_COMPR_FL))
166 		return -EINVAL;
167 
168 	return 0;
169 }
170 
171 static int btrfs_ioctl_setflags(struct file *file, void __user *arg)
172 {
173 	struct inode *inode = file->f_path.dentry->d_inode;
174 	struct btrfs_inode *ip = BTRFS_I(inode);
175 	struct btrfs_root *root = ip->root;
176 	struct btrfs_trans_handle *trans;
177 	unsigned int flags, oldflags;
178 	int ret;
179 	u64 ip_oldflags;
180 	unsigned int i_oldflags;
181 
182 	if (btrfs_root_readonly(root))
183 		return -EROFS;
184 
185 	if (copy_from_user(&flags, arg, sizeof(flags)))
186 		return -EFAULT;
187 
188 	ret = check_flags(flags);
189 	if (ret)
190 		return ret;
191 
192 	if (!inode_owner_or_capable(inode))
193 		return -EACCES;
194 
195 	mutex_lock(&inode->i_mutex);
196 
197 	ip_oldflags = ip->flags;
198 	i_oldflags = inode->i_flags;
199 
200 	flags = btrfs_mask_flags(inode->i_mode, flags);
201 	oldflags = btrfs_flags_to_ioctl(ip->flags);
202 	if ((flags ^ oldflags) & (FS_APPEND_FL | FS_IMMUTABLE_FL)) {
203 		if (!capable(CAP_LINUX_IMMUTABLE)) {
204 			ret = -EPERM;
205 			goto out_unlock;
206 		}
207 	}
208 
209 	ret = mnt_want_write(file->f_path.mnt);
210 	if (ret)
211 		goto out_unlock;
212 
213 	if (flags & FS_SYNC_FL)
214 		ip->flags |= BTRFS_INODE_SYNC;
215 	else
216 		ip->flags &= ~BTRFS_INODE_SYNC;
217 	if (flags & FS_IMMUTABLE_FL)
218 		ip->flags |= BTRFS_INODE_IMMUTABLE;
219 	else
220 		ip->flags &= ~BTRFS_INODE_IMMUTABLE;
221 	if (flags & FS_APPEND_FL)
222 		ip->flags |= BTRFS_INODE_APPEND;
223 	else
224 		ip->flags &= ~BTRFS_INODE_APPEND;
225 	if (flags & FS_NODUMP_FL)
226 		ip->flags |= BTRFS_INODE_NODUMP;
227 	else
228 		ip->flags &= ~BTRFS_INODE_NODUMP;
229 	if (flags & FS_NOATIME_FL)
230 		ip->flags |= BTRFS_INODE_NOATIME;
231 	else
232 		ip->flags &= ~BTRFS_INODE_NOATIME;
233 	if (flags & FS_DIRSYNC_FL)
234 		ip->flags |= BTRFS_INODE_DIRSYNC;
235 	else
236 		ip->flags &= ~BTRFS_INODE_DIRSYNC;
237 	if (flags & FS_NOCOW_FL)
238 		ip->flags |= BTRFS_INODE_NODATACOW;
239 	else
240 		ip->flags &= ~BTRFS_INODE_NODATACOW;
241 
242 	/*
243 	 * The COMPRESS flag can only be changed by users, while the NOCOMPRESS
244 	 * flag may be changed automatically if compression code won't make
245 	 * things smaller.
246 	 */
247 	if (flags & FS_NOCOMP_FL) {
248 		ip->flags &= ~BTRFS_INODE_COMPRESS;
249 		ip->flags |= BTRFS_INODE_NOCOMPRESS;
250 	} else if (flags & FS_COMPR_FL) {
251 		ip->flags |= BTRFS_INODE_COMPRESS;
252 		ip->flags &= ~BTRFS_INODE_NOCOMPRESS;
253 	} else {
254 		ip->flags &= ~(BTRFS_INODE_COMPRESS | BTRFS_INODE_NOCOMPRESS);
255 	}
256 
257 	trans = btrfs_start_transaction(root, 1);
258 	if (IS_ERR(trans)) {
259 		ret = PTR_ERR(trans);
260 		goto out_drop;
261 	}
262 
263 	btrfs_update_iflags(inode);
264 	inode->i_ctime = CURRENT_TIME;
265 	ret = btrfs_update_inode(trans, root, inode);
266 
267 	btrfs_end_transaction(trans, root);
268  out_drop:
269 	if (ret) {
270 		ip->flags = ip_oldflags;
271 		inode->i_flags = i_oldflags;
272 	}
273 
274 	mnt_drop_write(file->f_path.mnt);
275  out_unlock:
276 	mutex_unlock(&inode->i_mutex);
277 	return ret;
278 }
279 
280 static int btrfs_ioctl_getversion(struct file *file, int __user *arg)
281 {
282 	struct inode *inode = file->f_path.dentry->d_inode;
283 
284 	return put_user(inode->i_generation, arg);
285 }
286 
287 static noinline int btrfs_ioctl_fitrim(struct file *file, void __user *arg)
288 {
289 	struct btrfs_root *root = fdentry(file)->d_sb->s_fs_info;
290 	struct btrfs_fs_info *fs_info = root->fs_info;
291 	struct btrfs_device *device;
292 	struct request_queue *q;
293 	struct fstrim_range range;
294 	u64 minlen = ULLONG_MAX;
295 	u64 num_devices = 0;
296 	u64 total_bytes = btrfs_super_total_bytes(root->fs_info->super_copy);
297 	int ret;
298 
299 	if (!capable(CAP_SYS_ADMIN))
300 		return -EPERM;
301 
302 	rcu_read_lock();
303 	list_for_each_entry_rcu(device, &fs_info->fs_devices->devices,
304 				dev_list) {
305 		if (!device->bdev)
306 			continue;
307 		q = bdev_get_queue(device->bdev);
308 		if (blk_queue_discard(q)) {
309 			num_devices++;
310 			minlen = min((u64)q->limits.discard_granularity,
311 				     minlen);
312 		}
313 	}
314 	rcu_read_unlock();
315 
316 	if (!num_devices)
317 		return -EOPNOTSUPP;
318 	if (copy_from_user(&range, arg, sizeof(range)))
319 		return -EFAULT;
320 	if (range.start > total_bytes)
321 		return -EINVAL;
322 
323 	range.len = min(range.len, total_bytes - range.start);
324 	range.minlen = max(range.minlen, minlen);
325 	ret = btrfs_trim_fs(root, &range);
326 	if (ret < 0)
327 		return ret;
328 
329 	if (copy_to_user(arg, &range, sizeof(range)))
330 		return -EFAULT;
331 
332 	return 0;
333 }
334 
335 static noinline int create_subvol(struct btrfs_root *root,
336 				  struct dentry *dentry,
337 				  char *name, int namelen,
338 				  u64 *async_transid)
339 {
340 	struct btrfs_trans_handle *trans;
341 	struct btrfs_key key;
342 	struct btrfs_root_item root_item;
343 	struct btrfs_inode_item *inode_item;
344 	struct extent_buffer *leaf;
345 	struct btrfs_root *new_root;
346 	struct dentry *parent = dentry->d_parent;
347 	struct inode *dir;
348 	int ret;
349 	int err;
350 	u64 objectid;
351 	u64 new_dirid = BTRFS_FIRST_FREE_OBJECTID;
352 	u64 index = 0;
353 
354 	ret = btrfs_find_free_objectid(root->fs_info->tree_root, &objectid);
355 	if (ret)
356 		return ret;
357 
358 	dir = parent->d_inode;
359 
360 	/*
361 	 * 1 - inode item
362 	 * 2 - refs
363 	 * 1 - root item
364 	 * 2 - dir items
365 	 */
366 	trans = btrfs_start_transaction(root, 6);
367 	if (IS_ERR(trans))
368 		return PTR_ERR(trans);
369 
370 	leaf = btrfs_alloc_free_block(trans, root, root->leafsize,
371 				      0, objectid, NULL, 0, 0, 0, 0);
372 	if (IS_ERR(leaf)) {
373 		ret = PTR_ERR(leaf);
374 		goto fail;
375 	}
376 
377 	memset_extent_buffer(leaf, 0, 0, sizeof(struct btrfs_header));
378 	btrfs_set_header_bytenr(leaf, leaf->start);
379 	btrfs_set_header_generation(leaf, trans->transid);
380 	btrfs_set_header_backref_rev(leaf, BTRFS_MIXED_BACKREF_REV);
381 	btrfs_set_header_owner(leaf, objectid);
382 
383 	write_extent_buffer(leaf, root->fs_info->fsid,
384 			    (unsigned long)btrfs_header_fsid(leaf),
385 			    BTRFS_FSID_SIZE);
386 	write_extent_buffer(leaf, root->fs_info->chunk_tree_uuid,
387 			    (unsigned long)btrfs_header_chunk_tree_uuid(leaf),
388 			    BTRFS_UUID_SIZE);
389 	btrfs_mark_buffer_dirty(leaf);
390 
391 	inode_item = &root_item.inode;
392 	memset(inode_item, 0, sizeof(*inode_item));
393 	inode_item->generation = cpu_to_le64(1);
394 	inode_item->size = cpu_to_le64(3);
395 	inode_item->nlink = cpu_to_le32(1);
396 	inode_item->nbytes = cpu_to_le64(root->leafsize);
397 	inode_item->mode = cpu_to_le32(S_IFDIR | 0755);
398 
399 	root_item.flags = 0;
400 	root_item.byte_limit = 0;
401 	inode_item->flags = cpu_to_le64(BTRFS_INODE_ROOT_ITEM_INIT);
402 
403 	btrfs_set_root_bytenr(&root_item, leaf->start);
404 	btrfs_set_root_generation(&root_item, trans->transid);
405 	btrfs_set_root_level(&root_item, 0);
406 	btrfs_set_root_refs(&root_item, 1);
407 	btrfs_set_root_used(&root_item, leaf->len);
408 	btrfs_set_root_last_snapshot(&root_item, 0);
409 
410 	memset(&root_item.drop_progress, 0, sizeof(root_item.drop_progress));
411 	root_item.drop_level = 0;
412 
413 	btrfs_tree_unlock(leaf);
414 	free_extent_buffer(leaf);
415 	leaf = NULL;
416 
417 	btrfs_set_root_dirid(&root_item, new_dirid);
418 
419 	key.objectid = objectid;
420 	key.offset = 0;
421 	btrfs_set_key_type(&key, BTRFS_ROOT_ITEM_KEY);
422 	ret = btrfs_insert_root(trans, root->fs_info->tree_root, &key,
423 				&root_item);
424 	if (ret)
425 		goto fail;
426 
427 	key.offset = (u64)-1;
428 	new_root = btrfs_read_fs_root_no_name(root->fs_info, &key);
429 	BUG_ON(IS_ERR(new_root));
430 
431 	btrfs_record_root_in_trans(trans, new_root);
432 
433 	ret = btrfs_create_subvol_root(trans, new_root, new_dirid);
434 	/*
435 	 * insert the directory item
436 	 */
437 	ret = btrfs_set_inode_index(dir, &index);
438 	BUG_ON(ret);
439 
440 	ret = btrfs_insert_dir_item(trans, root,
441 				    name, namelen, dir, &key,
442 				    BTRFS_FT_DIR, index);
443 	if (ret)
444 		goto fail;
445 
446 	btrfs_i_size_write(dir, dir->i_size + namelen * 2);
447 	ret = btrfs_update_inode(trans, root, dir);
448 	BUG_ON(ret);
449 
450 	ret = btrfs_add_root_ref(trans, root->fs_info->tree_root,
451 				 objectid, root->root_key.objectid,
452 				 btrfs_ino(dir), index, name, namelen);
453 
454 	BUG_ON(ret);
455 
456 	d_instantiate(dentry, btrfs_lookup_dentry(dir, dentry));
457 fail:
458 	if (async_transid) {
459 		*async_transid = trans->transid;
460 		err = btrfs_commit_transaction_async(trans, root, 1);
461 	} else {
462 		err = btrfs_commit_transaction(trans, root);
463 	}
464 	if (err && !ret)
465 		ret = err;
466 	return ret;
467 }
468 
469 static int create_snapshot(struct btrfs_root *root, struct dentry *dentry,
470 			   char *name, int namelen, u64 *async_transid,
471 			   bool readonly)
472 {
473 	struct inode *inode;
474 	struct btrfs_pending_snapshot *pending_snapshot;
475 	struct btrfs_trans_handle *trans;
476 	int ret;
477 
478 	if (!root->ref_cows)
479 		return -EINVAL;
480 
481 	pending_snapshot = kzalloc(sizeof(*pending_snapshot), GFP_NOFS);
482 	if (!pending_snapshot)
483 		return -ENOMEM;
484 
485 	btrfs_init_block_rsv(&pending_snapshot->block_rsv);
486 	pending_snapshot->dentry = dentry;
487 	pending_snapshot->root = root;
488 	pending_snapshot->readonly = readonly;
489 
490 	trans = btrfs_start_transaction(root->fs_info->extent_root, 5);
491 	if (IS_ERR(trans)) {
492 		ret = PTR_ERR(trans);
493 		goto fail;
494 	}
495 
496 	ret = btrfs_snap_reserve_metadata(trans, pending_snapshot);
497 	BUG_ON(ret);
498 
499 	spin_lock(&root->fs_info->trans_lock);
500 	list_add(&pending_snapshot->list,
501 		 &trans->transaction->pending_snapshots);
502 	spin_unlock(&root->fs_info->trans_lock);
503 	if (async_transid) {
504 		*async_transid = trans->transid;
505 		ret = btrfs_commit_transaction_async(trans,
506 				     root->fs_info->extent_root, 1);
507 	} else {
508 		ret = btrfs_commit_transaction(trans,
509 					       root->fs_info->extent_root);
510 	}
511 	BUG_ON(ret);
512 
513 	ret = pending_snapshot->error;
514 	if (ret)
515 		goto fail;
516 
517 	ret = btrfs_orphan_cleanup(pending_snapshot->snap);
518 	if (ret)
519 		goto fail;
520 
521 	inode = btrfs_lookup_dentry(dentry->d_parent->d_inode, dentry);
522 	if (IS_ERR(inode)) {
523 		ret = PTR_ERR(inode);
524 		goto fail;
525 	}
526 	BUG_ON(!inode);
527 	d_instantiate(dentry, inode);
528 	ret = 0;
529 fail:
530 	kfree(pending_snapshot);
531 	return ret;
532 }
533 
534 /*  copy of check_sticky in fs/namei.c()
535 * It's inline, so penalty for filesystems that don't use sticky bit is
536 * minimal.
537 */
538 static inline int btrfs_check_sticky(struct inode *dir, struct inode *inode)
539 {
540 	uid_t fsuid = current_fsuid();
541 
542 	if (!(dir->i_mode & S_ISVTX))
543 		return 0;
544 	if (inode->i_uid == fsuid)
545 		return 0;
546 	if (dir->i_uid == fsuid)
547 		return 0;
548 	return !capable(CAP_FOWNER);
549 }
550 
551 /*  copy of may_delete in fs/namei.c()
552  *	Check whether we can remove a link victim from directory dir, check
553  *  whether the type of victim is right.
554  *  1. We can't do it if dir is read-only (done in permission())
555  *  2. We should have write and exec permissions on dir
556  *  3. We can't remove anything from append-only dir
557  *  4. We can't do anything with immutable dir (done in permission())
558  *  5. If the sticky bit on dir is set we should either
559  *	a. be owner of dir, or
560  *	b. be owner of victim, or
561  *	c. have CAP_FOWNER capability
562  *  6. If the victim is append-only or immutable we can't do antyhing with
563  *     links pointing to it.
564  *  7. If we were asked to remove a directory and victim isn't one - ENOTDIR.
565  *  8. If we were asked to remove a non-directory and victim isn't one - EISDIR.
566  *  9. We can't remove a root or mountpoint.
567  * 10. We don't allow removal of NFS sillyrenamed files; it's handled by
568  *     nfs_async_unlink().
569  */
570 
571 static int btrfs_may_delete(struct inode *dir,struct dentry *victim,int isdir)
572 {
573 	int error;
574 
575 	if (!victim->d_inode)
576 		return -ENOENT;
577 
578 	BUG_ON(victim->d_parent->d_inode != dir);
579 	audit_inode_child(victim, dir);
580 
581 	error = inode_permission(dir, MAY_WRITE | MAY_EXEC);
582 	if (error)
583 		return error;
584 	if (IS_APPEND(dir))
585 		return -EPERM;
586 	if (btrfs_check_sticky(dir, victim->d_inode)||
587 		IS_APPEND(victim->d_inode)||
588 	    IS_IMMUTABLE(victim->d_inode) || IS_SWAPFILE(victim->d_inode))
589 		return -EPERM;
590 	if (isdir) {
591 		if (!S_ISDIR(victim->d_inode->i_mode))
592 			return -ENOTDIR;
593 		if (IS_ROOT(victim))
594 			return -EBUSY;
595 	} else if (S_ISDIR(victim->d_inode->i_mode))
596 		return -EISDIR;
597 	if (IS_DEADDIR(dir))
598 		return -ENOENT;
599 	if (victim->d_flags & DCACHE_NFSFS_RENAMED)
600 		return -EBUSY;
601 	return 0;
602 }
603 
604 /* copy of may_create in fs/namei.c() */
605 static inline int btrfs_may_create(struct inode *dir, struct dentry *child)
606 {
607 	if (child->d_inode)
608 		return -EEXIST;
609 	if (IS_DEADDIR(dir))
610 		return -ENOENT;
611 	return inode_permission(dir, MAY_WRITE | MAY_EXEC);
612 }
613 
614 /*
615  * Create a new subvolume below @parent.  This is largely modeled after
616  * sys_mkdirat and vfs_mkdir, but we only do a single component lookup
617  * inside this filesystem so it's quite a bit simpler.
618  */
619 static noinline int btrfs_mksubvol(struct path *parent,
620 				   char *name, int namelen,
621 				   struct btrfs_root *snap_src,
622 				   u64 *async_transid, bool readonly)
623 {
624 	struct inode *dir  = parent->dentry->d_inode;
625 	struct dentry *dentry;
626 	int error;
627 
628 	mutex_lock_nested(&dir->i_mutex, I_MUTEX_PARENT);
629 
630 	dentry = lookup_one_len(name, parent->dentry, namelen);
631 	error = PTR_ERR(dentry);
632 	if (IS_ERR(dentry))
633 		goto out_unlock;
634 
635 	error = -EEXIST;
636 	if (dentry->d_inode)
637 		goto out_dput;
638 
639 	error = mnt_want_write(parent->mnt);
640 	if (error)
641 		goto out_dput;
642 
643 	error = btrfs_may_create(dir, dentry);
644 	if (error)
645 		goto out_drop_write;
646 
647 	down_read(&BTRFS_I(dir)->root->fs_info->subvol_sem);
648 
649 	if (btrfs_root_refs(&BTRFS_I(dir)->root->root_item) == 0)
650 		goto out_up_read;
651 
652 	if (snap_src) {
653 		error = create_snapshot(snap_src, dentry,
654 					name, namelen, async_transid, readonly);
655 	} else {
656 		error = create_subvol(BTRFS_I(dir)->root, dentry,
657 				      name, namelen, async_transid);
658 	}
659 	if (!error)
660 		fsnotify_mkdir(dir, dentry);
661 out_up_read:
662 	up_read(&BTRFS_I(dir)->root->fs_info->subvol_sem);
663 out_drop_write:
664 	mnt_drop_write(parent->mnt);
665 out_dput:
666 	dput(dentry);
667 out_unlock:
668 	mutex_unlock(&dir->i_mutex);
669 	return error;
670 }
671 
672 /*
673  * When we're defragging a range, we don't want to kick it off again
674  * if it is really just waiting for delalloc to send it down.
675  * If we find a nice big extent or delalloc range for the bytes in the
676  * file you want to defrag, we return 0 to let you know to skip this
677  * part of the file
678  */
679 static int check_defrag_in_cache(struct inode *inode, u64 offset, int thresh)
680 {
681 	struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
682 	struct extent_map *em = NULL;
683 	struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
684 	u64 end;
685 
686 	read_lock(&em_tree->lock);
687 	em = lookup_extent_mapping(em_tree, offset, PAGE_CACHE_SIZE);
688 	read_unlock(&em_tree->lock);
689 
690 	if (em) {
691 		end = extent_map_end(em);
692 		free_extent_map(em);
693 		if (end - offset > thresh)
694 			return 0;
695 	}
696 	/* if we already have a nice delalloc here, just stop */
697 	thresh /= 2;
698 	end = count_range_bits(io_tree, &offset, offset + thresh,
699 			       thresh, EXTENT_DELALLOC, 1);
700 	if (end >= thresh)
701 		return 0;
702 	return 1;
703 }
704 
705 /*
706  * helper function to walk through a file and find extents
707  * newer than a specific transid, and smaller than thresh.
708  *
709  * This is used by the defragging code to find new and small
710  * extents
711  */
712 static int find_new_extents(struct btrfs_root *root,
713 			    struct inode *inode, u64 newer_than,
714 			    u64 *off, int thresh)
715 {
716 	struct btrfs_path *path;
717 	struct btrfs_key min_key;
718 	struct btrfs_key max_key;
719 	struct extent_buffer *leaf;
720 	struct btrfs_file_extent_item *extent;
721 	int type;
722 	int ret;
723 	u64 ino = btrfs_ino(inode);
724 
725 	path = btrfs_alloc_path();
726 	if (!path)
727 		return -ENOMEM;
728 
729 	min_key.objectid = ino;
730 	min_key.type = BTRFS_EXTENT_DATA_KEY;
731 	min_key.offset = *off;
732 
733 	max_key.objectid = ino;
734 	max_key.type = (u8)-1;
735 	max_key.offset = (u64)-1;
736 
737 	path->keep_locks = 1;
738 
739 	while(1) {
740 		ret = btrfs_search_forward(root, &min_key, &max_key,
741 					   path, 0, newer_than);
742 		if (ret != 0)
743 			goto none;
744 		if (min_key.objectid != ino)
745 			goto none;
746 		if (min_key.type != BTRFS_EXTENT_DATA_KEY)
747 			goto none;
748 
749 		leaf = path->nodes[0];
750 		extent = btrfs_item_ptr(leaf, path->slots[0],
751 					struct btrfs_file_extent_item);
752 
753 		type = btrfs_file_extent_type(leaf, extent);
754 		if (type == BTRFS_FILE_EXTENT_REG &&
755 		    btrfs_file_extent_num_bytes(leaf, extent) < thresh &&
756 		    check_defrag_in_cache(inode, min_key.offset, thresh)) {
757 			*off = min_key.offset;
758 			btrfs_free_path(path);
759 			return 0;
760 		}
761 
762 		if (min_key.offset == (u64)-1)
763 			goto none;
764 
765 		min_key.offset++;
766 		btrfs_release_path(path);
767 	}
768 none:
769 	btrfs_free_path(path);
770 	return -ENOENT;
771 }
772 
773 static int should_defrag_range(struct inode *inode, u64 start, u64 len,
774 			       int thresh, u64 *last_len, u64 *skip,
775 			       u64 *defrag_end)
776 {
777 	struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
778 	struct extent_map *em = NULL;
779 	struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
780 	int ret = 1;
781 
782 	/*
783 	 * make sure that once we start defragging an extent, we keep on
784 	 * defragging it
785 	 */
786 	if (start < *defrag_end)
787 		return 1;
788 
789 	*skip = 0;
790 
791 	/*
792 	 * hopefully we have this extent in the tree already, try without
793 	 * the full extent lock
794 	 */
795 	read_lock(&em_tree->lock);
796 	em = lookup_extent_mapping(em_tree, start, len);
797 	read_unlock(&em_tree->lock);
798 
799 	if (!em) {
800 		/* get the big lock and read metadata off disk */
801 		lock_extent(io_tree, start, start + len - 1, GFP_NOFS);
802 		em = btrfs_get_extent(inode, NULL, 0, start, len, 0);
803 		unlock_extent(io_tree, start, start + len - 1, GFP_NOFS);
804 
805 		if (IS_ERR(em))
806 			return 0;
807 	}
808 
809 	/* this will cover holes, and inline extents */
810 	if (em->block_start >= EXTENT_MAP_LAST_BYTE)
811 		ret = 0;
812 
813 	/*
814 	 * we hit a real extent, if it is big don't bother defragging it again
815 	 */
816 	if ((*last_len == 0 || *last_len >= thresh) && em->len >= thresh)
817 		ret = 0;
818 
819 	/*
820 	 * last_len ends up being a counter of how many bytes we've defragged.
821 	 * every time we choose not to defrag an extent, we reset *last_len
822 	 * so that the next tiny extent will force a defrag.
823 	 *
824 	 * The end result of this is that tiny extents before a single big
825 	 * extent will force at least part of that big extent to be defragged.
826 	 */
827 	if (ret) {
828 		*defrag_end = extent_map_end(em);
829 	} else {
830 		*last_len = 0;
831 		*skip = extent_map_end(em);
832 		*defrag_end = 0;
833 	}
834 
835 	free_extent_map(em);
836 	return ret;
837 }
838 
839 /*
840  * it doesn't do much good to defrag one or two pages
841  * at a time.  This pulls in a nice chunk of pages
842  * to COW and defrag.
843  *
844  * It also makes sure the delalloc code has enough
845  * dirty data to avoid making new small extents as part
846  * of the defrag
847  *
848  * It's a good idea to start RA on this range
849  * before calling this.
850  */
851 static int cluster_pages_for_defrag(struct inode *inode,
852 				    struct page **pages,
853 				    unsigned long start_index,
854 				    int num_pages)
855 {
856 	unsigned long file_end;
857 	u64 isize = i_size_read(inode);
858 	u64 page_start;
859 	u64 page_end;
860 	int ret;
861 	int i;
862 	int i_done;
863 	struct btrfs_ordered_extent *ordered;
864 	struct extent_state *cached_state = NULL;
865 	gfp_t mask = btrfs_alloc_write_mask(inode->i_mapping);
866 
867 	if (isize == 0)
868 		return 0;
869 	file_end = (isize - 1) >> PAGE_CACHE_SHIFT;
870 
871 	mutex_lock(&inode->i_mutex);
872 	ret = btrfs_delalloc_reserve_space(inode,
873 					   num_pages << PAGE_CACHE_SHIFT);
874 	mutex_unlock(&inode->i_mutex);
875 	if (ret)
876 		return ret;
877 again:
878 	ret = 0;
879 	i_done = 0;
880 
881 	/* step one, lock all the pages */
882 	for (i = 0; i < num_pages; i++) {
883 		struct page *page;
884 		page = find_or_create_page(inode->i_mapping,
885 					    start_index + i, mask);
886 		if (!page)
887 			break;
888 
889 		if (!PageUptodate(page)) {
890 			btrfs_readpage(NULL, page);
891 			lock_page(page);
892 			if (!PageUptodate(page)) {
893 				unlock_page(page);
894 				page_cache_release(page);
895 				ret = -EIO;
896 				break;
897 			}
898 		}
899 		isize = i_size_read(inode);
900 		file_end = (isize - 1) >> PAGE_CACHE_SHIFT;
901 		if (!isize || page->index > file_end ||
902 		    page->mapping != inode->i_mapping) {
903 			/* whoops, we blew past eof, skip this page */
904 			unlock_page(page);
905 			page_cache_release(page);
906 			break;
907 		}
908 		pages[i] = page;
909 		i_done++;
910 	}
911 	if (!i_done || ret)
912 		goto out;
913 
914 	if (!(inode->i_sb->s_flags & MS_ACTIVE))
915 		goto out;
916 
917 	/*
918 	 * so now we have a nice long stream of locked
919 	 * and up to date pages, lets wait on them
920 	 */
921 	for (i = 0; i < i_done; i++)
922 		wait_on_page_writeback(pages[i]);
923 
924 	page_start = page_offset(pages[0]);
925 	page_end = page_offset(pages[i_done - 1]) + PAGE_CACHE_SIZE;
926 
927 	lock_extent_bits(&BTRFS_I(inode)->io_tree,
928 			 page_start, page_end - 1, 0, &cached_state,
929 			 GFP_NOFS);
930 	ordered = btrfs_lookup_first_ordered_extent(inode, page_end - 1);
931 	if (ordered &&
932 	    ordered->file_offset + ordered->len > page_start &&
933 	    ordered->file_offset < page_end) {
934 		btrfs_put_ordered_extent(ordered);
935 		unlock_extent_cached(&BTRFS_I(inode)->io_tree,
936 				     page_start, page_end - 1,
937 				     &cached_state, GFP_NOFS);
938 		for (i = 0; i < i_done; i++) {
939 			unlock_page(pages[i]);
940 			page_cache_release(pages[i]);
941 		}
942 		btrfs_wait_ordered_range(inode, page_start,
943 					 page_end - page_start);
944 		goto again;
945 	}
946 	if (ordered)
947 		btrfs_put_ordered_extent(ordered);
948 
949 	clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start,
950 			  page_end - 1, EXTENT_DIRTY | EXTENT_DELALLOC |
951 			  EXTENT_DO_ACCOUNTING, 0, 0, &cached_state,
952 			  GFP_NOFS);
953 
954 	if (i_done != num_pages) {
955 		spin_lock(&BTRFS_I(inode)->lock);
956 		BTRFS_I(inode)->outstanding_extents++;
957 		spin_unlock(&BTRFS_I(inode)->lock);
958 		btrfs_delalloc_release_space(inode,
959 				     (num_pages - i_done) << PAGE_CACHE_SHIFT);
960 	}
961 
962 
963 	btrfs_set_extent_delalloc(inode, page_start, page_end - 1,
964 				  &cached_state);
965 
966 	unlock_extent_cached(&BTRFS_I(inode)->io_tree,
967 			     page_start, page_end - 1, &cached_state,
968 			     GFP_NOFS);
969 
970 	for (i = 0; i < i_done; i++) {
971 		clear_page_dirty_for_io(pages[i]);
972 		ClearPageChecked(pages[i]);
973 		set_page_extent_mapped(pages[i]);
974 		set_page_dirty(pages[i]);
975 		unlock_page(pages[i]);
976 		page_cache_release(pages[i]);
977 	}
978 	return i_done;
979 out:
980 	for (i = 0; i < i_done; i++) {
981 		unlock_page(pages[i]);
982 		page_cache_release(pages[i]);
983 	}
984 	btrfs_delalloc_release_space(inode, num_pages << PAGE_CACHE_SHIFT);
985 	return ret;
986 
987 }
988 
989 int btrfs_defrag_file(struct inode *inode, struct file *file,
990 		      struct btrfs_ioctl_defrag_range_args *range,
991 		      u64 newer_than, unsigned long max_to_defrag)
992 {
993 	struct btrfs_root *root = BTRFS_I(inode)->root;
994 	struct btrfs_super_block *disk_super;
995 	struct file_ra_state *ra = NULL;
996 	unsigned long last_index;
997 	u64 isize = i_size_read(inode);
998 	u64 features;
999 	u64 last_len = 0;
1000 	u64 skip = 0;
1001 	u64 defrag_end = 0;
1002 	u64 newer_off = range->start;
1003 	unsigned long i;
1004 	unsigned long ra_index = 0;
1005 	int ret;
1006 	int defrag_count = 0;
1007 	int compress_type = BTRFS_COMPRESS_ZLIB;
1008 	int extent_thresh = range->extent_thresh;
1009 	int max_cluster = (256 * 1024) >> PAGE_CACHE_SHIFT;
1010 	int cluster = max_cluster;
1011 	u64 new_align = ~((u64)128 * 1024 - 1);
1012 	struct page **pages = NULL;
1013 
1014 	if (extent_thresh == 0)
1015 		extent_thresh = 256 * 1024;
1016 
1017 	if (range->flags & BTRFS_DEFRAG_RANGE_COMPRESS) {
1018 		if (range->compress_type > BTRFS_COMPRESS_TYPES)
1019 			return -EINVAL;
1020 		if (range->compress_type)
1021 			compress_type = range->compress_type;
1022 	}
1023 
1024 	if (isize == 0)
1025 		return 0;
1026 
1027 	/*
1028 	 * if we were not given a file, allocate a readahead
1029 	 * context
1030 	 */
1031 	if (!file) {
1032 		ra = kzalloc(sizeof(*ra), GFP_NOFS);
1033 		if (!ra)
1034 			return -ENOMEM;
1035 		file_ra_state_init(ra, inode->i_mapping);
1036 	} else {
1037 		ra = &file->f_ra;
1038 	}
1039 
1040 	pages = kmalloc(sizeof(struct page *) * max_cluster,
1041 			GFP_NOFS);
1042 	if (!pages) {
1043 		ret = -ENOMEM;
1044 		goto out_ra;
1045 	}
1046 
1047 	/* find the last page to defrag */
1048 	if (range->start + range->len > range->start) {
1049 		last_index = min_t(u64, isize - 1,
1050 			 range->start + range->len - 1) >> PAGE_CACHE_SHIFT;
1051 	} else {
1052 		last_index = (isize - 1) >> PAGE_CACHE_SHIFT;
1053 	}
1054 
1055 	if (newer_than) {
1056 		ret = find_new_extents(root, inode, newer_than,
1057 				       &newer_off, 64 * 1024);
1058 		if (!ret) {
1059 			range->start = newer_off;
1060 			/*
1061 			 * we always align our defrag to help keep
1062 			 * the extents in the file evenly spaced
1063 			 */
1064 			i = (newer_off & new_align) >> PAGE_CACHE_SHIFT;
1065 		} else
1066 			goto out_ra;
1067 	} else {
1068 		i = range->start >> PAGE_CACHE_SHIFT;
1069 	}
1070 	if (!max_to_defrag)
1071 		max_to_defrag = last_index;
1072 
1073 	/*
1074 	 * make writeback starts from i, so the defrag range can be
1075 	 * written sequentially.
1076 	 */
1077 	if (i < inode->i_mapping->writeback_index)
1078 		inode->i_mapping->writeback_index = i;
1079 
1080 	while (i <= last_index && defrag_count < max_to_defrag &&
1081 	       (i < (i_size_read(inode) + PAGE_CACHE_SIZE - 1) >>
1082 		PAGE_CACHE_SHIFT)) {
1083 		/*
1084 		 * make sure we stop running if someone unmounts
1085 		 * the FS
1086 		 */
1087 		if (!(inode->i_sb->s_flags & MS_ACTIVE))
1088 			break;
1089 
1090 		if (!newer_than &&
1091 		    !should_defrag_range(inode, (u64)i << PAGE_CACHE_SHIFT,
1092 					PAGE_CACHE_SIZE,
1093 					extent_thresh,
1094 					&last_len, &skip,
1095 					&defrag_end)) {
1096 			unsigned long next;
1097 			/*
1098 			 * the should_defrag function tells us how much to skip
1099 			 * bump our counter by the suggested amount
1100 			 */
1101 			next = (skip + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
1102 			i = max(i + 1, next);
1103 			continue;
1104 		}
1105 
1106 		if (!newer_than) {
1107 			cluster = (PAGE_CACHE_ALIGN(defrag_end) >>
1108 				   PAGE_CACHE_SHIFT) - i;
1109 			cluster = min(cluster, max_cluster);
1110 		} else {
1111 			cluster = max_cluster;
1112 		}
1113 
1114 		if (range->flags & BTRFS_DEFRAG_RANGE_COMPRESS)
1115 			BTRFS_I(inode)->force_compress = compress_type;
1116 
1117 		if (i + cluster > ra_index) {
1118 			ra_index = max(i, ra_index);
1119 			btrfs_force_ra(inode->i_mapping, ra, file, ra_index,
1120 				       cluster);
1121 			ra_index += max_cluster;
1122 		}
1123 
1124 		ret = cluster_pages_for_defrag(inode, pages, i, cluster);
1125 		if (ret < 0)
1126 			goto out_ra;
1127 
1128 		defrag_count += ret;
1129 		balance_dirty_pages_ratelimited_nr(inode->i_mapping, ret);
1130 
1131 		if (newer_than) {
1132 			if (newer_off == (u64)-1)
1133 				break;
1134 
1135 			newer_off = max(newer_off + 1,
1136 					(u64)i << PAGE_CACHE_SHIFT);
1137 
1138 			ret = find_new_extents(root, inode,
1139 					       newer_than, &newer_off,
1140 					       64 * 1024);
1141 			if (!ret) {
1142 				range->start = newer_off;
1143 				i = (newer_off & new_align) >> PAGE_CACHE_SHIFT;
1144 			} else {
1145 				break;
1146 			}
1147 		} else {
1148 			if (ret > 0) {
1149 				i += ret;
1150 				last_len += ret << PAGE_CACHE_SHIFT;
1151 			} else {
1152 				i++;
1153 				last_len = 0;
1154 			}
1155 		}
1156 	}
1157 
1158 	if ((range->flags & BTRFS_DEFRAG_RANGE_START_IO))
1159 		filemap_flush(inode->i_mapping);
1160 
1161 	if ((range->flags & BTRFS_DEFRAG_RANGE_COMPRESS)) {
1162 		/* the filemap_flush will queue IO into the worker threads, but
1163 		 * we have to make sure the IO is actually started and that
1164 		 * ordered extents get created before we return
1165 		 */
1166 		atomic_inc(&root->fs_info->async_submit_draining);
1167 		while (atomic_read(&root->fs_info->nr_async_submits) ||
1168 		      atomic_read(&root->fs_info->async_delalloc_pages)) {
1169 			wait_event(root->fs_info->async_submit_wait,
1170 			   (atomic_read(&root->fs_info->nr_async_submits) == 0 &&
1171 			    atomic_read(&root->fs_info->async_delalloc_pages) == 0));
1172 		}
1173 		atomic_dec(&root->fs_info->async_submit_draining);
1174 
1175 		mutex_lock(&inode->i_mutex);
1176 		BTRFS_I(inode)->force_compress = BTRFS_COMPRESS_NONE;
1177 		mutex_unlock(&inode->i_mutex);
1178 	}
1179 
1180 	disk_super = root->fs_info->super_copy;
1181 	features = btrfs_super_incompat_flags(disk_super);
1182 	if (range->compress_type == BTRFS_COMPRESS_LZO) {
1183 		features |= BTRFS_FEATURE_INCOMPAT_COMPRESS_LZO;
1184 		btrfs_set_super_incompat_flags(disk_super, features);
1185 	}
1186 
1187 	ret = defrag_count;
1188 
1189 out_ra:
1190 	if (!file)
1191 		kfree(ra);
1192 	kfree(pages);
1193 	return ret;
1194 }
1195 
1196 static noinline int btrfs_ioctl_resize(struct btrfs_root *root,
1197 					void __user *arg)
1198 {
1199 	u64 new_size;
1200 	u64 old_size;
1201 	u64 devid = 1;
1202 	struct btrfs_ioctl_vol_args *vol_args;
1203 	struct btrfs_trans_handle *trans;
1204 	struct btrfs_device *device = NULL;
1205 	char *sizestr;
1206 	char *devstr = NULL;
1207 	int ret = 0;
1208 	int mod = 0;
1209 
1210 	if (root->fs_info->sb->s_flags & MS_RDONLY)
1211 		return -EROFS;
1212 
1213 	if (!capable(CAP_SYS_ADMIN))
1214 		return -EPERM;
1215 
1216 	mutex_lock(&root->fs_info->volume_mutex);
1217 	if (root->fs_info->balance_ctl) {
1218 		printk(KERN_INFO "btrfs: balance in progress\n");
1219 		ret = -EINVAL;
1220 		goto out;
1221 	}
1222 
1223 	vol_args = memdup_user(arg, sizeof(*vol_args));
1224 	if (IS_ERR(vol_args)) {
1225 		ret = PTR_ERR(vol_args);
1226 		goto out;
1227 	}
1228 
1229 	vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
1230 
1231 	sizestr = vol_args->name;
1232 	devstr = strchr(sizestr, ':');
1233 	if (devstr) {
1234 		char *end;
1235 		sizestr = devstr + 1;
1236 		*devstr = '\0';
1237 		devstr = vol_args->name;
1238 		devid = simple_strtoull(devstr, &end, 10);
1239 		printk(KERN_INFO "btrfs: resizing devid %llu\n",
1240 		       (unsigned long long)devid);
1241 	}
1242 	device = btrfs_find_device(root, devid, NULL, NULL);
1243 	if (!device) {
1244 		printk(KERN_INFO "btrfs: resizer unable to find device %llu\n",
1245 		       (unsigned long long)devid);
1246 		ret = -EINVAL;
1247 		goto out_free;
1248 	}
1249 	if (!strcmp(sizestr, "max"))
1250 		new_size = device->bdev->bd_inode->i_size;
1251 	else {
1252 		if (sizestr[0] == '-') {
1253 			mod = -1;
1254 			sizestr++;
1255 		} else if (sizestr[0] == '+') {
1256 			mod = 1;
1257 			sizestr++;
1258 		}
1259 		new_size = memparse(sizestr, NULL);
1260 		if (new_size == 0) {
1261 			ret = -EINVAL;
1262 			goto out_free;
1263 		}
1264 	}
1265 
1266 	old_size = device->total_bytes;
1267 
1268 	if (mod < 0) {
1269 		if (new_size > old_size) {
1270 			ret = -EINVAL;
1271 			goto out_free;
1272 		}
1273 		new_size = old_size - new_size;
1274 	} else if (mod > 0) {
1275 		new_size = old_size + new_size;
1276 	}
1277 
1278 	if (new_size < 256 * 1024 * 1024) {
1279 		ret = -EINVAL;
1280 		goto out_free;
1281 	}
1282 	if (new_size > device->bdev->bd_inode->i_size) {
1283 		ret = -EFBIG;
1284 		goto out_free;
1285 	}
1286 
1287 	do_div(new_size, root->sectorsize);
1288 	new_size *= root->sectorsize;
1289 
1290 	printk(KERN_INFO "btrfs: new size for %s is %llu\n",
1291 		device->name, (unsigned long long)new_size);
1292 
1293 	if (new_size > old_size) {
1294 		trans = btrfs_start_transaction(root, 0);
1295 		if (IS_ERR(trans)) {
1296 			ret = PTR_ERR(trans);
1297 			goto out_free;
1298 		}
1299 		ret = btrfs_grow_device(trans, device, new_size);
1300 		btrfs_commit_transaction(trans, root);
1301 	} else if (new_size < old_size) {
1302 		ret = btrfs_shrink_device(device, new_size);
1303 	}
1304 
1305 out_free:
1306 	kfree(vol_args);
1307 out:
1308 	mutex_unlock(&root->fs_info->volume_mutex);
1309 	return ret;
1310 }
1311 
1312 static noinline int btrfs_ioctl_snap_create_transid(struct file *file,
1313 						    char *name,
1314 						    unsigned long fd,
1315 						    int subvol,
1316 						    u64 *transid,
1317 						    bool readonly)
1318 {
1319 	struct btrfs_root *root = BTRFS_I(fdentry(file)->d_inode)->root;
1320 	struct file *src_file;
1321 	int namelen;
1322 	int ret = 0;
1323 
1324 	if (root->fs_info->sb->s_flags & MS_RDONLY)
1325 		return -EROFS;
1326 
1327 	namelen = strlen(name);
1328 	if (strchr(name, '/')) {
1329 		ret = -EINVAL;
1330 		goto out;
1331 	}
1332 
1333 	if (subvol) {
1334 		ret = btrfs_mksubvol(&file->f_path, name, namelen,
1335 				     NULL, transid, readonly);
1336 	} else {
1337 		struct inode *src_inode;
1338 		src_file = fget(fd);
1339 		if (!src_file) {
1340 			ret = -EINVAL;
1341 			goto out;
1342 		}
1343 
1344 		src_inode = src_file->f_path.dentry->d_inode;
1345 		if (src_inode->i_sb != file->f_path.dentry->d_inode->i_sb) {
1346 			printk(KERN_INFO "btrfs: Snapshot src from "
1347 			       "another FS\n");
1348 			ret = -EINVAL;
1349 			fput(src_file);
1350 			goto out;
1351 		}
1352 		ret = btrfs_mksubvol(&file->f_path, name, namelen,
1353 				     BTRFS_I(src_inode)->root,
1354 				     transid, readonly);
1355 		fput(src_file);
1356 	}
1357 out:
1358 	return ret;
1359 }
1360 
1361 static noinline int btrfs_ioctl_snap_create(struct file *file,
1362 					    void __user *arg, int subvol)
1363 {
1364 	struct btrfs_ioctl_vol_args *vol_args;
1365 	int ret;
1366 
1367 	vol_args = memdup_user(arg, sizeof(*vol_args));
1368 	if (IS_ERR(vol_args))
1369 		return PTR_ERR(vol_args);
1370 	vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
1371 
1372 	ret = btrfs_ioctl_snap_create_transid(file, vol_args->name,
1373 					      vol_args->fd, subvol,
1374 					      NULL, false);
1375 
1376 	kfree(vol_args);
1377 	return ret;
1378 }
1379 
1380 static noinline int btrfs_ioctl_snap_create_v2(struct file *file,
1381 					       void __user *arg, int subvol)
1382 {
1383 	struct btrfs_ioctl_vol_args_v2 *vol_args;
1384 	int ret;
1385 	u64 transid = 0;
1386 	u64 *ptr = NULL;
1387 	bool readonly = false;
1388 
1389 	vol_args = memdup_user(arg, sizeof(*vol_args));
1390 	if (IS_ERR(vol_args))
1391 		return PTR_ERR(vol_args);
1392 	vol_args->name[BTRFS_SUBVOL_NAME_MAX] = '\0';
1393 
1394 	if (vol_args->flags &
1395 	    ~(BTRFS_SUBVOL_CREATE_ASYNC | BTRFS_SUBVOL_RDONLY)) {
1396 		ret = -EOPNOTSUPP;
1397 		goto out;
1398 	}
1399 
1400 	if (vol_args->flags & BTRFS_SUBVOL_CREATE_ASYNC)
1401 		ptr = &transid;
1402 	if (vol_args->flags & BTRFS_SUBVOL_RDONLY)
1403 		readonly = true;
1404 
1405 	ret = btrfs_ioctl_snap_create_transid(file, vol_args->name,
1406 					      vol_args->fd, subvol,
1407 					      ptr, readonly);
1408 
1409 	if (ret == 0 && ptr &&
1410 	    copy_to_user(arg +
1411 			 offsetof(struct btrfs_ioctl_vol_args_v2,
1412 				  transid), ptr, sizeof(*ptr)))
1413 		ret = -EFAULT;
1414 out:
1415 	kfree(vol_args);
1416 	return ret;
1417 }
1418 
1419 static noinline int btrfs_ioctl_subvol_getflags(struct file *file,
1420 						void __user *arg)
1421 {
1422 	struct inode *inode = fdentry(file)->d_inode;
1423 	struct btrfs_root *root = BTRFS_I(inode)->root;
1424 	int ret = 0;
1425 	u64 flags = 0;
1426 
1427 	if (btrfs_ino(inode) != BTRFS_FIRST_FREE_OBJECTID)
1428 		return -EINVAL;
1429 
1430 	down_read(&root->fs_info->subvol_sem);
1431 	if (btrfs_root_readonly(root))
1432 		flags |= BTRFS_SUBVOL_RDONLY;
1433 	up_read(&root->fs_info->subvol_sem);
1434 
1435 	if (copy_to_user(arg, &flags, sizeof(flags)))
1436 		ret = -EFAULT;
1437 
1438 	return ret;
1439 }
1440 
1441 static noinline int btrfs_ioctl_subvol_setflags(struct file *file,
1442 					      void __user *arg)
1443 {
1444 	struct inode *inode = fdentry(file)->d_inode;
1445 	struct btrfs_root *root = BTRFS_I(inode)->root;
1446 	struct btrfs_trans_handle *trans;
1447 	u64 root_flags;
1448 	u64 flags;
1449 	int ret = 0;
1450 
1451 	if (root->fs_info->sb->s_flags & MS_RDONLY)
1452 		return -EROFS;
1453 
1454 	if (btrfs_ino(inode) != BTRFS_FIRST_FREE_OBJECTID)
1455 		return -EINVAL;
1456 
1457 	if (copy_from_user(&flags, arg, sizeof(flags)))
1458 		return -EFAULT;
1459 
1460 	if (flags & BTRFS_SUBVOL_CREATE_ASYNC)
1461 		return -EINVAL;
1462 
1463 	if (flags & ~BTRFS_SUBVOL_RDONLY)
1464 		return -EOPNOTSUPP;
1465 
1466 	if (!inode_owner_or_capable(inode))
1467 		return -EACCES;
1468 
1469 	down_write(&root->fs_info->subvol_sem);
1470 
1471 	/* nothing to do */
1472 	if (!!(flags & BTRFS_SUBVOL_RDONLY) == btrfs_root_readonly(root))
1473 		goto out;
1474 
1475 	root_flags = btrfs_root_flags(&root->root_item);
1476 	if (flags & BTRFS_SUBVOL_RDONLY)
1477 		btrfs_set_root_flags(&root->root_item,
1478 				     root_flags | BTRFS_ROOT_SUBVOL_RDONLY);
1479 	else
1480 		btrfs_set_root_flags(&root->root_item,
1481 				     root_flags & ~BTRFS_ROOT_SUBVOL_RDONLY);
1482 
1483 	trans = btrfs_start_transaction(root, 1);
1484 	if (IS_ERR(trans)) {
1485 		ret = PTR_ERR(trans);
1486 		goto out_reset;
1487 	}
1488 
1489 	ret = btrfs_update_root(trans, root->fs_info->tree_root,
1490 				&root->root_key, &root->root_item);
1491 
1492 	btrfs_commit_transaction(trans, root);
1493 out_reset:
1494 	if (ret)
1495 		btrfs_set_root_flags(&root->root_item, root_flags);
1496 out:
1497 	up_write(&root->fs_info->subvol_sem);
1498 	return ret;
1499 }
1500 
1501 /*
1502  * helper to check if the subvolume references other subvolumes
1503  */
1504 static noinline int may_destroy_subvol(struct btrfs_root *root)
1505 {
1506 	struct btrfs_path *path;
1507 	struct btrfs_key key;
1508 	int ret;
1509 
1510 	path = btrfs_alloc_path();
1511 	if (!path)
1512 		return -ENOMEM;
1513 
1514 	key.objectid = root->root_key.objectid;
1515 	key.type = BTRFS_ROOT_REF_KEY;
1516 	key.offset = (u64)-1;
1517 
1518 	ret = btrfs_search_slot(NULL, root->fs_info->tree_root,
1519 				&key, path, 0, 0);
1520 	if (ret < 0)
1521 		goto out;
1522 	BUG_ON(ret == 0);
1523 
1524 	ret = 0;
1525 	if (path->slots[0] > 0) {
1526 		path->slots[0]--;
1527 		btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
1528 		if (key.objectid == root->root_key.objectid &&
1529 		    key.type == BTRFS_ROOT_REF_KEY)
1530 			ret = -ENOTEMPTY;
1531 	}
1532 out:
1533 	btrfs_free_path(path);
1534 	return ret;
1535 }
1536 
1537 static noinline int key_in_sk(struct btrfs_key *key,
1538 			      struct btrfs_ioctl_search_key *sk)
1539 {
1540 	struct btrfs_key test;
1541 	int ret;
1542 
1543 	test.objectid = sk->min_objectid;
1544 	test.type = sk->min_type;
1545 	test.offset = sk->min_offset;
1546 
1547 	ret = btrfs_comp_cpu_keys(key, &test);
1548 	if (ret < 0)
1549 		return 0;
1550 
1551 	test.objectid = sk->max_objectid;
1552 	test.type = sk->max_type;
1553 	test.offset = sk->max_offset;
1554 
1555 	ret = btrfs_comp_cpu_keys(key, &test);
1556 	if (ret > 0)
1557 		return 0;
1558 	return 1;
1559 }
1560 
1561 static noinline int copy_to_sk(struct btrfs_root *root,
1562 			       struct btrfs_path *path,
1563 			       struct btrfs_key *key,
1564 			       struct btrfs_ioctl_search_key *sk,
1565 			       char *buf,
1566 			       unsigned long *sk_offset,
1567 			       int *num_found)
1568 {
1569 	u64 found_transid;
1570 	struct extent_buffer *leaf;
1571 	struct btrfs_ioctl_search_header sh;
1572 	unsigned long item_off;
1573 	unsigned long item_len;
1574 	int nritems;
1575 	int i;
1576 	int slot;
1577 	int ret = 0;
1578 
1579 	leaf = path->nodes[0];
1580 	slot = path->slots[0];
1581 	nritems = btrfs_header_nritems(leaf);
1582 
1583 	if (btrfs_header_generation(leaf) > sk->max_transid) {
1584 		i = nritems;
1585 		goto advance_key;
1586 	}
1587 	found_transid = btrfs_header_generation(leaf);
1588 
1589 	for (i = slot; i < nritems; i++) {
1590 		item_off = btrfs_item_ptr_offset(leaf, i);
1591 		item_len = btrfs_item_size_nr(leaf, i);
1592 
1593 		if (item_len > BTRFS_SEARCH_ARGS_BUFSIZE)
1594 			item_len = 0;
1595 
1596 		if (sizeof(sh) + item_len + *sk_offset >
1597 		    BTRFS_SEARCH_ARGS_BUFSIZE) {
1598 			ret = 1;
1599 			goto overflow;
1600 		}
1601 
1602 		btrfs_item_key_to_cpu(leaf, key, i);
1603 		if (!key_in_sk(key, sk))
1604 			continue;
1605 
1606 		sh.objectid = key->objectid;
1607 		sh.offset = key->offset;
1608 		sh.type = key->type;
1609 		sh.len = item_len;
1610 		sh.transid = found_transid;
1611 
1612 		/* copy search result header */
1613 		memcpy(buf + *sk_offset, &sh, sizeof(sh));
1614 		*sk_offset += sizeof(sh);
1615 
1616 		if (item_len) {
1617 			char *p = buf + *sk_offset;
1618 			/* copy the item */
1619 			read_extent_buffer(leaf, p,
1620 					   item_off, item_len);
1621 			*sk_offset += item_len;
1622 		}
1623 		(*num_found)++;
1624 
1625 		if (*num_found >= sk->nr_items)
1626 			break;
1627 	}
1628 advance_key:
1629 	ret = 0;
1630 	if (key->offset < (u64)-1 && key->offset < sk->max_offset)
1631 		key->offset++;
1632 	else if (key->type < (u8)-1 && key->type < sk->max_type) {
1633 		key->offset = 0;
1634 		key->type++;
1635 	} else if (key->objectid < (u64)-1 && key->objectid < sk->max_objectid) {
1636 		key->offset = 0;
1637 		key->type = 0;
1638 		key->objectid++;
1639 	} else
1640 		ret = 1;
1641 overflow:
1642 	return ret;
1643 }
1644 
1645 static noinline int search_ioctl(struct inode *inode,
1646 				 struct btrfs_ioctl_search_args *args)
1647 {
1648 	struct btrfs_root *root;
1649 	struct btrfs_key key;
1650 	struct btrfs_key max_key;
1651 	struct btrfs_path *path;
1652 	struct btrfs_ioctl_search_key *sk = &args->key;
1653 	struct btrfs_fs_info *info = BTRFS_I(inode)->root->fs_info;
1654 	int ret;
1655 	int num_found = 0;
1656 	unsigned long sk_offset = 0;
1657 
1658 	path = btrfs_alloc_path();
1659 	if (!path)
1660 		return -ENOMEM;
1661 
1662 	if (sk->tree_id == 0) {
1663 		/* search the root of the inode that was passed */
1664 		root = BTRFS_I(inode)->root;
1665 	} else {
1666 		key.objectid = sk->tree_id;
1667 		key.type = BTRFS_ROOT_ITEM_KEY;
1668 		key.offset = (u64)-1;
1669 		root = btrfs_read_fs_root_no_name(info, &key);
1670 		if (IS_ERR(root)) {
1671 			printk(KERN_ERR "could not find root %llu\n",
1672 			       sk->tree_id);
1673 			btrfs_free_path(path);
1674 			return -ENOENT;
1675 		}
1676 	}
1677 
1678 	key.objectid = sk->min_objectid;
1679 	key.type = sk->min_type;
1680 	key.offset = sk->min_offset;
1681 
1682 	max_key.objectid = sk->max_objectid;
1683 	max_key.type = sk->max_type;
1684 	max_key.offset = sk->max_offset;
1685 
1686 	path->keep_locks = 1;
1687 
1688 	while(1) {
1689 		ret = btrfs_search_forward(root, &key, &max_key, path, 0,
1690 					   sk->min_transid);
1691 		if (ret != 0) {
1692 			if (ret > 0)
1693 				ret = 0;
1694 			goto err;
1695 		}
1696 		ret = copy_to_sk(root, path, &key, sk, args->buf,
1697 				 &sk_offset, &num_found);
1698 		btrfs_release_path(path);
1699 		if (ret || num_found >= sk->nr_items)
1700 			break;
1701 
1702 	}
1703 	ret = 0;
1704 err:
1705 	sk->nr_items = num_found;
1706 	btrfs_free_path(path);
1707 	return ret;
1708 }
1709 
1710 static noinline int btrfs_ioctl_tree_search(struct file *file,
1711 					   void __user *argp)
1712 {
1713 	 struct btrfs_ioctl_search_args *args;
1714 	 struct inode *inode;
1715 	 int ret;
1716 
1717 	if (!capable(CAP_SYS_ADMIN))
1718 		return -EPERM;
1719 
1720 	args = memdup_user(argp, sizeof(*args));
1721 	if (IS_ERR(args))
1722 		return PTR_ERR(args);
1723 
1724 	inode = fdentry(file)->d_inode;
1725 	ret = search_ioctl(inode, args);
1726 	if (ret == 0 && copy_to_user(argp, args, sizeof(*args)))
1727 		ret = -EFAULT;
1728 	kfree(args);
1729 	return ret;
1730 }
1731 
1732 /*
1733  * Search INODE_REFs to identify path name of 'dirid' directory
1734  * in a 'tree_id' tree. and sets path name to 'name'.
1735  */
1736 static noinline int btrfs_search_path_in_tree(struct btrfs_fs_info *info,
1737 				u64 tree_id, u64 dirid, char *name)
1738 {
1739 	struct btrfs_root *root;
1740 	struct btrfs_key key;
1741 	char *ptr;
1742 	int ret = -1;
1743 	int slot;
1744 	int len;
1745 	int total_len = 0;
1746 	struct btrfs_inode_ref *iref;
1747 	struct extent_buffer *l;
1748 	struct btrfs_path *path;
1749 
1750 	if (dirid == BTRFS_FIRST_FREE_OBJECTID) {
1751 		name[0]='\0';
1752 		return 0;
1753 	}
1754 
1755 	path = btrfs_alloc_path();
1756 	if (!path)
1757 		return -ENOMEM;
1758 
1759 	ptr = &name[BTRFS_INO_LOOKUP_PATH_MAX];
1760 
1761 	key.objectid = tree_id;
1762 	key.type = BTRFS_ROOT_ITEM_KEY;
1763 	key.offset = (u64)-1;
1764 	root = btrfs_read_fs_root_no_name(info, &key);
1765 	if (IS_ERR(root)) {
1766 		printk(KERN_ERR "could not find root %llu\n", tree_id);
1767 		ret = -ENOENT;
1768 		goto out;
1769 	}
1770 
1771 	key.objectid = dirid;
1772 	key.type = BTRFS_INODE_REF_KEY;
1773 	key.offset = (u64)-1;
1774 
1775 	while(1) {
1776 		ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1777 		if (ret < 0)
1778 			goto out;
1779 
1780 		l = path->nodes[0];
1781 		slot = path->slots[0];
1782 		if (ret > 0 && slot > 0)
1783 			slot--;
1784 		btrfs_item_key_to_cpu(l, &key, slot);
1785 
1786 		if (ret > 0 && (key.objectid != dirid ||
1787 				key.type != BTRFS_INODE_REF_KEY)) {
1788 			ret = -ENOENT;
1789 			goto out;
1790 		}
1791 
1792 		iref = btrfs_item_ptr(l, slot, struct btrfs_inode_ref);
1793 		len = btrfs_inode_ref_name_len(l, iref);
1794 		ptr -= len + 1;
1795 		total_len += len + 1;
1796 		if (ptr < name)
1797 			goto out;
1798 
1799 		*(ptr + len) = '/';
1800 		read_extent_buffer(l, ptr,(unsigned long)(iref + 1), len);
1801 
1802 		if (key.offset == BTRFS_FIRST_FREE_OBJECTID)
1803 			break;
1804 
1805 		btrfs_release_path(path);
1806 		key.objectid = key.offset;
1807 		key.offset = (u64)-1;
1808 		dirid = key.objectid;
1809 	}
1810 	if (ptr < name)
1811 		goto out;
1812 	memmove(name, ptr, total_len);
1813 	name[total_len]='\0';
1814 	ret = 0;
1815 out:
1816 	btrfs_free_path(path);
1817 	return ret;
1818 }
1819 
1820 static noinline int btrfs_ioctl_ino_lookup(struct file *file,
1821 					   void __user *argp)
1822 {
1823 	 struct btrfs_ioctl_ino_lookup_args *args;
1824 	 struct inode *inode;
1825 	 int ret;
1826 
1827 	if (!capable(CAP_SYS_ADMIN))
1828 		return -EPERM;
1829 
1830 	args = memdup_user(argp, sizeof(*args));
1831 	if (IS_ERR(args))
1832 		return PTR_ERR(args);
1833 
1834 	inode = fdentry(file)->d_inode;
1835 
1836 	if (args->treeid == 0)
1837 		args->treeid = BTRFS_I(inode)->root->root_key.objectid;
1838 
1839 	ret = btrfs_search_path_in_tree(BTRFS_I(inode)->root->fs_info,
1840 					args->treeid, args->objectid,
1841 					args->name);
1842 
1843 	if (ret == 0 && copy_to_user(argp, args, sizeof(*args)))
1844 		ret = -EFAULT;
1845 
1846 	kfree(args);
1847 	return ret;
1848 }
1849 
1850 static noinline int btrfs_ioctl_snap_destroy(struct file *file,
1851 					     void __user *arg)
1852 {
1853 	struct dentry *parent = fdentry(file);
1854 	struct dentry *dentry;
1855 	struct inode *dir = parent->d_inode;
1856 	struct inode *inode;
1857 	struct btrfs_root *root = BTRFS_I(dir)->root;
1858 	struct btrfs_root *dest = NULL;
1859 	struct btrfs_ioctl_vol_args *vol_args;
1860 	struct btrfs_trans_handle *trans;
1861 	int namelen;
1862 	int ret;
1863 	int err = 0;
1864 
1865 	vol_args = memdup_user(arg, sizeof(*vol_args));
1866 	if (IS_ERR(vol_args))
1867 		return PTR_ERR(vol_args);
1868 
1869 	vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
1870 	namelen = strlen(vol_args->name);
1871 	if (strchr(vol_args->name, '/') ||
1872 	    strncmp(vol_args->name, "..", namelen) == 0) {
1873 		err = -EINVAL;
1874 		goto out;
1875 	}
1876 
1877 	err = mnt_want_write(file->f_path.mnt);
1878 	if (err)
1879 		goto out;
1880 
1881 	mutex_lock_nested(&dir->i_mutex, I_MUTEX_PARENT);
1882 	dentry = lookup_one_len(vol_args->name, parent, namelen);
1883 	if (IS_ERR(dentry)) {
1884 		err = PTR_ERR(dentry);
1885 		goto out_unlock_dir;
1886 	}
1887 
1888 	if (!dentry->d_inode) {
1889 		err = -ENOENT;
1890 		goto out_dput;
1891 	}
1892 
1893 	inode = dentry->d_inode;
1894 	dest = BTRFS_I(inode)->root;
1895 	if (!capable(CAP_SYS_ADMIN)){
1896 		/*
1897 		 * Regular user.  Only allow this with a special mount
1898 		 * option, when the user has write+exec access to the
1899 		 * subvol root, and when rmdir(2) would have been
1900 		 * allowed.
1901 		 *
1902 		 * Note that this is _not_ check that the subvol is
1903 		 * empty or doesn't contain data that we wouldn't
1904 		 * otherwise be able to delete.
1905 		 *
1906 		 * Users who want to delete empty subvols should try
1907 		 * rmdir(2).
1908 		 */
1909 		err = -EPERM;
1910 		if (!btrfs_test_opt(root, USER_SUBVOL_RM_ALLOWED))
1911 			goto out_dput;
1912 
1913 		/*
1914 		 * Do not allow deletion if the parent dir is the same
1915 		 * as the dir to be deleted.  That means the ioctl
1916 		 * must be called on the dentry referencing the root
1917 		 * of the subvol, not a random directory contained
1918 		 * within it.
1919 		 */
1920 		err = -EINVAL;
1921 		if (root == dest)
1922 			goto out_dput;
1923 
1924 		err = inode_permission(inode, MAY_WRITE | MAY_EXEC);
1925 		if (err)
1926 			goto out_dput;
1927 
1928 		/* check if subvolume may be deleted by a non-root user */
1929 		err = btrfs_may_delete(dir, dentry, 1);
1930 		if (err)
1931 			goto out_dput;
1932 	}
1933 
1934 	if (btrfs_ino(inode) != BTRFS_FIRST_FREE_OBJECTID) {
1935 		err = -EINVAL;
1936 		goto out_dput;
1937 	}
1938 
1939 	mutex_lock(&inode->i_mutex);
1940 	err = d_invalidate(dentry);
1941 	if (err)
1942 		goto out_unlock;
1943 
1944 	down_write(&root->fs_info->subvol_sem);
1945 
1946 	err = may_destroy_subvol(dest);
1947 	if (err)
1948 		goto out_up_write;
1949 
1950 	trans = btrfs_start_transaction(root, 0);
1951 	if (IS_ERR(trans)) {
1952 		err = PTR_ERR(trans);
1953 		goto out_up_write;
1954 	}
1955 	trans->block_rsv = &root->fs_info->global_block_rsv;
1956 
1957 	ret = btrfs_unlink_subvol(trans, root, dir,
1958 				dest->root_key.objectid,
1959 				dentry->d_name.name,
1960 				dentry->d_name.len);
1961 	BUG_ON(ret);
1962 
1963 	btrfs_record_root_in_trans(trans, dest);
1964 
1965 	memset(&dest->root_item.drop_progress, 0,
1966 		sizeof(dest->root_item.drop_progress));
1967 	dest->root_item.drop_level = 0;
1968 	btrfs_set_root_refs(&dest->root_item, 0);
1969 
1970 	if (!xchg(&dest->orphan_item_inserted, 1)) {
1971 		ret = btrfs_insert_orphan_item(trans,
1972 					root->fs_info->tree_root,
1973 					dest->root_key.objectid);
1974 		BUG_ON(ret);
1975 	}
1976 
1977 	ret = btrfs_end_transaction(trans, root);
1978 	BUG_ON(ret);
1979 	inode->i_flags |= S_DEAD;
1980 out_up_write:
1981 	up_write(&root->fs_info->subvol_sem);
1982 out_unlock:
1983 	mutex_unlock(&inode->i_mutex);
1984 	if (!err) {
1985 		shrink_dcache_sb(root->fs_info->sb);
1986 		btrfs_invalidate_inodes(dest);
1987 		d_delete(dentry);
1988 	}
1989 out_dput:
1990 	dput(dentry);
1991 out_unlock_dir:
1992 	mutex_unlock(&dir->i_mutex);
1993 	mnt_drop_write(file->f_path.mnt);
1994 out:
1995 	kfree(vol_args);
1996 	return err;
1997 }
1998 
1999 static int btrfs_ioctl_defrag(struct file *file, void __user *argp)
2000 {
2001 	struct inode *inode = fdentry(file)->d_inode;
2002 	struct btrfs_root *root = BTRFS_I(inode)->root;
2003 	struct btrfs_ioctl_defrag_range_args *range;
2004 	int ret;
2005 
2006 	if (btrfs_root_readonly(root))
2007 		return -EROFS;
2008 
2009 	ret = mnt_want_write(file->f_path.mnt);
2010 	if (ret)
2011 		return ret;
2012 
2013 	switch (inode->i_mode & S_IFMT) {
2014 	case S_IFDIR:
2015 		if (!capable(CAP_SYS_ADMIN)) {
2016 			ret = -EPERM;
2017 			goto out;
2018 		}
2019 		ret = btrfs_defrag_root(root, 0);
2020 		if (ret)
2021 			goto out;
2022 		ret = btrfs_defrag_root(root->fs_info->extent_root, 0);
2023 		break;
2024 	case S_IFREG:
2025 		if (!(file->f_mode & FMODE_WRITE)) {
2026 			ret = -EINVAL;
2027 			goto out;
2028 		}
2029 
2030 		range = kzalloc(sizeof(*range), GFP_KERNEL);
2031 		if (!range) {
2032 			ret = -ENOMEM;
2033 			goto out;
2034 		}
2035 
2036 		if (argp) {
2037 			if (copy_from_user(range, argp,
2038 					   sizeof(*range))) {
2039 				ret = -EFAULT;
2040 				kfree(range);
2041 				goto out;
2042 			}
2043 			/* compression requires us to start the IO */
2044 			if ((range->flags & BTRFS_DEFRAG_RANGE_COMPRESS)) {
2045 				range->flags |= BTRFS_DEFRAG_RANGE_START_IO;
2046 				range->extent_thresh = (u32)-1;
2047 			}
2048 		} else {
2049 			/* the rest are all set to zero by kzalloc */
2050 			range->len = (u64)-1;
2051 		}
2052 		ret = btrfs_defrag_file(fdentry(file)->d_inode, file,
2053 					range, 0, 0);
2054 		if (ret > 0)
2055 			ret = 0;
2056 		kfree(range);
2057 		break;
2058 	default:
2059 		ret = -EINVAL;
2060 	}
2061 out:
2062 	mnt_drop_write(file->f_path.mnt);
2063 	return ret;
2064 }
2065 
2066 static long btrfs_ioctl_add_dev(struct btrfs_root *root, void __user *arg)
2067 {
2068 	struct btrfs_ioctl_vol_args *vol_args;
2069 	int ret;
2070 
2071 	if (!capable(CAP_SYS_ADMIN))
2072 		return -EPERM;
2073 
2074 	mutex_lock(&root->fs_info->volume_mutex);
2075 	if (root->fs_info->balance_ctl) {
2076 		printk(KERN_INFO "btrfs: balance in progress\n");
2077 		ret = -EINVAL;
2078 		goto out;
2079 	}
2080 
2081 	vol_args = memdup_user(arg, sizeof(*vol_args));
2082 	if (IS_ERR(vol_args)) {
2083 		ret = PTR_ERR(vol_args);
2084 		goto out;
2085 	}
2086 
2087 	vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
2088 	ret = btrfs_init_new_device(root, vol_args->name);
2089 
2090 	kfree(vol_args);
2091 out:
2092 	mutex_unlock(&root->fs_info->volume_mutex);
2093 	return ret;
2094 }
2095 
2096 static long btrfs_ioctl_rm_dev(struct btrfs_root *root, void __user *arg)
2097 {
2098 	struct btrfs_ioctl_vol_args *vol_args;
2099 	int ret;
2100 
2101 	if (!capable(CAP_SYS_ADMIN))
2102 		return -EPERM;
2103 
2104 	if (root->fs_info->sb->s_flags & MS_RDONLY)
2105 		return -EROFS;
2106 
2107 	mutex_lock(&root->fs_info->volume_mutex);
2108 	if (root->fs_info->balance_ctl) {
2109 		printk(KERN_INFO "btrfs: balance in progress\n");
2110 		ret = -EINVAL;
2111 		goto out;
2112 	}
2113 
2114 	vol_args = memdup_user(arg, sizeof(*vol_args));
2115 	if (IS_ERR(vol_args)) {
2116 		ret = PTR_ERR(vol_args);
2117 		goto out;
2118 	}
2119 
2120 	vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
2121 	ret = btrfs_rm_device(root, vol_args->name);
2122 
2123 	kfree(vol_args);
2124 out:
2125 	mutex_unlock(&root->fs_info->volume_mutex);
2126 	return ret;
2127 }
2128 
2129 static long btrfs_ioctl_fs_info(struct btrfs_root *root, void __user *arg)
2130 {
2131 	struct btrfs_ioctl_fs_info_args *fi_args;
2132 	struct btrfs_device *device;
2133 	struct btrfs_device *next;
2134 	struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices;
2135 	int ret = 0;
2136 
2137 	if (!capable(CAP_SYS_ADMIN))
2138 		return -EPERM;
2139 
2140 	fi_args = kzalloc(sizeof(*fi_args), GFP_KERNEL);
2141 	if (!fi_args)
2142 		return -ENOMEM;
2143 
2144 	fi_args->num_devices = fs_devices->num_devices;
2145 	memcpy(&fi_args->fsid, root->fs_info->fsid, sizeof(fi_args->fsid));
2146 
2147 	mutex_lock(&fs_devices->device_list_mutex);
2148 	list_for_each_entry_safe(device, next, &fs_devices->devices, dev_list) {
2149 		if (device->devid > fi_args->max_id)
2150 			fi_args->max_id = device->devid;
2151 	}
2152 	mutex_unlock(&fs_devices->device_list_mutex);
2153 
2154 	if (copy_to_user(arg, fi_args, sizeof(*fi_args)))
2155 		ret = -EFAULT;
2156 
2157 	kfree(fi_args);
2158 	return ret;
2159 }
2160 
2161 static long btrfs_ioctl_dev_info(struct btrfs_root *root, void __user *arg)
2162 {
2163 	struct btrfs_ioctl_dev_info_args *di_args;
2164 	struct btrfs_device *dev;
2165 	struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices;
2166 	int ret = 0;
2167 	char *s_uuid = NULL;
2168 	char empty_uuid[BTRFS_UUID_SIZE] = {0};
2169 
2170 	if (!capable(CAP_SYS_ADMIN))
2171 		return -EPERM;
2172 
2173 	di_args = memdup_user(arg, sizeof(*di_args));
2174 	if (IS_ERR(di_args))
2175 		return PTR_ERR(di_args);
2176 
2177 	if (memcmp(empty_uuid, di_args->uuid, BTRFS_UUID_SIZE) != 0)
2178 		s_uuid = di_args->uuid;
2179 
2180 	mutex_lock(&fs_devices->device_list_mutex);
2181 	dev = btrfs_find_device(root, di_args->devid, s_uuid, NULL);
2182 	mutex_unlock(&fs_devices->device_list_mutex);
2183 
2184 	if (!dev) {
2185 		ret = -ENODEV;
2186 		goto out;
2187 	}
2188 
2189 	di_args->devid = dev->devid;
2190 	di_args->bytes_used = dev->bytes_used;
2191 	di_args->total_bytes = dev->total_bytes;
2192 	memcpy(di_args->uuid, dev->uuid, sizeof(di_args->uuid));
2193 	strncpy(di_args->path, dev->name, sizeof(di_args->path));
2194 
2195 out:
2196 	if (ret == 0 && copy_to_user(arg, di_args, sizeof(*di_args)))
2197 		ret = -EFAULT;
2198 
2199 	kfree(di_args);
2200 	return ret;
2201 }
2202 
2203 static noinline long btrfs_ioctl_clone(struct file *file, unsigned long srcfd,
2204 				       u64 off, u64 olen, u64 destoff)
2205 {
2206 	struct inode *inode = fdentry(file)->d_inode;
2207 	struct btrfs_root *root = BTRFS_I(inode)->root;
2208 	struct file *src_file;
2209 	struct inode *src;
2210 	struct btrfs_trans_handle *trans;
2211 	struct btrfs_path *path;
2212 	struct extent_buffer *leaf;
2213 	char *buf;
2214 	struct btrfs_key key;
2215 	u32 nritems;
2216 	int slot;
2217 	int ret;
2218 	u64 len = olen;
2219 	u64 bs = root->fs_info->sb->s_blocksize;
2220 	u64 hint_byte;
2221 
2222 	/*
2223 	 * TODO:
2224 	 * - split compressed inline extents.  annoying: we need to
2225 	 *   decompress into destination's address_space (the file offset
2226 	 *   may change, so source mapping won't do), then recompress (or
2227 	 *   otherwise reinsert) a subrange.
2228 	 * - allow ranges within the same file to be cloned (provided
2229 	 *   they don't overlap)?
2230 	 */
2231 
2232 	/* the destination must be opened for writing */
2233 	if (!(file->f_mode & FMODE_WRITE) || (file->f_flags & O_APPEND))
2234 		return -EINVAL;
2235 
2236 	if (btrfs_root_readonly(root))
2237 		return -EROFS;
2238 
2239 	ret = mnt_want_write(file->f_path.mnt);
2240 	if (ret)
2241 		return ret;
2242 
2243 	src_file = fget(srcfd);
2244 	if (!src_file) {
2245 		ret = -EBADF;
2246 		goto out_drop_write;
2247 	}
2248 
2249 	src = src_file->f_dentry->d_inode;
2250 
2251 	ret = -EINVAL;
2252 	if (src == inode)
2253 		goto out_fput;
2254 
2255 	/* the src must be open for reading */
2256 	if (!(src_file->f_mode & FMODE_READ))
2257 		goto out_fput;
2258 
2259 	/* don't make the dst file partly checksummed */
2260 	if ((BTRFS_I(src)->flags & BTRFS_INODE_NODATASUM) !=
2261 	    (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM))
2262 		goto out_fput;
2263 
2264 	ret = -EISDIR;
2265 	if (S_ISDIR(src->i_mode) || S_ISDIR(inode->i_mode))
2266 		goto out_fput;
2267 
2268 	ret = -EXDEV;
2269 	if (src->i_sb != inode->i_sb || BTRFS_I(src)->root != root)
2270 		goto out_fput;
2271 
2272 	ret = -ENOMEM;
2273 	buf = vmalloc(btrfs_level_size(root, 0));
2274 	if (!buf)
2275 		goto out_fput;
2276 
2277 	path = btrfs_alloc_path();
2278 	if (!path) {
2279 		vfree(buf);
2280 		goto out_fput;
2281 	}
2282 	path->reada = 2;
2283 
2284 	if (inode < src) {
2285 		mutex_lock_nested(&inode->i_mutex, I_MUTEX_PARENT);
2286 		mutex_lock_nested(&src->i_mutex, I_MUTEX_CHILD);
2287 	} else {
2288 		mutex_lock_nested(&src->i_mutex, I_MUTEX_PARENT);
2289 		mutex_lock_nested(&inode->i_mutex, I_MUTEX_CHILD);
2290 	}
2291 
2292 	/* determine range to clone */
2293 	ret = -EINVAL;
2294 	if (off + len > src->i_size || off + len < off)
2295 		goto out_unlock;
2296 	if (len == 0)
2297 		olen = len = src->i_size - off;
2298 	/* if we extend to eof, continue to block boundary */
2299 	if (off + len == src->i_size)
2300 		len = ALIGN(src->i_size, bs) - off;
2301 
2302 	/* verify the end result is block aligned */
2303 	if (!IS_ALIGNED(off, bs) || !IS_ALIGNED(off + len, bs) ||
2304 	    !IS_ALIGNED(destoff, bs))
2305 		goto out_unlock;
2306 
2307 	if (destoff > inode->i_size) {
2308 		ret = btrfs_cont_expand(inode, inode->i_size, destoff);
2309 		if (ret)
2310 			goto out_unlock;
2311 	}
2312 
2313 	/* truncate page cache pages from target inode range */
2314 	truncate_inode_pages_range(&inode->i_data, destoff,
2315 				   PAGE_CACHE_ALIGN(destoff + len) - 1);
2316 
2317 	/* do any pending delalloc/csum calc on src, one way or
2318 	   another, and lock file content */
2319 	while (1) {
2320 		struct btrfs_ordered_extent *ordered;
2321 		lock_extent(&BTRFS_I(src)->io_tree, off, off+len, GFP_NOFS);
2322 		ordered = btrfs_lookup_first_ordered_extent(src, off+len);
2323 		if (!ordered &&
2324 		    !test_range_bit(&BTRFS_I(src)->io_tree, off, off+len,
2325 				   EXTENT_DELALLOC, 0, NULL))
2326 			break;
2327 		unlock_extent(&BTRFS_I(src)->io_tree, off, off+len, GFP_NOFS);
2328 		if (ordered)
2329 			btrfs_put_ordered_extent(ordered);
2330 		btrfs_wait_ordered_range(src, off, len);
2331 	}
2332 
2333 	/* clone data */
2334 	key.objectid = btrfs_ino(src);
2335 	key.type = BTRFS_EXTENT_DATA_KEY;
2336 	key.offset = 0;
2337 
2338 	while (1) {
2339 		/*
2340 		 * note the key will change type as we walk through the
2341 		 * tree.
2342 		 */
2343 		ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2344 		if (ret < 0)
2345 			goto out;
2346 
2347 		nritems = btrfs_header_nritems(path->nodes[0]);
2348 		if (path->slots[0] >= nritems) {
2349 			ret = btrfs_next_leaf(root, path);
2350 			if (ret < 0)
2351 				goto out;
2352 			if (ret > 0)
2353 				break;
2354 			nritems = btrfs_header_nritems(path->nodes[0]);
2355 		}
2356 		leaf = path->nodes[0];
2357 		slot = path->slots[0];
2358 
2359 		btrfs_item_key_to_cpu(leaf, &key, slot);
2360 		if (btrfs_key_type(&key) > BTRFS_EXTENT_DATA_KEY ||
2361 		    key.objectid != btrfs_ino(src))
2362 			break;
2363 
2364 		if (btrfs_key_type(&key) == BTRFS_EXTENT_DATA_KEY) {
2365 			struct btrfs_file_extent_item *extent;
2366 			int type;
2367 			u32 size;
2368 			struct btrfs_key new_key;
2369 			u64 disko = 0, diskl = 0;
2370 			u64 datao = 0, datal = 0;
2371 			u8 comp;
2372 			u64 endoff;
2373 
2374 			size = btrfs_item_size_nr(leaf, slot);
2375 			read_extent_buffer(leaf, buf,
2376 					   btrfs_item_ptr_offset(leaf, slot),
2377 					   size);
2378 
2379 			extent = btrfs_item_ptr(leaf, slot,
2380 						struct btrfs_file_extent_item);
2381 			comp = btrfs_file_extent_compression(leaf, extent);
2382 			type = btrfs_file_extent_type(leaf, extent);
2383 			if (type == BTRFS_FILE_EXTENT_REG ||
2384 			    type == BTRFS_FILE_EXTENT_PREALLOC) {
2385 				disko = btrfs_file_extent_disk_bytenr(leaf,
2386 								      extent);
2387 				diskl = btrfs_file_extent_disk_num_bytes(leaf,
2388 								 extent);
2389 				datao = btrfs_file_extent_offset(leaf, extent);
2390 				datal = btrfs_file_extent_num_bytes(leaf,
2391 								    extent);
2392 			} else if (type == BTRFS_FILE_EXTENT_INLINE) {
2393 				/* take upper bound, may be compressed */
2394 				datal = btrfs_file_extent_ram_bytes(leaf,
2395 								    extent);
2396 			}
2397 			btrfs_release_path(path);
2398 
2399 			if (key.offset + datal <= off ||
2400 			    key.offset >= off+len)
2401 				goto next;
2402 
2403 			memcpy(&new_key, &key, sizeof(new_key));
2404 			new_key.objectid = btrfs_ino(inode);
2405 			if (off <= key.offset)
2406 				new_key.offset = key.offset + destoff - off;
2407 			else
2408 				new_key.offset = destoff;
2409 
2410 			/*
2411 			 * 1 - adjusting old extent (we may have to split it)
2412 			 * 1 - add new extent
2413 			 * 1 - inode update
2414 			 */
2415 			trans = btrfs_start_transaction(root, 3);
2416 			if (IS_ERR(trans)) {
2417 				ret = PTR_ERR(trans);
2418 				goto out;
2419 			}
2420 
2421 			if (type == BTRFS_FILE_EXTENT_REG ||
2422 			    type == BTRFS_FILE_EXTENT_PREALLOC) {
2423 				/*
2424 				 *    a  | --- range to clone ---|  b
2425 				 * | ------------- extent ------------- |
2426 				 */
2427 
2428 				/* substract range b */
2429 				if (key.offset + datal > off + len)
2430 					datal = off + len - key.offset;
2431 
2432 				/* substract range a */
2433 				if (off > key.offset) {
2434 					datao += off - key.offset;
2435 					datal -= off - key.offset;
2436 				}
2437 
2438 				ret = btrfs_drop_extents(trans, inode,
2439 							 new_key.offset,
2440 							 new_key.offset + datal,
2441 							 &hint_byte, 1);
2442 				BUG_ON(ret);
2443 
2444 				ret = btrfs_insert_empty_item(trans, root, path,
2445 							      &new_key, size);
2446 				BUG_ON(ret);
2447 
2448 				leaf = path->nodes[0];
2449 				slot = path->slots[0];
2450 				write_extent_buffer(leaf, buf,
2451 					    btrfs_item_ptr_offset(leaf, slot),
2452 					    size);
2453 
2454 				extent = btrfs_item_ptr(leaf, slot,
2455 						struct btrfs_file_extent_item);
2456 
2457 				/* disko == 0 means it's a hole */
2458 				if (!disko)
2459 					datao = 0;
2460 
2461 				btrfs_set_file_extent_offset(leaf, extent,
2462 							     datao);
2463 				btrfs_set_file_extent_num_bytes(leaf, extent,
2464 								datal);
2465 				if (disko) {
2466 					inode_add_bytes(inode, datal);
2467 					ret = btrfs_inc_extent_ref(trans, root,
2468 							disko, diskl, 0,
2469 							root->root_key.objectid,
2470 							btrfs_ino(inode),
2471 							new_key.offset - datao,
2472 							0);
2473 					BUG_ON(ret);
2474 				}
2475 			} else if (type == BTRFS_FILE_EXTENT_INLINE) {
2476 				u64 skip = 0;
2477 				u64 trim = 0;
2478 				if (off > key.offset) {
2479 					skip = off - key.offset;
2480 					new_key.offset += skip;
2481 				}
2482 
2483 				if (key.offset + datal > off+len)
2484 					trim = key.offset + datal - (off+len);
2485 
2486 				if (comp && (skip || trim)) {
2487 					ret = -EINVAL;
2488 					btrfs_end_transaction(trans, root);
2489 					goto out;
2490 				}
2491 				size -= skip + trim;
2492 				datal -= skip + trim;
2493 
2494 				ret = btrfs_drop_extents(trans, inode,
2495 							 new_key.offset,
2496 							 new_key.offset + datal,
2497 							 &hint_byte, 1);
2498 				BUG_ON(ret);
2499 
2500 				ret = btrfs_insert_empty_item(trans, root, path,
2501 							      &new_key, size);
2502 				BUG_ON(ret);
2503 
2504 				if (skip) {
2505 					u32 start =
2506 					  btrfs_file_extent_calc_inline_size(0);
2507 					memmove(buf+start, buf+start+skip,
2508 						datal);
2509 				}
2510 
2511 				leaf = path->nodes[0];
2512 				slot = path->slots[0];
2513 				write_extent_buffer(leaf, buf,
2514 					    btrfs_item_ptr_offset(leaf, slot),
2515 					    size);
2516 				inode_add_bytes(inode, datal);
2517 			}
2518 
2519 			btrfs_mark_buffer_dirty(leaf);
2520 			btrfs_release_path(path);
2521 
2522 			inode->i_mtime = inode->i_ctime = CURRENT_TIME;
2523 
2524 			/*
2525 			 * we round up to the block size at eof when
2526 			 * determining which extents to clone above,
2527 			 * but shouldn't round up the file size
2528 			 */
2529 			endoff = new_key.offset + datal;
2530 			if (endoff > destoff+olen)
2531 				endoff = destoff+olen;
2532 			if (endoff > inode->i_size)
2533 				btrfs_i_size_write(inode, endoff);
2534 
2535 			ret = btrfs_update_inode(trans, root, inode);
2536 			BUG_ON(ret);
2537 			btrfs_end_transaction(trans, root);
2538 		}
2539 next:
2540 		btrfs_release_path(path);
2541 		key.offset++;
2542 	}
2543 	ret = 0;
2544 out:
2545 	btrfs_release_path(path);
2546 	unlock_extent(&BTRFS_I(src)->io_tree, off, off+len, GFP_NOFS);
2547 out_unlock:
2548 	mutex_unlock(&src->i_mutex);
2549 	mutex_unlock(&inode->i_mutex);
2550 	vfree(buf);
2551 	btrfs_free_path(path);
2552 out_fput:
2553 	fput(src_file);
2554 out_drop_write:
2555 	mnt_drop_write(file->f_path.mnt);
2556 	return ret;
2557 }
2558 
2559 static long btrfs_ioctl_clone_range(struct file *file, void __user *argp)
2560 {
2561 	struct btrfs_ioctl_clone_range_args args;
2562 
2563 	if (copy_from_user(&args, argp, sizeof(args)))
2564 		return -EFAULT;
2565 	return btrfs_ioctl_clone(file, args.src_fd, args.src_offset,
2566 				 args.src_length, args.dest_offset);
2567 }
2568 
2569 /*
2570  * there are many ways the trans_start and trans_end ioctls can lead
2571  * to deadlocks.  They should only be used by applications that
2572  * basically own the machine, and have a very in depth understanding
2573  * of all the possible deadlocks and enospc problems.
2574  */
2575 static long btrfs_ioctl_trans_start(struct file *file)
2576 {
2577 	struct inode *inode = fdentry(file)->d_inode;
2578 	struct btrfs_root *root = BTRFS_I(inode)->root;
2579 	struct btrfs_trans_handle *trans;
2580 	int ret;
2581 
2582 	ret = -EPERM;
2583 	if (!capable(CAP_SYS_ADMIN))
2584 		goto out;
2585 
2586 	ret = -EINPROGRESS;
2587 	if (file->private_data)
2588 		goto out;
2589 
2590 	ret = -EROFS;
2591 	if (btrfs_root_readonly(root))
2592 		goto out;
2593 
2594 	ret = mnt_want_write(file->f_path.mnt);
2595 	if (ret)
2596 		goto out;
2597 
2598 	atomic_inc(&root->fs_info->open_ioctl_trans);
2599 
2600 	ret = -ENOMEM;
2601 	trans = btrfs_start_ioctl_transaction(root);
2602 	if (IS_ERR(trans))
2603 		goto out_drop;
2604 
2605 	file->private_data = trans;
2606 	return 0;
2607 
2608 out_drop:
2609 	atomic_dec(&root->fs_info->open_ioctl_trans);
2610 	mnt_drop_write(file->f_path.mnt);
2611 out:
2612 	return ret;
2613 }
2614 
2615 static long btrfs_ioctl_default_subvol(struct file *file, void __user *argp)
2616 {
2617 	struct inode *inode = fdentry(file)->d_inode;
2618 	struct btrfs_root *root = BTRFS_I(inode)->root;
2619 	struct btrfs_root *new_root;
2620 	struct btrfs_dir_item *di;
2621 	struct btrfs_trans_handle *trans;
2622 	struct btrfs_path *path;
2623 	struct btrfs_key location;
2624 	struct btrfs_disk_key disk_key;
2625 	struct btrfs_super_block *disk_super;
2626 	u64 features;
2627 	u64 objectid = 0;
2628 	u64 dir_id;
2629 
2630 	if (!capable(CAP_SYS_ADMIN))
2631 		return -EPERM;
2632 
2633 	if (copy_from_user(&objectid, argp, sizeof(objectid)))
2634 		return -EFAULT;
2635 
2636 	if (!objectid)
2637 		objectid = root->root_key.objectid;
2638 
2639 	location.objectid = objectid;
2640 	location.type = BTRFS_ROOT_ITEM_KEY;
2641 	location.offset = (u64)-1;
2642 
2643 	new_root = btrfs_read_fs_root_no_name(root->fs_info, &location);
2644 	if (IS_ERR(new_root))
2645 		return PTR_ERR(new_root);
2646 
2647 	if (btrfs_root_refs(&new_root->root_item) == 0)
2648 		return -ENOENT;
2649 
2650 	path = btrfs_alloc_path();
2651 	if (!path)
2652 		return -ENOMEM;
2653 	path->leave_spinning = 1;
2654 
2655 	trans = btrfs_start_transaction(root, 1);
2656 	if (IS_ERR(trans)) {
2657 		btrfs_free_path(path);
2658 		return PTR_ERR(trans);
2659 	}
2660 
2661 	dir_id = btrfs_super_root_dir(root->fs_info->super_copy);
2662 	di = btrfs_lookup_dir_item(trans, root->fs_info->tree_root, path,
2663 				   dir_id, "default", 7, 1);
2664 	if (IS_ERR_OR_NULL(di)) {
2665 		btrfs_free_path(path);
2666 		btrfs_end_transaction(trans, root);
2667 		printk(KERN_ERR "Umm, you don't have the default dir item, "
2668 		       "this isn't going to work\n");
2669 		return -ENOENT;
2670 	}
2671 
2672 	btrfs_cpu_key_to_disk(&disk_key, &new_root->root_key);
2673 	btrfs_set_dir_item_key(path->nodes[0], di, &disk_key);
2674 	btrfs_mark_buffer_dirty(path->nodes[0]);
2675 	btrfs_free_path(path);
2676 
2677 	disk_super = root->fs_info->super_copy;
2678 	features = btrfs_super_incompat_flags(disk_super);
2679 	if (!(features & BTRFS_FEATURE_INCOMPAT_DEFAULT_SUBVOL)) {
2680 		features |= BTRFS_FEATURE_INCOMPAT_DEFAULT_SUBVOL;
2681 		btrfs_set_super_incompat_flags(disk_super, features);
2682 	}
2683 	btrfs_end_transaction(trans, root);
2684 
2685 	return 0;
2686 }
2687 
2688 static void get_block_group_info(struct list_head *groups_list,
2689 				 struct btrfs_ioctl_space_info *space)
2690 {
2691 	struct btrfs_block_group_cache *block_group;
2692 
2693 	space->total_bytes = 0;
2694 	space->used_bytes = 0;
2695 	space->flags = 0;
2696 	list_for_each_entry(block_group, groups_list, list) {
2697 		space->flags = block_group->flags;
2698 		space->total_bytes += block_group->key.offset;
2699 		space->used_bytes +=
2700 			btrfs_block_group_used(&block_group->item);
2701 	}
2702 }
2703 
2704 long btrfs_ioctl_space_info(struct btrfs_root *root, void __user *arg)
2705 {
2706 	struct btrfs_ioctl_space_args space_args;
2707 	struct btrfs_ioctl_space_info space;
2708 	struct btrfs_ioctl_space_info *dest;
2709 	struct btrfs_ioctl_space_info *dest_orig;
2710 	struct btrfs_ioctl_space_info __user *user_dest;
2711 	struct btrfs_space_info *info;
2712 	u64 types[] = {BTRFS_BLOCK_GROUP_DATA,
2713 		       BTRFS_BLOCK_GROUP_SYSTEM,
2714 		       BTRFS_BLOCK_GROUP_METADATA,
2715 		       BTRFS_BLOCK_GROUP_DATA | BTRFS_BLOCK_GROUP_METADATA};
2716 	int num_types = 4;
2717 	int alloc_size;
2718 	int ret = 0;
2719 	u64 slot_count = 0;
2720 	int i, c;
2721 
2722 	if (copy_from_user(&space_args,
2723 			   (struct btrfs_ioctl_space_args __user *)arg,
2724 			   sizeof(space_args)))
2725 		return -EFAULT;
2726 
2727 	for (i = 0; i < num_types; i++) {
2728 		struct btrfs_space_info *tmp;
2729 
2730 		info = NULL;
2731 		rcu_read_lock();
2732 		list_for_each_entry_rcu(tmp, &root->fs_info->space_info,
2733 					list) {
2734 			if (tmp->flags == types[i]) {
2735 				info = tmp;
2736 				break;
2737 			}
2738 		}
2739 		rcu_read_unlock();
2740 
2741 		if (!info)
2742 			continue;
2743 
2744 		down_read(&info->groups_sem);
2745 		for (c = 0; c < BTRFS_NR_RAID_TYPES; c++) {
2746 			if (!list_empty(&info->block_groups[c]))
2747 				slot_count++;
2748 		}
2749 		up_read(&info->groups_sem);
2750 	}
2751 
2752 	/* space_slots == 0 means they are asking for a count */
2753 	if (space_args.space_slots == 0) {
2754 		space_args.total_spaces = slot_count;
2755 		goto out;
2756 	}
2757 
2758 	slot_count = min_t(u64, space_args.space_slots, slot_count);
2759 
2760 	alloc_size = sizeof(*dest) * slot_count;
2761 
2762 	/* we generally have at most 6 or so space infos, one for each raid
2763 	 * level.  So, a whole page should be more than enough for everyone
2764 	 */
2765 	if (alloc_size > PAGE_CACHE_SIZE)
2766 		return -ENOMEM;
2767 
2768 	space_args.total_spaces = 0;
2769 	dest = kmalloc(alloc_size, GFP_NOFS);
2770 	if (!dest)
2771 		return -ENOMEM;
2772 	dest_orig = dest;
2773 
2774 	/* now we have a buffer to copy into */
2775 	for (i = 0; i < num_types; i++) {
2776 		struct btrfs_space_info *tmp;
2777 
2778 		if (!slot_count)
2779 			break;
2780 
2781 		info = NULL;
2782 		rcu_read_lock();
2783 		list_for_each_entry_rcu(tmp, &root->fs_info->space_info,
2784 					list) {
2785 			if (tmp->flags == types[i]) {
2786 				info = tmp;
2787 				break;
2788 			}
2789 		}
2790 		rcu_read_unlock();
2791 
2792 		if (!info)
2793 			continue;
2794 		down_read(&info->groups_sem);
2795 		for (c = 0; c < BTRFS_NR_RAID_TYPES; c++) {
2796 			if (!list_empty(&info->block_groups[c])) {
2797 				get_block_group_info(&info->block_groups[c],
2798 						     &space);
2799 				memcpy(dest, &space, sizeof(space));
2800 				dest++;
2801 				space_args.total_spaces++;
2802 				slot_count--;
2803 			}
2804 			if (!slot_count)
2805 				break;
2806 		}
2807 		up_read(&info->groups_sem);
2808 	}
2809 
2810 	user_dest = (struct btrfs_ioctl_space_info *)
2811 		(arg + sizeof(struct btrfs_ioctl_space_args));
2812 
2813 	if (copy_to_user(user_dest, dest_orig, alloc_size))
2814 		ret = -EFAULT;
2815 
2816 	kfree(dest_orig);
2817 out:
2818 	if (ret == 0 && copy_to_user(arg, &space_args, sizeof(space_args)))
2819 		ret = -EFAULT;
2820 
2821 	return ret;
2822 }
2823 
2824 /*
2825  * there are many ways the trans_start and trans_end ioctls can lead
2826  * to deadlocks.  They should only be used by applications that
2827  * basically own the machine, and have a very in depth understanding
2828  * of all the possible deadlocks and enospc problems.
2829  */
2830 long btrfs_ioctl_trans_end(struct file *file)
2831 {
2832 	struct inode *inode = fdentry(file)->d_inode;
2833 	struct btrfs_root *root = BTRFS_I(inode)->root;
2834 	struct btrfs_trans_handle *trans;
2835 
2836 	trans = file->private_data;
2837 	if (!trans)
2838 		return -EINVAL;
2839 	file->private_data = NULL;
2840 
2841 	btrfs_end_transaction(trans, root);
2842 
2843 	atomic_dec(&root->fs_info->open_ioctl_trans);
2844 
2845 	mnt_drop_write(file->f_path.mnt);
2846 	return 0;
2847 }
2848 
2849 static noinline long btrfs_ioctl_start_sync(struct file *file, void __user *argp)
2850 {
2851 	struct btrfs_root *root = BTRFS_I(file->f_dentry->d_inode)->root;
2852 	struct btrfs_trans_handle *trans;
2853 	u64 transid;
2854 	int ret;
2855 
2856 	trans = btrfs_start_transaction(root, 0);
2857 	if (IS_ERR(trans))
2858 		return PTR_ERR(trans);
2859 	transid = trans->transid;
2860 	ret = btrfs_commit_transaction_async(trans, root, 0);
2861 	if (ret) {
2862 		btrfs_end_transaction(trans, root);
2863 		return ret;
2864 	}
2865 
2866 	if (argp)
2867 		if (copy_to_user(argp, &transid, sizeof(transid)))
2868 			return -EFAULT;
2869 	return 0;
2870 }
2871 
2872 static noinline long btrfs_ioctl_wait_sync(struct file *file, void __user *argp)
2873 {
2874 	struct btrfs_root *root = BTRFS_I(file->f_dentry->d_inode)->root;
2875 	u64 transid;
2876 
2877 	if (argp) {
2878 		if (copy_from_user(&transid, argp, sizeof(transid)))
2879 			return -EFAULT;
2880 	} else {
2881 		transid = 0;  /* current trans */
2882 	}
2883 	return btrfs_wait_for_commit(root, transid);
2884 }
2885 
2886 static long btrfs_ioctl_scrub(struct btrfs_root *root, void __user *arg)
2887 {
2888 	int ret;
2889 	struct btrfs_ioctl_scrub_args *sa;
2890 
2891 	if (!capable(CAP_SYS_ADMIN))
2892 		return -EPERM;
2893 
2894 	sa = memdup_user(arg, sizeof(*sa));
2895 	if (IS_ERR(sa))
2896 		return PTR_ERR(sa);
2897 
2898 	ret = btrfs_scrub_dev(root, sa->devid, sa->start, sa->end,
2899 			      &sa->progress, sa->flags & BTRFS_SCRUB_READONLY);
2900 
2901 	if (copy_to_user(arg, sa, sizeof(*sa)))
2902 		ret = -EFAULT;
2903 
2904 	kfree(sa);
2905 	return ret;
2906 }
2907 
2908 static long btrfs_ioctl_scrub_cancel(struct btrfs_root *root, void __user *arg)
2909 {
2910 	if (!capable(CAP_SYS_ADMIN))
2911 		return -EPERM;
2912 
2913 	return btrfs_scrub_cancel(root);
2914 }
2915 
2916 static long btrfs_ioctl_scrub_progress(struct btrfs_root *root,
2917 				       void __user *arg)
2918 {
2919 	struct btrfs_ioctl_scrub_args *sa;
2920 	int ret;
2921 
2922 	if (!capable(CAP_SYS_ADMIN))
2923 		return -EPERM;
2924 
2925 	sa = memdup_user(arg, sizeof(*sa));
2926 	if (IS_ERR(sa))
2927 		return PTR_ERR(sa);
2928 
2929 	ret = btrfs_scrub_progress(root, sa->devid, &sa->progress);
2930 
2931 	if (copy_to_user(arg, sa, sizeof(*sa)))
2932 		ret = -EFAULT;
2933 
2934 	kfree(sa);
2935 	return ret;
2936 }
2937 
2938 static long btrfs_ioctl_ino_to_path(struct btrfs_root *root, void __user *arg)
2939 {
2940 	int ret = 0;
2941 	int i;
2942 	u64 rel_ptr;
2943 	int size;
2944 	struct btrfs_ioctl_ino_path_args *ipa = NULL;
2945 	struct inode_fs_paths *ipath = NULL;
2946 	struct btrfs_path *path;
2947 
2948 	if (!capable(CAP_SYS_ADMIN))
2949 		return -EPERM;
2950 
2951 	path = btrfs_alloc_path();
2952 	if (!path) {
2953 		ret = -ENOMEM;
2954 		goto out;
2955 	}
2956 
2957 	ipa = memdup_user(arg, sizeof(*ipa));
2958 	if (IS_ERR(ipa)) {
2959 		ret = PTR_ERR(ipa);
2960 		ipa = NULL;
2961 		goto out;
2962 	}
2963 
2964 	size = min_t(u32, ipa->size, 4096);
2965 	ipath = init_ipath(size, root, path);
2966 	if (IS_ERR(ipath)) {
2967 		ret = PTR_ERR(ipath);
2968 		ipath = NULL;
2969 		goto out;
2970 	}
2971 
2972 	ret = paths_from_inode(ipa->inum, ipath);
2973 	if (ret < 0)
2974 		goto out;
2975 
2976 	for (i = 0; i < ipath->fspath->elem_cnt; ++i) {
2977 		rel_ptr = ipath->fspath->val[i] -
2978 			  (u64)(unsigned long)ipath->fspath->val;
2979 		ipath->fspath->val[i] = rel_ptr;
2980 	}
2981 
2982 	ret = copy_to_user((void *)(unsigned long)ipa->fspath,
2983 			   (void *)(unsigned long)ipath->fspath, size);
2984 	if (ret) {
2985 		ret = -EFAULT;
2986 		goto out;
2987 	}
2988 
2989 out:
2990 	btrfs_free_path(path);
2991 	free_ipath(ipath);
2992 	kfree(ipa);
2993 
2994 	return ret;
2995 }
2996 
2997 static int build_ino_list(u64 inum, u64 offset, u64 root, void *ctx)
2998 {
2999 	struct btrfs_data_container *inodes = ctx;
3000 	const size_t c = 3 * sizeof(u64);
3001 
3002 	if (inodes->bytes_left >= c) {
3003 		inodes->bytes_left -= c;
3004 		inodes->val[inodes->elem_cnt] = inum;
3005 		inodes->val[inodes->elem_cnt + 1] = offset;
3006 		inodes->val[inodes->elem_cnt + 2] = root;
3007 		inodes->elem_cnt += 3;
3008 	} else {
3009 		inodes->bytes_missing += c - inodes->bytes_left;
3010 		inodes->bytes_left = 0;
3011 		inodes->elem_missed += 3;
3012 	}
3013 
3014 	return 0;
3015 }
3016 
3017 static long btrfs_ioctl_logical_to_ino(struct btrfs_root *root,
3018 					void __user *arg)
3019 {
3020 	int ret = 0;
3021 	int size;
3022 	u64 extent_item_pos;
3023 	struct btrfs_ioctl_logical_ino_args *loi;
3024 	struct btrfs_data_container *inodes = NULL;
3025 	struct btrfs_path *path = NULL;
3026 	struct btrfs_key key;
3027 
3028 	if (!capable(CAP_SYS_ADMIN))
3029 		return -EPERM;
3030 
3031 	loi = memdup_user(arg, sizeof(*loi));
3032 	if (IS_ERR(loi)) {
3033 		ret = PTR_ERR(loi);
3034 		loi = NULL;
3035 		goto out;
3036 	}
3037 
3038 	path = btrfs_alloc_path();
3039 	if (!path) {
3040 		ret = -ENOMEM;
3041 		goto out;
3042 	}
3043 
3044 	size = min_t(u32, loi->size, 4096);
3045 	inodes = init_data_container(size);
3046 	if (IS_ERR(inodes)) {
3047 		ret = PTR_ERR(inodes);
3048 		inodes = NULL;
3049 		goto out;
3050 	}
3051 
3052 	ret = extent_from_logical(root->fs_info, loi->logical, path, &key);
3053 	btrfs_release_path(path);
3054 
3055 	if (ret & BTRFS_EXTENT_FLAG_TREE_BLOCK)
3056 		ret = -ENOENT;
3057 	if (ret < 0)
3058 		goto out;
3059 
3060 	extent_item_pos = loi->logical - key.objectid;
3061 	ret = iterate_extent_inodes(root->fs_info, path, key.objectid,
3062 					extent_item_pos, build_ino_list,
3063 					inodes);
3064 
3065 	if (ret < 0)
3066 		goto out;
3067 
3068 	ret = copy_to_user((void *)(unsigned long)loi->inodes,
3069 			   (void *)(unsigned long)inodes, size);
3070 	if (ret)
3071 		ret = -EFAULT;
3072 
3073 out:
3074 	btrfs_free_path(path);
3075 	kfree(inodes);
3076 	kfree(loi);
3077 
3078 	return ret;
3079 }
3080 
3081 void update_ioctl_balance_args(struct btrfs_fs_info *fs_info, int lock,
3082 			       struct btrfs_ioctl_balance_args *bargs)
3083 {
3084 	struct btrfs_balance_control *bctl = fs_info->balance_ctl;
3085 
3086 	bargs->flags = bctl->flags;
3087 
3088 	if (atomic_read(&fs_info->balance_running))
3089 		bargs->state |= BTRFS_BALANCE_STATE_RUNNING;
3090 	if (atomic_read(&fs_info->balance_pause_req))
3091 		bargs->state |= BTRFS_BALANCE_STATE_PAUSE_REQ;
3092 	if (atomic_read(&fs_info->balance_cancel_req))
3093 		bargs->state |= BTRFS_BALANCE_STATE_CANCEL_REQ;
3094 
3095 	memcpy(&bargs->data, &bctl->data, sizeof(bargs->data));
3096 	memcpy(&bargs->meta, &bctl->meta, sizeof(bargs->meta));
3097 	memcpy(&bargs->sys, &bctl->sys, sizeof(bargs->sys));
3098 
3099 	if (lock) {
3100 		spin_lock(&fs_info->balance_lock);
3101 		memcpy(&bargs->stat, &bctl->stat, sizeof(bargs->stat));
3102 		spin_unlock(&fs_info->balance_lock);
3103 	} else {
3104 		memcpy(&bargs->stat, &bctl->stat, sizeof(bargs->stat));
3105 	}
3106 }
3107 
3108 static long btrfs_ioctl_balance(struct btrfs_root *root, void __user *arg)
3109 {
3110 	struct btrfs_fs_info *fs_info = root->fs_info;
3111 	struct btrfs_ioctl_balance_args *bargs;
3112 	struct btrfs_balance_control *bctl;
3113 	int ret;
3114 
3115 	if (!capable(CAP_SYS_ADMIN))
3116 		return -EPERM;
3117 
3118 	if (fs_info->sb->s_flags & MS_RDONLY)
3119 		return -EROFS;
3120 
3121 	mutex_lock(&fs_info->volume_mutex);
3122 	mutex_lock(&fs_info->balance_mutex);
3123 
3124 	if (arg) {
3125 		bargs = memdup_user(arg, sizeof(*bargs));
3126 		if (IS_ERR(bargs)) {
3127 			ret = PTR_ERR(bargs);
3128 			goto out;
3129 		}
3130 
3131 		if (bargs->flags & BTRFS_BALANCE_RESUME) {
3132 			if (!fs_info->balance_ctl) {
3133 				ret = -ENOTCONN;
3134 				goto out_bargs;
3135 			}
3136 
3137 			bctl = fs_info->balance_ctl;
3138 			spin_lock(&fs_info->balance_lock);
3139 			bctl->flags |= BTRFS_BALANCE_RESUME;
3140 			spin_unlock(&fs_info->balance_lock);
3141 
3142 			goto do_balance;
3143 		}
3144 	} else {
3145 		bargs = NULL;
3146 	}
3147 
3148 	if (fs_info->balance_ctl) {
3149 		ret = -EINPROGRESS;
3150 		goto out_bargs;
3151 	}
3152 
3153 	bctl = kzalloc(sizeof(*bctl), GFP_NOFS);
3154 	if (!bctl) {
3155 		ret = -ENOMEM;
3156 		goto out_bargs;
3157 	}
3158 
3159 	bctl->fs_info = fs_info;
3160 	if (arg) {
3161 		memcpy(&bctl->data, &bargs->data, sizeof(bctl->data));
3162 		memcpy(&bctl->meta, &bargs->meta, sizeof(bctl->meta));
3163 		memcpy(&bctl->sys, &bargs->sys, sizeof(bctl->sys));
3164 
3165 		bctl->flags = bargs->flags;
3166 	} else {
3167 		/* balance everything - no filters */
3168 		bctl->flags |= BTRFS_BALANCE_TYPE_MASK;
3169 	}
3170 
3171 do_balance:
3172 	ret = btrfs_balance(bctl, bargs);
3173 	/*
3174 	 * bctl is freed in __cancel_balance or in free_fs_info if
3175 	 * restriper was paused all the way until unmount
3176 	 */
3177 	if (arg) {
3178 		if (copy_to_user(arg, bargs, sizeof(*bargs)))
3179 			ret = -EFAULT;
3180 	}
3181 
3182 out_bargs:
3183 	kfree(bargs);
3184 out:
3185 	mutex_unlock(&fs_info->balance_mutex);
3186 	mutex_unlock(&fs_info->volume_mutex);
3187 	return ret;
3188 }
3189 
3190 static long btrfs_ioctl_balance_ctl(struct btrfs_root *root, int cmd)
3191 {
3192 	if (!capable(CAP_SYS_ADMIN))
3193 		return -EPERM;
3194 
3195 	switch (cmd) {
3196 	case BTRFS_BALANCE_CTL_PAUSE:
3197 		return btrfs_pause_balance(root->fs_info);
3198 	case BTRFS_BALANCE_CTL_CANCEL:
3199 		return btrfs_cancel_balance(root->fs_info);
3200 	}
3201 
3202 	return -EINVAL;
3203 }
3204 
3205 static long btrfs_ioctl_balance_progress(struct btrfs_root *root,
3206 					 void __user *arg)
3207 {
3208 	struct btrfs_fs_info *fs_info = root->fs_info;
3209 	struct btrfs_ioctl_balance_args *bargs;
3210 	int ret = 0;
3211 
3212 	if (!capable(CAP_SYS_ADMIN))
3213 		return -EPERM;
3214 
3215 	mutex_lock(&fs_info->balance_mutex);
3216 	if (!fs_info->balance_ctl) {
3217 		ret = -ENOTCONN;
3218 		goto out;
3219 	}
3220 
3221 	bargs = kzalloc(sizeof(*bargs), GFP_NOFS);
3222 	if (!bargs) {
3223 		ret = -ENOMEM;
3224 		goto out;
3225 	}
3226 
3227 	update_ioctl_balance_args(fs_info, 1, bargs);
3228 
3229 	if (copy_to_user(arg, bargs, sizeof(*bargs)))
3230 		ret = -EFAULT;
3231 
3232 	kfree(bargs);
3233 out:
3234 	mutex_unlock(&fs_info->balance_mutex);
3235 	return ret;
3236 }
3237 
3238 long btrfs_ioctl(struct file *file, unsigned int
3239 		cmd, unsigned long arg)
3240 {
3241 	struct btrfs_root *root = BTRFS_I(fdentry(file)->d_inode)->root;
3242 	void __user *argp = (void __user *)arg;
3243 
3244 	switch (cmd) {
3245 	case FS_IOC_GETFLAGS:
3246 		return btrfs_ioctl_getflags(file, argp);
3247 	case FS_IOC_SETFLAGS:
3248 		return btrfs_ioctl_setflags(file, argp);
3249 	case FS_IOC_GETVERSION:
3250 		return btrfs_ioctl_getversion(file, argp);
3251 	case FITRIM:
3252 		return btrfs_ioctl_fitrim(file, argp);
3253 	case BTRFS_IOC_SNAP_CREATE:
3254 		return btrfs_ioctl_snap_create(file, argp, 0);
3255 	case BTRFS_IOC_SNAP_CREATE_V2:
3256 		return btrfs_ioctl_snap_create_v2(file, argp, 0);
3257 	case BTRFS_IOC_SUBVOL_CREATE:
3258 		return btrfs_ioctl_snap_create(file, argp, 1);
3259 	case BTRFS_IOC_SNAP_DESTROY:
3260 		return btrfs_ioctl_snap_destroy(file, argp);
3261 	case BTRFS_IOC_SUBVOL_GETFLAGS:
3262 		return btrfs_ioctl_subvol_getflags(file, argp);
3263 	case BTRFS_IOC_SUBVOL_SETFLAGS:
3264 		return btrfs_ioctl_subvol_setflags(file, argp);
3265 	case BTRFS_IOC_DEFAULT_SUBVOL:
3266 		return btrfs_ioctl_default_subvol(file, argp);
3267 	case BTRFS_IOC_DEFRAG:
3268 		return btrfs_ioctl_defrag(file, NULL);
3269 	case BTRFS_IOC_DEFRAG_RANGE:
3270 		return btrfs_ioctl_defrag(file, argp);
3271 	case BTRFS_IOC_RESIZE:
3272 		return btrfs_ioctl_resize(root, argp);
3273 	case BTRFS_IOC_ADD_DEV:
3274 		return btrfs_ioctl_add_dev(root, argp);
3275 	case BTRFS_IOC_RM_DEV:
3276 		return btrfs_ioctl_rm_dev(root, argp);
3277 	case BTRFS_IOC_FS_INFO:
3278 		return btrfs_ioctl_fs_info(root, argp);
3279 	case BTRFS_IOC_DEV_INFO:
3280 		return btrfs_ioctl_dev_info(root, argp);
3281 	case BTRFS_IOC_BALANCE:
3282 		return btrfs_ioctl_balance(root, NULL);
3283 	case BTRFS_IOC_CLONE:
3284 		return btrfs_ioctl_clone(file, arg, 0, 0, 0);
3285 	case BTRFS_IOC_CLONE_RANGE:
3286 		return btrfs_ioctl_clone_range(file, argp);
3287 	case BTRFS_IOC_TRANS_START:
3288 		return btrfs_ioctl_trans_start(file);
3289 	case BTRFS_IOC_TRANS_END:
3290 		return btrfs_ioctl_trans_end(file);
3291 	case BTRFS_IOC_TREE_SEARCH:
3292 		return btrfs_ioctl_tree_search(file, argp);
3293 	case BTRFS_IOC_INO_LOOKUP:
3294 		return btrfs_ioctl_ino_lookup(file, argp);
3295 	case BTRFS_IOC_INO_PATHS:
3296 		return btrfs_ioctl_ino_to_path(root, argp);
3297 	case BTRFS_IOC_LOGICAL_INO:
3298 		return btrfs_ioctl_logical_to_ino(root, argp);
3299 	case BTRFS_IOC_SPACE_INFO:
3300 		return btrfs_ioctl_space_info(root, argp);
3301 	case BTRFS_IOC_SYNC:
3302 		btrfs_sync_fs(file->f_dentry->d_sb, 1);
3303 		return 0;
3304 	case BTRFS_IOC_START_SYNC:
3305 		return btrfs_ioctl_start_sync(file, argp);
3306 	case BTRFS_IOC_WAIT_SYNC:
3307 		return btrfs_ioctl_wait_sync(file, argp);
3308 	case BTRFS_IOC_SCRUB:
3309 		return btrfs_ioctl_scrub(root, argp);
3310 	case BTRFS_IOC_SCRUB_CANCEL:
3311 		return btrfs_ioctl_scrub_cancel(root, argp);
3312 	case BTRFS_IOC_SCRUB_PROGRESS:
3313 		return btrfs_ioctl_scrub_progress(root, argp);
3314 	case BTRFS_IOC_BALANCE_V2:
3315 		return btrfs_ioctl_balance(root, argp);
3316 	case BTRFS_IOC_BALANCE_CTL:
3317 		return btrfs_ioctl_balance_ctl(root, arg);
3318 	case BTRFS_IOC_BALANCE_PROGRESS:
3319 		return btrfs_ioctl_balance_progress(root, argp);
3320 	}
3321 
3322 	return -ENOTTY;
3323 }
3324