40k · Filesystem¶
Released Thu Nov 5, in class
This is the README OSlings gives every student for 40k_filesystem (oslings
lesson shows it). The starter code is not published here: it reaches you
through OSlings, in class on the class network. Back to
All Exercises.
Learn → Understand → Implement. You'll finish the core of a filesystem — inodes and directories — with an API that returns
Result. The Rust focus is error handling inno_std: no exceptions, just values you must check. This is the last subsystem; after it, every part of rv6 exists.
Learn¶
A filesystem turns raw storage into named files and folders. This exercise builds its heart: inodes and directories. (A real filesystem keeps these on a disk; we don't have a disk driver yet — that's exercise 41k — so ours lives in RAM. The structures and logic are the real ones; only the storage differs.)
Inodes¶
An inode ("index node") is the kernel's record of one file or directory. It holds the metadata and contents, but not the name — that lives in the directory that points to it. Each inode in our table has:
- a kind:
Free(unused slot),File, orDir; - a size;
- contents: file bytes (for a
File) or directory entries (for aDir).
Inodes are referred to by number — the inode number (inum), an index into
the inode table. Inode number ROOT is the root directory, created at init;
that's the top of the tree (the "/" you know from a shell).
Directories are just inodes¶
Here's the elegant part: a directory is simply an inode whose contents are a
list of entries, each mapping a name → inode number. Looking up "hello"
in a directory means scanning its entries for that name and returning the inode
number it points to. Creating "hello" means allocating a new inode and adding
an entry that points to it. Because directories can contain other directories,
this builds the whole tree: /sub/inner is "look up sub in the root to get a
directory inode, then look up inner in that."
Error handling without std: Result¶
Filesystem calls fail in ordinary, expected ways: the name isn't there, it
already exists, you tried to write into a directory. These aren't bugs — the
caller must handle them. In std land you might get an exception or a
std::io::Error; in a no_std kernel there's none of that. Rust's answer is the
Result<T, E> type: a value that is either Ok(T) (success, carrying the
result) or Err(E) (failure, carrying an error). The caller cannot ignore it —
they have to look inside to get the value, which is exactly what makes kernel
error handling robust.
We define our own error type, an enum:
and every operation returns Result<…, FsError>. Three idioms you'll use:
- Return an error:
return Err(FsError::AlreadyExists); - Match on a result to react to specific errors:
- The
?operator — "do this; if it's anErr, return that error from me immediately; otherwise give me theOkvalue":?is the clean way to chain fallible steps.Optionhas a sibling helper,.ok_or(err), which turnsSome(x)/NoneintoOk(x)/Err(err)so you can?it too.
Shared safely¶
The whole filesystem lives behind a SpinLock (your exercise 37k lock):
static FS: SpinLock<FileSystem>. Locking it hands you &mut FileSystem, so the
operations are ordinary safe Rust — the interesting part is the Result logic,
not pointers.
Understand¶
Read rv6/src/fs.rs: the InodeKind and FsError enums, the Inode/DirEnt
structs, the given new/init/alloc/read, and the FS lock.
dircreate is given — read it. It is the directory operation in full: a
length check, then a match on your dirlookup to reject duplicates (and,
for free, to reject a dir that isn't a directory), a scan for a free entry
slot, self.alloc(kind)? to get the inode (the ? hands NoFreeInode straight
back to the caller), and finally the entry itself. Notice what it assumes
about dirlookup: Err(NotFound) means "free to create". That is exactly what
the placeholder dirlookup returns, so creating files works before you write a
line — it is finding them again that doesn't.
Then read rv6/src/main.rs: it locks FS, creates /hello, looks it up, checks
a missing name gives NotFound, checks a duplicate gives AlreadyExists, writes
and reads the file, nests /sub/inner, and checks that writing to a directory
gives IsADirectory.
Implement¶
In rv6/src/fs.rs:
dirlookup— ensurediris a directory (NotADirectoryif not), scan its entries for a matching name, return itsinum, orNotFound.write—NotFoundif the inode is free,IsADirectoryif it's a directory,FileTooBigif the data won't fit; otherwise copy the data, set the size, and return the byte count.
Check your work:
Passes when create/lookup/read/write and all the error cases behave — printing
OSLINGS:PASS. Each failure message names the property that broke.
🎉 That's every part of the kernel¶
If this passes, you've built each piece of rv6, one self-test at a time: it boots on bare metal, manages physical memory, walks page tables and runs with the MMU on, creates and schedules processes with real context switches, synchronizes with spinlocks and semaphores over a heap, and keeps a filesystem of inodes and directories. Every line had a reason. Nicely done.
From here the job changes. Starting with 42k, you assemble these parts into an operating system that actually boots and keeps running — then teach it to take traps and interrupts, talk to a console, run a shell, and finally drop into user mode. (Want to drive real hardware first? 41k_devices, extra credit, turns the blind-write UART into a proper register-level driver.)
Stuck? oslings hint.