文件系统-文件和目录在硬盘上的存储方式

来源:互联网 发布:二级路由网络隔离 编辑:程序博客网 时间:2024/05/17 02:27

refer to  http://blog.csdn.net/engerled/article/details/6234045
文件系统指文件存在的物理空间,linux系统中每个分区都是一个文件系统,都有自己的目录层次结构。linux会将这些分属不同分区的、单独的文件系统按一定的方式形成一个系统的总的目录层次结构。一个操作系统的运行离不开对文件的操作,因此必然要拥有并维护自己的文件系统。linux文件系统使用索引节点来记录文件信息,作用像windows的文件分配表。

     索引节点是一个结构,它包含了一个文件的长度、创建及修改时间、权限、所属关系、磁盘中的位置等信息。一个文件系统维护了一个索引节点的数组,每个文件或目录都与索引节点数组中的唯一一个元素对应。系统给每个索引节点分配了一个号码,也就是该节点在数组中的索引号,称为索引节点号。

  linux文件系统将文件索引节点号和文件名同时保存在目录中。所以,目录只是将文件的名称和它的索引节点号结合在一起的一张表,目录中每一对文件名称和索引节点号称为一个连接。

如下图


一个文件系统(分区)对应一个i节点数组
一个目录对应一个目录块

   当一个文件系统被格式化成ext2或者ext3的时候,就会产生Inode number。大家注意到,在文件系统中每一个inode-no对应一个文件:例如图中对应的1-F1(文件名)、2-D1等等而每一个Inode-no对应一个inode-table,即图中下面部分的一张表首先我们看看,我们是怎么样读取和修改一个文件的:(VFS文件系统中描述的更详细些) 

1、我们首先根据文件名,找到这个文件的Inode-no(节点号)。
2、当我们找到个文件的Inode-no时,就会根据这个number数在inodetable中找到对应的条目。
3、现在要我们看一看inodetable中的信息:从左到右依次是:节点数、文件类型、文件的权限、硬链接数、用户ID、组ID、文件的大小、时间戳记,最后为指向硬盘上存放数据的数据块的指针。

     对于一个文件来说有唯一的索引节点号与之对应,对于一个索引节点号,却可以有多个文件名与之对应。因此,在磁盘上的同一个文件可以通过不同的路径去访问它。

/////////////////////////////////////////////一下节选apue2,4.14文件系统,

普通文件节点

原文:


The i-nodes are fixed-length entries that contain most of the information about a file.

If we examine the i-node and data block portion of a cylinder group in more detail, we could have what is shown inFigure 4.14.


Note the following points from Figure 4.14.

  • We show two directory entries that point to the same i-node entry. Every i-node has a link count that contains the number of directory entries that point to the i-node. Only when the link count goes to 0 can the file be deleted (i.e., can the data blocks associated with the file be released). This is why the operation of "unlinking a file" does not always mean "deleting the blocks associated with the file." This is why the function that removes a directory entry is calledunlink, not delete. In the stat structure, the link count is contained in thest_nlink member. Its primitive system data type isnlink_t. These types of links are called hard links. Recall fromSection 2.5.2 that the POSIX.1 constantLINK_MAX specifies the maximum value for a file's link count.

目录文件节点



原文

We've talked about the concept of a link count for a regular file, but what about the link count field for a directory? Assume that we make a new directory in the working directory, as in

   $ mkdir testdir

Figure 4.15 shows the result. Note that in this figure, we explicitly show the entries for dot and dot-dot.

Figure 4.15. Sample cylinder group after creating the directorytestdir、


The i-node whose number is 2549 has a type field of "directory" and a link count equal to 2. Any leaf directory (a directory that does not contain any other directories) always has a link count of 2. The value of 2 is from the directory entry that names the directory (testdir) and from the entry for dot in that directory. The i-node whose number is 1267 has a type field of "directory" and a link count that is greater than or equal to 3. The reason we know that the link count is greater than or equal to 3 is that minimally, it is pointed to from the directory entry that names it (which we don't show inFigure 4.15), from dot, and from dot-dot in thetestdir directory. Note that every subdirectory in a parent directory causes the parent directory's link count to be increased by 1.

This format is similar to the classic format of the UNIX file system, which is described in detail inChapter 4 of Bach [1986]. Refer toChapter 7 of McKusick et al. [1996] orChapter 8 of McKusick and Neville-Neil [2005] for additional information on the changes made with the Berkeley fast file system. SeeChapter 14 of Mauro and McDougall [2001] for details on UFS, the Solaris version of the Berkeley fast file system.

看来,
普通文件, 它是由i节点和数据块构成的。
而目录文件,是由i节点和目录块构成的。

当我们在终端进入一个目录,比如cd /opt
系统首先要根据文件名opt在所属目录块(/)中找到此文件的节点号,
再由节点号在i节点数组中找到对应条目,
条目里面记录有此文件类型,为d,表示一个目录,也记录着对应目录块在硬盘上的位置,
系统定位到该目录块,读取之--
目录块有各个目录项,表示有哪些文件或目录组织在这个目录下
系统遍历此目录块,将各项列出,

假如此目录下有个text.c文件
执行vi text.c
则系统首先根据text.c文件名在所属目录块(/opt)中找到此文件节点号,
再由节点号在i节点数组中找到对应的条目
条目里面有着这个文件的的类型及其数据块的在硬盘上的位置,
系统定位到该数据块,读取之--


原创粉丝点击