Skip to content

Commit 785b0b4

Browse files
committed
Fixed issue with aggressively rounding down lookahead configuration
The littlefs allows buffers to be passed statically in the case that a system does not have a heap. Unfortunately, this means we can't round up in the case of an unaligned lookahead buffer. Double unfortunately, rounding down after clamping to the block device size could result in a lookahead of zero for block devices < 32 blocks large. The assert in littlefs does catch this case, but rounding down prevents support for < 32 block devices. The solution is to simply require a 32-bit aligned buffer with an assert. This avoids runtime problems while allowing a user to pass in the correct buffer for < 32 block devices. Rounding up can be handled at higher API levels.
1 parent 71b429b commit 785b0b4

File tree

5 files changed

+15
-16
lines changed

5 files changed

+15
-16
lines changed

.travis.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ script:
2323
make -Clittlefs test
2424
- CFLAGS="-Wno-error=format -DLFS_BLOCK_COUNT=1023"
2525
make -Clittlefs test
26-
- CFLAGS="-Wno-error=format -DLFS_LOOKAHEAD=2047"
26+
- CFLAGS="-Wno-error=format -DLFS_LOOKAHEAD=2048"
2727
make -Clittlefs test
2828

2929
# Self-host with littlefs-fuse for fuzz test

LittleFileSystem.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ int LittleFileSystem::mount(BlockDevice *bd) {
156156
_config.block_size = _block_size;
157157
}
158158
_config.block_count = bd->size() / _config.block_size;
159-
_config.lookahead = _config.block_count - _config.block_count % 32;
159+
_config.lookahead = 32 * ((_config.block_count+31)/32);
160160
if (_config.lookahead > _lookahead) {
161161
_config.lookahead = _lookahead;
162162
}
@@ -226,7 +226,7 @@ int LittleFileSystem::format(BlockDevice *bd,
226226
_config.block_size = block_size;
227227
}
228228
_config.block_count = bd->size() / _config.block_size;
229-
_config.lookahead = _config.block_count - _config.block_count % 32;
229+
_config.lookahead = 32 * ((_config.block_count+31)/32);
230230
if (_config.lookahead > lookahead) {
231231
_config.lookahead = lookahead;
232232
}

littlefs/.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

littlefs/lfs.c

Lines changed: 11 additions & 11 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,7 +1919,7 @@ 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;
19251925
lfs->free.end = lfs->free.begin + lfs->cfg->block_count;
@@ -1998,8 +1998,8 @@ 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;
2001+
lfs->free.begin = -lfs->cfg->lookahead;
2002+
lfs->free.off = lfs->cfg->lookahead;
20032003
lfs->free.end = lfs->free.begin + lfs->cfg->block_count;
20042004

20052005
// load superblock

littlefs/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)