linux vfs 解析 之 mount 文件系统

来源:互联网 发布:南方舆情数据研究院 编辑:程序博客网 时间:2024/07/24 01:20

linux vfs 解析 之 mount 文件系统

http://blog.sina.com.cn/s/blog_5219094a0100calt.html

http://blog.sina.com.cn/s/blog_5219094a0100caly.html

http://blog.sina.com.cn/s/blog_5219094a0100cam0.html


linux vfs 解析 之 mount 文件系统 

mount 文件系统主要数据结构:

struct namespace {
 atomic_t  count;  
 struct vfsmount * root; 
 struct list_head list; 
 struct rw_semaphore sem; 
};

 

struct vfsmount
{
 struct list_head mnt_hash;  
 struct vfsmount *mnt_parent; 
 struct dentry *mnt_mountpoint; 
 struct dentry *mnt_root;  
 struct super_block *mnt_sb;  
 struct list_head mnt_mounts; 
 struct list_head mnt_child;  
 atomic_t mnt_count;    
 int mnt_flags;     
 int mnt_expiry_mark;   
 char *mnt_devname;    
 struct list_head mnt_list;  
 struct list_head mnt_fslink; 
 struct namespace *mnt_namespace;
};

struct nameidata {
 struct dentry *dentry;  
 struct vfsmount *mnt;   
 struct qstr last;    
 unsigned int flags;   
 int  last_type;    
 unsigned depth;    
 char *saved_names[MAX_NESTED_LINKS + 1]; 

 
 union {
  struct open_intent open;
 } intent;
};


enum {LAST_NORM, LAST_ROOT, LAST_DOT, LAST_DOTDOT, LAST_BIND}; 


#define LOOKUP_FOLLOW   1
#define LOOKUP_DIRECTORY  2
#define LOOKUP_CONTINUE   4
#define LOOKUP_PARENT  16
#define LOOKUP_NOALT  32
#define LOOKUP_REVAL  64

#define LOOKUP_OPEN  (0x0100)
#define LOOKUP_CREATE  (0x0200)
#define LOOKUP_ACCESS  (0x0400)

 


struct qstr {
 unsigned int 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 , 这可以理解为这个进程的地盘。
在这里,所有的文件系统都要挂上来统一管理, 如下所示:

           task_struct         
         +-------------+       
         |             |       
         +-------------+       
         | name_space  |-------\
         +-------------+       |                                                 
         |             |       |                                                 
         +-------------+       |        namespace                                
         |             |       \------>+---------+                               
                                       |  count  |                               
                                       +---------+                               
                                       |  root   |                               
                                       +---------+                               
          /--------------------------->|  list   |<-----------------------\      
          |                            +---------+                        |      
          |                            |  sem    |                        |      
          |                            +---------+                        |      
          |                                                               |      
          |                                                               |      
          |        vfsmount                               vfsmount        |      
          |     +------------+                         +------------+     |      
          |     | mnt_hash   |                         | mnt_hash   |     |      
          |     +------------+                         +------------+     |      
          \---->| mnt_list   |<-----   ......   ------>| mnt_list   |<----/      
                +------------+                         +------------+            
                | mnt_mounts |                         | mnt_mounts |            
                +------------+                         +------------+            
                | mnt_child  |                         | mnt_child  |            
                +------------+                         +------------+            
                |            |                         |            |            
                                                                            
        图(1)


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


                                                                       
                                                                       
                 +-------------+-------------+--------------+-------    
 mount_hashtable | list_head_0 | list_head_1 | list_head_2  |......     
                 +---/---\-----+-------------+--------------+-------    
                    |   |                                              
                    |   |                                              
                    |   |                                              
                    |   |                                              
                    |   |                                              
                    |   |                                              
                    |   |                                              
                    |   |                                              
      /-------------/   \------------------------------\               
      |                                                |               
      |                                                |               
      |                                                |               
      |      vfsmount                 vfsmount         |               
      |    +----------+             +----------+       |               
      \--->| mnt_hash |<--- ... --->| mnt_hash |<------/               
           +----------+             +----------+                       
           | mnt_list |             |          |                       
           +----------+             +----------+                       
           |          |             |          |                       
           +----------+             +----------+                       
           |          |             |          |                       
           +----------+             +----------+                       
           |          |             |          |                       
                                                                       
      图(2)


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

                                            
                                 vfsmount   
                              +------------+
                              | mnt_hash   |
                              +------------+
                             >| mnt_list   |<                          
                              +------------+                           
       /--------------------->| mnt_mounts |<------------------------\ 
       |                      +------------+                         | 
       |                      | mnt_child  |                         | 
       |                      +------------+                         | 
       |                      |            |                         | 
       |                                                             | 
       |                                                             | 
       |                                                             | 
       |                                                             | 
       |        vfsmount                            vfsmount         | 
       |     +------------+                      +------------+      | 
       |     | mnt_hash   |                      | mnt_hash   |      | 
       |     +------------+                      +------------+      | 
       |     | mnt_list   |                      | mnt_list   |      | 
       |     +------------+                      +------------+      | 
       |     | mnt_mounts |                      | mnt_mounts |      | 
       |     +------------+                      +------------+      | 
       |-----| mnt_child  |<-----  ......  ------| mnt_child  |<-----/ 
       \     +------------+                      +------------+        
             |            |                      |            |
        图(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


找到了要 mount 的目录,下面就开始实际的mount过程
mount的过程就是把设备的文件系统加入到 vfs 框架中
1. 首先,要mount一个新的设备,需要创建一个新的 super block。
 这通过要mount的文件系统的 file_system_type, 调用其 get_sb 方法来创建一个新的 super block

2. 对于任何一个 mount 的文件系统,都要有一个 vfsmount, 创建这个vfsmount, 并设置好其属性(就是 vfsmount 中的各个成员)

3. 将创建好的 vfsmount 加入到系统中。

整个过程如下所示:
                                                                             
                                                                                    
                                                                                    
 /---------> /--->  dentry ("/")  <-----------\                                     
           |    +--------------+            |                                     
           \----|  d_parent    |            |                                     
                +--------------+            |                                     
                |  d_child     |            |                                     
                +--------------+            |                                     
      /-------->|  d_subdirs   |<-----------|-------\                             
      |         +--------------+            |       |                             
      |         |  d_mounted   |            |       |                             
      |         +--------------+            |       |                             
      |         |              |            |       |                             
      |                                     |       |                             
      |                                     |       |                             
      |                                     |       |                             
      |                                     |       |                             
      |                                     |       |   
      |                                     |       |   
      |           dentry ("home") <-\       |       |   
      |         +--------------+    |       |       |   
      |         |  d_parent    |----|-------/       |   
      |         +--------------+    |               |   
      \-------->|  d_child     |<---|- ...... ------/   
                +--------------+    |                   
        /------>|  d_subdirs   |<---|-------------\     
        |       +--------------+    |             |     
        |       |  d_mounted   |    |             |     
        |       +--------------+    |             |     
        |       |              |    |             |     
        |                           |             |     
        |                           |             |
        |                           |             |
        |                           |             |
        |                           |             |
        |                           |             |
        |                           |             |
        |                           |             |
        |         dentry ("xpl") <--------------------------------------\
        |       +--------------+    |             |                     |
        |       |  d_parent    |----/             |                     |
        |       +--------------+                  |                     |
        \------>|  d_child     |<---- ......  ----/                     |
                +--------------+                                        |
                |  d_subdirs   |                                        |
                +--------------+                                        |
                |  d_mounted   |                                        |
                +--------------+                                        |
                |              |                                        |
                                                                        |
                                                                        |
                                                                        |
                             task_struct                                |
                           +-------------+                              |
                           |             |                              |
                           +-------------+                              |
                           | name_space  |---------\                    |
                           +-------------+         |                    |
           fs_struct <-----|     fs      |         |                    |
         +----------+      +-------------+         |                    |
 +----<----|  root    |      |             |         |                    |
         +----------+                              |                    |
         |  pwd     |                              |                    |  
         +----------+                              |                    |  
  /------|  rootmnt |                              |                    |  
  |      +----------+                              |                    |  
  |      |  pwdmnt  |                namespace <---/                    |  
  |      +----------+               +---------+                         |  
  |      |          |               |  count  |                         |  
  |                                 +---------+                         |  
  |                                 |  root   |                         |  
  |                                 +---------+                         |  
  |    /--------------------------->|  list   |<-------------------\    |  
  |    |                            +---------+                    |    |  
  |    |                            |  sem    |                    |    |  
  |    |                            +---------+                    |    |  
  |    |                                                           |    |  
  |    |                                                           |    |  
  |    |                                                           |    |  
  |    |                                                           |    |  
  |    |                                                           |    |  
  \---------->     vfsmount     <----\            vfsmount(new)    |    |  
       |      +---------------+      |       +---------------+     |    |  
       \------|   mnt_list    |<-----|------>|   mnt_list    |<----/    |  
              +---------------+      |       +---------------+          |  
              |   mnt_parent  |      \-------|   mnt_parent  |          |  
              +---------------+              +---------------+          |  
              | mnt_mountpoint|              | mnt_mountpoint|----------/  
              +---------------+              +---------------+             
 \-----<--------|   mnt_root    |              |   mnt_root    |------------\
                +---------------+              +---------------+            |
       /--------|   mnt_mounts  |<----\        |   mnt_mounts  |            |
       |        +---------------+     |        +---------------+            |
       |        |   mnt_sb      |     |        |   mnt_sb      |--------\   |
       |        +---------------+     |        +---------------+        |   |
       |        |   mnt_child   |     \------->|   mnt_child   |<---\   |   |
       |        +---------------+              +---------------+    |   |   |
       |        |               |              |               |    |   |   |
       |        |               |              |               |    |   |   |
       |                                                            |   |   |
       |                                                            |   |   |
       |                                                            |   |   |
       \------------------------------------------------------------/   |   |
                                                                        |   |
                                                                        |   |
                                                                        |   |
                                                                        |   |
                                                                        |   |
                                                                        |   |
                                                                        |   |
                                                                        |   |
                                                                        |   |
                           super_block ---------------------------------/   |
                          +-----------+                                     |
                          |  s_list   |                                     |
                          +-----------+                                     | 
                          |  s_inodes |                                     |
                          +-----------+                                     |
                          |  s_files  |                                     |
    +-------------+       +-----------+                                     |
    | "/dev/hdb1" |<------|  s_id     |                                     |
    +-------------+       +-----------+                                     |
                          |  s_op     |                                     |
                          +-----------+                                     |
                          |  s_root   |-----> dentry ("/" of hdb1) <--------/
                          +-----------+        +--------------+     
                          |           |        |  d_parent    |     
                                               +--------------+     
                                               |  d_child     |     
                                               +--------------+     
                                               |  d_subdirs   |     
                                               +--------------+     
                                               |  d_mounted   |     
                                               +--------------+     
                                               |              |     
                                                                    


从这张图可以看到,三个目录:  "/", "home" 和 "xpl" 的dentry
我们新mount的设备为/dev/hdb1, 新创建了一个super_block 和 一个 vfsmount (new)
新的super_block 在创建的时候已经加入到整个 vfs 的架构中(可参看前面的 super block 一节)
对于新的vfsmount:
 其mountpoint为 目录 "xpl" 的dentry,
 其mnt_root 是设备hdb1上的根目录的 dentry
 其父 vfsmount 就是原文件系统中的那个 vfsmount
 
同时,我们将新的这个vfsmount加入到了进程的namespace中。

至此,我们已经完成了整个mount的过程。


另外,对于Linux,可以将多个文件系统 mount 到同一个目录上,这样的话, 新 mount 的文件系统会覆盖原来mount的文件系统。
比如我们再把一个 "/dve/hdb2" 的设备 mount 到 "/home/xpl"  目录下,
这样,如果我们访问 "/home/xpl" 的时候,就会访问到 "/dev/hdb2"
当新mount的文件系统被 unmount 之后,原来被覆盖的文件系统就会再次显露出来了。

这个过程的之所以是这样的,是因为在路径查找的时候,如果发现要查找的目录上mount了 文件系统(其dentry的d_mounted 不为0),会切换文件系统。
比如我们上面的例子,当路径查找 "/home/xpl" 的时候,当查找到根文件系统的目录 "xpl" 的dentry时,发现有一个文件系统已经mount到这个目录上,就会切换文件系统,进而找到 "/dev/hdb1" 的根目录的 dentry
这样,路径查找"/home/xpl" 返回的时候,返回的是 "/dve/hdb1" 根目录的 dentry

这样,在我们 mount 第二个文件系统 "/dev/hdb2" 的时候,其mountpoint 就是 "/dev/hdb1" 的根目录的 dentry。
这个过程如下图所示:

                                                                     
                                                                     
                                                                     
   /-------> vfsmount("hdb1")          /-----> super_block           
   |        +---------------+          |      +-----------+          
   |        |   mnt_list    |          |      |  s_list   |                    
   |        +---------------+          |      +-----------+                    
   |        |   mnt_parent  |          |      |  s_inodes |                    
   |        +---------------+          |      +-----------+                    
   |        | mnt_mountpoint|          |      |  s_files  |                    
   |        +---------------+          |      +-----------+     +-------------+
   |        |   mnt_sb      |----------/      |  s_id     |---->| "/dev/hdb1" |
   |        +---------------+                 +-----------+     +-------------+
   |        |   mnt_root    |----->---\       |  s_op     |                    
   |        +---------------+         |       +-----------+                    
   |  /---->|   mnt_mounts  |<----\   |       |  s_root   |                    
   |  |     +---------------+     |   |       +-----------+                    
   |  |     |   mnt_child   |     |   |       |           |                    
   |  |     +---------------+     |   |                                        
   |  |     |               |     |   |                                        
   |  |     |               |     |   |                                        
   |  |                           |   |                                        
   |  |                           |   |                                        
   |  |                           |   |                                        
   |  |                           |   |                                        
   |  |                           |   |                                        
   |  |                           |   |                                        
   |  |                           |   |                                        
   |  |                           |   |                                        
   |  |                           |   |                                        
   |  |                           |   |                                        
   |  |                           |   \----> dentry ("/" of hdb1)              
   |  |                           |    /--->  +--------------+                 
   |  |                           |    |      |  d_parent    |                 
   |  |                           |    |      +--------------+                 
   |  |      vfsmount("hdb2")     |    |      |  d_child     |                 
   |  |     +---------------+     |    |      +--------------+                 
   |  |     |   mnt_list    |     |    |      |  d_subdirs   |                 
   |  |     +---------------+     |    |      +--------------+                 
   |  \---->|   mnt_child   |<----/    |      |  d_mounted   |                 
   |        +---------------+          |      +--------------+                 
   |        | mnt_mountpoint|----------/      |              |                 
   |        +---------------+                                                 
   |        |   mnt_sb      |-----\                                           
   |        +---------------+     |                                           
   |        |   mnt_root    |--\  |                                           
   |        +---------------+  |  |                                           
   |        |   mnt_mounts  |  |  |                                           
   |        +---------------+  |  |                                           
   \--<---- |   mnt_parent  |  |  |                                           
            +---------------+  |  |                                           
            |               |  |  |                                           
            |               |  |  \----->  super_block                        
                               |          +-----------+                       
                               |          |  s_list   |                       
                               |          +-----------+                       
                               |          |  s_inodes |                       
                               |          +-----------+                       
                               |          |  s_files  |                       
                               |          +-----------+       +-------------+ 
                               |          |  s_id     |------>| "/dev/hdb2" | 
                               |          +-----------+       +-------------+ 
                               |          |  s_op     |                       
                               |          +-----------+                       
                               |          |  s_root   |                       
                               |          +-----------+                       
                               |          |           |                       
                               |                                              
                               |                                              
                               |                                              
                               |                                              
                               \-------> dentry ("/" of hdb2)                 
                                          +--------------+                    
                                          |  d_parent    |                    
                                          +--------------+                    
                                          |  d_child     |                    
                                          +--------------+                    
                                          |  d_subdirs   |                    
                                          +--------------+                    
                                          |  d_mounted   |                    
                                          +--------------+                    
                                          |              |                    
                                                                              
                                                                              


以此类推,如果在该目录上再mount一个新的文件系统,基本逻辑是相同的。
因此在路径查找过程中,每查找到一个新的目录,都要对该目录的d_mounted 进行判断,看是否在该目录上mount的了文件系统,如果是,则要切换文件系统(切换查找过程中的 dentry 和 vfsmount)
这个过程是要递归的,即如果一个路径上mount了多个文件系统(如上面的例子),要递归到最后一个mount的文件系统(其dentry 的 d_mounted 为0)为止。

 

对于路径查找,我们补充说明一下两种特殊的情况:

一、路径中的 "." 和 ".."
 对于 "." ,很明显,就是当前目录,不需要额外的处理,简单跳过即可
  例如: /home/./xpl 当查找到 "." 的时候,还是 home 目录,因而 "." 的 dentry 和vfsmount 还是 目录 home的 dentry 和 vfsmount
 对于 "..", 这个需要跳到上一级目录,在这里,要注意:
 1. 如果发现已经没法向上了,就不再向上,而保持当前的路径。比如:"/../" ,已经是根目录了,返回的结果仍然是根目录。
 2. 如果发现当前的 dentry 是当前文件系统的根目录,并且该文件系统是mount到其他文件系统上的,这个时候就要反溯文件系统,要切换到原来mount的文件系统后,再向上一级目录。这个反溯过程也是要递归的。
  比如我们上面的例子中:"/home/xpl" 先mount了一个 hdb1 的文件系统,然后又mount 了一个 hdb2 的文件系统。
  在执行".." 查找的时候:
   1)我们发现当前的目录是hdb2 的根目录(vfsmount->mnt_root),并且mount到了hdb1上(vfsmount != vfsmount->parent),这个时候我们就要先切换到 hdb1 的文件系统中, 此时vfsmount 换为hdb1 的vfsmount, dentry换成 hdb2的mountpoint。
   2) 接下来要递归上面的文件系统切换,我们发现 当前的dentry 是hdb1文件系统的根目录,并且mount到了别的文件系统上,这个时候就要继续切换。
   3) 当所有的递归完成以后,我们得到了根文件系统的 xpl目录的dentry,然后再执行向上的操作(".."),最后得到 "/home"

 
二、路径中的符号链接
 如果路径中有符号链接,我们要跟随符号链接的路径。其实这个过程只是一个简单的递归。
 在路径查找中,我们得到一个dentry后,要判断这个dentry是否是一个符号链接。通过判断 dentry->d_inode->i_op->follow_link 是否是NULL,即是否有follow_link方法来判断是不是符号链接。
 如果是符号链接的话,我们需要通过这个inode的follow_link 来获得链接的路径。
 例如有这么一个符号链接: /home/xpl/link -> /mnt/disk
 我们会获得到其链接的路径  "/mnt/disk", 接下来就是继续解析链接的路径名,即查找 "/mnt/disk", 这个查找过程和普通的查找是一样的,递归下去,最终找到需要的目标。



原创粉丝点击