]> git.ipfire.org Git - thirdparty/linux.git/commitdiff
btrfs: add helper to calculate space for delayed references
authorFilipe Manana <fdmanana@suse.com>
Tue, 21 Mar 2023 11:13:55 +0000 (11:13 +0000)
committerDavid Sterba <dsterba@suse.com>
Mon, 17 Apr 2023 16:01:19 +0000 (18:01 +0200)
Instead of duplicating the logic for calculating how much space is
required for a given number of delayed references, add an inline helper
to encapsulate that logic and use it everywhere we are calculating the
space required.

Reviewed-by: Josef Bacik <josef@toxicpanda.com>
Signed-off-by: Filipe Manana <fdmanana@suse.com>
Reviewed-by: David Sterba <dsterba@suse.com>
Signed-off-by: David Sterba <dsterba@suse.com>
fs/btrfs/delayed-ref.c
fs/btrfs/delayed-ref.h
fs/btrfs/space-info.c

index b58a7e30d37c4821a20534444472e9bd99dd94f6..0b32432d7d560b5460cc38e51fdef45326ad230e 100644 (file)
@@ -65,20 +65,9 @@ bool btrfs_check_space_for_delayed_refs(struct btrfs_fs_info *fs_info)
 void btrfs_delayed_refs_rsv_release(struct btrfs_fs_info *fs_info, int nr)
 {
        struct btrfs_block_rsv *block_rsv = &fs_info->delayed_refs_rsv;
-       u64 num_bytes = btrfs_calc_insert_metadata_size(fs_info, nr);
+       const u64 num_bytes = btrfs_calc_delayed_ref_bytes(fs_info, nr);
        u64 released = 0;
 
-       /*
-        * We have to check the mount option here because we could be enabling
-        * the free space tree for the first time and don't have the compat_ro
-        * option set yet.
-        *
-        * We need extra reservations if we have the free space tree because
-        * we'll have to modify that tree as well.
-        */
-       if (btrfs_test_opt(fs_info, FREE_SPACE_TREE))
-               num_bytes *= 2;
-
        released = btrfs_block_rsv_release(fs_info, block_rsv, num_bytes, NULL);
        if (released)
                trace_btrfs_space_reservation(fs_info, "delayed_refs_rsv",
@@ -100,18 +89,8 @@ void btrfs_update_delayed_refs_rsv(struct btrfs_trans_handle *trans)
        if (!trans->delayed_ref_updates)
                return;
 
-       num_bytes = btrfs_calc_insert_metadata_size(fs_info,
-                                                   trans->delayed_ref_updates);
-       /*
-        * We have to check the mount option here because we could be enabling
-        * the free space tree for the first time and don't have the compat_ro
-        * option set yet.
-        *
-        * We need extra reservations if we have the free space tree because
-        * we'll have to modify that tree as well.
-        */
-       if (btrfs_test_opt(fs_info, FREE_SPACE_TREE))
-               num_bytes *= 2;
+       num_bytes = btrfs_calc_delayed_ref_bytes(fs_info,
+                                                trans->delayed_ref_updates);
 
        spin_lock(&delayed_rsv->lock);
        delayed_rsv->size += num_bytes;
@@ -182,21 +161,10 @@ int btrfs_delayed_refs_rsv_refill(struct btrfs_fs_info *fs_info,
                                  enum btrfs_reserve_flush_enum flush)
 {
        struct btrfs_block_rsv *block_rsv = &fs_info->delayed_refs_rsv;
-       u64 limit = btrfs_calc_insert_metadata_size(fs_info, 1);
+       u64 limit = btrfs_calc_delayed_ref_bytes(fs_info, 1);
        u64 num_bytes = 0;
        int ret = -ENOSPC;
 
-       /*
-        * We have to check the mount option here because we could be enabling
-        * the free space tree for the first time and don't have the compat_ro
-        * option set yet.
-        *
-        * We need extra reservations if we have the free space tree because
-        * we'll have to modify that tree as well.
-        */
-       if (btrfs_test_opt(fs_info, FREE_SPACE_TREE))
-               limit *= 2;
-
        spin_lock(&block_rsv->lock);
        if (block_rsv->reserved < block_rsv->size) {
                num_bytes = block_rsv->size - block_rsv->reserved;
index 6cf1adc9a01ace6967328458f8865995c58d3bd6..b54261fe509ba4914760c4f264d73d3843b6f428 100644 (file)
@@ -253,6 +253,27 @@ extern struct kmem_cache *btrfs_delayed_extent_op_cachep;
 int __init btrfs_delayed_ref_init(void);
 void __cold btrfs_delayed_ref_exit(void);
 
+static inline u64 btrfs_calc_delayed_ref_bytes(const struct btrfs_fs_info *fs_info,
+                                              int num_delayed_refs)
+{
+       u64 num_bytes;
+
+       num_bytes = btrfs_calc_insert_metadata_size(fs_info, num_delayed_refs);
+
+       /*
+        * We have to check the mount option here because we could be enabling
+        * the free space tree for the first time and don't have the compat_ro
+        * option set yet.
+        *
+        * We need extra reservations if we have the free space tree because
+        * we'll have to modify that tree as well.
+        */
+       if (btrfs_test_opt(fs_info, FREE_SPACE_TREE))
+               num_bytes *= 2;
+
+       return num_bytes;
+}
+
 static inline void btrfs_init_generic_ref(struct btrfs_ref *generic_ref,
                                int action, u64 bytenr, u64 len, u64 parent)
 {
index a2e14c4104161618cea148c6cd9da22a6d0c2cd9..75e7fa337e66c81ea003e91709a0f0c5d9a84ed6 100644 (file)
@@ -553,21 +553,9 @@ static inline u64 calc_reclaim_items_nr(const struct btrfs_fs_info *fs_info,
 static inline u64 calc_delayed_refs_nr(const struct btrfs_fs_info *fs_info,
                                       u64 to_reclaim)
 {
-       u64 bytes;
+       const u64 bytes = btrfs_calc_delayed_ref_bytes(fs_info, 1);
        u64 nr;
 
-       bytes = btrfs_calc_insert_metadata_size(fs_info, 1);
-       /*
-        * We have to check the mount option here because we could be enabling
-        * the free space tree for the first time and don't have the compat_ro
-        * option set yet.
-        *
-        * We need extra reservations if we have the free space tree because
-        * we'll have to modify that tree as well.
-        */
-       if (btrfs_test_opt(fs_info, FREE_SPACE_TREE))
-               bytes *= 2;
-
        nr = div64_u64(to_reclaim, bytes);
        if (!nr)
                nr = 1;