Renamed lfs_fs_findfreeblocks -> lfs_fs_gc, tweaked documentation

The idea is in the future this function may be extended to support other
block janitorial work. In such a case calling this lfs_fs_gc provides a
more general name that can include other operations.

This is currently just wishful thinking, however.
This commit is contained in:
Christopher Haster
2023-09-12 00:06:04 -05:00
parent 63e4408f2a
commit 6b33ee5e34
3 changed files with 26 additions and 21 deletions

12
lfs.c
View File

@@ -623,7 +623,7 @@ static void lfs_alloc_drop(lfs_t *lfs) {
} }
#ifndef LFS_READONLY #ifndef LFS_READONLY
static int lfs_fs_rawfindfreeblocks(lfs_t *lfs) { static int lfs_fs_rawgc(lfs_t *lfs) {
// Move free offset at the first unused block (lfs->free.i) // 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.i is equal lfs->free.size when all blocks are used
lfs->free.off = (lfs->free.off + lfs->free.i) % lfs->block_count; lfs->free.off = (lfs->free.off + lfs->free.i) % lfs->block_count;
@@ -674,7 +674,7 @@ static int lfs_alloc(lfs_t *lfs, lfs_block_t *block) {
return LFS_ERR_NOSPC; return LFS_ERR_NOSPC;
} }
int err = lfs_fs_rawfindfreeblocks(lfs); int err = lfs_fs_rawgc(lfs);
if(err) { if(err) {
return err; return err;
} }
@@ -6251,16 +6251,16 @@ int lfs_fs_traverse(lfs_t *lfs, int (*cb)(void *, lfs_block_t), void *data) {
} }
#ifndef LFS_READONLY #ifndef LFS_READONLY
int lfs_fs_findfreeblocks(lfs_t *lfs) { int lfs_fs_gc(lfs_t *lfs) {
int err = LFS_LOCK(lfs->cfg); int err = LFS_LOCK(lfs->cfg);
if (err) { if (err) {
return err; return err;
} }
LFS_TRACE("lfs_fs_findfreeblocks(%p)", (void*)lfs); LFS_TRACE("lfs_fs_gc(%p)", (void*)lfs);
err = lfs_fs_rawfindfreeblocks(lfs); err = lfs_fs_rawgc(lfs);
LFS_TRACE("lfs_fs_findfreeblocks -> %d", err); LFS_TRACE("lfs_fs_gc -> %d", err);
LFS_UNLOCK(lfs->cfg); LFS_UNLOCK(lfs->cfg);
return err; return err;
} }

15
lfs.h
View File

@@ -712,12 +712,17 @@ lfs_ssize_t lfs_fs_size(lfs_t *lfs);
// Returns a negative error code on failure. // Returns a negative error code on failure.
int lfs_fs_traverse(lfs_t *lfs, int (*cb)(void*, lfs_block_t), void *data); 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 // Attempt to proactively find free blocks
// search is unpredictable.
// //
// Search is costly operation which may delay write. In realtime write // Calling this function is not required, but may allowing the offloading of
// scenarios can be better to find them before a write. // the expensive block allocation scan to a less time-critical code path.
int lfs_fs_findfreeblocks(lfs_t *lfs); //
// Note: littlefs currently does not persist any found free blocks to disk.
// This may change in the future.
//
// Returns a negative error code on failure. Finding no free blocks is
// not an error.
int lfs_fs_gc(lfs_t *lfs);
#ifndef LFS_READONLY #ifndef LFS_READONLY
// Attempt to make the filesystem consistent and ready for writing // Attempt to make the filesystem consistent and ready for writing

View File

@@ -26,7 +26,7 @@ code = '''
} }
for (int n = 0; n < FILES; n++) { for (int n = 0; n < FILES; n++) {
if (GC) { if (GC) {
lfs_fs_findfreeblocks(&lfs) => 0; lfs_fs_gc(&lfs) => 0;
} }
size_t size = strlen(names[n]); size_t size = strlen(names[n]);
for (lfs_size_t i = 0; i < SIZE; i += size) { for (lfs_size_t i = 0; i < SIZE; i += size) {
@@ -81,7 +81,7 @@ code = '''
memcpy(buffer, names[n], size); memcpy(buffer, names[n], size);
for (int i = 0; i < SIZE; i += size) { for (int i = 0; i < SIZE; i += size) {
if (GC) { if (GC) {
lfs_fs_findfreeblocks(&lfs) => 0; lfs_fs_gc(&lfs) => 0;
} }
lfs_file_write(&lfs, &file, buffer, size) => size; lfs_file_write(&lfs, &file, buffer, size) => size;
} }
@@ -255,8 +255,8 @@ code = '''
} }
res => LFS_ERR_NOSPC; res => LFS_ERR_NOSPC;
// note that lfs_fs_findfreeblocks should not error here // note that lfs_fs_gc should not error here
lfs_fs_findfreeblocks(&lfs) => 0; lfs_fs_gc(&lfs) => 0;
lfs_file_close(&lfs, &file) => 0; lfs_file_close(&lfs, &file) => 0;
lfs_unmount(&lfs) => 0; lfs_unmount(&lfs) => 0;
@@ -309,8 +309,8 @@ code = '''
} }
res => LFS_ERR_NOSPC; res => LFS_ERR_NOSPC;
// note that lfs_fs_findfreeblocks should not error here // note that lfs_fs_gc should not error here
lfs_fs_findfreeblocks(&lfs) => 0; lfs_fs_gc(&lfs) => 0;
lfs_file_close(&lfs, &file) => 0; lfs_file_close(&lfs, &file) => 0;
lfs_unmount(&lfs) => 0; lfs_unmount(&lfs) => 0;
@@ -351,8 +351,8 @@ code = '''
count += 1; count += 1;
} }
err => LFS_ERR_NOSPC; err => LFS_ERR_NOSPC;
// note that lfs_fs_findfreeblocks should not error here // note that lfs_fs_gc should not error here
lfs_fs_findfreeblocks(&lfs) => 0; lfs_fs_gc(&lfs) => 0;
lfs_file_close(&lfs, &file) => 0; lfs_file_close(&lfs, &file) => 0;
lfs_remove(&lfs, "exhaustion") => 0; lfs_remove(&lfs, "exhaustion") => 0;
@@ -451,8 +451,8 @@ code = '''
break; break;
} }
} }
// note that lfs_fs_findfreeblocks should not error here // note that lfs_fs_gc should not error here
lfs_fs_findfreeblocks(&lfs) => 0; lfs_fs_gc(&lfs) => 0;
lfs_file_close(&lfs, &file) => 0; lfs_file_close(&lfs, &file) => 0;
lfs_unmount(&lfs) => 0; lfs_unmount(&lfs) => 0;