Btrfs: remove outdated comment in btrfs_ioctl_resize()
[sfrench/cifs-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/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/xattr.h>
42 #include <linux/vmalloc.h>
43 #include "compat.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         u64 index = 0;
71         unsigned long nr = 1;
72
73         ret = btrfs_check_metadata_free_space(root);
74         if (ret)
75                 goto fail_commit;
76
77         trans = btrfs_start_transaction(root, 1);
78         BUG_ON(!trans);
79
80         ret = btrfs_find_free_objectid(trans, root->fs_info->tree_root,
81                                        0, &objectid);
82         if (ret)
83                 goto fail;
84
85         leaf = btrfs_alloc_free_block(trans, root, root->leafsize, 0,
86                                       objectid, trans->transid, 0, 0, 0);
87         if (IS_ERR(leaf)) {
88                 ret = PTR_ERR(leaf);
89                 goto fail;
90         }
91
92         btrfs_set_header_nritems(leaf, 0);
93         btrfs_set_header_level(leaf, 0);
94         btrfs_set_header_bytenr(leaf, leaf->start);
95         btrfs_set_header_generation(leaf, trans->transid);
96         btrfs_set_header_owner(leaf, objectid);
97
98         write_extent_buffer(leaf, root->fs_info->fsid,
99                             (unsigned long)btrfs_header_fsid(leaf),
100                             BTRFS_FSID_SIZE);
101         btrfs_mark_buffer_dirty(leaf);
102
103         inode_item = &root_item.inode;
104         memset(inode_item, 0, sizeof(*inode_item));
105         inode_item->generation = cpu_to_le64(1);
106         inode_item->size = cpu_to_le64(3);
107         inode_item->nlink = cpu_to_le32(1);
108         inode_item->nbytes = cpu_to_le64(root->leafsize);
109         inode_item->mode = cpu_to_le32(S_IFDIR | 0755);
110
111         btrfs_set_root_bytenr(&root_item, leaf->start);
112         btrfs_set_root_generation(&root_item, trans->transid);
113         btrfs_set_root_level(&root_item, 0);
114         btrfs_set_root_refs(&root_item, 1);
115         btrfs_set_root_used(&root_item, 0);
116         btrfs_set_root_last_snapshot(&root_item, 0);
117
118         memset(&root_item.drop_progress, 0, sizeof(root_item.drop_progress));
119         root_item.drop_level = 0;
120
121         btrfs_tree_unlock(leaf);
122         free_extent_buffer(leaf);
123         leaf = NULL;
124
125         btrfs_set_root_dirid(&root_item, new_dirid);
126
127         key.objectid = objectid;
128         key.offset = 1;
129         btrfs_set_key_type(&key, BTRFS_ROOT_ITEM_KEY);
130         ret = btrfs_insert_root(trans, root->fs_info->tree_root, &key,
131                                 &root_item);
132         if (ret)
133                 goto fail;
134
135         /*
136          * insert the directory item
137          */
138         key.offset = (u64)-1;
139         dir = dentry->d_parent->d_inode;
140         ret = btrfs_set_inode_index(dir, &index);
141         BUG_ON(ret);
142
143         ret = btrfs_insert_dir_item(trans, root,
144                                     name, namelen, dir->i_ino, &key,
145                                     BTRFS_FT_DIR, index);
146         if (ret)
147                 goto fail;
148
149         btrfs_i_size_write(dir, dir->i_size + namelen * 2);
150         ret = btrfs_update_inode(trans, root, dir);
151         BUG_ON(ret);
152
153         /* add the backref first */
154         ret = btrfs_add_root_ref(trans, root->fs_info->tree_root,
155                                  objectid, BTRFS_ROOT_BACKREF_KEY,
156                                  root->root_key.objectid,
157                                  dir->i_ino, index, name, namelen);
158
159         BUG_ON(ret);
160
161         /* now add the forward ref */
162         ret = btrfs_add_root_ref(trans, root->fs_info->tree_root,
163                                  root->root_key.objectid, BTRFS_ROOT_REF_KEY,
164                                  objectid,
165                                  dir->i_ino, index, name, namelen);
166
167         BUG_ON(ret);
168
169         ret = btrfs_commit_transaction(trans, root);
170         if (ret)
171                 goto fail_commit;
172
173         new_root = btrfs_read_fs_root_no_name(root->fs_info, &key);
174         BUG_ON(!new_root);
175
176         trans = btrfs_start_transaction(new_root, 1);
177         BUG_ON(!trans);
178
179         ret = btrfs_create_subvol_root(trans, new_root, dentry, new_dirid,
180                                        BTRFS_I(dir)->block_group);
181         if (ret)
182                 goto fail;
183
184 fail:
185         nr = trans->blocks_used;
186         err = btrfs_commit_transaction(trans, new_root);
187         if (err && !ret)
188                 ret = err;
189 fail_commit:
190         btrfs_btree_balance_dirty(root, nr);
191         return ret;
192 }
193
194 static int create_snapshot(struct btrfs_root *root, struct dentry *dentry,
195                            char *name, int namelen)
196 {
197         struct btrfs_pending_snapshot *pending_snapshot;
198         struct btrfs_trans_handle *trans;
199         int ret = 0;
200         int err;
201         unsigned long nr = 0;
202
203         if (!root->ref_cows)
204                 return -EINVAL;
205
206         ret = btrfs_check_metadata_free_space(root);
207         if (ret)
208                 goto fail_unlock;
209
210         pending_snapshot = kzalloc(sizeof(*pending_snapshot), GFP_NOFS);
211         if (!pending_snapshot) {
212                 ret = -ENOMEM;
213                 goto fail_unlock;
214         }
215         pending_snapshot->name = kmalloc(namelen + 1, GFP_NOFS);
216         if (!pending_snapshot->name) {
217                 ret = -ENOMEM;
218                 kfree(pending_snapshot);
219                 goto fail_unlock;
220         }
221         memcpy(pending_snapshot->name, name, namelen);
222         pending_snapshot->name[namelen] = '\0';
223         pending_snapshot->dentry = dentry;
224         trans = btrfs_start_transaction(root, 1);
225         BUG_ON(!trans);
226         pending_snapshot->root = root;
227         list_add(&pending_snapshot->list,
228                  &trans->transaction->pending_snapshots);
229         err = btrfs_commit_transaction(trans, root);
230
231 fail_unlock:
232         btrfs_btree_balance_dirty(root, nr);
233         return ret;
234 }
235
236 /* copy of may_create in fs/namei.c() */
237 static inline int btrfs_may_create(struct inode *dir, struct dentry *child)
238 {
239         if (child->d_inode)
240                 return -EEXIST;
241         if (IS_DEADDIR(dir))
242                 return -ENOENT;
243         return inode_permission(dir, MAY_WRITE | MAY_EXEC);
244 }
245
246 /*
247  * Create a new subvolume below @parent.  This is largely modeled after
248  * sys_mkdirat and vfs_mkdir, but we only do a single component lookup
249  * inside this filesystem so it's quite a bit simpler.
250  */
251 static noinline int btrfs_mksubvol(struct path *parent, char *name,
252                                    int mode, int namelen,
253                                    struct btrfs_root *snap_src)
254 {
255         struct dentry *dentry;
256         int error;
257
258         mutex_lock_nested(&parent->dentry->d_inode->i_mutex, I_MUTEX_PARENT);
259
260         dentry = lookup_one_len(name, parent->dentry, namelen);
261         error = PTR_ERR(dentry);
262         if (IS_ERR(dentry))
263                 goto out_unlock;
264
265         error = -EEXIST;
266         if (dentry->d_inode)
267                 goto out_dput;
268
269         if (!IS_POSIXACL(parent->dentry->d_inode))
270                 mode &= ~current_umask();
271
272         error = mnt_want_write(parent->mnt);
273         if (error)
274                 goto out_dput;
275
276         error = btrfs_may_create(parent->dentry->d_inode, dentry);
277         if (error)
278                 goto out_drop_write;
279
280         /*
281          * Actually perform the low-level subvolume creation after all
282          * this VFS fuzz.
283          *
284          * Eventually we want to pass in an inode under which we create this
285          * subvolume, but for now all are under the filesystem root.
286          *
287          * Also we should pass on the mode eventually to allow creating new
288          * subvolume with specific mode bits.
289          */
290         if (snap_src) {
291                 struct dentry *dir = dentry->d_parent;
292                 struct dentry *test = dir->d_parent;
293                 struct btrfs_path *path = btrfs_alloc_path();
294                 int ret;
295                 u64 test_oid;
296                 u64 parent_oid = BTRFS_I(dir->d_inode)->root->root_key.objectid;
297
298                 test_oid = snap_src->root_key.objectid;
299
300                 ret = btrfs_find_root_ref(snap_src->fs_info->tree_root,
301                                           path, parent_oid, test_oid);
302                 if (ret == 0)
303                         goto create;
304                 btrfs_release_path(snap_src->fs_info->tree_root, path);
305
306                 /* we need to make sure we aren't creating a directory loop
307                  * by taking a snapshot of something that has our current
308                  * subvol in its directory tree.  So, this loops through
309                  * the dentries and checks the forward refs for each subvolume
310                  * to see if is references the subvolume where we are
311                  * placing this new snapshot.
312                  */
313                 while (1) {
314                         if (!test ||
315                             dir == snap_src->fs_info->sb->s_root ||
316                             test == snap_src->fs_info->sb->s_root ||
317                             test->d_inode->i_sb != snap_src->fs_info->sb) {
318                                 break;
319                         }
320                         if (S_ISLNK(test->d_inode->i_mode)) {
321                                 printk(KERN_INFO "Btrfs symlink in snapshot "
322                                        "path, failed\n");
323                                 error = -EMLINK;
324                                 btrfs_free_path(path);
325                                 goto out_drop_write;
326                         }
327                         test_oid =
328                                 BTRFS_I(test->d_inode)->root->root_key.objectid;
329                         ret = btrfs_find_root_ref(snap_src->fs_info->tree_root,
330                                   path, test_oid, parent_oid);
331                         if (ret == 0) {
332                                 printk(KERN_INFO "Btrfs snapshot creation "
333                                        "failed, looping\n");
334                                 error = -EMLINK;
335                                 btrfs_free_path(path);
336                                 goto out_drop_write;
337                         }
338                         btrfs_release_path(snap_src->fs_info->tree_root, path);
339                         test = test->d_parent;
340                 }
341 create:
342                 btrfs_free_path(path);
343                 error = create_snapshot(snap_src, dentry, name, namelen);
344         } else {
345                 error = create_subvol(BTRFS_I(parent->dentry->d_inode)->root,
346                                       dentry, name, namelen);
347         }
348         if (error)
349                 goto out_drop_write;
350
351         fsnotify_mkdir(parent->dentry->d_inode, dentry);
352 out_drop_write:
353         mnt_drop_write(parent->mnt);
354 out_dput:
355         dput(dentry);
356 out_unlock:
357         mutex_unlock(&parent->dentry->d_inode->i_mutex);
358         return error;
359 }
360
361
362 static int btrfs_defrag_file(struct file *file)
363 {
364         struct inode *inode = fdentry(file)->d_inode;
365         struct btrfs_root *root = BTRFS_I(inode)->root;
366         struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
367         struct btrfs_ordered_extent *ordered;
368         struct page *page;
369         unsigned long last_index;
370         unsigned long ra_pages = root->fs_info->bdi.ra_pages;
371         unsigned long total_read = 0;
372         u64 page_start;
373         u64 page_end;
374         unsigned long i;
375         int ret;
376
377         ret = btrfs_check_data_free_space(root, inode, inode->i_size);
378         if (ret)
379                 return -ENOSPC;
380
381         mutex_lock(&inode->i_mutex);
382         last_index = inode->i_size >> PAGE_CACHE_SHIFT;
383         for (i = 0; i <= last_index; i++) {
384                 if (total_read % ra_pages == 0) {
385                         btrfs_force_ra(inode->i_mapping, &file->f_ra, file, i,
386                                        min(last_index, i + ra_pages - 1));
387                 }
388                 total_read++;
389 again:
390                 page = grab_cache_page(inode->i_mapping, i);
391                 if (!page)
392                         goto out_unlock;
393                 if (!PageUptodate(page)) {
394                         btrfs_readpage(NULL, page);
395                         lock_page(page);
396                         if (!PageUptodate(page)) {
397                                 unlock_page(page);
398                                 page_cache_release(page);
399                                 goto out_unlock;
400                         }
401                 }
402
403                 wait_on_page_writeback(page);
404
405                 page_start = (u64)page->index << PAGE_CACHE_SHIFT;
406                 page_end = page_start + PAGE_CACHE_SIZE - 1;
407                 lock_extent(io_tree, page_start, page_end, GFP_NOFS);
408
409                 ordered = btrfs_lookup_ordered_extent(inode, page_start);
410                 if (ordered) {
411                         unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
412                         unlock_page(page);
413                         page_cache_release(page);
414                         btrfs_start_ordered_extent(inode, ordered, 1);
415                         btrfs_put_ordered_extent(ordered);
416                         goto again;
417                 }
418                 set_page_extent_mapped(page);
419
420                 /*
421                  * this makes sure page_mkwrite is called on the
422                  * page if it is dirtied again later
423                  */
424                 clear_page_dirty_for_io(page);
425
426                 btrfs_set_extent_delalloc(inode, page_start, page_end);
427
428                 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
429                 set_page_dirty(page);
430                 unlock_page(page);
431                 page_cache_release(page);
432                 balance_dirty_pages_ratelimited_nr(inode->i_mapping, 1);
433         }
434
435 out_unlock:
436         mutex_unlock(&inode->i_mutex);
437         return 0;
438 }
439
440 static int btrfs_ioctl_resize(struct btrfs_root *root, void __user *arg)
441 {
442         u64 new_size;
443         u64 old_size;
444         u64 devid = 1;
445         struct btrfs_ioctl_vol_args *vol_args;
446         struct btrfs_trans_handle *trans;
447         struct btrfs_device *device = NULL;
448         char *sizestr;
449         char *devstr = NULL;
450         int ret = 0;
451         int namelen;
452         int mod = 0;
453
454         if (root->fs_info->sb->s_flags & MS_RDONLY)
455                 return -EROFS;
456
457         if (!capable(CAP_SYS_ADMIN))
458                 return -EPERM;
459
460         vol_args = kmalloc(sizeof(*vol_args), GFP_NOFS);
461
462         if (!vol_args)
463                 return -ENOMEM;
464
465         if (copy_from_user(vol_args, arg, sizeof(*vol_args))) {
466                 ret = -EFAULT;
467                 goto out;
468         }
469
470         vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
471         namelen = strlen(vol_args->name);
472
473         mutex_lock(&root->fs_info->volume_mutex);
474         sizestr = vol_args->name;
475         devstr = strchr(sizestr, ':');
476         if (devstr) {
477                 char *end;
478                 sizestr = devstr + 1;
479                 *devstr = '\0';
480                 devstr = vol_args->name;
481                 devid = simple_strtoull(devstr, &end, 10);
482                 printk(KERN_INFO "resizing devid %llu\n",
483                        (unsigned long long)devid);
484         }
485         device = btrfs_find_device(root, devid, NULL, NULL);
486         if (!device) {
487                 printk(KERN_INFO "resizer unable to find device %llu\n",
488                        (unsigned long long)devid);
489                 ret = -EINVAL;
490                 goto out_unlock;
491         }
492         if (!strcmp(sizestr, "max"))
493                 new_size = device->bdev->bd_inode->i_size;
494         else {
495                 if (sizestr[0] == '-') {
496                         mod = -1;
497                         sizestr++;
498                 } else if (sizestr[0] == '+') {
499                         mod = 1;
500                         sizestr++;
501                 }
502                 new_size = btrfs_parse_size(sizestr);
503                 if (new_size == 0) {
504                         ret = -EINVAL;
505                         goto out_unlock;
506                 }
507         }
508
509         old_size = device->total_bytes;
510
511         if (mod < 0) {
512                 if (new_size > old_size) {
513                         ret = -EINVAL;
514                         goto out_unlock;
515                 }
516                 new_size = old_size - new_size;
517         } else if (mod > 0) {
518                 new_size = old_size + new_size;
519         }
520
521         if (new_size < 256 * 1024 * 1024) {
522                 ret = -EINVAL;
523                 goto out_unlock;
524         }
525         if (new_size > device->bdev->bd_inode->i_size) {
526                 ret = -EFBIG;
527                 goto out_unlock;
528         }
529
530         do_div(new_size, root->sectorsize);
531         new_size *= root->sectorsize;
532
533         printk(KERN_INFO "new size for %s is %llu\n",
534                 device->name, (unsigned long long)new_size);
535
536         if (new_size > old_size) {
537                 trans = btrfs_start_transaction(root, 1);
538                 ret = btrfs_grow_device(trans, device, new_size);
539                 btrfs_commit_transaction(trans, root);
540         } else {
541                 ret = btrfs_shrink_device(device, new_size);
542         }
543
544 out_unlock:
545         mutex_unlock(&root->fs_info->volume_mutex);
546 out:
547         kfree(vol_args);
548         return ret;
549 }
550
551 static noinline int btrfs_ioctl_snap_create(struct file *file,
552                                             void __user *arg, int subvol)
553 {
554         struct btrfs_root *root = BTRFS_I(fdentry(file)->d_inode)->root;
555         struct btrfs_ioctl_vol_args *vol_args;
556         struct btrfs_dir_item *di;
557         struct btrfs_path *path;
558         struct file *src_file;
559         u64 root_dirid;
560         int namelen;
561         int ret = 0;
562
563         if (root->fs_info->sb->s_flags & MS_RDONLY)
564                 return -EROFS;
565
566         vol_args = kmalloc(sizeof(*vol_args), GFP_NOFS);
567
568         if (!vol_args)
569                 return -ENOMEM;
570
571         if (copy_from_user(vol_args, arg, sizeof(*vol_args))) {
572                 ret = -EFAULT;
573                 goto out;
574         }
575
576         vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
577         namelen = strlen(vol_args->name);
578         if (strchr(vol_args->name, '/')) {
579                 ret = -EINVAL;
580                 goto out;
581         }
582
583         path = btrfs_alloc_path();
584         if (!path) {
585                 ret = -ENOMEM;
586                 goto out;
587         }
588
589         root_dirid = root->fs_info->sb->s_root->d_inode->i_ino,
590         di = btrfs_lookup_dir_item(NULL, root->fs_info->tree_root,
591                             path, root_dirid,
592                             vol_args->name, namelen, 0);
593         btrfs_free_path(path);
594
595         if (di && !IS_ERR(di)) {
596                 ret = -EEXIST;
597                 goto out;
598         }
599
600         if (IS_ERR(di)) {
601                 ret = PTR_ERR(di);
602                 goto out;
603         }
604
605         if (subvol) {
606                 ret = btrfs_mksubvol(&file->f_path, vol_args->name,
607                                      file->f_path.dentry->d_inode->i_mode,
608                                      namelen, NULL);
609         } else {
610                 struct inode *src_inode;
611                 src_file = fget(vol_args->fd);
612                 if (!src_file) {
613                         ret = -EINVAL;
614                         goto out;
615                 }
616
617                 src_inode = src_file->f_path.dentry->d_inode;
618                 if (src_inode->i_sb != file->f_path.dentry->d_inode->i_sb) {
619                         printk(KERN_INFO "btrfs: Snapshot src from "
620                                "another FS\n");
621                         ret = -EINVAL;
622                         fput(src_file);
623                         goto out;
624                 }
625                 ret = btrfs_mksubvol(&file->f_path, vol_args->name,
626                              file->f_path.dentry->d_inode->i_mode,
627                              namelen, BTRFS_I(src_inode)->root);
628                 fput(src_file);
629         }
630
631 out:
632         kfree(vol_args);
633         return ret;
634 }
635
636 static int btrfs_ioctl_defrag(struct file *file)
637 {
638         struct inode *inode = fdentry(file)->d_inode;
639         struct btrfs_root *root = BTRFS_I(inode)->root;
640         int ret;
641
642         ret = mnt_want_write(file->f_path.mnt);
643         if (ret)
644                 return ret;
645
646         switch (inode->i_mode & S_IFMT) {
647         case S_IFDIR:
648                 if (!capable(CAP_SYS_ADMIN)) {
649                         ret = -EPERM;
650                         goto out;
651                 }
652                 btrfs_defrag_root(root, 0);
653                 btrfs_defrag_root(root->fs_info->extent_root, 0);
654                 break;
655         case S_IFREG:
656                 if (!(file->f_mode & FMODE_WRITE)) {
657                         ret = -EINVAL;
658                         goto out;
659                 }
660                 btrfs_defrag_file(file);
661                 break;
662         }
663 out:
664         mnt_drop_write(file->f_path.mnt);
665         return ret;
666 }
667
668 static long btrfs_ioctl_add_dev(struct btrfs_root *root, void __user *arg)
669 {
670         struct btrfs_ioctl_vol_args *vol_args;
671         int ret;
672
673         if (!capable(CAP_SYS_ADMIN))
674                 return -EPERM;
675
676         vol_args = kmalloc(sizeof(*vol_args), GFP_NOFS);
677
678         if (!vol_args)
679                 return -ENOMEM;
680
681         if (copy_from_user(vol_args, arg, sizeof(*vol_args))) {
682                 ret = -EFAULT;
683                 goto out;
684         }
685         vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
686         ret = btrfs_init_new_device(root, vol_args->name);
687
688 out:
689         kfree(vol_args);
690         return ret;
691 }
692
693 static long btrfs_ioctl_rm_dev(struct btrfs_root *root, void __user *arg)
694 {
695         struct btrfs_ioctl_vol_args *vol_args;
696         int ret;
697
698         if (!capable(CAP_SYS_ADMIN))
699                 return -EPERM;
700
701         if (root->fs_info->sb->s_flags & MS_RDONLY)
702                 return -EROFS;
703
704         vol_args = kmalloc(sizeof(*vol_args), GFP_NOFS);
705
706         if (!vol_args)
707                 return -ENOMEM;
708
709         if (copy_from_user(vol_args, arg, sizeof(*vol_args))) {
710                 ret = -EFAULT;
711                 goto out;
712         }
713         vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
714         ret = btrfs_rm_device(root, vol_args->name);
715
716 out:
717         kfree(vol_args);
718         return ret;
719 }
720
721 static long btrfs_ioctl_clone(struct file *file, unsigned long srcfd,
722                 u64 off, u64 olen, u64 destoff)
723 {
724         struct inode *inode = fdentry(file)->d_inode;
725         struct btrfs_root *root = BTRFS_I(inode)->root;
726         struct file *src_file;
727         struct inode *src;
728         struct btrfs_trans_handle *trans;
729         struct btrfs_path *path;
730         struct extent_buffer *leaf;
731         char *buf;
732         struct btrfs_key key;
733         u32 nritems;
734         int slot;
735         int ret;
736         u64 len = olen;
737         u64 bs = root->fs_info->sb->s_blocksize;
738         u64 hint_byte;
739
740         /*
741          * TODO:
742          * - split compressed inline extents.  annoying: we need to
743          *   decompress into destination's address_space (the file offset
744          *   may change, so source mapping won't do), then recompress (or
745          *   otherwise reinsert) a subrange.
746          * - allow ranges within the same file to be cloned (provided
747          *   they don't overlap)?
748          */
749
750         /* the destination must be opened for writing */
751         if (!(file->f_mode & FMODE_WRITE))
752                 return -EINVAL;
753
754         ret = mnt_want_write(file->f_path.mnt);
755         if (ret)
756                 return ret;
757
758         src_file = fget(srcfd);
759         if (!src_file) {
760                 ret = -EBADF;
761                 goto out_drop_write;
762         }
763         src = src_file->f_dentry->d_inode;
764
765         ret = -EINVAL;
766         if (src == inode)
767                 goto out_fput;
768
769         ret = -EISDIR;
770         if (S_ISDIR(src->i_mode) || S_ISDIR(inode->i_mode))
771                 goto out_fput;
772
773         ret = -EXDEV;
774         if (src->i_sb != inode->i_sb || BTRFS_I(src)->root != root)
775                 goto out_fput;
776
777         ret = -ENOMEM;
778         buf = vmalloc(btrfs_level_size(root, 0));
779         if (!buf)
780                 goto out_fput;
781
782         path = btrfs_alloc_path();
783         if (!path) {
784                 vfree(buf);
785                 goto out_fput;
786         }
787         path->reada = 2;
788
789         if (inode < src) {
790                 mutex_lock(&inode->i_mutex);
791                 mutex_lock(&src->i_mutex);
792         } else {
793                 mutex_lock(&src->i_mutex);
794                 mutex_lock(&inode->i_mutex);
795         }
796
797         /* determine range to clone */
798         ret = -EINVAL;
799         if (off >= src->i_size || off + len > src->i_size)
800                 goto out_unlock;
801         if (len == 0)
802                 olen = len = src->i_size - off;
803         /* if we extend to eof, continue to block boundary */
804         if (off + len == src->i_size)
805                 len = ((src->i_size + bs-1) & ~(bs-1))
806                         - off;
807
808         /* verify the end result is block aligned */
809         if ((off & (bs-1)) ||
810             ((off + len) & (bs-1)))
811                 goto out_unlock;
812
813         /* do any pending delalloc/csum calc on src, one way or
814            another, and lock file content */
815         while (1) {
816                 struct btrfs_ordered_extent *ordered;
817                 lock_extent(&BTRFS_I(src)->io_tree, off, off+len, GFP_NOFS);
818                 ordered = btrfs_lookup_first_ordered_extent(inode, off+len);
819                 if (BTRFS_I(src)->delalloc_bytes == 0 && !ordered)
820                         break;
821                 unlock_extent(&BTRFS_I(src)->io_tree, off, off+len, GFP_NOFS);
822                 if (ordered)
823                         btrfs_put_ordered_extent(ordered);
824                 btrfs_wait_ordered_range(src, off, off+len);
825         }
826
827         trans = btrfs_start_transaction(root, 1);
828         BUG_ON(!trans);
829
830         /* punch hole in destination first */
831         btrfs_drop_extents(trans, root, inode, off, off + len,
832                            off + len, 0, &hint_byte);
833
834         /* clone data */
835         key.objectid = src->i_ino;
836         key.type = BTRFS_EXTENT_DATA_KEY;
837         key.offset = 0;
838
839         while (1) {
840                 /*
841                  * note the key will change type as we walk through the
842                  * tree.
843                  */
844                 ret = btrfs_search_slot(trans, root, &key, path, 0, 0);
845                 if (ret < 0)
846                         goto out;
847
848                 nritems = btrfs_header_nritems(path->nodes[0]);
849                 if (path->slots[0] >= nritems) {
850                         ret = btrfs_next_leaf(root, path);
851                         if (ret < 0)
852                                 goto out;
853                         if (ret > 0)
854                                 break;
855                         nritems = btrfs_header_nritems(path->nodes[0]);
856                 }
857                 leaf = path->nodes[0];
858                 slot = path->slots[0];
859
860                 btrfs_item_key_to_cpu(leaf, &key, slot);
861                 if (btrfs_key_type(&key) > BTRFS_EXTENT_DATA_KEY ||
862                     key.objectid != src->i_ino)
863                         break;
864
865                 if (btrfs_key_type(&key) == BTRFS_EXTENT_DATA_KEY) {
866                         struct btrfs_file_extent_item *extent;
867                         int type;
868                         u32 size;
869                         struct btrfs_key new_key;
870                         u64 disko = 0, diskl = 0;
871                         u64 datao = 0, datal = 0;
872                         u8 comp;
873
874                         size = btrfs_item_size_nr(leaf, slot);
875                         read_extent_buffer(leaf, buf,
876                                            btrfs_item_ptr_offset(leaf, slot),
877                                            size);
878
879                         extent = btrfs_item_ptr(leaf, slot,
880                                                 struct btrfs_file_extent_item);
881                         comp = btrfs_file_extent_compression(leaf, extent);
882                         type = btrfs_file_extent_type(leaf, extent);
883                         if (type == BTRFS_FILE_EXTENT_REG) {
884                                 disko = btrfs_file_extent_disk_bytenr(leaf,
885                                                                       extent);
886                                 diskl = btrfs_file_extent_disk_num_bytes(leaf,
887                                                                  extent);
888                                 datao = btrfs_file_extent_offset(leaf, extent);
889                                 datal = btrfs_file_extent_num_bytes(leaf,
890                                                                     extent);
891                         } else if (type == BTRFS_FILE_EXTENT_INLINE) {
892                                 /* take upper bound, may be compressed */
893                                 datal = btrfs_file_extent_ram_bytes(leaf,
894                                                                     extent);
895                         }
896                         btrfs_release_path(root, path);
897
898                         if (key.offset + datal < off ||
899                             key.offset >= off+len)
900                                 goto next;
901
902                         memcpy(&new_key, &key, sizeof(new_key));
903                         new_key.objectid = inode->i_ino;
904                         new_key.offset = key.offset + destoff - off;
905
906                         if (type == BTRFS_FILE_EXTENT_REG) {
907                                 ret = btrfs_insert_empty_item(trans, root, path,
908                                                               &new_key, size);
909                                 if (ret)
910                                         goto out;
911
912                                 leaf = path->nodes[0];
913                                 slot = path->slots[0];
914                                 write_extent_buffer(leaf, buf,
915                                             btrfs_item_ptr_offset(leaf, slot),
916                                             size);
917
918                                 extent = btrfs_item_ptr(leaf, slot,
919                                                 struct btrfs_file_extent_item);
920
921                                 if (off > key.offset) {
922                                         datao += off - key.offset;
923                                         datal -= off - key.offset;
924                                 }
925                                 if (key.offset + datao + datal + key.offset >
926                                     off + len)
927                                         datal = off + len - key.offset - datao;
928                                 /* disko == 0 means it's a hole */
929                                 if (!disko)
930                                         datao = 0;
931
932                                 btrfs_set_file_extent_offset(leaf, extent,
933                                                              datao);
934                                 btrfs_set_file_extent_num_bytes(leaf, extent,
935                                                                 datal);
936                                 if (disko) {
937                                         inode_add_bytes(inode, datal);
938                                         ret = btrfs_inc_extent_ref(trans, root,
939                                                    disko, diskl, leaf->start,
940                                                    root->root_key.objectid,
941                                                    trans->transid,
942                                                    inode->i_ino);
943                                         BUG_ON(ret);
944                                 }
945                         } else if (type == BTRFS_FILE_EXTENT_INLINE) {
946                                 u64 skip = 0;
947                                 u64 trim = 0;
948                                 if (off > key.offset) {
949                                         skip = off - key.offset;
950                                         new_key.offset += skip;
951                                 }
952
953                                 if (key.offset + datal > off+len)
954                                         trim = key.offset + datal - (off+len);
955
956                                 if (comp && (skip || trim)) {
957                                         ret = -EINVAL;
958                                         goto out;
959                                 }
960                                 size -= skip + trim;
961                                 datal -= skip + trim;
962                                 ret = btrfs_insert_empty_item(trans, root, path,
963                                                               &new_key, size);
964                                 if (ret)
965                                         goto out;
966
967                                 if (skip) {
968                                         u32 start =
969                                           btrfs_file_extent_calc_inline_size(0);
970                                         memmove(buf+start, buf+start+skip,
971                                                 datal);
972                                 }
973
974                                 leaf = path->nodes[0];
975                                 slot = path->slots[0];
976                                 write_extent_buffer(leaf, buf,
977                                             btrfs_item_ptr_offset(leaf, slot),
978                                             size);
979                                 inode_add_bytes(inode, datal);
980                         }
981
982                         btrfs_mark_buffer_dirty(leaf);
983                 }
984
985 next:
986                 btrfs_release_path(root, path);
987                 key.offset++;
988         }
989         ret = 0;
990 out:
991         btrfs_release_path(root, path);
992         if (ret == 0) {
993                 inode->i_mtime = inode->i_ctime = CURRENT_TIME;
994                 if (destoff + olen > inode->i_size)
995                         btrfs_i_size_write(inode, destoff + olen);
996                 BTRFS_I(inode)->flags = BTRFS_I(src)->flags;
997                 ret = btrfs_update_inode(trans, root, inode);
998         }
999         btrfs_end_transaction(trans, root);
1000         unlock_extent(&BTRFS_I(src)->io_tree, off, off+len, GFP_NOFS);
1001         if (ret)
1002                 vmtruncate(inode, 0);
1003 out_unlock:
1004         mutex_unlock(&src->i_mutex);
1005         mutex_unlock(&inode->i_mutex);
1006         vfree(buf);
1007         btrfs_free_path(path);
1008 out_fput:
1009         fput(src_file);
1010 out_drop_write:
1011         mnt_drop_write(file->f_path.mnt);
1012         return ret;
1013 }
1014
1015 static long btrfs_ioctl_clone_range(struct file *file, void __user *argp)
1016 {
1017         struct btrfs_ioctl_clone_range_args args;
1018
1019         if (copy_from_user(&args, argp, sizeof(args)))
1020                 return -EFAULT;
1021         return btrfs_ioctl_clone(file, args.src_fd, args.src_offset,
1022                                  args.src_length, args.dest_offset);
1023 }
1024
1025 /*
1026  * there are many ways the trans_start and trans_end ioctls can lead
1027  * to deadlocks.  They should only be used by applications that
1028  * basically own the machine, and have a very in depth understanding
1029  * of all the possible deadlocks and enospc problems.
1030  */
1031 static long btrfs_ioctl_trans_start(struct file *file)
1032 {
1033         struct inode *inode = fdentry(file)->d_inode;
1034         struct btrfs_root *root = BTRFS_I(inode)->root;
1035         struct btrfs_trans_handle *trans;
1036         int ret = 0;
1037
1038         if (!capable(CAP_SYS_ADMIN))
1039                 return -EPERM;
1040
1041         if (file->private_data) {
1042                 ret = -EINPROGRESS;
1043                 goto out;
1044         }
1045
1046         ret = mnt_want_write(file->f_path.mnt);
1047         if (ret)
1048                 goto out;
1049
1050         mutex_lock(&root->fs_info->trans_mutex);
1051         root->fs_info->open_ioctl_trans++;
1052         mutex_unlock(&root->fs_info->trans_mutex);
1053
1054         trans = btrfs_start_ioctl_transaction(root, 0);
1055         if (trans)
1056                 file->private_data = trans;
1057         else
1058                 ret = -ENOMEM;
1059         /*printk(KERN_INFO "btrfs_ioctl_trans_start on %p\n", file);*/
1060 out:
1061         return ret;
1062 }
1063
1064 /*
1065  * there are many ways the trans_start and trans_end ioctls can lead
1066  * to deadlocks.  They should only be used by applications that
1067  * basically own the machine, and have a very in depth understanding
1068  * of all the possible deadlocks and enospc problems.
1069  */
1070 long btrfs_ioctl_trans_end(struct file *file)
1071 {
1072         struct inode *inode = fdentry(file)->d_inode;
1073         struct btrfs_root *root = BTRFS_I(inode)->root;
1074         struct btrfs_trans_handle *trans;
1075         int ret = 0;
1076
1077         trans = file->private_data;
1078         if (!trans) {
1079                 ret = -EINVAL;
1080                 goto out;
1081         }
1082         btrfs_end_transaction(trans, root);
1083         file->private_data = NULL;
1084
1085         mutex_lock(&root->fs_info->trans_mutex);
1086         root->fs_info->open_ioctl_trans--;
1087         mutex_unlock(&root->fs_info->trans_mutex);
1088
1089         mnt_drop_write(file->f_path.mnt);
1090
1091 out:
1092         return ret;
1093 }
1094
1095 long btrfs_ioctl(struct file *file, unsigned int
1096                 cmd, unsigned long arg)
1097 {
1098         struct btrfs_root *root = BTRFS_I(fdentry(file)->d_inode)->root;
1099         void __user *argp = (void __user *)arg;
1100
1101         switch (cmd) {
1102         case BTRFS_IOC_SNAP_CREATE:
1103                 return btrfs_ioctl_snap_create(file, argp, 0);
1104         case BTRFS_IOC_SUBVOL_CREATE:
1105                 return btrfs_ioctl_snap_create(file, argp, 1);
1106         case BTRFS_IOC_DEFRAG:
1107                 return btrfs_ioctl_defrag(file);
1108         case BTRFS_IOC_RESIZE:
1109                 return btrfs_ioctl_resize(root, argp);
1110         case BTRFS_IOC_ADD_DEV:
1111                 return btrfs_ioctl_add_dev(root, argp);
1112         case BTRFS_IOC_RM_DEV:
1113                 return btrfs_ioctl_rm_dev(root, argp);
1114         case BTRFS_IOC_BALANCE:
1115                 return btrfs_balance(root->fs_info->dev_root);
1116         case BTRFS_IOC_CLONE:
1117                 return btrfs_ioctl_clone(file, arg, 0, 0, 0);
1118         case BTRFS_IOC_CLONE_RANGE:
1119                 return btrfs_ioctl_clone_range(file, argp);
1120         case BTRFS_IOC_TRANS_START:
1121                 return btrfs_ioctl_trans_start(file);
1122         case BTRFS_IOC_TRANS_END:
1123                 return btrfs_ioctl_trans_end(file);
1124         case BTRFS_IOC_SYNC:
1125                 btrfs_sync_fs(file->f_dentry->d_sb, 1);
1126                 return 0;
1127         }
1128
1129         return -ENOTTY;
1130 }