File system corruption after writing to unlinked file and using fdatasync on the file if system crashes
From: Vyacheslav Kovalevsky
Date: Wed Mar 11 2026 - 09:04:13 EST
Detailed description
====================
Hello, there seems to be an issue with NILFS2 crash behavior:
1. Create new file and truncate to some length.
2. Unlink the file but keep the file descriptor open.
3. Make new empty directory.
4. Sync file system.
5. Write some data to the file.
6. Apply fdatasync() to the file.
After system crash (e.g. power failure) mounting file system results in error message `Stale file handle`. See details below.
System info
===========
Linux version 7.0-rc2, also tested on 6.19.2
nilfs-tools version 2.2.11
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 file_fd;
status =
open("file", O_WRONLY | O_CREAT, S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH);
printf("OPEN: %d\n", status);
file_fd = status;
status = ftruncate(file_fd, 1000);
printf("FTRUNCATE: %d\n", status);
status = unlink("file");
printf("UNLINK: %d\n", status);
status = mkdir("dir", S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH);
printf("MKDIR: %d\n", status);
sync();
status = write(file_fd, "Test data!", 10);
printf("WRITE: %d\n", status);
status = fdatasync(file_fd); // everything is fine if using fsync() instead...
printf("FDATASYNC: %d\n", status);
}
// file system is unmountable after crash
// `mount` output:
// mount.nilfs2: Error while mounting /dev/vdb on /mnt/fstest: Stale file handle
// `dmesg` output:
// [ 29.941736] NILFS (vdb): mounting unchecked fs
// [ 29.953605] NILFS (vdb): error -116 recovering data block (ino=11, block-offset=0)
// [ 29.953609] NILFS (vdb): error -116 roll-forwarding partial segment at blocknr = 26
```
Steps:
1. Create and mount new NILFS2 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 mount fails.