eclipse-threadx / levelx

Eclipse ThreadX - LevelX Provides Flash Wear Leveling for FileX and Stand Alone purposes.
https://github.com/eclipse-threadx/rtos-docs/blob/main/rtos-docs/levelx/index.md
MIT License
102 stars 61 forks source link

Problems using LevelX with FileX + USBX #14

Closed maxkunes closed 2 years ago

maxkunes commented 2 years ago

I've had lots of problems trying to get LevelX to play nicely with a FileX filesystem that a host device can access using USB MSD.

In my case, I have about 8 megabytes of flash storage that I need to wear level using LevelX, so I do. I use a FileX exfat filesystem on top of this.

However, I need this storage to be accessible to a PC so I use USBX to allow a host machine to mount the FileX filesystem via USB MSD.

But-- From what I can tell after extensive research and debugging, LevelX starts to perform terribly if it is not notified about released sectors as the amount of (incorrectly) mapped sectors will continuously grow over time if files are deleted via USB and not via the filesystem (which would notify levelx of sectors to release).

From what I understand, there isn't any clean way for LevelX to be notified when USB "deletes" files, as USB MSD just writes and reads to logical sectors and the device itself isn't notified of higher level filesystem interaction.

Right now, I'm able to somewhat mitigate this issue through manual parsing of the EXFAT allocation bitmap on device startup. For every sector/cluster that is marked as unallocated, I tell LevelX about this in an attempt to turn any incorrectly mapped sectors into obsolete sectors that can be freed. Once this operation is complete, I defragment a bit to turn obsolete sectors into free sectors for use (just to increase performance during writes later).

Is there any guidance or anything better I can do here? Or any parameters that can be tuned to make LevelX perform better when the mapped sector count nears the total sector count (not because of many files, but due to USB deleting files and LevelX doesn't get notified about these sectors being released).

I'll note that USB has exclusive filesystem access when it has it. So there isn't an issue of USB and my device trying to access the filesystem at the same time. When USB needs to access the device's filesystem, we close the filesystem and reopen it after its done.

bhnaphade commented 2 years ago

Hi @maxkunes lx_nand_flash_defragment() function can be used to free up blocks.

https://docs.microsoft.com/en-us/azure/rtos/levelx/chapter3#:~:text=Periodic%20use%20of%20lx_nand_flash_defragment%20to%20free%20up%20as%20many%20NAND%20blocks%20as%20possible%20and%20thus%20improve%20write%20performance.