Skip to content

Commit fd1da60

Browse files
committed
Added support for handling corrupted blocks
This provides a limited form of wear leveling. While wear is not actually balanced across blocks, the filesystem can recover from corrupted blocks and extend the lifetime of a device nearly as much as dynamic wear leveling. For use-cases where wear is important, it would be better to use a full form of dynamic wear-leveling at the block level. (or consider a logging filesystem). Corrupted block handling was simply added on top of the existing logic in place for the filesystem, so it's a bit more noodly than it may have to be, but it gets the work done.
1 parent b35d761 commit fd1da60

File tree

8 files changed

+612
-289
lines changed

8 files changed

+612
-289
lines changed

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ size: $(OBJ)
3232

3333
.SUFFIXES:
3434
test: test_format test_dirs test_files test_seek test_parallel \
35-
test_alloc test_paths test_orphan
35+
test_alloc test_paths test_orphan test_corrupt
3636
test_%: tests/test_%.sh
3737
./$<
3838

emubd/lfs_emubd.c

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,13 +144,24 @@ int lfs_emubd_prog(const struct lfs_config *cfg, lfs_block_t block,
144144
return -errno;
145145
}
146146

147+
err = fseek(f, off, SEEK_SET);
148+
if (err) {
149+
return -errno;
150+
}
151+
152+
uint8_t dat;
153+
res = fread(&dat, 1, 1, f);
154+
if (res < 1) {
155+
return -errno;
156+
}
157+
147158
err = fclose(f);
148159
if (err) {
149160
return -errno;
150161
}
151162

152163
emu->stats.prog_count += 1;
153-
return 0;
164+
return (dat != data[0]) ? LFS_ERR_CORRUPT : 0;
154165
}
155166

156167
int lfs_emubd_erase(const struct lfs_config *cfg, lfs_block_t block) {

0 commit comments

Comments
 (0)