Chinaunix首页 | 论坛 | 博客
  • 博客访问: 449253
  • 博文数量: 73
  • 博客积分: 3593
  • 博客等级: 中校
  • 技术积分: 912
  • 用 户 组: 普通用户
  • 注册时间: 2009-10-14 11:32
文章分类

全部博文(73)

文章存档

2013年(2)

2012年(20)

2011年(25)

2010年(12)

2009年(14)

分类: LINUX

2011-03-29 12:11:57

原文链接

发表于 2009-03-19 15:53 |

mount 文件系统

主要数据结构:

struct namespace {
        atomic_t                count;                /* 引用技术 */
        struct vfsmount *        root;        /* 根目录的 vfsmount */
        struct list_head        list;        /* 所有已经mount的 文件系统的 list */
        struct rw_semaphore        sem;        /* 读写信号量 */
};



struct vfsmount
{
        struct list_head mnt_hash;                /* 用于散列表的list */
        struct vfsmount *mnt_parent;        /* 指向父文件系统的 vfsmount */
        struct dentry *mnt_mountpoint;        /* mountpoint 的 dentry */
        struct dentry *mnt_root;                /* mount的文件系统的根目录的 dentry */
        struct super_block *mnt_sb;                /* 指向该文件系统的 superblock */
        struct list_head mnt_mounts;        /* 所有mount到该文件系统的 vfsmount 的 list */
        struct list_head mnt_child;                /* 子文件系统的 list  */
        atomic_t mnt_count;                                /* 引用计数 */
        int mnt_flags;                                        /* mount 的 flag */
        int mnt_expiry_mark;                        /* 到期标志 */
        char *mnt_devname;                                /* mount设备的名称 比如: /dev/dsk/hda1 */
        struct list_head mnt_list;                /* 在namespace上的vfsmount的 list */
        struct list_head mnt_fslink;       
        struct namespace *mnt_namespace; /* 指向mount文件系统的进程的 namespace */
};

struct nameidata {
        struct dentry        *dentry;                /* 目录的dentry */
        struct vfsmount *mnt;                        /* 目录所在的文件系统的 vfsmount */
        struct qstr        last;                                /* 在LOOKUP_PARENT 标志被设置时使用,存储路径中最后一个分量 */
        unsigned int        flags;                        /* 查找的 flags */
        int                last_type;                                /* 在LOOKUP_PARENT 标志被设置时使用,存储路径名的最后一个分量的类型,值见下面的enum */
        unsigned        depth;                                /* 符号链接的嵌套深度 */
        char *saved_names[MAX_NESTED_LINKS + 1];        /* 存储嵌套的符号链接所关联的路径名数组, 通过depth作为下标来索引 */

        /* Intent data */
        union {
                struct open_intent open;
        } intent;
};

/*
* Type of the last component on LOOKUP_PARENT
*/
enum {LAST_NORM, LAST_ROOT, LAST_DOT, LAST_DOTDOT, LAST_BIND};        /* nameidata 的 last_type 的值 */

/*
* The bitmask for a lookup event:
*  - follow links at the end
*  - require a directory
*  - ending slashes ok even for nonexistent files
*  - internal "there are more path compnents" flag
*  - locked when lookup done with dcache_lock held
*  - dentry cache is untrusted; force a real lookup
*/
#define LOOKUP_FOLLOW                 1
#define LOOKUP_DIRECTORY         2
#define LOOKUP_CONTINUE                 4
#define LOOKUP_PARENT                16
#define LOOKUP_NOALT                32
#define LOOKUP_REVAL                64
/*
* Intent data
*/
#define LOOKUP_OPEN                (0x0100)
#define LOOKUP_CREATE                (0x0200)
#define LOOKUP_ACCESS                (0x0400)



/*
* "quick string" -- eases parameter passing, but more importantly
* saves "metadata" about the string (ie length and the hash).
*
* hash comes first so it snuggles against d_parent in the
* dentry.
*/
struct qstr {
        unsigned int hash;                        /* hash值 */
        unsigned int len;                        /* 路径中分量名称的长度 */
        const unsigned char *name;        /* 路径中分量名称的字符串地址 */
};


--------------------------------------------------------------------------

需要补习的内容:

对于一个文件(在Linux下所有都是文件,包括目录等等) ,如何判断该文件 是不是目录,或者是不是符号链接, 是通过inode :

如果是目录,则一定有 inode->i_op->lookup 方法, 即 inode->i_op->lookup 一定不是NULL

如果是符号链接, 则一定有 inode->i_op->follow_link 方法,即 inode->i_op->follow_link 一定不是NULL




--------------------------------------------------------------------------

对于每一个 mount 的文件系统,都由一个 vfsmount 实例来表示。

对于每一个进程,都有自己的 namespace , 这可以理解为这个进程的地盘。
在这里,所有的文件系统都要挂上来统一管理, 如下所示:

ascii_vfsmount_1.gif

            图(1)


同时,对于所有的vfsmount,都存在于 一个hash table中,他们通过一个 hash 数组组织在一起:


ascii_vfsmount_2.gif
                                                                        
        图(2)


对于mount的文件系统,会有在一个文件系统上 mount 另外一个文件系统的情况,这种情况,可以称原文件系统为 父vfsmount, 对于mount上的文件系统,称之位子文件系统。
他们的关系如下:

ascii_vfsmount_3.gif

                                             

        图(3)

                                                               

下面我以一个例子来说明:
例如我们要mount一个设备 /dev/hdb1 到 /home/xpl 目录下
我们假设 /home/xpl 就是当前进程的根文件系统中的目录(即 home 和 xpl 都没有mount任何文件系统),

我们mount的时候,传入的参数有三个: 要mount的设备( /dev/hdb1 ) , 设备的文件系统 ( ext2 之类的), mount到什么目录 ( /home/xpl )

首先,我们要根据要mount的目录的路径名( 我们传入的只是路径名称的字符串),来找到mount的目录 disk 的dentry (即 mountpoint )
这个查找过程如下所示:
1. 首先确定查找路径的起始目录,要么是根目录,要么是当前目录。
        如果是根目录,则根目录的 dentry 和 vfsmount 的信息在: current->fs->root 和 current->fs->rootmnt
        如果是当前目录,则当前目录的 dentry 和 vfsmount 的信息在:current->fs->pwd 和 current->fs->pwdmnt
2. 然后,从路径的起始目录开始逐级的往下找。
        对于我们的例子,我们首先要查找根目录下的 home 目录( 就是要找到 home 目录的 dentry 和 vfsmount 对象)
        1) 首先在 hashtable 中查找,就是在上面的图(2)中的链表中找。
        2) 如果这个目录没有在 hashtable 中,则需要到磁盘上(我们假设根文件系统是在一个磁盘上,如果不是,就是去根文件系统对应的存储介质中查找)去查找
                        这通过调用 根目录的 inode 的 lookup 方法来查找。
                        通过根目录的 dentry->d_inode 得到根目录的inode,然后调用 inode->i_ops->lookup 方法,将要查找目录的名称作为参数传递进去。

3. 找到了第一个目录,下面的过程就是简单的递归了,最后,找到 目录 xpl 的 dentry 和 vfsmount

评分人数

    • scutan: 精品文章可用积分 + 10
__________________________________
http://blog.sina.com.cn/bytex

阅读(1278) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~