Windows: Replace ParentObjectInformation pointer
[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 ( !BooleanFlagOn( pObjectInfo->Flags, AFS_OBJECT_FLAGS_PARENT_FID))
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 != NULL 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 ( AFSIsEqualFID( &pParentObjectInfo->FileId, &pObjectInfo->ParentFileId))
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 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->ParentFileId.Cell,
1797                                       pObjectInfo->ParentFileId.Volume,
1798                                       pObjectInfo->ParentFileId.Vnode,
1799                                       pObjectInfo->ParentFileId.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         ASSERT( BooleanFlagOn( pObjectInfo->Flags, AFS_OBJECT_FLAGS_PARENT_FID) &&
1825                 AFSIsEqualFID( &pParentObjectInfo->FileId, &pObjectInfo->ParentFileId));
1826
1827         //
1828         // We may have raced and the Fcb is already created
1829         //
1830
1831         //
1832         // Allocate and initialize the Fcb for the file.
1833         //
1834
1835         ntStatus = AFSInitFcb( pDirEntry);
1836
1837         *Fcb = pObjectInfo->Fcb;
1838
1839         if( !NT_SUCCESS( ntStatus))
1840         {
1841
1842             AFSDbgLogMsg( AFS_SUBSYSTEM_FILE_PROCESSING,
1843                           AFS_TRACE_LEVEL_ERROR,
1844                           "AFSProcessCreate (%p) Failed to initialize fcb %wZ Status %08lX\n",
1845                           Irp,
1846                           FullFileName,
1847                           ntStatus);
1848
1849             try_return( ntStatus);
1850         }
1851
1852         ntStatus = STATUS_SUCCESS;
1853
1854         //
1855         // Increment the open count on this Fcb
1856         //
1857
1858         lCount = InterlockedIncrement( &(*Fcb)->OpenReferenceCount);
1859
1860         AFSDbgLogMsg( AFS_SUBSYSTEM_FCB_REF_COUNTING,
1861                       AFS_TRACE_LEVEL_VERBOSE,
1862                       "AFSProcessCreate Increment count on Fcb %p Cnt %d\n",
1863                       *Fcb,
1864                       lCount);
1865
1866         bReleaseFcb = TRUE;
1867
1868         //
1869         // Initialize the Ccb for the file.
1870         //
1871
1872         ntStatus = AFSInitCcb( Ccb,
1873                                pDirEntry,
1874                                *pDesiredAccess,
1875                                0);
1876
1877         if( !NT_SUCCESS( ntStatus))
1878         {
1879
1880             AFSDbgLogMsg( AFS_SUBSYSTEM_FILE_PROCESSING,
1881                           AFS_TRACE_LEVEL_ERROR,
1882                           "AFSProcessCreate (%p) Failed to initialize ccb %wZ Status %08lX\n",
1883                           Irp,
1884                           FullFileName,
1885                           ntStatus);
1886
1887             try_return( ntStatus);
1888         }
1889
1890         bAllocatedCcb = TRUE;
1891
1892         //
1893         // If this is a file, update the headers filesizes.
1894         //
1895
1896         if( (*Fcb)->Header.NodeTypeCode == AFS_FILE_FCB)
1897         {
1898
1899             //
1900             // Update the sizes with the information passed in
1901             //
1902
1903             (*Fcb)->Header.AllocationSize.QuadPart  = pObjectInfo->AllocationSize.QuadPart;
1904             (*Fcb)->Header.FileSize.QuadPart        = pObjectInfo->EndOfFile.QuadPart;
1905             (*Fcb)->Header.ValidDataLength.QuadPart = pObjectInfo->EndOfFile.QuadPart;
1906
1907             //
1908             // Notify the system of the addition
1909             //
1910
1911             AFSFsRtlNotifyFullReportChange( pParentObjectInfo,
1912                                             *Ccb,
1913                                             (ULONG)FILE_NOTIFY_CHANGE_FILE_NAME,
1914                                             (ULONG)FILE_ACTION_ADDED);
1915
1916             (*Fcb)->NPFcb->Specific.File.ExtentsRequestStatus = STATUS_SUCCESS;
1917         }
1918         else if( (*Fcb)->Header.NodeTypeCode == AFS_DIRECTORY_FCB)
1919         {
1920
1921             //
1922             // This is a new directory node so indicate it has been enumerated
1923             //
1924
1925             SetFlag( pObjectInfo->Flags, AFS_OBJECT_FLAGS_DIRECTORY_ENUMERATED);
1926
1927             //
1928             // And the parent directory entry
1929             //
1930
1931             KeQuerySystemTime( &pParentObjectInfo->ChangeTime);
1932
1933             //
1934             // Notify the system of the addition
1935             //
1936
1937             AFSFsRtlNotifyFullReportChange( pParentObjectInfo,
1938                                             *Ccb,
1939                                             (ULONG)FILE_NOTIFY_CHANGE_DIR_NAME,
1940                                             (ULONG)FILE_ACTION_ADDED);
1941         }
1942         else if( (*Fcb)->Header.NodeTypeCode == AFS_MOUNT_POINT_FCB ||
1943                  (*Fcb)->Header.NodeTypeCode == AFS_SYMBOLIC_LINK_FCB ||
1944                  (*Fcb)->Header.NodeTypeCode == AFS_DFS_LINK_FCB ||
1945                  (*Fcb)->Header.NodeTypeCode == AFS_INVALID_FCB)
1946         {
1947
1948             //
1949             // And the parent directory entry
1950             //
1951
1952             KeQuerySystemTime( &pParentObjectInfo->ChangeTime);
1953
1954             //
1955             // Notify the system of the addition
1956             //
1957
1958             AFSFsRtlNotifyFullReportChange( pParentObjectInfo,
1959                                             *Ccb,
1960                                             (ULONG)FILE_NOTIFY_CHANGE_DIR_NAME,
1961                                             (ULONG)FILE_ACTION_ADDED);
1962         }
1963
1964         //
1965         // Save off the access for the open
1966         //
1967
1968         IoSetShareAccess( *pDesiredAccess,
1969                           usShareAccess,
1970                           pFileObject,
1971                           &(*Fcb)->ShareAccess);
1972
1973         lCount = InterlockedIncrement( &(*Fcb)->OpenHandleCount);
1974
1975         AFSDbgLogMsg( AFS_SUBSYSTEM_FCB_REF_COUNTING,
1976                       AFS_TRACE_LEVEL_VERBOSE,
1977                       "AFSProcessCreate Increment handle count on Fcb %p Cnt %d\n",
1978                       (*Fcb),
1979                       lCount);
1980
1981         //
1982         // Increment the open reference and handle on the parent node
1983         //
1984
1985         lCount = InterlockedIncrement( &pParentObjectInfo->Specific.Directory.ChildOpenHandleCount);
1986
1987         AFSDbgLogMsg( AFS_SUBSYSTEM_OBJECT_REF_COUNTING,
1988                       AFS_TRACE_LEVEL_VERBOSE,
1989                       "AFSProcessCreate Increment child open handle count on Parent object %p Cnt %d\n",
1990                       pParentObjectInfo,
1991                       lCount);
1992
1993         lCount = InterlockedIncrement( &pParentObjectInfo->Specific.Directory.ChildOpenReferenceCount);
1994
1995         AFSDbgLogMsg( AFS_SUBSYSTEM_FCB_REF_COUNTING,
1996                       AFS_TRACE_LEVEL_VERBOSE,
1997                       "AFSProcessCreate Increment child open ref count on Parent object %p Cnt %d\n",
1998                       pParentObjectInfo,
1999                       lCount);
2000
2001         if( ulOptions & FILE_DELETE_ON_CLOSE)
2002         {
2003
2004             //
2005             // Mark it for delete on close
2006             //
2007
2008             AFSDbgLogMsg( AFS_SUBSYSTEM_FILE_PROCESSING,
2009                           AFS_TRACE_LEVEL_VERBOSE,
2010                           "AFSProcessCreate (%p) Setting PENDING_DELETE flag in DirEntry %p Name %wZ\n",
2011                           Irp,
2012                           pDirEntry,
2013                           FullFileName);
2014
2015             SetFlag( pDirEntry->Flags, AFS_DIR_ENTRY_PENDING_DELETE);
2016         }
2017
2018         //
2019         // Indicate the object is locked in the service
2020         //
2021
2022         SetFlag( pObjectInfo->Flags, AFS_OBJECT_HELD_IN_SERVICE);
2023
2024         //
2025         // Return the open result for this file
2026         //
2027
2028         Irp->IoStatus.Information = FILE_CREATED;
2029
2030 try_exit:
2031
2032         //
2033         // If we created the Fcb we need to release the resources
2034         //
2035
2036         if( bReleaseFcb)
2037         {
2038
2039             if( !NT_SUCCESS( ntStatus))
2040             {
2041                 //
2042                 // Decrement the open count on this Fcb
2043                 //
2044
2045                 lCount = InterlockedDecrement( &(*Fcb)->OpenReferenceCount);
2046
2047                 AFSDbgLogMsg( AFS_SUBSYSTEM_FCB_REF_COUNTING,
2048                               AFS_TRACE_LEVEL_VERBOSE,
2049                               "AFSProcessCreate Decrement count on Fcb %p Cnt %d\n",
2050                               *Fcb,
2051                               lCount);
2052             }
2053
2054             AFSReleaseResource( &(*Fcb)->NPFcb->Resource);
2055         }
2056
2057         if ( bFileCreated)
2058         {
2059
2060             //
2061             // Decrement the reference added during initialization of the DE
2062             // AFSInitCcb allocates its own reference count.
2063             //
2064
2065             lCount = InterlockedDecrement( &pDirEntry->DirOpenReferenceCount);
2066
2067             AFSDbgLogMsg( AFS_SUBSYSTEM_DIRENTRY_REF_COUNTING,
2068                           AFS_TRACE_LEVEL_VERBOSE,
2069                           "AFSProcessCreate Decrement count on %wZ DE %p Cnt %d\n",
2070                           &pDirEntry->NameInformation.FileName,
2071                           pDirEntry,
2072                           lCount);
2073
2074             ASSERT( lCount >= 0);
2075         }
2076
2077         if( !NT_SUCCESS( ntStatus))
2078         {
2079
2080             if( bFileCreated)
2081             {
2082
2083                 AFSDbgLogMsg( AFS_SUBSYSTEM_FILE_PROCESSING,
2084                               AFS_TRACE_LEVEL_VERBOSE,
2085                               "AFSProcessCreate Create failed, removing DE %p from parent object %p Status %08lX\n",
2086                               pDirEntry,
2087                               pParentObjectInfo,
2088                               ntStatus);
2089
2090                 //
2091                 // Remove the dir entry from the parent
2092                 //
2093
2094                 AFSAcquireExcl( pParentObjectInfo->Specific.Directory.DirectoryNodeHdr.TreeLock,
2095                                 TRUE);
2096
2097                 SetFlag( pDirEntry->Flags, AFS_DIR_ENTRY_DELETED);
2098
2099                 AFSNotifyDelete( pDirEntry,
2100                                  AuthGroup,
2101                                  FALSE);
2102
2103                 //
2104                 // Pull the directory entry from the parent
2105                 //
2106
2107                 AFSRemoveDirNodeFromParent( pParentObjectInfo,
2108                                             pDirEntry,
2109                                             FALSE); // Leave it in the enum list so the worker cleans it up
2110
2111                 //
2112                 // Tag the parent as needing verification
2113                 //
2114
2115                 SetFlag( pParentObjectInfo->Flags, AFS_OBJECT_FLAGS_VERIFY);
2116
2117                 pParentObjectInfo->DataVersion.QuadPart = (ULONGLONG)-1;
2118
2119                 AFSReleaseResource( pParentObjectInfo->Specific.Directory.DirectoryNodeHdr.TreeLock);
2120             }
2121
2122             if( bAllocatedCcb)
2123             {
2124
2125                 AFSRemoveCcb( NULL,
2126                               *Ccb);
2127             }
2128
2129             //
2130             // Fcb will be freed by AFSPrimaryVolumeWorker thread
2131             //
2132
2133             *Fcb = NULL;
2134
2135             *Ccb = NULL;
2136         }
2137     }
2138
2139     return ntStatus;
2140 }
2141
2142 NTSTATUS
2143 AFSOpenTargetDirectory( IN PIRP Irp,
2144                         IN AFSVolumeCB *VolumeCB,
2145                         IN AFSDirectoryCB *ParentDirectoryCB,
2146                         IN AFSDirectoryCB *TargetDirectoryCB,
2147                         IN UNICODE_STRING *TargetName,
2148                         OUT AFSFcb **Fcb,
2149                         OUT AFSCcb **Ccb)
2150 {
2151     UNREFERENCED_PARAMETER(VolumeCB);
2152     NTSTATUS ntStatus = STATUS_SUCCESS;
2153     PFILE_OBJECT pFileObject = NULL;
2154     PIO_STACK_LOCATION pIrpSp = IoGetCurrentIrpStackLocation( Irp);
2155     PACCESS_MASK pDesiredAccess = NULL;
2156     USHORT usShareAccess;
2157     BOOLEAN bAllocatedCcb = FALSE;
2158     BOOLEAN bReleaseFcb = FALSE;
2159     AFSObjectInfoCB *pParentObject = NULL;
2160     AFSObjectInfoCB *pGrandParentObject = NULL;
2161     UNICODE_STRING uniTargetName;
2162     LONG lCount;
2163
2164     __Enter
2165     {
2166
2167         pDesiredAccess = &pIrpSp->Parameters.Create.SecurityContext->DesiredAccess;
2168         usShareAccess = pIrpSp->Parameters.Create.ShareAccess;
2169
2170         pFileObject = pIrpSp->FileObject;
2171
2172         AFSDbgLogMsg( AFS_SUBSYSTEM_FILE_PROCESSING,
2173                       AFS_TRACE_LEVEL_VERBOSE,
2174                       "AFSOpenTargetDirectory (%p) Processing file %wZ\n",
2175                       Irp,
2176                       TargetName);
2177
2178         pParentObject = ParentDirectoryCB->ObjectInformation;
2179
2180         if( pParentObject->FileType != AFS_FILE_TYPE_DIRECTORY)
2181         {
2182
2183             try_return( ntStatus = STATUS_INVALID_PARAMETER);
2184         }
2185
2186         //
2187         // Make sure we have an Fcb for the access
2188
2189         //
2190         // Allocate and initialize the Fcb for the file.
2191         //
2192
2193         ntStatus = AFSInitFcb( ParentDirectoryCB);
2194
2195         *Fcb = pParentObject->Fcb;
2196
2197         if( !NT_SUCCESS( ntStatus))
2198         {
2199
2200             AFSDbgLogMsg( AFS_SUBSYSTEM_FILE_PROCESSING,
2201                           AFS_TRACE_LEVEL_ERROR,
2202                           "AFSOpenTargetDirectory (%p) Failed to initialize fcb %wZ Status %08lX\n",
2203                           Irp,
2204                           &ParentDirectoryCB->NameInformation.FileName,
2205                           ntStatus);
2206
2207             try_return( ntStatus);
2208         }
2209
2210         ntStatus = STATUS_SUCCESS;
2211
2212         //
2213         // Increment the open count on this Fcb
2214         //
2215
2216         lCount = InterlockedIncrement( &pParentObject->Fcb->OpenReferenceCount);
2217
2218         AFSDbgLogMsg( AFS_SUBSYSTEM_FCB_REF_COUNTING,
2219                       AFS_TRACE_LEVEL_VERBOSE,
2220                       "AFSOpenTargetDirectory Increment count on Fcb %p Cnt %d\n",
2221                       pParentObject->Fcb,
2222                       lCount);
2223
2224         bReleaseFcb = TRUE;
2225
2226         //
2227         // If there are current opens on the Fcb, check the access.
2228         //
2229
2230         if( pParentObject->Fcb->OpenHandleCount > 0)
2231         {
2232
2233             ntStatus = IoCheckShareAccess( *pDesiredAccess,
2234                                            usShareAccess,
2235                                            pFileObject,
2236                                            &pParentObject->Fcb->ShareAccess,
2237                                            FALSE);
2238
2239             if( !NT_SUCCESS( ntStatus))
2240             {
2241
2242                 AFSDbgLogMsg( AFS_SUBSYSTEM_FILE_PROCESSING,
2243                               AFS_TRACE_LEVEL_ERROR,
2244                               "AFSOpenTargetDirectory (%p) Access check failure %wZ Status %08lX\n",
2245                               Irp,
2246                               &ParentDirectoryCB->NameInformation.FileName,
2247                               ntStatus);
2248
2249                 try_return( ntStatus);
2250             }
2251         }
2252
2253         //
2254         // Initialize the Ccb for the file.
2255         //
2256
2257         ntStatus = AFSInitCcb( Ccb,
2258                                ParentDirectoryCB,
2259                                *pDesiredAccess,
2260                                0);
2261
2262         if( !NT_SUCCESS( ntStatus))
2263         {
2264
2265             AFSDbgLogMsg( AFS_SUBSYSTEM_FILE_PROCESSING,
2266                           AFS_TRACE_LEVEL_ERROR,
2267                           "AFSOpenTargetDirectory (%p) Failed to initialize ccb %wZ Status %08lX\n",
2268                           Irp,
2269                           &ParentDirectoryCB->NameInformation.FileName,
2270                           ntStatus);
2271
2272             try_return( ntStatus);
2273         }
2274
2275         bAllocatedCcb = TRUE;
2276
2277         if( TargetDirectoryCB != NULL &&
2278             FsRtlAreNamesEqual( &TargetDirectoryCB->NameInformation.FileName,
2279                                 TargetName,
2280                                 FALSE,
2281                                 NULL))
2282         {
2283
2284             Irp->IoStatus.Information = FILE_EXISTS;
2285
2286             uniTargetName = TargetDirectoryCB->NameInformation.FileName;
2287         }
2288         else
2289         {
2290
2291             Irp->IoStatus.Information = FILE_DOES_NOT_EXIST;
2292
2293             uniTargetName = *TargetName;
2294         }
2295
2296         //
2297         // Update the filename in the fileobject for rename processing
2298         //
2299
2300         RtlCopyMemory( pFileObject->FileName.Buffer,
2301                        uniTargetName.Buffer,
2302                        uniTargetName.Length);
2303
2304         pFileObject->FileName.Length = uniTargetName.Length;
2305
2306         //
2307         // OK, update the share access on the fileobject
2308         //
2309
2310         if( pParentObject->Fcb->OpenHandleCount > 0)
2311         {
2312
2313             IoUpdateShareAccess( pFileObject,
2314                                  &pParentObject->Fcb->ShareAccess);
2315         }
2316         else
2317         {
2318
2319             //
2320             // Set the access
2321             //
2322
2323             IoSetShareAccess( *pDesiredAccess,
2324                               usShareAccess,
2325                               pFileObject,
2326                               &pParentObject->Fcb->ShareAccess);
2327         }
2328
2329         lCount = InterlockedIncrement( &pParentObject->Fcb->OpenHandleCount);
2330
2331         AFSDbgLogMsg( AFS_SUBSYSTEM_FCB_REF_COUNTING,
2332                       AFS_TRACE_LEVEL_VERBOSE,
2333                       "AFSOpenTargetDirectory Increment handle count on Fcb %p Cnt %d\n",
2334                       pParentObject->Fcb,
2335                       lCount);
2336
2337         //
2338         // Increment the open reference and handle on the parent node
2339         //
2340
2341         if( BooleanFlagOn( pParentObject->Flags, AFS_OBJECT_FLAGS_PARENT_FID))
2342         {
2343
2344             pGrandParentObject = AFSFindObjectInfo( pParentObject->VolumeCB,
2345                                                     &pParentObject->ParentFileId);
2346
2347             if ( pGrandParentObject)
2348             {
2349
2350                 lCount = InterlockedIncrement( &pGrandParentObject->Specific.Directory.ChildOpenHandleCount);
2351
2352                 AFSDbgLogMsg( AFS_SUBSYSTEM_OBJECT_REF_COUNTING,
2353                               AFS_TRACE_LEVEL_VERBOSE,
2354                               "AFSOpenTargetDirectory Increment child open handle count on Parent object %p Cnt %d\n",
2355                               pGrandParentObject,
2356                               lCount);
2357
2358                 lCount = InterlockedIncrement( &pGrandParentObject->Specific.Directory.ChildOpenReferenceCount);
2359
2360                 AFSDbgLogMsg( AFS_SUBSYSTEM_OBJECT_REF_COUNTING,
2361                               AFS_TRACE_LEVEL_VERBOSE,
2362                               "AFSOpenTargetDirectory Increment child open ref count on Parent object %p Cnt %d\n",
2363                               pGrandParentObject,
2364                               lCount);
2365
2366                 AFSReleaseObjectInfo( &pGrandParentObject);
2367             }
2368         }
2369
2370 try_exit:
2371
2372         if( bReleaseFcb)
2373         {
2374
2375             if( !NT_SUCCESS( ntStatus))
2376             {
2377                 //
2378                 // Decrement the open count on this Fcb
2379                 //
2380
2381                 lCount = InterlockedDecrement( &pParentObject->Fcb->OpenReferenceCount);
2382
2383                 AFSDbgLogMsg( AFS_SUBSYSTEM_FCB_REF_COUNTING,
2384                               AFS_TRACE_LEVEL_VERBOSE,
2385                               "AFSOpenTargetDirectory Decrement count on Fcb %p Cnt %d\n",
2386                               pParentObject->Fcb,
2387                               lCount);
2388             }
2389
2390             AFSReleaseResource( &pParentObject->Fcb->NPFcb->Resource);
2391         }
2392
2393         if( !NT_SUCCESS( ntStatus))
2394         {
2395
2396             if( bAllocatedCcb)
2397             {
2398
2399                 AFSRemoveCcb( NULL,
2400                               *Ccb);
2401             }
2402
2403             *Ccb = NULL;
2404
2405             //
2406             // Fcb will be freed by AFSPrimaryVolumeWorker thread
2407             //
2408
2409             *Fcb = NULL;
2410         }
2411     }
2412
2413     return ntStatus;
2414 }
2415
2416 NTSTATUS
2417 AFSProcessOpen( IN PIRP Irp,
2418                 IN GUID *AuthGroup,
2419                 IN AFSVolumeCB *VolumeCB,
2420                 IN AFSDirectoryCB *ParentDirCB,
2421                 IN AFSDirectoryCB *DirectoryCB,
2422                 OUT AFSFcb **Fcb,
2423                 OUT AFSCcb **Ccb)
2424 {
2425     UNREFERENCED_PARAMETER(VolumeCB);
2426     NTSTATUS ntStatus = STATUS_SUCCESS;
2427     PFILE_OBJECT pFileObject = NULL;
2428     PIO_STACK_LOCATION pIrpSp = IoGetCurrentIrpStackLocation( Irp);
2429     PACCESS_MASK pDesiredAccess = NULL;
2430     USHORT usShareAccess;
2431     BOOLEAN bAllocatedCcb = FALSE, bReleaseFcb = FALSE;
2432     ULONG ulOptions = 0;
2433     AFSFileOpenCB   stOpenCB;
2434     AFSFileOpenResultCB stOpenResultCB;
2435     ULONG       ulResultLen = 0;
2436     AFSObjectInfoCB *pParentObjectInfo = NULL;
2437     AFSObjectInfoCB *pObjectInfo = NULL;
2438     ULONG       ulFileAccess = 0;
2439     AFSFileAccessReleaseCB stReleaseFileAccess;
2440     LONG lCount;
2441
2442     __Enter
2443     {
2444
2445         pDesiredAccess = &pIrpSp->Parameters.Create.SecurityContext->DesiredAccess;
2446         usShareAccess = pIrpSp->Parameters.Create.ShareAccess;
2447
2448         pFileObject = pIrpSp->FileObject;
2449
2450         pParentObjectInfo = ParentDirCB->ObjectInformation;
2451
2452         pObjectInfo = DirectoryCB->ObjectInformation;
2453
2454         ASSERT( BooleanFlagOn( pObjectInfo->Flags, AFS_OBJECT_FLAGS_PARENT_FID) &&
2455                 AFSIsEqualFID( &pParentObjectInfo->FileId, &pObjectInfo->ParentFileId));
2456
2457         //
2458         // Check if the entry is pending a deletion
2459         //
2460
2461         if( BooleanFlagOn( DirectoryCB->Flags, AFS_DIR_ENTRY_PENDING_DELETE))
2462         {
2463
2464             ntStatus = STATUS_DELETE_PENDING;
2465
2466             AFSDbgLogMsg( AFS_SUBSYSTEM_FILE_PROCESSING,
2467                           AFS_TRACE_LEVEL_ERROR,
2468                           "AFSProcessOpen (%p) Entry pending delete %wZ Status %08lX\n",
2469                           Irp,
2470                           &DirectoryCB->NameInformation.FileName,
2471                           ntStatus);
2472
2473             try_return( ntStatus);
2474         }
2475
2476         //
2477         // Extract out the options
2478         //
2479
2480         ulOptions = pIrpSp->Parameters.Create.Options;
2481
2482         //
2483         // Check if we should go and retrieve updated information for the node
2484         //
2485
2486         ntStatus = AFSValidateEntry( DirectoryCB,
2487                                      AuthGroup,
2488                                      FALSE,
2489                                      TRUE);
2490
2491         if( !NT_SUCCESS( ntStatus))
2492         {
2493
2494             AFSDbgLogMsg( AFS_SUBSYSTEM_FILE_PROCESSING,
2495                           AFS_TRACE_LEVEL_ERROR,
2496                           "AFSProcessOpen (%p) Failed to validate entry %wZ Status %08lX\n",
2497                           Irp,
2498                           &DirectoryCB->NameInformation.FileName,
2499                           ntStatus);
2500
2501             try_return( ntStatus);
2502         }
2503
2504         //
2505         // If this is marked for delete on close then be sure we can delete the entry
2506         //
2507
2508         if( BooleanFlagOn( ulOptions, FILE_DELETE_ON_CLOSE))
2509         {
2510
2511             ntStatus = AFSNotifyDelete( DirectoryCB,
2512                                         AuthGroup,
2513                                         TRUE);
2514
2515             if( !NT_SUCCESS( ntStatus))
2516             {
2517
2518                 ntStatus = STATUS_CANNOT_DELETE;
2519
2520                 AFSDbgLogMsg( AFS_SUBSYSTEM_FILE_PROCESSING,
2521                               AFS_TRACE_LEVEL_ERROR,
2522                               "AFSProcessOpen (%p) Cannot delete entry %wZ marked for delete on close Status %08lX\n",
2523                               Irp,
2524                               &DirectoryCB->NameInformation.FileName,
2525                               ntStatus);
2526
2527                 try_return( ntStatus);
2528             }
2529         }
2530
2531         //
2532         // Be sure we have an Fcb for the current object
2533         //
2534
2535         ntStatus = AFSInitFcb( DirectoryCB);
2536
2537         if( !NT_SUCCESS( ntStatus))
2538         {
2539
2540             AFSDbgLogMsg( AFS_SUBSYSTEM_FILE_PROCESSING,
2541                           AFS_TRACE_LEVEL_ERROR,
2542                           "AFSProcessOpen (%p) Failed to init fcb on %wZ Status %08lX\n",
2543                           Irp,
2544                           &DirectoryCB->NameInformation.FileName,
2545                           ntStatus);
2546
2547             try_return( ntStatus);
2548         }
2549
2550         ntStatus = STATUS_SUCCESS;
2551
2552         //
2553         // AFSInitFcb returns the Fcb resource held
2554         //
2555
2556         bReleaseFcb = TRUE;
2557
2558         //
2559         // Increment the open count on this Fcb
2560         //
2561
2562         lCount = InterlockedIncrement( &pObjectInfo->Fcb->OpenReferenceCount);
2563
2564         AFSDbgLogMsg( AFS_SUBSYSTEM_FCB_REF_COUNTING,
2565                       AFS_TRACE_LEVEL_VERBOSE,
2566                       "AFSProcessOpen Increment2 count on Fcb %p Cnt %d\n",
2567                       pObjectInfo->Fcb,
2568                       lCount);
2569
2570         //
2571         // Check access on the entry
2572         //
2573
2574         if( pObjectInfo->Fcb->OpenHandleCount > 0)
2575         {
2576
2577             ntStatus = IoCheckShareAccess( *pDesiredAccess,
2578                                            usShareAccess,
2579                                            pFileObject,
2580                                            &pObjectInfo->Fcb->ShareAccess,
2581                                            FALSE);
2582
2583             if( !NT_SUCCESS( ntStatus))
2584             {
2585
2586                 AFSDbgLogMsg( AFS_SUBSYSTEM_FILE_PROCESSING,
2587                               AFS_TRACE_LEVEL_ERROR,
2588                               "AFSProcessOpen (%p) Failed to check share access on %wZ Status %08lX\n",
2589                               Irp,
2590                               &DirectoryCB->NameInformation.FileName,
2591                               ntStatus);
2592
2593                 try_return( ntStatus);
2594             }
2595         }
2596
2597         //
2598         // Additional checks
2599         //
2600
2601         if( pObjectInfo->Fcb->Header.NodeTypeCode == AFS_FILE_FCB)
2602         {
2603
2604             //
2605             // If the caller is asking for write access then try to flush the image section
2606             //
2607
2608             if( FlagOn( *pDesiredAccess, FILE_WRITE_DATA) ||
2609                 BooleanFlagOn(ulOptions, FILE_DELETE_ON_CLOSE))
2610             {
2611
2612                 BOOLEAN bMmFlushed;
2613
2614                 AFSDbgLogMsg( AFS_SUBSYSTEM_LOCK_PROCESSING,
2615                               AFS_TRACE_LEVEL_VERBOSE,
2616                               "AFSProcessOpen Acquiring Fcb SectionObject lock %p EXCL %08lX\n",
2617                               &pObjectInfo->Fcb->NPFcb->SectionObjectResource,
2618                               PsGetCurrentThread());
2619
2620                 AFSAcquireExcl( &pObjectInfo->Fcb->NPFcb->SectionObjectResource,
2621                                 TRUE);
2622
2623                 bMmFlushed = MmFlushImageSection( &pObjectInfo->Fcb->NPFcb->SectionObjectPointers,
2624                                                   MmFlushForWrite);
2625
2626                 AFSDbgLogMsg( AFS_SUBSYSTEM_LOCK_PROCESSING,
2627                               AFS_TRACE_LEVEL_VERBOSE,
2628                               "AFSProcessOpen Releasing Fcb SectionObject lock %p EXCL %08lX\n",
2629                               &pObjectInfo->Fcb->NPFcb->SectionObjectResource,
2630                               PsGetCurrentThread());
2631
2632                 AFSReleaseResource( &pObjectInfo->Fcb->NPFcb->SectionObjectResource);
2633
2634                 if ( !bMmFlushed)
2635                 {
2636
2637                     ntStatus = BooleanFlagOn(ulOptions, FILE_DELETE_ON_CLOSE) ? STATUS_CANNOT_DELETE :
2638                                                                             STATUS_SHARING_VIOLATION;
2639
2640                     AFSDbgLogMsg( AFS_SUBSYSTEM_FILE_PROCESSING,
2641                                   AFS_TRACE_LEVEL_ERROR,
2642                                   "AFSProcessOpen (%p) Failed to flush image section %wZ Status %08lX\n",
2643                                   Irp,
2644                                   &DirectoryCB->NameInformation.FileName,
2645                                   ntStatus);
2646
2647                     try_return( ntStatus);
2648                 }
2649             }
2650
2651             if( BooleanFlagOn( ulOptions, FILE_DIRECTORY_FILE))
2652             {
2653
2654                 ntStatus = STATUS_NOT_A_DIRECTORY;
2655
2656                 AFSDbgLogMsg( AFS_SUBSYSTEM_FILE_PROCESSING,
2657                               AFS_TRACE_LEVEL_ERROR,
2658                               "AFSProcessOpen (%p) Attempt to open file as directory %wZ Status %08lX\n",
2659                               Irp,
2660                               &DirectoryCB->NameInformation.FileName,
2661                               ntStatus);
2662
2663                 try_return( ntStatus);
2664             }
2665
2666             pObjectInfo->Fcb->NPFcb->Specific.File.ExtentsRequestStatus = STATUS_SUCCESS;
2667         }
2668         else if( pObjectInfo->Fcb->Header.NodeTypeCode == AFS_DIRECTORY_FCB ||
2669                  pObjectInfo->Fcb->Header.NodeTypeCode == AFS_ROOT_FCB)
2670         {
2671
2672             if( BooleanFlagOn( ulOptions, FILE_NON_DIRECTORY_FILE))
2673             {
2674
2675                 ntStatus = STATUS_FILE_IS_A_DIRECTORY;
2676
2677                 AFSDbgLogMsg( AFS_SUBSYSTEM_FILE_PROCESSING,
2678                               AFS_TRACE_LEVEL_ERROR,
2679                               "AFSProcessOpen (%p) Attempt to open directory as file %wZ Status %08lX\n",
2680                               Irp,
2681                               &DirectoryCB->NameInformation.FileName,
2682                               ntStatus);
2683
2684                 try_return( ntStatus);
2685             }
2686         }
2687         else if( pObjectInfo->Fcb->Header.NodeTypeCode == AFS_MOUNT_POINT_FCB ||
2688                  pObjectInfo->Fcb->Header.NodeTypeCode == AFS_SYMBOLIC_LINK_FCB ||
2689                  pObjectInfo->Fcb->Header.NodeTypeCode == AFS_DFS_LINK_FCB ||
2690                  pObjectInfo->Fcb->Header.NodeTypeCode == AFS_INVALID_FCB)
2691         {
2692
2693         }
2694         else
2695         {
2696             ASSERT( FALSE);
2697             try_return( ntStatus = STATUS_UNSUCCESSFUL);
2698         }
2699
2700         //
2701         // Check with the service that we can open the file
2702         //
2703
2704         stOpenCB.ParentId = pParentObjectInfo->FileId;
2705
2706         stOpenCB.DesiredAccess = *pDesiredAccess;
2707
2708         stOpenCB.ShareAccess = usShareAccess;
2709
2710         stOpenCB.ProcessId = (ULONGLONG)PsGetCurrentProcessId();
2711
2712         stOpenCB.Identifier = (ULONGLONG)pFileObject;
2713
2714         stOpenResultCB.GrantedAccess = 0;
2715
2716         ulResultLen = sizeof( AFSFileOpenResultCB);
2717
2718         ntStatus = AFSProcessRequest( AFS_REQUEST_TYPE_OPEN_FILE,
2719                                       AFS_REQUEST_FLAG_SYNCHRONOUS | AFS_REQUEST_FLAG_HOLD_FID,
2720                                       AuthGroup,
2721                                       &DirectoryCB->NameInformation.FileName,
2722                                       &pObjectInfo->FileId,
2723                                       (void *)&stOpenCB,
2724                                       sizeof( AFSFileOpenCB),
2725                                       (void *)&stOpenResultCB,
2726                                       &ulResultLen);
2727
2728         if( !NT_SUCCESS( ntStatus))
2729         {
2730
2731             AFSDbgLogMsg( AFS_SUBSYSTEM_FILE_PROCESSING,
2732                           AFS_TRACE_LEVEL_ERROR,
2733                           "AFSProcessOpen (%p) Failed open in service %wZ Status %08lX\n",
2734                           Irp,
2735                           &DirectoryCB->NameInformation.FileName,
2736                           ntStatus);
2737
2738             try_return( ntStatus);
2739         }
2740
2741         //
2742         // Save the granted access in case we need to release it below
2743         //
2744
2745         ulFileAccess = stOpenResultCB.FileAccess;
2746
2747         //
2748         // Check if there is a conflict
2749         //
2750
2751         if( !AFSCheckAccess( *pDesiredAccess,
2752                              stOpenResultCB.GrantedAccess,
2753                              BooleanFlagOn( DirectoryCB->ObjectInformation->FileAttributes, FILE_ATTRIBUTE_DIRECTORY)))
2754         {
2755
2756             ntStatus = STATUS_ACCESS_DENIED;
2757
2758             AFSDbgLogMsg( AFS_SUBSYSTEM_FILE_PROCESSING,
2759                           AFS_TRACE_LEVEL_ERROR,
2760                           "AFSProcessOpen (%p) Failed to check access from service Desired %08lX Granted %08lX Entry %wZ Status %08lX\n",
2761                           Irp,
2762                           *pDesiredAccess,
2763                           stOpenResultCB.GrantedAccess,
2764                           &DirectoryCB->NameInformation.FileName,
2765                           ntStatus);
2766
2767             try_return( ntStatus);
2768         }
2769
2770         //
2771         // Initialize the Ccb for the file.
2772         //
2773
2774         ntStatus = AFSInitCcb( Ccb,
2775                                DirectoryCB,
2776                                *pDesiredAccess,
2777                                ulFileAccess);
2778
2779         if( !NT_SUCCESS( ntStatus))
2780         {
2781
2782             AFSDbgLogMsg( AFS_SUBSYSTEM_FILE_PROCESSING,
2783                           AFS_TRACE_LEVEL_ERROR,
2784                           "AFSProcessOpen (%p) Failed to initialize ccb %wZ Status %08lX\n",
2785                           Irp,
2786                           &DirectoryCB->NameInformation.FileName,
2787                           ntStatus);
2788
2789             try_return( ntStatus);
2790         }
2791
2792         bAllocatedCcb = TRUE;
2793
2794         //
2795         // Perform the access check on the target if this is a mount point or symlink
2796         //
2797
2798         if( pObjectInfo->Fcb->OpenHandleCount > 0)
2799         {
2800
2801             IoUpdateShareAccess( pFileObject,
2802                                  &pObjectInfo->Fcb->ShareAccess);
2803         }
2804         else
2805         {
2806
2807             //
2808             // Set the access
2809             //
2810
2811             IoSetShareAccess( *pDesiredAccess,
2812                               usShareAccess,
2813                               pFileObject,
2814                               &pObjectInfo->Fcb->ShareAccess);
2815         }
2816
2817         lCount = InterlockedIncrement( &pObjectInfo->Fcb->OpenHandleCount);
2818
2819         AFSDbgLogMsg( AFS_SUBSYSTEM_FCB_REF_COUNTING,
2820                       AFS_TRACE_LEVEL_VERBOSE,
2821                       "AFSProcessOpen Increment handle count on Fcb %p Cnt %d\n",
2822                       pObjectInfo->Fcb,
2823                       lCount);
2824
2825         //
2826         // Increment the open reference and handle on the parent node
2827         //
2828
2829         lCount = InterlockedIncrement( &pParentObjectInfo->Specific.Directory.ChildOpenHandleCount);
2830
2831         AFSDbgLogMsg( AFS_SUBSYSTEM_OBJECT_REF_COUNTING,
2832                       AFS_TRACE_LEVEL_VERBOSE,
2833                       "AFSProcessOpen Increment child open handle count on Parent object %p Cnt %d\n",
2834                       pParentObjectInfo,
2835                       lCount);
2836
2837         lCount = InterlockedIncrement( &pParentObjectInfo->Specific.Directory.ChildOpenReferenceCount);
2838
2839         AFSDbgLogMsg( AFS_SUBSYSTEM_OBJECT_REF_COUNTING,
2840                       AFS_TRACE_LEVEL_VERBOSE,
2841                       "AFSProcessOpen Increment child open ref count on Parent object %p Cnt %d\n",
2842                       pParentObjectInfo,
2843                       lCount);
2844
2845         if( BooleanFlagOn( ulOptions, FILE_DELETE_ON_CLOSE))
2846         {
2847
2848             //
2849             // Mark it for delete on close
2850             //
2851
2852             AFSDbgLogMsg( AFS_SUBSYSTEM_FILE_PROCESSING,
2853                           AFS_TRACE_LEVEL_VERBOSE,
2854                           "AFSProcessOpen (%p) Setting PENDING_DELETE flag in DirEntry %p Name %wZ\n",
2855                           Irp,
2856                           DirectoryCB,
2857                           &DirectoryCB->NameInformation.FileName);
2858
2859             SetFlag( DirectoryCB->Flags, AFS_DIR_ENTRY_PENDING_DELETE);
2860         }
2861
2862         //
2863         // Indicate the object is held
2864         //
2865
2866         SetFlag( pObjectInfo->Flags, AFS_OBJECT_HELD_IN_SERVICE);
2867
2868         //
2869         // Return the open result for this file
2870         //
2871
2872         Irp->IoStatus.Information = FILE_OPENED;
2873
2874         *Fcb = pObjectInfo->Fcb;
2875
2876 try_exit:
2877
2878         if( bReleaseFcb)
2879         {
2880
2881             if( !NT_SUCCESS( ntStatus))
2882             {
2883                 //
2884                 // Decrement the open count on this Fcb
2885                 //
2886
2887                 lCount = InterlockedDecrement( &pObjectInfo->Fcb->OpenReferenceCount);
2888
2889                 AFSDbgLogMsg( AFS_SUBSYSTEM_FCB_REF_COUNTING,
2890                               AFS_TRACE_LEVEL_VERBOSE,
2891                               "AFSProcessOpen Decrement2 count on Fcb %p Cnt %d\n",
2892                               pObjectInfo->Fcb,
2893                               lCount);
2894             }
2895
2896             AFSReleaseResource( pObjectInfo->Fcb->Header.Resource);
2897         }
2898
2899         if( !NT_SUCCESS( ntStatus))
2900         {
2901
2902             if ( ulFileAccess > 0)
2903             {
2904
2905                 stReleaseFileAccess.ProcessId = (ULONGLONG)PsGetCurrentProcessId();
2906
2907                 stReleaseFileAccess.FileAccess = ulFileAccess;
2908
2909                 stReleaseFileAccess.Identifier = (ULONGLONG)pFileObject;
2910
2911                 AFSProcessRequest( AFS_REQUEST_TYPE_RELEASE_FILE_ACCESS,
2912                                    AFS_REQUEST_FLAG_SYNCHRONOUS,
2913                                    AuthGroup,
2914                                    &DirectoryCB->NameInformation.FileName,
2915                                    &pObjectInfo->FileId,
2916                                    (void *)&stReleaseFileAccess,
2917                                    sizeof( AFSFileAccessReleaseCB),
2918                                    NULL,
2919                                    NULL);
2920             }
2921
2922             if( bAllocatedCcb)
2923             {
2924
2925                 AFSRemoveCcb( NULL,
2926                               *Ccb);
2927             }
2928
2929             *Ccb = NULL;
2930
2931             //
2932             // Fcb will be freed by AFSPrimaryVolumeWorker thread
2933             //
2934
2935             *Fcb = NULL;
2936         }
2937     }
2938
2939     return ntStatus;
2940 }
2941
2942 NTSTATUS
2943 AFSProcessOverwriteSupersede( IN PDEVICE_OBJECT DeviceObject,
2944                               IN PIRP           Irp,
2945                               IN AFSVolumeCB   *VolumeCB,
2946                               IN GUID          *AuthGroup,
2947                               IN AFSDirectoryCB *ParentDirCB,
2948                               IN AFSDirectoryCB *DirectoryCB,
2949                               OUT AFSFcb       **Fcb,
2950                               OUT AFSCcb       **Ccb)
2951 {
2952     UNREFERENCED_PARAMETER(DeviceObject);
2953     NTSTATUS ntStatus = STATUS_SUCCESS;
2954     PIO_STACK_LOCATION pIrpSp = IoGetCurrentIrpStackLocation( Irp);
2955     PFILE_OBJECT pFileObject = NULL;
2956     LARGE_INTEGER liZero = {0,0};
2957     BOOLEAN bReleasePaging = FALSE, bReleaseFcb = FALSE;
2958     ULONG   ulAttributes = 0;
2959     ULONG ulCreateDisposition = 0;
2960     BOOLEAN bAllocatedCcb = FALSE;
2961     BOOLEAN bUserMapped = FALSE;
2962     PACCESS_MASK pDesiredAccess = NULL;
2963     USHORT usShareAccess;
2964     AFSObjectInfoCB *pParentObjectInfo = NULL;
2965     AFSObjectInfoCB *pObjectInfo = NULL;
2966     LONG lCount;
2967     LARGE_INTEGER liSaveSize;
2968     LARGE_INTEGER liSaveVDL;
2969     LARGE_INTEGER liSaveAlloc;
2970
2971     __Enter
2972     {
2973
2974         pDesiredAccess = &pIrpSp->Parameters.Create.SecurityContext->DesiredAccess;
2975
2976         usShareAccess = pIrpSp->Parameters.Create.ShareAccess;
2977
2978         pFileObject = pIrpSp->FileObject;
2979
2980         ulAttributes = pIrpSp->Parameters.Create.FileAttributes;
2981
2982         ulCreateDisposition = (pIrpSp->Parameters.Create.Options >> 24) & 0x000000ff;
2983
2984         if( BooleanFlagOn( VolumeCB->VolumeInformation.Characteristics, FILE_READ_ONLY_DEVICE))
2985         {
2986
2987             AFSDbgLogMsg( AFS_SUBSYSTEM_FILE_PROCESSING,
2988                           AFS_TRACE_LEVEL_ERROR,
2989                           "AFSProcessOverwriteSupersede Request failed on %wZ due to read only volume\n",
2990                           Irp,
2991                           &DirectoryCB->NameInformation.FileName);
2992
2993             try_return( ntStatus = STATUS_MEDIA_WRITE_PROTECTED);
2994         }
2995
2996         pParentObjectInfo = ParentDirCB->ObjectInformation;
2997
2998         pObjectInfo = DirectoryCB->ObjectInformation;
2999
3000         ASSERT( BooleanFlagOn( pObjectInfo->Flags, AFS_OBJECT_FLAGS_PARENT_FID) &&
3001                 AFSIsEqualFID( &pParentObjectInfo->FileId, &pObjectInfo->ParentFileId));
3002
3003         //
3004         // Check if we should go and retrieve updated information for the node
3005         //
3006
3007         ntStatus = AFSValidateEntry( DirectoryCB,
3008                                      AuthGroup,
3009                                      FALSE,
3010                                      TRUE);
3011
3012         if( !NT_SUCCESS( ntStatus))
3013         {
3014
3015             AFSDbgLogMsg( AFS_SUBSYSTEM_FILE_PROCESSING,
3016                           AFS_TRACE_LEVEL_ERROR,
3017                           "AFSProcessOverwriteSupersede (%p) Failed to validate entry %wZ Status %08lX\n",
3018                           Irp,
3019                           &DirectoryCB->NameInformation.FileName,
3020                           ntStatus);
3021
3022             try_return( ntStatus);
3023         }
3024
3025         //
3026         // Be sure we have an Fcb for the object block
3027         //
3028
3029         ntStatus = AFSInitFcb( DirectoryCB);
3030
3031         *Fcb = pObjectInfo->Fcb;
3032
3033         if( !NT_SUCCESS( ntStatus))
3034         {
3035
3036             AFSDbgLogMsg( AFS_SUBSYSTEM_FILE_PROCESSING,
3037                           AFS_TRACE_LEVEL_ERROR,
3038                           "AFSProcessOverwriteSupersede (%p) Failed to initialize fcb %wZ Status %08lX\n",
3039                           Irp,
3040                           &DirectoryCB->NameInformation.FileName,
3041                           ntStatus);
3042
3043             try_return( ntStatus);
3044         }
3045
3046         ntStatus = STATUS_SUCCESS;
3047
3048         //
3049         // Increment the open count on this Fcb.
3050         //
3051
3052         lCount = InterlockedIncrement( &pObjectInfo->Fcb->OpenReferenceCount);
3053
3054         AFSDbgLogMsg( AFS_SUBSYSTEM_FCB_REF_COUNTING,
3055                       AFS_TRACE_LEVEL_VERBOSE,
3056                       "AFSProcessOverwriteSupersede Increment2 count on Fcb %p Cnt %d\n",
3057                       pObjectInfo->Fcb,
3058                       lCount);
3059
3060         bReleaseFcb = TRUE;
3061
3062         //
3063         // Check access on the entry
3064         //
3065
3066         if( pObjectInfo->Fcb->OpenHandleCount > 0)
3067         {
3068
3069             ntStatus = IoCheckShareAccess( *pDesiredAccess,
3070                                            usShareAccess,
3071                                            pFileObject,
3072                                            &pObjectInfo->Fcb->ShareAccess,
3073                                            FALSE);
3074
3075             if( !NT_SUCCESS( ntStatus))
3076             {
3077
3078                 AFSDbgLogMsg( AFS_SUBSYSTEM_FILE_PROCESSING,
3079                               AFS_TRACE_LEVEL_ERROR,
3080                               "AFSProcessOverwriteSupersede (%p) Access check failure %wZ Status %08lX\n",
3081                               Irp,
3082                               &DirectoryCB->NameInformation.FileName,
3083                               ntStatus);
3084
3085                 try_return( ntStatus);
3086             }
3087         }
3088
3089         AFSDbgLogMsg( AFS_SUBSYSTEM_LOCK_PROCESSING,
3090                       AFS_TRACE_LEVEL_VERBOSE,
3091                       "AFSProcessOverwriteSupercede Acquiring Fcb SectionObject lock %p EXCL %08lX\n",
3092                       &pObjectInfo->Fcb->NPFcb->SectionObjectResource,
3093                       PsGetCurrentThread());
3094
3095         AFSAcquireExcl( &pObjectInfo->Fcb->NPFcb->SectionObjectResource,
3096                         TRUE);
3097
3098         //
3099         //  Before we actually truncate, check to see if the purge
3100         //  is going to fail.
3101         //
3102
3103         bUserMapped = !MmCanFileBeTruncated( &pObjectInfo->Fcb->NPFcb->SectionObjectPointers,
3104                                              &liZero);
3105
3106         AFSDbgLogMsg( AFS_SUBSYSTEM_LOCK_PROCESSING,
3107                       AFS_TRACE_LEVEL_VERBOSE,
3108                       "AFSProcessOverwriteSupercede Releasing Fcb SectionObject lock %p EXCL %08lX\n",
3109                       &pObjectInfo->Fcb->NPFcb->SectionObjectResource,
3110                       PsGetCurrentThread());
3111
3112         AFSReleaseResource( &pObjectInfo->Fcb->NPFcb->SectionObjectResource);
3113
3114         if( bUserMapped)
3115         {
3116
3117             ntStatus = STATUS_USER_MAPPED_FILE;
3118
3119             AFSDbgLogMsg( AFS_SUBSYSTEM_FILE_PROCESSING,
3120                           AFS_TRACE_LEVEL_ERROR,
3121                           "AFSProcessOverwriteSupersede (%p) File user mapped %wZ Status %08lX\n",
3122                           Irp,
3123                           &DirectoryCB->NameInformation.FileName,
3124                           ntStatus);
3125
3126             try_return( ntStatus);
3127         }
3128
3129         //
3130         // Initialize the Ccb for the file.
3131         //
3132
3133         ntStatus = AFSInitCcb( Ccb,
3134                                DirectoryCB,
3135                                *pDesiredAccess,
3136                                0);
3137
3138         if( !NT_SUCCESS( ntStatus))
3139         {
3140
3141             AFSDbgLogMsg( AFS_SUBSYSTEM_FILE_PROCESSING,
3142                           AFS_TRACE_LEVEL_ERROR,
3143                           "AFSProcessOverwriteSupersede (%p) Failed to initialize ccb %wZ Status %08lX\n",
3144                           Irp,
3145                           &DirectoryCB->NameInformation.FileName,
3146                           ntStatus);
3147
3148             try_return( ntStatus);
3149         }
3150
3151    &nb