From 444d243ae1dfbc00e1ab0109166ebb0138713397 Mon Sep 17 00:00:00 2001 From: ondrap Date: Wed, 2 Aug 2023 11:51:52 +0200 Subject: [PATCH 1/2] Update lfs_find_free_blocks to match the latest changes. --- lfs.c | 27 +++++++++++++++++---------- lfs.h | 4 ++++ 2 files changed, 21 insertions(+), 10 deletions(-) diff --git a/lfs.c b/lfs.c index 38b825dd..f24002ba 100644 --- a/lfs.c +++ b/lfs.c @@ -654,20 +654,27 @@ static int lfs_alloc(lfs_t *lfs, lfs_block_t *block) { return LFS_ERR_NOSPC; } - lfs->free.off = (lfs->free.off + lfs->free.size) - % lfs->cfg->block_count; - lfs->free.size = lfs_min(8*lfs->cfg->lookahead_size, lfs->free.ack); - lfs->free.i = 0; - - // find mask of free blocks from tree - memset(lfs->free.buffer, 0, lfs->cfg->lookahead_size); - int err = lfs_fs_rawtraverse(lfs, lfs_alloc_lookahead, lfs, true); - if (err) { - lfs_alloc_drop(lfs); + int err = lfs_find_free_blocks(lfs); + if(err) { return err; } } } + +int lfs_find_free_blocks(lfs_t *lfs){ + lfs->free.off = (lfs->free.off + lfs->free.size) + % lfs->cfg->block_count; + lfs->free.size = lfs_min(8*lfs->cfg->lookahead_size, lfs->free.ack); + lfs->free.i = 0; + + // find mask of free blocks from tree + memset(lfs->free.buffer, 0, lfs->cfg->lookahead_size); + int const err = lfs_fs_rawtraverse(lfs, lfs_alloc_lookahead, lfs, true); + if (err) { + lfs_alloc_drop(lfs); + } + return err; +} #endif /// Metadata pair and directory operations /// diff --git a/lfs.h b/lfs.h index 1081735f..004e1465 100644 --- a/lfs.h +++ b/lfs.h @@ -705,6 +705,10 @@ lfs_ssize_t lfs_fs_size(lfs_t *lfs); // Returns a negative error code on failure. int lfs_fs_traverse(lfs_t *lfs, int (*cb)(void*, lfs_block_t), void *data); +// Use Traverse function and try to find free blocks. LittleFS free blocks search is unpredictable. +// Search is costly operation which may delay write. In realtime write scenarios can be better to find them before a write. +int lfs_find_free_blocks(lfs_t *lfs); + #ifndef LFS_READONLY // Attempt to make the filesystem consistent and ready for writing // From 9f46d2f988cf7d9abf8ab6c24d596c8c6f0812a2 Mon Sep 17 00:00:00 2001 From: ondrap Date: Tue, 29 Aug 2023 12:50:16 +0200 Subject: [PATCH 2/2] Move lookahead buffer offset at the first free block if such block doesn't exist move it for whole lookahead size. --- lfs.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/lfs.c b/lfs.c index f24002ba..5b80fff2 100644 --- a/lfs.c +++ b/lfs.c @@ -662,7 +662,9 @@ static int lfs_alloc(lfs_t *lfs, lfs_block_t *block) { } int lfs_find_free_blocks(lfs_t *lfs){ - lfs->free.off = (lfs->free.off + lfs->free.size) + // Move free offset at the first unused block (lfs->free.i) + // lfs->free.i is equal lfs->free.size when all blocks are used + lfs->free.off = (lfs->free.off + lfs->free.i) % lfs->cfg->block_count; lfs->free.size = lfs_min(8*lfs->cfg->lookahead_size, lfs->free.ack); lfs->free.i = 0;