Skip to content

Commit ebb7044

Browse files
Liu Bokdave
authored andcommitted
Btrfs: fix list_add corruption and soft lockups in fsync
Xfstests btrfs/146 revealed this corruption, [ 58.138831] Buffer I/O error on dev dm-0, logical block 2621424, async page read [ 58.151233] BTRFS error (device sdf): bdev /dev/mapper/error-test errs: wr 1, rd 0, flush 0, corrupt 0, gen 0 [ 58.152403] list_add corruption. prev->next should be next (ffff88005e6775d8), but was ffffc9000189be88. (prev=ffffc9000189be88). [ 58.153518] ------------[ cut here ]------------ [ 58.153892] WARNING: CPU: 1 PID: 1287 at lib/list_debug.c:31 __list_add_valid+0x169/0x1f0 ... [ 58.157379] RIP: 0010:__list_add_valid+0x169/0x1f0 ... [ 58.161956] Call Trace: [ 58.162264] btrfs_log_inode_parent+0x5bd/0xfb0 [btrfs] [ 58.163583] btrfs_log_dentry_safe+0x60/0x80 [btrfs] [ 58.164003] btrfs_sync_file+0x4c2/0x6f0 [btrfs] [ 58.164393] vfs_fsync_range+0x5f/0xd0 [ 58.164898] do_fsync+0x5a/0x90 [ 58.165170] SyS_fsync+0x10/0x20 [ 58.165395] entry_SYSCALL_64_fastpath+0x1f/0xbe ... It turns out that we could record btrfs_log_ctx:io_err in log_one_extents when IO fails, but make log_one_extents() return '0' instead of -EIO, so the IO error is not acknowledged by the callers, i.e. btrfs_log_inode_parent(), which would remove btrfs_log_ctx:list from list head 'root->log_ctxs'. Since btrfs_log_ctx is allocated from stack memory, it'd get freed with a object alive on the list. then a future list_add will throw the above warning. This returns the correct error in the above case. Jeff also reported this while testing against his fsync error patch set[1]. [1]: https://www.spinics.net/lists/linux-btrfs/msg65308.html "btrfs list corruption and soft lockups while testing writeback error handling" Fixes: 8407f55 ("Btrfs: fix data corruption after fast fsync and writeback error") Signed-off-by: Liu Bo <[email protected]> Reviewed-by: David Sterba <[email protected]> Signed-off-by: David Sterba <[email protected]>
1 parent eae8d82 commit ebb7044

File tree

2 files changed

+4
-3
lines changed

2 files changed

+4
-3
lines changed

fs/btrfs/file.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2057,6 +2057,8 @@ int btrfs_sync_file(struct file *file, loff_t start, loff_t end, int datasync)
20572057
len = (u64)end - (u64)start + 1;
20582058
trace_btrfs_sync_file(file, datasync);
20592059

2060+
btrfs_init_log_ctx(&ctx, inode);
2061+
20602062
/*
20612063
* We write the dirty pages in the range and wait until they complete
20622064
* out of the ->i_mutex. If so, we can flush the dirty pages by
@@ -2203,8 +2205,6 @@ int btrfs_sync_file(struct file *file, loff_t start, loff_t end, int datasync)
22032205
}
22042206
trans->sync = true;
22052207

2206-
btrfs_init_log_ctx(&ctx, inode);
2207-
22082208
ret = btrfs_log_dentry_safe(trans, root, dentry, start, end, &ctx);
22092209
if (ret < 0) {
22102210
/* Fallthrough and commit/free transaction. */
@@ -2262,6 +2262,7 @@ int btrfs_sync_file(struct file *file, loff_t start, loff_t end, int datasync)
22622262
ret = btrfs_end_transaction(trans);
22632263
}
22642264
out:
2265+
ASSERT(list_empty(&ctx.list));
22652266
err = file_check_and_advance_wb_err(file);
22662267
if (!ret)
22672268
ret = err;

fs/btrfs/tree-log.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4102,7 +4102,7 @@ static int log_one_extent(struct btrfs_trans_handle *trans,
41024102

41034103
if (ordered_io_err) {
41044104
ctx->io_err = -EIO;
4105-
return 0;
4105+
return ctx->io_err;
41064106
}
41074107

41084108
btrfs_init_map_token(&token);

0 commit comments

Comments
 (0)