btrfs: defrag: don't try to defrag extents which are under writeback
authorQu Wenruo <wqu@suse.com>
Tue, 8 Feb 2022 06:54:05 +0000 (14:54 +0800)
committerGreg Kroah-Hartman <gregkh@linuxfoundation.org>
Wed, 23 Feb 2022 11:05:48 +0000 (12:05 +0100)
commit103b77296fee74e5ac3d3bbfda88cb26b220d85c
tree6c11c3871149b75d23d0ebcff6a5dca5157125c5
parent685de079846f7851aef8029f235ae20bd9dc3a4b
btrfs: defrag: don't try to defrag extents which are under writeback

commit 0d1ffa2228cb34f485f8fe927f134b82a0ea62ae upstream.

Once we start writeback (have called btrfs_run_delalloc_range()), we
allocate an extent, create an extent map point to that extent, with a
generation of (u64)-1, created the ordered extent and then clear the
DELALLOC bit from the range in the inode's io tree.

Such extent map can pass the first call of defrag_collect_targets(), as
its generation is (u64)-1, meets any possible minimal generation check.
And the range will not have DELALLOC bit, also passing the DELALLOC bit
check.

It will only be re-checked in the second call of
defrag_collect_targets(), which will wait for writeback.

But at that stage we have already spent our time waiting for some IO we
may or may not want to defrag.

Let's reject such extents early so we won't waste our time.

CC: stable@vger.kernel.org # 5.16
Reviewed-by: Filipe Manana <fdmanana@suse.com>
Signed-off-by: Qu Wenruo <wqu@suse.com>
Signed-off-by: David Sterba <dsterba@suse.com>
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
fs/btrfs/ioctl.c