3

I'd like to know if there is a way to force a block device to bypass the linux buffer cache (ie direct IO) and pass the requests directly to the underlying layer. I know one can open a file using O_DIRECT flag to achieve this, but my program is not the user of the block device, a file system is. And so far, I have not had any luck telling various FSes to use direct IO.

In summary, I want the raw device functionality without using raw devices (since they're deprecated in my distro, Fedora).

If this is of any importance, my block device is a Network Block Device. Any help would be appreciated.

Mansour
  • 499
  • 2
  • 7
  • 14

2 Answers2

1

You can achieve this by mounting with the sync-option.

From the mount(8)-man page:

   sync   All I/O to the filesystem should be done synchronously. In  case
          of  media  with  limited number of write cycles (e.g. some flash
          drives) "sync" may cause life-cycle shortening.
Kvisle
  • 4,193
  • 24
  • 25
1

I don't think it's possible to have direct I/O without using either O_DIRECT or raw devices. Using sync option on mount won't bypass the buffer cache either.

Because you can't set it from the filesystem, you'd have to modify your program to either use O_DIRECT or mmap() and madvise() to get similar behaviour.

"I know one can open a file using O_DIRECT flag to achieve this, but my program is not the user of the block device, a file system is."

Don't worry about the your program is not the user, O_DIRECT works for open() which is file based anyway.

imel96
  • 406
  • 4
  • 9
  • What I meant by "my program is not the user of the block device, a file system is" was that no part of my program deals with the block device. My code sits behind the block device as the backend. It's basically a Network Block Device which connects to an NBD server I've written. By bypassing the buffer cache, I was hoping to avoid some deadlocking issues with NBD setups. – Mansour Nov 15 '11 at 12:04