2 * Copyright (c) 2008, 2009, 2010, 2011 Kernel Drivers, LLC.
3 * Copyright (c) 2009, 2010, 2011 Your File System, Inc.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
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
14 * this list of conditions and the following disclaimer in the
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.
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.
39 #include "AFSCommon.h"
43 AFSCachedWrite( IN PDEVICE_OBJECT DeviceObject,
45 IN LARGE_INTEGER StartingByte,
47 IN BOOLEAN ForceFlush);
50 AFSNonCachedWrite( IN PDEVICE_OBJECT DeviceObject,
52 IN LARGE_INTEGER StartingByte,
57 AFSNonCachedWriteDirect( IN PDEVICE_OBJECT DeviceObject,
59 IN LARGE_INTEGER StartingByte,
64 AFSExtendingWrite( IN AFSFcb *Fcb,
65 IN PFILE_OBJECT FileObject,
66 IN LONGLONG NewLength);
73 // This is the dispatch handler for the IRP_MJ_WRITE request
77 // A status is returned for the function
80 AFSWrite( IN PDEVICE_OBJECT LibDeviceObject,
84 UNREFERENCED_PARAMETER(LibDeviceObject);
85 NTSTATUS ntStatus = STATUS_SUCCESS;
90 ntStatus = AFSCommonWrite( AFSRDRDeviceObject, Irp, NULL, FALSE);
92 __except( AFSExceptionFilter( __FUNCTION__, GetExceptionCode(), GetExceptionInformation()) )
95 ntStatus = STATUS_INSUFFICIENT_RESOURCES;
102 AFSCommonWrite( IN PDEVICE_OBJECT DeviceObject,
104 IN HANDLE OnBehalfOf,
108 NTSTATUS ntStatus = STATUS_SUCCESS;
109 AFSDeviceExt *pDeviceExt = (AFSDeviceExt *)DeviceObject->DeviceExtension;
110 IO_STACK_LOCATION *pIrpSp;
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;
127 HANDLE hCallingUser = OnBehalfOf;
128 ULONGLONG ullProcessId = (ULONGLONG)PsGetCurrentProcessId();
129 AFSDeviceExt *pRDRDevExt = (AFSDeviceExt *)AFSRDRDeviceObject->DeviceExtension;
131 pIrpSp = IoGetCurrentIrpStackLocation( Irp);
136 Irp->IoStatus.Information = 0;
138 pFileObject = pIrpSp->FileObject;
141 // Extract the fileobject references
144 pFcb = (AFSFcb *)pFileObject->FsContext;
145 pCcb = (AFSCcb *)pFileObject->FsContext2;
147 ObReferenceObject( pFileObject);
152 AFSDbgTrace(( AFS_SUBSYSTEM_IO_PROCESSING,
153 AFS_TRACE_LEVEL_ERROR,
154 "AFSCommonWrite Attempted write (%p) when pFcb == NULL\n",
157 try_return( ntStatus = STATUS_INVALID_DEVICE_REQUEST);
160 pNPFcb = pFcb->NPFcb;
163 // If we are in shutdown mode then fail the request
166 if( BooleanFlagOn( pDeviceExt->DeviceFlags, AFS_DEVICE_FLAG_REDIRECTOR_SHUTDOWN))
169 AFSDbgTrace(( AFS_SUBSYSTEM_FILE_PROCESSING,
170 AFS_TRACE_LEVEL_WARNING,
171 "AFSCommonWrite (%p) Open request after shutdown\n",
174 try_return( ntStatus = STATUS_TOO_LATE);
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);
183 if( pFcb->Header.NodeTypeCode != AFS_IOCTL_FCB &&
184 pFcb->Header.NodeTypeCode != AFS_FILE_FCB &&
185 pFcb->Header.NodeTypeCode != AFS_SPECIAL_SHARE_FCB)
188 AFSDbgTrace(( AFS_SUBSYSTEM_IO_PROCESSING,
189 AFS_TRACE_LEVEL_ERROR,
190 "AFSCommonWrite Attempted write (%p) on an invalid node type %08lX\n",
192 pFcb->Header.NodeTypeCode));
194 try_return( ntStatus = STATUS_INVALID_DEVICE_REQUEST);
198 // If this is a write against an IOCtl node then handle it
199 // in a different pathway
202 if( pFcb->Header.NodeTypeCode == AFS_IOCTL_FCB)
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",
209 liStartingByte.QuadPart,
213 ntStatus = AFSIOCtlWrite( DeviceObject,
216 try_return( ntStatus);
218 else if( pFcb->Header.NodeTypeCode == AFS_SPECIAL_SHARE_FCB)
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",
225 liStartingByte.QuadPart,
229 ntStatus = AFSShareWrite( DeviceObject,
232 try_return( ntStatus);
236 // Is the Cache not there yet? Exit.
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)
243 AFSDbgTrace(( AFS_SUBSYSTEM_IO_PROCESSING,
244 AFS_TRACE_LEVEL_ERROR,
245 "AFSCommonWrite (%p) Request failed due to AFS cache closed\n",
248 try_return( ntStatus = STATUS_TOO_LATE );
251 if( pFcb->ObjectInformation->VolumeCB != NULL &&
252 BooleanFlagOn( pFcb->ObjectInformation->VolumeCB->VolumeInformation.FileSystemAttributes, FILE_READ_ONLY_VOLUME))
255 AFSDbgTrace(( AFS_SUBSYSTEM_IO_PROCESSING,
256 AFS_TRACE_LEVEL_ERROR,
257 "AFSCommonWrite (%p) Request failed due to read only volume\n",
260 try_return( ntStatus = STATUS_ACCESS_DENIED);
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.
269 if( NULL == OnBehalfOf )
272 hCallingUser = PsGetCurrentProcessId();
277 hCallingUser = OnBehalfOf;
281 // Check for zero length write
284 if( ulByteCount == 0)
287 AFSDbgTrace(( AFS_SUBSYSTEM_IO_PROCESSING,
288 AFS_TRACE_LEVEL_VERBOSE,
289 "AFSCommonWrite (%p) Request completed due to zero length\n",
292 try_return( ntStatus);
296 // Is this Fcb valid???
299 if( BooleanFlagOn( pFcb->ObjectInformation->Flags, AFS_OBJECT_FLAGS_OBJECT_INVALID))
302 AFSDbgTrace(( AFS_SUBSYSTEM_IO_PROCESSING,
303 AFS_TRACE_LEVEL_ERROR,
304 "AFSCommonWrite (%p) Failing request due to INVALID fcb\n",
307 try_return( ntStatus = STATUS_FILE_DELETED);
310 if( BooleanFlagOn( pCcb->DirectoryCB->Flags, AFS_DIR_ENTRY_DELETED) ||
311 BooleanFlagOn( pFcb->ObjectInformation->Flags, AFS_OBJECT_FLAGS_DELETED))
314 AFSDbgTrace(( AFS_SUBSYSTEM_IO_PROCESSING,
315 AFS_TRACE_LEVEL_ERROR,
316 "AFSCommonWrite (%p) Request failed due to file deleted\n",
319 try_return( ntStatus = STATUS_FILE_DELETED);
322 if( FlagOn( pIrpSp->MinorFunction, IRP_MN_COMPLETE))
325 AFSDbgTrace(( AFS_SUBSYSTEM_IO_PROCESSING,
326 AFS_TRACE_LEVEL_VERBOSE,
327 "AFSCommonWrite (%p) IRP_MN_COMPLETE being processed\n",
330 CcMdlWriteComplete(pFileObject, &pIrpSp->Parameters.Write.ByteOffset, Irp->MdlAddress);
333 // Mdl is now Deallocated
336 Irp->MdlAddress = NULL;
338 try_return( ntStatus = STATUS_SUCCESS );
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
345 if( NULL != pFileObject->SectionObjectPointer->DataSectionObject && !bPagingIo && bNonCachedIo)
347 bNonCachedIo = FALSE;
351 if ( !bNonCachedIo && !bPagingIo)
354 if( pFileObject->PrivateCacheMap == NULL)
357 AFSDbgTrace(( AFS_SUBSYSTEM_LOCK_PROCESSING|AFS_SUBSYSTEM_SECTION_OBJECT,
358 AFS_TRACE_LEVEL_VERBOSE,
359 "AFSCommonWrite Acquiring Fcb SectionObject lock %p EXCL %08lX\n",
360 &pNPFcb->SectionObjectResource,
361 PsGetCurrentThread()));
363 AFSAcquireExcl( &pNPFcb->SectionObjectResource,
366 bReleaseSectionObject = TRUE;
371 AFSDbgTrace(( AFS_SUBSYSTEM_IO_PROCESSING,
372 AFS_TRACE_LEVEL_VERBOSE,
373 "AFSCommonWrite Initialize caching on Fcb %p FileObject %p\n",
377 CcInitializeCacheMap( pFileObject,
378 (PCC_FILE_SIZES)&pFcb->Header.AllocationSize,
380 AFSLibCacheManagerCallbacks,
383 CcSetReadAheadGranularity( pFileObject,
384 pDeviceExt->Specific.RDR.MaximumRPCLength);
386 CcSetDirtyPageThreshold( pFileObject,
387 AFS_DIRTY_CHUNK_THRESHOLD * pDeviceExt->Specific.RDR.MaximumRPCLength / 4096);
389 __except( EXCEPTION_EXECUTE_HANDLER)
392 ntStatus = GetExceptionCode();
394 AFSDbgTrace(( AFS_SUBSYSTEM_IO_PROCESSING,
395 AFS_TRACE_LEVEL_ERROR,
396 "AFSCommonWrite (%p) Exception thrown while initializing cache map Status %08lX\n",
401 AFSDbgTrace(( AFS_SUBSYSTEM_LOCK_PROCESSING|AFS_SUBSYSTEM_SECTION_OBJECT,
402 AFS_TRACE_LEVEL_VERBOSE,
403 "AFSCommonWrite Releasing Fcb SectionObject lock %p EXCL %08lX\n",
404 &pNPFcb->SectionObjectResource,
405 PsGetCurrentThread()));
407 AFSReleaseResource( &pNPFcb->SectionObjectResource);
409 bReleaseSectionObject = FALSE;
411 if( !NT_SUCCESS( ntStatus))
414 try_return( ntStatus);
419 // On versions of Microsoft Windows older than Vista the IO Manager
420 // will issue multiple outstanding writes on a synchronous file object
421 // if one of the cached writes completes with STATUS_PENDING. This can
422 // result in the writes being completed out of order which can corrupt
423 // the end of file marker. On OS versions older than Vista use a spin
424 // loop instead of deferring the write.
427 if ( bSynchronousFo &&
428 AFSRtlSysVersion.dwMajorVersion < 6)
431 while (!CcCanIWrite( pFileObject,
436 static const LONGLONG llWriteDelay = (LONGLONG)-100000;
439 AFSDbgLogMsg( AFS_SUBSYSTEM_IO_PROCESSING,
440 AFS_TRACE_LEVEL_WARNING,
441 "AFSCommonWrite (FO: %p) CcCanIWrite says No room for %u bytes! Retry in 10ms\n",
445 KeDelayExecutionThread(KernelMode, FALSE, (PLARGE_INTEGER)&llWriteDelay);
451 if (!CcCanIWrite( pFileObject,
457 AFSDbgTrace(( AFS_SUBSYSTEM_IO_PROCESSING,
458 AFS_TRACE_LEVEL_WARNING,
459 "AFSCommonWrite (FO: %p) CcCanIWrite says No room for Offset %0I64X Length %08lX bytes! Deferring%s\n",
461 liStartingByte.QuadPart,
463 bRetry ? " RETRY" : ""));
465 ntStatus = AFSDeferWrite( DeviceObject, pFileObject, hCallingUser, Irp, ulByteCount, bRetry);
467 if ( STATUS_PENDING == ntStatus)
470 bCompleteIrp = FALSE;
475 AFSDbgTrace(( AFS_SUBSYSTEM_IO_PROCESSING,
476 AFS_TRACE_LEVEL_ERROR,
477 "AFSCommonWrite (FO: %p) AFSDeferWrite failure Status %08lX\n",
482 try_return( ntStatus);
488 // Save off the PID if this is not a paging IO
492 ( pFcb->Specific.File.ExtentRequestProcessId == 0 ||
493 ( ullProcessId != (ULONGLONG)AFSSysProcess &&
494 pFcb->Specific.File.ExtentRequestProcessId != ullProcessId)))
497 pFcb->Specific.File.ExtentRequestProcessId = ullProcessId;
499 if( ullProcessId == (ULONGLONG)AFSSysProcess)
501 AFSDbgTrace(( AFS_SUBSYSTEM_EXTENT_PROCESSING,
502 AFS_TRACE_LEVEL_WARNING,
503 "%s Setting LastWriterExtentProcessId to system process for Fcb %p\n",
512 // - if Paging then we need to do nothing (the precalls will
513 // have acquired the paging resource), for clarity we will collect
514 // the paging resource
515 // - If extending Write then take the fileresource EX (EOF will change, Allocation will only move out)
516 // - Otherwise we collect the file shared, check against extending and
527 //ASSERT( NULL != OnBehalfOf || ExIsResourceAcquiredLite( &pNPFcb->Resource ));
529 AFSDbgTrace(( AFS_SUBSYSTEM_LOCK_PROCESSING,
530 AFS_TRACE_LEVEL_VERBOSE,
531 "AFSCommonWrite Acquiring Fcb PagingIo lock %p SHARED %08lX\n",
532 &pNPFcb->PagingResource,
533 PsGetCurrentThread()));
535 AFSAcquireShared( &pNPFcb->PagingResource,
538 bReleasePaging = TRUE;
541 // We have the correct lock - we cannot have the wrong one
548 bExtendingWrite = (((liStartingByte.QuadPart + ulByteCount) >=
549 pFcb->Header.FileSize.QuadPart) ||
550 (liStartingByte.LowPart == FILE_WRITE_TO_END_OF_FILE &&
551 liStartingByte.HighPart == -1)) ;
553 if( bExtendingWrite || bNonCachedIo)
556 // Check for lock inversion
560 // For bExtendingWrite the PagingResource is needed to protect
561 // the CcSetFileSizes call in AFSExtendingWrite
564 ASSERT( !ExIsResourceAcquiredLite( &pNPFcb->PagingResource ));
566 AFSDbgTrace(( AFS_SUBSYSTEM_LOCK_PROCESSING,
567 AFS_TRACE_LEVEL_VERBOSE,
568 "AFSCommonWrite Acquiring Fcb lock %p EXCL %08lX\n",
570 PsGetCurrentThread()));
572 AFSAcquireExcl( &pNPFcb->Resource,
577 AFSDbgTrace(( AFS_SUBSYSTEM_LOCK_PROCESSING|AFS_SUBSYSTEM_SECTION_OBJECT,
578 AFS_TRACE_LEVEL_VERBOSE,
579 "AFSCommonWrite Acquiring Fcb SectionObject lock %p EXCL %08lX\n",
580 &pNPFcb->SectionObjectResource,
581 PsGetCurrentThread()));
583 AFSAcquireExcl( &pNPFcb->SectionObjectResource,
586 bReleaseSectionObject = TRUE;
588 if (liStartingByte.LowPart == FILE_WRITE_TO_END_OF_FILE &&
589 liStartingByte.HighPart == -1)
591 if (pFcb->Header.ValidDataLength.QuadPart > pFcb->Header.FileSize.QuadPart)
593 liStartingByte = pFcb->Header.ValidDataLength;
597 liStartingByte = pFcb->Header.FileSize;
602 // We have the correct lock - even if we don't end up truncating
608 ASSERT( !ExIsResourceAcquiredLite( &pNPFcb->PagingResource ));
610 AFSDbgTrace(( AFS_SUBSYSTEM_LOCK_PROCESSING,
611 AFS_TRACE_LEVEL_VERBOSE,
612 "AFSCommonWrite Acquiring Fcb lock %p SHARED %08lX\n",
614 PsGetCurrentThread()));
616 AFSAcquireShared( &pNPFcb->Resource,
621 AFSDbgTrace(( AFS_SUBSYSTEM_LOCK_PROCESSING|AFS_SUBSYSTEM_SECTION_OBJECT,
622 AFS_TRACE_LEVEL_VERBOSE,
623 "AFSCommonWrite Acquiring Fcb SectionObject lock %p SHARED %08lX\n",
624 &pNPFcb->SectionObjectResource,
625 PsGetCurrentThread()));
627 AFSAcquireShared( &pNPFcb->SectionObjectResource,
630 bReleaseSectionObject = TRUE;
633 // Have things moved? Are we extending? If so, the the lock isn't OK
635 bLockOK = (liStartingByte.QuadPart + ulByteCount) < pFcb->Header.FileSize.QuadPart;
640 AFSDbgTrace(( AFS_SUBSYSTEM_LOCK_PROCESSING|AFS_SUBSYSTEM_SECTION_OBJECT,
641 AFS_TRACE_LEVEL_VERBOSE,
642 "AFSCommonWrite Releasing Fcb SectionObject lock %p SHARED %08lX\n",
643 &pNPFcb->SectionObjectResource,
644 PsGetCurrentThread()));
646 AFSReleaseResource( &pNPFcb->SectionObjectResource);
648 bReleaseSectionObject = FALSE;
650 AFSReleaseResource( &pNPFcb->Resource);
652 bReleaseMain = FALSE;
663 // Check the BR locks on the file.
666 if ( !FsRtlCheckLockForWriteAccess( &pFcb->Specific.File.FileLock,
670 AFSDbgTrace(( AFS_SUBSYSTEM_IO_PROCESSING,
671 AFS_TRACE_LEVEL_ERROR,
672 "AFSCommonWrite (%p) Request failed due to lock conflict\n",
675 try_return( ntStatus = STATUS_FILE_LOCK_CONFLICT);
681 ntStatus = AFSExtendingWrite( pFcb, pFileObject, (liStartingByte.QuadPart + ulByteCount));
684 // Fcb->NPFcb->Resource is now held SHARED
687 if( !NT_SUCCESS(ntStatus))
690 AFSDbgTrace(( AFS_SUBSYSTEM_IO_PROCESSING,
691 AFS_TRACE_LEVEL_ERROR,
692 "AFSCommonWrite (%p) Failed extending write request Status %08lX\n",
696 try_return( ntStatus );
702 // Fire off the request as appropriate
704 bCompleteIrp = FALSE;
711 // Main resource held Shared
712 // SectionObject resource held exclusive if extending write
715 AFSDbgTrace(( AFS_SUBSYSTEM_IO_PROCESSING,
716 AFS_TRACE_LEVEL_VERBOSE,
717 "AFSCommonWrite (%p) Processing CACHED request Offset %0I64X Len %08lX%s\n",
719 liStartingByte.QuadPart,
721 bRetry ? " RETRY" : ""));
723 ntStatus = AFSCachedWrite( DeviceObject, Irp, liStartingByte, ulByteCount, bForceFlush);
728 AFSDbgTrace(( AFS_SUBSYSTEM_IO_PROCESSING,
729 AFS_TRACE_LEVEL_VERBOSE,
730 "AFSCommonWrite (%p) Processing NON-CACHED request Offset %0I64X Len %08lX%s\n",
732 liStartingByte.QuadPart,
734 bRetry ? " RETRY" : ""));
736 if( BooleanFlagOn( pRDRDevExt->DeviceFlags, AFS_DEVICE_FLAG_DIRECT_SERVICE_IO))
739 ntStatus = AFSNonCachedWriteDirect( DeviceObject, Irp, liStartingByte, ulByteCount);
743 ntStatus = AFSNonCachedWrite( DeviceObject, Irp, liStartingByte, ulByteCount);
749 AFSDbgTrace(( AFS_SUBSYSTEM_IO_PROCESSING,
750 AFS_TRACE_LEVEL_VERBOSE,
751 "AFSCommonWrite (%p) Process complete Status %08lX\n",
755 if ( NT_SUCCESS( ntStatus))
763 pFileObject->CurrentByteOffset.QuadPart = liStartingByte.QuadPart + ulByteCount;
767 // If this extended the VDL, then update it accordingly.
768 // Increasing the VDL does not require a call to CcSetFileSizes.
771 if( liStartingByte.QuadPart + ulByteCount > pFcb->Header.ValidDataLength.QuadPart)
774 pFcb->Header.ValidDataLength.QuadPart = liStartingByte.QuadPart + ulByteCount;
777 if( !BooleanFlagOn( pFcb->Flags, AFS_FCB_FLAG_UPDATE_LAST_WRITE_TIME))
780 SetFlag( pFcb->Flags, AFS_FCB_FLAG_UPDATE_WRITE_TIME);
782 KeQuerySystemTime( &pFcb->ObjectInformation->LastWriteTime);
787 if ( !bPagingIo && bNonCachedIo && CcIsFileCached( pFileObject) &&
788 pNPFcb->SectionObjectPointers.DataSectionObject != NULL &&
789 bReleaseSectionObject)
792 // Regardless of whether or not the a non-paging non-cached write
793 // succeeds or fails, if the file is cached the contents of the
794 // cache are no longer up to date. A CcPurgeCacheSection must be
795 // performed to force subsequent cached reads to obtain the data
798 // The Fcb Resource is dropped in order to permit filters that perform
799 // an open via a worker thread in response to a purge to do so without
800 // deadlocking. The SectionObjectResource is held across the purge to
801 // prevent racing with other cache operations.
807 AFSReleaseResource( &pNPFcb->Resource);
809 bReleaseMain = FALSE;
815 if ( !CcPurgeCacheSection( &pNPFcb->SectionObjectPointers,
821 AFSDbgTrace(( AFS_SUBSYSTEM_IO_PROCESSING,
822 AFS_TRACE_LEVEL_WARNING,
823 "AFSCommonWrite CcPurgeCacheSection failure FID %08lX-%08lX-%08lX-%08lX\n",
824 pFcb->ObjectInformation->FileId.Cell,
825 pFcb->ObjectInformation->FileId.Volume,
826 pFcb->ObjectInformation->FileId.Vnode,
827 pFcb->ObjectInformation->FileId.Unique));
829 SetFlag( pFcb->Flags, AFS_FCB_FLAG_PURGE_ON_CLOSE);
832 __except( EXCEPTION_EXECUTE_HANDLER)
835 DWORD ntStatus2 = GetExceptionCode();
839 "EXCEPTION - AFSCommonWrite CcPurgeCacheSection failed FID %08lX-%08lX-%08lX-%08lX Status 0x%08lX\n",
840 pFcb->ObjectInformation->FileId.Cell,
841 pFcb->ObjectInformation->FileId.Volume,
842 pFcb->ObjectInformation->FileId.Vnode,
843 pFcb->ObjectInformation->FileId.Unique,
846 SetFlag( pFcb->Flags, AFS_FCB_FLAG_PURGE_ON_CLOSE);
850 ObDereferenceObject(pFileObject);
852 if( bReleaseSectionObject)
855 AFSDbgTrace(( AFS_SUBSYSTEM_LOCK_PROCESSING|AFS_SUBSYSTEM_SECTION_OBJECT,
856 AFS_TRACE_LEVEL_VERBOSE,
857 "AFSCommonWrite Releasing Fcb SectionObject lock %p EXCL/SHARED %08lX\n",
858 &pNPFcb->SectionObjectResource,
859 PsGetCurrentThread()));
861 AFSReleaseResource( &pNPFcb->SectionObjectResource);
867 AFSReleaseResource( &pNPFcb->PagingResource);
873 AFSReleaseResource( &pNPFcb->Resource);
879 AFSCompleteRequest( Irp,
888 AFSIOCtlWrite( IN PDEVICE_OBJECT DeviceObject,
892 UNREFERENCED_PARAMETER(DeviceObject);
893 NTSTATUS ntStatus = STATUS_SUCCESS;
894 AFSPIOCtlIORequestCB stIORequestCB;
895 PIO_STACK_LOCATION pIrpSp = IoGetCurrentIrpStackLocation( Irp);
898 AFSPIOCtlIOResultCB stIOResultCB;
899 ULONG ulBytesReturned = 0;
900 AFSFileID stParentFID;
905 Irp->IoStatus.Information = 0;
907 RtlZeroMemory( &stIORequestCB,
908 sizeof( AFSPIOCtlIORequestCB));
910 if( pIrpSp->Parameters.Write.Length == 0)
914 // Nothing to do in this case
917 try_return( ntStatus);
920 pFcb = (AFSFcb *)pIrpSp->FileObject->FsContext;
922 pCcb = (AFSCcb *)pIrpSp->FileObject->FsContext2;
924 AFSDbgTrace(( AFS_SUBSYSTEM_LOCK_PROCESSING,
925 AFS_TRACE_LEVEL_VERBOSE,
926 "AFSIOCtlWrite Acquiring Fcb lock %p SHARED %08lX\n",
927 &pFcb->NPFcb->Resource,
928 PsGetCurrentThread()));
930 AFSAcquireShared( &pFcb->NPFcb->Resource,
934 // Get the parent fid to pass to the cm
937 RtlZeroMemory( &stParentFID,
940 if( BooleanFlagOn( pFcb->ObjectInformation->Flags, AFS_OBJECT_FLAGS_PARENT_FID))
944 // The parent directory FID of the node
947 stParentFID = pFcb->ObjectInformation->ParentFileId;
951 // Set the control block up
954 stIORequestCB.RequestId = pCcb->RequestID;
956 if( pFcb->ObjectInformation->VolumeCB != NULL)
958 stIORequestCB.RootId = pFcb->ObjectInformation->VolumeCB->ObjectInformation.FileId;
962 // Lock down the buffer
965 stIORequestCB.MappedBuffer = AFSMapToService( Irp,
966 pIrpSp->Parameters.Write.Length);
968 if( stIORequestCB.MappedBuffer == NULL)
971 try_return( ntStatus = STATUS_INSUFFICIENT_RESOURCES);
974 stIORequestCB.BufferLength = pIrpSp->Parameters.Write.Length;
976 stIOResultCB.BytesProcessed = 0;
978 ulBytesReturned = sizeof( AFSPIOCtlIOResultCB);
981 // Issue the request to the service
984 ntStatus = AFSProcessRequest( AFS_REQUEST_TYPE_PIOCTL_WRITE,
985 AFS_REQUEST_FLAG_SYNCHRONOUS,
991 (void *)&stIORequestCB,
992 sizeof( AFSPIOCtlIORequestCB),
996 if( !NT_SUCCESS( ntStatus))
999 try_return( ntStatus);
1003 // Update the length written
1006 Irp->IoStatus.Information = stIOResultCB.BytesProcessed;
1010 if( stIORequestCB.MappedBuffer != NULL)
1013 AFSUnmapServiceMappedBuffer( stIORequestCB.MappedBuffer,
1020 AFSReleaseResource( &pFcb->NPFcb->Resource);
1028 // This function is called when we know we have to read from the AFS Cache.
1030 // It ensures that we have exents for the entirety of the write and
1031 // then pins the extents into memory (meaning that although we may
1032 // add we will not remove). Then it creates a scatter gather write
1033 // and fires off the IRPs
1037 AFSNonCachedWrite( IN PDEVICE_OBJECT DeviceObject,
1039 IN LARGE_INTEGER StartingByte,
1042 NTSTATUS ntStatus = STATUS_UNSUCCESSFUL;
1043 VOID *pSystemBuffer = NULL;
1044 BOOLEAN bPagingIo = BooleanFlagOn( Irp->Flags, IRP_PAGING_IO);
1045 BOOLEAN bLocked = FALSE;
1046 BOOLEAN bCompleteIrp = TRUE;
1047 AFSGatherIo *pGatherIo = NULL;
1048 AFSIoRun *pIoRuns = NULL;
1049 AFSIoRun stIoRuns[AFS_MAX_STACK_IO_RUNS];
1050 ULONG extentsCount = 0, runCount = 0;
1051 AFSExtent *pStartExtent = NULL;
1052 AFSExtent *pIgnoreExtent = NULL;
1053 IO_STACK_LOCATION *pIrpSp = IoGetCurrentIrpStackLocation( Irp);
1054 PFILE_OBJECT pFileObject = pIrpSp->FileObject;
1055 AFSFcb *pFcb = (AFSFcb *)pFileObject->FsContext;
1056 AFSCcb *pCcb = (AFSCcb *)pFileObject->FsContext2;
1057 BOOLEAN bSynchronousFo = BooleanFlagOn( pFileObject->Flags, FO_SYNCHRONOUS_IO);
1058 AFSDeviceExt *pDevExt = (AFSDeviceExt *)DeviceObject->DeviceExtension;
1059 LARGE_INTEGER liCurrentTime, liLastRequestTime;
1060 AFSDeviceExt *pControlDevExt = (AFSDeviceExt *)AFSControlDeviceObject->DeviceExtension;
1061 PFILE_OBJECT pCacheFileObject = NULL;
1062 BOOLEAN bDerefExtents = FALSE;
1067 AFSDbgTrace(( AFS_SUBSYSTEM_IO_PROCESSING,
1068 AFS_TRACE_LEVEL_VERBOSE,
1069 "AFSNonCachedWrite (FO: %p) StartingByte %08lX:%08lX Length %08lX\n",
1071 StartingByte.HighPart,
1072 StartingByte.LowPart,
1075 if (ByteCount > pDevExt->Specific.RDR.MaxIo.QuadPart)
1078 AFSDbgTrace(( AFS_SUBSYSTEM_IO_PROCESSING,
1079 AFS_TRACE_LEVEL_ERROR,
1080 "AFSNonCachedWrite (%p) Request %08lX Actual %08lX larger than MaxIO %I64X\n",
1083 pIrpSp->Parameters.Write.Length,
1084 pDevExt->Specific.RDR.MaxIo.QuadPart));
1086 try_return( ntStatus = STATUS_UNSUCCESSFUL);
1090 // Get the mapping for the buffer
1092 pSystemBuffer = AFSLockSystemBuffer( Irp,
1095 if( pSystemBuffer == NULL)
1098 AFSDbgTrace(( AFS_SUBSYSTEM_IO_PROCESSING,
1099 AFS_TRACE_LEVEL_ERROR,
1100 "AFSNonCachedWrite (%p) Failed to map system buffer\n",
1103 try_return( ntStatus = STATUS_INSUFFICIENT_RESOURCES);
1108 // Provoke a get of the extents - if we need to.
1111 AFSDbgTrace(( AFS_SUBSYSTEM_EXTENT_PROCESSING,
1112 AFS_TRACE_LEVEL_VERBOSE,
1113 "AFSNonCachedWrite Requesting extents for fid %08lX-%08lX-%08lX-%08lX Offset %0I64X Length %08lX\n",
1114 pFcb->ObjectInformation->FileId.Cell,
1115 pFcb->ObjectInformation->FileId.Volume,
1116 pFcb->ObjectInformation->FileId.Vnode,
1117 pFcb->ObjectInformation->FileId.Unique,
1118 StartingByte.QuadPart,
1121 ntStatus = AFSRequestExtentsAsync( pFcb,
1126 if (!NT_SUCCESS(ntStatus))
1129 AFSDbgTrace(( AFS_SUBSYSTEM_IO_PROCESSING,
1130 AFS_TRACE_LEVEL_ERROR,
1131 "AFSNonCachedWrite (%p) Failed to request extents Status %08lX\n",
1135 try_return( ntStatus);
1138 KeQueryTickCount( &liLastRequestTime);
1143 AFSDbgTrace(( AFS_SUBSYSTEM_LOCK_PROCESSING,
1144 AFS_TRACE_LEVEL_VERBOSE,
1145 "AFSNonCachedWrite Acquiring Fcb extents lock %p SHARED %08lX\n",
1146 &pFcb->NPFcb->Specific.File.ExtentsResource,
1147 PsGetCurrentThread()));
1149 ASSERT( !ExIsResourceAcquiredLite( &pFcb->NPFcb->Specific.File.ExtentsResource ));
1151 AFSAcquireShared( &pFcb->NPFcb->Specific.File.ExtentsResource, TRUE );
1154 pStartExtent = NULL;
1155 pIgnoreExtent = NULL;
1157 if ( AFSDoExtentsMapRegion( pFcb, &StartingByte, ByteCount, &pStartExtent, &pIgnoreExtent ))
1162 KeClearEvent( &pFcb->NPFcb->Specific.File.ExtentsRequestComplete );
1164 AFSDbgTrace(( AFS_SUBSYSTEM_LOCK_PROCESSING,
1165 AFS_TRACE_LEVEL_VERBOSE,
1166 "AFSNonCachedWrite Releasing(1) Fcb extents lock %p SHARED %08lX\n",
1167 &pFcb->NPFcb->Specific.File.ExtentsResource,
1168 PsGetCurrentThread()));
1170 AFSReleaseResource( &pFcb->NPFcb->Specific.File.ExtentsResource );
1174 // We will re-request the extents after waiting for them
1177 KeQueryTickCount( &liCurrentTime);
1179 if( liCurrentTime.QuadPart - liLastRequestTime.QuadPart >= pControlDevExt->Specific.Control.ExtentRequestTimeCount.QuadPart)
1182 AFSDbgTrace(( AFS_SUBSYSTEM_EXTENT_PROCESSING,
1183 AFS_TRACE_LEVEL_VERBOSE,
1184 "AFSNonCachedWrite Requesting extents, again, for fid %08lX-%08lX-%08lX-%08lX Offset %0I64X Length %08lX\n",
1185 pFcb->ObjectInformation->FileId.Cell,
1186 pFcb->ObjectInformation->FileId.Volume,
1187 pFcb->ObjectInformation->FileId.Vnode,
1188 pFcb->ObjectInformation->FileId.Unique,
1189 StartingByte.QuadPart,
1192 ntStatus = AFSRequestExtentsAsync( pFcb,
1197 if (!NT_SUCCESS(ntStatus))
1200 AFSDbgTrace(( AFS_SUBSYSTEM_IO_PROCESSING,
1201 AFS_TRACE_LEVEL_ERROR,
1202 "AFSNonCachedWrite (%p) Failed to request extents Status %08lX\n",
1206 try_return( ntStatus);
1209 KeQueryTickCount( &liLastRequestTime);
1213 AFSDbgTrace(( AFS_SUBSYSTEM_EXTENT_PROCESSING,
1214 AFS_TRACE_LEVEL_VERBOSE,
1215 "AFSNonCachedWrite Waiting for extents for fid %08lX-%08lX-%08lX-%08lX Offset %0I64X Length %08lX\n",
1216 pFcb->ObjectInformation->FileId.Cell,
1217 pFcb->ObjectInformation->FileId.Volume,
1218 pFcb->ObjectInformation->FileId.Vnode,
1219 pFcb->ObjectInformation->FileId.Unique,
1220 StartingByte.QuadPart,
1227 ntStatus = AFSWaitForExtentMapping ( pFcb, pCcb);
1229 if (!NT_SUCCESS(ntStatus))
1232 AFSDbgTrace(( AFS_SUBSYSTEM_EXTENT_PROCESSING,
1233 AFS_TRACE_LEVEL_ERROR,
1234 "AFSNonCachedWrite Failed wait for extents for fid %08lX-%08lX-%08lX-%08lX Offset %0I64X Length %08lX Status %08lX\n",
1235 pFcb->ObjectInformation->FileId.Cell,
1236 pFcb->ObjectInformation->FileId.Volume,
1237 pFcb->ObjectInformation->FileId.Vnode,
1238 pFcb->ObjectInformation->FileId.Unique,
1239 StartingByte.QuadPart,
1243 try_return( ntStatus);
1248 // As per the read path -
1251 AFSDbgTrace(( AFS_SUBSYSTEM_EXTENT_PROCESSING,
1252 AFS_TRACE_LEVEL_VERBOSE,
1253 "AFSNonCachedWrite Extents located for fid %08lX-%08lX-%08lX-%08lX Offset %0I64X Length %08lX\n",
1254 pFcb->ObjectInformation->FileId.Cell,
1255 pFcb->ObjectInformation->FileId.Volume,
1256 pFcb->ObjectInformation->FileId.Vnode,
1257 pFcb->ObjectInformation->FileId.Unique,
1258 StartingByte.QuadPart,
1261 ntStatus = AFSGetExtents( pFcb,
1268 if (!NT_SUCCESS(ntStatus))
1271 AFSDbgTrace(( AFS_SUBSYSTEM_IO_PROCESSING,
1272 AFS_TRACE_LEVEL_ERROR,
1273 "AFSNonCachedWrite (%p) Failed to retrieve mapped extents Status %08lX\n",
1277 try_return( ntStatus );
1280 AFSDbgTrace(( AFS_SUBSYSTEM_IO_PROCESSING,
1281 AFS_TRACE_LEVEL_VERBOSE,
1282 "AFSNonCachedWrite (%p) Successfully retrieved map extents count %d run count %d\n",
1287 if( BooleanFlagOn( AFSLibControlFlags, AFS_REDIR_LIB_FLAGS_NONPERSISTENT_CACHE))
1290 Irp->IoStatus.Information = ByteCount;
1294 // Setup the MD5 for each extent
1297 AFSSetupMD5Hash( pFcb,
1305 ntStatus = AFSProcessExtentRun( pSystemBuffer,
1311 if (!NT_SUCCESS(ntStatus))
1314 AFSDbgTrace(( AFS_SUBSYSTEM_IO_PROCESSING,
1315 AFS_TRACE_LEVEL_ERROR,
1316 "AFSNonCachedWrite (%p) Failed to process extent run for non-persistent cache Status %08lX\n",
1321 try_return( ntStatus);
1325 // Retrieve the cache file object
1328 pCacheFileObject = AFSReferenceCacheFileObject();
1330 if( pCacheFileObject == NULL)
1333 ntStatus = STATUS_DEVICE_NOT_READY;
1335 AFSDbgTrace(( AFS_SUBSYSTEM_EXTENT_PROCESSING,
1336 AFS_TRACE_LEVEL_ERROR,
1337 "AFSNonCachedWrite Failed to retrieve cache fileobject for fid %08lX-%08lX-%08lX-%08lX Offset %0I64X Length %08lX Status %08lX\n",
1338 pFcb->ObjectInformation->FileId.Cell,
1339 pFcb->ObjectInformation->FileId.Volume,
1340 pFcb->ObjectInformation->FileId.Vnode,
1341 pFcb->ObjectInformation->FileId.Unique,
1342 StartingByte.QuadPart,
1346 try_return( ntStatus);
1349 if (runCount > AFS_MAX_STACK_IO_RUNS)
1352 pIoRuns = (AFSIoRun*) AFSExAllocatePoolWithTag( PagedPool,
1353 runCount * sizeof( AFSIoRun ),
1355 if (NULL == pIoRuns)
1358 AFSDbgTrace(( AFS_SUBSYSTEM_IO_PROCESSING,
1359 AFS_TRACE_LEVEL_ERROR,
1360 "AFSNonCachedWrite (%p) Failed to allocate IO run block\n",
1363 try_return( ntStatus = STATUS_INSUFFICIENT_RESOURCES );
1372 RtlZeroMemory( pIoRuns, runCount * sizeof( AFSIoRun ));
1374 ntStatus = AFSSetupIoRun( IoGetRelatedDeviceObject( pCacheFileObject),
1383 if (!NT_SUCCESS(ntStatus))
1386 AFSDbgTrace(( AFS_SUBSYSTEM_IO_PROCESSING,
1387 AFS_TRACE_LEVEL_ERROR,
1388 "AFSNonCachedWrite (%p) Failed to initialize IO run block Status %08lX\n",
1392 try_return( ntStatus );
1395 AFSReferenceActiveExtents( pStartExtent,
1398 bDerefExtents = TRUE;
1400 AFSDbgTrace(( AFS_SUBSYSTEM_LOCK_PROCESSING,
1401 AFS_TRACE_LEVEL_VERBOSE,
1402 "AFSNonCachedWrite Releasing(2) Fcb extents lock %p SHARED %08lX\n",
1403 &pFcb->NPFcb->Specific.File.ExtentsResource,
1404 PsGetCurrentThread()));
1406 AFSReleaseResource( &pFcb->NPFcb->Specific.File.ExtentsResource );
1409 pGatherIo = (AFSGatherIo*) AFSExAllocatePoolWithTag( NonPagedPool,
1410 sizeof( AFSGatherIo),
1413 if (NULL == pGatherIo)
1416 AFSDbgTrace(( AFS_SUBSYSTEM_IO_PROCESSING,
1417 AFS_TRACE_LEVEL_ERROR,
1418 "AFSNonCachedWrite (%p) Failed to allocate IO gather block\n",
1421 AFSDbgTrace(( AFS_SUBSYSTEM_LOCK_PROCESSING,
1422 AFS_TRACE_LEVEL_VERBOSE,
1423 "AFSNonCachedWrite Acquiring(1) Fcb extents lock %p SHARED %08lX\n",
1424 &pFcb->NPFcb->Specific.File.ExtentsResource,
1425 PsGetCurrentThread()));
1427 AFSAcquireShared( &pFcb->NPFcb->Specific.File.ExtentsResource,
1431 AFSDereferenceActiveExtents( pStartExtent,
1434 try_return (ntStatus = STATUS_INSUFFICIENT_RESOURCES );
1437 RtlZeroMemory( pGatherIo, sizeof( AFSGatherIo ));
1440 // Initialize count to 1, that was we won't get an early
1441 // completion if the first irp completes before the second is
1444 pGatherIo->Count = 1;
1445 pGatherIo->Status = STATUS_SUCCESS;
1446 pGatherIo->MasterIrp = Irp;
1447 pGatherIo->Synchronous = TRUE;
1448 pGatherIo->CompleteMasterIrp = FALSE;
1450 bCompleteIrp = TRUE;
1452 if( pGatherIo->Synchronous)
1454 KeInitializeEvent( &pGatherIo->Event, NotificationEvent, FALSE );
1459 // Setup the MD5 for each extent
1462 AFSSetupMD5Hash( pFcb,
1471 // Pre-emptively set up the count
1474 Irp->IoStatus.Information = ByteCount;
1476 ntStatus = AFSQueueStartIos( pCacheFileObject,
1478 IRP_WRITE_OPERATION | IRP_SYNCHRONOUS_API,
1483 AFSDbgTrace(( AFS_SUBSYSTEM_IO_PROCESSING,
1484 AFS_TRACE_LEVEL_VERBOSE,
1485 "AFSNonCachedWrite (%p) AFSStartIos completed Status %08lX\n",
1489 if( !NT_SUCCESS( ntStatus))
1492 AFSDbgTrace(( AFS_SUBSYSTEM_LOCK_PROCESSING,
1493 AFS_TRACE_LEVEL_VERBOSE,
1494 "AFSNonCachedWrite Acquiring(2) Fcb extents lock %p SHARED %08lX\n",
1495 &pFcb->NPFcb->Specific.File.ExtentsResource,
1496 PsGetCurrentThread()));
1498 AFSAcquireShared( &pFcb->NPFcb->Specific.File.ExtentsResource,
1502 AFSDereferenceActiveExtents( pStartExtent,
1505 try_return( ntStatus);
1509 // Wait for completion of All IOs we started.
1512 ntStatus = KeWaitForSingleObject( &pGatherIo->Event,
1518 if( NT_SUCCESS( ntStatus))
1521 ntStatus = pGatherIo->Status;
1524 if( !NT_SUCCESS( ntStatus))
1527 AFSDbgTrace(( AFS_SUBSYSTEM_LOCK_PROCESSING,
1528 AFS_TRACE_LEVEL_VERBOSE,
1529 "AFSNonCachedWrite Acquiring(3) Fcb extents lock %p SHARED %08lX\n",
1530 &pFcb->NPFcb->Specific.File.ExtentsResource,
1531 PsGetCurrentThread()));
1533 AFSAcquireShared( &pFcb->NPFcb->Specific.File.ExtentsResource,
1537 AFSDereferenceActiveExtents( pStartExtent,
1540 try_return( ntStatus);
1545 if( NT_SUCCESS( ntStatus) &&
1546 pStartExtent != NULL &&
1547 Irp->IoStatus.Information > 0)
1553 AFSAcquireShared( &pFcb->NPFcb->Specific.File.ExtentsResource,
1559 // Since this is dirty we can mark the extents dirty now.
1560 // AFSMarkDirty will dereference the extents. Do not call
1561 // AFSDereferenceActiveExtents() in this code path.
1573 // This was an uncached user write - tell the server to do
1574 // the flush when the worker thread next wakes up
1576 pFcb->Specific.File.LastServerFlush.QuadPart = 0;
1580 if( pCacheFileObject != NULL)
1582 AFSReleaseCacheFileObject( pCacheFileObject);
1585 AFSDbgTrace(( AFS_SUBSYSTEM_IO_PROCESSING,
1586 AFS_TRACE_LEVEL_VERBOSE,
1587 "AFSNonCachedWrite (FO: %p) StartingByte %08lX:%08lX Length %08lX Status %08lX\n",
1589 StartingByte.HighPart,
1590 StartingByte.LowPart,
1594 if (NT_SUCCESS(ntStatus) &&
1599 pFileObject->CurrentByteOffset.QuadPart = StartingByte.QuadPart + ByteCount;
1605 AFSDbgTrace(( AFS_SUBSYSTEM_LOCK_PROCESSING,
1606 AFS_TRACE_LEVEL_VERBOSE,
1607 "AFSNonCachedWrite Releasing Fcb extents lock %p SHARED %08lX\n",
1608 &pFcb->NPFcb->Specific.File.ExtentsResource,
1609 PsGetCurrentThread()));
1611 AFSReleaseResource( &pFcb->NPFcb->Specific.File.ExtentsResource );
1616 AFSExFreePoolWithTag(pGatherIo, AFS_GATHER_TAG);
1619 if( NULL != pIoRuns &&
1620 stIoRuns != pIoRuns)
1622 AFSExFreePoolWithTag(pIoRuns, AFS_IO_RUN_TAG);
1628 AFSDbgTrace(( AFS_SUBSYSTEM_IO_PROCESSING,
1629 AFS_TRACE_LEVEL_VERBOSE,
1630 "AFSNonCachedWrite Completing Irp %p Status %08lX Info %08lX\n",
1633 Irp->IoStatus.Information));
1635 AFSCompleteRequest( Irp, ntStatus);
1644 AFSNonCachedWriteDirect( IN PDEVICE_OBJECT DeviceObject,
1646 IN LARGE_INTEGER StartingByte,
1649 NTSTATUS ntStatus = STATUS_UNSUCCESSFUL;
1650 VOID *pSystemBuffer = NULL;
1651 BOOLEAN bPagingIo = BooleanFlagOn( Irp->Flags, IRP_PAGING_IO);
1652 IO_STACK_LOCATION *pIrpSp = IoGetCurrentIrpStackLocation( Irp);
1653 PFILE_OBJECT pFileObject = pIrpSp->FileObject;
1654 AFSFcb *pFcb = (AFSFcb *)pFileObject->FsContext;
1655 AFSCcb *pCcb = (AFSCcb *)pFileObject->FsContext2;
1656 BOOLEAN bSynchronousFo = BooleanFlagOn( pFileObject->Flags, FO_SYNCHRONOUS_IO);
1657 BOOLEAN bNoIntermediateBuffering = BooleanFlagOn( pFileObject->Flags, FO_NO_INTERMEDIATE_BUFFERING);
1658 AFSDeviceExt *pDevExt = (AFSDeviceExt *)DeviceObject->DeviceExtension;
1659 AFSFileIOCB stFileIORequest;
1660 AFSFileIOResultCB stFileIOResult;
1661 ULONG ulResultLen = 0;
1666 Irp->IoStatus.Information = 0;
1668 AFSDbgTrace(( AFS_SUBSYSTEM_IO_PROCESSING,
1669 AFS_TRACE_LEVEL_VERBOSE,
1670 "AFSNonCachedWriteDirect (FO: %p) StartingByte %08lX:%08lX Length %08lX\n",
1672 StartingByte.HighPart,
1673 StartingByte.LowPart,
1676 if (ByteCount > pDevExt->Specific.RDR.MaxIo.QuadPart)
1679 AFSDbgTrace(( AFS_SUBSYSTEM_IO_PROCESSING,
1680 AFS_TRACE_LEVEL_ERROR,
1681 "AFSNonCachedWriteDirect (%p) Request %08lX Actual %08lX larger than MaxIO %I64X\n",
1684 pIrpSp->Parameters.Write.Length,
1685 pDevExt->Specific.RDR.MaxIo.QuadPart));
1687 try_return( ntStatus = STATUS_UNSUCCESSFUL);
1691 // Get the mapping for the buffer
1693 pSystemBuffer = AFSLockSystemBuffer( Irp,
1696 if( pSystemBuffer == NULL)
1699 AFSDbgTrace(( AFS_SUBSYSTEM_IO_PROCESSING,
1700 AFS_TRACE_LEVEL_ERROR,
1701 "AFSNonCachedWriteDirect (%p) Failed to map system buffer\n",
1704 try_return( ntStatus = STATUS_INSUFFICIENT_RESOURCES);
1708 // Issue the request at the service for processing
1711 ulResultLen = sizeof( AFSFileIOResultCB);
1713 RtlZeroMemory( &stFileIORequest,
1714 sizeof( AFSFileIOCB));
1716 RtlZeroMemory( &stFileIOResult,
1717 sizeof( AFSFileIOResultCB));
1719 stFileIORequest.SystemIOBuffer = pSystemBuffer;
1721 stFileIORequest.SystemIOBufferMdl = Irp->MdlAddress;
1723 stFileIORequest.IOLength = ByteCount;
1725 stFileIORequest.IOOffset = StartingByte;
1727 ulFlags = AFS_REQUEST_FLAG_SYNCHRONOUS;
1729 if ( bNoIntermediateBuffering)
1732 ulFlags |= AFS_REQUEST_FLAG_CACHE_BYPASS;
1736 // Update file metadata
1739 stFileIORequest.EndOfFile = pFcb->ObjectInformation->EndOfFile;
1741 stFileIORequest.CreateTime = pFcb->ObjectInformation->CreationTime;
1743 stFileIORequest.ChangeTime = pFcb->ObjectInformation->ChangeTime;
1745 stFileIORequest.LastAccessTime = pFcb->ObjectInformation->LastAccessTime;
1747 stFileIORequest.LastWriteTime = pFcb->ObjectInformation->LastWriteTime;
1750 // Write the data to the service
1753 ntStatus = AFSProcessRequest( AFS_REQUEST_TYPE_PROCESS_WRITE_FILE,
1756 &pCcb->DirectoryCB->NameInformation.FileName,
1757 &pFcb->ObjectInformation->FileId,
1758 pFcb->ObjectInformation->VolumeCB->VolumeInformation.Cell,
1759 pFcb->ObjectInformation->VolumeCB->VolumeInformation.CellLength,
1761 sizeof( AFSFileIOCB),
1765 if( NT_SUCCESS( ntStatus))
1768 Irp->IoStatus.Information = (ULONG_PTR)stFileIOResult.Length;
1773 AFSDbgTrace(( AFS_SUBSYSTEM_IO_PROCESSING,
1774 AFS_TRACE_LEVEL_ERROR,
1775 "AFSNonCachedWriteDirect (%p) Failed to send write to service Status %08lX\n",
1782 AFSDbgTrace(( AFS_SUBSYSTEM_IO_PROCESSING,
1783 AFS_TRACE_LEVEL_VERBOSE,
1784 "AFSNonCachedWriteDirect (FO: %p) StartingByte %08lX:%08lX Length %08lX Status %08lX\n",
1786 StartingByte.HighPart,
1787 StartingByte.LowPart,
1791 if (NT_SUCCESS(ntStatus) &&
1796 pFileObject->CurrentByteOffset.QuadPart = StartingByte.QuadPart + ByteCount;
1799 AFSDbgTrace(( AFS_SUBSYSTEM_IO_PROCESSING,
1800 AFS_TRACE_LEVEL_VERBOSE,
1801 "AFSNonCachedWriteDirect Completing Irp %p Status %08lX Info %08lX\n",
1804 Irp->IoStatus.Information));
1806 AFSCompleteRequest( Irp, ntStatus);
1814 AFSCachedWrite( IN PDEVICE_OBJECT DeviceObject,
1816 IN LARGE_INTEGER StartingByte,
1818 IN BOOLEAN ForceFlush)
1820 UNREFERENCED_PARAMETER(DeviceObject);
1821 PVOID pSystemBuffer = NULL;
1822 NTSTATUS ntStatus = STATUS_SUCCESS;
1823 IO_STATUS_BLOCK iosbFlush;
1824 IO_STACK_LOCATION *pIrpSp = IoGetCurrentIrpStackLocation( Irp);
1825 PFILE_OBJECT pFileObject = pIrpSp->FileObject;
1826 AFSFcb *pFcb = (AFSFcb *)pFileObject->FsContext;
1827 BOOLEAN bSynchronousFo = BooleanFlagOn( pFileObject->Flags, FO_SYNCHRONOUS_IO);
1828 ULONG ulCurrentIO = 0, ulTotalLen = ByteCount;
1829 PMDL pCurrentMdl = Irp->MdlAddress;
1830 LARGE_INTEGER liCurrentOffset;
1835 Irp->IoStatus.Information = 0;
1837 if( BooleanFlagOn( pIrpSp->MinorFunction, IRP_MN_MDL))
1843 CcPrepareMdlWrite( pFileObject,
1849 ntStatus = Irp->IoStatus.Status;
1851 __except( EXCEPTION_EXECUTE_HANDLER)
1853 ntStatus = GetExceptionCode();
1855 AFSDbgTrace(( AFS_SUBSYSTEM_IO_PROCESSING,
1856 AFS_TRACE_LEVEL_ERROR,
1857 "AFSCachedWrite (%p) Exception thrown while preparing mdl write Status %08lX\n",
1862 if( !NT_SUCCESS( ntStatus))
1866 // Free up any potentially allocated mdl's
1869 CcMdlWriteComplete( pFileObject,
1873 Irp->MdlAddress = NULL;
1875 AFSDbgTrace(( AFS_SUBSYSTEM_IO_PROCESSING,
1876 AFS_TRACE_LEVEL_ERROR,
1877 "AFSCachedWrite (%p) Failed to process MDL write Status %08lX\n",
1882 try_return( ntStatus);
1885 liCurrentOffset.QuadPart = StartingByte.QuadPart;
1887 while( ulTotalLen > 0)
1890 ntStatus = STATUS_SUCCESS;
1892 if( pCurrentMdl != NULL)
1895 pSystemBuffer = MmGetSystemAddressForMdlSafe( pCurrentMdl,
1896 NormalPagePriority);
1898 ulCurrentIO = MmGetMdlByteCount( pCurrentMdl);
1900 if( ulCurrentIO > ulTotalLen)
1902 ulCurrentIO = ulTotalLen;
1908 pSystemBuffer = AFSLockSystemBuffer( Irp,
1911 ulCurrentIO = ulTotalLen;
1914 if( pSystemBuffer == NULL)
1917 AFSDbgTrace(( AFS_SUBSYSTEM_IO_PROCESSING,
1918 AFS_TRACE_LEVEL_ERROR,
1919 "AFSCachedWrite (%p) Failed to lock system buffer\n",
1922 try_return( ntStatus = STATUS_INSUFFICIENT_RESOURCES);
1928 if( !CcCopyWrite( pFileObject,
1935 // Failed to process request.
1938 AFSDbgTrace(( AFS_SUBSYSTEM_FILE_PROCESSING,
1939 AFS_TRACE_LEVEL_ERROR,
1940 "AFSCachedWrite (%p) Failed to issue CcCopyWrite %wZ @ %0I64X Status %08lX\n",
1942 &pFileObject->FileName,
1943 liCurrentOffset.QuadPart,
1944 Irp->IoStatus.Status));
1946 try_return( ntStatus = STATUS_UNSUCCESSFUL);
1949 __except( EXCEPTION_EXECUTE_HANDLER)
1952 ntStatus = GetExceptionCode();
1954 AFSDbgTrace(( AFS_SUBSYSTEM_IO_PROCESSING,
1955 AFS_TRACE_LEVEL_ERROR,
1956 "AFSCachedWrite (%p) CcCopyWrite Threw exception %wZ @ %0I64X Status %08lX\n",
1958 &pFileObject->FileName,
1959 liCurrentOffset.QuadPart,
1963 if( !NT_SUCCESS( ntStatus))
1965 try_return( ntStatus);
1969 BooleanFlagOn(pFileObject->Flags, (FO_NO_INTERMEDIATE_BUFFERING + FO_WRITE_THROUGH)))
1975 // We have detected a file we do a write through with.
1978 CcFlushCache(&pFcb->NPFcb->SectionObjectPointers,
1983 if( !NT_SUCCESS( iosbFlush.Status))
1986 AFSDbgTrace(( AFS_SUBSYSTEM_IO_PROCESSING,
1987 AFS_TRACE_LEVEL_ERROR,
1988 "AFSCachedWrite (%p) CcFlushCache failure %wZ FID %08lX-%08lX-%08lX-%08lX Status 0x%08lX Bytes 0x%08lX\n",
1990 &pFileObject->FileName,
1991 pFcb->ObjectInformation->FileId.Cell,
1992 pFcb->ObjectInformation->FileId.Volume,
1993 pFcb->ObjectInformation->FileId.Vnode,
1994 pFcb->ObjectInformation->FileId.Unique,
1996 iosbFlush.Information));
1998 try_return( ntStatus = iosbFlush.Status);
2001 __except( EXCEPTION_EXECUTE_HANDLER)
2004 ntStatus = GetExceptionCode();
2006 AFSDbgTrace(( AFS_SUBSYSTEM_IO_PROCESSING,
2007 AFS_TRACE_LEVEL_ERROR,
2008 "AFSCachedWrite (%p) CcFlushCache Threw exception %wZ @ %0I64X Status %08lX\n",
2010 &pFileObject->FileName,
2011 liCurrentOffset.QuadPart,
2014 try_return( ntStatus);
2018 if( ulTotalLen <= ulCurrentIO)
2023 liCurrentOffset.QuadPart += ulCurrentIO;
2025 ulTotalLen -= ulCurrentIO;
2027 pCurrentMdl = pCurrentMdl->Next;
2032 if( NT_SUCCESS( ntStatus))
2035 Irp->IoStatus.Information = ByteCount;
2038 BooleanFlagOn(pFileObject->Flags, (FO_NO_INTERMEDIATE_BUFFERING + FO_WRITE_THROUGH)))
2041 // Write through asked for... Set things so that we get
2042 // flush when the worker thread next wakes up
2044 pFcb->Specific.File.LastServerFlush.QuadPart = 0;
2048 AFSCompleteRequest( Irp,
2056 // Called with Fcb->NPFcb->SectionObjectResource and Fcb->NPFcb->Resource held
2061 AFSExtendingWrite( IN AFSFcb *Fcb,
2062 IN PFILE_OBJECT FileObject,
2063 IN LONGLONG NewLength)
2065 LARGE_INTEGER liSaveFileSize = Fcb->Header.FileSize;
2066 LARGE_INTEGER liSaveAllocation = Fcb->Header.AllocationSize;
2067 NTSTATUS ntStatus = STATUS_SUCCESS;
2068 AFSCcb *pCcb = (AFSCcb *)FileObject->FsContext2;
2070 if( NewLength > Fcb->Header.AllocationSize.QuadPart)
2073 Fcb->Header.AllocationSize.QuadPart = NewLength;
2075 Fcb->ObjectInformation->AllocationSize = Fcb->Header.AllocationSize;
2078 if( NewLength > Fcb->Header.FileSize.QuadPart)
2081 Fcb->Header.FileSize.QuadPart = NewLength;
2083 Fcb->ObjectInformation->EndOfFile = Fcb->Header.FileSize;
2090 ntStatus = AFSUpdateFileInformation( &Fcb->ObjectInformation->ParentFileId,
2091 Fcb->ObjectInformation,
2094 if (NT_SUCCESS(ntStatus))
2097 KeQuerySystemTime( &Fcb->ObjectInformation->ChangeTime);
2099 SetFlag( Fcb->Flags, AFS_FCB_FLAG_FILE_MODIFIED | AFS_FCB_FLAG_UPDATE_CHANGE_TIME);
2102 // If the file is currently cached, then let the MM know about the extension
2104 // The CcSetFileSizes call should be made with only the PagingResource held
2105 // which we are currently not holding.
2108 if( CcIsFileCached( FileObject))
2110 CcSetFileSizes( FileObject,
2111 (PCC_FILE_SIZES)&Fcb->Header.AllocationSize);
2116 Fcb->Header.FileSize = liSaveFileSize;
2117 Fcb->Header.AllocationSize = liSaveAllocation;
2121 // DownConvert file resource to shared
2123 ExConvertExclusiveToSharedLite( &Fcb->NPFcb->Resource);
2129 AFSShareWrite( IN PDEVICE_OBJECT DeviceObject,
2133 UNREFERENCED_PARAMETER(DeviceObject);
2134 NTSTATUS ntStatus = STATUS_SUCCESS;
2135 PIO_STACK_LOCATION pIrpSp = IoGetCurrentIrpStackLocation( Irp);
2136 AFSFcb *pFcb = NULL;
2137 AFSCcb *pCcb = NULL;
2138 AFSPipeIORequestCB *pIoRequest = NULL;
2139 void *pBuffer = NULL;
2140 AFSPipeIOResultCB stIoResult;
2141 ULONG ulBytesReturned = 0;
2146 pFcb = (AFSFcb *)pIrpSp->FileObject->FsContext;
2148 pCcb = (AFSCcb *)pIrpSp->FileObject->FsContext2;
2150 AFSDbgTrace(( AFS_SUBSYSTEM_PIPE_PROCESSING,
2151 AFS_TRACE_LEVEL_VERBOSE,
2152 "AFSShareWrite On pipe %wZ Length %08lX\n",
2153 &pCcb->DirectoryCB->NameInformation.FileName,
2154 pIrpSp->Parameters.Write.Length));
2156 if( pIrpSp->Parameters.Write.Length == 0)
2160 // Nothing to do in this case
2163 try_return( ntStatus);
2167 // Retrieve the buffer for the read request
2170 pBuffer = AFSLockSystemBuffer( Irp,
2171 pIrpSp->Parameters.Write.Length);
2173 if( pBuffer == NULL)
2176 AFSDbgTrace(( AFS_SUBSYSTEM_PIPE_PROCESSING,
2177 AFS_TRACE_LEVEL_ERROR,
2178 "AFSShareWrite Failed to map buffer on pipe %wZ\n",
2179 &pCcb->DirectoryCB->NameInformation.FileName));
2181 try_return( ntStatus = STATUS_INSUFFICIENT_RESOURCES);
2184 AFSAcquireShared( &pFcb->NPFcb->Resource,
2187 pIoRequest = (AFSPipeIORequestCB *)AFSExAllocatePoolWithTag( PagedPool,
2188 sizeof( AFSPipeIORequestCB) +
2189 pIrpSp->Parameters.Write.Length,
2190 AFS_GENERIC_MEMORY_14_TAG);
2192 if( pIoRequest == NULL)
2195 try_return( ntStatus = STATUS_INSUFFICIENT_RESOURCES);
2198 RtlZeroMemory( pIoRequest,
2199 sizeof( AFSPipeIORequestCB) + pIrpSp->Parameters.Write.Length);
2201 pIoRequest->RequestId = pCcb->RequestID;
2203 pIoRequest->RootId = pFcb->ObjectInformation->VolumeCB->ObjectInformation.FileId;
2205 pIoRequest->BufferLength = pIrpSp->Parameters.Write.Length;
2207 RtlCopyMemory( (void *)((char *)pIoRequest + sizeof( AFSPipeIORequestCB)),
2209 pIrpSp->Parameters.Write.Length);
2211 stIoResult.BytesProcessed = 0;
2213 ulBytesReturned = sizeof( AFSPipeIOResultCB);
2216 // Issue the open request to the service
2219 ntStatus = AFSProcessRequest( AFS_REQUEST_TYPE_PIPE_WRITE,
2220 AFS_REQUEST_FLAG_SYNCHRONOUS,
2222 &pCcb->DirectoryCB->NameInformation.FileName,
2227 sizeof( AFSPipeIORequestCB) +
2228 pIrpSp->Parameters.Write.Length,
2232 if( !NT_SUCCESS( ntStatus))
2235 AFSDbgTrace(( AFS_SUBSYSTEM_FILE_PROCESSING,
2236 AFS_TRACE_LEVEL_ERROR,
2237 "AFSShareWrite (%p) Failed service write Status %08lX\n",
2241 try_return( ntStatus);
2244 AFSDbgTrace(( AFS_SUBSYSTEM_PIPE_PROCESSING,
2245 AFS_TRACE_LEVEL_VERBOSE,
2246 "AFSShareWrite Completed on pipe %wZ Length read %08lX\n",
2247 &pCcb->DirectoryCB->NameInformation.FileName,
2248 stIoResult.BytesProcessed));
2250 Irp->IoStatus.Information = stIoResult.BytesProcessed;
2257 AFSReleaseResource( &pFcb->NPFcb->Resource);
2260 if( pIoRequest != NULL)
2263 AFSExFreePoolWithTag( pIoRequest, AFS_GENERIC_MEMORY_14_TAG);