summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDamien George <damien@micropython.org>2024-01-16 11:13:22 +1100
committerDamien George <damien@micropython.org>2024-01-16 11:42:47 +1100
commita70367e293a2293918a3649dc8c6f875c39543cd (patch)
tree754e1f503827e20ce71ed9a7c45bcf4833405f4a
parentf9df08d8ee29145b547d7bf37a7977aa465c437c (diff)
nrf/modules/os/microbitfs: Sweep the filesystem if any free chunk found.
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>
-rw-r--r--ports/nrf/modules/os/microbitfs.c10
1 files changed, 3 insertions, 7 deletions
diff --git a/ports/nrf/modules/os/microbitfs.c b/ports/nrf/modules/os/microbitfs.c
index 5ffe837ef..bc007c161 100644
--- a/ports/nrf/modules/os/microbitfs.c
+++ b/ports/nrf/modules/os/microbitfs.c
@@ -80,10 +80,6 @@
/** 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 (FLASH_PAGESIZE / CHUNK_SIZE)
-
#define FILE_NOT_FOUND ((uint8_t)-1)
/** Maximum number of chunks allowed in filesystem. 252 chunks is 31.5kB */
@@ -268,9 +264,9 @@ STATIC 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_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.
@@ -311,7 +307,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.