2 * Copyright (c) 2008, 2009, 2010, 2011 Kernel Drivers, LLC.
3 * Copyright (c) 2009, 2010, 2011 Your File System, Inc.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
10 * - Redistributions of source code must retain the above copyright notice,
11 * this list of conditions and the following disclaimer.
12 * - Redistributions in binary form must reproduce the above copyright
14 * this list of conditions and the following disclaimer in the
16 * and/or other materials provided with the distribution.
17 * - Neither the names of Kernel Drivers, LLC and Your File System, Inc.
18 * nor the names of their contributors may be used to endorse or promote
19 * products derived from this software without specific prior written
20 * permission from Kernel Drivers, LLC and Your File System, Inc.
22 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
23 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
24 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
25 * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
26 * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
27 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
28 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
29 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
30 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
31 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
32 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
39 #include "AFSCommon.h"
43 AFSCachedWrite( IN PDEVICE_OBJECT DeviceObject,
45 IN LARGE_INTEGER StartingByte,
47 IN BOOLEAN ForceFlush);
50 AFSNonCachedWrite( IN PDEVICE_OBJECT DeviceObject,
52 IN LARGE_INTEGER StartingByte,
57 AFSExtendingWrite( IN AFSFcb *Fcb,
58 IN PFILE_OBJECT FileObject,
59 IN LONGLONG NewLength);
66 // This is the dispatch handler for the IRP_MJ_WRITE request
70 // A status is returned for the function
73 AFSWrite( IN PDEVICE_OBJECT LibDeviceObject,
77 UNREFERENCED_PARAMETER(LibDeviceObject);
78 NTSTATUS ntStatus = STATUS_SUCCESS;
83 ntStatus = AFSCommonWrite( AFSRDRDeviceObject, Irp, NULL);
85 __except( AFSExceptionFilter( __FUNCTION__, GetExceptionCode(), GetExceptionInformation()) )
88 ntStatus = STATUS_INSUFFICIENT_RESOURCES;
95 AFSCommonWrite( IN PDEVICE_OBJECT DeviceObject,
100 NTSTATUS ntStatus = STATUS_SUCCESS;
101 AFSDeviceExt *pDeviceExt = (AFSDeviceExt *)DeviceObject->DeviceExtension;
102 IO_STACK_LOCATION *pIrpSp;
105 AFSNonPagedFcb *pNPFcb = NULL;
106 ULONG ulByteCount = 0;
107 LARGE_INTEGER liStartingByte;
108 PFILE_OBJECT pFileObject;
109 BOOLEAN bPagingIo = FALSE;
110 BOOLEAN bNonCachedIo = FALSE;
111 BOOLEAN bReleaseMain = FALSE;
112 BOOLEAN bReleaseSectionObject = FALSE;
113 BOOLEAN bReleasePaging = FALSE;
114 BOOLEAN bExtendingWrite = FALSE;
115 BOOLEAN bCompleteIrp = TRUE;
117 HANDLE hCallingUser = OnBehalfOf;
118 BOOLEAN bRetry = FALSE;
119 ULONGLONG ullProcessId = (ULONGLONG)PsGetCurrentProcessId();
121 pIrpSp = IoGetCurrentIrpStackLocation( Irp);
126 pFileObject = pIrpSp->FileObject;
129 // Extract the fileobject references
132 pFcb = (AFSFcb *)pFileObject->FsContext;
133 pCcb = (AFSCcb *)pFileObject->FsContext2;
135 ObReferenceObject( pFileObject);
140 AFSDbgLogMsg( AFS_SUBSYSTEM_IO_PROCESSING,
141 AFS_TRACE_LEVEL_ERROR,
142 "AFSCommonWrite Attempted write (%p) when pFcb == NULL\n",
145 try_return( ntStatus = STATUS_INVALID_DEVICE_REQUEST);
148 pNPFcb = pFcb->NPFcb;
151 // If we are in shutdown mode then fail the request
154 if( BooleanFlagOn( pDeviceExt->DeviceFlags, AFS_DEVICE_FLAG_REDIRECTOR_SHUTDOWN))
157 AFSDbgLogMsg( AFS_SUBSYSTEM_FILE_PROCESSING,
158 AFS_TRACE_LEVEL_WARNING,
159 "AFSCommonWrite (%p) Open request after shutdown\n",
162 try_return( ntStatus = STATUS_TOO_LATE);
165 liStartingByte = pIrpSp->Parameters.Write.ByteOffset;
166 bPagingIo = BooleanFlagOn( Irp->Flags, IRP_PAGING_IO);
167 bNonCachedIo = BooleanFlagOn( Irp->Flags, IRP_NOCACHE);
168 ulByteCount = pIrpSp->Parameters.Write.Length;
170 if( pFcb->Header.NodeTypeCode != AFS_IOCTL_FCB &&
171 pFcb->Header.NodeTypeCode != AFS_FILE_FCB &&
172 pFcb->Header.NodeTypeCode != AFS_SPECIAL_SHARE_FCB)
175 AFSDbgLogMsg( AFS_SUBSYSTEM_IO_PROCESSING,
176 AFS_TRACE_LEVEL_ERROR,
177 "AFSCommonWrite Attempted write (%p) on an invalid node type %08lX\n",
179 pFcb->Header.NodeTypeCode);
181 try_return( ntStatus = STATUS_INVALID_DEVICE_REQUEST);
185 // If this is a write against an IOCtl node then handle it
186 // in a different pathway
189 if( pFcb->Header.NodeTypeCode == AFS_IOCTL_FCB)
192 AFSDbgLogMsg( AFS_SUBSYSTEM_IO_PROCESSING,
193 AFS_TRACE_LEVEL_VERBOSE,
194 "AFSCommonWrite (%p) Processing file (PIOCTL) Offset %I64X Length %08lX Irp Flags %08lX\n",
196 liStartingByte.QuadPart,
200 ntStatus = AFSIOCtlWrite( DeviceObject,
203 try_return( ntStatus);
205 else if( pFcb->Header.NodeTypeCode == AFS_SPECIAL_SHARE_FCB)
208 AFSDbgLogMsg( AFS_SUBSYSTEM_IO_PROCESSING,
209 AFS_TRACE_LEVEL_VERBOSE,
210 "AFSCommonWrite (%p) Processing file (SHARE) Offset %I64X Length %08lX Irp Flags %08lX\n",
212 liStartingByte.QuadPart,
216 ntStatus = AFSShareWrite( DeviceObject,
219 try_return( ntStatus);
223 // Is the Cache not there yet? Exit.
225 if( !BooleanFlagOn( AFSLibControlFlags, AFS_REDIR_LIB_FLAGS_NONPERSISTENT_CACHE) &&
226 NULL == pDeviceExt->Specific.RDR.CacheFileObject)
229 AFSDbgLogMsg( AFS_SUBSYSTEM_IO_PROCESSING,
230 AFS_TRACE_LEVEL_ERROR,
231 "AFSCommonWrite (%p) Request failed due to AFS cache closed\n",
234 try_return( ntStatus = STATUS_TOO_LATE );
237 if( pFcb->ObjectInformation->VolumeCB != NULL &&
238 BooleanFlagOn( pFcb->ObjectInformation->VolumeCB->VolumeInformation.Characteristics, FILE_READ_ONLY_DEVICE))
241 AFSDbgLogMsg( AFS_SUBSYSTEM_IO_PROCESSING,
242 AFS_TRACE_LEVEL_ERROR,
243 "AFSCommonWrite (%p) Request failed due to read only volume\n",
246 try_return( ntStatus = STATUS_ACCESS_DENIED);
250 // We need to know on whose behalf we have been called (which
251 // we will eventually tell to the server - for non paging
252 // writes). If we were posted then we were told. If this is
253 // the first time we saw the irp then we grab it now.
255 if( NULL == OnBehalfOf )
258 hCallingUser = PsGetCurrentProcessId();
263 hCallingUser = OnBehalfOf;
267 // Check for zero length write
270 if( ulByteCount == 0)
273 AFSDbgLogMsg( AFS_SUBSYSTEM_IO_PROCESSING,
274 AFS_TRACE_LEVEL_VERBOSE,
275 "AFSCommonWrite (%p) Request completed due to zero length\n",
278 try_return( ntStatus);
282 // Is this Fcb valid???
285 if( BooleanFlagOn( pFcb->ObjectInformation->Flags, AFS_OBJECT_FLAGS_OBJECT_INVALID))
288 AFSDbgLogMsg( AFS_SUBSYSTEM_IO_PROCESSING,
289 AFS_TRACE_LEVEL_ERROR,
290 "AFSCommonWrite (%p) Failing request due to INVALID fcb\n",
293 Irp->IoStatus.Information = 0;
295 try_return( ntStatus = STATUS_FILE_DELETED);
298 if( BooleanFlagOn( pCcb->DirectoryCB->Flags, AFS_DIR_ENTRY_DELETED) ||
299 BooleanFlagOn( pFcb->ObjectInformation->Flags, AFS_OBJECT_FLAGS_DELETED))
302 AFSDbgLogMsg( AFS_SUBSYSTEM_IO_PROCESSING,
303 AFS_TRACE_LEVEL_ERROR,
304 "AFSCommonWrite (%p) Request failed due to file deleted\n",
307 try_return( ntStatus = STATUS_FILE_DELETED);
310 if( FlagOn( pIrpSp->MinorFunction, IRP_MN_COMPLETE))
313 AFSDbgLogMsg( AFS_SUBSYSTEM_IO_PROCESSING,
314 AFS_TRACE_LEVEL_VERBOSE,
315 "AFSCommonWrite (%p) IRP_MN_COMPLETE being processed\n",
318 CcMdlWriteComplete(pFileObject, &pIrpSp->Parameters.Write.ByteOffset, Irp->MdlAddress);
321 // Mdl is now Deallocated
324 Irp->MdlAddress = NULL;
326 try_return( ntStatus = STATUS_SUCCESS );
330 // If we get a non cached IO for a cached file we should do a purge.
331 // For now we will just promote to cached
333 if( NULL != pFileObject->SectionObjectPointer->DataSectionObject && !bPagingIo && bNonCachedIo)
335 bNonCachedIo = FALSE;
338 if ( !bNonCachedIo && !bPagingIo)
341 if( pFileObject->PrivateCacheMap == NULL)
344 AFSDbgLogMsg( AFS_SUBSYSTEM_LOCK_PROCESSING,
345 AFS_TRACE_LEVEL_VERBOSE,
346 "AFSCommonWrite Acquiring Fcb SectionObject lock %p EXCL %08lX\n",
347 &pNPFcb->SectionObjectResource,
348 PsGetCurrentThread());
350 AFSAcquireExcl( &pNPFcb->SectionObjectResource,
353 bReleaseSectionObject = TRUE;
358 AFSDbgLogMsg( AFS_SUBSYSTEM_IO_PROCESSING,
359 AFS_TRACE_LEVEL_VERBOSE,
360 "AFSCommonWrite Initialize caching on Fcb %p FileObject %p\n",
364 CcInitializeCacheMap( pFileObject,
365 (PCC_FILE_SIZES)&pFcb->Header.AllocationSize,
367 AFSLibCacheManagerCallbacks,
370 CcSetReadAheadGranularity( pFileObject,
371 pDeviceExt->Specific.RDR.MaximumRPCLength);
373 CcSetDirtyPageThreshold( pFileObject,
374 AFS_DIRTY_CHUNK_THRESHOLD * pDeviceExt->Specific.RDR.MaximumRPCLength / 4096);
376 __except( EXCEPTION_EXECUTE_HANDLER)
379 ntStatus = GetExceptionCode();
381 AFSDbgLogMsg( AFS_SUBSYSTEM_IO_PROCESSING,
382 AFS_TRACE_LEVEL_ERROR,
383 "AFSCommonWrite (%p) Exception thrown while initializing cache map Status %08lX\n",
388 AFSDbgLogMsg( AFS_SUBSYSTEM_LOCK_PROCESSING,
389 AFS_TRACE_LEVEL_VERBOSE,
390 "AFSCommonWrite Releasing Fcb SectionObject lock %p EXCL %08lX\n",
391 &pNPFcb->SectionObjectResource,
392 PsGetCurrentThread());
394 AFSReleaseResource( &pNPFcb->SectionObjectResource);
396 bReleaseSectionObject = FALSE;
398 if( !NT_SUCCESS( ntStatus))
401 try_return( ntStatus);
405 while (!CcCanIWrite( pFileObject,
410 static const LONGLONG llWriteDelay = (LONGLONG)-100000;
413 AFSDbgLogMsg( AFS_SUBSYSTEM_IO_PROCESSING,
414 AFS_TRACE_LEVEL_WARNING,
415 "AFSCommonWrite (FO: %p) CcCanIWrite says No room for %u bytes! Retry in 10ms\n",
419 KeDelayExecutionThread(KernelMode, FALSE, (PLARGE_INTEGER)&llWriteDelay);
424 // Save off the PID if this is not a paging IO
428 ( pFcb->Specific.File.ExtentRequestProcessId == 0 ||
429 ( ullProcessId != (ULONGLONG)AFSSysProcess &&
430 pFcb->Specific.File.ExtentRequestProcessId != ullProcessId)))
433 pFcb->Specific.File.ExtentRequestProcessId = ullProcessId;
435 if( ullProcessId == (ULONGLONG)AFSSysProcess)
437 AFSDbgLogMsg( AFS_SUBSYSTEM_EXTENT_PROCESSING,
438 AFS_TRACE_LEVEL_WARNING,
439 "%s Setting LastWriterExtentProcessId to system process for Fcb %p\n",
448 // - if Paging then we need to do nothing (the precalls will
449 // have acquired the paging resource), for clarity we will collect
450 // the paging resource
451 // - If extending Write then take the fileresource EX (EOF will change, Allocation will only move out)
452 // - Otherwise we collect the file shared, check against extending and
463 //ASSERT( NULL != OnBehalfOf || ExIsResourceAcquiredLite( &pNPFcb->Resource ));
465 AFSDbgLogMsg( AFS_SUBSYSTEM_LOCK_PROCESSING,
466 AFS_TRACE_LEVEL_VERBOSE,
467 "AFSCommonWrite Acquiring Fcb PagingIo lock %p SHARED %08lX\n",
468 &pNPFcb->PagingResource,
469 PsGetCurrentThread());
471 AFSAcquireShared( &pNPFcb->PagingResource,
474 bReleasePaging = TRUE;
477 // We have the correct lock - we cannot have the wrong one
484 bExtendingWrite = (((liStartingByte.QuadPart + ulByteCount) >=
485 pFcb->Header.FileSize.QuadPart) ||
486 (liStartingByte.LowPart == FILE_WRITE_TO_END_OF_FILE &&
487 liStartingByte.HighPart == -1)) ;
492 // Check for lock inversion
495 ASSERT( !ExIsResourceAcquiredLite( &pNPFcb->PagingResource ));
497 AFSDbgLogMsg( AFS_SUBSYSTEM_LOCK_PROCESSING,
498 AFS_TRACE_LEVEL_VERBOSE,
499 "AFSCommonWrite Acquiring Fcb lock %p EXCL %08lX\n",
501 PsGetCurrentThread());
503 AFSAcquireExcl( &pNPFcb->Resource,
508 AFSDbgLogMsg( AFS_SUBSYSTEM_LOCK_PROCESSING,
509 AFS_TRACE_LEVEL_VERBOSE,
510 "AFSCommonWrite Acquiring Fcb SectionObject lock %p EXCL %08lX\n",
511 &pNPFcb->SectionObjectResource,
512 PsGetCurrentThread());
514 AFSAcquireExcl( &pNPFcb->SectionObjectResource,
517 bReleaseSectionObject = TRUE;
519 if (liStartingByte.LowPart == FILE_WRITE_TO_END_OF_FILE &&
520 liStartingByte.HighPart == -1)
522 if (pFcb->Header.ValidDataLength.QuadPart > pFcb->Header.FileSize.QuadPart)
524 liStartingByte = pFcb->Header.ValidDataLength;
528 liStartingByte = pFcb->Header.FileSize;
533 // We have the correct lock - even if we don't end up truncating
539 ASSERT( !ExIsResourceAcquiredLite( &pNPFcb->PagingResource ));
541 AFSDbgLogMsg( AFS_SUBSYSTEM_LOCK_PROCESSING,
542 AFS_TRACE_LEVEL_VERBOSE,
543 "AFSCommonWrite Acquiring Fcb lock %p SHARED %08lX\n",
545 PsGetCurrentThread());
547 AFSAcquireShared( &pNPFcb->Resource,
552 AFSDbgLogMsg( AFS_SUBSYSTEM_LOCK_PROCESSING,
553 AFS_TRACE_LEVEL_VERBOSE,
554 "AFSCommonWrite Acquiring Fcb SectionObject lock %p SHARED %08lX\n",
555 &pNPFcb->SectionObjectResource,
556 PsGetCurrentThread());
558 AFSAcquireShared( &pNPFcb->SectionObjectResource,
561 bReleaseSectionObject = TRUE;
564 // Have things moved? Are we extending? If so, the the lock isn't OK
566 bLockOK = (liStartingByte.QuadPart + ulByteCount) < pFcb->Header.FileSize.QuadPart;
570 AFSReleaseResource( &pNPFcb->Resource);
572 bReleaseMain = FALSE;
583 // Check the BR locks on the file.
586 if ( !FsRtlCheckLockForWriteAccess( &pFcb->Specific.File.FileLock,
590 AFSDbgLogMsg( AFS_SUBSYSTEM_IO_PROCESSING,
591 AFS_TRACE_LEVEL_ERROR,
592 "AFSCommonWrite (%p) Request failed due to lock conflict\n",
595 try_return( ntStatus = STATUS_FILE_LOCK_CONFLICT);
601 ntStatus = AFSExtendingWrite( pFcb, pFileObject, (liStartingByte.QuadPart + ulByteCount));
603 if( !NT_SUCCESS(ntStatus))
606 AFSDbgLogMsg( AFS_SUBSYSTEM_IO_PROCESSING,
607 AFS_TRACE_LEVEL_ERROR,
608 "AFSCommonWrite (%p) Failed extending write request Status %08lX\n",
612 try_return( ntStatus );
618 // Fire off the request as appropriate
620 bCompleteIrp = FALSE;
627 // Main and SectionObject resources held Shared
630 AFSDbgLogMsg( AFS_SUBSYSTEM_IO_PROCESSING,
631 AFS_TRACE_LEVEL_VERBOSE,
632 "AFSCommonWrite (%p) Processing CACHED request Offset %I64X Len %08lX\n",
634 liStartingByte.QuadPart,
637 ntStatus = AFSCachedWrite( DeviceObject, Irp, liStartingByte, ulByteCount, TRUE);
644 // if bPagingIo, Paging Resource held Shared
645 // else Main and SectionObject resources held Shared
648 if( bReleaseSectionObject)
651 AFSReleaseResource( &pNPFcb->SectionObjectResource);
653 bReleaseSectionObject = FALSE;
659 AFSReleaseResource( &pNPFcb->PagingResource);
661 bReleasePaging = FALSE;
667 AFSReleaseResource( &pNPFcb->Resource);
669 bReleaseMain = FALSE;
672 AFSDbgLogMsg( AFS_SUBSYSTEM_IO_PROCESSING,
673 AFS_TRACE_LEVEL_VERBOSE,
674 "AFSCommonWrite (%p) Processing NON-CACHED request Offset %I64X Len %08lX\n",
676 liStartingByte.QuadPart,
679 ntStatus = AFSNonCachedWrite( DeviceObject, Irp, liStartingByte, ulByteCount);
684 AFSDbgLogMsg( AFS_SUBSYSTEM_IO_PROCESSING,
685 AFS_TRACE_LEVEL_VERBOSE,
686 "AFSCommonWrite (%p) Process complete Status %08lX\n",
690 ObDereferenceObject(pFileObject);
692 if( bReleaseSectionObject)
695 AFSReleaseResource( &pNPFcb->SectionObjectResource);
701 AFSReleaseResource( &pNPFcb->PagingResource);
707 AFSReleaseResource( &pNPFcb->Resource);
713 AFSCompleteRequest( Irp,
722 AFSIOCtlWrite( IN PDEVICE_OBJECT DeviceObject,
726 UNREFERENCED_PARAMETER(DeviceObject);
727 NTSTATUS ntStatus = STATUS_SUCCESS;
728 AFSPIOCtlIORequestCB stIORequestCB;
729 PIO_STACK_LOCATION pIrpSp = IoGetCurrentIrpStackLocation( Irp);
732 AFSPIOCtlIOResultCB stIOResultCB;
733 ULONG ulBytesReturned = 0;
734 AFSFileID stParentFID;
739 RtlZeroMemory( &stIORequestCB,
740 sizeof( AFSPIOCtlIORequestCB));
742 if( pIrpSp->Parameters.Write.Length == 0)
746 // Nothing to do in this case
749 try_return( ntStatus);
752 pFcb = (AFSFcb *)pIrpSp->FileObject->FsContext;
754 pCcb = (AFSCcb *)pIrpSp->FileObject->FsContext2;
756 AFSDbgLogMsg( AFS_SUBSYSTEM_LOCK_PROCESSING,
757 AFS_TRACE_LEVEL_VERBOSE,
758 "AFSIOCtlWrite Acquiring Fcb lock %p SHARED %08lX\n",
759 &pFcb->NPFcb->Resource,
760 PsGetCurrentThread());
762 AFSAcquireShared( &pFcb->NPFcb->Resource,
766 // Get the parent fid to pass to the cm
769 RtlZeroMemory( &stParentFID,
772 if( BooleanFlagOn( pFcb->ObjectInformation->Flags, AFS_OBJECT_FLAGS_PARENT_FID))
776 // The parent directory FID of the node
779 stParentFID = pFcb->ObjectInformation->ParentFileId;
783 // Set the control block up
786 stIORequestCB.RequestId = pCcb->RequestID;
788 if( pFcb->ObjectInformation->VolumeCB != NULL)
790 stIORequestCB.RootId = pFcb->ObjectInformation->VolumeCB->ObjectInformation.FileId;
794 // Lock down the buffer
797 stIORequestCB.MappedBuffer = AFSMapToService( Irp,
798 pIrpSp->Parameters.Write.Length);
800 if( stIORequestCB.MappedBuffer == NULL)
803 try_return( ntStatus = STATUS_INSUFFICIENT_RESOURCES);
806 stIORequestCB.BufferLength = pIrpSp->Parameters.Write.Length;
808 stIOResultCB.BytesProcessed = 0;
810 ulBytesReturned = sizeof( AFSPIOCtlIOResultCB);
813 // Issue the request to the service
816 ntStatus = AFSProcessRequest( AFS_REQUEST_TYPE_PIOCTL_WRITE,
817 AFS_REQUEST_FLAG_SYNCHRONOUS,
823 (void *)&stIORequestCB,
824 sizeof( AFSPIOCtlIORequestCB),
828 if( !NT_SUCCESS( ntStatus))
831 try_return( ntStatus);
835 // Update the length written
838 Irp->IoStatus.Information = stIOResultCB.BytesProcessed;
842 if( stIORequestCB.MappedBuffer != NULL)
845 AFSUnmapServiceMappedBuffer( stIORequestCB.MappedBuffer,
852 AFSReleaseResource( &pFcb->NPFcb->Resource);
860 // This function is called when we know we have to read from the AFS Cache.
862 // It ensures that we have exents for the entirety of the write and
863 // then pins the extents into memory (meaning that although we may
864 // add we will not remove). Then it creates a scatter gather write
865 // and fires off the IRPs
869 AFSNonCachedWrite( IN PDEVICE_OBJECT DeviceObject,
871 IN LARGE_INTEGER StartingByte,
874 NTSTATUS ntStatus = STATUS_UNSUCCESSFUL;
875 VOID *pSystemBuffer = NULL;
876 BOOLEAN bPagingIo = BooleanFlagOn( Irp->Flags, IRP_PAGING_IO);
877 BOOLEAN bLocked = FALSE;
878 BOOLEAN bCompleteIrp = TRUE;
879 AFSGatherIo *pGatherIo = NULL;
880 AFSIoRun *pIoRuns = NULL;
881 AFSIoRun stIoRuns[AFS_MAX_STACK_IO_RUNS];
882 ULONG extentsCount = 0, runCount = 0;
883 AFSExtent *pStartExtent = NULL;
884 AFSExtent *pIgnoreExtent = NULL;
885 IO_STACK_LOCATION *pIrpSp = IoGetCurrentIrpStackLocation( Irp);
886 PFILE_OBJECT pFileObject = pIrpSp->FileObject;
887 AFSFcb *pFcb = (AFSFcb *)pFileObject->FsContext;
888 AFSCcb *pCcb = (AFSCcb *)pFileObject->FsContext2;
889 BOOLEAN bSynchronousFo = BooleanFlagOn( pFileObject->Flags, FO_SYNCHRONOUS_IO);
890 AFSDeviceExt *pDevExt = (AFSDeviceExt *)DeviceObject->DeviceExtension;
891 LARGE_INTEGER liCurrentTime, liLastRequestTime;
892 AFSDeviceExt *pControlDevExt = (AFSDeviceExt *)AFSControlDeviceObject->DeviceExtension;
893 PFILE_OBJECT pCacheFileObject = NULL;
894 BOOLEAN bDerefExtents = FALSE;
898 Irp->IoStatus.Information = 0;
900 AFSDbgLogMsg( AFS_SUBSYSTEM_IO_PROCESSING,
901 AFS_TRACE_LEVEL_VERBOSE,
902 "AFSNonCachedWrite (FO: %p) StartingByte %08lX:%08lX Length %08lX\n",
904 StartingByte.HighPart,
905 StartingByte.LowPart,
908 if (ByteCount > pDevExt->Specific.RDR.MaxIo.QuadPart)
911 AFSDbgLogMsg( AFS_SUBSYSTEM_IO_PROCESSING,
912 AFS_TRACE_LEVEL_ERROR,
913 "AFSNonCachedWrite (%p) Request %08lX Actual %08lX larger than MaxIO %I64X\n",
916 pIrpSp->Parameters.Write.Length,
917 pDevExt->Specific.RDR.MaxIo.QuadPart);
919 try_return( ntStatus = STATUS_UNSUCCESSFUL);
923 // Get the mapping for the buffer
925 pSystemBuffer = AFSLockSystemBuffer( Irp,
928 if( pSystemBuffer == NULL)
931 AFSDbgLogMsg( AFS_SUBSYSTEM_IO_PROCESSING,
932 AFS_TRACE_LEVEL_ERROR,
933 "AFSNonCachedWrite (%p) Failed to map system buffer\n",
936 try_return( ntStatus = STATUS_INSUFFICIENT_RESOURCES);
941 // Provoke a get of the extents - if we need to.
944 AFSDbgLogMsg( AFS_SUBSYSTEM_EXTENT_PROCESSING,
945 AFS_TRACE_LEVEL_VERBOSE,
946 "AFSNonCachedWrite Requesting extents for fid %08lX-%08lX-%08lX-%08lX Offset %I64X Length %08lX\n",
947 pFcb->ObjectInformation->FileId.Cell,
948 pFcb->ObjectInformation->FileId.Volume,
949 pFcb->ObjectInformation->FileId.Vnode,
950 pFcb->ObjectInformation->FileId.Unique,
951 StartingByte.QuadPart,
954 ntStatus = AFSRequestExtentsAsync( pFcb,
959 if (!NT_SUCCESS(ntStatus))
962 AFSDbgLogMsg( AFS_SUBSYSTEM_IO_PROCESSING,
963 AFS_TRACE_LEVEL_ERROR,
964 "AFSNonCachedWrite (%p) Failed to request extents Status %08lX\n",
968 try_return( ntStatus);
971 KeQueryTickCount( &liLastRequestTime);
976 AFSDbgLogMsg( AFS_SUBSYSTEM_LOCK_PROCESSING,
977 AFS_TRACE_LEVEL_VERBOSE,
978 "AFSNonCachedWrite Acquiring Fcb extents lock %p SHARED %08lX\n",
979 &pFcb->NPFcb->Specific.File.ExtentsResource,
980 PsGetCurrentThread());
982 ASSERT( !ExIsResourceAcquiredLite( &pFcb->NPFcb->Specific.File.ExtentsResource ));
984 AFSAcquireShared( &pFcb->NPFcb->Specific.File.ExtentsResource, TRUE );
988 pIgnoreExtent = NULL;
990 if ( AFSDoExtentsMapRegion( pFcb, &StartingByte, ByteCount, &pStartExtent, &pIgnoreExtent ))
995 KeClearEvent( &pFcb->NPFcb->Specific.File.ExtentsRequestComplete );
997 AFSDbgLogMsg( AFS_SUBSYSTEM_LOCK_PROCESSING,
998 AFS_TRACE_LEVEL_VERBOSE,
999 "AFSNonCachedWrite Releasing(1) Fcb extents lock %p SHARED %08lX\n",
1000 &pFcb->NPFcb->Specific.File.ExtentsResource,
1001 PsGetCurrentThread());
1003 AFSReleaseResource( &pFcb->NPFcb->Specific.File.ExtentsResource );
1007 // We will re-request the extents after waiting for them
1010 KeQueryTickCount( &liCurrentTime);
1012 if( liCurrentTime.QuadPart - liLastRequestTime.QuadPart >= pControlDevExt->Specific.Control.ExtentRequestTimeCount.QuadPart)
1015 AFSDbgLogMsg( AFS_SUBSYSTEM_EXTENT_PROCESSING,
1016 AFS_TRACE_LEVEL_VERBOSE,
1017 "AFSNonCachedWrite Requesting extents, again, for fid %08lX-%08lX-%08lX-%08lX Offset %I64X Length %08lX\n",
1018 pFcb->ObjectInformation->FileId.Cell,
1019 pFcb->ObjectInformation->FileId.Volume,
1020 pFcb->ObjectInformation->FileId.Vnode,
1021 pFcb->ObjectInformation->FileId.Unique,
1022 StartingByte.QuadPart,
1025 ntStatus = AFSRequestExtentsAsync( pFcb,
1030 if (!NT_SUCCESS(ntStatus))
1033 AFSDbgLogMsg( AFS_SUBSYSTEM_IO_PROCESSING,
1034 AFS_TRACE_LEVEL_ERROR,
1035 "AFSNonCachedWrite (%p) Failed to request extents Status %08lX\n",
1039 try_return( ntStatus);
1042 KeQueryTickCount( &liLastRequestTime);
1046 AFSDbgLogMsg( AFS_SUBSYSTEM_EXTENT_PROCESSING,
1047 AFS_TRACE_LEVEL_VERBOSE,
1048 "AFSNonCachedWrite Waiting for extents for fid %08lX-%08lX-%08lX-%08lX Offset %I64X Length %08lX\n",
1049 pFcb->ObjectInformation->FileId.Cell,
1050 pFcb->ObjectInformation->FileId.Volume,
1051 pFcb->ObjectInformation->FileId.Vnode,
1052 pFcb->ObjectInformation->FileId.Unique,
1053 StartingByte.QuadPart,
1060 ntStatus = AFSWaitForExtentMapping ( pFcb, pCcb);
1062 if (!NT_SUCCESS(ntStatus))
1065 AFSDbgLogMsg( AFS_SUBSYSTEM_EXTENT_PROCESSING,
1066 AFS_TRACE_LEVEL_ERROR,
1067 "AFSNonCachedWrite Failed wait for extents for fid %08lX-%08lX-%08lX-%08lX Offset %I64X Length %08lX Status %08lX\n",
1068 pFcb->ObjectInformation->FileId.Cell,
1069 pFcb->ObjectInformation->FileId.Volume,
1070 pFcb->ObjectInformation->FileId.Vnode,
1071 pFcb->ObjectInformation->FileId.Unique,
1072 StartingByte.QuadPart,
1076 try_return( ntStatus);
1081 // As per the read path -
1084 AFSDbgLogMsg( AFS_SUBSYSTEM_EXTENT_PROCESSING,
1085 AFS_TRACE_LEVEL_VERBOSE,
1086 "AFSNonCachedWrite Extents located for fid %08lX-%08lX-%08lX-%08lX Offset %I64X Length %08lX\n",
1087 pFcb->ObjectInformation->FileId.Cell,
1088 pFcb->ObjectInformation->FileId.Volume,
1089 pFcb->ObjectInformation->FileId.Vnode,
1090 pFcb->ObjectInformation->FileId.Unique,
1091 StartingByte.QuadPart,
1094 ntStatus = AFSGetExtents( pFcb,
1101 if (!NT_SUCCESS(ntStatus))
1104 AFSDbgLogMsg( AFS_SUBSYSTEM_IO_PROCESSING,
1105 AFS_TRACE_LEVEL_ERROR,
1106 "AFSNonCachedWrite (%p) Failed to retrieve mapped extents Status %08lX\n",
1110 try_return( ntStatus );
1113 AFSDbgLogMsg( AFS_SUBSYSTEM_IO_PROCESSING,
1114 AFS_TRACE_LEVEL_VERBOSE,
1115 "AFSNonCachedWrite (%p) Successfully retrieved map extents count %d run count %d\n",
1120 if( BooleanFlagOn( AFSLibControlFlags, AFS_REDIR_LIB_FLAGS_NONPERSISTENT_CACHE))
1123 Irp->IoStatus.Information = ByteCount;
1127 // Setup the MD5 for each extent
1130 AFSSetupMD5Hash( pFcb,
1138 ntStatus = AFSProcessExtentRun( pSystemBuffer,
1144 if (!NT_SUCCESS(ntStatus))
1147 AFSDbgLogMsg( AFS_SUBSYSTEM_IO_PROCESSING,
1148 AFS_TRACE_LEVEL_ERROR,
1149 "AFSNonCachedWrite (%p) Failed to process extent run for non-persistent cache Status %08lX\n",
1154 try_return( ntStatus);
1158 // Retrieve the cache file object
1161 pCacheFileObject = AFSReferenceCacheFileObject();
1163 if( pCacheFileObject == NULL)
1166 ntStatus = STATUS_DEVICE_NOT_READY;
1168 AFSDbgLogMsg( AFS_SUBSYSTEM_EXTENT_PROCESSING,
1169 AFS_TRACE_LEVEL_ERROR,
1170 "AFSNonCachedWrite Failed to retrieve cache fileobject for fid %08lX-%08lX-%08lX-%08lX Offset %I64X Length %08lX Status %08lX\n",
1171 pFcb->ObjectInformation->FileId.Cell,
1172 pFcb->ObjectInformation->FileId.Volume,
1173 pFcb->ObjectInformation->FileId.Vnode,
1174 pFcb->ObjectInformation->FileId.Unique,
1175 StartingByte.QuadPart,
1179 try_return( ntStatus);
1182 if (runCount > AFS_MAX_STACK_IO_RUNS)
1185 pIoRuns = (AFSIoRun*) AFSExAllocatePoolWithTag( PagedPool,
1186 runCount * sizeof( AFSIoRun ),
1188 if (NULL == pIoRuns)
1191 AFSDbgLogMsg( AFS_SUBSYSTEM_IO_PROCESSING,
1192 AFS_TRACE_LEVEL_ERROR,
1193 "AFSNonCachedWrite (%p) Failed to allocate IO run block\n",
1196 try_return( ntStatus = STATUS_INSUFFICIENT_RESOURCES );
1205 RtlZeroMemory( pIoRuns, runCount * sizeof( AFSIoRun ));
1207 ntStatus = AFSSetupIoRun( IoGetRelatedDeviceObject( pCacheFileObject),
1216 if (!NT_SUCCESS(ntStatus))
1219 AFSDbgLogMsg( AFS_SUBSYSTEM_IO_PROCESSING,
1220 AFS_TRACE_LEVEL_ERROR,
1221 "AFSNonCachedWrite (%p) Failed to initialize IO run block Status %08lX\n",
1225 try_return( ntStatus );
1228 AFSReferenceActiveExtents( pStartExtent,
1231 bDerefExtents = TRUE;
1233 AFSDbgLogMsg( AFS_SUBSYSTEM_LOCK_PROCESSING,
1234 AFS_TRACE_LEVEL_VERBOSE,
1235 "AFSNonCachedWrite Releasing(2) Fcb extents lock %p SHARED %08lX\n",
1236 &pFcb->NPFcb->Specific.File.ExtentsResource,
1237 PsGetCurrentThread());
1239 AFSReleaseResource( &pFcb->NPFcb->Specific.File.ExtentsResource );
1242 pGatherIo = (AFSGatherIo*) AFSExAllocatePoolWithTag( NonPagedPool,
1243 sizeof( AFSGatherIo),
1246 if (NULL == pGatherIo)
1249 AFSDbgLogMsg( AFS_SUBSYSTEM_IO_PROCESSING,
1250 AFS_TRACE_LEVEL_ERROR,
1251 "AFSNonCachedWrite (%p) Failed to allocate IO gather block\n",
1254 AFSDbgLogMsg( AFS_SUBSYSTEM_LOCK_PROCESSING,
1255 AFS_TRACE_LEVEL_VERBOSE,
1256 "AFSNonCachedWrite Acquiring(1) Fcb extents lock %p SHARED %08lX\n",
1257 &pFcb->NPFcb->Specific.File.ExtentsResource,
1258 PsGetCurrentThread());
1260 AFSAcquireShared( &pFcb->NPFcb->Specific.File.ExtentsResource,
1264 AFSDereferenceActiveExtents( pStartExtent,
1267 try_return (ntStatus = STATUS_INSUFFICIENT_RESOURCES );
1270 RtlZeroMemory( pGatherIo, sizeof( AFSGatherIo ));
1273 // Initialize count to 1, that was we won't get an early
1274 // completion if the first irp completes before the second is
1277 pGatherIo->Count = 1;
1278 pGatherIo->Status = STATUS_SUCCESS;
1279 pGatherIo->MasterIrp = Irp;
1280 pGatherIo->Synchronous = TRUE;
1281 pGatherIo->CompleteMasterIrp = FALSE;
1283 bCompleteIrp = TRUE;
1285 if( pGatherIo->Synchronous)
1287 KeInitializeEvent( &pGatherIo->Event, NotificationEvent, FALSE );
1292 // Setup the MD5 for each extent
1295 AFSSetupMD5Hash( pFcb,
1304 // Pre-emptively set up the count
1307 Irp->IoStatus.Information = ByteCount;
1309 ntStatus = AFSQueueStartIos( pCacheFileObject,
1311 IRP_WRITE_OPERATION | IRP_SYNCHRONOUS_API,
1316 AFSDbgLogMsg( AFS_SUBSYSTEM_IO_PROCESSING,
1317 AFS_TRACE_LEVEL_VERBOSE,
1318 "AFSNonCachedWrite (%p) AFSStartIos completed Status %08lX\n",
1322 if( !NT_SUCCESS( ntStatus))
1325 AFSDbgLogMsg( AFS_SUBSYSTEM_LOCK_PROCESSING,
1326 AFS_TRACE_LEVEL_VERBOSE,
1327 "AFSNonCachedWrite Acquiring(2) Fcb extents lock %p SHARED %08lX\n",
1328 &pFcb->NPFcb->Specific.File.ExtentsResource,
1329 PsGetCurrentThread());
1331 AFSAcquireShared( &pFcb->NPFcb->Specific.File.ExtentsResource,
1335 AFSDereferenceActiveExtents( pStartExtent,
1338 try_return( ntStatus);
1342 // Wait for completion of All IOs we started.
1345 ntStatus = KeWaitForSingleObject( &pGatherIo->Event,
1351 if( NT_SUCCESS( ntStatus))
1354 ntStatus = pGatherIo->Status;
1357 if( !NT_SUCCESS( ntStatus))
1360 AFSDbgLogMsg( AFS_SUBSYSTEM_LOCK_PROCESSING,
1361 AFS_TRACE_LEVEL_VERBOSE,
1362 "AFSNonCachedWrite Acquiring(3) Fcb extents lock %p SHARED %08lX\n",
1363 &pFcb->NPFcb->Specific.File.ExtentsResource,
1364 PsGetCurrentThread());
1366 AFSAcquireShared( &pFcb->NPFcb->Specific.File.ExtentsResource,
1370 AFSDereferenceActiveExtents( pStartExtent,
1373 try_return( ntStatus);
1378 if( NT_SUCCESS( ntStatus) &&
1379 pStartExtent != NULL &&
1380 Irp->IoStatus.Information > 0)
1386 AFSAcquireShared( &pFcb->NPFcb->Specific.File.ExtentsResource,
1392 // Since this is dirty we can mark the extents dirty now.
1393 // AFSMarkDirty will dereference the extents. Do not call
1394 // AFSDereferenceActiveExtents() in this code path.
1406 // This was an uncached user write - tell the server to do
1407 // the flush when the worker thread next wakes up
1409 pFcb->Specific.File.LastServerFlush.QuadPart = 0;
1413 if( pCacheFileObject != NULL)
1415 AFSReleaseCacheFileObject( pCacheFileObject);
1418 AFSDbgLogMsg( AFS_SUBSYSTEM_IO_PROCESSING,
1419 AFS_TRACE_LEVEL_VERBOSE,
1420 "AFSNonCachedWrite (FO: %p) StartingByte %08lX:%08lX Length %08lX Status %08lX\n",
1422 StartingByte.HighPart,
1423 StartingByte.LowPart,
1427 if (NT_SUCCESS(ntStatus) &&
1432 pFileObject->CurrentByteOffset.QuadPart = StartingByte.QuadPart + ByteCount;
1438 AFSDbgLogMsg( AFS_SUBSYSTEM_LOCK_PROCESSING,
1439 AFS_TRACE_LEVEL_VERBOSE,
1440 "AFSNonCachedWrite Releasing Fcb extents lock %p SHARED %08lX\n",
1441 &pFcb->NPFcb->Specific.File.ExtentsResource,
1442 PsGetCurrentThread());
1444 AFSReleaseResource( &pFcb->NPFcb->Specific.File.ExtentsResource );
1449 AFSExFreePoolWithTag(pGatherIo, AFS_GATHER_TAG);
1452 if( NULL != pIoRuns &&
1453 stIoRuns != pIoRuns)
1455 AFSExFreePoolWithTag(pIoRuns, AFS_IO_RUN_TAG);
1461 AFSDbgLogMsg( AFS_SUBSYSTEM_IO_PROCESSING,
1462 AFS_TRACE_LEVEL_VERBOSE,
1463 "AFSNonCachedWrite Completing Irp %p Status %08lX Info %08lX\n",
1466 Irp->IoStatus.Information);
1468 AFSCompleteRequest( Irp, ntStatus);
1477 AFSCachedWrite( IN PDEVICE_OBJECT DeviceObject,
1479 IN LARGE_INTEGER StartingByte,
1481 IN BOOLEAN ForceFlush)
1483 UNREFERENCED_PARAMETER(DeviceObject);
1484 PVOID pSystemBuffer = NULL;
1485 NTSTATUS ntStatus = STATUS_SUCCESS;
1486 IO_STATUS_BLOCK iosbFlush;
1487 IO_STACK_LOCATION *pIrpSp = IoGetCurrentIrpStackLocation( Irp);
1488 PFILE_OBJECT pFileObject = pIrpSp->FileObject;
1489 AFSFcb *pFcb = (AFSFcb *)pFileObject->FsContext;
1490 BOOLEAN bSynchronousFo = BooleanFlagOn( pFileObject->Flags, FO_SYNCHRONOUS_IO);
1491 ULONG ulCurrentIO = 0, ulTotalLen = ByteCount;
1492 PMDL pCurrentMdl = Irp->MdlAddress;
1493 LARGE_INTEGER liCurrentOffset;
1498 Irp->IoStatus.Information = 0;
1500 if( BooleanFlagOn( pIrpSp->MinorFunction, IRP_MN_MDL))
1506 CcPrepareMdlWrite( pFileObject,
1512 ntStatus = Irp->IoStatus.Status;
1514 __except( EXCEPTION_EXECUTE_HANDLER)
1516 ntStatus = GetExceptionCode();
1518 AFSDbgLogMsg( AFS_SUBSYSTEM_IO_PROCESSING,
1519 AFS_TRACE_LEVEL_ERROR,
1520 "AFSCachedWrite (%p) Exception thrown while preparing mdl write Status %08lX\n",
1525 if( !NT_SUCCESS( ntStatus))
1529 // Free up any potentially allocated mdl's
1532 CcMdlWriteComplete( pFileObject,
1536 Irp->MdlAddress = NULL;
1538 AFSDbgLogMsg( AFS_SUBSYSTEM_IO_PROCESSING,
1539 AFS_TRACE_LEVEL_ERROR,
1540 "AFSCachedWrite (%p) Failed to process MDL write Status %08lX\n",
1545 try_return( ntStatus);
1548 liCurrentOffset.QuadPart = StartingByte.QuadPart;
1550 while( ulTotalLen > 0)
1553 ntStatus = STATUS_SUCCESS;
1555 if( pCurrentMdl != NULL)
1558 pSystemBuffer = MmGetSystemAddressForMdlSafe( pCurrentMdl,
1559 NormalPagePriority);
1561 ulCurrentIO = MmGetMdlByteCount( pCurrentMdl);
1563 if( ulCurrentIO > ulTotalLen)
1565 ulCurrentIO = ulTotalLen;
1571 pSystemBuffer = AFSLockSystemBuffer( Irp,
1574 ulCurrentIO = ulTotalLen;
1577 if( pSystemBuffer == NULL)
1580 AFSDbgLogMsg( AFS_SUBSYSTEM_IO_PROCESSING,
1581 AFS_TRACE_LEVEL_ERROR,
1582 "AFSCachedWrite (%p) Failed to lock system buffer\n",
1585 try_return( ntStatus = STATUS_INSUFFICIENT_RESOURCES);
1591 if( !CcCopyWrite( pFileObject,
1598 // Failed to process request.
1601 AFSDbgLogMsg( AFS_SUBSYSTEM_FILE_PROCESSING,
1602 AFS_TRACE_LEVEL_ERROR,
1603 "AFSCachedWrite (%p) Failed to issue CcCopyWrite %wZ @ %0I64X Status %08lX\n",
1605 &pFileObject->FileName,
1606 liCurrentOffset.QuadPart,
1607 Irp->IoStatus.Status);
1609 try_return( ntStatus = STATUS_UNSUCCESSFUL);
1612 __except( EXCEPTION_EXECUTE_HANDLER)
1615 ntStatus = GetExceptionCode();
1617 AFSDbgLogMsg( AFS_SUBSYSTEM_IO_PROCESSING,
1618 AFS_TRACE_LEVEL_ERROR,
1619 "AFSCachedWrite (%p) CcCopyWrite Threw exception %wZ @ %0I64X Status %08lX\n",
1621 &pFileObject->FileName,
1622 liCurrentOffset.QuadPart,
1626 if( !NT_SUCCESS( ntStatus))
1628 try_return( ntStatus);
1632 BooleanFlagOn(pFileObject->Flags, (FO_NO_INTERMEDIATE_BUFFERING + FO_WRITE_THROUGH)))
1636 // We have detected a file we do a write through with.
1639 CcFlushCache(&pFcb->NPFcb->SectionObjectPointers,
1644 if( !NT_SUCCESS( iosbFlush.Status))
1647 AFSDbgLogMsg( AFS_SUBSYSTEM_IO_PROCESSING,
1648 AFS_TRACE_LEVEL_ERROR,
1649 "AFSCachedWrite (%p) CcFlushCache failure %wZ FID %08lX-%08lX-%08lX-%08lX Status 0x%08lX Bytes 0x%08lX\n",
1651 &pFileObject->FileName,
1652 pFcb->ObjectInformation->FileId.Cell,
1653 pFcb->ObjectInformation->FileId.Volume,
1654 pFcb->ObjectInformation->FileId.Vnode,
1655 pFcb->ObjectInformation->FileId.Unique,
1657 iosbFlush.Information);
1659 try_return( ntStatus = iosbFlush.Status);
1663 if( ulTotalLen <= ulCurrentIO)
1668 liCurrentOffset.QuadPart += ulCurrentIO;
1670 ulTotalLen -= ulCurrentIO;
1672 pCurrentMdl = pCurrentMdl->Next;
1677 if( NT_SUCCESS( ntStatus))
1680 Irp->IoStatus.Information = ByteCount;
1685 pFileObject->CurrentByteOffset.QuadPart = StartingByte.QuadPart + ByteCount;
1689 // If this extended the Vdl, then update it accordingly
1692 if( StartingByte.QuadPart + ByteCount > pFcb->Header.ValidDataLength.QuadPart)
1695 pFcb->Header.ValidDataLength.QuadPart = StartingByte.QuadPart + ByteCount;
1699 BooleanFlagOn(pFileObject->Flags, (FO_NO_INTERMEDIATE_BUFFERING + FO_WRITE_THROUGH)))
1702 // Write through asked for... Set things so that we get
1703 // flush when the worker thread next wakes up
1705 pFcb->Specific.File.LastServerFlush.QuadPart = 0;
1708 if( !BooleanFlagOn( pFcb->Flags, AFS_FCB_FLAG_UPDATE_LAST_WRITE_TIME))
1711 SetFlag( pFcb->Flags, AFS_FCB_FLAG_UPDATE_WRITE_TIME);
1713 KeQuerySystemTime( &pFcb->ObjectInformation->LastWriteTime);
1717 AFSCompleteRequest( Irp,
1726 AFSExtendingWrite( IN AFSFcb *Fcb,
1727 IN PFILE_OBJECT FileObject,
1728 IN LONGLONG NewLength)
1730 LARGE_INTEGER liSaveFileSize = Fcb->Header.FileSize;
1731 LARGE_INTEGER liSaveAllocation = Fcb->Header.AllocationSize;
1732 NTSTATUS ntStatus = STATUS_SUCCESS;
1733 AFSCcb *pCcb = (AFSCcb *)FileObject->FsContext2;
1735 if( NewLength > Fcb->Header.AllocationSize.QuadPart)
1738 Fcb->Header.AllocationSize.QuadPart = NewLength;
1740 Fcb->ObjectInformation->AllocationSize = Fcb->Header.AllocationSize;
1743 if( NewLength > Fcb->Header.FileSize.QuadPart)
1746 Fcb->Header.FileSize.QuadPart = NewLength;
1748 Fcb->ObjectInformation->EndOfFile = Fcb->Header.FileSize;
1755 ntStatus = AFSUpdateFileInformation( &Fcb->ObjectInformation->ParentFileId,
1756 Fcb->ObjectInformation,
1759 if (NT_SUCCESS(ntStatus))
1762 KeQuerySystemTime( &Fcb->ObjectInformation->ChangeTime);
1764 SetFlag( Fcb->Flags, AFS_FCB_FLAG_FILE_MODIFIED | AFS_FCB_FLAG_UPDATE_CHANGE_TIME);
1767 // If the file is currently cached, then let the MM know about the extension
1770 if( CcIsFileCached( FileObject))
1772 CcSetFileSizes( FileObject,
1773 (PCC_FILE_SIZES)&Fcb->Header.AllocationSize);
1778 Fcb->Header.FileSize = liSaveFileSize;
1779 Fcb->Header.AllocationSize = liSaveAllocation;
1783 // DownConvert file resource to shared
1785 ExConvertExclusiveToSharedLite( &Fcb->NPFcb->Resource);
1791 AFSShareWrite( IN PDEVICE_OBJECT DeviceObject,
1795 UNREFERENCED_PARAMETER(DeviceObject);
1796 NTSTATUS ntStatus = STATUS_SUCCESS;
1797 PIO_STACK_LOCATION pIrpSp = IoGetCurrentIrpStackLocation( Irp);
1798 AFSFcb *pFcb = NULL;
1799 AFSCcb *pCcb = NULL;
1800 AFSPipeIORequestCB *pIoRequest = NULL;
1801 void *pBuffer = NULL;
1802 AFSPipeIOResultCB stIoResult;
1803 ULONG ulBytesReturned = 0;
1808 pFcb = (AFSFcb *)pIrpSp->FileObject->FsContext;
1810 pCcb = (AFSCcb *)pIrpSp->FileObject->FsContext2;
1812 AFSDbgLogMsg( AFS_SUBSYSTEM_PIPE_PROCESSING,
1813 AFS_TRACE_LEVEL_VERBOSE,
1814 "AFSShareWrite On pipe %wZ Length %08lX\n",
1815 &pCcb->DirectoryCB->NameInformation.FileName,
1816 pIrpSp->Parameters.Write.Length);
1818 if( pIrpSp->Parameters.Write.Length == 0)
1822 // Nothing to do in this case
1825 try_return( ntStatus);
1829 // Retrieve the buffer for the read request
1832 pBuffer = AFSLockSystemBuffer( Irp,
1833 pIrpSp->Parameters.Write.Length);
1835 if( pBuffer == NULL)
1838 AFSDbgLogMsg( AFS_SUBSYSTEM_PIPE_PROCESSING,
1839 AFS_TRACE_LEVEL_ERROR,
1840 "AFSShareWrite Failed to map buffer on pipe %wZ\n",
1841 &pCcb->DirectoryCB->NameInformation.FileName);
1843 try_return( ntStatus = STATUS_INSUFFICIENT_RESOURCES);
1846 AFSAcquireShared( &pFcb->NPFcb->Resource,
1849 pIoRequest = (AFSPipeIORequestCB *)AFSExAllocatePoolWithTag( PagedPool,
1850 sizeof( AFSPipeIORequestCB) +
1851 pIrpSp->Parameters.Write.Length,
1852 AFS_GENERIC_MEMORY_14_TAG);
1854 if( pIoRequest == NULL)
1857 try_return( ntStatus = STATUS_INSUFFICIENT_RESOURCES);
1860 RtlZeroMemory( pIoRequest,
1861 sizeof( AFSPipeIORequestCB) + pIrpSp->Parameters.Write.Length);
1863 pIoRequest->RequestId = pCcb->RequestID;
1865 pIoRequest->RootId = pFcb->ObjectInformation->VolumeCB->ObjectInformation.FileId;
1867 pIoRequest->BufferLength = pIrpSp->Parameters.Write.Length;
1869 RtlCopyMemory( (void *)((char *)pIoRequest + sizeof( AFSPipeIORequestCB)),
1871 pIrpSp->Parameters.Write.Length);
1873 stIoResult.BytesProcessed = 0;
1875 ulBytesReturned = sizeof( AFSPipeIOResultCB);
1878 // Issue the open request to the service
1881 ntStatus = AFSProcessRequest( AFS_REQUEST_TYPE_PIPE_WRITE,
1882 AFS_REQUEST_FLAG_SYNCHRONOUS,
1884 &pCcb->DirectoryCB->NameInformation.FileName,
1889 sizeof( AFSPipeIORequestCB) +
1890 pIrpSp->Parameters.Write.Length,
1894 if( !NT_SUCCESS( ntStatus))
1897 AFSDbgLogMsg( AFS_SUBSYSTEM_FILE_PROCESSING,
1898 AFS_TRACE_LEVEL_ERROR,
1899 "AFSShareWrite (%p) Failed service write Status %08lX\n",
1903 try_return( ntStatus);
1906 AFSDbgLogMsg( AFS_SUBSYSTEM_PIPE_PROCESSING,
1907 AFS_TRACE_LEVEL_VERBOSE,
1908 "AFSShareWrite Completed on pipe %wZ Length read %08lX\n",
1909 &pCcb->DirectoryCB->NameInformation.FileName,
1910 stIoResult.BytesProcessed);
1912 Irp->IoStatus.Information = stIoResult.BytesProcessed;
1919 AFSReleaseResource( &pFcb->NPFcb->Resource);
1922 if( pIoRequest != NULL)
1925 AFSExFreePoolWithTag( pIoRequest, AFS_GENERIC_MEMORY_14_TAG);