Windows: Allocated VolumeCB from PagedPool
[openafs.git] / src / WINNT / afsrdr / kernel / lib / AFSFcbSupport.cpp
1 /*
2  * Copyright (c) 2008, 2009, 2010, 2011 Kernel Drivers, LLC.
3  * Copyright (c) 2009, 2010, 2011 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,
14  *   this list of conditions and the following disclaimer in the
15  *   documentation
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.
21  *
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.
33  */
34
35 //
36 // File: AFSFcbSupport.cpp
37 //
38
39 #include "AFSCommon.h"
40
41 //
42 // Function: AFSInitFcb
43 //
44 // Description:
45 //
46 //      This function performs Fcb initialization
47 //
48 // Return:
49 //
50 //      Return Fcb->NPFcb->Resource held exclusive
51 //
52
53 NTSTATUS
54 AFSInitFcb( IN AFSDirectoryCB  *DirEntry)
55 {
56
57     NTSTATUS ntStatus = STATUS_SUCCESS;
58     AFSFcb *pFcb = NULL;
59     AFSNonPagedFcb *pNPFcb = NULL;
60     USHORT  usFcbLength = 0;
61     AFSObjectInfoCB *pObjectInfo = NULL, *pParentObjectInfo = NULL;
62     AFSVolumeCB *pVolumeCB = NULL;
63
64     __Enter
65     {
66
67         pObjectInfo = DirEntry->ObjectInformation;
68
69         pParentObjectInfo = pObjectInfo->ParentObjectInformation;
70
71         pVolumeCB = pObjectInfo->VolumeCB;
72
73         if ( pObjectInfo->Fcb != NULL)
74         {
75
76             AFSAcquireExcl( &pObjectInfo->Fcb->NPFcb->Resource,
77                             TRUE);
78
79             try_return( ntStatus = STATUS_SUCCESS);
80         }
81
82         //
83         // Allocate the Fcb and the nonpaged portion of the Fcb.
84         //
85
86         AFSDbgLogMsg( AFS_SUBSYSTEM_FILE_PROCESSING,
87                       AFS_TRACE_LEVEL_VERBOSE_2,
88                       "AFSInitFcb Initializing fcb for %wZ FID %08lX-%08lX-%08lX-%08lX\n",
89                       &DirEntry->NameInformation.FileName,
90                       pObjectInfo->FileId.Cell,
91                       pObjectInfo->FileId.Volume,
92                       pObjectInfo->FileId.Vnode,
93                       pObjectInfo->FileId.Unique);
94
95         usFcbLength = sizeof( AFSFcb);
96
97         pFcb = (AFSFcb *)AFSExAllocatePoolWithTag( PagedPool,
98                                                    usFcbLength,
99                                                    AFS_FCB_ALLOCATION_TAG);
100
101         if( pFcb == NULL)
102         {
103
104             AFSDbgLogMsg( AFS_SUBSYSTEM_FILE_PROCESSING,
105                           AFS_TRACE_LEVEL_ERROR,
106                           "AFSInitFcb Failed to allocate fcb\n");
107
108             try_return( ntStatus = STATUS_INSUFFICIENT_RESOURCES);
109         }
110
111         RtlZeroMemory( pFcb,
112                        usFcbLength);
113
114         pFcb->Header.NodeByteSize = usFcbLength;
115
116         pNPFcb = (AFSNonPagedFcb *)AFSExAllocatePoolWithTag( NonPagedPool,
117                                                              sizeof( AFSNonPagedFcb),
118                                                              AFS_FCB_NP_ALLOCATION_TAG);
119
120         if( pNPFcb == NULL)
121         {
122
123             AFSDbgLogMsg( AFS_SUBSYSTEM_FILE_PROCESSING,
124                           AFS_TRACE_LEVEL_ERROR,
125                           "AFSInitFcb Failed to allocate non-paged fcb\n");
126
127             try_return( ntStatus = STATUS_INSUFFICIENT_RESOURCES);
128         }
129
130         RtlZeroMemory( pNPFcb,
131                        sizeof( AFSNonPagedFcb));
132
133         pNPFcb->Size = sizeof( AFSNonPagedFcb);
134
135         pNPFcb->Type = AFS_NON_PAGED_FCB;
136
137         //
138         // Initialize the advanced header
139         //
140
141         ExInitializeFastMutex( &pNPFcb->AdvancedHdrMutex);
142
143         FsRtlSetupAdvancedHeader( &pFcb->Header, &pNPFcb->AdvancedHdrMutex);
144
145         //
146         // OK, initialize the entry
147         //
148
149         ExInitializeResourceLite( &pNPFcb->Resource);
150
151         ExInitializeResourceLite( &pNPFcb->PagingResource);
152
153         ExInitializeResourceLite( &pNPFcb->SectionObjectResource);
154
155         ExInitializeResourceLite( &pNPFcb->CcbListLock);
156
157         pFcb->Header.Resource = &pNPFcb->Resource;
158
159         pFcb->Header.PagingIoResource = &pNPFcb->PagingResource;
160
161         //
162         // Grab the Fcb for processing
163         //
164
165         AFSDbgLogMsg( AFS_SUBSYSTEM_LOCK_PROCESSING,
166                       AFS_TRACE_LEVEL_VERBOSE,
167                       "AFSInitFcb Acquiring Fcb lock %p EXCL %08lX\n",
168                       &pNPFcb->Resource,
169                       PsGetCurrentThread());
170
171         AFSAcquireExcl( &pNPFcb->Resource,
172                         TRUE);
173
174         pFcb->NPFcb = pNPFcb;
175
176         //
177         // Set type specific information
178         //
179
180         if( pObjectInfo->FileType == AFS_FILE_TYPE_DIRECTORY)
181         {
182
183             //
184             // Reset the type to a directory type
185             //
186
187             pFcb->Header.NodeTypeCode = AFS_DIRECTORY_FCB;
188         }
189         else if( pObjectInfo->FileType == AFS_FILE_TYPE_FILE)
190         {
191
192             pFcb->Header.NodeTypeCode = AFS_FILE_FCB;
193
194             //
195             // Initialize the file specific information
196             //
197
198             FsRtlInitializeFileLock( &pFcb->Specific.File.FileLock,
199                                      NULL,
200                                      NULL);
201
202             //
203             // Initialize the header file sizes to our dir entry information
204             //
205
206             pFcb->Header.AllocationSize.QuadPart = pObjectInfo->AllocationSize.QuadPart;
207             pFcb->Header.FileSize.QuadPart = pObjectInfo->EndOfFile.QuadPart;
208             pFcb->Header.ValidDataLength.QuadPart = pObjectInfo->EndOfFile.QuadPart;
209
210             //
211             // Initialize the Extents resources and so forth.  The
212             // quiescent state is that no one has the extents for
213             // IO (do the extents are not busy) and there is no
214             // extents request outstanding (and hence the "last
215             // one" is complete).
216             //
217             ExInitializeResourceLite( &pNPFcb->Specific.File.ExtentsResource );
218
219             KeInitializeEvent( &pNPFcb->Specific.File.ExtentsRequestComplete,
220                                NotificationEvent,
221                                TRUE );
222
223             for (ULONG i = 0; i < AFS_NUM_EXTENT_LISTS; i++)
224             {
225                 InitializeListHead(&pFcb->Specific.File.ExtentsLists[i]);
226             }
227
228             pNPFcb->Specific.File.DirtyListHead = NULL;
229             pNPFcb->Specific.File.DirtyListTail = NULL;
230
231             ExInitializeResourceLite( &pNPFcb->Specific.File.DirtyExtentsListLock);
232
233             KeInitializeEvent( &pNPFcb->Specific.File.FlushEvent,
234                                SynchronizationEvent,
235                                TRUE);
236
237             KeInitializeEvent( &pNPFcb->Specific.File.QueuedFlushEvent,
238                                NotificationEvent,
239                                TRUE);
240         }
241         else if( pObjectInfo->FileType == AFS_FILE_TYPE_SPECIAL_SHARE_NAME)
242         {
243
244             pFcb->Header.NodeTypeCode = AFS_SPECIAL_SHARE_FCB;
245         }
246         else if( pObjectInfo->FileType == AFS_FILE_TYPE_PIOCTL)
247         {
248
249             pFcb->Header.NodeTypeCode = AFS_IOCTL_FCB;
250         }
251         else if( pObjectInfo->FileType == AFS_FILE_TYPE_SYMLINK)
252         {
253
254             pFcb->Header.NodeTypeCode = AFS_SYMBOLIC_LINK_FCB;
255         }
256         else if( pObjectInfo->FileType == AFS_FILE_TYPE_MOUNTPOINT)
257         {
258
259             pFcb->Header.NodeTypeCode = AFS_MOUNT_POINT_FCB;
260         }
261         else if( pObjectInfo->FileType == AFS_FILE_TYPE_DFSLINK)
262         {
263             pFcb->Header.NodeTypeCode = AFS_DFS_LINK_FCB;
264         }
265         else
266         {
267             pFcb->Header.NodeTypeCode = AFS_INVALID_FCB;
268         }
269
270         pFcb->ObjectInformation = pObjectInfo;
271
272         AFSAcquireShared( &pObjectInfo->NonPagedInfo->ObjectInfoLock,
273                           TRUE);
274         //
275         // Swap the allocated FCB into the ObjectInformation structure if it
276         // does not already have one.
277         //
278
279         if ( InterlockedCompareExchangePointer( (PVOID *)&pObjectInfo->Fcb, pFcb, NULL) != NULL)
280         {
281
282             AFSDbgLogMsg( AFS_SUBSYSTEM_FILE_PROCESSING,
283                           AFS_TRACE_LEVEL_WARNING,
284                           "AFSInitFcb Raced Fcb %p pFcb %p Name %wZ\n",
285                           pObjectInfo->Fcb,
286                           pFcb,
287                           &DirEntry->NameInformation.FileName);
288
289             AFSDbgLogMsg( AFS_SUBSYSTEM_LOCK_PROCESSING,
290                           AFS_TRACE_LEVEL_VERBOSE,
291                           "AFSInitFcb Acquiring Fcb lock %p EXCL %08lX\n",
292                           &pObjectInfo->Fcb->NPFcb->Resource,
293                           PsGetCurrentThread());
294
295             AFSReleaseResource( &pObjectInfo->NonPagedInfo->ObjectInfoLock);
296
297             AFSAcquireExcl( &pObjectInfo->Fcb->NPFcb->Resource,
298                             TRUE);
299
300             try_return( ntStatus = STATUS_REPARSE);
301         }
302
303         AFSReleaseResource( &pObjectInfo->NonPagedInfo->ObjectInfoLock);
304
305         AFSDbgLogMsg( AFS_SUBSYSTEM_FCB_REF_COUNTING,
306                       AFS_TRACE_LEVEL_VERBOSE,
307                       "AFSInitFcb Initialized Fcb %p Name %wZ\n",
308                       &pObjectInfo->Fcb,
309                       &DirEntry->NameInformation.FileName);
310
311 try_exit:
312
313         if( ntStatus != STATUS_SUCCESS)
314         {
315
316             if ( !NT_SUCCESS( ntStatus))
317             {
318
319                 AFSDbgLogMsg( AFS_SUBSYSTEM_FILE_PROCESSING,
320                               AFS_TRACE_LEVEL_ERROR,
321                               "AFSInitFcb Failed to initialize fcb Status %08lX\n",
322                               ntStatus);
323             }
324
325             if( pFcb != NULL)
326             {
327
328                 if( pNPFcb != NULL)
329                 {
330
331                     AFSReleaseResource( &pNPFcb->Resource);
332
333                     FsRtlTeardownPerStreamContexts( &pFcb->Header);
334
335                     if ( pObjectInfo->FileType == AFS_FILE_TYPE_FILE)
336                     {
337
338                         FsRtlUninitializeFileLock( &pFcb->Specific.File.FileLock);
339
340                         ExDeleteResourceLite( &pNPFcb->Specific.File.ExtentsResource);
341
342                         ExDeleteResourceLite( &pNPFcb->Specific.File.DirtyExtentsListLock);
343                     }
344
345                     ExDeleteResourceLite( &pNPFcb->SectionObjectResource);
346
347                     ExDeleteResourceLite( &pNPFcb->PagingResource);
348
349                     ExDeleteResourceLite( &pNPFcb->CcbListLock);
350
351                     ExDeleteResourceLite( &pNPFcb->Resource);
352
353                     AFSExFreePoolWithTag( pNPFcb, AFS_FCB_NP_ALLOCATION_TAG);
354                 }
355
356                 AFSExFreePoolWithTag( pFcb, AFS_FCB_ALLOCATION_TAG);
357             }
358         }
359     }
360
361     return ntStatus;
362 }
363
364 NTSTATUS
365 AFSInitVolume( IN GUID *AuthGroup,
366                IN AFSFileID *RootFid,
367                OUT AFSVolumeCB **VolumeCB)
368 {
369
370     NTSTATUS ntStatus = STATUS_SUCCESS;
371     AFSDeviceExt *pDeviceExt = (AFSDeviceExt *)AFSRDRDeviceObject->DeviceExtension;
372     AFSNonPagedVolumeCB *pNonPagedVcb = NULL;
373     AFSVolumeCB *pVolumeCB = NULL;
374     AFSNonPagedObjectInfoCB *pNonPagedObject = NULL;
375     ULONGLONG ullIndex = 0;
376     BOOLEAN bReleaseLocks = FALSE;
377     AFSVolumeInfoCB stVolumeInformation = {0};
378     AFSNonPagedDirectoryCB *pNonPagedDirEntry = NULL;
379     LONG lCount;
380
381     __Enter
382     {
383
384         //
385         // Before grabbing any locks ask the service for the volume information
386         // This may be a waste but we need to get this information prior to
387         // taking any volume tree locks. Don't do this for any 'reserved' cell entries
388         //
389
390         if( RootFid->Cell != 0)
391         {
392
393             RtlZeroMemory( &stVolumeInformation,
394                            sizeof( AFSVolumeInfoCB));
395
396             ntStatus = AFSRetrieveVolumeInformation( AuthGroup,
397                                                      RootFid,
398                                                      &stVolumeInformation);
399
400             if( !NT_SUCCESS( ntStatus))
401             {
402
403                 AFSDbgLogMsg( AFS_SUBSYSTEM_LOAD_LIBRARY | AFS_SUBSYSTEM_INIT_PROCESSING,
404                               AFS_TRACE_LEVEL_ERROR,
405                               "AFSInitVolume AFSRetrieveVolumeInformation(RootFid) failure %08lX\n",
406                               ntStatus);
407
408                 try_return( ntStatus);
409             }
410
411             //
412             // Grab our tree locks and see if we raced with someone else
413             //
414
415             AFSAcquireExcl( pDeviceExt->Specific.RDR.VolumeTree.TreeLock,
416                             TRUE);
417
418             AFSAcquireExcl( &pDeviceExt->Specific.RDR.VolumeListLock,
419                             TRUE);
420
421             bReleaseLocks = TRUE;
422
423             ullIndex = AFSCreateHighIndex( RootFid);
424
425             ntStatus = AFSLocateHashEntry( pDeviceExt->Specific.RDR.VolumeTree.TreeHead,
426                                            ullIndex,
427                                            (AFSBTreeEntry **)&pVolumeCB);
428
429             if( NT_SUCCESS( ntStatus) &&
430                 pVolumeCB != NULL)
431             {
432
433                 //
434                 // So we don't lock with an invalidation call ...
435                 //
436
437                 lCount = InterlockedIncrement( &pVolumeCB->VolumeReferenceCount);
438
439                 AFSDbgLogMsg( AFS_SUBSYSTEM_VOLUME_REF_COUNTING,
440                               AFS_TRACE_LEVEL_VERBOSE,
441                               "AFSInitVolume Increment count on volume %p Cnt %d\n",
442                               pVolumeCB,
443                               lCount);
444
445                 AFSReleaseResource( pDeviceExt->Specific.RDR.VolumeTree.TreeLock);
446
447                 AFSReleaseResource( &pDeviceExt->Specific.RDR.VolumeListLock);
448
449                 bReleaseLocks = FALSE;
450
451                 AFSAcquireExcl( pVolumeCB->VolumeLock,
452                                 TRUE);
453
454                 *VolumeCB = pVolumeCB;
455
456                 try_return( ntStatus);
457             }
458
459             //
460             // Revert our status from the above call back to success.
461             //
462
463             ntStatus = STATUS_SUCCESS;
464         }
465
466         //
467         // For the global root we allocate out volume node and insert it
468         // into the volume tree ...
469         //
470
471         pVolumeCB = (AFSVolumeCB *)AFSExAllocatePoolWithTag( PagedPool,
472                                                              sizeof( AFSVolumeCB),
473                                                              AFS_VCB_ALLOCATION_TAG);
474
475         if( pVolumeCB == NULL)
476         {
477
478             AFSDbgLogMsg( AFS_SUBSYSTEM_FILE_PROCESSING,
479                           AFS_TRACE_LEVEL_ERROR,
480                           "AFSInitVolume Failed to allocate the root volume cb\n");
481
482             try_return( ntStatus = STATUS_INSUFFICIENT_RESOURCES);
483         }
484
485         RtlZeroMemory( pVolumeCB,
486                        sizeof( AFSVolumeCB));
487
488         //
489         // The non paged portion
490         //
491
492         pNonPagedVcb = (AFSNonPagedVolumeCB *)AFSExAllocatePoolWithTag( NonPagedPool,
493                                                                         sizeof( AFSNonPagedVolumeCB),
494                                                                         AFS_VCB_NP_ALLOCATION_TAG);
495
496         if( pNonPagedVcb == NULL)
497         {
498
499             AFSDbgLogMsg( AFS_SUBSYSTEM_FILE_PROCESSING,
500                           AFS_TRACE_LEVEL_ERROR,
501                           "AFSInitVolume Failed to allocate the root non paged volume cb\n");
502
503             try_return( ntStatus = STATUS_INSUFFICIENT_RESOURCES);
504         }
505
506         RtlZeroMemory( pNonPagedVcb,
507                        sizeof( AFSNonPagedVolumeCB));
508
509         ExInitializeResourceLite( &pNonPagedVcb->VolumeLock);
510
511         ExInitializeResourceLite( &pNonPagedVcb->ObjectInfoTreeLock);
512
513         pNonPagedObject = (AFSNonPagedObjectInfoCB *)AFSExAllocatePoolWithTag( NonPagedPool,
514                                                                                sizeof( AFSNonPagedObjectInfoCB),
515                                                                                AFS_NP_OBJECT_INFO_TAG);
516
517         if( pNonPagedObject == NULL)
518         {
519
520             AFSDbgLogMsg( AFS_SUBSYSTEM_FILE_PROCESSING,
521                           AFS_TRACE_LEVEL_ERROR,
522                           "AFSInitVolume Failed to allocate the root non paged object cb\n");
523
524             try_return( ntStatus = STATUS_INSUFFICIENT_RESOURCES);
525         }
526
527         RtlZeroMemory( pNonPagedObject,
528                        sizeof( AFSNonPagedObjectInfoCB));
529
530         ExInitializeResourceLite( &pNonPagedObject->ObjectInfoLock);
531
532         ExInitializeResourceLite( &pNonPagedObject->DirectoryNodeHdrLock);
533
534         pVolumeCB->NonPagedVcb = pNonPagedVcb;
535
536         pVolumeCB->ObjectInformation.NonPagedInfo = pNonPagedObject;
537
538         pVolumeCB->VolumeLock = &pNonPagedVcb->VolumeLock;
539
540         pVolumeCB->ObjectInformation.Specific.Directory.DirectoryNodeHdr.TreeLock = &pNonPagedObject->DirectoryNodeHdrLock;
541
542         pVolumeCB->ObjectInfoTree.TreeLock = &pNonPagedVcb->ObjectInfoTreeLock;
543
544         //
545         // Bias our reference by 1
546         //
547
548         AFSDbgLogMsg( AFS_SUBSYSTEM_VOLUME_REF_COUNTING,
549                       AFS_TRACE_LEVEL_VERBOSE,
550                       "AFSInitVolume Initializing count (2) on volume %p\n",
551                       pVolumeCB);
552
553         pVolumeCB->VolumeReferenceCount = 2;
554
555         AFSAcquireExcl( pVolumeCB->VolumeLock,
556                         TRUE);
557
558         pVolumeCB->DirectoryCB = (AFSDirectoryCB *)AFSExAllocatePoolWithTag( PagedPool,
559                                                                              sizeof( AFSDirectoryCB) + sizeof( WCHAR),
560                                                                              AFS_DIR_ENTRY_TAG);
561
562         if( pVolumeCB->DirectoryCB == NULL)
563         {
564
565             try_return( ntStatus = STATUS_INSUFFICIENT_RESOURCES);
566         }
567
568         pNonPagedDirEntry = (AFSNonPagedDirectoryCB *)AFSExAllocatePoolWithTag( NonPagedPool,
569                                                                                 sizeof( AFSNonPagedDirectoryCB),
570                                                                                 AFS_DIR_ENTRY_NP_TAG);
571
572         if( pNonPagedDirEntry == NULL)
573         {
574
575             try_return( ntStatus = STATUS_INSUFFICIENT_RESOURCES);
576         }
577
578         RtlZeroMemory( pVolumeCB->DirectoryCB,
579                        sizeof( AFSDirectoryCB) + sizeof( WCHAR));
580
581         RtlZeroMemory( pNonPagedDirEntry,
582                        sizeof( AFSNonPagedDirectoryCB));
583
584         ExInitializeResourceLite( &pNonPagedDirEntry->Lock);
585
586         pVolumeCB->DirectoryCB->NonPaged = pNonPagedDirEntry;
587
588         //
589         // Initialize the non-paged portion of the directory entry
590         //
591
592         KeQuerySystemTime( &pVolumeCB->ObjectInformation.CreationTime);
593         KeQuerySystemTime( &pVolumeCB->ObjectInformation.LastWriteTime);
594         KeQuerySystemTime( &pVolumeCB->ObjectInformation.LastAccessTime);
595
596         pVolumeCB->ObjectInformation.FileType = AFS_FILE_TYPE_DIRECTORY;
597
598         SetFlag( pVolumeCB->ObjectInformation.Flags, AFS_OBJECT_ROOT_VOLUME);
599
600         pVolumeCB->ObjectInformation.FileId.Cell = RootFid->Cell;
601         pVolumeCB->ObjectInformation.FileId.Volume = RootFid->Volume;
602         pVolumeCB->ObjectInformation.FileId.Vnode = RootFid->Vnode;
603         pVolumeCB->ObjectInformation.FileId.Unique = RootFid->Unique;
604         pVolumeCB->ObjectInformation.FileId.Hash = RootFid->Hash;
605
606         pVolumeCB->ObjectInformation.FileAttributes = FILE_ATTRIBUTE_DIRECTORY;
607
608         pVolumeCB->DirectoryCB->NameInformation.FileName.Length = sizeof( WCHAR);
609
610         pVolumeCB->DirectoryCB->NameInformation.FileName.MaximumLength = pVolumeCB->DirectoryCB->NameInformation.FileName.Length;
611
612         pVolumeCB->DirectoryCB->NameInformation.FileName.Buffer = (WCHAR *)((char *)pVolumeCB->DirectoryCB + sizeof( AFSDirectoryCB));
613
614         RtlCopyMemory( pVolumeCB->DirectoryCB->NameInformation.FileName.Buffer,
615                        L"\\",
616                        sizeof( WCHAR));
617
618         //
619         // Copy in the volume information retrieved above
620         //
621
622         RtlCopyMemory( &pVolumeCB->VolumeInformation,
623                        &stVolumeInformation,
624                        sizeof( AFSVolumeInfoCB));
625
626         //
627         // Setup pointers
628         //
629
630         pVolumeCB->DirectoryCB->ObjectInformation = &pVolumeCB->ObjectInformation;
631
632         pVolumeCB->DirectoryCB->ObjectInformation->VolumeCB = pVolumeCB;
633
634         //
635         // Insert the volume into our volume tree. Don't insert any reserved entries
636         //
637
638         if( RootFid->Cell != 0)
639         {
640
641             pVolumeCB->TreeEntry.HashIndex = ullIndex;
642
643             if( pDeviceExt->Specific.RDR.VolumeTree.TreeHead == NULL)
644             {
645
646                 pDeviceExt->Specific.RDR.VolumeTree.TreeHead = &pVolumeCB->TreeEntry;
647
648                 SetFlag( pVolumeCB->Flags, AFS_VOLUME_INSERTED_HASH_TREE);
649             }
650             else
651             {
652
653                 if ( NT_SUCCESS( AFSInsertHashEntry( pDeviceExt->Specific.RDR.VolumeTree.TreeHead,
654                                                      &pVolumeCB->TreeEntry)))
655                 {
656
657                     SetFlag( pVolumeCB->Flags, AFS_VOLUME_INSERTED_HASH_TREE);
658                 }
659             }
660
661             if( pDeviceExt->Specific.RDR.VolumeListHead == NULL)
662             {
663
664                 pDeviceExt->Specific.RDR.VolumeListHead = pVolumeCB;
665             }
666             else
667             {
668
669                 pDeviceExt->Specific.RDR.VolumeListTail->ListEntry.fLink = (void *)pVolumeCB;
670
671                 pVolumeCB->ListEntry.bLink = pDeviceExt->Specific.RDR.VolumeListTail;
672             }
673
674             pDeviceExt->Specific.RDR.VolumeListTail = pVolumeCB;
675         }
676
677         *VolumeCB = pVolumeCB;
678
679 try_exit:
680
681         if( !NT_SUCCESS( ntStatus))
682         {
683
684             if( pNonPagedVcb != NULL)
685             {
686
687                 AFSReleaseResource( pVolumeCB->VolumeLock);
688
689                 ExDeleteResourceLite( &pNonPagedVcb->VolumeLock);
690
691                 ExDeleteResourceLite( &pNonPagedVcb->ObjectInfoTreeLock);
692
693                 AFSExFreePoolWithTag( pNonPagedVcb, AFS_VCB_NP_ALLOCATION_TAG);
694             }
695
696             if( pNonPagedObject != NULL)
697             {
698
699                 ExDeleteResourceLite( &pNonPagedObject->ObjectInfoLock);
700
701                 AFSExFreePoolWithTag( pNonPagedObject, AFS_NP_OBJECT_INFO_TAG);
702             }
703
704             if( pVolumeCB != NULL)
705             {
706
707                 if( pVolumeCB->DirectoryCB != NULL)
708                 {
709
710                     AFSExFreePoolWithTag( pVolumeCB->DirectoryCB, AFS_DIR_ENTRY_TAG);
711                 }
712
713                 AFSExFreePoolWithTag( pVolumeCB, AFS_VCB_ALLOCATION_TAG);
714             }
715
716             if( pNonPagedDirEntry != NULL)
717             {
718
719                 ExDeleteResourceLite( &pNonPagedDirEntry->Lock);
720
721                 AFSExFreePoolWithTag( pNonPagedDirEntry, AFS_DIR_ENTRY_NP_TAG);
722             }
723         }
724
725         if( bReleaseLocks)
726         {
727
728             AFSReleaseResource( pDeviceExt->Specific.RDR.VolumeTree.TreeLock);
729
730             AFSReleaseResource( &pDeviceExt->Specific.RDR.VolumeListLock);
731         }
732     }
733
734     return ntStatus;
735 }
736
737 NTSTATUS
738 AFSRemoveVolume( IN AFSVolumeCB *VolumeCB)
739 {
740
741     NTSTATUS ntStatus = STATUS_SUCCESS;
742     AFSDeviceExt *pDeviceExt = (AFSDeviceExt *)AFSRDRDeviceObject->DeviceExtension;
743
744     __Enter
745     {
746
747         //
748         // Remove the volume from the tree and list
749         // Don't process the list information for reserved entries
750         //
751
752         if( VolumeCB->ObjectInformation.FileId.Cell != 0)
753         {
754
755             if( BooleanFlagOn( VolumeCB->Flags, AFS_VOLUME_INSERTED_HASH_TREE))
756             {
757
758                 AFSRemoveHashEntry( &pDeviceExt->Specific.RDR.VolumeTree.TreeHead,
759                                     &VolumeCB->TreeEntry);
760             }
761
762             if( VolumeCB->ListEntry.fLink == NULL)
763             {
764
765                 pDeviceExt->Specific.RDR.VolumeListTail = (AFSVolumeCB *)VolumeCB->ListEntry.bLink;
766
767                 if( pDeviceExt->Specific.RDR.VolumeListTail != NULL)
768                 {
769
770                     pDeviceExt->Specific.RDR.VolumeListTail->ListEntry.fLink = NULL;
771                 }
772             }
773             else
774             {
775
776                 ((AFSVolumeCB *)(VolumeCB->ListEntry.fLink))->ListEntry.bLink = VolumeCB->ListEntry.bLink;
777             }
778
779             if( VolumeCB->ListEntry.bLink == NULL)
780             {
781
782                 pDeviceExt->Specific.RDR.VolumeListHead = (AFSVolumeCB *)VolumeCB->ListEntry.fLink;
783
784                 if( pDeviceExt->Specific.RDR.VolumeListHead != NULL)
785                 {
786
787                     pDeviceExt->Specific.RDR.VolumeListHead->ListEntry.bLink = NULL;
788                 }
789             }
790             else
791             {
792
793                 ((AFSVolumeCB *)(VolumeCB->ListEntry.bLink))->ListEntry.fLink = VolumeCB->ListEntry.fLink;
794             }
795         }
796
797         //
798         // Remove any PIOctl objects we have
799         //
800
801         if( VolumeCB->ObjectInformation.Specific.Directory.PIOCtlDirectoryCB != NULL)
802         {
803
804             if( VolumeCB->ObjectInformation.Specific.Directory.PIOCtlDirectoryCB->ObjectInformation->Fcb != NULL)
805             {
806
807                 AFSAcquireExcl( &VolumeCB->ObjectInformation.Specific.Directory.PIOCtlDirectoryCB->ObjectInformation->NonPagedInfo->ObjectInfoLock,
808                                 TRUE);
809
810                 AFSRemoveFcb( &VolumeCB->ObjectInformation.Specific.Directory.PIOCtlDirectoryCB->ObjectInformation->Fcb);
811
812                 AFSReleaseResource( &VolumeCB->ObjectInformation.Specific.Directory.PIOCtlDirectoryCB->ObjectInformation->NonPagedInfo->ObjectInfoLock);
813             }
814
815             AFSDeleteObjectInfo( VolumeCB->ObjectInformation.Specific.Directory.PIOCtlDirectoryCB->ObjectInformation);
816
817             ExDeleteResourceLite( &VolumeCB->ObjectInformation.Specific.Directory.PIOCtlDirectoryCB->NonPaged->Lock);
818
819             AFSExFreePoolWithTag( VolumeCB->ObjectInformation.Specific.Directory.PIOCtlDirectoryCB->NonPaged, AFS_DIR_ENTRY_NP_TAG);
820
821             AFSExFreePoolWithTag( VolumeCB->ObjectInformation.Specific.Directory.PIOCtlDirectoryCB, AFS_DIR_ENTRY_TAG);
822         }
823
824         if( BooleanFlagOn( VolumeCB->ObjectInformation.Flags, AFS_OBJECT_HELD_IN_SERVICE))
825         {
826
827             //
828             // Release the fid in the service
829             //
830
831             AFSReleaseFid( &VolumeCB->ObjectInformation.FileId);
832         }
833
834         //
835         // Free up the memory
836         //
837
838         if( VolumeCB->NonPagedVcb != NULL)
839         {
840
841             if( ExIsResourceAcquiredLite( VolumeCB->VolumeLock))
842             {
843                 AFSReleaseResource( VolumeCB->VolumeLock);
844             }
845
846             ExDeleteResourceLite( &VolumeCB->NonPagedVcb->VolumeLock);
847
848             ExDeleteResourceLite( &VolumeCB->NonPagedVcb->ObjectInfoTreeLock);
849
850             AFSExFreePoolWithTag( VolumeCB->NonPagedVcb, AFS_VCB_NP_ALLOCATION_TAG);
851         }
852
853         if( VolumeCB->ObjectInformation.NonPagedInfo != NULL)
854         {
855
856             ExDeleteResourceLite( &VolumeCB->ObjectInformation.NonPagedInfo->ObjectInfoLock);
857
858             ExDeleteResourceLite( &VolumeCB->ObjectInformation.NonPagedInfo->DirectoryNodeHdrLock);
859
860             AFSExFreePoolWithTag( VolumeCB->ObjectInformation.NonPagedInfo, AFS_NP_OBJECT_INFO_TAG);
861         }
862
863         if( VolumeCB->DirectoryCB != NULL)
864         {
865
866             if( VolumeCB->DirectoryCB->NonPaged != NULL)
867             {
868
869                 ExDeleteResourceLite( &VolumeCB->DirectoryCB->NonPaged->Lock);
870
871                 AFSExFreePoolWithTag( VolumeCB->DirectoryCB->NonPaged, AFS_DIR_ENTRY_NP_TAG);
872             }
873
874             AFSExFreePoolWithTag( VolumeCB->DirectoryCB, AFS_DIR_ENTRY_TAG);
875         }
876
877         AFSExFreePoolWithTag( VolumeCB, AFS_VCB_ALLOCATION_TAG);
878     }
879
880     return ntStatus;
881 }
882
883
884 //
885 // Function: AFSInitRootFcb
886 //
887 // Description:
888 //
889 //      This function performs Root node Fcb initialization
890 //
891 // Return:
892 //
893 //      A status is returned for the function
894 //
895
896 NTSTATUS
897 AFSInitRootFcb( IN ULONGLONG ProcessID,
898                 IN AFSVolumeCB *VolumeCB)
899 {
900
901     UNREFERENCED_PARAMETER(ProcessID);
902     NTSTATUS ntStatus = STATUS_SUCCESS;
903     AFSFcb *pFcb = NULL;
904     AFSNonPagedFcb *pNPFcb = NULL;
905
906     __Enter
907     {
908
909         //
910         // Initialize the root fcb
911         //
912
913         pFcb = (AFSFcb *)AFSExAllocatePoolWithTag( PagedPool,
914                                                    sizeof( AFSFcb),
915                                                    AFS_FCB_ALLOCATION_TAG);
916
917         if( pFcb == NULL)
918         {
919
920             AFSDbgLogMsg( AFS_SUBSYSTEM_FILE_PROCESSING,
921                           AFS_TRACE_LEVEL_ERROR,
922                           "AFSInitRootFcb Failed to allocate the root fcb\n");
923
924             try_return( ntStatus = STATUS_INSUFFICIENT_RESOURCES);
925         }
926
927         RtlZeroMemory( pFcb,
928                        sizeof( AFSFcb));
929
930         pFcb->Header.NodeByteSize = sizeof( AFSFcb);
931         pFcb->Header.NodeTypeCode = AFS_ROOT_FCB;
932
933         pNPFcb = (AFSNonPagedFcb *)AFSExAllocatePoolWithTag( NonPagedPool,
934                                                              sizeof( AFSNonPagedFcb),
935                                                              AFS_FCB_NP_ALLOCATION_TAG);
936
937         if( pNPFcb == NULL)
938         {
939
940             AFSDbgLogMsg( AFS_SUBSYSTEM_FILE_PROCESSING,
941                           AFS_TRACE_LEVEL_ERROR,
942                           "AFSInitRootFcb Failed to allocate the non-paged fcb\n");
943
944             try_return( ntStatus = STATUS_INSUFFICIENT_RESOURCES);
945         }
946
947         RtlZeroMemory( pNPFcb,
948                        sizeof( AFSNonPagedFcb));
949
950         pNPFcb->Size = sizeof( AFSNonPagedFcb);
951         pNPFcb->Type = AFS_NON_PAGED_FCB;
952
953         //
954         // OK, initialize the entry
955         //
956
957         ExInitializeFastMutex( &pNPFcb->AdvancedHdrMutex);
958
959         FsRtlSetupAdvancedHeader( &pFcb->Header, &pNPFcb->AdvancedHdrMutex);
960
961         ExInitializeResourceLite( &pNPFcb->Resource);
962
963         AFSDbgLogMsg( AFS_SUBSYSTEM_LOCK_PROCESSING,
964                       AFS_TRACE_LEVEL_VERBOSE,
965                       "AFSInitRootFcb Acquiring Fcb lock %p EXCL %08lX\n",
966                       &pNPFcb->Resource,
967                       PsGetCurrentThread());
968
969         AFSAcquireExcl( &pNPFcb->Resource,
970                         TRUE);
971
972         ExInitializeResourceLite( &pNPFcb->PagingResource);
973
974         ExInitializeResourceLite( &pNPFcb->CcbListLock);
975
976         pFcb->Header.Resource = &pNPFcb->Resource;
977
978         pFcb->Header.PagingIoResource = &pNPFcb->PagingResource;
979
980         pFcb->NPFcb = pNPFcb;
981
982         //
983         // Save the root Fcb in the VolumeCB
984         //
985
986         VolumeCB->ObjectInformation.Fcb = pFcb;
987
988         VolumeCB->ObjectInformation.VolumeCB = VolumeCB;
989
990         VolumeCB->RootFcb = pFcb;
991
992         pFcb->ObjectInformation = &VolumeCB->ObjectInformation;
993
994 try_exit:
995
996         if( !NT_SUCCESS( ntStatus))
997         {
998
999             if( pFcb != NULL)
1000             {
1001
1002                 AFSRemoveRootFcb( pFcb);
1003             }
1004         }
1005     }
1006
1007     return ntStatus;
1008 }
1009
1010 //
1011 // Function: AFSRemoveRootFcb
1012 //
1013 // Description:
1014 //
1015 //      This function performs root Fcb removal/deallocation
1016 //
1017 // Return:
1018 //
1019 //      A status is returned for the function
1020 //
1021
1022 void
1023 AFSRemoveRootFcb( IN AFSFcb *RootFcb)
1024 {
1025
1026     if( RootFcb->NPFcb != NULL)
1027     {
1028
1029         //
1030         // Now the resource
1031         //
1032
1033         ExDeleteResourceLite( &RootFcb->NPFcb->Resource);
1034
1035         ExDeleteResourceLite( &RootFcb->NPFcb->PagingResource);
1036
1037         ExDeleteResourceLite( &RootFcb->NPFcb->CcbListLock);
1038
1039         //
1040         // The non paged region
1041         //
1042
1043         AFSExFreePoolWithTag( RootFcb->NPFcb, AFS_FCB_NP_ALLOCATION_TAG);
1044     }
1045
1046     //
1047     // And the Fcb itself
1048     //
1049
1050     AFSExFreePoolWithTag( RootFcb, AFS_FCB_ALLOCATION_TAG);
1051
1052     return;
1053 }
1054
1055 //
1056 // Function: AFSRemoveFcb
1057 //
1058 // Description:
1059 //
1060 //      This function performs Fcb removal/deallocation
1061 //
1062 // Return:
1063 //
1064 //      A status is returned for the function
1065 //
1066
1067 void
1068 AFSRemoveFcb( IN AFSFcb **ppFcb)
1069 {
1070
1071     AFSFcb * pFcb;
1072
1073     pFcb = (AFSFcb *) InterlockedCompareExchangePointer( (PVOID *)ppFcb, NULL, (PVOID)(*ppFcb));
1074
1075     if ( pFcb == NULL)
1076     {
1077
1078         return;
1079     }
1080
1081     //
1082     // Uninitialize the file lock if it is a file
1083     //
1084
1085     AFSDbgLogMsg( AFS_SUBSYSTEM_FILE_PROCESSING,
1086                   AFS_TRACE_LEVEL_VERBOSE,
1087                   "AFSRemoveFcb Removing Fcb %p\n",
1088                   pFcb);
1089
1090     if( pFcb->Header.NodeTypeCode == AFS_FILE_FCB)
1091     {
1092
1093         FsRtlUninitializeFileLock( &pFcb->Specific.File.FileLock);
1094
1095         //
1096         // The resource we allocated
1097         //
1098
1099         ExDeleteResourceLite( &pFcb->NPFcb->Specific.File.ExtentsResource );
1100
1101         ExDeleteResourceLite( &pFcb->NPFcb->Specific.File.DirtyExtentsListLock);
1102
1103     }
1104     else if( pFcb->Header.NodeTypeCode == AFS_DIRECTORY_FCB)
1105     {
1106
1107
1108     }
1109
1110     //
1111     // Tear down the FM specific contexts
1112     //
1113
1114     FsRtlTeardownPerStreamContexts( &pFcb->Header);
1115
1116     //
1117     // Delete the resources
1118     //
1119
1120     ExDeleteResourceLite( &pFcb->NPFcb->Resource);
1121
1122     ExDeleteResourceLite( &pFcb->NPFcb->PagingResource);
1123
1124     ExDeleteResourceLite( &pFcb->NPFcb->SectionObjectResource);
1125
1126     ExDeleteResourceLite( &pFcb->NPFcb->CcbListLock);
1127
1128     //
1129     // The non paged region
1130     //
1131
1132     AFSExFreePoolWithTag( pFcb->NPFcb, AFS_FCB_NP_ALLOCATION_TAG);
1133
1134     //
1135     // And the Fcb itself, which includes the name
1136     //
1137
1138     AFSExFreePoolWithTag( pFcb, AFS_FCB_ALLOCATION_TAG);
1139
1140     return;
1141 }
1142
1143 NTSTATUS
1144 AFSInitCcb( IN OUT AFSCcb **Ccb,
1145             IN     AFSDirectoryCB *DirectoryCB,
1146             IN     ACCESS_MASK     GrantedAccess,
1147             IN     ULONG           FileAccess)
1148 {
1149
1150     NTSTATUS Status = STATUS_SUCCESS;
1151     AFSCcb *pCcb = NULL;
1152     LONG lCount;
1153
1154     __Enter
1155     {
1156
1157         //
1158         // Allocate our context control block
1159         //
1160
1161         pCcb = (AFSCcb *)AFSExAllocatePoolWithTag( PagedPool,
1162                                                    sizeof( AFSCcb),
1163                                                    AFS_CCB_ALLOCATION_TAG);
1164
1165         if( pCcb == NULL)
1166         {
1167
1168             AFSDbgLogMsg( AFS_SUBSYSTEM_FILE_PROCESSING,
1169                           AFS_TRACE_LEVEL_ERROR,
1170                           "AFSInitCcb Failed to allocate Ccb\n");
1171
1172             try_return( Status = STATUS_INSUFFICIENT_RESOURCES);
1173         }
1174
1175         RtlZeroMemory( pCcb,
1176                        sizeof( AFSCcb));
1177
1178         pCcb->Size = sizeof( AFSCcb);
1179
1180         pCcb->Type = AFS_CCB;
1181
1182         pCcb->NPCcb = (AFSNonPagedCcb *)AFSExAllocatePoolWithTag( NonPagedPool,
1183                                                      sizeof( AFSNonPagedCcb),
1184                                                      AFS_CCB_NP_ALLOCATION_TAG);
1185
1186         if( pCcb->NPCcb == NULL)
1187         {
1188
1189             AFSDbgLogMsg( AFS_SUBSYSTEM_FILE_PROCESSING,
1190                           AFS_TRACE_LEVEL_ERROR,
1191                           "AFSInitCcb Failed to allocate NPCcb\n");
1192
1193             try_return( Status = STATUS_INSUFFICIENT_RESOURCES);
1194         }
1195
1196         ExInitializeResourceLite( &pCcb->NPCcb->CcbLock);
1197
1198         pCcb->DirectoryCB = DirectoryCB;
1199
1200         lCount = InterlockedIncrement( &pCcb->DirectoryCB->DirOpenReferenceCount);
1201
1202         AFSDbgLogMsg( AFS_SUBSYSTEM_DIRENTRY_REF_COUNTING,
1203                       AFS_TRACE_LEVEL_VERBOSE,
1204                       "AFSInitCcb Increment count on %wZ DE %p Ccb %p Cnt %d\n",
1205                       &pCcb->DirectoryCB->NameInformation.FileName,
1206                       pCcb->DirectoryCB,
1207                       pCcb,
1208                       lCount);
1209
1210         pCcb->GrantedAccess = GrantedAccess;
1211
1212         pCcb->FileAccess = FileAccess;
1213
1214         //
1215         // Return the Ccb
1216         //
1217
1218         *Ccb = pCcb;
1219
1220 try_exit:
1221
1222         if( !NT_SUCCESS( Status))
1223         {
1224
1225             if( pCcb != NULL)
1226             {
1227
1228                 if ( pCcb->NPCcb != NULL)
1229                 {
1230
1231                     AFSExFreePoolWithTag( pCcb->NPCcb, AFS_CCB_NP_ALLOCATION_TAG);
1232                 }
1233
1234                 AFSExFreePoolWithTag( pCcb, AFS_CCB_ALLOCATION_TAG);
1235             }
1236
1237             *Ccb = NULL;
1238         }
1239     }
1240
1241     return Status;
1242 }
1243
1244 //
1245 // Function: AFSRemoveCcb
1246 //
1247 // Description:
1248 //
1249 //      This function performs Ccb removal/deallocation
1250 //
1251 // Return:
1252 //
1253 //      None
1254 //
1255
1256 void
1257 AFSRemoveCcb( IN AFSFcb *Fcb,
1258               IN AFSCcb *Ccb)
1259 {
1260
1261     LONG lCount;
1262
1263     AFSAcquireExcl( &Ccb->NPCcb->CcbLock,
1264                     TRUE);
1265
1266     if( Fcb != NULL &&
1267         BooleanFlagOn( Ccb->Flags, CCB_FLAG_INSERTED_CCB_LIST))
1268     {
1269
1270         AFSAcquireExcl( &Fcb->NPFcb->CcbListLock,
1271                         TRUE);
1272
1273         if( Ccb->ListEntry.fLink == NULL)
1274         {
1275
1276             Fcb->CcbListTail = (AFSCcb *)Ccb->ListEntry.bLink;
1277
1278             if( Fcb->CcbListTail != NULL)
1279             {
1280                 Fcb->CcbListTail->ListEntry.fLink = NULL;
1281             }
1282         }
1283         else
1284         {
1285             ((AFSCcb *)(Ccb->ListEntry.fLink))->ListEntry.bLink = Ccb->ListEntry.bLink;
1286         }
1287
1288         if( Ccb->ListEntry.bLink == NULL)
1289         {
1290
1291             Fcb->CcbListHead = (AFSCcb *)Ccb->ListEntry.fLink;
1292
1293             if( Fcb->CcbListHead != NULL)
1294             {
1295                 Fcb->CcbListHead->ListEntry.bLink = NULL;
1296             }
1297         }
1298         else
1299         {
1300             ((AFSCcb *)(Ccb->ListEntry.bLink))->ListEntry.fLink = Ccb->ListEntry.fLink;
1301         }
1302
1303         AFSReleaseResource( &Fcb->NPFcb->CcbListLock);
1304     }
1305
1306     if( Ccb->MaskName.Buffer != NULL)
1307     {
1308
1309         AFSExFreePoolWithTag( Ccb->MaskName.Buffer, AFS_GENERIC_MEMORY_6_TAG);
1310     }
1311
1312     if( BooleanFlagOn( Ccb->Flags, CCB_FLAG_FREE_FULL_PATHNAME))
1313     {
1314
1315         AFSExFreePoolWithTag( Ccb->FullFileName.Buffer, 0);
1316     }
1317
1318     //
1319     // If we have a name array then delete it
1320     //
1321
1322     if( Ccb->NameArray != NULL)
1323     {
1324
1325         AFSFreeNameArray( Ccb->NameArray);
1326
1327         Ccb->NameArray = NULL;
1328     }
1329
1330     if( Ccb->DirectorySnapshot != NULL)
1331     {
1332
1333         AFSExFreePoolWithTag( Ccb->DirectorySnapshot, AFS_DIR_SNAPSHOT_TAG);
1334
1335         Ccb->DirectorySnapshot = NULL;
1336     }
1337
1338     if( Ccb->NotifyMask.Buffer != NULL)
1339     {
1340
1341         AFSExFreePoolWithTag( Ccb->NotifyMask.Buffer, AFS_GENERIC_MEMORY_7_TAG);
1342     }
1343
1344     if ( Ccb->DirectoryCB != NULL)
1345     {
1346
1347         lCount = InterlockedDecrement( &Ccb->DirectoryCB->DirOpenReferenceCount);
1348
1349         AFSDbgLogMsg( AFS_SUBSYSTEM_DIRENTRY_REF_COUNTING,
1350                       AFS_TRACE_LEVEL_VERBOSE,
1351                       "AFSRemoveCcb Decrement count on %wZ DE %p Ccb %p Cnt %d\n",
1352                       &Ccb->DirectoryCB->NameInformation.FileName,
1353                       Ccb->DirectoryCB,
1354                       Ccb,
1355                       lCount);
1356
1357         ASSERT( lCount >= 0);
1358     }
1359
1360     AFSReleaseResource( &Ccb->NPCcb->CcbLock);
1361
1362     //
1363     // Free up the Ccb
1364     //
1365
1366     ExDeleteResourceLite( &Ccb->NPCcb->CcbLock);
1367
1368     AFSExFreePoolWithTag( Ccb->NPCcb, AFS_CCB_NP_ALLOCATION_TAG);
1369
1370     AFSExFreePoolWithTag( Ccb, AFS_CCB_ALLOCATION_TAG);
1371 }
1372
1373 NTSTATUS
1374 AFSInsertCcb( IN AFSFcb *Fcb,
1375               IN AFSCcb *Ccb)
1376 {
1377
1378     NTSTATUS ntStatus = STATUS_SUCCESS;
1379
1380     AFSAcquireExcl( &Fcb->NPFcb->CcbListLock,
1381                     TRUE);
1382
1383     AFSAcquireExcl( &Ccb->NPCcb->CcbLock,
1384                     TRUE);
1385
1386     if( Fcb->CcbListHead == NULL)
1387     {
1388         Fcb->CcbListHead = Ccb;
1389     }
1390     else
1391     {
1392         Fcb->CcbListTail->ListEntry.fLink = (void *)Ccb;
1393
1394         Ccb->ListEntry.bLink = (void *)Fcb->CcbListTail;
1395     }
1396
1397     Fcb->CcbListTail = Ccb;
1398
1399     SetFlag( Ccb->Flags, CCB_FLAG_INSERTED_CCB_LIST);
1400
1401     AFSReleaseResource( &Ccb->NPCcb->CcbLock);
1402
1403     AFSReleaseResource( &Fcb->NPFcb->CcbListLock);
1404
1405     return ntStatus;
1406 }