When a file is closed using close()
or fclose()
(for example), does Linux guarantee that the file is written back to (persistent) disc?
What I mean is, if close()
returns 0 and then immediately afterwards the power fails, are previously written data guaranteed to persist, i.e. be durable?
The fsync()
system call does provide this guarantee. Is closing a file also sufficient?
I can't find anything which makes any claim one way or another at the moment.
Question 2:
If close()
does implicitly do an fsync()
, is there a way of telling it not to?
The manpage for open says:
and that
One could toggle this flag using fcntl with F_SETFL so as to minimize cache effects of the I/O for every read and write there after.
No, it's not guaranteed. The OS has its own caching. All close really guarantees is that the programs buffers are flushed to the OS, but the OS may still be holding onto it unwritten. I believe there is some controversy in the Linux kernel world because even fsync doesn't guarantee that it's flushed to disk, at least in ext3.
I don't think Linux can guarantee this since the drive itself can cache data too.