5ee30f1890f0208b2d69060cb52b172d419c9dc7
[openafs.git] / src / WINNT / afsrdr / kernel / lib / AFSClose.cpp
1 /*
2  * Copyright (c) 2008, 2009, 2010, 2011, 2012, 2013 Kernel Drivers, LLC.
3  * Copyright (c) 2009, 2010, 2011, 2012, 2013 Your File System, Inc.
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  *
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
13  *   notice, this list of conditions and the following disclaimer in the
14  *   documentation and/or other materials provided with the distribution.
15  * - Neither the names of Kernel Drivers, LLC and Your File System, Inc.
16  *   nor the names of their contributors may be used to endorse or promote
17  *   products derived from this software without specific prior written
18  *   permission from Kernel Drivers, LLC and Your File System, Inc.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
23  * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
24  * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
25  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
26  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
27  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
28  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
29  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
30  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31  */
32
33 //
34 // File: AFSClose.cpp
35 //
36
37 #include "AFSCommon.h"
38
39 //
40 // Function: AFSClose
41 //
42 // Description:
43 //
44 //      This function is the IRP_MJ_CLOSE dispatch handler
45 //
46 // Return:
47 //
48 //       A status is returned for the handling of this request
49 //
50
51 NTSTATUS
52 AFSClose( IN PDEVICE_OBJECT LibDeviceObject,
53           IN PIRP Irp)
54 {
55     UNREFERENCED_PARAMETER(LibDeviceObject);
56     NTSTATUS ntStatus = STATUS_SUCCESS;
57     IO_STACK_LOCATION *pIrpSp = IoGetCurrentIrpStackLocation( Irp);
58     AFSFcb *pFcb = NULL;
59     AFSDeviceExt *pDeviceExt = NULL;
60     AFSCcb *pCcb = NULL;
61     AFSObjectInfoCB *pObjectInfo = NULL;
62     AFSObjectInfoCB *pParentObjectInfo = NULL;
63     AFSDirectoryCB *pDirCB = NULL;
64     LONG lCount;
65
66     __try
67     {
68
69         if( AFSRDRDeviceObject == NULL)
70         {
71
72             //
73             // Let this through, it's an close on the library control device
74             //
75
76             try_return( ntStatus);
77         }
78
79         pDeviceExt = (AFSDeviceExt *)AFSRDRDeviceObject->DeviceExtension;
80
81         pIrpSp = IoGetCurrentIrpStackLocation( Irp);
82
83         pFcb = (AFSFcb *)pIrpSp->FileObject->FsContext;
84
85         if( pFcb == NULL)
86         {
87             try_return( ntStatus);
88         }
89
90         pObjectInfo = pFcb->ObjectInformation;
91
92         //
93         // Perform the close functionality depending on the type of node it is
94         //
95
96         switch( pFcb->Header.NodeTypeCode)
97         {
98
99             case AFS_IOCTL_FCB:
100             {
101
102                 AFSPIOCtlOpenCloseRequestCB stPIOCtlClose;
103                 AFSFileID stParentFileId;
104
105                 AFSDbgTrace(( AFS_SUBSYSTEM_LOCK_PROCESSING,
106                               AFS_TRACE_LEVEL_VERBOSE,
107                               "AFSClose Acquiring GlobalRoot lock %p EXCL %08lX\n",
108                               &pFcb->NPFcb->Resource,
109                               PsGetCurrentThread()));
110
111                 AFSAcquireExcl( &pFcb->NPFcb->Resource,
112                                   TRUE);
113
114                 pCcb = (AFSCcb *)pIrpSp->FileObject->FsContext2;
115
116                 pIrpSp->FileObject->FsContext2 = NULL;
117
118                 //
119                 // Send the close to the CM
120                 //
121
122                 RtlZeroMemory( &stPIOCtlClose,
123                                sizeof( AFSPIOCtlOpenCloseRequestCB));
124
125                 stPIOCtlClose.RequestId = pCcb->RequestID;
126
127                 stPIOCtlClose.RootId = pObjectInfo->VolumeCB->ObjectInformation.FileId;
128
129                 RtlZeroMemory( &stParentFileId,
130                                sizeof( AFSFileID));
131
132                 stParentFileId = pObjectInfo->ParentFileId;
133
134                 //
135                 // Issue the close request to the service
136                 //
137
138                 AFSProcessRequest( AFS_REQUEST_TYPE_PIOCTL_CLOSE,
139                                    AFS_REQUEST_FLAG_SYNCHRONOUS,
140                                    &pCcb->AuthGroup,
141                                    NULL,
142                                    &stParentFileId,
143                                    NULL,
144                                    0,
145                                    (void *)&stPIOCtlClose,
146                                    sizeof( AFSPIOCtlOpenCloseRequestCB),
147                                    NULL,
148                                    NULL);
149
150                 //
151                 // Remove the Ccb and de-allocate it
152                 //
153
154                 AFSRemoveCcb( pFcb,
155                               pCcb);
156
157                 //
158                 // If this is not the root then decrement the open child reference count
159                 //
160
161                 if ( BooleanFlagOn( pObjectInfo->Flags, AFS_OBJECT_FLAGS_PARENT_FID))
162                 {
163
164                     pParentObjectInfo = AFSFindObjectInfo( pObjectInfo->VolumeCB,
165                                                            &pObjectInfo->ParentFileId,
166                                                            FALSE);
167                 }
168
169                 if( pParentObjectInfo != NULL &&
170                     pParentObjectInfo->Specific.Directory.ChildOpenReferenceCount > 0)
171                 {
172
173                     InterlockedDecrement( &pParentObjectInfo->Specific.Directory.ChildOpenReferenceCount);
174
175                     AFSDbgTrace(( AFS_SUBSYSTEM_OBJECT_REF_COUNTING,
176                                   AFS_TRACE_LEVEL_VERBOSE,
177                                   "AFSClose (IOCtl) Decrement child open ref count on Parent object %p Cnt %d\n",
178                                   pParentObjectInfo,
179                                   pParentObjectInfo->Specific.Directory.ChildOpenReferenceCount));
180                 }
181
182                 AFSReleaseResource( &pFcb->NPFcb->Resource);
183
184                 pIrpSp->FileObject->FsContext = NULL;
185
186                 lCount = InterlockedDecrement( &pFcb->OpenReferenceCount);
187
188                 AFSDbgTrace(( AFS_SUBSYSTEM_FCB_REF_COUNTING,
189                               AFS_TRACE_LEVEL_VERBOSE,
190                               "AFSClose (IOCtl) Decrement count on Fcb %p Cnt %d\n",
191                               pFcb,
192                               lCount));
193
194                 ASSERT( lCount >= 0);
195
196                 break;
197             }
198
199             case AFS_ROOT_ALL:
200             {
201
202                 AFSDbgTrace(( AFS_SUBSYSTEM_LOCK_PROCESSING,
203                               AFS_TRACE_LEVEL_VERBOSE,
204                               "AFSClose Acquiring Special Root ALL lock %p EXCL %08lX\n",
205                               &pFcb->NPFcb->Resource,
206                               PsGetCurrentThread()));
207
208                 AFSAcquireExcl( &pFcb->NPFcb->Resource,
209                                 TRUE);
210
211                 pCcb = (AFSCcb *)pIrpSp->FileObject->FsContext2;
212
213                 pIrpSp->FileObject->FsContext2;
214
215                 //
216                 // Remove the Ccb and de-allocate it
217                 //
218
219                 AFSRemoveCcb( pFcb,
220                               pCcb);
221
222                 AFSReleaseResource( &pFcb->NPFcb->Resource);
223
224                 pIrpSp->FileObject->FsContext = NULL;
225
226                 lCount = InterlockedDecrement( &pFcb->OpenReferenceCount);
227
228                 AFSDbgTrace(( AFS_SUBSYSTEM_FCB_REF_COUNTING,
229                               AFS_TRACE_LEVEL_VERBOSE,
230                               "AFSClose (RootAll) Decrement count on Fcb %p Cnt %d\n",
231                               pFcb,
232                               lCount));
233
234                 ASSERT( lCount >= 0);
235
236                 break;
237             }
238
239             //
240             // Root, file or directory node
241             //
242
243             case AFS_FILE_FCB:
244             case AFS_ROOT_FCB:
245             case AFS_DIRECTORY_FCB:
246             case AFS_SYMBOLIC_LINK_FCB:
247             case AFS_MOUNT_POINT_FCB:
248             case AFS_DFS_LINK_FCB:
249             case AFS_INVALID_FCB:
250             {
251
252                 pCcb = (AFSCcb *)pIrpSp->FileObject->FsContext2;
253
254                 pIrpSp->FileObject->FsContext2 = NULL;
255
256                 //
257                 // We may be performing some cleanup on the Fcb so grab it exclusive to ensure no collisions
258                 //
259
260                 AFSDbgTrace(( AFS_SUBSYSTEM_LOCK_PROCESSING,
261                               AFS_TRACE_LEVEL_VERBOSE,
262                               "AFSClose Acquiring Dcb lock %p EXCL %08lX\n",
263                               &pFcb->NPFcb->Resource,
264                               PsGetCurrentThread()));
265
266                 AFSAcquireExcl( &pFcb->NPFcb->Resource,
267                                 TRUE);
268
269                 KeQueryTickCount( &pFcb->ObjectInformation->LastAccessCount);
270
271                 if( pFcb->OpenReferenceCount == 1 &&
272                     pFcb->Header.NodeTypeCode == AFS_FILE_FCB)
273                 {
274
275                     SetFlag( pFcb->Flags, AFS_FCB_FILE_CLOSED);
276
277                     //
278                     // Attempt to tear down our extent list for the file
279                     // If there are remaining dirty extents then attempt to
280                     // flush them as well
281                     //
282
283                     if( pFcb->Specific.File.ExtentsDirtyCount)
284                     {
285
286                         AFSFlushExtents( pFcb,
287                                          &pCcb->AuthGroup);
288                     }
289
290                     //
291                     // Wait for any outstanding queued flushes to complete
292                     //
293
294                     AFSWaitOnQueuedFlushes( pFcb);
295
296                     ASSERT( pFcb->Specific.File.ExtentsDirtyCount == 0 &&
297                             pFcb->Specific.File.QueuedFlushCount == 0);
298
299                     AFSReleaseResource( &pFcb->NPFcb->Resource);
300
301                     //
302                     // Tear 'em down, we'll not be needing them again
303                     //
304
305                     AFSTearDownFcbExtents( pFcb,
306                                            &pCcb->AuthGroup);
307                 }
308                 else
309                 {
310
311                     if( pFcb->Header.NodeTypeCode == AFS_FILE_FCB &&
312                         pFcb->Specific.File.ExtentsDirtyCount &&
313                         (pCcb->GrantedAccess & FILE_WRITE_DATA))
314                     {
315
316                         AFSFlushExtents( pFcb,
317                                          &pCcb->AuthGroup);
318                     }
319
320                     AFSReleaseResource( &pFcb->NPFcb->Resource);
321                 }
322
323                 pDirCB = pCcb->DirectoryCB;
324
325                 //
326                 // Steal the DirOpenReferenceCount from the Ccb
327                 //
328
329                 pCcb->DirectoryCB = NULL;
330
331                 //
332                 // Object the Parent ObjectInformationCB
333                 //
334
335                 if( BooleanFlagOn( pObjectInfo->Flags, AFS_OBJECT_FLAGS_PARENT_FID))
336                 {
337
338                     pParentObjectInfo = AFSFindObjectInfo( pObjectInfo->VolumeCB,
339                                                            &pObjectInfo->ParentFileId,
340                                                            FALSE);
341                 }
342
343                 //
344                 // Remove the Ccb and de-allocate it
345                 //
346
347                 AFSRemoveCcb( pFcb,
348                               pCcb);
349
350                 //
351                 // If this entry is deleted then remove the object from the volume tree
352                 //
353
354                 if( BooleanFlagOn( pDirCB->Flags, AFS_DIR_ENTRY_DELETED))
355                 {
356
357                     if( pFcb->Header.NodeTypeCode == AFS_FILE_FCB &&
358                         pObjectInfo->Links == 0)
359                     {
360
361                         //
362                         // Stop anything possibly in process
363                         //
364
365                         AFSDbgTrace(( AFS_SUBSYSTEM_LOCK_PROCESSING,
366                                       AFS_TRACE_LEVEL_VERBOSE,
367                                       "AFSClose Acquiring Fcb extents lock %p EXCL %08lX\n",
368                                       &pFcb->NPFcb->Specific.File.ExtentsResource,
369                                       PsGetCurrentThread()));
370
371                         AFSAcquireExcl( &pObjectInfo->Fcb->NPFcb->Specific.File.ExtentsResource,
372                                         TRUE);
373
374                         pObjectInfo->Fcb->NPFcb->Specific.File.ExtentsRequestStatus = STATUS_FILE_DELETED;
375
376                         KeSetEvent( &pObjectInfo->Fcb->NPFcb->Specific.File.ExtentsRequestComplete,
377                                     0,
378                                     FALSE);
379
380                         AFSDbgTrace(( AFS_SUBSYSTEM_LOCK_PROCESSING,
381                                       AFS_TRACE_LEVEL_VERBOSE,
382                                       "AFSClose Releasing Fcb extents lock %p EXCL %08lX\n",
383                                       &pFcb->NPFcb->Specific.File.ExtentsResource,
384                                       PsGetCurrentThread()));
385
386                         AFSReleaseResource( &pObjectInfo->Fcb->NPFcb->Specific.File.ExtentsResource);
387                     }
388
389                     ASSERT( pParentObjectInfo != NULL);
390
391                     if ( pParentObjectInfo != NULL)
392                     {
393                         AFSAcquireExcl( pParentObjectInfo->Specific.Directory.DirectoryNodeHdr.TreeLock,
394                                         TRUE);
395
396                         AFSAcquireExcl( pObjectInfo->VolumeCB->ObjectInfoTree.TreeLock,
397                                         TRUE);
398
399                         lCount = InterlockedDecrement( &pDirCB->DirOpenReferenceCount);
400
401                         AFSDbgTrace(( AFS_SUBSYSTEM_DIRENTRY_REF_COUNTING,
402                                       AFS_TRACE_LEVEL_VERBOSE,
403                                       "AFSClose (Other) Decrement count on %wZ DE %p Ccb %p Cnt %d\n",
404                                       &pDirCB->NameInformation.FileName,
405                                       pDirCB,
406                                       pCcb,
407                                       lCount));
408
409                         ASSERT( lCount >= 0);
410
411                         if( lCount == 0 &&
412                             pDirCB->NameArrayReferenceCount <= 0)
413                         {
414
415                             AFSDbgTrace(( AFS_SUBSYSTEM_CLEANUP_PROCESSING,
416                                           AFS_TRACE_LEVEL_VERBOSE,
417                                           "AFSClose Deleting dir entry %p (%p) for %wZ  FID %08lX-%08lX-%08lX-%08lX\n",
418                                           pDirCB,
419                                           pObjectInfo,
420                                           &pDirCB->NameInformation.FileName,
421                                           pObjectInfo->FileId.Cell,
422                                           pObjectInfo->FileId.Volume,
423                                           pObjectInfo->FileId.Vnode,
424                                           pObjectInfo->FileId.Unique));
425
426                             //
427                             // Remove and delete the directory entry from the parent list
428                             //
429
430                             AFSDeleteDirEntry( pParentObjectInfo,
431                                                &pDirCB);
432
433                             AFSAcquireShared( &pObjectInfo->NonPagedInfo->ObjectInfoLock,
434                                               TRUE);
435
436                             if( pObjectInfo->ObjectReferenceCount <= 0)
437                             {
438
439                                 if( BooleanFlagOn( pObjectInfo->Flags, AFS_OBJECT_INSERTED_HASH_TREE))
440                                 {
441
442                                     AFSDbgTrace(( AFS_SUBSYSTEM_CLEANUP_PROCESSING,
443                                                   AFS_TRACE_LEVEL_VERBOSE,
444                                                   "AFSClose Removing object %p from volume tree\n",
445                                                   pObjectInfo));
446
447                                     AFSRemoveHashEntry( &pObjectInfo->VolumeCB->ObjectInfoTree.TreeHead,
448                                                         &pObjectInfo->TreeEntry);
449
450                                     ClearFlag( pObjectInfo->Flags, AFS_OBJECT_INSERTED_HASH_TREE);
451                                 }
452                             }
453
454                             AFSReleaseResource( &pObjectInfo->NonPagedInfo->ObjectInfoLock);
455                         }
456
457                         AFSReleaseResource( pObjectInfo->VolumeCB->ObjectInfoTree.TreeLock);
458
459                         AFSReleaseResource( pParentObjectInfo->Specific.Directory.DirectoryNodeHdr.TreeLock);
460                     }
461                 }
462                 else
463                 {
464
465                     lCount = InterlockedDecrement( &pDirCB->DirOpenReferenceCount);
466
467                     AFSDbgTrace(( AFS_SUBSYSTEM_DIRENTRY_REF_COUNTING,
468                                   AFS_TRACE_LEVEL_VERBOSE,
469                                   "AFSClose (Other2) Decrement count on %wZ DE %p Ccb %p Cnt %d\n",
470                                   &pDirCB->NameInformation.FileName,
471                                   pDirCB,
472                                   pCcb,
473                                   lCount));
474
475                     ASSERT( lCount >= 0);
476                 }
477
478                 //
479                 // If this is not the root then decrement the open child reference count
480                 //
481
482                 if( pObjectInfo != NULL &&
483                     pParentObjectInfo != NULL &&
484                     pParentObjectInfo->Specific.Directory.ChildOpenReferenceCount > 0)
485                 {
486
487                     InterlockedDecrement( &pParentObjectInfo->Specific.Directory.ChildOpenReferenceCount);
488
489                     AFSDbgTrace(( AFS_SUBSYSTEM_OBJECT_REF_COUNTING,
490                                   AFS_TRACE_LEVEL_VERBOSE,
491                                   "AFSClose Decrement child open ref count on Parent object %p Cnt %d\n",
492                                   pParentObjectInfo,
493                                   pParentObjectInfo->Specific.Directory.ChildOpenReferenceCount));
494                 }
495
496                 pIrpSp->FileObject->FsContext = NULL;
497
498                 //
499                 // Decrement the reference count on the Fcb. this is protecting it from teardown.
500                 //
501
502                 lCount = InterlockedDecrement( &pFcb->OpenReferenceCount);
503
504                 AFSDbgTrace(( AFS_SUBSYSTEM_FCB_REF_COUNTING,
505                               AFS_TRACE_LEVEL_VERBOSE,
506                               "AFSClose Decrement count on Fcb %p Cnt %d\n",
507                               pFcb,
508                               lCount));
509
510                 ASSERT( lCount >= 0);
511
512                 break;
513             }
514
515             case AFS_SPECIAL_SHARE_FCB:
516             {
517
518                 AFSPipeOpenCloseRequestCB stPipeClose;
519
520                 pCcb = (AFSCcb *)pIrpSp->FileObject->FsContext2;
521
522                 pIrpSp->FileObject->FsContext2 = NULL;
523
524                 //
525                 // Object the Parent ObjectInformationCB
526                 //
527
528                 if( BooleanFlagOn( pObjectInfo->Flags, AFS_OBJECT_FLAGS_PARENT_FID))
529                 {
530
531                     pParentObjectInfo = AFSFindObjectInfo( pObjectInfo->VolumeCB,
532                                                            &pObjectInfo->ParentFileId,
533                                                            FALSE);
534                 }
535
536                 AFSDbgTrace(( AFS_SUBSYSTEM_LOCK_PROCESSING,
537                               AFS_TRACE_LEVEL_VERBOSE,
538                               "AFSClose Acquiring Special Share lock %p EXCL %08lX\n",
539                               &pFcb->NPFcb->Resource,
540                               PsGetCurrentThread()));
541
542                 AFSAcquireExcl( &pFcb->NPFcb->Resource,
543                                 TRUE);
544
545                 RtlZeroMemory( &stPipeClose,
546                                sizeof( AFSPipeOpenCloseRequestCB));
547
548                 stPipeClose.RequestId = pCcb->RequestID;
549
550                 stPipeClose.RootId = pObjectInfo->VolumeCB->ObjectInformation.FileId;
551
552                 //
553                 // Remove the Ccb and de-allocate it
554                 //
555
556                 AFSRemoveCcb( pFcb,
557                               pCcb);
558
559                 //
560                 // If this is not the root then decrement the open child reference count
561                 //
562
563                 if( pParentObjectInfo != NULL &&
564                     pParentObjectInfo->Specific.Directory.ChildOpenReferenceCount > 0)
565                 {
566
567                     lCount = InterlockedDecrement( &pParentObjectInfo->Specific.Directory.ChildOpenReferenceCount);
568
569                     AFSDbgTrace(( AFS_SUBSYSTEM_OBJECT_REF_COUNTING,
570                                   AFS_TRACE_LEVEL_VERBOSE,
571                                   "AFSClose (Share) Decrement child open ref count on Parent object %p Cnt %d\n",
572                                   pParentObjectInfo,
573                                   lCount));
574                 }
575
576                 AFSReleaseResource( &pFcb->NPFcb->Resource);
577
578                 pIrpSp->FileObject->FsContext = NULL;
579
580                 lCount = InterlockedDecrement( &pFcb->OpenReferenceCount);
581
582                 AFSDbgTrace(( AFS_SUBSYSTEM_FCB_REF_COUNTING,
583                               AFS_TRACE_LEVEL_VERBOSE,
584                               "AFSClose (Share) Decrement count on Fcb %p Cnt %d\n",
585                               pFcb,
586                               lCount));
587
588                 ASSERT( lCount >= 0);
589
590                 break;
591             }
592
593             default:
594             {
595
596                 AFSDbgTrace(( AFS_SUBSYSTEM_FILE_PROCESSING,
597                               AFS_TRACE_LEVEL_ERROR,
598                               "AFSClose Processing unknown node type %d\n",
599                               pFcb->Header.NodeTypeCode));
600
601                 break;
602             }
603         }
604
605 try_exit:
606
607         //
608         // Complete the request
609         //
610
611         AFSCompleteRequest( Irp,
612                             ntStatus);
613     }
614     __except( AFSExceptionFilter( __FUNCTION__, GetExceptionCode(), GetExceptionInformation()) )
615     {
616
617         AFSDbgTrace(( 0,
618                       0,
619                       "EXCEPTION - AFSClose\n"));
620
621         AFSDumpTraceFilesFnc();
622     }
623
624     if ( pParentObjectInfo != NULL)
625     {
626
627         AFSReleaseObjectInfo( &pParentObjectInfo);
628     }
629
630     return ntStatus;
631 }