1. Data Structure
Vfsmount
Struct vfsmount {
Struct list_head mnt_hash; // hash table
Struct vfsmount * mnt_parent;/* FS we are mounted on */
Struct dentry * mnt_mountpoint;/* dentry ofmountpoint */
Struct dentry * mnt_root;/* root of the mounted tree */|
Struct super_block * mnt_sb;/* pointer to superblock */
Struct list_headmnt_mounts;/* List of children, anchored here */
Struct list_headmnt_child;/* and going throughtheir mnt_child */
Int mnt_flags;
/* 4 bytes hole on 64 bitsarches */
Const char * mnt_devname;/* Name of device e.g./dev/DSK/hda1 */
Struct list_head mnt_list;
Struct list_headmnt_expire;/* link in FS-specificexpiry list */
Struct list_headmnt_share;/* Circular List ofshared mounts */
Struct list_headmnt_slave_list;/* List of slave mounts */
Struct list_headmnt_slave;/* slave List entry */
Struct vfsmount * mnt_master;/* slave is onmaster-> mnt_slave_list */
Struct mnt_namespace * mnt_ns;/* containingnamespace */
Int mnt_id;/* Mount identifier */
Int mnt_group_id;/* peer groupidentifier */
/*
* We put mnt_count & mnt_expiry_mark atthe end of struct vfsmount
* To let these frequently modified fields in aseparate cache line
* (So that reads of mnt_flags wont Ping-pongon SMP machines)
*/
Atomic_t mnt_count;
Int mnt_expiry_mark;/* true if marked for expiry */
Int mnt_pinned;
Int mnt_ghosts;
Int mnt_writers;
};
Namespace
Struct mnt_namespace {
Atomic_t count;
Struct vfsmount * root;
Struct list_head list;
Wait_queue_head_t poll;
Int event;
};
2. Initialization
Void _ init mnt_init (void)
Init_rwsem (& namespace_sem );
Static struct kmem_cache * mnt_cache =
Kmem_cache_create ("mnt_cache", sizeof (struct vfsmount ),
0, slab_hwcache_align | slab_panic, null );
Static struct list_head * mount_hashtable = (struct list_head *) _ get_free_page (gfp_atomic );
For (u = 0; U Init_list_head (& mount_hashtable [u]);
// The following line initializes sysfs, which is not very good here, but because the Mount operation is used for initializing sysfs, sysfs is used for mounting other file systems, so you can only put it here.
Sysfs_init ();
Fs_kobj = kobject_create_and_add ("FS", null); // Add the FS node to sysfs
Init_rootfs ();
Init_mount_tree ();
Mnt_init finally calls the init_rootfs and init_mount_tree functions. Let's take a look at them.
2.1 init_rootfs
Int _ init init_rootfs (void)
Int err;
Err = bdi_init (& ramfs_backing_dev_info );
// Register the rootfs File System
Err = register_filesystem (& rootfs_fs_type );
Rootfs_fs_type is defined in FS/ramfs/inode. C.
Static struct file_system_type rootfs_fs_type = {
. Name = "rootfs ",
. Get_sb = rootfs_get_sb,
. Kill_sb = kill_litter_super,
};
2.2 init_mount_tree
Static void _ init init_mount_tree (void)
// Obtain the vfsmount structure of rootfs and set the super block and other information. See the following figure.
Structvfsmount * mnt = do_kern_mount ("rootfs", 0, "rootfs", null );
// Create a mnt_namespace Based on MNT. See the following figure.
Structmnt_namespace * NS = create_mnt_ns (mnt );
Init_task.nsproxy-> mnt_ns = ns;
Get_mnt_ns (NS );
Root. mnt = NS-> root;
Root. dentry = NS-> root-> mnt_root;
// Because the INIT process is not started when init_mount_tree is called, the current macro is determined by the stack pointer set at the beginning, which should be the taskstruct struct of init_task.
Set_fs_pwd (current-> FS, & root );
Set_fs_root (current-> FS, & root );
Do_kern_mount is used to allocate a vfsmount and initialize the super block.
Struct vfsmount * do_kern_mount (const char * fstype, intflags, const char * Name, void * Data)
Structfile_system_type * type = get_fs_type (fstype );
Structvfsmount * MNT;
Mnt = vfs_kern_mount (type, flags, name, data );
Put_filesystem (type );
Returnmnt;
Struct vfsmount * vfs_kern_mount (struct file_system_type * type, int flags, const char * Name, void * Data)
Structvfsmount * mnt = alloc_vfsmnt (name );
Type-> get_sb (type, flags, name, Data, MNT );
// Set mnt_mountpoint to mnt_root, that is, the mount point is your own
MNT-> mnt_mountpoint = mnt-> mnt_root;
// Set mnt_parent to itself
MNT-> mnt_parent = mnt;
Struct mnt_namespace * create_mnt_ns (struct vfsmount * MNT)
Structmnt_namespace * new_ns = alloc_mnt_ns ();
MNT-> mnt_ns = new_ns;
New_ns-> root = mnt;
List_add (& new_ns-> list, & new_ns-> root-> mnt_list );
3. File System Loading
The mount operations include remount, move_mount, Mount Block devices, and mountloop devices, all of which are completed by the kernel function do_mount. Essentially, the Mount operation creates a vfsmount structure and associates the structure with the mount point. After Association, the directory can be searched down at the vfsmount mount point level.
For more information about how to search for a file through vfsmount, see the kern_path function. The role of kern_path is to obtain the dentry and inode structures based on the path, which calls path_walk and finally calls _ follow_mount.
Next, let's take a look at the implementation of the Mount block device.
Long do_mount (char * dev_name, char * dir_name, char * type_page,
Unsignedlong flags, void * data_page)
Struct path;
// Obtain the vfsmount and dentry structures of the mount point and save them in structpath.
Kern_path (dir_name, lookup_follow, & Path );
...
// Call the block device to do_new_mount.
Do_new_mount (& Path, type_page, flags, mnt_flags, dev_name, data_page );
Static int do_new_mount (struct path * path, char * type, int flags, int mnt_flags, char * Name, void * Data)
Struct vfsmount * mnt = do_kern_mount (type, flags, name, data );
Do_add_mount (MNT, path, mnt_flags, null );
Int do_add_mount (struct vfsmount * newmnt, struct path * path, int mnt_flags, structlist_head * fslist)
// Find the vfsmount and dentry structures of the current mount point of the Directory
While (d_mountpoint (path-> dentry) & follow_down (PATH ));
// Graft_tree is mainly used to add the new vfsmount to mounttree.
Graft_tree (newmnt, PATH );
Similar to the Mount block device, loopmount creates a vfsmount structure based on the superblock and dentry structures of the loaded device and mounts the vfsmount to the mounttree.