5

man 2 write states:

POSIX requires that a read(2) that can be proved to occur after a write() has returned will return the new data. Note that not all filesystems are POSIX conforming.

In Linux, is this also true for stat(2) and fstat(2), in particular, for the stat.st_size member?
Specifically, if you open a file with O_CREAT, successfully write 948427 bytes to it, then either stat or fstat it, are you guaranteed to see a st_size of 948427?
(If so, is this a guarantee for POSIX filesystems, or something that typical Linux filesystems provide in practice, or a property of certain filesystems and not others?)

1
  • I would be very, very surprised if any one of the usual general purpose local FSs wouldn't be able to keep track of the file size after a write() to be able to return it with stat(). The OS needs to know the file size at least to allocate data blocks and to put append-mode writes in the correct position anyway. Now, NFS and such remote filesystems could be different, as all the data isn't available locally. I don't know if stat() goes to the server on NFS, but AFAIU, reads in general don't, so in the case of multiple clients writing, the read-after-write guarantees might not hold. Commented Dec 3, 2023 at 20:37

1 Answer 1

-1

stat retrieves the st_size as well as other informations (metadata) from the inode :

…Each file has an inode containing metadata about the file. An application can retrieve this metadata using stat(2) (or related calls), which returns a stat structure…

So everything boils down to the question : When will the inode be updated following a successful write ? it can be :

Synchronously : The metadata is commited in sync with the data, before write returns

  • per-file using the O_SYNC flag when open-ing the file

By the time write(2) (or similar) returns, the output data and associated file metadata have been transferred to the underlying hardware

When a file with the 'S' attribute set is modified, the changes are written synchronously to the disk; this is equivalent to the 'sync' mount option applied to a subset of the files.

In these cases of synchronous updates, yes, stat requested after some write will reflect changes made.

Asynchronously : The commit of the metadata is not triggered by data changes, the default… in which case, whatever the (modern) filesystem, a consecutive stat will reflect changes only… per pure chance…

6
  • 1
    The last part is incorrect. Even if the data hasn't been flushed to the disk, the metadata is stored in buffer cache. The kernel will serve stat calls from its internal structure and thus on POSIX filesystems it will always be up to date. The only exception is if the machine had cold boot before data was synced, and after boot the inode will not include the latest writes. Commented Dec 3, 2023 at 18:01
  • @aviro : I'm possibly missing something. Whatever the FS, the st_size might be extracted from the inode cache. And the kernel offers no guarantee that when write returns the inode cache has been updated. (Some experiences proves this with EXT4 but OK, I agree, EXT4 is not fully POSIX.) Commented Dec 3, 2023 at 18:32
  • 1
    Pay attention again to the quote in the question about the POSIX requirement. It's very clear. It requires that stat after write on POSIX compliant filesystems will return the updated data. It doesn't say anthing about sync/async. The implementation of most filesystems are in the kernel, and in order for them to be POSIX compliant they absolutely guarantee that this requirement is fulfilled, by any means necessary, regardless if the data is written to the disk or not. Not all filesystems are POSIX compliant (see NFS, for instance), but those who are absolutely guarantee this behavior. Commented Dec 3, 2023 at 19:31
  • By the way, what makes you think EXT4 is not POSIX compliant? Commented Dec 3, 2023 at 19:35
  • 1
    I'm pretty sure the boltdb issue is a red herring: it's about ACID compliance under power cut, but the question here is about the behavior on a running system, not about durability. Commented Dec 5, 2023 at 23:05

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.