您好,欢迎访问三七文档
当前位置:首页 > 商业/管理/HR > 质量控制/管理 > Nachos 实验10 设计并实现具有二级索引的文件系统
实验目的Nachos系统原有的文件系统只支持单级索引,最大能存取NumDirect*SectorSize的大小的文件,本次试验的目的:理解文件系统的组织结构扩展原有的文件系统,设计并实现具有二级索引的文件系统。实验环境linux操作系统,Nachos操作系统实验分析已知在文件头的定义中描述了:#defineNumDirect((SectorSize-2*sizeof(int))/sizeof(int))为了说明方便,经过实际计算,NumDirect=30.二级索引的文件系统的filehdr首先,通过观察Nachos原有的filehdr(即上图左边的部分),可知Nachos的单级索引的文件系统最大只支持存取29个扇区大小的文件。为了扩展二级索引,取数组的最后一个dataSectors[29]作为存取新的dataSectors数组块的索引,定义dataSectors[0]-dataSectors[28]存取数据所在的块号,dataSectors[29]==-1表示无二级索引块,为正值表示二级索引dataSectors2所在的索引块。当文件超过原dataSectors数组所能能够存取的大小28的时候,通过bitmap为文件头的dataSectors2分配空间,返回的Sector号存在dataSectors[29]中。fileSys每次读取filehdr的时候,仍然只读取原filehdr,如果想要访问和修改dataSectors2中的内容,则在filehdr中先通过dataSectors[29]获取到dataSectors2的扇区号,通过调用synchDisk-ReadSector(dataSectors[lastIndex],(char*)dataSectors2),读入dataSectors2的内容,然后再进行dataSectors数组29-62号所对应的数据块的读取。因为本次实验是在实验5的基础上进行更改的,即支持文件的扩展,这就要求不仅要有读取dataSectors2数组的方法,还要可以重新写入dataSectors2的方法。实现方法也就是首先如果需要访问dataSectors2,那么首先调用synchDisk-ReadSector(dataSectors[lastIndex],(char*)dataSectors2),读入dataSectors2的内容,然后进行各种应用程序的读写操作,最后调用synchDisk-WriteSector(dataSectors[lastIndex],(char*)dataSectors2),将更改后的结果写回。由分析可知,文件系统的二级索引功能的扩展只针对filehdr,所有的修改的都只在filehdr.cc中进行,连头文件filehdr.h也不涉及。关键源代码及注释——filehdr.cc在头文件中添加dataSectors2的大小定义:#defineNumDirect2(SectorSize/sizeof(int))更改MaxFileSize:#defineMaxFileSize((NumDirect+NumDirect2)*SectorSize)转向filehdr.cc进行说明,所有函数体中绿色部分均为本次实验的注释(建议看Allocate和Deallocate就好了,其他的原理类似,appSectors涉及实验5的部分)://filehdr.cc//省略无数责任声明////@LiZhen17/11/09////Extendsthefilesystemtodoublethemaxfilesizethat//Nachoscanstore#includecopyright.h#includesystem.h#includefilehdr.hAllocate//----------------------------------------------------------------------//FileHeader::Allocate//Initializeafreshfileheaderforanewlycreatedfile.//Allocatedatablocksforthefileoutofthemapoffreediskblocks.//ReturnFALSEiftherearenotenoughfreeblockstoaccomodate//thenewfile.////freeMapisthebitmapoffreedisksectors//fileSizeisthebitmapoffreedisksectors////@LiZhen17/11/09////Extendsthefilesystemtodoublethemaxfilesizethat//Nachoscanstore//CurrentmaxfilesizeofNachosfilesystemis//(NumDirect+NumDirect2)*SectorSize////IfthedataSectors[NumDirect-1]==-1,nosecondaryindex.//otherwise,usethedataSecotrs[NumDirect-1]tostroethe//pointertothesecondaryindexblock--dataSectors2[].//----------------------------------------------------------------------boolFileHeader::Allocate(BitMap*freeMap,intfileSize){numBytes=fileSize;numSectors=divRoundUp(fileSize,SectorSize);if(freeMap-NumClear()numSectors)returnFALSE;//notenoughspaceelseif(NumDirect+NumDirect2=numSectors)returnFALSE;//notenoughpointerspace//FirstfigureoutthecurrentlengthofdataSectors//dataSectorsarrayindexrangesfrom0tolastIndex-1intlastIndex=NumDirect-1;//Ifdonotneedthesecondaryindex,//donotchangetheoriginalcodeexcept//assigndataSectors[lastIndex]=-1if(numSectorslastIndex){for(inti=0;inumSectors;i++)dataSectors[i]=freeMap-Find();dataSectors[lastIndex]=-1;}//IfthenumSectorsexcendstherageofdataSectors,//firsthandlethefirst0--lastIndex-1asbefore.//Then,askbitmaptoallocateanewsectortostroe//theSecondaryindexblock--dataSectors2.//Atlast,writebackthesecondaryindexblockintothesector.else{for(inti=0;ilastIndex;i++)dataSectors[i]=freeMap-Find();dataSectors[lastIndex]=freeMap-Find();intdataSectors2[NumDirect2];//secondaryindexblockfor(inti=0;inumSectors-NumDirect;i++)dataSectors2[i]=freeMap-Find();synchDisk-WriteSector(dataSectors[lastIndex],(char*)dataSectors2);}returnTRUE;}AppSectors//---------------------------------------------------------//FileHeader::AppSectors//@LiZhen10/31/2009//Inlab5//addappFileSizemorebytesinthecurrentfile//refreshthebitmaptoshowtheallocatingchanges.//@LiZhen17/11/09//Inlab10//HandlethereadingandwritingofthedataSectors2//ifneeded.//---------------------------------------------------------boolFileHeader::AppSectors(BitMap*freeMap,intappFileSize){if(appFileSize=0)returnfalse;intrestFileSize=SectorSize*numSectors-numBytes;//printf(themoreFileSizeis%d\n,moreFileSize);//printf(theappFileSizeis%d\n,appFileSize);if(restFileSize=appFileSize){numBytes+=appFileSize;returntrue;}else{intmoreFileSize=appFileSize-restFileSize;if(freeMap-NumClear()divRoundUp(moreFileSize,SectorSize))returnFALSE;elseif(NumDirect+NumDirect2=numSectors+divRoundUp(moreFileSize,SectorSize))returnFALSE;inti=numSectors;numBytes+=appFileSize;numSectors+=divRoundUp(moreFileSize,SectorSize);/***Inlab10**///FirstfigureoutthecurrentlengthofdataSectors//dataSectorsarrayindexrangesfrom0tolastIndex-1intlastIndex=NumDirect-1;/**Ifbeforeappending,thereisnosecondaryindex*/if(dataSectors[lastIndex]==-1){//Ifafterbeingappended,newdataSecotersdoNOTneedthesecondaryindex,//donotchangetheoriginalcode.if(numSectorslastIndex)for(;inumSectors;i++)dataSectors[i]=freeMap-Find();//Ifafterbeingappended,newdataSecotersDOneedthesecondaryindex,//firsthandlethefirst0--lastIndex-1asbefore.//Then,askbitmaptoallocateanewsectortostroe//theSecondaryindexblock--dataSectors2.//Atlast,writebackthesecondaryindexblockintothesector.else{for(;ilastIndex;i++)dataSectors[i]
本文标题:Nachos 实验10 设计并实现具有二级索引的文件系统
链接地址:https://www.777doc.com/doc-5100474 .html