btrfs: update writeback index when starting defrag
authorFilipe Manana <fdmanana@suse.com>
Thu, 20 Jan 2022 17:41:17 +0000 (17:41 +0000)
committerDavid Sterba <dsterba@suse.com>
Mon, 24 Jan 2022 17:16:28 +0000 (18:16 +0100)
commit27cdfde181bcacd226c230b2fd831f6f5b8c215f
tree15096809b17500a4b9ae1bc0851ebb023c1d76f6
parent3c9d31c715948aaff0ee6d322a91a2dec07770bf
btrfs: update writeback index when starting defrag

When starting a defrag, we should update the writeback index of the
inode's mapping in case it currently has a value beyond the start of the
range we are defragging. This can help performance and often result in
getting less extents after writeback - for e.g., if the current value
of the writeback index sits somewhere in the middle of a range that
gets dirty by the defrag, then after writeback we can get two smaller
extents instead of a single, larger extent.

We used to have this before the refactoring in 5.16, but it was removed
without any reason to do so. Originally it was added in kernel 3.1, by
commit 2a0f7f5769992b ("Btrfs: fix recursive auto-defrag"), in order to
fix a loop with autodefrag resulting in dirtying and writing pages over
and over, but some testing on current code did not show that happening,
at least with the test described in that commit.

So add back the behaviour, as at the very least it is a nice to have
optimization.

Fixes: 7b508037d4cac3 ("btrfs: defrag: use defrag_one_cluster() to implement btrfs_defrag_file()")
CC: stable@vger.kernel.org # 5.16
Signed-off-by: Filipe Manana <fdmanana@suse.com>
Signed-off-by: David Sterba <dsterba@suse.com>
fs/btrfs/ioctl.c