Ensure fsync did its job

2019-07-16 05:51发布

问题:

manpage for fsync says:

If the underlying hard disk has write caching enabled, then the data may not really be on permanent storage when fsync() / fdatasync() return.

Is there any way how to ensure that data was physically written to the disk?

回答1:

No, there is not.

With fsync you tell your OS to write it to disk and as far as the OS is concerned, it has been written to disk.

If disks are faking this then it is not something you can really change unfortunately. With proper disk systems (i.e. BBU raid setups) you can simply enable/disable write cache to avoid this mostly.

Do note that if you specify the O_DIRECT and O_SYNC flags, it should write it to disk: http://www.kernel.org/doc/man-pages/online/pages/man2/open.2.html