Can I use O_DIRECT for write requests to avoid data loss during power failure?
No!
On Linux while O_DIRECT
tries to bypass your OS's cache it never bypasses your disk's cache. If your disk has a volatile write cache you can still lose data that was only in the disk cache during an abrupt power off!
Does O_DIRECT
mean that the data bypass OS cache completely?
Usually, but some Linux filesystems may fall back to buffered I/O with O_DIRECT
(the Ext4 Wiki Clarifying Direct IO's Semantics
page warns this can happen with allocating writes).
If the request returns successful to the application, does it mean that the data must have been flushed to the disk?
It usually means the disk has "seen" it but see the above caveats (e.g. data might have gone to buffer cache / data might only be in disk's volatile cache).
If I open a regular file in one file system, how about the FS metadata? Is it also be flushed immediately, or is it cached?
Excellent question! Metadata may still be rolling around in cache and not yet synced to disk even though the request finished successfully.
All of the above mean you HAVE to do the appropriate fsync()
command in the correct places (and check their results!) if you want to be sure whether an operation has reached non-volatile storage. See https://thunk.org/tytso/blog/2009/03/15/dont-fear-the-fsync/ and the LWN article "Ensuring data reaches disk" for details.