Skip to content

Commit 041e90a

Browse files
committed
Added handling for corrupt as initial state of blocks
Before this, littlefs incorrectly assumed corrupt blocks were only the result of our own modification. This would be fine for most cases of freshly erased storage, but for storage with block-level ECC this wasn't always true. Fortunately, it's quite easy for littlefs to handle this case correctly, as long as corrupt storage always reports that it is corrupt, which for most forms of ECC is the case unless we perform a write on the storage. found by rojer
1 parent f94d233 commit 041e90a

File tree

1 file changed

+11
-2
lines changed

1 file changed

+11
-2
lines changed

lfs.c

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -417,11 +417,14 @@ static int lfs_dir_alloc(lfs_t *lfs, lfs_dir_t *dir) {
417417
// rather than clobbering one of the blocks we just pretend
418418
// the revision may be valid
419419
int err = lfs_bd_read(lfs, dir->pair[0], 0, &dir->d.rev, 4);
420-
dir->d.rev = lfs_fromle32(dir->d.rev);
421-
if (err) {
420+
if (err && err != LFS_ERR_CORRUPT) {
422421
return err;
423422
}
424423

424+
if (err != LFS_ERR_CORRUPT) {
425+
dir->d.rev = lfs_fromle32(dir->d.rev);
426+
}
427+
425428
// set defaults
426429
dir->d.rev += 1;
427430
dir->d.size = sizeof(dir->d)+4;
@@ -445,6 +448,9 @@ static int lfs_dir_fetch(lfs_t *lfs,
445448
int err = lfs_bd_read(lfs, tpair[i], 0, &test, sizeof(test));
446449
lfs_dir_fromle32(&test);
447450
if (err) {
451+
if (err == LFS_ERR_CORRUPT) {
452+
continue;
453+
}
448454
return err;
449455
}
450456

@@ -464,6 +470,9 @@ static int lfs_dir_fetch(lfs_t *lfs,
464470
err = lfs_bd_crc(lfs, tpair[i], sizeof(test),
465471
(0x7fffffff & test.size) - sizeof(test), &crc);
466472
if (err) {
473+
if (err == LFS_ERR_CORRUPT) {
474+
continue;
475+
}
467476
return err;
468477
}
469478

0 commit comments

Comments
 (0)