913517a762ad618853bd204e5250b674b146d289
[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 DirtyPageThreshold;
163 LIST_ENTRY SharedCacheMapLinks;
164 #if DBG
165 BOOLEAN Trace; /* enable extra trace output for this cache map and it's VACBs */
166 #endif
167 } ROS_SHARED_CACHE_MAP, *PROS_SHARED_CACHE_MAP;
168
169 typedef struct _ROS_VACB
170 {
171 /* Base address of the region where the view's data is mapped. */
172 PVOID BaseAddress;
173 /* Memory area representing the region where the view's data is mapped. */
174 struct _MEMORY_AREA* MemoryArea;
175 /* Are the contents of the view valid. */
176 BOOLEAN Valid;
177 /* Are the contents of the view newer than those on disk. */
178 BOOLEAN Dirty;
179 /* Page out in progress */
180 BOOLEAN PageOut;
181 ULONG MappedCount;
182 /* Entry in the list of VACBs for this shared cache map. */
183 LIST_ENTRY CacheMapVacbListEntry;
184 /* Entry in the list of VACBs which are dirty. */
185 LIST_ENTRY DirtyVacbListEntry;
186 /* Entry in the list of VACBs. */
187 LIST_ENTRY VacbLruListEntry;
188 /* Offset in the file which this view maps. */
189 LARGE_INTEGER FileOffset;
190 /* Mutex */
191 KMUTEX Mutex;
192 /* Number of references. */
193 ULONG ReferenceCount;
194 /* How many times was it pinned? */
195 _Guarded_by_(Mutex)
196 LONG PinCount;
197 /* Pointer to the shared cache map for the file which this view maps data for. */
198 PROS_SHARED_CACHE_MAP SharedCacheMap;
199 /* Pointer to the next VACB in a chain. */
200 } ROS_VACB, *PROS_VACB;
201
202 typedef struct _ROS_DEFERRED_WRITE_CONTEXT
203 {
204 LIST_ENTRY CcDeferredWritesEntry;
205 PFILE_OBJECT FileObject;
206 PCC_POST_DEFERRED_WRITE PostRoutine;
207 PVOID Context1;
208 PVOID Context2;
209 ULONG BytesToWrite;
210 BOOLEAN Retrying;
211 } ROS_DEFERRED_WRITE_CONTEXT, *PROS_DEFERRED_WRITE_CONTEXT;
212
213 typedef struct _INTERNAL_BCB
214 {
215 /* Lock */
216 ERESOURCE Lock;
217 PUBLIC_BCB PFCB;
218 PROS_VACB Vacb;
219 BOOLEAN Dirty;
220 BOOLEAN Pinned;
221 CSHORT RefCount; /* (At offset 0x34 on WinNT4) */
222 } INTERNAL_BCB, *PINTERNAL_BCB;
223
224 VOID
225 NTAPI
226 CcPfInitializePrefetcher(
227 VOID
228 );
229
230 VOID
231 NTAPI
232 CcMdlReadComplete2(
233 IN PFILE_OBJECT FileObject,
234 IN PMDL MemoryDescriptorList
235 );
236
237 VOID
238 NTAPI
239 CcMdlWriteComplete2(
240 IN PFILE_OBJECT FileObject,
241 IN PLARGE_INTEGER FileOffset,
242 IN PMDL MdlChain
243 );
244
245 NTSTATUS
246 NTAPI
247 CcRosFlushVacb(PROS_VACB Vacb);
248
249 NTSTATUS
250 NTAPI
251 CcRosGetVacb(
252 PROS_SHARED_CACHE_MAP SharedCacheMap,
253 LONGLONG FileOffset,
254 PLONGLONG BaseOffset,
255 PVOID *BaseAddress,
256 PBOOLEAN UptoDate,
257 PROS_VACB *Vacb
258 );
259
260 BOOLEAN
261 NTAPI
262 CcInitView(VOID);
263
264 VOID
265 NTAPI
266 CcShutdownLazyWriter(VOID);
267
268 NTSTATUS
269 NTAPI
270 CcReadVirtualAddress(PROS_VACB Vacb);
271
272 NTSTATUS
273 NTAPI
274 CcWriteVirtualAddress(PROS_VACB Vacb);
275
276 BOOLEAN
277 NTAPI
278 CcInitializeCacheManager(VOID);
279
280 NTSTATUS
281 NTAPI
282 CcRosUnmapVacb(
283 PROS_SHARED_CACHE_MAP SharedCacheMap,
284 LONGLONG FileOffset,
285 BOOLEAN NowDirty
286 );
287
288 PROS_VACB
289 NTAPI
290 CcRosLookupVacb(
291 PROS_SHARED_CACHE_MAP SharedCacheMap,
292 LONGLONG FileOffset
293 );
294
295 VOID
296 NTAPI
297 CcInitCacheZeroPage(VOID);
298
299 NTSTATUS
300 NTAPI
301 CcRosMarkDirtyFile(
302 PROS_SHARED_CACHE_MAP SharedCacheMap,
303 LONGLONG FileOffset
304 );
305
306 VOID
307 NTAPI
308 CcRosMarkDirtyVacb(
309 PROS_VACB Vacb);
310
311 NTSTATUS
312 NTAPI
313 CcRosFlushDirtyPages(
314 ULONG Target,
315 PULONG Count,
316 BOOLEAN Wait,
317 BOOLEAN CalledFromLazy
318 );
319
320 VOID
321 NTAPI
322 CcRosDereferenceCache(PFILE_OBJECT FileObject);
323
324 VOID
325 NTAPI
326 CcRosReferenceCache(PFILE_OBJECT FileObject);
327
328 VOID
329 NTAPI
330 CcRosRemoveIfClosed(PSECTION_OBJECT_POINTERS SectionObjectPointer);
331
332 NTSTATUS
333 NTAPI
334 CcRosReleaseVacb(
335 PROS_SHARED_CACHE_MAP SharedCacheMap,
336 PROS_VACB Vacb,
337 BOOLEAN Valid,
338 BOOLEAN Dirty,
339 BOOLEAN Mapped
340 );
341
342 NTSTATUS
343 NTAPI
344 CcRosRequestVacb(
345 PROS_SHARED_CACHE_MAP SharedCacheMap,
346 LONGLONG FileOffset,
347 PVOID* BaseAddress,
348 PBOOLEAN UptoDate,
349 PROS_VACB *Vacb
350 );
351
352 NTSTATUS
353 NTAPI
354 CcRosInitializeFileCache(
355 PFILE_OBJECT FileObject,
356 PCC_FILE_SIZES FileSizes,
357 BOOLEAN PinAccess,
358 PCACHE_MANAGER_CALLBACKS CallBacks,
359 PVOID LazyWriterContext
360 );
361
362 NTSTATUS
363 NTAPI
364 CcRosReleaseFileCache(
365 PFILE_OBJECT FileObject
366 );
367
368 NTSTATUS
369 NTAPI
370 CcTryToInitializeFileCache(PFILE_OBJECT FileObject);
371
372 VOID
373 NTAPI
374 CcShutdownSystem(VOID);
375
376 FORCEINLINE
377 NTSTATUS
378 CcRosAcquireVacbLock(
379 _Inout_ PROS_VACB Vacb,
380 _In_ PLARGE_INTEGER Timeout)
381 {
382 NTSTATUS Status;
383 Status = KeWaitForSingleObject(&Vacb->Mutex,
384 Executive,
385 KernelMode,
386 FALSE,
387 Timeout);
388 return Status;
389 }
390
391 FORCEINLINE
392 VOID
393 CcRosReleaseVacbLock(
394 _Inout_ PROS_VACB Vacb)
395 {
396 KeReleaseMutex(&Vacb->Mutex, FALSE);
397 }
398
399 FORCEINLINE
400 BOOLEAN
401 DoRangesIntersect(
402 _In_ LONGLONG Offset1,
403 _In_ LONGLONG Length1,
404 _In_ LONGLONG Offset2,
405 _In_ LONGLONG Length2)
406 {
407 if (Offset1 + Length1 <= Offset2)
408 return FALSE;
409 if (Offset2 + Length2 <= Offset1)
410 return FALSE;
411 return TRUE;
412 }
413
414 FORCEINLINE
415 BOOLEAN
416 IsPointInRange(
417 _In_ LONGLONG Offset1,
418 _In_ LONGLONG Length1,
419 _In_ LONGLONG Point)
420 {
421 return DoRangesIntersect(Offset1, Length1, Point, 1);
422 }