標籤:
虛擬檔案系統(Virtual Filesystem)也可稱之為虛擬檔案系統轉換(Virtual Filesystem Switch),是一個核心軟體層,用來處理與Unix標準檔案系統相關的全部系統調用。其健壯性表如今能為各種檔案系統提供一個通用的介面。
通用檔案系統模型
VFS所隱含的主要思想在於引入一個通用的檔案系統模型(common file model),這個模型可以表示全部支援的檔案系統。在通用檔案模型中,每一個檔案夾被看做一個檔案,可以包括若干檔案和其它的子檔案夾。
通用檔案模型由下列物件類型組成:
超級塊對象(superblock object)
存放已安裝檔案系統的有關資訊(A superblock object represents a mounted filesystem)。對基於磁碟的檔案系統,這類對象通常相應於存放在磁碟上的檔案系統控制塊
索引節點對象(inode object)
存放關於詳細檔案的一般資訊(An inode object represents an object within the filesystem)。對於基於磁碟的檔案系統,這類對象通常相應於存放在磁碟上的檔案控制區。每一個索引節點對象都有一個索引節點號,這個節點號唯一地標識檔案系統中的檔案。
檔案對象(file object)
存放開啟檔案與進程之間進行互動的有關資訊(A file object represents a file opened by a process)。這類資訊僅當進程訪問檔案期間存在於核心記憶體中。
檔案夾項對象(dentry object)
存放檔案夾項(也就是檔案的特定名稱)與相應檔案進行連結的有關資訊。
VFS的資料結構
這裡僅僅列舉和進程相關的結構
索引節點對象
檔案系統處理檔案所須要的全部資訊都放在一個名為索引節點的資料結構中。檔案名稱能夠隨時更改,可是索引節點對檔案是唯一的,而且隨著檔案的存在而存在。記憶體中索引節點對象由一個struct inode資料結構構成。
struct inode {struct hlist_nodei_hash; //用於散列鏈表struct list_headi_list;/* backing dev IO list */struct list_headi_sb_list; struct list_headi_dentry; //引用索引節點的檔案夾項對象鏈表頭unsigned longi_ino; //索引節點號atomic_ti_count; //引用計數器unsigned inti_nlink; //永久連結數目uid_ti_uid; //全部者標識符gid_ti_gid; //群組識別碼dev_ti_rdev; //實裝置標識符u64i_version; //版本(每次使用後遞增)loff_ti_size; //檔案的位元組數#ifdef __NEED_I_SIZE_ORDEREDseqcount_ti_size_seqcount;#endifstruct timespeci_atime; //上次訪問檔案的時間struct timespeci_mtime; //上次寫檔案的時間struct timespeci_ctime; //上次改動索引節點的時間blkcnt_ti_blocks; //檔案的塊數unsigned inti_blkbits; //塊的位元unsigned short i_bytes;//塊的位元組數umode_ti_mode; //檔案的類型和訪問許可權spinlock_ti_lock;/* i_blocks, i_bytes, maybe i_size */struct mutexi_mutex;struct rw_semaphorei_alloc_sem; //在直接I/O檔案操作中避免出現競爭條件的讀寫訊號量const struct inode_operations*i_op; //索引節點的操作const struct file_operations*i_fop;/*預設檔案操作former ->i_op->default_file_ops */struct super_block*i_sb; //指向超級塊的指標struct file_lock*i_flock;struct address_space*i_mapping; //指向address_space對象的指標struct address_spacei_data; //檔案的address_space對象#ifdef CONFIG_QUOTAstruct dquot*i_dquot[MAXQUOTAS]; //索引節點磁碟限額#endifstruct list_headi_devices; //用於詳細的字元或塊裝置索引節點鏈表union {struct pipe_inode_info*i_pipe; //假設檔案是個管道則使用它struct block_device*i_bdev; //指向塊裝置驅程式的指標struct cdev*i_cdev; //指向字元裝置驅動程式的指標};__u32i_generation; //索引節點的版本#ifdef CONFIG_FSNOTIFY__u32i_fsnotify_mask; /* all events this inode cares about */struct hlist_headi_fsnotify_mark_entries; /* fsnotify mark entries */#endif#ifdef CONFIG_INOTIFYstruct list_headinotify_watches; /* watches on this inode */struct mutexinotify_mutex;/* protects the watches list */#endifunsigned longi_state; //索引節點狀態標誌unsigned longdirtied_when;/* jiffies of first dirtying */unsigned inti_flags; //檔案系統安裝標誌atomic_ti_writecount;#ifdef CONFIG_SECURITYvoid*i_security;#endif#ifdef CONFIG_FS_POSIX_ACLstruct posix_acl*i_acl;struct posix_acl*i_default_acl;#endifvoid*i_private; /* fs or device private pointer */};檔案對象
檔案對象描寫敘述進程如何與一個開啟的檔案進行互動。檔案對象是在檔案被開啟時建立的,由一個file結構組成。檔案對象在磁碟上是沒有相應的映像,因此file結構中沒有設定“髒”欄位來表示檔案對象是否已被改動。
struct file {/* * fu_list becomes invalid after file_free is called and queued via * fu_rcuhead for RCU freeing */union {struct list_headfu_list;struct rcu_head fu_rcuhead;} f_u;struct pathf_path;#define f_dentryf_path.dentry //與檔案相關的檔案夾項對象#define f_vfsmntf_path.mnt //含有該檔案的已安裝檔案系統const struct file_operations*f_op; //檔案動作表指標spinlock_tf_lock; /* f_ep_links, f_flags, no IRQ */atomic_long_tf_count; //檔案對象的引用計數器unsigned int f_flags; //當開啟檔案時所指定的標誌fmode_tf_mode; //進程訪問模式loff_tf_pos; //當前的檔案位移量struct fown_structf_owner; //通過訊號進行I/O事件通知的資料const struct cred*f_cred;struct file_ra_statef_ra; //檔案預讀狀態u64f_version; //版本,每次使用後自己主動遞增#ifdef CONFIG_SECURITYvoid*f_security;#endif/* needed for tty driver, and maybe others */void*private_data;//指向特定檔案系統或裝置驅動程式所須要資料的指標#ifdef CONFIG_EPOLL/* Used by fs/eventpoll.c to link all the hooks to this file */struct list_headf_ep_links;//檔案的事件輪詢等待著鏈表頭#endif /* #ifdef CONFIG_EPOLL */struct address_space*f_mapping;//指向檔案地址空間對象的指標#ifdef CONFIG_DEBUG_WRITECOUNTunsigned long f_mnt_write_state;#endif};檔案對象通過一個名為filp的slab快速緩衝分配,filp描寫敘述符地址存放在filp_cachep變數中。因為分配的檔案對象數目是有限的,因此files_stat變數在其max_files欄位中指定了可分配的檔案對象的最大數目,也就是系統可同一時候訪問的最大檔案數。
核心初始化期間,files_init()函數把max_files欄位設定為可用RAM大小的1/10。只是,系統管理員能夠通過寫/proc/sys/fs/file-max檔案來改動這個值。並且即使max_files個檔案對象已經被分配,超級使用者也總是能夠獲得一個檔案對象
void __init files_init(unsigned long mempages){ int n; filp_cachep = kmem_cache_create("filp", sizeof(struct file), 0,SLAB_HWCACHE_ALIGN | SLAB_PANIC, NULL);/* * One file with associated inode and dcache is very roughly 1K. * Per default don't use more than 10% of our memory for files. */ n = (mempages * (PAGE_SIZE / 1024)) / 10;files_stat.max_files = n; if (files_stat.max_files < NR_FILE)files_stat.max_files = NR_FILE;files_defer_init();percpu_counter_init(&nr_files, 0);} 檔案夾項對象
VFS把每一個檔案夾看做若干子檔案夾和檔案組成的一個普通檔案。一旦檔案夾項被讀入記憶體,VFS就把它轉換成基於dentry結構的一個檔案夾項對象。對於進程尋找的路徑名中的每一個分量,核心都為其建立一個檔案夾項對象;檔案夾項對象將每一個分量與其相應的索引節點相聯絡。比如在尋找路徑名/tmp/test時,核心為根資料夾“/”建立一個檔案夾項對象,為根資料夾下的tmp項建立第二級檔案夾項對象,為/tmp檔案夾下的test建立一個第三級檔案夾項對象。
檔案夾項對象在磁碟上並沒有相應的映像,因此在dentry結構中不包括指出該對象已被改動的欄位。檔案夾項對象存放在名為dentry_cache的快速緩衝中。
struct dentry {atomic_t d_count; //檔案夾項對象引用計數unsigned int d_flags;/* 檔案夾項快速緩衝標誌protected by d_lock */spinlock_t d_lock;/* per dentry lock */int d_mounted; //對檔案夾而言,用於記錄安裝該檔案夾項的檔案系統計數器struct inode *d_inode;/* 與檔案名稱關聯的索引節點Where the name belongs to - NULL is * negative *//* * The next three fields are touched by __d_lookup. Place them here * so they all fit in a cache line. */struct hlist_node d_hash;/* lookup hash list */struct dentry *d_parent;/* 父資料夾的檔案夾項對象parent directory */struct qstr d_name; //檔案名稱struct list_head d_lru;/* LRU list *//* * d_child and d_rcu can share memory */union {struct list_head d_child;/* child of parent list */ struct rcu_head d_rcu;} d_u;struct list_head d_subdirs;/* our children */struct list_head d_alias;/* inode alias list */unsigned long d_time;/* used by d_revalidate */const struct dentry_operations *d_op;//檔案夾項方法struct super_block *d_sb;/* 檔案的超級塊對象The root of the dentry tree */void *d_fsdata;/* 依賴於檔案系統的資料fs-specific data */unsigned char d_iname[DNAME_INLINE_LEN_MIN];/* small names */};與進程相關的檔案
每一個進程都有它自己當前的工作資料夾和他自己的根資料夾。這不過核心用來表示進程與檔案系統相互作用所必須維護的資料的兩個範例。類型為fs_struct的整個資料結構就用於此目的
struct fs_struct {int users;rwlock_t lock;int umask; //當開啟檔案設定檔案許可權是所用的位元遮罩int in_exec;struct path root, pwd; /* 根資料夾的檔案夾項,根資料夾所安裝的檔案系統對象 當前工作的檔案夾項,當前工作資料夾所安裝的檔案系統對象*/};進程當前開啟的檔案與files_struct結構有關
struct fdtable {unsigned int max_fds; //檔案對象的當前最大數目struct file ** fd; /* current fd array */fd_set *close_on_exec;fd_set *open_fds;struct rcu_head rcu;struct fdtable *next;};/* * Open file table structure */struct files_struct { /* * read mostly part */atomic_t count; //共用進程的數目struct fdtable *fdt; //struct fdtable fdtab; /* * written part on a separate cache line in SMP */spinlock_t file_lock ____cacheline_aligned_in_smp;int next_fd;struct embedded_fd_set close_on_exec_init;struct embedded_fd_set open_fds_init;struct file * fd_array[NR_OPEN_DEFAULT];//檔案對象指標的初始化數組};
fd欄位指向檔案對象的指標數組。該數組的長度存放在max_fds欄位中。通常,fd欄位指向files_struct結構中的fd_array欄位,該欄位包含32個檔案對象指標。假設進程開啟的檔案資料多於32,核心就分配一個新的、更大的檔案指標數組,並將其地址存放在fd欄位中,核心同一時候更新max_fds欄位的值。
相應fd數組中有元素的每一個檔案來說,數組的索引就是檔案描寫敘述符。通常,數組的第一個元素(索引0)是進程的標準輸入檔案,數組的第二個元素(索引1)是進程的標準輸出檔案,數組的第三個元素(索引2)是進程的標準錯誤輸出檔案。
核心在進程描寫敘述符的signal->rlim[RLIM_NLIMITS]結構上強制動態限制檔案描寫敘述符的最大數;這個值通常為1024,但假設進程具有超級許可權,就能夠增大這個值。
最經常使用的特殊檔案系統類型
名字 安裝點 說明
bdev 無 塊裝置
binfmt_misc 隨意 其它可運行格式
devpts /dev/pts 偽終端支援
eventpollfs 無 由有效事件輪詢機制使用
futexfs 無 由futex(高速使用者態加鎖)機制使用
pipefs 無 管道
proc /proc 對核心資料結構的常規訪問點
rootfs 無 為啟動階段提供一個空的根資料夾
shm 無 IPC共用線性區
mqueue 隨意 實現POSIX訊息佇列時使用
sockfs 無 通訊端
sysfs /sys 對系統參數的常規訪問
tmpfs 隨意 暫時檔案(假設不被交換出去就保持在RAM中)
usbfs /proc/bus/usb USB裝置
檔案系統注冊
每一個注冊的檔案系統都用一個類型為file_system_type的對象來表示
struct file_system_type {const char *name; //檔案系統名int fs_flags; //檔案系統標誌int (*get_sb) (struct file_system_type *, int, const char *, void *, struct vfsmount *); //讀取超級塊的方法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;struct lock_class_key i_lock_key;struct lock_class_key i_mutex_key;struct lock_class_key i_mutex_dir_key;struct lock_class_key i_alloc_sem_key;};以sockfs檔案系統注冊為例
static struct file_system_type sock_fs_type = {.name ="sockfs",.get_sb =sockfs_get_sb,.kill_sb =kill_anon_super,};static int __init sock_init(void){/* * Initialize sock SLAB cache. */sk_init();/* * Initialize skbuff SLAB cache */skb_init();/* * Initialize the protocols module. */init_inodecache(); /* 注冊sockfs檔案系統 */register_filesystem(&sock_fs_type);sock_mnt = kern_mount(&sock_fs_type);/* The real protocol initialization is performed in later initcalls. */#ifdef CONFIG_NETFILTERnetfilter_init();#endifreturn 0;}
Linux虛擬檔案系統(VFS)學習