Windows: AFSFindObjectInfo update last access time
[openafs.git] / src / WINNT / afsrdr / kernel / lib / AFSFileInfo.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: AFSFileInfo.cpp
37 //
38
39 #include "AFSCommon.h"
40
41 //
42 // Function: AFSQueryFileInfo
43 //
44 // Description:
45 //
46 //      This function is the dispatch handler for the IRP_MJ_QUERY_FILE_INFORMATION request
47 //
48 // Return:
49 //
50 //      A status is returned for the function
51 //
52
53 NTSTATUS
54 AFSQueryFileInfo( IN PDEVICE_OBJECT LibDeviceObject,
55                   IN PIRP Irp)
56 {
57
58     UNREFERENCED_PARAMETER(LibDeviceObject);
59     NTSTATUS ntStatus = STATUS_SUCCESS;
60     IO_STACK_LOCATION *pIrpSp = IoGetCurrentIrpStackLocation( Irp);
61     AFSFcb *pFcb = NULL;
62     AFSCcb *pCcb = NULL;
63     BOOLEAN bReleaseMain = FALSE;
64     LONG lLength = 0;
65     FILE_INFORMATION_CLASS stFileInformationClass;
66     GUID stAuthGroup;
67     PVOID pBuffer;
68
69     __try
70     {
71
72         //
73         // Determine the type of request this request is
74         //
75
76         pFcb = (AFSFcb *)pIrpSp->FileObject->FsContext;
77
78         pCcb = (AFSCcb *)pIrpSp->FileObject->FsContext2;
79
80         if( pFcb == NULL)
81         {
82
83             AFSDbgTrace(( AFS_SUBSYSTEM_FILE_PROCESSING,
84                           AFS_TRACE_LEVEL_ERROR,
85                           "AFSQueryFileInfo Attempted access (%p) when pFcb == NULL\n",
86                           Irp));
87
88             try_return( ntStatus = STATUS_INVALID_DEVICE_REQUEST);
89         }
90
91         lLength = (LONG)pIrpSp->Parameters.QueryFile.Length;
92         stFileInformationClass = pIrpSp->Parameters.QueryFile.FileInformationClass;
93         pBuffer = Irp->AssociatedIrp.SystemBuffer;
94
95         if ( BooleanFlagOn( pFcb->ObjectInformation->Flags, AFS_OBJECT_FLAGS_VERIFY))
96         {
97
98             RtlZeroMemory( &stAuthGroup,
99                            sizeof( GUID));
100
101             AFSRetrieveAuthGroupFnc( (ULONGLONG)PsGetCurrentProcessId(),
102                                      (ULONGLONG)PsGetCurrentThreadId(),
103                                      &stAuthGroup);
104
105             ntStatus = AFSVerifyEntry( &stAuthGroup,
106                                        pCcb->DirectoryCB);
107
108             if ( NT_SUCCESS( ntStatus))
109             {
110
111                 ClearFlag( pFcb->ObjectInformation->Flags, AFS_OBJECT_FLAGS_VERIFY);
112             }
113             else
114             {
115
116                 ntStatus = STATUS_SUCCESS;
117             }
118         }
119
120         //
121         // Grab the main shared right off the bat
122         //
123
124         AFSDbgTrace(( AFS_SUBSYSTEM_LOCK_PROCESSING,
125                       AFS_TRACE_LEVEL_VERBOSE,
126                       "AFSQueryFileInfo Acquiring Fcb lock %p SHARED %08lX\n",
127                       &pFcb->NPFcb->Resource,
128                       PsGetCurrentThread()));
129
130         AFSAcquireShared( &pFcb->NPFcb->Resource,
131                           TRUE);
132
133         bReleaseMain = TRUE;
134
135         //
136         // Don't allow requests against IOCtl nodes
137         //
138
139         if( pFcb->Header.NodeTypeCode == AFS_SPECIAL_SHARE_FCB)
140         {
141
142             AFSDbgTrace(( AFS_SUBSYSTEM_FILE_PROCESSING,
143                           AFS_TRACE_LEVEL_VERBOSE,
144                           "AFSQueryFileInfo Processing request against SpecialShare Fcb\n"));
145
146             ntStatus = AFSProcessShareQueryInfo( Irp,
147                                                  pFcb,
148                                                  pCcb);
149
150             try_return( ntStatus);
151         }
152         else if( pFcb->Header.NodeTypeCode == AFS_IOCTL_FCB)
153         {
154             AFSDbgTrace(( AFS_SUBSYSTEM_FILE_PROCESSING,
155                           AFS_TRACE_LEVEL_VERBOSE,
156                           "AFSQueryFileInfo request against PIOCtl Fcb\n"));
157
158             ntStatus = AFSProcessPIOCtlQueryInfo( Irp,
159                                                   pFcb,
160                                                   pCcb,
161                                                   &lLength);
162
163             try_return( ntStatus);
164         }
165
166         else if( pFcb->Header.NodeTypeCode == AFS_INVALID_FCB)
167         {
168             AFSDbgTrace(( AFS_SUBSYSTEM_FILE_PROCESSING,
169                           AFS_TRACE_LEVEL_VERBOSE,
170                           "AFSQueryFileInfo request against Invalid Fcb\n"));
171
172             try_return( ntStatus = STATUS_ACCESS_DENIED);
173         }
174
175         //
176         // Process the request
177         //
178
179         switch( stFileInformationClass)
180         {
181
182             case FileAllInformation:
183             {
184
185                 PFILE_ALL_INFORMATION pAllInfo;
186
187                 //
188                 //  For the all information class we'll typecast a local
189                 //  pointer to the output buffer and then call the
190                 //  individual routines to fill in the buffer.
191                 //
192
193                 pAllInfo = (PFILE_ALL_INFORMATION)pBuffer;
194
195                 ntStatus = AFSQueryBasicInfo( Irp,
196                                               pCcb->DirectoryCB,
197                                               &pAllInfo->BasicInformation,
198                                               &lLength);
199
200                 if( !NT_SUCCESS( ntStatus))
201                 {
202
203                     try_return( ntStatus);
204                 }
205
206                 ntStatus = AFSQueryStandardInfo( Irp,
207                                                  pCcb->DirectoryCB,
208                                                  &pAllInfo->StandardInformation,
209                                                  &lLength);
210
211                 if( !NT_SUCCESS( ntStatus))
212                 {
213
214                     try_return( ntStatus);
215                 }
216
217                 ntStatus = AFSQueryInternalInfo( Irp,
218                                                  pFcb,
219                                                  &pAllInfo->InternalInformation,
220                                                  &lLength);
221
222                 if( !NT_SUCCESS( ntStatus))
223                 {
224
225                     try_return( ntStatus);
226                 }
227
228                 ntStatus = AFSQueryEaInfo( Irp,
229                                            pCcb->DirectoryCB,
230                                            &pAllInfo->EaInformation,
231                                            &lLength);
232
233                 if( !NT_SUCCESS( ntStatus))
234                 {
235
236                     try_return( ntStatus);
237                 }
238
239                 ntStatus = AFSQueryAccess( Irp,
240                                            pFcb,
241                                            &pAllInfo->AccessInformation,
242                                            &lLength);
243
244                 if( !NT_SUCCESS( ntStatus))
245                 {
246
247                     try_return( ntStatus);
248                 }
249
250                 ntStatus = AFSQueryPositionInfo( Irp,
251                                                  pFcb,
252                                                  &pAllInfo->PositionInformation,
253                                                  &lLength);
254
255                 if( !NT_SUCCESS( ntStatus))
256                 {
257
258                     try_return( ntStatus);
259                 }
260
261                 ntStatus = AFSQueryMode( Irp,
262                                          pFcb,
263                                          &pAllInfo->ModeInformation,
264                                          &lLength);
265
266                 if( !NT_SUCCESS( ntStatus))
267                 {
268
269                     try_return( ntStatus);
270                 }
271
272                 ntStatus = AFSQueryAlignment( Irp,
273                                               pFcb,
274                                               &pAllInfo->AlignmentInformation,
275                                               &lLength);
276
277                 if( !NT_SUCCESS( ntStatus))
278                 {
279
280                     try_return( ntStatus);
281                 }
282
283                 ntStatus = AFSQueryNameInfo( Irp,
284                                              pCcb->DirectoryCB,
285                                              &pAllInfo->NameInformation,
286                                              &lLength);
287
288                 if( !NT_SUCCESS( ntStatus))
289                 {
290
291                     try_return( ntStatus);
292                 }
293
294                 break;
295             }
296
297             case FileBasicInformation:
298             {
299
300                 ntStatus = AFSQueryBasicInfo( Irp,
301                                               pCcb->DirectoryCB,
302                                               (PFILE_BASIC_INFORMATION)pBuffer,
303                                               &lLength);
304
305                 break;
306             }
307
308             case FileStandardInformation:
309             {
310
311                 ntStatus = AFSQueryStandardInfo( Irp,
312                                                  pCcb->DirectoryCB,
313                                                  (PFILE_STANDARD_INFORMATION)pBuffer,
314                                                  &lLength);
315
316                 break;
317             }
318
319             case FileInternalInformation:
320             {
321
322                 ntStatus = AFSQueryInternalInfo( Irp,
323                                                  pFcb,
324                                                  (PFILE_INTERNAL_INFORMATION)pBuffer,
325                                                  &lLength);
326
327                 break;
328             }
329
330             case FileEaInformation:
331             {
332
333                 ntStatus = AFSQueryEaInfo( Irp,
334                                            pCcb->DirectoryCB,
335                                            (PFILE_EA_INFORMATION)pBuffer,
336                                            &lLength);
337
338                 break;
339             }
340
341             case FilePositionInformation:
342             {
343
344                 ntStatus = AFSQueryPositionInfo( Irp,
345                                       pFcb,
346                                       (PFILE_POSITION_INFORMATION)pBuffer,
347                                       &lLength);
348
349                 break;
350             }
351
352             case FileNormalizedNameInformation:
353             case FileNameInformation:
354             {
355
356                 ntStatus = AFSQueryNameInfo( Irp,
357                                   pCcb->DirectoryCB,
358                                   (PFILE_NAME_INFORMATION)pBuffer,
359                                   &lLength);
360
361                 break;
362             }
363
364             case FileAlternateNameInformation:
365             {
366
367                 ntStatus = AFSQueryShortNameInfo( Irp,
368                                        pCcb->DirectoryCB,
369                                        (PFILE_NAME_INFORMATION)pBuffer,
370                                        &lLength);
371
372                 break;
373             }
374
375             case FileNetworkOpenInformation:
376             {
377
378                 ntStatus = AFSQueryNetworkInfo( Irp,
379                                      pCcb->DirectoryCB,
380                                      (PFILE_NETWORK_OPEN_INFORMATION)pBuffer,
381                                      &lLength);
382
383                 break;
384             }
385
386             case FileStreamInformation:
387             {
388
389                 ntStatus = AFSQueryStreamInfo( Irp,
390                                                pCcb->DirectoryCB,
391                                                (FILE_STREAM_INFORMATION *)pBuffer,
392                                                &lLength);
393
394                 break;
395             }
396
397
398             case FileAttributeTagInformation:
399             {
400
401                 ntStatus = AFSQueryAttribTagInfo( Irp,
402                                                   pCcb->DirectoryCB,
403                                                   (FILE_ATTRIBUTE_TAG_INFORMATION *)pBuffer,
404                                                   &lLength);
405
406                 break;
407             }
408
409             case FileRemoteProtocolInformation:
410             {
411
412                     ntStatus = AFSQueryRemoteProtocolInfo( Irp,
413                                                            pCcb->DirectoryCB,
414                                                            (FILE_REMOTE_PROTOCOL_INFORMATION *)pBuffer,
415                                                            &lLength);
416
417                 break;
418             }
419
420             case FileNetworkPhysicalNameInformation:
421             {
422
423                 ntStatus = AFSQueryPhysicalNameInfo( Irp,
424                                                      pCcb->DirectoryCB,
425                                                      (FILE_NETWORK_PHYSICAL_NAME_INFORMATION *)pBuffer,
426                                                      &lLength);
427
428                 break;
429             }
430
431             default:
432             {
433                 ntStatus = STATUS_INVALID_PARAMETER;
434                 break;
435             }
436         }
437
438 try_exit:
439
440         Irp->IoStatus.Information = pIrpSp->Parameters.QueryFile.Length - lLength;
441
442         if( bReleaseMain)
443         {
444
445             AFSReleaseResource( &pFcb->NPFcb->Resource);
446         }
447
448         if( !NT_SUCCESS( ntStatus) &&
449             ntStatus != STATUS_INVALID_PARAMETER &&
450             ntStatus != STATUS_BUFFER_OVERFLOW)
451         {
452
453             if( pCcb != NULL &&
454                 pCcb->DirectoryCB != NULL)
455             {
456
457                 AFSDbgTrace(( AFS_SUBSYSTEM_FILE_PROCESSING,
458                               AFS_TRACE_LEVEL_ERROR,
459                               "AFSQueryFileInfo Failed to process request for %wZ FID %08lX-%08lX-%08lX-%08lX Status %08lX\n",
460                               &pCcb->DirectoryCB->NameInformation.FileName,
461                               pFcb->ObjectInformation->FileId.Cell,
462                               pFcb->ObjectInformation->FileId.Volume,
463                               pFcb->ObjectInformation->FileId.Vnode,
464                               pFcb->ObjectInformation->FileId.Unique,
465                               ntStatus));
466             }
467         }
468     }
469     __except( AFSExceptionFilter( __FUNCTION__, GetExceptionCode(), GetExceptionInformation()) )
470     {
471
472         AFSDbgTrace(( 0,
473                       0,
474                       "EXCEPTION - AFSQueryFileInfo\n"));
475
476         AFSDumpTraceFilesFnc();
477
478         ntStatus = STATUS_UNSUCCESSFUL;
479
480         if( bReleaseMain)
481         {
482
483             AFSReleaseResource( &pFcb->NPFcb->Resource);
484         }
485     }
486
487     AFSCompleteRequest( Irp,
488                         ntStatus);
489
490     return ntStatus;
491 }
492
493 //
494 // Function: AFSSetFileInfo
495 //
496 // Description:
497 //
498 //      This function is the dispatch handler for the IRP_MJ_SET_FILE_INFORMATION request
499 //
500 // Return:
501 //
502 //      A status is returned for the function
503 //
504
505 NTSTATUS
506 AFSSetFileInfo( IN PDEVICE_OBJECT LibDeviceObject,
507                 IN PIRP Irp)
508 {
509
510     UNREFERENCED_PARAMETER(LibDeviceObject);
511     NTSTATUS ntStatus = STATUS_SUCCESS;
512     IO_STACK_LOCATION *pIrpSp = IoGetCurrentIrpStackLocation( Irp);
513     AFSFcb *pFcb = NULL;
514     AFSCcb *pCcb = NULL;
515     FILE_INFORMATION_CLASS FileInformationClass;
516     BOOLEAN bCanQueueRequest = FALSE;
517     PFILE_OBJECT pFileObject = NULL;
518     BOOLEAN bReleaseMain = FALSE;
519     BOOLEAN bUpdateFileInfo = FALSE;
520     AFSFileID stParentFileId;
521
522     __try
523     {
524
525         pFileObject = pIrpSp->FileObject;
526
527         pFcb = (AFSFcb *)pFileObject->FsContext;
528         pCcb = (AFSCcb *)pFileObject->FsContext2;
529
530         if( pFcb == NULL)
531         {
532
533             AFSDbgTrace(( AFS_SUBSYSTEM_FILE_PROCESSING,
534                           AFS_TRACE_LEVEL_ERROR,
535                           "AFSSetFileInfo Attempted access (%p) when pFcb == NULL\n",
536                           Irp));
537
538             try_return( ntStatus = STATUS_INVALID_DEVICE_REQUEST);
539         }
540
541         bCanQueueRequest = !(IoIsOperationSynchronous( Irp) | (KeGetCurrentIrql() != PASSIVE_LEVEL));
542         FileInformationClass = pIrpSp->Parameters.SetFile.FileInformationClass;
543
544         //
545         // Grab the Fcb EXCL
546         //
547
548         AFSDbgTrace(( AFS_SUBSYSTEM_LOCK_PROCESSING,
549                       AFS_TRACE_LEVEL_VERBOSE,
550                       "AFSSetFileInfo Acquiring Fcb lock %p EXCL %08lX\n",
551                       &pFcb->NPFcb->Resource,
552                       PsGetCurrentThread()));
553
554         AFSAcquireExcl( &pFcb->NPFcb->Resource,
555                         TRUE);
556
557         bReleaseMain = TRUE;
558
559         //
560         // Don't allow requests against IOCtl nodes
561         //
562
563         if( pFcb->Header.NodeTypeCode == AFS_IOCTL_FCB)
564         {
565
566             AFSDbgTrace(( AFS_SUBSYSTEM_FILE_PROCESSING,
567                           AFS_TRACE_LEVEL_ERROR,
568                           "AFSSetFileInfo Failing request against PIOCtl Fcb\n"));
569
570             try_return( ntStatus = STATUS_INVALID_DEVICE_REQUEST);
571         }
572         else if( pFcb->Header.NodeTypeCode == AFS_SPECIAL_SHARE_FCB)
573         {
574
575             AFSDbgTrace(( AFS_SUBSYSTEM_FILE_PROCESSING,
576                           AFS_TRACE_LEVEL_VERBOSE,
577                           "AFSSetFileInfo Processing request against SpecialShare Fcb\n"));
578
579             ntStatus = AFSProcessShareSetInfo( Irp,
580                                                pFcb,
581                                                pCcb);
582
583             try_return( ntStatus);
584         }
585
586         if( BooleanFlagOn( pFcb->ObjectInformation->VolumeCB->VolumeInformation.FileSystemAttributes, FILE_READ_ONLY_VOLUME))
587         {
588
589             AFSDbgTrace(( AFS_SUBSYSTEM_FILE_PROCESSING,
590                           AFS_TRACE_LEVEL_ERROR,
591                           "AFSSetFileInfo Request failed due to read only volume\n",
592                           Irp));
593
594             try_return( ntStatus = STATUS_MEDIA_WRITE_PROTECTED);
595         }
596
597         if( pFcb->Header.NodeTypeCode == AFS_INVALID_FCB &&
598             FileInformationClass != FileDispositionInformation)
599         {
600             AFSDbgTrace(( AFS_SUBSYSTEM_FILE_PROCESSING,
601                           AFS_TRACE_LEVEL_VERBOSE,
602                           "AFSSetFileInfo request against Invalid Fcb\n"));
603
604             try_return( ntStatus = STATUS_ACCESS_DENIED);
605         }
606
607         //
608         // Ensure rename operations are synchronous
609         //
610
611         if( FileInformationClass == FileRenameInformation)
612         {
613
614             bCanQueueRequest = FALSE;
615         }
616
617         //
618         // Store away the parent fid
619         //
620
621         RtlZeroMemory( &stParentFileId,
622                        sizeof( AFSFileID));
623
624         if( BooleanFlagOn( pFcb->ObjectInformation->Flags, AFS_OBJECT_FLAGS_PARENT_FID))
625         {
626
627             stParentFileId = pFcb->ObjectInformation->ParentFileId;
628         }
629
630         //
631         // Process the request
632         //
633
634         switch( FileInformationClass)
635         {
636
637             case FileBasicInformation:
638             {
639
640                 bUpdateFileInfo = TRUE;
641
642                 ntStatus = AFSSetBasicInfo( Irp,
643                                             pCcb->DirectoryCB);
644
645                 break;
646             }
647
648             case FileDispositionInformation:
649             {
650
651                 ntStatus = AFSSetDispositionInfo( Irp,
652                                                   pCcb->DirectoryCB);
653
654                 break;
655             }
656
657             case FileRenameInformation:
658             {
659
660                 ntStatus = AFSSetRenameInfo( Irp);
661
662                 break;
663             }
664
665             case FilePositionInformation:
666             {
667
668                 ntStatus = AFSSetPositionInfo( Irp,
669                                                pCcb->DirectoryCB);
670
671                 break;
672             }
673
674             case FileLinkInformation:
675             {
676
677                 ntStatus = AFSSetFileLinkInfo( Irp);
678
679                 break;
680             }
681
682             case FileAllocationInformation:
683             {
684
685                 ntStatus = AFSSetAllocationInfo( Irp,
686                                                  pCcb->DirectoryCB);
687
688                 break;
689             }
690
691             case FileEndOfFileInformation:
692             {
693
694                 ntStatus = AFSSetEndOfFileInfo( Irp,
695                                                 pCcb->DirectoryCB);
696
697                 break;
698             }
699
700             default:
701
702                 ntStatus = STATUS_INVALID_PARAMETER;
703
704                 break;
705         }
706
707 try_exit:
708
709         if( bReleaseMain)
710         {
711
712             AFSReleaseResource( &pFcb->NPFcb->Resource);
713         }
714
715         if( NT_SUCCESS( ntStatus) &&
716             bUpdateFileInfo)
717         {
718
719             ntStatus = AFSUpdateFileInformation( &stParentFileId,
720                                                  pFcb->ObjectInformation,
721                                                  &pCcb->AuthGroup);
722
723             if( !NT_SUCCESS( ntStatus))
724             {
725
726                 AFSAcquireExcl( &pFcb->NPFcb->Resource,
727                                 TRUE);
728
729                 //
730                 // Unwind the update and fail the request
731                 //
732
733                 AFSUnwindFileInfo( pFcb,
734                                    pCcb);
735
736                 AFSDbgTrace(( AFS_SUBSYSTEM_FILE_PROCESSING,
737                               AFS_TRACE_LEVEL_ERROR,
738                               "AFSSetFileInfo Failed to send file info update to service request for %wZ FID %08lX-%08lX-%08lX-%08lX Status %08lX\n",
739                               &pCcb->DirectoryCB->NameInformation.FileName,
740                               pFcb->ObjectInformation->FileId.Cell,
741                               pFcb->ObjectInformation->FileId.Volume,
742                               pFcb->ObjectInformation->FileId.Vnode,
743                               pFcb->ObjectInformation->FileId.Unique,
744                               ntStatus));
745
746                 AFSReleaseResource( &pFcb->NPFcb->Resource);
747             }
748         }
749
750         if( !NT_SUCCESS( ntStatus))
751         {
752
753             if( pCcb != NULL &&
754                 pCcb->DirectoryCB != NULL)
755             {
756
757                 AFSDbgTrace(( AFS_SUBSYSTEM_FILE_PROCESSING,
758                               AFS_TRACE_LEVEL_ERROR,
759                               "AFSSetFileInfo Failed to process request for %wZ FID %08lX-%08lX-%08lX-%08lX Status %08lX\n",
760                               &pCcb->DirectoryCB->NameInformation.FileName,
761                               pFcb->ObjectInformation->FileId.Cell,
762                               pFcb->ObjectInformation->FileId.Volume,
763                               pFcb->ObjectInformation->FileId.Vnode,
764                               pFcb->ObjectInformation->FileId.Unique,
765                               ntStatus));
766             }
767         }
768     }
769     __except( AFSExceptionFilter( __FUNCTION__, GetExceptionCode(), GetExceptionInformation()) )
770     {
771
772         AFSDbgTrace(( 0,
773                       0,
774                       "EXCEPTION - AFSSetFileInfo\n"));
775
776         AFSDumpTraceFilesFnc();
777
778         ntStatus = STATUS_UNSUCCESSFUL;
779
780         if( bReleaseMain)
781         {
782
783             AFSReleaseResource( &pFcb->NPFcb->Resource);
784         }
785     }
786
787     AFSCompleteRequest( Irp,
788                         ntStatus);
789
790     return ntStatus;
791 }
792
793 //
794 // Function: AFSQueryBasicInfo
795 //
796 // Description:
797 //
798 //      This function is the handler for the query basic information request
799 //
800 // Return:
801 //
802 //      A status is returned for the function
803 //
804
805 NTSTATUS
806 AFSQueryBasicInfo( IN PIRP Irp,
807                    IN AFSDirectoryCB *DirectoryCB,
808                    IN OUT PFILE_BASIC_INFORMATION Buffer,
809                    IN OUT PLONG Length)
810 {
811     NTSTATUS ntStatus = STATUS_SUCCESS;
812     AFSDeviceExt *pDeviceExt = (AFSDeviceExt *)AFSRDRDeviceObject->DeviceExtension;
813     ULONG ulFileAttribs = 0;
814     AFSFcb *pFcb = NULL;
815     AFSCcb *pCcb = NULL;
816     IO_STACK_LOCATION *pIrpSp = IoGetCurrentIrpStackLocation( Irp);
817     AFSFileInfoCB stFileInfo;
818     AFSDirectoryCB *pParentDirectoryCB = NULL;
819     UNICODE_STRING uniParentPath;
820
821     if( *Length >= sizeof( FILE_BASIC_INFORMATION))
822     {
823
824         RtlZeroMemory( Buffer,
825                        *Length);
826
827         ulFileAttribs = DirectoryCB->ObjectInformation->FileAttributes;
828
829         pFcb = (AFSFcb *)pIrpSp->FileObject->FsContext;
830         pCcb = (AFSCcb *)pIrpSp->FileObject->FsContext2;
831
832         if( DirectoryCB->ObjectInformation->FileType == AFS_FILE_TYPE_SYMLINK)
833         {
834
835             pParentDirectoryCB = AFSGetParentEntry( pCcb->NameArray);
836
837             AFSRetrieveParentPath( &pCcb->FullFileName,
838                                    &uniParentPath);
839
840             RtlZeroMemory( &stFileInfo,
841                            sizeof( AFSFileInfoCB));
842
843             //
844             // Can't hold the Fcb while evaluating the path, leads to lock inversion
845             //
846
847             AFSReleaseResource( &pFcb->NPFcb->Resource);
848
849             //
850             // Its a reparse point regardless of whether the file attributes
851             // can be retrieved for the target.
852             //
853
854             if ( ulFileAttribs == FILE_ATTRIBUTE_NORMAL)
855             {
856
857                 ulFileAttribs = FILE_ATTRIBUTE_REPARSE_POINT;
858             }
859             else
860             {
861
862                 ulFileAttribs |= FILE_ATTRIBUTE_REPARSE_POINT;
863             }
864
865             if( NT_SUCCESS( AFSRetrieveFileAttributes( pParentDirectoryCB,
866                                                        DirectoryCB,
867                                                        &uniParentPath,
868                                                        pCcb->NameArray,
869                                                        &pCcb->AuthGroup,
870                                                        &stFileInfo)))
871             {
872
873                 if ( stFileInfo.FileAttributes & FILE_ATTRIBUTE_DIRECTORY)
874                 {
875
876                     ulFileAttribs |= FILE_ATTRIBUTE_DIRECTORY;
877                 }
878             }
879
880             AFSAcquireShared( &pFcb->NPFcb->Resource,
881                               TRUE);
882         }
883
884
885         AFSDbgTrace(( AFS_SUBSYSTEM_FILE_PROCESSING,
886                       AFS_TRACE_LEVEL_VERBOSE_2,
887                       "AFSQueryBasicInfo %wZ Type 0x%x Attrib 0x%x -> 0x%x\n",
888                       &pCcb->DirectoryCB->NameInformation.FileName,
889                       pFcb->ObjectInformation->FileType,
890                       pFcb->ObjectInformation->FileAttributes,
891                       ulFileAttribs));
892
893         Buffer->CreationTime = DirectoryCB->ObjectInformation->CreationTime;
894         Buffer->LastAccessTime = DirectoryCB->ObjectInformation->LastAccessTime;
895         Buffer->LastWriteTime = DirectoryCB->ObjectInformation->LastWriteTime;
896         Buffer->ChangeTime = DirectoryCB->ObjectInformation->ChangeTime;
897         Buffer->FileAttributes = ulFileAttribs;
898
899         if( DirectoryCB->NameInformation.FileName.Buffer[ 0] == L'.' &&
900             BooleanFlagOn( pDeviceExt->DeviceFlags, AFS_DEVICE_FLAG_HIDE_DOT_NAMES))
901         {
902
903             if ( Buffer->FileAttributes != FILE_ATTRIBUTE_NORMAL)
904             {
905                 Buffer->FileAttributes |= FILE_ATTRIBUTE_HIDDEN;
906             }
907             else
908             {
909                 Buffer->FileAttributes = FILE_ATTRIBUTE_HIDDEN;
910             }
911         }
912
913         *Length -= sizeof( FILE_BASIC_INFORMATION);
914     }
915     else
916     {
917
918         ntStatus = STATUS_BUFFER_TOO_SMALL;
919     }
920
921     return ntStatus;
922 }
923
924 NTSTATUS
925 AFSQueryStandardInfo( IN PIRP Irp,
926                       IN AFSDirectoryCB *DirectoryCB,
927                       IN OUT PFILE_STANDARD_INFORMATION Buffer,
928                       IN OUT PLONG Length)
929 {
930
931     NTSTATUS ntStatus = STATUS_SUCCESS;
932     AFSFcb *pFcb = NULL;
933     AFSCcb *pCcb = NULL;
934     PIO_STACK_LOCATION pIrpSp = IoGetCurrentIrpStackLocation( Irp);
935     AFSFileInfoCB stFileInfo;
936     AFSDirectoryCB *pParentDirectoryCB = NULL;
937     UNICODE_STRING uniParentPath;
938     ULONG ulFileAttribs = 0;
939
940     if( *Length >= sizeof( FILE_STANDARD_INFORMATION))
941     {
942
943         pFcb = (AFSFcb *)pIrpSp->FileObject->FsContext;
944         pCcb = (AFSCcb *)pIrpSp->FileObject->FsContext2;
945
946         RtlZeroMemory( Buffer,
947                        *Length);
948
949         Buffer->NumberOfLinks = 1;
950         Buffer->DeletePending = BooleanFlagOn( pCcb->DirectoryCB->Flags, AFS_DIR_ENTRY_PENDING_DELETE);
951
952         Buffer->AllocationSize.QuadPart = (ULONGLONG)((DirectoryCB->ObjectInformation->AllocationSize.QuadPart/PAGE_SIZE) + 1) * PAGE_SIZE;
953
954         Buffer->EndOfFile = DirectoryCB->ObjectInformation->EndOfFile;
955
956         ulFileAttribs = DirectoryCB->ObjectInformation->FileAttributes;
957
958         if( DirectoryCB->ObjectInformation->FileType == AFS_FILE_TYPE_SYMLINK)
959         {
960
961             pParentDirectoryCB = AFSGetParentEntry( pCcb->NameArray);
962
963             AFSRetrieveParentPath( &pCcb->FullFileName,
964                                    &uniParentPath);
965
966             RtlZeroMemory( &stFileInfo,
967                            sizeof( AFSFileInfoCB));
968
969             //
970             // Can't hold the Fcb while evaluating the path, leads to lock inversion
971             //
972
973             AFSReleaseResource( &pFcb->NPFcb->Resource);
974
975             //
976             // Its a reparse point regardless of whether or not the
977             // file attributes can be retrieved.
978             //
979
980             if ( ulFileAttribs == FILE_ATTRIBUTE_NORMAL)
981             {
982
983                 ulFileAttribs = FILE_ATTRIBUTE_REPARSE_POINT;
984             }
985             else
986             {
987
988                 ulFileAttribs |= FILE_ATTRIBUTE_REPARSE_POINT;
989             }
990
991             if( NT_SUCCESS( AFSRetrieveFileAttributes( pParentDirectoryCB,
992                                                        DirectoryCB,
993                                                        &uniParentPath,
994                                                        pCcb->NameArray,
995                                                        &pCcb->AuthGroup,
996                                                        &stFileInfo)))
997             {
998
999                 if ( stFileInfo.FileAttributes & FILE_ATTRIBUTE_DIRECTORY)
1000                 {
1001
1002                     ulFileAttribs |= FILE_ATTRIBUTE_DIRECTORY;
1003                 }
1004             }
1005
1006             AFSAcquireShared( &pFcb->NPFcb->Resource,
1007                               TRUE);
1008         }
1009
1010         AFSDbgTrace(( AFS_SUBSYSTEM_FILE_PROCESSING,
1011                       AFS_TRACE_LEVEL_VERBOSE_2,
1012                       "AFSQueryStandardInfo %wZ Type 0x%x Attrib 0x%x -> 0x%x\n",
1013                       &pCcb->DirectoryCB->NameInformation.FileName,
1014                       pFcb->ObjectInformation->FileType,
1015                       pFcb->ObjectInformation->FileAttributes,
1016                       ulFileAttribs));
1017
1018         Buffer->Directory = BooleanFlagOn( ulFileAttribs, FILE_ATTRIBUTE_DIRECTORY);
1019
1020         *Length -= sizeof( FILE_STANDARD_INFORMATION);
1021     }
1022     else
1023     {
1024
1025         ntStatus = STATUS_BUFFER_TOO_SMALL;
1026     }
1027
1028     return ntStatus;
1029 }
1030
1031 NTSTATUS
1032 AFSQueryInternalInfo( IN PIRP Irp,
1033                       IN AFSFcb *Fcb,
1034                       IN OUT PFILE_INTERNAL_INFORMATION Buffer,
1035                       IN OUT PLONG Length)
1036 {
1037
1038     UNREFERENCED_PARAMETER(Irp);
1039     NTSTATUS ntStatus = STATUS_SUCCESS;
1040
1041     if( *Length >= sizeof( FILE_INTERNAL_INFORMATION))
1042     {
1043
1044         Buffer->IndexNumber.HighPart = Fcb->ObjectInformation->FileId.Vnode;
1045
1046         Buffer->IndexNumber.LowPart = Fcb->ObjectInformation->FileId.Unique;
1047
1048         *Length -= sizeof( FILE_INTERNAL_INFORMATION);
1049     }
1050     else
1051     {
1052
1053         ntStatus = STATUS_BUFFER_TOO_SMALL;
1054     }
1055
1056     return ntStatus;
1057 }
1058
1059 NTSTATUS
1060 AFSQueryEaInfo( IN PIRP Irp,
1061                 IN AFSDirectoryCB *DirectoryCB,
1062                 IN OUT PFILE_EA_INFORMATION Buffer,
1063                 IN OUT PLONG Length)
1064 {
1065
1066     UNREFERENCED_PARAMETER(Irp);
1067     UNREFERENCED_PARAMETER(DirectoryCB);
1068     NTSTATUS ntStatus = STATUS_SUCCESS;
1069
1070     RtlZeroMemory( Buffer,
1071                    *Length);
1072
1073     if( *Length >= sizeof( FILE_EA_INFORMATION))
1074     {
1075
1076         Buffer->EaSize = 0;
1077
1078         *Length -= sizeof( FILE_EA_INFORMATION);
1079     }
1080     else
1081     {
1082
1083         ntStatus = STATUS_BUFFER_TOO_SMALL;
1084     }
1085
1086     return ntStatus;
1087 }
1088
1089 NTSTATUS
1090 AFSQueryPositionInfo( IN PIRP Irp,
1091                       IN AFSFcb *Fcb,
1092                       IN OUT PFILE_POSITION_INFORMATION Buffer,
1093                       IN OUT PLONG Length)
1094 {
1095
1096     UNREFERENCED_PARAMETER(Fcb);
1097     NTSTATUS ntStatus = STATUS_SUCCESS;
1098     PIO_STACK_LOCATION pIrpSp = IoGetCurrentIrpStackLocation( Irp);
1099
1100     if( *Length >= sizeof( FILE_POSITION_INFORMATION))
1101     {
1102
1103         RtlZeroMemory( Buffer,
1104                        *Length);
1105
1106         Buffer->CurrentByteOffset.QuadPart = pIrpSp->FileObject->CurrentByteOffset.QuadPart;
1107
1108         *Length -= sizeof( FILE_POSITION_INFORMATION);
1109     }
1110     else
1111     {
1112
1113         ntStatus = STATUS_BUFFER_TOO_SMALL;
1114     }
1115
1116     return ntStatus;
1117 }
1118
1119 NTSTATUS
1120 AFSQueryAccess( IN PIRP Irp,
1121                 IN AFSFcb *Fcb,
1122                 IN OUT PFILE_ACCESS_INFORMATION Buffer,
1123                 IN OUT PLONG Length)
1124 {
1125
1126     UNREFERENCED_PARAMETER(Irp);
1127     UNREFERENCED_PARAMETER(Fcb);
1128     NTSTATUS ntStatus = STATUS_SUCCESS;
1129
1130     if( *Length >= sizeof( FILE_ACCESS_INFORMATION))
1131     {
1132
1133         RtlZeroMemory( Buffer,
1134                        *Length);
1135
1136         Buffer->AccessFlags = 0;
1137
1138         *Length -= sizeof( FILE_ACCESS_INFORMATION);
1139     }
1140     else
1141     {
1142
1143         ntStatus = STATUS_BUFFER_TOO_SMALL;
1144     }
1145
1146     return ntStatus;
1147 }
1148
1149 NTSTATUS
1150 AFSQueryMode( IN PIRP Irp,
1151               IN AFSFcb *Fcb,
1152               IN OUT PFILE_MODE_INFORMATION Buffer,
1153               IN OUT PLONG Length)
1154 {
1155
1156     UNREFERENCED_PARAMETER(Irp);
1157     UNREFERENCED_PARAMETER(Fcb);
1158     NTSTATUS ntStatus = STATUS_SUCCESS;
1159
1160     if( *Length >= sizeof( FILE_MODE_INFORMATION))
1161     {
1162
1163         RtlZeroMemory( Buffer,
1164                        *Length);
1165
1166         Buffer->Mode = 0;
1167
1168         *Length -= sizeof( FILE_MODE_INFORMATION);
1169     }
1170     else
1171     {
1172
1173         ntStatus = STATUS_BUFFER_TOO_SMALL;
1174     }
1175
1176     return ntStatus;
1177 }
1178
1179 NTSTATUS
1180 AFSQueryAlignment( IN PIRP Irp,
1181                    IN AFSFcb *Fcb,
1182                    IN OUT PFILE_ALIGNMENT_INFORMATION Buffer,
1183                    IN OUT PLONG Length)
1184 {
1185
1186     UNREFERENCED_PARAMETER(Irp);
1187     UNREFERENCED_PARAMETER(Fcb);
1188     NTSTATUS ntStatus = STATUS_SUCCESS;
1189
1190     if( *Length >= sizeof( FILE_ALIGNMENT_INFORMATION))
1191     {
1192
1193         RtlZeroMemory( Buffer,
1194                        *Length);
1195
1196         Buffer->AlignmentRequirement = 1;
1197
1198         *Length -= sizeof( FILE_ALIGNMENT_INFORMATION);
1199     }
1200     else
1201     {
1202
1203         ntStatus = STATUS_BUFFER_TOO_SMALL;
1204     }
1205
1206     return ntStatus;
1207 }
1208
1209 NTSTATUS
1210 AFSQueryNameInfo( IN PIRP Irp,
1211                   IN AFSDirectoryCB *DirectoryCB,
1212                   IN OUT PFILE_NAME_INFORMATION Buffer,
1213                   IN OUT PLONG Length)
1214 {
1215
1216     UNREFERENCED_PARAMETER(DirectoryCB);
1217     NTSTATUS ntStatus = STATUS_SUCCESS;
1218     ULONG ulCopyLength = 0;
1219     ULONG cchCopied = 0;
1220     AFSFcb *pFcb = NULL;
1221     AFSCcb *pCcb = NULL;
1222     IO_STACK_LOCATION *pIrpSp = IoGetCurrentIrpStackLocation( Irp);
1223     BOOLEAN bAddLeadingSlash = FALSE;
1224     BOOLEAN bAddTrailingSlash = FALSE;
1225     USHORT usFullNameLength = 0;
1226
1227     pFcb = (AFSFcb *)pIrpSp->FileObject->FsContext;
1228
1229     pCcb = (AFSCcb *)pIrpSp->FileObject->FsContext2;
1230
1231     if( *Length >= FIELD_OFFSET( FILE_NAME_INFORMATION, FileName))
1232     {
1233
1234         RtlZeroMemory( Buffer,
1235                        *Length);
1236
1237         if( pCcb->FullFileName.Length == 0 ||
1238             pCcb->FullFileName.Buffer[ 0] != L'\\')
1239         {
1240             bAddLeadingSlash = TRUE;
1241         }
1242
1243         if( pFcb->ObjectInformation->FileType == AFS_FILE_TYPE_DIRECTORY &&
1244             pCcb->FullFileName.Length > 0 &&
1245             pCcb->FullFileName.Buffer[ (pCcb->FullFileName.Length/sizeof( WCHAR)) - 1] != L'\\')
1246         {
1247             bAddTrailingSlash = TRUE;
1248         }
1249
1250         usFullNameLength = sizeof( WCHAR) +
1251                                     AFSServerName.Length +
1252                                     pCcb->FullFileName.Length;
1253
1254         if( bAddLeadingSlash)
1255         {
1256             usFullNameLength += sizeof( WCHAR);
1257         }
1258
1259         if( bAddTrailingSlash)
1260         {
1261             usFullNameLength += sizeof( WCHAR);
1262         }
1263
1264         if( *Length >= (LONG)(FIELD_OFFSET( FILE_NAME_INFORMATION, FileName) + (LONG)usFullNameLength))
1265         {
1266
1267             ulCopyLength = (LONG)usFullNameLength;
1268         }
1269         else
1270         {
1271
1272             ulCopyLength = *Length - FIELD_OFFSET( FILE_NAME_INFORMATION, FileName);
1273
1274             ntStatus = STATUS_BUFFER_OVERFLOW;
1275         }
1276
1277         Buffer->FileNameLength = (ULONG)usFullNameLength;
1278
1279         *Length -= FIELD_OFFSET( FILE_NAME_INFORMATION, FileName);
1280
1281         if( ulCopyLength > 0)
1282         {
1283
1284             Buffer->FileName[ 0] = L'\\';
1285             ulCopyLength -= sizeof( WCHAR);
1286
1287             *Length -= sizeof( WCHAR);
1288             cchCopied += 1;
1289
1290             if( ulCopyLength >= AFSServerName.Length)
1291             {
1292
1293                 RtlCopyMemory( &Buffer->FileName[ 1],
1294                                AFSServerName.Buffer,
1295                                AFSServerName.Length);
1296
1297                 ulCopyLength -= AFSServerName.Length;
1298                 *Length -= AFSServerName.Length;
1299                 cchCopied += AFSServerName.Length/sizeof( WCHAR);
1300
1301                 if ( ulCopyLength > 0 &&
1302                      bAddLeadingSlash)
1303                 {
1304
1305                     Buffer->FileName[ cchCopied] = L'\\';
1306
1307                     ulCopyLength -= sizeof( WCHAR);
1308                     *Length -= sizeof( WCHAR);
1309                     cchCopied++;
1310                 }
1311
1312                 if( ulCopyLength >= pCcb->FullFileName.Length)
1313                 {
1314
1315                     RtlCopyMemory( &Buffer->FileName[ cchCopied],
1316                                    pCcb->FullFileName.Buffer,
1317                                    pCcb->FullFileName.Length);
1318
1319                     ulCopyLength -= pCcb->FullFileName.Length;
1320                     *Length -= pCcb->FullFileName.Length;
1321                     cchCopied += pCcb->FullFileName.Length/sizeof( WCHAR);
1322
1323                     if( ulCopyLength > 0 &&
1324                         bAddTrailingSlash)
1325                     {
1326                         Buffer->FileName[ cchCopied] = L'\\';
1327
1328                         *Length -= sizeof( WCHAR);
1329                     }
1330                 }
1331                 else
1332                 {
1333
1334                     RtlCopyMemory( &Buffer->FileName[ cchCopied],
1335                                    pCcb->FullFileName.Buffer,
1336                                    ulCopyLength);
1337
1338                     *Length -= ulCopyLength;
1339                 }
1340             }
1341         }
1342     }
1343     else
1344     {
1345
1346         ntStatus = STATUS_BUFFER_TOO_SMALL;
1347     }
1348
1349     return ntStatus;
1350 }
1351
1352 NTSTATUS
1353 AFSQueryShortNameInfo( IN PIRP Irp,
1354                        IN AFSDirectoryCB *DirectoryCB,
1355                        IN OUT PFILE_NAME_INFORMATION Buffer,
1356                        IN OUT PLONG Length)
1357 {
1358
1359     UNREFERENCED_PARAMETER(Irp);
1360     NTSTATUS ntStatus = STATUS_BUFFER_TOO_SMALL;
1361     ULONG ulCopyLength = 0;
1362
1363     RtlZeroMemory( Buffer,
1364                    *Length);
1365
1366     if( DirectoryCB->NameInformation.ShortNameLength == 0)
1367     {
1368
1369         //
1370         // The short name IS the long name
1371         //
1372
1373         if( *Length >= (LONG)FIELD_OFFSET( FILE_NAME_INFORMATION, FileName))
1374         {
1375
1376             if( *Length >= (LONG)(FIELD_OFFSET( FILE_NAME_INFORMATION, FileName) + (LONG)DirectoryCB->NameInformation.FileName.Length))
1377             {
1378
1379                 ulCopyLength = (LONG)DirectoryCB->NameInformation.FileName.Length;
1380
1381                 ntStatus = STATUS_SUCCESS;
1382             }
1383             else
1384             {
1385
1386                 ulCopyLength = *Length - FIELD_OFFSET( FILE_NAME_INFORMATION, FileName);
1387
1388                 ntStatus = STATUS_BUFFER_OVERFLOW;
1389             }
1390
1391             Buffer->FileNameLength = DirectoryCB->NameInformation.FileName.Length;
1392
1393             *Length -= FIELD_OFFSET( FILE_NAME_INFORMATION, FileName);
1394
1395             if( ulCopyLength > 0)
1396             {
1397
1398                 RtlCopyMemory( Buffer->FileName,
1399                                DirectoryCB->NameInformation.FileName.Buffer,
1400                                ulCopyLength);
1401
1402                 *Length -= ulCopyLength;
1403             }
1404         }
1405     }
1406     else
1407     {
1408
1409         if( *Length >= (LONG)FIELD_OFFSET( FILE_NAME_INFORMATION, FileName))
1410         {
1411
1412             if( *Length >= (LONG)(FIELD_OFFSET( FILE_NAME_INFORMATION, FileName) + (LONG)DirectoryCB->NameInformation.FileName.Length))
1413             {
1414
1415                 ulCopyLength = (LONG)DirectoryCB->NameInformation.ShortNameLength;
1416
1417                 ntStatus = STATUS_SUCCESS;
1418             }
1419             else
1420             {
1421
1422                 ulCopyLength = *Length - FIELD_OFFSET( FILE_NAME_INFORMATION, FileName);
1423
1424                 ntStatus = STATUS_BUFFER_OVERFLOW;
1425             }
1426
1427             Buffer->FileNameLength = DirectoryCB->NameInformation.ShortNameLength;
1428
1429             *Length -= FIELD_OFFSET( FILE_NAME_INFORMATION, FileName);
1430
1431             if( ulCopyLength > 0)
1432             {
1433
1434                 RtlCopyMemory( Buffer->FileName,
1435                                DirectoryCB->NameInformation.ShortName,
1436                                Buffer->FileNameLength);
1437
1438                 *Length -= ulCopyLength;
1439             }
1440         }
1441     }
1442
1443     return ntStatus;
1444 }
1445
1446 NTSTATUS
1447 AFSQueryNetworkInfo( IN PIRP Irp,
1448                      IN AFSDirectoryCB *DirectoryCB,
1449                      IN OUT PFILE_NETWORK_OPEN_INFORMATION Buffer,
1450                      IN OUT PLONG Length)
1451 {
1452
1453     NTSTATUS ntStatus = STATUS_SUCCESS;
1454     AFSDeviceExt *pDeviceExt = (AFSDeviceExt *)AFSRDRDeviceObject->DeviceExtension;
1455     AFSFcb *pFcb = NULL;
1456     AFSCcb *pCcb = NULL;
1457     PIO_STACK_LOCATION pIrpSp = IoGetCurrentIrpStackLocation( Irp);
1458     AFSFileInfoCB stFileInfo;
1459     AFSDirectoryCB *pParentDirectoryCB = NULL;
1460     UNICODE_STRING uniParentPath;
1461     ULONG ulFileAttribs = 0;
1462
1463     RtlZeroMemory( Buffer,
1464                    *Length);
1465
1466     if( *Length >= sizeof( FILE_NETWORK_OPEN_INFORMATION))
1467     {
1468
1469         ulFileAttribs = DirectoryCB->ObjectInformation->FileAttributes;
1470
1471         pFcb = (AFSFcb *)pIrpSp->FileObject->FsContext;
1472         pCcb = (AFSCcb *)pIrpSp->FileObject->FsContext2;
1473
1474         if( DirectoryCB->ObjectInformation->FileType == AFS_FILE_TYPE_SYMLINK)
1475         {
1476
1477             pParentDirectoryCB = AFSGetParentEntry( pCcb->NameArray);
1478
1479             AFSRetrieveParentPath( &pCcb->FullFileName,
1480                                    &uniParentPath);
1481
1482             RtlZeroMemory( &stFileInfo,
1483                            sizeof( AFSFileInfoCB));
1484
1485             //
1486             // Can't hold the Fcb while evaluating the path, leads to lock inversion
1487             //
1488
1489             AFSReleaseResource( &pFcb->NPFcb->Resource);
1490
1491             //
1492             // Its a reparse point regardless of whether the file attributes
1493             // can be retrieved for the target.
1494             //
1495
1496             if ( ulFileAttribs == FILE_ATTRIBUTE_NORMAL)
1497             {
1498
1499                 ulFileAttribs = FILE_ATTRIBUTE_REPARSE_POINT;
1500             }
1501             else
1502             {
1503
1504                 ulFileAttribs |= FILE_ATTRIBUTE_REPARSE_POINT;
1505             }
1506
1507             if( NT_SUCCESS( AFSRetrieveFileAttributes( pParentDirectoryCB,
1508                                                        DirectoryCB,
1509                                                        &uniParentPath,
1510                                                        pCcb->NameArray,
1511                                                        &pCcb->AuthGroup,
1512                                                        &stFileInfo)))
1513             {
1514
1515                 if ( stFileInfo.FileAttributes & FILE_ATTRIBUTE_DIRECTORY)
1516                 {
1517
1518                     ulFileAttribs |= FILE_ATTRIBUTE_DIRECTORY;
1519                 }
1520             }
1521
1522             AFSAcquireShared( &pFcb->NPFcb->Resource,
1523                               TRUE);
1524         }
1525
1526         AFSDbgTrace(( AFS_SUBSYSTEM_FILE_PROCESSING,
1527                       AFS_TRACE_LEVEL_VERBOSE_2,
1528                       "AFSQueryNetworkInfo %wZ Type 0x%x Attrib 0x%x -> 0x%x\n",
1529                       &pCcb->DirectoryCB->NameInformation.FileName,
1530                       pFcb->ObjectInformation->FileType,
1531                       pFcb->ObjectInformation->FileAttributes,
1532                       ulFileAttribs));
1533
1534         Buffer->CreationTime.QuadPart = DirectoryCB->ObjectInformation->CreationTime.QuadPart;
1535         Buffer->LastAccessTime.QuadPart = DirectoryCB->ObjectInformation->LastAccessTime.QuadPart;
1536         Buffer->LastWriteTime.QuadPart = DirectoryCB->ObjectInformation->LastWriteTime.QuadPart;
1537         Buffer->ChangeTime.QuadPart = DirectoryCB->ObjectInformation->ChangeTime.QuadPart;
1538
1539         Buffer->AllocationSize.QuadPart = DirectoryCB->ObjectInformation->AllocationSize.QuadPart;
1540         Buffer->EndOfFile.QuadPart = DirectoryCB->ObjectInformation->EndOfFile.QuadPart;
1541
1542         Buffer->FileAttributes = ulFileAttribs;
1543
1544         if( DirectoryCB->NameInformation.FileName.Buffer[ 0] == L'.' &&
1545             BooleanFlagOn( pDeviceExt->DeviceFlags, AFS_DEVICE_FLAG_HIDE_DOT_NAMES))
1546         {
1547
1548             if ( Buffer->FileAttributes != FILE_ATTRIBUTE_NORMAL)
1549             {
1550
1551                 Buffer->FileAttributes |= FILE_ATTRIBUTE_HIDDEN;
1552             }
1553             else
1554             {
1555
1556                 Buffer->FileAttributes = FILE_ATTRIBUTE_HIDDEN;
1557             }
1558         }
1559
1560         *Length -= sizeof( FILE_NETWORK_OPEN_INFORMATION);
1561     }
1562     else
1563     {
1564
1565         ntStatus = STATUS_BUFFER_TOO_SMALL;
1566     }
1567
1568     return ntStatus;
1569 }
1570
1571 NTSTATUS
1572 AFSQueryStreamInfo( IN PIRP Irp,
1573                     IN AFSDirectoryCB *DirectoryCB,
1574                     IN OUT FILE_STREAM_INFORMATION *Buffer,
1575                     IN OUT PLONG Length)
1576 {
1577
1578     UNREFERENCED_PARAMETER(Irp);
1579     NTSTATUS ntStatus = STATUS_BUFFER_TOO_SMALL;
1580     ULONG ulCopyLength = 0;
1581
1582     if( *Length >= FIELD_OFFSET( FILE_STREAM_INFORMATION, StreamName))
1583     {
1584
1585         RtlZeroMemory( Buffer,
1586                        *Length);
1587
1588         Buffer->NextEntryOffset = 0;
1589
1590
1591         if( !BooleanFlagOn( DirectoryCB->ObjectInformation->FileAttributes, FILE_ATTRIBUTE_DIRECTORY))
1592         {
1593
1594             if( *Length >= (LONG)(FIELD_OFFSET( FILE_STREAM_INFORMATION, StreamName) + 14))  // ::$DATA
1595             {
1596
1597                 ulCopyLength = 14;
1598
1599                 ntStatus = STATUS_SUCCESS;
1600             }
1601             else
1602             {
1603
1604                 ulCopyLength = *Length - FIELD_OFFSET( FILE_STREAM_INFORMATION, StreamName);
1605
1606                 ntStatus = STATUS_BUFFER_OVERFLOW;
1607             }
1608
1609             Buffer->StreamNameLength = 14; // ::$DATA
1610
1611             Buffer->StreamSize.QuadPart = DirectoryCB->ObjectInformation->EndOfFile.QuadPart;
1612
1613             Buffer->StreamAllocationSize.QuadPart = DirectoryCB->ObjectInformation->AllocationSize.QuadPart;
1614
1615             *Length -= FIELD_OFFSET( FILE_STREAM_INFORMATION, StreamName);
1616
1617             if( ulCopyLength > 0)
1618             {
1619
1620                 RtlCopyMemory( Buffer->StreamName,
1621                                L"::$DATA",
1622                                ulCopyLength);
1623
1624                 *Length -= ulCopyLength;
1625             }
1626         }
1627         else
1628         {
1629
1630             Buffer->StreamNameLength = 0;       // No stream for a directory
1631
1632             // The response size is zero
1633
1634             ntStatus = STATUS_SUCCESS;
1635         }
1636     }
1637
1638     return ntStatus;
1639 }
1640
1641 NTSTATUS
1642 AFSQueryAttribTagInfo( IN PIRP Irp,
1643                        IN AFSDirectoryCB *DirectoryCB,
1644                        IN OUT FILE_ATTRIBUTE_TAG_INFORMATION *Buffer,
1645                        IN OUT PLONG Length)
1646 {
1647
1648     NTSTATUS ntStatus = STATUS_BUFFER_TOO_SMALL;
1649     AFSDeviceExt *pDeviceExt = (AFSDeviceExt *)AFSRDRDeviceObject->DeviceExtension;
1650     AFSFcb *pFcb = NULL;
1651     AFSCcb *pCcb = NULL;
1652     PIO_STACK_LOCATION pIrpSp = IoGetCurrentIrpStackLocation( Irp);
1653     AFSFileInfoCB stFileInfo;
1654     AFSDirectoryCB *pParentDirectoryCB = NULL;
1655     UNICODE_STRING uniParentPath;
1656     ULONG ulFileAttribs = 0;
1657
1658     if( *Length >= sizeof( FILE_ATTRIBUTE_TAG_INFORMATION))
1659     {
1660
1661         RtlZeroMemory( Buffer,
1662                        *Length);
1663
1664         ulFileAttribs = DirectoryCB->ObjectInformation->FileAttributes;
1665
1666         pFcb = (AFSFcb *)pIrpSp->FileObject->FsContext;
1667         pCcb = (AFSCcb *)pIrpSp->FileObject->FsContext2;
1668
1669         if( DirectoryCB->ObjectInformation->FileType == AFS_FILE_TYPE_SYMLINK)
1670         {
1671
1672             pParentDirectoryCB = AFSGetParentEntry( pCcb->NameArray);
1673
1674             AFSRetrieveParentPath( &pCcb->FullFileName,
1675                                    &uniParentPath);
1676
1677             RtlZeroMemory( &stFileInfo,
1678                            sizeof( AFSFileInfoCB));
1679
1680             //
1681             // Can't hold the Fcb while evaluating the path, leads to lock inversion
1682             //
1683
1684             AFSReleaseResource( &pFcb->NPFcb->Resource);
1685
1686             //
1687             // Its a reparse point regardless of whether the file attributes
1688             // can be retrieved for the target.
1689             //
1690
1691             if ( ulFileAttribs == FILE_ATTRIBUTE_NORMAL)
1692             {
1693
1694                 ulFileAttribs = FILE_ATTRIBUTE_REPARSE_POINT;
1695             }
1696             else
1697             {
1698
1699                 ulFileAttribs |= FILE_ATTRIBUTE_REPARSE_POINT;
1700             }
1701
1702             if( NT_SUCCESS( AFSRetrieveFileAttributes( pParentDirectoryCB,
1703                                                        DirectoryCB,
1704                                                        &uniParentPath,
1705                                                        pCcb->NameArray,
1706                                                        &pCcb->AuthGroup,
1707                                                        &stFileInfo)))
1708             {
1709
1710                 if ( stFileInfo.FileAttributes & FILE_ATTRIBUTE_DIRECTORY)
1711                 {
1712
1713                     ulFileAttribs |= FILE_ATTRIBUTE_DIRECTORY;
1714                 }
1715             }
1716
1717             AFSAcquireShared( &pFcb->NPFcb->Resource,
1718                               TRUE);
1719         }
1720
1721         AFSDbgTrace(( AFS_SUBSYSTEM_FILE_PROCESSING,
1722                       AFS_TRACE_LEVEL_VERBOSE_2,
1723                       "AFSAttribTagInfo %wZ Type 0x%x Attrib 0x%x -> 0x%x\n",
1724                       &pCcb->DirectoryCB->NameInformation.FileName,
1725                       pFcb->ObjectInformation->FileType,
1726                       pFcb->ObjectInformation->FileAttributes,
1727                       ulFileAttribs));
1728
1729         Buffer->FileAttributes = ulFileAttribs;
1730
1731         if( DirectoryCB->NameInformation.FileName.Buffer[ 0] == L'.' &&
1732             BooleanFlagOn( pDeviceExt->DeviceFlags, AFS_DEVICE_FLAG_HIDE_DOT_NAMES))
1733         {
1734
1735             if ( Buffer->FileAttributes != FILE_ATTRIBUTE_NORMAL)
1736             {
1737
1738                 Buffer->FileAttributes |= FILE_ATTRIBUTE_HIDDEN;
1739             }
1740             else
1741             {
1742
1743                 Buffer->FileAttributes = FILE_ATTRIBUTE_HIDDEN;
1744             }
1745         }
1746
1747         if ( DirectoryCB->ObjectInformation->FileType == AFS_FILE_TYPE_MOUNTPOINT)
1748         {
1749
1750             Buffer->ReparseTag = IO_REPARSE_TAG_SURROGATE|IO_REPARSE_TAG_OPENAFS_DFS;
1751         }
1752         else if( BooleanFlagOn( DirectoryCB->ObjectInformation->FileAttributes, FILE_ATTRIBUTE_REPARSE_POINT))
1753         {
1754
1755             Buffer->ReparseTag = IO_REPARSE_TAG_SYMLINK;
1756         }
1757
1758         *Length -= sizeof( FILE_ATTRIBUTE_TAG_INFORMATION);
1759
1760         ntStatus = STATUS_SUCCESS;
1761     }
1762
1763     return ntStatus;
1764 }
1765
1766 NTSTATUS
1767 AFSQueryRemoteProtocolInfo( IN PIRP Irp,
1768                             IN AFSDirectoryCB *DirectoryCB,
1769                             IN OUT FILE_REMOTE_PROTOCOL_INFORMATION *Buffer,
1770                             IN OUT PLONG Length)
1771 {
1772
1773     UNREFERENCED_PARAMETER(Irp);
1774     UNREFERENCED_PARAMETER(DirectoryCB);
1775     NTSTATUS ntStatus = STATUS_BUFFER_TOO_SMALL;
1776
1777     if( *Length >= sizeof( FILE_REMOTE_PROTOCOL_INFORMATION))
1778     {
1779
1780         RtlZeroMemory( Buffer,
1781                        *Length);
1782
1783         Buffer->StructureVersion = 1;
1784
1785         Buffer->StructureSize = sizeof(FILE_REMOTE_PROTOCOL_INFORMATION);
1786
1787         Buffer->Protocol = WNNC_NET_OPENAFS;
1788
1789         Buffer->ProtocolMajorVersion = 3;
1790
1791         Buffer->ProtocolMinorVersion = 0;
1792
1793         Buffer->ProtocolRevision = 0;
1794
1795         *Length -= sizeof( FILE_REMOTE_PROTOCOL_INFORMATION);
1796
1797         ntStatus = STATUS_SUCCESS;
1798     }
1799
1800     return ntStatus;
1801 }
1802
1803 NTSTATUS
1804 AFSQueryPhysicalNameInfo( IN PIRP Irp,
1805                           IN AFSDirectoryCB *DirectoryCB,
1806                           IN OUT PFILE_NETWORK_PHYSICAL_NAME_INFORMATION Buffer,
1807                           IN OUT PLONG Length)
1808 {
1809
1810     UNREFERENCED_PARAMETER(DirectoryCB);
1811     NTSTATUS ntStatus = STATUS_SUCCESS;
1812     ULONG ulCopyLength = 0;
1813     ULONG cchCopied = 0;
1814     AFSFcb *pFcb = NULL;
1815     AFSCcb *pCcb = NULL;
1816     IO_STACK_LOCATION *pIrpSp = IoGetCurrentIrpStackLocation( Irp);
1817     BOOLEAN bAddLeadingSlash = FALSE;
1818     USHORT usFullNameLength = 0;
1819
1820     pFcb = (AFSFcb *)pIrpSp->FileObject->FsContext;
1821
1822     pCcb = (AFSCcb *)pIrpSp->FileObject->FsContext2;
1823
1824     if( *Length >= FIELD_OFFSET( FILE_NETWORK_PHYSICAL_NAME_INFORMATION, FileName))
1825     {
1826
1827         RtlZeroMemory( Buffer,
1828                        *Length);
1829
1830         if( pCcb->FullFileName.Length == 0 ||
1831             pCcb->FullFileName.Buffer[ 0] != L'\\')
1832         {
1833             bAddLeadingSlash = TRUE;
1834         }
1835
1836         usFullNameLength = pCcb->FullFileName.Length;
1837
1838         if( bAddLeadingSlash)
1839         {
1840             usFullNameLength += sizeof( WCHAR);
1841         }
1842
1843         if( *Length >= (LONG)(FIELD_OFFSET( FILE_NETWORK_PHYSICAL_NAME_INFORMATION, FileName) + (LONG)usFullNameLength))
1844         {
1845             ulCopyLength = (LONG)usFullNameLength;
1846         }
1847         else
1848         {
1849
1850             ulCopyLength = *Length - FIELD_OFFSET( FILE_NETWORK_PHYSICAL_NAME_INFORMATION, FileName);
1851
1852             ntStatus = STATUS_BUFFER_OVERFLOW;
1853         }
1854
1855         Buffer->FileNameLength = (ULONG)usFullNameLength;
1856
1857         *Length -= FIELD_OFFSET( FILE_NETWORK_PHYSICAL_NAME_INFORMATION, FileName);
1858
1859         if( ulCopyLength > 0)
1860         {
1861
1862             if( bAddLeadingSlash)
1863             {
1864
1865                 Buffer->FileName[ cchCopied] = L'\\';
1866
1867                 ulCopyLength -= sizeof( WCHAR);
1868                 *Length -= sizeof( WCHAR);
1869                 cchCopied++;
1870             }
1871
1872             if( ulCopyLength >= pCcb->FullFileName.Length)
1873             {
1874
1875                 RtlCopyMemory( &Buffer->FileName[ cchCopied],
1876                                pCcb->FullFileName.Buffer,
1877                                pCcb->FullFileName.Length);
1878
1879                 ulCopyLength -= pCcb->FullFileName.Length;
1880                 *Length -= pCcb->FullFileName.Length;
1881                 cchCopied += pCcb->FullFileName.Length/sizeof( WCHAR);
1882             }
1883             else
1884             {
1885
1886                 RtlCopyMemory( &Buffer->FileName[ cchCopied],
1887                                pCcb->FullFileName.Buffer,
1888                                ulCopyLength);
1889
1890                 *Length -= ulCopyLength;
1891             }
1892         }
1893     }
1894     else
1895     {
1896
1897         ntStatus = STATUS_BUFFER_TOO_SMALL;
1898     }
1899
1900     return ntStatus;
1901 }
1902
1903 NTSTATUS
1904 AFSSetBasicInfo( IN PIRP Irp,
1905                  IN AFSDirectoryCB *DirectoryCB)
1906 {
1907     NTSTATUS ntStatus = STATUS_SUCCESS;
1908     PFILE_BASIC_INFORMATION pBuffer;
1909     PIO_STACK_LOCATION pIrpSp = IoGetCurrentIrpStackLocation( Irp);
1910     ULONG ulNotifyFilter = 0;
1911     AFSCcb *pCcb = NULL;
1912
1913     __Enter
1914     {
1915
1916         pCcb = (AFSCcb *)pIrpSp->FileObject->FsContext2;
1917
1918         pBuffer = (PFILE_BASIC_INFORMATION)Irp->AssociatedIrp.SystemBuffer;
1919
1920         pCcb->FileUnwindInfo.FileAttributes = (ULONG)-1;
1921
1922         if( pBuffer->FileAttributes != (ULONGLONG)0)
1923         {
1924
1925             if( DirectoryCB->ObjectInformation->Fcb->Header.NodeTypeCode == AFS_FILE_FCB &&
1926                 BooleanFlagOn( pBuffer->FileAttributes, FILE_ATTRIBUTE_DIRECTORY))
1927             {
1928
1929                 try_return( ntStatus = STATUS_INVALID_PARAMETER);
1930             }
1931
1932             if( DirectoryCB->ObjectInformation->Fcb->Header.NodeTypeCode == AFS_DIRECTORY_FCB)
1933             {
1934
1935                 pBuffer->FileAttributes |= FILE_ATTRIBUTE_DIRECTORY;
1936             }
1937
1938             pCcb->FileUnwindInfo.FileAttributes = DirectoryCB->ObjectInformation->FileAttributes;
1939
1940             DirectoryCB->ObjectInformation->FileAttributes = pBuffer->FileAttributes;
1941
1942             ulNotifyFilter |= FILE_NOTIFY_CHANGE_ATTRIBUTES;
1943
1944             SetFlag( DirectoryCB->ObjectInformation->Fcb->Flags, AFS_FCB_FLAG_FILE_MODIFIED);
1945         }
1946
1947         pCcb->FileUnwindInfo.CreationTime.QuadPart = (ULONGLONG)-1;
1948
1949         if( pBuffer->CreationTime.QuadPart != (ULONGLONG)-1 &&
1950             pBuffer->CreationTime.QuadPart != (ULONGLONG)0)
1951         {
1952
1953             pCcb->FileUnwindInfo.CreationTime.QuadPart = DirectoryCB->ObjectInformation->CreationTime.QuadPart;
1954
1955             DirectoryCB->ObjectInformation->CreationTime.QuadPart = pBuffer->CreationTime.QuadPart;
1956
1957             ulNotifyFilter |= FILE_NOTIFY_CHANGE_CREATION;
1958
1959             SetFlag( DirectoryCB->ObjectInformation->Fcb->Flags, AFS_FCB_FLAG_FILE_MODIFIED | AFS_FCB_FLAG_UPDATE_CREATE_TIME);
1960         }
1961
1962         pCcb->FileUnwindInfo.LastAccessTime.QuadPart = (ULONGLONG)-1;
1963
1964         if( pBuffer->LastAccessTime.QuadPart != (ULONGLONG)-1 &&
1965             pBuffer->LastAccessTime.QuadPart != (ULONGLONG)0)
1966         {
1967
1968             pCcb->FileUnwindInfo.LastAccessTime.QuadPart = DirectoryCB->ObjectInformation->LastAccessTime.QuadPart;
1969
1970             DirectoryCB->ObjectInformation->LastAccessTime.QuadPart = pBuffer->LastAccessTime.QuadPart;
1971
1972             ulNotifyFilter |= FILE_NOTIFY_CHANGE_LAST_ACCESS;
1973
1974             SetFlag( DirectoryCB->ObjectInformation->Fcb->Flags, AFS_FCB_FLAG_FILE_MODIFIED | AFS_FCB_FLAG_UPDATE_ACCESS_TIME);
1975         }
1976
1977         pCcb->FileUnwindInfo.LastWriteTime.QuadPart = (ULONGLONG)-1;
1978
1979         if( pBuffer->LastWriteTime.QuadPart != (ULONGLONG)-1 &&
1980             pBuffer->LastWriteTime.QuadPart != (ULONGLONG)0)
1981         {
1982
1983             pCcb->FileUnwindInfo.LastWriteTime.QuadPart = DirectoryCB->ObjectInformation->LastWriteTime.QuadPart;
1984
1985             DirectoryCB->ObjectInformation->LastWriteTime.QuadPart = pBuffer->LastWriteTime.QuadPart;
1986
1987             ulNotifyFilter |= FILE_NOTIFY_CHANGE_LAST_WRITE;
1988
1989             SetFlag( DirectoryCB->ObjectInformation->Fcb->Flags, AFS_FCB_FLAG_FILE_MODIFIED | AFS_FCB_FLAG_UPDATE_LAST_WRITE_TIME);
1990         }
1991
1992         pCcb->FileUnwindInfo.ChangeTime.QuadPart = (ULONGLONG)-1;
1993
1994         if( pBuffer->ChangeTime.QuadPart != (ULONGLONG)-1 &&
1995             pBuffer->ChangeTime.QuadPart != (ULONGLONG)0)
1996         {
1997
1998             pCcb->FileUnwindInfo.ChangeTime.QuadPart = DirectoryCB->ObjectInformation->ChangeTime.QuadPart;
1999
2000             DirectoryCB->ObjectInformation->ChangeTime.QuadPart = pBuffer->ChangeTime.QuadPart;
2001
2002             ulNotifyFilter |= FILE_NOTIFY_CHANGE_LAST_ACCESS;
2003
2004             SetFlag( DirectoryCB->ObjectInformation->Fcb->Flags, AFS_FCB_FLAG_FILE_MODIFIED | AFS_FCB_FLAG_UPDATE_CHANGE_TIME);
2005         }
2006
2007         if( ulNotifyFilter > 0)
2008         {
2009
2010             if( BooleanFlagOn( DirectoryCB->ObjectInformation->Flags, AFS_OBJECT_FLAGS_PARENT_FID))
2011             {
2012
2013                 AFSObjectInfoCB * pParentObjectInfo = AFSFindObjectInfo( DirectoryCB->ObjectInformation->VolumeCB,
2014                                                                          &DirectoryCB->ObjectInformation->ParentFileId,
2015                                                                          TRUE);
2016
2017                 if ( pParentObjectInfo != NULL)
2018                 {
2019                     AFSFsRtlNotifyFullReportChange( pParentObjectInfo,
2020                                                     pCcb,
2021                                                     (ULONG)ulNotifyFilter,
2022                                                     (ULONG)FILE_ACTION_MODIFIED);
2023
2024                     AFSReleaseObjectInfo( &pParentObjectInfo);
2025                 }
2026             }
2027         }
2028
2029 try_exit:
2030
2031         NOTHING;
2032     }
2033
2034     return ntStatus;
2035 }
2036
2037 NTSTATUS
2038 AFSSetDispositionInfo( IN PIRP Irp,
2039                        IN AFSDirectoryCB *DirectoryCB)
2040 {
2041     NTSTATUS ntStatus = STATUS_SUCCESS;
2042     PFILE_DISPOSITION_INFORMATION pBuffer;
2043     PIO_STACK_LOCATION pIrpSp = IoGetCurrentIrpStackLocation( Irp);
2044     AFSFcb *pFcb = NULL;
2045     AFSCcb *pCcb = NULL;
2046
2047     __Enter
2048     {
2049
2050         pBuffer = (PFILE_DISPOSITION_INFORMATION)Irp->AssociatedIrp.SystemBuffer;
2051
2052         pFcb = (AFSFcb *)pIrpSp->FileObject->FsContext;
2053
2054         pCcb = (AFSCcb *)pIrpSp->FileObject->FsContext2;
2055
2056         //
2057         // Can't delete the root
2058         //
2059
2060         if( pFcb->Header.NodeTypeCode == AFS_ROOT_FCB)
2061         {
2062
2063             AFSDbgTrace(( AFS_SUBSYSTEM_FILE_PROCESSING,
2064                           AFS_TRACE_LEVEL_ERROR,
2065                           "AFSSetDispositionInfo Attempt to delete root entry\n"));
2066
2067             try_return( ntStatus = STATUS_CANNOT_DELETE);
2068         }
2069
2070         //
2071         // If the file is read only then do not allow the delete
2072         //
2073
2074         if( BooleanFlagOn( DirectoryCB->ObjectInformation->FileAttributes, FILE_ATTRIBUTE_READONLY))
2075         {
2076
2077             AFSDbgTrace(( AFS_SUBSYSTEM_FILE_PROCESSING,
2078                           AFS_TRACE_LEVEL_ERROR,
2079                           "AFSSetDispositionInfo Attempt to delete read only entry %wZ\n",
2080                           &DirectoryCB->NameInformation.FileName));
2081
2082             try_return( ntStatus = STATUS_CANNOT_DELETE);
2083         }
2084
2085         if( pBuffer->DeleteFile)
2086         {
2087
2088             //
2089             // Check if the caller can delete the file
2090             //
2091
2092             ntStatus = AFSNotifyDelete( DirectoryCB,
2093                                         &pCcb->AuthGroup,
2094                                         TRUE);
2095
2096             if( !NT_SUCCESS( ntStatus))
2097             {
2098
2099                 AFSDbgTrace(( AFS_SUBSYSTEM_FILE_PROCESSING,
2100                               AFS_TRACE_LEVEL_ERROR,
2101                               "AFSSetDispositionInfo Cannot delete entry %wZ Status %08lX\n",
2102                               &DirectoryCB->NameInformation.FileName,
2103                               ntStatus));
2104
2105                 try_return( ntStatus);
2106             }
2107
2108             if( pFcb->Header.NodeTypeCode == AFS_DIRECTORY_FCB)
2109             {
2110
2111                 //
2112                 // Reduce the Link count in the object information block
2113                 // to correspond with the deletion of the directory entry.
2114                 //
2115
2116                 pFcb->ObjectInformation->Links--;
2117
2118                 //
2119                 // Check if this is a directory that there are not currently other opens
2120                 //
2121
2122                 if( pFcb->ObjectInformation->Specific.Directory.ChildOpenHandleCount > 0)
2123                 {
2124
2125                     AFSDbgTrace(( AFS_SUBSYSTEM_FILE_PROCESSING,
2126                                   AFS_TRACE_LEVEL_ERROR,
2127                                   "AFSSetDispositionInfo Attempt to delete directory %wZ with open %u handles\n",
2128                                   &DirectoryCB->NameInformation.FileName,
2129                                   pFcb->ObjectInformation->Specific.Directory.ChildOpenHandleCount));
2130
2131                     try_return( ntStatus = STATUS_DIRECTORY_NOT_EMPTY);
2132                 }
2133
2134                 //
2135                 // Make sure the directory is enumerated before checking to see if it is empty.
2136                 //
2137
2138                 if( !BooleanFlagOn( pFcb->ObjectInformation->Flags, AFS_OBJECT_FLAGS_DIRECTORY_ENUMERATED))
2139                 {
2140
2141                     AFSAcquireExcl( pFcb->ObjectInformation->Specific.Directory.DirectoryNodeHdr.TreeLock,
2142                                     TRUE);
2143
2144                     if( !BooleanFlagOn( pFcb->ObjectInformation->Flags, AFS_OBJECT_FLAGS_DIRECTORY_ENUMERATED))
2145                     {
2146
2147                         AFSDbgTrace(( AFS_SUBSYSTEM_FILE_PROCESSING,
2148                                       AFS_TRACE_LEVEL_VERBOSE,
2149                                       "AFSSetDispositionInfo Enumerating parent FID %08lX-%08lX-%08lX-%08lX\n",
2150                                       pFcb->ObjectInformation->FileId.Cell,
2151                                       pFcb->ObjectInformation->FileId.Volume,
2152                                       pFcb->ObjectInformation->FileId.Vnode,
2153                                       pFcb->ObjectInformation->FileId.Unique));
2154
2155                         ntStatus = AFSEnumerateDirectory( &pCcb->AuthGroup,
2156                                                           pFcb->ObjectInformation,
2157                                                           TRUE);
2158
2159                         if( !NT_SUCCESS( ntStatus))
2160                         {
2161
2162                             AFSReleaseResource( pFcb->ObjectInformation->Specific.Directory.DirectoryNodeHdr.TreeLock);
2163
2164                             AFSDbgTrace(( AFS_SUBSYSTEM_FILE_PROCESSING,
2165                                           AFS_TRACE_LEVEL_ERROR,
2166                                           "AFSSetDispositionInfo Failed to enumerate parent FID %08lX-%08lX-%08lX-%08lX Status %08lX\n",
2167                                           pFcb->ObjectInformation->FileId.Cell,
2168                                           pFcb->ObjectInformation->FileId.Volume,
2169                                           pFcb->ObjectInformation->FileId.Vnode,
2170                                           pFcb->ObjectInformation->FileId.Unique,
2171                                           ntStatus));
2172
2173                             try_return( ntStatus);
2174                         }
2175                     }
2176
2177                     AFSReleaseResource( pFcb->ObjectInformation->Specific.Directory.DirectoryNodeHdr.TreeLock);
2178                 }
2179
2180                 if( !AFSIsDirectoryEmptyForDelete( pFcb))
2181                 {
2182
2183                     AFSDbgTrace(( AFS_SUBSYSTEM_FILE_PROCESSING,
2184                                   AFS_TRACE_LEVEL_ERROR,
2185                                   "AFSSetDispositionInfo Attempt to delete non-empty directory %wZ\n",
2186                                   &DirectoryCB->NameInformation.FileName));
2187
2188                     try_return( ntStatus = STATUS_DIRECTORY_NOT_EMPTY);
2189                 }
2190
2191                 AFSDbgTrace(( AFS_SUBSYSTEM_FILE_PROCESSING,
2192                               AFS_TRACE_LEVEL_VERBOSE,
2193                               "AFSSetDispositionInfo Setting PENDING_DELETE on DirEntry  %p Name %wZ\n",
2194                               DirectoryCB,
2195                               &DirectoryCB->NameInformation.FileName));
2196
2197                 SetFlag( pCcb->DirectoryCB->Flags, AFS_DIR_ENTRY_PENDING_DELETE);
2198             }
2199             else if( pFcb->Header.NodeTypeCode == AFS_FILE_FCB)
2200             {
2201                 BOOLEAN bMmFlushed;
2202
2203                 AFSDbgTrace(( AFS_SUBSYSTEM_LOCK_PROCESSING,
2204                               AFS_TRACE_LEVEL_VERBOSE,
2205                               "AFSSetDispositionInfo Acquiring Fcb SectionObject lock %p EXCL %08lX\n",
2206                               &pFcb->NPFcb->SectionObjectResource,
2207                               PsGetCurrentThread()));
2208
2209                 AFSAcquireExcl( &pFcb->NPFcb->SectionObjectResource,
2210                                 TRUE);
2211
2212                 //
2213                 // Attempt to flush any outstanding data
2214                 //
2215
2216                 bMmFlushed = MmFlushImageSection( &pFcb->NPFcb->SectionObjectPointers,
2217                                                   MmFlushForDelete);
2218
2219                 if ( bMmFlushed)
2220                 {
2221
2222                     //
2223                     // Set PENDING_DELETE before CcPurgeCacheSection to avoid a
2224                     // deadlock with Trend Micro's Enterprise anti-virus product
2225                     // which attempts to open the file which is being deleted.
2226                     //
2227
2228                     AFSDbgTrace(( AFS_SUBSYSTEM_FILE_PROCESSING,
2229                                   AFS_TRACE_LEVEL_VERBOSE,
2230                                   "AFSSetDispositionInfo Setting PENDING_DELETE on DirEntry %p Name %wZ\n",
2231                                   DirectoryCB,
2232                                   &DirectoryCB->NameInformation.FileName));
2233
2234                     SetFlag( pCcb->DirectoryCB->Flags, AFS_DIR_ENTRY_PENDING_DELETE);
2235
2236                     //
2237                     // Purge the cache as well
2238                     //
2239
2240                     if( pFcb->NPFcb->SectionObjectPointers.DataSectionObject != NULL)
2241                     {
2242
2243                         if ( !CcPurgeCacheSection( &pFcb->NPFcb->SectionObjectPointers,
2244                                                    NULL,
2245                                                    0,
2246                                                    TRUE))
2247                         {
2248
2249                             SetFlag( pFcb->Flags, AFS_FCB_FLAG_PURGE_ON_CLOSE);
2250                         }
2251                     }
2252                 }
2253
2254                 AFSDbgTrace(( AFS_SUBSYSTEM_LOCK_PROCESSING,
2255                               AFS_TRACE_LEVEL_VERBOSE,
2256                               "AFSSetDispositionInfo Releasing Fcb SectionObject lock %p EXCL %08lX\n",
2257                               &pFcb->NPFcb->SectionObjectResource,
2258                               PsGetCurrentThread()));
2259
2260                 AFSReleaseResource( &pFcb->NPFcb->SectionObjectResource);
2261
2262                 if ( !bMmFlushed)
2263                 {
2264
2265                     AFSDbgTrace(( AFS_SUBSYSTEM_FILE_PROCESSING,
2266                                   AFS_TRACE_LEVEL_ERROR,
2267                                   "AFSSetDispositionInfo Failed to flush image section for delete Entry %wZ\n",
2268                                   &DirectoryCB->NameInformation.FileName));
2269
2270                     try_return( ntStatus = STATUS_CANNOT_DELETE);
2271                 }
2272             }
2273             else if( pFcb->Header.NodeTypeCode == AFS_SYMBOLIC_LINK_FCB ||
2274                      pFcb->Header.NodeTypeCode == AFS_MOUNT_POINT_FCB ||
2275                      pFcb->Header.NodeTypeCode == AFS_DFS_LINK_FCB ||
2276                      pFcb->Header.NodeTypeCode == AFS_INVALID_FCB)
2277             {
2278
2279                 AFSDbgTrace(( AFS_SUBSYSTEM_FILE_PROCESSING,
2280                               AFS_TRACE_LEVEL_VERBOSE,
2281                               "AFSSetDispositionInfo Setting PENDING_DELETE on DirEntry %p Name %wZ\n",
2282                               DirectoryCB,
2283                               &DirectoryCB->NameInformation.FileName));
2284
2285                 SetFlag( pCcb->DirectoryCB->Flags, AFS_DIR_ENTRY_PENDING_DELETE);
2286             }
2287         }
2288         else
2289         {
2290
2291             ClearFlag( pCcb->DirectoryCB->Flags, AFS_DIR_ENTRY_PENDING_DELETE);
2292         }
2293
2294         //
2295         // OK, should be good to go, set the flag in the file object
2296         //
2297
2298         pIrpSp->FileObject->DeletePending = pBuffer->DeleteFile;
2299
2300 try_exit:
2301
2302         NOTHING;
2303     }
2304
2305     return ntStatus;
2306 }
2307
2308 NTSTATUS
2309 AFSSetFileLinkInfo( IN PIRP Irp)
2310 {
2311
2312     NTSTATUS ntStatus = STATUS_SUCCESS;
2313     AFSDeviceExt *pDeviceExt = (AFSDeviceExt *)AFSRDRDeviceObject->DeviceExtension;
2314     PIO_STACK_LOCATION pIrpSp = IoGetCurrentIrpStackLocation( Irp);
2315     PFILE_LINK_INFORMATION pFileLinkInfo = NULL;
2316     PFILE_OBJECT pSrcFileObj = NULL;
2317     PFILE_OBJECT pTargetFileObj = pIrpSp->Parameters.SetFile.FileObject;
2318     AFSFcb *pSrcFcb = NULL, *pTargetDcb = NULL;
2319     AFSCcb *pSrcCcb = NULL, *pTargetDirCcb = NULL;
2320     AFSObjectInfoCB *pSrcObject = NULL;
2321     AFSObjectInfoCB *pSrcParentObject = NULL, *pTargetParentObject = NULL;
2322     UNICODE_STRING uniSourceName, uniTargetName;
2323     UNICODE_STRING uniFullTargetName, uniTargetParentName;
2324     BOOLEAN bCommonParent = FALSE;
2325     AFSDirectoryCB *pTargetDirEntry = NULL;
2326     AFSDirectoryCB *pNewTargetDirEntry = NULL;
2327     ULONG ulTargetCRC;
2328     BOOLEAN bTargetEntryExists = FALSE;
2329     LONG lCount;
2330     BOOLEAN bReleaseTargetDirLock = FALSE;
2331     ULONG ulNotificationAction = 0, ulNotifyFilter = 0;
2332
2333     __Enter
2334     {
2335
2336         pSrcFileObj = pIrpSp->FileObject;
2337
2338         pSrcFcb = (AFSFcb *)pSrcFileObj->FsContext;
2339         pSrcCcb = (AFSCcb *)pSrcFileObj->FsContext2;
2340
2341         pSrcObject = pSrcFcb->ObjectInformation;
2342
2343         if ( BooleanFlagOn( pSrcFcb->ObjectInformation->Flags, AFS_OBJECT_FLAGS_PARENT_FID))
2344         {
2345
2346             pSrcParentObject = AFSFindObjectInfo( pSrcFcb->ObjectInformation->VolumeCB,
2347                                                   &pSrcFcb->ObjectInformation->ParentFileId,
2348                                                   TRUE);
2349         }
2350
2351         if( pSrcParentObject == NULL)
2352         {
2353
2354             AFSDbgTrace(( AFS_SUBSYSTEM_FILE_PROCESSING,
2355                           AFS_TRACE_LEVEL_ERROR,
2356                           "AFSSetFileLinkInfo Unable to resolve SrcParentObject (INVALID_PARAMETER)\n"));
2357
2358             ASSERT( FALSE);
2359
2360             try_return( ntStatus = STATUS_INVALID_PARAMETER);
2361         }
2362
2363         pFileLinkInfo = (PFILE_LINK_INFORMATION)Irp->AssociatedIrp.SystemBuffer;
2364
2365         //
2366         // Perform some basic checks to ensure FS integrity
2367         //
2368
2369         if( pSrcFcb->Header.NodeTypeCode != AFS_FILE_FCB)
2370         {
2371
2372             AFSDbgTrace(( AFS_SUBSYSTEM_FILE_PROCESSING,
2373                           AFS_TRACE_LEVEL_ERROR,
2374                           "AFSSetFileLinkInfo Attempt to non-file (INVALID_PARAMETER)\n"));
2375
2376             try_return( ntStatus = STATUS_INVALID_PARAMETER);
2377         }
2378
2379         if( pTargetFileObj == NULL)
2380         {
2381
2382             if ( pFileLinkInfo->RootDirectory)
2383             {
2384
2385                 //
2386                 // The target directory is provided by HANDLE
2387                 // RootDirectory is only set when the target directory is not the same
2388                 // as the source directory.
2389                 //
2390                 // AFS only supports hard links within a single directory.
2391                 //
2392                 // The IOManager should translate any Handle to a FileObject for us.
2393                 // However, the failure to receive a FileObject is treated as a fatal
2394                 // error.
2395                 //
2396
2397                 AFSDbgTrace(( AFS_SUBSYSTEM_FILE_PROCESSING,
2398                               AFS_TRACE_LEVEL_ERROR,
2399                               "AFSSetFileLinkInfo Attempt to link %wZ to alternate directory by handle INVALID_PARAMETER\n",
2400                               &pSrcCcb->DirectoryCB->NameInformation.FileName));
2401
2402                 try_return( ntStatus = STATUS_INVALID_PARAMETER);
2403             }
2404             else
2405             {
2406
2407                 uniFullTargetName.Length = (USHORT)pFileLinkInfo->FileNameLength;
2408
2409                 uniFullTargetName.Buffer = (PWSTR)&pFileLinkInfo->FileName;
2410
2411                 AFSRetrieveFinalComponent( &uniFullTargetName,
2412                                            &uniTargetName);
2413
2414                 AFSRetrieveParentPath( &uniFullTargetName,
2415                                        &uniTargetParentName);
2416
2417                 if ( uniTargetParentName.Length == 0)
2418                 {
2419
2420                     //
2421                     // This is a simple rename. Here the target directory is the same as the source parent directory
2422                     // and the name is retrieved from the system buffer information
2423                     //
2424
2425                     pTargetParentObject = pSrcParentObject;
2426                 }
2427                 else
2428                 {
2429                     //
2430                     // uniTargetParentName contains the directory the renamed object
2431                     // will be moved to.  Must obtain the TargetParentObject.
2432                     //
2433
2434                     AFSDbgTrace(( AFS_SUBSYSTEM_FILE_PROCESSING,
2435                                   AFS_TRACE_LEVEL_ERROR,
2436                                   "AFSSetFileLinkInfo Attempt to link  %wZ to alternate directory %wZ (NOT_SAME_DEVICE)\n",
2437                                   &pSrcCcb->DirectoryCB->NameInformation.FileName,
2438                                   &uniFullTargetName));
2439
2440                     try_return( ntStatus = STATUS_NOT_SAME_DEVICE);
2441                 }
2442             }
2443
2444             pTargetDcb = pTargetParentObject->Fcb;
2445         }
2446         else
2447         {
2448
2449             //
2450             // So here we have the target directory taken from the targetfile object
2451             //
2452
2453             pTargetDcb = (AFSFcb *)pTargetFileObj->FsContext;
2454
2455             pTargetDirCcb = (AFSCcb *)pTargetFileObj->FsContext2;
2456
2457             pTargetParentObject = (AFSObjectInfoCB *)pTargetDcb->ObjectInformation;
2458
2459             //
2460             // Grab the target name which we setup in the IRP_MJ_CREATE handler. By how we set this up
2461             // it is only the target component of the rename operation
2462             //
2463
2464             uniTargetName = *((PUNICODE_STRING)&pTargetFileObj->FileName);
2465         }
2466
2467         //
2468         // The quick check to see if they are self linking.
2469         // Do the names match? Only do this where the parent directories are
2470         // the same
2471         //
2472
2473         if( pTargetParentObject == pSrcParentObject)
2474         {
2475
2476             if( FsRtlAreNamesEqual( &uniTargetName,
2477                                     &uniSourceName,
2478                                     FALSE,
2479                                     NULL))
2480             {
2481                 try_return( ntStatus = STATUS_SUCCESS);
2482             }
2483
2484             bCommonParent = TRUE;
2485         }
2486         else
2487         {
2488
2489             //
2490             // We do not allow cross-volume hard links
2491             //
2492
2493             if( pTargetParentObject->VolumeCB != pSrcObject->VolumeCB)
2494             {
2495
2496                 AFSDbgTrace(( AFS_SUBSYSTEM_FILE_PROCESSING,
2497                               AFS_TRACE_LEVEL_ERROR,
2498                               "AFSSetFileLinkInfo Attempt to link to different volume %wZ\n",
2499                               &pSrcCcb->DirectoryCB->NameInformation.FileName));
2500
2501                 try_return( ntStatus = STATUS_NOT_SAME_DEVICE);
2502             }
2503         }
2504
2505         ulTargetCRC = AFSGenerateCRC( &uniTargetName,
2506                                       FALSE);
2507
2508         AFSAcquireExcl( pTargetParentObject->Specific.Directory.DirectoryNodeHdr.TreeLock,
2509                         TRUE);
2510
2511         bReleaseTargetDirLock = TRUE;
2512
2513         AFSLocateCaseSensitiveDirEntry( pTargetParentObject->Specific.Directory.DirectoryNodeHdr.CaseSensitiveTreeHead,
2514                                         ulTargetCRC,
2515                                         &pTargetDirEntry);
2516
2517         if( pTargetDirEntry == NULL)
2518         {
2519
2520             //
2521             // Missed so perform a case insensitive lookup
2522             //
2523
2524             ulTargetCRC = AFSGenerateCRC( &uniTargetName,
2525                                           TRUE);
2526
2527             AFSLocateCaseInsensitiveDirEntry( pTargetParentObject->Specific.Directory.DirectoryNodeHdr.CaseInsensitiveTreeHead,
2528                                               ulTargetCRC,
2529                                               &pTargetDirEntry);
2530         }
2531
2532         if ( !BooleanFlagOn( pDeviceExt->DeviceFlags, AFS_DEVICE_FLAG_DISABLE_SHORTNAMES) &&
2533              pTargetDirEntry == NULL && RtlIsNameLegalDOS8Dot3( &uniTargetName,
2534                                                                 NULL,
2535                                                                 NULL))
2536         {
2537             //
2538             // Try the short name
2539             //
2540             AFSLocateShortNameDirEntry( pTargetParentObject->Specific.Directory.ShortNameTree,
2541                                         ulTargetCRC,
2542                                         &pTargetDirEntry);
2543         }
2544
2545         //
2546         // Increment our ref count on the dir entry
2547         //
2548
2549         if( pTargetDirEntry != NULL)
2550         {
2551
2552             ASSERT( BooleanFlagOn( pTargetDirEntry->ObjectInformation->Flags, AFS_OBJECT_FLAGS_PARENT_FID) &&
2553                     AFSIsEqualFID( &pTargetParentObject->FileId, &pTargetDirEntry->ObjectInformation->ParentFileId));
2554
2555             lCount = InterlockedIncrement( &pTargetDirEntry->DirOpenReferenceCount);
2556
2557             AFSDbgTrace(( AFS_SUBSYSTEM_DIRENTRY_REF_COUNTING,
2558                           AFS_TRACE_LEVEL_VERBOSE,
2559                           "AFSSetFileLinkInfo Increment count on %wZ DE %p Ccb %p Cnt %d\n",
2560                           &pTargetDirEntry->NameInformation.FileName,
2561                           pTargetDirEntry,
2562                           pSrcCcb,
2563                           lCount));
2564
2565             ASSERT( lCount >= 0);
2566
2567             if( !pFileLinkInfo->ReplaceIfExists)
2568             {
2569
2570                 AFSDbgTrace(( AFS_SUBSYSTEM_FILE_PROCESSING,
2571                               AFS_TRACE_LEVEL_ERROR,
2572                               "AFSSetFileLinkInfo Attempt to link with target collision %wZ Target %wZ\n",
2573                               &pSrcCcb->DirectoryCB->NameInformation.FileName,
2574                               &pTargetDirEntry->NameInformation.FileName));
2575
2576                 try_return( ntStatus = STATUS_OBJECT_NAME_COLLISION);
2577             }
2578
2579             AFSDbgTrace(( AFS_SUBSYSTEM_FILE_PROCESSING | AFS_SUBSYSTEM_DIRENTRY_REF_COUNTING,
2580                           AFS_TRACE_LEVEL_ERROR,
2581                           "AFSSetFileLinkInfo Target %wZ exists DE %p Count %d, performing delete of target\n",
2582                           &pTargetDirEntry->NameInformation.FileName,
2583                           pTargetDirEntry,
2584                           lCount));
2585
2586             //
2587             // Pull the directory entry from the parent
2588             //
2589
2590             AFSRemoveDirNodeFromParent( pTargetParentObject,
2591                                         pTargetDirEntry,
2592                                         FALSE);
2593
2594             bTargetEntryExists = TRUE;
2595         }
2596         else
2597         {
2598             AFSDbgTrace(( AFS_SUBSYSTEM_FILE_PROCESSING,
2599                           AFS_TRACE_LEVEL_VERBOSE,
2600                           "AFSSetFileLinkInfo Target does NOT exist, normal linking\n"));
2601         }
2602
2603         //
2604         // OK, this is a simple rename. Issue the rename
2605         // request to the service.
2606         //
2607
2608         ntStatus = AFSNotifyHardLink( pSrcFcb->ObjectInformation,
2609                                       &pSrcCcb->AuthGroup,
2610                                       pSrcParentObject,
2611                                       pTargetDcb->ObjectInformation,
2612                                       pSrcCcb->DirectoryCB,
2613                                       &uniTargetName,
2614                                       pFileLinkInfo->ReplaceIfExists,
2615                                       &pNewTargetDirEntry);
2616
2617         if( ntStatus != STATUS_REPARSE &&
2618             !NT_SUCCESS( ntStatus))
2619         {
2620
2621             AFSDbgTrace(( AFS_SUBSYSTEM_FILE_PROCESSING,
2622                           AFS_TRACE_LEVEL_ERROR,
2623                           "AFSSetFileLinkInfo Failed link of %wZ to target %wZ Status %08lX\n",
2624                           &pSrcCcb->DirectoryCB->NameInformation.FileName,
2625                           &uniTargetName,
2626                           ntStatus));
2627
2628             try_return( ntStatus);
2629         }
2630
2631         if ( ntStatus != STATUS_REPARSE)
2632         {
2633
2634             AFSInsertDirectoryNode( pTargetDcb->ObjectInformation,
2635                                     pNewTargetDirEntry,
2636                                     TRUE);
2637         }
2638
2639         //
2640         // Send notification for the target link file
2641         //
2642
2643         if( bTargetEntryExists || pNewTargetDirEntry)
2644         {
2645
2646             ulNotificationAction = FILE_ACTION_MODIFIED;
2647         }
2648         else
2649         {
2650
2651             ulNotificationAction = FILE_ACTION_ADDED;
2652         }
2653
2654         AFSFsRtlNotifyFullReportChange( pTargetParentObject,
2655                                         pSrcCcb,
2656                                         (ULONG)ulNotifyFilter,
2657                                         (ULONG)ulNotificationAction);
2658
2659       try_exit:
2660
2661         if( !NT_SUCCESS( ntStatus))
2662         {
2663
2664             if( bTargetEntryExists)
2665             {
2666
2667                 AFSInsertDirectoryNode( pTargetParentObject,
2668                                         pTargetDirEntry,
2669                                         FALSE);
2670             }
2671         }
2672
2673         if( pTargetDirEntry != NULL)
2674         {
2675
2676             //
2677             // Release DirOpenReferenceCount obtained above
2678             //
2679
2680             lCount = InterlockedDecrement( &pTargetDirEntry->DirOpenReferenceCount);
2681
2682             AFSDbgTrace(( AFS_SUBSYSTEM_DIRENTRY_REF_COUNTING,
2683                           AFS_TRACE_LEVEL_VERBOSE,
2684                           "AFSSetFileLinkInfo Decrement count on %wZ DE %p Ccb %p Cnt %d\n",
2685                           &pTargetDirEntry->NameInformation.FileName,
2686                           pTargetDirEntry,
2687                           pSrcCcb,
2688                           lCount));
2689
2690             ASSERT( lCount >= 0);
2691         }
2692
2693         if( pNewTargetDirEntry != NULL)
2694         {
2695
2696             //
2697             // Release DirOpenReferenceCount obtained from AFSNotifyHardLink
2698             //
2699
2700             lCount = InterlockedDecrement( &pNewTargetDirEntry->DirOpenReferenceCount);
2701
2702             AFSDbgTrace(( AFS_SUBSYSTEM_DIRENTRY_REF_COUNTING,
2703                           AFS_TRACE_LEVEL_VERBOSE,
2704                           "AFSSetFileLinkInfo Decrement count on %wZ DE %p Ccb %p Cnt %d\n",
2705                           &pNewTargetDirEntry->NameInformation.FileName,
2706                           pNewTargetDirEntry,
2707                           pSrcCcb,
2708                           lCount));
2709
2710             ASSERT( lCount >= 0);
2711         }
2712
2713         if( bReleaseTargetDirLock)
2714         {
2715
2716             AFSReleaseResource( pTargetParentObject->Specific.Directory.DirectoryNodeHdr.TreeLock);
2717         }
2718
2719         if ( pSrcParentObject != NULL)
2720         {
2721
2722             AFSReleaseObjectInfo( &pSrcParentObject);
2723         }
2724
2725         //
2726         // No need to release pTargetParentObject as it is either a copy of pSrcParentObject
2727         // or (AFSFcb *)pTargetFileObj->FsContext->ObjectInformation
2728         //
2729
2730         pTargetParentObject = NULL;
2731     }
2732
2733     return ntStatus;
2734 }
2735
2736 NTSTATUS
2737 AFSSetRenameInfo( IN PIRP Irp)
2738 {
2739
2740     NTSTATUS ntStatus = STATUS_SUCCESS;
2741     AFSDeviceExt *pDeviceExt = (AFSDeviceExt *)AFSRDRDeviceObject->DeviceExtension;
2742     PIO_STACK_LOCATION pIrpSp = IoGetCurrentIrpStackLocation( Irp);
2743     AFSFcb *pSrcFcb = NULL, *pTargetDcb = NULL, *pTargetFcb = NULL;
2744     AFSCcb *pSrcCcb = NULL, *pTargetDirCcb = NULL;
2745     PFILE_OBJECT pSrcFileObj = pIrpSp->FileObject;
2746     PFILE_OBJECT pTargetFileObj = pIrpSp->Parameters.SetFile.FileObject;
2747     PFILE_OBJECT pTargetParentFileObj = NULL;
2748     PFILE_RENAME_INFORMATION pRenameInfo = NULL;
2749     UNICODE_STRING uniTargetName, uniSourceName, uniTargetParentName;
2750     BOOLEAN bReplaceIfExists = FALSE;
2751     UNICODE_STRING uniShortName;
2752     AFSDirectoryCB *pTargetDirEntry = NULL;
2753     ULONG ulTargetCRC = 0;
2754     BOOLEAN bTargetEntryExists = FALSE;
2755     AFSObjectInfoCB *pSrcObject = NULL;
2756     AFSObjectInfoCB *pSrcParentObject = NULL, *pTargetParentObject = NULL;
2757     AFSFileID stNewFid;
2758     ULONG ulNotificationAction = 0, ulNotifyFilter = 0;
2759     UNICODE_STRING uniFullTargetName;
2760     BOOLEAN bCommonParent = FALSE;
2761     BOOLEAN bReleaseTargetDirLock = FALSE;
2762     BOOLEAN bReleaseSourceDirLock = FALSE;
2763     BOOLEAN bDereferenceTargetParentObject = FALSE;
2764     PERESOURCE  pSourceDirLock = NULL;
2765     LONG lCount;
2766
2767     __Enter
2768     {
2769
2770         bReplaceIfExists = pIrpSp->Parameters.SetFile.ReplaceIfExists;
2771
2772         pRenameInfo = (PFILE_RENAME_INFORMATION)Irp->AssociatedIrp.SystemBuffer;
2773
2774         pSrcFcb = (AFSFcb *)pSrcFileObj->FsContext;
2775         pSrcCcb = (AFSCcb *)pSrcFileObj->FsContext2;
2776
2777         pSrcObject = pSrcFcb->ObjectInformation;
2778
2779         if ( BooleanFlagOn( pSrcFcb->ObjectInformation->Flags, AFS_OBJECT_FLAGS_PARENT_FID))
2780         {
2781
2782             pSrcParentObject = AFSFindObjectInfo( pSrcFcb->ObjectInformation->VolumeCB,
2783                                                   &pSrcFcb->ObjectInformation->ParentFileId,
2784                                                   TRUE);
2785         }
2786
2787         if( pSrcParentObject == NULL)
2788         {
2789
2790             AFSDbgTrace(( AFS_SUBSYSTEM_FILE_PROCESSING,
2791                           AFS_TRACE_LEVEL_ERROR,
2792                           "AFSSetRenameInfo Unable to resolve SrcParentObject (INVALID_PARAMETER)\n"));
2793
2794             ASSERT( FALSE);
2795
2796             try_return( ntStatus = STATUS_INVALID_PARAMETER);
2797         }
2798
2799         //
2800         // Perform some basic checks to ensure FS integrity
2801         //
2802
2803         if( pSrcFcb->Header.NodeTypeCode == AFS_ROOT_FCB)
2804         {
2805
2806             //
2807             // Can't rename the root directory
2808             //
2809
2810             AFSDbgTrace(( AFS_SUBSYSTEM_FILE_PROCESSING,
2811                           AFS_TRACE_LEVEL_ERROR,
2812                           "AFSSetRenameInfo Attempt to rename root entry\n"));
2813
2814             try_return( ntStatus = STATUS_INVALID_PARAMETER);
2815         }
2816
2817         if( pSrcFcb->Header.NodeTypeCode == AFS_DIRECTORY_FCB)
2818         {
2819
2820             //
2821             // If there are any open children then fail the rename
2822             //
2823
2824             if( pSrcFcb->ObjectInformation->Specific.Directory.ChildOpenHandleCount > 0)
2825             {
2826
2827                 AFSDbgTrace(( AFS_SUBSYSTEM_FILE_PROCESSING,
2828                               AFS_TRACE_LEVEL_ERROR,
2829                               "AFSSetRenameInfo Attempt to rename directory with open children %wZ\n",
2830                               &pSrcCcb->DirectoryCB->NameInformation.FileName));
2831
2832                 try_return( ntStatus = STATUS_ACCESS_DENIED);
2833             }
2834         }
2835
2836
2837         //
2838         // Extract off the final component name from the Fcb
2839         //
2840
2841         uniSourceName.Length = (USHORT)pSrcCcb->DirectoryCB->NameInformation.FileName.Length;
2842         uniSourceName.MaximumLength = uniSourceName.Length;
2843
2844         uniSourceName.Buffer = pSrcCcb->DirectoryCB->NameInformation.FileName.Buffer;
2845
2846         //
2847         // Resolve the target fileobject
2848         //
2849
2850         if( pTargetFileObj == NULL)
2851         {
2852
2853             if ( pRenameInfo->RootDirectory)
2854             {
2855
2856                 AFSDbgTrace(( AFS_SUBSYSTEM_FILE_PROCESSING,
2857                               AFS_TRACE_LEVEL_ERROR,
2858                               "AFSSetRenameInfo Handle provided but no FileObject ntStatus INVALID_PARAMETER\n"));
2859
2860                 try_return( ntStatus = STATUS_INVALID_PARAMETER);
2861             }
2862             else
2863             {
2864
2865                 uniFullTargetName.Length = (USHORT)pRenameInfo->FileNameLength;
2866
2867                 uniFullTargetName.Buffer = (PWSTR)&pRenameInfo->FileName;
2868
2869                 AFSRetrieveFinalComponent( &uniFullTargetName,
2870                                            &uniTargetName);
2871
2872                 AFSRetrieveParentPath( &uniFullTargetName,
2873                                        &uniTargetParentName);
2874
2875                 if ( uniTargetParentName.Length == 0)
2876                 {
2877
2878                     //
2879                     // This is a simple rename. Here the target directory is the same as the source parent directory
2880                     // and the name is retrieved from the system buffer information
2881                     //
2882
2883                     pTargetParentObject = pSrcParentObject;
2884                 }
2885                 else
2886                 {
2887                     //
2888                     // uniTargetParentName contains the directory the renamed object
2889                     // will be moved to.  Must obtain the TargetParentObject.
2890                     //
2891
2892                     AFSDbgTrace(( AFS_SUBSYSTEM_FILE_PROCESSING,
2893                                   AFS_TRACE_LEVEL_ERROR,
2894                                   "AFSSetRenameInfo Attempt to move %wZ to %wZ -- not yet supported (NOT_SAME_DEVICE)\n",
2895                                   &pSrcCcb->DirectoryCB->NameInformation.FileName,
2896                                   &uniFullTargetName));
2897
2898                     try_return( ntStatus = STATUS_NOT_SAME_DEVICE);
2899                 }
2900             }
2901
2902             pTargetDcb = pTargetParentObject->Fcb;
2903         }
2904         else
2905         {
2906
2907             //
2908             // So here we have the target directory taken from the targetfile object
2909             //
2910
2911             pTargetDcb = (AFSFcb *)pTargetFileObj->FsContext;
2912
2913             pTargetDirCcb = (AFSCcb *)pTargetFileObj->FsContext2;
2914
2915             pTargetParentObject = (AFSObjectInfoCB *)pTargetDcb->ObjectInformation;
2916
2917             //
2918             // Grab the target name which we setup in the IRP_MJ_CREATE handler. By how we set this up
2919             // it is only the target component of the rename operation
2920             //
2921
2922             uniTargetName = *((PUNICODE_STRING)&pTargetFileObj->FileName);
2923         }
2924
2925         //
2926         // The quick check to see if they are not really performing a rename
2927         // Do the names match? Only do this where the parent directories are
2928         // the same
2929         //
2930
2931         if( pTargetParentObject == pSrcParentObject)
2932         {
2933
2934             if( FsRtlAreNamesEqual( &uniTargetName,
2935                                     &uniSourceName,
2936                                     FALSE,
2937                                     NULL))
2938             {
2939                 try_return( ntStatus = STATUS_SUCCESS);
2940             }
2941
2942             bCommonParent = TRUE;
2943         }
2944         else
2945         {
2946
2947             bCommonParent = FALSE;
2948         }
2949
2950         //
2951         // We do not allow cross-volume renames to occur
2952         //
2953
2954         if( pTargetParentObject->VolumeCB != pSrcObject->VolumeCB)
2955         {
2956
2957             AFSDbgTrace(( AFS_SUBSYSTEM_FILE_PROCESSING,
2958                           AFS_TRACE_LEVEL_ERROR,
2959                           "AFSSetRenameInfo Attempt to rename directory to different volume %wZ\n",
2960                           &pSrcCcb->DirectoryCB->NameInformation.FileName));
2961
2962             try_return( ntStatus = STATUS_NOT_SAME_DEVICE);
2963         }
2964
2965         ulTargetCRC = AFSGenerateCRC( &uniTargetName,
2966                                       FALSE);
2967
2968         AFSAcquireExcl( pTargetParentObject->Specific.Directory.DirectoryNodeHdr.TreeLock,
2969                         TRUE);
2970
2971         bReleaseTargetDirLock = TRUE;
2972
2973         if( pTargetParentObject != pSrcParentObject)
2974         {
2975             AFSAcquireExcl( pSrcParentObject->Specific.Directory.DirectoryNodeHdr.TreeLock,
2976                             TRUE);
2977
2978             bReleaseSourceDirLock = TRUE;
2979
2980             pSourceDirLock = pSrcParentObject->Specific.Directory.DirectoryNodeHdr.TreeLock;
2981         }
2982
2983         AFSLocateCaseSensitiveDirEntry( pTargetParentObject->Specific.Directory.DirectoryNodeHdr.CaseSensitiveTreeHead,
2984                                         ulTargetCRC,
2985                                         &pTargetDirEntry);
2986
2987         if( pTargetDirEntry == NULL)
2988         {
2989
2990             //
2991             // Missed so perform a case insensitive lookup
2992             //
2993
2994             ulTargetCRC = AFSGenerateCRC( &uniTargetName,
2995                                           TRUE);
2996
2997             AFSLocateCaseInsensitiveDirEntry( pTargetParentObject->Specific.Directory.DirectoryNodeHdr.CaseInsensitiveTreeHead,
2998                                               ulTargetCRC,
2999                                               &pTargetDirEntry);
3000         }
3001
3002         if ( !BooleanFlagOn( pDeviceExt->DeviceFlags, AFS_DEVICE_FLAG_DISABLE_SHORTNAMES) &&
3003              pTargetDirEntry == NULL && RtlIsNameLegalDOS8Dot3( &uniTargetName,
3004                                                                NULL,
3005                                                                NULL))
3006         {
3007             //
3008             // Try the short name
3009             //
3010             AFSLocateShortNameDirEntry( pTargetParentObject->Specific.Directory.ShortNameTree,
3011                                         ulTargetCRC,
3012                                         &pTargetDirEntry);
3013         }
3014
3015         //
3016         // Increment our ref count on the dir entry
3017         //
3018
3019         if( pTargetDirEntry != NULL)
3020         {
3021
3022             ASSERT( BooleanFlagOn( pTargetDirEntry->ObjectInformation->Flags, AFS_OBJECT_FLAGS_PARENT_FID) &&
3023                     AFSIsEqualFID( &pTargetParentObject->FileId, &pTargetDirEntry->ObjectInformation->ParentFileId));
3024
3025             lCount = InterlockedIncrement( &pTargetDirEntry->DirOpenReferenceCount);
3026
3027             AFSDbgTrace(( AFS_SUBSYSTEM_DIRENTRY_REF_COUNTING,
3028                           AFS_TRACE_LEVEL_VERBOSE,
3029                           "AFSSetRenameInfo Increment count on %wZ DE %p Ccb %p Cnt %d\n",
3030                           &pTargetDirEntry->NameInformation.FileName,
3031                           pTargetDirEntry,
3032                           pSrcCcb,
3033                           lCount));
3034
3035             ASSERT( lCount >= 0);
3036
3037             if( !bReplaceIfExists)
3038             {
3039
3040                 AFSDbgTrace(( AFS_SUBSYSTEM_FILE_PROCESSING,
3041                               AFS_TRACE_LEVEL_ERROR,
3042                               "AFSSetRenameInfo Attempt to rename directory with target collision %wZ Target %wZ\n",
3043                               &pSrcCcb->DirectoryCB->NameInformation.FileName,
3044                               &pTargetDirEntry->NameInformation.FileName));
3045
3046                 try_return( ntStatus = STATUS_OBJECT_NAME_COLLISION);
3047             }
3048
3049             AFSDbgTrace(( AFS_SUBSYSTEM_FILE_PROCESSING | AFS_SUBSYSTEM_DIRENTRY_REF_COUNTING,
3050                           AFS_TRACE_LEVEL_ERROR,
3051                           "AFSSetRenameInfo Target %wZ exists DE %p Count %d, performing delete of target\n",
3052                           &pTargetDirEntry->NameInformation.FileName,
3053                           pTargetDirEntry,
3054                           lCount));
3055
3056             //
3057             // Pull the directory entry from the parent
3058             //
3059
3060             AFSRemoveDirNodeFromParent( pTargetParentObject,
3061                                         pTargetDirEntry,
3062                                         FALSE);
3063
3064             bTargetEntryExists = TRUE;
3065         }
3066         else
3067         {
3068             AFSDbgTrace(( AFS_SUBSYSTEM_FILE_PROCESSING,
3069                           AFS_TRACE_LEVEL_VERBOSE,
3070                           "AFSSetRenameInfo Target does NOT exist, normal rename\n"));
3071         }
3072
3073         //
3074         // We need to remove the DirEntry from the parent node, update the index
3075         // and reinsert it into the parent tree. Note that for entries with the
3076         // same parent we do not pull the node from the enumeration list
3077         //
3078
3079         AFSRemoveDirNodeFromParent( pSrcParentObject,
3080                                     pSrcCcb->DirectoryCB,
3081                                     !bCommonParent);
3082
3083         //
3084         // OK, this is a simple rename. Issue the rename
3085         // request to the service.
3086         //
3087
3088         ntStatus = AFSNotifyRename( pSrcFcb->ObjectInformation,
3089                                     &pSrcCcb->AuthGroup,
3090                                     pSrcParentObject,
3091                                     pTargetDcb->ObjectInformation,
3092                                     pSrcCcb->DirectoryCB,
3093                                     &uniTargetName,
3094                                     &stNewFid);
3095
3096         if( !NT_SUCCESS( ntStatus))
3097         {
3098
3099             //
3100             // Attempt to re-insert the directory entry
3101             //
3102
3103             AFSInsertDirectoryNode( pSrcParentObject,
3104                                     pSrcCcb->DirectoryCB,
3105                                     !bCommonParent);
3106
3107             AFSDbgTrace(( AFS_SUBSYSTEM_FILE_PROCESSING,
3108                           AFS_TRACE_LEVEL_ERROR,
3109                           "AFSSetRenameInfo Failed rename of %wZ to target %wZ Status %08lX\n",
3110                           &pSrcCcb->DirectoryCB->NameInformation.FileName,
3111                           &uniTargetName,
3112                           ntStatus));
3113
3114             try_return( ntStatus);
3115         }
3116
3117         //
3118         // Set the notification up for the source file
3119         //
3120
3121         if( pSrcParentObject == pTargetParentObject &&
3122             !bTargetEntryExists)
3123         {
3124
3125             ulNotificationAction = FILE_ACTION_RENAMED_OLD_NAME;
3126         }
3127         else
3128         {
3129
3130             ulNotificationAction = FILE_ACTION_REMOVED;
3131         }
3132
3133         if( pSrcFcb->ObjectInformation->FileType == AFS_FILE_TYPE_DIRECTORY)
3134         {
3135
3136             ulNotifyFilter = FILE_NOTIFY_CHANGE_DIR_NAME;
3137         }
3138         else
3139         {
3140
3141             ulNotifyFilter = FILE_NOTIFY_CHANGE_FILE_NAME;
3142         }
3143
3144         AFSFsRtlNotifyFullReportChange( pSrcParentObject,
3145                                         pSrcCcb,
3146                                         (ULONG)ulNotifyFilter,
3147                                         (ULONG)ulNotificationAction);
3148
3149         //
3150         // Update the name in the dir entry.
3151         //
3152
3153         ntStatus = AFSUpdateDirEntryName( pSrcCcb->DirectoryCB,
3154                                           &uniTargetName);
3155
3156         if( !NT_SUCCESS( ntStatus))
3157         {
3158
3159             //
3160             // Attempt to re-insert the directory entry
3161             //
3162
3163             AFSInsertDirectoryNode( pSrcParentObject,
3164                                     pSrcCcb->DirectoryCB,
3165                                     !bCommonParent);
3166
3167             AFSDbgTrace(( AFS_SUBSYSTEM_FILE_PROCESSING,
3168                           AFS_TRACE_LEVEL_ERROR,
3169                           "AFSSetRenameInfo Failed update of dir entry %wZ to target %wZ Status %08lX\n",
3170                           &pSrcCcb->DirectoryCB->NameInformation.FileName,
3171                           &uniTargetName,
3172                           ntStatus));
3173
3174             try_return( ntStatus);
3175         }
3176
3177         //
3178         // Update the object information block, if needed
3179         //
3180
3181         if( !AFSIsEqualFID( &pSrcObject->FileId,
3182                             &stNewFid))
3183         {
3184
3185             AFSAcquireExcl( pSrcObject->VolumeCB->ObjectInfoTree.TreeLock,
3186                             TRUE);
3187
3188             //
3189             // Remove the old information entry
3190             //
3191
3192             AFSRemoveHashEntry( &pSrcObject->VolumeCB->ObjectInfoTree.TreeHead,
3193                                 &pSrcObject->TreeEntry);
3194
3195             RtlCopyMemory( &pSrcObject->FileId,
3196                            &stNewFid,
3197                            sizeof( AFSFileID));
3198
3199             //
3200             // Insert the entry into the new object table.
3201             //
3202
3203             pSrcObject->TreeEntry.HashIndex = AFSCreateLowIndex( &pSrcObject->FileId);
3204
3205             if( pSrcObject->VolumeCB->ObjectInfoTree.TreeHead == NULL)
3206             {
3207
3208                 pSrcObject->VolumeCB->ObjectInfoTree.TreeHead = &pSrcObject->TreeEntry;
3209             }
3210             else
3211             {
3212
3213                 if ( !NT_SUCCESS( AFSInsertHashEntry( pSrcObject->VolumeCB->ObjectInfoTree.TreeHead,
3214                                                      &pSrcObject->TreeEntry)))
3215                 {
3216
3217                     //
3218                     // Lost a race, an ObjectInfo object already exists for this FID.
3219                     // Let this copy be garbage collected.
3220                     //
3221
3222                     ClearFlag( pSrcObject->Flags, AFS_