744ef0aa09ddf9626eab54c9e8ba48fc976fbfdb
[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                         AFSReleaseResource( &pNPFcb->Resource);
601
602                         bReleaseMain = FALSE;
603                     }
604                 }
605             }
606         }
607         while (!bLockOK);
608
609         if( !bPagingIo)
610         {
611
612             //
613             // Check the BR locks on the file.
614             //
615
616             if ( !FsRtlCheckLockForWriteAccess( &pFcb->Specific.File.FileLock,
617                                                 Irp))
618             {
619
620                 AFSDbgTrace(( AFS_SUBSYSTEM_IO_PROCESSING,
621                               AFS_TRACE_LEVEL_ERROR,
622                               "AFSCommonWrite (%p) Request failed due to lock conflict\n",
623                               Irp));
624
625                 try_return( ntStatus = STATUS_FILE_LOCK_CONFLICT);
626             }
627
628             if( bExtendingWrite)
629             {
630
631                 ntStatus = AFSExtendingWrite( pFcb, pFileObject, (liStartingByte.QuadPart + ulByteCount));
632
633                 if( !NT_SUCCESS(ntStatus))
634                 {
635
636                     AFSDbgTrace(( AFS_SUBSYSTEM_IO_PROCESSING,
637                                   AFS_TRACE_LEVEL_ERROR,
638                                   "AFSCommonWrite (%p) Failed extending write request Status %08lX\n",
639                                   Irp,
640                                   ntStatus));
641
642                     try_return( ntStatus );
643                 }
644             }
645         }
646
647         //
648         // Fire off the request as appropriate
649         //
650         bCompleteIrp = FALSE;
651
652         if( !bPagingIo &&
653             !bNonCachedIo)
654         {
655
656             //
657             // Main and SectionObject resources held Shared
658             //
659
660             AFSDbgTrace(( AFS_SUBSYSTEM_IO_PROCESSING,
661                           AFS_TRACE_LEVEL_VERBOSE,
662                           "AFSCommonWrite (%p) Processing CACHED request Offset %0I64X Len %08lX%s\n",
663                           Irp,
664                           liStartingByte.QuadPart,
665                           ulByteCount,
666                           bRetry ? " RETRY" : ""));
667
668             ntStatus = AFSCachedWrite( DeviceObject, Irp, liStartingByte, ulByteCount, bForceFlush);
669         }
670         else
671         {
672
673             AFSDbgTrace(( AFS_SUBSYSTEM_IO_PROCESSING,
674                           AFS_TRACE_LEVEL_VERBOSE,
675                           "AFSCommonWrite (%p) Processing NON-CACHED request Offset %0I64X Len %08lX%s\n",
676                           Irp,
677                           liStartingByte.QuadPart,
678                           ulByteCount,
679                           bRetry ? " RETRY" : ""));
680
681             if( BooleanFlagOn( pRDRDevExt->DeviceFlags, AFS_DEVICE_FLAG_DIRECT_SERVICE_IO))
682             {
683
684                 ntStatus = AFSNonCachedWriteDirect( DeviceObject, Irp,  liStartingByte, ulByteCount);
685             }
686             else
687             {
688                 ntStatus = AFSNonCachedWrite( DeviceObject, Irp,  liStartingByte, ulByteCount);
689             }
690         }
691
692 try_exit:
693
694         AFSDbgTrace(( AFS_SUBSYSTEM_IO_PROCESSING,
695                       AFS_TRACE_LEVEL_VERBOSE,
696                       "AFSCommonWrite (%p) Process complete Status %08lX\n",
697                       Irp,
698                       ntStatus));
699
700         if ( NT_SUCCESS( ntStatus))
701         {
702             if ( !bPagingIo)
703             {
704
705                 if( bSynchronousFo)
706                 {
707
708                     pFileObject->CurrentByteOffset.QuadPart = liStartingByte.QuadPart + ulByteCount;
709                 }
710
711                 //
712                 // If this extended the Vdl, then update it accordingly
713                 //
714
715                 if( liStartingByte.QuadPart + ulByteCount > pFcb->Header.ValidDataLength.QuadPart)
716                 {
717
718                     pFcb->Header.ValidDataLength.QuadPart = liStartingByte.QuadPart + ulByteCount;
719                 }
720
721                 if( !BooleanFlagOn( pFcb->Flags, AFS_FCB_FLAG_UPDATE_LAST_WRITE_TIME))
722                 {
723
724                     SetFlag( pFcb->Flags, AFS_FCB_FLAG_UPDATE_WRITE_TIME);
725
726                     KeQuerySystemTime( &pFcb->ObjectInformation->LastWriteTime);
727                 }
728             }
729         }
730
731         if ( !bPagingIo && bNonCachedIo && CcIsFileCached( pFileObject) &&
732              pNPFcb->SectionObjectPointers.DataSectionObject != NULL &&
733              bReleaseSectionObject)
734         {
735             //
736             // Regardless of whether or not the a non-paging non-cached write
737             // succeeds or fails, if the file is cached the contents of the
738             // cache are no longer up to date.  A CcPurgeCacheSection must be
739             // performed to force subsequent cached reads to obtain the data
740             // from the service.
741             //
742             // The Fcb Resource is dropped in order to permit filters that perform
743             // an open via a worker thread in response to a purge to do so without
744             // deadlocking.  The SectionObjectResource is held across the purge to
745             // prevent racing with other cache operations.
746             //
747
748             if( bReleaseMain)
749             {
750
751                 AFSReleaseResource( &pNPFcb->Resource);
752
753                 bReleaseMain = FALSE;
754             }
755
756             if ( !CcPurgeCacheSection( &pNPFcb->SectionObjectPointers,
757                                        &liStartingByte,
758                                        ulByteCount,
759                                        FALSE))
760             {
761
762                 AFSDbgTrace(( AFS_SUBSYSTEM_IO_PROCESSING,
763                               AFS_TRACE_LEVEL_WARNING,
764                               "AFSCommonWrite CcPurgeCacheSection failure FID %08lX-%08lX-%08lX-%08lX\n",
765                               pFcb->ObjectInformation->FileId.Cell,
766                               pFcb->ObjectInformation->FileId.Volume,
767                               pFcb->ObjectInformation->FileId.Vnode,
768                               pFcb->ObjectInformation->FileId.Unique));
769
770                 SetFlag( pFcb->Flags, AFS_FCB_FLAG_PURGE_ON_CLOSE);
771             }
772         }
773
774         ObDereferenceObject(pFileObject);
775
776         if( bReleaseSectionObject)
777         {
778
779             AFSReleaseResource( &pNPFcb->SectionObjectResource);
780         }
781
782         if( bReleasePaging)
783         {
784
785             AFSReleaseResource( &pNPFcb->PagingResource);
786         }
787
788         if( bReleaseMain)
789         {
790
791             AFSReleaseResource( &pNPFcb->Resource);
792         }
793
794         if( bCompleteIrp)
795         {
796
797             AFSCompleteRequest( Irp,
798                                 ntStatus);
799         }
800     }
801
802     return ntStatus;
803 }
804
805 NTSTATUS
806 AFSIOCtlWrite( IN PDEVICE_OBJECT DeviceObject,
807                IN PIRP Irp)
808 {
809
810     UNREFERENCED_PARAMETER(DeviceObject);
811     NTSTATUS ntStatus = STATUS_SUCCESS;
812     AFSPIOCtlIORequestCB stIORequestCB;
813     PIO_STACK_LOCATION pIrpSp = IoGetCurrentIrpStackLocation( Irp);
814     AFSFcb *pFcb = NULL;
815     AFSCcb *pCcb = NULL;
816     AFSPIOCtlIOResultCB stIOResultCB;
817     ULONG ulBytesReturned = 0;
818     AFSFileID stParentFID;
819
820     __Enter
821     {
822
823         Irp->IoStatus.Information = 0;
824
825         RtlZeroMemory( &stIORequestCB,
826                        sizeof( AFSPIOCtlIORequestCB));
827
828         if( pIrpSp->Parameters.Write.Length == 0)
829         {
830
831             //
832             // Nothing to do in this case
833             //
834
835             try_return( ntStatus);
836         }
837
838         pFcb = (AFSFcb *)pIrpSp->FileObject->FsContext;
839
840         pCcb = (AFSCcb *)pIrpSp->FileObject->FsContext2;
841
842         AFSDbgTrace(( AFS_SUBSYSTEM_LOCK_PROCESSING,
843                       AFS_TRACE_LEVEL_VERBOSE,
844                       "AFSIOCtlWrite Acquiring Fcb lock %p SHARED %08lX\n",
845                       &pFcb->NPFcb->Resource,
846                       PsGetCurrentThread()));
847
848         AFSAcquireShared( &pFcb->NPFcb->Resource,
849                           TRUE);
850
851         //
852         // Get the parent fid to pass to the cm
853         //
854
855         RtlZeroMemory( &stParentFID,
856                        sizeof( AFSFileID));
857
858         if( BooleanFlagOn( pFcb->ObjectInformation->Flags, AFS_OBJECT_FLAGS_PARENT_FID))
859         {
860
861             //
862             // The parent directory FID of the node
863             //
864
865             stParentFID = pFcb->ObjectInformation->ParentFileId;
866         }
867
868         //
869         // Set the control block up
870         //
871
872         stIORequestCB.RequestId = pCcb->RequestID;
873
874         if( pFcb->ObjectInformation->VolumeCB != NULL)
875         {
876             stIORequestCB.RootId = pFcb->ObjectInformation->VolumeCB->ObjectInformation.FileId;
877         }
878
879         //
880         // Lock down the buffer
881         //
882
883         stIORequestCB.MappedBuffer = AFSMapToService( Irp,
884                                                       pIrpSp->Parameters.Write.Length);
885
886         if( stIORequestCB.MappedBuffer == NULL)
887         {
888
889             try_return( ntStatus = STATUS_INSUFFICIENT_RESOURCES);
890         }
891
892         stIORequestCB.BufferLength = pIrpSp->Parameters.Write.Length;
893
894         stIOResultCB.BytesProcessed = 0;
895
896         ulBytesReturned = sizeof( AFSPIOCtlIOResultCB);
897
898         //
899         // Issue the request to the service
900         //
901
902         ntStatus = AFSProcessRequest( AFS_REQUEST_TYPE_PIOCTL_WRITE,
903                                       AFS_REQUEST_FLAG_SYNCHRONOUS,
904                                       &pCcb->AuthGroup,
905                                       NULL,
906                                       &stParentFID,
907                                       NULL,
908                                       0,
909                                       (void *)&stIORequestCB,
910                                       sizeof( AFSPIOCtlIORequestCB),
911                                       &stIOResultCB,
912                                       &ulBytesReturned);
913
914         if( !NT_SUCCESS( ntStatus))
915         {
916
917             try_return( ntStatus);
918         }
919
920         //
921         // Update the length written
922         //
923
924         Irp->IoStatus.Information = stIOResultCB.BytesProcessed;
925
926 try_exit:
927
928         if( stIORequestCB.MappedBuffer != NULL)
929         {
930
931             AFSUnmapServiceMappedBuffer( stIORequestCB.MappedBuffer,
932                                          Irp->MdlAddress);
933         }
934
935         if( pFcb != NULL)
936         {
937
938             AFSReleaseResource( &pFcb->NPFcb->Resource);
939         }
940     }
941
942     return ntStatus;
943 }
944
945 //
946 // This function is called when we know we have to read from the AFS Cache.
947 //
948 // It ensures that we have exents for the entirety of the write and
949 // then pins the extents into memory (meaning that although we may
950 // add we will not remove).  Then it creates a scatter gather write
951 // and fires off the IRPs
952 //
953 static
954 NTSTATUS
955 AFSNonCachedWrite( IN PDEVICE_OBJECT DeviceObject,
956                    IN PIRP Irp,
957                    IN LARGE_INTEGER StartingByte,
958                    IN ULONG ByteCount)
959 {
960     NTSTATUS           ntStatus = STATUS_UNSUCCESSFUL;
961     VOID              *pSystemBuffer = NULL;
962     BOOLEAN            bPagingIo = BooleanFlagOn( Irp->Flags, IRP_PAGING_IO);
963     BOOLEAN            bLocked = FALSE;
964     BOOLEAN            bCompleteIrp = TRUE;
965     AFSGatherIo       *pGatherIo = NULL;
966     AFSIoRun          *pIoRuns = NULL;
967     AFSIoRun           stIoRuns[AFS_MAX_STACK_IO_RUNS];
968     ULONG              extentsCount = 0, runCount = 0;
969     AFSExtent         *pStartExtent = NULL;
970     AFSExtent         *pIgnoreExtent = NULL;
971     IO_STACK_LOCATION *pIrpSp = IoGetCurrentIrpStackLocation( Irp);
972     PFILE_OBJECT       pFileObject = pIrpSp->FileObject;
973     AFSFcb            *pFcb = (AFSFcb *)pFileObject->FsContext;
974     AFSCcb            *pCcb = (AFSCcb *)pFileObject->FsContext2;
975     BOOLEAN            bSynchronousFo = BooleanFlagOn( pFileObject->Flags, FO_SYNCHRONOUS_IO);
976     AFSDeviceExt      *pDevExt = (AFSDeviceExt *)DeviceObject->DeviceExtension;
977     LARGE_INTEGER      liCurrentTime, liLastRequestTime;
978     AFSDeviceExt      *pControlDevExt = (AFSDeviceExt *)AFSControlDeviceObject->DeviceExtension;
979     PFILE_OBJECT       pCacheFileObject = NULL;
980     BOOLEAN            bDerefExtents = FALSE;
981
982     __Enter
983     {
984
985         AFSDbgTrace(( AFS_SUBSYSTEM_IO_PROCESSING,
986                       AFS_TRACE_LEVEL_VERBOSE,
987                       "AFSNonCachedWrite (FO: %p) StartingByte %08lX:%08lX Length %08lX\n",
988                       pFileObject,
989                       StartingByte.HighPart,
990                       StartingByte.LowPart,
991                       ByteCount));
992
993         if (ByteCount > pDevExt->Specific.RDR.MaxIo.QuadPart)
994         {
995
996             AFSDbgTrace(( AFS_SUBSYSTEM_IO_PROCESSING,
997                           AFS_TRACE_LEVEL_ERROR,
998                           "AFSNonCachedWrite (%p) Request %08lX Actual %08lX larger than MaxIO %I64X\n",
999                           Irp,
1000                           ByteCount,
1001                           pIrpSp->Parameters.Write.Length,
1002                           pDevExt->Specific.RDR.MaxIo.QuadPart));
1003
1004             try_return( ntStatus = STATUS_UNSUCCESSFUL);
1005         }
1006
1007         //
1008         // Get the mapping for the buffer
1009         //
1010         pSystemBuffer = AFSLockSystemBuffer( Irp,
1011                                              ByteCount);
1012
1013         if( pSystemBuffer == NULL)
1014         {
1015
1016             AFSDbgTrace(( AFS_SUBSYSTEM_IO_PROCESSING,
1017                           AFS_TRACE_LEVEL_ERROR,
1018                           "AFSNonCachedWrite (%p) Failed to map system buffer\n",
1019                           Irp));
1020
1021             try_return( ntStatus = STATUS_INSUFFICIENT_RESOURCES);
1022         }
1023
1024
1025         //
1026         // Provoke a get of the extents - if we need to.
1027         //
1028
1029         AFSDbgTrace(( AFS_SUBSYSTEM_EXTENT_PROCESSING,
1030                       AFS_TRACE_LEVEL_VERBOSE,
1031                       "AFSNonCachedWrite Requesting extents for fid %08lX-%08lX-%08lX-%08lX Offset %0I64X Length %08lX\n",
1032                       pFcb->ObjectInformation->FileId.Cell,
1033                       pFcb->ObjectInformation->FileId.Volume,
1034                       pFcb->ObjectInformation->FileId.Vnode,
1035                       pFcb->ObjectInformation->FileId.Unique,
1036                       StartingByte.QuadPart,
1037                       ByteCount));
1038
1039         ntStatus = AFSRequestExtentsAsync( pFcb,
1040                                            pCcb,
1041                                            &StartingByte,
1042                                            ByteCount);
1043
1044         if (!NT_SUCCESS(ntStatus))
1045         {
1046
1047             AFSDbgTrace(( AFS_SUBSYSTEM_IO_PROCESSING,
1048                           AFS_TRACE_LEVEL_ERROR,
1049                           "AFSNonCachedWrite (%p) Failed to request extents Status %08lX\n",
1050                           Irp,
1051                           ntStatus));
1052
1053             try_return( ntStatus);
1054         }
1055
1056         KeQueryTickCount( &liLastRequestTime);
1057
1058         while (TRUE)
1059         {
1060
1061             AFSDbgTrace(( AFS_SUBSYSTEM_LOCK_PROCESSING,
1062                           AFS_TRACE_LEVEL_VERBOSE,
1063                           "AFSNonCachedWrite Acquiring Fcb extents lock %p SHARED %08lX\n",
1064                           &pFcb->NPFcb->Specific.File.ExtentsResource,
1065                           PsGetCurrentThread()));
1066
1067             ASSERT( !ExIsResourceAcquiredLite( &pFcb->NPFcb->Specific.File.ExtentsResource ));
1068
1069             AFSAcquireShared( &pFcb->NPFcb->Specific.File.ExtentsResource, TRUE );
1070             bLocked = TRUE;
1071
1072             pStartExtent = NULL;
1073             pIgnoreExtent = NULL;
1074
1075             if ( AFSDoExtentsMapRegion( pFcb, &StartingByte, ByteCount, &pStartExtent, &pIgnoreExtent ))
1076             {
1077                 break;
1078             }
1079
1080             KeClearEvent( &pFcb->NPFcb->Specific.File.ExtentsRequestComplete );
1081
1082             AFSDbgTrace(( AFS_SUBSYSTEM_LOCK_PROCESSING,
1083                           AFS_TRACE_LEVEL_VERBOSE,
1084                           "AFSNonCachedWrite Releasing(1) Fcb extents lock %p SHARED %08lX\n",
1085                           &pFcb->NPFcb->Specific.File.ExtentsResource,
1086                           PsGetCurrentThread()));
1087
1088             AFSReleaseResource( &pFcb->NPFcb->Specific.File.ExtentsResource );
1089             bLocked= FALSE;
1090
1091             //
1092             // We will re-request the extents after waiting for them
1093             //
1094
1095             KeQueryTickCount( &liCurrentTime);
1096
1097             if( liCurrentTime.QuadPart - liLastRequestTime.QuadPart >= pControlDevExt->Specific.Control.ExtentRequestTimeCount.QuadPart)
1098             {
1099
1100                 AFSDbgTrace(( AFS_SUBSYSTEM_EXTENT_PROCESSING,
1101                               AFS_TRACE_LEVEL_VERBOSE,
1102                               "AFSNonCachedWrite Requesting extents, again, for fid %08lX-%08lX-%08lX-%08lX Offset %0I64X Length %08lX\n",
1103                               pFcb->ObjectInformation->FileId.Cell,
1104                               pFcb->ObjectInformation->FileId.Volume,
1105                               pFcb->ObjectInformation->FileId.Vnode,
1106                               pFcb->ObjectInformation->FileId.Unique,
1107                               StartingByte.QuadPart,
1108                               ByteCount));
1109
1110                 ntStatus = AFSRequestExtentsAsync( pFcb,
1111                                                    pCcb,
1112                                                    &StartingByte,
1113                                                    ByteCount);
1114
1115                 if (!NT_SUCCESS(ntStatus))
1116                 {
1117
1118                     AFSDbgTrace(( AFS_SUBSYSTEM_IO_PROCESSING,
1119                                   AFS_TRACE_LEVEL_ERROR,
1120                                   "AFSNonCachedWrite (%p) Failed to request extents Status %08lX\n",
1121                                   Irp,
1122                                   ntStatus));
1123
1124                     try_return( ntStatus);
1125                 }
1126
1127                 KeQueryTickCount( &liLastRequestTime);
1128             }
1129
1130
1131             AFSDbgTrace(( AFS_SUBSYSTEM_EXTENT_PROCESSING,
1132                           AFS_TRACE_LEVEL_VERBOSE,
1133                           "AFSNonCachedWrite Waiting for extents for fid %08lX-%08lX-%08lX-%08lX Offset %0I64X Length %08lX\n",
1134                           pFcb->ObjectInformation->FileId.Cell,
1135                           pFcb->ObjectInformation->FileId.Volume,
1136                           pFcb->ObjectInformation->FileId.Vnode,
1137                           pFcb->ObjectInformation->FileId.Unique,
1138                           StartingByte.QuadPart,
1139                           ByteCount));
1140
1141             //
1142             // Wait for it
1143             //
1144
1145             ntStatus =  AFSWaitForExtentMapping ( pFcb, pCcb);
1146
1147             if (!NT_SUCCESS(ntStatus))
1148             {
1149
1150                 AFSDbgTrace(( AFS_SUBSYSTEM_EXTENT_PROCESSING,
1151                               AFS_TRACE_LEVEL_ERROR,
1152                               "AFSNonCachedWrite Failed wait for extents for fid %08lX-%08lX-%08lX-%08lX Offset %0I64X Length %08lX Status %08lX\n",
1153                               pFcb->ObjectInformation->FileId.Cell,
1154                               pFcb->ObjectInformation->FileId.Volume,
1155                               pFcb->ObjectInformation->FileId.Vnode,
1156                               pFcb->ObjectInformation->FileId.Unique,
1157                               StartingByte.QuadPart,
1158                               ByteCount,
1159                               ntStatus));
1160
1161                 try_return( ntStatus);
1162             }
1163         }
1164
1165         //
1166         // As per the read path -
1167         //
1168
1169         AFSDbgTrace(( AFS_SUBSYSTEM_EXTENT_PROCESSING,
1170                       AFS_TRACE_LEVEL_VERBOSE,
1171                       "AFSNonCachedWrite Extents located for fid %08lX-%08lX-%08lX-%08lX Offset %0I64X Length %08lX\n",
1172                       pFcb->ObjectInformation->FileId.Cell,
1173                       pFcb->ObjectInformation->FileId.Volume,
1174                       pFcb->ObjectInformation->FileId.Vnode,
1175                       pFcb->ObjectInformation->FileId.Unique,
1176                       StartingByte.QuadPart,
1177                       ByteCount));
1178
1179         ntStatus = AFSGetExtents( pFcb,
1180                                   &StartingByte,
1181                                   ByteCount,
1182                                   pStartExtent,
1183                                   &extentsCount,
1184                                   &runCount);
1185
1186         if (!NT_SUCCESS(ntStatus))
1187         {
1188
1189             AFSDbgTrace(( AFS_SUBSYSTEM_IO_PROCESSING,
1190                           AFS_TRACE_LEVEL_ERROR,
1191                           "AFSNonCachedWrite (%p) Failed to retrieve mapped extents Status %08lX\n",
1192                           Irp,
1193                           ntStatus));
1194
1195             try_return( ntStatus );
1196         }
1197
1198         AFSDbgTrace(( AFS_SUBSYSTEM_IO_PROCESSING,
1199                       AFS_TRACE_LEVEL_VERBOSE,
1200                       "AFSNonCachedWrite (%p) Successfully retrieved map extents count %d run count %d\n",
1201                       Irp,
1202                       extentsCount,
1203                       runCount));
1204
1205         if( BooleanFlagOn( AFSLibControlFlags, AFS_REDIR_LIB_FLAGS_NONPERSISTENT_CACHE))
1206         {
1207
1208             Irp->IoStatus.Information = ByteCount;
1209
1210 #if GEN_MD5
1211             //
1212             // Setup the MD5 for each extent
1213             //
1214
1215             AFSSetupMD5Hash( pFcb,
1216                              pStartExtent,
1217                              extentsCount,
1218                              pSystemBuffer,
1219                              &StartingByte,
1220                              ByteCount);
1221 #endif
1222
1223             ntStatus = AFSProcessExtentRun( pSystemBuffer,
1224                                             &StartingByte,
1225                                             ByteCount,
1226                                             pStartExtent,
1227                                             TRUE);
1228
1229             if (!NT_SUCCESS(ntStatus))
1230             {
1231
1232                 AFSDbgTrace(( AFS_SUBSYSTEM_IO_PROCESSING,
1233                               AFS_TRACE_LEVEL_ERROR,
1234                               "AFSNonCachedWrite (%p) Failed to process extent run for non-persistent cache Status %08lX\n",
1235                               Irp,
1236                               ntStatus));
1237             }
1238
1239             try_return( ntStatus);
1240         }
1241
1242         //
1243         // Retrieve the cache file object
1244         //
1245
1246         pCacheFileObject = AFSReferenceCacheFileObject();
1247
1248         if( pCacheFileObject == NULL)
1249         {
1250
1251             ntStatus = STATUS_DEVICE_NOT_READY;
1252
1253             AFSDbgTrace(( AFS_SUBSYSTEM_EXTENT_PROCESSING,
1254                           AFS_TRACE_LEVEL_ERROR,
1255                           "AFSNonCachedWrite Failed to retrieve cache fileobject for fid %08lX-%08lX-%08lX-%08lX Offset %0I64X Length %08lX Status %08lX\n",
1256                           pFcb->ObjectInformation->FileId.Cell,
1257                           pFcb->ObjectInformation->FileId.Volume,
1258                           pFcb->ObjectInformation->FileId.Vnode,
1259                           pFcb->ObjectInformation->FileId.Unique,
1260                           StartingByte.QuadPart,
1261                           ByteCount,
1262                           ntStatus));
1263
1264             try_return( ntStatus);
1265         }
1266
1267         if (runCount > AFS_MAX_STACK_IO_RUNS)
1268         {
1269
1270             pIoRuns = (AFSIoRun*) AFSExAllocatePoolWithTag( PagedPool,
1271                                                             runCount * sizeof( AFSIoRun ),
1272                                                             AFS_IO_RUN_TAG );
1273             if (NULL == pIoRuns)
1274             {
1275
1276                 AFSDbgTrace(( AFS_SUBSYSTEM_IO_PROCESSING,
1277                               AFS_TRACE_LEVEL_ERROR,
1278                               "AFSNonCachedWrite (%p) Failed to allocate IO run block\n",
1279                               Irp));
1280
1281                 try_return( ntStatus = STATUS_INSUFFICIENT_RESOURCES );
1282             }
1283         }
1284         else
1285         {
1286
1287             pIoRuns = stIoRuns;
1288         }
1289
1290         RtlZeroMemory( pIoRuns, runCount * sizeof( AFSIoRun ));
1291
1292         ntStatus = AFSSetupIoRun( IoGetRelatedDeviceObject( pCacheFileObject),
1293                                   Irp,
1294                                   pSystemBuffer,
1295                                   pIoRuns,
1296                                   &StartingByte,
1297                                   ByteCount,
1298                                   pStartExtent,
1299                                   &runCount );
1300
1301         if (!NT_SUCCESS(ntStatus))
1302         {
1303
1304             AFSDbgTrace(( AFS_SUBSYSTEM_IO_PROCESSING,
1305                           AFS_TRACE_LEVEL_ERROR,
1306                           "AFSNonCachedWrite (%p) Failed to initialize IO run block Status %08lX\n",
1307                           Irp,
1308                           ntStatus));
1309
1310             try_return( ntStatus );
1311         }
1312
1313         AFSReferenceActiveExtents( pStartExtent,
1314                                    extentsCount);
1315
1316         bDerefExtents = TRUE;
1317
1318         AFSDbgTrace(( AFS_SUBSYSTEM_LOCK_PROCESSING,
1319                       AFS_TRACE_LEVEL_VERBOSE,
1320                       "AFSNonCachedWrite Releasing(2) Fcb extents lock %p SHARED %08lX\n",
1321                       &pFcb->NPFcb->Specific.File.ExtentsResource,
1322                       PsGetCurrentThread()));
1323
1324         AFSReleaseResource( &pFcb->NPFcb->Specific.File.ExtentsResource );
1325         bLocked = FALSE;
1326
1327         pGatherIo = (AFSGatherIo*) AFSExAllocatePoolWithTag( NonPagedPool,
1328                                                              sizeof( AFSGatherIo),
1329                                                              AFS_GATHER_TAG);
1330
1331         if (NULL == pGatherIo)
1332         {
1333
1334             AFSDbgTrace(( AFS_SUBSYSTEM_IO_PROCESSING,
1335                           AFS_TRACE_LEVEL_ERROR,
1336                           "AFSNonCachedWrite (%p) Failed to allocate IO gather block\n",
1337                           Irp));
1338
1339             AFSDbgTrace(( AFS_SUBSYSTEM_LOCK_PROCESSING,
1340                           AFS_TRACE_LEVEL_VERBOSE,
1341                           "AFSNonCachedWrite Acquiring(1) Fcb extents lock %p SHARED %08lX\n",
1342                           &pFcb->NPFcb->Specific.File.ExtentsResource,
1343                           PsGetCurrentThread()));
1344
1345             AFSAcquireShared( &pFcb->NPFcb->Specific.File.ExtentsResource,
1346                               TRUE);
1347             bLocked = TRUE;
1348
1349             AFSDereferenceActiveExtents( pStartExtent,
1350                                          extentsCount);
1351
1352             try_return (ntStatus = STATUS_INSUFFICIENT_RESOURCES );
1353         }
1354
1355         RtlZeroMemory( pGatherIo, sizeof( AFSGatherIo ));
1356
1357         //
1358         // Initialize count to 1, that was we won't get an early
1359         // completion if the first irp completes before the second is
1360         // queued.
1361         //
1362         pGatherIo->Count = 1;
1363         pGatherIo->Status = STATUS_SUCCESS;
1364         pGatherIo->MasterIrp = Irp;
1365         pGatherIo->Synchronous = TRUE;
1366         pGatherIo->CompleteMasterIrp = FALSE;
1367
1368         bCompleteIrp = TRUE;
1369
1370         if( pGatherIo->Synchronous)
1371         {
1372             KeInitializeEvent( &pGatherIo->Event, NotificationEvent, FALSE );
1373         }
1374
1375 #if GEN_MD5
1376         //
1377         // Setup the MD5 for each extent
1378         //
1379
1380         AFSSetupMD5Hash( pFcb,
1381                          pStartExtent,
1382                          extentsCount,
1383                          pSystemBuffer,
1384                          &StartingByte,
1385                          ByteCount);
1386 #endif
1387
1388         //
1389         // Pre-emptively set up the count
1390         //
1391
1392         Irp->IoStatus.Information = ByteCount;
1393
1394         ntStatus = AFSQueueStartIos( pCacheFileObject,
1395                                      IRP_MJ_WRITE,
1396                                      IRP_WRITE_OPERATION | IRP_SYNCHRONOUS_API,
1397                                      pIoRuns,
1398                                      runCount,
1399                                      pGatherIo);
1400
1401         AFSDbgTrace(( AFS_SUBSYSTEM_IO_PROCESSING,
1402                       AFS_TRACE_LEVEL_VERBOSE,
1403                       "AFSNonCachedWrite (%p) AFSStartIos completed Status %08lX\n",
1404                       Irp,
1405                       ntStatus));
1406
1407         if( !NT_SUCCESS( ntStatus))
1408         {
1409
1410             AFSDbgTrace(( AFS_SUBSYSTEM_LOCK_PROCESSING,
1411                           AFS_TRACE_LEVEL_VERBOSE,
1412                           "AFSNonCachedWrite Acquiring(2) Fcb extents lock %p SHARED %08lX\n",
1413                           &pFcb->NPFcb->Specific.File.ExtentsResource,
1414                           PsGetCurrentThread()));
1415
1416             AFSAcquireShared( &pFcb->NPFcb->Specific.File.ExtentsResource,
1417                               TRUE);
1418             bLocked = TRUE;
1419
1420             AFSDereferenceActiveExtents( pStartExtent,
1421                                          extentsCount);
1422
1423             try_return( ntStatus);
1424         }
1425
1426         //
1427         // Wait for completion of All IOs we started.
1428         //
1429
1430         ntStatus = KeWaitForSingleObject( &pGatherIo->Event,
1431                                           Executive,
1432                                           KernelMode,
1433                                           FALSE,
1434                                           NULL);
1435
1436         if( NT_SUCCESS( ntStatus))
1437         {
1438
1439             ntStatus = pGatherIo->Status;
1440         }
1441
1442         if( !NT_SUCCESS( ntStatus))
1443         {
1444
1445             AFSDbgTrace(( AFS_SUBSYSTEM_LOCK_PROCESSING,
1446                           AFS_TRACE_LEVEL_VERBOSE,
1447                           "AFSNonCachedWrite Acquiring(3) Fcb extents lock %p SHARED %08lX\n",
1448                           &pFcb->NPFcb->Specific.File.ExtentsResource,
1449                           PsGetCurrentThread()));
1450
1451             AFSAcquireShared( &pFcb->NPFcb->Specific.File.ExtentsResource,
1452                               TRUE);
1453             bLocked = TRUE;
1454
1455             AFSDereferenceActiveExtents( pStartExtent,
1456                                          extentsCount);
1457
1458             try_return( ntStatus);
1459         }
1460
1461 try_exit:
1462
1463         if( NT_SUCCESS( ntStatus) &&
1464             pStartExtent != NULL &&
1465             Irp->IoStatus.Information > 0)
1466         {
1467
1468             if ( !bLocked)
1469             {
1470
1471                 AFSAcquireShared( &pFcb->NPFcb->Specific.File.ExtentsResource,
1472                                   TRUE);
1473                 bLocked = TRUE;
1474             }
1475
1476             //
1477             // Since this is dirty we can mark the extents dirty now.
1478             // AFSMarkDirty will dereference the extents.  Do not call
1479             // AFSDereferenceActiveExtents() in this code path.
1480             //
1481
1482             AFSMarkDirty( pFcb,
1483                           pStartExtent,
1484                           extentsCount,
1485                           &StartingByte,
1486                           bDerefExtents);
1487
1488             if (!bPagingIo)
1489             {
1490                 //
1491                 // This was an uncached user write - tell the server to do
1492                 // the flush when the worker thread next wakes up
1493                 //
1494                 pFcb->Specific.File.LastServerFlush.QuadPart = 0;
1495             }
1496         }
1497
1498         if( pCacheFileObject != NULL)
1499         {
1500             AFSReleaseCacheFileObject( pCacheFileObject);
1501         }
1502
1503         AFSDbgTrace(( AFS_SUBSYSTEM_IO_PROCESSING,
1504                       AFS_TRACE_LEVEL_VERBOSE,
1505                       "AFSNonCachedWrite (FO: %p) StartingByte %08lX:%08lX Length %08lX Status %08lX\n",
1506                       pFileObject,
1507                       StartingByte.HighPart,
1508                       StartingByte.LowPart,
1509                       ByteCount,
1510                       ntStatus));
1511
1512         if (NT_SUCCESS(ntStatus) &&
1513             !bPagingIo &&
1514             bSynchronousFo)
1515         {
1516
1517             pFileObject->CurrentByteOffset.QuadPart = StartingByte.QuadPart + ByteCount;
1518         }
1519
1520         if( bLocked)
1521         {
1522
1523             AFSDbgTrace(( AFS_SUBSYSTEM_LOCK_PROCESSING,
1524                           AFS_TRACE_LEVEL_VERBOSE,
1525                           "AFSNonCachedWrite Releasing Fcb extents lock %p SHARED %08lX\n",
1526                           &pFcb->NPFcb->Specific.File.ExtentsResource,
1527                           PsGetCurrentThread()));
1528
1529             AFSReleaseResource( &pFcb->NPFcb->Specific.File.ExtentsResource );
1530         }
1531
1532         if( pGatherIo)
1533         {
1534             AFSExFreePoolWithTag(pGatherIo, AFS_GATHER_TAG);
1535         }
1536
1537         if( NULL != pIoRuns &&
1538             stIoRuns != pIoRuns)
1539         {
1540             AFSExFreePoolWithTag(pIoRuns, AFS_IO_RUN_TAG);
1541         }
1542
1543         if( bCompleteIrp)
1544         {
1545
1546             AFSDbgTrace(( AFS_SUBSYSTEM_IO_PROCESSING,
1547                           AFS_TRACE_LEVEL_VERBOSE,
1548                           "AFSNonCachedWrite Completing Irp %p Status %08lX Info %08lX\n",
1549                           Irp,
1550                           ntStatus,
1551                           Irp->IoStatus.Information));
1552
1553             AFSCompleteRequest( Irp, ntStatus);
1554         }
1555     }
1556
1557     return ntStatus;
1558 }
1559
1560 static
1561 NTSTATUS
1562 AFSNonCachedWriteDirect( IN PDEVICE_OBJECT DeviceObject,
1563                          IN PIRP Irp,
1564                          IN LARGE_INTEGER StartingByte,
1565                          IN ULONG ByteCount)
1566 {
1567     NTSTATUS           ntStatus = STATUS_UNSUCCESSFUL;
1568     VOID              *pSystemBuffer = NULL;
1569     BOOLEAN            bPagingIo = BooleanFlagOn( Irp->Flags, IRP_PAGING_IO);
1570     IO_STACK_LOCATION *pIrpSp = IoGetCurrentIrpStackLocation( Irp);
1571     PFILE_OBJECT       pFileObject = pIrpSp->FileObject;
1572     AFSFcb            *pFcb = (AFSFcb *)pFileObject->FsContext;
1573     AFSCcb            *pCcb = (AFSCcb *)pFileObject->FsContext2;
1574     BOOLEAN            bSynchronousFo = BooleanFlagOn( pFileObject->Flags, FO_SYNCHRONOUS_IO);
1575     BOOLEAN            bNoIntermediateBuffering = BooleanFlagOn( pFileObject->Flags, FO_NO_INTERMEDIATE_BUFFERING);
1576     AFSDeviceExt      *pDevExt = (AFSDeviceExt *)DeviceObject->DeviceExtension;
1577     AFSFileIOCB        stFileIORequest;
1578     AFSFileIOResultCB  stFileIOResult;
1579     ULONG              ulResultLen = 0;
1580     ULONG              ulFlags;
1581
1582     __Enter
1583     {
1584         Irp->IoStatus.Information = 0;
1585
1586         AFSDbgTrace(( AFS_SUBSYSTEM_IO_PROCESSING,
1587                       AFS_TRACE_LEVEL_VERBOSE,
1588                       "AFSNonCachedWriteDirect (FO: %p) StartingByte %08lX:%08lX Length %08lX\n",
1589                       pFileObject,
1590                       StartingByte.HighPart,
1591                       StartingByte.LowPart,
1592                       ByteCount));
1593
1594         if (ByteCount > pDevExt->Specific.RDR.MaxIo.QuadPart)
1595         {
1596
1597             AFSDbgTrace(( AFS_SUBSYSTEM_IO_PROCESSING,
1598                           AFS_TRACE_LEVEL_ERROR,
1599                           "AFSNonCachedWriteDirect (%p) Request %08lX Actual %08lX larger than MaxIO %I64X\n",
1600                           Irp,
1601                           ByteCount,
1602                           pIrpSp->Parameters.Write.Length,
1603                           pDevExt->Specific.RDR.MaxIo.QuadPart));
1604
1605             try_return( ntStatus = STATUS_UNSUCCESSFUL);
1606         }
1607
1608         //
1609         // Get the mapping for the buffer
1610         //
1611         pSystemBuffer = AFSLockSystemBuffer( Irp,
1612                                              ByteCount);
1613
1614         if( pSystemBuffer == NULL)
1615         {
1616
1617             AFSDbgTrace(( AFS_SUBSYSTEM_IO_PROCESSING,
1618                           AFS_TRACE_LEVEL_ERROR,
1619                           "AFSNonCachedWriteDirect (%p) Failed to map system buffer\n",
1620                           Irp));
1621
1622             try_return( ntStatus = STATUS_INSUFFICIENT_RESOURCES);
1623         }
1624
1625         //
1626         // Issue the request at the service for processing
1627         //
1628
1629         ulResultLen = sizeof( AFSFileIOResultCB);
1630
1631         RtlZeroMemory( &stFileIORequest,
1632                        sizeof( AFSFileIOCB));
1633
1634         RtlZeroMemory( &stFileIOResult,
1635                        sizeof( AFSFileIOResultCB));
1636
1637         stFileIORequest.SystemIOBuffer = pSystemBuffer;
1638
1639         stFileIORequest.SystemIOBufferMdl = Irp->MdlAddress;
1640
1641         stFileIORequest.IOLength = ByteCount;
1642
1643         stFileIORequest.IOOffset = StartingByte;
1644
1645         ulFlags = AFS_REQUEST_FLAG_SYNCHRONOUS;
1646
1647         if ( bNoIntermediateBuffering)
1648         {
1649
1650             ulFlags |= AFS_REQUEST_FLAG_CACHE_BYPASS;
1651         }
1652
1653         //
1654         // Update file metadata
1655         //
1656
1657         stFileIORequest.EndOfFile = pFcb->ObjectInformation->EndOfFile;
1658
1659         stFileIORequest.CreateTime = pFcb->ObjectInformation->CreationTime;
1660
1661         stFileIORequest.ChangeTime = pFcb->ObjectInformation->ChangeTime;
1662
1663         stFileIORequest.LastAccessTime = pFcb->ObjectInformation->LastAccessTime;
1664
1665         stFileIORequest.LastWriteTime = pFcb->ObjectInformation->LastWriteTime;
1666
1667         //
1668         // Write the data to the service
1669         //
1670
1671         ntStatus = AFSProcessRequest( AFS_REQUEST_TYPE_PROCESS_WRITE_FILE,
1672                                       ulFlags,
1673                                       &pCcb->AuthGroup,
1674                                       &pCcb->DirectoryCB->NameInformation.FileName,
1675                                       &pFcb->ObjectInformation->FileId,
1676                                       pFcb->ObjectInformation->VolumeCB->VolumeInformation.Cell,
1677                                       pFcb->ObjectInformation->VolumeCB->VolumeInformation.CellLength,
1678                                       &stFileIORequest,
1679                                       sizeof( AFSFileIOCB),
1680                                       &stFileIOResult,
1681                                       &ulResultLen);
1682
1683         if( NT_SUCCESS( ntStatus))
1684         {
1685
1686             Irp->IoStatus.Information = (ULONG_PTR)stFileIOResult.Length;
1687         }
1688         else
1689         {
1690
1691             AFSDbgTrace(( AFS_SUBSYSTEM_IO_PROCESSING,
1692                           AFS_TRACE_LEVEL_ERROR,
1693                           "AFSNonCachedWriteDirect (%p) Failed to send write to service Status %08lX\n",
1694                           Irp,
1695                           ntStatus));
1696         }
1697
1698 try_exit:
1699
1700         AFSDbgTrace(( AFS_SUBSYSTEM_IO_PROCESSING,
1701                       AFS_TRACE_LEVEL_VERBOSE,
1702                       "AFSNonCachedWriteDirect (FO: %p) StartingByte %08lX:%08lX Length %08lX Status %08lX\n",
1703                       pFileObject,
1704                       StartingByte.HighPart,
1705                       StartingByte.LowPart,
1706                       ByteCount,
1707                       ntStatus));
1708
1709         if (NT_SUCCESS(ntStatus) &&
1710             !bPagingIo &&
1711             bSynchronousFo)
1712         {
1713
1714             pFileObject->CurrentByteOffset.QuadPart = StartingByte.QuadPart + ByteCount;
1715         }
1716
1717         AFSDbgTrace(( AFS_SUBSYSTEM_IO_PROCESSING,
1718                       AFS_TRACE_LEVEL_VERBOSE,
1719                       "AFSNonCachedWriteDirect Completing Irp %p Status %08lX Info %08lX\n",
1720                       Irp,
1721                       ntStatus,
1722                       Irp->IoStatus.Information));
1723
1724         AFSCompleteRequest( Irp, ntStatus);
1725     }
1726
1727     return ntStatus;
1728 }
1729
1730 static
1731 NTSTATUS
1732 AFSCachedWrite( IN PDEVICE_OBJECT DeviceObject,
1733                 IN PIRP Irp,
1734                 IN LARGE_INTEGER StartingByte,
1735                 IN ULONG ByteCount,
1736                 IN BOOLEAN ForceFlush)
1737 {
1738     UNREFERENCED_PARAMETER(DeviceObject);
1739     PVOID              pSystemBuffer = NULL;
1740     NTSTATUS           ntStatus = STATUS_SUCCESS;
1741     IO_STATUS_BLOCK    iosbFlush;
1742     IO_STACK_LOCATION *pIrpSp = IoGetCurrentIrpStackLocation( Irp);
1743     PFILE_OBJECT       pFileObject = pIrpSp->FileObject;
1744     AFSFcb            *pFcb = (AFSFcb *)pFileObject->FsContext;
1745     BOOLEAN            bSynchronousFo = BooleanFlagOn( pFileObject->Flags, FO_SYNCHRONOUS_IO);
1746     ULONG              ulCurrentIO = 0, ulTotalLen = ByteCount;
1747     PMDL               pCurrentMdl = Irp->MdlAddress;
1748     LARGE_INTEGER      liCurrentOffset;
1749
1750     __Enter
1751     {
1752
1753         Irp->IoStatus.Information = 0;
1754
1755         if( BooleanFlagOn( pIrpSp->MinorFunction, IRP_MN_MDL))
1756         {
1757
1758             __try
1759             {
1760
1761                 CcPrepareMdlWrite( pFileObject,
1762                                    &StartingByte,
1763                                    ByteCount,
1764                                    &Irp->MdlAddress,
1765                                    &Irp->IoStatus);
1766
1767                 ntStatus = Irp->IoStatus.Status;
1768             }
1769             __except( EXCEPTION_EXECUTE_HANDLER)
1770             {
1771                 ntStatus = GetExceptionCode();
1772
1773                 AFSDbgTrace(( AFS_SUBSYSTEM_IO_PROCESSING,
1774                               AFS_TRACE_LEVEL_ERROR,
1775                               "AFSCachedWrite (%p) Exception thrown while preparing mdl write Status %08lX\n",
1776                               Irp,
1777                               ntStatus));
1778             }
1779
1780             if( !NT_SUCCESS( ntStatus))
1781             {
1782
1783                 //
1784                 // Free up any potentially allocated mdl's
1785                 //
1786
1787                 CcMdlWriteComplete( pFileObject,
1788                                     &StartingByte,
1789                                     Irp->MdlAddress);
1790
1791                 Irp->MdlAddress = NULL;
1792
1793                 AFSDbgTrace(( AFS_SUBSYSTEM_IO_PROCESSING,
1794                               AFS_TRACE_LEVEL_ERROR,
1795                               "AFSCachedWrite (%p) Failed to process MDL write Status %08lX\n",
1796                               Irp,
1797                               ntStatus));
1798             }
1799
1800             try_return( ntStatus);
1801         }
1802
1803         liCurrentOffset.QuadPart = StartingByte.QuadPart;
1804
1805         while( ulTotalLen > 0)
1806         {
1807
1808             ntStatus = STATUS_SUCCESS;
1809
1810             if( pCurrentMdl != NULL)
1811             {
1812
1813                 pSystemBuffer = MmGetSystemAddressForMdlSafe( pCurrentMdl,
1814                                                               NormalPagePriority);
1815
1816                 ulCurrentIO = MmGetMdlByteCount( pCurrentMdl);
1817
1818                 if( ulCurrentIO > ulTotalLen)
1819                 {
1820                     ulCurrentIO = ulTotalLen;
1821                 }
1822             }
1823             else
1824             {
1825
1826                 pSystemBuffer = AFSLockSystemBuffer( Irp,
1827                                                      ulTotalLen);
1828
1829                 ulCurrentIO = ulTotalLen;
1830             }
1831
1832             if( pSystemBuffer == NULL)
1833             {
1834
1835                 AFSDbgTrace(( AFS_SUBSYSTEM_IO_PROCESSING,
1836                               AFS_TRACE_LEVEL_ERROR,
1837                               "AFSCachedWrite (%p) Failed to lock system buffer\n",
1838                               Irp));
1839
1840                 try_return( ntStatus = STATUS_INSUFFICIENT_RESOURCES);
1841             }
1842
1843             __try
1844             {
1845
1846                 if( !CcCopyWrite( pFileObject,
1847                                   &liCurrentOffset,
1848                                   ulCurrentIO,
1849                                   TRUE,
1850                                   pSystemBuffer))
1851                 {
1852                     //
1853                     // Failed to process request.
1854                     //
1855
1856                     AFSDbgTrace(( AFS_SUBSYSTEM_FILE_PROCESSING,
1857                                   AFS_TRACE_LEVEL_ERROR,
1858                                   "AFSCachedWrite (%p) Failed to issue CcCopyWrite %wZ @ %0I64X Status %08lX\n",
1859                                   Irp,
1860                                   &pFileObject->FileName,
1861                                   liCurrentOffset.QuadPart,
1862                                   Irp->IoStatus.Status));
1863
1864                     try_return( ntStatus = STATUS_UNSUCCESSFUL);
1865                 }
1866             }
1867             __except( EXCEPTION_EXECUTE_HANDLER)
1868             {
1869
1870                 ntStatus = GetExceptionCode();
1871
1872                 AFSDbgTrace(( AFS_SUBSYSTEM_IO_PROCESSING,
1873                               AFS_TRACE_LEVEL_ERROR,
1874                               "AFSCachedWrite (%p) CcCopyWrite Threw exception %wZ @ %0I64X Status %08lX\n",
1875                               Irp,
1876                               &pFileObject->FileName,
1877                               liCurrentOffset.QuadPart,
1878                               ntStatus));
1879             }
1880
1881             if( !NT_SUCCESS( ntStatus))
1882             {
1883                 try_return( ntStatus);
1884             }
1885
1886             if( ForceFlush ||
1887                 BooleanFlagOn(pFileObject->Flags, (FO_NO_INTERMEDIATE_BUFFERING + FO_WRITE_THROUGH)))
1888             {
1889
1890                 //
1891                 // We have detected a file we do a write through with.
1892                 //
1893
1894                 CcFlushCache(&pFcb->NPFcb->SectionObjectPointers,
1895                              &liCurrentOffset,
1896                              ulCurrentIO,
1897                              &iosbFlush);
1898
1899                 if( !NT_SUCCESS( iosbFlush.Status))
1900                 {
1901
1902                     AFSDbgTrace(( AFS_SUBSYSTEM_IO_PROCESSING,
1903                                   AFS_TRACE_LEVEL_ERROR,
1904                                   "AFSCachedWrite (%p) CcFlushCache failure %wZ FID %08lX-%08lX-%08lX-%08lX Status 0x%08lX Bytes 0x%08lX\n",
1905                                   Irp,
1906                                   &pFileObject->FileName,
1907                                   pFcb->ObjectInformation->FileId.Cell,
1908                                   pFcb->ObjectInformation->FileId.Volume,
1909                                   pFcb->ObjectInformation->FileId.Vnode,
1910                                   pFcb->ObjectInformation->FileId.Unique,
1911                                   iosbFlush.Status,
1912                                   iosbFlush.Information));
1913
1914                     try_return( ntStatus = iosbFlush.Status);
1915                 }
1916             }
1917
1918             if( ulTotalLen <= ulCurrentIO)
1919             {
1920                 break;
1921             }
1922
1923             liCurrentOffset.QuadPart += ulCurrentIO;
1924
1925             ulTotalLen -= ulCurrentIO;
1926
1927             pCurrentMdl = pCurrentMdl->Next;
1928         }
1929
1930 try_exit:
1931
1932         if( NT_SUCCESS( ntStatus))
1933         {
1934
1935             Irp->IoStatus.Information = ByteCount;
1936
1937             if ( ForceFlush ||
1938                  BooleanFlagOn(pFileObject->Flags, (FO_NO_INTERMEDIATE_BUFFERING + FO_WRITE_THROUGH)))
1939             {
1940                 //
1941                 // Write through asked for... Set things so that we get
1942                 // flush when the worker thread next wakes up
1943                 //
1944                 pFcb->Specific.File.LastServerFlush.QuadPart = 0;
1945             }
1946         }
1947
1948         AFSCompleteRequest( Irp,
1949                             ntStatus);
1950     }
1951
1952     return ntStatus;
1953 }
1954
1955 static
1956 NTSTATUS
1957 AFSExtendingWrite( IN AFSFcb *Fcb,
1958                    IN PFILE_OBJECT FileObject,
1959                    IN LONGLONG NewLength)
1960 {
1961     LARGE_INTEGER liSaveFileSize = Fcb->Header.FileSize;
1962     LARGE_INTEGER liSaveAllocation = Fcb->Header.AllocationSize;
1963     NTSTATUS      ntStatus = STATUS_SUCCESS;
1964     AFSCcb       *pCcb = (AFSCcb *)FileObject->FsContext2;
1965
1966     if( NewLength > Fcb->Header.AllocationSize.QuadPart)
1967     {
1968
1969         Fcb->Header.AllocationSize.QuadPart = NewLength;
1970
1971         Fcb->ObjectInformation->AllocationSize = Fcb->Header.AllocationSize;
1972     }
1973
1974     if( NewLength > Fcb->Header.FileSize.QuadPart)
1975     {
1976
1977         Fcb->Header.FileSize.QuadPart = NewLength;
1978
1979         Fcb->ObjectInformation->EndOfFile = Fcb->Header.FileSize;
1980     }
1981
1982     //
1983     // Tell the server
1984     //
1985
1986     ntStatus = AFSUpdateFileInformation( &Fcb->ObjectInformation->ParentFileId,
1987                                          Fcb->ObjectInformation,
1988                                          &pCcb->AuthGroup);
1989
1990     if (NT_SUCCESS(ntStatus))
1991     {
1992
1993         KeQuerySystemTime( &Fcb->ObjectInformation->ChangeTime);
1994
1995         SetFlag( Fcb->Flags, AFS_FCB_FLAG_FILE_MODIFIED | AFS_FCB_FLAG_UPDATE_CHANGE_TIME);
1996
1997         //
1998         // If the file is currently cached, then let the MM know about the extension
1999         //
2000
2001         if( CcIsFileCached( FileObject))
2002         {
2003             CcSetFileSizes( FileObject,
2004                             (PCC_FILE_SIZES)&Fcb->Header.AllocationSize);
2005         }
2006     }
2007     else
2008     {
2009         Fcb->Header.FileSize = liSaveFileSize;
2010         Fcb->Header.AllocationSize = liSaveAllocation;
2011     }
2012
2013     //
2014     // DownConvert file resource to shared
2015     //
2016     ExConvertExclusiveToSharedLite( &Fcb->NPFcb->Resource);
2017
2018     return ntStatus;
2019 }
2020
2021 NTSTATUS
2022 AFSShareWrite( IN PDEVICE_OBJECT DeviceObject,
2023                IN PIRP Irp)
2024 {
2025
2026     UNREFERENCED_PARAMETER(DeviceObject);
2027     NTSTATUS ntStatus = STATUS_SUCCESS;
2028     PIO_STACK_LOCATION pIrpSp = IoGetCurrentIrpStackLocation( Irp);
2029     AFSFcb *pFcb = NULL;
2030     AFSCcb *pCcb = NULL;
2031     AFSPipeIORequestCB *pIoRequest = NULL;
2032     void *pBuffer = NULL;
2033     AFSPipeIOResultCB stIoResult;
2034     ULONG ulBytesReturned = 0;
2035
2036     __Enter
2037     {
2038
2039         pFcb = (AFSFcb *)pIrpSp->FileObject->FsContext;
2040
2041         pCcb = (AFSCcb *)pIrpSp->FileObject->FsContext2;
2042
2043         AFSDbgTrace(( AFS_SUBSYSTEM_PIPE_PROCESSING,
2044                       AFS_TRACE_LEVEL_VERBOSE,
2045                       "AFSShareWrite On pipe %wZ Length %08lX\n",
2046                       &pCcb->DirectoryCB->NameInformation.FileName,
2047                       pIrpSp->Parameters.Write.Length));
2048
2049         if( pIrpSp->Parameters.Write.Length == 0)
2050         {
2051
2052             //
2053             // Nothing to do in this case
2054             //
2055
2056             try_return( ntStatus);
2057         }
2058
2059         //
2060         // Retrieve the buffer for the read request
2061         //
2062
2063         pBuffer = AFSLockSystemBuffer( Irp,
2064                                        pIrpSp->Parameters.Write.Length);
2065
2066         if( pBuffer == NULL)
2067         {
2068
2069             AFSDbgTrace(( AFS_SUBSYSTEM_PIPE_PROCESSING,
2070                           AFS_TRACE_LEVEL_ERROR,
2071                           "AFSShareWrite Failed to map buffer on pipe %wZ\n",
2072                           &pCcb->DirectoryCB->NameInformation.FileName));
2073
2074             try_return( ntStatus = STATUS_INSUFFICIENT_RESOURCES);
2075         }
2076
2077         AFSAcquireShared( &pFcb->NPFcb->Resource,
2078                           TRUE);
2079
2080         pIoRequest = (AFSPipeIORequestCB *)AFSExAllocatePoolWithTag( PagedPool,
2081                                                                      sizeof( AFSPipeIORequestCB) +
2082                                                                                 pIrpSp->Parameters.Write.Length,
2083                                                                      AFS_GENERIC_MEMORY_14_TAG);
2084
2085         if( pIoRequest == NULL)
2086         {
2087
2088             try_return( ntStatus = STATUS_INSUFFICIENT_RESOURCES);
2089         }
2090
2091         RtlZeroMemory( pIoRequest,
2092                        sizeof( AFSPipeIORequestCB) + pIrpSp->Parameters.Write.Length);
2093
2094         pIoRequest->RequestId = pCcb->RequestID;
2095
2096         pIoRequest->RootId = pFcb->ObjectInformation->VolumeCB->ObjectInformation.FileId;
2097
2098         pIoRequest->BufferLength = pIrpSp->Parameters.Write.Length;
2099
2100         RtlCopyMemory( (void *)((char *)pIoRequest + sizeof( AFSPipeIORequestCB)),
2101                        pBuffer,
2102                        pIrpSp->Parameters.Write.Length);
2103
2104         stIoResult.BytesProcessed = 0;
2105
2106         ulBytesReturned = sizeof( AFSPipeIOResultCB);
2107
2108         //
2109         // Issue the open request to the service
2110         //
2111
2112         ntStatus = AFSProcessRequest( AFS_REQUEST_TYPE_PIPE_WRITE,
2113                                       AFS_REQUEST_FLAG_SYNCHRONOUS,
2114                                       &pCcb->AuthGroup,
2115                                       &pCcb->DirectoryCB->NameInformation.FileName,
2116                                       NULL,
2117                                       NULL,
2118                                       0,
2119                                       pIoRequest,
2120                                       sizeof( AFSPipeIORequestCB) +
2121                                                 pIrpSp->Parameters.Write.Length,
2122                                       &stIoResult,
2123                                       &ulBytesReturned);
2124
2125         if( !NT_SUCCESS( ntStatus))
2126         {
2127
2128             AFSDbgTrace(( AFS_SUBSYSTEM_FILE_PROCESSING,
2129                           AFS_TRACE_LEVEL_ERROR,
2130                           "AFSShareWrite (%p) Failed service write Status %08lX\n",
2131                           Irp,
2132                           ntStatus));
2133
2134             try_return( ntStatus);
2135         }
2136
2137         AFSDbgTrace(( AFS_SUBSYSTEM_PIPE_PROCESSING,
2138                       AFS_TRACE_LEVEL_VERBOSE,
2139                       "AFSShareWrite Completed on pipe %wZ Length read %08lX\n",
2140                       &pCcb->DirectoryCB->NameInformation.FileName,
2141                       stIoResult.BytesProcessed));
2142
2143         Irp->IoStatus.Information = stIoResult.BytesProcessed;
2144
2145 try_exit:
2146
2147         if( pFcb != NULL)
2148         {
2149
2150             AFSReleaseResource( &pFcb->NPFcb->Resource);
2151         }
2152
2153         if( pIoRequest != NULL)
2154         {
2155
2156             AFSExFreePoolWithTag( pIoRequest, AFS_GENERIC_MEMORY_14_TAG);
2157         }
2158     }
2159
2160     return ntStatus;
2161 }