Chinaunix

标题: 请教:哪种方式能保证文件系统数据完整写入磁盘?sync or direct IO or 其它? [打印本页]

作者: humjb_1983    时间: 2014-11-14 12:50
标题: 请教:哪种方式能保证文件系统数据完整写入磁盘?sync or direct IO or 其它?
RT,谢谢!
作者: Tinnal    时间: 2014-11-14 14:17
回复 1# humjb_1983

http://stackoverflow.com/questio ... en2-different-alike

O_DIRECT alone only promises that the kernel will avoid copying data from user space to kernel space, and will instead write it directly via DMA (if possible). Data does not go into caches. There is no strict guarantee that the function will return only after all data has been transferred.

O_SYNC guarantees that the call will not return before all data has been transferred to the disk (as far as the OS can tell). This still does not guarantee that the data isn't somewhere in the harddisk write cache, but it is as much as the OS can guarantee.

O_DIRECT|O_SYNC is the combination of these, i.e. "DMA + guarantee".
作者: Tinnal    时间: 2014-11-14 14:20
回复 1# humjb_1983


    最近很少上来呀。 特别的,这两种方法,如果是采用mmap文件的方式,就都不能实现直接写到硬盘。
作者: humjb_1983    时间: 2014-11-14 17:04
Tinnal 发表于 2014-11-14 14:17
回复 1# humjb_1983

http://stackoverflow.com/questio ... ync-and-o-direct-fl ...

呵呵,最近比较忙,感谢Tinnal兄!
那就是说,o_direct标记下,write时,不会等待数据写入磁盘后中断上来,触发endio么?
作者: Tinnal    时间: 2014-11-14 20:30
回复 4# humjb_1983


   安stackoverflow的解析应该是这样的。O_DIRECT,从英文直译来说,也只体现直接,而非同步。体现的是免拷贝。
作者: Tinnal    时间: 2014-11-14 20:31
但我也没有去翻内核代码,有时候Linux常做得比承诺的要好。
作者: 镇水铁牛    时间: 2014-11-14 21:47
O_DIRECT是直写的概念,免拷贝、io不会merge,直接下发给磁盘,但磁盘什么时候endio不一定,数据是有丢失的风险。
要保证数据的一致性,只有以同步写方式下发io,应该是O_SYNC,这个一般很少用。
direct io性能一般较低(一些特殊应用场景,性能也不错), 同步io就更慢点。
作者: humjb_1983    时间: 2014-11-17 12:39
镇水铁牛 发表于 2014-11-14 21:47
O_DIRECT是直写的概念,免拷贝、io不会merge,直接下发给磁盘,但磁盘什么时候endio不一定,数据是有丢失的 ...

感谢,代码看起来差不多是这样~
作者: humjb_1983    时间: 2014-11-17 12:41
Tinnal 发表于 2014-11-14 20:30
回复 4# humjb_1983

感谢兄弟。
看了下代码,好像确实没发现有等的地方,但是各个内核版本间,相关流程差异比较大,看得有点晕~,后面有时间再研究下~。




欢迎光临 Chinaunix (http://bbs.chinaunix.net/) Powered by Discuz! X3.2