Free — no signup required

The Block Layer and the `bio` Struct

2 min read

From Files to Sectors

The filesystem understands files, directories, and inodes. The physical disk understands only one thing: numbered sectors (or Logical Block Addresses, LBAs). The Block Layer is the translation engine between these two worlds.

When the filesystem needs to write data, it tells the Block Layer: "Write these memory pages to these disk blocks." The Block Layer packages this request into a kernel data structure called a bio (Block I/O). The bio struct is the fundamental unit of I/O in the Linux kernel. It contains:

  • bi_bdev: Which block device to target.
  • bi_iter.bi_sector: The starting LBA (sector address) on the device.
  • bi_opf: The operation flags — read, write, flush, discard.
  • bi_io_vec: A list of memory page segments containing the data (a scatter-gather list).

Multiple bio structs can be chained together for large transfers. The Block Layer then hands these to the I/O scheduler for optimization before passing them to the device driver.

Merging: The First Optimization

Before scheduling, the Block Layer performs request merging. If three separate 4 KB writes arrive for consecutive sectors (LBA 100, LBA 101, LBA 102), the Block Layer merges them into a single 12 KB request. This reduces the number of commands sent to the hardware, which reduces overhead — especially on HDDs where each command has a fixed setup cost.

Before merging:          After merging:
bio(LBA=100, 4KB)  ─┐
bio(LBA=101, 4KB)  ─┤──► bio(LBA=100, 12KB)
bio(LBA=102, 4KB)  ─┘

Key insight: The Block Layer also handles plugging. When a process starts issuing I/O, the kernel "plugs" the queue — holding requests briefly to accumulate more, then "unplugs" to flush them all at once. This is analogous to letting a bathtub fill before draining rather than draining each drop individually.

This is one of 18 chapters

Every chapter is free — Kubernetes, Terraform, SRE, distributed systems, and more — with fast daily review built in.

Sign up free