btrfs: accurately calculate number of delayed refs when flushing
authorFilipe Manana <fdmanana@suse.com>
Tue, 21 Mar 2023 11:13:52 +0000 (11:13 +0000)
committerDavid Sterba <dsterba@suse.com>
Mon, 17 Apr 2023 16:01:19 +0000 (18:01 +0200)
When flushing a limited number of delayed references (FLUSH_DELAYED_REFS_NR
state), we are assuming each delayed reference is holding a number of bytes
matching the needed space for inserting for a single metadata item (the
result of btrfs_calc_insert_metadata_size()). That is not correct when
using the free space tree, as in that case we have to multiply that value
by 2 since we need to touch the free space tree as well. This is the same
computation as we do at btrfs_update_delayed_refs_rsv() and at
btrfs_delayed_refs_rsv_release().

So correct the computation for the amount of delayed references we need to
flush in case we have the free space tree. This does not fix a functional
issue, instead it makes the flush code flush less delayed references, only
the minimum necessary to satisfy a ticket.

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/space-info.c

index 5eb161d..f36b16e 100644 (file)
@@ -550,6 +550,30 @@ static inline u64 calc_reclaim_items_nr(struct btrfs_fs_info *fs_info,
        return nr;
 }
 
+static inline u64 calc_delayed_refs_nr(struct btrfs_fs_info *fs_info,
+                                      u64 to_reclaim)
+{
+       u64 bytes;
+       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;
+       return nr;
+}
+
 #define EXTENT_SIZE_PER_ITEM   SZ_256K
 
 /*
@@ -727,7 +751,7 @@ static void flush_space(struct btrfs_fs_info *fs_info,
                        break;
                }
                if (state == FLUSH_DELAYED_REFS_NR)
-                       nr = calc_reclaim_items_nr(fs_info, num_bytes);
+                       nr = calc_delayed_refs_nr(fs_info, num_bytes);
                else
                        nr = 0;
                btrfs_run_delayed_refs(trans, nr);