Lab 3: File Systems
Due: Fri April 18, 2014, 8pm
The goal of this lab is to write a simple UNIX-like file system based
on the topics covered in class. The file system you will write makes
the following simplifying assumptions:
- The file system resides on a disk that is 128KB in size.
- There is only one root directory. No subdirectories are allowed.
- The file system supports a maximum of 16 files.
- The maximum size of a file is 8 blocks; each block is 1KB in size.
- Each file has a unique name, the file name can be no longer than
The layout of your 128 KB disk is as follows
- The first 1KB block is called the super block. It stores
the free block list and index nodes (inode) for each file.
- The remaining 127 1KB blocks store data blocks of the files
on your file system.
- The exact structure of the super block is as follows.
- The first 128 bytes stores the free block list. Each entry in
this list indicates whether the corresponding block is free or
in use (if the i-th byte is 0, it indicates that the block
is free, else it is in use). Intially all blocks except the super
block are free.
- Immediately following the free block list are the 16 index nodes, one for
each file that is allowed in the file system. Initially, all
inode are free. Each inode stores the following information:
char name; //file name
int size; // file size (in number of blocks)
int blockPointers; // direct block pointers
int used; // 0 => inode is free; 1 => in use
Note that each inode is 56 bytes in size; Since you have 16 of these,
the total size of occupied by the inodes is 896 bytes. The free/used
block information (mentioned above) is 128 byes. So the total space
used in the super block is 1024 bytes.
You need to implement the following operations for your file system.
- create(char name, int size): create a new file
with this name and with these many blocks. (We shall assume that
the file size is specified at file creation time and the file
does not grow or shrink from this point on)
- delete(char name): delete the file with this name
- read(char name, int blockNum, char buf): read
the specified block from this file into the specified buffer; blockNum
can range from 0 to 7.
- write(char name, int blockNum, char buf): write
the data in the buffer to the specified block in this file.
- ls(void): list the names of all files in the file system
and their sizes.
- We will use a 128KB file to act as the "disk" for your
file system. We have provided a program to create this file
java version or
Download and compile either version of this program (using javac or gcc) and then run it by passing the name of your disk, like so:
java CreateFS disk0
or for C:
gcc -o create_fs create_fs.c
This will create a file with the name
in your current directory. The program also "formats" your
file system---this is done by initializing all blocks in the
super block to be free and marking all 16 inodes to be free.
- Here is template code for your file system.
- Remember that your file system must be persistent. If you
shutdown your program and then restart it at a later time, all files
on your file system must be intact.
- Input file
You program should take input from a input file and perform actions
specified in the file, while printing out the result of each action.
The format of the input file is as follows.
diskName // name of the file that emulates the disk
C fileName Size //create a file of this size
D fileNAme // delete this file
L // list all files on disk and their sizes
R fileName blockNum // read this block from this file
W fileName blockNum // write to this block in the file (use a dummy
An sample input file looks like this:
C file1 3
W file1 0
W file1 1
C lab.java 7
C file2 4
R file1 1
A sample input file is available. Be sure
to print out what your program does after reading each line from this file.
It'd also be helpful if you printed out the disk addresses of any block that
you allocate, deallocate, read or write.
- How do I write to a specific location on the disk?
You will need to be completely familiar with the RandomAccessFile
class in order to read and write to specific locations in the disk
file. I strongly recommend that you read up on this class before
starting on this assignment. In particular, you'll need to understand
how to use the seek method to move the file pointer to a specific
location in the file and then you can read/write at that location.
Once you understand how seek works, all you need to do is compute
the byte offset of the object you are tying to access and seek to
that location before reading or writing. Remember to count from 0!
- Your disk file is 128 KB in size. But (16 files * 8KB per
file) plus 1 super block equals 129KB. How can all files fit?
Your file system can support a maximum file size of 8 blocks
(8KB). That does NOT mean that every file will be 8KB in size. In most
cases, files will be smaller than the max file size. Each
"create" request should check whether the disk has sufficient space for the
file and if not, return a "not enough space on disk" error message.
- Can I use Java data types such as Strings to represent file
names instead of character arrays?
Feel free to do so in your file system program. HOWEVER, you should be
very careful in what get written out to the disk. Since each inode has
space allocated for exactly 8 characters (16 bytes) to hold the file name, you should
always write out 8 characters to disk, regardless of the file size. If
your file name is shorter, the unused characters should have the '\0'
or spaces. Thus, if you use strings, you'll need to make sure exactly
8 characters are written out, regardless of the string length. We
recommend that you use character arrays instead of strings if possible.
- Do I need to touch anything other than the superblock?
Most file operations will only touch the superblock (the free block
list and the inode). However, the read and write calls need you to
actually read and write data from the specified block on disk. In case
of write, you can use a dummy buffer (e.g., with all 1s) to write to disk.
Remember, what you write out is what should get read in if you use "read"
at a later time.
(10 pts) Write a defragmenter for the above file system. The
defragmenter is invoked (by the user) whenever free space is
fragmented and when files blocks are randomly scattered on the
disk. Your defragmenter should attempt to place each file contiguously
on disk and move all free space to the end of the disk.
Any valid defragmentation algorithm will get you
full credit on this question. The only restriction is that your
defragmenter should not use a memory buffer of more than 17 KB (a
trivial defragmentation algorithm is to read the entire disk into
memory, rearrange blocks and write everything back. Things get more
interesting if you assume that you have limited memory space
(17KB)---in this case, you will need to repeatedly read a small number
of files or disk blocks, rearrange them and then write them back.).
Sample input files that create a fragmented disk and
the defragmented version of this disk are
available. You need to run these scripts on an empty disk to obtain the framented
and defragmented versions of the disk.
How to Turn in Lab 3
- Your submission archive should contain a copy of all source files.
Your submission should contain a README file identifying your lab partner (if you
have one) and containing an outline of what you did for the assignment.
It should also explain and motivate your design choices. Explain the design
of your filesystem how it works. Clearly explain your approach but keep it short and to the point. If your implementation does not work, you should also document the
problems in the README, preferably with your explanation of why it does
not work and how you would solve it if you had more time. Of course,
you should also comment your code. We can't give you credit for something
we don't understand!
Fnally, your submission should contain a copy showing sample output from your programs.
- (if working in a group) A file called GROUP that lists both members of your group and what contributions they made to the project. Of course, you should strive for an equal distribution of labor and effort. We reserve the right to assign different grades to group members if one member was clearly inadequately participating in completing the project.
- Note: We will strictly enforce policies on cheating.
Remember that we routinely run similarity checking programs on your
solutions to detect cheating. Please make sure you turn in your
You should be very careful about using code snippets you find on
the Internet. In general your code should be your own. It is OK to
read tutorials on the web and use these concepts in your
assignment. Blind use of code from web is strictly disallowed. Feel free to
check with us if you have questions on this policy. And be sure to document any
Internet sources/ tutorials you have used to complete the assignment
in your README file.
- Late Policy: Please refer to the course syllabus for late
policy on labs assignments. This late policy will be strictly
Please start early so that you can submit the assignment on time.