btrfs: defrag: fix the wrong defragged number of sectors
PATCH 1/1 btrfs: defrag: fix the wrong defragged number of sectors
[BUG]
Since the recent rework on btrfs defrag to support subpage, the number of defragged sectors is no longer reported in sector size, but in byte.
Normally this is fine and defrag ioctl won't return the number of defragged sectors anyway.
But it will change the behavior of auto-defrag, which has re-defrag behavior based on the defragged sectors.
Furthermore, the refactor changed how we report the number of defragged sectors.
The refactor no longer reports the real defragged sectors, but just the initial scan, which can report more bytes than the defragged sectors.
[FIX]
Fix the problems by:
- Pass @sectors_defragged to defrag_one_range() and update it in the real loop where we defrag the file
- Properly calculate the @sectors_defragged using number of sectors, not number of bytes.
--- fs/btrfs/ioctl.c | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-)
fs/btrfs/ioctl.c +9 -3
@@ -1343,7 +1343,8 @@ static int defrag_one_locked_target(struct btrfs_inode *inode, }static int defrag_one_range (struct btrfs_inode *inode,u64 start,u32 len, -u32 extent_thresh,u64 newer_than,bool do_compress) +u32 extent_thresh,u64 newer_than,bool do_compress, +unsigned long *sectors_defragged) {struct extent_state *cached_state =NULL ;struct defrag_target_range *entry;
@@ -1398,6 +1399,8 @@ static int defrag_one_range(struct btrfs_inode *inode, u64 start, u32 len, &cached_state);if (ret <0 )break ; + *sectors_defragged += entry->len >> + (inode->root->fs_info->sectorsize_bits); }list_for_each_entry_safe (entry, tmp, &target_list, list) {
@@ -1462,10 +1465,10 @@ static int defrag_one_cluster(struct btrfs_inode *inode,* accounting. */ ret =defrag_one_range (inode, entry->start, range_len, - extent_thresh, newer_than, do_compress); + extent_thresh, newer_than, do_compress, + sectors_defragged);if (ret <0 )break ; - *sectors_defragged += range_len; } out:list_for_each_entry_safe (entry, tmp, &target_list, list) {
@@ -1484,6 +1487,9 @@ static int defrag_one_cluster(struct btrfs_inode *inode,* @newer_than: minimum transid to defrag * @max_to_defrag: max number of sectors to be defragged, if 0, the whole inode * will be defragged. +* +* Return <0 for error. +* Return >=0 for the number of sectors defragged. */ int btrfs_defrag_file(struct inode *inode, struct file_ra_state *ra, struct btrfs_ioctl_defrag_range_args *range,
Signature · 1 line
2.34.1
Quoted 54 lines · Qu Wenruo
[BUG]
Since the recent rework on btrfs defrag to support subpage, the number of defragged sectors is no longer reported in sector size, but in byte.Normally this is fine and defrag ioctl won't return the number of defragged sectors anyway.
But it will change the behavior of auto-defrag, which has re-defrag behavior based on the defragged sectors.
Furthermore, the refactor changed how we report the number of defragged sectors.
The refactor no longer reports the real defragged sectors, but just the initial scan, which can report more bytes than the defragged sectors.[FIX]
Fix the problems by:- Pass @sectors_defragged to
defrag_one_range()and update it in the real loop where we defrag the file- Properly calculate the @sectors_defragged using number of sectors, not number of bytes.
Fixes 7b508037d4ca ("btrfs: defrag: use defrag_one_cluster() to implement btrfs_defrag_file()")Signed-off-by Qu Wenruo <[email protected]>--- fs/btrfs/ioctl.c | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-)fs/btrfs/ioctl.c +4
-1@@ -1343,7 +1343,8 @@ static int defrag_one_locked_target(struct btrfs_inode *inode, }static int defrag_one_range (struct btrfs_inode *inode,u64 start,u32 len, -u32 extent_thresh,u64 newer_than,bool do_compress) +u32 extent_thresh,u64 newer_than,bool do_compress, +unsigned long *sectors_defragged) {struct extent_state *cached_state =NULL ;struct defrag_target_range *entry;@@ -1398,6 +1399,8 @@ static int defrag_one_range(struct btrfs_inode *inode, u64 start, u32 len, &cached_state);if (ret <0 )break ; + *sectors_defragged += entry->len >> + (inode->root->fs_info->sectorsize_bits); }list_for_each_entry_safe (entry, tmp, &target_list, list) {
fs/btrfs/ioctl.c static int defrag_one_cluster(struct btrfs_inode *inode, · line 1466
Quoted 2 lines
fs/btrfs/ioctl.c +0
-0@@ -1462,10 +1465,10 @@ static int defrag_one_cluster(struct btrfs_inode *inode,* accounting. */
Just lines above, there is a clamping using @max_sectors against @sectors_defragged.
And that is the root cause of the extra IO.
As @sectors_defragged is returned in bytes, while @max_sectors are normally just 1024 from auto-defrag, this means later calculation of @max_sectors - @sectors_defragged always underflow if we defragged any bytes, rendering the whole @max_sectors limits useless.
Although my refactor slightly changed the policy (like no more adjacent check for previous extents), it shouldn't cause that huge change in IO. Thus it's really the bad @sectors_defragged causing the bug.
Will include this part into the changelog in next version, and to be extra safe, will change the @max_sectors check against @sectors_defragged using max_sectors <= sectors_defragged, just in case we exceeded the limit by somehow.
Thanks,
Qu
Quoted 20 lines
fs/btrfs/ioctl.c +5
-2ret =defrag_one_range (inode, entry->start, range_len, - extent_thresh, newer_than, do_compress); + extent_thresh, newer_than, do_compress, + sectors_defragged);if (ret <0 )break ; - *sectors_defragged += range_len; } out:list_for_each_entry_safe (entry, tmp, &target_list, list) {@@ -1484,6 +1487,9 @@ static int defrag_one_cluster(struct btrfs_inode *inode,* @newer_than: minimum transid to defrag * @max_to_defrag: max number of sectors to be defragged, if 0, the whole inode * will be defragged. +* +* Return <0 for error. +* Return >=0 for the number of sectors defragged. */ int btrfs_defrag_file(struct inode *inode, struct file_ra_state *ra, struct btrfs_ioctl_defrag_range_args *range,