diff options
Diffstat (limited to 'fs/btrfs/extent-tree.c')
-rw-r--r-- | fs/btrfs/extent-tree.c | 2610 |
1 files changed, 1758 insertions, 852 deletions
diff --git a/fs/btrfs/extent-tree.c b/fs/btrfs/extent-tree.c index 35af93355063..edc7d208c5ce 100644 --- a/fs/btrfs/extent-tree.c +++ b/fs/btrfs/extent-tree.c | |||
@@ -23,50 +23,39 @@ | |||
23 | #include <linux/rcupdate.h> | 23 | #include <linux/rcupdate.h> |
24 | #include "compat.h" | 24 | #include "compat.h" |
25 | #include "hash.h" | 25 | #include "hash.h" |
26 | #include "crc32c.h" | ||
27 | #include "ctree.h" | 26 | #include "ctree.h" |
28 | #include "disk-io.h" | 27 | #include "disk-io.h" |
29 | #include "print-tree.h" | 28 | #include "print-tree.h" |
30 | #include "transaction.h" | 29 | #include "transaction.h" |
31 | #include "volumes.h" | 30 | #include "volumes.h" |
32 | #include "locking.h" | 31 | #include "locking.h" |
33 | #include "ref-cache.h" | ||
34 | #include "free-space-cache.h" | 32 | #include "free-space-cache.h" |
35 | 33 | ||
36 | #define PENDING_EXTENT_INSERT 0 | ||
37 | #define PENDING_EXTENT_DELETE 1 | ||
38 | #define PENDING_BACKREF_UPDATE 2 | ||
39 | |||
40 | struct pending_extent_op { | ||
41 | int type; | ||
42 | u64 bytenr; | ||
43 | u64 num_bytes; | ||
44 | u64 parent; | ||
45 | u64 orig_parent; | ||
46 | u64 generation; | ||
47 | u64 orig_generation; | ||
48 | int level; | ||
49 | struct list_head list; | ||
50 | int del; | ||
51 | }; | ||
52 | |||
53 | static int __btrfs_alloc_reserved_extent(struct btrfs_trans_handle *trans, | ||
54 | struct btrfs_root *root, u64 parent, | ||
55 | u64 root_objectid, u64 ref_generation, | ||
56 | u64 owner, struct btrfs_key *ins, | ||
57 | int ref_mod); | ||
58 | static int update_reserved_extents(struct btrfs_root *root, | 34 | static int update_reserved_extents(struct btrfs_root *root, |
59 | u64 bytenr, u64 num, int reserve); | 35 | u64 bytenr, u64 num, int reserve); |
60 | static int update_block_group(struct btrfs_trans_handle *trans, | 36 | static int update_block_group(struct btrfs_trans_handle *trans, |
61 | struct btrfs_root *root, | 37 | struct btrfs_root *root, |
62 | u64 bytenr, u64 num_bytes, int alloc, | 38 | u64 bytenr, u64 num_bytes, int alloc, |
63 | int mark_free); | 39 | int mark_free); |
64 | static noinline int __btrfs_free_extent(struct btrfs_trans_handle *trans, | 40 | static int __btrfs_free_extent(struct btrfs_trans_handle *trans, |
65 | struct btrfs_root *root, | 41 | struct btrfs_root *root, |
66 | u64 bytenr, u64 num_bytes, u64 parent, | 42 | u64 bytenr, u64 num_bytes, u64 parent, |
67 | u64 root_objectid, u64 ref_generation, | 43 | u64 root_objectid, u64 owner_objectid, |
68 | u64 owner_objectid, int pin, | 44 | u64 owner_offset, int refs_to_drop, |
69 | int ref_to_drop); | 45 | struct btrfs_delayed_extent_op *extra_op); |
46 | static void __run_delayed_extent_op(struct btrfs_delayed_extent_op *extent_op, | ||
47 | struct extent_buffer *leaf, | ||
48 | struct btrfs_extent_item *ei); | ||
49 | static int alloc_reserved_file_extent(struct btrfs_trans_handle *trans, | ||
50 | struct btrfs_root *root, | ||
51 | u64 parent, u64 root_objectid, | ||
52 | u64 flags, u64 owner, u64 offset, | ||
53 | struct btrfs_key *ins, int ref_mod); | ||
54 | static int alloc_reserved_tree_block(struct btrfs_trans_handle *trans, | ||
55 | struct btrfs_root *root, | ||
56 | u64 parent, u64 root_objectid, | ||
57 | u64 flags, struct btrfs_disk_key *key, | ||
58 | int level, struct btrfs_key *ins); | ||
70 | 59 | ||
71 | static int do_chunk_alloc(struct btrfs_trans_handle *trans, | 60 | static int do_chunk_alloc(struct btrfs_trans_handle *trans, |
72 | struct btrfs_root *extent_root, u64 alloc_bytes, | 61 | struct btrfs_root *extent_root, u64 alloc_bytes, |
@@ -453,199 +442,969 @@ int btrfs_lookup_extent(struct btrfs_root *root, u64 start, u64 len) | |||
453 | * maintenance. This is actually the same as #2, but with a slightly | 442 | * maintenance. This is actually the same as #2, but with a slightly |
454 | * different use case. | 443 | * different use case. |
455 | * | 444 | * |
445 | * There are two kinds of back refs. The implicit back refs is optimized | ||
446 | * for pointers in non-shared tree blocks. For a given pointer in a block, | ||
447 | * back refs of this kind provide information about the block's owner tree | ||
448 | * and the pointer's key. These information allow us to find the block by | ||
449 | * b-tree searching. The full back refs is for pointers in tree blocks not | ||
450 | * referenced by their owner trees. The location of tree block is recorded | ||
451 | * in the back refs. Actually the full back refs is generic, and can be | ||
452 | * used in all cases the implicit back refs is used. The major shortcoming | ||
453 | * of the full back refs is its overhead. Every time a tree block gets | ||
454 | * COWed, we have to update back refs entry for all pointers in it. | ||
455 | * | ||
456 | * For a newly allocated tree block, we use implicit back refs for | ||
457 | * pointers in it. This means most tree related operations only involve | ||
458 | * implicit back refs. For a tree block created in old transaction, the | ||
459 | * only way to drop a reference to it is COW it. So we can detect the | ||
460 | * event that tree block loses its owner tree's reference and do the | ||
461 | * back refs conversion. | ||
462 | * | ||
463 | * When a tree block is COW'd through a tree, there are four cases: | ||
464 | * | ||
465 | * The reference count of the block is one and the tree is the block's | ||
466 | * owner tree. Nothing to do in this case. | ||
467 | * | ||
468 | * The reference count of the block is one and the tree is not the | ||
469 | * block's owner tree. In this case, full back refs is used for pointers | ||
470 | * in the block. Remove these full back refs, add implicit back refs for | ||
471 | * every pointers in the new block. | ||
472 | * | ||
473 | * The reference count of the block is greater than one and the tree is | ||
474 | * the block's owner tree. In this case, implicit back refs is used for | ||
475 | * pointers in the block. Add full back refs for every pointers in the | ||
476 | * block, increase lower level extents' reference counts. The original | ||
477 | * implicit back refs are entailed to the new block. | ||
478 | * | ||
479 | * The reference count of the block is greater than one and the tree is | ||
480 | * not the block's owner tree. Add implicit back refs for every pointer in | ||
481 | * the new block, increase lower level extents' reference count. | ||
482 | * | ||
483 | * Back Reference Key composing: | ||
484 | * | ||
485 | * The key objectid corresponds to the first byte in the extent, | ||
486 | * The key type is used to differentiate between types of back refs. | ||
487 | * There are different meanings of the key offset for different types | ||
488 | * of back refs. | ||
489 | * | ||
456 | * File extents can be referenced by: | 490 | * File extents can be referenced by: |
457 | * | 491 | * |
458 | * - multiple snapshots, subvolumes, or different generations in one subvol | 492 | * - multiple snapshots, subvolumes, or different generations in one subvol |
459 | * - different files inside a single subvolume | 493 | * - different files inside a single subvolume |
460 | * - different offsets inside a file (bookend extents in file.c) | 494 | * - different offsets inside a file (bookend extents in file.c) |
461 | * | 495 | * |
462 | * The extent ref structure has fields for: | 496 | * The extent ref structure for the implicit back refs has fields for: |
463 | * | 497 | * |
464 | * - Objectid of the subvolume root | 498 | * - Objectid of the subvolume root |
465 | * - Generation number of the tree holding the reference | ||
466 | * - objectid of the file holding the reference | 499 | * - objectid of the file holding the reference |
467 | * - number of references holding by parent node (alway 1 for tree blocks) | 500 | * - original offset in the file |
468 | * | 501 | * - how many bookend extents |
469 | * Btree leaf may hold multiple references to a file extent. In most cases, | ||
470 | * these references are from same file and the corresponding offsets inside | ||
471 | * the file are close together. | ||
472 | * | ||
473 | * When a file extent is allocated the fields are filled in: | ||
474 | * (root_key.objectid, trans->transid, inode objectid, 1) | ||
475 | * | 502 | * |
476 | * When a leaf is cow'd new references are added for every file extent found | 503 | * The key offset for the implicit back refs is hash of the first |
477 | * in the leaf. It looks similar to the create case, but trans->transid will | 504 | * three fields. |
478 | * be different when the block is cow'd. | ||
479 | * | 505 | * |
480 | * (root_key.objectid, trans->transid, inode objectid, | 506 | * The extent ref structure for the full back refs has field for: |
481 | * number of references in the leaf) | ||
482 | * | 507 | * |
483 | * When a file extent is removed either during snapshot deletion or | 508 | * - number of pointers in the tree leaf |
484 | * file truncation, we find the corresponding back reference and check | ||
485 | * the following fields: | ||
486 | * | 509 | * |
487 | * (btrfs_header_owner(leaf), btrfs_header_generation(leaf), | 510 | * The key offset for the implicit back refs is the first byte of |
488 | * inode objectid) | 511 | * the tree leaf |
489 | * | 512 | * |
490 | * Btree extents can be referenced by: | 513 | * When a file extent is allocated, The implicit back refs is used. |
491 | * | 514 | * the fields are filled in: |
492 | * - Different subvolumes | ||
493 | * - Different generations of the same subvolume | ||
494 | * | ||
495 | * When a tree block is created, back references are inserted: | ||
496 | * | 515 | * |
497 | * (root->root_key.objectid, trans->transid, level, 1) | 516 | * (root_key.objectid, inode objectid, offset in file, 1) |
498 | * | 517 | * |
499 | * When a tree block is cow'd, new back references are added for all the | 518 | * When a file extent is removed file truncation, we find the |
500 | * blocks it points to. If the tree block isn't in reference counted root, | 519 | * corresponding implicit back refs and check the following fields: |
501 | * the old back references are removed. These new back references are of | ||
502 | * the form (trans->transid will have increased since creation): | ||
503 | * | 520 | * |
504 | * (root->root_key.objectid, trans->transid, level, 1) | 521 | * (btrfs_header_owner(leaf), inode objectid, offset in file) |
505 | * | 522 | * |
506 | * When a backref is in deleting, the following fields are checked: | 523 | * Btree extents can be referenced by: |
507 | * | 524 | * |
508 | * if backref was for a tree root: | 525 | * - Different subvolumes |
509 | * (btrfs_header_owner(itself), btrfs_header_generation(itself), level) | ||
510 | * else | ||
511 | * (btrfs_header_owner(parent), btrfs_header_generation(parent), level) | ||
512 | * | 526 | * |
513 | * Back Reference Key composing: | 527 | * Both the implicit back refs and the full back refs for tree blocks |
528 | * only consist of key. The key offset for the implicit back refs is | ||
529 | * objectid of block's owner tree. The key offset for the full back refs | ||
530 | * is the first byte of parent block. | ||
514 | * | 531 | * |
515 | * The key objectid corresponds to the first byte in the extent, the key | 532 | * When implicit back refs is used, information about the lowest key and |
516 | * type is set to BTRFS_EXTENT_REF_KEY, and the key offset is the first | 533 | * level of the tree block are required. These information are stored in |
517 | * byte of parent extent. If a extent is tree root, the key offset is set | 534 | * tree block info structure. |
518 | * to the key objectid. | ||
519 | */ | 535 | */ |
520 | 536 | ||
521 | static noinline int lookup_extent_backref(struct btrfs_trans_handle *trans, | 537 | #ifdef BTRFS_COMPAT_EXTENT_TREE_V0 |
522 | struct btrfs_root *root, | 538 | static int convert_extent_item_v0(struct btrfs_trans_handle *trans, |
523 | struct btrfs_path *path, | 539 | struct btrfs_root *root, |
524 | u64 bytenr, u64 parent, | 540 | struct btrfs_path *path, |
525 | u64 ref_root, u64 ref_generation, | 541 | u64 owner, u32 extra_size) |
526 | u64 owner_objectid, int del) | ||
527 | { | 542 | { |
543 | struct btrfs_extent_item *item; | ||
544 | struct btrfs_extent_item_v0 *ei0; | ||
545 | struct btrfs_extent_ref_v0 *ref0; | ||
546 | struct btrfs_tree_block_info *bi; | ||
547 | struct extent_buffer *leaf; | ||
528 | struct btrfs_key key; | 548 | struct btrfs_key key; |
529 | struct btrfs_extent_ref *ref; | 549 | struct btrfs_key found_key; |
550 | u32 new_size = sizeof(*item); | ||
551 | u64 refs; | ||
552 | int ret; | ||
553 | |||
554 | leaf = path->nodes[0]; | ||
555 | BUG_ON(btrfs_item_size_nr(leaf, path->slots[0]) != sizeof(*ei0)); | ||
556 | |||
557 | btrfs_item_key_to_cpu(leaf, &key, path->slots[0]); | ||
558 | ei0 = btrfs_item_ptr(leaf, path->slots[0], | ||
559 | struct btrfs_extent_item_v0); | ||
560 | refs = btrfs_extent_refs_v0(leaf, ei0); | ||
561 | |||
562 | if (owner == (u64)-1) { | ||
563 | while (1) { | ||
564 | if (path->slots[0] >= btrfs_header_nritems(leaf)) { | ||
565 | ret = btrfs_next_leaf(root, path); | ||
566 | if (ret < 0) | ||
567 | return ret; | ||
568 | BUG_ON(ret > 0); | ||
569 | leaf = path->nodes[0]; | ||
570 | } | ||
571 | btrfs_item_key_to_cpu(leaf, &found_key, | ||
572 | path->slots[0]); | ||
573 | BUG_ON(key.objectid != found_key.objectid); | ||
574 | if (found_key.type != BTRFS_EXTENT_REF_V0_KEY) { | ||
575 | path->slots[0]++; | ||
576 | continue; | ||
577 | } | ||
578 | ref0 = btrfs_item_ptr(leaf, path->slots[0], | ||
579 | struct btrfs_extent_ref_v0); | ||
580 | owner = btrfs_ref_objectid_v0(leaf, ref0); | ||
581 | break; | ||
582 | } | ||
583 | } | ||
584 | btrfs_release_path(root, path); | ||
585 | |||
586 | if (owner < BTRFS_FIRST_FREE_OBJECTID) | ||
587 | new_size += sizeof(*bi); | ||
588 | |||
589 | new_size -= sizeof(*ei0); | ||
590 | ret = btrfs_search_slot(trans, root, &key, path, | ||
591 | new_size + extra_size, 1); | ||
592 | if (ret < 0) | ||
593 | return ret; | ||
594 | BUG_ON(ret); | ||
595 | |||
596 | ret = btrfs_extend_item(trans, root, path, new_size); | ||
597 | BUG_ON(ret); | ||
598 | |||
599 | leaf = path->nodes[0]; | ||
600 | item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item); | ||
601 | btrfs_set_extent_refs(leaf, item, refs); | ||
602 | /* FIXME: get real generation */ | ||
603 | btrfs_set_extent_generation(leaf, item, 0); | ||
604 | if (owner < BTRFS_FIRST_FREE_OBJECTID) { | ||
605 | btrfs_set_extent_flags(leaf, item, | ||
606 | BTRFS_EXTENT_FLAG_TREE_BLOCK | | ||
607 | BTRFS_BLOCK_FLAG_FULL_BACKREF); | ||
608 | bi = (struct btrfs_tree_block_info *)(item + 1); | ||
609 | /* FIXME: get first key of the block */ | ||
610 | memset_extent_buffer(leaf, 0, (unsigned long)bi, sizeof(*bi)); | ||
611 | btrfs_set_tree_block_level(leaf, bi, (int)owner); | ||
612 | } else { | ||
613 | btrfs_set_extent_flags(leaf, item, BTRFS_EXTENT_FLAG_DATA); | ||
614 | } | ||
615 | btrfs_mark_buffer_dirty(leaf); | ||
616 | return 0; | ||
617 | } | ||
618 | #endif | ||
619 | |||
620 | static u64 hash_extent_data_ref(u64 root_objectid, u64 owner, u64 offset) | ||
621 | { | ||
622 | u32 high_crc = ~(u32)0; | ||
623 | u32 low_crc = ~(u32)0; | ||
624 | __le64 lenum; | ||
625 | |||
626 | lenum = cpu_to_le64(root_objectid); | ||
627 | high_crc = crc32c(high_crc, &lenum, sizeof(lenum)); | ||
628 | lenum = cpu_to_le64(owner); | ||
629 | low_crc = crc32c(low_crc, &lenum, sizeof(lenum)); | ||
630 | lenum = cpu_to_le64(offset); | ||
631 | low_crc = crc32c(low_crc, &lenum, sizeof(lenum)); | ||
632 | |||
633 | return ((u64)high_crc << 31) ^ (u64)low_crc; | ||
634 | } | ||
635 | |||
636 | static u64 hash_extent_data_ref_item(struct extent_buffer *leaf, | ||
637 | struct btrfs_extent_data_ref *ref) | ||
638 | { | ||
639 | return hash_extent_data_ref(btrfs_extent_data_ref_root(leaf, ref), | ||
640 | btrfs_extent_data_ref_objectid(leaf, ref), | ||
641 | btrfs_extent_data_ref_offset(leaf, ref)); | ||
642 | } | ||
643 | |||
644 | static int match_extent_data_ref(struct extent_buffer *leaf, | ||
645 | struct btrfs_extent_data_ref *ref, | ||
646 | u64 root_objectid, u64 owner, u64 offset) | ||
647 | { | ||
648 | if (btrfs_extent_data_ref_root(leaf, ref) != root_objectid || | ||
649 | btrfs_extent_data_ref_objectid(leaf, ref) != owner || | ||
650 | btrfs_extent_data_ref_offset(leaf, ref) != offset) | ||
651 | return 0; | ||
652 | return 1; | ||
653 | } | ||
654 | |||
655 | static noinline int lookup_extent_data_ref(struct btrfs_trans_handle *trans, | ||
656 | struct btrfs_root *root, | ||
657 | struct btrfs_path *path, | ||
658 | u64 bytenr, u64 parent, | ||
659 | u64 root_objectid, | ||
660 | u64 owner, u64 offset) | ||
661 | { | ||
662 | struct btrfs_key key; | ||
663 | struct btrfs_extent_data_ref *ref; | ||
530 | struct extent_buffer *leaf; | 664 | struct extent_buffer *leaf; |
531 | u64 ref_objectid; | 665 | u32 nritems; |
532 | int ret; | 666 | int ret; |
667 | int recow; | ||
668 | int err = -ENOENT; | ||
533 | 669 | ||
534 | key.objectid = bytenr; | 670 | key.objectid = bytenr; |
535 | key.type = BTRFS_EXTENT_REF_KEY; | 671 | if (parent) { |
536 | key.offset = parent; | 672 | key.type = BTRFS_SHARED_DATA_REF_KEY; |
673 | key.offset = parent; | ||
674 | } else { | ||
675 | key.type = BTRFS_EXTENT_DATA_REF_KEY; | ||
676 | key.offset = hash_extent_data_ref(root_objectid, | ||
677 | owner, offset); | ||
678 | } | ||
679 | again: | ||
680 | recow = 0; | ||
681 | ret = btrfs_search_slot(trans, root, &key, path, -1, 1); | ||
682 | if (ret < 0) { | ||
683 | err = ret; | ||
684 | goto fail; | ||
685 | } | ||
537 | 686 | ||
538 | ret = btrfs_search_slot(trans, root, &key, path, del ? -1 : 0, 1); | 687 | if (parent) { |
539 | if (ret < 0) | 688 | if (!ret) |
540 | goto out; | 689 | return 0; |
541 | if (ret > 0) { | 690 | #ifdef BTRFS_COMPAT_EXTENT_TREE_V0 |
542 | ret = -ENOENT; | 691 | key.type = BTRFS_EXTENT_REF_V0_KEY; |
543 | goto out; | 692 | btrfs_release_path(root, path); |
693 | ret = btrfs_search_slot(trans, root, &key, path, -1, 1); | ||
694 | if (ret < 0) { | ||
695 | err = ret; | ||
696 | goto fail; | ||
697 | } | ||
698 | if (!ret) | ||
699 | return 0; | ||
700 | #endif | ||
701 | goto fail; | ||
544 | } | 702 | } |
545 | 703 | ||
546 | leaf = path->nodes[0]; | 704 | leaf = path->nodes[0]; |
547 | ref = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_ref); | 705 | nritems = btrfs_header_nritems(leaf); |
548 | ref_objectid = btrfs_ref_objectid(leaf, ref); | 706 | while (1) { |
549 | if (btrfs_ref_root(leaf, ref) != ref_root || | 707 | if (path->slots[0] >= nritems) { |
550 | btrfs_ref_generation(leaf, ref) != ref_generation || | 708 | ret = btrfs_next_leaf(root, path); |
551 | (ref_objectid != owner_objectid && | 709 | if (ret < 0) |
552 | ref_objectid != BTRFS_MULTIPLE_OBJECTIDS)) { | 710 | err = ret; |
553 | ret = -EIO; | 711 | if (ret) |
554 | WARN_ON(1); | 712 | goto fail; |
555 | goto out; | 713 | |
714 | leaf = path->nodes[0]; | ||
715 | nritems = btrfs_header_nritems(leaf); | ||
716 | recow = 1; | ||
717 | } | ||
718 | |||
719 | btrfs_item_key_to_cpu(leaf, &key, path->slots[0]); | ||
720 | if (key.objectid != bytenr || | ||
721 | key.type != BTRFS_EXTENT_DATA_REF_KEY) | ||
722 | goto fail; | ||
723 | |||
724 | ref = btrfs_item_ptr(leaf, path->slots[0], | ||
725 | struct btrfs_extent_data_ref); | ||
726 | |||
727 | if (match_extent_data_ref(leaf, ref, root_objectid, | ||
728 | owner, offset)) { | ||
729 | if (recow) { | ||
730 | btrfs_release_path(root, path); | ||
731 | goto again; | ||
732 | } | ||
733 | err = 0; | ||
734 | break; | ||
735 | } | ||
736 | path->slots[0]++; | ||
556 | } | 737 | } |
557 | ret = 0; | 738 | fail: |
558 | out: | 739 | return err; |
559 | return ret; | ||
560 | } | 740 | } |
561 | 741 | ||
562 | static noinline int insert_extent_backref(struct btrfs_trans_handle *trans, | 742 | static noinline int insert_extent_data_ref(struct btrfs_trans_handle *trans, |
563 | struct btrfs_root *root, | 743 | struct btrfs_root *root, |
564 | struct btrfs_path *path, | 744 | struct btrfs_path *path, |
565 | u64 bytenr, u64 parent, | 745 | u64 bytenr, u64 parent, |
566 | u64 ref_root, u64 ref_generation, | 746 | u64 root_objectid, u64 owner, |
567 | u64 owner_objectid, | 747 | u64 offset, int refs_to_add) |
568 | int refs_to_add) | ||
569 | { | 748 | { |
570 | struct btrfs_key key; | 749 | struct btrfs_key key; |
571 | struct extent_buffer *leaf; | 750 | struct extent_buffer *leaf; |
572 | struct btrfs_extent_ref *ref; | 751 | u32 size; |
573 | u32 num_refs; | 752 | u32 num_refs; |
574 | int ret; | 753 | int ret; |
575 | 754 | ||
576 | key.objectid = bytenr; | 755 | key.objectid = bytenr; |
577 | key.type = BTRFS_EXTENT_REF_KEY; | 756 | if (parent) { |
578 | key.offset = parent; | 757 | key.type = BTRFS_SHARED_DATA_REF_KEY; |
758 | key.offset = parent; | ||
759 | size = sizeof(struct btrfs_shared_data_ref); | ||
760 | } else { | ||
761 | key.type = BTRFS_EXTENT_DATA_REF_KEY; | ||
762 | key.offset = hash_extent_data_ref(root_objectid, | ||
763 | owner, offset); | ||
764 | size = sizeof(struct btrfs_extent_data_ref); | ||
765 | } | ||
579 | 766 | ||
580 | ret = btrfs_insert_empty_item(trans, root, path, &key, sizeof(*ref)); | 767 | ret = btrfs_insert_empty_item(trans, root, path, &key, size); |
581 | if (ret == 0) { | 768 | if (ret && ret != -EEXIST) |
582 | leaf = path->nodes[0]; | 769 | goto fail; |
583 | ref = btrfs_item_ptr(leaf, path->slots[0], | 770 | |
584 | struct btrfs_extent_ref); | 771 | leaf = path->nodes[0]; |
585 | btrfs_set_ref_root(leaf, ref, ref_root); | 772 | if (parent) { |
586 | btrfs_set_ref_generation(leaf, ref, ref_generation); | 773 | struct btrfs_shared_data_ref *ref; |
587 | btrfs_set_ref_objectid(leaf, ref, owner_objectid); | ||
588 | btrfs_set_ref_num_refs(leaf, ref, refs_to_add); | ||
589 | } else if (ret == -EEXIST) { | ||
590 | u64 existing_owner; | ||
591 | |||
592 | BUG_ON(owner_objectid < BTRFS_FIRST_FREE_OBJECTID); | ||
593 | leaf = path->nodes[0]; | ||
594 | ref = btrfs_item_ptr(leaf, path->slots[0], | 774 | ref = btrfs_item_ptr(leaf, path->slots[0], |
595 | struct btrfs_extent_ref); | 775 | struct btrfs_shared_data_ref); |
596 | if (btrfs_ref_root(leaf, ref) != ref_root || | 776 | if (ret == 0) { |
597 | btrfs_ref_generation(leaf, ref) != ref_generation) { | 777 | btrfs_set_shared_data_ref_count(leaf, ref, refs_to_add); |
598 | ret = -EIO; | 778 | } else { |
599 | WARN_ON(1); | 779 | num_refs = btrfs_shared_data_ref_count(leaf, ref); |
600 | goto out; | 780 | num_refs += refs_to_add; |
781 | btrfs_set_shared_data_ref_count(leaf, ref, num_refs); | ||
601 | } | 782 | } |
783 | } else { | ||
784 | struct btrfs_extent_data_ref *ref; | ||
785 | while (ret == -EEXIST) { | ||
786 | ref = btrfs_item_ptr(leaf, path->slots[0], | ||
787 | struct btrfs_extent_data_ref); | ||
788 | if (match_extent_data_ref(leaf, ref, root_objectid, | ||
789 | owner, offset)) | ||
790 | break; | ||
791 | btrfs_release_path(root, path); | ||
792 | key.offset++; | ||
793 | ret = btrfs_insert_empty_item(trans, root, path, &key, | ||
794 | size); | ||
795 | if (ret && ret != -EEXIST) | ||
796 | goto fail; | ||
602 | 797 | ||
603 | num_refs = btrfs_ref_num_refs(leaf, ref); | 798 | leaf = path->nodes[0]; |
604 | BUG_ON(num_refs == 0); | 799 | } |
605 | btrfs_set_ref_num_refs(leaf, ref, num_refs + refs_to_add); | 800 | ref = btrfs_item_ptr(leaf, path->slots[0], |
606 | 801 | struct btrfs_extent_data_ref); | |
607 | existing_owner = btrfs_ref_objectid(leaf, ref); | 802 | if (ret == 0) { |
608 | if (existing_owner != owner_objectid && | 803 | btrfs_set_extent_data_ref_root(leaf, ref, |
609 | existing_owner != BTRFS_MULTIPLE_OBJECTIDS) { | 804 | root_objectid); |
610 | btrfs_set_ref_objectid(leaf, ref, | 805 | btrfs_set_extent_data_ref_objectid(leaf, ref, owner); |
611 | BTRFS_MULTIPLE_OBJECTIDS); | 806 | btrfs_set_extent_data_ref_offset(leaf, ref, offset); |
807 | btrfs_set_extent_data_ref_count(leaf, ref, refs_to_add); | ||
808 | } else { | ||
809 | num_refs = btrfs_extent_data_ref_count(leaf, ref); | ||
810 | num_refs += refs_to_add; | ||
811 | btrfs_set_extent_data_ref_count(leaf, ref, num_refs); | ||
612 | } | 812 | } |
613 | ret = 0; | ||
614 | } else { | ||
615 | goto out; | ||
616 | } | 813 | } |
617 | btrfs_unlock_up_safe(path, 1); | 814 | btrfs_mark_buffer_dirty(leaf); |
618 | btrfs_mark_buffer_dirty(path->nodes[0]); | 815 | ret = 0; |
619 | out: | 816 | fail: |
620 | btrfs_release_path(root, path); | 817 | btrfs_release_path(root, path); |
621 | return ret; | 818 | return ret; |
622 | } | 819 | } |
623 | 820 | ||
624 | static noinline int remove_extent_backref(struct btrfs_trans_handle *trans, | 821 | static noinline int remove_extent_data_ref(struct btrfs_trans_handle *trans, |
625 | struct btrfs_root *root, | 822 | struct btrfs_root *root, |
626 | struct btrfs_path *path, | 823 | struct btrfs_path *path, |
627 | int refs_to_drop) | 824 | int refs_to_drop) |
628 | { | 825 | { |
826 | struct btrfs_key key; | ||
827 | struct btrfs_extent_data_ref *ref1 = NULL; | ||
828 | struct btrfs_shared_data_ref *ref2 = NULL; | ||
629 | struct extent_buffer *leaf; | 829 | struct extent_buffer *leaf; |
630 | struct btrfs_extent_ref *ref; | 830 | u32 num_refs = 0; |
631 | u32 num_refs; | ||
632 | int ret = 0; | 831 | int ret = 0; |
633 | 832 | ||
634 | leaf = path->nodes[0]; | 833 | leaf = path->nodes[0]; |
635 | ref = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_ref); | 834 | btrfs_item_key_to_cpu(leaf, &key, path->slots[0]); |
636 | num_refs = btrfs_ref_num_refs(leaf, ref); | 835 | |
836 | if (key.type == BTRFS_EXTENT_DATA_REF_KEY) { | ||
837 | ref1 = btrfs_item_ptr(leaf, path->slots[0], | ||
838 | struct btrfs_extent_data_ref); | ||
839 | num_refs = btrfs_extent_data_ref_count(leaf, ref1); | ||
840 | } else if (key.type == BTRFS_SHARED_DATA_REF_KEY) { | ||
841 | ref2 = btrfs_item_ptr(leaf, path->slots[0], | ||
842 | struct btrfs_shared_data_ref); | ||
843 | num_refs = btrfs_shared_data_ref_count(leaf, ref2); | ||
844 | #ifdef BTRFS_COMPAT_EXTENT_TREE_V0 | ||
845 | } else if (key.type == BTRFS_EXTENT_REF_V0_KEY) { | ||
846 | struct btrfs_extent_ref_v0 *ref0; | ||
847 | ref0 = btrfs_item_ptr(leaf, path->slots[0], | ||
848 | struct btrfs_extent_ref_v0); | ||
849 | num_refs = btrfs_ref_count_v0(leaf, ref0); | ||
850 | #endif | ||
851 | } else { | ||
852 | BUG(); | ||
853 | } | ||
854 | |||
637 | BUG_ON(num_refs < refs_to_drop); | 855 | BUG_ON(num_refs < refs_to_drop); |
638 | num_refs -= refs_to_drop; | 856 | num_refs -= refs_to_drop; |
857 | |||
639 | if (num_refs == 0) { | 858 | if (num_refs == 0) { |
640 | ret = btrfs_del_item(trans, root, path); | 859 | ret = btrfs_del_item(trans, root, path); |
641 | } else { | 860 | } else { |
642 | btrfs_set_ref_num_refs(leaf, ref, num_refs); | 861 | if (key.type == BTRFS_EXTENT_DATA_REF_KEY) |
862 | btrfs_set_extent_data_ref_count(leaf, ref1, num_refs); | ||
863 | else if (key.type == BTRFS_SHARED_DATA_REF_KEY) | ||
864 | btrfs_set_shared_data_ref_count(leaf, ref2, num_refs); | ||
865 | #ifdef BTRFS_COMPAT_EXTENT_TREE_V0 | ||
866 | else { | ||
867 | struct btrfs_extent_ref_v0 *ref0; | ||
868 | ref0 = btrfs_item_ptr(leaf, path->slots[0], | ||
869 | struct btrfs_extent_ref_v0); | ||
870 | btrfs_set_ref_count_v0(leaf, ref0, num_refs); | ||
871 | } | ||
872 | #endif | ||
643 | btrfs_mark_buffer_dirty(leaf); | 873 | btrfs_mark_buffer_dirty(leaf); |
644 | } | 874 | } |
875 | return ret; | ||
876 | } | ||
877 | |||
878 | static noinline u32 extent_data_ref_count(struct btrfs_root *root, | ||
879 | struct btrfs_path *path, | ||
880 | struct btrfs_extent_inline_ref *iref) | ||
881 | { | ||
882 | struct btrfs_key key; | ||
883 | struct extent_buffer *leaf; | ||
884 | struct btrfs_extent_data_ref *ref1; | ||
885 | struct btrfs_shared_data_ref *ref2; | ||
886 | u32 num_refs = 0; | ||
887 | |||
888 | leaf = path->nodes[0]; | ||
889 | btrfs_item_key_to_cpu(leaf, &key, path->slots[0]); | ||
890 | if (iref) { | ||
891 | if (btrfs_extent_inline_ref_type(leaf, iref) == | ||
892 | BTRFS_EXTENT_DATA_REF_KEY) { | ||
893 | ref1 = (struct btrfs_extent_data_ref *)(&iref->offset); | ||
894 | num_refs = btrfs_extent_data_ref_count(leaf, ref1); | ||
895 | } else { | ||
896 | ref2 = (struct btrfs_shared_data_ref *)(iref + 1); | ||
897 | num_refs = btrfs_shared_data_ref_count(leaf, ref2); | ||
898 | } | ||
899 | } else if (key.type == BTRFS_EXTENT_DATA_REF_KEY) { | ||
900 | ref1 = btrfs_item_ptr(leaf, path->slots[0], | ||
901 | struct btrfs_extent_data_ref); | ||
902 | num_refs = btrfs_extent_data_ref_count(leaf, ref1); | ||
903 | } else if (key.type == BTRFS_SHARED_DATA_REF_KEY) { | ||
904 | ref2 = btrfs_item_ptr(leaf, path->slots[0], | ||
905 | struct btrfs_shared_data_ref); | ||
906 | num_refs = btrfs_shared_data_ref_count(leaf, ref2); | ||
907 | #ifdef BTRFS_COMPAT_EXTENT_TREE_V0 | ||
908 | } else if (key.type == BTRFS_EXTENT_REF_V0_KEY) { | ||
909 | struct btrfs_extent_ref_v0 *ref0; | ||
910 | ref0 = btrfs_item_ptr(leaf, path->slots[0], | ||
911 | struct btrfs_extent_ref_v0); | ||
912 | num_refs = btrfs_ref_count_v0(leaf, ref0); | ||
913 | #endif | ||
914 | } else { | ||
915 | WARN_ON(1); | ||
916 | } | ||
917 | return num_refs; | ||
918 | } | ||
919 | |||
920 | static noinline int lookup_tree_block_ref(struct btrfs_trans_handle *trans, | ||
921 | struct btrfs_root *root, | ||
922 | struct btrfs_path *path, | ||
923 | u64 bytenr, u64 parent, | ||
924 | u64 root_objectid) | ||
925 | { | ||
926 | struct btrfs_key key; | ||
927 | int ret; | ||
928 | |||
929 | key.objectid = bytenr; | ||
930 | if (parent) { | ||
931 | key.type = BTRFS_SHARED_BLOCK_REF_KEY; | ||
932 | key.offset = parent; | ||
933 | } else { | ||
934 | key.type = BTRFS_TREE_BLOCK_REF_KEY; | ||
935 | key.offset = root_objectid; | ||
936 | } | ||
937 | |||
938 | ret = btrfs_search_slot(trans, root, &key, path, -1, 1); | ||
939 | if (ret > 0) | ||
940 | ret = -ENOENT; | ||
941 | #ifdef BTRFS_COMPAT_EXTENT_TREE_V0 | ||
942 | if (ret == -ENOENT && parent) { | ||
943 | btrfs_release_path(root, path); | ||
944 | key.type = BTRFS_EXTENT_REF_V0_KEY; | ||
945 | ret = btrfs_search_slot(trans, root, &key, path, -1, 1); | ||
946 | if (ret > 0) | ||
947 | ret = -ENOENT; | ||
948 | } | ||
949 | #endif | ||
950 | return ret; | ||
951 | } | ||
952 | |||
953 | static noinline int insert_tree_block_ref(struct btrfs_trans_handle *trans, | ||
954 | struct btrfs_root *root, | ||
955 | struct btrfs_path *path, | ||
956 | u64 bytenr, u64 parent, | ||
957 | u64 root_objectid) | ||
958 | { | ||
959 | struct btrfs_key key; | ||
960 | int ret; | ||
961 | |||
962 | key.objectid = bytenr; | ||
963 | if (parent) { | ||
964 | key.type = BTRFS_SHARED_BLOCK_REF_KEY; | ||
965 | key.offset = parent; | ||
966 | } else { | ||
967 | key.type = BTRFS_TREE_BLOCK_REF_KEY; | ||
968 | key.offset = root_objectid; | ||
969 | } | ||
970 | |||
971 | ret = btrfs_insert_empty_item(trans, root, path, &key, 0); | ||
645 | btrfs_release_path(root, path); | 972 | btrfs_release_path(root, path); |
646 | return ret; | 973 | return ret; |
647 | } | 974 | } |
648 | 975 | ||
976 | static inline int extent_ref_type(u64 parent, u64 owner) | ||
977 | { | ||
978 | int type; | ||
979 | if (owner < BTRFS_FIRST_FREE_OBJECTID) { | ||
980 | if (parent > 0) | ||
981 | type = BTRFS_SHARED_BLOCK_REF_KEY; | ||
982 | else | ||
983 | type = BTRFS_TREE_BLOCK_REF_KEY; | ||
984 | } else { | ||
985 | if (parent > 0) | ||
986 | type = BTRFS_SHARED_DATA_REF_KEY; | ||
987 | else | ||
988 | type = BTRFS_EXTENT_DATA_REF_KEY; | ||
989 | } | ||
990 | return type; | ||
991 | } | ||
992 | |||
993 | static int find_next_key(struct btrfs_path *path, struct btrfs_key *key) | ||
994 | |||
995 | { | ||
996 | int level; | ||
997 | BUG_ON(!path->keep_locks); | ||
998 | for (level = 0; level < BTRFS_MAX_LEVEL; level++) { | ||
999 | if (!path->nodes[level]) | ||
1000 | break; | ||
1001 | btrfs_assert_tree_locked(path->nodes[level]); | ||
1002 | if (path->slots[level] + 1 >= | ||
1003 | btrfs_header_nritems(path->nodes[level])) | ||
1004 | continue; | ||
1005 | if (level == 0) | ||
1006 | btrfs_item_key_to_cpu(path->nodes[level], key, | ||
1007 | path->slots[level] + 1); | ||
1008 | else | ||
1009 | btrfs_node_key_to_cpu(path->nodes[level], key, | ||
1010 | path->slots[level] + 1); | ||
1011 | return 0; | ||
1012 | } | ||
1013 | return 1; | ||
1014 | } | ||
1015 | |||
1016 | /* | ||
1017 | * look for inline back ref. if back ref is found, *ref_ret is set | ||
1018 | * to the address of inline back ref, and 0 is returned. | ||
1019 | * | ||
1020 | * if back ref isn't found, *ref_ret is set to the address where it | ||
1021 | * should be inserted, and -ENOENT is returned. | ||
1022 | * | ||
1023 | * if insert is true and there are too many inline back refs, the path | ||
1024 | * points to the extent item, and -EAGAIN is returned. | ||
1025 | * | ||
1026 | * NOTE: inline back refs are ordered in the same way that back ref | ||
1027 | * items in the tree are ordered. | ||
1028 | */ | ||
1029 | static noinline_for_stack | ||
1030 | int lookup_inline_extent_backref(struct btrfs_trans_handle *trans, | ||
1031 | struct btrfs_root *root, | ||
1032 | struct btrfs_path *path, | ||
1033 | struct btrfs_extent_inline_ref **ref_ret, | ||
1034 | u64 bytenr, u64 num_bytes, | ||
1035 | u64 parent, u64 root_objectid, | ||
1036 | u64 owner, u64 offset, int insert) | ||
1037 | { | ||
1038 | struct btrfs_key key; | ||
1039 | struct extent_buffer *leaf; | ||
1040 | struct btrfs_extent_item *ei; | ||
1041 | struct btrfs_extent_inline_ref *iref; | ||
1042 | u64 flags; | ||
1043 | u64 item_size; | ||
1044 | unsigned long ptr; | ||
1045 | unsigned long end; | ||
1046 | int extra_size; | ||
1047 | int type; | ||
1048 | int want; | ||
1049 | int ret; | ||
1050 | int err = 0; | ||
1051 | |||
1052 | key.objectid = bytenr; | ||
1053 | key.type = BTRFS_EXTENT_ITEM_KEY; | ||
1054 | key.offset = num_bytes; | ||
1055 | |||
1056 | want = extent_ref_type(parent, owner); | ||
1057 | if (insert) { | ||
1058 | extra_size = btrfs_extent_inline_ref_size(want); | ||
1059 | path->keep_locks = 1; | ||
1060 | } else | ||
1061 | extra_size = -1; | ||
1062 | ret = btrfs_search_slot(trans, root, &key, path, extra_size, 1); | ||
1063 | if (ret < 0) { | ||
1064 | err = ret; | ||
1065 | goto out; | ||
1066 | } | ||
1067 | BUG_ON(ret); | ||
1068 | |||
1069 | leaf = path->nodes[0]; | ||
1070 | item_size = btrfs_item_size_nr(leaf, path->slots[0]); | ||
1071 | #ifdef BTRFS_COMPAT_EXTENT_TREE_V0 | ||
1072 | if (item_size < sizeof(*ei)) { | ||
1073 | if (!insert) { | ||
1074 | err = -ENOENT; | ||
1075 | goto out; | ||
1076 | } | ||
1077 | ret = convert_extent_item_v0(trans, root, path, owner, | ||
1078 | extra_size); | ||
1079 | if (ret < 0) { | ||
1080 | err = ret; | ||
1081 | goto out; | ||
1082 | } | ||
1083 | leaf = path->nodes[0]; | ||
1084 | item_size = btrfs_item_size_nr(leaf, path->slots[0]); | ||
1085 | } | ||
1086 | #endif | ||
1087 | BUG_ON(item_size < sizeof(*ei)); | ||
1088 | |||
1089 | ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item); | ||
1090 | flags = btrfs_extent_flags(leaf, ei); | ||
1091 | |||
1092 | ptr = (unsigned long)(ei + 1); | ||
1093 | end = (unsigned long)ei + item_size; | ||
1094 | |||
1095 | if (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK) { | ||
1096 | ptr += sizeof(struct btrfs_tree_block_info); | ||
1097 | BUG_ON(ptr > end); | ||
1098 | } else { | ||
1099 | BUG_ON(!(flags & BTRFS_EXTENT_FLAG_DATA)); | ||
1100 | } | ||
1101 | |||
1102 | err = -ENOENT; | ||
1103 | while (1) { | ||
1104 | if (ptr >= end) { | ||
1105 | WARN_ON(ptr > end); | ||
1106 | break; | ||
1107 | } | ||
1108 | iref = (struct btrfs_extent_inline_ref *)ptr; | ||
1109 | type = btrfs_extent_inline_ref_type(leaf, iref); | ||
1110 | if (want < type) | ||
1111 | break; | ||
1112 | if (want > type) { | ||
1113 | ptr += btrfs_extent_inline_ref_size(type); | ||
1114 | continue; | ||
1115 | } | ||
1116 | |||
1117 | if (type == BTRFS_EXTENT_DATA_REF_KEY) { | ||
1118 | struct btrfs_extent_data_ref *dref; | ||
1119 | dref = (struct btrfs_extent_data_ref *)(&iref->offset); | ||
1120 | if (match_extent_data_ref(leaf, dref, root_objectid, | ||
1121 | owner, offset)) { | ||
1122 | err = 0; | ||
1123 | break; | ||
1124 | } | ||
1125 | if (hash_extent_data_ref_item(leaf, dref) < | ||
1126 | hash_extent_data_ref(root_objectid, owner, offset)) | ||
1127 | break; | ||
1128 | } else { | ||
1129 | u64 ref_offset; | ||
1130 | ref_offset = btrfs_extent_inline_ref_offset(leaf, iref); | ||
1131 | if (parent > 0) { | ||
1132 | if (parent == ref_offset) { | ||
1133 | err = 0; | ||
1134 | break; | ||
1135 | } | ||
1136 | if (ref_offset < parent) | ||
1137 | break; | ||
1138 | } else { | ||
1139 | if (root_objectid == ref_offset) { | ||
1140 | err = 0; | ||
1141 | break; | ||
1142 | } | ||
1143 | if (ref_offset < root_objectid) | ||
1144 | break; | ||
1145 | } | ||
1146 | } | ||
1147 | ptr += btrfs_extent_inline_ref_size(type); | ||
1148 | } | ||
1149 | if (err == -ENOENT && insert) { | ||
1150 | if (item_size + extra_size >= | ||
1151 | BTRFS_MAX_EXTENT_ITEM_SIZE(root)) { | ||
1152 | err = -EAGAIN; | ||
1153 | goto out; | ||
1154 | } | ||
1155 | /* | ||
1156 | * To add new inline back ref, we have to make sure | ||
1157 | * there is no corresponding back ref item. | ||
1158 | * For simplicity, we just do not add new inline back | ||
1159 | * ref if there is any kind of item for this block | ||
1160 | */ | ||
1161 | if (find_next_key(path, &key) == 0 && key.objectid == bytenr && | ||
1162 | key.type < BTRFS_BLOCK_GROUP_ITEM_KEY) { | ||
1163 | err = -EAGAIN; | ||
1164 | goto out; | ||
1165 | } | ||
1166 | } | ||
1167 | *ref_ret = (struct btrfs_extent_inline_ref *)ptr; | ||
1168 | out: | ||
1169 | if (insert) { | ||
1170 | path->keep_locks = 0; | ||
1171 | btrfs_unlock_up_safe(path, 1); | ||
1172 | } | ||
1173 | return err; | ||
1174 | } | ||
1175 | |||
1176 | /* | ||
1177 | * helper to add new inline back ref | ||
1178 | */ | ||
1179 | static noinline_for_stack | ||
1180 | int setup_inline_extent_backref(struct btrfs_trans_handle *trans, | ||
1181 | struct btrfs_root *root, | ||
1182 | struct btrfs_path *path, | ||
1183 | struct btrfs_extent_inline_ref *iref, | ||
1184 | u64 parent, u64 root_objectid, | ||
1185 | u64 owner, u64 offset, int refs_to_add, | ||
1186 | struct btrfs_delayed_extent_op *extent_op) | ||
1187 | { | ||
1188 | struct extent_buffer *leaf; | ||
1189 | struct btrfs_extent_item *ei; | ||
1190 | unsigned long ptr; | ||
1191 | unsigned long end; | ||
1192 | unsigned long item_offset; | ||
1193 | u64 refs; | ||
1194 | int size; | ||
1195 | int type; | ||
1196 | int ret; | ||
1197 | |||
1198 | leaf = path->nodes[0]; | ||
1199 | ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item); | ||
1200 | item_offset = (unsigned long)iref - (unsigned long)ei; | ||
1201 | |||
1202 | type = extent_ref_type(parent, owner); | ||
1203 | size = btrfs_extent_inline_ref_size(type); | ||
1204 | |||
1205 | ret = btrfs_extend_item(trans, root, path, size); | ||
1206 | BUG_ON(ret); | ||
1207 | |||
1208 | ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item); | ||
1209 | refs = btrfs_extent_refs(leaf, ei); | ||
1210 | refs += refs_to_add; | ||
1211 | btrfs_set_extent_refs(leaf, ei, refs); | ||
1212 | if (extent_op) | ||
1213 | __run_delayed_extent_op(extent_op, leaf, ei); | ||
1214 | |||
1215 | ptr = (unsigned long)ei + item_offset; | ||
1216 | end = (unsigned long)ei + btrfs_item_size_nr(leaf, path->slots[0]); | ||
1217 | if (ptr < end - size) | ||
1218 | memmove_extent_buffer(leaf, ptr + size, ptr, | ||
1219 | end - size - ptr); | ||
1220 | |||
1221 | iref = (struct btrfs_extent_inline_ref *)ptr; | ||
1222 | btrfs_set_extent_inline_ref_type(leaf, iref, type); | ||
1223 | if (type == BTRFS_EXTENT_DATA_REF_KEY) { | ||
1224 | struct btrfs_extent_data_ref *dref; | ||
1225 | dref = (struct btrfs_extent_data_ref *)(&iref->offset); | ||
1226 | btrfs_set_extent_data_ref_root(leaf, dref, root_objectid); | ||
1227 | btrfs_set_extent_data_ref_objectid(leaf, dref, owner); | ||
1228 | btrfs_set_extent_data_ref_offset(leaf, dref, offset); | ||
1229 | btrfs_set_extent_data_ref_count(leaf, dref, refs_to_add); | ||
1230 | } else if (type == BTRFS_SHARED_DATA_REF_KEY) { | ||
1231 | struct btrfs_shared_data_ref *sref; | ||
1232 | sref = (struct btrfs_shared_data_ref *)(iref + 1); | ||
1233 | btrfs_set_shared_data_ref_count(leaf, sref, refs_to_add); | ||
1234 | btrfs_set_extent_inline_ref_offset(leaf, iref, parent); | ||
1235 | } else if (type == BTRFS_SHARED_BLOCK_REF_KEY) { | ||
1236 | btrfs_set_extent_inline_ref_offset(leaf, iref, parent); | ||
1237 | } else { | ||
1238 | btrfs_set_extent_inline_ref_offset(leaf, iref, root_objectid); | ||
1239 | } | ||
1240 | btrfs_mark_buffer_dirty(leaf); | ||
1241 | return 0; | ||
1242 | } | ||
1243 | |||
1244 | static int lookup_extent_backref(struct btrfs_trans_handle *trans, | ||
1245 | struct btrfs_root *root, | ||
1246 | struct btrfs_path *path, | ||
1247 | struct btrfs_extent_inline_ref **ref_ret, | ||
1248 | u64 bytenr, u64 num_bytes, u64 parent, | ||
1249 | u64 root_objectid, u64 owner, u64 offset) | ||
1250 | { | ||
1251 | int ret; | ||
1252 | |||
1253 | ret = lookup_inline_extent_backref(trans, root, path, ref_ret, | ||
1254 | bytenr, num_bytes, parent, | ||
1255 | root_objectid, owner, offset, 0); | ||
1256 | if (ret != -ENOENT) | ||
1257 | return ret; | ||
1258 | |||
1259 | btrfs_release_path(root, path); | ||
1260 | *ref_ret = NULL; | ||
1261 | |||
1262 | if (owner < BTRFS_FIRST_FREE_OBJECTID) { | ||
1263 | ret = lookup_tree_block_ref(trans, root, path, bytenr, parent, | ||
1264 | root_objectid); | ||
1265 | } else { | ||
1266 | ret = lookup_extent_data_ref(trans, root, path, bytenr, parent, | ||
1267 | root_objectid, owner, offset); | ||
1268 | } | ||
1269 | return ret; | ||
1270 | } | ||
1271 | |||
1272 | /* | ||
1273 | * helper to update/remove inline back ref | ||
1274 | */ | ||
1275 | static noinline_for_stack | ||
1276 | int update_inline_extent_backref(struct btrfs_trans_handle *trans, | ||
1277 | struct btrfs_root *root, | ||
1278 | struct btrfs_path *path, | ||
1279 | struct btrfs_extent_inline_ref *iref, | ||
1280 | int refs_to_mod, | ||
1281 | struct btrfs_delayed_extent_op *extent_op) | ||
1282 | { | ||
1283 | struct extent_buffer *leaf; | ||
1284 | struct btrfs_extent_item *ei; | ||
1285 | struct btrfs_extent_data_ref *dref = NULL; | ||
1286 | struct btrfs_shared_data_ref *sref = NULL; | ||
1287 | unsigned long ptr; | ||
1288 | unsigned long end; | ||
1289 | u32 item_size; | ||
1290 | int size; | ||
1291 | int type; | ||
1292 | int ret; | ||
1293 | u64 refs; | ||
1294 | |||
1295 | leaf = path->nodes[0]; | ||
1296 | ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item); | ||
1297 | refs = btrfs_extent_refs(leaf, ei); | ||
1298 | WARN_ON(refs_to_mod < 0 && refs + refs_to_mod <= 0); | ||
1299 | refs += refs_to_mod; | ||
1300 | btrfs_set_extent_refs(leaf, ei, refs); | ||
1301 | if (extent_op) | ||
1302 | __run_delayed_extent_op(extent_op, leaf, ei); | ||
1303 | |||
1304 | type = btrfs_extent_inline_ref_type(leaf, iref); | ||
1305 | |||
1306 | if (type == BTRFS_EXTENT_DATA_REF_KEY) { | ||
1307 | dref = (struct btrfs_extent_data_ref *)(&iref->offset); | ||
1308 | refs = btrfs_extent_data_ref_count(leaf, dref); | ||
1309 | } else if (type == BTRFS_SHARED_DATA_REF_KEY) { | ||
1310 | sref = (struct btrfs_shared_data_ref *)(iref + 1); | ||
1311 | refs = btrfs_shared_data_ref_count(leaf, sref); | ||
1312 | } else { | ||
1313 | refs = 1; | ||
1314 | BUG_ON(refs_to_mod != -1); | ||
1315 | } | ||
1316 | |||
1317 | BUG_ON(refs_to_mod < 0 && refs < -refs_to_mod); | ||
1318 | refs += refs_to_mod; | ||
1319 | |||
1320 | if (refs > 0) { | ||
1321 | if (type == BTRFS_EXTENT_DATA_REF_KEY) | ||
1322 | btrfs_set_extent_data_ref_count(leaf, dref, refs); | ||
1323 | else | ||
1324 | btrfs_set_shared_data_ref_count(leaf, sref, refs); | ||
1325 | } else { | ||
1326 | size = btrfs_extent_inline_ref_size(type); | ||
1327 | item_size = btrfs_item_size_nr(leaf, path->slots[0]); | ||
1328 | ptr = (unsigned long)iref; | ||
1329 | end = (unsigned long)ei + item_size; | ||
1330 | if (ptr + size < end) | ||
1331 | memmove_extent_buffer(leaf, ptr, ptr + size, | ||
1332 | end - ptr - size); | ||
1333 | item_size -= size; | ||
1334 | ret = btrfs_truncate_item(trans, root, path, item_size, 1); | ||
1335 | BUG_ON(ret); | ||
1336 | } | ||
1337 | btrfs_mark_buffer_dirty(leaf); | ||
1338 | return 0; | ||
1339 | } | ||
1340 | |||
1341 | static noinline_for_stack | ||
1342 | int insert_inline_extent_backref(struct btrfs_trans_handle *trans, | ||
1343 | struct btrfs_root *root, | ||
1344 | struct btrfs_path *path, | ||
1345 | u64 bytenr, u64 num_bytes, u64 parent, | ||
1346 | u64 root_objectid, u64 owner, | ||
1347 | u64 offset, int refs_to_add, | ||
1348 | struct btrfs_delayed_extent_op *extent_op) | ||
1349 | { | ||
1350 | struct btrfs_extent_inline_ref *iref; | ||
1351 | int ret; | ||
1352 | |||
1353 | ret = lookup_inline_extent_backref(trans, root, path, &iref, | ||
1354 | bytenr, num_bytes, parent, | ||
1355 | root_objectid, owner, offset, 1); | ||
1356 | if (ret == 0) { | ||
1357 | BUG_ON(owner < BTRFS_FIRST_FREE_OBJECTID); | ||
1358 | ret = update_inline_extent_backref(trans, root, path, iref, | ||
1359 | refs_to_add, extent_op); | ||
1360 | } else if (ret == -ENOENT) { | ||
1361 | ret = setup_inline_extent_backref(trans, root, path, iref, | ||
1362 | parent, root_objectid, | ||
1363 | owner, offset, refs_to_add, | ||
1364 | extent_op); | ||
1365 | } | ||
1366 | return ret; | ||
1367 | } | ||
1368 | |||
1369 | static int insert_extent_backref(struct btrfs_trans_handle *trans, | ||
1370 | struct btrfs_root *root, | ||
1371 | struct btrfs_path *path, | ||
1372 | u64 bytenr, u64 parent, u64 root_objectid, | ||
1373 | u64 owner, u64 offset, int refs_to_add) | ||
1374 | { | ||
1375 | int ret; | ||
1376 | if (owner < BTRFS_FIRST_FREE_OBJECTID) { | ||
1377 | BUG_ON(refs_to_add != 1); | ||
1378 | ret = insert_tree_block_ref(trans, root, path, bytenr, | ||
1379 | parent, root_objectid); | ||
1380 | } else { | ||
1381 | ret = insert_extent_data_ref(trans, root, path, bytenr, | ||
1382 | parent, root_objectid, | ||
1383 | owner, offset, refs_to_add); | ||
1384 | } | ||
1385 | return ret; | ||
1386 | } | ||
1387 | |||
1388 | static int remove_extent_backref(struct btrfs_trans_handle *trans, | ||
1389 | struct btrfs_root *root, | ||
1390 | struct btrfs_path *path, | ||
1391 | struct btrfs_extent_inline_ref *iref, | ||
1392 | int refs_to_drop, int is_data) | ||
1393 | { | ||
1394 | int ret; | ||
1395 | |||
1396 | BUG_ON(!is_data && refs_to_drop != 1); | ||
1397 | if (iref) { | ||
1398 | ret = update_inline_extent_backref(trans, root, path, iref, | ||
1399 | -refs_to_drop, NULL); | ||
1400 | } else if (is_data) { | ||
1401 | ret = remove_extent_data_ref(trans, root, path, refs_to_drop); | ||
1402 | } else { | ||
1403 | ret = btrfs_del_item(trans, root, path); | ||
1404 | } | ||
1405 | return ret; | ||
1406 | } | ||
1407 | |||
649 | #ifdef BIO_RW_DISCARD | 1408 | #ifdef BIO_RW_DISCARD |
650 | static void btrfs_issue_discard(struct block_device *bdev, | 1409 | static void btrfs_issue_discard(struct block_device *bdev, |
651 | u64 start, u64 len) | 1410 | u64 start, u64 len) |
@@ -686,71 +1445,40 @@ static int btrfs_discard_extent(struct btrfs_root *root, u64 bytenr, | |||
686 | #endif | 1445 | #endif |
687 | } | 1446 | } |
688 | 1447 | ||
689 | static int __btrfs_update_extent_ref(struct btrfs_trans_handle *trans, | 1448 | int btrfs_inc_extent_ref(struct btrfs_trans_handle *trans, |
690 | struct btrfs_root *root, u64 bytenr, | 1449 | struct btrfs_root *root, |
691 | u64 num_bytes, | 1450 | u64 bytenr, u64 num_bytes, u64 parent, |
692 | u64 orig_parent, u64 parent, | 1451 | u64 root_objectid, u64 owner, u64 offset) |
693 | u64 orig_root, u64 ref_root, | ||
694 | u64 orig_generation, u64 ref_generation, | ||
695 | u64 owner_objectid) | ||
696 | { | 1452 | { |
697 | int ret; | 1453 | int ret; |
698 | int pin = owner_objectid < BTRFS_FIRST_FREE_OBJECTID; | 1454 | BUG_ON(owner < BTRFS_FIRST_FREE_OBJECTID && |
1455 | root_objectid == BTRFS_TREE_LOG_OBJECTID); | ||
699 | 1456 | ||
700 | ret = btrfs_update_delayed_ref(trans, bytenr, num_bytes, | 1457 | if (owner < BTRFS_FIRST_FREE_OBJECTID) { |
701 | orig_parent, parent, orig_root, | 1458 | ret = btrfs_add_delayed_tree_ref(trans, bytenr, num_bytes, |
702 | ref_root, orig_generation, | 1459 | parent, root_objectid, (int)owner, |
703 | ref_generation, owner_objectid, pin); | 1460 | BTRFS_ADD_DELAYED_REF, NULL); |
704 | BUG_ON(ret); | 1461 | } else { |
1462 | ret = btrfs_add_delayed_data_ref(trans, bytenr, num_bytes, | ||
1463 | parent, root_objectid, owner, offset, | ||
1464 | BTRFS_ADD_DELAYED_REF, NULL); | ||
1465 | } | ||
705 | return ret; | 1466 | return ret; |
706 | } | 1467 | } |
707 | 1468 | ||
708 | int btrfs_update_extent_ref(struct btrfs_trans_handle *trans, | ||
709 | struct btrfs_root *root, u64 bytenr, | ||
710 | u64 num_bytes, u64 orig_parent, u64 parent, | ||
711 | u64 ref_root, u64 ref_generation, | ||
712 | u64 owner_objectid) | ||
713 | { | ||
714 | int ret; | ||
715 | if (ref_root == BTRFS_TREE_LOG_OBJECTID && | ||
716 | owner_objectid < BTRFS_FIRST_FREE_OBJECTID) | ||
717 | return 0; | ||
718 | |||
719 | ret = __btrfs_update_extent_ref(trans, root, bytenr, num_bytes, | ||
720 | orig_parent, parent, ref_root, | ||
721 | ref_root, ref_generation, | ||
722 | ref_generation, owner_objectid); | ||
723 | return ret; | ||
724 | } | ||
725 | static int __btrfs_inc_extent_ref(struct btrfs_trans_handle *trans, | 1469 | static int __btrfs_inc_extent_ref(struct btrfs_trans_handle *trans, |
726 | struct btrfs_root *root, u64 bytenr, | 1470 | struct btrfs_root *root, |
727 | u64 num_bytes, | 1471 | u64 bytenr, u64 num_bytes, |
728 | u64 orig_parent, u64 parent, | 1472 | u64 parent, u64 root_objectid, |
729 | u64 orig_root, u64 ref_root, | 1473 | u64 owner, u64 offset, int refs_to_add, |
730 | u64 orig_generation, u64 ref_generation, | 1474 | struct btrfs_delayed_extent_op *extent_op) |
731 | u64 owner_objectid) | ||
732 | { | ||
733 | int ret; | ||
734 | |||
735 | ret = btrfs_add_delayed_ref(trans, bytenr, num_bytes, parent, ref_root, | ||
736 | ref_generation, owner_objectid, | ||
737 | BTRFS_ADD_DELAYED_REF, 0); | ||
738 | BUG_ON(ret); | ||
739 | return ret; | ||
740 | } | ||
741 | |||
742 | static noinline_for_stack int add_extent_ref(struct btrfs_trans_handle *trans, | ||
743 | struct btrfs_root *root, u64 bytenr, | ||
744 | u64 num_bytes, u64 parent, u64 ref_root, | ||
745 | u64 ref_generation, u64 owner_objectid, | ||
746 | int refs_to_add) | ||
747 | { | 1475 | { |
748 | struct btrfs_path *path; | 1476 | struct btrfs_path *path; |
749 | int ret; | 1477 | struct extent_buffer *leaf; |
750 | struct btrfs_key key; | ||
751 | struct extent_buffer *l; | ||
752 | struct btrfs_extent_item *item; | 1478 | struct btrfs_extent_item *item; |
753 | u32 refs; | 1479 | u64 refs; |
1480 | int ret; | ||
1481 | int err = 0; | ||
754 | 1482 | ||
755 | path = btrfs_alloc_path(); | 1483 | path = btrfs_alloc_path(); |
756 | if (!path) | 1484 | if (!path) |
@@ -758,43 +1486,27 @@ static noinline_for_stack int add_extent_ref(struct btrfs_trans_handle *trans, | |||
758 | 1486 | ||
759 | path->reada = 1; | 1487 | path->reada = 1; |
760 | path->leave_spinning = 1; | 1488 | path->leave_spinning = 1; |
761 | key.objectid = bytenr; | 1489 | /* this will setup the path even if it fails to insert the back ref */ |
762 | key.type = BTRFS_EXTENT_ITEM_KEY; | 1490 | ret = insert_inline_extent_backref(trans, root->fs_info->extent_root, |
763 | key.offset = num_bytes; | 1491 | path, bytenr, num_bytes, parent, |
764 | 1492 | root_objectid, owner, offset, | |
765 | /* first find the extent item and update its reference count */ | 1493 | refs_to_add, extent_op); |
766 | ret = btrfs_search_slot(trans, root->fs_info->extent_root, &key, | 1494 | if (ret == 0) |
767 | path, 0, 1); | 1495 | goto out; |
768 | if (ret < 0) { | ||
769 | btrfs_set_path_blocking(path); | ||
770 | return ret; | ||
771 | } | ||
772 | |||
773 | if (ret > 0) { | ||
774 | WARN_ON(1); | ||
775 | btrfs_free_path(path); | ||
776 | return -EIO; | ||
777 | } | ||
778 | l = path->nodes[0]; | ||
779 | 1496 | ||
780 | btrfs_item_key_to_cpu(l, &key, path->slots[0]); | 1497 | if (ret != -EAGAIN) { |
781 | if (key.objectid != bytenr) { | 1498 | err = ret; |
782 | btrfs_print_leaf(root->fs_info->extent_root, path->nodes[0]); | 1499 | goto out; |
783 | printk(KERN_ERR "btrfs wanted %llu found %llu\n", | ||
784 | (unsigned long long)bytenr, | ||
785 | (unsigned long long)key.objectid); | ||
786 | BUG(); | ||
787 | } | 1500 | } |
788 | BUG_ON(key.type != BTRFS_EXTENT_ITEM_KEY); | ||
789 | |||
790 | item = btrfs_item_ptr(l, path->slots[0], struct btrfs_extent_item); | ||
791 | |||
792 | refs = btrfs_extent_refs(l, item); | ||
793 | btrfs_set_extent_refs(l, item, refs + refs_to_add); | ||
794 | btrfs_unlock_up_safe(path, 1); | ||
795 | 1501 | ||
796 | btrfs_mark_buffer_dirty(path->nodes[0]); | 1502 | leaf = path->nodes[0]; |
1503 | item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item); | ||
1504 | refs = btrfs_extent_refs(leaf, item); | ||
1505 | btrfs_set_extent_refs(leaf, item, refs + refs_to_add); | ||
1506 | if (extent_op) | ||
1507 | __run_delayed_extent_op(extent_op, leaf, item); | ||
797 | 1508 | ||
1509 | btrfs_mark_buffer_dirty(leaf); | ||
798 | btrfs_release_path(root->fs_info->extent_root, path); | 1510 | btrfs_release_path(root->fs_info->extent_root, path); |
799 | 1511 | ||
800 | path->reada = 1; | 1512 | path->reada = 1; |
@@ -802,56 +1514,197 @@ static noinline_for_stack int add_extent_ref(struct btrfs_trans_handle *trans, | |||
802 | 1514 | ||
803 | /* now insert the actual backref */ | 1515 | /* now insert the actual backref */ |
804 | ret = insert_extent_backref(trans, root->fs_info->extent_root, | 1516 | ret = insert_extent_backref(trans, root->fs_info->extent_root, |
805 | path, bytenr, parent, | 1517 | path, bytenr, parent, root_objectid, |
806 | ref_root, ref_generation, | 1518 | owner, offset, refs_to_add); |
807 | owner_objectid, refs_to_add); | ||
808 | BUG_ON(ret); | 1519 | BUG_ON(ret); |
1520 | out: | ||
809 | btrfs_free_path(path); | 1521 | btrfs_free_path(path); |
810 | return 0; | 1522 | return err; |
811 | } | 1523 | } |
812 | 1524 | ||
813 | int btrfs_inc_extent_ref(struct btrfs_trans_handle *trans, | 1525 | static int run_delayed_data_ref(struct btrfs_trans_handle *trans, |
814 | struct btrfs_root *root, | 1526 | struct btrfs_root *root, |
815 | u64 bytenr, u64 num_bytes, u64 parent, | 1527 | struct btrfs_delayed_ref_node *node, |
816 | u64 ref_root, u64 ref_generation, | 1528 | struct btrfs_delayed_extent_op *extent_op, |
817 | u64 owner_objectid) | 1529 | int insert_reserved) |
818 | { | 1530 | { |
819 | int ret; | 1531 | int ret = 0; |
820 | if (ref_root == BTRFS_TREE_LOG_OBJECTID && | 1532 | struct btrfs_delayed_data_ref *ref; |
821 | owner_objectid < BTRFS_FIRST_FREE_OBJECTID) | 1533 | struct btrfs_key ins; |
822 | return 0; | 1534 | u64 parent = 0; |
1535 | u64 ref_root = 0; | ||
1536 | u64 flags = 0; | ||
823 | 1537 | ||
824 | ret = __btrfs_inc_extent_ref(trans, root, bytenr, num_bytes, 0, parent, | 1538 | ins.objectid = node->bytenr; |
825 | 0, ref_root, 0, ref_generation, | 1539 | ins.offset = node->num_bytes; |
826 | owner_objectid); | 1540 | ins.type = BTRFS_EXTENT_ITEM_KEY; |
1541 | |||
1542 | ref = btrfs_delayed_node_to_data_ref(node); | ||
1543 | if (node->type == BTRFS_SHARED_DATA_REF_KEY) | ||
1544 | parent = ref->parent; | ||
1545 | else | ||
1546 | ref_root = ref->root; | ||
1547 | |||
1548 | if (node->action == BTRFS_ADD_DELAYED_REF && insert_reserved) { | ||
1549 | if (extent_op) { | ||
1550 | BUG_ON(extent_op->update_key); | ||
1551 | flags |= extent_op->flags_to_set; | ||
1552 | } | ||
1553 | ret = alloc_reserved_file_extent(trans, root, | ||
1554 | parent, ref_root, flags, | ||
1555 | ref->objectid, ref->offset, | ||
1556 | &ins, node->ref_mod); | ||
1557 | update_reserved_extents(root, ins.objectid, ins.offset, 0); | ||
1558 | } else if (node->action == BTRFS_ADD_DELAYED_REF) { | ||
1559 | ret = __btrfs_inc_extent_ref(trans, root, node->bytenr, | ||
1560 | node->num_bytes, parent, | ||
1561 | ref_root, ref->objectid, | ||
1562 | ref->offset, node->ref_mod, | ||
1563 | extent_op); | ||
1564 | } else if (node->action == BTRFS_DROP_DELAYED_REF) { | ||
1565 | ret = __btrfs_free_extent(trans, root, node->bytenr, | ||
1566 | node->num_bytes, parent, | ||
1567 | ref_root, ref->objectid, | ||
1568 | ref->offset, node->ref_mod, | ||
1569 | extent_op); | ||
1570 | } else { | ||
1571 | BUG(); | ||
1572 | } | ||
827 | return ret; | 1573 | return ret; |
828 | } | 1574 | } |
829 | 1575 | ||
830 | static int drop_delayed_ref(struct btrfs_trans_handle *trans, | 1576 | static void __run_delayed_extent_op(struct btrfs_delayed_extent_op *extent_op, |
831 | struct btrfs_root *root, | 1577 | struct extent_buffer *leaf, |
832 | struct btrfs_delayed_ref_node *node) | 1578 | struct btrfs_extent_item *ei) |
1579 | { | ||
1580 | u64 flags = btrfs_extent_flags(leaf, ei); | ||
1581 | if (extent_op->update_flags) { | ||
1582 | flags |= extent_op->flags_to_set; | ||
1583 | btrfs_set_extent_flags(leaf, ei, flags); | ||
1584 | } | ||
1585 | |||
1586 | if (extent_op->update_key) { | ||
1587 | struct btrfs_tree_block_info *bi; | ||
1588 | BUG_ON(!(flags & BTRFS_EXTENT_FLAG_TREE_BLOCK)); | ||
1589 | bi = (struct btrfs_tree_block_info *)(ei + 1); | ||
1590 | btrfs_set_tree_block_key(leaf, bi, &extent_op->key); | ||
1591 | } | ||
1592 | } | ||
1593 | |||
1594 | static int run_delayed_extent_op(struct btrfs_trans_handle *trans, | ||
1595 | struct btrfs_root *root, | ||
1596 | struct btrfs_delayed_ref_node *node, | ||
1597 | struct btrfs_delayed_extent_op *extent_op) | ||
1598 | { | ||
1599 | struct btrfs_key key; | ||
1600 | struct btrfs_path *path; | ||
1601 | struct btrfs_extent_item *ei; | ||
1602 | struct extent_buffer *leaf; | ||
1603 | u32 item_size; | ||
1604 | int ret; | ||
1605 | int err = 0; | ||
1606 | |||
1607 | path = btrfs_alloc_path(); | ||
1608 | if (!path) | ||
1609 | return -ENOMEM; | ||
1610 | |||
1611 | key.objectid = node->bytenr; | ||
1612 | key.type = BTRFS_EXTENT_ITEM_KEY; | ||
1613 | key.offset = node->num_bytes; | ||
1614 | |||
1615 | path->reada = 1; | ||
1616 | path->leave_spinning = 1; | ||
1617 | ret = btrfs_search_slot(trans, root->fs_info->extent_root, &key, | ||
1618 | path, 0, 1); | ||
1619 | if (ret < 0) { | ||
1620 | err = ret; | ||
1621 | goto out; | ||
1622 | } | ||
1623 | if (ret > 0) { | ||
1624 | err = -EIO; | ||
1625 | goto out; | ||
1626 | } | ||
1627 | |||
1628 | leaf = path->nodes[0]; | ||
1629 | item_size = btrfs_item_size_nr(leaf, path->slots[0]); | ||
1630 | #ifdef BTRFS_COMPAT_EXTENT_TREE_V0 | ||
1631 | if (item_size < sizeof(*ei)) { | ||
1632 | ret = convert_extent_item_v0(trans, root->fs_info->extent_root, | ||
1633 | path, (u64)-1, 0); | ||
1634 | if (ret < 0) { | ||
1635 | err = ret; | ||
1636 | goto out; | ||
1637 | } | ||
1638 | leaf = path->nodes[0]; | ||
1639 | item_size = btrfs_item_size_nr(leaf, path->slots[0]); | ||
1640 | } | ||
1641 | #endif | ||
1642 | BUG_ON(item_size < sizeof(*ei)); | ||
1643 | ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item); | ||
1644 | __run_delayed_extent_op(extent_op, leaf, ei); | ||
1645 | |||
1646 | btrfs_mark_buffer_dirty(leaf); | ||
1647 | out: | ||
1648 | btrfs_free_path(path); | ||
1649 | return err; | ||
1650 | } | ||
1651 | |||
1652 | static int run_delayed_tree_ref(struct btrfs_trans_handle *trans, | ||
1653 | struct btrfs_root *root, | ||
1654 | struct btrfs_delayed_ref_node *node, | ||
1655 | struct btrfs_delayed_extent_op *extent_op, | ||
1656 | int insert_reserved) | ||
833 | { | 1657 | { |
834 | int ret = 0; | 1658 | int ret = 0; |
835 | struct btrfs_delayed_ref *ref = btrfs_delayed_node_to_ref(node); | 1659 | struct btrfs_delayed_tree_ref *ref; |
1660 | struct btrfs_key ins; | ||
1661 | u64 parent = 0; | ||
1662 | u64 ref_root = 0; | ||
836 | 1663 | ||
837 | BUG_ON(node->ref_mod == 0); | 1664 | ins.objectid = node->bytenr; |
838 | ret = __btrfs_free_extent(trans, root, node->bytenr, node->num_bytes, | 1665 | ins.offset = node->num_bytes; |
839 | node->parent, ref->root, ref->generation, | 1666 | ins.type = BTRFS_EXTENT_ITEM_KEY; |
840 | ref->owner_objectid, ref->pin, node->ref_mod); | ||
841 | 1667 | ||
1668 | ref = btrfs_delayed_node_to_tree_ref(node); | ||
1669 | if (node->type == BTRFS_SHARED_BLOCK_REF_KEY) | ||
1670 | parent = ref->parent; | ||
1671 | else | ||
1672 | ref_root = ref->root; | ||
1673 | |||
1674 | BUG_ON(node->ref_mod != 1); | ||
1675 | if (node->action == BTRFS_ADD_DELAYED_REF && insert_reserved) { | ||
1676 | BUG_ON(!extent_op || !extent_op->update_flags || | ||
1677 | !extent_op->update_key); | ||
1678 | ret = alloc_reserved_tree_block(trans, root, | ||
1679 | parent, ref_root, | ||
1680 | extent_op->flags_to_set, | ||
1681 | &extent_op->key, | ||
1682 | ref->level, &ins); | ||
1683 | update_reserved_extents(root, ins.objectid, ins.offset, 0); | ||
1684 | } else if (node->action == BTRFS_ADD_DELAYED_REF) { | ||
1685 | ret = __btrfs_inc_extent_ref(trans, root, node->bytenr, | ||
1686 | node->num_bytes, parent, ref_root, | ||
1687 | ref->level, 0, 1, extent_op); | ||
1688 | } else if (node->action == BTRFS_DROP_DELAYED_REF) { | ||
1689 | ret = __btrfs_free_extent(trans, root, node->bytenr, | ||
1690 | node->num_bytes, parent, ref_root, | ||
1691 | ref->level, 0, 1, extent_op); | ||
1692 | } else { | ||
1693 | BUG(); | ||
1694 | } | ||
842 | return ret; | 1695 | return ret; |
843 | } | 1696 | } |
844 | 1697 | ||
1698 | |||
845 | /* helper function to actually process a single delayed ref entry */ | 1699 | /* helper function to actually process a single delayed ref entry */ |
846 | static noinline int run_one_delayed_ref(struct btrfs_trans_handle *trans, | 1700 | static int run_one_delayed_ref(struct btrfs_trans_handle *trans, |
847 | struct btrfs_root *root, | 1701 | struct btrfs_root *root, |
848 | struct btrfs_delayed_ref_node *node, | 1702 | struct btrfs_delayed_ref_node *node, |
849 | int insert_reserved) | 1703 | struct btrfs_delayed_extent_op *extent_op, |
1704 | int insert_reserved) | ||
850 | { | 1705 | { |
851 | int ret; | 1706 | int ret; |
852 | struct btrfs_delayed_ref *ref; | 1707 | if (btrfs_delayed_ref_is_head(node)) { |
853 | |||
854 | if (node->parent == (u64)-1) { | ||
855 | struct btrfs_delayed_ref_head *head; | 1708 | struct btrfs_delayed_ref_head *head; |
856 | /* | 1709 | /* |
857 | * we've hit the end of the chain and we were supposed | 1710 | * we've hit the end of the chain and we were supposed |
@@ -859,44 +1712,35 @@ static noinline int run_one_delayed_ref(struct btrfs_trans_handle *trans, | |||
859 | * deleted before we ever needed to insert it, so all | 1712 | * deleted before we ever needed to insert it, so all |
860 | * we have to do is clean up the accounting | 1713 | * we have to do is clean up the accounting |
861 | */ | 1714 | */ |
1715 | BUG_ON(extent_op); | ||
1716 | head = btrfs_delayed_node_to_head(node); | ||
862 | if (insert_reserved) { | 1717 | if (insert_reserved) { |
1718 | if (head->is_data) { | ||
1719 | ret = btrfs_del_csums(trans, root, | ||
1720 | node->bytenr, | ||
1721 | node->num_bytes); | ||
1722 | BUG_ON(ret); | ||
1723 | } | ||
1724 | btrfs_update_pinned_extents(root, node->bytenr, | ||
1725 | node->num_bytes, 1); | ||
863 | update_reserved_extents(root, node->bytenr, | 1726 | update_reserved_extents(root, node->bytenr, |
864 | node->num_bytes, 0); | 1727 | node->num_bytes, 0); |
865 | } | 1728 | } |
866 | head = btrfs_delayed_node_to_head(node); | ||
867 | mutex_unlock(&head->mutex); | 1729 | mutex_unlock(&head->mutex); |
868 | return 0; | 1730 | return 0; |
869 | } | 1731 | } |
870 | 1732 | ||
871 | ref = btrfs_delayed_node_to_ref(node); | 1733 | if (node->type == BTRFS_TREE_BLOCK_REF_KEY || |
872 | if (ref->action == BTRFS_ADD_DELAYED_REF) { | 1734 | node->type == BTRFS_SHARED_BLOCK_REF_KEY) |
873 | if (insert_reserved) { | 1735 | ret = run_delayed_tree_ref(trans, root, node, extent_op, |
874 | struct btrfs_key ins; | 1736 | insert_reserved); |
875 | 1737 | else if (node->type == BTRFS_EXTENT_DATA_REF_KEY || | |
876 | ins.objectid = node->bytenr; | 1738 | node->type == BTRFS_SHARED_DATA_REF_KEY) |
877 | ins.offset = node->num_bytes; | 1739 | ret = run_delayed_data_ref(trans, root, node, extent_op, |
878 | ins.type = BTRFS_EXTENT_ITEM_KEY; | 1740 | insert_reserved); |
879 | 1741 | else | |
880 | /* record the full extent allocation */ | 1742 | BUG(); |
881 | ret = __btrfs_alloc_reserved_extent(trans, root, | 1743 | return ret; |
882 | node->parent, ref->root, | ||
883 | ref->generation, ref->owner_objectid, | ||
884 | &ins, node->ref_mod); | ||
885 | update_reserved_extents(root, node->bytenr, | ||
886 | node->num_bytes, 0); | ||
887 | } else { | ||
888 | /* just add one backref */ | ||
889 | ret = add_extent_ref(trans, root, node->bytenr, | ||
890 | node->num_bytes, | ||
891 | node->parent, ref->root, ref->generation, | ||
892 | ref->owner_objectid, node->ref_mod); | ||
893 | } | ||
894 | BUG_ON(ret); | ||
895 | } else if (ref->action == BTRFS_DROP_DELAYED_REF) { | ||
896 | WARN_ON(insert_reserved); | ||
897 | ret = drop_delayed_ref(trans, root, node); | ||
898 | } | ||
899 | return 0; | ||
900 | } | 1744 | } |
901 | 1745 | ||
902 | static noinline struct btrfs_delayed_ref_node * | 1746 | static noinline struct btrfs_delayed_ref_node * |
@@ -919,7 +1763,7 @@ again: | |||
919 | rb_node); | 1763 | rb_node); |
920 | if (ref->bytenr != head->node.bytenr) | 1764 | if (ref->bytenr != head->node.bytenr) |
921 | break; | 1765 | break; |
922 | if (btrfs_delayed_node_to_ref(ref)->action == action) | 1766 | if (ref->action == action) |
923 | return ref; | 1767 | return ref; |
924 | node = rb_prev(node); | 1768 | node = rb_prev(node); |
925 | } | 1769 | } |
@@ -937,6 +1781,7 @@ static noinline int run_clustered_refs(struct btrfs_trans_handle *trans, | |||
937 | struct btrfs_delayed_ref_root *delayed_refs; | 1781 | struct btrfs_delayed_ref_root *delayed_refs; |
938 | struct btrfs_delayed_ref_node *ref; | 1782 | struct btrfs_delayed_ref_node *ref; |
939 | struct btrfs_delayed_ref_head *locked_ref = NULL; | 1783 | struct btrfs_delayed_ref_head *locked_ref = NULL; |
1784 | struct btrfs_delayed_extent_op *extent_op; | ||
940 | int ret; | 1785 | int ret; |
941 | int count = 0; | 1786 | int count = 0; |
942 | int must_insert_reserved = 0; | 1787 | int must_insert_reserved = 0; |
@@ -975,6 +1820,9 @@ static noinline int run_clustered_refs(struct btrfs_trans_handle *trans, | |||
975 | must_insert_reserved = locked_ref->must_insert_reserved; | 1820 | must_insert_reserved = locked_ref->must_insert_reserved; |
976 | locked_ref->must_insert_reserved = 0; | 1821 | locked_ref->must_insert_reserved = 0; |
977 | 1822 | ||
1823 | extent_op = locked_ref->extent_op; | ||
1824 | locked_ref->extent_op = NULL; | ||
1825 | |||
978 | /* | 1826 | /* |
979 | * locked_ref is the head node, so we have to go one | 1827 | * locked_ref is the head node, so we have to go one |
980 | * node back for any delayed ref updates | 1828 | * node back for any delayed ref updates |
@@ -986,6 +1834,25 @@ static noinline int run_clustered_refs(struct btrfs_trans_handle *trans, | |||
986 | * so that any accounting fixes can happen | 1834 | * so that any accounting fixes can happen |
987 | */ | 1835 | */ |
988 | ref = &locked_ref->node; | 1836 | ref = &locked_ref->node; |
1837 | |||
1838 | if (extent_op && must_insert_reserved) { | ||
1839 | kfree(extent_op); | ||
1840 | extent_op = NULL; | ||
1841 | } | ||
1842 | |||
1843 | if (extent_op) { | ||
1844 | spin_unlock(&delayed_refs->lock); | ||
1845 | |||
1846 | ret = run_delayed_extent_op(trans, root, | ||
1847 | ref, extent_op); | ||
1848 | BUG_ON(ret); | ||
1849 | kfree(extent_op); | ||
1850 | |||
1851 | cond_resched(); | ||
1852 | spin_lock(&delayed_refs->lock); | ||
1853 | continue; | ||
1854 | } | ||
1855 | |||
989 | list_del_init(&locked_ref->cluster); | 1856 | list_del_init(&locked_ref->cluster); |
990 | locked_ref = NULL; | 1857 | locked_ref = NULL; |
991 | } | 1858 | } |
@@ -993,14 +1860,17 @@ static noinline int run_clustered_refs(struct btrfs_trans_handle *trans, | |||
993 | ref->in_tree = 0; | 1860 | ref->in_tree = 0; |
994 | rb_erase(&ref->rb_node, &delayed_refs->root); | 1861 | rb_erase(&ref->rb_node, &delayed_refs->root); |
995 | delayed_refs->num_entries--; | 1862 | delayed_refs->num_entries--; |
1863 | |||
996 | spin_unlock(&delayed_refs->lock); | 1864 | spin_unlock(&delayed_refs->lock); |
997 | 1865 | ||
998 | ret = run_one_delayed_ref(trans, root, ref, | 1866 | ret = run_one_delayed_ref(trans, root, ref, extent_op, |
999 | must_insert_reserved); | 1867 | must_insert_reserved); |
1000 | BUG_ON(ret); | 1868 | BUG_ON(ret); |
1001 | btrfs_put_delayed_ref(ref); | ||
1002 | 1869 | ||
1870 | btrfs_put_delayed_ref(ref); | ||
1871 | kfree(extent_op); | ||
1003 | count++; | 1872 | count++; |
1873 | |||
1004 | cond_resched(); | 1874 | cond_resched(); |
1005 | spin_lock(&delayed_refs->lock); | 1875 | spin_lock(&delayed_refs->lock); |
1006 | } | 1876 | } |
@@ -1095,25 +1965,112 @@ out: | |||
1095 | return 0; | 1965 | return 0; |
1096 | } | 1966 | } |
1097 | 1967 | ||
1098 | int btrfs_cross_ref_exist(struct btrfs_trans_handle *trans, | 1968 | int btrfs_set_disk_extent_flags(struct btrfs_trans_handle *trans, |
1099 | struct btrfs_root *root, u64 objectid, u64 bytenr) | 1969 | struct btrfs_root *root, |
1970 | u64 bytenr, u64 num_bytes, u64 flags, | ||
1971 | int is_data) | ||
1972 | { | ||
1973 | struct btrfs_delayed_extent_op *extent_op; | ||
1974 | int ret; | ||
1975 | |||
1976 | extent_op = kmalloc(sizeof(*extent_op), GFP_NOFS); | ||
1977 | if (!extent_op) | ||
1978 | return -ENOMEM; | ||
1979 | |||
1980 | extent_op->flags_to_set = flags; | ||
1981 | extent_op->update_flags = 1; | ||
1982 | extent_op->update_key = 0; | ||
1983 | extent_op->is_data = is_data ? 1 : 0; | ||
1984 | |||
1985 | ret = btrfs_add_delayed_extent_op(trans, bytenr, num_bytes, extent_op); | ||
1986 | if (ret) | ||
1987 | kfree(extent_op); | ||
1988 | return ret; | ||
1989 | } | ||
1990 | |||
1991 | static noinline int check_delayed_ref(struct btrfs_trans_handle *trans, | ||
1992 | struct btrfs_root *root, | ||
1993 | struct btrfs_path *path, | ||
1994 | u64 objectid, u64 offset, u64 bytenr) | ||
1995 | { | ||
1996 | struct btrfs_delayed_ref_head *head; | ||
1997 | struct btrfs_delayed_ref_node *ref; | ||
1998 | struct btrfs_delayed_data_ref *data_ref; | ||
1999 | struct btrfs_delayed_ref_root *delayed_refs; | ||
2000 | struct rb_node *node; | ||
2001 | int ret = 0; | ||
2002 | |||
2003 | ret = -ENOENT; | ||
2004 | delayed_refs = &trans->transaction->delayed_refs; | ||
2005 | spin_lock(&delayed_refs->lock); | ||
2006 | head = btrfs_find_delayed_ref_head(trans, bytenr); | ||
2007 | if (!head) | ||
2008 | goto out; | ||
2009 | |||
2010 | if (!mutex_trylock(&head->mutex)) { | ||
2011 | atomic_inc(&head->node.refs); | ||
2012 | spin_unlock(&delayed_refs->lock); | ||
2013 | |||
2014 | btrfs_release_path(root->fs_info->extent_root, path); | ||
2015 | |||
2016 | mutex_lock(&head->mutex); | ||
2017 | mutex_unlock(&head->mutex); | ||
2018 | btrfs_put_delayed_ref(&head->node); | ||
2019 | return -EAGAIN; | ||
2020 | } | ||
2021 | |||
2022 | node = rb_prev(&head->node.rb_node); | ||
2023 | if (!node) | ||
2024 | goto out_unlock; | ||
2025 | |||
2026 | ref = rb_entry(node, struct btrfs_delayed_ref_node, rb_node); | ||
2027 | |||
2028 | if (ref->bytenr != bytenr) | ||
2029 | goto out_unlock; | ||
2030 | |||
2031 | ret = 1; | ||
2032 | if (ref->type != BTRFS_EXTENT_DATA_REF_KEY) | ||
2033 | goto out_unlock; | ||
2034 | |||
2035 | data_ref = btrfs_delayed_node_to_data_ref(ref); | ||
2036 | |||
2037 | node = rb_prev(node); | ||
2038 | if (node) { | ||
2039 | ref = rb_entry(node, struct btrfs_delayed_ref_node, rb_node); | ||
2040 | if (ref->bytenr == bytenr) | ||
2041 | goto out_unlock; | ||
2042 | } | ||
2043 | |||
2044 | if (data_ref->root != root->root_key.objectid || | ||
2045 | data_ref->objectid != objectid || data_ref->offset != offset) | ||
2046 | goto out_unlock; | ||
2047 | |||
2048 | ret = 0; | ||
2049 | out_unlock: | ||
2050 | mutex_unlock(&head->mutex); | ||
2051 | out: | ||
2052 | spin_unlock(&delayed_refs->lock); | ||
2053 | return ret; | ||
2054 | } | ||
2055 | |||
2056 | static noinline int check_committed_ref(struct btrfs_trans_handle *trans, | ||
2057 | struct btrfs_root *root, | ||
2058 | struct btrfs_path *path, | ||
2059 | u64 objectid, u64 offset, u64 bytenr) | ||
1100 | { | 2060 | { |
1101 | struct btrfs_root *extent_root = root->fs_info->extent_root; | 2061 | struct btrfs_root *extent_root = root->fs_info->extent_root; |
1102 | struct btrfs_path *path; | ||
1103 | struct extent_buffer *leaf; | 2062 | struct extent_buffer *leaf; |
1104 | struct btrfs_extent_ref *ref_item; | 2063 | struct btrfs_extent_data_ref *ref; |
2064 | struct btrfs_extent_inline_ref *iref; | ||
2065 | struct btrfs_extent_item *ei; | ||
1105 | struct btrfs_key key; | 2066 | struct btrfs_key key; |
1106 | struct btrfs_key found_key; | 2067 | u32 item_size; |
1107 | u64 ref_root; | ||
1108 | u64 last_snapshot; | ||
1109 | u32 nritems; | ||
1110 | int ret; | 2068 | int ret; |
1111 | 2069 | ||
1112 | key.objectid = bytenr; | 2070 | key.objectid = bytenr; |
1113 | key.offset = (u64)-1; | 2071 | key.offset = (u64)-1; |
1114 | key.type = BTRFS_EXTENT_ITEM_KEY; | 2072 | key.type = BTRFS_EXTENT_ITEM_KEY; |
1115 | 2073 | ||
1116 | path = btrfs_alloc_path(); | ||
1117 | ret = btrfs_search_slot(NULL, extent_root, &key, path, 0, 0); | 2074 | ret = btrfs_search_slot(NULL, extent_root, &key, path, 0, 0); |
1118 | if (ret < 0) | 2075 | if (ret < 0) |
1119 | goto out; | 2076 | goto out; |
@@ -1125,55 +2082,83 @@ int btrfs_cross_ref_exist(struct btrfs_trans_handle *trans, | |||
1125 | 2082 | ||
1126 | path->slots[0]--; | 2083 | path->slots[0]--; |
1127 | leaf = path->nodes[0]; | 2084 | leaf = path->nodes[0]; |
1128 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); | 2085 | btrfs_item_key_to_cpu(leaf, &key, path->slots[0]); |
1129 | 2086 | ||
1130 | if (found_key.objectid != bytenr || | 2087 | if (key.objectid != bytenr || key.type != BTRFS_EXTENT_ITEM_KEY) |
1131 | found_key.type != BTRFS_EXTENT_ITEM_KEY) | ||
1132 | goto out; | 2088 | goto out; |
1133 | 2089 | ||
1134 | last_snapshot = btrfs_root_last_snapshot(&root->root_item); | 2090 | ret = 1; |
1135 | while (1) { | 2091 | item_size = btrfs_item_size_nr(leaf, path->slots[0]); |
1136 | leaf = path->nodes[0]; | 2092 | #ifdef BTRFS_COMPAT_EXTENT_TREE_V0 |
1137 | nritems = btrfs_header_nritems(leaf); | 2093 | if (item_size < sizeof(*ei)) { |
1138 | if (path->slots[0] >= nritems) { | 2094 | WARN_ON(item_size != sizeof(struct btrfs_extent_item_v0)); |
1139 | ret = btrfs_next_leaf(extent_root, path); | 2095 | goto out; |
1140 | if (ret < 0) | 2096 | } |
1141 | goto out; | 2097 | #endif |
1142 | if (ret == 0) | 2098 | ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item); |
1143 | continue; | ||
1144 | break; | ||
1145 | } | ||
1146 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); | ||
1147 | if (found_key.objectid != bytenr) | ||
1148 | break; | ||
1149 | 2099 | ||
1150 | if (found_key.type != BTRFS_EXTENT_REF_KEY) { | 2100 | if (item_size != sizeof(*ei) + |
1151 | path->slots[0]++; | 2101 | btrfs_extent_inline_ref_size(BTRFS_EXTENT_DATA_REF_KEY)) |
1152 | continue; | 2102 | goto out; |
1153 | } | ||
1154 | 2103 | ||
1155 | ref_item = btrfs_item_ptr(leaf, path->slots[0], | 2104 | if (btrfs_extent_generation(leaf, ei) <= |
1156 | struct btrfs_extent_ref); | 2105 | btrfs_root_last_snapshot(&root->root_item)) |
1157 | ref_root = btrfs_ref_root(leaf, ref_item); | 2106 | goto out; |
1158 | if ((ref_root != root->root_key.objectid && | 2107 | |
1159 | ref_root != BTRFS_TREE_LOG_OBJECTID) || | 2108 | iref = (struct btrfs_extent_inline_ref *)(ei + 1); |
1160 | objectid != btrfs_ref_objectid(leaf, ref_item)) { | 2109 | if (btrfs_extent_inline_ref_type(leaf, iref) != |
1161 | ret = 1; | 2110 | BTRFS_EXTENT_DATA_REF_KEY) |
1162 | goto out; | 2111 | goto out; |
1163 | } | 2112 | |
1164 | if (btrfs_ref_generation(leaf, ref_item) <= last_snapshot) { | 2113 | ref = (struct btrfs_extent_data_ref *)(&iref->offset); |
1165 | ret = 1; | 2114 | if (btrfs_extent_refs(leaf, ei) != |
2115 | btrfs_extent_data_ref_count(leaf, ref) || | ||
2116 | btrfs_extent_data_ref_root(leaf, ref) != | ||
2117 | root->root_key.objectid || | ||
2118 | btrfs_extent_data_ref_objectid(leaf, ref) != objectid || | ||
2119 | btrfs_extent_data_ref_offset(leaf, ref) != offset) | ||
2120 | goto out; | ||
2121 | |||
2122 | ret = 0; | ||
2123 | out: | ||
2124 | return ret; | ||
2125 | } | ||
2126 | |||
2127 | int btrfs_cross_ref_exist(struct btrfs_trans_handle *trans, | ||
2128 | struct btrfs_root *root, | ||
2129 | u64 objectid, u64 offset, u64 bytenr) | ||
2130 | { | ||
2131 | struct btrfs_path *path; | ||
2132 | int ret; | ||
2133 | int ret2; | ||
2134 | |||
2135 | path = btrfs_alloc_path(); | ||
2136 | if (!path) | ||
2137 | return -ENOENT; | ||
2138 | |||
2139 | do { | ||
2140 | ret = check_committed_ref(trans, root, path, objectid, | ||
2141 | offset, bytenr); | ||
2142 | if (ret && ret != -ENOENT) | ||
1166 | goto out; | 2143 | goto out; |
1167 | } | ||
1168 | 2144 | ||
1169 | path->slots[0]++; | 2145 | ret2 = check_delayed_ref(trans, root, path, objectid, |
2146 | offset, bytenr); | ||
2147 | } while (ret2 == -EAGAIN); | ||
2148 | |||
2149 | if (ret2 && ret2 != -ENOENT) { | ||
2150 | ret = ret2; | ||
2151 | goto out; | ||
1170 | } | 2152 | } |
1171 | ret = 0; | 2153 | |
2154 | if (ret != -ENOENT || ret2 != -ENOENT) | ||
2155 | ret = 0; | ||
1172 | out: | 2156 | out: |
1173 | btrfs_free_path(path); | 2157 | btrfs_free_path(path); |
1174 | return ret; | 2158 | return ret; |
1175 | } | 2159 | } |
1176 | 2160 | ||
2161 | #if 0 | ||
1177 | int btrfs_cache_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root, | 2162 | int btrfs_cache_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root, |
1178 | struct extent_buffer *buf, u32 nr_extents) | 2163 | struct extent_buffer *buf, u32 nr_extents) |
1179 | { | 2164 | { |
@@ -1291,62 +2276,44 @@ static int refsort_cmp(const void *a_void, const void *b_void) | |||
1291 | return 1; | 2276 | return 1; |
1292 | return 0; | 2277 | return 0; |
1293 | } | 2278 | } |
2279 | #endif | ||
1294 | 2280 | ||
1295 | 2281 | static int __btrfs_mod_ref(struct btrfs_trans_handle *trans, | |
1296 | noinline int btrfs_inc_ref(struct btrfs_trans_handle *trans, | ||
1297 | struct btrfs_root *root, | 2282 | struct btrfs_root *root, |
1298 | struct extent_buffer *orig_buf, | 2283 | struct extent_buffer *buf, |
1299 | struct extent_buffer *buf, u32 *nr_extents) | 2284 | int full_backref, int inc) |
1300 | { | 2285 | { |
1301 | u64 bytenr; | 2286 | u64 bytenr; |
2287 | u64 num_bytes; | ||
2288 | u64 parent; | ||
1302 | u64 ref_root; | 2289 | u64 ref_root; |
1303 | u64 orig_root; | ||
1304 | u64 ref_generation; | ||
1305 | u64 orig_generation; | ||
1306 | struct refsort *sorted; | ||
1307 | u32 nritems; | 2290 | u32 nritems; |
1308 | u32 nr_file_extents = 0; | ||
1309 | struct btrfs_key key; | 2291 | struct btrfs_key key; |
1310 | struct btrfs_file_extent_item *fi; | 2292 | struct btrfs_file_extent_item *fi; |
1311 | int i; | 2293 | int i; |
1312 | int level; | 2294 | int level; |
1313 | int ret = 0; | 2295 | int ret = 0; |
1314 | int faili = 0; | ||
1315 | int refi = 0; | ||
1316 | int slot; | ||
1317 | int (*process_func)(struct btrfs_trans_handle *, struct btrfs_root *, | 2296 | int (*process_func)(struct btrfs_trans_handle *, struct btrfs_root *, |
1318 | u64, u64, u64, u64, u64, u64, u64, u64, u64); | 2297 | u64, u64, u64, u64, u64, u64); |
1319 | 2298 | ||
1320 | ref_root = btrfs_header_owner(buf); | 2299 | ref_root = btrfs_header_owner(buf); |
1321 | ref_generation = btrfs_header_generation(buf); | ||
1322 | orig_root = btrfs_header_owner(orig_buf); | ||
1323 | orig_generation = btrfs_header_generation(orig_buf); | ||
1324 | |||
1325 | nritems = btrfs_header_nritems(buf); | 2300 | nritems = btrfs_header_nritems(buf); |
1326 | level = btrfs_header_level(buf); | 2301 | level = btrfs_header_level(buf); |
1327 | 2302 | ||
1328 | sorted = kmalloc(sizeof(struct refsort) * nritems, GFP_NOFS); | 2303 | if (!root->ref_cows && level == 0) |
1329 | BUG_ON(!sorted); | 2304 | return 0; |
1330 | 2305 | ||
1331 | if (root->ref_cows) { | 2306 | if (inc) |
1332 | process_func = __btrfs_inc_extent_ref; | 2307 | process_func = btrfs_inc_extent_ref; |
1333 | } else { | 2308 | else |
1334 | if (level == 0 && | 2309 | process_func = btrfs_free_extent; |
1335 | root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID) | 2310 | |
1336 | goto out; | 2311 | if (full_backref) |
1337 | if (level != 0 && | 2312 | parent = buf->start; |
1338 | root->root_key.objectid == BTRFS_TREE_LOG_OBJECTID) | 2313 | else |
1339 | goto out; | 2314 | parent = 0; |
1340 | process_func = __btrfs_update_extent_ref; | ||
1341 | } | ||
1342 | 2315 | ||
1343 | /* | ||
1344 | * we make two passes through the items. In the first pass we | ||
1345 | * only record the byte number and slot. Then we sort based on | ||
1346 | * byte number and do the actual work based on the sorted results | ||
1347 | */ | ||
1348 | for (i = 0; i < nritems; i++) { | 2316 | for (i = 0; i < nritems; i++) { |
1349 | cond_resched(); | ||
1350 | if (level == 0) { | 2317 | if (level == 0) { |
1351 | btrfs_item_key_to_cpu(buf, &key, i); | 2318 | btrfs_item_key_to_cpu(buf, &key, i); |
1352 | if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY) | 2319 | if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY) |
@@ -1360,151 +2327,38 @@ noinline int btrfs_inc_ref(struct btrfs_trans_handle *trans, | |||
1360 | if (bytenr == 0) | 2327 | if (bytenr == 0) |
1361 | continue; | 2328 | continue; |
1362 | 2329 | ||
1363 | nr_file_extents++; | 2330 | num_bytes = btrfs_file_extent_disk_num_bytes(buf, fi); |
1364 | sorted[refi].bytenr = bytenr; | 2331 | key.offset -= btrfs_file_extent_offset(buf, fi); |
1365 | sorted[refi].slot = i; | 2332 | ret = process_func(trans, root, bytenr, num_bytes, |
1366 | refi++; | 2333 | parent, ref_root, key.objectid, |
1367 | } else { | 2334 | key.offset); |
1368 | bytenr = btrfs_node_blockptr(buf, i); | 2335 | if (ret) |
1369 | sorted[refi].bytenr = bytenr; | ||
1370 | sorted[refi].slot = i; | ||
1371 | refi++; | ||
1372 | } | ||
1373 | } | ||
1374 | /* | ||
1375 | * if refi == 0, we didn't actually put anything into the sorted | ||
1376 | * array and we're done | ||
1377 | */ | ||
1378 | if (refi == 0) | ||
1379 | goto out; | ||
1380 | |||
1381 | sort(sorted, refi, sizeof(struct refsort), refsort_cmp, NULL); | ||
1382 | |||
1383 | for (i = 0; i < refi; i++) { | ||
1384 | cond_resched(); | ||
1385 | slot = sorted[i].slot; | ||
1386 | bytenr = sorted[i].bytenr; | ||
1387 | |||
1388 | if (level == 0) { | ||
1389 | btrfs_item_key_to_cpu(buf, &key, slot); | ||
1390 | fi = btrfs_item_ptr(buf, slot, | ||
1391 | struct btrfs_file_extent_item); | ||
1392 | |||
1393 | bytenr = btrfs_file_extent_disk_bytenr(buf, fi); | ||
1394 | if (bytenr == 0) | ||
1395 | continue; | ||
1396 | |||
1397 | ret = process_func(trans, root, bytenr, | ||
1398 | btrfs_file_extent_disk_num_bytes(buf, fi), | ||
1399 | orig_buf->start, buf->start, | ||
1400 | orig_root, ref_root, | ||
1401 | orig_generation, ref_generation, | ||
1402 | key.objectid); | ||
1403 | |||
1404 | if (ret) { | ||
1405 | faili = slot; | ||
1406 | WARN_ON(1); | ||
1407 | goto fail; | 2336 | goto fail; |
1408 | } | ||
1409 | } else { | 2337 | } else { |
1410 | ret = process_func(trans, root, bytenr, buf->len, | 2338 | bytenr = btrfs_node_blockptr(buf, i); |
1411 | orig_buf->start, buf->start, | 2339 | num_bytes = btrfs_level_size(root, level - 1); |
1412 | orig_root, ref_root, | 2340 | ret = process_func(trans, root, bytenr, num_bytes, |
1413 | orig_generation, ref_generation, | 2341 | parent, ref_root, level - 1, 0); |
1414 | level - 1); | 2342 | if (ret) |
1415 | if (ret) { | ||
1416 | faili = slot; | ||
1417 | WARN_ON(1); | ||
1418 | goto fail; | 2343 | goto fail; |
1419 | } | ||
1420 | } | 2344 | } |
1421 | } | 2345 | } |
1422 | out: | ||
1423 | kfree(sorted); | ||
1424 | if (nr_extents) { | ||
1425 | if (level == 0) | ||
1426 | *nr_extents = nr_file_extents; | ||
1427 | else | ||
1428 | *nr_extents = nritems; | ||
1429 | } | ||
1430 | return 0; | 2346 | return 0; |
1431 | fail: | 2347 | fail: |
1432 | kfree(sorted); | 2348 | BUG(); |
1433 | WARN_ON(1); | ||
1434 | return ret; | 2349 | return ret; |
1435 | } | 2350 | } |
1436 | 2351 | ||
1437 | int btrfs_update_ref(struct btrfs_trans_handle *trans, | 2352 | int btrfs_inc_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root, |
1438 | struct btrfs_root *root, struct extent_buffer *orig_buf, | 2353 | struct extent_buffer *buf, int full_backref) |
1439 | struct extent_buffer *buf, int start_slot, int nr) | ||
1440 | |||
1441 | { | 2354 | { |
1442 | u64 bytenr; | 2355 | return __btrfs_mod_ref(trans, root, buf, full_backref, 1); |
1443 | u64 ref_root; | 2356 | } |
1444 | u64 orig_root; | ||
1445 | u64 ref_generation; | ||
1446 | u64 orig_generation; | ||
1447 | struct btrfs_key key; | ||
1448 | struct btrfs_file_extent_item *fi; | ||
1449 | int i; | ||
1450 | int ret; | ||
1451 | int slot; | ||
1452 | int level; | ||
1453 | |||
1454 | BUG_ON(start_slot < 0); | ||
1455 | BUG_ON(start_slot + nr > btrfs_header_nritems(buf)); | ||
1456 | |||
1457 | ref_root = btrfs_header_owner(buf); | ||
1458 | ref_generation = btrfs_header_generation(buf); | ||
1459 | orig_root = btrfs_header_owner(orig_buf); | ||
1460 | orig_generation = btrfs_header_generation(orig_buf); | ||
1461 | level = btrfs_header_level(buf); | ||
1462 | |||
1463 | if (!root->ref_cows) { | ||
1464 | if (level == 0 && | ||
1465 | root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID) | ||
1466 | return 0; | ||
1467 | if (level != 0 && | ||
1468 | root->root_key.objectid == BTRFS_TREE_LOG_OBJECTID) | ||
1469 | return 0; | ||
1470 | } | ||
1471 | 2357 | ||
1472 | for (i = 0, slot = start_slot; i < nr; i++, slot++) { | 2358 | int btrfs_dec_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root, |
1473 | cond_resched(); | 2359 | struct extent_buffer *buf, int full_backref) |
1474 | if (level == 0) { | 2360 | { |
1475 | btrfs_item_key_to_cpu(buf, &key, slot); | 2361 | return __btrfs_mod_ref(trans, root, buf, full_backref, 0); |
1476 | if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY) | ||
1477 | continue; | ||
1478 | fi = btrfs_item_ptr(buf, slot, | ||
1479 | struct btrfs_file_extent_item); | ||
1480 | if (btrfs_file_extent_type(buf, fi) == | ||
1481 | BTRFS_FILE_EXTENT_INLINE) | ||
1482 | continue; | ||
1483 | bytenr = btrfs_file_extent_disk_bytenr(buf, fi); | ||
1484 | if (bytenr == 0) | ||
1485 | continue; | ||
1486 | ret = __btrfs_update_extent_ref(trans, root, bytenr, | ||
1487 | btrfs_file_extent_disk_num_bytes(buf, fi), | ||
1488 | orig_buf->start, buf->start, | ||
1489 | orig_root, ref_root, orig_generation, | ||
1490 | ref_generation, key.objectid); | ||
1491 | if (ret) | ||
1492 | goto fail; | ||
1493 | } else { | ||
1494 | bytenr = btrfs_node_blockptr(buf, slot); | ||
1495 | ret = __btrfs_update_extent_ref(trans, root, bytenr, | ||
1496 | buf->len, orig_buf->start, | ||
1497 | buf->start, orig_root, ref_root, | ||
1498 | orig_generation, ref_generation, | ||
1499 | level - 1); | ||
1500 | if (ret) | ||
1501 | goto fail; | ||
1502 | } | ||
1503 | } | ||
1504 | return 0; | ||
1505 | fail: | ||
1506 | WARN_ON(1); | ||
1507 | return -1; | ||
1508 | } | 2362 | } |
1509 | 2363 | ||
1510 | static int write_one_cache_group(struct btrfs_trans_handle *trans, | 2364 | static int write_one_cache_group(struct btrfs_trans_handle *trans, |
@@ -2007,6 +2861,24 @@ static int update_block_group(struct btrfs_trans_handle *trans, | |||
2007 | u64 old_val; | 2861 | u64 old_val; |
2008 | u64 byte_in_group; | 2862 | u64 byte_in_group; |
2009 | 2863 | ||
2864 | /* block accounting for super block */ | ||
2865 | spin_lock(&info->delalloc_lock); | ||
2866 | old_val = btrfs_super_bytes_used(&info->super_copy); | ||
2867 | if (alloc) | ||
2868 | old_val += num_bytes; | ||
2869 | else | ||
2870 | old_val -= num_bytes; | ||
2871 | btrfs_set_super_bytes_used(&info->super_copy, old_val); | ||
2872 | |||
2873 | /* block accounting for root item */ | ||
2874 | old_val = btrfs_root_used(&root->root_item); | ||
2875 | if (alloc) | ||
2876 | old_val += num_bytes; | ||
2877 | else | ||
2878 | old_val -= num_bytes; | ||
2879 | btrfs_set_root_used(&root->root_item, old_val); | ||
2880 | spin_unlock(&info->delalloc_lock); | ||
2881 | |||
2010 | while (total) { | 2882 | while (total) { |
2011 | cache = btrfs_lookup_block_group(info, bytenr); | 2883 | cache = btrfs_lookup_block_group(info, bytenr); |
2012 | if (!cache) | 2884 | if (!cache) |
@@ -2216,8 +3088,6 @@ static int pin_down_bytes(struct btrfs_trans_handle *trans, | |||
2216 | u64 header_owner = btrfs_header_owner(buf); | 3088 | u64 header_owner = btrfs_header_owner(buf); |
2217 | u64 header_transid = btrfs_header_generation(buf); | 3089 | u64 header_transid = btrfs_header_generation(buf); |
2218 | if (header_owner != BTRFS_TREE_LOG_OBJECTID && | 3090 | if (header_owner != BTRFS_TREE_LOG_OBJECTID && |
2219 | header_owner != BTRFS_TREE_RELOC_OBJECTID && | ||
2220 | header_owner != BTRFS_DATA_RELOC_TREE_OBJECTID && | ||
2221 | header_transid == trans->transid && | 3091 | header_transid == trans->transid && |
2222 | !btrfs_header_flag(buf, BTRFS_HEADER_FLAG_WRITTEN)) { | 3092 | !btrfs_header_flag(buf, BTRFS_HEADER_FLAG_WRITTEN)) { |
2223 | *must_clean = buf; | 3093 | *must_clean = buf; |
@@ -2235,63 +3105,77 @@ pinit: | |||
2235 | return 0; | 3105 | return 0; |
2236 | } | 3106 | } |
2237 | 3107 | ||
2238 | /* | 3108 | |
2239 | * remove an extent from the root, returns 0 on success | 3109 | static int __btrfs_free_extent(struct btrfs_trans_handle *trans, |
2240 | */ | 3110 | struct btrfs_root *root, |
2241 | static int __free_extent(struct btrfs_trans_handle *trans, | 3111 | u64 bytenr, u64 num_bytes, u64 parent, |
2242 | struct btrfs_root *root, | 3112 | u64 root_objectid, u64 owner_objectid, |
2243 | u64 bytenr, u64 num_bytes, u64 parent, | 3113 | u64 owner_offset, int refs_to_drop, |
2244 | u64 root_objectid, u64 ref_generation, | 3114 | struct btrfs_delayed_extent_op *extent_op) |
2245 | u64 owner_objectid, int pin, int mark_free, | ||
2246 | int refs_to_drop) | ||
2247 | { | 3115 | { |
2248 | struct btrfs_path *path; | ||
2249 | struct btrfs_key key; | 3116 | struct btrfs_key key; |
3117 | struct btrfs_path *path; | ||
2250 | struct btrfs_fs_info *info = root->fs_info; | 3118 | struct btrfs_fs_info *info = root->fs_info; |
2251 | struct btrfs_root *extent_root = info->extent_root; | 3119 | struct btrfs_root *extent_root = info->extent_root; |
2252 | struct extent_buffer *leaf; | 3120 | struct extent_buffer *leaf; |
3121 | struct btrfs_extent_item *ei; | ||
3122 | struct btrfs_extent_inline_ref *iref; | ||
2253 | int ret; | 3123 | int ret; |
3124 | int is_data; | ||
2254 | int extent_slot = 0; | 3125 | int extent_slot = 0; |
2255 | int found_extent = 0; | 3126 | int found_extent = 0; |
2256 | int num_to_del = 1; | 3127 | int num_to_del = 1; |
2257 | struct btrfs_extent_item *ei; | 3128 | u32 item_size; |
2258 | u32 refs; | 3129 | u64 refs; |
2259 | 3130 | ||
2260 | key.objectid = bytenr; | ||
2261 | btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY); | ||
2262 | key.offset = num_bytes; | ||
2263 | path = btrfs_alloc_path(); | 3131 | path = btrfs_alloc_path(); |
2264 | if (!path) | 3132 | if (!path) |
2265 | return -ENOMEM; | 3133 | return -ENOMEM; |
2266 | 3134 | ||
2267 | path->reada = 1; | 3135 | path->reada = 1; |
2268 | path->leave_spinning = 1; | 3136 | path->leave_spinning = 1; |
2269 | ret = lookup_extent_backref(trans, extent_root, path, | 3137 | |
2270 | bytenr, parent, root_objectid, | 3138 | is_data = owner_objectid >= BTRFS_FIRST_FREE_OBJECTID; |
2271 | ref_generation, owner_objectid, 1); | 3139 | BUG_ON(!is_data && refs_to_drop != 1); |
3140 | |||
3141 | ret = lookup_extent_backref(trans, extent_root, path, &iref, | ||
3142 | bytenr, num_bytes, parent, | ||
3143 | root_objectid, owner_objectid, | ||
3144 | owner_offset); | ||
2272 | if (ret == 0) { | 3145 | if (ret == 0) { |
2273 | struct btrfs_key found_key; | ||
2274 | extent_slot = path->slots[0]; | 3146 | extent_slot = path->slots[0]; |
2275 | while (extent_slot > 0) { | 3147 | while (extent_slot >= 0) { |
2276 | extent_slot--; | 3148 | btrfs_item_key_to_cpu(path->nodes[0], &key, |
2277 | btrfs_item_key_to_cpu(path->nodes[0], &found_key, | ||
2278 | extent_slot); | 3149 | extent_slot); |
2279 | if (found_key.objectid != bytenr) | 3150 | if (key.objectid != bytenr) |
2280 | break; | 3151 | break; |
2281 | if (found_key.type == BTRFS_EXTENT_ITEM_KEY && | 3152 | if (key.type == BTRFS_EXTENT_ITEM_KEY && |
2282 | found_key.offset == num_bytes) { | 3153 | key.offset == num_bytes) { |
2283 | found_extent = 1; | 3154 | found_extent = 1; |
2284 | break; | 3155 | break; |
2285 | } | 3156 | } |
2286 | if (path->slots[0] - extent_slot > 5) | 3157 | if (path->slots[0] - extent_slot > 5) |
2287 | break; | 3158 | break; |
3159 | extent_slot--; | ||
2288 | } | 3160 | } |
3161 | #ifdef BTRFS_COMPAT_EXTENT_TREE_V0 | ||
3162 | item_size = btrfs_item_size_nr(path->nodes[0], extent_slot); | ||
3163 | if (found_extent && item_size < sizeof(*ei)) | ||
3164 | found_extent = 0; | ||
3165 | #endif | ||
2289 | if (!found_extent) { | 3166 | if (!found_extent) { |
3167 | BUG_ON(iref); | ||
2290 | ret = remove_extent_backref(trans, extent_root, path, | 3168 | ret = remove_extent_backref(trans, extent_root, path, |
2291 | refs_to_drop); | 3169 | NULL, refs_to_drop, |
3170 | is_data); | ||
2292 | BUG_ON(ret); | 3171 | BUG_ON(ret); |
2293 | btrfs_release_path(extent_root, path); | 3172 | btrfs_release_path(extent_root, path); |
2294 | path->leave_spinning = 1; | 3173 | path->leave_spinning = 1; |
3174 | |||
3175 | key.objectid = bytenr; | ||
3176 | key.type = BTRFS_EXTENT_ITEM_KEY; | ||
3177 | key.offset = num_bytes; | ||
3178 | |||
2295 | ret = btrfs_search_slot(trans, extent_root, | 3179 | ret = btrfs_search_slot(trans, extent_root, |
2296 | &key, path, -1, 1); | 3180 | &key, path, -1, 1); |
2297 | if (ret) { | 3181 | if (ret) { |
@@ -2307,82 +3191,98 @@ static int __free_extent(struct btrfs_trans_handle *trans, | |||
2307 | btrfs_print_leaf(extent_root, path->nodes[0]); | 3191 | btrfs_print_leaf(extent_root, path->nodes[0]); |
2308 | WARN_ON(1); | 3192 | WARN_ON(1); |
2309 | printk(KERN_ERR "btrfs unable to find ref byte nr %llu " | 3193 | printk(KERN_ERR "btrfs unable to find ref byte nr %llu " |
2310 | "parent %llu root %llu gen %llu owner %llu\n", | 3194 | "parent %llu root %llu owner %llu offset %llu\n", |
2311 | (unsigned long long)bytenr, | 3195 | (unsigned long long)bytenr, |
2312 | (unsigned long long)parent, | 3196 | (unsigned long long)parent, |
2313 | (unsigned long long)root_objectid, | 3197 | (unsigned long long)root_objectid, |
2314 | (unsigned long long)ref_generation, | 3198 | (unsigned long long)owner_objectid, |
2315 | (unsigned long long)owner_objectid); | 3199 | (unsigned long long)owner_offset); |
2316 | } | 3200 | } |
2317 | 3201 | ||
2318 | leaf = path->nodes[0]; | 3202 | leaf = path->nodes[0]; |
3203 | item_size = btrfs_item_size_nr(leaf, extent_slot); | ||
3204 | #ifdef BTRFS_COMPAT_EXTENT_TREE_V0 | ||
3205 | if (item_size < sizeof(*ei)) { | ||
3206 | BUG_ON(found_extent || extent_slot != path->slots[0]); | ||
3207 | ret = convert_extent_item_v0(trans, extent_root, path, | ||
3208 | owner_objectid, 0); | ||
3209 | BUG_ON(ret < 0); | ||
3210 | |||
3211 | btrfs_release_path(extent_root, path); | ||
3212 | path->leave_spinning = 1; | ||
3213 | |||
3214 | key.objectid = bytenr; | ||
3215 | key.type = BTRFS_EXTENT_ITEM_KEY; | ||
3216 | key.offset = num_bytes; | ||
3217 | |||
3218 | ret = btrfs_search_slot(trans, extent_root, &key, path, | ||
3219 | -1, 1); | ||
3220 | if (ret) { | ||
3221 | printk(KERN_ERR "umm, got %d back from search" | ||
3222 | ", was looking for %llu\n", ret, | ||
3223 | (unsigned long long)bytenr); | ||
3224 | btrfs_print_leaf(extent_root, path->nodes[0]); | ||
3225 | } | ||
3226 | BUG_ON(ret); | ||
3227 | extent_slot = path->slots[0]; | ||
3228 | leaf = path->nodes[0]; | ||
3229 | item_size = btrfs_item_size_nr(leaf, extent_slot); | ||
3230 | } | ||
3231 | #endif | ||
3232 | BUG_ON(item_size < sizeof(*ei)); | ||
2319 | ei = btrfs_item_ptr(leaf, extent_slot, | 3233 | ei = btrfs_item_ptr(leaf, extent_slot, |
2320 | struct btrfs_extent_item); | 3234 | struct btrfs_extent_item); |
2321 | refs = btrfs_extent_refs(leaf, ei); | 3235 | if (owner_objectid < BTRFS_FIRST_FREE_OBJECTID) { |
2322 | 3236 | struct btrfs_tree_block_info *bi; | |
2323 | /* | 3237 | BUG_ON(item_size < sizeof(*ei) + sizeof(*bi)); |
2324 | * we're not allowed to delete the extent item if there | 3238 | bi = (struct btrfs_tree_block_info *)(ei + 1); |
2325 | * are other delayed ref updates pending | 3239 | WARN_ON(owner_objectid != btrfs_tree_block_level(leaf, bi)); |
2326 | */ | 3240 | } |
2327 | 3241 | ||
3242 | refs = btrfs_extent_refs(leaf, ei); | ||
2328 | BUG_ON(refs < refs_to_drop); | 3243 | BUG_ON(refs < refs_to_drop); |
2329 | refs -= refs_to_drop; | 3244 | refs -= refs_to_drop; |
2330 | btrfs_set_extent_refs(leaf, ei, refs); | ||
2331 | btrfs_mark_buffer_dirty(leaf); | ||
2332 | 3245 | ||
2333 | if (refs == 0 && found_extent && | 3246 | if (refs > 0) { |
2334 | path->slots[0] == extent_slot + 1) { | 3247 | if (extent_op) |
2335 | struct btrfs_extent_ref *ref; | 3248 | __run_delayed_extent_op(extent_op, leaf, ei); |
2336 | ref = btrfs_item_ptr(leaf, path->slots[0], | 3249 | /* |
2337 | struct btrfs_extent_ref); | 3250 | * In the case of inline back ref, reference count will |
2338 | BUG_ON(btrfs_ref_num_refs(leaf, ref) != refs_to_drop); | 3251 | * be updated by remove_extent_backref |
2339 | /* if the back ref and the extent are next to each other | ||
2340 | * they get deleted below in one shot | ||
2341 | */ | 3252 | */ |
2342 | path->slots[0] = extent_slot; | 3253 | if (iref) { |
2343 | num_to_del = 2; | 3254 | BUG_ON(!found_extent); |
2344 | } else if (found_extent) { | 3255 | } else { |
2345 | /* otherwise delete the extent back ref */ | 3256 | btrfs_set_extent_refs(leaf, ei, refs); |
2346 | ret = remove_extent_backref(trans, extent_root, path, | 3257 | btrfs_mark_buffer_dirty(leaf); |
2347 | refs_to_drop); | 3258 | } |
2348 | BUG_ON(ret); | 3259 | if (found_extent) { |
2349 | /* if refs are 0, we need to setup the path for deletion */ | 3260 | ret = remove_extent_backref(trans, extent_root, path, |
2350 | if (refs == 0) { | 3261 | iref, refs_to_drop, |
2351 | btrfs_release_path(extent_root, path); | 3262 | is_data); |
2352 | path->leave_spinning = 1; | ||
2353 | ret = btrfs_search_slot(trans, extent_root, &key, path, | ||
2354 | -1, 1); | ||
2355 | BUG_ON(ret); | 3263 | BUG_ON(ret); |
2356 | } | 3264 | } |
2357 | } | 3265 | } else { |
2358 | 3266 | int mark_free = 0; | |
2359 | if (refs == 0) { | ||
2360 | u64 super_used; | ||
2361 | u64 root_used; | ||
2362 | struct extent_buffer *must_clean = NULL; | 3267 | struct extent_buffer *must_clean = NULL; |
2363 | 3268 | ||
2364 | if (pin) { | 3269 | if (found_extent) { |
2365 | ret = pin_down_bytes(trans, root, path, | 3270 | BUG_ON(is_data && refs_to_drop != |
2366 | bytenr, num_bytes, | 3271 | extent_data_ref_count(root, path, iref)); |
2367 | owner_objectid >= BTRFS_FIRST_FREE_OBJECTID, | 3272 | if (iref) { |
2368 | &must_clean); | 3273 | BUG_ON(path->slots[0] != extent_slot); |
2369 | if (ret > 0) | 3274 | } else { |
2370 | mark_free = 1; | 3275 | BUG_ON(path->slots[0] != extent_slot + 1); |
2371 | BUG_ON(ret < 0); | 3276 | path->slots[0] = extent_slot; |
3277 | num_to_del = 2; | ||
3278 | } | ||
2372 | } | 3279 | } |
2373 | 3280 | ||
2374 | /* block accounting for super block */ | 3281 | ret = pin_down_bytes(trans, root, path, bytenr, |
2375 | spin_lock(&info->delalloc_lock); | 3282 | num_bytes, is_data, &must_clean); |
2376 | super_used = btrfs_super_bytes_used(&info->super_copy); | 3283 | if (ret > 0) |
2377 | btrfs_set_super_bytes_used(&info->super_copy, | 3284 | mark_free = 1; |
2378 | super_used - num_bytes); | 3285 | BUG_ON(ret < 0); |
2379 | |||
2380 | /* block accounting for root item */ | ||
2381 | root_used = btrfs_root_used(&root->root_item); | ||
2382 | btrfs_set_root_used(&root->root_item, | ||
2383 | root_used - num_bytes); | ||
2384 | spin_unlock(&info->delalloc_lock); | ||
2385 | |||
2386 | /* | 3286 | /* |
2387 | * it is going to be very rare for someone to be waiting | 3287 | * it is going to be very rare for someone to be waiting |
2388 | * on the block we're freeing. del_items might need to | 3288 | * on the block we're freeing. del_items might need to |
@@ -2403,7 +3303,7 @@ static int __free_extent(struct btrfs_trans_handle *trans, | |||
2403 | free_extent_buffer(must_clean); | 3303 | free_extent_buffer(must_clean); |
2404 | } | 3304 | } |
2405 | 3305 | ||
2406 | if (owner_objectid >= BTRFS_FIRST_FREE_OBJECTID) { | 3306 | if (is_data) { |
2407 | ret = btrfs_del_csums(trans, root, bytenr, num_bytes); | 3307 | ret = btrfs_del_csums(trans, root, bytenr, num_bytes); |
2408 | BUG_ON(ret); | 3308 | BUG_ON(ret); |
2409 | } else { | 3309 | } else { |
@@ -2421,34 +3321,6 @@ static int __free_extent(struct btrfs_trans_handle *trans, | |||
2421 | } | 3321 | } |
2422 | 3322 | ||
2423 | /* | 3323 | /* |
2424 | * remove an extent from the root, returns 0 on success | ||
2425 | */ | ||
2426 | static int __btrfs_free_extent(struct btrfs_trans_handle *trans, | ||
2427 | struct btrfs_root *root, | ||
2428 | u64 bytenr, u64 num_bytes, u64 parent, | ||
2429 | u64 root_objectid, u64 ref_generation, | ||
2430 | u64 owner_objectid, int pin, | ||
2431 | int refs_to_drop) | ||
2432 | { | ||
2433 | WARN_ON(num_bytes < root->sectorsize); | ||
2434 | |||
2435 | /* | ||
2436 | * if metadata always pin | ||
2437 | * if data pin when any transaction has committed this | ||
2438 | */ | ||
2439 | if (owner_objectid < BTRFS_FIRST_FREE_OBJECTID || | ||
2440 | ref_generation != trans->transid) | ||
2441 | pin = 1; | ||
2442 | |||
2443 | if (ref_generation != trans->transid) | ||
2444 | pin = 1; | ||
2445 | |||
2446 | return __free_extent(trans, root, bytenr, num_bytes, parent, | ||
2447 | root_objectid, ref_generation, | ||
2448 | owner_objectid, pin, pin == 0, refs_to_drop); | ||
2449 | } | ||
2450 | |||
2451 | /* | ||
2452 | * when we free an extent, it is possible (and likely) that we free the last | 3324 | * when we free an extent, it is possible (and likely) that we free the last |
2453 | * delayed ref for that extent as well. This searches the delayed ref tree for | 3325 | * delayed ref for that extent as well. This searches the delayed ref tree for |
2454 | * a given extent, and if there are no other delayed refs to be processed, it | 3326 | * a given extent, and if there are no other delayed refs to be processed, it |
@@ -2479,6 +3351,13 @@ static noinline int check_ref_cleanup(struct btrfs_trans_handle *trans, | |||
2479 | if (ref->bytenr == bytenr) | 3351 | if (ref->bytenr == bytenr) |
2480 | goto out; | 3352 | goto out; |
2481 | 3353 | ||
3354 | if (head->extent_op) { | ||
3355 | if (!head->must_insert_reserved) | ||
3356 | goto out; | ||
3357 | kfree(head->extent_op); | ||
3358 | head->extent_op = NULL; | ||
3359 | } | ||
3360 | |||
2482 | /* | 3361 | /* |
2483 | * waiting for the lock here would deadlock. If someone else has it | 3362 | * waiting for the lock here would deadlock. If someone else has it |
2484 | * locked they are already in the process of dropping it anyway | 3363 | * locked they are already in the process of dropping it anyway |
@@ -2507,7 +3386,8 @@ static noinline int check_ref_cleanup(struct btrfs_trans_handle *trans, | |||
2507 | spin_unlock(&delayed_refs->lock); | 3386 | spin_unlock(&delayed_refs->lock); |
2508 | 3387 | ||
2509 | ret = run_one_delayed_ref(trans, root->fs_info->tree_root, | 3388 | ret = run_one_delayed_ref(trans, root->fs_info->tree_root, |
2510 | &head->node, head->must_insert_reserved); | 3389 | &head->node, head->extent_op, |
3390 | head->must_insert_reserved); | ||
2511 | BUG_ON(ret); | 3391 | BUG_ON(ret); |
2512 | btrfs_put_delayed_ref(&head->node); | 3392 | btrfs_put_delayed_ref(&head->node); |
2513 | return 0; | 3393 | return 0; |
@@ -2519,32 +3399,32 @@ out: | |||
2519 | int btrfs_free_extent(struct btrfs_trans_handle *trans, | 3399 | int btrfs_free_extent(struct btrfs_trans_handle *trans, |
2520 | struct btrfs_root *root, | 3400 | struct btrfs_root *root, |
2521 | u64 bytenr, u64 num_bytes, u64 parent, | 3401 | u64 bytenr, u64 num_bytes, u64 parent, |
2522 | u64 root_objectid, u64 ref_generation, | 3402 | u64 root_objectid, u64 owner, u64 offset) |
2523 | u64 owner_objectid, int pin) | ||
2524 | { | 3403 | { |
2525 | int ret; | 3404 | int ret; |
2526 | 3405 | ||
2527 | /* | 3406 | /* |
2528 | * tree log blocks never actually go into the extent allocation | 3407 | * tree log blocks never actually go into the extent allocation |
2529 | * tree, just update pinning info and exit early. | 3408 | * tree, just update pinning info and exit early. |
2530 | * | ||
2531 | * data extents referenced by the tree log do need to have | ||
2532 | * their reference counts bumped. | ||
2533 | */ | 3409 | */ |
2534 | if (root->root_key.objectid == BTRFS_TREE_LOG_OBJECTID && | 3410 | if (root_objectid == BTRFS_TREE_LOG_OBJECTID) { |
2535 | owner_objectid < BTRFS_FIRST_FREE_OBJECTID) { | 3411 | WARN_ON(owner >= BTRFS_FIRST_FREE_OBJECTID); |
2536 | /* unlocks the pinned mutex */ | 3412 | /* unlocks the pinned mutex */ |
2537 | btrfs_update_pinned_extents(root, bytenr, num_bytes, 1); | 3413 | btrfs_update_pinned_extents(root, bytenr, num_bytes, 1); |
2538 | update_reserved_extents(root, bytenr, num_bytes, 0); | 3414 | update_reserved_extents(root, bytenr, num_bytes, 0); |
2539 | ret = 0; | 3415 | ret = 0; |
2540 | } else { | 3416 | } else if (owner < BTRFS_FIRST_FREE_OBJECTID) { |
2541 | ret = btrfs_add_delayed_ref(trans, bytenr, num_bytes, parent, | 3417 | ret = btrfs_add_delayed_tree_ref(trans, bytenr, num_bytes, |
2542 | root_objectid, ref_generation, | 3418 | parent, root_objectid, (int)owner, |
2543 | owner_objectid, | 3419 | BTRFS_DROP_DELAYED_REF, NULL); |
2544 | BTRFS_DROP_DELAYED_REF, 1); | ||
2545 | BUG_ON(ret); | 3420 | BUG_ON(ret); |
2546 | ret = check_ref_cleanup(trans, root, bytenr); | 3421 | ret = check_ref_cleanup(trans, root, bytenr); |
2547 | BUG_ON(ret); | 3422 | BUG_ON(ret); |
3423 | } else { | ||
3424 | ret = btrfs_add_delayed_data_ref(trans, bytenr, num_bytes, | ||
3425 | parent, root_objectid, owner, | ||
3426 | offset, BTRFS_DROP_DELAYED_REF, NULL); | ||
3427 | BUG_ON(ret); | ||
2548 | } | 3428 | } |
2549 | return ret; | 3429 | return ret; |
2550 | } | 3430 | } |
@@ -2719,7 +3599,7 @@ refill_cluster: | |||
2719 | last_ptr_loop = 0; | 3599 | last_ptr_loop = 0; |
2720 | 3600 | ||
2721 | /* allocate a cluster in this block group */ | 3601 | /* allocate a cluster in this block group */ |
2722 | ret = btrfs_find_space_cluster(trans, | 3602 | ret = btrfs_find_space_cluster(trans, root, |
2723 | block_group, last_ptr, | 3603 | block_group, last_ptr, |
2724 | offset, num_bytes, | 3604 | offset, num_bytes, |
2725 | empty_cluster + empty_size); | 3605 | empty_cluster + empty_size); |
@@ -2969,99 +3849,147 @@ int btrfs_reserve_extent(struct btrfs_trans_handle *trans, | |||
2969 | return ret; | 3849 | return ret; |
2970 | } | 3850 | } |
2971 | 3851 | ||
2972 | static int __btrfs_alloc_reserved_extent(struct btrfs_trans_handle *trans, | 3852 | static int alloc_reserved_file_extent(struct btrfs_trans_handle *trans, |
2973 | struct btrfs_root *root, u64 parent, | 3853 | struct btrfs_root *root, |
2974 | u64 root_objectid, u64 ref_generation, | 3854 | u64 parent, u64 root_objectid, |
2975 | u64 owner, struct btrfs_key *ins, | 3855 | u64 flags, u64 owner, u64 offset, |
2976 | int ref_mod) | 3856 | struct btrfs_key *ins, int ref_mod) |
2977 | { | 3857 | { |
2978 | int ret; | 3858 | int ret; |
2979 | u64 super_used; | 3859 | struct btrfs_fs_info *fs_info = root->fs_info; |
2980 | u64 root_used; | ||
2981 | u64 num_bytes = ins->offset; | ||
2982 | u32 sizes[2]; | ||
2983 | struct btrfs_fs_info *info = root->fs_info; | ||
2984 | struct btrfs_root *extent_root = info->extent_root; | ||
2985 | struct btrfs_extent_item *extent_item; | 3860 | struct btrfs_extent_item *extent_item; |
2986 | struct btrfs_extent_ref *ref; | 3861 | struct btrfs_extent_inline_ref *iref; |
2987 | struct btrfs_path *path; | 3862 | struct btrfs_path *path; |
2988 | struct btrfs_key keys[2]; | 3863 | struct extent_buffer *leaf; |
2989 | 3864 | int type; | |
2990 | if (parent == 0) | 3865 | u32 size; |
2991 | parent = ins->objectid; | ||
2992 | |||
2993 | /* block accounting for super block */ | ||
2994 | spin_lock(&info->delalloc_lock); | ||
2995 | super_used = btrfs_super_bytes_used(&info->super_copy); | ||
2996 | btrfs_set_super_bytes_used(&info->super_copy, super_used + num_bytes); | ||
2997 | 3866 | ||
2998 | /* block accounting for root item */ | 3867 | if (parent > 0) |
2999 | root_used = btrfs_root_used(&root->root_item); | 3868 | type = BTRFS_SHARED_DATA_REF_KEY; |
3000 | btrfs_set_root_used(&root->root_item, root_used + num_bytes); | 3869 | else |
3001 | spin_unlock(&info->delalloc_lock); | 3870 | type = BTRFS_EXTENT_DATA_REF_KEY; |
3002 | 3871 | ||
3003 | memcpy(&keys[0], ins, sizeof(*ins)); | 3872 | size = sizeof(*extent_item) + btrfs_extent_inline_ref_size(type); |
3004 | keys[1].objectid = ins->objectid; | ||
3005 | keys[1].type = BTRFS_EXTENT_REF_KEY; | ||
3006 | keys[1].offset = parent; | ||
3007 | sizes[0] = sizeof(*extent_item); | ||
3008 | sizes[1] = sizeof(*ref); | ||
3009 | 3873 | ||
3010 | path = btrfs_alloc_path(); | 3874 | path = btrfs_alloc_path(); |
3011 | BUG_ON(!path); | 3875 | BUG_ON(!path); |
3012 | 3876 | ||
3013 | path->leave_spinning = 1; | 3877 | path->leave_spinning = 1; |
3014 | ret = btrfs_insert_empty_items(trans, extent_root, path, keys, | 3878 | ret = btrfs_insert_empty_item(trans, fs_info->extent_root, path, |
3015 | sizes, 2); | 3879 | ins, size); |
3016 | BUG_ON(ret); | 3880 | BUG_ON(ret); |
3017 | 3881 | ||
3018 | extent_item = btrfs_item_ptr(path->nodes[0], path->slots[0], | 3882 | leaf = path->nodes[0]; |
3883 | extent_item = btrfs_item_ptr(leaf, path->slots[0], | ||
3019 | struct btrfs_extent_item); | 3884 | struct btrfs_extent_item); |
3020 | btrfs_set_extent_refs(path->nodes[0], extent_item, ref_mod); | 3885 | btrfs_set_extent_refs(leaf, extent_item, ref_mod); |
3021 | ref = btrfs_item_ptr(path->nodes[0], path->slots[0] + 1, | 3886 | btrfs_set_extent_generation(leaf, extent_item, trans->transid); |
3022 | struct btrfs_extent_ref); | 3887 | btrfs_set_extent_flags(leaf, extent_item, |
3023 | 3888 | flags | BTRFS_EXTENT_FLAG_DATA); | |
3024 | btrfs_set_ref_root(path->nodes[0], ref, root_objectid); | 3889 | |
3025 | btrfs_set_ref_generation(path->nodes[0], ref, ref_generation); | 3890 | iref = (struct btrfs_extent_inline_ref *)(extent_item + 1); |
3026 | btrfs_set_ref_objectid(path->nodes[0], ref, owner); | 3891 | btrfs_set_extent_inline_ref_type(leaf, iref, type); |
3027 | btrfs_set_ref_num_refs(path->nodes[0], ref, ref_mod); | 3892 | if (parent > 0) { |
3893 | struct btrfs_shared_data_ref *ref; | ||
3894 | ref = (struct btrfs_shared_data_ref *)(iref + 1); | ||
3895 | btrfs_set_extent_inline_ref_offset(leaf, iref, parent); | ||
3896 | btrfs_set_shared_data_ref_count(leaf, ref, ref_mod); | ||
3897 | } else { | ||
3898 | struct btrfs_extent_data_ref *ref; | ||
3899 | ref = (struct btrfs_extent_data_ref *)(&iref->offset); | ||
3900 | btrfs_set_extent_data_ref_root(leaf, ref, root_objectid); | ||
3901 | btrfs_set_extent_data_ref_objectid(leaf, ref, owner); | ||
3902 | btrfs_set_extent_data_ref_offset(leaf, ref, offset); | ||
3903 | btrfs_set_extent_data_ref_count(leaf, ref, ref_mod); | ||
3904 | } | ||
3028 | 3905 | ||
3029 | btrfs_mark_buffer_dirty(path->nodes[0]); | 3906 | btrfs_mark_buffer_dirty(path->nodes[0]); |
3030 | |||
3031 | trans->alloc_exclude_start = 0; | ||
3032 | trans->alloc_exclude_nr = 0; | ||
3033 | btrfs_free_path(path); | 3907 | btrfs_free_path(path); |
3034 | 3908 | ||
3035 | if (ret) | 3909 | ret = update_block_group(trans, root, ins->objectid, ins->offset, |
3036 | goto out; | 3910 | 1, 0); |
3037 | |||
3038 | ret = update_block_group(trans, root, ins->objectid, | ||
3039 | ins->offset, 1, 0); | ||
3040 | if (ret) { | 3911 | if (ret) { |
3041 | printk(KERN_ERR "btrfs update block group failed for %llu " | 3912 | printk(KERN_ERR "btrfs update block group failed for %llu " |
3042 | "%llu\n", (unsigned long long)ins->objectid, | 3913 | "%llu\n", (unsigned long long)ins->objectid, |
3043 | (unsigned long long)ins->offset); | 3914 | (unsigned long long)ins->offset); |
3044 | BUG(); | 3915 | BUG(); |
3045 | } | 3916 | } |
3046 | out: | ||
3047 | return ret; | 3917 | return ret; |
3048 | } | 3918 | } |
3049 | 3919 | ||
3050 | int btrfs_alloc_reserved_extent(struct btrfs_trans_handle *trans, | 3920 | static int alloc_reserved_tree_block(struct btrfs_trans_handle *trans, |
3051 | struct btrfs_root *root, u64 parent, | 3921 | struct btrfs_root *root, |
3052 | u64 root_objectid, u64 ref_generation, | 3922 | u64 parent, u64 root_objectid, |
3053 | u64 owner, struct btrfs_key *ins) | 3923 | u64 flags, struct btrfs_disk_key *key, |
3924 | int level, struct btrfs_key *ins) | ||
3054 | { | 3925 | { |
3055 | int ret; | 3926 | int ret; |
3927 | struct btrfs_fs_info *fs_info = root->fs_info; | ||
3928 | struct btrfs_extent_item *extent_item; | ||
3929 | struct btrfs_tree_block_info *block_info; | ||
3930 | struct btrfs_extent_inline_ref *iref; | ||
3931 | struct btrfs_path *path; | ||
3932 | struct extent_buffer *leaf; | ||
3933 | u32 size = sizeof(*extent_item) + sizeof(*block_info) + sizeof(*iref); | ||
3056 | 3934 | ||
3057 | if (root_objectid == BTRFS_TREE_LOG_OBJECTID) | 3935 | path = btrfs_alloc_path(); |
3058 | return 0; | 3936 | BUG_ON(!path); |
3059 | 3937 | ||
3060 | ret = btrfs_add_delayed_ref(trans, ins->objectid, | 3938 | path->leave_spinning = 1; |
3061 | ins->offset, parent, root_objectid, | 3939 | ret = btrfs_insert_empty_item(trans, fs_info->extent_root, path, |
3062 | ref_generation, owner, | 3940 | ins, size); |
3063 | BTRFS_ADD_DELAYED_EXTENT, 0); | ||
3064 | BUG_ON(ret); | 3941 | BUG_ON(ret); |
3942 | |||
3943 | leaf = path->nodes[0]; | ||
3944 | extent_item = btrfs_item_ptr(leaf, path->slots[0], | ||
3945 | struct btrfs_extent_item); | ||
3946 | btrfs_set_extent_refs(leaf, extent_item, 1); | ||
3947 | btrfs_set_extent_generation(leaf, extent_item, trans->transid); | ||
3948 | btrfs_set_extent_flags(leaf, extent_item, | ||
3949 | flags | BTRFS_EXTENT_FLAG_TREE_BLOCK); | ||
3950 | block_info = (struct btrfs_tree_block_info *)(extent_item + 1); | ||
3951 | |||
3952 | btrfs_set_tree_block_key(leaf, block_info, key); | ||
3953 | btrfs_set_tree_block_level(leaf, block_info, level); | ||
3954 | |||
3955 | iref = (struct btrfs_extent_inline_ref *)(block_info + 1); | ||
3956 | if (parent > 0) { | ||
3957 | BUG_ON(!(flags & BTRFS_BLOCK_FLAG_FULL_BACKREF)); | ||
3958 | btrfs_set_extent_inline_ref_type(leaf, iref, | ||
3959 | BTRFS_SHARED_BLOCK_REF_KEY); | ||
3960 | btrfs_set_extent_inline_ref_offset(leaf, iref, parent); | ||
3961 | } else { | ||
3962 | btrfs_set_extent_inline_ref_type(leaf, iref, | ||
3963 | BTRFS_TREE_BLOCK_REF_KEY); | ||
3964 | btrfs_set_extent_inline_ref_offset(leaf, iref, root_objectid); | ||
3965 | } | ||
3966 | |||
3967 | btrfs_mark_buffer_dirty(leaf); | ||
3968 | btrfs_free_path(path); | ||
3969 | |||
3970 | ret = update_block_group(trans, root, ins->objectid, ins->offset, | ||
3971 | 1, 0); | ||
3972 | if (ret) { | ||
3973 | printk(KERN_ERR "btrfs update block group failed for %llu " | ||
3974 | "%llu\n", (unsigned long long)ins->objectid, | ||
3975 | (unsigned long long)ins->offset); | ||
3976 | BUG(); | ||
3977 | } | ||
3978 | return ret; | ||
3979 | } | ||
3980 | |||
3981 | int btrfs_alloc_reserved_file_extent(struct btrfs_trans_handle *trans, | ||
3982 | struct btrfs_root *root, | ||
3983 | u64 root_objectid, u64 owner, | ||
3984 | u64 offset, struct btrfs_key *ins) | ||
3985 | { | ||
3986 | int ret; | ||
3987 | |||
3988 | BUG_ON(root_objectid == BTRFS_TREE_LOG_OBJECTID); | ||
3989 | |||
3990 | ret = btrfs_add_delayed_data_ref(trans, ins->objectid, ins->offset, | ||
3991 | 0, root_objectid, owner, offset, | ||
3992 | BTRFS_ADD_DELAYED_EXTENT, NULL); | ||
3065 | return ret; | 3993 | return ret; |
3066 | } | 3994 | } |
3067 | 3995 | ||
@@ -3070,10 +3998,10 @@ int btrfs_alloc_reserved_extent(struct btrfs_trans_handle *trans, | |||
3070 | * an extent has been allocated and makes sure to clear the free | 3998 | * an extent has been allocated and makes sure to clear the free |
3071 | * space cache bits as well | 3999 | * space cache bits as well |
3072 | */ | 4000 | */ |
3073 | int btrfs_alloc_logged_extent(struct btrfs_trans_handle *trans, | 4001 | int btrfs_alloc_logged_file_extent(struct btrfs_trans_handle *trans, |
3074 | struct btrfs_root *root, u64 parent, | 4002 | struct btrfs_root *root, |
3075 | u64 root_objectid, u64 ref_generation, | 4003 | u64 root_objectid, u64 owner, u64 offset, |
3076 | u64 owner, struct btrfs_key *ins) | 4004 | struct btrfs_key *ins) |
3077 | { | 4005 | { |
3078 | int ret; | 4006 | int ret; |
3079 | struct btrfs_block_group_cache *block_group; | 4007 | struct btrfs_block_group_cache *block_group; |
@@ -3087,8 +4015,8 @@ int btrfs_alloc_logged_extent(struct btrfs_trans_handle *trans, | |||
3087 | ins->offset); | 4015 | ins->offset); |
3088 | BUG_ON(ret); | 4016 | BUG_ON(ret); |
3089 | btrfs_put_block_group(block_group); | 4017 | btrfs_put_block_group(block_group); |
3090 | ret = __btrfs_alloc_reserved_extent(trans, root, parent, root_objectid, | 4018 | ret = alloc_reserved_file_extent(trans, root, 0, root_objectid, |
3091 | ref_generation, owner, ins, 1); | 4019 | 0, owner, offset, ins, 1); |
3092 | return ret; | 4020 | return ret; |
3093 | } | 4021 | } |
3094 | 4022 | ||
@@ -3099,26 +4027,48 @@ int btrfs_alloc_logged_extent(struct btrfs_trans_handle *trans, | |||
3099 | * | 4027 | * |
3100 | * returns 0 if everything worked, non-zero otherwise. | 4028 | * returns 0 if everything worked, non-zero otherwise. |
3101 | */ | 4029 | */ |
3102 | int btrfs_alloc_extent(struct btrfs_trans_handle *trans, | 4030 | static int alloc_tree_block(struct btrfs_trans_handle *trans, |
3103 | struct btrfs_root *root, | 4031 | struct btrfs_root *root, |
3104 | u64 num_bytes, u64 parent, u64 min_alloc_size, | 4032 | u64 num_bytes, u64 parent, u64 root_objectid, |
3105 | u64 root_objectid, u64 ref_generation, | 4033 | struct btrfs_disk_key *key, int level, |
3106 | u64 owner_objectid, u64 empty_size, u64 hint_byte, | 4034 | u64 empty_size, u64 hint_byte, u64 search_end, |
3107 | u64 search_end, struct btrfs_key *ins, u64 data) | 4035 | struct btrfs_key *ins) |
3108 | { | 4036 | { |
3109 | int ret; | 4037 | int ret; |
3110 | ret = __btrfs_reserve_extent(trans, root, num_bytes, | 4038 | u64 flags = 0; |
3111 | min_alloc_size, empty_size, hint_byte, | 4039 | |
3112 | search_end, ins, data); | 4040 | ret = __btrfs_reserve_extent(trans, root, num_bytes, num_bytes, |
4041 | empty_size, hint_byte, search_end, | ||
4042 | ins, 0); | ||
3113 | BUG_ON(ret); | 4043 | BUG_ON(ret); |
4044 | |||
4045 | if (root_objectid == BTRFS_TREE_RELOC_OBJECTID) { | ||
4046 | if (parent == 0) | ||
4047 | parent = ins->objectid; | ||
4048 | flags |= BTRFS_BLOCK_FLAG_FULL_BACKREF; | ||
4049 | } else | ||
4050 | BUG_ON(parent > 0); | ||
4051 | |||
4052 | update_reserved_extents(root, ins->objectid, ins->offset, 1); | ||
3114 | if (root_objectid != BTRFS_TREE_LOG_OBJECTID) { | 4053 | if (root_objectid != BTRFS_TREE_LOG_OBJECTID) { |
3115 | ret = btrfs_add_delayed_ref(trans, ins->objectid, | 4054 | struct btrfs_delayed_extent_op *extent_op; |
3116 | ins->offset, parent, root_objectid, | 4055 | extent_op = kmalloc(sizeof(*extent_op), GFP_NOFS); |
3117 | ref_generation, owner_objectid, | 4056 | BUG_ON(!extent_op); |
3118 | BTRFS_ADD_DELAYED_EXTENT, 0); | 4057 | if (key) |
4058 | memcpy(&extent_op->key, key, sizeof(extent_op->key)); | ||
4059 | else | ||
4060 | memset(&extent_op->key, 0, sizeof(extent_op->key)); | ||
4061 | extent_op->flags_to_set = flags; | ||
4062 | extent_op->update_key = 1; | ||
4063 | extent_op->update_flags = 1; | ||
4064 | extent_op->is_data = 0; | ||
4065 | |||
4066 | ret = btrfs_add_delayed_tree_ref(trans, ins->objectid, | ||
4067 | ins->offset, parent, root_objectid, | ||
4068 | level, BTRFS_ADD_DELAYED_EXTENT, | ||
4069 | extent_op); | ||
3119 | BUG_ON(ret); | 4070 | BUG_ON(ret); |
3120 | } | 4071 | } |
3121 | update_reserved_extents(root, ins->objectid, ins->offset, 1); | ||
3122 | return ret; | 4072 | return ret; |
3123 | } | 4073 | } |
3124 | 4074 | ||
@@ -3157,21 +4107,17 @@ struct extent_buffer *btrfs_init_new_buffer(struct btrfs_trans_handle *trans, | |||
3157 | * returns the tree buffer or NULL. | 4107 | * returns the tree buffer or NULL. |
3158 | */ | 4108 | */ |
3159 | struct extent_buffer *btrfs_alloc_free_block(struct btrfs_trans_handle *trans, | 4109 | struct extent_buffer *btrfs_alloc_free_block(struct btrfs_trans_handle *trans, |
3160 | struct btrfs_root *root, | 4110 | struct btrfs_root *root, u32 blocksize, |
3161 | u32 blocksize, u64 parent, | 4111 | u64 parent, u64 root_objectid, |
3162 | u64 root_objectid, | 4112 | struct btrfs_disk_key *key, int level, |
3163 | u64 ref_generation, | 4113 | u64 hint, u64 empty_size) |
3164 | int level, | ||
3165 | u64 hint, | ||
3166 | u64 empty_size) | ||
3167 | { | 4114 | { |
3168 | struct btrfs_key ins; | 4115 | struct btrfs_key ins; |
3169 | int ret; | 4116 | int ret; |
3170 | struct extent_buffer *buf; | 4117 | struct extent_buffer *buf; |
3171 | 4118 | ||
3172 | ret = btrfs_alloc_extent(trans, root, blocksize, parent, blocksize, | 4119 | ret = alloc_tree_block(trans, root, blocksize, parent, root_objectid, |
3173 | root_objectid, ref_generation, level, | 4120 | key, level, empty_size, hint, (u64)-1, &ins); |
3174 | empty_size, hint, (u64)-1, &ins, 0); | ||
3175 | if (ret) { | 4121 | if (ret) { |
3176 | BUG_ON(ret > 0); | 4122 | BUG_ON(ret > 0); |
3177 | return ERR_PTR(ret); | 4123 | return ERR_PTR(ret); |
@@ -3185,32 +4131,19 @@ struct extent_buffer *btrfs_alloc_free_block(struct btrfs_trans_handle *trans, | |||
3185 | int btrfs_drop_leaf_ref(struct btrfs_trans_handle *trans, | 4131 | int btrfs_drop_leaf_ref(struct btrfs_trans_handle *trans, |
3186 | struct btrfs_root *root, struct extent_buffer *leaf) | 4132 | struct btrfs_root *root, struct extent_buffer *leaf) |
3187 | { | 4133 | { |
3188 | u64 leaf_owner; | 4134 | u64 disk_bytenr; |
3189 | u64 leaf_generation; | 4135 | u64 num_bytes; |
3190 | struct refsort *sorted; | ||
3191 | struct btrfs_key key; | 4136 | struct btrfs_key key; |
3192 | struct btrfs_file_extent_item *fi; | 4137 | struct btrfs_file_extent_item *fi; |
4138 | u32 nritems; | ||
3193 | int i; | 4139 | int i; |
3194 | int nritems; | ||
3195 | int ret; | 4140 | int ret; |
3196 | int refi = 0; | ||
3197 | int slot; | ||
3198 | 4141 | ||
3199 | BUG_ON(!btrfs_is_leaf(leaf)); | 4142 | BUG_ON(!btrfs_is_leaf(leaf)); |
3200 | nritems = btrfs_header_nritems(leaf); | 4143 | nritems = btrfs_header_nritems(leaf); |
3201 | leaf_owner = btrfs_header_owner(leaf); | ||
3202 | leaf_generation = btrfs_header_generation(leaf); | ||
3203 | 4144 | ||
3204 | sorted = kmalloc(sizeof(*sorted) * nritems, GFP_NOFS); | ||
3205 | /* we do this loop twice. The first time we build a list | ||
3206 | * of the extents we have a reference on, then we sort the list | ||
3207 | * by bytenr. The second time around we actually do the | ||
3208 | * extent freeing. | ||
3209 | */ | ||
3210 | for (i = 0; i < nritems; i++) { | 4145 | for (i = 0; i < nritems; i++) { |
3211 | u64 disk_bytenr; | ||
3212 | cond_resched(); | 4146 | cond_resched(); |
3213 | |||
3214 | btrfs_item_key_to_cpu(leaf, &key, i); | 4147 | btrfs_item_key_to_cpu(leaf, &key, i); |
3215 | 4148 | ||
3216 | /* only extents have references, skip everything else */ | 4149 | /* only extents have references, skip everything else */ |
@@ -3230,45 +4163,16 @@ int btrfs_drop_leaf_ref(struct btrfs_trans_handle *trans, | |||
3230 | if (disk_bytenr == 0) | 4163 | if (disk_bytenr == 0) |
3231 | continue; | 4164 | continue; |
3232 | 4165 | ||
3233 | sorted[refi].bytenr = disk_bytenr; | 4166 | num_bytes = btrfs_file_extent_disk_num_bytes(leaf, fi); |
3234 | sorted[refi].slot = i; | 4167 | ret = btrfs_free_extent(trans, root, disk_bytenr, num_bytes, |
3235 | refi++; | 4168 | leaf->start, 0, key.objectid, 0); |
3236 | } | ||
3237 | |||
3238 | if (refi == 0) | ||
3239 | goto out; | ||
3240 | |||
3241 | sort(sorted, refi, sizeof(struct refsort), refsort_cmp, NULL); | ||
3242 | |||
3243 | for (i = 0; i < refi; i++) { | ||
3244 | u64 disk_bytenr; | ||
3245 | |||
3246 | disk_bytenr = sorted[i].bytenr; | ||
3247 | slot = sorted[i].slot; | ||
3248 | |||
3249 | cond_resched(); | ||
3250 | |||
3251 | btrfs_item_key_to_cpu(leaf, &key, slot); | ||
3252 | if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY) | ||
3253 | continue; | ||
3254 | |||
3255 | fi = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item); | ||
3256 | |||
3257 | ret = btrfs_free_extent(trans, root, disk_bytenr, | ||
3258 | btrfs_file_extent_disk_num_bytes(leaf, fi), | ||
3259 | leaf->start, leaf_owner, leaf_generation, | ||
3260 | key.objectid, 0); | ||
3261 | BUG_ON(ret); | 4169 | BUG_ON(ret); |
3262 | |||
3263 | atomic_inc(&root->fs_info->throttle_gen); | ||
3264 | wake_up(&root->fs_info->transaction_throttle); | ||
3265 | cond_resched(); | ||
3266 | } | 4170 | } |
3267 | out: | ||
3268 | kfree(sorted); | ||
3269 | return 0; | 4171 | return 0; |
3270 | } | 4172 | } |
3271 | 4173 | ||
4174 | #if 0 | ||
4175 | |||
3272 | static noinline int cache_drop_leaf_ref(struct btrfs_trans_handle *trans, | 4176 | static noinline int cache_drop_leaf_ref(struct btrfs_trans_handle *trans, |
3273 | struct btrfs_root *root, | 4177 | struct btrfs_root *root, |
3274 | struct btrfs_leaf_ref *ref) | 4178 | struct btrfs_leaf_ref *ref) |
@@ -3311,13 +4215,14 @@ static noinline int cache_drop_leaf_ref(struct btrfs_trans_handle *trans, | |||
3311 | return 0; | 4215 | return 0; |
3312 | } | 4216 | } |
3313 | 4217 | ||
4218 | |||
3314 | static int drop_snap_lookup_refcount(struct btrfs_trans_handle *trans, | 4219 | static int drop_snap_lookup_refcount(struct btrfs_trans_handle *trans, |
3315 | struct btrfs_root *root, u64 start, | 4220 | struct btrfs_root *root, u64 start, |
3316 | u64 len, u32 *refs) | 4221 | u64 len, u32 *refs) |
3317 | { | 4222 | { |
3318 | int ret; | 4223 | int ret; |
3319 | 4224 | ||
3320 | ret = btrfs_lookup_extent_ref(trans, root, start, len, refs); | 4225 | ret = btrfs_lookup_extent_refs(trans, root, start, len, refs); |
3321 | BUG_ON(ret); | 4226 | BUG_ON(ret); |
3322 | 4227 | ||
3323 | #if 0 /* some debugging code in case we see problems here */ | 4228 | #if 0 /* some debugging code in case we see problems here */ |
@@ -3352,6 +4257,7 @@ static int drop_snap_lookup_refcount(struct btrfs_trans_handle *trans, | |||
3352 | return ret; | 4257 | return ret; |
3353 | } | 4258 | } |
3354 | 4259 | ||
4260 | |||
3355 | /* | 4261 | /* |
3356 | * this is used while deleting old snapshots, and it drops the refs | 4262 | * this is used while deleting old snapshots, and it drops the refs |
3357 | * on a whole subtree starting from a level 1 node. | 4263 | * on a whole subtree starting from a level 1 node. |
@@ -3645,32 +4551,36 @@ out: | |||
3645 | cond_resched(); | 4551 | cond_resched(); |
3646 | return 0; | 4552 | return 0; |
3647 | } | 4553 | } |
4554 | #endif | ||
3648 | 4555 | ||
3649 | /* | 4556 | /* |
3650 | * helper function for drop_subtree, this function is similar to | 4557 | * helper function for drop_subtree, this function is similar to |
3651 | * walk_down_tree. The main difference is that it checks reference | 4558 | * walk_down_tree. The main difference is that it checks reference |
3652 | * counts while tree blocks are locked. | 4559 | * counts while tree blocks are locked. |
3653 | */ | 4560 | */ |
3654 | static noinline int walk_down_subtree(struct btrfs_trans_handle *trans, | 4561 | static noinline int walk_down_tree(struct btrfs_trans_handle *trans, |
3655 | struct btrfs_root *root, | 4562 | struct btrfs_root *root, |
3656 | struct btrfs_path *path, int *level) | 4563 | struct btrfs_path *path, int *level) |
3657 | { | 4564 | { |
3658 | struct extent_buffer *next; | 4565 | struct extent_buffer *next; |
3659 | struct extent_buffer *cur; | 4566 | struct extent_buffer *cur; |
3660 | struct extent_buffer *parent; | 4567 | struct extent_buffer *parent; |
3661 | u64 bytenr; | 4568 | u64 bytenr; |
3662 | u64 ptr_gen; | 4569 | u64 ptr_gen; |
4570 | u64 refs; | ||
4571 | u64 flags; | ||
3663 | u32 blocksize; | 4572 | u32 blocksize; |
3664 | u32 refs; | ||
3665 | int ret; | 4573 | int ret; |
3666 | 4574 | ||
3667 | cur = path->nodes[*level]; | 4575 | cur = path->nodes[*level]; |
3668 | ret = btrfs_lookup_extent_ref(trans, root, cur->start, cur->len, | 4576 | ret = btrfs_lookup_extent_info(trans, root, cur->start, cur->len, |
3669 | &refs); | 4577 | &refs, &flags); |
3670 | BUG_ON(ret); | 4578 | BUG_ON(ret); |
3671 | if (refs > 1) | 4579 | if (refs > 1) |
3672 | goto out; | 4580 | goto out; |
3673 | 4581 | ||
4582 | BUG_ON(!(flags & BTRFS_BLOCK_FLAG_FULL_BACKREF)); | ||
4583 | |||
3674 | while (*level >= 0) { | 4584 | while (*level >= 0) { |
3675 | cur = path->nodes[*level]; | 4585 | cur = path->nodes[*level]; |
3676 | if (*level == 0) { | 4586 | if (*level == 0) { |
@@ -3692,16 +4602,15 @@ static noinline int walk_down_subtree(struct btrfs_trans_handle *trans, | |||
3692 | btrfs_tree_lock(next); | 4602 | btrfs_tree_lock(next); |
3693 | btrfs_set_lock_blocking(next); | 4603 | btrfs_set_lock_blocking(next); |
3694 | 4604 | ||
3695 | ret = btrfs_lookup_extent_ref(trans, root, bytenr, blocksize, | 4605 | ret = btrfs_lookup_extent_info(trans, root, bytenr, blocksize, |
3696 | &refs); | 4606 | &refs, &flags); |
3697 | BUG_ON(ret); | 4607 | BUG_ON(ret); |
3698 | if (refs > 1) { | 4608 | if (refs > 1) { |
3699 | parent = path->nodes[*level]; | 4609 | parent = path->nodes[*level]; |
3700 | ret = btrfs_free_extent(trans, root, bytenr, | 4610 | ret = btrfs_free_extent(trans, root, bytenr, |
3701 | blocksize, parent->start, | 4611 | blocksize, parent->start, |
3702 | btrfs_header_owner(parent), | 4612 | btrfs_header_owner(parent), |
3703 | btrfs_header_generation(parent), | 4613 | *level - 1, 0); |
3704 | *level - 1, 1); | ||
3705 | BUG_ON(ret); | 4614 | BUG_ON(ret); |
3706 | path->slots[*level]++; | 4615 | path->slots[*level]++; |
3707 | btrfs_tree_unlock(next); | 4616 | btrfs_tree_unlock(next); |
@@ -3709,6 +4618,8 @@ static noinline int walk_down_subtree(struct btrfs_trans_handle *trans, | |||
3709 | continue; | 4618 | continue; |
3710 | } | 4619 | } |
3711 | 4620 | ||
4621 | BUG_ON(!(flags & BTRFS_BLOCK_FLAG_FULL_BACKREF)); | ||
4622 | |||
3712 | *level = btrfs_header_level(next); | 4623 | *level = btrfs_header_level(next); |
3713 | path->nodes[*level] = next; | 4624 | path->nodes[*level] = next; |
3714 | path->slots[*level] = 0; | 4625 | path->slots[*level] = 0; |
@@ -3716,13 +4627,15 @@ static noinline int walk_down_subtree(struct btrfs_trans_handle *trans, | |||
3716 | cond_resched(); | 4627 | cond_resched(); |
3717 | } | 4628 | } |
3718 | out: | 4629 | out: |
3719 | parent = path->nodes[*level + 1]; | 4630 | if (path->nodes[*level] == root->node) |
4631 | parent = path->nodes[*level]; | ||
4632 | else | ||
4633 | parent = path->nodes[*level + 1]; | ||
3720 | bytenr = path->nodes[*level]->start; | 4634 | bytenr = path->nodes[*level]->start; |
3721 | blocksize = path->nodes[*level]->len; | 4635 | blocksize = path->nodes[*level]->len; |
3722 | 4636 | ||
3723 | ret = btrfs_free_extent(trans, root, bytenr, blocksize, | 4637 | ret = btrfs_free_extent(trans, root, bytenr, blocksize, parent->start, |
3724 | parent->start, btrfs_header_owner(parent), | 4638 | btrfs_header_owner(parent), *level, 0); |
3725 | btrfs_header_generation(parent), *level, 1); | ||
3726 | BUG_ON(ret); | 4639 | BUG_ON(ret); |
3727 | 4640 | ||
3728 | if (path->locks[*level]) { | 4641 | if (path->locks[*level]) { |
@@ -3746,8 +4659,6 @@ static noinline int walk_up_tree(struct btrfs_trans_handle *trans, | |||
3746 | struct btrfs_path *path, | 4659 | struct btrfs_path *path, |
3747 | int *level, int max_level) | 4660 | int *level, int max_level) |
3748 | { | 4661 | { |
3749 | u64 root_owner; | ||
3750 | u64 root_gen; | ||
3751 | struct btrfs_root_item *root_item = &root->root_item; | 4662 | struct btrfs_root_item *root_item = &root->root_item; |
3752 | int i; | 4663 | int i; |
3753 | int slot; | 4664 | int slot; |
@@ -3755,24 +4666,22 @@ static noinline int walk_up_tree(struct btrfs_trans_handle *trans, | |||
3755 | 4666 | ||
3756 | for (i = *level; i < max_level && path->nodes[i]; i++) { | 4667 | for (i = *level; i < max_level && path->nodes[i]; i++) { |
3757 | slot = path->slots[i]; | 4668 | slot = path->slots[i]; |
3758 | if (slot < btrfs_header_nritems(path->nodes[i]) - 1) { | 4669 | if (slot + 1 < btrfs_header_nritems(path->nodes[i])) { |
3759 | struct extent_buffer *node; | ||
3760 | struct btrfs_disk_key disk_key; | ||
3761 | |||
3762 | /* | 4670 | /* |
3763 | * there is more work to do in this level. | 4671 | * there is more work to do in this level. |
3764 | * Update the drop_progress marker to reflect | 4672 | * Update the drop_progress marker to reflect |
3765 | * the work we've done so far, and then bump | 4673 | * the work we've done so far, and then bump |
3766 | * the slot number | 4674 | * the slot number |
3767 | */ | 4675 | */ |
3768 | node = path->nodes[i]; | ||
3769 | path->slots[i]++; | 4676 | path->slots[i]++; |
3770 | *level = i; | ||
3771 | WARN_ON(*level == 0); | 4677 | WARN_ON(*level == 0); |
3772 | btrfs_node_key(node, &disk_key, path->slots[i]); | 4678 | if (max_level == BTRFS_MAX_LEVEL) { |
3773 | memcpy(&root_item->drop_progress, | 4679 | btrfs_node_key(path->nodes[i], |
3774 | &disk_key, sizeof(disk_key)); | 4680 | &root_item->drop_progress, |
3775 | root_item->drop_level = i; | 4681 | path->slots[i]); |
4682 | root_item->drop_level = i; | ||
4683 | } | ||
4684 | *level = i; | ||
3776 | return 0; | 4685 | return 0; |
3777 | } else { | 4686 | } else { |
3778 | struct extent_buffer *parent; | 4687 | struct extent_buffer *parent; |
@@ -3786,22 +4695,20 @@ static noinline int walk_up_tree(struct btrfs_trans_handle *trans, | |||
3786 | else | 4695 | else |
3787 | parent = path->nodes[*level + 1]; | 4696 | parent = path->nodes[*level + 1]; |
3788 | 4697 | ||
3789 | root_owner = btrfs_header_owner(parent); | 4698 | clean_tree_block(trans, root, path->nodes[i]); |
3790 | root_gen = btrfs_header_generation(parent); | ||
3791 | |||
3792 | clean_tree_block(trans, root, path->nodes[*level]); | ||
3793 | ret = btrfs_free_extent(trans, root, | 4699 | ret = btrfs_free_extent(trans, root, |
3794 | path->nodes[*level]->start, | 4700 | path->nodes[i]->start, |
3795 | path->nodes[*level]->len, | 4701 | path->nodes[i]->len, |
3796 | parent->start, root_owner, | 4702 | parent->start, |
3797 | root_gen, *level, 1); | 4703 | btrfs_header_owner(parent), |
4704 | *level, 0); | ||
3798 | BUG_ON(ret); | 4705 | BUG_ON(ret); |
3799 | if (path->locks[*level]) { | 4706 | if (path->locks[*level]) { |
3800 | btrfs_tree_unlock(path->nodes[*level]); | 4707 | btrfs_tree_unlock(path->nodes[i]); |
3801 | path->locks[*level] = 0; | 4708 | path->locks[i] = 0; |
3802 | } | 4709 | } |
3803 | free_extent_buffer(path->nodes[*level]); | 4710 | free_extent_buffer(path->nodes[i]); |
3804 | path->nodes[*level] = NULL; | 4711 | path->nodes[i] = NULL; |
3805 | *level = i + 1; | 4712 | *level = i + 1; |
3806 | } | 4713 | } |
3807 | } | 4714 | } |
@@ -3820,21 +4727,18 @@ int btrfs_drop_snapshot(struct btrfs_trans_handle *trans, struct btrfs_root | |||
3820 | int wret; | 4727 | int wret; |
3821 | int level; | 4728 | int level; |
3822 | struct btrfs_path *path; | 4729 | struct btrfs_path *path; |
3823 | int i; | ||
3824 | int orig_level; | ||
3825 | int update_count; | 4730 | int update_count; |
3826 | struct btrfs_root_item *root_item = &root->root_item; | 4731 | struct btrfs_root_item *root_item = &root->root_item; |
3827 | 4732 | ||
3828 | WARN_ON(!mutex_is_locked(&root->fs_info->drop_mutex)); | ||
3829 | path = btrfs_alloc_path(); | 4733 | path = btrfs_alloc_path(); |
3830 | BUG_ON(!path); | 4734 | BUG_ON(!path); |
3831 | 4735 | ||
3832 | level = btrfs_header_level(root->node); | 4736 | level = btrfs_header_level(root->node); |
3833 | orig_level = level; | ||
3834 | if (btrfs_disk_key_objectid(&root_item->drop_progress) == 0) { | 4737 | if (btrfs_disk_key_objectid(&root_item->drop_progress) == 0) { |
3835 | path->nodes[level] = root->node; | 4738 | path->nodes[level] = btrfs_lock_root_node(root); |
3836 | extent_buffer_get(root->node); | 4739 | btrfs_set_lock_blocking(path->nodes[level]); |
3837 | path->slots[level] = 0; | 4740 | path->slots[level] = 0; |
4741 | path->locks[level] = 1; | ||
3838 | } else { | 4742 | } else { |
3839 | struct btrfs_key key; | 4743 | struct btrfs_key key; |
3840 | struct btrfs_disk_key found_key; | 4744 | struct btrfs_disk_key found_key; |
@@ -3856,12 +4760,7 @@ int btrfs_drop_snapshot(struct btrfs_trans_handle *trans, struct btrfs_root | |||
3856 | * unlock our path, this is safe because only this | 4760 | * unlock our path, this is safe because only this |
3857 | * function is allowed to delete this snapshot | 4761 | * function is allowed to delete this snapshot |
3858 | */ | 4762 | */ |
3859 | for (i = 0; i < BTRFS_MAX_LEVEL; i++) { | 4763 | btrfs_unlock_up_safe(path, 0); |
3860 | if (path->nodes[i] && path->locks[i]) { | ||
3861 | path->locks[i] = 0; | ||
3862 | btrfs_tree_unlock(path->nodes[i]); | ||
3863 | } | ||
3864 | } | ||
3865 | } | 4764 | } |
3866 | while (1) { | 4765 | while (1) { |
3867 | unsigned long update; | 4766 | unsigned long update; |
@@ -3882,8 +4781,6 @@ int btrfs_drop_snapshot(struct btrfs_trans_handle *trans, struct btrfs_root | |||
3882 | ret = -EAGAIN; | 4781 | ret = -EAGAIN; |
3883 | break; | 4782 | break; |
3884 | } | 4783 | } |
3885 | atomic_inc(&root->fs_info->throttle_gen); | ||
3886 | wake_up(&root->fs_info->transaction_throttle); | ||
3887 | for (update_count = 0; update_count < 16; update_count++) { | 4784 | for (update_count = 0; update_count < 16; update_count++) { |
3888 | update = trans->delayed_ref_updates; | 4785 | update = trans->delayed_ref_updates; |
3889 | trans->delayed_ref_updates = 0; | 4786 | trans->delayed_ref_updates = 0; |
@@ -3893,12 +4790,6 @@ int btrfs_drop_snapshot(struct btrfs_trans_handle *trans, struct btrfs_root | |||
3893 | break; | 4790 | break; |
3894 | } | 4791 | } |
3895 | } | 4792 | } |
3896 | for (i = 0; i <= orig_level; i++) { | ||
3897 | if (path->nodes[i]) { | ||
3898 | free_extent_buffer(path->nodes[i]); | ||
3899 | path->nodes[i] = NULL; | ||
3900 | } | ||
3901 | } | ||
3902 | out: | 4793 | out: |
3903 | btrfs_free_path(path); | 4794 | btrfs_free_path(path); |
3904 | return ret; | 4795 | return ret; |
@@ -3931,7 +4822,7 @@ int btrfs_drop_subtree(struct btrfs_trans_handle *trans, | |||
3931 | path->slots[level] = 0; | 4822 | path->slots[level] = 0; |
3932 | 4823 | ||
3933 | while (1) { | 4824 | while (1) { |
3934 | wret = walk_down_subtree(trans, root, path, &level); | 4825 | wret = walk_down_tree(trans, root, path, &level); |
3935 | if (wret < 0) | 4826 | if (wret < 0) |
3936 | ret = wret; | 4827 | ret = wret; |
3937 | if (wret != 0) | 4828 | if (wret != 0) |
@@ -3948,6 +4839,7 @@ int btrfs_drop_subtree(struct btrfs_trans_handle *trans, | |||
3948 | return ret; | 4839 | return ret; |
3949 | } | 4840 | } |
3950 | 4841 | ||
4842 | #if 0 | ||
3951 | static unsigned long calc_ra(unsigned long start, unsigned long last, | 4843 | static unsigned long calc_ra(unsigned long start, unsigned long last, |
3952 | unsigned long nr) | 4844 | unsigned long nr) |
3953 | { | 4845 | { |
@@ -5429,6 +6321,7 @@ out: | |||
5429 | kfree(ref_path); | 6321 | kfree(ref_path); |
5430 | return ret; | 6322 | return ret; |
5431 | } | 6323 | } |
6324 | #endif | ||
5432 | 6325 | ||
5433 | static u64 update_block_group_flags(struct btrfs_root *root, u64 flags) | 6326 | static u64 update_block_group_flags(struct btrfs_root *root, u64 flags) |
5434 | { | 6327 | { |
@@ -5477,7 +6370,8 @@ static int __alloc_chunk_for_shrink(struct btrfs_root *root, | |||
5477 | u64 calc; | 6370 | u64 calc; |
5478 | 6371 | ||
5479 | spin_lock(&shrink_block_group->lock); | 6372 | spin_lock(&shrink_block_group->lock); |
5480 | if (btrfs_block_group_used(&shrink_block_group->item) > 0) { | 6373 | if (btrfs_block_group_used(&shrink_block_group->item) + |
6374 | shrink_block_group->reserved > 0) { | ||
5481 | spin_unlock(&shrink_block_group->lock); | 6375 | spin_unlock(&shrink_block_group->lock); |
5482 | 6376 | ||
5483 | trans = btrfs_start_transaction(root, 1); | 6377 | trans = btrfs_start_transaction(root, 1); |
@@ -5502,6 +6396,17 @@ static int __alloc_chunk_for_shrink(struct btrfs_root *root, | |||
5502 | return 0; | 6396 | return 0; |
5503 | } | 6397 | } |
5504 | 6398 | ||
6399 | |||
6400 | int btrfs_prepare_block_group_relocation(struct btrfs_root *root, | ||
6401 | struct btrfs_block_group_cache *group) | ||
6402 | |||
6403 | { | ||
6404 | __alloc_chunk_for_shrink(root, group, 1); | ||
6405 | set_block_group_readonly(group); | ||
6406 | return 0; | ||
6407 | } | ||
6408 | |||
6409 | #if 0 | ||
5505 | static int __insert_orphan_inode(struct btrfs_trans_handle *trans, | 6410 | static int __insert_orphan_inode(struct btrfs_trans_handle *trans, |
5506 | struct btrfs_root *root, | 6411 | struct btrfs_root *root, |
5507 | u64 objectid, u64 size) | 6412 | u64 objectid, u64 size) |
@@ -5781,6 +6686,7 @@ out: | |||
5781 | btrfs_free_path(path); | 6686 | btrfs_free_path(path); |
5782 | return ret; | 6687 | return ret; |
5783 | } | 6688 | } |
6689 | #endif | ||
5784 | 6690 | ||
5785 | static int find_first_block_group(struct btrfs_root *root, | 6691 | static int find_first_block_group(struct btrfs_root *root, |
5786 | struct btrfs_path *path, struct btrfs_key *key) | 6692 | struct btrfs_path *path, struct btrfs_key *key) |