mm: close page_mkwrite races
[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/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 /*
441  * Called inside transaction, so use GFP_NOFS
442  */
443
444 static int btrfs_ioctl_resize(struct btrfs_root *root, void __user *arg)
445 {
446         u64 new_size;
447         u64 old_size;
448         u64 devid = 1;
449         struct btrfs_ioctl_vol_args *vol_args;
450         struct btrfs_trans_handle *trans;
451         struct btrfs_device *device = NULL;
452         char *sizestr;
453         char *devstr = NULL;
454         int ret = 0;
455         int namelen;
456         int mod = 0;
457
458         if (root->fs_info->sb->s_flags & MS_RDONLY)
459                 return -EROFS;
460
461         if (!capable(CAP_SYS_ADMIN))
462                 return -EPERM;
463
464         vol_args = memdup_user(arg, sizeof(*vol_args));
465         if (IS_ERR(vol_args))
466                 return PTR_ERR(vol_args);
467
468         vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
469         namelen = strlen(vol_args->name);
470
471         mutex_lock(&root->fs_info->volume_mutex);
472         sizestr = vol_args->name;
473         devstr = strchr(sizestr, ':');
474         if (devstr) {
475                 char *end;
476                 sizestr = devstr + 1;
477                 *devstr = '\0';
478                 devstr = vol_args->name;
479                 devid = simple_strtoull(devstr, &end, 10);
480                 printk(KERN_INFO "resizing devid %llu\n",
481                        (unsigned long long)devid);
482         }
483         device = btrfs_find_device(root, devid, NULL, NULL);
484         if (!device) {
485                 printk(KERN_INFO "resizer unable to find device %llu\n",
486                        (unsigned long long)devid);
487                 ret = -EINVAL;
488                 goto out_unlock;
489         }
490         if (!strcmp(sizestr, "max"))
491                 new_size = device->bdev->bd_inode->i_size;
492         else {
493                 if (sizestr[0] == '-') {
494                         mod = -1;
495                         sizestr++;
496                 } else if (sizestr[0] == '+') {
497                         mod = 1;
498                         sizestr++;
499                 }
500                 new_size = btrfs_parse_size(sizestr);
501                 if (new_size == 0) {
502                         ret = -EINVAL;
503                         goto out_unlock;
504                 }
505         }
506
507         old_size = device->total_bytes;
508
509         if (mod < 0) {
510                 if (new_size > old_size) {
511                         ret = -EINVAL;
512                         goto out_unlock;
513                 }
514                 new_size = old_size - new_size;
515         } else if (mod > 0) {
516                 new_size = old_size + new_size;
517         }
518
519         if (new_size < 256 * 1024 * 1024) {
520                 ret = -EINVAL;
521                 goto out_unlock;
522         }
523         if (new_size > device->bdev->bd_inode->i_size) {
524                 ret = -EFBIG;
525                 goto out_unlock;
526         }
527
528         do_div(new_size, root->sectorsize);
529         new_size *= root->sectorsize;
530
531         printk(KERN_INFO "new size for %s is %llu\n",
532                 device->name, (unsigned long long)new_size);
533
534         if (new_size > old_size) {
535                 trans = btrfs_start_transaction(root, 1);
536                 ret = btrfs_grow_device(trans, device, new_size);
537                 btrfs_commit_transaction(trans, root);
538         } else {
539                 ret = btrfs_shrink_device(device, new_size);
540         }
541
542 out_unlock:
543         mutex_unlock(&root->fs_info->volume_mutex);
544         kfree(vol_args);
545         return ret;
546 }
547
548 static noinline int btrfs_ioctl_snap_create(struct file *file,
549                                             void __user *arg, int subvol)
550 {
551         struct btrfs_root *root = BTRFS_I(fdentry(file)->d_inode)->root;
552         struct btrfs_ioctl_vol_args *vol_args;
553         struct btrfs_dir_item *di;
554         struct btrfs_path *path;
555         struct file *src_file;
556         u64 root_dirid;
557         int namelen;
558         int ret = 0;
559
560         if (root->fs_info->sb->s_flags & MS_RDONLY)
561                 return -EROFS;
562
563         vol_args = memdup_user(arg, sizeof(*vol_args));
564         if (IS_ERR(vol_args))
565                 return PTR_ERR(vol_args);
566
567         vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
568         namelen = strlen(vol_args->name);
569         if (strchr(vol_args->name, '/')) {
570                 ret = -EINVAL;
571                 goto out;
572         }
573
574         path = btrfs_alloc_path();
575         if (!path) {
576                 ret = -ENOMEM;
577                 goto out;
578         }
579
580         root_dirid = root->fs_info->sb->s_root->d_inode->i_ino,
581         di = btrfs_lookup_dir_item(NULL, root->fs_info->tree_root,
582                             path, root_dirid,
583                             vol_args->name, namelen, 0);
584         btrfs_free_path(path);
585
586         if (di && !IS_ERR(di)) {
587                 ret = -EEXIST;
588                 goto out;
589         }
590
591         if (IS_ERR(di)) {
592                 ret = PTR_ERR(di);
593                 goto out;
594         }
595
596         if (subvol) {
597                 ret = btrfs_mksubvol(&file->f_path, vol_args->name,
598                                      file->f_path.dentry->d_inode->i_mode,
599                                      namelen, NULL);
600         } else {
601                 struct inode *src_inode;
602                 src_file = fget(vol_args->fd);
603                 if (!src_file) {
604                         ret = -EINVAL;
605                         goto out;
606                 }
607
608                 src_inode = src_file->f_path.dentry->d_inode;
609                 if (src_inode->i_sb != file->f_path.dentry->d_inode->i_sb) {
610                         printk(KERN_INFO "btrfs: Snapshot src from "
611                                "another FS\n");
612                         ret = -EINVAL;
613                         fput(src_file);
614                         goto out;
615                 }
616                 ret = btrfs_mksubvol(&file->f_path, vol_args->name,
617                              file->f_path.dentry->d_inode->i_mode,
618                              namelen, BTRFS_I(src_inode)->root);
619                 fput(src_file);
620         }
621
622 out:
623         kfree(vol_args);
624         return ret;
625 }
626
627 static int btrfs_ioctl_defrag(struct file *file)
628 {
629         struct inode *inode = fdentry(file)->d_inode;
630         struct btrfs_root *root = BTRFS_I(inode)->root;
631         int ret;
632
633         ret = mnt_want_write(file->f_path.mnt);
634         if (ret)
635                 return ret;
636
637         switch (inode->i_mode & S_IFMT) {
638         case S_IFDIR:
639                 if (!capable(CAP_SYS_ADMIN)) {
640                         ret = -EPERM;
641                         goto out;
642                 }
643                 btrfs_defrag_root(root, 0);
644                 btrfs_defrag_root(root->fs_info->extent_root, 0);
645                 break;
646         case S_IFREG:
647                 if (!(file->f_mode & FMODE_WRITE)) {
648                         ret = -EINVAL;
649                         goto out;
650                 }
651                 btrfs_defrag_file(file);
652                 break;
653         }
654 out:
655         mnt_drop_write(file->f_path.mnt);
656         return ret;
657 }
658
659 static long btrfs_ioctl_add_dev(struct btrfs_root *root, void __user *arg)
660 {
661         struct btrfs_ioctl_vol_args *vol_args;
662         int ret;
663
664         if (!capable(CAP_SYS_ADMIN))
665                 return -EPERM;
666
667         vol_args = memdup_user(arg, sizeof(*vol_args));
668         if (IS_ERR(vol_args))
669                 return PTR_ERR(vol_args);
670
671         vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
672         ret = btrfs_init_new_device(root, vol_args->name);
673
674         kfree(vol_args);
675         return ret;
676 }
677
678 static long btrfs_ioctl_rm_dev(struct btrfs_root *root, void __user *arg)
679 {
680         struct btrfs_ioctl_vol_args *vol_args;
681         int ret;
682
683         if (!capable(CAP_SYS_ADMIN))
684                 return -EPERM;
685
686         if (root->fs_info->sb->s_flags & MS_RDONLY)
687                 return -EROFS;
688
689         vol_args = memdup_user(arg, sizeof(*vol_args));
690         if (IS_ERR(vol_args))
691                 return PTR_ERR(vol_args);
692
693         vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
694         ret = btrfs_rm_device(root, vol_args->name);
695
696         kfree(vol_args);
697         return ret;
698 }
699
700 static long btrfs_ioctl_clone(struct file *file, unsigned long srcfd,
701                 u64 off, u64 olen, u64 destoff)
702 {
703         struct inode *inode = fdentry(file)->d_inode;
704         struct btrfs_root *root = BTRFS_I(inode)->root;
705         struct file *src_file;
706         struct inode *src;
707         struct btrfs_trans_handle *trans;
708         struct btrfs_path *path;
709         struct extent_buffer *leaf;
710         char *buf;
711         struct btrfs_key key;
712         u32 nritems;
713         int slot;
714         int ret;
715         u64 len = olen;
716         u64 bs = root->fs_info->sb->s_blocksize;
717         u64 hint_byte;
718
719         /*
720          * TODO:
721          * - split compressed inline extents.  annoying: we need to
722          *   decompress into destination's address_space (the file offset
723          *   may change, so source mapping won't do), then recompress (or
724          *   otherwise reinsert) a subrange.
725          * - allow ranges within the same file to be cloned (provided
726          *   they don't overlap)?
727          */
728
729         /* the destination must be opened for writing */
730         if (!(file->f_mode & FMODE_WRITE))
731                 return -EINVAL;
732
733         ret = mnt_want_write(file->f_path.mnt);
734         if (ret)
735                 return ret;
736
737         src_file = fget(srcfd);
738         if (!src_file) {
739                 ret = -EBADF;
740                 goto out_drop_write;
741         }
742         src = src_file->f_dentry->d_inode;
743
744         ret = -EINVAL;
745         if (src == inode)
746                 goto out_fput;
747
748         ret = -EISDIR;
749         if (S_ISDIR(src->i_mode) || S_ISDIR(inode->i_mode))
750                 goto out_fput;
751
752         ret = -EXDEV;
753         if (src->i_sb != inode->i_sb || BTRFS_I(src)->root != root)
754                 goto out_fput;
755
756         ret = -ENOMEM;
757         buf = vmalloc(btrfs_level_size(root, 0));
758         if (!buf)
759                 goto out_fput;
760
761         path = btrfs_alloc_path();
762         if (!path) {
763                 vfree(buf);
764                 goto out_fput;
765         }
766         path->reada = 2;
767
768         if (inode < src) {
769                 mutex_lock(&inode->i_mutex);
770                 mutex_lock(&src->i_mutex);
771         } else {
772                 mutex_lock(&src->i_mutex);
773                 mutex_lock(&inode->i_mutex);
774         }
775
776         /* determine range to clone */
777         ret = -EINVAL;
778         if (off >= src->i_size || off + len > src->i_size)
779                 goto out_unlock;
780         if (len == 0)
781                 olen = len = src->i_size - off;
782         /* if we extend to eof, continue to block boundary */
783         if (off + len == src->i_size)
784                 len = ((src->i_size + bs-1) & ~(bs-1))
785                         - off;
786
787         /* verify the end result is block aligned */
788         if ((off & (bs-1)) ||
789             ((off + len) & (bs-1)))
790                 goto out_unlock;
791
792         /* do any pending delalloc/csum calc on src, one way or
793            another, and lock file content */
794         while (1) {
795                 struct btrfs_ordered_extent *ordered;
796                 lock_extent(&BTRFS_I(src)->io_tree, off, off+len, GFP_NOFS);
797                 ordered = btrfs_lookup_first_ordered_extent(inode, off+len);
798                 if (BTRFS_I(src)->delalloc_bytes == 0 && !ordered)
799                         break;
800                 unlock_extent(&BTRFS_I(src)->io_tree, off, off+len, GFP_NOFS);
801                 if (ordered)
802                         btrfs_put_ordered_extent(ordered);
803                 btrfs_wait_ordered_range(src, off, off+len);
804         }
805
806         trans = btrfs_start_transaction(root, 1);
807         BUG_ON(!trans);
808
809         /* punch hole in destination first */
810         btrfs_drop_extents(trans, root, inode, off, off + len,
811                            off + len, 0, &hint_byte);
812
813         /* clone data */
814         key.objectid = src->i_ino;
815         key.type = BTRFS_EXTENT_DATA_KEY;
816         key.offset = 0;
817
818         while (1) {
819                 /*
820                  * note the key will change type as we walk through the
821                  * tree.
822                  */
823                 ret = btrfs_search_slot(trans, root, &key, path, 0, 0);
824                 if (ret < 0)
825                         goto out;
826
827                 nritems = btrfs_header_nritems(path->nodes[0]);
828                 if (path->slots[0] >= nritems) {
829                         ret = btrfs_next_leaf(root, path);
830                         if (ret < 0)
831                                 goto out;
832                         if (ret > 0)
833                                 break;
834                         nritems = btrfs_header_nritems(path->nodes[0]);
835                 }
836                 leaf = path->nodes[0];
837                 slot = path->slots[0];
838
839                 btrfs_item_key_to_cpu(leaf, &key, slot);
840                 if (btrfs_key_type(&key) > BTRFS_EXTENT_DATA_KEY ||
841                     key.objectid != src->i_ino)
842                         break;
843
844                 if (btrfs_key_type(&key) == BTRFS_EXTENT_DATA_KEY) {
845                         struct btrfs_file_extent_item *extent;
846                         int type;
847                         u32 size;
848                         struct btrfs_key new_key;
849                         u64 disko = 0, diskl = 0;
850                         u64 datao = 0, datal = 0;
851                         u8 comp;
852
853                         size = btrfs_item_size_nr(leaf, slot);
854                         read_extent_buffer(leaf, buf,
855                                            btrfs_item_ptr_offset(leaf, slot),
856                                            size);
857
858                         extent = btrfs_item_ptr(leaf, slot,
859                                                 struct btrfs_file_extent_item);
860                         comp = btrfs_file_extent_compression(leaf, extent);
861                         type = btrfs_file_extent_type(leaf, extent);
862                         if (type == BTRFS_FILE_EXTENT_REG) {
863                                 disko = btrfs_file_extent_disk_bytenr(leaf,
864                                                                       extent);
865                                 diskl = btrfs_file_extent_disk_num_bytes(leaf,
866                                                                  extent);
867                                 datao = btrfs_file_extent_offset(leaf, extent);
868                                 datal = btrfs_file_extent_num_bytes(leaf,
869                                                                     extent);
870                         } else if (type == BTRFS_FILE_EXTENT_INLINE) {
871                                 /* take upper bound, may be compressed */
872                                 datal = btrfs_file_extent_ram_bytes(leaf,
873                                                                     extent);
874                         }
875                         btrfs_release_path(root, path);
876
877                         if (key.offset + datal < off ||
878                             key.offset >= off+len)
879                                 goto next;
880
881                         memcpy(&new_key, &key, sizeof(new_key));
882                         new_key.objectid = inode->i_ino;
883                         new_key.offset = key.offset + destoff - off;
884
885                         if (type == BTRFS_FILE_EXTENT_REG) {
886                                 ret = btrfs_insert_empty_item(trans, root, path,
887                                                               &new_key, size);
888                                 if (ret)
889                                         goto out;
890
891                                 leaf = path->nodes[0];
892                                 slot = path->slots[0];
893                                 write_extent_buffer(leaf, buf,
894                                             btrfs_item_ptr_offset(leaf, slot),
895                                             size);
896
897                                 extent = btrfs_item_ptr(leaf, slot,
898                                                 struct btrfs_file_extent_item);
899
900                                 if (off > key.offset) {
901                                         datao += off - key.offset;
902                                         datal -= off - key.offset;
903                                 }
904                                 if (key.offset + datao + datal + key.offset >
905                                     off + len)
906                                         datal = off + len - key.offset - datao;
907                                 /* disko == 0 means it's a hole */
908                                 if (!disko)
909                                         datao = 0;
910
911                                 btrfs_set_file_extent_offset(leaf, extent,
912                                                              datao);
913                                 btrfs_set_file_extent_num_bytes(leaf, extent,
914                                                                 datal);
915                                 if (disko) {
916                                         inode_add_bytes(inode, datal);
917                                         ret = btrfs_inc_extent_ref(trans, root,
918                                                    disko, diskl, leaf->start,
919                                                    root->root_key.objectid,
920                                                    trans->transid,
921                                                    inode->i_ino);
922                                         BUG_ON(ret);
923                                 }
924                         } else if (type == BTRFS_FILE_EXTENT_INLINE) {
925                                 u64 skip = 0;
926                                 u64 trim = 0;
927                                 if (off > key.offset) {
928                                         skip = off - key.offset;
929                                         new_key.offset += skip;
930                                 }
931
932                                 if (key.offset + datal > off+len)
933                                         trim = key.offset + datal - (off+len);
934
935                                 if (comp && (skip || trim)) {
936                                         ret = -EINVAL;
937                                         goto out;
938                                 }
939                                 size -= skip + trim;
940                                 datal -= skip + trim;
941                                 ret = btrfs_insert_empty_item(trans, root, path,
942                                                               &new_key, size);
943                                 if (ret)
944                                         goto out;
945
946                                 if (skip) {
947                                         u32 start =
948                                           btrfs_file_extent_calc_inline_size(0);
949                                         memmove(buf+start, buf+start+skip,
950                                                 datal);
951                                 }
952
953                                 leaf = path->nodes[0];
954                                 slot = path->slots[0];
955                                 write_extent_buffer(leaf, buf,
956                                             btrfs_item_ptr_offset(leaf, slot),
957                                             size);
958                                 inode_add_bytes(inode, datal);
959                         }
960
961                         btrfs_mark_buffer_dirty(leaf);
962                 }
963
964 next:
965                 btrfs_release_path(root, path);
966                 key.offset++;
967         }
968         ret = 0;
969 out:
970         btrfs_release_path(root, path);
971         if (ret == 0) {
972                 inode->i_mtime = inode->i_ctime = CURRENT_TIME;
973                 if (destoff + olen > inode->i_size)
974                         btrfs_i_size_write(inode, destoff + olen);
975                 BTRFS_I(inode)->flags = BTRFS_I(src)->flags;
976                 ret = btrfs_update_inode(trans, root, inode);
977         }
978         btrfs_end_transaction(trans, root);
979         unlock_extent(&BTRFS_I(src)->io_tree, off, off+len, GFP_NOFS);
980         if (ret)
981                 vmtruncate(inode, 0);
982 out_unlock:
983         mutex_unlock(&src->i_mutex);
984         mutex_unlock(&inode->i_mutex);
985         vfree(buf);
986         btrfs_free_path(path);
987 out_fput:
988         fput(src_file);
989 out_drop_write:
990         mnt_drop_write(file->f_path.mnt);
991         return ret;
992 }
993
994 static long btrfs_ioctl_clone_range(struct file *file, void __user *argp)
995 {
996         struct btrfs_ioctl_clone_range_args args;
997
998         if (copy_from_user(&args, argp, sizeof(args)))
999                 return -EFAULT;
1000         return btrfs_ioctl_clone(file, args.src_fd, args.src_offset,
1001                                  args.src_length, args.dest_offset);
1002 }
1003
1004 /*
1005  * there are many ways the trans_start and trans_end ioctls can lead
1006  * to deadlocks.  They should only be used by applications that
1007  * basically own the machine, and have a very in depth understanding
1008  * of all the possible deadlocks and enospc problems.
1009  */
1010 static long btrfs_ioctl_trans_start(struct file *file)
1011 {
1012         struct inode *inode = fdentry(file)->d_inode;
1013         struct btrfs_root *root = BTRFS_I(inode)->root;
1014         struct btrfs_trans_handle *trans;
1015         int ret = 0;
1016
1017         if (!capable(CAP_SYS_ADMIN))
1018                 return -EPERM;
1019
1020         if (file->private_data) {
1021                 ret = -EINPROGRESS;
1022                 goto out;
1023         }
1024
1025         ret = mnt_want_write(file->f_path.mnt);
1026         if (ret)
1027                 goto out;
1028
1029         mutex_lock(&root->fs_info->trans_mutex);
1030         root->fs_info->open_ioctl_trans++;
1031         mutex_unlock(&root->fs_info->trans_mutex);
1032
1033         trans = btrfs_start_ioctl_transaction(root, 0);
1034         if (trans)
1035                 file->private_data = trans;
1036         else
1037                 ret = -ENOMEM;
1038         /*printk(KERN_INFO "btrfs_ioctl_trans_start on %p\n", file);*/
1039 out:
1040         return ret;
1041 }
1042
1043 /*
1044  * there are many ways the trans_start and trans_end ioctls can lead
1045  * to deadlocks.  They should only be used by applications that
1046  * basically own the machine, and have a very in depth understanding
1047  * of all the possible deadlocks and enospc problems.
1048  */
1049 long btrfs_ioctl_trans_end(struct file *file)
1050 {
1051         struct inode *inode = fdentry(file)->d_inode;
1052         struct btrfs_root *root = BTRFS_I(inode)->root;
1053         struct btrfs_trans_handle *trans;
1054         int ret = 0;
1055
1056         trans = file->private_data;
1057         if (!trans) {
1058                 ret = -EINVAL;
1059                 goto out;
1060         }
1061         btrfs_end_transaction(trans, root);
1062         file->private_data = NULL;
1063
1064         mutex_lock(&root->fs_info->trans_mutex);
1065         root->fs_info->open_ioctl_trans--;
1066         mutex_unlock(&root->fs_info->trans_mutex);
1067
1068         mnt_drop_write(file->f_path.mnt);
1069
1070 out:
1071         return ret;
1072 }
1073
1074 long btrfs_ioctl(struct file *file, unsigned int
1075                 cmd, unsigned long arg)
1076 {
1077         struct btrfs_root *root = BTRFS_I(fdentry(file)->d_inode)->root;
1078         void __user *argp = (void __user *)arg;
1079
1080         switch (cmd) {
1081         case BTRFS_IOC_SNAP_CREATE:
1082                 return btrfs_ioctl_snap_create(file, argp, 0);
1083         case BTRFS_IOC_SUBVOL_CREATE:
1084                 return btrfs_ioctl_snap_create(file, argp, 1);
1085         case BTRFS_IOC_DEFRAG:
1086                 return btrfs_ioctl_defrag(file);
1087         case BTRFS_IOC_RESIZE:
1088                 return btrfs_ioctl_resize(root, argp);
1089         case BTRFS_IOC_ADD_DEV:
1090                 return btrfs_ioctl_add_dev(root, argp);
1091         case BTRFS_IOC_RM_DEV:
1092                 return btrfs_ioctl_rm_dev(root, argp);
1093         case BTRFS_IOC_BALANCE:
1094                 return btrfs_balance(root->fs_info->dev_root);
1095         case BTRFS_IOC_CLONE:
1096                 return btrfs_ioctl_clone(file, arg, 0, 0, 0);
1097         case BTRFS_IOC_CLONE_RANGE:
1098                 return btrfs_ioctl_clone_range(file, argp);
1099         case BTRFS_IOC_TRANS_START:
1100                 return btrfs_ioctl_trans_start(file);
1101         case BTRFS_IOC_TRANS_END:
1102                 return btrfs_ioctl_trans_end(file);
1103         case BTRFS_IOC_SYNC:
1104                 btrfs_sync_fs(file->f_dentry->d_sb, 1);
1105                 return 0;
1106         }
1107
1108         return -ENOTTY;
1109 }