Does Linux guarantee the contents of a file is flu

2020-01-26 08:48发布

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?

9条回答
爱情/是我丢掉的垃圾
2楼-- · 2020-01-26 09:41

The manpage for open says:

To guarantee synchronous I/O the O_SYNC must be used in addition to O_DIRECT.

and that

In general this (O_DIRECT) will degrade performance.

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.

查看更多
戒情不戒烟
3楼-- · 2020-01-26 09:43

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.

查看更多
老娘就宠你
4楼-- · 2020-01-26 09:43

I don't think Linux can guarantee this since the drive itself can cache data too.

查看更多
登录 后发表回答