Linux Kernel Architecture

(Jacob Rumans) #1

Chapter 6: Device Drivers


Partitions represented bystruct hd_structalso contain an embeddedkobject. Conceptually, partitions
are subelements of a hard disk, and this is also captured in the data structures: Theparentpointer of the
kobjectembedded in everyhd_structpoints to thekobjectof the generic hard disk.

Block Device Operations


Operations specific to a class of block devices are collected in the following (slightly simplified) data
structure:

<fs.h>
struct block_device_operations {
int (*open) (struct inode *, struct file *);
int (*release) (struct inode *, struct file *);
int (*ioctl) (struct inode *, struct file *, unsigned, unsigned long);
...
int (*media_changed) (struct gendisk *);
int (*revalidate_disk) (struct gendisk *);
...
struct module *owner;
};

open,releaseandioctlhave the same meaning as the equivalent functions infile_operationsand
are used to open and close files, and to send special commands to a block device.

The functions are not invoked directly by the VFS code but indirectly by the
operations contained in the standard file operations for block devices,
def_blk_fops.

The remaining elements ofblock_device_operationslist the options available only to block devices.

❑ media_changedchecks whether the storage medium has been changed as can happen with
devices such as floppy disks and ZIP drives (hard disks do not usually support this function
because they cannot normally be exchanged...). The routine is provided for internal use in
the kernel to prevent inconsistencies owing to careless user interaction. Data loss is inevitable if
a floppy disk is removed from the drive without first having been unmounted and if the data
in the cache have not been synchronized with the contents on the disk in the meantime. The
situation is even worse if a user removes a floppy disk whose changes have not yet been written
back and inserts a new floppy with different contents. When writeback finally takes place,
the contents of the new floppy are destroyed orat least severely compromised — this should
be prevented at all costs because it compounds the fact that the data on the first floppy have
already been lost. The kernel can, indeed, prevent such loss by invokingcheck_media_changeat
the appropriate points in the code.

❑ As its name suggests,revalidate_diskis called to revalidate the device. Currently, this is only
necessary when an old medium is removed and replaced with a new medium without first per-
forming a correct unmount followed by a new mount.

Theownerfield holds a pointer to a module structure in memory if the driver is implemented as a mod-
ule. Otherwise, it contains aNULLpointer.
Free download pdf