Skip to content

Commit 9bf887d

Browse files
committed
Add comments that make it more clear how cluster size is determined
1 parent ef362a1 commit 9bf887d

File tree

1 file changed

+21
-1
lines changed

1 file changed

+21
-1
lines changed

src/boot_sector.rs

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -486,22 +486,40 @@ fn determine_bytes_per_cluster(total_bytes: u64, bytes_per_sector: u16, fat_type
486486

487487
let fat_type = fat_type.unwrap_or_else(|| estimate_fat_type(total_bytes));
488488
let bytes_per_cluster = match fat_type {
489-
FatType::Fat12 => (total_bytes.next_power_of_two() / MB_64 * 512) as u32,
489+
FatType::Fat12 => {
490+
// approximate cluster count: (1024, 2048]
491+
(total_bytes.next_power_of_two() / MB_64 * 512) as u32
492+
}
490493
FatType::Fat16 => {
491494
if total_bytes <= 16 * MB_64 {
495+
// cluster size: 1 KB
496+
// approximate cluster count: (4096, 16384]
492497
KB_32
493498
} else if total_bytes <= 128 * MB_64 {
499+
// cluster size: 2 KB
500+
// approximate cluster count: (8192, 65536]
494501
2 * KB_32
495502
} else {
503+
// cluster size: 4 KB or more
504+
// approximate cluster count: (32768, 65536]
496505
((total_bytes.next_power_of_two() / (64 * MB_64)) as u32) * KB_32
497506
}
498507
}
499508
FatType::Fat32 => {
500509
if total_bytes <= 260 * MB_64 {
510+
// cluster size: 512 B
511+
// approximate cluster count: (65_536, 532_480]
501512
512
502513
} else if total_bytes <= 8 * GB_64 {
514+
// cluster size: 4 KB
515+
// approximate cluster count: (66_560, 2_097_152]
516+
// Note: for minimal volume size (260 MB + 1 sector) it always results in enough cluster for this FAT type
517+
// unless there is more than ~7000 reserved sectors
503518
4 * KB_32
504519
} else {
520+
// cluster size: 8 KB or more
521+
// approximate cluster count: (1_048_576, 2_097_152]
522+
// at 32 GB it already uses maximal cluster size and then cluster count goes out of the above range
505523
((total_bytes.next_power_of_two() / (2 * GB_64)) as u32) * KB_32
506524
}
507525
}
@@ -652,6 +670,8 @@ fn determine_fs_layout<E: IoError>(options: &FormatVolumeOptions, total_sectors:
652670
.as_ref()
653671
.map_or(&[FatType::Fat32, FatType::Fat16, FatType::Fat12], slice::from_ref);
654672

673+
// Note: this loop is needed because in case of user-provided cluster size it is hard to reliably determine
674+
// a proper FAT type. In case of automatic cluster size actual FAT type is determined in `estimate_fat_type`
655675
for &fat_type in allowed_fat_types {
656676
let root_dir_sectors =
657677
determine_root_dir_sectors(options.max_root_dir_entries, options.bytes_per_sector, fat_type);

0 commit comments

Comments
 (0)