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