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
,
51 CcGetFileObjectFromBcb (
54 PINTERNAL_BCB iBcb
= (PINTERNAL_BCB
)Bcb
;
55 return iBcb
->CacheSegment
->Bcb
->FileObject
;
63 CcGetLsnForFileObject (
64 IN PFILE_OBJECT FileObject
,
65 OUT PLARGE_INTEGER OldestLsn OPTIONAL
)
78 CcInitializeCacheMap (
79 IN PFILE_OBJECT FileObject
,
80 IN PCC_FILE_SIZES FileSizes
,
82 IN PCACHE_MANAGER_CALLBACKS CallBacks
,
83 IN PVOID LazyWriterContext
)
88 /* Call old ROS cache init function */
89 CcRosInitializeFileCache(FileObject
,
90 /*PAGE_SIZE*/ VACB_MAPPING_GRANULARITY
, CallBacks
,
111 CcPurgeCacheSection (
112 IN PSECTION_OBJECT_POINTERS SectionObjectPointer
,
113 IN PLARGE_INTEGER FileOffset OPTIONAL
,
115 IN BOOLEAN UninitializeCacheMaps
)
127 IN PFILE_OBJECT FileObject
,
128 IN PCC_FILE_SIZES FileSizes
)
132 PLIST_ENTRY current_entry
;
133 PCACHE_SEGMENT current
;
134 LIST_ENTRY FreeListHead
;
137 DPRINT("CcSetFileSizes(FileObject 0x%p, FileSizes 0x%p)\n",
138 FileObject
, FileSizes
);
139 DPRINT("AllocationSize %I64d, FileSize %I64d, ValidDataLength %I64d\n",
140 FileSizes
->AllocationSize
.QuadPart
,
141 FileSizes
->FileSize
.QuadPart
,
142 FileSizes
->ValidDataLength
.QuadPart
);
144 Bcb
= FileObject
->SectionObjectPointer
->SharedCacheMap
;
147 * It is valid to call this function on file objects that weren't
148 * initialized for caching. In this case it's simple no-op.
153 if (FileSizes
->AllocationSize
.QuadPart
< Bcb
->AllocationSize
.QuadPart
)
155 InitializeListHead(&FreeListHead
);
156 KeAcquireGuardedMutex(&ViewLock
);
157 KeAcquireSpinLock(&Bcb
->BcbLock
, &oldirql
);
159 current_entry
= Bcb
->BcbSegmentListHead
.Flink
;
160 while (current_entry
!= &Bcb
->BcbSegmentListHead
)
162 current
= CONTAINING_RECORD(current_entry
, CACHE_SEGMENT
, BcbSegmentListEntry
);
163 current_entry
= current_entry
->Flink
;
164 if (current
->FileOffset
> FileSizes
->AllocationSize
.QuadPart
||
165 (current
->FileOffset
== 0 && FileSizes
->AllocationSize
.QuadPart
== 0))
167 if ((current
->ReferenceCount
== 0) || ((current
->ReferenceCount
== 1) && current
->Dirty
))
169 RemoveEntryList(¤t
->BcbSegmentListEntry
);
170 RemoveEntryList(¤t
->CacheSegmentListEntry
);
171 RemoveEntryList(¤t
->CacheSegmentLRUListEntry
);
174 RemoveEntryList(¤t
->DirtySegmentListEntry
);
175 DirtyPageCount
-= Bcb
->CacheSegmentSize
/ PAGE_SIZE
;
177 InsertHeadList(&FreeListHead
, ¤t
->BcbSegmentListEntry
);
181 DPRINT1("Anyone has referenced a cache segment behind the new size.\n");
182 KeBugCheck(CACHE_MANAGER
);
187 Bcb
->AllocationSize
= FileSizes
->AllocationSize
;
188 Bcb
->FileSize
= FileSizes
->FileSize
;
189 KeReleaseSpinLock(&Bcb
->BcbLock
, oldirql
);
190 KeReleaseGuardedMutex(&ViewLock
);
192 current_entry
= FreeListHead
.Flink
;
193 while(current_entry
!= &FreeListHead
)
195 current
= CONTAINING_RECORD(current_entry
, CACHE_SEGMENT
, BcbSegmentListEntry
);
196 current_entry
= current_entry
->Flink
;
197 Status
= CcRosInternalFreeCacheSegment(current
);
198 if (!NT_SUCCESS(Status
))
200 DPRINT1("CcRosInternalFreeCacheSegment failed, status = %x\n", Status
);
201 KeBugCheck(CACHE_MANAGER
);
207 KeAcquireSpinLock(&Bcb
->BcbLock
, &oldirql
);
208 Bcb
->AllocationSize
= FileSizes
->AllocationSize
;
209 Bcb
->FileSize
= FileSizes
->FileSize
;
210 KeReleaseSpinLock(&Bcb
->BcbLock
, oldirql
);
219 CcSetLogHandleForFile (
220 IN PFILE_OBJECT FileObject
,
222 IN PFLUSH_TO_LSN FlushToLsnRoutine
)
232 CcUninitializeCacheMap (
233 IN PFILE_OBJECT FileObject
,
234 IN PLARGE_INTEGER TruncateSize OPTIONAL
,
235 IN PCACHE_UNINITIALIZE_EVENT UninitializeCompleteEvent OPTIONAL
)
241 return NT_SUCCESS(CcRosReleaseFileCache(FileObject
));
248 IN PFILE_OBJECT FileObject
,
249 IN PCC_FILE_SIZES FileSizes
)
253 Bcb
= FileObject
->SectionObjectPointer
->SharedCacheMap
;
258 FileSizes
->AllocationSize
= Bcb
->AllocationSize
;
259 FileSizes
->FileSize
= FileSizes
->ValidDataLength
= Bcb
->FileSize
;