Linux kernel MD Source code interpretation of the three-array creation process

Source: Internet
Author: User
Tags function prototype linux

In this section we read the creation process of the array.

According to common sense card, we go to IOCTL to find array create command, MD corresponding IOCTL function is md_ioctl, when looking for the corresponding cmd command word, but there is no similar create_array command, then the MD device is not through the IOCTL function to create. In fact, if we look at the prototype of the MD_IOCTL function, we find that the MD device is not actually in place, the function prototype is as follows:

6303 static int md_ioctl (struct block_device *bdev, fmode_t mode,  
6304 unsigned                         int cmd, unsigned long arg)

6303 lines, the first parameter is struct block_device*, that is, the command issued for a block device, but we do not have the corresponding MD block device before creating the MD device.

Here, the clue is broken, where is the entrance to the building of the device?

Blocked, let's go another way. Creating a MD device always creates a struct MDDEV structure, so find out where to apply the struct MDDEV memory structure is it OK? This method is feasible, but struct MDDEV structure is applied by kmalloc, how does this know? Because there is no code in the function md_init to request the struct Mddev memory pool, you have to apply Kmalloc. We search for Kmalloc in MD.C file and find out the location of struct Mddev creation based on the result. But here we use a simpler method, that is to apply to the struct MDDEV structure is always initialized, the initialization function is mddev_init, search this function, MD.C file only function Mddev_find () a call to, It's obvious that you've found the creation portal for the struct MDDEV structure, so call the upper level to find the entry function for the MD device.

We can find this invocation relationship, and the arrows represent the invocation relationship:

Mddev_find () <---md_alloc () <---md_probe ()

The Md_probe () function is the incoming parameter in the Blk_register_region () function called in the module initialization function Md_init (), and the students familiar with the BLK layer know that as long as a MD device is created in the user state, the kernel probe () is invoked accordingly. function, and the probe () function passed in here is Md_probe (). So creating the struct Mddev structure is triggered by the user state, not directly by the kernel state. If in today's era, but also put this trivial things in the core State to do, you are embarrassed to say that you are doing Linux development. To do Linux development is to guide fashion, advocating simplicity is the United States. The Linux kernel provides only a mechanism and does not provide a specific implementation strategy. Control commands unrelated to the mechanism need to move from the kernel to the user state, on the one hand, simplifying the kernel, highlighting the key to facilitate the kernel maintenance, on the other hand in the user state maintenance strategy to make the application more flexible and convenient debugging.

In this way we will kill the user state from the kernel state, the user state program is the famous mdadm, the online random search is a lot of the article, but the best article is not online, but with the command man Mdadm. With the command mdadm create an array, here do not read Mdadm code, because this is the user state program is not the focus of our reading, and then the code is very simple basically learned junior high school English students can read. What we need to know is that the Mdadm create command eventually calls the Mknod () function to create a/dev/md* device, so that the kernel has a struct MDDEV structure, and the structure is an empty structure, which means that the array has no properties set. No corresponding physical disk, no array running.

By this time, now that you have the MD device, it's time for Md_ioctl to play, and this function corresponds to the IOCTL command word in file include\linux\raid\md_u.h:

 +/* IOCTLs/Raid_version/_ior (Md_major, 0x10, mdu_version_t) 40 # * Define Get_array_info _ior (Md_major, 0x11, mdu_array_info_t) #define Get_disk_info _ior (md_major , 0x12, mdu_disk_info_t) #define Print_raid_debug _io (md_major, 0x13) #define Raid_autorun _i O (Md_major, 0x14) #define Get_bitmap_file _ior (md_major, 0x15, mdu_bitmap_file_t)/* Configurati On/* #define Clear_array _io (md_major, 0x20) #define Add_new_disk _iow (Md_major, 0x21, M du_disk_info_t) #define Hot_remove_disk _io (md_major, 0x22) #define Set_array_info _iow (md_ma         Jor, 0x23, mdu_array_info_t) Wuyi #define Set_disk_info _io (md_major, 0x24) #define Write_raid_info _io (Md_major, 0x25) #define Unprotect_array _io (md_major, 0x26) #define Protect_array _io (Md_major, 0x27) #dEfine Hot_add_disk _io (md_major, 0x28) #define Set_disk_faulty _io (md_major, 0x29) #define  Hot_generate_error _io (md_major, 0x2a) #define Set_bitmap_file _iow (md_major, 0x2b, int) 59 60/* Usage * * #define Run_array _iow (md_major, 0x30, mdu_param_t)/* 0X31 was Start_array * * #defi Ne stop_array _io (md_major, 0x32) #define Stop_array_ro _io (md_major, 0x33) #define RES TART_ARRAY_RW _io (Md_major, 0x34) 

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.