[NTOSKRNL] When marking a BCB dirty, also mark the underlying VACB dirty.
[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 BOOLEAN
244 NTAPI
245 CcInitView(VOID);
246
247 VOID
248 NTAPI
249 CcShutdownLazyWriter(VOID);
250
251 NTSTATUS
252 NTAPI
253 CcReadVirtualAddress(PROS_VACB Vacb);
254
255 NTSTATUS
256 NTAPI
257 CcWriteVirtualAddress(PROS_VACB Vacb);
258
259 BOOLEAN
260 NTAPI
261 CcInitializeCacheManager(VOID);
262
263 NTSTATUS
264 NTAPI
265 CcRosUnmapVacb(
266 PROS_SHARED_CACHE_MAP SharedCacheMap,
267 LONGLONG FileOffset,
268 BOOLEAN NowDirty
269 );
270
271 PROS_VACB
272 NTAPI
273 CcRosLookupVacb(
274 PROS_SHARED_CACHE_MAP SharedCacheMap,
275 LONGLONG FileOffset
276 );
277
278 VOID
279 NTAPI
280 CcInitCacheZeroPage(VOID);
281
282 NTSTATUS
283 NTAPI
284 CcRosMarkDirtyFile(
285 PROS_SHARED_CACHE_MAP SharedCacheMap,
286 LONGLONG FileOffset
287 );
288
289 VOID
290 NTAPI
291 CcRosMarkDirtyVacb(
292 PROS_VACB Vacb);
293
294 NTSTATUS
295 NTAPI
296 CcRosFlushDirtyPages(
297 ULONG Target,
298 PULONG Count,
299 BOOLEAN Wait,
300 BOOLEAN CalledFromLazy
301 );
302
303 VOID
304 NTAPI
305 CcRosDereferenceCache(PFILE_OBJECT FileObject);
306
307 VOID
308 NTAPI
309 CcRosReferenceCache(PFILE_OBJECT FileObject);
310
311 VOID
312 NTAPI
313 CcRosRemoveIfClosed(PSECTION_OBJECT_POINTERS SectionObjectPointer);
314
315 NTSTATUS
316 NTAPI
317 CcRosReleaseVacb(
318 PROS_SHARED_CACHE_MAP SharedCacheMap,
319 PROS_VACB Vacb,
320 BOOLEAN Valid,
321 BOOLEAN Dirty,
322 BOOLEAN Mapped
323 );
324
325 NTSTATUS
326 NTAPI
327 CcRosRequestVacb(
328 PROS_SHARED_CACHE_MAP SharedCacheMap,
329 LONGLONG FileOffset,
330 PVOID* BaseAddress,
331 PBOOLEAN UptoDate,
332 PROS_VACB *Vacb
333 );
334
335 NTSTATUS
336 NTAPI
337 CcRosInitializeFileCache(
338 PFILE_OBJECT FileObject,
339 PCC_FILE_SIZES FileSizes,
340 BOOLEAN PinAccess,
341 PCACHE_MANAGER_CALLBACKS CallBacks,
342 PVOID LazyWriterContext
343 );
344
345 NTSTATUS
346 NTAPI
347 CcRosReleaseFileCache(
348 PFILE_OBJECT FileObject
349 );
350
351 NTSTATUS
352 NTAPI
353 CcTryToInitializeFileCache(PFILE_OBJECT FileObject);
354
355 VOID
356 NTAPI
357 CcShutdownSystem(VOID);
358
359 FORCEINLINE
360 NTSTATUS
361 CcRosAcquireVacbLock(
362 _Inout_ PROS_VACB Vacb,
363 _In_ PLARGE_INTEGER Timeout)
364 {
365 NTSTATUS Status;
366 Status = KeWaitForSingleObject(&Vacb->Mutex,
367 Executive,
368 KernelMode,
369 FALSE,
370 Timeout);
371 return Status;
372 }
373
374 FORCEINLINE
375 VOID
376 CcRosReleaseVacbLock(
377 _Inout_ PROS_VACB Vacb)
378 {
379 KeReleaseMutex(&Vacb->Mutex, FALSE);
380 }
381
382 FORCEINLINE
383 BOOLEAN
384 DoRangesIntersect(
385 _In_ LONGLONG Offset1,
386 _In_ LONGLONG Length1,
387 _In_ LONGLONG Offset2,
388 _In_ LONGLONG Length2)
389 {
390 if (Offset1 + Length1 <= Offset2)
391 return FALSE;
392 if (Offset2 + Length2 <= Offset1)
393 return FALSE;
394 return TRUE;
395 }
396
397 FORCEINLINE
398 BOOLEAN
399 IsPointInRange(
400 _In_ LONGLONG Offset1,
401 _In_ LONGLONG Length1,
402 _In_ LONGLONG Point)
403 {
404 return DoRangesIntersect(Offset1, Length1, Point, 1);
405 }