Linux Kernel Architecture

(Jacob Rumans) #1

Chapter 6: Device Drivers


struct block_device *i_bdev;
struct cdev *i_cdev;
};
...
};

❑ To uniquely identify that device associated with a device file, the kernel stores the file type
(block- or character-oriented) ini_modeand the major and minor numbers ini_rdev.Thesetwo
items of information are combined in the kernel into a single variable of typedev_t.

The definition of this data type is by no means set in stone but may be modified if
kernel developers deem it necessary. Forthis reason, only the two helper functions
imajorandiminor— which expect a pointer to aninodeinstance as
parameter — should be used to extract the major and minor number fromi_rdev.

❑ i_fopis a collection of file operations such as open, read, and write used by the virtual filesystem
to work with the block device (the exact definition of the structure is given in Chapter 8).
❑ Depending on whether the inode represents a block or a character device,i_bdevori_cdev
point to more specific information, which is discussed further below.

6.3.2 Standard File Operations


When a device file is opened, the various filesystem implementations invoke theinit_special_inode
function to create the inode for a block or character device file.^6

fs/inode.c
void init_special_inode(struct inode *inode, umode_t mode, dev_t rdev)
{
inode->i_mode = mode;
if (S_ISCHR(mode)) {
inode->i_fop = &def_chr_fops;
inode->i_rdev = rdev;
} else if (S_ISBLK(mode)) {
inode->i_fop = &def_blk_fops;
inode->i_rdev = rdev;
}
else
printk(KERN_DEBUG "init_special_inode: bogus i_mode (%o)\n",
mode);
}

The underlying filesystem must return the major and minor numbers of a device in addition to the device
type (block or character) passed inmode. The inode is supplied with different file operations depending
on device type.

6.3.3 Standard Operations for Character Devices


The situation with character devices is initially very unclear because only a single file operation is made
available.

(^6) For the sake of simplicity, I omit the creation of inodes for sockets and fifos as these are not relevant in this context.

Free download pdf