Linux Kernel Architecture

(Jacob Rumans) #1

Chapter 6: Device Drivers


❑ disk->fops->openinvokes the appropriateopenfunction for the file to perform hardware-
specific initialization tasks.
❑ If the partition information has become invalid as indicated byblock_device->bd_invalidated,
rescan_partitionsis called to re-read the partition information. Partition information can
become invalid if a removable medium is exchanged.

If the device has not been opened before, some more work is required. First suppose that not a
partition, but the main block device is opened — which may nevertheless contain partitions. In this
case, the required actions are modulo some bookkeeping details identical to the case shown above:
disk->fops->openhandles the low-level work of opening the device, andrescan_partitionsreads the
partition table if the existing information is invalid.

This is, however, usually the first time that partition information is read in. When a new disk is registered
usingadd_disk,thekernelsetsgendisk->bd_invalidatedto 1, which signals an invalid partition table
on the block device (in fact, since there is no partition table at all, it cannot really be calledvalid!). Then a
fake file is constructed as a parameter passed todo_open, and this, in turn, triggers reading the partition
table.

If the opened block device represents a partition that has not been opened before, the kernel needs to
associate theblock_deviceinstance for the partition with theblock_devicethat contains the partition.
Essentially this works as follows:

fs/block_dev.c
struct hd_struct *p;
struct block_device *whole;
whole = bdget_disk(disk, 0);
...
bdev->bd_contains = whole;
p = disk->part[part - 1];
...
bdev->bd_part = p;

After finding theblock_deviceinstance that represents the whole disk that includes the partition, a link
between partition and container is set up usingblock_device->bd_contains. Note that the kernel can
find the whole block device starting from the partition’s block device, but not vice versa! Additionally, the
partition information inhd_structis now shared between the generic hard disk and theblock_device
instance for the partition, as indicated in Figure 6-10.

6.5.5 Request Structure


The kernel provides its own data structure to describe a request to a block device.

<blkdev.h>
struct request {
struct list_head queuelist;
struct list_head donelist;

struct request_queue *q;

unsigned int cmd_flags;
Free download pdf