open:文件描述符的操做(如: open)返回的是一個文件描述符(int fd),內核會在每一個進程空間中維護一個文件描述符表, 全部打開的文件都將經過此表中的文件描述符來引用(fd1,fd2,fd3...);
fopen:而流(如: fopen)返回的是一個FILE結構指針, FILE結構是包含有文件描述符的,FILE結構函數能夠看做是對fd直接操做的系統調用的封裝, 它的優勢是帶有I/O緩存.node
Linux支持各類各樣的文件系統格式,如ext二、ext三、reiserfs、FAT、NTFS、iso9660等等,不一樣的磁盤分區、光盤或其它存儲設備都有不一樣的文件系統格式,然而這些文件系統均可以mount
到某個目錄下,使咱們看到一個統一的目錄樹,各類文件系統上的目錄和文件咱們用ls
命令看起來是同樣的,讀寫操做用起來也都是同樣的,這是怎麼作到的呢?Linux內核在各類不一樣的文件系統格式之上作了一個抽象層,使得文件、目錄、讀寫訪問等概念成爲抽象層的概念,所以各類文件系統看起來用起來都同樣(VFS做用),這個抽象層稱爲虛擬文件系統(VFS,Virtual Filesystem),這一節咱們介紹運行時文件系統在內核中的表示。linux
Linux內核的VFS子系統能夠圖示以下:緩存
每一個進程在PCB(Process Control Block)即進程控制塊中都保存着一份文件描述符表,文件描述符就是這個表的索引,文件描述表中每一個表項都有一個指向已打開文件的指針,如今咱們明確一下:已打開的文件在內核中用file
結構體表示,文件描述符表中的指針指向file
結構體(理解:fd爲打開文件的文件描述符,而每一個進程都有一張文件描述表,fd文件描述符就是這張表的索引,一樣這張表中有一表項,該表項又是指向前面提到打開文件的file結構體,file結構體纔是內核中用於描述文件屬性的結構體)。app
struct file-----define in inlcude/linux/fs.hasync
struct file_operations------define in include/linux/fs.hide
struct file { union { struct llist_node fu_llist; struct rcu_head fu_rcuhead; } f_u; struct path f_path; #define f_dentry f_path.dentry struct inode *f_inode; /* cached value */ const struct file_operations *f_op; /* * Protects f_ep_links, f_flags. * Must not be taken from IRQ context. */ spinlock_t f_lock; atomic_long_t f_count; unsigned int f_flags; fmode_t f_mode; struct mutex f_pos_lock; loff_t f_pos; struct fown_struct f_owner; const struct cred *f_cred; struct file_ra_state f_ra; u64 f_version; #ifdef CONFIG_SECURITY void *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_head f_ep_links; struct list_head f_tfile_llink; #endif /* #ifdef CONFIG_EPOLL */ struct address_space *f_mapping; #ifdef CONFIG_DEBUG_WRITECOUNT unsigned long f_mnt_write_state; #endif } __attribute__((aligned(4))); /* lest something weird decides that 2 is OK */ struct file
struct file_operations { struct module *owner; loff_t (*llseek) (struct file *, loff_t, int); ssize_t (*read) (struct file *, char __user *, size_t, loff_t *); ssize_t (*write) (struct file *, const char __user *, size_t, loff_t *); ssize_t (*aio_read) (struct kiocb *, const struct iovec *, unsigned long, loff_t); ssize_t (*aio_write) (struct kiocb *, const struct iovec *, unsigned long, loff_t); int (*iterate) (struct file *, struct dir_context *); unsigned int (*poll) (struct file *, struct poll_table_struct *); long (*unlocked_ioctl) (struct file *, unsigned int, unsigned long); long (*compat_ioctl) (struct file *, unsigned int, unsigned long); int (*mmap) (struct file *, struct vm_area_struct *); int (*open) (struct inode *, struct file *); int (*flush) (struct file *, fl_owner_t id); int (*release) (struct inode *, struct file *); int (*fsync) (struct file *, loff_t, loff_t, int datasync); int (*aio_fsync) (struct kiocb *, int datasync); int (*fasync) (int, struct file *, int); int (*lock) (struct file *, int, struct file_lock *); ssize_t (*sendpage) (struct file *, struct page *, int, size_t, loff_t *, int); unsigned long (*get_unmapped_area)(struct file *, unsigned long, unsigned long, unsigned long, unsigned long); int (*check_flags)(int); int (*flock) (struct file *, int, struct file_lock *); ssize_t (*splice_write)(struct pipe_inode_info *, struct file *, loff_t *, size_t, unsigned int); ssize_t (*splice_read)(struct file *, loff_t *, struct pipe_inode_info *, size_t, unsigned int); int (*setlease)(struct file *, long, struct file_lock **); long (*fallocate)(struct file *file, int mode, loff_t offset, loff_t len); int (*show_fdinfo)(struct seq_file *m, struct file *f); }; struct file_operations
在file
結構體中維護File Status Flag(file
結構體的成員f_flags
)和當前讀寫位置(file
結構體的成員f_pos
)。在上圖中,進程1和進程2都打開同一文件,可是對應不一樣的file
結構體,所以能夠有不一樣的File Status Flag和讀寫位置。file
結構體中比較重要的成員還有f_count
,表示引用計數(Reference Count),後面咱們會講到,dup
、fork
等系統調用會致使多個文件描述符指向同一個file
結構體,例若有fd1
和fd2
都引用同一個file
結構體,那麼它的引用計數就是2,當close(fd1)
時並不會釋放file
結構體,而只是把引用計數減到1,若是再close(fd2)
,引用計數就會減到0同時釋放file
結構體,這才真的關閉了文件。函數
每一個file
結構體都指向一個file_operations
結構體,這個結構體的成員都是函數指針,指向實現各類文件操做的內核函數。好比在用戶程序中read
一個文件描述符,read
經過系統調用進入內核,而後找到這個文件描述符所指向的file
結構體,找到file
結構體所指向的file_operations
結構體,調用它的read
成員所指向的內核函數以完成用戶請求(應用層到內核層的調用流程)。在用戶程序中調用lseek
、read
、write
、ioctl
、open
等函數,最終都由內核調用file_operations
的各成員所指向的內核函數完成用戶請求。file_operations
結構體中的release
成員用於完成用戶程序的close
請求,之因此叫release
而不叫close
是由於它不必定真的關閉文件,而是減小引用計數,只有引用計數減到0才關閉文件。對於同一個文件系統上打開的常規文件來講,read
、write
等文件操做的步驟和方法應該是同樣的,調用的函數應該是相同的,因此圖中的三個打開文件的file
結構體指向同一個file_operations
結構體。若是打開一個字符設備文件,那麼它的read
、write
操做確定和常規文件不同,不是讀寫磁盤的數據塊而是讀寫硬件設備,因此file
結構體應該指向不一樣的file_operations
結構體(也就有了用戶自定義結構體對象或者內核自定義結構體對象),其中的各類文件操做函數由該設備的驅動程序實現。佈局
每一個file
結構體都有一個指向dentry
結構體的指針,「dentry」是directory entry(目錄項)的縮寫。咱們傳給open
、stat
等函數的參數的是一個路徑,例如/home/akaedu/a
,須要根據路徑找到文件的inode。爲了減小讀盤次數,內核緩存了目錄的樹狀結構,稱爲dentry cache(做用),其中每一個節點是一個dentry
結構體,只要沿着路徑各部分的dentry搜索便可,從根目錄/
找到home
目錄,而後找到akaedu
目錄,而後找到文件a
。dentry cache只保存最近訪問過的目錄項,若是要找的目錄項在cache中沒有,就要從磁盤讀到內存中。this
每一個dentry
結構體都有一個指針指向inode
結構體。inode
結構體保存着從磁盤inode讀上來的信息。在上圖的例子中,有兩個dentry,分別表示/home/akaedu/a
和/home/akaedu/b
,它們都指向同一個inode,說明這兩個文件互爲硬連接。inode
結構體中保存着從磁盤分區的inode讀上來信息,例如全部者、文件大小、文件類型和權限位等(inode有哪些參數,正常理解file結構體可能包含這些信息,實際上是file.inode成員管理這些信息)。每一個inode
結構體都有一個指向inode_operations
結構體的指針,後者也是一組函數指針指向一些完成文件目錄操做的內核函數。和file_operations
不一樣,inode_operations
所指向的不是針對某一個文件進行操做的函數,而是影響文件和目錄佈局的函數,例如添加刪除文件和目錄、跟蹤符號連接等等,屬於同一文件系統的各inode
結構體能夠指向同一個inode_operations
結構體。atom
inode
結構體有一個指向super_block
結構體的指針。super_block
結構體保存着從磁盤分區的超級塊讀上來的信息,例如文件系統類型、塊大小等。super_block
結構體的s_root
成員是一個指向dentry
的指針,表示這個文件系統的根目錄被mount
到哪裏,在上圖的例子中這個分區被mount
到/home
目錄下。
file
、dentry
、inode
、super_block
這幾個結構體組成了VFS的核心概念。對於ext2文件系統來講,在磁盤存儲佈局上也有inode和超級塊的概念,因此很容易和VFS中的概念創建對應關係。而另一些文件系統格式來自非UNIX系統(例如Windows的FAT3二、NTFS),可能沒有inode或超級塊這樣的概念,但爲了能mount
到Linux系統,也只好在驅動程序中硬湊一下,在Linux下看FAT32和NTFS分區會發現權限位是錯的,全部文件都是rwxrwxrwx
,由於它們原本就沒有inode和權限位的概念,這是硬湊出來的。