diff options
Diffstat (limited to 'fs/btrfs/ioctl.c')
| -rw-r--r-- | fs/btrfs/ioctl.c | 897 |
1 files changed, 795 insertions, 102 deletions
diff --git a/fs/btrfs/ioctl.c b/fs/btrfs/ioctl.c index 645a17927a8f..9254b3d58dbe 100644 --- a/fs/btrfs/ioctl.c +++ b/fs/btrfs/ioctl.c | |||
| @@ -39,6 +39,7 @@ | |||
| 39 | #include <linux/security.h> | 39 | #include <linux/security.h> |
| 40 | #include <linux/xattr.h> | 40 | #include <linux/xattr.h> |
| 41 | #include <linux/vmalloc.h> | 41 | #include <linux/vmalloc.h> |
| 42 | #include <linux/slab.h> | ||
| 42 | #include "compat.h" | 43 | #include "compat.h" |
| 43 | #include "ctree.h" | 44 | #include "ctree.h" |
| 44 | #include "disk-io.h" | 45 | #include "disk-io.h" |
| @@ -238,23 +239,19 @@ static noinline int create_subvol(struct btrfs_root *root, | |||
| 238 | u64 new_dirid = BTRFS_FIRST_FREE_OBJECTID; | 239 | u64 new_dirid = BTRFS_FIRST_FREE_OBJECTID; |
| 239 | u64 index = 0; | 240 | u64 index = 0; |
| 240 | 241 | ||
| 242 | ret = btrfs_find_free_objectid(NULL, root->fs_info->tree_root, | ||
| 243 | 0, &objectid); | ||
| 244 | if (ret) | ||
| 245 | return ret; | ||
| 241 | /* | 246 | /* |
| 242 | * 1 - inode item | 247 | * 1 - inode item |
| 243 | * 2 - refs | 248 | * 2 - refs |
| 244 | * 1 - root item | 249 | * 1 - root item |
| 245 | * 2 - dir items | 250 | * 2 - dir items |
| 246 | */ | 251 | */ |
| 247 | ret = btrfs_reserve_metadata_space(root, 6); | 252 | trans = btrfs_start_transaction(root, 6); |
| 248 | if (ret) | 253 | if (IS_ERR(trans)) |
| 249 | return ret; | 254 | return PTR_ERR(trans); |
| 250 | |||
| 251 | trans = btrfs_start_transaction(root, 1); | ||
| 252 | BUG_ON(!trans); | ||
| 253 | |||
| 254 | ret = btrfs_find_free_objectid(trans, root->fs_info->tree_root, | ||
| 255 | 0, &objectid); | ||
| 256 | if (ret) | ||
| 257 | goto fail; | ||
| 258 | 255 | ||
| 259 | leaf = btrfs_alloc_free_block(trans, root, root->leafsize, | 256 | leaf = btrfs_alloc_free_block(trans, root, root->leafsize, |
| 260 | 0, objectid, NULL, 0, 0, 0); | 257 | 0, objectid, NULL, 0, 0, 0); |
| @@ -344,13 +341,10 @@ fail: | |||
| 344 | err = btrfs_commit_transaction(trans, root); | 341 | err = btrfs_commit_transaction(trans, root); |
| 345 | if (err && !ret) | 342 | if (err && !ret) |
| 346 | ret = err; | 343 | ret = err; |
| 347 | |||
| 348 | btrfs_unreserve_metadata_space(root, 6); | ||
| 349 | return ret; | 344 | return ret; |
| 350 | } | 345 | } |
| 351 | 346 | ||
| 352 | static int create_snapshot(struct btrfs_root *root, struct dentry *dentry, | 347 | static int create_snapshot(struct btrfs_root *root, struct dentry *dentry) |
| 353 | char *name, int namelen) | ||
| 354 | { | 348 | { |
| 355 | struct inode *inode; | 349 | struct inode *inode; |
| 356 | struct btrfs_pending_snapshot *pending_snapshot; | 350 | struct btrfs_pending_snapshot *pending_snapshot; |
| @@ -360,40 +354,33 @@ static int create_snapshot(struct btrfs_root *root, struct dentry *dentry, | |||
| 360 | if (!root->ref_cows) | 354 | if (!root->ref_cows) |
| 361 | return -EINVAL; | 355 | return -EINVAL; |
| 362 | 356 | ||
| 363 | /* | ||
| 364 | * 1 - inode item | ||
| 365 | * 2 - refs | ||
| 366 | * 1 - root item | ||
| 367 | * 2 - dir items | ||
| 368 | */ | ||
| 369 | ret = btrfs_reserve_metadata_space(root, 6); | ||
| 370 | if (ret) | ||
| 371 | goto fail; | ||
| 372 | |||
| 373 | pending_snapshot = kzalloc(sizeof(*pending_snapshot), GFP_NOFS); | 357 | pending_snapshot = kzalloc(sizeof(*pending_snapshot), GFP_NOFS); |
| 374 | if (!pending_snapshot) { | 358 | if (!pending_snapshot) |
| 375 | ret = -ENOMEM; | 359 | return -ENOMEM; |
| 376 | btrfs_unreserve_metadata_space(root, 6); | 360 | |
| 377 | goto fail; | 361 | btrfs_init_block_rsv(&pending_snapshot->block_rsv); |
| 378 | } | ||
| 379 | pending_snapshot->name = kmalloc(namelen + 1, GFP_NOFS); | ||
| 380 | if (!pending_snapshot->name) { | ||
| 381 | ret = -ENOMEM; | ||
| 382 | kfree(pending_snapshot); | ||
| 383 | btrfs_unreserve_metadata_space(root, 6); | ||
| 384 | goto fail; | ||
| 385 | } | ||
| 386 | memcpy(pending_snapshot->name, name, namelen); | ||
| 387 | pending_snapshot->name[namelen] = '\0'; | ||
| 388 | pending_snapshot->dentry = dentry; | 362 | pending_snapshot->dentry = dentry; |
| 389 | trans = btrfs_start_transaction(root, 1); | ||
| 390 | BUG_ON(!trans); | ||
| 391 | pending_snapshot->root = root; | 363 | pending_snapshot->root = root; |
| 364 | |||
| 365 | trans = btrfs_start_transaction(root->fs_info->extent_root, 5); | ||
| 366 | if (IS_ERR(trans)) { | ||
| 367 | ret = PTR_ERR(trans); | ||
| 368 | goto fail; | ||
| 369 | } | ||
| 370 | |||
| 371 | ret = btrfs_snap_reserve_metadata(trans, pending_snapshot); | ||
| 372 | BUG_ON(ret); | ||
| 373 | |||
| 392 | list_add(&pending_snapshot->list, | 374 | list_add(&pending_snapshot->list, |
| 393 | &trans->transaction->pending_snapshots); | 375 | &trans->transaction->pending_snapshots); |
| 394 | ret = btrfs_commit_transaction(trans, root); | 376 | ret = btrfs_commit_transaction(trans, root->fs_info->extent_root); |
| 395 | BUG_ON(ret); | 377 | BUG_ON(ret); |
| 396 | btrfs_unreserve_metadata_space(root, 6); | 378 | |
| 379 | ret = pending_snapshot->error; | ||
| 380 | if (ret) | ||
| 381 | goto fail; | ||
| 382 | |||
| 383 | btrfs_orphan_cleanup(pending_snapshot->snap); | ||
| 397 | 384 | ||
| 398 | inode = btrfs_lookup_dentry(dentry->d_parent->d_inode, dentry); | 385 | inode = btrfs_lookup_dentry(dentry->d_parent->d_inode, dentry); |
| 399 | if (IS_ERR(inode)) { | 386 | if (IS_ERR(inode)) { |
| @@ -404,6 +391,7 @@ static int create_snapshot(struct btrfs_root *root, struct dentry *dentry, | |||
| 404 | d_instantiate(dentry, inode); | 391 | d_instantiate(dentry, inode); |
| 405 | ret = 0; | 392 | ret = 0; |
| 406 | fail: | 393 | fail: |
| 394 | kfree(pending_snapshot); | ||
| 407 | return ret; | 395 | return ret; |
| 408 | } | 396 | } |
| 409 | 397 | ||
| @@ -455,8 +443,7 @@ static noinline int btrfs_mksubvol(struct path *parent, | |||
| 455 | goto out_up_read; | 443 | goto out_up_read; |
| 456 | 444 | ||
| 457 | if (snap_src) { | 445 | if (snap_src) { |
| 458 | error = create_snapshot(snap_src, dentry, | 446 | error = create_snapshot(snap_src, dentry); |
| 459 | name, namelen); | ||
| 460 | } else { | 447 | } else { |
| 461 | error = create_subvol(BTRFS_I(dir)->root, dentry, | 448 | error = create_subvol(BTRFS_I(dir)->root, dentry, |
| 462 | name, namelen); | 449 | name, namelen); |
| @@ -474,7 +461,79 @@ out_unlock: | |||
| 474 | return error; | 461 | return error; |
| 475 | } | 462 | } |
| 476 | 463 | ||
| 477 | static int btrfs_defrag_file(struct file *file) | 464 | static int should_defrag_range(struct inode *inode, u64 start, u64 len, |
| 465 | int thresh, u64 *last_len, u64 *skip, | ||
| 466 | u64 *defrag_end) | ||
| 467 | { | ||
| 468 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; | ||
| 469 | struct extent_map *em = NULL; | ||
| 470 | struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree; | ||
| 471 | int ret = 1; | ||
| 472 | |||
| 473 | |||
| 474 | if (thresh == 0) | ||
| 475 | thresh = 256 * 1024; | ||
| 476 | |||
| 477 | /* | ||
| 478 | * make sure that once we start defragging and extent, we keep on | ||
| 479 | * defragging it | ||
| 480 | */ | ||
| 481 | if (start < *defrag_end) | ||
| 482 | return 1; | ||
| 483 | |||
| 484 | *skip = 0; | ||
| 485 | |||
| 486 | /* | ||
| 487 | * hopefully we have this extent in the tree already, try without | ||
| 488 | * the full extent lock | ||
| 489 | */ | ||
| 490 | read_lock(&em_tree->lock); | ||
| 491 | em = lookup_extent_mapping(em_tree, start, len); | ||
| 492 | read_unlock(&em_tree->lock); | ||
| 493 | |||
| 494 | if (!em) { | ||
| 495 | /* get the big lock and read metadata off disk */ | ||
| 496 | lock_extent(io_tree, start, start + len - 1, GFP_NOFS); | ||
| 497 | em = btrfs_get_extent(inode, NULL, 0, start, len, 0); | ||
| 498 | unlock_extent(io_tree, start, start + len - 1, GFP_NOFS); | ||
| 499 | |||
| 500 | if (IS_ERR(em)) | ||
| 501 | return 0; | ||
| 502 | } | ||
| 503 | |||
| 504 | /* this will cover holes, and inline extents */ | ||
| 505 | if (em->block_start >= EXTENT_MAP_LAST_BYTE) | ||
| 506 | ret = 0; | ||
| 507 | |||
| 508 | /* | ||
| 509 | * we hit a real extent, if it is big don't bother defragging it again | ||
| 510 | */ | ||
| 511 | if ((*last_len == 0 || *last_len >= thresh) && em->len >= thresh) | ||
| 512 | ret = 0; | ||
| 513 | |||
| 514 | /* | ||
| 515 | * last_len ends up being a counter of how many bytes we've defragged. | ||
| 516 | * every time we choose not to defrag an extent, we reset *last_len | ||
| 517 | * so that the next tiny extent will force a defrag. | ||
| 518 | * | ||
| 519 | * The end result of this is that tiny extents before a single big | ||
| 520 | * extent will force at least part of that big extent to be defragged. | ||
| 521 | */ | ||
| 522 | if (ret) { | ||
| 523 | *last_len += len; | ||
| 524 | *defrag_end = extent_map_end(em); | ||
| 525 | } else { | ||
| 526 | *last_len = 0; | ||
| 527 | *skip = extent_map_end(em); | ||
| 528 | *defrag_end = 0; | ||
| 529 | } | ||
| 530 | |||
| 531 | free_extent_map(em); | ||
| 532 | return ret; | ||
| 533 | } | ||
| 534 | |||
| 535 | static int btrfs_defrag_file(struct file *file, | ||
| 536 | struct btrfs_ioctl_defrag_range_args *range) | ||
| 478 | { | 537 | { |
| 479 | struct inode *inode = fdentry(file)->d_inode; | 538 | struct inode *inode = fdentry(file)->d_inode; |
| 480 | struct btrfs_root *root = BTRFS_I(inode)->root; | 539 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| @@ -486,37 +545,88 @@ static int btrfs_defrag_file(struct file *file) | |||
| 486 | unsigned long total_read = 0; | 545 | unsigned long total_read = 0; |
| 487 | u64 page_start; | 546 | u64 page_start; |
| 488 | u64 page_end; | 547 | u64 page_end; |
| 548 | u64 last_len = 0; | ||
| 549 | u64 skip = 0; | ||
| 550 | u64 defrag_end = 0; | ||
| 489 | unsigned long i; | 551 | unsigned long i; |
| 490 | int ret; | 552 | int ret; |
| 491 | 553 | ||
| 492 | ret = btrfs_check_data_free_space(root, inode, inode->i_size); | 554 | if (inode->i_size == 0) |
| 493 | if (ret) | 555 | return 0; |
| 494 | return -ENOSPC; | 556 | |
| 557 | if (range->start + range->len > range->start) { | ||
| 558 | last_index = min_t(u64, inode->i_size - 1, | ||
| 559 | range->start + range->len - 1) >> PAGE_CACHE_SHIFT; | ||
| 560 | } else { | ||
| 561 | last_index = (inode->i_size - 1) >> PAGE_CACHE_SHIFT; | ||
| 562 | } | ||
| 563 | |||
| 564 | i = range->start >> PAGE_CACHE_SHIFT; | ||
| 565 | while (i <= last_index) { | ||
| 566 | if (!should_defrag_range(inode, (u64)i << PAGE_CACHE_SHIFT, | ||
| 567 | PAGE_CACHE_SIZE, | ||
| 568 | range->extent_thresh, | ||
| 569 | &last_len, &skip, | ||
| 570 | &defrag_end)) { | ||
| 571 | unsigned long next; | ||
| 572 | /* | ||
| 573 | * the should_defrag function tells us how much to skip | ||
| 574 | * bump our counter by the suggested amount | ||
| 575 | */ | ||
| 576 | next = (skip + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT; | ||
| 577 | i = max(i + 1, next); | ||
| 578 | continue; | ||
| 579 | } | ||
| 495 | 580 | ||
| 496 | mutex_lock(&inode->i_mutex); | ||
| 497 | last_index = inode->i_size >> PAGE_CACHE_SHIFT; | ||
| 498 | for (i = 0; i <= last_index; i++) { | ||
| 499 | if (total_read % ra_pages == 0) { | 581 | if (total_read % ra_pages == 0) { |
| 500 | btrfs_force_ra(inode->i_mapping, &file->f_ra, file, i, | 582 | btrfs_force_ra(inode->i_mapping, &file->f_ra, file, i, |
| 501 | min(last_index, i + ra_pages - 1)); | 583 | min(last_index, i + ra_pages - 1)); |
| 502 | } | 584 | } |
| 503 | total_read++; | 585 | total_read++; |
| 586 | mutex_lock(&inode->i_mutex); | ||
| 587 | if (range->flags & BTRFS_DEFRAG_RANGE_COMPRESS) | ||
| 588 | BTRFS_I(inode)->force_compress = 1; | ||
| 589 | |||
| 590 | ret = btrfs_delalloc_reserve_space(inode, PAGE_CACHE_SIZE); | ||
| 591 | if (ret) | ||
| 592 | goto err_unlock; | ||
| 504 | again: | 593 | again: |
| 594 | if (inode->i_size == 0 || | ||
| 595 | i > ((inode->i_size - 1) >> PAGE_CACHE_SHIFT)) { | ||
| 596 | ret = 0; | ||
| 597 | goto err_reservations; | ||
| 598 | } | ||
| 599 | |||
| 505 | page = grab_cache_page(inode->i_mapping, i); | 600 | page = grab_cache_page(inode->i_mapping, i); |
| 506 | if (!page) | 601 | if (!page) { |
| 507 | goto out_unlock; | 602 | ret = -ENOMEM; |
| 603 | goto err_reservations; | ||
| 604 | } | ||
| 605 | |||
| 508 | if (!PageUptodate(page)) { | 606 | if (!PageUptodate(page)) { |
| 509 | btrfs_readpage(NULL, page); | 607 | btrfs_readpage(NULL, page); |
| 510 | lock_page(page); | 608 | lock_page(page); |
| 511 | if (!PageUptodate(page)) { | 609 | if (!PageUptodate(page)) { |
| 512 | unlock_page(page); | 610 | unlock_page(page); |
| 513 | page_cache_release(page); | 611 | page_cache_release(page); |
| 514 | goto out_unlock; | 612 | ret = -EIO; |
| 613 | goto err_reservations; | ||
| 515 | } | 614 | } |
| 516 | } | 615 | } |
| 517 | 616 | ||
| 617 | if (page->mapping != inode->i_mapping) { | ||
| 618 | unlock_page(page); | ||
| 619 | page_cache_release(page); | ||
| 620 | goto again; | ||
| 621 | } | ||
| 622 | |||
| 518 | wait_on_page_writeback(page); | 623 | wait_on_page_writeback(page); |
| 519 | 624 | ||
| 625 | if (PageDirty(page)) { | ||
| 626 | btrfs_delalloc_release_space(inode, PAGE_CACHE_SIZE); | ||
| 627 | goto loop_unlock; | ||
| 628 | } | ||
| 629 | |||
| 520 | page_start = (u64)page->index << PAGE_CACHE_SHIFT; | 630 | page_start = (u64)page->index << PAGE_CACHE_SHIFT; |
| 521 | page_end = page_start + PAGE_CACHE_SIZE - 1; | 631 | page_end = page_start + PAGE_CACHE_SIZE - 1; |
| 522 | lock_extent(io_tree, page_start, page_end, GFP_NOFS); | 632 | lock_extent(io_tree, page_start, page_end, GFP_NOFS); |
| @@ -537,18 +647,53 @@ again: | |||
| 537 | * page if it is dirtied again later | 647 | * page if it is dirtied again later |
| 538 | */ | 648 | */ |
| 539 | clear_page_dirty_for_io(page); | 649 | clear_page_dirty_for_io(page); |
| 650 | clear_extent_bits(&BTRFS_I(inode)->io_tree, page_start, | ||
| 651 | page_end, EXTENT_DIRTY | EXTENT_DELALLOC | | ||
| 652 | EXTENT_DO_ACCOUNTING, GFP_NOFS); | ||
| 540 | 653 | ||
| 541 | btrfs_set_extent_delalloc(inode, page_start, page_end); | 654 | btrfs_set_extent_delalloc(inode, page_start, page_end, NULL); |
| 655 | ClearPageChecked(page); | ||
| 542 | set_page_dirty(page); | 656 | set_page_dirty(page); |
| 543 | unlock_extent(io_tree, page_start, page_end, GFP_NOFS); | 657 | unlock_extent(io_tree, page_start, page_end, GFP_NOFS); |
| 658 | |||
| 659 | loop_unlock: | ||
| 544 | unlock_page(page); | 660 | unlock_page(page); |
| 545 | page_cache_release(page); | 661 | page_cache_release(page); |
| 662 | mutex_unlock(&inode->i_mutex); | ||
| 663 | |||
| 546 | balance_dirty_pages_ratelimited_nr(inode->i_mapping, 1); | 664 | balance_dirty_pages_ratelimited_nr(inode->i_mapping, 1); |
| 665 | i++; | ||
| 666 | } | ||
| 667 | |||
| 668 | if ((range->flags & BTRFS_DEFRAG_RANGE_START_IO)) | ||
| 669 | filemap_flush(inode->i_mapping); | ||
| 670 | |||
| 671 | if ((range->flags & BTRFS_DEFRAG_RANGE_COMPRESS)) { | ||
| 672 | /* the filemap_flush will queue IO into the worker threads, but | ||
| 673 | * we have to make sure the IO is actually started and that | ||
| 674 | * ordered extents get created before we return | ||
| 675 | */ | ||
| 676 | atomic_inc(&root->fs_info->async_submit_draining); | ||
| 677 | while (atomic_read(&root->fs_info->nr_async_submits) || | ||
| 678 | atomic_read(&root->fs_info->async_delalloc_pages)) { | ||
| 679 | wait_event(root->fs_info->async_submit_wait, | ||
| 680 | (atomic_read(&root->fs_info->nr_async_submits) == 0 && | ||
| 681 | atomic_read(&root->fs_info->async_delalloc_pages) == 0)); | ||
| 682 | } | ||
| 683 | atomic_dec(&root->fs_info->async_submit_draining); | ||
| 684 | |||
| 685 | mutex_lock(&inode->i_mutex); | ||
| 686 | BTRFS_I(inode)->force_compress = 0; | ||
| 687 | mutex_unlock(&inode->i_mutex); | ||
| 547 | } | 688 | } |
| 548 | 689 | ||
| 549 | out_unlock: | ||
| 550 | mutex_unlock(&inode->i_mutex); | ||
| 551 | return 0; | 690 | return 0; |
| 691 | |||
| 692 | err_reservations: | ||
| 693 | btrfs_delalloc_release_space(inode, PAGE_CACHE_SIZE); | ||
| 694 | err_unlock: | ||
| 695 | mutex_unlock(&inode->i_mutex); | ||
| 696 | return ret; | ||
| 552 | } | 697 | } |
| 553 | 698 | ||
| 554 | static noinline int btrfs_ioctl_resize(struct btrfs_root *root, | 699 | static noinline int btrfs_ioctl_resize(struct btrfs_root *root, |
| @@ -608,7 +753,7 @@ static noinline int btrfs_ioctl_resize(struct btrfs_root *root, | |||
| 608 | mod = 1; | 753 | mod = 1; |
| 609 | sizestr++; | 754 | sizestr++; |
| 610 | } | 755 | } |
| 611 | new_size = btrfs_parse_size(sizestr); | 756 | new_size = memparse(sizestr, NULL); |
| 612 | if (new_size == 0) { | 757 | if (new_size == 0) { |
| 613 | ret = -EINVAL; | 758 | ret = -EINVAL; |
| 614 | goto out_unlock; | 759 | goto out_unlock; |
| @@ -643,7 +788,7 @@ static noinline int btrfs_ioctl_resize(struct btrfs_root *root, | |||
| 643 | device->name, (unsigned long long)new_size); | 788 | device->name, (unsigned long long)new_size); |
| 644 | 789 | ||
| 645 | if (new_size > old_size) { | 790 | if (new_size > old_size) { |
| 646 | trans = btrfs_start_transaction(root, 1); | 791 | trans = btrfs_start_transaction(root, 0); |
| 647 | ret = btrfs_grow_device(trans, device, new_size); | 792 | ret = btrfs_grow_device(trans, device, new_size); |
| 648 | btrfs_commit_transaction(trans, root); | 793 | btrfs_commit_transaction(trans, root); |
| 649 | } else { | 794 | } else { |
| @@ -743,6 +888,330 @@ out: | |||
| 743 | return ret; | 888 | return ret; |
| 744 | } | 889 | } |
| 745 | 890 | ||
| 891 | static noinline int key_in_sk(struct btrfs_key *key, | ||
| 892 | struct btrfs_ioctl_search_key *sk) | ||
| 893 | { | ||
| 894 | struct btrfs_key test; | ||
| 895 | int ret; | ||
| 896 | |||
| 897 | test.objectid = sk->min_objectid; | ||
| 898 | test.type = sk->min_type; | ||
| 899 | test.offset = sk->min_offset; | ||
| 900 | |||
| 901 | ret = btrfs_comp_cpu_keys(key, &test); | ||
| 902 | if (ret < 0) | ||
| 903 | return 0; | ||
| 904 | |||
| 905 | test.objectid = sk->max_objectid; | ||
| 906 | test.type = sk->max_type; | ||
| 907 | test.offset = sk->max_offset; | ||
| 908 | |||
| 909 | ret = btrfs_comp_cpu_keys(key, &test); | ||
| 910 | if (ret > 0) | ||
| 911 | return 0; | ||
| 912 | return 1; | ||
| 913 | } | ||
| 914 | |||
| 915 | static noinline int copy_to_sk(struct btrfs_root *root, | ||
| 916 | struct btrfs_path *path, | ||
| 917 | struct btrfs_key *key, | ||
| 918 | struct btrfs_ioctl_search_key *sk, | ||
| 919 | char *buf, | ||
| 920 | unsigned long *sk_offset, | ||
| 921 | int *num_found) | ||
| 922 | { | ||
| 923 | u64 found_transid; | ||
| 924 | struct extent_buffer *leaf; | ||
| 925 | struct btrfs_ioctl_search_header sh; | ||
| 926 | unsigned long item_off; | ||
| 927 | unsigned long item_len; | ||
| 928 | int nritems; | ||
| 929 | int i; | ||
| 930 | int slot; | ||
| 931 | int found = 0; | ||
| 932 | int ret = 0; | ||
| 933 | |||
| 934 | leaf = path->nodes[0]; | ||
| 935 | slot = path->slots[0]; | ||
| 936 | nritems = btrfs_header_nritems(leaf); | ||
| 937 | |||
| 938 | if (btrfs_header_generation(leaf) > sk->max_transid) { | ||
| 939 | i = nritems; | ||
| 940 | goto advance_key; | ||
| 941 | } | ||
| 942 | found_transid = btrfs_header_generation(leaf); | ||
| 943 | |||
| 944 | for (i = slot; i < nritems; i++) { | ||
| 945 | item_off = btrfs_item_ptr_offset(leaf, i); | ||
| 946 | item_len = btrfs_item_size_nr(leaf, i); | ||
| 947 | |||
| 948 | if (item_len > BTRFS_SEARCH_ARGS_BUFSIZE) | ||
| 949 | item_len = 0; | ||
| 950 | |||
| 951 | if (sizeof(sh) + item_len + *sk_offset > | ||
| 952 | BTRFS_SEARCH_ARGS_BUFSIZE) { | ||
| 953 | ret = 1; | ||
| 954 | goto overflow; | ||
| 955 | } | ||
| 956 | |||
| 957 | btrfs_item_key_to_cpu(leaf, key, i); | ||
| 958 | if (!key_in_sk(key, sk)) | ||
| 959 | continue; | ||
| 960 | |||
| 961 | sh.objectid = key->objectid; | ||
| 962 | sh.offset = key->offset; | ||
| 963 | sh.type = key->type; | ||
| 964 | sh.len = item_len; | ||
| 965 | sh.transid = found_transid; | ||
| 966 | |||
| 967 | /* copy search result header */ | ||
| 968 | memcpy(buf + *sk_offset, &sh, sizeof(sh)); | ||
| 969 | *sk_offset += sizeof(sh); | ||
| 970 | |||
| 971 | if (item_len) { | ||
| 972 | char *p = buf + *sk_offset; | ||
| 973 | /* copy the item */ | ||
| 974 | read_extent_buffer(leaf, p, | ||
| 975 | item_off, item_len); | ||
| 976 | *sk_offset += item_len; | ||
| 977 | } | ||
| 978 | found++; | ||
| 979 | |||
| 980 | if (*num_found >= sk->nr_items) | ||
| 981 | break; | ||
| 982 | } | ||
| 983 | advance_key: | ||
| 984 | ret = 0; | ||
| 985 | if (key->offset < (u64)-1 && key->offset < sk->max_offset) | ||
| 986 | key->offset++; | ||
| 987 | else if (key->type < (u8)-1 && key->type < sk->max_type) { | ||
| 988 | key->offset = 0; | ||
| 989 | key->type++; | ||
| 990 | } else if (key->objectid < (u64)-1 && key->objectid < sk->max_objectid) { | ||
| 991 | key->offset = 0; | ||
| 992 | key->type = 0; | ||
| 993 | key->objectid++; | ||
| 994 | } else | ||
| 995 | ret = 1; | ||
| 996 | overflow: | ||
| 997 | *num_found += found; | ||
| 998 | return ret; | ||
| 999 | } | ||
| 1000 | |||
| 1001 | static noinline int search_ioctl(struct inode *inode, | ||
| 1002 | struct btrfs_ioctl_search_args *args) | ||
| 1003 | { | ||
| 1004 | struct btrfs_root *root; | ||
| 1005 | struct btrfs_key key; | ||
| 1006 | struct btrfs_key max_key; | ||
| 1007 | struct btrfs_path *path; | ||
| 1008 | struct btrfs_ioctl_search_key *sk = &args->key; | ||
| 1009 | struct btrfs_fs_info *info = BTRFS_I(inode)->root->fs_info; | ||
| 1010 | int ret; | ||
| 1011 | int num_found = 0; | ||
| 1012 | unsigned long sk_offset = 0; | ||
| 1013 | |||
| 1014 | path = btrfs_alloc_path(); | ||
| 1015 | if (!path) | ||
| 1016 | return -ENOMEM; | ||
| 1017 | |||
| 1018 | if (sk->tree_id == 0) { | ||
| 1019 | /* search the root of the inode that was passed */ | ||
| 1020 | root = BTRFS_I(inode)->root; | ||
| 1021 | } else { | ||
| 1022 | key.objectid = sk->tree_id; | ||
| 1023 | key.type = BTRFS_ROOT_ITEM_KEY; | ||
| 1024 | key.offset = (u64)-1; | ||
| 1025 | root = btrfs_read_fs_root_no_name(info, &key); | ||
| 1026 | if (IS_ERR(root)) { | ||
| 1027 | printk(KERN_ERR "could not find root %llu\n", | ||
| 1028 | sk->tree_id); | ||
| 1029 | btrfs_free_path(path); | ||
| 1030 | return -ENOENT; | ||
| 1031 | } | ||
| 1032 | } | ||
| 1033 | |||
| 1034 | key.objectid = sk->min_objectid; | ||
| 1035 | key.type = sk->min_type; | ||
| 1036 | key.offset = sk->min_offset; | ||
| 1037 | |||
| 1038 | max_key.objectid = sk->max_objectid; | ||
| 1039 | max_key.type = sk->max_type; | ||
| 1040 | max_key.offset = sk->max_offset; | ||
| 1041 | |||
| 1042 | path->keep_locks = 1; | ||
| 1043 | |||
| 1044 | while(1) { | ||
| 1045 | ret = btrfs_search_forward(root, &key, &max_key, path, 0, | ||
| 1046 | sk->min_transid); | ||
| 1047 | if (ret != 0) { | ||
| 1048 | if (ret > 0) | ||
| 1049 | ret = 0; | ||
| 1050 | goto err; | ||
| 1051 | } | ||
| 1052 | ret = copy_to_sk(root, path, &key, sk, args->buf, | ||
| 1053 | &sk_offset, &num_found); | ||
| 1054 | btrfs_release_path(root, path); | ||
| 1055 | if (ret || num_found >= sk->nr_items) | ||
| 1056 | break; | ||
| 1057 | |||
| 1058 | } | ||
| 1059 | ret = 0; | ||
| 1060 | err: | ||
| 1061 | sk->nr_items = num_found; | ||
| 1062 | btrfs_free_path(path); | ||
| 1063 | return ret; | ||
| 1064 | } | ||
| 1065 | |||
| 1066 | static noinline int btrfs_ioctl_tree_search(struct file *file, | ||
| 1067 | void __user *argp) | ||
| 1068 | { | ||
| 1069 | struct btrfs_ioctl_search_args *args; | ||
| 1070 | struct inode *inode; | ||
| 1071 | int ret; | ||
| 1072 | |||
| 1073 | if (!capable(CAP_SYS_ADMIN)) | ||
| 1074 | return -EPERM; | ||
| 1075 | |||
| 1076 | args = kmalloc(sizeof(*args), GFP_KERNEL); | ||
| 1077 | if (!args) | ||
| 1078 | return -ENOMEM; | ||
| 1079 | |||
| 1080 | if (copy_from_user(args, argp, sizeof(*args))) { | ||
| 1081 | kfree(args); | ||
| 1082 | return -EFAULT; | ||
| 1083 | } | ||
| 1084 | inode = fdentry(file)->d_inode; | ||
| 1085 | ret = search_ioctl(inode, args); | ||
| 1086 | if (ret == 0 && copy_to_user(argp, args, sizeof(*args))) | ||
| 1087 | ret = -EFAULT; | ||
| 1088 | kfree(args); | ||
| 1089 | return ret; | ||
| 1090 | } | ||
| 1091 | |||
| 1092 | /* | ||
| 1093 | * Search INODE_REFs to identify path name of 'dirid' directory | ||
| 1094 | * in a 'tree_id' tree. and sets path name to 'name'. | ||
| 1095 | */ | ||
| 1096 | static noinline int btrfs_search_path_in_tree(struct btrfs_fs_info *info, | ||
| 1097 | u64 tree_id, u64 dirid, char *name) | ||
| 1098 | { | ||
| 1099 | struct btrfs_root *root; | ||
| 1100 | struct btrfs_key key; | ||
| 1101 | char *ptr; | ||
| 1102 | int ret = -1; | ||
| 1103 | int slot; | ||
| 1104 | int len; | ||
| 1105 | int total_len = 0; | ||
| 1106 | struct btrfs_inode_ref *iref; | ||
| 1107 | struct extent_buffer *l; | ||
| 1108 | struct btrfs_path *path; | ||
| 1109 | |||
| 1110 | if (dirid == BTRFS_FIRST_FREE_OBJECTID) { | ||
| 1111 | name[0]='\0'; | ||
| 1112 | return 0; | ||
| 1113 | } | ||
| 1114 | |||
| 1115 | path = btrfs_alloc_path(); | ||
| 1116 | if (!path) | ||
| 1117 | return -ENOMEM; | ||
| 1118 | |||
| 1119 | ptr = &name[BTRFS_INO_LOOKUP_PATH_MAX]; | ||
| 1120 | |||
| 1121 | key.objectid = tree_id; | ||
| 1122 | key.type = BTRFS_ROOT_ITEM_KEY; | ||
| 1123 | key.offset = (u64)-1; | ||
| 1124 | root = btrfs_read_fs_root_no_name(info, &key); | ||
| 1125 | if (IS_ERR(root)) { | ||
| 1126 | printk(KERN_ERR "could not find root %llu\n", tree_id); | ||
| 1127 | ret = -ENOENT; | ||
| 1128 | goto out; | ||
| 1129 | } | ||
| 1130 | |||
| 1131 | key.objectid = dirid; | ||
| 1132 | key.type = BTRFS_INODE_REF_KEY; | ||
| 1133 | key.offset = (u64)-1; | ||
| 1134 | |||
| 1135 | while(1) { | ||
| 1136 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); | ||
| 1137 | if (ret < 0) | ||
| 1138 | goto out; | ||
| 1139 | |||
| 1140 | l = path->nodes[0]; | ||
| 1141 | slot = path->slots[0]; | ||
| 1142 | if (ret > 0 && slot > 0) | ||
| 1143 | slot--; | ||
| 1144 | btrfs_item_key_to_cpu(l, &key, slot); | ||
| 1145 | |||
| 1146 | if (ret > 0 && (key.objectid != dirid || | ||
| 1147 | key.type != BTRFS_INODE_REF_KEY)) { | ||
| 1148 | ret = -ENOENT; | ||
| 1149 | goto out; | ||
| 1150 | } | ||
| 1151 | |||
| 1152 | iref = btrfs_item_ptr(l, slot, struct btrfs_inode_ref); | ||
| 1153 | len = btrfs_inode_ref_name_len(l, iref); | ||
| 1154 | ptr -= len + 1; | ||
| 1155 | total_len += len + 1; | ||
| 1156 | if (ptr < name) | ||
| 1157 | goto out; | ||
| 1158 | |||
| 1159 | *(ptr + len) = '/'; | ||
| 1160 | read_extent_buffer(l, ptr,(unsigned long)(iref + 1), len); | ||
| 1161 | |||
| 1162 | if (key.offset == BTRFS_FIRST_FREE_OBJECTID) | ||
| 1163 | break; | ||
| 1164 | |||
| 1165 | btrfs_release_path(root, path); | ||
| 1166 | key.objectid = key.offset; | ||
| 1167 | key.offset = (u64)-1; | ||
| 1168 | dirid = key.objectid; | ||
| 1169 | |||
| 1170 | } | ||
| 1171 | if (ptr < name) | ||
| 1172 | goto out; | ||
| 1173 | memcpy(name, ptr, total_len); | ||
| 1174 | name[total_len]='\0'; | ||
| 1175 | ret = 0; | ||
| 1176 | out: | ||
| 1177 | btrfs_free_path(path); | ||
| 1178 | return ret; | ||
| 1179 | } | ||
| 1180 | |||
| 1181 | static noinline int btrfs_ioctl_ino_lookup(struct file *file, | ||
| 1182 | void __user *argp) | ||
| 1183 | { | ||
| 1184 | struct btrfs_ioctl_ino_lookup_args *args; | ||
| 1185 | struct inode *inode; | ||
| 1186 | int ret; | ||
| 1187 | |||
| 1188 | if (!capable(CAP_SYS_ADMIN)) | ||
| 1189 | return -EPERM; | ||
| 1190 | |||
| 1191 | args = kmalloc(sizeof(*args), GFP_KERNEL); | ||
| 1192 | if (!args) | ||
| 1193 | return -ENOMEM; | ||
| 1194 | |||
| 1195 | if (copy_from_user(args, argp, sizeof(*args))) { | ||
| 1196 | kfree(args); | ||
| 1197 | return -EFAULT; | ||
| 1198 | } | ||
| 1199 | inode = fdentry(file)->d_inode; | ||
| 1200 | |||
| 1201 | if (args->treeid == 0) | ||
| 1202 | args->treeid = BTRFS_I(inode)->root->root_key.objectid; | ||
| 1203 | |||
| 1204 | ret = btrfs_search_path_in_tree(BTRFS_I(inode)->root->fs_info, | ||
| 1205 | args->treeid, args->objectid, | ||
| 1206 | args->name); | ||
| 1207 | |||
| 1208 | if (ret == 0 && copy_to_user(argp, args, sizeof(*args))) | ||
| 1209 | ret = -EFAULT; | ||
| 1210 | |||
| 1211 | kfree(args); | ||
| 1212 | return ret; | ||
| 1213 | } | ||
| 1214 | |||
| 746 | static noinline int btrfs_ioctl_snap_destroy(struct file *file, | 1215 | static noinline int btrfs_ioctl_snap_destroy(struct file *file, |
| 747 | void __user *arg) | 1216 | void __user *arg) |
| 748 | { | 1217 | { |
| @@ -808,7 +1277,13 @@ static noinline int btrfs_ioctl_snap_destroy(struct file *file, | |||
| 808 | if (err) | 1277 | if (err) |
| 809 | goto out_up_write; | 1278 | goto out_up_write; |
| 810 | 1279 | ||
| 811 | trans = btrfs_start_transaction(root, 1); | 1280 | trans = btrfs_start_transaction(root, 0); |
| 1281 | if (IS_ERR(trans)) { | ||
| 1282 | err = PTR_ERR(trans); | ||
| 1283 | goto out_up_write; | ||
| 1284 | } | ||
| 1285 | trans->block_rsv = &root->fs_info->global_block_rsv; | ||
| 1286 | |||
| 812 | ret = btrfs_unlink_subvol(trans, root, dir, | 1287 | ret = btrfs_unlink_subvol(trans, root, dir, |
| 813 | dest->root_key.objectid, | 1288 | dest->root_key.objectid, |
| 814 | dentry->d_name.name, | 1289 | dentry->d_name.name, |
| @@ -822,10 +1297,12 @@ static noinline int btrfs_ioctl_snap_destroy(struct file *file, | |||
| 822 | dest->root_item.drop_level = 0; | 1297 | dest->root_item.drop_level = 0; |
| 823 | btrfs_set_root_refs(&dest->root_item, 0); | 1298 | btrfs_set_root_refs(&dest->root_item, 0); |
| 824 | 1299 | ||
| 825 | ret = btrfs_insert_orphan_item(trans, | 1300 | if (!xchg(&dest->orphan_item_inserted, 1)) { |
| 826 | root->fs_info->tree_root, | 1301 | ret = btrfs_insert_orphan_item(trans, |
| 827 | dest->root_key.objectid); | 1302 | root->fs_info->tree_root, |
| 828 | BUG_ON(ret); | 1303 | dest->root_key.objectid); |
| 1304 | BUG_ON(ret); | ||
| 1305 | } | ||
| 829 | 1306 | ||
| 830 | ret = btrfs_commit_transaction(trans, root); | 1307 | ret = btrfs_commit_transaction(trans, root); |
| 831 | BUG_ON(ret); | 1308 | BUG_ON(ret); |
| @@ -849,10 +1326,11 @@ out: | |||
| 849 | return err; | 1326 | return err; |
| 850 | } | 1327 | } |
| 851 | 1328 | ||
| 852 | static int btrfs_ioctl_defrag(struct file *file) | 1329 | static int btrfs_ioctl_defrag(struct file *file, void __user *argp) |
| 853 | { | 1330 | { |
| 854 | struct inode *inode = fdentry(file)->d_inode; | 1331 | struct inode *inode = fdentry(file)->d_inode; |
| 855 | struct btrfs_root *root = BTRFS_I(inode)->root; | 1332 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 1333 | struct btrfs_ioctl_defrag_range_args *range; | ||
| 856 | int ret; | 1334 | int ret; |
| 857 | 1335 | ||
| 858 | ret = mnt_want_write(file->f_path.mnt); | 1336 | ret = mnt_want_write(file->f_path.mnt); |
| @@ -865,16 +1343,44 @@ static int btrfs_ioctl_defrag(struct file *file) | |||
| 865 | ret = -EPERM; | 1343 | ret = -EPERM; |
| 866 | goto out; | 1344 | goto out; |
| 867 | } | 1345 | } |
| 868 | btrfs_defrag_root(root, 0); | 1346 | ret = btrfs_defrag_root(root, 0); |
| 869 | btrfs_defrag_root(root->fs_info->extent_root, 0); | 1347 | if (ret) |
| 1348 | goto out; | ||
| 1349 | ret = btrfs_defrag_root(root->fs_info->extent_root, 0); | ||
| 870 | break; | 1350 | break; |
| 871 | case S_IFREG: | 1351 | case S_IFREG: |
| 872 | if (!(file->f_mode & FMODE_WRITE)) { | 1352 | if (!(file->f_mode & FMODE_WRITE)) { |
| 873 | ret = -EINVAL; | 1353 | ret = -EINVAL; |
| 874 | goto out; | 1354 | goto out; |
| 875 | } | 1355 | } |
| 876 | btrfs_defrag_file(file); | 1356 | |
| 1357 | range = kzalloc(sizeof(*range), GFP_KERNEL); | ||
| 1358 | if (!range) { | ||
| 1359 | ret = -ENOMEM; | ||
| 1360 | goto out; | ||
| 1361 | } | ||
| 1362 | |||
| 1363 | if (argp) { | ||
| 1364 | if (copy_from_user(range, argp, | ||
| 1365 | sizeof(*range))) { | ||
| 1366 | ret = -EFAULT; | ||
| 1367 | kfree(range); | ||
| 1368 | goto out; | ||
| 1369 | } | ||
| 1370 | /* compression requires us to start the IO */ | ||
| 1371 | if ((range->flags & BTRFS_DEFRAG_RANGE_COMPRESS)) { | ||
| 1372 | range->flags |= BTRFS_DEFRAG_RANGE_START_IO; | ||
| 1373 | range->extent_thresh = (u32)-1; | ||
| 1374 | } | ||
| 1375 | } else { | ||
| 1376 | /* the rest are all set to zero by kzalloc */ | ||
| 1377 | range->len = (u64)-1; | ||
| 1378 | } | ||
| 1379 | ret = btrfs_defrag_file(file, range); | ||
| 1380 | kfree(range); | ||
| 877 | break; | 1381 | break; |
| 1382 | default: | ||
| 1383 | ret = -EINVAL; | ||
| 878 | } | 1384 | } |
| 879 | out: | 1385 | out: |
| 880 | mnt_drop_write(file->f_path.mnt); | 1386 | mnt_drop_write(file->f_path.mnt); |
| @@ -952,7 +1458,7 @@ static noinline long btrfs_ioctl_clone(struct file *file, unsigned long srcfd, | |||
| 952 | */ | 1458 | */ |
| 953 | 1459 | ||
| 954 | /* the destination must be opened for writing */ | 1460 | /* the destination must be opened for writing */ |
| 955 | if (!(file->f_mode & FMODE_WRITE)) | 1461 | if (!(file->f_mode & FMODE_WRITE) || (file->f_flags & O_APPEND)) |
| 956 | return -EINVAL; | 1462 | return -EINVAL; |
| 957 | 1463 | ||
| 958 | ret = mnt_want_write(file->f_path.mnt); | 1464 | ret = mnt_want_write(file->f_path.mnt); |
| @@ -964,12 +1470,17 @@ static noinline long btrfs_ioctl_clone(struct file *file, unsigned long srcfd, | |||
| 964 | ret = -EBADF; | 1470 | ret = -EBADF; |
| 965 | goto out_drop_write; | 1471 | goto out_drop_write; |
| 966 | } | 1472 | } |
| 1473 | |||
| 967 | src = src_file->f_dentry->d_inode; | 1474 | src = src_file->f_dentry->d_inode; |
| 968 | 1475 | ||
| 969 | ret = -EINVAL; | 1476 | ret = -EINVAL; |
| 970 | if (src == inode) | 1477 | if (src == inode) |
| 971 | goto out_fput; | 1478 | goto out_fput; |
| 972 | 1479 | ||
| 1480 | /* the src must be open for reading */ | ||
| 1481 | if (!(src_file->f_mode & FMODE_READ)) | ||
| 1482 | goto out_fput; | ||
| 1483 | |||
| 973 | ret = -EISDIR; | 1484 | ret = -EISDIR; |
| 974 | if (S_ISDIR(src->i_mode) || S_ISDIR(inode->i_mode)) | 1485 | if (S_ISDIR(src->i_mode) || S_ISDIR(inode->i_mode)) |
| 975 | goto out_fput; | 1486 | goto out_fput; |
| @@ -1000,7 +1511,7 @@ static noinline long btrfs_ioctl_clone(struct file *file, unsigned long srcfd, | |||
| 1000 | 1511 | ||
| 1001 | /* determine range to clone */ | 1512 | /* determine range to clone */ |
| 1002 | ret = -EINVAL; | 1513 | ret = -EINVAL; |
| 1003 | if (off >= src->i_size || off + len > src->i_size) | 1514 | if (off + len > src->i_size || off + len < off) |
| 1004 | goto out_unlock; | 1515 | goto out_unlock; |
| 1005 | if (len == 0) | 1516 | if (len == 0) |
| 1006 | olen = len = src->i_size - off; | 1517 | olen = len = src->i_size - off; |
| @@ -1028,12 +1539,6 @@ static noinline long btrfs_ioctl_clone(struct file *file, unsigned long srcfd, | |||
| 1028 | btrfs_wait_ordered_range(src, off, off+len); | 1539 | btrfs_wait_ordered_range(src, off, off+len); |
| 1029 | } | 1540 | } |
| 1030 | 1541 | ||
| 1031 | trans = btrfs_start_transaction(root, 1); | ||
| 1032 | BUG_ON(!trans); | ||
| 1033 | |||
| 1034 | /* punch hole in destination first */ | ||
| 1035 | btrfs_drop_extents(trans, inode, off, off + len, &hint_byte, 1); | ||
| 1036 | |||
| 1037 | /* clone data */ | 1542 | /* clone data */ |
| 1038 | key.objectid = src->i_ino; | 1543 | key.objectid = src->i_ino; |
| 1039 | key.type = BTRFS_EXTENT_DATA_KEY; | 1544 | key.type = BTRFS_EXTENT_DATA_KEY; |
| @@ -1044,7 +1549,7 @@ static noinline long btrfs_ioctl_clone(struct file *file, unsigned long srcfd, | |||
| 1044 | * note the key will change type as we walk through the | 1549 | * note the key will change type as we walk through the |
| 1045 | * tree. | 1550 | * tree. |
| 1046 | */ | 1551 | */ |
| 1047 | ret = btrfs_search_slot(trans, root, &key, path, 0, 0); | 1552 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); |
| 1048 | if (ret < 0) | 1553 | if (ret < 0) |
| 1049 | goto out; | 1554 | goto out; |
| 1050 | 1555 | ||
| @@ -1073,6 +1578,7 @@ static noinline long btrfs_ioctl_clone(struct file *file, unsigned long srcfd, | |||
| 1073 | u64 disko = 0, diskl = 0; | 1578 | u64 disko = 0, diskl = 0; |
| 1074 | u64 datao = 0, datal = 0; | 1579 | u64 datao = 0, datal = 0; |
| 1075 | u8 comp; | 1580 | u8 comp; |
| 1581 | u64 endoff; | ||
| 1076 | 1582 | ||
| 1077 | size = btrfs_item_size_nr(leaf, slot); | 1583 | size = btrfs_item_size_nr(leaf, slot); |
| 1078 | read_extent_buffer(leaf, buf, | 1584 | read_extent_buffer(leaf, buf, |
| @@ -1107,12 +1613,31 @@ static noinline long btrfs_ioctl_clone(struct file *file, unsigned long srcfd, | |||
| 1107 | new_key.objectid = inode->i_ino; | 1613 | new_key.objectid = inode->i_ino; |
| 1108 | new_key.offset = key.offset + destoff - off; | 1614 | new_key.offset = key.offset + destoff - off; |
| 1109 | 1615 | ||
| 1616 | trans = btrfs_start_transaction(root, 1); | ||
| 1617 | if (IS_ERR(trans)) { | ||
| 1618 | ret = PTR_ERR(trans); | ||
| 1619 | goto out; | ||
| 1620 | } | ||
| 1621 | |||
| 1110 | if (type == BTRFS_FILE_EXTENT_REG || | 1622 | if (type == BTRFS_FILE_EXTENT_REG || |
| 1111 | type == BTRFS_FILE_EXTENT_PREALLOC) { | 1623 | type == BTRFS_FILE_EXTENT_PREALLOC) { |
| 1624 | if (off > key.offset) { | ||
| 1625 | datao += off - key.offset; | ||
| 1626 | datal -= off - key.offset; | ||
| 1627 | } | ||
| 1628 | |||
| 1629 | if (key.offset + datal > off + len) | ||
| 1630 | datal = off + len - key.offset; | ||
| 1631 | |||
| 1632 | ret = btrfs_drop_extents(trans, inode, | ||
| 1633 | new_key.offset, | ||
| 1634 | new_key.offset + datal, | ||
| 1635 | &hint_byte, 1); | ||
| 1636 | BUG_ON(ret); | ||
| 1637 | |||
| 1112 | ret = btrfs_insert_empty_item(trans, root, path, | 1638 | ret = btrfs_insert_empty_item(trans, root, path, |
| 1113 | &new_key, size); | 1639 | &new_key, size); |
| 1114 | if (ret) | 1640 | BUG_ON(ret); |
| 1115 | goto out; | ||
| 1116 | 1641 | ||
| 1117 | leaf = path->nodes[0]; | 1642 | leaf = path->nodes[0]; |
| 1118 | slot = path->slots[0]; | 1643 | slot = path->slots[0]; |
| @@ -1123,14 +1648,6 @@ static noinline long btrfs_ioctl_clone(struct file *file, unsigned long srcfd, | |||
| 1123 | extent = btrfs_item_ptr(leaf, slot, | 1648 | extent = btrfs_item_ptr(leaf, slot, |
| 1124 | struct btrfs_file_extent_item); | 1649 | struct btrfs_file_extent_item); |
| 1125 | 1650 | ||
| 1126 | if (off > key.offset) { | ||
| 1127 | datao += off - key.offset; | ||
| 1128 | datal -= off - key.offset; | ||
| 1129 | } | ||
| 1130 | |||
| 1131 | if (key.offset + datal > off + len) | ||
| 1132 | datal = off + len - key.offset; | ||
| 1133 | |||
| 1134 | /* disko == 0 means it's a hole */ | 1651 | /* disko == 0 means it's a hole */ |
| 1135 | if (!disko) | 1652 | if (!disko) |
| 1136 | datao = 0; | 1653 | datao = 0; |
| @@ -1161,14 +1678,21 @@ static noinline long btrfs_ioctl_clone(struct file *file, unsigned long srcfd, | |||
| 1161 | 1678 | ||
| 1162 | if (comp && (skip || trim)) { | 1679 | if (comp && (skip || trim)) { |
| 1163 | ret = -EINVAL; | 1680 | ret = -EINVAL; |
| 1681 | btrfs_end_transaction(trans, root); | ||
| 1164 | goto out; | 1682 | goto out; |
| 1165 | } | 1683 | } |
| 1166 | size -= skip + trim; | 1684 | size -= skip + trim; |
| 1167 | datal -= skip + trim; | 1685 | datal -= skip + trim; |
| 1686 | |||
| 1687 | ret = btrfs_drop_extents(trans, inode, | ||
| 1688 | new_key.offset, | ||
| 1689 | new_key.offset + datal, | ||
| 1690 | &hint_byte, 1); | ||
| 1691 | BUG_ON(ret); | ||
| 1692 | |||
| 1168 | ret = btrfs_insert_empty_item(trans, root, path, | 1693 | ret = btrfs_insert_empty_item(trans, root, path, |
| 1169 | &new_key, size); | 1694 | &new_key, size); |
| 1170 | if (ret) | 1695 | BUG_ON(ret); |
| 1171 | goto out; | ||
| 1172 | 1696 | ||
| 1173 | if (skip) { | 1697 | if (skip) { |
| 1174 | u32 start = | 1698 | u32 start = |
| @@ -1186,8 +1710,26 @@ static noinline long btrfs_ioctl_clone(struct file *file, unsigned long srcfd, | |||
| 1186 | } | 1710 | } |
| 1187 | 1711 | ||
| 1188 | btrfs_mark_buffer_dirty(leaf); | 1712 | btrfs_mark_buffer_dirty(leaf); |
| 1189 | } | 1713 | btrfs_release_path(root, path); |
| 1190 | 1714 | ||
| 1715 | inode->i_mtime = inode->i_ctime = CURRENT_TIME; | ||
| 1716 | |||
| 1717 | /* | ||
| 1718 | * we round up to the block size at eof when | ||
| 1719 | * determining which extents to clone above, | ||
| 1720 | * but shouldn't round up the file size | ||
| 1721 | */ | ||
| 1722 | endoff = new_key.offset + datal; | ||
| 1723 | if (endoff > off+olen) | ||
| 1724 | endoff = off+olen; | ||
| 1725 | if (endoff > inode->i_size) | ||
| 1726 | btrfs_i_size_write(inode, endoff); | ||
| 1727 | |||
| 1728 | BTRFS_I(inode)->flags = BTRFS_I(src)->flags; | ||
| 1729 | ret = btrfs_update_inode(trans, root, inode); | ||
| 1730 | BUG_ON(ret); | ||
| 1731 | btrfs_end_transaction(trans, root); | ||
| 1732 | } | ||
| 1191 | next: | 1733 | next: |
| 1192 | btrfs_release_path(root, path); | 1734 | btrfs_release_path(root, path); |
| 1193 | key.offset++; | 1735 | key.offset++; |
| @@ -1195,17 +1737,7 @@ next: | |||
| 1195 | ret = 0; | 1737 | ret = 0; |
| 1196 | out: | 1738 | out: |
| 1197 | btrfs_release_path(root, path); | 1739 | btrfs_release_path(root, path); |
| 1198 | if (ret == 0) { | ||
| 1199 | inode->i_mtime = inode->i_ctime = CURRENT_TIME; | ||
| 1200 | if (destoff + olen > inode->i_size) | ||
| 1201 | btrfs_i_size_write(inode, destoff + olen); | ||
| 1202 | BTRFS_I(inode)->flags = BTRFS_I(src)->flags; | ||
| 1203 | ret = btrfs_update_inode(trans, root, inode); | ||
| 1204 | } | ||
| 1205 | btrfs_end_transaction(trans, root); | ||
| 1206 | unlock_extent(&BTRFS_I(src)->io_tree, off, off+len, GFP_NOFS); | 1740 | unlock_extent(&BTRFS_I(src)->io_tree, off, off+len, GFP_NOFS); |
| 1207 | if (ret) | ||
| 1208 | vmtruncate(inode, 0); | ||
| 1209 | out_unlock: | 1741 | out_unlock: |
| 1210 | mutex_unlock(&src->i_mutex); | 1742 | mutex_unlock(&src->i_mutex); |
| 1211 | mutex_unlock(&inode->i_mutex); | 1743 | mutex_unlock(&inode->i_mutex); |
| @@ -1274,6 +1806,157 @@ out: | |||
| 1274 | return ret; | 1806 | return ret; |
| 1275 | } | 1807 | } |
| 1276 | 1808 | ||
| 1809 | static long btrfs_ioctl_default_subvol(struct file *file, void __user *argp) | ||
| 1810 | { | ||
| 1811 | struct inode *inode = fdentry(file)->d_inode; | ||
| 1812 | struct btrfs_root *root = BTRFS_I(inode)->root; | ||
| 1813 | struct btrfs_root *new_root; | ||
| 1814 | struct btrfs_dir_item *di; | ||
| 1815 | struct btrfs_trans_handle *trans; | ||
| 1816 | struct btrfs_path *path; | ||
| 1817 | struct btrfs_key location; | ||
| 1818 | struct btrfs_disk_key disk_key; | ||
| 1819 | struct btrfs_super_block *disk_super; | ||
| 1820 | u64 features; | ||
| 1821 | u64 objectid = 0; | ||
| 1822 | u64 dir_id; | ||
| 1823 | |||
| 1824 | if (!capable(CAP_SYS_ADMIN)) | ||
| 1825 | return -EPERM; | ||
| 1826 | |||
| 1827 | if (copy_from_user(&objectid, argp, sizeof(objectid))) | ||
| 1828 | return -EFAULT; | ||
| 1829 | |||
| 1830 | if (!objectid) | ||
| 1831 | objectid = root->root_key.objectid; | ||
| 1832 | |||
| 1833 | location.objectid = objectid; | ||
| 1834 | location.type = BTRFS_ROOT_ITEM_KEY; | ||
| 1835 | location.offset = (u64)-1; | ||
| 1836 | |||
| 1837 | new_root = btrfs_read_fs_root_no_name(root->fs_info, &location); | ||
| 1838 | if (IS_ERR(new_root)) | ||
| 1839 | return PTR_ERR(new_root); | ||
| 1840 | |||
| 1841 | if (btrfs_root_refs(&new_root->root_item) == 0) | ||
| 1842 | return -ENOENT; | ||
| 1843 | |||
| 1844 | path = btrfs_alloc_path(); | ||
| 1845 | if (!path) | ||
| 1846 | return -ENOMEM; | ||
| 1847 | path->leave_spinning = 1; | ||
| 1848 | |||
| 1849 | trans = btrfs_start_transaction(root, 1); | ||
| 1850 | if (!trans) { | ||
| 1851 | btrfs_free_path(path); | ||
| 1852 | return -ENOMEM; | ||
| 1853 | } | ||
| 1854 | |||
| 1855 | dir_id = btrfs_super_root_dir(&root->fs_info->super_copy); | ||
| 1856 | di = btrfs_lookup_dir_item(trans, root->fs_info->tree_root, path, | ||
| 1857 | dir_id, "default", 7, 1); | ||
| 1858 | if (IS_ERR_OR_NULL(di)) { | ||
| 1859 | btrfs_free_path(path); | ||
| 1860 | btrfs_end_transaction(trans, root); | ||
| 1861 | printk(KERN_ERR "Umm, you don't have the default dir item, " | ||
| 1862 | "this isn't going to work\n"); | ||
| 1863 | return -ENOENT; | ||
| 1864 | } | ||
| 1865 | |||
| 1866 | btrfs_cpu_key_to_disk(&disk_key, &new_root->root_key); | ||
| 1867 | btrfs_set_dir_item_key(path->nodes[0], di, &disk_key); | ||
| 1868 | btrfs_mark_buffer_dirty(path->nodes[0]); | ||
| 1869 | btrfs_free_path(path); | ||
| 1870 | |||
| 1871 | disk_super = &root->fs_info->super_copy; | ||
| 1872 | features = btrfs_super_incompat_flags(disk_super); | ||
| 1873 | if (!(features & BTRFS_FEATURE_INCOMPAT_DEFAULT_SUBVOL)) { | ||
| 1874 | features |= BTRFS_FEATURE_INCOMPAT_DEFAULT_SUBVOL; | ||
| 1875 | btrfs_set_super_incompat_flags(disk_super, features); | ||
| 1876 | } | ||
| 1877 | btrfs_end_transaction(trans, root); | ||
| 1878 | |||
| 1879 | return 0; | ||
| 1880 | } | ||
| 1881 | |||
| 1882 | long btrfs_ioctl_space_info(struct btrfs_root *root, void __user *arg) | ||
| 1883 | { | ||
| 1884 | struct btrfs_ioctl_space_args space_args; | ||
| 1885 | struct btrfs_ioctl_space_info space; | ||
| 1886 | struct btrfs_ioctl_space_info *dest; | ||
| 1887 | struct btrfs_ioctl_space_info *dest_orig; | ||
| 1888 | struct btrfs_ioctl_space_info *user_dest; | ||
| 1889 | struct btrfs_space_info *info; | ||
| 1890 | int alloc_size; | ||
| 1891 | int ret = 0; | ||
| 1892 | int slot_count = 0; | ||
| 1893 | |||
| 1894 | if (copy_from_user(&space_args, | ||
| 1895 | (struct btrfs_ioctl_space_args __user *)arg, | ||
| 1896 | sizeof(space_args))) | ||
| 1897 | return -EFAULT; | ||
| 1898 | |||
| 1899 | /* first we count slots */ | ||
| 1900 | rcu_read_lock(); | ||
| 1901 | list_for_each_entry_rcu(info, &root->fs_info->space_info, list) | ||
| 1902 | slot_count++; | ||
| 1903 | rcu_read_unlock(); | ||
| 1904 | |||
| 1905 | /* space_slots == 0 means they are asking for a count */ | ||
| 1906 | if (space_args.space_slots == 0) { | ||
| 1907 | space_args.total_spaces = slot_count; | ||
| 1908 | goto out; | ||
| 1909 | } | ||
| 1910 | alloc_size = sizeof(*dest) * slot_count; | ||
| 1911 | /* we generally have at most 6 or so space infos, one for each raid | ||
| 1912 | * level. So, a whole page should be more than enough for everyone | ||
| 1913 | */ | ||
| 1914 | if (alloc_size > PAGE_CACHE_SIZE) | ||
| 1915 | return -ENOMEM; | ||
| 1916 | |||
| 1917 | space_args.total_spaces = 0; | ||
| 1918 | dest = kmalloc(alloc_size, GFP_NOFS); | ||
| 1919 | if (!dest) | ||
| 1920 | return -ENOMEM; | ||
| 1921 | dest_orig = dest; | ||
| 1922 | |||
| 1923 | /* now we have a buffer to copy into */ | ||
| 1924 | rcu_read_lock(); | ||
| 1925 | list_for_each_entry_rcu(info, &root->fs_info->space_info, list) { | ||
| 1926 | /* make sure we don't copy more than we allocated | ||
| 1927 | * in our buffer | ||
| 1928 | */ | ||
| 1929 | if (slot_count == 0) | ||
| 1930 | break; | ||
| 1931 | slot_count--; | ||
| 1932 | |||
| 1933 | /* make sure userland has enough room in their buffer */ | ||
| 1934 | if (space_args.total_spaces >= space_args.space_slots) | ||
| 1935 | break; | ||
| 1936 | |||
| 1937 | space.flags = info->flags; | ||
| 1938 | space.total_bytes = info->total_bytes; | ||
| 1939 | space.used_bytes = info->bytes_used; | ||
| 1940 | memcpy(dest, &space, sizeof(space)); | ||
| 1941 | dest++; | ||
| 1942 | space_args.total_spaces++; | ||
| 1943 | } | ||
| 1944 | rcu_read_unlock(); | ||
| 1945 | |||
| 1946 | user_dest = (struct btrfs_ioctl_space_info *) | ||
| 1947 | (arg + sizeof(struct btrfs_ioctl_space_args)); | ||
| 1948 | |||
| 1949 | if (copy_to_user(user_dest, dest_orig, alloc_size)) | ||
| 1950 | ret = -EFAULT; | ||
| 1951 | |||
| 1952 | kfree(dest_orig); | ||
| 1953 | out: | ||
| 1954 | if (ret == 0 && copy_to_user(arg, &space_args, sizeof(space_args))) | ||
| 1955 | ret = -EFAULT; | ||
| 1956 | |||
| 1957 | return ret; | ||
| 1958 | } | ||
| 1959 | |||
| 1277 | /* | 1960 | /* |
| 1278 | * there are many ways the trans_start and trans_end ioctls can lead | 1961 | * there are many ways the trans_start and trans_end ioctls can lead |
| 1279 | * to deadlocks. They should only be used by applications that | 1962 | * to deadlocks. They should only be used by applications that |
| @@ -1320,8 +2003,12 @@ long btrfs_ioctl(struct file *file, unsigned int | |||
| 1320 | return btrfs_ioctl_snap_create(file, argp, 1); | 2003 | return btrfs_ioctl_snap_create(file, argp, 1); |
| 1321 | case BTRFS_IOC_SNAP_DESTROY: | 2004 | case BTRFS_IOC_SNAP_DESTROY: |
| 1322 | return btrfs_ioctl_snap_destroy(file, argp); | 2005 | return btrfs_ioctl_snap_destroy(file, argp); |
| 2006 | case BTRFS_IOC_DEFAULT_SUBVOL: | ||
| 2007 | return btrfs_ioctl_default_subvol(file, argp); | ||
| 1323 | case BTRFS_IOC_DEFRAG: | 2008 | case BTRFS_IOC_DEFRAG: |
| 1324 | return btrfs_ioctl_defrag(file); | 2009 | return btrfs_ioctl_defrag(file, NULL); |
| 2010 | case BTRFS_IOC_DEFRAG_RANGE: | ||
| 2011 | return btrfs_ioctl_defrag(file, argp); | ||
| 1325 | case BTRFS_IOC_RESIZE: | 2012 | case BTRFS_IOC_RESIZE: |
| 1326 | return btrfs_ioctl_resize(root, argp); | 2013 | return btrfs_ioctl_resize(root, argp); |
| 1327 | case BTRFS_IOC_ADD_DEV: | 2014 | case BTRFS_IOC_ADD_DEV: |
| @@ -1338,6 +2025,12 @@ long btrfs_ioctl(struct file *file, unsigned int | |||
| 1338 | return btrfs_ioctl_trans_start(file); | 2025 | return btrfs_ioctl_trans_start(file); |
| 1339 | case BTRFS_IOC_TRANS_END: | 2026 | case BTRFS_IOC_TRANS_END: |
| 1340 | return btrfs_ioctl_trans_end(file); | 2027 | return btrfs_ioctl_trans_end(file); |
| 2028 | case BTRFS_IOC_TREE_SEARCH: | ||
| 2029 | return btrfs_ioctl_tree_search(file, argp); | ||
| 2030 | case BTRFS_IOC_INO_LOOKUP: | ||
| 2031 | return btrfs_ioctl_ino_lookup(file, argp); | ||
| 2032 | case BTRFS_IOC_SPACE_INFO: | ||
| 2033 | return btrfs_ioctl_space_info(root, argp); | ||
| 1341 | case BTRFS_IOC_SYNC: | 2034 | case BTRFS_IOC_SYNC: |
| 1342 | btrfs_sync_fs(file->f_dentry->d_sb, 1); | 2035 | btrfs_sync_fs(file->f_dentry->d_sb, 1); |
| 1343 | return 0; | 2036 | return 0; |
