Operating system course design of Software College of Shandong University Nachos Experiment 5 file system with secondary index

Posted by kb0000 on Sat, 15 Jan 2022 14:47:04 +0100

Explanation: these experiments written in blog are not comprehensive and are not final versions of the experimental reports (which are simple notes recorded in typora in their own experiments). The complete content (including code + experimental report) can be downloaded by links, or WeChat official account is paid attention to "free blog" for free.

1, Secondary index

1. Problem analysis

In... / lab4, the function of nachos file system is improved so that it has the function of extending the file length. However, the maximum length of the file after file expansion cannot exceed the maximum capacity of 3840, that is, only the primary index can be used. Therefore, the function to be realized in this experiment is the secondary index of nachos.

There are explanations about Nachos secondary index in the experimental instruction. In the current design of Nachos, the file data space is allocated by primary index. The file header (inode) can only record NumDirect (this value is equal to 30) fan numbers. Therefore, the maximum capacity of a Nachos file is only NumDirect*SectorSize=3840 bytes. Now our task is to add a multi-level index method similar to Unix file system in Nachos file system, expand the fan number that Nachos can record, and increase the maximum capacity of Nachos file.

View the source file filehdr h. You can see that two variables NumDirect and MaxFileSize are defined, which respectively represent the maximum number of sector numbers and file size that the original primary index can record in nachos.

The general idea is to first define the Sector address of the dataSector2 array to store the secondary index, whether the last mark of the primary index array uses the secondary index, and the address of the secondary index array. If the secondary index is not used, the dataSector[numDirect] in the primary index array is assigned to - 1. If the secondary index is used, the dataSector[numDirect] stores the block number of the secondary index dataSector2. When it is determined that the file size exceeds the capacity of the primary index, allocate space for dataSectors2 of the file header through bitmap, and the returned Sector number is stored in the last item of the primary index array. When reading file data, first read the data through the primary index, then obtain the address of the secondary index through the last item of the primary index, and then access the secondary index to read the file data.

The ExtendSpace method implemented by lab4 needs to be modified to make it have more space to expand and handle secondary indexes; Modify the Allocate method so that it can Allocate secondary indexes when creating files; Modify the Deallocate method so that it can process the secondary index when releasing space; Modify the ByteToSector method so that it can query the data under the secondary index; Finally, the Print method is modified to add the printing of relevant contents of the secondary index, which is convenient for debugging the output and verifying the correctness of the results.

2. Implement secondary index

First in filehdr H file defines the maximum number of sectors of the file and the number of sector numbers that can be recorded by the secondary index under the secondary index mechanism

#define MaxNumSector   ((SectorSize*2 - 3 * sizeof(int)) / sizeof(int))
#Define numdirect2 (sectorsize / sizeof (int)) / / secondary index

Modify filehdr The ExtendSpace method in CC file increases the creation of secondary indexes

//lab5 changed+++++++++++++++++++++++++++++++
bool
FileHeader::ExtendSpace(OpenFile *bitmapfile,int newSize)
{
    int newNumSectors  = divRoundUp(newSize, SectorSize);
    if(newNumSectors == numSectors){
        numBytes = newSize; 
        return true;
    }
	//Sectors to be extended
    int diffSector = newNumSectors - numSectors;    
    BitMap *freeMap = new BitMap(NumSectors);
    freeMap->FetchFrom(bitmapfile);	
   //The file is too large (exceeding the secondary index) | insufficient disk space
    if(newNumSectors>MaxNumSector||freeMap->NumClear()< diffSector){
		return false;
    }	
    //Allocate sectors and modify file header information
	int i;
	//No secondary index is required
	if (newNumSectors<NumDirect){ 
		for(i = numSectors; i<newNumSectors; i++)
		{
			dataSectors[i] = freeMap->Find();
		}
	//Secondary index required
	}else{	
		//Secondary index
		int dataSectors2 [NumDirect2];
		//Secondary index not used before expansion
		if(numSectors<NumDirect){	
			for(i = numSectors; i<NumDirect; i++)
			{
				dataSectors[i] = freeMap->Find();
			}
			//Secondary index part
			for (i=0; i < newNumSectors-NumDirect+1; i++)
				dataSectors2[i] = freeMap->Find();
		}else{//The secondary index has been used before expansion
			//Read secondary index
			synchDisk->ReadSector(dataSectors[NumDirect-1], (char *)dataSectors2);
			for (i=numSectors-NumDirect+1; i < newNumSectors-NumDirect+1; i++)
				dataSectors2[i] = freeMap->Find();		
		}
		//The secondary index address is saved in the last item of the primary index, dataSectors[NumDirect-1]
		synchDisk->WriteSector(dataSectors[NumDirect-1], (char *)dataSectors2);
	}
	//Update header information
    numBytes = newSize;
    numSectors = newNumSectors;
    
    freeMap->WriteBack(bitmapfile);
    return true;
}
//lab5 changed+++++++++++++++++++++++++++++++

Modify filehdr The Allocate method in CC file can increase the size of allocable files and create secondary indexes

Modify filehdr Deallocate method in CC file to increase the release of secondary index space

Modify filehdr The ByteToSector method in the CC file increases the return of the sector number under the secondary index

Modify filehdr The Print method in CC file prints out the data related to the secondary index

3. Testing

(for more details and code, please refer to the shared materials. The acquisition method is described at the beginning of the article.)

Topics: Operating System