Re: Directory is not persisted after removing another directory and creating a hard linked symlink with the same name if system crashes

From: Filipe Manana

Date: Tue Mar 03 2026 - 12:23:33 EST


On Tue, Mar 3, 2026 at 1:32 PM Vyacheslav Kovalevsky
<slava.kovalevskiy.2014@xxxxxxxxx> wrote:
>
> Detailed description
> ====================
>
> Hello, there seems to be an issue with btrfs crash behavior:
>
> 1. Create new directory `foo`, sync, then remove the directory.
> 2. Create new directories `dir1` and `dir2`.
> 3. Create new symlink named `foo` (NOTE: must have the same name as the
> removed directory).

It doesn't need to be a symlink, a regular file is enough.

Fix here: https://lore.kernel.org/linux-btrfs/9a367f025abf4ea19c96aead75d206e129b2c56e.1772558089.git.fdmanana@xxxxxxxx/

Btw, I think you may have sent a message with one of the longest subjects ever.

Thanks.

> 4. Create new (hard) link to the symlink inside `dir2` (NOTE: this is
> also important).
> 5. Sync directory `dir2`.
> 6. Sync root directory.
>
> Directory `dir1` will be missing even though it was synced in the last step.
>
>
> System info
> ===========
>
> Linux version 7.0-rc2, also tested on 6.19.2
>
>
> How to reproduce
> ================
>
> ```
> #include <errno.h>
> #include <fcntl.h>
> #include <stdio.h>
> #include <string.h>
> #include <sys/stat.h>
> #include <sys/types.h>
> #include <unistd.h>
>
> int main() {
> int status;
> int dir_fd;
> int root_fd;
>
> status = mkdir("foo", S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH);
> printf("MKDIR: %d\n", status);
>
> sync();
>
> status = rmdir("foo");
> printf("RMDIR: %d\n", status);
>
> status = mkdir("dir1", S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH);
> printf("MKDIR: %d\n", status);
>
> status = mkdir("dir2", S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH);
> printf("MKDIR: %d\n", status);
>
> status = symlink("bar", "foo");
> printf("SYMLINK: %d\n", status);
>
> status = link("foo", "dir2/link");
> printf("LINK: %d\n", status);
>
> status = open("dir2", O_RDONLY);
> printf("OPEN: %d\n", status);
> dir_fd = status;
>
> status = fsync(dir_fd);
> printf("FSYNC: %d\n", status);
>
> status = open(".", O_RDONLY);
> printf("OPEN: %d\n", status);
> root_fd = status;
>
> status = fsync(root_fd);
> printf("FSYNC: %d\n", status);
> }
> // after crash `dir1` is missing
> ```
>
> Steps:
>
> 1. Create and mount new btrfs file system in default configuration.
> 2. Change directory to root of the file system and run the compiled test.
> 3. Cause hard system crash (e.g. QEMU `system_reset` command).
> 4. Remount file system after crash.
> 5. Observe that directory `dir1` is missing.
>
>