0

When interacting with block devices that have been mounted on the file system, the IO is buffered so reads and writes may not actually reach out to the backing hardware until some time in the future. I believe the same is also true for block devices that have been mmap'd as they map now exists in the filesystem (?) but as soon as the map & device are closed the buffer is emptied as it stops existing in the filesystem.

What about when accessing a block device without mounting it or mmaping it? I have a SPI memory block device and I use the dd command to write some data to it, and then a while later I will use the dd command to read that data back. Is any of that data buffered or is it all being written and read from the device directly?

I believe it's not buffered because when I perform a tiny write the operation hangs for a moment, if it was buffered I'd expect it to be instant since it doesn't have to communicate with the device, but I can't find any concrete info on if that's true or not.

  • See https://unix.stackexchange.com/a/60147/61098 **Data from block device can be cached in memory and read back from cache; writes can be buffered** – Barmar Oct 07 '22 at 15:41
  • Whether a particular device is buffered is up to the device driver, it's not a general character vs. block distinction. You can also use the `O_DIRECT` and `O_SYNC` open options to give hints to the device driver. – Barmar Oct 07 '22 at 15:47
  • @Barmar looking through the dd source code, it looks like it uses O_DIRECT when communicating with the device and there are a lot of comments that imply dd is doing whatever it can to ensure stable storage is hit and no caching takes place – dazed_and_confused Oct 07 '22 at 16:35
  • I didn't notice that you said you were using `dd`. It was designed as a more low-level tool, which is why it uses that option. – Barmar Oct 07 '22 at 16:36
  • @dazed_and_confused dd only uses O_DIRECT if you give it the `direct` flag argument, otherwise it goes through block cache. Read the man page. – stark Oct 07 '22 at 19:27

0 Answers0