This article talks on the DeviceNumber management works in Linux block subsystem. However, the idea applies tocharacter device number management too.
NOTE:I am referring to linux-3.17 sourcecode.
The purpose of introducing device numbermanagement mechanism is to efficiently obtain the corresponding kobject by Agiven 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 Correspondinggendisk, and Then initializes the inode and Block_device structures by Gendiskinstance.
First,i ' d like to introduce the basic struct kobj_map in drivers/base/map.c:
650) this.width=650; "title=" F1.png "src=" http://s3.51cto.com/wyfs02/M01/74/C6/wKioL1Yo0fCiTrxlAAFPlgZRX8M867.jpg "alt=" Wkiol1yo0fcitrxlaafplgzrx8m867.jpg "/>
There is total 255 probe entries in Eachkobj_map object. The probe instances of the same entry is linked by Nextpointer. One probe instance is the unique according to Dev and range parameters.
Then which entry should a device number Beput are decided by the value of device major% 255, as a result, some Devicenumbe Rs'll be placed in the same entry. And each of them have a probeinstance, and is linked. The probe instances of the same device number issorted by the range value, which make sure the most accurate instance over Ridesothers and matches first when lookup.
For block subsystem, the Kobj_map isdefined in GENHD.C:
650) this.width=650; "title=" F2.png "src=" http://s3.51cto.com/wyfs02/M00/74/C9/wKiom1Yo0g7iJmUWAAArsxYGPAM961.jpg "alt=" Wkiom1yo0g7ijmuwaaarsxygpam961.jpg "/>
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:
650) this.width=650; "title=" F3.png "src=" http://s3.51cto.com/wyfs02/M01/74/C6/wKioL1Yo0mryTKDuAABRoOr8LaA225.jpg "alt=" Wkiol1yo0mrytkduaabroor8laa225.jpg "/>
Functionkobj_map_init () is defined as below:
650) this.width=650; "title=" F4.png "src=" http://s3.51cto.com/wyfs02/M02/74/C9/wKiom1Yo0nnBJWoiAAJlQiDCEgw142.jpg "alt=" Wkiom1yo0nnbjwoiaajlqidcegw142.jpg "/>
From the code fragment, we can see all theprobe entries is 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.
650) this.width=650; "title=" F5.png "src=" http://s3.51cto.com/wyfs02/M02/74/C6/wKioL1Yo0sqgXJ-rAAILQsQruL4011.jpg "alt=" Wkiol1yo0sqgxj-raailqsqrul4011.jpg "/>
Kobj_map () would 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 would be used to obtain Kobject instance WH Enlookup.
For disk device, the input data value Isgendisk instance, the probe function was 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.
650) this.width=650; "title=" F6.png "src=" http://s3.51cto.com/wyfs02/M00/74/C6/wKioL1Yo0vzgGbFqAAMnMdPv9V4409.jpg "alt=" Wkiol1yo0vzggbfqaamnmdpv9v4409.jpg "/>
Now, we know how the device numbermanagement works, and how the device number and Gendisk instance get Connectedin Linux B Lock subsystem.
This article is from the "Jenny Technology sharing" blog, please be sure to keep this source http://jennyyi.blog.51cto.com/7764939/1705364
Linux Block Device Management