Lines Matching refs:root

34 #include "root-tree.h"
89 * map address of tree root to tree
142 /* map start of tree root to corresponding reloc tree */
191 * walk up backref nodes until reach node presents tree root
302 static bool reloc_root_is_dead(struct btrfs_root *root)
310 if (test_bit(BTRFS_ROOT_DEAD_RELOC_TREE, &root->state))
319 * This is enough for most callers, as they don't distinguish dead reloc root
320 * from no reloc root. But btrfs_should_ignore_reloc_root() below is a
323 static bool have_reloc_root(struct btrfs_root *root)
325 if (reloc_root_is_dead(root))
327 if (!root->reloc_root)
332 int btrfs_should_ignore_reloc_root(struct btrfs_root *root)
336 if (!test_bit(BTRFS_ROOT_SHAREABLE, &root->state))
339 /* This root has been merged with its reloc tree, we can ignore it */
340 if (reloc_root_is_dead(root))
343 reloc_root = root->reloc_root;
348 root->fs_info->running_transaction->transid)
353 * so backref node for the fs tree root is useless for
360 * find reloc tree by address of tree root
367 struct btrfs_root *root = NULL;
374 root = node->data;
377 return btrfs_grab_root(root);
407 /* Only tree root nodes can be added to @useless_nodes */
461 * these upper level blocks recursively. The recursion stops when tree root is
546 * corresponds to root of source tree
593 new_node->root = btrfs_grab_root(dest);
594 ASSERT(new_node->root);
633 * helper to add 'address of tree root -> reloc tree' mapping
635 static int __must_check __add_reloc_root(struct btrfs_root *root)
637 struct btrfs_fs_info *fs_info = root->fs_info;
646 node->bytenr = root->commit_root->start;
647 node->data = root;
655 "Duplicate root found for start=%llu while inserting into relocation tree",
660 list_add_tail(&root->root_list, &rc->reloc_roots);
665 * helper to delete the 'address of tree root -> reloc tree'
668 static void __del_reloc_root(struct btrfs_root *root)
670 struct btrfs_fs_info *fs_info = root->fs_info;
676 if (rc && root->node) {
679 root->commit_root->start);
686 ASSERT(!node || (struct btrfs_root *)node->data == root);
690 * We only put the reloc root here if it's on the list. There's a lot
692 * the reloc roots, and then add the reloc root back onto
698 if (!list_empty(&root->root_list)) {
700 list_del_init(&root->root_list);
704 btrfs_put_root(root);
709 * helper to update the 'address of tree root -> reloc tree'
712 static int __update_reloc_root(struct btrfs_root *root)
714 struct btrfs_fs_info *fs_info = root->fs_info;
721 root->commit_root->start);
730 BUG_ON((struct btrfs_root *)node->data != root);
733 node->bytenr = root->node->start;
743 struct btrfs_root *root, u64 objectid)
745 struct btrfs_fs_info *fs_info = root->fs_info;
761 if (root->root_key.objectid == objectid) {
765 ret = btrfs_copy_root(trans, root, root->commit_root, &eb,
772 * root - like this ctree.c:btrfs_block_can_be_shared() behaves
773 * correctly (returns true) when the relocation root is created
778 commit_root_gen = btrfs_header_generation(root->commit_root);
779 btrfs_set_root_last_snapshot(&root->root_item, commit_root_gen);
788 ret = btrfs_copy_root(trans, root, root->node, &eb,
800 memcpy(root_item, &root->root_item, sizeof(*root_item));
805 if (root->root_key.objectid == objectid) {
840 * snapshot of the fs tree with special root objectid.
843 * root->reloc_root, and another for being on the rc->reloc_roots list.
846 struct btrfs_root *root)
848 struct btrfs_fs_info *fs_info = root->fs_info;
862 if (reloc_root_is_dead(root))
868 * corresponding fs root, and then here we update the last trans for the
869 * reloc root. This means that we have to do this for the entire life
870 * of the reloc root, regardless of which stage of the relocation we are
873 if (root->reloc_root) {
874 reloc_root = root->reloc_root;
884 root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID)
892 reloc_root = create_reloc_root(trans, root, root->root_key.objectid);
905 root->reloc_root = btrfs_grab_root(reloc_root);
910 * update root item of reloc tree
913 struct btrfs_root *root)
915 struct btrfs_fs_info *fs_info = root->fs_info;
920 if (!have_reloc_root(root))
923 reloc_root = root->reloc_root;
928 * the root. We have the ref for root->reloc_root, but just in case
929 * hold it while we update the reloc root.
933 /* root->reloc_root will stay until current relocation finished */
936 set_bit(BTRFS_ROOT_DEAD_RELOC_TREE, &root->state);
962 static struct inode *find_next_inode(struct btrfs_root *root, u64 objectid)
969 spin_lock(&root->inode_lock);
971 node = root->inode_tree.rb_node;
998 spin_unlock(&root->inode_lock);
1003 if (cond_resched_lock(&root->inode_lock))
1008 spin_unlock(&root->inode_lock);
1018 struct btrfs_root *root = BTRFS_I(reloc_inode)->root;
1029 ret = btrfs_lookup_file_extent(NULL, root, path,
1066 struct btrfs_root *root,
1069 struct btrfs_fs_info *fs_info = root->fs_info;
1088 if (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID)
1117 if (root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID) {
1119 inode = find_next_inode(root, key.objectid);
1123 inode = find_next_inode(root, key.objectid);
1165 root->root_key.objectid, false);
1176 root->root_key.objectid, false);
1364 * CoW on the subtree root node before transaction commit.
1438 int walk_up_reloc_tree(struct btrfs_root *root, struct btrfs_path *path,
1446 last_snapshot = btrfs_root_last_snapshot(&root->root_item);
1475 int walk_down_reloc_tree(struct btrfs_root *root, struct btrfs_path *path,
1484 last_snapshot = btrfs_root_last_snapshot(&root->root_item);
1520 static int invalidate_extent_cache(struct btrfs_root *root,
1524 struct btrfs_fs_info *fs_info = root->fs_info;
1540 inode = find_next_inode(root, objectid);
1614 struct btrfs_root *root)
1616 struct btrfs_root *reloc_root = root->reloc_root;
1620 /* @root must be a subvolume tree root with a valid reloc tree */
1621 ASSERT(root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID);
1629 ret = btrfs_update_reloc_root(trans, root);
1633 if (list_empty(&root->reloc_dirty_list)) {
1634 btrfs_grab_root(root);
1635 list_add_tail(&root->reloc_dirty_list, &rc->dirty_subvol_roots);
1643 struct btrfs_root *root;
1648 list_for_each_entry_safe(root, next, &rc->dirty_subvol_roots,
1650 if (root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID) {
1651 /* Merged subvolume, cleanup its reloc root */
1652 struct btrfs_root *reloc_root = root->reloc_root;
1654 list_del_init(&root->reloc_dirty_list);
1655 root->reloc_root = NULL;
1658 * root->reloc_root = NULL. Pairs with have_reloc_root.
1661 clear_bit(BTRFS_ROOT_DEAD_RELOC_TREE, &root->state);
1675 btrfs_put_root(root);
1678 ret2 = btrfs_drop_snapshot(root, 0, 1);
1680 btrfs_put_root(root);
1694 struct btrfs_root *root)
1716 reloc_root = root->reloc_root;
1747 * block COW, we COW at most from level 1 to root level for each tree.
1762 trans = btrfs_start_transaction(root, 0);
1776 * btrfs_update_reloc_root() and update our root item
1795 ret = replace_path(trans, rc, root, reloc_root, path,
1814 * this is OK since root refs == 1 in this case.
1826 invalidate_extent_cache(root, &key, &next_key);
1831 * relocated and the block is tree root.
1833 leaf = btrfs_lock_root_node(root);
1834 ret = btrfs_cow_block(trans, root, leaf, NULL, 0, &leaf,
1842 ret = insert_dirty_subvol(trans, rc, root);
1853 invalidate_extent_cache(root, &key, &next_key);
1861 struct btrfs_root *root = rc->extent_root;
1862 struct btrfs_fs_info *fs_info = root->fs_info;
1907 root = btrfs_get_fs_root(fs_info, reloc_root->root_key.offset,
1909 if (IS_ERR(root)) {
1911 * Even if we have an error we need this reloc root
1915 btrfs_abort_transaction(trans, (int)PTR_ERR(root));
1917 err = PTR_ERR(root);
1921 if (unlikely(root->reloc_root != reloc_root)) {
1922 if (root->reloc_root) {
1924 "reloc tree mismatch, root %lld has reloc root key (%lld %u %llu) gen %llu, expect reloc root key (%lld %u %llu) gen %llu",
1925 root->root_key.objectid,
1926 root->reloc_root->root_key.objectid,
1927 root->reloc_root->root_key.type,
1928 root->reloc_root->root_key.offset,
1930 &root->reloc_root->root_item),
1938 "reloc tree mismatch, root %lld has no reloc root, expect reloc root key (%lld %u %llu) gen %llu",
1939 root->root_key.objectid,
1947 btrfs_put_root(root);
1960 ret = btrfs_update_reloc_root(trans, root);
1963 * Even if we have an error we need this reloc root back on our
1967 btrfs_put_root(root);
1999 struct btrfs_root *root;
2005 root = rc->extent_root;
2022 root = btrfs_get_fs_root(fs_info, reloc_root->root_key.offset,
2025 if (WARN_ON(IS_ERR(root))) {
2028 * and if we didn't find the root then we marked
2029 * the reloc root as a garbage root. For normal
2030 * relocation obviously the root should exist in
2034 ret = PTR_ERR(root);
2037 if (WARN_ON(root->reloc_root != reloc_root)) {
2045 ret = merge_reloc_root(rc, root);
2046 btrfs_put_root(root);
2054 if (!IS_ERR(root)) {
2055 if (root->reloc_root == reloc_root) {
2056 root->reloc_root = NULL;
2060 &root->state);
2061 btrfs_put_root(root);
2065 /* Don't forget to queue this reloc root for cleanup */
2080 /* new reloc root may be added */
2095 * fine because we're bailing here, and we hold a reference on the root
2097 * do the reloc_dirty_list afterwards. Meanwhile the root->reloc_root
2119 struct btrfs_root *root;
2125 root = btrfs_get_fs_root(fs_info, reloc_root->root_key.offset, false);
2128 * This should succeed, since we can't have a reloc root without having
2129 * already looked up the actual root and created the reloc root for this
2130 * root.
2133 * reloc root without a corresponding root this could return ENOENT.
2135 if (IS_ERR(root)) {
2137 return PTR_ERR(root);
2139 if (root->reloc_root != reloc_root) {
2142 "root %llu has two reloc roots associated with it",
2144 btrfs_put_root(root);
2147 ret = btrfs_record_root_in_trans(trans, root);
2148 btrfs_put_root(root);
2160 struct btrfs_root *root;
2168 root = next->root;
2171 * If there is no root, then our references for this block are
2173 * block that is owned by a root.
2176 * non-SHAREABLE root then we have backrefs that resolve
2182 if (!root) {
2185 "bytenr %llu doesn't have a backref path ending in a root",
2189 if (!test_bit(BTRFS_ROOT_SHAREABLE, &root->state)) {
2192 "bytenr %llu has multiple refs with one ending in a non-shareable root",
2197 if (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID) {
2198 ret = record_reloc_root_in_trans(trans, root);
2204 ret = btrfs_record_root_in_trans(trans, root);
2207 root = root->reloc_root;
2211 * root->reloc_root may not be set, return ENOENT in this case.
2213 if (!root)
2216 if (next->new_bytenr != root->node->start) {
2218 * We just created the reloc root, so we shouldn't have
2233 next->new_bytenr = root->node->start;
2234 btrfs_put_root(next->root);
2235 next->root = btrfs_grab_root(root);
2236 ASSERT(next->root);
2244 root = NULL;
2249 if (!root) {
2266 return root;
2270 * Select a tree root for relocation.
2275 * Return a tree root pointer if the block is shareable.
2276 * Return -ENOENT if the block is root of reloc tree.
2282 struct btrfs_root *root;
2291 root = next->root;
2297 if (!root)
2301 if (!test_bit(BTRFS_ROOT_SHAREABLE, &root->state))
2302 return root;
2304 if (root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID)
2305 fs_root = root;
2358 struct btrfs_root *root = rc->extent_root;
2359 struct btrfs_fs_info *fs_info = root->fs_info;
2411 struct btrfs_root *root;
2432 root = select_reloc_root(trans, rc, upper, edges);
2433 if (IS_ERR(root)) {
2434 ret = PTR_ERR(root);
2452 ret = btrfs_search_slot(trans, root, key, path, 0, 1);
2483 btrfs_err(root->fs_info,
2495 blocksize = root->fs_info->nodesize;
2504 ret = btrfs_cow_block(trans, root, eb, upper->eb,
2527 root->root_key.objectid, false);
2530 ret = btrfs_drop_subtree(trans, root, eb,
2676 struct btrfs_root *root;
2691 root = select_one_root(node);
2692 if (IS_ERR(root)) {
2693 ret = PTR_ERR(root);
2704 if (root) {
2705 if (test_bit(BTRFS_ROOT_SHAREABLE, &root->state)) {
2707 * This block was the root block of a root, and this is
2722 btrfs_err(root->fs_info,
2728 ret = btrfs_record_root_in_trans(trans, root);
2735 if (!root->reloc_root) {
2739 root = root->reloc_root;
2740 node->new_bytenr = root->node->start;
2741 btrfs_put_root(node->root);
2742 node->root = btrfs_grab_root(root);
2743 ASSERT(node->root);
2747 if (root == root->fs_info->chunk_root)
2749 ret = btrfs_search_slot(trans, root, key, path, 0, 1);
2751 if (root == root->fs_info->chunk_root)
2860 struct btrfs_fs_info *fs_info = inode->root->fs_info;
2935 btrfs_free_reserved_data_space_noquota(inode->root->fs_info,
3235 * inline ref offset. We know it's an fs root if
3370 struct btrfs_root *root = fs_info->tree_root;
3377 inode = btrfs_iget(fs_info->sb, ino, root);
3387 trans = btrfs_join_transaction(root);
3403 * Locate the free space cache EXTENT_DATA in root tree leaf and delete the
3815 struct btrfs_root *root, u64 objectid)
3826 ret = btrfs_insert_empty_inode(trans, root, path, objectid);
3845 struct btrfs_root *root, u64 objectid)
3860 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
3866 ret = btrfs_del_item(trans, root, path);
3883 struct btrfs_root *root;
3887 root = btrfs_grab_root(fs_info->data_reloc_root);
3888 trans = btrfs_start_transaction(root, 6);
3890 btrfs_put_root(root);
3894 err = btrfs_get_free_objectid(root, &objectid);
3898 err = __insert_orphan_inode(trans, root, objectid);
3902 inode = btrfs_iget(fs_info->sb, objectid, root);
3904 delete_orphan_inode(trans, root, objectid);
3913 btrfs_put_root(root);
4175 static noinline_for_stack int mark_garbage_root(struct btrfs_root *root)
4177 struct btrfs_fs_info *fs_info = root->fs_info;
4185 memset(&root->root_item.drop_progress, 0,
4186 sizeof(root->root_item.drop_progress));
4187 btrfs_set_root_drop_level(&root->root_item, 0);
4188 btrfs_set_root_refs(&root->root_item, 0);
4190 &root->root_key, &root->root_item);
4387 struct btrfs_fs_info *fs_info = inode->root->fs_info;
4425 struct btrfs_root *root, struct extent_buffer *buf,
4428 struct btrfs_fs_info *fs_info = root->fs_info;
4439 BUG_ON(rc->stage == UPDATE_DATA_PTRS && btrfs_is_data_reloc_root(root));
4443 btrfs_root_last_snapshot(&root->root_item))
4446 if (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID &&
4473 ret = replace_file_extents(trans, rc, root, cow);
4484 struct btrfs_root *root = pending->root;
4485 struct reloc_control *rc = root->fs_info->reloc_ctl;
4487 if (!rc || !have_reloc_root(root))
4493 root = root->reloc_root;
4494 BUG_ON(btrfs_root_refs(&root->root_item) == 0);
4510 * and create reloc root for the newly created snapshot
4513 * references held on the reloc_root, one for root->reloc_root and one for
4519 struct btrfs_root *root = pending->root;
4522 struct reloc_control *rc = root->fs_info->reloc_ctl;
4525 if (!rc || !have_reloc_root(root))
4528 rc = root->fs_info->reloc_ctl;
4540 reloc_root = create_reloc_root(trans, root->reloc_root,
4555 ret = clone_backref_node(trans, rc, root, reloc_root);