littlefs: fix statfs underflow during active writes

lfs_fs_size() can return more blocks than block_count during active
writes due to copy-on-write blocks. This caused f_bavail to underflow
to a large positive number. Add clamping to prevent this.

Signed-off-by: Julian Oes <julian@oes.ch>
This commit is contained in:
Julian Oes
2025-12-29 14:40:18 +13:00
committed by Donny(董九柱)
parent 0900b21217
commit 2a5de4ebc6
+14 -2
View File
@@ -1544,8 +1544,20 @@ static int littlefs_statfs(FAR struct inode *mountpt, FAR struct statfs *buf)
ret = littlefs_convert_result(lfs_fs_size(&fs->lfs));
if (ret > 0)
{
buf->f_bfree -= ret;
buf->f_bavail -= ret;
/* Clamp to prevent underflow - lfs_fs_size can return more than
* block_count during active writes due to COW blocks
*/
if ((fsblkcnt_t)ret < buf->f_bfree)
{
buf->f_bfree -= ret;
buf->f_bavail -= ret;
}
else
{
buf->f_bfree = 0;
buf->f_bavail = 0;
}
ret = 0;
}