第十一章学习笔记

EX2文件系统数据结构

EXT2文件系统

  • The Second Extended File System (ext2)文件系统是linux系统中的标准文件系统。多年来,Linux一直使用EXT2作为默认文件系统。EXT3是EXT2的扩展。EXT3中增加的主要内容是一个日志文件,它将文件系统的变更记录在日志中。日志可###在文件系统崩溃时更快地从错误中恢复。

EXT2文件系统数据结构

  • 在linux下,通过mkfs创建虚拟磁盘,输入命令:
  • mke2fs [-b blksize -N ninodes] device nblocks 可创建一个带有nblocks个块(每个块大小为blksize个字节)和ninodes个索引节点的EXT2文件系统

虚拟磁盘布局:

  • Block#0:引导块文件系统不会使用它,用来容纳一个引导程序,从磁盘引导操作系统
  • Block#1:超级块,用于容纳整个文件系统的信息。

部分重要字段:

struct ext2_super_block {
u32 s_inodes_count; // Inodes count
u32 s_blocks_count; // Blocks count
u32 s_r_blocks_count; // Reserved blocks count
u32 s_free_blocks_count; // Free blocks count
u32 s_free_inodes_count; // Free inodes count
u32 s_first_data_block; // First Data Block
u32 s_log_block_size; // Block size
u32 s_log_cluster_size; // Allocation cluster size
u32 s_blocks_per_group; // # Blocks per group
u32 s_clusters_per_group; // # Fragments per group
u32 s_inodes_per_group; // # Inodes per group
u32 s_mtime; // Mount time
u32 s_wtime; // Write time
u32 s_mnt_count; // Mount count
u16 s_max_mnt_count; // Maximal mount count
u16 s_magic; // Magic signature
// more non-essential fields
u16 s_inode_size; // size of inode structure
};

Block#2块组描述符块(EXT2将磁盘块分成几个组。每个组有8192个块(硬盘上的大小为32K)。每组用一个块组描述符结构体描述):

struct ext2_ group_ desc (
u32
bg_ block_ bi tmap; // Bmap block number
u32 bg inode_ bi tmap; //Imap b1ock number
u32 bg inode_ table; // Indes begin block number
u16 bg_ free_ blocks_ count ; // THESE are OBVIOUS
u16 bg_ free_ inodes_ count ;
u16 bg_ used_ dirs_ count;
u16 bg_ pad; //ignore these
u32 bg_ reserved[3] ;
};

Block#8:块位图(Bmap)(bg_block_bitmap)位图用来表示某种项的位序列,例如,磁盘块或索引节点。位图用于分配和回收项。在位图中,0位表示对应项处于FREE状态,1位表示对应项处于IN_USE状态。一个软盘有1440块。

Block#9:索引节点位图(Imap)(bg_inode_bitmap)一个索引节点就是用来代表一个文件的数据结构。EXT2文件系统是使用有限数量的索引节点创建的。各索引节点的状态用B9中Imap中的一个位表示。在EXT2 FS中,前10个索引节点是预留的。

索引节点Block#10:索引(开始)节点块(bg_inode_table)每个文件都用一个128字节(EXT4中的是256字节)的独特索引节点结构体表示。

struct ext2_ inode {
u16 i_ mode;// 16 bits - ttttlugsIrwxJrwxIrwxl
u16 i_ uid;//owner uid
u32 i_ size;//file size in bytes
u32 i_ atime;//time fields in seconds
u32 i_ ctime;// since 00:00:00,1-1-1970
u32 i_ mtime;
u32 i_ dtime;
u16 i_ gid;// group ID
u16 i_ 1 inks_ count;// hard-link count
u32 i_ blocks;// number of 512-byte sectors
u32 i_ flags;//IGNORE
u32 i_ reserved1 ;//IGNORE
u32 i_ b1ock[15] ;//See details below
u32 i_ pad[7] ;//for inode size = 128 bytes
}

直接块:i_block[0]至i_block[11]指向直接磁块盘

间接块:i_block[12]指向一个包含256个块编号的磁盘块,每个块编号指向一个磁盘块

双重间接块:i_block[13]指向一个指向256个块的块,每个块指向256个磁盘块

三重间接块:i_block[14]对于小型EXT2文件可忽略

目录条目

目录包含dir_entry_2结构,即:

struct ext2_dir_entry_2 {
u32 inode;
u16 rec_len;
u8 name_len;
u8 file_type;
char name[EXT2_NAME_LEN];
};
  • 遍历EXT2文件系统树

遍历算法:

  • 1.读取超级块
  • 2.读取块组描述符
  • 3.读取InodeBegin Block,以获取/的索引节点
  • 4.将路径名标记为组件字符串
  • 5.从3.中的跟索引节点开始搜索
  • 6.使用索引节点号ino来定位相应的索引节点
  • 7.重复第5第6步

文件系统结构:

  • 第一级别实现基本文件系统树
    = 第二级别实现文件读/写函数
  • 第三级别实现系统的挂载,卸载和文件保护

基本文件系统

  • type.h文件:包含ext2文件系统的数据结构类型
  • global.c文件:这类文件包含文件系统的全局变量

实用程序函数

  • util.c file:该文件包含文件系统常用的实用程序函数
  • get_block/put_block 将虚拟磁盘块读/写到内存的缓冲区中
  • iget(dev,ino) 返回一个指针,指向包含INODE(dev,ino)的内存minode
  • The put(INODE *mip) 释放一个mip指向用完的minode
  • getino() 实现文件系统树遍历算法
  • mkdir命令:创建一个带目录名的新路径
  • rmdir命令:可删除目录
  • 2级文件系统由open,close,lseek,read,write,opendir和readdir组成
  • 挂载操作命令: mount filesys mount_point 允许文件系统包含其他文件系统作为现有文件系统一部分

苏格拉底挑战




实践

用一个文件来模拟一个磁盘;

1.创建一个1M文件,内容全是0;

dd if=/dev/zero of=fs count=256 bs=4k

2.对文件fs格式化

格式化后的fs文件大小依然是1M,但内容已经不是全零。
3.用dumpe2fs工具查看这个分区的超级块和块组描述表信息

(base)
1.
dumpe2fs 1.42.13 (17-May-2015)
Filesystem volume name:  <none>
Last mounted on:     <not available>
Filesystem UUID:     a00715b2-528b-4ca6-8c2b-953389a5ab00
Filesystem magic number: 0xEF53
Filesystem revision #:  1 (dynamic)
Filesystem features:   ext_attr resize_inode dir_index filetype sparse_super
large_file
Filesystem flags:     signed_directory_hash
Default mount options:  user_xattr acl
Filesystem state:     clean
Errors behavior:     Continue
Filesystem OS type:    Linux
Inode count:       128
Block count:       1024
Reserved block count:   51
Free blocks:       986
Free inodes:       117
First block:       1
Block size:        1024
Fragment size:      1024
Reserved GDT blocks:   3
Blocks per group:     8192
Fragments per group:   8192
Inodes per group:     128
Inode blocks per group:  16
Filesystem created:    Fri Aug 21 16:48:02 2020
Last mount time:     n/a
Last write time:     Fri Aug 21 16:48:02 2020
Mount count:       0
Maximum mount count:   -1
Last checked:       Fri Aug 21 16:48:02 2020
Check interval:      0 (<none>)
Reserved blocks uid:   0 (user root)
Reserved blocks gid:   0 (group root)
First inode:       11
Inode size:        128
Default directory hash:  half_md4
Directory Hash Seed:   e5c519af-d42e-43b5-bc8d-c67c5a79bcbe
Group 0: (Blocks 1-1023)
superblock at 1, Group descriptors at 2-2

保留的GDT块位于 3-5
Block bitmap at 6 (+5), Inode bitmap at 7 (+6)
Inode表位于 8-23 (+7)
986 free blocks, 117 free inodes, 2 directories
可用块数: 38-1023
可用inode数: 12-128

posted @ 2023-10-15 11:04  20211308wjc  阅读(6)  评论(0编辑  收藏  举报