userlibandfileserver/fileserver/sfat32/inc/fat_table32.h
changeset 0 a41df078684a
child 6 0173bcd7697c
equal deleted inserted replaced
-1:000000000000 0:a41df078684a
       
     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 TFatDriveInterface& 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     TFatDriveInterface&    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 
       
   165 private:
       
   166     CRamFatTable(CFatMountCB& aOwner);
       
   167 
       
   168     inline TUint8 *RamDiskBase() const;
       
   169     inline TInt AllocateClusterNumber();
       
   170     inline void WriteFatTable(TInt aFatIndex,TInt aValue);
       
   171     inline void WriteFatTable(TInt aFatIndex,TInt aFatValue,TInt anIndirectionTableValue);
       
   172     inline void ReadIndirectionTable(TUint32& aCluster) const;
       
   173     inline void WriteIndirectionTable(TInt aFatIndex,TInt aValue);
       
   174     inline TUint8* MemCopy(TAny* aTrg,const TAny* aSrc,TInt aLength);
       
   175     inline TUint8* MemCopyFillZ(TAny* aTrg, TAny* aSrc, TInt aLength);
       
   176     inline void ZeroFillCluster(TInt aCluster);
       
   177     
       
   178     void UpdateIndirectionTable(TUint32 aStartCluster,TUint32 anEndCluster,TInt aNum);
       
   179 
       
   180 protected:
       
   181 
       
   182     TInt iFatTablePos;          ///< Current position in the fat table
       
   183     TInt iIndirectionTablePos;  ///< Current position in indirection table, second fat used for this
       
   184     TUint8* iRamDiskBase;       ///< Pointer to the Ram disk base
       
   185     };
       
   186 
       
   187 
       
   188 
       
   189 //---------------------------------------------------------------------------------------------------------------------------------------
       
   190 
       
   191 /**
       
   192     Abstract base class for the FAT32 helper threads.
       
   193     Provides basic functionality of the helper threads and interface to the owher CAtaFatTable.
       
   194 */
       
   195 class CFatHelperThreadBase : public CBase
       
   196 {
       
   197  public: 
       
   198     
       
   199     /** Possible types of the FAT32 helper threads */
       
   200     enum TFatHelperThreadType
       
   201         {
       
   202         EInvalidType,       ///< invalid type
       
   203         EFreeSpaceScanner,  ///< Free FAT32 entries counter, see CFat32FreeSpaceScanner
       
   204         EBitCachePopulator  ///< FAT32 bit supercache populating thread.
       
   205         };
       
   206     
       
   207     /** this object states, mostly related to the worker thread activity and results */
       
   208     enum TState
       
   209         {
       
   210         EInvalid,       ///< invalid initial state
       
   211         ENotStarted,    ///< the worker thread hasn't started yet
       
   212         EWorking,       ///< worker thread is working
       
   213         EFinished_OK,   ///< worker thread has successfully finished, everything is fine.
       
   214         EFailed         ///< worker thread failed to finish its job for some reason; see the thread completion status
       
   215         };
       
   216  
       
   217  public:
       
   218     ~CFatHelperThreadBase();
       
   219 
       
   220     //-- virtual interface
       
   221     virtual TFatHelperThreadType Type() const = 0;
       
   222     virtual TInt Launch()=0; 
       
   223     virtual void RequestFatEntryWriteAccess(TUint32 aFatIndex) const=0;
       
   224 
       
   225     //-- non-virtual interface for external user only
       
   226     void Close();
       
   227     void ForceStop(); 
       
   228     
       
   229     inline TState State() const;
       
   230 
       
   231     inline void Suspend() const;
       
   232     inline void Resume()  const;
       
   233     
       
   234     inline TInt  ThreadCompletionCode() const;
       
   235     inline TBool ThreadWorking() const;
       
   236     
       
   237     inline void BoostPriority(TBool aBoost) const;
       
   238     inline TBool IsPriorityBoosted() const;
       
   239 
       
   240     inline TThreadId ThreadId() const;
       
   241 
       
   242     TInt WaitToFinish() const;
       
   243 
       
   244  protected:
       
   245     CFatHelperThreadBase(CAtaFatTable& aOwner);
       
   246      
       
   247     //-- outlaws
       
   248     CFatHelperThreadBase();
       
   249     CFatHelperThreadBase(const CFatHelperThreadBase&);
       
   250     CFatHelperThreadBase& operator=(const CFatHelperThreadBase&);
       
   251 
       
   252     /** the worker thread priorities values */
       
   253     enum 
       
   254         {
       
   255         EHelperPriorityNormal  = EPriorityMuchLess, ///< FAT32 Helper thread normal priority (assigned on start)
       
   256         EHelperPriorityBoosted = EPriorityNormal    ///< FAT32 Helper thread bosted priority
       
   257         };                                   
       
   258 
       
   259     TInt DoLaunchThread(TThreadFunction aFunction, TAny* aThreadParameter);
       
   260 
       
   261     inline void SetState(TState aState);
       
   262     inline TBool AllowedToLive() const;
       
   263     inline void  AllowToLive(TBool aAllow);
       
   264 
       
   265  protected:
       
   266     CAtaFatTable&           iOwner;         ///< owner, CAtaFatTable
       
   267 
       
   268  private:
       
   269     TState                  iState;         ///< internal state of this object
       
   270     RThread                 iThread;        ///< FAT helper thread handle
       
   271     mutable TRequestStatus  iThreadStatus;  ///< helper thread logon status
       
   272     
       
   273             TBool           iAllowedToLive   : 1;    ///< if EFalse the worker thread must gracefully finish ASAP.
       
   274     mutable TBool           iPriorityBoosted : 1;    ///< ETrue when thread priority is boosted by BoostPriority() call
       
   275 
       
   276 
       
   277 };
       
   278 
       
   279 //---------------------------------------------------------------------------------------------------------------------------------------
       
   280 /**
       
   281     Abstract base class for the FAT32 helper threads that read FAT by big chunks of data and parse it.
       
   282     Provides basic functionality of the helper threads and interface to the owher CAtaFatTable.
       
   283 */
       
   284 class CFat32ScanThread : public CFatHelperThreadBase
       
   285 {
       
   286  public:   
       
   287 
       
   288     virtual TInt Launch();
       
   289 
       
   290  protected:
       
   291     CFat32ScanThread(CAtaFatTable& aOwner);
       
   292     
       
   293     //-- virtual private interface for the thread function.
       
   294     virtual TInt Thread_Preamble();
       
   295     virtual TInt Thread_Postamble(TInt aResult);
       
   296     virtual TInt Thread_ProcessCollectedFreeEntries(const CAtaFatTable::TFatScanParam& aFatScanParam)=0;    
       
   297 
       
   298     friend TInt FAT32_ScanThread(TAny* apHostObject); ///< FAT32 scanner thread function, generic functionality for derived classes
       
   299 
       
   300  protected:
       
   301      
       
   302     RBuf8   iFatChunkBuf; ///< a buffer for reading FAT directly from the media
       
   303 
       
   304     TTime   iTimeStart;   ///< thread start time, used to measure how long thread worked
       
   305     TTime   iTimeEnd;     ///< thread end time, used to measure how long thread worked
       
   306 
       
   307     CFatBitCache *ipFatBitCache; ///< interface to the FAT bit supercache (if it is present)
       
   308 };
       
   309 
       
   310 
       
   311 //---------------------------------------------------------------------------------------------------------------------------------------
       
   312 /**
       
   313     FAT32 free FAT entries scanner thread.
       
   314     Represents transient FAT32 helper thread that can be launched on FAT table object mounting stage and will be 
       
   315     counting free FAT entries in order to find out free space on the volume.
       
   316 */
       
   317 class CFat32FreeSpaceScanner : public CFat32ScanThread
       
   318 {
       
   319  public:   
       
   320 
       
   321     static CFat32FreeSpaceScanner* NewL(CAtaFatTable& aOwner);
       
   322 
       
   323     virtual inline TFatHelperThreadType Type() const;
       
   324 
       
   325  private:
       
   326     CFat32FreeSpaceScanner(CAtaFatTable& aOwner);
       
   327     
       
   328     void RequestFatEntryWriteAccess(TUint32 aFatIndex) const;
       
   329 
       
   330     //-- virtual private interface for the thread function.
       
   331     TInt Thread_Preamble();
       
   332     TInt Thread_Postamble(TInt aResult);
       
   333     TInt Thread_ProcessCollectedFreeEntries(const CAtaFatTable::TFatScanParam& aFatScanParam);    
       
   334     //--
       
   335 
       
   336  private:
       
   337     
       
   338     void SetClustersScanned(TUint32 aClusters);
       
   339     TUint32 ClustersScanned() const;
       
   340     
       
   341     friend TInt FAT32_ScanThread(TAny* apHostObject); ///< FAT32 scanner thread function, generic functionality for CFat32ScanThread derived classes
       
   342 
       
   343  private:
       
   344 
       
   345     enum 
       
   346         {
       
   347         KFatChunkBufSize_Small = 16*K1KiloByte, //-- buffer size for reading small FAT tables
       
   348         KFatChunkBufSize_Big   = 64*K1KiloByte, //-- buffer size for reading large FAT tables
       
   349         
       
   350         KBigSzFat_Threshold    = 2*K1MegaByte,  //-- if FAT table size > this value, larger FAT read chunk (KFatChunkBufSize_Big) will be used
       
   351         }; 
       
   352 
       
   353     TUint32  iClustersScanned;  ///<  Number of FAT entries already scanned by the thread. Counts from the beginning of the FAT
       
   354 
       
   355     //-- volume space treshold in bytes that causes CMountCB::SetDiskSpaceChange() to be called by FAT32 free space scanner thread.
       
   356     //-- This thread will be calling CMountCB::SetDiskSpaceChange() after processing number of FAT32 entries corresponding to
       
   357     //-- this amount of space in FAT clusters. e.g. after processing amount of FAT32 entries comprising 256MB volume space
       
   358     enum {KVolSpaceNotifyThreshold = 256 * K1MegaByte}; 
       
   359 
       
   360     TUint32 iEntriesNotifyThreshold; ///< the value of FAT32 entries need to be counted for CMountCB::SetDiskSpaceChange() call
       
   361     TUint32 iNfyThresholdInc;        ///< Threshold increment in FAT32 entries. 
       
   362 
       
   363     
       
   364 };
       
   365 
       
   366 //---------------------------------------------------------------------------------------------------------------------------------------
       
   367 /**
       
   368     FAT32 Bit supercache populating thread.
       
   369     Represents transient FAT32 helper thread that is populating bit supercache in backgroud.
       
   370 */
       
   371 class CFat32BitCachePopulator : public CFat32ScanThread
       
   372 {
       
   373  public:   
       
   374 
       
   375     static CFat32BitCachePopulator* NewL(CAtaFatTable& aOwner);
       
   376 
       
   377     virtual inline TFatHelperThreadType Type() const;
       
   378 
       
   379  private:
       
   380     CFat32BitCachePopulator(CAtaFatTable& aOwner);
       
   381     
       
   382     void RequestFatEntryWriteAccess(TUint32 aFatIndex) const;
       
   383 
       
   384     //-- virtual private interface for the thread function.
       
   385     TInt Thread_Preamble();
       
   386     TInt Thread_Postamble(TInt aResult);
       
   387     TInt Thread_ProcessCollectedFreeEntries(const CAtaFatTable::TFatScanParam& aFatScanParam);    
       
   388     //--
       
   389 
       
   390  private:
       
   391     friend TInt FAT32_ScanThread(TAny* apHostObject); ///< FAT32 scanner thread function, generic functionality for CFat32ScanThread derived classes
       
   392     enum {KFatChunkBufSize = 16*K1KiloByte}; //-- buffer size for FAT reading 
       
   393 
       
   394  private:
       
   395     TUint32  iTotalOccupiedFatEntries; ///< total counted number of non-free FAT entries
       
   396 
       
   397 };
       
   398 
       
   399 
       
   400 //---------------------------------------------------------------------------------------------------------------------------------------
       
   401 
       
   402 #include "fat_table32.inl"
       
   403 
       
   404 #endif //FAT_TABLE_32_H
       
   405 
       
   406 
       
   407 
       
   408 
       
   409 
       
   410 
       
   411 
       
   412 
       
   413 
       
   414 
       
   415 
       
   416 
       
   417 
       
   418 
       
   419 
       
   420 
       
   421 
       
   422 
       
   423 
       
   424 
       
   425 
       
   426 
       
   427