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