- /*
- * create a new mount for userspace and request it to be added into the
- * namespace's tree
- */
- static int do_new_mount(struct path *path, char *type, int flags,
- int mnt_flags, char *name, void *data)
- {
- struct vfsmount *mnt;
- if (!type)
- return -EINVAL;
- /* we need capabilities... */
- if (!capable(CAP_SYS_ADMIN))
- return -EPERM;
- lock_kernel();
- /*处理实际的安装操作并返回一个新的安装文件系统
- 描述符地址,使用get_fs_type扫描已经注册文件系统链表
- 找到匹配的file_system_type实例。然后分配或获取sb结构
- 并与mnt关联,初始化mnt并返回*/
- */
- mnt = do_kern_mount(type, flags, name, data);
- unlock_kernel();
- if (IS_ERR(mnt))
- return PTR_ERR(mnt);
- /*处理必要的锁定操作,确保一个文件系统不会重复装载到
- 同一个位置,将文件系统整合到系统中*/
- return do_add_mount(mnt, path, mnt_flags, NULL);
- }
do_kern_mount函数在前面初始化中介绍过了,下面看do_add_mount函数用于将文件系统整合到系统中。
- /*
- * add a mount into a namespace's mount tree
- * - provide the option of adding the new mount to an expiration list
- */
- int do_add_mount(struct vfsmount *newmnt, struct path *path,
- int mnt_flags, struct list_head *fslist)
- {
- int err;
- down_write(&namespace_sem);
- /* Something was mounted here while we slept */
- while (d_mountpoint(path->dentry) &&
- follow_down(path))
- ;
- err = -EINVAL;
- /*验证再改安装点上最近安装的文件系统是否任然指向
- 当前命名空间*/
- if (!(mnt_flags & MNT_SHRINKABLE) && !check_mnt(path->mnt))
- goto unlock;
- /* Refuse the same filesystem on the same mount point */
- err = -EBUSY;
- /*如果要安装的文件系统已经被安装在由系统调用的参数
- 所指定的安装点上*/
- if (path->mnt->mnt_sb == newmnt->mnt_sb &&
- path->mnt->mnt_root == path->dentry)
- goto unlock;
- err = -EINVAL;
- /*该安装点是一个符号链接*/
- if (S_ISLNK(newmnt->mnt_root->d_inode->i_mode))
- goto unlock;
- newmnt->mnt_flags = mnt_flags;
- /*把新安装的文件系统对象插入到namespace链表、
- 散列表以及父文件系统的子链表中*/
- if ((err = graft_tree(newmnt, path)))
- goto unlock;
- if (fslist) /* add to the specified expiration list */
- list_add_tail(&newmnt->mnt_expire, fslist);
- up_write(&namespace_sem);
- return 0;
- unlock:
- up_write(&namespace_sem);
- mntput(newmnt);
- return err;
- }
新安装的文件系统对象链入系统树。
- static int graft_tree(struct vfsmount *mnt, struct path *path)
- {
- int err;
- if (mnt->mnt_sb->s_flags & MS_NOUSER)
- return -EINVAL;
- if (S_ISDIR(path->dentry->d_inode->i_mode) !=
- S_ISDIR(mnt->mnt_root->d_inode->i_mode))
- return -ENOTDIR;
- err = -ENOENT;
- mutex_lock(&path->dentry->d_inode->i_mutex);
- if (IS_DEADDIR(path->dentry->d_inode))
- goto out_unlock;
- err = security_sb_check_sb(mnt, path);
- if (err)
- goto out_unlock;
- err = -ENOENT;
- if (!d_unlinked(path->dentry))/*加入树*/
- err = attach_recursive_mnt(mnt, path, NULL);
- out_unlock:
- mutex_unlock(&path->dentry->d_inode->i_mutex);
- if (!err)
- security_sb_post_addmount(mnt, path);
- return err;
- }
- /*将文件系统添加到父文件系统的命名空间中*/
- static int attach_recursive_mnt(struct vfsmount *source_mnt,
- struct path *path, struct path *parent_path)
- {
- LIST_HEAD(tree_list);
- struct vfsmount *dest_mnt = path->mnt;
- struct dentry *dest_dentry = path->dentry;
- struct vfsmount *child, *p;
- int err;
- if (IS_MNT_SHARED(dest_mnt)) {
- err = invent_group_ids(source_mnt, true);
- if (err)
- goto out;
- }
- err = propagate_mnt(dest_mnt, dest_dentry, source_mnt, &tree_list);
- if (err)
- goto out_cleanup_ids;
- if (IS_MNT_SHARED(dest_mnt)) {
- for (p = source_mnt; p; p = next_mnt(p, source_mnt))
- set_mnt_shared(p);
- }
- spin_lock(&vfsmount_lock);
- if (parent_path) {
- detach_mnt(source_mnt, parent_path);
- attach_mnt(source_mnt, path);
- touch_mnt_namespace(parent_path->mnt->mnt_ns);
- } else {
- /*确保新的vfsmount实例的mnt_parent成员指向父文件系统
- 的vfsmount实例,而mnt_mountpoint成员指向装载点在父文件
- 系统中的dentry实例*/
- mnt_set_mountpoint(dest_mnt, dest_dentry, source_mnt);
- commit_tree(source_mnt);
- }
- list_for_each_entry_safe(child, p, &tree_list, mnt_hash) {
- list_del_init(&child->mnt_hash);
- /*将新的mnt添加到全局散列表以及父文件系统mnt实例中
- 的子文件系统链表*/
- commit_tree(child);
- }
- spin_unlock(&vfsmount_lock);
- return 0;
- out_cleanup_ids:
- if (IS_MNT_SHARED(dest_mnt))
- cleanup_group_ids(source_mnt, NULL);
- out:
- return err;
- }
