Flash and the FTL
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.
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.
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.
Writing and deleting
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 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 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.
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.
Cleaning up
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.
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.
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.
What happens
;; step 1/10 · two-views
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 view
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.