[fat] align start of data region to MiB #2387
Closed
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Rufus uses
fat32format
to create FAT32 volumes larger than 32GB. That piece of code is from 10+ years ago and is not optimized for Advanced Format disks. To be specific, when usingfat32format
, even if the partition itself is 4K (or MiB)-aligned, you may still end up with non-aligned I/O operations, causing performance degradation.Most modern filesystems, including the younger sibling in the FAT family, uniformly uses "clusters" or "blocks" as (logical) I/O units. For example, NTFS boot code occupies clusters 0 and 1 (typically 8KiB in total); ext4 superblock occupies block 0, even though the superblock itself fits in 1KiB of space, leaving 3KiB unused.
However, FAT or FAT32 only divides the data zone into clusters. In metadata zone (boot code, FATs, etc.) there are only sectors. In case of metadata zone size not being a multiple of cluster size, FAT32 cluster boundaries will not align with physical block boundaries on the underlying device.
An example of misaligned FAT32 internal structure:
(Green - logical clusters; yellow - physical blocks; the example uses 1 cluster = 4 logical sectors and 3 "reserved sectors" in total)
Fortunately, the FAT32 number of reserved sectors is pretty arbitrary. By adjusting that number, we'll be able to ensure the data zone begins on physical block boundary.
Since Vista, Windows has been aligning FAT32 data zones aggressively, to multiples of 4MiB; I don't know the notion behind that behavior. Because Rufus (and Windows
diskpart
) align partitions to 1MiB, I decide to also align the data zone to 1MiB, which is multiple of all common cluster sizes. The resulting "reserved sector count" will be in the range of [32, 2080).Misaligned data zone under the old behavior (60308 is not a multiple of 64 [sectors per cluster]):
Log showing the new behavior (310 reserved sectors):
Aligned data zone under the new behavior (120832 is a multiple of 32):
Chkdsk showing the newly formatted volume is ok:
I tested this on two moderately large devices (80G [ancient] HDD and 128G TF card) and was successful booting from the resulting media.