[NTOSKRNL] Add an helper for marking a VACB clean (ie, not dirty).
[reactos.git] / ntoskrnl / include / internal / cc.h
1 #pragma once
2
3 //
4 // Define this if you want debugging support
5 //
6 #define _CC_DEBUG_ 0x00
7
8 //
9 // These define the Debug Masks Supported
10 //
11 #define CC_API_DEBUG 0x01
12
13 //
14 // Debug/Tracing support
15 //
16 #if _CC_DEBUG_
17 #ifdef NEW_DEBUG_SYSTEM_IMPLEMENTED // enable when Debug Filters are implemented
18 #define CCTRACE(x, ...) \
19 { \
20 DbgPrintEx("%s [%.16s] - ", \
21 __FUNCTION__, \
22 PsGetCurrentProcess()->ImageFileName); \
23 DbgPrintEx(__VA_ARGS__); \
24 }
25 #else
26 #define CCTRACE(x, ...) \
27 if (x & CcRosTraceLevel) \
28 { \
29 DbgPrint("%s [%.16s] - ", \
30 __FUNCTION__, \
31 PsGetCurrentProcess()->ImageFileName); \
32 DbgPrint(__VA_ARGS__); \
33 }
34 #endif
35 #else
36 #define CCTRACE(x, fmt, ...) DPRINT(fmt, ##__VA_ARGS__)
37 #endif
38
39 //
40 // Global Cc Data
41 //
42 extern ULONG CcRosTraceLevel;
43 extern LIST_ENTRY DirtyVacbListHead;
44 extern ULONG CcDirtyPageThreshold;
45 extern ULONG CcTotalDirtyPages;
46 extern LIST_ENTRY CcDeferredWrites;
47 extern KSPIN_LOCK CcDeferredWriteSpinLock;
48
49 typedef struct _PF_SCENARIO_ID
50 {
51 WCHAR ScenName[30];
52 ULONG HashId;
53 } PF_SCENARIO_ID, *PPF_SCENARIO_ID;
54
55 typedef struct _PF_LOG_ENTRY
56 {
57 ULONG FileOffset:30;
58 ULONG Type:2;
59 union
60 {
61 ULONG FileKey;
62 ULONG FileSequenceNumber;
63 };
64 } PF_LOG_ENTRY, *PPF_LOG_ENTRY;
65
66 typedef struct _PFSN_LOG_ENTRIES
67 {
68 LIST_ENTRY TraceBuffersLink;
69 LONG NumEntries;
70 LONG MaxEntries;
71 PF_LOG_ENTRY Entries[ANYSIZE_ARRAY];
72 } PFSN_LOG_ENTRIES, *PPFSN_LOG_ENTRIES;
73
74 typedef struct _PF_SECTION_INFO
75 {
76 ULONG FileKey;
77 ULONG FileSequenceNumber;
78 ULONG FileIdLow;
79 ULONG FileIdHigh;
80 } PF_SECTION_INFO, *PPF_SECTION_INFO;
81
82 typedef struct _PF_TRACE_HEADER
83 {
84 ULONG Version;
85 ULONG MagicNumber;
86 ULONG Size;
87 PF_SCENARIO_ID ScenarioId;
88 ULONG ScenarioType; // PF_SCENARIO_TYPE
89 ULONG EventEntryIdxs[8];
90 ULONG NumEventEntryIdxs;
91 ULONG TraceBufferOffset;
92 ULONG NumEntries;
93 ULONG SectionInfoOffset;
94 ULONG NumSections;
95 ULONG FaultsPerPeriod[10];
96 LARGE_INTEGER LaunchTime;
97 ULONGLONG Reserved[5];
98 } PF_TRACE_HEADER, *PPF_TRACE_HEADER;
99
100 typedef struct _PFSN_TRACE_DUMP
101 {
102 LIST_ENTRY CompletedTracesLink;
103 PF_TRACE_HEADER Trace;
104 } PFSN_TRACE_DUMP, *PPFSN_TRACE_DUMP;
105
106 typedef struct _PFSN_TRACE_HEADER
107 {
108 ULONG Magic;
109 LIST_ENTRY ActiveTracesLink;
110 PF_SCENARIO_ID ScenarioId;
111 ULONG ScenarioType; // PF_SCENARIO_TYPE
112 ULONG EventEntryIdxs[8];
113 ULONG NumEventEntryIdxs;
114 PPFSN_LOG_ENTRIES CurrentTraceBuffer;
115 LIST_ENTRY TraceBuffersList;
116 ULONG NumTraceBuffers;
117 KSPIN_LOCK TraceBufferSpinLock;
118 KTIMER TraceTimer;
119 LARGE_INTEGER TraceTimerPeriod;
120 KDPC TraceTimerDpc;
121 KSPIN_LOCK TraceTimerSpinLock;
122 ULONG FaultsPerPeriod[10];
123 LONG LastNumFaults;
124 LONG CurPeriod;
125 LONG NumFaults;
126 LONG MaxFaults;
127 PEPROCESS Process;
128 EX_RUNDOWN_REF RefCount;
129 WORK_QUEUE_ITEM EndTraceWorkItem;
130 LONG EndTraceCalled;
131 PPFSN_TRACE_DUMP TraceDump;
132 NTSTATUS TraceDumpStatus;
133 LARGE_INTEGER LaunchTime;
134 PPF_SECTION_INFO SectionInfo;
135 ULONG SectionInfoCount;
136 } PFSN_TRACE_HEADER, *PPFSN_TRACE_HEADER;
137
138 typedef struct _PFSN_PREFETCHER_GLOBALS
139 {
140 LIST_ENTRY ActiveTraces;
141 KSPIN_LOCK ActiveTracesLock;
142 PPFSN_TRACE_HEADER SystemWideTrace;
143 LIST_ENTRY CompletedTraces;
144 FAST_MUTEX CompletedTracesLock;
145 LONG NumCompletedTraces;
146 PKEVENT CompletedTracesEvent;
147 LONG ActivePrefetches;
148 } PFSN_PREFETCHER_GLOBALS, *PPFSN_PREFETCHER_GLOBALS;
149
150 typedef struct _ROS_SHARED_CACHE_MAP
151 {
152 LIST_ENTRY CacheMapVacbListHead;
153 ULONG TimeStamp;
154 PFILE_OBJECT FileObject;
155 LARGE_INTEGER SectionSize;
156 LARGE_INTEGER FileSize;
157 BOOLEAN PinAccess;
158 PCACHE_MANAGER_CALLBACKS Callbacks;
159 PVOID LazyWriteContext;
160 KSPIN_LOCK CacheMapLock;
161 ULONG OpenCount;
162 ULONG DirtyPages;
163 LIST_ENTRY SharedCacheMapLinks;
164 ULONG DirtyPageThreshold;
165 #if DBG
166 BOOLEAN Trace; /* enable extra trace output for this cache map and it's VACBs */
167 #endif
168 } ROS_SHARED_CACHE_MAP, *PROS_SHARED_CACHE_MAP;
169
170 typedef struct _ROS_VACB
171 {
172 /* Base address of the region where the view's data is mapped. */
173 PVOID BaseAddress;
174 /* Memory area representing the region where the view's data is mapped. */
175 struct _MEMORY_AREA* MemoryArea;
176 /* Are the contents of the view valid. */
177 BOOLEAN Valid;
178 /* Are the contents of the view newer than those on disk. */
179 BOOLEAN Dirty;
180 /* Page out in progress */
181 BOOLEAN PageOut;
182 ULONG MappedCount;
183 /* Entry in the list of VACBs for this shared cache map. */
184 LIST_ENTRY CacheMapVacbListEntry;
185 /* Entry in the list of VACBs which are dirty. */
186 LIST_ENTRY DirtyVacbListEntry;
187 /* Entry in the list of VACBs. */
188 LIST_ENTRY VacbLruListEntry;
189 /* Offset in the file which this view maps. */
190 LARGE_INTEGER FileOffset;
191 /* Mutex */
192 KMUTEX Mutex;
193 /* Number of references. */
194 ULONG ReferenceCount;
195 /* How many times was it pinned? */
196 _Guarded_by_(Mutex)
197 LONG PinCount;
198 /* Pointer to the shared cache map for the file which this view maps data for. */
199 PROS_SHARED_CACHE_MAP SharedCacheMap;
200 /* Pointer to the next VACB in a chain. */
201 } ROS_VACB, *PROS_VACB;
202
203 typedef struct _INTERNAL_BCB
204 {
205 /* Lock */
206 ERESOURCE Lock;
207 PUBLIC_BCB PFCB;
208 PROS_VACB Vacb;
209 BOOLEAN Dirty;
210 BOOLEAN Pinned;
211 CSHORT RefCount; /* (At offset 0x34 on WinNT4) */
212 } INTERNAL_BCB, *PINTERNAL_BCB;
213
214 #define NODE_TYPE_DEFERRED_WRITE 0x02FC
215
216 VOID
217 NTAPI
218 CcPfInitializePrefetcher(
219 VOID
220 );
221
222 VOID
223 NTAPI
224 CcMdlReadComplete2(
225 IN PFILE_OBJECT FileObject,
226 IN PMDL MemoryDescriptorList
227 );
228
229 VOID
230 NTAPI
231 CcMdlWriteComplete2(
232 IN PFILE_OBJECT FileObject,
233 IN PLARGE_INTEGER FileOffset,
234 IN PMDL MdlChain
235 );
236
237 NTSTATUS
238 NTAPI
239 CcRosFlushVacb(PROS_VACB Vacb);
240
241 NTSTATUS
242 NTAPI
243 CcRosGetVacb(
244 PROS_SHARED_CACHE_MAP SharedCacheMap,
245 LONGLONG FileOffset,
246 PLONGLONG BaseOffset,
247 PVOID *BaseAddress,
248 PBOOLEAN UptoDate,
249 PROS_VACB *Vacb
250 );
251
252 BOOLEAN
253 NTAPI
254 CcInitView(VOID);
255
256 VOID
257 NTAPI
258 CcShutdownLazyWriter(VOID);
259
260 NTSTATUS
261 NTAPI
262 CcReadVirtualAddress(PROS_VACB Vacb);
263
264 NTSTATUS
265 NTAPI
266 CcWriteVirtualAddress(PROS_VACB Vacb);
267
268 BOOLEAN
269 NTAPI
270 CcInitializeCacheManager(VOID);
271
272 NTSTATUS
273 NTAPI
274 CcRosUnmapVacb(
275 PROS_SHARED_CACHE_MAP SharedCacheMap,
276 LONGLONG FileOffset,
277 BOOLEAN NowDirty
278 );
279
280 PROS_VACB
281 NTAPI
282 CcRosLookupVacb(
283 PROS_SHARED_CACHE_MAP SharedCacheMap,
284 LONGLONG FileOffset
285 );
286
287 VOID
288 NTAPI
289 CcInitCacheZeroPage(VOID);
290
291 NTSTATUS
292 NTAPI
293 CcRosMarkDirtyFile(
294 PROS_SHARED_CACHE_MAP SharedCacheMap,
295 LONGLONG FileOffset
296 );
297
298 VOID
299 NTAPI
300 CcRosMarkDirtyVacb(
301 PROS_VACB Vacb);
302
303 VOID
304 NTAPI
305 CcRosUnmarkDirtyVacb(
306 PROS_VACB Vacb,
307 BOOLEAN LockViews);
308
309 NTSTATUS
310 NTAPI
311 CcRosFlushDirtyPages(
312 ULONG Target,
313 PULONG Count,
314 BOOLEAN Wait,
315 BOOLEAN CalledFromLazy
316 );
317
318 VOID
319 NTAPI
320 CcRosDereferenceCache(PFILE_OBJECT FileObject);
321
322 VOID
323 NTAPI
324 CcRosReferenceCache(PFILE_OBJECT FileObject);
325
326 VOID
327 NTAPI
328 CcRosRemoveIfClosed(PSECTION_OBJECT_POINTERS SectionObjectPointer);
329
330 NTSTATUS
331 NTAPI
332 CcRosReleaseVacb(
333 PROS_SHARED_CACHE_MAP SharedCacheMap,
334 PROS_VACB Vacb,
335 BOOLEAN Valid,
336 BOOLEAN Dirty,
337 BOOLEAN Mapped
338 );
339
340 NTSTATUS
341 NTAPI
342 CcRosRequestVacb(
343 PROS_SHARED_CACHE_MAP SharedCacheMap,
344 LONGLONG FileOffset,
345 PVOID* BaseAddress,
346 PBOOLEAN UptoDate,
347 PROS_VACB *Vacb
348 );
349
350 NTSTATUS
351 NTAPI
352 CcRosInitializeFileCache(
353 PFILE_OBJECT FileObject,
354 PCC_FILE_SIZES FileSizes,
355 BOOLEAN PinAccess,
356 PCACHE_MANAGER_CALLBACKS CallBacks,
357 PVOID LazyWriterContext
358 );
359
360 NTSTATUS
361 NTAPI
362 CcRosReleaseFileCache(
363 PFILE_OBJECT FileObject
364 );
365
366 NTSTATUS
367 NTAPI
368 CcTryToInitializeFileCache(PFILE_OBJECT FileObject);
369
370 VOID
371 NTAPI
372 CcShutdownSystem(VOID);
373
374 FORCEINLINE
375 NTSTATUS
376 CcRosAcquireVacbLock(
377 _Inout_ PROS_VACB Vacb,
378 _In_ PLARGE_INTEGER Timeout)
379 {
380 NTSTATUS Status;
381 Status = KeWaitForSingleObject(&Vacb->Mutex,
382 Executive,
383 KernelMode,
384 FALSE,
385 Timeout);
386 return Status;
387 }
388
389 FORCEINLINE
390 VOID
391 CcRosReleaseVacbLock(
392 _Inout_ PROS_VACB Vacb)
393 {
394 KeReleaseMutex(&Vacb->Mutex, FALSE);
395 }
396
397 FORCEINLINE
398 BOOLEAN
399 DoRangesIntersect(
400 _In_ LONGLONG Offset1,
401 _In_ LONGLONG Length1,
402 _In_ LONGLONG Offset2,
403 _In_ LONGLONG Length2)
404 {
405 if (Offset1 + Length1 <= Offset2)
406 return FALSE;
407 if (Offset2 + Length2 <= Offset1)
408 return FALSE;
409 return TRUE;
410 }
411
412 FORCEINLINE
413 BOOLEAN
414 IsPointInRange(
415 _In_ LONGLONG Offset1,
416 _In_ LONGLONG Length1,
417 _In_ LONGLONG Point)
418 {
419 return DoRangesIntersect(Offset1, Length1, Point, 1);
420 }