btrfs: extent-tree: Fix a bug that btrfs is unable to add pinned bytes
authorQu Wenruo <wqu@suse.com>
Fri, 10 May 2019 04:45:05 +0000 (12:45 +0800)
committerDavid Sterba <dsterba@suse.com>
Thu, 16 May 2019 12:31:13 +0000 (14:31 +0200)
Commit ddf30cf03fb5 ("btrfs: extent-tree: Use btrfs_ref to refactor
add_pinned_bytes()") refactored add_pinned_bytes(), but during that
refactor, there are two callers which add the pinned bytes instead
of subtracting.

That refactor misses those two caller, causing incorrect pinned bytes
calculation and resulting unexpected ENOSPC error.

Fix it by adding a new parameter @sign to restore the original behavior.

Reported-by: kernel test robot <rong.a.chen@intel.com>
Fixes: ddf30cf03fb5 ("btrfs: extent-tree: Use btrfs_ref to refactor add_pinned_bytes()")
Signed-off-by: Qu Wenruo <wqu@suse.com>
Signed-off-by: David Sterba <dsterba@suse.com>
fs/btrfs/extent-tree.c

index 9bcb3570750eec1f0418b22e6ccb65b0a9f461a2..1aee51a9f3bfa28f23fbb26d131be6bfc650ba33 100644 (file)
@@ -757,12 +757,14 @@ static struct btrfs_space_info *__find_space_info(struct btrfs_fs_info *info,
 }
 
 static void add_pinned_bytes(struct btrfs_fs_info *fs_info,
-                            struct btrfs_ref *ref)
+                            struct btrfs_ref *ref, int sign)
 {
        struct btrfs_space_info *space_info;
-       s64 num_bytes = -ref->len;
+       s64 num_bytes;
        u64 flags;
 
+       ASSERT(sign == 1 || sign == -1);
+       num_bytes = sign * ref->len;
        if (ref->type == BTRFS_REF_METADATA) {
                if (ref->tree_ref.root == BTRFS_CHUNK_TREE_OBJECTID)
                        flags = BTRFS_BLOCK_GROUP_SYSTEM;
@@ -2063,7 +2065,7 @@ int btrfs_inc_extent_ref(struct btrfs_trans_handle *trans,
        btrfs_ref_tree_mod(fs_info, generic_ref);
 
        if (ret == 0 && old_ref_mod < 0 && new_ref_mod >= 0)
-               add_pinned_bytes(fs_info, generic_ref);
+               add_pinned_bytes(fs_info, generic_ref, -1);
 
        return ret;
 }
@@ -7189,7 +7191,7 @@ void btrfs_free_tree_block(struct btrfs_trans_handle *trans,
        }
 out:
        if (pin)
-               add_pinned_bytes(fs_info, &generic_ref);
+               add_pinned_bytes(fs_info, &generic_ref, 1);
 
        if (last_ref) {
                /*
@@ -7237,7 +7239,7 @@ int btrfs_free_extent(struct btrfs_trans_handle *trans, struct btrfs_ref *ref)
                btrfs_ref_tree_mod(fs_info, ref);
 
        if (ret == 0 && old_ref_mod >= 0 && new_ref_mod < 0)
-               add_pinned_bytes(fs_info, ref);
+               add_pinned_bytes(fs_info, ref, 1);
 
        return ret;
 }