Device Drivers & Kernel Modules

a block device

Imagine a wall of numbered lockers rather than a flowing tap: you can open locker number 5, then locker number 900, then locker number 12, in any order, and each locker holds the same fixed amount. A block device is the kernel's model of storage you address in fixed-size blocks that can be reached in any order — a hard disk, an SSD, a USB stick. Unlike a character device's strict byte stream, a block device supports random access: read block 1000, write block 7, jump anywhere.

A block driver does not expose a simple read/write handler the way a char driver does. Instead, the kernel's block layer collects, sorts, and merges I/O into a request queue, and hands the driver bio or request structures describing 'transfer these sectors to or from these memory pages.' The driver's job is to take a request off its queue, program the hardware to move those sectors (often via DMA), and signal completion when the transfer finishes. Sitting between the application and the driver are the page cache and an I/O scheduler, which is why writes can be buffered and reordered for efficiency, and why a write() may return before the data is truly on the platter.

Block devices matter because all persistent storage and the filesystems on top of it ride on this model. An important honesty: this field covers the block DRIVER — the bottom layer that talks to one piece of storage hardware. The large machinery above it (the page cache, the I/O scheduler, journaling, the filesystem) is a separate stack. Also, the 'block' in block device has nothing to do with blocking versus non-blocking calls; it refers to the fixed-size block as the unit of addressing.

// block layer hands the driver requests, not byte read()s: // request: read sectors 2048..2055 into pages P // driver: set up DMA, tell disk to move those sectors, // then on interrupt, blk_mq_end_request(rq, status)

A block driver consumes queued requests describing sector transfers, not per-byte reads; completion is signalled asynchronously.

'Block' here means fixed-size addressable unit, not 'a blocking call.' This field is the block DRIVER only; the page cache, I/O scheduler, and filesystem above it are a separate layer.

Also called
block device driver區塊型裝置