xref: /openbmc/linux/fs/btrfs/ioctl.c (revision 06d352f2)
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 "compat.h"
43 #include "ctree.h"
44 #include "disk-io.h"
45 #include "transaction.h"
46 #include "btrfs_inode.h"
47 #include "ioctl.h"
48 #include "print-tree.h"
49 #include "volumes.h"
50 #include "locking.h"
51 
52 /* Mask out flags that are inappropriate for the given type of inode. */
53 static inline __u32 btrfs_mask_flags(umode_t mode, __u32 flags)
54 {
55 	if (S_ISDIR(mode))
56 		return flags;
57 	else if (S_ISREG(mode))
58 		return flags & ~FS_DIRSYNC_FL;
59 	else
60 		return flags & (FS_NODUMP_FL | FS_NOATIME_FL);
61 }
62 
63 /*
64  * Export inode flags to the format expected by the FS_IOC_GETFLAGS ioctl.
65  */
66 static unsigned int btrfs_flags_to_ioctl(unsigned int flags)
67 {
68 	unsigned int iflags = 0;
69 
70 	if (flags & BTRFS_INODE_SYNC)
71 		iflags |= FS_SYNC_FL;
72 	if (flags & BTRFS_INODE_IMMUTABLE)
73 		iflags |= FS_IMMUTABLE_FL;
74 	if (flags & BTRFS_INODE_APPEND)
75 		iflags |= FS_APPEND_FL;
76 	if (flags & BTRFS_INODE_NODUMP)
77 		iflags |= FS_NODUMP_FL;
78 	if (flags & BTRFS_INODE_NOATIME)
79 		iflags |= FS_NOATIME_FL;
80 	if (flags & BTRFS_INODE_DIRSYNC)
81 		iflags |= FS_DIRSYNC_FL;
82 
83 	return iflags;
84 }
85 
86 /*
87  * Update inode->i_flags based on the btrfs internal flags.
88  */
89 void btrfs_update_iflags(struct inode *inode)
90 {
91 	struct btrfs_inode *ip = BTRFS_I(inode);
92 
93 	inode->i_flags &= ~(S_SYNC|S_APPEND|S_IMMUTABLE|S_NOATIME|S_DIRSYNC);
94 
95 	if (ip->flags & BTRFS_INODE_SYNC)
96 		inode->i_flags |= S_SYNC;
97 	if (ip->flags & BTRFS_INODE_IMMUTABLE)
98 		inode->i_flags |= S_IMMUTABLE;
99 	if (ip->flags & BTRFS_INODE_APPEND)
100 		inode->i_flags |= S_APPEND;
101 	if (ip->flags & BTRFS_INODE_NOATIME)
102 		inode->i_flags |= S_NOATIME;
103 	if (ip->flags & BTRFS_INODE_DIRSYNC)
104 		inode->i_flags |= S_DIRSYNC;
105 }
106 
107 /*
108  * Inherit flags from the parent inode.
109  *
110  * Unlike extN we don't have any flags we don't want to inherit currently.
111  */
112 void btrfs_inherit_iflags(struct inode *inode, struct inode *dir)
113 {
114 	unsigned int flags;
115 
116 	if (!dir)
117 		return;
118 
119 	flags = BTRFS_I(dir)->flags;
120 
121 	if (S_ISREG(inode->i_mode))
122 		flags &= ~BTRFS_INODE_DIRSYNC;
123 	else if (!S_ISDIR(inode->i_mode))
124 		flags &= (BTRFS_INODE_NODUMP | BTRFS_INODE_NOATIME);
125 
126 	BTRFS_I(inode)->flags = flags;
127 	btrfs_update_iflags(inode);
128 }
129 
130 static int btrfs_ioctl_getflags(struct file *file, void __user *arg)
131 {
132 	struct btrfs_inode *ip = BTRFS_I(file->f_path.dentry->d_inode);
133 	unsigned int flags = btrfs_flags_to_ioctl(ip->flags);
134 
135 	if (copy_to_user(arg, &flags, sizeof(flags)))
136 		return -EFAULT;
137 	return 0;
138 }
139 
140 static int btrfs_ioctl_setflags(struct file *file, void __user *arg)
141 {
142 	struct inode *inode = file->f_path.dentry->d_inode;
143 	struct btrfs_inode *ip = BTRFS_I(inode);
144 	struct btrfs_root *root = ip->root;
145 	struct btrfs_trans_handle *trans;
146 	unsigned int flags, oldflags;
147 	int ret;
148 
149 	if (copy_from_user(&flags, arg, sizeof(flags)))
150 		return -EFAULT;
151 
152 	if (flags & ~(FS_IMMUTABLE_FL | FS_APPEND_FL | \
153 		      FS_NOATIME_FL | FS_NODUMP_FL | \
154 		      FS_SYNC_FL | FS_DIRSYNC_FL))
155 		return -EOPNOTSUPP;
156 
157 	if (!is_owner_or_cap(inode))
158 		return -EACCES;
159 
160 	mutex_lock(&inode->i_mutex);
161 
162 	flags = btrfs_mask_flags(inode->i_mode, flags);
163 	oldflags = btrfs_flags_to_ioctl(ip->flags);
164 	if ((flags ^ oldflags) & (FS_APPEND_FL | FS_IMMUTABLE_FL)) {
165 		if (!capable(CAP_LINUX_IMMUTABLE)) {
166 			ret = -EPERM;
167 			goto out_unlock;
168 		}
169 	}
170 
171 	ret = mnt_want_write(file->f_path.mnt);
172 	if (ret)
173 		goto out_unlock;
174 
175 	if (flags & FS_SYNC_FL)
176 		ip->flags |= BTRFS_INODE_SYNC;
177 	else
178 		ip->flags &= ~BTRFS_INODE_SYNC;
179 	if (flags & FS_IMMUTABLE_FL)
180 		ip->flags |= BTRFS_INODE_IMMUTABLE;
181 	else
182 		ip->flags &= ~BTRFS_INODE_IMMUTABLE;
183 	if (flags & FS_APPEND_FL)
184 		ip->flags |= BTRFS_INODE_APPEND;
185 	else
186 		ip->flags &= ~BTRFS_INODE_APPEND;
187 	if (flags & FS_NODUMP_FL)
188 		ip->flags |= BTRFS_INODE_NODUMP;
189 	else
190 		ip->flags &= ~BTRFS_INODE_NODUMP;
191 	if (flags & FS_NOATIME_FL)
192 		ip->flags |= BTRFS_INODE_NOATIME;
193 	else
194 		ip->flags &= ~BTRFS_INODE_NOATIME;
195 	if (flags & FS_DIRSYNC_FL)
196 		ip->flags |= BTRFS_INODE_DIRSYNC;
197 	else
198 		ip->flags &= ~BTRFS_INODE_DIRSYNC;
199 
200 
201 	trans = btrfs_join_transaction(root, 1);
202 	BUG_ON(!trans);
203 
204 	ret = btrfs_update_inode(trans, root, inode);
205 	BUG_ON(ret);
206 
207 	btrfs_update_iflags(inode);
208 	inode->i_ctime = CURRENT_TIME;
209 	btrfs_end_transaction(trans, root);
210 
211 	mnt_drop_write(file->f_path.mnt);
212  out_unlock:
213 	mutex_unlock(&inode->i_mutex);
214 	return 0;
215 }
216 
217 static int btrfs_ioctl_getversion(struct file *file, int __user *arg)
218 {
219 	struct inode *inode = file->f_path.dentry->d_inode;
220 
221 	return put_user(inode->i_generation, arg);
222 }
223 
224 static noinline int create_subvol(struct btrfs_root *root,
225 				  struct dentry *dentry,
226 				  char *name, int namelen)
227 {
228 	struct btrfs_trans_handle *trans;
229 	struct btrfs_key key;
230 	struct btrfs_root_item root_item;
231 	struct btrfs_inode_item *inode_item;
232 	struct extent_buffer *leaf;
233 	struct btrfs_root *new_root;
234 	struct inode *dir = dentry->d_parent->d_inode;
235 	int ret;
236 	int err;
237 	u64 objectid;
238 	u64 new_dirid = BTRFS_FIRST_FREE_OBJECTID;
239 	u64 index = 0;
240 
241 	/*
242 	 * 1 - inode item
243 	 * 2 - refs
244 	 * 1 - root item
245 	 * 2 - dir items
246 	 */
247 	ret = btrfs_reserve_metadata_space(root, 6);
248 	if (ret)
249 		return ret;
250 
251 	trans = btrfs_start_transaction(root, 1);
252 	BUG_ON(!trans);
253 
254 	ret = btrfs_find_free_objectid(trans, root->fs_info->tree_root,
255 				       0, &objectid);
256 	if (ret)
257 		goto fail;
258 
259 	leaf = btrfs_alloc_free_block(trans, root, root->leafsize,
260 				      0, objectid, NULL, 0, 0, 0);
261 	if (IS_ERR(leaf)) {
262 		ret = PTR_ERR(leaf);
263 		goto fail;
264 	}
265 
266 	memset_extent_buffer(leaf, 0, 0, sizeof(struct btrfs_header));
267 	btrfs_set_header_bytenr(leaf, leaf->start);
268 	btrfs_set_header_generation(leaf, trans->transid);
269 	btrfs_set_header_backref_rev(leaf, BTRFS_MIXED_BACKREF_REV);
270 	btrfs_set_header_owner(leaf, objectid);
271 
272 	write_extent_buffer(leaf, root->fs_info->fsid,
273 			    (unsigned long)btrfs_header_fsid(leaf),
274 			    BTRFS_FSID_SIZE);
275 	write_extent_buffer(leaf, root->fs_info->chunk_tree_uuid,
276 			    (unsigned long)btrfs_header_chunk_tree_uuid(leaf),
277 			    BTRFS_UUID_SIZE);
278 	btrfs_mark_buffer_dirty(leaf);
279 
280 	inode_item = &root_item.inode;
281 	memset(inode_item, 0, sizeof(*inode_item));
282 	inode_item->generation = cpu_to_le64(1);
283 	inode_item->size = cpu_to_le64(3);
284 	inode_item->nlink = cpu_to_le32(1);
285 	inode_item->nbytes = cpu_to_le64(root->leafsize);
286 	inode_item->mode = cpu_to_le32(S_IFDIR | 0755);
287 
288 	btrfs_set_root_bytenr(&root_item, leaf->start);
289 	btrfs_set_root_generation(&root_item, trans->transid);
290 	btrfs_set_root_level(&root_item, 0);
291 	btrfs_set_root_refs(&root_item, 1);
292 	btrfs_set_root_used(&root_item, leaf->len);
293 	btrfs_set_root_last_snapshot(&root_item, 0);
294 
295 	memset(&root_item.drop_progress, 0, sizeof(root_item.drop_progress));
296 	root_item.drop_level = 0;
297 
298 	btrfs_tree_unlock(leaf);
299 	free_extent_buffer(leaf);
300 	leaf = NULL;
301 
302 	btrfs_set_root_dirid(&root_item, new_dirid);
303 
304 	key.objectid = objectid;
305 	key.offset = 0;
306 	btrfs_set_key_type(&key, BTRFS_ROOT_ITEM_KEY);
307 	ret = btrfs_insert_root(trans, root->fs_info->tree_root, &key,
308 				&root_item);
309 	if (ret)
310 		goto fail;
311 
312 	key.offset = (u64)-1;
313 	new_root = btrfs_read_fs_root_no_name(root->fs_info, &key);
314 	BUG_ON(IS_ERR(new_root));
315 
316 	btrfs_record_root_in_trans(trans, new_root);
317 
318 	ret = btrfs_create_subvol_root(trans, new_root, new_dirid,
319 				       BTRFS_I(dir)->block_group);
320 	/*
321 	 * insert the directory item
322 	 */
323 	ret = btrfs_set_inode_index(dir, &index);
324 	BUG_ON(ret);
325 
326 	ret = btrfs_insert_dir_item(trans, root,
327 				    name, namelen, dir->i_ino, &key,
328 				    BTRFS_FT_DIR, index);
329 	if (ret)
330 		goto fail;
331 
332 	btrfs_i_size_write(dir, dir->i_size + namelen * 2);
333 	ret = btrfs_update_inode(trans, root, dir);
334 	BUG_ON(ret);
335 
336 	ret = btrfs_add_root_ref(trans, root->fs_info->tree_root,
337 				 objectid, root->root_key.objectid,
338 				 dir->i_ino, index, name, namelen);
339 
340 	BUG_ON(ret);
341 
342 	d_instantiate(dentry, btrfs_lookup_dentry(dir, dentry));
343 fail:
344 	err = btrfs_commit_transaction(trans, root);
345 	if (err && !ret)
346 		ret = err;
347 
348 	btrfs_unreserve_metadata_space(root, 6);
349 	return ret;
350 }
351 
352 static int create_snapshot(struct btrfs_root *root, struct dentry *dentry,
353 			   char *name, int namelen)
354 {
355 	struct inode *inode;
356 	struct btrfs_pending_snapshot *pending_snapshot;
357 	struct btrfs_trans_handle *trans;
358 	int ret;
359 
360 	if (!root->ref_cows)
361 		return -EINVAL;
362 
363 	/*
364 	 * 1 - inode item
365 	 * 2 - refs
366 	 * 1 - root item
367 	 * 2 - dir items
368 	 */
369 	ret = btrfs_reserve_metadata_space(root, 6);
370 	if (ret)
371 		goto fail;
372 
373 	pending_snapshot = kzalloc(sizeof(*pending_snapshot), GFP_NOFS);
374 	if (!pending_snapshot) {
375 		ret = -ENOMEM;
376 		btrfs_unreserve_metadata_space(root, 6);
377 		goto fail;
378 	}
379 	pending_snapshot->name = kmalloc(namelen + 1, GFP_NOFS);
380 	if (!pending_snapshot->name) {
381 		ret = -ENOMEM;
382 		kfree(pending_snapshot);
383 		btrfs_unreserve_metadata_space(root, 6);
384 		goto fail;
385 	}
386 	memcpy(pending_snapshot->name, name, namelen);
387 	pending_snapshot->name[namelen] = '\0';
388 	pending_snapshot->dentry = dentry;
389 	trans = btrfs_start_transaction(root, 1);
390 	BUG_ON(!trans);
391 	pending_snapshot->root = root;
392 	list_add(&pending_snapshot->list,
393 		 &trans->transaction->pending_snapshots);
394 	ret = btrfs_commit_transaction(trans, root);
395 	BUG_ON(ret);
396 	btrfs_unreserve_metadata_space(root, 6);
397 
398 	inode = btrfs_lookup_dentry(dentry->d_parent->d_inode, dentry);
399 	if (IS_ERR(inode)) {
400 		ret = PTR_ERR(inode);
401 		goto fail;
402 	}
403 	BUG_ON(!inode);
404 	d_instantiate(dentry, inode);
405 	ret = 0;
406 fail:
407 	return ret;
408 }
409 
410 /* copy of may_create in fs/namei.c() */
411 static inline int btrfs_may_create(struct inode *dir, struct dentry *child)
412 {
413 	if (child->d_inode)
414 		return -EEXIST;
415 	if (IS_DEADDIR(dir))
416 		return -ENOENT;
417 	return inode_permission(dir, MAY_WRITE | MAY_EXEC);
418 }
419 
420 /*
421  * Create a new subvolume below @parent.  This is largely modeled after
422  * sys_mkdirat and vfs_mkdir, but we only do a single component lookup
423  * inside this filesystem so it's quite a bit simpler.
424  */
425 static noinline int btrfs_mksubvol(struct path *parent,
426 				   char *name, int namelen,
427 				   struct btrfs_root *snap_src)
428 {
429 	struct inode *dir  = parent->dentry->d_inode;
430 	struct dentry *dentry;
431 	int error;
432 
433 	mutex_lock_nested(&dir->i_mutex, I_MUTEX_PARENT);
434 
435 	dentry = lookup_one_len(name, parent->dentry, namelen);
436 	error = PTR_ERR(dentry);
437 	if (IS_ERR(dentry))
438 		goto out_unlock;
439 
440 	error = -EEXIST;
441 	if (dentry->d_inode)
442 		goto out_dput;
443 
444 	error = mnt_want_write(parent->mnt);
445 	if (error)
446 		goto out_dput;
447 
448 	error = btrfs_may_create(dir, dentry);
449 	if (error)
450 		goto out_drop_write;
451 
452 	down_read(&BTRFS_I(dir)->root->fs_info->subvol_sem);
453 
454 	if (btrfs_root_refs(&BTRFS_I(dir)->root->root_item) == 0)
455 		goto out_up_read;
456 
457 	if (snap_src) {
458 		error = create_snapshot(snap_src, dentry,
459 					name, namelen);
460 	} else {
461 		error = create_subvol(BTRFS_I(dir)->root, dentry,
462 				      name, namelen);
463 	}
464 	if (!error)
465 		fsnotify_mkdir(dir, dentry);
466 out_up_read:
467 	up_read(&BTRFS_I(dir)->root->fs_info->subvol_sem);
468 out_drop_write:
469 	mnt_drop_write(parent->mnt);
470 out_dput:
471 	dput(dentry);
472 out_unlock:
473 	mutex_unlock(&dir->i_mutex);
474 	return error;
475 }
476 
477 static int btrfs_defrag_file(struct file *file)
478 {
479 	struct inode *inode = fdentry(file)->d_inode;
480 	struct btrfs_root *root = BTRFS_I(inode)->root;
481 	struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
482 	struct btrfs_ordered_extent *ordered;
483 	struct page *page;
484 	unsigned long last_index;
485 	unsigned long ra_pages = root->fs_info->bdi.ra_pages;
486 	unsigned long total_read = 0;
487 	u64 page_start;
488 	u64 page_end;
489 	unsigned long i;
490 	int ret;
491 
492 	ret = btrfs_check_data_free_space(root, inode, inode->i_size);
493 	if (ret)
494 		return -ENOSPC;
495 
496 	mutex_lock(&inode->i_mutex);
497 	last_index = inode->i_size >> PAGE_CACHE_SHIFT;
498 	for (i = 0; i <= last_index; i++) {
499 		if (total_read % ra_pages == 0) {
500 			btrfs_force_ra(inode->i_mapping, &file->f_ra, file, i,
501 				       min(last_index, i + ra_pages - 1));
502 		}
503 		total_read++;
504 again:
505 		page = grab_cache_page(inode->i_mapping, i);
506 		if (!page)
507 			goto out_unlock;
508 		if (!PageUptodate(page)) {
509 			btrfs_readpage(NULL, page);
510 			lock_page(page);
511 			if (!PageUptodate(page)) {
512 				unlock_page(page);
513 				page_cache_release(page);
514 				goto out_unlock;
515 			}
516 		}
517 
518 		wait_on_page_writeback(page);
519 
520 		page_start = (u64)page->index << PAGE_CACHE_SHIFT;
521 		page_end = page_start + PAGE_CACHE_SIZE - 1;
522 		lock_extent(io_tree, page_start, page_end, GFP_NOFS);
523 
524 		ordered = btrfs_lookup_ordered_extent(inode, page_start);
525 		if (ordered) {
526 			unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
527 			unlock_page(page);
528 			page_cache_release(page);
529 			btrfs_start_ordered_extent(inode, ordered, 1);
530 			btrfs_put_ordered_extent(ordered);
531 			goto again;
532 		}
533 		set_page_extent_mapped(page);
534 
535 		/*
536 		 * this makes sure page_mkwrite is called on the
537 		 * page if it is dirtied again later
538 		 */
539 		clear_page_dirty_for_io(page);
540 
541 		btrfs_set_extent_delalloc(inode, page_start, page_end);
542 		set_page_dirty(page);
543 		unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
544 		unlock_page(page);
545 		page_cache_release(page);
546 		balance_dirty_pages_ratelimited_nr(inode->i_mapping, 1);
547 	}
548 
549 out_unlock:
550 	mutex_unlock(&inode->i_mutex);
551 	return 0;
552 }
553 
554 static noinline int btrfs_ioctl_resize(struct btrfs_root *root,
555 					void __user *arg)
556 {
557 	u64 new_size;
558 	u64 old_size;
559 	u64 devid = 1;
560 	struct btrfs_ioctl_vol_args *vol_args;
561 	struct btrfs_trans_handle *trans;
562 	struct btrfs_device *device = NULL;
563 	char *sizestr;
564 	char *devstr = NULL;
565 	int ret = 0;
566 	int namelen;
567 	int mod = 0;
568 
569 	if (root->fs_info->sb->s_flags & MS_RDONLY)
570 		return -EROFS;
571 
572 	if (!capable(CAP_SYS_ADMIN))
573 		return -EPERM;
574 
575 	vol_args = memdup_user(arg, sizeof(*vol_args));
576 	if (IS_ERR(vol_args))
577 		return PTR_ERR(vol_args);
578 
579 	vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
580 	namelen = strlen(vol_args->name);
581 
582 	mutex_lock(&root->fs_info->volume_mutex);
583 	sizestr = vol_args->name;
584 	devstr = strchr(sizestr, ':');
585 	if (devstr) {
586 		char *end;
587 		sizestr = devstr + 1;
588 		*devstr = '\0';
589 		devstr = vol_args->name;
590 		devid = simple_strtoull(devstr, &end, 10);
591 		printk(KERN_INFO "resizing devid %llu\n",
592 		       (unsigned long long)devid);
593 	}
594 	device = btrfs_find_device(root, devid, NULL, NULL);
595 	if (!device) {
596 		printk(KERN_INFO "resizer unable to find device %llu\n",
597 		       (unsigned long long)devid);
598 		ret = -EINVAL;
599 		goto out_unlock;
600 	}
601 	if (!strcmp(sizestr, "max"))
602 		new_size = device->bdev->bd_inode->i_size;
603 	else {
604 		if (sizestr[0] == '-') {
605 			mod = -1;
606 			sizestr++;
607 		} else if (sizestr[0] == '+') {
608 			mod = 1;
609 			sizestr++;
610 		}
611 		new_size = btrfs_parse_size(sizestr);
612 		if (new_size == 0) {
613 			ret = -EINVAL;
614 			goto out_unlock;
615 		}
616 	}
617 
618 	old_size = device->total_bytes;
619 
620 	if (mod < 0) {
621 		if (new_size > old_size) {
622 			ret = -EINVAL;
623 			goto out_unlock;
624 		}
625 		new_size = old_size - new_size;
626 	} else if (mod > 0) {
627 		new_size = old_size + new_size;
628 	}
629 
630 	if (new_size < 256 * 1024 * 1024) {
631 		ret = -EINVAL;
632 		goto out_unlock;
633 	}
634 	if (new_size > device->bdev->bd_inode->i_size) {
635 		ret = -EFBIG;
636 		goto out_unlock;
637 	}
638 
639 	do_div(new_size, root->sectorsize);
640 	new_size *= root->sectorsize;
641 
642 	printk(KERN_INFO "new size for %s is %llu\n",
643 		device->name, (unsigned long long)new_size);
644 
645 	if (new_size > old_size) {
646 		trans = btrfs_start_transaction(root, 1);
647 		ret = btrfs_grow_device(trans, device, new_size);
648 		btrfs_commit_transaction(trans, root);
649 	} else {
650 		ret = btrfs_shrink_device(device, new_size);
651 	}
652 
653 out_unlock:
654 	mutex_unlock(&root->fs_info->volume_mutex);
655 	kfree(vol_args);
656 	return ret;
657 }
658 
659 static noinline int btrfs_ioctl_snap_create(struct file *file,
660 					    void __user *arg, int subvol)
661 {
662 	struct btrfs_root *root = BTRFS_I(fdentry(file)->d_inode)->root;
663 	struct btrfs_ioctl_vol_args *vol_args;
664 	struct file *src_file;
665 	int namelen;
666 	int ret = 0;
667 
668 	if (root->fs_info->sb->s_flags & MS_RDONLY)
669 		return -EROFS;
670 
671 	vol_args = memdup_user(arg, sizeof(*vol_args));
672 	if (IS_ERR(vol_args))
673 		return PTR_ERR(vol_args);
674 
675 	vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
676 	namelen = strlen(vol_args->name);
677 	if (strchr(vol_args->name, '/')) {
678 		ret = -EINVAL;
679 		goto out;
680 	}
681 
682 	if (subvol) {
683 		ret = btrfs_mksubvol(&file->f_path, vol_args->name, namelen,
684 				     NULL);
685 	} else {
686 		struct inode *src_inode;
687 		src_file = fget(vol_args->fd);
688 		if (!src_file) {
689 			ret = -EINVAL;
690 			goto out;
691 		}
692 
693 		src_inode = src_file->f_path.dentry->d_inode;
694 		if (src_inode->i_sb != file->f_path.dentry->d_inode->i_sb) {
695 			printk(KERN_INFO "btrfs: Snapshot src from "
696 			       "another FS\n");
697 			ret = -EINVAL;
698 			fput(src_file);
699 			goto out;
700 		}
701 		ret = btrfs_mksubvol(&file->f_path, vol_args->name, namelen,
702 				     BTRFS_I(src_inode)->root);
703 		fput(src_file);
704 	}
705 out:
706 	kfree(vol_args);
707 	return ret;
708 }
709 
710 /*
711  * helper to check if the subvolume references other subvolumes
712  */
713 static noinline int may_destroy_subvol(struct btrfs_root *root)
714 {
715 	struct btrfs_path *path;
716 	struct btrfs_key key;
717 	int ret;
718 
719 	path = btrfs_alloc_path();
720 	if (!path)
721 		return -ENOMEM;
722 
723 	key.objectid = root->root_key.objectid;
724 	key.type = BTRFS_ROOT_REF_KEY;
725 	key.offset = (u64)-1;
726 
727 	ret = btrfs_search_slot(NULL, root->fs_info->tree_root,
728 				&key, path, 0, 0);
729 	if (ret < 0)
730 		goto out;
731 	BUG_ON(ret == 0);
732 
733 	ret = 0;
734 	if (path->slots[0] > 0) {
735 		path->slots[0]--;
736 		btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
737 		if (key.objectid == root->root_key.objectid &&
738 		    key.type == BTRFS_ROOT_REF_KEY)
739 			ret = -ENOTEMPTY;
740 	}
741 out:
742 	btrfs_free_path(path);
743 	return ret;
744 }
745 
746 static noinline int btrfs_ioctl_snap_destroy(struct file *file,
747 					     void __user *arg)
748 {
749 	struct dentry *parent = fdentry(file);
750 	struct dentry *dentry;
751 	struct inode *dir = parent->d_inode;
752 	struct inode *inode;
753 	struct btrfs_root *root = BTRFS_I(dir)->root;
754 	struct btrfs_root *dest = NULL;
755 	struct btrfs_ioctl_vol_args *vol_args;
756 	struct btrfs_trans_handle *trans;
757 	int namelen;
758 	int ret;
759 	int err = 0;
760 
761 	if (!capable(CAP_SYS_ADMIN))
762 		return -EPERM;
763 
764 	vol_args = memdup_user(arg, sizeof(*vol_args));
765 	if (IS_ERR(vol_args))
766 		return PTR_ERR(vol_args);
767 
768 	vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
769 	namelen = strlen(vol_args->name);
770 	if (strchr(vol_args->name, '/') ||
771 	    strncmp(vol_args->name, "..", namelen) == 0) {
772 		err = -EINVAL;
773 		goto out;
774 	}
775 
776 	err = mnt_want_write(file->f_path.mnt);
777 	if (err)
778 		goto out;
779 
780 	mutex_lock_nested(&dir->i_mutex, I_MUTEX_PARENT);
781 	dentry = lookup_one_len(vol_args->name, parent, namelen);
782 	if (IS_ERR(dentry)) {
783 		err = PTR_ERR(dentry);
784 		goto out_unlock_dir;
785 	}
786 
787 	if (!dentry->d_inode) {
788 		err = -ENOENT;
789 		goto out_dput;
790 	}
791 
792 	inode = dentry->d_inode;
793 	if (inode->i_ino != BTRFS_FIRST_FREE_OBJECTID) {
794 		err = -EINVAL;
795 		goto out_dput;
796 	}
797 
798 	dest = BTRFS_I(inode)->root;
799 
800 	mutex_lock(&inode->i_mutex);
801 	err = d_invalidate(dentry);
802 	if (err)
803 		goto out_unlock;
804 
805 	down_write(&root->fs_info->subvol_sem);
806 
807 	err = may_destroy_subvol(dest);
808 	if (err)
809 		goto out_up_write;
810 
811 	trans = btrfs_start_transaction(root, 1);
812 	ret = btrfs_unlink_subvol(trans, root, dir,
813 				dest->root_key.objectid,
814 				dentry->d_name.name,
815 				dentry->d_name.len);
816 	BUG_ON(ret);
817 
818 	btrfs_record_root_in_trans(trans, dest);
819 
820 	memset(&dest->root_item.drop_progress, 0,
821 		sizeof(dest->root_item.drop_progress));
822 	dest->root_item.drop_level = 0;
823 	btrfs_set_root_refs(&dest->root_item, 0);
824 
825 	ret = btrfs_insert_orphan_item(trans,
826 				root->fs_info->tree_root,
827 				dest->root_key.objectid);
828 	BUG_ON(ret);
829 
830 	ret = btrfs_commit_transaction(trans, root);
831 	BUG_ON(ret);
832 	inode->i_flags |= S_DEAD;
833 out_up_write:
834 	up_write(&root->fs_info->subvol_sem);
835 out_unlock:
836 	mutex_unlock(&inode->i_mutex);
837 	if (!err) {
838 		shrink_dcache_sb(root->fs_info->sb);
839 		btrfs_invalidate_inodes(dest);
840 		d_delete(dentry);
841 	}
842 out_dput:
843 	dput(dentry);
844 out_unlock_dir:
845 	mutex_unlock(&dir->i_mutex);
846 	mnt_drop_write(file->f_path.mnt);
847 out:
848 	kfree(vol_args);
849 	return err;
850 }
851 
852 static int btrfs_ioctl_defrag(struct file *file)
853 {
854 	struct inode *inode = fdentry(file)->d_inode;
855 	struct btrfs_root *root = BTRFS_I(inode)->root;
856 	int ret;
857 
858 	ret = mnt_want_write(file->f_path.mnt);
859 	if (ret)
860 		return ret;
861 
862 	switch (inode->i_mode & S_IFMT) {
863 	case S_IFDIR:
864 		if (!capable(CAP_SYS_ADMIN)) {
865 			ret = -EPERM;
866 			goto out;
867 		}
868 		btrfs_defrag_root(root, 0);
869 		btrfs_defrag_root(root->fs_info->extent_root, 0);
870 		break;
871 	case S_IFREG:
872 		if (!(file->f_mode & FMODE_WRITE)) {
873 			ret = -EINVAL;
874 			goto out;
875 		}
876 		btrfs_defrag_file(file);
877 		break;
878 	}
879 out:
880 	mnt_drop_write(file->f_path.mnt);
881 	return ret;
882 }
883 
884 static long btrfs_ioctl_add_dev(struct btrfs_root *root, void __user *arg)
885 {
886 	struct btrfs_ioctl_vol_args *vol_args;
887 	int ret;
888 
889 	if (!capable(CAP_SYS_ADMIN))
890 		return -EPERM;
891 
892 	vol_args = memdup_user(arg, sizeof(*vol_args));
893 	if (IS_ERR(vol_args))
894 		return PTR_ERR(vol_args);
895 
896 	vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
897 	ret = btrfs_init_new_device(root, vol_args->name);
898 
899 	kfree(vol_args);
900 	return ret;
901 }
902 
903 static long btrfs_ioctl_rm_dev(struct btrfs_root *root, void __user *arg)
904 {
905 	struct btrfs_ioctl_vol_args *vol_args;
906 	int ret;
907 
908 	if (!capable(CAP_SYS_ADMIN))
909 		return -EPERM;
910 
911 	if (root->fs_info->sb->s_flags & MS_RDONLY)
912 		return -EROFS;
913 
914 	vol_args = memdup_user(arg, sizeof(*vol_args));
915 	if (IS_ERR(vol_args))
916 		return PTR_ERR(vol_args);
917 
918 	vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
919 	ret = btrfs_rm_device(root, vol_args->name);
920 
921 	kfree(vol_args);
922 	return ret;
923 }
924 
925 static noinline long btrfs_ioctl_clone(struct file *file, unsigned long srcfd,
926 				       u64 off, u64 olen, u64 destoff)
927 {
928 	struct inode *inode = fdentry(file)->d_inode;
929 	struct btrfs_root *root = BTRFS_I(inode)->root;
930 	struct file *src_file;
931 	struct inode *src;
932 	struct btrfs_trans_handle *trans;
933 	struct btrfs_path *path;
934 	struct extent_buffer *leaf;
935 	char *buf;
936 	struct btrfs_key key;
937 	u32 nritems;
938 	int slot;
939 	int ret;
940 	u64 len = olen;
941 	u64 bs = root->fs_info->sb->s_blocksize;
942 	u64 hint_byte;
943 
944 	/*
945 	 * TODO:
946 	 * - split compressed inline extents.  annoying: we need to
947 	 *   decompress into destination's address_space (the file offset
948 	 *   may change, so source mapping won't do), then recompress (or
949 	 *   otherwise reinsert) a subrange.
950 	 * - allow ranges within the same file to be cloned (provided
951 	 *   they don't overlap)?
952 	 */
953 
954 	/* the destination must be opened for writing */
955 	if (!(file->f_mode & FMODE_WRITE))
956 		return -EINVAL;
957 
958 	ret = mnt_want_write(file->f_path.mnt);
959 	if (ret)
960 		return ret;
961 
962 	src_file = fget(srcfd);
963 	if (!src_file) {
964 		ret = -EBADF;
965 		goto out_drop_write;
966 	}
967 	src = src_file->f_dentry->d_inode;
968 
969 	ret = -EINVAL;
970 	if (src == inode)
971 		goto out_fput;
972 
973 	ret = -EISDIR;
974 	if (S_ISDIR(src->i_mode) || S_ISDIR(inode->i_mode))
975 		goto out_fput;
976 
977 	ret = -EXDEV;
978 	if (src->i_sb != inode->i_sb || BTRFS_I(src)->root != root)
979 		goto out_fput;
980 
981 	ret = -ENOMEM;
982 	buf = vmalloc(btrfs_level_size(root, 0));
983 	if (!buf)
984 		goto out_fput;
985 
986 	path = btrfs_alloc_path();
987 	if (!path) {
988 		vfree(buf);
989 		goto out_fput;
990 	}
991 	path->reada = 2;
992 
993 	if (inode < src) {
994 		mutex_lock(&inode->i_mutex);
995 		mutex_lock(&src->i_mutex);
996 	} else {
997 		mutex_lock(&src->i_mutex);
998 		mutex_lock(&inode->i_mutex);
999 	}
1000 
1001 	/* determine range to clone */
1002 	ret = -EINVAL;
1003 	if (off >= src->i_size || off + len > src->i_size)
1004 		goto out_unlock;
1005 	if (len == 0)
1006 		olen = len = src->i_size - off;
1007 	/* if we extend to eof, continue to block boundary */
1008 	if (off + len == src->i_size)
1009 		len = ((src->i_size + bs-1) & ~(bs-1))
1010 			- off;
1011 
1012 	/* verify the end result is block aligned */
1013 	if ((off & (bs-1)) ||
1014 	    ((off + len) & (bs-1)))
1015 		goto out_unlock;
1016 
1017 	/* do any pending delalloc/csum calc on src, one way or
1018 	   another, and lock file content */
1019 	while (1) {
1020 		struct btrfs_ordered_extent *ordered;
1021 		lock_extent(&BTRFS_I(src)->io_tree, off, off+len, GFP_NOFS);
1022 		ordered = btrfs_lookup_first_ordered_extent(inode, off+len);
1023 		if (BTRFS_I(src)->delalloc_bytes == 0 && !ordered)
1024 			break;
1025 		unlock_extent(&BTRFS_I(src)->io_tree, off, off+len, GFP_NOFS);
1026 		if (ordered)
1027 			btrfs_put_ordered_extent(ordered);
1028 		btrfs_wait_ordered_range(src, off, off+len);
1029 	}
1030 
1031 	trans = btrfs_start_transaction(root, 1);
1032 	BUG_ON(!trans);
1033 
1034 	/* punch hole in destination first */
1035 	btrfs_drop_extents(trans, inode, off, off + len, &hint_byte, 1);
1036 
1037 	/* clone data */
1038 	key.objectid = src->i_ino;
1039 	key.type = BTRFS_EXTENT_DATA_KEY;
1040 	key.offset = 0;
1041 
1042 	while (1) {
1043 		/*
1044 		 * note the key will change type as we walk through the
1045 		 * tree.
1046 		 */
1047 		ret = btrfs_search_slot(trans, root, &key, path, 0, 0);
1048 		if (ret < 0)
1049 			goto out;
1050 
1051 		nritems = btrfs_header_nritems(path->nodes[0]);
1052 		if (path->slots[0] >= nritems) {
1053 			ret = btrfs_next_leaf(root, path);
1054 			if (ret < 0)
1055 				goto out;
1056 			if (ret > 0)
1057 				break;
1058 			nritems = btrfs_header_nritems(path->nodes[0]);
1059 		}
1060 		leaf = path->nodes[0];
1061 		slot = path->slots[0];
1062 
1063 		btrfs_item_key_to_cpu(leaf, &key, slot);
1064 		if (btrfs_key_type(&key) > BTRFS_EXTENT_DATA_KEY ||
1065 		    key.objectid != src->i_ino)
1066 			break;
1067 
1068 		if (btrfs_key_type(&key) == BTRFS_EXTENT_DATA_KEY) {
1069 			struct btrfs_file_extent_item *extent;
1070 			int type;
1071 			u32 size;
1072 			struct btrfs_key new_key;
1073 			u64 disko = 0, diskl = 0;
1074 			u64 datao = 0, datal = 0;
1075 			u8 comp;
1076 
1077 			size = btrfs_item_size_nr(leaf, slot);
1078 			read_extent_buffer(leaf, buf,
1079 					   btrfs_item_ptr_offset(leaf, slot),
1080 					   size);
1081 
1082 			extent = btrfs_item_ptr(leaf, slot,
1083 						struct btrfs_file_extent_item);
1084 			comp = btrfs_file_extent_compression(leaf, extent);
1085 			type = btrfs_file_extent_type(leaf, extent);
1086 			if (type == BTRFS_FILE_EXTENT_REG ||
1087 			    type == BTRFS_FILE_EXTENT_PREALLOC) {
1088 				disko = btrfs_file_extent_disk_bytenr(leaf,
1089 								      extent);
1090 				diskl = btrfs_file_extent_disk_num_bytes(leaf,
1091 								 extent);
1092 				datao = btrfs_file_extent_offset(leaf, extent);
1093 				datal = btrfs_file_extent_num_bytes(leaf,
1094 								    extent);
1095 			} else if (type == BTRFS_FILE_EXTENT_INLINE) {
1096 				/* take upper bound, may be compressed */
1097 				datal = btrfs_file_extent_ram_bytes(leaf,
1098 								    extent);
1099 			}
1100 			btrfs_release_path(root, path);
1101 
1102 			if (key.offset + datal < off ||
1103 			    key.offset >= off+len)
1104 				goto next;
1105 
1106 			memcpy(&new_key, &key, sizeof(new_key));
1107 			new_key.objectid = inode->i_ino;
1108 			new_key.offset = key.offset + destoff - off;
1109 
1110 			if (type == BTRFS_FILE_EXTENT_REG ||
1111 			    type == BTRFS_FILE_EXTENT_PREALLOC) {
1112 				ret = btrfs_insert_empty_item(trans, root, path,
1113 							      &new_key, size);
1114 				if (ret)
1115 					goto out;
1116 
1117 				leaf = path->nodes[0];
1118 				slot = path->slots[0];
1119 				write_extent_buffer(leaf, buf,
1120 					    btrfs_item_ptr_offset(leaf, slot),
1121 					    size);
1122 
1123 				extent = btrfs_item_ptr(leaf, slot,
1124 						struct btrfs_file_extent_item);
1125 
1126 				if (off > key.offset) {
1127 					datao += off - key.offset;
1128 					datal -= off - key.offset;
1129 				}
1130 
1131 				if (key.offset + datal > off + len)
1132 					datal = off + len - key.offset;
1133 
1134 				/* disko == 0 means it's a hole */
1135 				if (!disko)
1136 					datao = 0;
1137 
1138 				btrfs_set_file_extent_offset(leaf, extent,
1139 							     datao);
1140 				btrfs_set_file_extent_num_bytes(leaf, extent,
1141 								datal);
1142 				if (disko) {
1143 					inode_add_bytes(inode, datal);
1144 					ret = btrfs_inc_extent_ref(trans, root,
1145 							disko, diskl, 0,
1146 							root->root_key.objectid,
1147 							inode->i_ino,
1148 							new_key.offset - datao);
1149 					BUG_ON(ret);
1150 				}
1151 			} else if (type == BTRFS_FILE_EXTENT_INLINE) {
1152 				u64 skip = 0;
1153 				u64 trim = 0;
1154 				if (off > key.offset) {
1155 					skip = off - key.offset;
1156 					new_key.offset += skip;
1157 				}
1158 
1159 				if (key.offset + datal > off+len)
1160 					trim = key.offset + datal - (off+len);
1161 
1162 				if (comp && (skip || trim)) {
1163 					ret = -EINVAL;
1164 					goto out;
1165 				}
1166 				size -= skip + trim;
1167 				datal -= skip + trim;
1168 				ret = btrfs_insert_empty_item(trans, root, path,
1169 							      &new_key, size);
1170 				if (ret)
1171 					goto out;
1172 
1173 				if (skip) {
1174 					u32 start =
1175 					  btrfs_file_extent_calc_inline_size(0);
1176 					memmove(buf+start, buf+start+skip,
1177 						datal);
1178 				}
1179 
1180 				leaf = path->nodes[0];
1181 				slot = path->slots[0];
1182 				write_extent_buffer(leaf, buf,
1183 					    btrfs_item_ptr_offset(leaf, slot),
1184 					    size);
1185 				inode_add_bytes(inode, datal);
1186 			}
1187 
1188 			btrfs_mark_buffer_dirty(leaf);
1189 		}
1190 
1191 next:
1192 		btrfs_release_path(root, path);
1193 		key.offset++;
1194 	}
1195 	ret = 0;
1196 out:
1197 	btrfs_release_path(root, path);
1198 	if (ret == 0) {
1199 		inode->i_mtime = inode->i_ctime = CURRENT_TIME;
1200 		if (destoff + olen > inode->i_size)
1201 			btrfs_i_size_write(inode, destoff + olen);
1202 		BTRFS_I(inode)->flags = BTRFS_I(src)->flags;
1203 		ret = btrfs_update_inode(trans, root, inode);
1204 	}
1205 	btrfs_end_transaction(trans, root);
1206 	unlock_extent(&BTRFS_I(src)->io_tree, off, off+len, GFP_NOFS);
1207 	if (ret)
1208 		vmtruncate(inode, 0);
1209 out_unlock:
1210 	mutex_unlock(&src->i_mutex);
1211 	mutex_unlock(&inode->i_mutex);
1212 	vfree(buf);
1213 	btrfs_free_path(path);
1214 out_fput:
1215 	fput(src_file);
1216 out_drop_write:
1217 	mnt_drop_write(file->f_path.mnt);
1218 	return ret;
1219 }
1220 
1221 static long btrfs_ioctl_clone_range(struct file *file, void __user *argp)
1222 {
1223 	struct btrfs_ioctl_clone_range_args args;
1224 
1225 	if (copy_from_user(&args, argp, sizeof(args)))
1226 		return -EFAULT;
1227 	return btrfs_ioctl_clone(file, args.src_fd, args.src_offset,
1228 				 args.src_length, args.dest_offset);
1229 }
1230 
1231 /*
1232  * there are many ways the trans_start and trans_end ioctls can lead
1233  * to deadlocks.  They should only be used by applications that
1234  * basically own the machine, and have a very in depth understanding
1235  * of all the possible deadlocks and enospc problems.
1236  */
1237 static long btrfs_ioctl_trans_start(struct file *file)
1238 {
1239 	struct inode *inode = fdentry(file)->d_inode;
1240 	struct btrfs_root *root = BTRFS_I(inode)->root;
1241 	struct btrfs_trans_handle *trans;
1242 	int ret;
1243 
1244 	ret = -EPERM;
1245 	if (!capable(CAP_SYS_ADMIN))
1246 		goto out;
1247 
1248 	ret = -EINPROGRESS;
1249 	if (file->private_data)
1250 		goto out;
1251 
1252 	ret = mnt_want_write(file->f_path.mnt);
1253 	if (ret)
1254 		goto out;
1255 
1256 	mutex_lock(&root->fs_info->trans_mutex);
1257 	root->fs_info->open_ioctl_trans++;
1258 	mutex_unlock(&root->fs_info->trans_mutex);
1259 
1260 	ret = -ENOMEM;
1261 	trans = btrfs_start_ioctl_transaction(root, 0);
1262 	if (!trans)
1263 		goto out_drop;
1264 
1265 	file->private_data = trans;
1266 	return 0;
1267 
1268 out_drop:
1269 	mutex_lock(&root->fs_info->trans_mutex);
1270 	root->fs_info->open_ioctl_trans--;
1271 	mutex_unlock(&root->fs_info->trans_mutex);
1272 	mnt_drop_write(file->f_path.mnt);
1273 out:
1274 	return ret;
1275 }
1276 
1277 /*
1278  * there are many ways the trans_start and trans_end ioctls can lead
1279  * to deadlocks.  They should only be used by applications that
1280  * basically own the machine, and have a very in depth understanding
1281  * of all the possible deadlocks and enospc problems.
1282  */
1283 long btrfs_ioctl_trans_end(struct file *file)
1284 {
1285 	struct inode *inode = fdentry(file)->d_inode;
1286 	struct btrfs_root *root = BTRFS_I(inode)->root;
1287 	struct btrfs_trans_handle *trans;
1288 
1289 	trans = file->private_data;
1290 	if (!trans)
1291 		return -EINVAL;
1292 	file->private_data = NULL;
1293 
1294 	btrfs_end_transaction(trans, root);
1295 
1296 	mutex_lock(&root->fs_info->trans_mutex);
1297 	root->fs_info->open_ioctl_trans--;
1298 	mutex_unlock(&root->fs_info->trans_mutex);
1299 
1300 	mnt_drop_write(file->f_path.mnt);
1301 	return 0;
1302 }
1303 
1304 long btrfs_ioctl(struct file *file, unsigned int
1305 		cmd, unsigned long arg)
1306 {
1307 	struct btrfs_root *root = BTRFS_I(fdentry(file)->d_inode)->root;
1308 	void __user *argp = (void __user *)arg;
1309 
1310 	switch (cmd) {
1311 	case FS_IOC_GETFLAGS:
1312 		return btrfs_ioctl_getflags(file, argp);
1313 	case FS_IOC_SETFLAGS:
1314 		return btrfs_ioctl_setflags(file, argp);
1315 	case FS_IOC_GETVERSION:
1316 		return btrfs_ioctl_getversion(file, argp);
1317 	case BTRFS_IOC_SNAP_CREATE:
1318 		return btrfs_ioctl_snap_create(file, argp, 0);
1319 	case BTRFS_IOC_SUBVOL_CREATE:
1320 		return btrfs_ioctl_snap_create(file, argp, 1);
1321 	case BTRFS_IOC_SNAP_DESTROY:
1322 		return btrfs_ioctl_snap_destroy(file, argp);
1323 	case BTRFS_IOC_DEFRAG:
1324 		return btrfs_ioctl_defrag(file);
1325 	case BTRFS_IOC_RESIZE:
1326 		return btrfs_ioctl_resize(root, argp);
1327 	case BTRFS_IOC_ADD_DEV:
1328 		return btrfs_ioctl_add_dev(root, argp);
1329 	case BTRFS_IOC_RM_DEV:
1330 		return btrfs_ioctl_rm_dev(root, argp);
1331 	case BTRFS_IOC_BALANCE:
1332 		return btrfs_balance(root->fs_info->dev_root);
1333 	case BTRFS_IOC_CLONE:
1334 		return btrfs_ioctl_clone(file, arg, 0, 0, 0);
1335 	case BTRFS_IOC_CLONE_RANGE:
1336 		return btrfs_ioctl_clone_range(file, argp);
1337 	case BTRFS_IOC_TRANS_START:
1338 		return btrfs_ioctl_trans_start(file);
1339 	case BTRFS_IOC_TRANS_END:
1340 		return btrfs_ioctl_trans_end(file);
1341 	case BTRFS_IOC_SYNC:
1342 		btrfs_sync_fs(file->f_dentry->d_sb, 1);
1343 		return 0;
1344 	}
1345 
1346 	return -ENOTTY;
1347 }
1348