Use of O_SYNC flag while opening a file
Opening a file in Linux is generally done by the help of open( ) system call. The open( ) function establishes the connection between a file and a file descriptor. It will create an open file description that refers to a file and a file descriptor that refers to that open file description. The file descriptor is used by other I/O functions to refer to the file.
int open(const char *pathname, int flags, mode_t mode);
The function open( ) opens the file specified by pathname. If the specified file does not exist it may optionally be created by providing the O_CREAT flag in open( ). The argument flags must include one of the following access modes: O_RDONLY, O_WRONLY, or O_RDWR . These requests open the file in read-only, write-only, or read/write modes, respectively.
In addition, zero or more file creation flags and file status flags can be bitwise-or%u2019d in flags. The file creation flags affect the semantics of the open operation itself while the file status flag affects the semantics of subsequent I/O operations.
Bitwise-or of O_RDONLY and O_SYNC flag:
Synchronized I/O specifies the open() flags O_SYNC,O_DSYNC, and O_RSYNC for controlling the behaviour. Regardless of whether an implementation supports this option, it must at least support the use of O_SYSNC for regular files.
Linux implements O_SYNC and O_DSYNC, but not O_RSYNC. Somewhat incorrectly, glibc defines O_RSYNC to have the same value as O_SYNC.
O_SYNC provides synchronized I/O file integrity completion, meaning write operations will flush data and all associated metadata to the underlying hardware.
O_DSYNC provides synchronized I/O data integrity completion, meaning write operations will flush data to the underlying hardware, but will only flush metadata updates that are required to allow a subsequent read operation to complete successfully.
Data integrity completion can reduce the number of disk operations that are required for applications that don’t need the guarantees of file integrity completion.
Since Linux 2.6.33, proper O_SYNC support has been provided. However, to ensure backward binary compatibility, O_DSYNC was defined with the same value as the historical O_SYNC, and O_SYNC was defined as a new (two-bit) flag value that includes the O_DSYNC flag value.
This ensures that applications compiled against new headers get at least O_DSYNC semantics on pre-2.6.33 kernels.