I know you can use regular I/O functions for block devices (/dev/sda etc), but can you just read some data of size n, or does it have to be divisible by 512? Is there any overheard to reading in smaller sizes? Some devices have blocks bigger than 512 bytes, if there is overhead for smaller sizes, how can I know the optimal block size?
-
see: https://stackoverflow.com/q/584142/1216776 – stark Jan 20 '22 at 13:50
1 Answers
According to Wikipedia and for Unix and Unix-like systems (hence Linux):
Block special files or block devices provide buffered access to hardware devices, and provide some abstraction from their specifics. Unlike character devices, block devices will always allow the programmer to read or write a block of any size (including single characters/bytes) and any alignment. The downside is that because block devices are buffered, the programmer does not know how long it will take before written data is passed from the kernel's buffers to the actual device, or indeed in what order two separate writes will arrive at the physical device...
That means that you can use use any size for a read. Because of the in driver buffering, the physical reads will always read physical sectors.

- 143,923
- 11
- 122
- 252
-
1So making a few smaller reads on the device will result in one actual block read? – circl Jan 20 '22 at 14:24
-
@circl: AFAIK yes. But it will still have a cost because you will have a context change user/kernel per read – Serge Ballesta Jan 20 '22 at 14:26
-
The Wikipedia article does not say for which OSs this is valid: Windows XP (I'm not sure about Windows 10) for example also has block devices but it only supports writing entire sectors. And Solaris 10 (once again I don't know about the latest version) does not allow reading or writing from block files at all! – Martin Rosenau Jan 20 '22 at 15:12
-
@MartinRosenau: as the question is tagged as linux, I assumed that OP was only concerned by Unix block devices. And the extract from the Wikipedia page is about *Unix and Unix like systems*. But I should have make it explicit. Post edited, and thank you for the comment. BTW not only Solaris but also BSD systems like FreeBSD have no *block devices*. – Serge Ballesta Jan 20 '22 at 15:19
-
1Well, Solaris has block devices but they can be used for the "mount" operation only (not for reading or writing). – Martin Rosenau Jan 20 '22 at 15:52
-
Just in case it's not obvious: when you write less than 512 bytes, the driver has to read the 512 byte block, merge in the data you intend to write, and then write out the 512 byte block. – Mike Andrews Jan 20 '22 at 18:28
-
@SergeBallesta I fired up the FreeBSD VM on copy.sh/v86 and had no problem reading from the block devices – circl Feb 01 '22 at 12:02
-
@circl: You could certainly read from the disk device, but it is a character device (type `c` and not `b`) – Serge Ballesta Feb 01 '22 at 12:12