userlibandfileserver/fileserver/sfat/sl_fat16.cpp
changeset 90 947f0dc9f7a8
parent 15 4122176ea935
--- a/userlibandfileserver/fileserver/sfat/sl_fat16.cpp	Tue Feb 02 01:24:03 2010 +0200
+++ b/userlibandfileserver/fileserver/sfat/sl_fat16.cpp	Fri Apr 16 16:24:37 2010 +0300
@@ -82,37 +82,49 @@
         iSectorsPerFat=MaxFat16Sectors();
         }
     
-    // Ensure cluster size is a multiple of the block size
-    TInt blockSizeInSectors = aCaps.iBlockSize >> iSectorSizeLog2;
-    __PRINT1(_L("blockSizeInSectors: %d"),blockSizeInSectors);
-    ASSERT(blockSizeInSectors == 0 || IsPowerOf2(blockSizeInSectors));
-    if (blockSizeInSectors != 0 && IsPowerOf2(blockSizeInSectors))
-        {
-        __PRINT1(_L("iSectorsPerCluster (old): %d"),iSectorsPerCluster);
-        AdjustClusterSize(blockSizeInSectors);
-        __PRINT1(_L("iSectorsPerCluster (new): %d"),iSectorsPerCluster);
-        }
-    
-    // Align first data sector on an erase block boundary if
-    // (1) the iEraseBlockSize is specified
-    // (2) the start of the partition is already aligned to an erase block boundary, 
-    //     i.e. iHiddenSectors is zero or a multiple of iEraseBlockSize
-    __PRINT1(_L("iHiddenSectors: %d"),iHiddenSectors);
-    TInt eraseblockSizeInSectors = aCaps.iEraseBlockSize >> iSectorSizeLog2;
-    __PRINT1(_L("eraseblockSizeInSectors: %d"),eraseblockSizeInSectors);
-    ASSERT(eraseblockSizeInSectors == 0 || IsPowerOf2(eraseblockSizeInSectors));    
-    ASSERT(eraseblockSizeInSectors == 0 || eraseblockSizeInSectors >= blockSizeInSectors);
-    if ((eraseblockSizeInSectors != 0) &&
-        (iHiddenSectors % eraseblockSizeInSectors == 0) &&  
-        (IsPowerOf2(eraseblockSizeInSectors)) &&
-        (eraseblockSizeInSectors >= blockSizeInSectors))
-        {
-        TInt r = AdjustFirstDataSectorAlignment(eraseblockSizeInSectors);
-        ASSERT(r == KErrNone);
-        (void) r;
-        }
-    __PRINT1(_L("iReservedSectors: %d"),iReservedSectors);
-    __PRINT1(_L("FirstDataSector: %d"), FirstDataSector());
+	const TFatType fatType = SuggestFatType();
+
+	// Ensure cluster size is a multiple of the block size
+	TInt blockSizeInSectors = aCaps.iBlockSize >> iSectorSizeLog2;
+	__PRINT1(_L("blockSizeInSectors: %d"),blockSizeInSectors);
+	ASSERT(blockSizeInSectors == 0 || IsPowerOf2(blockSizeInSectors));
+	if (blockSizeInSectors != 0 && IsPowerOf2(blockSizeInSectors))
+		{
+		__PRINT1(_L("iSectorsPerCluster    (old): %d"),iSectorsPerCluster);
+		AdjustClusterSize(blockSizeInSectors);
+		__PRINT1(_L("iSectorsPerCluster    (new): %d"),iSectorsPerCluster);
+		}
+
+
+	for (; iSectorsPerCluster>1; iSectorsPerCluster>>= 1)
+		{
+		// Align first data sector on an erase block boundary if
+		// (1) the iEraseBlockSize is specified
+		// (2) the start of the partition is already aligned to an erase block boundary, 
+		//     i.e. iHiddenSectors is zero or a multiple of iEraseBlockSize
+		__PRINT1(_L("iHiddenSectors: %d"),iHiddenSectors);
+		TInt eraseblockSizeInSectors = aCaps.iEraseBlockSize >> iSectorSizeLog2;
+		__PRINT1(_L("eraseblockSizeInSectors: %d"),eraseblockSizeInSectors);
+		ASSERT(eraseblockSizeInSectors == 0 || IsPowerOf2(eraseblockSizeInSectors));	
+		ASSERT(eraseblockSizeInSectors == 0 || eraseblockSizeInSectors >= blockSizeInSectors);
+		if ((eraseblockSizeInSectors != 0) &&
+			(iHiddenSectors % eraseblockSizeInSectors == 0) &&	
+			(IsPowerOf2(eraseblockSizeInSectors)) &&
+			(eraseblockSizeInSectors >= blockSizeInSectors))
+			{
+			TInt r = AdjustFirstDataSectorAlignment(eraseblockSizeInSectors);
+			ASSERT(r == KErrNone);
+			(void) r;
+			}
+		__PRINT1(_L("iReservedSectors: %d"),iReservedSectors);
+		__PRINT1(_L("FirstDataSector: %d"), FirstDataSector());
+
+		// If we've shrunk the number of clusters by so much that it's now invalid for this FAT type
+		// then we need to decrease the cluster size and try again, otherwise we're finshed.
+		if (SuggestFatType() == fatType)
+			break;
+		}
+	__PRINT1(_L("iSectorsPerCluster  (final): %d"),iSectorsPerCluster);
 
     return KErrNone;
     }
@@ -124,11 +136,11 @@
     }
 
 void CFatFormatCB::AdjustClusterSize(TInt aRecommendedSectorsPerCluster)
-    {
-    const TInt KMaxSecPerCluster = 64;  // 32K
-    while (aRecommendedSectorsPerCluster > iSectorsPerCluster && iSectorsPerCluster <= (KMaxSecPerCluster/2))
-        iSectorsPerCluster<<= 1;
-    }
+	{
+    const TInt KMaxSecPerCluster = 64;	// 32K
+	while (aRecommendedSectorsPerCluster > iSectorsPerCluster && iSectorsPerCluster <= (KMaxSecPerCluster/2))
+		iSectorsPerCluster<<= 1;
+	}
 
 // AdjustFirstDataSectorAlignment()
 // Attempts to align the first data sector on an erase block boundary by modifying the