[NTOSKRNL] Add an helper for marking a VACB clean (ie, not dirty).
[reactos.git] / 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 /* GLOBALS *****************************************************************/
17
18 extern KGUARDED_MUTEX ViewLock;
19
20 NTSTATUS CcRosInternalFreeVacb(PROS_VACB Vacb);
21
22 /* FUNCTIONS *****************************************************************/
23
24 /*
25 * @unimplemented
26 */
27 LARGE_INTEGER
28 NTAPI
29 CcGetDirtyPages (
30 IN PVOID LogHandle,
31 IN PDIRTY_PAGE_ROUTINE DirtyPageRoutine,
32 IN PVOID Context1,
33 IN PVOID Context2)
34 {
35 LARGE_INTEGER i;
36
37 CCTRACE(CC_API_DEBUG, "LogHandle=%p DirtyPageRoutine=%p Context1=%p Context2=%p\n",
38 LogHandle, DirtyPageRoutine, Context1, Context2);
39
40 UNIMPLEMENTED;
41 i.QuadPart = 0;
42 return i;
43 }
44
45 /*
46 * @implemented
47 */
48 PFILE_OBJECT
49 NTAPI
50 CcGetFileObjectFromBcb (
51 IN PVOID Bcb)
52 {
53 PINTERNAL_BCB iBcb = (PINTERNAL_BCB)Bcb;
54
55 CCTRACE(CC_API_DEBUG, "Bcb=%p\n", Bcb);
56
57 return iBcb->Vacb->SharedCacheMap->FileObject;
58 }
59
60 /*
61 * @unimplemented
62 */
63 LARGE_INTEGER
64 NTAPI
65 CcGetLsnForFileObject (
66 IN PFILE_OBJECT FileObject,
67 OUT PLARGE_INTEGER OldestLsn OPTIONAL)
68 {
69 LARGE_INTEGER i;
70
71 CCTRACE(CC_API_DEBUG, "FileObject=%p\n", FileObject);
72
73 UNIMPLEMENTED;
74 i.QuadPart = 0;
75 return i;
76 }
77
78 /*
79 * @unimplemented
80 */
81 VOID
82 NTAPI
83 CcInitializeCacheMap (
84 IN PFILE_OBJECT FileObject,
85 IN PCC_FILE_SIZES FileSizes,
86 IN BOOLEAN PinAccess,
87 IN PCACHE_MANAGER_CALLBACKS CallBacks,
88 IN PVOID LazyWriterContext)
89 {
90 NTSTATUS Status;
91
92 ASSERT(FileObject);
93 ASSERT(FileSizes);
94
95 CCTRACE(CC_API_DEBUG, "FileObject=%p FileSizes=%p PinAccess=%d CallBacks=%p LazyWriterContext=%p\n",
96 FileObject, FileSizes, PinAccess, CallBacks, LazyWriterContext);
97
98 /* Call old ROS cache init function */
99 Status = CcRosInitializeFileCache(FileObject,
100 FileSizes,
101 PinAccess,
102 CallBacks,
103 LazyWriterContext);
104 if (!NT_SUCCESS(Status))
105 ExRaiseStatus(Status);
106 }
107
108 /*
109 * @implemented
110 */
111 BOOLEAN
112 NTAPI
113 CcIsThereDirtyData (
114 IN PVPB Vpb)
115 {
116 PROS_VACB Vacb;
117 PLIST_ENTRY Entry;
118 /* Assume no dirty data */
119 BOOLEAN Dirty = FALSE;
120
121 CCTRACE(CC_API_DEBUG, "Vpb=%p\n", Vpb);
122
123 KeAcquireGuardedMutex(&ViewLock);
124
125 /* Browse dirty VACBs */
126 for (Entry = DirtyVacbListHead.Flink; Entry != &DirtyVacbListHead; Entry = Entry->Flink)
127 {
128 Vacb = CONTAINING_RECORD(Entry, ROS_VACB, DirtyVacbListEntry);
129 /* Look for these associated with our volume */
130 if (Vacb->SharedCacheMap->FileObject->Vpb != Vpb)
131 {
132 continue;
133 }
134
135 /* From now on, we are associated with our VPB */
136
137 /* Temporary files are not counted as dirty */
138 if (BooleanFlagOn(Vacb->SharedCacheMap->FileObject->Flags, FO_TEMPORARY_FILE))
139 {
140 continue;
141 }
142
143 /* A single dirty VACB is enough to have dirty data */
144 if (Vacb->Dirty)
145 {
146 Dirty = TRUE;
147 break;
148 }
149 }
150
151 KeReleaseGuardedMutex(&ViewLock);
152
153 return Dirty;
154 }
155
156 /*
157 * @unimplemented
158 */
159 BOOLEAN
160 NTAPI
161 CcPurgeCacheSection (
162 IN PSECTION_OBJECT_POINTERS SectionObjectPointer,
163 IN PLARGE_INTEGER FileOffset OPTIONAL,
164 IN ULONG Length,
165 IN BOOLEAN UninitializeCacheMaps)
166 {
167 PROS_SHARED_CACHE_MAP SharedCacheMap;
168 LONGLONG StartOffset;
169 LONGLONG EndOffset;
170 LIST_ENTRY FreeList;
171 KIRQL OldIrql;
172 PLIST_ENTRY ListEntry;
173 PROS_VACB Vacb;
174 LONGLONG ViewEnd;
175 BOOLEAN Success;
176
177 CCTRACE(CC_API_DEBUG, "SectionObjectPointer=%p\n FileOffset=%p Length=%lu UninitializeCacheMaps=%d",
178 SectionObjectPointer, FileOffset, Length, UninitializeCacheMaps);
179
180 if (UninitializeCacheMaps)
181 {
182 DPRINT1("FIXME: CcPurgeCacheSection not uninitializing private cache maps\n");
183 }
184
185 SharedCacheMap = SectionObjectPointer->SharedCacheMap;
186 if (!SharedCacheMap)
187 return FALSE;
188
189 StartOffset = FileOffset != NULL ? FileOffset->QuadPart : 0;
190 if (Length == 0 || FileOffset == NULL)
191 {
192 EndOffset = MAXLONGLONG;
193 }
194 else
195 {
196 EndOffset = StartOffset + Length;
197 ASSERT(EndOffset > StartOffset);
198 }
199
200 InitializeListHead(&FreeList);
201
202 /* Assume success */
203 Success = TRUE;
204
205 KeAcquireGuardedMutex(&ViewLock);
206 KeAcquireSpinLock(&SharedCacheMap->CacheMapLock, &OldIrql);
207 ListEntry = SharedCacheMap->CacheMapVacbListHead.Flink;
208 while (ListEntry != &SharedCacheMap->CacheMapVacbListHead)
209 {
210 Vacb = CONTAINING_RECORD(ListEntry, ROS_VACB, CacheMapVacbListEntry);
211 ListEntry = ListEntry->Flink;
212
213 /* Skip VACBs outside the range, or only partially in range */
214 if (Vacb->FileOffset.QuadPart < StartOffset)
215 {
216 continue;
217 }
218 ViewEnd = min(Vacb->FileOffset.QuadPart + VACB_MAPPING_GRANULARITY,
219 SharedCacheMap->SectionSize.QuadPart);
220 if (ViewEnd >= EndOffset)
221 {
222 break;
223 }
224
225 /* Still in use, it cannot be purged, fail */
226 if (Vacb->ReferenceCount != 0 && !Vacb->Dirty)
227 {
228 Success = FALSE;
229 break;
230 }
231
232 /* This VACB is in range, so unlink it and mark for free */
233 RemoveEntryList(&Vacb->VacbLruListEntry);
234 if (Vacb->Dirty)
235 {
236 CcRosUnmarkDirtyVacb(Vacb, FALSE);
237 }
238 RemoveEntryList(&Vacb->CacheMapVacbListEntry);
239 InsertHeadList(&FreeList, &Vacb->CacheMapVacbListEntry);
240 }
241 KeReleaseSpinLock(&SharedCacheMap->CacheMapLock, OldIrql);
242 KeReleaseGuardedMutex(&ViewLock);
243
244 while (!IsListEmpty(&FreeList))
245 {
246 Vacb = CONTAINING_RECORD(RemoveHeadList(&FreeList),
247 ROS_VACB,
248 CacheMapVacbListEntry);
249 CcRosInternalFreeVacb(Vacb);
250 }
251
252 return Success;
253 }
254
255
256 /*
257 * @implemented
258 */
259 VOID NTAPI
260 CcSetFileSizes (
261 IN PFILE_OBJECT FileObject,
262 IN PCC_FILE_SIZES FileSizes)
263 {
264 KIRQL oldirql;
265 PROS_SHARED_CACHE_MAP SharedCacheMap;
266
267 CCTRACE(CC_API_DEBUG, "FileObject=%p FileSizes=%p\n",
268 FileObject, FileSizes);
269
270 DPRINT("CcSetFileSizes(FileObject 0x%p, FileSizes 0x%p)\n",
271 FileObject, FileSizes);
272 DPRINT("AllocationSize %I64d, FileSize %I64d, ValidDataLength %I64d\n",
273 FileSizes->AllocationSize.QuadPart,
274 FileSizes->FileSize.QuadPart,
275 FileSizes->ValidDataLength.QuadPart);
276
277 SharedCacheMap = FileObject->SectionObjectPointer->SharedCacheMap;
278
279 /*
280 * It is valid to call this function on file objects that weren't
281 * initialized for caching. In this case it's simple no-op.
282 */
283 if (SharedCacheMap == NULL)
284 return;
285
286 if (FileSizes->AllocationSize.QuadPart < SharedCacheMap->SectionSize.QuadPart)
287 {
288 CcPurgeCacheSection(FileObject->SectionObjectPointer,
289 &FileSizes->AllocationSize,
290 0,
291 FALSE);
292 }
293
294 KeAcquireSpinLock(&SharedCacheMap->CacheMapLock, &oldirql);
295 SharedCacheMap->SectionSize = FileSizes->AllocationSize;
296 SharedCacheMap->FileSize = FileSizes->FileSize;
297 KeReleaseSpinLock(&SharedCacheMap->CacheMapLock, oldirql);
298 }
299
300 /*
301 * @unimplemented
302 */
303 VOID
304 NTAPI
305 CcSetLogHandleForFile (
306 IN PFILE_OBJECT FileObject,
307 IN PVOID LogHandle,
308 IN PFLUSH_TO_LSN FlushToLsnRoutine)
309 {
310 CCTRACE(CC_API_DEBUG, "FileObject=%p LogHandle=%p FlushToLsnRoutine=%p\n",
311 FileObject, LogHandle, FlushToLsnRoutine);
312
313 UNIMPLEMENTED;
314 }
315
316 /*
317 * @unimplemented
318 */
319 BOOLEAN
320 NTAPI
321 CcUninitializeCacheMap (
322 IN PFILE_OBJECT FileObject,
323 IN PLARGE_INTEGER TruncateSize OPTIONAL,
324 IN PCACHE_UNINITIALIZE_EVENT UninitializeCompleteEvent OPTIONAL)
325 {
326 NTSTATUS Status;
327 PROS_SHARED_CACHE_MAP SharedCacheMap;
328 KIRQL OldIrql;
329
330 CCTRACE(CC_API_DEBUG, "FileObject=%p TruncateSize=%p UninitializeCompleteEvent=%p\n",
331 FileObject, TruncateSize, UninitializeCompleteEvent);
332
333 if (TruncateSize != NULL &&
334 FileObject->SectionObjectPointer->SharedCacheMap != NULL)
335 {
336 SharedCacheMap = FileObject->SectionObjectPointer->SharedCacheMap;
337 KeAcquireSpinLock(&SharedCacheMap->CacheMapLock, &OldIrql);
338 if (SharedCacheMap->FileSize.QuadPart > TruncateSize->QuadPart)
339 {
340 SharedCacheMap->FileSize = *TruncateSize;
341 }
342 KeReleaseSpinLock(&SharedCacheMap->CacheMapLock, OldIrql);
343 CcPurgeCacheSection(FileObject->SectionObjectPointer,
344 TruncateSize,
345 0,
346 FALSE);
347 }
348
349 Status = CcRosReleaseFileCache(FileObject);
350 if (UninitializeCompleteEvent)
351 {
352 KeSetEvent(&UninitializeCompleteEvent->Event, IO_NO_INCREMENT, FALSE);
353 }
354 return NT_SUCCESS(Status);
355 }
356
357 BOOLEAN
358 NTAPI
359 CcGetFileSizes (
360 IN PFILE_OBJECT FileObject,
361 IN PCC_FILE_SIZES FileSizes)
362 {
363 PROS_SHARED_CACHE_MAP SharedCacheMap;
364
365 SharedCacheMap = FileObject->SectionObjectPointer->SharedCacheMap;
366
367 if (!SharedCacheMap)
368 return FALSE;
369
370 FileSizes->AllocationSize = SharedCacheMap->SectionSize;
371 FileSizes->FileSize = FileSizes->ValidDataLength = SharedCacheMap->FileSize;
372 return TRUE;
373 }