os-xv6分析-文件系统-fs.h 代码分析
// fs.h 主要定义了超级块superblock、索引节点dinde和目录项dirent,其实声明//了文件系统中的常量。
// On-disk file system format. // 磁盘文件系统格式。
// Both the kernel and user programs use this header file.
// 内核和用户程序都使用这个头文件。
#define ROOTINO 1 // root i-number // 根i数
#define BSIZE 512 // block size // 块大小
// Disk layout:
// [ boot block | super block | log | inode blocks |
// free bit map | data blocks]
//
// mkfs computes the super block and builds an initial file system. The
// super block describes the disk layout:
// 定义超级块superblock
struct superblock {
uint size; // Size of file system image (blocks)
//文件系统映像的大小(块)
uint nblocks; // Number of data blocks
// 数据块的数量
uint ninodes; // Number of inodes.
//索引节点数。
uint nlog; // Number of log blocks //日志块数
uint logstart; // Block number of first log block //第一个日志块的块号
uint inodestart; // Block number of first inode block //第一个inode块的块数
uint bmapstart; // Block number of first free map block
// 第一个空闲地图块的块号
};
#define NDIRECT 12 //一级寻址的12个块大小
#define NINDIRECT (BSIZE / sizeof(uint))
#define MAXFILE (NDIRECT + NINDIRECT) //块大小
// On-disk inode structure //索引节点数据项
struct dinode {
short type; // File type //文件类型
short major; // Major device number (T_DEV only)
// 主要设备编号(仅限T_DEV)
short minor; // Minor device number (T_DEV only)
// 次要设备编号(仅限T_DEV)
short nlink; // Number of links to inode in file system
// 文件系统中指向索引节点的链接数
uint size; // Size of file (bytes) // 文件大小
uint addrs[NDIRECT+1]; // Data block addresses // 数据块地址
};
// Inodes per block.
#define IPB (BSIZE / sizeof(struct dinode))
// 每个盘块上可以记录的inode数量
// Block containing inode i
#define IBLOCK(i, sb) ((i) / IPB + sb.inodestart)
// 计算第i个inode位于哪个盘块
// Bitmap bits per block
#define BPB (BSIZE*8) // 一个盘块的位数
// Block of free map containing bit for block b
#define BBLOCK(b, sb) (b/BPB + sb.bmapstart)
// 用于计算第b个数据盘块所对应的位图位所在的盘块号
// Directory is a file containing a sequence of dirent structures.
#define DIRSIZ 14 // 目录项中文件名字符串的大小
// 定义目录项数据结构
struct dirent {
ushort inum;
char name[DIRSIZ];
};