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