Skip to content

Commit

Permalink
Added API boilerplate for lfs_fs_findfreeblocks and consistent style
Browse files Browse the repository at this point in the history
This adds the tracing and optional locking for the littlefs API.

Also updated to match the code style, and added LFS_READONLY guards
where necessary.
  • Loading branch information
geky committed Sep 21, 2023
1 parent d85a0fe commit dbe4598
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 21 deletions.
55 changes: 37 additions & 18 deletions lfs.c
Original file line number Diff line number Diff line change
Expand Up @@ -622,6 +622,26 @@ static void lfs_alloc_drop(lfs_t *lfs) {
lfs_alloc_ack(lfs);
}

#ifndef LFS_READONLY
static int lfs_fs_rawfindfreeblocks(lfs_t *lfs) {
// 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->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);
return err;
}

return 0;
}
#endif

#ifndef LFS_READONLY
static int lfs_alloc(lfs_t *lfs, lfs_block_t *block) {
while (true) {
Expand Down Expand Up @@ -654,29 +674,12 @@ static int lfs_alloc(lfs_t *lfs, lfs_block_t *block) {
return LFS_ERR_NOSPC;
}

int err = lfs_find_free_blocks(lfs);
int err = lfs_fs_rawfindfreeblocks(lfs);
if(err) {
return err;
}
}
}

int lfs_find_free_blocks(lfs_t *lfs){
// 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->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 ///
Expand Down Expand Up @@ -6247,6 +6250,22 @@ int lfs_fs_traverse(lfs_t *lfs, int (*cb)(void *, lfs_block_t), void *data) {
return err;
}

#ifndef LFS_READONLY
int lfs_fs_findfreeblocks(lfs_t *lfs) {
int err = LFS_LOCK(lfs->cfg);
if (err) {
return err;
}
LFS_TRACE("lfs_fs_findfreeblocks(%p)", (void*)lfs);

err = lfs_fs_rawfindfreeblocks(lfs);

LFS_TRACE("lfs_fs_findfreeblocks -> %d", err);
LFS_UNLOCK(lfs->cfg);
return err;
}
#endif

#ifndef LFS_READONLY
int lfs_fs_mkconsistent(lfs_t *lfs) {
int err = LFS_LOCK(lfs->cfg);
Expand Down
9 changes: 6 additions & 3 deletions lfs.h
Original file line number Diff line number Diff line change
Expand Up @@ -712,9 +712,12 @@ 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);
// 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_fs_findfreeblocks(lfs_t *lfs);

#ifndef LFS_READONLY
// Attempt to make the filesystem consistent and ready for writing
Expand Down

0 comments on commit dbe4598

Please sign in to comment.