@@ -486,22 +486,40 @@ fn determine_bytes_per_cluster(total_bytes: u64, bytes_per_sector: u16, fat_type
486
486
487
487
let fat_type = fat_type. unwrap_or_else ( || estimate_fat_type ( total_bytes) ) ;
488
488
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
+ }
490
493
FatType :: Fat16 => {
491
494
if total_bytes <= 16 * MB_64 {
495
+ // cluster size: 1 KB
496
+ // approximate cluster count: (4096, 16384]
492
497
KB_32
493
498
} else if total_bytes <= 128 * MB_64 {
499
+ // cluster size: 2 KB
500
+ // approximate cluster count: (8192, 65536]
494
501
2 * KB_32
495
502
} else {
503
+ // cluster size: 4 KB or more
504
+ // approximate cluster count: (32768, 65536]
496
505
( ( total_bytes. next_power_of_two ( ) / ( 64 * MB_64 ) ) as u32 ) * KB_32
497
506
}
498
507
}
499
508
FatType :: Fat32 => {
500
509
if total_bytes <= 260 * MB_64 {
510
+ // cluster size: 512 B
511
+ // approximate cluster count: (65_536, 532_480]
501
512
512
502
513
} 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
503
518
4 * KB_32
504
519
} 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
505
523
( ( total_bytes. next_power_of_two ( ) / ( 2 * GB_64 ) ) as u32 ) * KB_32
506
524
}
507
525
}
@@ -652,6 +670,8 @@ fn determine_fs_layout<E: IoError>(options: &FormatVolumeOptions, total_sectors:
652
670
. as_ref ( )
653
671
. map_or ( & [ FatType :: Fat32 , FatType :: Fat16 , FatType :: Fat12 ] , slice:: from_ref) ;
654
672
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`
655
675
for & fat_type in allowed_fat_types {
656
676
let root_dir_sectors =
657
677
determine_root_dir_sectors ( options. max_root_dir_entries , options. bytes_per_sector , fat_type) ;
0 commit comments