Writing programs to cope with I/O errors causing lost writes on Linux

前端 未结 5 860
忘掉有多难
忘掉有多难 2020-12-22 17:01

TL;DR: If the Linux kernel loses a buffered I/O write, is there any way for the application to find out?

I know you have to fsync()

5条回答
  •  别那么骄傲
    2020-12-22 17:37

    write(2) provides less than you expect. The man page is very open about the semantic of a successful write() call:

    A successful return from write() does not make any guarantee that data has been committed to disk. In fact, on some buggy implementations, it does not even guarantee that space has successfully been reserved for the data. The only way to be sure is to call fsync(2) after you are done writing all your data.

    We can conclude that a successful write() merely means that the data has reached the kernel's buffering facilities. If persisting the buffer fails, a subsequent access to the file descriptor will return the error code. As last resort that may be close(). The man page of the close(2) system call contains the following sentence:

    It is quite possible that errors on a previous write(2) operation are first reported at the final close().

    If your application needs to persist data write away it has to use fsync/fsyncdata on a regular basis:

    fsync() transfers ("flushes") all modified in-core data of (i.e., modified buffer cache pages for) the file referred to by the file descriptor fd to the disk device (or other permanent storage device) so that all changed information can be retrieved even after the system crashed or was rebooted. This includes writing through or flushing a disk cache if present. The call blocks until the device reports that the transfer has completed.

提交回复
热议问题