Skip to content

Commit 088bd41

Browse files
jinbaohongfdmanana
authored andcommitted
btrfs: handle user interrupt properly in btrfs_trim_fs()
When a fatal signal is pending or the process is freezing, btrfs_trim_block_group() and btrfs_trim_free_extents() return -ERESTARTSYS. Currently this is treated as a regular error: the loops continue to the next iteration and count it as a block group or device failure. Instead, break out of the loops immediately and return -ERESTARTSYS to userspace without counting it as a failure. Also skip the device loop entirely if the block group loop was interrupted. Reviewed-by: Qu Wenruo <wqu@suse.com> Reviewed-by: Filipe Manana <fdmanana@suse.com> Signed-off-by: Robbie Ko <robbieko@synology.com> Signed-off-by: jinbaohong <jinbaohong@synology.com> Signed-off-by: Filipe Manana <fdmanana@suse.com>
1 parent 4bef0f5 commit 088bd41

File tree

1 file changed

+11
-0
lines changed

1 file changed

+11
-0
lines changed

fs/btrfs/extent-tree.c

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6665,6 +6665,10 @@ int btrfs_trim_fs(struct btrfs_fs_info *fs_info, struct fstrim_range *range)
66656665
range->minlen);
66666666

66676667
trimmed += group_trimmed;
6668+
if (ret == -ERESTARTSYS || ret == -EINTR) {
6669+
btrfs_put_block_group(cache);
6670+
break;
6671+
}
66686672
if (ret) {
66696673
bg_failed++;
66706674
if (!bg_ret)
@@ -6679,6 +6683,9 @@ int btrfs_trim_fs(struct btrfs_fs_info *fs_info, struct fstrim_range *range)
66796683
"failed to trim %llu block group(s), first error %d",
66806684
bg_failed, bg_ret);
66816685

6686+
if (ret == -ERESTARTSYS || ret == -EINTR)
6687+
return ret;
6688+
66826689
mutex_lock(&fs_devices->device_list_mutex);
66836690
list_for_each_entry(device, &fs_devices->devices, dev_list) {
66846691
if (test_bit(BTRFS_DEV_STATE_MISSING, &device->dev_state))
@@ -6687,6 +6694,8 @@ int btrfs_trim_fs(struct btrfs_fs_info *fs_info, struct fstrim_range *range)
66876694
ret = btrfs_trim_free_extents(device, &group_trimmed);
66886695

66896696
trimmed += group_trimmed;
6697+
if (ret == -ERESTARTSYS || ret == -EINTR)
6698+
break;
66906699
if (ret) {
66916700
dev_failed++;
66926701
if (!dev_ret)
@@ -6701,6 +6710,8 @@ int btrfs_trim_fs(struct btrfs_fs_info *fs_info, struct fstrim_range *range)
67016710
"failed to trim %llu device(s), first error %d",
67026711
dev_failed, dev_ret);
67036712
range->len = trimmed;
6713+
if (ret == -ERESTARTSYS || ret == -EINTR)
6714+
return ret;
67046715
if (bg_ret)
67056716
return bg_ret;
67066717
return dev_ret;

0 commit comments

Comments
 (0)