Chinaunix首页 | 论坛 | 博客
  • 博客访问: 173551
  • 博文数量: 33
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 305
  • 用 户 组: 普通用户
  • 注册时间: 2014-10-07 15:33
文章存档

2015年(23)

2014年(10)

分类: 嵌入式

2015-01-07 11:24:38

对于前面的根目录文件系统的安装中涉及到了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. long do_mount(char *dev_name, char *dir_name, char *type_page,  
  2.           unsigned long flags, void *data_page)  
  3. {  
  4.     struct path path;  
  5.     int retval = 0;  
  6.     int mnt_flags = 0;  
  7.   
  8.     /* Discard magic */  
  9.     if ((flags & MS_MGC_MSK) == MS_MGC_VAL)  
  10.         flags &= ~MS_MGC_MSK;  
  11.   
  12.     /* Basic sanity checks */  
  13.   
  14.     if (!dir_name || !*dir_name || !memchr(dir_name, 0, PAGE_SIZE))  
  15.         return -EINVAL;  
  16.   
  17.     if (data_page)  
  18.         ((char *)data_page)[PAGE_SIZE - 1] = 0;  
  19.   
  20.     /* Default to relatime unless overriden */  
  21.     if (!(flags & MS_NOATIME))  
  22.         mnt_flags |= MNT_RELATIME;  
  23.   
  24.     /* Separate the per-mountpoint flags */  
  25.     if (flags & MS_NOSUID)  
  26.         mnt_flags |= MNT_NOSUID;  
  27.     if (flags & MS_NODEV)  
  28.         mnt_flags |= MNT_NODEV;  
  29.     if (flags & MS_NOEXEC)  
  30.         mnt_flags |= MNT_NOEXEC;  
  31.     if (flags & MS_NOATIME)  
  32.         mnt_flags |= MNT_NOATIME;  
  33.     if (flags & MS_NODIRATIME)  
  34.         mnt_flags |= MNT_NODIRATIME;  
  35.     if (flags & MS_STRICTATIME)  
  36.         mnt_flags &= ~(MNT_RELATIME | MNT_NOATIME);  
  37.     if (flags & MS_RDONLY)  
  38.         mnt_flags |= MNT_READONLY;  
  39.   
  40.     flags &= ~(MS_NOSUID | MS_NOEXEC | MS_NODEV | MS_ACTIVE |  
  41.            MS_NOATIME | MS_NODIRATIME | MS_RELATIME| MS_KERNMOUNT |  
  42.            MS_STRICTATIME);  
  43.   
  44.     /* ... and get the mountpoint */  
  45.     /*获得安装点path结构*/  
  46.     retval = kern_path(dir_name, LOOKUP_FOLLOW, &path);  
  47.     if (retval)  
  48.         return retval;  
  49.       
  50.     retval = security_sb_mount(dev_name, &path,  
  51.                    type_page, flags, data_page);  
  52.     if (retval)  
  53.         goto dput_out;  
  54.   
  55.     if (flags & MS_REMOUNT)  
  56.         /*修改已经存在的文件系统参数,即改变超级块对象s_flags 
  57.     字段的安装标志*/  
  58.         retval = do_remount(&path, flags & ~MS_REMOUNT, mnt_flags,  
  59.                     data_page);  
  60.     else if (flags & MS_BIND)  
  61.             /*要求在系统目录树的另一个安装点上得文件或目录能够可见*/  
  62.         retval = do_loopback(&path, dev_name, flags & MS_REC);  
  63.     else if (flags & (MS_SHARED | MS_PRIVATE | MS_SLAVE | MS_UNBINDABLE))  
  64.         /*is responsible for handling shared, slave, and unbindable mounts by changing 
  65.         the mount flags or building up the required data structure connections  
  66.         between the vfsmount 
  67.         instances involved.*/  
  68.         retval = do_change_type(&path, flags);  
  69.     else if (flags & MS_MOVE)  
  70.         /*改变已安装文件的安装点*/  
  71.             /*used to move a mounted filesystem*/  
  72.         retval = do_move_mount(&path, dev_name);  
  73.     else  
  74.         /*handles normal mount operations. This is the default situation, so no special  
  75.         flags 
  76.         are required*/  
  77.         /*当用户要求安装一个特殊文件系统或存放在磁盘分区 
  78.         中的普通文件系统时,调用*/  
  79.         retval = do_new_mount(&path, type_page, flags, mnt_flags,  
  80.                       dev_name, data_page);  
  81. dput_out:  
  82.     path_put(&path);  
  83.     return retval;  
  84. }  
  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. }  
阅读(1828) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~