Fixed mroot death returning LFS_ERR_CORRUPT -> LFS_ERR_NOSPC

It's counter-intuitive, but no top-level API should return
LFS_ERR_CORRUPT. Instead, if we can't make progress because of a corrupt
block, we should return LFS_ERR_NOSPC. This makes it easier for users to
write code that is well behaved even when a device is end-of-life.

It's up to our mroot extension algorithm to make sure this case can't be
reached in normal operation unless the device is _actually_ at
end-of-life.

Because mroot extension is a bit of a special case, we weren't
converting these corrupt errors to nospc errors consistently. This is
fixed now, along with a couple more hopefully-useful logging statements.

Found while playing around with test_exhaustion + block_recycles=-1.
This should assert on bad wear-leveling, but LFS_ERR_CORRUPT was
unexpected. Added an explicit test because this is an easy thing to let
split through:

- test_badblocks_mrootanchor_wear

Code changes were surprisingly minimal, I wonder if constants are being
swapped out somewhere low-level?

           code          stack
  before: 33766           2600
  after:  33770 (+0.0%)   2600 (+0.0%)
This commit is contained in:
Christopher Haster
2024-06-05 00:02:32 -05:00
parent 26f3034813
commit 76ffb0e7b6
2 changed files with 67 additions and 3 deletions
+48 -1
View File
@@ -5095,7 +5095,7 @@ code = '''
## other corner cases
# test formatting with 0 or 1 bad, this should just error
[cases.test_badblocks_mrootanchor]
[cases.test_badblocks_mrootanchor_format]
defines.ERASE_CYCLES = 0xffffffff
defines.BADBLOCKS = [0x1, 0x2, 0x3]
defines.BADBLOCK_BEHAVIOR = [
@@ -5119,6 +5119,53 @@ code = '''
lfsr_format(&lfs, CFG) => LFS_ERR_CORRUPT;
'''
# test blocks 0 or 1 going bad, this should just error
[cases.test_badblocks_mrootanchor_wear]
defines.ERASE_CYCLES = 0xffffffff
defines.BADBLOCKS = [0x1, 0x2]
defines.BADBLOCK_BEHAVIOR = [
'LFS_EMUBD_BADBLOCK_PROGERROR',
'LFS_EMUBD_BADBLOCK_ERASEERROR',
'LFS_EMUBD_BADBLOCK_READERROR',
'LFS_EMUBD_BADBLOCK_PROGNOOP',
'LFS_EMUBD_BADBLOCK_ERASENOOP',
]
# we need prog checking to detect read errors
defines.CHECK_PROGS = 'BADBLOCK_BEHAVIOR >= LFS_EMUBD_BADBLOCK_READERROR'
code = '''
lfs_t lfs;
lfsr_format(&lfs, CFG) => 0;
if (BADBLOCKS & 0x1) {
lfs_emubd_setwear(CFG, 0, 0xffffffff) => 0;
}
if (BADBLOCKS & 0x2) {
lfs_emubd_setwear(CFG, 1, 0xffffffff) => 0;
}
lfsr_mount(&lfs, CFG) => 0;
for (lfs_size_t i = 0;; i++) {
// this should eventually fail
assert(i <= BLOCK_COUNT);
int err = lfsr_mkdir(&lfs, "hi");
assert(!err || err == LFS_ERR_NOSPC);
if (err == LFS_ERR_NOSPC) {
break;
}
err = lfsr_remove(&lfs, "hi");
assert(!err || err == LFS_ERR_NOSPC);
if (err == LFS_ERR_NOSPC) {
break;
}
}
lfsr_unmount(&lfs) => 0;
'''