* [PATCH v2 0/2] btrfs: don't lose fsynced data to a transaction abort @ 2026-12-03 23:56 Leo Martins 2026-12-03 23:56 ` [PATCH v2 1/2] btrfs: fix loss of fsynced data when ordered extent completion fails Leo Martins 2026-12-03 23:56 ` [PATCH v2 2/2] btrfs: refuse NOCOW writes after a filesystem error Leo Martins 0 siblings, 2 replies; 5+ messages in thread From: Leo Martins @ 2026-12-03 23:56 UTC (permalink / raw) To: linux-btrfs, kernel-team Cc: David Sterba, Chris Mason, Filipe Manana, Josef Bacik Fast fsync can sync a file extent item and its checksums to the tree log while the ordered extent is still waiting to update the subvolume tree. If the transaction aborts in between, replay can restore a file mapping whose blocks were overwritten after fsync returned. If an ordered extent that fast fsync logged fails to complete, patch 1 preserves the log for replay, and for a COW write keeps the new blocks out of the free space cache. Patch 2 refuses NOCOW writes after a filesystem error, so they can't overwrite logged data in place. That covers a preallocated range the failed ordered extent left unwritten, and an extent shared by a reflink that the cross-ref check can no longer see after an abort. The series is based on for-next, which has Josef's "btrfs: data integrity fixes" v3 [1]. Patch 1 describes the fast fsync prerequisites. Changes in v2: - Also handle failed writes into prealloc extents (Sashiko, via Filipe) and NOCOW writes in patch 1. - Check IOERR before marking an extent logged if ordered extent completion races fsync. - New patch 2. - Fix the discard comment (Filipe). v1: https://lore.kernel.org/linux-btrfs/54da67463c57f41302f8456a8a7a1190dcb65ae2.1794266796.git.loemra.dev@gmail.com/ [1] https://lore.kernel.org/linux-btrfs/20261120-b4-btrfs-data-integrity-v3-0-4de4b91b826e@toxicpanda.com/ Leo Martins (2): btrfs: fix loss of fsynced data when ordered extent completion fails btrfs: refuse NOCOW writes after a filesystem error fs/btrfs/inode.c | 100 +++++++++++++++++++++++++++++++++++++------- fs/btrfs/tree-log.c | 26 ++++++++---- 2 files changed, 104 insertions(+), 22 deletions(-) base-commit: 418f7723b545cf064fc584529710b28d1347188e -- 2.53.1-Meta ^ permalink raw reply [flat|nested] 5+ messages in thread
* [PATCH v2 1/2] btrfs: fix loss of fsynced data when ordered extent completion fails 2026-12-03 23:56 [PATCH v2 0/2] btrfs: don't lose fsynced data to a transaction abort Leo Martins @ 2026-12-03 23:56 ` Leo Martins 2026-12-04 11:50 ` Filipe Manana 2026-12-03 23:56 ` [PATCH v2 2/2] btrfs: refuse NOCOW writes after a filesystem error Leo Martins 1 sibling, 1 reply; 5+ messages in thread From: Leo Martins @ 2026-12-03 23:56 UTC (permalink / raw) To: linux-btrfs, kernel-team Cc: David Sterba, Chris Mason, Filipe Manana, Josef Bacik, stable A fast fsync waits for the data I/O of its ordered extents, but not for them to complete. It logs the new extents from their extent maps, with the checksums attached to the ordered extents, and syncs the log. For a COW write, the file extent item and the checksums are inserted only when btrfs_finish_one_ordered() joins a transaction to complete the ordered extent. Until then, the synced log is the only on-disk record that maps the file range to the new extent. If the transaction aborts after the log is synced but before btrfs_finish_one_ordered() joins it, the join fails and the error path returns the new extent, X, to the free space cache while the synced log still points to it. Writeback keeps running after an abort, so writeback of another dirty page can allocate X and overwrite it: fast fsync ordered extent completion writeback wait for I/O to X log file range -> X sync log, return 0 ------------------- transaction aborts ------------------- join fails return X to free space allocate X, write X At the next mount, log replay points the file range at X again, with the logged checksums, and reads of the fsynced range fail checksum verification. Fix this in btrfs_finish_one_ordered(). If a COW ordered extent that a fast fsync logged (BTRFS_ORDERED_LOGGED) fails, release its space reservation, but keep X out of the free space cache and skip the synchronous discard, as clean_log_buffer() does for a log tree block when it has no transaction. Log replay allocates X again at the next mount. Until then X is neither free nor reserved, which is fine because the filesystem has an error by then, as described below. The transaction that logged X must not commit either, because its commit drops the log. It can't commit before the ordered extent completes, since the fsync registered the ordered extent as pending and the commit waits for it. Any failure after joining the transaction aborts it. If the filesystem has no error yet, for example because allocating the transaction handle failed, call btrfs_handle_fs_error() before btrfs_remove_ordered_extent() wakes the commit. With the filesystem in error, the commit fails to write its tree blocks and never writes a superblock, so the superblock written by the log sync keeps pointing at the log. A logged PREALLOC or NOCOW write needs the error too, although its blocks are never freed. Until the ordered extent completes, only the log records that a PREALLOC range was written, and only the log has the inode update of any write: the new i_size of a write that extended the file, and the new mtime and ctime. Ordered extent completion can also fail while fsync is still logging. If the error path checks LOGGED before fsync sets it, it frees X although the log tree already points to it. To close that window, have btrfs_log_changed_extents() check BTRFS_ORDERED_IOERR and set LOGGED under ordered_tree_lock. The error path sets IOERR before it checks LOGGED under the same lock, so either it sees LOGGED and keeps X, or fsync sees IOERR. In that case btrfs_log_changed_extents() returns -EIO, which makes btrfs_log_inode_parent() mark the transaction for a full commit, so the log is never synced and fsync returns the error. The failed ordered extent keeps LOGGED clear, so its error path doesn't force the filesystem read-only. A failed NOCOW write relies on this: a fast fsync doesn't wait for it to complete, so without the check a plain write error would force the filesystem read-only. Ordered extents that fsync logged before the failed one keep LOGGED, so if one of them fails as well, the filesystem still goes read-only, although the log is never synced. This relies on "btrfs: make fsync wait for all incomplete COW writes" and "btrfs: wait for ordered extent IO in fast fsync". With them, a fast fsync waits for BTRFS_ORDERED_IO_DONE, which is set after IOERR if the write failed, and when a write that isn't NOCOW fails, it waits for the ordered extent to complete instead of logging it. So a plain write error can't reach the new error path with LOGGED set. Stable backports need both commits, which go back to 6.10. Fixes: 487781796d30 ("btrfs: make fast fsyncs wait only for writeback") CC: stable@vger.kernel.org # 6.10+ Signed-off-by: Leo Martins <loemra.dev@gmail.com> --- fs/btrfs/inode.c | 75 ++++++++++++++++++++++++++++++++++++--------- fs/btrfs/tree-log.c | 26 +++++++++++----- 2 files changed, 79 insertions(+), 22 deletions(-) diff --git a/fs/btrfs/inode.c b/fs/btrfs/inode.c index 402bbba0b11a..c31b96082686 100644 --- a/fs/btrfs/inode.c +++ b/fs/btrfs/inode.c @@ -3173,6 +3173,23 @@ static int insert_ordered_extent_file_extent(struct btrfs_trans_handle *trans, update_inode_bytes, oe->qgroup_rsv); } +/* + * A synced log may point to this extent. Release its reservation, but keep it + * out of the free space cache, so it can't be reused before log replay at the + * next mount allocates it again. The caller has made sure the filesystem has an + * error, so no transaction commit can write a superblock that drops the log. + */ +static void keep_logged_extent(struct btrfs_ordered_extent *oe) +{ + struct btrfs_fs_info *fs_info = oe->inode->root->fs_info; + struct btrfs_block_group *bg; + + bg = btrfs_lookup_block_group(fs_info, oe->disk_bytenr); + ASSERT(bg); + btrfs_free_reserved_bytes(bg, oe->disk_num_bytes, true); + btrfs_put_block_group(bg); +} + /* * As ordered data IO finishes, this gets called so we can finish * an ordered extent if the range of bytes in the file it covers are @@ -3192,6 +3209,7 @@ int btrfs_finish_one_ordered(struct btrfs_ordered_extent *ordered_extent) u64 logical_len = ordered_extent->num_bytes; u64 unwritten_start; bool truncated = false; + bool logged = false; bool clear_reserved_extent = true; unsigned int clear_bits = 0; @@ -3352,10 +3370,32 @@ int btrfs_finish_one_ordered(struct btrfs_ordered_extent *ordered_extent) if (ret) btrfs_mark_ordered_extent_error(ordered_extent); + /* + * A fast fsync may have logged this ordered extent and synced + * the log, which is then the only record of what the ordered + * extent should have added to the subvolume tree: the extent of + * a COW write, the written state of a PREALLOC range, and the + * inode update of any write, such as a new i_size or mtime. A + * transaction commit drops the log, so if the filesystem has no + * error yet, set one, which keeps any commit from writing a + * superblock. + * + * IOERR is set by now. Testing LOGGED under ordered_tree_lock + * pairs with the end of btrfs_log_changed_extents(): either + * fsync sees IOERR and never syncs its log, or we see LOGGED. + */ + if (ret) { + spin_lock(&inode->ordered_tree_lock); + logged = test_bit(BTRFS_ORDERED_LOGGED, &ordered_extent->flags); + spin_unlock(&inode->ordered_tree_lock); + if (logged && !BTRFS_FS_ERROR(fs_info)) + btrfs_handle_fs_error(fs_info, ret, NULL); + } + /* * Drop extent maps for the part of the extent we didn't write. - * On error the written part is freed below as well, so drop - * its maps too. + * On error the written part doesn't make it into the subvolume + * tree either, so drop its maps too. */ unwritten_start = start; if (truncated && !ret) @@ -3371,23 +3411,30 @@ int btrfs_finish_one_ordered(struct btrfs_ordered_extent *ordered_extent) * If we made it past insert_reserved_file_extent before we * errored out then we don't need to do this as the accounting * has already been done. + * + * If a fast fsync logged the ordered extent, keep the extent out + * of the free space cache instead, see keep_logged_extent(). */ if ((ret || !logical_len) && clear_reserved_extent && !test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags) && !test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags)) { - /* - * Discard the range before returning it back to the - * free space pool - */ - if (ret && btrfs_test_opt(fs_info, DISCARD_SYNC)) - btrfs_discard_extent(fs_info, - ordered_extent->disk_bytenr, - ordered_extent->disk_num_bytes, - NULL, true); - btrfs_free_reserved_extent(fs_info, - ordered_extent->disk_bytenr, - ordered_extent->disk_num_bytes, true); + const u64 disk_bytenr = ordered_extent->disk_bytenr; + const u64 disk_num_bytes = ordered_extent->disk_num_bytes; + + if (logged) { + keep_logged_extent(ordered_extent); + } else { + /* + * Discard the range before returning it to the + * free space pool. + */ + if (ret && btrfs_test_opt(fs_info, DISCARD_SYNC)) + btrfs_discard_extent(fs_info, disk_bytenr, + disk_num_bytes, NULL, true); + btrfs_free_reserved_extent(fs_info, disk_bytenr, + disk_num_bytes, true); + } /* * Actually free the qgroup rsv which was released when * the ordered extent was created. diff --git a/fs/btrfs/tree-log.c b/fs/btrfs/tree-log.c index 2a014087dd83..1a4367ce8202 100644 --- a/fs/btrfs/tree-log.c +++ b/fs/btrfs/tree-log.c @@ -5456,19 +5456,29 @@ static int btrfs_log_changed_extents(struct btrfs_trans_handle *trans, * before it commits and wipes out the log trees, otherwise we would * lose data if an ordered extents completes after the transaction * commits and a power failure happens after the transaction commit. + * + * btrfs_finish_one_ordered() sets IOERR and then tests LOGGED under the + * same lock. If we see IOERR, its completion may already have found + * LOGGED clear and freed an extent we just logged, so this log must + * never be synced: return an error, which makes + * btrfs_log_inode_parent() mark the transaction for a full commit. + * Leaving LOGGED clear also keeps a failed NOCOW write, whose + * completion a fast fsync doesn't wait for, from forcing the + * filesystem read-only. */ list_for_each_entry_safe(ordered, tmp, &ctx->ordered_extents, log_list) { - list_del_init(&ordered->log_list); + spin_lock(&inode->ordered_tree_lock); + if (test_bit(BTRFS_ORDERED_IOERR, &ordered->flags)) { + spin_unlock(&inode->ordered_tree_lock); + return -EIO; + } set_bit(BTRFS_ORDERED_LOGGED, &ordered->flags); - if (!test_bit(BTRFS_ORDERED_COMPLETE, &ordered->flags)) { - spin_lock(&inode->ordered_tree_lock); - if (!test_bit(BTRFS_ORDERED_COMPLETE, &ordered->flags)) { - set_bit(BTRFS_ORDERED_PENDING, &ordered->flags); - atomic_inc(&trans->transaction->pending_ordered); - } - spin_unlock(&inode->ordered_tree_lock); + set_bit(BTRFS_ORDERED_PENDING, &ordered->flags); + atomic_inc(&trans->transaction->pending_ordered); } + spin_unlock(&inode->ordered_tree_lock); + list_del_init(&ordered->log_list); btrfs_put_ordered_extent(ordered); } -- 2.53.1-Meta ^ permalink raw reply related [flat|nested] 5+ messages in thread
* Re: [PATCH v2 1/2] btrfs: fix loss of fsynced data when ordered extent completion fails 2026-12-03 23:56 ` [PATCH v2 1/2] btrfs: fix loss of fsynced data when ordered extent completion fails Leo Martins @ 2026-12-04 11:50 ` Filipe Manana 0 siblings, 0 replies; 5+ messages in thread From: Filipe Manana @ 2026-12-04 11:50 UTC (permalink / raw) To: Leo Martins Cc: linux-btrfs, kernel-team, David Sterba, Chris Mason, Josef Bacik, stable On Thu, Dec 3, 2026 at 11:57 PM Leo Martins <loemra.dev@gmail.com> wrote: > > A fast fsync waits for the data I/O of its ordered extents, but not for > them to complete. It logs the new extents from their extent maps, with > the checksums attached to the ordered extents, and syncs the log. For a > COW write, the file extent item and the checksums are inserted only when > btrfs_finish_one_ordered() joins a transaction to complete the ordered > extent. Until then, the synced log is the only on-disk record that maps > the file range to the new extent. > > If the transaction aborts after the log is synced but before > btrfs_finish_one_ordered() joins it, the join fails and the error path > returns the new extent, X, to the free space cache while the synced log > still points to it. Writeback keeps running after an abort, so writeback > of another dirty page can allocate X and overwrite it: > > fast fsync ordered extent completion writeback > > wait for I/O to X > log file range -> X > sync log, return 0 > > ------------------- transaction aborts ------------------- > > join fails > return X to free space > allocate X, > write X > > At the next mount, log replay points the file range at X again, with the > logged checksums, and reads of the fsynced range fail checksum > verification. > > Fix this in btrfs_finish_one_ordered(). If a COW ordered extent that a > fast fsync logged (BTRFS_ORDERED_LOGGED) fails, release its space > reservation, but keep X out of the free space cache and skip the > synchronous discard, as clean_log_buffer() does for a log tree block > when it has no transaction. Log replay allocates X again at the next > mount. Until then X is neither free nor reserved, which is fine because > the filesystem has an error by then, as described below. > > The transaction that logged X must not commit either, because its commit > drops the log. It can't commit before the ordered extent completes, > since the fsync registered the ordered extent as pending and the commit > waits for it. Any failure after joining the transaction aborts it. If > the filesystem has no error yet, for example because allocating the > transaction handle failed, call btrfs_handle_fs_error() before > btrfs_remove_ordered_extent() wakes the commit. With the filesystem in > error, the commit fails to write its tree blocks and never writes a > superblock, so the superblock written by the log sync keeps pointing at > the log. > > A logged PREALLOC or NOCOW write needs the error too, although its > blocks are never freed. Until the ordered extent completes, only the log > records that a PREALLOC range was written, and only the log has the > inode update of any write: the new i_size of a write that extended the > file, and the new mtime and ctime. > > Ordered extent completion can also fail while fsync is still logging. If > the error path checks LOGGED before fsync sets it, it frees X although > the log tree already points to it. To close that window, have > btrfs_log_changed_extents() check BTRFS_ORDERED_IOERR and set LOGGED > under ordered_tree_lock. The error path sets IOERR before it checks > LOGGED under the same lock, so either it sees LOGGED and keeps X, or > fsync sees IOERR. In that case btrfs_log_changed_extents() returns -EIO, > which makes btrfs_log_inode_parent() mark the transaction for a full > commit, so the log is never synced and fsync returns the error. > > The failed ordered extent keeps LOGGED clear, so its error path doesn't > force the filesystem read-only. A failed NOCOW write relies on this: a > fast fsync doesn't wait for it to complete, so without the check a plain > write error would force the filesystem read-only. Ordered extents that > fsync logged before the failed one keep LOGGED, so if one of them fails > as well, the filesystem still goes read-only, although the log is never > synced. > > This relies on "btrfs: make fsync wait for all incomplete COW writes" > and "btrfs: wait for ordered extent IO in fast fsync". With them, a fast > fsync waits for BTRFS_ORDERED_IO_DONE, which is set after IOERR if the > write failed, and when a write that isn't NOCOW fails, it waits for the > ordered extent to complete instead of logging it. So a plain write error > can't reach the new error path with LOGGED set. Stable backports need > both commits, which go back to 6.10. > > Fixes: 487781796d30 ("btrfs: make fast fsyncs wait only for writeback") > CC: stable@vger.kernel.org # 6.10+ > Signed-off-by: Leo Martins <loemra.dev@gmail.com> > --- > fs/btrfs/inode.c | 75 ++++++++++++++++++++++++++++++++++++--------- > fs/btrfs/tree-log.c | 26 +++++++++++----- > 2 files changed, 79 insertions(+), 22 deletions(-) > > diff --git a/fs/btrfs/inode.c b/fs/btrfs/inode.c > index 402bbba0b11a..c31b96082686 100644 > --- a/fs/btrfs/inode.c > +++ b/fs/btrfs/inode.c > @@ -3173,6 +3173,23 @@ static int insert_ordered_extent_file_extent(struct btrfs_trans_handle *trans, > update_inode_bytes, oe->qgroup_rsv); > } > > +/* > + * A synced log may point to this extent. Release its reservation, but keep it > + * out of the free space cache, so it can't be reused before log replay at the > + * next mount allocates it again. The caller has made sure the filesystem has an > + * error, so no transaction commit can write a superblock that drops the log. > + */ > +static void keep_logged_extent(struct btrfs_ordered_extent *oe) > +{ > + struct btrfs_fs_info *fs_info = oe->inode->root->fs_info; > + struct btrfs_block_group *bg; > + > + bg = btrfs_lookup_block_group(fs_info, oe->disk_bytenr); > + ASSERT(bg); > + btrfs_free_reserved_bytes(bg, oe->disk_num_bytes, true); > + btrfs_put_block_group(bg); > +} > + > /* > * As ordered data IO finishes, this gets called so we can finish > * an ordered extent if the range of bytes in the file it covers are > @@ -3192,6 +3209,7 @@ int btrfs_finish_one_ordered(struct btrfs_ordered_extent *ordered_extent) > u64 logical_len = ordered_extent->num_bytes; > u64 unwritten_start; > bool truncated = false; > + bool logged = false; > bool clear_reserved_extent = true; > unsigned int clear_bits = 0; > > @@ -3352,10 +3370,32 @@ int btrfs_finish_one_ordered(struct btrfs_ordered_extent *ordered_extent) > if (ret) > btrfs_mark_ordered_extent_error(ordered_extent); > > + /* > + * A fast fsync may have logged this ordered extent and synced > + * the log, which is then the only record of what the ordered > + * extent should have added to the subvolume tree: the extent of > + * a COW write, the written state of a PREALLOC range, and the > + * inode update of any write, such as a new i_size or mtime. A > + * transaction commit drops the log, so if the filesystem has no > + * error yet, set one, which keeps any commit from writing a > + * superblock. > + * > + * IOERR is set by now. Testing LOGGED under ordered_tree_lock > + * pairs with the end of btrfs_log_changed_extents(): either > + * fsync sees IOERR and never syncs its log, or we see LOGGED. > + */ > + if (ret) { > + spin_lock(&inode->ordered_tree_lock); > + logged = test_bit(BTRFS_ORDERED_LOGGED, &ordered_extent->flags); > + spin_unlock(&inode->ordered_tree_lock); > + if (logged && !BTRFS_FS_ERROR(fs_info)) > + btrfs_handle_fs_error(fs_info, ret, NULL); > + } > + > /* > * Drop extent maps for the part of the extent we didn't write. > - * On error the written part is freed below as well, so drop > - * its maps too. > + * On error the written part doesn't make it into the subvolume > + * tree either, so drop its maps too. > */ > unwritten_start = start; > if (truncated && !ret) > @@ -3371,23 +3411,30 @@ int btrfs_finish_one_ordered(struct btrfs_ordered_extent *ordered_extent) > * If we made it past insert_reserved_file_extent before we > * errored out then we don't need to do this as the accounting > * has already been done. > + * > + * If a fast fsync logged the ordered extent, keep the extent out > + * of the free space cache instead, see keep_logged_extent(). > */ > if ((ret || !logical_len) && > clear_reserved_extent && > !test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags) && > !test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags)) { > - /* > - * Discard the range before returning it back to the > - * free space pool > - */ > - if (ret && btrfs_test_opt(fs_info, DISCARD_SYNC)) > - btrfs_discard_extent(fs_info, > - ordered_extent->disk_bytenr, > - ordered_extent->disk_num_bytes, > - NULL, true); > - btrfs_free_reserved_extent(fs_info, > - ordered_extent->disk_bytenr, > - ordered_extent->disk_num_bytes, true); > + const u64 disk_bytenr = ordered_extent->disk_bytenr; > + const u64 disk_num_bytes = ordered_extent->disk_num_bytes; These are declared here but only the else statement below uses them. We have other places in the function that use those fields, so we could declare these at the top of the function and change those other places to use the variables. But that would make the diff larger and maybe harder to backport. > + > + if (logged) { > + keep_logged_extent(ordered_extent); > + } else { > + /* > + * Discard the range before returning it to the > + * free space pool. > + */ > + if (ret && btrfs_test_opt(fs_info, DISCARD_SYNC)) > + btrfs_discard_extent(fs_info, disk_bytenr, > + disk_num_bytes, NULL, true); > + btrfs_free_reserved_extent(fs_info, disk_bytenr, > + disk_num_bytes, true); > + } > /* > * Actually free the qgroup rsv which was released when > * the ordered extent was created. > diff --git a/fs/btrfs/tree-log.c b/fs/btrfs/tree-log.c > index 2a014087dd83..1a4367ce8202 100644 > --- a/fs/btrfs/tree-log.c > +++ b/fs/btrfs/tree-log.c > @@ -5456,19 +5456,29 @@ static int btrfs_log_changed_extents(struct btrfs_trans_handle *trans, > * before it commits and wipes out the log trees, otherwise we would > * lose data if an ordered extents completes after the transaction > * commits and a power failure happens after the transaction commit. > + * > + * btrfs_finish_one_ordered() sets IOERR and then tests LOGGED under the > + * same lock. If we see IOERR, its completion may already have found > + * LOGGED clear and freed an extent we just logged, so this log must > + * never be synced: return an error, which makes > + * btrfs_log_inode_parent() mark the transaction for a full commit. > + * Leaving LOGGED clear also keeps a failed NOCOW write, whose > + * completion a fast fsync doesn't wait for, from forcing the > + * filesystem read-only. > */ > list_for_each_entry_safe(ordered, tmp, &ctx->ordered_extents, log_list) { > - list_del_init(&ordered->log_list); > + spin_lock(&inode->ordered_tree_lock); > + if (test_bit(BTRFS_ORDERED_IOERR, &ordered->flags)) { > + spin_unlock(&inode->ordered_tree_lock); > + return -EIO; > + } > set_bit(BTRFS_ORDERED_LOGGED, &ordered->flags); > - > if (!test_bit(BTRFS_ORDERED_COMPLETE, &ordered->flags)) { > - spin_lock(&inode->ordered_tree_lock); > - if (!test_bit(BTRFS_ORDERED_COMPLETE, &ordered->flags)) { > - set_bit(BTRFS_ORDERED_PENDING, &ordered->flags); > - atomic_inc(&trans->transaction->pending_ordered); > - } > - spin_unlock(&inode->ordered_tree_lock); > + set_bit(BTRFS_ORDERED_PENDING, &ordered->flags); > + atomic_inc(&trans->transaction->pending_ordered); > } > + spin_unlock(&inode->ordered_tree_lock); > + list_del_init(&ordered->log_list); So there's one problem with this that Sashiko reported, and it's valid: https://sashiko.dev/#/patchset/cover.1796341585.git.loemra.dev%40gmail.com Thanks. > btrfs_put_ordered_extent(ordered); > } > > -- > 2.53.1-Meta > > ^ permalink raw reply [flat|nested] 5+ messages in thread
* [PATCH v2 2/2] btrfs: refuse NOCOW writes after a filesystem error 2026-12-03 23:56 [PATCH v2 0/2] btrfs: don't lose fsynced data to a transaction abort Leo Martins 2026-12-03 23:56 ` [PATCH v2 1/2] btrfs: fix loss of fsynced data when ordered extent completion fails Leo Martins @ 2026-12-03 23:56 ` Leo Martins 2026-12-04 11:51 ` Filipe Manana 1 sibling, 1 reply; 5+ messages in thread From: Leo Martins @ 2026-12-03 23:56 UTC (permalink / raw) To: linux-btrfs, kernel-team Cc: David Sterba, Chris Mason, Filipe Manana, Josef Bacik, stable The first write into a preallocated extent writes its blocks in place, with an ordered extent of type BTRFS_ORDERED_PREALLOC. When its data I/O finishes, btrfs_finish_one_ordered() still has to mark the range written in the subvolume tree and insert the checksums. A fast fsync can log the range as written, with the checksums attached to the ordered extent, before that. If the transaction aborts at that point, the ordered extent fails and the subvolume tree still has the range as a prealloc extent, with no checksums in the csum tree. The blocks still belong to the file, so keeping them out of the free space cache, which protects a logged COW write, doesn't help. A later write to the same range waits for the failed ordered extent to complete. Then its writeback, or the write itself for direct IO, finds the prealloc extent without checksums, passes the NOCOW checks and overwrites the logged data in place: fast fsync ordered extent completion later write wait for I/O to X log X as written, with its checksums sync log, return 0 ------------------- transaction aborts ------------------- join fails, X stays prealloc NOCOW write into X At the next mount, log replay marks X as written with the logged checksums, which no longer match its data, and reads of the fsynced range fail checksum verification. Make can_nocow_file_extent() return -EROFS once the filesystem has an error. Writeback then fails the range, as all writeback does after a shutdown, and direct IO falls back to COW, so the logged blocks stay intact for replay. The later write waits for the failed ordered extent to complete, and by then the filesystem has an error: either the transaction aborted, or the ordered extent set the error itself because a fast fsync logged it. Refuse NOCOW for every extent type, not only prealloc extents. After an abort, the cleanup destroys the aborted transaction's delayed refs, so btrfs_cross_ref_exist() can miss a reference that only existed as a delayed ref. If a range of a NODATACOW file was cloned in that transaction and the clone was fsynced, the log references the shared extent, and a NOCOW write to the source would change the data that log replay gives the clone, with no checksums to catch it. The cleanup runs in another task, and it destroys the delayed refs only after the error is set, under locks that btrfs_cross_ref_exist() also takes. So test the error after the lookup: if the lookup missed a delayed ref that the cleanup destroyed, the test sees the error. A test before the lookup could pass just before the cleanup runs. After an error, ordered extents can't join a transaction and fsync fails, so data written back then can't become durable. Writeback fails the range rather than falling back to COW, which would only allocate and write new extents for data that is dropped anyway. No write that was reported as durable is affected. This depends on "btrfs: fix loss of fsynced data when ordered extent completion fails". Fixes: 487781796d30 ("btrfs: make fast fsyncs wait only for writeback") CC: stable@vger.kernel.org # 6.10+ Signed-off-by: Leo Martins <loemra.dev@gmail.com> --- fs/btrfs/inode.c | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/fs/btrfs/inode.c b/fs/btrfs/inode.c index c31b96082686..e6e76e3e4171 100644 --- a/fs/btrfs/inode.c +++ b/fs/btrfs/inode.c @@ -1841,6 +1841,31 @@ static int can_nocow_file_extent(struct btrfs_path *path, if (ret != 0) goto out; + /* + * After a filesystem error, a synced log can reference this extent in + * a way the trees in memory don't show, and a NOCOW write would change + * the data that log replay restores. A failed PREALLOC ordered extent + * that a fast fsync logged leaves the range PREALLOC in memory. If a + * clone (reflink) shared this extent with another file that was then + * fsynced, the cleanup of the aborted transaction drops that file's + * delayed ref. + * + * The failed ordered extent makes sure the error is set before it + * completes, and later writes to its range wait for it to complete. + * The cleanup runs in another task and sets the error, or has seen it + * set, before it drops the delayed refs under locks that + * btrfs_cross_ref_exist() also takes. So test the error after that + * lookup, not before: if the lookup missed a dropped ref, the error is + * visible here. + * + * Return an error rather than 0: nothing written now can be committed, + * so writeback fails the range instead of allocating new extents. + */ + if (unlikely(BTRFS_FS_ERROR(root->fs_info))) { + ret = -EROFS; + goto out; + } + if (args->free_path) { /* * We don't need the path anymore, plus through the -- 2.53.1-Meta ^ permalink raw reply related [flat|nested] 5+ messages in thread
* Re: [PATCH v2 2/2] btrfs: refuse NOCOW writes after a filesystem error 2026-12-03 23:56 ` [PATCH v2 2/2] btrfs: refuse NOCOW writes after a filesystem error Leo Martins @ 2026-12-04 11:51 ` Filipe Manana 0 siblings, 0 replies; 5+ messages in thread From: Filipe Manana @ 2026-12-04 11:51 UTC (permalink / raw) To: Leo Martins Cc: linux-btrfs, kernel-team, David Sterba, Chris Mason, Josef Bacik, stable On Thu, Dec 3, 2026 at 11:57 PM Leo Martins <loemra.dev@gmail.com> wrote: > > The first write into a preallocated extent writes its blocks in place, > with an ordered extent of type BTRFS_ORDERED_PREALLOC. When its data I/O > finishes, btrfs_finish_one_ordered() still has to mark the range written > in the subvolume tree and insert the checksums. A fast fsync can log the > range as written, with the checksums attached to the ordered extent, > before that. > > If the transaction aborts at that point, the ordered extent fails and > the subvolume tree still has the range as a prealloc extent, with no > checksums in the csum tree. The blocks still belong to the file, so > keeping them out of the free space cache, which protects a logged COW > write, doesn't help. A later write to the same range waits for the > failed ordered extent to complete. Then its writeback, or the write > itself for direct IO, finds the prealloc extent without checksums, > passes the NOCOW checks and overwrites the logged data in place: > > fast fsync ordered extent completion later write > > wait for I/O to X > log X as written, > with its checksums > sync log, return 0 > > ------------------- transaction aborts ------------------- > > join fails, > X stays prealloc > NOCOW write > into X > > At the next mount, log replay marks X as written with the logged > checksums, which no longer match its data, and reads of the fsynced > range fail checksum verification. > > Make can_nocow_file_extent() return -EROFS once the filesystem has an > error. Writeback then fails the range, as all writeback does after a > shutdown, and direct IO falls back to COW, so the logged blocks stay > intact for replay. The later write waits for the failed ordered extent > to complete, and by then the filesystem has an error: either the > transaction aborted, or the ordered extent set the error itself because > a fast fsync logged it. > > Refuse NOCOW for every extent type, not only prealloc extents. After an > abort, the cleanup destroys the aborted transaction's delayed refs, so > btrfs_cross_ref_exist() can miss a reference that only existed as a > delayed ref. If a range of a NODATACOW file was cloned in that > transaction and the clone was fsynced, the log references the shared > extent, and a NOCOW write to the source would change the data that log > replay gives the clone, with no checksums to catch it. The cleanup runs > in another task, and it destroys the delayed refs only after the error > is set, under locks that btrfs_cross_ref_exist() also takes. So test the > error after the lookup: if the lookup missed a delayed ref that the > cleanup destroyed, the test sees the error. A test before the lookup > could pass just before the cleanup runs. > > After an error, ordered extents can't join a transaction and fsync > fails, so data written back then can't become durable. Writeback fails > the range rather than falling back to COW, which would only allocate > and write new extents for data that is dropped anyway. No write that > was reported as durable is affected. > > This depends on "btrfs: fix loss of fsynced data when ordered extent > completion fails". > > Fixes: 487781796d30 ("btrfs: make fast fsyncs wait only for writeback") > CC: stable@vger.kernel.org # 6.10+ > Signed-off-by: Leo Martins <loemra.dev@gmail.com> Reviewed-by: Filipe Manana <fdmanana@suse.com> Thanks. > --- > fs/btrfs/inode.c | 25 +++++++++++++++++++++++++ > 1 file changed, 25 insertions(+) > > diff --git a/fs/btrfs/inode.c b/fs/btrfs/inode.c > index c31b96082686..e6e76e3e4171 100644 > --- a/fs/btrfs/inode.c > +++ b/fs/btrfs/inode.c > @@ -1841,6 +1841,31 @@ static int can_nocow_file_extent(struct btrfs_path *path, > if (ret != 0) > goto out; > > + /* > + * After a filesystem error, a synced log can reference this extent in > + * a way the trees in memory don't show, and a NOCOW write would change > + * the data that log replay restores. A failed PREALLOC ordered extent > + * that a fast fsync logged leaves the range PREALLOC in memory. If a > + * clone (reflink) shared this extent with another file that was then > + * fsynced, the cleanup of the aborted transaction drops that file's > + * delayed ref. > + * > + * The failed ordered extent makes sure the error is set before it > + * completes, and later writes to its range wait for it to complete. > + * The cleanup runs in another task and sets the error, or has seen it > + * set, before it drops the delayed refs under locks that > + * btrfs_cross_ref_exist() also takes. So test the error after that > + * lookup, not before: if the lookup missed a dropped ref, the error is > + * visible here. > + * > + * Return an error rather than 0: nothing written now can be committed, > + * so writeback fails the range instead of allocating new extents. > + */ > + if (unlikely(BTRFS_FS_ERROR(root->fs_info))) { > + ret = -EROFS; > + goto out; > + } > + > if (args->free_path) { > /* > * We don't need the path anymore, plus through the > -- > 2.53.1-Meta > > ^ permalink raw reply [flat|nested] 5+ messages in thread
end of thread, other threads:[~2026-12-04 11:52 UTC | newest] Thread overview: 5+ messages (download: mbox.gz follow: Atom feed -- links below jump to the message on this page -- 2026-12-03 23:56 [PATCH v2 0/2] btrfs: don't lose fsynced data to a transaction abort Leo Martins 2026-12-03 23:56 ` [PATCH v2 1/2] btrfs: fix loss of fsynced data when ordered extent completion fails Leo Martins 2026-12-04 11:50 ` Filipe Manana 2026-12-03 23:56 ` [PATCH v2 2/2] btrfs: refuse NOCOW writes after a filesystem error Leo Martins 2026-12-04 11:51 ` Filipe Manana
This is a public inbox, see mirroring instructions for how to clone and mirror all data and code used for this inbox