Extended lfsr_format with some gc flags

These fall out quite naturally when you consider that we call
lfsr_mountinited internally to check that our format was successful.

That being said... they don't really do anything right now since we only
write a single mdir:

- LFS_F_COMPACT - The only gc operation that _might_ actually do
  something is LFS_F_COMPACT, but only if our fs config exceeds >1/2 the
  block size. But I'm not sure littlefs will even be able to write file
  metadata if this happens...

- LFS_F_CKMETA - We already check the only mdir by calling
  lfsr_mountinited, which implicitly fetches the mrootanchor.

- LFS_F_CKDATA - We uh, don't have any data immediately after
  lfsr_format. But I guess it doesn't hurt to keep this around for
  consistency, it at least implies CKMETA.

Hopefully these flags will be more interesting if/when we start adding
auxiliary trees to the filesystem, otherwise they may be worth reverting
in the future...

Until then, they at least provide some consistency, and I guess a way to
triply check that format was successful.

---

This could probably be better deduplicated, but calling lfsr_fs_gc from
both lfsr_mount and lfsr_format provides a bit better code organization:

           code          stack
  before: 36448           2680
  after:  36480 (+0.1%)   2680 (+0.0%)
This commit is contained in:
Christopher Haster
2024-08-12 15:03:24 -05:00
parent acad3a3143
commit 6d0b05da6c
3 changed files with 93 additions and 32 deletions
+49 -25
View File
@@ -13373,21 +13373,6 @@ int lfsr_mount(lfs_t *lfs, uint32_t flags,
goto failed;
}
// TODO this should use any configured values
LFS_DEBUG("Mounted littlefs v%"PRId32".%"PRId32" "
"%"PRId32"x%"PRId32" "
"0x{%"PRIx32",%"PRIx32"}.%"PRIx32" "
"w%"PRId32".%"PRId32,
LFS_DISK_VERSION_MAJOR,
LFS_DISK_VERSION_MINOR,
lfs->cfg->block_size,
lfs->block_count,
lfs->mroot.rbyd.blocks[0],
lfs->mroot.rbyd.blocks[1],
lfsr_rbyd_trunk(&lfs->mroot.rbyd),
lfsr_mtree_weight_(&lfs->mtree) >> lfs->mdir_bits,
1 << lfs->mdir_bits);
// run gc if requested
if (flags & (
LFS_M_MTREEONLY
@@ -13409,6 +13394,21 @@ int lfsr_mount(lfs_t *lfs, uint32_t flags,
}
}
// TODO this should use any configured values
LFS_DEBUG("Mounted littlefs v%"PRId32".%"PRId32" "
"%"PRId32"x%"PRId32" "
"0x{%"PRIx32",%"PRIx32"}.%"PRIx32" "
"w%"PRId32".%"PRId32,
LFS_DISK_VERSION_MAJOR,
LFS_DISK_VERSION_MINOR,
lfs->cfg->block_size,
lfs->block_count,
lfs->mroot.rbyd.blocks[0],
lfs->mroot.rbyd.blocks[1],
lfsr_rbyd_trunk(&lfs->mroot.rbyd),
lfsr_mtree_weight_(&lfs->mtree) >> lfs->mdir_bits,
1 << lfs->mdir_bits);
return 0;
failed:;
@@ -13493,12 +13493,6 @@ static int lfsr_formatinited(lfs_t *lfs) {
return err;
}
// test that mount works with our formatted disk
err = lfsr_mountinited(lfs);
if (err) {
return err;
}
return 0;
}
@@ -13508,7 +13502,11 @@ int lfsr_format(lfs_t *lfs, uint32_t flags,
LFS_ASSERT((flags & ~(
LFS_F_RDWR
| LFS_F_CKPROGS
| LFS_IFDEF_CKREADS(LFS_F_CKREADS, 0))) == 0);
| LFS_IFDEF_CKREADS(LFS_F_CKREADS, 0)
| LFS_F_MTREEONLY
| LFS_F_COMPACT
| LFS_F_CKMETA
| LFS_F_CKDATA)) == 0);
// some flags don't make sense when only traversing the mtree
LFS_ASSERT(!lfsr_t_ismtreeonly(flags) || !lfsr_t_isckdata(flags));
@@ -13526,12 +13524,38 @@ int lfsr_format(lfs_t *lfs, uint32_t flags,
err = lfsr_formatinited(lfs);
if (err) {
// make sure we clean up on error
lfs_deinit(lfs);
return err;
goto failed;
}
// test that mount works with our formatted disk
err = lfsr_mountinited(lfs);
if (err) {
goto failed;
}
// run gc if requested
if (flags & (
LFS_F_MTREEONLY
| LFS_F_COMPACT
| LFS_F_CKMETA
| LFS_F_CKDATA)) {
err = lfsr_fs_gc(lfs, -1,
flags & (
LFS_F_MTREEONLY
| LFS_F_COMPACT
| LFS_F_CKMETA
| LFS_F_CKDATA));
if (err) {
goto failed;
}
}
return lfs_deinit(lfs);
failed:;
// make sure we clean up on error
lfs_deinit(lfs);
return err;
}
+5
View File
@@ -158,6 +158,11 @@ enum lfs_type {
#define LFS_F_CKREADS 0x00000020 // Check reads via parity bits/checksums
#endif
#define LFS_F_MTREEONLY 0x00010000 // Only traverse the mtree
#define LFS_F_COMPACT 0x00000800 // Compact metadata logs
#define LFS_F_CKMETA 0x00001000 // Check metadata checksums
#define LFS_F_CKDATA 0x00002000 // Check metadata + data checksums
// Filesystem mount flags
#define LFS_M_RDWR 0 // Mount the filesystem as read and write
#define LFS_M_RDONLY 1 // Mount the filesystem as read only
+39 -7
View File
@@ -11,14 +11,28 @@ code = '''
lfsr_unmount(&lfs) => 0;
'''
# test that various mount flags are returned by lfsr_fs_stat
# test that various mount flags don't assert and are returned by
# lfsr_fs_stat
[cases.test_mount_flags]
defines.RDONLY = [false, true]
defines.CKPROGS = [false, true]
defines.CKREADS = [false, true]
defines.FLUSH = [false, true]
defines.SYNC = [false, true]
if = 'LFS_IFDEF_CKREADS(true, !CKREADS)'
defines.MTREEONLY = [false, true]
defines.MKCONSISTENT = [false, true]
defines.LOOKAHEAD = [false, true]
defines.COMPACT = [false, true]
defines.CKMETA = [false, true]
defines.CKDATA = [false, true]
if = [
'LFS_IFDEF_CKREADS(true, !CKREADS)',
'!RDONLY || !MKCONSISTENT',
'!RDONLY || !LOOKAHEAD',
'!RDONLY || !COMPACT',
'!MTREEONLY || !LOOKAHEAD',
'!MTREEONLY || !CKDATA',
]
code = '''
lfs_t lfs;
lfsr_format(&lfs, LFS_F_RDWR, CFG) => 0;
@@ -27,9 +41,16 @@ code = '''
| ((CKPROGS) ? LFS_M_CKPROGS : 0)
| ((CKREADS) ? LFS_IFDEF_CKREADS(LFS_M_CKREADS, 0) : 0)
| ((FLUSH) ? LFS_M_FLUSH : 0)
| ((SYNC) ? LFS_M_SYNC : 0),
| ((SYNC) ? LFS_M_SYNC : 0)
| ((MTREEONLY) ? LFS_M_MTREEONLY : 0)
| ((MKCONSISTENT) ? LFS_M_MKCONSISTENT : 0)
| ((LOOKAHEAD) ? LFS_M_LOOKAHEAD : 0)
| ((COMPACT) ? LFS_M_COMPACT : 0)
| ((CKMETA) ? LFS_M_CKMETA : 0)
| ((CKDATA) ? LFS_M_CKDATA : 0),
CFG) => 0;
// lfsr_fs_stat only returns some flags
struct lfs_fsinfo fsinfo;
lfsr_fs_stat(&lfs, &fsinfo) => 0;
assert(fsinfo.flags == (
@@ -38,8 +59,8 @@ code = '''
| ((CKREADS) ? LFS_IFDEF_CKREADS(LFS_I_CKREADS, 0) : 0)
| ((FLUSH) ? LFS_I_FLUSH : 0)
| ((SYNC) ? LFS_I_SYNC : 0)
| LFS_I_CANLOOKAHEAD
| LFS_I_UNCOMPACTED));
| ((!LOOKAHEAD) ? LFS_I_CANLOOKAHEAD : 0)
| ((!COMPACT) ? LFS_I_UNCOMPACTED : 0)));
lfsr_unmount(&lfs) => 0;
'''
@@ -50,13 +71,24 @@ code = '''
[cases.test_mount_format_flags]
defines.CKPROGS = [false, true]
defines.CKREADS = [false, true]
if = 'LFS_IFDEF_CKREADS(true, !CKREADS)'
defines.MTREEONLY = [false, true]
defines.COMPACT = [false, true]
defines.CKMETA = [false, true]
defines.CKDATA = [false, true]
if = [
'LFS_IFDEF_CKREADS(true, !CKREADS)',
'!MTREEONLY || !CKDATA',
]
code = '''
lfs_t lfs;
lfsr_format(&lfs,
LFS_F_RDWR
| ((CKPROGS) ? LFS_F_CKPROGS : 0)
| ((CKREADS) ? LFS_IFDEF_CKREADS(LFS_F_CKREADS, 0) : 0),
| ((CKREADS) ? LFS_IFDEF_CKREADS(LFS_F_CKREADS, 0) : 0)
| ((MTREEONLY) ? LFS_M_MTREEONLY : 0)
| ((COMPACT) ? LFS_M_COMPACT : 0)
| ((CKMETA) ? LFS_M_CKMETA : 0)
| ((CKDATA) ? LFS_M_CKDATA : 0),
CFG) => 0;
lfsr_mount(&lfs, LFS_M_RDWR, CFG) => 0;