FreeRTOS+FAT Calculating free space

I have found a couple of examples of free space calculation, in the RAMDisk example:

			iPercentageFree = (int) (( HUNDRED_64_BIT * ullFreeSectors + pxIOManager->xPartition.ulDataSectors / 2)
					/ ((uint64_t) pxIOManager->xPartition.ulDataSectors));

and in the FTP Server Demo:

			ulFreeCount = ff_diskfree( pxClient->pcCurrentDir, &ulTotalCount );
			ulPercentage = ( uint32_t ) ( ( 100ULL * ulFreeCount + ulTotalCount / 2 ) / ulTotalCount );

I am curious about the pxIOManager->xPartition.ulDataSectors / 2 (or ulTotalCount / 2) term. Where does that come from? Intuitively, I would guess something like

100 * FreeSectors / TotalCount 

but it seems like the examples say that half of the total sectors are always free but not counted in pxIOManager->xPartition.ulFreeClusterCount.

The code is rounding the outcome: perc = ( free + total / 2 ) / total

It would have been more clear with the use of a macro:

#define PERCENTAGE_ROUNDED( divident, divisor ) \
    ( ( 100UL * divident ) + ( divisor / 2 ) ) / divisor

It’s interesting, but not enough to worry about, that FF_SDDiskShowPartition gets different results than Windows or Ubuntu (in Windows Subsystem for Linux (WSL)):

Reading FAT and calculating Free Space
Partition Nr          0
Type                 12 (FAT32)
VolLabel       'NO NAME    '
TotalSectors   15523832
SecsPerCluster        8
Size            7746769 KB
FreeSize        7732448 KB ( 100 perc free )

image

sudo mount -t drvfs G: /mnt/g
df /mnt/g
Filesystem     1K-blocks  Used Available Use% Mounted on
G:               7746768 14328   7732440   1% /mnt/g
df -h /mnt/g
Filesystem      Size  Used Avail Use% Mounted on
G:              7.4G   14M  7.4G   1% /mnt/g

Size in 1k blocks is close: 7746769 vs. 7746768.
FreeSize (Available) is a little further different, but still close: 7732448 vs. 7732440.

After copying in a file called big3 the numbers are closer:

[FreeRTOS+FAT+CLI]:

> dir
System Volume Information [directory] [size=0]
SpeedNTemp.bin [writable file] [size=1499138]
Vibration.bin [writable file] [size=13143942]
big3 [writable file] [size=1610612736]
.. [directory] [size=1024]
. [directory] [size=1024]

[Windows]:

C:\Users\carlk>chkdsk g:
The type of the file system is FAT32.
Volume MY_DISK created 4/20/2020 9:26 PM
Volume Serial Number is 1CFD-3E73
Windows is verifying files and folders...
File and folder verification is complete.

Windows has scanned the file system and found no problems.
No further action is required.
    7,746,768 KB total disk space.
            4 KB in 1 hidden files.
            8 KB in 2 folders.
    1,587,176 KB in 5 files.
    6,159,576 KB are available.

        4,096 bytes in each allocation unit.
    1,936,692 total allocation units on disk.
    1,539,894 allocation units available on disk.

[WSL]:

df /mnt/g
Filesystem     1K-blocks    Used Available Use% Mounted on
G:               7746768 1587192   6159576  21% /mnt/g
df -h /mnt/g
Filesystem      Size  Used Avail Use% Mounted on
G:              7.4G  1.6G  5.9G  21% /mnt/g

[FreeRTOS+FAT+CLI]:

Reading FAT and calculating Free Space
Partition Nr          0
Type                 12 (FAT32)
VolLabel       'NO NAME    '
TotalSectors   15523832
SecsPerCluster        8
Size            7746769 KB
FreeSize        6159576 KB ( 80 perc free )