Skip to content

Commit

Permalink
fs: littlefs: Gracefully fail when static buffers are too small
Browse files Browse the repository at this point in the history
The cache_size and lookahead_size are set at compile time using the
CONFIG_FS_LITTLEFS_CACHE_SIZE and CONFIG_FS_LITTLEFS_LOOKAHEAD_SIZE values
from Kconfig, or from the cache-size and lookahead-size properties in a
'zephyr,fstab,littlefs' compatible in the devicetree.  Those values are
also used to statically allocate buffers that are pointed at by the
read_buffer, prog_buffer, and lookahead_buffer members of the lfs_config
structure.

At runtime, when using a block device, the cache_size and lookahead_size
are updated to be multiples of the underlying block device's block_size,
which may make them bigger than the original size used to allocate the
static buffers. Log an error and fail the operation when this occurs.

Signed-off-by: Phil Hindman <[email protected]>
  • Loading branch information
pahindman committed Oct 17, 2024
1 parent 03959a2 commit 4fd8d97
Showing 1 changed file with 22 additions and 2 deletions.
24 changes: 22 additions & 2 deletions subsys/fs/littlefs_fs.c
Original file line number Diff line number Diff line change
Expand Up @@ -836,14 +836,34 @@ static int littlefs_init_cfg(struct fs_littlefs *fs, int flags)
lcp->context = fs->backend;
/* Set the validated/defaulted values. */
if (littlefs_on_blkdev(flags)) {
lfs_size_t new_cache_size = block_size;
lfs_size_t new_lookahead_size = block_size * 4;

lcp->read = lfs_api_read_blk;
lcp->prog = lfs_api_prog_blk;
lcp->erase = lfs_api_erase_blk;

lcp->read_size = block_size;
lcp->prog_size = block_size;
lcp->cache_size = block_size;
lcp->lookahead_size = block_size * 4;

if (lcp->cache_size < new_cache_size) {
LOG_ERR("Configured cache size is too small: %d < %d", lcp->cache_size,
new_cache_size);
ret = -ENOMEM;
}
lcp->cache_size = new_cache_size;

if (lcp->lookahead_size < new_lookahead_size) {
LOG_ERR("Configured lookahead size is too small: %d < %d",
lcp->lookahead_size, new_lookahead_size);
ret = -ENOMEM;
}
lcp->lookahead_size = new_lookahead_size;

if (ret < 0) {
goto out;
}

lcp->sync = lfs_api_sync_blk;

LOG_INF("sizes: rd %u ; pr %u ; ca %u ; la %u",
Expand Down

0 comments on commit 4fd8d97

Please sign in to comment.