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
7 * PROGRAMMERS: Alex Ionescu
10 /* INCLUDES ******************************************************************/
16 #ifndef VACB_MAPPING_GRANULARITY
17 #define VACB_MAPPING_GRANULARITY (256 * 1024)
20 /* GLOBALS *****************************************************************/
22 extern KGUARDED_MUTEX ViewLock
;
23 extern ULONG DirtyPageCount
;
25 NTSTATUS
CcRosInternalFreeCacheSegment(PCACHE_SEGMENT CacheSeg
);
27 /* FUNCTIONS *****************************************************************/
36 IN PDIRTY_PAGE_ROUTINE DirtyPageRoutine
,
52 CcGetFileObjectFromBcb (
56 PINTERNAL_BCB iBcb
= (PINTERNAL_BCB
)Bcb
;
57 return iBcb
->CacheSegment
->Bcb
->FileObject
;
65 CcGetLsnForFileObject (
66 IN PFILE_OBJECT FileObject
,
67 OUT PLARGE_INTEGER OldestLsn OPTIONAL
81 CcInitializeCacheMap (
82 IN PFILE_OBJECT FileObject
,
83 IN PCC_FILE_SIZES FileSizes
,
85 IN PCACHE_MANAGER_CALLBACKS CallBacks
,
86 IN PVOID LazyWriterContext
92 /* Call old ROS cache init function */
93 CcRosInitializeFileCache(FileObject
,
94 /*PAGE_SIZE*/ VACB_MAPPING_GRANULARITY
, CallBacks
,
116 CcPurgeCacheSection (
117 IN PSECTION_OBJECT_POINTERS SectionObjectPointer
,
118 IN PLARGE_INTEGER FileOffset OPTIONAL
,
120 IN BOOLEAN UninitializeCacheMaps
132 CcSetFileSizes (IN PFILE_OBJECT FileObject
,
133 IN PCC_FILE_SIZES FileSizes
)
137 PLIST_ENTRY current_entry
;
138 PCACHE_SEGMENT current
;
139 LIST_ENTRY FreeListHead
;
142 DPRINT("CcSetFileSizes(FileObject 0x%p, FileSizes 0x%p)\n",
143 FileObject
, FileSizes
);
144 DPRINT("AllocationSize %d, FileSize %d, ValidDataLength %d\n",
145 (ULONG
)FileSizes
->AllocationSize
.QuadPart
,
146 (ULONG
)FileSizes
->FileSize
.QuadPart
,
147 (ULONG
)FileSizes
->ValidDataLength
.QuadPart
);
149 Bcb
= FileObject
->SectionObjectPointer
->SharedCacheMap
;
152 * It is valid to call this function on file objects that weren't
153 * initialized for caching. In this case it's simple no-op.
158 if (FileSizes
->AllocationSize
.QuadPart
< Bcb
->AllocationSize
.QuadPart
)
160 InitializeListHead(&FreeListHead
);
161 KeAcquireGuardedMutex(&ViewLock
);
162 KeAcquireSpinLock(&Bcb
->BcbLock
, &oldirql
);
164 current_entry
= Bcb
->BcbSegmentListHead
.Flink
;
165 while (current_entry
!= &Bcb
->BcbSegmentListHead
)
167 current
= CONTAINING_RECORD(current_entry
, CACHE_SEGMENT
, BcbSegmentListEntry
);
168 current_entry
= current_entry
->Flink
;
169 if (current
->FileOffset
> FileSizes
->AllocationSize
.QuadPart
||
170 (current
->FileOffset
== 0 && FileSizes
->AllocationSize
.QuadPart
== 0))
172 if (current
->ReferenceCount
== 0 || (current
->ReferenceCount
== 1 && current
->Dirty
))
174 RemoveEntryList(¤t
->BcbSegmentListEntry
);
175 RemoveEntryList(¤t
->CacheSegmentListEntry
);
176 RemoveEntryList(¤t
->CacheSegmentLRUListEntry
);
179 RemoveEntryList(¤t
->DirtySegmentListEntry
);
180 DirtyPageCount
-= Bcb
->CacheSegmentSize
/ PAGE_SIZE
;
182 InsertHeadList(&FreeListHead
, ¤t
->BcbSegmentListEntry
);
186 DPRINT1("Anyone has referenced a cache segment behind the new size.\n");
187 KeBugCheck(CACHE_MANAGER
);
192 Bcb
->AllocationSize
= FileSizes
->AllocationSize
;
193 Bcb
->FileSize
= FileSizes
->FileSize
;
194 KeReleaseSpinLock(&Bcb
->BcbLock
, oldirql
);
195 KeReleaseGuardedMutex(&ViewLock
);
197 current_entry
= FreeListHead
.Flink
;
198 while(current_entry
!= &FreeListHead
)
200 current
= CONTAINING_RECORD(current_entry
, CACHE_SEGMENT
, BcbSegmentListEntry
);
201 current_entry
= current_entry
->Flink
;
202 Status
= CcRosInternalFreeCacheSegment(current
);
203 if (!NT_SUCCESS(Status
))
205 DPRINT1("CcRosInternalFreeCacheSegment failed, status = %x\n", Status
);
206 KeBugCheck(CACHE_MANAGER
);
212 KeAcquireSpinLock(&Bcb
->BcbLock
, &oldirql
);
213 Bcb
->AllocationSize
= FileSizes
->AllocationSize
;
214 Bcb
->FileSize
= FileSizes
->FileSize
;
215 KeReleaseSpinLock(&Bcb
->BcbLock
, oldirql
);
224 CcSetLogHandleForFile (
225 IN PFILE_OBJECT FileObject
,
227 IN PFLUSH_TO_LSN FlushToLsnRoutine
238 CcUninitializeCacheMap (
239 IN PFILE_OBJECT FileObject
,
240 IN PLARGE_INTEGER TruncateSize OPTIONAL
,
241 IN PCACHE_UNINITIALIZE_EVENT UninitializeCompleteEvent OPTIONAL
248 return NT_SUCCESS(CcRosReleaseFileCache(FileObject
));
255 (IN PFILE_OBJECT FileObject
,
256 IN PCC_FILE_SIZES FileSizes
)
260 Bcb
= FileObject
->SectionObjectPointer
->SharedCacheMap
;
265 FileSizes
->AllocationSize
= Bcb
->AllocationSize
;
266 FileSizes
->FileSize
= FileSizes
->ValidDataLength
= Bcb
->FileSize
;