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