How trylobite keeps your files
A gentle tour of trylobite's filesystem: how it remembers which files exist, what is in them, and why pulling the plug at any moment never leaves your files half written.
What a filesystem is for
A computer's disk is a long row of numbered storage spaces and nothing more. It has no idea what a "file" or a "folder" is. The filesystem is the part of the operating system that builds those ideas on top: it keeps track of every file's name, which folder it is in, who owns it, and where on the disk its contents are, and it does so in a way that survives being switched off.
A disk is a row of pages
trylobite's disk is divided into blocks, which you can think of as numbered pages. Each block holds 19,683 trytes (that is 3 to the power 9, since everything on this machine comes in threes), and a disk can have up to 19,683 blocks.
Some blocks hold what is in your files. Others hold the filesystem's own catalogue, which says what files there are. Blocks 0 and 1 are special: they are the bookmarks that say where the catalogue starts.
trylobite puts file contents at the start of the disk and the catalogue at the end, so the two do not get mixed up and files tend to sit in tidy runs of neighbouring blocks.
One big catalogue, kept in order
Picture an old library card catalogue. Every fact about every file is a card:
- the top folder, /, is a folder;
- / holds a file called notes.txt;
- notes.txt belongs to root, is 34 trytes long, and was last changed on Tuesday;
- notes.txt's text is in block 5.
All the cards for one file sit together, and all of them are kept in one sorted order.
The catalogue is far too big for one page, so it is spread over many, and a top page says which page holds which part, like the labels on the drawers of a card cabinet.
To find a file, trylobite reads the top page, follows it to the right page, and reads the cards. However big the disk gets, that is never more than a few pages to read. Programmers call this shape a tree, with the top page as its root, though it grows upside down.
Never cross anything out
Here is the most important idea in the whole filesystem. trylobite never writes over a page that the disk's catalogue points to. When something changes, it writes the changed page somewhere new instead.
Say you edit notes.txt and its card on page C must change. trylobite
copies page C to a free block and makes the change in the copy. But the
top page still points at the old page C, so the top page needs changing
too, and it gets copied in the same way. Pages A and B did not change, so
the new top page simply points at the same A and B as before.
The result is two complete catalogues sitting side by side on the disk: the old one, exactly as it was, and the new one, sharing every page that did not change. This is called copy-on-write. The contents of your files are treated the same way: a changed block of text is written to a fresh block, never over the old one.
Two bookmarks
All that is left is to tell the disk which catalogue is the current one. That is the bookmark's job. The bookmark is a single block that says "the catalogue's top page is here", and changing it is the very last step of any change.
trylobite keeps two bookmarks and writes them in turn: if the last change wrote bookmark 0, the next one writes bookmark 1. Each bookmark is numbered, and when the machine starts it believes whichever is newer.
Now think about what happens if the power fails partway through a change:
- While the new pages are being written, nothing points at them yet. The newest bookmark still points at the old catalogue, which nobody has touched. When the machine starts again, the half-finished change simply never happened.
- While the bookmark itself is being written, the other bookmark still holds the previous catalogue, whole. If the one being written came out garbled, the machine uses the other one.
- Once the bookmark is written, the change is complete.
So whenever the power goes, the disk holds either everything as it was before the change or everything as it was after it, never a mixture. There is no repair to run after a crash, because nothing was ever left broken.
Spotting damage
Disks are not perfect. Now and then a block comes back from the disk different from how it was written. So every page carries a sum: a number worked out from everything on the page. The page above it writes down what the sum should be, and the bookmark does the same for the top page.
When trylobite reads a page, it adds it up and compares. If the numbers differ, the page is damaged, and trylobite says so instead of quietly handing you garbage. If the newest catalogue turns out to be damaged when the machine starts, it falls back to the one the other bookmark points at. The same check covers the contents of your files: every block of text has its sum written down in the catalogue.
A few more good habits
- No list of free space. Most filesystems keep a separate map of which blocks are free, and that map can fall out of step with everything else. trylobite keeps none. When it starts, it walks the catalogue and marks every block it finds in use, so the free ones are simply the rest. The two can never disagree.
- A little kept in reserve. A full disk has a nasty problem: deleting a file is a change, a change needs new pages, and a full disk has none. So trylobite holds a few blocks back (9 on a disk of 243 blocks, never more than 27) that only deleting may use. A full disk can always be emptied again.
- Small files live in the catalogue. A file of up to 2,187 trytes is kept right on its card instead of taking a whole block of its own, which saves a great deal of space for the many tiny files a system has. The little icon in your browser's tab for this site is one of them.
- Owners, permissions and times, as on any Unix system. Every file has an owner and a group, says who may read or change it, and remembers when it was made, changed and last read. To avoid writing to the disk every time a file is merely read, the "last read" time only moves when the file has changed since it was last read.
Where you can see the threes
The big ideas on this page, copying instead of crossing out, two bookmarks and sums, would work just as well on a binary computer. Ternary shows up in the details instead.
The sizes are powers of three. Where a binary filesystem picks sizes like 4,096 or 512, trylobite picks the nearest power of three that fits:
| What | How many | As a power of three |
|---|---|---|
| trytes in a block | 19,683 | 3⁹ |
| blocks a disk can have | 19,683 | 3⁹ |
| trytes a small file can keep in the catalogue | 2,187 | 3⁷ |
| blocks kept in reserve, at most | 27 | 3³ |
A block is exactly one page of the machine's memory, so the filesystem can hand a whole block to a program in one go. Like everything in detrytus's memory, a block is centred: its trytes are numbered from −9,841 to +9,841, with 0 in the middle, rather than from 0 upwards.
One character fits in one tryte. A byte has 256 possible values, room for one alphabet or so, so binary computers today spell most of the world's characters with two to four bytes each. A tryte has 19,683 values, and trylobite gives a name one tryte per character, for any character numbered up to 9,841. That covers most of the world's alphabets (Greek, Cyrillic, Hebrew, Arabic, Devanagari, Thai and many more), though not Chinese characters or emoji. A name of up to 60 characters packs three characters into each word, and because characters are stored as their numbers, sorting the numbers sorts the names. What is inside a file is just trytes, and the program decides what they mean: the pages of this site, for instance, are stored one byte to a tryte, as the web expects.
Options add up like the weights on the scale. When a program opens a file, it can ask for extras, each one a power of three:
| Option | Value |
|---|---|
| make the file if it doesn't exist yet | 3 |
| empty it first | 9 |
| always add to the end | 27 |
To ask for several, add them up: "make it, and empty it first" is 3 + 9 = 12. Because each option has its own place, trylobite can read every choice back out of the total, just as each weight in the balancing puzzle has its own place in a number.
−1 means minus one. On Unix, a call that fails usually answers −1. In balanced ternary −1 is just an ordinary number, so trylobite's calls answer with named negative numbers instead: "no such file", "no space left" and so on.
Some things stay binary on purpose. Permissions are still Unix's familiar read, write and execute switches, written in the usual way (755 for a program anyone may run, 644 for a file anyone may read), so that the tools and habits people know from Unix still work.
Where the ideas come from
The design borrows ideas, though not code, from two Linux filesystems. bcachefs, by Kent Overstreet, keeps everything (folders, file details and where file contents are) in one sorted tree, as trylobite does. btrfs is built on the copy-on-write tree, from a 2008 paper by Ohad Rodeh. Doing without a list of free space is trylobite's own simplification, which works because its disks are small.
Because nothing the bookmark points to is ever written over, keeping an old catalogue around is most of the way to a snapshot: a frozen picture of every file at one moment. The bookmark already has room for more than one catalogue, and snapshots are planned.
Read more
The full description, with every block, record and check, is trylobite's
docs/filesystem.md.
To try the filesystem yourself, boot trylobite and make a
few files: tools/fs.py ls disk.img on your own computer then lists what
you made.