The ten commands our file system is able to handle are:
-
M [disk name]
This command calls the fs_mount function which takes a disk name as the input and performs six consistency checks before mounting the disk. In performing the consistency checks, I first loaded the superblock of the disk and checked its free block list against its inodes. To check uniqueness of filenames, I maintained a map of directory names and a vector of the indeces of its children. This also helps a lot in other functions. It is only after passing the consistency checks do I load the superblock and set the current working directory to root, which is represented as a variablecwd
storing the integer of the inode of the current working directory (127 for root). -
C [file name] [size]
This command calls the fs_create function which takes a file name and its size (in blocks) as the input. If the specified size is 0, that means a directory is to be created. The main challenge to this implementation is finding contiguous blocks which can accomodate a file of that size. I found this was easier done by checking the free block list. The first available inode is used, which is done by iterating through the superblock's inode list. From there, the inode attributes are updated based on the start block, parent directory (which is the current working directory), file size, file type, and of course name and state. The free block list and map of parent directory names are also updated. -
D [file name]
This command calls the fs_delete function deletes the given file or directory if it exists in the current working directory. To achieve this, the map of directory names and its children are checked. In fact, for most commands involving files located in the current working directory, this map is often referred to. If the file/directory exists, its inode and data blocks are cleared. The free block list and directory map are updated. -
R [file name] [block number]
This command calls the fs_read reads from the nth block of a file and writes the data into a 1KB buffer used by the entire file system for holding data for read/write operations. Once again, the file name is checked against the files in the current working directory using the directory map. -
W [file name] [block number]
This command calls the fs_write function which, similar to the R command, uses the system-wide data buffer to write into the nth block of the file (both given as arguments). The file name must also exist in the current working directory. -
B [new buffer characters]
This command callsfs_buff
which does not interact directly with disk data. It populates the data buffer with the characters provided as arguments. -
L
This command calls thefs_ls
function which is similar tols
command in that it prints out the files and directories located in the current working directory. Files would display their file size and directories would display the number of files/directories they contain. The map of parent directories is once again very useful for this. The directories '.' and '..' are also included. -
E [file name] [new size]
This command calls thefs_resize
function which changes the size of the file to the specified new size. If the new size is less than the current size, the last blocks are cleared to reduce the size. If the size is increased, it searches for contiguous free blocks of that size to hold the file. If no contiguous blocks are found, the file cannot be resized. Finding the free blocks are achieved by checking the free block list, similar to how files were created. The old data blocks are then moved and the inode's attributes (used size and start block) are updated. -
O
This command calls thefs_defrag
function, which reorganizes the disk's data blocks to clear out free blocks between used blocks. I first sorted the file inodes by start block, moving ones with the lower start blocks first. I would then go through the free block list, update the inode's start block to the first available data block found. Similar to the resize function, I would move the data blocks, update the inode attributes (only the start block this time) and update the free block list. -
Y [directory name]
This command calls thefs_cd
function, which is similar to thecd
command in that it changes the current working directory to the directory named passed as an argument to this command. First, the directory name is checked against the directories that exist within the current directory. The arguments '.' and '..' are also considered. The variablecwd
which holds the inode index of the current directory is updated.
- Class notes and slides on file systems and their implementation
- C++ Reference Page
- Various StackOverflow pages for debugging
- eClass Discussion Board