btrfs: qgroup: track metadata relocation COW with simple quota
authorBoris Burkov <boris@bur.io>
Thu, 22 Jun 2023 00:37:23 +0000 (17:37 -0700)
committerDavid Sterba <dsterba@suse.com>
Thu, 12 Oct 2023 14:44:12 +0000 (16:44 +0200)
Relocation COWs metadata blocks in two cases for the reloc root:

- copying the subvolume root item when creating the reloc root
- copying a btree node when there is a COW during relocation

In both cases, the resulting btree node hits an abnormal code path with
respect to the owner field in its btrfs_header. It first creates the
root item for the new objectid, which populates the reloc root id, and
it at this point that delayed refs are created.

Later, it fully copies the old node into the new node (including the
original owner field) which overwrites it. This results in a simple
quotas mismatch where we run the delayed ref for the reloc root which
has no simple quota effect (reloc root is not an fstree) but when we
ultimately delete the node, the owner is the real original fstree and we
do free the space.

To work around this without tampering with the behavior of relocation,
add a parameter to btrfs_add_tree_block that lets the relocation code
path specify a different owning root than the "operating" root (in this
case, owning root is the real root and the operating root is the reloc
root). These can naturally be plumbed into delayed refs that have the
same concept.

Note that this is a double count in some sense, but a relatively natural
one, as there are really two extents, and the old one will be deleted
soon. This is consistent with how data relocation extents are accounted
by simple quotas.

Reviewed-by: Josef Bacik <josef@toxicpanda.com>
Signed-off-by: Boris Burkov <boris@bur.io>
Signed-off-by: David Sterba <dsterba@suse.com>
fs/btrfs/ctree.c
fs/btrfs/disk-io.c
fs/btrfs/extent-tree.c
fs/btrfs/extent-tree.h
fs/btrfs/ioctl.c

index 2a51cac..2d29a48 100644 (file)
@@ -316,6 +316,7 @@ int btrfs_copy_root(struct btrfs_trans_handle *trans,
        int ret = 0;
        int level;
        struct btrfs_disk_key disk_key;
+       u64 reloc_src_root = 0;
 
        WARN_ON(test_bit(BTRFS_ROOT_SHAREABLE, &root->state) &&
                trans->transid != fs_info->running_transaction->transid);
@@ -328,9 +329,11 @@ int btrfs_copy_root(struct btrfs_trans_handle *trans,
        else
                btrfs_node_key(buf, &disk_key, 0);
 
+       if (new_root_objectid == BTRFS_TREE_RELOC_OBJECTID)
+               reloc_src_root = btrfs_header_owner(buf);
        cow = btrfs_alloc_tree_block(trans, root, 0, new_root_objectid,
                                     &disk_key, level, buf->start, 0,
-                                    BTRFS_NESTING_NEW_ROOT);
+                                    reloc_src_root, BTRFS_NESTING_NEW_ROOT);
        if (IS_ERR(cow))
                return PTR_ERR(cow);
 
@@ -522,6 +525,7 @@ static noinline int __btrfs_cow_block(struct btrfs_trans_handle *trans,
        int last_ref = 0;
        int unlock_orig = 0;
        u64 parent_start = 0;
+       u64 reloc_src_root = 0;
 
        if (*cow_ret == buf)
                unlock_orig = 1;
@@ -540,12 +544,14 @@ static noinline int __btrfs_cow_block(struct btrfs_trans_handle *trans,
        else
                btrfs_node_key(buf, &disk_key, 0);
 
-       if ((root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID) && parent)
-               parent_start = parent->start;
-
+       if (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID) {
+               if (parent)
+                       parent_start = parent->start;
+               reloc_src_root = btrfs_header_owner(buf);
+       }
        cow = btrfs_alloc_tree_block(trans, root, parent_start,
                                     root->root_key.objectid, &disk_key, level,
-                                    search_start, empty_size, nest);
+                                    search_start, empty_size, reloc_src_root, nest);
        if (IS_ERR(cow))
                return PTR_ERR(cow);
 
@@ -2983,7 +2989,7 @@ static noinline int insert_new_root(struct btrfs_trans_handle *trans,
 
        c = btrfs_alloc_tree_block(trans, root, 0, root->root_key.objectid,
                                   &lower_key, level, root->node->start, 0,
-                                  BTRFS_NESTING_NEW_ROOT);
+                                  0, BTRFS_NESTING_NEW_ROOT);
        if (IS_ERR(c))
                return PTR_ERR(c);
 
@@ -3127,7 +3133,7 @@ static noinline int split_node(struct btrfs_trans_handle *trans,
 
        split = btrfs_alloc_tree_block(trans, root, 0, root->root_key.objectid,
                                       &disk_key, level, c->start, 0,
-                                      BTRFS_NESTING_SPLIT);
+                                      0, BTRFS_NESTING_SPLIT);
        if (IS_ERR(split))
                return PTR_ERR(split);
 
@@ -3878,7 +3884,7 @@ again:
         * use BTRFS_NESTING_NEW_ROOT.
         */
        right = btrfs_alloc_tree_block(trans, root, 0, root->root_key.objectid,
-                                      &disk_key, 0, l->start, 0,
+                                      &disk_key, 0, l->start, 0, 0,
                                       num_doubles ? BTRFS_NESTING_NEW_ROOT :
                                       BTRFS_NESTING_SPLIT);
        if (IS_ERR(right))
index dc577b3..fc6adbc 100644 (file)
@@ -859,7 +859,7 @@ struct btrfs_root *btrfs_create_tree(struct btrfs_trans_handle *trans,
        root->root_key.offset = 0;
 
        leaf = btrfs_alloc_tree_block(trans, root, 0, objectid, NULL, 0, 0, 0,
-                                     BTRFS_NESTING_NORMAL);
+                                     0, BTRFS_NESTING_NORMAL);
        if (IS_ERR(leaf)) {
                ret = PTR_ERR(leaf);
                leaf = NULL;
@@ -936,7 +936,7 @@ int btrfs_alloc_log_tree_node(struct btrfs_trans_handle *trans,
         */
 
        leaf = btrfs_alloc_tree_block(trans, root, 0, BTRFS_TREE_LOG_OBJECTID,
-                       NULL, 0, 0, 0, BTRFS_NESTING_NORMAL);
+                       NULL, 0, 0, 0, 0, BTRFS_NESTING_NORMAL);
        if (IS_ERR(leaf))
                return PTR_ERR(leaf);
 
index 8372d06..3ad8aec 100644 (file)
@@ -5077,6 +5077,7 @@ struct extent_buffer *btrfs_alloc_tree_block(struct btrfs_trans_handle *trans,
                                             const struct btrfs_disk_key *key,
                                             int level, u64 hint,
                                             u64 empty_size,
+                                            u64 reloc_src_root,
                                             enum btrfs_lock_nesting nest)
 {
        struct btrfs_fs_info *fs_info = root->fs_info;
@@ -5089,6 +5090,7 @@ struct extent_buffer *btrfs_alloc_tree_block(struct btrfs_trans_handle *trans,
        int ret;
        u32 blocksize = fs_info->nodesize;
        bool skinny_metadata = btrfs_fs_incompat(fs_info, SKINNY_METADATA);
+       u64 owning_root;
 
 #ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS
        if (btrfs_is_testing(fs_info)) {
@@ -5115,11 +5117,13 @@ struct extent_buffer *btrfs_alloc_tree_block(struct btrfs_trans_handle *trans,
                ret = PTR_ERR(buf);
                goto out_free_reserved;
        }
+       owning_root = btrfs_header_owner(buf);
 
        if (root_objectid == BTRFS_TREE_RELOC_OBJECTID) {
                if (parent == 0)
                        parent = ins.objectid;
                flags |= BTRFS_BLOCK_FLAG_FULL_BACKREF;
+               owning_root = reloc_src_root;
        } else
                BUG_ON(parent > 0);
 
@@ -5139,8 +5143,7 @@ struct extent_buffer *btrfs_alloc_tree_block(struct btrfs_trans_handle *trans,
                extent_op->level = level;
 
                btrfs_init_generic_ref(&generic_ref, BTRFS_ADD_DELAYED_EXTENT,
-                                      ins.objectid, ins.offset, parent,
-                                      btrfs_header_owner(buf));
+                                      ins.objectid, ins.offset, parent, owning_root);
                btrfs_init_tree_ref(&generic_ref, level, root_objectid,
                                    root->root_key.objectid, false);
                btrfs_ref_tree_mod(fs_info, &generic_ref);
index 3c43539..0716f65 100644 (file)
@@ -114,6 +114,7 @@ struct extent_buffer *btrfs_alloc_tree_block(struct btrfs_trans_handle *trans,
                                             const struct btrfs_disk_key *key,
                                             int level, u64 hint,
                                             u64 empty_size,
+                                            u64 reloc_src_root,
                                             enum btrfs_lock_nesting nest);
 void btrfs_free_tree_block(struct btrfs_trans_handle *trans,
                           u64 root_id,
index 6603b6f..6dc089a 100644 (file)
@@ -657,7 +657,7 @@ static noinline int create_subvol(struct mnt_idmap *idmap,
                goto out;
 
        leaf = btrfs_alloc_tree_block(trans, root, 0, objectid, NULL, 0, 0, 0,
-                                     BTRFS_NESTING_NORMAL);
+                                     0, BTRFS_NESTING_NORMAL);
        if (IS_ERR(leaf)) {
                ret = PTR_ERR(leaf);
                goto out;