计算机操
作系统
课程设计说明书
Linux-ext2 文件系统
起止日期: 2009 年 12 月 30 日 至 2010 年 1 月 8 日
学 生 姓 名 肖君诺
班 级 计 073
学 号
07408100321
成 绩
指 导 教 师 ( 签 字 )
计通学院(部)
年 月 日
目录
1 课程设计简介..................................................................................................................................1
1.1 课程设计的目的...................................................................................................................1
..
1
1.2 课程设计内容.......................................................................................................................1
2 数据结构的设计..............................................................................................................................2
2.1 预定义...................................................................................................................................2
2.2 结构体...................................................................................................................................2
//磁盘 i 结点结构,....................................................................................................................2
struct inode{................................................................................................................................2
// char di_name[DIRSIZ];..........................................................................................................2
unsigned __int16 di_ino; /*磁盘 i 节点标识*/..........................................................................3
unsigned __int16 di_number; /*关联文件数,当为 0 时表示删除文件*/.............................3
unsigned __int16 di_mode; /*存取权限*/.................................................................................3
unsigned __int16 di_uid; /*磁盘 i 节点用户 id*/......................................................................3
unsigned __int16 di_gid; /*磁盘 i 节点权限组 id*/ //1 管理员组 2 用户组...........................3
unsigned __int32 di_size; /*文件大小*/...................................................................................3
unsigned __int32 di_ctime; /* Creation time */.........................................................................3
unsigned __int32 di_mtime; /* Modification time */................................................................3
unsigned __int16 di_block[P_N_BLOCKS]; /* 一组 block 指针 */.......................................3
};.................................................................................................................................................3
// 目录项结构..............................................................................................................................3
struct direct{................................................................................................................................3
char d_name[DIRSIZ]; /*目录名(14 字节)*/......................................................................3
__int16 d_ino; /*目录号*/.........................................................................................................3
};.................................................................................................................................................3
//超级快结构...............................................................................................................................3
struct super_block{.....................................................................................................................3
unsigned __int16 s_inodes_count; /* inodes 计数 */................................................................3
unsigned __int16 s_blocks_count; /* blocks 计数 */................................................................3
unsigned __int16 s_r_blocks_count; /* 保留的 blocks 计数 */...............................................3
unsigned __int16 s_free_blocks_count; // 空闲的 blocks 计数...............................................3
unsigned __int16 s_free_inodes_count; /* 空闲的 inodes 计数 */..........................................3
unsigned __int16 s_free_blocks_group[GROUPNUM];//新增 一个数组来记录每个数据块
组中的空闲数据块计数.............................................................................................................3
unsigned __int16 s_first_data_block; /* 第一个数据 block */.................................................3
unsigned __int16 s_log_block_size; /* block 的大小 */..........................................................3
unsigned __int16 s_blocks_per_group; /* 每 block group 的 block 数量 */...........................3
unsigned __int16 s_inodes_per_group; /* 每 block group 的 inode 数量 */...........................3
};.................................................................................................................................................3
// 用户密码..................................................................................................................................3
struct pwd{..................................................................................................................................3
unsigned __int8 p_uid;...............................................................................................................3
unsigned __int8 p_gid;...............................................................................................................3
char username[UNAMSIZ];/*用户名 新加的*/.......................................................................3
char password[UPWDSIZ];.......................................................................................................3
};.................................................................................................................................................3
// 目录结构..................................................................................................................................3
..
2
struct dir{.....................................................................................................................................3
struct direct direct[DIRNUM];...................................................................................................3
__int16 size;...............................................................................................................................3
};.................................................................................................................................................3
2.3 全局变量和函数...................................................................................................................4
// 全局变量..................................................................................................................................4
unsigned __int8 di_bitmap[DINODENUM]; // 硬盘 inode 节点位图 1 表示已使用 0 表示未
使用.............................................................................................................................................4
unsigned __int8 bk_bitmap[DATABLKNUM]; // 数据块 block 位图......................................4
struct super_block filsys; //超级块.............................................................................................4
struct pwd pwd[PWDNUM];......................................................................................................4
FILE *fd; //文件指针..................................................................................................................4
struct inode *cur_inode; //i 节点当前目录指针........................................................................4
struct inode *inodetemp; //i 节点指针........................................................................................4
const char fsystemname[20]="Linux.EXT2"; //模拟硬盘的文件名.........................................4
struct direct dir_buf[BLOCKSIZ / sizeof(struct direct)]; //目录数组.......................................4
char cmdhead[20];//cmd 的头 表示所在哪个文件夹 、..........................................................4
int i_lock=0;//inode 位图锁 可能会多线程...............................................................................4
int b_lock=0;//block 位图锁.......................................................................................................4
struct pwd *cur_user;..................................................................................................................4
/* 全局函数 */............................................................................................................................4
extern int Format();//格式化磁盘...............................................................................................4
extern int Install();//启动,安装文件系统................................................................................4
struct inode * read_inode(int);//install 里面读取文件 dinode...................................................4
struct direct * read_dir_data(int);//读取存储文件夹的物理块.................................................4
extern void showdir();//命令 dir.................................................................................................4
int Enterdir(char[]);//进入某个文件夹 命令-- cd 文件名.........................................................4
int Fd_dirfile(char[]);//查找当前目录里的文件 没找到返回-1 找到返回 inode 号...............4
int Iscmd(char[]);//判断是否两个字符串的命令......................................................................4
void two_cmd(char[],char[]);//两个字符串的命令...................................................................4
int creat(char[]);//创建文件........................................................................................................4
void changeinode();//交换指针..................................................................................................4
char * ReadFile(char[]);//读取文件............................................................................................4
int mkdir(char[]);//创建文件夹..................................................................................................4
void showbitmap();//显示位图...................................................................................................4
int deletefd(char[]);//删除文件...................................................................................................4
int editfile(char[]);//编辑文件.....................................................................................................4
int rename(char[]);//重命名........................................................................................................4
void showhelp();//命令帮助.......................................................................................................4
void login();.................................................................................................................................4
void logout();...............................................................................................................................4
int access();//权限判断...............................................................................................................4
/*磁盘 i 节点的分配与释放(当一个新文件被建立的时候,在给该文件分配磁盘存储区
之前,.........................................................................................................................................4
..
3
应为该文件分配存放该文件说明信息的磁盘 i 节点,当从文件系统中删除某个文件时
.....................................................................................................................................................4
应首先删除它的磁盘 i 节点项。)*/.......................................................................................4
int ialloc();/*开辟一个空闲的 i 节点,返回 i 节点*///磁盘块分配与释放函数....................4
int balloc(int);//申请硬盘空间...................................................................................................4
结构体说明:.............................................................................................................................5
3 功能模块(或算法)描述..............................................................................................................5
3.1 format 格式化.......................................................................................................................5
只写打开模拟文件,每个数据块 512 字节,第 2 块是超级块,第 3 块是 inode 位图,第
4 块是 block 位图,第 5 块开始有 32 块是 inode 节点,然后是数据区 block,512 块。. .5
函数功能:初始化超级快,初始化 dinode 位图 block 位图,初始化主目录,初始化 etc
目录,初始化管理员 admin 目录,初始化用户 xiao 目录,初始化 用户 passwd 文件,
写入模拟硬盘文件。.................................................................................................................5
3.2 install 安装............................................................................................................................5
读写打开模拟文件,读取 dinode 位图 block 位图,读取主目录,读取 etc 目录,读取管
理员 admin 目录,读取用户 xiao 目录,读取 用户 passwd 文件。.....................................5
3.2 login 登陆..............................................................................................................................5
用户输入用户名和密码,在 passwd 文件中查找是否有此用户,核对密码。正确则登陆
成功,当前目录设定到当前用户文件夹下。........................................................................5
3.3 ialloc 申请 inode 空间..........................................................................................................5
先检测 inode 位图是否加锁,是则退出。加锁,检测 inode 空间是否还有已满,是则退
出。在 inode 位图中顺序查找空闲的 inode,找到则返回 inode 地址,block 解锁。函数
结束。流程图 3.3.......................................................................................................................5
.....................................................................................................................................................6
ialloc 流程图 3.3 block 流程图 3.4...........................................................................................6
3.4 balloc 申请 block 空间.........................................................................................................6
先检测 block 位图是否加锁,是则退出。加锁,检测 block 空间是否还有 k 个空闲,否
则退出。在还有空闲 block 的组中查找是否有 k 个空闲,没有则去下一个 block 组中查
找,找到则返回 block 地址,block 解锁。函数结束。流程图 3.4......................................6
3.5 create 创建文本文件............................................................................................................6
查找当前目录下是否有同名文件,是则退出,否则让用户输入文本文件内容,以‘ ###’
结束。申请 inode 空间(ialloc 函数),申请硬盘 block 空间(balloc 函数),申请失败
则结束。将文件内容写入 inode 空间和 block 空间。修改当前目录的结构,修改超级快,
修改 inode 位图,block 位图,写入模拟硬盘。流程图 3.5..................................................6
.....................................................................................................................................................7
create 流程图 3.5 cdir 流程图 3.6...............................................................................................7
3.7 cdir 创建文件夹....................................................................................................................7
查找当前目录下是否有同名文件,是则退出,否则,申请 inode 空间(ialloc 函数),申请
硬盘 block 空间(balloc 函数),申请失败则结束。将文件夹内容写入 inode 空间和
block 空间。修改当前目录的结构,修改超级快,修改 inode 位图,block 位图,写入模
拟硬盘。流程图 3.6...................................................................................................................7
3.7 read edit –读取和追加文本文件..........................................................................................7
Read----查找当前目录下是否该文件,没有则退出,否则调用 access()权限判断,有
权限则判断是不是文件,不是则退出,是文件则读取文件并显示。流程图 3.7(1)。
..
4
.....................................................................................................................................................7
Edit----调用读取文件模块,读取成功则用户输入追加的内容,如果追加的内容大于一
个 block 则申请 block 空间,失败则退出,申请成功则保存文件。流程图 3.7(2).......7
.....................................................................................................................................................8
流程图 3.7(1) 流程图 3.7(2)............................................................................................8
3.8 access(文件名) 权限判断...............................................................................................8
先判断当前目录是否有该文件,在当前目录的 block 找到该文件,判断当前登录用户
是哪个组,判断是否该用户创建,判断该文件的可见级别。............................................8
如果是该用户创建的 则有读写权限如果当前用户是管理员组的 也具有读写权限如果
该文件是用户可查看文件则都具有权限。............................................................................8
int access()...................................................................................................................................8
{if(inodetemp->di_uid==cur_user->p_uid)//如果是该用户创建的 则有读写权限................8
return 1;......................................................................................................................................8
else if(cur_user->p_gid==0)//如果当前用户是管理员组的 也具有读写权限......................8
return 1;......................................................................................................................................8
else if(inodetemp->di_gid==1)//如果该文件是用户可查看文件 则都具有权限..................8
return 1;......................................................................................................................................8
return 0; }...................................................................................................................................8
3.9 cd –进入子目录 或上级目录...............................................................................................8
查找当前目录是否有该子目录,没有则退出,调用 access()判断当前用户是否有权
限,无则退出,有则读取该子目录的 inode,将当前目录指向该目录。..........................8
3.10 attr(文件名)查看文件或者文件夹的属性...................................................................8
先查找当前目录下是否有该文件或目录,有则判断文件是否系统文件,是否文本文件,
是否目录,由谁创建,属于什么组,占用的 block,和 inode。将其全部显示出来。....8
void showaccess(char strname[20])............................................................................................9
{int fi;..........................................................................................................................................9
fi=Fd_dirfile(strname);//查找该文件夹下是否有文件............................................................9
if(fi==-1).....................................................................................................................................9
{printf("该文件夹下没有 %s 文件或文件夹\n",strname);......................................................9
return ; }.....................................................................................................................................9
inodetemp=read_inode(fi);.........................................................................................................9
printf("---%s---属性\n",strname);..............................................................................................9
if((inodetemp->di_mode&DIMODE_SYSTEM)==DIMODE_SYSTEM)..............................9
{printf("系统文件\n");...............................................................................................................9
}..................................................................................................................................................9
if((inodetemp->di_mode&DIMODE_FILE)==DIMODE_FILE)............................................9
printf("文本文件\n");.................................................................................................................9
if((inodetemp->di_mode&DIMODE_DIR)==DIMODE_DIR)................................................9
printf("文件夹\n");.....................................................................................................................9
printf("占据 inode %d,block %d\n",inodetemp->di_ino,inodetemp->di_block[0]);.................9
if(inodetemp->di_gid==0).........................................................................................................9
printf("由%s 创建,属于管理员组\n",pwd[inodetemp->di_uid-1].username);.....................9
else if(inodetemp->di_gid==1)..................................................................................................9
printf("由%s 创建,属于普通用户组\n",pwd[inodetemp->di_uid-1].username);}...............9
..
5
- 1
- 2
- 3
- 4
- 5
前往页