3 Copyright (c) 1990-2000 Microsoft Corporation
11 This module implements the cache management routines for the Cdfs
12 FSD and FSP, by calling the Common Cache Manager.
20 // The Bug check file id for this module
23 #define BugCheckFileId (CDFS_BUG_CHECK_CACHESUP)
26 // Local debug trace level
30 #pragma alloc_text(PAGE, CdCompleteMdl)
31 #pragma alloc_text(PAGE, CdCreateInternalStream)
32 #pragma alloc_text(PAGE, CdDeleteInternalStream)
33 #pragma alloc_text(PAGE, CdPurgeVolume)
38 CdCreateInternalStream (
39 IN PIRP_CONTEXT IrpContext
,
48 This function creates an internal stream file for interaction
49 with the cache manager. The Fcb here can be for either a
50 directory stream or for a path table stream.
54 Vcb - Vcb for this volume.
56 Fcb - Points to the Fcb for this file. It is either an Index or
66 PFILE_OBJECT StreamFile
= NULL
;
67 BOOLEAN DecrementReference
= FALSE
;
69 BOOLEAN CleanupDirContext
= FALSE
;
70 BOOLEAN UpdateFcbSizes
= FALSE
;
73 DIRENT_ENUM_CONTEXT DirContext
;
77 ASSERT_IRP_CONTEXT( IrpContext
);
81 // We may only have the Fcb shared. Lock the Fcb and do a
82 // safe test to see if we need to really create the file object.
85 CdLockFcb( IrpContext
, Fcb
);
87 if (Fcb
->FileObject
!= NULL
) {
89 CdUnlockFcb( IrpContext
, Fcb
);
94 // Use a try-finally to facilitate cleanup.
100 // Create the internal stream. The Vpb should be pointing at our volume
101 // device object at this point.
104 StreamFile
= IoCreateStreamFileObject( NULL
, Vcb
->Vpb
->RealDevice
);
106 if (StreamFile
== NULL
) {
108 CdRaiseStatus( IrpContext
, STATUS_INSUFFICIENT_RESOURCES
);
112 // Initialize the fields of the file object.
115 StreamFile
->ReadAccess
= TRUE
;
116 StreamFile
->WriteAccess
= FALSE
;
117 StreamFile
->DeleteAccess
= FALSE
;
119 StreamFile
->SectionObjectPointer
= &Fcb
->FcbNonpaged
->SegmentObject
;
122 // Set the file object type and increment the Vcb counts.
125 CdSetFileObject( IrpContext
,
132 // We will reference the current Fcb twice to keep it from going
133 // away in the error path. Otherwise if we dereference it
134 // below in the finally clause a close could cause the Fcb to
138 CdLockVcb( IrpContext
, Vcb
);
139 CdIncrementReferenceCounts( IrpContext
, Fcb
, 2, 0 );
140 CdUnlockVcb( IrpContext
, Vcb
);
141 DecrementReference
= TRUE
;
144 // Initialize the cache map for the file.
147 CcInitializeCacheMap( StreamFile
,
148 (PCC_FILE_SIZES
)&Fcb
->AllocationSize
,
150 &CdData
.CacheManagerCallbacks
,
154 // Go ahead and store the stream file into the Fcb.
157 Fcb
->FileObject
= StreamFile
;
161 // If this is the first file object for a directory then we need to
162 // read the self entry for this directory and update the sizes
163 // in the Fcb. We know that the Fcb has been initialized so
164 // that we have a least one sector available to read.
167 if (!FlagOn( Fcb
->FcbState
, FCB_STATE_INITIALIZED
)) {
172 // Initialize the search structures.
175 CdInitializeDirContext( IrpContext
, &DirContext
);
176 CdInitializeDirent( IrpContext
, &Dirent
);
177 CleanupDirContext
= TRUE
;
180 // Read the dirent from disk and transfer the data to the
184 CdLookupDirent( IrpContext
,
189 CdUpdateDirentFromRawDirent( IrpContext
, Fcb
, &DirContext
, &Dirent
);
192 // Verify that this really for the self entry. We do this by
193 // updating the name in the dirent and then checking that it matches
194 // one of the hard coded names.
197 CdUpdateDirentName( IrpContext
, &Dirent
, FALSE
);
199 if (Dirent
.CdFileName
.FileName
.Buffer
!= CdUnicodeSelfArray
) {
201 CdRaiseStatus( IrpContext
, STATUS_FILE_CORRUPT_ERROR
);
205 // If the data sizes are different then update the header
206 // and Mcb for this Fcb.
209 NewDataLength
= BlockAlign( Vcb
, Dirent
.DataLength
+ Fcb
->StreamOffset
);
211 if (NewDataLength
== 0) {
213 CdRaiseStatus( IrpContext
, STATUS_FILE_CORRUPT_ERROR
);
216 if (NewDataLength
!= Fcb
->FileSize
.QuadPart
) {
218 Fcb
->AllocationSize
.QuadPart
=
219 Fcb
->FileSize
.QuadPart
=
220 Fcb
->ValidDataLength
.QuadPart
= NewDataLength
;
222 CcSetFileSizes( Fcb
->FileObject
, (PCC_FILE_SIZES
) &Fcb
->AllocationSize
);
224 CdTruncateAllocation( IrpContext
, Fcb
, 0 );
225 CdAddInitialAllocation( IrpContext
,
227 Dirent
.StartingOffset
,
230 UpdateFcbSizes
= TRUE
;
234 // Check for the existence flag and transform to hidden.
237 if (FlagOn( Dirent
.DirentFlags
, CD_ATTRIBUTE_HIDDEN
)) {
239 SetFlag( Fcb
->FileAttributes
, FILE_ATTRIBUTE_HIDDEN
);
243 // Convert the time to NT time.
246 CdConvertCdTimeToNtTime( IrpContext
,
248 (PLARGE_INTEGER
) &Fcb
->CreationTime
);
251 // Update the Fcb flags to indicate we have read the
255 SetFlag( Fcb
->FcbState
, FCB_STATE_INITIALIZED
);
258 // If we updated the sizes then we want to purge the file. Go
259 // ahead and unpin and then purge the first page.
262 CdCleanupDirContext( IrpContext
, &DirContext
);
263 CdCleanupDirent( IrpContext
, &Dirent
);
264 CleanupDirContext
= FALSE
;
266 if (UpdateFcbSizes
) {
268 CcPurgeCacheSection( &Fcb
->FcbNonpaged
->SegmentObject
,
278 // Cleanup any dirent structures we may have used.
281 if (CleanupDirContext
) {
283 CdCleanupDirContext( IrpContext
, &DirContext
);
284 CdCleanupDirent( IrpContext
, &Dirent
);
288 // If we raised then we need to dereference the file object.
291 if (StreamFile
!= NULL
) {
293 ObDereferenceObject( StreamFile
);
294 Fcb
->FileObject
= NULL
;
298 // Dereference and unlock the Fcb.
301 if (DecrementReference
) {
303 CdLockVcb( IrpContext
, Vcb
);
304 CdDecrementReferenceCounts( IrpContext
, Fcb
, 1, 0 );
305 CdUnlockVcb( IrpContext
, Vcb
);
308 CdUnlockFcb( IrpContext
, Fcb
);
316 CdDeleteInternalStream (
317 IN PIRP_CONTEXT IrpContext
,
325 This function creates an internal stream file for interaction
326 with the cache manager. The Fcb here can be for either a
327 directory stream or for a path table stream.
331 Fcb - Points to the Fcb for this file. It is either an Index or
341 PFILE_OBJECT FileObject
;
345 ASSERT_IRP_CONTEXT( IrpContext
);
352 CdLockFcb( IrpContext
, Fcb
);
355 // Capture the file object.
358 FileObject
= Fcb
->FileObject
;
359 Fcb
->FileObject
= NULL
;
362 // It is now safe to unlock the Fcb.
365 CdUnlockFcb( IrpContext
, Fcb
);
368 // Dereference the file object if present.
371 if (FileObject
!= NULL
) {
373 if (FileObject
->PrivateCacheMap
!= NULL
) {
375 CcUninitializeCacheMap( FileObject
, NULL
, NULL
);
378 ObDereferenceObject( FileObject
);
387 IN PIRP_CONTEXT IrpContext
,
395 This routine performs the function of completing Mdl reads.
396 It should be called only from CdFsdRead.
400 Irp - Supplies the originating Irp.
404 NTSTATUS - Will always be STATUS_SUCCESS.
409 PFILE_OBJECT FileObject
;
414 // Do completion processing.
417 FileObject
= IoGetCurrentIrpStackLocation( Irp
)->FileObject
;
419 CcMdlReadComplete( FileObject
, Irp
->MdlAddress
);
422 // Mdl is now deallocated.
425 Irp
->MdlAddress
= NULL
;
428 // Complete the request and exit right away.
431 CdCompleteRequest( IrpContext
, Irp
, STATUS_SUCCESS
);
433 return STATUS_SUCCESS
;
439 IN PIRP_CONTEXT IrpContext
,
441 IN BOOLEAN DismountUnderway
448 This routine is called to purge the volume. The purpose is to make all the stale file
449 objects in the system go away in order to lock the volume.
451 The Vcb is already acquired exclusively. We will lock out all file operations by
452 acquiring the global file resource. Then we will walk through all of the Fcb's and
457 Vcb - Vcb for the volume to purge.
459 DismountUnderway - Indicates that we are trying to delete all of the objects.
460 We will purge the Path Table and VolumeDasd and dereference all
465 NTSTATUS - The first failure of the purge operation.
470 NTSTATUS Status
= STATUS_SUCCESS
;
472 PVOID RestartKey
= NULL
;
480 ASSERT_EXCLUSIVE_VCB( Vcb
);
483 // Force any remaining Fcb's in the delayed close queue to be closed.
489 // Acquire the global file resource.
492 CdAcquireAllFiles( IrpContext
, Vcb
);
495 // Loop through each Fcb in the Fcb Table and perform the flush.
501 // Lock the Vcb to lookup the next Fcb.
504 CdLockVcb( IrpContext
, Vcb
);
505 NextFcb
= CdGetNextFcb( IrpContext
, Vcb
, &RestartKey
);
508 // Reference the NextFcb if present.
511 if (NextFcb
!= NULL
) {
513 NextFcb
->FcbReference
+= 1;
517 // If the last Fcb is present then decrement reference count and call teardown
518 // to see if it should be removed.
521 if (ThisFcb
!= NULL
) {
523 ThisFcb
->FcbReference
-= 1;
525 CdUnlockVcb( IrpContext
, Vcb
);
527 CdTeardownStructures( IrpContext
, ThisFcb
, &RemovedFcb
);
531 CdUnlockVcb( IrpContext
, Vcb
);
535 // Break out of the loop if no more Fcb's.
538 if (NextFcb
== NULL
) {
544 // Move to the next Fcb.
550 // If there is a image section then see if that can be closed.
553 if (ThisFcb
->FcbNonpaged
->SegmentObject
.ImageSectionObject
!= NULL
) {
555 MmFlushImageSection( &ThisFcb
->FcbNonpaged
->SegmentObject
, MmFlushForWrite
);
559 // If there is a data section then purge this. If there is an image
560 // section then we won't be able to. Remember this if it is our first
564 if ((ThisFcb
->FcbNonpaged
->SegmentObject
.DataSectionObject
!= NULL
) &&
565 !CcPurgeCacheSection( &ThisFcb
->FcbNonpaged
->SegmentObject
,
569 (Status
== STATUS_SUCCESS
)) {
571 Status
= STATUS_UNABLE_TO_DELETE_SECTION
;
575 // Dereference the internal stream if dismounting.
578 if (DismountUnderway
&&
579 (SafeNodeType( ThisFcb
) != CDFS_NTC_FCB_DATA
) &&
580 (ThisFcb
->FileObject
!= NULL
)) {
582 CdDeleteInternalStream( IrpContext
, ThisFcb
);
587 // Now look at the path table and volume Dasd Fcb's.
590 if (DismountUnderway
) {
592 if (Vcb
->PathTableFcb
!= NULL
) {
594 ThisFcb
= Vcb
->PathTableFcb
;
595 InterlockedIncrement( &Vcb
->PathTableFcb
->FcbReference
);
597 if ((ThisFcb
->FcbNonpaged
->SegmentObject
.DataSectionObject
!= NULL
) &&
598 !CcPurgeCacheSection( &ThisFcb
->FcbNonpaged
->SegmentObject
,
602 (Status
== STATUS_SUCCESS
)) {
604 Status
= STATUS_UNABLE_TO_DELETE_SECTION
;
607 CdDeleteInternalStream( IrpContext
, ThisFcb
);
609 InterlockedDecrement( &ThisFcb
->FcbReference
);
611 CdTeardownStructures( IrpContext
, ThisFcb
, &RemovedFcb
);
614 if (Vcb
->VolumeDasdFcb
!= NULL
) {
616 ThisFcb
= Vcb
->VolumeDasdFcb
;
617 InterlockedIncrement( &ThisFcb
->FcbReference
);
619 if ((ThisFcb
->FcbNonpaged
->SegmentObject
.DataSectionObject
!= NULL
) &&
620 !CcPurgeCacheSection( &ThisFcb
->FcbNonpaged
->SegmentObject
,
624 (Status
== STATUS_SUCCESS
)) {
626 Status
= STATUS_UNABLE_TO_DELETE_SECTION
;
629 InterlockedDecrement( &ThisFcb
->FcbReference
);
631 CdTeardownStructures( IrpContext
, ThisFcb
, &RemovedFcb
);
636 // Release all of the files.
639 CdReleaseAllFiles( IrpContext
, Vcb
);