nachos.kernel.filesys
Class FileSystemReal

java.lang.Object
  extended by nachos.kernel.filesys.FileSystem
      extended by nachos.kernel.filesys.FileSystemReal

 class FileSystemReal
extends FileSystem

This class manages the overall operation of the file system. It implements methods to map from textual file names to files. Each file in the file system has: A file header, stored in a sector on disk (the size of the file header data structure is arranged to be precisely the size of 1 disk sector); A number of data blocks; An entry in the file system directory. The file system consists of several data structures: A bitmap of free disk sectors (cf. bitmap.h); A directory of file names and file headers. Both the bitmap and the directory are represented as normal files. Their file headers are located in specific sectors (sector 0 and sector 1), so that the file system can find them on bootup. The file system assumes that the bitmap and directory files are kept "open" continuously while Nachos is running. For those operations (such as create, remove) that modify the directory and/or bitmap, if the operation succeeds, the changes are written immediately back to disk (the two files are kept open during all this time). If the operation fails, and we have modified part of the directory and/or bitmap, we simply discard the changed version, without writing it back to disk. Our implementation at this point has the following restrictions: there is no synchronization for concurrent accesses; files have a fixed size, set when the file is created; files cannot be bigger than about 3KB in size; there is no hierarchical directory structure, and only a limited number of files can be added to the system; there is no attempt to make the system robust to failures (if Nachos exits in the middle of an operation that modifies the file system, it may corrupt the disk). A file system is a set of files stored on disk, organized into directories. Operations on the file system have to do with "naming" -- creating, opening, and deleting files, given a textual file name. Operations on an individual "open" file (read, write, close) are to be found in the OpenFile class (OpenFile.java). We define two separate implementations of the file system. This version is a "real" file system, built on top of a disk simulator. The disk is simulated using the native file system on the host platform (in a file named "DISK"). In the "real" implementation, there are two key data structures used in the file system. There is a single "root" directory, listing all of the files in the file system; unlike UNIX, the baseline system does not provide a hierarchical directory structure. In addition, there is a bitmap for allocating disk sectors. Both the root directory and the bitmap are themselves stored as files in the Nachos file system -- this causes an interesting bootstrap problem when the simulated disk is initialized.


Field Summary
private  OpenFile directoryFile
          "Root" directory -- list of file names, represented as a file.
private static int DirectoryFileSize
          The initial size of a directory file.
private static int DirectorySector
          The disk sector containing the directory of files.
private  OpenFile freeMapFile
          Bit map of free disk blocks, represented as a file.
private static int FreeMapFileSize
          The initial file size for the bitmap file.
private static int FreeMapSector
          The disk sector containing the bitmap of free sectors.
private static int NumDirEntries
          The maximum number of entries in a directory.
 
Constructor Summary
protected FileSystemReal(boolean format)
          Initialize the file system.
 
Method Summary
 boolean create(java.lang.String name, long initialSize)
          Create a file in the Nachos file system (similar to UNIX create).
 void list()
          List all the files in the file system directory (for debugging).
 OpenFile open(java.lang.String name)
          Open a file for reading and writing.
 void print()
          Print everything about the file system (for debugging): the contents of the bitmap; the contents of the directory; for each file in the directory: the contents of the file header; the data in the file.
 boolean remove(java.lang.String name)
          Delete a file from the file system.
 
Methods inherited from class nachos.kernel.filesys.FileSystem
init
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

FreeMapSector

private static final int FreeMapSector
The disk sector containing the bitmap of free sectors.

See Also:
Constant Field Values

DirectorySector

private static final int DirectorySector
The disk sector containing the directory of files.

See Also:
Constant Field Values

FreeMapFileSize

private static final int FreeMapFileSize
The initial file size for the bitmap file.

See Also:
Constant Field Values

NumDirEntries

private static final int NumDirEntries
The maximum number of entries in a directory.

See Also:
Constant Field Values

DirectoryFileSize

private static final int DirectoryFileSize
The initial size of a directory file.


freeMapFile

private OpenFile freeMapFile
Bit map of free disk blocks, represented as a file.


directoryFile

private OpenFile directoryFile
"Root" directory -- list of file names, represented as a file.

Constructor Detail

FileSystemReal

protected FileSystemReal(boolean format)
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.

Parameters:
format - Should we initialize the disk?
Method Detail

create

public boolean create(java.lang.String name,
                      long initialSize)
Create a file in the Nachos file system (similar to UNIX create). Since we can't increase the size of files dynamically, we have to supply the initial size of the file. The steps to create a file are: Make sure the file doesn't already exist; Allocate a sector for the file header; Allocate space on disk for the data blocks for the file; Add the name to the directory; Store the new file header on disk; Flush the changes to the bitmap and the directory back to disk. Return true if everything goes ok, otherwise, return false. Create fails if: file is already in directory; no free space for file header; no free entry for file in directory; no free space for data blocks for the file. Note that this implementation assumes there is no concurrent access to the file system!

Specified by:
create in class FileSystem
Parameters:
name - The name of file to be created.
initialSize - The size of file to be created.
Returns:
true if the file was successfully created, otherwise false.

open

public OpenFile open(java.lang.String name)
Open a file for reading and writing. To open a file: Find the location of the file's header, using the directory; Bring the header into memory.

Specified by:
open in class FileSystem
Parameters:
name - The text name of the file to be opened.
Returns:
An OpenFile object that provides access to the file contents, if the file was successfully opened, otherwise null.

remove

public boolean remove(java.lang.String name)
Delete a file from the file system. This requires: Remove it from the directory; Delete the space for its header; Delete the space for its data blocks; Write changes to directory, bitmap back to disk. Return true if the file was deleted, false if the file wasn't in the file system.

Specified by:
remove in class FileSystem
Parameters:
name - The text name of the file to be removed.
Returns:
true if the operation was successful, otherwise false.

list

public void list()
List all the files in the file system directory (for debugging).

Overrides:
list in class FileSystem

print

public void print()
Print everything about the file system (for debugging): the contents of the bitmap; the contents of the directory; for each file in the directory: the contents of the file header; the data in the file.

Overrides:
print in class FileSystem