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_ACCESS_DENIED);
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)
595 if (pFcb->Header.ValidDataLength.QuadPart > pFcb->Header.FileSize.QuadPart)
597 liStartingByte = pFcb->Header.ValidDataLength;
601 liStartingByte = pFcb->Header.FileSize;
606 // We have the correct lock - even if we don't end up truncating
612 ASSERT( !ExIsResourceAcquiredLite( &pNPFcb->PagingResource ));
614 AFSDbgTrace(( AFS_SUBSYSTEM_LOCK_PROCESSING,
615 AFS_TRACE_LEVEL_VERBOSE,
616 "AFSCommonWrite Acquiring Fcb lock %p SHARED %08lX\n",
618 PsGetCurrentThread()));
620 AFSAcquireShared( &pNPFcb->Resource,
625 AFSDbgTrace(( AFS_SUBSYSTEM_LOCK_PROCESSING|AFS_SUBSYSTEM_SECTION_OBJECT,
626 AFS_TRACE_LEVEL_VERBOSE,
627 "AFSCommonWrite Acquiring Fcb SectionObject lock %p SHARED %08lX\n",
628 &pNPFcb->SectionObjectResource,
629 PsGetCurrentThread()));
631 AFSAcquireShared( &pNPFcb->SectionObjectResource,
634 bReleaseSectionObject = TRUE;
637 // Have things moved? Are we extending? If so, the the lock isn't OK
639 bLockOK = (liStartingByte.QuadPart + ulByteCount) < pFcb->Header.FileSize.QuadPart;
644 AFSDbgTrace(( AFS_SUBSYSTEM_LOCK_PROCESSING|AFS_SUBSYSTEM_SECTION_OBJECT,
645 AFS_TRACE_LEVEL_VERBOSE,
646 "AFSCommonWrite Releasing Fcb SectionObject lock %p SHARED %08lX\n",
647 &pNPFcb->SectionObjectResource,
648 PsGetCurrentThread()));
650 AFSReleaseResource( &pNPFcb->SectionObjectResource);
652 bReleaseSectionObject = FALSE;
654 AFSReleaseResource( &pNPFcb->Resource);
656 bReleaseMain = FALSE;
667 // Check the BR locks on the file.
670 if ( !FsRtlCheckLockForWriteAccess( &pFcb->Specific.File.FileLock,
674 AFSDbgTrace(( AFS_SUBSYSTEM_IO_PROCESSING,
675 AFS_TRACE_LEVEL_ERROR,
676 "AFSCommonWrite (%p) Request failed due to lock conflict\n",
679 try_return( ntStatus = STATUS_FILE_LOCK_CONFLICT);
685 ntStatus = AFSExtendingWrite( pFcb, pFileObject, (liStartingByte.QuadPart + ulByteCount));
688 // Fcb->NPFcb->Resource is now held SHARED
691 if( !NT_SUCCESS(ntStatus))
694 AFSDbgTrace(( AFS_SUBSYSTEM_IO_PROCESSING,
695 AFS_TRACE_LEVEL_ERROR,
696 "AFSCommonWrite (%p) Failed extending write request Status %08lX\n",
700 try_return( ntStatus );
706 // Fire off the request as appropriate
708 bCompleteIrp = FALSE;
715 // Main resource held Shared
716 // SectionObject resource held exclusive if extending write
719 AFSDbgTrace(( AFS_SUBSYSTEM_IO_PROCESSING,
720 AFS_TRACE_LEVEL_VERBOSE,
721 "AFSCommonWrite (%p) Processing CACHED request Offset %0I64X Len %08lX%s\n",
723 liStartingByte.QuadPart,
725 bRetry ? " RETRY" : ""));
727 ntStatus = AFSCachedWrite( DeviceObject, Irp, liStartingByte, ulByteCount, bForceFlush);
732 AFSDbgTrace(( AFS_SUBSYSTEM_IO_PROCESSING,
733 AFS_TRACE_LEVEL_VERBOSE,
734 "AFSCommonWrite (%p) Processing NON-CACHED request Offset %0I64X Len %08lX%s\n",
736 liStartingByte.QuadPart,
738 bRetry ? " RETRY" : ""));
740 if( BooleanFlagOn( pRDRDevExt->DeviceFlags, AFS_DEVICE_FLAG_DIRECT_SERVICE_IO))
743 ntStatus = AFSNonCachedWriteDirect( DeviceObject, Irp, liStartingByte, ulByteCount);
747 ntStatus = AFSNonCachedWrite( DeviceObject, Irp, liStartingByte, ulByteCount);
753 AFSDbgTrace(( AFS_SUBSYSTEM_IO_PROCESSING,
754 AFS_TRACE_LEVEL_VERBOSE,
755 "AFSCommonWrite (%p) Process complete Status %08lX\n",
759 if ( NT_SUCCESS( ntStatus) &&
760 ntStatus != STATUS_PENDING)
768 pFileObject->CurrentByteOffset.QuadPart = liStartingByte.QuadPart + ulByteCount;
772 // If this extended the VDL, then update it accordingly.
773 // Increasing the VDL does not require a call to CcSetFileSizes.
776 if( liStartingByte.QuadPart + ulByteCount > pFcb->Header.ValidDataLength.QuadPart)
779 pFcb->Header.ValidDataLength.QuadPart = liStartingByte.QuadPart + ulByteCount;
782 if( !BooleanFlagOn( pFcb->Flags, AFS_FCB_FLAG_UPDATE_LAST_WRITE_TIME))
785 SetFlag( pFcb->Flags, AFS_FCB_FLAG_UPDATE_WRITE_TIME);
787 KeQuerySystemTime( &pFcb->ObjectInformation->LastWriteTime);
792 if ( ntStatus != STATUS_PENDING &&
793 !bPagingIo && bNonCachedIo && CcIsFileCached( pFileObject) &&
794 pNPFcb->SectionObjectPointers.DataSectionObject != NULL &&
795 bReleaseSectionObject)
798 // Regardless of whether or not the a non-paging non-cached write
799 // succeeds or fails, if the file is cached the contents of the
800 // cache are no longer up to date. A CcPurgeCacheSection must be
801 // performed to force subsequent cached reads to obtain the data
804 // The Fcb Resource is dropped in order to permit filters that perform
805 // an open via a worker thread in response to a purge to do so without
806 // deadlocking. The SectionObjectResource is held across the purge to
807 // prevent racing with other cache operations.
813 AFSReleaseResource( &pNPFcb->Resource);
815 bReleaseMain = FALSE;
821 if ( !CcPurgeCacheSection( &pNPFcb->SectionObjectPointers,
827 AFSDbgTrace(( AFS_SUBSYSTEM_IO_PROCESSING,
828 AFS_TRACE_LEVEL_WARNING,
829 "AFSCommonWrite CcPurgeCacheSection failure FID %08lX-%08lX-%08lX-%08lX\n",
830 pFcb->ObjectInformation->FileId.Cell,
831 pFcb->ObjectInformation->FileId.Volume,
832 pFcb->ObjectInformation->FileId.Vnode,
833 pFcb->ObjectInformation->FileId.Unique));
835 SetFlag( pFcb->Flags, AFS_FCB_FLAG_PURGE_ON_CLOSE);
838 __except( EXCEPTION_EXECUTE_HANDLER)
841 DWORD ntStatus2 = GetExceptionCode();
845 "EXCEPTION - AFSCommonWrite CcPurgeCacheSection failed FID %08lX-%08lX-%08lX-%08lX Status 0x%08lX\n",
846 pFcb->ObjectInformation->FileId.Cell,
847 pFcb->ObjectInformation->FileId.Volume,
848 pFcb->ObjectInformation->FileId.Vnode,
849 pFcb->ObjectInformation->FileId.Unique,
852 SetFlag( pFcb->Flags, AFS_FCB_FLAG_PURGE_ON_CLOSE);
856 ObDereferenceObject(pFileObject);
858 if( bReleaseSectionObject)
861 AFSDbgTrace(( AFS_SUBSYSTEM_LOCK_PROCESSING|AFS_SUBSYSTEM_SECTION_OBJECT,
862 AFS_TRACE_LEVEL_VERBOSE,
863 "AFSCommonWrite Releasing Fcb SectionObject lock %p EXCL/SHARED %08lX\n",
864 &pNPFcb->SectionObjectResource,
865 PsGetCurrentThread()));
867 AFSReleaseResource( &pNPFcb->SectionObjectResource);
873 AFSReleaseResource( &pNPFcb->PagingResource);
879 AFSReleaseResource( &pNPFcb->Resource);
885 AFSCompleteRequest( Irp,
894 AFSIOCtlWrite( IN PDEVICE_OBJECT DeviceObject,
898 UNREFERENCED_PARAMETER(DeviceObject);
899 NTSTATUS ntStatus = STATUS_SUCCESS;
900 AFSPIOCtlIORequestCB stIORequestCB;
901 PIO_STACK_LOCATION pIrpSp = IoGetCurrentIrpStackLocation( Irp);
904 AFSPIOCtlIOResultCB stIOResultCB;
905 ULONG ulBytesReturned = 0;
906 AFSFileID stParentFID;
911 Irp->IoStatus.Information = 0;
913 RtlZeroMemory( &stIORequestCB,
914 sizeof( AFSPIOCtlIORequestCB));
916 if( pIrpSp->Parameters.Write.Length == 0)
920 // Nothing to do in this case
923 try_return( ntStatus);
926 pFcb = (AFSFcb *)pIrpSp->FileObject->FsContext;
928 pCcb = (AFSCcb *)pIrpSp->FileObject->FsContext2;
930 AFSDbgTrace(( AFS_SUBSYSTEM_LOCK_PROCESSING,
931 AFS_TRACE_LEVEL_VERBOSE,
932 "AFSIOCtlWrite Acquiring Fcb lock %p SHARED %08lX\n",
933 &pFcb->NPFcb->Resource,
934 PsGetCurrentThread()));
936 AFSAcquireShared( &pFcb->NPFcb->Resource,
940 // Get the parent fid to pass to the cm
943 RtlZeroMemory( &stParentFID,
946 if( BooleanFlagOn( pFcb->ObjectInformation->Flags, AFS_OBJECT_FLAGS_PARENT_FID))
950 // The parent directory FID of the node
953 stParentFID = pFcb->ObjectInformation->ParentFileId;
957 // Set the control block up
960 stIORequestCB.RequestId = pCcb->RequestID;
962 if( pFcb->ObjectInformation->VolumeCB != NULL)
964 stIORequestCB.RootId = pFcb->ObjectInformation->VolumeCB->ObjectInformation.FileId;
968 // Lock down the buffer
971 stIORequestCB.MappedBuffer = AFSMapToService( Irp,
972 pIrpSp->Parameters.Write.Length);
974 if( stIORequestCB.MappedBuffer == NULL)
977 try_return( ntStatus = STATUS_INSUFFICIENT_RESOURCES);
980 stIORequestCB.BufferLength = pIrpSp->Parameters.Write.Length;
982 stIOResultCB.BytesProcessed = 0;
984 ulBytesReturned = sizeof( AFSPIOCtlIOResultCB);
987 // Issue the request to the service
990 ntStatus = AFSProcessRequest( AFS_REQUEST_TYPE_PIOCTL_WRITE,
991 AFS_REQUEST_FLAG_SYNCHRONOUS,
997 (void *)&stIORequestCB,
998 sizeof( AFSPIOCtlIORequestCB),
1002 if( !NT_SUCCESS( ntStatus))
1005 try_return( ntStatus);
1009 // Update the length written
1012 Irp->IoStatus.Information = stIOResultCB.BytesProcessed;
1016 if( stIORequestCB.MappedBuffer != NULL)
1019 AFSUnmapServiceMappedBuffer( stIORequestCB.MappedBuffer,
1026 AFSReleaseResource( &pFcb->NPFcb->Resource);
1034 // This function is called when we know we have to read from the AFS Cache.
1036 // It ensures that we have exents for the entirety of the write and
1037 // then pins the extents into memory (meaning that although we may
1038 // add we will not remove). Then it creates a scatter gather write
1039 // and fires off the IRPs
1043 AFSNonCachedWrite( IN PDEVICE_OBJECT DeviceObject,
1045 IN LARGE_INTEGER StartingByte,
1048 NTSTATUS ntStatus = STATUS_UNSUCCESSFUL;
1049 VOID *pSystemBuffer = NULL;
1050 BOOLEAN bPagingIo = BooleanFlagOn( Irp->Flags, IRP_PAGING_IO);
1051 BOOLEAN bLocked = FALSE;
1052 BOOLEAN bCompleteIrp = TRUE;
1053 AFSGatherIo *pGatherIo = NULL;
1054 AFSIoRun *pIoRuns = NULL;
1055 AFSIoRun stIoRuns[AFS_MAX_STACK_IO_RUNS];
1056 ULONG extentsCount = 0, runCount = 0;
1057 AFSExtent *pStartExtent = NULL;
1058 AFSExtent *pIgnoreExtent = NULL;
1059 IO_STACK_LOCATION *pIrpSp = IoGetCurrentIrpStackLocation( Irp);
1060 PFILE_OBJECT pFileObject = pIrpSp->FileObject;
1061 AFSFcb *pFcb = (AFSFcb *)pFileObject->FsContext;
1062 AFSCcb *pCcb = (AFSCcb *)pFileObject->FsContext2;
1063 BOOLEAN bSynchronousFo = BooleanFlagOn( pFileObject->Flags, FO_SYNCHRONOUS_IO);
1064 AFSDeviceExt *pDevExt = (AFSDeviceExt *)DeviceObject->DeviceExtension;
1065 LARGE_INTEGER liCurrentTime, liLastRequestTime;
1066 AFSDeviceExt *pControlDevExt = (AFSDeviceExt *)AFSControlDeviceObject->DeviceExtension;
1067 PFILE_OBJECT pCacheFileObject = NULL;
1068 BOOLEAN bDerefExtents = FALSE;
1073 AFSDbgTrace(( AFS_SUBSYSTEM_IO_PROCESSING,
1074 AFS_TRACE_LEVEL_VERBOSE,
1075 "AFSNonCachedWrite (FO: %p) StartingByte %08lX:%08lX Length %08lX\n",
1077 StartingByte.HighPart,
1078 StartingByte.LowPart,
1081 if (ByteCount > pDevExt->Specific.RDR.MaxIo.QuadPart)
1084 AFSDbgTrace(( AFS_SUBSYSTEM_IO_PROCESSING,
1085 AFS_TRACE_LEVEL_ERROR,
1086 "AFSNonCachedWrite (%p) Request %08lX Actual %08lX larger than MaxIO %I64X\n",
1089 pIrpSp->Parameters.Write.Length,
1090 pDevExt->Specific.RDR.MaxIo.QuadPart));
1092 try_return( ntStatus = STATUS_UNSUCCESSFUL);
1096 // Get the mapping for the buffer
1098 pSystemBuffer = AFSLockSystemBuffer( Irp,
1101 if( pSystemBuffer == NULL)
1104 AFSDbgTrace(( AFS_SUBSYSTEM_IO_PROCESSING,
1105 AFS_TRACE_LEVEL_ERROR,
1106 "AFSNonCachedWrite (%p) Failed to map system buffer\n",
1109 try_return( ntStatus = STATUS_INSUFFICIENT_RESOURCES);
1114 // Provoke a get of the extents - if we need to.
1117 AFSDbgTrace(( AFS_SUBSYSTEM_EXTENT_PROCESSING,
1118 AFS_TRACE_LEVEL_VERBOSE,
1119 "AFSNonCachedWrite Requesting extents for fid %08lX-%08lX-%08lX-%08lX Offset %0I64X Length %08lX\n",
1120 pFcb->ObjectInformation->FileId.Cell,
1121 pFcb->ObjectInformation->FileId.Volume,
1122 pFcb->ObjectInformation->FileId.Vnode,
1123 pFcb->ObjectInformation->FileId.Unique,
1124 StartingByte.QuadPart,
1127 ntStatus = AFSRequestExtentsAsync( pFcb,
1132 if (!NT_SUCCESS(ntStatus))
1135 AFSDbgTrace(( AFS_SUBSYSTEM_IO_PROCESSING,
1136 AFS_TRACE_LEVEL_ERROR,
1137 "AFSNonCachedWrite (%p) Failed to request extents Status %08lX\n",
1141 try_return( ntStatus);
1144 KeQueryTickCount( &liLastRequestTime);
1149 AFSDbgTrace(( AFS_SUBSYSTEM_LOCK_PROCESSING,
1150 AFS_TRACE_LEVEL_VERBOSE,
1151 "AFSNonCachedWrite Acquiring Fcb extents lock %p SHARED %08lX\n",
1152 &pFcb->NPFcb->Specific.File.ExtentsResource,
1153 PsGetCurrentThread()));
1155 ASSERT( !ExIsResourceAcquiredLite( &pFcb->NPFcb->Specific.File.ExtentsResource ));
1157 AFSAcquireShared( &pFcb->NPFcb->Specific.File.ExtentsResource, TRUE );
1160 pStartExtent = NULL;
1161 pIgnoreExtent = NULL;
1163 if ( AFSDoExtentsMapRegion( pFcb, &StartingByte, ByteCount, &pStartExtent, &pIgnoreExtent ))
1168 KeClearEvent( &pFcb->NPFcb->Specific.File.ExtentsRequestComplete );
1170 AFSDbgTrace(( AFS_SUBSYSTEM_LOCK_PROCESSING,
1171 AFS_TRACE_LEVEL_VERBOSE,
1172 "AFSNonCachedWrite Releasing(1) Fcb extents lock %p SHARED %08lX\n",
1173 &pFcb->NPFcb->Specific.File.ExtentsResource,
1174 PsGetCurrentThread()));
1176 AFSReleaseResource( &pFcb->NPFcb->Specific.File.ExtentsResource );
1180 // We will re-request the extents after waiting for them
1183 KeQueryTickCount( &liCurrentTime);
1185 if( liCurrentTime.QuadPart - liLastRequestTime.QuadPart >= pControlDevExt->Specific.Control.ExtentRequestTimeCount.QuadPart)
1188 AFSDbgTrace(( AFS_SUBSYSTEM_EXTENT_PROCESSING,
1189 AFS_TRACE_LEVEL_VERBOSE,
1190 "AFSNonCachedWrite Requesting extents, again, for fid %08lX-%08lX-%08lX-%08lX Offset %0I64X Length %08lX\n",
1191 pFcb->ObjectInformation->FileId.Cell,
1192 pFcb->ObjectInformation->FileId.Volume,
1193 pFcb->ObjectInformation->FileId.Vnode,
1194 pFcb->ObjectInformation->FileId.Unique,
1195 StartingByte.QuadPart,
1198 ntStatus = AFSRequestExtentsAsync( pFcb,
1203 if (!NT_SUCCESS(ntStatus))
1206 AFSDbgTrace(( AFS_SUBSYSTEM_IO_PROCESSING,
1207 AFS_TRACE_LEVEL_ERROR,
1208 "AFSNonCachedWrite (%p) Failed to request extents Status %08lX\n",
1212 try_return( ntStatus);
1215 KeQueryTickCount( &liLastRequestTime);
1219 AFSDbgTrace(( AFS_SUBSYSTEM_EXTENT_PROCESSING,
1220 AFS_TRACE_LEVEL_VERBOSE,
1221 "AFSNonCachedWrite Waiting for extents for fid %08lX-%08lX-%08lX-%08lX Offset %0I64X Length %08lX\n",
1222 pFcb->ObjectInformation->FileId.Cell,
1223 pFcb->ObjectInformation->FileId.Volume,
1224 pFcb->ObjectInformation->FileId.Vnode,
1225 pFcb->ObjectInformation->FileId.Unique,
1226 StartingByte.QuadPart,
1233 ntStatus = AFSWaitForExtentMapping ( pFcb, pCcb);
1235 if (!NT_SUCCESS(ntStatus))
1238 AFSDbgTrace(( AFS_SUBSYSTEM_EXTENT_PROCESSING,
1239 AFS_TRACE_LEVEL_ERROR,
1240 "AFSNonCachedWrite Failed wait for extents for fid %08lX-%08lX-%08lX-%08lX Offset %0I64X Length %08lX Status %08lX\n",
1241 pFcb->ObjectInformation->FileId.Cell,
1242 pFcb->ObjectInformation->FileId.Volume,
1243 pFcb->ObjectInformation->FileId.Vnode,
1244 pFcb->ObjectInformation->FileId.Unique,
1245 StartingByte.QuadPart,
1249 try_return( ntStatus);
1254 // As per the read path -
1257 AFSDbgTrace(( AFS_SUBSYSTEM_EXTENT_PROCESSING,
1258 AFS_TRACE_LEVEL_VERBOSE,
1259 "AFSNonCachedWrite Extents located for fid %08lX-%08lX-%08lX-%08lX Offset %0I64X Length %08lX\n",
1260 pFcb->ObjectInformation->FileId.Cell,
1261 pFcb->ObjectInformation->FileId.Volume,
1262 pFcb->ObjectInformation->FileId.Vnode,
1263 pFcb->ObjectInformation->FileId.Unique,
1264 StartingByte.QuadPart,
1267 ntStatus = AFSGetExtents( pFcb,
1274 if (!NT_SUCCESS(ntStatus))
1277 AFSDbgTrace(( AFS_SUBSYSTEM_IO_PROCESSING,
1278 AFS_TRACE_LEVEL_ERROR,
1279 "AFSNonCachedWrite (%p) Failed to retrieve mapped extents Status %08lX\n",
1283 try_return( ntStatus );
1286 AFSDbgTrace(( AFS_SUBSYSTEM_IO_PROCESSING,
1287 AFS_TRACE_LEVEL_VERBOSE,
1288 "AFSNonCachedWrite (%p) Successfully retrieved map extents count %d run count %d\n",
1293 if( BooleanFlagOn( AFSLibControlFlags, AFS_REDIR_LIB_FLAGS_NONPERSISTENT_CACHE))
1296 Irp->IoStatus.Information = ByteCount;
1300 // Setup the MD5 for each extent
1303 AFSSetupMD5Hash( pFcb,
1311 ntStatus = AFSProcessExtentRun( pSystemBuffer,
1317 if (!NT_SUCCESS(ntStatus))
1320 AFSDbgTrace(( AFS_SUBSYSTEM_IO_PROCESSING,
1321 AFS_TRACE_LEVEL_ERROR,
1322 "AFSNonCachedWrite (%p) Failed to process extent run for non-persistent cache Status %08lX\n",
1327 try_return( ntStatus);
1331 // Retrieve the cache file object
1334 pCacheFileObject = AFSReferenceCacheFileObject();
1336 if( pCacheFileObject == NULL)
1339 ntStatus = STATUS_DEVICE_NOT_READY;
1341 AFSDbgTrace(( AFS_SUBSYSTEM_EXTENT_PROCESSING,
1342 AFS_TRACE_LEVEL_ERROR,
1343 "AFSNonCachedWrite Failed to retrieve cache fileobject for fid %08lX-%08lX-%08lX-%08lX Offset %0I64X Length %08lX Status %08lX\n",
1344 pFcb->ObjectInformation->FileId.Cell,
1345 pFcb->ObjectInformation->FileId.Volume,
1346 pFcb->ObjectInformation->FileId.Vnode,
1347 pFcb->ObjectInformation->FileId.Unique,
1348 StartingByte.QuadPart,
1352 try_return( ntStatus);
1355 if (runCount > AFS_MAX_STACK_IO_RUNS)
1358 pIoRuns = (AFSIoRun*) AFSExAllocatePoolWithTag( PagedPool,
1359 runCount * sizeof( AFSIoRun ),
1361 if (NULL == pIoRuns)
1364 AFSDbgTrace(( AFS_SUBSYSTEM_IO_PROCESSING,
1365 AFS_TRACE_LEVEL_ERROR,
1366 "AFSNonCachedWrite (%p) Failed to allocate IO run block\n",
1369 try_return( ntStatus = STATUS_INSUFFICIENT_RESOURCES );
1378 RtlZeroMemory( pIoRuns, runCount * sizeof( AFSIoRun ));
1380 ntStatus = AFSSetupIoRun( IoGetRelatedDeviceObject( pCacheFileObject),
1389 if (!NT_SUCCESS(ntStatus))
1392 AFSDbgTrace(( AFS_SUBSYSTEM_IO_PROCESSING,
1393 AFS_TRACE_LEVEL_ERROR,
1394 "AFSNonCachedWrite (%p) Failed to initialize IO run block Status %08lX\n",
1398 try_return( ntStatus );
1401 AFSReferenceActiveExtents( pStartExtent,
1404 bDerefExtents = TRUE;
1406 AFSDbgTrace(( AFS_SUBSYSTEM_LOCK_PROCESSING,
1407 AFS_TRACE_LEVEL_VERBOSE,
1408 "AFSNonCachedWrite Releasing(2) Fcb extents lock %p SHARED %08lX\n",
1409 &pFcb->NPFcb->Specific.File.ExtentsResource,
1410 PsGetCurrentThread()));
1412 AFSReleaseResource( &pFcb->NPFcb->Specific.File.ExtentsResource );
1415 pGatherIo = (AFSGatherIo*) AFSExAllocatePoolWithTag( NonPagedPool,
1416 sizeof( AFSGatherIo),
1419 if (NULL == pGatherIo)
1422 AFSDbgTrace(( AFS_SUBSYSTEM_IO_PROCESSING,
1423 AFS_TRACE_LEVEL_ERROR,
1424 "AFSNonCachedWrite (%p) Failed to allocate IO gather block\n",
1427 AFSDbgTrace(( AFS_SUBSYSTEM_LOCK_PROCESSING,
1428 AFS_TRACE_LEVEL_VERBOSE,
1429 "AFSNonCachedWrite Acquiring(1) Fcb extents lock %p SHARED %08lX\n",
1430 &pFcb->NPFcb->Specific.File.ExtentsResource,
1431 PsGetCurrentThread()));
1433 AFSAcquireShared( &pFcb->NPFcb->Specific.File.ExtentsResource,
1437 AFSDereferenceActiveExtents( pStartExtent,
1440 try_return (ntStatus = STATUS_INSUFFICIENT_RESOURCES );
1443 RtlZeroMemory( pGatherIo, sizeof( AFSGatherIo ));
1446 // Initialize count to 1, that was we won't get an early
1447 // completion if the first irp completes before the second is
1450 pGatherIo->Count = 1;
1451 pGatherIo->Status = STATUS_SUCCESS;
1452 pGatherIo->MasterIrp = Irp;
1453 pGatherIo->Synchronous = TRUE;
1454 pGatherIo->CompleteMasterIrp = FALSE;
1456 bCompleteIrp = TRUE;
1458 if( pGatherIo->Synchronous)
1460 KeInitializeEvent( &pGatherIo->Event, NotificationEvent, FALSE );
1465 // Setup the MD5 for each extent
1468 AFSSetupMD5Hash( pFcb,
1477 // Pre-emptively set up the count
1480 Irp->IoStatus.Information = ByteCount;
1482 ntStatus = AFSQueueStartIos( pCacheFileObject,
1484 IRP_WRITE_OPERATION | IRP_SYNCHRONOUS_API,
1489 AFSDbgTrace(( AFS_SUBSYSTEM_IO_PROCESSING,
1490 AFS_TRACE_LEVEL_VERBOSE,
1491 "AFSNonCachedWrite (%p) AFSStartIos completed Status %08lX\n",
1495 if( !NT_SUCCESS( ntStatus))
1498 AFSDbgTrace(( AFS_SUBSYSTEM_LOCK_PROCESSING,
1499 AFS_TRACE_LEVEL_VERBOSE,
1500 "AFSNonCachedWrite Acquiring(2) Fcb extents lock %p SHARED %08lX\n",
1501 &pFcb->NPFcb->Specific.File.ExtentsResource,
1502 PsGetCurrentThread()));
1504 AFSAcquireShared( &pFcb->NPFcb->Specific.File.ExtentsResource,
1508 AFSDereferenceActiveExtents( pStartExtent,
1511 try_return( ntStatus);
1515 // Wait for completion of All IOs we started.
1518 ntStatus = KeWaitForSingleObject( &pGatherIo->Event,
1524 if( NT_SUCCESS( ntStatus))
1527 ntStatus = pGatherIo->Status;
1530 if( !NT_SUCCESS( ntStatus))
1533 AFSDbgTrace(( AFS_SUBSYSTEM_LOCK_PROCESSING,
1534 AFS_TRACE_LEVEL_VERBOSE,
1535 "AFSNonCachedWrite Acquiring(3) Fcb extents lock %p SHARED %08lX\n",
1536 &pFcb->NPFcb->Specific.File.ExtentsResource,
1537 PsGetCurrentThread()));
1539 AFSAcquireShared( &pFcb->NPFcb->Specific.File.ExtentsResource,
1543 AFSDereferenceActiveExtents( pStartExtent,
1546 try_return( ntStatus);
1551 if( NT_SUCCESS( ntStatus) &&
1552 pStartExtent != NULL &&
1553 Irp->IoStatus.Information > 0)
1559 AFSAcquireShared( &pFcb->NPFcb->Specific.File.ExtentsResource,
1565 // Since this is dirty we can mark the extents dirty now.
1566 // AFSMarkDirty will dereference the extents. Do not call
1567 // AFSDereferenceActiveExtents() in this code path.
1579 // This was an uncached user write - tell the server to do
1580 // the flush when the worker thread next wakes up
1582 pFcb->Specific.File.LastServerFlush.QuadPart = 0;
1586 if( pCacheFileObject != NULL)
1588 AFSReleaseCacheFileObject( pCacheFileObject);
1591 AFSDbgTrace(( AFS_SUBSYSTEM_IO_PROCESSING,
1592 AFS_TRACE_LEVEL_VERBOSE,
1593 "AFSNonCachedWrite (FO: %p) StartingByte %08lX:%08lX Length %08lX Status %08lX\n",
1595 StartingByte.HighPart,
1596 StartingByte.LowPart,
1600 if (NT_SUCCESS(ntStatus) &&
1605 pFileObject->CurrentByteOffset.QuadPart = StartingByte.QuadPart + ByteCount;
1611 AFSDbgTrace(( AFS_SUBSYSTEM_LOCK_PROCESSING,
1612 AFS_TRACE_LEVEL_VERBOSE,
1613 "AFSNonCachedWrite Releasing Fcb extents lock %p SHARED %08lX\n",
1614 &pFcb->NPFcb->Specific.File.ExtentsResource,
1615 PsGetCurrentThread()));
1617 AFSReleaseResource( &pFcb->NPFcb->Specific.File.ExtentsResource );
1622 AFSExFreePoolWithTag(pGatherIo, AFS_GATHER_TAG);
1625 if( NULL != pIoRuns &&
1626 stIoRuns != pIoRuns)
1628 AFSExFreePoolWithTag(pIoRuns, AFS_IO_RUN_TAG);
1634 AFSDbgTrace(( AFS_SUBSYSTEM_IO_PROCESSING,
1635 AFS_TRACE_LEVEL_VERBOSE,
1636 "AFSNonCachedWrite Completing Irp %p Status %08lX Info %08lX\n",
1639 Irp->IoStatus.Information));
1641 AFSCompleteRequest( Irp, ntStatus);
1650 AFSNonCachedWriteDirect( IN PDEVICE_OBJECT DeviceObject,
1652 IN LARGE_INTEGER StartingByte,
1655 NTSTATUS ntStatus = STATUS_UNSUCCESSFUL;
1656 VOID *pSystemBuffer = NULL;
1657 BOOLEAN bPagingIo = BooleanFlagOn( Irp->Flags, IRP_PAGING_IO);
1658 IO_STACK_LOCATION *pIrpSp = IoGetCurrentIrpStackLocation( Irp);
1659 PFILE_OBJECT pFileObject = pIrpSp->FileObject;
1660 AFSFcb *pFcb = (AFSFcb *)pFileObject->FsContext;
1661 AFSCcb *pCcb = (AFSCcb *)pFileObject->FsContext2;
1662 BOOLEAN bSynchronousFo = BooleanFlagOn( pFileObject->Flags, FO_SYNCHRONOUS_IO);
1663 BOOLEAN bNoIntermediateBuffering = BooleanFlagOn( pFileObject->Flags, FO_NO_INTERMEDIATE_BUFFERING);
1664 AFSDeviceExt *pDevExt = (AFSDeviceExt *)DeviceObject->DeviceExtension;
1665 AFSFileIOCB stFileIORequest;
1666 AFSFileIOResultCB stFileIOResult;
1667 ULONG ulResultLen = 0;
1672 Irp->IoStatus.Information = 0;
1674 AFSDbgTrace(( AFS_SUBSYSTEM_IO_PROCESSING,
1675 AFS_TRACE_LEVEL_VERBOSE,
1676 "AFSNonCachedWriteDirect (FO: %p) StartingByte %08lX:%08lX Length %08lX\n",
1678 StartingByte.HighPart,
1679 StartingByte.LowPart,
1682 if (ByteCount > pDevExt->Specific.RDR.MaxIo.QuadPart)
1685 AFSDbgTrace(( AFS_SUBSYSTEM_IO_PROCESSING,
1686 AFS_TRACE_LEVEL_ERROR,
1687 "AFSNonCachedWriteDirect (%p) Request %08lX Actual %08lX larger than MaxIO %I64X\n",
1690 pIrpSp->Parameters.Write.Length,
1691 pDevExt->Specific.RDR.MaxIo.QuadPart));
1693 try_return( ntStatus = STATUS_UNSUCCESSFUL);
1697 // Get the mapping for the buffer
1699 pSystemBuffer = AFSLockSystemBuffer( Irp,
1702 if( pSystemBuffer == NULL)
1705 AFSDbgTrace(( AFS_SUBSYSTEM_IO_PROCESSING,
1706 AFS_TRACE_LEVEL_ERROR,
1707 "AFSNonCachedWriteDirect (%p) Failed to map system buffer\n",
1710 try_return( ntStatus = STATUS_INSUFFICIENT_RESOURCES);
1714 // Issue the request at the service for processing
1717 ulResultLen = sizeof( AFSFileIOResultCB);
1719 RtlZeroMemory( &stFileIORequest,
1720 sizeof( AFSFileIOCB));
1722 RtlZeroMemory( &stFileIOResult,
1723 sizeof( AFSFileIOResultCB));
1725 stFileIORequest.SystemIOBuffer = pSystemBuffer;
1727 stFileIORequest.SystemIOBufferMdl = Irp->MdlAddress;
1729 stFileIORequest.IOLength = ByteCount;
1731 stFileIORequest.IOOffset = StartingByte;
1733 ulFlags = AFS_REQUEST_FLAG_SYNCHRONOUS;
1735 if ( bNoIntermediateBuffering)
1738 ulFlags |= AFS_REQUEST_FLAG_CACHE_BYPASS;
1742 // Update file metadata
1745 stFileIORequest.EndOfFile = pFcb->ObjectInformation->EndOfFile;
1747 stFileIORequest.CreateTime = pFcb->ObjectInformation->CreationTime;
1749 stFileIORequest.ChangeTime = pFcb->ObjectInformation->ChangeTime;
1751 stFileIORequest.LastAccessTime = pFcb->ObjectInformation->LastAccessTime;
1753 stFileIORequest.LastWriteTime = pFcb->ObjectInformation->LastWriteTime;
1756 // Write the data to the service
1759 ntStatus = AFSProcessRequest( AFS_REQUEST_TYPE_PROCESS_WRITE_FILE,
1762 &pCcb->DirectoryCB->NameInformation.FileName,
1763 &pFcb->ObjectInformation->FileId,
1764 pFcb->ObjectInformation->VolumeCB->VolumeInformation.Cell,
1765 pFcb->ObjectInformation->VolumeCB->VolumeInformation.CellLength,
1767 sizeof( AFSFileIOCB),
1771 if( NT_SUCCESS( ntStatus))
1774 Irp->IoStatus.Information = (ULONG_PTR)stFileIOResult.Length;
1779 AFSDbgTrace(( AFS_SUBSYSTEM_IO_PROCESSING,
1780 AFS_TRACE_LEVEL_ERROR,
1781 "AFSNonCachedWriteDirect (%p) Failed to send write to service Status %08lX\n",
1788 AFSDbgTrace(( AFS_SUBSYSTEM_IO_PROCESSING,
1789 AFS_TRACE_LEVEL_VERBOSE,
1790 "AFSNonCachedWriteDirect (FO: %p) StartingByte %08lX:%08lX Length %08lX Status %08lX\n",
1792 StartingByte.HighPart,
1793 StartingByte.LowPart,
1797 if (NT_SUCCESS(ntStatus) &&
1802 pFileObject->CurrentByteOffset.QuadPart = StartingByte.QuadPart + ByteCount;
1805 AFSDbgTrace(( AFS_SUBSYSTEM_IO_PROCESSING,
1806 AFS_TRACE_LEVEL_VERBOSE,
1807 "AFSNonCachedWriteDirect Completing Irp %p Status %08lX Info %08lX\n",
1810 Irp->IoStatus.Information));
1812 AFSCompleteRequest( Irp, ntStatus);
1820 AFSCachedWrite( IN PDEVICE_OBJECT DeviceObject,
1822 IN LARGE_INTEGER StartingByte,
1824 IN BOOLEAN ForceFlush)
1826 UNREFERENCED_PARAMETER(DeviceObject);
1827 PVOID pSystemBuffer = NULL;
1828 NTSTATUS ntStatus = STATUS_SUCCESS;
1829 IO_STATUS_BLOCK iosbFlush;
1830 IO_STACK_LOCATION *pIrpSp = IoGetCurrentIrpStackLocation( Irp);
1831 PFILE_OBJECT pFileObject = pIrpSp->FileObject;
1832 AFSFcb *pFcb = (AFSFcb *)pFileObject->FsContext;
1833 BOOLEAN bSynchronousFo = BooleanFlagOn( pFileObject->Flags, FO_SYNCHRONOUS_IO);
1834 ULONG ulCurrentIO = 0, ulTotalLen = ByteCount;
1835 PMDL pCurrentMdl = Irp->MdlAddress;
1836 LARGE_INTEGER liCurrentOffset;
1841 Irp->IoStatus.Information = 0;
1843 if( BooleanFlagOn( pIrpSp->MinorFunction, IRP_MN_MDL))
1849 CcPrepareMdlWrite( pFileObject,
1855 ntStatus = Irp->IoStatus.Status;
1857 __except( EXCEPTION_EXECUTE_HANDLER)
1859 ntStatus = GetExceptionCode();
1861 AFSDbgTrace(( AFS_SUBSYSTEM_IO_PROCESSING,
1862 AFS_TRACE_LEVEL_ERROR,
1863 "AFSCachedWrite (%p) Exception thrown while preparing mdl write Status %08lX\n",
1868 if( !NT_SUCCESS( ntStatus))
1872 // Free up any potentially allocated mdl's
1875 CcMdlWriteComplete( pFileObject,
1879 Irp->MdlAddress = NULL;
1881 AFSDbgTrace(( AFS_SUBSYSTEM_IO_PROCESSING,
1882 AFS_TRACE_LEVEL_ERROR,
1883 "AFSCachedWrite (%p) Failed to process MDL write Status %08lX\n",
1888 try_return( ntStatus);
1891 liCurrentOffset.QuadPart = StartingByte.QuadPart;
1893 while( ulTotalLen > 0)
1896 ntStatus = STATUS_SUCCESS;
1898 if( pCurrentMdl != NULL)
1901 pSystemBuffer = MmGetSystemAddressForMdlSafe( pCurrentMdl,
1902 NormalPagePriority);
1904 ulCurrentIO = MmGetMdlByteCount( pCurrentMdl);
1906 if( ulCurrentIO > ulTotalLen)
1908 ulCurrentIO = ulTotalLen;
1914 pSystemBuffer = AFSLockSystemBuffer( Irp,
1917 ulCurrentIO = ulTotalLen;
1920 if( pSystemBuffer == NULL)
1923 AFSDbgTrace(( AFS_SUBSYSTEM_IO_PROCESSING,
1924 AFS_TRACE_LEVEL_ERROR,
1925 "AFSCachedWrite (%p) Failed to lock system buffer\n",
1928 try_return( ntStatus = STATUS_INSUFFICIENT_RESOURCES);
1934 if( !CcCopyWrite( pFileObject,
1941 // Failed to process request.
1944 AFSDbgTrace(( AFS_SUBSYSTEM_FILE_PROCESSING,
1945 AFS_TRACE_LEVEL_ERROR,
1946 "AFSCachedWrite (%p) Failed to issue CcCopyWrite %wZ @ %0I64X Status %08lX\n",
1948 &pFileObject->FileName,
1949 liCurrentOffset.QuadPart,
1950 Irp->IoStatus.Status));
1952 try_return( ntStatus = STATUS_UNSUCCESSFUL);
1955 __except( EXCEPTION_EXECUTE_HANDLER)
1958 ntStatus = GetExceptionCode();
1960 AFSDbgTrace(( AFS_SUBSYSTEM_IO_PROCESSING,
1961 AFS_TRACE_LEVEL_ERROR,
1962 "AFSCachedWrite (%p) CcCopyWrite Threw exception %wZ @ %0I64X Status %08lX\n",
1964 &pFileObject->FileName,
1965 liCurrentOffset.QuadPart,
1969 if( !NT_SUCCESS( ntStatus))
1971 try_return( ntStatus);
1975 BooleanFlagOn(pFileObject->Flags, (FO_NO_INTERMEDIATE_BUFFERING + FO_WRITE_THROUGH)))
1981 // We have detected a file we do a write through with.
1984 CcFlushCache(&pFcb->NPFcb->SectionObjectPointers,
1989 if( !NT_SUCCESS( iosbFlush.Status))
1992 AFSDbgTrace(( AFS_SUBSYSTEM_IO_PROCESSING,
1993 AFS_TRACE_LEVEL_ERROR,
1994 "AFSCachedWrite (%p) CcFlushCache failure %wZ FID %08lX-%08lX-%08lX-%08lX Status 0x%08lX Bytes 0x%08lX\n",
1996 &pFileObject->FileName,
1997 pFcb->ObjectInformation->FileId.Cell,
1998 pFcb->ObjectInformation->FileId.Volume,
1999 pFcb->ObjectInformation->FileId.Vnode,
2000 pFcb->ObjectInformation->FileId.Unique,
2002 iosbFlush.Information));
2004 try_return( ntStatus = iosbFlush.Status);
2007 __except( EXCEPTION_EXECUTE_HANDLER)
2010 ntStatus = GetExceptionCode();
2012 AFSDbgTrace(( AFS_SUBSYSTEM_IO_PROCESSING,
2013 AFS_TRACE_LEVEL_ERROR,
2014 "AFSCachedWrite (%p) CcFlushCache Threw exception %wZ @ %0I64X Status %08lX\n",
2016 &pFileObject->FileName,
2017 liCurrentOffset.QuadPart,
2020 try_return( ntStatus);
2024 if( ulTotalLen <= ulCurrentIO)
2029 liCurrentOffset.QuadPart += ulCurrentIO;
2031 ulTotalLen -= ulCurrentIO;
2033 pCurrentMdl = pCurrentMdl->Next;
2038 if( NT_SUCCESS( ntStatus))
2041 Irp->IoStatus.Information = ByteCount;
2044 BooleanFlagOn(pFileObject->Flags, (FO_NO_INTERMEDIATE_BUFFERING + FO_WRITE_THROUGH)))
2047 // Write through asked for... Set things so that we get
2048 // flush when the worker thread next wakes up
2050 pFcb->Specific.File.LastServerFlush.QuadPart = 0;
2054 AFSCompleteRequest( Irp,
2062 // Called with Fcb->NPFcb->SectionObjectResource and Fcb->NPFcb->Resource held
2067 AFSExtendingWrite( IN AFSFcb *Fcb,
2068 IN PFILE_OBJECT FileObject,
2069 IN LONGLONG NewLength)
2071 LARGE_INTEGER liSaveFileSize = Fcb->Header.FileSize;
2072 LARGE_INTEGER liSaveAllocation = Fcb->Header.AllocationSize;
2073 NTSTATUS ntStatus = STATUS_SUCCESS;
2074 AFSCcb *pCcb = (AFSCcb *)FileObject->FsContext2;
2076 if( NewLength > Fcb->Header.AllocationSize.QuadPart)
2079 Fcb->Header.AllocationSize.QuadPart = NewLength;
2081 Fcb->ObjectInformation->AllocationSize = Fcb->Header.AllocationSize;
2084 if( NewLength > Fcb->Header.FileSize.QuadPart)
2087 Fcb->Header.FileSize.QuadPart = NewLength;
2089 Fcb->ObjectInformation->EndOfFile = Fcb->Header.FileSize;
2096 ntStatus = AFSUpdateFileInformation( &Fcb->ObjectInformation->ParentFileId,
2097 Fcb->ObjectInformation,
2100 if (NT_SUCCESS(ntStatus))
2103 KeQuerySystemTime( &Fcb->ObjectInformation->ChangeTime);
2105 SetFlag( Fcb->Flags, AFS_FCB_FLAG_FILE_MODIFIED | AFS_FCB_FLAG_UPDATE_CHANGE_TIME);
2108 // If the file is currently cached, then let the MM know about the extension
2110 // The CcSetFileSizes call should be made with only the PagingResource held
2111 // which we are currently not holding.
2114 if( CcIsFileCached( FileObject))
2116 CcSetFileSizes( FileObject,
2117 (PCC_FILE_SIZES)&Fcb->Header.AllocationSize);
2122 Fcb->Header.FileSize = liSaveFileSize;
2123 Fcb->Header.AllocationSize = liSaveAllocation;
2127 // DownConvert file resource to shared
2129 ExConvertExclusiveToSharedLite( &Fcb->NPFcb->Resource);
2135 AFSShareWrite( IN PDEVICE_OBJECT DeviceObject,
2139 UNREFERENCED_PARAMETER(DeviceObject);
2140 NTSTATUS ntStatus = STATUS_SUCCESS;
2141 PIO_STACK_LOCATION pIrpSp = IoGetCurrentIrpStackLocation( Irp);
2142 AFSFcb *pFcb = NULL;
2143 AFSCcb *pCcb = NULL;
2144 AFSPipeIORequestCB *pIoRequest = NULL;
2145 void *pBuffer = NULL;
2146 AFSPipeIOResultCB stIoResult;
2147 ULONG ulBytesReturned = 0;
2152 pCcb = (AFSCcb *)pIrpSp->FileObject->FsContext2;
2154 AFSDbgTrace(( AFS_SUBSYSTEM_PIPE_PROCESSING,
2155 AFS_TRACE_LEVEL_VERBOSE,
2156 "AFSShareWrite On pipe %wZ Length %08lX\n",
2157 &pCcb->DirectoryCB->NameInformation.FileName,
2158 pIrpSp->Parameters.Write.Length));
2160 if( pIrpSp->Parameters.Write.Length == 0)
2164 // Nothing to do in this case
2167 try_return( ntStatus);
2171 // Retrieve the buffer for the read request
2174 pBuffer = AFSLockSystemBuffer( Irp,
2175 pIrpSp->Parameters.Write.Length);
2177 if( pBuffer == NULL)
2180 AFSDbgTrace(( AFS_SUBSYSTEM_PIPE_PROCESSING,
2181 AFS_TRACE_LEVEL_ERROR,
2182 "AFSShareWrite Failed to map buffer on pipe %wZ\n",
2183 &pCcb->DirectoryCB->NameInformation.FileName));
2185 try_return( ntStatus = STATUS_INSUFFICIENT_RESOURCES);
2188 pFcb = (AFSFcb *)pIrpSp->FileObject->FsContext;
2190 AFSAcquireShared( &pFcb->NPFcb->Resource,
2193 pIoRequest = (AFSPipeIORequestCB *)AFSExAllocatePoolWithTag( PagedPool,
2194 sizeof( AFSPipeIORequestCB) +
2195 pIrpSp->Parameters.Write.Length,
2196 AFS_GENERIC_MEMORY_14_TAG);
2198 if( pIoRequest == NULL)
2201 try_return( ntStatus = STATUS_INSUFFICIENT_RESOURCES);
2204 RtlZeroMemory( pIoRequest,
2205 sizeof( AFSPipeIORequestCB) + pIrpSp->Parameters.Write.Length);
2207 pIoRequest->RequestId = pCcb->RequestID;
2209 pIoRequest->RootId = pFcb->ObjectInformation->VolumeCB->ObjectInformation.FileId;
2211 pIoRequest->BufferLength = pIrpSp->Parameters.Write.Length;
2213 RtlCopyMemory( (void *)((char *)pIoRequest + sizeof( AFSPipeIORequestCB)),
2215 pIrpSp->Parameters.Write.Length);
2217 stIoResult.BytesProcessed = 0;
2219 ulBytesReturned = sizeof( AFSPipeIOResultCB);
2222 // Issue the open request to the service
2225 ntStatus = AFSProcessRequest( AFS_REQUEST_TYPE_PIPE_WRITE,
2226 AFS_REQUEST_FLAG_SYNCHRONOUS,
2228 &pCcb->DirectoryCB->NameInformation.FileName,
2233 sizeof( AFSPipeIORequestCB) +
2234 pIrpSp->Parameters.Write.Length,
2238 if( !NT_SUCCESS( ntStatus))
2241 AFSDbgTrace(( AFS_SUBSYSTEM_FILE_PROCESSING,
2242 AFS_TRACE_LEVEL_ERROR,
2243 "AFSShareWrite (%p) Failed service write Status %08lX\n",
2247 try_return( ntStatus);
2250 AFSDbgTrace(( AFS_SUBSYSTEM_PIPE_PROCESSING,
2251 AFS_TRACE_LEVEL_VERBOSE,
2252 "AFSShareWrite Completed on pipe %wZ Length read %08lX\n",
2253 &pCcb->DirectoryCB->NameInformation.FileName,
2254 stIoResult.BytesProcessed));
2256 Irp->IoStatus.Information = stIoResult.BytesProcessed;
2263 AFSReleaseResource( &pFcb->NPFcb->Resource);
2266 if( pIoRequest != NULL)
2269 AFSExFreePoolWithTag( pIoRequest, AFS_GENERIC_MEMORY_14_TAG);