Skip to content

Commit e44163e

Browse files
jeffmahoneymasoncl
authored andcommitted
btrfs: explictly delete unused block groups in close_ctree and ro-remount
The cleaner thread may already be sleeping by the time we enter close_ctree. If that's the case, we'll skip removing any unused block groups queued for removal, even during a normal umount. They'll be cleaned up automatically at next mount, but users expect a umount to be a clean synchronization point, especially when used on thin-provisioned storage with -odiscard. We also explicitly remove unused block groups in the ro-remount path for the same reason. Signed-off-by: Jeff Mahoney <jeffm@suse.com> Reviewed-by: Filipe Manana <fdmanana@suse.com> Tested-by: Filipe Manana <fdmanana@suse.com> Signed-off-by: Chris Mason <clm@fb.com>
1 parent 499f377 commit e44163e

File tree

2 files changed

+20
-0
lines changed

2 files changed

+20
-0
lines changed

fs/btrfs/disk-io.c

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3767,6 +3767,15 @@ void close_ctree(struct btrfs_root *root)
37673767
cancel_work_sync(&fs_info->async_reclaim_work);
37683768

37693769
if (!(fs_info->sb->s_flags & MS_RDONLY)) {
3770+
/*
3771+
* If the cleaner thread is stopped and there are
3772+
* block groups queued for removal, the deletion will be
3773+
* skipped when we quit the cleaner thread.
3774+
*/
3775+
mutex_lock(&root->fs_info->cleaner_mutex);
3776+
btrfs_delete_unused_bgs(root->fs_info);
3777+
mutex_unlock(&root->fs_info->cleaner_mutex);
3778+
37703779
ret = btrfs_commit_super(root);
37713780
if (ret)
37723781
btrfs_err(fs_info, "commit super ret %d", ret);

fs/btrfs/super.c

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1650,6 +1650,17 @@ static int btrfs_remount(struct super_block *sb, int *flags, char *data)
16501650

16511651
sb->s_flags |= MS_RDONLY;
16521652

1653+
/*
1654+
* Setting MS_RDONLY will put the cleaner thread to
1655+
* sleep at the next loop if it's already active.
1656+
* If it's already asleep, we'll leave unused block
1657+
* groups on disk until we're mounted read-write again
1658+
* unless we clean them up here.
1659+
*/
1660+
mutex_lock(&root->fs_info->cleaner_mutex);
1661+
btrfs_delete_unused_bgs(fs_info);
1662+
mutex_unlock(&root->fs_info->cleaner_mutex);
1663+
16531664
btrfs_dev_replace_suspend_for_unmount(fs_info);
16541665
btrfs_scrub_cancel(fs_info);
16551666
btrfs_pause_balance(fs_info);

0 commit comments

Comments
 (0)