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

From: Filipe Manana

Date: Thu Nov 12 2026 - 10:11:46 EST


On Wed, Nov 11, 2026 at 9:42 PM Mani Tofigh via B4 Relay
<devnull+mt4022.columbia.edu@xxxxxxxxxx> wrote:
>
> 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) /* or fdatasync(fd) */

fdatasync and fsync do exactly the same thing on btrfs, so no need to comment.

> <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. That only
> adds a search of the subvolume tree.
>
> The problem is old, e.g., v7.1 loses the file as well. It was only
> hidden, by a fallback to a transaction commit, 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 and its data survive.
>
> This bug was identified and confirmed with the help of a deterministic
> hypervisor currently under development. The C reproducer below however
> hits it every time.

This last paragraph is useless in the changelog because:

1) It's pointless to know that it was found using a hypervisor under
development.
Unless it exists, has a name, and is available somewhere, it adds
nothing useful.

2) Who reads the change log after commiting the patch, has no idea
what the C reproducer is.
The link to the reproducer is after the ---, so it does not get to the
changelog.
Plus, the log should should be as self contained as possible, with no
links to personal github accounts that may disappear anytime.
The reproducer is also unnecessarily long and complex, nearly 400 lines.
At the top of the changelog we already have simple steps to reproduce,
that is enough.

>
> CC: stable@xxxxxxxxxxxxxxx
> Signed-off-by: Mani Tofigh <mt4022@xxxxxxxxxxxx>
> ---
> Reproducer: https://github.com/manitofigh/files/blob/main/linux-repros/btrfslink.c
>
> It uses dm-flakey to drop all writes once fsync returns.
>
> No Fixes: tag since this looks older than the single link fast path.
> ---
> fs/btrfs/tree-log.c | 8 ++++++--
> 1 file changed, 6 insertions(+), 2 deletions(-)
>
> diff --git a/fs/btrfs/tree-log.c b/fs/btrfs/tree-log.c
> index 85ef3c26e0e4..4f24b277f1b7 100644
> --- a/fs/btrfs/tree-log.c
> +++ b/fs/btrfs/tree-log.c
> @@ -7543,9 +7543,13 @@ static int log_all_new_ancestors(struct btrfs_trans_handle *trans,
>
> /*
> * 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 then the
> + * dentry used for the fsync may be an unlinked one, whose parent is
> + * not the parent of the remaining link.
> */
> - if (inode->vfs_inode.i_nlink < 2) {
> + if (inode->vfs_inode.i_nlink < 2 &&
> + inode->last_unlink_trans < trans->transid) {

So this now introduces the possibility of entering the fast path for
an inode with no refs (link count of 0),
wich will result in the function returning 1 (coming from the
btrfs_search_slot() call), and triggering a
transaction commit.

Early after the tracepoint call we should have:

if (inode->vfs_inode.i_nlink == 0) {
ret = 0;
goto out;
}

See: https://sashiko.dev/#/patchset/20261111-btrfs-link-unlink-fsync-v1-1-a817d4a890af%40columbia.edu

Otherwise it looks good, thanks.


> ret = log_new_ancestors_fast(trans, inode, parent, ctx);
> goto out;
> }
>
> ---
> base-commit: 4c34f0909344e48d917afac0eeffb6e1c2681524
> change-id: 20261111-btrfs-link-unlink-fsync-28ea82e5c56b
>
> Best regards,
> --
> Mani Tofigh <mt4022@xxxxxxxxxxxx>
>
>
>