CSc 360: Operating Systems (Spring 2018)
Programming Assignment 3 (P3): A Simple File System (SFS)
Spec out: March 5, 2018
Due Date: April 2, 2018
1 Introduction1
So far, you have built a shell environment and a multi-thread scheduler with process synchronization. Excellent job!2
What is still missing for a “real” operating system? A file system! In this assignment, you will implement utilities3
that perform. operations on a file system similar to Microsoft’s FAT file system with some improvement.4
1.1 Sample File Systems5
You will be given a test file system image for self-testing, but you can create your own image following the specification,6
and your submission may be tested against other disk images following the same specification.7
You should get comfortable examining the raw, binary data in the file system images using the program xxd.8
IMPORTANT: since you are dealing with binary data, functions intended for string manipulation9
such as strcpy() do NOT work (since binary data may contain binary ‘0’ anywhere), and you should10
use functions intended for binary data such as memcpy().11
2 Tutorial Schedule12
In order to help you finish this programming assignment on time successfully (and also the last one ;-), the schedule13
of the lectures and the tutorials has been adjusted. There are three tutorials arranged during the course of this14
assignment. NOTE: Please do attend the tutorials and follow the tutorial schedule closely.15
Date Tutorial Milestones
Mar 6/7 no tutorial due to p2b extension and m2 preparation p3 self-practice questions
Mar 13/14 p3 spec and practice questions go-through design done
Mar 20/21 more on design and implementation coding done
Mar 27/28 testing and last-minute help final deliverable
3 Requirements16
3.1 Part I (3 points)17
In part I, you will write a program that displays information about the file system. In order to complete part I, you18
will need to read the file system super block and use the information in the super block to read the FAT.19
Your program for part I will be invoked as follows (output value just for illustration purposes):20
./diskinfo test.img
Sample output:21
Super block information:
Block size: 512
Block count: 5120
FAT starts: 1
1
FAT blocks: 40
Root directory start: 41
Root directory blocks: 8
FAT information:
Free Blocks: 5071
Reserved Blocks: 41
Allocated Blocks: 8
Please be sure to use the exact same output format as shown above.22
3.2 Part II (4 points)23
In part II, you will write a program, with the routines already implemented for part I, that displays the contents of24
the root directory or a given sub-directory in the file system.25
Your program for part II will be invoked as follows:26
./disklist test.img /sub_dir
The directory listing should be formatted as follows:27
1. The first column will contain:28
(a) F for regular files, or29
(b) D for directories;30
followed by a single space31
2. then 10 characters to show the file size, followed by a single space32
3. then 30 characters for the file name, followed by a single space33
4. then the file modification date (we will not display the file creation date).34
For example:
F 2560 foo.txt 2005/11/15 12:00:00
F 5120 foo2.txt 2005/11/15 12:00:00
F 48127 makefs 2005/11/15 12:00:00
F 8 foo3.txt 2005/11/15 12:00:00
3.3 Part III (4 points)35
In part III, you will write a program that copies a file from the file system to the current directory in Linux. If the36
specified file is not found in the root directory or a given sub-directory of the file system, you should output the37
message File not found. and exit.38
Your program for part III will be invoked as follows:39
./diskget test.img /sub_dir/foo2.txt foo.txt
3.4 Part IV (4 points)40
In part IV, you will write a program that copies a file from the current Linux directory into the file system, at the41
root directory or a given sub-directory. If the specified file is not found, you should output the message File not42
found. on a single line and exit.43
Your program for part IV will be invoked as follows:44
./diskput test.img foo.txt /sub_dir/foo3.txt
2
4 File System Specification45
The FAT file system has three major components:46
1. the super block,47
2. the directory structure.48
3. the File Allocation Table (informally referred to as the FAT),49
Each of these three components is described in the subsections below.50
4.1 File System Superblock51
The first block (512 bytes) is reserved to contain information about the file system. The layout of the superblock is52
as follows:53
Description Size Default Value
File system identifier 8 bytes CSC360FS
Block Size 2 bytes 0x200
File system size (in blocks) 4 bytes 0x00001400
Block where FAT starts 4 bytes 0x00000001
Number of blocks in FAT 4 bytes 0x00000028
Block where root directory starts 4 bytes 0x00000029
Number of blocks in root dir 4 bytes 0x00000008
Figure 1: Superblock Fields
Note: Block number starts from 0 in the file system.54
4.2 Directory Entries55
Each directory entry takes up 64 bytes, which implies there are 8 directory entries per 512 byte block.56
Each directory entry has the following structure:57
Description Size
Status 1 byte
Starting Block 4 bytes
Number of Blocks 4 bytes
File Size (in bytes) 4 bytes
Create Time 7 bytes
Modify Time 7 bytes
File Name 31 bytes
unused (set to 0xFF) 6 bytes
Figure 2: Directory Entry
The description of each field follows:58
Status This is bit mask that is used to describe the status of the file. Currently only 3 of the bits are used.59
It is implied that only one of bit 2 or bit 1 can be set to 1. That is, an entry is either a normal file or it is a60
directory, not both.61
Starting Block This is the location on disk of the first block in the file62
Number of Blocks The total number of blocks in this file63
File Size The size of the file, in bytes. The size of this field implies that the largest file we can support is 232 bytes64
long.65
3
Bit 0 set to 0 if this directory entry is available,
set to 1 if it is in use
Bit 1 set to 1 if this entry is a normal file
Bit 2 set to 1 if this entry is a directory
Figure 3: Format of Status Field
Create Time The date and time when this file was created. The file system stores the system times as integer66
values in the format:67
YYYYMMDDHHMMSS68
Field Size
YYYY 2 bytes
MM 1 byte
DD 1 byte
HH 1 byte
MM 1 byte
SS 1 byte
Figure 4: Format of Date-Time Field
Modify Time The last time this file was modified. Stored in the same format as the Create Time shown above.69
File Name The file name, null terminated. Because of the null terminator, the maximum length of any filename is70
30 bytes.71
Valid characters are upper and lower case letters (a-z, A-Z), digits (0-9) and the underscore character ( ).72
4.3 File Allocation Table (FAT)73
Each directory entry contains the starting block number for a file, let’s say it is block number X. To find the next74
block in the file, you should look at entry X in the FAT. If the value you find there does not indicate End-of-File75
(see below) then that value, call it Y, is the next block number in the file.76
That is, the first block is at block number X, you look in the FAT table at entry X and find the value Y. The77
second data block is at block number Y. Then you look in the FAT at entry Y to find the next data block number...78
continue this until you find the special value in the FAT entry indicating that you are at the last FAT entry of the79
file.80
The FAT is really just a linked list, which the head of the list being stored in the “Starting Block” field in the81
directory entry, and the ‘next pointers’ being stored in the FAT entries.82
FAT entries are 4 bytes long (32 bits), which implies there are 128 FAT entries per block.83
Special values for FAT entries are described in Figure 5.
Value Meaning
0x00000000 This block is available
0x00000001 This block is reserved
0x00000002–
0xFFFFFF00 Allocated blocks as part of files
0xFFFFFFFF This is the last block in a file
Figure 5: Value of FAT entry
84
4
5 Byte Ordering85
Different hardware architectures store multi-byte data (like integers) in different orders. Consider the large integer:86
0xDEADBEEF87
On the Intel architecture (Little Endian), it would be stored in memory as:88
EF BE AD DE89
On the PowerPC (Big Endian), it would be stored in memory as:90
DE AD BE EF91
Our file system will use Big Endian for storage. This will make debugging the file system by examining the raw92
data much easier.93
This will mean that you have to convert all your integer values to Big Endian before writing them to disk. There94
are utility functions in netinit/in.h that do exactly that. (When sending data over the network, it is expected the95
data is in Big Endian format.)96
See the functions htons, htonl, ntohs and ntohl.97
The side effect of using these functions will be that your code will work on multiple platforms. (On machines98
that natively store integers in Big Endian format, like the Mac (not the Intel-based ones), the above functions don’t99
actually do anything but you should still use them!)100
6 Submission Requirements101
What to hand in: You need to hand in a .tar.gz file containing all your source code and a Makefile that produces102
the executables for parts 1 - 4.103
Please include a readme.txt file that explains your design and implementation.104
The file is submitted through connex.csc.uvic.ca site.105
5
A An Exercise106
Q1 Consider the superblock shown below:107
0000000: 4353 4333 3630 4653 0200 0000 1400 0000 CSC360FS........108
0000010: 0001 0000 0028 0000 0029 0000 0008 0000 .....(...)......109
0000020: 0000 0000 0000 0000 0000 0000 0000 0000 ................110
(a) What block does the FAT start on? How many blocks are used for the FAT?111
(b) What block does the root directory start on? How many blocks are used for the root directory?112
6
Q2 Consider the following block from the root directory:113
0005200: 0300 0000 3100 0000 0500 000a 0007 d50b ....1...........114
0005210: 0f0c 0000 07d5 0b0f 0c00 0066 6f6f 2e74 ...........foo.t115
0005220: 7874 0000 0000 0000 0000 0000 0000 0000 xt..............116
0005230: 0000 0000 0000 0000 0000 00ff ffff ffff ................117
0005240: 0300 0000 3600 0000 0a00 0014 0007 d50b ....6...........118
0005250: 0f0c 0000 07d5 0b0f 0c00 0066 6f6f 322e ...........foo2.119
0005260: 7478 7400 0000 0000 0000 0000 0000 0000 txt.............120
0005270: 0000 0000 0000 0000 0000 00ff ffff ffff ................121
0005280: 0300 0000 4000 0000 5e00 00bb ff07 d50b ....@...^.......122
0005290: 0f0c 0000 07d5 0b0f 0c00 006d 616b 6566 ...........makef123
00052a0: 7300 0000 0000 0000 0000 0000 0000 0000 s...............124
00052b0: 0000 0000 0000 0000 0000 00ff ffff ffff ................125
00052c0: 0300 0000 9e00 0000 0100 0000 0807 d50b ................126
00052d0: 0f0c 0000 07d5 0b0f 0c00 0066 6f6f 332e ...........foo3.127
00052e0: 7478 7400 0000 0000 0000 0000 0000 0000 txt.............128
00052f0: 0000 0000 0000 0000 0000 00ff ffff ffff ................129
(a) How many files are allocated in this directory? What are their names?130
(b) How many blocks does the file makefs occupy on the disk?131
7
Q3 Given the root directory information from the previous question and the FAT table shown below:132
0000200: 0000 0001 0000 0001 0000 0001 0000 0001 ................133
0000210: 0000 0001 0000 0001 0000 0001 0000 0001 ................134
0000220: 0000 0001 0000 0001 0000 0001 0000 0001 ................135
0000230: 0000 0001 0000 0001 0000 0001 0000 0001 ................136
0000240: 0000 0001 0000 0001 0000 0001 0000 0001 ................137
0000250: 0000 0001 0000 0001 0000 0001 0000 0001 ................138
0000260: 0000 0001 0000 0001 0000 0001 0000 0001 ................139
0000270: 0000 0001 0000 0001 0000 0001 0000 0001 ................140
0000280: 0000 0001 0000 0001 0000 0001 0000 0001 ................141
0000290: 0000 0001 0000 0001 0000 0001 0000 0001 ................142
00002a0: 0000 0001 0000 002a 0000 002b 0000 002c .......*...+...,143
00002b0: 0000 002d 0000 002e 0000 002f 0000 0030 ...-......./...0144
00002c0: ffff ffff 0000 0032 0000 0033 0000 0034 .......2...3...4145
00002d0: 0000 0035 ffff ffff 0000 0037 0000 0038 ...5.......7...8146
00002e0: 0000 0039 0000 003a 0000 003b 0000 003c ...9...:...;......?....148
(a) What blocks does the file foo.txt occupy on the disk?149
(b) What blocks does the file foo2.txt occupy on the disk?150