Linux Btrfs filesystem development
 help / color / mirror / Atom feed
From: Miroslav Cimerman <mc@raidlab.org>
To: linux-btrfs@vger.kernel.org
Cc: David Sterba <dsterba@suse.com>, Chris Mason <mason@kernel.org>,
	Filipe Manana <fdmanana@suse.com>
Subject: [PATCH] btrfs: fix fsync of an evicted file logged only as existing
Date: Thu,  3 Dec 2026 00:17:43 +0000	[thread overview]
Message-ID: <20261203001743.401733-1-mc@raidlab.org> (raw)

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


                 reply	other threads:[~2026-12-03  0:17 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20261203001743.401733-1-mc@raidlab.org \
    --to=mc@raidlab.org \
    --cc=dsterba@suse.com \
    --cc=fdmanana@suse.com \
    --cc=linux-btrfs@vger.kernel.org \
    --cc=mason@kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox