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