1)打开一个文件时如果加入了O_DIRECT标志位,意味着对该文件的读写操作将会绕过page cache,直接与存储设备打交道,但是这里不保证每次write返回后,该write要写入的数据已经写完。
2)如果是带O_SYNC标志位,则不绕过pape cache, 每次write的数据不仅会在page cache里写入,还会写入存储设备,并且,O_SYNC保证每次write返回后,数据都已经写入page cache和存储设备。
Direct I/O and Data I/O Integrity Completion
Although direct I/O writes are done synchronously, they do not provide synchronized I/O data integrity completion, as defined by POSIX. Applications that need this feature should use O_DSYNC in addition to O_DIRECT. O_DSYNC guarantees that all of the data and enough of the metadata (for example, indirect blocks) have written to the stable store to be able to retrieve the data after a system crash. O_DIRECT only writes the data; it does not write the metadata.
在通常的write中,实际是写的页缓存,页缓存通过周期性的flush(pdflush)或强制回写到磁盘,页缓存回写时,是通过sync_single_inode按inode来回写的,回写过程中会先调用do_writepages回写数据,然后再调用write_inode回写元数据,所以是有先后顺序的,通常也能保证一致性。
innodb_flush_method这个参数控制着innodb数据文件及redo log的打开、刷写模式,对于这个参数,文档上是这样描述的:
有三个值:fdatasync(默认),O_DSYNC,O_DIRECT
默认是fdatasync,调用fsync()去刷数据文件与redo log的buffer
为O_DSYNC时,innodb会使用O_SYNC方式打开和刷写redo log,使用fsync()刷写数据文件
为O_DIRECT时,innodb使用O_DIRECT打开数据文件,使用fsync()刷写数据文件跟redo log
Command-Line Format
--innodb_flush_method=name
System Variable Name
innodb_flush_method
Variable Scope
Global
Dynamic Variable
No
Permitted Values
Type (Windows)
string
Default
async_unbuffered
Permitted Values (<= 5.1.23)
Type (Unix)
string
Default
fdatasync
Valid Values
fdatasync
O_DSYNC
O_DIRECT
Permitted Values (>= 5.1.24)
Type (Unix)
string
Default
fsync
Valid Values
fsync
O_DSYNC
O_DIRECT