2 * Copyright (c) 2008, 2009, 2010, 2011 Kernel Drivers, LLC.
3 * Copyright (c) 2009, 2010, 2011 Your File System, Inc.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
10 * - Redistributions of source code must retain the above copyright notice,
11 * this list of conditions and the following disclaimer.
12 * - Redistributions in binary form must reproduce the above copyright
14 * this list of conditions and the following disclaimer in the
16 * and/or other materials provided with the distribution.
17 * - Neither the names of Kernel Drivers, LLC and Your File System, Inc.
18 * nor the names of their contributors may be used to endorse or promote
19 * products derived from this software without specific prior written
20 * permission from Kernel Drivers, LLC and Your File System, Inc.
22 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
23 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
24 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
25 * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
26 * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
27 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
28 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
29 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
30 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
31 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
32 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
39 #include "AFSCommon.h"
46 // This function is the IRP_MJ_CLOSE dispatch handler
50 // A status is returned for the handling of this request
54 AFSClose( IN PDEVICE_OBJECT LibDeviceObject,
58 NTSTATUS ntStatus = STATUS_SUCCESS;
59 ULONG ulRequestType = 0;
60 IO_STACK_LOCATION *pIrpSp = IoGetCurrentIrpStackLocation( Irp);
62 AFSDeviceExt *pDeviceExt = NULL;
64 AFSObjectInfoCB *pObjectInfo = NULL;
65 AFSDirectoryCB *pDirCB = NULL;
70 if( AFSRDRDeviceObject == NULL)
74 // Let this through, it's an close on the library control device
77 try_return( ntStatus);
80 pDeviceExt = (AFSDeviceExt *)AFSRDRDeviceObject->DeviceExtension;
82 pIrpSp = IoGetCurrentIrpStackLocation( Irp);
84 pFcb = (AFSFcb *)pIrpSp->FileObject->FsContext;
88 try_return( ntStatus);
91 pObjectInfo = pFcb->ObjectInformation;
94 // Perform the close functionality depending on the type of node it is
97 switch( pFcb->Header.NodeTypeCode)
103 AFSPIOCtlOpenCloseRequestCB stPIOCtlClose;
104 AFSFileID stParentFileId;
106 AFSDbgLogMsg( AFS_SUBSYSTEM_LOCK_PROCESSING,
107 AFS_TRACE_LEVEL_VERBOSE,
108 "AFSClose Acquiring GlobalRoot lock %08lX EXCL %08lX\n",
109 &pFcb->NPFcb->Resource,
110 PsGetCurrentThread());
112 AFSAcquireExcl( &pFcb->NPFcb->Resource,
115 pCcb = (AFSCcb *)pIrpSp->FileObject->FsContext2;
118 // Send the close to the CM
121 RtlZeroMemory( &stPIOCtlClose,
122 sizeof( AFSPIOCtlOpenCloseRequestCB));
124 stPIOCtlClose.RequestId = pCcb->RequestID;
126 stPIOCtlClose.RootId = pObjectInfo->VolumeCB->ObjectInformation.FileId;
128 RtlZeroMemory( &stParentFileId,
131 stParentFileId = pObjectInfo->ParentObjectInformation->FileId;
134 // Issue the close request to the service
137 AFSProcessRequest( AFS_REQUEST_TYPE_PIOCTL_CLOSE,
138 AFS_REQUEST_FLAG_SYNCHRONOUS,
142 (void *)&stPIOCtlClose,
143 sizeof( AFSPIOCtlOpenCloseRequestCB),
147 pDirCB = pCcb->DirectoryCB;
150 // Remove the Ccb and de-allocate it
153 ntStatus = AFSRemoveCcb( pFcb,
156 if( !NT_SUCCESS( ntStatus))
159 AFSDbgLogMsg( AFS_SUBSYSTEM_FILE_PROCESSING,
160 AFS_TRACE_LEVEL_WARNING,
161 "AFSClose Failed to remove Ccb from Fcb Status %08lX\n", ntStatus);
164 // We can't actually fail a close operation so reset the status
167 ntStatus = STATUS_SUCCESS;
170 ASSERT( pDirCB->OpenReferenceCount > 0);
172 InterlockedDecrement( &pDirCB->OpenReferenceCount);
174 AFSDbgLogMsg( AFS_SUBSYSTEM_DIRENTRY_REF_COUNTING,
175 AFS_TRACE_LEVEL_VERBOSE,
176 "AFSClose (IOCtl) Decrement count on %wZ DE %p Ccb %p Cnt %d\n",
177 &pDirCB->NameInformation.FileName,
180 pDirCB->OpenReferenceCount);
183 // If this is not the root then decrement the open child reference count
186 if( pObjectInfo->ParentObjectInformation != NULL &&
187 pObjectInfo->ParentObjectInformation->Specific.Directory.ChildOpenReferenceCount > 0)
190 InterlockedDecrement( &pObjectInfo->ParentObjectInformation->Specific.Directory.ChildOpenReferenceCount);
192 AFSDbgLogMsg( AFS_SUBSYSTEM_FCB_REF_COUNTING,
193 AFS_TRACE_LEVEL_VERBOSE,
194 "AFSClose (IOCtl) Decrement child open ref count on Parent object %08lX Cnt %d\n",
195 pObjectInfo->ParentObjectInformation,
196 pObjectInfo->ParentObjectInformation->Specific.Directory.ChildOpenReferenceCount);
199 AFSReleaseResource( &pFcb->NPFcb->Resource);
201 ASSERT( pFcb->OpenReferenceCount != 0);
203 InterlockedDecrement( &pFcb->OpenReferenceCount);
205 AFSDbgLogMsg( AFS_SUBSYSTEM_FCB_REF_COUNTING,
206 AFS_TRACE_LEVEL_VERBOSE,
207 "AFSClose (IOCtl) Decrement count on Fcb %08lX Cnt %d\n",
209 pFcb->OpenReferenceCount);
217 AFSDbgLogMsg( AFS_SUBSYSTEM_LOCK_PROCESSING,
218 AFS_TRACE_LEVEL_VERBOSE,
219 "AFSClose Acquiring Special Root ALL lock %08lX EXCL %08lX\n",
220 &pFcb->NPFcb->Resource,
221 PsGetCurrentThread());
223 AFSAcquireExcl( &pFcb->NPFcb->Resource,
226 pCcb = (AFSCcb *)pIrpSp->FileObject->FsContext2;
228 pDirCB = pCcb->DirectoryCB;
231 // Remove the Ccb and de-allocate it
234 ntStatus = AFSRemoveCcb( pFcb,
237 if( !NT_SUCCESS( ntStatus))
240 AFSDbgLogMsg( AFS_SUBSYSTEM_FILE_PROCESSING,
241 AFS_TRACE_LEVEL_WARNING,
242 "AFSClose Failed to remove Ccb from Fcb Status %08lX\n", ntStatus);
245 // We can't actually fail a close operation so reset the status
248 ntStatus = STATUS_SUCCESS;
251 ASSERT( pDirCB->OpenReferenceCount > 0);
253 InterlockedDecrement( &pDirCB->OpenReferenceCount);
255 AFSDbgLogMsg( AFS_SUBSYSTEM_DIRENTRY_REF_COUNTING,
256 AFS_TRACE_LEVEL_VERBOSE,
257 "AFSClose Decrement (Root ALL) count on %wZ DE %p Ccb %p Cnt %d\n",
258 &pDirCB->NameInformation.FileName,
261 pDirCB->OpenReferenceCount);
263 AFSReleaseResource( &pFcb->NPFcb->Resource);
265 ASSERT( pFcb->OpenReferenceCount > 0);
267 InterlockedDecrement( &pFcb->OpenReferenceCount);
269 AFSDbgLogMsg( AFS_SUBSYSTEM_FCB_REF_COUNTING,
270 AFS_TRACE_LEVEL_VERBOSE,
271 "AFSClose (RootAll) Decrement count on Fcb %08lX Cnt %d\n",
273 pFcb->OpenReferenceCount);
279 // Root, file or directory node
284 case AFS_DIRECTORY_FCB:
285 case AFS_SYMBOLIC_LINK_FCB:
286 case AFS_MOUNT_POINT_FCB:
287 case AFS_DFS_LINK_FCB:
290 pCcb = (AFSCcb *)pIrpSp->FileObject->FsContext2;
293 // We may be performing some cleanup on the Fcb so grab it exclusive to ensure no collisions
296 AFSDbgLogMsg( AFS_SUBSYSTEM_LOCK_PROCESSING,
297 AFS_TRACE_LEVEL_VERBOSE,
298 "AFSClose Acquiring Dcb lock %08lX EXCL %08lX\n",
299 &pFcb->NPFcb->Resource,
300 PsGetCurrentThread());
302 AFSAcquireExcl( &pFcb->NPFcb->Resource,
305 KeQueryTickCount( &pFcb->ObjectInformation->LastAccessCount);
307 pDirCB = pCcb->DirectoryCB;
310 // If this entry is deleted then remove the object from the volume tree
313 if( BooleanFlagOn( pDirCB->Flags, AFS_DIR_ENTRY_DELETED))
316 if( pFcb->Header.NodeTypeCode == AFS_FILE_FCB)
320 // Stop anything possibly in process
323 AFSDbgLogMsg( AFS_SUBSYSTEM_LOCK_PROCESSING,
324 AFS_TRACE_LEVEL_VERBOSE,
325 "AFSClose Acquiring Fcb extents lock %08lX EXCL %08lX\n",
326 &pFcb->NPFcb->Specific.File.ExtentsResource,
327 PsGetCurrentThread());
329 AFSAcquireExcl( &pObjectInfo->Fcb->NPFcb->Specific.File.ExtentsResource,
332 pObjectInfo->Fcb->NPFcb->Specific.File.ExtentsRequestStatus = STATUS_FILE_DELETED;
334 KeSetEvent( &pObjectInfo->Fcb->NPFcb->Specific.File.ExtentsRequestComplete,
338 AFSDbgLogMsg( AFS_SUBSYSTEM_LOCK_PROCESSING,
339 AFS_TRACE_LEVEL_VERBOSE,
340 "AFSClose Releasing Fcb extents lock %08lX EXCL %08lX\n",
341 &pFcb->NPFcb->Specific.File.ExtentsResource,
342 PsGetCurrentThread());
344 AFSReleaseResource( &pObjectInfo->Fcb->NPFcb->Specific.File.ExtentsResource);
347 AFSAcquireExcl( pObjectInfo->ParentObjectInformation->Specific.Directory.DirectoryNodeHdr.TreeLock,
350 AFSAcquireExcl( pObjectInfo->VolumeCB->ObjectInfoTree.TreeLock,
353 if ( pDirCB->OpenReferenceCount == 0)
357 "AFSClose (Other) OpenReferenceCount is Zero on DE %08lX Ccb %08lX FileName %wZ\n",
360 &pDirCB->NameInformation.FileName);
363 ASSERT( pDirCB->OpenReferenceCount > 0);
365 InterlockedDecrement( &pDirCB->OpenReferenceCount);
367 AFSDbgLogMsg( AFS_SUBSYSTEM_DIRENTRY_REF_COUNTING,
368 AFS_TRACE_LEVEL_VERBOSE,
369 "AFSClose (Other) Decrement count on %wZ DE %p Ccb %p Cnt %d\n",
370 &pDirCB->NameInformation.FileName,
373 pDirCB->OpenReferenceCount);
375 if( pDirCB->OpenReferenceCount == 0)
378 AFSDbgLogMsg( AFS_SUBSYSTEM_CLEANUP_PROCESSING,
379 AFS_TRACE_LEVEL_VERBOSE,
380 "AFSClose Deleting dir entry %08lX (%08lX) for %wZ\n",
383 &pDirCB->NameInformation.FileName);
386 // Remove and delete the directory entry from the parent list
389 AFSDeleteDirEntry( pObjectInfo->ParentObjectInformation,
392 if( pObjectInfo->ObjectReferenceCount == 0)
395 if( BooleanFlagOn( pObjectInfo->Flags, AFS_OBJECT_INSERTED_HASH_TREE))
398 AFSDbgLogMsg( AFS_SUBSYSTEM_CLEANUP_PROCESSING,
399 AFS_TRACE_LEVEL_VERBOSE,
400 "AFSClose Removing object %08lX from volume tree\n",
403 AFSRemoveHashEntry( &pObjectInfo->VolumeCB->ObjectInfoTree.TreeHead,
404 &pObjectInfo->TreeEntry);
406 ClearFlag( pObjectInfo->Flags, AFS_OBJECT_INSERTED_HASH_TREE);
409 SetFlag( pObjectInfo->Flags, AFS_OBJECT_FLAGS_DELETED);
413 AFSReleaseResource( pObjectInfo->ParentObjectInformation->Specific.Directory.DirectoryNodeHdr.TreeLock);
415 AFSReleaseResource( pObjectInfo->VolumeCB->ObjectInfoTree.TreeLock);
420 ASSERT( pDirCB->OpenReferenceCount > 0);
422 InterlockedDecrement( &pDirCB->OpenReferenceCount);
424 AFSDbgLogMsg( AFS_SUBSYSTEM_DIRENTRY_REF_COUNTING,
425 AFS_TRACE_LEVEL_VERBOSE,
426 "AFSClose (Other2) Decrement count on %wZ DE %p Ccb %p Cnt %d\n",
427 &pDirCB->NameInformation.FileName,
430 pDirCB->OpenReferenceCount);
434 // If this is not the root then decrement the open child reference count
437 if( pObjectInfo != NULL &&
438 pObjectInfo->ParentObjectInformation != NULL &&
439 pObjectInfo->ParentObjectInformation->Specific.Directory.ChildOpenReferenceCount > 0)
442 InterlockedDecrement( &pObjectInfo->ParentObjectInformation->Specific.Directory.ChildOpenReferenceCount);
444 AFSDbgLogMsg( AFS_SUBSYSTEM_FCB_REF_COUNTING,
445 AFS_TRACE_LEVEL_VERBOSE,
446 "AFSClose Decrement child open ref count on Parent object %08lX Cnt %d\n",
447 pObjectInfo->ParentObjectInformation,
448 pObjectInfo->ParentObjectInformation->Specific.Directory.ChildOpenReferenceCount);
451 if( pFcb->OpenReferenceCount == 1 &&
452 pFcb->Header.NodeTypeCode == AFS_FILE_FCB)
455 SetFlag( pFcb->Flags, AFS_FCB_FILE_CLOSED);
458 // Attempt to tear down our extent list for the file
459 // If there are remaining dirty extents then attempt to
460 // flush them as well
463 if( pFcb->Specific.File.ExtentsDirtyCount)
466 AFSFlushExtents( pFcb,
471 // Wait for any outstanding queued flushes to complete
474 AFSWaitOnQueuedFlushes( pFcb);
476 ASSERT( pFcb->Specific.File.ExtentsDirtyCount == 0 &&
477 pFcb->Specific.File.QueuedFlushCount == 0);
479 AFSReleaseResource( &pFcb->NPFcb->Resource);
482 // Tear 'em down, we'll not be needing them again
485 if( AFSTearDownFcbExtents( pFcb,
490 // Indicate to the service that the file required complete flushing to the
494 AFSProcessRequest( AFS_REQUEST_TYPE_FLUSH_FILE,
495 AFS_REQUEST_FLAG_SYNCHRONOUS,
498 &pFcb->ObjectInformation->FileId,
508 if( pFcb->Header.NodeTypeCode == AFS_FILE_FCB)
511 if( pFcb->Specific.File.ExtentsDirtyCount)
514 AFSFlushExtents( pFcb,
519 AFSReleaseResource( &pFcb->NPFcb->Resource);
523 // Remove the Ccb and de-allocate it
526 ntStatus = AFSRemoveCcb( pFcb,
529 if( !NT_SUCCESS( ntStatus))
532 AFSDbgLogMsg( AFS_SUBSYSTEM_FILE_PROCESSING,
533 AFS_TRACE_LEVEL_WARNING,
534 "AFSClose Failed to remove Ccb from Fcb Status %08lX\n",
538 // We can't actually fail a close operation so reset the status
541 ntStatus = STATUS_SUCCESS;
545 // Decrement the reference count on the Fcb. this is protecting it from teardown.
548 ASSERT( pFcb->OpenReferenceCount != 0);
550 InterlockedDecrement( &pFcb->OpenReferenceCount);
552 AFSDbgLogMsg( AFS_SUBSYSTEM_FCB_REF_COUNTING,
553 AFS_TRACE_LEVEL_VERBOSE,
554 "AFSClose Decrement count on Fcb %08lX Cnt %d\n",
556 pFcb->OpenReferenceCount);
561 case AFS_SPECIAL_SHARE_FCB:
564 AFSPipeOpenCloseRequestCB stPipeClose;
566 AFSDbgLogMsg( AFS_SUBSYSTEM_LOCK_PROCESSING,
567 AFS_TRACE_LEVEL_VERBOSE,
568 "AFSClose Acquiring Special Share lock %08lX EXCL %08lX\n",
569 &pFcb->NPFcb->Resource,
570 PsGetCurrentThread());
572 AFSAcquireExcl( &pFcb->NPFcb->Resource,
575 pCcb = (AFSCcb *)pIrpSp->FileObject->FsContext2;
577 pDirCB = pCcb->DirectoryCB;
579 RtlZeroMemory( &stPipeClose,
580 sizeof( AFSPipeOpenCloseRequestCB));
582 stPipeClose.RequestId = pCcb->RequestID;
584 stPipeClose.RootId = pObjectInfo->VolumeCB->ObjectInformation.FileId;
587 // Issue the open request to the service
591 AFSProcessRequest( AFS_REQUEST_TYPE_PIPE_CLOSE,
592 AFS_REQUEST_FLAG_SYNCHRONOUS,
594 &pDirCB->NameInformation.FileName,
596 (void *)&stPipeClose,
597 sizeof( AFSPipeOpenCloseRequestCB),
603 // Remove the Ccb and de-allocate it
606 ntStatus = AFSRemoveCcb( pFcb,
609 if( !NT_SUCCESS( ntStatus))
612 AFSDbgLogMsg( AFS_SUBSYSTEM_FILE_PROCESSING,
613 AFS_TRACE_LEVEL_WARNING,
614 "AFSClose Failed to remove Ccb from Fcb Status %08lX\n", ntStatus);
617 // We can't actually fail a close operation so reset the status
620 ntStatus = STATUS_SUCCESS;
623 ASSERT( pDirCB->OpenReferenceCount > 0);
625 InterlockedDecrement( &pDirCB->OpenReferenceCount);
627 AFSDbgLogMsg( AFS_SUBSYSTEM_DIRENTRY_REF_COUNTING,
628 AFS_TRACE_LEVEL_VERBOSE,
629 "AFSClose (Share) Decrement count on %wZ DE %p Ccb %p Cnt %d\n",
630 &pDirCB->NameInformation.FileName,
633 pDirCB->OpenReferenceCount);
636 // If this is not the root then decrement the open child reference count
639 if( pObjectInfo->ParentObjectInformation != NULL &&
640 pObjectInfo->ParentObjectInformation->Specific.Directory.ChildOpenReferenceCount > 0)
643 InterlockedDecrement( &pObjectInfo->ParentObjectInformation->Specific.Directory.ChildOpenReferenceCount);
645 AFSDbgLogMsg( AFS_SUBSYSTEM_FCB_REF_COUNTING,
646 AFS_TRACE_LEVEL_VERBOSE,
647 "AFSClose (Share) Decrement child open ref count on Parent object %08lX Cnt %d\n",
648 pObjectInfo->ParentObjectInformation,
649 pObjectInfo->ParentObjectInformation->Specific.Directory.ChildOpenReferenceCount);
652 AFSReleaseResource( &pFcb->NPFcb->Resource);
654 ASSERT( pFcb->OpenReferenceCount != 0);
656 InterlockedDecrement( &pFcb->OpenReferenceCount);
658 AFSDbgLogMsg( AFS_SUBSYSTEM_FCB_REF_COUNTING,
659 AFS_TRACE_LEVEL_VERBOSE,
660 "AFSClose (Share) Decrement count on Fcb %08lX Cnt %d\n",
662 pFcb->OpenReferenceCount);
669 AFSDbgLogMsg( AFS_SUBSYSTEM_FILE_PROCESSING,
670 AFS_TRACE_LEVEL_ERROR,
671 "AFSClose Processing unknown node type %d\n",
672 pFcb->Header.NodeTypeCode);
680 // Complete the request
683 AFSCompleteRequest( Irp,
686 __except( AFSExceptionFilter( GetExceptionCode(), GetExceptionInformation()) )
691 "EXCEPTION - AFSClose\n");