From: Vyacheslav Kovalevsky <slava.kovalevskiy.2014@gmail.com>
To: clm@fb.com, dsterba@suse.com
Cc: linux-btrfs@vger.kernel.org, linux-kernel@vger.kernel.org
Subject: File size is not persisted after opening the file with O_TRUNC flag and creating hard link if system crashes
Date: Tue, 17 Feb 2026 11:42:49 +0300 [thread overview]
Message-ID: <af8c15fa-4e41-4bb2-885c-0bc4e97532a6@gmail.com> (raw)
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.
next reply other threads:[~2026-02-17 8:42 UTC|newest]
Thread overview: 2+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-02-17 8:42 Vyacheslav Kovalevsky [this message]
2026-02-17 18:32 ` File size is not persisted after opening the file with O_TRUNC flag and creating hard link if system crashes Filipe Manana
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=af8c15fa-4e41-4bb2-885c-0bc4e97532a6@gmail.com \
--to=slava.kovalevskiy.2014@gmail.com \
--cc=clm@fb.com \
--cc=dsterba@suse.com \
--cc=linux-btrfs@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox