Skip to content

Commit 3778759

Browse files
committed
Squashed 'littlefs/' changes from 2ab150c..3f31c8c
3f31c8c Fixed corner case with immediate exhaustion and lookahead==block_count f4aeb83 Fixed issue with aggressively rounding down lookahead configuration db51a39 Removed stray newline in LFS_ERROR for version git-subtree-dir: littlefs git-subtree-split: 3f31c8c
1 parent 0171b57 commit 3778759

File tree

3 files changed

+15
-16
lines changed

3 files changed

+15
-16
lines changed

.travis.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ script:
1515
- CFLAGS="-DLFS_READ_SIZE=1 -DLFS_PROG_SIZE=1" make test
1616
- CFLAGS="-DLFS_READ_SIZE=512 -DLFS_PROG_SIZE=512" make test
1717
- CFLAGS="-DLFS_BLOCK_COUNT=1023" make test
18-
- CFLAGS="-DLFS_LOOKAHEAD=2047" make test
18+
- CFLAGS="-DLFS_LOOKAHEAD=2048" make test
1919

2020
# self-host with littlefs-fuse for fuzz test
2121
- make -C littlefs-fuse

lfs.c

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -278,7 +278,7 @@ static int lfs_alloc_lookahead(void *p, lfs_block_t block) {
278278
% (lfs_soff_t)(lfs->cfg->block_count))
279279
+ lfs->cfg->block_count) % lfs->cfg->block_count;
280280

281-
if (off < lfs->free.lookahead) {
281+
if (off < lfs->cfg->lookahead) {
282282
lfs->free.buffer[off / 32] |= 1U << (off % 32);
283283
}
284284

@@ -294,7 +294,8 @@ static int lfs_alloc(lfs_t *lfs, lfs_block_t *block) {
294294
return LFS_ERR_NOSPC;
295295
}
296296

297-
if (lfs->free.off >= lfs->free.lookahead) {
297+
if (lfs->free.off >= lfs_min(
298+
lfs->cfg->lookahead, lfs->cfg->block_count)) {
298299
break;
299300
}
300301

@@ -308,11 +309,11 @@ static int lfs_alloc(lfs_t *lfs, lfs_block_t *block) {
308309
}
309310
}
310311

311-
lfs->free.begin += lfs->free.lookahead;
312+
lfs->free.begin += lfs_min(lfs->cfg->lookahead, lfs->cfg->block_count);
312313
lfs->free.off = 0;
313314

314315
// find mask of free blocks from tree
315-
memset(lfs->free.buffer, 0, lfs->free.lookahead/8);
316+
memset(lfs->free.buffer, 0, lfs->cfg->lookahead/8);
316317
int err = lfs_traverse(lfs, lfs_alloc_lookahead, lfs);
317318
if (err) {
318319
return err;
@@ -1870,13 +1871,12 @@ static int lfs_init(lfs_t *lfs, const struct lfs_config *cfg) {
18701871
}
18711872

18721873
// setup lookahead, round down to nearest 32-bits
1873-
lfs->free.lookahead = lfs_min(lfs->cfg->lookahead, lfs->cfg->block_count);
1874-
lfs->free.lookahead = 32 * (lfs->free.lookahead / 32);
1875-
assert(lfs->free.lookahead > 0);
1874+
assert(lfs->cfg->lookahead % 32 == 0);
1875+
assert(lfs->cfg->lookahead > 0);
18761876
if (lfs->cfg->lookahead_buffer) {
18771877
lfs->free.buffer = lfs->cfg->lookahead_buffer;
18781878
} else {
1879-
lfs->free.buffer = malloc(lfs->free.lookahead/8);
1879+
lfs->free.buffer = malloc(lfs->cfg->lookahead/8);
18801880
if (!lfs->free.buffer) {
18811881
return LFS_ERR_NOMEM;
18821882
}
@@ -1919,10 +1919,10 @@ int lfs_format(lfs_t *lfs, const struct lfs_config *cfg) {
19191919
}
19201920

19211921
// create free lookahead
1922-
memset(lfs->free.buffer, 0, lfs->free.lookahead/8);
1922+
memset(lfs->free.buffer, 0, lfs->cfg->lookahead/8);
19231923
lfs->free.begin = 0;
19241924
lfs->free.off = 0;
1925-
lfs->free.end = lfs->free.begin + lfs->cfg->block_count;
1925+
lfs->free.end = lfs->free.begin + lfs->free.off + lfs->cfg->block_count;
19261926

19271927
// create superblock dir
19281928
lfs_alloc_ack(lfs);
@@ -1998,9 +1998,9 @@ int lfs_mount(lfs_t *lfs, const struct lfs_config *cfg) {
19981998
}
19991999

20002000
// setup free lookahead
2001-
lfs->free.begin = -lfs->free.lookahead;
2002-
lfs->free.off = lfs->free.lookahead;
2003-
lfs->free.end = lfs->free.begin + lfs->cfg->block_count;
2001+
lfs->free.begin = -lfs->cfg->lookahead;
2002+
lfs->free.off = lfs->cfg->lookahead;
2003+
lfs->free.end = lfs->free.begin + lfs->free.off + lfs->cfg->block_count;
20042004

20052005
// load superblock
20062006
lfs_dir_t dir;
@@ -2027,7 +2027,7 @@ int lfs_mount(lfs_t *lfs, const struct lfs_config *cfg) {
20272027
}
20282028

20292029
if (superblock.d.version > (0x00010001 | 0x0000ffff)) {
2030-
LFS_ERROR("Invalid version %d.%d\n",
2030+
LFS_ERROR("Invalid version %d.%d",
20312031
0xffff & (superblock.d.version >> 16),
20322032
0xffff & (superblock.d.version >> 0));
20332033
return LFS_ERR_INVAL;

lfs.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -236,7 +236,6 @@ typedef struct lfs_superblock {
236236
} lfs_superblock_t;
237237

238238
typedef struct lfs_free {
239-
lfs_size_t lookahead;
240239
lfs_block_t begin;
241240
lfs_block_t end;
242241
lfs_block_t off;

0 commit comments

Comments
 (0)