"James K. Lowden" <
jklo...@speakeasy.net> writes:
[...]
> What exactly am I guaranteed -- and what can I really expect -- from
> msync(2)? What is its relationship to fsync(2)?
Inherently, none. On systems which don't have a 'unified page cache',
fsync may be need so that data written via write becomes visible in
mmapped pages and vice-versa (OTOH, I've heard that even the HP-UX
people have meanwhile - grudgeingly - accepted that this really
doesn't make any sense :-).
[...]
> Then I call mmap(2), then memcpy(3), then msync(2). Now what?
>
>
http://pubs.opengroup.org/onlinepubs/009695399/functions/msync.html
>
> says
>
> "the msync() function shall ensure that all write operations
> are completed as defined for synchronized I/O data integrity
> completion."
>
> which isn't much of a commitment; "synchronized I/O data integrity"
> says nothing about rusting bits on the disk. It just says that a read
> initiated after a write reads what the write wrote.
It doesn't say that and the property you mention is part of the
definition of read and write, without any 'sync operation'. The actual
definition is
For write, when the operation has been completed or diagnosed
if unsuccessful. The write is complete only when the data
specified in the write request is successfully transferred and
all file system information required to retrieve the data is
successfully transferred.
> My intention is to write to the file only via the pointer returned by
> mmap(2), and to call msync(2) at unit-of-work boundaries. Am I also
> bound to call fsync(2)?
No, except for possibly for directories.
>
> If I do call fsync(2), that's apparently not enough;
>
http://linux.die.net/man/2/fsync says
>
> "Calling fsync() does not necessarily ensure that the entry in
> the directory containing the file has also reached disk. For that an
> explicit fsync() on a file descriptor for the directory is also needed."
>
> But what could that mean?
That a directory is a file and that fsyncs done on file A -
unsurprisingly - don't affect file B. It's just that writes to
directory files happen automatically as side effect of writes to other
files and traditionally, these 'metadata writes' are not treated
specially by 'Linux filesystems'. If this concerns you, just do as the
manpage suggests - open a file descriptor to the directory and fsync
it.