detrytus

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.

012 345 678 91011 bookmarks file contents free catalogue blocks 0 and 1 from the start from the end
Where things go on a small disk.

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:

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.

top page / and its names→ page A bin and its names→ page B notes.txt→ page C page A / is a folder/ holds: bin/ holds: notes.txt page B bin is a folderbin holds: lsbin holds: vi page C notes.txt is a fileowner, times, sizewhere its text is
The catalogue: a top page, and pages of cards in order.

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.

old top new top C A B C old page, left untouched unchanged pages, shared by both a fresh copy, with the change
Changing page C writes two new pages and leaves the old ones alone.

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.

1. Before 2. Writing 3. After 0 v4 1 v3 0 v4 1 v5 0 v4 1 v5 bookmark 0 is the newest: it finds v4 a power cut now still leaves 0 and v4 whole 1 is the newest now; 0 and v4 are the spare, just in case
One change, from the bookmarks' point of view.

Now think about what happens if the power fails partway through a change:

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.

page 12 should add up to 4721 page 15 should add up to 3388 page 12 adds up to 4721 ✓ page 15 adds up to 3390 ✗ matches: read it differs: damaged, not trusted
Each page checks the pages below it. (The numbers are made up.)

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

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.