Btrfs: Replace the big fs_mutex with a collection of other locks
[linux-2.6-block.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/pagemap.h>
25 #include <linux/highmem.h>
26 #include <linux/time.h>
27 #include <linux/init.h>
28 #include <linux/string.h>
29 #include <linux/smp_lock.h>
30 #include <linux/backing-dev.h>
31 #include <linux/mpage.h>
32 #include <linux/swap.h>
33 #include <linux/writeback.h>
34 #include <linux/statfs.h>
35 #include <linux/compat.h>
36 #include <linux/bit_spinlock.h>
37 #include <linux/version.h>
38 #include <linux/xattr.h>
39 #include "ctree.h"
40 #include "disk-io.h"
41 #include "transaction.h"
42 #include "btrfs_inode.h"
43 #include "ioctl.h"
44 #include "print-tree.h"
45 #include "volumes.h"
46 #include "locking.h"
47
48
49
50 static noinline int create_subvol(struct btrfs_root *root, char *name,
51                                   int namelen)
52 {
53         struct btrfs_trans_handle *trans;
54         struct btrfs_key key;
55         struct btrfs_root_item root_item;
56         struct btrfs_inode_item *inode_item;
57         struct extent_buffer *leaf;
58         struct btrfs_root *new_root = root;
59         struct inode *dir;
60         int ret;
61         int err;
62         u64 objectid;
63         u64 new_dirid = BTRFS_FIRST_FREE_OBJECTID;
64         unsigned long nr = 1;
65
66         ret = btrfs_check_free_space(root, 1, 0);
67         if (ret)
68                 goto fail_commit;
69
70         trans = btrfs_start_transaction(root, 1);
71         BUG_ON(!trans);
72
73         ret = btrfs_find_free_objectid(trans, root->fs_info->tree_root,
74                                        0, &objectid);
75         if (ret)
76                 goto fail;
77
78         leaf = btrfs_alloc_free_block(trans, root, root->leafsize,
79                                       objectid, trans->transid, 0, 0,
80                                       0, 0);
81         if (IS_ERR(leaf))
82                 return PTR_ERR(leaf);
83
84         btrfs_set_header_nritems(leaf, 0);
85         btrfs_set_header_level(leaf, 0);
86         btrfs_set_header_bytenr(leaf, leaf->start);
87         btrfs_set_header_generation(leaf, trans->transid);
88         btrfs_set_header_owner(leaf, objectid);
89
90         write_extent_buffer(leaf, root->fs_info->fsid,
91                             (unsigned long)btrfs_header_fsid(leaf),
92                             BTRFS_FSID_SIZE);
93         btrfs_mark_buffer_dirty(leaf);
94
95         inode_item = &root_item.inode;
96         memset(inode_item, 0, sizeof(*inode_item));
97         inode_item->generation = cpu_to_le64(1);
98         inode_item->size = cpu_to_le64(3);
99         inode_item->nlink = cpu_to_le32(1);
100         inode_item->nblocks = cpu_to_le64(1);
101         inode_item->mode = cpu_to_le32(S_IFDIR | 0755);
102
103         btrfs_set_root_bytenr(&root_item, leaf->start);
104         btrfs_set_root_level(&root_item, 0);
105         btrfs_set_root_refs(&root_item, 1);
106         btrfs_set_root_used(&root_item, 0);
107
108         memset(&root_item.drop_progress, 0, sizeof(root_item.drop_progress));
109         root_item.drop_level = 0;
110
111         btrfs_tree_unlock(leaf);
112         free_extent_buffer(leaf);
113         leaf = NULL;
114
115         btrfs_set_root_dirid(&root_item, new_dirid);
116
117         key.objectid = objectid;
118         key.offset = 1;
119         btrfs_set_key_type(&key, BTRFS_ROOT_ITEM_KEY);
120         ret = btrfs_insert_root(trans, root->fs_info->tree_root, &key,
121                                 &root_item);
122         if (ret)
123                 goto fail;
124
125         /*
126          * insert the directory item
127          */
128         key.offset = (u64)-1;
129         dir = root->fs_info->sb->s_root->d_inode;
130         ret = btrfs_insert_dir_item(trans, root->fs_info->tree_root,
131                                     name, namelen, dir->i_ino, &key,
132                                     BTRFS_FT_DIR);
133         if (ret)
134                 goto fail;
135
136         ret = btrfs_insert_inode_ref(trans, root->fs_info->tree_root,
137                              name, namelen, objectid,
138                              root->fs_info->sb->s_root->d_inode->i_ino);
139         if (ret)
140                 goto fail;
141
142         ret = btrfs_commit_transaction(trans, root);
143         if (ret)
144                 goto fail_commit;
145
146         new_root = btrfs_read_fs_root(root->fs_info, &key, name, namelen);
147         BUG_ON(!new_root);
148
149         trans = btrfs_start_transaction(new_root, 1);
150         BUG_ON(!trans);
151
152         ret = btrfs_create_subvol_root(new_root, trans, new_dirid,
153                                        BTRFS_I(dir)->block_group);
154         if (ret)
155                 goto fail;
156
157         /* Invalidate existing dcache entry for new subvolume. */
158         btrfs_invalidate_dcache_root(root, name, namelen);
159
160 fail:
161         nr = trans->blocks_used;
162         err = btrfs_commit_transaction(trans, new_root);
163         if (err && !ret)
164                 ret = err;
165 fail_commit:
166         btrfs_btree_balance_dirty(root, nr);
167         btrfs_throttle(root);
168         return ret;
169 }
170
171 static int create_snapshot(struct btrfs_root *root, char *name, int namelen)
172 {
173         struct btrfs_pending_snapshot *pending_snapshot;
174         struct btrfs_trans_handle *trans;
175         int ret;
176         int err;
177         unsigned long nr = 0;
178
179         if (!root->ref_cows)
180                 return -EINVAL;
181
182         ret = btrfs_check_free_space(root, 1, 0);
183         if (ret)
184                 goto fail_unlock;
185
186         pending_snapshot = kmalloc(sizeof(*pending_snapshot), GFP_NOFS);
187         if (!pending_snapshot) {
188                 ret = -ENOMEM;
189                 goto fail_unlock;
190         }
191         pending_snapshot->name = kmalloc(namelen + 1, GFP_NOFS);
192         if (!pending_snapshot->name) {
193                 ret = -ENOMEM;
194                 kfree(pending_snapshot);
195                 goto fail_unlock;
196         }
197         memcpy(pending_snapshot->name, name, namelen);
198         pending_snapshot->name[namelen] = '\0';
199         trans = btrfs_start_transaction(root, 1);
200         BUG_ON(!trans);
201         pending_snapshot->root = root;
202         list_add(&pending_snapshot->list,
203                  &trans->transaction->pending_snapshots);
204         ret = btrfs_update_inode(trans, root, root->inode);
205         err = btrfs_commit_transaction(trans, root);
206
207 fail_unlock:
208         btrfs_btree_balance_dirty(root, nr);
209         btrfs_throttle(root);
210         return ret;
211 }
212
213 int btrfs_defrag_file(struct file *file)
214 {
215         struct inode *inode = fdentry(file)->d_inode;
216         struct btrfs_root *root = BTRFS_I(inode)->root;
217         struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
218         struct page *page;
219         unsigned long last_index;
220         unsigned long ra_pages = root->fs_info->bdi.ra_pages;
221         unsigned long total_read = 0;
222         u64 page_start;
223         u64 page_end;
224         unsigned long i;
225         int ret;
226
227         ret = btrfs_check_free_space(root, inode->i_size, 0);
228         if (ret)
229                 return -ENOSPC;
230
231         mutex_lock(&inode->i_mutex);
232         last_index = inode->i_size >> PAGE_CACHE_SHIFT;
233         for (i = 0; i <= last_index; i++) {
234                 if (total_read % ra_pages == 0) {
235                         btrfs_force_ra(inode->i_mapping, &file->f_ra, file, i,
236                                        min(last_index, i + ra_pages - 1));
237                 }
238                 total_read++;
239                 page = grab_cache_page(inode->i_mapping, i);
240                 if (!page)
241                         goto out_unlock;
242                 if (!PageUptodate(page)) {
243                         btrfs_readpage(NULL, page);
244                         lock_page(page);
245                         if (!PageUptodate(page)) {
246                                 unlock_page(page);
247                                 page_cache_release(page);
248                                 goto out_unlock;
249                         }
250                 }
251
252 #if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,18)
253                 ClearPageDirty(page);
254 #else
255                 cancel_dirty_page(page, PAGE_CACHE_SIZE);
256 #endif
257                 wait_on_page_writeback(page);
258                 set_page_extent_mapped(page);
259
260                 page_start = (u64)page->index << PAGE_CACHE_SHIFT;
261                 page_end = page_start + PAGE_CACHE_SIZE - 1;
262
263                 lock_extent(io_tree, page_start, page_end, GFP_NOFS);
264                 set_extent_delalloc(io_tree, page_start,
265                                     page_end, GFP_NOFS);
266
267                 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
268                 set_page_dirty(page);
269                 unlock_page(page);
270                 page_cache_release(page);
271                 balance_dirty_pages_ratelimited_nr(inode->i_mapping, 1);
272         }
273
274 out_unlock:
275         mutex_unlock(&inode->i_mutex);
276         return 0;
277 }
278
279 /*
280  * Called inside transaction, so use GFP_NOFS
281  */
282
283 static int btrfs_ioctl_resize(struct btrfs_root *root, void __user *arg)
284 {
285         u64 new_size;
286         u64 old_size;
287         u64 devid = 1;
288         struct btrfs_ioctl_vol_args *vol_args;
289         struct btrfs_trans_handle *trans;
290         struct btrfs_device *device = NULL;
291         char *sizestr;
292         char *devstr = NULL;
293         int ret = 0;
294         int namelen;
295         int mod = 0;
296
297         vol_args = kmalloc(sizeof(*vol_args), GFP_NOFS);
298
299         if (!vol_args)
300                 return -ENOMEM;
301
302         if (copy_from_user(vol_args, arg, sizeof(*vol_args))) {
303                 ret = -EFAULT;
304                 goto out;
305         }
306         namelen = strlen(vol_args->name);
307         if (namelen > BTRFS_VOL_NAME_MAX) {
308                 ret = -EINVAL;
309                 goto out;
310         }
311
312         mutex_lock(&root->fs_info->alloc_mutex);
313         mutex_lock(&root->fs_info->chunk_mutex);
314         sizestr = vol_args->name;
315         devstr = strchr(sizestr, ':');
316         if (devstr) {
317                 char *end;
318                 sizestr = devstr + 1;
319                 *devstr = '\0';
320                 devstr = vol_args->name;
321                 devid = simple_strtoull(devstr, &end, 10);
322                 printk(KERN_INFO "resizing devid %llu\n", devid);
323         }
324         device = btrfs_find_device(root, devid, NULL);
325         if (!device) {
326                 printk(KERN_INFO "resizer unable to find device %llu\n", devid);
327                 ret = -EINVAL;
328                 goto out_unlock;
329         }
330         if (!strcmp(sizestr, "max"))
331                 new_size = device->bdev->bd_inode->i_size;
332         else {
333                 if (sizestr[0] == '-') {
334                         mod = -1;
335                         sizestr++;
336                 } else if (sizestr[0] == '+') {
337                         mod = 1;
338                         sizestr++;
339                 }
340                 new_size = btrfs_parse_size(sizestr);
341                 if (new_size == 0) {
342                         ret = -EINVAL;
343                         goto out_unlock;
344                 }
345         }
346
347         old_size = device->total_bytes;
348
349         if (mod < 0) {
350                 if (new_size > old_size) {
351                         ret = -EINVAL;
352                         goto out_unlock;
353                 }
354                 new_size = old_size - new_size;
355         } else if (mod > 0) {
356                 new_size = old_size + new_size;
357         }
358
359         if (new_size < 256 * 1024 * 1024) {
360                 ret = -EINVAL;
361                 goto out_unlock;
362         }
363         if (new_size > device->bdev->bd_inode->i_size) {
364                 ret = -EFBIG;
365                 goto out_unlock;
366         }
367
368         do_div(new_size, root->sectorsize);
369         new_size *= root->sectorsize;
370
371         printk(KERN_INFO "new size for %s is %llu\n",
372                 device->name, (unsigned long long)new_size);
373
374         if (new_size > old_size) {
375                 trans = btrfs_start_transaction(root, 1);
376                 ret = btrfs_grow_device(trans, device, new_size);
377                 btrfs_commit_transaction(trans, root);
378         } else {
379                 ret = btrfs_shrink_device(device, new_size);
380         }
381
382 out_unlock:
383         mutex_lock(&root->fs_info->alloc_mutex);
384         mutex_lock(&root->fs_info->chunk_mutex);
385 out:
386         kfree(vol_args);
387         return ret;
388 }
389
390 static noinline int btrfs_ioctl_snap_create(struct btrfs_root *root,
391                                             void __user *arg)
392 {
393         struct btrfs_ioctl_vol_args *vol_args;
394         struct btrfs_dir_item *di;
395         struct btrfs_path *path;
396         u64 root_dirid;
397         int namelen;
398         int ret;
399
400         vol_args = kmalloc(sizeof(*vol_args), GFP_NOFS);
401
402         if (!vol_args)
403                 return -ENOMEM;
404
405         if (copy_from_user(vol_args, arg, sizeof(*vol_args))) {
406                 ret = -EFAULT;
407                 goto out;
408         }
409
410         namelen = strlen(vol_args->name);
411         if (namelen > BTRFS_VOL_NAME_MAX) {
412                 ret = -EINVAL;
413                 goto out;
414         }
415         if (strchr(vol_args->name, '/')) {
416                 ret = -EINVAL;
417                 goto out;
418         }
419
420         path = btrfs_alloc_path();
421         if (!path) {
422                 ret = -ENOMEM;
423                 goto out;
424         }
425
426         root_dirid = root->fs_info->sb->s_root->d_inode->i_ino,
427         di = btrfs_lookup_dir_item(NULL, root->fs_info->tree_root,
428                             path, root_dirid,
429                             vol_args->name, namelen, 0);
430         btrfs_free_path(path);
431
432         if (di && !IS_ERR(di)) {
433                 ret = -EEXIST;
434                 goto out;
435         }
436
437         if (IS_ERR(di)) {
438                 ret = PTR_ERR(di);
439                 goto out;
440         }
441
442         mutex_lock(&root->fs_info->drop_mutex);
443         if (root == root->fs_info->tree_root)
444                 ret = create_subvol(root, vol_args->name, namelen);
445         else
446                 ret = create_snapshot(root, vol_args->name, namelen);
447         mutex_unlock(&root->fs_info->drop_mutex);
448 out:
449         kfree(vol_args);
450         return ret;
451 }
452
453 static int btrfs_ioctl_defrag(struct file *file)
454 {
455         struct inode *inode = fdentry(file)->d_inode;
456         struct btrfs_root *root = BTRFS_I(inode)->root;
457
458         switch (inode->i_mode & S_IFMT) {
459         case S_IFDIR:
460                 btrfs_defrag_root(root, 0);
461                 btrfs_defrag_root(root->fs_info->extent_root, 0);
462                 break;
463         case S_IFREG:
464                 btrfs_defrag_file(file);
465                 break;
466         }
467
468         return 0;
469 }
470
471 long btrfs_ioctl_add_dev(struct btrfs_root *root, void __user *arg)
472 {
473         struct btrfs_ioctl_vol_args *vol_args;
474         int ret;
475
476         vol_args = kmalloc(sizeof(*vol_args), GFP_NOFS);
477
478         if (!vol_args)
479                 return -ENOMEM;
480
481         if (copy_from_user(vol_args, arg, sizeof(*vol_args))) {
482                 ret = -EFAULT;
483                 goto out;
484         }
485         ret = btrfs_init_new_device(root, vol_args->name);
486
487 out:
488         kfree(vol_args);
489         return ret;
490 }
491
492 long btrfs_ioctl_rm_dev(struct btrfs_root *root, void __user *arg)
493 {
494         struct btrfs_ioctl_vol_args *vol_args;
495         int ret;
496
497         vol_args = kmalloc(sizeof(*vol_args), GFP_NOFS);
498
499         if (!vol_args)
500                 return -ENOMEM;
501
502         if (copy_from_user(vol_args, arg, sizeof(*vol_args))) {
503                 ret = -EFAULT;
504                 goto out;
505         }
506         ret = btrfs_rm_device(root, vol_args->name);
507
508 out:
509         kfree(vol_args);
510         return ret;
511 }
512
513 int dup_item_to_inode(struct btrfs_trans_handle *trans,
514                        struct btrfs_root *root,
515                        struct btrfs_path *path,
516                        struct extent_buffer *leaf,
517                        int slot,
518                        struct btrfs_key *key,
519                        u64 destino)
520 {
521         char *dup;
522         int len = btrfs_item_size_nr(leaf, slot);
523         struct btrfs_key ckey = *key;
524         int ret = 0;
525
526         dup = kmalloc(len, GFP_NOFS);
527         if (!dup)
528                 return -ENOMEM;
529
530         read_extent_buffer(leaf, dup, btrfs_item_ptr_offset(leaf, slot), len);
531         btrfs_release_path(root, path);
532
533         ckey.objectid = destino;
534         ret = btrfs_insert_item(trans, root, &ckey, dup, len);
535         kfree(dup);
536         return ret;
537 }
538
539 long btrfs_ioctl_clone(struct file *file, unsigned long src_fd)
540 {
541         struct inode *inode = fdentry(file)->d_inode;
542         struct btrfs_root *root = BTRFS_I(inode)->root;
543         struct file *src_file;
544         struct inode *src;
545         struct btrfs_trans_handle *trans;
546         int ret;
547         u64 pos;
548         struct btrfs_path *path;
549         struct btrfs_key key;
550         struct extent_buffer *leaf;
551         u32 nritems;
552         int slot;
553
554         src_file = fget(src_fd);
555         if (!src_file)
556                 return -EBADF;
557         src = src_file->f_dentry->d_inode;
558
559         ret = -EXDEV;
560         if (src->i_sb != inode->i_sb)
561                 goto out_fput;
562
563         if (inode < src) {
564                 mutex_lock(&inode->i_mutex);
565                 mutex_lock(&src->i_mutex);
566         } else {
567                 mutex_lock(&src->i_mutex);
568                 mutex_lock(&inode->i_mutex);
569         }
570
571         ret = -ENOTEMPTY;
572         if (inode->i_size)
573                 goto out_unlock;
574
575         /* do any pending delalloc/csum calc on src, one way or
576            another, and lock file content */
577         while (1) {
578                 filemap_write_and_wait(src->i_mapping);
579                 lock_extent(&BTRFS_I(src)->io_tree, 0, (u64)-1, GFP_NOFS);
580                 if (BTRFS_I(src)->delalloc_bytes == 0)
581                         break;
582                 unlock_extent(&BTRFS_I(src)->io_tree, 0, (u64)-1, GFP_NOFS);
583         }
584
585         trans = btrfs_start_transaction(root, 0);
586         path = btrfs_alloc_path();
587         if (!path) {
588                 ret = -ENOMEM;
589                 goto out;
590         }
591         key.offset = 0;
592         key.type = BTRFS_EXTENT_DATA_KEY;
593         key.objectid = src->i_ino;
594         pos = 0;
595         path->reada = 2;
596
597         while (1) {
598                 /*
599                  * note the key will change type as we walk through the
600                  * tree.
601                  */
602                 ret = btrfs_search_slot(trans, root, &key, path, 0, 0);
603                 if (ret < 0)
604                         goto out;
605
606                 if (path->slots[0] >= btrfs_header_nritems(path->nodes[0])) {
607                         ret = btrfs_next_leaf(root, path);
608                         if (ret < 0)
609                                 goto out;
610                         if (ret > 0)
611                                 break;
612                 }
613                 leaf = path->nodes[0];
614                 slot = path->slots[0];
615                 btrfs_item_key_to_cpu(leaf, &key, slot);
616                 nritems = btrfs_header_nritems(leaf);
617
618                 if (btrfs_key_type(&key) > BTRFS_CSUM_ITEM_KEY ||
619                     key.objectid != src->i_ino)
620                         break;
621
622                 if (btrfs_key_type(&key) == BTRFS_EXTENT_DATA_KEY) {
623                         struct btrfs_file_extent_item *extent;
624                         int found_type;
625                         pos = key.offset;
626                         extent = btrfs_item_ptr(leaf, slot,
627                                                 struct btrfs_file_extent_item);
628                         found_type = btrfs_file_extent_type(leaf, extent);
629                         if (found_type == BTRFS_FILE_EXTENT_REG) {
630                                 u64 len = btrfs_file_extent_num_bytes(leaf,
631                                                                       extent);
632                                 u64 ds = btrfs_file_extent_disk_bytenr(leaf,
633                                                                        extent);
634                                 u64 dl = btrfs_file_extent_disk_num_bytes(leaf,
635                                                                  extent);
636                                 u64 off = btrfs_file_extent_offset(leaf,
637                                                                    extent);
638                                 btrfs_insert_file_extent(trans, root,
639                                                          inode->i_ino, pos,
640                                                          ds, dl, len, off);
641                                 /* ds == 0 means there's a hole */
642                                 if (ds != 0) {
643                                         btrfs_inc_extent_ref(trans, root,
644                                                      ds, dl,
645                                                      root->root_key.objectid,
646                                                      trans->transid,
647                                                      inode->i_ino, pos);
648                                 }
649                                 pos = key.offset + len;
650                         } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
651                                 ret = dup_item_to_inode(trans, root, path,
652                                                         leaf, slot, &key,
653                                                         inode->i_ino);
654                                 if (ret)
655                                         goto out;
656                                 pos = key.offset + btrfs_item_size_nr(leaf,
657                                                                       slot);
658                         }
659                 } else if (btrfs_key_type(&key) == BTRFS_CSUM_ITEM_KEY) {
660                         ret = dup_item_to_inode(trans, root, path, leaf,
661                                                 slot, &key, inode->i_ino);
662
663                         if (ret)
664                                 goto out;
665                 }
666                 key.offset++;
667                 btrfs_release_path(root, path);
668         }
669
670         ret = 0;
671 out:
672         btrfs_free_path(path);
673
674         inode->i_blocks = src->i_blocks;
675         i_size_write(inode, src->i_size);
676         btrfs_update_inode(trans, root, inode);
677
678         unlock_extent(&BTRFS_I(src)->io_tree, 0, (u64)-1, GFP_NOFS);
679
680         btrfs_end_transaction(trans, root);
681
682 out_unlock:
683         mutex_unlock(&src->i_mutex);
684         mutex_unlock(&inode->i_mutex);
685 out_fput:
686         fput(src_file);
687         return ret;
688 }
689
690 /*
691  * there are many ways the trans_start and trans_end ioctls can lead
692  * to deadlocks.  They should only be used by applications that
693  * basically own the machine, and have a very in depth understanding
694  * of all the possible deadlocks and enospc problems.
695  */
696 long btrfs_ioctl_trans_start(struct file *file)
697 {
698         struct inode *inode = fdentry(file)->d_inode;
699         struct btrfs_root *root = BTRFS_I(inode)->root;
700         struct btrfs_trans_handle *trans;
701         int ret = 0;
702
703         if (!capable(CAP_SYS_ADMIN))
704                 return -EPERM;
705
706         if (file->private_data) {
707                 ret = -EINPROGRESS;
708                 goto out;
709         }
710         trans = btrfs_start_transaction(root, 0);
711         if (trans)
712                 file->private_data = trans;
713         else
714                 ret = -ENOMEM;
715         /*printk(KERN_INFO "btrfs_ioctl_trans_start on %p\n", file);*/
716 out:
717         return ret;
718 }
719
720 /*
721  * there are many ways the trans_start and trans_end ioctls can lead
722  * to deadlocks.  They should only be used by applications that
723  * basically own the machine, and have a very in depth understanding
724  * of all the possible deadlocks and enospc problems.
725  */
726 long btrfs_ioctl_trans_end(struct file *file)
727 {
728         struct inode *inode = fdentry(file)->d_inode;
729         struct btrfs_root *root = BTRFS_I(inode)->root;
730         struct btrfs_trans_handle *trans;
731         int ret = 0;
732
733         trans = file->private_data;
734         if (!trans) {
735                 ret = -EINVAL;
736                 goto out;
737         }
738         btrfs_end_transaction(trans, root);
739         file->private_data = 0;
740 out:
741         return ret;
742 }
743
744 long btrfs_ioctl(struct file *file, unsigned int
745                 cmd, unsigned long arg)
746 {
747         struct btrfs_root *root = BTRFS_I(fdentry(file)->d_inode)->root;
748
749         switch (cmd) {
750         case BTRFS_IOC_SNAP_CREATE:
751                 return btrfs_ioctl_snap_create(root, (void __user *)arg);
752         case BTRFS_IOC_DEFRAG:
753                 return btrfs_ioctl_defrag(file);
754         case BTRFS_IOC_RESIZE:
755                 return btrfs_ioctl_resize(root, (void __user *)arg);
756         case BTRFS_IOC_ADD_DEV:
757                 return btrfs_ioctl_add_dev(root, (void __user *)arg);
758         case BTRFS_IOC_RM_DEV:
759                 return btrfs_ioctl_rm_dev(root, (void __user *)arg);
760         case BTRFS_IOC_BALANCE:
761                 return btrfs_balance(root->fs_info->dev_root);
762         case BTRFS_IOC_CLONE:
763                 return btrfs_ioctl_clone(file, arg);
764         case BTRFS_IOC_TRANS_START:
765                 return btrfs_ioctl_trans_start(file);
766         case BTRFS_IOC_TRANS_END:
767                 return btrfs_ioctl_trans_end(file);
768         case BTRFS_IOC_SYNC:
769                 btrfs_sync_fs(file->f_dentry->d_sb, 1);
770                 return 0;
771         }
772
773         return -ENOTTY;
774 }