[NTOS:CC]
[reactos.git] / reactos / ntoskrnl / include / internal / cc.h
1 #pragma once
2
3 typedef struct _PF_SCENARIO_ID
4 {
5 WCHAR ScenName[30];
6 ULONG HashId;
7 } PF_SCENARIO_ID, *PPF_SCENARIO_ID;
8
9 typedef struct _PF_LOG_ENTRY
10 {
11 ULONG FileOffset:30;
12 ULONG Type:2;
13 union
14 {
15 ULONG FileKey;
16 ULONG FileSequenceNumber;
17 };
18 } PF_LOG_ENTRY, *PPF_LOG_ENTRY;
19
20 typedef struct _PFSN_LOG_ENTRIES
21 {
22 LIST_ENTRY TraceBuffersLink;
23 LONG NumEntries;
24 LONG MaxEntries;
25 PF_LOG_ENTRY Entries[ANYSIZE_ARRAY];
26 } PFSN_LOG_ENTRIES, *PPFSN_LOG_ENTRIES;
27
28 typedef struct _PF_SECTION_INFO
29 {
30 ULONG FileKey;
31 ULONG FileSequenceNumber;
32 ULONG FileIdLow;
33 ULONG FileIdHigh;
34 } PF_SECTION_INFO, *PPF_SECTION_INFO;
35
36 typedef struct _PF_TRACE_HEADER
37 {
38 ULONG Version;
39 ULONG MagicNumber;
40 ULONG Size;
41 PF_SCENARIO_ID ScenarioId;
42 ULONG ScenarioType; // PF_SCENARIO_TYPE
43 ULONG EventEntryIdxs[8];
44 ULONG NumEventEntryIdxs;
45 ULONG TraceBufferOffset;
46 ULONG NumEntries;
47 ULONG SectionInfoOffset;
48 ULONG NumSections;
49 ULONG FaultsPerPeriod[10];
50 LARGE_INTEGER LaunchTime;
51 ULONGLONG Reserved[5];
52 } PF_TRACE_HEADER, *PPF_TRACE_HEADER;
53
54 typedef struct _PFSN_TRACE_DUMP
55 {
56 LIST_ENTRY CompletedTracesLink;
57 PF_TRACE_HEADER Trace;
58 } PFSN_TRACE_DUMP, *PPFSN_TRACE_DUMP;
59
60 typedef struct _PFSN_TRACE_HEADER
61 {
62 ULONG Magic;
63 LIST_ENTRY ActiveTracesLink;
64 PF_SCENARIO_ID ScenarioId;
65 ULONG ScenarioType; // PF_SCENARIO_TYPE
66 ULONG EventEntryIdxs[8];
67 ULONG NumEventEntryIdxs;
68 PPFSN_LOG_ENTRIES CurrentTraceBuffer;
69 LIST_ENTRY TraceBuffersList;
70 ULONG NumTraceBuffers;
71 KSPIN_LOCK TraceBufferSpinLock;
72 KTIMER TraceTimer;
73 LARGE_INTEGER TraceTimerPeriod;
74 KDPC TraceTimerDpc;
75 KSPIN_LOCK TraceTimerSpinLock;
76 ULONG FaultsPerPeriod[10];
77 LONG LastNumFaults;
78 LONG CurPeriod;
79 LONG NumFaults;
80 LONG MaxFaults;
81 PEPROCESS Process;
82 EX_RUNDOWN_REF RefCount;
83 WORK_QUEUE_ITEM EndTraceWorkItem;
84 LONG EndTraceCalled;
85 PPFSN_TRACE_DUMP TraceDump;
86 NTSTATUS TraceDumpStatus;
87 LARGE_INTEGER LaunchTime;
88 PPF_SECTION_INFO SectionInfo;
89 ULONG SectionInfoCount;
90 } PFSN_TRACE_HEADER, *PPFSN_TRACE_HEADER;
91
92 typedef struct _PFSN_PREFETCHER_GLOBALS
93 {
94 LIST_ENTRY ActiveTraces;
95 KSPIN_LOCK ActiveTracesLock;
96 PPFSN_TRACE_HEADER SystemWideTrace;
97 LIST_ENTRY CompletedTraces;
98 FAST_MUTEX CompletedTracesLock;
99 LONG NumCompletedTraces;
100 PKEVENT CompletedTracesEvent;
101 LONG ActivePrefetches;
102 } PFSN_PREFETCHER_GLOBALS, *PPFSN_PREFETCHER_GLOBALS;
103
104 typedef struct _BCB
105 {
106 LIST_ENTRY BcbSegmentListHead;
107 LIST_ENTRY BcbRemoveListEntry;
108 BOOLEAN RemoveOnClose;
109 ULONG TimeStamp;
110 PFILE_OBJECT FileObject;
111 LARGE_INTEGER AllocationSize;
112 LARGE_INTEGER FileSize;
113 PCACHE_MANAGER_CALLBACKS Callbacks;
114 PVOID LazyWriteContext;
115 KSPIN_LOCK BcbLock;
116 ULONG RefCount;
117 #if DBG
118 BOOLEAN Trace; /* enable extra trace output for this BCB and it's cache segments */
119 #endif
120 } BCB, *PBCB;
121
122 typedef struct _CACHE_SEGMENT
123 {
124 /* Base address of the region where the cache segment data is mapped. */
125 PVOID BaseAddress;
126 /*
127 * Memory area representing the region where the cache segment data is
128 * mapped.
129 */
130 struct _MEMORY_AREA* MemoryArea;
131 /* Are the contents of the cache segment data valid. */
132 BOOLEAN Valid;
133 /* Are the contents of the cache segment data newer than those on disk. */
134 BOOLEAN Dirty;
135 /* Page out in progress */
136 BOOLEAN PageOut;
137 ULONG MappedCount;
138 /* Entry in the list of segments for this BCB. */
139 LIST_ENTRY BcbSegmentListEntry;
140 /* Entry in the list of segments which are dirty. */
141 LIST_ENTRY DirtySegmentListEntry;
142 /* Entry in the list of segments. */
143 LIST_ENTRY CacheSegmentListEntry;
144 LIST_ENTRY CacheSegmentLRUListEntry;
145 /* Offset in the file which this cache segment maps. */
146 ULONG FileOffset;
147 /* Mutex */
148 KMUTEX Mutex;
149 /* Number of references. */
150 ULONG ReferenceCount;
151 /* Pointer to the BCB for the file which this cache segment maps data for. */
152 PBCB Bcb;
153 /* Pointer to the next cache segment in a chain. */
154 struct _CACHE_SEGMENT* NextInChain;
155 } CACHE_SEGMENT, *PCACHE_SEGMENT;
156
157 typedef struct _INTERNAL_BCB
158 {
159 PUBLIC_BCB PFCB;
160 PCACHE_SEGMENT CacheSegment;
161 BOOLEAN Dirty;
162 CSHORT RefCount; /* (At offset 0x34 on WinNT4) */
163 } INTERNAL_BCB, *PINTERNAL_BCB;
164
165 VOID
166 NTAPI
167 CcPfInitializePrefetcher(
168 VOID
169 );
170
171 VOID
172 NTAPI
173 CcMdlReadComplete2(
174 IN PFILE_OBJECT FileObject,
175 IN PMDL MemoryDescriptorList
176 );
177
178 VOID
179 NTAPI
180 CcMdlWriteComplete2(
181 IN PFILE_OBJECT FileObject,
182 IN PLARGE_INTEGER FileOffset,
183 IN PMDL MdlChain
184 );
185
186 NTSTATUS
187 NTAPI
188 CcRosFlushCacheSegment(PCACHE_SEGMENT CacheSegment);
189
190 NTSTATUS
191 NTAPI
192 CcRosGetCacheSegment(
193 PBCB Bcb,
194 ULONG FileOffset,
195 PULONG BaseOffset,
196 PVOID *BaseAddress,
197 PBOOLEAN UptoDate,
198 PCACHE_SEGMENT *CacheSeg
199 );
200
201 VOID
202 NTAPI
203 CcInitView(VOID);
204
205 NTSTATUS
206 NTAPI
207 ReadCacheSegment(PCACHE_SEGMENT CacheSeg);
208
209 NTSTATUS
210 NTAPI
211 WriteCacheSegment(PCACHE_SEGMENT CacheSeg);
212
213 BOOLEAN
214 NTAPI
215 CcInitializeCacheManager(VOID);
216
217 NTSTATUS
218 NTAPI
219 CcRosUnmapCacheSegment(
220 PBCB Bcb,
221 ULONG FileOffset,
222 BOOLEAN NowDirty
223 );
224
225 PCACHE_SEGMENT
226 NTAPI
227 CcRosLookupCacheSegment(
228 PBCB Bcb,
229 ULONG FileOffset
230 );
231
232 NTSTATUS
233 NTAPI
234 CcRosGetCacheSegmentChain(
235 PBCB Bcb,
236 ULONG FileOffset,
237 ULONG Length,
238 PCACHE_SEGMENT* CacheSeg
239 );
240
241 VOID
242 NTAPI
243 CcInitCacheZeroPage(VOID);
244
245 NTSTATUS
246 NTAPI
247 CcRosMarkDirtyCacheSegment(
248 PBCB Bcb,
249 ULONG FileOffset
250 );
251
252 NTSTATUS
253 NTAPI
254 CcRosFlushDirtyPages(
255 ULONG Target,
256 PULONG Count,
257 BOOLEAN Wait
258 );
259
260 VOID
261 NTAPI
262 CcRosDereferenceCache(PFILE_OBJECT FileObject);
263
264 VOID
265 NTAPI
266 CcRosReferenceCache(PFILE_OBJECT FileObject);
267
268 VOID
269 NTAPI
270 CcRosSetRemoveOnClose(PSECTION_OBJECT_POINTERS SectionObjectPointer);
271
272 NTSTATUS
273 NTAPI
274 CcRosReleaseCacheSegment(
275 BCB* Bcb,
276 CACHE_SEGMENT *CacheSeg,
277 BOOLEAN Valid,
278 BOOLEAN Dirty,
279 BOOLEAN Mapped
280 );
281
282 NTSTATUS
283 NTAPI
284 CcRosRequestCacheSegment(
285 BCB *Bcb,
286 ULONG FileOffset,
287 PVOID* BaseAddress,
288 PBOOLEAN UptoDate,
289 CACHE_SEGMENT **CacheSeg
290 );
291
292 NTSTATUS
293 NTAPI
294 CcRosInitializeFileCache(
295 PFILE_OBJECT FileObject,
296 PCACHE_MANAGER_CALLBACKS CallBacks,
297 PVOID LazyWriterContext
298 );
299
300 NTSTATUS
301 NTAPI
302 CcRosReleaseFileCache(
303 PFILE_OBJECT FileObject
304 );
305
306 NTSTATUS
307 NTAPI
308 CcTryToInitializeFileCache(PFILE_OBJECT FileObject);
309
310 FORCEINLINE
311 BOOLEAN
312 DoSegmentsIntersect(
313 _In_ ULONG Offset1,
314 _In_ ULONG Length1,
315 _In_ ULONG Offset2,
316 _In_ ULONG Length2)
317 {
318 if (Offset1 + Length1 <= Offset2)
319 return FALSE;
320 if (Offset2 + Length2 <= Offset1)
321 return FALSE;
322 return TRUE;
323 }
324
325 FORCEINLINE
326 BOOLEAN
327 IsPointInSegment(
328 _In_ ULONG Offset1,
329 _In_ ULONG Length1,
330 _In_ ULONG Point)
331 {
332 return DoSegmentsIntersect(Offset1, Length1, Point, 1);
333 }