[NTOSKRNL]
[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 /* Lock */
168 ERESOURCE Lock;
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 /* 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 PVOID OwnerPointer;
198 } INTERNAL_BCB, *PINTERNAL_BCB;
199
200 VOID
201 NTAPI
202 CcPfInitializePrefetcher(
203 VOID
204 );
205
206 VOID
207 NTAPI
208 CcMdlReadComplete2(
209 IN PFILE_OBJECT FileObject,
210 IN PMDL MemoryDescriptorList
211 );
212
213 VOID
214 NTAPI
215 CcMdlWriteComplete2(
216 IN PFILE_OBJECT FileObject,
217 IN PLARGE_INTEGER FileOffset,
218 IN PMDL MdlChain
219 );
220
221 NTSTATUS
222 NTAPI
223 CcRosFlushVacb(PROS_VACB Vacb);
224
225 NTSTATUS
226 NTAPI
227 CcRosGetVacb(
228 PROS_SHARED_CACHE_MAP SharedCacheMap,
229 LONGLONG FileOffset,
230 PLONGLONG BaseOffset,
231 PVOID *BaseAddress,
232 PBOOLEAN UptoDate,
233 PROS_VACB *Vacb
234 );
235
236 VOID
237 NTAPI
238 CcInitView(VOID);
239
240 NTSTATUS
241 NTAPI
242 CcReadVirtualAddress(PROS_VACB Vacb);
243
244 NTSTATUS
245 NTAPI
246 CcWriteVirtualAddress(PROS_VACB Vacb);
247
248 BOOLEAN
249 NTAPI
250 CcInitializeCacheManager(VOID);
251
252 NTSTATUS
253 NTAPI
254 CcRosUnmapVacb(
255 PROS_SHARED_CACHE_MAP SharedCacheMap,
256 LONGLONG FileOffset,
257 BOOLEAN NowDirty
258 );
259
260 PROS_VACB
261 NTAPI
262 CcRosLookupVacb(
263 PROS_SHARED_CACHE_MAP SharedCacheMap,
264 LONGLONG FileOffset
265 );
266
267 VOID
268 NTAPI
269 CcInitCacheZeroPage(VOID);
270
271 NTSTATUS
272 NTAPI
273 CcRosMarkDirtyVacb(
274 PROS_SHARED_CACHE_MAP SharedCacheMap,
275 LONGLONG FileOffset
276 );
277
278 NTSTATUS
279 NTAPI
280 CcRosFlushDirtyPages(
281 ULONG Target,
282 PULONG Count,
283 BOOLEAN Wait
284 );
285
286 VOID
287 NTAPI
288 CcRosDereferenceCache(PFILE_OBJECT FileObject);
289
290 VOID
291 NTAPI
292 CcRosReferenceCache(PFILE_OBJECT FileObject);
293
294 VOID
295 NTAPI
296 CcRosRemoveIfClosed(PSECTION_OBJECT_POINTERS SectionObjectPointer);
297
298 NTSTATUS
299 NTAPI
300 CcRosReleaseVacb(
301 PROS_SHARED_CACHE_MAP SharedCacheMap,
302 PROS_VACB Vacb,
303 BOOLEAN Valid,
304 BOOLEAN Dirty,
305 BOOLEAN Mapped
306 );
307
308 NTSTATUS
309 NTAPI
310 CcRosRequestVacb(
311 PROS_SHARED_CACHE_MAP SharedCacheMap,
312 LONGLONG FileOffset,
313 PVOID* BaseAddress,
314 PBOOLEAN UptoDate,
315 PROS_VACB *Vacb
316 );
317
318 NTSTATUS
319 NTAPI
320 CcRosInitializeFileCache(
321 PFILE_OBJECT FileObject,
322 PCC_FILE_SIZES FileSizes,
323 PCACHE_MANAGER_CALLBACKS CallBacks,
324 PVOID LazyWriterContext
325 );
326
327 NTSTATUS
328 NTAPI
329 CcRosReleaseFileCache(
330 PFILE_OBJECT FileObject
331 );
332
333 NTSTATUS
334 NTAPI
335 CcTryToInitializeFileCache(PFILE_OBJECT FileObject);
336
337 FORCEINLINE
338 BOOLEAN
339 DoRangesIntersect(
340 _In_ LONGLONG Offset1,
341 _In_ LONGLONG Length1,
342 _In_ LONGLONG Offset2,
343 _In_ LONGLONG Length2)
344 {
345 if (Offset1 + Length1 <= Offset2)
346 return FALSE;
347 if (Offset2 + Length2 <= Offset1)
348 return FALSE;
349 return TRUE;
350 }
351
352 FORCEINLINE
353 BOOLEAN
354 IsPointInRange(
355 _In_ LONGLONG Offset1,
356 _In_ LONGLONG Length1,
357 _In_ LONGLONG Point)
358 {
359 return DoRangesIntersect(Offset1, Length1, Point, 1);
360 }