虛擬文件系統[6]

  • 和文件系統相關的數據結構

     內核使用一些標準數據結構來管理文件系統的其他相關數據。結構體file_system_type,用於描述各種特定的文件系統類型:

 

  1. 在<Fs.h(include/linux)>中
  2. struct file_system_type {
  3.     const char *name;
  4.     int fs_flags;
  5.       /*該函數從磁盤上讀取超級塊,並在文件系統被安裝時,在內存中組裝超級塊對象*/
  6.     int (*get_sb) (struct file_system_type *, int,
  7.                const char *, void *, struct vfsmount *);
  8.     void (*kill_sb) (struct super_block *);
  9.     struct module *owner;
  10.     struct file_system_type * next;
  11.     struct list_head fs_supers;
  12.     struct lock_class_key s_lock_key;
  13.     struct lock_class_key s_umount_key;
  14. };

     每種文件系統,不管有多少個實例安裝到系統中,還是根本沒有安裝到系統中,都只有一個file_system_type結構。
    結構體vfsmount用於描述一個安裝文件系統的實例:

  1. 在<Mount.h(include/linux)>中
  2. struct vfsmount {
  3.     struct list_head mnt_hash;
  4.     struct vfsmount *mnt_parent;    /* fs we are mounted on */
  5.     struct dentry *mnt_mountpoint;  /* dentry of mountpoint */
  6.     struct dentry *mnt_root;    /* root of the mounted tree */
  7.     struct super_block *mnt_sb; /* pointer to superblock */
  8.     struct list_head mnt_mounts;    /* list of children, anchored here */
  9.     struct list_head mnt_child; /* and going through their mnt_child */
  10.     int mnt_flags;
  11.     /* 4 bytes hole on 64bits arches */
  12.     char *mnt_devname;      /* Name of device e.g. /dev/dsk/hda1 */
  13.     struct list_head mnt_list;
  14.     struct list_head mnt_expire;    /* link in fs-specific expiry list */
  15.     struct list_head mnt_share; /* circular list of shared mounts */
  16.     struct list_head mnt_slave_list;/* list of slave mounts */
  17.     struct list_head mnt_slave; /* slave list entry */
  18.     struct vfsmount *mnt_master;    /* slave is on master->mnt_slave_list */
  19.     struct mnt_namespace *mnt_ns;   /* containing namespace */
  20.     /*
  21.      * We put mnt_count & mnt_expiry_mark at the end of struct vfsmount
  22.      * to let these frequently modified fields in a separate cache line
  23.      * (so that reads of mnt_flags wont ping-pong on SMP machines)
  24.      */
  25.     atomic_t mnt_count;
  26.     int mnt_expiry_mark;        /* true if marked for expiry */
  27.     int mnt_pinned;
  28. };

   當文件系統被實際安裝時,將有一個vfsmount結構體在安裝點被創建。該結構體用來代表文件系統的示例,即代表一個安裝點。

   vfsmount結構體中保存了在安裝時指定的標誌信息,這些信息存儲在mnt_flags域中:

  1. #define MNT_NOSUID  0x01      //禁止該文件系統的可執行文件執行文件設置setuid和setgid
  2. #define MNT_NODEV   0x02      //禁止訪問該文件系統上的設備文件
  3. #define MNT_NOEXEC  0x04      //禁止執行該文件系統上的可執行文件
  4. #define MNT_NOATIME 0x08
  5. #define MNT_NODIRATIME  0x10
  6. #define MNT_RELATIME    0x20
  • 和進程相關的數據結構

   系統中的每一個進程都有自己的一組打開文件,像根文件系統、當前工作目錄、安裝點等。有三個數據結構將VFS層和進程緊密聯繫在一起:files_struct,fs_struc和namespace結構體。

   files_struct結構體由進程描述符中的files域指向。所有與每個進程(per-process)相關的信息如打開的文件及文件描述符都包含在其中:

 

  1. 在<Files.h(include/linux)>中
  2. /*
  3.  * The default fd array needs to be at least BITS_PER_LONG,
  4.  * as this is the granularity returned by copy_fdset().
  5.  */
  6. #define NR_OPEN_DEFAULT BITS_PER_LONG
  7. /*
  8.  * The embedded_fd_set is a small fd_set,
  9.  * suitable for most tasks (which open <= BITS_PER_LONG files)
  10.  */
  11. struct embedded_fd_set {
  12.     unsigned long fds_bits[1];
  13. };
  14. struct fdtable {
  15.     unsigned int max_fds;
  16.     struct file ** fd;      /* current fd array   fd數組指針指向已打開的文件對象鏈表,默認情況下,指向fd_array數組。  */
  17.     fd_set *close_on_exec;
  18.     fd_set *open_fds;
  19.     struct rcu_head rcu;
  20.     struct fdtable *next;
  21. };
  22. /*
  23.  * Open file table structure
  24.  */
  25. struct files_struct {
  26.   /*
  27.    * read mostly part
  28.    */
  29.     atomic_t count;
  30.     struct fdtable *fdt;
  31.     struct fdtable fdtab;
  32.   /*
  33.    * written part on a separate cache line in SMP
  34.    */
  35.     spinlock_t file_lock ____cacheline_aligned_in_smp;
  36.     int next_fd;
  37.     struct embedded_fd_set close_on_exec_init;
  38.     struct embedded_fd_set open_fds_init;
  39.     struct file * fd_array[NR_OPEN_DEFAULT];
  40. };

 

  fs_struct結構體由進程描述符中的fs域指向。它包含文件系統和進程相關的信息:

 

 

  1. 在<Fs_struc.h>中
  2. struct dentry;
  3. struct vfsmount;
  4. struct fs_struct {
  5.     atomic_t count;
  6.     rwlock_t lock;
  7.     int umask;
  8.     struct dentry * root, * pwd, * altroot;
  9.     struct vfsmount * rootmnt, * pwdmnt, * altrootmnt;
  10. };

  namespace結構體由進程描述符中的namespace域指向。2.4版內核之後,單進程命名空間被加入到內核中,它使得每一個進程在系統中都看到唯一的安裝文件系統,不僅是惟一的根目錄,而且是惟一的文件系統層次結構:

 

  1. 沒有找到nampspace.h文件,所以在<mnt_namespace.h(include/linux)>中找到下面的結構:
  2. struct mnt_namespace {
  3.     atomic_t        count;
  4.     struct vfsmount *   root;
  5.     struct list_head    list;
  6.     wait_queue_head_t poll;
  7.     int event;
  8. };

   上述數據結構都是通過進程描述符連接起來的。對多數進程來說,它們的描述符都指向唯一的files_struct和fs_struct結構體。所以多個進程描述符可能指向同一個files_struct和fs_struct結構體。

   默認情況下,所有的進程共享同樣的命名空間(即它們都從相同的掛載表中看到同一個文件系統層次結構)。只有在進行CLONE_NEWNS標誌,纔會給進程一個另外的命名空間結構體的拷貝。

發佈了79 篇原創文章 · 獲贊 6 · 訪問量 31萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章