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