/* Allocate a free cluster and return the cluster index. */
STATIC UINTN FatAllocateCluster(IN FAT_VOLUME *Volume)
/* Allocate a free cluster and return the cluster index. */
STATIC UINTN FatAllocateCluster(IN FAT_VOLUME *Volume)
{
  UINTN  Cluster;
  if (Volume->DiskError) {
    return (UINTN)FAT_CLUSTER_LAST;
  }
  for ( ; ;) {
    if (Volume->FatInfoSector.FreeInfo.NextCluster > (Volume->MaxCluster + 1)) {
      if (Volume->FreeInfoValid && (0 < (INT32)(Volume->FatInfoSector.FreeInfo.ClusterCount))) {
        Volume->FreeInfoValid = FALSE;
      }
      FatComputeFreeInfo (Volume);
      if (Volume->FatInfoSector.FreeInfo.NextCluster > (Volume->MaxCluster + 1)) {
        return (UINTN)FAT_CLUSTER_LAST;
      }
    }
    Cluster = FatGetFatEntry (Volume, Volume->FatInfoSector.FreeInfo.NextCluster);
    if (Cluster == FAT_CLUSTER_FREE) {
      break;
    }
    Volume->FatInfoSector.FreeInfo.NextCluster += 1;
  }
  Cluster                                     = Volume->FatInfoSector.FreeInfo.NextCluster;
  Volume->FatInfoSector.FreeInfo.NextCluster += 1;
  return Cluster;
}