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