ENع
→File systems on SSDs
الدرس 47 · File systems

File systems on SSDs

لم يُترجم بعد؛ يُعرض باللغة الإنجليزية

14 دقيقة10 خطوات5 أسئلةAdvanced
عن هذا الدرس

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.

ستتعلّم

حدّد ما تشعر بأنك أتقنته. هذا التقييم الذاتي لا يؤثر في درجة الدرس.

شكل 47 · تفاعلي
01 / 10

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 view
جارٍ تحميل الرسم…
أو ← →
اقرأ الدرس خطوة بخطوة
  1. 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 view
  2. Flash: 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 KB
  3. The 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 n
  4. Lines 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: 3
  5. Line 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: 4
  6. Line 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 it
  7. TRIM 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)
  8. 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]
  9. 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)
  10. 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

اختبار يتاح بعد الخطوة الأخيرة.