Linux Btrfs filesystem development
 help / color / mirror / Atom feed
* [PATCH] btrfs: fix fsync of an evicted file logged only as existing
@ 2026-12-03  0:17 Miroslav Cimerman
  0 siblings, 0 replies; only message in thread
From: Miroslav Cimerman @ 2026-12-03  0:17 UTC (permalink / raw)
  To: linux-btrfs; +Cc: David Sterba, Chris Mason, Filipe Manana

btrfs_inode_in_log() takes a file as logged if it was logged in the
current transaction after its last change:

  logged_trans == generation
  last_sub_trans <= inode->last_log_commit
  last_sub_trans <= root->last_log_commit

A directory fsync logs its new files with LOG_INODE_EXISTS. This sets
their logged_trans but not their last_log_commit, so that a later fsync
of such a file still logs its data. However, an inode that was evicted
and loaded again starts with last_sub_trans and last_log_commit both 0.
The check then passes, and the fsync of the file returns 0 without
logging its data:

  mkdir /mnt/dir
  sync
  xfs_io -f -d -c "pwrite 0 64K" /mnt/dir/foo
  touch /mnt/dir/bar
  ( cd /mnt/dir; sleep 60 ) &    # keep dir in memory
  sleep 0.1
  echo 2 > /proc/sys/vm/drop_caches
  xfs_io -c fsync /mnt/dir
  xfs_io -c fsync /mnt/dir/foo
  <power failure>

After log replay foo has a size of 0.

Start last_log_commit at -1, so that an inode counts as logged only
after it was logged with LOG_INODE_ALL.

Fixes: 2f2ff0ee5e43 ("Btrfs: fix metadata inconsistencies after directory fsync")
CC: stable@vger.kernel.org
Signed-off-by: Miroslav Cimerman <mc@raidlab.org>
---
Found while analyzing a FuzzyRAID finding.

Reproduced on for-next with dm-flakey dropping writes after the last
fsync: the data is lost on stock and kept with the fix.

Prepared with help from an LLM.

 fs/btrfs/inode.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/fs/btrfs/inode.c b/fs/btrfs/inode.c
index 539b1851a477..2e1cb26a5aa1 100644
--- a/fs/btrfs/inode.c
+++ b/fs/btrfs/inode.c
@@ -8057,7 +8057,7 @@ struct inode *btrfs_alloc_inode(struct super_block *sb)
 	ei->dir_index = 0;
 	ei->last_unlink_trans = 0;
 	ei->last_reflink_trans = 0;
-	ei->last_log_commit = 0;
+	ei->last_log_commit = -1;
 
 	spin_lock_init(&ei->lock);
 	ei->outstanding_extents = 0;

base-commit: 6dc1b4de5a72572f84cc3af901172205d2b82ff2
-- 
2.53.0


^ permalink raw reply related	[flat|nested] only message in thread

only message in thread, other threads:[~2026-12-03  0:17 UTC | newest]

Thread overview: (only message) (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-12-03  0:17 [PATCH] btrfs: fix fsync of an evicted file logged only as existing Miroslav Cimerman

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox