Skip to content

Commit 39a0705

Browse files
adam900710gregkh
authored andcommitted
btrfs: scrub: try to fix super block errors
[ Upstream commit f9eab5f ] [BUG] The following script shows that, although scrub can detect super block errors, it never tries to fix it: mkfs.btrfs -f -d raid1 -m raid1 $dev1 $dev2 xfs_io -c "pwrite 67108864 4k" $dev2 mount $dev1 $mnt btrfs scrub start -B $dev2 btrfs scrub start -Br $dev2 umount $mnt The first scrub reports the super error correctly: scrub done for f3289218-abd3-41ac-a630-202f766c0859 Scrub started: Tue Aug 2 14:44:11 2022 Status: finished Duration: 0:00:00 Total to scrub: 1.26GiB Rate: 0.00B/s Error summary: super=1 Corrected: 0 Uncorrectable: 0 Unverified: 0 But the second read-only scrub still reports the same super error: Scrub started: Tue Aug 2 14:44:11 2022 Status: finished Duration: 0:00:00 Total to scrub: 1.26GiB Rate: 0.00B/s Error summary: super=1 Corrected: 0 Uncorrectable: 0 Unverified: 0 [CAUSE] The comments already shows that super block can be easily fixed by committing a transaction: /* * If we find an error in a super block, we just report it. * They will get written with the next transaction commit * anyway */ But the truth is, such assumption is not always true, and since scrub should try to repair every error it found (except for read-only scrub), we should really actively commit a transaction to fix this. [FIX] Just commit a transaction if we found any super block errors, after everything else is done. We cannot do this just after scrub_supers(), as btrfs_commit_transaction() will try to pause and wait for the running scrub, thus we can not call it with scrub_lock hold. Signed-off-by: Qu Wenruo <[email protected]> Reviewed-by: David Sterba <[email protected]> Signed-off-by: David Sterba <[email protected]> Signed-off-by: Sasha Levin <[email protected]>
1 parent f3857dd commit 39a0705

File tree

1 file changed

+36
-0
lines changed

1 file changed

+36
-0
lines changed

fs/btrfs/scrub.c

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4072,6 +4072,7 @@ int btrfs_scrub_dev(struct btrfs_fs_info *fs_info, u64 devid, u64 start,
40724072
int ret;
40734073
struct btrfs_device *dev;
40744074
unsigned int nofs_flag;
4075+
bool need_commit = false;
40754076

40764077
if (btrfs_fs_closing(fs_info))
40774078
return -EAGAIN;
@@ -4177,6 +4178,12 @@ int btrfs_scrub_dev(struct btrfs_fs_info *fs_info, u64 devid, u64 start,
41774178
*/
41784179
nofs_flag = memalloc_nofs_save();
41794180
if (!is_dev_replace) {
4181+
u64 old_super_errors;
4182+
4183+
spin_lock(&sctx->stat_lock);
4184+
old_super_errors = sctx->stat.super_errors;
4185+
spin_unlock(&sctx->stat_lock);
4186+
41804187
btrfs_info(fs_info, "scrub: started on devid %llu", devid);
41814188
/*
41824189
* by holding device list mutex, we can
@@ -4185,6 +4192,16 @@ int btrfs_scrub_dev(struct btrfs_fs_info *fs_info, u64 devid, u64 start,
41854192
mutex_lock(&fs_info->fs_devices->device_list_mutex);
41864193
ret = scrub_supers(sctx, dev);
41874194
mutex_unlock(&fs_info->fs_devices->device_list_mutex);
4195+
4196+
spin_lock(&sctx->stat_lock);
4197+
/*
4198+
* Super block errors found, but we can not commit transaction
4199+
* at current context, since btrfs_commit_transaction() needs
4200+
* to pause the current running scrub (hold by ourselves).
4201+
*/
4202+
if (sctx->stat.super_errors > old_super_errors && !sctx->readonly)
4203+
need_commit = true;
4204+
spin_unlock(&sctx->stat_lock);
41884205
}
41894206

41904207
if (!ret)
@@ -4211,6 +4228,25 @@ int btrfs_scrub_dev(struct btrfs_fs_info *fs_info, u64 devid, u64 start,
42114228
scrub_workers_put(fs_info);
42124229
scrub_put_ctx(sctx);
42134230

4231+
/*
4232+
* We found some super block errors before, now try to force a
4233+
* transaction commit, as scrub has finished.
4234+
*/
4235+
if (need_commit) {
4236+
struct btrfs_trans_handle *trans;
4237+
4238+
trans = btrfs_start_transaction(fs_info->tree_root, 0);
4239+
if (IS_ERR(trans)) {
4240+
ret = PTR_ERR(trans);
4241+
btrfs_err(fs_info,
4242+
"scrub: failed to start transaction to fix super block errors: %d", ret);
4243+
return ret;
4244+
}
4245+
ret = btrfs_commit_transaction(trans);
4246+
if (ret < 0)
4247+
btrfs_err(fs_info,
4248+
"scrub: failed to commit transaction to fix super block errors: %d", ret);
4249+
}
42144250
return ret;
42154251
out:
42164252
scrub_workers_put(fs_info);

0 commit comments

Comments
 (0)