bmap: Simplified bmap configs, reduced to one LFS3_F_GBMAP flag

TLDR: This drops the idea of different bmap strategies/modes, and sorts
out most of the compile-time/runtime conditional bmap interactions.

---

Motivation: Benchmarking (at least up to the 32-bit word limit) has
shown the bmap will unlikely be a significant bottleneck, even on large
disks. The largest disks tend to be NAND, and NAND's ridiculous block
size limits pressure on block allocation.

There are still concerns for areas I haven't measured yet:

- SD/eMMC/FTL - Small blocks, so more pressure on block allocation. In
  theory the logical block size can be artificially increased, but this
  comes with a granularity tradeoff.

- I've only measured throughput, latency is a whole other story.

  However, users have reported lfs3_fs_gc is useful for mitigating this,
  so maybe latency is less of a concern now?

But while there may still be room for improvement via alternative bmap
strategies, the risk a concerning amount of complexity. Yes,
configuration gets more complicated, but the real issue is any bmap
strategies that try to track _deallocations_ (the original idea being
treediffing) risk falling leaking blocks if all cases aren't covered.

The current "bmap cache" strategy strikes a really nice balance where it
reduces _amortized_ block allocation -> ~O(log n) without RAM, while
retaining the safe, bug-resistant, single-source-of-truth properties
that come with lookahead-based allocation.

---

So, long story short, dropping other strategies, and now the presence of
the bmap is a boolean flag.

This is also the first format-specific flag:

- Define LFS3_BMAP to enable the bmap logic, but note by default the
  bmap will still not be used.

- Define LFS3_YES_BMAP to force the bmap to be used.

- With LFS3_BMAP, passing LFS3_F_GBMAP to lfs3_format will include the
  on-disk block-map.

- No flag is needed during mount, the presence of the bmap is determined
  by the on-disk wcompat flags (LFS3_WCOMPAT_GBMAP). This also prevents
  rw mounting if the bmap is not supported, but rdonly mounting is
  allowed.

- Users can check if the bmap is in use via lfs3_fs_stat, which reports
  LFS3_I_GBMAP in the flags field.

There's still some missing pieces, but these will be a bit more
involved:

- lfs3_fs_grow needs to be made bmap aware!

- We probably want something like lfs3_fs_mkgbmap and lfs3_fs_rmgbmap to
  allow converting between bmap backed/not-backed filesystem images.

Code changes minimal:

                code          stack          ctx
  before:      37172           2352          684
  after:       37172 (+0.0%)   2352 (+0.0%)  684 (+0.0%)

                code          stack          ctx
  bmap before: 38844           2456          800
  bmap after:  38852 (+0.0%)   2456 (+0.0%)  800 (+0.0%)
This commit is contained in:
Christopher Haster
2025-10-06 13:06:38 -05:00
parent 38cfa5cc5e
commit 9d322741ca
8 changed files with 374 additions and 449 deletions
+4 -23
View File
@@ -194,12 +194,7 @@ enum lfs3_type {
#define LFS3_F_CKDATA 0x00002000 // Check metadata + data checksums
#ifdef LFS3_BMAP
#define LFS3_F_BMAPMODE 0x03000000 // On-disk block-map mode
#define LFS3_F_BMAPNONE 0x00000000 // Don't use the bmap
#define LFS3_F_BMAPCACHE \
0x01000000 // Use the bmap to cache lookahead scans
#define LFS3_F_BMAPVFR 0x02000000 // Use the bmap in VFR mode
#define LFS3_F_BMAPIFR 0x03000000 // Use the bmap in IFR mode
#define LFS3_F_GBMAP 0x01000000 // Use the global on-disk block-map
#endif
#endif
@@ -247,15 +242,6 @@ enum lfs3_type {
#define LFS3_M_CKMETA 0x00001000 // Check metadata checksums
#define LFS3_M_CKDATA 0x00002000 // Check metadata + data checksums
#ifdef LFS3_BMAP
#define LFS3_M_BMAPMODE 0x03000000 // On-disk block map mode
#define LFS3_M_BMAPNONE 0x00000000 // Don't use bmap
#define LFS3_M_BMAPCACHE \
0x01000000 // Use the bmap to cache lookahead scans
#define LFS3_M_BMAPVFR 0x02000000 // Use the bmap in VFR mode
#define LFS3_M_BMAPIFR 0x03000000 // Use the bmap in IFR mode
#endif
// Filesystem info flags
#define LFS3_I_RDONLY 0x00000001 // Mounted read only
@@ -298,19 +284,14 @@ enum lfs3_type {
#define LFS3_I_CKDATA 0x00002000 // Data checksums not checked recently
#ifdef LFS3_BMAP
#define LFS3_I_BMAPMODE 0x03000000 // On-disk block map mode
#define LFS3_I_BMAPNONE 0x00000000 // Mounted with LFS3_M_BMAPNONE
#define LFS3_I_BMAPCACHE \
0x01000000 // Mounted with LFS3_M_BMAPCACHE
#define LFS3_I_BMAPVFR 0x02000000 // Mounted with LFS3_M_BMAPVFR
#define LFS3_I_BMAPIFR 0x03000000 // Mounted with LFS3_M_BMAPIFR
#define LFS3_I_GBMAP 0x01000000 // Global on-disk block-map in use
#endif
// internally used flags, don't use these
#ifdef LFS3_REVDBG
#define LFS3_i_INMODE 0x00030000
#define LFS3_i_INMODE 0x00030000 // Btree commit mode
#define LFS3_i_INMTREE 0x00010000 // Committing to mtree
#define LFS3_i_INBMAP 0x00020000 // Committing to bmap
#define LFS3_i_INGBMAP 0x00020000 // Committing to gbmap
#endif