Nachos文件系统目录管理的解读
首先在实现目录树中对目录项结构做了修改,导致所占数据增多,按照原来的解读,所创建的文件个数也该相应减少(受限于目录项个数),但是却没有,为什么?
问题
当我对目录结构进行修改,改成多叉树时,会增加目录表项的占用空间,如果说目录表内容只有2个扇区的话,通过计算按理说创建五个目录表项就会出现问题,但是缺没有,这是为什么呢?
原因
首先 当format 初始化文件系统的时候
//----------------------------------------------------------------------
// FileSystem::FileSystem
// Initialize the file system. If format = TRUE, the disk has
// nothing on it, and we need to initialize the disk to contain
// an empty directory, and a bitmap of free sectors (with almost but
// not all of the sectors marked as free).
//
// If format = FALSE, we just have to open the files
// representing the bitmap and the directory.
//
// "format" -- should we initialize the disk?
//----------------------------------------------------------------------
FileSystem::FileSystem(bool format)
{
DEBUG('f', "Initializing the file system.\n");
if (format) {
BitMap *freeMap = new BitMap(NumSectors);
Directory *directory = new Directory(NumDirEntries);
FileHeader *mapHdr = new FileHeader;
FileHeader *dirHdr = new FileHeader;
DEBUG('f', "Formatting the file system.\n");
// First, allocate space for FileHeaders for the directory and bitmap
// (make sure no one else grabs these!)
freeMap->Mark(FreeMapSector);
freeMap->Mark(DirectorySector);
// Second, allocate space for the data blocks containing the contents
// of the directory and bitmap files. There better be enough space!
ASSERT(mapHdr->Allocate(freeMap, FreeMapFileSize));
ASSERT(dirHdr->Allocate(freeMap, DirectoryFileSize));
// Flush the bitmap and directory FileHeaders back to disk
// We need to do this before we can "Open" the file, since open
// reads the file header off of disk (and currently the disk has garbage
// on it!).
DEBUG('f', "Writing headers back to disk.\n");
mapHdr->WriteBack(FreeMapSector);
dirHdr->WriteBack(DirectorySector);
// OK to open the bitmap and directory files now
// The file system operations assume these two files are left open
// while Nachos is running.
freeMapFile = new OpenFile(FreeMapSector);
directoryFile = new OpenFile(DirectorySector);
// Once we have the files "open", we can write the initial version
// of each file back to disk. The directory at this point is completely
// empty; but the bitmap has been changed to reflect the fact that
// sectors on the disk have been allocated for the file headers and
// to hold the file data for the directory and bitmap.
DEBUG('f', "Writing bitmap and directory back to disk.\n");
freeMap->WriteBack(freeMapFile); // flush changes to disk
directory->WriteBack(directoryFile);
if (DebugIsEnabled('f')) {
freeMap->Print();
directory->Print();
delete freeMap;
delete directory;
delete mapHdr;
delete dirHdr;
}
} else {
// if we are not formatting the disk, just open the files representing
// the bitmap and directory; these are left open while Nachos is running
freeMapFile = new OpenFile(FreeMapSector);
directoryFile = new OpenFile(DirectorySector);
}
}
可以看出首先
Directory *directory = new Directory(NumDirEntries);
而
#define NumDirEntries 20
所以目录表里面新建了 20个目录表项
之后标记的是 Bitmap 和 Directory的文件头,分别是0 / 1 sector
freeMap->Mark(FreeMapSector);
freeMap->Mark(DirectorySector);
再往后
ASSERT(mapHdr->Allocate(freeMap, FreeMapFileSize));
ASSERT(dirHdr->Allocate(freeMap, DirectoryFileSize));
问题来了 Allocate的 DirectoryFileSize 是多少呢
#define DirectoryFileSize (sizeof(DirectoryEntry) * NumDirEntries)
所以Nachos分配的目录表的大小并不是固定的3、4扇区,而是根据具体的表项个数以及表项大小进行动态分配的
所以 我改成了 20个表项,就目录表而言,必须要能放开20个表项的,但是至于往后磁盘的其他空间,他并不关心.
在我插入五个文件之后
./nachos -D
我的目录项
class DirectoryEntry {
public:
bool inUse; // Is this directory entry in use?
int sector; // Location on disk to find the
// FileHeader for this file
char name[FileNameMaxLen + 1]; // Text name for file, with +1 for
// the trailing '\0'
int parent; // father dir 's index
int Child_Num; // dir's child num
int children[MaxDirChildNUm]; // dir's children
};
#define MaxDirChildNUm 10 // the number of child that a dir can have
Entry Size = 1 + 4 + 10 + 8 + 10*4 bytes = 63bytes
而20个Entry 则20 * 63= 1260 bytes
1260 / 128 = 10 (向上取整),故最后目录表要占10个扇区,3 ~ 13
所以其他文件的数据都是从0x704开始写,最高地址为0x20004 即文件数据块能占 32*32 - 13 个扇区
1260 / 128 = 10 (向上取整),故最后目录表要占10个扇区,3 ~ 13
[外链图片转存中…(img-rfOU0D9g-1651035093466)]
所以其他文件的数据都是从0x704开始写,最高地址为0x20004 即文件数据块能占 32*32 - 13 个扇区
但是受限于设置的目录表个数,因此最多还是20个文件。