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