CSE Roadmap

Core tier · about 3 hours · 7 parts

Part 2 of 7 · 2 min read

The layers

Think of a letter that goes from your desk to a far city. It passes the mail room, the sorting centre, the truck and the local post office, and each stage does one job and hides the others. The analogy stops in one place: a letter moves one way, and an I/O request goes down the layers and the answer comes back up.

In plain words: The I/O software of the kernel is built in layers. At the top, a user program calls read or write. The virtual file system (VFS) picks the right file system. The file system turns a file offset into block numbers and looks in the page cache, which keeps recently used data in memory. On a miss, the block layer merges the requests and the I/O scheduler of page 2 orders them. A device driver then turns the request into the commands and the DMA set-up that the controller understands (page 4). When the transfer is done, an interrupt wakes the driver, and the data goes back up.

A worked example. A program calls read(fd, buf, 4096) on a file. The call enters the kernel (Level 0, page 2). The VFS finds the file system of fd. The file system maps the offset to block 8,000. The page cache does not hold it. The block layer builds a request for block 8,000 and queues it. The driver programs the controller and the DMA. The program sleeps. The controller finishes and interrupts. The driver marks the request complete, the page cache keeps the data, and the kernel copies 4,096 bytes to buf and wakes the program.

The key lines. Each layer talks only to the one below it, so one layer can be replaced without changing the others. A new file system needs no new disk driver, and a new disk driver needs no new file system.

I/O software layers

The I/O software layers of a kernel are the stack of modules between a system call and a device: the system call interface, the virtual file system, the file system with its cache, the block layer with the scheduler, the device driver and the interrupt handler. Each layer uses only the layer below it.

Example: a read on an ext4 file passes the VFS, the ext4 code, the page cache, the block layer and the driver of a virtual disk.

Check yourself: 1. Which layer decides whether the data is already in memory? 2. Which layer knows the commands of the controller? 3. Why does a new file system not need a new disk driver?