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) {
343 if( pFileObject->PrivateCacheMap == NULL)
346 AFSDbgLogMsg( AFS_SUBSYSTEM_LOCK_PROCESSING,
347 AFS_TRACE_LEVEL_VERBOSE,
348 "AFSCommonWrite Acquiring Fcb SectionObject lock %p EXCL %08lX\n",
349 &pNPFcb->SectionObjectResource,
350 PsGetCurrentThread());
352 AFSAcquireExcl( &pNPFcb->SectionObjectResource,
355 bReleaseSectionObject = TRUE;
360 AFSDbgLogMsg( AFS_SUBSYSTEM_IO_PROCESSING,
361 AFS_TRACE_LEVEL_VERBOSE,
362 "AFSCommonWrite Initialize caching on Fcb %p FileObject %p\n",
366 CcInitializeCacheMap( pFileObject,
367 (PCC_FILE_SIZES)&pFcb->Header.AllocationSize,
369 AFSLibCacheManagerCallbacks,
372 CcSetReadAheadGranularity( pFileObject,
373 pDeviceExt->Specific.RDR.MaximumRPCLength);
375 CcSetDirtyPageThreshold( pFileObject,
376 AFS_DIRTY_CHUNK_THRESHOLD * pDeviceExt->Specific.RDR.MaximumRPCLength / 4096);
378 __except( EXCEPTION_EXECUTE_HANDLER)
381 ntStatus = GetExceptionCode();
383 AFSDbgLogMsg( AFS_SUBSYSTEM_IO_PROCESSING,
384 AFS_TRACE_LEVEL_ERROR,
385 "AFSCommonWrite (%p) Exception thrown while initializing cache map Status %08lX\n",
390 AFSDbgLogMsg( AFS_SUBSYSTEM_LOCK_PROCESSING,
391 AFS_TRACE_LEVEL_VERBOSE,
392 "AFSCommonWrite Releasing Fcb SectionObject lock %p EXCL %08lX\n",
393 &pNPFcb->SectionObjectResource,
394 PsGetCurrentThread());
396 AFSReleaseResource( &pNPFcb->SectionObjectResource);
398 bReleaseSectionObject = FALSE;
401 if( !NT_SUCCESS( ntStatus))
404 try_return( ntStatus);
409 while (!CcCanIWrite( pFileObject,
414 static const LONGLONG llWriteDelay = (LONGLONG)-100000;
416 KeDelayExecutionThread(KernelMode, FALSE, (PLARGE_INTEGER)&llWriteDelay);
421 // Save off the PID if this is not a paging IO
425 ( pFcb->Specific.File.ExtentRequestProcessId == 0 ||
426 ( ullProcessId != (ULONGLONG)AFSSysProcess &&
427 pFcb->Specific.File.ExtentRequestProcessId != ullProcessId)))
430 pFcb->Specific.File.ExtentRequestProcessId = ullProcessId;
432 if( ullProcessId == (ULONGLONG)AFSSysProcess)
434 AFSDbgLogMsg( AFS_SUBSYSTEM_EXTENT_PROCESSING,
435 AFS_TRACE_LEVEL_WARNING,
436 "%s Setting LastWriterExtentProcessId to system process for Fcb %p\n",
445 // - if Paging then we need to do nothing (the precalls will
446 // have acquired the paging resource), for clarity we will collect
447 // the paging resource
448 // - If extending Write then take the fileresource EX (EOF will change, Allocation will only move out)
449 // - Otherwise we collect the file shared, check against extending and
460 //ASSERT( NULL != OnBehalfOf || ExIsResourceAcquiredLite( &pNPFcb->Resource ));
462 AFSDbgLogMsg( AFS_SUBSYSTEM_LOCK_PROCESSING,
463 AFS_TRACE_LEVEL_VERBOSE,
464 "AFSCommonWrite Acquiring Fcb PagingIo lock %p SHARED %08lX\n",
465 &pNPFcb->PagingResource,
466 PsGetCurrentThread());
468 AFSAcquireShared( &pNPFcb->PagingResource,
471 bReleasePaging = TRUE;
474 // We have the correct lock - we cannot have the wrong one
481 bExtendingWrite = (((liStartingByte.QuadPart + ulByteCount) >=
482 pFcb->Header.FileSize.QuadPart) ||
483 (liStartingByte.LowPart == FILE_WRITE_TO_END_OF_FILE &&
484 liStartingByte.HighPart == -1)) ;
489 // Check for lock inversion
492 ASSERT( !ExIsResourceAcquiredLite( &pNPFcb->PagingResource ));
494 AFSDbgLogMsg( AFS_SUBSYSTEM_LOCK_PROCESSING,
495 AFS_TRACE_LEVEL_VERBOSE,
496 "AFSCommonWrite Acquiring Fcb lock %p EXCL %08lX\n",
498 PsGetCurrentThread());
500 AFSAcquireExcl( &pNPFcb->Resource,
505 AFSDbgLogMsg( AFS_SUBSYSTEM_LOCK_PROCESSING,
506 AFS_TRACE_LEVEL_VERBOSE,
507 "AFSCommonWrite Acquiring Fcb SectionObject lock %p EXCL %08lX\n",
508 &pNPFcb->SectionObjectResource,
509 PsGetCurrentThread());
511 AFSAcquireExcl( &pNPFcb->SectionObjectResource,
514 bReleaseSectionObject = TRUE;
516 if (liStartingByte.LowPart == FILE_WRITE_TO_END_OF_FILE &&
517 liStartingByte.HighPart == -1)
519 if (pFcb->Header.ValidDataLength.QuadPart > pFcb->Header.FileSize.QuadPart)
521 liStartingByte = pFcb->Header.ValidDataLength;
525 liStartingByte = pFcb->Header.FileSize;
530 // We have the correct lock - even if we don't end up truncating
536 ASSERT( !ExIsResourceAcquiredLite( &pNPFcb->PagingResource ));
538 AFSDbgLogMsg( AFS_SUBSYSTEM_LOCK_PROCESSING,
539 AFS_TRACE_LEVEL_VERBOSE,
540 "AFSCommonWrite Acquiring Fcb lock %p SHARED %08lX\n",
542 PsGetCurrentThread());
544 AFSAcquireShared( &pNPFcb->Resource,
549 AFSDbgLogMsg( AFS_SUBSYSTEM_LOCK_PROCESSING,
550 AFS_TRACE_LEVEL_VERBOSE,
551 "AFSCommonWrite Acquiring Fcb SectionObject lock %p SHARED %08lX\n",
552 &pNPFcb->SectionObjectResource,
553 PsGetCurrentThread());
555 AFSAcquireShared( &pNPFcb->SectionObjectResource,
558 bReleaseSectionObject = TRUE;
561 // Have things moved? Are we extending? If so, the the lock isn't OK
563 bLockOK = (liStartingByte.QuadPart + ulByteCount) < pFcb->Header.FileSize.QuadPart;
567 AFSReleaseResource( &pNPFcb->Resource);
569 bReleaseMain = FALSE;
580 // Check the BR locks on the file.
583 if ( !FsRtlCheckLockForWriteAccess( &pFcb->Specific.File.FileLock,
587 AFSDbgLogMsg( AFS_SUBSYSTEM_IO_PROCESSING,
588 AFS_TRACE_LEVEL_ERROR,
589 "AFSCommonWrite (%p) Request failed due to lock conflict\n",
592 try_return( ntStatus = STATUS_FILE_LOCK_CONFLICT);
598 ntStatus = AFSExtendingWrite( pFcb, pFileObject, (liStartingByte.QuadPart + ulByteCount));
600 if( !NT_SUCCESS(ntStatus))
603 AFSDbgLogMsg( AFS_SUBSYSTEM_IO_PROCESSING,
604 AFS_TRACE_LEVEL_ERROR,
605 "AFSCommonWrite (%p) Failed extending write request Status %08lX\n",
609 try_return( ntStatus );
615 // Fire off the request as appropriate
617 bCompleteIrp = FALSE;
624 // Main and SectionObject resources held Shared
627 AFSDbgLogMsg( AFS_SUBSYSTEM_IO_PROCESSING,
628 AFS_TRACE_LEVEL_VERBOSE,
629 "AFSCommonWrite (%p) Processing CACHED request Offset %I64X Len %08lX\n",
631 liStartingByte.QuadPart,
634 ntStatus = AFSCachedWrite( DeviceObject, Irp, liStartingByte, ulByteCount, TRUE);
641 // if bPagingIo, Paging Resource held Shared
642 // else Main and SectionObject resources held Shared
645 if( bReleaseSectionObject)
648 AFSReleaseResource( &pNPFcb->SectionObjectResource);
650 bReleaseSectionObject = FALSE;
656 AFSReleaseResource( &pNPFcb->PagingResource);
658 bReleasePaging = FALSE;
664 AFSReleaseResource( &pNPFcb->Resource);
666 bReleaseMain = FALSE;
669 AFSDbgLogMsg( AFS_SUBSYSTEM_IO_PROCESSING,
670 AFS_TRACE_LEVEL_VERBOSE,
671 "AFSCommonWrite (%p) Processing NON-CACHED request Offset %I64X Len %08lX\n",
673 liStartingByte.QuadPart,
676 ntStatus = AFSNonCachedWrite( DeviceObject, Irp, liStartingByte, ulByteCount);
681 AFSDbgLogMsg( AFS_SUBSYSTEM_IO_PROCESSING,
682 AFS_TRACE_LEVEL_VERBOSE,
683 "AFSCommonWrite (%p) Process complete Status %08lX\n",
687 ObDereferenceObject(pFileObject);
689 if( bReleaseSectionObject)
692 AFSReleaseResource( &pNPFcb->SectionObjectResource);
698 AFSReleaseResource( &pNPFcb->PagingResource);
704 AFSReleaseResource( &pNPFcb->Resource);
710 AFSCompleteRequest( Irp,
719 AFSIOCtlWrite( IN PDEVICE_OBJECT DeviceObject,
723 UNREFERENCED_PARAMETER(DeviceObject);
724 NTSTATUS ntStatus = STATUS_SUCCESS;
725 AFSPIOCtlIORequestCB stIORequestCB;
726 PIO_STACK_LOCATION pIrpSp = IoGetCurrentIrpStackLocation( Irp);
729 AFSPIOCtlIOResultCB stIOResultCB;
730 ULONG ulBytesReturned = 0;
731 AFSFileID stParentFID;
736 RtlZeroMemory( &stIORequestCB,
737 sizeof( AFSPIOCtlIORequestCB));
739 if( pIrpSp->Parameters.Write.Length == 0)
743 // Nothing to do in this case
746 try_return( ntStatus);
749 pFcb = (AFSFcb *)pIrpSp->FileObject->FsContext;
751 pCcb = (AFSCcb *)pIrpSp->FileObject->FsContext2;
753 AFSDbgLogMsg( AFS_SUBSYSTEM_LOCK_PROCESSING,
754 AFS_TRACE_LEVEL_VERBOSE,
755 "AFSIOCtlWrite Acquiring Fcb lock %p SHARED %08lX\n",
756 &pFcb->NPFcb->Resource,
757 PsGetCurrentThread());
759 AFSAcquireShared( &pFcb->NPFcb->Resource,
763 // Get the parent fid to pass to the cm
766 RtlZeroMemory( &stParentFID,
769 if( pFcb->ObjectInformation->ParentObjectInformation != NULL)
773 // The parent directory FID of the node
776 stParentFID = pFcb->ObjectInformation->ParentObjectInformation->FileId;
780 // Set the control block up
783 stIORequestCB.RequestId = pCcb->RequestID;
785 if( pFcb->ObjectInformation->VolumeCB != NULL)
787 stIORequestCB.RootId = pFcb->ObjectInformation->VolumeCB->ObjectInformation.FileId;
791 // Lock down the buffer
794 stIORequestCB.MappedBuffer = AFSMapToService( Irp,
795 pIrpSp->Parameters.Write.Length);
797 if( stIORequestCB.MappedBuffer == NULL)
800 try_return( ntStatus = STATUS_INSUFFICIENT_RESOURCES);
803 stIORequestCB.BufferLength = pIrpSp->Parameters.Write.Length;
805 stIOResultCB.BytesProcessed = 0;
807 ulBytesReturned = sizeof( AFSPIOCtlIOResultCB);
810 // Issue the request to the service
813 ntStatus = AFSProcessRequest( AFS_REQUEST_TYPE_PIOCTL_WRITE,
814 AFS_REQUEST_FLAG_SYNCHRONOUS,
818 (void *)&stIORequestCB,
819 sizeof( AFSPIOCtlIORequestCB),
823 if( !NT_SUCCESS( ntStatus))
826 try_return( ntStatus);
830 // Update the length written
833 Irp->IoStatus.Information = stIOResultCB.BytesProcessed;
837 if( stIORequestCB.MappedBuffer != NULL)
840 AFSUnmapServiceMappedBuffer( stIORequestCB.MappedBuffer,
847 AFSReleaseResource( &pFcb->NPFcb->Resource);
855 // This function is called when we know we have to read from the AFS Cache.
857 // It ensures that we have exents for the entirety of the write and
858 // then pins the extents into memory (meaning that although we may
859 // add we will not remove). Then it creates a scatter gather write
860 // and fires off the IRPs
864 AFSNonCachedWrite( IN PDEVICE_OBJECT DeviceObject,
866 IN LARGE_INTEGER StartingByte,
869 NTSTATUS ntStatus = STATUS_UNSUCCESSFUL;
870 VOID *pSystemBuffer = NULL;
871 BOOLEAN bPagingIo = BooleanFlagOn( Irp->Flags, IRP_PAGING_IO);
872 BOOLEAN bLocked = FALSE;
873 BOOLEAN bCompleteIrp = TRUE;
874 AFSGatherIo *pGatherIo = NULL;
875 AFSIoRun *pIoRuns = NULL;
876 AFSIoRun stIoRuns[AFS_MAX_STACK_IO_RUNS];
877 ULONG extentsCount = 0, runCount = 0;
878 AFSExtent *pStartExtent = NULL;
879 AFSExtent *pIgnoreExtent = NULL;
880 IO_STACK_LOCATION *pIrpSp = IoGetCurrentIrpStackLocation( Irp);
881 PFILE_OBJECT pFileObject = pIrpSp->FileObject;
882 AFSFcb *pFcb = (AFSFcb *)pFileObject->FsContext;
883 AFSCcb *pCcb = (AFSCcb *)pFileObject->FsContext2;
884 BOOLEAN bSynchronousFo = BooleanFlagOn( pFileObject->Flags, FO_SYNCHRONOUS_IO);
885 AFSDeviceExt *pDevExt = (AFSDeviceExt *)DeviceObject->DeviceExtension;
886 LARGE_INTEGER liCurrentTime, liLastRequestTime;
887 AFSDeviceExt *pControlDevExt = (AFSDeviceExt *)AFSControlDeviceObject->DeviceExtension;
888 PFILE_OBJECT pCacheFileObject = NULL;
889 BOOLEAN bDerefExtents = FALSE;
893 Irp->IoStatus.Information = 0;
895 if (ByteCount > pDevExt->Specific.RDR.MaxIo.QuadPart)
898 AFSDbgLogMsg( AFS_SUBSYSTEM_IO_PROCESSING,
899 AFS_TRACE_LEVEL_ERROR,
900 "AFSNonCachedWrite (%p) Request %08lX Actual %08lX larger than MaxIO %I64X\n",
903 pIrpSp->Parameters.Write.Length,
904 pDevExt->Specific.RDR.MaxIo.QuadPart);
906 try_return( ntStatus = STATUS_UNSUCCESSFUL);
910 // Get the mapping for the buffer
912 pSystemBuffer = AFSLockSystemBuffer( Irp,
915 if( pSystemBuffer == NULL)
918 AFSDbgLogMsg( AFS_SUBSYSTEM_IO_PROCESSING,
919 AFS_TRACE_LEVEL_ERROR,
920 "AFSNonCachedWrite (%p) Failed to map system buffer\n",
923 try_return( ntStatus = STATUS_INSUFFICIENT_RESOURCES);
928 // Provoke a get of the extents - if we need to.
931 AFSDbgLogMsg( AFS_SUBSYSTEM_EXTENT_PROCESSING,
932 AFS_TRACE_LEVEL_VERBOSE,
933 "AFSNonCachedWrite Requesting extents for fid %08lX-%08lX-%08lX-%08lX Offset %I64X Length %08lX\n",
934 pFcb->ObjectInformation->FileId.Cell,
935 pFcb->ObjectInformation->FileId.Volume,
936 pFcb->ObjectInformation->FileId.Vnode,
937 pFcb->ObjectInformation->FileId.Unique,
938 StartingByte.QuadPart,
941 ntStatus = AFSRequestExtentsAsync( pFcb,
946 if (!NT_SUCCESS(ntStatus))
949 AFSDbgLogMsg( AFS_SUBSYSTEM_IO_PROCESSING,
950 AFS_TRACE_LEVEL_ERROR,
951 "AFSNonCachedWrite (%p) Failed to request extents Status %08lX\n",
955 try_return( ntStatus);
958 KeQueryTickCount( &liLastRequestTime);
963 AFSDbgLogMsg( AFS_SUBSYSTEM_LOCK_PROCESSING,
964 AFS_TRACE_LEVEL_VERBOSE,
965 "AFSNonCachedWrite Acquiring Fcb extents lock %p SHARED %08lX\n",
966 &pFcb->NPFcb->Specific.File.ExtentsResource,
967 PsGetCurrentThread());
969 ASSERT( !ExIsResourceAcquiredLite( &pFcb->NPFcb->Specific.File.ExtentsResource ));
971 AFSAcquireShared( &pFcb->NPFcb->Specific.File.ExtentsResource, TRUE );
975 pIgnoreExtent = NULL;
977 if ( AFSDoExtentsMapRegion( pFcb, &StartingByte, ByteCount, &pStartExtent, &pIgnoreExtent ))
982 KeClearEvent( &pFcb->NPFcb->Specific.File.ExtentsRequestComplete );
984 AFSDbgLogMsg( AFS_SUBSYSTEM_LOCK_PROCESSING,
985 AFS_TRACE_LEVEL_VERBOSE,
986 "AFSNonCachedWrite Releasing(1) Fcb extents lock %p SHARED %08lX\n",
987 &pFcb->NPFcb->Specific.File.ExtentsResource,
988 PsGetCurrentThread());
990 AFSReleaseResource( &pFcb->NPFcb->Specific.File.ExtentsResource );
994 // We will re-request the extents after waiting for them
997 KeQueryTickCount( &liCurrentTime);
999 if( liCurrentTime.QuadPart - liLastRequestTime.QuadPart >= pControlDevExt->Specific.Control.ExtentRequestTimeCount.QuadPart)
1002 AFSDbgLogMsg( AFS_SUBSYSTEM_EXTENT_PROCESSING,
1003 AFS_TRACE_LEVEL_VERBOSE,
1004 "AFSNonCachedWrite Requesting extents, again, for fid %08lX-%08lX-%08lX-%08lX Offset %I64X Length %08lX\n",
1005 pFcb->ObjectInformation->FileId.Cell,
1006 pFcb->ObjectInformation->FileId.Volume,
1007 pFcb->ObjectInformation->FileId.Vnode,
1008 pFcb->ObjectInformation->FileId.Unique,
1009 StartingByte.QuadPart,
1012 ntStatus = AFSRequestExtentsAsync( pFcb,
1017 if (!NT_SUCCESS(ntStatus))
1020 AFSDbgLogMsg( AFS_SUBSYSTEM_IO_PROCESSING,
1021 AFS_TRACE_LEVEL_ERROR,
1022 "AFSNonCachedWrite (%p) Failed to request extents Status %08lX\n",
1026 try_return( ntStatus);
1029 KeQueryTickCount( &liLastRequestTime);
1033 AFSDbgLogMsg( AFS_SUBSYSTEM_EXTENT_PROCESSING,
1034 AFS_TRACE_LEVEL_VERBOSE,
1035 "AFSNonCachedWrite Waiting for extents for fid %08lX-%08lX-%08lX-%08lX Offset %I64X Length %08lX\n",
1036 pFcb->ObjectInformation->FileId.Cell,
1037 pFcb->ObjectInformation->FileId.Volume,
1038 pFcb->ObjectInformation->FileId.Vnode,
1039 pFcb->ObjectInformation->FileId.Unique,
1040 StartingByte.QuadPart,
1047 ntStatus = AFSWaitForExtentMapping ( pFcb, pCcb);
1049 if (!NT_SUCCESS(ntStatus))
1052 AFSDbgLogMsg( AFS_SUBSYSTEM_EXTENT_PROCESSING,
1053 AFS_TRACE_LEVEL_ERROR,
1054 "AFSNonCachedWrite Failed wait for extents for fid %08lX-%08lX-%08lX-%08lX Offset %I64X Length %08lX Status %08lX\n",
1055 pFcb->ObjectInformation->FileId.Cell,
1056 pFcb->ObjectInformation->FileId.Volume,
1057 pFcb->ObjectInformation->FileId.Vnode,
1058 pFcb->ObjectInformation->FileId.Unique,
1059 StartingByte.QuadPart,
1063 try_return( ntStatus);
1068 // As per the read path -
1071 AFSDbgLogMsg( AFS_SUBSYSTEM_EXTENT_PROCESSING,
1072 AFS_TRACE_LEVEL_VERBOSE,
1073 "AFSNonCachedWrite Extents located for fid %08lX-%08lX-%08lX-%08lX Offset %I64X Length %08lX\n",
1074 pFcb->ObjectInformation->FileId.Cell,
1075 pFcb->ObjectInformation->FileId.Volume,
1076 pFcb->ObjectInformation->FileId.Vnode,
1077 pFcb->ObjectInformation->FileId.Unique,
1078 StartingByte.QuadPart,
1081 ntStatus = AFSGetExtents( pFcb,
1088 if (!NT_SUCCESS(ntStatus))
1091 AFSDbgLogMsg( AFS_SUBSYSTEM_IO_PROCESSING,
1092 AFS_TRACE_LEVEL_ERROR,
1093 "AFSNonCachedWrite (%p) Failed to retrieve mapped extents Status %08lX\n",
1097 try_return( ntStatus );
1100 AFSDbgLogMsg( AFS_SUBSYSTEM_IO_PROCESSING,
1101 AFS_TRACE_LEVEL_VERBOSE,
1102 "AFSNonCachedWrite (%p) Successfully retrieved map extents count %d run count %d\n",
1107 if( BooleanFlagOn( AFSLibControlFlags, AFS_REDIR_LIB_FLAGS_NONPERSISTENT_CACHE))
1110 Irp->IoStatus.Information = ByteCount;
1114 // Setup the MD5 for each extent
1117 AFSSetupMD5Hash( pFcb,
1125 ntStatus = AFSProcessExtentRun( pSystemBuffer,
1131 if (!NT_SUCCESS(ntStatus))
1134 AFSDbgLogMsg( AFS_SUBSYSTEM_IO_PROCESSING,
1135 AFS_TRACE_LEVEL_ERROR,
1136 "AFSNonCachedWrite (%p) Failed to process extent run for non-persistent cache Status %08lX\n",
1141 try_return( ntStatus);
1145 // Retrieve the cache file object
1148 pCacheFileObject = AFSReferenceCacheFileObject();
1150 if( pCacheFileObject == NULL)
1153 ntStatus = STATUS_DEVICE_NOT_READY;
1155 AFSDbgLogMsg( AFS_SUBSYSTEM_EXTENT_PROCESSING,
1156 AFS_TRACE_LEVEL_ERROR,
1157 "AFSNonCachedWrite Failed to retrieve cache fileobject for fid %08lX-%08lX-%08lX-%08lX Offset %I64X Length %08lX Status %08lX\n",
1158 pFcb->ObjectInformation->FileId.Cell,
1159 pFcb->ObjectInformation->FileId.Volume,
1160 pFcb->ObjectInformation->FileId.Vnode,
1161 pFcb->ObjectInformation->FileId.Unique,
1162 StartingByte.QuadPart,
1166 try_return( ntStatus);
1169 if (runCount > AFS_MAX_STACK_IO_RUNS)
1172 pIoRuns = (AFSIoRun*) AFSExAllocatePoolWithTag( PagedPool,
1173 runCount * sizeof( AFSIoRun ),
1175 if (NULL == pIoRuns)
1178 AFSDbgLogMsg( AFS_SUBSYSTEM_IO_PROCESSING,
1179 AFS_TRACE_LEVEL_ERROR,
1180 "AFSNonCachedWrite (%p) Failed to allocate IO run block\n",
1183 try_return( ntStatus = STATUS_INSUFFICIENT_RESOURCES );
1192 RtlZeroMemory( pIoRuns, runCount * sizeof( AFSIoRun ));
1194 ntStatus = AFSSetupIoRun( IoGetRelatedDeviceObject( pCacheFileObject),
1203 if (!NT_SUCCESS(ntStatus))
1206 AFSDbgLogMsg( AFS_SUBSYSTEM_IO_PROCESSING,
1207 AFS_TRACE_LEVEL_ERROR,
1208 "AFSNonCachedWrite (%p) Failed to initialize IO run block Status %08lX\n",
1212 try_return( ntStatus );
1215 AFSReferenceActiveExtents( pStartExtent,
1218 bDerefExtents = TRUE;
1220 AFSDbgLogMsg( AFS_SUBSYSTEM_LOCK_PROCESSING,
1221 AFS_TRACE_LEVEL_VERBOSE,
1222 "AFSNonCachedWrite Releasing(2) Fcb extents lock %p SHARED %08lX\n",
1223 &pFcb->NPFcb->Specific.File.ExtentsResource,
1224 PsGetCurrentThread());
1226 AFSReleaseResource( &pFcb->NPFcb->Specific.File.ExtentsResource );
1229 pGatherIo = (AFSGatherIo*) AFSExAllocatePoolWithTag( NonPagedPool,
1230 sizeof( AFSGatherIo),
1233 if (NULL == pGatherIo)
1236 AFSDbgLogMsg( AFS_SUBSYSTEM_IO_PROCESSING,
1237 AFS_TRACE_LEVEL_ERROR,
1238 "AFSNonCachedWrite (%p) Failed to allocate IO gather block\n",
1241 AFSDbgLogMsg( AFS_SUBSYSTEM_LOCK_PROCESSING,
1242 AFS_TRACE_LEVEL_VERBOSE,
1243 "AFSNonCachedWrite Acquiring(1) Fcb extents lock %p SHARED %08lX\n",
1244 &pFcb->NPFcb->Specific.File.ExtentsResource,
1245 PsGetCurrentThread());
1247 AFSAcquireShared( &pFcb->NPFcb->Specific.File.ExtentsResource,
1251 AFSDereferenceActiveExtents( pStartExtent,
1254 try_return (ntStatus = STATUS_INSUFFICIENT_RESOURCES );
1257 RtlZeroMemory( pGatherIo, sizeof( AFSGatherIo ));
1260 // Initialize count to 1, that was we won't get an early
1261 // completion if the first irp completes before the second is
1264 pGatherIo->Count = 1;
1265 pGatherIo->Status = STATUS_SUCCESS;
1266 pGatherIo->MasterIrp = Irp;
1267 pGatherIo->Synchronous = TRUE;
1268 pGatherIo->CompleteMasterIrp = FALSE;
1270 bCompleteIrp = TRUE;
1272 if( pGatherIo->Synchronous)
1274 KeInitializeEvent( &pGatherIo->Event, NotificationEvent, FALSE );
1279 // Setup the MD5 for each extent
1282 AFSSetupMD5Hash( pFcb,
1291 // Pre-emptively set up the count
1294 Irp->IoStatus.Information = ByteCount;
1296 ntStatus = AFSQueueStartIos( pCacheFileObject,
1298 IRP_WRITE_OPERATION | IRP_SYNCHRONOUS_API,
1303 AFSDbgLogMsg( AFS_SUBSYSTEM_IO_PROCESSING,
1304 AFS_TRACE_LEVEL_VERBOSE,
1305 "AFSNonCachedWrite (%p) AFSStartIos completed Status %08lX\n",
1309 if( !NT_SUCCESS( ntStatus))
1312 AFSDbgLogMsg( AFS_SUBSYSTEM_LOCK_PROCESSING,
1313 AFS_TRACE_LEVEL_VERBOSE,
1314 "AFSNonCachedWrite Acquiring(2) Fcb extents lock %p SHARED %08lX\n",
1315 &pFcb->NPFcb->Specific.File.ExtentsResource,
1316 PsGetCurrentThread());
1318 AFSAcquireShared( &pFcb->NPFcb->Specific.File.ExtentsResource,
1322 AFSDereferenceActiveExtents( pStartExtent,
1325 try_return( ntStatus);
1329 // Wait for completion of All IOs we started.
1332 ntStatus = KeWaitForSingleObject( &pGatherIo->Event,
1338 if( NT_SUCCESS( ntStatus))
1341 ntStatus = pGatherIo->Status;
1344 if( !NT_SUCCESS( ntStatus))
1347 AFSDbgLogMsg( AFS_SUBSYSTEM_LOCK_PROCESSING,
1348 AFS_TRACE_LEVEL_VERBOSE,
1349 "AFSNonCachedWrite Acquiring(3) Fcb extents lock %p SHARED %08lX\n",
1350 &pFcb->NPFcb->Specific.File.ExtentsResource,
1351 PsGetCurrentThread());
1353 AFSAcquireShared( &pFcb->NPFcb->Specific.File.ExtentsResource,
1357 AFSDereferenceActiveExtents( pStartExtent,
1360 try_return( ntStatus);
1365 if( NT_SUCCESS( ntStatus) &&
1366 pStartExtent != NULL &&
1367 Irp->IoStatus.Information > 0)
1373 AFSAcquireShared( &pFcb->NPFcb->Specific.File.ExtentsResource,
1379 // Since this is dirty we can mark the extents dirty now.
1380 // AFSMarkDirty will dereference the extents. Do not call
1381 // AFSDereferenceActiveExtents() in this code path.
1393 // This was an uncached user write - tell the server to do
1394 // the flush when the worker thread next wakes up
1396 pFcb->Specific.File.LastServerFlush.QuadPart = 0;
1400 if( pCacheFileObject != NULL)
1402 AFSReleaseCacheFileObject( pCacheFileObject);
1405 AFSDbgLogMsg( AFS_SUBSYSTEM_IO_PROCESSING,
1406 AFS_TRACE_LEVEL_VERBOSE,
1407 "AFSNonCachedWrite (%p) Completed request Status %08lX\n",
1411 if (NT_SUCCESS(ntStatus) &&
1416 pFileObject->CurrentByteOffset.QuadPart = StartingByte.QuadPart + ByteCount;
1422 AFSDbgLogMsg( AFS_SUBSYSTEM_LOCK_PROCESSING,
1423 AFS_TRACE_LEVEL_VERBOSE,
1424 "AFSNonCachedWrite Releasing Fcb extents lock %p SHARED %08lX\n",
1425 &pFcb->NPFcb->Specific.File.ExtentsResource,
1426 PsGetCurrentThread());
1428 AFSReleaseResource( &pFcb->NPFcb->Specific.File.ExtentsResource );
1433 AFSExFreePoolWithTag(pGatherIo, AFS_GATHER_TAG);
1436 if( NULL != pIoRuns &&
1437 stIoRuns != pIoRuns)
1439 AFSExFreePoolWithTag(pIoRuns, AFS_IO_RUN_TAG);
1445 AFSDbgLogMsg( AFS_SUBSYSTEM_IO_PROCESSING,
1446 AFS_TRACE_LEVEL_VERBOSE,
1447 "AFSNonCachedWrite Completing Irp %p Status %08lX Info %08lX\n",
1450 Irp->IoStatus.Information);
1452 AFSCompleteRequest( Irp, ntStatus);
1461 AFSCachedWrite( IN PDEVICE_OBJECT DeviceObject,
1463 IN LARGE_INTEGER StartingByte,
1465 IN BOOLEAN ForceFlush)
1467 UNREFERENCED_PARAMETER(DeviceObject);
1468 PVOID pSystemBuffer = NULL;
1469 NTSTATUS ntStatus = STATUS_SUCCESS;
1470 IO_STATUS_BLOCK iosbFlush;
1471 IO_STACK_LOCATION *pIrpSp = IoGetCurrentIrpStackLocation( Irp);
1472 PFILE_OBJECT pFileObject = pIrpSp->FileObject;
1473 AFSFcb *pFcb = (AFSFcb *)pFileObject->FsContext;
1474 BOOLEAN bSynchronousFo = BooleanFlagOn( pFileObject->Flags, FO_SYNCHRONOUS_IO);
1475 ULONG ulCurrentIO = 0, ulTotalLen = ByteCount;
1476 PMDL pCurrentMdl = Irp->MdlAddress;
1477 LARGE_INTEGER liCurrentOffset;
1482 Irp->IoStatus.Information = 0;
1484 if( BooleanFlagOn( pIrpSp->MinorFunction, IRP_MN_MDL))
1490 CcPrepareMdlWrite( pFileObject,
1496 ntStatus = Irp->IoStatus.Status;
1498 __except( EXCEPTION_EXECUTE_HANDLER)
1500 ntStatus = GetExceptionCode();
1502 AFSDbgLogMsg( AFS_SUBSYSTEM_IO_PROCESSING,
1503 AFS_TRACE_LEVEL_ERROR,
1504 "AFSCachedWrite (%p) Exception thrown while preparing mdl write Status %08lX\n",
1509 if( !NT_SUCCESS( ntStatus))
1513 // Free up any potentially allocated mdl's
1516 CcMdlWriteComplete( pFileObject,
1520 Irp->MdlAddress = NULL;
1522 AFSDbgLogMsg( AFS_SUBSYSTEM_IO_PROCESSING,
1523 AFS_TRACE_LEVEL_ERROR,
1524 "AFSCachedWrite (%p) Failed to process MDL write Status %08lX\n",
1529 try_return( ntStatus);
1532 liCurrentOffset.QuadPart = StartingByte.QuadPart;
1534 while( ulTotalLen > 0)
1537 ntStatus = STATUS_SUCCESS;
1539 if( pCurrentMdl != NULL)
1542 pSystemBuffer = MmGetSystemAddressForMdlSafe( pCurrentMdl,
1543 NormalPagePriority);
1545 ulCurrentIO = MmGetMdlByteCount( pCurrentMdl);
1547 if( ulCurrentIO > ulTotalLen)
1549 ulCurrentIO = ulTotalLen;
1555 pSystemBuffer = AFSLockSystemBuffer( Irp,
1558 ulCurrentIO = ulTotalLen;
1561 if( pSystemBuffer == NULL)
1564 AFSDbgLogMsg( AFS_SUBSYSTEM_IO_PROCESSING,
1565 AFS_TRACE_LEVEL_ERROR,
1566 "AFSCachedWrite (%p) Failed to lock system buffer\n",
1569 try_return( ntStatus = STATUS_INSUFFICIENT_RESOURCES);
1575 if( !CcCopyWrite( pFileObject,
1582 // Failed to process request.
1585 AFSDbgLogMsg( AFS_SUBSYSTEM_FILE_PROCESSING,
1586 AFS_TRACE_LEVEL_ERROR,
1587 "AFSCachedWrite (%p) Failed to issue CcCopyWrite %wZ @ %0I64X Status %08lX\n",
1589 &pFileObject->FileName,
1590 liCurrentOffset.QuadPart,
1591 Irp->IoStatus.Status);
1593 try_return( ntStatus = STATUS_UNSUCCESSFUL);
1596 __except( EXCEPTION_EXECUTE_HANDLER)
1599 ntStatus = GetExceptionCode();
1601 AFSDbgLogMsg( AFS_SUBSYSTEM_IO_PROCESSING,
1602 AFS_TRACE_LEVEL_ERROR,
1603 "AFSCachedWrite (%p) CcCopyWrite Threw exception %wZ @ %0I64X Status %08lX\n",
1605 &pFileObject->FileName,
1606 liCurrentOffset.QuadPart,
1610 if( !NT_SUCCESS( ntStatus))
1612 try_return( ntStatus);
1616 BooleanFlagOn(pFileObject->Flags, (FO_NO_INTERMEDIATE_BUFFERING + FO_WRITE_THROUGH)))
1620 // We have detected a file we do a write through with.
1623 CcFlushCache(&pFcb->NPFcb->SectionObjectPointers,
1628 if( !NT_SUCCESS( iosbFlush.Status))
1631 AFSDbgLogMsg( AFS_SUBSYSTEM_IO_PROCESSING,
1632 AFS_TRACE_LEVEL_ERROR,
1633 "AFSCachedWrite (%p) CcFlushCache failure %wZ FID %08lX-%08lX-%08lX-%08lX Status 0x%08lX Bytes 0x%08lX\n",
1635 &pFileObject->FileName,
1636 pFcb->ObjectInformation->FileId.Cell,
1637 pFcb->ObjectInformation->FileId.Volume,
1638 pFcb->ObjectInformation->FileId.Vnode,
1639 pFcb->ObjectInformation->FileId.Unique,
1641 iosbFlush.Information);
1643 try_return( ntStatus = iosbFlush.Status);
1647 if( ulTotalLen <= ulCurrentIO)
1652 liCurrentOffset.QuadPart += ulCurrentIO;
1654 ulTotalLen -= ulCurrentIO;
1656 pCurrentMdl = pCurrentMdl->Next;
1661 if( NT_SUCCESS( ntStatus))
1664 Irp->IoStatus.Information = ByteCount;
1669 pFileObject->CurrentByteOffset.QuadPart = StartingByte.QuadPart + ByteCount;
1673 // If this extended the Vdl, then update it accordingly
1676 if( StartingByte.QuadPart + ByteCount > pFcb->Header.ValidDataLength.QuadPart)
1679 pFcb->Header.ValidDataLength.QuadPart = StartingByte.QuadPart + ByteCount;
1683 BooleanFlagOn(pFileObject->Flags, (FO_NO_INTERMEDIATE_BUFFERING + FO_WRITE_THROUGH)))
1686 // Write through asked for... Set things so that we get
1687 // flush when the worker thread next wakes up
1689 pFcb->Specific.File.LastServerFlush.QuadPart = 0;
1692 if( !BooleanFlagOn( pFcb->Flags, AFS_FCB_FLAG_UPDATE_LAST_WRITE_TIME))
1695 SetFlag( pFcb->Flags, AFS_FCB_FLAG_UPDATE_WRITE_TIME);
1697 KeQuerySystemTime( &pFcb->ObjectInformation->LastWriteTime);
1701 AFSCompleteRequest( Irp,
1710 AFSExtendingWrite( IN AFSFcb *Fcb,
1711 IN PFILE_OBJECT FileObject,
1712 IN LONGLONG NewLength)
1714 LARGE_INTEGER liSaveFileSize = Fcb->Header.FileSize;
1715 LARGE_INTEGER liSaveAllocation = Fcb->Header.AllocationSize;
1716 NTSTATUS ntStatus = STATUS_SUCCESS;
1717 AFSCcb *pCcb = (AFSCcb *)FileObject->FsContext2;
1719 if( NewLength > Fcb->Header.AllocationSize.QuadPart)
1722 Fcb->Header.AllocationSize.QuadPart = NewLength;
1724 Fcb->ObjectInformation->AllocationSize = Fcb->Header.AllocationSize;
1727 if( NewLength > Fcb->Header.FileSize.QuadPart)
1730 Fcb->Header.FileSize.QuadPart = NewLength;
1732 Fcb->ObjectInformation->EndOfFile = Fcb->Header.FileSize;
1739 ntStatus = AFSUpdateFileInformation( &Fcb->ObjectInformation->ParentObjectInformation->FileId,
1740 Fcb->ObjectInformation,
1743 if (NT_SUCCESS(ntStatus))
1746 KeQuerySystemTime( &Fcb->ObjectInformation->ChangeTime);
1748 SetFlag( Fcb->Flags, AFS_FCB_FLAG_FILE_MODIFIED | AFS_FCB_FLAG_UPDATE_CHANGE_TIME);
1751 // If the file is currently cached, then let the MM know about the extension
1754 if( CcIsFileCached( FileObject))
1756 CcSetFileSizes( FileObject,
1757 (PCC_FILE_SIZES)&Fcb->Header.AllocationSize);
1762 Fcb->Header.FileSize = liSaveFileSize;
1763 Fcb->Header.AllocationSize = liSaveAllocation;
1767 // DownConvert file resource to shared
1769 ExConvertExclusiveToSharedLite( &Fcb->NPFcb->Resource);
1775 AFSShareWrite( IN PDEVICE_OBJECT DeviceObject,
1779 UNREFERENCED_PARAMETER(DeviceObject);
1780 NTSTATUS ntStatus = STATUS_SUCCESS;
1781 PIO_STACK_LOCATION pIrpSp = IoGetCurrentIrpStackLocation( Irp);
1782 AFSFcb *pFcb = NULL;
1783 AFSCcb *pCcb = NULL;
1784 AFSPipeIORequestCB *pIoRequest = NULL;
1785 void *pBuffer = NULL;
1786 AFSPipeIOResultCB stIoResult;
1787 ULONG ulBytesReturned = 0;
1792 pFcb = (AFSFcb *)pIrpSp->FileObject->FsContext;
1794 pCcb = (AFSCcb *)pIrpSp->FileObject->FsContext2;
1796 AFSDbgLogMsg( AFS_SUBSYSTEM_PIPE_PROCESSING,
1797 AFS_TRACE_LEVEL_VERBOSE,
1798 "AFSShareWrite On pipe %wZ Length %08lX\n",
1799 &pCcb->DirectoryCB->NameInformation.FileName,
1800 pIrpSp->Parameters.Write.Length);
1802 if( pIrpSp->Parameters.Write.Length == 0)
1806 // Nothing to do in this case
1809 try_return( ntStatus);
1813 // Retrieve the buffer for the read request
1816 pBuffer = AFSLockSystemBuffer( Irp,
1817 pIrpSp->Parameters.Write.Length);
1819 if( pBuffer == NULL)
1822 AFSDbgLogMsg( AFS_SUBSYSTEM_PIPE_PROCESSING,
1823 AFS_TRACE_LEVEL_ERROR,
1824 "AFSShareWrite Failed to map buffer on pipe %wZ\n",
1825 &pCcb->DirectoryCB->NameInformation.FileName);
1827 try_return( ntStatus = STATUS_INSUFFICIENT_RESOURCES);
1830 AFSAcquireShared( &pFcb->NPFcb->Resource,
1833 pIoRequest = (AFSPipeIORequestCB *)AFSExAllocatePoolWithTag( PagedPool,
1834 sizeof( AFSPipeIORequestCB) +
1835 pIrpSp->Parameters.Write.Length,
1836 AFS_GENERIC_MEMORY_14_TAG);
1838 if( pIoRequest == NULL)
1841 try_return( ntStatus = STATUS_INSUFFICIENT_RESOURCES);
1844 RtlZeroMemory( pIoRequest,
1845 sizeof( AFSPipeIORequestCB) + pIrpSp->Parameters.Write.Length);
1847 pIoRequest->RequestId = pCcb->RequestID;
1849 pIoRequest->RootId = pFcb->ObjectInformation->VolumeCB->ObjectInformation.FileId;
1851 pIoRequest->BufferLength = pIrpSp->Parameters.Write.Length;
1853 RtlCopyMemory( (void *)((char *)pIoRequest + sizeof( AFSPipeIORequestCB)),
1855 pIrpSp->Parameters.Write.Length);
1857 stIoResult.BytesProcessed = 0;
1859 ulBytesReturned = sizeof( AFSPipeIOResultCB);
1862 // Issue the open request to the service
1865 ntStatus = AFSProcessRequest( AFS_REQUEST_TYPE_PIPE_WRITE,
1866 AFS_REQUEST_FLAG_SYNCHRONOUS,
1868 &pCcb->DirectoryCB->NameInformation.FileName,
1871 sizeof( AFSPipeIORequestCB) +
1872 pIrpSp->Parameters.Write.Length,
1876 if( !NT_SUCCESS( ntStatus))
1879 AFSDbgLogMsg( AFS_SUBSYSTEM_FILE_PROCESSING,
1880 AFS_TRACE_LEVEL_ERROR,
1881 "AFSShareWrite (%p) Failed service write Status %08lX\n",
1885 try_return( ntStatus);
1888 AFSDbgLogMsg( AFS_SUBSYSTEM_PIPE_PROCESSING,
1889 AFS_TRACE_LEVEL_VERBOSE,
1890 "AFSShareWrite Completed on pipe %wZ Length read %08lX\n",
1891 &pCcb->DirectoryCB->NameInformation.FileName,
1892 stIoResult.BytesProcessed);
1894 Irp->IoStatus.Information = stIoResult.BytesProcessed;
1901 AFSReleaseResource( &pFcb->NPFcb->Resource);
1904 if( pIoRequest != NULL)
1907 AFSExFreePoolWithTag( pIoRequest, AFS_GENERIC_MEMORY_14_TAG);