Saturday, January 8, 2011

Resolution in Linux VFS file system mechanism (bottom)

The establishment of the next directory 5.VFS to better understand the VFS, here we use a practical example to see how the Linux VFS root to create a new directory "/dev".

To establish a new VFS, first of all we have to search the catalog, the search is to find the directory that will be the establishment of the parent directory of relevant information, because "the hair adhere to attach. For example, to set up directory/home/ricard, then you must first be conducted along the directory path to the drill, in this case starting from the root directory to find, and then locate the directory in the root directory of the home, and then down to the new directory name ricard, so the preceding speaks first directory search, in this case is to find ricard this new directory's parent directory, also is the home directory. Of course, if you search for errors, for example, to build the directory's parent directory does not exist, or the current process does not have the appropriate permissions, and so on, the system will call the relevant procedure for processing, for this kind of situation, this article is skipped. Linux system calls sys_mkdir to VFS directory tree, add a new node. At the same time as part of path search, introduces the following data structure: structnameidata {structdentry * dentry; structvfsmount*mnt; structqstrlast; unsignedintflags; intlast_type; }; This data structure in the path search process used to record information, plays a similar role of "road sign". The first two records in the dentry is to build the directory's parent directory information, mnt members will explain to the next. After the third record is the last node of the path (that is, to build the directory or file). Now for establish directory "/dev" and call sys_mkdir ("/dev", 0700), of which parameter 0700 we go it, it just limits will establish a pattern of the directory. Sys_mkdir function first calls the path_lookup ("/dev", LOOKUP_PARENT, & nd); to find the path, where nd to structnameidatand declared variables. In the next installment, because the function call of cumbersome, in order to highlight the main course will no longer be strictly in accordance with the call to the function. Path_lookup found "/dev" is beginning with "/", so it from the root directory of the current process begun to find the specific code is as follows: nd-> mnt = mntget (current-> fs-> rootmnt); nd->dentry=dget(current->fs->root); Remember that in init_mount_tree () function of the second half of the new building have VFS root directory-related information in the course of process init_task block, so in this scenario, nd-> mnt will point to a figure 3 mnt variables, nd-> dentry then points to the dentry in Figure 3. Then call the function path_walk then go find, locate the last pass variable nd returned information is nd.last.name = "dev", nd.last.len = 3, nd.last_type = LAST_NORM, nd dentry members in mnt and, in a scene or a previous set of values, and no change. Such a circle, just using nd record the relevant information, the actual directory building work has not really started, but the work of the front is next to create new nodes collection of the necessary information. Well, here is truly a new directory node, will begin, this is done by the function lookup_create, calls this function is passed two parameters: lookup_create (& nd, 1); the parameters of the previously mentioned nd is variable, parameter 1 indicates that you want to create a new directory. Here is the general process: new assigned a structdentry structure memory space used to record the dev directory information, the dentry structure will be attached to its parent directory, which is the figure 3 "/" directory corresponding dentry structure, consisting of linked list implementation of this relationship. Next you will be assigned a structinode structure. Inode i_sb and in the dentry d_sb respectively in points to figure 3 sb, on this view, in the same file system when you create the new directory does not need to reassign a superblock structure, because after all, they all belong to the same file system, so a file system is only one super block. In this way, when you call sys_mkdir successfully in VFS directory tree new build a directory "/dev", in Figure 3, a new relationship between the data structure is shown in Figure 4. Figure 4 the darker of the two rectangular block new_inode and new_entry is sys_mkdir () function in the newly allocated memory structure, as figure in mnt, sb, dentry, inode and structure, as Figure 3 in the appropriate data structure, their mutual link between unchanged (chart to avoid excessive link curve, omit some link relationships, such as mnt and sb, the link between the dentry, readers can figure 3 basis see Figure 4). Need to emphasize that, since the rootfs mount the file system is to the VFS tree, so it is in the process of sys_mkdirInevitably involved, in fact, throughout the process, the rootfs file system functions such as ramfs_lookup ramfs_mkdir, have been called before. Figure 4: VFS tree create a directory "dev" 6. at the VFS tree mount file systems in this section will describe the VFS of directory tree to a directory (installation point mountpoint) on Mount (mount) a file system. This procedure can be simply described as: will a device (dev_name) on a file system (file_system_type) installed VFS directory tree on a mount point (dir_name). It is to resolve the question is: will the VFS directory tree a directory operation into concrete installation onto which the corresponding actual file system. For example, if you will on hda2 root file system (assuming that the file system type ext2) installed in the previous section, in the newly established "/dev" directory (in this case, "/dev" directory becomes the installation point), then the installation succeeds should achieve such purpose, namely: VFS file system "/dev" directory "ls" command, that directive should be listed on hda2 ext2 file system in the root directory of all directories and files. Obviously, the key here is how the VFS tree "/dev" Directory operation instructions to install the ext2 file system of the real in the directive. Therefore, the description of how the conversion will take a core issue. In describing the reader may wish to own before, imagine how a Linux system. Remember: for a directory or file operations will end by the directory or file the corresponding inode structure i_op and i_fop points to a function table corresponds to the function to execute. Therefore, regardless of the final solution, you can imagine is bound to adopt the "/dev" directory in the inode i_op and calls to i_fop hda2 ext2 root file system on in the root directory inode i_fop i_op and in operation. Initial process by sys_mount () system call functions, the function prototype statement: asmlinkagelongsys_mount (char * dev_name, char *, char * type dir_name, unsignedlongflags, void * data); Where the parameter type char * to identify file system to be installed, type the string for the ext2 file system, the "ext2". Parameter flags to identify the installation mode, and the next data parameters, this article does not do it. In order to help the reader better understand this process, the authors use a concrete example to illustrate: as we prepare for future autonomous hard disk partition 2 (hda2) ext2 file system on installation into the front created "/dev" directory. So for sys_mount () function call is specific to: sys_mount ("hda2", "ext2", "/dev", …) ; This function in the from user memory space (userspace) parameter to the kernel space, call do_mount () function to start the real work of installed file system. Similarly, in order to describe and explain the primary process, the next shows will be in strict accordance with the specific details of the function call. Do_mount () function will first call the path_lookup () function to get the installation point for related information, as described in the process of creating the directory, the mount point information is eventually recorded in the type of a variable structnameidata, convenient for narrative, note that variable to nd. In this case when path_lookup () function returns, the information recorded in nd: nd.entry = new_entry; nd.mnt = mnt; here the variable as in Figure 3 and 4 below. Then, do_mount () function will call the parameter flags to determine call one of the following four functions: do_loopback do_remount (), (), (), do_add_mount do_move_mount (). In our current example, the system will call do_add_mount () function to the VFS tree installation point "/dev" install an actual file system. In do_add_mount (), mainly through two important things: first, get a new installation of regional blocks, and second, the new installation regional block joined the installation system linked list. They are called do_kern_mount () function and graft_tree () function. Here's a description may be a bit abstract, such as installation of regional blocks, installation systems such as linked lists, but don't worry, because they are the author's own definition of the concept, the following will have specialized chart interpretation, there will be clear. Do_kern_mount () function to do, is to establish a new installation of regional blocks, specific content in the previous sections VFS directory tree for already described earlier, will not repeat them here. Graft_tree () function do they do_kern_mount () function returns a variable of type structvfsmount added to the list of installed system, while at the same time also will graft_tree () to the newly allocated structvfsmount type variables into a hash table, we will see later. That way, when do_kern_mount () function returns, in Figure 4, on the basis ofThe new relationships between the data structure will be as shown in Figure 5. Among them, the red circle area inside the data structure is referred to as the installation of regional blocks, which may call for the installation of regional blocks e2_mnt, blue arrow curve shall constitute the so-called installation system linked list. In the function call after the formation of clarify the relationship between the data structure, let us go back to the beginning of this chapter, is about the issues referred to ext2 file system installed into a "/dev", on the directory operations on how to convert ext2 file system. See from Figure 5, sys_mount () function call does not directly change the/dev "directory corresponding inode (i.e. map new_inode variable) structure of i_op and i_fop pointer and"/dev the dentry (i.e. map new_dentry variable) structure still in VFS directory tree, and is not being hidden, and correspondingly, from hda2 ext2 file system on the root directory for the corresponding e2_entry nor is as original as I imagine to be VFS in the directory tree in place, so this new_dentry between transformation in the end is how to

No comments:

Post a Comment