Part 13: Block Device Drivers

Block devices provide random-access storage (disks, SSDs, RAM disks). This part covers the modern blk-mq (multi-queue) interface.

Why Block Devices Are Different

Aspect Character Device Block Device
Access pattern Sequential stream Random access by sector
Data unit Byte Block (usually 512B or 4KB)
Caching None (usually) Page cache, writeback
Interface read/write syscalls Filesystem or direct I/O
Scheduling None I/O scheduler (mq-deadline, none, etc.)

Mental Model

flowchart TB
    subgraph UserSpace["User Space"]
        APP["Application<br/>read()/write()"]
    end

    subgraph Kernel["Kernel"]
        VFS["VFS Layer"]
        FS["Filesystem (ext4, xfs)"]
        CACHE["Page Cache"]
        BIO["Block I/O (bio)"]
        BLKMQ["blk-mq Layer"]
        SCHED["I/O Scheduler"]
    end

    subgraph Driver["Your Driver"]
        QUEUE["Request Queue"]
        OPS["blk_mq_ops"]
    end

    subgraph HW["Hardware"]
        DISK["Storage Device"]
    end

    APP --> VFS
    VFS --> FS
    FS --> CACHE
    CACHE --> BIO
    BIO --> BLKMQ
    BLKMQ --> SCHED
    SCHED --> QUEUE
    QUEUE --> OPS
    OPS --> DISK

    style UserSpace fill:#7a8f73,stroke:#2e7d32
    style Kernel fill:#738f99,stroke:#0277bd
    style Driver fill:#8f8a73,stroke:#f9a825
    style HW fill:#826563,stroke:#c62828

Key insight: The kernel batches and schedules I/O requests. Your driver receives struct request containing one or more sectors to read/write.

Key Concepts

gendisk

gendisk represents your disk device:

  • Capacity (in sectors)
  • Block size
  • Partitioning info
  • Link to request queue

blk-mq (Multi-Queue Block Layer)

Modern block layer with:

  • Per-CPU software queues (no lock contention)
  • Hardware queue mapping
  • Tag-based request tracking

bio vs request

  • bio: Single contiguous I/O operation from filesystem
  • request: Multiple bios merged by scheduler for efficiency

Your driver typically works with struct request.

Chapters

Chapter What You’ll Learn
Concepts gendisk, bio, request, and the I/O path
Driver Skeleton blk-mq driver structure and callbacks
Block Operations open/release, ioctl, disk geometry
Request Handling Request types, error handling, async completion

Example

  • RAM Disk - Simple RAM-backed block device

Prerequisites

  • Kernel module basics (Part 1-2)
  • Memory management (Part 5) - for buffer allocation

Further Reading


Table of contents


Back to top

Linux Driver Development Guide is a community resource for learning kernel driver development. Not affiliated with the Linux Foundation. Content provided for educational purposes.

This site uses Just the Docs, a documentation theme for Jekyll.