os/kernelhwsrv/userlibandfileserver/fileserver/sfat32/fat_config.cpp
author sl@SLION-WIN7.fritz.box
Fri, 15 Jun 2012 03:10:57 +0200
changeset 0 bde4ae8d615e
permissions -rw-r--r--
First public contribution.
     1 // Copyright (c) 1996-2009 Nokia Corporation and/or its subsidiary(-ies).
     2 // All rights reserved.
     3 // This component and the accompanying materials are made available
     4 // under the terms of the License "Eclipse Public License v1.0"
     5 // which accompanies this distribution, and is available
     6 // at the URL "http://www.eclipse.org/legal/epl-v10.html".
     7 //
     8 // Initial Contributors:
     9 // Nokia Corporation - initial contribution.
    10 //
    11 // Contributors:
    12 //
    13 // Description:
    14 // f32\sfat\fat_config.cpp
    15 // Implementation of the class that reads FAT filesystem configuration parameters from the estart.txt file, 
    16 // caches and provides access to them
    17 // 
    18 //
    19 
    20 /**
    21  @file
    22 */
    23 
    24 #include <f32fsys.h>
    25 
    26 #include "common_constants.h"
    27 #include "sl_std.h"
    28 #include "fat_config.h"
    29 
    30 //########################################################################################################################
    31 //-- parameters that can be defined in estart.txt file under [Drive%] section
    32 
    33 //=======================================================================================================================
    34 //-- FAT volumes mounting related settings
    35 //=======================================================================================================================
    36 
    37 //-- if this parameter is not 0, ScanDrive will skip finalised volume. Applicable for FAT16/32
    38 //-- e.g:   FAT_ScanDrv_SkipFinalisedVolume 1
    39 _LIT8(KPN_ScanDrvSkipFinalisedVolume,"FAT_ScanDrv_SkipFinalisedVolume");  //-- parameter key name
    40 static const TUint32 KDef_ScanDrvSkipFinalisedVolume = 0; //-- parameter default value
    41 
    42 //-- if this parameter is not 0, enables asynchronous mounting FAT32 volumes. 
    43 //-- At present it means enabling/disabling FAT32 background scanning for free clusters. Applicable for FAT32 only
    44 //-- e.g:   FAT_FAT32_AsynchMount=0
    45 _LIT8(KPN_FAT32_AsynchMount, "FAT_FAT32_AsynchMount"); 
    46 static const TUint32 KDef_FAT32_AsynchMount = 1;
    47 
    48 //-- Using FSInfo sector data during FAT32 mount to find out number of free clusters etc.
    49 //-- e.g:   FAT_FAT32_UseFsInfo=1
    50 _LIT8(KPN_FAT32_UseFsInfoOnMount, "FAT_FAT32_UseFsInfo"); 
    51 static const TUint32 KDef_FAT32_UseFsInfoOnMount = 1;
    52 
    53 //-- Using FAT bit supercache for fast free cluster lookup
    54 //-- e.g:   FAT_FAT32_UseBitSupercache 0
    55 _LIT8(KPN_FAT32_UseFatBitSupercache, "FAT_FAT32_UseBitSupercache"); 
    56 static const TUint32 KDef_FAT32_UseFatBitSupercache = 1;
    57 
    58 
    59 //-- if this parameter is not 0, "clean shutdown mask" bit in FAT16[1] will be used during volume finalisation.
    60 //-- Otherwise, FAT16[1] will not be affected during finalisation
    61 //-- e.g:   KDef_FAT16_UseCleanShutDownBit 0
    62 _LIT8(KPN_FAT16_UseCleanShutDownBit,"FAT16_UseCleanShutDownBit");  //-- parameter key name
    63 static const TUint32 KDef_FAT16_UseCleanShutDownBit = 1;           //-- parameter default value
    64 
    65 
    66 //=======================================================================================================================
    67 //-- FAT directory cache settings
    68 //=======================================================================================================================
    69 //-- FAT_DirCache <CacheSizeKB>,<Log2(max page size)>
    70 //-- e.g:   FAT_DirCache 16,12
    71 _LIT8(KPN_FAT_DirCache, "FAT_DirCache"); 
    72 
    73 static const TUint32 KDef_KDirCacheSizeKB = 16;      //-- default value for the total directory cache size, kilobytes. 16K by default
    74 static const TUint32 KDef_MaxDirCachePageSzLog2 = 12;//-- default value for directory cache single page maximal size Log2, 2^12 (4K) by default
    75 
    76 
    77 //=======================================================================================================================
    78 //-- FAT cache settings
    79 //=======================================================================================================================
    80 
    81 //-- Fat16 fixed cache
    82 //-- FAT_FAT16FixedCache <Log2(read granularity)>,<Log2(Write granularity)> 
    83 //-- e.g:   FAT_FAT16FixedCache 12,9 
    84 _LIT8(KPN_FAT_FAT16FixedCache, "FAT_FAT16FixedCache"); 
    85 static const TUint32 KDef_FAT16_FixedCache_Read_Granularity_LOG2 = 12;  //-- read granularity  Log2, 4K
    86 static const TUint32 KDef_FAT16_FixedCache_Write_Granularity_LOG2 = 9;  //-- write granularity Log2, 512 bytes    
    87 
    88 
    89 //-- Fat32 LRU cache
    90 //-- FAT_FAT32LruCache <Size KB>,<Log2(read granularity)>,<Log2(Write granularity)> 
    91 //-- e.g:   FAT_FAT32LruCache 128,12,9
    92 _LIT8(KPN_FAT_FAT32LruCache, "FAT_FAT32LruCache"); 
    93 
    94 
    95 static const TUint32 KDef_FAT32_LruCache_SizeKB = 128;                  //-- LRU cache size, 128KB
    96 static const TUint32 KDef_FAT32_LruCache_Read_Granularity_LOG2 = 12;    //-- read granularity  Log2, 4K     
    97 static const TUint32 KDef_FAT32_LruCache_Write_Granularity_LOG2 = 9;    //-- write granularity Log2, 512 bytes      
    98 
    99 
   100 //=======================================================================================================================
   101 //-- FAT leaf directory cache settings
   102 //=======================================================================================================================
   103 //-- A leaf directory cache for Fat volumes
   104 _LIT8(KPN_FAT_LeafDirCache, "FAT_LeafDirCacheSize"); 
   105 static const TUint32 KDef_KLeafDirCacheSize = 1;    //-- default number of the most recently visited leaf dirs to be cached
   106 
   107 //=======================================================================================================================
   108 //-- New directory cache settings
   109 //=======================================================================================================================
   110 //-- New directory cache uses the global cache memory manager for dynamic size allocation
   111 _LIT8(KPN_FAT_DynamicDirCacheMin, "FAT_DirCacheSizeMin"); 
   112 _LIT8(KPN_FAT_DynamicDirCacheMax, "FAT_DirCacheSizeMax"); 
   113 static const TUint32 KDef_DynamicDirCacheMin = 128;		// default minimum fat dir cache size in KB
   114 static const TUint32 KDef_DynamicDirCacheMax = 256;		// default maximum fat dir cache size in KB
   115 static const TUint32 KDef_MaxDynamicDirCachePageSzLog2 = 14;    // default value for directory cache single page 
   116                                                                 //  maximal size Log2, 2^14 (16K) by default
   117 
   118 
   119 
   120 
   121 
   122 
   123 
   124 
   125 //########################################################################################################################
   126 
   127 
   128 
   129 //-----------------------------------------------------------------------------
   130 /**
   131     Extract a token from the comma-separated tokens string.
   132 
   133     @param  aSrc        source string descriptor
   134     @param  aTokenNo    token number to extract (the 1st token is number 0)
   135     @param  aToken      on success there will be extracted token
   136     
   137     @return ETrue on success and an extracted token in aToken
   138 */
   139 static TBool GetCSV_Token(const TDesC8& aSrc, TUint aTokenNo, TPtrC8& aToken)
   140     {
   141 
   142     const TChar chDelim = ','; //-- token delimiter, comma
   143     TInt nRes;
   144     
   145     //-- 1. find the beginning of the token we are looking for
   146     TPtrC8 ptrCurrToken(aSrc);
   147     for(TUint i=0; i<aTokenNo; ++i)
   148         {
   149         const TInt len = ptrCurrToken.Length();
   150 
   151         nRes = ptrCurrToken.Locate(chDelim);
   152         if(nRes == KErrNotFound)
   153             return EFalse; //-- did tot find the required token
   154 
   155         ptrCurrToken.Set(ptrCurrToken.Right(len-nRes-1));
   156         }
   157 
   158     //-- 2. set the end position of the token we found
   159     aToken.Set(ptrCurrToken);
   160 
   161     nRes = ptrCurrToken.Locate(chDelim);
   162     if(nRes != KErrNotFound)
   163         {
   164         aToken.Set(ptrCurrToken.Left(nRes));
   165         }
   166 
   167     return ETrue;
   168     }
   169 
   170 
   171 //-----------------------------------------------------------------------------
   172 /**
   173     Extract a token from the comma-separated tokens string and try to convert it into TInt
   174 
   175     @param  aSrc        source string descriptor
   176     @param  aTokenNo    token number to extract (the 1st token is number 0)
   177     @param  aVal        On success will contain the value represented by the token
   178 
   179     @return ETrue if the token is extracted and converted to TInt OK, in this case aVal contains value
   180 */
   181 static TBool GetIntTokenVal(const TDesC8& aSrc, TUint aTokenNo, TInt& aVal)
   182 {
   183     TPtrC8      ptrToken;
   184     TLex8       lex;
   185     TInt        val;
   186 
   187     if(!GetCSV_Token(aSrc, aTokenNo, ptrToken))
   188         return EFalse;
   189 
   190     lex.Assign(ptrToken);
   191     lex.SkipSpace();
   192     
   193     if(lex.Val(val) != KErrNone)
   194         return EFalse;
   195 
   196     aVal = val;
   197     return ETrue;
   198 }
   199 
   200 static TBool GetIntTokenVal(const TDesC8& aSrc, TUint aTokenNo, TUint16& aVal)
   201 {
   202     TInt  val;
   203     if(!GetIntTokenVal(aSrc, aTokenNo, val))
   204         return EFalse;
   205 
   206     aVal = (TUint16)val;
   207     return ETrue;
   208 }
   209 
   210 
   211 //-----------------------------------------------------------------------------
   212 TFatConfig::TFatConfig()
   213     {
   214     iInitialised = EFalse;
   215     }
   216 
   217 //-----------------------------------------------------------------------------
   218 
   219 /**
   220     reads FAT parameters from estart.txt file.
   221     @param  aDrvNumber a valid drive number
   222     @param  aForceRead if ETrue parameters will be forcedly re-read.
   223 */
   224 void TFatConfig::ReadConfig(TInt aDrvNumber, TBool aForceRead /*=EFalse*/)
   225     {
   226     if(aForceRead)
   227         iInitialised = EFalse;
   228 
   229     if(iInitialised)
   230         return;
   231 
   232     //__PRINT1(_L("#>- TFatConfig::ReadConfig() drive:%d\n"),aDrvNumber);
   233     ASSERT(aDrvNumber >= EDriveA && aDrvNumber <= EDriveZ);
   234     
   235     //-- make a section name, like "DriveX"
   236     TBuf8<32> section;
   237     section.Format(_L8("Drive%c"), 'A'+aDrvNumber);
   238     
   239     //-- read values from estart.txt and cache them
   240     iScanDrvSkipFinalisedVolume = ReadUint(section, KPN_ScanDrvSkipFinalisedVolume, KDef_ScanDrvSkipFinalisedVolume);
   241     iFAT32_AsynchMount          = ReadUint(section, KPN_FAT32_AsynchMount,          KDef_FAT32_AsynchMount);
   242     iFAT32_UseFSInfoOnMount     = ReadUint(section, KPN_FAT32_UseFsInfoOnMount,     KDef_FAT32_UseFsInfoOnMount);
   243     iFAT32_UseBitSupercache     = ReadUint(section, KPN_FAT32_UseFatBitSupercache,  KDef_FAT32_UseFatBitSupercache);
   244     iFAT16_UseCleanShutDownBit  = ReadUint(section, KPN_FAT16_UseCleanShutDownBit, KDef_FAT16_UseCleanShutDownBit);
   245 
   246     // If leaf dir cache is supported, read the configuration from estart.txt file
   247     iLeafDirCacheSize			= ReadUint(section, KPN_FAT_LeafDirCache,  			KDef_KLeafDirCacheSize);
   248     ProcessDynamicDirCacheParams(section);	//-- read dynamic dir cache parameters;
   249 
   250     ProcessDirCacheParams(section); //-- read FAT directory cache parameters
   251     ProcessFatCacheParams(section); //-- read FAT cache parameters
   252 
   253     iInitialised = ETrue;
   254 
   255     DumpParameters(); //-- print out parameters in debug mode
   256     }
   257 
   258 
   259 //-----------------------------------------------------------------------------
   260 /** 
   261     process dynamic directory cache parameters 
   262     @param aSection section name, like "DriveX"
   263 */
   264 void TFatConfig::ProcessDynamicDirCacheParams(const TDesC8& aSection)
   265 	{
   266 	// we have to process the data in this file as the default values are all static variables, which means
   267 	//  their scope is limited within this file only.
   268     iDynamicDirCacheMaxPageSizeLog2 = KDef_MaxDynamicDirCachePageSzLog2; 
   269     iDynamicDirCacheSizeMinKB = ReadUint(aSection, KPN_FAT_DynamicDirCacheMin, KDef_DynamicDirCacheMin);
   270     iDynamicDirCacheSizeMaxKB = ReadUint(aSection, KPN_FAT_DynamicDirCacheMax, KDef_DynamicDirCacheMax);
   271 
   272     // if less than default values, set to default values
   273     if (iDynamicDirCacheSizeMinKB < KDef_DynamicDirCacheMin)
   274     	iDynamicDirCacheSizeMinKB = KDef_DynamicDirCacheMin;
   275     if (iDynamicDirCacheSizeMaxKB < KDef_DynamicDirCacheMax)
   276     	iDynamicDirCacheSizeMaxKB = KDef_DynamicDirCacheMax;
   277 
   278     // validate settings for those values that the default values does not apply onto them
   279     __ASSERT_ALWAYS(iDynamicDirCacheSizeMinKB <= iDynamicDirCacheSizeMaxKB, Fault(EFatBadParameter));
   280 	}
   281 
   282 /** 
   283     process directory cache parameters 
   284     @param aSection section name, like "DriveX"
   285 */
   286 void TFatConfig::ProcessDirCacheParams(const TDesC8& aSection)
   287     {
   288     TBuf8<128>  buf;
   289 
   290     //-- set default values.
   291     iDirCacheSizeKB = KDef_KDirCacheSizeKB;
   292     iDirCacheMaxPageSizeLog2 = KDef_MaxDirCachePageSzLog2;
   293 
   294     //-- read a string containing DirCache parameters
   295     //-- it looks like this: FAT_DirCache cacheSzKB, maxPageSzBytes
   296     if(!F32Properties::GetString(aSection, KPN_FAT_DirCache, buf))
   297         return;
   298     
   299     GetIntTokenVal(buf, 0, iDirCacheSizeKB);          //-- 1. extract directory cache size in KB. this is token 0
   300     GetIntTokenVal(buf, 1, iDirCacheMaxPageSizeLog2); //-- 2. extract directory cache max page size in bytes Log2. this is token 1  
   301     }
   302 
   303 //-----------------------------------------------------------------------------
   304 /** 
   305     process fat cache parameters 
   306     @param aSection section name, like "DriveX"
   307 */
   308 void TFatConfig::ProcessFatCacheParams(const TDesC8& aSection)
   309 {
   310     TBuf8<128>  buf;
   311     //-- set default values.
   312     iFat16FixedCacheReadGrLog2 = KDef_FAT16_FixedCache_Read_Granularity_LOG2;
   313     iFat16FixedCacheWriteGrLog2 = KDef_FAT16_FixedCache_Write_Granularity_LOG2;
   314 
   315     iFat32LRUCacheSizeKB = KDef_FAT32_LruCache_SizeKB;
   316     iFat32LRUCacheReadGrLog2 = KDef_FAT32_LruCache_Read_Granularity_LOG2;
   317     iFat32LRUCacheWriteGrLog2 = KDef_FAT32_LruCache_Write_Granularity_LOG2;
   318 
   319     //-- read a string containing FAT16 fixed cache parameters
   320     if(F32Properties::GetString(aSection, KPN_FAT_FAT16FixedCache, buf))
   321     {
   322         GetIntTokenVal(buf, 0, iFat16FixedCacheReadGrLog2);  //-- 1. extract Log2(FAT16 fixed cache read granularity)
   323         GetIntTokenVal(buf, 1, iFat16FixedCacheWriteGrLog2); //-- 2. extract Log2(FAT16 fixed cache write granularity)
   324     }
   325 
   326     //-- read a string containing FAT32 LRU cache parameters
   327     if(F32Properties::GetString(aSection, KPN_FAT_FAT32LruCache, buf))
   328     {
   329         GetIntTokenVal(buf, 0, iFat32LRUCacheSizeKB);        //-- 1. extract FAT32 LRU cache size, Kbytes
   330         GetIntTokenVal(buf, 1, iFat32LRUCacheReadGrLog2);    //-- 2. extract Log2(FAT32 LRU cache read granularity)
   331         GetIntTokenVal(buf, 2, iFat32LRUCacheWriteGrLog2);   //-- 3. extract Log2(FAT32 LRU cache write granularity)
   332     }
   333 }
   334 
   335 
   336 //-----------------------------------------------------------------------------
   337 /**
   338     Just a helper method. Reads TUint32 value from the estart.txt
   339     
   340     @param  aSection        section name, e.g. [DriveD]
   341     @param  aKeyName        Key name
   342     @param  aDefaultValue   Default value to return in parameter isn't found
   343     @return a value from config file or default aDefaultValue
   344 */
   345 TUint32 TFatConfig::ReadUint(const TDesC8& aSection, const TDesC8& aKeyName, TUint32 aDefaultValue) const
   346     {
   347     TInt32 val = aDefaultValue;
   348 	// coverity[check_return] coverity[unchecked_value]
   349     F32Properties::GetInt(aSection, aKeyName, val);
   350 
   351     return val;
   352     }
   353 
   354 //-----------------------------------------------------------------------------
   355 /** Debug method, prints out the parameters*/
   356 void TFatConfig::DumpParameters() const
   357     {
   358 #ifdef _DEBUG
   359 
   360     ASSERT(iInitialised);
   361     __PRINT(_L("#>- TFatConfig parameters:\n"));
   362 
   363     DoDumpUintParam(KPN_ScanDrvSkipFinalisedVolume, iScanDrvSkipFinalisedVolume);
   364     DoDumpUintParam(KPN_FAT32_AsynchMount, iFAT32_AsynchMount);
   365     DoDumpUintParam(KPN_FAT32_UseFsInfoOnMount, iFAT32_UseFSInfoOnMount);
   366     DoDumpUintParam(KPN_FAT32_UseFatBitSupercache, iFAT32_UseBitSupercache);
   367     DoDumpUintParam(KPN_FAT16_UseCleanShutDownBit, iFAT16_UseCleanShutDownBit);
   368 
   369     DoDumpUintParam(_L8("FAT_DirCache Size, KB"), iDirCacheSizeKB);
   370     DoDumpUintParam(_L8("FAT_DirCache MaxPage Size Log2"), iDirCacheMaxPageSizeLog2);
   371 
   372     DoDumpUintParam(_L8("FAT_16Cache RdGr Log2"), iFat16FixedCacheReadGrLog2);
   373     DoDumpUintParam(_L8("FAT_16Cache WrGr Log2"), iFat16FixedCacheWriteGrLog2);
   374 
   375     DoDumpUintParam(_L8("FAT_32Cache Size, KB"),  iFat32LRUCacheSizeKB);
   376     DoDumpUintParam(_L8("FAT_32Cache RdGr Log2"), iFat32LRUCacheReadGrLog2);
   377     DoDumpUintParam(_L8("FAT_32Cache WrGr Log2"), iFat32LRUCacheWriteGrLog2);
   378 
   379     __PRINT(_L("#>------ end -------<#\n\n"));
   380 
   381 #endif
   382     }
   383 
   384 void TFatConfig::DoDumpUintParam(const TDesC8& aKeyName, TUint32 aParam) const
   385     {
   386 #ifdef _DEBUG
   387     TBuf<100> buf;
   388 
   389     buf.Copy(aKeyName);
   390     buf.Insert(0,_L("#>-  "));
   391     buf.AppendFormat(_L(": %d"), aParam);
   392     __PRINT(buf);
   393 #else
   394     (void)aKeyName; (void)aParam; //-- get rid of warning
   395 
   396 #endif
   397     }
   398 
   399 
   400 
   401 
   402 
   403 
   404 
   405 
   406 
   407 
   408 
   409 
   410 
   411 
   412 
   413 
   414 
   415 
   416 
   417 
   418 
   419 
   420 
   421 
   422 
   423