First public contribution.
1 // Copyright (c) 2006-2009 Nokia Corporation and/or its subsidiary(-ies).
2 // All rights reserved.
3 // This component and the accompanying materials are made available
4 // under the terms of the License "Eclipse Public License v1.0"
5 // which accompanies this distribution, and is available
6 // at the URL "http://www.eclipse.org/legal/epl-v10.html".
8 // Initial Contributors:
9 // Nokia Corporation - initial contribution.
14 // f32\sfile\sf_file_cache.h
24 #if !defined(__SF_FILE_CACHE_H__)
25 #define __SF_FILE_CACHE_H__
27 #include "sf_cache_client.h"
29 //#define _DEBUG_READ_AHEAD
32 NONSHARABLE_CLASS(CFileCache) : public CFsDispatchObject
35 static CFileCache* NewL(CFileShare& aShare);
36 CFileCache* ReNewL(CFileShare& aShare);
38 void Init(CFileShare& aShare);
40 CMountCB& Mount() const;
44 void ReadAhead(CFsMessageRequest& aMsgRequest, TUint aMode);
45 void ResetReadAhead();
47 static TInt CompleteRead(CFsRequest* aMsgRequest);
48 static TInt CompleteWrite(CFsRequest* aMsgRequest);
49 TInt ReadBuffered(CFsMessageRequest& aMsgRequest, TUint aMode);
50 TInt WriteBuffered(CFsMessageRequest& aMsgRequest, TUint aMode);
52 inline TBool IsDirty() {return iDirtyDataOwner != NULL;}
53 TInt FlushDirty(CFsRequest* aOldRequest = NULL);
54 void Purge(TBool aPurgeDirty = EFalse);
59 inline TInt64 Size64() const {return iSize64;}
61 // Sets 64 bit (cached) file size
62 void SetSize64(TInt64 aSize);
64 TDrive& Drive() const;
65 TUint32 NameHash() const;
66 HBufC& FileNameF() const;
71 void ConstructL(CFileShare& aShare);
74 void SetFileCacheFlags(CFileShare& aShare);
76 TInt DoReadBuffered(CFsMessageRequest& aMsgRequest, TUint aMode, CFsClientMessageRequest*& aNewRequest);
77 TInt DoWriteBuffered(CFsMessageRequest& aMsgRequest, CFsClientMessageRequest*& aNewRequest, TUint aMode);
78 TInt DoFlushDirty(CFsClientMessageRequest*& aNewRequest, CFsRequest* aOldRequest, TBool aFlushAll);
81 inline TInt SegmentSize() const;
82 inline TInt SegmentSizeLog2() const;
83 inline TInt64 SegmentSizeMask() const;
85 inline void UpdateSharePosition(CFsMessageRequest& aMsgRequest, TMsgOperation& aCurrentOperation);
87 static TInt CompleteFlushDirty(CFsRequest* aMsgRequest);
88 TInt FlushDirtySm(CFsMessageRequest& aMsgRequest);
90 TInt AllocateRequest(CFsClientMessageRequest*& aNewRequest, TBool aWrite, CSessionFs* aSession = NULL);
92 void DoReadAhead(CFsMessageRequest& aMsgRequest, TUint aMode);
94 void FileDirty(CFsMessageRequest& aMsgRequest);
98 TInt HandleWriteDirtyError(TInt aError);
99 void PropagateFlushErrorToAllFileShares();
101 static TInt ClosedTimerEvent(TAny* aFileCache);
102 static TInt DirtyTimerEvent(TAny* aFileCache);
104 TBool IsDriveThread();
108 CCacheClient* iCacheClient;
110 TInt iMaxReadAheadLen;
113 TInt32 iClosedFileKeepAliveTime; // in microseconds
114 TInt32 iDirtyDataFlushTime; // in microseconds
123 CDriveThread* iDriveThread;
126 // closed queue eject timer
127 TThreadTimer iClosedTimer;
129 // dirty data flush timer
130 TThreadTimer iDirtyTimer;
131 // The last session writing to this file. If non-NULL indicates cache contains dirty data
132 CSessionFs* iDirtyDataOwner;
134 // The size of the file.
137 // The size of the file at the start of a write request
140 // The full name of the file, including drive and extensions - Folded. This is "stolen"
141 // from the owning CFileCB when the CFileCB destructor calls CFileCache::Close().
151 TInt iSequentialReads;
153 TInt64 iReadAheadPos;
156 TBool iFileCacheReadAsync;
157 CFsClientMessageRequest* iReadAheadRequest;
159 friend class TClosedFileUtils;
160 friend class TFsFileWriteDirty;
164 class TFileCacheSettings
167 class TFileCacheConfig
170 TFileCacheConfig(TInt aDrive);
171 static TFileCacheFlags ConvertEnumToFlags(const TInt aFileCacheRead, const TInt aFileCacheReadAhead, const TInt aFileCacheWrite);
174 TFileCacheFlags iFlags;
176 TBool iFileCacheReadAsync;
177 TInt32 iFairSchedulingLen; // in bytes
178 TInt32 iCacheSize; // in bytes
179 TInt32 iMaxReadAheadLen; // in bytes
180 TInt32 iClosedFileKeepAliveTime; // in microseconds
181 TInt32 iDirtyDataFlushTime; // in microseconds
185 static TInt ReadPropertiesFile(TInt aDriveNumber);
186 static void SetFlags(TInt aDrive, TFileCacheFlags alags);
187 static TFileCacheFlags Flags(TInt aDrive);
190 static TInt FileCacheReadAsync(TInt aDrive);
191 static TInt FairSchedulingLen(TInt aDrive);
192 static TInt MaxReadAheadLen(TInt aDrive);
193 static TInt CacheSize(TInt aDrive);
194 static TInt ClosedFileKeepAliveTime(TInt aDrive);
195 static TInt DirtyDataFlushTime(TInt aDrive);
196 static TInt GetFileCacheConfig(TInt aDrive, TFileCacheConfig*& aConfig);
199 static void ReadEnum(const TDesC8& aSection, const TDesC8& aProperty, TInt32& aEnumVal, const TPtrC8* aEnumStrings);
202 static RArray<TFileCacheConfig>* iFileCacheSettings;
206 class TClosedFileUtils
212 static CFileCache* At(TInt aIndex);
213 static TBool IsClosed(CFileCache* aFileCache);
215 static void AddL(CFileCache* aFileCache, TBool aLock);
217 static void Remove();
218 static void Remove(TInt aDrvNumber);
219 static void Remove(CFileCache* aFileCache);
222 static void Unlock();
224 static void ReOpen(CFileCache* aFileCache, TBool aLock);
226 typedef TBool (*TTestFunc)(CFileCache& aFileCache, TAny* aVal);
227 static void RemoveFiles(TTestFunc aTestFunc, TAny* aTestVal);
229 static TBool TestDrive(CFileCache& aFileCache, TAny* aVal);
230 static TBool TestFile(CFileCache& aFileCache, TAny* aVal);
233 static CFsObjectCon* iClosedFiles;