Linux虚拟文件系统之文件系统安装(sys_mount())

文件系统安装解析
本文深入探讨了文件系统安装过程中的核心系统调用mount的工作原理,包括sys_mount服务例程及其实现细节,如数据和选项的复制、权限检查、文件系统安装点的处理等关键步骤。
对于前面的根目录文件系统的安装中涉及到了mount系统调用的调用,这里我们考虑一个文件系统将被安装在一个已经安装文件系统之上的情形,即调用mount系统调用实现。mount系统调用被用来安装一个普通文件系统,他的服务例程为sys_mount()。
   1. /*sys_mount系统调用*/  
   2. /*dev_name为待安装设备的路径名; 
   3. dir_name为安装点的路径名; 
   4. type是表示文件系统类型的字符串; 
   5. */  
   6. SYSCALL_DEFINE5(mount, char __user *, dev_name, char __user *, dir_name,  
   7.         char __user *, type, unsigned long, flags, void __user *, data)  
   8. {  
   9.     int ret;  
  10.     char *kernel_type;  
  11.     char *kernel_dir;  
  12.     char *kernel_dev;  
  13.     unsigned long data_page;  
  14.     /*从用户空间复制到系统空间*/  
  15.     ret = copy_mount_string(type, &kernel_type);  
  16.     if (ret < 0)  
  17.         goto out_type;  
  18.   
  19.     kernel_dir = getname(dir_name);  
  20.     if (IS_ERR(kernel_dir)) {  
  21.         ret = PTR_ERR(kernel_dir);  
  22.         goto out_dir;  
  23.     }  
  24.   
  25.     ret = copy_mount_string(dev_name, &kernel_dev);  
  26.     if (ret < 0)  
  27.         goto out_dev;  
  28.     /*用户空间复制到系统空间,拷贝整个页面*/  
  29.     ret = copy_mount_options(data, &data_page);  
  30.     if (ret < 0)  
  31.         goto out_data;  
  32.     /*操作主体*/  
  33.     ret = do_mount(kernel_dev, kernel_dir, kernel_type, flags,  
  34.         (void *) data_page);  
  35.   
  36.     free_page(data_page);  
  37. out_data:  
  38.     kfree(kernel_dev);  
  39. out_dev:  
  40.     putname(kernel_dir);  
  41. out_dir:  
  42.     kfree(kernel_type);  
  43. out_type:  
  44.     return ret;  
  45. }  

下面是主体实现

   1. /* 
   2.  * create a new mount for userspace and request it to be added into the 
   3.  * namespace's tree 
   4.  */  
   5. static int do_new_mount(struct path *path, char *type, int flags,  
   6.             int mnt_flags, char *name, void *data)  
   7. {  
   8.     struct vfsmount *mnt;  
   9.   
  10.     if (!type)  
  11.         return -EINVAL;  
  12.   
  13.     /* we need capabilities... */  
  14.     if (!capable(CAP_SYS_ADMIN))  
  15.         return -EPERM;  
  16.   
  17.     lock_kernel();  
  18.     /*处理实际的安装操作并返回一个新的安装文件系统 
  19.     描述符地址,使用get_fs_type扫描已经注册文件系统链表 
  20.     找到匹配的file_system_type实例。然后分配或获取sb结构 
  21.     并与mnt关联,初始化mnt并返回*/  
  22.     */  
  23.     mnt = do_kern_mount(type, flags, name, data);  
  24.     unlock_kernel();  
  25.     if (IS_ERR(mnt))  
  26.         return PTR_ERR(mnt);  
  27.     /*处理必要的锁定操作,确保一个文件系统不会重复装载到 
  28.     同一个位置,将文件系统整合到系统中*/  
  29.     return do_add_mount(mnt, path, mnt_flags, NULL);  
  30. }  

do_kern_mount函数在前面初始化中介绍过了,下面看do_add_mount函数用于将文件系统整合到系统中。

   1. /* 
   2.  * add a mount into a namespace's mount tree 
   3.  * - provide the option of adding the new mount to an expiration list 
   4.  */  
   5. int do_add_mount(struct vfsmount *newmnt, struct path *path,  
   6.          int mnt_flags, struct list_head *fslist)  
   7. {  
   8.     int err;  
   9.   
  10.     down_write(&namespace_sem);  
  11.     /* Something was mounted here while we slept */  
  12.     while (d_mountpoint(path->dentry) &&  
  13.            follow_down(path))  
  14.         ;  
  15.     err = -EINVAL;  
  16.     /*验证再改安装点上最近安装的文件系统是否任然指向 
  17.     当前命名空间*/  
  18.     if (!(mnt_flags & MNT_SHRINKABLE) && !check_mnt(path->mnt))  
  19.         goto unlock;  
  20.   
  21.     /* Refuse the same filesystem on the same mount point */  
  22.     err = -EBUSY;  
  23.     /*如果要安装的文件系统已经被安装在由系统调用的参数 
  24.     所指定的安装点上*/  
  25.     if (path->mnt->mnt_sb == newmnt->mnt_sb &&  
  26.         path->mnt->mnt_root == path->dentry)  
  27.         goto unlock;  
  28.   
  29.     err = -EINVAL;  
  30.     /*该安装点是一个符号链接*/  
  31.     if (S_ISLNK(newmnt->mnt_root->d_inode->i_mode))  
  32.         goto unlock;  
  33.   
  34.     newmnt->mnt_flags = mnt_flags;  
  35.     /*把新安装的文件系统对象插入到namespace链表、 
  36.     散列表以及父文件系统的子链表中*/  
  37.     if ((err = graft_tree(newmnt, path)))  
  38.         goto unlock;  
  39.   
  40.     if (fslist) /* add to the specified expiration list */  
  41.         list_add_tail(&newmnt->mnt_expire, fslist);  
  42.   
  43.     up_write(&namespace_sem);  
  44.     return 0;  
  45.   
  46. unlock:  
  47.     up_write(&namespace_sem);  
  48.     mntput(newmnt);  
  49.     return err;  
  50. }  

新安装的文件系统对象链入系统树。


   1. static int graft_tree(struct vfsmount *mnt, struct path *path)  
   2. {  
   3.     int err;  
   4.     if (mnt->mnt_sb->s_flags & MS_NOUSER)  
   5.         return -EINVAL;  
   6.   
   7.     if (S_ISDIR(path->dentry->d_inode->i_mode) !=  
   8.           S_ISDIR(mnt->mnt_root->d_inode->i_mode))  
   9.         return -ENOTDIR;  
  10.   
  11.     err = -ENOENT;  
  12.     mutex_lock(&path->dentry->d_inode->i_mutex);  
  13.     if (IS_DEADDIR(path->dentry->d_inode))  
  14.         goto out_unlock;  
  15.   
  16.     err = security_sb_check_sb(mnt, path);  
  17.     if (err)  
  18.         goto out_unlock;  
  19.   
  20.     err = -ENOENT;  
  21.     if (!d_unlinked(path->dentry))/*加入树*/  
  22.         err = attach_recursive_mnt(mnt, path, NULL);  
  23. out_unlock:  
  24.     mutex_unlock(&path->dentry->d_inode->i_mutex);  
  25.     if (!err)  
  26.         security_sb_post_addmount(mnt, path);  
  27.     return err;  
  28. }  

   1. /*将文件系统添加到父文件系统的命名空间中*/  
   2. static int attach_recursive_mnt(struct vfsmount *source_mnt,  
   3.             struct path *path, struct path *parent_path)  
   4. {  
   5.     LIST_HEAD(tree_list);  
   6.     struct vfsmount *dest_mnt = path->mnt;  
   7.     struct dentry *dest_dentry = path->dentry;  
   8.     struct vfsmount *child, *p;  
   9.     int err;  
  10.   
  11.     if (IS_MNT_SHARED(dest_mnt)) {  
  12.         err = invent_group_ids(source_mnt, true);  
  13.         if (err)  
  14.             goto out;  
  15.     }  
  16.     err = propagate_mnt(dest_mnt, dest_dentry, source_mnt, &tree_list);  
  17.     if (err)  
  18.         goto out_cleanup_ids;  
  19.   
  20.     if (IS_MNT_SHARED(dest_mnt)) {  
  21.         for (p = source_mnt; p; p = next_mnt(p, source_mnt))  
  22.             set_mnt_shared(p);  
  23.     }  
  24.   
  25.     spin_lock(&vfsmount_lock);  
  26.     if (parent_path) {  
  27.         detach_mnt(source_mnt, parent_path);  
  28.         attach_mnt(source_mnt, path);  
  29.         touch_mnt_namespace(parent_path->mnt->mnt_ns);  
  30.     } else {  
  31.         /*确保新的vfsmount实例的mnt_parent成员指向父文件系统 
  32.         的vfsmount实例,而mnt_mountpoint成员指向装载点在父文件 
  33.         系统中的dentry实例*/  
  34.         mnt_set_mountpoint(dest_mnt, dest_dentry, source_mnt);  
  35.         commit_tree(source_mnt);  
  36.     }  
  37.   
  38.     list_for_each_entry_safe(child, p, &tree_list, mnt_hash) {  
  39.         list_del_init(&child->mnt_hash);  
  40.         /*将新的mnt添加到全局散列表以及父文件系统mnt实例中  
  41.         的子文件系统链表*/  
  42.         commit_tree(child);  
  43.     }  
  44.     spin_unlock(&vfsmount_lock);  
  45.     return 0;  
  46.   
  47.  out_cleanup_ids:  
  48.     if (IS_MNT_SHARED(dest_mnt))  
  49.         cleanup_group_ids(source_mnt, NULL);  
  50.  out:  
  51.     return err;  
  52. }  



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值