3c7f8d78d15b2a6a270acfd25e291723087e768b
[reactos.git] / reactos / 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 PCACHE_MANAGER_CALLBACKS Callbacks;
153 PVOID LazyWriteContext;
154 KSPIN_LOCK CacheMapLock;
155 ULONG RefCount;
156 #if DBG
157 BOOLEAN Trace; /* enable extra trace output for this cache map and it's VACBs */
158 #endif
159 } ROS_SHARED_CACHE_MAP, *PROS_SHARED_CACHE_MAP;
160
161 typedef struct _ROS_VACB
162 {
163 /* Base address of the region where the view's data is mapped. */
164 PVOID BaseAddress;
165 /* Memory area representing the region where the view's data is mapped. */
166 struct _MEMORY_AREA* MemoryArea;
167 /* Are the contents of the view valid. */
168 BOOLEAN Valid;
169 /* Are the contents of the view newer than those on disk. */
170 BOOLEAN Dirty;
171 /* Page out in progress */
172 BOOLEAN PageOut;
173 ULONG MappedCount;
174 /* Entry in the list of VACBs for this shared cache map. */
175 LIST_ENTRY CacheMapVacbListEntry;
176 /* Entry in the list of VACBs which are dirty. */
177 LIST_ENTRY DirtyVacbListEntry;
178 /* Entry in the list of VACBs. */
179 LIST_ENTRY VacbLruListEntry;
180 /* Offset in the file which this view maps. */
181 LARGE_INTEGER FileOffset;
182 /* Mutex */
183 KMUTEX Mutex;
184 /* Number of references. */
185 ULONG ReferenceCount;
186 /* Pointer to the shared cache map for the file which this view maps data for. */
187 PROS_SHARED_CACHE_MAP SharedCacheMap;
188 /* Pointer to the next VACB in a chain. */
189 } ROS_VACB, *PROS_VACB;
190
191 typedef struct _INTERNAL_BCB
192 {
193 PUBLIC_BCB PFCB;
194 PROS_VACB Vacb;
195 BOOLEAN Dirty;
196 CSHORT RefCount; /* (At offset 0x34 on WinNT4) */
197 } INTERNAL_BCB, *PINTERNAL_BCB;
198
199 VOID
200 NTAPI
201 CcPfInitializePrefetcher(
202 VOID
203 );
204
205 VOID
206 NTAPI
207 CcMdlReadComplete2(
208 IN PFILE_OBJECT FileObject,
209 IN PMDL MemoryDescriptorList
210 );
211
212 VOID
213 NTAPI
214 CcMdlWriteComplete2(
215 IN PFILE_OBJECT FileObject,
216 IN PLARGE_INTEGER FileOffset,
217 IN PMDL MdlChain
218 );
219
220 NTSTATUS
221 NTAPI
222 CcRosFlushVacb(PROS_VACB Vacb);
223
224 NTSTATUS
225 NTAPI
226 CcRosGetVacb(
227 PROS_SHARED_CACHE_MAP SharedCacheMap,
228 LONGLONG FileOffset,
229 PLONGLONG BaseOffset,
230 PVOID *BaseAddress,
231 PBOOLEAN UptoDate,
232 PROS_VACB *Vacb
233 );
234
235 VOID
236 NTAPI
237 CcInitView(VOID);
238
239 NTSTATUS
240 NTAPI
241 CcReadVirtualAddress(PROS_VACB Vacb);
242
243 NTSTATUS
244 NTAPI
245 CcWriteVirtualAddress(PROS_VACB Vacb);
246
247 BOOLEAN
248 NTAPI
249 CcInitializeCacheManager(VOID);
250
251 NTSTATUS
252 NTAPI
253 CcRosUnmapVacb(
254 PROS_SHARED_CACHE_MAP SharedCacheMap,
255 LONGLONG FileOffset,
256 BOOLEAN NowDirty
257 );
258
259 PROS_VACB
260 NTAPI
261 CcRosLookupVacb(
262 PROS_SHARED_CACHE_MAP SharedCacheMap,
263 LONGLONG FileOffset
264 );
265
266 VOID
267 NTAPI
268 CcInitCacheZeroPage(VOID);
269
270 NTSTATUS
271 NTAPI
272 CcRosMarkDirtyVacb(
273 PROS_SHARED_CACHE_MAP SharedCacheMap,
274 LONGLONG FileOffset
275 );
276
277 NTSTATUS
278 NTAPI
279 CcRosFlushDirtyPages(
280 ULONG Target,
281 PULONG Count,
282 BOOLEAN Wait
283 );
284
285 VOID
286 NTAPI
287 CcRosDereferenceCache(PFILE_OBJECT FileObject);
288
289 VOID
290 NTAPI
291 CcRosReferenceCache(PFILE_OBJECT FileObject);
292
293 VOID
294 NTAPI
295 CcRosRemoveIfClosed(PSECTION_OBJECT_POINTERS SectionObjectPointer);
296
297 NTSTATUS
298 NTAPI
299 CcRosReleaseVacb(
300 PROS_SHARED_CACHE_MAP SharedCacheMap,
301 PROS_VACB Vacb,
302 BOOLEAN Valid,
303 BOOLEAN Dirty,
304 BOOLEAN Mapped
305 );
306
307 NTSTATUS
308 NTAPI
309 CcRosRequestVacb(
310 PROS_SHARED_CACHE_MAP SharedCacheMap,
311 LONGLONG FileOffset,
312 PVOID* BaseAddress,
313 PBOOLEAN UptoDate,
314 PROS_VACB *Vacb
315 );
316
317 NTSTATUS
318 NTAPI
319 CcRosInitializeFileCache(
320 PFILE_OBJECT FileObject,
321 PCC_FILE_SIZES FileSizes,
322 PCACHE_MANAGER_CALLBACKS CallBacks,
323 PVOID LazyWriterContext
324 );
325
326 NTSTATUS
327 NTAPI
328 CcRosReleaseFileCache(
329 PFILE_OBJECT FileObject
330 );
331
332 NTSTATUS
333 NTAPI
334 CcTryToInitializeFileCache(PFILE_OBJECT FileObject);
335
336 FORCEINLINE
337 BOOLEAN
338 DoRangesIntersect(
339 _In_ LONGLONG Offset1,
340 _In_ LONGLONG Length1,
341 _In_ LONGLONG Offset2,
342 _In_ LONGLONG Length2)
343 {
344 if (Offset1 + Length1 <= Offset2)
345 return FALSE;
346 if (Offset2 + Length2 <= Offset1)
347 return FALSE;
348 return TRUE;
349 }
350
351 FORCEINLINE
352 BOOLEAN
353 IsPointInRange(
354 _In_ LONGLONG Offset1,
355 _In_ LONGLONG Length1,
356 _In_ LONGLONG Point)
357 {
358 return DoRangesIntersect(Offset1, Length1, Point, 1);
359 }