Linux Kernel Architecture

(Jacob Rumans) #1

Chapter 6: Device Drivers


Inode

Userspace application

File operations

VFS

Gen-
Disk

Block Device Operations


I/O Ports


Submitting Requests


Request Structure


block_device

Block
device database

Kernel

User

Figure 6-9: Overview of the block device layer.

Raw block devices are represented bystruct block_device, which I discuss further below. Since this
structure is managed in an interesting way by the kernel, we need to take a close look at this first.

By convention, the kernel stores theblock_deviceinstance associated with a block device immediately
in front of the block device’s inode. This behavior is implemented by the following data structure:

fs/block_dev.c
struct bdev_inode {
struct block_device bdev;
struct inode vfs_inode;
};

All inodes that represent block devices are kept on thebdevpseudo-filesystem (see Section 8.4.1), which
is not visible to userland. This allows for using standard VFS functions to work with the collection of

Opening Block Device Files


In particular, this is exploited by the auxiliary functionbdget. Given a device number represented by
adev_t, the function searches through the pseudo-filesystem to see if a corresponding inode already
exists and returns a pointer to it. Thanks tostruct bdev_inode, this immediately allows for finding the
block_deviceinstance for the device. If the inode does not yet exist because the device has not been
opened before,bdgetand the pseudo-filesystem automatically ensure that a newbdev_inodeis allocated
and set up properly.

In contrast to the character device layer, the block device layer provides comprehensive queueing func-
tions as demonstrated by therequest queueassociated with each device. Queues of this kind are the reason
for most of the complexity of the block device layer. AsFigure 6-9 shows, the individual array entries (in
simplified form) contain pointers to various structures and procedures whose most important elements
are as follows:

❑ A wait queue to hold both read and write requests to the device.
❑ Function pointers to the I/O scheduler implementation used to rearrange requests.
Free download pdf