[File System] File System Study Notes (8) --- Mount System Call (Code-related)

Source: Internet
Author: User

1. Mount System Call-related code
Source code location: do_mount () function of the kernel/fs/namespace. c file,

[CPP]View plaincopy
  1. Long do_mount (char * dev_name, char * dir_name, char * type_page, unsigned long flags, void * data_page)


Dev_name refers to the name of the file system to be mounted, such as tmpfs,
Dir_name refers to the target directory to which the file system will be mounted.
Type_page indicates the type of the file system to be mounted.
Flags indicates mounting options, such as ms_rdonly.
Data_page refers to some additional options, such as the wait keyword.


1. The do_mount () function will first perform some parameter checks. dir_name cannot be blank and the size cannot exceed the size of a page. This truncates the part where data_page exceeds the size of a page.

If (! Memchr (dir_name, 0, page_size ))

 

Check whether the length of data_page exceeds one page. If it exceeds the length, the excess part is truncated.

If (data_page)
(Char *) data_page) [page_size-1] = 0;

 

The do_mount () function first calls the kern_path () function to convert dir_name to the struct path struct,

[CPP]View plaincopy
  1. Struct path {
  2. Struct vfsmount * MNT;
  3. Struct dentry * dentry;
  4. };


2. Set different mnt_flags temporary variables according to different flag parameters. If the flag does not contain ms_remount, ms_bind, ms_move, ms_shared, ms_private, and so on, the final call will be made.
Do_new_mount () function.


3, do_new_mount () function

[CPP]View plaincopy
  1. Static int do_new_mount (struct path * path, char * type, int flags, int mnt_flags, char * Name, void * Data)


The path parameter is the path struct after dir_name is converted by kern_path ().
The type parameter indicates the type of the file system.
The flags parameter indicates some mount options.
The mnt_flags parameter refers to some monut options.
The name parameter indicates the name of the file system to be mounted, such as tmpfs.
The data parameter refers to some additional options, such as the wait keyword.


The do_new_mount () function first calls the do_kern_mount (type, flags, name, data) function. The function is used to create a new block area and obtain a vfsmount instance, obtain the vfsmount structure of the source file system and load it to the system by operating the specific file system. Return the root directory of the mount point and call do_add_mount (real_mount (mnt), path, mnt_flags) function, which is used to mount the Mount instance to the Mount tree and add the source file system to the target file system.

 

[CPP]View plaincopy
  1. 1651 static int do_new_mount (struct path * path, char * type, int flags,
  2. 1652 int mnt_flags, char * Name, void * Data)
  3. 1653 {
  4. 1654 struct vfsmount * MNT;
  5. 1655
  6. 1656 if (! Type)
  7. 1657 return-einval;
  8. 1658
  9. 1659/* We need capabilities ...*/
  10. 1660 if (! Capable (cap_sys_admin ))
  11. 1661 return-eperm;
  12. 1662
  13. 1663 lock_kernel ();
  14. 1664 mnt = do_kern_mount (type, flags, name, data );
  15. 1665 unlock_kernel ();
  16. 1666 if (is_err (mnt ))
  17. 1667 return ptr_err (mnt );
  18. 1668
  19. 1669 return do_add_mount (MNT, path, mnt_flags, null );
  20. 1670}


Do_kern_mount () function details: do_kern_mount () first calls the get_fs_type () function to return the file_system_type instance of the file system to be mounted. file_system_type is registered to the kernel when the file system is started, all registered file systems form a single-chain table, and do_kern_mount () calls the vfs_kern_mount () function. The vfs_kern_mount () function is used to allocate a struct Mount struct, and then vfs_kern_mount () call the Mount member function of the file_system_type structure of each file system (for example, ext4 calls the ext4_mount function). The function is used to create a super fast object of the file system, return the dentry instance of the root directory (Root) of the file system, and assign the created super fast object to the newly created vfsmount structure, at the same time, the mnt_root point in vfsmount is assigned as the root dentry in super fast.

Do_add_mount () function details. This function is used to add the current Mount instance to the Mount tree. The two key points of the do_add_mount () function are: lock_mount () function and graft_tree () function, lock_mount () function () check if another file system has been mounted to the directory to be mounted, perform a file system switchover. Graft means grafting, the directory tree to be mounted is connected to the directory tree of the file system of the current directory, similar to the grafting technology, but the directory tree of the original file system is not damaged.

The lock_mount () function mainly calls the lookup_mnt () function. This function returns a struct vfsmount instance. The lookup_mnt () function calls the _ lookup_mnt () function to return a struct Mount instance, the same directory under the same parent file system can be used as a mount point for multiple sub-file systems. Therefore, if multiple sub-file systems are mounted, then these sub-file systems will be placed on the same linked list in the hash table through the hash function. The _ lookup_mnt () function returns the Mount instance of the last mounted file system in the directory. The _ lookup_mnt () function is as follows:

[CPP]View plaincopy
  1. 414 struct vfsmount * _ lookup_mnt (struct vfsmount * MNT, struct dentry * dentry,
  2. 415 int DIR)
  3. 416 {
  4. 417 struct list_head * head = mount_hashtable + Hash (MNT, dentry );
  5. 418 struct list_head * TMP = head;
  6. 419 struct vfsmount * P, * found = NULL;
  7. 420
  8. 421 (;;){
  9. 422 TMP = dir? TMP-> next: TMP-> Prev;
  10. 423 P = NULL;
  11. 424 if (TMP = head)
  12. 425 break;
  13. 426 P = list_entry (TMP, struct vfsmount, mnt_hash );
  14. 427 If (p-> mnt_parent = mnt & P-> mnt_mountpoint = dentry ){
  15. 428 found = P;
  16. 429 break;
  17. 430}
  18. 431}
  19. 432 return found;
  20. 433}


Implementation Details of the graft_tree () function. The graft_tree () function mainly calls the attach_recursive_mnt () function, static int struct (struct Mount * source_mnt, struct path * path, struct path * parent_path ), the main operations performed by the attach_recursive_mnt () function are 1. use mnt_set_mountpoint () to point mnt_parent in the Child vfsmount to the parent vfsmount, and point mnt_mountpoint of the child vfsmount to the mount point dentry in the parent file system; 2. use commit_tree () to add the Sub-File System to the file system hash table of the kernel, and add the Sub-File System to the sub-file system linked list corresponding to the parent file system;
The commit_tree () function is used to set the namespace of the current file system as the parent namespace. The parent vfsmount is obtained through mnt_parent in the current vfsmount and connected to the parent namespace linked list. 2. Add the current vfsmount to the conflicting linked list corresponding to the hash value. The hash value is calculated by hash. Mnt_hash is used as the linked list element. 3. Add the current vfsmount to the sub-file system chain mnt_mounts corresponding to the parent vfsmount. Mnt_child serves as the linked list element.

[File System] File System Study Notes (8) --- Mount System Call (Code-related)

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.