bmap: Changing direction, store bmap mode in wcompat flags

The idea behind separate ctrled+unctrled airspaces was to try to avoid
multiple interpretations of the on-disk bmap, but I'm starting to think
this adds more complexity than it solves.

The main conflict is the meaning of "in-flight" blocks. When using the
"uncontrolled" bmap algorithm, in-flight blocks need to be
double-checked by traversing the filesystem. But in the "controlled"
bmap algorithm, blocks are only marked as "in-flight" while they are
truly in-flight (in-use in RAM, but not yet in use on disk).
Representing these both with the same "in-flight" state risks
incompatible algorithms misinterpreting the bmap across different
mounts.

In theory the separate airspaces solve this, but now all the algorithms
need to know how to convert the bmap from different modes, adding
complexity and code cost.

Well, in theory at least. I'm unsure separate airspaces actually solves
this due to subtleties between what "in-flight" means in the different
algorithms (note both in-use and free blocks are "in-flight" in the
unknown airspace!). It really depends on how the "controlled" algorithm
actually works, which isn't implemented/fully designed yet.

---

Long story short, due to a time crunch, I'm ripping this out for now and
just storing the current algorithm in the wcompat flags:

  LFS3_WCOMPAT_GBMAP       0x00006000  Global block-map in use
  LFS3_WCOMPAT_GBMAPNONE   0x00000000  Gbmap not in use
  LFS3_WCOMPAT_GBMAPCACHE  0x00002000  Gbmap in cache mode
  LFS3_WCOMPAT_GBMAPVFR    0x00004000  Gbmap in VFR mode
  LFS3_WCOMPAT_GBMAPIFR    0x00006000  Gbmap in IFR mode

Note GBMAPVFR/IFR != BMAPSLOW/FAST! At least BMAPSLOW/FAST can share
bmap representations:

- GBMAPVFR => Uncontrolled airspace, i.e. in-flight blocks may or may
  not be in use, need to traverse open files.

- GBMAPIFR => Controlled airspace, i.e. in-flight blocks are in use,
  at least until powerloss, no traversal needed, but requires more bmap
  writes.

- BMAPSLOW => Treediff by checking what blocks are in B but not in A,
  and what blocks are in A but not in B, O(n^2), but minimizes bmap
  updates.

  Can be optimized with a bloom filter.

- BMAPFAST => Treediff by clearing all blocks in A, and then setting all
  blocks in B, O(n), but also writes all blocks to the bmap twice even
  on small changes.

  Can be optimized with a sliding bitmap window (or a block hashtable,
  though a bitmap converges to the same thing in both algorithms when
  >=disk_size).

It will probably be worth unifying the bmap representation later (the
more algorithm-specific flags there are, the harder interop becomes for
users, but for now this opens a path to implementing/experimenting with
bmap algorithms without dealing with this headache.
This commit is contained in:
Christopher Haster
2025-07-27 16:41:30 -05:00
parent beb1f1346a
commit ebae43898e
7 changed files with 156 additions and 185 deletions
+16 -30
View File
@@ -193,12 +193,12 @@ 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_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_BMAPSLOW 0x02000000 // Use the slow bmap algorithm
#define LFS3_F_BMAPFAST 0x03000000 // Use the fast bmap algorithm
#define LFS3_F_BMAPVFR 0x02000000 // Use the bmap in VFR mode
#define LFS3_F_BMAPIFR 0x03000000 // Use the bmap in IFR mode
#endif
#endif
@@ -248,11 +248,11 @@ enum lfs3_type {
#ifdef LFS3_BMAP
#define LFS3_M_BMAPMODE 0x03000000 // On-disk block map mode
#define LFS3_M_BMAPNONE 0x00000000 // Don't use the bmap
#define LFS3_M_BMAPNONE 0x00000000 // Don't use bmap
#define LFS3_M_BMAPCACHE \
0x01000000 // Use the bmap to cache lookahead scans
#define LFS3_M_BMAPSLOW 0x02000000 // Use the slow bmap algorithm
#define LFS3_M_BMAPFAST 0x03000000 // Use the fast bmap algorithm
#define LFS3_M_BMAPVFR 0x02000000 // Use the bmap in VFR mode
#define LFS3_M_BMAPIFR 0x03000000 // Use the bmap in IFR mode
#endif
@@ -301,8 +301,8 @@ enum lfs3_type {
#define LFS3_I_BMAPNONE 0x00000000 // Mounted with LFS3_M_BMAPNONE
#define LFS3_I_BMAPCACHE \
0x01000000 // Mounted with LFS3_M_BMAPCACHE
#define LFS3_I_BMAPSLOW 0x02000000 // Mounted with LFS3_M_BMAPSLOW
#define LFS3_I_BMAPFAST 0x03000000 // Mounted with LFS3_M_BMAPFAST
#define LFS3_I_BMAPVFR 0x02000000 // Mounted with LFS3_M_BMAPVFR
#define LFS3_I_BMAPIFR 0x03000000 // Mounted with LFS3_M_BMAPIFR
#endif
// internally used flags, don't use these
@@ -843,25 +843,22 @@ typedef struct lfs3_grm {
} lfs3_grm_t;
// gbmap encoding:
// .---+- -+- -+- -+- -. cursor: 1 leb128 <=5 bytes
// | cursor | ctrled: 1 leb128 <=5 bytes
// +---+- -+- -+- -+- -+ unctrled: 1 leb128 <=5 bytes
// | ctrled | block: 1 leb128 <=5 bytes
// +---+- -+- -+- -+- -+ trunk: 1 leb128 <=4 bytes
// | unctrled | cksum: 1 le32 4 bytes
// +---+- -+- -+- -+- -+ total: 28 bytes
// | block |
// .---+- -+- -+- -+- -. cursor: 1 leb128 <=5 bytes
// | cursor | known: 1 leb128 <=5 bytes
// +---+- -+- -+- -+- -+ block: 1 leb128 <=5 bytes
// | known | trunk: 1 leb128 <=4 bytes
// +---+- -+- -+- -+- -+ cksum: 1 le32 4 bytes
// | block | total: 23 bytes
// +---+- -+- -+- -+- -'
// | trunk |
// +---+- -+- -+- -+
// | cksum |
// '---+---+---+---'
#define LFS3_GBMAP_DSIZE (5+5+5+5+4+4)
#define LFS3_GBMAP_DSIZE (5+5+5+4+4)
typedef struct lfs3_gbmap {
lfs3_block_t cursor;
lfs3_block_t ctrled;
lfs3_block_t unctrled;
lfs3_block_t known;
lfs3_btree_t b;
} lfs3_gbmap_t;
@@ -952,18 +949,7 @@ typedef struct lfs3 {
uint8_t grm_d[LFS3_GRM_DSIZE];
#if !defined(LFS3_RDONLY) && !defined(LFS3_2BONLY) && defined(LFS3_BMAP)
// TODO do we only need known for the in-flight block-map?
// on-disk block-map
lfs3_gbmap_t gbmap;
// in-flight block-map
struct {
lfs3_block_t cursor;
lfs3_block_t known;
lfs3_block_t free;
//lfs3_block_t erased; // TODO
lfs3_btree_t gbatc;
} bmap;
// block-map delta state
uint8_t gbmap_p[LFS3_GBMAP_DSIZE];
uint8_t gbmap_d[LFS3_GBMAP_DSIZE];
#endif