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