Commit 007145ff authored by Filipe Manana's avatar Filipe Manana Committed by David Sterba
Browse files

btrfs: accurately calculate number of delayed refs when flushing



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: default avatarJosef Bacik <josef@toxicpanda.com>
Signed-off-by: default avatarFilipe Manana <fdmanana@suse.com>
Reviewed-by: default avatarDavid Sterba <dsterba@suse.com>
Signed-off-by: default avatarDavid Sterba <dsterba@suse.com>
parent 1d0df22a
Loading
Loading
Loading
Loading
+25 −1
Original line number Diff line number Diff line
@@ -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);