Add btrfs_end_transaction_throttle to force writers to wait for pending commits
[safe/jmp/linux-2.6] / 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         return ret;
168 }
169
170 static int create_snapshot(struct btrfs_root *root, char *name, int namelen)
171 {
172         struct btrfs_pending_snapshot *pending_snapshot;
173         struct btrfs_trans_handle *trans;
174         int ret;
175         int err;
176         unsigned long nr = 0;
177
178         if (!root->ref_cows)
179                 return -EINVAL;
180
181         ret = btrfs_check_free_space(root, 1, 0);
182         if (ret)
183                 goto fail_unlock;
184
185         pending_snapshot = kmalloc(sizeof(*pending_snapshot), GFP_NOFS);
186         if (!pending_snapshot) {
187                 ret = -ENOMEM;
188                 goto fail_unlock;
189         }
190         pending_snapshot->name = kmalloc(namelen + 1, GFP_NOFS);
191         if (!pending_snapshot->name) {
192                 ret = -ENOMEM;
193                 kfree(pending_snapshot);
194                 goto fail_unlock;
195         }
196         memcpy(pending_snapshot->name, name, namelen);
197         pending_snapshot->name[namelen] = '\0';
198         trans = btrfs_start_transaction(root, 1);
199         BUG_ON(!trans);
200         pending_snapshot->root = root;
201         list_add(&pending_snapshot->list,
202                  &trans->transaction->pending_snapshots);
203         ret = btrfs_update_inode(trans, root, root->inode);
204         err = btrfs_commit_transaction(trans, root);
205
206 fail_unlock:
207         btrfs_btree_balance_dirty(root, nr);
208         return ret;
209 }
210
211 int btrfs_defrag_file(struct file *file)
212 {
213         struct inode *inode = fdentry(file)->d_inode;
214         struct btrfs_root *root = BTRFS_I(inode)->root;
215         struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
216         struct page *page;
217         unsigned long last_index;
218         unsigned long ra_pages = root->fs_info->bdi.ra_pages;
219         unsigned long total_read = 0;
220         u64 page_start;
221         u64 page_end;
222         unsigned long i;
223         int ret;
224
225         ret = btrfs_check_free_space(root, inode->i_size, 0);
226         if (ret)
227                 return -ENOSPC;
228
229         mutex_lock(&inode->i_mutex);
230         last_index = inode->i_size >> PAGE_CACHE_SHIFT;
231         for (i = 0; i <= last_index; i++) {
232                 if (total_read % ra_pages == 0) {
233                         btrfs_force_ra(inode->i_mapping, &file->f_ra, file, i,
234                                        min(last_index, i + ra_pages - 1));
235                 }
236                 total_read++;
237                 page = grab_cache_page(inode->i_mapping, i);
238                 if (!page)
239                         goto out_unlock;
240                 if (!PageUptodate(page)) {
241                         btrfs_readpage(NULL, page);
242                         lock_page(page);
243                         if (!PageUptodate(page)) {
244                                 unlock_page(page);
245                                 page_cache_release(page);
246                                 goto out_unlock;
247                         }
248                 }
249
250 #if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,18)
251                 ClearPageDirty(page);
252 #else
253                 cancel_dirty_page(page, PAGE_CACHE_SIZE);
254 #endif
255                 wait_on_page_writeback(page);
256                 set_page_extent_mapped(page);
257
258                 page_start = (u64)page->index << PAGE_CACHE_SHIFT;
259                 page_end = page_start + PAGE_CACHE_SIZE - 1;
260
261                 lock_extent(io_tree, page_start, page_end, GFP_NOFS);
262                 set_extent_delalloc(io_tree, page_start,
263                                     page_end, GFP_NOFS);
264
265                 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
266                 set_page_dirty(page);
267                 unlock_page(page);
268                 page_cache_release(page);
269                 balance_dirty_pages_ratelimited_nr(inode->i_mapping, 1);
270         }
271
272 out_unlock:
273         mutex_unlock(&inode->i_mutex);
274         return 0;
275 }
276
277 /*
278  * Called inside transaction, so use GFP_NOFS
279  */
280
281 static int btrfs_ioctl_resize(struct btrfs_root *root, void __user *arg)
282 {
283         u64 new_size;
284         u64 old_size;
285         u64 devid = 1;
286         struct btrfs_ioctl_vol_args *vol_args;
287         struct btrfs_trans_handle *trans;
288         struct btrfs_device *device = NULL;
289         char *sizestr;
290         char *devstr = NULL;
291         int ret = 0;
292         int namelen;
293         int mod = 0;
294
295         vol_args = kmalloc(sizeof(*vol_args), GFP_NOFS);
296
297         if (!vol_args)
298                 return -ENOMEM;
299
300         if (copy_from_user(vol_args, arg, sizeof(*vol_args))) {
301                 ret = -EFAULT;
302                 goto out;
303         }
304         namelen = strlen(vol_args->name);
305         if (namelen > BTRFS_VOL_NAME_MAX) {
306                 ret = -EINVAL;
307                 goto out;
308         }
309
310         mutex_lock(&root->fs_info->alloc_mutex);
311         mutex_lock(&root->fs_info->chunk_mutex);
312         sizestr = vol_args->name;
313         devstr = strchr(sizestr, ':');
314         if (devstr) {
315                 char *end;
316                 sizestr = devstr + 1;
317                 *devstr = '\0';
318                 devstr = vol_args->name;
319                 devid = simple_strtoull(devstr, &end, 10);
320                 printk(KERN_INFO "resizing devid %llu\n", devid);
321         }
322         device = btrfs_find_device(root, devid, NULL);
323         if (!device) {
324                 printk(KERN_INFO "resizer unable to find device %llu\n", devid);
325                 ret = -EINVAL;
326                 goto out_unlock;
327         }
328         if (!strcmp(sizestr, "max"))
329                 new_size = device->bdev->bd_inode->i_size;
330         else {
331                 if (sizestr[0] == '-') {
332                         mod = -1;
333                         sizestr++;
334                 } else if (sizestr[0] == '+') {
335                         mod = 1;
336                         sizestr++;
337                 }
338                 new_size = btrfs_parse_size(sizestr);
339                 if (new_size == 0) {
340                         ret = -EINVAL;
341                         goto out_unlock;
342                 }
343         }
344
345         old_size = device->total_bytes;
346
347         if (mod < 0) {
348                 if (new_size > old_size) {
349                         ret = -EINVAL;
350                         goto out_unlock;
351                 }
352                 new_size = old_size - new_size;
353         } else if (mod > 0) {
354                 new_size = old_size + new_size;
355         }
356
357         if (new_size < 256 * 1024 * 1024) {
358                 ret = -EINVAL;
359                 goto out_unlock;
360         }
361         if (new_size > device->bdev->bd_inode->i_size) {
362                 ret = -EFBIG;
363                 goto out_unlock;
364         }
365
366         do_div(new_size, root->sectorsize);
367         new_size *= root->sectorsize;
368
369         printk(KERN_INFO "new size for %s is %llu\n",
370                 device->name, (unsigned long long)new_size);
371
372         if (new_size > old_size) {
373                 trans = btrfs_start_transaction(root, 1);
374                 ret = btrfs_grow_device(trans, device, new_size);
375                 btrfs_commit_transaction(trans, root);
376         } else {
377                 ret = btrfs_shrink_device(device, new_size);
378         }
379
380 out_unlock:
381         mutex_lock(&root->fs_info->alloc_mutex);
382         mutex_lock(&root->fs_info->chunk_mutex);
383 out:
384         kfree(vol_args);
385         return ret;
386 }
387
388 static noinline int btrfs_ioctl_snap_create(struct btrfs_root *root,
389                                             void __user *arg)
390 {
391         struct btrfs_ioctl_vol_args *vol_args;
392         struct btrfs_dir_item *di;
393         struct btrfs_path *path;
394         u64 root_dirid;
395         int namelen;
396         int ret;
397
398         vol_args = kmalloc(sizeof(*vol_args), GFP_NOFS);
399
400         if (!vol_args)
401                 return -ENOMEM;
402
403         if (copy_from_user(vol_args, arg, sizeof(*vol_args))) {
404                 ret = -EFAULT;
405                 goto out;
406         }
407
408         namelen = strlen(vol_args->name);
409         if (namelen > BTRFS_VOL_NAME_MAX) {
410                 ret = -EINVAL;
411                 goto out;
412         }
413         if (strchr(vol_args->name, '/')) {
414                 ret = -EINVAL;
415                 goto out;
416         }
417
418         path = btrfs_alloc_path();
419         if (!path) {
420                 ret = -ENOMEM;
421                 goto out;
422         }
423
424         root_dirid = root->fs_info->sb->s_root->d_inode->i_ino,
425         di = btrfs_lookup_dir_item(NULL, root->fs_info->tree_root,
426                             path, root_dirid,
427                             vol_args->name, namelen, 0);
428         btrfs_free_path(path);
429
430         if (di && !IS_ERR(di)) {
431                 ret = -EEXIST;
432                 goto out;
433         }
434
435         if (IS_ERR(di)) {
436                 ret = PTR_ERR(di);
437                 goto out;
438         }
439
440         mutex_lock(&root->fs_info->drop_mutex);
441         if (root == root->fs_info->tree_root)
442                 ret = create_subvol(root, vol_args->name, namelen);
443         else
444                 ret = create_snapshot(root, vol_args->name, namelen);
445         mutex_unlock(&root->fs_info->drop_mutex);
446 out:
447         kfree(vol_args);
448         return ret;
449 }
450
451 static int btrfs_ioctl_defrag(struct file *file)
452 {
453         struct inode *inode = fdentry(file)->d_inode;
454         struct btrfs_root *root = BTRFS_I(inode)->root;
455
456         switch (inode->i_mode & S_IFMT) {
457         case S_IFDIR:
458                 btrfs_defrag_root(root, 0);
459                 btrfs_defrag_root(root->fs_info->extent_root, 0);
460                 break;
461         case S_IFREG:
462                 btrfs_defrag_file(file);
463                 break;
464         }
465
466         return 0;
467 }
468
469 long btrfs_ioctl_add_dev(struct btrfs_root *root, void __user *arg)
470 {
471         struct btrfs_ioctl_vol_args *vol_args;
472         int ret;
473
474         vol_args = kmalloc(sizeof(*vol_args), GFP_NOFS);
475
476         if (!vol_args)
477                 return -ENOMEM;
478
479         if (copy_from_user(vol_args, arg, sizeof(*vol_args))) {
480                 ret = -EFAULT;
481                 goto out;
482         }
483         ret = btrfs_init_new_device(root, vol_args->name);
484
485 out:
486         kfree(vol_args);
487         return ret;
488 }
489
490 long btrfs_ioctl_rm_dev(struct btrfs_root *root, void __user *arg)
491 {
492         struct btrfs_ioctl_vol_args *vol_args;
493         int ret;
494
495         vol_args = kmalloc(sizeof(*vol_args), GFP_NOFS);
496
497         if (!vol_args)
498                 return -ENOMEM;
499
500         if (copy_from_user(vol_args, arg, sizeof(*vol_args))) {
501                 ret = -EFAULT;
502                 goto out;
503         }
504         ret = btrfs_rm_device(root, vol_args->name);
505
506 out:
507         kfree(vol_args);
508         return ret;
509 }
510
511 int dup_item_to_inode(struct btrfs_trans_handle *trans,
512                        struct btrfs_root *root,
513                        struct btrfs_path *path,
514                        struct extent_buffer *leaf,
515                        int slot,
516                        struct btrfs_key *key,
517                        u64 destino)
518 {
519         char *dup;
520         int len = btrfs_item_size_nr(leaf, slot);
521         struct btrfs_key ckey = *key;
522         int ret = 0;
523
524         dup = kmalloc(len, GFP_NOFS);
525         if (!dup)
526                 return -ENOMEM;
527
528         read_extent_buffer(leaf, dup, btrfs_item_ptr_offset(leaf, slot), len);
529         btrfs_release_path(root, path);
530
531         ckey.objectid = destino;
532         ret = btrfs_insert_item(trans, root, &ckey, dup, len);
533         kfree(dup);
534         return ret;
535 }
536
537 long btrfs_ioctl_clone(struct file *file, unsigned long src_fd)
538 {
539         struct inode *inode = fdentry(file)->d_inode;
540         struct btrfs_root *root = BTRFS_I(inode)->root;
541         struct file *src_file;
542         struct inode *src;
543         struct btrfs_trans_handle *trans;
544         int ret;
545         u64 pos;
546         struct btrfs_path *path;
547         struct btrfs_key key;
548         struct extent_buffer *leaf;
549         u32 nritems;
550         int slot;
551
552         src_file = fget(src_fd);
553         if (!src_file)
554                 return -EBADF;
555         src = src_file->f_dentry->d_inode;
556
557         ret = -EXDEV;
558         if (src->i_sb != inode->i_sb)
559                 goto out_fput;
560
561         if (inode < src) {
562                 mutex_lock(&inode->i_mutex);
563                 mutex_lock(&src->i_mutex);
564         } else {
565                 mutex_lock(&src->i_mutex);
566                 mutex_lock(&inode->i_mutex);
567         }
568
569         ret = -ENOTEMPTY;
570         if (inode->i_size)
571                 goto out_unlock;
572
573         /* do any pending delalloc/csum calc on src, one way or
574            another, and lock file content */
575         while (1) {
576                 filemap_write_and_wait(src->i_mapping);
577                 lock_extent(&BTRFS_I(src)->io_tree, 0, (u64)-1, GFP_NOFS);
578                 if (BTRFS_I(src)->delalloc_bytes == 0)
579                         break;
580                 unlock_extent(&BTRFS_I(src)->io_tree, 0, (u64)-1, GFP_NOFS);
581         }
582
583         trans = btrfs_start_transaction(root, 0);
584         path = btrfs_alloc_path();
585         if (!path) {
586                 ret = -ENOMEM;
587                 goto out;
588         }
589         key.offset = 0;
590         key.type = BTRFS_EXTENT_DATA_KEY;
591         key.objectid = src->i_ino;
592         pos = 0;
593         path->reada = 2;
594
595         while (1) {
596                 /*
597                  * note the key will change type as we walk through the
598                  * tree.
599                  */
600                 ret = btrfs_search_slot(trans, root, &key, path, 0, 0);
601                 if (ret < 0)
602                         goto out;
603
604                 if (path->slots[0] >= btrfs_header_nritems(path->nodes[0])) {
605                         ret = btrfs_next_leaf(root, path);
606                         if (ret < 0)
607                                 goto out;
608                         if (ret > 0)
609                                 break;
610                 }
611                 leaf = path->nodes[0];
612                 slot = path->slots[0];
613                 btrfs_item_key_to_cpu(leaf, &key, slot);
614                 nritems = btrfs_header_nritems(leaf);
615
616                 if (btrfs_key_type(&key) > BTRFS_CSUM_ITEM_KEY ||
617                     key.objectid != src->i_ino)
618                         break;
619
620                 if (btrfs_key_type(&key) == BTRFS_EXTENT_DATA_KEY) {
621                         struct btrfs_file_extent_item *extent;
622                         int found_type;
623                         pos = key.offset;
624                         extent = btrfs_item_ptr(leaf, slot,
625                                                 struct btrfs_file_extent_item);
626                         found_type = btrfs_file_extent_type(leaf, extent);
627                         if (found_type == BTRFS_FILE_EXTENT_REG) {
628                                 u64 len = btrfs_file_extent_num_bytes(leaf,
629                                                                       extent);
630                                 u64 ds = btrfs_file_extent_disk_bytenr(leaf,
631                                                                        extent);
632                                 u64 dl = btrfs_file_extent_disk_num_bytes(leaf,
633                                                                  extent);
634                                 u64 off = btrfs_file_extent_offset(leaf,
635                                                                    extent);
636                                 btrfs_insert_file_extent(trans, root,
637                                                          inode->i_ino, pos,
638                                                          ds, dl, len, off);
639                                 /* ds == 0 means there's a hole */
640                                 if (ds != 0) {
641                                         btrfs_inc_extent_ref(trans, root,
642                                                      ds, dl,
643                                                      root->root_key.objectid,
644                                                      trans->transid,
645                                                      inode->i_ino, pos);
646                                 }
647                                 pos = key.offset + len;
648                         } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
649                                 ret = dup_item_to_inode(trans, root, path,
650                                                         leaf, slot, &key,
651                                                         inode->i_ino);
652                                 if (ret)
653                                         goto out;
654                                 pos = key.offset + btrfs_item_size_nr(leaf,
655                                                                       slot);
656                         }
657                 } else if (btrfs_key_type(&key) == BTRFS_CSUM_ITEM_KEY) {
658                         ret = dup_item_to_inode(trans, root, path, leaf,
659                                                 slot, &key, inode->i_ino);
660
661                         if (ret)
662                                 goto out;
663                 }
664                 key.offset++;
665                 btrfs_release_path(root, path);
666         }
667
668         ret = 0;
669 out:
670         btrfs_free_path(path);
671
672         inode->i_blocks = src->i_blocks;
673         i_size_write(inode, src->i_size);
674         btrfs_update_inode(trans, root, inode);
675
676         unlock_extent(&BTRFS_I(src)->io_tree, 0, (u64)-1, GFP_NOFS);
677
678         btrfs_end_transaction(trans, root);
679
680 out_unlock:
681         mutex_unlock(&src->i_mutex);
682         mutex_unlock(&inode->i_mutex);
683 out_fput:
684         fput(src_file);
685         return ret;
686 }
687
688 /*
689  * there are many ways the trans_start and trans_end ioctls can lead
690  * to deadlocks.  They should only be used by applications that
691  * basically own the machine, and have a very in depth understanding
692  * of all the possible deadlocks and enospc problems.
693  */
694 long btrfs_ioctl_trans_start(struct file *file)
695 {
696         struct inode *inode = fdentry(file)->d_inode;
697         struct btrfs_root *root = BTRFS_I(inode)->root;
698         struct btrfs_trans_handle *trans;
699         int ret = 0;
700
701         if (!capable(CAP_SYS_ADMIN))
702                 return -EPERM;
703
704         if (file->private_data) {
705                 ret = -EINPROGRESS;
706                 goto out;
707         }
708         trans = btrfs_start_transaction(root, 0);
709         if (trans)
710                 file->private_data = trans;
711         else
712                 ret = -ENOMEM;
713         /*printk(KERN_INFO "btrfs_ioctl_trans_start on %p\n", file);*/
714 out:
715         return ret;
716 }
717
718 /*
719  * there are many ways the trans_start and trans_end ioctls can lead
720  * to deadlocks.  They should only be used by applications that
721  * basically own the machine, and have a very in depth understanding
722  * of all the possible deadlocks and enospc problems.
723  */
724 long btrfs_ioctl_trans_end(struct file *file)
725 {
726         struct inode *inode = fdentry(file)->d_inode;
727         struct btrfs_root *root = BTRFS_I(inode)->root;
728         struct btrfs_trans_handle *trans;
729         int ret = 0;
730
731         trans = file->private_data;
732         if (!trans) {
733                 ret = -EINVAL;
734                 goto out;
735         }
736         btrfs_end_transaction(trans, root);
737         file->private_data = 0;
738 out:
739         return ret;
740 }
741
742 long btrfs_ioctl(struct file *file, unsigned int
743                 cmd, unsigned long arg)
744 {
745         struct btrfs_root *root = BTRFS_I(fdentry(file)->d_inode)->root;
746
747         switch (cmd) {
748         case BTRFS_IOC_SNAP_CREATE:
749                 return btrfs_ioctl_snap_create(root, (void __user *)arg);
750         case BTRFS_IOC_DEFRAG:
751                 return btrfs_ioctl_defrag(file);
752         case BTRFS_IOC_RESIZE:
753                 return btrfs_ioctl_resize(root, (void __user *)arg);
754         case BTRFS_IOC_ADD_DEV:
755                 return btrfs_ioctl_add_dev(root, (void __user *)arg);
756         case BTRFS_IOC_RM_DEV:
757                 return btrfs_ioctl_rm_dev(root, (void __user *)arg);
758         case BTRFS_IOC_BALANCE:
759                 return btrfs_balance(root->fs_info->dev_root);
760         case BTRFS_IOC_CLONE:
761                 return btrfs_ioctl_clone(file, arg);
762         case BTRFS_IOC_TRANS_START:
763                 return btrfs_ioctl_trans_start(file);
764         case BTRFS_IOC_TRANS_END:
765                 return btrfs_ioctl_trans_end(file);
766         case BTRFS_IOC_SYNC:
767                 btrfs_sync_fs(file->f_dentry->d_sb, 1);
768                 return 0;
769         }
770
771         return -ENOTTY;
772 }