littlefs
littlefs copied to clipboard
Strategy to handle the case when the littleFS partition becomes full and we cannot sync()/close() an opened file
Hello,
I am experiencing some difficulties when the littleFS partition becomes full and I am searching ideas to correctly handle this case.
My littleFS partition runs over a FRAM with that settings:
.read_size = 16,
.prog_size = 16,
.block_size = 256,
.block_count = 896,
.lookahead = 128,
Several tasks can write files in this partition. Among these tasks, there is one main task that generate recording files. The purpose of my device is to record data continually and send them latter on a "server".
The recordings are sent through the cellular network and deleted only when they are correctly sent. This task could fail or take more time than usual. This situation can lead in a case where lfs_file_sync() or lfs_file_close() returns an error because there are no more blocks available. By now, I choose to handle this case as a fatal error. It means I stay in an infinite loop if these functions failed and latter a watchdog reset the device. It is not pretty especially because this case could occur at boot and therefore the device could stays in a dead loop.
As the recordings are something important and I do no want to loss a recording period, I tried to fix that issue but implementing a small routine that move that files into an another flash device (an eMMC) if I detect the available space goes under a lower limit. To do that, I track the available space with lfs_traverse() calls each time a file is opened or data is written to it and trigger that routine if needed. I do not really like this design. It works most of the time. But it is not fail-proof because the routine can fail if the eMMC is not available at this moment (the eMMC could be mounted through the USB...).
Are there design patterns or strategies to handle this kind of issues?
To sum up, I saw two sides:
- a design to prevent the case where there is no more space. There is an additional difficulty compared to an another filesystem because there is not a direct relation between the bytes we want to write and the available bytes on the partition;
- a strategy to handle the case where a sync() or close() fails caused by a lack of blocks. Perhaps it should be better to cancel last writes, close the file (it should succeed) and pause the recording task until more space is available?
Regards, Anthony.