os/kernelhwsrv/userlibandfileserver/fileserver/sfat32/sl_fatcache.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\sfat\sl_facache.h
    15 // FAT cache base classes definition
    16 // FAT12 and FAT16 cache classes definition
    17 // 
    18 //
    19 
    20 /**
    21  @file
    22  @internalTechnology
    23 */
    24 
    25 #ifndef SL_FAT_CACHE_H
    26 #define SL_FAT_CACHE_H
    27 
    28 
    29 //-----------------------------------------------------------------------------
    30 
    31 class CFatBitCache;
    32 
    33 /**
    34     An abstract base class for all types of FAT caches.
    35     Provides user interface and some common for all types of FAT caches functionality.
    36 */
    37 class CFatCacheBase : public CBase
    38 {
    39  public:
    40 
    41     virtual ~CFatCacheBase();
    42 
    43     //-- public interface
    44     virtual void Close(TBool /*aDiscardDirtyData*/) {};
    45     virtual void FlushL() = 0;
    46 
    47     virtual TUint32 ReadEntryL(TUint32 aIndex) = 0;
    48     virtual void WriteEntryL(TUint32 aIndex, TUint32 aEntry) = 0;
    49     
    50     virtual TInt Invalidate() = 0;
    51     virtual TInt InvalidateRegion(TUint32 aStartEntry, TUint32 aNumEntries) = 0;
    52 
    53     TInt ReadFatData(TUint32 aPos, TUint32 aLen, TDes8& aData) const;
    54     TInt WriteFatData(TUint32 aPos, const TDesC8& aData) const;
    55 
    56     inline TUint32  FatStartPos() const;
    57     inline TUint32  FatSize() const;
    58     inline TFatType FatType() const;
    59 
    60  public:
    61     
    62     //-- auxilary interface to additional bit supercache (it may exist only in FAT32 cache implementation)
    63     virtual CFatBitCache* BitCacheInterface();
    64 
    65 
    66  protected:
    67     CFatCacheBase();
    68 
    69     virtual void InitialiseL(CFatMountCB* aOwner);
    70 
    71     inline TBool IsDirty() const;
    72     inline void SetDirty(TBool aDirty);
    73     inline TUint NumFATs() const;
    74 
    75     TBool CheckInvalidatingDirtyCache() const;
    76 
    77     inline TUint FAT_SectorSzLog2() const;
    78     inline TUint FAT_SectorSz() const; 
    79     inline TUint FAT_ClusterSzLog2() const;
    80 
    81  protected:
    82     
    83     enum {KInvalidFatNo = 0xFF}; ///< used to invalidate current FAT no.
    84     TUint   iCurrentFatNo;       ///< current FAT number WriteFatData will write to.
    85 
    86  private:    
    87     //-- values cached from owning mount.
    88     TUint32     iFatStartPos;   ///< media position of FAT1 start 
    89     TUint32     iFatSize;       ///< size of FAT in bytes
    90     TUint16     iNumFATs;       ///< number of FATs on the volume
    91     TUint16     iFatSecSzLog2;  ///< Log2(FAT Sector size)
    92     TUint16     iFatClustSzLog2;///< Log2(FAT cluster size)
    93     TFatType    iFatType;       ///< FAT type
    94     TDriveInterface* ipDrive;///< interface to the media driver
    95     //---
    96 
    97     TBool       iDirty;         ///< ETrue if the cache is dirty
    98 };
    99 
   100 
   101 //-----------------------------------------------------------------------------
   102 
   103 /**
   104     Fixed FAT12 cache. This is a contiguous cache that caches whole FAT12.
   105     This cache is logically divided to sectors, maximal number of sectors in this cache is KMaxSectorsInCache (32).
   106     
   107     Read granularity: whole cache; anyway it can't be larger than 6126 bytes.
   108     Write granularity: cache sector size, which is always "FAT Sector Size" and non-configurable.
   109 */
   110 class CFat12Cache : public CFatCacheBase
   111 {
   112  public:
   113     static CFat12Cache* NewL(CFatMountCB* aOwner, TUint32 aFatSize);
   114 
   115     //-- overrides from base class
   116     virtual void Close(TBool aDiscardDirtyData);
   117     virtual void FlushL();
   118 
   119     virtual TUint32 ReadEntryL(TUint32 aIndex);
   120     virtual void WriteEntryL(TUint32 aIndex, TUint32 aEntry);
   121 
   122     virtual TInt Invalidate();
   123     virtual TInt InvalidateRegion(TUint32 aStartEntry, TUint32 aNumEntries);
   124     //------------------------------------
   125 
   126  private:
   127     
   128     void InitialiseL(CFatMountCB* aOwner, TUint32 aFatSize); 
   129 
   130     CFat12Cache();
   131     CFat12Cache(const CFat12Cache&);
   132     CFat12Cache& operator=(const CFat12Cache&);
   133 
   134 
   135     inline TUint32 NumSectors() const;
   136     void AssertCacheReallyClean() const;
   137 
   138  private:
   139 
   140     enum {KMaxSectorsInCache = 32};  ///< maximal number sectors in FAT12 cache
   141     enum {KFat12EntryMask = 0x0FFF}; ///< FAT12 entry mask
   142 
   143     TUint32 iSectorsInCache;    ///< total number sectors in the cache, KMaxSectorsInCache max.
   144     T32Bits iDirtySectors;      ///< dirty sectors bitmap. '1' bit corresponds to the dirty sector;
   145     RBuf8   iData;              ///< Whole FAT12 cache data.
   146 };
   147 
   148 
   149 //-----------------------------------------------------------------------------
   150 
   151 /**
   152     Abstract base class for paged caches, i.e. those that consist of some number of cache pages.
   153     In this case the most of the functionality is implemented in page classes and this is just a page container.
   154     Each cache page in turn is logically divided to sectors. The sector is a logical unit of write granularity
   155     See also CFatCachePageBase et al.
   156 */
   157 class CFatPagedCacheBase : public CFatCacheBase
   158 {
   159  public:
   160 
   161     inline TUint PageSizeLog2()  const;
   162     inline TUint PageSize()      const;
   163     
   164     inline TUint SectorSizeLog2() const;
   165     inline TUint SectorsInPage()  const;
   166 
   167  protected:
   168     CFatPagedCacheBase();
   169 
   170  protected:
   171     
   172     enum {KMaxSectorsInPage = 32}; ///< maximal number sectors in FAT cache page
   173 
   174     TUint iPageSizeLog2;    ///< Log2(page size)
   175     TUint iSectorSizeLog2;  ///< Log2(page sector size)
   176  
   177 };
   178 
   179 //-----------------------------------------------------------------------------
   180 
   181 class CFat16FixedCachePage;
   182 
   183 /**
   184     FAT16 fixed paged cache. Used for FAT16 only and caches whole FAT16 (its max size is 131048 bytes).
   185     Consists of the fixed array of cache pages; Pages are allocated on demand and never get evicted.
   186     Each page is logically divided to page sectors. The number of pages depends on the FAT16 size.
   187 
   188     Read granularity: One page, which size is 2^aRdGranularityLog2
   189     Write granularity: cache's page sector; its size is 2^aWrGranularityLog2
   190 */
   191 class CFat16FixedCache : public CFatPagedCacheBase
   192 {
   193  public:
   194 
   195     static CFat16FixedCache* NewL(CFatMountCB* aOwner, TUint32 aFatSize, TUint32 aRdGranularityLog2, TUint32 aWrGranularityLog2);
   196 
   197     //-- overrides from base class
   198     virtual void Close(TBool aDiscardDirtyData);
   199     virtual void FlushL();
   200 
   201     virtual TUint32 ReadEntryL(TUint32 aIndex);
   202     virtual void WriteEntryL(TUint32 aIndex, TUint32 aEntry);
   203     
   204 
   205     virtual TInt Invalidate();
   206     virtual TInt InvalidateRegion(TUint32 aStartEntry, TUint32 aNumEntries);
   207     //------------------------------------
   208 
   209  private:
   210 
   211     void InitialiseL(CFatMountCB* aOwner, TUint32 aFatSize, TUint32 aRdGranularityLog2, TUint32 aWrGranularityLog2); 
   212     
   213     CFat16FixedCache();
   214     CFat16FixedCache(const CFat16FixedCache&);
   215     CFat16FixedCache& operator=(const CFat16FixedCache&);
   216 
   217     inline TUint NumPages() const;
   218     void AssertCacheReallyClean() const;
   219 
   220  private:    
   221     RPointerArray<CFat16FixedCachePage> iPages;  ///< array of pointer to the cahe pages; if the entry is NULL, it means that the page isn't allocated yet.
   222 
   223 };
   224 
   225 
   226 //-----------------------------------------------------------------------------
   227 
   228 
   229 /**
   230     An abstract base class for the cache page. Paged caches, i.e derived form CFatPagedCacheBase uses this functionality.
   231     Provides an interface and common functionality for all types of cache pages.
   232 
   233     The FAT cache page contains a number of FAT16 or FAT32 entries, their number is always the power of 2.
   234     The page is logically divided into sectors, the maximal number of sectors in the page is KMaxSectorsInPage (32).
   235     The page read granularity is whole page and the write granularity is the sector  (see aRdGranularityLog2, aWrGranularityLog2 from the cache)
   236 
   237     The caching is write-back, i.e WriteCachedEntryL() modifies data in the cache and marks corresponding page sector as dirty.
   238     FlushL() shall be called to flust all dirty sectors in page to the media
   239 
   240 */
   241 class CFatCachePageBase : public CBase
   242 {
   243 public:
   244     
   245     ~CFatCachePageBase();
   246 
   247     //----------------
   248     virtual TBool ReadCachedEntryL (TUint32 aFatIndex, TUint32& aResult) = 0;
   249     virtual TBool WriteCachedEntryL(TUint32 aFatIndex, TUint32 aFatEntry) = 0; 
   250     virtual TUint32 ReadFromMediaL(TUint32 aFatIndex) = 0;
   251     virtual void FlushL(TBool aKeepDirty);
   252     
   253     //----------------
   254     inline TBool IsEntryCached(TUint32 aFatIndex) const ;
   255     void Invalidate(TBool aIgnoreDirtyData = EFalse);
   256     
   257     inline TBool IsDirty() const;
   258     inline TBool IsValid() const;
   259     
   260     inline TUint32 StartFatIndex() const;
   261 
   262 protected:
   263     CFatCachePageBase(CFatPagedCacheBase& aCache);
   264 
   265     /** possible states of the page */
   266     enum TState
   267         {
   268         EInvalid, ///< the page's data are invalid
   269         EClean,   ///< the page is clean, data valid and the same as on the media  
   270         EDirty    ///< the page is dirty, there are data eventually to be flushed to the media, iDirtySectors contains dirty sectors bitmap.
   271         };
   272 
   273     inline void SetState(TState aState);
   274     inline TState State() const;
   275     inline void SetClean();
   276     inline TUint32 PageSize() const; 
   277     inline TUint32 NumSectors() const; 
   278     
   279     virtual void DoWriteSectorL(TUint32 aSector)=0;
   280     inline TUint32 EntriesInPage() const;
   281 
   282 protected:
   283     TUint32 iStartIndexInFAT;   ///< FAT index this page starts from
   284     T32Bits iDirtySectors;      ///< dirty sectors bitmap. '1' bit corresponds to the dirty sector;
   285     CFatPagedCacheBase& iCache; ///< reference to the owher cache
   286     RBuf8   iData;              ///< page Data
   287 
   288 private:
   289     TState  iState;             ///< page state
   290     TUint32 iFatEntriesInPage;  ///< number of FAT entries in the page. 
   291 
   292 };
   293 
   294 
   295 //---------------------------------------------------------------------------------------------------------------------------------
   296 
   297 /**
   298     FAT16 cache page. Used only by CFat16FixedCache.
   299 */
   300 class CFat16FixedCachePage : public CFatCachePageBase
   301 {
   302  public:
   303     ~CFat16FixedCachePage() {}
   304     
   305     static CFat16FixedCachePage* NewL(CFatPagedCacheBase& aCache);
   306 
   307     //-- overrides
   308     virtual TBool ReadCachedEntryL (TUint32 aFatIndex, TUint32& aResult);
   309     virtual TBool WriteCachedEntryL(TUint32 aFatIndex, TUint32 aFatEntry); 
   310     virtual TUint32 ReadFromMediaL(TUint32 aFatIndex);
   311     //----
   312 
   313  private:
   314     CFat16FixedCachePage(CFatPagedCacheBase& aCache);
   315 
   316     //-- outlaws here
   317     CFat16FixedCachePage();
   318     CFat16FixedCachePage(const CFat16FixedCachePage&);
   319     CFat16FixedCachePage& operator=(const CFat16FixedCachePage&);
   320 
   321     virtual void DoWriteSectorL(TUint32 aSector);
   322 
   323     inline TFat16Entry* GetEntryPtr(TUint32 aFatIndex) const;
   324 
   325  private:
   326     enum {KFat16EntryMask = 0xFFFF}; ///< FAT16 entry mask
   327 };
   328 
   329 
   330 
   331 //---------------------------------------------------------------------------------------------------------------------------------
   332 
   333 
   334 
   335 #include "sl_fatcache.inl"
   336 
   337 
   338 #endif //SL_FAT_CACHE_H
   339 
   340 
   341 
   342 
   343 
   344 
   345 
   346 
   347 
   348 
   349 
   350 
   351 
   352 
   353 
   354 
   355 
   356 
   357 
   358 
   359 
   360 
   361