initdisk: further optimizations in CalculateFATData(): merging the FAT16 & FAT32

loops.
fatfs: #if DEBUG fix.


git-svn-id: https://svn.code.sf.net/p/freedos/svn/kernel/trunk@1451 6ac86273-5f31-0410-b378-82cca8765d1b
This commit is contained in:
Bart Oldeman 2009-06-17 20:37:26 +00:00
parent 9e0b5e5bf6
commit e3f90446b3
2 changed files with 97 additions and 119 deletions

View File

@ -253,8 +253,7 @@ f_node_ptr split_path(const char * path, f_node_ptr fnp)
#ifdef DEBUG #ifdef DEBUG
if (get_cds(path[0]-'A')->cdsFlags & CDSNETWDRV) if (get_cds(path[0]-'A')->cdsFlags & CDSNETWDRV)
{ {
printf("split path called for redirected file: `%s'\n", printf("split path called for redirected file: `%s'\n", path);
fcbname);
return (f_node_ptr) 0; return (f_node_ptr) 0;
} }
#endif #endif

View File

@ -412,15 +412,7 @@ VOID CalculateFATData(ddt * pddt, ULONG NumSectors, UBYTE FileSystem)
defbpb->bpb_nreserved = 1; defbpb->bpb_nreserved = 1;
/* SEC_SIZE * DIRENT_SIZE / defbpb->bpb_ndirent + defbpb->bpb_nreserved */ /* SEC_SIZE * DIRENT_SIZE / defbpb->bpb_ndirent + defbpb->bpb_nreserved */
fatdata = NumSectors - (DIRENT_SIZE + 1); fatdata = NumSectors - (DIRENT_SIZE + 1);
#ifdef DEBUG if (FileSystem == FAT12 || FileSystem == FAT12_LBA)
if (FileSystem != FAT12)
DebugPrintf(("%ld sectors for FAT+data, starting with %d sectors/cluster\n", fatdata, defbpb->bpb_nsector));
#endif
switch (FileSystem)
{
case FAT12:
case FAT12_LBA:
{ {
unsigned fatdat; unsigned fatdat;
/* in DOS, FAT12 defaults to 4096kb (8 sector) - clusters. */ /* in DOS, FAT12 defaults to 4096kb (8 sector) - clusters. */
@ -445,76 +437,27 @@ VOID CalculateFATData(ddt * pddt, ULONG NumSectors, UBYTE FileSystem)
unsigned maxclust = (defbpb->bpb_nfsect * 2 * SEC_SIZE) / 3; unsigned maxclust = (defbpb->bpb_nfsect * 2 * SEC_SIZE) / 3;
if (maxclust > FAT12MAX) if (maxclust > FAT12MAX)
maxclust = FAT12MAX; maxclust = FAT12MAX;
printf(("FAT12: #clu=%u, fatlength=%u, maxclu=%u, limit=%u\n", printf("FAT12: #clu=%u, fatlength=%u, maxclu=%u, limit=%u\n",
clust, defbpb->bpb_nfsect, maxclust, FAT12MAX)); clust, defbpb->bpb_nfsect, maxclust, FAT12MAX);
if (clust > maxclust - 2) if (clust > maxclust - 2)
{ {
clust = maxclust - 2; clust = maxclust - 2;
printf(("FAT12: too many clusters: setting to maxclu-2\n")); printf("FAT12: too many clusters: setting to maxclu-2\n");
} }
} }
#endif #endif
memcpy(pddt->ddt_fstype, MSDOS_FAT12_SIGN, 8); memcpy(pddt->ddt_fstype, MSDOS_FAT12_SIGN, 8);
break;
} }
case FAT16SMALL: else
case FAT16LARGE: { /* FAT16/FAT32 */
case FAT16_LBA: CLUSTER fatlength, maxcl;
{
unsigned fatlength;
unsigned long clust, maxclust; unsigned long clust, maxclust;
/* FAT16: start at 4 sectors per cluster */ unsigned fatentpersec;
defbpb->bpb_nsector = 4; unsigned divisor;
/* Force maximal fatdata=8387584 sectors (NumSectors=8387617)
since with our only possible sectorsize (512 bytes) this is the
maximum we can address with 64k clusters
#clus*secperclus+#fats*fatlength=65517 * 128 + 2 * 256=8386688.
max FAT16 size for FreeDOS = 4,293,984,256 bytes = 4GiB-983,040 */
if (fatdata > 8386688ul)
fatdata = 8386688ul;
do
{
DebugPrintf(("Trying with %d sectors/cluster:\n",
defbpb->bpb_nsector));
fatlength = (unsigned)cdiv(fatdata + 2 * defbpb->bpb_nsector,
(SEC_SIZE/2) * defbpb->bpb_nsector + NFAT);
/* Need to calculate number of clusters, since the unused parts of the
* FATS and data area together could make up space for an additional,
* not really present cluster. */
clust = (fatdata - NFAT * fatlength) / defbpb->bpb_nsector;
maxclust = (unsigned long)fatlength * (SEC_SIZE/2);
if (maxclust > FAT16MAX)
maxclust = FAT16MAX;
DebugPrintf(("FAT16: #clu=%lu, fatlen=%u, maxclu=%lu, limit=%u\n",
clust, fatlength, maxclust, FAT_MAGIC16));
if (clust > maxclust - 2)
{
DebugPrintf(("FAT16: too many clusters\n"));
clust = 0;
}
else if (clust <= FAT_MAGIC)
{
/* The <= 4086 avoids that the filesystem will be misdetected as having a
* 12 bit FAT. */
DebugPrintf(("FAT16: would be misdetected as FAT12\n"));
clust = 0;
}
if (clust)
break;
defbpb->bpb_nsector <<= 1;
}
while (defbpb->bpb_nsector && defbpb->bpb_nsector <= MAXCLUSTSIZE);
defbpb->bpb_nfsect = fatlength;
memcpy(pddt->ddt_fstype, MSDOS_FAT16_SIGN, 8);
break;
}
#ifdef WITHFAT32 #ifdef WITHFAT32
case FAT32: if (FileSystem == FAT32 || FileSystem == FAT32_LBA)
case FAT32_LBA:
{ {
unsigned long fatlength, clust, maxclust;
/* For FAT32, use the cluster size table described in the FAT spec: /* For FAT32, use the cluster size table described in the FAT spec:
* http://www.microsoft.com/hwdev/download/hardware/fatgen103.pdf * http://www.microsoft.com/hwdev/download/hardware/fatgen103.pdf
*/ */
@ -532,29 +475,61 @@ VOID CalculateFATData(ddt * pddt, ULONG NumSectors, UBYTE FileSystem)
defbpb->bpb_ndirent = 0; defbpb->bpb_ndirent = 0;
defbpb->bpb_nreserved = 0x20; defbpb->bpb_nreserved = 0x20;
fatdata = NumSectors - 0x20; fatdata = NumSectors - 0x20;
fatentpersec = SEC_SIZE/4;
maxcl = FAT32MAX;
}
else
#endif
{
/* FAT16: start at 4 sectors per cluster */
defbpb->bpb_nsector = 4;
/* Force maximal fatdata=8387584 sectors (NumSectors=8387617)
since with our only possible sectorsize (512 bytes) this is the
maximum we can address with 64k clusters
#clus*secperclus+#fats*fatlength=65517 * 128 + 2 * 256=8386688.
max FAT16 size for FreeDOS = 4,293,984,256 bytes = 4GiB-983,040 */
if (fatdata > 8386688ul)
fatdata = 8386688ul;
fatentpersec = SEC_SIZE/2;
maxcl = FAT16MAX;
}
DebugPrintf(("%ld sectors for FAT+data, starting with %d sectors/cluster\n", fatdata, defbpb->bpb_nsector));
do do
{ {
fatlength = cdiv(fatdata + 2 * defbpb->bpb_nsector, DebugPrintf(("Trying with %d sectors/cluster:\n", defbpb->bpb_nsector));
(SEC_SIZE/4) * defbpb->bpb_nsector + NFAT); divisor = fatentpersec * defbpb->bpb_nsector + NFAT;
fatlength = (CLUSTER)((fatdata + (2 * defbpb->bpb_nsector + divisor - 1))/
divisor);
/* Need to calculate number of clusters, since the unused parts of the /* Need to calculate number of clusters, since the unused parts of the
* FATS and data area together could make up space for an additional, * FATS and data area together could make up space for an additional,
* not really present cluster. */ * not really present cluster. */
clust = (fatdata - NFAT * fatlength) / defbpb->bpb_nsector; clust = (fatdata - NFAT * fatlength) / defbpb->bpb_nsector;
maxclust = fatlength * (SEC_SIZE/4); maxclust = fatlength * fatentpersec;
if (maxclust > FAT32MAX) if (maxclust > maxcl)
maxclust = FAT32MAX; maxclust = maxcl;
DebugPrintf(("FAT32: #clu=%u, fatlen=%u, maxclu=%u, limit=%u\n", DebugPrintf(("FAT: #clu=%lu, fatlen=%lu, maxclu=%lu, limit=%lu\n",
clust, fatlength, maxclust, FAT32MAX)); clust, fatlength, maxclust, maxcl));
if (clust > maxclust - 2) if (clust > maxclust - 2)
{ {
clust = 0; clust = 0;
DebugPrintf(("FAT32: too many clusters\n")); DebugPrintf(("FAT: too many clusters\n"));
}
else if (clust <= FAT_MAGIC)
{
/* The <= 4086 avoids that the filesystem will be misdetected as having a
* 12 bit FAT. */
DebugPrintf(("FAT: would be misdetected as FAT12\n"));
clust = 0;
} }
if (clust) if (clust)
break; break;
defbpb->bpb_nsector <<= 1; defbpb->bpb_nsector <<= 1;
} }
while (defbpb->bpb_nsector && defbpb->bpb_nsector <= MAXCLUSTSIZE); while (defbpb->bpb_nsector && defbpb->bpb_nsector <= MAXCLUSTSIZE);
#ifdef WITHFAT32
if (FileSystem == FAT32 || FileSystem == FAT32_LBA)
{
defbpb->bpb_nfsect = 0; defbpb->bpb_nfsect = 0;
defbpb->bpb_xnfsect = fatlength; defbpb->bpb_xnfsect = fatlength;
/* set up additional FAT32 fields */ /* set up additional FAT32 fields */
@ -564,9 +539,13 @@ VOID CalculateFATData(ddt * pddt, ULONG NumSectors, UBYTE FileSystem)
defbpb->bpb_xfsinfosec = 1; defbpb->bpb_xfsinfosec = 1;
defbpb->bpb_xbackupsec = 6; defbpb->bpb_xbackupsec = 6;
memcpy(pddt->ddt_fstype, MSDOS_FAT32_SIGN, 8); memcpy(pddt->ddt_fstype, MSDOS_FAT32_SIGN, 8);
break;
} }
else
#endif #endif
{
defbpb->bpb_nfsect = (UWORD)fatlength;
memcpy(pddt->ddt_fstype, MSDOS_FAT16_SIGN, 8);
}
} }
pddt->ddt_fstype[8] = '\0'; pddt->ddt_fstype[8] = '\0';
} }