码迷,mamicode.com
首页 > 系统相关 > 详细

Linux Block Device Management

时间:2015-10-23 00:15:35      阅读:1138      评论:0      收藏:0      [点我收藏+]

标签:linux block subsystem

This article talks about how the device number management works in Linux block subsystem. However, the idea applies to character device number management too.

Note: I am referring to linux-3.17 source code.

 

The purpose of introducing device number management mechanism is to efficiently obtain the corresponding kobject by a given device number, then gendisk instance that represents the whole disk.

When creating a block device file under /dev/, the generic block layer uses the dev_t to find the corresponding gendisk, and then initializes the inode and block_device structures by gendisk instance.

 

First, I’d like to introduce the basic struct kobj_map in drivers/base/map.c:

技术分享

There are total 255 probe entries in each kobj_map object. The probe instances of the same entry is linked by next pointer. One probe instance is unique according to dev and range parameters.

Then which entry should a device number be put is decided by the value of device major % 255, as a result, some device numbers will be placed in the same entry. And each of them has a probe instance, and is linked. The probe instances of the same device number is sorted by the range value, which make sure the most accurate instance overrides others and matches first when lookup.

 

For block subsystem, the kobj_map is defined in genhd.c:

技术分享


Second, I’ll explain how the device numbermanagement works in block subsystem.

1.  bdev_map initialization

bdev_map is initialized during genericblock layer initialization in genhd_device_init() defined in genhd.c:

技术分享

Function kobj_map_init() is defined as below:

技术分享

From the code fragment, we can see all theprobe entries are malloc and initialized by default value.


2.  device number registration

Any block device driver can callblk_register_region() to register the device number and region. Actuallyblk_register_region packages kobj_map() with bdev_map as kobj_map parameter.

技术分享

Kobj_map() will create one probe instancethat is set by the function input and inserted to bdev_map probe entries basedon the devt and range values. What’s more, the probe function pointer and datais saved in probe instance, and will be used to obtain kobject instance whenlookup.

For disk device, the input data value isgendisk instance, the probe function is exact_match() that converts gendisk tothe corresponding kobject.

 

3.  device number lookup

Blockdevice driver calls get_gendisk() to obtain the gendisk instance based on thedevice number. get_gendisk() calls to kobj_lookup() to find the kobject in bdev_map.

技术分享

Now, we know how the device number management works, and how the device number and gendisk instance get connected in Linux Block subsystem.



本文出自 “Jenny的技术分享” 博客,请务必保留此出处http://jennyyi.blog.51cto.com/7764939/1705364

Linux Block Device Management

标签:linux block subsystem

原文地址:http://jennyyi.blog.51cto.com/7764939/1705364

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!