[深刻理解文件系統之二] 文件描述符、inode和打開文件表

Unix文件系統學習筆記之二: 文件描述符、inode和打開文件表node



  • 系統盤上數據的佈局數組



    文件系統無非是關於數據在磁盤上的組織以及存儲空間管理的,爲此,首先須要知道磁盤上數據的整體佈局方式。以Unix爲例,最重要的一張表以下:緩存

wKiom1ith2uiNQk2AACQA04dGbg056.png



  • Unix 進程管理中和用戶文件、io 最相關的數據結構:usr 數據結構
    數據結構


The procstructure does not record information related to file access.  However the userstructure contains a number of important  file-access-related fields, namely:ide

u_cdir. The inode of the current working directory is stored here. This is佈局

used during pathname resolution when a user specifies a relative學習

pathname.ui

u_uid/u_gid. The process user ID and group ID used for permissionsthis

checking for file-access-based system calls. Similarly, u_euidandspa

u_egidhold the effective user and group IDs.

u_ofile. This array holds the process file descriptors. This is described in

more detail later.

u_arg. An array of system call arguments set up during the transition

from user to kernel mode when invoking a system call.

u_base. This field holds the address of a user space buffer in which to read

data from or write data to when processing a system call such as read()

orwrite().

u_count. The number of bytes to read or write is held here. It is

decremented during the I/O operation and the result can be passed back

to the user.

u_offset. This field records the offset within the file for the current read

or write operation.

u_error. When processing a system call, this field is set if an error is

encountered. The value of u_erroris then passed back to the user

when the system call returns.


  • INODE

    爲了深刻理解文件inode、打開文件表、描述符三者之間的關係,首先就須要瞭解indoe。

深入理解inode就是在理解meta-data,inode是文件系統中最重要的meta data, 它的主要數據結構以下

(注意,它的原始數據存儲在非易失性的器件上,文件系統啓動以後,訪問到的或者常常訪問的inode被讀到內存裏面,這一部分被稱爲inode in-core。)


Each file in the filesystem was represented by a unique inode that contained fields such as:

i_mode. This field specifies whether the file is a directory (IFDIR), a block

special file (IFBLK), or a character special file (IFCHR). Note that if one

of the above modes was not set, the file was assumed to be a regular file.

This would later be replaced by an explicit flag, IFREG.

i_nlink. This field recorded the number of hard links to the file. When

this field reaches zero, the inode is freed.

i_uid. The file’s user ID.

i_gid. The file’s group ID.

i_size. The file size in bytes.

i_addr. This field holds block addresses on disk where the file’s data blocks are held.

i_mtime. The time the file was last modified.


關於inode的操做,須要考慮如下方面:


Inode in core/memory

a.什麼時候從磁盤讀入到內存: 打開的時候須要讀入inode;

b.什麼時候從內存寫入到磁盤:若是對inode有任何更新,好比新申請了塊、釋放了塊

c.什麼時候能夠寫入到磁盤: 文件已經關閉,而且沒有任何進程打開了inode對應的文件

d.哪些inode須要緩存:DNLC (directory name lookup cache for vnode)


  • 打開文件表

由此天然引出一個問題,如何表示進程打開的一個文件,或者說操做系統如何記錄一個打開的文件呢?若是你去設計,你會怎麼作?

a.它必然包含inode的部分信息(或者所有)

b.它必然包含運行時的信息(讀寫指針的偏移,讀寫buffer的位置,以及它所對應的inode指針?

固然還必須包含引用計數、打開的方式flag)


在unix系統中,正是這樣實現的, 下面就是unix用來記錄一個打開文件的信息:

wKiom1itiFOSPF_vAAEtqbDTNC0294.png

上述數據結構file_structure (內存中的就用來在unix中記錄進程打開的一個文件的,而若是程序打開了多

個文件,就須要一個file_structure數組。而這偏偏是偏偏是理解文件描述符的關鍵,文件描述符就是前進程打開的文件列表的索引(index)。


下面這張圖前面展現了 文件描述符、打開文件列表和inode的關係:


wKiom1itiBOwys4GAACo-w3gKpM515.png

相關文章
相關標籤/搜索