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