Linux kernel MD Source code interpretation two MD module initialization

Source: Internet
Author: User
Tags config goto linux

When compiling the Linux kernel source code, the DRIVERS/MD directory will generate multiple KO files, then which one of these kernel modules to load first, which after loading? For example, Md-mod.ko, Raid5.ko, Raid10.ko, are these modules loaded together or in a sequential order? If you are familiar with Linux kernel programming, you know that there is a request_module function that asks to load a module, but this function does not explain the dependencies of a module on another module. The exact information comes from Kconfig, where only the relevant parts of the kconfig are extracted:

Config BLK_DEV_MD

TriState "RAID Support"

Config MD_RAID10

TriState "RAID-10 (mirrored striping) mode"

Depends on BLK_DEV_MD

Config md_raid456

TriState "raid-4/raid-5/raid-6 Mode"

Depends on BLK_DEV_MD

From here we can see that Raid5.ko, Raid10.ko are all dependent on the Md-mod.ko, which determines that our reading direction starts from the Md-mod.ko.

So which document did the Md-mod.ko start with? This is going to find the Module_init function, which is defined in MD.C, so start here.

 8416 static int __init md_init (void) 8417 {8418 int ret =-ENOMEM;  
8419 8420 md_wq = Alloc_workqueue ("MD", Wq_mem_reclaim, 0);  
8421 if (!MD_WQ) 8422 goto ERR_WQ;  
8423 8424 md_misc_wq = Alloc_workqueue ("Md_misc", 0, 0);  
8425 if (!MD_MISC_WQ) 8426 goto ERR_MISC_WQ;  
8427 8428 if (ret = Register_blkdev (md_major, "MD")) < 0) 8429 Goto ERR_MD;  
8430 8431 if (ret = Register_blkdev (0, "MDP")) < 0) 8432 Goto ERR_MDP;  
8433 mdp_major = ret;                             8434 8435 blk_register_region (Mkdev (md_major, 0), 1ul<<minorbits, This_module, 8436  
Md_probe, NULL, NULL);                             8437 blk_register_region (Mkdev (mdp_major, 0), 1ul<<minorbits, This_module, 8438  
Md_probe, NULL, NULL);  
8439 8440 Register_reboot_notifier (&md_notifier);        8441 Raid_table_header = register_sysctl_table (raid_root_table);  
8442 8443 md_geninit ();  
8444 return 0;  
8445 8446 err_mdp:8447 Unregister_blkdev (md_major, "MD");  
8448 err_md:8449 Destroy_workqueue (MD_MISC_WQ);  
8450 err_misc_wq:8451 Destroy_workqueue (MD_WQ);  
8452 err_wq:8453 return ret; 8454} 

The initialization process of the module looks unusually simple, as some people appear to be very ordinary, inside the heart is extremely strong, so do not only look at the appearance, but also listen to their words. The inner beauty is more attractive and lasting than the outward splendor.

Lines 8420 and 8424, respectively, create work queues, md_wq are for flush commands, and the other Md_misc_wq,misc is miscellaneous shorthand, which is miscellaneous, and is used to handle bits and pieces.

8428 and 8431 Lines, created two block devices, at first I only pay attention to the MD device, did not care about MDP, search variable mdp_major, in the function autorun_devices use this variable:

5474                 if (part) {  
5475                         dev = Mkdev (mdp_major,  
5476                                     rdev0->preferred_minor << Mdpminorshift);  
5477 Unit                         = MINOR (dev) >> mdpminorshift;  
5478                 } else {  
5479                         dev = Mkdev (md_major, rdev0->preferred_minor);  
5480 Unit                         = MINOR (dev);  
5481                 }

5474 lines, the variable part is the function pass in the argument, which indicates the partition of the disk, then you know that the letter P in the mdp_major is meant to represent it, and Mdp_major represents the array created with the partition of the disk. Lines 8435 and 8437 create two region, which function when the user state creates a/dev/md* device, the kernel state corresponds to the call md_probe creates a MDDEV structure, and then the user state to the/dev/md* Operation to the kernel state is accordingly to the mddev operation. 8440 Line, register shutdown callback function, the main function is to stop the array thread, brush the data operation. 8441 lines, register the SYSCTL function to control the minimum and maximum sync speed of the array. 8443 lines, register the proc function, and then have the directory/proc/mdstat, the display of the directory by the function Md_seq_show control. The MD initialization code was done so easily, but back to our original intention, we still knew nothing about the MD device. So how is the MD device created? What about the process of creating? What resources does an array have? In the next section, we go straight to the core and start reading the array creation process.

Source: Http://blog.csdn.net/liumangxiong

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.