ReactOS  0.4.15-dev-2720-g5ee0925
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  */
23 NTAPI
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  */
44 NTAPI
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  */
59 NTAPI
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  */
76 VOID
77 NTAPI
81  IN BOOLEAN PinAccess,
82  IN PCACHE_MANAGER_CALLBACKS CallBacks,
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 */
95  FileSizes,
96  PinAccess,
97  CallBacks,
98  LazyWriterContext);
99  if (!NT_SUCCESS(Status))
101 }
102 
103 /*
104  * @implemented
105  */
106 BOOLEAN
107 NTAPI
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  */
155 BOOLEAN
156 NTAPI
160  IN ULONG Length,
161  IN BOOLEAN UninitializeCacheMaps)
162 {
163  PROS_SHARED_CACHE_MAP SharedCacheMap;
164  LONGLONG StartOffset;
165  LONGLONG EndOffset;
167  KIRQL OldIrql;
168  PLIST_ENTRY ListEntry;
169  PROS_VACB Vacb;
170  LONGLONG ViewEnd;
172 
173  CCTRACE(CC_API_DEBUG, "SectionObjectPointer=%p\n FileOffset=%p Length=%lu UninitializeCacheMaps=%d",
174  SectionObjectPointer, FileOffset, Length, UninitializeCacheMaps);
175 
176  if (UninitializeCacheMaps)
177  {
178  DPRINT1("FIXME: CcPurgeCacheSection not uninitializing private cache maps\n");
179  }
180 
181  SharedCacheMap = SectionObjectPointer->SharedCacheMap;
182  if (!SharedCacheMap)
183  {
184  Success = TRUE;
185  goto purgeMm;
186  }
187 
188  StartOffset = FileOffset != NULL ? FileOffset->QuadPart : 0;
189  if (Length == 0 || FileOffset == NULL)
190  {
191  EndOffset = MAXLONGLONG;
192  }
193  else
194  {
195  EndOffset = StartOffset + Length;
196  ASSERT(EndOffset > StartOffset);
197  }
198 
200 
201  /* Assume success */
202  Success = TRUE;
203 
205  KeAcquireSpinLockAtDpcLevel(&SharedCacheMap->CacheMapLock);
206  ListEntry = SharedCacheMap->CacheMapVacbListHead.Flink;
207  while (ListEntry != &SharedCacheMap->CacheMapVacbListHead)
208  {
209  ULONG Refs;
210 
211  Vacb = CONTAINING_RECORD(ListEntry, ROS_VACB, CacheMapVacbListEntry);
212  ListEntry = ListEntry->Flink;
213 
214  /* Skip VACBs outside the range, or only partially in range */
215  if (Vacb->FileOffset.QuadPart < StartOffset)
216  {
217  continue;
218  }
220  SharedCacheMap->SectionSize.QuadPart);
221  if (ViewEnd >= EndOffset)
222  {
223  break;
224  }
225 
226  /* Still in use, it cannot be purged, fail
227  * Allow one ref: VACB is supposed to be always 1-referenced
228  */
229  Refs = CcRosVacbGetRefCount(Vacb);
230  if ((Refs > 1 && !Vacb->Dirty) ||
231  (Refs > 2 && Vacb->Dirty))
232  {
233  Success = FALSE;
234  break;
235  }
236 
237  /* This VACB is in range, so unlink it and mark for free */
238  ASSERT(Refs == 1 || Vacb->Dirty);
241  if (Vacb->Dirty)
242  {
244  }
247  }
250 
251  while (!IsListEmpty(&FreeList))
252  {
253  ULONG Refs;
254 
256  ROS_VACB,
257  CacheMapVacbListEntry);
259  Refs = CcRosVacbDecRefCount(Vacb);
260  ASSERT(Refs == 0);
261  }
262 
263  /* Now make sure that Mm doesn't hold some pages here. */
264 purgeMm:
265  if (Success)
267 
268  return Success;
269 }
270 
271 
272 /*
273  * @implemented
274  */
275 VOID NTAPI
279 {
280  KIRQL OldIrql;
281  PROS_SHARED_CACHE_MAP SharedCacheMap;
282  LARGE_INTEGER OldSectionSize;
283 
284  CCTRACE(CC_API_DEBUG, "FileObject=%p FileSizes=%p\n",
286 
287  DPRINT("CcSetFileSizes(FileObject 0x%p, FileSizes 0x%p)\n",
289  DPRINT("AllocationSize %I64d, FileSize %I64d, ValidDataLength %I64d\n",
293 
294  SharedCacheMap = FileObject->SectionObjectPointer->SharedCacheMap;
295 
296  /*
297  * It is valid to call this function on file objects that weren't
298  * initialized for caching. In this case it's simple no-op.
299  */
300  if (SharedCacheMap == NULL)
301  return;
302 
303  /* Update the relevant fields */
304  KeAcquireSpinLock(&SharedCacheMap->CacheMapLock, &OldIrql);
305  OldSectionSize = SharedCacheMap->SectionSize;
306  SharedCacheMap->SectionSize = FileSizes->AllocationSize;
307  SharedCacheMap->FileSize = FileSizes->FileSize;
308  SharedCacheMap->ValidDataLength = FileSizes->ValidDataLength;
309  KeReleaseSpinLock(&SharedCacheMap->CacheMapLock, OldIrql);
310 
311  if (FileSizes->AllocationSize.QuadPart < OldSectionSize.QuadPart)
312  {
313  CcPurgeCacheSection(FileObject->SectionObjectPointer,
315  0,
316  FALSE);
317  }
318  else
319  {
320  /* Extend our section object */
321  MmExtendSection(SharedCacheMap->Section, &SharedCacheMap->SectionSize);
322  }
323 }
324 
325 /*
326  * @unimplemented
327  */
328 VOID
329 NTAPI
333  IN PFLUSH_TO_LSN FlushToLsnRoutine)
334 {
335  CCTRACE(CC_API_DEBUG, "FileObject=%p LogHandle=%p FlushToLsnRoutine=%p\n",
336  FileObject, LogHandle, FlushToLsnRoutine);
337 
339 }
340 
341 /*
342  * @unimplemented
343  */
344 BOOLEAN
345 NTAPI
348  IN PLARGE_INTEGER TruncateSize OPTIONAL,
349  IN PCACHE_UNINITIALIZE_EVENT UninitializeCompleteEvent OPTIONAL)
350 {
352  PROS_SHARED_CACHE_MAP SharedCacheMap;
353  KIRQL OldIrql;
354 
355  CCTRACE(CC_API_DEBUG, "FileObject=%p TruncateSize=%p UninitializeCompleteEvent=%p\n",
356  FileObject, TruncateSize, UninitializeCompleteEvent);
357 
358  if (TruncateSize != NULL &&
359  FileObject->SectionObjectPointer->SharedCacheMap != NULL)
360  {
361  SharedCacheMap = FileObject->SectionObjectPointer->SharedCacheMap;
362  KeAcquireSpinLock(&SharedCacheMap->CacheMapLock, &OldIrql);
363  if (SharedCacheMap->FileSize.QuadPart > TruncateSize->QuadPart)
364  {
365  SharedCacheMap->FileSize = *TruncateSize;
366  }
367  KeReleaseSpinLock(&SharedCacheMap->CacheMapLock, OldIrql);
368  CcPurgeCacheSection(FileObject->SectionObjectPointer,
369  TruncateSize,
370  0,
371  FALSE);
372  }
373 
375  if (UninitializeCompleteEvent)
376  {
377  KeSetEvent(&UninitializeCompleteEvent->Event, IO_NO_INCREMENT, FALSE);
378  }
379  return NT_SUCCESS(Status);
380 }
381 
382 BOOLEAN
383 NTAPI
387 {
388  PROS_SHARED_CACHE_MAP SharedCacheMap;
389 
390  SharedCacheMap = FileObject->SectionObjectPointer->SharedCacheMap;
391 
392  if (!SharedCacheMap)
393  return FALSE;
394 
395  FileSizes->AllocationSize = SharedCacheMap->SectionSize;
396  FileSizes->FileSize = FileSizes->ValidDataLength = SharedCacheMap->FileSize;
397  return TRUE;
398 }
NTSTATUS NTAPI MmExtendSection(_In_ PVOID Section, _Inout_ PLARGE_INTEGER NewSize)
Definition: section.c:4957
VOID(NTAPI * PFLUSH_TO_LSN)(_In_ PVOID LogHandle, _In_ LARGE_INTEGER Lsn)
Definition: cctypes.h:61
#define IN
Definition: typedefs.h:39
Definition: cc.h:206
_In_ ULONG _In_ ULONG _In_ ULONG Length
Definition: ntddpcm.h:101
IN PVCB IN VBO IN ULONG OUT PBCB * Bcb
Definition: fatprocs.h:411
struct _Entry Entry
Definition: kefuncs.h:627
LARGE_INTEGER SectionSize
Definition: cc.h:177
#define TRUE
Definition: types.h:120
Definition: cdstruc.h:902
#define BooleanFlagOn(F, SF)
Definition: ext2fs.h:183
FORCEINLINE VOID InsertHeadList(_Inout_ PLIST_ENTRY ListHead, _Inout_ __drv_aliasesMem PLIST_ENTRY Entry)
Definition: rtlfuncs.h:201
BOOLEAN NTAPI CcUninitializeCacheMap(IN PFILE_OBJECT FileObject, IN PLARGE_INTEGER TruncateSize OPTIONAL, IN PCACHE_UNINITIALIZE_EVENT UninitializeCompleteEvent OPTIONAL)
Definition: fs.c:346
VOID NTAPI KeAcquireSpinLockAtDpcLevel(IN PKSPIN_LOCK SpinLock)
Definition: spinlock.c:192
LONG NTSTATUS
Definition: precomp.h:26
#define ExRaiseStatus
Definition: ntoskrnl.h:104
BOOLEAN NTAPI MmPurgeSegment(_In_ PSECTION_OBJECT_POINTERS SectionObjectPointer, _In_opt_ PLARGE_INTEGER Offset, _In_ ULONG Length)
Definition: section.c:4550
#define MAXLONGLONG
static CC_FILE_SIZES FileSizes
LONG NTAPI KeSetEvent(IN PKEVENT Event, IN KPRIORITY Increment, IN BOOLEAN Wait)
Definition: eventobj.c:159
PFILE_OBJECT NTAPI CcGetFileObjectFromBcb(IN PVOID Bcb)
Definition: fs.c:45
_Must_inspect_result_ FORCEINLINE BOOLEAN IsListEmpty(_In_ const LIST_ENTRY *ListHead)
Definition: rtlfuncs.h:57
_Must_inspect_result_ _In_ PDEVICE_OBJECT _In_ PSECTION_OBJECT_POINTERS SectionObjectPointer
Definition: fsrtlfuncs.h:1368
LARGE_INTEGER FileSize
Definition: cctypes.h:16
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
FORCEINLINE BOOLEAN RemoveEntryList(_In_ PLIST_ENTRY Entry)
Definition: rtlfuncs.h:105
UCHAR KIRQL
Definition: env_spec_w32.h:591
NTSTATUS(* NTAPI)(IN PFILE_FULL_EA_INFORMATION EaBuffer, IN ULONG EaLength, OUT PULONG ErrorOffset)
Definition: IoEaTest.cpp:117
#define FALSE
Definition: types.h:117
LIST_ENTRY CacheMapVacbListEntry
Definition: cc.h:216
VOID CcRosUnmarkDirtyVacb(PROS_VACB Vacb, BOOLEAN LockViews)
Definition: view.c:564
unsigned char BOOLEAN
FORCEINLINE PLIST_ENTRY RemoveHeadList(_Inout_ PLIST_ENTRY ListHead)
Definition: rtlfuncs.h:128
VOID NTAPI CcSetLogHandleForFile(IN PFILE_OBJECT FileObject, IN PVOID LogHandle, IN PFLUSH_TO_LSN FlushToLsnRoutine)
Definition: fs.c:330
#define CcRosVacbGetRefCount(vacb)
Definition: cc.h:507
PFLT_MESSAGE_WAITER_QUEUE CONTAINING_RECORD(Csq, DEVICE_EXTENSION, IrpQueue)) -> WaiterQ.mLock) _IRQL_raises_(DISPATCH_LEVEL) VOID NTAPI FltpAcquireMessageWaiterLock(_In_ PIO_CSQ Csq, _Out_ PKIRQL Irql)
Definition: Messaging.c:560
_In_ WDFREQUEST _In_ WDFFILEOBJECT FileObject
Definition: wdfdevice.h:547
PROS_SHARED_CACHE_MAP SharedCacheMap
Definition: cc.h:226
LIST_ENTRY CacheMapVacbListHead
Definition: cc.h:193
BOOLEAN NTAPI CcIsThereDirtyData(IN PVPB Vpb)
Definition: fs.c:108
Status
Definition: gdiplustypes.h:24
int64_t LONGLONG
Definition: typedefs.h:68
struct _LIST_ENTRY * Flink
Definition: typedefs.h:121
#define CC_API_DEBUG
Definition: cc.h:11
#define ASSERT(a)
Definition: mode.c:44
#define NT_SUCCESS(StatCode)
Definition: apphelp.c:32
VOID NTAPI KeReleaseSpinLockFromDpcLevel(IN PKSPIN_LOCK SpinLock)
Definition: spinlock.c:215
#define KeAcquireSpinLock(sl, irql)
Definition: env_spec_w32.h:609
VOID FASTCALL KeReleaseQueuedSpinLock(IN KSPIN_LOCK_QUEUE_NUMBER LockNumber, IN KIRQL OldIrql)
Definition: spinlock.c:154
#define CCTRACE(x, fmt,...)
Definition: cc.h:36
* PFILE_OBJECT
Definition: iotypes.h:1998
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
FORCEINLINE ULONG CcRosVacbDecRefCount(PROS_VACB vacb)
Definition: cc.h:495
BOOLEAN Dirty
Definition: cc.h:211
NTSTATUS CcRosReleaseFileCache(PFILE_OBJECT FileObject)
Definition: view.c:1110
MmuFreePage * FreeList
Definition: mmuobject.c:60
_Requires_lock_held_ Interrupt _Releases_lock_ Interrupt _In_ _IRQL_restores_ KIRQL OldIrql
Definition: kefuncs.h:790
LARGE_INTEGER NTAPI CcGetDirtyPages(IN PVOID LogHandle, IN PDIRTY_PAGE_ROUTINE DirtyPageRoutine, IN PVOID Context1, IN PVOID Context2)
Definition: fs.c:24
LARGE_INTEGER ValidDataLength
Definition: cctypes.h:17
Definition: typedefs.h:119
KIRQL FASTCALL KeAcquireQueuedSpinLock(IN KSPIN_LOCK_QUEUE_NUMBER LockNumber)
Definition: spinlock.c:108
BOOLEAN NTAPI CcPurgeCacheSection(IN PSECTION_OBJECT_POINTERS SectionObjectPointer, IN PLARGE_INTEGER FileOffset OPTIONAL, IN ULONG Length, IN BOOLEAN UninitializeCacheMaps)
Definition: fs.c:157
_In_ PNET_PNP_EVENT _In_ PTDI_PNP_CONTEXT _In_ PTDI_PNP_CONTEXT Context2
Definition: tdikrnl.h:1094
LARGE_INTEGER AllocationSize
Definition: cctypes.h:15
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
_In_ PFCB _In_ LONGLONG FileOffset
Definition: cdprocs.h:159
#define FO_TEMPORARY_FILE
Definition: iotypes.h:1791
#define InitializeListHead(ListHead)
Definition: env_spec_w32.h:944
_In_ PNET_PNP_EVENT _In_ PTDI_PNP_CONTEXT Context1
Definition: tdikrnl.h:1094
BOOLEAN NTAPI CcGetFileSizes(IN PFILE_OBJECT FileObject, IN PCC_FILE_SIZES FileSizes)
Definition: fs.c:384
#define min(a, b)
Definition: monoChain.cc:55
#define NULL
Definition: types.h:112
#define KeReleaseSpinLock(sl, irql)
Definition: env_spec_w32.h:627
NTSTATUS CcRosInitializeFileCache(PFILE_OBJECT FileObject, PCC_FILE_SIZES FileSizes, BOOLEAN PinAccess, PCACHE_MANAGER_CALLBACKS CallBacks, PVOID LazyWriterContext)
Definition: view.c:1166
#define DPRINT1
Definition: precomp.h:8
LARGE_INTEGER FileOffset
Definition: cc.h:222
#define VACB_MAPPING_GRANULARITY
Definition: iotypes.h:189
#define OUT
Definition: typedefs.h:40
LARGE_INTEGER FileSize
Definition: cc.h:175
unsigned int ULONG
Definition: retypes.h:1
#define IO_NO_INCREMENT
Definition: iotypes.h:598
#define UNIMPLEMENTED
Definition: debug.h:115
LARGE_INTEGER ValidDataLength
Definition: cc.h:178
LARGE_INTEGER NTAPI CcGetLsnForFileObject(IN PFILE_OBJECT FileObject, OUT PLARGE_INTEGER OldestLsn OPTIONAL)
Definition: fs.c:60
KSPIN_LOCK CacheMapLock
Definition: cc.h:195
PFILE_OBJECT FileObject
Definition: cc.h:179
_In_ UINT _Out_ PNDIS_HANDLE LogHandle
Definition: ndis.h:5381
#define DPRINT
Definition: sndvol32.h:71
LIST_ENTRY DirtyVacbListHead
Definition: view.c:41
base of all file and directory entries
Definition: entries.h:82
IN OUT PVCB IN PDEVICE_OBJECT IN PVPB Vpb
Definition: fatprocs.h:1673
VOID NTAPI CcSetFileSizes(IN PFILE_OBJECT FileObject, IN PCC_FILE_SIZES FileSizes)
Definition: fs.c:276
LIST_ENTRY VacbLruListEntry
Definition: cc.h:220
LONGLONG QuadPart
Definition: typedefs.h:114
PROS_VACB Vacb
Definition: cc.h:235
PULONG MinorVersion OPTIONAL
Definition: CrossNt.h:68