preerase: Implemented the gc side of preerase

Allocating pre-erased blocks gets quite complicated due to our
restricted flash model, but at least the actual pre-erasing is
relatively straightforward:

- We keep track of known preerased state in lfs3->gbmap.preeraser.

- If LFS3_GC_PREERASE is provided during gc work, we increment the
  preeraser's known window by scanning the gbmap.

- Any BMFREE ranges we find, we erase a block at a time, and store the
  resulting ecksum in a BMERASED range in the gbmap.

- We keep track of how many blocks we erased, and stop early if this
  exceeds cfg.gc_preerase_count. This just lets users tune how many
  blocks to preerase in case something (?) prevents preerased blocks
  from being used.

Some notes:

- We don't really do anything with ranges in lfs3_alloc_preerase. In
  theory we could bulk in erase to minimize the number of commits to the
  gbmap, but we expect erase to dominate, so this probably isn't worth
  it.

  And if erase doesn't dominate, why would you bother pre-erasing
  blocks?

- Preerasing isn't really a traversal operation, and is managed by a
  sort of secondary state machine in lfs3_fs_gc_.

  This also means lfs3_trv_read with LFS3_T_PREERASE does nothing, but I
  guess that is ok? It's tempting to try to make lfs3_trv_read also
  preerase, but it's unclear what block it should return -- it's
  probably the wrong API.

- Introducing ecksums actually went quite a bit smoother than I
  expected. Though it helps ecksums are the only optional payload, no
  type punning or anything.

  Ecksums do muddy the gbmap's design a bit, unfortunately. The main
  issue being that we can only merge BMERASED ranges with equal ecksums.
  This makes BMERASED ranges less compressable than the others, and may
  be one reason to limit cfg.gc_preerase_count.

  However:

  1. This is where I think it's useful to emphasize that the gbmap's
     responsibility is to track _free_ blocks, in-use blocks are
     secondary.

     When allocating, we're going to stop at the first BMFREE/BMERASED,
     but may need to skip over an unbounded number of BMINUSE/BMBAD
     blocks. So the compressability of BMFREE/BMERASED ranges should
     have less of an impact on block allocation.

  2. In practice, most flash uses consistent erase values, so the
     resulting ecksums will probably be compressable. The exceptions are
     noop-erases (SD/eMMC, RAM, NVRAM, etc), and encryption with block
     address permutation?

     Though noop-erases are a pretty big exception.

Code changes:

                    code          stack          ctx
  before:          35116           2136          660
  after:           35116 (+0.0%)   2136 (+0.0%)  660 (+0.0%)

                    code          stack          ctx
  gbmap+np before: 38040           2136          776
  gbmap+np after:  38188 (+0.4%)   2144 (+0.4%)  776 (+0.0%)

                    code          stack          ctx
  gbmap+yp before: 38040           2136          776
  gbmap+yp after:  38608 (+1.5%)   2144 (+0.4%)  796 (+2.6%)
This commit is contained in:
Christopher Haster
2025-12-08 01:51:41 -06:00
parent f05be19d0e
commit 843412cc79
7 changed files with 1049 additions and 201 deletions
+11 -5
View File
@@ -84,11 +84,12 @@ F_CKDATACKSUMS = 0x01000000 # y- Check data checksums on reads
F_MKCONSISTENT = 0x00000100 # y- Make the filesystem consistent
F_LOOKAHEAD = 0x00000200 # y- Repopulate lookahead buffer
F_PREERASE = 0x00000400 # y- Try to pre-erase free blocks
F_COMPACT = 0x00000800 # y- Compact metadata logs
F_CKMETA = 0x00001000 # y- Check metadata checksums
F_CKDATA = 0x00002000 # y- Check metadata + data checksums
F_CK = 0x00003000 # a- Alias for all check work
F_GC = 0x00003b00 # a- Alias for all gc work
F_GC = 0x00003f00 # a- Alias for all gc work
# Filesystem mount flags
M_MODE = 1 # -m Mount's access mode
@@ -105,29 +106,32 @@ M_CKDATACKSUMS = 0x01000000 # y- Check data checksums on reads
M_MKCONSISTENT = 0x00000100 # y- Make the filesystem consistent
M_LOOKAHEAD = 0x00000200 # y- Repopulate lookahead buffer
M_PREERASE = 0x00000400 # y- Try to pre-erase free blocks
M_COMPACT = 0x00000800 # y- Compact metadata logs
M_CKMETA = 0x00001000 # y- Check metadata checksums
M_CKDATA = 0x00002000 # y- Check metadata + data checksums
M_CK = 0x00003000 # a- Alias for all check work
M_GC = 0x00003b00 # a- Alias for all gc work
M_GC = 0x00003f00 # a- Alias for all gc work
# File/filesystem check flags
CK_MKCONSISTENT = 0x00000100 # -- Make the filesystem consistent
CK_LOOKAHEAD = 0x00000200 # -- Repopulate lookahead buffer
CK_PREERASE = 0x00000400 # -- Try to pre-erase free blocks
CK_COMPACT = 0x00000800 # -- Compact metadata logs
CK_CKMETA = 0x00001000 # -- Check metadata checksums
CK_CKDATA = 0x00002000 # -- Check metadata + data checksums
CK_CK = 0x00003000 # a- Alias for all check work
CK_GC = 0x00003b00 # a- Alias for all gc work
CK_GC = 0x00003f00 # a- Alias for all gc work
# GC flags
GC_MKCONSISTENT = 0x00000100 # -- Make the filesystem consistent
GC_LOOKAHEAD = 0x00000200 # -- Repopulate lookahead buffer
GC_PREERASE = 0x00000400 # -- Try to pre-erase free blocks
GC_COMPACT = 0x00000800 # -- Compact metadata logs
GC_CKMETA = 0x00001000 # -- Check metadata checksums
GC_CKDATA = 0x00002000 # -- Check metadata + data checksums
GC_CK = 0x00003000 # a- Alias for all check work
GC_GC = 0x00003b00 # a- Alias for all gc work
GC_GC = 0x00003f00 # a- Alias for all gc work
# Filesystem info flags
I_RDONLY = 0x00000001 # -- Mounted read only
@@ -144,6 +148,7 @@ I_CKDATACKSUMS = 0x01000000 # -- Mounted with LFS3_M_CKDATACKSUMS
I_MKCONSISTENT = 0x00000100 # -- Filesystem needs mkconsistent to write
I_LOOKAHEAD = 0x00000200 # -- Lookahead buffer is not full
I_PREERASE = 0x00000400 # -- Blocks can be pre-erased
I_COMPACT = 0x00000800 # -- Filesystem may have uncompacted metadata
I_CKMETA = 0x00001000 # -- Metadata checksums not checked recently
I_CKDATA = 0x00002000 # -- Data checksums not checked recently
@@ -156,11 +161,12 @@ T_MTREEONLY = 0x00000002 # -- Only traverse the mtree
T_EXCL = 0x00000008 # -- Error if filesystem modified
T_MKCONSISTENT = 0x00000100 # -- Make the filesystem consistent
T_LOOKAHEAD = 0x00000200 # -- Repopulate lookahead buffer
T_PREERASE = 0x00000400 # -- Try to pre-erase free blocks
T_COMPACT = 0x00000800 # -- Compact metadata logs
T_CKMETA = 0x00001000 # -- Check metadata checksums
T_CKDATA = 0x00002000 # -- Check metadata + data checksums
T_CK = 0x00003000 # a- Alias for all check work
T_GC = 0x00003b00 # a- Alias for all gc work
T_GC = 0x00003f00 # a- Alias for all gc work
t_TYPE = 0xf0000000 # im The traversal's type
t_REG = 0x10000000 # i^ Type = regular-file