os/kernelhwsrv/userlibandfileserver/fileserver/sfat32/sl_fatcache.h
changeset 0 bde4ae8d615e
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/os/kernelhwsrv/userlibandfileserver/fileserver/sfat32/sl_fatcache.h	Fri Jun 15 03:10:57 2012 +0200
     1.3 @@ -0,0 +1,361 @@
     1.4 +// Copyright (c) 1998-2009 Nokia Corporation and/or its subsidiary(-ies).
     1.5 +// All rights reserved.
     1.6 +// This component and the accompanying materials are made available
     1.7 +// under the terms of the License "Eclipse Public License v1.0"
     1.8 +// which accompanies this distribution, and is available
     1.9 +// at the URL "http://www.eclipse.org/legal/epl-v10.html".
    1.10 +//
    1.11 +// Initial Contributors:
    1.12 +// Nokia Corporation - initial contribution.
    1.13 +//
    1.14 +// Contributors:
    1.15 +//
    1.16 +// Description:
    1.17 +// f32\sfat\sl_facache.h
    1.18 +// FAT cache base classes definition
    1.19 +// FAT12 and FAT16 cache classes definition
    1.20 +// 
    1.21 +//
    1.22 +
    1.23 +/**
    1.24 + @file
    1.25 + @internalTechnology
    1.26 +*/
    1.27 +
    1.28 +#ifndef SL_FAT_CACHE_H
    1.29 +#define SL_FAT_CACHE_H
    1.30 +
    1.31 +
    1.32 +//-----------------------------------------------------------------------------
    1.33 +
    1.34 +class CFatBitCache;
    1.35 +
    1.36 +/**
    1.37 +    An abstract base class for all types of FAT caches.
    1.38 +    Provides user interface and some common for all types of FAT caches functionality.
    1.39 +*/
    1.40 +class CFatCacheBase : public CBase
    1.41 +{
    1.42 + public:
    1.43 +
    1.44 +    virtual ~CFatCacheBase();
    1.45 +
    1.46 +    //-- public interface
    1.47 +    virtual void Close(TBool /*aDiscardDirtyData*/) {};
    1.48 +    virtual void FlushL() = 0;
    1.49 +
    1.50 +    virtual TUint32 ReadEntryL(TUint32 aIndex) = 0;
    1.51 +    virtual void WriteEntryL(TUint32 aIndex, TUint32 aEntry) = 0;
    1.52 +    
    1.53 +    virtual TInt Invalidate() = 0;
    1.54 +    virtual TInt InvalidateRegion(TUint32 aStartEntry, TUint32 aNumEntries) = 0;
    1.55 +
    1.56 +    TInt ReadFatData(TUint32 aPos, TUint32 aLen, TDes8& aData) const;
    1.57 +    TInt WriteFatData(TUint32 aPos, const TDesC8& aData) const;
    1.58 +
    1.59 +    inline TUint32  FatStartPos() const;
    1.60 +    inline TUint32  FatSize() const;
    1.61 +    inline TFatType FatType() const;
    1.62 +
    1.63 + public:
    1.64 +    
    1.65 +    //-- auxilary interface to additional bit supercache (it may exist only in FAT32 cache implementation)
    1.66 +    virtual CFatBitCache* BitCacheInterface();
    1.67 +
    1.68 +
    1.69 + protected:
    1.70 +    CFatCacheBase();
    1.71 +
    1.72 +    virtual void InitialiseL(CFatMountCB* aOwner);
    1.73 +
    1.74 +    inline TBool IsDirty() const;
    1.75 +    inline void SetDirty(TBool aDirty);
    1.76 +    inline TUint NumFATs() const;
    1.77 +
    1.78 +    TBool CheckInvalidatingDirtyCache() const;
    1.79 +
    1.80 +    inline TUint FAT_SectorSzLog2() const;
    1.81 +    inline TUint FAT_SectorSz() const; 
    1.82 +    inline TUint FAT_ClusterSzLog2() const;
    1.83 +
    1.84 + protected:
    1.85 +    
    1.86 +    enum {KInvalidFatNo = 0xFF}; ///< used to invalidate current FAT no.
    1.87 +    TUint   iCurrentFatNo;       ///< current FAT number WriteFatData will write to.
    1.88 +
    1.89 + private:    
    1.90 +    //-- values cached from owning mount.
    1.91 +    TUint32     iFatStartPos;   ///< media position of FAT1 start 
    1.92 +    TUint32     iFatSize;       ///< size of FAT in bytes
    1.93 +    TUint16     iNumFATs;       ///< number of FATs on the volume
    1.94 +    TUint16     iFatSecSzLog2;  ///< Log2(FAT Sector size)
    1.95 +    TUint16     iFatClustSzLog2;///< Log2(FAT cluster size)
    1.96 +    TFatType    iFatType;       ///< FAT type
    1.97 +    TDriveInterface* ipDrive;///< interface to the media driver
    1.98 +    //---
    1.99 +
   1.100 +    TBool       iDirty;         ///< ETrue if the cache is dirty
   1.101 +};
   1.102 +
   1.103 +
   1.104 +//-----------------------------------------------------------------------------
   1.105 +
   1.106 +/**
   1.107 +    Fixed FAT12 cache. This is a contiguous cache that caches whole FAT12.
   1.108 +    This cache is logically divided to sectors, maximal number of sectors in this cache is KMaxSectorsInCache (32).
   1.109 +    
   1.110 +    Read granularity: whole cache; anyway it can't be larger than 6126 bytes.
   1.111 +    Write granularity: cache sector size, which is always "FAT Sector Size" and non-configurable.
   1.112 +*/
   1.113 +class CFat12Cache : public CFatCacheBase
   1.114 +{
   1.115 + public:
   1.116 +    static CFat12Cache* NewL(CFatMountCB* aOwner, TUint32 aFatSize);
   1.117 +
   1.118 +    //-- overrides from base class
   1.119 +    virtual void Close(TBool aDiscardDirtyData);
   1.120 +    virtual void FlushL();
   1.121 +
   1.122 +    virtual TUint32 ReadEntryL(TUint32 aIndex);
   1.123 +    virtual void WriteEntryL(TUint32 aIndex, TUint32 aEntry);
   1.124 +
   1.125 +    virtual TInt Invalidate();
   1.126 +    virtual TInt InvalidateRegion(TUint32 aStartEntry, TUint32 aNumEntries);
   1.127 +    //------------------------------------
   1.128 +
   1.129 + private:
   1.130 +    
   1.131 +    void InitialiseL(CFatMountCB* aOwner, TUint32 aFatSize); 
   1.132 +
   1.133 +    CFat12Cache();
   1.134 +    CFat12Cache(const CFat12Cache&);
   1.135 +    CFat12Cache& operator=(const CFat12Cache&);
   1.136 +
   1.137 +
   1.138 +    inline TUint32 NumSectors() const;
   1.139 +    void AssertCacheReallyClean() const;
   1.140 +
   1.141 + private:
   1.142 +
   1.143 +    enum {KMaxSectorsInCache = 32};  ///< maximal number sectors in FAT12 cache
   1.144 +    enum {KFat12EntryMask = 0x0FFF}; ///< FAT12 entry mask
   1.145 +
   1.146 +    TUint32 iSectorsInCache;    ///< total number sectors in the cache, KMaxSectorsInCache max.
   1.147 +    T32Bits iDirtySectors;      ///< dirty sectors bitmap. '1' bit corresponds to the dirty sector;
   1.148 +    RBuf8   iData;              ///< Whole FAT12 cache data.
   1.149 +};
   1.150 +
   1.151 +
   1.152 +//-----------------------------------------------------------------------------
   1.153 +
   1.154 +/**
   1.155 +    Abstract base class for paged caches, i.e. those that consist of some number of cache pages.
   1.156 +    In this case the most of the functionality is implemented in page classes and this is just a page container.
   1.157 +    Each cache page in turn is logically divided to sectors. The sector is a logical unit of write granularity
   1.158 +    See also CFatCachePageBase et al.
   1.159 +*/
   1.160 +class CFatPagedCacheBase : public CFatCacheBase
   1.161 +{
   1.162 + public:
   1.163 +
   1.164 +    inline TUint PageSizeLog2()  const;
   1.165 +    inline TUint PageSize()      const;
   1.166 +    
   1.167 +    inline TUint SectorSizeLog2() const;
   1.168 +    inline TUint SectorsInPage()  const;
   1.169 +
   1.170 + protected:
   1.171 +    CFatPagedCacheBase();
   1.172 +
   1.173 + protected:
   1.174 +    
   1.175 +    enum {KMaxSectorsInPage = 32}; ///< maximal number sectors in FAT cache page
   1.176 +
   1.177 +    TUint iPageSizeLog2;    ///< Log2(page size)
   1.178 +    TUint iSectorSizeLog2;  ///< Log2(page sector size)
   1.179 + 
   1.180 +};
   1.181 +
   1.182 +//-----------------------------------------------------------------------------
   1.183 +
   1.184 +class CFat16FixedCachePage;
   1.185 +
   1.186 +/**
   1.187 +    FAT16 fixed paged cache. Used for FAT16 only and caches whole FAT16 (its max size is 131048 bytes).
   1.188 +    Consists of the fixed array of cache pages; Pages are allocated on demand and never get evicted.
   1.189 +    Each page is logically divided to page sectors. The number of pages depends on the FAT16 size.
   1.190 +
   1.191 +    Read granularity: One page, which size is 2^aRdGranularityLog2
   1.192 +    Write granularity: cache's page sector; its size is 2^aWrGranularityLog2
   1.193 +*/
   1.194 +class CFat16FixedCache : public CFatPagedCacheBase
   1.195 +{
   1.196 + public:
   1.197 +
   1.198 +    static CFat16FixedCache* NewL(CFatMountCB* aOwner, TUint32 aFatSize, TUint32 aRdGranularityLog2, TUint32 aWrGranularityLog2);
   1.199 +
   1.200 +    //-- overrides from base class
   1.201 +    virtual void Close(TBool aDiscardDirtyData);
   1.202 +    virtual void FlushL();
   1.203 +
   1.204 +    virtual TUint32 ReadEntryL(TUint32 aIndex);
   1.205 +    virtual void WriteEntryL(TUint32 aIndex, TUint32 aEntry);
   1.206 +    
   1.207 +
   1.208 +    virtual TInt Invalidate();
   1.209 +    virtual TInt InvalidateRegion(TUint32 aStartEntry, TUint32 aNumEntries);
   1.210 +    //------------------------------------
   1.211 +
   1.212 + private:
   1.213 +
   1.214 +    void InitialiseL(CFatMountCB* aOwner, TUint32 aFatSize, TUint32 aRdGranularityLog2, TUint32 aWrGranularityLog2); 
   1.215 +    
   1.216 +    CFat16FixedCache();
   1.217 +    CFat16FixedCache(const CFat16FixedCache&);
   1.218 +    CFat16FixedCache& operator=(const CFat16FixedCache&);
   1.219 +
   1.220 +    inline TUint NumPages() const;
   1.221 +    void AssertCacheReallyClean() const;
   1.222 +
   1.223 + private:    
   1.224 +    RPointerArray<CFat16FixedCachePage> iPages;  ///< array of pointer to the cahe pages; if the entry is NULL, it means that the page isn't allocated yet.
   1.225 +
   1.226 +};
   1.227 +
   1.228 +
   1.229 +//-----------------------------------------------------------------------------
   1.230 +
   1.231 +
   1.232 +/**
   1.233 +    An abstract base class for the cache page. Paged caches, i.e derived form CFatPagedCacheBase uses this functionality.
   1.234 +    Provides an interface and common functionality for all types of cache pages.
   1.235 +
   1.236 +    The FAT cache page contains a number of FAT16 or FAT32 entries, their number is always the power of 2.
   1.237 +    The page is logically divided into sectors, the maximal number of sectors in the page is KMaxSectorsInPage (32).
   1.238 +    The page read granularity is whole page and the write granularity is the sector  (see aRdGranularityLog2, aWrGranularityLog2 from the cache)
   1.239 +
   1.240 +    The caching is write-back, i.e WriteCachedEntryL() modifies data in the cache and marks corresponding page sector as dirty.
   1.241 +    FlushL() shall be called to flust all dirty sectors in page to the media
   1.242 +
   1.243 +*/
   1.244 +class CFatCachePageBase : public CBase
   1.245 +{
   1.246 +public:
   1.247 +    
   1.248 +    ~CFatCachePageBase();
   1.249 +
   1.250 +    //----------------
   1.251 +    virtual TBool ReadCachedEntryL (TUint32 aFatIndex, TUint32& aResult) = 0;
   1.252 +    virtual TBool WriteCachedEntryL(TUint32 aFatIndex, TUint32 aFatEntry) = 0; 
   1.253 +    virtual TUint32 ReadFromMediaL(TUint32 aFatIndex) = 0;
   1.254 +    virtual void FlushL(TBool aKeepDirty);
   1.255 +    
   1.256 +    //----------------
   1.257 +    inline TBool IsEntryCached(TUint32 aFatIndex) const ;
   1.258 +    void Invalidate(TBool aIgnoreDirtyData = EFalse);
   1.259 +    
   1.260 +    inline TBool IsDirty() const;
   1.261 +    inline TBool IsValid() const;
   1.262 +    
   1.263 +    inline TUint32 StartFatIndex() const;
   1.264 +
   1.265 +protected:
   1.266 +    CFatCachePageBase(CFatPagedCacheBase& aCache);
   1.267 +
   1.268 +    /** possible states of the page */
   1.269 +    enum TState
   1.270 +        {
   1.271 +        EInvalid, ///< the page's data are invalid
   1.272 +        EClean,   ///< the page is clean, data valid and the same as on the media  
   1.273 +        EDirty    ///< the page is dirty, there are data eventually to be flushed to the media, iDirtySectors contains dirty sectors bitmap.
   1.274 +        };
   1.275 +
   1.276 +    inline void SetState(TState aState);
   1.277 +    inline TState State() const;
   1.278 +    inline void SetClean();
   1.279 +    inline TUint32 PageSize() const; 
   1.280 +    inline TUint32 NumSectors() const; 
   1.281 +    
   1.282 +    virtual void DoWriteSectorL(TUint32 aSector)=0;
   1.283 +    inline TUint32 EntriesInPage() const;
   1.284 +
   1.285 +protected:
   1.286 +    TUint32 iStartIndexInFAT;   ///< FAT index this page starts from
   1.287 +    T32Bits iDirtySectors;      ///< dirty sectors bitmap. '1' bit corresponds to the dirty sector;
   1.288 +    CFatPagedCacheBase& iCache; ///< reference to the owher cache
   1.289 +    RBuf8   iData;              ///< page Data
   1.290 +
   1.291 +private:
   1.292 +    TState  iState;             ///< page state
   1.293 +    TUint32 iFatEntriesInPage;  ///< number of FAT entries in the page. 
   1.294 +
   1.295 +};
   1.296 +
   1.297 +
   1.298 +//---------------------------------------------------------------------------------------------------------------------------------
   1.299 +
   1.300 +/**
   1.301 +    FAT16 cache page. Used only by CFat16FixedCache.
   1.302 +*/
   1.303 +class CFat16FixedCachePage : public CFatCachePageBase
   1.304 +{
   1.305 + public:
   1.306 +    ~CFat16FixedCachePage() {}
   1.307 +    
   1.308 +    static CFat16FixedCachePage* NewL(CFatPagedCacheBase& aCache);
   1.309 +
   1.310 +    //-- overrides
   1.311 +    virtual TBool ReadCachedEntryL (TUint32 aFatIndex, TUint32& aResult);
   1.312 +    virtual TBool WriteCachedEntryL(TUint32 aFatIndex, TUint32 aFatEntry); 
   1.313 +    virtual TUint32 ReadFromMediaL(TUint32 aFatIndex);
   1.314 +    //----
   1.315 +
   1.316 + private:
   1.317 +    CFat16FixedCachePage(CFatPagedCacheBase& aCache);
   1.318 +
   1.319 +    //-- outlaws here
   1.320 +    CFat16FixedCachePage();
   1.321 +    CFat16FixedCachePage(const CFat16FixedCachePage&);
   1.322 +    CFat16FixedCachePage& operator=(const CFat16FixedCachePage&);
   1.323 +
   1.324 +    virtual void DoWriteSectorL(TUint32 aSector);
   1.325 +
   1.326 +    inline TFat16Entry* GetEntryPtr(TUint32 aFatIndex) const;
   1.327 +
   1.328 + private:
   1.329 +    enum {KFat16EntryMask = 0xFFFF}; ///< FAT16 entry mask
   1.330 +};
   1.331 +
   1.332 +
   1.333 +
   1.334 +//---------------------------------------------------------------------------------------------------------------------------------
   1.335 +
   1.336 +
   1.337 +
   1.338 +#include "sl_fatcache.inl"
   1.339 +
   1.340 +
   1.341 +#endif //SL_FAT_CACHE_H
   1.342 +
   1.343 +
   1.344 +
   1.345 +
   1.346 +
   1.347 +
   1.348 +
   1.349 +
   1.350 +
   1.351 +
   1.352 +
   1.353 +
   1.354 +
   1.355 +
   1.356 +
   1.357 +
   1.358 +
   1.359 +
   1.360 +
   1.361 +
   1.362 +
   1.363 +
   1.364 +