3 * COPYRIGHT: See COPYING in the top level directory
4 * PROJECT: ReactOS kernel
5 * FILE: ntoskrnl/cc/fs.c
6 * PURPOSE: Implements cache managers functions useful for File Systems
8 * PROGRAMMERS: Alex Ionescu
11 /* INCLUDES ******************************************************************/
15 #include <internal/debug.h>
17 #ifndef VACB_MAPPING_GRANULARITY
18 #define VACB_MAPPING_GRANULARITY (256 * 1024)
21 /* GLOBALS *****************************************************************/
23 extern FAST_MUTEX ViewLock
;
24 extern ULONG DirtyPageCount
;
26 NTSTATUS
CcRosInternalFreeCacheSegment(PCACHE_SEGMENT CacheSeg
);
28 /* FUNCTIONS *****************************************************************/
37 IN PDIRTY_PAGE_ROUTINE DirtyPageRoutine
,
54 CcGetFileObjectFromBcb (
58 PINTERNAL_BCB iBcb
= (PINTERNAL_BCB
)Bcb
;
59 return iBcb
->CacheSegment
->Bcb
->FileObject
;
67 CcGetLsnForFileObject (
68 IN PFILE_OBJECT FileObject
,
69 OUT PLARGE_INTEGER OldestLsn OPTIONAL
84 CcInitializeCacheMap (
85 IN PFILE_OBJECT FileObject
,
86 IN PCC_FILE_SIZES FileSizes
,
88 IN PCACHE_MANAGER_CALLBACKS CallBacks
,
89 IN PVOID LazyWriterContext
92 CcRosInitializeFileCache(FileObject
, VACB_MAPPING_GRANULARITY
);
113 CcPurgeCacheSection (
114 IN PSECTION_OBJECT_POINTERS SectionObjectPointer
,
115 IN PLARGE_INTEGER FileOffset OPTIONAL
,
117 IN BOOLEAN UninitializeCacheMaps
129 CcSetFileSizes (IN PFILE_OBJECT FileObject
,
130 IN PCC_FILE_SIZES FileSizes
)
134 PLIST_ENTRY current_entry
;
135 PCACHE_SEGMENT current
;
136 LIST_ENTRY FreeListHead
;
139 DPRINT("CcSetFileSizes(FileObject %x, FileSizes %x)\n",
140 FileObject
, FileSizes
);
141 DPRINT("AllocationSize %d, FileSize %d, ValidDataLength %d\n",
142 (ULONG
)FileSizes
->AllocationSize
.QuadPart
,
143 (ULONG
)FileSizes
->FileSize
.QuadPart
,
144 (ULONG
)FileSizes
->ValidDataLength
.QuadPart
);
146 Bcb
= FileObject
->SectionObjectPointer
->SharedCacheMap
;
149 * It is valid to call this function on file objects that weren't
150 * initialized for caching. In this case it's simple no-op.
155 if (FileSizes
->AllocationSize
.QuadPart
< Bcb
->AllocationSize
.QuadPart
)
157 InitializeListHead(&FreeListHead
);
158 ExAcquireFastMutex(&ViewLock
);
159 KeAcquireSpinLock(&Bcb
->BcbLock
, &oldirql
);
161 current_entry
= Bcb
->BcbSegmentListHead
.Flink
;
162 while (current_entry
!= &Bcb
->BcbSegmentListHead
)
164 current
= CONTAINING_RECORD(current_entry
, CACHE_SEGMENT
, BcbSegmentListEntry
);
165 current_entry
= current_entry
->Flink
;
166 if (current
->FileOffset
> FileSizes
->AllocationSize
.QuadPart
)
168 if (current
->ReferenceCount
== 0 || (current
->ReferenceCount
== 1 && current
->Dirty
))
170 RemoveEntryList(¤t
->BcbSegmentListEntry
);
171 RemoveEntryList(¤t
->CacheSegmentListEntry
);
172 RemoveEntryList(¤t
->CacheSegmentLRUListEntry
);
175 RemoveEntryList(¤t
->DirtySegmentListEntry
);
176 DirtyPageCount
-= Bcb
->CacheSegmentSize
/ PAGE_SIZE
;
178 InsertHeadList(&FreeListHead
, ¤t
->BcbSegmentListEntry
);
182 DPRINT1("Anyone has referenced a cache segment behind the new size.\n");
188 Bcb
->AllocationSize
= FileSizes
->AllocationSize
;
189 Bcb
->FileSize
= FileSizes
->FileSize
;
190 KeReleaseSpinLock(&Bcb
->BcbLock
, oldirql
);
191 ExReleaseFastMutex(&ViewLock
);
193 current_entry
= FreeListHead
.Flink
;
194 while(current_entry
!= &FreeListHead
)
196 current
= CONTAINING_RECORD(current_entry
, CACHE_SEGMENT
, BcbSegmentListEntry
);
197 current_entry
= current_entry
->Flink
;
198 Status
= CcRosInternalFreeCacheSegment(current
);
199 if (!NT_SUCCESS(Status
))
201 DPRINT1("CcRosInternalFreeCacheSegment failed, status = %x\n");
208 KeAcquireSpinLock(&Bcb
->BcbLock
, &oldirql
);
209 Bcb
->AllocationSize
= FileSizes
->AllocationSize
;
210 Bcb
->FileSize
= FileSizes
->FileSize
;
211 KeReleaseSpinLock(&Bcb
->BcbLock
, oldirql
);
220 CcSetLogHandleForFile (
221 IN PFILE_OBJECT FileObject
,
223 IN PFLUSH_TO_LSN FlushToLsnRoutine
234 CcUninitializeCacheMap (
235 IN PFILE_OBJECT FileObject
,
236 IN PLARGE_INTEGER TruncateSize OPTIONAL
,
237 IN PCACHE_UNINITIALIZE_EVENT UninitializeCompleteEvent OPTIONAL
244 return CcRosReleaseFileCache(FileObject
);