Skip to content

Commit d0e0453

Browse files
committed
Changed how we write out superblock to use append
Making the superblock look like "just another entry" allows us to treat the superblock like "just another entry" and reuse a decent amount of logic that would otherwise only be used a format and mount time. In this case we can use append to write out the superblock like it was creating a new entry on the filesystem.
1 parent 701e4fa commit d0e0453

File tree

4 files changed

+46
-52
lines changed

4 files changed

+46
-52
lines changed

lfs.c

Lines changed: 43 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -2372,38 +2372,40 @@ int lfs_format(lfs_t *lfs, const struct lfs_config *cfg) {
23722372

23732373
lfs->root[0] = root.pair[0];
23742374
lfs->root[1] = root.pair[1];
2375+
superdir.d.tail[0] = lfs->root[0];
2376+
superdir.d.tail[1] = lfs->root[1];
23752377

2376-
// write superblocks
2377-
lfs_superblock_t superblock = {
2378-
.d.type = LFS_STRUCT_DIR | LFS_TYPE_SUPERBLOCK,
2379-
.d.elen = sizeof(superblock.d) - sizeof(superblock.d.magic) - 4,
2380-
.d.nlen = sizeof(superblock.d.magic),
2381-
.d.version = LFS_DISK_VERSION,
2382-
.d.magic = {"littlefs"},
2383-
.d.block_size = lfs->cfg->block_size,
2384-
.d.block_count = lfs->cfg->block_count,
2385-
.d.root = {lfs->root[0], lfs->root[1]},
2386-
};
2387-
superdir.d.tail[0] = root.pair[0];
2388-
superdir.d.tail[1] = root.pair[1];
2389-
superdir.d.size = sizeof(superdir.d) + sizeof(superblock.d) + 4;
2390-
2391-
// write both pairs to be safe
2378+
// write one superblocks
2379+
lfs_superblock_t superblock;
2380+
superblock.d.version = LFS_DISK_VERSION,
2381+
superblock.d.root[0] = lfs->root[0];
2382+
superblock.d.root[1] = lfs->root[1];
2383+
superblock.d.block_size = lfs->cfg->block_size;
2384+
superblock.d.block_count = lfs->cfg->block_count;
2385+
2386+
lfs_entry_t superentry;
2387+
superentry.d.type = LFS_STRUCT_DIR | LFS_TYPE_SUPERBLOCK;
2388+
superentry.d.elen = sizeof(superblock.d);
2389+
superentry.d.alen = 0;
2390+
superentry.d.nlen = strlen("littlefs");
2391+
superentry.off = sizeof(superdir.d);
2392+
superentry.size = 4 + superentry.d.elen +
2393+
superentry.d.alen + superentry.d.nlen;
2394+
2395+
lfs_entry_tole32(&superentry.d);
23922396
lfs_superblock_tole32(&superblock.d);
2393-
bool valid = false;
2394-
for (int i = 0; i < 2; i++) {
2395-
err = lfs_dir_commit(lfs, &superdir, &(struct lfs_region){
2396-
sizeof(superdir.d), 0,
2397-
lfs_commit_mem, &superblock.d, sizeof(superblock.d)});
2398-
if (err && err != LFS_ERR_CORRUPT) {
2399-
return err;
2400-
}
2401-
2402-
valid = valid || !err;
2403-
}
2404-
2405-
if (!valid) {
2406-
return LFS_ERR_CORRUPT;
2397+
err = lfs_dir_append(lfs, &superdir, &superentry,
2398+
&(struct lfs_region){
2399+
0, +4,
2400+
lfs_commit_mem, &superentry.d, 4,
2401+
&(struct lfs_region){
2402+
0, +sizeof(superblock.d),
2403+
lfs_commit_mem, &superblock.d, sizeof(superblock.d),
2404+
&(struct lfs_region){
2405+
0, +superentry.d.nlen,
2406+
lfs_commit_mem, "littlefs", superentry.d.nlen}}});
2407+
if (err) {
2408+
return err;
24072409
}
24082410

24092411
// sanity check that fetch works
@@ -2412,7 +2414,6 @@ int lfs_format(lfs_t *lfs, const struct lfs_config *cfg) {
24122414
return err;
24132415
}
24142416

2415-
lfs_alloc_ack(lfs);
24162417
return lfs_deinit(lfs);
24172418
}
24182419

@@ -2431,25 +2432,33 @@ int lfs_mount(lfs_t *lfs, const struct lfs_config *cfg) {
24312432
// load superblock
24322433
lfs_dir_t dir;
24332434
lfs_superblock_t superblock;
2435+
char magic[8];
24342436
err = lfs_dir_fetch(lfs, &dir, (const lfs_block_t[2]){0, 1});
24352437
if (err && err != LFS_ERR_CORRUPT) {
24362438
return err;
24372439
}
24382440

24392441
if (!err) {
2440-
err = lfs_bd_read(lfs, dir.pair[0], sizeof(dir.d),
2442+
err = lfs_bd_read(lfs, dir.pair[0], sizeof(dir.d)+4,
24412443
&superblock.d, sizeof(superblock.d));
24422444
lfs_superblock_fromle32(&superblock.d);
24432445
if (err) {
24442446
return err;
24452447
}
24462448

2449+
err = lfs_bd_read(lfs, dir.pair[0],
2450+
sizeof(dir.d) + 4 + sizeof(superblock.d),
2451+
magic, sizeof(magic));
2452+
if (err) {
2453+
return err;
2454+
}
2455+
24472456
lfs->root[0] = superblock.d.root[0];
24482457
lfs->root[1] = superblock.d.root[1];
24492458
}
24502459

2451-
if (err || memcmp(superblock.d.magic, "littlefs", 8) != 0) {
2452-
LFS_ERROR("Invalid superblock at %d %d", 0, 1);
2460+
if (err || memcmp(magic, "littlefs", 8) != 0) {
2461+
LFS_ERROR("Invalid superblock at %d %d", dir.pair[0], dir.pair[1]);
24532462
return LFS_ERR_CORRUPT;
24542463
}
24552464

lfs.h

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -257,15 +257,10 @@ typedef struct lfs_dir {
257257

258258
typedef struct lfs_superblock {
259259
struct lfs_disk_superblock {
260-
uint8_t type;
261-
uint8_t elen;
262-
uint8_t alen;
263-
uint8_t nlen;
264260
lfs_block_t root[2];
265261
uint32_t block_size;
266262
uint32_t block_count;
267263
uint32_t version;
268-
char magic[8];
269264
} d;
270265
} lfs_superblock_t;
271266

tests/test_corrupt.sh

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ lfs_mktree
7373
lfs_chktree
7474

7575
echo "--- Block corruption ---"
76-
for i in {0..33}
76+
for i in {2..33}
7777
do
7878
rm -rf blocks
7979
mkdir blocks
@@ -83,7 +83,7 @@ do
8383
done
8484

8585
echo "--- Block persistance ---"
86-
for i in {0..33}
86+
for i in {2..33}
8787
do
8888
rm -rf blocks
8989
mkdir blocks

tests/test_format.sh

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -30,20 +30,10 @@ echo "--- Invalid mount ---"
3030
tests/test.py << TEST
3131
lfs_format(&lfs, &cfg) => 0;
3232
TEST
33-
rm blocks/0 blocks/1
33+
rm -f blocks/0 blocks/1
3434
tests/test.py << TEST
3535
lfs_mount(&lfs, &cfg) => LFS_ERR_CORRUPT;
3636
TEST
3737

38-
echo "--- Valid corrupt mount ---"
39-
tests/test.py << TEST
40-
lfs_format(&lfs, &cfg) => 0;
41-
TEST
42-
rm blocks/0
43-
tests/test.py << TEST
44-
lfs_mount(&lfs, &cfg) => 0;
45-
lfs_unmount(&lfs) => 0;
46-
TEST
47-
4838
echo "--- Results ---"
4939
tests/stats.py

0 commit comments

Comments
 (0)