[PATCH v2] btrfs: fix loss of fsynced file after link into new dir and unlink

From: Mani Tofigh via B4 Relay

Date: Mon Nov 23 2026 - 01:08:21 EST


From: Mani Tofigh <mt4022@xxxxxxxxxxxx>

If we create a file, add a hard link to it in a new directory, remove
its old name and then fsync the file through a file descriptor opened
before the unlink, the file is gone after a power failure. Example:

mkdir /mnt/A
fd = open("/mnt/A/foo", O_CREAT | O_WRONLY)
mkdir /mnt/A/B
ln /mnt/A/foo /mnt/A/B/bar
unlink /mnt/A/foo
fsync(fd)
<power fail>

After log replay, B and B/bar don't exist and the file is gone.

The fsync uses the file's dentry, which is still the unlinked A/foo.
The inode is new, so btrfs_log_all_parents() finds no parent in the
commit root, and log_all_new_ancestors() then takes the fast path for a
single link. That path walks up from the dentry's parent A and never
logs B. On log replay the inode ref for B/bar is skipped because B
doesn't exist, and the inode ends up with no links.

Fix this by not taking the fast path when the inode had a link, unlink
or rename in the current transaction. The slow path finds the parents
from the inode refs in the subvolume tree and logs B and A. Also return
early for an inode with a link count of 0, which has no parents to log.

The problem is old, e.g., v7.1 has it. It was hidden by a transaction
commit fallback between commit 0f24ea456ae1 ("btrfs: tracepoints: add
trace event for btrfs_log_all_parents()") and
commit 36f9aafa46f5 ("btrfs: fix unnecessary transaction commit fallback
from btrfs_log_all_parents()").

With the fix, the file survives log replay.

CC: stable@xxxxxxxxxxxxxxx
Signed-off-by: Mani Tofigh <mt4022@xxxxxxxxxxxx>
---
Changes in v2:
- Return early from log_all_new_ancestors() for an inode with a link
count of 0, to avoid a transaction commit (Filipe)
- Drop the fdatasync mention and the reproducer paragraph (Filipe)
- Rebase on btrfs for-next
- Link to v1: https://lore.kernel.org/r/20261111-btrfs-link-unlink-fsync-v1-1-a817d4a890af@xxxxxxxxxxxx
---
fs/btrfs/tree-log.c | 12 ++++++++++--
1 file changed, 10 insertions(+), 2 deletions(-)

diff --git a/fs/btrfs/tree-log.c b/fs/btrfs/tree-log.c
index 763487114f85..445374b06ee8 100644
--- a/fs/btrfs/tree-log.c
+++ b/fs/btrfs/tree-log.c
@@ -7541,11 +7541,19 @@ static int log_all_new_ancestors(struct btrfs_trans_handle *trans,

trace_btrfs_log_all_new_ancestors_enter(trans, inode);

+ if (inode->vfs_inode.i_nlink == 0) {
+ ret = 0;
+ goto out;
+ }
+
/*
* For a single hard link case, go through a fast path that does not
- * need to iterate the fs/subvolume tree.
+ * need to iterate the fs/subvolume tree. But not if the inode had a
+ * link, unlink or rename in the current transaction, since the dentry
+ * used for the fsync may then be an unlinked one.
*/
- if (inode->vfs_inode.i_nlink < 2) {
+ if (inode->vfs_inode.i_nlink < 2 &&
+ inode->last_unlink_trans < trans->transid) {
ret = log_new_ancestors_fast(trans, inode, parent, ctx);
goto out;
}

---
base-commit: 845c6105edd82f5d0aa72377ef6eb8592ac4e00a
change-id: 20261123-btrfs-link-unlink-fsync-a3a915511369

Best regards,
--
Mani Tofigh <mt4022@xxxxxxxxxxxx>