Module 10 · Lesson 86 of 112

The layered file system and VFS

Trace a file descriptor to disk, explain how VFS dispatches file operations, and map fixed-size file system blocks to virtio-blk sectors.

  • Beginner to advanced
  • 8 min read
  • 11 min lab
What you will need

Ubuntu 24.04 in WSL2 or a VM with Bash, stat, and dd. The exercises use ordinary files and shell arithmetic.

Your place in the module
By the end you can 4 objectives
  • Trace a disk-backed file read from the process descriptor table to storage.
  • Distinguish descriptor slots, open file state, and inode metadata.
  • Explain how Rust traits serve the role of C operation tables in the course kernel.
  • Convert file system block numbers to virtio-blk sector ranges.
Video coming soonThe layered file system and VFS
The video for this lesson is being recorded. Read the lesson below in the meantime.
Recall first from Sending and completing block requests
  • The available ring carries a descriptor head index, and QueueNotify identifies the queue.
  • The used-ring entry identifies the completed chain, and the trailing status byte reports the result.
  • The data descriptor is device-writable for reads and device-readable for writes.
01

From a file descriptor to disk

4 min read

The last lesson read raw sectors from virtio-blk. Now you organize those sectors into files, mapping fd to file to inode to block device, with a trait-based VFS and a BlockDevice trait.

On Ubuntu, open the same regular file twice and a read through the first descriptor leaves the second descriptor's position where it was. Duplicate the first descriptor, however, and reading through the duplicate also advances the position seen through the first. All three descriptors refer to the same underlying file. Where does the kernel keep the position so that one pair shares it and the other opens stay independent? The answer starts with the distinction Linux makes between a descriptor and an open file description. open(2) and dup(2).

From descriptor to diskProcess fd tableDescriptor selects an open fileOpen fileCurrent byte offset and status flagsInodeMetadata and access to dataBlock devicevirtio-blk transfers storage bytesDiskQEMU disk image backingDevice I/O occurs when storage must be accessedProcess fd tableDescriptor selects an openfileOpen fileCurrent byte offset andstatus flagsInodeMetadata and access to dataBlock devicevirtio-blk transfers storagebytesDiskQEMU disk image backingDevice I/O occurs when storagemust be accessed
The layers used to read a regular file backed by a virtio-blk disk

A identifies a slot in the calling process's table. Two processes can both use descriptor 3 and have those slots refer to entirely different objects. When the kernel receives a read request, it uses the caller's table to find the open file. An invalid descriptor can be rejected at this point, before a file system or device has any work to do.

The slot refers to an . Linux represents it with struct file. Its state includes the current file offset and file status flags. For an ordinary read on a seekable file, the offset is measured in bytes, and a successful read advances it by the number of bytes returned. Each independent open creates a new open file description. A duplicated descriptor adds another slot referring to the existing description. That is why a read through the duplicate advances the shared offset. read(2).

The open file refers to an . For a regular file, that metadata includes the file length, ownership, permissions, and timestamps. The inode also gives the file system access to the information it needs to find the file's contents. The representation of that information depends on the file system format. You can think of the inode layer as metadata plus access to data. The data bytes themselves can occupy storage blocks elsewhere. Two separate opens of the same existing file can share the inode while keeping independent positions. Overview of the Linux Virtual File System.

Once the file system knows where the requested bytes are stored, it can request the relevant blocks. In the course kernel, virtio-blk provides the block device and QEMU can back it with a disk image. The device handles storage addresses and transfers bytes. The file system supplies the meaning of those bytes. A guest file name and its permissions come from the file system format, so the virtio-blk driver only needs the storage address, the transfer buffer, and the direction of the operation.

This sequence describes how a disk-backed read can reach storage. Linux can satisfy an ordinary buffered read from data already in memory. A read at end of file can return zero bytes without fetching file data. The descriptor and open file still have the same roles in both cases. To work out whether a read needs device I/O, first determine which file and byte range it requests, then ask whether those bytes need to be fetched. read(2).

02

What the VFS abstracts

3 min read

Linux can read a file on ext4 and a file on tmpfs through the same read interface. Ext4 can store file contents on a block device, while tmpfs does not require a disk-backed file system layout. The kernel caller still needs a consistent way to ask for bytes and receive a count or an error. The defines that interface. A file system can participate by supplying the operations appropriate to its objects. Overview of the Linux Virtual File System.

File operation dispatchKernel readcodeUses theopen fileCommon VFSoperationSelects theassociatedimplementationFile systemimplementationObtains therequestedbytesKernel readcodeUses theopen fileCommon VFSoperationSelects theassociatedimplementationFile systemimplementationObtains therequestedbytes
A common read operation dispatches to the implementation associated with the open file

In Linux's C implementation, an operation table such as struct file_operations is a struct with function pointers. An open file has an associated set of file operations. When the kernel performs an operation on it, the appropriate function comes from that set. The common interface means the caller can work with the open file it was given. The file system provides the code that understands how that particular file stores its contents.

The course kernel expresses the same design using Rust traits. A trait gives the operations names and types, and each file system supplies implementations for the methods it supports. When kernel code uses a reference to a trait object, a method call selects the implementation associated with that object at run time. In this design, the trait and trait object take the place of a manually defined C struct of function pointers. Linux on Ubuntu uses its C operation tables, and the Rust traits are the corresponding design for the kernel you are building in this module.

There are two interfaces to keep distinct. The VFS interface lets a caller work with files without knowing their file system format. The BlockDevice interface lets a disk-backed file system request storage without knowing how the driver communicates with the device. A memory-backed file system can implement the VFS operations without using BlockDevice. A block device can transfer bytes even before the kernel has a file system that can interpret them. Each interface describes a different operation.

Consider a request for bytes near the middle of a disk-backed regular file. The open file supplies the current position. The VFS dispatches the request to the implementation associated with that file. That implementation uses the inode and its own format to determine which stored bytes correspond to the request. By the time it calls the block device, the request is expressed in storage units. The next question is how those units correspond to the addresses the virtio-blk driver accepts.

03

From file system blocks to virtio-blk sectors

3 min read

A file offset is a byte position within one file. A file system block number identifies a fixed-size region of the file system's storage. A virtio-blk sector number identifies a position on the virtual disk in 512-byte units. These numbers can all have the same value and still refer to different locations. For example, a byte offset within a file first needs the file system's mapping to a storage block. Multiplying the byte offset directly by a sectors-per-block factor would skip that mapping.

In the course design, choose a fixed size of 512 bytes or 4 KiB. A 512-byte block covers one virtio-blk sector. A 4 KiB block contains 4096 bytes, so it covers eight consecutive sectors. The 512-byte unit is how the virtio-blk request address is expressed: the sector field of a request counts 512-byte sectors, and the device capacity is also reported in 512-byte sectors. If the device offers the optional blk_size feature, that field tells the driver the optimal block size, but the specification states that it does not change the units used in the protocol, which stay 512 bytes. A file system block size is therefore a multiple of 512 bytes, and choosing one that matches blk_size avoids extra work in the device. Virtio specification, block device.

4 KiB block to sector mappingBlock 001234567Block 189101112131415Each cell is one 512-byte sector address unit.
A 4 KiB block occupies eight consecutive 512-byte sector address units

For a file system starting at device sector zero, divide the block size by 512 to get the number of sectors per block. Multiply the block number by that count to get the starting sector. With 4 KiB blocks, block 0 covers sectors 0 through 7, and block 1 covers sectors 8 through 15. These ranges follow from the chosen sizes. If the file system starts later on the device, add its starting sector to the result. The transfer still spans the same number of sectors. A transfer spanning eight sector address units can be issued as one request when the device supports it.

For the course's BlockDevice trait, read_block takes a block number and fills a destination buffer with one whole block. write_block takes a block number and transfers one whole block from a source buffer. The file system and the adapter agree on the block size, so the buffer for a 4 KiB block holds 4096 bytes. The adapter translates the address, passes the operation to the virtio-blk driver, and reports failure if the transfer fails. The file system can then handle the error instead of treating a failed transfer as successful.

A file read can ask for fewer bytes than a block. If those bytes are inside one stored block and the data must be fetched, the file system can request the whole block into a buffer and copy the requested range out of it. The byte count returned to the program describes that requested range, while the block transfer size describes the storage operation. This difference is why both interfaces are useful even when a single file read results in a single device request.

When tracing a read, ask what each layer knows. The descriptor table knows which open file to use. The open file knows the current position. The file system knows how that inode's byte range maps to storage. The device adapter knows how to express a storage block as sectors. The next lesson, The VFS traits, turns the file system side of that sequence into Rust interfaces and asks which operations belong on each object.

Key takeaways

  • A descriptor selects an open file in the process descriptor table, and the open file holds the current offset.
  • An inode represents a file and its metadata, while the file system uses that inode to locate the file's data.
  • VFS gives kernel callers a common interface across file system implementations.
  • Virtio-blk request sector numbers use 512-byte units, so a 4 KiB file system block spans eight sectors.
Reflect
  1. Explain to a colleague, in two sentences: trace a disk-backed file read from the process descriptor table to storage.
  2. Which objective are you least sure about? Re-read that section, then do its exercise in the practice pack.
  3. What surprised you when you ran the commands yourself?
On a real board

Repeat the key commands on your Raspberry Pi or lab board over SSH or the serial console. Note anything that behaves differently from your PC, such as missing tools, a BusyBox shell or a read-only filesystem.

References and further reading

Find your next step

↑ ↓ to move · Enter to open · Esc to close