author | Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com> |
Tue, 11 May 2010 17:28:22 +0300 | |
branch | RCL_3 |
changeset 110 | c734af59ce98 |
parent 0 | a41df078684a |
child 248 | 0ffb4e86fcc9 |
permissions | -rw-r--r-- |
0 | 1 |
// Copyright (c) 2006-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\sfile\sf_file_cache.cpp |
|
15 |
// |
|
16 |
// |
|
17 |
||
18 |
/** |
|
19 |
@file |
|
20 |
@internalTechnology |
|
21 |
*/ |
|
22 |
||
23 |
#include <e32std.h> |
|
24 |
#include <e32std_private.h> |
|
25 |
#include "sf_std.h" |
|
26 |
#include <e32uid.h> |
|
27 |
#include <e32wins.h> |
|
28 |
#include <f32file.h> |
|
29 |
#include <hal.h> |
|
30 |
#include "sf_file_cache.h" |
|
31 |
#include "sf_cache_man.h" |
|
32 |
#include "sf_cache_client.h" |
|
33 |
#include "sf_file_cache_defs.h" |
|
34 |
||
35 |
||
36 |
// disables flushing of stale cachelines before each write |
|
37 |
#define LAZY_WRITE |
|
38 |
||
39 |
enum TFileCacheFault |
|
40 |
{ |
|
41 |
ECacheSettingsInitFailed, |
|
42 |
ECacheSettingsNotFound, |
|
43 |
ECacheSettingGetFailed, |
|
44 |
ECacheCodeFault, |
|
45 |
ECacheBadOperationIndex, |
|
46 |
ENoFileCache, |
|
47 |
EFileAlreadyClosed, |
|
48 |
EClosingDirtyFile, |
|
49 |
ECompletingWriteWithDataRemaining, |
|
50 |
EPosBeyondSize, |
|
51 |
EMsgAlreadyCompleted, |
|
52 |
EBadRetCode, |
|
53 |
EInternalError, |
|
54 |
ERequestUncancelled, |
|
55 |
ELockAlreadyOpen, |
|
56 |
EBadSegmentCount, |
|
57 |
EReNewingOpenCache, |
|
58 |
EClosingUnNamedFile, |
|
59 |
EFileNameAlreadyOwned, |
|
60 |
EClosedFileHasNoName, |
|
61 |
EReOpeningUnNamedFile, |
|
62 |
EStartingDirtyAWrongStage, |
|
63 |
EUnexpectedReNewLFailure, |
|
64 |
EDirtyDataOwnerNull, |
|
65 |
EFlushingWithSessionNull, |
|
66 |
}; |
|
67 |
||
68 |
||
69 |
LOCAL_C void Fault(TFileCacheFault aFault) |
|
70 |
// |
|
71 |
// Report a fault in the file cache |
|
72 |
// |
|
73 |
{ |
|
74 |
User::Panic(_L("FSFILECACHE"), aFault); |
|
75 |
} |
|
76 |
||
77 |
const TInt KMinSequentialReadsBeforeReadAhead = 3; |
|
78 |
||
79 |
//************************************ |
|
80 |
// CFileCache |
|
81 |
//************************************ |
|
82 |
||
83 |
inline TBool ReadCachingEnabled(CFileShare& aShare) |
|
84 |
{ |
|
85 |
TUint mode = aShare.iMode; |
|
86 |
||
87 |
TInt fileCacheFlags = TFileCacheSettings::Flags(aShare.File().Drive().DriveNumber()); |
|
88 |
||
89 |
if (((mode & EFileReadBuffered) && (fileCacheFlags & (EFileCacheReadEnabled | EFileCacheReadOn))) || |
|
90 |
(((mode & EFileReadDirectIO) == 0) && (fileCacheFlags & EFileCacheReadOn))) |
|
91 |
{ |
|
92 |
return ETrue; |
|
93 |
} |
|
94 |
else |
|
95 |
{ |
|
96 |
return EFalse; |
|
97 |
} |
|
98 |
} |
|
99 |
||
100 |
inline TBool WriteCachingEnabled(CFileShare& aShare) |
|
101 |
{ |
|
102 |
TUint mode = aShare.iMode; |
|
103 |
||
104 |
TInt fileCacheFlags = TFileCacheSettings::Flags(aShare.File().Drive().DriveNumber()); |
|
105 |
||
106 |
if ((mode & EFileWrite) == 0) |
|
107 |
{ |
|
108 |
return EFalse; |
|
109 |
} |
|
110 |
else if (((mode & EFileWriteBuffered) && (fileCacheFlags & (EFileCacheWriteEnabled | EFileCacheWriteOn))) || |
|
111 |
(((mode & EFileWriteDirectIO) == 0) && (fileCacheFlags & EFileCacheWriteOn))) |
|
112 |
{ |
|
113 |
return ETrue; |
|
114 |
} |
|
115 |
else |
|
116 |
{ |
|
117 |
return EFalse; |
|
118 |
} |
|
119 |
} |
|
120 |
||
121 |
void CFileCache::SetFileCacheFlags(CFileShare& aShare) |
|
122 |
{ |
|
123 |
TInt fileCacheFlags = TFileCacheSettings::Flags(iDriveNum); |
|
124 |
||
125 |
TUint& mode = aShare.iMode; |
|
126 |
||
127 |
// enable/disable read ahead |
|
128 |
if (((mode & EFileReadAheadOn) && (fileCacheFlags & (EFileCacheReadAheadEnabled | EFileCacheReadAheadOn))) || |
|
129 |
(((mode & EFileReadAheadOff) == 0) && (fileCacheFlags & EFileCacheReadAheadOn))) |
|
130 |
{ |
|
131 |
__CACHE_PRINT(_L("CACHEFILE: EFileCacheReadAhead ENABLED")); |
|
132 |
mode|= EFileReadAheadOn; |
|
133 |
} |
|
134 |
else |
|
135 |
{ |
|
136 |
__CACHE_PRINT(_L("CACHEFILE: EFileCacheReadAhead disabled")); |
|
137 |
mode&= ~EFileReadAheadOn; |
|
138 |
} |
|
139 |
||
140 |
// enable/disable read caching |
|
141 |
if (ReadCachingEnabled(aShare)) |
|
142 |
{ |
|
143 |
__CACHE_PRINT(_L("CACHEFILE: EFileCacheRead ENABLED")); |
|
144 |
mode|= EFileReadBuffered; |
|
145 |
} |
|
146 |
else |
|
147 |
{ |
|
148 |
__CACHE_PRINT(_L("CACHEFILE: EFileCacheRead disabled")); |
|
149 |
// if read caching is off, turn off read-ahead too |
|
150 |
mode&= ~(EFileReadBuffered | EFileReadAheadOn); |
|
151 |
} |
|
152 |
||
153 |
// enable/disable write caching |
|
154 |
if (WriteCachingEnabled(aShare)) |
|
155 |
{ |
|
156 |
__CACHE_PRINT(_L("CACHEFILE: EFileCacheWrite ENABLED")); |
|
157 |
mode|= EFileWriteBuffered; |
|
158 |
} |
|
159 |
else |
|
160 |
{ |
|
161 |
__CACHE_PRINT(_L("CACHEFILE: EFileCacheWrite disabled")); |
|
162 |
mode&= ~EFileWriteBuffered; |
|
163 |
} |
|
164 |
||
165 |
} |
|
166 |
||
167 |
void CFileCache::ConstructL(CFileShare& aShare) |
|
168 |
{ |
|
169 |
iDrive = &aShare.File().Drive(); |
|
170 |
iDriveNum = iDrive->DriveNumber(); |
|
171 |
iMount=&iDrive->CurrentMount(); |
|
172 |
||
173 |
DoInitL(iDriveNum); |
|
174 |
||
175 |
||
176 |
CCacheManager* manager = CCacheManagerFactory::CacheManager(); |
|
177 |
||
178 |
iCacheClient = manager->CreateClientL(); |
|
179 |
manager->RegisterClient(*iCacheClient); |
|
180 |
||
181 |
||
182 |
TInt segmentSize = SegmentSize(); |
|
183 |
TInt segmentSizeMask = I64LOW(SegmentSizeMask()); |
|
184 |
// Get file cache size |
|
185 |
iCacheSize = TFileCacheSettings::CacheSize(iDriveNum); |
|
186 |
// must be at least one segment |
|
187 |
iCacheSize = Max(iCacheSize, segmentSize); |
|
188 |
// round up to nearest whole segment |
|
189 |
iCacheSize = (iCacheSize + segmentSize - 1) & segmentSizeMask; |
|
190 |
||
191 |
// Get max read-ahead length |
|
192 |
iMaxReadAheadLen = TFileCacheSettings::MaxReadAheadLen(iDriveNum); |
|
193 |
// must be at least one segment |
|
194 |
iMaxReadAheadLen = Max(iMaxReadAheadLen, segmentSize); |
|
195 |
// round up to nearest whole segment |
|
196 |
iMaxReadAheadLen = (iMaxReadAheadLen + segmentSize - 1) & segmentSizeMask; |
|
197 |
// read-ahead should not be greater than the cache size minus one segment |
|
198 |
iMaxReadAheadLen = Min(iMaxReadAheadLen, iCacheSize - segmentSize); |
|
199 |
// ... or greater than one cacheline (128K should be enough !) |
|
200 |
iMaxReadAheadLen = Min(iMaxReadAheadLen, iCacheClient->CacheLineSize()); |
|
201 |
||
202 |
iFileCacheReadAsync = TFileCacheSettings::FileCacheReadAsync(iDriveNum); |
|
203 |
||
204 |
iClosedFileKeepAliveTime = TFileCacheSettings::ClosedFileKeepAliveTime(iDriveNum); |
|
205 |
iDirtyDataFlushTime = TFileCacheSettings::DirtyDataFlushTime(iDriveNum); |
|
206 |
||
207 |
// Calculate max number of segments to cache |
|
208 |
TInt maxSegmentsToCache = iCacheSize >> SegmentSizeLog2(); |
|
209 |
||
210 |
__CACHE_PRINT1(_L("CACHEFILE: maxSegmentsToCache %d"), maxSegmentsToCache); |
|
211 |
||
212 |
iCacheClient->SetMaxSegments(maxSegmentsToCache); |
|
213 |
||
214 |
CFileCache* fileCache = ReNewL(aShare); |
|
215 |
__ASSERT_ALWAYS(fileCache != NULL, Fault(EUnexpectedReNewLFailure)); |
|
216 |
} |
|
217 |
||
218 |
CFileCache* CFileCache::NewL(CFileShare& aShare) |
|
219 |
{ |
|
220 |
__CACHE_PRINT(_L("CACHEFILE: CFileCache::NewL()")); |
|
221 |
||
222 |
CFileCB* file = &aShare.File(); |
|
223 |
if ((CCacheManagerFactory::CacheManager() == NULL) || |
|
224 |
(!ReadCachingEnabled(aShare) && !WriteCachingEnabled(aShare)) || |
|
225 |
(FsThreadManager::IsDriveSync(file->Drive().DriveNumber(),EFalse))) |
|
226 |
return NULL; |
|
227 |
||
228 |
CFileCache* fileCache = new(ELeave) CFileCache(); |
|
229 |
CleanupClosePushL(*fileCache); |
|
230 |
fileCache->ConstructL(aShare); |
|
231 |
CleanupStack::Pop(1, fileCache); |
|
232 |
return fileCache; |
|
233 |
} |
|
234 |
||
235 |
CFileCache* CFileCache::ReNewL(CFileShare& aShare) |
|
236 |
{ |
|
237 |
__CACHE_PRINT(_L("CACHEFILE: CFileCache::ReNewL()")); |
|
238 |
||
239 |
// check not already open i.e. attached to a CFileCB |
|
240 |
__ASSERT_DEBUG(iFileCB == NULL, Fault(EReNewingOpenCache)); |
|
241 |
||
242 |
// make sure the drive thread exists (the mount may have been mounted |
|
243 |
// synchronously since the drive was last open) |
|
244 |
const TInt r = FsThreadManager::GetDriveThread(iDriveNum, &iDriveThread); |
|
245 |
if ((r!= KErrNone) || !iDriveThread) |
|
246 |
return NULL; |
|
247 |
||
248 |
// if re-opening in DirectIo mode, destroy the file cache |
|
249 |
if (!ReadCachingEnabled(aShare) && !WriteCachingEnabled(aShare)) |
|
250 |
{ |
|
251 |
Close(); |
|
252 |
return NULL; |
|
253 |
} |
|
254 |
||
255 |
SetFileCacheFlags(aShare); |
|
256 |
||
257 |
// assign ownership of this object to aFileCB before any leaves occur |
|
258 |
iFileCB = &aShare.File(); |
|
259 |
iFileCB->iBody->iFileCache = this; |
|
260 |
||
261 |
__ASSERT_DEBUG(iLock.Handle() == KNullHandle, Fault(ELockAlreadyOpen)); |
|
262 |
User::LeaveIfError(iLock.CreateLocal()); |
|
263 |
||
264 |
// delete the file name to save heap space (it's only needed |
|
265 |
// while the file is on the closed queue so that it can be matched) |
|
266 |
delete iFileNameF; |
|
267 |
iFileNameF = NULL; |
|
268 |
||
269 |
return this; |
|
270 |
} |
|
271 |
||
272 |
void CFileCache::Init(CFileShare& aShare) |
|
273 |
{ |
|
274 |
SetFileCacheFlags(aShare); |
|
275 |
} |
|
276 |
||
277 |
CFileCache::CFileCache() : |
|
278 |
iClosedTimer(ClosedTimerEvent, this), |
|
279 |
iDirtyTimer(DirtyTimerEvent, this) |
|
280 |
{ |
|
281 |
} |
|
282 |
||
283 |
CFileCache::~CFileCache() |
|
284 |
{ |
|
285 |
iLock.Close(); |
|
286 |
||
287 |
// stop the owning CFileCB from pointing to an about-to-be deleted object |
|
288 |
if (iFileCB && iFileCB->iBody) |
|
289 |
iFileCB->iBody->iFileCache = NULL; |
|
290 |
||
291 |
CCacheManagerFactory::CacheManager()->DeregisterClient(*iCacheClient); |
|
292 |
||
293 |
delete iCacheClient; |
|
294 |
||
295 |
delete iFileNameF; |
|
296 |
} |
|
297 |
||
298 |
||
299 |
TInt CFileCache::ClosedTimerEvent(TAny* aFileCache) |
|
300 |
{ |
|
301 |
TClosedFileUtils::Remove((CFileCache*) aFileCache); |
|
302 |
return KErrNone; |
|
303 |
} |
|
304 |
||
305 |
TInt CFileCache::DirtyTimerEvent(TAny* aFileCache) |
|
306 |
{ |
|
307 |
// Cannot report errors here |
|
308 |
// coverity [unchecked_value] |
|
309 |
(void)((CFileCache*) aFileCache)->FlushDirty(); |
|
310 |
||
311 |
return KErrNone; |
|
312 |
} |
|
313 |
||
314 |
||
315 |
||
316 |
void CFileCache::Close() |
|
317 |
{ |
|
318 |
__CACHE_PRINT1(_L("CFileCache::Close() 0x%x"),this); |
|
319 |
||
320 |
TInt r = KErrNone; |
|
321 |
||
322 |
#ifdef _DEBUG |
|
323 |
if (iCacheClient) // NB Object may not have been fully constructed |
|
324 |
{ |
|
325 |
TInt64 pos; |
|
326 |
TUint8* addr; |
|
327 |
__ASSERT_DEBUG(((iCacheClient->FindFirstDirtySegment(pos, addr)) == 0), Fault(EClosingDirtyFile)); |
|
328 |
__ASSERT_DEBUG(!iDirtyDataOwner, Fault(EClosingDirtyFile)); |
|
329 |
} |
|
330 |
#endif |
|
331 |
||
332 |
||
333 |
__CACHE_PRINT2(_L("CFileCache::Close() iFileCB %08X IsClosed %d"), |
|
334 |
iFileCB, TClosedFileUtils::IsClosed(this)); |
|
335 |
// if not already closed move to closed file queue |
|
336 |
if (iFileCB != NULL && |
|
337 |
!iFileCB->DeleteOnClose() && |
|
338 |
!TClosedFileUtils::IsClosed(this) && |
|
339 |
IsDriveThread()) |
|
340 |
{ |
|
341 |
// add to ClosedFiles container |
|
342 |
__CACHE_PRINT1(_L("CLOSEDFILES: Adding %S\n"), &iFileCB->FileNameF() ); |
|
343 |
TRAP(r, TClosedFileUtils::AddL(this, ETrue)); |
|
344 |
||
345 |
// Acquire ownership of the CFileCB's file name |
|
346 |
__ASSERT_DEBUG(iFileCB->iFileNameF, Fault(EClosingUnNamedFile)); |
|
347 |
__ASSERT_DEBUG(iFileNameF == NULL, Fault(EFileNameAlreadyOwned)); |
|
348 |
iFileNameF = iFileCB->iFileNameF; |
|
349 |
iNameHash = iFileCB->iNameHash; |
|
350 |
iFileCB->iFileNameF = NULL; |
|
351 |
||
352 |
// remove pointer to owning CFileCB as this is called from CFileCB's destructor |
|
353 |
iFileCB = NULL; |
|
354 |
||
355 |
// Successfully moved file ! |
|
356 |
if (r == KErrNone) |
|
357 |
{ |
|
358 |
// close the RFastLock object here to prevent OOM kernel tests from failing |
|
359 |
iLock.Close(); |
|
360 |
return; |
|
361 |
} |
|
362 |
} |
|
363 |
||
364 |
iClosedTimer.Stop(); |
|
365 |
||
366 |
// if already closed, close for good. N.B. CFsObject::DoClose() will |
|
367 |
// cause it to be removed from the ClosedFiles container |
|
368 |
CFsDispatchObject::Close(); |
|
369 |
} |
|
370 |
||
371 |
||
372 |
CMountCB& CFileCache::Mount() const |
|
373 |
{ |
|
374 |
return *iMount; |
|
375 |
} |
|
376 |
||
377 |
CFileCB* CFileCache::FileCB() |
|
378 |
{ |
|
379 |
return iFileCB; |
|
380 |
} |
|
381 |
||
382 |
||
383 |
||
384 |
TInt CFileCache::SegmentSize() const |
|
385 |
{ |
|
386 |
return iCacheClient->SegmentSize(); |
|
387 |
} |
|
388 |
||
389 |
TInt CFileCache::SegmentSizeLog2() const |
|
390 |
{ |
|
391 |
return iCacheClient->SegmentSizeLog2(); |
|
392 |
} |
|
393 |
||
394 |
TInt64 CFileCache::SegmentSizeMask() const |
|
395 |
{ |
|
396 |
return iCacheClient->SegmentSizeMask(); |
|
397 |
} |
|
398 |
||
399 |
/** |
|
400 |
Sets the cached file size |
|
401 |
||
402 |
@param aSize The size of the file. |
|
403 |
*/ |
|
404 |
void CFileCache::SetSize64(TInt64 aSize) |
|
405 |
{ |
|
406 |
__e32_atomic_store_ord64(&iSize64, aSize); |
|
407 |
} |
|
408 |
||
409 |
TDrive& CFileCache::Drive() const |
|
410 |
{ |
|
411 |
return *iDrive; |
|
412 |
} |
|
413 |
||
414 |
TUint32 CFileCache::NameHash() const |
|
415 |
{ |
|
416 |
return(iNameHash); |
|
417 |
} |
|
418 |
||
419 |
HBufC& CFileCache::FileNameF() const |
|
420 |
{ |
|
421 |
__ASSERT_DEBUG(iFileNameF, Fault(EClosedFileHasNoName)); |
|
422 |
return(*iFileNameF); |
|
423 |
} |
|
424 |
||
425 |
||
426 |
||
427 |
||
428 |
TBool CFileCache::IsDriveThread() |
|
429 |
{ |
|
430 |
return FsThreadManager::IsDriveThread(iDriveNum, EFalse); |
|
431 |
} |
|
432 |
||
433 |
||
434 |
void CFileCache::ResetReadAhead() |
|
435 |
{ |
|
436 |
// iSequentialReads = 0; |
|
437 |
iReadAheadLen = 0; |
|
438 |
iReadAheadPos = 0; |
|
439 |
} |
|
440 |
||
441 |
/** |
|
442 |
ReadAhead() - |
|
443 |
dispatches a new message to fill the read cache if |
|
444 |
(ReadAheadPos - ShareReadPos) <= (ReadAheadLen) |
|
445 |
||
446 |
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX |
|
447 |
^ ^ |
|
448 |
|----- ReadAheadLen -----------------> |
|
449 |
| | |
|
450 |
ShareReadPos ReadAheadPos |
|
451 |
||
452 |
Every successful read-ahead doubles ReadAheadLen until it reaches the maximum |
|
453 |
(KDefaultFileCacheMaxReadAheadLen). |
|
454 |
*/ |
|
455 |
void CFileCache::ReadAhead(CFsMessageRequest& aMsgRequest, TUint aMode) |
|
456 |
{ |
|
457 |
if (!(aMode & EFileReadAheadOn) || |
|
458 |
(iSequentialReads < KMinSequentialReadsBeforeReadAhead) || |
|
459 |
iMaxReadAheadLen == 0) |
|
460 |
return; |
|
461 |
||
462 |
||
463 |
TInt segmentSize = SegmentSize(); |
|
464 |
TInt64 segmentSizeMask = SegmentSizeMask(); |
|
465 |
||
466 |
// if the read-ahead pos has been reset to zero, then the read-ahead |
|
467 |
// position and length must be re-calculated |
|
468 |
||
469 |
TBool resetting = (iReadAheadPos == 0)?(TBool)ETrue:(TBool)EFalse; |
|
470 |
if (resetting) |
|
471 |
{ |
|
472 |
iReadAheadPos = (iLastReadPos + segmentSize - 1) & segmentSizeMask; |
|
473 |
||
474 |
// ensure read ahead len at least as big as last read |
|
475 |
iReadAheadLen = Max(iReadAheadLen, iLastReadLen); |
|
476 |
||
477 |
// round up to a segment size |
|
478 |
iReadAheadLen = (iReadAheadLen + segmentSize - 1) & (TInt) segmentSizeMask; |
|
479 |
||
480 |
// ensure read ahead len at least as big as 1 segments |
|
481 |
iReadAheadLen = Max(iReadAheadLen, segmentSize); |
|
482 |
||
483 |
// ensure read ahead len not greater than the maximum read ahead len |
|
484 |
iReadAheadLen = Min(iReadAheadLen, iMaxReadAheadLen); |
|
485 |
||
486 |
iReadAheadRequest = NULL; |
|
487 |
} |
|
488 |
||
489 |
TInt bytesBuffered = (TInt) (iReadAheadPos - iLastReadPos); |
|
490 |
TInt bytesNotBuffered = iMaxReadAheadLen - bytesBuffered; |
|
491 |
||
492 |
||
493 |
// if read-ahead buffer len > current read-ahead len OR |
|
494 |
// read-ahead buffer is more than half full, do nothing |
|
495 |
if ((iReadAheadRequest) || |
|
496 |
(bytesBuffered > iReadAheadLen) || |
|
497 |
(bytesBuffered > (iMaxReadAheadLen>>1)) || |
|
498 |
(iReadAheadPos >= iSize64)) |
|
499 |
{ |
|
500 |
return; |
|
501 |
} |
|
502 |
||
503 |
// double the read-ahead length - unless this is the first |
|
504 |
if (!resetting) |
|
505 |
iReadAheadLen<<= 1; |
|
506 |
||
507 |
// ensure read ahead len not greater than the free space available in buffer |
|
508 |
iReadAheadLen = Min(iReadAheadLen, bytesNotBuffered); |
|
509 |
||
510 |
// round up to a segment size |
|
511 |
iReadAheadLen = (iReadAheadLen + segmentSize - 1) & (TInt) segmentSizeMask; |
|
512 |
||
513 |
#if defined (_DEBUG_READ_AHEAD) |
|
514 |
TInt64 oldReadAheadPos = iReadAheadPos; |
|
515 |
#endif |
|
516 |
||
517 |
DoReadAhead(aMsgRequest, aMode); |
|
518 |
||
519 |
||
520 |
// RDebug::Print(_L("Buffered: old %d new %d"), bytesBuffered, (iReadAheadPos == 0)?bytesBuffered:iReadAheadPos - iLastReadPos); |
|
521 |
||
522 |
#if defined (_DEBUG_READ_AHEAD) |
|
523 |
RDebug::Print(_L("Buffered: old %d new %d iLastReadPos %d ReadAheadPos old %d new %d iReadAheadLen %d"), |
|
524 |
bytesBuffered, |
|
525 |
(TInt) ((iReadAheadPos == 0)?bytesBuffered:iReadAheadPos - iLastReadPos), |
|
526 |
I64LOW(iLastReadPos), |
|
527 |
I64LOW(oldReadAheadPos), |
|
528 |
I64LOW(iReadAheadPos), |
|
529 |
iReadAheadLen); |
|
530 |
#endif // (_DEBUG_READ_AHEAD) |
|
531 |
||
532 |
||
533 |
return; |
|
534 |
} |
|
535 |
||
536 |
||
537 |
void CFileCache::DoReadAhead(CFsMessageRequest& aMsgRequest, TUint aMode) |
|
538 |
{ |
|
539 |
||
540 |
||
541 |
if (iCacheClient->FindSegment(iReadAheadPos) != NULL) |
|
542 |
{ |
|
543 |
// if read ahead pos is already cached, then synchronous reads have caught up, |
|
544 |
// so reset read ahead pos. |
|
545 |
#if defined (_DEBUG_READ_AHEAD) |
|
546 |
RDebug::Print(_L("ReadAhead: pos %d already cached"), I64LOW(iReadAheadPos)); |
|
547 |
#endif |
|
548 |
iReadAheadPos = 0; |
|
549 |
return; |
|
550 |
} |
|
551 |
||
552 |
||
553 |
CFsClientMessageRequest* newRequest = NULL; |
|
554 |
TInt r = AllocateRequest(newRequest, EFalse, aMsgRequest.Session()); |
|
555 |
if (r != KErrNone) |
|
556 |
return; |
|
557 |
||
558 |
r = newRequest->PushOperation( |
|
559 |
iReadAheadPos, |
|
560 |
iReadAheadLen, |
|
561 |
(TUint8*) NULL, |
|
562 |
0, // aOffset |
|
563 |
NULL); // aCallback |
|
564 |
if (r != KErrNone) |
|
565 |
{ |
|
566 |
newRequest->Free(); |
|
567 |
newRequest = NULL; |
|
568 |
return; |
|
569 |
} |
|
570 |
||
571 |
__CACHE_PRINT2(_L("TFsFileRead: ReadAhead pos %ld len %d"), newRequest->CurrentOperation().iReadWriteArgs.iPos, newRequest->CurrentOperation().iReadWriteArgs.iTotalLength); |
|
572 |
||
573 |
// RDebug::Print(_L("ReadH:\tpos %d\tlen %d"), I64LOW(newRequest->CurrentOperation().iReadWriteArgs.iPos), newRequest->CurrentOperation().iReadWriteArgs.iTotalLength); |
|
574 |
||
575 |
r = ReadBuffered(*newRequest, aMode); |
|
576 |
if (r != CFsRequest::EReqActionContinue) |
|
577 |
{ |
|
578 |
// if read ahead pos is already cached, then synchronous reads have caught up, |
|
579 |
// so reset read ahead pos. |
|
580 |
if (r == CFsRequest::EReqActionComplete) |
|
581 |
{ |
|
582 |
#if defined (_DEBUG_READ_AHEAD) |
|
583 |
RDebug::Print(_L("ReadAhead pos %d ALREADY DONE !!!"), I64LOW(iReadAheadPos)); |
|
584 |
#endif |
|
585 |
iReadAheadPos = 0; |
|
586 |
} |
|
587 |
||
588 |
newRequest->PopOperation(); |
|
589 |
newRequest->Free(); |
|
590 |
newRequest = NULL; |
|
591 |
return; |
|
592 |
} |
|
593 |
||
594 |
iReadAheadPos = iReadAheadPos + iReadAheadLen; |
|
595 |
iReadAheadRequest = newRequest; |
|
596 |
||
597 |
#if defined (_DEBUG_READ_AHEAD) |
|
598 |
RDebug::Print(_L("Dispatching ReadAhead with %s priority"), iFileCacheReadAsync?_S16("HIGH"):_S16("LOW")); |
|
599 |
#endif |
|
600 |
// If if media driver reads are synchronous (i.e. not interrupt driven) dispatch read-ahead |
|
601 |
// with low priority so that it doesn't prevent client thread from running |
|
602 |
newRequest->Dispatch( |
|
603 |
EFalse, // don't init |
|
604 |
iFileCacheReadAsync?(TBool)EFalse:(TBool)ETrue, |
|
605 |
EFalse); // dispatch to back |
|
606 |
} |
|
607 |
||
608 |
TInt CFileCache::CompleteRead(CFsRequest* aRequest) |
|
609 |
{ |
|
610 |
CFsMessageRequest& msgRequest = *(CFsMessageRequest*) aRequest; |
|
611 |
||
612 |
__ASSERT_DEBUG(msgRequest.CurrentOperationPtr() != NULL, Fault(ECacheBadOperationIndex)); |
|
613 |
||
614 |
CFileShare* share; |
|
615 |
CFileCB* file; |
|
616 |
GetFileFromScratch(aRequest, share, file); |
|
617 |
CFileCache* fileCache = file->FileCache(); |
|
618 |
||
619 |
__ASSERT_DEBUG(fileCache != NULL, Fault(ENoFileCache)); |
|
620 |
#ifdef _DEBUG |
|
621 |
TInt cancelling = (msgRequest.LastError() == KErrCancel)?(TBool)ETrue:(TBool)EFalse; |
|
622 |
#endif |
|
623 |
||
624 |
TUint mode = share?share->iMode : EFileReadBuffered; |
|
625 |
TInt r = fileCache->ReadBuffered(msgRequest, mode); |
|
626 |
||
627 |
// if this request has been cancelled we mustn't dispatch it again - |
|
628 |
// we still need to call state machine however so that any locked segments can be unlocked |
|
629 |
||
630 |
||
631 |
TInt lastError = msgRequest.LastError(); |
|
632 |
||
633 |
#ifdef _DEBUG |
|
634 |
__ASSERT_DEBUG(!cancelling || msgRequest.LastError() == KErrCancel, Fault(ERequestUncancelled)); |
|
635 |
#endif |
|
636 |
||
637 |
if (lastError == KErrCancel) |
|
638 |
r = CFsRequest::EReqActionComplete; |
|
639 |
||
640 |
return r; |
|
641 |
} |
|
642 |
||
643 |
/** |
|
644 |
ReadBuffered - attempts to read from cache. |
|
645 |
Called from TFsFileRead::Initialise() and CFileCache::CompleteRead() |
|
646 |
||
647 |
@return CFsRequest::EReqActionComplete if request entirely satisfied |
|
648 |
CFsRequest::EReqActionContinue if request not yet complete. |
|
649 |
Results in transition from : |
|
650 |
TFsFileRead::PostInitialise() to TFsFileRead::DoRequestL() or |
|
651 |
CFileCache::CompleteRead() to TFsFileRead::DoRequestL() |
|
652 |
CFsRequest::EReqActionBusy if filecache is "busy" |
|
653 |
*/ |
|
654 |
TInt CFileCache::ReadBuffered(CFsMessageRequest& aMsgRequest, TUint aMode) |
|
655 |
{ |
|
656 |
iLock.Wait(); |
|
657 |
||
658 |
CFsClientMessageRequest* newRequest = NULL; |
|
659 |
||
660 |
__CACHE_PRINT2(_L("CFileCache::ReadBuffered() pos %d, len %d"), I64LOW(aMsgRequest.CurrentOperation().iReadWriteArgs.iPos), aMsgRequest.CurrentOperation().iReadWriteArgs.iTotalLength); |
|
661 |
TInt r = DoReadBuffered(aMsgRequest, aMode, newRequest); |
|
662 |
||
663 |
iLock.Signal(); |
|
664 |
||
665 |
if (newRequest) |
|
666 |
newRequest->Dispatch(); |
|
667 |
||
668 |
return r; |
|
669 |
} |
|
670 |
||
671 |
||
672 |
void CFileCache::UpdateSharePosition(CFsMessageRequest& aMsgRequest, TMsgOperation& aCurrentOperation) |
|
673 |
{ |
|
674 |
// update the file share's position if this request came from client |
|
675 |
if (aCurrentOperation.iClientRequest) |
|
676 |
{ |
|
677 |
CFileShare* share = (CFileShare*)aMsgRequest.ScratchValue(); |
|
678 |
if (aMsgRequest.LastError() == KErrNone) |
|
679 |
{ |
|
680 |
__e32_atomic_store_ord64(&share->iPos, aCurrentOperation.iReadWriteArgs.iPos); |
|
681 |
} |
|
682 |
} |
|
683 |
} |
|
684 |
||
685 |
TInt CFileCache::DoReadBuffered(CFsMessageRequest& aMsgRequest, TUint aMode, CFsClientMessageRequest*& aNewRequest) |
|
686 |
{ |
|
687 |
enum states |
|
688 |
{ |
|
689 |
EStBegin=0, |
|
690 |
EStReadFromCache, |
|
691 |
EStReadFromDiskComplete, |
|
692 |
EStCopyToClient, |
|
693 |
EStReStart, |
|
694 |
EStEnd |
|
695 |
}; |
|
696 |
||
697 |
||
698 |
TInt retCode = CFsRequest::EReqActionComplete; |
|
699 |
TInt& lastError = aMsgRequest.LastError(); |
|
700 |
TBool driveThread = IsDriveThread(); |
|
701 |
||
702 |
// temp storage for transition between EStReadFromCache / EStReadFromDiskComplete and EStCopyToClient |
|
703 |
TInt readLen = 0; |
|
704 |
TUint8* addr = NULL; |
|
705 |
TInt64 segmentStartPos = 0; |
|
706 |
||
707 |
TInt segmentSize = SegmentSize(); |
|
708 |
TInt segmentSizeLog2 = SegmentSizeLog2(); |
|
709 |
TInt64 segmentSizeMask = SegmentSizeMask(); |
|
710 |
||
711 |
for(;;) |
|
712 |
{ |
|
713 |
TMsgOperation* currentOperation = &aMsgRequest.CurrentOperation(); |
|
714 |
TInt64& currentPos = currentOperation->iReadWriteArgs.iPos; |
|
715 |
TInt& totalLen = currentOperation->iReadWriteArgs.iTotalLength; |
|
716 |
TInt& currentOffset = currentOperation->iReadWriteArgs.iOffset; |
|
717 |
TBool readAhead = (currentOperation->iReadWriteArgs.iData == NULL)?(TBool)ETrue:(TBool)EFalse; |
|
718 |
||
719 |
switch(currentOperation->iState) |
|
720 |
{ |
|
721 |
case EStBegin: |
|
722 |
||
723 |
||
724 |
// if EFileReadDirectIO, flush write cache and read direct from disk |
|
725 |
if (!(aMode & EFileReadBuffered)) |
|
726 |
{ |
|
727 |
iLock.Signal(); |
|
728 |
TInt r = FlushDirty(&aMsgRequest); |
|
729 |
iLock.Wait(); |
|
730 |
if (r == CFsRequest::EReqActionBusy) |
|
731 |
return CFsRequest::EReqActionBusy; |
|
732 |
return CFsRequest::EReqActionContinue; // read uncached |
|
733 |
} |
|
734 |
||
735 |
if (currentPos > iSize64) |
|
736 |
currentPos = iSize64; |
|
737 |
||
738 |
currentOperation->iState = EStReadFromCache; |
|
739 |
||
740 |
// count the number of sequential reads for read-ahead |
|
741 |
if (currentOperation->iClientRequest) |
|
742 |
{ |
|
743 |
if (currentPos == iLastReadPos) |
|
744 |
{ |
|
745 |
iSequentialReads++; |
|
746 |
} |
|
747 |
else |
|
748 |
{ |
|
749 |
iSequentialReads = 0; |
|
750 |
ResetReadAhead(); |
|
751 |
} |
|
752 |
iLastReadPos = currentPos + totalLen; |
|
753 |
iLastReadLen = totalLen; |
|
754 |
} |
|
755 |
||
756 |
||
757 |
// fall into... |
|
758 |
||
759 |
case EStReadFromCache: |
|
760 |
{ |
|
761 |
// reading past end of file ? |
|
762 |
if (currentPos + totalLen > iSize64) |
|
763 |
totalLen = (TInt) (iSize64 - currentPos); |
|
764 |
||
765 |
||
766 |
if (totalLen == 0 || lastError != KErrNone) |
|
767 |
{ |
|
768 |
currentOperation->iState = EStEnd; |
|
769 |
break; |
|
770 |
} |
|
771 |
||
772 |
segmentStartPos = currentPos & segmentSizeMask; |
|
773 |
||
774 |
||
775 |
TInt64 endPos = currentPos + totalLen; |
|
776 |
TUint maxLenToRead = (TUint)Min((TInt64)(iCacheClient->CacheLineSize()), (endPos - segmentStartPos)); |
|
777 |
TInt maxSegments = (maxLenToRead + segmentSize - 1) >> segmentSizeLog2; |
|
778 |
||
779 |
||
780 |
TInt segmentCount = maxSegments; |
|
781 |
TInt filledSegmentCount; |
|
782 |
TInt lockError; |
|
783 |
addr = iCacheClient->FindAndLockSegments(segmentStartPos, segmentCount, filledSegmentCount, lockError, EFalse); |
|
784 |
||
785 |
||
786 |
#if defined (_DEBUG_READ_AHEAD) |
|
787 |
if (addr && readAhead) |
|
788 |
RDebug::Print(_L("READAHEAD CACHELINE ALREADY EXISTS POS %d"), I64LOW(segmentStartPos)); |
|
789 |
#endif |
|
790 |
||
791 |
// if cacheline contains filled and empty segments, deal with these seperately |
|
792 |
// to simplify the code..... |
|
793 |
if (filledSegmentCount > 0 && segmentCount > filledSegmentCount) |
|
794 |
{ |
|
795 |
segmentCount = Min(segmentCount, filledSegmentCount); |
|
796 |
} |
|
797 |
||
798 |
if (lockError == KErrInUse) |
|
799 |
{ |
|
800 |
// cacheline in use (by other thread): |
|
801 |
// if this is a read-ahead, abandon it |
|
802 |
// otherwise re-post the request |
|
803 |
if (readAhead) |
|
804 |
{ |
|
805 |
totalLen = 0; |
|
806 |
retCode = CFsRequest::EReqActionComplete; |
|
807 |
currentOperation->iState = EStEnd; |
|
808 |
} |
|
809 |
else |
|
810 |
{ |
|
811 |
#if defined (_DEBUG_READ_AHEAD) |
|
812 |
RDebug::Print(_L("READC CACHELINE BUSY POS %d"), I64LOW(segmentStartPos)); |
|
813 |
#endif |
|
814 |
||
815 |
return CFsRequest::EReqActionBusy; |
|
816 |
} |
|
817 |
break; |
|
818 |
} |
|
819 |
||
820 |
// if not found, try to allocate as many contiguous segments |
|
821 |
// as possible so that the number of reads issued to the media |
|
822 |
// driver is kept to a minumum |
|
823 |
||
824 |
if (addr == NULL) // not found ? |
|
825 |
{ |
|
826 |
// if read len > size of the cache, don't read excess |
|
827 |
// through the cache as this is wasteful |
|
828 |
if (totalLen > iCacheSize) |
|
829 |
{ |
|
830 |
TInt len = totalLen - iCacheSize; |
|
831 |
TInt r = aMsgRequest.PushOperation( |
|
832 |
currentPos, len, |
|
833 |
(TDesC8*) currentOperation->iReadWriteArgs.iData, currentOffset, |
|
834 |
CompleteRead, |
|
835 |
EStReadFromCache); |
|
836 |
if (r != KErrNone) |
|
837 |
{ |
|
838 |
currentOperation->iState = EStReStart; |
|
839 |
break; |
|
840 |
} |
|
841 |
||
842 |
currentOffset+= len; |
|
843 |
currentPos+= len; |
|
844 |
totalLen-= len; |
|
845 |
return CFsRequest::EReqActionContinue; |
|
846 |
} |
|
847 |
||
848 |
// if position not cached postpone Initialise() to drive thread |
|
849 |
// as there may be a read ahead already in the queue |
|
850 |
||
851 |
if (!driveThread && !readAhead) |
|
852 |
{ |
|
853 |
#if defined (_DEBUG_READ_AHEAD) |
|
854 |
RDebug::Print(_L("*** POSTING READ TO DRIVE THREAD POS %d ***"), I64LOW(segmentStartPos)); |
|
855 |
#endif |
|
856 |
return CFsRequest::EReqActionBusy; |
|
857 |
} |
|
858 |
||
859 |
segmentCount = maxSegments; |
|
860 |
addr = iCacheClient->AllocateAndLockSegments(segmentStartPos, segmentCount, EFalse, !readAhead); |
|
861 |
if (addr == NULL) |
|
862 |
{ |
|
863 |
__CACHE_PRINT(_L("AllocateSegment failed")); |
|
864 |
currentOperation->iState = EStReStart; |
|
865 |
break; |
|
866 |
} |
|
867 |
} |
|
868 |
||
869 |
||
870 |
readLen = segmentCount << segmentSizeLog2; |
|
871 |
__ASSERT_DEBUG(iSize64 > segmentStartPos, Fault(EPosBeyondSize)); |
|
872 |
readLen = (TInt)Min((TInt64)readLen, (iSize64 - segmentStartPos)); |
|
873 |
||
874 |
if (iCacheClient->SegmentEmpty(segmentStartPos)) |
|
875 |
{ |
|
876 |
// store readLen & addr in scratch area |
|
877 |
currentOperation->iScratchValue0 = addr; |
|
878 |
currentOperation->iScratchValue1 = (TAny*) readLen; |
|
879 |
||
880 |
// read into cache segment(s) |
|
881 |
__CACHE_PRINT2(_L("CACHEFILE: read pos %ld, readLen %d"), segmentStartPos, readLen); |
|
882 |
TInt r = aMsgRequest.PushOperation( |
|
883 |
segmentStartPos, readLen, addr, 0, |
|
884 |
CompleteRead, |
|
885 |
EStReadFromDiskComplete); |
|
886 |
if (r != KErrNone) |
|
887 |
{ |
|
888 |
iCacheClient->UnlockSegments(segmentStartPos); |
|
889 |
currentOperation->iState = EStReStart; |
|
890 |
break; |
|
891 |
} |
|
892 |
||
893 |
return CFsRequest::EReqActionContinue; |
|
894 |
} |
|
895 |
else |
|
896 |
{ |
|
897 |
currentOperation->iState = EStCopyToClient; |
|
898 |
} |
|
899 |
} |
|
900 |
// fall into ... |
|
901 |
||
902 |
case EStCopyToClient: |
|
903 |
{ |
|
904 |
TInt segmentsLocked = (readLen + segmentSize - 1) >> segmentSizeLog2; |
|
905 |
||
906 |
if (lastError == KErrNone) |
|
907 |
{ |
|
908 |
// copy to user buffer |
|
909 |
TInt offset = iCacheClient->CacheOffset(currentPos); // offset into segment |
|
910 |
TInt len = Min(readLen - offset, totalLen); |
|
911 |
||
912 |
// if addr is NULL then this is a read ahead request |
|
913 |
if (currentOperation->iReadWriteArgs.iData != NULL) |
|
914 |
{ |
|
915 |
if (currentOperation->iClientRequest) |
|
916 |
{ |
|
917 |
__ASSERT_DEBUG (aMsgRequest.Message().Handle() != NULL, Fault(EMsgAlreadyCompleted)); |
|
918 |
TPtrC8 ptr(addr+offset, len); |
|
919 |
lastError = aMsgRequest.Write(0, ptr, currentOffset); |
|
920 |
} |
|
921 |
else |
|
922 |
{ |
|
923 |
memcpy(((TUint8*) currentOperation->iReadWriteArgs.iData) + currentOffset, addr+offset, len); |
|
924 |
} |
|
925 |
} |
|
926 |
else |
|
927 |
{ |
|
928 |
iReadAheadRequest = NULL; |
|
929 |
} |
|
930 |
||
931 |
currentOffset+= len; |
|
932 |
currentPos+= len; |
|
933 |
totalLen-= len; |
|
934 |
} |
|
935 |
||
936 |
if (lastError == KErrNone) |
|
937 |
iCacheClient->MarkSegmentsAsFilled(segmentStartPos, segmentsLocked); |
|
938 |
iCacheClient->UnlockSegments(segmentStartPos); |
|
939 |
||
940 |
if (lastError != KErrNone) |
|
941 |
{ |
|
942 |
retCode = CFsRequest::EReqActionComplete; |
|
943 |
currentOperation->iState = EStEnd; |
|
944 |
break; |
|
945 |
} |
|
946 |
||
947 |
if (totalLen > 0 && lastError == KErrNone) |
|
948 |
{ |
|
949 |
currentOperation->iState = EStReadFromCache; |
|
950 |
break; |
|
951 |
} |
|
952 |
currentOperation->iState = EStEnd; |
|
953 |
} |
|
954 |
// fall into ... |
|
955 |
||
956 |
||
957 |
case EStEnd: |
|
958 |
// update the file share's position if this request came from client |
|
959 |
UpdateSharePosition(aMsgRequest, *currentOperation); |
|
960 |
return retCode; |
|
961 |
||
962 |
case EStReadFromDiskComplete: |
|
963 |
{ |
|
964 |
// restore readLen etc from scratch area |
|
965 |
segmentStartPos = currentPos & segmentSizeMask; |
|
966 |
addr = (TUint8*) currentOperation->iScratchValue0; |
|
967 |
readLen = (TInt) currentOperation->iScratchValue1; |
|
968 |
||
969 |
aMsgRequest.CurrentOperation().iState = EStCopyToClient; |
|
970 |
} |
|
971 |
break; |
|
972 |
||
973 |
case EStReStart: |
|
974 |
||
975 |
// ignore the failure if this is a read-ahead |
|
976 |
if (readAhead) |
|
977 |
{ |
|
978 |
totalLen = 0; |
|
979 |
retCode = CFsRequest::EReqActionComplete; |
|
980 |
currentOperation->iState = EStEnd; |
|
981 |
break; |
|
982 |
} |
|
983 |
||
984 |
// We need to flush all dirty data to disk in case this read overlaps |
|
985 |
// with any dirty data already in the file cache |
|
986 |
if (aMode & EFileWriteBuffered) |
|
987 |
{ |
|
988 |
TInt r = DoFlushDirty(aNewRequest, &aMsgRequest, ETrue); |
|
989 |
if (r == CFsRequest::EReqActionBusy || r != CFsRequest::EReqActionComplete) |
|
990 |
return r; |
|
991 |
} |
|
992 |
||
993 |
retCode = CFsRequest::EReqActionContinue; // read uncached |
|
994 |
currentOperation->iState = EStEnd; |
|
995 |
||
996 |
/* |
|
997 |
We're now going to by-pass the file cache. |
|
998 |
If we've already written something to the client's buffer then, in the flexible |
|
999 |
memory model, the KDesWrittenShift bit will be set and so the descriptor length will |
|
1000 |
updated in RMessageK::CallbackFunc() when the client thread runs. This (shorter) |
|
1001 |
length will overwrite the descriptor length written by the local media subsystem. |
|
1002 |
To get round this problem, we set the descriptor length artificially by writing a |
|
1003 |
zero-length descriptor at the end of the client's buffer. |
|
1004 |
*/ |
|
1005 |
if (currentOffset > 0 && currentOperation->iClientRequest) |
|
1006 |
{ |
|
1007 |
__ASSERT_DEBUG (aMsgRequest.Message().Handle() != NULL, Fault(EMsgAlreadyCompleted)); |
|
1008 |
TPtrC8 ptr(NULL, 0); |
|
1009 |
TInt r = aMsgRequest.Write(0, ptr, currentOffset+totalLen); |
|
1010 |
__CACHE_PRINT3(_L("CFileCache::DoReadBuffered() failed at pos %lx offset %x totalLen %x\n"), currentPos, currentOffset, totalLen); |
|
1011 |
__CACHE_PRINT2(_L("CFileCache::DoReadBuffered() writing zero bytes at offset %x r %d\n"), currentOffset + totalLen, r); |
|
1012 |
if (r != KErrNone) |
|
1013 |
retCode = r; |
|
1014 |
} |
|
1015 |
||
1016 |
aMsgRequest.ReStart(); |
|
1017 |
UpdateSharePosition(aMsgRequest, *currentOperation); |
|
1018 |
return retCode; |
|
1019 |
||
1020 |
||
1021 |
}; |
|
1022 |
} // for (;;) |
|
1023 |
||
1024 |
} |
|
1025 |
||
1026 |
||
1027 |
||
1028 |
/** |
|
1029 |
WriteBuffered - attempts to write to cache. |
|
1030 |
Called from TFsFileRead::Initialise and TFsFileRead::DoRequestL |
|
1031 |
||
1032 |
@return CFsRequest::EReqActionComplete if request entirely satisfied |
|
1033 |
CFsRequest::EReqActionContinue if request not yet complete |
|
1034 |
CFsRequest::EReqActionBusy if filecache is busy |
|
1035 |
*/ |
|
1036 |
TInt CFileCache::WriteBuffered(CFsMessageRequest& aMsgRequest, TUint aMode) |
|
1037 |
{ |
|
1038 |
iLock.Wait(); |
|
1039 |
||
1040 |
||
1041 |
CFsClientMessageRequest* newRequest = NULL; |
|
1042 |
||
1043 |
__CACHE_PRINT2(_L("CFileCache::WriteBuffered() pos %d, len %d"), I64LOW(aMsgRequest.CurrentOperation().iReadWriteArgs.iPos), aMsgRequest.CurrentOperation().iReadWriteArgs.iTotalLength); |
|
1044 |
TInt r = DoWriteBuffered(aMsgRequest, newRequest, aMode); |
|
1045 |
||
1046 |
iLock.Signal(); |
|
1047 |
||
1048 |
if (newRequest) |
|
1049 |
newRequest->Dispatch(); |
|
1050 |
||
1051 |
// completion ? |
|
1052 |
if (r == CFsRequest::EReqActionComplete) |
|
1053 |
{ |
|
1054 |
TMsgOperation& currentOperation = aMsgRequest.CurrentOperation(); |
|
1055 |
||
1056 |
__ASSERT_DEBUG(aMsgRequest.CurrentOperationPtr() != NULL, Fault(ECacheBadOperationIndex)); |
|
1057 |
||
1058 |
TFsFileWrite::CommonEnd(&aMsgRequest, r, iInitialSize, iSize64, currentOperation.iReadWriteArgs.iPos, EFalse); |
|
1059 |
} |
|
1060 |
||
1061 |
return r; |
|
1062 |
} |
|
1063 |
||
1064 |
||
1065 |
||
1066 |
||
1067 |
TInt CFileCache::CompleteWrite(CFsRequest* aRequest) |
|
1068 |
{ |
|
1069 |
CFsMessageRequest& msgRequest = *(CFsMessageRequest*) aRequest; |
|
1070 |
||
1071 |
CFileShare* share = (CFileShare*)aRequest->ScratchValue(); |
|
1072 |
CFileCB& file = share->File(); |
|
1073 |
CFileCache* fileCache = file.FileCache(); //&file; |
|
1074 |
__ASSERT_DEBUG(fileCache != NULL, Fault(ENoFileCache)); |
|
1075 |
||
1076 |
#ifdef _DEBUG |
|
1077 |
TInt cancelling = (msgRequest.LastError() == KErrCancel)?(TBool)ETrue:(TBool)EFalse; |
|
1078 |
#endif |
|
1079 |
||
1080 |
||
1081 |
TInt r = fileCache->WriteBuffered(msgRequest, share->iMode); |
|
1082 |
||
1083 |
#ifdef _DEBUG |
|
1084 |
__ASSERT_DEBUG(!cancelling || msgRequest.LastError() == KErrCancel, Fault(ERequestUncancelled)); |
|
1085 |
#endif |
|
1086 |
||
1087 |
// if this request has been cancelled we mustn't dispatch it again - |
|
1088 |
// we still need to call state machine however so that any locked segments can be unlocked |
|
1089 |
TInt lastError = msgRequest.LastError(); |
|
1090 |
if (lastError == KErrCancel) |
|
1091 |
r = CFsRequest::EReqActionComplete; |
|
1092 |
||
1093 |
return r; |
|
1094 |
} |
|
1095 |
||
1096 |
||
1097 |
||
1098 |
TInt CFileCache::DoWriteBuffered(CFsMessageRequest& aMsgRequest, CFsClientMessageRequest*& aNewRequest, TUint aMode) |
|
1099 |
||
1100 |
{ |
|
1101 |
enum states |
|
1102 |
{ |
|
1103 |
EStBegin=0, |
|
1104 |
EStWriteToCache, |
|
1105 |
EStReadFromDisk, |
|
1106 |
EStReadFromDiskComplete, |
|
1107 |
EStWriteToCacheComplete, |
|
1108 |
EStCopyFromClient, |
|
1109 |
EStReStart, |
|
1110 |
EStEnd, |
|
1111 |
EStWriteThrough |
|
1112 |
}; |
|
1113 |
||
1114 |
enum flags |
|
1115 |
{ |
|
1116 |
EReadFirstSegment = 0x01, |
|
1117 |
EReadLastSegment = 0x02 |
|
1118 |
}; |
|
1119 |
||
1120 |
TBool cachingWrites = (aMode & EFileWriteBuffered)?(TBool)ETrue:(TBool)EFalse; |
|
1121 |
TInt& lastError = aMsgRequest.LastError(); |
|
1122 |
TBool driveThread = IsDriveThread(); |
|
1123 |
TInt segmentSize = SegmentSize(); |
|
1124 |
TInt segmentSizeLog2 = SegmentSizeLog2(); |
|
1125 |
TInt64 segmentSizeMask = SegmentSizeMask(); |
|
1126 |
||
1127 |
// temp storage for transition between EStWriteToCache / EStWriteToCacheComplete and EStCopyFromClient |
|
1128 |
TInt segmentCount = 0; |
|
1129 |
TUint8* addr = NULL; |
|
1130 |
TInt64 firstSegmentStartPos = 0; |
|
1131 |
TInt64 readPos = 0; |
|
1132 |
TUint8* readAddr = NULL; |
|
1133 |
TInt readSegmentCount = 0; |
|
1134 |
||
1135 |
TInt readFlags = 0; |
|
1136 |
||
1137 |
for(;;) |
|
1138 |
{ |
|
1139 |
TMsgOperation* currentOperation = &aMsgRequest.CurrentOperation(); |
|
1140 |
TInt retCode = CFsRequest::EReqActionComplete; |
|
1141 |
||
1142 |
TInt64& currentPos = currentOperation->iReadWriteArgs.iPos; |
|
1143 |
TInt& totalLen = currentOperation->iReadWriteArgs.iTotalLength; |
|
1144 |
TInt& currentOffset = currentOperation->iReadWriteArgs.iOffset; |
|
1145 |
switch(currentOperation->iState) |
|
1146 |
{ |
|
1147 |
case EStBegin: |
|
1148 |
{ |
|
1149 |
// Report background flush errors back to client |
|
1150 |
CFileShare* share = (CFileShare*) aMsgRequest.ScratchValue(); |
|
1151 |
if (share->iFlushError != KErrNone) |
|
1152 |
{ |
|
1153 |
TInt r = share->iFlushError; |
|
1154 |
share->iFlushError = KErrNone; |
|
1155 |
return r; |
|
1156 |
} |
|
1157 |
||
1158 |
if (currentPos > iSize64) |
|
1159 |
currentPos = iSize64; |
|
1160 |
iInitialSize = iSize64; |
|
1161 |
||
1162 |
// if EFileWriteDirectIO OR |
|
1163 |
// (caching writes and requested write len > size of the cache), |
|
1164 |
// flush the write cache |
|
1165 |
if ((aMode & EFileWriteBuffered) == 0 || |
|
1166 |
(cachingWrites && totalLen > iCacheSize)) |
|
1167 |
{ |
|
1168 |
TInt r = DoFlushDirty(aNewRequest, &aMsgRequest, ETrue); |
|
1169 |
if (r == CFsRequest::EReqActionBusy || r != CFsRequest::EReqActionComplete) |
|
1170 |
return r; |
|
1171 |
} |
|
1172 |
// if EFileWriteDirectIO then overwrite any non-dirty data and |
|
1173 |
// write direct to disk.... |
|
1174 |
||
1175 |
// if caching writes and requested write len > size of the cache, |
|
1176 |
// don't write excess through the cache as this is wasteful |
|
1177 |
if (cachingWrites && totalLen > iCacheSize) |
|
1178 |
{ |
|
1179 |
// Destroy ALL cached data (dirty and non-dirty) to ensure |
|
1180 |
// cache is consistent with data written to disk |
|
1181 |
iCacheClient->Purge(ETrue); |
|
1182 |
||
1183 |
TInt len = totalLen - iCacheSize; |
|
1184 |
TInt r = aMsgRequest.PushOperation( |
|
1185 |
currentPos, len, |
|
1186 |
(TDesC8*) currentOperation->iReadWriteArgs.iData, currentOffset, |
|
1187 |
CompleteWrite, |
|
1188 |
EStWriteToCache); |
|
1189 |
if (r != KErrNone) |
|
1190 |
{ |
|
1191 |
currentOperation->iState = EStReStart; |
|
1192 |
break; |
|
1193 |
} |
|
1194 |
||
1195 |
currentOffset+= len; |
|
1196 |
currentPos+= len; |
|
1197 |
totalLen-= len; |
|
1198 |
return CFsRequest::EReqActionContinue; |
|
1199 |
} |
|
1200 |
||
1201 |
currentOperation->iState = EStWriteToCache; |
|
1202 |
} |
|
1203 |
break; |
|
1204 |
||
1205 |
case EStWriteToCache: |
|
1206 |
__ASSERT_DEBUG(aMsgRequest.CurrentOperationPtr() != NULL, Fault(ECacheBadOperationIndex)); |
|
1207 |
{ |
|
1208 |
// NB we must carry on if we get an error to ensure cache is consistent with disk |
|
1209 |
if (totalLen == 0 || lastError == KErrCancel) |
|
1210 |
{ |
|
1211 |
currentOperation->iState = EStWriteToCacheComplete; |
|
1212 |
break; |
|
1213 |
} |
|
1214 |
||
1215 |
firstSegmentStartPos = currentPos & segmentSizeMask; |
|
1216 |
TInt64 endPos = currentPos + totalLen; |
|
1217 |
||
1218 |
// Find the maximum number of contiguous segments we need to lock |
|
1219 |
// in this cacheline - when we unlock these will be marked as filled |
|
1220 |
const TInt64 dataRange = Min((TInt64)iCacheSize, (endPos + segmentSize - 1 - firstSegmentStartPos)); |
|
1221 |
TInt maxSegmentCount = (TInt)(dataRange >> segmentSizeLog2); |
|
1222 |
||
1223 |
segmentCount = maxSegmentCount; |
|
1224 |
||
1225 |
TInt lockError; |
|
1226 |
TInt filledSegmentCount; |
|
1227 |
addr = iCacheClient->FindAndLockSegments(firstSegmentStartPos, segmentCount, filledSegmentCount, lockError, cachingWrites); |
|
1228 |
||
1229 |
if (lockError == KErrInUse) |
|
1230 |
return CFsRequest::EReqActionBusy; |
|
1231 |
||
1232 |
#ifdef LAZY_WRITE |
|
1233 |
if (cachingWrites && addr == NULL && lastError == KErrNone && iCacheClient->TooManyLockedSegments()) |
|
1234 |
{ |
|
1235 |
// Flush a single dirty cacheline (if there is one for this file) |
|
1236 |
// or all dirty data on this drive (if there is any). |
|
1237 |
// If there's nothing to flush then the dirty (locked) data |
|
1238 |
// must belong to another drive, so there's not much we can do |
|
1239 |
// but write through |
|
1240 |
TInt r = DoFlushDirty(aNewRequest, &aMsgRequest, EFalse); |
|
1241 |
if (r == CFsRequest::EReqActionBusy) |
|
1242 |
return CFsRequest::EReqActionBusy; |
|
1243 |
if (r != KErrInUse) |
|
1244 |
{ |
|
1245 |
// Need to switch to drive thread to flush all data on this drive |
|
1246 |
if (!driveThread) |
|
1247 |
return CFsRequest::EReqActionBusy; |
|
1248 |
iLock.Signal(); |
|
1249 |
TInt r = iDrive->FlushCachedFileInfo(); |
|
1250 |
iLock.Wait(); |
|
1251 |
if (r == CFsRequest::EReqActionBusy) |
|
1252 |
return CFsRequest::EReqActionBusy; |
|
1253 |
||
1254 |
lastError = KErrNoMemory; // write through |
|
1255 |
} |
|
1256 |
} |
|
1257 |
#else |
|
1258 |
// flush cache before allocating a new cacheline |
|
1259 |
if (cachingWrites && addr == NULL && lastError == KErrNone) |
|
1260 |
{ |
|
1261 |
// if no segment available, flush a single dirty cacheline |
|
1262 |
// or wait if already flushing |
|
1263 |
if (iFlushBusy) |
|
1264 |
return CFsRequest::EReqActionBusy; |
|
1265 |
TInt r = DoFlushDirty(aNewRequest, &aMsgRequest, EFalse); |
|
1266 |
if (r != CFsRequest::EReqActionBusy && r != CFsRequest::EReqActionComplete) |
|
1267 |
lastError = r; |
|
1268 |
} |
|
1269 |
#endif |
|
1270 |
// if no cacheline found & write caching is enabled, allocate a new cacheline |
|
1271 |
if (cachingWrites && addr == NULL && lastError == KErrNone) |
|
1272 |
{ |
|
1273 |
// try to allocate up to write cache size - this may not |
|
1274 |
// be possible if a segment in the range is already cached |
|
1275 |
segmentCount = maxSegmentCount; |
|
1276 |
addr = iCacheClient->AllocateAndLockSegments(currentPos, segmentCount, cachingWrites, ETrue); |
|
1277 |
||
1278 |
// continue if alloc failed |
|
1279 |
if (addr == NULL) |
|
1280 |
lastError = KErrNoMemory; |
|
1281 |
||
1282 |
} |
|
1283 |
||
1284 |
if (addr == NULL) |
|
1285 |
{ |
|
1286 |
if (cachingWrites && lastError == KErrNone) |
|
1287 |
lastError = KErrNoMemory; |
|
1288 |
segmentCount = 1; |
|
1289 |
currentOperation->iState = EStCopyFromClient; |
|
1290 |
break; |
|
1291 |
} |
|
1292 |
||
1293 |
// if the first or last segment are empty then we'll have to read-before-write |
|
1294 |
TInt64 lastSegmentStartPos = firstSegmentStartPos + ((segmentCount-1) << segmentSizeLog2); |
|
1295 |
||
1296 |
TInt startPosSegOffset = iCacheClient->CacheOffset(currentPos); |
|
1297 |
TInt endPosSegOffset = iCacheClient->CacheOffset(endPos); |
|
1298 |
||
1299 |
// partial first segment ? |
|
1300 |
if (startPosSegOffset != 0 && |
|
1301 |
firstSegmentStartPos < iFileCB->Size64() && |
|
1302 |
iCacheClient->SegmentEmpty(firstSegmentStartPos)) |
|
1303 |
{ |
|
1304 |
readFlags|= EReadFirstSegment; |
|
1305 |
} |
|
1306 |
||
1307 |
// partial last segment ? |
|
1308 |
// NB this may be the first segment too ! |
|
1309 |
if (endPosSegOffset != 0 && |
|
1310 |
endPos < lastSegmentStartPos + segmentSize && |
|
1311 |
endPos < iFileCB->Size64() && |
|
1312 |
iCacheClient->SegmentEmpty(lastSegmentStartPos)) |
|
1313 |
{ |
|
1314 |
readFlags|= EReadLastSegment; |
|
1315 |
} |
|
1316 |
||
1317 |
// read-before-write required ? |
|
1318 |
if (readFlags & EReadFirstSegment) |
|
1319 |
{ |
|
1320 |
readFlags&= ~EReadFirstSegment; |
|
1321 |
readPos = firstSegmentStartPos; |
|
1322 |
readAddr = addr; |
|
1323 |
readSegmentCount = 1; |
|
1324 |
// if the last segment is empty and it's the same as the first or contiguous, |
|
1325 |
// then read that too |
|
1326 |
if ((readFlags & EReadLastSegment) && (segmentCount <= 2)) |
|
1327 |
{ |
|
1328 |
readSegmentCount = segmentCount; |
|
1329 |
readFlags&= ~EReadLastSegment; |
|
1330 |
} |
|
1331 |
currentOperation->iState = EStReadFromDisk; |
|
1332 |
} |
|
1333 |
else if (readFlags & EReadLastSegment) |
|
1334 |
{ |
|
1335 |
readFlags&= ~EReadLastSegment; |
|
1336 |
readPos = lastSegmentStartPos; |
|
1337 |
readAddr = addr + ((segmentCount-1) << segmentSizeLog2); |
|
1338 |
readSegmentCount = 1; |
|
1339 |
currentOperation->iState = EStReadFromDisk; |
|
1340 |
} |
|
1341 |
else |
|
1342 |
{ |
|
1343 |
currentOperation->iState = EStCopyFromClient; |
|
1344 |
} |
|
1345 |
} |
|
1346 |
break; |
|
1347 |
||
1348 |
case EStReadFromDisk: |
|
1349 |
{ |
|
1350 |
// Save address & segmentCount in scratch area |
|
1351 |
currentOperation->iScratchValue0 = addr; |
|
1352 |
__ASSERT_DEBUG(segmentCount < 0xFFFF, Fault(EBadSegmentCount)); |
|
1353 |
currentOperation->iScratchValue1 = (TAny*) ((readFlags << 16) | segmentCount); |
|
1354 |
||
1355 |
TInt maxReadLen = readSegmentCount << segmentSizeLog2; |
|
1356 |
#ifdef __VC32__ |
|
1357 |
#pragma warning( disable : 4244 ) |
|
1358 |
//Disable Compiler Warning (levels 3 and 4) C4244: 'conversion' conversion from 'type1' to 'type2', possible loss of data |
|
1359 |
// maxReadLen is of 31 bits. Hence Min result is reduced to 31 bits |
|
1360 |
#endif |
|
1361 |
TInt readLen = (TInt)Min((TInt64)maxReadLen, (iSize64 - readPos)); |
|
1362 |
#ifdef __VC32__ |
|
1363 |
#pragma warning( default : 4244 ) |
|
1364 |
#endif |
|
1365 |
__ASSERT_DEBUG (aMsgRequest.Message().Handle() != NULL, Fault(EMsgAlreadyCompleted)); |
|
1366 |
TInt r = aMsgRequest.PushOperation( |
|
1367 |
readPos, readLen, readAddr, 0, |
|
1368 |
CompleteWrite, |
|
1369 |
EStReadFromDiskComplete, |
|
1370 |
EFsFileRead); |
|
1371 |
if (r != KErrNone) |
|
1372 |
{ |
|
1373 |
lastError = KErrNoMemory; |
|
1374 |
currentOperation->iState = EStEnd; |
|
1375 |
iCacheClient->UnlockSegments(firstSegmentStartPos); |
|
1376 |
break; |
|
1377 |
} |
|
1378 |
||
1379 |
// requeue this request |
|
1380 |
return CFsRequest::EReqActionContinue; |
|
1381 |
} |
|
1382 |
||
1383 |
case EStReadFromDiskComplete: |
|
1384 |
{ |
|
1385 |
__ASSERT_DEBUG(aMsgRequest.CurrentOperationPtr() != NULL, Fault(ECacheBadOperationIndex)); |
|
1386 |
// restore addr & segmentCount etc from scratch area |
|
1387 |
firstSegmentStartPos = currentPos & segmentSizeMask; |
|
1388 |
addr = (TUint8*) currentOperation->iScratchValue0; |
|
1389 |
segmentCount = ((TInt) currentOperation->iScratchValue1) & 0xFFFF; |
|
1390 |
readFlags = ((TInt) currentOperation->iScratchValue1) >> 16; |
|
1391 |
||
1392 |
if (readFlags & EReadLastSegment) |
|
1393 |
{ |
|
1394 |
TInt64 lastSegmentStartPos = firstSegmentStartPos + ((segmentCount-1) << segmentSizeLog2); |
|
1395 |
readFlags&= ~EReadLastSegment; |
|
1396 |
readPos = lastSegmentStartPos; |
|
1397 |
readAddr = addr + ((segmentCount-1) << segmentSizeLog2); |
|
1398 |
readSegmentCount = 1; |
|
1399 |
currentOperation->iState = EStReadFromDisk; |
|
1400 |
break; |
|
1401 |
} |
|
1402 |
||
1403 |
aMsgRequest.CurrentOperation().iState = EStCopyFromClient; |
|
1404 |
} |
|
1405 |
break; |
|
1406 |
||
1407 |
case EStCopyFromClient: |
|
1408 |
{ |
|
1409 |
TInt writeLen = segmentCount << segmentSizeLog2; |
|
1410 |
TInt offset = iCacheClient->CacheOffset(currentPos); // offset into segment |
|
1411 |
writeLen = Min(writeLen - offset, totalLen); |
|
1412 |
||
1413 |
if (addr != NULL) |
|
1414 |
{ |
|
1415 |
__ASSERT_DEBUG (aMsgRequest.Message().Handle() != NULL, Fault(EMsgAlreadyCompleted)); |
|
1416 |
// copy from user buffer to cache buffer |
|
1417 |
TInt writeError = KErrNone; |
|
1418 |
if (currentOperation->iClientRequest) |
|
1419 |
{ |
|
1420 |
TPtr8 ptr(addr+offset, writeLen, writeLen); |
|
1421 |
writeError = aMsgRequest.Read(0, ptr, currentOffset); |
|
1422 |
} |
|
1423 |
else |
|
1424 |
{ |
|
1425 |
memcpy(addr+offset, ((TUint8*) currentOperation->iReadWriteArgs.iData) + currentOffset, writeLen); |
|
1426 |
} |
|
1427 |
||
1428 |
// "writing" past end of file ? |
|
1429 |
if (currentPos + writeLen > iSize64 && cachingWrites && lastError == KErrNone && writeError == KErrNone) |
|
1430 |
{ |
|
1431 |
__CACHE_PRINT2(_L("CACHEFILE: extending size old %ld new %ld"), iSize64, currentPos + totalLen); |
|
1432 |
iSize64 = currentPos + writeLen; |
|
1433 |
} |
|
1434 |
||
1435 |
TInt anyError = (writeError != KErrNone)?writeError:lastError; |
|
1436 |
||
1437 |
if (anyError == KErrNone) |
|
1438 |
iCacheClient->MarkSegmentsAsFilled(firstSegmentStartPos, segmentCount); |
|
1439 |
||
1440 |
if (cachingWrites && anyError == KErrNone) |
|
1441 |
{ |
|
1442 |
iCacheClient->MarkSegmentsAsDirty(firstSegmentStartPos, segmentCount); |
|
1443 |
// start dirty data timer |
|
1444 |
FileDirty(aMsgRequest); |
|
1445 |
} |
|
1446 |
||
1447 |
// unlock if we're not buffering writes (segments won't be unlocked if dirty) |
|
1448 |
iCacheClient->UnlockSegments(firstSegmentStartPos); |
|
1449 |
} |
|
1450 |
||
1451 |
currentOffset+= writeLen; |
|
1452 |
currentPos+= writeLen; |
|
1453 |
totalLen-= writeLen; |
|
1454 |
||
1455 |
currentOperation->iState = EStWriteToCache; |
|
1456 |
break; |
|
1457 |
} |
|
1458 |
||
1459 |
case EStWriteToCacheComplete: |
|
1460 |
{ |
|
1461 |
__ASSERT_DEBUG(aMsgRequest.CurrentOperationPtr() != NULL, Fault(ECacheBadOperationIndex)); |
|
1462 |
||
1463 |
if (lastError == KErrCancel) |
|
1464 |
{ |
|
1465 |
currentOperation->iState = EStEnd; |
|
1466 |
} |
|
1467 |
else if ((!cachingWrites) || (lastError != KErrNone)) |
|
1468 |
{ |
|
1469 |
// allow TFsFileWrite::DoRequestL() to proceed normally using original pos & len |
|
1470 |
__ASSERT_DEBUG(aMsgRequest.CurrentOperationPtr() != NULL, Fault(ECacheBadOperationIndex)); |
|
1471 |
||
1472 |
currentOperation->iState = EStWriteThrough; |
|
1473 |
} |
|
1474 |
else |
|
1475 |
{ |
|
1476 |
__CACHE_PRINT2(_L("CACHEFILE: write buffered pos %ld, len %d"), |
|
1477 |
aMsgRequest.CurrentOperation().iReadWriteArgs.iPos, aMsgRequest.CurrentOperation().iReadWriteArgs.iOffset); |
|
1478 |
__ASSERT_DEBUG(totalLen == 0, Fault(ECompletingWriteWithDataRemaining)); |
|
1479 |
||
1480 |
currentOperation->iState = EStEnd; |
|
1481 |
} |
|
1482 |
break; |
|
1483 |
} |
|
1484 |
||
1485 |
case EStWriteThrough: |
|
1486 |
||
1487 |
if (lastError == KErrCancel) |
|
1488 |
{ |
|
1489 |
currentOperation->iState = EStEnd; |
|
1490 |
break; |
|
1491 |
} |
|
1492 |
||
1493 |
// we're going to issue an uncached write so clear any error |
|
1494 |
lastError = KErrNone; |
|
1495 |
||
1496 |
if (cachingWrites) |
|
1497 |
{ |
|
1498 |
TInt r = DoFlushDirty(aNewRequest, &aMsgRequest, ETrue); |
|
1499 |
#ifdef _DEBUG |
|
1500 |
if (r == CFsRequest::EReqActionBusy) |
|
1501 |
CCacheManagerFactory::CacheManager()->Stats().iWriteThroughWithDirtyDataCount++; |
|
1502 |
#endif |
|
1503 |
// Need to reset currentOperation.iReadWriteArgs.iTotalLength here to make sure |
|
1504 |
// TFsFileWrite::PostInitialise() doesn't think there's no data left to process |
|
1505 |
aMsgRequest.ReStart(); |
|
110
c734af59ce98
Revision: 201019
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
1506 |
|
c734af59ce98
Revision: 201019
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
1507 |
//Need to preserve the current state otherwise if we are over the ram threshold |
c734af59ce98
Revision: 201019
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
1508 |
//the request can end up in a livelock trying to repeatedly flush. |
c734af59ce98
Revision: 201019
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
1509 |
currentOperation->iState = EStWriteThrough; |
c734af59ce98
Revision: 201019
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
1510 |
|
0 | 1511 |
if (r == CFsRequest::EReqActionBusy || r != CFsRequest::EReqActionComplete) |
1512 |
return r; |
|
1513 |
} |
|
1514 |
||
1515 |
retCode = CFsRequest::EReqActionContinue; |
|
1516 |
||
1517 |
// fall into...EStRestart |
|
1518 |
currentOperation->iState = EStReStart; |
|
1519 |
||
1520 |
case EStReStart: |
|
1521 |
||
1522 |
__ASSERT_DEBUG(retCode == CFsRequest::EReqActionBusy || retCode == CFsRequest::EReqActionContinue, Fault(EBadRetCode)); |
|
1523 |
||
1524 |
aMsgRequest.ReStart(); |
|
1525 |
UpdateSharePosition(aMsgRequest, *currentOperation); |
|
1526 |
if (currentOperation->iClientRequest) |
|
1527 |
currentOperation->iReadWriteArgs.iPos = currentOperation->iClientPosition; // NB maybe KCurrentPosition64 |
|
1528 |
return retCode; |
|
1529 |
||
1530 |
case EStEnd: |
|
1531 |
return retCode; |
|
1532 |
} |
|
1533 |
} |
|
1534 |
} |
|
1535 |
||
1536 |
||
1537 |
||
1538 |
TInt CFileCache::AllocateRequest(CFsClientMessageRequest*& aNewRequest, TBool aWrite, CSessionFs* aSession) |
|
1539 |
{ |
|
1540 |
||
1541 |
RLocalMessage msgNew; |
|
1542 |
const TOperation& oP = OperationArray[aWrite?EFsFileWriteDirty:EFsFileRead]; |
|
1543 |
TInt r = RequestAllocator::GetMessageRequest(oP, msgNew, aNewRequest); |
|
1544 |
if (r != KErrNone) |
|
1545 |
return r; |
|
1546 |
||
1547 |
aNewRequest->Set(msgNew, oP, aSession); |
|
1548 |
aNewRequest->SetDrive(iDrive); |
|
1549 |
||
1550 |
// read-aheads and write-dirty requests should not be posted to plugins |
|
1551 |
// If there are data-modifying plugins, then these should sit above the file cache |
|
1552 |
aNewRequest->iCurrentPlugin = NULL; |
|
1553 |
aNewRequest->EnablePostIntercept(EFalse); |
|
1554 |
||
1555 |
// Scratch value is a CFileCB pointer NOT a CFileShare for requests |
|
1556 |
// allocated by the file cache |
|
1557 |
aNewRequest->SetScratchValue64( MAKE_TINT64(EFalse, (TUint) iFileCB) ); |
|
1558 |
||
1559 |
// don't call Initialise(), don't call PostInitialise() |
|
1560 |
aNewRequest->SetState(CFsRequest::EReqStateDoRequest); |
|
1561 |
||
1562 |
// don't call Message().Complete() |
|
1563 |
aNewRequest->SetCompleted(EFalse); |
|
1564 |
||
1565 |
__ASSERT_DEBUG(aNewRequest->CurrentOperationPtr() == NULL, Fault(EBadOperationIndex)); |
|
1566 |
||
1567 |
return KErrNone; |
|
1568 |
} |
|
1569 |
||
1570 |
TInt CFileCache::FlushDirty(CFsRequest* aOldRequest) |
|
1571 |
{ |
|
1572 |
iLock.Wait(); |
|
1573 |
||
1574 |
CFsClientMessageRequest* newRequest = NULL; |
|
1575 |
||
1576 |
TInt r = DoFlushDirty(newRequest, aOldRequest, ETrue); |
|
1577 |
||
1578 |
iLock.Signal(); |
|
1579 |
||
1580 |
if (newRequest) |
|
1581 |
{ |
|
1582 |
//To be used in notification framework. |
|
1583 |
//newRequest->iUID = aOldRequest->Message().Identity(); |
|
1584 |
newRequest->Dispatch(); |
|
1585 |
} |
|
1586 |
||
1587 |
return r; |
|
1588 |
} |
|
1589 |
||
1590 |
||
1591 |
void CFileCache::Purge(TBool aPurgeDirty) |
|
1592 |
{ |
|
1593 |
iLock.Wait(); |
|
1594 |
||
1595 |
iCacheClient->Purge(aPurgeDirty); |
|
1596 |
||
1597 |
iLock.Signal(); |
|
1598 |
} |
|
1599 |
||
1600 |
void CFileCache::PropagateFlushErrorToAllFileShares() |
|
1601 |
{ |
|
1602 |
FileShares->Lock(); |
|
1603 |
TInt count = FileShares->Count(); |
|
1604 |
while(count--) |
|
1605 |
{ |
|
1606 |
CFileShare* share = (CFileShare*)(*FileShares)[count]; |
|
1607 |
if (&share->File() == iFileCB) |
|
1608 |
{ |
|
1609 |
share->iFlushError = iFlushError; |
|
1610 |
} |
|
1611 |
} |
|
1612 |
FileShares->Unlock(); |
|
1613 |
} |
|
1614 |
||
1615 |
/** |
|
1616 |
CFileCache::DoFlushDirty() |
|
1617 |
||
1618 |
@param aFlushAll. If EFalse then only a single cacheline will be flushed |
|
1619 |
||
1620 |
returns CFsRequest::EReqActionBusy if a flush is in progress OR cacheline already in use |
|
1621 |
CFsRequest::EReqActionComplete if nothing to flush / flush completed |
|
1622 |
KErrNoMemory if failed to allocate a request |
|
1623 |
or one of the system wide error codes |
|
1624 |
||
1625 |
*/ |
|
1626 |
TInt CFileCache::DoFlushDirty(CFsClientMessageRequest*& aNewRequest, CFsRequest* aOldRequest, TBool aFlushAll) |
|
1627 |
{ |
|
1628 |
TInt64 pos; |
|
1629 |
TUint8* addr; |
|
1630 |
||
1631 |
if (iFlushBusy) |
|
1632 |
return CFsRequest::EReqActionBusy; |
|
1633 |
||
1634 |
TInt segmentCount = iCacheClient->FindFirstDirtySegment(pos, addr); |
|
1635 |
||
1636 |
if (segmentCount == 0) |
|
1637 |
{ |
|
1638 |
TInt flushError = iFlushError; |
|
1639 |
iFlushError = KErrNone; |
|
1640 |
||
1641 |
// If this flush didn't originate from a client request return last error |
|
1642 |
if (!aOldRequest || !aOldRequest->Session()) |
|
1643 |
return (flushError == KErrNone)? CFsRequest::EReqActionComplete : flushError; |
|
1644 |
||
1645 |
// Return the last error from CFileShare::iFlushError |
|
1646 |
// and then clear CFileShare::iFlushError |
|
1647 |
||
1648 |
CFileShare* share = (CFileShare*) SessionObjectFromHandle( |
|
1649 |
aOldRequest->Message().Int3(), |
|
1650 |
FileShares->UniqueID(), |
|
1651 |
aOldRequest->Session()); |
|
1652 |
||
1653 |
TInt r = KErrNone; |
|
1654 |
if (share) |
|
1655 |
{ |
|
1656 |
r = share->iFlushError; |
|
1657 |
share->iFlushError = KErrNone; |
|
1658 |
} |
|
1659 |
if (r == KErrNone) |
|
1660 |
return CFsRequest::EReqActionComplete; |
|
1661 |
||
1662 |
return r; |
|
1663 |
} |
|
1664 |
||
1665 |
||
1666 |
// NB aOldRequest->Session may be NULL - e.g for FileShareCloseOp |
|
1667 |
CSessionFs* session = aOldRequest && aOldRequest->Session() ? aOldRequest->Session() : iDirtyDataOwner; |
|
1668 |
||
1669 |
__ASSERT_ALWAYS(session, Fault(EFlushingWithSessionNull)); |
|
1670 |
||
1671 |
TInt r = AllocateRequest(aNewRequest, ETrue, session); |
|
1672 |
if (r != KErrNone) |
|
1673 |
return r; |
|
1674 |
||
1675 |
r = aNewRequest->PushOperation(0, 0, (TUint8*) NULL, 0); |
|
1676 |
||
1677 |
if (r != KErrNone) |
|
1678 |
{ |
|
1679 |
aNewRequest->Free(); |
|
1680 |
aNewRequest = NULL; |
|
1681 |
return r; |
|
1682 |
} |
|
1683 |
||
1684 |
// set the number of segments to flush - either all dirty data or the equivalent of one full cacheline |
|
1685 |
aNewRequest->CurrentOperation().iScratchValue0 = |
|
1686 |
(TAny*) (aFlushAll?KMaxTInt:iCacheClient->CacheLineSizeInSegments()); |
|
1687 |
||
1688 |
||
1689 |
// issue flush request |
|
1690 |
r = FlushDirtySm(*aNewRequest); |
|
1691 |
||
1692 |
// We should only get three possible return codes from FlushDirtySm() : |
|
1693 |
// CFsRequest::EReqActionContinue - a write request (aNewRequest) needs to be dispatched |
|
1694 |
// CFsRequest::EReqActionComplete - completed already - this can happen if dirty data was beyond file end |
|
1695 |
// CFsRequest::EReqActionBusy - first dirty cacheline is already in use. |
|
1696 |
__ASSERT_DEBUG( r == CFsRequest::EReqActionContinue || r == CFsRequest::EReqActionComplete || r == CFsRequest::EReqActionBusy, Fault(EBadRetCode)); |
|
1697 |
if (r == CFsRequest::EReqActionContinue || r == CFsRequest::EReqActionBusy) |
|
1698 |
{ |
|
1699 |
// requeue the caller's request (aOldRequest) if there is one |
|
1700 |
return CFsRequest::EReqActionBusy; |
|
1701 |
} |
|
1702 |
else // CFsRequest::EReqActionComplete |
|
1703 |
{ |
|
1704 |
aNewRequest->PopOperation(); |
|
1705 |
aNewRequest->Free(); |
|
1706 |
aNewRequest = NULL; |
|
1707 |
return r; |
|
1708 |
} |
|
1709 |
||
1710 |
} |
|
1711 |
||
1712 |
||
1713 |
TInt TFsFileWriteDirty::PostInitialise(CFsRequest* aRequest) |
|
1714 |
{ |
|
1715 |
return CFileCache::CompleteFlushDirty(aRequest); |
|
1716 |
} |
|
1717 |
||
1718 |
TInt CFileCache::CompleteFlushDirty(CFsRequest* aRequest) |
|
1719 |
{ |
|
1720 |
CFsMessageRequest& msgRequest = *(CFsMessageRequest*) aRequest; |
|
1721 |
||
1722 |
CFileShare* share; |
|
1723 |
CFileCB* file; |
|
1724 |
GetFileFromScratch(aRequest, share, file); |
|
1725 |
CFileCache* fileCache = file->FileCache(); |
|
1726 |
||
1727 |
__ASSERT_DEBUG(fileCache != NULL, Fault(ENoFileCache)); |
|
1728 |
||
1729 |
#ifdef _DEBUG |
|
1730 |
TInt cancelling = (msgRequest.LastError() == KErrCancel)?(TBool)ETrue:(TBool)EFalse; |
|
1731 |
#endif |
|
1732 |
||
1733 |
fileCache->iLock.Wait(); |
|
1734 |
TInt r = fileCache->FlushDirtySm(msgRequest); |
|
1735 |
fileCache->iLock.Signal(); |
|
1736 |
||
1737 |
#ifdef _DEBUG |
|
1738 |
__ASSERT_DEBUG(!cancelling || msgRequest.LastError() == KErrCancel, Fault(ERequestUncancelled)); |
|
1739 |
#endif |
|
1740 |
||
1741 |
// if this request has been cancelled we mustn't dispatch it again - |
|
1742 |
// we still need to call state machine however so that any locked segments can be unlocked |
|
1743 |
if (msgRequest.LastError() == KErrCancel) |
|
1744 |
r = CFsRequest::EReqActionComplete; |
|
1745 |
||
1746 |
return r; |
|
1747 |
} |
|
1748 |
||
1749 |
TInt CFileCache::FlushDirtySm(CFsMessageRequest& aMsgRequest) |
|
1750 |
{ |
|
1751 |
enum states |
|
1752 |
{ |
|
1753 |
EStBegin=0, |
|
1754 |
EStWriteToDisk, |
|
1755 |
EStWriteToDiskComplete, |
|
1756 |
EStMarkAsClean, |
|
1757 |
EStEnd |
|
1758 |
}; |
|
1759 |
||
1760 |
TMsgOperation* currentOperation = &aMsgRequest.CurrentOperation(); |
|
1761 |
TInt& lastError = aMsgRequest.LastError(); |
|
1762 |
TInt64 pos = 0; |
|
1763 |
||
1764 |
for(;;) |
|
1765 |
{ |
|
1766 |
||
1767 |
switch(currentOperation->iState) |
|
1768 |
{ |
|
1769 |
case EStBegin: |
|
1770 |
iFlushBusy = ETrue; |
|
1771 |
||
1772 |
// fall into... |
|
1773 |
||
1774 |
case EStWriteToDisk: |
|
1775 |
{ |
|
1776 |
currentOperation->iState = EStWriteToDisk; |
|
1777 |
||
1778 |
TUint8* addr; |
|
1779 |
TInt segmentCount = iCacheClient->FindFirstDirtySegment(pos, addr); |
|
1780 |
||
1781 |
// keep track of how many segments we've written |
|
1782 |
currentOperation->iScratchValue0 = (TAny*) ((TInt) currentOperation->iScratchValue0 - segmentCount); |
|
1783 |
||
1784 |
// if no more dirty segments of if a genuine error then finish |
|
1785 |
// NB if the request has been cancelled then we still need to proceed and mark all the |
|
1786 |
// segments as clean, otherwise they will never get freed ! |
|
1787 |
if (segmentCount == 0 || (lastError != KErrNone && lastError != KErrCancel)) |
|
1788 |
{ |
|
1789 |
currentOperation->iState = EStEnd; |
|
1790 |
break; |
|
1791 |
} |
|
1792 |
||
1793 |
TInt len = segmentCount << SegmentSizeLog2(); |
|
1794 |
||
1795 |
if (pos < iSize64) |
|
1796 |
// Result of Min shall be of size TInt |
|
1797 |
// Hence to suppress warning |
|
1798 |
len = (TInt)(Min(iSize64 - pos, (TInt64)len)); |
|
1799 |
else |
|
1800 |
len = 0; |
|
1801 |
||
1802 |
||
1803 |
// if writing past end of file or this request has been cancelled, just mark as clean |
|
1804 |
if (len == 0 || lastError == KErrCancel) |
|
1805 |
{ |
|
1806 |
currentOperation->iState = EStMarkAsClean; |
|
1807 |
break; |
|
1808 |
} |
|
1809 |
||
1810 |
__CACHE_PRINT3(_L("CACHEFILE: FlushDirty first dirty pos %d, addr %08X count %d"), I64LOW(pos), addr, segmentCount); |
|
1811 |
||
1812 |
TInt filledSegmentCount; |
|
1813 |
TInt lockError; |
|
1814 |
addr = iCacheClient->FindAndLockSegments(pos, segmentCount, filledSegmentCount, lockError, ETrue); |
|
1815 |
||
1816 |
// If cacheline is busy, we need to post request to back of drive queue |
|
1817 |
// To dispatch to drive thread and intercept before DoRequestL() we must call iPostInitialise(), |
|
1818 |
// so set the state back to CFsRequest::EReqStatePostInitialise |
|
1819 |
if (lockError == KErrInUse) |
|
1820 |
{ |
|
1821 |
__CACHE_PRINT(_L("FlushDirtySm() - cacheline BUSY !")); |
|
1822 |
aMsgRequest.SetState(CFsRequest::EReqStatePostInitialise); |
|
1823 |
return CFsRequest::EReqActionBusy; |
|
1824 |
} |
|
1825 |
else if (lockError != KErrNone) |
|
1826 |
{ |
|
1827 |
iFlushBusy = EFalse; |
|
1828 |
lastError = lockError; |
|
1829 |
return CFsRequest::EReqActionComplete; |
|
1830 |
} |
|
1831 |
||
1832 |
currentOperation->Set(pos, len, addr, 0, EStWriteToDiskComplete); |
|
1833 |
if (pos < iSize64) |
|
1834 |
{ |
|
1835 |
TInt r = aMsgRequest.PushOperation( |
|
1836 |
pos, len, addr, 0, |
|
1837 |
CompleteFlushDirty, |
|
1838 |
EStWriteToDiskComplete); |
|
1839 |
if (r != KErrNone) |
|
1840 |
{ |
|
1841 |
iCacheClient->UnlockSegments(pos); |
|
1842 |
iFlushBusy = EFalse; |
|
1843 |
lastError = r; |
|
1844 |
return CFsRequest::EReqActionComplete; |
|
1845 |
} |
|
1846 |
||
1847 |
return CFsRequest::EReqActionContinue; // continue on to TFsFileWrite::DoRequestL()() |
|
1848 |
} |
|
1849 |
} |
|
1850 |
||
1851 |
case EStWriteToDiskComplete: |
|
1852 |
{ |
|
1853 |
#ifdef _DEBUG |
|
1854 |
// simulate a media eject to test critical error server |
|
1855 |
if (CCacheManagerFactory::CacheManager()->SimulateWriteFailureEnabled()) |
|
1856 |
{ |
|
1857 |
lastError = KErrNotReady; |
|
1858 |
iDrive->Dismount(); |
|
1859 |
} |
|
1860 |
#endif |
|
1861 |
pos = currentOperation->iReadWriteArgs.iPos; |
|
1862 |
iCacheClient->UnlockSegments(pos); |
|
1863 |
} |
|
1864 |
// fall into... |
|
1865 |
||
1866 |
case EStMarkAsClean: |
|
1867 |
{ |
|
1868 |
// NB pos must be set by EStWriteToDiskComplete or EStWriteToDisk |
|
1869 |
||
1870 |
if (lastError != KErrNone) |
|
1871 |
{ |
|
1872 |
__CACHE_PRINT1(_L("CACHEFILE: WriteThrough FAILED %d"), lastError); |
|
1873 |
||
1874 |
lastError = HandleWriteDirtyError(lastError); |
|
1875 |
||
1876 |
// retry ? |
|
1877 |
if (lastError == KErrNone) |
|
1878 |
{ |
|
1879 |
// clear error and try again |
|
1880 |
currentOperation->iState = EStWriteToDisk; |
|
1881 |
break; |
|
1882 |
} |
|
1883 |
||
1884 |
iFlushError = lastError; |
|
1885 |
PropagateFlushErrorToAllFileShares(); |
|
1886 |
||
1887 |
__CACHE_PRINT2(_L("CACHEFILE: Resetting size from %ld to %ld"), iSize64, iFileCB->Size64()); |
|
1888 |
SetSize64(iFileCB->Size64()); |
|
1889 |
} |
|
1890 |
||
1891 |
if (lastError != KErrNone) |
|
1892 |
{ |
|
1893 |
// Destroy ALL cached data (dirty and non-dirty) ! |
|
1894 |
iCacheClient->Purge(ETrue); |
|
1895 |
} |
|
1896 |
else |
|
1897 |
{ |
|
1898 |
// Mark segment as clean |
|
1899 |
iCacheClient->MarkSegmentsAsClean(pos); |
|
1900 |
} |
|
1901 |
||
1902 |
||
1903 |
if (TInt(currentOperation->iScratchValue0) > 0) |
|
1904 |
currentOperation->iState = EStWriteToDisk; |
|
1905 |
else |
|
1906 |
currentOperation->iState = EStEnd; |
|
1907 |
||
1908 |
||
1909 |
} |
|
1910 |
break; |
|
1911 |
||
1912 |
case EStEnd: |
|
1913 |
{ |
|
1914 |
iFlushBusy = EFalse; |
|
1915 |
||
1916 |
TUint8* addr; |
|
1917 |
MarkFileClean(); |
|
1918 |
// Re-start dirty data timer if there is still some dirty data |
|
1919 |
if (iCacheClient->FindFirstDirtySegment(pos, addr) != 0) |
|
1920 |
FileDirty(aMsgRequest); |
|
1921 |
||
1922 |
return CFsRequest::EReqActionComplete; |
|
1923 |
} |
|
1924 |
} |
|
1925 |
} |
|
1926 |
} |
|
1927 |
||
1928 |
/** |
|
1929 |
Handle a dirty data write error |
|
1930 |
||
1931 |
Returns aError if dirty data should be thrown away or |
|
1932 |
KErrNone if write should be retried |
|
1933 |
*/ |
|
1934 |
TInt CFileCache::HandleWriteDirtyError(TInt aError) |
|
1935 |
{ |
|
1936 |
__THRD_PRINT3(_L(">TRACE: CFileCache::HandleWriteDirtyError() aError %d mounted %d changed %d"), aError, iDrive->IsMounted(), iDrive->IsChanged()); |
|
1937 |
||
1938 |
// normally the disk change will have been detected by TDrive::CheckMount() but occasionally, |
|
1939 |
// the change will occur while writing - in which case we need to mimick what TDrive::CheckMount does, |
|
1940 |
// to make sure we are in a consisten state i.e. dismount the drive and set iCurrentMount to NULL |
|
1941 |
if (iDrive->IsChanged()) |
|
1942 |
{ |
|
1943 |
iDrive->SetChanged(EFalse); |
|
1944 |
if (iDrive->IsMounted()) // Dismount the mount if it is still marked as mounted |
|
1945 |
iDrive->Dismount(); |
|
1946 |
} |
|
1947 |
||
1948 |
// if error didn't occur because of a media eject, do nothing |
|
1949 |
if (aError == KErrNotReady && !iDrive->IsMounted()) |
|
1950 |
{ |
|
1951 |
||
1952 |
TLocaleMessage line1; |
|
1953 |
TLocaleMessage line2; |
|
1954 |
line1=EFileServer_PutTheCardBackLine1; |
|
1955 |
line2=EFileServer_PutTheCardBackLine2; |
|
1956 |
||
1957 |
//-- create Notifier |
|
1958 |
CAsyncNotifier* notifier = CAsyncNotifier::New(); |
|
1959 |
if( !notifier ) |
|
1960 |
{ |
|
1961 |
return aError; |
|
1962 |
} |
|
1963 |
||
1964 |
notifier->SetMount(iMount); |
|
1965 |
||
1966 |
||
1967 |
// While this (drive) thread is calling the notifier server (& effectively suspended), |
|
1968 |
// the main thread may call TFsFileRead::PostInitialise() or TFsFileWrite::PostInitialise() |
|
1969 |
// which would cause dead-lock unless we release the lock here. We also need to release |
|
1970 |
// the lock before calling CDriveThread::CompleteReadWriteRequests(). |
|
1971 |
iLock.Signal(); |
|
1972 |
||
1973 |
FOREVER |
|
1974 |
{ |
|
1975 |
TInt buttonVal; |
|
1976 |
||
1977 |
TInt ret = notifier->Notify( |
|
1978 |
TLocaleMessageText(line1), |
|
1979 |
TLocaleMessageText(line2), |
|
1980 |
TLocaleMessageText(EFileServer_Button1), |
|
1981 |
TLocaleMessageText(EFileServer_Button2), |
|
1982 |
buttonVal); |
|
1983 |
if (ret!=KErrNone) |
|
1984 |
break; |
|
1985 |
if (buttonVal!=1) |
|
1986 |
break; // Abort |
|
1987 |
||
1988 |
||
1989 |
// Wait up to 3 seconds for a disk change - this is because there is often a substantial |
|
1990 |
// (one/two second) delay between inserting a card & getting a notification that the card is ready; |
|
1991 |
// if we give up too soon and fire of the notifier again, it's not very user-friendly ! |
|
1992 |
const TTimeIntervalMicroSeconds32 KHalfSecond(500000); |
|
1993 |
const TInt KRetries = 6;; |
|
1994 |
for (TInt n=0; !iDrive->IsChanged() && n<KRetries; n++) |
|
1995 |
User::After(KHalfSecond); |
|
1996 |
||
1997 |
||
1998 |
||
1999 |
// Without this code, retry will indiscriminately write over whatever disk happens to be present. |
|
2000 |
// However if the write error is to the bootsector remounting will always fail because the boot |
|
2001 |
// sector will have changed and hence the disk is useless. |
|
2002 |
// |
|
2003 |
TRACE1(UTF::EBorder, UTraceModuleFileSys::ECMountCBReMount, EF32TraceUidFileSys, DriveNumber()); |
|
2004 |
||
2005 |
TInt remountSuccess = iDrive->ReMount(*iMount); |
|
2006 |
||
2007 |
TRACE1(UTF::EBorder, UTraceModuleFileSys::ECMountCBReMountRet, EF32TraceUidFileSys, remountSuccess); |
|
2008 |
if (!remountSuccess) |
|
2009 |
continue; |
|
2010 |
||
2011 |
||
2012 |
iMount->Drive().SetChanged(EFalse); |
|
2013 |
||
2014 |
aError = KErrNone; // Retry |
|
2015 |
break; |
|
2016 |
} |
|
2017 |
||
2018 |
delete notifier; |
|
2019 |
||
2020 |
||
2021 |
// Cancel hung state |
|
2022 |
FsThreadManager::SetDriveHung(DriveNumber(), EFalse); |
|
2023 |
||
2024 |
||
2025 |
// media had been removed and NOT replaced: so destroy ALL cached data |
|
2026 |
// (dirty and non-dirty) for all filecaches on this drive |
|
2027 |
if (aError != KErrNone) |
|
2028 |
{ |
|
2029 |
CDriveThread* pT=NULL; |
|
2030 |
TInt r=FsThreadManager::GetDriveThread(DriveNumber(), &pT); |
|
2031 |
if(r==KErrNone) |
|
2032 |
{ |
|
2033 |
pT->CompleteReadWriteRequests(); |
|
2034 |
iDrive->PurgeDirty(*iMount); |
|
2035 |
} |
|
2036 |
} |
|
2037 |
||
2038 |
iLock.Wait(); |
|
2039 |
} |
|
2040 |
||
2041 |
||
2042 |
return aError; |
|
2043 |
} |
|
2044 |
||
2045 |
||
2046 |
/** |
|
2047 |
Mark file as dirty |
|
2048 |
*/ |
|
2049 |
void CFileCache::FileDirty(CFsMessageRequest& aMsgRequest) |
|
2050 |
{ |
|
2051 |
CSessionFs* session = aMsgRequest.Session(); |
|
2052 |
__ASSERT_ALWAYS(session, Fault(EDirtyDataOwnerNull)); |
|
2053 |
||
2054 |
// Remember the last session which caused the file to become dirty |
|
2055 |
// Always record whether any session has reserved access so the CheckDiskSpace() behaves correctly |
|
2056 |
if (iDirtyDataOwner == NULL || session->ReservedAccess(iDriveNum)) |
|
2057 |
iDirtyDataOwner = session; |
|
2058 |
||
2059 |
// start a timer after which file will be flushed |
|
2060 |
CDriveThread* driveThread=NULL; |
|
2061 |
TInt r = FsThreadManager::GetDriveThread(iDriveNum, &driveThread); |
|
2062 |
if(r == KErrNone && driveThread != NULL) |
|
2063 |
iDirtyTimer.Start(driveThread, iDirtyDataFlushTime); |
|
2064 |
} |
|
2065 |
||
2066 |
//---------------------------------------------------------------------------- |
|
2067 |
/** |
|
2068 |
Mark the file as clean and stop dirty data timer |
|
2069 |
*/ |
|
2070 |
void CFileCache::MarkFileClean() |
|
2071 |
{ |
|
2072 |
iDirtyDataOwner = NULL; |
|
2073 |
||
2074 |
if (!iDriveThread) |
|
2075 |
return; |
|
2076 |
||
2077 |
iDirtyTimer.Stop(); |
|
2078 |
} |
|
2079 |
||
2080 |
||
2081 |
||
2082 |
//************************************ |
|
2083 |
// TFileCacheSettings |
|
2084 |
//************************************ |
|
2085 |
||
2086 |
RArray<TFileCacheSettings::TFileCacheConfig>* TFileCacheSettings::iFileCacheSettings = NULL; |
|
2087 |
||
2088 |
||
2089 |
||
2090 |
const TInt KDriveCacheSettingsArrayGranularity = 4; |
|
2091 |
||
2092 |
||
2093 |
TFileCacheSettings::TFileCacheConfig::TFileCacheConfig(TInt aDrive) : |
|
2094 |
iDrive(aDrive), |
|
2095 |
iFileCacheReadAsync(KDefaultFileCacheReadAsync), |
|
2096 |
iFairSchedulingLen(KDefaultFairSchedulingLen << KByteToByteShift), |
|
2097 |
iCacheSize(KDefaultFileCacheSize << KByteToByteShift), |
|
2098 |
iMaxReadAheadLen(KDefaultFileCacheMaxReadAheadLen << KByteToByteShift), |
|
2099 |
iClosedFileKeepAliveTime(KDefaultClosedFileKeepAliveTime << KByteToByteShift), // convert milliSecs -> microSecs (approximately) |
|
2100 |
iDirtyDataFlushTime(KDefaultDirtyDataFlushTime << KByteToByteShift) // convert milliSecs -> microSecs (approximately) |
|
2101 |
{ |
|
2102 |
iFlags = ConvertEnumToFlags(KDefaultFileCacheRead, KDefaultFileCacheReadAhead, KDefaultFileCacheWrite); |
|
2103 |
} |
|
2104 |
||
2105 |
||
2106 |
TFileCacheFlags TFileCacheSettings::TFileCacheConfig::ConvertEnumToFlags(const TInt aFileCacheRead, const TInt aFileCacheReadAhead, const TInt aFileCacheWrite) |
|
2107 |
{ |
|
2108 |
TInt flags(0); |
|
2109 |
||
2110 |
// read caching |
|
2111 |
if (aFileCacheRead == EFileCacheFlagEnabled) |
|
2112 |
flags|= EFileCacheReadEnabled; |
|
2113 |
else if (aFileCacheRead == EFileCacheFlagOn) |
|
2114 |
flags|= EFileCacheReadEnabled | EFileCacheReadOn; |
|
2115 |
||
2116 |
// read ahead |
|
2117 |
if (aFileCacheReadAhead == EFileCacheFlagEnabled) |
|
2118 |
flags|= EFileCacheReadAheadEnabled; |
|
2119 |
else if (aFileCacheReadAhead == EFileCacheFlagOn) |
|
2120 |
flags|= EFileCacheReadAheadEnabled | EFileCacheReadAheadOn; |
|
2121 |
||
2122 |
// write caching |
|
2123 |
if (aFileCacheWrite == EFileCacheFlagEnabled) |
|
2124 |
flags|= EFileCacheWriteEnabled; |
|
2125 |
else if (aFileCacheWrite == EFileCacheFlagOn) |
|
2126 |
flags|= EFileCacheWriteEnabled | EFileCacheWriteOn; |
|
2127 |
||
2128 |
return TFileCacheFlags(flags); |
|
2129 |
} |
|
2130 |
||
2131 |
||
2132 |
||
2133 |
_LIT8(KLitSectionNameDrive,"Drive%C"); |
|
2134 |
||
2135 |
static const TPtrC8 KCacheFlagEnumStrings[]= |
|
2136 |
{ |
|
2137 |
_S8("OFF"), |
|
2138 |
_S8("ENABLED"), |
|
2139 |
_S8("ON"), |
|
2140 |
_S8(""), // terminator |
|
2141 |
}; |
|
2142 |
const TInt KMaxEnumLen = 7; |
|
2143 |
||
2144 |
void TFileCacheSettings::ReadEnum(const TDesC8& aSection, const TDesC8& aProperty, TInt32& aEnumVal, const TPtrC8* aEnumStrings) |
|
2145 |
{ |
|
2146 |
TBuf8<KMaxEnumLen> buf; |
|
2147 |
if (!F32Properties::GetString(aSection, aProperty, buf)) |
|
2148 |
return; |
|
2149 |
TInt n; |
|
2150 |
const TPtrC8* enumString; |
|
2151 |
for (enumString=aEnumStrings, n=0; enumString->Length()!= 0; enumString++, n++) |
|
2152 |
{ |
|
2153 |
if (buf.LeftTPtr(enumString->Length()).MatchF(*enumString) == 0) |
|
2154 |
{ |
|
2155 |
aEnumVal = n; |
|
2156 |
break; |
|
2157 |
} |
|
2158 |
} |
|
2159 |
} |
|
2160 |
||
2161 |
TInt TFileCacheSettings::ReadPropertiesFile(TInt aDriveNumber) |
|
2162 |
{ |
|
2163 |
Init(); |
|
2164 |
||
2165 |
||
2166 |
if (!TGlobalFileCacheSettings::Enabled()) |
|
2167 |
return KErrNone; |
|
2168 |
||
2169 |
TFileCacheConfig* driveCacheSettings; |
|
2170 |
TInt r = GetFileCacheConfig(aDriveNumber, driveCacheSettings); |
|
2171 |
if (r != KErrNone) |
|
2172 |
return r; |
|
2173 |
||
2174 |
// restore default settings in case they've been changed by SetFlags() |
|
2175 |
driveCacheSettings->iFlags = TFileCacheConfig::ConvertEnumToFlags(KDefaultFileCacheRead, KDefaultFileCacheReadAhead, KDefaultFileCacheWrite); |
|
2176 |
||
2177 |
||
2178 |
// Get file cache configuration settings for this drive |
|
2179 |
// N.B. Size/length values are specified in Kilobytes, timer values in Milliseconds |
|
2180 |
TBuf8<8> sectionName; |
|
2181 |
sectionName.Format(KLitSectionNameDrive, 'A' + aDriveNumber); |
|
2182 |
||
2183 |
TInt32 val; |
|
2184 |
// Read FileCacheSize |
|
2185 |
if (F32Properties::GetInt(sectionName, _L8("FileCacheSize"), val)) |
|
2186 |
driveCacheSettings->iCacheSize = val << KByteToByteShift; |
|
2187 |
||
2188 |
// ensure read-ahead len is not greater than the cache size |
|
2189 |
driveCacheSettings->iMaxReadAheadLen = |
|
2190 |
Min(driveCacheSettings->iMaxReadAheadLen, driveCacheSettings->iCacheSize); |
|
2191 |
||
2192 |
// Read FileCacheReadAsync |
|
2193 |
TBool bVal; |
|
2194 |
if (F32Properties::GetBool(sectionName, _L8("FileCacheReadAsync"), bVal)) |
|
2195 |
driveCacheSettings->iFileCacheReadAsync = bVal; |
|
2196 |
||
2197 |
// Read FairSchedulingLen |
|
2198 |
if (F32Properties::GetInt(sectionName, _L8("FairSchedulingLen"), val)) |
|
2199 |
driveCacheSettings->iFairSchedulingLen = val << KByteToByteShift; |
|
2200 |
||
2201 |
// Read ClosedFileKeepAliveTime - convert miliSecs to microSecs (approximately) |
|
2202 |
if (F32Properties::GetInt(sectionName, _L8("ClosedFileKeepAliveTime"), val)) |
|
2203 |
driveCacheSettings->iClosedFileKeepAliveTime = val << KByteToByteShift; |
|
2204 |
||
2205 |
// Read DirtyDataFlushTime - convert miliSecs to microSecs (approximately) |
|
2206 |
if (F32Properties::GetInt(sectionName, _L8("DirtyDataFlushTime"), val)) |
|
2207 |
driveCacheSettings->iDirtyDataFlushTime = val << KByteToByteShift; |
|
2208 |
||
2209 |
// get read, read-ahead and write states |
|
2210 |
TInt32 readVal = KDefaultFileCacheRead; |
|
2211 |
TInt32 readAheadVal = KDefaultFileCacheReadAhead; |
|
2212 |
TInt32 writeVal = KDefaultFileCacheWrite; |
|
2213 |
||
2214 |
ReadEnum(sectionName, _L8("FileCacheRead"), readVal, &KCacheFlagEnumStrings[0]); |
|
2215 |
ReadEnum(sectionName, _L8("FileCacheReadAhead"), readAheadVal, &KCacheFlagEnumStrings[0]); |
|
2216 |
ReadEnum(sectionName, _L8("FileCacheWrite"), writeVal, &KCacheFlagEnumStrings[0]); |
|
2217 |
driveCacheSettings->iFlags = TFileCacheConfig::ConvertEnumToFlags(readVal, readAheadVal, writeVal); |
|
2218 |
||
2219 |
__CACHE_PRINT7(_L("ReadPropertiesFile() drive %C flags %08X CacheSize %d FileCacheReadAsync %d iFairSchedulingLen %d iClosedFileKeepAliveTime %d iDirtyDataFlushTime %d\n"), |
|
2220 |
aDriveNumber + 'A', |
|
2221 |
driveCacheSettings->iFlags, |
|
2222 |
driveCacheSettings->iCacheSize, |
|
2223 |
driveCacheSettings->iFileCacheReadAsync, |
|
2224 |
driveCacheSettings->iFairSchedulingLen, |
|
2225 |
driveCacheSettings->iClosedFileKeepAliveTime, |
|
2226 |
driveCacheSettings->iDirtyDataFlushTime); |
|
2227 |
||
2228 |
return KErrNone; |
|
2229 |
} |
|
2230 |
||
2231 |
||
2232 |
TInt TFileCacheSettings::GetFileCacheConfig(TInt aDrive, TFileCacheConfig*& aConfig) |
|
2233 |
{ |
|
2234 |
Init(); |
|
2235 |
||
2236 |
aConfig = NULL; |
|
2237 |
||
2238 |
TFileCacheConfig driveCacheSettingsToMatch(aDrive); |
|
2239 |
||
2240 |
TInt index = iFileCacheSettings->FindInUnsignedKeyOrder(driveCacheSettingsToMatch); |
|
2241 |
if (index == KErrNotFound) |
|
2242 |
{ |
|
2243 |
// create a new entry. |
|
2244 |
TInt r = iFileCacheSettings->InsertInUnsignedKeyOrder(driveCacheSettingsToMatch); |
|
2245 |
if (r != KErrNone) |
|
2246 |
return r; |
|
2247 |
index = iFileCacheSettings->FindInUnsignedKeyOrder(driveCacheSettingsToMatch); |
|
2248 |
__ASSERT_ALWAYS(index != KErrNotFound, Fault(ECacheSettingsNotFound)); |
|
2249 |
} |
|
2250 |
||
2251 |
aConfig = &(*iFileCacheSettings)[index]; |
|
2252 |
return KErrNone; |
|
2253 |
} |
|
2254 |
||
2255 |
void TFileCacheSettings::SetFlags(TInt aDrive, TFileCacheFlags aFlags) |
|
2256 |
{ |
|
2257 |
TFileCacheConfig* driveCacheSettings; |
|
2258 |
TInt r = GetFileCacheConfig(aDrive, driveCacheSettings); |
|
2259 |
__ASSERT_ALWAYS(r == KErrNone && driveCacheSettings != NULL, Fault(ECacheSettingGetFailed)); |
|
2260 |
||
2261 |
driveCacheSettings->iFlags = aFlags; |
|
2262 |
} |
|
2263 |
||
2264 |
TFileCacheFlags TFileCacheSettings::Flags(TInt aDrive) |
|
2265 |
{ |
|
2266 |
TFileCacheConfig* driveCacheSettings; |
|
2267 |
TInt r = GetFileCacheConfig(aDrive, driveCacheSettings); |
|
2268 |
__ASSERT_ALWAYS(r == KErrNone && driveCacheSettings != NULL, Fault(ECacheSettingGetFailed)); |
|
2269 |
||
2270 |
return driveCacheSettings->iFlags; |
|
2271 |
} |
|
2272 |
||
2273 |
void TFileCacheSettings::Init() |
|
2274 |
{ |
|
2275 |
if (iFileCacheSettings == NULL) |
|
2276 |
{ |
|
2277 |
iFileCacheSettings = new RArray<TFileCacheConfig>(KDriveCacheSettingsArrayGranularity, _FOFF(TFileCacheConfig, iDrive)); |
|
2278 |
__ASSERT_ALWAYS(iFileCacheSettings != NULL, Fault(ECacheSettingsInitFailed)); |
|
2279 |
} |
|
2280 |
} |
|
2281 |
||
2282 |
||
2283 |
TBool TFileCacheSettings::FileCacheReadAsync(TInt aDrive) |
|
2284 |
{ |
|
2285 |
TFileCacheConfig* driveCacheSettings; |
|
2286 |
TInt r = GetFileCacheConfig(aDrive, driveCacheSettings); |
|
2287 |
__ASSERT_ALWAYS(r == KErrNone && driveCacheSettings != NULL, Fault(ECacheSettingGetFailed)); |
|
2288 |
||
2289 |
return driveCacheSettings->iFileCacheReadAsync; |
|
2290 |
} |
|
2291 |
||
2292 |
TInt TFileCacheSettings::FairSchedulingLen(TInt aDrive) |
|
2293 |
{ |
|
2294 |
TFileCacheConfig* driveCacheSettings; |
|
2295 |
TInt r = GetFileCacheConfig(aDrive, driveCacheSettings); |
|
2296 |
__ASSERT_ALWAYS(r == KErrNone && driveCacheSettings != NULL, Fault(ECacheSettingGetFailed)); |
|
2297 |
||
2298 |
return driveCacheSettings->iFairSchedulingLen; |
|
2299 |
} |
|
2300 |
||
2301 |
TInt TFileCacheSettings::MaxReadAheadLen(TInt aDrive) |
|
2302 |
{ |
|
2303 |
TFileCacheConfig* driveCacheSettings; |
|
2304 |
TInt r = GetFileCacheConfig(aDrive, driveCacheSettings); |
|
2305 |
__ASSERT_ALWAYS(r == KErrNone && driveCacheSettings != NULL, Fault(ECacheSettingGetFailed)); |
|
2306 |
||
2307 |
return driveCacheSettings->iMaxReadAheadLen; |
|
2308 |
} |
|
2309 |
||
2310 |
TInt TFileCacheSettings::CacheSize(TInt aDrive) |
|
2311 |
{ |
|
2312 |
TFileCacheConfig* driveCacheSettings; |
|
2313 |
TInt r = GetFileCacheConfig(aDrive, driveCacheSettings); |
|
2314 |
__ASSERT_ALWAYS(r == KErrNone && driveCacheSettings != NULL, Fault(ECacheSettingGetFailed)); |
|
2315 |
||
2316 |
return driveCacheSettings->iCacheSize; |
|
2317 |
} |
|
2318 |
||
2319 |
TInt TFileCacheSettings::ClosedFileKeepAliveTime(TInt aDrive) |
|
2320 |
{ |
|
2321 |
TFileCacheConfig* driveCacheSettings; |
|
2322 |
TInt r = GetFileCacheConfig(aDrive, driveCacheSettings); |
|
2323 |
__ASSERT_ALWAYS(r == KErrNone && driveCacheSettings != NULL, Fault(ECacheSettingGetFailed)); |
|
2324 |
||
2325 |
return driveCacheSettings->iClosedFileKeepAliveTime; |
|
2326 |
} |
|
2327 |
||
2328 |
||
2329 |
TInt TFileCacheSettings::DirtyDataFlushTime(TInt aDrive) |
|
2330 |
{ |
|
2331 |
TFileCacheConfig* driveCacheSettings; |
|
2332 |
TInt r = GetFileCacheConfig(aDrive, driveCacheSettings); |
|
2333 |
__ASSERT_ALWAYS(r == KErrNone && driveCacheSettings != NULL, Fault(ECacheSettingGetFailed)); |
|
2334 |
||
2335 |
return driveCacheSettings->iDirtyDataFlushTime; |
|
2336 |
} |
|
2337 |
||
2338 |
//************************************ |
|
2339 |
// TClosedFileUtils |
|
2340 |
//************************************ |
|
2341 |
||
2342 |
CFsObjectCon* TClosedFileUtils::iClosedFiles = NULL; |
|
2343 |
||
2344 |
void TClosedFileUtils::InitL() |
|
2345 |
{ |
|
2346 |
iClosedFiles = TheContainer->CreateL(); |
|
2347 |
if (iClosedFiles == NULL) |
|
2348 |
User::LeaveIfError(KErrNoMemory); |
|
2349 |
||
2350 |
} |
|
2351 |
||
2352 |
||
2353 |
TBool TClosedFileUtils::IsClosed(CFileCache* aFileCache) |
|
2354 |
{ |
|
2355 |
return (aFileCache->Container() == iClosedFiles); |
|
2356 |
} |
|
2357 |
||
2358 |
TInt TClosedFileUtils::Count() |
|
2359 |
{ |
|
2360 |
return iClosedFiles->Count(); |
|
2361 |
} |
|
2362 |
||
2363 |
CFileCache* TClosedFileUtils::At(TInt aIndex) |
|
2364 |
{ |
|
2365 |
return (CFileCache*) (*iClosedFiles)[aIndex]; |
|
2366 |
} |
|
2367 |
||
2368 |
||
2369 |
// Add a closed file to closed file container |
|
2370 |
void TClosedFileUtils::AddL(CFileCache* aFileCache, TBool aLock) |
|
2371 |
{ |
|
2372 |
if (aFileCache->iDriveThread) |
|
2373 |
{ |
|
2374 |
iClosedFiles->AddL(aFileCache, aLock); |
|
2375 |
||
2376 |
// start a timer after which file will be removed from closed queue |
|
2377 |
CDriveThread* driveThread=NULL; |
|
2378 |
TInt r = FsThreadManager::GetDriveThread(aFileCache->iDriveNum, &driveThread); |
|
2379 |
if(r == KErrNone && driveThread != NULL) |
|
2380 |
aFileCache->iClosedTimer.Start(driveThread, aFileCache->iClosedFileKeepAliveTime); |
|
2381 |
} |
|
2382 |
} |
|
2383 |
||
2384 |
||
2385 |
||
2386 |
// Remove a closed file from closed file container so that it can be re-opened |
|
2387 |
void TClosedFileUtils::ReOpen(CFileCache* aFileCache, TBool aLock) |
|
2388 |
{ |
|
2389 |
// get the drive thread in case it has changed since the file was last open |
|
2390 |
const TInt r = FsThreadManager::GetDriveThread(aFileCache->iDriveNum, &aFileCache->iDriveThread); |
|
2391 |
if ((r == KErrNone) && aFileCache->iDriveThread) |
|
2392 |
aFileCache->iClosedTimer.Stop(); |
|
2393 |
||
2394 |
iClosedFiles->Remove(aFileCache, aLock); |
|
2395 |
} |
|
2396 |
||
2397 |
// Remove all closed files from closed file container and close them for good |
|
2398 |
void TClosedFileUtils::Remove() |
|
2399 |
{ |
|
2400 |
RemoveFiles(NULL, NULL); |
|
2401 |
} |
|
2402 |
||
2403 |
// Remove all closed files belonging to a particular TDrive from closed file container and close them for good |
|
2404 |
void TClosedFileUtils::Remove(TInt aDrvNumber) |
|
2405 |
{ |
|
2406 |
RemoveFiles(&TClosedFileUtils::TestDrive, (TAny*) aDrvNumber); |
|
2407 |
} |
|
2408 |
||
2409 |
// Remove a closed file from closed file container and close it for good |
|
2410 |
void TClosedFileUtils::Remove(CFileCache* aFileCache) |
|
2411 |
{ |
|
2412 |
RemoveFiles(&TClosedFileUtils::TestFile, (TAny*) aFileCache); |
|
2413 |
} |
|
2414 |
||
2415 |
void TClosedFileUtils::RemoveFiles(TTestFunc aTestFunc, TAny* aTestVal) |
|
2416 |
{ |
|
2417 |
Lock(); |
|
2418 |
||
2419 |
TInt count = TClosedFileUtils::Count(); |
|
2420 |
while(count--) |
|
2421 |
{ |
|
2422 |
CFileCache& file = *(CFileCache*)(*iClosedFiles)[count]; |
|
2423 |
if ((aTestFunc == NULL) || ((*aTestFunc)(file, aTestVal))) |
|
2424 |
{ |
|
2425 |
iClosedFiles->Remove(&file, EFalse); |
|
2426 |
file.Close(); |
|
2427 |
} |
|
2428 |
} |
|
2429 |
||
2430 |
Unlock(); |
|
2431 |
} |
|
2432 |
||
2433 |
||
2434 |
TBool TClosedFileUtils::TestDrive(CFileCache& aFileCache, TAny* aVal) |
|
2435 |
{ |
|
2436 |
TBool r = (aFileCache.iDriveNum == (TInt) aVal)?(TBool) ETrue: (TBool) EFalse; |
|
2437 |
if (r) |
|
2438 |
{ |
|
2439 |
__CACHE_PRINT1(_L("CLOSEDFILES: TestDrive() closing %S\n"), &aFileCache.FileNameF() ); |
|
2440 |
} |
|
2441 |
return r; |
|
2442 |
} |
|
2443 |
TBool TClosedFileUtils::TestFile(CFileCache& aFileCache, TAny* aVal) |
|
2444 |
{ |
|
2445 |
TBool r = (&aFileCache == ((CFileCache*) aVal))?(TBool)ETrue:(TBool)EFalse; |
|
2446 |
if (r) |
|
2447 |
{ |
|
2448 |
__CACHE_PRINT1(_L("CLOSEDFILES: TestFile() closing %S\n"), &aFileCache.FileNameF() ); |
|
2449 |
} |
|
2450 |
return r; |
|
2451 |
} |
|
2452 |
||
2453 |
||
2454 |
||
2455 |
void TClosedFileUtils::Lock() |
|
2456 |
{ |
|
2457 |
iClosedFiles->Lock(); |
|
2458 |
} |
|
2459 |
||
2460 |
void TClosedFileUtils::Unlock() |
|
2461 |
{ |
|
2462 |
iClosedFiles->Unlock(); |
|
2463 |
} |
|
2464 |
||
2465 |