Windows: File Attribute Reporting Consistency
[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             AFSDbgLogMsg( 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         AFSDbgLogMsg( 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             AFSDbgLogMsg( 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             AFSDbgLogMsg( 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             AFSDbgLogMsg( 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                 AFSDbgLogMsg( 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                               pCcb->DirectoryCB->ObjectInformation->FileId.Cell,
462                               pCcb->DirectoryCB->ObjectInformation->FileId.Volume,
463                               pCcb->DirectoryCB->ObjectInformation->FileId.Vnode,
464                               pCcb->DirectoryCB->ObjectInformation->FileId.Unique,
465                               ntStatus);
466             }
467         }
468     }
469     __except( AFSExceptionFilter( __FUNCTION__, GetExceptionCode(), GetExceptionInformation()) )
470     {
471
472         AFSDbgLogMsg( 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             AFSDbgLogMsg( 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         AFSDbgLogMsg( 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             AFSDbgLogMsg( 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             AFSDbgLogMsg( 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.Characteristics, FILE_READ_ONLY_DEVICE))
587         {
588
589             AFSDbgLogMsg( 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             AFSDbgLogMsg( 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                 AFSDbgLogMsg( 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                               pCcb->DirectoryCB->ObjectInformation->FileId.Cell,
741                               pCcb->DirectoryCB->ObjectInformation->FileId.Volume,
742                               pCcb->DirectoryCB->ObjectInformation->FileId.Vnode,
743                               pCcb->DirectoryCB->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                 AFSDbgLogMsg( 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                               pCcb->DirectoryCB->ObjectInformation->FileId.Cell,
762                               pCcb->DirectoryCB->ObjectInformation->FileId.Volume,
763                               pCcb->DirectoryCB->ObjectInformation->FileId.Vnode,
764                               pCcb->DirectoryCB->ObjectInformation->FileId.Unique,
765                               ntStatus);
766             }
767         }
768     }
769     __except( AFSExceptionFilter( __FUNCTION__, GetExceptionCode(), GetExceptionInformation()) )
770     {
771
772         AFSDbgLogMsg( 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         AFSDbgLogMsg( 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                       pCcb->DirectoryCB->ObjectInformation->FileType,
890                       pCcb->DirectoryCB->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         AFSDbgLogMsg( 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                       pCcb->DirectoryCB->ObjectInformation->FileType,
1015                       pCcb->DirectoryCB->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         AFSDbgLogMsg( 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                       pCcb->DirectoryCB->ObjectInformation->FileType,
1531                       pCcb->DirectoryCB->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         AFSDbgLogMsg( 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                       pCcb->DirectoryCB->ObjectInformation->FileType,
1726                       pCcb->DirectoryCB->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
2016                 if ( pParentObjectInfo != NULL)
2017                 {
2018                     AFSFsRtlNotifyFullReportChange( pParentObjectInfo,
2019                                                     pCcb,
2020                                                     (ULONG)ulNotifyFilter,
2021                                                     (ULONG)FILE_ACTION_MODIFIED);
2022
2023                     AFSReleaseObjectInfo( &pParentObjectInfo);
2024                 }
2025             }
2026         }
2027
2028 try_exit:
2029
2030         NOTHING;
2031     }
2032
2033     return ntStatus;
2034 }
2035
2036 NTSTATUS
2037 AFSSetDispositionInfo( IN PIRP Irp,
2038                        IN AFSDirectoryCB *DirectoryCB)
2039 {
2040     NTSTATUS ntStatus = STATUS_SUCCESS;
2041     PFILE_DISPOSITION_INFORMATION pBuffer;
2042     PIO_STACK_LOCATION pIrpSp = IoGetCurrentIrpStackLocation( Irp);
2043     AFSFcb *pFcb = NULL;
2044     AFSCcb *pCcb = NULL;
2045
2046     __Enter
2047     {
2048
2049         pBuffer = (PFILE_DISPOSITION_INFORMATION)Irp->AssociatedIrp.SystemBuffer;
2050
2051         pFcb = (AFSFcb *)pIrpSp->FileObject->FsContext;
2052
2053         pCcb = (AFSCcb *)pIrpSp->FileObject->FsContext2;
2054
2055         //
2056         // Can't delete the root
2057         //
2058
2059         if( pFcb->Header.NodeTypeCode == AFS_ROOT_FCB)
2060         {
2061
2062             AFSDbgLogMsg( AFS_SUBSYSTEM_FILE_PROCESSING,
2063                           AFS_TRACE_LEVEL_ERROR,
2064                           "AFSSetDispositionInfo Attempt to delete root entry\n");
2065
2066             try_return( ntStatus = STATUS_CANNOT_DELETE);
2067         }
2068
2069         //
2070         // If the file is read only then do not allow the delete
2071         //
2072
2073         if( BooleanFlagOn( DirectoryCB->ObjectInformation->FileAttributes, FILE_ATTRIBUTE_READONLY))
2074         {
2075
2076             AFSDbgLogMsg( AFS_SUBSYSTEM_FILE_PROCESSING,
2077                           AFS_TRACE_LEVEL_ERROR,
2078                           "AFSSetDispositionInfo Attempt to delete read only entry %wZ\n",
2079                           &DirectoryCB->NameInformation.FileName);
2080
2081             try_return( ntStatus = STATUS_CANNOT_DELETE);
2082         }
2083
2084         if( pBuffer->DeleteFile)
2085         {
2086
2087             //
2088             // Check if the caller can delete the file
2089             //
2090
2091             ntStatus = AFSNotifyDelete( DirectoryCB,
2092                                         &pCcb->AuthGroup,
2093                                         TRUE);
2094
2095             if( !NT_SUCCESS( ntStatus))
2096             {
2097
2098                 AFSDbgLogMsg( AFS_SUBSYSTEM_FILE_PROCESSING,
2099                               AFS_TRACE_LEVEL_ERROR,
2100                               "AFSSetDispositionInfo Cannot delete entry %wZ Status %08lX\n",
2101                               &DirectoryCB->NameInformation.FileName,
2102                               ntStatus);
2103
2104                 try_return( ntStatus);
2105             }
2106
2107             if( pFcb->Header.NodeTypeCode == AFS_DIRECTORY_FCB)
2108             {
2109
2110                 //
2111                 // Reduce the Link count in the object information block
2112                 // to correspond with the deletion of the directory entry.
2113                 //
2114
2115                 pFcb->ObjectInformation->Links--;
2116
2117                 //
2118                 // Check if this is a directory that there are not currently other opens
2119                 //
2120
2121                 if( pFcb->ObjectInformation->Specific.Directory.ChildOpenHandleCount > 0)
2122                 {
2123
2124                     AFSDbgLogMsg( AFS_SUBSYSTEM_FILE_PROCESSING,
2125                                   AFS_TRACE_LEVEL_ERROR,
2126                                   "AFSSetDispositionInfo Attempt to delete directory %wZ with open %u handles\n",
2127                                   &DirectoryCB->NameInformation.FileName,
2128                                   pFcb->ObjectInformation->Specific.Directory.ChildOpenHandleCount);
2129
2130                     try_return( ntStatus = STATUS_DIRECTORY_NOT_EMPTY);
2131                 }
2132
2133                 if( !AFSIsDirectoryEmptyForDelete( pFcb))
2134                 {
2135
2136                     AFSDbgLogMsg( AFS_SUBSYSTEM_FILE_PROCESSING,
2137                                   AFS_TRACE_LEVEL_ERROR,
2138                                   "AFSSetDispositionInfo Attempt to delete non-empty directory %wZ\n",
2139                                   &DirectoryCB->NameInformation.FileName);
2140
2141                     try_return( ntStatus = STATUS_DIRECTORY_NOT_EMPTY);
2142                 }
2143
2144                 AFSDbgLogMsg( AFS_SUBSYSTEM_FILE_PROCESSING,
2145                               AFS_TRACE_LEVEL_VERBOSE,
2146                               "AFSSetDispositionInfo Setting PENDING_DELETE on DirEntry  %p Name %wZ\n",
2147                               DirectoryCB,
2148                               &DirectoryCB->NameInformation.FileName);
2149
2150                 SetFlag( pCcb->DirectoryCB->Flags, AFS_DIR_ENTRY_PENDING_DELETE);
2151             }
2152             else if( pFcb->Header.NodeTypeCode == AFS_FILE_FCB)
2153             {
2154                 BOOLEAN bMmFlushed;
2155
2156                 AFSDbgLogMsg( AFS_SUBSYSTEM_LOCK_PROCESSING,
2157                               AFS_TRACE_LEVEL_VERBOSE,
2158                               "AFSSetDispositionInfo Acquiring Fcb SectionObject lock %p EXCL %08lX\n",
2159                               &pFcb->NPFcb->SectionObjectResource,
2160                               PsGetCurrentThread());
2161
2162                 AFSAcquireExcl( &pFcb->NPFcb->SectionObjectResource,
2163                                 TRUE);
2164
2165                 //
2166                 // Attempt to flush any outstanding data
2167                 //
2168
2169                 bMmFlushed = MmFlushImageSection( &pFcb->NPFcb->SectionObjectPointers,
2170                                                   MmFlushForDelete);
2171
2172                 if ( bMmFlushed)
2173                 {
2174
2175                     //
2176                     // Set PENDING_DELETE before CcPurgeCacheSection to avoid a
2177                     // deadlock with Trend Micro's Enterprise anti-virus product
2178                     // which attempts to open the file which is being deleted.
2179                     //
2180
2181                     AFSDbgLogMsg( AFS_SUBSYSTEM_FILE_PROCESSING,
2182                                   AFS_TRACE_LEVEL_VERBOSE,
2183                                   "AFSSetDispositionInfo Setting PENDING_DELETE on DirEntry %p Name %wZ\n",
2184                                   DirectoryCB,
2185                                   &DirectoryCB->NameInformation.FileName);
2186
2187                     SetFlag( pCcb->DirectoryCB->Flags, AFS_DIR_ENTRY_PENDING_DELETE);
2188
2189                     //
2190                     // Purge the cache as well
2191                     //
2192
2193                     if( pFcb->NPFcb->SectionObjectPointers.DataSectionObject != NULL)
2194                     {
2195
2196                         if ( !CcPurgeCacheSection( &pFcb->NPFcb->SectionObjectPointers,
2197                                                    NULL,
2198                                                    0,
2199                                                    TRUE))
2200                         {
2201
2202                             SetFlag( pFcb->Flags, AFS_FCB_FLAG_PURGE_ON_CLOSE);
2203                         }
2204                     }
2205                 }
2206
2207                 AFSDbgLogMsg( AFS_SUBSYSTEM_LOCK_PROCESSING,
2208                               AFS_TRACE_LEVEL_VERBOSE,
2209                               "AFSSetDispositionInfo Releasing Fcb SectionObject lock %p EXCL %08lX\n",
2210                               &pFcb->NPFcb->SectionObjectResource,
2211                               PsGetCurrentThread());
2212
2213                 AFSReleaseResource( &pFcb->NPFcb->SectionObjectResource);
2214
2215                 if ( !bMmFlushed)
2216                 {
2217
2218                     AFSDbgLogMsg( AFS_SUBSYSTEM_FILE_PROCESSING,
2219                                   AFS_TRACE_LEVEL_ERROR,
2220                                   "AFSSetDispositionInfo Failed to flush image section for delete Entry %wZ\n",
2221                                   &DirectoryCB->NameInformation.FileName);
2222
2223                     try_return( ntStatus = STATUS_CANNOT_DELETE);
2224                 }
2225             }
2226             else if( pFcb->Header.NodeTypeCode == AFS_SYMBOLIC_LINK_FCB ||
2227                      pFcb->Header.NodeTypeCode == AFS_MOUNT_POINT_FCB ||
2228                      pFcb->Header.NodeTypeCode == AFS_DFS_LINK_FCB ||
2229                      pFcb->Header.NodeTypeCode == AFS_INVALID_FCB)
2230             {
2231
2232                 AFSDbgLogMsg( AFS_SUBSYSTEM_FILE_PROCESSING,
2233                               AFS_TRACE_LEVEL_VERBOSE,
2234                               "AFSSetDispositionInfo Setting PENDING_DELETE on DirEntry %p Name %wZ\n",
2235                               DirectoryCB,
2236                               &DirectoryCB->NameInformation.FileName);
2237
2238                 SetFlag( pCcb->DirectoryCB->Flags, AFS_DIR_ENTRY_PENDING_DELETE);
2239             }
2240         }
2241         else
2242         {
2243
2244             ClearFlag( pCcb->DirectoryCB->Flags, AFS_DIR_ENTRY_PENDING_DELETE);
2245         }
2246
2247         //
2248         // OK, should be good to go, set the flag in the file object
2249         //
2250
2251         pIrpSp->FileObject->DeletePending = pBuffer->DeleteFile;
2252
2253 try_exit:
2254
2255         NOTHING;
2256     }
2257
2258     return ntStatus;
2259 }
2260
2261 NTSTATUS
2262 AFSSetFileLinkInfo( IN PIRP Irp)
2263 {
2264
2265     NTSTATUS ntStatus = STATUS_SUCCESS;
2266     AFSDeviceExt *pDeviceExt = (AFSDeviceExt *)AFSRDRDeviceObject->DeviceExtension;
2267     PIO_STACK_LOCATION pIrpSp = IoGetCurrentIrpStackLocation( Irp);
2268     PFILE_LINK_INFORMATION pFileLinkInfo = NULL;
2269     PFILE_OBJECT pSrcFileObj = NULL;
2270     PFILE_OBJECT pTargetFileObj = pIrpSp->Parameters.SetFile.FileObject;
2271     AFSFcb *pSrcFcb = NULL, *pTargetDcb = NULL;
2272     AFSCcb *pSrcCcb = NULL, *pTargetDirCcb = NULL;
2273     AFSObjectInfoCB *pSrcObject = NULL;
2274     AFSObjectInfoCB *pSrcParentObject = NULL, *pTargetParentObject = NULL;
2275     UNICODE_STRING uniSourceName, uniTargetName;
2276     UNICODE_STRING uniFullTargetName, uniTargetParentName;
2277     BOOLEAN bCommonParent = FALSE;
2278     AFSDirectoryCB *pTargetDirEntry = NULL;
2279     AFSDirectoryCB *pNewTargetDirEntry = NULL;
2280     ULONG ulTargetCRC;
2281     BOOLEAN bTargetEntryExists = FALSE;
2282     LONG lCount;
2283     BOOLEAN bReleaseTargetDirLock = FALSE;
2284     ULONG ulNotificationAction = 0, ulNotifyFilter = 0;
2285
2286     __Enter
2287     {
2288
2289         pSrcFileObj = pIrpSp->FileObject;
2290
2291         pSrcFcb = (AFSFcb *)pSrcFileObj->FsContext;
2292         pSrcCcb = (AFSCcb *)pSrcFileObj->FsContext2;
2293
2294         pSrcObject = pSrcFcb->ObjectInformation;
2295
2296         if ( BooleanFlagOn( pSrcFcb->ObjectInformation->Flags, AFS_OBJECT_FLAGS_PARENT_FID))
2297         {
2298
2299             pSrcParentObject = AFSFindObjectInfo( pSrcFcb->ObjectInformation->VolumeCB,
2300                                                   &pSrcFcb->ObjectInformation->ParentFileId);
2301         }
2302
2303         pFileLinkInfo = (PFILE_LINK_INFORMATION)Irp->AssociatedIrp.SystemBuffer;
2304
2305         //
2306         // Perform some basic checks to ensure FS integrity
2307         //
2308
2309         if( pSrcFcb->Header.NodeTypeCode != AFS_FILE_FCB)
2310         {
2311
2312             AFSDbgLogMsg( AFS_SUBSYSTEM_FILE_PROCESSING,
2313                           AFS_TRACE_LEVEL_ERROR,
2314                           "AFSSetFileLinkInfo Attempt to non-file (INVALID_PARAMETER)\n");
2315
2316             try_return( ntStatus = STATUS_INVALID_PARAMETER);
2317         }
2318
2319         if( pTargetFileObj == NULL)
2320         {
2321
2322             if ( pFileLinkInfo->RootDirectory)
2323             {
2324
2325                 //
2326                 // The target directory is provided by HANDLE
2327                 // RootDirectory is only set when the target directory is not the same
2328                 // as the source directory.
2329                 //
2330                 // AFS only supports hard links within a single directory.
2331                 //
2332                 // The IOManager should translate any Handle to a FileObject for us.
2333                 // However, the failure to receive a FileObject is treated as a fatal
2334                 // error.
2335                 //
2336
2337                 AFSDbgLogMsg( AFS_SUBSYSTEM_FILE_PROCESSING,
2338                               AFS_TRACE_LEVEL_ERROR,
2339                               "AFSSetFileLinkInfo Attempt to link %wZ to alternate directory by handle INVALID_PARAMETER\n",
2340                               &pSrcCcb->DirectoryCB->NameInformation.FileName);
2341
2342                 try_return( ntStatus = STATUS_INVALID_PARAMETER);
2343             }
2344             else
2345             {
2346
2347                 uniFullTargetName.Length = (USHORT)pFileLinkInfo->FileNameLength;
2348
2349                 uniFullTargetName.Buffer = (PWSTR)&pFileLinkInfo->FileName;
2350
2351                 AFSRetrieveFinalComponent( &uniFullTargetName,
2352                                            &uniTargetName);
2353
2354                 AFSRetrieveParentPath( &uniFullTargetName,
2355                                        &uniTargetParentName);
2356
2357                 if ( uniTargetParentName.Length == 0)
2358                 {
2359
2360                     //
2361                     // This is a simple rename. Here the target directory is the same as the source parent directory
2362                     // and the name is retrieved from the system buffer information
2363                     //
2364
2365                     pTargetParentObject = pSrcParentObject;
2366                 }
2367                 else
2368                 {
2369                     //
2370                     // uniTargetParentName contains the directory the renamed object
2371                     // will be moved to.  Must obtain the TargetParentObject.
2372                     //
2373
2374                     AFSDbgLogMsg( AFS_SUBSYSTEM_FILE_PROCESSING,
2375                                   AFS_TRACE_LEVEL_ERROR,
2376                                   "AFSSetFileLinkInfo Attempt to link  %wZ to alternate directory %wZ (NOT_SAME_DEVICE)\n",
2377                                   &pSrcCcb->DirectoryCB->NameInformation.FileName,
2378                                   &uniFullTargetName);
2379
2380                     try_return( ntStatus = STATUS_NOT_SAME_DEVICE);
2381                 }
2382             }
2383
2384             pTargetDcb = pTargetParentObject->Fcb;
2385         }
2386         else
2387         {
2388
2389             //
2390             // So here we have the target directory taken from the targetfile object
2391             //
2392
2393             pTargetDcb = (AFSFcb *)pTargetFileObj->FsContext;
2394
2395             pTargetDirCcb = (AFSCcb *)pTargetFileObj->FsContext2;
2396
2397             pTargetParentObject = (AFSObjectInfoCB *)pTargetDcb->ObjectInformation;
2398
2399             //
2400             // Grab the target name which we setup in the IRP_MJ_CREATE handler. By how we set this up
2401             // it is only the target component of the rename operation
2402             //
2403
2404             uniTargetName = *((PUNICODE_STRING)&pTargetFileObj->FileName);
2405         }
2406
2407         //
2408         // The quick check to see if they are self linking.
2409         // Do the names match? Only do this where the parent directories are
2410         // the same
2411         //
2412
2413         if( pTargetParentObject == pSrcParentObject)
2414         {
2415
2416             if( FsRtlAreNamesEqual( &uniTargetName,
2417                                     &uniSourceName,
2418                                     FALSE,
2419                                     NULL))
2420             {
2421                 try_return( ntStatus = STATUS_SUCCESS);
2422             }
2423
2424             bCommonParent = TRUE;
2425         }
2426         else
2427         {
2428
2429             //
2430             // We do not allow cross-volume hard links
2431             //
2432
2433             if( pTargetParentObject->VolumeCB != pSrcObject->VolumeCB)
2434             {
2435
2436                 AFSDbgLogMsg( AFS_SUBSYSTEM_FILE_PROCESSING,
2437                               AFS_TRACE_LEVEL_ERROR,
2438                               "AFSSetFileLinkInfo Attempt to link to different volume %wZ\n",
2439                               &pSrcCcb->DirectoryCB->NameInformation.FileName);
2440
2441                 try_return( ntStatus = STATUS_NOT_SAME_DEVICE);
2442             }
2443         }
2444
2445         ulTargetCRC = AFSGenerateCRC( &uniTargetName,
2446                                       FALSE);
2447
2448         AFSAcquireExcl( pTargetParentObject->Specific.Directory.DirectoryNodeHdr.TreeLock,
2449                         TRUE);
2450
2451         bReleaseTargetDirLock = TRUE;
2452
2453         AFSLocateCaseSensitiveDirEntry( pTargetParentObject->Specific.Directory.DirectoryNodeHdr.CaseSensitiveTreeHead,
2454                                         ulTargetCRC,
2455                                         &pTargetDirEntry);
2456
2457         if( pTargetDirEntry == NULL)
2458         {
2459
2460             //
2461             // Missed so perform a case insensitive lookup
2462             //
2463
2464             ulTargetCRC = AFSGenerateCRC( &uniTargetName,
2465                                           TRUE);
2466
2467             AFSLocateCaseInsensitiveDirEntry( pTargetParentObject->Specific.Directory.DirectoryNodeHdr.CaseInsensitiveTreeHead,
2468                                               ulTargetCRC,
2469                                               &pTargetDirEntry);
2470         }
2471
2472         if ( !BooleanFlagOn( pDeviceExt->DeviceFlags, AFS_DEVICE_FLAG_DISABLE_SHORTNAMES) &&
2473              pTargetDirEntry == NULL && RtlIsNameLegalDOS8Dot3( &uniTargetName,
2474                                                                 NULL,
2475                                                                 NULL))
2476         {
2477             //
2478             // Try the short name
2479             //
2480             AFSLocateShortNameDirEntry( pTargetParentObject->Specific.Directory.ShortNameTree,
2481                                         ulTargetCRC,
2482                                         &pTargetDirEntry);
2483         }
2484
2485         //
2486         // Increment our ref count on the dir entry
2487         //
2488
2489         if( pTargetDirEntry != NULL)
2490         {
2491
2492             ASSERT( BooleanFlagOn( pTargetDirEntry->ObjectInformation->Flags, AFS_OBJECT_FLAGS_PARENT_FID) &&
2493                     AFSIsEqualFID( &pTargetParentObject->FileId, &pTargetDirEntry->ObjectInformation->ParentFileId));
2494
2495             lCount = InterlockedIncrement( &pTargetDirEntry->DirOpenReferenceCount);
2496
2497             AFSDbgLogMsg( AFS_SUBSYSTEM_DIRENTRY_REF_COUNTING,
2498                           AFS_TRACE_LEVEL_VERBOSE,
2499                           "AFSSetFileLinkInfo Increment count on %wZ DE %p Ccb %p Cnt %d\n",
2500                           &pTargetDirEntry->NameInformation.FileName,
2501                           pTargetDirEntry,
2502                           pSrcCcb,
2503                           lCount);
2504
2505             ASSERT( lCount >= 0);
2506
2507             if( !pFileLinkInfo->ReplaceIfExists)
2508             {
2509
2510                 AFSDbgLogMsg( AFS_SUBSYSTEM_FILE_PROCESSING,
2511                               AFS_TRACE_LEVEL_ERROR,
2512                               "AFSSetFileLinkInfo Attempt to link with target collision %wZ Target %wZ\n",
2513                               &pSrcCcb->DirectoryCB->NameInformation.FileName,
2514                               &pTargetDirEntry->NameInformation.FileName);
2515
2516                 try_return( ntStatus = STATUS_OBJECT_NAME_COLLISION);
2517             }
2518
2519             AFSDbgLogMsg( AFS_SUBSYSTEM_FILE_PROCESSING | AFS_SUBSYSTEM_DIRENTRY_REF_COUNTING,
2520                           AFS_TRACE_LEVEL_ERROR,
2521                           "AFSSetFileLinkInfo Target %wZ exists DE %p Count %d, performing delete of target\n",
2522                           &pTargetDirEntry->NameInformation.FileName,
2523                           pTargetDirEntry,
2524                           lCount);
2525
2526             //
2527             // Pull the directory entry from the parent
2528             //
2529
2530             AFSRemoveDirNodeFromParent( pTargetParentObject,
2531                                         pTargetDirEntry,
2532                                         FALSE);
2533
2534             bTargetEntryExists = TRUE;
2535         }
2536         else
2537         {
2538             AFSDbgLogMsg( AFS_SUBSYSTEM_FILE_PROCESSING,
2539                           AFS_TRACE_LEVEL_VERBOSE,
2540                           "AFSSetFileLinkInfo Target does NOT exist, normal linking\n");
2541         }
2542
2543         //
2544         // OK, this is a simple rename. Issue the rename
2545         // request to the service.
2546         //
2547
2548         ntStatus = AFSNotifyHardLink( pSrcFcb->ObjectInformation,
2549                                       &pSrcCcb->AuthGroup,
2550                                       pSrcParentObject,
2551                                       pTargetDcb->ObjectInformation,
2552                                       pSrcCcb->DirectoryCB,
2553                                       &uniTargetName,
2554                                       pFileLinkInfo->ReplaceIfExists,
2555                                       &pNewTargetDirEntry);
2556
2557         if( ntStatus != STATUS_REPARSE &&
2558             !NT_SUCCESS( ntStatus))
2559         {
2560
2561             AFSDbgLogMsg( AFS_SUBSYSTEM_FILE_PROCESSING,
2562                           AFS_TRACE_LEVEL_ERROR,
2563                           "AFSSetFileLinkInfo Failed link of %wZ to target %wZ Status %08lX\n",
2564                           &pSrcCcb->DirectoryCB->NameInformation.FileName,
2565                           &uniTargetName,
2566                           ntStatus);
2567
2568             try_return( ntStatus);
2569         }
2570
2571         if ( ntStatus != STATUS_REPARSE)
2572         {
2573
2574             AFSInsertDirectoryNode( pTargetDcb->ObjectInformation,
2575                                     pNewTargetDirEntry,
2576                                     TRUE);
2577         }
2578
2579         //
2580         // Send notification for the target link file
2581         //
2582
2583         if( bTargetEntryExists || pNewTargetDirEntry)
2584         {
2585
2586             ulNotificationAction = FILE_ACTION_MODIFIED;
2587         }
2588         else
2589         {
2590
2591             ulNotificationAction = FILE_ACTION_ADDED;
2592         }
2593
2594         AFSFsRtlNotifyFullReportChange( pTargetParentObject,
2595                                         pSrcCcb,
2596                                         (ULONG)ulNotifyFilter,
2597                                         (ULONG)ulNotificationAction);
2598
2599       try_exit:
2600
2601         if( !NT_SUCCESS( ntStatus))
2602         {
2603
2604             if( bTargetEntryExists)
2605             {
2606
2607                 AFSInsertDirectoryNode( pTargetParentObject,
2608                                         pTargetDirEntry,
2609                                         FALSE);
2610             }
2611         }
2612
2613         if( pTargetDirEntry != NULL)
2614         {
2615
2616             //
2617             // Release DirOpenReferenceCount obtained above
2618             //
2619
2620             lCount = InterlockedDecrement( &pTargetDirEntry->DirOpenReferenceCount);
2621
2622             AFSDbgLogMsg( AFS_SUBSYSTEM_DIRENTRY_REF_COUNTING,
2623                           AFS_TRACE_LEVEL_VERBOSE,
2624                           "AFSSetFileLinkInfo Decrement count on %wZ DE %p Ccb %p Cnt %d\n",
2625                           &pTargetDirEntry->NameInformation.FileName,
2626                           pTargetDirEntry,
2627                           pSrcCcb,
2628                           lCount);
2629
2630             ASSERT( lCount >= 0);
2631         }
2632
2633         if( pNewTargetDirEntry != NULL)
2634         {
2635
2636             //
2637             // Release DirOpenReferenceCount obtained from AFSNotifyHardLink
2638             //
2639
2640             lCount = InterlockedDecrement( &pNewTargetDirEntry->DirOpenReferenceCount);
2641
2642             AFSDbgLogMsg( AFS_SUBSYSTEM_DIRENTRY_REF_COUNTING,
2643                           AFS_TRACE_LEVEL_VERBOSE,
2644                           "AFSSetFileLinkInfo Decrement count on %wZ DE %p Ccb %p Cnt %d\n",
2645                           &pNewTargetDirEntry->NameInformation.FileName,
2646                           pNewTargetDirEntry,
2647                           pSrcCcb,
2648                           lCount);
2649
2650             ASSERT( lCount >= 0);
2651         }
2652
2653         if( bReleaseTargetDirLock)
2654         {
2655
2656             AFSReleaseResource( pTargetParentObject->Specific.Directory.DirectoryNodeHdr.TreeLock);
2657         }
2658
2659         if ( pSrcParentObject != NULL)
2660         {
2661
2662             AFSReleaseObjectInfo( &pSrcParentObject);
2663         }
2664
2665         //
2666         // No need to release pTargetParentObject as it is either a copy of pSrcParentObject
2667         // or (AFSFcb *)pTargetFileObj->FsContext->ObjectInformation
2668         //
2669
2670         pTargetParentObject = NULL;
2671     }
2672
2673     return ntStatus;
2674 }
2675
2676 NTSTATUS
2677 AFSSetRenameInfo( IN PIRP Irp)
2678 {
2679
2680     NTSTATUS ntStatus = STATUS_SUCCESS;
2681     AFSDeviceExt *pDeviceExt = (AFSDeviceExt *)AFSRDRDeviceObject->DeviceExtension;
2682     PIO_STACK_LOCATION pIrpSp = IoGetCurrentIrpStackLocation( Irp);
2683     AFSFcb *pSrcFcb = NULL, *pTargetDcb = NULL, *pTargetFcb = NULL;
2684     AFSCcb *pSrcCcb = NULL, *pTargetDirCcb = NULL;
2685     PFILE_OBJECT pSrcFileObj = pIrpSp->FileObject;
2686     PFILE_OBJECT pTargetFileObj = pIrpSp->Parameters.SetFile.FileObject;
2687     PFILE_OBJECT pTargetParentFileObj = NULL;
2688     PFILE_RENAME_INFORMATION pRenameInfo = NULL;
2689     UNICODE_STRING uniTargetName, uniSourceName, uniTargetParentName;
2690     BOOLEAN bReplaceIfExists = FALSE;
2691     UNICODE_STRING uniShortName;
2692     AFSDirectoryCB *pTargetDirEntry = NULL;
2693     ULONG ulTargetCRC = 0;
2694     BOOLEAN bTargetEntryExists = FALSE;
2695     AFSObjectInfoCB *pSrcObject = NULL;
2696     AFSObjectInfoCB *pSrcParentObject = NULL, *pTargetParentObject = NULL;
2697     AFSFileID stNewFid;
2698     ULONG ulNotificationAction = 0, ulNotifyFilter = 0;
2699     UNICODE_STRING uniFullTargetName;
2700     BOOLEAN bCommonParent = FALSE;
2701     BOOLEAN bReleaseTargetDirLock = FALSE;
2702     BOOLEAN bReleaseSourceDirLock = FALSE;
2703     BOOLEAN bDereferenceTargetParentObject = FALSE;
2704     PERESOURCE  pSourceDirLock = NULL;
2705     LONG lCount;
2706
2707     __Enter
2708     {
2709
2710         bReplaceIfExists = pIrpSp->Parameters.SetFile.ReplaceIfExists;
2711
2712         pRenameInfo = (PFILE_RENAME_INFORMATION)Irp->AssociatedIrp.SystemBuffer;
2713
2714         pSrcFcb = (AFSFcb *)pSrcFileObj->FsContext;
2715         pSrcCcb = (AFSCcb *)pSrcFileObj->FsContext2;
2716
2717         pSrcObject = pSrcFcb->ObjectInformation;
2718
2719         if ( BooleanFlagOn( pSrcFcb->ObjectInformation->Flags, AFS_OBJECT_FLAGS_PARENT_FID))
2720         {
2721
2722             pSrcParentObject = AFSFindObjectInfo( pSrcFcb->ObjectInformation->VolumeCB,
2723                                                   &pSrcFcb->ObjectInformation->ParentFileId);
2724         }
2725
2726         //
2727         // Perform some basic checks to ensure FS integrity
2728         //
2729
2730         if( pSrcFcb->Header.NodeTypeCode == AFS_ROOT_FCB)
2731         {
2732
2733             //
2734             // Can't rename the root directory
2735             //
2736
2737             AFSDbgLogMsg( AFS_SUBSYSTEM_FILE_PROCESSING,
2738                           AFS_TRACE_LEVEL_ERROR,
2739                           "AFSSetRenameInfo Attempt to rename root entry\n");
2740
2741             try_return( ntStatus = STATUS_INVALID_PARAMETER);
2742         }
2743
2744         if( pSrcFcb->Header.NodeTypeCode == AFS_DIRECTORY_FCB)
2745         {
2746
2747             //
2748             // If there are any open children then fail the rename
2749             //
2750
2751             if( pSrcFcb->ObjectInformation->Specific.Directory.ChildOpenHandleCount > 0)
2752             {
2753
2754                 AFSDbgLogMsg( AFS_SUBSYSTEM_FILE_PROCESSING,
2755                               AFS_TRACE_LEVEL_ERROR,
2756                               "AFSSetRenameInfo Attempt to rename directory with open children %wZ\n",
2757                               &pSrcCcb->DirectoryCB->NameInformation.FileName);
2758
2759                 try_return( ntStatus = STATUS_ACCESS_DENIED);
2760             }
2761         }
2762
2763
2764         //
2765         // Extract off the final component name from the Fcb
2766         //
2767
2768         uniSourceName.Length = (USHORT)pSrcCcb->DirectoryCB->NameInformation.FileName.Length;
2769         uniSourceName.MaximumLength = uniSourceName.Length;
2770
2771         uniSourceName.Buffer = pSrcCcb->DirectoryCB->NameInformation.FileName.Buffer;
2772
2773         //
2774         // Resolve the target fileobject
2775         //
2776
2777         if( pTargetFileObj == NULL)
2778         {
2779
2780             if ( pRenameInfo->RootDirectory)
2781             {
2782
2783                 AFSDbgLogMsg( AFS_SUBSYSTEM_FILE_PROCESSING,
2784                               AFS_TRACE_LEVEL_ERROR,
2785                               "AFSSetRenameInfo Handle provided but no FileObject ntStatus INVALID_PARAMETER\n");
2786
2787                 try_return( ntStatus = STATUS_INVALID_PARAMETER);
2788             }
2789             else
2790             {
2791
2792                 uniFullTargetName.Length = (USHORT)pRenameInfo->FileNameLength;
2793
2794                 uniFullTargetName.Buffer = (PWSTR)&pRenameInfo->FileName;
2795
2796                 AFSRetrieveFinalComponent( &uniFullTargetName,
2797                                            &uniTargetName);
2798
2799                 AFSRetrieveParentPath( &uniFullTargetName,
2800                                        &uniTargetParentName);
2801
2802                 if ( uniTargetParentName.Length == 0)
2803                 {
2804
2805                     //
2806                     // This is a simple rename. Here the target directory is the same as the source parent directory
2807                     // and the name is retrieved from the system buffer information
2808                     //
2809
2810                     pTargetParentObject = pSrcParentObject;
2811                 }
2812                 else
2813                 {
2814                     //
2815                     // uniTargetParentName contains the directory the renamed object
2816                     // will be moved to.  Must obtain the TargetParentObject.
2817                     //
2818
2819                     AFSDbgLogMsg( AFS_SUBSYSTEM_FILE_PROCESSING,
2820                                   AFS_TRACE_LEVEL_ERROR,
2821                                   "AFSSetRenameInfo Attempt to move %wZ to %wZ -- not yet supported (NOT_SAME_DEVICE)\n",
2822                                   &pSrcCcb->DirectoryCB->NameInformation.FileName,
2823                                   &uniFullTargetName);
2824
2825                     try_return( ntStatus = STATUS_NOT_SAME_DEVICE);
2826                 }
2827             }
2828
2829             pTargetDcb = pTargetParentObject->Fcb;
2830         }
2831         else
2832         {
2833
2834             //
2835             // So here we have the target directory taken from the targetfile object
2836             //
2837
2838             pTargetDcb = (AFSFcb *)pTargetFileObj->FsContext;
2839
2840             pTargetDirCcb = (AFSCcb *)pTargetFileObj->FsContext2;
2841
2842             pTargetParentObject = (AFSObjectInfoCB *)pTargetDcb->ObjectInformation;
2843
2844             //
2845             // Grab the target name which we setup in the IRP_MJ_CREATE handler. By how we set this up
2846             // it is only the target component of the rename operation
2847             //
2848
2849             uniTargetName = *((PUNICODE_STRING)&pTargetFileObj->FileName);
2850         }
2851
2852         //
2853         // The quick check to see if they are not really performing a rename
2854         // Do the names match? Only do this where the parent directories are
2855         // the same
2856         //
2857
2858         if( pTargetParentObject == pSrcParentObject)
2859         {
2860
2861             if( FsRtlAreNamesEqual( &uniTargetName,
2862                                     &uniSourceName,
2863                                     FALSE,
2864                                     NULL))
2865             {
2866                 try_return( ntStatus = STATUS_SUCCESS);
2867             }
2868
2869             bCommonParent = TRUE;
2870         }
2871         else
2872         {
2873
2874             bCommonParent = FALSE;
2875         }
2876
2877         //
2878         // We do not allow cross-volume renames to occur
2879         //
2880
2881         if( pTargetParentObject->VolumeCB != pSrcObject->VolumeCB)
2882         {
2883
2884             AFSDbgLogMsg( AFS_SUBSYSTEM_FILE_PROCESSING,
2885                           AFS_TRACE_LEVEL_ERROR,
2886                           "AFSSetRenameInfo Attempt to rename directory to different volume %wZ\n",
2887                           &pSrcCcb->DirectoryCB->NameInformation.FileName);
2888
2889             try_return( ntStatus = STATUS_NOT_SAME_DEVICE);
2890         }
2891
2892         ulTargetCRC = AFSGenerateCRC( &uniTargetName,
2893                                       FALSE);
2894
2895         AFSAcquireExcl( pTargetParentObject->Specific.Directory.DirectoryNodeHdr.TreeLock,
2896                         TRUE);
2897
2898         bReleaseTargetDirLock = TRUE;
2899
2900         if( pTargetParentObject != pSrcParentObject)
2901         {
2902             AFSAcquireExcl( pSrcParentObject->Specific.Directory.DirectoryNodeHdr.TreeLock,
2903                             TRUE);
2904
2905             bReleaseSourceDirLock = TRUE;
2906
2907             pSourceDirLock = pSrcParentObject->Specific.Directory.DirectoryNodeHdr.TreeLock;
2908         }
2909
2910         AFSLocateCaseSensitiveDirEntry( pTargetParentObject->Specific.Directory.DirectoryNodeHdr.CaseSensitiveTreeHead,
2911                                         ulTargetCRC,
2912                                         &pTargetDirEntry);
2913
2914         if( pTargetDirEntry == NULL)
2915         {
2916
2917             //
2918             // Missed so perform a case insensitive lookup
2919             //
2920
2921             ulTargetCRC = AFSGenerateCRC( &uniTargetName,
2922                                           TRUE);
2923
2924             AFSLocateCaseInsensitiveDirEntry( pTargetParentObject->Specific.Directory.DirectoryNodeHdr.CaseInsensitiveTreeHead,
2925                                               ulTargetCRC,
2926                                               &pTargetDirEntry);
2927         }
2928
2929         if ( !BooleanFlagOn( pDeviceExt->DeviceFlags, AFS_DEVICE_FLAG_DISABLE_SHORTNAMES) &&
2930              pTargetDirEntry == NULL && RtlIsNameLegalDOS8Dot3( &uniTargetName,
2931                                                                NULL,
2932                                                                NULL))
2933         {
2934             //
2935             // Try the short name
2936             //
2937             AFSLocateShortNameDirEntry( pTargetParentObject->Specific.Directory.ShortNameTree,
2938                                         ulTargetCRC,
2939                                         &pTargetDirEntry);
2940         }
2941
2942         //
2943         // Increment our ref count on the dir entry
2944         //
2945
2946         if( pTargetDirEntry != NULL)
2947         {
2948
2949             ASSERT( BooleanFlagOn( pTargetDirEntry->ObjectInformation->Flags, AFS_OBJECT_FLAGS_PARENT_FID) &&
2950                     AFSIsEqualFID( &pTargetParentObject->FileId, &pTargetDirEntry->ObjectInformation->ParentFileId));
2951
2952             lCount = InterlockedIncrement( &pTargetDirEntry->DirOpenReferenceCount);
2953
2954             AFSDbgLogMsg( AFS_SUBSYSTEM_DIRENTRY_REF_COUNTING,
2955                           AFS_TRACE_LEVEL_VERBOSE,
2956                           "AFSSetRenameInfo Increment count on %wZ DE %p Ccb %p Cnt %d\n",
2957                           &pTargetDirEntry->NameInformation.FileName,
2958                           pTargetDirEntry,
2959                           pSrcCcb,
2960                           lCount);
2961
2962             ASSERT( lCount >= 0);
2963
2964             if( !bReplaceIfExists)
2965             {
2966
2967                 AFSDbgLogMsg( AFS_SUBSYSTEM_FILE_PROCESSING,
2968                               AFS_TRACE_LEVEL_ERROR,
2969                               "AFSSetRenameInfo Attempt to rename directory with target collision %wZ Target %wZ\n",
2970                               &pSrcCcb->DirectoryCB->NameInformation.FileName,
2971                               &pTargetDirEntry->NameInformation.FileName);
2972
2973                 try_return( ntStatus = STATUS_OBJECT_NAME_COLLISION);
2974             }
2975
2976             AFSDbgLogMsg( AFS_SUBSYSTEM_FILE_PROCESSING | AFS_SUBSYSTEM_DIRENTRY_REF_COUNTING,
2977                           AFS_TRACE_LEVEL_ERROR,
2978                           "AFSSetRenameInfo Target %wZ exists DE %p Count %d, performing delete of target\n",
2979                           &pTargetDirEntry->NameInformation.FileName,
2980                           pTargetDirEntry,
2981                           lCount);
2982
2983             //
2984             // Pull the directory entry from the parent
2985             //
2986
2987             AFSRemoveDirNodeFromParent( pTargetParentObject,
2988                                         pTargetDirEntry,
2989                                         FALSE);
2990
2991             bTargetEntryExists = TRUE;
2992         }
2993         else
2994         {
2995             AFSDbgLogMsg( AFS_SUBSYSTEM_FILE_PROCESSING,
2996                           AFS_TRACE_LEVEL_VERBOSE,
2997                           "AFSSetRenameInfo Target does NOT exist, normal rename\n");
2998         }
2999
3000         //
3001         // We need to remove the DirEntry from the parent node, update the index
3002         // and reinsert it into the parent tree. Note that for entries with the
3003         // same parent we do not pull the node from the enumeration list
3004         //
3005
3006         AFSRemoveDirNodeFromParent( pSrcParentObject,
3007                                     pSrcCcb->DirectoryCB,
3008                                     !bCommonParent);
3009
3010         //
3011         // OK, this is a simple rename. Issue the rename
3012         // request to the service.
3013         //
3014
3015         ntStatus = AFSNotifyRename( pSrcFcb->ObjectInformation,
3016                                     &pSrcCcb->AuthGroup,
3017                                     pSrcParentObject,
3018                                     pTargetDcb->ObjectInformation,
3019                                     pSrcCcb->DirectoryCB,
3020                                     &uniTargetName,
3021                                     &stNewFid);
3022
3023         if( !NT_SUCCESS( ntStatus))
3024         {
3025
3026             //
3027             // Attempt to re-insert the directory entry
3028             //
3029
3030             AFSInsertDirectoryNode( pSrcParentObject,
3031                                     pSrcCcb->DirectoryCB,
3032                                     !bCommonParent);
3033
3034             AFSDbgLogMsg( AFS_SUBSYSTEM_FILE_PROCESSING,
3035                           AFS_TRACE_LEVEL_ERROR,
3036                           "AFSSetRenameInfo Failed rename of %wZ to target %wZ Status %08lX\n",
3037                           &pSrcCcb->DirectoryCB->NameInformation.FileName,
3038                           &uniTargetName,
3039                           ntStatus);
3040
3041             try_return( ntStatus);
3042         }
3043
3044         //
3045         // Set the notification up for the source file
3046         //
3047
3048         if( pSrcParentObject == pTargetParentObject &&
3049             !bTargetEntryExists)
3050         {
3051
3052             ulNotificationAction = FILE_ACTION_RENAMED_OLD_NAME;
3053         }
3054         else
3055         {
3056
3057             ulNotificationAction = FILE_ACTION_REMOVED;
3058         }
3059
3060         if( pSrcCcb->DirectoryCB->ObjectInformation->FileType == AFS_FILE_TYPE_DIRECTORY)
3061         {
3062
3063             ulNotifyFilter = FILE_NOTIFY_CHANGE_DIR_NAME;
3064         }
3065         else
3066         {
3067
3068             ulNotifyFilter = FILE_NOTIFY_CHANGE_FILE_NAME;
3069         }
3070
3071         AFSFsRtlNotifyFullReportChange( pSrcParentObject,
3072                                         pSrcCcb,
3073                                         (ULONG)ulNotifyFilter,
3074                                         (ULONG)ulNotificationAction);
3075
3076         //
3077         // Update the name in the dir entry.
3078         //
3079
3080         ntStatus = AFSUpdateDirEntryName( pSrcCcb->DirectoryCB,
3081                                           &uniTargetName);
3082
3083         if( !NT_SUCCESS( ntStatus))
3084         {
3085
3086             //
3087             // Attempt to re-insert the directory entry
3088             //
3089
3090             AFSInsertDirectoryNode( pSrcParentObject,
3091                                     pSrcCcb->DirectoryCB,
3092                                     !bCommonParent);
3093
3094             AFSDbgLogMsg( AFS_SUBSYSTEM_FILE_PROCESSING,
3095                           AFS_TRACE_LEVEL_ERROR,
3096                           "AFSSetRenameInfo Failed update of dir entry %wZ to target %wZ Status %08lX\n",
3097                           &pSrcCcb->DirectoryCB->NameInformation.FileName,
3098                           &uniTargetName,
3099                           ntStatus);
3100
3101             try_return( ntStatus);
3102         }
3103
3104         //
3105         // Update the object information block, if needed
3106         //
3107
3108         if( !AFSIsEqualFID( &pSrcObject->FileId,
3109                             &stNewFid))
3110         {
3111
3112             AFSAcquireExcl( pSrcObject->VolumeCB->ObjectInfoTree.TreeLock,
3113                             TRUE);
3114
3115             //
3116             // Remove the old information entry
3117             //
3118
3119             AFSRemoveHashEntry( &pSrcObject->VolumeCB->ObjectInfoTree.TreeHead,
3120                                 &pSrcObject->TreeEntry);
3121
3122             RtlCopyMemory( &pSrcObject->FileId,
3123                            &stNewFid,
3124                            sizeof( AFSFileID));
3125
3126             //
3127             // Insert the entry into the new object table.
3128             //
3129
3130             pSrcObject->TreeEntry.HashIndex = AFSCreateLowIndex( &pSrcObject->FileId);
3131
3132             if( pSrcObject->VolumeCB->ObjectInfoTree.TreeHead == NULL)
3133             {
3134
3135                 pSrcObject->VolumeCB->ObjectInfoTree.TreeHead = &pSrcObject->TreeEntry;
3136             }
3137             else
3138             {
3139
3140                 if ( !NT_SUCCESS( AFSInsertHashEntry( pSrcObject->VolumeCB->ObjectInfoTree.TreeHead,
3141                                                      &pSrcObject->TreeEntry)))
3142                 {
3143
3144                     //
3145                     // Lost a race, an ObjectInfo object already exists for this FID.
3146                     // Let this copy be garbage collected.
3147                     //
3148
3149                     ClearFlag( pSrcObject->Flags, AFS_OBJECT_INSERTED_HASH_TREE);
3150                 }
3151             }
3152
3153             AFSReleaseResource( pSrcObject->VolumeCB->ObjectInfoTree.TreeLock);
3154         }
3155
3156         //
3157         // Update the hash values for the name trees.
3158         //
3159
3160         pSrcCcb->DirectoryCB->CaseSensitiveTreeEntry.HashIndex = AFSGenerateCRC( &pSrcCcb->DirectoryCB->NameInformation.FileName,
3161                                                                                  FALSE);
3162
3163         pSrcCcb->DirectoryCB->CaseInsensitiveTreeEntry.HashIndex = AFSGenerateCRC( &pSrcCcb->DirectoryCB->NameInformation.FileName,
3164                                                                                    TRUE);
3165
3166         if( !BooleanFlagOn( pDeviceExt->DeviceFlags, AFS_DEVICE_FLAG_DISABLE_SHORTNAMES) &&
3167             pSrcCcb->DirectoryCB->NameInformation.ShortNameLength > 0 &&
3168             !RtlIsNameLegalDOS8Dot3( &pSrcCcb->DirectoryCB->NameInformation.FileName,
3169                                      NULL,
3170                                      NULL))
3171         {
3172
3173             uniShortName.Length = pSrcCcb->DirectoryCB->NameInformation.ShortNameLength;
3174             uniShortName.MaximumLength = uniShortName.Length;
3175             uniShortName.Buffer = pSrcCcb->DirectoryCB->NameInformation.ShortName;
3176
3177             pSrcCcb->DirectoryCB->Type.Data.ShortNameTreeEntry.HashIndex = AFSGenerateCRC( &uniShortName,
3178                                                                                            TRUE);
3179
3180             AFSDbgLogMsg( AFS_SUBSYSTEM_FILE_PROCESSING,
3181                           AFS_TRACE_LEVEL_VERBOSE,
3182                           "AFSSetRenameInfo Initialized short name hash for %wZ longname %wZ\n",
3183                           &uniShortName,
3184                           &pSrcCcb->DirectoryCB->NameInformation.FileName);
3185         }
3186         else
3187         {
3188
3189             pSrcCcb->DirectoryCB->Type.Data.ShortNameTreeEntry.HashIndex = 0;
3190         }
3191
3192         if( !bCommonParent)
3193         {
3194
3195             //
3196             // Update the file index for the object in the new parent
3197             //
3198
3199             pSrcCcb->DirectoryCB->FileIndex = (ULONG)InterlockedIncrement( &pTargetParentObject->Specific.Directory.DirectoryNodeHdr.ContentIndex);
3200         }
3201
3202         //
3203         // Re-insert the directory entry
3204         //
3205
3206         AFSInsertDirectoryNode( pTargetParentObject,
3207                                 pSrcCcb->DirectoryCB,
3208                                 !bCommonParent);
3209
3210         //
3211         // Update the parent pointer in the source object if they are different
3212         //
3213
3214         if( pSrcParentObject != pTargetParentObject)
3215         {
3216
3217             lCount = InterlockedDecrement( &pSrcParentObject->Specific.Directory.ChildOpenHandleCount);
3218
3219             lCount = InterlockedDecrement( &pSrcParentObject->Specific.Directory.ChildOpenReferenceCount);
3220
3221             lCount = InterlockedIncrement( &pTargetParentObject->Specific.Directory.ChildOpenHandleCount);
3222
3223             lCount = InterlockedIncrement( &pTargetParentObject->Specific.Directory.ChildOpenReferenceCount);
3224
3225             lCount = AFSObjectInfoIncrement( pTargetParentObject,
3226                                              AFS_OBJECT_REFERENCE_CHILD);
3227
3228             AFSDbgLogMsg( AFS_SUBSYSTEM_OBJECT_REF_COUNTING,
3229                           AFS_TRACE_LEVEL_VERBOSE,
3230