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