Skip to content

Commit acad709

Browse files
committed
Squashed 'littlefs/' changes from 130790f..f77214d
f77214d Merge pull request #877 from littlefs-project/devel f91c5bd Bumped minor version to v2.8 0eb52a2 Merge pull request #875 from littlefs-project/fs-gc 6b33ee5 Renamed lfs_fs_findfreeblocks -> lfs_fs_gc, tweaked documentation 63e4408 Extended alloc tests to test some properties of lfs_fs_findfreeblocks dbe4598 Added API boilerplate for lfs_fs_findfreeblocks and consistent style d85a0fe Move lookahead buffer offset at the first free block if such block doesn't exist move it for whole lookahead size. b637379 Update lfs_find_free_blocks to match the latest changes. 1ba4ed0 Merge pull request #872 from littlefs-project/fs-grow e4b7fa1 Merge pull request #866 from BrianPugh/optional-block-count 23505fa Added lfs_fs_grow for growing the filesystem to a different block_count 2c222af Tweaked lfs_fsinfo block_size/block_count fields 127d84b Added a couple mixed/unknown block_count tests 027331b Adopted erase_size/erase_count config in test block-devices/runners 9c23329 Revert of refactor lfs_scan_* out of lfs_format 23089d5 remove previous block_count detection from lfs_format d6098bd Add block_count and block_size to fsinfo d6c0c6a linting 5caa83f forgot to unmount lfs in test; leaking memory 7521e0a fix newly introduced missing cleanup when an invalid superblock is found. 2ebfec7 test for failure when interpretting block count when formatting without superblock 3d0bcf4 Add test_superblocks_mount_unknown_block_count 6de3fc6 fix corruption check df238eb Add a unit test; currently hanging on final permutation. be68122 introduce lfs->block_count. If cfg->block_count is 0, autopopulate from superblock 6dae703 remove redundant superblock check 7328527 refactor lfs_scan_for_state_updates and lfs_scan_for_superblock out of lfs_format git-subtree-dir: littlefs git-subtree-split: f77214d1f0a8ccd7ddc7e1204fedd25ee5a41534
1 parent b708976 commit acad709

16 files changed

+689
-234
lines changed

bd/lfs_emubd.c

+44-61
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ static void lfs_emubd_decblock(lfs_emubd_block_t *block) {
4848
static lfs_emubd_block_t *lfs_emubd_mutblock(
4949
const struct lfs_config *cfg,
5050
lfs_emubd_block_t **block) {
51+
lfs_emubd_t *bd = cfg->context;
5152
lfs_emubd_block_t *block_ = *block;
5253
if (block_ && block_->rc == 1) {
5354
// rc == 1? can modify
@@ -56,13 +57,13 @@ static lfs_emubd_block_t *lfs_emubd_mutblock(
5657
} else if (block_) {
5758
// rc > 1? need to create a copy
5859
lfs_emubd_block_t *nblock = malloc(
59-
sizeof(lfs_emubd_block_t) + cfg->block_size);
60+
sizeof(lfs_emubd_block_t) + bd->cfg->erase_size);
6061
if (!nblock) {
6162
return NULL;
6263
}
6364

6465
memcpy(nblock, block_,
65-
sizeof(lfs_emubd_block_t) + cfg->block_size);
66+
sizeof(lfs_emubd_block_t) + bd->cfg->erase_size);
6667
nblock->rc = 1;
6768

6869
lfs_emubd_decblock(block_);
@@ -72,7 +73,7 @@ static lfs_emubd_block_t *lfs_emubd_mutblock(
7273
} else {
7374
// no block? need to allocate
7475
lfs_emubd_block_t *nblock = malloc(
75-
sizeof(lfs_emubd_block_t) + cfg->block_size);
76+
sizeof(lfs_emubd_block_t) + bd->cfg->erase_size);
7677
if (!nblock) {
7778
return NULL;
7879
}
@@ -81,10 +82,9 @@ static lfs_emubd_block_t *lfs_emubd_mutblock(
8182
nblock->wear = 0;
8283

8384
// zero for consistency
84-
lfs_emubd_t *bd = cfg->context;
8585
memset(nblock->data,
8686
(bd->cfg->erase_value != -1) ? bd->cfg->erase_value : 0,
87-
cfg->block_size);
87+
bd->cfg->erase_size);
8888

8989
*block = nblock;
9090
return nblock;
@@ -94,35 +94,35 @@ static lfs_emubd_block_t *lfs_emubd_mutblock(
9494

9595
// emubd create/destroy
9696

97-
int lfs_emubd_createcfg(const struct lfs_config *cfg, const char *path,
97+
int lfs_emubd_create(const struct lfs_config *cfg,
9898
const struct lfs_emubd_config *bdcfg) {
99-
LFS_EMUBD_TRACE("lfs_emubd_createcfg(%p {.context=%p, "
100-
".read=%p, .prog=%p, .erase=%p, .sync=%p, "
101-
".read_size=%"PRIu32", .prog_size=%"PRIu32", "
102-
".block_size=%"PRIu32", .block_count=%"PRIu32"}, "
103-
"\"%s\", "
104-
"%p {.erase_value=%"PRId32", .erase_cycles=%"PRIu32", "
99+
LFS_EMUBD_TRACE("lfs_emubd_create(%p {.context=%p, "
100+
".read=%p, .prog=%p, .erase=%p, .sync=%p}, "
101+
"%p {.read_size=%"PRIu32", .prog_size=%"PRIu32", "
102+
".erase_size=%"PRIu32", .erase_count=%"PRIu32", "
103+
".erase_value=%"PRId32", .erase_cycles=%"PRIu32", "
105104
".badblock_behavior=%"PRIu8", .power_cycles=%"PRIu32", "
106105
".powerloss_behavior=%"PRIu8", .powerloss_cb=%p, "
107106
".powerloss_data=%p, .track_branches=%d})",
108107
(void*)cfg, cfg->context,
109108
(void*)(uintptr_t)cfg->read, (void*)(uintptr_t)cfg->prog,
110109
(void*)(uintptr_t)cfg->erase, (void*)(uintptr_t)cfg->sync,
111-
cfg->read_size, cfg->prog_size, cfg->block_size, cfg->block_count,
112-
path, (void*)bdcfg, bdcfg->erase_value, bdcfg->erase_cycles,
110+
(void*)bdcfg,
111+
bdcfg->read_size, bdcfg->prog_size, bdcfg->erase_size,
112+
bdcfg->erase_count, bdcfg->erase_value, bdcfg->erase_cycles,
113113
bdcfg->badblock_behavior, bdcfg->power_cycles,
114114
bdcfg->powerloss_behavior, (void*)(uintptr_t)bdcfg->powerloss_cb,
115115
bdcfg->powerloss_data, bdcfg->track_branches);
116116
lfs_emubd_t *bd = cfg->context;
117117
bd->cfg = bdcfg;
118118

119119
// allocate our block array, all blocks start as uninitialized
120-
bd->blocks = malloc(cfg->block_count * sizeof(lfs_emubd_block_t*));
120+
bd->blocks = malloc(bd->cfg->erase_count * sizeof(lfs_emubd_block_t*));
121121
if (!bd->blocks) {
122-
LFS_EMUBD_TRACE("lfs_emubd_createcfg -> %d", LFS_ERR_NOMEM);
122+
LFS_EMUBD_TRACE("lfs_emubd_create -> %d", LFS_ERR_NOMEM);
123123
return LFS_ERR_NOMEM;
124124
}
125-
memset(bd->blocks, 0, cfg->block_count * sizeof(lfs_emubd_block_t*));
125+
memset(bd->blocks, 0, bd->cfg->erase_count * sizeof(lfs_emubd_block_t*));
126126

127127
// setup testing things
128128
bd->readed = 0;
@@ -134,7 +134,7 @@ int lfs_emubd_createcfg(const struct lfs_config *cfg, const char *path,
134134
if (bd->cfg->disk_path) {
135135
bd->disk = malloc(sizeof(lfs_emubd_disk_t));
136136
if (!bd->disk) {
137-
LFS_EMUBD_TRACE("lfs_emubd_createcfg -> %d", LFS_ERR_NOMEM);
137+
LFS_EMUBD_TRACE("lfs_emubd_create -> %d", LFS_ERR_NOMEM);
138138
return LFS_ERR_NOMEM;
139139
}
140140
bd->disk->rc = 1;
@@ -156,21 +156,21 @@ int lfs_emubd_createcfg(const struct lfs_config *cfg, const char *path,
156156
// if we're emulating erase values, we can keep a block around in
157157
// memory of just the erase state to speed up emulated erases
158158
if (bd->cfg->erase_value != -1) {
159-
bd->disk->scratch = malloc(cfg->block_size);
159+
bd->disk->scratch = malloc(bd->cfg->erase_size);
160160
if (!bd->disk->scratch) {
161-
LFS_EMUBD_TRACE("lfs_emubd_createcfg -> %d", LFS_ERR_NOMEM);
161+
LFS_EMUBD_TRACE("lfs_emubd_create -> %d", LFS_ERR_NOMEM);
162162
return LFS_ERR_NOMEM;
163163
}
164164
memset(bd->disk->scratch,
165165
bd->cfg->erase_value,
166-
cfg->block_size);
166+
bd->cfg->erase_size);
167167

168168
// go ahead and erase all of the disk, otherwise the file will not
169169
// match our internal representation
170-
for (size_t i = 0; i < cfg->block_count; i++) {
170+
for (size_t i = 0; i < bd->cfg->erase_count; i++) {
171171
ssize_t res = write(bd->disk->fd,
172172
bd->disk->scratch,
173-
cfg->block_size);
173+
bd->cfg->erase_size);
174174
if (res < 0) {
175175
int err = -errno;
176176
LFS_EMUBD_TRACE("lfs_emubd_create -> %d", err);
@@ -180,33 +180,16 @@ int lfs_emubd_createcfg(const struct lfs_config *cfg, const char *path,
180180
}
181181
}
182182

183-
LFS_EMUBD_TRACE("lfs_emubd_createcfg -> %d", 0);
183+
LFS_EMUBD_TRACE("lfs_emubd_create -> %d", 0);
184184
return 0;
185185
}
186186

187-
int lfs_emubd_create(const struct lfs_config *cfg, const char *path) {
188-
LFS_EMUBD_TRACE("lfs_emubd_create(%p {.context=%p, "
189-
".read=%p, .prog=%p, .erase=%p, .sync=%p, "
190-
".read_size=%"PRIu32", .prog_size=%"PRIu32", "
191-
".block_size=%"PRIu32", .block_count=%"PRIu32"}, "
192-
"\"%s\")",
193-
(void*)cfg, cfg->context,
194-
(void*)(uintptr_t)cfg->read, (void*)(uintptr_t)cfg->prog,
195-
(void*)(uintptr_t)cfg->erase, (void*)(uintptr_t)cfg->sync,
196-
cfg->read_size, cfg->prog_size, cfg->block_size, cfg->block_count,
197-
path);
198-
static const struct lfs_emubd_config defaults = {.erase_value=-1};
199-
int err = lfs_emubd_createcfg(cfg, path, &defaults);
200-
LFS_EMUBD_TRACE("lfs_emubd_create -> %d", err);
201-
return err;
202-
}
203-
204187
int lfs_emubd_destroy(const struct lfs_config *cfg) {
205188
LFS_EMUBD_TRACE("lfs_emubd_destroy(%p)", (void*)cfg);
206189
lfs_emubd_t *bd = cfg->context;
207190

208191
// decrement reference counts
209-
for (lfs_block_t i = 0; i < cfg->block_count; i++) {
192+
for (lfs_block_t i = 0; i < bd->cfg->erase_count; i++) {
210193
lfs_emubd_decblock(bd->blocks[i]);
211194
}
212195
free(bd->blocks);
@@ -237,10 +220,10 @@ int lfs_emubd_read(const struct lfs_config *cfg, lfs_block_t block,
237220
lfs_emubd_t *bd = cfg->context;
238221

239222
// check if read is valid
240-
LFS_ASSERT(block < cfg->block_count);
241-
LFS_ASSERT(off % cfg->read_size == 0);
242-
LFS_ASSERT(size % cfg->read_size == 0);
243-
LFS_ASSERT(off+size <= cfg->block_size);
223+
LFS_ASSERT(block < bd->cfg->erase_count);
224+
LFS_ASSERT(off % bd->cfg->read_size == 0);
225+
LFS_ASSERT(size % bd->cfg->read_size == 0);
226+
LFS_ASSERT(off+size <= bd->cfg->erase_size);
244227

245228
// get the block
246229
const lfs_emubd_block_t *b = bd->blocks[block];
@@ -287,10 +270,10 @@ int lfs_emubd_prog(const struct lfs_config *cfg, lfs_block_t block,
287270
lfs_emubd_t *bd = cfg->context;
288271

289272
// check if write is valid
290-
LFS_ASSERT(block < cfg->block_count);
291-
LFS_ASSERT(off % cfg->prog_size == 0);
292-
LFS_ASSERT(size % cfg->prog_size == 0);
293-
LFS_ASSERT(off+size <= cfg->block_size);
273+
LFS_ASSERT(block < bd->cfg->erase_count);
274+
LFS_ASSERT(off % bd->cfg->prog_size == 0);
275+
LFS_ASSERT(size % bd->cfg->prog_size == 0);
276+
LFS_ASSERT(off+size <= bd->cfg->erase_size);
294277

295278
// get the block
296279
lfs_emubd_block_t *b = lfs_emubd_mutblock(cfg, &bd->blocks[block]);
@@ -327,7 +310,7 @@ int lfs_emubd_prog(const struct lfs_config *cfg, lfs_block_t block,
327310
// mirror to disk file?
328311
if (bd->disk) {
329312
off_t res1 = lseek(bd->disk->fd,
330-
(off_t)block*cfg->block_size + (off_t)off,
313+
(off_t)block*bd->cfg->erase_size + (off_t)off,
331314
SEEK_SET);
332315
if (res1 < 0) {
333316
int err = -errno;
@@ -372,11 +355,11 @@ int lfs_emubd_prog(const struct lfs_config *cfg, lfs_block_t block,
372355

373356
int lfs_emubd_erase(const struct lfs_config *cfg, lfs_block_t block) {
374357
LFS_EMUBD_TRACE("lfs_emubd_erase(%p, 0x%"PRIx32" (%"PRIu32"))",
375-
(void*)cfg, block, cfg->block_size);
358+
(void*)cfg, block, ((lfs_emubd_t*)cfg->context)->cfg->erase_size);
376359
lfs_emubd_t *bd = cfg->context;
377360

378361
// check if erase is valid
379-
LFS_ASSERT(block < cfg->block_count);
362+
LFS_ASSERT(block < bd->cfg->erase_count);
380363

381364
// get the block
382365
lfs_emubd_block_t *b = lfs_emubd_mutblock(cfg, &bd->blocks[block]);
@@ -405,12 +388,12 @@ int lfs_emubd_erase(const struct lfs_config *cfg, lfs_block_t block) {
405388

406389
// emulate an erase value?
407390
if (bd->cfg->erase_value != -1) {
408-
memset(b->data, bd->cfg->erase_value, cfg->block_size);
391+
memset(b->data, bd->cfg->erase_value, bd->cfg->erase_size);
409392

410393
// mirror to disk file?
411394
if (bd->disk) {
412395
off_t res1 = lseek(bd->disk->fd,
413-
(off_t)block*cfg->block_size,
396+
(off_t)block*bd->cfg->erase_size,
414397
SEEK_SET);
415398
if (res1 < 0) {
416399
int err = -errno;
@@ -420,7 +403,7 @@ int lfs_emubd_erase(const struct lfs_config *cfg, lfs_block_t block) {
420403

421404
ssize_t res2 = write(bd->disk->fd,
422405
bd->disk->scratch,
423-
cfg->block_size);
406+
bd->cfg->erase_size);
424407
if (res2 < 0) {
425408
int err = -errno;
426409
LFS_EMUBD_TRACE("lfs_emubd_erase -> %d", err);
@@ -430,7 +413,7 @@ int lfs_emubd_erase(const struct lfs_config *cfg, lfs_block_t block) {
430413
}
431414

432415
// track erases
433-
bd->erased += cfg->block_size;
416+
bd->erased += bd->cfg->erase_size;
434417
if (bd->cfg->erase_sleep) {
435418
int err = nanosleep(&(struct timespec){
436419
.tv_sec=bd->cfg->erase_sleep/1000000000,
@@ -573,7 +556,7 @@ lfs_emubd_swear_t lfs_emubd_wear(const struct lfs_config *cfg,
573556
lfs_emubd_t *bd = cfg->context;
574557

575558
// check if block is valid
576-
LFS_ASSERT(block < cfg->block_count);
559+
LFS_ASSERT(block < bd->cfg->erase_count);
577560

578561
// get the wear
579562
lfs_emubd_wear_t wear;
@@ -595,7 +578,7 @@ int lfs_emubd_setwear(const struct lfs_config *cfg,
595578
lfs_emubd_t *bd = cfg->context;
596579

597580
// check if block is valid
598-
LFS_ASSERT(block < cfg->block_count);
581+
LFS_ASSERT(block < bd->cfg->erase_count);
599582

600583
// set the wear
601584
lfs_emubd_block_t *b = lfs_emubd_mutblock(cfg, &bd->blocks[block]);
@@ -635,13 +618,13 @@ int lfs_emubd_copy(const struct lfs_config *cfg, lfs_emubd_t *copy) {
635618
lfs_emubd_t *bd = cfg->context;
636619

637620
// lazily copy over our block array
638-
copy->blocks = malloc(cfg->block_count * sizeof(lfs_emubd_block_t*));
621+
copy->blocks = malloc(bd->cfg->erase_count * sizeof(lfs_emubd_block_t*));
639622
if (!copy->blocks) {
640623
LFS_EMUBD_TRACE("lfs_emubd_copy -> %d", LFS_ERR_NOMEM);
641624
return LFS_ERR_NOMEM;
642625
}
643626

644-
for (size_t i = 0; i < cfg->block_count; i++) {
627+
for (size_t i = 0; i < bd->cfg->erase_count; i++) {
645628
copy->blocks[i] = lfs_emubd_incblock(bd->blocks[i]);
646629
}
647630

bd/lfs_emubd.h

+13-5
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,18 @@ typedef int64_t lfs_emubd_ssleep_t;
6767

6868
// emubd config, this is required for testing
6969
struct lfs_emubd_config {
70+
// Minimum size of a read operation in bytes.
71+
lfs_size_t read_size;
72+
73+
// Minimum size of a program operation in bytes.
74+
lfs_size_t prog_size;
75+
76+
// Size of an erase operation in bytes.
77+
lfs_size_t erase_size;
78+
79+
// Number of erase blocks on the device.
80+
lfs_size_t erase_count;
81+
7082
// 8-bit erase value to use for simulating erases. -1 does not simulate
7183
// erases, which can speed up testing by avoiding the extra block-device
7284
// operations to store the erase value.
@@ -149,11 +161,7 @@ typedef struct lfs_emubd {
149161
/// Block device API ///
150162

151163
// Create an emulating block device using the geometry in lfs_config
152-
//
153-
// Note that filebd is used if a path is provided, if path is NULL
154-
// emubd will use rambd which can be much faster.
155-
int lfs_emubd_create(const struct lfs_config *cfg, const char *path);
156-
int lfs_emubd_createcfg(const struct lfs_config *cfg, const char *path,
164+
int lfs_emubd_create(const struct lfs_config *cfg,
157165
const struct lfs_emubd_config *bdcfg);
158166

159167
// Clean up memory associated with block device

0 commit comments

Comments
 (0)