From a9377073ff39cd47d61fe06ee6470f9c6b354e5b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Thomas=20G=C3=B6ttgens?= Date: Mon, 5 Oct 2026 10:12:43 +0200 Subject: [PATCH] Count InternalFS cache pages dropped after repeated flush failures A cached page whose flush fails three times in a row is dropped, and with it writes LittleFS was already told had succeeded. Count each drop in flash_nrf5x_stats.pages_dropped, so the loss shows in the flash flight recorder. --- libraries/InternalFileSytem/src/flash/flash_cache.c | 2 ++ libraries/InternalFileSytem/src/flash/flash_cache.h | 1 + libraries/InternalFileSytem/src/flash/flash_nrf5x.c | 3 ++- libraries/InternalFileSytem/src/flash/flash_nrf5x.h | 1 + 4 files changed, 6 insertions(+), 1 deletion(-) diff --git a/libraries/InternalFileSytem/src/flash/flash_cache.c b/libraries/InternalFileSytem/src/flash/flash_cache.c index dbe288d..16c7496 100644 --- a/libraries/InternalFileSytem/src/flash/flash_cache.c +++ b/libraries/InternalFileSytem/src/flash/flash_cache.c @@ -118,6 +118,8 @@ bool flash_cache_flush (flash_cache_t* fc) // included, and its data does not survive a reboot anyway. if ( ok || ++fc->flush_failures >= FLASH_CACHE_MAX_FLUSH_FAILURES ) { + // Writes already reported as done are lost with it + if ( !ok && fc->pages_dropped ) (*fc->pages_dropped)++; fc->cache_addr = FLASH_CACHE_INVALID_ADDR; fc->flush_failures = 0; } diff --git a/libraries/InternalFileSytem/src/flash/flash_cache.h b/libraries/InternalFileSytem/src/flash/flash_cache.h index fa6a0ca..3414c07 100644 --- a/libraries/InternalFileSytem/src/flash/flash_cache.h +++ b/libraries/InternalFileSytem/src/flash/flash_cache.h @@ -44,6 +44,7 @@ typedef struct uint32_t cache_addr; uint8_t* cache_buf; uint8_t flush_failures; // consecutive failed flushes of the cached page + uint32_t* pages_dropped; // counts pages given up after FLASH_CACHE_MAX_FLUSH_FAILURES, may be NULL } flash_cache_t; #ifdef __cplusplus diff --git a/libraries/InternalFileSytem/src/flash/flash_nrf5x.c b/libraries/InternalFileSytem/src/flash/flash_nrf5x.c index 30c5d46..f1ee2c8 100644 --- a/libraries/InternalFileSytem/src/flash/flash_nrf5x.c +++ b/libraries/InternalFileSytem/src/flash/flash_nrf5x.c @@ -330,7 +330,8 @@ static flash_cache_t _cache = .verify = fal_verify, .cache_addr = FLASH_CACHE_INVALID_ADDR, - .cache_buf = _cache_buffer + .cache_buf = _cache_buffer, + .pages_dropped = &flash_nrf5x_stats.pages_dropped }; //--------------------------------------------------------------------+ diff --git a/libraries/InternalFileSytem/src/flash/flash_nrf5x.h b/libraries/InternalFileSytem/src/flash/flash_nrf5x.h index 0c0fc1d..fdae711 100644 --- a/libraries/InternalFileSytem/src/flash/flash_nrf5x.h +++ b/libraries/InternalFileSytem/src/flash/flash_nrf5x.h @@ -55,6 +55,7 @@ typedef struct uint32_t max_ticks; // longest completed operation uint32_t last_result; // NRF_SUCCESS or the last error code uint32_t written_off; // completions given up as lost once the SoftDevice accepted a later operation + uint32_t pages_dropped; // cached pages given up after repeated flush failures, losing their writes } flash_nrf5x_stats_t; extern flash_nrf5x_stats_t flash_nrf5x_stats;