Lines Matching refs:root

34 #include "root-tree.h"
89 * map address of tree root to tree
149 /* map start of tree root to corresponding reloc tree */
187 * walk up backref nodes until reach node presents tree root
298 static bool reloc_root_is_dead(const struct btrfs_root *root)
306 if (test_bit(BTRFS_ROOT_DEAD_RELOC_TREE, &root->state))
315 * This is enough for most callers, as they don't distinguish dead reloc root
316 * from no reloc root. But btrfs_should_ignore_reloc_root() below is a
319 static bool have_reloc_root(const struct btrfs_root *root)
321 if (reloc_root_is_dead(root))
323 if (!root->reloc_root)
328 bool btrfs_should_ignore_reloc_root(const struct btrfs_root *root)
332 if (!test_bit(BTRFS_ROOT_SHAREABLE, &root->state))
335 /* This root has been merged with its reloc tree, we can ignore it */
336 if (reloc_root_is_dead(root))
339 reloc_root = root->reloc_root;
344 root->fs_info->running_transaction->transid)
349 * tree root is useless for relocation.
355 * find reloc tree by address of tree root
362 struct btrfs_root *root = NULL;
369 root = node->data;
372 return btrfs_grab_root(root);
402 /* Only tree root nodes can be added to @useless_nodes */
456 * these upper level blocks recursively. The recursion stops when tree root is
538 * corresponds to root of source tree
585 new_node->root = btrfs_grab_root(dest);
586 ASSERT(new_node->root);
625 * helper to add 'address of tree root -> reloc tree' mapping
627 static int __add_reloc_root(struct btrfs_root *root)
629 struct btrfs_fs_info *fs_info = root->fs_info;
638 node->bytenr = root->commit_root->start;
639 node->data = root;
647 "Duplicate root found for start=%llu while inserting into relocation tree",
652 list_add_tail(&root->root_list, &rc->reloc_roots);
657 * helper to delete the 'address of tree root -> reloc tree'
660 static void __del_reloc_root(struct btrfs_root *root)
662 struct btrfs_fs_info *fs_info = root->fs_info;
668 if (rc && root->node) {
671 root->commit_root->start);
678 ASSERT(!node || (struct btrfs_root *)node->data == root);
682 * We only put the reloc root here if it's on the list. There's a lot
684 * the reloc roots, and then add the reloc root back onto
690 if (!list_empty(&root->root_list)) {
692 list_del_init(&root->root_list);
696 btrfs_put_root(root);
701 * helper to update the 'address of tree root -> reloc tree'
704 static int __update_reloc_root(struct btrfs_root *root)
706 struct btrfs_fs_info *fs_info = root->fs_info;
713 root->commit_root->start);
722 BUG_ON((struct btrfs_root *)node->data != root);
725 node->bytenr = root->node->start;
735 struct btrfs_root *root, u64 objectid)
737 struct btrfs_fs_info *fs_info = root->fs_info;
753 if (btrfs_root_id(root) == objectid) {
757 ret = btrfs_copy_root(trans, root, root->commit_root, &eb,
764 * root - like this ctree.c:btrfs_block_can_be_shared() behaves
765 * correctly (returns true) when the relocation root is created
770 commit_root_gen = btrfs_header_generation(root->commit_root);
771 btrfs_set_root_last_snapshot(&root->root_item, commit_root_gen);
780 ret = btrfs_copy_root(trans, root, root->node, &eb,
792 memcpy(root_item, &root->root_item, sizeof(*root_item));
797 if (btrfs_root_id(root) == objectid) {
832 * snapshot of the fs tree with special root objectid.
835 * root->reloc_root, and another for being on the rc->reloc_roots list.
838 struct btrfs_root *root)
840 struct btrfs_fs_info *fs_info = root->fs_info;
854 if (reloc_root_is_dead(root))
860 * corresponding fs root, and then here we update the last trans for the
861 * reloc root. This means that we have to do this for the entire life
862 * of the reloc root, regardless of which stage of the relocation we are
865 if (root->reloc_root) {
866 reloc_root = root->reloc_root;
875 if (!rc->create_reloc_tree || btrfs_root_id(root) == BTRFS_TREE_RELOC_OBJECTID)
883 reloc_root = create_reloc_root(trans, root, btrfs_root_id(root));
896 root->reloc_root = btrfs_grab_root(reloc_root);
901 * update root item of reloc tree
904 struct btrfs_root *root)
906 struct btrfs_fs_info *fs_info = root->fs_info;
911 if (!have_reloc_root(root))
914 reloc_root = root->reloc_root;
919 * the root. We have the ref for root->reloc_root, but just in case
920 * hold it while we update the reloc root.
924 /* root->reloc_root will stay until current relocation finished */
927 set_bit(BTRFS_ROOT_DEAD_RELOC_TREE, &root->state);
955 struct btrfs_root *root = BTRFS_I(reloc_inode)->root;
966 ret = btrfs_lookup_file_extent(NULL, root, path,
1003 struct btrfs_root *root,
1006 struct btrfs_fs_info *fs_info = root->fs_info;
1025 if (btrfs_root_id(root) == BTRFS_TREE_RELOC_OBJECTID)
1054 if (btrfs_root_id(root) != BTRFS_TREE_RELOC_OBJECTID) {
1056 inode = btrfs_find_first_inode(root, key.objectid);
1060 inode = btrfs_find_first_inode(root, key.objectid);
1106 ref.owning_root = btrfs_root_id(root);
1109 btrfs_root_id(root), false);
1120 ref.owning_root = btrfs_root_id(root);
1123 btrfs_root_id(root), false);
1311 * CoW on the subtree root node before transaction commit.
1400 int walk_up_reloc_tree(struct btrfs_root *root, struct btrfs_path *path,
1408 last_snapshot = btrfs_root_last_snapshot(&root->root_item);
1437 int walk_down_reloc_tree(struct btrfs_root *root, struct btrfs_path *path,
1446 last_snapshot = btrfs_root_last_snapshot(&root->root_item);
1482 static int invalidate_extent_cache(struct btrfs_root *root,
1486 struct btrfs_fs_info *fs_info = root->fs_info;
1503 inode = btrfs_find_first_inode(root, objectid);
1577 struct btrfs_root *root)
1579 struct btrfs_root *reloc_root = root->reloc_root;
1583 /* @root must be a subvolume tree root with a valid reloc tree */
1584 ASSERT(btrfs_root_id(root) != BTRFS_TREE_RELOC_OBJECTID);
1592 ret = btrfs_update_reloc_root(trans, root);
1596 if (list_empty(&root->reloc_dirty_list)) {
1597 btrfs_grab_root(root);
1598 list_add_tail(&root->reloc_dirty_list, &rc->dirty_subvol_roots);
1606 struct btrfs_root *root;
1611 list_for_each_entry_safe(root, next, &rc->dirty_subvol_roots,
1613 if (btrfs_root_id(root) != BTRFS_TREE_RELOC_OBJECTID) {
1614 /* Merged subvolume, cleanup its reloc root */
1615 struct btrfs_root *reloc_root = root->reloc_root;
1617 list_del_init(&root->reloc_dirty_list);
1618 root->reloc_root = NULL;
1621 * root->reloc_root = NULL. Pairs with have_reloc_root.
1624 clear_bit(BTRFS_ROOT_DEAD_RELOC_TREE, &root->state);
1638 btrfs_put_root(root);
1641 ret2 = btrfs_drop_snapshot(root, 0, 1);
1643 btrfs_put_root(root);
1657 struct btrfs_root *root)
1679 reloc_root = root->reloc_root;
1710 * block COW, we COW at most from level 1 to root level for each tree.
1725 trans = btrfs_start_transaction(root, 0);
1739 * btrfs_update_reloc_root() and update our root item
1758 ret = replace_path(trans, rc, root, reloc_root, path,
1777 * this is OK since root refs == 1 in this case.
1789 invalidate_extent_cache(root, &key, &next_key);
1794 * relocated and the block is tree root.
1796 leaf = btrfs_lock_root_node(root);
1797 ret = btrfs_cow_block(trans, root, leaf, NULL, 0, &leaf,
1805 ret = insert_dirty_subvol(trans, rc, root);
1816 invalidate_extent_cache(root, &key, &next_key);
1824 struct btrfs_root *root = rc->extent_root;
1825 struct btrfs_fs_info *fs_info = root->fs_info;
1870 root = btrfs_get_fs_root(fs_info, reloc_root->root_key.offset,
1872 if (IS_ERR(root)) {
1874 * Even if we have an error we need this reloc root
1878 btrfs_abort_transaction(trans, (int)PTR_ERR(root));
1880 err = PTR_ERR(root);
1884 if (unlikely(root->reloc_root != reloc_root)) {
1885 if (root->reloc_root) {
1887 "reloc tree mismatch, root %lld has reloc root key (%lld %u %llu) gen %llu, expect reloc root key (%lld %u %llu) gen %llu",
1888 btrfs_root_id(root),
1889 btrfs_root_id(root->reloc_root),
1890 root->reloc_root->root_key.type,
1891 root->reloc_root->root_key.offset,
1893 &root->reloc_root->root_item),
1901 "reloc tree mismatch, root %lld has no reloc root, expect reloc root key (%lld %u %llu) gen %llu",
1902 btrfs_root_id(root),
1910 btrfs_put_root(root);
1923 ret = btrfs_update_reloc_root(trans, root);
1926 * Even if we have an error we need this reloc root back on our
1930 btrfs_put_root(root);
1962 struct btrfs_root *root;
1968 root = rc->extent_root;
1985 root = btrfs_get_fs_root(fs_info, reloc_root->root_key.offset,
1988 if (WARN_ON(IS_ERR(root))) {
1991 * and if we didn't find the root then we marked
1992 * the reloc root as a garbage root. For normal
1993 * relocation obviously the root should exist in
1997 ret = PTR_ERR(root);
2000 if (WARN_ON(root->reloc_root != reloc_root)) {
2008 ret = merge_reloc_root(rc, root);
2009 btrfs_put_root(root);
2017 if (!IS_ERR(root)) {
2018 if (root->reloc_root == reloc_root) {
2019 root->reloc_root = NULL;
2023 &root->state);
2024 btrfs_put_root(root);
2028 /* Don't forget to queue this reloc root for cleanup */
2043 /* new reloc root may be added */
2058 * fine because we're bailing here, and we hold a reference on the root
2060 * do the reloc_dirty_list afterwards. Meanwhile the root->reloc_root
2082 struct btrfs_root *root;
2088 root = btrfs_get_fs_root(fs_info, reloc_root->root_key.offset, false);
2091 * This should succeed, since we can't have a reloc root without having
2092 * already looked up the actual root and created the reloc root for this
2093 * root.
2096 * reloc root without a corresponding root this could return ENOENT.
2098 if (IS_ERR(root)) {
2100 return PTR_ERR(root);
2102 if (root->reloc_root != reloc_root) {
2105 "root %llu has two reloc roots associated with it",
2107 btrfs_put_root(root);
2110 ret = btrfs_record_root_in_trans(trans, root);
2111 btrfs_put_root(root);
2123 struct btrfs_root *root;
2131 root = next->root;
2134 * If there is no root, then our references for this block are
2136 * block that is owned by a root.
2139 * non-SHAREABLE root then we have backrefs that resolve
2145 if (!root) {
2148 "bytenr %llu doesn't have a backref path ending in a root",
2152 if (!test_bit(BTRFS_ROOT_SHAREABLE, &root->state)) {
2155 "bytenr %llu has multiple refs with one ending in a non-shareable root",
2160 if (btrfs_root_id(root) == BTRFS_TREE_RELOC_OBJECTID) {
2161 ret = record_reloc_root_in_trans(trans, root);
2167 ret = btrfs_record_root_in_trans(trans, root);
2170 root = root->reloc_root;
2174 * root->reloc_root may not be set, return ENOENT in this case.
2176 if (!root)
2179 if (next->new_bytenr != root->node->start) {
2181 * We just created the reloc root, so we shouldn't have
2196 next->new_bytenr = root->node->start;
2197 btrfs_put_root(next->root);
2198 next->root = btrfs_grab_root(root);
2199 ASSERT(next->root);
2207 root = NULL;
2212 if (!root) {
2229 return root;
2233 * Select a tree root for relocation.
2238 * Return a tree root pointer if the block is shareable.
2239 * Return -ENOENT if the block is root of reloc tree.
2245 struct btrfs_root *root;
2254 root = next->root;
2260 if (!root)
2264 if (!test_bit(BTRFS_ROOT_SHAREABLE, &root->state))
2265 return root;
2267 if (btrfs_root_id(root) != BTRFS_TREE_RELOC_OBJECTID)
2268 fs_root = root;
2320 struct btrfs_root *root = rc->extent_root;
2321 struct btrfs_fs_info *fs_info = root->fs_info;
2373 struct btrfs_root *root;
2392 root = select_reloc_root(trans, rc, upper, edges);
2393 if (IS_ERR(root)) {
2394 ret = PTR_ERR(root);
2412 ret = btrfs_search_slot(trans, root, key, path, 0, 1);
2443 btrfs_err(root->fs_info,
2455 blocksize = root->fs_info->nodesize;
2464 ret = btrfs_cow_block(trans, root, eb, upper->eb,
2492 btrfs_root_id(root), false);
2495 ret = btrfs_drop_subtree(trans, root, eb,
2641 struct btrfs_root *root;
2656 root = select_one_root(node);
2657 if (IS_ERR(root)) {
2658 ret = PTR_ERR(root);
2669 if (root) {
2670 if (test_bit(BTRFS_ROOT_SHAREABLE, &root->state)) {
2672 * This block was the root block of a root, and this is
2687 btrfs_err(root->fs_info,
2693 ret = btrfs_record_root_in_trans(trans, root);
2700 if (!root->reloc_root) {
2704 root = root->reloc_root;
2705 node->new_bytenr = root->node->start;
2706 btrfs_put_root(node->root);
2707 node->root = btrfs_grab_root(root);
2708 ASSERT(node->root);
2712 if (root == root->fs_info->chunk_root)
2714 ret = btrfs_search_slot(trans, root, key, path, 0, 1);
2716 if (root == root->fs_info->chunk_root)
2822 struct btrfs_fs_info *fs_info = inode->root->fs_info;
2897 btrfs_free_reserved_data_space_noquota(inode->root->fs_info,
3130 struct btrfs_root *root = BTRFS_I(inode)->root;
3140 * Under simple quotas, we set root->relocation_src_root when we find
3142 * them while relocating. Handle this by storing the owning root that
3143 * started a cluster and if we see an extent from a different root break
3150 if (cluster->nr > 0 && cluster->owning_root != root->relocation_src_root) {
3151 u64 tmp = root->relocation_src_root;
3154 * root->relocation_src_root is the state that actually affects
3155 * the preallocation we do here, so set it to the root owning
3158 root->relocation_src_root = cluster->owning_root;
3163 /* And reset it back for the current extent's owning root. */
3164 root->relocation_src_root = tmp;
3169 cluster->owning_root = root->relocation_src_root;
3230 * inline ref offset. We know it's an fs root if
3365 struct btrfs_root *root = fs_info->tree_root;
3372 inode = btrfs_iget(fs_info->sb, ino, root);
3382 trans = btrfs_join_transaction(root);
3398 * Locate the free space cache EXTENT_DATA in root tree leaf and delete the
3701 * need to note the owner on the reloc data root so that when
3703 * correct eventual owner (rather than the reloc data root).
3706 struct btrfs_root *root = BTRFS_I(rc->data_inode)->root;
3711 root->relocation_src_root = owning_root_id;
3824 struct btrfs_root *root, u64 objectid)
3835 ret = btrfs_insert_empty_inode(trans, root, path, objectid);
3854 struct btrfs_root *root, u64 objectid)
3869 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
3875 ret = btrfs_del_item(trans, root, path);
3892 struct btrfs_root *root;
3896 root = btrfs_grab_root(fs_info->data_reloc_root);
3897 trans = btrfs_start_transaction(root, 6);
3899 btrfs_put_root(root);
3903 ret = btrfs_get_free_objectid(root, &objectid);
3907 ret = __insert_orphan_inode(trans, root, objectid);
3911 inode = btrfs_iget(fs_info->sb, objectid, root);
3913 delete_orphan_inode(trans, root, objectid);
3922 btrfs_put_root(root);
4185 static noinline_for_stack int mark_garbage_root(struct btrfs_root *root)
4187 struct btrfs_fs_info *fs_info = root->fs_info;
4195 memset(&root->root_item.drop_progress, 0,
4196 sizeof(root->root_item.drop_progress));
4197 btrfs_set_root_drop_level(&root->root_item, 0);
4198 btrfs_set_root_refs(&root->root_item, 0);
4200 &root->root_key, &root->root_item);
4397 struct btrfs_fs_info *fs_info = inode->root->fs_info;
4437 struct btrfs_root *root,
4441 struct btrfs_fs_info *fs_info = root->fs_info;
4452 BUG_ON(rc->stage == UPDATE_DATA_PTRS && btrfs_is_data_reloc_root(root));
4456 btrfs_root_last_snapshot(&root->root_item))
4459 if (btrfs_root_id(root) == BTRFS_TREE_RELOC_OBJECTID && rc->create_reloc_tree) {
4485 ret = replace_file_extents(trans, rc, root, cow);
4496 struct btrfs_root *root = pending->root;
4497 struct reloc_control *rc = root->fs_info->reloc_ctl;
4499 if (!rc || !have_reloc_root(root))
4505 root = root->reloc_root;
4506 BUG_ON(btrfs_root_refs(&root->root_item) == 0);
4522 * and create reloc root for the newly created snapshot
4525 * references held on the reloc_root, one for root->reloc_root and one for
4531 struct btrfs_root *root = pending->root;
4534 struct reloc_control *rc = root->fs_info->reloc_ctl;
4537 if (!rc || !have_reloc_root(root))
4540 rc = root->fs_info->reloc_ctl;
4552 reloc_root = create_reloc_root(trans, root->reloc_root, btrfs_root_id(new_root));
4566 ret = clone_backref_node(trans, rc, root, reloc_root);