Skip to content

Commit

Permalink
Update lfs_find_free_blocks to match the latest changes.
Browse files Browse the repository at this point in the history
  • Loading branch information
ondrapCZE committed Aug 2, 2023
1 parent 611c9b2 commit 444d243
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 10 deletions.
27 changes: 17 additions & 10 deletions lfs.c
Original file line number Diff line number Diff line change
Expand Up @@ -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 ///
Expand Down
4 changes: 4 additions & 0 deletions lfs.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
//
Expand Down

0 comments on commit 444d243

Please sign in to comment.