ReactOS 0.4.15-dev-5669-g09dde2c
fs.c
Go to the documentation of this file.
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
14#define NDEBUG
15#include <debug.h>
16
17/* FUNCTIONS *****************************************************************/
18
19/*
20 * @unimplemented
21 */
26 IN PDIRTY_PAGE_ROUTINE DirtyPageRoutine,
29{
31
32 CCTRACE(CC_API_DEBUG, "LogHandle=%p DirtyPageRoutine=%p Context1=%p Context2=%p\n",
33 LogHandle, DirtyPageRoutine, Context1, Context2);
34
36 i.QuadPart = 0;
37 return i;
38}
39
40/*
41 * @implemented
42 */
46 IN PVOID Bcb)
47{
49
50 CCTRACE(CC_API_DEBUG, "Bcb=%p\n", Bcb);
51
52 return iBcb->Vacb->SharedCacheMap->FileObject;
53}
54
55/*
56 * @unimplemented
57 */
62 OUT PLARGE_INTEGER OldestLsn OPTIONAL)
63{
65
66 CCTRACE(CC_API_DEBUG, "FileObject=%p\n", FileObject);
67
69 i.QuadPart = 0;
70 return i;
71}
72
73/*
74 * @unimplemented
75 */
76VOID
81 IN BOOLEAN PinAccess,
83 IN PVOID LazyWriterContext)
84{
86
89
90 CCTRACE(CC_API_DEBUG, "FileObject=%p FileSizes=%p PinAccess=%d CallBacks=%p LazyWriterContext=%p\n",
91 FileObject, FileSizes, PinAccess, CallBacks, LazyWriterContext);
92
93 /* Call old ROS cache init function */
96 PinAccess,
97 CallBacks,
98 LazyWriterContext);
99 if (!NT_SUCCESS(Status))
101}
102
103/*
104 * @implemented
105 */
107NTAPI
109 IN PVPB Vpb)
110{
111 PROS_VACB Vacb;
113 KIRQL oldIrql;
114 /* Assume no dirty data */
115 BOOLEAN Dirty = FALSE;
116
117 CCTRACE(CC_API_DEBUG, "Vpb=%p\n", Vpb);
118
120
121 /* Browse dirty VACBs */
123 {
124 Vacb = CONTAINING_RECORD(Entry, ROS_VACB, DirtyVacbListEntry);
125 /* Look for these associated with our volume */
126 if (Vacb->SharedCacheMap->FileObject->Vpb != Vpb)
127 {
128 continue;
129 }
130
131 /* From now on, we are associated with our VPB */
132
133 /* Temporary files are not counted as dirty */
135 {
136 continue;
137 }
138
139 /* A single dirty VACB is enough to have dirty data */
140 if (Vacb->Dirty)
141 {
142 Dirty = TRUE;
143 break;
144 }
145 }
146
148
149 return Dirty;
150}
151
152/*
153 * @unimplemented
154 */
156NTAPI
161 IN BOOLEAN UninitializeCacheMaps)
162{
163 PROS_SHARED_CACHE_MAP SharedCacheMap;
164 PPRIVATE_CACHE_MAP PrivateCacheMap;
165 LONGLONG StartOffset;
166 LONGLONG EndOffset;
167 LIST_ENTRY FreeList;
169 PLIST_ENTRY ListEntry;
170 PROS_VACB Vacb;
171 LONGLONG ViewEnd;
173
174 CCTRACE(CC_API_DEBUG, "SectionObjectPointer=%p\n FileOffset=%p Length=%lu UninitializeCacheMaps=%d",
175 SectionObjectPointer, FileOffset, Length, UninitializeCacheMaps);
176
177 /* Obtain the shared cache from the section */
178 SharedCacheMap = SectionObjectPointer->SharedCacheMap;
179 if (!SharedCacheMap)
180 {
181 Success = TRUE;
182 goto purgeMm;
183 }
184
185 if (UninitializeCacheMaps)
186 {
187 /*
188 * We have gotten the acknowledgement that
189 * the caller wants to unintialize the private
190 * cache maps so let's do this. Since we already
191 * have the shared cache map from above, iterate
192 * over that cache's private lists.
193 */
194 while (!IsListEmpty(&SharedCacheMap->PrivateList))
195 {
196 /*
197 * This list is not empty, grab the
198 * private cache map.
199 */
200 PrivateCacheMap = CONTAINING_RECORD(SharedCacheMap->PrivateList.Flink, PRIVATE_CACHE_MAP, PrivateLinks);
201
202 /* Unintialize the private cache now */
203 CcUninitializeCacheMap(PrivateCacheMap->FileObject, NULL, NULL);
204 }
205 }
206
207 StartOffset = FileOffset != NULL ? FileOffset->QuadPart : 0;
208 if (Length == 0 || FileOffset == NULL)
209 {
210 EndOffset = MAXLONGLONG;
211 }
212 else
213 {
214 EndOffset = StartOffset + Length;
215 ASSERT(EndOffset > StartOffset);
216 }
217
218 InitializeListHead(&FreeList);
219
220 /* Assume success */
221 Success = TRUE;
222
225 ListEntry = SharedCacheMap->CacheMapVacbListHead.Flink;
226 while (ListEntry != &SharedCacheMap->CacheMapVacbListHead)
227 {
228 ULONG Refs;
229
230 Vacb = CONTAINING_RECORD(ListEntry, ROS_VACB, CacheMapVacbListEntry);
231 ListEntry = ListEntry->Flink;
232
233 /* Skip VACBs outside the range, or only partially in range */
234 if (Vacb->FileOffset.QuadPart < StartOffset)
235 {
236 continue;
237 }
239 SharedCacheMap->SectionSize.QuadPart);
240 if (ViewEnd >= EndOffset)
241 {
242 break;
243 }
244
245 /* Still in use, it cannot be purged, fail
246 * Allow one ref: VACB is supposed to be always 1-referenced
247 */
248 Refs = CcRosVacbGetRefCount(Vacb);
249 if ((Refs > 1 && !Vacb->Dirty) ||
250 (Refs > 2 && Vacb->Dirty))
251 {
252 Success = FALSE;
253 break;
254 }
255
256 /* This VACB is in range, so unlink it and mark for free */
257 ASSERT(Refs == 1 || Vacb->Dirty);
260 if (Vacb->Dirty)
261 {
263 }
265 InsertHeadList(&FreeList, &Vacb->CacheMapVacbListEntry);
266 }
269
270 while (!IsListEmpty(&FreeList))
271 {
272 ULONG Refs;
273
274 Vacb = CONTAINING_RECORD(RemoveHeadList(&FreeList),
275 ROS_VACB,
276 CacheMapVacbListEntry);
278 Refs = CcRosVacbDecRefCount(Vacb);
279 ASSERT(Refs == 0);
280 }
281
282 /* Now make sure that Mm doesn't hold some pages here. */
283purgeMm:
284 if (Success)
286
287 return Success;
288}
289
290
291/*
292 * @implemented
293 */
298{
300 PROS_SHARED_CACHE_MAP SharedCacheMap;
301 LARGE_INTEGER OldSectionSize;
302
303 CCTRACE(CC_API_DEBUG, "FileObject=%p FileSizes=%p\n",
305
306 DPRINT("CcSetFileSizes(FileObject 0x%p, FileSizes 0x%p)\n",
308 DPRINT("AllocationSize %I64d, FileSize %I64d, ValidDataLength %I64d\n",
312
313 SharedCacheMap = FileObject->SectionObjectPointer->SharedCacheMap;
314
315 /*
316 * It is valid to call this function on file objects that weren't
317 * initialized for caching. In this case it's simple no-op.
318 */
319 if (SharedCacheMap == NULL)
320 return;
321
322 /* Update the relevant fields */
323 KeAcquireSpinLock(&SharedCacheMap->CacheMapLock, &OldIrql);
324 OldSectionSize = SharedCacheMap->SectionSize;
325 SharedCacheMap->SectionSize = FileSizes->AllocationSize;
326 SharedCacheMap->FileSize = FileSizes->FileSize;
327 SharedCacheMap->ValidDataLength = FileSizes->ValidDataLength;
328 KeReleaseSpinLock(&SharedCacheMap->CacheMapLock, OldIrql);
329
330 if (FileSizes->AllocationSize.QuadPart < OldSectionSize.QuadPart)
331 {
332 CcPurgeCacheSection(FileObject->SectionObjectPointer,
334 0,
335 FALSE);
336 }
337 else
338 {
339 /* Extend our section object */
340 MmExtendSection(SharedCacheMap->Section, &SharedCacheMap->SectionSize);
341 }
342}
343
344/*
345 * @unimplemented
346 */
347VOID
348NTAPI
352 IN PFLUSH_TO_LSN FlushToLsnRoutine)
353{
354 CCTRACE(CC_API_DEBUG, "FileObject=%p LogHandle=%p FlushToLsnRoutine=%p\n",
355 FileObject, LogHandle, FlushToLsnRoutine);
356
358}
359
360/*
361 * @unimplemented
362 */
364NTAPI
367 IN PLARGE_INTEGER TruncateSize OPTIONAL,
368 IN PCACHE_UNINITIALIZE_EVENT UninitializeCompleteEvent OPTIONAL)
369{
371 PROS_SHARED_CACHE_MAP SharedCacheMap;
373
374 CCTRACE(CC_API_DEBUG, "FileObject=%p TruncateSize=%p UninitializeCompleteEvent=%p\n",
375 FileObject, TruncateSize, UninitializeCompleteEvent);
376
377 if (TruncateSize != NULL &&
378 FileObject->SectionObjectPointer->SharedCacheMap != NULL)
379 {
380 SharedCacheMap = FileObject->SectionObjectPointer->SharedCacheMap;
381 KeAcquireSpinLock(&SharedCacheMap->CacheMapLock, &OldIrql);
382 if (SharedCacheMap->FileSize.QuadPart > TruncateSize->QuadPart)
383 {
384 SharedCacheMap->FileSize = *TruncateSize;
385 }
386 KeReleaseSpinLock(&SharedCacheMap->CacheMapLock, OldIrql);
387 CcPurgeCacheSection(FileObject->SectionObjectPointer,
388 TruncateSize,
389 0,
390 FALSE);
391 }
392
394 if (UninitializeCompleteEvent)
395 {
396 KeSetEvent(&UninitializeCompleteEvent->Event, IO_NO_INCREMENT, FALSE);
397 }
398 return NT_SUCCESS(Status);
399}
400
402NTAPI
406{
407 PROS_SHARED_CACHE_MAP SharedCacheMap;
408
409 SharedCacheMap = FileObject->SectionObjectPointer->SharedCacheMap;
410
411 if (!SharedCacheMap)
412 return FALSE;
413
414 FileSizes->AllocationSize = SharedCacheMap->SectionSize;
416 return TRUE;
417}
static CC_FILE_SIZES FileSizes
unsigned char BOOLEAN
LONG NTSTATUS
Definition: precomp.h:26
#define UNIMPLEMENTED
Definition: debug.h:115
_In_ PFCB _In_ LONGLONG FileOffset
Definition: cdprocs.h:160
#define NULL
Definition: types.h:112
#define TRUE
Definition: types.h:120
#define FALSE
Definition: types.h:117
#define NT_SUCCESS(StatCode)
Definition: apphelp.c:32
#define RemoveEntryList(Entry)
Definition: env_spec_w32.h:986
#define InsertHeadList(ListHead, Entry)
#define IsListEmpty(ListHead)
Definition: env_spec_w32.h:954
UCHAR KIRQL
Definition: env_spec_w32.h:591
#define KeReleaseSpinLock(sl, irql)
Definition: env_spec_w32.h:627
#define KeSetEvent(pEvt, foo, foo2)
Definition: env_spec_w32.h:476
#define KeAcquireSpinLock(sl, irql)
Definition: env_spec_w32.h:609
#define RemoveHeadList(ListHead)
Definition: env_spec_w32.h:964
#define InitializeListHead(ListHead)
Definition: env_spec_w32.h:944
@ Success
Definition: eventcreate.c:712
#define BooleanFlagOn(F, SF)
Definition: ext2fs.h:183
IN PVCB IN VBO IN ULONG OUT PBCB * Bcb
Definition: fatprocs.h:414
IN OUT PVCB IN PDEVICE_OBJECT IN PVPB Vpb
Definition: fatprocs.h:1675
_Must_inspect_result_ _In_ PDEVICE_OBJECT _In_ PSECTION_OBJECT_POINTERS SectionObjectPointer
Definition: fsrtlfuncs.h:1369
Status
Definition: gdiplustypes.h:25
GLsizei GLenum const GLvoid GLsizei GLenum GLbyte GLbyte GLbyte GLdouble GLdouble GLdouble GLfloat GLfloat GLfloat GLint GLint GLint GLshort GLshort GLshort GLubyte GLubyte GLubyte GLuint GLuint GLuint GLushort GLushort GLushort GLbyte GLbyte GLbyte GLbyte GLdouble GLdouble GLdouble GLdouble GLfloat GLfloat GLfloat GLfloat GLint GLint GLint GLint GLshort GLshort GLshort GLshort GLubyte GLubyte GLubyte GLubyte GLuint GLuint GLuint GLuint GLushort GLushort GLushort GLushort GLboolean const GLdouble const GLfloat const GLint const GLshort const GLbyte const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLdouble const GLfloat const GLfloat const GLint const GLint const GLshort const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort GLenum GLenum GLenum GLfloat GLenum GLint GLenum GLenum GLenum GLfloat GLenum GLenum GLint GLenum GLfloat GLenum GLint GLint GLushort GLenum GLenum GLfloat GLenum GLenum GLint GLfloat const GLubyte GLenum GLenum GLenum const GLfloat GLenum GLenum const GLint GLenum GLint GLint GLsizei GLsizei GLint GLenum GLenum const GLvoid GLenum GLenum const GLfloat GLenum GLenum const GLint GLenum GLenum const GLdouble GLenum GLenum const GLfloat GLenum GLenum const GLint GLsizei GLuint GLfloat GLuint GLbitfield GLfloat GLint GLuint GLboolean GLenum GLfloat GLenum GLbitfield GLenum GLfloat GLfloat GLint GLint const GLfloat GLenum GLfloat GLfloat GLint GLint GLfloat GLfloat GLint GLint const GLfloat GLint GLfloat GLfloat GLint GLfloat GLfloat GLint GLfloat GLfloat const GLdouble const GLfloat const GLdouble const GLfloat GLint i
Definition: glfuncs.h:248
VOID FASTCALL KeReleaseQueuedSpinLock(IN KSPIN_LOCK_QUEUE_NUMBER LockNumber, IN KIRQL OldIrql)
Definition: spinlock.c:154
KIRQL FASTCALL KeAcquireQueuedSpinLock(IN KSPIN_LOCK_QUEUE_NUMBER LockNumber)
Definition: spinlock.c:108
#define ASSERT(a)
Definition: mode.c:44
#define min(a, b)
Definition: monoChain.cc:55
_In_ UINT _Out_ PNDIS_HANDLE LogHandle
Definition: ndis.h:5382
#define MAXLONGLONG
_In_ ULONG _In_ ULONG _In_ ULONG Length
Definition: ntddpcm.h:102
VOID NTAPI CcSetFileSizes(IN PFILE_OBJECT FileObject, IN PCC_FILE_SIZES FileSizes)
Definition: fs.c:295
BOOLEAN NTAPI CcPurgeCacheSection(IN PSECTION_OBJECT_POINTERS SectionObjectPointer, IN PLARGE_INTEGER FileOffset OPTIONAL, IN ULONG Length, IN BOOLEAN UninitializeCacheMaps)
Definition: fs.c:157
PFILE_OBJECT NTAPI CcGetFileObjectFromBcb(IN PVOID Bcb)
Definition: fs.c:45
BOOLEAN NTAPI CcGetFileSizes(IN PFILE_OBJECT FileObject, IN PCC_FILE_SIZES FileSizes)
Definition: fs.c:403
VOID NTAPI CcSetLogHandleForFile(IN PFILE_OBJECT FileObject, IN PVOID LogHandle, IN PFLUSH_TO_LSN FlushToLsnRoutine)
Definition: fs.c:349
VOID NTAPI CcInitializeCacheMap(IN PFILE_OBJECT FileObject, IN PCC_FILE_SIZES FileSizes, IN BOOLEAN PinAccess, IN PCACHE_MANAGER_CALLBACKS CallBacks, IN PVOID LazyWriterContext)
Definition: fs.c:78
LARGE_INTEGER NTAPI CcGetDirtyPages(IN PVOID LogHandle, IN PDIRTY_PAGE_ROUTINE DirtyPageRoutine, IN PVOID Context1, IN PVOID Context2)
Definition: fs.c:24
BOOLEAN NTAPI CcIsThereDirtyData(IN PVPB Vpb)
Definition: fs.c:108
LARGE_INTEGER NTAPI CcGetLsnForFileObject(IN PFILE_OBJECT FileObject, OUT PLARGE_INTEGER OldestLsn OPTIONAL)
Definition: fs.c:60
BOOLEAN NTAPI CcUninitializeCacheMap(IN PFILE_OBJECT FileObject, IN PLARGE_INTEGER TruncateSize OPTIONAL, IN PCACHE_UNINITIALIZE_EVENT UninitializeCompleteEvent OPTIONAL)
Definition: fs.c:365
NTSTATUS CcRosInitializeFileCache(PFILE_OBJECT FileObject, PCC_FILE_SIZES FileSizes, BOOLEAN PinAccess, PCACHE_MANAGER_CALLBACKS CallBacks, PVOID LazyWriterContext)
Definition: view.c:1166
NTSTATUS CcRosReleaseFileCache(PFILE_OBJECT FileObject)
Definition: view.c:1110
LIST_ENTRY DirtyVacbListHead
Definition: view.c:41
VOID CcRosUnmarkDirtyVacb(PROS_VACB Vacb, BOOLEAN LockViews)
Definition: view.c:564
#define CcRosVacbGetRefCount(vacb)
Definition: cc.h:510
#define CC_API_DEBUG
Definition: cc.h:11
#define CCTRACE(x, fmt,...)
Definition: cc.h:36
FORCEINLINE ULONG CcRosVacbDecRefCount(PROS_VACB vacb)
Definition: cc.h:498
BOOLEAN NTAPI MmPurgeSegment(_In_ PSECTION_OBJECT_POINTERS SectionObjectPointer, _In_opt_ PLARGE_INTEGER Offset, _In_ ULONG Length)
Definition: section.c:4729
NTSTATUS NTAPI MmExtendSection(_In_ PVOID Section, _Inout_ PLARGE_INTEGER NewSize)
Definition: section.c:5174
#define ExRaiseStatus
Definition: ntoskrnl.h:108
#define KeAcquireSpinLockAtDpcLevel(SpinLock)
Definition: ke.h:125
#define KeReleaseSpinLockFromDpcLevel(SpinLock)
Definition: ke.h:135
#define DPRINT
Definition: sndvol32.h:71
PULONG MinorVersion OPTIONAL
Definition: CrossNt.h:68
base of all file and directory entries
Definition: entries.h:83
LARGE_INTEGER FileSize
Definition: cctypes.h:16
LARGE_INTEGER ValidDataLength
Definition: cctypes.h:17
LARGE_INTEGER AllocationSize
Definition: cctypes.h:15
Definition: cdstruc.h:902
PROS_VACB Vacb
Definition: cc.h:235
Definition: typedefs.h:120
struct _LIST_ENTRY * Flink
Definition: typedefs.h:121
PFILE_OBJECT FileObject
Definition: cctypes.h:76
LIST_ENTRY CacheMapVacbListHead
Definition: cc.h:193
LARGE_INTEGER FileSize
Definition: cc.h:175
KSPIN_LOCK CacheMapLock
Definition: cc.h:195
LARGE_INTEGER ValidDataLength
Definition: cc.h:178
LIST_ENTRY PrivateList
Definition: cc.h:187
LARGE_INTEGER SectionSize
Definition: cc.h:177
PFILE_OBJECT FileObject
Definition: cc.h:179
Definition: cc.h:207
LIST_ENTRY CacheMapVacbListEntry
Definition: cc.h:216
BOOLEAN Dirty
Definition: cc.h:211
PROS_SHARED_CACHE_MAP SharedCacheMap
Definition: cc.h:226
LARGE_INTEGER FileOffset
Definition: cc.h:222
LIST_ENTRY VacbLruListEntry
Definition: cc.h:220
Definition: iotypes.h:189
_In_ PNET_PNP_EVENT _In_ PTDI_PNP_CONTEXT Context1
Definition: tdikrnl.h:1095
_In_ PNET_PNP_EVENT _In_ PTDI_PNP_CONTEXT _In_ PTDI_PNP_CONTEXT Context2
Definition: tdikrnl.h:1096
int64_t LONGLONG
Definition: typedefs.h:68
#define NTAPI
Definition: typedefs.h:36
#define IN
Definition: typedefs.h:39
#define CONTAINING_RECORD(address, type, field)
Definition: typedefs.h:260
uint32_t ULONG
Definition: typedefs.h:59
#define OUT
Definition: typedefs.h:40
LONGLONG QuadPart
Definition: typedefs.h:114
_In_ WDFREQUEST _In_ WDFFILEOBJECT FileObject
Definition: wdfdevice.h:550
#define VACB_MAPPING_GRANULARITY
VOID(NTAPI * PDIRTY_PAGE_ROUTINE)(_In_ PFILE_OBJECT FileObject, _In_ PLARGE_INTEGER FileOffset, _In_ ULONG Length, _In_ PLARGE_INTEGER OldestLsn, _In_ PLARGE_INTEGER NewestLsn, _In_ PVOID Context1, _In_ PVOID Context2)
Definition: cctypes.h:51
VOID(NTAPI * PFLUSH_TO_LSN)(_In_ PVOID LogHandle, _In_ LARGE_INTEGER Lsn)
Definition: cctypes.h:61
#define IO_NO_INCREMENT
Definition: iotypes.h:598
#define FO_TEMPORARY_FILE
Definition: iotypes.h:1791
* PFILE_OBJECT
Definition: iotypes.h:1998
_Requires_lock_held_ Interrupt _Releases_lock_ Interrupt _In_ _IRQL_restores_ KIRQL OldIrql
Definition: kefuncs.h:792
@ LockQueueMasterLock
Definition: ketypes.h:651