author | William Roberts <williamr@symbian.org> |
Wed, 16 Jun 2010 15:41:52 +0100 | |
branch | GCC_SURGE |
changeset 161 | d5bf4ee37cad |
parent 109 | b3a1d9898418 |
child 257 | 3e88ff8f41d5 |
child 269 | d57b86b1867a |
permissions | -rw-r--r-- |
0 | 1 |
// Copyright (c) 1996-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\sl_fatcache32.cpp |
|
15 |
// |
|
16 |
// |
|
17 |
||
18 |
#include "sl_std.h" |
|
19 |
#include "sl_fatcache32.h" |
|
20 |
||
21 |
/** |
|
22 |
@file |
|
23 |
Various FAT32 caches implementation |
|
24 |
*/ |
|
25 |
||
26 |
||
27 |
||
28 |
//################################################################################################################################# |
|
29 |
//# CFat32LruCache implementation |
|
30 |
//################################################################################################################################# |
|
31 |
||
32 |
//----------------------------------------------------------------------------- |
|
33 |
CFat32LruCache::CFat32LruCache() |
|
34 |
:CFatPagedCacheBase(), iPageList(_FOFF(CFat32LruCachePage, iLink)) |
|
35 |
{ |
|
36 |
} |
|
37 |
||
38 |
//----------------------------------------------------------------------------- |
|
39 |
/** |
|
40 |
FAT32 LRU cache factory function. |
|
41 |
@param aOwner pointer to the owning FAT mount |
|
42 |
@param aMaxMemSize maximal size of the memory the cache can use, bytes |
|
43 |
@param aRdGranularityLog2 Log2(read granularity) |
|
44 |
@param aWrGranularityLog2 Log2(write granularity) |
|
45 |
||
46 |
@return pointer to the constructed object. |
|
47 |
*/ |
|
48 |
CFat32LruCache* CFat32LruCache::NewL(CFatMountCB* aOwner, TUint32 aMaxMemSize, TUint32 aRdGranularityLog2, TUint32 aWrGranularityLog2) |
|
49 |
{ |
|
50 |
__PRINT(_L("#-CFat32LruCache::NewL()")); |
|
51 |
CFat32LruCache* pSelf = NULL; |
|
52 |
pSelf = new (ELeave) CFat32LruCache; |
|
53 |
||
54 |
CleanupStack::PushL(pSelf); |
|
55 |
pSelf->InitialiseL(aOwner, aMaxMemSize, aRdGranularityLog2, aWrGranularityLog2); |
|
56 |
CleanupStack::Pop(); |
|
57 |
||
58 |
return pSelf; |
|
59 |
} |
|
60 |
||
61 |
//----------------------------------------------------------------------------- |
|
62 |
/** |
|
63 |
@return pointer to the CFatBitCache interface. |
|
64 |
*/ |
|
65 |
CFatBitCache* CFat32LruCache::BitCacheInterface() |
|
66 |
{ |
|
67 |
return iBitCache; |
|
68 |
} |
|
69 |
||
70 |
//----------------------------------------------------------------------------- |
|
71 |
||
72 |
/** |
|
73 |
FAT32 LRU cache initialisation. |
|
74 |
||
75 |
@param aOwner pointer to the owning FAT mount |
|
76 |
@param aMaxMemSize maximal size of the memory the cache can use, bytes |
|
77 |
@param aRdGranularityLog2 Log2(read granularity) |
|
78 |
@param aWrGranularityLog2 Log2(write granularity) |
|
79 |
||
80 |
@return pointer to the constructed object. |
|
81 |
*/ |
|
82 |
void CFat32LruCache::InitialiseL(CFatMountCB* aOwner, TUint32 aMaxMemSize, TUint32 aRdGranularityLog2, TUint32 aWrGranularityLog2) |
|
83 |
{ |
|
84 |
const TUint32 KReadGranularity = Pow2(aRdGranularityLog2); |
|
85 |
const TUint32 KWriteGranularity = Pow2(aWrGranularityLog2); |
|
86 |
||
87 |
__PRINT3(_L("#-CFat32LruCache::InitialiseL MaxMem:%u, RdGr:%d, WrGr:%d"),aMaxMemSize, KReadGranularity, KWriteGranularity); |
|
88 |
(void)KReadGranularity; |
|
89 |
(void)KWriteGranularity; |
|
90 |
||
91 |
||
92 |
const TBool bParamsValid = (aRdGranularityLog2 >= aWrGranularityLog2) && (aWrGranularityLog2 >= KDefSectorSzLog2) && (aMaxMemSize > KReadGranularity); |
|
93 |
__ASSERT_ALWAYS(bParamsValid, Fault(EFatCache_BadGranularity)); |
|
94 |
||
95 |
CFatPagedCacheBase::InitialiseL(aOwner); |
|
96 |
||
97 |
ASSERT(FatType() == EFat32); |
|
98 |
||
99 |
//-- according to the FAT32 specs, FAT32 min size is 65526 entries or 262104 bytes. |
|
100 |
//-- It's possible to incorrectly format a small volume to FAT32, it shall be accessible read-only. |
|
101 |
if(aMaxMemSize > FatSize()) |
|
102 |
{//-- strange situation, memory allocated for LRU cache is enough to cache whole FAT32 |
|
103 |
__PRINT(_L("#-CFat32LruCache::InitialiseL warning: LRU cache becomes fixed! (too much memory allowed)")); |
|
104 |
aMaxMemSize = FatSize(); |
|
105 |
} |
|
106 |
||
107 |
//-- LRU cache page size is (2^aRdGranularityLog2) bytes and consists of 2^(aRdGranularityLog2-aWrGranularity) sectors. |
|
108 |
iPageSizeLog2 = aRdGranularityLog2; |
|
109 |
iSectorSizeLog2 = aWrGranularityLog2; //-- Log2(number of sectors in cache page) |
|
110 |
||
111 |
iMaxPages = aMaxMemSize / PageSize(); //-- maximal number of cache pages we can allocate |
|
112 |
iNumPagesAllocated = 0; |
|
113 |
||
114 |
__ASSERT_ALWAYS((iMaxPages > 1 && SectorsInPage() < KMaxSectorsInPage), Fault(EFatCache_BadGranularity)); |
|
115 |
||
116 |
//-- obtain maximal number of entries in the table |
|
117 |
if(aOwner->UsableClusters() < 1) |
|
118 |
{ |
|
119 |
ASSERT(0); |
|
120 |
User::Leave(KErrCorrupt); |
|
121 |
} |
|
122 |
||
123 |
iMaxFatEntries = aOwner->UsableClusters()+KFatFirstSearchCluster; //-- FAT[0] & FAT[1] are not in use |
|
124 |
||
125 |
//-- create FAT bit supercache if it is enabled in config |
|
126 |
ASSERT(!iBitCache); |
|
127 |
if(aOwner->FatConfig().FAT32_UseBitSupercache()) |
|
128 |
{ |
|
129 |
iBitCache = CFatBitCache::New(*this); |
|
130 |
} |
|
131 |
else |
|
132 |
{ |
|
133 |
__PRINT(_L("#++ !! Fat Bit Supercache is disabled in config !!")); |
|
134 |
} |
|
135 |
||
136 |
} |
|
137 |
||
138 |
//----------------------------------------------------------------------------- |
|
139 |
/** |
|
140 |
Close the cache and deallocate its memory. |
|
141 |
@param aDiscardDirtyData if ETrue, will ignore dirty data. If EFalse, will panic on atempt to close dirty cache. |
|
142 |
*/ |
|
143 |
void CFat32LruCache::Close(TBool aDiscardDirtyData) |
|
144 |
{ |
|
145 |
__PRINT1(_L("#-CFat32LruCache::Close(%d)"), aDiscardDirtyData); |
|
146 |
||
147 |
//-- delete FAT bit supercache if present |
|
148 |
delete iBitCache; |
|
149 |
iBitCache=NULL; |
|
150 |
||
151 |
||
152 |
//-- delete existing cache pages |
|
153 |
TPageIterator itr(iPageList); |
|
154 |
||
155 |
for(;;) |
|
156 |
{ |
|
157 |
CFat32LruCachePage* pPage = itr++; |
|
158 |
if(!pPage) |
|
159 |
break; |
|
160 |
||
161 |
pPage->iLink.Deque(); //-- remove page from the list |
|
162 |
||
163 |
if(pPage->IsDirty()) |
|
164 |
{//-- trying to destroy the cache that has dirty pages |
|
165 |
__PRINT1(_L("#-CFat32LruCache::Close() The page is dirty! Start idx:%d"), pPage->StartFatIndex()); |
|
166 |
if(!aDiscardDirtyData) |
|
167 |
{ |
|
168 |
Fault(EFatCache_DiscardingDirtyData); |
|
169 |
} |
|
170 |
//-- ignore this fact if requested. |
|
171 |
} |
|
172 |
||
173 |
delete pPage; |
|
174 |
--iNumPagesAllocated; |
|
175 |
} |
|
176 |
||
177 |
SetDirty(EFalse); |
|
178 |
ASSERT(!iNumPagesAllocated); |
|
179 |
} |
|
180 |
||
181 |
||
182 |
//----------------------------------------------------------------------------- |
|
183 |
/** |
|
184 |
Tries to read FAT entry from the cache. If the entry at aFatIndex is not cached, does nothing and returns EFalse. |
|
185 |
If finds the cache page that contains entry at index "aFatIndex", reads it and returns ETrue. |
|
186 |
||
187 |
@param aFatIndex FAT entry index within FAT table |
|
188 |
@param aFatEntry on success it will contain FAT entry value |
|
189 |
@return ETrue if the entry has been read |
|
190 |
EFalse if index aFatIndex isn't cached |
|
191 |
*/ |
|
192 |
TBool CFat32LruCache::ReadCachedEntryL(TUint32 aFatIndex, TFat32Entry& aResult) |
|
193 |
{ |
|
194 |
//-- iterate through LRU list looking if the entry is cached. |
|
195 |
TPageIterator itr(iPageList); |
|
196 |
||
197 |
for(;;) |
|
198 |
{ |
|
199 |
CFat32LruCachePage* pPage = itr++; |
|
200 |
if(!pPage) |
|
201 |
break; |
|
202 |
||
203 |
if(pPage->ReadCachedEntryL(aFatIndex, aResult)) |
|
204 |
{//-- found entry in some cache page. Make this page LRU |
|
205 |
if(!iPageList.IsFirst(pPage)) |
|
206 |
{ |
|
207 |
pPage->iLink.Deque(); |
|
208 |
iPageList.AddFirst(*pPage); |
|
209 |
} |
|
210 |
return ETrue; |
|
211 |
} |
|
212 |
} |
|
213 |
||
214 |
return EFalse; //-- the entry is not cached |
|
215 |
} |
|
216 |
||
217 |
//----------------------------------------------------------------------------- |
|
218 |
/** |
|
219 |
Tries to write FAT entry to the cache. If the entry at aFatIndex is not cached, does nothing and returns EFalse. |
|
220 |
If finds the cache page that contains entry at index "aFatIndex", overwrites it and returns ETrue |
|
221 |
||
222 |
@param aFatIndex FAT entry index within FAT table |
|
223 |
@param aFatEntry new FAT entry value |
|
224 |
@return ETrue if the entry has been overwritten |
|
225 |
EFalse if index aFatIndex isn't cached |
|
226 |
*/ |
|
227 |
TBool CFat32LruCache::WriteCachedEntryL(TUint32 aFatIndex, TFat32Entry aFatEntry) |
|
228 |
{ |
|
229 |
//-- iterate through LRU list looking if the entry is cached. |
|
230 |
TPageIterator itr(iPageList); |
|
231 |
||
232 |
for(;;) |
|
233 |
{ |
|
234 |
CFat32LruCachePage* pPage = itr++; |
|
235 |
if(!pPage) |
|
236 |
break; |
|
237 |
||
238 |
if(pPage->WriteCachedEntryL(aFatIndex, aFatEntry)) |
|
239 |
{//-- the entry was cached and modified now. Make this page LRU |
|
240 |
if(!iPageList.IsFirst(pPage)) |
|
241 |
{ |
|
242 |
pPage->iLink.Deque(); |
|
243 |
iPageList.AddFirst(*pPage); |
|
244 |
} |
|
245 |
return ETrue; |
|
246 |
} |
|
247 |
} |
|
248 |
||
249 |
return EFalse; //-- the entry is not cached |
|
250 |
} |
|
251 |
||
252 |
//----------------------------------------------------------------------------- |
|
253 |
/** |
|
254 |
Get a spare page. This function can either allocate a page if memory limit isn't reached yet |
|
255 |
or find the least recently used (in the end of the LRU list) and evict it. |
|
256 |
||
257 |
@return pointer to the cache page to use, it will be insertet to the beginning of the LRU list |
|
258 |
*/ |
|
259 |
CFat32LruCachePage* CFat32LruCache::DoGetSpareCachePageL() |
|
260 |
{ |
|
261 |
CFat32LruCachePage* pPage=NULL; |
|
262 |
||
263 |
if(iNumPagesAllocated < iMaxPages) |
|
264 |
{//-- we still can allocate a page |
|
265 |
||
266 |
pPage = CFat32LruCachePage::NewL(*this); |
|
267 |
++iNumPagesAllocated; |
|
268 |
iPageList.AddFirst(*pPage); //-- insert the page into the beginning of LRU list |
|
269 |
return pPage; |
|
270 |
} |
|
271 |
||
272 |
//-- all pages are already allocated, evict the last recently used and remove it from the list |
|
273 |
pPage = iPageList.Last(); //-- least recently used page, last in the list |
|
274 |
pPage->iLink.Deque(); //-- remove it from the LRU list |
|
275 |
iPageList.AddFirst(*pPage); //-- insert the page into the beginning of LRU list |
|
276 |
||
277 |
//__PRINT1(_L("#-CFat32LruCache::DoGetSpareCachePageL() page @FAT idx:%d evicted"), pPage->StartFatIndex()); |
|
278 |
||
279 |
//-- flush the page, writing its data to all copies of FAT, to FAT1, then to FAT2 etc. |
|
280 |
ASSERT(NumFATs() >0); |
|
281 |
if(pPage->IsDirty()) |
|
282 |
{ |
|
283 |
//-- write page data to all copies of FAT |
|
284 |
for(iCurrentFatNo=0; iCurrentFatNo < NumFATs(); ++iCurrentFatNo) |
|
285 |
{ |
|
286 |
const TBool keepDirty = iCurrentFatNo < (NumFATs()-1); |
|
287 |
pPage->FlushL(keepDirty); |
|
288 |
} |
|
289 |
||
290 |
iCurrentFatNo = KInvalidFatNo; |
|
291 |
} |
|
292 |
||
293 |
||
294 |
return pPage; |
|
295 |
} |
|
296 |
||
297 |
||
298 |
||
299 |
//----------------------------------------------------------------------------- |
|
300 |
/** |
|
301 |
Read FAT entry from the cache. |
|
302 |
||
303 |
@param aIndex FAT entry index to read |
|
304 |
@return FAT entry value at the index "aIndex" |
|
305 |
*/ |
|
306 |
TUint32 CFat32LruCache::ReadEntryL(TUint32 aIndex) |
|
307 |
{ |
|
308 |
// __PRINT1(_L("#-CFat32LruCache::ReadEntryL() FAT idx:%d"), aIndex); |
|
309 |
||
310 |
ASSERT(aIndex >= KFatFirstSearchCluster && aIndex < (FatSize() >> KFat32EntrySzLog2)); |
|
311 |
||
312 |
//-- firstly try to locate required entry in cache |
|
313 |
TFat32Entry entry; |
|
314 |
if(ReadCachedEntryL(aIndex, entry)) |
|
315 |
return entry; //-- the requested entry found in cache |
|
316 |
||
317 |
//-- No luck, get a spare cache page (it will be inserted to the head of the LRU list) |
|
318 |
CFat32LruCachePage* pPage = DoGetSpareCachePageL(); |
|
319 |
ASSERT(pPage); |
|
320 |
||
321 |
entry = pPage->ReadFromMediaL(aIndex); //-- read whole FAT page from the media |
|
322 |
||
323 |
return entry; |
|
324 |
} |
|
325 |
||
326 |
//----------------------------------------------------------------------------- |
|
327 |
/** |
|
328 |
Write FAT entry to the cache. |
|
329 |
Appropriate FAT cache sector will be marked as "dirty" and will be eventually flushed to the media. |
|
330 |
||
331 |
@param aIndex FAT entry index |
|
332 |
@param aEntry FAT entry value |
|
333 |
*/ |
|
334 |
void CFat32LruCache::WriteEntryL(TUint32 aIndex, TUint32 aEntry) |
|
335 |
{ |
|
336 |
//__PRINT2(_L("#-CFat32LruCache::WriteEntryL() FAT idx:%d, val:%d"), aIndex, aEntry); |
|
337 |
||
338 |
ASSERT(aIndex >= KFatFirstSearchCluster && aIndex < (FatSize() >> KFat32EntrySzLog2)); |
|
339 |
||
340 |
SetDirty(ETrue); |
|
341 |
||
342 |
//-- 1. try to locate entry in the cache and overwrite it there if it is cached |
|
343 |
if(WriteCachedEntryL(aIndex, aEntry)) |
|
344 |
return; //-- the entry in cache altered |
|
345 |
||
346 |
//-- 2. the entry isn't cached; find a spare cache page (it will be inserted to the head of the LRU list) |
|
347 |
CFat32LruCachePage* pPage = DoGetSpareCachePageL(); |
|
348 |
ASSERT(pPage); |
|
349 |
||
350 |
pPage->ReadFromMediaL(aIndex); //-- read whole FAT page from the media |
|
351 |
||
352 |
||
353 |
//-- 3. overwrite entry in cache |
|
354 |
TBool bRes = pPage->WriteCachedEntryL(aIndex, aEntry); |
|
355 |
ASSERT(bRes); |
|
356 |
(void)bRes; |
|
357 |
} |
|
358 |
||
359 |
//----------------------------------------------------------------------------- |
|
360 |
/** |
|
361 |
A debug method that asserts that the cache is really clean |
|
362 |
*/ |
|
363 |
void CFat32LruCache::AssertCacheReallyClean() |
|
364 |
{ |
|
365 |
#ifdef _DEBUG |
|
366 |
||
367 |
TPageIterator itr(iPageList); |
|
368 |
for(;;) |
|
369 |
{//-- iterate through LRU list flushing pages into the current copy of FAT |
|
370 |
CFat32LruCachePage* pPage = itr++; |
|
371 |
||
372 |
if(!pPage) |
|
373 |
break; |
|
374 |
||
375 |
if(pPage->IsDirty()) |
|
376 |
{ |
|
377 |
__PRINT(_L("#-CFat32LruCache::AssertCacheReallyClean()")); |
|
378 |
ASSERT(0); |
|
379 |
} |
|
380 |
} |
|
381 |
||
382 |
#endif |
|
383 |
} |
|
384 |
||
385 |
//----------------------------------------------------------------------------- |
|
386 |
/** |
|
387 |
Flushes all dirty data to the media. |
|
388 |
*/ |
|
389 |
void CFat32LruCache::FlushL() |
|
390 |
{ |
|
391 |
if(!IsDirty()) |
|
392 |
{ |
|
393 |
AssertCacheReallyClean(); |
|
394 |
return; |
|
395 |
} |
|
396 |
||
397 |
//-- flush dirty data to all copies of FAT |
|
398 |
//-- all dirty pages will be written firstly to FAT1, then all of them will be written to FAT2 etc. |
|
399 |
for(iCurrentFatNo=0; iCurrentFatNo < NumFATs(); ++iCurrentFatNo) |
|
400 |
{ |
|
401 |
TPageIterator itr(iPageList); |
|
402 |
for(;;) |
|
403 |
{//-- iterate through LRU list flushing pages into the current copy of FAT |
|
404 |
CFat32LruCachePage* pPage = itr++; |
|
405 |
if(!pPage) |
|
406 |
break; |
|
407 |
||
408 |
//-- we need to keep page dirty until it is flushed to the last copy of FAT table |
|
409 |
const TBool keepDirty = iCurrentFatNo < (NumFATs() - 1); |
|
410 |
pPage->FlushL(keepDirty); |
|
411 |
} |
|
412 |
} |
|
413 |
||
414 |
iCurrentFatNo = KInvalidFatNo; |
|
415 |
||
416 |
SetDirty(EFalse); |
|
417 |
} |
|
418 |
||
419 |
//----------------------------------------------------------------------------- |
|
420 |
||
421 |
/** |
|
422 |
Invalidate whole cache. All pages will be marked as invalid and will be re-read from the media on first access to them. |
|
423 |
@return always KErrNone |
|
424 |
*/ |
|
425 |
TInt CFat32LruCache::Invalidate() |
|
426 |
{ |
|
427 |
__PRINT(_L("#-CFat32LruCache::Invalidate()")); |
|
428 |
const TBool bIgnoreDirtyData = CheckInvalidatingDirtyCache(); |
|
429 |
||
430 |
//-- iterate through LRU list marking every page as invalid |
|
431 |
TPageIterator itr(iPageList); |
|
432 |
for(;;) |
|
433 |
{ |
|
434 |
CFat32LruCachePage* pPage = itr++; |
|
435 |
if(!pPage) |
|
436 |
break; |
|
437 |
||
438 |
pPage->Invalidate(bIgnoreDirtyData); |
|
439 |
} |
|
440 |
||
441 |
SetDirty(EFalse); |
|
442 |
||
443 |
return KErrNone; |
|
444 |
} |
|
445 |
||
446 |
||
447 |
//----------------------------------------------------------------------------- |
|
448 |
||
449 |
/** |
|
450 |
Invalidate FAT cache pages that contain FAT32 entries from aStartIndex to (aStartIndex+aNumEntries) |
|
451 |
These pages will be marked as invalid and will be re-read from the media on first access to them. |
|
452 |
||
453 |
@param aStartIndex FAT start index of the region being invalidated |
|
454 |
@param aNumEntries number of entries to invalidate |
|
455 |
@return always KErrNone |
|
456 |
*/ |
|
457 |
TInt CFat32LruCache::InvalidateRegion(TUint32 aStartIndex, TUint32 aNumEntries) |
|
458 |
{ |
|
459 |
__PRINT2(_L("#-CFat32LruCache::InvalidateRegion() startIndex:%d, entries:%d"),aStartIndex, aNumEntries); |
|
460 |
ASSERT(aStartIndex >= KFatFirstSearchCluster && aStartIndex < (FatSize() >> KFat32EntrySzLog2)); |
|
461 |
||
462 |
if(!aNumEntries) |
|
463 |
{ |
|
464 |
ASSERT(0); |
|
465 |
return KErrNone; |
|
466 |
} |
|
467 |
||
468 |
const TBool bIgnoreDirtyData = CheckInvalidatingDirtyCache(); |
|
469 |
const TUint KEntriesInPage = Pow2(PageSizeLog2() - KFat32EntrySzLog2); |
|
470 |
const TUint KLastIndex = aStartIndex+aNumEntries; |
|
471 |
||
472 |
TBool bCacheIsStillDirty = EFalse; //-- ETrue if the cache is still dirty after invalidating its region |
|
473 |
||
474 |
for(TUint currIndex = aStartIndex; currIndex < KLastIndex; currIndex+=KEntriesInPage) |
|
475 |
{ |
|
476 |
TPageIterator itr(iPageList); |
|
477 |
for(;;) |
|
478 |
{//-- iterate through all pages, invalidating required |
|
479 |
CFat32LruCachePage* pPage = itr++; |
|
480 |
if(!pPage) |
|
481 |
break; |
|
482 |
||
483 |
if(pPage->IsEntryCached(currIndex)) |
|
484 |
{ |
|
485 |
pPage->Invalidate(bIgnoreDirtyData); |
|
486 |
} |
|
487 |
else if(pPage->IsDirty()) //-- invalid page can't be ditry. |
|
488 |
{ |
|
489 |
bCacheIsStillDirty = ETrue; //-- we have at least 1 dirty page |
|
490 |
} |
|
491 |
} |
|
492 |
} |
|
493 |
||
494 |
SetDirty(bCacheIsStillDirty); |
|
495 |
||
496 |
return KErrNone; |
|
497 |
} |
|
498 |
||
499 |
//----------------------------------------------------------------------------- |
|
500 |
||
501 |
||
502 |
||
503 |
/** |
|
504 |
Look for free FAT entry in the FAT sector that corresponds to the aFatEntryIndex. |
|
505 |
Search is performed in both directions, the right one has more priority (FAT cluster chain needs to grow right). |
|
506 |
See FindFreeEntryInCacheSector() |
|
507 |
*/ |
|
508 |
TBool CFat32LruCache::FindFreeEntryInCacheSectorL(TUint32& aFatEntryIndex) |
|
509 |
{ |
|
510 |
if(ReadEntryL(aFatEntryIndex) == KSpareCluster) |
|
511 |
return ETrue; |
|
512 |
||
513 |
//-- look for free FAT entries in the FAT cache sector corresponting to the aStartIndex. |
|
514 |
//-- use the same approach as in CFatTable::FindClosestFreeClusterL() |
|
515 |
const TUint32 coeff = SectorSizeLog2()-KFat32EntrySzLog2; |
|
516 |
const TUint32 numEntriesInSector = Pow2(coeff); //-- number of FAT32 entries in FAT cache sector |
|
517 |
||
518 |
TUint32 MinIdx = (aFatEntryIndex >> coeff) << coeff; |
|
519 |
TUint32 MaxIdx = MinIdx+numEntriesInSector-1; |
|
520 |
||
521 |
if(MinIdx == 0) |
|
522 |
{//-- correct values if this is the first FAT sector; FAT[0] & FAT[1] are reserved |
|
523 |
MinIdx += KFatFirstSearchCluster; |
|
524 |
} |
|
525 |
||
526 |
//-- actual number of usable FAT entries can be less than deducted from number of FAT sectors. |
|
527 |
MaxIdx = Min(MaxIdx, iMaxFatEntries-1); |
|
528 |
||
529 |
//-- look in both directions starting from the aFatEntryIndex |
|
530 |
//-- but in one FAT cache page sector only |
|
531 |
TBool canGoRight = ETrue; |
|
532 |
TBool canGoLeft = ETrue; |
|
533 |
||
534 |
TUint32 rightIdx=aFatEntryIndex; |
|
535 |
TUint32 leftIdx=aFatEntryIndex; |
|
536 |
||
537 |
for(TUint i=0; i<numEntriesInSector; ++i) |
|
538 |
{ |
|
539 |
if(canGoRight) |
|
540 |
{ |
|
541 |
if(rightIdx < MaxIdx) |
|
542 |
++rightIdx; |
|
543 |
else |
|
544 |
canGoRight = EFalse; |
|
545 |
} |
|
546 |
||
547 |
if(canGoLeft) |
|
548 |
{ |
|
549 |
if(leftIdx > MinIdx) |
|
550 |
--leftIdx; |
|
551 |
else |
|
552 |
canGoLeft = EFalse; |
|
553 |
} |
|
554 |
||
555 |
if(!canGoRight && !canGoLeft) |
|
556 |
return EFalse; //-- no free entries in this sector |
|
557 |
||
558 |
if(canGoRight && ReadEntryL(rightIdx) == KSpareCluster) |
|
559 |
{ |
|
560 |
aFatEntryIndex = rightIdx; |
|
561 |
return ETrue; |
|
562 |
} |
|
563 |
||
564 |
if (canGoLeft && ReadEntryL(leftIdx) == KSpareCluster) |
|
565 |
{ |
|
566 |
aFatEntryIndex = leftIdx; |
|
567 |
return ETrue; |
|
568 |
} |
|
569 |
}//for(TUint i=0; i<numEntriesInSector; ++i) |
|
570 |
||
571 |
return EFalse; |
|
572 |
} |
|
573 |
||
574 |
||
575 |
||
576 |
//################################################################################################################################# |
|
577 |
// CFat32LruCachePage implementation |
|
578 |
//################################################################################################################################# |
|
579 |
||
580 |
||
581 |
CFat32LruCachePage::CFat32LruCachePage(CFatPagedCacheBase& aCache) |
|
582 |
:CFatCachePageBase(aCache) |
|
583 |
{ |
|
584 |
||
585 |
ASSERT(IsPowerOf2(EntriesInPage())); |
|
586 |
} |
|
587 |
||
588 |
||
589 |
/** |
|
590 |
Factory function. |
|
591 |
@param aCache reference to the owning cache. |
|
592 |
@return pointer to the constructed object or NULL on error |
|
593 |
*/ |
|
594 |
CFat32LruCachePage* CFat32LruCachePage::NewL(CFatPagedCacheBase& aCache) |
|
595 |
{ |
|
596 |
||
597 |
CFat32LruCachePage* pSelf = NULL; |
|
598 |
pSelf = new (ELeave) CFat32LruCachePage(aCache); |
|
599 |
||
600 |
CleanupStack::PushL(pSelf); |
|
601 |
||
602 |
pSelf->iData.CreateMaxL(pSelf->PageSize()); //-- allocate memory for the page |
|
603 |
||
604 |
CleanupStack::Pop(); |
|
605 |
||
606 |
return pSelf; |
|
607 |
} |
|
608 |
||
609 |
||
610 |
//----------------------------------------------------------------------------- |
|
611 |
||
612 |
/** |
|
613 |
Get a pointer to the FAT32 entry in the page buffer. |
|
614 |
The page 's data shall be valid and the entry shall belong to this page. |
|
615 |
||
616 |
@param aFatIndex absolute FAT index (from the FAT start) of the entry |
|
617 |
@return pointer to the FAT32 entry in the page buffer. |
|
618 |
*/ |
|
619 |
TFat32Entry* CFat32LruCachePage::GetEntryPtr(TUint32 aFatIndex) const |
|
620 |
{ |
|
621 |
||
622 |
ASSERT(IsValid() && IsEntryCached(aFatIndex)); |
|
623 |
||
624 |
const TUint KEntryIndexInPage = aFatIndex & (EntriesInPage()-1); //-- number of entries in page is always a power of 2 |
|
625 |
||
626 |
TFat32Entry* pEntry = ((TFat32Entry*)iData.Ptr()) + KEntryIndexInPage; |
|
627 |
return pEntry; |
|
628 |
} |
|
629 |
||
630 |
//----------------------------------------------------------------------------- |
|
631 |
||
632 |
/** |
|
633 |
Read FAT32 entry from the cache. |
|
634 |
||
635 |
1. If the entry at aFatIndex doesn't belong to this page, returns EFalse |
|
636 |
2. If page's data are valid and the entry is cached just extracts data from the page buffer. |
|
637 |
3. If page's data are invalid but the entry's index belongs to this page, firstly reads data from the media and goto 2 |
|
638 |
||
639 |
@param aFatIndex entry's absolute FAT index (from the FAT start) |
|
640 |
@param aResult on sucess there will be FAT32 entry value |
|
641 |
@return ETrue if the entry at aFatIndex belongs to this page (cached) and in this case aResult will contain this entry. |
|
642 |
EFalse if the entry isn't cached. |
|
643 |
||
644 |
*/ |
|
645 |
TBool CFat32LruCachePage::ReadCachedEntryL(TUint32 aFatIndex, TUint32& aResult) |
|
646 |
{ |
|
647 |
if(!IsEntryCached(aFatIndex)) |
|
648 |
return EFalse; //-- the page doesn't contain required index |
|
649 |
||
650 |
if(IsValid()) |
|
651 |
{//-- read entry directly from page buffer, the cached data are valid |
|
652 |
aResult = (*GetEntryPtr(aFatIndex)) & KFat32EntryMask; |
|
653 |
} |
|
654 |
else |
|
655 |
{//-- aFatIndex belongs to this page, but the page is invalid and needs to be read from the media |
|
656 |
__PRINT1(_L("#-CFat32LruCachePage::ReadCachedEntry(%d) The page is invalid, reading from the media"), aFatIndex); |
|
657 |
aResult = ReadFromMediaL(aFatIndex); |
|
658 |
} |
|
659 |
||
660 |
return ETrue; |
|
661 |
} |
|
662 |
||
663 |
//----------------------------------------------------------------------------- |
|
664 |
||
665 |
/** |
|
666 |
Read the FAT32 cache page from the media and return required FAT32 entry. |
|
667 |
||
668 |
@param aFatIndex entry's absolute FAT index (from the FAT start) |
|
669 |
@return entry value at aFatIndex. |
|
670 |
*/ |
|
671 |
TUint32 CFat32LruCachePage::ReadFromMediaL(TUint32 aFatIndex) |
|
672 |
{ |
|
673 |
//__PRINT1(_L("#-CFat32LruCachePage::ReadFromMediaL() FAT idx:%d"), aFatIndex); |
|
674 |
||
675 |
const TUint KFat32EntriesInPageLog2 = iCache.PageSizeLog2()-KFat32EntrySzLog2; //-- number of FAT32 entries in page is always a power of 2 |
|
676 |
||
677 |
//-- find out index in FAT this page starts from |
|
678 |
iStartIndexInFAT = (aFatIndex >> KFat32EntriesInPageLog2) << KFat32EntriesInPageLog2; |
|
679 |
||
680 |
SetState(EInvalid); //-- mark the page as invalid just in case if the read fails. |
|
681 |
||
682 |
//-- read page from the media |
|
683 |
const TUint32 pageStartPos = iCache.FatStartPos() + (iStartIndexInFAT << KFat32EntrySzLog2); |
|
684 |
TInt nRes = iCache.ReadFatData(pageStartPos, iCache.PageSize(), iData); |
|
685 |
if(nRes != KErrNone) |
|
686 |
{ |
|
687 |
__PRINT1(_L("#-CFat32LruCachePage::ReadFromMediaL() failed! code:%d"), nRes); |
|
688 |
User::Leave(nRes); |
|
689 |
} |
|
690 |
||
691 |
SetClean(); //-- mark this page as clean |
|
692 |
||
693 |
const TFat32Entry entry = (*GetEntryPtr(aFatIndex)) & KFat32EntryMask; |
|
694 |
||
695 |
return entry; |
|
696 |
} |
|
697 |
||
698 |
//----------------------------------------------------------------------------- |
|
699 |
||
700 |
/** |
|
701 |
Writes FAT cache page sector to the media (to all copies of the FAT) |
|
109
b3a1d9898418
Revision: 201019
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
702 |
|
0 | 703 |
@param aSector page sector number |
704 |
*/ |
|
705 |
void CFat32LruCachePage::DoWriteSectorL(TUint32 aSector) |
|
706 |
{ |
|
707 |
//__PRINT1(_L("#-CFat32LruCachePage::DoWriteContiguousSectorsL() startSec:%d"),aSector); |
|
708 |
||
709 |
ASSERT(aSector < iCache.SectorsInPage()); |
|
710 |
||
711 |
const TUint CacheSecSzLog2=iCache.SectorSizeLog2(); |
|
712 |
||
713 |
TInt offset = 0; |
|
714 |
if(iStartIndexInFAT == 0 && aSector == 0) |
|
715 |
{//-- this is the very beginning of FAT32. We must skip FAT[0] & FAT[1] entries and do not write them to media. |
|
716 |
offset = KFatFirstSearchCluster << KFat32EntrySzLog2; |
|
717 |
} |
|
718 |
||
719 |
const TUint8* pData = iData.Ptr()+offset+(aSector << CacheSecSzLog2); |
|
720 |
||
721 |
TUint32 dataLen = (1 << CacheSecSzLog2) - offset; |
|
722 |
||
723 |
const TUint32 mediaPosStart = iCache.FatStartPos() + (iStartIndexInFAT << KFat32EntrySzLog2) + (aSector << CacheSecSzLog2) + offset; |
|
724 |
const TUint32 mediaPosEnd = mediaPosStart + dataLen; |
|
725 |
||
726 |
//-- check if we are going to write beyond FAT. It can happen if the write granularity is bigger that the sector size. |
|
727 |
const TUint32 posFatEnd = iCache.FatStartPos() + iCache.FatSize(); |
|
728 |
if(mediaPosEnd > posFatEnd) |
|
729 |
{//-- correct the leength of the data to write. |
|
730 |
dataLen -= (mediaPosEnd-posFatEnd); |
|
731 |
} |
|
732 |
||
733 |
TPtrC8 ptrData(pData, dataLen); //-- source data descriptor |
|
734 |
||
735 |
TInt nRes = iCache.WriteFatData(mediaPosStart, ptrData); |
|
736 |
||
737 |
if(nRes != KErrNone) |
|
738 |
{ |
|
739 |
__PRINT1(_L("#-CFat32LruCachePage::DoWriteSectorsL() failed! code:%d"), nRes); |
|
740 |
User::Leave(nRes); |
|
741 |
} |
|
742 |
||
743 |
||
744 |
//-- if we have FAT bit supercache and it is in consistent state, check if the entry in this cache differs from the data in dirty FAT cache sector. |
|
745 |
CFatBitCache *pFatBitCache = iCache.BitCacheInterface(); |
|
746 |
if(pFatBitCache && pFatBitCache->UsableState()) |
|
747 |
{ |
|
748 |
//-- absolute FAT cache sector number corresponding aSector number in _this_ cache page |
|
749 |
const TUint32 absSectorNum = (iStartIndexInFAT >> (CacheSecSzLog2-KFat32EntrySzLog2)) + aSector; |
|
750 |
||
751 |
if(pFatBitCache->FatSectorHasFreeEntry(absSectorNum)) |
|
752 |
{ //-- it means that the corresponding FAT cache sector may or may not contain free FAT entry. |
|
753 |
//-- in this case we need to repopulate corresponding bit cache entry. |
|
754 |
||
755 |
const TUint32 numEntries = dataLen >> KFat32EntrySzLog2; //-- amount of FAT entries in this sector |
|
756 |
const TFat32Entry* pFat32Entry = (const TFat32Entry* )pData; |
|
757 |
||
758 |
TBool bHasFreeFatEntry = EFalse; |
|
759 |
||
760 |
for(TUint i=0; i<numEntries; ++i) |
|
761 |
{//-- look for free entries in this particular FAT cache sector. |
|
762 |
if(pFat32Entry[i] == KSpareCluster) |
|
763 |
{ |
|
764 |
bHasFreeFatEntry = ETrue; |
|
765 |
break; |
|
766 |
} |
|
767 |
} |
|
768 |
||
769 |
if(!bHasFreeFatEntry) |
|
770 |
{ //-- FAT bit cache indicates that FAT sector absSectorNum has free entries, but it doesn't. |
|
771 |
//-- this is because we can only set "has free entry" flag in CAtaFatTable::WriteL(). |
|
772 |
//-- correct FAT bit cache entry |
|
773 |
pFatBitCache->SetFreeEntryInFatSector(absSectorNum, EFalse); |
|
774 |
||
775 |
//__PRINT2(_L("#++ :DoWriteSectorL() Fixed FAT bit cache BitVec[%d]=%d"), absSectorNum, pFatBitCache->FatSectorHasFreeEntry(absSectorNum)); |
|
776 |
} |
|
777 |
||
778 |
} |
|
779 |
else //if(pBitCache->FatSectorHasFreeEntry(absSectorNum)) |
|
780 |
{//-- don't need to do anything. The corresponding FAT cache sector never contained free FAT entry and |
|
781 |
//-- free FAT entry has never been written there in CAtaFatTable::WriteL(). |
|
782 |
} |
|
783 |
||
784 |
}//if(pFatBitCache && pFatBitCache->UsableState()) |
|
785 |
||
786 |
||
787 |
} |
|
788 |
||
789 |
||
790 |
//----------------------------------------------------------------------------- |
|
791 |
/** |
|
792 |
Write FAT32 entry at aFatIndex to the cache. Note that the data are not written to the media, only to the cache page. |
|
793 |
Corresponding page sector is marked as dirty and will be flushed on FlushL() call later. |
|
794 |
||
795 |
1. If the entry at aFatIndex doesn't belong to this page, returns EFalse |
|
796 |
2. If page's data are valid and the entry is cached, copies data to the page buffer and marks sector as dirty. |
|
797 |
3. If page's data are invalid but the entry's index belongs to this page, firstly reads data from the media and goto 2 |
|
798 |
||
799 |
@param aFatIndex entry's absolute FAT index (from the FAT start) |
|
800 |
@param aFatEntry FAT32 entry value |
|
801 |
@return ETrue if the entry at aFatIndex belongs to this page (cached) and in this case aResult will contain this entry. |
|
802 |
EFalse if the entry isn't cached. |
|
803 |
||
804 |
*/ |
|
805 |
TBool CFat32LruCachePage::WriteCachedEntryL(TUint32 aFatIndex, TUint32 aFatEntry) |
|
806 |
{ |
|
807 |
||
808 |
if(!IsEntryCached(aFatIndex)) |
|
809 |
return EFalse; //-- the page doesn't contain required index |
|
810 |
||
811 |
if(!IsValid()) |
|
812 |
{//-- we are trying to write data to the page that has invalid data. //-- read the data from the media first. |
|
813 |
ReadFromMediaL(aFatIndex); |
|
814 |
} |
|
815 |
||
816 |
//-- for FAT32 only low 28 bits are used, 4 high are reserved; preserve them |
|
817 |
TFat32Entry* pEntry = GetEntryPtr(aFatIndex); |
|
818 |
const TFat32Entry orgEntry = *pEntry; |
|
819 |
*pEntry = (orgEntry & ~KFat32EntryMask) | (aFatEntry & KFat32EntryMask); |
|
820 |
||
821 |
//-- mark corresponding sector of the cache page as dirty |
|
822 |
const TUint entryIndexInPage = aFatIndex & (EntriesInPage()-1); //-- number of entries in page is always a power of 2 |
|
823 |
const TUint dirtySectorNum = entryIndexInPage >> (iCache.SectorSizeLog2() - KFat32EntrySzLog2); |
|
824 |
||
825 |
ASSERT(dirtySectorNum < iCache.SectorsInPage()); |
|
826 |
||
827 |
iDirtySectors.SetBit(dirtySectorNum); |
|
828 |
SetState(EDirty); //-- mark page as dirty. |
|
829 |
||
830 |
return ETrue; |
|
831 |
} |
|
832 |
||
833 |
||
834 |
||
835 |
//################################################################################################################################# |
|
836 |
// CFatBitCache implementation |
|
837 |
//################################################################################################################################# |
|
838 |
||
839 |
//-- define this macro for extra debugging facilities for the CFatBitCache |
|
840 |
//-- probably needs to be removed completely as soon as everything settles |
|
841 |
//#define FAT_BIT_CACHE_DEBUG |
|
842 |
||
843 |
//----------------------------------------------------------------------------- |
|
844 |
||
845 |
CFatBitCache::CFatBitCache(CFat32LruCache& aOnwerFatCache) |
|
846 |
:iOwnerFatCache(aOnwerFatCache) |
|
847 |
{ |
|
848 |
SetState(EInvalid); |
|
849 |
DBG_STATEMENT(iPopulatingThreadId=0); |
|
850 |
} |
|
851 |
||
852 |
CFatBitCache::~CFatBitCache() |
|
853 |
{ |
|
854 |
Close(); |
|
855 |
} |
|
856 |
||
857 |
//----------------------------------------------------------------------------- |
|
858 |
/** |
|
859 |
FAT bit supercache factory method |
|
860 |
@return pointer to the created object or NULL if it coud not create or initialise it. |
|
861 |
*/ |
|
862 |
CFatBitCache* CFatBitCache::New(CFat32LruCache& aOnwerFatCache) |
|
863 |
{ |
|
864 |
__PRINT(_L("#++ CFatBitCache::New()")); |
|
865 |
||
866 |
CFatBitCache* pSelf = NULL; |
|
867 |
pSelf = new CFatBitCache(aOnwerFatCache); |
|
868 |
||
869 |
if(!pSelf) |
|
870 |
return NULL; //-- failed to create object |
|
871 |
||
872 |
TInt nRes = pSelf->Initialise(); |
|
873 |
if(nRes != KErrNone) |
|
874 |
{//-- failed to initialise the object |
|
875 |
delete pSelf; |
|
876 |
pSelf = NULL; |
|
877 |
} |
|
878 |
||
879 |
return pSelf; |
|
880 |
} |
|
881 |
||
882 |
||
883 |
//----------------------------------------------------------------------------- |
|
884 |
||
885 |
/** |
|
886 |
Initialisation. |
|
887 |
Note that this cache suports FAT32 only. |
|
888 |
@return KErrNone on success; otherwise standard error code. |
|
889 |
*/ |
|
890 |
TInt CFatBitCache::Initialise() |
|
891 |
{ |
|
892 |
__PRINT(_L("#++ CFatBitCache::Initialise()")); |
|
893 |
||
894 |
Close(); |
|
895 |
||
896 |
//-- only FAT32 supported |
|
897 |
if(iOwnerFatCache.FatType() != EFat32) |
|
898 |
{ |
|
899 |
ASSERT(0); |
|
900 |
Fault(EFatCache_BadFatType); |
|
901 |
} |
|
902 |
||
903 |
//-- create the bit vector. each bit position there represents one FAT cache sector (in FAT cache page terms, see FAT page structure) |
|
904 |
const TUint fatSize = iOwnerFatCache.FatSize(); //-- FAT size in bytes |
|
905 |
const TUint fatCacheSecSize = Pow2(iOwnerFatCache.SectorSizeLog2()); //-- FAT cache sector size |
|
906 |
const TUint maxFatUsableCacheSectors = (fatSize + (fatCacheSecSize-1)) >> iOwnerFatCache.SectorSizeLog2(); //-- maximal number of usable fat cache sectors in whole FAT table |
|
907 |
||
908 |
//-- create a bit vector |
|
909 |
__PRINT1(_L("#++ CFatBitCache::Initialise() FAT supercache bits:%u"), maxFatUsableCacheSectors); |
|
910 |
||
911 |
TInt nRes = iBitCache.Create(maxFatUsableCacheSectors); |
|
912 |
if(nRes != KErrNone) |
|
913 |
{ |
|
914 |
__PRINT1(_L("#++ Failed to create a bit vector! code:%d"), nRes); |
|
915 |
return nRes; |
|
916 |
} |
|
917 |
||
918 |
//-- calculate the coefficient to be used to convet FAT index to FAT cache sector number (bit vector index). |
|
919 |
iFatIdxToSecCoeff = iOwnerFatCache.SectorSizeLog2()-KFat32EntrySzLog2; |
|
920 |
SetState(ENotPopulated); |
|
921 |
||
922 |
return KErrNone; |
|
923 |
} |
|
924 |
||
925 |
//----------------------------------------------------------------------------- |
|
926 |
/** |
|
927 |
Closes the cache and deallocates bit vector memory. |
|
928 |
*/ |
|
929 |
void CFatBitCache::Close() |
|
930 |
{ |
|
931 |
__PRINT(_L("#++ CFatBitCache::Close()")); |
|
932 |
||
933 |
//-- this method must not be called during populating (optionally by another thread) |
|
934 |
ASSERT(State() != EPopulating); |
|
935 |
ASSERT(iPopulatingThreadId == 0); |
|
936 |
||
937 |
iBitCache.Close(); |
|
938 |
SetState(EInvalid); |
|
939 |
} |
|
940 |
||
941 |
//----------------------------------------------------------------------------- |
|
942 |
||
943 |
/** |
|
944 |
Tell the cache that we are starting to populate it. |
|
945 |
N.B. Start, Finish and populating methods shall be called from the same thread. |
|
946 |
Only one thread can be populating the bit vector; |
|
947 |
||
948 |
@return ETrue on success. Efalse means that the cache is in the invalid state for some reason. |
|
949 |
*/ |
|
950 |
TBool CFatBitCache::StartPopulating() |
|
951 |
{ |
|
952 |
__PRINT2(_L("#++ CFatBitCache::StartPopulating(), State:%d, ThreadId:%d"), State(), (TUint)RThread().Id()); |
|
953 |
||
954 |
if(State() != ENotPopulated) |
|
955 |
{//-- wrong state |
|
956 |
ASSERT(0); |
|
957 |
return EFalse; |
|
958 |
} |
|
959 |
||
960 |
ASSERT(iPopulatingThreadId == 0); |
|
961 |
||
962 |
iBitCache.Fill(0); |
|
963 |
SetState(EPopulating); |
|
964 |
||
965 |
//-- store the the ID of the thread that starts populating the cache; it'll be checked later during populating. |
|
966 |
DBG_STATEMENT(iPopulatingThreadId = RThread().Id()); |
|
967 |
||
968 |
return ETrue; |
|
969 |
} |
|
970 |
||
971 |
//----------------------------------------------------------------------------- |
|
972 |
||
973 |
/** |
|
974 |
Tell the cache that we have finished to populate it. |
|
975 |
||
976 |
@return ETrue on success. EFalse means that the cache is in the invalid state for some reason. |
|
977 |
*/ |
|
978 |
TBool CFatBitCache::FinishPopulating(TBool aSuccess) |
|
979 |
{ |
|
980 |
__PRINT2(_L("#++ CFatBitCache::PopulatingFinished(), ThreadId:%d, success:%d"), (TUint)RThread().Id(), aSuccess); |
|
981 |
||
982 |
if(State() != EPopulating) |
|
983 |
{//-- wrong state |
|
984 |
ASSERT(0); |
|
985 |
return EFalse; |
|
986 |
} |
|
987 |
||
988 |
ASSERT(iPopulatingThreadId == RThread().Id()); //-- check that this method is called from the same thread that started populating |
|
989 |
DBG_STATEMENT(iPopulatingThreadId = 0); |
|
990 |
||
991 |
if(aSuccess) |
|
992 |
SetState(EPopulated); //-- the cache is usable; populated OK |
|
993 |
else |
|
994 |
SetState(EInvalid); //-- the cache isn't populated properly, make it not usable |
|
995 |
||
996 |
return ETrue; |
|
997 |
} |
|
998 |
||
999 |
//----------------------------------------------------------------------------- |
|
1000 |
/** |
|
1001 |
Tell FAT bit cache that there is a free entry at FAT aFatIndex. |
|
1002 |
Only this method can be used to populate the bit array (in EPopulating state). |
|
1003 |
Other methods can't access bit array in EPopulating state i.e. it is safe to populate the cache |
|
1004 |
from the thread other than FS drive thread (e.g within background FAT scan) |
|
1005 |
||
1006 |
@param aFatIndex free FAT32 entry index |
|
1007 |
@return ETrue on success. EFalse means that the cache is in the invalid state for some reason. |
|
1008 |
*/ |
|
1009 |
TBool CFatBitCache::SetFreeFatEntry(TUint32 aFatIndex) |
|
1010 |
{ |
|
1011 |
//__PRINT3(_L("#++ ReportFreeFatEntry: idx:%d, state:%s, tid:%d"), aFatIndex, State(), (TUint)RThread().Id()); |
|
1012 |
||
1013 |
if(State() != EPopulating && State() != EPopulated) |
|
1014 |
{//-- wrong state, this can happen if someone forcedly invalidated this cache during populating |
|
1015 |
return EFalse; |
|
1016 |
} |
|
1017 |
||
1018 |
#if defined _DEBUG && defined FAT_BIT_CACHE_DEBUG |
|
1019 |
//-- This leads to serious performance degradation, so be careful with it. |
|
1020 |
if(State() == EPopulating) |
|
1021 |
{//-- check that this method is called from the same thread that started populating |
|
1022 |
if(iPopulatingThreadId != RThread().Id()) |
|
1023 |
{ |
|
1024 |
__PRINT3(_L("#++ !! ReportFreeFatEntry: Access from different thread!! idx:%d, state:%d, tid:%d"), aFatIndex, State(), (TUint)RThread().Id()); |
|
1025 |
} |
|
1026 |
//ASSERT(iPopulatingThreadId == RThread().Id()); |
|
1027 |
} |
|
1028 |
#endif |
|
1029 |
||
1030 |
//-- set bit to '1' which indicates that the FAT cache sector corresponding to the aFatIndex has at least one free FAT entry |
|
1031 |
const TUint32 bitNumber = FatIndexToCacheSectorNumber(aFatIndex); //-- index in the bit array corresponding FAT cache sector |
|
1032 |
||
1033 |
#if defined _DEBUG && defined FAT_BIT_CACHE_DEBUG |
|
1034 |
//-- This leads to serious performance degradation, so be careful with it. |
|
1035 |
TBool b = iBitCache[bitNumber]; |
|
1036 |
if(!b && State()==EPopulated) |
|
1037 |
{//-- someone is reporting a free entry in the given cache sector. |
|
1038 |
__PRINT1(_L("#++ CFatBitCache::ReportFreeFatEntry BitVec[%d]=1"), bitNumber); |
|
1039 |
} |
|
1040 |
#endif |
|
1041 |
||
1042 |
||
1043 |
iBitCache.SetBit(bitNumber); |
|
1044 |
||
1045 |
return ETrue; |
|
1046 |
} |
|
1047 |
||
1048 |
//----------------------------------------------------------------------------- |
|
1049 |
/** |
|
1050 |
Forcedly mark a part of the FAT bit super cache as containing free clusters (or not). |
|
1051 |
||
1052 |
@param aStartFatIndex start FAT index of the range |
|
1053 |
@param aEndFatIndex end FAT index of the range |
|
1054 |
@param aAsFree if ETrue, the range will be marked as containing free clusters |
|
1055 |
*/ |
|
1056 |
void CFatBitCache::MarkFatRange(TUint32 aStartFatIndex, TUint32 aEndFatIndex, TBool aAsFree) |
|
1057 |
{ |
|
1058 |
__PRINT3(_L("#++ CFatBitCache::MarkFatRange(%d, %d, %d)"), aStartFatIndex, aEndFatIndex, aAsFree); |
|
1059 |
||
1060 |
ASSERT(State() == EPopulating || State() == EPopulated); |
|
1061 |
||
1062 |
const TUint32 bitNumberStart = FatIndexToCacheSectorNumber(aStartFatIndex); |
|
1063 |
const TUint32 bitNumberEnd = FatIndexToCacheSectorNumber(aEndFatIndex); |
|
1064 |
||
1065 |
iBitCache.Fill(bitNumberStart, bitNumberEnd, aAsFree); |
|
1066 |
} |
|
1067 |
||
1068 |
||
1069 |
//----------------------------------------------------------------------------- |
|
1070 |
/** |
|
1071 |
Try to locate closest to the aFatIndex free FAT entry in the FAT32 LRU cache. |
|
1072 |
This is done by several steps: |
|
1073 |
||
1074 |
1. Try to find FAT cache sector containing free FAT entry (by using FAT sectors bitmap) |
|
1075 |
2. locate free FAT entry within this sector. |
|
1076 |
||
1077 |
@param aFatIndex in: absolute FAT entry index that will be used to start search from (we need to find the closest free entry to it) |
|
1078 |
out: may contain FAT index of the located free entry. |
|
1079 |
||
1080 |
@return one of the completion codes: |
|
1081 |
KErrNone free entry found and its index is in aFatIndex |
|
1082 |
KErrNotFound FAT sector closest to the aFatIndex entry doesn't contain free FAT entries; the conflict is resolved, need to call this method again |
|
1083 |
KErrEof couldn't find any free sectors in FAT; need to fall back to the old search method |
|
1084 |
KErrCorrupt if the state of the cache is inconsistent |
|
1085 |
*/ |
|
1086 |
TInt CFatBitCache::FindClosestFreeFatEntry(TUint32& aFatIndex) |
|
1087 |
{ |
|
1088 |
const TUint32 startFatCacheSec = FatIndexToCacheSectorNumber(aFatIndex); |
|
1089 |
||
1090 |
//__PRINT2(_L("#++ CFatBitCache::FindClosestFreeFatEntry() start idx:%d, start cache sec:%d"), aFatIndex, startFatCacheSec); |
|
1091 |
||
1092 |
ASSERT(aFatIndex >= KFatFirstSearchCluster); |
|
1093 |
if(!UsableState()) |
|
1094 |
{ |
|
1095 |
ASSERT(0); |
|
1096 |
return KErrCorrupt; |
|
1097 |
} |
|
1098 |
||
1099 |
TUint32 fatSeekCacheSec = startFatCacheSec; //-- FAT cache sector number that has free FAT entry, used for search . |
|
1100 |
TUint32 fatSeekIndex = aFatIndex; //-- FAT index to start search with |
|
1101 |
||
1102 |
//-- 1. look if FAT sector that corresponds to the aStartFatIndex already has free entries. |
|
1103 |
//-- 2. if not, try to locate closest FAT cache sector that has by searching a bit vector |
|
1104 |
if(FatSectorHasFreeEntry(fatSeekCacheSec)) |
|
1105 |
{ |
|
1106 |
} |
|
1107 |
else |
|
1108 |
{//-- look in iBitCache for '1' entries nearest to the fatCacheSec, right side priority |
|
1109 |
||
1110 |
if(!iBitCache.Find(fatSeekCacheSec, 1, RBitVector::ENearestR)) |
|
1111 |
{//-- strange situation, there are no '1' bits in whole vector, search failed |
|
1112 |
__PRINT(_L("#++ CFatBitCache::FindClosestFreeFatEntry() bit vector search failed!")); |
|
1113 |
return KErrEof; |
|
1114 |
} |
|
1115 |
||
1116 |
//-- bit cache found FAT sector(fatSeekCacheSec) that may have free FAT entries |
|
1117 |
//-- calculate FAT entry start index in this sector |
|
1118 |
fatSeekIndex = Max(KFatFirstSearchCluster, CacheSectorNumberToFatIndex(fatSeekCacheSec)); |
|
1119 |
} |
|
1120 |
||
1121 |
//-- here we have absolute FAT cache sector number, which may contain at least one free FAT entty |
|
1122 |
ASSERT(FatSectorHasFreeEntry(fatSeekCacheSec)); |
|
1123 |
||
1124 |
//-- ask FAT cache to find the exact index of free FAT entry in this particular FAT cache sector |
|
1125 |
TInt nRes; |
|
1126 |
TBool bFreeEntryFound=EFalse; |
|
1127 |
||
1128 |
TRAP(nRes, bFreeEntryFound = iOwnerFatCache.FindFreeEntryInCacheSectorL(fatSeekIndex)); |
|
1129 |
if(nRes != KErrNone) |
|
1130 |
{//-- it's possible on media read error |
|
1131 |
return KErrCorrupt; |
|
1132 |
} |
|
1133 |
||
1134 |
if(bFreeEntryFound) |
|
1135 |
{//-- found free entry at aNewFreeEntryIndex |
|
1136 |
aFatIndex = fatSeekIndex; |
|
1137 |
return KErrNone; |
|
1138 |
} |
|
1139 |
||
1140 |
//-- bit cache mismatch; its entry ('1') indicates that cache sector number fatCacheSec has free FAT entries, |
|
1141 |
//-- while in reality it doesnt. We need to fix the bit cache. |
|
1142 |
//__PRINT1(_L("#++ CFatBitCache::FindClosestFreeFatEntry fixing cache conflict; BitVec[%d]=0"), fatSeekCacheSec); |
|
1143 |
SetFreeEntryInFatSector(fatSeekCacheSec, EFalse); |
|
1144 |
||
1145 |
return KErrNotFound; |
|
1146 |
} |
|
1147 |
||
1148 |
||
1149 |
//----------------------------------------------------------------------------- |
|
1150 |
/** |
|
1151 |
Print out the contents of the object. This is a debug only method |
|
1152 |
*/ |
|
1153 |
void CFatBitCache::Dump() const |
|
1154 |
{ |
|
1155 |
#if defined _DEBUG && defined FAT_BIT_CACHE_DEBUG |
|
1156 |
||
1157 |
const TUint32 vecSz = iBitCache.Size(); |
|
1158 |
__PRINT2(_L("#++ CFatBitCache::Dump(): state:%d, entries:%d"), State(), vecSz); |
|
1159 |
||
1160 |
||
1161 |
TBuf<120> printBuf; |
|
1162 |
const TUint KPrintEntries = 32; |
|
1163 |
||
1164 |
TUint i; |
|
1165 |
printBuf.Append(_L(" ")); |
|
1166 |
for(i=0; i<KPrintEntries; ++i) |
|
1167 |
{ |
|
1168 |
printBuf.AppendFormat(_L("%02d "),i); |
|
1169 |
} |
|
1170 |
||
1171 |
__PRINT(printBuf); |
|
1172 |
for(i=0; i<vecSz;) |
|
1173 |
{ |
|
1174 |
printBuf.Format(_L("%03d: "), i); |
|
1175 |
for(TInt j=0; j<KPrintEntries; ++j) |
|
1176 |
{ |
|
1177 |
if(i >= vecSz) |
|
1178 |
break; |
|
1179 |
||
1180 |
printBuf.AppendFormat(_L("% d "), (iBitCache[i]!=0)); |
|
1181 |
++i; |
|
1182 |
} |
|
1183 |
__PRINT(printBuf); |
|
1184 |
||
1185 |
} |
|
1186 |
#endif |
|
1187 |
} |
|
1188 |
||
1189 |
||
1190 |
||
1191 |
||
1192 |
||
1193 |
||
1194 |
||
1195 |
||
1196 |
||
1197 |
||
1198 |
||
1199 |
||
1200 |
||
1201 |
||
1202 |
||
1203 |
||
1204 |
||
1205 |
||
1206 |
||
1207 |
||
1208 |
||
1209 |
||
1210 |
||
1211 |
||
1212 |
||
1213 |
||
1214 |
||
1215 |
||
1216 |
||
1217 |
||
1218 |
||
1219 |
||
1220 |
||
1221 |
||
1222 |
||
1223 |
||
1224 |
||
1225 |
||
1226 |
||
1227 |
||
1228 |