当前位置:首页 > 操作系统实验文件系统设计
strcpy(dir_buf[2].d_name, \ dir_buf[2].d_ino=2; fseek(fd,DATASTART,SEEK_SET); fwrite(dir_buf,1,3*(DIRSIZ+2),fd); iput(inode); inode=iget(2); // 2 etc dir id inode->di_number=1; inode->di_mode=DEFAULTMODE|DIDIR; inode->di_size=3*(DIRSIZ+2); inode->di_addr[0]=1; //block 0#is used by the etc directory strcpy(dir_buf[0].d_name, \ dir_buf[0].d_ino=1; strcpy(dir_buf[1].d_name, \ dir_buf[1].d_ino=2; strcpy(dir_buf[2].d_name, \ dir_buf[2].d_ino=3; fseek(fd,DATASTART+BLOCKSIZ*1,SEEK_SET); fwrite(dir_buf,1,3*(DIRSIZ+2),fd); iput(inode); inode=iget(3);//3 password id inode->di_number=1; inode->di_mode=DEFAULTMODE|DIDIR; inode->di_size=BLOCKSIZ; inode->di_addr[0]=2; //block 2#is used by the password file for(i=5;i // begin with 4,0,1,2,3,is used by main,etc,password filsys.s_inode[i]=4+i; } filsys.s_pinode=0; filsys.s_rinode=NICINOD+4; block_buf[NICFREE-1]=FILEBLK+1; //FILEBLK+1 is a flag of end for(i=0;i filsys.s_free[NICFREE-1+i-j]=i; } filsys.s_pfree=NICFREE-1-j+3; filsys.s_pinode=0; fseek(fd,BLOCKSIZ,SEEK_SET); fwrite(&filsys.s_isize,1,sizeof(filsys),fd); fseek(fd,BLOCKSIZ,SEEK_SET); fread(&filsys.s_isize,1,sizeof(filsys),fd); } 5.进入文件系统程序install()(文件名install.c) #include //1.read the filsys from the superblock fseek(fd,BLOCKSIZ,SEEK_SET); fread(&filsys,1,sizeof(struct filsys),fd); //2.initialize the inode hash chain for(i=0;i //3.initialize the sys_file for(i=0;i // 4.initialize the user for(i=0;i // 5 read the main directory to initialize the dir cur_path_inode=iget(1); dir.size=cur_path_inode->di_size/(DIRSIZ+2); for(i=0;i 6.退出程序halt()(文件名halt.c) #include // 1.write back the current dir chdir(\ iput(cur_path_inode); // 2.free the u_ofile and sys_ofile and inode for(i=0;i // 3.write back the filesys to the disk fseek(fd,BLOCKSIZ,SEEK_SET); fwrite(&filsys,1,sizeof(struct filsys),fd); // 4.close the file system column fclose(fd); // 5.say GOODBYE to all the user printf(\ exit(0); } 7.获取释放i节点内容程序iget()/iput()(文件名igetput.c) #include struct inode *iget(dinodeid) //iget() unsigned int dinodeid; { int existed=0,inodeid; long addr; struct inode *temp,*newinode; inodeid=dinodeid%NHINO; if(hinode[inodeid].i_forw==NULL) existed=0; else { temp=hinode[inodeid].i_forw; while(temp) { if(temp->i_ino==inodeid) { //existed existed=1; temp->i_count++; return temp; } Else { // not existed temp=temp->i_forw; } } } //not existed //1.calculate the addr of the dinode in the file sys column addr=DINODESTART+dinodeid*DINODESIZ; // 2.malloc the new inode newinode=(struct inode *)malloc(sizeof(struct inode)); // 3.read the dinode to the inode fseek(fd,addr,SEEK_SET); fread(&(newinode->di_number),DINODESIZ,1,fd); // 4.put it into hinode[inodeid] queue newinode->i_forw=hinode[inodeid].i_forw; newinode->i_back=newinode; if(newinode->i_forw!=NULL) newinode->i_forw->i_back=newinode; hinode[inodeid].i_forw=newinode; // 5.initialize the irlode newinode->i_count=1; newinode->i_flag=0; newinode->i_ino=dinodeid; newinode->di_size=3*(DIRSIZ+2); if(dinodeid==3) newinode->di_size=BLOCKSIZ; return newinode; } iput(pinode) // iput() struct inode *pinode; { long addr; unsigned int block_num; int i; if(pinode->i_count>1) { pinode->i_count--; return; } else { if(pinode->di_number!=0) { //write back the inode addr=DINODESTART+pinode->i_ino*DINODESIZ; fseek(fd,addr,SEEK_SET); fwrite(&pinode->di_number,DINODESIZ,1,fd); } else { // rm the inode & the block of the file in the disk block_num=pinode->di_size/BLOCKSIZ; for(i=0; i // free the inode in the memory if(pinode->i_forw==NULL) pinode->i_back->i_forw=NULL; else { pinode->i_forw->i_back=pinode->i_back; pinode->i_back->i_forw=pinode->i_forw; } ifree(pinode); } } 8.i节点分配和释放函数ialloc()和ifree()(文件名iallfre.c) #include static struct dinode block_buf[BLOCKSIZ/DINODESIZ]; struct inode *ialloc() //ialloc { struct inode *temp_inod; unsigned int cur_di; int i,count,block_end_flag; if(filsys.s_pinode==NICINOD) { // s_inode empty i=0; count=0; block_end_flag=1; filsys.s_pinode=NICINOD-1; cur_di=filsys.s_rinode; while((count
共分享92篇相关文档