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