xref: /reactos/ntoskrnl/include/internal/cc.h (revision 8a978a17)
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 extern ULONG CcNumberWorkerThreads;
49 extern LIST_ENTRY CcIdleWorkerThreadList;
50 extern LIST_ENTRY CcExpressWorkQueue;
51 extern LIST_ENTRY CcRegularWorkQueue;
52 extern LIST_ENTRY CcPostTickWorkQueue;
53 extern NPAGED_LOOKASIDE_LIST CcTwilightLookasideList;
54 extern LARGE_INTEGER CcIdleDelay;
55 
56 //
57 // Counters
58 //
59 extern ULONG CcLazyWritePages;
60 extern ULONG CcLazyWriteIos;
61 extern ULONG CcMapDataWait;
62 extern ULONG CcMapDataNoWait;
63 extern ULONG CcPinReadWait;
64 extern ULONG CcPinReadNoWait;
65 extern ULONG CcPinMappedDataCount;
66 extern ULONG CcDataPages;
67 extern ULONG CcDataFlushes;
68 
69 typedef struct _PF_SCENARIO_ID
70 {
71     WCHAR ScenName[30];
72     ULONG HashId;
73 } PF_SCENARIO_ID, *PPF_SCENARIO_ID;
74 
75 typedef struct _PF_LOG_ENTRY
76 {
77     ULONG FileOffset:30;
78     ULONG Type:2;
79     union
80     {
81         ULONG FileKey;
82         ULONG FileSequenceNumber;
83     };
84 } PF_LOG_ENTRY, *PPF_LOG_ENTRY;
85 
86 typedef struct _PFSN_LOG_ENTRIES
87 {
88     LIST_ENTRY TraceBuffersLink;
89     LONG NumEntries;
90     LONG MaxEntries;
91     PF_LOG_ENTRY Entries[ANYSIZE_ARRAY];
92 } PFSN_LOG_ENTRIES, *PPFSN_LOG_ENTRIES;
93 
94 typedef struct _PF_SECTION_INFO
95 {
96     ULONG FileKey;
97     ULONG FileSequenceNumber;
98     ULONG FileIdLow;
99     ULONG FileIdHigh;
100 } PF_SECTION_INFO, *PPF_SECTION_INFO;
101 
102 typedef struct _PF_TRACE_HEADER
103 {
104     ULONG Version;
105     ULONG MagicNumber;
106     ULONG Size;
107     PF_SCENARIO_ID ScenarioId;
108     ULONG ScenarioType; // PF_SCENARIO_TYPE
109     ULONG EventEntryIdxs[8];
110     ULONG NumEventEntryIdxs;
111     ULONG TraceBufferOffset;
112     ULONG NumEntries;
113     ULONG SectionInfoOffset;
114     ULONG NumSections;
115     ULONG FaultsPerPeriod[10];
116     LARGE_INTEGER LaunchTime;
117     ULONGLONG Reserved[5];
118 } PF_TRACE_HEADER, *PPF_TRACE_HEADER;
119 
120 typedef struct _PFSN_TRACE_DUMP
121 {
122     LIST_ENTRY CompletedTracesLink;
123     PF_TRACE_HEADER Trace;
124 } PFSN_TRACE_DUMP, *PPFSN_TRACE_DUMP;
125 
126 typedef struct _PFSN_TRACE_HEADER
127 {
128     ULONG Magic;
129     LIST_ENTRY ActiveTracesLink;
130     PF_SCENARIO_ID ScenarioId;
131     ULONG ScenarioType; // PF_SCENARIO_TYPE
132     ULONG EventEntryIdxs[8];
133     ULONG NumEventEntryIdxs;
134     PPFSN_LOG_ENTRIES CurrentTraceBuffer;
135     LIST_ENTRY TraceBuffersList;
136     ULONG NumTraceBuffers;
137     KSPIN_LOCK TraceBufferSpinLock;
138     KTIMER TraceTimer;
139     LARGE_INTEGER TraceTimerPeriod;
140     KDPC TraceTimerDpc;
141     KSPIN_LOCK TraceTimerSpinLock;
142     ULONG FaultsPerPeriod[10];
143     LONG LastNumFaults;
144     LONG CurPeriod;
145     LONG NumFaults;
146     LONG MaxFaults;
147     PEPROCESS Process;
148     EX_RUNDOWN_REF RefCount;
149     WORK_QUEUE_ITEM EndTraceWorkItem;
150     LONG EndTraceCalled;
151     PPFSN_TRACE_DUMP TraceDump;
152     NTSTATUS TraceDumpStatus;
153     LARGE_INTEGER LaunchTime;
154     PPF_SECTION_INFO SectionInfo;
155     ULONG SectionInfoCount;
156 } PFSN_TRACE_HEADER, *PPFSN_TRACE_HEADER;
157 
158 typedef struct _PFSN_PREFETCHER_GLOBALS
159 {
160     LIST_ENTRY ActiveTraces;
161     KSPIN_LOCK ActiveTracesLock;
162     PPFSN_TRACE_HEADER SystemWideTrace;
163     LIST_ENTRY CompletedTraces;
164     FAST_MUTEX CompletedTracesLock;
165     LONG NumCompletedTraces;
166     PKEVENT CompletedTracesEvent;
167     LONG ActivePrefetches;
168 } PFSN_PREFETCHER_GLOBALS, *PPFSN_PREFETCHER_GLOBALS;
169 
170 typedef struct _ROS_SHARED_CACHE_MAP
171 {
172     CSHORT NodeTypeCode;
173     CSHORT NodeByteSize;
174     ULONG OpenCount;
175     LARGE_INTEGER FileSize;
176     LIST_ENTRY BcbList;
177     LARGE_INTEGER SectionSize;
178     PFILE_OBJECT FileObject;
179     ULONG DirtyPages;
180     LIST_ENTRY SharedCacheMapLinks;
181     ULONG Flags;
182     PCACHE_MANAGER_CALLBACKS Callbacks;
183     PVOID LazyWriteContext;
184     LIST_ENTRY PrivateList;
185     ULONG DirtyPageThreshold;
186     KSPIN_LOCK BcbSpinLock;
187     PRIVATE_CACHE_MAP PrivateCacheMap;
188 
189     /* ROS specific */
190     LIST_ENTRY CacheMapVacbListHead;
191     BOOLEAN PinAccess;
192     KSPIN_LOCK CacheMapLock;
193 #if DBG
194     BOOLEAN Trace; /* enable extra trace output for this cache map and it's VACBs */
195 #endif
196 } ROS_SHARED_CACHE_MAP, *PROS_SHARED_CACHE_MAP;
197 
198 #define READAHEAD_DISABLED 0x1
199 #define WRITEBEHIND_DISABLED 0x2
200 
201 typedef struct _ROS_VACB
202 {
203     /* Base address of the region where the view's data is mapped. */
204     PVOID BaseAddress;
205     /* Memory area representing the region where the view's data is mapped. */
206     struct _MEMORY_AREA* MemoryArea;
207     /* Are the contents of the view valid. */
208     BOOLEAN Valid;
209     /* Are the contents of the view newer than those on disk. */
210     BOOLEAN Dirty;
211     /* Page out in progress */
212     BOOLEAN PageOut;
213     ULONG MappedCount;
214     /* Entry in the list of VACBs for this shared cache map. */
215     LIST_ENTRY CacheMapVacbListEntry;
216     /* Entry in the list of VACBs which are dirty. */
217     LIST_ENTRY DirtyVacbListEntry;
218     /* Entry in the list of VACBs. */
219     LIST_ENTRY VacbLruListEntry;
220     /* Offset in the file which this view maps. */
221     LARGE_INTEGER FileOffset;
222     /* Number of references. */
223     volatile ULONG ReferenceCount;
224     /* Pointer to the shared cache map for the file which this view maps data for. */
225     PROS_SHARED_CACHE_MAP SharedCacheMap;
226     /* Pointer to the next VACB in a chain. */
227 } ROS_VACB, *PROS_VACB;
228 
229 typedef struct _INTERNAL_BCB
230 {
231     /* Lock */
232     ERESOURCE Lock;
233     PUBLIC_BCB PFCB;
234     PROS_VACB Vacb;
235     ULONG PinCount;
236     CSHORT RefCount; /* (At offset 0x34 on WinNT4) */
237     LIST_ENTRY BcbEntry;
238 } INTERNAL_BCB, *PINTERNAL_BCB;
239 
240 typedef struct _LAZY_WRITER
241 {
242     LIST_ENTRY WorkQueue;
243     KDPC ScanDpc;
244     KTIMER ScanTimer;
245     BOOLEAN ScanActive;
246     BOOLEAN OtherWork;
247     BOOLEAN PendingTeardown;
248 } LAZY_WRITER, *PLAZY_WRITER;
249 
250 typedef struct _WORK_QUEUE_ENTRY
251 {
252     LIST_ENTRY WorkQueueLinks;
253     union
254     {
255         struct
256         {
257             FILE_OBJECT *FileObject;
258         } Read;
259         struct
260         {
261             SHARED_CACHE_MAP *SharedCacheMap;
262         } Write;
263         struct
264         {
265             KEVENT *Event;
266         } Event;
267         struct
268         {
269             unsigned long Reason;
270         } Notification;
271     } Parameters;
272     unsigned char Function;
273 } WORK_QUEUE_ENTRY, *PWORK_QUEUE_ENTRY;
274 
275 typedef enum _WORK_QUEUE_FUNCTIONS
276 {
277     ReadAhead = 1,
278     WriteBehind = 2,
279     LazyScan = 3,
280     SetDone = 4,
281 } WORK_QUEUE_FUNCTIONS, *PWORK_QUEUE_FUNCTIONS;
282 
283 extern LAZY_WRITER LazyWriter;
284 
285 #define NODE_TYPE_DEFERRED_WRITE 0x02FC
286 #define NODE_TYPE_PRIVATE_MAP    0x02FE
287 #define NODE_TYPE_SHARED_MAP     0x02FF
288 
289 VOID
290 NTAPI
291 CcPfInitializePrefetcher(
292     VOID
293 );
294 
295 VOID
296 NTAPI
297 CcMdlReadComplete2(
298     IN PFILE_OBJECT FileObject,
299     IN PMDL MemoryDescriptorList
300 );
301 
302 VOID
303 NTAPI
304 CcMdlWriteComplete2(
305     IN PFILE_OBJECT FileObject,
306     IN PLARGE_INTEGER FileOffset,
307     IN PMDL MdlChain
308 );
309 
310 NTSTATUS
311 NTAPI
312 CcRosFlushVacb(PROS_VACB Vacb);
313 
314 NTSTATUS
315 NTAPI
316 CcRosGetVacb(
317     PROS_SHARED_CACHE_MAP SharedCacheMap,
318     LONGLONG FileOffset,
319     PLONGLONG BaseOffset,
320     PVOID *BaseAddress,
321     PBOOLEAN UptoDate,
322     PROS_VACB *Vacb
323 );
324 
325 VOID
326 NTAPI
327 CcInitView(VOID);
328 
329 VOID
330 NTAPI
331 CcShutdownLazyWriter(VOID);
332 
333 NTSTATUS
334 NTAPI
335 CcReadVirtualAddress(PROS_VACB Vacb);
336 
337 NTSTATUS
338 NTAPI
339 CcWriteVirtualAddress(PROS_VACB Vacb);
340 
341 BOOLEAN
342 NTAPI
343 CcInitializeCacheManager(VOID);
344 
345 NTSTATUS
346 NTAPI
347 CcRosUnmapVacb(
348     PROS_SHARED_CACHE_MAP SharedCacheMap,
349     LONGLONG FileOffset,
350     BOOLEAN NowDirty
351 );
352 
353 PROS_VACB
354 NTAPI
355 CcRosLookupVacb(
356     PROS_SHARED_CACHE_MAP SharedCacheMap,
357     LONGLONG FileOffset
358 );
359 
360 VOID
361 NTAPI
362 CcInitCacheZeroPage(VOID);
363 
364 NTSTATUS
365 NTAPI
366 CcRosMarkDirtyFile(
367     PROS_SHARED_CACHE_MAP SharedCacheMap,
368     LONGLONG FileOffset
369 );
370 
371 VOID
372 NTAPI
373 CcRosMarkDirtyVacb(
374     PROS_VACB Vacb);
375 
376 VOID
377 NTAPI
378 CcRosUnmarkDirtyVacb(
379     PROS_VACB Vacb,
380     BOOLEAN LockViews);
381 
382 NTSTATUS
383 NTAPI
384 CcRosFlushDirtyPages(
385     ULONG Target,
386     PULONG Count,
387     BOOLEAN Wait,
388     BOOLEAN CalledFromLazy
389 );
390 
391 VOID
392 NTAPI
393 CcRosDereferenceCache(PFILE_OBJECT FileObject);
394 
395 VOID
396 NTAPI
397 CcRosReferenceCache(PFILE_OBJECT FileObject);
398 
399 VOID
400 NTAPI
401 CcRosRemoveIfClosed(PSECTION_OBJECT_POINTERS SectionObjectPointer);
402 
403 NTSTATUS
404 NTAPI
405 CcRosReleaseVacb(
406     PROS_SHARED_CACHE_MAP SharedCacheMap,
407     PROS_VACB Vacb,
408     BOOLEAN Valid,
409     BOOLEAN Dirty,
410     BOOLEAN Mapped
411 );
412 
413 NTSTATUS
414 NTAPI
415 CcRosRequestVacb(
416     PROS_SHARED_CACHE_MAP SharedCacheMap,
417     LONGLONG FileOffset,
418     PVOID* BaseAddress,
419     PBOOLEAN UptoDate,
420     PROS_VACB *Vacb
421 );
422 
423 NTSTATUS
424 NTAPI
425 CcRosInitializeFileCache(
426     PFILE_OBJECT FileObject,
427     PCC_FILE_SIZES FileSizes,
428     BOOLEAN PinAccess,
429     PCACHE_MANAGER_CALLBACKS CallBacks,
430     PVOID LazyWriterContext
431 );
432 
433 NTSTATUS
434 NTAPI
435 CcRosReleaseFileCache(
436     PFILE_OBJECT FileObject
437 );
438 
439 VOID
440 NTAPI
441 CcShutdownSystem(VOID);
442 
443 VOID
444 NTAPI
445 CcWorkerThread(PVOID Parameter);
446 
447 VOID
448 NTAPI
449 CcScanDpc(
450     PKDPC Dpc,
451     PVOID DeferredContext,
452     PVOID SystemArgument1,
453     PVOID SystemArgument2);
454 
455 VOID
456 CcScheduleLazyWriteScan(BOOLEAN NoDelay);
457 
458 VOID
459 CcPostDeferredWrites(VOID);
460 
461 VOID
462 CcPostWorkQueue(
463     IN PWORK_QUEUE_ENTRY WorkItem,
464     IN PLIST_ENTRY WorkQueue);
465 
466 VOID
467 CcPerformReadAhead(
468     IN PFILE_OBJECT FileObject);
469 
470 NTSTATUS
471 CcRosInternalFreeVacb(
472     IN PROS_VACB Vacb);
473 
474 FORCEINLINE
475 BOOLEAN
476 DoRangesIntersect(
477     _In_ LONGLONG Offset1,
478     _In_ LONGLONG Length1,
479     _In_ LONGLONG Offset2,
480     _In_ LONGLONG Length2)
481 {
482     if (Offset1 + Length1 <= Offset2)
483         return FALSE;
484     if (Offset2 + Length2 <= Offset1)
485         return FALSE;
486     return TRUE;
487 }
488 
489 FORCEINLINE
490 BOOLEAN
491 IsPointInRange(
492     _In_ LONGLONG Offset1,
493     _In_ LONGLONG Length1,
494     _In_ LONGLONG Point)
495 {
496     return DoRangesIntersect(Offset1, Length1, Point, 1);
497 }
498 
499 #define CcBugCheck(A, B, C) KeBugCheckEx(CACHE_MANAGER, BugCheckFileId | ((ULONG)(__LINE__)), A, B, C)
500 
501 #if DBG
502 #define CcRosVacbIncRefCount(vacb) CcRosVacbIncRefCount_(vacb,__FILE__,__LINE__)
503 #define CcRosVacbDecRefCount(vacb) CcRosVacbDecRefCount_(vacb,__FILE__,__LINE__)
504 #define CcRosVacbGetRefCount(vacb) CcRosVacbGetRefCount_(vacb,__FILE__,__LINE__)
505 
506 ULONG
507 CcRosVacbIncRefCount_(
508     PROS_VACB vacb,
509     PCSTR file,
510     INT line);
511 
512 ULONG
513 CcRosVacbDecRefCount_(
514     PROS_VACB vacb,
515     PCSTR file,
516     INT line);
517 
518 ULONG
519 CcRosVacbGetRefCount_(
520     PROS_VACB vacb,
521     PCSTR file,
522     INT line);
523 
524 #else
525 #define CcRosVacbIncRefCount(vacb) InterlockedIncrement((PLONG)&(vacb)->ReferenceCount)
526 FORCEINLINE
527 ULONG
528 CcRosVacbDecRefCount(
529     PROS_VACB vacb)
530 {
531     ULONG Refs;
532 
533     Refs = InterlockedDecrement((PLONG)&vacb->ReferenceCount);
534     if (Refs == 0)
535     {
536         CcRosInternalFreeVacb(vacb);
537     }
538     return Refs;
539 }
540 #define CcRosVacbGetRefCount(vacb) InterlockedCompareExchange((PLONG)&(vacb)->ReferenceCount, 0, 0)
541 #endif
542