VFS (2)

Source: Internet
Author: User

A few important data structures in VFS have been described in the previous article, so let's look at what happens when other file systems are mounted to the above directory.

I borrowed a graph to indicate the relationship between the first file system after it was mounted.


The kernel first mounts the filesystem for Rootfs, the relationship between its important data structures is already very clear, it has its own root directory, assuming I want to create a/dev directory in the root directory of this filesystem, then the relationship between the data structures such as:


Because the freshmen become a directory, so there will be a new_inode, and there will be a new_dentry, the new directory entry structure called "Dev", its d_parent is the parent directory is dentry, that is, the root directory "/", now has the/dev, we want to in this directory On mount an actual file system, EXT4, then after mounting, the structural relationships between them are as follows:


Because it is a new file system, it will have its own Super block (E2_SB), because it is mounted to Rootfs's/dev directory, so it will produce a Mount structure Vfsmount (E2_MNT), and E2_mnt->mnt_moun Tpoint points to the directory it is mounted on, new_dentry, and then sets new_dentry->d_mounted to 1, which also produces its own root directory E2_entry and node object E2_inode for that directory entry.
With such a structure, if you want to access a file in the new file system/dev/tmp/file, first, through the path traversal, will be rootfs the/dev directory of the directory entry structure new_dentry, the results found that its d_mounted is 1, indicates that there are other file systems attached to the directory, that is, EXT4, at this time through an algorithm, get mounted to the directory under the E2_mnt, and then get e2_mnt->mnt_root, that is, the new file system root directory, and then you can continue to find, The whole process is seamless.
Then you may have to ask, where is the rootfs attached? That is how the root directory entry is created?
This is really a problem with an egg first or chicken first, because Rootfs is a memory file system that, when the kernel loads, passes through the fs/namespace.c Mnt_init last Init_rootfs () and Init_mo Unt_tree () to initialize. The Rootfs file system is registered in the Init_rootfs and is mounted on the init_mount_tree. Let the source to tell us the truth.
In Init_mount_tree (), Do_kern_mount ("Rootfs", 0, "Rootfs", NULL) is called to Mount Rootfs, and the core method is Vfs_kern_mount, which creates a vfsmount struct to represent a Mount point, which is then called, when registering Rootfs, the GET_SB in File_system_type to let the Rootfs file system drive itself to generate a super block to populate the Vfsmount, (here you can see the flexibility of VFS, because a Some file systems may not have super block, so when the file system itself implements the function, it can virtual a super blocks, the mystery is here. For Rootfs in the GET_SB is actually implemented by the function ROOTFS_GET_SB, the core function is Get_sb_nodev, it will create a super block, and call the Fill_super function pointer, here it is Ramfs_fil L_super to achieve. The code is as follows:

   static int Ramfs_fill_super (struct super_block * sb, void * data, int silent) {struct Inode * inode;struct dentry * ROOT;SB ->s_maxbytes = Max_lfs_filesize;sb->s_blocksize = Page_cache_size;sb->s_blocksize_bits = PAGE_CACHE_SHIFT; Sb->s_magic = Ramfs_magic;sb->s_op = &ramfs_ops;sb->s_time_gran = 1;inode = Ramfs_get_inode (sb, S_IFDIR | 07 0), if (!inode) Return-enomem;root = D_alloc_root (inode), if (!root) {iput (inode); return-enomem;} Sb->s_root = Root;return 0;}
It initializes the super block just now and creates an inode, and a dentry (root), and the sb->s_root = root; That is, the root directory of the Rootfs. There's still no telling where the filesystem is mounted, but Rootfs's important data structures are already clear. Then look back to Get_sb_nodev, which will call simple_set_mnt.


int simple_set_mnt (struct vfsmount *mnt, struct super_block *sb) {mnt->mnt_sb = Sb;mnt->mnt_root = Dget (sb->s_ root); return 0;}
It sets the root of the mount point, and when the path is traversed, when the directory is mounted and the mount point is found, the root directory of the mounted file system is known.
And then go back to the Vfs_kern_mount, when the Super block gets, start setting the mount point structure,
Mnt->mnt_mountpoint = mnt->mnt_root;
Mnt->mnt_parent = MNT;
These two important code to solve the truth, it is set to mount the structure of the mounted directory when set to its own root directory, that is, the root directory created in Rootfs, both as its root directory, as well as its mount directory, the VFS root directory, so that it successfully became the first VFS The root of the.
At this time, the root of the VFS and the first file system has been initialized, but generally we will also mount a root file system, so that Linux can really run up, assuming that the mount is mounted as a disk containing EXT4 the first partition, in the author's source code, it will first mount to Rootfs/root below, with Rootfs, all this is very simple, after loading EXT4, set the initialization task of the current directory is the root of ext4, equivalent to the previous e2_entry, these work is mainly done in Prepare_namespace. After setting the current directory for the initial task, but not setting the root directory, the root directory is set by the following code in Prepare_namespace.
Mount_root ();
Sys_mount (".", "/", NULL, ms_move, NULL);
Sys_chroot (".");
Mount_root loaded the root file system to/root, that is, EXT4, but then moved to the root of the VFS, and rootfs root directory, overwriting the Rootfs, and then set the initial task of the root directory for the current directory, that is, e2_entry, that is, the root directory of EXT4, Equivalent to the root directory of the VFS. Because if the root file system is not moved, the current directory and root directory of the initial task is set to/root, then if the derived process inherits the two, they can find the regular Linux directory through the normal/bin/xxx, but in case it does not inherit to these two, or the two items of task 0 are not set correctly, Then using/BIN/XXX traversal, in fact, is from the root of rootfs traversal, so there will be problems.
And the VFS all flexible, because many operations are provided in the form of pointers to file system developers to achieve, so as to maximize the flexibility, interested friends can view the open mkdir read interface, to feel its strong.
VFS is still relatively complex, the two articles just mentioned a small part, if interested, the source code has you want to know the details.


VFS (2)

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.