Home >Operation and Maintenance >Linux Operation and Maintenance >How to ensure safe data transfer in Linux
In many IO scenarios, we often need to ensure that the data has been safely written to the disk so that it can be read after the system crashes and restarts. data. But we all know that the IO path of the Linux system is still very complicated and is divided into many layers. Each layer may have buffers to accelerate IO reading and writing. At the same time, user-mode applications and library functions may also have their own buffers, which adds some complexity to the IO path. It can be seen that if you want to ensure that data is safely written to the disk, it cannot be done by simply adjusting write/fwrite.
So what to do? Many people will think of many ways, such as: fflush(), fsync(), fdatasync(), sync(), open() using the O_DIRECT or O_SYNC flag, etc. Well, these means (or some combination) can indeed ensure the safe persistence of data, so what is the difference between them? What is the difference between fflush() and fsync()? What does O_DIRECT mean? Can it ensure safe data persistence? What is the difference between O_DIRECT and O_SYNC? What about O_SYNC and fsync()? Can fsync complete the functions of msync? This article will try to understand and explain the functions and differences of these concepts.
It is said that a picture is worth a thousand words. In order to analyze the difference between these concepts clearly, I specially drew a picture and look at it carefully. , it should be possible to clearly see their functions and differences.
http://man7.org/linux/man-pages/man2/open.2.html Description of related parameters:
https://lwn.net/Articles/457667/
http://man7.org/linux/man-pages/man2/fsync.2.html
http://man7.org/linux/man-pages/man2/msync.2.html
In fact, there is another IO mode, which is DAX (Direct Access). Does it look similar to O_DIRECT? This mode requires support from both the filesystem and block driver. It is generally used on non-volatile memory. In essence, it bypasses the page cache and directly operates the device. This article will not discuss DAX in depth. Later, I will write a ramdisk block device driver that supports DAX mode, then format it into an ext4 file system and mount it in -o dax mode, and then study the IO path of DAX in detail.
For more Linux articles, please visit the Linux Tutorial column to learn!
The above is the detailed content of How to ensure safe data transfer in Linux. For more information, please follow other related articles on the PHP Chinese website!