Initializing a Block Device Driver
Let's now describe how a block device driver is initialized. We already described how the kernel customizes the methods of the file object when a block device file is opened in Section 13.2.3. Its f_op field is set to the address of the def_blk_fops variable. The contents of this table are shown in Table 13-5. The dentry_open( ) function checks whether the open method is defined; this is always true for a block device file, so the blkdev_open( ) function is executed.
|
Method |
Function for block device file |
|
open |
blkdev open( ) |
|
release |
blkdev close( ) |
|
llseek |
block llseek( ) |
|
read |
generic file read( ) |
|
write |
generic file write( ) |
|
mmap |
generic file mmap( ) |
|
fsync |
block fsync( ) |
|
ioctl |
blkdev ioctl( ) |
This function checks whether the block device driver is already in use:
bd_acquire(inode); do open(inode->i bdev, filp);
The bd_acquire( ) function essentially executes the following operations:
1. Checks whether the block device file corresponding to the inode object is already open (in this case, inode->i_bdev field points to the block device descriptor). If the file is already open, increments the usage counter of the block device descriptor
(inode->i_bdev->bd_count) and returns.
2. Looks up the block device driver in the hash table using the major and minor numbers stored in inode->rdev. If the descriptor is not found because the driver is not in use, allocates a new block_device and a new inode object for the block device, and then inserts the new descriptor in the hash table.
3. Stores the address of the block device driver descriptor in inode->i_bdev.
4. Adds inode to the list of inodes of the driver descriptor.
Next, blkdev_open( ) invokes do_open( ), which executes the following main steps:
1. If the bd_op field of the block device driver descriptor is null, initializes it from the blkdevs table's element corresponding to the major number of the block device file
2. Invokes the open method of the block device driver descriptor (bd_op->open) if it is defined
3. Increments the bd_openers counter of the block device driver descriptor
4. Sets the i_size and i_blkbits fields of the block device inode object (bd_inode)
The open method of the block device driver descriptor can further customize the methods of the block device driver, allocate resources, and take other measures based on the minor number of the block device file.
Among other things, the device driver initialization function must determine the size of the physical block device corresponding to the device file. This length, represented in 1,024-byte units, is stored in the blk_size global array indexed by both the major and minor number of the device file.
Continue reading here: Sectors Blocks and Buffers
Was this article helpful?