* [PATCH] btrfs: fix fsync returning before the log is committed
@ 2026-12-02 16:02 Miroslav Cimerman
0 siblings, 0 replies; only message in thread
From: Miroslav Cimerman @ 2026-12-02 16:02 UTC (permalink / raw)
To: linux-btrfs; +Cc: David Sterba, Chris Mason
btrfs_inode_in_log() lets fsync return 0 without syncing the log if the
inode was logged after its last change and that log is committed:
last_sub_trans <= inode->last_log_commit
last_sub_trans <= root->last_log_commit
However, last_sub_trans is the log transaction of the inode's last
change, not the one the inode was logged into. The two differ when an
fsync of another inode commits the log in between. The second check
then passes while the log transaction that holds the inode is still
being committed:
inode F changed in log transaction 0
fsync of another inode commits log 0, root->last_log_commit = 0
fsync A of F fsync B of F
btrfs_log_inode()
log F into log transaction 1
inode->last_log_commit = 0
btrfs_inode_unlock()
btrfs_sync_log()
btrfs_write_marked_extents()
btrfs_sync_file()
skip_inode_logging()
btrfs_inode_in_log() == true
return 0
write_all_supers()
root->last_log_commit = 1
The same happens inside log transaction 0 without another fsync.
btrfs_add_log_tree() sets root->last_log_commit to 0 before log
transaction 0 is committed, so an inode changed and logged in it passes
both checks as soon as it is logged.
A power failure before fsync A's log commit completes loses the data
that fsync B reported as durable.
Start root->last_log_commit at -1, as log_transid_committed already
does, and when btrfs_log_inode() logs an inode, set last_sub_trans to
the log transaction it was logged into. A later change is still caught.
btrfs_set_inode_last_trans() lowers last_log_commit when the inode item
is updated, and a write that is not complete yet is in the ordered
extent list of the next fsync. With the -1 start, root->last_log_commit
is always below the log transaction being committed, so the ASSERT in
btrfs_sync_log() can use <.
Fixes: 257c62e1bce0 ("Btrfs: avoid tree log commit when there are no changes")
CC: stable@vger.kernel.org
Signed-off-by: Miroslav Cimerman <mc@raidlab.org>
---
Found with FuzzyRAID, a fault-injection tester for RAID.
Reproduced on for-next with a test-only delay at the start of
btrfs_sync_log(): after a fresh mount, a direct write, fsync A of the
file (delayed), then fsync B of the same file, then a power failure
right after fsync B returned (dm-flakey dropping writes). Also with an
fsync of another file between the write and fsync A. The write is lost
on stock and kept with the fix.
Backport note: the btrfs_log_inode() hunk needs the logging_new_name
check from bfe3d755ef7c ("btrfs: do not update last_log_commit when
logging inode due to a new name"). Without it, inodes logged under
btrfs_log_new_name() get ctx->log_transid 0.
This patch was prepared with help from an LLM.
fs/btrfs/btrfs_inode.h | 2 +-
fs/btrfs/disk-io.c | 2 +-
fs/btrfs/tree-log.c | 9 +++++----
3 files changed, 7 insertions(+), 6 deletions(-)
diff --git a/fs/btrfs/btrfs_inode.h b/fs/btrfs/btrfs_inode.h
index c9893a1e433b..289d29a25915 100644
--- a/fs/btrfs/btrfs_inode.h
+++ b/fs/btrfs/btrfs_inode.h
@@ -215,7 +215,7 @@ struct btrfs_inode {
u64 logged_trans;
/*
- * Log transaction ID when this inode was last modified.
+ * Log transaction ID when this inode was last modified or logged.
* Protected by 'lock'.
*/
int last_sub_trans;
diff --git a/fs/btrfs/disk-io.c b/fs/btrfs/disk-io.c
index fde9f4c4ea11..0b9868b3f60b 100644
--- a/fs/btrfs/disk-io.c
+++ b/fs/btrfs/disk-io.c
@@ -991,7 +991,7 @@ int btrfs_add_log_tree(struct btrfs_trans_handle *trans,
root->log_root = log_root;
btrfs_set_root_log_transid(root, 0);
root->log_transid_committed = -1;
- btrfs_set_root_last_log_commit(root, 0);
+ btrfs_set_root_last_log_commit(root, -1);
return 0;
}
diff --git a/fs/btrfs/tree-log.c b/fs/btrfs/tree-log.c
index 2a014087dd83..4c7cd9059ee8 100644
--- a/fs/btrfs/tree-log.c
+++ b/fs/btrfs/tree-log.c
@@ -3575,10 +3575,9 @@ int btrfs_sync_log(struct btrfs_trans_handle *trans,
* root->log_commit[index1] to false and any task attempting to sync the
* log must wait for the previous log transaction to commit if it's
* still in progress or wait for the current log transaction commit if
- * someone else already started it. We use <= and not < because the
- * first log transaction has an ID of 0.
+ * someone else already started it.
*/
- ASSERT(btrfs_get_root_last_log_commit(root) <= log_transid,
+ ASSERT(btrfs_get_root_last_log_commit(root) < log_transid,
"last_log_commit(root)=%d log_transid=%d",
btrfs_get_root_last_log_commit(root), log_transid);
btrfs_set_root_last_log_commit(root, log_transid);
@@ -7271,8 +7270,10 @@ static int btrfs_log_inode(struct btrfs_trans_handle *trans,
* a power failure unless the log was synced as part of an fsync
* against any other unrelated inode.
*/
- if (!ctx->logging_new_name && log_mode != LOG_INODE_EXISTS)
+ if (!ctx->logging_new_name && log_mode != LOG_INODE_EXISTS) {
+ inode->last_sub_trans = ctx->log_transid;
inode->last_log_commit = inode->last_sub_trans;
+ }
spin_unlock(&inode->lock);
/*
base-commit: 6dc1b4de5a72572f84cc3af901172205d2b82ff2
--
2.53.0
^ permalink raw reply related [flat|nested] only message in thread
only message in thread, other threads:[~2026-12-02 16:02 UTC | newest]
Thread overview: (only message) (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-12-02 16:02 [PATCH] btrfs: fix fsync returning before the log is committed Miroslav Cimerman
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.