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