FAT clusters that are not aligned to VM pages cause data before and after the requested range to be fetched into the buffer cache. In case of unaligned 64 KB clusters, this will require 68 KB of buffer space, which exceeds the default value of maxbcachebuf, which is 65536 bytes. See PR 277414 for a corresponing bug report.
Typical FAT12 or FAT16 file systems have data clusters starting at odd sector numbers, since there is 1 reserved sector, followed by an even number of FAT sectors and an even number of root directory sectors (assuming a default sector size of 512 bytes and 32 or 512 root directory entries). The odd number of sectors in front of the first data cluster leads to a mis-alignment with respect to buffer cache page boundaries.
This review adds 2 alignments to fix the issue:
- The number of directory entries is adjusted to fill full logical sectors (as required according to the FAT specification).
- The number of reserved sectors is adjusted to make the start of the first data cluster aligned with the size of a VM page (unless overridden by passing the -Aor -roption). The effect of the -A option has been changed to align the data region (not the root directory) with the cluster size.
The advantages of this alignment are:
- Better utilization of the buffer cache, since each cluster is always loaded into the minimum number of buffer pages (instead of needing 1 extra page due to mis-alignment).
- Less write amplification on SSDs, SD cards, or USB memory sticks. (The alignment of the end of the root directory is at least as good as the alignment of the start of the root directory, with respect to write amplification effects.)
This review improves the creation and handling of FAT file systems (e.g. UEFI boot partitions) on FreeBSD, but does not deal with mis-alignment of already created partitions or of FAT file systems created on other systems. A different approach (a GEOM module) is supposed to deal with them.