Curriculum / Part 1 · Foundations: what storage even is
Lesson 4

The Linux I/O path

Follow one read() call from an application down to the device: VFS, page cache, filesystem, block layer, driver. Buffered vs direct I/O.

⏱ 40 min5-question quizLab 1.2

The simple version

Sending a letter goes through layers: you hand it to the front desk, the mailroom sorts it, a truck carries it, and a postman delivers it. Sometimes the front desk already has a copy of the letter you want and hands it straight back: that's the page cache.

That shortcut is wonderful for speed and terrible for honest benchmarks.

The layers

Application: read(), write() VFS (generic file layer) Page cache (RAM) Filesystem (XFS, ext4) Block layer + I/O scheduler NVMe driver Device O_DIRECT cache hit returnshere, no disk I/O
Buffered I/O goes through the page cache. O_DIRECT skips it (the dashed path) and goes to the device.

Buffered reads and writes

Reads: on a cache hit, data comes straight from RAM and the disk is never touched. On a miss, the kernel reads from the device, often with read-ahead, and keeps a copy.

Writes: buffered writes land in the page cache as dirty pages and return immediately. Background writeback flushes them later, controlled by vm.dirty_background_ratio and vm.dirty_ratio. Data isn't durable until it's flushed: that is what fsync() is for.

$ grep -E '^(Cached|Dirty|Writeback):' /proc/meminfo
$ sysctl vm.dirty_ratio vm.dirty_background_ratio

Direct I/O

Opening a file with O_DIRECT (fio: --direct=1) bypasses the page cache. Buffers and offsets must be aligned to the logical block size. Databases and many storage benchmarks use it so they measure the device, not RAM.

Benchmark rule. If a test file fits in RAM and you're not using direct I/O, you are probably measuring memory. Either use --direct=1, use a file much larger than RAM, or drop caches between runs: sync; echo 3 | sudo tee /proc/sys/vm/drop_caches (test machines only).

Watching the block layer

$ sudo blktrace -d /dev/nvme0n1 -o - | blkparse -i -

blktrace shows every request as it is queued (Q), dispatched to the driver (D) and completed (C). The time from D to C is device time; from Q to D is time spent waiting in the block layer.

Tools

/proc/meminfo
Cached, Dirty and Writeback show page-cache state.
blktrace + blkparse
Per-request trace of the block layer.
iostat -x
Device-level view: compare it with what the app thinks it's getting.
vmtouch / fincore
Show how much of a file is in the page cache (fincore ships with util-linux).

Quiz

Lab

Lab 1.2 · The impossibly fast benchmark

A report claims a single local NVMe drive read /scratch/bench.bin (8 GiB) at 18 GB/s. The drive is a PCIe Gen4 x4 class device (about 7 GB/s at most).

Start lab 1.2 →