ReactOS  0.4.14-dev-50-g13bb5e2
cacheman.c
Go to the documentation of this file.
1 /*
2  * COPYRIGHT: See COPYING in the top level directory
3  * PROJECT: ReactOS kernel
4  * FILE: ntoskrnl/cc/cacheman.c
5  * PURPOSE: Cache manager
6  *
7  * PROGRAMMERS: David Welch (welch@cwcom.net)
8  * Pierre Schweitzer (pierre@reactos.org)
9  */
10 
11 /* INCLUDES *****************************************************************/
12 
13 #include <ntoskrnl.h>
14 #define NDEBUG
15 #include <debug.h>
16 
20 
21 static ULONG BugCheckFileId = 0x4 << 16;
22 
23 /* FUNCTIONS *****************************************************************/
24 
25 INIT_FUNCTION
26 VOID
27 NTAPI
29 {
30  /* Notify debugger */
33  "CCPF: InitializePrefetecher()\n");
34 
35  /* Setup the Prefetcher Data */
39 
40  /* FIXME: Setup the rest of the prefetecher */
41 }
42 
43 INIT_FUNCTION
44 BOOLEAN
45 NTAPI
47 {
48  ULONG Thread;
49 
50  CcInitView();
51 
52  /* Initialize lazy-writer lists */
57 
58  /* Define lazy writer threshold and the amount of workers,
59  * depending on the system type
60  */
62  switch (CcCapturedSystemSize)
63  {
64  case MmSmallSystem:
67  break;
68 
69  case MmMediumSystem:
72  break;
73 
74  case MmLargeSystem:
77  break;
78 
79  default:
82  break;
83  }
84 
85  /* Allocate a work item for all our threads */
87  {
88  PWORK_QUEUE_ITEM Item;
89 
90  Item = ExAllocatePoolWithTag(NonPagedPool, sizeof(WORK_QUEUE_ITEM), 'qWcC');
91  if (Item == NULL)
92  {
93  CcBugCheck(0, 0, 0);
94  }
95 
96  /* By default, it's obviously idle */
99  }
100 
101  /* Initialize our lazy writer */
104  /* Delay activation of the lazy writer */
107 
108  /* Lookaside list for our work items */
110 
111  return TRUE;
112 }
113 
114 VOID
115 NTAPI
117 {
118  /* NOTHING TO DO */
119 }
120 
121 /*
122  * @unimplemented
123  */
125 NTAPI
128  IN BOOLEAN BcbListHeld
129  )
130 {
132 
134 
135  i.QuadPart = 0;
136  return i;
137 }
138 
139 /*
140  * @unimplemented
141  */
142 PVOID
143 NTAPI
145  IN PVOID Bcb
146  )
147 {
149 
150  return 0;
151 }
152 
153 /*
154  * @unimplemented
155  */
156 VOID
157 NTAPI
161  IN ULONG Length
162  )
163 {
164  KIRQL OldIrql;
165  LARGE_INTEGER NewOffset;
166  PROS_SHARED_CACHE_MAP SharedCacheMap;
167  PPRIVATE_CACHE_MAP PrivateCacheMap;
168 
169  SharedCacheMap = FileObject->SectionObjectPointer->SharedCacheMap;
170  PrivateCacheMap = FileObject->PrivateCacheMap;
171 
172  /* If file isn't cached, or if read ahead is disabled, this is no op */
173  if (SharedCacheMap == NULL || PrivateCacheMap == NULL ||
174  BooleanFlagOn(SharedCacheMap->Flags, READAHEAD_DISABLED))
175  {
176  return;
177  }
178 
179  /* Round read length with read ahead mask */
180  Length = ROUND_UP(Length, PrivateCacheMap->ReadAheadMask + 1);
181  /* Compute the offset we'll reach */
182  NewOffset.QuadPart = FileOffset->QuadPart + Length;
183 
184  /* Lock read ahead spin lock */
185  KeAcquireSpinLock(&PrivateCacheMap->ReadAheadSpinLock, &OldIrql);
186  /* Easy case: the file is sequentially read */
188  {
189  /* If we went backward, this is no go! */
190  if (NewOffset.QuadPart < PrivateCacheMap->ReadAheadOffset[1].QuadPart)
191  {
192  KeReleaseSpinLock(&PrivateCacheMap->ReadAheadSpinLock, OldIrql);
193  return;
194  }
195 
196  /* FIXME: hackish, but will do the job for now */
197  PrivateCacheMap->ReadAheadOffset[1].QuadPart = NewOffset.QuadPart;
198  PrivateCacheMap->ReadAheadLength[1] = Length;
199  }
200  /* Other cases: try to find some logic in that mess... */
201  else
202  {
203  /* Let's check if we always read the same way (like going down in the file)
204  * and pretend it's enough for now
205  */
206  if (PrivateCacheMap->FileOffset2.QuadPart >= PrivateCacheMap->FileOffset1.QuadPart &&
207  FileOffset->QuadPart >= PrivateCacheMap->FileOffset2.QuadPart)
208  {
209  /* FIXME: hackish, but will do the job for now */
210  PrivateCacheMap->ReadAheadOffset[1].QuadPart = NewOffset.QuadPart;
211  PrivateCacheMap->ReadAheadLength[1] = Length;
212  }
213  else
214  {
215  /* FIXME: handle the other cases */
216  KeReleaseSpinLock(&PrivateCacheMap->ReadAheadSpinLock, OldIrql);
218  return;
219  }
220  }
221 
222  /* If read ahead isn't active yet */
223  if (!PrivateCacheMap->Flags.ReadAheadActive)
224  {
225  PWORK_QUEUE_ENTRY WorkItem;
226 
227  /* It's active now!
228  * Be careful with the mask, you don't want to mess with node code
229  */
230  InterlockedOr((volatile long *)&PrivateCacheMap->UlongFlags, PRIVATE_CACHE_MAP_READ_AHEAD_ACTIVE);
231  KeReleaseSpinLock(&PrivateCacheMap->ReadAheadSpinLock, OldIrql);
232 
233  /* Get a work item */
234  WorkItem = ExAllocateFromNPagedLookasideList(&CcTwilightLookasideList);
235  if (WorkItem != NULL)
236  {
237  /* Reference our FO so that it doesn't go in between */
239 
240  /* We want to do read ahead! */
241  WorkItem->Function = ReadAhead;
242  WorkItem->Parameters.Read.FileObject = FileObject;
243 
244  /* Queue in the read ahead dedicated queue */
246 
247  return;
248  }
249 
250  /* Fail path: lock again, and revert read ahead active */
251  KeAcquireSpinLock(&PrivateCacheMap->ReadAheadSpinLock, &OldIrql);
252  InterlockedAnd((volatile long *)&PrivateCacheMap->UlongFlags, ~PRIVATE_CACHE_MAP_READ_AHEAD_ACTIVE);
253  }
254 
255  /* Done (fail) */
256  KeReleaseSpinLock(&PrivateCacheMap->ReadAheadSpinLock, OldIrql);
257 }
258 
259 /*
260  * @implemented
261  */
262 VOID
263 NTAPI
266  IN BOOLEAN DisableReadAhead,
267  IN BOOLEAN DisableWriteBehind
268  )
269 {
270  KIRQL OldIrql;
271  PROS_SHARED_CACHE_MAP SharedCacheMap;
272 
273  CCTRACE(CC_API_DEBUG, "FileObject=%p DisableReadAhead=%d DisableWriteBehind=%d\n",
274  FileObject, DisableReadAhead, DisableWriteBehind);
275 
276  SharedCacheMap = FileObject->SectionObjectPointer->SharedCacheMap;
277 
279 
280  if (DisableReadAhead)
281  {
282  SetFlag(SharedCacheMap->Flags, READAHEAD_DISABLED);
283  }
284  else
285  {
286  ClearFlag(SharedCacheMap->Flags, READAHEAD_DISABLED);
287  }
288 
289  if (DisableWriteBehind)
290  {
291  /* FIXME: also set flag 0x200 */
292  SetFlag(SharedCacheMap->Flags, WRITEBEHIND_DISABLED);
293  }
294  else
295  {
296  ClearFlag(SharedCacheMap->Flags, WRITEBEHIND_DISABLED);
297  }
299 }
300 
301 /*
302  * @unimplemented
303  */
304 VOID
305 NTAPI
307  IN PVOID Bcb,
308  IN PVOID Owner
309  )
310 {
311  PINTERNAL_BCB iBcb = Bcb;
312 
313  CCTRACE(CC_API_DEBUG, "Bcb=%p Owner=%p\n",
314  Bcb, Owner);
315 
317  {
318  DPRINT1("Current thread doesn't own resource!\n");
319  return;
320  }
321 
323 }
324 
325 /*
326  * @implemented
327  */
328 VOID
329 NTAPI
332  IN ULONG DirtyPageThreshold
333  )
334 {
336  PROS_SHARED_CACHE_MAP SharedCacheMap;
337 
338  CCTRACE(CC_API_DEBUG, "FileObject=%p DirtyPageThreshold=%lu\n",
339  FileObject, DirtyPageThreshold);
340 
341  SharedCacheMap = FileObject->SectionObjectPointer->SharedCacheMap;
342  if (SharedCacheMap != NULL)
343  {
344  SharedCacheMap->DirtyPageThreshold = DirtyPageThreshold;
345  }
346 
347  Fcb = FileObject->FsContext;
349  {
351  }
352 }
353 
354 /*
355  * @implemented
356  */
357 VOID
358 NTAPI
361  IN ULONG Granularity
362  )
363 {
364  PPRIVATE_CACHE_MAP PrivateMap;
365 
366  CCTRACE(CC_API_DEBUG, "FileObject=%p Granularity=%lu\n",
367  FileObject, Granularity);
368 
369  PrivateMap = FileObject->PrivateCacheMap;
370  PrivateMap->ReadAheadMask = Granularity - 1;
371 }
VOID CcPostWorkQueue(IN PWORK_QUEUE_ENTRY WorkItem, IN PLIST_ENTRY WorkQueue)
Definition: lazywrite.c:53
INIT_FUNCTION BOOLEAN NTAPI CcInitializeCacheManager(VOID)
Definition: cacheman.c:46
#define UNIMPLEMENTED_ONCE
Definition: debug.h:138
#define IN
Definition: typedefs.h:38
#define InterlockedAnd
Definition: interlocked.h:62
#define TRUE
Definition: types.h:120
ULONG CcDirtyPageThreshold
Definition: view.c:59
LIST_ENTRY List
Definition: extypes.h:203
FAST_MUTEX CompletedTracesLock
Definition: cc.h:164
PFSN_PREFETCHER_GLOBALS CcPfGlobals
Definition: cacheman.c:18
ULONG CcNumberWorkerThreads
Definition: lazywrite.c:48
IN PVCB IN VBO IN ULONG OUT PBCB * Bcb
Definition: fatprocs.h:402
#define ROUND_UP(n, align)
Definition: eventvwr.h:31
#define BooleanFlagOn(F, SF)
Definition: ext2fs.h:183
union _WORK_QUEUE_ENTRY::@1676 Parameters
NTSYSAPI ULONG __cdecl DbgPrintEx(_In_ ULONG ComponentId, _In_ ULONG Level, _In_z_ _Printf_format_string_ PCSTR Format,...)
struct _WORK_QUEUE_ENTRY::@1676::@1677 Read
#define InsertTailList(ListHead, Entry)
VOID NTAPI CcSetBcbOwnerPointer(IN PVOID Bcb, IN PVOID Owner)
Definition: cacheman.c:306
VOID NTAPI CcShutdownSystem(VOID)
Definition: cacheman.c:116
ULONG ReadAheadMask
Definition: cctypes.h:75
_Must_inspect_result_ _In_ PDEVICE_OBJECT _In_ PSECTION_OBJECT_POINTERS SectionObjectPointer
Definition: fsrtlfuncs.h:1368
enum _MM_SYSTEM_SIZE MM_SYSTEMSIZE
UCHAR KIRQL
Definition: env_spec_w32.h:591
VOID NTAPI CcSetAdditionalCacheAttributes(IN PFILE_OBJECT FileObject, IN BOOLEAN DisableReadAhead, IN BOOLEAN DisableWriteBehind)
Definition: cacheman.c:264
LIST_ENTRY CcIdleWorkerThreadList
Definition: lazywrite.c:41
GLsizei GLenum const GLvoid GLsizei GLenum GLbyte GLbyte GLbyte GLdouble GLdouble GLdouble GLfloat GLfloat GLfloat GLint GLint GLint GLshort GLshort GLshort GLubyte GLubyte GLubyte GLuint GLuint GLuint GLushort GLushort GLushort GLbyte GLbyte GLbyte GLbyte GLdouble GLdouble GLdouble GLdouble GLfloat GLfloat GLfloat GLfloat GLint GLint GLint GLint GLshort GLshort GLshort GLshort GLubyte GLubyte GLubyte GLubyte GLuint GLuint GLuint GLuint GLushort GLushort GLushort GLushort GLboolean const GLdouble const GLfloat const GLint const GLshort const GLbyte const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLdouble const GLfloat const GLfloat const GLint const GLint const GLshort const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort GLenum GLenum GLenum GLfloat GLenum GLint GLenum GLenum GLenum GLfloat GLenum GLenum GLint GLenum GLfloat GLenum GLint GLint GLushort GLenum GLenum GLfloat GLenum GLenum GLint GLfloat const GLubyte GLenum GLenum GLenum const GLfloat GLenum GLenum const GLint GLenum GLint GLint GLsizei GLsizei GLint GLenum GLenum const GLvoid GLenum GLenum const GLfloat GLenum GLenum const GLint GLenum GLenum const GLdouble GLenum GLenum const GLfloat GLenum GLenum const GLint GLsizei GLuint GLfloat GLuint GLbitfield GLfloat GLint GLuint GLboolean GLenum GLfloat GLenum GLbitfield GLenum GLfloat GLfloat GLint GLint const GLfloat GLenum GLfloat GLfloat GLint GLint GLfloat GLfloat GLint GLint const GLfloat GLint GLfloat GLfloat GLint GLfloat GLfloat GLint GLfloat GLfloat const GLdouble const GLfloat const GLdouble const GLfloat GLint i
Definition: glfuncs.h:248
LAZY_WRITER LazyWriter
Definition: lazywrite.c:37
NTSTATUS(* NTAPI)(IN PFILE_FULL_EA_INFORMATION EaBuffer, IN ULONG EaLength, OUT PULONG ErrorOffset)
Definition: IoEaTest.cpp:117
unsigned char Function
Definition: cc.h:272
KDPC ScanDpc
Definition: cc.h:243
LIST_ENTRY CcRegularWorkQueue
Definition: lazywrite.c:40
unsigned char BOOLEAN
smooth NULL
Definition: ftsmooth.c:416
BOOLEAN NTAPI ExIsResourceAcquiredExclusiveLite(IN PERESOURCE Resource)
Definition: resource.c:1619
KTIMER ScanTimer
Definition: cc.h:244
VOID NTAPI KeInitializeTimer(OUT PKTIMER Timer)
Definition: timerobj.c:233
#define WRITEBEHIND_DISABLED
Definition: cc.h:199
_Inout_ PFILE_OBJECT FileObject
Definition: cdprocs.h:593
#define FO_SEQUENTIAL_ONLY
Definition: iotypes.h:1737
KSPIN_LOCK ReadAheadSpinLock
Definition: cctypes.h:83
#define CC_API_DEBUG
Definition: cc.h:11
#define ExInitializeWorkItem(Item, Routine, Context)
Definition: exfuncs.h:265
VOID NTAPI ExInitializeNPagedLookasideList(IN PNPAGED_LOOKASIDE_LIST Lookaside, IN PALLOCATE_FUNCTION Allocate OPTIONAL, IN PFREE_FUNCTION Free OPTIONAL, IN ULONG Flags, IN SIZE_T Size, IN ULONG Tag, IN USHORT Depth)
Definition: lookas.c:222
LIST_ENTRY CompletedTraces
Definition: cc.h:163
INIT_FUNCTION VOID NTAPI CcInitView(VOID)
Definition: view.c:1535
FORCEINLINE VOID ExInitializeFastMutex(_Out_ PFAST_MUTEX FastMutex)
Definition: exfuncs.h:274
#define KeAcquireSpinLock(sl, irql)
Definition: env_spec_w32.h:609
MM_SYSTEMSIZE NTAPI MmQuerySystemSize(VOID)
Definition: mmsup.c:257
VOID NTAPI CcScheduleReadAhead(IN PFILE_OBJECT FileObject, IN PLARGE_INTEGER FileOffset, IN ULONG Length)
Definition: cacheman.c:158
LARGE_INTEGER NTAPI CcGetFlushedValidData(IN PSECTION_OBJECT_POINTERS SectionObjectPointer, IN BOOLEAN BcbListHeld)
Definition: cacheman.c:126
VOID FASTCALL KeReleaseQueuedSpinLock(IN KSPIN_LOCK_QUEUE_NUMBER LockNumber, IN KIRQL OldIrql)
Definition: spinlock.c:154
#define CCTRACE(x, fmt,...)
Definition: cc.h:36
_In_opt_ PFILE_OBJECT _In_opt_ PETHREAD Thread
Definition: fltkernel.h:2653
_In_ ULONG _In_ ULONG _In_ ULONG Length
Definition: ntddpcm.h:101
Definition: cc.h:250
VOID NTAPI CcSetDirtyPageThreshold(IN PFILE_OBJECT FileObject, IN ULONG DirtyPageThreshold)
Definition: cacheman.c:330
* PFILE_OBJECT
Definition: iotypes.h:1955
#define FSRTL_FLAG_LIMIT_MODIFIED_PAGES
Definition: fsrtltypes.h:47
ULONG Flags
Definition: ntfs.h:532
#define ExAllocatePoolWithTag(hernya, size, tag)
Definition: env_spec_w32.h:350
VOID NTAPI CcWorkerThread(IN PVOID Parameter)
Definition: lazywrite.c:226
_Requires_lock_held_ Interrupt _Releases_lock_ Interrupt _In_ _IRQL_restores_ KIRQL OldIrql
Definition: kefuncs.h:803
BOOLEAN CcPfEnablePrefetcher
Definition: cacheman.c:17
LARGE_INTEGER FileOffset1
Definition: cctypes.h:77
#define InterlockedOr
Definition: interlocked.h:224
MM_SYSTEMSIZE CcCapturedSystemSize
Definition: cacheman.c:19
PRIVATE_CACHE_MAP_FLAGS Flags
Definition: cctypes.h:72
VOID NTAPI ExSetResourceOwnerPointer(IN PERESOURCE Resource, IN PVOID OwnerPointer)
Definition: resource.c:2045
INIT_FUNCTION VOID NTAPI CcPfInitializePrefetcher(VOID)
Definition: cacheman.c:28
KIRQL FASTCALL KeAcquireQueuedSpinLock(IN KSPIN_LOCK_QUEUE_NUMBER LockNumber)
Definition: spinlock.c:108
LIST_ENTRY CcExpressWorkQueue
Definition: lazywrite.c:39
#define READAHEAD_DISABLED
Definition: cc.h:198
ClearFlag(Dirent->Flags, DIRENT_FLAG_NOT_PERSISTENT)
PVOID NTAPI CcRemapBcb(IN PVOID Bcb)
Definition: cacheman.c:144
#define SetFlag(_F, _SF)
Definition: ext2fs.h:187
PFN_COUNT MmNumberOfPhysicalPages
Definition: init.c:48
VOID NTAPI CcSetReadAheadGranularity(IN PFILE_OBJECT FileObject, IN ULONG Granularity)
Definition: cacheman.c:359
LARGE_INTEGER ReadAheadOffset[2]
Definition: cctypes.h:81
_In_ PFCB _In_ LONGLONG FileOffset
Definition: cdprocs.h:151
#define InitializeListHead(ListHead)
Definition: env_spec_w32.h:944
_Out_writes_bytes_to_opt_ AbsoluteSecurityDescriptorSize PSECURITY_DESCRIPTOR _Inout_ PULONG _Out_writes_bytes_to_opt_ DaclSize PACL _Inout_ PULONG _Out_writes_bytes_to_opt_ SaclSize PACL _Inout_ PULONG _Out_writes_bytes_to_opt_ OwnerSize PSID Owner
Definition: rtlfuncs.h:1557
#define KeReleaseSpinLock(sl, irql)
Definition: env_spec_w32.h:627
#define DPRINT1
Definition: precomp.h:8
LARGE_INTEGER FileOffset2
Definition: cctypes.h:79
#define CcBugCheck(A, B, C)
Definition: cc.h:502
#define ObReferenceObject
Definition: obfuncs.h:204
unsigned int ULONG
Definition: retypes.h:1
Definition: cc.h:277
ULONG NTAPI ExIsResourceAcquiredSharedLite(IN PERESOURCE Resource)
Definition: resource.c:1658
ULONG ExCriticalWorkerThreads
Definition: work.c:38
#define UNIMPLEMENTED
Definition: debug.h:114
ERESOURCE Lock
Definition: cc.h:232
#define RtlZeroMemory(Destination, Length)
Definition: typedefs.h:261
VOID NTAPI CcScanDpc(IN PKDPC Dpc, IN PVOID DeferredContext, IN PVOID SystemArgument1, IN PVOID SystemArgument2)
Definition: lazywrite.c:95
LIST_ENTRY ActiveTraces
Definition: cc.h:160
static ULONG BugCheckFileId
Definition: cacheman.c:21
_In_ PFCB Fcb
Definition: cdprocs.h:151
VOID NTAPI KeInitializeDpc(IN PKDPC Dpc, IN PKDEFERRED_ROUTINE DeferredRoutine, IN PVOID DeferredContext)
Definition: dpc.c:711
ULONG UlongFlags
Definition: cctypes.h:73
#define PRIVATE_CACHE_MAP_READ_AHEAD_ACTIVE
Definition: cctypes.h:64
LIST_ENTRY CcPostTickWorkQueue
Definition: lazywrite.c:42
#define DPFLTR_TRACE_LEVEL
Definition: kdtypes.h:32
ULONG DirtyPageThreshold
Definition: cc.h:185
LIST_ENTRY WorkQueue
Definition: cc.h:242
ULONG ReadAheadLength[2]
Definition: cctypes.h:82
NPAGED_LOOKASIDE_LIST CcTwilightLookasideList
Definition: lazywrite.c:38
LONGLONG QuadPart
Definition: typedefs.h:112