From 38eadbb57598f952c9c580dece28695cac3fc566 Mon Sep 17 00:00:00 2001 From: GAEHWILER Reto Date: Sun, 4 Jul 2021 14:20:45 +0200 Subject: [PATCH] FAT32 kconfig entry to enforce computation of free clusters at mount Follow up commit for [1] in response to the request to make the mount behaviour configurable whether the number of free clusters is read from the fsinfo section or computed (as the white paper suggests). Default is the original behaviour of NUTTX, just read fsinfo. [1] https://github.com/apache/incubator-nuttx/pull/3760 --- fs/fat/Kconfig | 12 ++++++++++++ fs/fat/fs_fat32util.c | 17 +++++++++++------ 2 files changed, 23 insertions(+), 6 deletions(-) diff --git a/fs/fat/Kconfig b/fs/fat/Kconfig index 710d634d5c..c572c3c957 100644 --- a/fs/fat/Kconfig +++ b/fs/fat/Kconfig @@ -12,6 +12,18 @@ config FS_FAT if FS_FAT +config FAT_COMPUTE_FSINFO + bool "FAT compute free space in FSINFO at mount time" + default n + ---help--- + Enables the computation of free clusters at mount time as suggested by the + white paper for FAT. The standard behavior of Nuttx is to trust the stored + value and only recompute it once required. This works if the file system + is never mounted to another OS. SD-cards which are mounted to Windows to + modify the content might report wrong space after reinserting it to Nuttx. + It is recommended to activate this setting if the "SD-Card" is swapped + between systems. + config FAT_LCNAMES bool "FAT upper/lower names" default n diff --git a/fs/fat/fs_fat32util.c b/fs/fat/fs_fat32util.c index 7045068e85..d24d8fec08 100644 --- a/fs/fat/fs_fat32util.c +++ b/fs/fat/fs_fat32util.c @@ -626,13 +626,23 @@ int fat_mount(struct fat_mountpt_s *fs, bool writeable) if (fs->fs_type == FSTYPE_FAT32) { - ret = fat_computefreeclusters(fs); + ret = fat_checkfsinfo(fs); if (ret != OK) { goto errout_with_buffer; } } + /* Enforce computation of free clusters if configured */ + +#ifdef CONFIG_FAT_COMPUTE_FSINFO + ret = fat_computefreeclusters(fs); + if (ret != OK) + { + goto errout_with_buffer; + } +#endif + /* We did it! */ finfo("FAT%d:\n", fs->fs_type == 0 ? 12 : fs->fs_type == 1 ? 16 : 32); @@ -2028,11 +2038,6 @@ int fat_updatefsinfo(struct fat_mountpt_s *fs) int fat_computefreeclusters(struct fat_mountpt_s *fs) { - if (fat_checkfsinfo(fs) != OK) - { - return -ENODEV; - } - /* We have to count the number of free clusters */ uint32_t nfreeclusters = 0;