天天看點

[深入了解檔案系統之二] 檔案描述符、inode和打開檔案表

Unix檔案系統學習筆記之二: 檔案描述符、inode和打開檔案表

系統盤上資料的布局

檔案系統無非是關于資料在磁盤上的組織以及存儲空間管理的,為此,首先需要知道磁盤上資料的總體布局方式。以Unix為例,最重要的一張表如下:

<a href="https://s3.51cto.com/wyfs02/M01/8D/E1/wKiom1ith2uiNQk2AACQA04dGbg056.png" target="_blank"></a>

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:

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.

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

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

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用來記錄一個打開檔案的資訊:

<a href="https://s1.51cto.com/wyfs02/M02/8D/E1/wKiom1itiFOSPF_vAAEtqbDTNC0294.png" target="_blank"></a>

上述資料結構file_structure (記憶體中的就用來在unix中記錄程序打開的一個檔案的,而如果程式打開了多

個檔案,就需要一個file_structure數組。而這恰恰是恰恰是了解檔案描述符的關鍵,檔案描述符就是前程序打開的檔案清單的索引(index)。

下面這張圖前面展示了 檔案描述符、打開檔案清單和inode的關系:

<a href="https://s2.51cto.com/wyfs02/M01/8D/E1/wKiom1itiBOwys4GAACo-w3gKpM515.png" target="_blank"></a>

本文轉自存儲之廚51CTO部落格,原文連結:http://blog.51cto.com/xiamachao/1900342,如需轉載請自行聯系原作者