Skip to content

Commit

Permalink
microbit: Sweep the filesystem if any free chunks found.
Browse files Browse the repository at this point in the history
If there are any free chunks found then it's better to sweep the filesystem
and use the available chunks, rather than error out with ENOSPC when there
is in fact a bit of space remaining.

Signed-off-by: Damien George <damien@micropython.org>
  • Loading branch information
dpgeorge committed Aug 24, 2022
1 parent 256520f commit 5ebaca3
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 7 deletions.
4 changes: 0 additions & 4 deletions inc/microbit/filesystem.h
Original file line number Diff line number Diff line change
Expand Up @@ -76,10 +76,6 @@ typedef struct _file_descriptor_obj {
/** Must be such that sizeof(file_header) < DATA_PER_CHUNK */
#define MAX_FILENAME_LENGTH 120

//Minimum number of free chunks to justify sweeping.
//If this is too low it may cause excessive wear
#define MIN_CHUNKS_FOR_SWEEP 8

typedef struct _file_header {
uint8_t end_offset;
uint8_t name_len;
Expand Down
6 changes: 3 additions & 3 deletions source/microbit/filesystem.c
Original file line number Diff line number Diff line change
Expand Up @@ -202,9 +202,9 @@ uint8_t microbit_find_file(const char *name, int name_len) {
* Search the chunks:
* 1 If an UNUSED chunk is found, then return that.
* 2. If an entire page of FREED chunks is found, then erase the page and return the first chunk
* 3. If the number of FREED chunks is >= MIN_FREE_CHUNKS_FOR_SWEEP, then
* 3. If the number of FREED chunks is > 0, then
* 3a. Sweep the filesystem and restart.
* 3b. Fail and return FILE_NOT_FOUND
* 3b. Otherwise, fail and return FILE_NOT_FOUND.
*/
static uint8_t find_chunk_and_erase(void) {
// Start search at a random chunk to spread the wear more evenly.
Expand Down Expand Up @@ -245,7 +245,7 @@ static uint8_t find_chunk_and_erase(void) {
if (index == chunks_in_file_system+1) index = 1;
} while (index != start_index);
DEBUG(("FILE DEBUG: %lu free chunks\r\n", freed_chunks));
if (freed_chunks < MIN_CHUNKS_FOR_SWEEP) {
if (freed_chunks == 0) {
return FILE_NOT_FOUND;
}
// No freed pages, so sweep file system.
Expand Down

0 comments on commit 5ebaca3

Please sign in to comment.