DeviceMapper mechanism in Linux kernel

Source: Internet
Author: User
Article title: DeviceMapper mechanism in Linux kernel. Linux is a technology channel of the IT lab in China. Includes basic categories such as desktop applications, Linux system management, kernel research, embedded systems, and open source.

June 29, 2006

This article introduces the device er ing mechanism in the Linux kernel based on the specific code. Device er is a ing framework from a logical Device to a physical Device provided in the Linux 2.6 kernel. under this mechanism, users can easily develop storage resource management policies based on their own needs. Currently, Logical Volume Manager in Linux is popular, such as LVM2 (Linux Volume Manager 2 version) and EVMS (Enterprise Volume Management System) and dmraid (Device Mapper Raid Tool) are all implemented based on this mechanism. Understanding this mechanism is the basis for further analysis and understanding of the implementation and design of these Volume managers. Through this article, we can further understand the design and implementation of Linux System Block I/O.

Device Mapper is a general Device ing mechanism that supports logical volume management in Linux2.6 kernel. It provides a highly modular kernel architecture for block Device drivers used for storage resource management.


Kernel architecture of Device Mapper

In the kernel, it uses a modular target driver plug-in to filter IO requests or redirect requests, currently, the target driver plug-in includes soft raid, soft encryption, logical volume strip, multi-path, image, and snapshot, in the figure, linear, mirror, snapshot, and multipath represent these target drivers. Device er further embodies the principle of policy and mechanism separation in the Linux kernel design, and puts all policy-related work into the user space, the kernel mainly provides the mechanisms required to complete these policies. The Device mapper user space is mainly responsible for configuring specific policies and control logic, such as establishing mappings between logical devices and physical devices and how to establish these mappings, the specific filtering and redirection of IO requests are completed by the relevant code in the kernel. Therefore, the entire device mapper mechanism consists of two parts: the device mapper driver of the kernel space, the device mapper library of the user space, and the dmsetup tool provided by it. In the following section, we will introduce the kernel and user space.

Kernel

The kernel-related code of Device mapper has been integrated into the kernel source code as part of the Linux 2.6 kernel release version. the code is in the driver/md/directory of the kernel source code, the code file can be divided into two parts: files that implement the basic architecture in the device mapper kernel and target driver plug-in files that implement specific ING. The analysis results below are mainly obtained based on the above source code file.

Important concepts

Device er is registered as a block device driver in the kernel. It contains three important object concepts: mapped device, ing table, and target Device. The Mapped device is a logical abstraction that can be understood as a logical device provided by the kernel. it establishes a ing with the target device through the ing relationship described in the ing table. The ing table from Mapped device to a target device is represented by a multi-element group, the multivariate group is composed of the starting address, range, and variable indicating the address offset and target type of the physical device where the target device is located (these addresses and offsets are based on the disk's slice in units, that is, the size of 512 bytes ). Target device indicates the physical space segment mapped to the mapped device. for the logical device indicated by mapped device, it is a physical device mapped to the logical device. The three objects in Device mapper and the target driver Plug-In constitute an iterative Device tree. In this tree structure, the top-level root node is the mapped device finally provided as the logic device. the leaf node is the underlying physical device represented by the target device. The smallest device tree consists of a single mapped device and a target device. Each target device is exclusive to the mapped device and can only be used by one mapped device. A mapped device can be mapped to one or more target devices, and a mapped device can be used as the target device of its upper-layer mapped device, theoretically, this layer can be infinitely iterated in the device mapper architecture.


Hierarchy of objects in the Device mapper kernel

In, we can see that mapped device1 establishes a ing relationship with the target device a, B, and c through the ing table, and target device a is evolved through mapped device 2, mapped device 2 establishes a ing relationship with target device d through the ing table.

Let's further look at the specific implementation of the above three objects in the code, dm. the mapped_device structure defined in file c is used to represent the mapped device. It mainly includes the locks related to the mapped device, the registered request queue, some memory pools, pointers to the corresponding ing tables, and other fields. The ing table corresponding to the Mapped device is represented by the dm_table structure defined in the dm_table.c file, which contains an array of dm_target structures, the dm_target structure describes the ing between mapped_device and a target device. In the dm_table structure, these dm_targets are organized in the form of B trees to facilitate the search operation during IO request ING. The Dm_target structure records the start address and range of the mapped device logical region mapped to the target device. It also contains pointers to the target_type structure of specific target device operations. The Target_type structure mainly includes the name of the target driver plug-in corresponding to the target device, the method for defining the construction and deletion of the target device type, the IO request re ing corresponding to the target device type, and method. The target device domain is the private domain in dm_target. the pointer points to the structure of the target device mapped by mapped device. The specific structure of the target device varies with the target type. for example, the simplest linear ING of the target type corresponds to the structure of the target device, which is the linear_c structure defined in the dm-linear.c file. It is defined as follows:


struct linear_c { struct dm_dev *dev; sector_t start;};                  

The definition of the target device is quite simple. it only includes the dm_dev structure pointer indicating the corresponding physical device and the start offset address in the unit of sector in the physical device. The preceding data structure relationship 3 is shown in the following figure:


Relationship between several important data structures in device mapper

Kernel creation process

The following describes how to create a mapped device in the kernel based on the specific code:

1. use the dev_create function in the dm-ioctl.c file to create the mapped device structure based on the parameters sent from the ioctl interface provided by the kernel to the user space. This process is very simple. it mainly applies to the kernel for necessary memory resources, including the mapped device and the memory pool pre-applied for IO operations, use the blk_queue_make_request function provided by the kernel to register the request queue dm_request corresponding to the mapped device. And register the mapped device as a disk block device to the kernel.

2. call dm_hash_insert to insert the created mapped device to a global hash table in device mapper. The table stores all the mapped devices currently created in the kernel.

3. the user space command calls the table_load function through ioctl. this function constructs a ing table for the specified mapped device and the target device mapped based on parameters sent from the user space. This function first constructs the corresponding dm_table, dm_target structure, then calls the dm_table_add_target function in the dm-table.c to initialize these structures according to the parameters passed by the user, and according to the target type specified by the parameter, call the corresponding target construction function ctr to build the structure of the target device in the memory, and then update the B tree maintained in the dm_table according to the established dm_target structure. After the above process is completed, add the created dm_table to the hash_cell structure corresponding to the global hash table of the mapped device.

4. finally, call the do_resume function through ioctl to establish the binding relationship between the mapped device and the ing table. In fact, this process is to assign the current dm_table structure pointer value to the map domain corresponding to mapped_device through the dm_table function, modify the ed_device field to indicate the current state.

Through the four main steps above, device mapper creates a mapped device logical block device in the kernel that can be provided to users.

IO stream

The essential function of Device er is to forward IO requests from the logical device mapped device to the target Device based on the ing relationship and the IO processing rules described by the target driver. Device er processes all the generic_make_request and submit_bio interfaces of the block-level IO subsystem in the kernel and directs all block read/write IO requests to the mapped device. IO requests are forwarded from top to bottom in the device tree of device mapper. When a bio request is forwarded to the lower layer by mapped deivce in the device tree, one or more bio clones are created and sent to the lower target device. Then, the same process repeats at every layer of the device tree. as long as the device tree is large enough, theoretically such a forwarding process can be carried out infinitely. At a certain level in the device tree, after the target driver ends a bio request, it reports the event that ended the bio request to its upper-layer mapped device, this process is performed at various layers until the event is finally uploaded to the root mapped device. then the device mapper ends the original bio request on the root mapped device and ends the entire IO request process.

Bio is finally forwarded to one or more leaf target nodes during layer-by-layer forwarding of the device mapper device tree. Because a bio request cannot span multiple target devices (that is, physical space segments, the device mapper clones one or more bio based on the target ing information of the Ed device that the user informs, splits bio, and forwards it to the target device. These cloned bio files are first handed over to the target driver corresponding to the mapped device for processing, and IO requests are filtered according to the IO processing rules defined in the target driver, then, submit it to the target device. The above process is completed in the dm_request function in the dm. c file. Target driver can process these bio as follows:

1. wait for the bio to be processed in the internal queue of the driver;

2. redirect bio to one or more target devices or different sectors on each target device;

3. return error to device mapper.

IO requests are processed layer by layer in the device tree as described above until the IO request ends.

Summary

Device er provides a ING architecture from a logical Device to a physical Device in the kernel, as long as the user develops a ing policy in the user space, compile the target driver plug-in to process specific IO requests according to your own needs, so that you can easily implement a Logical Volume Manager similar to LVM. Device er provides an interface in ioctl mode. you can use the device mapper library in the user space to send the ioctl command to the character device of Device mapper for inner communication. It also provides an expected event notification mechanism through ioctl, allowing the target driver to send IO-related events to the user space.


 

[1] [2] Next page

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.