extent-tree.c (1c2308f8e7d8491467e0095af2b01500f1b70819) extent-tree.c (31840ae1a6b433ca0e6a8d341756ff478bbf959e)
1/*
2 * Copyright (C) 2007 Oracle. All rights reserved.
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public
6 * License v2 as published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful,

--- 15 unchanged lines hidden (view full) ---

24#include "ctree.h"
25#include "disk-io.h"
26#include "print-tree.h"
27#include "transaction.h"
28#include "volumes.h"
29#include "locking.h"
30#include "ref-cache.h"
31
1/*
2 * Copyright (C) 2007 Oracle. All rights reserved.
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public
6 * License v2 as published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful,

--- 15 unchanged lines hidden (view full) ---

24#include "ctree.h"
25#include "disk-io.h"
26#include "print-tree.h"
27#include "transaction.h"
28#include "volumes.h"
29#include "locking.h"
30#include "ref-cache.h"
31
32#define PENDING_EXTENT_INSERT 0
33#define PENDING_EXTENT_DELETE 1
34#define PENDING_BACKREF_UPDATE 2
35
36struct pending_extent_op {
37 int type;
38 u64 bytenr;
39 u64 num_bytes;
40 u64 parent;
41 u64 orig_parent;
42 u64 generation;
43 u64 orig_generation;
44 int level;
45};
46
32static int finish_current_insert(struct btrfs_trans_handle *trans, struct
33 btrfs_root *extent_root);
34static int del_pending_extents(struct btrfs_trans_handle *trans, struct
35 btrfs_root *extent_root);
36static struct btrfs_block_group_cache *
37__btrfs_find_block_group(struct btrfs_root *root,
38 struct btrfs_block_group_cache *hint,
39 u64 search_start, int data, int owner);

--- 442 unchanged lines hidden (view full) ---

482 int data, int owner)
483{
484
485 struct btrfs_block_group_cache *ret;
486 ret = __btrfs_find_block_group(root, hint, search_start, data, owner);
487 return ret;
488}
489
47static int finish_current_insert(struct btrfs_trans_handle *trans, struct
48 btrfs_root *extent_root);
49static int del_pending_extents(struct btrfs_trans_handle *trans, struct
50 btrfs_root *extent_root);
51static struct btrfs_block_group_cache *
52__btrfs_find_block_group(struct btrfs_root *root,
53 struct btrfs_block_group_cache *hint,
54 u64 search_start, int data, int owner);

--- 442 unchanged lines hidden (view full) ---

497 int data, int owner)
498{
499
500 struct btrfs_block_group_cache *ret;
501 ret = __btrfs_find_block_group(root, hint, search_start, data, owner);
502 return ret;
503}
504
490static u64 hash_extent_ref(u64 root_objectid, u64 ref_generation,
491 u64 owner, u64 owner_offset)
492{
493 u32 high_crc = ~(u32)0;
494 u32 low_crc = ~(u32)0;
495 __le64 lenum;
496 lenum = cpu_to_le64(root_objectid);
497 high_crc = btrfs_crc32c(high_crc, &lenum, sizeof(lenum));
498 lenum = cpu_to_le64(ref_generation);
499 low_crc = btrfs_crc32c(low_crc, &lenum, sizeof(lenum));
500 if (owner >= BTRFS_FIRST_FREE_OBJECTID) {
501 lenum = cpu_to_le64(owner);
502 low_crc = btrfs_crc32c(low_crc, &lenum, sizeof(lenum));
503 lenum = cpu_to_le64(owner_offset);
504 low_crc = btrfs_crc32c(low_crc, &lenum, sizeof(lenum));
505 }
506 return ((u64)high_crc << 32) | (u64)low_crc;
507}
508
509static int match_extent_ref(struct extent_buffer *leaf,
510 struct btrfs_extent_ref *disk_ref,
511 struct btrfs_extent_ref *cpu_ref)
512{
513 int ret;
514 int len;
515
516 if (cpu_ref->objectid)
517 len = sizeof(*cpu_ref);
518 else
519 len = 2 * sizeof(u64);
520 ret = memcmp_extent_buffer(leaf, cpu_ref, (unsigned long)disk_ref,
521 len);
522 return ret == 0;
523}
524
525/* simple helper to search for an existing extent at a given offset */
505/* simple helper to search for an existing extent at a given offset */
526int btrfs_lookup_extent(struct btrfs_root *root, struct btrfs_path *path,
527 u64 start, u64 len)
506int btrfs_lookup_extent(struct btrfs_root *root, u64 start, u64 len)
528{
529 int ret;
530 struct btrfs_key key;
507{
508 int ret;
509 struct btrfs_key key;
510 struct btrfs_path *path;
531
511
512 path = btrfs_alloc_path();
513 BUG_ON(!path);
532 maybe_lock_mutex(root);
533 key.objectid = start;
534 key.offset = len;
535 btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
536 ret = btrfs_search_slot(NULL, root->fs_info->extent_root, &key, path,
537 0, 0);
538 maybe_unlock_mutex(root);
514 maybe_lock_mutex(root);
515 key.objectid = start;
516 key.offset = len;
517 btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
518 ret = btrfs_search_slot(NULL, root->fs_info->extent_root, &key, path,
519 0, 0);
520 maybe_unlock_mutex(root);
521 btrfs_free_path(path);
539 return ret;
540}
541
522 return ret;
523}
524
542static int noinline lookup_extent_backref(struct btrfs_trans_handle *trans,
543 struct btrfs_root *root,
544 struct btrfs_path *path, u64 bytenr,
545 u64 root_objectid,
546 u64 ref_generation, u64 owner,
547 u64 owner_offset, int del)
548{
549 u64 hash;
550 struct btrfs_key key;
551 struct btrfs_key found_key;
552 struct btrfs_extent_ref ref;
553 struct extent_buffer *leaf;
554 struct btrfs_extent_ref *disk_ref;
555 int ret;
556 int ret2;
557
558 btrfs_set_stack_ref_root(&ref, root_objectid);
559 btrfs_set_stack_ref_generation(&ref, ref_generation);
560 btrfs_set_stack_ref_objectid(&ref, owner);
561 btrfs_set_stack_ref_offset(&ref, owner_offset);
562
563 hash = hash_extent_ref(root_objectid, ref_generation, owner,
564 owner_offset);
565 key.offset = hash;
566 key.objectid = bytenr;
567 key.type = BTRFS_EXTENT_REF_KEY;
568
569 while (1) {
570 ret = btrfs_search_slot(trans, root, &key, path,
571 del ? -1 : 0, del);
572 if (ret < 0)
573 goto out;
574 leaf = path->nodes[0];
575 if (ret != 0) {
576 u32 nritems = btrfs_header_nritems(leaf);
577 if (path->slots[0] >= nritems) {
578 ret2 = btrfs_next_leaf(root, path);
579 if (ret2)
580 goto out;
581 leaf = path->nodes[0];
582 }
583 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
584 if (found_key.objectid != bytenr ||
585 found_key.type != BTRFS_EXTENT_REF_KEY)
586 goto out;
587 key.offset = found_key.offset;
588 if (del) {
589 btrfs_release_path(root, path);
590 continue;
591 }
592 }
593 disk_ref = btrfs_item_ptr(path->nodes[0],
594 path->slots[0],
595 struct btrfs_extent_ref);
596 if (match_extent_ref(path->nodes[0], disk_ref, &ref)) {
597 ret = 0;
598 goto out;
599 }
600 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
601 key.offset = found_key.offset + 1;
602 btrfs_release_path(root, path);
603 }
604out:
605 return ret;
606}
607
608/*
609 * Back reference rules. Back refs have three main goals:
610 *
611 * 1) differentiate between all holders of references to an extent so that
612 * when a reference is dropped we can make sure it was a valid reference
613 * before freeing the extent.
614 *
615 * 2) Provide enough information to quickly find the holders of an extent
616 * if we notice a given block is corrupted or bad.
617 *
618 * 3) Make it easy to migrate blocks for FS shrinking or storage pool
619 * maintenance. This is actually the same as #2, but with a slightly
620 * different use case.
621 *
622 * File extents can be referenced by:
623 *
624 * - multiple snapshots, subvolumes, or different generations in one subvol
525/*
526 * Back reference rules. Back refs have three main goals:
527 *
528 * 1) differentiate between all holders of references to an extent so that
529 * when a reference is dropped we can make sure it was a valid reference
530 * before freeing the extent.
531 *
532 * 2) Provide enough information to quickly find the holders of an extent
533 * if we notice a given block is corrupted or bad.
534 *
535 * 3) Make it easy to migrate blocks for FS shrinking or storage pool
536 * maintenance. This is actually the same as #2, but with a slightly
537 * different use case.
538 *
539 * File extents can be referenced by:
540 *
541 * - multiple snapshots, subvolumes, or different generations in one subvol
625 * - different files inside a single subvolume (in theory, not implemented yet)
542 * - different files inside a single subvolume
626 * - different offsets inside a file (bookend extents in file.c)
627 *
628 * The extent ref structure has fields for:
629 *
630 * - Objectid of the subvolume root
631 * - Generation number of the tree holding the reference
632 * - objectid of the file holding the reference
633 * - offset in the file corresponding to the key holding the reference
543 * - different offsets inside a file (bookend extents in file.c)
544 *
545 * The extent ref structure has fields for:
546 *
547 * - Objectid of the subvolume root
548 * - Generation number of the tree holding the reference
549 * - objectid of the file holding the reference
550 * - offset in the file corresponding to the key holding the reference
551 * - number of references holding by parent node (alway 1 for tree blocks)
634 *
552 *
553 * Btree leaf may hold multiple references to a file extent. In most cases,
554 * these references are from same file and the corresponding offsets inside
555 * the file are close together. So inode objectid and offset in file are
556 * just hints, they provide hints about where in the btree the references
557 * can be found and when we can stop searching.
558 *
635 * When a file extent is allocated the fields are filled in:
559 * When a file extent is allocated the fields are filled in:
636 * (root_key.objectid, trans->transid, inode objectid, offset in file)
560 * (root_key.objectid, trans->transid, inode objectid, offset in file, 1)
637 *
638 * When a leaf is cow'd new references are added for every file extent found
561 *
562 * When a leaf is cow'd new references are added for every file extent found
639 * in the leaf. It looks the same as the create case, but trans->transid
640 * will be different when the block is cow'd.
563 * in the leaf. It looks similar to the create case, but trans->transid will
564 * be different when the block is cow'd.
641 *
565 *
642 * (root_key.objectid, trans->transid, inode objectid, offset in file)
566 * (root_key.objectid, trans->transid, inode objectid, offset in file,
567 * number of references in the leaf)
643 *
568 *
644 * When a file extent is removed either during snapshot deletion or file
645 * truncation, the corresponding back reference is found
646 * by searching for:
569 * Because inode objectid and offset in file are just hints, they are not
570 * used when backrefs are deleted. When a file extent is removed either
571 * during snapshot deletion or file truncation, we find the corresponding
572 * back back reference and check the following fields.
647 *
573 *
648 * (btrfs_header_owner(leaf), btrfs_header_generation(leaf),
649 * inode objectid, offset in file)
574 * (btrfs_header_owner(leaf), btrfs_header_generation(leaf))
650 *
651 * Btree extents can be referenced by:
652 *
653 * - Different subvolumes
654 * - Different generations of the same subvolume
655 *
575 *
576 * Btree extents can be referenced by:
577 *
578 * - Different subvolumes
579 * - Different generations of the same subvolume
580 *
656 * Storing sufficient information for a full reverse mapping of a btree
657 * block would require storing the lowest key of the block in the backref,
658 * and it would require updating that lowest key either before write out or
659 * every time it changed. Instead, the objectid of the lowest key is stored
660 * along with the level of the tree block. This provides a hint
661 * about where in the btree the block can be found. Searches through the
662 * btree only need to look for a pointer to that block, so they stop one
663 * level higher than the level recorded in the backref.
664 *
665 * Some btrees do not do reference counting on their extents. These
666 * include the extent tree and the tree of tree roots. Backrefs for these
667 * trees always have a generation of zero.
668 *
669 * When a tree block is created, back references are inserted:
670 *
581 * When a tree block is created, back references are inserted:
582 *
671 * (root->root_key.objectid, trans->transid or zero, level, lowest_key_objectid)
583 * (root->root_key.objectid, trans->transid, level, 0, 1)
672 *
584 *
673 * When a tree block is cow'd in a reference counted root,
674 * new back references are added for all the blocks it points to.
675 * These are of the form (trans->transid will have increased since creation):
585 * When a tree block is cow'd, new back references are added for all the
586 * blocks it points to. If the tree block isn't in reference counted root,
587 * the old back references are removed. These new back references are of
588 * the form (trans->transid will have increased since creation):
676 *
589 *
677 * (root->root_key.objectid, trans->transid, level, lowest_key_objectid)
590 * (root->root_key.objectid, trans->transid, level, 0, 1)
678 *
591 *
679 * Because the lowest_key_objectid and the level are just hints
680 * they are not used when backrefs are deleted. When a backref is deleted:
592 * When a backref is in deleting, the following fields are checked:
681 *
682 * if backref was for a tree root:
593 *
594 * if backref was for a tree root:
683 * root_objectid = root->root_key.objectid
595 * (btrfs_header_owner(itself), btrfs_header_generation(itself))
684 * else
596 * else
685 * root_objectid = btrfs_header_owner(parent)
597 * (btrfs_header_owner(parent), btrfs_header_generation(parent))
686 *
598 *
687 * (root_objectid, btrfs_header_generation(parent) or zero, 0, 0)
599 * Back Reference Key composing:
688 *
600 *
689 * Back Reference Key hashing:
690 *
691 * Back references have four fields, each 64 bits long. Unfortunately,
692 * This is hashed into a single 64 bit number and placed into the key offset.
693 * The key objectid corresponds to the first byte in the extent, and the
694 * key type is set to BTRFS_EXTENT_REF_KEY
601 * The key objectid corresponds to the first byte in the extent, the key
602 * type is set to BTRFS_EXTENT_REF_KEY, and the key offset is the first
603 * byte of parent extent. If a extent is tree root, the key offset is set
604 * to the key objectid.
695 */
605 */
696int btrfs_insert_extent_backref(struct btrfs_trans_handle *trans,
697 struct btrfs_root *root,
698 struct btrfs_path *path, u64 bytenr,
699 u64 root_objectid, u64 ref_generation,
700 u64 owner, u64 owner_offset)
606
607static int noinline lookup_extent_backref(struct btrfs_trans_handle *trans,
608 struct btrfs_root *root,
609 struct btrfs_path *path, u64 bytenr,
610 u64 parent, u64 ref_root,
611 u64 ref_generation, int del)
701{
612{
702 u64 hash;
703 struct btrfs_key key;
613 struct btrfs_key key;
704 struct btrfs_extent_ref ref;
705 struct btrfs_extent_ref *disk_ref;
614 struct btrfs_extent_ref *ref;
615 struct extent_buffer *leaf;
706 int ret;
707
616 int ret;
617
708 btrfs_set_stack_ref_root(&ref, root_objectid);
709 btrfs_set_stack_ref_generation(&ref, ref_generation);
710 btrfs_set_stack_ref_objectid(&ref, owner);
711 btrfs_set_stack_ref_offset(&ref, owner_offset);
618 key.objectid = bytenr;
619 key.type = BTRFS_EXTENT_REF_KEY;
620 key.offset = parent;
712
621
713 hash = hash_extent_ref(root_objectid, ref_generation, owner,
714 owner_offset);
715 key.offset = hash;
622 ret = btrfs_search_slot(trans, root, &key, path, del ? -1 : 0, 1);
623 if (ret < 0)
624 goto out;
625 if (ret > 0) {
626 ret = -ENOENT;
627 goto out;
628 }
629
630 leaf = path->nodes[0];
631 ref = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_ref);
632 if (btrfs_ref_root(leaf, ref) != ref_root ||
633 btrfs_ref_generation(leaf, ref) != ref_generation) {
634 ret = -EIO;
635 WARN_ON(1);
636 goto out;
637 }
638 ret = 0;
639out:
640 return ret;
641}
642
643static int noinline insert_extent_backref(struct btrfs_trans_handle *trans,
644 struct btrfs_root *root,
645 struct btrfs_path *path,
646 u64 bytenr, u64 parent,
647 u64 ref_root, u64 ref_generation,
648 u64 owner_objectid, u64 owner_offset)
649{
650 struct btrfs_key key;
651 struct extent_buffer *leaf;
652 struct btrfs_extent_ref *ref;
653 u32 num_refs;
654 int ret;
655
716 key.objectid = bytenr;
717 key.type = BTRFS_EXTENT_REF_KEY;
656 key.objectid = bytenr;
657 key.type = BTRFS_EXTENT_REF_KEY;
658 key.offset = parent;
718
659
719 ret = btrfs_insert_empty_item(trans, root, path, &key, sizeof(ref));
720 while (ret == -EEXIST) {
721 disk_ref = btrfs_item_ptr(path->nodes[0], path->slots[0],
722 struct btrfs_extent_ref);
723 if (match_extent_ref(path->nodes[0], disk_ref, &ref))
660 ret = btrfs_insert_empty_item(trans, root, path, &key, sizeof(*ref));
661 if (ret == 0) {
662 leaf = path->nodes[0];
663 ref = btrfs_item_ptr(leaf, path->slots[0],
664 struct btrfs_extent_ref);
665 btrfs_set_ref_root(leaf, ref, ref_root);
666 btrfs_set_ref_generation(leaf, ref, ref_generation);
667 btrfs_set_ref_objectid(leaf, ref, owner_objectid);
668 btrfs_set_ref_offset(leaf, ref, owner_offset);
669 btrfs_set_ref_num_refs(leaf, ref, 1);
670 } else if (ret == -EEXIST) {
671 u64 existing_owner;
672 BUG_ON(owner_objectid < BTRFS_FIRST_FREE_OBJECTID);
673 leaf = path->nodes[0];
674 ref = btrfs_item_ptr(leaf, path->slots[0],
675 struct btrfs_extent_ref);
676 if (btrfs_ref_root(leaf, ref) != ref_root ||
677 btrfs_ref_generation(leaf, ref) != ref_generation) {
678 ret = -EIO;
679 WARN_ON(1);
724 goto out;
680 goto out;
725 key.offset++;
726 btrfs_release_path(root, path);
727 ret = btrfs_insert_empty_item(trans, root, path, &key,
728 sizeof(ref));
729 }
730 if (ret)
681 }
682
683 num_refs = btrfs_ref_num_refs(leaf, ref);
684 BUG_ON(num_refs == 0);
685 btrfs_set_ref_num_refs(leaf, ref, num_refs + 1);
686
687 existing_owner = btrfs_ref_objectid(leaf, ref);
688 if (existing_owner == owner_objectid &&
689 btrfs_ref_offset(leaf, ref) > owner_offset) {
690 btrfs_set_ref_offset(leaf, ref, owner_offset);
691 } else if (existing_owner != owner_objectid &&
692 existing_owner != BTRFS_MULTIPLE_OBJECTIDS) {
693 btrfs_set_ref_objectid(leaf, ref,
694 BTRFS_MULTIPLE_OBJECTIDS);
695 btrfs_set_ref_offset(leaf, ref, 0);
696 }
697 ret = 0;
698 } else {
731 goto out;
699 goto out;
732 disk_ref = btrfs_item_ptr(path->nodes[0], path->slots[0],
733 struct btrfs_extent_ref);
734 write_extent_buffer(path->nodes[0], &ref, (unsigned long)disk_ref,
735 sizeof(ref));
700 }
736 btrfs_mark_buffer_dirty(path->nodes[0]);
737out:
738 btrfs_release_path(root, path);
739 return ret;
740}
741
701 btrfs_mark_buffer_dirty(path->nodes[0]);
702out:
703 btrfs_release_path(root, path);
704 return ret;
705}
706
707static int noinline remove_extent_backref(struct btrfs_trans_handle *trans,
708 struct btrfs_root *root,
709 struct btrfs_path *path)
710{
711 struct extent_buffer *leaf;
712 struct btrfs_extent_ref *ref;
713 u32 num_refs;
714 int ret = 0;
715
716 leaf = path->nodes[0];
717 ref = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_ref);
718 num_refs = btrfs_ref_num_refs(leaf, ref);
719 BUG_ON(num_refs == 0);
720 num_refs -= 1;
721 if (num_refs == 0) {
722 ret = btrfs_del_item(trans, root, path);
723 } else {
724 btrfs_set_ref_num_refs(leaf, ref, num_refs);
725 btrfs_mark_buffer_dirty(leaf);
726 }
727 btrfs_release_path(root, path);
728 return ret;
729}
730
731static int __btrfs_update_extent_ref(struct btrfs_trans_handle *trans,
732 struct btrfs_root *root, u64 bytenr,
733 u64 orig_parent, u64 parent,
734 u64 orig_root, u64 ref_root,
735 u64 orig_generation, u64 ref_generation,
736 u64 owner_objectid, u64 owner_offset)
737{
738 int ret;
739 struct btrfs_root *extent_root = root->fs_info->extent_root;
740 struct btrfs_path *path;
741
742 if (root == root->fs_info->extent_root) {
743 struct pending_extent_op *extent_op;
744 u64 num_bytes;
745
746 BUG_ON(owner_objectid >= BTRFS_MAX_LEVEL);
747 num_bytes = btrfs_level_size(root, (int)owner_objectid);
748 if (test_range_bit(&root->fs_info->extent_ins, bytenr,
749 bytenr + num_bytes - 1, EXTENT_LOCKED, 0)) {
750 u64 priv;
751 ret = get_state_private(&root->fs_info->extent_ins,
752 bytenr, &priv);
753 BUG_ON(ret);
754 extent_op = (struct pending_extent_op *)
755 (unsigned long)priv;
756 BUG_ON(extent_op->parent != orig_parent);
757 BUG_ON(extent_op->generation != orig_generation);
758 extent_op->parent = parent;
759 extent_op->generation = ref_generation;
760 } else {
761 extent_op = kmalloc(sizeof(*extent_op), GFP_NOFS);
762 BUG_ON(!extent_op);
763
764 extent_op->type = PENDING_BACKREF_UPDATE;
765 extent_op->bytenr = bytenr;
766 extent_op->num_bytes = num_bytes;
767 extent_op->parent = parent;
768 extent_op->orig_parent = orig_parent;
769 extent_op->generation = ref_generation;
770 extent_op->orig_generation = orig_generation;
771 extent_op->level = (int)owner_objectid;
772
773 set_extent_bits(&root->fs_info->extent_ins,
774 bytenr, bytenr + num_bytes - 1,
775 EXTENT_LOCKED, GFP_NOFS);
776 set_state_private(&root->fs_info->extent_ins,
777 bytenr, (unsigned long)extent_op);
778 }
779 return 0;
780 }
781
782 path = btrfs_alloc_path();
783 if (!path)
784 return -ENOMEM;
785 ret = lookup_extent_backref(trans, extent_root, path,
786 bytenr, orig_parent, orig_root,
787 orig_generation, 1);
788 if (ret)
789 goto out;
790 ret = remove_extent_backref(trans, extent_root, path);
791 if (ret)
792 goto out;
793 ret = insert_extent_backref(trans, extent_root, path, bytenr,
794 parent, ref_root, ref_generation,
795 owner_objectid, owner_offset);
796 BUG_ON(ret);
797 finish_current_insert(trans, extent_root);
798 del_pending_extents(trans, extent_root);
799out:
800 btrfs_free_path(path);
801 return ret;
802}
803
804int btrfs_update_extent_ref(struct btrfs_trans_handle *trans,
805 struct btrfs_root *root, u64 bytenr,
806 u64 orig_parent, u64 parent,
807 u64 ref_root, u64 ref_generation,
808 u64 owner_objectid, u64 owner_offset)
809{
810 int ret;
811 if (ref_root == BTRFS_TREE_LOG_OBJECTID &&
812 owner_objectid < BTRFS_FIRST_FREE_OBJECTID)
813 return 0;
814 maybe_lock_mutex(root);
815 ret = __btrfs_update_extent_ref(trans, root, bytenr, orig_parent,
816 parent, ref_root, ref_root,
817 ref_generation, ref_generation,
818 owner_objectid, owner_offset);
819 maybe_unlock_mutex(root);
820 return ret;
821}
822
742static int __btrfs_inc_extent_ref(struct btrfs_trans_handle *trans,
823static int __btrfs_inc_extent_ref(struct btrfs_trans_handle *trans,
743 struct btrfs_root *root,
744 u64 bytenr, u64 num_bytes,
745 u64 root_objectid, u64 ref_generation,
746 u64 owner, u64 owner_offset)
824 struct btrfs_root *root, u64 bytenr,
825 u64 orig_parent, u64 parent,
826 u64 orig_root, u64 ref_root,
827 u64 orig_generation, u64 ref_generation,
828 u64 owner_objectid, u64 owner_offset)
747{
748 struct btrfs_path *path;
749 int ret;
750 struct btrfs_key key;
751 struct extent_buffer *l;
752 struct btrfs_extent_item *item;
753 u32 refs;
754
829{
830 struct btrfs_path *path;
831 int ret;
832 struct btrfs_key key;
833 struct extent_buffer *l;
834 struct btrfs_extent_item *item;
835 u32 refs;
836
755 WARN_ON(num_bytes < root->sectorsize);
756 path = btrfs_alloc_path();
757 if (!path)
758 return -ENOMEM;
759
760 path->reada = 1;
761 key.objectid = bytenr;
837 path = btrfs_alloc_path();
838 if (!path)
839 return -ENOMEM;
840
841 path->reada = 1;
842 key.objectid = bytenr;
762 btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
763 key.offset = num_bytes;
843 key.type = BTRFS_EXTENT_ITEM_KEY;
844 key.offset = (u64)-1;
845
764 ret = btrfs_search_slot(trans, root->fs_info->extent_root, &key, path,
765 0, 1);
766 if (ret < 0)
767 return ret;
846 ret = btrfs_search_slot(trans, root->fs_info->extent_root, &key, path,
847 0, 1);
848 if (ret < 0)
849 return ret;
768 if (ret != 0) {
769 BUG();
770 }
771 BUG_ON(ret != 0);
850 BUG_ON(ret == 0 || path->slots[0] == 0);
851
852 path->slots[0]--;
772 l = path->nodes[0];
853 l = path->nodes[0];
854
855 btrfs_item_key_to_cpu(l, &key, path->slots[0]);
856 BUG_ON(key.objectid != bytenr);
857 BUG_ON(key.type != BTRFS_EXTENT_ITEM_KEY);
858
773 item = btrfs_item_ptr(l, path->slots[0], struct btrfs_extent_item);
774 refs = btrfs_extent_refs(l, item);
775 btrfs_set_extent_refs(l, item, refs + 1);
776 btrfs_mark_buffer_dirty(path->nodes[0]);
777
778 btrfs_release_path(root->fs_info->extent_root, path);
779
780 path->reada = 1;
859 item = btrfs_item_ptr(l, path->slots[0], struct btrfs_extent_item);
860 refs = btrfs_extent_refs(l, item);
861 btrfs_set_extent_refs(l, item, refs + 1);
862 btrfs_mark_buffer_dirty(path->nodes[0]);
863
864 btrfs_release_path(root->fs_info->extent_root, path);
865
866 path->reada = 1;
781 ret = btrfs_insert_extent_backref(trans, root->fs_info->extent_root,
782 path, bytenr, root_objectid,
783 ref_generation, owner, owner_offset);
867 ret = insert_extent_backref(trans, root->fs_info->extent_root,
868 path, bytenr, parent,
869 ref_root, ref_generation,
870 owner_objectid, owner_offset);
784 BUG_ON(ret);
785 finish_current_insert(trans, root->fs_info->extent_root);
786 del_pending_extents(trans, root->fs_info->extent_root);
787
788 btrfs_free_path(path);
789 return 0;
790}
791
792int btrfs_inc_extent_ref(struct btrfs_trans_handle *trans,
871 BUG_ON(ret);
872 finish_current_insert(trans, root->fs_info->extent_root);
873 del_pending_extents(trans, root->fs_info->extent_root);
874
875 btrfs_free_path(path);
876 return 0;
877}
878
879int btrfs_inc_extent_ref(struct btrfs_trans_handle *trans,
793 struct btrfs_root *root,
794 u64 bytenr, u64 num_bytes,
795 u64 root_objectid, u64 ref_generation,
796 u64 owner, u64 owner_offset)
880 struct btrfs_root *root,
881 u64 bytenr, u64 num_bytes, u64 parent,
882 u64 ref_root, u64 ref_generation,
883 u64 owner_objectid, u64 owner_offset)
797{
798 int ret;
884{
885 int ret;
799
800 mutex_lock(&root->fs_info->alloc_mutex);
801 ret = __btrfs_inc_extent_ref(trans, root, bytenr, num_bytes,
802 root_objectid, ref_generation,
803 owner, owner_offset);
804 mutex_unlock(&root->fs_info->alloc_mutex);
886 if (ref_root == BTRFS_TREE_LOG_OBJECTID &&
887 owner_objectid < BTRFS_FIRST_FREE_OBJECTID)
888 return 0;
889 maybe_lock_mutex(root);
890 ret = __btrfs_inc_extent_ref(trans, root, bytenr, 0, parent,
891 0, ref_root, 0, ref_generation,
892 owner_objectid, owner_offset);
893 maybe_unlock_mutex(root);
805 return ret;
806}
807
808int btrfs_extent_post_op(struct btrfs_trans_handle *trans,
809 struct btrfs_root *root)
810{
811 finish_current_insert(trans, root->fs_info->extent_root);
812 del_pending_extents(trans, root->fs_info->extent_root);
813 return 0;
814}
815
894 return ret;
895}
896
897int btrfs_extent_post_op(struct btrfs_trans_handle *trans,
898 struct btrfs_root *root)
899{
900 finish_current_insert(trans, root->fs_info->extent_root);
901 del_pending_extents(trans, root->fs_info->extent_root);
902 return 0;
903}
904
816static int lookup_extent_ref(struct btrfs_trans_handle *trans,
817 struct btrfs_root *root, u64 bytenr,
818 u64 num_bytes, u32 *refs)
905int btrfs_lookup_extent_ref(struct btrfs_trans_handle *trans,
906 struct btrfs_root *root, u64 bytenr,
907 u64 num_bytes, u32 *refs)
819{
820 struct btrfs_path *path;
821 int ret;
822 struct btrfs_key key;
823 struct extent_buffer *l;
824 struct btrfs_extent_item *item;
825
826 WARN_ON(num_bytes < root->sectorsize);

--- 14 unchanged lines hidden (view full) ---

841 l = path->nodes[0];
842 item = btrfs_item_ptr(l, path->slots[0], struct btrfs_extent_item);
843 *refs = btrfs_extent_refs(l, item);
844out:
845 btrfs_free_path(path);
846 return 0;
847}
848
908{
909 struct btrfs_path *path;
910 int ret;
911 struct btrfs_key key;
912 struct extent_buffer *l;
913 struct btrfs_extent_item *item;
914
915 WARN_ON(num_bytes < root->sectorsize);

--- 14 unchanged lines hidden (view full) ---

930 l = path->nodes[0];
931 item = btrfs_item_ptr(l, path->slots[0], struct btrfs_extent_item);
932 *refs = btrfs_extent_refs(l, item);
933out:
934 btrfs_free_path(path);
935 return 0;
936}
937
849
850static int get_reference_status(struct btrfs_root *root, u64 bytenr,
851 u64 parent_gen, u64 ref_objectid,
852 u64 *min_generation, u32 *ref_count)
853{
854 struct btrfs_root *extent_root = root->fs_info->extent_root;
855 struct btrfs_path *path;
856 struct extent_buffer *leaf;
857 struct btrfs_extent_ref *ref_item;
858 struct btrfs_key key;
859 struct btrfs_key found_key;
860 u64 root_objectid = root->root_key.objectid;
861 u64 ref_generation;
862 u32 nritems;
863 int ret;
864
865 key.objectid = bytenr;
938static int get_reference_status(struct btrfs_root *root, u64 bytenr,
939 u64 parent_gen, u64 ref_objectid,
940 u64 *min_generation, u32 *ref_count)
941{
942 struct btrfs_root *extent_root = root->fs_info->extent_root;
943 struct btrfs_path *path;
944 struct extent_buffer *leaf;
945 struct btrfs_extent_ref *ref_item;
946 struct btrfs_key key;
947 struct btrfs_key found_key;
948 u64 root_objectid = root->root_key.objectid;
949 u64 ref_generation;
950 u32 nritems;
951 int ret;
952
953 key.objectid = bytenr;
866 key.offset = 0;
954 key.offset = (u64)-1;
867 key.type = BTRFS_EXTENT_ITEM_KEY;
868
869 path = btrfs_alloc_path();
870 mutex_lock(&root->fs_info->alloc_mutex);
871 ret = btrfs_search_slot(NULL, extent_root, &key, path, 0, 0);
872 if (ret < 0)
873 goto out;
874 BUG_ON(ret == 0);
955 key.type = BTRFS_EXTENT_ITEM_KEY;
956
957 path = btrfs_alloc_path();
958 mutex_lock(&root->fs_info->alloc_mutex);
959 ret = btrfs_search_slot(NULL, extent_root, &key, path, 0, 0);
960 if (ret < 0)
961 goto out;
962 BUG_ON(ret == 0);
963 if (ret < 0 || path->slots[0] == 0)
964 goto out;
875
965
966 path->slots[0]--;
876 leaf = path->nodes[0];
877 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
878
879 if (found_key.objectid != bytenr ||
880 found_key.type != BTRFS_EXTENT_ITEM_KEY) {
881 ret = 1;
882 goto out;
883 }

--- 20 unchanged lines hidden (view full) ---

904 path->slots[0]++;
905 continue;
906 }
907
908 ref_item = btrfs_item_ptr(leaf, path->slots[0],
909 struct btrfs_extent_ref);
910 ref_generation = btrfs_ref_generation(leaf, ref_item);
911 /*
967 leaf = path->nodes[0];
968 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
969
970 if (found_key.objectid != bytenr ||
971 found_key.type != BTRFS_EXTENT_ITEM_KEY) {
972 ret = 1;
973 goto out;
974 }

--- 20 unchanged lines hidden (view full) ---

995 path->slots[0]++;
996 continue;
997 }
998
999 ref_item = btrfs_item_ptr(leaf, path->slots[0],
1000 struct btrfs_extent_ref);
1001 ref_generation = btrfs_ref_generation(leaf, ref_item);
1002 /*
912 * For (parent_gen > 0 && parent_gen > ref_gen):
1003 * For (parent_gen > 0 && parent_gen > ref_generation):
913 *
914 * we reach here through the oldest root, therefore
915 * all other reference from same snapshot should have
916 * a larger generation.
917 */
918 if ((root_objectid != btrfs_ref_root(leaf, ref_item)) ||
919 (parent_gen > 0 && parent_gen > ref_generation) ||
920 (ref_objectid >= BTRFS_FIRST_FREE_OBJECTID &&
921 ref_objectid != btrfs_ref_objectid(leaf, ref_item))) {
1004 *
1005 * we reach here through the oldest root, therefore
1006 * all other reference from same snapshot should have
1007 * a larger generation.
1008 */
1009 if ((root_objectid != btrfs_ref_root(leaf, ref_item)) ||
1010 (parent_gen > 0 && parent_gen > ref_generation) ||
1011 (ref_objectid >= BTRFS_FIRST_FREE_OBJECTID &&
1012 ref_objectid != btrfs_ref_objectid(leaf, ref_item))) {
922 if (ref_count)
923 *ref_count = 2;
1013 *ref_count = 2;
924 break;
925 }
926
927 *ref_count = 1;
928 if (*min_generation > ref_generation)
929 *min_generation = ref_generation;
930
931 path->slots[0]++;

--- 83 unchanged lines hidden (view full) ---

1015 }
1016 ret = 0;
1017out:
1018 if (path)
1019 btrfs_free_path(path);
1020 return ret;
1021}
1022
1014 break;
1015 }
1016
1017 *ref_count = 1;
1018 if (*min_generation > ref_generation)
1019 *min_generation = ref_generation;
1020
1021 path->slots[0]++;

--- 83 unchanged lines hidden (view full) ---

1105 }
1106 ret = 0;
1107out:
1108 if (path)
1109 btrfs_free_path(path);
1110 return ret;
1111}
1112
1023int btrfs_inc_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root,
1024 struct extent_buffer *buf, int cache_ref)
1113int btrfs_cache_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root,
1114 struct extent_buffer *buf, u32 nr_extents)
1025{
1115{
1026 u64 bytenr;
1027 u32 nritems;
1028 struct btrfs_key key;
1029 struct btrfs_file_extent_item *fi;
1030 int i;
1031 int level;
1116 u32 nritems;
1117 struct btrfs_key key;
1118 struct btrfs_file_extent_item *fi;
1119 int i;
1120 int level;
1032 int ret;
1033 int faili;
1034 int nr_file_extents = 0;
1121 int ret = 0;
1035
1036 if (!root->ref_cows)
1037 return 0;
1038
1039 level = btrfs_header_level(buf);
1040 nritems = btrfs_header_nritems(buf);
1122
1123 if (!root->ref_cows)
1124 return 0;
1125
1126 level = btrfs_header_level(buf);
1127 nritems = btrfs_header_nritems(buf);
1041 for (i = 0; i < nritems; i++) {
1042 cond_resched();
1043 if (level == 0) {
1044 u64 disk_bytenr;
1045 btrfs_item_key_to_cpu(buf, &key, i);
1046 if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
1047 continue;
1048 fi = btrfs_item_ptr(buf, i,
1049 struct btrfs_file_extent_item);
1050 if (btrfs_file_extent_type(buf, fi) ==
1051 BTRFS_FILE_EXTENT_INLINE)
1052 continue;
1053 disk_bytenr = btrfs_file_extent_disk_bytenr(buf, fi);
1054 if (disk_bytenr == 0)
1055 continue;
1056
1128
1057 if (buf != root->commit_root)
1058 nr_file_extents++;
1059
1060 mutex_lock(&root->fs_info->alloc_mutex);
1061 ret = __btrfs_inc_extent_ref(trans, root, disk_bytenr,
1062 btrfs_file_extent_disk_num_bytes(buf, fi),
1063 root->root_key.objectid, trans->transid,
1064 key.objectid, key.offset);
1065 mutex_unlock(&root->fs_info->alloc_mutex);
1066 if (ret) {
1067 faili = i;
1068 WARN_ON(1);
1069 goto fail;
1070 }
1071 } else {
1072 bytenr = btrfs_node_blockptr(buf, i);
1073 btrfs_node_key_to_cpu(buf, &key, i);
1074
1075 mutex_lock(&root->fs_info->alloc_mutex);
1076 ret = __btrfs_inc_extent_ref(trans, root, bytenr,
1077 btrfs_level_size(root, level - 1),
1078 root->root_key.objectid,
1079 trans->transid,
1080 level - 1, key.objectid);
1081 mutex_unlock(&root->fs_info->alloc_mutex);
1082 if (ret) {
1083 faili = i;
1084 WARN_ON(1);
1085 goto fail;
1086 }
1087 }
1088 }
1089 /* cache orignal leaf block's references */
1090 if (level == 0 && cache_ref && buf != root->commit_root) {
1129 if (level == 0) {
1091 struct btrfs_leaf_ref *ref;
1092 struct btrfs_extent_info *info;
1093
1130 struct btrfs_leaf_ref *ref;
1131 struct btrfs_extent_info *info;
1132
1094 ref = btrfs_alloc_leaf_ref(root, nr_file_extents);
1133 ref = btrfs_alloc_leaf_ref(root, nr_extents);
1095 if (!ref) {
1134 if (!ref) {
1096 WARN_ON(1);
1135 ret = -ENOMEM;
1097 goto out;
1098 }
1099
1100 ref->root_gen = root->root_key.offset;
1101 ref->bytenr = buf->start;
1102 ref->owner = btrfs_header_owner(buf);
1103 ref->generation = btrfs_header_generation(buf);
1136 goto out;
1137 }
1138
1139 ref->root_gen = root->root_key.offset;
1140 ref->bytenr = buf->start;
1141 ref->owner = btrfs_header_owner(buf);
1142 ref->generation = btrfs_header_generation(buf);
1104 ref->nritems = nr_file_extents;
1143 ref->nritems = nr_extents;
1105 info = ref->extents;
1106
1144 info = ref->extents;
1145
1107 for (i = 0; nr_file_extents > 0 && i < nritems; i++) {
1146 for (i = 0; nr_extents > 0 && i < nritems; i++) {
1108 u64 disk_bytenr;
1109 btrfs_item_key_to_cpu(buf, &key, i);
1110 if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
1111 continue;
1112 fi = btrfs_item_ptr(buf, i,
1113 struct btrfs_file_extent_item);
1114 if (btrfs_file_extent_type(buf, fi) ==
1115 BTRFS_FILE_EXTENT_INLINE)

--- 11 unchanged lines hidden (view full) ---

1127 }
1128
1129 BUG_ON(!root->ref_tree);
1130 ret = btrfs_add_leaf_ref(root, ref);
1131 WARN_ON(ret);
1132 btrfs_free_leaf_ref(root, ref);
1133 }
1134out:
1147 u64 disk_bytenr;
1148 btrfs_item_key_to_cpu(buf, &key, i);
1149 if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
1150 continue;
1151 fi = btrfs_item_ptr(buf, i,
1152 struct btrfs_file_extent_item);
1153 if (btrfs_file_extent_type(buf, fi) ==
1154 BTRFS_FILE_EXTENT_INLINE)

--- 11 unchanged lines hidden (view full) ---

1166 }
1167
1168 BUG_ON(!root->ref_tree);
1169 ret = btrfs_add_leaf_ref(root, ref);
1170 WARN_ON(ret);
1171 btrfs_free_leaf_ref(root, ref);
1172 }
1173out:
1135 return 0;
1136fail:
1137 WARN_ON(1);
1138#if 0
1139 for (i =0; i < faili; i++) {
1174 return ret;
1175}
1176
1177int btrfs_inc_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root,
1178 struct extent_buffer *orig_buf, struct extent_buffer *buf,
1179 u32 *nr_extents)
1180{
1181 u64 bytenr;
1182 u64 ref_root;
1183 u64 orig_root;
1184 u64 ref_generation;
1185 u64 orig_generation;
1186 u32 nritems;
1187 u32 nr_file_extents = 0;
1188 struct btrfs_key key;
1189 struct btrfs_file_extent_item *fi;
1190 int i;
1191 int level;
1192 int ret = 0;
1193 int faili = 0;
1194 int (*process_func)(struct btrfs_trans_handle *, struct btrfs_root *,
1195 u64, u64, u64, u64, u64, u64, u64, u64, u64);
1196
1197 ref_root = btrfs_header_owner(buf);
1198 ref_generation = btrfs_header_generation(buf);
1199 orig_root = btrfs_header_owner(orig_buf);
1200 orig_generation = btrfs_header_generation(orig_buf);
1201
1202 nritems = btrfs_header_nritems(buf);
1203 level = btrfs_header_level(buf);
1204
1205 if (root->ref_cows) {
1206 process_func = __btrfs_inc_extent_ref;
1207 } else {
1208 if (level == 0 &&
1209 root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID)
1210 goto out;
1211 if (level != 0 &&
1212 root->root_key.objectid == BTRFS_TREE_LOG_OBJECTID)
1213 goto out;
1214 process_func = __btrfs_update_extent_ref;
1215 }
1216
1217 for (i = 0; i < nritems; i++) {
1218 cond_resched();
1140 if (level == 0) {
1219 if (level == 0) {
1141 u64 disk_bytenr;
1142 btrfs_item_key_to_cpu(buf, &key, i);
1143 if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
1144 continue;
1145 fi = btrfs_item_ptr(buf, i,
1146 struct btrfs_file_extent_item);
1147 if (btrfs_file_extent_type(buf, fi) ==
1148 BTRFS_FILE_EXTENT_INLINE)
1149 continue;
1220 btrfs_item_key_to_cpu(buf, &key, i);
1221 if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
1222 continue;
1223 fi = btrfs_item_ptr(buf, i,
1224 struct btrfs_file_extent_item);
1225 if (btrfs_file_extent_type(buf, fi) ==
1226 BTRFS_FILE_EXTENT_INLINE)
1227 continue;
1150 disk_bytenr = btrfs_file_extent_disk_bytenr(buf, fi);
1151 if (disk_bytenr == 0)
1228 bytenr = btrfs_file_extent_disk_bytenr(buf, fi);
1229 if (bytenr == 0)
1152 continue;
1230 continue;
1153 err = btrfs_free_extent(trans, root, disk_bytenr,
1154 btrfs_file_extent_disk_num_bytes(buf,
1155 fi), 0);
1156 BUG_ON(err);
1231
1232 nr_file_extents++;
1233
1234 maybe_lock_mutex(root);
1235 ret = process_func(trans, root, bytenr,
1236 orig_buf->start, buf->start,
1237 orig_root, ref_root,
1238 orig_generation, ref_generation,
1239 key.objectid, key.offset);
1240 maybe_unlock_mutex(root);
1241
1242 if (ret) {
1243 faili = i;
1244 WARN_ON(1);
1245 goto fail;
1246 }
1157 } else {
1158 bytenr = btrfs_node_blockptr(buf, i);
1247 } else {
1248 bytenr = btrfs_node_blockptr(buf, i);
1159 err = btrfs_free_extent(trans, root, bytenr,
1160 btrfs_level_size(root, level - 1), 0);
1161 BUG_ON(err);
1249 maybe_lock_mutex(root);
1250 ret = process_func(trans, root, bytenr,
1251 orig_buf->start, buf->start,
1252 orig_root, ref_root,
1253 orig_generation, ref_generation,
1254 level - 1, 0);
1255 maybe_unlock_mutex(root);
1256 if (ret) {
1257 faili = i;
1258 WARN_ON(1);
1259 goto fail;
1260 }
1162 }
1163 }
1261 }
1262 }
1164#endif
1263out:
1264 if (nr_extents) {
1265 if (level == 0)
1266 *nr_extents = nr_file_extents;
1267 else
1268 *nr_extents = nritems;
1269 }
1270 return 0;
1271fail:
1272 WARN_ON(1);
1165 return ret;
1166}
1167
1273 return ret;
1274}
1275
1276int btrfs_update_ref(struct btrfs_trans_handle *trans,
1277 struct btrfs_root *root, struct extent_buffer *orig_buf,
1278 struct extent_buffer *buf, int start_slot, int nr)
1279
1280{
1281 u64 bytenr;
1282 u64 ref_root;
1283 u64 orig_root;
1284 u64 ref_generation;
1285 u64 orig_generation;
1286 struct btrfs_key key;
1287 struct btrfs_file_extent_item *fi;
1288 int i;
1289 int ret;
1290 int slot;
1291 int level;
1292
1293 BUG_ON(start_slot < 0);
1294 BUG_ON(start_slot + nr > btrfs_header_nritems(buf));
1295
1296 ref_root = btrfs_header_owner(buf);
1297 ref_generation = btrfs_header_generation(buf);
1298 orig_root = btrfs_header_owner(orig_buf);
1299 orig_generation = btrfs_header_generation(orig_buf);
1300 level = btrfs_header_level(buf);
1301
1302 if (!root->ref_cows) {
1303 if (level == 0 &&
1304 root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID)
1305 return 0;
1306 if (level != 0 &&
1307 root->root_key.objectid == BTRFS_TREE_LOG_OBJECTID)
1308 return 0;
1309 }
1310
1311 for (i = 0, slot = start_slot; i < nr; i++, slot++) {
1312 cond_resched();
1313 if (level == 0) {
1314 btrfs_item_key_to_cpu(buf, &key, slot);
1315 if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
1316 continue;
1317 fi = btrfs_item_ptr(buf, slot,
1318 struct btrfs_file_extent_item);
1319 if (btrfs_file_extent_type(buf, fi) ==
1320 BTRFS_FILE_EXTENT_INLINE)
1321 continue;
1322 bytenr = btrfs_file_extent_disk_bytenr(buf, fi);
1323 if (bytenr == 0)
1324 continue;
1325 maybe_lock_mutex(root);
1326 ret = __btrfs_update_extent_ref(trans, root, bytenr,
1327 orig_buf->start, buf->start,
1328 orig_root, ref_root,
1329 orig_generation, ref_generation,
1330 key.objectid, key.offset);
1331 maybe_unlock_mutex(root);
1332 if (ret)
1333 goto fail;
1334 } else {
1335 bytenr = btrfs_node_blockptr(buf, slot);
1336 maybe_lock_mutex(root);
1337 ret = __btrfs_update_extent_ref(trans, root, bytenr,
1338 orig_buf->start, buf->start,
1339 orig_root, ref_root,
1340 orig_generation, ref_generation,
1341 level - 1, 0);
1342 maybe_unlock_mutex(root);
1343 if (ret)
1344 goto fail;
1345 }
1346 }
1347 return 0;
1348fail:
1349 WARN_ON(1);
1350 return -1;
1351}
1352
1168static int write_one_cache_group(struct btrfs_trans_handle *trans,
1169 struct btrfs_root *root,
1170 struct btrfs_path *path,
1171 struct btrfs_block_group_cache *cache)
1172{
1173 int ret;
1174 int pending_ret;
1175 struct btrfs_root *extent_root = root->fs_info->extent_root;

--- 358 unchanged lines hidden (view full) ---

1534 return 0;
1535}
1536
1537static int finish_current_insert(struct btrfs_trans_handle *trans,
1538 struct btrfs_root *extent_root)
1539{
1540 u64 start;
1541 u64 end;
1353static int write_one_cache_group(struct btrfs_trans_handle *trans,
1354 struct btrfs_root *root,
1355 struct btrfs_path *path,
1356 struct btrfs_block_group_cache *cache)
1357{
1358 int ret;
1359 int pending_ret;
1360 struct btrfs_root *extent_root = root->fs_info->extent_root;

--- 358 unchanged lines hidden (view full) ---

1719 return 0;
1720}
1721
1722static int finish_current_insert(struct btrfs_trans_handle *trans,
1723 struct btrfs_root *extent_root)
1724{
1725 u64 start;
1726 u64 end;
1727 u64 priv;
1542 struct btrfs_fs_info *info = extent_root->fs_info;
1728 struct btrfs_fs_info *info = extent_root->fs_info;
1543 struct extent_buffer *eb;
1544 struct btrfs_path *path;
1729 struct btrfs_path *path;
1545 struct btrfs_key ins;
1546 struct btrfs_disk_key first;
1730 struct btrfs_extent_ref *ref;
1731 struct pending_extent_op *extent_op;
1732 struct btrfs_key key;
1547 struct btrfs_extent_item extent_item;
1548 int ret;
1733 struct btrfs_extent_item extent_item;
1734 int ret;
1549 int level;
1550 int err = 0;
1551
1552 WARN_ON(!mutex_is_locked(&extent_root->fs_info->alloc_mutex));
1553 btrfs_set_stack_extent_refs(&extent_item, 1);
1735 int err = 0;
1736
1737 WARN_ON(!mutex_is_locked(&extent_root->fs_info->alloc_mutex));
1738 btrfs_set_stack_extent_refs(&extent_item, 1);
1554 btrfs_set_key_type(&ins, BTRFS_EXTENT_ITEM_KEY);
1555 path = btrfs_alloc_path();
1556
1557 while(1) {
1558 ret = find_first_extent_bit(&info->extent_ins, 0, &start,
1559 &end, EXTENT_LOCKED);
1560 if (ret)
1561 break;
1562
1739 path = btrfs_alloc_path();
1740
1741 while(1) {
1742 ret = find_first_extent_bit(&info->extent_ins, 0, &start,
1743 &end, EXTENT_LOCKED);
1744 if (ret)
1745 break;
1746
1563 ins.objectid = start;
1564 ins.offset = end + 1 - start;
1565 err = btrfs_insert_item(trans, extent_root, &ins,
1747 ret = get_state_private(&info->extent_ins, start, &priv);
1748 BUG_ON(ret);
1749 extent_op = (struct pending_extent_op *)(unsigned long)priv;
1750
1751 if (extent_op->type == PENDING_EXTENT_INSERT) {
1752 key.objectid = start;
1753 key.offset = end + 1 - start;
1754 key.type = BTRFS_EXTENT_ITEM_KEY;
1755 err = btrfs_insert_item(trans, extent_root, &key,
1566 &extent_item, sizeof(extent_item));
1756 &extent_item, sizeof(extent_item));
1567 clear_extent_bits(&info->extent_ins, start, end, EXTENT_LOCKED,
1568 GFP_NOFS);
1757 BUG_ON(err);
1569
1758
1570 eb = btrfs_find_create_tree_block(extent_root, ins.objectid,
1571 ins.offset);
1759 clear_extent_bits(&info->extent_ins, start, end,
1760 EXTENT_LOCKED, GFP_NOFS);
1572
1761
1573 if (!btrfs_buffer_uptodate(eb, trans->transid))
1574 btrfs_read_buffer(eb, trans->transid);
1762 err = insert_extent_backref(trans, extent_root, path,
1763 start, extent_op->parent,
1764 extent_root->root_key.objectid,
1765 extent_op->generation,
1766 extent_op->level, 0);
1767 BUG_ON(err);
1768 } else if (extent_op->type == PENDING_BACKREF_UPDATE) {
1769 err = lookup_extent_backref(trans, extent_root, path,
1770 start, extent_op->orig_parent,
1771 extent_root->root_key.objectid,
1772 extent_op->orig_generation, 0);
1773 BUG_ON(err);
1575
1774
1576 btrfs_tree_lock(eb);
1577 level = btrfs_header_level(eb);
1578 if (level == 0) {
1579 btrfs_item_key(eb, &first, 0);
1775 clear_extent_bits(&info->extent_ins, start, end,
1776 EXTENT_LOCKED, GFP_NOFS);
1777
1778 key.objectid = start;
1779 key.offset = extent_op->parent;
1780 key.type = BTRFS_EXTENT_REF_KEY;
1781 err = btrfs_set_item_key_safe(trans, extent_root, path,
1782 &key);
1783 BUG_ON(err);
1784 ref = btrfs_item_ptr(path->nodes[0], path->slots[0],
1785 struct btrfs_extent_ref);
1786 btrfs_set_ref_generation(path->nodes[0], ref,
1787 extent_op->generation);
1788 btrfs_mark_buffer_dirty(path->nodes[0]);
1789 btrfs_release_path(extent_root, path);
1580 } else {
1790 } else {
1581 btrfs_node_key(eb, &first, 0);
1791 BUG_ON(1);
1582 }
1792 }
1583 btrfs_tree_unlock(eb);
1584 free_extent_buffer(eb);
1585 /*
1586 * the first key is just a hint, so the race we've created
1587 * against reading it is fine
1588 */
1589 err = btrfs_insert_extent_backref(trans, extent_root, path,
1590 start, extent_root->root_key.objectid,
1591 0, level,
1592 btrfs_disk_key_objectid(&first));
1593 BUG_ON(err);
1793 kfree(extent_op);
1794
1594 if (need_resched()) {
1595 mutex_unlock(&extent_root->fs_info->alloc_mutex);
1596 cond_resched();
1597 mutex_lock(&extent_root->fs_info->alloc_mutex);
1598 }
1599 }
1600 btrfs_free_path(path);
1601 return 0;
1602}
1603
1795 if (need_resched()) {
1796 mutex_unlock(&extent_root->fs_info->alloc_mutex);
1797 cond_resched();
1798 mutex_lock(&extent_root->fs_info->alloc_mutex);
1799 }
1800 }
1801 btrfs_free_path(path);
1802 return 0;
1803}
1804
1604static int pin_down_bytes(struct btrfs_root *root, u64 bytenr, u32 num_bytes,
1605 int is_data, int pending)
1805static int pin_down_bytes(struct btrfs_trans_handle *trans,
1806 struct btrfs_root *root,
1807 u64 bytenr, u64 num_bytes, int is_data)
1606{
1607 int err = 0;
1808{
1809 int err = 0;
1810 struct extent_buffer *buf;
1608
1609 WARN_ON(!mutex_is_locked(&root->fs_info->alloc_mutex));
1811
1812 WARN_ON(!mutex_is_locked(&root->fs_info->alloc_mutex));
1610 if (!pending) {
1611 struct extent_buffer *buf;
1813 if (is_data)
1814 goto pinit;
1612
1815
1613 if (is_data)
1614 goto pinit;
1816 buf = btrfs_find_tree_block(root, bytenr, num_bytes);
1817 if (!buf)
1818 goto pinit;
1615
1819
1616 buf = btrfs_find_tree_block(root, bytenr, num_bytes);
1617 if (buf) {
1618 /* we can reuse a block if it hasn't been written
1619 * and it is from this transaction. We can't
1620 * reuse anything from the tree log root because
1621 * it has tiny sub-transactions.
1622 */
1623 if (btrfs_buffer_uptodate(buf, 0) &&
1624 btrfs_try_tree_lock(buf)) {
1625 u64 transid =
1626 root->fs_info->running_transaction->transid;
1627 u64 header_transid =
1628 btrfs_header_generation(buf);
1629 if (btrfs_header_owner(buf) !=
1630 BTRFS_TREE_LOG_OBJECTID &&
1631 header_transid == transid &&
1632 !btrfs_header_flag(buf,
1633 BTRFS_HEADER_FLAG_WRITTEN)) {
1634 clean_tree_block(NULL, root, buf);
1635 btrfs_tree_unlock(buf);
1636 free_extent_buffer(buf);
1637 return 1;
1638 }
1639 btrfs_tree_unlock(buf);
1640 }
1820 /* we can reuse a block if it hasn't been written
1821 * and it is from this transaction. We can't
1822 * reuse anything from the tree log root because
1823 * it has tiny sub-transactions.
1824 */
1825 if (btrfs_buffer_uptodate(buf, 0) &&
1826 btrfs_try_tree_lock(buf)) {
1827 u64 header_owner = btrfs_header_owner(buf);
1828 u64 header_transid = btrfs_header_generation(buf);
1829 if (header_owner != BTRFS_TREE_LOG_OBJECTID &&
1830 header_transid == trans->transid &&
1831 !btrfs_header_flag(buf, BTRFS_HEADER_FLAG_WRITTEN)) {
1832 clean_tree_block(NULL, root, buf);
1833 btrfs_tree_unlock(buf);
1641 free_extent_buffer(buf);
1834 free_extent_buffer(buf);
1835 return 1;
1642 }
1836 }
1643pinit:
1644 btrfs_update_pinned_extents(root, bytenr, num_bytes, 1);
1645 } else {
1646 set_extent_bits(&root->fs_info->pending_del,
1647 bytenr, bytenr + num_bytes - 1,
1648 EXTENT_LOCKED, GFP_NOFS);
1837 btrfs_tree_unlock(buf);
1649 }
1838 }
1839 free_extent_buffer(buf);
1840pinit:
1841 btrfs_update_pinned_extents(root, bytenr, num_bytes, 1);
1842
1650 BUG_ON(err < 0);
1651 return 0;
1652}
1653
1654/*
1655 * remove an extent from the root, returns 0 on success
1656 */
1843 BUG_ON(err < 0);
1844 return 0;
1845}
1846
1847/*
1848 * remove an extent from the root, returns 0 on success
1849 */
1657static int __free_extent(struct btrfs_trans_handle *trans, struct btrfs_root
1658 *root, u64 bytenr, u64 num_bytes,
1850static int __free_extent(struct btrfs_trans_handle *trans,
1851 struct btrfs_root *root,
1852 u64 bytenr, u64 num_bytes, u64 parent,
1659 u64 root_objectid, u64 ref_generation,
1853 u64 root_objectid, u64 ref_generation,
1660 u64 owner_objectid, u64 owner_offset, int pin,
1661 int mark_free)
1854 u64 owner_objectid, u64 owner_offset,
1855 int pin, int mark_free)
1662{
1663 struct btrfs_path *path;
1664 struct btrfs_key key;
1665 struct btrfs_fs_info *info = root->fs_info;
1666 struct btrfs_root *extent_root = info->extent_root;
1667 struct extent_buffer *leaf;
1668 int ret;
1669 int extent_slot = 0;

--- 6 unchanged lines hidden (view full) ---

1676 key.objectid = bytenr;
1677 btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
1678 key.offset = num_bytes;
1679 path = btrfs_alloc_path();
1680 if (!path)
1681 return -ENOMEM;
1682
1683 path->reada = 1;
1856{
1857 struct btrfs_path *path;
1858 struct btrfs_key key;
1859 struct btrfs_fs_info *info = root->fs_info;
1860 struct btrfs_root *extent_root = info->extent_root;
1861 struct extent_buffer *leaf;
1862 int ret;
1863 int extent_slot = 0;

--- 6 unchanged lines hidden (view full) ---

1870 key.objectid = bytenr;
1871 btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
1872 key.offset = num_bytes;
1873 path = btrfs_alloc_path();
1874 if (!path)
1875 return -ENOMEM;
1876
1877 path->reada = 1;
1684 ret = lookup_extent_backref(trans, extent_root, path,
1685 bytenr, root_objectid,
1686 ref_generation,
1687 owner_objectid, owner_offset, 1);
1878 ret = lookup_extent_backref(trans, extent_root, path, bytenr, parent,
1879 root_objectid, ref_generation, 1);
1688 if (ret == 0) {
1689 struct btrfs_key found_key;
1690 extent_slot = path->slots[0];
1691 while(extent_slot > 0) {
1692 extent_slot--;
1693 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
1694 extent_slot);
1695 if (found_key.objectid != bytenr)
1696 break;
1697 if (found_key.type == BTRFS_EXTENT_ITEM_KEY &&
1698 found_key.offset == num_bytes) {
1699 found_extent = 1;
1700 break;
1701 }
1702 if (path->slots[0] - extent_slot > 5)
1703 break;
1704 }
1880 if (ret == 0) {
1881 struct btrfs_key found_key;
1882 extent_slot = path->slots[0];
1883 while(extent_slot > 0) {
1884 extent_slot--;
1885 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
1886 extent_slot);
1887 if (found_key.objectid != bytenr)
1888 break;
1889 if (found_key.type == BTRFS_EXTENT_ITEM_KEY &&
1890 found_key.offset == num_bytes) {
1891 found_extent = 1;
1892 break;
1893 }
1894 if (path->slots[0] - extent_slot > 5)
1895 break;
1896 }
1705 if (!found_extent)
1706 ret = btrfs_del_item(trans, extent_root, path);
1897 if (!found_extent) {
1898 ret = remove_extent_backref(trans, extent_root, path);
1899 BUG_ON(ret);
1900 btrfs_release_path(extent_root, path);
1901 ret = btrfs_search_slot(trans, extent_root,
1902 &key, path, -1, 1);
1903 BUG_ON(ret);
1904 extent_slot = path->slots[0];
1905 }
1707 } else {
1708 btrfs_print_leaf(extent_root, path->nodes[0]);
1709 WARN_ON(1);
1710 printk("Unable to find ref byte nr %Lu root %Lu "
1711 " gen %Lu owner %Lu offset %Lu\n", bytenr,
1712 root_objectid, ref_generation, owner_objectid,
1713 owner_offset);
1714 }
1906 } else {
1907 btrfs_print_leaf(extent_root, path->nodes[0]);
1908 WARN_ON(1);
1909 printk("Unable to find ref byte nr %Lu root %Lu "
1910 " gen %Lu owner %Lu offset %Lu\n", bytenr,
1911 root_objectid, ref_generation, owner_objectid,
1912 owner_offset);
1913 }
1715 if (!found_extent) {
1716 btrfs_release_path(extent_root, path);
1717 ret = btrfs_search_slot(trans, extent_root, &key, path, -1, 1);
1718 if (ret < 0)
1719 return ret;
1720 BUG_ON(ret);
1721 extent_slot = path->slots[0];
1722 }
1723
1724 leaf = path->nodes[0];
1725 ei = btrfs_item_ptr(leaf, extent_slot,
1726 struct btrfs_extent_item);
1727 refs = btrfs_extent_refs(leaf, ei);
1728 BUG_ON(refs == 0);
1729 refs -= 1;
1730 btrfs_set_extent_refs(leaf, ei, refs);
1731
1732 btrfs_mark_buffer_dirty(leaf);
1733
1734 if (refs == 0 && found_extent && path->slots[0] == extent_slot + 1) {
1914
1915 leaf = path->nodes[0];
1916 ei = btrfs_item_ptr(leaf, extent_slot,
1917 struct btrfs_extent_item);
1918 refs = btrfs_extent_refs(leaf, ei);
1919 BUG_ON(refs == 0);
1920 refs -= 1;
1921 btrfs_set_extent_refs(leaf, ei, refs);
1922
1923 btrfs_mark_buffer_dirty(leaf);
1924
1925 if (refs == 0 && found_extent && path->slots[0] == extent_slot + 1) {
1926 struct btrfs_extent_ref *ref;
1927 ref = btrfs_item_ptr(leaf, path->slots[0],
1928 struct btrfs_extent_ref);
1929 BUG_ON(btrfs_ref_num_refs(leaf, ref) != 1);
1735 /* if the back ref and the extent are next to each other
1736 * they get deleted below in one shot
1737 */
1738 path->slots[0] = extent_slot;
1739 num_to_del = 2;
1740 } else if (found_extent) {
1741 /* otherwise delete the extent back ref */
1930 /* if the back ref and the extent are next to each other
1931 * they get deleted below in one shot
1932 */
1933 path->slots[0] = extent_slot;
1934 num_to_del = 2;
1935 } else if (found_extent) {
1936 /* otherwise delete the extent back ref */
1742 ret = btrfs_del_item(trans, extent_root, path);
1937 ret = remove_extent_backref(trans, extent_root, path);
1743 BUG_ON(ret);
1744 /* if refs are 0, we need to setup the path for deletion */
1745 if (refs == 0) {
1746 btrfs_release_path(extent_root, path);
1747 ret = btrfs_search_slot(trans, extent_root, &key, path,
1748 -1, 1);
1938 BUG_ON(ret);
1939 /* if refs are 0, we need to setup the path for deletion */
1940 if (refs == 0) {
1941 btrfs_release_path(extent_root, path);
1942 ret = btrfs_search_slot(trans, extent_root, &key, path,
1943 -1, 1);
1749 if (ret < 0)
1750 return ret;
1751 BUG_ON(ret);
1752 }
1753 }
1754
1755 if (refs == 0) {
1756 u64 super_used;
1757 u64 root_used;
1758#ifdef BIO_RW_DISCARD
1759 u64 map_length = num_bytes;
1760 struct btrfs_multi_bio *multi = NULL;
1761#endif
1762
1763 if (pin) {
1944 BUG_ON(ret);
1945 }
1946 }
1947
1948 if (refs == 0) {
1949 u64 super_used;
1950 u64 root_used;
1951#ifdef BIO_RW_DISCARD
1952 u64 map_length = num_bytes;
1953 struct btrfs_multi_bio *multi = NULL;
1954#endif
1955
1956 if (pin) {
1764 ret = pin_down_bytes(root, bytenr, num_bytes,
1765 owner_objectid >= BTRFS_FIRST_FREE_OBJECTID, 0);
1957 ret = pin_down_bytes(trans, root, bytenr, num_bytes,
1958 owner_objectid >= BTRFS_FIRST_FREE_OBJECTID);
1766 if (ret > 0)
1767 mark_free = 1;
1768 BUG_ON(ret < 0);
1769 }
1770
1771 /* block accounting for super block */
1772 spin_lock_irq(&info->delalloc_lock);
1773 super_used = btrfs_super_bytes_used(&info->super_copy);
1774 btrfs_set_super_bytes_used(&info->super_copy,
1775 super_used - num_bytes);
1776 spin_unlock_irq(&info->delalloc_lock);
1777
1778 /* block accounting for root item */
1779 root_used = btrfs_root_used(&root->root_item);
1780 btrfs_set_root_used(&root->root_item,
1781 root_used - num_bytes);
1782 ret = btrfs_del_items(trans, extent_root, path, path->slots[0],
1783 num_to_del);
1959 if (ret > 0)
1960 mark_free = 1;
1961 BUG_ON(ret < 0);
1962 }
1963
1964 /* block accounting for super block */
1965 spin_lock_irq(&info->delalloc_lock);
1966 super_used = btrfs_super_bytes_used(&info->super_copy);
1967 btrfs_set_super_bytes_used(&info->super_copy,
1968 super_used - num_bytes);
1969 spin_unlock_irq(&info->delalloc_lock);
1970
1971 /* block accounting for root item */
1972 root_used = btrfs_root_used(&root->root_item);
1973 btrfs_set_root_used(&root->root_item,
1974 root_used - num_bytes);
1975 ret = btrfs_del_items(trans, extent_root, path, path->slots[0],
1976 num_to_del);
1784 if (ret) {
1785 return ret;
1786 }
1977 BUG_ON(ret);
1787 ret = update_block_group(trans, root, bytenr, num_bytes, 0,
1788 mark_free);
1789 BUG_ON(ret);
1790
1791#ifdef BIO_RW_DISCARD
1792 /* Tell the block device(s) that the sectors can be discarded */
1793 ret = btrfs_map_block(&root->fs_info->mapping_tree, READ,
1794 bytenr, &map_length, &multi, 0);

--- 22 unchanged lines hidden (view full) ---

1817 * find all the blocks marked as pending in the radix tree and remove
1818 * them from the extent map
1819 */
1820static int del_pending_extents(struct btrfs_trans_handle *trans, struct
1821 btrfs_root *extent_root)
1822{
1823 int ret;
1824 int err = 0;
1978 ret = update_block_group(trans, root, bytenr, num_bytes, 0,
1979 mark_free);
1980 BUG_ON(ret);
1981
1982#ifdef BIO_RW_DISCARD
1983 /* Tell the block device(s) that the sectors can be discarded */
1984 ret = btrfs_map_block(&root->fs_info->mapping_tree, READ,
1985 bytenr, &map_length, &multi, 0);

--- 22 unchanged lines hidden (view full) ---

2008 * find all the blocks marked as pending in the radix tree and remove
2009 * them from the extent map
2010 */
2011static int del_pending_extents(struct btrfs_trans_handle *trans, struct
2012 btrfs_root *extent_root)
2013{
2014 int ret;
2015 int err = 0;
2016 int mark_free = 0;
1825 u64 start;
1826 u64 end;
2017 u64 start;
2018 u64 end;
2019 u64 priv;
1827 struct extent_io_tree *pending_del;
2020 struct extent_io_tree *pending_del;
1828 struct extent_io_tree *pinned_extents;
2021 struct extent_io_tree *extent_ins;
2022 struct pending_extent_op *extent_op;
1829
1830 WARN_ON(!mutex_is_locked(&extent_root->fs_info->alloc_mutex));
2023
2024 WARN_ON(!mutex_is_locked(&extent_root->fs_info->alloc_mutex));
2025 extent_ins = &extent_root->fs_info->extent_ins;
1831 pending_del = &extent_root->fs_info->pending_del;
2026 pending_del = &extent_root->fs_info->pending_del;
1832 pinned_extents = &extent_root->fs_info->pinned_extents;
1833
1834 while(1) {
1835 ret = find_first_extent_bit(pending_del, 0, &start, &end,
1836 EXTENT_LOCKED);
1837 if (ret)
1838 break;
2027
2028 while(1) {
2029 ret = find_first_extent_bit(pending_del, 0, &start, &end,
2030 EXTENT_LOCKED);
2031 if (ret)
2032 break;
2033
2034 ret = get_state_private(pending_del, start, &priv);
2035 BUG_ON(ret);
2036 extent_op = (struct pending_extent_op *)(unsigned long)priv;
2037
1839 clear_extent_bits(pending_del, start, end, EXTENT_LOCKED,
1840 GFP_NOFS);
2038 clear_extent_bits(pending_del, start, end, EXTENT_LOCKED,
2039 GFP_NOFS);
1841 if (!test_range_bit(&extent_root->fs_info->extent_ins,
1842 start, end, EXTENT_LOCKED, 0)) {
1843 btrfs_update_pinned_extents(extent_root, start,
1844 end + 1 - start, 1);
2040
2041 ret = pin_down_bytes(trans, extent_root, start,
2042 end + 1 - start, 0);
2043 mark_free = ret > 0;
2044 if (!test_range_bit(extent_ins, start, end,
2045 EXTENT_LOCKED, 0)) {
2046free_extent:
1845 ret = __free_extent(trans, extent_root,
2047 ret = __free_extent(trans, extent_root,
1846 start, end + 1 - start,
1847 extent_root->root_key.objectid,
1848 0, 0, 0, 0, 0);
2048 start, end + 1 - start,
2049 extent_op->orig_parent,
2050 extent_root->root_key.objectid,
2051 extent_op->orig_generation,
2052 extent_op->level, 0, 0, mark_free);
2053 kfree(extent_op);
1849 } else {
2054 } else {
1850 clear_extent_bits(&extent_root->fs_info->extent_ins,
1851 start, end, EXTENT_LOCKED, GFP_NOFS);
2055 kfree(extent_op);
2056 ret = get_state_private(extent_ins, start, &priv);
2057 BUG_ON(ret);
2058 extent_op = (struct pending_extent_op *)
2059 (unsigned long)priv;
2060
2061 clear_extent_bits(extent_ins, start, end,
2062 EXTENT_LOCKED, GFP_NOFS);
2063
2064 if (extent_op->type == PENDING_BACKREF_UPDATE)
2065 goto free_extent;
2066
2067 ret = update_block_group(trans, extent_root, start,
2068 end + 1 - start, 0, mark_free);
2069 BUG_ON(ret);
2070 kfree(extent_op);
1852 }
1853 if (ret)
1854 err = ret;
1855
1856 if (need_resched()) {
1857 mutex_unlock(&extent_root->fs_info->alloc_mutex);
1858 cond_resched();
1859 mutex_lock(&extent_root->fs_info->alloc_mutex);
1860 }
1861 }
1862 return err;
1863}
1864
1865/*
1866 * remove an extent from the root, returns 0 on success
1867 */
1868static int __btrfs_free_extent(struct btrfs_trans_handle *trans,
2071 }
2072 if (ret)
2073 err = ret;
2074
2075 if (need_resched()) {
2076 mutex_unlock(&extent_root->fs_info->alloc_mutex);
2077 cond_resched();
2078 mutex_lock(&extent_root->fs_info->alloc_mutex);
2079 }
2080 }
2081 return err;
2082}
2083
2084/*
2085 * remove an extent from the root, returns 0 on success
2086 */
2087static int __btrfs_free_extent(struct btrfs_trans_handle *trans,
1869 struct btrfs_root *root, u64 bytenr,
1870 u64 num_bytes, u64 root_objectid,
1871 u64 ref_generation, u64 owner_objectid,
1872 u64 owner_offset, int pin)
2088 struct btrfs_root *root,
2089 u64 bytenr, u64 num_bytes, u64 parent,
2090 u64 root_objectid, u64 ref_generation,
2091 u64 owner_objectid, u64 owner_offset, int pin)
1873{
1874 struct btrfs_root *extent_root = root->fs_info->extent_root;
1875 int pending_ret;
1876 int ret;
1877
1878 WARN_ON(num_bytes < root->sectorsize);
2092{
2093 struct btrfs_root *extent_root = root->fs_info->extent_root;
2094 int pending_ret;
2095 int ret;
2096
2097 WARN_ON(num_bytes < root->sectorsize);
1879 if (!root->ref_cows)
1880 ref_generation = 0;
1881
1882 if (root == extent_root) {
2098 if (root == extent_root) {
1883 pin_down_bytes(root, bytenr, num_bytes, 0, 1);
2099 struct pending_extent_op *extent_op;
2100
2101 extent_op = kmalloc(sizeof(*extent_op), GFP_NOFS);
2102 BUG_ON(!extent_op);
2103
2104 extent_op->type = PENDING_EXTENT_DELETE;
2105 extent_op->bytenr = bytenr;
2106 extent_op->num_bytes = num_bytes;
2107 extent_op->parent = parent;
2108 extent_op->orig_parent = parent;
2109 extent_op->generation = ref_generation;
2110 extent_op->orig_generation = ref_generation;
2111 extent_op->level = (int)owner_objectid;
2112
2113 set_extent_bits(&root->fs_info->pending_del,
2114 bytenr, bytenr + num_bytes - 1,
2115 EXTENT_LOCKED, GFP_NOFS);
2116 set_state_private(&root->fs_info->pending_del,
2117 bytenr, (unsigned long)extent_op);
1884 return 0;
1885 }
1886 /* if metadata always pin */
1887 if (owner_objectid < BTRFS_FIRST_FREE_OBJECTID) {
1888 if (root->root_key.objectid == BTRFS_TREE_LOG_OBJECTID) {
1889 struct btrfs_block_group_cache *cache;
1890
1891 /* btrfs_free_reserved_extent */

--- 4 unchanged lines hidden (view full) ---

1896 }
1897 pin = 1;
1898 }
1899
1900 /* if data pin when any transaction has committed this */
1901 if (ref_generation != trans->transid)
1902 pin = 1;
1903
2118 return 0;
2119 }
2120 /* if metadata always pin */
2121 if (owner_objectid < BTRFS_FIRST_FREE_OBJECTID) {
2122 if (root->root_key.objectid == BTRFS_TREE_LOG_OBJECTID) {
2123 struct btrfs_block_group_cache *cache;
2124
2125 /* btrfs_free_reserved_extent */

--- 4 unchanged lines hidden (view full) ---

2130 }
2131 pin = 1;
2132 }
2133
2134 /* if data pin when any transaction has committed this */
2135 if (ref_generation != trans->transid)
2136 pin = 1;
2137
1904 ret = __free_extent(trans, root, bytenr, num_bytes, root_objectid,
1905 ref_generation, owner_objectid, owner_offset,
1906 pin, pin == 0);
2138 ret = __free_extent(trans, root, bytenr, num_bytes, parent,
2139 root_objectid, ref_generation, owner_objectid,
2140 owner_offset, pin, pin == 0);
1907
1908 finish_current_insert(trans, root->fs_info->extent_root);
1909 pending_ret = del_pending_extents(trans, root->fs_info->extent_root);
1910 return ret ? ret : pending_ret;
1911}
1912
1913int btrfs_free_extent(struct btrfs_trans_handle *trans,
2141
2142 finish_current_insert(trans, root->fs_info->extent_root);
2143 pending_ret = del_pending_extents(trans, root->fs_info->extent_root);
2144 return ret ? ret : pending_ret;
2145}
2146
2147int btrfs_free_extent(struct btrfs_trans_handle *trans,
1914 struct btrfs_root *root, u64 bytenr,
1915 u64 num_bytes, u64 root_objectid,
1916 u64 ref_generation, u64 owner_objectid,
1917 u64 owner_offset, int pin)
2148 struct btrfs_root *root,
2149 u64 bytenr, u64 num_bytes, u64 parent,
2150 u64 root_objectid, u64 ref_generation,
2151 u64 owner_objectid, u64 owner_offset, int pin)
1918{
1919 int ret;
1920
1921 maybe_lock_mutex(root);
2152{
2153 int ret;
2154
2155 maybe_lock_mutex(root);
1922 ret = __btrfs_free_extent(trans, root, bytenr, num_bytes,
2156 ret = __btrfs_free_extent(trans, root, bytenr, num_bytes, parent,
1923 root_objectid, ref_generation,
1924 owner_objectid, owner_offset, pin);
1925 maybe_unlock_mutex(root);
1926 return ret;
1927}
1928
1929static u64 stripe_align(struct btrfs_root *root, u64 val)
1930{

--- 335 unchanged lines hidden (view full) ---

2266 ret = __btrfs_reserve_extent(trans, root, num_bytes, min_alloc_size,
2267 empty_size, hint_byte, search_end, ins,
2268 data);
2269 maybe_unlock_mutex(root);
2270 return ret;
2271}
2272
2273static int __btrfs_alloc_reserved_extent(struct btrfs_trans_handle *trans,
2157 root_objectid, ref_generation,
2158 owner_objectid, owner_offset, pin);
2159 maybe_unlock_mutex(root);
2160 return ret;
2161}
2162
2163static u64 stripe_align(struct btrfs_root *root, u64 val)
2164{

--- 335 unchanged lines hidden (view full) ---

2500 ret = __btrfs_reserve_extent(trans, root, num_bytes, min_alloc_size,
2501 empty_size, hint_byte, search_end, ins,
2502 data);
2503 maybe_unlock_mutex(root);
2504 return ret;
2505}
2506
2507static int __btrfs_alloc_reserved_extent(struct btrfs_trans_handle *trans,
2274 struct btrfs_root *root,
2508 struct btrfs_root *root, u64 parent,
2275 u64 root_objectid, u64 ref_generation,
2276 u64 owner, u64 owner_offset,
2277 struct btrfs_key *ins)
2278{
2279 int ret;
2280 int pending_ret;
2281 u64 super_used;
2282 u64 root_used;
2283 u64 num_bytes = ins->offset;
2284 u32 sizes[2];
2285 struct btrfs_fs_info *info = root->fs_info;
2286 struct btrfs_root *extent_root = info->extent_root;
2287 struct btrfs_extent_item *extent_item;
2288 struct btrfs_extent_ref *ref;
2289 struct btrfs_path *path;
2290 struct btrfs_key keys[2];
2291
2509 u64 root_objectid, u64 ref_generation,
2510 u64 owner, u64 owner_offset,
2511 struct btrfs_key *ins)
2512{
2513 int ret;
2514 int pending_ret;
2515 u64 super_used;
2516 u64 root_used;
2517 u64 num_bytes = ins->offset;
2518 u32 sizes[2];
2519 struct btrfs_fs_info *info = root->fs_info;
2520 struct btrfs_root *extent_root = info->extent_root;
2521 struct btrfs_extent_item *extent_item;
2522 struct btrfs_extent_ref *ref;
2523 struct btrfs_path *path;
2524 struct btrfs_key keys[2];
2525
2526 if (parent == 0)
2527 parent = ins->objectid;
2528
2292 /* block accounting for super block */
2293 spin_lock_irq(&info->delalloc_lock);
2294 super_used = btrfs_super_bytes_used(&info->super_copy);
2295 btrfs_set_super_bytes_used(&info->super_copy, super_used + num_bytes);
2296 spin_unlock_irq(&info->delalloc_lock);
2297
2298 /* block accounting for root item */
2299 root_used = btrfs_root_used(&root->root_item);
2300 btrfs_set_root_used(&root->root_item, root_used + num_bytes);
2301
2302 if (root == extent_root) {
2529 /* block accounting for super block */
2530 spin_lock_irq(&info->delalloc_lock);
2531 super_used = btrfs_super_bytes_used(&info->super_copy);
2532 btrfs_set_super_bytes_used(&info->super_copy, super_used + num_bytes);
2533 spin_unlock_irq(&info->delalloc_lock);
2534
2535 /* block accounting for root item */
2536 root_used = btrfs_root_used(&root->root_item);
2537 btrfs_set_root_used(&root->root_item, root_used + num_bytes);
2538
2539 if (root == extent_root) {
2540 struct pending_extent_op *extent_op;
2541
2542 extent_op = kmalloc(sizeof(*extent_op), GFP_NOFS);
2543 BUG_ON(!extent_op);
2544
2545 extent_op->type = PENDING_EXTENT_INSERT;
2546 extent_op->bytenr = ins->objectid;
2547 extent_op->num_bytes = ins->offset;
2548 extent_op->parent = parent;
2549 extent_op->orig_parent = 0;
2550 extent_op->generation = ref_generation;
2551 extent_op->orig_generation = 0;
2552 extent_op->level = (int)owner;
2553
2303 set_extent_bits(&root->fs_info->extent_ins, ins->objectid,
2304 ins->objectid + ins->offset - 1,
2305 EXTENT_LOCKED, GFP_NOFS);
2554 set_extent_bits(&root->fs_info->extent_ins, ins->objectid,
2555 ins->objectid + ins->offset - 1,
2556 EXTENT_LOCKED, GFP_NOFS);
2557 set_state_private(&root->fs_info->extent_ins,
2558 ins->objectid, (unsigned long)extent_op);
2306 goto update_block;
2307 }
2308
2309 memcpy(&keys[0], ins, sizeof(*ins));
2559 goto update_block;
2560 }
2561
2562 memcpy(&keys[0], ins, sizeof(*ins));
2310 keys[1].offset = hash_extent_ref(root_objectid, ref_generation,
2311 owner, owner_offset);
2312 keys[1].objectid = ins->objectid;
2313 keys[1].type = BTRFS_EXTENT_REF_KEY;
2563 keys[1].objectid = ins->objectid;
2564 keys[1].type = BTRFS_EXTENT_REF_KEY;
2565 keys[1].offset = parent;
2314 sizes[0] = sizeof(*extent_item);
2315 sizes[1] = sizeof(*ref);
2316
2317 path = btrfs_alloc_path();
2318 BUG_ON(!path);
2319
2320 ret = btrfs_insert_empty_items(trans, extent_root, path, keys,
2321 sizes, 2);

--- 4 unchanged lines hidden (view full) ---

2326 btrfs_set_extent_refs(path->nodes[0], extent_item, 1);
2327 ref = btrfs_item_ptr(path->nodes[0], path->slots[0] + 1,
2328 struct btrfs_extent_ref);
2329
2330 btrfs_set_ref_root(path->nodes[0], ref, root_objectid);
2331 btrfs_set_ref_generation(path->nodes[0], ref, ref_generation);
2332 btrfs_set_ref_objectid(path->nodes[0], ref, owner);
2333 btrfs_set_ref_offset(path->nodes[0], ref, owner_offset);
2566 sizes[0] = sizeof(*extent_item);
2567 sizes[1] = sizeof(*ref);
2568
2569 path = btrfs_alloc_path();
2570 BUG_ON(!path);
2571
2572 ret = btrfs_insert_empty_items(trans, extent_root, path, keys,
2573 sizes, 2);

--- 4 unchanged lines hidden (view full) ---

2578 btrfs_set_extent_refs(path->nodes[0], extent_item, 1);
2579 ref = btrfs_item_ptr(path->nodes[0], path->slots[0] + 1,
2580 struct btrfs_extent_ref);
2581
2582 btrfs_set_ref_root(path->nodes[0], ref, root_objectid);
2583 btrfs_set_ref_generation(path->nodes[0], ref, ref_generation);
2584 btrfs_set_ref_objectid(path->nodes[0], ref, owner);
2585 btrfs_set_ref_offset(path->nodes[0], ref, owner_offset);
2586 btrfs_set_ref_num_refs(path->nodes[0], ref, 1);
2334
2335 btrfs_mark_buffer_dirty(path->nodes[0]);
2336
2337 trans->alloc_exclude_start = 0;
2338 trans->alloc_exclude_nr = 0;
2339 btrfs_free_path(path);
2340 finish_current_insert(trans, extent_root);
2341 pending_ret = del_pending_extents(trans, extent_root);

--- 12 unchanged lines hidden (view full) ---

2354 ins->objectid, ins->offset);
2355 BUG();
2356 }
2357out:
2358 return ret;
2359}
2360
2361int btrfs_alloc_reserved_extent(struct btrfs_trans_handle *trans,
2587
2588 btrfs_mark_buffer_dirty(path->nodes[0]);
2589
2590 trans->alloc_exclude_start = 0;
2591 trans->alloc_exclude_nr = 0;
2592 btrfs_free_path(path);
2593 finish_current_insert(trans, extent_root);
2594 pending_ret = del_pending_extents(trans, extent_root);

--- 12 unchanged lines hidden (view full) ---

2607 ins->objectid, ins->offset);
2608 BUG();
2609 }
2610out:
2611 return ret;
2612}
2613
2614int btrfs_alloc_reserved_extent(struct btrfs_trans_handle *trans,
2362 struct btrfs_root *root,
2615 struct btrfs_root *root, u64 parent,
2363 u64 root_objectid, u64 ref_generation,
2364 u64 owner, u64 owner_offset,
2365 struct btrfs_key *ins)
2366{
2367 int ret;
2368
2369 if (root_objectid == BTRFS_TREE_LOG_OBJECTID)
2370 return 0;
2371 maybe_lock_mutex(root);
2616 u64 root_objectid, u64 ref_generation,
2617 u64 owner, u64 owner_offset,
2618 struct btrfs_key *ins)
2619{
2620 int ret;
2621
2622 if (root_objectid == BTRFS_TREE_LOG_OBJECTID)
2623 return 0;
2624 maybe_lock_mutex(root);
2372 ret = __btrfs_alloc_reserved_extent(trans, root, root_objectid,
2373 ref_generation, owner,
2374 owner_offset, ins);
2625 ret = __btrfs_alloc_reserved_extent(trans, root, parent,
2626 root_objectid, ref_generation,
2627 owner, owner_offset, ins);
2375 maybe_unlock_mutex(root);
2376 return ret;
2377}
2378
2379/*
2380 * this is used by the tree logging recovery code. It records that
2381 * an extent has been allocated and makes sure to clear the free
2382 * space cache bits as well
2383 */
2384int btrfs_alloc_logged_extent(struct btrfs_trans_handle *trans,
2628 maybe_unlock_mutex(root);
2629 return ret;
2630}
2631
2632/*
2633 * this is used by the tree logging recovery code. It records that
2634 * an extent has been allocated and makes sure to clear the free
2635 * space cache bits as well
2636 */
2637int btrfs_alloc_logged_extent(struct btrfs_trans_handle *trans,
2385 struct btrfs_root *root,
2638 struct btrfs_root *root, u64 parent,
2386 u64 root_objectid, u64 ref_generation,
2387 u64 owner, u64 owner_offset,
2388 struct btrfs_key *ins)
2389{
2390 int ret;
2391 struct btrfs_block_group_cache *block_group;
2392
2393 maybe_lock_mutex(root);
2394 block_group = btrfs_lookup_block_group(root->fs_info, ins->objectid);
2395 cache_block_group(root, block_group);
2396
2397 ret = btrfs_remove_free_space(block_group, ins->objectid, ins->offset);
2398 BUG_ON(ret);
2639 u64 root_objectid, u64 ref_generation,
2640 u64 owner, u64 owner_offset,
2641 struct btrfs_key *ins)
2642{
2643 int ret;
2644 struct btrfs_block_group_cache *block_group;
2645
2646 maybe_lock_mutex(root);
2647 block_group = btrfs_lookup_block_group(root->fs_info, ins->objectid);
2648 cache_block_group(root, block_group);
2649
2650 ret = btrfs_remove_free_space(block_group, ins->objectid, ins->offset);
2651 BUG_ON(ret);
2399
2400 ret = __btrfs_alloc_reserved_extent(trans, root, root_objectid,
2401 ref_generation, owner,
2402 owner_offset, ins);
2652 ret = __btrfs_alloc_reserved_extent(trans, root, parent,
2653 root_objectid, ref_generation,
2654 owner, owner_offset, ins);
2403 maybe_unlock_mutex(root);
2404 return ret;
2405}
2406
2407/*
2408 * finds a free extent and does all the dirty work required for allocation
2409 * returns the key for the extent through ins, and a tree buffer for
2410 * the first block of the extent through buf.
2411 *
2412 * returns 0 if everything worked, non-zero otherwise.
2413 */
2414int btrfs_alloc_extent(struct btrfs_trans_handle *trans,
2415 struct btrfs_root *root,
2655 maybe_unlock_mutex(root);
2656 return ret;
2657}
2658
2659/*
2660 * finds a free extent and does all the dirty work required for allocation
2661 * returns the key for the extent through ins, and a tree buffer for
2662 * the first block of the extent through buf.
2663 *
2664 * returns 0 if everything worked, non-zero otherwise.
2665 */
2666int btrfs_alloc_extent(struct btrfs_trans_handle *trans,
2667 struct btrfs_root *root,
2416 u64 num_bytes, u64 min_alloc_size,
2668 u64 num_bytes, u64 parent, u64 min_alloc_size,
2417 u64 root_objectid, u64 ref_generation,
2669 u64 root_objectid, u64 ref_generation,
2418 u64 owner, u64 owner_offset,
2670 u64 owner_objectid, u64 owner_offset,
2419 u64 empty_size, u64 hint_byte,
2420 u64 search_end, struct btrfs_key *ins, u64 data)
2421{
2422 int ret;
2423
2424 maybe_lock_mutex(root);
2425
2426 ret = __btrfs_reserve_extent(trans, root, num_bytes,
2427 min_alloc_size, empty_size, hint_byte,
2428 search_end, ins, data);
2429 BUG_ON(ret);
2430 if (root_objectid != BTRFS_TREE_LOG_OBJECTID) {
2671 u64 empty_size, u64 hint_byte,
2672 u64 search_end, struct btrfs_key *ins, u64 data)
2673{
2674 int ret;
2675
2676 maybe_lock_mutex(root);
2677
2678 ret = __btrfs_reserve_extent(trans, root, num_bytes,
2679 min_alloc_size, empty_size, hint_byte,
2680 search_end, ins, data);
2681 BUG_ON(ret);
2682 if (root_objectid != BTRFS_TREE_LOG_OBJECTID) {
2431 ret = __btrfs_alloc_reserved_extent(trans, root, root_objectid,
2432 ref_generation, owner,
2433 owner_offset, ins);
2683 ret = __btrfs_alloc_reserved_extent(trans, root, parent,
2684 root_objectid, ref_generation,
2685 owner_objectid, owner_offset, ins);
2434 BUG_ON(ret);
2435
2436 }
2437 maybe_unlock_mutex(root);
2438 return ret;
2439}
2440
2441struct extent_buffer *btrfs_init_new_buffer(struct btrfs_trans_handle *trans,

--- 21 unchanged lines hidden (view full) ---

2463}
2464
2465/*
2466 * helper function to allocate a block for a given tree
2467 * returns the tree buffer or NULL.
2468 */
2469struct extent_buffer *btrfs_alloc_free_block(struct btrfs_trans_handle *trans,
2470 struct btrfs_root *root,
2686 BUG_ON(ret);
2687
2688 }
2689 maybe_unlock_mutex(root);
2690 return ret;
2691}
2692
2693struct extent_buffer *btrfs_init_new_buffer(struct btrfs_trans_handle *trans,

--- 21 unchanged lines hidden (view full) ---

2715}
2716
2717/*
2718 * helper function to allocate a block for a given tree
2719 * returns the tree buffer or NULL.
2720 */
2721struct extent_buffer *btrfs_alloc_free_block(struct btrfs_trans_handle *trans,
2722 struct btrfs_root *root,
2471 u32 blocksize,
2723 u32 blocksize, u64 parent,
2472 u64 root_objectid,
2473 u64 ref_generation,
2724 u64 root_objectid,
2725 u64 ref_generation,
2474 u64 first_objectid,
2475 int level,
2476 u64 hint,
2477 u64 empty_size)
2478{
2479 struct btrfs_key ins;
2480 int ret;
2481 struct extent_buffer *buf;
2482
2726 int level,
2727 u64 hint,
2728 u64 empty_size)
2729{
2730 struct btrfs_key ins;
2731 int ret;
2732 struct extent_buffer *buf;
2733
2483 ret = btrfs_alloc_extent(trans, root, blocksize, blocksize,
2484 root_objectid, ref_generation,
2485 level, first_objectid, empty_size, hint,
2486 (u64)-1, &ins, 0);
2734 ret = btrfs_alloc_extent(trans, root, blocksize, parent, blocksize,
2735 root_objectid, ref_generation, level, 0,
2736 empty_size, hint, (u64)-1, &ins, 0);
2487 if (ret) {
2488 BUG_ON(ret > 0);
2489 return ERR_PTR(ret);
2490 }
2491
2492 buf = btrfs_init_new_buffer(trans, root, ins.objectid, blocksize);
2493 return buf;
2494}

--- 31 unchanged lines hidden (view full) ---

2526 */
2527 disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
2528 if (disk_bytenr == 0)
2529 continue;
2530
2531 mutex_lock(&root->fs_info->alloc_mutex);
2532 ret = __btrfs_free_extent(trans, root, disk_bytenr,
2533 btrfs_file_extent_disk_num_bytes(leaf, fi),
2737 if (ret) {
2738 BUG_ON(ret > 0);
2739 return ERR_PTR(ret);
2740 }
2741
2742 buf = btrfs_init_new_buffer(trans, root, ins.objectid, blocksize);
2743 return buf;
2744}

--- 31 unchanged lines hidden (view full) ---

2776 */
2777 disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
2778 if (disk_bytenr == 0)
2779 continue;
2780
2781 mutex_lock(&root->fs_info->alloc_mutex);
2782 ret = __btrfs_free_extent(trans, root, disk_bytenr,
2783 btrfs_file_extent_disk_num_bytes(leaf, fi),
2534 leaf_owner, leaf_generation,
2784 leaf->start, leaf_owner, leaf_generation,
2535 key.objectid, key.offset, 0);
2536 mutex_unlock(&root->fs_info->alloc_mutex);
2785 key.objectid, key.offset, 0);
2786 mutex_unlock(&root->fs_info->alloc_mutex);
2787 BUG_ON(ret);
2537
2538 atomic_inc(&root->fs_info->throttle_gen);
2539 wake_up(&root->fs_info->transaction_throttle);
2540 cond_resched();
2788
2789 atomic_inc(&root->fs_info->throttle_gen);
2790 wake_up(&root->fs_info->transaction_throttle);
2791 cond_resched();
2541
2542 BUG_ON(ret);
2543 }
2544 return 0;
2545}
2546
2547static int noinline cache_drop_leaf_ref(struct btrfs_trans_handle *trans,
2548 struct btrfs_root *root,
2549 struct btrfs_leaf_ref *ref)
2550{
2551 int i;
2552 int ret;
2553 struct btrfs_extent_info *info = ref->extents;
2554
2555 for (i = 0; i < ref->nritems; i++) {
2556 mutex_lock(&root->fs_info->alloc_mutex);
2792 }
2793 return 0;
2794}
2795
2796static int noinline cache_drop_leaf_ref(struct btrfs_trans_handle *trans,
2797 struct btrfs_root *root,
2798 struct btrfs_leaf_ref *ref)
2799{
2800 int i;
2801 int ret;
2802 struct btrfs_extent_info *info = ref->extents;
2803
2804 for (i = 0; i < ref->nritems; i++) {
2805 mutex_lock(&root->fs_info->alloc_mutex);
2557 ret = __btrfs_free_extent(trans, root,
2558 info->bytenr, info->num_bytes,
2559 ref->owner, ref->generation,
2560 info->objectid, info->offset, 0);
2806 ret = __btrfs_free_extent(trans, root, info->bytenr,
2807 info->num_bytes, ref->bytenr,
2808 ref->owner, ref->generation,
2809 info->objectid, info->offset, 0);
2561 mutex_unlock(&root->fs_info->alloc_mutex);
2562
2563 atomic_inc(&root->fs_info->throttle_gen);
2564 wake_up(&root->fs_info->transaction_throttle);
2565 cond_resched();
2566
2567 BUG_ON(ret);
2568 info++;
2569 }
2570
2571 return 0;
2572}
2573
2574int drop_snap_lookup_refcount(struct btrfs_root *root, u64 start, u64 len,
2575 u32 *refs)
2576{
2577 int ret;
2578
2810 mutex_unlock(&root->fs_info->alloc_mutex);
2811
2812 atomic_inc(&root->fs_info->throttle_gen);
2813 wake_up(&root->fs_info->transaction_throttle);
2814 cond_resched();
2815
2816 BUG_ON(ret);
2817 info++;
2818 }
2819
2820 return 0;
2821}
2822
2823int drop_snap_lookup_refcount(struct btrfs_root *root, u64 start, u64 len,
2824 u32 *refs)
2825{
2826 int ret;
2827
2579 ret = lookup_extent_ref(NULL, root, start, len, refs);
2828 ret = btrfs_lookup_extent_ref(NULL, root, start, len, refs);
2580 BUG_ON(ret);
2581
2582#if 0 // some debugging code in case we see problems here
2583 /* if the refs count is one, it won't get increased again. But
2584 * if the ref count is > 1, someone may be decreasing it at
2585 * the same time we are.
2586 */
2587 if (*refs != 1) {

--- 79 unchanged lines hidden (view full) ---

2667 if (refs != 1) {
2668 parent = path->nodes[*level];
2669 root_owner = btrfs_header_owner(parent);
2670 root_gen = btrfs_header_generation(parent);
2671 path->slots[*level]++;
2672
2673 mutex_lock(&root->fs_info->alloc_mutex);
2674 ret = __btrfs_free_extent(trans, root, bytenr,
2829 BUG_ON(ret);
2830
2831#if 0 // some debugging code in case we see problems here
2832 /* if the refs count is one, it won't get increased again. But
2833 * if the ref count is > 1, someone may be decreasing it at
2834 * the same time we are.
2835 */
2836 if (*refs != 1) {

--- 79 unchanged lines hidden (view full) ---

2916 if (refs != 1) {
2917 parent = path->nodes[*level];
2918 root_owner = btrfs_header_owner(parent);
2919 root_gen = btrfs_header_generation(parent);
2920 path->slots[*level]++;
2921
2922 mutex_lock(&root->fs_info->alloc_mutex);
2923 ret = __btrfs_free_extent(trans, root, bytenr,
2675 blocksize, root_owner,
2676 root_gen, 0, 0, 1);
2924 blocksize, parent->start,
2925 root_owner, root_gen, 0, 0, 1);
2677 BUG_ON(ret);
2678 mutex_unlock(&root->fs_info->alloc_mutex);
2679
2680 atomic_inc(&root->fs_info->throttle_gen);
2681 wake_up(&root->fs_info->transaction_throttle);
2682 cond_resched();
2683
2684 continue;
2685 }
2686 /*
2687 * at this point, we have a single ref, and since the
2688 * only place referencing this extent is a dead root
2689 * the reference count should never go higher.
2690 * So, we don't need to check it again
2691 */
2692 if (*level == 1) {
2926 BUG_ON(ret);
2927 mutex_unlock(&root->fs_info->alloc_mutex);
2928
2929 atomic_inc(&root->fs_info->throttle_gen);
2930 wake_up(&root->fs_info->transaction_throttle);
2931 cond_resched();
2932
2933 continue;
2934 }
2935 /*
2936 * at this point, we have a single ref, and since the
2937 * only place referencing this extent is a dead root
2938 * the reference count should never go higher.
2939 * So, we don't need to check it again
2940 */
2941 if (*level == 1) {
2693 struct btrfs_key key;
2694 btrfs_node_key_to_cpu(cur, &key, path->slots[*level]);
2695 ref = btrfs_lookup_leaf_ref(root, bytenr);
2696 if (ref) {
2697 ret = cache_drop_leaf_ref(trans, root, ref);
2698 BUG_ON(ret);
2699 btrfs_remove_leaf_ref(root, ref);
2700 btrfs_free_leaf_ref(root, ref);
2701 *level = 0;
2702 break;

--- 42 unchanged lines hidden (view full) ---

2745 }
2746
2747 blocksize = btrfs_level_size(root, *level);
2748 root_owner = btrfs_header_owner(parent);
2749 root_gen = btrfs_header_generation(parent);
2750
2751 mutex_lock(&root->fs_info->alloc_mutex);
2752 ret = __btrfs_free_extent(trans, root, bytenr, blocksize,
2942 ref = btrfs_lookup_leaf_ref(root, bytenr);
2943 if (ref) {
2944 ret = cache_drop_leaf_ref(trans, root, ref);
2945 BUG_ON(ret);
2946 btrfs_remove_leaf_ref(root, ref);
2947 btrfs_free_leaf_ref(root, ref);
2948 *level = 0;
2949 break;

--- 42 unchanged lines hidden (view full) ---

2992 }
2993
2994 blocksize = btrfs_level_size(root, *level);
2995 root_owner = btrfs_header_owner(parent);
2996 root_gen = btrfs_header_generation(parent);
2997
2998 mutex_lock(&root->fs_info->alloc_mutex);
2999 ret = __btrfs_free_extent(trans, root, bytenr, blocksize,
2753 root_owner, root_gen, 0, 0, 1);
3000 parent->start, root_owner, root_gen,
3001 0, 0, 1);
3002 mutex_unlock(&root->fs_info->alloc_mutex);
2754 free_extent_buffer(path->nodes[*level]);
2755 path->nodes[*level] = NULL;
2756 *level += 1;
2757 BUG_ON(ret);
3003 free_extent_buffer(path->nodes[*level]);
3004 path->nodes[*level] = NULL;
3005 *level += 1;
3006 BUG_ON(ret);
2758 mutex_unlock(&root->fs_info->alloc_mutex);
2759
2760 cond_resched();
2761 return 0;
2762}
2763
2764/*
2765 * helper for dropping snapshots. This walks back up the tree in the path
2766 * to find the first node higher up where we haven't yet gone through

--- 20 unchanged lines hidden (view full) ---

2787 *level = i;
2788 WARN_ON(*level == 0);
2789 btrfs_node_key(node, &disk_key, path->slots[i]);
2790 memcpy(&root_item->drop_progress,
2791 &disk_key, sizeof(disk_key));
2792 root_item->drop_level = i;
2793 return 0;
2794 } else {
3007
3008 cond_resched();
3009 return 0;
3010}
3011
3012/*
3013 * helper for dropping snapshots. This walks back up the tree in the path
3014 * to find the first node higher up where we haven't yet gone through

--- 20 unchanged lines hidden (view full) ---

3035 *level = i;
3036 WARN_ON(*level == 0);
3037 btrfs_node_key(node, &disk_key, path->slots[i]);
3038 memcpy(&root_item->drop_progress,
3039 &disk_key, sizeof(disk_key));
3040 root_item->drop_level = i;
3041 return 0;
3042 } else {
2795 if (path->nodes[*level] == root->node) {
2796 root_owner = root->root_key.objectid;
2797 root_gen =
2798 btrfs_header_generation(path->nodes[*level]);
2799 } else {
2800 struct extent_buffer *node;
2801 node = path->nodes[*level + 1];
2802 root_owner = btrfs_header_owner(node);
2803 root_gen = btrfs_header_generation(node);
2804 }
3043 struct extent_buffer *parent;
3044 if (path->nodes[*level] == root->node)
3045 parent = path->nodes[*level];
3046 else
3047 parent = path->nodes[*level + 1];
3048
3049 root_owner = btrfs_header_owner(parent);
3050 root_gen = btrfs_header_generation(parent);
2805 ret = btrfs_free_extent(trans, root,
2806 path->nodes[*level]->start,
2807 path->nodes[*level]->len,
3051 ret = btrfs_free_extent(trans, root,
3052 path->nodes[*level]->start,
3053 path->nodes[*level]->len,
3054 parent->start,
2808 root_owner, root_gen, 0, 0, 1);
2809 BUG_ON(ret);
2810 free_extent_buffer(path->nodes[*level]);
2811 path->nodes[*level] = NULL;
2812 *level = i + 1;
2813 }
2814 }
2815 return 1;

--- 972 unchanged lines hidden ---
3055 root_owner, root_gen, 0, 0, 1);
3056 BUG_ON(ret);
3057 free_extent_buffer(path->nodes[*level]);
3058 path->nodes[*level] = NULL;
3059 *level = i + 1;
3060 }
3061 }
3062 return 1;

--- 972 unchanged lines hidden ---