* File size is not persisted after opening the file with O_TRUNC flag and creating hard link if system crashes
@ 2026-02-17 8:42 Vyacheslav Kovalevsky
2026-02-17 18:32 ` Filipe Manana
0 siblings, 1 reply; 2+ messages in thread
From: Vyacheslav Kovalevsky @ 2026-02-17 8:42 UTC (permalink / raw)
To: clm, dsterba; +Cc: linux-btrfs, linux-kernel
Detailed description
====================
Hello, there seems to be an issue with btrfs crash behavior:
1. Create an empty file in a directory.
2. Fill file with data (e.g. truncate) and sync the file.
3. Open file with O_TRUNC flag (should set size to 0) and sync the file.
4. Create a new hard link to the file in the same directory.
5. Sync the directory.
After system crash the file will have the old size (>0) even though the
file was synced after truncating (O_TRUNC).
System info
===========
Linux version 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 file_fd;
int dir_fd;
status = creat("file1", S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH);
printf("CREAT: %d\n", status);
status = truncate("file1", 1000);
printf("TRUNCATE: %d\n", status);
sync();
status = open("file1", O_RDWR | O_TRUNC);
printf("OPEN: %d\n", status);
file_fd = status;
status = fsync(file_fd);
printf("FSYNC: %d\n", status);
status = open(".", O_RDONLY | O_DIRECTORY);
printf("OPEN: %d\n", status);
dir_fd = status;
status = link("file1", "file2");
printf("LINK: %d\n", status);
status = fsync(dir_fd);
printf("FSYNC: %d\n", status);
}
// file size is 1000 instead of 0
```
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 file size is 1000 instead of 0.
^ permalink raw reply [flat|nested] 2+ messages in thread
* Re: File size is not persisted after opening the file with O_TRUNC flag and creating hard link if system crashes
2026-02-17 8:42 File size is not persisted after opening the file with O_TRUNC flag and creating hard link if system crashes Vyacheslav Kovalevsky
@ 2026-02-17 18:32 ` Filipe Manana
0 siblings, 0 replies; 2+ messages in thread
From: Filipe Manana @ 2026-02-17 18:32 UTC (permalink / raw)
To: Vyacheslav Kovalevsky; +Cc: clm, dsterba, linux-btrfs, linux-kernel
On Tue, Feb 17, 2026 at 8:43 AM Vyacheslav Kovalevsky
<slava.kovalevskiy.2014@gmail.com> wrote:
>
> Detailed description
> ====================
>
> Hello, there seems to be an issue with btrfs crash behavior:
>
> 1. Create an empty file in a directory.
> 2. Fill file with data (e.g. truncate) and sync the file.
By the way, truncate doesn't fill any data, it just increases i_size
and leaves a hole in the file.
> 3. Open file with O_TRUNC flag (should set size to 0) and sync the file.
A truncate to 0 achieves the same, O_TRUNC is not necessary.
The issue only happens if we truncate to 0, to any other size it works fine.
Fixed here:
https://lore.kernel.org/linux-btrfs/cover.1771350720.git.fdmanana@suse.com/
Thanks.
> 4. Create a new hard link to the file in the same directory.
> 5. Sync the directory.
>
> After system crash the file will have the old size (>0) even though the
> file was synced after truncating (O_TRUNC).
>
>
> System info
> ===========
>
> Linux version 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 file_fd;
> int dir_fd;
>
> status = creat("file1", S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH);
> printf("CREAT: %d\n", status);
>
> status = truncate("file1", 1000);
> printf("TRUNCATE: %d\n", status);
>
> sync();
>
> status = open("file1", O_RDWR | O_TRUNC);
> printf("OPEN: %d\n", status);
> file_fd = status;
>
> status = fsync(file_fd);
> printf("FSYNC: %d\n", status);
>
> status = open(".", O_RDONLY | O_DIRECTORY);
> printf("OPEN: %d\n", status);
> dir_fd = status;
>
> status = link("file1", "file2");
> printf("LINK: %d\n", status);
>
> status = fsync(dir_fd);
> printf("FSYNC: %d\n", status);
> }
> // file size is 1000 instead of 0
> ```
>
> 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 file size is 1000 instead of 0.
>
^ permalink raw reply [flat|nested] 2+ messages in thread
end of thread, other threads:[~2026-02-17 18:33 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-02-17 8:42 File size is not persisted after opening the file with O_TRUNC flag and creating hard link if system crashes Vyacheslav Kovalevsky
2026-02-17 18:32 ` Filipe Manana
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox