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 authored and dleach02 committed Oct 25, 2024
1 parent 87ce37f commit 09574e6
Showing 1 changed file with 18 additions and 2 deletions.
20 changes: 18 additions & 2 deletions subsys/fs/littlefs_fs.c
Original file line number Diff line number Diff line change
Expand Up @@ -836,14 +836,30 @@ 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);
return -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);
return -ENOMEM;
}
lcp->lookahead_size = new_lookahead_size;

lcp->sync = lfs_api_sync_blk;

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

0 comments on commit 09574e6

Please sign in to comment.