Btrfs: fix data corruption after fast fsync and writeback error
PATCH 1/1 Btrfs: fix data corruption after fast fsync and writeback error
When we do a fast fsync, we start all ordered operations and then while they're running in parallel we visit the list of modified extent maps and construct their matching file extent items and write them to the log btree. After that, in btrfs_sync_log() we wait for all the ordered operations to finish (via btrfs_wait_logged_extents).
The problem with this is that we were completely ignoring errors that can happen in the extent write path, such as -ENOSPC, a temporary -ENOMEM or -EIO errors for example. When such error happens, it means we have parts of the on disk extent that weren't written to, and so we end up logging file extent items that point to these extents that contain garbage/random data - so after a crash/reboot plus log replay, we get our inode's metadata pointing to those extents.
This worked in contrast with the full (non-fast) fsync path, where we start all ordered operations, wait for them to finish and then write to the log btree. In this path, after each ordered operation completes we check if it's flagged with an error (BTRFS_ORDERED_IOERR) and return -EIO if so (via btrfs_wait_ordered_range).
So if an error happens with any ordered operation, just return a -EIO error to userspace, so that it knows that not all of its previous writes were durably persisted and the application can take proper action (like redo the writes for e.g.) - and definitely not leave any file extent items in the log refer to non fully written extents.
---
This patch applies on top of the patches with the titles:
"Btrfs: fix fsync data loss after a ranged fsync" "Btrfs: fix fsync race leading to invalid data after log replay"
fs/btrfs/file.c | 19 ++++ fs/btrfs/tree-log.c | 247 ++++++++++++++++++++++++++++++---------------------- fs/btrfs/tree-log.h | 2 + 3 files changed, 166 insertions(+), 102 deletions(-)
fs/btrfs/file.c +19 -0
@@ -2045,6 +2045,25 @@ int btrfs_sync_file(struct file *file, loff_t start, loff_t end, int datasync)*/ mutex_unlock (&inode->i_mutex); +/* +* If any of the ordered extents had an error, just return it to user +* space, so that the application knows some writes didn't succeed and +* can take proper action (retry for e.g.). Blindly committing the +* transaction in this case, would fool userspace that everything was +* successful. And we also want to make sure our log doesn't contain +* file extent items pointing to extents that weren't fully written to - +* just like in the non fast fsync path, where we check for the ordered +* operation's error flag before writing to the log tree and return -EIO +* if any of them had this flag set (btrfs_wait_ordered_range) - +* therefore we need to check for errors in the ordered operations, +* which are indicated by ctx.io_err. +*/ +if (ctx.io_err) { +btrfs_end_transaction (trans, root); + ret = ctx.io_err; + goto out; + } +if (ret !=BTRFS_NO_LOG_SYNC ) {if (!ret) { ret =btrfs_sync_log (trans, root, &ctx);
fs/btrfs/tree-log.c +145 -102
335 lines not on this page · raw message
fs/btrfs/tree-log.h +2 -0
@@ -28,6 +28,7 @@struct btrfs_log_ctx {int log_ret;int log_transid; +int io_err;struct list_head list; };
@@ -35,6 +36,7 @@ static inline void btrfs_init_log_ctx(struct btrfs_log_ctx *ctx) { ctx->log_ret =0 ; ctx->log_transid =0 ; + ctx->io_err =0 ;INIT_LIST_HEAD (&ctx->list); }
Signature · 1 line
1.9.1
Quoted 20 lines · Filipe Manana
When we do a fast fsync, we start all ordered operations and then while they're running in parallel we visit the list of modified extent maps and construct their matching file extent items and write them to the log btree. After that, in
btrfs_sync_log()we wait for all the ordered operations to finish (viabtrfs_wait_logged_extents).The problem with this is that we were completely ignoring errors that can happen in the extent write path, such as
-ENOSPC, a temporary-ENOMEMor-EIOerrors for example. When such error happens, it means we have parts of the on disk extent that weren't written to, and so we end up logging file extent items that point to these extents that contain garbage/random data - so after a crash/reboot plus log replay, we get our inode's metadata pointing to those extents.This worked in contrast with the full (non-fast) fsync path, where we start all ordered operations, wait for them to finish and then write to the log btree. In this path, after each ordered operation completes we check if it's flagged with an error (
BTRFS_ORDERED_IOERR) and return-EIOif so (viabtrfs_wait_ordered_range).
Quoted 5 lines
So if an error happens with any ordered operation, just return a
-EIOerror to userspace, so that it knows that not all of its previous writes were durably persisted and the application can take proper action (like redo the writes for e.g.) - and definitely not leave any file extent items in the log refer to non fully written extents.
Thanks Filipe,
I'm pushing this one off for the merge window. It's not that I think it's wrong, but we're getting late in the rcs and I want to test it more.
-chris