Linux – How to directly read data from a file in Linux without using the filesystem cache

cachefilesystemslinuxlinux-kernelzfsonlinux

I have shared storage which is mounted (read-only) on SERVER1, and mounted (read-write) on SERVER2.

SERVER2 is writing data to a file.
SERVER1 is reading data from that file.

The file poling is <100ms, which is fast, but lets just say it's necessary.

SERVER1 is accessing the filesystem cache, and not showing new data in the file. I can drop_caches which then returns the latest data in that file, but this process drops all file caches on SERVER1, which is not what I want to do, and it takes too long to process.

I have tried using the cat command, as well as dd if=/path/to/file -iflag=direct, but that doesn't work either. It returns an error. The flag seems not to be supported for files. Any idea on how to read a file directly from a disk, or how to disable filesystem cache for a file/directory/mount point?

I would also consider disabling file cache system-wide, but thats not my first preference, this is just for the one file. Google doesn't return a way to do this either, as most forum posts argue against such action.

Thanks!

Best Answer

Do not do this. I repeat: Do not do this!

You will get cluster headaches out of this. If metadata changes in meantime, it could cause crashes and/or invalid data returned. These filesystems are not designed to work like this. Do not use them that way. This is especially bad idea with ZFS. If you really have to, use something very very basic, without journals and advanced features (like ext2 or maybe vfat).

Either use clustered filesystem (gfs, ocfs, cxfs), or some networked filesystem (glusterfs,cifs,nfs).

And just by the way. DirectIO is not implemented in ZFS On Linux. It will work on base filesystems (ext{2,3,4}, xfs, etc.)