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

前端 未结 5 849
忘掉有多难
忘掉有多难 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:38

    Since the application's write() will have already returned without error, there seems to be no way to report an error back to the application.

    I do not agree. write can return without error if the write is simply queued, but the error will be reported on the next operation that will require the actual writing on disk, that means on next fsync, possibly on a following write if the system decides to flush the cache and at least on last file close.

    That is the reason why it is essential for application to test the return value of close to detect possible write errors.

    If you really need to be able to do clever error processing you must assume that everything that was written since the last successful fsync may have failed and that in all that at least something has failed.

提交回复
热议问题