public inbox for linux-btrfs@vger.kernel.org
 help / color / mirror / Atom feed
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: mtime and ctime is not updated after truncate operation if file length was not changed
Date: Fri, 12 Dec 2025 12:34:10 +0300	[thread overview]
Message-ID: <3d517755-3ed1-46eb-9fee-c4e56193ebc4@gmail.com> (raw)

Detailed description
====================

Last data modification and last file status change timestamps are not 
updated after truncate operation if the file content was not changed 
(truncate length == file length).

ext4 updates both mtime and ctime. But many (if not all?) other file 
systems (btrfs, xfs, nilfs2, f2fs, ???) do not update any timestamps.

The latter behavior seems to violate the POSIX.1-2024 standard. 
Documentation for `truncate()`: “Upon successful completion, truncate() 
shall mark for update the last data modification and last file status 
change timestamps of the file". No special cases described for (not) 
updating timestamps. `ftruncate` behaves as expected though.

Maybe I should also report this to the developers of other file systems.


System info
===========

Linux version 6.18.


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;
   struct stat file_stat;

   status = creat("file", S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH);
   printf("CREAT: %d\n", status);
   close(status);

   status = stat("file", &file_stat);
   printf("STAT: %d\n", status);
   printf("atime=%ld\n", file_stat.st_atim.tv_nsec);
   printf("mtime=%ld\n", file_stat.st_mtim.tv_nsec);
   printf("ctime=%ld\n", file_stat.st_ctim.tv_nsec);

   status = truncate("file", 0);
   printf("TRUNCATE: %d\n", status);

   status = stat("file", &file_stat);
   printf("STAT: %d\n", status);
   printf("atime=%ld\n", file_stat.st_atim.tv_nsec);
   printf("mtime=%ld\n", file_stat.st_mtim.tv_nsec);
   printf("ctime=%ld\n", file_stat.st_ctim.tv_nsec);
}
```

Expected output (ext4):

```
CREAT: 3
STAT: 0
atime=122561651
mtime=122561651
ctime=122561651
TRUNCATE: 0
STAT: 0
atime=122561651
mtime=124063093
ctime=124063093
```

Actual output (Btrfs):

```
CREAT: 3
STAT: 0
atime=502902570
mtime=502902570
ctime=502902570
TRUNCATE: 0
STAT: 0
atime=502902570
mtime=502902570
ctime=502902570
```


                 reply	other threads:[~2025-12-12  9:34 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

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=3d517755-3ed1-46eb-9fee-c4e56193ebc4@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