2 * Copyright (c) 2008, 2009, 2010, 2011 Kernel Drivers, LLC.
3 * Copyright (c) 2009, 2010, 2011, 2012, 2013, 2014 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 bWriteToEndOfFile = FALSE;
125 BOOLEAN bWait = FALSE;
126 BOOLEAN bCompleteIrp = TRUE;
127 BOOLEAN bForceFlush = FALSE;
129 HANDLE hCallingUser = OnBehalfOf;
130 ULONGLONG ullProcessId = (ULONGLONG)PsGetCurrentProcessId();
131 AFSDeviceExt *pRDRDevExt = (AFSDeviceExt *)AFSRDRDeviceObject->DeviceExtension;
133 pIrpSp = IoGetCurrentIrpStackLocation( Irp);
138 Irp->IoStatus.Information = 0;
140 pFileObject = pIrpSp->FileObject;
143 // Extract the fileobject references
146 pFcb = (AFSFcb *)pFileObject->FsContext;
147 pCcb = (AFSCcb *)pFileObject->FsContext2;
149 ObReferenceObject( pFileObject);
154 AFSDbgTrace(( AFS_SUBSYSTEM_IO_PROCESSING,
155 AFS_TRACE_LEVEL_ERROR,
156 "AFSCommonWrite Attempted write (%p) when pFcb == NULL\n",
159 try_return( ntStatus = STATUS_INVALID_DEVICE_REQUEST);
162 pNPFcb = pFcb->NPFcb;
165 // If we are in shutdown mode then fail the request
168 if( BooleanFlagOn( pDeviceExt->DeviceFlags, AFS_DEVICE_FLAG_REDIRECTOR_SHUTDOWN))
171 AFSDbgTrace(( AFS_SUBSYSTEM_FILE_PROCESSING,
172 AFS_TRACE_LEVEL_WARNING,
173 "AFSCommonWrite (%p) Open request after shutdown\n",
176 try_return( ntStatus = STATUS_TOO_LATE);
179 liStartingByte = pIrpSp->Parameters.Write.ByteOffset;
180 bPagingIo = BooleanFlagOn( Irp->Flags, IRP_PAGING_IO);
181 bNonCachedIo = BooleanFlagOn( Irp->Flags, IRP_NOCACHE);
182 bWait = IoIsOperationSynchronous( Irp);
183 ulByteCount = pIrpSp->Parameters.Write.Length;
184 bSynchronousFo = BooleanFlagOn( pFileObject->Flags, FO_SYNCHRONOUS_IO);
186 if( pFcb->Header.NodeTypeCode != AFS_IOCTL_FCB &&
187 pFcb->Header.NodeTypeCode != AFS_FILE_FCB &&
188 pFcb->Header.NodeTypeCode != AFS_SPECIAL_SHARE_FCB)
191 AFSDbgTrace(( AFS_SUBSYSTEM_IO_PROCESSING,
192 AFS_TRACE_LEVEL_ERROR,
193 "AFSCommonWrite Attempted write (%p) on an invalid node type %08lX\n",
195 pFcb->Header.NodeTypeCode));
197 try_return( ntStatus = STATUS_INVALID_DEVICE_REQUEST);
201 // If this is a write against an IOCtl node then handle it
202 // in a different pathway
205 if( pFcb->Header.NodeTypeCode == AFS_IOCTL_FCB)
208 AFSDbgTrace(( AFS_SUBSYSTEM_IO_PROCESSING,
209 AFS_TRACE_LEVEL_VERBOSE,
210 "AFSCommonWrite (%p) Processing file (PIOCTL) Offset %0I64X Length %08lX Irp Flags %08lX\n",
212 liStartingByte.QuadPart,
216 ntStatus = AFSIOCtlWrite( DeviceObject,
219 try_return( ntStatus);
221 else if( pFcb->Header.NodeTypeCode == AFS_SPECIAL_SHARE_FCB)
224 AFSDbgTrace(( AFS_SUBSYSTEM_IO_PROCESSING,
225 AFS_TRACE_LEVEL_VERBOSE,
226 "AFSCommonWrite (%p) Processing file (SHARE) Offset %0I64X Length %08lX Irp Flags %08lX\n",
228 liStartingByte.QuadPart,
232 ntStatus = AFSShareWrite( DeviceObject,
235 try_return( ntStatus);
239 // Is the Cache not there yet? Exit.
241 if( !BooleanFlagOn( AFSLibControlFlags, AFS_REDIR_LIB_FLAGS_NONPERSISTENT_CACHE) &&
242 !BooleanFlagOn( pRDRDevExt->DeviceFlags, AFS_REDIR_INIT_PERFORM_SERVICE_IO) &&
243 NULL == pDeviceExt->Specific.RDR.CacheFileObject)
246 AFSDbgTrace(( AFS_SUBSYSTEM_IO_PROCESSING,
247 AFS_TRACE_LEVEL_ERROR,
248 "AFSCommonWrite (%p) Request failed due to AFS cache closed\n",
251 try_return( ntStatus = STATUS_TOO_LATE );
254 if( pFcb->ObjectInformation->VolumeCB != NULL &&
255 BooleanFlagOn( pFcb->ObjectInformation->VolumeCB->VolumeInformation.FileSystemAttributes, FILE_READ_ONLY_VOLUME))
258 AFSDbgTrace(( AFS_SUBSYSTEM_IO_PROCESSING,
259 AFS_TRACE_LEVEL_ERROR,
260 "AFSCommonWrite (%p) Request failed due to read only volume\n",
263 try_return( ntStatus = STATUS_MEDIA_WRITE_PROTECTED);
267 // We need to know on whose behalf we have been called (which
268 // we will eventually tell to the server - for non paging
269 // writes). If we were posted then we were told. If this is
270 // the first time we saw the irp then we grab it now.
272 if( NULL == OnBehalfOf )
275 hCallingUser = PsGetCurrentProcessId();
280 hCallingUser = OnBehalfOf;
284 // Check for zero length write
287 if( ulByteCount == 0)
290 AFSDbgTrace(( AFS_SUBSYSTEM_IO_PROCESSING,
291 AFS_TRACE_LEVEL_VERBOSE,
292 "AFSCommonWrite (%p) Request completed due to zero length\n",
295 try_return( ntStatus);
299 // Is this Fcb valid???
302 if( BooleanFlagOn( pFcb->ObjectInformation->Flags, AFS_OBJECT_FLAGS_OBJECT_INVALID))
305 AFSDbgTrace(( AFS_SUBSYSTEM_IO_PROCESSING,
306 AFS_TRACE_LEVEL_ERROR,
307 "AFSCommonWrite (%p) Failing request due to INVALID fcb\n",
310 try_return( ntStatus = STATUS_FILE_DELETED);
313 if( BooleanFlagOn( pCcb->DirectoryCB->Flags, AFS_DIR_ENTRY_DELETED) ||
314 BooleanFlagOn( pFcb->ObjectInformation->Flags, AFS_OBJECT_FLAGS_DELETED))
317 AFSDbgTrace(( AFS_SUBSYSTEM_IO_PROCESSING,
318 AFS_TRACE_LEVEL_ERROR,
319 "AFSCommonWrite (%p) Request failed due to file deleted\n",
322 try_return( ntStatus = STATUS_FILE_DELETED);
325 if( FlagOn( pIrpSp->MinorFunction, IRP_MN_COMPLETE))
328 AFSDbgTrace(( AFS_SUBSYSTEM_IO_PROCESSING,
329 AFS_TRACE_LEVEL_VERBOSE,
330 "AFSCommonWrite (%p) IRP_MN_COMPLETE being processed\n",
333 CcMdlWriteComplete(pFileObject, &pIrpSp->Parameters.Write.ByteOffset, Irp->MdlAddress);
336 // Mdl is now Deallocated
339 Irp->MdlAddress = NULL;
341 try_return( ntStatus = STATUS_SUCCESS );
345 // If we get a non cached IO for a cached file we should do a purge.
346 // For now we will just promote to cached
348 if( NULL != pFileObject->SectionObjectPointer->DataSectionObject && !bPagingIo && bNonCachedIo)
350 bNonCachedIo = FALSE;
354 if ( !bNonCachedIo && !bPagingIo)
357 if( pFileObject->PrivateCacheMap == NULL)
360 AFSDbgTrace(( AFS_SUBSYSTEM_LOCK_PROCESSING|AFS_SUBSYSTEM_SECTION_OBJECT,
361 AFS_TRACE_LEVEL_VERBOSE,
362 "AFSCommonWrite Acquiring Fcb SectionObject lock %p EXCL %08lX\n",
363 &pNPFcb->SectionObjectResource,
364 PsGetCurrentThread()));
366 AFSAcquireExcl( &pNPFcb->SectionObjectResource,
369 bReleaseSectionObject = TRUE;
374 AFSDbgTrace(( AFS_SUBSYSTEM_IO_PROCESSING,
375 AFS_TRACE_LEVEL_VERBOSE,
376 "AFSCommonWrite Initialize caching on Fcb %p FileObject %p\n",
380 CcInitializeCacheMap( pFileObject,
381 (PCC_FILE_SIZES)&pFcb->Header.AllocationSize,
383 AFSLibCacheManagerCallbacks,
386 CcSetReadAheadGranularity( pFileObject,
387 pDeviceExt->Specific.RDR.MaximumRPCLength);
389 CcSetDirtyPageThreshold( pFileObject,
390 AFS_DIRTY_CHUNK_THRESHOLD * pDeviceExt->Specific.RDR.MaximumRPCLength / 4096);
392 __except( EXCEPTION_EXECUTE_HANDLER)
395 ntStatus = GetExceptionCode();
397 AFSDbgTrace(( AFS_SUBSYSTEM_IO_PROCESSING,
398 AFS_TRACE_LEVEL_ERROR,
399 "AFSCommonWrite (%p) Exception thrown while initializing cache map Status %08lX\n",
404 AFSDbgTrace(( AFS_SUBSYSTEM_LOCK_PROCESSING|AFS_SUBSYSTEM_SECTION_OBJECT,
405 AFS_TRACE_LEVEL_VERBOSE,
406 "AFSCommonWrite Releasing Fcb SectionObject lock %p EXCL %08lX\n",
407 &pNPFcb->SectionObjectResource,
408 PsGetCurrentThread()));
410 AFSReleaseResource( &pNPFcb->SectionObjectResource);
412 bReleaseSectionObject = FALSE;
414 if( !NT_SUCCESS( ntStatus))
417 try_return( ntStatus);
422 // On versions of Microsoft Windows older than Vista the IO Manager
423 // will issue multiple outstanding writes on a synchronous file object
424 // if one of the cached writes completes with STATUS_PENDING. This can
425 // result in the writes being completed out of order which can corrupt
426 // the end of file marker. On OS versions older than Vista use a spin
427 // loop instead of deferring the write.
430 if ( bSynchronousFo &&
431 AFSRtlSysVersion.dwMajorVersion < 6)
434 while (!CcCanIWrite( pFileObject,
439 static const LONGLONG llWriteDelay = (LONGLONG)-100000;
442 AFSDbgLogMsg( AFS_SUBSYSTEM_IO_PROCESSING,
443 AFS_TRACE_LEVEL_WARNING,
444 "AFSCommonWrite (FO: %p) CcCanIWrite says No room for %u bytes! Retry in 10ms\n",
448 KeDelayExecutionThread(KernelMode, FALSE, (PLARGE_INTEGER)&llWriteDelay);
454 if (!CcCanIWrite( pFileObject,
460 AFSDbgTrace(( AFS_SUBSYSTEM_IO_PROCESSING,
461 AFS_TRACE_LEVEL_WARNING,
462 "AFSCommonWrite (FO: %p) CcCanIWrite says No room for Offset %0I64X Length %08lX bytes! Deferring%s\n",
464 liStartingByte.QuadPart,
466 bRetry ? " RETRY" : ""));
468 ntStatus = AFSDeferWrite( DeviceObject, pFileObject, hCallingUser, Irp, ulByteCount, bRetry);
470 if ( STATUS_PENDING == ntStatus)
473 bCompleteIrp = FALSE;
478 AFSDbgTrace(( AFS_SUBSYSTEM_IO_PROCESSING,
479 AFS_TRACE_LEVEL_ERROR,
480 "AFSCommonWrite (FO: %p) AFSDeferWrite failure Status %08lX\n",
485 try_return( ntStatus);
491 // Save off the PID if this is not a paging IO
495 ( pFcb->Specific.File.ExtentRequestProcessId == 0 ||
496 ( ullProcessId != (ULONGLONG)AFSSysProcess &&
497 pFcb->Specific.File.ExtentRequestProcessId != ullProcessId)))
500 pFcb->Specific.File.ExtentRequestProcessId = ullProcessId;
502 if( ullProcessId == (ULONGLONG)AFSSysProcess)
504 AFSDbgTrace(( AFS_SUBSYSTEM_EXTENT_PROCESSING,
505 AFS_TRACE_LEVEL_WARNING,
506 "%s Setting LastWriterExtentProcessId to system process for Fcb %p\n",
515 // - if Paging then we need to do nothing (the precalls will
516 // have acquired the paging resource), for clarity we will collect
517 // the paging resource
518 // - If extending Write then take the fileresource EX (EOF will change, Allocation will only move out)
519 // - Otherwise we collect the file shared, check against extending and
530 //ASSERT( NULL != OnBehalfOf || ExIsResourceAcquiredLite( &pNPFcb->Resource ));
532 AFSDbgTrace(( AFS_SUBSYSTEM_LOCK_PROCESSING,
533 AFS_TRACE_LEVEL_VERBOSE,
534 "AFSCommonWrite Acquiring Fcb PagingIo lock %p SHARED %08lX\n",
535 &pNPFcb->PagingResource,
536 PsGetCurrentThread()));
538 AFSAcquireShared( &pNPFcb->PagingResource,
541 bReleasePaging = TRUE;
544 // We have the correct lock - we cannot have the wrong one
551 bWriteToEndOfFile = liStartingByte.LowPart == FILE_WRITE_TO_END_OF_FILE &&
552 liStartingByte.HighPart == -1;
554 bExtendingWrite = ( bWriteToEndOfFile ||
555 ((liStartingByte.QuadPart + ulByteCount) >=
556 pFcb->Header.FileSize.QuadPart));
558 if( bExtendingWrite || bNonCachedIo)
561 // Check for lock inversion
565 // For bExtendingWrite the PagingResource is needed to protect
566 // the CcSetFileSizes call in AFSExtendingWrite
569 ASSERT( !ExIsResourceAcquiredLite( &pNPFcb->PagingResource ));
571 AFSDbgTrace(( AFS_SUBSYSTEM_LOCK_PROCESSING,
572 AFS_TRACE_LEVEL_VERBOSE,
573 "AFSCommonWrite Acquiring Fcb lock %p EXCL %08lX\n",
575 PsGetCurrentThread()));
577 AFSAcquireExcl( &pNPFcb->Resource,
582 AFSDbgTrace(( AFS_SUBSYSTEM_LOCK_PROCESSING|AFS_SUBSYSTEM_SECTION_OBJECT,
583 AFS_TRACE_LEVEL_VERBOSE,
584 "AFSCommonWrite Acquiring Fcb SectionObject lock %p EXCL %08lX\n",
585 &pNPFcb->SectionObjectResource,
586 PsGetCurrentThread()));
588 AFSAcquireExcl( &pNPFcb->SectionObjectResource,
591 bReleaseSectionObject = TRUE;
593 if ( bWriteToEndOfFile)
596 if (pFcb->Header.ValidDataLength.QuadPart > pFcb->Header.FileSize.QuadPart)
599 liStartingByte = pFcb->Header.ValidDataLength;
604 liStartingByte = pFcb->Header.FileSize;
607 pIrpSp->Parameters.Write.ByteOffset = liStartingByte;
611 // We have the correct lock - even if we don't end up truncating
617 ASSERT( !ExIsResourceAcquiredLite( &pNPFcb->PagingResource ));
619 AFSDbgTrace(( AFS_SUBSYSTEM_LOCK_PROCESSING,
620 AFS_TRACE_LEVEL_VERBOSE,
621 "AFSCommonWrite Acquiring Fcb lock %p SHARED %08lX\n",
623 PsGetCurrentThread()));
625 AFSAcquireShared( &pNPFcb->Resource,
630 AFSDbgTrace(( AFS_SUBSYSTEM_LOCK_PROCESSING|AFS_SUBSYSTEM_SECTION_OBJECT,
631 AFS_TRACE_LEVEL_VERBOSE,
632 "AFSCommonWrite Acquiring Fcb SectionObject lock %p SHARED %08lX\n",
633 &pNPFcb->SectionObjectResource,
634 PsGetCurrentThread()));
636 AFSAcquireShared( &pNPFcb->SectionObjectResource,
639 bReleaseSectionObject = TRUE;
642 // Have things moved? Are we extending? If so, the the lock isn't OK
644 bLockOK = (liStartingByte.QuadPart + ulByteCount) < pFcb->Header.FileSize.QuadPart;
649 AFSDbgTrace(( AFS_SUBSYSTEM_LOCK_PROCESSING|AFS_SUBSYSTEM_SECTION_OBJECT,
650 AFS_TRACE_LEVEL_VERBOSE,
651 "AFSCommonWrite Releasing Fcb SectionObject lock %p SHARED %08lX\n",
652 &pNPFcb->SectionObjectResource,
653 PsGetCurrentThread()));
655 AFSReleaseResource( &pNPFcb->SectionObjectResource);
657 bReleaseSectionObject = FALSE;
659 AFSReleaseResource( &pNPFcb->Resource);
661 bReleaseMain = FALSE;
672 // Check the BR locks on the file.
675 if ( !FsRtlCheckLockForWriteAccess( &pFcb->Specific.File.FileLock,
679 AFSDbgTrace(( AFS_SUBSYSTEM_IO_PROCESSING,
680 AFS_TRACE_LEVEL_ERROR,
681 "AFSCommonWrite (%p) Request failed due to lock conflict\n",
684 try_return( ntStatus = STATUS_FILE_LOCK_CONFLICT);
690 ntStatus = AFSExtendingWrite( pFcb, pFileObject, (liStartingByte.QuadPart + ulByteCount));
693 // Fcb->NPFcb->Resource is now held SHARED
696 if( !NT_SUCCESS(ntStatus))
699 AFSDbgTrace(( AFS_SUBSYSTEM_IO_PROCESSING,
700 AFS_TRACE_LEVEL_ERROR,
701 "AFSCommonWrite (%p) Failed extending write request Status %08lX\n",
705 try_return( ntStatus );
711 // Fire off the request as appropriate
713 bCompleteIrp = FALSE;
720 // Main resource held Shared
721 // SectionObject resource held exclusive if extending write
724 AFSDbgTrace(( AFS_SUBSYSTEM_IO_PROCESSING,
725 AFS_TRACE_LEVEL_VERBOSE,
726 "AFSCommonWrite (%p) Processing CACHED request Offset %0I64X Len %08lX%s\n",
728 liStartingByte.QuadPart,
730 bRetry ? " RETRY" : ""));
732 ntStatus = AFSCachedWrite( DeviceObject, Irp, liStartingByte, ulByteCount, bForceFlush);
737 AFSDbgTrace(( AFS_SUBSYSTEM_IO_PROCESSING,
738 AFS_TRACE_LEVEL_VERBOSE,
739 "AFSCommonWrite (%p) Processing NON-CACHED request Offset %0I64X Len %08lX%s\n",
741 liStartingByte.QuadPart,
743 bRetry ? " RETRY" : ""));
745 if( BooleanFlagOn( pRDRDevExt->DeviceFlags, AFS_DEVICE_FLAG_DIRECT_SERVICE_IO))
748 ntStatus = AFSNonCachedWriteDirect( DeviceObject, Irp, liStartingByte, ulByteCount);
752 ntStatus = AFSNonCachedWrite( DeviceObject, Irp, liStartingByte, ulByteCount);
758 AFSDbgTrace(( AFS_SUBSYSTEM_IO_PROCESSING,
759 AFS_TRACE_LEVEL_VERBOSE,
760 "AFSCommonWrite (%p) Process complete Status %08lX\n",
764 if ( NT_SUCCESS( ntStatus) &&
765 ntStatus != STATUS_PENDING)
773 pFileObject->CurrentByteOffset.QuadPart = liStartingByte.QuadPart + ulByteCount;
777 // If this extended the VDL, then update it accordingly.
778 // Increasing the VDL does not require a call to CcSetFileSizes.
781 if( liStartingByte.QuadPart + ulByteCount > pFcb->Header.ValidDataLength.QuadPart)
784 pFcb->Header.ValidDataLength.QuadPart = liStartingByte.QuadPart + ulByteCount;
788 // Register the File Object as having modified the file.
790 SetFlag( pFileObject->Flags, FO_FILE_MODIFIED);
794 if ( ntStatus != STATUS_PENDING &&
795 !bPagingIo && bNonCachedIo && CcIsFileCached( pFileObject) &&
796 pNPFcb->SectionObjectPointers.DataSectionObject != NULL &&
797 bReleaseSectionObject)
800 // Regardless of whether or not the a non-paging non-cached write
801 // succeeds or fails, if the file is cached the contents of the
802 // cache are no longer up to date. A CcPurgeCacheSection must be
803 // performed to force subsequent cached reads to obtain the data
806 // The Fcb Resource is dropped in order to permit filters that perform
807 // an open via a worker thread in response to a purge to do so without
808 // deadlocking. The SectionObjectResource is held across the purge to
809 // prevent racing with other cache operations.
815 AFSReleaseResource( &pNPFcb->Resource);
817 bReleaseMain = FALSE;
823 if ( !CcPurgeCacheSection( &pNPFcb->SectionObjectPointers,
829 AFSDbgTrace(( AFS_SUBSYSTEM_IO_PROCESSING,
830 AFS_TRACE_LEVEL_WARNING,
831 "AFSCommonWrite CcPurgeCacheSection failure FID %08lX-%08lX-%08lX-%08lX\n",
832 pFcb->ObjectInformation->FileId.Cell,
833 pFcb->ObjectInformation->FileId.Volume,
834 pFcb->ObjectInformation->FileId.Vnode,
835 pFcb->ObjectInformation->FileId.Unique));
837 SetFlag( pFcb->Flags, AFS_FCB_FLAG_PURGE_ON_CLOSE);
840 __except( EXCEPTION_EXECUTE_HANDLER)
843 DWORD ntStatus2 = GetExceptionCode();
847 "EXCEPTION - AFSCommonWrite CcPurgeCacheSection failed FID %08lX-%08lX-%08lX-%08lX Status 0x%08lX\n",
848 pFcb->ObjectInformation->FileId.Cell,
849 pFcb->ObjectInformation->FileId.Volume,
850 pFcb->ObjectInformation->FileId.Vnode,
851 pFcb->ObjectInformation->FileId.Unique,
854 SetFlag( pFcb->Flags, AFS_FCB_FLAG_PURGE_ON_CLOSE);
858 ObDereferenceObject(pFileObject);
860 if( bReleaseSectionObject)
863 AFSDbgTrace(( AFS_SUBSYSTEM_LOCK_PROCESSING|AFS_SUBSYSTEM_SECTION_OBJECT,
864 AFS_TRACE_LEVEL_VERBOSE,
865 "AFSCommonWrite Releasing Fcb SectionObject lock %p EXCL/SHARED %08lX\n",
866 &pNPFcb->SectionObjectResource,
867 PsGetCurrentThread()));
869 AFSReleaseResource( &pNPFcb->SectionObjectResource);
875 AFSReleaseResource( &pNPFcb->PagingResource);
881 AFSReleaseResource( &pNPFcb->Resource);
887 AFSCompleteRequest( Irp,
896 AFSIOCtlWrite( IN PDEVICE_OBJECT DeviceObject,
900 UNREFERENCED_PARAMETER(DeviceObject);
901 NTSTATUS ntStatus = STATUS_SUCCESS;
902 AFSPIOCtlIORequestCB stIORequestCB;
903 PIO_STACK_LOCATION pIrpSp = IoGetCurrentIrpStackLocation( Irp);
906 AFSPIOCtlIOResultCB stIOResultCB;
907 ULONG ulBytesReturned = 0;
908 AFSFileID stParentFID;
913 Irp->IoStatus.Information = 0;
915 RtlZeroMemory( &stIORequestCB,
916 sizeof( AFSPIOCtlIORequestCB));
918 if( pIrpSp->Parameters.Write.Length == 0)
922 // Nothing to do in this case
925 try_return( ntStatus);
928 pFcb = (AFSFcb *)pIrpSp->FileObject->FsContext;
930 pCcb = (AFSCcb *)pIrpSp->FileObject->FsContext2;
932 AFSDbgTrace(( AFS_SUBSYSTEM_LOCK_PROCESSING,
933 AFS_TRACE_LEVEL_VERBOSE,
934 "AFSIOCtlWrite Acquiring Fcb lock %p SHARED %08lX\n",
935 &pFcb->NPFcb->Resource,
936 PsGetCurrentThread()));
938 AFSAcquireShared( &pFcb->NPFcb->Resource,
942 // Get the parent fid to pass to the cm
945 RtlZeroMemory( &stParentFID,
948 if( BooleanFlagOn( pFcb->ObjectInformation->Flags, AFS_OBJECT_FLAGS_PARENT_FID))
952 // The parent directory FID of the node
955 stParentFID = pFcb->ObjectInformation->ParentFileId;
959 // Set the control block up
962 stIORequestCB.RequestId = pCcb->RequestID;
964 if( pFcb->ObjectInformation->VolumeCB != NULL)
966 stIORequestCB.RootId = pFcb->ObjectInformation->VolumeCB->ObjectInformation.FileId;
970 // Lock down the buffer
973 stIORequestCB.MappedBuffer = AFSMapToService( Irp,
974 pIrpSp->Parameters.Write.Length);
976 if( stIORequestCB.MappedBuffer == NULL)
979 try_return( ntStatus = STATUS_INSUFFICIENT_RESOURCES);
982 stIORequestCB.BufferLength = pIrpSp->Parameters.Write.Length;
984 stIOResultCB.BytesProcessed = 0;
986 ulBytesReturned = sizeof( AFSPIOCtlIOResultCB);
989 // Issue the request to the service
992 ntStatus = AFSProcessRequest( AFS_REQUEST_TYPE_PIOCTL_WRITE,
993 AFS_REQUEST_FLAG_SYNCHRONOUS,
999 (void *)&stIORequestCB,
1000 sizeof( AFSPIOCtlIORequestCB),
1004 if( !NT_SUCCESS( ntStatus))
1007 try_return( ntStatus);
1011 // Update the length written
1014 Irp->IoStatus.Information = stIOResultCB.BytesProcessed;
1018 if( stIORequestCB.MappedBuffer != NULL)
1021 AFSUnmapServiceMappedBuffer( stIORequestCB.MappedBuffer,
1028 AFSReleaseResource( &pFcb->NPFcb->Resource);
1036 // This function is called when we know we have to read from the AFS Cache.
1038 // It ensures that we have exents for the entirety of the write and
1039 // then pins the extents into memory (meaning that although we may
1040 // add we will not remove). Then it creates a scatter gather write
1041 // and fires off the IRPs
1045 AFSNonCachedWrite( IN PDEVICE_OBJECT DeviceObject,
1047 IN LARGE_INTEGER StartingByte,
1050 NTSTATUS ntStatus = STATUS_UNSUCCESSFUL;
1051 VOID *pSystemBuffer = NULL;
1052 BOOLEAN bPagingIo = BooleanFlagOn( Irp->Flags, IRP_PAGING_IO);
1053 BOOLEAN bLocked = FALSE;
1054 BOOLEAN bCompleteIrp = TRUE;
1055 AFSGatherIo *pGatherIo = NULL;
1056 AFSIoRun *pIoRuns = NULL;
1057 AFSIoRun stIoRuns[AFS_MAX_STACK_IO_RUNS];
1058 ULONG extentsCount = 0, runCount = 0;
1059 AFSExtent *pStartExtent = NULL;
1060 AFSExtent *pIgnoreExtent = NULL;
1061 IO_STACK_LOCATION *pIrpSp = IoGetCurrentIrpStackLocation( Irp);
1062 PFILE_OBJECT pFileObject = pIrpSp->FileObject;
1063 AFSFcb *pFcb = (AFSFcb *)pFileObject->FsContext;
1064 AFSCcb *pCcb = (AFSCcb *)pFileObject->FsContext2;
1065 BOOLEAN bSynchronousFo = BooleanFlagOn( pFileObject->Flags, FO_SYNCHRONOUS_IO);
1066 AFSDeviceExt *pDevExt = (AFSDeviceExt *)DeviceObject->DeviceExtension;
1067 LARGE_INTEGER liCurrentTime, liLastRequestTime;
1068 AFSDeviceExt *pControlDevExt = (AFSDeviceExt *)AFSControlDeviceObject->DeviceExtension;
1069 PFILE_OBJECT pCacheFileObject = NULL;
1070 BOOLEAN bDerefExtents = FALSE;
1075 AFSDbgTrace(( AFS_SUBSYSTEM_IO_PROCESSING,
1076 AFS_TRACE_LEVEL_VERBOSE,
1077 "AFSNonCachedWrite (FO: %p) StartingByte %08lX:%08lX Length %08lX\n",
1079 StartingByte.HighPart,
1080 StartingByte.LowPart,
1083 if (ByteCount > pDevExt->Specific.RDR.MaxIo.QuadPart)
1086 AFSDbgTrace(( AFS_SUBSYSTEM_IO_PROCESSING,
1087 AFS_TRACE_LEVEL_ERROR,
1088 "AFSNonCachedWrite (%p) Request %08lX Actual %08lX larger than MaxIO %I64X\n",
1091 pIrpSp->Parameters.Write.Length,
1092 pDevExt->Specific.RDR.MaxIo.QuadPart));
1094 try_return( ntStatus = STATUS_UNSUCCESSFUL);
1098 // Get the mapping for the buffer
1100 pSystemBuffer = AFSLockSystemBuffer( Irp,
1103 if( pSystemBuffer == NULL)
1106 AFSDbgTrace(( AFS_SUBSYSTEM_IO_PROCESSING,
1107 AFS_TRACE_LEVEL_ERROR,
1108 "AFSNonCachedWrite (%p) Failed to map system buffer\n",
1111 try_return( ntStatus = STATUS_INSUFFICIENT_RESOURCES);
1116 // Provoke a get of the extents - if we need to.
1119 AFSDbgTrace(( AFS_SUBSYSTEM_EXTENT_PROCESSING,
1120 AFS_TRACE_LEVEL_VERBOSE,
1121 "AFSNonCachedWrite Requesting extents for fid %08lX-%08lX-%08lX-%08lX Offset %0I64X Length %08lX\n",
1122 pFcb->ObjectInformation->FileId.Cell,
1123 pFcb->ObjectInformation->FileId.Volume,
1124 pFcb->ObjectInformation->FileId.Vnode,
1125 pFcb->ObjectInformation->FileId.Unique,
1126 StartingByte.QuadPart,
1129 ntStatus = AFSRequestExtentsAsync( pFcb,
1134 if (!NT_SUCCESS(ntStatus))
1137 AFSDbgTrace(( AFS_SUBSYSTEM_IO_PROCESSING,
1138 AFS_TRACE_LEVEL_ERROR,
1139 "AFSNonCachedWrite (%p) Failed to request extents Status %08lX\n",
1143 try_return( ntStatus);
1146 KeQueryTickCount( &liLastRequestTime);
1151 AFSDbgTrace(( AFS_SUBSYSTEM_LOCK_PROCESSING,
1152 AFS_TRACE_LEVEL_VERBOSE,
1153 "AFSNonCachedWrite Acquiring Fcb extents lock %p SHARED %08lX\n",
1154 &pFcb->NPFcb->Specific.File.ExtentsResource,
1155 PsGetCurrentThread()));
1157 ASSERT( !ExIsResourceAcquiredLite( &pFcb->NPFcb->Specific.File.ExtentsResource ));
1159 AFSAcquireShared( &pFcb->NPFcb->Specific.File.ExtentsResource, TRUE );
1162 pStartExtent = NULL;
1163 pIgnoreExtent = NULL;
1165 if ( AFSDoExtentsMapRegion( pFcb, &StartingByte, ByteCount, &pStartExtent, &pIgnoreExtent ))
1170 KeClearEvent( &pFcb->NPFcb->Specific.File.ExtentsRequestComplete );
1172 AFSDbgTrace(( AFS_SUBSYSTEM_LOCK_PROCESSING,
1173 AFS_TRACE_LEVEL_VERBOSE,
1174 "AFSNonCachedWrite Releasing(1) Fcb extents lock %p SHARED %08lX\n",
1175 &pFcb->NPFcb->Specific.File.ExtentsResource,
1176 PsGetCurrentThread()));
1178 AFSReleaseResource( &pFcb->NPFcb->Specific.File.ExtentsResource );
1182 // We will re-request the extents after waiting for them
1185 KeQueryTickCount( &liCurrentTime);
1187 if( liCurrentTime.QuadPart - liLastRequestTime.QuadPart >= pControlDevExt->Specific.Control.ExtentRequestTimeCount.QuadPart)
1190 AFSDbgTrace(( AFS_SUBSYSTEM_EXTENT_PROCESSING,
1191 AFS_TRACE_LEVEL_VERBOSE,
1192 "AFSNonCachedWrite Requesting extents, again, for fid %08lX-%08lX-%08lX-%08lX Offset %0I64X Length %08lX\n",
1193 pFcb->ObjectInformation->FileId.Cell,
1194 pFcb->ObjectInformation->FileId.Volume,
1195 pFcb->ObjectInformation->FileId.Vnode,
1196 pFcb->ObjectInformation->FileId.Unique,
1197 StartingByte.QuadPart,
1200 ntStatus = AFSRequestExtentsAsync( pFcb,
1205 if (!NT_SUCCESS(ntStatus))
1208 AFSDbgTrace(( AFS_SUBSYSTEM_IO_PROCESSING,
1209 AFS_TRACE_LEVEL_ERROR,
1210 "AFSNonCachedWrite (%p) Failed to request extents Status %08lX\n",
1214 try_return( ntStatus);
1217 KeQueryTickCount( &liLastRequestTime);
1221 AFSDbgTrace(( AFS_SUBSYSTEM_EXTENT_PROCESSING,
1222 AFS_TRACE_LEVEL_VERBOSE,
1223 "AFSNonCachedWrite Waiting for extents for fid %08lX-%08lX-%08lX-%08lX Offset %0I64X Length %08lX\n",
1224 pFcb->ObjectInformation->FileId.Cell,
1225 pFcb->ObjectInformation->FileId.Volume,
1226 pFcb->ObjectInformation->FileId.Vnode,
1227 pFcb->ObjectInformation->FileId.Unique,
1228 StartingByte.QuadPart,
1235 ntStatus = AFSWaitForExtentMapping ( pFcb, pCcb);
1237 if (!NT_SUCCESS(ntStatus))
1240 AFSDbgTrace(( AFS_SUBSYSTEM_EXTENT_PROCESSING,
1241 AFS_TRACE_LEVEL_ERROR,
1242 "AFSNonCachedWrite Failed wait for extents for fid %08lX-%08lX-%08lX-%08lX Offset %0I64X Length %08lX Status %08lX\n",
1243 pFcb->ObjectInformation->FileId.Cell,
1244 pFcb->ObjectInformation->FileId.Volume,
1245 pFcb->ObjectInformation->FileId.Vnode,
1246 pFcb->ObjectInformation->FileId.Unique,
1247 StartingByte.QuadPart,
1251 try_return( ntStatus);
1256 // As per the read path -
1259 AFSDbgTrace(( AFS_SUBSYSTEM_EXTENT_PROCESSING,
1260 AFS_TRACE_LEVEL_VERBOSE,
1261 "AFSNonCachedWrite Extents located for fid %08lX-%08lX-%08lX-%08lX Offset %0I64X Length %08lX\n",
1262 pFcb->ObjectInformation->FileId.Cell,
1263 pFcb->ObjectInformation->FileId.Volume,
1264 pFcb->ObjectInformation->FileId.Vnode,
1265 pFcb->ObjectInformation->FileId.Unique,
1266 StartingByte.QuadPart,
1269 ntStatus = AFSGetExtents( pFcb,
1276 if (!NT_SUCCESS(ntStatus))
1279 AFSDbgTrace(( AFS_SUBSYSTEM_IO_PROCESSING,
1280 AFS_TRACE_LEVEL_ERROR,
1281 "AFSNonCachedWrite (%p) Failed to retrieve mapped extents Status %08lX\n",
1285 try_return( ntStatus );
1288 AFSDbgTrace(( AFS_SUBSYSTEM_IO_PROCESSING,
1289 AFS_TRACE_LEVEL_VERBOSE,
1290 "AFSNonCachedWrite (%p) Successfully retrieved map extents count %d run count %d\n",
1295 if( BooleanFlagOn( AFSLibControlFlags, AFS_REDIR_LIB_FLAGS_NONPERSISTENT_CACHE))
1298 Irp->IoStatus.Information = ByteCount;
1302 // Setup the MD5 for each extent
1305 AFSSetupMD5Hash( pFcb,
1313 ntStatus = AFSProcessExtentRun( pSystemBuffer,
1319 if (!NT_SUCCESS(ntStatus))
1322 AFSDbgTrace(( AFS_SUBSYSTEM_IO_PROCESSING,
1323 AFS_TRACE_LEVEL_ERROR,
1324 "AFSNonCachedWrite (%p) Failed to process extent run for non-persistent cache Status %08lX\n",
1329 try_return( ntStatus);
1333 // Retrieve the cache file object
1336 pCacheFileObject = AFSReferenceCacheFileObject();
1338 if( pCacheFileObject == NULL)
1341 ntStatus = STATUS_DEVICE_NOT_READY;
1343 AFSDbgTrace(( AFS_SUBSYSTEM_EXTENT_PROCESSING,
1344 AFS_TRACE_LEVEL_ERROR,
1345 "AFSNonCachedWrite Failed to retrieve cache fileobject for fid %08lX-%08lX-%08lX-%08lX Offset %0I64X Length %08lX Status %08lX\n",
1346 pFcb->ObjectInformation->FileId.Cell,
1347 pFcb->ObjectInformation->FileId.Volume,
1348 pFcb->ObjectInformation->FileId.Vnode,
1349 pFcb->ObjectInformation->FileId.Unique,
1350 StartingByte.QuadPart,
1354 try_return( ntStatus);
1357 if (runCount > AFS_MAX_STACK_IO_RUNS)
1360 pIoRuns = (AFSIoRun*) AFSExAllocatePoolWithTag( PagedPool,
1361 runCount * sizeof( AFSIoRun ),
1363 if (NULL == pIoRuns)
1366 AFSDbgTrace(( AFS_SUBSYSTEM_IO_PROCESSING,
1367 AFS_TRACE_LEVEL_ERROR,
1368 "AFSNonCachedWrite (%p) Failed to allocate IO run block\n",
1371 try_return( ntStatus = STATUS_INSUFFICIENT_RESOURCES );
1380 RtlZeroMemory( pIoRuns, runCount * sizeof( AFSIoRun ));
1382 ntStatus = AFSSetupIoRun( IoGetRelatedDeviceObject( pCacheFileObject),
1391 if (!NT_SUCCESS(ntStatus))
1394 AFSDbgTrace(( AFS_SUBSYSTEM_IO_PROCESSING,
1395 AFS_TRACE_LEVEL_ERROR,
1396 "AFSNonCachedWrite (%p) Failed to initialize IO run block Status %08lX\n",
1400 try_return( ntStatus );
1403 AFSReferenceActiveExtents( pStartExtent,
1406 bDerefExtents = TRUE;
1408 AFSDbgTrace(( AFS_SUBSYSTEM_LOCK_PROCESSING,
1409 AFS_TRACE_LEVEL_VERBOSE,
1410 "AFSNonCachedWrite Releasing(2) Fcb extents lock %p SHARED %08lX\n",
1411 &pFcb->NPFcb->Specific.File.ExtentsResource,
1412 PsGetCurrentThread()));
1414 AFSReleaseResource( &pFcb->NPFcb->Specific.File.ExtentsResource );
1417 pGatherIo = (AFSGatherIo*) AFSExAllocatePoolWithTag( NonPagedPool,
1418 sizeof( AFSGatherIo),
1421 if (NULL == pGatherIo)
1424 AFSDbgTrace(( AFS_SUBSYSTEM_IO_PROCESSING,
1425 AFS_TRACE_LEVEL_ERROR,
1426 "AFSNonCachedWrite (%p) Failed to allocate IO gather block\n",
1429 AFSDbgTrace(( AFS_SUBSYSTEM_LOCK_PROCESSING,
1430 AFS_TRACE_LEVEL_VERBOSE,
1431 "AFSNonCachedWrite Acquiring(1) Fcb extents lock %p SHARED %08lX\n",
1432 &pFcb->NPFcb->Specific.File.ExtentsResource,
1433 PsGetCurrentThread()));
1435 AFSAcquireShared( &pFcb->NPFcb->Specific.File.ExtentsResource,
1439 AFSDereferenceActiveExtents( pStartExtent,
1442 try_return (ntStatus = STATUS_INSUFFICIENT_RESOURCES );
1445 RtlZeroMemory( pGatherIo, sizeof( AFSGatherIo ));
1448 // Initialize count to 1, that was we won't get an early
1449 // completion if the first irp completes before the second is
1452 pGatherIo->Count = 1;
1453 pGatherIo->Status = STATUS_SUCCESS;
1454 pGatherIo->MasterIrp = Irp;
1455 pGatherIo->Synchronous = TRUE;
1456 pGatherIo->CompleteMasterIrp = FALSE;
1458 bCompleteIrp = TRUE;
1460 if( pGatherIo->Synchronous)
1462 KeInitializeEvent( &pGatherIo->Event, NotificationEvent, FALSE );
1467 // Setup the MD5 for each extent
1470 AFSSetupMD5Hash( pFcb,
1479 // Pre-emptively set up the count
1482 Irp->IoStatus.Information = ByteCount;
1484 ntStatus = AFSQueueStartIos( pCacheFileObject,
1486 IRP_WRITE_OPERATION | IRP_SYNCHRONOUS_API,
1491 AFSDbgTrace(( AFS_SUBSYSTEM_IO_PROCESSING,
1492 AFS_TRACE_LEVEL_VERBOSE,
1493 "AFSNonCachedWrite (%p) AFSStartIos completed Status %08lX\n",
1497 if( !NT_SUCCESS( ntStatus))
1500 AFSDbgTrace(( AFS_SUBSYSTEM_LOCK_PROCESSING,
1501 AFS_TRACE_LEVEL_VERBOSE,
1502 "AFSNonCachedWrite Acquiring(2) Fcb extents lock %p SHARED %08lX\n",
1503 &pFcb->NPFcb->Specific.File.ExtentsResource,
1504 PsGetCurrentThread()));
1506 AFSAcquireShared( &pFcb->NPFcb->Specific.File.ExtentsResource,
1510 AFSDereferenceActiveExtents( pStartExtent,
1513 try_return( ntStatus);
1517 // Wait for completion of All IOs we started.
1520 ntStatus = KeWaitForSingleObject( &pGatherIo->Event,
1526 if( NT_SUCCESS( ntStatus))
1529 ntStatus = pGatherIo->Status;
1532 if( !NT_SUCCESS( ntStatus))
1535 AFSDbgTrace(( AFS_SUBSYSTEM_LOCK_PROCESSING,
1536 AFS_TRACE_LEVEL_VERBOSE,
1537 "AFSNonCachedWrite Acquiring(3) Fcb extents lock %p SHARED %08lX\n",
1538 &pFcb->NPFcb->Specific.File.ExtentsResource,
1539 PsGetCurrentThread()));
1541 AFSAcquireShared( &pFcb->NPFcb->Specific.File.ExtentsResource,
1545 AFSDereferenceActiveExtents( pStartExtent,
1548 try_return( ntStatus);
1553 if( NT_SUCCESS( ntStatus) &&
1554 pStartExtent != NULL &&
1555 Irp->IoStatus.Information > 0)
1561 AFSAcquireShared( &pFcb->NPFcb->Specific.File.ExtentsResource,
1567 // Since this is dirty we can mark the extents dirty now.
1568 // AFSMarkDirty will dereference the extents. Do not call
1569 // AFSDereferenceActiveExtents() in this code path.
1581 // This was an uncached user write - tell the server to do
1582 // the flush when the worker thread next wakes up
1584 pFcb->Specific.File.LastServerFlush.QuadPart = 0;
1588 if( pCacheFileObject != NULL)
1590 AFSReleaseCacheFileObject( pCacheFileObject);
1593 AFSDbgTrace(( AFS_SUBSYSTEM_IO_PROCESSING,
1594 AFS_TRACE_LEVEL_VERBOSE,
1595 "AFSNonCachedWrite (FO: %p) StartingByte %08lX:%08lX Length %08lX Status %08lX\n",
1597 StartingByte.HighPart,
1598 StartingByte.LowPart,
1602 if (NT_SUCCESS(ntStatus) &&
1607 pFileObject->CurrentByteOffset.QuadPart = StartingByte.QuadPart + ByteCount;
1613 AFSDbgTrace(( AFS_SUBSYSTEM_LOCK_PROCESSING,
1614 AFS_TRACE_LEVEL_VERBOSE,
1615 "AFSNonCachedWrite Releasing Fcb extents lock %p SHARED %08lX\n",
1616 &pFcb->NPFcb->Specific.File.ExtentsResource,
1617 PsGetCurrentThread()));
1619 AFSReleaseResource( &pFcb->NPFcb->Specific.File.ExtentsResource );
1624 AFSExFreePoolWithTag(pGatherIo, AFS_GATHER_TAG);
1627 if( NULL != pIoRuns &&
1628 stIoRuns != pIoRuns)
1630 AFSExFreePoolWithTag(pIoRuns, AFS_IO_RUN_TAG);
1636 AFSDbgTrace(( AFS_SUBSYSTEM_IO_PROCESSING,
1637 AFS_TRACE_LEVEL_VERBOSE,
1638 "AFSNonCachedWrite Completing Irp %p Status %08lX Info %08lX\n",
1641 Irp->IoStatus.Information));
1643 AFSCompleteRequest( Irp, ntStatus);
1652 AFSNonCachedWriteDirect( IN PDEVICE_OBJECT DeviceObject,
1654 IN LARGE_INTEGER StartingByte,
1657 NTSTATUS ntStatus = STATUS_UNSUCCESSFUL;
1658 VOID *pSystemBuffer = NULL;
1659 BOOLEAN bPagingIo = BooleanFlagOn( Irp->Flags, IRP_PAGING_IO);
1660 IO_STACK_LOCATION *pIrpSp = IoGetCurrentIrpStackLocation( Irp);
1661 PFILE_OBJECT pFileObject = pIrpSp->FileObject;
1662 AFSFcb *pFcb = (AFSFcb *)pFileObject->FsContext;
1663 AFSCcb *pCcb = (AFSCcb *)pFileObject->FsContext2;
1664 BOOLEAN bSynchronousFo = BooleanFlagOn( pFileObject->Flags, FO_SYNCHRONOUS_IO);
1665 BOOLEAN bNoIntermediateBuffering = BooleanFlagOn( pFileObject->Flags, FO_NO_INTERMEDIATE_BUFFERING);
1666 AFSDeviceExt *pDevExt = (AFSDeviceExt *)DeviceObject->DeviceExtension;
1667 AFSFileIOCB stFileIORequest;
1668 AFSFileIOResultCB stFileIOResult;
1669 ULONG ulResultLen = 0;
1674 Irp->IoStatus.Information = 0;
1676 AFSDbgTrace(( AFS_SUBSYSTEM_IO_PROCESSING,
1677 AFS_TRACE_LEVEL_VERBOSE,
1678 "AFSNonCachedWriteDirect (FO: %p) StartingByte %08lX:%08lX Length %08lX\n",
1680 StartingByte.HighPart,
1681 StartingByte.LowPart,
1684 if (ByteCount > pDevExt->Specific.RDR.MaxIo.QuadPart)
1687 AFSDbgTrace(( AFS_SUBSYSTEM_IO_PROCESSING,
1688 AFS_TRACE_LEVEL_ERROR,
1689 "AFSNonCachedWriteDirect (%p) Request %08lX Actual %08lX larger than MaxIO %I64X\n",
1692 pIrpSp->Parameters.Write.Length,
1693 pDevExt->Specific.RDR.MaxIo.QuadPart));
1695 try_return( ntStatus = STATUS_UNSUCCESSFUL);
1699 // Get the mapping for the buffer
1701 pSystemBuffer = AFSLockSystemBuffer( Irp,
1704 if( pSystemBuffer == NULL)
1707 AFSDbgTrace(( AFS_SUBSYSTEM_IO_PROCESSING,
1708 AFS_TRACE_LEVEL_ERROR,
1709 "AFSNonCachedWriteDirect (%p) Failed to map system buffer\n",
1712 try_return( ntStatus = STATUS_INSUFFICIENT_RESOURCES);
1716 // Issue the request at the service for processing
1719 ulResultLen = sizeof( AFSFileIOResultCB);
1721 RtlZeroMemory( &stFileIORequest,
1722 sizeof( AFSFileIOCB));
1724 RtlZeroMemory( &stFileIOResult,
1725 sizeof( AFSFileIOResultCB));
1727 stFileIORequest.SystemIOBuffer = pSystemBuffer;
1729 stFileIORequest.SystemIOBufferMdl = Irp->MdlAddress;
1731 stFileIORequest.IOLength = ByteCount;
1733 stFileIORequest.IOOffset = StartingByte;
1735 ulFlags = AFS_REQUEST_FLAG_SYNCHRONOUS;
1737 if ( bNoIntermediateBuffering)
1740 ulFlags |= AFS_REQUEST_FLAG_CACHE_BYPASS;
1744 // Update file metadata
1747 stFileIORequest.EndOfFile = pFcb->ObjectInformation->EndOfFile;
1749 stFileIORequest.CreateTime = pFcb->ObjectInformation->CreationTime;
1751 stFileIORequest.ChangeTime = pFcb->ObjectInformation->ChangeTime;
1753 stFileIORequest.LastAccessTime = pFcb->ObjectInformation->LastAccessTime;
1755 stFileIORequest.LastWriteTime = pFcb->ObjectInformation->LastWriteTime;
1758 // Write the data to the service
1761 ntStatus = AFSProcessRequest( AFS_REQUEST_TYPE_PROCESS_WRITE_FILE,
1764 &pCcb->DirectoryCB->NameInformation.FileName,
1765 &pFcb->ObjectInformation->FileId,
1766 pFcb->ObjectInformation->VolumeCB->VolumeInformation.Cell,
1767 pFcb->ObjectInformation->VolumeCB->VolumeInformation.CellLength,
1769 sizeof( AFSFileIOCB),
1773 if( NT_SUCCESS( ntStatus))
1776 Irp->IoStatus.Information = (ULONG_PTR)stFileIOResult.Length;
1781 AFSDbgTrace(( AFS_SUBSYSTEM_IO_PROCESSING,
1782 AFS_TRACE_LEVEL_ERROR,
1783 "AFSNonCachedWriteDirect (%p) Failed to send write to service Status %08lX\n",
1790 AFSDbgTrace(( AFS_SUBSYSTEM_IO_PROCESSING,
1791 AFS_TRACE_LEVEL_VERBOSE,
1792 "AFSNonCachedWriteDirect (FO: %p) StartingByte %08lX:%08lX Length %08lX Status %08lX\n",
1794 StartingByte.HighPart,
1795 StartingByte.LowPart,
1799 if (NT_SUCCESS(ntStatus) &&
1804 pFileObject->CurrentByteOffset.QuadPart = StartingByte.QuadPart + ByteCount;
1807 AFSDbgTrace(( AFS_SUBSYSTEM_IO_PROCESSING,
1808 AFS_TRACE_LEVEL_VERBOSE,
1809 "AFSNonCachedWriteDirect Completing Irp %p Status %08lX Info %08lX\n",
1812 Irp->IoStatus.Information));
1814 AFSCompleteRequest( Irp, ntStatus);
1822 AFSCachedWrite( IN PDEVICE_OBJECT DeviceObject,
1824 IN LARGE_INTEGER StartingByte,
1826 IN BOOLEAN ForceFlush)
1828 UNREFERENCED_PARAMETER(DeviceObject);
1829 PVOID pSystemBuffer = NULL;
1830 NTSTATUS ntStatus = STATUS_SUCCESS;
1831 IO_STATUS_BLOCK iosbFlush;
1832 IO_STACK_LOCATION *pIrpSp = IoGetCurrentIrpStackLocation( Irp);
1833 PFILE_OBJECT pFileObject = pIrpSp->FileObject;
1834 AFSFcb *pFcb = (AFSFcb *)pFileObject->FsContext;
1835 BOOLEAN bSynchronousFo = BooleanFlagOn( pFileObject->Flags, FO_SYNCHRONOUS_IO);
1836 ULONG ulCurrentIO = 0, ulTotalLen = ByteCount;
1837 PMDL pCurrentMdl = Irp->MdlAddress;
1838 LARGE_INTEGER liCurrentOffset;
1843 Irp->IoStatus.Information = 0;
1845 if( BooleanFlagOn( pIrpSp->MinorFunction, IRP_MN_MDL))
1851 CcPrepareMdlWrite( pFileObject,
1857 ntStatus = Irp->IoStatus.Status;
1859 __except( EXCEPTION_EXECUTE_HANDLER)
1861 ntStatus = GetExceptionCode();
1863 AFSDbgTrace(( AFS_SUBSYSTEM_IO_PROCESSING,
1864 AFS_TRACE_LEVEL_ERROR,
1865 "AFSCachedWrite (%p) Exception thrown while preparing mdl write Status %08lX\n",
1870 if( !NT_SUCCESS( ntStatus))
1873 AFSDbgTrace(( AFS_SUBSYSTEM_IO_PROCESSING,
1874 AFS_TRACE_LEVEL_ERROR,
1875 "AFSCachedWrite (%p) Failed to process MDL write Status %08lX\n",
1879 if ( Irp->IoStatus.Information > 0)
1882 CcMdlWriteComplete( pFileObject,
1887 // Mdl is now Deallocated
1890 Irp->MdlAddress = NULL;
1894 try_return( ntStatus);
1897 liCurrentOffset.QuadPart = StartingByte.QuadPart;
1899 while( ulTotalLen > 0)
1902 ntStatus = STATUS_SUCCESS;
1904 if( pCurrentMdl != NULL)
1907 pSystemBuffer = MmGetSystemAddressForMdlSafe( pCurrentMdl,
1908 NormalPagePriority);
1910 ulCurrentIO = MmGetMdlByteCount( pCurrentMdl);
1912 if( ulCurrentIO > ulTotalLen)
1914 ulCurrentIO = ulTotalLen;
1920 pSystemBuffer = AFSLockSystemBuffer( Irp,
1923 ulCurrentIO = ulTotalLen;
1926 if( pSystemBuffer == NULL)
1929 AFSDbgTrace(( AFS_SUBSYSTEM_IO_PROCESSING,
1930 AFS_TRACE_LEVEL_ERROR,
1931 "AFSCachedWrite (%p) Failed to lock system buffer\n",
1934 try_return( ntStatus = STATUS_INSUFFICIENT_RESOURCES);
1940 if( !CcCopyWrite( pFileObject,
1947 // Failed to process request.
1950 AFSDbgTrace(( AFS_SUBSYSTEM_FILE_PROCESSING,
1951 AFS_TRACE_LEVEL_ERROR,
1952 "AFSCachedWrite (%p) Failed to issue CcCopyWrite %wZ @ %0I64X Status %08lX\n",
1954 &pFileObject->FileName,
1955 liCurrentOffset.QuadPart,
1956 Irp->IoStatus.Status));
1958 try_return( ntStatus = STATUS_UNSUCCESSFUL);
1961 __except( EXCEPTION_EXECUTE_HANDLER)
1964 ntStatus = GetExceptionCode();
1966 AFSDbgTrace(( AFS_SUBSYSTEM_IO_PROCESSING,
1967 AFS_TRACE_LEVEL_ERROR,
1968 "AFSCachedWrite (%p) CcCopyWrite Threw exception %wZ @ %0I64X Status %08lX\n",
1970 &pFileObject->FileName,
1971 liCurrentOffset.QuadPart,
1975 if( !NT_SUCCESS( ntStatus))
1977 try_return( ntStatus);
1981 BooleanFlagOn(pFileObject->Flags, (FO_NO_INTERMEDIATE_BUFFERING + FO_WRITE_THROUGH)))
1987 // We have detected a file we do a write through with.
1990 CcFlushCache(&pFcb->NPFcb->SectionObjectPointers,
1995 if( !NT_SUCCESS( iosbFlush.Status))
1998 AFSDbgTrace(( AFS_SUBSYSTEM_IO_PROCESSING,
1999 AFS_TRACE_LEVEL_ERROR,
2000 "AFSCachedWrite (%p) CcFlushCache failure %wZ FID %08lX-%08lX-%08lX-%08lX Status 0x%08lX Bytes 0x%08lX\n",
2002 &pFileObject->FileName,
2003 pFcb->ObjectInformation->FileId.Cell,
2004 pFcb->ObjectInformation->FileId.Volume,
2005 pFcb->ObjectInformation->FileId.Vnode,
2006 pFcb->ObjectInformation->FileId.Unique,
2008 iosbFlush.Information));
2010 try_return( ntStatus = iosbFlush.Status);
2013 __except( EXCEPTION_EXECUTE_HANDLER)
2016 ntStatus = GetExceptionCode();
2018 AFSDbgTrace(( AFS_SUBSYSTEM_IO_PROCESSING,
2019 AFS_TRACE_LEVEL_ERROR,
2020 "AFSCachedWrite (%p) CcFlushCache Threw exception %wZ @ %0I64X Status %08lX\n",
2022 &pFileObject->FileName,
2023 liCurrentOffset.QuadPart,
2026 try_return( ntStatus);
2030 if( ulTotalLen <= ulCurrentIO)
2035 liCurrentOffset.QuadPart += ulCurrentIO;
2037 ulTotalLen -= ulCurrentIO;
2039 pCurrentMdl = pCurrentMdl->Next;
2044 if( NT_SUCCESS( ntStatus))
2047 Irp->IoStatus.Information = ByteCount;
2050 BooleanFlagOn(pFileObject->Flags, (FO_NO_INTERMEDIATE_BUFFERING + FO_WRITE_THROUGH)))
2053 // Write through asked for... Set things so that we get
2054 // flush when the worker thread next wakes up
2056 pFcb->Specific.File.LastServerFlush.QuadPart = 0;
2060 AFSCompleteRequest( Irp,
2068 // Called with Fcb->NPFcb->SectionObjectResource and Fcb->NPFcb->Resource held
2073 AFSExtendingWrite( IN AFSFcb *Fcb,
2074 IN PFILE_OBJECT FileObject,
2075 IN LONGLONG NewLength)
2077 LARGE_INTEGER liSaveFileSize = Fcb->Header.FileSize;
2078 LARGE_INTEGER liSaveAllocation = Fcb->Header.AllocationSize;
2079 NTSTATUS ntStatus = STATUS_SUCCESS;
2080 AFSCcb *pCcb = (AFSCcb *)FileObject->FsContext2;
2082 if( NewLength > Fcb->Header.AllocationSize.QuadPart)
2085 Fcb->Header.AllocationSize.QuadPart = NewLength;
2087 Fcb->ObjectInformation->AllocationSize = Fcb->Header.AllocationSize;
2090 if( NewLength > Fcb->Header.FileSize.QuadPart)
2093 Fcb->Header.FileSize.QuadPart = NewLength;
2095 Fcb->ObjectInformation->EndOfFile = Fcb->Header.FileSize;
2102 ntStatus = AFSUpdateFileInformation( &Fcb->ObjectInformation->ParentFileId,
2103 Fcb->ObjectInformation,
2106 if (NT_SUCCESS(ntStatus))
2109 KeQuerySystemTime( &Fcb->ObjectInformation->ChangeTime);
2111 SetFlag( Fcb->Flags, AFS_FCB_FLAG_FILE_MODIFIED | AFS_FCB_FLAG_UPDATE_CHANGE_TIME);
2114 // If the file is currently cached, then let the MM know about the extension
2116 // The CcSetFileSizes call should be made with only the PagingResource held
2117 // which we are currently not holding.
2120 if( CcIsFileCached( FileObject))
2122 CcSetFileSizes( FileObject,
2123 (PCC_FILE_SIZES)&Fcb->Header.AllocationSize);
2128 Fcb->Header.FileSize = liSaveFileSize;
2129 Fcb->Header.AllocationSize = liSaveAllocation;
2133 // DownConvert file resource to shared
2135 ExConvertExclusiveToSharedLite( &Fcb->NPFcb->Resource);
2141 AFSShareWrite( IN PDEVICE_OBJECT DeviceObject,
2145 UNREFERENCED_PARAMETER(DeviceObject);
2146 NTSTATUS ntStatus = STATUS_SUCCESS;
2147 PIO_STACK_LOCATION pIrpSp = IoGetCurrentIrpStackLocation( Irp);
2148 AFSFcb *pFcb = NULL;
2149 AFSCcb *pCcb = NULL;
2150 AFSPipeIORequestCB *pIoRequest = NULL;
2151 void *pBuffer = NULL;
2152 AFSPipeIOResultCB stIoResult;
2153 ULONG ulBytesReturned = 0;
2158 pCcb = (AFSCcb *)pIrpSp->FileObject->FsContext2;
2160 AFSDbgTrace(( AFS_SUBSYSTEM_PIPE_PROCESSING,
2161 AFS_TRACE_LEVEL_VERBOSE,
2162 "AFSShareWrite On pipe %wZ Length %08lX\n",
2163 &pCcb->DirectoryCB->NameInformation.FileName,
2164 pIrpSp->Parameters.Write.Length));
2166 if( pIrpSp->Parameters.Write.Length == 0)
2170 // Nothing to do in this case
2173 try_return( ntStatus);
2177 // Retrieve the buffer for the read request
2180 pBuffer = AFSLockSystemBuffer( Irp,
2181 pIrpSp->Parameters.Write.Length);
2183 if( pBuffer == NULL)
2186 AFSDbgTrace(( AFS_SUBSYSTEM_PIPE_PROCESSING,
2187 AFS_TRACE_LEVEL_ERROR,
2188 "AFSShareWrite Failed to map buffer on pipe %wZ\n",
2189 &pCcb->DirectoryCB->NameInformation.FileName));
2191 try_return( ntStatus = STATUS_INSUFFICIENT_RESOURCES);
2194 pFcb = (AFSFcb *)pIrpSp->FileObject->FsContext;
2196 AFSAcquireShared( &pFcb->NPFcb->Resource,
2199 pIoRequest = (AFSPipeIORequestCB *)AFSExAllocatePoolWithTag( PagedPool,
2200 sizeof( AFSPipeIORequestCB) +
2201 pIrpSp->Parameters.Write.Length,
2202 AFS_GENERIC_MEMORY_14_TAG);
2204 if( pIoRequest == NULL)
2207 try_return( ntStatus = STATUS_INSUFFICIENT_RESOURCES);
2210 RtlZeroMemory( pIoRequest,
2211 sizeof( AFSPipeIORequestCB) + pIrpSp->Parameters.Write.Length);
2213 pIoRequest->RequestId = pCcb->RequestID;
2215 pIoRequest->RootId = pFcb->ObjectInformation->VolumeCB->ObjectInformation.FileId;
2217 pIoRequest->BufferLength = pIrpSp->Parameters.Write.Length;
2219 RtlCopyMemory( (void *)((char *)pIoRequest + sizeof( AFSPipeIORequestCB)),
2221 pIrpSp->Parameters.Write.Length);
2223 stIoResult.BytesProcessed = 0;
2225 ulBytesReturned = sizeof( AFSPipeIOResultCB);
2228 // Issue the open request to the service
2231 ntStatus = AFSProcessRequest( AFS_REQUEST_TYPE_PIPE_WRITE,
2232 AFS_REQUEST_FLAG_SYNCHRONOUS,
2234 &pCcb->DirectoryCB->NameInformation.FileName,
2239 sizeof( AFSPipeIORequestCB) +
2240 pIrpSp->Parameters.Write.Length,
2244 if( !NT_SUCCESS( ntStatus))
2247 AFSDbgTrace(( AFS_SUBSYSTEM_FILE_PROCESSING,
2248 AFS_TRACE_LEVEL_ERROR,
2249 "AFSShareWrite (%p) Failed service write Status %08lX\n",
2253 try_return( ntStatus);
2256 AFSDbgTrace(( AFS_SUBSYSTEM_PIPE_PROCESSING,
2257 AFS_TRACE_LEVEL_VERBOSE,
2258 "AFSShareWrite Completed on pipe %wZ Length read %08lX\n",
2259 &pCcb->DirectoryCB->NameInformation.FileName,
2260 stIoResult.BytesProcessed));
2262 Irp->IoStatus.Information = stIoResult.BytesProcessed;
2269 AFSReleaseResource( &pFcb->NPFcb->Resource);
2272 if( pIoRequest != NULL)
2275 AFSExFreePoolWithTag( pIoRequest, AFS_GENERIC_MEMORY_14_TAG);