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