ReactOS  0.4.14-dev-583-g2a1ba2c
cachesub.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/cache/cachesub.c
5  * PURPOSE: Logging and configuration routines
6  * PROGRAMMERS: Alex Ionescu (alex.ionescu@reactos.org)
7  * Art Yerkes
8  */
9 
10 /* INCLUDES *******************************************************************/
11 
12 #include <ntoskrnl.h>
13 #include "newcc.h"
14 #include "section/newmm.h"
15 #define NDEBUG
16 #include <debug.h>
17 
18 /* STRUCTURES *****************************************************************/
19 
21 {
27 
28 /* FUNCTIONS ******************************************************************/
29 
31 NTAPI
33 
34 VOID
35 NTAPI
37  IN ULONG Granularity)
38 {
39  PNOCC_CACHE_MAP Map = (PNOCC_CACHE_MAP)FileObject->SectionObjectPointer->SharedCacheMap;
40  if (Map)
41  {
42  Map->ReadAheadGranularity = Granularity;
43  }
44 }
45 
46 VOID
47 NTAPI
49 {
52  PNOCC_CACHE_MAP Map = (PNOCC_CACHE_MAP)WorkItem->FileObject->SectionObjectPointer->SharedCacheMap;
53 
54  DPRINT1("Reading ahead %08x%08x:%x %wZ\n",
55  WorkItem->FileOffset.HighPart,
56  WorkItem->FileOffset.LowPart,
57  WorkItem->Length,
58  &WorkItem->FileObject->FileName);
59 
60  Offset.HighPart = WorkItem->FileOffset.HighPart;
61  Offset.LowPart = PAGE_ROUND_DOWN(WorkItem->FileOffset.LowPart);
62  if (Map)
63  {
64  PLIST_ENTRY ListEntry;
65  volatile char *chptr;
66  PNOCC_BCB Bcb;
67  for (ListEntry = Map->AssociatedBcb.Flink;
68  ListEntry != &Map->AssociatedBcb;
69  ListEntry = ListEntry->Flink)
70  {
71  Bcb = CONTAINING_RECORD(ListEntry, NOCC_BCB, ThisFileList);
72  if ((Offset.QuadPart + WorkItem->Length < Bcb->FileOffset.QuadPart) ||
73  (Bcb->FileOffset.QuadPart + Bcb->Length < Offset.QuadPart))
74  continue;
75  for (chptr = Bcb->BaseAddress, Offset = Bcb->FileOffset;
76  chptr < ((PCHAR)Bcb->BaseAddress) + Bcb->Length &&
77  Offset.QuadPart <
78  WorkItem->FileOffset.QuadPart + WorkItem->Length;
79  chptr += PAGE_SIZE, Offset.QuadPart += PAGE_SIZE)
80  {
81  *chptr ^= 0;
82  }
83  }
84  }
86  ExFreePool(WorkItem);
87  DPRINT("Done\n");
88 }
89 
90 VOID
91 NTAPI
94  IN ULONG Length)
95 {
97 
98  DPRINT("Schedule read ahead %08x%08x:%x %wZ\n",
99  FileOffset->HighPart,
100  FileOffset->LowPart,
101  Length,
102  &FileObject->FileName);
103 
104  WorkItem = ExAllocatePool(NonPagedPool, sizeof(*WorkItem));
105  if (!WorkItem) KeBugCheck(0);
107  WorkItem->FileObject = FileObject;
108  WorkItem->FileOffset = *FileOffset;
109  WorkItem->Length = Length;
110 
111  ExInitializeWorkItem(&WorkItem->WorkItem,
113  WorkItem);
114 
116  DPRINT("Done\n");
117 }
118 
119 VOID
120 NTAPI
123 {
124  PNOCC_BCB Bcb = (PNOCC_BCB)BcbVoid;
125  Bcb->Dirty = TRUE;
126 }
127 
129 NTAPI
131  IN BOOLEAN CcInternalCaller)
132 {
133  LARGE_INTEGER Result = {{0}};
135  return Result;
136 }
137 
138 
139 
140 VOID
141 NTAPI
144  IN ULONG Length,
146  BOOLEAN Delete,
147  const char *File,
148  int Line)
149 {
150  PNOCC_BCB Bcb = NULL;
151  LARGE_INTEGER LowerBound, UpperBound;
152  PLIST_ENTRY ListEntry;
153  IO_STATUS_BLOCK IOSB;
154 
155  RtlZeroMemory(&IOSB, sizeof(IO_STATUS_BLOCK));
156 
157  DPRINT("CcFlushCache (while file) (%s:%d)\n", File, Line);
158 
159  if (FileOffset && Length)
160  {
161  LowerBound.QuadPart = FileOffset->QuadPart;
162  UpperBound.QuadPart = LowerBound.QuadPart + Length;
163  }
164  else
165  {
166  LowerBound.QuadPart = 0;
167  UpperBound.QuadPart = 0x7fffffffffffffffull;
168  }
169 
170  CcpLock();
171  ListEntry = Map->AssociatedBcb.Flink;
172 
173  while (ListEntry != &Map->AssociatedBcb)
174  {
175  Bcb = CONTAINING_RECORD(ListEntry, NOCC_BCB, ThisFileList);
177 
178  if (Bcb->FileOffset.QuadPart + Bcb->Length >= LowerBound.QuadPart &&
179  Bcb->FileOffset.QuadPart < UpperBound.QuadPart)
180  {
181  DPRINT("Bcb #%x (@%08x%08x)\n",
183  Bcb->FileOffset.u.HighPart, Bcb->FileOffset.u.LowPart);
184 
185  Bcb->RefCount++;
186  CcpUnlock();
187  MiFlushMappedSection(Bcb->BaseAddress,
188  &Bcb->FileOffset,
189  &Map->FileSizes.FileSize,
190  Bcb->Dirty);
191  CcpLock();
192  Bcb->RefCount--;
193 
194  Bcb->Dirty = FALSE;
195 
196  ListEntry = ListEntry->Flink;
197  if (Delete && Bcb->RefCount < 2)
198  {
199  Bcb->RefCount = 1;
201  }
202  else
203  {
205  }
206  }
207  else
208  {
209  ListEntry = ListEntry->Flink;
211  }
212 
213  DPRINT("End loop\n");
214  }
215  CcpUnlock();
216 
217  if (IoStatus) *IoStatus = IOSB;
218 }
219 
220 VOID
221 NTAPI
224  IN ULONG Length,
226 {
228 
229  /* Not cached */
230  if (!Map)
231  {
232  if (IoStatus)
233  {
234  IoStatus->Status = STATUS_SUCCESS;
235  IoStatus->Information = 0;
236  }
237  return;
238  }
239 
241 }
242 
243 BOOLEAN
244 NTAPI
247 {
249  PNOCC_BCB Bcb;
251  IO_STATUS_BLOCK IOSB;
252  BOOLEAN Result = TRUE;
253 
254  if (!Map) return TRUE;
255 
256  for (Entry = Map->AssociatedBcb.Flink;
257  Entry != &Map->AssociatedBcb;
258  Entry = Entry->Flink)
259  {
260  Bcb = CONTAINING_RECORD(Entry, NOCC_BCB, ThisFileList);
261 
262  if (!Bcb->Dirty) continue;
263 
264  switch (FlushType)
265  {
266  case MmFlushForDelete:
268  &Bcb->FileOffset,
269  Bcb->Length,
270  FALSE);
271  break;
272  case MmFlushForWrite:
274  &Bcb->FileOffset,
275  Bcb->Length,
276  &IOSB);
277  break;
278  }
279  }
280 
281  return Result;
282 }
283 
284 /* Always succeeds for us */
285 PVOID
286 NTAPI
288 {
290  CcpLock();
293  CcpUnlock();
294  return Bcb;
295 }
296 
297 VOID
298 NTAPI
300 {
301  ULONG i, Result;
303 
304  DPRINT1("CC: Shutdown\n");
305 
306  for (i = 0; i < CACHE_NUM_SECTIONS; i++)
307  {
309  if (Bcb->SectionObject)
310  {
311  DPRINT1("Evicting #%02x %08x%08x %wZ\n",
312  i,
313  Bcb->FileOffset.u.HighPart,
314  Bcb->FileOffset.u.LowPart,
315  &MmGetFileObjectForSection((PROS_SECTION_OBJECT)Bcb->SectionObject)->FileName);
316 
317  CcpFlushCache(Bcb->Map, NULL, 0, NULL, TRUE);
318  Bcb->Dirty = FALSE;
319  }
320  }
321 
322  /* Evict all section pages */
323  Status = MiRosTrimCache(~0, 0, &Result);
324 
325  DPRINT1("Done (Evicted %d, Status %x)\n", Result, Status);
326 }
327 
328 
329 VOID
330 NTAPI
332 {
334  CcpLock();
336  DPRINT("CcRepinBcb(#%x)\n", Number);
338  CcpUnlock();
339 }
340 
341 VOID
342 NTAPI
344  IN BOOLEAN WriteThrough,
346 {
347  PNOCC_BCB RealBcb = (PNOCC_BCB)Bcb;
348 
349  if (WriteThrough)
350  {
351  DPRINT("BCB #%x\n", RealBcb - CcCacheSections);
352 
353  CcpFlushCache(RealBcb->Map,
354  &RealBcb->FileOffset,
355  RealBcb->Length,
356  IoStatus,
357  RealBcb->Dirty);
358  }
359 
360  CcUnpinData(Bcb);
361 }
362 
363 /* EOF */
BOOLEAN NTAPI CcpUnpinData(PNOCC_BCB Bcb, BOOLEAN ActuallyRelease)
MMFLUSH_TYPE
Definition: mmtypes.h:183
signed char * PCHAR
Definition: retypes.h:7
BOOLEAN NTAPI CcPurgeCacheSection(IN PSECTION_OBJECT_POINTERS SectionObjectPointer, IN OPTIONAL PLARGE_INTEGER FileOffset, IN ULONG Length, IN BOOLEAN UninitializeCacheMaps)
Definition: fssup.c:384
PFILE_OBJECT NTAPI MmGetFileObjectForSection(IN PVOID Section)
Definition: section.c:1681
#define IN
Definition: typedefs.h:38
#define TRUE
Definition: types.h:120
VOID NTAPI ExQueueWorkItem(IN PWORK_QUEUE_ITEM WorkItem, IN WORK_QUEUE_TYPE QueueType)
Definition: work.c:717
BOOLEAN NTAPI CcFlushImageSection(PSECTION_OBJECT_POINTERS SectionObjectPointer, MMFLUSH_TYPE FlushType)
Definition: cachesub.c:245
IN PVCB IN VBO IN ULONG OUT PBCB * Bcb
Definition: fatprocs.h:402
PVOID NTAPI CcRemapBcb(IN PVOID Bcb)
Definition: cachesub.c:287
#define CcpLock()
Definition: newcc.h:143
VOID NTAPI CcFlushCache(IN PSECTION_OBJECT_POINTERS SectionObjectPointer, IN OPTIONAL PLARGE_INTEGER FileOffset, IN ULONG Length, OUT OPTIONAL PIO_STATUS_BLOCK IoStatus)
Definition: cachesub.c:222
IN PLARGE_INTEGER IN ULONG IN BOOLEAN IN ULONG IN BOOLEAN OUT PIO_STATUS_BLOCK IoStatus
Definition: fatprocs.h:2650
VOID NTAPI CcSetDirtyPinnedData(IN PVOID BcbVoid, IN OPTIONAL PLARGE_INTEGER Lsn)
Definition: cachesub.c:121
ULONG Length
Definition: newcc.h:11
#define CACHE_NUM_SECTIONS
Definition: newcc.h:130
LONG NTSTATUS
Definition: precomp.h:26
#define CcpFlushCache(M, F, L, I, D)
Definition: newcc.h:96
VOID NTAPI CcUnpinData(IN PVOID Bcb)
Definition: pinsup.c:955
#define PAGE_ROUND_DOWN(x)
Definition: mmtypes.h:36
VOID NTAPI CcpReadAhead(PVOID Context)
Definition: cachesub.c:48
VOID CcpReferenceCache(ULONG Sector)
Definition: pinsup.c:368
VOID NTAPI ObDereferenceObject(IN PVOID Object)
Definition: obref.c:375
NTSTATUS MiRosTrimCache(ULONG Target, ULONG Priority, PULONG NrFreed)
Definition: swapout.c:633
Definition: newcc.h:3
NOCC_BCB CcCacheSections[CACHE_NUM_SECTIONS]
Definition: pinsup.c:98
WORK_QUEUE_ITEM WorkItem
Definition: cachesub.c:22
_Must_inspect_result_ _In_ PDEVICE_OBJECT _In_ PSECTION_OBJECT_POINTERS SectionObjectPointer
Definition: fsrtlfuncs.h:1368
VOID NTAPI CcScheduleReadAhead(IN PFILE_OBJECT FileObject, IN PLARGE_INTEGER FileOffset, IN ULONG Length)
Definition: cachesub.c:92
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
NTSTATUS(* NTAPI)(IN PFILE_FULL_EA_INFORMATION EaBuffer, IN ULONG EaLength, OUT PULONG ErrorOffset)
Definition: IoEaTest.cpp:117
_In_ ULONG _In_ ULONG Offset
Definition: ntddpcm.h:101
BOOLEAN Dirty
Definition: newcc.h:13
PFILE_OBJECT FileObject
Definition: cachesub.c:23
LARGE_INTEGER FileOffset
Definition: cachesub.c:24
unsigned char BOOLEAN
smooth NULL
Definition: ftsmooth.c:416
_At_(*)(_In_ PWSK_CLIENT Client, _In_opt_ PUNICODE_STRING NodeName, _In_opt_ PUNICODE_STRING ServiceName, _In_opt_ ULONG NameSpace, _In_opt_ GUID *Provider, _In_opt_ PADDRINFOEXW Hints, _Outptr_ PADDRINFOEXW *Result, _In_opt_ PEPROCESS OwningProcess, _In_opt_ PETHREAD OwningThread, _Inout_ PIRP Irp Result)(Mem)) NTSTATUS(WSKAPI *PFN_WSK_GET_ADDRESS_INFO
Definition: wsk.h:426
void DPRINT(...)
Definition: polytest.cpp:61
VOID NTAPI KeBugCheck(ULONG BugCheckCode)
Definition: bug.c:1507
LIST_ENTRY AssociatedBcb
Definition: newcc.h:29
_Inout_ PFILE_OBJECT FileObject
Definition: cdprocs.h:593
PDEVICE_OBJECT NTAPI MmGetDeviceObjectForFile(IN PFILE_OBJECT FileObject)
Definition: io.c:62
IN PFCB IN FAT_FLUSH_TYPE FlushType
Definition: fatprocs.h:1071
PFLT_MESSAGE_WAITER_QUEUE CONTAINING_RECORD(Csq, DEVICE_EXTENSION, IrpQueue)) -> WaiterQ.mLock) _IRQL_raises_(DISPATCH_LEVEL) VOID NTAPI FltpAcquireMessageWaiterLock(_In_ PIO_CSQ Csq, _Out_ PKIRQL Irql)
Definition: Messaging.c:560
struct _LIST_ENTRY * Flink
Definition: typedefs.h:119
VOID NTAPI CcShutdownSystem(VOID)
Definition: cachesub.c:299
#define ExInitializeWorkItem(Item, Routine, Context)
Definition: exfuncs.h:265
if(!(yy_init))
Definition: macro.lex.yy.c:714
VOID NTAPI CcUnpinRepinnedBcb(IN PVOID Bcb, IN BOOLEAN WriteThrough, OUT PIO_STATUS_BLOCK IoStatus)
Definition: cachesub.c:343
#define MiFlushMappedSection(A, O, S, D)
Definition: newmm.h:402
_Must_inspect_result_ NTSYSAPI BOOLEAN NTAPI RtlTestBit(_In_ PRTL_BITMAP BitMapHeader, _In_range_(<, BitMapHeader->SizeOfBitMap) ULONG BitNumber)
Definition: bitmap.c:434
_In_ ULONG _In_ ULONG _In_ ULONG Length
Definition: ntddpcm.h:101
* PFILE_OBJECT
Definition: iotypes.h:1955
ASSERT((InvokeOnSuccess||InvokeOnError||InvokeOnCancel) ?(CompletionRoutine !=NULL) :TRUE)
Definition: ncftp.h:79
PRTL_BITMAP CcCacheBitmap
Definition: pinsup.c:100
#define ExAllocatePool(type, size)
Definition: fbtusb.h:44
ULONG LowPart
Definition: typedefs.h:104
#define PAGE_SIZE
Definition: env_spec_w32.h:49
Definition: typedefs.h:117
LARGE_INTEGER FileOffset
Definition: newcc.h:10
_In_opt_ PENTER_STATE_SYSTEM_HANDLER _In_opt_ PVOID _In_ LONG _In_opt_ LONG volatile * Number
Definition: ntpoapi.h:204
Status
Definition: gdiplustypes.h:24
struct _NOCC_CACHE_MAP * Map
Definition: newcc.h:8
VOID NTAPI _CcpFlushCache(IN PNOCC_CACHE_MAP Map, IN OPTIONAL PLARGE_INTEGER FileOffset, IN ULONG Length, OUT OPTIONAL PIO_STATUS_BLOCK IoStatus, BOOLEAN Delete, const char *File, int Line)
Definition: cachesub.c:142
VOID CcpDereferenceCache(ULONG Sector, BOOLEAN Immediate)
Definition: pinsup.c:213
struct _WORK_QUEUE_WITH_READ_AHEAD * PWORK_QUEUE_WITH_READ_AHEAD
_In_ PFCB _In_ LONGLONG FileOffset
Definition: cdprocs.h:151
struct _NOCC_BCB * PNOCC_BCB
#define UNIMPLEMENTED_DBGBREAK(...)
Definition: debug.h:57
#define DPRINT1
Definition: precomp.h:8
#define OUT
Definition: typedefs.h:39
#define ObReferenceObject
Definition: obfuncs.h:204
unsigned int ULONG
Definition: retypes.h:1
#define RtlZeroMemory(Destination, Length)
Definition: typedefs.h:261
#define CcpUnlock()
Definition: newcc.h:144
VOID NTAPI CcRepinBcb(IN PVOID Bcb)
Definition: cachesub.c:331
struct _WORK_QUEUE_WITH_READ_AHEAD WORK_QUEUE_WITH_READ_AHEAD
Definition: File.h:15
ULONG ReadAheadGranularity
Definition: newcc.h:38
return STATUS_SUCCESS
Definition: btrfs.c:2938
BOOL Delete(LPCTSTR ServiceName)
Definition: delete.c:12
base of all file and directory entries
Definition: entries.h:82
#define ExFreePool(addr)
Definition: env_spec_w32.h:352
LONGLONG QuadPart
Definition: typedefs.h:112
LARGE_INTEGER NTAPI CcGetFlushedValidData(IN PSECTION_OBJECT_POINTERS SectionObjectPointer, IN BOOLEAN CcInternalCaller)
Definition: cachesub.c:130
struct _NOCC_CACHE_MAP * PNOCC_CACHE_MAP
WORKER_THREAD_ROUTINE * PWORKER_THREAD_ROUTINE
Definition: extypes.h:200
PULONG MinorVersion OPTIONAL
Definition: CrossNt.h:68
VOID NTAPI CcSetReadAheadGranularity(IN PFILE_OBJECT FileObject, IN ULONG Granularity)
Definition: cachesub.c:36