Understanding the Block Layer, Block Devices, and Data Structures
local graph
Chapter 4 — Understanding the Block Layer, Block Devices, and Data Structures
Section titled “Chapter 4 — Understanding the Block Layer, Block Devices, and Data Structures”The first three chapters of this book were centered around the first component of the kernel’s I/O hierarchy, which is the VFS layer. We explained the functions and purpose of VFS, as well as how it serves as an intermediary layer between the generic system call interface and filesystems, along with its primary data structures. In addition, we discussed the filesystems that can be found under the VFS layer and introduced some of the essential concepts associated with them.
We’ll now turn our focus to the second major section in the kernel’s storage hierarchy: the block layer. The block layer deals with block devices and is responsible for handling I/O operations performed on block devices. All the user-space programs use the block layer interfaces to address and access the underlying storage devices. Over the last decade or so, physical storage media has undergone a significant transformation, shifting from slower mechanical drives to faster flash drives. Consequently, the block layer within the kernel has undergone substantial modifications. As performance is a critical factor when it comes to storage hardware, several enhancements have been made to the kernel code to enable disk drives to realize their full potential. In this chapter, we’re going to introduce the block layer, define block devices, and then dive into the major data structures in the block layer.
Here’s a summary of what follows:
- Explaining the role of the block layer
- Defining block devices
- The defining characteristics of a block device
- Representation of block devices
- Looking at the major data structures in the block layer
- The journey of an I/O request in the block layer
Technical requirements
Section titled “Technical requirements”The Linux kernel’s block layer is a slightly complex topic. A good understanding of the material presented in the first three chapters will help you comprehend the interaction between the block layer and various filesystems. Having experience with the C programming language will help you understand the code presented in this chapter. Additionally, any practical experience with a Linux system will enhance your understanding of the concepts discussed herein.
If you want to download the kernel source, you can download it from https://www.kernel.org. The code segments referred to in this chapter and book are from kernel 5.19.9.
Explaining the role of the block layer
Section titled “Explaining the role of the block layer”The block layer is tasked with implementing the kernel interfaces that enable filesystems to interact with storage devices. In the context of accessing physical storage, applications use block devices, and any requests to access data on these devices are managed by the block layer. The kernel also contains a mapping layer just above the block layer. This layer provides a flexible and powerful way to map one block device to another, allowing for operations such as creating snapshots, encrypting data, and creating logical volumes that span multiple physical devices. The interfaces that are implemented in the block layer are central to managing physical storage in Linux. The device files for block devices are created in the / dev directory.
Like VFS, abstraction is the core function of the block layer. The VFS layer allows applications to make generic requests for interacting with files without having to worry about the underlying filesystem. In a similar vein, the block layer allows applications to access storage devices uniformly. The choice of backend storage medium is not a point of concern for the application.
To highlight the major functions of the block layer, let’s build on the storage hierarchy we defined when describing VFS. The following figure outlines the major components of the block layer:

Figure 4.1 – I/O hierarchy from VFS to the block layer
Let’s take a brief look at these functions:
- The block layer provides an upstream interface to filesystems and allows them to access a diverse range of storage devices uniformly. Likewise, it implements a downstream interface for drivers and storage devices by providing them with a single point of entry from all applications.
- As we’ll see in this chapter, the block layer incorporates several tricky structures to provide its services generically. Probably the most important of them is the
biostructure. The filesystem layer creates a bio structure to represent the I/O request and passes it down to the block layer. The bio structure is responsible for transporting all I/O requests to the driver. The mapping layer is in charge of providing an infrastructure to map physical block devices to logical devices. The mapping layer can achieve this by using the device mapper framework in the kernel. The device mapper lays the groundwork for several technologies in the kernel. These include volume management, multipathing, thin provisioning, encryption, and software RAID. The most well known of these is logical volume management (LVM). The device mapper creates every logical volume as a mapped device. LVM provides a great deal of flexibility to storage administrators and simplifies storage management. - The
blk-mqframework has become an important part of the block layer as it solved its performance limitations by isolating request queues for every CPU core. This framework is responsible for shepherding block I/O requests to multiple dispatch queues. We’ll cover theblk-mqframework in more detail in Chapter 5. - The block layer also includes several schedulers for handling I/O requests. These schedulers are pluggable and can be set for individual block devices. Non-multi-queue schedulers have been deprecated and are no longer supported in modern kernels. As we’ll see in Chapter 6, these schedulers make use of several techniques to make intelligent decisions regarding I/O scheduling.
- Additionally, the block layer implements functions such as error handling and collecting I/O statistics for block devices.
At the heart of the block layer are block devices. Apart from devices that stream data, such as tape drives, most storage devices, such as mechanical drives, and solid-state drives flashcards, are considered block-based devices. Let’s take a look at the defining traits of a block device and how they’re represented in Linux.
Defining block devices
Section titled “Defining block devices”There are two major ways that the kernel exchanges data with external devices. One method is to exchange one character at a time with the device. Devices addressed through such methods are known as character devices. Character devices are addressed using a stream of sequential data. They can be accessed by programs to perform input and output operations one character at a time. Due to the absence of random access methods, managing character devices is simpler for the kernel. Devices such as keyboards, text-based consoles, and serial ports are all examples of character devices.
Communicating through one character at a time is acceptable when the volume of data is low, such as when using serial ports or keyboards. A keyboard can only accept one character at a time, so the use of a character interface makes sense. But this approach becomes untenable when transferring large volumes of data. When writing to physical disks, we expect them to be able to address more than one character at a time and allow for data to be accessed randomly. The kernel addresses physical drives in fixed-size chunks, known as blocks. In addition to traditional disks, devices such as CD-ROMs and flash drives also use this approach. Such devices are known as block devices. Block devices are more complex to manage compared to character devices and require more careful considerations from the kernel. The kernel has to make critical decisions regarding the addressing and organization of block devices as these can significantly impact not only the block device but also the overall system performance.
Block devices can exist in memory. This can be achieved by creating a ramdisk. One of the most notable use cases of ramdisks is during the boot-up sequence in Linux systems. An initial ramdisk (initrd) is responsible for loading a temporary root filesystem in memory to aid in the boot process. A filesystem can be created on a ramdisk and mounted like any regular filesystem. The speed of RAM makes ramdisks incredibly fast. But due to the volatile nature of RAM, any data written to a ramdisk is maintained only until the device is powered on.
Although ramdisks are also block-based, they’re rarely used. As you’ll see, throughout this book, a block device is typically regarded as a persistent data storage medium with a filesystem layer on top.
All operations on block devices are performed by the kernel in fixed-size chunks of N bytes, known as blocks, which is the currency of exchange when dealing with block devices. The actual value of N varies across the stack since different layers in the kernel’s I/O hierarchy use chunks of different sizes to address block devices. Because of this, the term block is defined in several ways, depending on its presence in the stack:
- User-space applications: As applications interact with the kernel space through standard system calls, the term block in this context refers to the amount of data read from and written to via system calls. Depending on the application, this can vary in size.
- Page cache: The kernel makes extensive use of the VFS page cache to improve the performance of read and write operations. Here, the fundamental unit of data transfer is a page, which is 4 KB in size.
- Disk-based filesystems: As explained in Chapter 3, a block represents the fixed number of bytes in which I/O operations are performed by the filesystem. Although filesystems allow for higher block sizes, often up to 64 KB, the block size for a filesystem is usually between 512 bytes and 4 KB because of the page size.
- Physical storage: On physical disks, the smallest addressable unit is known as a sector, which is usually 512 bytes. This sector is often further classified as logical or physical.
We discussed filesystem blocks in Chapter 3. Don’t get confused; the filesystem block’s size is not the fundamental unit of block I/O. The basic unit of block I/O is a sector. The data structures in the block layer define a variable of the sector_t type in the kernel code that represents an offset or size that is a multiple of 512. The sector_t variable is defined as an unsigned integer type that is large enough to represent the maximum number of sectors that can be addressed by the block device. It is used extensively throughout the block layer in structures such as “bio” to represent disk addresses and offsets.
To summarize, devices that are organized and addressed in terms of blocks are known as block devices. They allow for random access and offer superior performance compared to character devices. To take full advantage of block devices, the kernel has to make informed decisions regarding their addressing and organization.
Let’s briefly go over some key features that define a block device.
The defining characteristics of a block device
Section titled “The defining characteristics of a block device”As discussed earlier, block devices allow far more advanced ways to handle I/O requests. Some of the defining characteristics of block devices are as follows:
- Random access: Block devices allow for random access. This means that the device can seek from one position to another.
- Block size: Block devices address and transfer data in fixed-sized blocks.
- Stackability: Block devices can be stacked through the use of the device mapper framework. This extends the basic functionalities of physical disks and allows for scaling logical volumes.
- Buffered I/O: Block devices use buffered I/O, which means that data is written to a buffer in memory before being written to the device. Read and write operations on block devices make extensive use of the page cache. Data that’s read from the block device is loaded and kept in memory for a certain period. Similarly, any data to be written to a block device is written to the cache first.
- Filesystems/partitioning: Block devices can be partitioned into smaller logical units, with separate filesystems created on top of them.
- Request queues: Block devices implement the concept of request queues, which are responsible for managing the I/O requests that are submitted to a block device.
Let’s see how block devices are represented in Linux.
Looking at the representation of block devices
Section titled “Looking at the representation of block devices”When discussing VFS, we saw that abstractions are at the heart of the kernel’s I/O stack. The block layer is no exception to this rule. Regardless of the differences in the physical make and model, the kernel should be able to work with storage devices uniformly. To implement a standard interface for all devices, the operations should be independent of the properties of the underlying storage device.
As explained in Chapter 1, almost everything is represented in the form of a file, including hardware devices. A block device is a special file and is named as such because the kernel interacts with it using a fixed number of bytes. Depending on the nature of the devices, the files representing them are created and stored at specific locations in the system. The block devices in the system are present in the /dev directory. Filenames representing disk drives in the system start with sd, followed by a letter representing the order of discovery. The first drive is named sda and so on. Similarly, the first partition on the sda drive is represented as sda1. If we look at the sd* devices in /dev, notice that the file type is b, for block devices. You can also list block devices using the lsblk command, as shown in the following figure:

Figure 4.2 – Major and minor numbers
Just before the modification timestamp, note the two numbers separated by a comma. The kernel represents block devices as a pair of numbers. These numbers are called major and minor numbers for a device. The major number identifies the driver associated with the device, whereas the minor number is used for differentiating between individual devices.
In the preceding figure, all three devices – sda, sda1, and sda2 – use the same driver and hence have the same major number, 8. The minor numbers – 0, 1, and 2 – are used to identify the driver instance for each device.

Figure 4.3 – Device major and minor numbers
The device files present in the /dev directory are hooked up to the corresponding device drivers to establish a communication link with the actual hardware. When a program interacts with the block device file, the kernel uses the major number to identify the appropriate driver for that device and sends the request. Since a driver can be responsible for handling multiple devices, there has to be a way through which the kernel can distinguish devices using the same major number. For this purpose, minor numbers are used.
We will now explore the primary data structures that are used in the block layer.
Looking at data structures in the block layer
Section titled “Looking at data structures in the block layer”Dealing with block devices is fairly complex as the kernel has to implement features such as queue management, scheduling, and the ability to access data randomly. The speed of block devices is much higher than character devices. This makes block devices extremely performance-sensitive and the kernel has to make intelligent decisions to extract their maximum performance. Thus, it makes sense to handle the two devices differently. Because of this, there is an entire kernel subsystem dedicated to managing block devices. All this makes the block layer the most sophisticated piece of code in the Linux kernel.
Throughout this book, we’ve referred to the relevant pieces of kernel code so that you can familiarize yourself with the implementations of certain concepts. If you’re interested in pursuing kernel development, this may seem like a good starting point. However, if you are more focused on theoretical understanding, the use of code might be mildly confusing. But it is essential to get a basic idea of how certain things are represented in the kernel. Talking specifically about the block layer, it is not possible to discuss all the structures that make up its intricate design. Nevertheless, we must highlight some more important constructs that give us a fair understanding of the representation and organization of block devices in the kernel.
Some of the major data structures that are used to work with block devices are as follows:
register_blkdevblock_devicegendiskbuffer_headbiobio_vecrequestrequest_queue
Let’s take a look at them one by one.
The register_blkdev function (block device registration)
Section titled “The register_blkdev function (block device registration)”To make block devices available for use, they must first be registered with the kernel. The registration process is performed by the register_blkdev() function, which is defined in include/linux/blkdev.h:
int __register_blkdev(unsigned int major, const char *name, void (*probe)(dev_t devt))The register_blkdev function is used by block device drivers to register themselves and is a macro that’s directed to __register_blkdev. The __register_blkdev function performs the actual registration process. The purpose of having a separate internal function is to provide additional error handling and validation before modifying the kernel’s data structures.
The registration function performs the following tasks:
- It requests a major number from the kernel’s dynamic major number allocation pool. The major number uniquely identifies the block device driver within the system.
- Once a major number has been successfully obtained, the function creates a
block_devicestruct, which represents the block device driver. This struct contains information such as the major number, the name of the driver, and function pointers to various driver operations.
In summary, the register_blkdev function acts as a friendly interface through which block device drivers can initiate their registration processes with the kernel’s block layer. It handles the necessary steps for acquiring a major number, creating a block_device struct, and establishing the necessary connections with the block layer.
The block_device structure (representing block devices)
Section titled “The block_device structure (representing block devices)”The block device is defined in include/linux/blk_types.h by the block_device structure in the kernel:
struct block_device { sector_t bd_start_sect; sector_t bd_nr_sectors; struct disk_stats __percpu *bd_stats; unsigned long bd_stamp; bool bd_read_only; dev_t bd_dev; atomic_t bd_openers; struct inode * bd_inode;[……..]The block_device structure instance is created when the device file is opened. A block device can be a whole disk or a single partition and the block_device structure can represent either. When using partitions, the individual partitions are identified through the bd_partno field. Since access to block devices happens through the VFS layer, the corresponding device files are also assigned an inode number. The inodes for block devices are virtual and are stored in the bdev virtual filesystem. The inode for a block device also contains information about its major and minor numbers.
The block_device structure also provides information about the device, such as its name, size, and block size. It also contains a pointer to the gendisk structure, which represents the disk, and a list of request_queues structures for handling I/O requests.
The gendisk structure (representing physical disks)
Section titled “The gendisk structure (representing physical disks)”An important field in the definition of the block_device structure is the bd_disk pointer, which points to the gendisk structure. The gendisk structure, which is defined in include/linux/blkdev.h, represents information about the disk and is used to implement the notion of a physical hard disk in the kernel.
The gendisk structure represents the disk’s properties and the methods used to access it. It is used to register a block device and its associated I/O operations with the kernel, allowing it to communicate with the device:
struct gendisk { int major; int first_minor; int minors; char disk_name[DISK_NAME_LEN]; unsigned short events; unsigned short event_flags;[……]gendisk can be considered a link between the block and filesystem interfaces mentioned previously and the hardware interface. There will be a block_device structure for representing the entire physical disk defined in gendisk. Similarly, there will be separate block_device structures that describe individual partitions within gendisk. Note that gendisk is allocated and controlled by the block device driver and registered with the kernel using the register_blkdev function. Once registered, the block device driver can use the gendisk structure to perform I/O operations on the device.
Let’s look at some of the important fields of this structure:
major: This field specifies the major number associated with thegendiskstructure. As discussed earlier, the major number is used by the kernel to identify the driver responsible for handling the block device.first_minor: This field refers to the smallest minor number that is allocated to a given block device. This can be thought of as an offset from which the minor numbers for the different partitions of the device are allocated.minors: This field specifies the total number of minor numbers associated with thegendiskstructure.fops: This field points to a structure of file operations that are associated with thegendiskstructure. These file operations are used by the kernel to handle read, write, and other file operations on the device.private_data: This field is used by the driver to store any private data associated with thegendiskstructure, such as any driver-specific information.queue: This field points to the request queue associated with thegendiskstructure. The request queue is responsible for managing the I/O requests that are issued to the device. This makes it a very important field as it enables the kernel to associate a specific I/O queue with each block device. By having separate I/O queues for each block device, the kernel can manage multiple block devices independently and handle their I/O operations more efficiently. This allows the kernel to optimize performance, apply appropriate scheduling policies, and prevent I/O bottlenecks.disk_name: This field is a string that specifies the name of the device. The name is used by the kernel to identify the device and is usually displayed in system logs.
Let us move on to the next structure.
The buffer_head structure (representing blocks in memory)
Section titled “The buffer_head structure (representing blocks in memory)”One of the defining features of a block device is its extensive use of the page cache. The read and write operations on a block device are performed in the cache. When an application reads from a block for the first time, the block is fetched from the physical disk into memory. Similarly, when a program wants to write some data, the write operation is first performed in the cache. It is written to the physical disk at a later stage.
The block that’s read from the disk or to be written to the disk is stored in a buffer. This buffer is represented by the buffer_head structure, which is defined in include/linux/buffer_head.h in the kernel. We can say that this buffer is an in-memory representation of an individual block:
struct buffer_head { unsigned long b_state; struct buffer_head *b_this_page; struct page *b_page; sector_t b_blocknr; size_t b_size;[………...]The fields in the buffer_head structure contain the information required to uniquely identify a particular block in the block device. The fields in the buffer_head structure are described as follows:
b_data: This field points to the start of the data buffer associated withbuffer_head. The size of the buffer is determined by the block size of the filesystem.b_size: This field specifies the size of the buffer in bytes.b_page: This is a pointer to the page in memory where the block is stored. This field is typically used in conjunction with fields such asb_dataandb_size, to manipulate the buffer data.b_blocknr: This field specifies the logical block number of the buffer in the filesystem. Each block in the filesystem is assigned a unique number called a logical block number. This number represents the order of the block in the filesystem, starting from 0 for the first block.b_state: This field is a bitfield that represents the state of the buffer. It can have several values. For instance, a value ofBH_Uptodateindicates that the buffer contains up-to-date data, while a value ofBH_Dirtyindicates that the buffer contains dirty (modified) data and needs to be written to disk.b_count: This field keeps track of the number of users ofbuffer_head.b_page: This field points to the page in the page cache that contains the data associated with thebuffer_headstructure.b_assoc_map: This field is used by some filesystems to track which blocks are currently associated withbuffer_head.b_private: This field is a pointer to private data associated withbuffer_head. This can be used by the filesystem to store information related to the buffer.b_bdev: This field is a pointer to the block device that the buffer belongs to.b_end_io: This field is a function pointer that specifies the completion function for an I/O operation on a buffer, and is used to perform any necessary cleanup operations.
By default, as the filesystem’s block size is equal to the page size, a single page in memory can hold a single block. If the block size is less than the page size, the page can hold more than one block.
The buffer head maintains a mapping between a page in memory and its corresponding on-disk version. Although it still holds important information, it was an even more integral component of the kernel before version 2.6. Back then, in addition to maintaining page-to-disk block mapping, it also served as a container for all I/O operations in the block layer. The use of buffer heads as an I/O container resulted in a significant amount of memory usage. When dealing with a large block of I/O requests, the kernel had to break it into smaller requests, each of which, in turn, had a buffer_head structure associated with them.
The bio structure (representing active block I/Os)
Section titled “The bio structure (representing active block I/Os)”Due to the limitations in the buffer_head structure, the bio structure was created to represent an ongoing block I/O operation. The bio structure has been the fundamental unit of an I/O in the block layer since kernel 2.5. When an application issues an I/O request, the underlying filesystem translates it into one or more bio structures, which are sent down to the block layer. The block layer then uses these bio structures to issue I/O requests to the underlying block device. The bio structure is defined in include/linux/blk_types.h:
struct bio { struct bio *bi_next; struct block_device *bi_bdev; unsigned int bi_opf;…… unsigned short bi_max_vecs; atomic_t __bi_cnt; struct bio_vec *bi_io_vec;[……….]Some particularly interesting fields are as follows:
bi_next: This is a pointer to the nextbiostructure in a list and is used to link multiplebiostructures that represent a single I/O operation. This is important to understand because a single I/O operation may need to be split into multiplebiostructures.bi_vcnt: This field specifies the number ofbio_vecstructures that are being used to describe the I/O operation. Eachbio_vecstructure in the vector describes a contiguous block of memory that is transferred between the block device and the user space program.bi_io_vec: This is a pointer to an array ofbio_vecstructures that describes the location and length of the data buffers associated with the I/O operation. This lays the ground for performingscatter-gatherI/O – that is, the data can be spread across multiple non-contiguous memory locations.bi_vcnt: This field specifies the number of data buffers associated with the I/O operation. Each data buffer is represented by abio_vecstructure, which contains a pointer to the memory buffer and the length of the buffer.bi_end_io: This is a pointer to a function that is called when the I/O operation completes. This function is responsible for cleaning up any resources associated with the I/O operation and waking up any processes that are waiting for the operation to complete.bi_private: This is a pointer to any private data associated with the I/O operation.bi_opf: This is a bit mask that specifies any additional options or flags associated with the I/O operation. This can include options such as force synchronous I/O or disable write caching.
When an I/O request is initiated by a user-space application, the bio structure keeps track of all the active I/O transactions at the block layer. Once the bio structure has been constructed, it is handed over to the block I/O layer through the submit_bio function. The submit_bio() function is used to submit I/O requests to block devices. Once the I/O has been submitted to the block device, it is added to a request queue. The submit_bio() function will not wait for the I/O to be completed.
It can be said that the bio structure acts as a bridge between the filesystem and the block device layer, enabling the filesystem to perform I/O operations on the block device.
The bio_vec structure (representing vector I/O)
Section titled “The bio_vec structure (representing vector I/O)”The bio_vec structure defines vector or scatter-gather I/O operations in the block layer.
The bio_vec structure is defined in include/linux/bvec.h:
struct bio_vec { struct page *bv_page; unsigned int bv_len; unsigned int bv_offset;};The fields are described as follows:
bv_page: This field holds a reference to the page structure (struct page) that contains the data to be transferred. As we explained in Chapter 2, a page is a fixed-size block of memory.bv_offset: This field holds the offset within the page where the data to be transferred starts.bv_len: This field holds the length of the data to be transferred.
Scatter-gather I/O involves transferring data between a device and the memory. Usually, data is read from or written to a single contiguous memory buffer. With scatter-gather I/O, the data is divided into smaller segments and spread across multiple non-contiguous memory buffers, known as scatter lists, for input operations. For output operations, the data is then gathered from these multiple non-contiguous memory buffers. The bio_vec structure is used to represent a scatter-gather I/O operation. The block layer may construct a single bio with multiple bio_vec structures, each representing a different physical page in memory and a different offset within that page.
Requests and request queues (representing pending I/O requests)
Section titled “Requests and request queues (representing pending I/O requests)”When an I/O request is submitted to the block layer, the block layer creates a request structure to represent the request.
The request and request_queue structures are defined in include/linux/blk-mq.h and include/linux/blkdev.h, respectively:
struct request { struct request_queue *q; struct blk_mq_ctx *mq_ctx; struct blk_mq_hw_ctx *mq_hctx;[……..]Some of the major fields are explained here:
struct request_queue *q: Each I/O request is added to the request queue of a block device. Theqfield here points to this request queue.struct blk_mq_ctx *mq_ctx: Theblk_mq_ctx *mq_ctxfield points to the software staging queues; this structure is allocated on a per-CPU core basis. Each CPU has ablk_mq_ctx, which is used to track the state of requests that are processed on that CPU.struct blk_mq_hw_ctx *mq_hctx: This field represents the hardware context with which a request queue is associated. This is used to keep track of which hardware queue the request belongs to.struct list_head queuelist: This is a linked list of requests that are waiting to be processed. When a request is submitted to the block layer, it is added to this list.struct request *rq_next: This is a pointer to the next request in the queue. It is used to link requests within the request queue.sector_t sector: This field specifies the starting sector number of the I/O operation.struct bio *bio: This field points to abiostructure that contains information about the I/O operation, such as its type (read or write).struct bio *biotail: This field points to the lastbiostructure in the queue. When a new bio is added to the queue, it is linked to the end of the list pointed to bybiotail.
The request_queue structure represents the request queue associated with a block device. The request queue is responsible for managing all I/O requests that are issued to the block device:
struct request_queue { struct request *last_merge; struct elevator_queue *elevator; struct percpu_ref q_usage_counter;[………..]Let’s look at some of the important fields:
struct request *last_merge: This field is used by the I/O scheduler to track the last request that was merged with another request.struct elevator_queue *elevator: This field points to the I/O scheduler for the request queue. The I/O scheduler determines the order in which requests are serviced.struct percpu_ref q_usage_counter: This field represents the usage counter for the request queue. The kernel uses a per-CPU counter to track the reference count of a resource on a per-CPU basis.struct rq_qos *rq_qos: This field points to a request queue of quality-of-service agreements that the request queue provides to the block device. These are used to prioritize I/O requests based on different criteria, such as the priority of the request.const struct blk_mq_ops *mq_ops: This structure contains function pointers that define the behavior of the request queue for multi-queue I/O schedulers.struct gendisk *disk: This field points to thegendiskstructure associated with the request queue.gendiskrepresents a generic disk device.
Phew! There are too many of them. Let’s summarize the role of each structure and see how they all work together.
The journey of an I/O request in the block layer
Section titled “The journey of an I/O request in the block layer”The following table provides a concise overview of the structures that were covered in the previous section:
| Structure | Representation of | Description |
|---|---|---|
gendisk | Physical disk | This is used to represent the physical device as a whole and contains information such as the disk’s capacity and geometry |
block_device | Block device | This represents a specific instance of a device and contains information such as the major and minor numbers, partitions, and the queue to handle I/O requests |
buffer_head | Block of data in memory | This is used to track the data that is read from or written to a block device in memory |
request | I/O request | This includes information such as the type of I/O operation and the starting block number |
request_queue | Queue of I/O requests | This contains information about the current state of the queue, such as the number of requests waiting to be processed |
bio | Block I/O | This is a higher-level I/O request and can include multiple request structures |
bio_vec | Scatter-gather list of memory buffers | This is used as a part of the bio structure and describes an individual data buffer |
Table 4.1 – Summary of major block layer structures
Let’s take a look at the relationship between these structures when a process issues an I/O request:
- When an application writes data in a buffer in its address space, the block layer creates a
buffer_headstructure to represent this data. - The block layer constructs a
biostructure to represent the block I/O request and maps thebuffer_headstructure to thebio_vecstructure. For eachbio, the block layer creates one or morebio_vecstructures to represent the data being read from or written to. - The
biostructure is then added to therequest_queuestructure for the intended block device through therequeststructure. - The device driver for that device, which is registered through
register_blkdev, dequeues thebiostructure and schedules it for processing. biois then split into one or morerequeststructures based on the block size of the device.- Each
requestobject is then added to therequest_queuestructure of the corresponding device driver for processing. - After processing the request, the device driver writes the data to the physical storage.
- Once the I/O request has been completed, the device driver notifies the block layer.
- The block layer then updates the buffer cache and the associated data structures. It marks the request structure as completed and notifies any waiting processes that the I/O operation has been completed.
- The corresponding
buffer_headstructures are updated to reflect the current state of the data on the block device.
The block layer, with its intricate design, makes use of some complex structures to work with block devices. We covered some major structures to help you understand how things work under the hood. Each structure defines a ton of fields in its definition; we’ve tried to highlight some to get a gist of things.
It’s important to note that the request queues in older kernels were single-threaded and were not able to exploit the capabilities of modern hardware. The Linux kernel added multi-queue support in version 3.13. The framework for implementing multi-queue support is known as blk-mq. We’re going to cover the multi-queue framework in the next chapter.
Summary
Section titled “Summary”The first part of this book, which included Chapters 1, 2, and 3, dealt with VFS and filesystems. The second part of this book, which constitutes Chapters 4, 5, and 6, is all about the block layer. This chapter introduced the role of the block layer in the Linux kernel. The block layer is the kernel subsystem and is in charge of managing I/O operations performed on block devices. The kernel’s block device interface is central to managing persistent storage on Linux. The user-space applications can access the block devices through block special devices in the /dev directory. Working with block devices is far more complicated than working with character devices, which can only work sequentially. Character devices have a single current position. Managing block devices is a far more complex task for the kernel as block devices must be able to move to any position to provide random access to data. Because of this, performance is a major concern when working with block devices. The Linux kernel provides a complex ecosystem of structures in the block layer for working with block devices.
In the next chapter, we will build on our understanding and see how an I/O request is served in the block layer. We’ll also cover the device mapper and multi-queue frameworks in the kernel.