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
,
53 CcGetFileObjectFromBcb (
57 PINTERNAL_BCB iBcb
= (PINTERNAL_BCB
)Bcb
;
58 return iBcb
->CacheSegment
->Bcb
->FileObject
;
66 CcGetLsnForFileObject (
67 IN PFILE_OBJECT FileObject
,
68 OUT PLARGE_INTEGER OldestLsn OPTIONAL
82 CcInitializeCacheMap (
83 IN PFILE_OBJECT FileObject
,
84 IN PCC_FILE_SIZES FileSizes
,
86 IN PCACHE_MANAGER_CALLBACKS CallBacks
,
87 IN PVOID LazyWriterContext
90 CcRosInitializeFileCache(FileObject
, VACB_MAPPING_GRANULARITY
);
111 CcPurgeCacheSection (
112 IN PSECTION_OBJECT_POINTERS SectionObjectPointer
,
113 IN PLARGE_INTEGER FileOffset OPTIONAL
,
115 IN BOOLEAN UninitializeCacheMaps
127 CcSetFileSizes (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 %d, FileSize %d, ValidDataLength %d\n",
140 (ULONG
)FileSizes
->AllocationSize
.QuadPart
,
141 (ULONG
)FileSizes
->FileSize
.QuadPart
,
142 (ULONG
)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 CcAcquireBrokenMutex(&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
)
166 if (current
->ReferenceCount
== 0 || (current
->ReferenceCount
== 1 && current
->Dirty
))
168 RemoveEntryList(¤t
->BcbSegmentListEntry
);
169 RemoveEntryList(¤t
->CacheSegmentListEntry
);
170 RemoveEntryList(¤t
->CacheSegmentLRUListEntry
);
173 RemoveEntryList(¤t
->DirtySegmentListEntry
);
174 DirtyPageCount
-= Bcb
->CacheSegmentSize
/ PAGE_SIZE
;
176 InsertHeadList(&FreeListHead
, ¤t
->BcbSegmentListEntry
);
180 DPRINT1("Anyone has referenced a cache segment behind the new size.\n");
186 Bcb
->AllocationSize
= FileSizes
->AllocationSize
;
187 Bcb
->FileSize
= FileSizes
->FileSize
;
188 KeReleaseSpinLock(&Bcb
->BcbLock
, oldirql
);
189 CcReleaseBrokenMutex(&ViewLock
);
191 current_entry
= FreeListHead
.Flink
;
192 while(current_entry
!= &FreeListHead
)
194 current
= CONTAINING_RECORD(current_entry
, CACHE_SEGMENT
, BcbSegmentListEntry
);
195 current_entry
= current_entry
->Flink
;
196 Status
= CcRosInternalFreeCacheSegment(current
);
197 if (!NT_SUCCESS(Status
))
199 DPRINT1("CcRosInternalFreeCacheSegment failed, status = %x\n", Status
);
206 KeAcquireSpinLock(&Bcb
->BcbLock
, &oldirql
);
207 Bcb
->AllocationSize
= FileSizes
->AllocationSize
;
208 Bcb
->FileSize
= FileSizes
->FileSize
;
209 KeReleaseSpinLock(&Bcb
->BcbLock
, oldirql
);
218 CcSetLogHandleForFile (
219 IN PFILE_OBJECT FileObject
,
221 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
242 return CcRosReleaseFileCache(FileObject
);