author | Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com> |
Sat, 20 Feb 2010 00:10:51 +0200 | |
branch | RCL_3 |
changeset 62 | 4a8fed1c0ef6 |
parent 36 | 538db54a451d |
child 87 | 2f92ad2dc5db |
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\fat_table32.cpp |
|
15 |
// FAT32 File Allocation Table classes implementation |
|
16 |
// |
|
17 |
// |
|
18 |
||
19 |
/** |
|
20 |
@file |
|
21 |
@internalTechnology |
|
22 |
*/ |
|
23 |
||
24 |
||
25 |
||
26 |
#include "sl_std.h" |
|
27 |
#include "sl_fatcache32.h" |
|
28 |
#include "fat_table32.h" |
|
29 |
||
30 |
||
31 |
||
32 |
||
33 |
//--------------------------------------------------------------------------------------------------------------------------------------- |
|
34 |
/** |
|
35 |
Implements automatic locking object. |
|
33
0173bcd7697c
Revision: 201001
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
36 |
Calls TDriveInterface::AcquireLock() on construction and TDriveInterface::ReleaseLock() on destruction. |
0 | 37 |
Can be constructed on the stack only. |
38 |
*/ |
|
39 |
class XAutoLock |
|
40 |
{ |
|
41 |
public: |
|
42 |
inline XAutoLock(CFatMountCB* apOwner) : iDrv(apOwner->DriveInterface()) {iDrv.AcquireLock();} |
|
33
0173bcd7697c
Revision: 201001
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
43 |
inline XAutoLock(TDriveInterface& aDrv) : iDrv(aDrv) {iDrv.AcquireLock();} |
0 | 44 |
inline ~XAutoLock() {iDrv.ReleaseLock();} |
45 |
||
46 |
private: |
|
47 |
void* operator new(TUint); //-- disable creating objects on heap. |
|
48 |
void* operator new(TUint, void*); |
|
49 |
||
50 |
private: |
|
33
0173bcd7697c
Revision: 201001
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
51 |
TDriveInterface &iDrv; ///< reference to the drive interface |
0 | 52 |
}; |
53 |
||
54 |
||
55 |
//--------------------------------------------------------------------------------------------------------------------------------------- |
|
56 |
||
57 |
||
58 |
||
59 |
//####################################################################################################################################### |
|
60 |
//# CFatTable class implementation |
|
61 |
//####################################################################################################################################### |
|
62 |
||
63 |
/** |
|
64 |
FAT object factory method. |
|
65 |
Constructs either CAtaFatTable or CRamFatTable depending on the media type parameter |
|
66 |
||
67 |
@param aOwner Pointer to the owning mount |
|
68 |
@param aLocDrvCaps local drive attributes |
|
69 |
@leave KErrNoMemory |
|
70 |
@return Pointer to the Fat table |
|
71 |
*/ |
|
72 |
CFatTable* CFatTable::NewL(CFatMountCB& aOwner, const TLocalDriveCaps& aLocDrvCaps) |
|
73 |
{ |
|
74 |
CFatTable* pFatTable=NULL; |
|
75 |
||
76 |
switch(aLocDrvCaps.iType) |
|
77 |
{ |
|
78 |
case EMediaRam: |
|
79 |
{//-- this is RAM media, try to create CRamFatTable instance. |
|
80 |
const TFatType fatType = aOwner.FatType(); |
|
81 |
||
82 |
if(fatType != EFat16 && fatType != EFat32) |
|
83 |
{//-- CRamFatTable doesn't support FAT12, FAT16 & FAT32 only. |
|
84 |
__PRINT1(_L("CFatTable::NewL() CRamFatTable doesn't support this FAT type:%d"), fatType); |
|
85 |
ASSERT(0); |
|
86 |
return NULL; |
|
87 |
} |
|
88 |
||
89 |
pFatTable = CRamFatTable::NewL(aOwner); |
|
90 |
} |
|
91 |
break; |
|
92 |
||
93 |
default: |
|
94 |
//-- other media |
|
95 |
pFatTable = CAtaFatTable::NewL(aOwner); |
|
96 |
break; |
|
97 |
}; |
|
98 |
||
99 |
return pFatTable; |
|
100 |
} |
|
101 |
||
102 |
||
103 |
CFatTable::CFatTable(CFatMountCB& aOwner) |
|
104 |
{ |
|
105 |
iOwner = &aOwner; |
|
106 |
ASSERT(iOwner); |
|
107 |
} |
|
108 |
||
109 |
CFatTable::~CFatTable() |
|
110 |
{ |
|
111 |
//-- destroy cache ignoring dirty data in cache |
|
112 |
//-- the destructor isn't an appropriate place to flush the data. |
|
113 |
Dismount(ETrue); |
|
114 |
} |
|
115 |
||
116 |
//----------------------------------------------------------------------------- |
|
117 |
||
118 |
/** |
|
119 |
Initialise the object, get data from the owning CFatMountCB |
|
120 |
*/ |
|
121 |
void CFatTable::InitializeL() |
|
122 |
{ |
|
123 |
ASSERT(iOwner); |
|
124 |
||
125 |
//-- get FAT type from the owner |
|
126 |
iFatType = iOwner->FatType(); |
|
127 |
ASSERT(IsFat12() || IsFat16() || IsFat32()); |
|
128 |
||
33
0173bcd7697c
Revision: 201001
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
129 |
//-- set the EOC code |
0173bcd7697c
Revision: 201001
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
130 |
iFatEocCode = EocCodeByFatType(iFatType); |
0173bcd7697c
Revision: 201001
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
131 |
|
0173bcd7697c
Revision: 201001
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
132 |
|
0173bcd7697c
Revision: 201001
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
133 |
|
0173bcd7697c
Revision: 201001
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
134 |
|
0 | 135 |
iFreeClusterHint = KFatFirstSearchCluster; |
136 |
||
137 |
//-- cache the media attributes |
|
138 |
TLocalDriveCapsV2 caps; |
|
139 |
TPckg<TLocalDriveCapsV2> capsPckg(caps); |
|
140 |
User::LeaveIfError(iOwner->LocalDrive()->Caps(capsPckg)); |
|
141 |
iMediaAtt = caps.iMediaAtt; |
|
142 |
||
143 |
//-- obtain maximal number of entries in the table |
|
144 |
iMaxEntries = iOwner->UsableClusters()+KFatFirstSearchCluster; //-- FAT[0] & FAT[1] are not in use |
|
145 |
||
146 |
__PRINT3(_L("CFatTable::InitializeL(), drv:%d, iMediaAtt = %08X, max Entries:%d"), iOwner->DriveNumber(), iMediaAtt, iMaxEntries); |
|
147 |
} |
|
148 |
||
149 |
//----------------------------------------------------------------------------- |
|
150 |
||
151 |
/** |
|
152 |
Decrements the free cluster count. |
|
153 |
Note that can be quite expensive operation (especially for overrides with synchronisation), if it is called for every |
|
154 |
cluster of a large file. Use more than one cluster granularity. |
|
155 |
||
156 |
@param aCount a number of clusters |
|
157 |
*/ |
|
158 |
void CFatTable::DecrementFreeClusterCount(TUint32 aCount) |
|
159 |
{ |
|
160 |
__ASSERT_DEBUG(iFreeClusters >= aCount, Fault(EFatCorrupt)); |
|
161 |
iFreeClusters -= aCount; |
|
162 |
} |
|
163 |
||
164 |
/** |
|
165 |
Increments the free cluster count. |
|
166 |
Note that can be quite expensive operation (especially for overrides with synchronisation), if it is called for every |
|
167 |
cluster of a large file. Use more than one cluster granularity. |
|
168 |
||
169 |
@param aCount a number of clusters |
|
170 |
*/ |
|
171 |
void CFatTable::IncrementFreeClusterCount(TUint32 aCount) |
|
172 |
{ |
|
173 |
const TUint32 newVal = iFreeClusters+aCount; |
|
174 |
__ASSERT_DEBUG(newVal<=MaxEntries(), Fault(EFatCorrupt)); |
|
175 |
||
176 |
iFreeClusters = newVal; |
|
177 |
} |
|
178 |
||
179 |
/** @return number of free clusters in the FAT */ |
|
180 |
TUint32 CFatTable::NumberOfFreeClusters(TBool /*aSyncOperation=EFalse*/) const |
|
181 |
{ |
|
182 |
return FreeClusters(); |
|
183 |
} |
|
184 |
||
185 |
void CFatTable::SetFreeClusters(TUint32 aFreeClusters) |
|
186 |
{ |
|
187 |
iFreeClusters=aFreeClusters; |
|
188 |
} |
|
189 |
||
190 |
/** |
|
191 |
Get the hint about the last known free cluster number. |
|
192 |
Note that can be quite expensive operation (especially for overrides with synchronisation), if it is called for every |
|
193 |
cluster of a large file. |
|
194 |
||
195 |
@return cluster number supposedly close to the free one. |
|
196 |
*/ |
|
197 |
TUint32 CFatTable::FreeClusterHint() const |
|
198 |
{ |
|
199 |
ASSERT(ClusterNumberValid(iFreeClusterHint)); |
|
200 |
return iFreeClusterHint; |
|
201 |
} |
|
202 |
||
203 |
/** |
|
204 |
Set a free cluster hint. The next search fro the free cluster can start from this value. |
|
205 |
aCluster doesn't have to be a precise number of free FAT entry; it just needs to be as close as possible to the |
|
206 |
free entries chain. |
|
207 |
Note that can be quite expensive operation (especially for overrides with synchronisation), if it is called for every |
|
208 |
cluster of a large file. |
|
209 |
||
210 |
@param aCluster cluster number hint. |
|
211 |
*/ |
|
212 |
void CFatTable::SetFreeClusterHint(TUint32 aCluster) |
|
213 |
{ |
|
214 |
ASSERT(ClusterNumberValid(aCluster)); |
|
215 |
iFreeClusterHint=aCluster; |
|
216 |
} |
|
217 |
||
218 |
//----------------------------------------------------------------------------- |
|
219 |
||
220 |
/** |
|
221 |
Find out the number of free clusters on the volume. |
|
222 |
Reads whole FAT and counts free clusters. |
|
223 |
*/ |
|
224 |
void CFatTable::CountFreeClustersL() |
|
225 |
{ |
|
226 |
__PRINT1(_L("#- CFatTable::CountFreeClustersL(), drv:%d"), iOwner->DriveNumber()); |
|
227 |
||
228 |
const TUint32 KUsableClusters = iOwner->UsableClusters(); |
|
229 |
(void)KUsableClusters; |
|
230 |
||
231 |
TUint32 freeClusters = 0; |
|
232 |
TUint32 firstFreeCluster = 0; |
|
233 |
||
234 |
TTime timeStart; |
|
235 |
TTime timeEnd; |
|
236 |
timeStart.UniversalTime(); //-- take start time |
|
237 |
||
238 |
//-- walk through whole FAT table looking for free clusters |
|
239 |
for(TUint i=KFatFirstSearchCluster; i<MaxEntries(); ++i) |
|
240 |
{ |
|
241 |
if(ReadL(i) == KSpareCluster) |
|
242 |
{//-- found a free cluster |
|
243 |
++freeClusters; |
|
244 |
||
245 |
if(!firstFreeCluster) |
|
246 |
firstFreeCluster = i; |
|
247 |
} |
|
248 |
} |
|
249 |
||
250 |
timeEnd.UniversalTime(); //-- take end time |
|
251 |
const TInt msScanTime = (TInt)( (timeEnd.MicroSecondsFrom(timeStart)).Int64() / K1mSec); |
|
252 |
__PRINT1(_L("#- CFatTable::CountFreeClustersL() finished. Taken:%d ms"), msScanTime); |
|
253 |
(void)msScanTime; |
|
254 |
||
255 |
if(!firstFreeCluster) //-- haven't found free clusters on the volume |
|
256 |
firstFreeCluster = KFatFirstSearchCluster; |
|
257 |
||
258 |
ASSERT(freeClusters <= KUsableClusters); |
|
259 |
||
260 |
SetFreeClusters(freeClusters); |
|
261 |
SetFreeClusterHint(firstFreeCluster); |
|
262 |
} |
|
263 |
||
264 |
//----------------------------------------------------------------------------- |
|
265 |
||
266 |
/** |
|
267 |
Count the number of contiguous cluster from a start cluster |
|
268 |
||
269 |
@param aStartCluster cluster to start counting from |
|
270 |
@param anEndCluster contains the end cluster number upon return |
|
271 |
@param aMaxCount Maximum cluster required |
|
272 |
@leave System wide error values |
|
273 |
@return Number of contiguous clusters from aStartCluster. |
|
274 |
*/ |
|
275 |
TInt CFatTable::CountContiguousClustersL(TUint32 aStartCluster,TInt& anEndCluster,TUint32 aMaxCount) const |
|
276 |
{ |
|
277 |
__PRINT2(_L("CFatTable::CountContiguousClustersL() start:%d, max:%d"),aStartCluster, aMaxCount); |
|
278 |
TUint32 clusterListLen=1; |
|
279 |
TInt endCluster=aStartCluster; |
|
280 |
TInt64 endClusterPos=DataPositionInBytes(endCluster); |
|
281 |
while (clusterListLen<aMaxCount) |
|
282 |
{ |
|
283 |
TInt oldCluster=endCluster; |
|
284 |
TInt64 oldClusterPos=endClusterPos; |
|
285 |
if (GetNextClusterL(endCluster)==EFalse || (endClusterPos=DataPositionInBytes(endCluster))!=(oldClusterPos+(1<<iOwner->ClusterSizeLog2()))) |
|
286 |
{ |
|
287 |
endCluster=oldCluster; |
|
288 |
break; |
|
289 |
} |
|
290 |
clusterListLen++; |
|
291 |
} |
|
292 |
anEndCluster=endCluster; |
|
293 |
return(clusterListLen); |
|
294 |
} |
|
295 |
||
296 |
//----------------------------------------------------------------------------- |
|
297 |
||
298 |
/** |
|
299 |
Extend a file or directory cluster chain, leaves if there are no free clusters (the disk is full). |
|
300 |
||
301 |
@param aNumber amount of clusters to allocate |
|
302 |
@param aCluster FAT entry index to start with. |
|
303 |
||
304 |
@leave KErrDiskFull + system wide error codes |
|
305 |
*/ |
|
306 |
void CFatTable::ExtendClusterListL(TUint32 aNumber,TInt& aCluster) |
|
307 |
{ |
|
308 |
__PRINT2(_L("CFatTable::ExtendClusterListL() num:%d, clust:%d"), aNumber, aCluster); |
|
309 |
__ASSERT_DEBUG(aNumber>0,Fault(EFatBadParameter)); |
|
310 |
||
311 |
while(aNumber && GetNextClusterL(aCluster)) |
|
312 |
aNumber--; |
|
313 |
||
314 |
if(!aNumber) |
|
315 |
return; |
|
316 |
||
317 |
if(!RequestFreeClusters(aNumber)) |
|
318 |
{ |
|
319 |
__PRINT(_L("CFatTable::ExtendClusterListL - leaving KErrDirFull")); |
|
320 |
User::Leave(KErrDiskFull); |
|
321 |
} |
|
322 |
||
323 |
||
324 |
TUint32 freeCluster = 0; |
|
325 |
||
326 |
//-- note: this can be impoved by trying to fing as long chain of free clusters as possible in FindClosestFreeClusterL() |
|
327 |
for(TUint i=0; i<aNumber; ++i) |
|
328 |
{ |
|
329 |
freeCluster = FindClosestFreeClusterL(aCluster); |
|
330 |
WriteFatEntryEofL(freeCluster); // Must write EOF for FindClosestFreeCluster to work again |
|
331 |
WriteL(aCluster,freeCluster); |
|
332 |
aCluster=freeCluster; |
|
333 |
} |
|
334 |
||
335 |
//-- decrement number of available clusters |
|
336 |
DecrementFreeClusterCount(aNumber); |
|
337 |
||
338 |
//-- update free cluster hint, it isn't required to be a precise value, just a hint where to start the from from |
|
339 |
SetFreeClusterHint(aCluster); |
|
340 |
||
341 |
} |
|
342 |
||
343 |
//----------------------------------------------------------------------------- |
|
344 |
||
345 |
/** |
|
346 |
Allocate and mark as EOF a single cluster as close as possible to aNearestCluster |
|
347 |
||
348 |
@param aNearestCluster Cluster the new cluster should be nearest to |
|
349 |
@leave System wide error codes |
|
350 |
@return The cluster number allocated |
|
351 |
*/ |
|
352 |
TUint32 CFatTable::AllocateSingleClusterL(TUint32 aNearestCluster) |
|
353 |
{ |
|
354 |
__PRINT1(_L("CFatTable::AllocateSingleCluster() nearest:%d"), aNearestCluster); |
|
355 |
||
356 |
const TInt freeCluster=FindClosestFreeClusterL(aNearestCluster); |
|
357 |
WriteFatEntryEofL(freeCluster); |
|
358 |
DecrementFreeClusterCount(1); |
|
359 |
||
360 |
//-- update free cluster hint, it isn't required to be a precise value, just a hint where to start the from from. |
|
361 |
SetFreeClusterHint(freeCluster); |
|
362 |
||
363 |
return(freeCluster); |
|
364 |
} |
|
365 |
||
366 |
//----------------------------------------------------------------------------- |
|
367 |
||
368 |
/** |
|
369 |
Allocate and link a cluster chain, leaves if there are not enough free clusters. |
|
370 |
Chain starts as close as possible to aNearestCluster, last cluster will be marked as EOF. |
|
371 |
||
372 |
@param aNumber Number of clusters to allocate |
|
373 |
@param aNearestCluster Cluster the new chain should be nearest to |
|
374 |
@leave System wide error codes |
|
375 |
@return The first cluster number allocated |
|
376 |
*/ |
|
377 |
TUint32 CFatTable::AllocateClusterListL(TUint32 aNumber, TUint32 aNearestCluster) |
|
378 |
{ |
|
379 |
__PRINT2(_L("CFatTable::AllocateClusterList() N:%d,NearestCL:%d"),aNumber,aNearestCluster); |
|
380 |
__ASSERT_DEBUG(aNumber>0, Fault(EFatBadParameter)); |
|
381 |
||
382 |
if(!RequestFreeClusters(aNumber)) |
|
383 |
{ |
|
384 |
__PRINT(_L("CFatTable::AllocateClusterListL - leaving KErrDirFull")); |
|
385 |
User::Leave(KErrDiskFull); |
|
386 |
} |
|
387 |
||
388 |
TInt firstCluster = aNearestCluster = AllocateSingleClusterL(aNearestCluster); |
|
389 |
if (aNumber>1) |
|
390 |
ExtendClusterListL(aNumber-1, (TInt&)aNearestCluster); |
|
391 |
||
392 |
return(firstCluster); |
|
393 |
} |
|
394 |
||
395 |
//----------------------------------------------------------------------------- |
|
396 |
||
397 |
/** |
|
398 |
Notify the media drive about media areas that shall be treated as "deleted" if this feature is supported. |
|
399 |
@param aFreedClusters array with FAT numbers of clusters that shall be marked as "deleted" |
|
400 |
*/ |
|
401 |
void CFatTable::DoFreedClustersNotify(RClusterArray &aFreedClusters) |
|
402 |
{ |
|
403 |
ASSERT(iMediaAtt & KMediaAttDeleteNotify); |
|
404 |
||
405 |
const TUint clusterCount = aFreedClusters.Count(); |
|
406 |
||
407 |
if(!clusterCount) |
|
408 |
return; |
|
409 |
||
410 |
FlushL(); //-- Commit the FAT changes to disk first to be safe |
|
411 |
||
412 |
const TUint bytesPerCluster = 1 << iOwner->ClusterSizeLog2(); |
|
413 |
||
414 |
TInt64 byteAddress = 0; |
|
415 |
TUint deleteLen = 0; // zero indicates no clusters accumulated yet |
|
416 |
||
417 |
for (TUint i=0; i<clusterCount; ++i) |
|
418 |
{ |
|
419 |
const TUint currCluster = aFreedClusters[i]; |
|
420 |
||
421 |
if (deleteLen == 0) |
|
422 |
byteAddress = DataPositionInBytes(currCluster); //-- start of the media range |
|
423 |
||
424 |
deleteLen += bytesPerCluster; |
|
425 |
||
426 |
//-- if this is the last entry in the array or the net cluster number is not consecutive, notify the driver |
|
427 |
if ((i+1) == clusterCount || aFreedClusters[i+1] != (currCluster+1)) |
|
428 |
{ |
|
429 |
//__PRINT3(_L("DeleteNotify(%08X:%08X, %u), first cluster %u last cluster #%u"), I64HIGH(byteAddress), I64LOW(byteAddress), deleteLen); |
|
430 |
//__PRINT2(_L(" first cluster %u last cluster #%u"), I64LOW((byteAddress - iOwner->ClusterBasePosition()) >> iOwner->ClusterSizeLog2()) + 2, cluster); |
|
431 |
||
432 |
const TInt r = iOwner->LocalDrive()->DeleteNotify(byteAddress, deleteLen); |
|
433 |
if(r != KErrNone) |
|
434 |
{//-- if DeleteNotify() failed, it means that something terribly wrong happened to the NAND media; |
|
435 |
//-- in normal circumstances it can not happen. One of the reasons: totally worn out media. |
|
436 |
const TBool platSecEnabled = PlatSec::ConfigSetting(PlatSec::EPlatSecEnforcement); |
|
437 |
__PRINT3(_L("CFatTable::DoFreedClustersNotify() DeleteNotify failure! drv:%d err:%d, PlatSec:%d"),iOwner->DriveNumber(), r, platSecEnabled); |
|
438 |
||
439 |
if(platSecEnabled) |
|
440 |
{ |
|
441 |
//-- if PlatSec is enabled, we can't afford jeopardize the security; without DeleteNotify() |
|
442 |
//-- it's possible to pick up data from deleted files, so, panic the file server. |
|
443 |
Fault(EFatBadLocalDrive); |
|
444 |
} |
|
445 |
else |
|
446 |
{ |
|
447 |
//-- if PlatSec is disabled, it's OK to ignore the NAND fault in release mode. |
|
448 |
__ASSERT_DEBUG(0, Fault(EFatBadLocalDrive)); |
|
449 |
} |
|
450 |
} |
|
451 |
||
452 |
||
453 |
deleteLen = 0; |
|
454 |
} |
|
455 |
||
456 |
} |
|
457 |
||
458 |
//-- empty the array. |
|
459 |
aFreedClusters.Reset(); |
|
460 |
} |
|
461 |
||
462 |
//----------------------------------------------------------------------------- |
|
463 |
/** |
|
464 |
Mark a chain of clusters as free in the FAT. |
|
465 |
||
466 |
@param aCluster Start cluster of cluster chain to free |
|
467 |
@leave System wide error codes |
|
468 |
*/ |
|
469 |
void CFatTable::FreeClusterListL(TUint32 aCluster) |
|
470 |
{ |
|
471 |
__PRINT1(_L("CFatTable::FreeClusterListL startCluster=%d"),aCluster); |
|
472 |
if (aCluster == KSpareCluster) |
|
473 |
return; |
|
474 |
||
475 |
//-- here we can store array of freed cluster numbers in order to |
|
476 |
//-- notify media drive about the media addresses marked as "invalid" |
|
477 |
RClusterArray deletedClusters; |
|
478 |
CleanupClosePushL(deletedClusters); |
|
479 |
||
480 |
//-- if ETrue, we need to notify media driver about invalidated media addressses |
|
481 |
const TBool bFreeClustersNotify = iMediaAtt & KMediaAttDeleteNotify; |
|
482 |
||
483 |
//-- this is a maximal number of FAT entries in the deletedClusters array. |
|
484 |
//-- as soon as we collect this number of entries in the array, FAT cache will be flushed |
|
485 |
//-- and driver notified. The array will be emptied. Used to avoid huge array when deleting |
|
486 |
//-- large files on NAND media |
|
487 |
const TUint KSubListLen = 4096; |
|
488 |
ASSERT(IsPowerOf2(KSubListLen)); |
|
489 |
||
490 |
TUint32 lastKnownFreeCluster = FreeClusterHint(); |
|
491 |
TUint32 cntFreedClusters = 0; |
|
492 |
||
493 |
TUint32 currCluster = aCluster; |
|
494 |
TInt nextCluster = aCluster; |
|
495 |
||
496 |
for(;;) |
|
497 |
{ |
|
498 |
const TBool bEOF = !GetNextClusterL(nextCluster); |
|
499 |
WriteL(currCluster, KSpareCluster); |
|
500 |
||
501 |
lastKnownFreeCluster = Min(currCluster, lastKnownFreeCluster); |
|
502 |
||
503 |
// Keep a record of the deleted clusters so that we can subsequently notify the media driver. This is only safe |
|
504 |
// to do once the FAT changes have been written to disk. |
|
505 |
if(bFreeClustersNotify) |
|
506 |
deletedClusters.Append(currCluster); |
|
507 |
||
508 |
++cntFreedClusters; |
|
509 |
currCluster = nextCluster; |
|
510 |
||
511 |
if (bEOF || aCluster == KSpareCluster) |
|
512 |
break; |
|
513 |
||
514 |
if(bFreeClustersNotify && cntFreedClusters && (cntFreedClusters & (KSubListLen-1))==0) |
|
515 |
{//-- reached a limit of the entries in the array. Flush FAT cache, notify the driver and empty the array. |
|
516 |
IncrementFreeClusterCount(cntFreedClusters); |
|
517 |
cntFreedClusters = 0; |
|
518 |
||
519 |
SetFreeClusterHint(lastKnownFreeCluster); |
|
520 |
DoFreedClustersNotify(deletedClusters); |
|
521 |
} |
|
522 |
||
523 |
} |
|
524 |
||
525 |
//-- increase the number of free clusters and notify the driver if required. |
|
526 |
IncrementFreeClusterCount(cntFreedClusters); |
|
527 |
SetFreeClusterHint(lastKnownFreeCluster); |
|
528 |
||
529 |
if(bFreeClustersNotify) |
|
530 |
DoFreedClustersNotify(deletedClusters); |
|
531 |
||
532 |
CleanupStack::PopAndDestroy(&deletedClusters); |
|
533 |
} |
|
534 |
||
535 |
//----------------------------------------------------------------------------- |
|
536 |
||
537 |
/** |
|
538 |
Find a free cluster nearest to aCluster, Always checks to the right of aCluster first |
|
539 |
but checks in both directions in the Fat. |
|
540 |
||
541 |
@param aCluster Cluster to find nearest free cluster to. |
|
542 |
@leave KErrDiskFull + system wide error codes |
|
543 |
@return cluster number found |
|
544 |
*/ |
|
545 |
TUint32 CFatTable::FindClosestFreeClusterL(TUint32 aCluster) |
|
546 |
{ |
|
547 |
__PRINT2(_L("CFatTable::FindClosestFreeClusterL() drv:%d cl:%d"),iOwner->DriveNumber(),aCluster); |
|
548 |
||
549 |
if(!ClusterNumberValid(aCluster)) |
|
550 |
{ |
|
551 |
ASSERT(0); |
|
552 |
User::Leave(KErrCorrupt); |
|
553 |
} |
|
554 |
||
555 |
if(!RequestFreeClusters(1)) |
|
556 |
{//-- there is no at least 1 free cluster available |
|
557 |
__PRINT(_L("CFatTable::FindClosestFreeClusterL() leaving KErrDiskFull #1")); |
|
558 |
User::Leave(KErrDiskFull); |
|
559 |
} |
|
560 |
||
561 |
//-- 1. look if the given index contains a free entry |
|
562 |
if(ReadL(aCluster) != KSpareCluster) |
|
563 |
{//-- no, it doesn't... |
|
564 |
||
565 |
//-- 2. look in both directions starting from the aCluster, looking in the right direction first |
|
566 |
||
567 |
const TUint32 maxEntries = MaxEntries(); |
|
568 |
const TUint32 MinIdx = KFatFirstSearchCluster; |
|
569 |
const TUint32 MaxIdx = maxEntries-1; |
|
570 |
||
571 |
TBool canGoRight = ETrue; |
|
572 |
TBool canGoLeft = ETrue; |
|
573 |
||
574 |
TUint32 rightIdx = aCluster; |
|
575 |
TUint32 leftIdx = aCluster; |
|
576 |
||
577 |
for(TUint i=0; i<maxEntries; ++i) |
|
578 |
{ |
|
579 |
if(canGoRight) |
|
580 |
{ |
|
581 |
if(rightIdx < MaxIdx) |
|
582 |
++rightIdx; |
|
583 |
else |
|
584 |
canGoRight = EFalse; |
|
585 |
} |
|
586 |
||
587 |
if(canGoLeft) |
|
588 |
{ |
|
589 |
if(leftIdx > MinIdx) |
|
590 |
--leftIdx; |
|
591 |
else |
|
592 |
canGoLeft = EFalse; |
|
593 |
} |
|
594 |
||
595 |
if(!canGoRight && !canGoLeft) |
|
596 |
{ |
|
597 |
__PRINT(_L("CFatTable::FindClosestFreeClusterL() leaving KErrDiskFull #2")); |
|
598 |
User::Leave(KErrDiskFull); |
|
599 |
} |
|
600 |
||
601 |
if(canGoRight && ReadL(rightIdx) == KSpareCluster) |
|
602 |
{ |
|
603 |
aCluster = rightIdx; |
|
604 |
break; |
|
605 |
} |
|
606 |
||
607 |
if (canGoLeft && ReadL(leftIdx) == KSpareCluster) |
|
608 |
{ |
|
609 |
aCluster = leftIdx; |
|
610 |
break; |
|
611 |
} |
|
612 |
}//for(..) |
|
613 |
||
614 |
}//if(ReadL(aCluster) != KSpareCluster) |
|
615 |
||
616 |
||
617 |
//-- note: do not update free cluster hint here by calling SetFreeClusterHint(). This is going to be |
|
618 |
//-- expensive especially if overridden methods with synchronisation are called. Instead, set the number of |
|
619 |
//-- the last known free cluster in the caller of this internal method. |
|
620 |
||
621 |
//__PRINT1(_L("CFatTable::FindClosestFreeClusterL found:%d"),aCluster); |
|
622 |
||
623 |
return aCluster; |
|
624 |
} |
|
625 |
||
626 |
//----------------------------------------------------------------------------- |
|
627 |
||
628 |
/** |
|
629 |
Converts a cluster number to byte offset in the FAT |
|
630 |
||
631 |
@param aFatIndex Cluster number |
|
632 |
@return Number of bytes from the beginning of the FAT |
|
633 |
*/ |
|
634 |
TUint32 CFatTable::PosInBytes(TUint32 aFatIndex) const |
|
635 |
{ |
|
636 |
switch(FatType()) |
|
637 |
{ |
|
638 |
case EFat12: |
|
639 |
return (((aFatIndex>>1)<<1) + (aFatIndex>>1)); //-- 1.5 bytes per FAT entry |
|
640 |
||
641 |
case EFat16: |
|
642 |
return aFatIndex<<1; //-- 2 bytes per FAT entry |
|
643 |
||
644 |
case EFat32: |
|
645 |
return aFatIndex<<2; //-- 4 bytes per FAT entry |
|
646 |
||
647 |
default: |
|
648 |
ASSERT(0); |
|
649 |
return 0;//-- get rid of warning |
|
650 |
}; |
|
651 |
||
652 |
} |
|
653 |
||
654 |
//----------------------------------------------------------------------------- |
|
655 |
||
656 |
/** |
|
657 |
Checks if we have at least aClustersRequired clusters free in the FAT. |
|
658 |
This is, actually a dummy implementation. |
|
659 |
||
660 |
@param aClustersRequired number of free clusters required |
|
661 |
@return ETrue if there is at least aClustersRequired free clusters available, EFalse otherwise. |
|
662 |
*/ |
|
663 |
TBool CFatTable::RequestFreeClusters(TUint32 aClustersRequired) const |
|
664 |
{ |
|
665 |
//__PRINT1(_L("#- CFatTable::RequestFreeClusters(%d)"),aClustersRequired); |
|
666 |
ASSERT(aClustersRequired >0); |
|
667 |
return (NumberOfFreeClusters() >= aClustersRequired); |
|
668 |
} |
|
669 |
||
670 |
//----------------------------------------------------------------------------- |
|
671 |
/** |
|
672 |
@return ETrue if the cluster number aClusterNo is valid, i.e. belongs to the FAT table |
|
673 |
*/ |
|
674 |
TBool CFatTable::ClusterNumberValid(TUint32 aClusterNo) const |
|
675 |
{ |
|
676 |
return (aClusterNo >= KFatFirstSearchCluster) && (aClusterNo < iMaxEntries); |
|
677 |
} |
|
678 |
||
679 |
||
680 |
||
681 |
//####################################################################################################################################### |
|
682 |
//# CAtaFatTable class implementation |
|
683 |
//####################################################################################################################################### |
|
684 |
||
685 |
/** |
|
686 |
Constructor |
|
687 |
*/ |
|
688 |
CAtaFatTable::CAtaFatTable(CFatMountCB& aOwner) |
|
689 |
:CFatTable(aOwner), iDriveInteface(aOwner.DriveInterface()) |
|
690 |
{ |
|
691 |
iState = ENotInitialised; |
|
692 |
} |
|
693 |
||
694 |
||
695 |
CAtaFatTable::~CAtaFatTable() |
|
696 |
{ |
|
697 |
DestroyHelperThread(); |
|
698 |
} |
|
699 |
||
700 |
||
701 |
/** factory method */ |
|
702 |
CAtaFatTable* CAtaFatTable::NewL(CFatMountCB& aOwner) |
|
703 |
{ |
|
704 |
__PRINT1(_L("CAtaFatTable::NewL() drv:%d"),aOwner.DriveNumber()); |
|
705 |
CAtaFatTable* pSelf = new (ELeave) CAtaFatTable(aOwner); |
|
706 |
||
707 |
CleanupStack::PushL(pSelf); |
|
708 |
pSelf->InitializeL(); |
|
709 |
CleanupStack::Pop(); |
|
710 |
||
711 |
return pSelf; |
|
712 |
} |
|
713 |
||
714 |
||
715 |
//--------------------------------------------------------------------------------------------------------------------------------------- |
|
716 |
||
717 |
/** |
|
718 |
CAtaFatTable's FAT cache factory method. |
|
719 |
Creates fixed cache for FAT12/FAT16 or LRU cache for FAT32 |
|
720 |
*/ |
|
721 |
void CAtaFatTable::CreateCacheL() |
|
722 |
{ |
|
723 |
ASSERT(iOwner); |
|
724 |
const TUint32 fatSize=iOwner->FatSizeInBytes(); |
|
725 |
__PRINT3(_L("CAtaFatTable::CreateCacheL drv:%d, FAT:%d, FAT Size:%d"), iOwner->DriveNumber(), FatType(), fatSize); |
|
726 |
||
727 |
||
728 |
//-- according to FAT specs: |
|
729 |
//-- FAT12 max size is 4084 entries or 6126 bytes => create fixed cache for whole FAT |
|
730 |
//-- FAT16 min size is 4085 entries or 8170 bytes, max size is 65525 entries or 131048 bytes => create fixed cache for whole FAT |
|
731 |
//-- FAT32 min size is 65526 entries or 262104 bytes => create LRU paged cache of max size: KFat32LRUCacheSize |
|
732 |
||
733 |
ASSERT(!iCache); |
|
734 |
||
735 |
//-- this is used for chaches granularity sanity check |
|
736 |
const TUint32 KMinGranularityLog2 = KDefSectorSzLog2; //-- 512 bytes is a minimal allowed granularity |
|
737 |
const TUint32 KMaxGranularityLog2 = 18; //-- 256K is a maximal allowed granularity |
|
738 |
||
739 |
switch(FatType()) |
|
740 |
{ |
|
741 |
//-- create fixed FAT12 cache |
|
742 |
case EFat12: |
|
743 |
iCache = CFat12Cache::NewL(iOwner, fatSize); |
|
744 |
break; |
|
745 |
||
746 |
//-- create fixed FAT16 cache |
|
747 |
case EFat16: |
|
748 |
{ |
|
749 |
TUint32 fat16_ReadGranularity_Log2; //-- FAT16 cache read granularity Log2 |
|
750 |
TUint32 fat16_WriteGranularity_Log2;//-- FAT16 cache write granularity Log2 |
|
751 |
||
752 |
iOwner->FatConfig().Fat16FixedCacheParams(fat16_ReadGranularity_Log2, fat16_WriteGranularity_Log2); |
|
753 |
||
754 |
//-- check if granularity values look sensible |
|
755 |
const TBool bParamsValid = fat16_ReadGranularity_Log2 >= KMinGranularityLog2 && fat16_ReadGranularity_Log2 <= KMaxGranularityLog2 && |
|
756 |
fat16_WriteGranularity_Log2 >= KMinGranularityLog2 && fat16_WriteGranularity_Log2 <= KMaxGranularityLog2; |
|
757 |
||
758 |
__ASSERT_ALWAYS(bParamsValid, Fault(EFatCache_BadGranularity)); |
|
759 |
||
760 |
||
761 |
iCache = CFat16FixedCache::NewL(iOwner, fatSize, fat16_ReadGranularity_Log2, fat16_WriteGranularity_Log2); |
|
762 |
} |
|
763 |
break; |
|
764 |
||
765 |
//-- create FAT32 LRU paged cache |
|
766 |
case EFat32: |
|
767 |
{ |
|
768 |
TUint32 fat32_LRUCache_MaxMemSize; //-- Maximum memory for the LRU FAT32 cache |
|
769 |
TUint32 fat32_ReadGranularity_Log2; //-- FAT32 cache read granularity Log2 |
|
770 |
TUint32 fat32_WriteGranularity_Log2;//-- FAT32 cache write granularity Log2 |
|
771 |
||
772 |
iOwner->FatConfig().Fat32LruCacheParams(fat32_ReadGranularity_Log2, fat32_WriteGranularity_Log2, fat32_LRUCache_MaxMemSize); |
|
773 |
||
774 |
||
775 |
//-- check if granularity and required cache size values look sensible |
|
776 |
const TBool bParamsValid = fat32_ReadGranularity_Log2 >= KMinGranularityLog2 && fat32_ReadGranularity_Log2 <= KMaxGranularityLog2 && |
|
777 |
fat32_WriteGranularity_Log2 >= KMinGranularityLog2 && fat32_WriteGranularity_Log2 <= KMaxGranularityLog2 && |
|
778 |
fat32_LRUCache_MaxMemSize >= 8*K1KiloByte && fat32_LRUCache_MaxMemSize < 4*K1MegaByte; |
|
779 |
||
780 |
__ASSERT_ALWAYS(bParamsValid, Fault(EFatCache_BadGranularity)); |
|
781 |
||
782 |
iCache = CFat32LruCache::NewL(iOwner, fat32_LRUCache_MaxMemSize, fat32_ReadGranularity_Log2, fat32_WriteGranularity_Log2); |
|
783 |
} |
|
784 |
break; |
|
785 |
||
786 |
default: |
|
787 |
ASSERT(0); |
|
788 |
User::Leave(KErrCorrupt); |
|
789 |
break; |
|
790 |
}; |
|
791 |
||
792 |
ASSERT(iCache); |
|
793 |
} |
|
794 |
||
795 |
//--------------------------------------------------------------------------------------------------------------------------------------- |
|
796 |
||
797 |
/** |
|
798 |
Destroys a helper thread object. |
|
799 |
If the thread is running, stops it first. than deletes the ipHelperThread and sets it to NULL |
|
800 |
*/ |
|
801 |
void CAtaFatTable::DestroyHelperThread() |
|
802 |
{ |
|
803 |
||
804 |
if(!ipHelperThread) |
|
805 |
return; |
|
806 |
||
807 |
__PRINT1(_L("CAtaFatTable::DestroyHelperThread(), drv:%d"), iOwner->DriveNumber()); |
|
808 |
ipHelperThread->ForceStop(); |
|
809 |
delete ipHelperThread; |
|
810 |
ipHelperThread = NULL; |
|
811 |
} |
|
812 |
||
813 |
//--------------------------------------------------------------------------------------------------------------------------------------- |
|
814 |
||
815 |
/** |
|
816 |
Flush the FAT cache on disk |
|
817 |
@leave System wide error codes |
|
818 |
*/ |
|
819 |
void CAtaFatTable::FlushL() |
|
820 |
{ |
|
821 |
__PRINT1(_L("CAtaFatTable::FlushL(), drv:%d"), iOwner->DriveNumber()); |
|
822 |
||
823 |
//-- the data can't be written if the mount is inconsistent |
|
824 |
iOwner->CheckStateConsistentL(); |
|
825 |
||
826 |
if (iCache) |
|
827 |
iCache->FlushL(); |
|
828 |
} |
|
829 |
||
830 |
||
831 |
//--------------------------------------------------------------------------------------------------------------------------------------- |
|
832 |
||
833 |
/** |
|
834 |
Dismount the cache. Stops any activity, deallocates caches etc. |
|
835 |
@param aDiscardDirtyData if ETrue, non-flushed data in the cache will be discarded. |
|
836 |
*/ |
|
837 |
void CAtaFatTable::Dismount(TBool aDiscardDirtyData) |
|
838 |
{ |
|
839 |
__PRINT3(_L("#=-= CAtaFatTable::Dismount(%d), drv:%d, state:%d"), aDiscardDirtyData, iOwner->DriveNumber(), State()); |
|
840 |
||
841 |
//-- if there is a helper thread, stop it and delete its object |
|
842 |
DestroyHelperThread(); |
|
843 |
||
844 |
//-- if there is the cache, close it (it may lead to deallocating its memory) |
|
845 |
if(iCache) |
|
846 |
{ |
|
847 |
//-- cache's Close() can check if the cache is clean. |
|
848 |
//-- ignore dirty data in cache if the mount is not in consistent state (it's impossible to flush cache data) |
|
849 |
//-- or if we are asked to do so. |
|
850 |
const TBool bIgnoreDirtyData = aDiscardDirtyData || !iOwner->ConsistentState(); |
|
851 |
iCache->Close(bIgnoreDirtyData); |
|
852 |
||
853 |
delete iCache; |
|
854 |
iCache=NULL; |
|
855 |
} |
|
856 |
||
857 |
SetState(EDismounted); |
|
858 |
} |
|
859 |
||
860 |
//--------------------------------------------------------------------------------------------------------------------------------------- |
|
861 |
||
862 |
/** |
|
863 |
Invalidate whole FAT cache. |
|
864 |
Depending of cache type this may just mark cache invalid with reading on demand or re-read whole cache from the media |
|
865 |
*/ |
|
866 |
void CAtaFatTable::InvalidateCacheL() |
|
867 |
{ |
|
868 |
__PRINT1(_L("CAtaFatTable::InvalidateCache(), drv:%d"), iOwner->DriveNumber()); |
|
869 |
||
870 |
//-- if we have a cache, invalidate it entirely |
|
871 |
if(iCache) |
|
872 |
{ |
|
873 |
User::LeaveIfError(iCache->Invalidate()); |
|
874 |
} |
|
875 |
||
876 |
//-- invalidating whole FAT cache means that something very serious happened. |
|
877 |
//-- if we have a helper thread running, abort it. |
|
878 |
if(ipHelperThread) |
|
879 |
ipHelperThread->ForceStop(); |
|
880 |
||
881 |
} |
|
882 |
||
883 |
||
884 |
//--------------------------------------------------------------------------------------------------------------------------------------- |
|
885 |
||
886 |
/** |
|
887 |
Invalidate specified region of the FAT cache |
|
888 |
Depending of cache type this may just mark part of the cache invalid with reading on demand later |
|
889 |
or re-read whole cache from the media. |
|
890 |
||
891 |
@param aPos absolute media position where the region being invalidated starts. |
|
892 |
@param aLength length in bytes of region to invalidate / refresh |
|
893 |
*/ |
|
894 |
void CAtaFatTable::InvalidateCacheL(TInt64 aPos, TUint32 aLength) |
|
895 |
{ |
|
896 |
__PRINT3(_L("CAtaFatTable::InvalidateCacheL() drv:%d, pos:%LU, len:%u,"), iOwner->DriveNumber(), aPos, aLength); |
|
897 |
||
898 |
if(I64HIGH(aPos) || !aLength || I64HIGH(aPos+aLength)) |
|
899 |
return; //-- FAT tables can't span over 4G |
|
900 |
||
901 |
const TUint32 mediaPos32 = I64LOW(aPos); |
|
902 |
||
903 |
//-- we do not use other copies of FAT, so trach changes only in FAT1 |
|
904 |
const TUint32 fat1StartPos = iOwner->StartOfFatInBytes(); |
|
905 |
const TUint32 fat1EndPos = fat1StartPos + iOwner->FatSizeInBytes(); |
|
906 |
||
907 |
TUint32 invRegionPosStart = 0; //-- media pos where the invalidated region starts |
|
908 |
TUint32 invRegionLen = 0; //-- size of the invalidated region, bytes |
|
909 |
||
910 |
//-- calculate the FAT1 region being invalidated |
|
911 |
if(mediaPos32 < fat1StartPos) |
|
912 |
{ |
|
913 |
if((mediaPos32 + aLength) <= fat1StartPos) |
|
914 |
return; |
|
915 |
||
916 |
invRegionPosStart = fat1StartPos; |
|
917 |
invRegionLen = aLength - (fat1StartPos-mediaPos32); |
|
918 |
} |
|
919 |
else //if(mediaPos32 < fat1StartPos) |
|
920 |
{//-- mediaPos32 >= fat1StartPos) |
|
921 |
if(mediaPos32 >= fat1EndPos) |
|
922 |
return; |
|
923 |
||
924 |
invRegionPosStart = mediaPos32; |
|
925 |
||
926 |
if((mediaPos32 + aLength) <= fat1EndPos) |
|
927 |
{ |
|
928 |
invRegionLen = aLength; |
|
929 |
} |
|
930 |
else |
|
931 |
{ |
|
932 |
invRegionLen = mediaPos32+aLength-fat1EndPos; |
|
933 |
} |
|
934 |
} |
|
935 |
||
936 |
//-- convert the media pos of the region into FAT entries basis, depending on the FAT type |
|
937 |
ASSERT(invRegionPosStart >= fat1StartPos && invRegionLen <= (TUint)iOwner->FatSizeInBytes()); |
|
938 |
||
939 |
TUint32 startFatEntry=0; |
|
940 |
TUint32 numEntries = 0; |
|
941 |
||
942 |
switch(FatType()) |
|
943 |
{ |
|
944 |
case EFat12: |
|
945 |
//-- invalidate whole cache; it is not worth making calculations for such small memory region. |
|
946 |
User::LeaveIfError(iCache->Invalidate()); |
|
947 |
return; |
|
948 |
||
949 |
case EFat16: |
|
950 |
startFatEntry = (invRegionPosStart-fat1StartPos) >> KFat16EntrySzLog2; |
|
951 |
numEntries = (invRegionLen + (sizeof(TFat16Entry)-1)) >> KFat16EntrySzLog2; |
|
952 |
break; |
|
953 |
||
954 |
case EFat32: |
|
955 |
startFatEntry = (invRegionPosStart-fat1StartPos) >> KFat32EntrySzLog2; |
|
956 |
numEntries = (invRegionLen + (sizeof(TFat32Entry)-1)) >> KFat32EntrySzLog2; |
|
957 |
break; |
|
958 |
||
959 |
default: |
|
960 |
ASSERT(0); |
|
961 |
return; |
|
962 |
}; |
|
963 |
||
964 |
if(startFatEntry < KFatFirstSearchCluster) |
|
965 |
{//-- FAT[0] and FAT[1] can't be legally accessed, they are reserved entries. We need to adjust region being refreshed. |
|
966 |
if(numEntries <= KFatFirstSearchCluster) |
|
967 |
return; //-- nothing to refresh |
|
968 |
||
969 |
startFatEntry += KFatFirstSearchCluster; |
|
970 |
numEntries -= KFatFirstSearchCluster; |
|
971 |
} |
|
972 |
||
973 |
User::LeaveIfError(iCache->InvalidateRegion(startFatEntry, numEntries)); |
|
974 |
} |
|
975 |
||
976 |
||
977 |
//----------------------------------------------------------------------------- |
|
978 |
/** |
|
979 |
Initialize the object, create FAT cache if required |
|
980 |
@leave KErrNoMemory |
|
981 |
*/ |
|
982 |
void CAtaFatTable::InitializeL() |
|
983 |
{ |
|
984 |
__PRINT2(_L("CAtaFatTable::InitializeL() drv:%d, state%d"), iOwner->DriveNumber(), State()); |
|
985 |
CFatTable::InitializeL(); |
|
986 |
||
987 |
ASSERT(!iCache); |
|
988 |
ASSERT(State() == ENotInitialised); |
|
989 |
||
990 |
//-- create the FAT cache. |
|
991 |
CreateCacheL(); |
|
992 |
||
993 |
SetState(EInitialised); |
|
994 |
||
995 |
} |
|
996 |
||
997 |
//----------------------------------------------------------------------------- |
|
998 |
/** |
|
999 |
Mount the FAT table to the CFatMountCB. Depending on mount parameters and configuration this method |
|
1000 |
can do various things, like counting free clusters synchronously if data from FSInfo isn't valid, |
|
1001 |
or setting up a FAT backround thread and return immediately etc. |
|
1002 |
||
1003 |
@param aMountParam mounting parameters, like some data from FSInfo |
|
1004 |
||
1005 |
*/ |
|
1006 |
void CAtaFatTable::MountL(const TMountParams& aMountParam) |
|
1007 |
{ |
|
1008 |
__PRINT2(_L("CAtaFatTable::MountL() drv:%d, state:%d"), iOwner->DriveNumber(), State()); |
|
1009 |
||
1010 |
ASSERT(State() == EInitialised); |
|
1011 |
SetState(EMounting); |
|
1012 |
||
1013 |
if(ipHelperThread) |
|
1014 |
{ |
|
1015 |
__PRINT(_L("CAtaFatTable::MountL() Helper thread is present!")); |
|
1016 |
ASSERT(0); |
|
1017 |
DestroyHelperThread(); |
|
1018 |
} |
|
1019 |
||
1020 |
||
1021 |
//-- Check if we have valid data from FSInfo. In this case we don't need to count free clusters |
|
1022 |
if(aMountParam.iFsInfoValid) |
|
1023 |
{ |
|
1024 |
ASSERT(IsFat32()); |
|
1025 |
ASSERT(aMountParam.iFreeClusters <= MaxEntries()); |
|
1026 |
||
1027 |
ASSERT(ClusterNumberValid(aMountParam.iFirstFreeCluster)); |
|
1028 |
||
1029 |
SetFreeClusters(aMountParam.iFreeClusters); |
|
1030 |
SetFreeClusterHint(aMountParam.iFirstFreeCluster); |
|
1031 |
||
1032 |
__PRINT2(_L("CAtaFatTable::MountL() Using data from FSInfo sector. free clusters:%d, 1st free:%d"), FreeClusters(), FreeClusterHint()); |
|
1033 |
||
1034 |
//-- We don't need to scan entire FAT to find out the number of free entries, because the data are taken from FSInfo. |
|
1035 |
//-- But if we are going to use the FAT32 bit supercache, we need to populate it. So, try to start up a special |
|
1036 |
//-- populating thread. |
|
1037 |
CFatBitCache *pFatBitCache = iCache->BitCacheInterface(); |
|
1038 |
if(pFatBitCache) |
|
1039 |
{//-- bit cache is present, we need to populate (or repopulate it) |
|
1040 |
//-- create helper thread object and start the thread |
|
1041 |
ipHelperThread = CFat32BitCachePopulator::NewL(*this); |
|
1042 |
||
1043 |
ipHelperThread->Launch(); |
|
1044 |
//-- background FAT bit cache populating thread is running now. |
|
1045 |
//-- the result of thread start up and completion isn't very interesting: If it fails to |
|
1046 |
//-- properly populate the cache, nothing fatal will happen. |
|
1047 |
} |
|
1048 |
||
1049 |
//-- CFat32BitCachePopulator doesn't affect FAT table state. |
|
1050 |
SetState(EMounted); |
|
1051 |
return; |
|
1052 |
} |
|
1053 |
||
1054 |
//-- FSInfo data are invalid; we need to count free clusters by reading whole FAT table |
|
1055 |
//-- This method can optionally create a background thread (that will count free clusters) and return immideately. |
|
1056 |
CountFreeClustersL(); |
|
1057 |
} |
|
1058 |
||
1059 |
//----------------------------------------------------------------------------- |
|
1060 |
||
1061 |
/** |
|
1062 |
Decrements the free cluster count. This is an overridden method with synchronisation. |
|
1063 |
@param aCount a number of clusters |
|
1064 |
*/ |
|
1065 |
void CAtaFatTable::DecrementFreeClusterCount(TUint32 aCount) |
|
1066 |
{ |
|
1067 |
XAutoLock lock(iOwner); //-- enter critical section |
|
1068 |
CFatTable::DecrementFreeClusterCount(aCount); |
|
1069 |
} |
|
1070 |
||
1071 |
/** |
|
1072 |
Increments the free cluster count. This is an overridden method with synchronisation. |
|
1073 |
@param aCount a number of clusters |
|
1074 |
*/ |
|
1075 |
void CAtaFatTable::IncrementFreeClusterCount(TUint32 aCount) |
|
1076 |
{ |
|
1077 |
XAutoLock lock(iOwner); //-- enter critical section |
|
1078 |
CFatTable::IncrementFreeClusterCount(aCount); |
|
1079 |
} |
|
1080 |
||
1081 |
//----------------------------------------------------------------------------- |
|
1082 |
||
1083 |
/** |
|
1084 |
Obtain number of free clusters on the volume. This is an overridden method. |
|
1085 |
Depending on the "aSyncOperation" parameter this operation can be fully synhronous (exact number of free clusters ) or asynchronous |
|
1086 |
(current number of free clusters) if the FAT scanning thread is still running. |
|
1087 |
||
1088 |
@param aSyncOperation if ETrue, this method will wait until FAT scan thread finishes and return exact number of free clusters |
|
1089 |
if false, it will return current number of free clusters counted by FAT scan thread if it hasn't finished yet. |
|
1090 |
||
1091 |
@return Number of free clusters. See also CAtaFatTable::RequestFreeClusters() |
|
1092 |
*/ |
|
1093 |
TUint32 CAtaFatTable::NumberOfFreeClusters(TBool aSyncOperation/*=EFalse*/) const |
|
1094 |
{ |
|
1095 |
if(ipHelperThread && ipHelperThread->ThreadWorking() && ipHelperThread->Type() == CFatHelperThreadBase::EFreeSpaceScanner) |
|
1096 |
{//-- here we have running helper thread that counts free entries in FAT. |
|
1097 |
//-- if this operation is synchronous, we need to wait until it finish its job in order to get _exact_ number of free cluster, |
|
1098 |
//-- not currently counted |
|
1099 |
||
1100 |
//__PRINT2(_L("#- CAtaFatTable::NumberOfFreeClusters(), drv:%d enter, sync:%d"), iOwner->DriveNumber(), aSyncOperation); |
|
1101 |
||
1102 |
if(aSyncOperation) |
|
1103 |
{//-- wait for background scanning thread to finish counting free clusters if this operation is synchronous |
|
1104 |
ipHelperThread->BoostPriority(ETrue); |
|
1105 |
ipHelperThread->WaitToFinish(); |
|
1106 |
} |
|
1107 |
||
1108 |
XAutoLock lock(iOwner); //-- enter critical section |
|
1109 |
||
1110 |
const TUint32 freeClusters = FreeClusters(); |
|
1111 |
//__PRINT2(_L("#- CAtaFatTable::NumberOfFreeClusters(), drv:%d Exit, clusters:%d"), iOwner->DriveNumber(), freeClusters); |
|
1112 |
return freeClusters; |
|
1113 |
} |
|
1114 |
||
1115 |
return FreeClusters(); |
|
1116 |
||
1117 |
} |
|
1118 |
||
1119 |
//----------------------------------------------------------------------------- |
|
1120 |
||
1121 |
/** |
|
1122 |
Set free cluster count. This is an overridden method with synchronisation. |
|
1123 |
@param aFreeClusters new value of free clusters |
|
1124 |
*/ |
|
1125 |
void CAtaFatTable::SetFreeClusters(TUint32 aFreeClusters) |
|
1126 |
{ |
|
1127 |
XAutoLock lock(iOwner); //-- enter critical section |
|
1128 |
CFatTable::SetFreeClusters(aFreeClusters); |
|
1129 |
} |
|
1130 |
||
1131 |
/** |
|
1132 |
This is an overridden method with synchronisation. |
|
1133 |
@return the last known free cluster number. |
|
1134 |
*/ |
|
1135 |
TUint32 CAtaFatTable::FreeClusterHint() const |
|
1136 |
{ |
|
1137 |
XAutoLock lock(iOwner); //-- enter critical section |
|
1138 |
return CFatTable::FreeClusterHint(); |
|
1139 |
} |
|
1140 |
||
1141 |
/** Set next free cluster number. This is an overridden method with synchronisation. */ |
|
1142 |
void CAtaFatTable::SetFreeClusterHint(TUint32 aCluster) |
|
1143 |
{ |
|
1144 |
XAutoLock lock(iOwner); //-- enter critical section |
|
1145 |
CFatTable::SetFreeClusterHint(aCluster); |
|
1146 |
} |
|
1147 |
||
1148 |
/** |
|
1149 |
@return ETrue if the state of the object is consistent; i.e. it is |
|
1150 |
fully constructed, valid and the amount of free entries is known. |
|
1151 |
Used in the case of asynchronous mounting. |
|
1152 |
*/ |
|
1153 |
TBool CAtaFatTable::ConsistentState() const |
|
1154 |
{ |
|
1155 |
return State() == EMounted; |
|
1156 |
} |
|
1157 |
||
1158 |
//----------------------------------------------------------------------------- |
|
1159 |
||
1160 |
/** |
|
1161 |
Request for the raw write access to the FAT area (all copies of FAT). |
|
1162 |
If FAT helper thread is running, waits until it finishes. |
|
1163 |
||
1164 |
@param aPos absolute media position we are going to write to. Be careful with casting it from TInt64 and losing high word. |
|
1165 |
@param aLen length of the area being written |
|
1166 |
*/ |
|
1167 |
void CAtaFatTable::RequestRawWriteAccess(TInt64 aPos, TUint32 aLen) const |
|
1168 |
{ |
|
1169 |
if(I64HIGH(aPos)) |
|
1170 |
return; |
|
1171 |
||
1172 |
const TUint32 pos32 = I64LOW(aPos); |
|
1173 |
const TUint32 posFatStart = iOwner->StartOfFatInBytes(); //-- position of the FAT start on the volume |
|
1174 |
const TUint32 posFatsEnd = posFatStart + iOwner->NumberOfFats()*iOwner->FatSizeInBytes(); //-- position of the ent of ALL FATs |
|
1175 |
||
1176 |
if(pos32 >= posFatsEnd || (pos32+aLen) <= posFatStart) |
|
1177 |
return; |
|
1178 |
||
1179 |
__PRINT2(_L("#=- CAtaFatTable::RequestRawWriteAccess() pos:%d, len:%d"),pos32, aLen); |
|
1180 |
||
1181 |
//-- someone tries to write to FAT area directly. Wait for the FAT helper thread to finish |
|
1182 |
if(ipHelperThread) |
|
1183 |
ipHelperThread->WaitToFinish(); |
|
1184 |
||
1185 |
} |
|
1186 |
||
1187 |
//----------------------------------------------------------------------------- |
|
1188 |
||
1189 |
/** |
|
1190 |
Checks if we have at least "aClustersRequired" clusters free in the FAT. |
|
1191 |
If FAT scannng thread is running, waits until requested number of free clusters counted or the thread finishes. |
|
1192 |
||
1193 |
@param aClustersRequired number of free clusters required |
|
1194 |
@return ETrue if there is at least aClustersRequired free clusters available, EFalse otherwise. |
|
1195 |
*/ |
|
1196 |
TBool CAtaFatTable::RequestFreeClusters(TUint32 aClustersRequired) const |
|
1197 |
{ |
|
1198 |
//__PRINT1(_L("#- CAtaFatTable::RequestFreeClusters(%d)"),aClustersRequired); |
|
1199 |
ASSERT(aClustersRequired >0); |
|
1200 |
||
1201 |
if(!ipHelperThread || !ipHelperThread->ThreadWorking() || ipHelperThread->Type() != CFatHelperThreadBase::EFreeSpaceScanner) |
|
1202 |
{//-- there is no FAT free space scan thread running, number of free entries can't increase in background |
|
1203 |
return (FreeClusters() >= aClustersRequired); //-- use simple, non-thread safe method |
|
1204 |
} |
|
1205 |
||
1206 |
//-- FAT free space scan thread is running, counting free FAT entries. wait until it has counted enough or finish. |
|
1207 |
ASSERT(ipHelperThread->Type() == CFatHelperThreadBase::EFreeSpaceScanner); |
|
1208 |
||
1209 |
TUint32 currFreeClusters; |
|
1210 |
const TUint KWaitGranularity = 20*K1mSec; //-- wait granularity |
|
1211 |
||
1212 |
ipHelperThread->BoostPriority(ETrue); //-- increase thread priority |
|
1213 |
||
1214 |
for(;;) |
|
1215 |
{ |
|
1216 |
currFreeClusters = NumberOfFreeClusters(EFalse); //-- get _current_ number of free clusters asynchronously |
|
1217 |
if(currFreeClusters >= aClustersRequired) |
|
1218 |
break; //-- OK, the request is satisfied |
|
1219 |
||
1220 |
if(!ipHelperThread->ThreadWorking()) |
|
1221 |
{//-- the thread has finished its work |
|
1222 |
currFreeClusters = NumberOfFreeClusters(EFalse); //-- get _current_ number of free clusters asynchronously |
|
1223 |
break; |
|
1224 |
} |
|
1225 |
||
1226 |
User::After(KWaitGranularity); //-- wait some time allowing FAT scanning thread to count free clusters. |
|
1227 |
} |
|
1228 |
||
1229 |
ipHelperThread->BoostPriority(EFalse); //-- set thread priority back to normal |
|
1230 |
//__PRINT1(_L("#- CAtaFatTable::RequestFreeClusters() #2 curr:%d"),currFreeClusters); |
|
1231 |
||
1232 |
return (currFreeClusters >= aClustersRequired); |
|
1233 |
||
1234 |
} |
|
1235 |
||
1236 |
//----------------------------------------------------------------------------- |
|
1237 |
||
1238 |
/** |
|
1239 |
Parse a buffer filled with FAT16 or FAT32 entries, counting free clusters and looking for the firs free cluster number. |
|
1240 |
Note that this method can be called from a helper FAT scan thread. |
|
1241 |
||
1242 |
@param aBuf FAT buffer descriptor. Must contain whole number of FAT16 or FAT32 entries |
|
1243 |
@param aScanParam the structure to be filled with values, like number of counted free and non-free clusters, etc. |
|
1244 |
*/ |
|
1245 |
void CAtaFatTable::DoParseFatBuf(const TPtrC8& aBuf, TFatScanParam& aScanParam) const |
|
1246 |
{ |
|
1247 |
||
1248 |
if(IsFat16()) |
|
1249 |
{//-- we are processing a buffer of FAT16 entries |
|
1250 |
ASSERT(!ipHelperThread); |
|
1251 |
ASSERT((aBuf.Size() & (sizeof(TFat16Entry)-1)) == 0); |
|
1252 |
const TInt KNumEntries = aBuf.Size() >> KFat16EntrySzLog2; |
|
1253 |
const TFat16Entry* const pFatEntry = (const TFat16Entry*)(aBuf.Ptr()); |
|
1254 |
||
1255 |
for(TInt i=0; i<KNumEntries; ++i) |
|
1256 |
{ |
|
1257 |
if(aScanParam.iEntriesScanned >= KFatFirstSearchCluster) |
|
1258 |
{ |
|
1259 |
const TFat16Entry entry = pFatEntry[i]; |
|
1260 |
||
1261 |
if(entry == KSpareCluster) |
|
1262 |
{//-- found a free FAT entry |
|
1263 |
aScanParam.iCurrFreeEntries++; |
|
1264 |
||
1265 |
if(aScanParam.iFirstFree < KFatFirstSearchCluster) |
|
1266 |
aScanParam.iFirstFree = aScanParam.iEntriesScanned; |
|
1267 |
||
1268 |
} |
|
1269 |
else |
|
1270 |
{//-- found occupied FAT entry, count bad clusters as well |
|
1271 |
aScanParam.iCurrOccupiedEntries++; |
|
1272 |
} |
|
1273 |
||
1274 |
} |
|
1275 |
||
1276 |
aScanParam.iEntriesScanned++; |
|
1277 |
} |
|
1278 |
}//if(IsFat16()) |
|
1279 |
else |
|
1280 |
if(IsFat32()) |
|
1281 |
{//-- we are processing a buffer of FAT32 entries. |
|
1282 |
//-- note that here we can be in the context of the FAT free entries scan thread. |
|
1283 |
ASSERT((aBuf.Size() & (sizeof(TFat32Entry)-1)) == 0); |
|
1284 |
||
1285 |
//-- pointer to the FAT32 bit supercache. If present, we will populate it here |
|
1286 |
CFatBitCache *pFatBitCache = iCache->BitCacheInterface(); |
|
1287 |
||
1288 |
const TInt KNumEntries = aBuf.Size() >> KFat32EntrySzLog2; |
|
1289 |
const TFat32Entry* const pFatEntry = (const TFat32Entry*)(aBuf.Ptr()); |
|
1290 |
||
1291 |
for(TInt i=0; i<KNumEntries; ++i) |
|
1292 |
{ |
|
1293 |
if(aScanParam.iEntriesScanned >= KFatFirstSearchCluster) |
|
1294 |
{ |
|
1295 |
const TFat32Entry entry = pFatEntry[i] & KFat32EntryMask; |
|
1296 |
||
1297 |
if(entry == KSpareCluster) |
|
1298 |
{//-- found a free FAT32 entry |
|
1299 |
++aScanParam.iCurrFreeEntries; |
|
1300 |
||
1301 |
if(aScanParam.iFirstFree < KFatFirstSearchCluster) |
|
1302 |
aScanParam.iFirstFree = aScanParam.iEntriesScanned; |
|
1303 |
||
1304 |
||
1305 |
//-- feed the information about free FAT entry at index aClustersScanned to the FAT bit supercache. |
|
1306 |
if(pFatBitCache) |
|
1307 |
{ |
|
1308 |
pFatBitCache->SetFreeFatEntry(aScanParam.iEntriesScanned); |
|
1309 |
} |
|
1310 |
||
1311 |
||
1312 |
}//if(entry == KSpareCluster) |
|
1313 |
else |
|
1314 |
{//-- found occupied FAT32 entry, count bad clusters as well |
|
1315 |
aScanParam.iCurrOccupiedEntries++; |
|
1316 |
} |
|
1317 |
} |
|
1318 |
||
1319 |
++aScanParam.iEntriesScanned; |
|
1320 |
} |
|
1321 |
||
1322 |
}//if(IsFat32()) |
|
1323 |
else |
|
1324 |
{ |
|
1325 |
ASSERT(0); |
|
1326 |
} |
|
1327 |
} |
|
1328 |
||
1329 |
//----------------------------------------------------------------------------- |
|
1330 |
||
1331 |
/** |
|
1332 |
Count free clusters in FAT16 or FAT32. Uses relatively large buffer to read FAT entries into; |
|
1333 |
This is faster than usual ReadL() calls. |
|
1334 |
*/ |
|
1335 |
void CAtaFatTable::DoCountFreeClustersL() |
|
1336 |
{ |
|
1337 |
__PRINT2(_L("#- CAtaFatTable::DoCountFreeClustersL() drv:%d, state:%d"), iOwner->DriveNumber(), State()); |
|
1338 |
||
1339 |
if(!IsFat16() && !IsFat32()) |
|
1340 |
{ |
|
1341 |
ASSERT(0); |
|
1342 |
User::Leave(KErrNotSupported); |
|
1343 |
} |
|
1344 |
||
1345 |
const TUint32 KFat1StartPos = iOwner->StartOfFatInBytes(); |
|
1346 |
const TUint32 KNumClusters = MaxEntries(); //-- FAT[0] & FAT[1] are reserved and not counted by UsableClusters() |
|
1347 |
const TUint32 KNumFATs = iOwner->NumberOfFats(); |
|
1348 |
const TUint32 KFatSize = KNumClusters * (IsFat32() ? sizeof(TFat32Entry) : sizeof(TFat16Entry)); //-- usable size of one FAT. |
|
1349 |
||
1350 |
(void)KNumFATs; |
|
1351 |
||
1352 |
ASSERT(KFat1StartPos >= 1*KDefaultSectorSize); |
|
1353 |
ASSERT(KNumClusters > KFatFirstSearchCluster); |
|
1354 |
ASSERT(KNumFATs > 0); |
|
1355 |
||
1356 |
const TUint32 KFatBufSz = 32*K1KiloByte; //-- buffer size for FAT reading. 32K seems to be optimal size |
|
1357 |
||
1358 |
__ASSERT_COMPILE((KFatBufSz % sizeof(TFat32Entry)) == 0); |
|
1359 |
__ASSERT_COMPILE((KFatBufSz % sizeof(TFat16Entry)) == 0); |
|
1360 |
||
1361 |
RBuf8 buf; |
|
1362 |
CleanupClosePushL(buf); |
|
1363 |
||
1364 |
//-- allocate memory for FAT parse buffer |
|
1365 |
buf.CreateMaxL(KFatBufSz); |
|
1366 |
||
1367 |
//-- read FAT into the large buffer and parse it |
|
1368 |
TUint32 rem = KFatSize; |
|
1369 |
TUint32 mediaPos = KFat1StartPos; |
|
1370 |
||
1371 |
//-- prepare FAT bit supercache to being populated. |
|
1372 |
//-- actual populating will happen in ::DoParseFatBuf() |
|
1373 |
CFatBitCache *pFatBitCache = iCache->BitCacheInterface(); |
|
1374 |
||
1375 |
if(pFatBitCache) |
|
1376 |
{ |
|
1377 |
pFatBitCache->StartPopulating(); |
|
1378 |
} |
|
1379 |
||
1380 |
TFatScanParam fatScanParam; |
|
1381 |
||
1382 |
//-- used for measuring time |
|
1383 |
TTime timeStart; |
|
1384 |
TTime timeEnd; |
|
1385 |
timeStart.UniversalTime(); //-- take start time |
|
1386 |
||
1387 |
||
1388 |
while(rem) |
|
1389 |
{ |
|
1390 |
const TUint32 bytesToRead=Min(rem, KFatBufSz); |
|
1391 |
TPtrC8 ptrData(buf.Ptr(), bytesToRead); |
|
1392 |
||
1393 |
//__PRINT2(_L("#=--- CAtaFatTable::DoCountFreeClustersL() read %d bytes pos:0x%x"), bytesToRead, (TUint32)mediaPos); |
|
1394 |
User::LeaveIfError(iOwner->LocalDrive()->Read(mediaPos, bytesToRead, buf)); |
|
1395 |
||
1396 |
DoParseFatBuf(ptrData, fatScanParam); |
|
1397 |
||
1398 |
mediaPos += bytesToRead; |
|
1399 |
rem -= bytesToRead; |
|
1400 |
} |
|
1401 |
||
1402 |
//-- here fatScanParam contains values for the whole FAT. |
|
1403 |
||
1404 |
timeEnd.UniversalTime(); //-- take end time |
|
1405 |
const TInt msScanTime = (TInt)( (timeEnd.MicroSecondsFrom(timeStart)).Int64() / K1mSec); |
|
1406 |
(void)msScanTime; |
|
1407 |
__PRINT1(_L("#- CAtaFatTable::DoCountFreeClustersL() finished. Taken:%d ms "), msScanTime); |
|
1408 |
||
1409 |
||
1410 |
//-- tell FAT bit cache that we have finished populating it |
|
1411 |
if(pFatBitCache) |
|
1412 |
{ |
|
1413 |
pFatBitCache->FinishPopulating(ETrue); |
|
1414 |
pFatBitCache->Dump(); |
|
1415 |
} |
|
1416 |
||
1417 |
if(!fatScanParam.iFirstFree)//-- haven't found free clusters on the volume |
|
1418 |
fatScanParam.iFirstFree = KFatFirstSearchCluster; |
|
1419 |
||
1420 |
ASSERT(fatScanParam.iCurrFreeEntries <= iOwner->UsableClusters()); |
|
1421 |
ASSERT(ClusterNumberValid(fatScanParam.iFirstFree)); |
|
1422 |
||
1423 |
SetFreeClusters(fatScanParam.iCurrFreeEntries); |
|
1424 |
SetFreeClusterHint(fatScanParam.iFirstFree); |
|
1425 |
||
1426 |
CleanupStack::PopAndDestroy(&buf); |
|
1427 |
} |
|
1428 |
||
1429 |
//----------------------------------------------------------------------------- |
|
1430 |
||
1431 |
/** |
|
1432 |
Count free clusters on the volume. |
|
1433 |
Depending on FAT type can count clusters synchronously or start a thread to do it in background. |
|
1434 |
*/ |
|
1435 |
void CAtaFatTable::CountFreeClustersL() |
|
1436 |
{ |
|
1437 |
__PRINT3(_L("#=- CAtaFatTable::CountFreeClustersL() drv:%d, FAT%d, state:%d"),iOwner->DriveNumber(),FatType(), State()); |
|
1438 |
||
1439 |
ASSERT(State() == EMounting); |
|
1440 |
ASSERT(!ipHelperThread); |
|
1441 |
||
1442 |
TInt nRes; |
|
1443 |
||
1444 |
switch(FatType()) |
|
1445 |
{ |
|
1446 |
case EFat12: //-- use old default scanning, it is synchronous |
|
1447 |
CFatTable::CountFreeClustersL(); |
|
1448 |
SetState(EMounted); |
|
1449 |
break; |
|
1450 |
||
1451 |
case EFat16: //-- enhanced FAT scan, but still synchronous |
|
1452 |
TRAP(nRes, DoCountFreeClustersL()); |
|
1453 |
if(nRes !=KErrNone) |
|
1454 |
{ |
|
1455 |
CFatTable::CountFreeClustersL(); //-- fall back to the legacy method |
|
1456 |
} |
|
1457 |
||
1458 |
SetState(EMounted); |
|
1459 |
break; |
|
1460 |
||
1461 |
case EFat32: //-- This is FAT32, try to set up a FAT scanning thread if allowed |
|
1462 |
{ |
|
1463 |
TBool bFat32BkGndScan = ETrue; //-- if true, we will try to start up a background scanning thread. |
|
1464 |
||
1465 |
//-- 1. check if background FAT scanning is disabled in config |
|
1466 |
if(!iOwner->FatConfig().FAT32_AsynchMount()) |
|
1467 |
{ |
|
1468 |
__PRINT(_L("#=- FAT32 BkGnd scan is disabled in config.")); |
|
1469 |
bFat32BkGndScan = EFalse; |
|
1470 |
} |
|
1471 |
||
1472 |
//-- 2. check if background FAT scanning is disabled by test interface |
|
1473 |
#ifdef _DEBUG |
|
1474 |
TInt nMntDebugFlags; |
|
1475 |
if(bFat32BkGndScan && RProperty::Get(KSID_Test1, iOwner->DriveNumber(), nMntDebugFlags) == KErrNone) |
|
1476 |
{//-- test property for this drive is defined |
|
1477 |
if(nMntDebugFlags & KMntDisable_FatBkGndScan) |
|
1478 |
{ |
|
1479 |
__PRINT(_L("#- FAT32 BkGnd scan is disabled is disabled by debug interface.")); |
|
1480 |
bFat32BkGndScan = EFalse; |
|
1481 |
} |
|
1482 |
||
1483 |
} |
|
1484 |
#endif |
|
1485 |
//-- 3. try to start FAT32 free entries scanning thread. |
|
1486 |
if(bFat32BkGndScan) |
|
1487 |
{ |
|
1488 |
__PRINT(_L("#=- Starting up FAT32 free entries scanner thread...")); |
|
1489 |
TRAP(nRes, DoLaunchFat32FreeSpaceScanThreadL()); |
|
1490 |
if(nRes == KErrNone) |
|
1491 |
break; //-- let thread run by itself |
|
1492 |
||
1493 |
//-- DoLaunchFat32FreeSpaceScanThreadL() has set this object state. |
|
1494 |
} |
|
1495 |
||
1496 |
//-- we either failed to launch the thread or this feature was disabled somehow. Fall back to the synchronous scan. |
|
1497 |
TRAP(nRes, DoCountFreeClustersL()); |
|
1498 |
if(nRes !=KErrNone) |
|
1499 |
{ |
|
1500 |
CFatTable::CountFreeClustersL(); //-- fall back to the legacy method |
|
1501 |
} |
|
1502 |
||
1503 |
SetState(EMounted); |
|
1504 |
}//case EFat32 |
|
1505 |
break; |
|
1506 |
||
1507 |
default: |
|
1508 |
ASSERT(0); |
|
1509 |
break; |
|
1510 |
||
1511 |
} //switch(FatType()) |
|
1512 |
} |
|
1513 |
||
1514 |
//----------------------------------------------------------------------------- |
|
1515 |
||
1516 |
/** |
|
1517 |
Set up and start FAT scan thread. |
|
1518 |
Leaves on error. |
|
1519 |
*/ |
|
1520 |
void CAtaFatTable::DoLaunchFat32FreeSpaceScanThreadL() |
|
1521 |
{ |
|
1522 |
__PRINT2(_L("#=- CAtaFatTable::DoLaunchFat32FreeSpaceScanThreadL() drv:%d, state:%d"),iOwner->DriveNumber(), State()); |
|
1523 |
ASSERT(State() == EMounting); |
|
1524 |
||
1525 |
//-- 1. check if something is already working (shan't be!) |
|
1526 |
if(ipHelperThread) |
|
1527 |
{ |
|
1528 |
if(ipHelperThread->ThreadWorking()) |
|
1529 |
{ |
|
1530 |
__PRINT(_L("#=- CAtaFatTable::DoLaunchScanThread() some thread is already running ?")); |
|
1531 |
ASSERT(0); |
|
1532 |
User::Leave(KErrAlreadyExists); |
|
1533 |
} |
|
1534 |
||
1535 |
DestroyHelperThread(); |
|
1536 |
} |
|
1537 |
||
1538 |
//-- 2. create helper thread object and start the thread |
|
1539 |
ipHelperThread = CFat32FreeSpaceScanner::NewL(*this); |
|
1540 |
||
1541 |
SetState(EFreeClustersScan); |
|
1542 |
||
1543 |
ipHelperThread->Launch(); |
|
1544 |
//-- background FAT scanning thread is running now |
|
1545 |
} |
|
1546 |
||
1547 |
//----------------------------------------------------------------------------- |
|
1548 |
/** |
|
1549 |
Read an entry from the FAT table |
|
1550 |
||
1551 |
@param aFatIndex FAT entry number to read |
|
1552 |
@return FAT entry value |
|
1553 |
*/ |
|
1554 |
TUint32 CAtaFatTable::ReadL(TUint32 aFatIndex) const |
|
1555 |
{ |
|
1556 |
if(!ClusterNumberValid(aFatIndex)) |
|
1557 |
{ |
|
1558 |
//ASSERT(0); //-- deliberately corrupted (by some tests) DOS directory entries can have 0 in the "first cluster" field. |
|
1559 |
__PRINT1(_L("CAtaFatTable::ReadL(%d) bad Index!"), aFatIndex); |
|
1560 |
User::Leave(KErrCorrupt); |
|
1561 |
} |
|
1562 |
||
1563 |
||
1564 |
const TUint entry = iCache->ReadEntryL(aFatIndex); |
|
1565 |
return entry; |
|
1566 |
} |
|
1567 |
||
1568 |
||
1569 |
//----------------------------------------------------------------------------- |
|
1570 |
/** |
|
1571 |
Write an entry to the FAT table |
|
1572 |
||
1573 |
@param aFatIndex aFatIndex FAT entry number to write |
|
1574 |
@param aValue FAT entry to write |
|
1575 |
@leave |
|
1576 |
*/ |
|
1577 |
void CAtaFatTable::WriteL(TUint32 aFatIndex, TUint32 aValue) |
|
1578 |
{ |
|
1579 |
||
1580 |
__PRINT2(_L("CAtaFatTable::WriteL() entry:%d, val:0x%x"), aFatIndex, aValue); |
|
1581 |
||
1582 |
if(!ClusterNumberValid(aFatIndex)) |
|
1583 |
{ |
|
1584 |
ASSERT(0); |
|
1585 |
User::Leave(KErrCorrupt); |
|
1586 |
} |
|
1587 |
||
1588 |
if(aValue != KSpareCluster && (aValue < KFatFirstSearchCluster || aValue > KFat32EntryMask)) |
|
1589 |
{ |
|
1590 |
ASSERT(0); |
|
1591 |
User::Leave(KErrCorrupt); |
|
1592 |
} |
|
1593 |
||
1594 |
//-- wait until we are allowed to write FAT entry |
|
1595 |
if(ipHelperThread && ipHelperThread->ThreadWorking()) |
|
1596 |
{ |
|
1597 |
ASSERT(ipHelperThread->ThreadId() != RThread().Id()); //-- this method must not be called the FAT helper thread |
|
1598 |
ipHelperThread->RequestFatEntryWriteAccess(aFatIndex); |
|
1599 |
} |
|
1600 |
||
1601 |
//-- write entry to the FAT through FAT cache |
|
1602 |
iCache->WriteEntryL(aFatIndex, aValue); |
|
1603 |
||
1604 |
||
1605 |
//-- if we are writing "spare" FAT entry, tell FAT bit supercache about it. |
|
1606 |
//-- it will store the information that corresponding FAT cache sector has a spare FAT entry. |
|
1607 |
//-- writing non-spare FAT entry doesn't mean anything: that FAT cache sector might or might not contain free entries. |
|
1608 |
if(aValue == KSpareCluster && iCache->BitCacheInterface()) |
|
1609 |
{ |
|
1610 |
CFatBitCache *pFatBitCache = iCache->BitCacheInterface(); |
|
1611 |
const CFatBitCache::TState cacheState= pFatBitCache->State(); |
|
1612 |
if(cacheState == CFatBitCache::EPopulated || cacheState == CFatBitCache::EPopulating) |
|
1613 |
{//-- bit cache is either normally populated or being populated by one of the helper threads |
|
1614 |
if(ipHelperThread && ipHelperThread->ThreadWorking()) |
|
1615 |
{ |
|
1616 |
//-- here we have a multithreading issue. Helper FAT thread can be parsing FAT and optionally calling ReportFreeFatEntry(..) as well. |
|
1617 |
//-- in this case we need either to suspend the helper thread in order to prevent corruption of the FAT bit cache data, |
|
1618 |
//-- or ignore this call and rely on the fact that the FAT bit supercache is a kind of self-learning and the missing data will be |
|
1619 |
//-- fixed during conflict resolution (this can lead to performance degradation). |
|
1620 |
||
1621 |
//-- ok, suspend the helper thread while we are changing data in the bit cache |
|
1622 |
AcquireLock(); |
|
1623 |
ipHelperThread->Suspend(); |
|
1624 |
pFatBitCache->SetFreeFatEntry(aFatIndex); |
|
1625 |
ipHelperThread->Resume(); |
|
1626 |
ReleaseLock(); |
|
1627 |
||
1628 |
} |
|
1629 |
else |
|
1630 |
{//-- no one else is accessing FAT in this time |
|
1631 |
ASSERT(pFatBitCache->UsableState()); |
|
1632 |
pFatBitCache->SetFreeFatEntry(aFatIndex); |
|
1633 |
} |
|
1634 |
} |
|
1635 |
||
1636 |
}//if(aValue == KSpareCluster) |
|
1637 |
||
1638 |
} |
|
1639 |
||
1640 |
//----------------------------------------------------------------------------- |
|
1641 |
/** |
|
1642 |
This is an overridden method from CFatTable. See CFatTable::FindClosestFreeClusterL(...) |
|
1643 |
Does the same, i.e looks for the closest to "aCluster" free FAT entry, but more advanced, |
|
1644 |
it can use FAT bit supercache for quick lookup. |
|
1645 |
||
1646 |
@param aCluster Cluster to find nearest free cluster to. |
|
1647 |
@leave KErrDiskFull + system wide error codes |
|
1648 |
@return cluster number found |
|
1649 |
*/ |
|
1650 |
TUint32 CAtaFatTable::FindClosestFreeClusterL(TUint32 aCluster) |
|
1651 |
{ |
|
1652 |
__PRINT2(_L("CAtaFatTable::FindClosestFreeClusterL() drv:%d cl:%d"),iOwner->DriveNumber(),aCluster); |
|
1653 |
||
1654 |
if(!ClusterNumberValid(aCluster)) |
|
1655 |
{ |
|
1656 |
ASSERT(0); |
|
1657 |
User::Leave(KErrCorrupt); |
|
1658 |
} |
|
1659 |
||
1660 |
||
1661 |
if(!RequestFreeClusters(1)) |
|
1662 |
{//-- there is no at least 1 free cluster available |
|
1663 |
__PRINT(_L("CAtaFatTable::FindClosestFreeClusterL() leaving KErrDiskFull #1")); |
|
1664 |
User::Leave(KErrDiskFull); |
|
1665 |
} |
|
1666 |
||
1667 |
//-- check if we have FAT bit supercache and it is in consistent state |
|
1668 |
CFatBitCache *pFatBitCache = iCache->BitCacheInterface(); |
|
1669 |
if(!pFatBitCache) |
|
1670 |
return CFatTable::FindClosestFreeClusterL(aCluster); //-- fall back to the old search method |
|
1671 |
||
1672 |
ASSERT(IsFat32()); |
|
1673 |
||
1674 |
if(!pFatBitCache->UsableState()) |
|
1675 |
{ |
|
1676 |
//__PRINT(_L("#++ CAtaFatTable::FindClosestFreeClusterL() FAT bit cache isn't consistent!")); |
|
1677 |
return CFatTable::FindClosestFreeClusterL(aCluster); //-- fall back to the old search method |
|
1678 |
} |
|
1679 |
||
1680 |
//-- ask FAT bit supercache to find us FAT cache sector (closest to the aCluster) that contains free FAT entries. |
|
1681 |
//__PRINT2(_L("#++ CAtaFatTable::FindClosestFreeClusterL(%d) hint free cl:%d"), aCluster, FreeClusterHint()); |
|
1682 |
||
1683 |
const TInt KMaxLookupRetries = 2; |
|
1684 |
for(TInt i=0; i<KMaxLookupRetries; ++i) |
|
1685 |
{ |
|
1686 |
const TInt nRes = pFatBitCache->FindClosestFreeFatEntry(aCluster); |
|
1687 |
switch(nRes) |
|
1688 |
{ |
|
1689 |
case KErrNone: |
|
1690 |
//-- FAT bit supercache has found a free FAT entry in the FAT32 cache |
|
1691 |
//__PRINT1(_L("#++ CAtaFatTable::FindClosestFreeClusterL FOUND! cl:%d"), aCluster); |
|
1692 |
||
1693 |
ASSERT(ClusterNumberValid(aCluster)); |
|
1694 |
||
1695 |
//-- do not update the last known free cluster, it can be quite expensive. |
|
1696 |
//-- do it in the caller method with bigger granularity. |
|
1697 |
return aCluster; |
|
1698 |
||
1699 |
case KErrNotFound: |
|
1700 |
//-- there was a bit cache conflict, when FAT cache sector is marked as having free FAT entries, but it doesn't have them in reality. |
|
1701 |
//-- It can happen because FAT bit cache entry is marked '1' only on populating the bit vector or if someone writes KSpareCluster into the |
|
1702 |
//-- corresponding FAT cache sector. Such conflict can happen quite often. |
|
1703 |
//-- Try search again, the search is very likely to succeed very close, because the FAT bit cache entry had already been fixed as the result of conflict resolution. |
|
1704 |
break; |
|
1705 |
||
1706 |
case KErrCorrupt: |
|
1707 |
//-- pFatBitCache->FindClosestFreeFatEntry failed to read a page from the media |
|
1708 |
//-- break out from the loop and fall back to old search just in case. |
|
1709 |
||
1710 |
case KErrEof: |
|
1711 |
//-- there are no '1' entries in whole FAT bit cache vector at all, which is quite unlikely |
|
1712 |
//-- break out from the loop and fall back to old search. |
|
1713 |
i=KMaxLookupRetries; |
|
1714 |
break; |
|
1715 |
||
1716 |
//-- unexpected result code. |
|
1717 |
default: |
|
1718 |
ASSERT(0); |
|
1719 |
i=KMaxLookupRetries; |
|
1720 |
break; |
|
1721 |
||
1722 |
||
1723 |
};//switch(nRes) |
|
1724 |
||
1725 |
}//for(TInt i=0; i<KMaxLookupRetries; ++i) |
|
1726 |
||
1727 |
//-- something went wrong, Bit Fat supercache could not find FAT cache sector that contains at least one free FAT entry. |
|
1728 |
//-- this is most likely because of the FAT data mismatch between FAT and bit cache. |
|
1729 |
__PRINT(_L("#++ CAtaFatTable::FindClosestFreeClusterL FALLBACK #1")); |
|
1730 |
||
1731 |
//!!!!?? use not aCluster, but previous search result here ??? |
|
1732 |
return CFatTable::FindClosestFreeClusterL(aCluster); //-- fall back to the old search method |
|
1733 |
} |
|
1734 |
||
1735 |
||
1736 |
||
1737 |
/** |
|
1738 |
Get the next cluster in the chain from the FAT |
|
1739 |
||
1740 |
@param aCluster number to read, contains next cluster upon return |
|
1741 |
@return False if end of cluster chain |
|
1742 |
*/ |
|
1743 |
TBool CFatTable::GetNextClusterL(TInt& aCluster) const |
|
1744 |
{ |
|
1745 |
__PRINT1(_L("CAtaFatTable::GetNextClusterL(%d)"), aCluster); |
|
1746 |
||
33
0173bcd7697c
Revision: 201001
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
1747 |
const TUint32 nextCluster = ReadL(aCluster); |
0173bcd7697c
Revision: 201001
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
1748 |
const TBool bEOC = IsEndOfClusterCh(nextCluster); |
0173bcd7697c
Revision: 201001
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
1749 |
|
0173bcd7697c
Revision: 201001
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
1750 |
if(bEOC) |
0173bcd7697c
Revision: 201001
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
1751 |
return EFalse; //-- the end of cluster chain |
0173bcd7697c
Revision: 201001
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
1752 |
|
0173bcd7697c
Revision: 201001
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
1753 |
aCluster = nextCluster; |
0 | 1754 |
|
33
0173bcd7697c
Revision: 201001
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
1755 |
return ETrue; |
0 | 1756 |
} |
1757 |
||
1758 |
/** |
|
1759 |
Write EOF to aFatIndex |
|
1760 |
@param aFatIndex index in FAT (cluster number) to be written |
|
1761 |
*/ |
|
1762 |
void CFatTable::WriteFatEntryEofL(TUint32 aFatIndex) |
|
1763 |
{ |
|
36
538db54a451d
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
33
diff
changeset
|
1764 |
__PRINT1(_L("CFatTable::WriteFatEntryEofL(%d)"), aFatIndex); |
0 | 1765 |
|
1766 |
//-- use EOF_32Bit (0x0fffffff) for all types of FAT, FAT cache will mask it appropriately |
|
1767 |
WriteL(aFatIndex, EOF_32Bit); |
|
1768 |
} |
|
1769 |
||
1770 |
||
1771 |
||
1772 |
/** |
|
1773 |
Mark cluster number aFatIndex in FAT as bad |
|
1774 |
@param aFatIndex index in FAT (cluster number) to be written |
|
1775 |
*/ |
|
1776 |
void CFatTable::MarkAsBadClusterL(TUint32 aFatIndex) |
|
1777 |
{ |
|
1778 |
__PRINT1(_L("CAtaFatTable::MarkAsBadClusterL(%d)"),aFatIndex); |
|
1779 |
||
1780 |
//-- use KBad_32Bit (0x0ffffff7) for all types of FAT, FAT cache will mask it appropriately |
|
1781 |
WriteL(aFatIndex, KBad_32Bit); |
|
1782 |
||
1783 |
FlushL(); |
|
1784 |
} |
|
1785 |
||
1786 |
||
1787 |
/** |
|
1788 |
Return the location of a Cluster in the data section of the media |
|
1789 |
||
1790 |
@param aCluster to find location of |
|
1791 |
@return Byte offset of the cluster data |
|
1792 |
*/ |
|
1793 |
TInt64 CAtaFatTable::DataPositionInBytes(TUint32 aCluster) const |
|
1794 |
{ |
|
1795 |
||
1796 |
__ASSERT_DEBUG(ClusterNumberValid(aCluster), Fault(EFatTable_InvalidIndex)); |
|
1797 |
||
1798 |
const TInt clusterBasePosition=iOwner->ClusterBasePosition(); |
|
1799 |
return(((TInt64(aCluster)-KFatFirstSearchCluster) << iOwner->ClusterSizeLog2()) + clusterBasePosition); |
|
1800 |
} |
|
1801 |
||
1802 |
||
1803 |
||
1804 |
||
1805 |
//####################################################################################################################################### |
|
1806 |
//# CFatHelperThreadBase implementation |
|
1807 |
//####################################################################################################################################### |
|
1808 |
||
1809 |
//----------------------------------------------------------------------------- |
|
1810 |
CFatHelperThreadBase::CFatHelperThreadBase(CAtaFatTable& aOwner) |
|
1811 |
:iOwner(aOwner) |
|
1812 |
{ |
|
1813 |
||
1814 |
SetState(EInvalid); |
|
1815 |
} |
|
1816 |
||
1817 |
CFatHelperThreadBase::~CFatHelperThreadBase() |
|
1818 |
{ |
|
1819 |
Close(); |
|
1820 |
} |
|
1821 |
||
1822 |
//----------------------------------------------------------------------------- |
|
1823 |
/** |
|
1824 |
Closes the thread object handle. |
|
1825 |
The thread shall not be running. |
|
1826 |
*/ |
|
1827 |
void CFatHelperThreadBase::Close() |
|
1828 |
{ |
|
1829 |
if(ThreadWorking()) |
|
1830 |
{ |
|
1831 |
ASSERT(0); |
|
1832 |
ForceStop(); |
|
1833 |
} |
|
1834 |
||
1835 |
iThread.Close(); |
|
1836 |
} |
|
1837 |
||
1838 |
//----------------------------------------------------------------------------- |
|
1839 |
/** |
|
1840 |
Waits for the thread to finish (thread function exit). if it is running. |
|
1841 |
@return thread completion code. |
|
1842 |
||
1843 |
!!!! definitely need a timeout processing here to avoid any possibitlity of hanging forever !! |
|
1844 |
||
1845 |
*/ |
|
1846 |
TInt CFatHelperThreadBase::WaitToFinish() const |
|
1847 |
{ |
|
1848 |
if(!ThreadWorking()) |
|
1849 |
return ThreadCompletionCode(); |
|
1850 |
||
1851 |
||
1852 |
//--todo: use timeout and assert to avoid hanging forever ? |
|
1853 |
__PRINT1(_L("#= CFatHelperThreadBase::WaitToFinish(), stat:%d"),iThreadStatus.Int()); |
|
1854 |
User::WaitForRequest(iThreadStatus); |
|
1855 |
return iThreadStatus.Int(); |
|
1856 |
} |
|
1857 |
||
1858 |
//----------------------------------------------------------------------------- |
|
1859 |
||
1860 |
/** |
|
1861 |
Requests the fat helper thread function to finish gracefully ASAP; then closes the thread handle. |
|
1862 |
Just sets a flag that is analysed by the thread function and waits thread's request completion. |
|
1863 |
*/ |
|
1864 |
void CFatHelperThreadBase::ForceStop() |
|
1865 |
{ |
|
1866 |
if(ThreadWorking()) |
|
1867 |
{ |
|
1868 |
DBG_STATEMENT(TName name = iThread.Name();) |
|
1869 |
__PRINT3(_L("#=!! CFatHelperThreadBase::ForceStop() id:%u, name:%S, status:%d"), (TUint)iThread.Id(), &name, ThreadCompletionCode()); |
|
1870 |
DBG_STATEMENT(name.Zero()); //-- to avoid warning |
|
1871 |
||
1872 |
iOwner.AcquireLock(); |
|
1873 |
||
1874 |
AllowToLive(EFalse) ; //-- signal the thread to exit ASAP |
|
1875 |
||
1876 |
iOwner.ReleaseLock(); |
|
1877 |
||
1878 |
WaitToFinish(); //-- wait for the thread to finish. |
|
1879 |
||
1880 |
//-- don't know why but we need a delay, at least on the emulator. Otherwise thread object doesn't look destroyed. |
|
1881 |
//-- probably something with scheduling. |
|
1882 |
User::After(10*K1mSec); |
|
1883 |
} |
|
1884 |
||
1885 |
iThread.Close(); |
|
1886 |
} |
|
1887 |
||
1888 |
||
1889 |
//----------------------------------------------------------------------------- |
|
1890 |
||
1891 |
||
1892 |
/** |
|
1893 |
Created, initialises and starts the helper thread. |
|
1894 |
||
1895 |
@param aFunction pointer to the thread function |
|
1896 |
@param aThreadParameter parameter to be passed to the thread function. Its interpretation depends on the thread function. |
|
1897 |
@return KErrNone on success; standard error code otherwise |
|
1898 |
*/ |
|
1899 |
TInt CFatHelperThreadBase::DoLaunchThread(TThreadFunction aFunction, TAny* aThreadParameter) |
|
1900 |
{ |
|
1901 |
__PRINT2(_L("#=- CFatHelperThreadBase::DoLaunchThread() thread stat:%d, state:%d"), ThreadCompletionCode(), State()); |
|
1902 |
||
1903 |
ASSERT(aFunction); |
|
1904 |
ASSERT(State() != EWorking); |
|
1905 |
||
1906 |
if(ThreadWorking()) |
|
1907 |
{ |
|
1908 |
ASSERT(0); |
|
1909 |
return KErrInUse; |
|
1910 |
} |
|
1911 |
||
1912 |
if(iOwner.OwnerMount()->Drive().IsSynchronous()) |
|
1913 |
{ |
|
1914 |
//-- if the drive is synchronous, this is a main File Server thread. Don't play with it, it has its own scheduler |
|
1915 |
//-- and completing other requests rather than native CFsRequest leads to the stray events, because it waits on the |
|
1916 |
//-- User::WaitForAnyRequest and doesn't check which request has completed. |
|
1917 |
__PRINT(_L("CFatHelperThreadBase::DoLaunchThread() the drive is synchronous, skipping.")); |
|
1918 |
return KErrNotSupported; |
|
1919 |
} |
|
1920 |
||
1921 |
||
1922 |
TInt nRes; |
|
1923 |
TName nameBuf; //-- this will be initial thread name, it will rename itself in its thread function |
|
1924 |
nameBuf.Format(_L("Fat32HelperThread_drv_%d"), iOwner.OwnerMount()->DriveNumber()); |
|
1925 |
const TInt stackSz = 4*K1KiloByte; //-- thread stack size, 4K |
|
1926 |
||
1927 |
iThread.Close(); |
|
1928 |
||
1929 |
//-- 1. create the thread |
|
1930 |
nRes = iThread.Create(nameBuf, aFunction, stackSz, &User::Allocator(), aThreadParameter, EOwnerProcess); |
|
1931 |
if(nRes != KErrNone) |
|
1932 |
{ |
|
1933 |
__PRINT1(_L("#=- CFatHelperThreadBase::DoLaunchThread() failure#1 res:%d"), nRes); |
|
1934 |
iThread.Close(); |
|
1935 |
ASSERT(0); |
|
1936 |
return nRes; |
|
1937 |
} |
|
1938 |
||
1939 |
//-- 2. set up its working environment |
|
1940 |
AllowToLive(ETrue); |
|
1941 |
iThread.SetPriority((TThreadPriority)EHelperPriorityNormal); //-- initially the thread has very low priority |
|
1942 |
||
1943 |
//-- the state of this object now will be controlled by the thread |
|
1944 |
SetState(ENotStarted); |
|
1945 |
||
1946 |
//-- 3. resume thread and wait until it finishes its initialisation |
|
1947 |
TRequestStatus rqStatInit(KRequestPending); |
|
1948 |
||
1949 |
iThread.Logon(iThreadStatus); |
|
1950 |
iThread.Rendezvous(rqStatInit); |
|
1951 |
iThread.Resume(); |
|
1952 |
||
1953 |
User::WaitForRequest(rqStatInit); |
|
1954 |
||
1955 |
if(rqStatInit.Int() != KErrNone) |
|
1956 |
{//-- thread couldn't initialise |
|
1957 |
__PRINT1(_L("#=- CFatHelperThreadBase::DoLaunchThread() failure#2 res:%d"), nRes); |
|
1958 |
ForceStop(); |
|
1959 |
ASSERT(0); |
|
1960 |
return nRes; |
|
1961 |
} |
|
1962 |
||
1963 |
//-- Helper FAT thread is running now |
|
1964 |
return KErrNone; |
|
1965 |
} |
|
1966 |
||
1967 |
||
1968 |
//####################################################################################################################################### |
|
1969 |
//# CFat32ScanThread implementation |
|
1970 |
//####################################################################################################################################### |
|
1971 |
||
1972 |
||
1973 |
CFat32ScanThread::CFat32ScanThread(CAtaFatTable& aOwner) |
|
1974 |
:CFatHelperThreadBase(aOwner) |
|
1975 |
{ |
|
1976 |
} |
|
1977 |
||
1978 |
//----------------------------------------------------------------------------- |
|
1979 |
||
1980 |
/** |
|
1981 |
Launches the FAT32_ScanThread scaner thread. |
|
1982 |
@return standard error code |
|
1983 |
*/ |
|
1984 |
TInt CFat32ScanThread::Launch() |
|
1985 |
{ |
|
1986 |
return DoLaunchThread(FAT32_ScanThread, this); |
|
1987 |
} |
|
1988 |
||
1989 |
//----------------------------------------------------------------------------- |
|
1990 |
||
1991 |
/** |
|
1992 |
FAT32_ScanThread preamble function. It gets called by the scan thread at the very beginning. |
|
1993 |
Does some initialisation work and its return code is signaled to the thread owner by RThread::Rendezvous(); |
|
1994 |
||
1995 |
@return Thread object initialisation code, KErrNone on success. |
|
1996 |
*/ |
|
1997 |
TInt CFat32ScanThread::Thread_Preamble() |
|
1998 |
{ |
|
1999 |
//__PRINT(_L("#=- CFat32ScanThread::Thread_Preamble()")); |
|
2000 |
||
2001 |
ipFatBitCache = iOwner.iCache->BitCacheInterface(); |
|
2002 |
iTimeStart.UniversalTime(); //-- take thread start time |
|
2003 |
||
2004 |
ASSERT(State() == CFatHelperThreadBase::ENotStarted); //-- see the thread launcher |
|
2005 |
||
2006 |
if(!iOwner.IsFat32()) |
|
2007 |
{//-- this stuff is supposed to work for FAT32 only |
|
2008 |
ASSERT(0); |
|
2009 |
return KErrArgument; |
|
2010 |
} |
|
2011 |
||
2012 |
return KErrNone; |
|
2013 |
} |
|
2014 |
||
2015 |
//----------------------------------------------------------------------------- |
|
2016 |
/** |
|
2017 |
FAT32_ScanThread postamble function. It gets called by the scan thread just before its function exits. |
|
2018 |
Does some finalisation work and its return code is the thread completion code; |
|
2019 |
||
2020 |
@return Thread object finalisation code, KErrNone on success. |
|
2021 |
*/ |
|
2022 |
TInt CFat32ScanThread::Thread_Postamble(TInt aResult) |
|
2023 |
{ |
|
2024 |
//__PRINT(_L("#=- CFat32ScanThread::Thread_Postamble()")); |
|
2025 |
||
2026 |
#ifdef _DEBUG |
|
2027 |
//-- print out time taken the thread to finish |
|
2028 |
TName nameBuf; |
|
2029 |
iTimeEnd.UniversalTime(); //-- take end time |
|
2030 |
const TInt msScanTime = (TInt)( (iTimeEnd.MicroSecondsFrom(iTimeStart)).Int64() / K1mSec); |
|
2031 |
nameBuf.Copy(RThread().Name()); |
|
2032 |
nameBuf.Insert(0,_L("#=-<<<")); |
|
2033 |
nameBuf.AppendFormat(_L(" Thread Exit. id:%d, Code:%d, time:%d ms"), (TUint)RThread().Id(), aResult, msScanTime); |
|
2034 |
__PRINT(nameBuf); |
|
2035 |
#endif |
|
2036 |
||
2037 |
//-- tell FAT bit supercache (if we have it) that we have finished populating it, successfully or not |
|
2038 |
if(ipFatBitCache) |
|
2039 |
{ |
|
2040 |
ipFatBitCache->FinishPopulating(aResult == KErrNone); |
|
2041 |
ipFatBitCache->Dump(); |
|
2042 |
} |
|
2043 |
||
2044 |
//-- close FAT chunk buffer |
|
2045 |
iFatChunkBuf.Close(); |
|
2046 |
||
2047 |
//-- set the host object state depending on the work results. |
|
2048 |
if(aResult == KErrNone) |
|
2049 |
SetState(CFatHelperThreadBase::EFinished_OK); |
|
2050 |
else |
|
2051 |
SetState(CFatHelperThreadBase::EFailed); |
|
2052 |
||
2053 |
||
2054 |
return aResult; |
|
2055 |
} |
|
2056 |
||
2057 |
//####################################################################################################################################### |
|
2058 |
//# CFat32FreeSpaceScanner implementation |
|
2059 |
//####################################################################################################################################### |
|
2060 |
||
2061 |
CFat32FreeSpaceScanner::CFat32FreeSpaceScanner(CAtaFatTable& aOwner) |
|
2062 |
:CFat32ScanThread(aOwner) |
|
2063 |
{ |
|
2064 |
} |
|
2065 |
||
2066 |
/** |
|
2067 |
Factory method. |
|
2068 |
@param aOwner owning CAtaFatTable |
|
2069 |
@return pointer to the constructed instance of the class |
|
2070 |
*/ |
|
2071 |
CFat32FreeSpaceScanner* CFat32FreeSpaceScanner::NewL(CAtaFatTable& aOwner) |
|
2072 |
{ |
|
2073 |
CFat32FreeSpaceScanner* pThis = NULL; |
|
2074 |
pThis = new (ELeave) CFat32FreeSpaceScanner(aOwner); |
|
2075 |
||
2076 |
return pThis; |
|
2077 |
} |
|
2078 |
||
2079 |
//----------------------------------------------------------------------------- |
|
2080 |
||
2081 |
/** |
|
2082 |
Waits until FAT32 free clusters scan thread allows other thread (caller) to write to the FAT entry "aFatIndex". |
|
2083 |
Thread scans FAT from the beginning to the end and just waits untill scanning passes the entry number "aFatIndex" |
|
2084 |
||
2085 |
@param aFatIndex index of the FAT entry we are going to write. |
|
2086 |
*/ |
|
2087 |
void CFat32FreeSpaceScanner::RequestFatEntryWriteAccess(TUint32 aFatIndex) const |
|
2088 |
{ |
|
2089 |
if(!ThreadWorking()) |
|
2090 |
return; |
|
2091 |
||
2092 |
ASSERT(iOwner.ClusterNumberValid(aFatIndex)); |
|
2093 |
||
2094 |
const TUint KWaitGranularity = 20*K1mSec; //-- wait granularity |
|
2095 |
||
2096 |
//-- wait until FAT[aFatIndex] is available to write |
|
2097 |
while(aFatIndex > ClustersScanned() && ThreadWorking()) |
|
2098 |
{ |
|
2099 |
BoostPriority(ETrue); //-- Boost scan thread priority |
|
2100 |
User::After(KWaitGranularity); |
|
2101 |
} |
|
2102 |
} |
|
2103 |
||
2104 |
//----------------------------------------------------------------------------- |
|
2105 |
||
2106 |
/** just an internal helper method. Stores the number of FAT entries already scanned by FAT free entries scan thread. */ |
|
2107 |
void CFat32FreeSpaceScanner::SetClustersScanned(TUint32 aClusters) |
|
2108 |
{ |
|
2109 |
XAutoLock lock(iOwner.DriveInterface()); //-- enter critical section |
|
2110 |
iClustersScanned=aClusters; |
|
2111 |
} |
|
2112 |
||
2113 |
/** just an internal helper method. returns the number of FAT entries already scanned by FAT free entrie sscan thread. */ |
|
2114 |
TUint32 CFat32FreeSpaceScanner::ClustersScanned() const |
|
2115 |
{ |
|
2116 |
XAutoLock lock(iOwner.DriveInterface()); //-- enter critical section |
|
2117 |
return iClustersScanned; |
|
2118 |
} |
|
2119 |
||
2120 |
//----------------------------------------------------------------------------- |
|
2121 |
||
2122 |
/** |
|
2123 |
overriden FAT32_ScanThread preamble function. |
|
2124 |
See CFat32ScanThread::Thread_Preamble() |
|
2125 |
*/ |
|
2126 |
TInt CFat32FreeSpaceScanner::Thread_Preamble() |
|
2127 |
{ |
|
2128 |
__PRINT1(_L("#=- CFat32FreeSpaceScanner::Thread_Preamble(), FAT state:%d"), iOwner.State()); |
|
2129 |
||
2130 |
ASSERT(iOwner.State() == CAtaFatTable::EFreeClustersScan); |
|
2131 |
||
2132 |
//-- invoke generic preamble first |
|
2133 |
TInt nRes = CFat32ScanThread::Thread_Preamble(); |
|
2134 |
if(nRes != KErrNone) |
|
2135 |
return nRes; |
|
2136 |
||
2137 |
//-- do specific to this thread object initialisation work |
|
2138 |
||
2139 |
//-- rename the thread |
|
2140 |
TName nameBuf; |
|
2141 |
const CFatMountCB& fatMount = *(iOwner.OwnerMount()); |
|
2142 |
nameBuf.Format(_L("Fat32FreeSpaceScanner_drv_%d"), fatMount.DriveNumber()); |
|
2143 |
RThread::RenameMe(nameBuf); |
|
2144 |
||
2145 |
//-- allocate FAT chunk buffer; its size will depend on FAT table size. |
|
2146 |
const TUint32 fatSz = iOwner.MaxEntries() << KFat32EntrySzLog2; |
|
2147 |
||
2148 |
if(fatSz < KBigSzFat_Threshold) |
|
2149 |
{//-- create a small buffer |
|
2150 |
if(iFatChunkBuf.CreateMax(KFatChunkBufSize_Small) != KErrNone) |
|
2151 |
return KErrNoMemory; |
|
2152 |
} |
|
2153 |
else |
|
2154 |
{//-- try to create larger buffer |
|
2155 |
if(iFatChunkBuf.CreateMax(KFatChunkBufSize_Big) != KErrNone && iFatChunkBuf.CreateMax(KFatChunkBufSize_Small) != KErrNone) |
|
2156 |
return KErrNoMemory; |
|
2157 |
} |
|
2158 |
||
2159 |
||
2160 |
//-- setup FAT table's parameters |
|
2161 |
//-- No free clusters yet; be careful with SetFreeClusters(), free clusters count can be |
|
2162 |
//-- modified from other thread, e.g. from FreeClusterList. Use read-modify-write instead of assignment. |
|
2163 |
SetClustersScanned(0); |
|
2164 |
iOwner.SetFreeClusters(0); |
|
2165 |
||
2166 |
//-- calculate number of FAT entires need to be processed for CMountCB::SetDiskSpaceChange() call. |
|
2167 |
//-- if number of processed entries in FAT exceeds iEntriesNotifyThreshold, CMountCB::SetDiskSpaceChange() |
|
2168 |
//-- will be called and the iEntriesNotifyThreshold will be updated. |
|
2169 |
iNfyThresholdInc = (TUint32)KVolSpaceNotifyThreshold >> fatMount.ClusterSizeLog2(); |
|
2170 |
iEntriesNotifyThreshold = iNfyThresholdInc; |
|
2171 |
||
2172 |
//-- if there is an interface to the FAT bit supercache, tell it to start populating. |
|
2173 |
//-- We will be populating this cache while reading and parsing FAT32. |
|
2174 |
if(ipFatBitCache) |
|
2175 |
ipFatBitCache->StartPopulating(); |
|
2176 |
||
2177 |
||
2178 |
return KErrNone; |
|
2179 |
} |
|
2180 |
||
2181 |
//----------------------------------------------------------------------------- |
|
2182 |
/** |
|
2183 |
overriden FAT32_ScanThread postamble function. |
|
2184 |
See CFat32ScanThread::Thread_Postamble() |
|
2185 |
*/ |
|
2186 |
TInt CFat32FreeSpaceScanner::Thread_Postamble(TInt aResult) |
|
2187 |
{ |
|
2188 |
__PRINT2(_L("#=- CFat32FreeSpaceScanner::Thread_Postamble(%d), FAT state:%d"), aResult, iOwner.State()); |
|
2189 |
__PRINT2(_L("#=- FAT_ScanThread: counted Free clusters:%d, 1st free:%d"), iOwner.NumberOfFreeClusters(), iOwner.FreeClusterHint()); |
|
2190 |
||
2191 |
ASSERT(iOwner.State() == CAtaFatTable::EFreeClustersScan); |
|
2192 |
||
2193 |
//-- there was an error somewhere within FAT32 scan thread |
|
2194 |
if(aResult != KErrNone) |
|
2195 |
{ |
|
2196 |
//-- indicate that the FAT table initialisation failed |
|
2197 |
__PRINT(_L("#=- Asynch FAT table initialisation failed !")); |
|
2198 |
||
2199 |
iOwner.SetState(CAtaFatTable::EMountAborted); |
|
2200 |
||
2201 |
//-- fix up some FAT table parameters |
|
2202 |
if(iOwner.FreeClusterHint() < KFatFirstSearchCluster) |
|
2203 |
iOwner.SetFreeClusterHint(KFatFirstSearchCluster); |
|
2204 |
||
2205 |
} |
|
2206 |
||
2207 |
||
2208 |
//-- call generic postamble |
|
2209 |
TInt nRes = CFat32ScanThread::Thread_Postamble(aResult); |
|
2210 |
||
2211 |
if(nRes == KErrNone) |
|
2212 |
{//-- FAT table now fully initialised |
|
2213 |
ASSERT(aResult == KErrNone); |
|
2214 |
iOwner.SetState(CAtaFatTable::EMounted); |
|
2215 |
||
2216 |
//-- free space counting finished OK, call the notifier last time |
|
2217 |
CFatMountCB& fatMount = *(iOwner.OwnerMount()); |
|
2218 |
||
2219 |
iOwner.AcquireLock(); |
|
2220 |
const TInt64 currFreeSpace = ((TInt64)iOwner.FreeClusters()) << fatMount.ClusterSizeLog2(); |
|
2221 |
iOwner.ReleaseLock(); |
|
2222 |
||
2223 |
fatMount.SetDiskSpaceChange(currFreeSpace); |
|
2224 |
||
2225 |
||
2226 |
} |
|
2227 |
else if(aResult == KErrNone) |
|
2228 |
{//-- CFat32ScanThread::Thread_Postamble() signaled a fault |
|
2229 |
iOwner.SetState(CAtaFatTable::EMountAborted); |
|
2230 |
} |
|
2231 |
||
2232 |
return aResult; |
|
2233 |
} |
|
2234 |
||
2235 |
//----------------------------------------------------------------------------- |
|
2236 |
/** |
|
2237 |
Process free FAT entries collected by the scan thread that parses chunk of FAT data. |
|
2238 |
This method gets called by the FAT scanning thread after a portion of FAT is read into the buffer and parsed |
|
2239 |
||
2240 |
@param aFreeEntriesInChunk number of free FAT entries counted in FAT chunk |
|
2241 |
@param aCurrFirstFreeEntry current number of the first free FAT entry found |
|
2242 |
@param aClustersScanned total number of FAT entries scanned by the thread |
|
2243 |
||
2244 |
@return standard error code, KErrNone on success |
|
2245 |
*/ |
|
2246 |
TInt CFat32FreeSpaceScanner::Thread_ProcessCollectedFreeEntries(const CAtaFatTable::TFatScanParam& aFatScanParam) |
|
2247 |
{ |
|
2248 |
ASSERT(State() == CFatHelperThreadBase::EWorking); |
|
2249 |
||
2250 |
CAtaFatTable& ataFatTable = iOwner; |
|
2251 |
||
2252 |
//------------------------------------------- |
|
2253 |
//-- publish values to the CAtaFatTable object |
|
2254 |
ataFatTable.AcquireLock(); |
|
2255 |
||
2256 |
//-- publish free cluster count, use read-modify-write here |
|
2257 |
//-- CFatTable::iFreeClusters can be already modified from other thread. |
|
2258 |
TUint32 currFreeClusters = ataFatTable.FreeClusters(); //-- simple non-thread safe method |
|
2259 |
||
2260 |
currFreeClusters += aFatScanParam.iCurrFreeEntries; |
|
2261 |
||
2262 |
ataFatTable.SetFreeClusters(currFreeClusters); |
|
2263 |
||
2264 |
//-- store total number of scanned clusters (not to be modified from other thread) |
|
2265 |
const TUint32 scannedEntries = aFatScanParam.iEntriesScanned; |
|
2266 |
SetClustersScanned(scannedEntries); |
|
2267 |
||
2268 |
||
2269 |
if(aFatScanParam.iFirstFree >= KFatFirstSearchCluster) |
|
2270 |
ataFatTable.SetFreeClusterHint(aFatScanParam.iFirstFree);//-- probably found next free cluster number |
|
2271 |
||
2272 |
ataFatTable.ReleaseLock(); |
|
2273 |
||
2274 |
//-- check if we need to call CMountCB::SetDiskSpaceChange() to notify it that the amount of processed FAT entries has reached the given threshold |
|
2275 |
if(scannedEntries >= iEntriesNotifyThreshold) |
|
2276 |
{ |
|
2277 |
iEntriesNotifyThreshold += iNfyThresholdInc; |
|
2278 |
||
2279 |
CFatMountCB& fatMount = *(iOwner.OwnerMount()); |
|
2280 |
const TInt64 currFreeSpace = ((TInt64)currFreeClusters) << fatMount.ClusterSizeLog2(); |
|
2281 |
fatMount.SetDiskSpaceChange(currFreeSpace); |
|
2282 |
} |
|
2283 |
||
2284 |
||
2285 |
return KErrNone; |
|
2286 |
} |
|
2287 |
||
2288 |
//####################################################################################################################################### |
|
2289 |
//# CFat32BitCachePopulator implementation |
|
2290 |
//####################################################################################################################################### |
|
2291 |
CFat32BitCachePopulator::CFat32BitCachePopulator(CAtaFatTable& aOwner) |
|
2292 |
:CFat32ScanThread(aOwner) |
|
2293 |
{ |
|
2294 |
} |
|
2295 |
||
2296 |
/** |
|
2297 |
Factory method. |
|
2298 |
@param aOwner owning CAtaFatTable |
|
2299 |
@return pointer to the constructed instance of the class |
|
2300 |
*/ |
|
2301 |
CFat32BitCachePopulator* CFat32BitCachePopulator::NewL(CAtaFatTable& aOwner) |
|
2302 |
{ |
|
2303 |
CFat32BitCachePopulator* pThis = NULL; |
|
2304 |
pThis = new (ELeave) CFat32BitCachePopulator(aOwner); |
|
2305 |
||
2306 |
return pThis; |
|
2307 |
} |
|
2308 |
||
2309 |
//----------------------------------------------------------------------------- |
|
2310 |
||
2311 |
/** |
|
2312 |
The main FS thread tries to write the "aFatIndex" entry in FAT while this thread is running. |
|
2313 |
We can't do anything useful here, because FAT32 bit supercache doesn't work on FAT entry level and |
|
2314 |
deals with much less scale - FAT32 cache sector, which can consist from many FAT32 entries. |
|
2315 |
The conflict situation will be resolved in the CAtaFatTable::WriteL() |
|
2316 |
*/ |
|
2317 |
void CFat32BitCachePopulator::RequestFatEntryWriteAccess(TUint32 /*aFatIndex*/) const |
|
2318 |
{ |
|
2319 |
//-- do nothing here, do not block the caller |
|
2320 |
} |
|
2321 |
||
2322 |
||
2323 |
//----------------------------------------------------------------------------- |
|
2324 |
/** |
|
2325 |
overriden FAT32_ScanThread preamble function. |
|
2326 |
See CFat32ScanThread::Thread_Preamble() |
|
2327 |
*/ |
|
2328 |
TInt CFat32BitCachePopulator::Thread_Preamble() |
|
2329 |
{ |
|
2330 |
__PRINT(_L("#=- CFat32BitCachePopulator::Thread_Preamble()")); |
|
2331 |
||
2332 |
//-- invoke generic preamble |
|
2333 |
TInt nRes = CFat32ScanThread::Thread_Preamble(); |
|
2334 |
if(nRes != KErrNone) |
|
2335 |
return nRes; |
|
2336 |
||
2337 |
//-- do specific to this thread object initialisation work |
|
2338 |
iTotalOccupiedFatEntries = 0; |
|
2339 |
||
2340 |
//-- rename the thread |
|
2341 |
TName nameBuf; |
|
2342 |
const CFatMountCB& fatMount = *(iOwner.OwnerMount()); |
|
2343 |
nameBuf.Format(_L("CFat32BitCachePopulator_drv_%d"), fatMount.DriveNumber()); |
|
2344 |
RThread::RenameMe(nameBuf); |
|
2345 |
||
2346 |
//-- allocate FAT chunk buffer |
|
2347 |
nRes = iFatChunkBuf.CreateMax(KFatChunkBufSize); |
|
2348 |
if(nRes != KErrNone) |
|
2349 |
return nRes; |
|
2350 |
||
2351 |
||
2352 |
if(!ipFatBitCache) |
|
2353 |
{//-- this is a bit cache populator and the bit cache object must have been constructed before setting up the populating thread. |
|
2354 |
ASSERT(0); |
|
2355 |
return KErrCorrupt; |
|
2356 |
} |
|
2357 |
||
2358 |
//-- Tell FAT bit supercache to start populating. We will be populating this cache while reading and parsing FAT32. |
|
2359 |
if(ipFatBitCache->StartPopulating()) |
|
2360 |
nRes = KErrNone; |
|
2361 |
else |
|
2362 |
nRes = KErrCorrupt; |
|
2363 |
||
2364 |
return nRes; |
|
2365 |
} |
|
2366 |
||
2367 |
//----------------------------------------------------------------------------- |
|
2368 |
||
2369 |
/** |
|
2370 |
overriden FAT32_ScanThread postamble function. |
|
2371 |
See CFat32ScanThread::Thread_Postamble() |
|
2372 |
*/ |
|
2373 |
TInt CFat32BitCachePopulator::Thread_Postamble(TInt aResult) |
|
2374 |
{ |
|
2375 |
__PRINT1(_L("#=- CFat32BitCachePopulator::Thread_Postamble(%d)"), aResult); |
|
2376 |
||
2377 |
//-- nothing specific to do, just call generic method |
|
2378 |
return CFat32ScanThread::Thread_Postamble(aResult); |
|
2379 |
} |
|
2380 |
||
2381 |
//----------------------------------------------------------------------------- |
|
2382 |
/** |
|
2383 |
This method gets called by the FAT scanning thread after a portion of FAT is read into the buffer and parsed |
|
2384 |
@return standard error code, KErrNone on success |
|
2385 |
*/ |
|
2386 |
TInt CFat32BitCachePopulator::Thread_ProcessCollectedFreeEntries(const CAtaFatTable::TFatScanParam& aFatScanParam) |
|
2387 |
{ |
|
2388 |
ASSERT(State() == CFatHelperThreadBase::EWorking); |
|
2389 |
||
2390 |
//-- check the bit cache state |
|
2391 |
if(ipFatBitCache->State() != CFatBitCache::EPopulating) |
|
2392 |
{//-- something wrong happened to the cache, e.g. someone forcedly invalidated it (probably from another thread) |
|
2393 |
return KErrAbort; |
|
2394 |
} |
|
2395 |
||
2396 |
||
2397 |
//-- if CFat32BitCachePopulator has already counted all _occupied_ FAT entries, there is no need to |
|
2398 |
//-- continue FAT reading; just mark the rest of the FAT bit supercache as containing free FAT entries and abort scanning |
|
2399 |
||
2400 |
CAtaFatTable& ataFatTable = iOwner; |
|
2401 |
||
2402 |
ataFatTable.AcquireLock(); |
|
2403 |
||
2404 |
//-- current amount of non-free entries in FAT, excluding FAT[0] & FAT[1] |
|
2405 |
const TUint32 KCurrNonFreeEntries = ataFatTable.MaxEntries() - ataFatTable.FreeClusters() - KFatFirstSearchCluster; |
|
2406 |
||
2407 |
iTotalOccupiedFatEntries += aFatScanParam.iCurrOccupiedEntries; |
|
2408 |
||
2409 |
//-- check if the thread needs to continue it work |
|
2410 |
const TBool KNoNeedToScanFurther = (iTotalOccupiedFatEntries >= KCurrNonFreeEntries); |
|
2411 |
||
2412 |
if(KNoNeedToScanFurther) |
|
2413 |
{ |
|
2414 |
//-- tell FAT bit supercache to mark the range from currently scanned FAT entry to the end of the FAT as containing free entries. |
|
2415 |
__PRINT2(_L("#=- CFat32BitCachePopulator::Thread_ProcessCollectedFreeEntries() counted: %d/%d; aborting scan."), iTotalOccupiedFatEntries, KCurrNonFreeEntries); |
|
2416 |
||
2417 |
const TUint32 entryStart = aFatScanParam.iEntriesScanned; //-- first FAT entry in the range to be marked as 'free' |
|
2418 |
const TUint32 entryEnd = ataFatTable.MaxEntries()-1; //-- last FAT entry in the range to be marked as 'free', last FAT entry |
|
2419 |
||
2420 |
ipFatBitCache->MarkFatRange(entryStart, entryEnd, ETrue); |
|
2421 |
||
2422 |
//-- signal that the thread shall finish with normal (KErrNone) reason |
|
2423 |
//-- it will also normally finish FAT bit cache populating in postamble |
|
2424 |
AllowToLive(EFalse); |
|
2425 |
} |
|
2426 |
||
2427 |
ataFatTable.ReleaseLock(); |
|
2428 |
||
2429 |
||
2430 |
return KErrNone; |
|
2431 |
} |
|
2432 |
||
2433 |
||
2434 |
//####################################################################################################################################### |
|
2435 |
/** |
|
2436 |
FAT32 free entries scan thread function. Walks through FAT32 and counts free entries. |
|
2437 |
It uses its own buffer to read FAT and parse it in order to avoid multithreaded problems with FAT cache and don't thrash it. |
|
2438 |
||
2439 |
@param apHostObject pointer to the host object of CFat32ScanThread base class. |
|
2440 |
*/ |
|
2441 |
//####################################################################################################################################### |
|
2442 |
TInt FAT32_ScanThread(TAny* apHostObject) |
|
2443 |
{ |
|
2444 |
TInt nRes; |
|
2445 |
||
2446 |
#ifdef _DEBUG |
|
2447 |
TName nameBuf; |
|
2448 |
nameBuf.Copy(RThread().Name()); |
|
2449 |
nameBuf.Insert(0,_L("#=->>>")); nameBuf.AppendFormat(_L(" Thread Enter (id:%d)"), (TUint)RThread().Id()); |
|
2450 |
__PRINT(nameBuf); |
|
2451 |
#endif |
|
2452 |
||
2453 |
ASSERT(apHostObject); |
|
2454 |
CFat32FreeSpaceScanner* pSelf = (CFat32FreeSpaceScanner*)apHostObject; |
|
2455 |
||
2456 |
CAtaFatTable& ataFatTable = pSelf->iOwner; |
|
2457 |
CFatMountCB& fatMount = *(ataFatTable.OwnerMount()); |
|
2458 |
||
2459 |
const TUint32 KFat32EntrySz = sizeof(TFat32Entry); |
|
2460 |
const TUint32 KFat1StartPos = fatMount.StartOfFatInBytes(); |
|
2461 |
const TUint32 KNumClusters = ataFatTable.MaxEntries(); //-- FAT[0] & FAT[1] are reserved and not counted by UsableClusters() |
|
2462 |
||
2463 |
//-- perform thread preamble work |
|
2464 |
nRes = pSelf->Thread_Preamble(); |
|
2465 |
||
2466 |
//-- signal the thread initialisation result |
|
2467 |
RThread::Rendezvous(nRes); |
|
2468 |
||
2469 |
||
2470 |
//-- Initialisation OK, do real job: FAT scanning |
|
2471 |
if(nRes == KErrNone) |
|
2472 |
{ |
|
2473 |
pSelf->SetState(CFatHelperThreadBase::EWorking); |
|
2474 |
||
2475 |
TUint32 rem = KNumClusters * KFat32EntrySz; |
|
2476 |
TUint32 mediaPos = KFat1StartPos; |
|
2477 |
||
2478 |
CAtaFatTable::TFatScanParam fatScanParam; //-- FAT scanning parameters |
|
2479 |
||
2480 |
//============================================ |
|
2481 |
//=== FAT read and parse loop ================ |
|
2482 |
//-- in this loop we read portions of raw FAT32 data in a buffer, than parse this buffer |
|
2483 |
//-- in order to find out the number of free FAT entries there and other stuff |
|
2484 |
while(rem) |
|
2485 |
{ |
|
2486 |
const TUint32 bytesToRead=Min(rem, (TUint32)pSelf->iFatChunkBuf.Size()); |
|
2487 |
TPtrC8 ptrData(pSelf->iFatChunkBuf.Ptr(), bytesToRead); |
|
2488 |
||
2489 |
//-- check for sudden media change |
|
2490 |
if(fatMount.Drive().IsChanged()) |
|
2491 |
{ |
|
2492 |
__PRINT(_L("#=--- FAT32_ScanThread: Media change occured, aborting!")); |
|
2493 |
nRes = KErrAbort; |
|
2494 |
break; |
|
2495 |
} |
|
2496 |
||
2497 |
//------------------------------------------- |
|
2498 |
//-- read a portion of FAT into the buffer |
|
2499 |
ataFatTable.AcquireLock(); |
|
2500 |
||
2501 |
//-- check if the thread was requested to finish |
|
2502 |
if(!pSelf->AllowedToLive()) |
|
2503 |
{ |
|
2504 |
ataFatTable.ReleaseLock(); |
|
2505 |
nRes = KErrAbort; |
|
2506 |
break; |
|
2507 |
} |
|
2508 |
||
2509 |
//-- actual read |
|
2510 |
//__PRINT3(_L("#=--- FAT32_ScanThread: read %d bytes pos:0x%x, boost:%d"), bytesToRead, mediaPos, pSelf->IsPriorityBoosted()); |
|
2511 |
||
2512 |
nRes = fatMount.LocalDrive()->Read(mediaPos, bytesToRead, pSelf->iFatChunkBuf); |
|
2513 |
||
2514 |
ataFatTable.ReleaseLock(); |
|
2515 |
||
2516 |
//------------------------------------------- |
|
2517 |
//-- analyse the read error code |
|
2518 |
if(nRes != KErrNone) |
|
2519 |
{ |
|
2520 |
__PRINT1(_L("#=--- FAT32_ScanThread read error! res:%d"), nRes); |
|
2521 |
break; //-- abort scanning |
|
2522 |
} |
|
2523 |
||
2524 |
//------------------------------------------- |
|
2525 |
//-- parse FAT from the buffer |
|
2526 |
||
2527 |
//-- we need number of free and occupied entries in the _current_ FAT chunk being read and parsed |
|
2528 |
fatScanParam.iCurrFreeEntries = 0; |
|
2529 |
fatScanParam.iCurrOccupiedEntries = 0; |
|
2530 |
||
2531 |
ataFatTable.DoParseFatBuf(ptrData, fatScanParam); |
|
2532 |
||
2533 |
//--- process the the results of FAT buffer parsing |
|
2534 |
nRes = pSelf->Thread_ProcessCollectedFreeEntries(fatScanParam); |
|
2535 |
if(nRes != KErrNone || !pSelf->AllowedToLive()) |
|
2536 |
{//-- some types of worker threads may wish to finish normally but prematurely, by the result of Thread_ProcessCollectedFreeEntries() |
|
2537 |
break; //-- abort scanning |
|
2538 |
} |
|
2539 |
||
2540 |
||
2541 |
//-- allow this thread to be preempted by another one that wants to access the media driver. |
|
2542 |
//-- without this wait we will have priority inversion, because this (low priority) thread continiously reads data by big chunks |
|
2543 |
//-- and doesn't allow others to access the driver. |
|
2544 |
//-- On the other hand, if the thread's priority is boosted, there is no reason to be polite. |
|
2545 |
if(!pSelf->IsPriorityBoosted()) |
|
2546 |
User::After(K1mSec); //-- User::After() granularity can be much coarser than 1ms |
|
2547 |
||
2548 |
//------------------------------------------- |
|
2549 |
mediaPos += bytesToRead; |
|
2550 |
rem -= bytesToRead; |
|
2551 |
||
2552 |
}//while(rem) |
|
2553 |
||
2554 |
}//if(nRes == KErrNone) |
|
2555 |
||
2556 |
||
2557 |
//-- perform thread postamble work |
|
2558 |
nRes = pSelf->Thread_Postamble(nRes); |
|
2559 |
||
2560 |
return nRes; |
|
2561 |
} |
|
2562 |
||
2563 |
||
2564 |
||
2565 |
||
2566 |
||
2567 |
||
2568 |
||
2569 |
||
2570 |
||
2571 |
||
2572 |
||
2573 |
||
2574 |
||
2575 |
||
2576 |
||
2577 |
||
2578 |
||
2579 |
||
2580 |
||
2581 |
||
2582 |
||
2583 |
||
2584 |
||
2585 |
||
2586 |
||
2587 |
||
2588 |
||
2589 |
||
2590 |
||
2591 |
||
2592 |
||
2593 |
||
2594 |