Added on-mount traversal flags: LFS_M_MKCONSISTENT/CKMETA/CKDATA/etc
These tell littlefs to do the relevant gc work during mount, which may
be more convenient than calling lfsr_mount and then lfsr_fs_gc.
It also implicitly tears down the filesystem on error, which you can
imagine would be quite useful for LFS_M_CKMETA/LFS_M_CKDATA.
Some flags are more useful here than other (is LFS_M_LOOKAHEAD/COMPACT
really useful?), but since we just pass these directly to our traversal
APIs, we might as well support all of them for consistency.
Also note that since these only change mount's behavior, and have no
effect on the rest of the filesystem, these LFS_M_* flags don't have
related LFS_I_* flags and are not returned by lfsr_fs_stat.
---
This added quite a chunk of code, considering that this is entirely for
convenience:
code stack
before: 35932 2680
after: 36280 (+1.0%) 2680 (+0.0%)
But I think this is mostly because our low-level traversal state is
relatively costly to manage. It may be possible to deduplicate this a
bit better...
This commit is contained in:
@@ -1127,6 +1127,209 @@ code = '''
|
||||
'''
|
||||
|
||||
|
||||
# test we can detect at least fully clobbered blocks
|
||||
[cases.test_gc_ckmeta]
|
||||
defines.GC_STEPS = [-1, 1, 2, 10, 100, 1000]
|
||||
defines.N = [1, 2, 4, 8, 16, 32, 64]
|
||||
defines.SIZE = [
|
||||
'0',
|
||||
'FILE_BUFFER_SIZE/2',
|
||||
'2*FILE_BUFFER_SIZE',
|
||||
'BLOCK_SIZE/2',
|
||||
'BLOCK_SIZE',
|
||||
'2*BLOCK_SIZE',
|
||||
'8*BLOCK_SIZE',
|
||||
]
|
||||
if = '(SIZE*N)/BLOCK_SIZE <= 32'
|
||||
code = '''
|
||||
lfs_block_t i = 0;
|
||||
while (true) {
|
||||
// a bit hacky, but this catches infinite loops
|
||||
assert(i < 2*BLOCK_COUNT);
|
||||
|
||||
lfs_t lfs;
|
||||
lfsr_format(&lfs, CFG) => 0;
|
||||
lfsr_mount(&lfs, LFS_M_RDWR, CFG) => 0;
|
||||
|
||||
// create an interesting filesystem
|
||||
uint32_t prng = 42;
|
||||
for (lfs_size_t i = 0; i < N; i++) {
|
||||
char name[256];
|
||||
sprintf(name, "squid%03x", i);
|
||||
|
||||
uint8_t wbuf[SIZE];
|
||||
for (lfs_size_t j = 0; j < SIZE; j++) {
|
||||
wbuf[j] = 'a' + (TEST_PRNG(&prng) % 26);
|
||||
}
|
||||
|
||||
lfsr_file_t file;
|
||||
lfsr_file_open(&lfs, &file, name,
|
||||
LFS_O_WRONLY | LFS_O_CREAT | LFS_O_EXCL) => 0;
|
||||
lfsr_file_write(&lfs, &file, wbuf, SIZE) => SIZE;
|
||||
lfsr_file_close(&lfs, &file) => 0;
|
||||
}
|
||||
|
||||
// traverse to find blocks
|
||||
lfsr_traversal_t t;
|
||||
lfsr_traversal_open(&lfs, &t, 0) => 0;
|
||||
lfs_block_t k = 0;
|
||||
for (lfs_block_t j = 0;; j++) {
|
||||
assert(j < 2*BLOCK_COUNT);
|
||||
|
||||
struct lfs_tinfo tinfo;
|
||||
int err = lfsr_traversal_read(&lfs, &t, &tinfo);
|
||||
assert(!err || err == LFS_ERR_NOENT);
|
||||
if (err == LFS_ERR_NOENT) {
|
||||
lfsr_traversal_close(&lfs, &t) => 0;
|
||||
lfsr_unmount(&lfs) => 0;
|
||||
goto done;
|
||||
}
|
||||
|
||||
// this gets a bit tricky be cause we need to clobber both
|
||||
// blocks in mdir pairs
|
||||
if (tinfo.btype == LFS_BTYPE_MDIR
|
||||
|| tinfo.btype == LFS_BTYPE_BTREE) {
|
||||
if (k == i || k == i+1) {
|
||||
// clobber this block
|
||||
printf("clobbering 0x%x\n", tinfo.block);
|
||||
uint8_t clobber_buf[BLOCK_SIZE];
|
||||
memset(clobber_buf, 0xcc, BLOCK_SIZE);
|
||||
CFG->erase(CFG, tinfo.block) => 0;
|
||||
CFG->prog(CFG, tinfo.block, 0,
|
||||
clobber_buf, BLOCK_SIZE) => 0;
|
||||
if (tinfo.btype != LFS_BTYPE_MDIR || k == i+1) {
|
||||
i += (tinfo.btype == LFS_BTYPE_MDIR) ? 2 : 1;
|
||||
lfsr_traversal_close(&lfs, &t) => 0;
|
||||
goto clobbered;
|
||||
}
|
||||
}
|
||||
k += 1;
|
||||
}
|
||||
}
|
||||
|
||||
clobbered:;
|
||||
// running lfsr_fs_gc should eventually find the clobbered block
|
||||
while (true) {
|
||||
int err = lfsr_fs_gc(&lfs, LFS_GC_CKMETA);
|
||||
assert(!err || err == LFS_ERR_CORRUPT);
|
||||
// found it
|
||||
if (err == LFS_ERR_CORRUPT) {
|
||||
break;
|
||||
}
|
||||
|
||||
// we should find the clobbered block before finishing the
|
||||
// traversal
|
||||
assert(lfs.omdirs == &lfs.gc.o.o);
|
||||
}
|
||||
|
||||
lfsr_unmount(&lfs) => 0;
|
||||
}
|
||||
done:;
|
||||
'''
|
||||
|
||||
[cases.test_gc_ckdata]
|
||||
defines.GC_STEPS = [-1, 1, 2, 10, 100, 1000]
|
||||
defines.N = [1, 2, 4, 8, 16, 32, 64]
|
||||
defines.SIZE = [
|
||||
'0',
|
||||
'FILE_BUFFER_SIZE/2',
|
||||
'2*FILE_BUFFER_SIZE',
|
||||
'BLOCK_SIZE/2',
|
||||
'BLOCK_SIZE',
|
||||
'2*BLOCK_SIZE',
|
||||
'8*BLOCK_SIZE',
|
||||
]
|
||||
if = '(SIZE*N)/BLOCK_SIZE <= 32'
|
||||
code = '''
|
||||
lfs_block_t i = 0;
|
||||
while (true) {
|
||||
// a bit hacky, but this catches infinite loops
|
||||
assert(i < 2*BLOCK_COUNT);
|
||||
|
||||
lfs_t lfs;
|
||||
lfsr_format(&lfs, CFG) => 0;
|
||||
lfsr_mount(&lfs, LFS_M_RDWR, CFG) => 0;
|
||||
|
||||
// create an interesting filesystem
|
||||
uint32_t prng = 42;
|
||||
for (lfs_size_t i = 0; i < N; i++) {
|
||||
char name[256];
|
||||
sprintf(name, "squid%03x", i);
|
||||
|
||||
uint8_t wbuf[SIZE];
|
||||
for (lfs_size_t j = 0; j < SIZE; j++) {
|
||||
wbuf[j] = 'a' + (TEST_PRNG(&prng) % 26);
|
||||
}
|
||||
|
||||
lfsr_file_t file;
|
||||
lfsr_file_open(&lfs, &file, name,
|
||||
LFS_O_WRONLY | LFS_O_CREAT | LFS_O_EXCL) => 0;
|
||||
lfsr_file_write(&lfs, &file, wbuf, SIZE) => SIZE;
|
||||
lfsr_file_close(&lfs, &file) => 0;
|
||||
}
|
||||
|
||||
// traverse to find blocks
|
||||
lfsr_traversal_t t;
|
||||
lfsr_traversal_open(&lfs, &t, 0) => 0;
|
||||
lfs_block_t k = 0;
|
||||
for (lfs_block_t j = 0;; j++) {
|
||||
assert(j < 2*BLOCK_COUNT);
|
||||
|
||||
struct lfs_tinfo tinfo;
|
||||
int err = lfsr_traversal_read(&lfs, &t, &tinfo);
|
||||
assert(!err || err == LFS_ERR_NOENT);
|
||||
if (err == LFS_ERR_NOENT) {
|
||||
lfsr_traversal_close(&lfs, &t) => 0;
|
||||
lfsr_unmount(&lfs) => 0;
|
||||
goto done;
|
||||
}
|
||||
|
||||
// this gets a bit tricky be cause we need to clobber both
|
||||
// blocks in mdir pairs
|
||||
if (tinfo.btype == LFS_BTYPE_MDIR
|
||||
|| tinfo.btype == LFS_BTYPE_BTREE
|
||||
|| tinfo.btype == LFS_BTYPE_DATA) {
|
||||
if (k == i || k == i+1) {
|
||||
// clobber this block
|
||||
printf("clobbering 0x%x\n", tinfo.block);
|
||||
uint8_t clobber_buf[BLOCK_SIZE];
|
||||
memset(clobber_buf, 0xcc, BLOCK_SIZE);
|
||||
CFG->erase(CFG, tinfo.block) => 0;
|
||||
CFG->prog(CFG, tinfo.block, 0,
|
||||
clobber_buf, BLOCK_SIZE) => 0;
|
||||
if (tinfo.btype != LFS_BTYPE_MDIR || k == i+1) {
|
||||
i += (tinfo.btype == LFS_BTYPE_MDIR) ? 2 : 1;
|
||||
lfsr_traversal_close(&lfs, &t) => 0;
|
||||
goto clobbered;
|
||||
}
|
||||
}
|
||||
k += 1;
|
||||
}
|
||||
}
|
||||
|
||||
clobbered:;
|
||||
// running lfsr_fs_gc should eventually find the clobbered block
|
||||
//
|
||||
// note LFS_GC_CKDATA implies LFS_GC_CKMETA
|
||||
while (true) {
|
||||
int err = lfsr_fs_gc(&lfs, LFS_GC_CKDATA);
|
||||
assert(!err || err == LFS_ERR_CORRUPT);
|
||||
// found it
|
||||
if (err == LFS_ERR_CORRUPT) {
|
||||
break;
|
||||
}
|
||||
|
||||
// we should find the clobbered block before finishing the
|
||||
// traversal
|
||||
assert(lfs.omdirs == &lfs.gc.o.o);
|
||||
}
|
||||
|
||||
lfsr_unmount(&lfs) => 0;
|
||||
}
|
||||
done:;
|
||||
'''
|
||||
|
||||
|
||||
# pseudo-fuzz test that dirtying still works with the GC API
|
||||
[cases.test_gc_mutation]
|
||||
defines.GC_STEPS = [-1, 1, 2, 10, 100, 1000]
|
||||
|
||||
Reference in New Issue
Block a user