linux的虛擬文件系統VFS

    虛擬文件系統(virtual file system),別名虛擬文件系統開關,是linux中的一個軟件層,向用戶空間提供文件系統操做接口。html

    VFS包含的系統調用包括open(2)、stat(2)、read(2)、write(2)、chmod(2)等等,這些系統調用在進程環境中執行。下面幾個重要的數據結構是VFS(虛擬文件系統中涉及到的數據結構):node

    一、Directory Entry Cache(dcache)linux

    VFS實現了系統調用open(2)、stat(2)、chmod(2),和其餘相似的文件系統調用。用於這些系統調用中的參數被VFS用來進行directory entry cache的對象搜索,其中directory entry cache有多個名字,好比dentry cache或者dcache。 經過dentry cache提供了一種快速查找機制,能夠快速的根據路徑名字找到具體的dentry。須要特別說明的是:dentry是存在於內存中的,不會保存到磁盤上進行永久性存儲的。數據結構

    二、inode 對象函數

    每個dentry對象都有一個指針指向具體的inode對象。inode是文件系統的對象,好比正規文件(regular file)、目錄(directory)、FIFO(管道)。這些inode對象存在於磁盤(塊設備文件文件系統)或者內存(linux下的僞文件系統procfs)。若是是存在於磁盤上的inode對象則須要加載到內存中去,當發生的改變須要保存到磁盤上去。一個inode對象可能有多個dentry對象指向它(由於linux下實現了硬鏈接特性)。spa

    爲了查找一個具體的inode對象須要在父目錄的inode上執行lookup操做。而具體的lookup方法是由inode存放的文件系統具體的實現,這個操做是由具體的文件系統進行初始化。一旦VFS擁有具體的dentry對象的時候,不少系統調用就能夠迅速完成,好比stat(2)系統調用,它就是獲取inode上的數據,根據dentry上的指針能夠迅速的找到inode對象。.net

    三、File對象指針

    打開一個文件還須要另一個操做:分配一個File結構的對象(這是linux內核端的文件描述符的實現)。新申請的File對象初始化了一個指針指向dentry對象和多個文件操做函數(好比read、write、mmap等操做)。File數據結構放在這個進程的文件描述表中(file descriptor table)。code

    用戶空間的read、write等操做都是經過用戶空間態(userspace file descriptor)的描述符得到正確的File結構,而後調用File結構中的具體的方法。只要文件處於打開狀態,linux內核中就有對應的dentry對象,天然也有對象的inode對象。orm

    四、文件系統對象(filesystem)

    登記和卸載一個文件系統使用下面的函數調用。    

    #include <linux/fs.h>


     extern  int register_filesystem( struct file_system_type *);

     extern int unregister_filesystem(struct file_system_type *); 

    傳入的參數struct file_system_type描述了具體的文件系統。當發出一個命令,須要在你的名字空間的一個目錄上掛載一個文件系統的時候,VFS會調用具體文件中的mount方法。當mount操做完成以後,會返回一個struct dentry對象,這個時候會新建一個vfsmount對象,用於指向這個dentry對象,同時這個vfsmount對象會添加到這個掛載點上。因此當路徑解析到達這個目錄的時候,會跳轉到這個vfsmount指向的文件系統中去。

    在/proc/filesystems下能夠觀察到全部掛載的文件系統。

    如下是file_system_type結構:

    struct file_system_type {

     const  char *name;
     int fs_flags;
     struct dentry *(*mount) ( struct file_system_type *,  int,
                        const  char *,  void *);
     void (*kill_sb) ( struct super_block *);
    struct module *owner;
    struct file_system_type * next;
    struct list_head fs_supers;
     struct lock_class_key s_lock_key;
     struct lock_class_key s_umount_key;

    }; 

    name:文件系統的名字,當前支持的文件名字有「ext2」,「ext3」,「ext4」,「msdos」等

    fs_flags:一些標誌,好比:FS_REQUIRES_DEV, FS_NO_DCACHE

    mount:重要的field,當一個filesystem實例化的時候須要具體的filesystem的mount方法

    五、superblock對象 

    一個superblock(超級塊)對象表明了一個掛載的文件系統。

    下面是關於超級塊的操做的數據結構:

    struct super_operations {

         struct inode *(*alloc_inode)( struct super_block *sb);
         void (*destroy_inode)( struct inode *);
         void (*dirty_inode) ( struct inode *,  int flags);
         int (*write_inode) ( struct inode *,  int);
         void (*drop_inode) ( struct inode *);
         void (*delete_inode) ( struct inode *);
         void (*put_super) ( struct super_block *);
         int (*sync_fs)( struct super_block *sb,  int wait);
         int (*freeze_fs) ( struct super_block *);
         int (*unfreeze_fs) ( struct super_block *);
         int (*statfs) ( struct dentry *,  struct kstatfs *);
         int (*remount_fs) ( struct super_block *,  int *,  char *);
         void (*clear_inode) ( struct inode *);
         void (*umount_begin) ( struct super_block *);
         int (*show_options)( struct seq_file *,  struct dentry *);
        ssize_t (*quota_read)( struct super_block *,  intchar *, size_t, loff_t);
        ssize_t (*quota_write)( struct super_block *,  intconst  char *, size_t, loff_t);
int (*nr_cached_objects)( struct super_block *);
         void (*free_cached_objects)( struct super_block *,  int);

    };

   全部的方法都沒有lock來維持,這說明了這些方法只能夠在一個進程上下文中執行,不能在中斷上下文中執行。

   六、address space對象 

   address_space對象用於對page cache中的page進行分組和管理。它能夠用來跟蹤一個文件中的page cache和一個文件中映射到進程地址空間中的文件區域。

   address space提供了大量有顯著特徵的服務,好比根據經過地址查找page,追蹤標誌爲Dirty或者WriteBack的page。

 

   Further Reading:

   1.Creating Linux virtual filesystems. 2002. http://lwn.net/Articles/13325/

   2.A tour of the Linux VFS by Michael K. Johnson. 1996. http://www.tldp.org/LDP/khg/HyperNews/get/fs/vfstour.html

相關文章
相關標籤/搜索