File systems on SSDs
About this lesson
A file system treats the disk as numbered blocks it can overwrite in place, but the flash inside an SSD cannot be overwritten: pages are written once, and only whole blocks can be erased. Follow four lines of C into a tiny SSD of 3 blocks and 4 pages as its flash translation layer remaps every write, leaves stale copies behind, learns about a deleted file through TRIM, and cleans up with garbage collection, costing 6 flash writes for 4 file system writes. Crash consistency and journaling covers how the file system stays safe through crashes; the Linux lessons The file system and Disks, partitions and mounts show the user's view of the same disk.
Two views of one disk
The file system sees the disk as a row of numbered blocks, logical block addresses (LBAs), that it can overwrite in place. An SSD keeps data in flash chips that cannot be overwritten. A controller inside the SSD hides the difference.
file system view LBA 0, LBA 1, LBA 2, ... (here: 4 KB blocks)
"write LBA 1" replaces it in place
inside the SSD flash chips + a controller
flash cannot overwrite in place
the controller makes the flash look like the simple viewRead the lesson step by step
Two views of one disk
The file system sees the disk as a row of numbered blocks, logical block addresses (LBAs), that it can overwrite in place. An SSD keeps data in flash chips that cannot be overwritten. A controller inside the SSD hides the difference.
file system view LBA 0, LBA 1, LBA 2, ... (here: 4 KB blocks) "write LBA 1" replaces it in place inside the SSD flash chips + a controller flash cannot overwrite in place the controller makes the flash look like the simple viewFlash: erase before write
Flash is written a page at a time, often 4 KB, but erased only a whole block at a time, typically 128 or 256 KB. A written page cannot change until its block is erased. Our tiny SSD has 3 blocks of 4 pages.
operation unit time (SLC flash, OSTEP Fig. 44.2) read page 25 us program (write) page 200-300 us erase block 1500-2000 us page states free (erased) -> valid (programmed) valid -> stale (old copy, no longer needed) only an erase makes a page free again this figure 3 blocks x 4 pages x 4 KBThe flash translation layer
The controller runs the flash translation layer (FTL). It keeps a map from each LBA to the flash page that holds it, and puts every write into the next free page, like a log. The file system never sees the pages.
FTL map LBA -> flash page (in the controller) LBA 0 -> - LBA 1 -> - LBA 2 -> - write program the next free page, then update the map read look up the map, read that page pages B<block>.<page>, e.g. B0.3 = block 0, page 3 labels a page that shows L<n> holds LBA nLines 1 and 2: first writes
Lines 1 and 2 write three 4 KB blocks, and each fsync() sends them to the SSD: a.txt gets LBAs 0 and 1, b.txt gets LBA 2. The FTL programs them into the next free pages, B0.0, B0.1 and B0.2, and maps each one.
line 1 write(a, x, 8192); fsync(a); a.txt 8 KB = LBA 0, LBA 1 line 2 write(b, y, 4096); fsync(b); b.txt 4 KB = LBA 2 fsync waits until the data is on the SSD, not just in the page cache program B0.0 <- LBA 0 B0.1 <- LBA 1 B0.2 <- LBA 2 map 0 -> B0.0 1 -> B0.1 2 -> B0.2 flash programs so far: 3Line 3: no overwrite in place
Line 3 changes a.txt, and its fsync() makes the file system send LBA 1 to the SSD again. Flash cannot rewrite B0.1, so the FTL programs the next free page, B0.3, points LBA 1 there and marks B0.1 stale.
line 3 pwrite(a, z, 4096, 4096); fsync(a); a.txt bytes 4096-8191 = LBA 1 fsync a second, separate write of LBA 1 to the SSD program B0.3 <- LBA 1 (new data) map 1 -> B0.3 (was B0.1) B0.1 stale: the old copy, still on the flash flash programs so far: 4Line 4: a silent delete
Line 4 deletes b.txt. The file system marks LBA 2 free in its own records, but sends nothing to the SSD. So the SSD still maps LBA 2 to B0.2 and treats that dead data as live.
line 4 unlink("b.txt"); file system LBA 2 free (its own bitmap) SSD map 2 -> B0.2 (it was not told) B0.2 still valid to the SSD garbage collection would copy itTRIM tells the SSD
So the file system sends a TRIM (discard) for LBA 2: this block is not in use. The FTL drops LBA 2 from its map and marks B0.2 stale. Linux sends TRIM with the discard mount option or the fstrim command.
TRIM LBA 2 "this block is not in use" map 2 -> - (removed) B0.2 stale Linux: mount -o discard TRIM as blocks are freed fstrim / TRIM all unused blocks now (ext4: discard is off by default)Garbage collection
Block 0 is full, and half of it is stale. The controller's garbage collection copies its two live pages to B1.0 and B1.1, updates the map, then erases block 0. The file system wrote 4 pages but the flash programmed 6.
copy B0.0 (LBA 0) -> B1.0 B0.3 (LBA 1) -> B1.1 erase block 0: 4 free pages again write amplification = flash writes / host writes = 6 / 4 = 1.5 without TRIM, B0.2 is copied too: 7 / 4 = 1.75 the same FTL in Python: PAGES = 4 class SSD: def __init__(self, blocks=3): self.flash = [[None] * PAGES for _ in range(blocks)] self.map, self.erases = {}, [0] * blocks self.head, self.programs = (0, 0), 0 def program(self, lba): # next free page, like a log b, p = self.head self.flash[b][p] = [lba, True] # [lba, live] self.map[lba] = (b, p) self.programs += 1 self.head = (b, p + 1) if p + 1 < PAGES else (b + 1, 0) def trim(self, lba): # old copy becomes stale if lba in self.map: b, p = self.map.pop(lba) self.flash[b][p][1] = False def write(self, lba): self.trim(lba) self.program(lba) def gc(self, b): # copy live pages, erase for page in self.flash[b]: if page and page[1]: self.program(page[0]) self.flash[b] = [None] * PAGES self.erases[b] += 1 ssd = SSD() for lba in (0, 1, 2): # lines 1 and 2 ssd.write(lba) ssd.write(1) # line 3 ssd.trim(2) # line 4, then TRIM ssd.gc(0) print(ssd.map) print(ssd.programs, "programs for 4 writes:", ssd.programs / 4) print("erases", ssd.erases) prints: {0: (1, 0), 1: (1, 1)} 6 programs for 4 writes: 1.5 erases [1, 0, 0]Wear leveling
Each erase wears a block a little; makers rate MLC flash blocks at about 10,000 program/erase cycles. The FTL counts erases per block and spreads the work, even moving data that never changes, so no block wears out early.
erases block 0: 1 block 1: 0 block 2: 0 rated lifetime (OSTEP) MLC ~10,000 P/E cycles SLC ~100,000 P/E cycles wear leveling new writes go to lightly worn blocks; cold data is moved now and then (that costs extra writes too)File systems that fit flash
Log-structured file systems such as F2FS already write in big sequential segments, which suits flash. With TRIM, ext4, F2FS and others tell the SSD what is free, so garbage collection copies less and the flash wears more slowly.
F2FS a Linux file system for NAND flash, based on the log-structured file system (LFS) idea; writes in 2 MB segments, cleans whole segments ext4 overwrites blocks in place; the FTL remaps them both send TRIM (discard) for freed blocks fewer stale pages copied -> less write amplification -> fewer erases, longer life
5 quick questions
Choose one answer per question. You can change your choices before checking them.
01A flash page already holds data. What must happen before that same page can be written again?
Flash is programmed a page at a time but erased only a whole block at a time, and a programmed page cannot change until its block is erased. That is why the FTL writes changes somewhere else instead.
02The program rewrites LBA 1, which lives in flash page B0.1, and B0.3 is the next free page. What does the FTL do?
The FTL writes out of place, like a log: the new copy goes to the next free page and the map is updated. The old page becomes stale garbage that garbage collection reclaims later.
03unlink("b.txt") frees LBA 2 in the file system. Without TRIM, what does the SSD know about LBA 2?
Deleting a file changes only the file system's own records. Until a TRIM (discard) arrives, the SSD keeps the page as valid data, and garbage collection would copy it for nothing.
04The file system wrote 4 pages. Garbage collection then copied 2 live pages before erasing a block. What is the write amplification?
Write amplification is flash writes divided by host writes: (4 + 2) / 4 = 1.5. Without the TRIM the dead page would have been copied too, giving 7 / 4 = 1.75.
05Why does the FTL sometimes move data that never changes?
Each block survives a limited number of program/erase cycles (about 10,000 for MLC flash, as rated by makers). Wear leveling spreads erases evenly, even if that means rewriting cold data, which itself adds some write amplification.
Glossary
- Flash page
- The smallest unit flash can read or program, often 4 KB. Once programmed it cannot change until its block is erased.
- Erase block
- A group of flash pages, typically 128 or 256 KB, that can only be erased all together.
- LBA (logical block address)
- The number that names a place on the disk, usually a 512-byte sector (this lesson counts in 4 KB blocks). The SSD decides which flash page really holds it.
- Flash translation layer (FTL)
- Software in the SSD's controller that maps each LBA to a flash page, writes changes into free pages and cleans up stale ones.
- Stale page
- A flash page holding an old copy that is no longer needed. It still takes space until its block is erased.
- Garbage collection
- The FTL's clean-up: copy the live pages out of a block, then erase the whole block so its pages are free again.
- Write amplification
- Bytes the SSD writes to flash divided by bytes the host asked it to write. Above 1 means extra wear and extra work.
- TRIM (discard)
- A command the file system sends to tell the SSD a range of blocks is no longer in use, so it can drop them from the map.
Notes and sources
- Real SSDs have many thousands of blocks (a 256 KB block holds 64 pages of 4 KB), many flash chips working in parallel, and spare blocks the file system never sees. The figure keeps 3 blocks of 4 pages so every page is visible.
- Which LBAs a file gets is the file system's choice, and it also writes metadata (inodes, bitmaps, a journal). The figure shows only the data blocks and gives them LBAs 0, 1 and 2.
- Each write first lands in the kernel's page cache. The fsync() calls on lines 1 to 3 force the data to the SSD before the next line runs, so the drive sees 4 separate page writes: LBA 0, 1 and 2, then LBA 1 again. Without them, the overwrite could be merged in the cache, and b.txt could be deleted before it was ever written.
- On most drives an LBA names a 512-byte sector, so one 4 KB file-system block covers 8 LBAs (MIT 6.1810: "4 KB blocks = 8 sectors"). To keep the numbers small, the figure counts in 4 KB blocks and calls them LBA 0, 1 and 2; that matches real LBA numbers only on drives with 4 KB sectors (4Kn).
- The FTL map lives in the controller's fast memory. To survive a power cut, SSDs also record which LBA each page holds next to the page, or log the map to flash.
- Real controllers clean in the background, whenever free pages run low. The numbers here are for this exact run: 6 flash programs for 4 host writes.
- Lifetimes are the manufacturers' ratings quoted in OSTEP (MLC about 10,000 and SLC about 100,000 program/erase cycles); OSTEP adds that research has measured much longer lifetimes than expected.
- R. and A. Arpaci-Dusseau, Operating Systems: Three Easy Pieces, ch. 44: Flash-based SSDs
- R. and A. Arpaci-Dusseau, Operating Systems: Three Easy Pieces, ch. 43: Log-structured File Systems
- Berkeley CS162, Lecture 19: Storage Devices, Performance
- MIT 6.1810 (2024), lecture notes: File systems
- Linux kernel documentation: Flash-Friendly File System (F2FS)
- Linux kernel documentation: ext4 (discard mount option)
- Linux man-pages: fstrim(8)
- Linux man-pages: unlink(2)
- Linux man-pages: pwrite(2)
- Linux man-pages: fsync(2)
Quiz Unlocks after the last step.