os/kernelhwsrv/userlibandfileserver/fileserver/sfat32/inc/fat_table32.h
author sl
Tue, 10 Jun 2014 14:32:02 +0200
changeset 1 260cb5ec6c19
permissions -rw-r--r--
Update contrib.
     1 // Copyright (c) 1998-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\sfat32\inc\fat_table32.h
    15 // FAT32 File Allocation Table classes definitions
    16 // 
    17 //
    18 
    19 /**
    20  @file
    21  @internalTechnology
    22 */
    23 
    24 #ifndef FAT_TABLE_32_H
    25 #define FAT_TABLE_32_H
    26 
    27 //---------------------------------------------------------------------------------------------------------------------------------------
    28 
    29 class CFatHelperThreadBase;
    30 class CFat32ScanThread;
    31 
    32 TInt FAT32_ScanThread(TAny* apHostObject);
    33 
    34 //---------------------------------------------------------------------------------------------------------------------------------------
    35 
    36 /**
    37     Fat table abstraction for all media types except RAM.
    38     Supports FAT12/16/32
    39 */
    40 class CAtaFatTable : public CFatTable
    41     {
    42 
    43 public:
    44     
    45     static CAtaFatTable* NewL(CFatMountCB& aOwner);
    46     ~CAtaFatTable();
    47 
    48     //-- overrides from th ebase class
    49     TUint32 ReadL(TUint32 aFatIndex) const;
    50     void WriteL(TUint32 aFatIndex, TUint32 aValue);
    51     void MountL(const TMountParams& aMountParam);
    52 
    53     TInt64 DataPositionInBytes(TUint32 aCluster) const;
    54 
    55     void InitializeL();
    56     void Dismount(TBool aDiscardDirtyData);
    57     void FlushL();
    58     
    59     void InvalidateCacheL();
    60     void InvalidateCacheL(TInt64 aPos, TUint32 aLength);
    61 
    62     TUint32 FreeClusterHint() const;
    63     TUint32 NumberOfFreeClusters(TBool aSyncOperation=EFalse) const;
    64 
    65     TBool ConsistentState() const;
    66 
    67     inline void AcquireLock() const {iDriveInteface.AcquireLock();} 
    68     inline void ReleaseLock() const {iDriveInteface.ReleaseLock();} 
    69     
    70     inline TDriveInterface& DriveInterface() const;
    71     inline CFatMountCB* OwnerMount() const;
    72 
    73 
    74 private:
    75     
    76     /** internal states of this object */
    77     enum TState
    78         {
    79         ENotInitialised,    ///< 0 initial invalid state
    80         EInitialised,       ///< 1 initialised, i.e. internal objects created, but unusable becasue number of free entries isn't known
    81         EMounting,          ///< 2 mounting started
    82         EMounted,           ///< 3 successfully mounted; number of free entries is known. This is the only consistent state.
    83         EDismounted,        ///< 4 FAT table object is dismounted
    84         EFreeClustersScan,  ///< 5 FAT32 scan thread is currently scanning FAT table for free clusters
    85         EMountAborted       ///< 6 Mounting failed, probably because of FAT32 free clusters scan thread failure.
    86         };
    87 
    88 public:
    89 
    90     /** A helper class used in FAT scanning for free clusters*/
    91     class TFatScanParam
    92         {
    93     public:
    94         inline TFatScanParam();
    95 
    96     public:
    97         TUint32 iEntriesScanned;        ///< total number of FAT entries scanned by DoParseFatBuf()
    98         TUint32 iFirstFree;             ///< first free FAT entry found 
    99         TUint32 iCurrFreeEntries;       ///< current number of free FAT entries found by DoParseFatBuf()
   100         TUint32 iCurrOccupiedEntries;   ///< current number of non-free FAT entries found by DoParseFatBuf()
   101         };
   102 
   103 
   104 private:
   105     CAtaFatTable(CFatMountCB& aOwner);
   106 
   107     void RequestRawWriteAccess(TInt64 aPos, TUint32 aLen) const;
   108     
   109     void SetFreeClusters(TUint32 aFreeClusters);
   110     void SetFreeClusterHint(TUint32 aCluster);
   111     void CountFreeClustersL();
   112     void CreateCacheL();
   113     
   114     virtual void DecrementFreeClusterCount(TUint32 aCount); 
   115     virtual void IncrementFreeClusterCount(TUint32 aCount);
   116     virtual TUint32 FindClosestFreeClusterL(TUint32 aCluster);
   117 
   118     void DoCountFreeClustersL();
   119     void DoParseFatBuf(const TPtrC8& aBuf, TFatScanParam& aScanParam) const;
   120 
   121     TBool RequestFreeClusters(TUint32 aClustersRequired) const;
   122     void DoLaunchFat32FreeSpaceScanThreadL();
   123     void DestroyHelperThread();
   124 
   125     inline TState State() const;
   126     inline void SetState(TState aState);
   127     
   128 private:
   129 
   130     
   131     CFatCacheBase*          iCache;         ///< FAT cache, fixed or LRU depending on the FAT type
   132     TDriveInterface&        iDriveInteface; ///< reference to the drive interface
   133     CFatHelperThreadBase*   ipHelperThread; ///< helper thread object pointer. NULL if it is not present
   134     TState                  iState;         ///< state of this object 
   135 
   136     //-- friends
   137     friend TInt FAT32_ScanThread(TAny* apHostObject);
   138     friend class CFat32ScanThread;
   139     friend class CFat32FreeSpaceScanner;
   140     friend class CFat32BitCachePopulator;
   141     };
   142 
   143 //---------------------------------------------------------------------------------------------------------------------------------------
   144 
   145 /**
   146     Fat table abstraction for RAM media type.
   147     Supports FAT16/32 only
   148 */
   149 class CRamFatTable : public CFatTable
   150     {
   151 public:
   152     
   153     static CRamFatTable* NewL(CFatMountCB& aOwner);
   154     void InitializeL();
   155     void MountL(const TMountParams& aMountParam);
   156 
   157 
   158     TUint32 ReadL(TUint32 aFatIndex) const;
   159     void WriteL(TUint32 aFatIndex, TUint32 aValue);
   160     TInt64 DataPositionInBytes(TUint32 aCluster) const;
   161     void FreeClusterListL(TUint32 aCluster);
   162     TUint32 AllocateSingleClusterL(TUint32 aNearestCluster);
   163     void ExtendClusterListL(TUint32 aNumber,TInt& aCluster);
   164     TUint32 AllocateClusterListL(TUint32 aNumber,TUint32 aNearestCluster);
   165 
   166 private:
   167     CRamFatTable(CFatMountCB& aOwner);
   168 
   169     inline TUint8 *RamDiskBase() const;
   170     inline TInt AllocateClusterNumber();
   171     inline void WriteFatTable(TInt aFatIndex,TInt aValue);
   172     inline void WriteFatTable(TInt aFatIndex,TInt aFatValue,TInt anIndirectionTableValue);
   173     inline void ReadIndirectionTable(TUint32& aCluster) const;
   174     inline void WriteIndirectionTable(TInt aFatIndex,TInt aValue);
   175     inline TUint8* MemCopy(TAny* aTrg,const TAny* aSrc,TInt aLength);
   176     inline TUint8* MemCopyFillZ(TAny* aTrg, TAny* aSrc, TInt aLength);
   177     inline void ZeroFillCluster(TInt aCluster);
   178     
   179     void UpdateIndirectionTable(TUint32 aStartCluster,TUint32 anEndCluster,TInt aNum);
   180 
   181 protected:
   182 
   183     TInt iFatTablePos;          ///< Current position in the fat table
   184     TInt iIndirectionTablePos;  ///< Current position in indirection table, second fat used for this
   185     TUint8* iRamDiskBase;       ///< Pointer to the Ram disk base
   186     };
   187 
   188 
   189 
   190 //---------------------------------------------------------------------------------------------------------------------------------------
   191 
   192 /**
   193     Abstract base class for the FAT32 helper threads.
   194     Provides basic functionality of the helper threads and interface to the owher CAtaFatTable.
   195 */
   196 class CFatHelperThreadBase : public CBase
   197 {
   198  public: 
   199     
   200     /** Possible types of the FAT32 helper threads */
   201     enum TFatHelperThreadType
   202         {
   203         EInvalidType,       ///< invalid type
   204         EFreeSpaceScanner,  ///< Free FAT32 entries counter, see CFat32FreeSpaceScanner
   205         EBitCachePopulator  ///< FAT32 bit supercache populating thread.
   206         };
   207     
   208     /** this object states, mostly related to the worker thread activity and results */
   209     enum TState
   210         {
   211         EInvalid,       ///< invalid initial state
   212         ENotStarted,    ///< the worker thread hasn't started yet
   213         EWorking,       ///< worker thread is working
   214         EFinished_OK,   ///< worker thread has successfully finished, everything is fine.
   215         EFailed         ///< worker thread failed to finish its job for some reason; see the thread completion status
   216         };
   217  
   218  public:
   219     ~CFatHelperThreadBase();
   220 
   221     //-- virtual interface
   222     virtual TFatHelperThreadType Type() const = 0;
   223     virtual TInt Launch()=0; 
   224     virtual void RequestFatEntryWriteAccess(TUint32 aFatIndex) const=0;
   225 
   226     //-- non-virtual interface for external user only
   227     void Close();
   228     void ForceStop(); 
   229     
   230     inline TState State() const;
   231 
   232     inline void Suspend() const;
   233     inline void Resume()  const;
   234     
   235     inline TInt  ThreadCompletionCode() const;
   236     inline TBool ThreadWorking() const;
   237     
   238     inline void BoostPriority(TBool aBoost) const;
   239     inline TBool IsPriorityBoosted() const;
   240 
   241     inline TThreadId ThreadId() const;
   242 
   243     TInt WaitToFinish() const;
   244 
   245  protected:
   246     CFatHelperThreadBase(CAtaFatTable& aOwner);
   247      
   248     //-- outlaws
   249     CFatHelperThreadBase();
   250     CFatHelperThreadBase(const CFatHelperThreadBase&);
   251     CFatHelperThreadBase& operator=(const CFatHelperThreadBase&);
   252 
   253     /** the worker thread priorities values */
   254     enum 
   255         {
   256         EHelperPriorityNormal  = EPriorityMuchLess, ///< FAT32 Helper thread normal priority (assigned on start)
   257         EHelperPriorityBoosted = EPriorityNormal    ///< FAT32 Helper thread bosted priority
   258         };                                   
   259 
   260     TInt DoLaunchThread(TThreadFunction aFunction, TAny* aThreadParameter);
   261 
   262     inline void SetState(TState aState);
   263     inline TBool AllowedToLive() const;
   264     inline void  AllowToLive(TBool aAllow);
   265 
   266  protected:
   267     CAtaFatTable&           iOwner;         ///< owner, CAtaFatTable
   268 
   269  private:
   270     TState                  iState;         ///< internal state of this object
   271     RThread                 iThread;        ///< FAT helper thread handle
   272     mutable TRequestStatus  iThreadStatus;  ///< helper thread logon status
   273     
   274             TBool           iAllowedToLive   : 1;    ///< if EFalse the worker thread must gracefully finish ASAP.
   275     mutable TBool           iPriorityBoosted : 1;    ///< ETrue when thread priority is boosted by BoostPriority() call
   276 
   277 
   278 };
   279 
   280 //---------------------------------------------------------------------------------------------------------------------------------------
   281 /**
   282     Abstract base class for the FAT32 helper threads that read FAT by big chunks of data and parse it.
   283     Provides basic functionality of the helper threads and interface to the owher CAtaFatTable.
   284 */
   285 class CFat32ScanThread : public CFatHelperThreadBase
   286 {
   287  public:   
   288 
   289     virtual TInt Launch();
   290 
   291  protected:
   292     CFat32ScanThread(CAtaFatTable& aOwner);
   293     
   294     //-- virtual private interface for the thread function.
   295     virtual TInt Thread_Preamble();
   296     virtual TInt Thread_Postamble(TInt aResult);
   297     virtual TInt Thread_ProcessCollectedFreeEntries(const CAtaFatTable::TFatScanParam& aFatScanParam)=0;    
   298 
   299     friend TInt FAT32_ScanThread(TAny* apHostObject); ///< FAT32 scanner thread function, generic functionality for derived classes
   300 
   301  protected:
   302      
   303     RBuf8   iFatChunkBuf; ///< a buffer for reading FAT directly from the media
   304 
   305     TTime   iTimeStart;   ///< thread start time, used to measure how long thread worked
   306     TTime   iTimeEnd;     ///< thread end time, used to measure how long thread worked
   307 
   308     CFatBitCache *ipFatBitCache; ///< interface to the FAT bit supercache (if it is present)
   309 };
   310 
   311 
   312 //---------------------------------------------------------------------------------------------------------------------------------------
   313 /**
   314     FAT32 free FAT entries scanner thread.
   315     Represents transient FAT32 helper thread that can be launched on FAT table object mounting stage and will be 
   316     counting free FAT entries in order to find out free space on the volume.
   317 */
   318 class CFat32FreeSpaceScanner : public CFat32ScanThread
   319 {
   320  public:   
   321 
   322     static CFat32FreeSpaceScanner* NewL(CAtaFatTable& aOwner);
   323 
   324     virtual inline TFatHelperThreadType Type() const;
   325 
   326  private:
   327     CFat32FreeSpaceScanner(CAtaFatTable& aOwner);
   328     
   329     void RequestFatEntryWriteAccess(TUint32 aFatIndex) const;
   330 
   331     //-- virtual private interface for the thread function.
   332     TInt Thread_Preamble();
   333     TInt Thread_Postamble(TInt aResult);
   334     TInt Thread_ProcessCollectedFreeEntries(const CAtaFatTable::TFatScanParam& aFatScanParam);    
   335     //--
   336 
   337  private:
   338     
   339     void SetClustersScanned(TUint32 aClusters);
   340     TUint32 ClustersScanned() const;
   341     
   342     friend TInt FAT32_ScanThread(TAny* apHostObject); ///< FAT32 scanner thread function, generic functionality for CFat32ScanThread derived classes
   343 
   344  private:
   345 
   346     enum 
   347         {
   348         KFatChunkBufSize_Small = 16*K1KiloByte, //-- buffer size for reading small FAT tables
   349         KFatChunkBufSize_Big   = 64*K1KiloByte, //-- buffer size for reading large FAT tables
   350         
   351         KBigSzFat_Threshold    = 2*K1MegaByte,  //-- if FAT table size > this value, larger FAT read chunk (KFatChunkBufSize_Big) will be used
   352         }; 
   353 
   354     TUint32  iClustersScanned;  ///<  Number of FAT entries already scanned by the thread. Counts from the beginning of the FAT
   355 
   356     //-- volume space treshold in bytes that causes CMountCB::SetDiskSpaceChange() to be called by FAT32 free space scanner thread.
   357     //-- This thread will be calling CMountCB::SetDiskSpaceChange() after processing number of FAT32 entries corresponding to
   358     //-- this amount of space in FAT clusters. e.g. after processing amount of FAT32 entries comprising 256MB volume space
   359     enum {KVolSpaceNotifyThreshold = 256 * K1MegaByte}; 
   360 
   361     TUint32 iEntriesNotifyThreshold; ///< the value of FAT32 entries need to be counted for CMountCB::SetDiskSpaceChange() call
   362     TUint32 iNfyThresholdInc;        ///< Threshold increment in FAT32 entries. 
   363 
   364     
   365 };
   366 
   367 //---------------------------------------------------------------------------------------------------------------------------------------
   368 /**
   369     FAT32 Bit supercache populating thread.
   370     Represents transient FAT32 helper thread that is populating bit supercache in backgroud.
   371 */
   372 class CFat32BitCachePopulator : public CFat32ScanThread
   373 {
   374  public:   
   375 
   376     static CFat32BitCachePopulator* NewL(CAtaFatTable& aOwner);
   377 
   378     virtual inline TFatHelperThreadType Type() const;
   379 
   380  private:
   381     CFat32BitCachePopulator(CAtaFatTable& aOwner);
   382     
   383     void RequestFatEntryWriteAccess(TUint32 aFatIndex) const;
   384 
   385     //-- virtual private interface for the thread function.
   386     TInt Thread_Preamble();
   387     TInt Thread_Postamble(TInt aResult);
   388     TInt Thread_ProcessCollectedFreeEntries(const CAtaFatTable::TFatScanParam& aFatScanParam);    
   389     //--
   390 
   391  private:
   392     friend TInt FAT32_ScanThread(TAny* apHostObject); ///< FAT32 scanner thread function, generic functionality for CFat32ScanThread derived classes
   393     enum {KFatChunkBufSize = 16*K1KiloByte}; //-- buffer size for FAT reading 
   394 
   395  private:
   396     TUint32  iTotalOccupiedFatEntries; ///< total counted number of non-free FAT entries
   397 
   398 };
   399 
   400 
   401 //---------------------------------------------------------------------------------------------------------------------------------------
   402 
   403 #include "fat_table32.inl"
   404 
   405 #endif //FAT_TABLE_32_H
   406 
   407 
   408 
   409 
   410 
   411 
   412 
   413 
   414 
   415 
   416 
   417 
   418 
   419 
   420 
   421 
   422 
   423 
   424 
   425 
   426 
   427 
   428