I'm confused with fsync
+ direct IO
.
It's easy to understand the code like this:
fd = open(filename, O_RDWR, 00644);
write(fd, data, size);
fsync(fd);
In this case, write()
will write the data to the page cache, and fsync
will force all modified data in the page cache referred to by the fd
to the disk device.
But if we open a file with O_DIRECT
flag, like this,
fd = open(filename, O_RDWR|O_DIRECT, 00644);
write(fd, data, size);
fsync(fd);
In this case, write()
will bypass the page cache, the write directly to disk device. So what will the fsync
do, there is no dirty page in the page cache referred to by the fd
.
And if we open a raw device, what will fsync
do,
fd = open('/dev/sda', O_RDWR|O_DIRECT, 00644);
write(fd, data, size);
fsync(fd);
In this case, we open a raw device with O_DIRECT
, there is no filesystem on this device. What will sync
do here ?