trv: Split lfs3_trv_t -> lfs3_trv_t, lfs3_mgc_t, and lfs3_mtrv_t

A big downside of LFS3_T_REBUILDGBMAP is the addition of an lfs3_btree_t
struct to _every_ traversal object.

Unfortunately, I don't see a way around this. We need to track the new
gbmap snapshot _somewhere_, and other options (such as a global gbmap.b_
snapshot) just move the RAM around without actually saving anything.

To at least mitigate this internally, this splits lfs3_trv_t into
distinct lfs3_trv_t, lfs3_mgc_t, and lfs3_mtrv_t structs that capture
only the relevant state for internal traversal layers:

- lfs3_mtree_traverse <- lfs3_mtrv_t
- lfs3_mtree_gc       <- lfs3_mgc_t (contains lfs3_mtrv_t)
- lfs3_trv_read       <- lfs3_trv_t (contains lfs3_mgc_t)

This minimizes the impact of the gbmap rebuild snapshots, and saves a
big chunk of RAM. As a plus it also saves RAM in the default build by
limiting the 2-block block queue to the high-level lfs3_trv_read API:

                 code          stack          ctx
  before:       37176           2360          684
  after:        37176 (+0.0%)   2352 (-0.3%)  684 (+0.0%)

                 code          stack          ctx
  gbmap before: 40060           2432          848
  gbmap after:  40024 (-0.1%)   2368 (-2.6%)  848 (+0.0%)

The main downside? Our field names are continuing in their
ridiculousness:

  lfs3.gc.gc.t.b.h.flags // where else would the global gc flags be?
This commit is contained in:
Christopher Haster
2025-10-16 00:10:21 -05:00
parent 06bc4dff04
commit fb90bf976c
6 changed files with 299 additions and 285 deletions

448
lfs3.c

File diff suppressed because it is too large Load Diff

26
lfs3.h
View File

@ -819,7 +819,7 @@ typedef struct lfs3_dir {
} lfs3_dir_t;
// littlefs traversal type
typedef struct lfs3_trv {
typedef struct lfs3_mtrv {
// mdir/bshrub/btree state, this also includes our traversal
// state machine and cycle detection state
lfs3_bshrub_t b;
@ -828,12 +828,24 @@ typedef struct lfs3_trv {
// bshrub/btree traversal state
lfs3_sbid_t bid;
// rebuild gbmap when traversing with rebuildgbmap
#ifdef LFS3_GBMAP
lfs3_btree_t gbmap_;
#endif
// recalculate gcksum when traversing with ckmeta
uint32_t gcksum;
} lfs3_mtrv_t;
typedef struct lfs3_mgc {
// core traversal state
lfs3_mtrv_t t;
#ifdef LFS3_GBMAP
// rebuild gbmap when traversing with rebuildgbmap
lfs3_btree_t gbmap_;
#endif
} lfs3_mgc_t;
typedef struct lfs3_trv {
// core traversal/gc state
lfs3_mgc_t gc;
// pending blocks, only used in lfs3_trv_read
lfs3_sblock_t blocks[2];
} lfs3_trv_t;
@ -962,9 +974,7 @@ typedef struct lfs3 {
// optional incremental gc state
#ifdef LFS3_GC
struct {
lfs3_trv_t trv;
} gc;
lfs3_trv_t gc;
#endif
} lfs3_t;

View File

@ -207,8 +207,8 @@ code = '''
uint8_t *seen = malloc((BLOCK_COUNT+7)/8);
memset(seen, 0, (BLOCK_COUNT+7)/8);
lfs3_trv_t trv;
lfs3_trv_init(&trv,
lfs3_mtrv_t mtrv;
lfs3_mtrv_init(&mtrv,
LFS3_T_RDONLY
| ((CKMETA) ? LFS3_T_CKMETA : 0));
for (lfs3_block_t i = 0;; i++) {
@ -217,7 +217,7 @@ code = '''
lfs3_stag_t tag;
lfs3_bptr_t bptr;
tag = lfs3_mtree_traverse(&lfs3, &trv,
tag = lfs3_mtree_traverse(&lfs3, &mtrv,
&bptr);
assert(tag >= 0 || tag == LFS3_ERR_NOENT);
if (tag == LFS3_ERR_NOENT) {
@ -381,8 +381,8 @@ code = '''
uint8_t *seen = malloc((BLOCK_COUNT+7)/8);
memset(seen, 0, (BLOCK_COUNT+7)/8);
lfs3_trv_t trv;
lfs3_trv_init(&trv,
lfs3_mtrv_t mtrv;
lfs3_mtrv_init(&mtrv,
LFS3_T_RDONLY
| ((CKMETA) ? LFS3_T_CKMETA : 0));
for (lfs3_block_t i = 0;; i++) {
@ -391,7 +391,7 @@ code = '''
lfs3_stag_t tag;
lfs3_bptr_t bptr;
tag = lfs3_mtree_traverse(&lfs3, &trv,
tag = lfs3_mtree_traverse(&lfs3, &mtrv,
&bptr);
assert(tag >= 0 || tag == LFS3_ERR_NOENT);
if (tag == LFS3_ERR_NOENT) {
@ -541,8 +541,8 @@ code = '''
uint8_t *seen = malloc((BLOCK_COUNT+7)/8);
memset(seen, 0, (BLOCK_COUNT+7)/8);
lfs3_trv_t trv;
lfs3_trv_init(&trv,
lfs3_mtrv_t mtrv;
lfs3_mtrv_init(&mtrv,
LFS3_T_RDONLY
| ((CKMETA) ? LFS3_T_CKMETA : 0));
for (lfs3_block_t i = 0;; i++) {
@ -551,7 +551,7 @@ code = '''
lfs3_stag_t tag;
lfs3_bptr_t bptr;
tag = lfs3_mtree_traverse(&lfs3, &trv,
tag = lfs3_mtree_traverse(&lfs3, &mtrv,
&bptr);
assert(tag >= 0 || tag == LFS3_ERR_NOENT);
if (tag == LFS3_ERR_NOENT) {

View File

@ -1025,7 +1025,7 @@ code = '''
}
if (tinfo.btype == LFS3_BTYPE_BTREE
&& lfs3_t_tstate(trv.b.h.flags) != LFS3_TSTATE_GBMAP) {
&& lfs3_t_tstate(trv.gc.t.b.h.flags) != LFS3_TSTATE_GBMAP) {
if (k == i) {
// clobber this block
printf("clobbering 0x%x\n", tinfo.block);
@ -1125,7 +1125,7 @@ code = '''
if ((tinfo.btype == LFS3_BTYPE_BTREE
|| tinfo.btype == LFS3_BTYPE_DATA)
&& lfs3_t_tstate(trv.b.h.flags) != LFS3_TSTATE_GBMAP) {
&& lfs3_t_tstate(trv.gc.t.b.h.flags) != LFS3_TSTATE_GBMAP) {
if (k == i) {
// clobber this block
printf("clobbering 0x%x\n", tinfo.block);
@ -1231,7 +1231,7 @@ code = '''
}
if (tinfo.btype == LFS3_BTYPE_BTREE
&& lfs3_t_tstate(trv.b.h.flags) != LFS3_TSTATE_GBMAP) {
&& lfs3_t_tstate(trv.gc.t.b.h.flags) != LFS3_TSTATE_GBMAP) {
// found an interesting block?
if (k == i) {
badblock = tinfo.block;
@ -1382,7 +1382,7 @@ code = '''
if ((tinfo.btype == LFS3_BTYPE_BTREE
|| tinfo.btype == LFS3_BTYPE_DATA)
&& lfs3_t_tstate(trv.b.h.flags) != LFS3_TSTATE_GBMAP) {
&& lfs3_t_tstate(trv.gc.t.b.h.flags) != LFS3_TSTATE_GBMAP) {
// found an interesting block?
if (k == i) {
badblock = tinfo.block;

View File

@ -57,7 +57,7 @@ code = '''
struct lfs3_fsinfo fsinfo;
lfs3_fs_stat(&lfs3, &fsinfo) => 0;
assert(fsinfo.flags & LFS3_I_LOOKAHEAD);
assert(lfs3.handles != &lfs3.gc.trv.b.h);
assert(lfs3.handles != &lfs3.gc.gc.t.b.h);
// run GC until we make progress
for (lfs3_block_t i = 0;; i++) {
@ -127,11 +127,11 @@ code = '''
struct lfs3_fsinfo fsinfo;
lfs3_fs_stat(&lfs3, &fsinfo) => 0;
assert(fsinfo.flags & LFS3_I_LOOKAHEAD);
assert(lfs3.handles != &lfs3.gc.trv.b.h);
assert(lfs3.handles != &lfs3.gc.gc.t.b.h);
// run GC one step
lfs3_fs_gc(&lfs3) => 0;
assert(lfs3.handles == &lfs3.gc.trv.b.h);
assert(lfs3.handles == &lfs3.gc.gc.t.b.h);
// mutate the filesystem
lfs3_file_open(&lfs3, &file, "spider",
@ -143,7 +143,7 @@ code = '''
lfs3_file_close(&lfs3, &file) => 0;
// run GC until our traversal is done
while (lfs3.handles == &lfs3.gc.trv.b.h) {
while (lfs3.handles == &lfs3.gc.gc.t.b.h) {
lfs3_fs_gc(&lfs3) => 0;
}
@ -207,7 +207,7 @@ code = '''
struct lfs3_fsinfo fsinfo;
lfs3_fs_stat(&lfs3, &fsinfo) => 0;
assert(fsinfo.flags & LFS3_I_REBUILDGBMAP);
assert(lfs3.handles != &lfs3.gc.trv.b.h);
assert(lfs3.handles != &lfs3.gc.gc.t.b.h);
// run GC until we make progress
for (lfs3_block_t i = 0;; i++) {
@ -280,11 +280,11 @@ code = '''
struct lfs3_fsinfo fsinfo;
lfs3_fs_stat(&lfs3, &fsinfo) => 0;
assert(fsinfo.flags & LFS3_I_REBUILDGBMAP);
assert(lfs3.handles != &lfs3.gc.trv.b.h);
assert(lfs3.handles != &lfs3.gc.gc.t.b.h);
// run GC one step
lfs3_fs_gc(&lfs3) => 0;
assert(lfs3.handles == &lfs3.gc.trv.b.h);
assert(lfs3.handles == &lfs3.gc.gc.t.b.h);
// mutate the filesystem
lfs3_file_open(&lfs3, &file, "spider",
@ -296,7 +296,7 @@ code = '''
lfs3_file_close(&lfs3, &file) => 0;
// run GC until our traversal is done
while (lfs3.handles == &lfs3.gc.trv.b.h) {
while (lfs3.handles == &lfs3.gc.gc.t.b.h) {
lfs3_fs_gc(&lfs3) => 0;
}
@ -368,7 +368,7 @@ code = '''
struct lfs3_fsinfo fsinfo;
lfs3_fs_stat(&lfs3, &fsinfo) => 0;
assert(fsinfo.flags & LFS3_I_COMPACT);
assert(lfs3.handles != &lfs3.gc.trv.b.h);
assert(lfs3.handles != &lfs3.gc.gc.t.b.h);
// run GC until we make progress
for (lfs3_block_t i = 0;; i++) {
@ -460,19 +460,19 @@ code = '''
struct lfs3_fsinfo fsinfo;
lfs3_fs_stat(&lfs3, &fsinfo) => 0;
assert(fsinfo.flags & LFS3_I_COMPACT);
assert(lfs3.handles != &lfs3.gc.trv.b.h);
assert(lfs3.handles != &lfs3.gc.gc.t.b.h);
// run GC one traversal + one step
while (true) {
lfs3_fs_gc(&lfs3) => 0;
// internal traversal done?
if (lfs3.handles != &lfs3.gc.trv.b.h) {
if (lfs3.handles != &lfs3.gc.gc.t.b.h) {
break;
}
}
lfs3_fs_gc(&lfs3) => 0;
assert(lfs3.handles == &lfs3.gc.trv.b.h);
assert(lfs3.handles == &lfs3.gc.gc.t.b.h);
// mutate the filesystem
lfs3_file_rewind(&lfs3, &file) => 0;
@ -483,7 +483,7 @@ code = '''
lfs3_file_sync(&lfs3, &file) => 0;
// run GC until our traversal is done (twice for compact)
while (lfs3.handles == &lfs3.gc.trv.b.h) {
while (lfs3.handles == &lfs3.gc.gc.t.b.h) {
lfs3_fs_gc(&lfs3) => 0;
}
@ -587,7 +587,7 @@ code = '''
struct lfs3_fsinfo fsinfo;
lfs3_fs_stat(&lfs3, &fsinfo) => 0;
assert(fsinfo.flags & LFS3_I_MKCONSISTENT);
assert(lfs3.handles != &lfs3.gc.trv.b.h);
assert(lfs3.handles != &lfs3.gc.gc.t.b.h);
// run GC until we make progress
for (lfs3_block_t i = 0;; i++) {
@ -693,7 +693,7 @@ code = '''
lfs3_fs_stat(&lfs3, &fsinfo) => 0;
assert(fsinfo.flags & LFS3_I_MKCONSISTENT);
#ifdef LFS3_GC
assert(lfs3.handles != &lfs3.gc.trv.b.h);
assert(lfs3.handles != &lfs3.gc.gc.t.b.h);
#endif
// call lfs3_fs_mkconsistent
@ -797,9 +797,9 @@ code = '''
}
// run GC one step
assert(lfs3.handles != &lfs3.gc.trv.b.h);
assert(lfs3.handles != &lfs3.gc.gc.t.b.h);
lfs3_fs_gc(&lfs3) => 0;
assert(lfs3.handles == &lfs3.gc.trv.b.h);
assert(lfs3.handles == &lfs3.gc.gc.t.b.h);
// create the rest of the orphans after GC has started
for (lfs3_size_t i = 0; i < ORPHANS; i++) {
@ -821,7 +821,7 @@ code = '''
assert(fsinfo.flags & LFS3_I_MKCONSISTENT);
// run GC until our traversal is done
while (lfs3.handles == &lfs3.gc.trv.b.h) {
while (lfs3.handles == &lfs3.gc.gc.t.b.h) {
lfs3_fs_gc(&lfs3) => 0;
}

View File

@ -3665,8 +3665,8 @@ code = '''
uint8_t *seen = malloc((BLOCK_COUNT+7)/8);
memset(seen, 0, (BLOCK_COUNT+7)/8);
lfs3_trv_t trv;
lfs3_trv_init(&trv,
lfs3_mtrv_t mtrv;
lfs3_mtrv_init(&mtrv,
LFS3_T_RDONLY
| LFS3_T_MTREEONLY
| ((CKMETA) ? LFS3_T_CKMETA : 0));
@ -3676,7 +3676,7 @@ code = '''
lfs3_stag_t tag;
lfs3_bptr_t bptr;
tag = lfs3_mtree_traverse(&lfs3, &trv,
tag = lfs3_mtree_traverse(&lfs3, &mtrv,
&bptr);
assert(tag >= 0 || tag == LFS3_ERR_NOENT);
if (tag == LFS3_ERR_NOENT) {
@ -3787,8 +3787,8 @@ code = '''
uint8_t *seen = malloc((BLOCK_COUNT+7)/8);
memset(seen, 0, (BLOCK_COUNT+7)/8);
lfs3_trv_t trv;
lfs3_trv_init(&trv,
lfs3_mtrv_t mtrv;
lfs3_mtrv_init(&mtrv,
LFS3_T_RDONLY
| LFS3_T_MTREEONLY
| ((CKMETA) ? LFS3_T_CKMETA : 0));
@ -3798,7 +3798,7 @@ code = '''
lfs3_stag_t tag;
lfs3_bptr_t bptr;
tag = lfs3_mtree_traverse(&lfs3, &trv,
tag = lfs3_mtree_traverse(&lfs3, &mtrv,
&bptr);
assert(tag >= 0 || tag == LFS3_ERR_NOENT);
if (tag == LFS3_ERR_NOENT) {
@ -3935,8 +3935,8 @@ code = '''
uint8_t *seen = malloc((BLOCK_COUNT+7)/8);
memset(seen, 0, (BLOCK_COUNT+7)/8);
lfs3_trv_t trv;
lfs3_trv_init(&trv,
lfs3_mtrv_t mtrv;
lfs3_mtrv_init(&mtrv,
LFS3_T_RDONLY
| LFS3_T_MTREEONLY
| ((CKMETA) ? LFS3_T_CKMETA : 0));
@ -3946,7 +3946,7 @@ code = '''
lfs3_stag_t tag;
lfs3_bptr_t bptr;
tag = lfs3_mtree_traverse(&lfs3, &trv,
tag = lfs3_mtree_traverse(&lfs3, &mtrv,
&bptr);
assert(tag >= 0 || tag == LFS3_ERR_NOENT);
if (tag == LFS3_ERR_NOENT) {
@ -4106,8 +4106,8 @@ code = '''
uint8_t *seen = malloc((BLOCK_COUNT+7)/8);
memset(seen, 0, (BLOCK_COUNT+7)/8);
lfs3_trv_t trv;
lfs3_trv_init(&trv,
lfs3_mtrv_t mtrv;
lfs3_mtrv_init(&mtrv,
LFS3_T_RDONLY
| LFS3_T_MTREEONLY
| ((CKMETA) ? LFS3_T_CKMETA : 0));
@ -4117,7 +4117,7 @@ code = '''
lfs3_stag_t tag;
lfs3_bptr_t bptr;
tag = lfs3_mtree_traverse(&lfs3, &trv,
tag = lfs3_mtree_traverse(&lfs3, &mtrv,
&bptr);
assert(tag >= 0 || tag == LFS3_ERR_NOENT);
if (tag == LFS3_ERR_NOENT) {
@ -4257,8 +4257,8 @@ code = '''
uint8_t *seen = malloc((BLOCK_COUNT+7)/8);
memset(seen, 0, (BLOCK_COUNT+7)/8);
lfs3_trv_t trv;
lfs3_trv_init(&trv,
lfs3_mtrv_t mtrv;
lfs3_mtrv_init(&mtrv,
LFS3_T_RDONLY
| LFS3_T_MTREEONLY
| ((CKMETA) ? LFS3_T_CKMETA : 0));
@ -4268,7 +4268,7 @@ code = '''
lfs3_stag_t tag;
lfs3_bptr_t bptr;
tag = lfs3_mtree_traverse(&lfs3, &trv,
tag = lfs3_mtree_traverse(&lfs3, &mtrv,
&bptr);
assert(tag >= 0 || tag == LFS3_ERR_NOENT);
if (tag == LFS3_ERR_NOENT) {
@ -4383,8 +4383,8 @@ code = '''
uint8_t *seen = malloc((BLOCK_COUNT+7)/8);
memset(seen, 0, (BLOCK_COUNT+7)/8);
lfs3_trv_t trv;
lfs3_trv_init(&trv,
lfs3_mtrv_t mtrv;
lfs3_mtrv_init(&mtrv,
LFS3_T_RDONLY
| LFS3_T_MTREEONLY
| ((CKMETA) ? LFS3_T_CKMETA : 0));
@ -4394,7 +4394,7 @@ code = '''
lfs3_stag_t tag;
lfs3_bptr_t bptr;
tag = lfs3_mtree_traverse(&lfs3, &trv,
tag = lfs3_mtree_traverse(&lfs3, &mtrv,
&bptr);
assert(tag >= 0 || tag == LFS3_ERR_NOENT);
if (tag == LFS3_ERR_NOENT) {
@ -4553,8 +4553,8 @@ code = '''
uint8_t *seen = malloc((BLOCK_COUNT+7)/8);
memset(seen, 0, (BLOCK_COUNT+7)/8);
lfs3_trv_t trv;
lfs3_trv_init(&trv,
lfs3_mtrv_t mtrv;
lfs3_mtrv_init(&mtrv,
LFS3_T_RDONLY
| LFS3_T_MTREEONLY
| ((CKMETA) ? LFS3_T_CKMETA : 0));
@ -4564,7 +4564,7 @@ code = '''
lfs3_stag_t tag;
lfs3_bptr_t bptr;
tag = lfs3_mtree_traverse(&lfs3, &trv,
tag = lfs3_mtree_traverse(&lfs3, &mtrv,
&bptr);
assert(tag >= 0 || tag == LFS3_ERR_NOENT);
if (tag == LFS3_ERR_NOENT) {
@ -4686,8 +4686,8 @@ code = '''
LFS3_MPTR_MROOTANCHOR()))) => 0;
// technically, cycle detection only needs to work when we're validating
lfs3_trv_t trv;
lfs3_trv_init(&trv,
lfs3_mtrv_t mtrv;
lfs3_mtrv_init(&mtrv,
LFS3_T_RDONLY | LFS3_T_MTREEONLY | LFS3_T_CKMETA);
for (lfs3_block_t i = 0;; i++) {
// assert that we detect the cycle in a reasonable number of iterations
@ -4695,7 +4695,7 @@ code = '''
lfs3_stag_t tag;
lfs3_bptr_t bptr;
tag = lfs3_mtree_traverse(&lfs3, &trv,
tag = lfs3_mtree_traverse(&lfs3, &mtrv,
&bptr);
assert(tag >= 0 || tag == LFS3_ERR_CORRUPT);
if (tag == LFS3_ERR_CORRUPT) {