vx6中mkfs.c代码写注释
时间: 2024-02-25 21:56:48 浏览: 23
以下是对vx6中mkfs.c代码的注释:
```
/*
* mkfs.c - file system initialization program
*/
#include "param.h"
#include "buf.h"
#include "inode.h"
#include "filsys.h"
#define NINOBLK (BSIZE/sizeof(struct dinode))
#define NDIRECT (BSIZE/sizeof(struct direct))
struct filsys sblock; /* the file system super block */
int fmod = 0; /* is the file system already modified? */
int fso, fsi; /* file system device file descriptors */
unsigned int fmax; /* number of blocks in file system */
unsigned char *fbuf; /* buffer for file system super block */
/*
* main - file system initialization program
*/
main(argc, argv)
int argc;
char *argv[];
{
register struct dinode *ip;
struct direct dp;
int n, i, j;
daddr_t bn;
char filename[50];
/* Open file system device file */
if(argc < 2) {
printf("Usage: mkfs filsys\n");
exit(1);
}
fso = open(argv[1], 1);
if(fso < 0) {
printf("Can't open %s\n", argv[1]);
exit(1);
}
/* Compute the number of blocks in the file system */
fsi = open(argv[1], 0);
fmax = 0;
while(read(fsi, fbuf, BSIZE) == BSIZE)
fmax++;
close(fsi);
/* Compute the number of inodes in the file system */
sblock.s_isize = roundup(fmax/16, BSIZE) + 2;
sblock.s_fsize = fmax;
/* Initialize the super block */
sblock.s_flock = 0;
sblock.s_ilock = 0;
sblock.s_fmod = 0;
sblock.s_ronly = 0;
sblock.s_time = 0;
sblock.s_tfree = fmax;
sblock.s_tinode = sblock.s_isize * NINOBLK;
sblock.s_dinfo[0] = 0;
sblock.s_dinfo[1] = 0;
sblock.s_dinfo[2] = 0;
sblock.s_dinfo[3] = 0;
sblock.s_dinfo[4] = 0;
sblock.s_dinfo[5] = 0;
sblock.s_dinfo[6] = 0;
sblock.s_dinfo[7] = 0;
/* Write the super block to disk */
lseek(fso, SUPERBOFF, 0);
write(fso, &sblock, sizeof(sblock));
/* Initialize the inode blocks */
n = sblock.s_isize;
ip = (struct dinode *) fbuf;
for(i = 2; i < n; i++) {
bzero(fbuf, BSIZE);
for(j = 0; j < NINOBLK; j++, ip++) {
ip->di_mode = 0;
ip->di_nlink = 0;
ip->di_uid = 0;
ip->di_gid = 0;
ip->di_size = 0;
bn = itod(i) * BSIZE + j * sizeof(struct dinode);
lseek(fso, bn, 0);
write(fso, ip, sizeof(struct dinode));
}
}
/* Initialize the root directory */
bzero(fbuf, BSIZE);
ip = (struct dinode *) fbuf;
ip->di_mode = IFDIR | 0777;
ip->di_nlink = 2;
ip->di_uid = 0;
ip->di_gid = 0;
ip->di_size = sizeof(struct direct);
ip->di_addr[0] = itod(2);
lseek(fso, itod(2) * BSIZE, 0);
write(fso, fbuf, BSIZE);
/* Add "." and ".." entries to the root directory */
dp.d_ino = 1;
strncpy(dp.d_name, ".", DIRSIZ);
lseek(fso, itod(2) * BSIZE, 0);
write(fso, &dp, sizeof(dp));
dp.d_ino = 1;
strncpy(dp.d_name, "..", DIRSIZ);
write(fso, &dp, sizeof(dp));
/* Initialize the free block list */
bzero(fbuf, BSIZE);
fbuf[0] |= BUSY;
for(i = 2; i < fmax; i++)
write(fso, fbuf, BSIZE);
/* All done */
close(fso);
exit(0);
}
```
上面代码的主要作用是:
1. 定义了文件系统超级块结构体 `struct filsys` 和磁盘上的 `inode` 结构体 `struct dinode`。
2. 定义了文件系统初始化工具 `mkfs` 的全局变量,包括文件系统设备文件描述符、文件系统设备文件的块数量、文件系统超级块等。
3. 通过读取命令行参数,打开文件系统设备文件。
4. 计算文件系统中的块数量和inode数量,并初始化文件系统的超级块结构体。
5. 将文件系统的超级块写入磁盘,并初始化inode块和根目录。
6. 最后,初始化空闲块链表,写入磁盘,完成文件系统的初始化工作。
总体而言,mkfs.c的作用是初始化vx6文件系统,并在磁盘上创建一个文件系统,以便操作系统可以使用这个文件系统来存储和管理文件和目录等数据。