285342164cfc3178448995004cd191fabd2b2eab
[openafs.git] / src / WINNT / afsrdr / kernel / lib / AFSWrite.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: AFSWrite.cpp
37 //
38
39 #include "AFSCommon.h"
40
41 static
42 NTSTATUS
43 AFSCachedWrite( IN PDEVICE_OBJECT DeviceObject,
44                 IN PIRP Irp,
45                 IN LARGE_INTEGER StartingByte,
46                 IN ULONG ByteCount,
47                 IN BOOLEAN ForceFlush);
48 static
49 NTSTATUS
50 AFSNonCachedWrite( IN PDEVICE_OBJECT DeviceObject,
51                    IN PIRP Irp,
52                    IN LARGE_INTEGER StartingByte,
53                    IN ULONG ByteCount);
54
55 static
56 NTSTATUS
57 AFSNonCachedWriteDirect( IN PDEVICE_OBJECT DeviceObject,
58                          IN PIRP Irp,
59                          IN LARGE_INTEGER StartingByte,
60                          IN ULONG ByteCount);
61
62 static
63 NTSTATUS
64 AFSExtendingWrite( IN AFSFcb *Fcb,
65                    IN PFILE_OBJECT FileObject,
66                    IN LONGLONG NewLength);
67
68 //
69 // Function: AFSWrite
70 //
71 // Description:
72 //
73 //      This is the dispatch handler for the IRP_MJ_WRITE request
74 //
75 // Return:
76 //
77 //      A status is returned for the function
78 //
79 NTSTATUS
80 AFSWrite( IN PDEVICE_OBJECT LibDeviceObject,
81           IN PIRP Irp)
82 {
83
84     UNREFERENCED_PARAMETER(LibDeviceObject);
85     NTSTATUS ntStatus = STATUS_SUCCESS;
86
87     __try
88     {
89
90         ntStatus = AFSCommonWrite( AFSRDRDeviceObject, Irp, NULL, FALSE);
91     }
92     __except( AFSExceptionFilter( __FUNCTION__, GetExceptionCode(), GetExceptionInformation()) )
93     {
94
95         ntStatus = STATUS_INSUFFICIENT_RESOURCES;
96     }
97
98     return ntStatus;
99 }
100
101 NTSTATUS
102 AFSCommonWrite( IN PDEVICE_OBJECT DeviceObject,
103                 IN PIRP Irp,
104                 IN HANDLE OnBehalfOf,
105                 IN BOOLEAN bRetry)
106 {
107
108     NTSTATUS           ntStatus = STATUS_SUCCESS;
109     AFSDeviceExt      *pDeviceExt = (AFSDeviceExt *)DeviceObject->DeviceExtension;
110     IO_STACK_LOCATION *pIrpSp;
111     AFSFcb            *pFcb = NULL;
112     AFSCcb            *pCcb = NULL;
113     AFSNonPagedFcb    *pNPFcb = NULL;
114     ULONG              ulByteCount = 0;
115     LARGE_INTEGER      liStartingByte;
116     PFILE_OBJECT       pFileObject;
117     BOOLEAN            bPagingIo = FALSE;
118     BOOLEAN            bNonCachedIo = FALSE;
119     BOOLEAN            bReleaseMain = FALSE;
120     BOOLEAN            bReleaseSectionObject = FALSE;
121     BOOLEAN            bReleasePaging = FALSE;
122     BOOLEAN            bExtendingWrite = FALSE;
123     BOOLEAN            bSynchronousFo = FALSE;
124     BOOLEAN            bCompleteIrp = TRUE;
125     BOOLEAN            bForceFlush = FALSE;
126     BOOLEAN            bLockOK;
127     HANDLE             hCallingUser = OnBehalfOf;
128     ULONGLONG          ullProcessId = (ULONGLONG)PsGetCurrentProcessId();
129     AFSDeviceExt       *pRDRDevExt = (AFSDeviceExt *)AFSRDRDeviceObject->DeviceExtension;
130
131     pIrpSp = IoGetCurrentIrpStackLocation( Irp);
132
133     __Enter
134     {
135
136         Irp->IoStatus.Information = 0;
137
138         pFileObject = pIrpSp->FileObject;
139
140         //
141         // Extract the fileobject references
142         //
143
144         pFcb = (AFSFcb *)pFileObject->FsContext;
145         pCcb = (AFSCcb *)pFileObject->FsContext2;
146
147         ObReferenceObject( pFileObject);
148
149         if( pFcb == NULL)
150         {
151
152             AFSDbgTrace(( AFS_SUBSYSTEM_IO_PROCESSING,
153                           AFS_TRACE_LEVEL_ERROR,
154                           "AFSCommonWrite Attempted write (%p) when pFcb == NULL\n",
155                           Irp));
156
157             try_return( ntStatus = STATUS_INVALID_DEVICE_REQUEST);
158         }
159
160         pNPFcb = pFcb->NPFcb;
161
162         //
163         // If we are in shutdown mode then fail the request
164         //
165
166         if( BooleanFlagOn( pDeviceExt->DeviceFlags, AFS_DEVICE_FLAG_REDIRECTOR_SHUTDOWN))
167         {
168
169             AFSDbgTrace(( AFS_SUBSYSTEM_FILE_PROCESSING,
170                           AFS_TRACE_LEVEL_WARNING,
171                           "AFSCommonWrite (%p) Open request after shutdown\n",
172                           Irp));
173
174             try_return( ntStatus = STATUS_TOO_LATE);
175         }
176
177         liStartingByte = pIrpSp->Parameters.Write.ByteOffset;
178         bPagingIo      = BooleanFlagOn( Irp->Flags, IRP_PAGING_IO);
179         bNonCachedIo   = BooleanFlagOn( Irp->Flags, IRP_NOCACHE);
180         ulByteCount    = pIrpSp->Parameters.Write.Length;
181         bSynchronousFo = BooleanFlagOn( pFileObject->Flags, FO_SYNCHRONOUS_IO);
182
183         if( pFcb->Header.NodeTypeCode != AFS_IOCTL_FCB &&
184             pFcb->Header.NodeTypeCode != AFS_FILE_FCB  &&
185             pFcb->Header.NodeTypeCode != AFS_SPECIAL_SHARE_FCB)
186         {
187
188             AFSDbgTrace(( AFS_SUBSYSTEM_IO_PROCESSING,
189                           AFS_TRACE_LEVEL_ERROR,
190                           "AFSCommonWrite Attempted write (%p) on an invalid node type %08lX\n",
191                           Irp,
192                           pFcb->Header.NodeTypeCode));
193
194             try_return( ntStatus = STATUS_INVALID_DEVICE_REQUEST);
195         }
196
197         //
198         // If this is a write against an IOCtl node then handle it
199         // in a different pathway
200         //
201
202         if( pFcb->Header.NodeTypeCode == AFS_IOCTL_FCB)
203         {
204
205             AFSDbgTrace(( AFS_SUBSYSTEM_IO_PROCESSING,
206                           AFS_TRACE_LEVEL_VERBOSE,
207                           "AFSCommonWrite (%p) Processing file (PIOCTL) Offset %0I64X Length %08lX Irp Flags %08lX\n",
208                           Irp,
209                           liStartingByte.QuadPart,
210                           ulByteCount,
211                           Irp->Flags));
212
213             ntStatus = AFSIOCtlWrite( DeviceObject,
214                                       Irp);
215
216             try_return( ntStatus);
217         }
218         else if( pFcb->Header.NodeTypeCode == AFS_SPECIAL_SHARE_FCB)
219         {
220
221             AFSDbgTrace(( AFS_SUBSYSTEM_IO_PROCESSING,
222                           AFS_TRACE_LEVEL_VERBOSE,
223                           "AFSCommonWrite (%p) Processing file (SHARE) Offset %0I64X Length %08lX Irp Flags %08lX\n",
224                           Irp,
225                           liStartingByte.QuadPart,
226                           ulByteCount,
227                           Irp->Flags));
228
229             ntStatus = AFSShareWrite( DeviceObject,
230                                       Irp);
231
232             try_return( ntStatus);
233         }
234
235         //
236         // Is the Cache not there yet?  Exit.
237         //
238         if( !BooleanFlagOn( AFSLibControlFlags, AFS_REDIR_LIB_FLAGS_NONPERSISTENT_CACHE) &&
239             !BooleanFlagOn( pRDRDevExt->DeviceFlags, AFS_REDIR_INIT_PERFORM_SERVICE_IO) &&
240             NULL == pDeviceExt->Specific.RDR.CacheFileObject)
241         {
242
243             AFSDbgTrace(( AFS_SUBSYSTEM_IO_PROCESSING,
244                           AFS_TRACE_LEVEL_ERROR,
245                           "AFSCommonWrite (%p) Request failed due to AFS cache closed\n",
246                           Irp));
247
248             try_return( ntStatus = STATUS_TOO_LATE );
249         }
250
251         if( pFcb->ObjectInformation->VolumeCB != NULL &&
252             BooleanFlagOn( pFcb->ObjectInformation->VolumeCB->VolumeInformation.FileSystemAttributes, FILE_READ_ONLY_VOLUME))
253         {
254
255             AFSDbgTrace(( AFS_SUBSYSTEM_IO_PROCESSING,
256                           AFS_TRACE_LEVEL_ERROR,
257                           "AFSCommonWrite (%p) Request failed due to read only volume\n",
258                           Irp));
259
260             try_return( ntStatus = STATUS_ACCESS_DENIED);
261         }
262
263         //
264         // We need to know on whose behalf we have been called (which
265         // we will eventually tell to the server - for non paging
266         // writes).  If we were posted then we were told.  If this is
267         // the first time we saw the irp then we grab it now.
268         //
269         if( NULL == OnBehalfOf )
270         {
271
272             hCallingUser = PsGetCurrentProcessId();
273         }
274         else
275         {
276
277             hCallingUser = OnBehalfOf;
278         }
279
280         //
281         // Check for zero length write
282         //
283
284         if( ulByteCount == 0)
285         {
286
287             AFSDbgTrace(( AFS_SUBSYSTEM_IO_PROCESSING,
288                           AFS_TRACE_LEVEL_VERBOSE,
289                           "AFSCommonWrite (%p) Request completed due to zero length\n",
290                           Irp));
291
292             try_return( ntStatus);
293         }
294
295         //
296         // Is this Fcb valid???
297         //
298
299         if( BooleanFlagOn( pFcb->ObjectInformation->Flags, AFS_OBJECT_FLAGS_OBJECT_INVALID))
300         {
301
302             AFSDbgTrace(( AFS_SUBSYSTEM_IO_PROCESSING,
303                           AFS_TRACE_LEVEL_ERROR,
304                           "AFSCommonWrite (%p) Failing request due to INVALID fcb\n",
305                           Irp));
306
307             try_return( ntStatus = STATUS_FILE_DELETED);
308         }
309
310         if( BooleanFlagOn( pCcb->DirectoryCB->Flags, AFS_DIR_ENTRY_DELETED) ||
311             BooleanFlagOn( pFcb->ObjectInformation->Flags, AFS_OBJECT_FLAGS_DELETED))
312         {
313
314             AFSDbgTrace(( AFS_SUBSYSTEM_IO_PROCESSING,
315                           AFS_TRACE_LEVEL_ERROR,
316                           "AFSCommonWrite (%p) Request failed due to file deleted\n",
317                           Irp));
318
319             try_return( ntStatus = STATUS_FILE_DELETED);
320         }
321
322         if( FlagOn( pIrpSp->MinorFunction, IRP_MN_COMPLETE))
323         {
324
325             AFSDbgTrace(( AFS_SUBSYSTEM_IO_PROCESSING,
326                           AFS_TRACE_LEVEL_VERBOSE,
327                           "AFSCommonWrite (%p) IRP_MN_COMPLETE being processed\n",
328                           Irp));
329
330             CcMdlWriteComplete(pFileObject, &pIrpSp->Parameters.Write.ByteOffset, Irp->MdlAddress);
331
332             //
333             // Mdl is now Deallocated
334             //
335
336             Irp->MdlAddress = NULL;
337
338             try_return( ntStatus = STATUS_SUCCESS );
339         }
340
341         //
342         // If we get a non cached IO for a cached file we should do a purge.
343         // For now we will just promote to cached
344         //
345         if( NULL != pFileObject->SectionObjectPointer->DataSectionObject && !bPagingIo && bNonCachedIo)
346         {
347             bNonCachedIo = FALSE;
348             bForceFlush = TRUE;
349         }
350
351         if ( !bNonCachedIo && !bPagingIo)
352         {
353
354             if( pFileObject->PrivateCacheMap == NULL)
355             {
356
357                 AFSDbgTrace(( AFS_SUBSYSTEM_LOCK_PROCESSING,
358                               AFS_TRACE_LEVEL_VERBOSE,
359                               "AFSCommonWrite Acquiring Fcb SectionObject lock %p EXCL %08lX\n",
360                               &pNPFcb->SectionObjectResource,
361                               PsGetCurrentThread()));
362
363                 AFSAcquireExcl( &pNPFcb->SectionObjectResource,
364                                 TRUE);
365
366                 bReleaseSectionObject = TRUE;
367
368                 __try
369                 {
370
371                     AFSDbgTrace(( AFS_SUBSYSTEM_IO_PROCESSING,
372                                   AFS_TRACE_LEVEL_VERBOSE,
373                                   "AFSCommonWrite Initialize caching on Fcb %p FileObject %p\n",
374                                   pFcb,
375                                   pFileObject));
376
377                     CcInitializeCacheMap( pFileObject,
378                                           (PCC_FILE_SIZES)&pFcb->Header.AllocationSize,
379                                           FALSE,
380                                           AFSLibCacheManagerCallbacks,
381                                           pFcb);
382
383                     CcSetReadAheadGranularity( pFileObject,
384                                                pDeviceExt->Specific.RDR.MaximumRPCLength);
385
386                     CcSetDirtyPageThreshold( pFileObject,
387                                              AFS_DIRTY_CHUNK_THRESHOLD * pDeviceExt->Specific.RDR.MaximumRPCLength / 4096);
388                 }
389                 __except( EXCEPTION_EXECUTE_HANDLER)
390                 {
391
392                     ntStatus = GetExceptionCode();
393
394                     AFSDbgTrace(( AFS_SUBSYSTEM_IO_PROCESSING,
395                                   AFS_TRACE_LEVEL_ERROR,
396                                   "AFSCommonWrite (%p) Exception thrown while initializing cache map Status %08lX\n",
397                                   Irp,
398                                   ntStatus));
399                 }
400
401                 AFSDbgTrace(( AFS_SUBSYSTEM_LOCK_PROCESSING,
402                               AFS_TRACE_LEVEL_VERBOSE,
403                               "AFSCommonWrite Releasing Fcb SectionObject lock %p EXCL %08lX\n",
404                               &pNPFcb->SectionObjectResource,
405                               PsGetCurrentThread()));
406
407                 AFSReleaseResource( &pNPFcb->SectionObjectResource);
408
409                 bReleaseSectionObject = FALSE;
410
411                 if( !NT_SUCCESS( ntStatus))
412                 {
413
414                     try_return( ntStatus);
415                 }
416             }
417
418             if (!CcCanIWrite( pFileObject,
419                               ulByteCount,
420                               FALSE,
421                               bRetry))
422             {
423
424                 AFSDbgTrace(( AFS_SUBSYSTEM_IO_PROCESSING,
425                               AFS_TRACE_LEVEL_WARNING,
426                               "AFSCommonWrite (FO: %p) CcCanIWrite says No room for Offset %0I64X Length %08lX bytes! Deferring%s\n",
427                               pFileObject,
428                               liStartingByte.QuadPart,
429                               ulByteCount,
430                               bRetry ? " RETRY" : ""));
431
432                 ntStatus = AFSDeferWrite( DeviceObject, pFileObject, hCallingUser, Irp, ulByteCount, bRetry);
433
434                 if ( STATUS_PENDING == ntStatus)
435                 {
436
437                     bCompleteIrp = FALSE;
438                 }
439                 else
440                 {
441
442                     AFSDbgTrace(( AFS_SUBSYSTEM_IO_PROCESSING,
443                                   AFS_TRACE_LEVEL_ERROR,
444                                   "AFSCommonWrite (FO: %p) AFSDeferWrite failure Status %08lX\n",
445                                   pFileObject,
446                                   ntStatus));
447                 }
448
449                 try_return( ntStatus);
450             }
451         }
452
453         //
454         // Save off the PID if this is not a paging IO
455         //
456
457         if( !bPagingIo &&
458             ( pFcb->Specific.File.ExtentRequestProcessId == 0 ||
459               ( ullProcessId != (ULONGLONG)AFSSysProcess &&
460                 pFcb->Specific.File.ExtentRequestProcessId != ullProcessId)))
461         {
462
463             pFcb->Specific.File.ExtentRequestProcessId = ullProcessId;
464
465             if( ullProcessId == (ULONGLONG)AFSSysProcess)
466             {
467                 AFSDbgTrace(( AFS_SUBSYSTEM_EXTENT_PROCESSING,
468                               AFS_TRACE_LEVEL_WARNING,
469                               "%s Setting LastWriterExtentProcessId to system process for Fcb %p\n",
470                               __FUNCTION__,
471                               pFcb));
472             }
473         }
474
475         //
476         // Take locks
477         //
478         //   - if Paging then we need to do nothing (the precalls will
479         //     have acquired the paging resource), for clarity we will collect
480         //     the paging resource
481         //   - If extending Write then take the fileresource EX (EOF will change, Allocation will only move out)
482         //   - Otherwise we collect the file shared, check against extending and
483         //
484
485         bLockOK = FALSE;
486
487         do
488         {
489
490             if( bPagingIo)
491             {
492
493                 //ASSERT( NULL != OnBehalfOf || ExIsResourceAcquiredLite( &pNPFcb->Resource ));
494
495                 AFSDbgTrace(( AFS_SUBSYSTEM_LOCK_PROCESSING,
496                               AFS_TRACE_LEVEL_VERBOSE,
497                               "AFSCommonWrite Acquiring Fcb PagingIo lock %p SHARED %08lX\n",
498                               &pNPFcb->PagingResource,
499                               PsGetCurrentThread()));
500
501                 AFSAcquireShared( &pNPFcb->PagingResource,
502                                   TRUE);
503
504                 bReleasePaging = TRUE;
505
506                 //
507                 // We have the correct lock - we cannot have the wrong one
508                 //
509                 bLockOK = TRUE;
510             }
511             else
512             {
513
514                 bExtendingWrite = (((liStartingByte.QuadPart + ulByteCount) >=
515                                      pFcb->Header.FileSize.QuadPart) ||
516                                     (liStartingByte.LowPart == FILE_WRITE_TO_END_OF_FILE &&
517                                       liStartingByte.HighPart == -1)) ;
518
519                 if( bExtendingWrite || bNonCachedIo)
520                 {
521                     //
522                     // Check for lock inversion
523                     //
524
525                     ASSERT( !ExIsResourceAcquiredLite( &pNPFcb->PagingResource ));
526
527                     AFSDbgTrace(( AFS_SUBSYSTEM_LOCK_PROCESSING,
528                                   AFS_TRACE_LEVEL_VERBOSE,
529                                   "AFSCommonWrite Acquiring Fcb lock %p EXCL %08lX\n",
530                                   &pNPFcb->Resource,
531                                   PsGetCurrentThread()));
532
533                     AFSAcquireExcl( &pNPFcb->Resource,
534                                     TRUE);
535
536                     bReleaseMain = TRUE;
537
538                     AFSDbgTrace(( AFS_SUBSYSTEM_LOCK_PROCESSING,
539                                   AFS_TRACE_LEVEL_VERBOSE,
540                                   "AFSCommonWrite Acquiring Fcb SectionObject lock %p EXCL %08lX\n",
541                                   &pNPFcb->SectionObjectResource,
542                                   PsGetCurrentThread()));
543
544                     AFSAcquireExcl( &pNPFcb->SectionObjectResource,
545                                     TRUE);
546
547                     bReleaseSectionObject = TRUE;
548
549                     if (liStartingByte.LowPart == FILE_WRITE_TO_END_OF_FILE &&
550                          liStartingByte.HighPart == -1)
551                     {
552                         if (pFcb->Header.ValidDataLength.QuadPart > pFcb->Header.FileSize.QuadPart)
553                         {
554                             liStartingByte = pFcb->Header.ValidDataLength;
555                         }
556                         else
557                         {
558                             liStartingByte = pFcb->Header.FileSize;
559                         }
560                     }
561
562                     //
563                     // We have the correct lock - even if we don't end up truncating
564                     //
565                     bLockOK = TRUE;
566                 }
567                 else
568                 {
569                     ASSERT( !ExIsResourceAcquiredLite( &pNPFcb->PagingResource ));
570
571                     AFSDbgTrace(( AFS_SUBSYSTEM_LOCK_PROCESSING,
572                                   AFS_TRACE_LEVEL_VERBOSE,
573                                   "AFSCommonWrite Acquiring Fcb lock %p SHARED %08lX\n",
574                                   &pNPFcb->Resource,
575                                   PsGetCurrentThread()));
576
577                     AFSAcquireShared( &pNPFcb->Resource,
578                                       TRUE);
579
580                     bReleaseMain = TRUE;
581
582                     AFSDbgTrace(( AFS_SUBSYSTEM_LOCK_PROCESSING,
583                                   AFS_TRACE_LEVEL_VERBOSE,
584                                   "AFSCommonWrite Acquiring Fcb SectionObject lock %p SHARED %08lX\n",
585                                   &pNPFcb->SectionObjectResource,
586                                   PsGetCurrentThread()));
587
588                     AFSAcquireShared( &pNPFcb->SectionObjectResource,
589                                       TRUE);
590
591                     bReleaseSectionObject = TRUE;
592
593                     //
594                     // Have things moved?  Are we extending? If so, the the lock isn't OK
595                     //
596                     bLockOK = (liStartingByte.QuadPart + ulByteCount) < pFcb->Header.FileSize.QuadPart;
597
598                     if (!bLockOK)
599                     {
600
601                         AFSReleaseResource( &pNPFcb->SectionObjectResource);
602
603                         bReleaseSectionObject = FALSE;
604
605                         AFSReleaseResource( &pNPFcb->Resource);
606
607                         bReleaseMain = FALSE;
608                     }
609                 }
610             }
611         }
612         while (!bLockOK);
613
614         if( !bPagingIo)
615         {
616
617             //
618             // Check the BR locks on the file.
619             //
620
621             if ( !FsRtlCheckLockForWriteAccess( &pFcb->Specific.File.FileLock,
622                                                 Irp))
623             {
624
625                 AFSDbgTrace(( AFS_SUBSYSTEM_IO_PROCESSING,
626                               AFS_TRACE_LEVEL_ERROR,
627                               "AFSCommonWrite (%p) Request failed due to lock conflict\n",
628                               Irp));
629
630                 try_return( ntStatus = STATUS_FILE_LOCK_CONFLICT);
631             }
632
633             if( bExtendingWrite)
634             {
635
636                 ntStatus = AFSExtendingWrite( pFcb, pFileObject, (liStartingByte.QuadPart + ulByteCount));
637
638                 if( !NT_SUCCESS(ntStatus))
639                 {
640
641                     AFSDbgTrace(( AFS_SUBSYSTEM_IO_PROCESSING,
642                                   AFS_TRACE_LEVEL_ERROR,
643                                   "AFSCommonWrite (%p) Failed extending write request Status %08lX\n",
644                                   Irp,
645                                   ntStatus));
646
647                     try_return( ntStatus );
648                 }
649             }
650         }
651
652         //
653         // Fire off the request as appropriate
654         //
655         bCompleteIrp = FALSE;
656
657         if( !bPagingIo &&
658             !bNonCachedIo)
659         {
660
661             //
662             // Main and SectionObject resources held Shared
663             //
664
665             AFSDbgTrace(( AFS_SUBSYSTEM_IO_PROCESSING,
666                           AFS_TRACE_LEVEL_VERBOSE,
667                           "AFSCommonWrite (%p) Processing CACHED request Offset %0I64X Len %08lX%s\n",
668                           Irp,
669                           liStartingByte.QuadPart,
670                           ulByteCount,
671                           bRetry ? " RETRY" : ""));
672
673             ntStatus = AFSCachedWrite( DeviceObject, Irp, liStartingByte, ulByteCount, bForceFlush);
674         }
675         else
676         {
677
678             AFSDbgTrace(( AFS_SUBSYSTEM_IO_PROCESSING,
679                           AFS_TRACE_LEVEL_VERBOSE,
680                           "AFSCommonWrite (%p) Processing NON-CACHED request Offset %0I64X Len %08lX%s\n",
681                           Irp,
682                           liStartingByte.QuadPart,
683                           ulByteCount,
684                           bRetry ? " RETRY" : ""));
685
686             if( BooleanFlagOn( pRDRDevExt->DeviceFlags, AFS_DEVICE_FLAG_DIRECT_SERVICE_IO))
687             {
688
689                 ntStatus = AFSNonCachedWriteDirect( DeviceObject, Irp,  liStartingByte, ulByteCount);
690             }
691             else
692             {
693                 ntStatus = AFSNonCachedWrite( DeviceObject, Irp,  liStartingByte, ulByteCount);
694             }
695         }
696
697 try_exit:
698
699         AFSDbgTrace(( AFS_SUBSYSTEM_IO_PROCESSING,
700                       AFS_TRACE_LEVEL_VERBOSE,
701                       "AFSCommonWrite (%p) Process complete Status %08lX\n",
702                       Irp,
703                       ntStatus));
704
705         if ( NT_SUCCESS( ntStatus))
706         {
707             if ( !bPagingIo)
708             {
709
710                 if( bSynchronousFo)
711                 {
712
713                     pFileObject->CurrentByteOffset.QuadPart = liStartingByte.QuadPart + ulByteCount;
714                 }
715
716                 //
717                 // If this extended the VDL, then update it accordingly.
718                 // Increasing the VDL does not require a call to CcSetFileSizes.
719                 //
720
721                 if( liStartingByte.QuadPart + ulByteCount > pFcb->Header.ValidDataLength.QuadPart)
722                 {
723
724                     pFcb->Header.ValidDataLength.QuadPart = liStartingByte.QuadPart + ulByteCount;
725                 }
726
727                 if( !BooleanFlagOn( pFcb->Flags, AFS_FCB_FLAG_UPDATE_LAST_WRITE_TIME))
728                 {
729
730                     SetFlag( pFcb->Flags, AFS_FCB_FLAG_UPDATE_WRITE_TIME);
731
732                     KeQuerySystemTime( &pFcb->ObjectInformation->LastWriteTime);
733                 }
734             }
735         }
736
737         if ( !bPagingIo && bNonCachedIo && CcIsFileCached( pFileObject) &&
738              pNPFcb->SectionObjectPointers.DataSectionObject != NULL &&
739              bReleaseSectionObject)
740         {
741             //
742             // Regardless of whether or not the a non-paging non-cached write
743             // succeeds or fails, if the file is cached the contents of the
744             // cache are no longer up to date.  A CcPurgeCacheSection must be
745             // performed to force subsequent cached reads to obtain the data
746             // from the service.
747             //
748             // The Fcb Resource is dropped in order to permit filters that perform
749             // an open via a worker thread in response to a purge to do so without
750             // deadlocking.  The SectionObjectResource is held across the purge to
751             // prevent racing with other cache operations.
752             //
753
754             if( bReleaseMain)
755             {
756
757                 AFSReleaseResource( &pNPFcb->Resource);
758
759                 bReleaseMain = FALSE;
760             }
761
762             if ( !CcPurgeCacheSection( &pNPFcb->SectionObjectPointers,
763                                        &liStartingByte,
764                                        ulByteCount,
765                                        FALSE))
766             {
767
768                 AFSDbgTrace(( AFS_SUBSYSTEM_IO_PROCESSING,
769                               AFS_TRACE_LEVEL_WARNING,
770                               "AFSCommonWrite CcPurgeCacheSection failure FID %08lX-%08lX-%08lX-%08lX\n",
771                               pFcb->ObjectInformation->FileId.Cell,
772                               pFcb->ObjectInformation->FileId.Volume,
773                               pFcb->ObjectInformation->FileId.Vnode,
774                               pFcb->ObjectInformation->FileId.Unique));
775
776                 SetFlag( pFcb->Flags, AFS_FCB_FLAG_PURGE_ON_CLOSE);
777             }
778         }
779
780         ObDereferenceObject(pFileObject);
781
782         if( bReleaseSectionObject)
783         {
784
785             AFSReleaseResource( &pNPFcb->SectionObjectResource);
786         }
787
788         if( bReleasePaging)
789         {
790
791             AFSReleaseResource( &pNPFcb->PagingResource);
792         }
793
794         if( bReleaseMain)
795         {
796
797             AFSReleaseResource( &pNPFcb->Resource);
798         }
799
800         if( bCompleteIrp)
801         {
802
803             AFSCompleteRequest( Irp,
804                                 ntStatus);
805         }
806     }
807
808     return ntStatus;
809 }
810
811 NTSTATUS
812 AFSIOCtlWrite( IN PDEVICE_OBJECT DeviceObject,
813                IN PIRP Irp)
814 {
815
816     UNREFERENCED_PARAMETER(DeviceObject);
817     NTSTATUS ntStatus = STATUS_SUCCESS;
818     AFSPIOCtlIORequestCB stIORequestCB;
819     PIO_STACK_LOCATION pIrpSp = IoGetCurrentIrpStackLocation( Irp);
820     AFSFcb *pFcb = NULL;
821     AFSCcb *pCcb = NULL;
822     AFSPIOCtlIOResultCB stIOResultCB;
823     ULONG ulBytesReturned = 0;
824     AFSFileID stParentFID;
825
826     __Enter
827     {
828
829         Irp->IoStatus.Information = 0;
830
831         RtlZeroMemory( &stIORequestCB,
832                        sizeof( AFSPIOCtlIORequestCB));
833
834         if( pIrpSp->Parameters.Write.Length == 0)
835         {
836
837             //
838             // Nothing to do in this case
839             //
840
841             try_return( ntStatus);
842         }
843
844         pFcb = (AFSFcb *)pIrpSp->FileObject->FsContext;
845
846         pCcb = (AFSCcb *)pIrpSp->FileObject->FsContext2;
847
848         AFSDbgTrace(( AFS_SUBSYSTEM_LOCK_PROCESSING,
849                       AFS_TRACE_LEVEL_VERBOSE,
850                       "AFSIOCtlWrite Acquiring Fcb lock %p SHARED %08lX\n",
851                       &pFcb->NPFcb->Resource,
852                       PsGetCurrentThread()));
853
854         AFSAcquireShared( &pFcb->NPFcb->Resource,
855                           TRUE);
856
857         //
858         // Get the parent fid to pass to the cm
859         //
860
861         RtlZeroMemory( &stParentFID,
862                        sizeof( AFSFileID));
863
864         if( BooleanFlagOn( pFcb->ObjectInformation->Flags, AFS_OBJECT_FLAGS_PARENT_FID))
865         {
866
867             //
868             // The parent directory FID of the node
869             //
870
871             stParentFID = pFcb->ObjectInformation->ParentFileId;
872         }
873
874         //
875         // Set the control block up
876         //
877
878         stIORequestCB.RequestId = pCcb->RequestID;
879
880         if( pFcb->ObjectInformation->VolumeCB != NULL)
881         {
882             stIORequestCB.RootId = pFcb->ObjectInformation->VolumeCB->ObjectInformation.FileId;
883         }
884
885         //
886         // Lock down the buffer
887         //
888
889         stIORequestCB.MappedBuffer = AFSMapToService( Irp,
890                                                       pIrpSp->Parameters.Write.Length);
891
892         if( stIORequestCB.MappedBuffer == NULL)
893         {
894
895             try_return( ntStatus = STATUS_INSUFFICIENT_RESOURCES);
896         }
897
898         stIORequestCB.BufferLength = pIrpSp->Parameters.Write.Length;
899
900         stIOResultCB.BytesProcessed = 0;
901
902         ulBytesReturned = sizeof( AFSPIOCtlIOResultCB);
903
904         //
905         // Issue the request to the service
906         //
907
908         ntStatus = AFSProcessRequest( AFS_REQUEST_TYPE_PIOCTL_WRITE,
909                                       AFS_REQUEST_FLAG_SYNCHRONOUS,
910                                       &pCcb->AuthGroup,
911                                       NULL,
912                                       &stParentFID,
913                                       NULL,
914                                       0,
915                                       (void *)&stIORequestCB,
916                                       sizeof( AFSPIOCtlIORequestCB),
917                                       &stIOResultCB,
918                                       &ulBytesReturned);
919
920         if( !NT_SUCCESS( ntStatus))
921         {
922
923             try_return( ntStatus);
924         }
925
926         //
927         // Update the length written
928         //
929
930         Irp->IoStatus.Information = stIOResultCB.BytesProcessed;
931
932 try_exit:
933
934         if( stIORequestCB.MappedBuffer != NULL)
935         {
936
937             AFSUnmapServiceMappedBuffer( stIORequestCB.MappedBuffer,
938                                          Irp->MdlAddress);
939         }
940
941         if( pFcb != NULL)
942         {
943
944             AFSReleaseResource( &pFcb->NPFcb->Resource);
945         }
946     }
947
948     return ntStatus;
949 }
950
951 //
952 // This function is called when we know we have to read from the AFS Cache.
953 //
954 // It ensures that we have exents for the entirety of the write and
955 // then pins the extents into memory (meaning that although we may
956 // add we will not remove).  Then it creates a scatter gather write
957 // and fires off the IRPs
958 //
959 static
960 NTSTATUS
961 AFSNonCachedWrite( IN PDEVICE_OBJECT DeviceObject,
962                    IN PIRP Irp,
963                    IN LARGE_INTEGER StartingByte,
964                    IN ULONG ByteCount)
965 {
966     NTSTATUS           ntStatus = STATUS_UNSUCCESSFUL;
967     VOID              *pSystemBuffer = NULL;
968     BOOLEAN            bPagingIo = BooleanFlagOn( Irp->Flags, IRP_PAGING_IO);
969     BOOLEAN            bLocked = FALSE;
970     BOOLEAN            bCompleteIrp = TRUE;
971     AFSGatherIo       *pGatherIo = NULL;
972     AFSIoRun          *pIoRuns = NULL;
973     AFSIoRun           stIoRuns[AFS_MAX_STACK_IO_RUNS];
974     ULONG              extentsCount = 0, runCount = 0;
975     AFSExtent         *pStartExtent = NULL;
976     AFSExtent         *pIgnoreExtent = NULL;
977     IO_STACK_LOCATION *pIrpSp = IoGetCurrentIrpStackLocation( Irp);
978     PFILE_OBJECT       pFileObject = pIrpSp->FileObject;
979     AFSFcb            *pFcb = (AFSFcb *)pFileObject->FsContext;
980     AFSCcb            *pCcb = (AFSCcb *)pFileObject->FsContext2;
981     BOOLEAN            bSynchronousFo = BooleanFlagOn( pFileObject->Flags, FO_SYNCHRONOUS_IO);
982     AFSDeviceExt      *pDevExt = (AFSDeviceExt *)DeviceObject->DeviceExtension;
983     LARGE_INTEGER      liCurrentTime, liLastRequestTime;
984     AFSDeviceExt      *pControlDevExt = (AFSDeviceExt *)AFSControlDeviceObject->DeviceExtension;
985     PFILE_OBJECT       pCacheFileObject = NULL;
986     BOOLEAN            bDerefExtents = FALSE;
987
988     __Enter
989     {
990
991         AFSDbgTrace(( AFS_SUBSYSTEM_IO_PROCESSING,
992                       AFS_TRACE_LEVEL_VERBOSE,
993                       "AFSNonCachedWrite (FO: %p) StartingByte %08lX:%08lX Length %08lX\n",
994                       pFileObject,
995                       StartingByte.HighPart,
996                       StartingByte.LowPart,
997                       ByteCount));
998
999         if (ByteCount > pDevExt->Specific.RDR.MaxIo.QuadPart)
1000         {
1001
1002             AFSDbgTrace(( AFS_SUBSYSTEM_IO_PROCESSING,
1003                           AFS_TRACE_LEVEL_ERROR,
1004                           "AFSNonCachedWrite (%p) Request %08lX Actual %08lX larger than MaxIO %I64X\n",
1005                           Irp,
1006                           ByteCount,
1007                           pIrpSp->Parameters.Write.Length,
1008                           pDevExt->Specific.RDR.MaxIo.QuadPart));
1009
1010             try_return( ntStatus = STATUS_UNSUCCESSFUL);
1011         }
1012
1013         //
1014         // Get the mapping for the buffer
1015         //
1016         pSystemBuffer = AFSLockSystemBuffer( Irp,
1017                                              ByteCount);
1018
1019         if( pSystemBuffer == NULL)
1020         {
1021
1022             AFSDbgTrace(( AFS_SUBSYSTEM_IO_PROCESSING,
1023                           AFS_TRACE_LEVEL_ERROR,
1024                           "AFSNonCachedWrite (%p) Failed to map system buffer\n",
1025                           Irp));
1026
1027             try_return( ntStatus = STATUS_INSUFFICIENT_RESOURCES);
1028         }
1029
1030
1031         //
1032         // Provoke a get of the extents - if we need to.
1033         //
1034
1035         AFSDbgTrace(( AFS_SUBSYSTEM_EXTENT_PROCESSING,
1036                       AFS_TRACE_LEVEL_VERBOSE,
1037                       "AFSNonCachedWrite Requesting extents for fid %08lX-%08lX-%08lX-%08lX Offset %0I64X Length %08lX\n",
1038                       pFcb->ObjectInformation->FileId.Cell,
1039                       pFcb->ObjectInformation->FileId.Volume,
1040                       pFcb->ObjectInformation->FileId.Vnode,
1041                       pFcb->ObjectInformation->FileId.Unique,
1042                       StartingByte.QuadPart,
1043                       ByteCount));
1044
1045         ntStatus = AFSRequestExtentsAsync( pFcb,
1046                                            pCcb,
1047                                            &StartingByte,
1048                                            ByteCount);
1049
1050         if (!NT_SUCCESS(ntStatus))
1051         {
1052
1053             AFSDbgTrace(( AFS_SUBSYSTEM_IO_PROCESSING,
1054                           AFS_TRACE_LEVEL_ERROR,
1055                           "AFSNonCachedWrite (%p) Failed to request extents Status %08lX\n",
1056                           Irp,
1057                           ntStatus));
1058
1059             try_return( ntStatus);
1060         }
1061
1062         KeQueryTickCount( &liLastRequestTime);
1063
1064         while (TRUE)
1065         {
1066
1067             AFSDbgTrace(( AFS_SUBSYSTEM_LOCK_PROCESSING,
1068                           AFS_TRACE_LEVEL_VERBOSE,
1069                           "AFSNonCachedWrite Acquiring Fcb extents lock %p SHARED %08lX\n",
1070                           &pFcb->NPFcb->Specific.File.ExtentsResource,
1071                           PsGetCurrentThread()));
1072
1073             ASSERT( !ExIsResourceAcquiredLite( &pFcb->NPFcb->Specific.File.ExtentsResource ));
1074
1075             AFSAcquireShared( &pFcb->NPFcb->Specific.File.ExtentsResource, TRUE );
1076             bLocked = TRUE;
1077
1078             pStartExtent = NULL;
1079             pIgnoreExtent = NULL;
1080
1081             if ( AFSDoExtentsMapRegion( pFcb, &StartingByte, ByteCount, &pStartExtent, &pIgnoreExtent ))
1082             {
1083                 break;
1084             }
1085
1086             KeClearEvent( &pFcb->NPFcb->Specific.File.ExtentsRequestComplete );
1087
1088             AFSDbgTrace(( AFS_SUBSYSTEM_LOCK_PROCESSING,
1089                           AFS_TRACE_LEVEL_VERBOSE,
1090                           "AFSNonCachedWrite Releasing(1) Fcb extents lock %p SHARED %08lX\n",
1091                           &pFcb->NPFcb->Specific.File.ExtentsResource,
1092                           PsGetCurrentThread()));
1093
1094             AFSReleaseResource( &pFcb->NPFcb->Specific.File.ExtentsResource );
1095             bLocked= FALSE;
1096
1097             //
1098             // We will re-request the extents after waiting for them
1099             //
1100
1101             KeQueryTickCount( &liCurrentTime);
1102
1103             if( liCurrentTime.QuadPart - liLastRequestTime.QuadPart >= pControlDevExt->Specific.Control.ExtentRequestTimeCount.QuadPart)
1104             {
1105
1106                 AFSDbgTrace(( AFS_SUBSYSTEM_EXTENT_PROCESSING,
1107                               AFS_TRACE_LEVEL_VERBOSE,
1108                               "AFSNonCachedWrite Requesting extents, again, for fid %08lX-%08lX-%08lX-%08lX Offset %0I64X Length %08lX\n",
1109                               pFcb->ObjectInformation->FileId.Cell,
1110                               pFcb->ObjectInformation->FileId.Volume,
1111                               pFcb->ObjectInformation->FileId.Vnode,
1112                               pFcb->ObjectInformation->FileId.Unique,
1113                               StartingByte.QuadPart,
1114                               ByteCount));
1115
1116                 ntStatus = AFSRequestExtentsAsync( pFcb,
1117                                                    pCcb,
1118                                                    &StartingByte,
1119                                                    ByteCount);
1120
1121                 if (!NT_SUCCESS(ntStatus))
1122                 {
1123
1124                     AFSDbgTrace(( AFS_SUBSYSTEM_IO_PROCESSING,
1125                                   AFS_TRACE_LEVEL_ERROR,
1126                                   "AFSNonCachedWrite (%p) Failed to request extents Status %08lX\n",
1127                                   Irp,
1128                                   ntStatus));
1129
1130                     try_return( ntStatus);
1131                 }
1132
1133                 KeQueryTickCount( &liLastRequestTime);
1134             }
1135
1136
1137             AFSDbgTrace(( AFS_SUBSYSTEM_EXTENT_PROCESSING,
1138                           AFS_TRACE_LEVEL_VERBOSE,
1139                           "AFSNonCachedWrite Waiting for extents for fid %08lX-%08lX-%08lX-%08lX Offset %0I64X Length %08lX\n",
1140                           pFcb->ObjectInformation->FileId.Cell,
1141                           pFcb->ObjectInformation->FileId.Volume,
1142                           pFcb->ObjectInformation->FileId.Vnode,
1143                           pFcb->ObjectInformation->FileId.Unique,
1144                           StartingByte.QuadPart,
1145                           ByteCount));
1146
1147             //
1148             // Wait for it
1149             //
1150
1151             ntStatus =  AFSWaitForExtentMapping ( pFcb, pCcb);
1152
1153             if (!NT_SUCCESS(ntStatus))
1154             {
1155
1156                 AFSDbgTrace(( AFS_SUBSYSTEM_EXTENT_PROCESSING,
1157                               AFS_TRACE_LEVEL_ERROR,
1158                               "AFSNonCachedWrite Failed wait for extents for fid %08lX-%08lX-%08lX-%08lX Offset %0I64X Length %08lX Status %08lX\n",
1159                               pFcb->ObjectInformation->FileId.Cell,
1160                               pFcb->ObjectInformation->FileId.Volume,
1161                               pFcb->ObjectInformation->FileId.Vnode,
1162                               pFcb->ObjectInformation->FileId.Unique,
1163                               StartingByte.QuadPart,
1164                               ByteCount,
1165                               ntStatus));
1166
1167                 try_return( ntStatus);
1168             }
1169         }
1170
1171         //
1172         // As per the read path -
1173         //
1174
1175         AFSDbgTrace(( AFS_SUBSYSTEM_EXTENT_PROCESSING,
1176                       AFS_TRACE_LEVEL_VERBOSE,
1177                       "AFSNonCachedWrite Extents located for fid %08lX-%08lX-%08lX-%08lX Offset %0I64X Length %08lX\n",
1178                       pFcb->ObjectInformation->FileId.Cell,
1179                       pFcb->ObjectInformation->FileId.Volume,
1180                       pFcb->ObjectInformation->FileId.Vnode,
1181                       pFcb->ObjectInformation->FileId.Unique,
1182                       StartingByte.QuadPart,
1183                       ByteCount));
1184
1185         ntStatus = AFSGetExtents( pFcb,
1186                                   &StartingByte,
1187                                   ByteCount,
1188                                   pStartExtent,
1189                                   &extentsCount,
1190                                   &runCount);
1191
1192         if (!NT_SUCCESS(ntStatus))
1193         {
1194
1195             AFSDbgTrace(( AFS_SUBSYSTEM_IO_PROCESSING,
1196                           AFS_TRACE_LEVEL_ERROR,
1197                           "AFSNonCachedWrite (%p) Failed to retrieve mapped extents Status %08lX\n",
1198                           Irp,
1199                           ntStatus));
1200
1201             try_return( ntStatus );
1202         }
1203
1204         AFSDbgTrace(( AFS_SUBSYSTEM_IO_PROCESSING,
1205                       AFS_TRACE_LEVEL_VERBOSE,
1206                       "AFSNonCachedWrite (%p) Successfully retrieved map extents count %d run count %d\n",
1207                       Irp,
1208                       extentsCount,
1209                       runCount));
1210
1211         if( BooleanFlagOn( AFSLibControlFlags, AFS_REDIR_LIB_FLAGS_NONPERSISTENT_CACHE))
1212         {
1213
1214             Irp->IoStatus.Information = ByteCount;
1215
1216 #if GEN_MD5
1217             //
1218             // Setup the MD5 for each extent
1219             //
1220
1221             AFSSetupMD5Hash( pFcb,
1222                              pStartExtent,
1223                              extentsCount,
1224                              pSystemBuffer,
1225                              &StartingByte,
1226                              ByteCount);
1227 #endif
1228
1229             ntStatus = AFSProcessExtentRun( pSystemBuffer,
1230                                             &StartingByte,
1231                                             ByteCount,
1232                                             pStartExtent,
1233                                             TRUE);
1234
1235             if (!NT_SUCCESS(ntStatus))
1236             {
1237
1238                 AFSDbgTrace(( AFS_SUBSYSTEM_IO_PROCESSING,
1239                               AFS_TRACE_LEVEL_ERROR,
1240                               "AFSNonCachedWrite (%p) Failed to process extent run for non-persistent cache Status %08lX\n",
1241                               Irp,
1242                               ntStatus));
1243             }
1244
1245             try_return( ntStatus);
1246         }
1247
1248         //
1249         // Retrieve the cache file object
1250         //
1251
1252         pCacheFileObject = AFSReferenceCacheFileObject();
1253
1254         if( pCacheFileObject == NULL)
1255         {
1256
1257             ntStatus = STATUS_DEVICE_NOT_READY;
1258
1259             AFSDbgTrace(( AFS_SUBSYSTEM_EXTENT_PROCESSING,
1260                           AFS_TRACE_LEVEL_ERROR,
1261                           "AFSNonCachedWrite Failed to retrieve cache fileobject for fid %08lX-%08lX-%08lX-%08lX Offset %0I64X Length %08lX Status %08lX\n",
1262                           pFcb->ObjectInformation->FileId.Cell,
1263                           pFcb->ObjectInformation->FileId.Volume,
1264                           pFcb->ObjectInformation->FileId.Vnode,
1265                           pFcb->ObjectInformation->FileId.Unique,
1266                           StartingByte.QuadPart,
1267                           ByteCount,
1268                           ntStatus));
1269
1270             try_return( ntStatus);
1271         }
1272
1273         if (runCount > AFS_MAX_STACK_IO_RUNS)
1274         {
1275
1276             pIoRuns = (AFSIoRun*) AFSExAllocatePoolWithTag( PagedPool,
1277                                                             runCount * sizeof( AFSIoRun ),
1278                                                             AFS_IO_RUN_TAG );
1279             if (NULL == pIoRuns)
1280             {
1281
1282                 AFSDbgTrace(( AFS_SUBSYSTEM_IO_PROCESSING,
1283                               AFS_TRACE_LEVEL_ERROR,
1284                               "AFSNonCachedWrite (%p) Failed to allocate IO run block\n",
1285                               Irp));
1286
1287                 try_return( ntStatus = STATUS_INSUFFICIENT_RESOURCES );
1288             }
1289         }
1290         else
1291         {
1292
1293             pIoRuns = stIoRuns;
1294         }
1295
1296         RtlZeroMemory( pIoRuns, runCount * sizeof( AFSIoRun ));
1297
1298         ntStatus = AFSSetupIoRun( IoGetRelatedDeviceObject( pCacheFileObject),
1299                                   Irp,
1300                                   pSystemBuffer,
1301                                   pIoRuns,
1302                                   &StartingByte,
1303                                   ByteCount,
1304                                   pStartExtent,
1305                                   &runCount );
1306
1307         if (!NT_SUCCESS(ntStatus))
1308         {
1309
1310             AFSDbgTrace(( AFS_SUBSYSTEM_IO_PROCESSING,
1311                           AFS_TRACE_LEVEL_ERROR,
1312                           "AFSNonCachedWrite (%p) Failed to initialize IO run block Status %08lX\n",
1313                           Irp,
1314                           ntStatus));
1315
1316             try_return( ntStatus );
1317         }
1318
1319         AFSReferenceActiveExtents( pStartExtent,
1320                                    extentsCount);
1321
1322         bDerefExtents = TRUE;
1323
1324         AFSDbgTrace(( AFS_SUBSYSTEM_LOCK_PROCESSING,
1325                       AFS_TRACE_LEVEL_VERBOSE,
1326                       "AFSNonCachedWrite Releasing(2) Fcb extents lock %p SHARED %08lX\n",
1327                       &pFcb->NPFcb->Specific.File.ExtentsResource,
1328                       PsGetCurrentThread()));
1329
1330         AFSReleaseResource( &pFcb->NPFcb->Specific.File.ExtentsResource );
1331         bLocked = FALSE;
1332
1333         pGatherIo = (AFSGatherIo*) AFSExAllocatePoolWithTag( NonPagedPool,
1334                                                              sizeof( AFSGatherIo),
1335                                                              AFS_GATHER_TAG);
1336
1337         if (NULL == pGatherIo)
1338         {
1339
1340             AFSDbgTrace(( AFS_SUBSYSTEM_IO_PROCESSING,
1341                           AFS_TRACE_LEVEL_ERROR,
1342                           "AFSNonCachedWrite (%p) Failed to allocate IO gather block\n",
1343                           Irp));
1344
1345             AFSDbgTrace(( AFS_SUBSYSTEM_LOCK_PROCESSING,
1346                           AFS_TRACE_LEVEL_VERBOSE,
1347                           "AFSNonCachedWrite Acquiring(1) Fcb extents lock %p SHARED %08lX\n",
1348                           &pFcb->NPFcb->Specific.File.ExtentsResource,
1349                           PsGetCurrentThread()));
1350
1351             AFSAcquireShared( &pFcb->NPFcb->Specific.File.ExtentsResource,
1352                               TRUE);
1353             bLocked = TRUE;
1354
1355             AFSDereferenceActiveExtents( pStartExtent,
1356                                          extentsCount);
1357
1358             try_return (ntStatus = STATUS_INSUFFICIENT_RESOURCES );
1359         }
1360
1361         RtlZeroMemory( pGatherIo, sizeof( AFSGatherIo ));
1362
1363         //
1364         // Initialize count to 1, that was we won't get an early
1365         // completion if the first irp completes before the second is
1366         // queued.
1367         //
1368         pGatherIo->Count = 1;
1369         pGatherIo->Status = STATUS_SUCCESS;
1370         pGatherIo->MasterIrp = Irp;
1371         pGatherIo->Synchronous = TRUE;
1372         pGatherIo->CompleteMasterIrp = FALSE;
1373
1374         bCompleteIrp = TRUE;
1375
1376         if( pGatherIo->Synchronous)
1377         {
1378             KeInitializeEvent( &pGatherIo->Event, NotificationEvent, FALSE );
1379         }
1380
1381 #if GEN_MD5
1382         //
1383         // Setup the MD5 for each extent
1384         //
1385
1386         AFSSetupMD5Hash( pFcb,
1387                          pStartExtent,
1388                          extentsCount,
1389                          pSystemBuffer,
1390                          &StartingByte,
1391                          ByteCount);
1392 #endif
1393
1394         //
1395         // Pre-emptively set up the count
1396         //
1397
1398         Irp->IoStatus.Information = ByteCount;
1399
1400         ntStatus = AFSQueueStartIos( pCacheFileObject,
1401                                      IRP_MJ_WRITE,
1402                                      IRP_WRITE_OPERATION | IRP_SYNCHRONOUS_API,
1403                                      pIoRuns,
1404                                      runCount,
1405                                      pGatherIo);
1406
1407         AFSDbgTrace(( AFS_SUBSYSTEM_IO_PROCESSING,
1408                       AFS_TRACE_LEVEL_VERBOSE,
1409                       "AFSNonCachedWrite (%p) AFSStartIos completed Status %08lX\n",
1410                       Irp,
1411                       ntStatus));
1412
1413         if( !NT_SUCCESS( ntStatus))
1414         {
1415
1416             AFSDbgTrace(( AFS_SUBSYSTEM_LOCK_PROCESSING,
1417                           AFS_TRACE_LEVEL_VERBOSE,
1418                           "AFSNonCachedWrite Acquiring(2) Fcb extents lock %p SHARED %08lX\n",
1419                           &pFcb->NPFcb->Specific.File.ExtentsResource,
1420                           PsGetCurrentThread()));
1421
1422             AFSAcquireShared( &pFcb->NPFcb->Specific.File.ExtentsResource,
1423                               TRUE);
1424             bLocked = TRUE;
1425
1426             AFSDereferenceActiveExtents( pStartExtent,
1427                                          extentsCount);
1428
1429             try_return( ntStatus);
1430         }
1431
1432         //
1433         // Wait for completion of All IOs we started.
1434         //
1435
1436         ntStatus = KeWaitForSingleObject( &pGatherIo->Event,
1437                                           Executive,
1438                                           KernelMode,
1439                                           FALSE,
1440                                           NULL);
1441
1442         if( NT_SUCCESS( ntStatus))
1443         {
1444
1445             ntStatus = pGatherIo->Status;
1446         }
1447
1448         if( !NT_SUCCESS( ntStatus))
1449         {
1450
1451             AFSDbgTrace(( AFS_SUBSYSTEM_LOCK_PROCESSING,
1452                           AFS_TRACE_LEVEL_VERBOSE,
1453                           "AFSNonCachedWrite Acquiring(3) Fcb extents lock %p SHARED %08lX\n",
1454                           &pFcb->NPFcb->Specific.File.ExtentsResource,
1455                           PsGetCurrentThread()));
1456
1457             AFSAcquireShared( &pFcb->NPFcb->Specific.File.ExtentsResource,
1458                               TRUE);
1459             bLocked = TRUE;
1460
1461             AFSDereferenceActiveExtents( pStartExtent,
1462                                          extentsCount);
1463
1464             try_return( ntStatus);
1465         }
1466
1467 try_exit:
1468
1469         if( NT_SUCCESS( ntStatus) &&
1470             pStartExtent != NULL &&
1471             Irp->IoStatus.Information > 0)
1472         {
1473
1474             if ( !bLocked)
1475             {
1476
1477                 AFSAcquireShared( &pFcb->NPFcb->Specific.File.ExtentsResource,
1478                                   TRUE);
1479                 bLocked = TRUE;
1480             }
1481
1482             //
1483             // Since this is dirty we can mark the extents dirty now.
1484             // AFSMarkDirty will dereference the extents.  Do not call
1485             // AFSDereferenceActiveExtents() in this code path.
1486             //
1487
1488             AFSMarkDirty( pFcb,
1489                           pStartExtent,
1490                           extentsCount,
1491                           &StartingByte,
1492                           bDerefExtents);
1493
1494             if (!bPagingIo)
1495             {
1496                 //
1497                 // This was an uncached user write - tell the server to do
1498                 // the flush when the worker thread next wakes up
1499                 //
1500                 pFcb->Specific.File.LastServerFlush.QuadPart = 0;
1501             }
1502         }
1503
1504         if( pCacheFileObject != NULL)
1505         {
1506             AFSReleaseCacheFileObject( pCacheFileObject);
1507         }
1508
1509         AFSDbgTrace(( AFS_SUBSYSTEM_IO_PROCESSING,
1510                       AFS_TRACE_LEVEL_VERBOSE,
1511                       "AFSNonCachedWrite (FO: %p) StartingByte %08lX:%08lX Length %08lX Status %08lX\n",
1512                       pFileObject,
1513                       StartingByte.HighPart,
1514                       StartingByte.LowPart,
1515                       ByteCount,
1516                       ntStatus));
1517
1518         if (NT_SUCCESS(ntStatus) &&
1519             !bPagingIo &&
1520             bSynchronousFo)
1521         {
1522
1523             pFileObject->CurrentByteOffset.QuadPart = StartingByte.QuadPart + ByteCount;
1524         }
1525
1526         if( bLocked)
1527         {
1528
1529             AFSDbgTrace(( AFS_SUBSYSTEM_LOCK_PROCESSING,
1530                           AFS_TRACE_LEVEL_VERBOSE,
1531                           "AFSNonCachedWrite Releasing Fcb extents lock %p SHARED %08lX\n",
1532                           &pFcb->NPFcb->Specific.File.ExtentsResource,
1533                           PsGetCurrentThread()));
1534
1535             AFSReleaseResource( &pFcb->NPFcb->Specific.File.ExtentsResource );
1536         }
1537
1538         if( pGatherIo)
1539         {
1540             AFSExFreePoolWithTag(pGatherIo, AFS_GATHER_TAG);
1541         }
1542
1543         if( NULL != pIoRuns &&
1544             stIoRuns != pIoRuns)
1545         {
1546             AFSExFreePoolWithTag(pIoRuns, AFS_IO_RUN_TAG);
1547         }
1548
1549         if( bCompleteIrp)
1550         {
1551
1552             AFSDbgTrace(( AFS_SUBSYSTEM_IO_PROCESSING,
1553                           AFS_TRACE_LEVEL_VERBOSE,
1554                           "AFSNonCachedWrite Completing Irp %p Status %08lX Info %08lX\n",
1555                           Irp,
1556                           ntStatus,
1557                           Irp->IoStatus.Information));
1558
1559             AFSCompleteRequest( Irp, ntStatus);
1560         }
1561     }
1562
1563     return ntStatus;
1564 }
1565
1566 static
1567 NTSTATUS
1568 AFSNonCachedWriteDirect( IN PDEVICE_OBJECT DeviceObject,
1569                          IN PIRP Irp,
1570                          IN LARGE_INTEGER StartingByte,
1571                          IN ULONG ByteCount)
1572 {
1573     NTSTATUS           ntStatus = STATUS_UNSUCCESSFUL;
1574     VOID              *pSystemBuffer = NULL;
1575     BOOLEAN            bPagingIo = BooleanFlagOn( Irp->Flags, IRP_PAGING_IO);
1576     IO_STACK_LOCATION *pIrpSp = IoGetCurrentIrpStackLocation( Irp);
1577     PFILE_OBJECT       pFileObject = pIrpSp->FileObject;
1578     AFSFcb            *pFcb = (AFSFcb *)pFileObject->FsContext;
1579     AFSCcb            *pCcb = (AFSCcb *)pFileObject->FsContext2;
1580     BOOLEAN            bSynchronousFo = BooleanFlagOn( pFileObject->Flags, FO_SYNCHRONOUS_IO);
1581     BOOLEAN            bNoIntermediateBuffering = BooleanFlagOn( pFileObject->Flags, FO_NO_INTERMEDIATE_BUFFERING);
1582     AFSDeviceExt      *pDevExt = (AFSDeviceExt *)DeviceObject->DeviceExtension;
1583     AFSFileIOCB        stFileIORequest;
1584     AFSFileIOResultCB  stFileIOResult;
1585     ULONG              ulResultLen = 0;
1586     ULONG              ulFlags;
1587
1588     __Enter
1589     {
1590         Irp->IoStatus.Information = 0;
1591
1592         AFSDbgTrace(( AFS_SUBSYSTEM_IO_PROCESSING,
1593                       AFS_TRACE_LEVEL_VERBOSE,
1594                       "AFSNonCachedWriteDirect (FO: %p) StartingByte %08lX:%08lX Length %08lX\n",
1595                       pFileObject,
1596                       StartingByte.HighPart,
1597                       StartingByte.LowPart,
1598                       ByteCount));
1599
1600         if (ByteCount > pDevExt->Specific.RDR.MaxIo.QuadPart)
1601         {
1602
1603             AFSDbgTrace(( AFS_SUBSYSTEM_IO_PROCESSING,
1604                           AFS_TRACE_LEVEL_ERROR,
1605                           "AFSNonCachedWriteDirect (%p) Request %08lX Actual %08lX larger than MaxIO %I64X\n",
1606                           Irp,
1607                           ByteCount,
1608                           pIrpSp->Parameters.Write.Length,
1609                           pDevExt->Specific.RDR.MaxIo.QuadPart));
1610
1611             try_return( ntStatus = STATUS_UNSUCCESSFUL);
1612         }
1613
1614         //
1615         // Get the mapping for the buffer
1616         //
1617         pSystemBuffer = AFSLockSystemBuffer( Irp,
1618                                              ByteCount);
1619
1620         if( pSystemBuffer == NULL)
1621         {
1622
1623             AFSDbgTrace(( AFS_SUBSYSTEM_IO_PROCESSING,
1624                           AFS_TRACE_LEVEL_ERROR,
1625                           "AFSNonCachedWriteDirect (%p) Failed to map system buffer\n",
1626                           Irp));
1627
1628             try_return( ntStatus = STATUS_INSUFFICIENT_RESOURCES);
1629         }
1630
1631         //
1632         // Issue the request at the service for processing
1633         //
1634
1635         ulResultLen = sizeof( AFSFileIOResultCB);
1636
1637         RtlZeroMemory( &stFileIORequest,
1638                        sizeof( AFSFileIOCB));
1639
1640         RtlZeroMemory( &stFileIOResult,
1641                        sizeof( AFSFileIOResultCB));
1642
1643         stFileIORequest.SystemIOBuffer = pSystemBuffer;
1644
1645         stFileIORequest.SystemIOBufferMdl = Irp->MdlAddress;
1646
1647         stFileIORequest.IOLength = ByteCount;
1648
1649         stFileIORequest.IOOffset = StartingByte;
1650
1651         ulFlags = AFS_REQUEST_FLAG_SYNCHRONOUS;
1652
1653         if ( bNoIntermediateBuffering)
1654         {
1655
1656             ulFlags |= AFS_REQUEST_FLAG_CACHE_BYPASS;
1657         }
1658
1659         //
1660         // Update file metadata
1661         //
1662
1663         stFileIORequest.EndOfFile = pFcb->ObjectInformation->EndOfFile;
1664
1665         stFileIORequest.CreateTime = pFcb->ObjectInformation->CreationTime;
1666
1667         stFileIORequest.ChangeTime = pFcb->ObjectInformation->ChangeTime;
1668
1669         stFileIORequest.LastAccessTime = pFcb->ObjectInformation->LastAccessTime;
1670
1671         stFileIORequest.LastWriteTime = pFcb->ObjectInformation->LastWriteTime;
1672
1673         //
1674         // Write the data to the service
1675         //
1676
1677         ntStatus = AFSProcessRequest( AFS_REQUEST_TYPE_PROCESS_WRITE_FILE,
1678                                       ulFlags,
1679                                       &pCcb->AuthGroup,
1680                                       &pCcb->DirectoryCB->NameInformation.FileName,
1681                                       &pFcb->ObjectInformation->FileId,
1682                                       pFcb->ObjectInformation->VolumeCB->VolumeInformation.Cell,
1683                                       pFcb->ObjectInformation->VolumeCB->VolumeInformation.CellLength,
1684                                       &stFileIORequest,
1685                                       sizeof( AFSFileIOCB),
1686                                       &stFileIOResult,
1687                                       &ulResultLen);
1688
1689         if( NT_SUCCESS( ntStatus))
1690         {
1691
1692             Irp->IoStatus.Information = (ULONG_PTR)stFileIOResult.Length;
1693         }
1694         else
1695         {
1696
1697             AFSDbgTrace(( AFS_SUBSYSTEM_IO_PROCESSING,
1698                           AFS_TRACE_LEVEL_ERROR,
1699                           "AFSNonCachedWriteDirect (%p) Failed to send write to service Status %08lX\n",
1700                           Irp,
1701                           ntStatus));
1702         }
1703
1704 try_exit:
1705
1706         AFSDbgTrace(( AFS_SUBSYSTEM_IO_PROCESSING,
1707                       AFS_TRACE_LEVEL_VERBOSE,
1708                       "AFSNonCachedWriteDirect (FO: %p) StartingByte %08lX:%08lX Length %08lX Status %08lX\n",
1709                       pFileObject,
1710                       StartingByte.HighPart,
1711                       StartingByte.LowPart,
1712                       ByteCount,
1713                       ntStatus));
1714
1715         if (NT_SUCCESS(ntStatus) &&
1716             !bPagingIo &&
1717             bSynchronousFo)
1718         {
1719
1720             pFileObject->CurrentByteOffset.QuadPart = StartingByte.QuadPart + ByteCount;
1721         }
1722
1723         AFSDbgTrace(( AFS_SUBSYSTEM_IO_PROCESSING,
1724                       AFS_TRACE_LEVEL_VERBOSE,
1725                       "AFSNonCachedWriteDirect Completing Irp %p Status %08lX Info %08lX\n",
1726                       Irp,
1727                       ntStatus,
1728                       Irp->IoStatus.Information));
1729
1730         AFSCompleteRequest( Irp, ntStatus);
1731     }
1732
1733     return ntStatus;
1734 }
1735
1736 static
1737 NTSTATUS
1738 AFSCachedWrite( IN PDEVICE_OBJECT DeviceObject,
1739                 IN PIRP Irp,
1740                 IN LARGE_INTEGER StartingByte,
1741                 IN ULONG ByteCount,
1742                 IN BOOLEAN ForceFlush)
1743 {
1744     UNREFERENCED_PARAMETER(DeviceObject);
1745     PVOID              pSystemBuffer = NULL;
1746     NTSTATUS           ntStatus = STATUS_SUCCESS;
1747     IO_STATUS_BLOCK    iosbFlush;
1748     IO_STACK_LOCATION *pIrpSp = IoGetCurrentIrpStackLocation( Irp);
1749     PFILE_OBJECT       pFileObject = pIrpSp->FileObject;
1750     AFSFcb            *pFcb = (AFSFcb *)pFileObject->FsContext;
1751     BOOLEAN            bSynchronousFo = BooleanFlagOn( pFileObject->Flags, FO_SYNCHRONOUS_IO);
1752     ULONG              ulCurrentIO = 0, ulTotalLen = ByteCount;
1753     PMDL               pCurrentMdl = Irp->MdlAddress;
1754     LARGE_INTEGER      liCurrentOffset;
1755
1756     __Enter
1757     {
1758
1759         Irp->IoStatus.Information = 0;
1760
1761         if( BooleanFlagOn( pIrpSp->MinorFunction, IRP_MN_MDL))
1762         {
1763
1764             __try
1765             {
1766
1767                 CcPrepareMdlWrite( pFileObject,
1768                                    &StartingByte,
1769                                    ByteCount,
1770                                    &Irp->MdlAddress,
1771                                    &Irp->IoStatus);
1772
1773                 ntStatus = Irp->IoStatus.Status;
1774             }
1775             __except( EXCEPTION_EXECUTE_HANDLER)
1776             {
1777                 ntStatus = GetExceptionCode();
1778
1779                 AFSDbgTrace(( AFS_SUBSYSTEM_IO_PROCESSING,
1780                               AFS_TRACE_LEVEL_ERROR,
1781                               "AFSCachedWrite (%p) Exception thrown while preparing mdl write Status %08lX\n",
1782                               Irp,
1783                               ntStatus));
1784             }
1785
1786             if( !NT_SUCCESS( ntStatus))
1787             {
1788
1789                 //
1790                 // Free up any potentially allocated mdl's
1791                 //
1792
1793                 CcMdlWriteComplete( pFileObject,
1794                                     &StartingByte,
1795                                     Irp->MdlAddress);
1796
1797                 Irp->MdlAddress = NULL;
1798
1799                 AFSDbgTrace(( AFS_SUBSYSTEM_IO_PROCESSING,
1800                               AFS_TRACE_LEVEL_ERROR,
1801                               "AFSCachedWrite (%p) Failed to process MDL write Status %08lX\n",
1802                               Irp,
1803                               ntStatus));
1804             }
1805
1806             try_return( ntStatus);
1807         }
1808
1809         liCurrentOffset.QuadPart = StartingByte.QuadPart;
1810
1811         while( ulTotalLen > 0)
1812         {
1813
1814             ntStatus = STATUS_SUCCESS;
1815
1816             if( pCurrentMdl != NULL)
1817             {
1818
1819                 pSystemBuffer = MmGetSystemAddressForMdlSafe( pCurrentMdl,
1820                                                               NormalPagePriority);
1821
1822                 ulCurrentIO = MmGetMdlByteCount( pCurrentMdl);
1823
1824                 if( ulCurrentIO > ulTotalLen)
1825                 {
1826                     ulCurrentIO = ulTotalLen;
1827                 }
1828             }
1829             else
1830             {
1831
1832                 pSystemBuffer = AFSLockSystemBuffer( Irp,
1833                                                      ulTotalLen);
1834
1835                 ulCurrentIO = ulTotalLen;
1836             }
1837
1838             if( pSystemBuffer == NULL)
1839             {
1840
1841                 AFSDbgTrace(( AFS_SUBSYSTEM_IO_PROCESSING,
1842                               AFS_TRACE_LEVEL_ERROR,
1843                               "AFSCachedWrite (%p) Failed to lock system buffer\n",
1844                               Irp));
1845
1846                 try_return( ntStatus = STATUS_INSUFFICIENT_RESOURCES);
1847             }
1848
1849             __try
1850             {
1851
1852                 if( !CcCopyWrite( pFileObject,
1853                                   &liCurrentOffset,
1854                                   ulCurrentIO,
1855                                   TRUE,
1856                                   pSystemBuffer))
1857                 {
1858                     //
1859                     // Failed to process request.
1860                     //
1861
1862                     AFSDbgTrace(( AFS_SUBSYSTEM_FILE_PROCESSING,
1863                                   AFS_TRACE_LEVEL_ERROR,
1864                                   "AFSCachedWrite (%p) Failed to issue CcCopyWrite %wZ @ %0I64X Status %08lX\n",
1865                                   Irp,
1866                                   &pFileObject->FileName,
1867                                   liCurrentOffset.QuadPart,
1868                                   Irp->IoStatus.Status));
1869
1870                     try_return( ntStatus = STATUS_UNSUCCESSFUL);
1871                 }
1872             }
1873             __except( EXCEPTION_EXECUTE_HANDLER)
1874             {
1875
1876                 ntStatus = GetExceptionCode();
1877
1878                 AFSDbgTrace(( AFS_SUBSYSTEM_IO_PROCESSING,
1879                               AFS_TRACE_LEVEL_ERROR,
1880                               "AFSCachedWrite (%p) CcCopyWrite Threw exception %wZ @ %0I64X Status %08lX\n",
1881                               Irp,
1882                               &pFileObject->FileName,
1883                               liCurrentOffset.QuadPart,
1884                               ntStatus));
1885             }
1886
1887             if( !NT_SUCCESS( ntStatus))
1888             {
1889                 try_return( ntStatus);
1890             }
1891
1892             if( ForceFlush ||
1893                 BooleanFlagOn(pFileObject->Flags, (FO_NO_INTERMEDIATE_BUFFERING + FO_WRITE_THROUGH)))
1894             {
1895
1896                 //
1897                 // We have detected a file we do a write through with.
1898                 //
1899
1900                 CcFlushCache(&pFcb->NPFcb->SectionObjectPointers,
1901                              &liCurrentOffset,
1902                              ulCurrentIO,
1903                              &iosbFlush);
1904
1905                 if( !NT_SUCCESS( iosbFlush.Status))
1906                 {
1907
1908                     AFSDbgTrace(( AFS_SUBSYSTEM_IO_PROCESSING,
1909                                   AFS_TRACE_LEVEL_ERROR,
1910                                   "AFSCachedWrite (%p) CcFlushCache failure %wZ FID %08lX-%08lX-%08lX-%08lX Status 0x%08lX Bytes 0x%08lX\n",
1911                                   Irp,
1912                                   &pFileObject->FileName,
1913                                   pFcb->ObjectInformation->FileId.Cell,
1914                                   pFcb->ObjectInformation->FileId.Volume,
1915                                   pFcb->ObjectInformation->FileId.Vnode,
1916                                   pFcb->ObjectInformation->FileId.Unique,
1917                                   iosbFlush.Status,
1918                                   iosbFlush.Information));
1919
1920                     try_return( ntStatus = iosbFlush.Status);
1921                 }
1922             }
1923
1924             if( ulTotalLen <= ulCurrentIO)
1925             {
1926                 break;
1927             }
1928
1929             liCurrentOffset.QuadPart += ulCurrentIO;
1930
1931             ulTotalLen -= ulCurrentIO;
1932
1933             pCurrentMdl = pCurrentMdl->Next;
1934         }
1935
1936 try_exit:
1937
1938         if( NT_SUCCESS( ntStatus))
1939         {
1940
1941             Irp->IoStatus.Information = ByteCount;
1942
1943             if ( ForceFlush ||
1944                  BooleanFlagOn(pFileObject->Flags, (FO_NO_INTERMEDIATE_BUFFERING + FO_WRITE_THROUGH)))
1945             {
1946                 //
1947                 // Write through asked for... Set things so that we get
1948                 // flush when the worker thread next wakes up
1949                 //
1950                 pFcb->Specific.File.LastServerFlush.QuadPart = 0;
1951             }
1952         }
1953
1954         AFSCompleteRequest( Irp,
1955                             ntStatus);
1956     }
1957
1958     return ntStatus;
1959 }
1960
1961 static
1962 NTSTATUS
1963 AFSExtendingWrite( IN AFSFcb *Fcb,
1964                    IN PFILE_OBJECT FileObject,
1965                    IN LONGLONG NewLength)
1966 {
1967     LARGE_INTEGER liSaveFileSize = Fcb->Header.FileSize;
1968     LARGE_INTEGER liSaveAllocation = Fcb->Header.AllocationSize;
1969     NTSTATUS      ntStatus = STATUS_SUCCESS;
1970     AFSCcb       *pCcb = (AFSCcb *)FileObject->FsContext2;
1971
1972     if( NewLength > Fcb->Header.AllocationSize.QuadPart)
1973     {
1974
1975         Fcb->Header.AllocationSize.QuadPart = NewLength;
1976
1977         Fcb->ObjectInformation->AllocationSize = Fcb->Header.AllocationSize;
1978     }
1979
1980     if( NewLength > Fcb->Header.FileSize.QuadPart)
1981     {
1982
1983         Fcb->Header.FileSize.QuadPart = NewLength;
1984
1985         Fcb->ObjectInformation->EndOfFile = Fcb->Header.FileSize;
1986     }
1987
1988     //
1989     // Tell the server
1990     //
1991
1992     ntStatus = AFSUpdateFileInformation( &Fcb->ObjectInformation->ParentFileId,
1993                                          Fcb->ObjectInformation,
1994                                          &pCcb->AuthGroup);
1995
1996     if (NT_SUCCESS(ntStatus))
1997     {
1998
1999         KeQuerySystemTime( &Fcb->ObjectInformation->ChangeTime);
2000
2001         SetFlag( Fcb->Flags, AFS_FCB_FLAG_FILE_MODIFIED | AFS_FCB_FLAG_UPDATE_CHANGE_TIME);
2002
2003         //
2004         // If the file is currently cached, then let the MM know about the extension
2005         //
2006
2007         if( CcIsFileCached( FileObject))
2008         {
2009             CcSetFileSizes( FileObject,
2010                             (PCC_FILE_SIZES)&Fcb->Header.AllocationSize);
2011         }
2012     }
2013     else
2014     {
2015         Fcb->Header.FileSize = liSaveFileSize;
2016         Fcb->Header.AllocationSize = liSaveAllocation;
2017     }
2018
2019     //
2020     // DownConvert file resource to shared
2021     //
2022     ExConvertExclusiveToSharedLite( &Fcb->NPFcb->Resource);
2023
2024     return ntStatus;
2025 }
2026
2027 NTSTATUS
2028 AFSShareWrite( IN PDEVICE_OBJECT DeviceObject,
2029                IN PIRP Irp)
2030 {
2031
2032     UNREFERENCED_PARAMETER(DeviceObject);
2033     NTSTATUS ntStatus = STATUS_SUCCESS;
2034     PIO_STACK_LOCATION pIrpSp = IoGetCurrentIrpStackLocation( Irp);
2035     AFSFcb *pFcb = NULL;
2036     AFSCcb *pCcb = NULL;
2037     AFSPipeIORequestCB *pIoRequest = NULL;
2038     void *pBuffer = NULL;
2039     AFSPipeIOResultCB stIoResult;
2040     ULONG ulBytesReturned = 0;
2041
2042     __Enter
2043     {
2044
2045         pFcb = (AFSFcb *)pIrpSp->FileObject->FsContext;
2046
2047         pCcb = (AFSCcb *)pIrpSp->FileObject->FsContext2;
2048
2049         AFSDbgTrace(( AFS_SUBSYSTEM_PIPE_PROCESSING,
2050                       AFS_TRACE_LEVEL_VERBOSE,
2051                       "AFSShareWrite On pipe %wZ Length %08lX\n",
2052                       &pCcb->DirectoryCB->NameInformation.FileName,
2053                       pIrpSp->Parameters.Write.Length));
2054
2055         if( pIrpSp->Parameters.Write.Length == 0)
2056         {
2057
2058             //
2059             // Nothing to do in this case
2060             //
2061
2062             try_return( ntStatus);
2063         }
2064
2065         //
2066         // Retrieve the buffer for the read request
2067         //
2068
2069         pBuffer = AFSLockSystemBuffer( Irp,
2070                                        pIrpSp->Parameters.Write.Length);
2071
2072         if( pBuffer == NULL)
2073         {
2074
2075             AFSDbgTrace(( AFS_SUBSYSTEM_PIPE_PROCESSING,
2076                           AFS_TRACE_LEVEL_ERROR,
2077                           "AFSShareWrite Failed to map buffer on pipe %wZ\n",
2078                           &pCcb->DirectoryCB->NameInformation.FileName));
2079
2080             try_return( ntStatus = STATUS_INSUFFICIENT_RESOURCES);
2081         }
2082
2083         AFSAcquireShared( &pFcb->NPFcb->Resource,
2084                           TRUE);
2085
2086         pIoRequest = (AFSPipeIORequestCB *)AFSExAllocatePoolWithTag( PagedPool,
2087                                                                      sizeof( AFSPipeIORequestCB) +
2088                                                                                 pIrpSp->Parameters.Write.Length,
2089                                                                      AFS_GENERIC_MEMORY_14_TAG);
2090
2091         if( pIoRequest == NULL)
2092         {
2093
2094             try_return( ntStatus = STATUS_INSUFFICIENT_RESOURCES);
2095         }
2096
2097         RtlZeroMemory( pIoRequest,
2098                        sizeof( AFSPipeIORequestCB) + pIrpSp->Parameters.Write.Length);
2099
2100         pIoRequest->RequestId = pCcb->RequestID;
2101
2102         pIoRequest->RootId = pFcb->ObjectInformation->VolumeCB->ObjectInformation.FileId;
2103
2104         pIoRequest->BufferLength = pIrpSp->Parameters.Write.Length;
2105
2106         RtlCopyMemory( (void *)((char *)pIoRequest + sizeof( AFSPipeIORequestCB)),
2107                        pBuffer,
2108                        pIrpSp->Parameters.Write.Length);
2109
2110         stIoResult.BytesProcessed = 0;
2111
2112         ulBytesReturned = sizeof( AFSPipeIOResultCB);
2113
2114         //
2115         // Issue the open request to the service
2116         //
2117
2118         ntStatus = AFSProcessRequest( AFS_REQUEST_TYPE_PIPE_WRITE,
2119                                       AFS_REQUEST_FLAG_SYNCHRONOUS,
2120                                       &pCcb->AuthGroup,
2121                                       &pCcb->DirectoryCB->NameInformation.FileName,
2122                                       NULL,
2123                                       NULL,
2124                                       0,
2125                                       pIoRequest,
2126                                       sizeof( AFSPipeIORequestCB) +
2127                                                 pIrpSp->Parameters.Write.Length,
2128                                       &stIoResult,
2129                                       &ulBytesReturned);
2130
2131         if( !NT_SUCCESS( ntStatus))
2132         {
2133
2134             AFSDbgTrace(( AFS_SUBSYSTEM_FILE_PROCESSING,
2135                           AFS_TRACE_LEVEL_ERROR,
2136                           "AFSShareWrite (%p) Failed service write Status %08lX\n",
2137                           Irp,
2138                           ntStatus));
2139
2140             try_return( ntStatus);
2141         }
2142
2143         AFSDbgTrace(( AFS_SUBSYSTEM_PIPE_PROCESSING,
2144                       AFS_TRACE_LEVEL_VERBOSE,
2145                       "AFSShareWrite Completed on pipe %wZ Length read %08lX\n",
2146                       &pCcb->DirectoryCB->NameInformation.FileName,
2147                       stIoResult.BytesProcessed));
2148
2149         Irp->IoStatus.Information = stIoResult.BytesProcessed;
2150
2151 try_exit:
2152
2153         if( pFcb != NULL)
2154         {
2155
2156             AFSReleaseResource( &pFcb->NPFcb->Resource);
2157         }
2158
2159         if( pIoRequest != NULL)
2160         {
2161
2162             AFSExFreePoolWithTag( pIoRequest, AFS_GENERIC_MEMORY_14_TAG);
2163         }
2164     }
2165
2166     return ntStatus;
2167 }