Objectives
The purpose of this laboratory session is to study the functionality of the file system in Nachos. The file system in Nachos is designed to be small and simple so that you can read all its source code in a short period of time. Before starting to read the code, it is very useful to get an idea of what functionality the Nachos file system offers. In this laboratory session, you will run the commands of the Nachos file system and watch the effects on the simulated hard disk in Nachos. On the completion of this laboratory session, you should know
• what is the functionality of the Nachos file system, and
• how to examine the contents of the simulated hard disk in Nachos.
Preliminary preparation
-
Learn about bitmap
参考:https://blog.csdn.net/pipisorry/article/details/62443757?utm_source=itdadao&utm_medium=referral
所谓的bitmap就是用一个bit位来标记某个元素对应的value,而key即是该元素。由于采用了bit为单位来存储数据,因此在存储空间方面,可以大大节省空间。文件系统中用bitmap来映射块空间,bitmap中1表示块被占用,0表示空闲块。
像上述的bitmap可以被称作空间磁盘块分配表,其以文件的形式存储,头文件位于文件目录的第0项。每一个bit对应磁盘中的一个块的使用情况,0表示空闲,1表示已分配。
Note: Block number的计算
此外,在nachos中,文件目录是以文件的形式存储,头文件位于文件目录的第1项。是由nachos中directory类生成的。directory的项是<name,sector>的pair,用于文件名->磁盘块的索引,找到的是文件头部所在的磁盘块。
-
Learn about Ext2
参考:https://www.cnblogs.com/bellkosmos/p/detail_of_linux_file_system.html
-
Reading the source code
-
Filesys.h(cc)
概述:定义了一个filesystem类,包括了对nachos filesystem的初始化函数,以及若干个对nachos filesystem中某个文件的操作函数。
- FileSystem(bool format)
filesystem构造函数。用于创建或者载入一个文件系统。
当format为true时会初始化filesystem(相当于格式化了文件系统),使用新的空闲磁盘块分配表以及新的文件目录。否则仅仅载入原来存在的空闲磁盘块表以及文件目录。
- bool Create(char *name, int initialSize)
用于在文件系统中创建文件,name为文件名,initialSize为文件初始长度
1)从磁盘载入空闲磁盘块分配表文件和文件目录文件(通过bitmap和directory->FetchFrom(sector)实现; bitmap->find(name)用来检测文件是否已存在,如果存在则返回-1)
2)为新文件创建文件头部(filehdr):找到一个空闲磁盘块(通过bitmap->Find()实现,如果没有空闲的块空间,则返回-1,否则返回空闲磁盘块的sector号);将头文件信息写入目录文件(directory->add(name,sector));
3)将文件块的信息写入文件头部:文件被切分成若干个文件块,并在文件头部记录每个文件块位于磁盘的哪个sector(通过file