[NTOSKRNL] CcPurgeCacheSection: Check if SharedCacheMap is NULL. Change suggested...
[reactos.git] / reactos / ntoskrnl / cc / fs.c
1 /*
2 * COPYRIGHT: See COPYING in the top level directory
3 * PROJECT: ReactOS kernel
4 * FILE: ntoskrnl/cc/fs.c
5 * PURPOSE: Implements cache managers functions useful for File Systems
6 *
7 * PROGRAMMERS: Alex Ionescu
8 */
9
10 /* INCLUDES ******************************************************************/
11
12 #include <ntoskrnl.h>
13 #define NDEBUG
14 #include <debug.h>
15
16 #ifndef VACB_MAPPING_GRANULARITY
17 #define VACB_MAPPING_GRANULARITY (256 * 1024)
18 #endif
19
20 /* GLOBALS *****************************************************************/
21
22 extern KGUARDED_MUTEX ViewLock;
23 extern ULONG DirtyPageCount;
24
25 NTSTATUS CcRosInternalFreeVacb(PROS_VACB Vacb);
26
27 /* FUNCTIONS *****************************************************************/
28
29 /*
30 * @unimplemented
31 */
32 LARGE_INTEGER
33 NTAPI
34 CcGetDirtyPages (
35 IN PVOID LogHandle,
36 IN PDIRTY_PAGE_ROUTINE DirtyPageRoutine,
37 IN PVOID Context1,
38 IN PVOID Context2)
39 {
40 LARGE_INTEGER i;
41
42 CCTRACE(CC_API_DEBUG, "LogHandle=%p DirtyPageRoutine=%p Context1=%p Context2=%p\n",
43 LogHandle, DirtyPageRoutine, Context1, Context2);
44
45 UNIMPLEMENTED;
46 i.QuadPart = 0;
47 return i;
48 }
49
50 /*
51 * @implemented
52 */
53 PFILE_OBJECT
54 NTAPI
55 CcGetFileObjectFromBcb (
56 IN PVOID Bcb)
57 {
58 PINTERNAL_BCB iBcb = (PINTERNAL_BCB)Bcb;
59
60 CCTRACE(CC_API_DEBUG, "Bcb=%p\n", Bcb);
61
62 return iBcb->Vacb->SharedCacheMap->FileObject;
63 }
64
65 /*
66 * @unimplemented
67 */
68 LARGE_INTEGER
69 NTAPI
70 CcGetLsnForFileObject (
71 IN PFILE_OBJECT FileObject,
72 OUT PLARGE_INTEGER OldestLsn OPTIONAL)
73 {
74 LARGE_INTEGER i;
75
76 CCTRACE(CC_API_DEBUG, "FileObject=%p\n", FileObject);
77
78 UNIMPLEMENTED;
79 i.QuadPart = 0;
80 return i;
81 }
82
83 /*
84 * @unimplemented
85 */
86 VOID
87 NTAPI
88 CcInitializeCacheMap (
89 IN PFILE_OBJECT FileObject,
90 IN PCC_FILE_SIZES FileSizes,
91 IN BOOLEAN PinAccess,
92 IN PCACHE_MANAGER_CALLBACKS CallBacks,
93 IN PVOID LazyWriterContext)
94 {
95 NTSTATUS Status;
96
97 ASSERT(FileObject);
98 ASSERT(FileSizes);
99
100 CCTRACE(CC_API_DEBUG, "FileObject=%p FileSizes=%p PinAccess=%d CallBacks=%p LazyWriterContext=%p\n",
101 FileObject, FileSizes, PinAccess, CallBacks, LazyWriterContext);
102
103 /* Call old ROS cache init function */
104 Status = CcRosInitializeFileCache(FileObject,
105 FileSizes,
106 PinAccess,
107 CallBacks,
108 LazyWriterContext);
109 if (!NT_SUCCESS(Status))
110 ExRaiseStatus(Status);
111 }
112
113 /*
114 * @unimplemented
115 */
116 BOOLEAN
117 NTAPI
118 CcIsThereDirtyData (
119 IN PVPB Vpb)
120 {
121 CCTRACE(CC_API_DEBUG, "Vpb=%p\n", Vpb);
122
123 UNIMPLEMENTED;
124 return FALSE;
125 }
126
127 /*
128 * @unimplemented
129 */
130 BOOLEAN
131 NTAPI
132 CcPurgeCacheSection (
133 IN PSECTION_OBJECT_POINTERS SectionObjectPointer,
134 IN PLARGE_INTEGER FileOffset OPTIONAL,
135 IN ULONG Length,
136 IN BOOLEAN UninitializeCacheMaps)
137 {
138 PROS_SHARED_CACHE_MAP SharedCacheMap;
139 LONGLONG StartOffset;
140 LONGLONG EndOffset;
141 LIST_ENTRY FreeList;
142 KIRQL OldIrql;
143 PLIST_ENTRY ListEntry;
144 PROS_VACB Vacb;
145 LONGLONG ViewEnd;
146
147 CCTRACE(CC_API_DEBUG, "SectionObjectPointer=%p\n FileOffset=%p Length=%lu UninitializeCacheMaps=%d",
148 SectionObjectPointer, FileOffset, Length, UninitializeCacheMaps);
149
150 if (UninitializeCacheMaps)
151 {
152 DPRINT1("FIXME: CcPurgeCacheSection not uninitializing private cache maps\n");
153 }
154
155 SharedCacheMap = SectionObjectPointer->SharedCacheMap;
156 if (!SharedCacheMap)
157 return FALSE;
158
159 StartOffset = FileOffset != NULL ? FileOffset->QuadPart : 0;
160 if (Length == 0 || FileOffset == NULL)
161 {
162 EndOffset = MAXLONGLONG;
163 }
164 else
165 {
166 EndOffset = StartOffset + Length;
167 ASSERT(EndOffset > StartOffset);
168 }
169
170 InitializeListHead(&FreeList);
171
172 KeAcquireGuardedMutex(&ViewLock);
173 KeAcquireSpinLock(&SharedCacheMap->CacheMapLock, &OldIrql);
174 ListEntry = SharedCacheMap->CacheMapVacbListHead.Flink;
175 while (ListEntry != &SharedCacheMap->CacheMapVacbListHead)
176 {
177 Vacb = CONTAINING_RECORD(ListEntry, ROS_VACB, CacheMapVacbListEntry);
178 ListEntry = ListEntry->Flink;
179
180 /* Skip VACBs outside the range, or only partially in range */
181 if (Vacb->FileOffset.QuadPart < StartOffset)
182 {
183 continue;
184 }
185 ViewEnd = min(Vacb->FileOffset.QuadPart + VACB_MAPPING_GRANULARITY,
186 SharedCacheMap->SectionSize.QuadPart);
187 if (ViewEnd >= EndOffset)
188 {
189 break;
190 }
191
192 ASSERT((Vacb->ReferenceCount == 0) ||
193 (Vacb->ReferenceCount == 1 && Vacb->Dirty));
194
195 /* This VACB is in range, so unlink it and mark for free */
196 RemoveEntryList(&Vacb->VacbLruListEntry);
197 if (Vacb->Dirty)
198 {
199 RemoveEntryList(&Vacb->DirtyVacbListEntry);
200 DirtyPageCount -= VACB_MAPPING_GRANULARITY / PAGE_SIZE;
201 }
202 RemoveEntryList(&Vacb->CacheMapVacbListEntry);
203 InsertHeadList(&FreeList, &Vacb->CacheMapVacbListEntry);
204 }
205 KeReleaseSpinLock(&SharedCacheMap->CacheMapLock, OldIrql);
206 KeReleaseGuardedMutex(&ViewLock);
207
208 while (!IsListEmpty(&FreeList))
209 {
210 Vacb = CONTAINING_RECORD(RemoveHeadList(&FreeList),
211 ROS_VACB,
212 CacheMapVacbListEntry);
213 CcRosInternalFreeVacb(Vacb);
214 }
215
216 return FALSE;
217 }
218
219
220 /*
221 * @implemented
222 */
223 VOID NTAPI
224 CcSetFileSizes (
225 IN PFILE_OBJECT FileObject,
226 IN PCC_FILE_SIZES FileSizes)
227 {
228 KIRQL oldirql;
229 PROS_SHARED_CACHE_MAP SharedCacheMap;
230
231 CCTRACE(CC_API_DEBUG, "FileObject=%p FileSizes=%p\n",
232 FileObject, FileSizes);
233
234 DPRINT("CcSetFileSizes(FileObject 0x%p, FileSizes 0x%p)\n",
235 FileObject, FileSizes);
236 DPRINT("AllocationSize %I64d, FileSize %I64d, ValidDataLength %I64d\n",
237 FileSizes->AllocationSize.QuadPart,
238 FileSizes->FileSize.QuadPart,
239 FileSizes->ValidDataLength.QuadPart);
240
241 SharedCacheMap = FileObject->SectionObjectPointer->SharedCacheMap;
242
243 /*
244 * It is valid to call this function on file objects that weren't
245 * initialized for caching. In this case it's simple no-op.
246 */
247 if (SharedCacheMap == NULL)
248 return;
249
250 if (FileSizes->AllocationSize.QuadPart < SharedCacheMap->SectionSize.QuadPart)
251 {
252 CcPurgeCacheSection(FileObject->SectionObjectPointer,
253 &FileSizes->AllocationSize,
254 0,
255 FALSE);
256 }
257
258 KeAcquireSpinLock(&SharedCacheMap->CacheMapLock, &oldirql);
259 SharedCacheMap->SectionSize = FileSizes->AllocationSize;
260 SharedCacheMap->FileSize = FileSizes->FileSize;
261 KeReleaseSpinLock(&SharedCacheMap->CacheMapLock, oldirql);
262 }
263
264 /*
265 * @unimplemented
266 */
267 VOID
268 NTAPI
269 CcSetLogHandleForFile (
270 IN PFILE_OBJECT FileObject,
271 IN PVOID LogHandle,
272 IN PFLUSH_TO_LSN FlushToLsnRoutine)
273 {
274 CCTRACE(CC_API_DEBUG, "FileObject=%p LogHandle=%p FlushToLsnRoutine=%p\n",
275 FileObject, LogHandle, FlushToLsnRoutine);
276
277 UNIMPLEMENTED;
278 }
279
280 /*
281 * @unimplemented
282 */
283 BOOLEAN
284 NTAPI
285 CcUninitializeCacheMap (
286 IN PFILE_OBJECT FileObject,
287 IN PLARGE_INTEGER TruncateSize OPTIONAL,
288 IN PCACHE_UNINITIALIZE_EVENT UninitializeCompleteEvent OPTIONAL)
289 {
290 NTSTATUS Status;
291 PROS_SHARED_CACHE_MAP SharedCacheMap;
292 KIRQL OldIrql;
293
294 CCTRACE(CC_API_DEBUG, "FileObject=%p TruncateSize=%p UninitializeCompleteEvent=%p\n",
295 FileObject, TruncateSize, UninitializeCompleteEvent);
296
297 if (TruncateSize != NULL &&
298 FileObject->SectionObjectPointer != NULL &&
299 FileObject->SectionObjectPointer->SharedCacheMap != NULL)
300 {
301 SharedCacheMap = FileObject->SectionObjectPointer->SharedCacheMap;
302 KeAcquireSpinLock(&SharedCacheMap->CacheMapLock, &OldIrql);
303 if (SharedCacheMap->FileSize.QuadPart > TruncateSize->QuadPart)
304 {
305 SharedCacheMap->FileSize = *TruncateSize;
306 }
307 KeReleaseSpinLock(&SharedCacheMap->CacheMapLock, OldIrql);
308 CcPurgeCacheSection(FileObject->SectionObjectPointer,
309 TruncateSize,
310 0,
311 FALSE);
312 }
313
314 Status = CcRosReleaseFileCache(FileObject);
315 if (UninitializeCompleteEvent)
316 {
317 KeSetEvent(&UninitializeCompleteEvent->Event, IO_NO_INCREMENT, FALSE);
318 }
319 return NT_SUCCESS(Status);
320 }
321
322 BOOLEAN
323 NTAPI
324 CcGetFileSizes (
325 IN PFILE_OBJECT FileObject,
326 IN PCC_FILE_SIZES FileSizes)
327 {
328 PROS_SHARED_CACHE_MAP SharedCacheMap;
329
330 SharedCacheMap = FileObject->SectionObjectPointer->SharedCacheMap;
331
332 if (!SharedCacheMap)
333 return FALSE;
334
335 FileSizes->AllocationSize = SharedCacheMap->SectionSize;
336 FileSizes->FileSize = FileSizes->ValidDataLength = SharedCacheMap->FileSize;
337 return TRUE;
338 }