Unix文件系统学习笔记之二: 文件描述符、inode和打开文件表



  • 系统盘上数据的布局



    文件系统无非是关于数据在磁盘上的组织以及存储空间管理的,为此,首先需要知道磁盘上数据的总体布局方式。以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:

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用来记录一个打开文件的信息:

wKiom1itiFOSPF_vAAEtqbDTNC0294.png

上述数据结构file_structure (内存中的就用来在unix中记录进程打开的一个文件的,而如果程序打开了多

个文件,就需要一个file_structure数组。而这恰恰是恰恰是理解文件描述符的关键,文件描述符就是前进程打开的文件列表的索引(index)。


下面这张图前面展示了 文件描述符、打开文件列表和inode的关系:


wKiom1itiBOwys4GAACo-w3gKpM515.png