Linux Kernel Architecture

(Jacob Rumans) #1

Chapter 6: Device Drivers


fs/devices.c
struct file_operations def_chr_fops = {
.open = chrdev_open,
};

Character devices differ considerably. The kernel cannot therefore initially provide more than one
operation because each device file requires a separate, custom set of operations. The main task of the
chrdev_openfunction is therefore to fill the structure withthe missing function pointers appropriate to
the opened device so that meaningful operations can be performed on the device file and ultimately on
the device itself.

6.3.4 Standard Operations for Block Devices


Block devices conform to a much more uniform scheme. This allows the kernel to provide a much larger
selection of operations right from the start. These are grouped together in a general structure called
blk_fops.
fs/block_dev.c
const struct file_operations def_blk_fops = {
.open = blkdev_open,
.release = blkdev_close,
.llseek = block_llseek,
.read = do_sync_read,
.write = do_sync_write,
.aio_read = generic_file_aio_read,
.aio_write = generic_file_aio_write_nolock,
.mmap = generic_file_mmap,
.fsync = block_fsync,
.unlocked_ioctl = block_ioctl,
.splice_read = generic_file_splice_read,
.splice_write = generic_file_splice_write,
};
Read and write operations are performed by generic kernel routines. The caches generally present in the
kernel are used automatically for block devices.

file_operationsmust not be used withblock_device_operationsalthough both
are similarly structured. Althoughfile_operationsis used by the VFS layer to
communicate with userspace, the routines it contains invoke the functions in
block_device_operationsto implement communication with the block device. The
block_device_operationsmust be implemented specifically for each block device
to abstract the device properties, but the samefile_operationsthat build on these
can be used for all block devices.

In contrast to character devices, block devices are not fully described by the above data structures,
because access to block devices is not performed in response to each individual request but is efficiently
managed by a refined and complex system of caches and request lists. The caches are operated predomi-
nantly by general kernel code, but the request listsare managed by the block device layer. When I discuss
possible block device driver operations at greater length, you will see further structures used to manage
the request queue, which collects and arrangesstatements addressed to the relevant device.
Free download pdf