68f770f410b969ebd75a279f566ba66263ed948a
[openafs.git] / src / WINNT / afsrdr / kernel / lib / AFSCreate.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: AFSCreate.cpp
37 //
38
39 #include "AFSCommon.h"
40
41 //
42 // Function: AFSCreate
43 //
44 // Description:
45 //
46 //      This function is the dispatch handler for the IRP_MJ_CREATE requests. It makes the determination to
47 //      which interface this request is destined.
48 //
49 // Return:
50 //
51 //      A status is returned for the function. The Irp completion processing is handled in the specific
52 //      interface handler.
53 //
54
55 NTSTATUS
56 AFSCreate( IN PDEVICE_OBJECT LibDeviceObject,
57            IN PIRP Irp)
58 {
59     UNREFERENCED_PARAMETER(LibDeviceObject);
60     NTSTATUS ntStatus = STATUS_SUCCESS;
61     IO_STACK_LOCATION  *pIrpSp;
62     FILE_OBJECT        *pFileObject = NULL;
63
64     __try
65     {
66
67         pIrpSp = IoGetCurrentIrpStackLocation( Irp);
68         pFileObject = pIrpSp->FileObject;
69
70         if( pFileObject == NULL ||
71             pFileObject->FileName.Buffer == NULL)
72         {
73
74             AFSDbgLogMsg( AFS_SUBSYSTEM_FILE_PROCESSING,
75                           AFS_TRACE_LEVEL_VERBOSE,
76                           "AFSCreate (%p) Processing control device open request\n",
77                           Irp);
78
79             ntStatus = AFSControlDeviceCreate( Irp);
80
81             try_return( ntStatus);
82         }
83
84         if( AFSRDRDeviceObject == NULL)
85         {
86
87             AFSDbgLogMsg( AFS_SUBSYSTEM_FILE_PROCESSING,
88                           AFS_TRACE_LEVEL_VERBOSE,
89                           "AFSCreate (%p) Invalid request to open before library is initialized\n",
90                           Irp);
91
92             try_return( ntStatus = STATUS_DEVICE_NOT_READY);
93         }
94
95         ntStatus = AFSCommonCreate( AFSRDRDeviceObject,
96                                     Irp);
97
98 try_exit:
99
100         NOTHING;
101     }
102     __except( AFSExceptionFilter( __FUNCTION__, GetExceptionCode(), GetExceptionInformation()) )
103     {
104
105         AFSDbgLogMsg( 0,
106                       0,
107                       "EXCEPTION - AFSCreate\n");
108
109         ntStatus = STATUS_ACCESS_DENIED;
110
111         AFSDumpTraceFilesFnc();
112     }
113
114     //
115     // Complete the request
116     //
117
118     AFSCompleteRequest( Irp,
119                           ntStatus);
120
121     return ntStatus;
122 }
123
124 NTSTATUS
125 AFSCommonCreate( IN PDEVICE_OBJECT DeviceObject,
126                  IN PIRP Irp)
127 {
128
129     NTSTATUS            ntStatus = STATUS_SUCCESS;
130     UNICODE_STRING      uniFileName;
131     ULONG               ulCreateDisposition = 0;
132     ULONG               ulOptions = 0;
133     BOOLEAN             bNoIntermediateBuffering = FALSE;
134     FILE_OBJECT        *pFileObject = NULL;
135     IO_STACK_LOCATION  *pIrpSp;
136     AFSFcb             *pFcb = NULL;
137     AFSCcb             *pCcb = NULL;
138     AFSDeviceExt       *pDeviceExt = NULL;
139     BOOLEAN             bOpenTargetDirectory = FALSE, bReleaseVolume = FALSE;
140     PACCESS_MASK        pDesiredAccess = NULL;
141     UNICODE_STRING      uniComponentName, uniRootFileName, uniParsedFileName;
142     UNICODE_STRING      uniSubstitutedPathName;
143     UNICODE_STRING      uniRelativeName;
144     AFSNameArrayHdr    *pNameArray = NULL;
145     AFSVolumeCB        *pVolumeCB = NULL;
146     AFSDirectoryCB     *pParentDirectoryCB = NULL, *pDirectoryCB = NULL;
147     BOOLEAN             bReleaseParentDir = FALSE, bReleaseDir = FALSE;
148     ULONG               ulParseFlags = 0;
149     GUID                stAuthGroup = {0};
150     ULONG               ulNameProcessingFlags = 0;
151     BOOLEAN             bOpenedReparsePoint = FALSE;
152     LONG                lCount;
153
154     __Enter
155     {
156
157         pIrpSp = IoGetCurrentIrpStackLocation( Irp);
158         pDeviceExt = (AFSDeviceExt *)DeviceObject->DeviceExtension;
159         ulCreateDisposition = (pIrpSp->Parameters.Create.Options >> 24) & 0x000000ff;
160         ulOptions = pIrpSp->Parameters.Create.Options;
161         bNoIntermediateBuffering = BooleanFlagOn( ulOptions, FILE_NO_INTERMEDIATE_BUFFERING);
162         bOpenTargetDirectory = BooleanFlagOn( pIrpSp->Flags, SL_OPEN_TARGET_DIRECTORY);
163         pFileObject = pIrpSp->FileObject;
164         pDesiredAccess = &pIrpSp->Parameters.Create.SecurityContext->DesiredAccess;
165
166         uniFileName.Length = uniFileName.MaximumLength = 0;
167         uniFileName.Buffer = NULL;
168
169         uniRootFileName.Length = uniRootFileName.MaximumLength = 0;
170         uniRootFileName.Buffer = NULL;
171
172         uniParsedFileName.Length = uniParsedFileName.MaximumLength = 0;
173         uniParsedFileName.Buffer = NULL;
174
175         uniSubstitutedPathName.Buffer = NULL;
176         uniSubstitutedPathName.Length = 0;
177
178         uniRelativeName.Buffer = NULL;
179         uniRelativeName.Length = 0;
180
181         if( AFSGlobalRoot == NULL)
182         {
183             try_return( ntStatus = STATUS_DEVICE_NOT_READY);
184         }
185
186         RtlZeroMemory( &stAuthGroup,
187                        sizeof( GUID));
188
189         AFSRetrieveAuthGroupFnc( (ULONGLONG)PsGetCurrentProcessId(),
190                                  (ULONGLONG)PsGetCurrentThreadId(),
191                                   &stAuthGroup);
192
193         //
194         // If we are in shutdown mode then fail the request
195         //
196
197         if( BooleanFlagOn( pDeviceExt->DeviceFlags, AFS_DEVICE_FLAG_REDIRECTOR_SHUTDOWN))
198         {
199
200             AFSDbgLogMsg( AFS_SUBSYSTEM_FILE_PROCESSING,
201                           AFS_TRACE_LEVEL_WARNING,
202                           "AFSCommonCreate (%p) Open request after shutdown\n",
203                           Irp);
204
205             try_return( ntStatus = STATUS_TOO_LATE);
206         }
207
208         if( !BooleanFlagOn( AFSGlobalRoot->ObjectInformation.Flags, AFS_OBJECT_FLAGS_DIRECTORY_ENUMERATED))
209         {
210
211             ntStatus = AFSEnumerateGlobalRoot( &stAuthGroup);
212
213             if( !NT_SUCCESS( ntStatus))
214             {
215
216                 AFSDbgLogMsg( AFS_SUBSYSTEM_FILE_PROCESSING,
217                               AFS_TRACE_LEVEL_ERROR,
218                               "AFSCommonCreate Failed to enumerate global root Status %08lX\n",
219                               ntStatus);
220
221                 try_return( ntStatus);
222             }
223         }
224
225         //
226         // Go and parse the name for processing.
227         // If ulParseFlags is returned with AFS_PARSE_FLAG_FREE_FILE_BUFFER set,
228         // then we are responsible for releasing the uniRootFileName.Buffer.
229         //
230
231         ntStatus = AFSParseName( Irp,
232                                  &stAuthGroup,
233                                  &uniFileName,
234                                  &uniParsedFileName,
235                                  &uniRootFileName,
236                                  &ulParseFlags,
237                                  &pVolumeCB,
238                                  &pParentDirectoryCB,
239                                  &pNameArray);
240
241         if( !NT_SUCCESS( ntStatus))
242         {
243
244             AFSDbgLogMsg( AFS_SUBSYSTEM_FILE_PROCESSING,
245                           uniFileName.Length > 0 ? AFS_TRACE_LEVEL_ERROR : AFS_TRACE_LEVEL_VERBOSE,
246                           "AFSCommonCreate (%p) Failed to parse name \"%wZ\" Status %08lX\n",
247                           Irp,
248                           &uniFileName,
249                           ntStatus);
250
251             try_return( ntStatus);
252         }
253
254         //
255         // Check for STATUS_REPARSE
256         //
257
258         if( ntStatus == STATUS_REPARSE)
259         {
260
261             //
262             // Update the information and return
263             //
264
265             Irp->IoStatus.Information = IO_REPARSE;
266
267             try_return( ntStatus);
268         }
269
270         if ( pParentDirectoryCB != NULL)
271         {
272
273             bReleaseParentDir = TRUE;
274         }
275
276         //
277         // If the returned volume cb is NULL then we are dealing with the \\Server\GlobalRoot
278         // name
279         //
280
281         if( pVolumeCB == NULL)
282         {
283
284             //
285             // Remove any leading or trailing slashes
286             //
287
288             if( uniFileName.Length >= sizeof( WCHAR) &&
289                 uniFileName.Buffer[ (uniFileName.Length/sizeof( WCHAR)) - 1] == L'\\')
290             {
291
292                 uniFileName.Length -= sizeof( WCHAR);
293             }
294
295             if( uniFileName.Length >= sizeof( WCHAR) &&
296                 uniFileName.Buffer[ 0] == L'\\')
297             {
298
299                 uniFileName.Buffer = &uniFileName.Buffer[ 1];
300
301                 uniFileName.Length -= sizeof( WCHAR);
302             }
303
304             //
305             // If there is a remaining portion returned for this request then
306             // check if it is for the PIOCtl interface
307             //
308
309             if( uniFileName.Length > 0)
310             {
311
312                 //
313                 // We don't accept any other opens off of the AFS Root
314                 //
315
316                 ntStatus = STATUS_OBJECT_NAME_NOT_FOUND;
317
318                 //
319                 // If this is an open on "_._AFS_IOCTL_._" then perform handling on it accordingly
320                 //
321
322                 if( RtlCompareUnicodeString( &AFSPIOCtlName,
323                                              &uniFileName,
324                                              TRUE) == 0)
325                 {
326
327                     //
328                     // AFSOpenIOCtlFcb does not free a DirOpenReferenceCount for
329                     // AFSGlobalRoot->DirectoryCB.
330                     //
331
332                     ntStatus = AFSOpenIOCtlFcb( Irp,
333                                                 &stAuthGroup,
334                                                 AFSGlobalRoot->DirectoryCB,
335                                                 &pFcb,
336                                                 &pCcb);
337
338                     if( !NT_SUCCESS( ntStatus))
339                     {
340
341                         AFSDbgLogMsg( AFS_SUBSYSTEM_FILE_PROCESSING,
342                                       AFS_TRACE_LEVEL_ERROR,
343                                       "AFSCommonCreate Failed to open root IOCtl Fcb Status %08lX\n",
344                                       ntStatus);
345                     }
346                 }
347                 else if( pParentDirectoryCB != NULL)
348                 {
349
350                     if( pParentDirectoryCB->ObjectInformation->FileType == AFS_FILE_TYPE_SPECIAL_SHARE_NAME)
351                     {
352
353                         ntStatus = AFSOpenSpecialShareFcb( Irp,
354                                                            &stAuthGroup,
355                                                            pParentDirectoryCB,
356                                                            &pFcb,
357                                                            &pCcb);
358
359                         if( !NT_SUCCESS( ntStatus))
360                         {
361
362                             AFSDbgLogMsg( AFS_SUBSYSTEM_FILE_PROCESSING,
363                                           AFS_TRACE_LEVEL_ERROR,
364                                           "AFSCommonCreate Failed to open special share Fcb Status %08lX\n",
365                                           ntStatus);
366                         }
367                     }
368                 }
369
370                 try_return( ntStatus);
371             }
372
373             ntStatus = AFSOpenAFSRoot( Irp,
374                                        &pFcb,
375                                        &pCcb);
376
377             if( !NT_SUCCESS( ntStatus))
378             {
379
380                 AFSDbgLogMsg( AFS_SUBSYSTEM_FILE_PROCESSING,
381                               AFS_TRACE_LEVEL_ERROR,
382                               "AFSCommonCreate Failed to open root Status %08lX\n",
383                               ntStatus);
384             }
385
386             try_return( ntStatus);
387         }
388
389         //
390         // We have a reference on the root volume
391         //
392
393         bReleaseVolume = TRUE;
394
395         //
396         // Attempt to locate the node in the name tree if this is not a target
397         // open and the target is not the root
398         //
399
400         uniComponentName.Length = 0;
401         uniComponentName.Buffer = NULL;
402
403         if( uniFileName.Length > sizeof( WCHAR) ||
404             uniFileName.Buffer[ 0] != L'\\')
405         {
406
407             if( !AFSValidNameFormat( &uniFileName))
408             {
409
410                 ntStatus = STATUS_OBJECT_NAME_NOT_FOUND;
411
412                 AFSDbgLogMsg( AFS_SUBSYSTEM_FILE_PROCESSING,
413                               AFS_TRACE_LEVEL_VERBOSE,
414                               "AFSCommonCreate (%p) Invalid name %wZ Status %08lX\n",
415                               Irp,
416                               &uniFileName,
417                               ntStatus);
418
419                 try_return( ntStatus);
420             }
421
422             //
423             // Opening a reparse point directly?
424             //
425
426             ulNameProcessingFlags = AFS_LOCATE_FLAGS_SUBSTITUTE_NAME;
427
428             if( BooleanFlagOn( ulOptions, FILE_OPEN_REPARSE_POINT))
429             {
430                 ulNameProcessingFlags |= (AFS_LOCATE_FLAGS_NO_MP_TARGET_EVAL |
431                                           AFS_LOCATE_FLAGS_NO_SL_TARGET_EVAL |
432                                           AFS_LOCATE_FLAGS_NO_DFS_LINK_EVAL);
433             }
434
435             uniSubstitutedPathName = uniRootFileName;
436
437             ntStatus = AFSLocateNameEntry( &stAuthGroup,
438                                            pFileObject,
439                                            &uniRootFileName,
440                                            &uniParsedFileName,
441                                            pNameArray,
442                                            ulNameProcessingFlags,
443                                            &pVolumeCB,
444                                            &pParentDirectoryCB,
445                                            &pDirectoryCB,
446                                            &uniComponentName);
447
448             if( !NT_SUCCESS( ntStatus) &&
449                 ntStatus != STATUS_OBJECT_NAME_NOT_FOUND)
450             {
451
452                 if ( uniSubstitutedPathName.Buffer == uniRootFileName.Buffer)
453                 {
454                     uniSubstitutedPathName.Buffer = NULL;
455                 }
456
457                 //
458                 // AFSLocateNameEntry released the Parent while walking the
459                 // branch
460                 //
461
462                 AFSDbgLogMsg( AFS_SUBSYSTEM_FILE_PROCESSING,
463                               AFS_TRACE_LEVEL_VERBOSE,
464                               "AFSCommonCreate (%p) Failed to locate name entry for %wZ Status %08lX\n",
465                               Irp,
466                               &uniFileName,
467                               ntStatus);
468
469                 //
470                 // We released any root volume locks in the above on failure
471                 //
472
473                 bReleaseVolume = FALSE;
474
475                 bReleaseParentDir = FALSE;
476
477                 try_return( ntStatus);
478             }
479
480             //
481             // Check for STATUS_REPARSE
482             //
483
484             if( ntStatus == STATUS_REPARSE)
485             {
486
487                 uniSubstitutedPathName.Buffer = NULL;
488
489                 //
490                 // Update the information and return
491                 //
492
493                 Irp->IoStatus.Information = IO_REPARSE;
494
495                 //
496                 // We released the volume lock above
497                 //
498
499                 bReleaseVolume = FALSE;
500
501                 bReleaseParentDir = FALSE;
502
503                 try_return( ntStatus);
504             }
505
506             //
507             // If we re-allocated the name, then update our substitute name
508             //
509
510             if( uniSubstitutedPathName.Buffer != uniRootFileName.Buffer)
511             {
512
513                 uniSubstitutedPathName = uniRootFileName;
514             }
515             else
516             {
517
518                 uniSubstitutedPathName.Buffer = NULL;
519             }
520
521             //
522             // Check for a symlink access
523             //
524
525             if( ntStatus == STATUS_OBJECT_NAME_NOT_FOUND &&
526                 pParentDirectoryCB != NULL)
527             {
528
529                 //
530                 // pParentDirectoryCB DirOpenReferenceCount is still held
531                 //
532
533                 UNICODE_STRING uniFinalComponent;
534
535                 uniFinalComponent.Length = 0;
536                 uniFinalComponent.MaximumLength = 0;
537                 uniFinalComponent.Buffer = NULL;
538
539                 AFSRetrieveFinalComponent( &uniFileName,
540                                            &uniFinalComponent);
541
542                 ntStatus = AFSCheckSymlinkAccess( pParentDirectoryCB,
543                                                   &uniFinalComponent);
544
545                 if( !NT_SUCCESS( ntStatus) &&
546                     ntStatus != STATUS_OBJECT_NAME_NOT_FOUND)
547                 {
548
549                     AFSDbgLogMsg( AFS_SUBSYSTEM_FILE_PROCESSING,
550                                   AFS_TRACE_LEVEL_VERBOSE,
551                                   "AFSCommonCreate (%p) Failing access to symlink %wZ Status %08lX\n",
552                                   Irp,
553                                   &uniFileName,
554                                   ntStatus);
555
556                     try_return( ntStatus);
557                 }
558             }
559             else
560             {
561
562                 //
563                 // AFSLocateNameEntry succeeded.  The parent directory reference
564                 // has been released and if there is a directory returned, it is
565                 // referenced.
566                 //
567
568                 bReleaseParentDir = FALSE;
569
570                 if ( pDirectoryCB)
571                 {
572
573                     bReleaseDir = TRUE;
574                 }
575             }
576         }
577
578         //
579         // If we have no parent then this is a root open, be sure there is a directory entry
580         // for the root
581         //
582
583         else if( pParentDirectoryCB == NULL &&
584                  pDirectoryCB == NULL)
585         {
586
587             pDirectoryCB = pVolumeCB->DirectoryCB;
588
589             lCount = InterlockedIncrement( &pDirectoryCB->DirOpenReferenceCount);
590
591             AFSDbgLogMsg( AFS_SUBSYSTEM_DIRENTRY_REF_COUNTING,
592                           AFS_TRACE_LEVEL_VERBOSE,
593                           "AFSCommonCreate Increment0 count on %wZ DE %p Ccb %p Cnt %d\n",
594                           &pDirectoryCB->NameInformation.FileName,
595                           pDirectoryCB,
596                           pCcb,
597                           lCount);
598
599             bReleaseDir = TRUE;
600         }
601
602         if( bOpenTargetDirectory)
603         {
604
605             //
606             // If we have a directory cb for the entry then dereference it and reference the parent
607             //
608
609             if( pDirectoryCB != NULL)
610             {
611
612                 if ( !bReleaseParentDir)
613                 {
614
615                     //
616                     // Perform in this order to prevent thrashing
617                     //
618
619                     lCount = InterlockedIncrement( &pParentDirectoryCB->DirOpenReferenceCount);
620
621                     AFSDbgLogMsg( AFS_SUBSYSTEM_DIRENTRY_REF_COUNTING,
622                                   AFS_TRACE_LEVEL_VERBOSE,
623                                   "AFSCommonCreate Increment1 count on %wZ DE %p Ccb %p Cnt %d\n",
624                                   &pParentDirectoryCB->NameInformation.FileName,
625                                   pParentDirectoryCB,
626                                   pCcb,
627                                   lCount);
628
629                     bReleaseParentDir = TRUE;
630                 }
631
632                 //
633                 // Do NOT decrement the reference count on the pDirectoryCB yet.
634                 // The BackupEntry below might drop the count to zero leaving
635                 // the entry subject to being deleted and we need some of the
636                 // contents during later processing
637                 //
638
639                 AFSBackupEntry( pNameArray);
640             }
641
642             //
643             // OK, open the target directory
644             //
645
646             if( uniComponentName.Length == 0)
647             {
648                 AFSRetrieveFinalComponent( &uniFileName,
649                                            &uniComponentName);
650             }
651
652             ntStatus = AFSOpenTargetDirectory( Irp,
653                                                pVolumeCB,
654                                                pParentDirectoryCB,
655                                                pDirectoryCB,
656                                                &uniComponentName,
657                                                &pFcb,
658                                                &pCcb);
659
660             if( !NT_SUCCESS( ntStatus))
661             {
662
663                 AFSDbgLogMsg( AFS_SUBSYSTEM_FILE_PROCESSING,
664                               AFS_TRACE_LEVEL_ERROR,
665                               "AFSCommonCreate Failed to open target directory %wZ Status %08lX\n",
666                               &pParentDirectoryCB->NameInformation.FileName,
667                               ntStatus);
668             }
669
670             try_return( ntStatus);
671         }
672
673         if ( BooleanFlagOn( ulOptions, FILE_OPEN_REPARSE_POINT))
674         {
675
676             if( pDirectoryCB == NULL ||
677                 !BooleanFlagOn( pDirectoryCB->ObjectInformation->FileAttributes, FILE_ATTRIBUTE_REPARSE_POINT))
678             {
679                 AFSDbgLogMsg( AFS_SUBSYSTEM_FILE_PROCESSING,
680                               AFS_TRACE_LEVEL_VERBOSE,
681                               "AFSCommonCreate (%p) Reparse open request but attribute not set for %wZ DirCB %p Type %08lX\n",
682                               Irp,
683                               &uniFileName,
684                               pDirectoryCB,
685                               pDirectoryCB ? pDirectoryCB->ObjectInformation->FileType : 0);
686             }
687             else
688             {
689                 AFSDbgLogMsg( AFS_SUBSYSTEM_FILE_PROCESSING,
690                               AFS_TRACE_LEVEL_VERBOSE,
691                               "AFSCommonCreate (%p) Opening as reparse point %wZ Type %08lX\n",
692                               Irp,
693                               &uniFileName,
694                               pDirectoryCB->ObjectInformation->FileType);
695
696                 bOpenedReparsePoint = TRUE;
697             }
698         }
699
700         //
701         // Based on the options passed in, process the file accordingly.
702         //
703
704         if( ulCreateDisposition == FILE_CREATE ||
705             ( ( ulCreateDisposition == FILE_OPEN_IF ||
706                 ulCreateDisposition == FILE_OVERWRITE_IF) &&
707               pDirectoryCB == NULL))
708         {
709
710             if( uniComponentName.Length == 0 ||
711                 pDirectoryCB != NULL)
712             {
713
714                 //
715                 // We traversed the entire path so we found each entry,
716                 // fail with collision
717                 //
718
719                 if( pDirectoryCB != NULL)
720                 {
721
722                     AFSDbgLogMsg( AFS_SUBSYSTEM_FILE_PROCESSING,
723                                   AFS_TRACE_LEVEL_VERBOSE,
724                                   "AFSCommonCreate Object name collision on create of %wZ Status %08lX\n",
725                                   &pDirectoryCB->NameInformation.FileName,
726                                   ntStatus);
727                 }
728                 else
729                 {
730
731                     AFSDbgLogMsg( AFS_SUBSYSTEM_FILE_PROCESSING,
732                                   AFS_TRACE_LEVEL_VERBOSE,
733                                   "AFSCommonCreate Object name collision on create Status %08lX\n",
734                                   ntStatus);
735                 }
736
737                 try_return( ntStatus = STATUS_OBJECT_NAME_COLLISION);
738             }
739
740             //
741             // OK, go and create the node
742             //
743
744             ntStatus = AFSProcessCreate( Irp,
745                                          &stAuthGroup,
746                                          pVolumeCB,
747                                          pParentDirectoryCB,
748                                          &uniFileName,
749                                          &uniComponentName,
750                                          &uniRootFileName,
751                                          &pFcb,
752                                          &pCcb);
753
754             if( !NT_SUCCESS( ntStatus))
755             {
756
757                 AFSDbgLogMsg( AFS_SUBSYSTEM_FILE_PROCESSING,
758                               AFS_TRACE_LEVEL_ERROR,
759                               "AFSCommonCreate Failed to create of %wZ in directory %wZ Status %08lX\n",
760                               &uniComponentName,
761                               &pParentDirectoryCB->NameInformation.FileName,
762                               ntStatus);
763             }
764
765             try_return( ntStatus);
766         }
767
768         //
769         // We should not have an extra component except for PIOCtl opens
770         //
771
772         if( uniComponentName.Length > 0)
773         {
774
775             //
776             // If this is an open on "_._AFS_IOCTL_._" then perform handling on it accordingly
777             //
778
779             if( RtlCompareUnicodeString( &AFSPIOCtlName,
780                                          &uniComponentName,
781                                          TRUE) == 0)
782             {
783
784                 //
785                 // AFSOpenIOCtlFcb does not free a DirOpenReferenceCount for
786                 // pParentDirectoryCB.
787                 //
788
789                 ntStatus = AFSOpenIOCtlFcb( Irp,
790                                             &stAuthGroup,
791                                             pParentDirectoryCB,
792                                             &pFcb,
793                                             &pCcb);
794
795                 if( !NT_SUCCESS( ntStatus))
796                 {
797
798                     AFSDbgLogMsg( AFS_SUBSYSTEM_FILE_PROCESSING,
799                                   AFS_TRACE_LEVEL_ERROR,
800                                   "AFSCommonCreate Failed to IOCtl open on %wZ Status %08lX\n",
801                                   &uniComponentName,
802                                   ntStatus);
803                 }
804             }
805             else
806             {
807
808                 AFSDbgLogMsg( AFS_SUBSYSTEM_FILE_PROCESSING,
809                               AFS_TRACE_LEVEL_VERBOSE,
810                               "AFSCommonCreate (%p) File %wZ name not found\n",
811                               Irp,
812                               &uniFileName);
813
814                 ntStatus = STATUS_OBJECT_NAME_NOT_FOUND;
815             }
816
817             try_return( ntStatus);
818         }
819
820         //
821         // For root opens the parent will be NULL
822         //
823
824         if( pParentDirectoryCB == NULL)
825         {
826
827             //
828             // Check for the delete on close flag for the root
829             //
830
831             if( BooleanFlagOn( ulOptions, FILE_DELETE_ON_CLOSE ))
832             {
833
834                 AFSDbgLogMsg( AFS_SUBSYSTEM_FILE_PROCESSING,
835                               AFS_TRACE_LEVEL_ERROR,
836                               "AFSCommonCreate (%p) Attempt to open root as delete on close\n",
837                               Irp);
838
839                 try_return( ntStatus = STATUS_CANNOT_DELETE);
840             }
841
842             //
843             // If this is the target directory, then bail
844             //
845
846             if( bOpenTargetDirectory)
847             {
848
849                 AFSDbgLogMsg( AFS_SUBSYSTEM_FILE_PROCESSING,
850                               AFS_TRACE_LEVEL_ERROR,
851                               "AFSCommonCreate (%p) Attempt to open root as target directory\n",
852                               Irp);
853
854                 try_return( ntStatus = STATUS_INVALID_PARAMETER);
855             }
856
857             //
858             // Go and open the root of the volume
859             //
860
861             ntStatus = AFSOpenRoot( Irp,
862                                     pVolumeCB,
863                                     &stAuthGroup,
864                                     &pFcb,
865                                     &pCcb);
866
867             if( !NT_SUCCESS( ntStatus))
868             {
869
870                 AFSDbgLogMsg( AFS_SUBSYSTEM_FILE_PROCESSING,
871                               AFS_TRACE_LEVEL_ERROR,
872                               "AFSCommonCreate Failed to open volume root %08lX-%08lX Status %08lX\n",
873                               pVolumeCB->ObjectInformation.FileId.Cell,
874                               pVolumeCB->ObjectInformation.FileId.Volume,
875                               ntStatus);
876             }
877
878             try_return( ntStatus);
879         }
880
881         //
882         // At this point if we have no pDirectoryCB it was not found.
883         //
884
885         if( pDirectoryCB == NULL)
886         {
887
888             AFSDbgLogMsg( AFS_SUBSYSTEM_FILE_PROCESSING,
889                           AFS_TRACE_LEVEL_ERROR,
890                           "AFSCommonCreate Failing access to %wZ Name not found\n",
891                           &uniFileName);
892
893             try_return( ntStatus = STATUS_OBJECT_NAME_NOT_FOUND);
894         }
895
896         if( ulCreateDisposition == FILE_OVERWRITE ||
897             ulCreateDisposition == FILE_SUPERSEDE ||
898             ulCreateDisposition == FILE_OVERWRITE_IF)
899         {
900
901             //
902             // Go process a file for overwrite or supersede.
903             //
904
905             ntStatus = AFSProcessOverwriteSupersede( DeviceObject,
906                                                      Irp,
907                                                      pVolumeCB,
908                                                      &stAuthGroup,
909                                                      pParentDirectoryCB,
910                                                      pDirectoryCB,
911                                                      &pFcb,
912                                                      &pCcb);
913
914             if( !NT_SUCCESS( ntStatus))
915             {
916
917                 AFSDbgLogMsg( AFS_SUBSYSTEM_FILE_PROCESSING,
918                               AFS_TRACE_LEVEL_ERROR,
919                               "AFSCommonCreate Failed overwrite/supersede on %wZ Status %08lX\n",
920                               &pDirectoryCB->NameInformation.FileName,
921                               ntStatus);
922             }
923
924             try_return( ntStatus);
925         }
926
927         //
928         // Trying to open the file
929         //
930
931         ntStatus = AFSProcessOpen( Irp,
932                                    &stAuthGroup,
933                                    pVolumeCB,
934                                    pParentDirectoryCB,
935                                    pDirectoryCB,
936                                    &pFcb,
937                                    &pCcb);
938
939         if( !NT_SUCCESS( ntStatus))
940         {
941
942             AFSDbgLogMsg( AFS_SUBSYSTEM_FILE_PROCESSING,
943                           AFS_TRACE_LEVEL_ERROR,
944                           "AFSCommonCreate Failed open on %wZ Status %08lX\n",
945                           &pDirectoryCB->NameInformation.FileName,
946                           ntStatus);
947         }
948
949 try_exit:
950
951         if( NT_SUCCESS( ntStatus) &&
952             ntStatus != STATUS_REPARSE)
953         {
954
955             if( pCcb != NULL)
956             {
957
958                 AFSAcquireExcl( &pCcb->NPCcb->CcbLock,
959                                 TRUE);
960
961                 RtlCopyMemory( &pCcb->AuthGroup,
962                                &stAuthGroup,
963                                sizeof( GUID));
964
965                 //
966                 // If we have a substitute name, then use it
967                 //
968
969                 if( uniSubstitutedPathName.Buffer != NULL)
970                 {
971
972                     pCcb->FullFileName = uniSubstitutedPathName;
973
974                     SetFlag( pCcb->Flags, CCB_FLAG_FREE_FULL_PATHNAME);
975
976                     ClearFlag( ulParseFlags, AFS_PARSE_FLAG_FREE_FILE_BUFFER);
977                 }
978                 else
979                 {
980
981                     pCcb->FullFileName = uniRootFileName;
982
983                     if( BooleanFlagOn( ulParseFlags, AFS_PARSE_FLAG_FREE_FILE_BUFFER))
984                     {
985
986                         SetFlag( pCcb->Flags, CCB_FLAG_FREE_FULL_PATHNAME);
987
988                         ClearFlag( ulParseFlags, AFS_PARSE_FLAG_FREE_FILE_BUFFER);
989                     }
990                 }
991
992                 if( bOpenedReparsePoint)
993                 {
994                     SetFlag( pCcb->Flags, CCB_FLAG_MASK_OPENED_REPARSE_POINT);
995                 }
996
997                 AFSDbgLogMsg( AFS_SUBSYSTEM_DIRENTRY_REF_COUNTING,
998                               AFS_TRACE_LEVEL_VERBOSE,
999                               "AFSCommonCreate Count on %wZ DE %p Ccb %p Cnt %d\n",
1000                               &pCcb->DirectoryCB->NameInformation.FileName,
1001                               pCcb->DirectoryCB,
1002                               pCcb,
1003                               lCount = pCcb->DirectoryCB->DirOpenReferenceCount);
1004
1005                 ASSERT( lCount >= 0);
1006
1007                 pCcb->CurrentDirIndex = 0;
1008
1009                 if( !BooleanFlagOn( ulParseFlags, AFS_PARSE_FLAG_ROOT_ACCESS))
1010                 {
1011
1012                     SetFlag( pCcb->Flags, CCB_FLAG_RETURN_RELATIVE_ENTRIES);
1013                 }
1014
1015                 //
1016                 // Save off the name array for this instance
1017                 //
1018
1019                 pCcb->NameArray = pNameArray;
1020
1021                 pNameArray = NULL;
1022
1023                 AFSReleaseResource( &pCcb->NPCcb->CcbLock);
1024             }
1025
1026             //
1027             // If we make it here then init the FO for the request.
1028             //
1029
1030             AFSDbgLogMsg( AFS_SUBSYSTEM_FILE_PROCESSING,
1031                           AFS_TRACE_LEVEL_VERBOSE_2,
1032                           "AFSCommonCreate (%p) FileObject %p FsContext %p FsContext2 %p\n",
1033                           Irp,
1034                           pFileObject,
1035                           pFcb,
1036                           pCcb);
1037
1038             pFileObject->FsContext = (void *)pFcb;
1039
1040             pFileObject->FsContext2 = (void *)pCcb;
1041
1042             if( pFcb != NULL)
1043             {
1044
1045                 ASSERT( pFcb->OpenHandleCount > 0);
1046
1047                 ClearFlag( pFcb->Flags, AFS_FCB_FILE_CLOSED);
1048
1049                 //
1050                 // For files perform additional processing
1051                 //
1052
1053                 switch( pFcb->Header.NodeTypeCode)
1054                 {
1055
1056                     case AFS_FILE_FCB:
1057                     case AFS_IOCTL_FCB:
1058                     {
1059
1060                         pFileObject->SectionObjectPointer = &pFcb->NPFcb->SectionObjectPointers;
1061                     }
1062                 }
1063
1064                 //
1065                 // If the user did not request nobuffering then mark the FO as cacheable
1066                 //
1067
1068                 if( bNoIntermediateBuffering)
1069                 {
1070
1071                     pFileObject->Flags |= FO_NO_INTERMEDIATE_BUFFERING;
1072                 }
1073                 else
1074                 {
1075
1076                     pFileObject->Flags |= FO_CACHE_SUPPORTED;
1077                 }
1078
1079                 //
1080                 // If the file was opened for execution then we need to set the bit in the FO
1081                 //
1082
1083                 if( BooleanFlagOn( *pDesiredAccess,
1084                                    FILE_EXECUTE))
1085                 {
1086
1087                     SetFlag( pFileObject->Flags, FO_FILE_FAST_IO_READ);
1088                 }
1089
1090                 //
1091                 // Update the last access time
1092                 //
1093
1094                 KeQuerySystemTime( &pFcb->ObjectInformation->LastAccessTime);
1095
1096                 if( pCcb != NULL)
1097                 {
1098                     AFSInsertCcb( pFcb,
1099                                   pCcb);
1100                 }
1101             }
1102             else
1103             {
1104
1105                 AFSDbgLogMsg( AFS_SUBSYSTEM_FILE_PROCESSING,
1106                               AFS_TRACE_LEVEL_ERROR,
1107                               "AFSCommonCreate (%p) Returning with NULL Fcb FileObject %p FsContext %p FsContext2 %p\n",
1108                               Irp,
1109                               pFileObject,
1110                               pFcb,
1111                               pCcb);
1112             }
1113         }
1114         else
1115         {
1116             if( NT_SUCCESS( ntStatus) &&
1117                 ntStatus == STATUS_REPARSE)
1118             {
1119
1120                 AFSDbgLogMsg( AFS_SUBSYSTEM_FILE_PROCESSING,
1121                               AFS_TRACE_LEVEL_ERROR,
1122                               "AFSCommonCreate (%p) STATUS_REPARSE FileObject %p FsContext %p FsContext2 %p\n",
1123                               Irp,
1124                               pFileObject,
1125                               pFcb,
1126                               pCcb);
1127             }
1128
1129             //
1130             // Free up the sub name if we have one
1131             //
1132
1133             if( uniSubstitutedPathName.Buffer != NULL)
1134             {
1135
1136                 AFSExFreePoolWithTag( uniSubstitutedPathName.Buffer, 0);
1137
1138                 ClearFlag( ulParseFlags, AFS_PARSE_FLAG_FREE_FILE_BUFFER);
1139             }
1140         }
1141
1142         //
1143         // Free up the name array ...
1144         //
1145
1146         if( pNameArray != NULL)
1147         {
1148
1149             AFSFreeNameArray( pNameArray);
1150         }
1151
1152         if( BooleanFlagOn( ulParseFlags, AFS_PARSE_FLAG_FREE_FILE_BUFFER))
1153         {
1154
1155             AFSExFreePoolWithTag( uniRootFileName.Buffer, 0);
1156         }
1157
1158         if( bReleaseVolume)
1159         {
1160
1161             lCount = InterlockedDecrement( &pVolumeCB->VolumeReferenceCount);
1162
1163             AFSDbgLogMsg( AFS_SUBSYSTEM_VOLUME_REF_COUNTING,
1164                           AFS_TRACE_LEVEL_VERBOSE,
1165                           "AFSCommonCreate Decrement count on Volume %08lX Cnt %d\n",
1166                           pVolumeCB,
1167                           lCount);
1168         }
1169
1170         if ( bReleaseDir)
1171         {
1172
1173             //
1174             // Release the reference from AFSLocateNameEntry
1175             //
1176
1177             lCount = InterlockedDecrement( &pDirectoryCB->DirOpenReferenceCount);
1178
1179             AFSDbgLogMsg( AFS_SUBSYSTEM_DIRENTRY_REF_COUNTING,
1180                           AFS_TRACE_LEVEL_VERBOSE,
1181                           "AFSCommonCreate Decrement count on %wZ DE %p Ccb %p Cnt %d\n",
1182                           &pDirectoryCB->NameInformation.FileName,
1183                           pDirectoryCB,
1184                           pCcb,
1185                           lCount);
1186
1187             ASSERT( lCount >= 0);
1188         }
1189
1190         if ( bReleaseParentDir)
1191         {
1192
1193             //
1194             // Release the reference from AFSLocateNameEntry
1195             //
1196
1197             lCount = InterlockedDecrement( &pParentDirectoryCB->DirOpenReferenceCount);
1198
1199             AFSDbgLogMsg( AFS_SUBSYSTEM_DIRENTRY_REF_COUNTING,
1200                           AFS_TRACE_LEVEL_VERBOSE,
1201                           "AFSCommonCreate Decrement count on %wZ DE %p Ccb %p Cnt %d\n",
1202                           &pParentDirectoryCB->NameInformation.FileName,
1203                           pParentDirectoryCB,
1204                           pCcb,
1205                           lCount);
1206
1207             ASSERT( lCount >= 0);
1208         }
1209
1210         //
1211         // Setup the Irp for completion, the Information has been set previously
1212         //
1213
1214         Irp->IoStatus.Status = ntStatus;
1215     }
1216
1217     return ntStatus;
1218 }
1219
1220 NTSTATUS
1221 AFSOpenAFSRoot( IN PIRP Irp,
1222                 IN AFSFcb **Fcb,
1223                 IN AFSCcb **Ccb)
1224 {
1225
1226     NTSTATUS ntStatus = STATUS_SUCCESS;
1227     LONG lCount;
1228
1229     __Enter
1230     {
1231
1232         //
1233         // Initialize the Ccb for the file.
1234         //
1235
1236         ntStatus = AFSInitCcb( Ccb,
1237                                AFSGlobalRoot->DirectoryCB,
1238                                0,
1239                                0);
1240
1241         if( !NT_SUCCESS( ntStatus))
1242         {
1243
1244             AFSDbgLogMsg( AFS_SUBSYSTEM_FILE_PROCESSING,
1245                           AFS_TRACE_LEVEL_ERROR,
1246                           "AFSOpenAFSRoot (%p) Failed to allocate Ccb\n",
1247                           Irp);
1248
1249             try_return( ntStatus);
1250         }
1251
1252         //
1253         // Increment the open count on this Fcb
1254         //
1255
1256         lCount = InterlockedIncrement( &AFSGlobalRoot->RootFcb->OpenReferenceCount);
1257
1258         AFSDbgLogMsg( AFS_SUBSYSTEM_FCB_REF_COUNTING,
1259                       AFS_TRACE_LEVEL_VERBOSE,
1260                       "AFSOpenAFSRoot Increment count on Fcb %p Cnt %d\n",
1261                       AFSGlobalRoot->RootFcb,
1262                       lCount);
1263
1264         lCount = InterlockedIncrement( &AFSGlobalRoot->RootFcb->OpenHandleCount);
1265
1266         AFSDbgLogMsg( AFS_SUBSYSTEM_FCB_REF_COUNTING,
1267                       AFS_TRACE_LEVEL_VERBOSE,
1268                       "AFSOpenAFSRoot Increment handle count on Fcb %p Cnt %d\n",
1269                       AFSGlobalRoot->RootFcb,
1270                       lCount);
1271
1272         *Fcb = AFSGlobalRoot->RootFcb;
1273
1274         //
1275         // Return the open result for this file
1276         //
1277
1278         Irp->IoStatus.Information = FILE_OPENED;
1279
1280 try_exit:
1281
1282         NOTHING;
1283     }
1284
1285     return ntStatus;
1286 }
1287
1288 NTSTATUS
1289 AFSOpenRoot( IN PIRP Irp,
1290              IN AFSVolumeCB *VolumeCB,
1291              IN GUID *AuthGroup,
1292              OUT AFSFcb **RootFcb,
1293              OUT AFSCcb **Ccb)
1294 {
1295
1296     NTSTATUS ntStatus = STATUS_SUCCESS;
1297     PFILE_OBJECT pFileObject = NULL;
1298     PIO_STACK_LOCATION pIrpSp = IoGetCurrentIrpStackLocation( Irp);
1299     PACCESS_MASK pDesiredAccess = NULL;
1300     USHORT usShareAccess;
1301     ULONG ulOptions;
1302     BOOLEAN bAllocatedCcb = FALSE;
1303     BOOLEAN bReleaseFcb = FALSE;
1304     AFSFileOpenCB   stOpenCB;
1305     AFSFileOpenResultCB stOpenResultCB;
1306     ULONG       ulResultLen = 0;
1307     LONG        lCount;
1308
1309     __Enter
1310     {
1311
1312         pDesiredAccess = &pIrpSp->Parameters.Create.SecurityContext->DesiredAccess;
1313         usShareAccess = pIrpSp->Parameters.Create.ShareAccess;
1314         ulOptions = pIrpSp->Parameters.Create.Options;
1315
1316         pFileObject = pIrpSp->FileObject;
1317
1318         if( BooleanFlagOn( ulOptions, FILE_NON_DIRECTORY_FILE))
1319         {
1320
1321             ntStatus = STATUS_FILE_IS_A_DIRECTORY;
1322
1323             AFSDbgLogMsg( AFS_SUBSYSTEM_FILE_PROCESSING,
1324                           AFS_TRACE_LEVEL_ERROR,
1325                           "AFSOpenRoot (%p) Attempt to open root as file Status %08lX\n",
1326                           Irp,
1327                           ntStatus);
1328
1329             try_return( ntStatus);
1330         }
1331
1332         //
1333         // Check if we should go and retrieve updated information for the node
1334         //
1335
1336         ntStatus = AFSValidateEntry( VolumeCB->DirectoryCB,
1337                                      AuthGroup,
1338                                      FALSE,
1339                                      TRUE);
1340
1341         if( !NT_SUCCESS( ntStatus))
1342         {
1343
1344             AFSDbgLogMsg( AFS_SUBSYSTEM_FILE_PROCESSING,
1345                           AFS_TRACE_LEVEL_ERROR,
1346                           "AFSOpenRoot (%p) Failed to validate root entry Status %08lX\n",
1347                           Irp,
1348                           ntStatus);
1349
1350             try_return( ntStatus);
1351         }
1352
1353         //
1354         // Check with the service that we can open the file
1355         //
1356
1357         RtlZeroMemory( &stOpenCB,
1358                        sizeof( AFSFileOpenCB));
1359
1360         stOpenCB.DesiredAccess = *pDesiredAccess;
1361
1362         stOpenCB.ShareAccess = usShareAccess;
1363
1364         stOpenResultCB.GrantedAccess = 0;
1365
1366         ulResultLen = sizeof( AFSFileOpenResultCB);
1367
1368         ntStatus = AFSProcessRequest( AFS_REQUEST_TYPE_OPEN_FILE,
1369                                       AFS_REQUEST_FLAG_SYNCHRONOUS | AFS_REQUEST_FLAG_HOLD_FID,
1370                                       AuthGroup,
1371                                       NULL,
1372                                       &VolumeCB->ObjectInformation.FileId,
1373                                       (void *)&stOpenCB,
1374                                       sizeof( AFSFileOpenCB),
1375                                       (void *)&stOpenResultCB,
1376                                       &ulResultLen);
1377
1378         if( !NT_SUCCESS( ntStatus))
1379         {
1380
1381             UNICODE_STRING uniGUID;
1382
1383             uniGUID.Length = 0;
1384             uniGUID.MaximumLength = 0;
1385             uniGUID.Buffer = NULL;
1386
1387             if( AuthGroup != NULL)
1388             {
1389                 RtlStringFromGUID( *AuthGroup,
1390                                    &uniGUID);
1391             }
1392
1393             AFSDbgLogMsg( AFS_SUBSYSTEM_FILE_PROCESSING,
1394                           AFS_TRACE_LEVEL_ERROR,
1395                           "AFSOpenRoot (%p) Failed open in service volume %08lX-%08lX AuthGroup %wZ Status %08lX\n",
1396                           Irp,
1397                           VolumeCB->ObjectInformation.FileId.Cell,
1398                           VolumeCB->ObjectInformation.FileId.Volume,
1399                           &uniGUID,
1400                           ntStatus);
1401
1402             if( AuthGroup != NULL)
1403             {
1404                 RtlFreeUnicodeString( &uniGUID);
1405             }
1406
1407             try_return( ntStatus);
1408         }
1409
1410         //
1411         // If the entry is not initialized then do it now
1412         //
1413
1414         if( !BooleanFlagOn( VolumeCB->ObjectInformation.Flags, AFS_OBJECT_FLAGS_DIRECTORY_ENUMERATED))
1415         {
1416
1417             AFSAcquireExcl( VolumeCB->ObjectInformation.Specific.Directory.DirectoryNodeHdr.TreeLock,
1418                             TRUE);
1419
1420             if( !BooleanFlagOn( VolumeCB->ObjectInformation.Flags, AFS_OBJECT_FLAGS_DIRECTORY_ENUMERATED))
1421             {
1422
1423                 ntStatus = AFSEnumerateDirectory( AuthGroup,
1424                                                   &VolumeCB->ObjectInformation,
1425                                                   TRUE);
1426
1427                 if( !NT_SUCCESS( ntStatus))
1428                 {
1429
1430                     AFSReleaseResource( VolumeCB->ObjectInformation.Specific.Directory.DirectoryNodeHdr.TreeLock);
1431
1432                     AFSDbgLogMsg( AFS_SUBSYSTEM_FILE_PROCESSING,
1433                                   AFS_TRACE_LEVEL_ERROR,
1434                                   "AFSOpenRoot (%p) Failed to enumerate directory Status %08lX\n",
1435                                   Irp,
1436                                   ntStatus);
1437
1438                     try_return( ntStatus);
1439                 }
1440
1441                 SetFlag( VolumeCB->ObjectInformation.Flags, AFS_OBJECT_FLAGS_DIRECTORY_ENUMERATED);
1442             }
1443
1444             AFSReleaseResource( VolumeCB->ObjectInformation.Specific.Directory.DirectoryNodeHdr.TreeLock);
1445         }
1446
1447         //
1448         // If the root fcb has been initialized then check access otherwise
1449         // init the volume fcb
1450         //
1451
1452         if( VolumeCB->RootFcb == NULL)
1453         {
1454
1455             ntStatus = AFSInitRootFcb( (ULONGLONG)PsGetCurrentProcessId(),
1456                                        VolumeCB);
1457
1458             if( !NT_SUCCESS( ntStatus))
1459             {
1460
1461                 try_return( ntStatus);
1462             }
1463         }
1464         else
1465         {
1466
1467             AFSAcquireExcl( VolumeCB->RootFcb->Header.Resource,
1468                             TRUE);
1469         }
1470
1471         lCount = InterlockedIncrement( &VolumeCB->RootFcb->OpenReferenceCount);
1472
1473         AFSDbgLogMsg( AFS_SUBSYSTEM_FCB_REF_COUNTING,
1474                       AFS_TRACE_LEVEL_VERBOSE,
1475                       "AFSOpenRoot Increment count on Fcb %p Cnt %d\n",
1476                       VolumeCB->RootFcb,
1477                       lCount);
1478
1479         bReleaseFcb = TRUE;
1480
1481         //
1482         // If there are current opens on the Fcb, check the access.
1483         //
1484
1485         if( VolumeCB->RootFcb->OpenHandleCount > 0)
1486         {
1487
1488             ntStatus = IoCheckShareAccess( *pDesiredAccess,
1489                                            usShareAccess,
1490                                            pFileObject,
1491                                            &VolumeCB->RootFcb->ShareAccess,
1492                                            FALSE);
1493
1494             if( !NT_SUCCESS( ntStatus))
1495             {
1496
1497                 AFSDbgLogMsg( AFS_SUBSYSTEM_FILE_PROCESSING,
1498                               AFS_TRACE_LEVEL_ERROR,
1499                               "AFSOpenRoot (%p) Access check failure Status %08lX\n",
1500                               Irp,
1501                               ntStatus);
1502
1503                 try_return( ntStatus);
1504             }
1505         }
1506
1507         //
1508         // Initialize the Ccb for the file.
1509         //
1510
1511         ntStatus = AFSInitCcb( Ccb,
1512                                VolumeCB->DirectoryCB,
1513                                *pDesiredAccess,
1514                                0);
1515
1516         if( !NT_SUCCESS( ntStatus))
1517         {
1518
1519             AFSDbgLogMsg( AFS_SUBSYSTEM_FILE_PROCESSING,
1520                           AFS_TRACE_LEVEL_ERROR,
1521                           "AFSOpenRoot (%p) Failed to allocate Ccb Status %08lX\n",
1522                           Irp,
1523                           ntStatus);
1524
1525             try_return( ntStatus);
1526         }
1527
1528         bAllocatedCcb = TRUE;
1529
1530         //
1531         // OK, update the share access on the fileobject
1532         //
1533
1534         if( VolumeCB->RootFcb->OpenHandleCount > 0)
1535         {
1536
1537             IoUpdateShareAccess( pFileObject,
1538                                  &VolumeCB->RootFcb->ShareAccess);
1539         }
1540         else
1541         {
1542
1543             //
1544             // Set the access
1545             //
1546
1547             IoSetShareAccess( *pDesiredAccess,
1548                               usShareAccess,
1549                               pFileObject,
1550                               &VolumeCB->RootFcb->ShareAccess);
1551         }
1552
1553         //
1554         // Increment the open count on this Fcb
1555         //
1556
1557         lCount = InterlockedIncrement( &VolumeCB->RootFcb->OpenHandleCount);
1558
1559         AFSDbgLogMsg( AFS_SUBSYSTEM_FCB_REF_COUNTING,
1560                       AFS_TRACE_LEVEL_VERBOSE,
1561                       "AFSOpenRoot Increment handle count on Fcb %p Cnt %d\n",
1562                       VolumeCB->RootFcb,
1563                       lCount);
1564
1565         //
1566         // Indicate the object is held
1567         //
1568
1569         SetFlag( VolumeCB->ObjectInformation.Flags, AFS_OBJECT_HELD_IN_SERVICE);
1570
1571         //
1572         // Return the open result for this file
1573         //
1574
1575         Irp->IoStatus.Information = FILE_OPENED;
1576
1577         *RootFcb = VolumeCB->RootFcb;
1578
1579 try_exit:
1580
1581         if( bReleaseFcb)
1582         {
1583             if ( !NT_SUCCESS( ntStatus))
1584             {
1585
1586                 lCount = InterlockedDecrement( &VolumeCB->RootFcb->OpenReferenceCount);
1587
1588                 AFSDbgLogMsg( AFS_SUBSYSTEM_FCB_REF_COUNTING,
1589                               AFS_TRACE_LEVEL_VERBOSE,
1590                               "AFSOpenRoot Decrement count on Fcb %p Cnt %d\n",
1591                               VolumeCB->RootFcb,
1592                               lCount);
1593             }
1594
1595             AFSReleaseResource( VolumeCB->RootFcb->Header.Resource);
1596         }
1597
1598         if( !NT_SUCCESS( ntStatus))
1599         {
1600
1601             if( bAllocatedCcb)
1602             {
1603
1604                 AFSRemoveCcb( NULL,
1605                               *Ccb);
1606
1607                 *Ccb = NULL;
1608             }
1609
1610             Irp->IoStatus.Information = 0;
1611         }
1612     }
1613
1614     return ntStatus;
1615 }
1616
1617 NTSTATUS
1618 AFSProcessCreate( IN PIRP               Irp,
1619                   IN GUID              *AuthGroup,
1620                   IN AFSVolumeCB       *VolumeCB,
1621                   IN AFSDirectoryCB    *ParentDirCB,
1622                   IN PUNICODE_STRING    FileName,
1623                   IN PUNICODE_STRING    ComponentName,
1624                   IN PUNICODE_STRING    FullFileName,
1625                   OUT AFSFcb          **Fcb,
1626                   OUT AFSCcb          **Ccb)
1627 {
1628
1629     NTSTATUS ntStatus = STATUS_SUCCESS;
1630     PFILE_OBJECT pFileObject = NULL;
1631     PIO_STACK_LOCATION pIrpSp = IoGetCurrentIrpStackLocation( Irp);
1632     ULONG ulOptions = 0;
1633     ULONG ulAttributes = 0;
1634     BOOLEAN bFileCreated = FALSE, bReleaseFcb = FALSE, bAllocatedCcb = FALSE;
1635     PACCESS_MASK pDesiredAccess = NULL;
1636     USHORT usShareAccess;
1637     AFSDirectoryCB *pDirEntry = NULL;
1638     AFSObjectInfoCB *pParentObjectInfo = NULL;
1639     AFSObjectInfoCB *pObjectInfo = NULL;
1640     LONG lCount;
1641
1642     __Enter
1643     {
1644
1645         pDesiredAccess = &pIrpSp->Parameters.Create.SecurityContext->DesiredAccess;
1646         usShareAccess = pIrpSp->Parameters.Create.ShareAccess;
1647
1648         pFileObject = pIrpSp->FileObject;
1649
1650         //
1651         // Extract out the options
1652         //
1653
1654         ulOptions = pIrpSp->Parameters.Create.Options;
1655
1656         //
1657         // We pass all attributes they want to apply to the file to the create
1658         //
1659
1660         ulAttributes = pIrpSp->Parameters.Create.FileAttributes;
1661
1662         //
1663         // If this is a directory create then set the attribute correctly
1664         //
1665
1666         if( ulOptions & FILE_DIRECTORY_FILE)
1667         {
1668
1669             ulAttributes |= FILE_ATTRIBUTE_DIRECTORY;
1670         }
1671
1672         AFSDbgLogMsg( AFS_SUBSYSTEM_FILE_PROCESSING,
1673                       AFS_TRACE_LEVEL_VERBOSE,
1674                       "AFSProcessCreate (%p) Creating file %wZ Attributes %08lX\n",
1675                       Irp,
1676                       FullFileName,
1677                       ulAttributes);
1678
1679         if( BooleanFlagOn( VolumeCB->VolumeInformation.Characteristics, FILE_READ_ONLY_DEVICE))
1680         {
1681
1682             AFSDbgLogMsg( AFS_SUBSYSTEM_FILE_PROCESSING,
1683                           AFS_TRACE_LEVEL_ERROR,
1684                           "AFSProcessCreate Request failed due to read only volume %wZ\n",
1685                           FullFileName);
1686
1687             try_return( ntStatus = STATUS_MEDIA_WRITE_PROTECTED);
1688         }
1689
1690         pParentObjectInfo = ParentDirCB->ObjectInformation;
1691
1692         //
1693         // Allocate and insert the direntry into the parent node
1694         //
1695
1696         ntStatus = AFSCreateDirEntry( AuthGroup,
1697                                       pParentObjectInfo,
1698                                       ParentDirCB,
1699                                       FileName,
1700                                       ComponentName,
1701                                       ulAttributes,
1702                                       &pDirEntry);
1703
1704         if( !NT_SUCCESS( ntStatus))
1705         {
1706
1707             AFSDbgLogMsg( AFS_SUBSYSTEM_FILE_PROCESSING,
1708                           AFS_TRACE_LEVEL_ERROR,
1709                           "AFSProcessCreate (%p) Failed to create directory entry %wZ Status %08lX\n",
1710                           Irp,
1711                           FullFileName,
1712                           ntStatus);
1713
1714             try_return( ntStatus);
1715         }
1716
1717         bFileCreated = TRUE;
1718
1719         pObjectInfo = pDirEntry->ObjectInformation;
1720
1721         if( BooleanFlagOn( pObjectInfo->Flags, AFS_OBJECT_FLAGS_NOT_EVALUATED) ||
1722             pObjectInfo->FileType == AFS_FILE_TYPE_UNKNOWN)
1723         {
1724
1725             AFSDbgLogMsg( AFS_SUBSYSTEM_FILE_PROCESSING,
1726                           AFS_TRACE_LEVEL_VERBOSE,
1727                           "AFSProcessCreate (%p) Evaluating object %wZ FID %08lX-%08lX-%08lX-%08lX\n",
1728                           Irp,
1729                           &pDirEntry->NameInformation.FileName,
1730                           pObjectInfo->FileId.Cell,
1731                           pObjectInfo->FileId.Volume,
1732                           pObjectInfo->FileId.Vnode,
1733                           pObjectInfo->FileId.Unique);
1734
1735             ntStatus = AFSEvaluateNode( AuthGroup,
1736                                         pDirEntry);
1737
1738             if( !NT_SUCCESS( ntStatus))
1739             {
1740
1741                 if ( ntStatus == STATUS_NOT_A_DIRECTORY)
1742                 {
1743
1744                     if ( pParentObjectInfo == pObjectInfo->ParentObjectInformation)
1745                     {
1746
1747                         AFSDbgLogMsg( AFS_SUBSYSTEM_FILE_PROCESSING,
1748                                       AFS_TRACE_LEVEL_ERROR,
1749                                       "AFSProcessCreate (%p) Failed to evaluate object %wZ FID %08lX-%08lX-%08lX-%08lX PARENT %08lX-%08lX-%08lX-%08lX Status %08lX\n",
1750                                       Irp,
1751                                       &pDirEntry->NameInformation.FileName,
1752                                       pObjectInfo->FileId.Cell,
1753                                       pObjectInfo->FileId.Volume,
1754                                       pObjectInfo->FileId.Vnode,
1755                                       pObjectInfo->FileId.Unique,
1756                                       pParentObjectInfo->FileId.Cell,
1757                                       pParentObjectInfo->FileId.Volume,
1758                                       pParentObjectInfo->FileId.Vnode,
1759                                       pParentObjectInfo->FileId.Unique,
1760                                       ntStatus);
1761                     }
1762                     else if ( pObjectInfo->ParentObjectInformation == NULL)
1763                     {
1764
1765                         AFSDbgLogMsg( AFS_SUBSYSTEM_FILE_PROCESSING,
1766                                       AFS_TRACE_LEVEL_ERROR,
1767                                       "AFSProcessCreate (%p) Failed to evaluate object %wZ FID %08lX-%08lX-%08lX-%08lX PARENT %08lX-%08lX-%08lX-%08lX != NULL Status %08lX\n",
1768                                       Irp,
1769                                       &pDirEntry->NameInformation.FileName,
1770                                       pObjectInfo->FileId.Cell,
1771                                       pObjectInfo->FileId.Volume,
1772                                       pObjectInfo->FileId.Vnode,
1773                                       pObjectInfo->FileId.Unique,
1774                                       pParentObjectInfo->FileId.Cell,
1775                                       pParentObjectInfo->FileId.Volume,
1776                                       pParentObjectInfo->FileId.Vnode,
1777                                       pParentObjectInfo->FileId.Unique,
1778                                       ntStatus);
1779                     }
1780                     else
1781                     {
1782
1783                         AFSDbgLogMsg( AFS_SUBSYSTEM_FILE_PROCESSING,
1784                                       AFS_TRACE_LEVEL_ERROR,
1785                                       "AFSProcessCreate (%p) Failed to evaluate object %wZ FID %08lX-%08lX-%08lX-%08lX PARENT %08lX-%08lX-%08lX-%08lX != %08lX-%08lX-%08lX-%08lX Status %08lX\n",
1786                                       Irp,
1787                                       &pDirEntry->NameInformation.FileName,
1788                                       pObjectInfo->FileId.Cell,
1789                                       pObjectInfo->FileId.Volume,
1790                                       pObjectInfo->FileId.Vnode,
1791                                       pObjectInfo->FileId.Unique,
1792                                       pParentObjectInfo->FileId.Cell,
1793                                       pParentObjectInfo->FileId.Volume,
1794                                       pParentObjectInfo->FileId.Vnode,
1795                                       pParentObjectInfo->FileId.Unique,
1796                                       pObjectInfo->ParentObjectInformation->FileId.Cell,
1797                                       pObjectInfo->ParentObjectInformation->FileId.Volume,
1798                                       pObjectInfo->ParentObjectInformation->FileId.Vnode,
1799                                       pObjectInfo->ParentObjectInformation->FileId.Unique,
1800                                       ntStatus);
1801                     }
1802                 }
1803                 else
1804                 {
1805
1806                     AFSDbgLogMsg( AFS_SUBSYSTEM_FILE_PROCESSING,
1807                                   AFS_TRACE_LEVEL_ERROR,
1808                                   "AFSProcessCreate (%p) Failed to evaluate object %wZ FID %08lX-%08lX-%08lX-%08lX Status %08lX\n",
1809                                   Irp,
1810                                   &pDirEntry->NameInformation.FileName,
1811                                   pObjectInfo->FileId.Cell,
1812                                   pObjectInfo->FileId.Volume,
1813                                   pObjectInfo->FileId.Vnode,
1814                                   pObjectInfo->FileId.Unique,
1815                                   ntStatus);
1816                 }
1817
1818                 try_return( ntStatus);
1819             }
1820
1821             ClearFlag( pObjectInfo->Flags, AFS_OBJECT_FLAGS_NOT_EVALUATED);
1822         }
1823
1824         //
1825         // We may have raced and the Fcb is already created
1826         //
1827
1828         //
1829         // Allocate and initialize the Fcb for the file.
1830         //
1831
1832         ntStatus = AFSInitFcb( pDirEntry);
1833
1834         *Fcb = pObjectInfo->Fcb;
1835
1836         if( !NT_SUCCESS( ntStatus))
1837         {
1838
1839             AFSDbgLogMsg( AFS_SUBSYSTEM_FILE_PROCESSING,
1840                           AFS_TRACE_LEVEL_ERROR,
1841                           "AFSProcessCreate (%p) Failed to initialize fcb %wZ Status %08lX\n",
1842                           Irp,
1843                           FullFileName,
1844                           ntStatus);
1845
1846             try_return( ntStatus);
1847         }
1848
1849         ntStatus = STATUS_SUCCESS;
1850
1851         //
1852         // Increment the open count on this Fcb
1853         //
1854
1855         lCount = InterlockedIncrement( &(*Fcb)->OpenReferenceCount);
1856
1857         AFSDbgLogMsg( AFS_SUBSYSTEM_FCB_REF_COUNTING,
1858                       AFS_TRACE_LEVEL_VERBOSE,
1859                       "AFSProcessCreate Increment count on Fcb %p Cnt %d\n",
1860                       *Fcb,
1861                       lCount);
1862
1863         bReleaseFcb = TRUE;
1864
1865         //
1866         // Initialize the Ccb for the file.
1867         //
1868
1869         ntStatus = AFSInitCcb( Ccb,
1870                                pDirEntry,
1871                                *pDesiredAccess,
1872                                0);
1873
1874         if( !NT_SUCCESS( ntStatus))
1875         {
1876
1877             AFSDbgLogMsg( AFS_SUBSYSTEM_FILE_PROCESSING,
1878                           AFS_TRACE_LEVEL_ERROR,
1879                           "AFSProcessCreate (%p) Failed to initialize ccb %wZ Status %08lX\n",
1880                           Irp,
1881                           FullFileName,
1882                           ntStatus);
1883
1884             try_return( ntStatus);
1885         }
1886
1887         bAllocatedCcb = TRUE;
1888
1889         //
1890         // If this is a file, update the headers filesizes.
1891         //
1892
1893         if( (*Fcb)->Header.NodeTypeCode == AFS_FILE_FCB)
1894         {
1895
1896             //
1897             // Update the sizes with the information passed in
1898             //
1899
1900             (*Fcb)->Header.AllocationSize.QuadPart  = pObjectInfo->AllocationSize.QuadPart;
1901             (*Fcb)->Header.FileSize.QuadPart        = pObjectInfo->EndOfFile.QuadPart;
1902             (*Fcb)->Header.ValidDataLength.QuadPart = pObjectInfo->EndOfFile.QuadPart;
1903
1904             //
1905             // Notify the system of the addition
1906             //
1907
1908             AFSFsRtlNotifyFullReportChange( pParentObjectInfo,
1909                                             *Ccb,
1910                                             (ULONG)FILE_NOTIFY_CHANGE_FILE_NAME,
1911                                             (ULONG)FILE_ACTION_ADDED);
1912
1913             (*Fcb)->NPFcb->Specific.File.ExtentsRequestStatus = STATUS_SUCCESS;
1914         }
1915         else if( (*Fcb)->Header.NodeTypeCode == AFS_DIRECTORY_FCB)
1916         {
1917
1918             //
1919             // This is a new directory node so indicate it has been enumerated
1920             //
1921
1922             SetFlag( pObjectInfo->Flags, AFS_OBJECT_FLAGS_DIRECTORY_ENUMERATED);
1923
1924             //
1925             // And the parent directory entry
1926             //
1927
1928             KeQuerySystemTime( &pParentObjectInfo->ChangeTime);
1929
1930             //
1931             // Notify the system of the addition
1932             //
1933
1934             AFSFsRtlNotifyFullReportChange( pParentObjectInfo,
1935                                             *Ccb,
1936                                             (ULONG)FILE_NOTIFY_CHANGE_DIR_NAME,
1937                                             (ULONG)FILE_ACTION_ADDED);
1938         }
1939         else if( (*Fcb)->Header.NodeTypeCode == AFS_MOUNT_POINT_FCB ||
1940                  (*Fcb)->Header.NodeTypeCode == AFS_SYMBOLIC_LINK_FCB ||
1941                  (*Fcb)->Header.NodeTypeCode == AFS_DFS_LINK_FCB ||
1942                  (*Fcb)->Header.NodeTypeCode == AFS_INVALID_FCB)
1943         {
1944
1945             //
1946             // And the parent directory entry
1947             //
1948
1949             KeQuerySystemTime( &pParentObjectInfo->ChangeTime);
1950
1951             //
1952             // Notify the system of the addition
1953             //
1954
1955             AFSFsRtlNotifyFullReportChange( pParentObjectInfo,
1956                                             *Ccb,
1957                                             (ULONG)FILE_NOTIFY_CHANGE_DIR_NAME,
1958                                             (ULONG)FILE_ACTION_ADDED);
1959         }
1960
1961         //
1962         // Save off the access for the open
1963         //
1964
1965         IoSetShareAccess( *pDesiredAccess,
1966                           usShareAccess,
1967                           pFileObject,
1968                           &(*Fcb)->ShareAccess);
1969
1970         lCount = InterlockedIncrement( &(*Fcb)->OpenHandleCount);
1971
1972         AFSDbgLogMsg( AFS_SUBSYSTEM_FCB_REF_COUNTING,
1973                       AFS_TRACE_LEVEL_VERBOSE,
1974                       "AFSProcessCreate Increment handle count on Fcb %p Cnt %d\n",
1975                       (*Fcb),
1976                       lCount);
1977
1978         //
1979         // Increment the open reference and handle on the parent node
1980         //
1981
1982         lCount = InterlockedIncrement( &pObjectInfo->ParentObjectInformation->Specific.Directory.ChildOpenHandleCount);
1983
1984         AFSDbgLogMsg( AFS_SUBSYSTEM_OBJECT_REF_COUNTING,
1985                       AFS_TRACE_LEVEL_VERBOSE,
1986                       "AFSProcessCreate Increment child open handle count on Parent object %p Cnt %d\n",
1987                       pObjectInfo->ParentObjectInformation,
1988                       lCount);
1989
1990         lCount = InterlockedIncrement( &pObjectInfo->ParentObjectInformation->Specific.Directory.ChildOpenReferenceCount);
1991
1992         AFSDbgLogMsg( AFS_SUBSYSTEM_FCB_REF_COUNTING,
1993                       AFS_TRACE_LEVEL_VERBOSE,
1994                       "AFSProcessCreate Increment child open ref count on Parent object %p Cnt %d\n",
1995                       pObjectInfo->ParentObjectInformation,
1996                       lCount);
1997
1998         if( ulOptions & FILE_DELETE_ON_CLOSE)
1999         {
2000
2001             //
2002             // Mark it for delete on close
2003             //
2004
2005             AFSDbgLogMsg( AFS_SUBSYSTEM_FILE_PROCESSING,
2006                           AFS_TRACE_LEVEL_VERBOSE,
2007                           "AFSProcessCreate (%p) Setting PENDING_DELETE flag in DirEntry %p Name %wZ\n",
2008                           Irp,
2009                           pDirEntry,
2010                           FullFileName);
2011
2012             SetFlag( pDirEntry->Flags, AFS_DIR_ENTRY_PENDING_DELETE);
2013         }
2014
2015         //
2016         // Indicate the object is locked in the service
2017         //
2018
2019         SetFlag( pObjectInfo->Flags, AFS_OBJECT_HELD_IN_SERVICE);
2020
2021         //
2022         // Return the open result for this file
2023         //
2024
2025         Irp->IoStatus.Information = FILE_CREATED;
2026
2027 try_exit:
2028
2029         //
2030         // If we created the Fcb we need to release the resources
2031         //
2032
2033         if( bReleaseFcb)
2034         {
2035
2036             if( !NT_SUCCESS( ntStatus))
2037             {
2038                 //
2039                 // Decrement the open count on this Fcb
2040                 //
2041
2042                 lCount = InterlockedDecrement( &(*Fcb)->OpenReferenceCount);
2043
2044                 AFSDbgLogMsg( AFS_SUBSYSTEM_FCB_REF_COUNTING,
2045                               AFS_TRACE_LEVEL_VERBOSE,
2046                               "AFSProcessCreate Decrement count on Fcb %p Cnt %d\n",
2047                               *Fcb,
2048                               lCount);
2049             }
2050
2051             AFSReleaseResource( &(*Fcb)->NPFcb->Resource);
2052         }
2053
2054         if( !NT_SUCCESS( ntStatus))
2055         {
2056
2057             if( bFileCreated)
2058             {
2059
2060                 AFSDbgLogMsg( AFS_SUBSYSTEM_FILE_PROCESSING,
2061                               AFS_TRACE_LEVEL_VERBOSE,
2062                               "AFSProcessCreate Create failed, removing DE %p from parent object %p Status %08lX\n",
2063                               pDirEntry,
2064                               pParentObjectInfo,
2065                               ntStatus);
2066
2067                 //
2068                 // Remove the dir entry from the parent
2069                 //
2070
2071                 AFSAcquireExcl( pParentObjectInfo->Specific.Directory.DirectoryNodeHdr.TreeLock,
2072                                 TRUE);
2073
2074                 SetFlag( pDirEntry->Flags, AFS_DIR_ENTRY_DELETED);
2075
2076                 AFSNotifyDelete( pDirEntry,
2077                                  AuthGroup,
2078                                  FALSE);
2079
2080                 //
2081                 // Decrement the reference added during initialization of the DE
2082                 //
2083
2084                 lCount = InterlockedDecrement( &pDirEntry->DirOpenReferenceCount);
2085
2086                 AFSDbgLogMsg( AFS_SUBSYSTEM_DIRENTRY_REF_COUNTING,
2087                               AFS_TRACE_LEVEL_VERBOSE,
2088                               "AFSProcessCreate Decrement count on %wZ DE %p Cnt %d\n",
2089                               &pDirEntry->NameInformation.FileName,
2090                               pDirEntry,
2091                               lCount);
2092
2093                 ASSERT( lCount >= 0);
2094
2095                 //
2096                 // Pull the directory entry from the parent
2097                 //
2098
2099                 AFSRemoveDirNodeFromParent( pParentObjectInfo,
2100                                             pDirEntry,
2101                                             FALSE); // Leave it in the enum list so the worker cleans it up
2102
2103                 //
2104                 // Tag the parent as needing verification
2105                 //
2106
2107                 SetFlag( pParentObjectInfo->Flags, AFS_OBJECT_FLAGS_VERIFY);
2108
2109                 pParentObjectInfo->DataVersion.QuadPart = (ULONGLONG)-1;
2110
2111                 AFSReleaseResource( pParentObjectInfo->Specific.Directory.DirectoryNodeHdr.TreeLock);
2112             }
2113
2114             if( bAllocatedCcb)
2115             {
2116
2117                 AFSRemoveCcb( NULL,
2118                               *Ccb);
2119             }
2120
2121             //
2122             // Fcb will be freed by AFSPrimaryVolumeWorker thread
2123             //
2124
2125             *Fcb = NULL;
2126
2127             *Ccb = NULL;
2128         }
2129     }
2130
2131     return ntStatus;
2132 }
2133
2134 NTSTATUS
2135 AFSOpenTargetDirectory( IN PIRP Irp,
2136                         IN AFSVolumeCB *VolumeCB,
2137                         IN AFSDirectoryCB *ParentDirectoryCB,
2138                         IN AFSDirectoryCB *TargetDirectoryCB,
2139                         IN UNICODE_STRING *TargetName,
2140                         OUT AFSFcb **Fcb,
2141                         OUT AFSCcb **Ccb)
2142 {
2143     UNREFERENCED_PARAMETER(VolumeCB);
2144     NTSTATUS ntStatus = STATUS_SUCCESS;
2145     PFILE_OBJECT pFileObject = NULL;
2146     PIO_STACK_LOCATION pIrpSp = IoGetCurrentIrpStackLocation( Irp);
2147     PACCESS_MASK pDesiredAccess = NULL;
2148     USHORT usShareAccess;
2149     BOOLEAN bAllocatedCcb = FALSE;
2150     BOOLEAN bReleaseFcb = FALSE;
2151     AFSObjectInfoCB *pParentObject = NULL;
2152     UNICODE_STRING uniTargetName;
2153     LONG lCount;
2154
2155     __Enter
2156     {
2157
2158         pDesiredAccess = &pIrpSp->Parameters.Create.SecurityContext->DesiredAccess;
2159         usShareAccess = pIrpSp->Parameters.Create.ShareAccess;
2160
2161         pFileObject = pIrpSp->FileObject;
2162
2163         AFSDbgLogMsg( AFS_SUBSYSTEM_FILE_PROCESSING,
2164                       AFS_TRACE_LEVEL_VERBOSE,
2165                       "AFSOpenTargetDirectory (%p) Processing file %wZ\n",
2166                       Irp,
2167                       TargetName);
2168
2169         pParentObject = ParentDirectoryCB->ObjectInformation;
2170
2171         if( pParentObject->FileType != AFS_FILE_TYPE_DIRECTORY)
2172         {
2173
2174             try_return( ntStatus = STATUS_INVALID_PARAMETER);
2175         }
2176
2177         //
2178         // Make sure we have an Fcb for the access
2179
2180         //
2181         // Allocate and initialize the Fcb for the file.
2182         //
2183
2184         ntStatus = AFSInitFcb( ParentDirectoryCB);
2185
2186         *Fcb = pParentObject->Fcb;
2187
2188         if( !NT_SUCCESS( ntStatus))
2189         {
2190
2191             AFSDbgLogMsg( AFS_SUBSYSTEM_FILE_PROCESSING,
2192                           AFS_TRACE_LEVEL_ERROR,
2193                           "AFSOpenTargetDirectory (%p) Failed to initialize fcb %wZ Status %08lX\n",
2194                           Irp,
2195                           &ParentDirectoryCB->NameInformation.FileName,
2196                           ntStatus);
2197
2198             try_return( ntStatus);
2199         }
2200
2201         ntStatus = STATUS_SUCCESS;
2202
2203         //
2204         // Increment the open count on this Fcb
2205         //
2206
2207         lCount = InterlockedIncrement( &pParentObject->Fcb->OpenReferenceCount);
2208
2209         AFSDbgLogMsg( AFS_SUBSYSTEM_FCB_REF_COUNTING,
2210                       AFS_TRACE_LEVEL_VERBOSE,
2211                       "AFSOpenTargetDirectory Increment count on Fcb %p Cnt %d\n",
2212                       pParentObject->Fcb,
2213                       lCount);
2214
2215         bReleaseFcb = TRUE;
2216
2217         //
2218         // If there are current opens on the Fcb, check the access.
2219         //
2220
2221         if( pParentObject->Fcb->OpenHandleCount > 0)
2222         {
2223
2224             ntStatus = IoCheckShareAccess( *pDesiredAccess,
2225                                            usShareAccess,
2226                                            pFileObject,
2227                                            &pParentObject->Fcb->ShareAccess,
2228                                            FALSE);
2229
2230             if( !NT_SUCCESS( ntStatus))
2231             {
2232
2233                 AFSDbgLogMsg( AFS_SUBSYSTEM_FILE_PROCESSING,
2234                               AFS_TRACE_LEVEL_ERROR,
2235                               "AFSOpenTargetDirectory (%p) Access check failure %wZ Status %08lX\n",
2236                               Irp,
2237                               &ParentDirectoryCB->NameInformation.FileName,
2238                               ntStatus);
2239
2240                 try_return( ntStatus);
2241             }
2242         }
2243
2244         //
2245         // Initialize the Ccb for the file.
2246         //
2247
2248         ntStatus = AFSInitCcb( Ccb,
2249                                ParentDirectoryCB,
2250                                *pDesiredAccess,
2251                                0);
2252
2253         if( !NT_SUCCESS( ntStatus))
2254         {
2255
2256             AFSDbgLogMsg( AFS_SUBSYSTEM_FILE_PROCESSING,
2257                           AFS_TRACE_LEVEL_ERROR,
2258                           "AFSOpenTargetDirectory (%p) Failed to initialize ccb %wZ Status %08lX\n",
2259                           Irp,
2260                           &ParentDirectoryCB->NameInformation.FileName,
2261                           ntStatus);
2262
2263             try_return( ntStatus);
2264         }
2265
2266         bAllocatedCcb = TRUE;
2267
2268         if( TargetDirectoryCB != NULL &&
2269             FsRtlAreNamesEqual( &TargetDirectoryCB->NameInformation.FileName,
2270                                 TargetName,
2271                                 FALSE,
2272                                 NULL))
2273         {
2274
2275             Irp->IoStatus.Information = FILE_EXISTS;
2276
2277             uniTargetName = TargetDirectoryCB->NameInformation.FileName;
2278         }
2279         else
2280         {
2281
2282             Irp->IoStatus.Information = FILE_DOES_NOT_EXIST;
2283
2284             uniTargetName = *TargetName;
2285         }
2286
2287         //
2288         // Update the filename in the fileobject for rename processing
2289         //
2290
2291         RtlCopyMemory( pFileObject->FileName.Buffer,
2292                        uniTargetName.Buffer,
2293                        uniTargetName.Length);
2294
2295         pFileObject->FileName.Length = uniTargetName.Length;
2296
2297         //
2298         // OK, update the share access on the fileobject
2299         //
2300
2301         if( pParentObject->Fcb->OpenHandleCount > 0)
2302         {
2303
2304             IoUpdateShareAccess( pFileObject,
2305                                  &pParentObject->Fcb->ShareAccess);
2306         }
2307         else
2308         {
2309
2310             //
2311             // Set the access
2312             //
2313
2314             IoSetShareAccess( *pDesiredAccess,
2315                               usShareAccess,
2316                               pFileObject,
2317                               &pParentObject->Fcb->ShareAccess);
2318         }
2319
2320         lCount = InterlockedIncrement( &pParentObject->Fcb->OpenHandleCount);
2321
2322         AFSDbgLogMsg( AFS_SUBSYSTEM_FCB_REF_COUNTING,
2323                       AFS_TRACE_LEVEL_VERBOSE,
2324                       "AFSOpenTargetDirectory Increment handle count on Fcb %p Cnt %d\n",
2325                       pParentObject->Fcb,
2326                       lCount);
2327
2328         //
2329         // Increment the open reference and handle on the parent node
2330         //
2331
2332         if( pParentObject->ParentObjectInformation != NULL)
2333         {
2334
2335             lCount = InterlockedIncrement( &pParentObject->ParentObjectInformation->Specific.Directory.ChildOpenHandleCount);
2336
2337             AFSDbgLogMsg( AFS_SUBSYSTEM_OBJECT_REF_COUNTING,
2338                           AFS_TRACE_LEVEL_VERBOSE,
2339                           "AFSOpenTargetDirectory Increment child open handle count on Parent object %p Cnt %d\n",
2340                           pParentObject->ParentObjectInformation,
2341                           lCount);
2342
2343             lCount = InterlockedIncrement( &pParentObject->ParentObjectInformation->Specific.Directory.ChildOpenReferenceCount);
2344
2345             AFSDbgLogMsg( AFS_SUBSYSTEM_OBJECT_REF_COUNTING,
2346                           AFS_TRACE_LEVEL_VERBOSE,
2347                           "AFSOpenTargetDirectory Increment child open ref count on Parent object %p Cnt %d\n",
2348                           pParentObject->ParentObjectInformation,
2349                           lCount);
2350         }
2351
2352 try_exit:
2353
2354         if( bReleaseFcb)
2355         {
2356
2357             if( !NT_SUCCESS( ntStatus))
2358             {
2359                 //
2360                 // Decrement the open count on this Fcb
2361                 //
2362
2363                 lCount = InterlockedDecrement( &pParentObject->Fcb->OpenReferenceCount);
2364
2365                 AFSDbgLogMsg( AFS_SUBSYSTEM_FCB_REF_COUNTING,
2366                               AFS_TRACE_LEVEL_VERBOSE,
2367                               "AFSOpenTargetDirectory Decrement count on Fcb %p Cnt %d\n",
2368                               pParentObject->Fcb,
2369                               lCount);
2370             }
2371
2372             AFSReleaseResource( &pParentObject->Fcb->NPFcb->Resource);
2373         }
2374
2375         if( !NT_SUCCESS( ntStatus))
2376         {
2377
2378             if( bAllocatedCcb)
2379             {
2380
2381                 AFSRemoveCcb( NULL,
2382                               *Ccb);
2383             }
2384
2385             *Ccb = NULL;
2386
2387             //
2388             // Fcb will be freed by AFSPrimaryVolumeWorker thread
2389             //
2390
2391             *Fcb = NULL;
2392         }
2393     }
2394
2395     return ntStatus;
2396 }
2397
2398 NTSTATUS
2399 AFSProcessOpen( IN PIRP Irp,
2400                 IN GUID *AuthGroup,
2401                 IN AFSVolumeCB *VolumeCB,
2402                 IN AFSDirectoryCB *ParentDirCB,
2403                 IN AFSDirectoryCB *DirectoryCB,
2404                 OUT AFSFcb **Fcb,
2405                 OUT AFSCcb **Ccb)
2406 {
2407     UNREFERENCED_PARAMETER(VolumeCB);
2408     NTSTATUS ntStatus = STATUS_SUCCESS;
2409     PFILE_OBJECT pFileObject = NULL;
2410     PIO_STACK_LOCATION pIrpSp = IoGetCurrentIrpStackLocation( Irp);
2411     PACCESS_MASK pDesiredAccess = NULL;
2412     USHORT usShareAccess;
2413     BOOLEAN bAllocatedCcb = FALSE, bReleaseFcb = FALSE;
2414     ULONG ulOptions = 0;
2415     AFSFileOpenCB   stOpenCB;
2416     AFSFileOpenResultCB stOpenResultCB;
2417     ULONG       ulResultLen = 0;
2418     AFSObjectInfoCB *pParentObjectInfo = NULL;
2419     AFSObjectInfoCB *pObjectInfo = NULL;
2420     ULONG       ulFileAccess = 0;
2421     AFSFileAccessReleaseCB stReleaseFileAccess;
2422     LONG lCount;
2423
2424     __Enter
2425     {
2426
2427         pDesiredAccess = &pIrpSp->Parameters.Create.SecurityContext->DesiredAccess;
2428         usShareAccess = pIrpSp->Parameters.Create.ShareAccess;
2429
2430         pFileObject = pIrpSp->FileObject;
2431
2432         pParentObjectInfo = ParentDirCB->ObjectInformation;
2433
2434         pObjectInfo = DirectoryCB->ObjectInformation;
2435
2436         //
2437         // Check if the entry is pending a deletion
2438         //
2439
2440         if( BooleanFlagOn( DirectoryCB->Flags, AFS_DIR_ENTRY_PENDING_DELETE))
2441         {
2442
2443             ntStatus = STATUS_DELETE_PENDING;
2444
2445             AFSDbgLogMsg( AFS_SUBSYSTEM_FILE_PROCESSING,
2446                           AFS_TRACE_LEVEL_ERROR,
2447                           "AFSProcessOpen (%p) Entry pending delete %wZ Status %08lX\n",
2448                           Irp,
2449                           &DirectoryCB->NameInformation.FileName,
2450                           ntStatus);
2451
2452             try_return( ntStatus);
2453         }
2454
2455         //
2456         // Extract out the options
2457         //
2458
2459         ulOptions = pIrpSp->Parameters.Create.Options;
2460
2461         //
2462         // Check if we should go and retrieve updated information for the node
2463         //
2464
2465         ntStatus = AFSValidateEntry( DirectoryCB,
2466                                      AuthGroup,
2467                                      FALSE,
2468                                      TRUE);
2469
2470         if( !NT_SUCCESS( ntStatus))
2471         {
2472
2473             AFSDbgLogMsg( AFS_SUBSYSTEM_FILE_PROCESSING,
2474                           AFS_TRACE_LEVEL_ERROR,
2475                           "AFSProcessOpen (%p) Failed to validate entry %wZ Status %08lX\n",
2476                           Irp,
2477                           &DirectoryCB->NameInformation.FileName,
2478                           ntStatus);
2479
2480             try_return( ntStatus);
2481         }
2482
2483         //
2484         // If this is marked for delete on close then be sure we can delete the entry
2485         //
2486
2487         if( BooleanFlagOn( ulOptions, FILE_DELETE_ON_CLOSE))
2488         {
2489
2490             ntStatus = AFSNotifyDelete( DirectoryCB,
2491                                         AuthGroup,
2492                                         TRUE);
2493
2494             if( !NT_SUCCESS( ntStatus))
2495             {
2496
2497                 ntStatus = STATUS_CANNOT_DELETE;
2498
2499                 AFSDbgLogMsg( AFS_SUBSYSTEM_FILE_PROCESSING,
2500                               AFS_TRACE_LEVEL_ERROR,
2501                               "AFSProcessOpen (%p) Cannot delete entry %wZ marked for delete on close Status %08lX\n",
2502                               Irp,
2503                               &DirectoryCB->NameInformation.FileName,
2504                               ntStatus);
2505
2506                 try_return( ntStatus);
2507             }
2508         }
2509
2510         //
2511         // Be sure we have an Fcb for the current object
2512         //
2513
2514         ntStatus = AFSInitFcb( DirectoryCB);
2515
2516         if( !NT_SUCCESS( ntStatus))
2517         {
2518
2519             AFSDbgLogMsg( AFS_SUBSYSTEM_FILE_PROCESSING,
2520                           AFS_TRACE_LEVEL_ERROR,
2521                           "AFSProcessOpen (%p) Failed to init fcb on %wZ Status %08lX\n",
2522                           Irp,
2523                           &DirectoryCB->NameInformation.FileName,
2524                           ntStatus);
2525
2526             try_return( ntStatus);
2527         }
2528
2529         ntStatus = STATUS_SUCCESS;
2530
2531         //
2532         // AFSInitFcb returns the Fcb resource held
2533         //
2534
2535         bReleaseFcb = TRUE;
2536
2537         //
2538         // Increment the open count on this Fcb
2539         //
2540
2541         lCount = InterlockedIncrement( &pObjectInfo->Fcb->OpenReferenceCount);
2542
2543         AFSDbgLogMsg( AFS_SUBSYSTEM_FCB_REF_COUNTING,
2544                       AFS_TRACE_LEVEL_VERBOSE,
2545                       "AFSProcessOpen Increment2 count on Fcb %p Cnt %d\n",
2546                       pObjectInfo->Fcb,
2547                       lCount);
2548
2549         //
2550         // Check access on the entry
2551         //
2552
2553         if( pObjectInfo->Fcb->OpenHandleCount > 0)
2554         {
2555
2556             ntStatus = IoCheckShareAccess( *pDesiredAccess,
2557                                            usShareAccess,
2558                                            pFileObject,
2559                                            &pObjectInfo->Fcb->ShareAccess,
2560                                            FALSE);
2561
2562             if( !NT_SUCCESS( ntStatus))
2563             {
2564
2565                 AFSDbgLogMsg( AFS_SUBSYSTEM_FILE_PROCESSING,
2566                               AFS_TRACE_LEVEL_ERROR,
2567                               "AFSProcessOpen (%p) Failed to check share access on %wZ Status %08lX\n",
2568                               Irp,
2569                               &DirectoryCB->NameInformation.FileName,
2570                               ntStatus);
2571
2572                 try_return( ntStatus);
2573             }
2574         }
2575
2576         //
2577         // Additional checks
2578         //
2579
2580         if( pObjectInfo->Fcb->Header.NodeTypeCode == AFS_FILE_FCB)
2581         {
2582
2583             //
2584             // If the caller is asking for write access then try to flush the image section
2585             //
2586
2587             if( FlagOn( *pDesiredAccess, FILE_WRITE_DATA) ||
2588                 BooleanFlagOn(ulOptions, FILE_DELETE_ON_CLOSE))
2589             {
2590
2591                 BOOLEAN bMmFlushed;
2592
2593                 AFSDbgLogMsg( AFS_SUBSYSTEM_LOCK_PROCESSING,
2594                               AFS_TRACE_LEVEL_VERBOSE,
2595                               "AFSProcessOpen Acquiring Fcb SectionObject lock %p EXCL %08lX\n",
2596                               &pObjectInfo->Fcb->NPFcb->SectionObjectResource,
2597                               PsGetCurrentThread());
2598
2599                 AFSAcquireExcl( &pObjectInfo->Fcb->NPFcb->SectionObjectResource,
2600                                 TRUE);
2601
2602                 bMmFlushed = MmFlushImageSection( &pObjectInfo->Fcb->NPFcb->SectionObjectPointers,
2603                                                   MmFlushForWrite);
2604
2605                 AFSDbgLogMsg( AFS_SUBSYSTEM_LOCK_PROCESSING,
2606                               AFS_TRACE_LEVEL_VERBOSE,
2607                               "AFSProcessOpen Releasing Fcb SectionObject lock %p EXCL %08lX\n",
2608                               &pObjectInfo->Fcb->NPFcb->SectionObjectResource,
2609                               PsGetCurrentThread());
2610
2611                 AFSReleaseResource( &pObjectInfo->Fcb->NPFcb->SectionObjectResource);
2612
2613                 if ( !bMmFlushed)
2614                 {
2615
2616                     ntStatus = BooleanFlagOn(ulOptions, FILE_DELETE_ON_CLOSE) ? STATUS_CANNOT_DELETE :
2617                                                                             STATUS_SHARING_VIOLATION;
2618
2619                     AFSDbgLogMsg( AFS_SUBSYSTEM_FILE_PROCESSING,
2620                                   AFS_TRACE_LEVEL_ERROR,
2621                                   "AFSProcessOpen (%p) Failed to flush image section %wZ Status %08lX\n",
2622                                   Irp,
2623                                   &DirectoryCB->NameInformation.FileName,
2624                                   ntStatus);
2625
2626                     try_return( ntStatus);
2627                 }
2628             }
2629
2630             if( BooleanFlagOn( ulOptions, FILE_DIRECTORY_FILE))
2631             {
2632
2633                 ntStatus = STATUS_NOT_A_DIRECTORY;
2634
2635                 AFSDbgLogMsg( AFS_SUBSYSTEM_FILE_PROCESSING,
2636                               AFS_TRACE_LEVEL_ERROR,
2637                               "AFSProcessOpen (%p) Attempt to open file as directory %wZ Status %08lX\n",
2638                               Irp,
2639                               &DirectoryCB->NameInformation.FileName,
2640                               ntStatus);
2641
2642                 try_return( ntStatus);
2643             }
2644
2645             pObjectInfo->Fcb->NPFcb->Specific.File.ExtentsRequestStatus = STATUS_SUCCESS;
2646         }
2647         else if( pObjectInfo->Fcb->Header.NodeTypeCode == AFS_DIRECTORY_FCB ||
2648                  pObjectInfo->Fcb->Header.NodeTypeCode == AFS_ROOT_FCB)
2649         {
2650
2651             if( BooleanFlagOn( ulOptions, FILE_NON_DIRECTORY_FILE))
2652             {
2653
2654                 ntStatus = STATUS_FILE_IS_A_DIRECTORY;
2655
2656                 AFSDbgLogMsg( AFS_SUBSYSTEM_FILE_PROCESSING,
2657                               AFS_TRACE_LEVEL_ERROR,
2658                               "AFSProcessOpen (%p) Attempt to open directory as file %wZ Status %08lX\n",
2659                               Irp,
2660                               &DirectoryCB->NameInformation.FileName,
2661                               ntStatus);
2662
2663                 try_return( ntStatus);
2664             }
2665         }
2666         else if( pObjectInfo->Fcb->Header.NodeTypeCode == AFS_MOUNT_POINT_FCB ||
2667                  pObjectInfo->Fcb->Header.NodeTypeCode == AFS_SYMBOLIC_LINK_FCB ||
2668                  pObjectInfo->Fcb->Header.NodeTypeCode == AFS_DFS_LINK_FCB ||
2669                  pObjectInfo->Fcb->Header.NodeTypeCode == AFS_INVALID_FCB)
2670         {
2671
2672         }
2673         else
2674         {
2675             ASSERT( FALSE);
2676             try_return( ntStatus = STATUS_UNSUCCESSFUL);
2677         }
2678
2679         //
2680         // Check with the service that we can open the file
2681         //
2682
2683         stOpenCB.ParentId = pParentObjectInfo->FileId;
2684
2685         stOpenCB.DesiredAccess = *pDesiredAccess;
2686
2687         stOpenCB.ShareAccess = usShareAccess;
2688
2689         stOpenCB.ProcessId = (ULONGLONG)PsGetCurrentProcessId();
2690
2691         stOpenCB.Identifier = (ULONGLONG)pFileObject;
2692
2693         stOpenResultCB.GrantedAccess = 0;
2694
2695         ulResultLen = sizeof( AFSFileOpenResultCB);
2696
2697         ntStatus = AFSProcessRequest( AFS_REQUEST_TYPE_OPEN_FILE,
2698                                       AFS_REQUEST_FLAG_SYNCHRONOUS | AFS_REQUEST_FLAG_HOLD_FID,
2699                                       AuthGroup,
2700                                       &DirectoryCB->NameInformation.FileName,
2701                                       &pObjectInfo->FileId,
2702                                       (void *)&stOpenCB,
2703                                       sizeof( AFSFileOpenCB),
2704                                       (void *)&stOpenResultCB,
2705                                       &ulResultLen);
2706
2707         if( !NT_SUCCESS( ntStatus))
2708         {
2709
2710             AFSDbgLogMsg( AFS_SUBSYSTEM_FILE_PROCESSING,
2711                           AFS_TRACE_LEVEL_ERROR,
2712                           "AFSProcessOpen (%p) Failed open in service %wZ Status %08lX\n",
2713                           Irp,
2714                           &DirectoryCB->NameInformation.FileName,
2715                           ntStatus);
2716
2717             try_return( ntStatus);
2718         }
2719
2720         //
2721         // Save the granted access in case we need to release it below
2722         //
2723
2724         ulFileAccess = stOpenResultCB.FileAccess;
2725
2726         //
2727         // Check if there is a conflict
2728         //
2729
2730         if( !AFSCheckAccess( *pDesiredAccess,
2731                              stOpenResultCB.GrantedAccess,
2732                              BooleanFlagOn( DirectoryCB->ObjectInformation->FileAttributes, FILE_ATTRIBUTE_DIRECTORY)))
2733         {
2734
2735             ntStatus = STATUS_ACCESS_DENIED;
2736
2737             AFSDbgLogMsg( AFS_SUBSYSTEM_FILE_PROCESSING,
2738                           AFS_TRACE_LEVEL_ERROR,
2739                           "AFSProcessOpen (%p) Failed to check access from service Desired %08lX Granted %08lX Entry %wZ Status %08lX\n",
2740                           Irp,
2741                           *pDesiredAccess,
2742                           stOpenResultCB.GrantedAccess,
2743                           &DirectoryCB->NameInformation.FileName,
2744                           ntStatus);
2745
2746             try_return( ntStatus);
2747         }
2748
2749         //
2750         // Initialize the Ccb for the file.
2751         //
2752
2753         ntStatus = AFSInitCcb( Ccb,
2754                                DirectoryCB,
2755                                *pDesiredAccess,
2756                                ulFileAccess);
2757
2758         if( !NT_SUCCESS( ntStatus))
2759         {
2760
2761             AFSDbgLogMsg( AFS_SUBSYSTEM_FILE_PROCESSING,
2762                           AFS_TRACE_LEVEL_ERROR,
2763                           "AFSProcessOpen (%p) Failed to initialize ccb %wZ Status %08lX\n",
2764                           Irp,
2765                           &DirectoryCB->NameInformation.FileName,
2766                           ntStatus);
2767
2768             try_return( ntStatus);
2769         }
2770
2771         bAllocatedCcb = TRUE;
2772
2773         //
2774         // Perform the access check on the target if this is a mount point or symlink
2775         //
2776
2777         if( pObjectInfo->Fcb->OpenHandleCount > 0)
2778         {
2779
2780             IoUpdateShareAccess( pFileObject,
2781                                  &pObjectInfo->Fcb->ShareAccess);
2782         }
2783         else
2784         {
2785
2786             //
2787             // Set the access
2788             //
2789
2790             IoSetShareAccess( *pDesiredAccess,
2791                               usShareAccess,
2792                               pFileObject,
2793                               &pObjectInfo->Fcb->ShareAccess);
2794         }
2795
2796         lCount = InterlockedIncrement( &pObjectInfo->Fcb->OpenHandleCount);
2797
2798         AFSDbgLogMsg( AFS_SUBSYSTEM_FCB_REF_COUNTING,
2799                       AFS_TRACE_LEVEL_VERBOSE,
2800                       "AFSProcessOpen Increment handle count on Fcb %p Cnt %d\n",
2801                       pObjectInfo->Fcb,
2802                       lCount);
2803
2804         //
2805         // Increment the open reference and handle on the parent node
2806         //
2807
2808         lCount = InterlockedIncrement( &pObjectInfo->ParentObjectInformation->Specific.Directory.ChildOpenHandleCount);
2809
2810         AFSDbgLogMsg( AFS_SUBSYSTEM_OBJECT_REF_COUNTING,
2811                       AFS_TRACE_LEVEL_VERBOSE,
2812                       "AFSProcessOpen Increment child open handle count on Parent object %p Cnt %d\n",
2813                       pObjectInfo->ParentObjectInformation,
2814                       lCount);
2815
2816         lCount = InterlockedIncrement( &pObjectInfo->ParentObjectInformation->Specific.Directory.ChildOpenReferenceCount);
2817
2818         AFSDbgLogMsg( AFS_SUBSYSTEM_OBJECT_REF_COUNTING,
2819                       AFS_TRACE_LEVEL_VERBOSE,
2820                       "AFSProcessOpen Increment child open ref count on Parent object %p Cnt %d\n",
2821                       pObjectInfo->ParentObjectInformation,
2822                       lCount);
2823
2824         if( BooleanFlagOn( ulOptions, FILE_DELETE_ON_CLOSE))
2825         {
2826
2827             //
2828             // Mark it for delete on close
2829             //
2830
2831             AFSDbgLogMsg( AFS_SUBSYSTEM_FILE_PROCESSING,
2832                           AFS_TRACE_LEVEL_VERBOSE,
2833                           "AFSProcessOpen (%p) Setting PENDING_DELETE flag in DirEntry %p Name %wZ\n",
2834                           Irp,
2835                           DirectoryCB,
2836                           &DirectoryCB->NameInformation.FileName);
2837
2838             SetFlag( DirectoryCB->Flags, AFS_DIR_ENTRY_PENDING_DELETE);
2839         }
2840
2841         //
2842         // Indicate the object is held
2843         //
2844
2845         SetFlag( pObjectInfo->Flags, AFS_OBJECT_HELD_IN_SERVICE);
2846
2847         //
2848         // Return the open result for this file
2849         //
2850
2851         Irp->IoStatus.Information = FILE_OPENED;
2852
2853         *Fcb = pObjectInfo->Fcb;
2854
2855 try_exit:
2856
2857         if( bReleaseFcb)
2858         {
2859
2860             if( !NT_SUCCESS( ntStatus))
2861             {
2862                 //
2863                 // Decrement the open count on this Fcb
2864                 //
2865
2866                 lCount = InterlockedDecrement( &pObjectInfo->Fcb->OpenReferenceCount);
2867
2868                 AFSDbgLogMsg( AFS_SUBSYSTEM_FCB_REF_COUNTING,
2869                               AFS_TRACE_LEVEL_VERBOSE,
2870                               "AFSProcessOpen Decrement2 count on Fcb %p Cnt %d\n",
2871                               pObjectInfo->Fcb,
2872                               lCount);
2873             }
2874
2875             AFSReleaseResource( pObjectInfo->Fcb->Header.Resource);
2876         }
2877
2878         if( !NT_SUCCESS( ntStatus))
2879         {
2880
2881             if ( ulFileAccess > 0)
2882             {
2883
2884                 stReleaseFileAccess.ProcessId = (ULONGLONG)PsGetCurrentProcessId();
2885
2886                 stReleaseFileAccess.FileAccess = ulFileAccess;
2887
2888                 stReleaseFileAccess.Identifier = (ULONGLONG)pFileObject;
2889
2890                 AFSProcessRequest( AFS_REQUEST_TYPE_RELEASE_FILE_ACCESS,
2891                                    AFS_REQUEST_FLAG_SYNCHRONOUS,
2892                                    AuthGroup,
2893                                    &DirectoryCB->NameInformation.FileName,
2894                                    &pObjectInfo->FileId,
2895                                    (void *)&stReleaseFileAccess,
2896                                    sizeof( AFSFileAccessReleaseCB),
2897                                    NULL,
2898                                    NULL);
2899             }
2900
2901             if( bAllocatedCcb)
2902             {
2903
2904                 AFSRemoveCcb( NULL,
2905                               *Ccb);
2906             }
2907
2908             *Ccb = NULL;
2909
2910             //
2911             // Fcb will be freed by AFSPrimaryVolumeWorker thread
2912             //
2913
2914             *Fcb = NULL;
2915         }
2916     }
2917
2918     return ntStatus;
2919 }
2920
2921 NTSTATUS
2922 AFSProcessOverwriteSupersede( IN PDEVICE_OBJECT DeviceObject,
2923                               IN PIRP           Irp,
2924                               IN AFSVolumeCB   *VolumeCB,
2925                               IN GUID          *AuthGroup,
2926                               IN AFSDirectoryCB *ParentDirCB,
2927                               IN AFSDirectoryCB *DirectoryCB,
2928                               OUT AFSFcb       **Fcb,
2929                               OUT AFSCcb       **Ccb)
2930 {
2931     UNREFERENCED_PARAMETER(DeviceObject);
2932     NTSTATUS ntStatus = STATUS_SUCCESS;
2933     PIO_STACK_LOCATION pIrpSp = IoGetCurrentIrpStackLocation( Irp);
2934     PFILE_OBJECT pFileObject = NULL;
2935     LARGE_INTEGER liZero = {0,0};
2936     BOOLEAN bReleasePaging = FALSE, bReleaseFcb = FALSE;
2937     ULONG   ulAttributes = 0;
2938     ULONG ulCreateDisposition = 0;
2939     BOOLEAN bAllocatedCcb = FALSE;
2940     BOOLEAN bUserMapped = FALSE;
2941     PACCESS_MASK pDesiredAccess = NULL;
2942     USHORT usShareAccess;
2943     AFSObjectInfoCB *pParentObjectInfo = NULL;
2944     AFSObjectInfoCB *pObjectInfo = NULL;
2945     LONG lCount;
2946     LARGE_INTEGER liSaveSize;
2947     LARGE_INTEGER liSaveVDL;
2948     LARGE_INTEGER liSaveAlloc;
2949
2950     __Enter
2951     {
2952
2953         pDesiredAccess = &pIrpSp->Parameters.Create.SecurityContext->DesiredAccess;
2954
2955         usShareAccess = pIrpSp->Parameters.Create.ShareAccess;
2956
2957         pFileObject = pIrpSp->FileObject;
2958
2959         ulAttributes = pIrpSp->Parameters.Create.FileAttributes;
2960
2961         ulCreateDisposition = (pIrpSp->Parameters.Create.Options >> 24) & 0x000000ff;
2962
2963         if( BooleanFlagOn( VolumeCB->VolumeInformation.Characteristics, FILE_READ_ONLY_DEVICE))
2964         {
2965
2966             AFSDbgLogMsg( AFS_SUBSYSTEM_FILE_PROCESSING,
2967                           AFS_TRACE_LEVEL_ERROR,
2968                           "AFSProcessOverwriteSupersede Request failed on %wZ due to read only volume\n",
2969                           Irp,
2970                           &DirectoryCB->NameInformation.FileName);
2971
2972             try_return( ntStatus = STATUS_MEDIA_WRITE_PROTECTED);
2973         }
2974
2975         pParentObjectInfo = ParentDirCB->ObjectInformation;
2976
2977         pObjectInfo = DirectoryCB->ObjectInformation;
2978
2979         //
2980         // Check if we should go and retrieve updated information for the node
2981         //
2982
2983         ntStatus = AFSValidateEntry( DirectoryCB,
2984                                      AuthGroup,
2985                                      FALSE,
2986                                      TRUE);
2987
2988         if( !NT_SUCCESS( ntStatus))
2989         {
2990
2991             AFSDbgLogMsg( AFS_SUBSYSTEM_FILE_PROCESSING,
2992                           AFS_TRACE_LEVEL_ERROR,
2993                           "AFSProcessOverwriteSupersede (%p) Failed to validate entry %wZ Status %08lX\n",
2994                           Irp,
2995                           &DirectoryCB->NameInformation.FileName,
2996                           ntStatus);
2997
2998             try_return( ntStatus);
2999         }
3000
3001         //
3002         // Be sure we have an Fcb for the object block
3003         //
3004
3005         ntStatus = AFSInitFcb( DirectoryCB);
3006
3007         *Fcb = pObjectInfo->Fcb;
3008
3009         if( !NT_SUCCESS( ntStatus))
3010         {
3011
3012             AFSDbgLogMsg( AFS_SUBSYSTEM_FILE_PROCESSING,
3013                           AFS_TRACE_LEVEL_ERROR,
3014                           "AFSProcessOverwriteSupersede (%p) Failed to initialize fcb %wZ Status %08lX\n",
3015                           Irp,
3016                           &DirectoryCB->NameInformation.FileName,
3017                           ntStatus);
3018
3019             try_return( ntStatus);
3020         }
3021
3022         ntStatus = STATUS_SUCCESS;
3023
3024         //
3025         // Increment the open count on this Fcb.
3026         //
3027
3028         lCount = InterlockedIncrement( &pObjectInfo->Fcb->OpenReferenceCount);
3029
3030         AFSDbgLogMsg( AFS_SUBSYSTEM_FCB_REF_COUNTING,
3031                       AFS_TRACE_LEVEL_VERBOSE,
3032                       "AFSProcessOverwriteSupersede Increment2 count on Fcb %p Cnt %d\n",
3033                       pObjectInfo->Fcb,
3034                       lCount);
3035
3036         bReleaseFcb = TRUE;
3037
3038         //
3039         // Check access on the entry
3040         //
3041
3042         if( pObjectInfo->Fcb->OpenHandleCount > 0)
3043         {
3044
3045             ntStatus = IoCheckShareAccess( *pDesiredAccess,
3046                                            usShareAccess,
3047                                            pFileObject,
3048                                            &pObjectInfo->Fcb->ShareAccess,
3049                                            FALSE);
3050
3051             if( !NT_SUCCESS( ntStatus))
3052             {
3053
3054                 AFSDbgLogMsg( AFS_SUBSYSTEM_FILE_PROCESSING,
3055                               AFS_TRACE_LEVEL_ERROR,
3056                               "AFSProcessOverwriteSupersede (%p) Access check failure %wZ Status %08lX\n",
3057                               Irp,
3058                               &DirectoryCB->NameInformation.FileName,
3059                               ntStatus);
3060
3061                 try_return( ntStatus);
3062             }
3063         }
3064
3065         AFSDbgLogMsg( AFS_SUBSYSTEM_LOCK_PROCESSING,
3066                       AFS_TRACE_LEVEL_VERBOSE,
3067                       "AFSProcessOverwriteSupercede Acquiring Fcb SectionObject lock %p EXCL %08lX\n",
3068                       &pObjectInfo->Fcb->NPFcb->SectionObjectResource,
3069                       PsGetCurrentThread());
3070
3071         AFSAcquireExcl( &pObjectInfo->Fcb->NPFcb->SectionObjectResource,
3072                         TRUE);
3073
3074         //
3075         //  Before we actually truncate, check to see if the purge
3076         //  is going to fail.
3077         //
3078
3079         bUserMapped = !MmCanFileBeTruncated( &pObjectInfo->Fcb->NPFcb->SectionObjectPointers,
3080                                              &liZero);
3081
3082         AFSDbgLogMsg( AFS_SUBSYSTEM_LOCK_PROCESSING,
3083                       AFS_TRACE_LEVEL_VERBOSE,
3084                       "AFSProcessOverwriteSupercede Releasing Fcb SectionObject lock %p EXCL %08lX\n",
3085                       &pObjectInfo->Fcb->NPFcb->SectionObjectResource,
3086                       PsGetCurrentThread());
3087
3088         AFSReleaseResource( &pObjectInfo->Fcb->NPFcb->SectionObjectResource);
3089
3090         if( bUserMapped)
3091         {
3092
3093             ntStatus = STATUS_USER_MAPPED_FILE;
3094
3095             AFSDbgLogMsg( AFS_SUBSYSTEM_FILE_PROCESSING,
3096                           AFS_TRACE_LEVEL_ERROR,
3097                           "AFSProcessOverwriteSupersede (%p) File user mapped %wZ Status %08lX\n",
3098                           Irp,
3099                           &DirectoryCB->NameInformation.FileName,
3100                           ntStatus);
3101
3102             try_return( ntStatus);
3103         }
3104
3105         //
3106         // Initialize the Ccb for the file.
3107         //
3108
3109         ntStatus = AFSInitCcb( Ccb,
3110                                DirectoryCB,
3111                                *pDesiredAccess,
3112                                0);
3113
3114         if( !NT_SUCCESS( ntStatus))
3115         {
3116
3117             AFSDbgLogMsg( AFS_SUBSYSTEM_FILE_PROCESSING,
3118                           AFS_TRACE_LEVEL_ERROR,
3119                           "AFSProcessOverwriteSupersede (%p) Failed to initialize ccb %wZ Status %08lX\n",
3120                           Irp,
3121                           &DirectoryCB->NameInformation.FileName,
3122                           ntStatus);
3123
3124             try_return( ntStatus);
3125         }
3126
3127         bAllocatedCcb = TRUE;
3128
3129         //
3130         // Set the file length to zero
3131         //
3132
3133         AFSAcquireExcl( pObjectInfo->Fcb->Header.PagingIoResource,
3134                         TRUE);
3135
3136         bReleasePaging = TRUE;
3137
3138         liSaveSize = pObjectInfo->Fcb->Header.FileSize;
3139         liSaveAlloc = pObjectInfo->Fcb->Header.AllocationSize;
3140         liSaveVDL = pObjectInfo->Fcb->Header.ValidDataLength;
3141
3142         pObjectInfo->Fcb->Header.FileSize.QuadPart = 0;
3143         pObjectInfo->Fcb->Header.ValidDataLength.QuadPart = 0;
3144         pObjectInfo->Fcb->Header.AllocationSize.QuadPart = 0;
3145
3146         pObjectInfo->EndOfFile.QuadPart = 0;
3147         pObjectInfo->AllocationSize.QuadPart = 0;
3148
3149         //
3150         // Trim down the extents. We do this BEFORE telling the service
3151         // the file is truncated since there is a potential race between
3152         // a worker thread releasing extents and us trimming
3153         //
3154
3155         AFSTrimExtents( pObjectInfo->Fcb,
3156                         &pObjectInfo->Fcb->Header.FileSize);
3157
3158         KeQuerySystemTime( &pObjectInfo->ChangeTime);
3159
3160         KeQuerySystemTime( &pObjectInfo->LastAccessTime);
3161
3162         KeQuerySystemTime( &pObjectInfo->LastWriteTime);
3163
3164         //
3165         // Set the update flag accordingly
3166         //
3167
3168         SetFlag( pObjectInfo->Fcb->Flags, AFS_FCB_FLAG_FILE_MODIFIED |
3169                                           AFS_FCB_FLAG_UPDATE_CREATE_TIME |
3170                                           AFS_FCB_FLAG_UPDATE_CHANGE_TIME |
3171                                           AFS_FCB_FLAG_UPDATE_ACCESS_TIME |
3172                                           AFS_FCB_FLAG_UPDATE_LAST_WRITE_TIME);
3173
3174         ntStatus = AFSUpdateFileInformation( &pParentObjectInfo->FileId,
3175                                              pObjectInfo,
3176                                              AuthGroup);
3177
3178         if( !NT_SUCCESS( ntStatus))
3179         {
3180
3181             pObjectInfo->Fcb->Header.ValidDataLength = liSaveVDL;
3182             pObjectInfo->Fcb->Header.FileSize = liSaveSize;
3183             pObjectInfo->Fcb->Header.AllocationSize = liSaveAlloc;
3184             pObjectInfo->Fcb->ObjectInformation->EndOfFile = liSaveSize;
3185             pObjectInfo->Fcb->ObjectInformation->AllocationSize = liSaveAlloc;
3186
3187             AFSDbgLogMsg( AFS_SUBSYSTEM_FILE_PROCESSING,
3188                           AFS_TRACE_LEVEL_ERROR,
3189                           "AFSProcessOverwriteSupersede (%p) Failed to update file information %wZ Status %08lX\n",
3190                           Irp,
3191                           &DirectoryCB->NameInformation.FileName,
3192                           ntStatus);
3193
3194             try_return( ntStatus);
3195         }
3196
3197         ulAttributes |= FILE_ATTRIBUTE_ARCHIVE;
3198
3199         if( ulCreateDisposition == FILE_SUPERSEDE)
3200         {
3201
3202             pObjectInfo->FileAttributes = ulAttributes;
3203
3204         }
3205         else
3206         {
3207
3208             pObjectInfo->FileAttributes |= ulAttributes;
3209         }
3210
3211         //
3212         // Save off the access for the open
3213         //
3214
3215         if( pObjectInfo->Fcb->OpenHandleCount > 0)
3216         {
3217
3218             IoUpdateShareAccess( pFileObject,
3219                                  &pObjectInfo->Fcb->ShareAccess);
3220         }
3221         else
3222         {
3223
3224             //
3225             // Set the access
3226             //
3227
3228             IoSetShareAccess( *pDesiredAccess,
3229                               usShareAccess,
3230                               pFileObject,
3231                               &pObjectInfo->Fcb->ShareAccess);
3232         }
3233
3234         //
3235         // Return the correct action
3236         //
3237
3238         if( ulCreateDisposition == FILE_SUPERSEDE)
3239         {
3240
3241             Irp->IoStatus.Information = FILE_SUPERSEDED;
3242         }
3243         else
3244         {
3245
3246             Irp->IoStatus.Information = FILE_OVERWRITTEN;
3247         }
3248
3249         lCount = InterlockedIncrement( &pObjectInfo->Fcb->OpenHandleCount);
3250
3251         AFSDbgLogMsg( AFS_SUBSYSTEM_FCB_REF_COUNTING,
3252                       AFS_TRACE_LEVEL_VERBOSE,
3253                       "AFSProcessOverwriteSupersede Increment handle count on Fcb %p Cnt %d\n",
3254                       pObjectInfo->Fcb,
3255                       lCount);
3256
3257         //
3258         // Increment the open reference and handle on the parent node
3259         //
3260
3261         lCount = InterlockedIncrement( &pObjectInfo->ParentObjectInformation->Specific.Directory.ChildOpenHandleCount);
3262
3263         AFSDbgLogMsg( AFS_SUBSYSTEM_OBJECT_REF_COUNTING,
3264                       AFS_TRACE_LEVEL_VERBOSE,
3265                       "AFSProcessOverwriteSupersede Increment child open handle count on Parent object %p Cnt %d\n",
3266                       pObjectInfo->ParentObjectInformation,
3267                       lCount);
3268
3269         lCount = InterlockedIncrement( &pObjectInfo->ParentObjectInformation->Specific.Directory.ChildOpenReferenceCount);
3270
3271         AFSDbgLogMsg( AFS_SUBSYSTEM_OBJECT_REF_COUNTING,
3272                       AFS_TRACE_LEVEL_VERBOSE,
3273                       "AFSProcessOverwriteSupersede Increment child open ref count on Parent object %p Cnt %d\n",
3274                       pObjectInfo->ParentObjectInformation,
3275                       lCount);
3276
3277         AFSReleaseResource( pObjectInfo->Fcb->Header.Resource);
3278
3279         bReleaseFcb = FALSE;
3280
3281         *Fcb = pObjectInfo->Fcb;
3282
3283         //
3284         // Now that the Fcb->Resource has been dropped
3285         // we can call CcSetFileSizes.  We are still holding
3286         // the PagingIoResource
3287         //
3288
3289         pFileObject->SectionObjectPointer = &pObjectInfo->Fcb->NPFcb->SectionObjectPointers;
3290
3291         pFileObject->FsContext = (void *)pObjectInfo->Fcb;
3292
3293         pFileObject->FsContext2 = (void *)*Ccb;
3294
3295         CcSetFileSizes( pFileObject,
3296                         (PCC_FILE_SIZES)&pObjectInfo->Fcb->Header.AllocationSize);
3297
3298 try_exit:
3299
3300         if( bReleasePaging)
3301         {
3302
3303             AFSReleaseResource( pObjectInfo->Fcb->Header.PagingIoResource);
3304         }
3305
3306         if( bReleaseFcb)
3307         {
3308
3309             if( !NT_SUCCESS( ntStatus))
3310             {
3311                 //
3312                 // Decrement the open count on this Fcb.
3313                 //
3314
3315                 lCount = InterlockedDecrement( &pObjectInfo->Fcb->OpenReferenceCount);
3316
3317                 AFSDbgLogMsg( AFS_SUBSYSTEM_FCB_REF_COUNTING,
3318                               AFS_TRACE_LEVEL_VERBOSE,
3319                               "AFSProcessOverwriteSupersede Decrement2 count on Fcb %p Cnt %d\n",
3320                               pObjectInfo->Fcb,
3321                               lCount);
3322             }
3323
3324             AFSReleaseResource( pObjectInfo->Fcb->Header.Resource);
3325         }
3326
3327         if( !NT_SUCCESS( ntStatus))
3328         {
3329
3330             if( bAllocatedCcb)
3331             {
3332
3333                 AFSRemoveCcb( NULL,
3334                               *Ccb);
3335             }
3336
3337             *Ccb = NULL;
3338
3339             //
3340             // Fcb will be freed by AFSPrimaryVolumeWorker thread
3341             //
3342
3343             *Fcb = NULL;
3344         }
3345     }
3346
3347     return ntStatus;
3348 }
3349
3350 NTSTATUS
3351 AFSControlDeviceCreate( IN PIRP Irp)
3352 {
3353
3354     NTSTATUS ntStatus = STATUS_SUCCESS;
3355
3356     __Enter
3357     {
3358
3359         if ( KernelMode == Irp->RequestorMode) {
3360             //
3361             // For now, just let the open happen
3362             //
3363             Irp->IoStatus.Information = FILE_OPENED;
3364         }
3365         else
3366         {
3367             //
3368             // Not from usermode, All access must be via
3369             // the FS component (which will do the
3370             // security check)
3371             //
3372             ntStatus = STATUS_ACCESS_DENIED;
3373         }
3374     }
3375
3376     return ntStatus;
3377 }
3378
3379 //
3380 // AFSOpenIOCtlFcb does not release a DirOpenReferenceCount on
3381 // the ParentDirCB.
3382 //
3383
3384 NTSTATUS
3385 AFSOpenIOCtlFcb( IN PIRP Irp,
3386                  IN GUID *AuthGroup,
3387                  IN AFSDirectoryCB *ParentDirCB,
3388                  OUT AFSFcb **Fcb,
3389                  OUT AFSCcb **Ccb)
3390 {
3391
3392     NTSTATUS ntStatus = STATUS_SUCCESS;
3393     PFILE_OBJECT pFileObject = NULL;
3394     PIO_STACK_LOCATION pIrpSp = IoGetCurrentIrpStackLocation( Irp);
3395     BOOLEAN bReleaseFcb = FALSE, bAllocatedCcb = FALSE;
3396     AFSPIOCtlOpenCloseRequestCB stPIOCtlOpen;
3397     AFSFileID stFileID;
3398     AFSObjectInfoCB *pParentObjectInfo = NULL;
3399     LONG lCount;
3400
3401     __Enter
3402     {
3403
3404         pFileObject = pIrpSp->FileObject;
3405
3406         pParentObjectInfo = ParentDirCB->ObjectInformation;
3407
3408         //
3409         // If we haven't initialized the PIOCtl DirectoryCB for this directory then do it now
3410         //
3411
3412         if( pParentObjectInfo->Specific.Directory.PIOCtlDirectoryCB == NULL)
3413         {
3414
3415             ntStatus = AFSInitPIOCtlDirectoryCB( pParentObjectInfo);
3416
3417             if( !NT_SUCCESS( ntStatus))
3418             {
3419
3420                 try_return( ntStatus);
3421             }
3422         }
3423
3424         //
3425         // Allocate and initialize the Fcb for the file.
3426         //
3427
3428         ntStatus = AFSInitFcb( pParentObjectInfo->Specific.Directory.PIOCtlDirectoryCB);
3429
3430         *Fcb = pParentObjectInfo->Specific.Directory.PIOCtlDirectoryCB->ObjectInformation->Fcb;
3431
3432         if( !NT_SUCCESS( ntStatus))
3433         {
3434
3435             AFSDbgLogMsg( AFS_SUBSYSTEM_FILE_PROCESSING,
3436                           AFS_TRACE_LEVEL_ERROR,
3437                           "AFSOpenIOCtlFcb (%p) Failed to initialize fcb Status %08lX\n",
3438                           Irp,
3439                           ntStatus);
3440
3441             try_return( ntStatus);
3442         }
3443
3444         ntStatus = STATUS_SUCCESS;
3445
3446         //
3447         // Increment the open reference and handle on the node
3448         //
3449
3450         lCount = InterlockedIncrement( &(*Fcb)->OpenReferenceCount);
3451
3452         AFSDbgLogMsg( AFS_SUBSYSTEM_FCB_REF_COUNTING,
3453                       AFS_TRACE_LEVEL_VERBOSE,
3454                       "AFSOpenIOCtlFcb Increment count on Fcb %p Cnt %d\n",
3455                       (*Fcb),
3456                       lCount);
3457
3458         bReleaseFcb = TRUE;
3459
3460         //
3461         // Initialize the Ccb for the file.
3462         //
3463
3464         ntStatus = AFSInitCcb( Ccb,
3465                                pParentObjectInfo->Specific.Directory.PIOCtlDirectoryCB,
3466                                0,
3467                                0);
3468
3469         if( !NT_SUCCESS( ntStatus))
3470         {
3471
3472             AFSDbgLogMsg( AFS_SUBSYSTEM_FILE_PROCESSING,
3473                           AFS_TRACE_LEVEL_ERROR,
3474                           "AFSOpenIOCtlFcb (%p) Failed to initialize ccb Status %08lX\n",
3475                           Irp,
3476                           ntStatus);
3477
3478             try_return( ntStatus);
3479         }
3480
3481         bAllocatedCcb = TRUE;
3482
3483         //
3484         // Set the PIOCtl index
3485         //
3486
3487         (*Ccb)->RequestID = InterlockedIncrement( &pParentObjectInfo->Specific.Directory.OpenRequestIndex);
3488
3489         RtlZeroMemory( &stPIOCtlOpen,
3490                        sizeof( AFSPIOCtlOpenCloseRequestCB));
3491
3492         stPIOCtlOpen.RequestId = (*Ccb)->RequestID;
3493
3494         stPIOCtlOpen.RootId = pParentObjectInfo->VolumeCB->ObjectInformation.FileId;
3495
3496         RtlZeroMemory( &stFileID,
3497                        sizeof( AFSFileID));
3498
3499         //
3500         // The parent directory FID of the node
3501         //
3502
3503         stFileID = pParentObjectInfo->FileId;
3504
3505         //
3506         // Issue the open request to the service
3507         //
3508
3509         ntStatus = AFSProcessRequest( AFS_REQUEST_TYPE_PIOCTL_OPEN,
3510                                       AFS_REQUEST_FLAG_SYNCHRONOUS,
3511                                       AuthGroup,
3512                                       NULL,
3513                                       &stFileID,
3514                                       (void *)&stPIOCtlOpen,
3515                                       sizeof( AFSPIOCtlOpenCloseRequestCB),
3516                                       NULL,
3517                                       NULL);
3518
3519         if( !NT_SUCCESS( ntStatus))
3520         {
3521
3522             AFSDbgLogMsg( AFS_SUBSYSTEM_FILE_PROCESSING,
3523                           AFS_TRACE_LEVEL_ERROR,
3524                           "AFSOpenIOCtlFcb (%p) Failed service open Status %08lX\n",
3525                           Irp,
3526                           ntStatus);
3527
3528             try_return( ntStatus);
3529         }
3530
3531         //
3532         // Increment the handle on the node
3533         //
3534
3535         lCount = InterlockedIncrement( &(*Fcb)->OpenHandleCount);
3536
3537         AFSDbgLogMsg( AFS_SUBSYSTEM_FCB_REF_COUNTING,
3538                       AFS_TRACE_LEVEL_VERBOSE,
3539                       "AFSOpenIOCtlFcb Increment handle count on Fcb %p Cnt %d\n",
3540                       (*Fcb),
3541                       lCount);
3542
3543         //
3544         // Increment the open reference and handle on the parent node
3545         //
3546
3547         lCount = InterlockedIncrement( &pParentObjectInfo->Specific.Directory.ChildOpenHandleCount);
3548
3549         AFSDbgLogMsg( AFS_SUBSYSTEM_OBJECT_REF_COUNTING,
3550                       AFS_TRACE_LEVEL_VERBOSE,
3551                       "AFSOpenIOCtlFcb Increment child open handle count on Parent object %p Cnt %d\n",
3552                       pParentObjectInfo,
3553                       lCount);
3554
3555         lCount = InterlockedIncrement( &pParentObjectInfo->Specific.Directory.ChildOpenReferenceCount);
3556
3557         AFSDbgLogMsg( AFS_SUBSYSTEM_OBJECT_REF_COUNTING,
3558                       AFS_TRACE_LEVEL_VERBOSE,
3559                       "AFSOpenIOCtlFcb Increment child open ref count on Parent object %p Cnt %d\n",
3560                       pParentObjectInfo,
3561                       lCount);
3562
3563         //
3564         // Return the open result for this file
3565         //
3566
3567         Irp->IoStatus.Information = FILE_OPENED;
3568
3569 try_exit:
3570
3571         //
3572         // If we created the Fcb we need to release the resources
3573         //
3574
3575         if( bReleaseFcb)
3576         {
3577
3578             if( !NT_SUCCESS( ntStatus))
3579             {
3580                 //
3581                 // Decrement the open reference and handle on the node
3582                 //
3583
3584                 lCount = InterlockedDecrement( &(*Fcb)->OpenReferenceCount);
3585
3586                 AFSDbgLogMsg( AFS_SUBSYSTEM_FCB_REF_COUNTING,
3587                               AFS_TRACE_LEVEL_VERBOSE,
3588                               "AFSOpenIOCtlFcb Decrement count on Fcb %p Cnt %d\n",
3589                               (*Fcb),
3590                               lCount);
3591             }
3592
3593             AFSReleaseResource( &(*Fcb)->NPFcb->Resource);
3594         }
3595
3596         if( !NT_SUCCESS( ntStatus))
3597         {
3598
3599             if( bAllocatedCcb)
3600             {
3601
3602                 AFSRemoveCcb( NULL,
3603                               *Ccb);
3604             }
3605
3606             *Ccb = NULL;
3607
3608             //
3609             // Fcb will be freed by AFSPrimaryVolumeWorker thread
3610             //
3611
3612             *Fcb = NULL;
3613         }
3614     }
3615
3616     return ntStatus;
3617 }
3618
3619 NTSTATUS
3620 AFSOpenSpecialShareFcb( IN PIRP Irp,
3621                         IN GUID *AuthGroup,
3622                         IN AFSDirectoryCB *DirectoryCB,
3623                         OUT AFSFcb **Fcb,
3624                         OUT AFSCcb **Ccb)
3625 {
3626
3627     NTSTATUS ntStatus = STATUS_SUCCESS;
3628     PFILE_OBJECT pFileObject = NULL;
3629     PIO_STACK_LOCATION pIrpSp = IoGetCurrentIrpStackLocation( Irp);
3630     BOOLEAN bReleaseFcb = FALSE, bAllocatedCcb = FALSE, bAllocateFcb = FALSE;
3631     AFSObjectInfoCB *pParentObjectInfo = NULL;
3632     AFSPipeOpenCloseRequestCB stPipeOpen;
3633     LONG lCount;
3634
3635     __Enter
3636     {
3637
3638         pFileObject = pIrpSp->FileObject;
3639
3640         AFSDbgLogMsg( AFS_SUBSYSTEM_PIPE_PROCESSING,
3641                       AFS_TRACE_LEVEL_VERBOSE_2,
3642                       "AFSOpenSpecialShareFcb (%p) Processing Share %wZ open\n",
3643                       Irp,
3644                       &DirectoryCB->NameInformation.FileName);
3645
3646         pParentObjectInfo = DirectoryCB->ObjectInformation->ParentObjectInformation;
3647
3648         if( DirectoryCB->ObjectInformation->Fcb == NULL)
3649         {
3650
3651             //
3652             // Allocate and initialize the Fcb for the file.
3653             //
3654
3655             ntStatus = AFSInitFcb( DirectoryCB);
3656
3657             *Fcb = DirectoryCB->ObjectInformation->Fcb;
3658
3659             if( !NT_SUCCESS( ntStatus))
3660             {
3661
3662                 AFSDbgLogMsg( AFS_SUBSYSTEM_PIPE_PROCESSING,
3663                               AFS_TRACE_LEVEL_ERROR,
3664                               "AFSOpenSpecialShareFcb (%p) Failed to initialize fcb Status %08lX\n",
3665                               Irp,
3666                               ntStatus);
3667
3668                 try_return( ntStatus);
3669             }
3670
3671             if ( ntStatus != STATUS_REPARSE)
3672             {
3673