手机版
你好,游客 登录 注册
背景:
阅读新闻

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

[日期:2012-02-11] 来源:Linux社区  作者:bullbat [字体:大 中 小]
[cpp]
  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函数用于将文件系统整合到系统中。

[cpp]
  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. }  

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

[cpp]
  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. }  
[cpp]
  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. }  
linux
相关资讯       Linux基础教程 
本文评论   查看全部评论 (0)
表情: 表情 姓名: 字数

       

评论声明
  • 尊重网上道德,遵守中华人民共和国的各项有关法律法规
  • 承担一切因您的行为而直接或间接导致的民事或刑事法律责任
  • 本站管理人员有权保留或删除其管辖留言中的任意内容
  • 本站有权在网站内转载或引用您的评论
  • 参与本评论即表明您已经阅读并接受上述条款