Background Garbage Collection
When an FTL is used, deleting or overwriting files generates dirty flash pages. Dirty pages are normally reclaimed “just in time” by performing recycles when the free page count falls below a threshold. The FTL recycles blocks by copying their in-use pages elsewhere before erasing the selected block. vclean() provides a way to manually invoke recycles.
vclean() takes a single parameter that specifies the volume to be cleaned. It can be the volume name, the path to any file on the volume, or NULL, which selects the volume containing the cur-rent working directory.
vclean() returns -1 if there is an error, 0 if garbage collection is finished, or 1 if there is more cleaning to do. To avoid ‘hogging’ access to the file system, each vclean() call performs at most one block erase. To fully clean a volume, vclean() should be called until it no longer returns 1, as in this example:
/***********************************************************************/
/* cleaner: Reclaim dirty sectors in 'background'. If the flash */
/* driver is guaranteed not to block (i.e. uses polling), */
/* then this vclean() call may be moved to the idle task. */
/* */
/***********************************************************************/
static void cleaner(void)
{
for (;;)
{
while (vclean("flash") > 0) ;
taskSleep(2 * OsTicksPerSec);
}
}
For continual background garbage collection, it is recommended to call vclean() periodically from a low priority task. As above, in its periodic invocation vclean() should be called repeatedly until it returns 0.