From 9503c4a9fc30aaa8ea2031d4fb794436f3c98055 Mon Sep 17 00:00:00 2001 From: Far Date: Fri, 7 May 2021 15:07:50 +0800 Subject: [PATCH] fix: statfs can't get f_bfree and f_bavail of a FAT12/FAT16 partition FAT12/FAT16 partition has no FSINFO sector storing the free cluster number, so scanning the FAT is necessary to get the free clusters nums. Close #I3Q0VS --- fs/fat/os_adapt/fatfs.c | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/fs/fat/os_adapt/fatfs.c b/fs/fat/os_adapt/fatfs.c index ee39d653..abb31580 100644 --- a/fs/fat/os_adapt/fatfs.c +++ b/fs/fat/os_adapt/fatfs.c @@ -1141,6 +1141,9 @@ int fatfs_umount(struct Mount *mnt, struct Vnode **blkdriver) int fatfs_statfs(struct Mount *mnt, struct statfs *info) { FATFS *fs = (FATFS *)mnt->data; + DWORD nclst = 0; + FRESULT result = FR_OK; + int ret; info->f_type = MSDOS_SUPER_MAGIC; #if FF_MAX_SS != FF_MIN_SS @@ -1149,8 +1152,17 @@ int fatfs_statfs(struct Mount *mnt, struct statfs *info) info->f_bsize = FF_MIN_SS * fs->csize; #endif info->f_blocks = fs->n_fatent; + ret = lock_fs(fs); + if (ret == FALSE) { + return -EBUSY; + } + /* free cluster is unavailable, update it */ + if (fs->free_clst == DISK_ERROR) { + result = fat_count_free_entries(&nclst, fs); + } info->f_bfree = fs->free_clst; info->f_bavail = fs->free_clst; + unlock_fs(fs, result); #if FF_USE_LFN /* Maximum length of filenames */ @@ -1166,7 +1178,7 @@ int fatfs_statfs(struct Mount *mnt, struct statfs *info) info->f_ffree = 0; info->f_flags = mnt->mountFlags; - return 0; + return -fatfs_2_vfs(result); } static inline int GET_SECONDS(WORD ftime)