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 NTSTATUS ntStatus = STATUS_SUCCESS;
82 ntStatus = AFSCommonWrite( AFSRDRDeviceObject, Irp, NULL);
84 __except( AFSExceptionFilter( GetExceptionCode(), GetExceptionInformation()) )
87 ntStatus = STATUS_INSUFFICIENT_RESOURCES;
94 AFSCommonWrite( IN PDEVICE_OBJECT DeviceObject,
99 NTSTATUS ntStatus = STATUS_SUCCESS;
100 AFSDeviceExt *pDeviceExt = (AFSDeviceExt *)DeviceObject->DeviceExtension;
101 IO_STACK_LOCATION *pIrpSp;
104 AFSNonPagedFcb *pNPFcb = NULL;
105 ULONG ulByteCount = 0;
106 LARGE_INTEGER liStartingByte;
107 PFILE_OBJECT pFileObject;
108 BOOLEAN bPagingIo = FALSE;
109 BOOLEAN bNonCachedIo = FALSE;
110 BOOLEAN bReleaseMain = FALSE;
111 BOOLEAN bReleasePaging = FALSE;
112 BOOLEAN bExtendingWrite = FALSE;
113 BOOLEAN bCompleteIrp = TRUE;
114 BOOLEAN bForceFlush = FALSE;
116 BOOLEAN bMapped = TRUE;
117 HANDLE hCallingUser = OnBehalfOf;
118 ULONG ulExtensionLength = 0;
119 BOOLEAN bRetry = FALSE;
120 ULONGLONG ullProcessId = (ULONGLONG)PsGetCurrentProcessId();
122 pIrpSp = IoGetCurrentIrpStackLocation( Irp);
127 pFileObject = pIrpSp->FileObject;
130 // Extract the fileobject references
133 pFcb = (AFSFcb *)pFileObject->FsContext;
134 pCcb = (AFSCcb *)pFileObject->FsContext2;
136 ObReferenceObject( pFileObject);
141 AFSDbgLogMsg( AFS_SUBSYSTEM_IO_PROCESSING,
142 AFS_TRACE_LEVEL_ERROR,
143 "AFSCommonWrite Attempted write (%08lX) when pFcb == NULL\n",
146 try_return( ntStatus = STATUS_INVALID_DEVICE_REQUEST);
149 pNPFcb = pFcb->NPFcb;
152 // If we are in shutdown mode then fail the request
155 if( BooleanFlagOn( pDeviceExt->DeviceFlags, AFS_DEVICE_FLAG_REDIRECTOR_SHUTDOWN))
158 AFSDbgLogMsg( AFS_SUBSYSTEM_FILE_PROCESSING,
159 AFS_TRACE_LEVEL_WARNING,
160 "AFSCommonWrite (%08lX) Open request after shutdown\n",
163 try_return( ntStatus = STATUS_TOO_LATE);
166 liStartingByte = pIrpSp->Parameters.Write.ByteOffset;
167 bPagingIo = BooleanFlagOn( Irp->Flags, IRP_PAGING_IO);
168 bNonCachedIo = BooleanFlagOn( Irp->Flags, IRP_NOCACHE);
169 ulByteCount = pIrpSp->Parameters.Write.Length;
171 if( pFcb->Header.NodeTypeCode != AFS_IOCTL_FCB &&
172 pFcb->Header.NodeTypeCode != AFS_FILE_FCB &&
173 pFcb->Header.NodeTypeCode != AFS_SPECIAL_SHARE_FCB)
176 AFSDbgLogMsg( AFS_SUBSYSTEM_IO_PROCESSING,
177 AFS_TRACE_LEVEL_ERROR,
178 "AFSCommonWrite Attempted write (%08lX) on an invalid node type %08lX\n",
180 pFcb->Header.NodeTypeCode);
182 try_return( ntStatus = STATUS_INVALID_DEVICE_REQUEST);
186 // If this is a write against an IOCtl node then handle it
187 // in a different pathway
190 if( pFcb->Header.NodeTypeCode == AFS_IOCTL_FCB)
193 AFSDbgLogMsg( AFS_SUBSYSTEM_IO_PROCESSING,
194 AFS_TRACE_LEVEL_VERBOSE,
195 "AFSCommonWrite (%08lX) Processing file (PIOCTL) Offset %I64X Length %08lX Irp Flags %08lX\n",
197 liStartingByte.QuadPart,
201 ntStatus = AFSIOCtlWrite( DeviceObject,
204 try_return( ntStatus);
206 else if( pFcb->Header.NodeTypeCode == AFS_SPECIAL_SHARE_FCB)
209 AFSDbgLogMsg( AFS_SUBSYSTEM_IO_PROCESSING,
210 AFS_TRACE_LEVEL_VERBOSE,
211 "AFSCommonWrite (%08lX) Processing file (SHARE) Offset %I64X Length %08lX Irp Flags %08lX\n",
213 liStartingByte.QuadPart,
217 ntStatus = AFSShareWrite( DeviceObject,
220 try_return( ntStatus);
224 // Is the Cache not there yet? Exit.
226 if( !BooleanFlagOn( AFSLibControlFlags, AFS_REDIR_LIB_FLAGS_NONPERSISTENT_CACHE) &&
227 NULL == pDeviceExt->Specific.RDR.CacheFileObject)
230 AFSDbgLogMsg( AFS_SUBSYSTEM_IO_PROCESSING,
231 AFS_TRACE_LEVEL_ERROR,
232 "AFSCommonWrite (%08lX) Request failed due to AFS cache closed\n",
235 try_return( ntStatus = STATUS_TOO_LATE );
238 if( pFcb->ObjectInformation->VolumeCB != NULL &&
239 BooleanFlagOn( pFcb->ObjectInformation->VolumeCB->VolumeInformation.Characteristics, FILE_READ_ONLY_DEVICE))
242 AFSDbgLogMsg( AFS_SUBSYSTEM_IO_PROCESSING,
243 AFS_TRACE_LEVEL_ERROR,
244 "AFSCommonWrite (%08lX) Request failed due to read only volume\n",
247 try_return( ntStatus = STATUS_ACCESS_DENIED);
251 // We need to know on whose behalf we have been called (which
252 // we will eventually tell to the server - for non paging
253 // writes). If we were posted then we were told. If this is
254 // the first time we saw the irp then we grab it now.
256 if( NULL == OnBehalfOf )
259 hCallingUser = PsGetCurrentProcessId();
264 hCallingUser = OnBehalfOf;
268 // Check for zero length write
271 if( ulByteCount == 0)
274 AFSDbgLogMsg( AFS_SUBSYSTEM_IO_PROCESSING,
275 AFS_TRACE_LEVEL_VERBOSE,
276 "AFSCommonWrite (%08lX) Request completed due to zero length\n",
279 try_return( ntStatus);
283 // Is this Fcb valid???
286 if( BooleanFlagOn( pFcb->ObjectInformation->Flags, AFS_OBJECT_FLAGS_OBJECT_INVALID))
289 AFSDbgLogMsg( AFS_SUBSYSTEM_IO_PROCESSING,
290 AFS_TRACE_LEVEL_ERROR,
291 "AFSCommonWrite (%08lX) Failing request due to INVALID fcb\n",
294 Irp->IoStatus.Information = 0;
296 try_return( ntStatus = STATUS_FILE_DELETED);
299 if( BooleanFlagOn( pCcb->DirectoryCB->Flags, AFS_DIR_ENTRY_DELETED) ||
300 BooleanFlagOn( pFcb->ObjectInformation->Flags, AFS_OBJECT_FLAGS_DELETED))
303 AFSDbgLogMsg( AFS_SUBSYSTEM_IO_PROCESSING,
304 AFS_TRACE_LEVEL_ERROR,
305 "AFSCommonWrite (%08lX) Request failed due to file deleted\n",
308 try_return( ntStatus = STATUS_FILE_DELETED);
311 if( FlagOn( pIrpSp->MinorFunction, IRP_MN_COMPLETE))
314 AFSDbgLogMsg( AFS_SUBSYSTEM_IO_PROCESSING,
315 AFS_TRACE_LEVEL_VERBOSE,
316 "AFSCommonWrite (%08lX) IRP_MN_COMPLETE being processed\n",
319 CcMdlWriteComplete(pFileObject, &pIrpSp->Parameters.Write.ByteOffset, Irp->MdlAddress);
322 // Mdl is now Deallocated
325 Irp->MdlAddress = NULL;
327 try_return( ntStatus = STATUS_SUCCESS );
331 // If we get a non cached IO for a cached file we should do a purge.
332 // For now we will just promote to cached
334 if( NULL != pFileObject->SectionObjectPointer->DataSectionObject && !bPagingIo && bNonCachedIo)
336 bNonCachedIo = FALSE;
340 if( (!bPagingIo && !bNonCachedIo))
343 if( pFileObject->PrivateCacheMap == NULL)
349 AFSDbgLogMsg( AFS_SUBSYSTEM_IO_PROCESSING,
350 AFS_TRACE_LEVEL_VERBOSE,
351 "AFSCommonWrite Initialize caching on Fcb %08lX FileObject %08lX\n",
355 CcInitializeCacheMap( pFileObject,
356 (PCC_FILE_SIZES)&pFcb->Header.AllocationSize,
358 AFSLibCacheManagerCallbacks,
361 CcSetReadAheadGranularity( pFileObject,
362 READ_AHEAD_GRANULARITY);
365 __except( EXCEPTION_EXECUTE_HANDLER)
368 ntStatus = GetExceptionCode();
370 AFSDbgLogMsg( AFS_SUBSYSTEM_IO_PROCESSING,
371 AFS_TRACE_LEVEL_ERROR,
372 "AFSCommonWrite (%08lX) Exception thrown while initializing cache map Status %08lX\n",
377 if( !NT_SUCCESS( ntStatus))
380 try_return( ntStatus);
385 while (!bNonCachedIo && !CcCanIWrite( pFileObject,
390 static const LONGLONG llWriteDelay = (LONGLONG)-100000;
392 KeDelayExecutionThread(KernelMode, FALSE, (PLARGE_INTEGER)&llWriteDelay);
396 // Save off the PID if this is not a paging IO
400 ( pFcb->Specific.File.ExtentRequestProcessId == 0 ||
401 ( ullProcessId != (ULONGLONG)AFSSysProcess &&
402 pFcb->Specific.File.ExtentRequestProcessId != ullProcessId)))
405 pFcb->Specific.File.ExtentRequestProcessId = ullProcessId;
407 if( ullProcessId == (ULONGLONG)AFSSysProcess)
409 AFSDbgLogMsg( AFS_SUBSYSTEM_EXTENT_PROCESSING,
410 AFS_TRACE_LEVEL_WARNING,
411 "%s Setting LastWriterExtentProcessId to system process for Fcb %p\n",
418 // We should be ready to go. So first of all ask for the extents
419 // Provoke a get of the extents - if we need to.
423 if( !bPagingIo && !bNonCachedIo)
426 ntStatus = AFSRequestExtentsAsync( pFcb, pCcb, &liStartingByte, ulByteCount);
428 if (!NT_SUCCESS(ntStatus))
431 AFSDbgLogMsg( AFS_SUBSYSTEM_IO_PROCESSING,
432 AFS_TRACE_LEVEL_ERROR,
433 "AFSCommonWrite (%08lX) Failed to request extents Status %08lX\n",
437 try_return( ntStatus );
443 // If they are not mapped and we are the Lazy Writer then just
446 if (!bMapped && pFcb->Specific.File.LazyWriterThread == PsGetCurrentThread())
449 AFSDbgLogMsg( AFS_SUBSYSTEM_IO_PROCESSING,
450 AFS_TRACE_LEVEL_VERBOSE,
451 "AFSCommonWrite (%08lX) Failing lazy writer for unmapped request\n",
454 try_return ( ntStatus = STATUS_FILE_LOCK_CONFLICT);
460 // - if Paging then we need to nothing (the precalls will
461 // have acquired the paging resource), for clarity we will collect
462 // the paging resource
463 // - If extending Write then take the fileresource EX (EOF will change, Allocation will only move out)
464 // - Otherwise we collect the file shared, check against extending and
475 bExtendingWrite = (((liStartingByte.QuadPart + ulByteCount) >=
476 pFcb->Header.FileSize.QuadPart) ||
477 (liStartingByte.LowPart == FILE_WRITE_TO_END_OF_FILE &&
478 liStartingByte.HighPart == -1)) ;
484 //ASSERT( NULL != OnBehalfOf || ExIsResourceAcquiredLite( &pNPFcb->Resource ));
486 AFSDbgLogMsg( AFS_SUBSYSTEM_LOCK_PROCESSING,
487 AFS_TRACE_LEVEL_VERBOSE,
488 "AFSCommonWrite Acquiring Fcb PagingIo lock %08lX SHARED %08lX\n",
489 &pNPFcb->PagingResource,
490 PsGetCurrentThread());
492 AFSAcquireShared( &pNPFcb->PagingResource,
495 bReleasePaging = TRUE;
498 // We have the correct lock - we cannot have the wrong one
502 else if( bExtendingWrite)
505 // Check for lock inversion
508 ASSERT( !ExIsResourceAcquiredLite( &pNPFcb->PagingResource ));
510 AFSDbgLogMsg( AFS_SUBSYSTEM_LOCK_PROCESSING,
511 AFS_TRACE_LEVEL_VERBOSE,
512 "AFSCommonWrite Acquiring Fcb lock %08lX EXCL %08lX\n",
514 PsGetCurrentThread());
516 AFSAcquireExcl( &pNPFcb->Resource,
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;
534 // We have the correct lock - even if we don't end up truncating
540 ASSERT( !ExIsResourceAcquiredLite( &pNPFcb->PagingResource ));
542 AFSDbgLogMsg( AFS_SUBSYSTEM_LOCK_PROCESSING,
543 AFS_TRACE_LEVEL_VERBOSE,
544 "AFSCommonWrite Acquiring Fcb lock %08lX SHARED %08lX\n",
546 PsGetCurrentThread());
548 AFSAcquireShared( &pNPFcb->Resource,
554 // Have things moved? Are we extending? If so, the the lock isn't OK
556 bLockOK = (liStartingByte.QuadPart + ulByteCount) < pFcb->Header.FileSize.QuadPart;
560 AFSReleaseResource( &pNPFcb->Resource);
561 bReleaseMain = FALSE;
568 // Check the BR locks on the file.
572 !FsRtlCheckLockForWriteAccess( &pFcb->Specific.File.FileLock,
576 AFSDbgLogMsg( AFS_SUBSYSTEM_IO_PROCESSING,
577 AFS_TRACE_LEVEL_ERROR,
578 "AFSCommonWrite (%08lX) Request failed due to lock conflict\n",
581 try_return( ntStatus = STATUS_FILE_LOCK_CONFLICT);
587 ntStatus = AFSExtendingWrite( pFcb, pFileObject, (liStartingByte.QuadPart + ulByteCount));
589 if( !NT_SUCCESS(ntStatus))
592 AFSDbgLogMsg( AFS_SUBSYSTEM_IO_PROCESSING,
593 AFS_TRACE_LEVEL_ERROR,
594 "AFSCommonWrite (%08lX) Failed extending write request Status %08lX\n",
598 try_return( ntStatus );
603 // Fire off the request as appropriate
605 bCompleteIrp = FALSE;
611 AFSDbgLogMsg( AFS_SUBSYSTEM_IO_PROCESSING,
612 AFS_TRACE_LEVEL_VERBOSE,
613 "AFSCommonWrite (%08lX) Processing CACHED request Offset %I64X Len %08lX\n",
615 liStartingByte.QuadPart,
618 ntStatus = AFSCachedWrite( DeviceObject, Irp, liStartingByte, ulByteCount, bForceFlush);
624 AFSDbgLogMsg( AFS_SUBSYSTEM_IO_PROCESSING,
625 AFS_TRACE_LEVEL_VERBOSE,
626 "AFSCommonWrite (%08lX) Processing NON-CACHED request Offset %I64X Len %08lX\n",
628 liStartingByte.QuadPart,
631 ntStatus = AFSNonCachedWrite( DeviceObject, Irp, liStartingByte, ulByteCount);
636 AFSDbgLogMsg( AFS_SUBSYSTEM_IO_PROCESSING,
637 AFS_TRACE_LEVEL_VERBOSE,
638 "AFSCommonWrite (%08lX) Process complete Status %08lX\n",
642 ObDereferenceObject(pFileObject);
647 AFSReleaseResource( &pNPFcb->Resource);
653 AFSReleaseResource( &pNPFcb->PagingResource);
659 AFSCompleteRequest( Irp,
668 AFSIOCtlWrite( IN PDEVICE_OBJECT DeviceObject,
672 NTSTATUS ntStatus = STATUS_SUCCESS;
673 AFSPIOCtlIORequestCB stIORequestCB;
674 PIO_STACK_LOCATION pIrpSp = IoGetCurrentIrpStackLocation( Irp);
677 AFSPIOCtlIOResultCB stIOResultCB;
678 ULONG ulBytesReturned = 0;
679 AFSFileID stParentFID;
684 RtlZeroMemory( &stIORequestCB,
685 sizeof( AFSPIOCtlIORequestCB));
687 if( pIrpSp->Parameters.Write.Length == 0)
691 // Nothing to do in this case
694 try_return( ntStatus);
697 pFcb = (AFSFcb *)pIrpSp->FileObject->FsContext;
699 pCcb = (AFSCcb *)pIrpSp->FileObject->FsContext2;
701 AFSDbgLogMsg( AFS_SUBSYSTEM_LOCK_PROCESSING,
702 AFS_TRACE_LEVEL_VERBOSE,
703 "AFSIOCtlWrite Acquiring Fcb lock %08lX SHARED %08lX\n",
704 &pFcb->NPFcb->Resource,
705 PsGetCurrentThread());
707 AFSAcquireShared( &pFcb->NPFcb->Resource,
711 // Get the parent fid to pass to the cm
714 RtlZeroMemory( &stParentFID,
717 if( pFcb->ObjectInformation->ParentObjectInformation != NULL)
721 // The parent directory FID of the node
724 stParentFID = pFcb->ObjectInformation->ParentObjectInformation->FileId;
728 // Set the control block up
731 stIORequestCB.RequestId = pCcb->RequestID;
733 if( pFcb->ObjectInformation->VolumeCB != NULL)
735 stIORequestCB.RootId = pFcb->ObjectInformation->VolumeCB->ObjectInformation.FileId;
739 // Lock down the buffer
742 stIORequestCB.MappedBuffer = AFSMapToService( Irp,
743 pIrpSp->Parameters.Write.Length);
745 if( stIORequestCB.MappedBuffer == NULL)
748 try_return( ntStatus = STATUS_INSUFFICIENT_RESOURCES);
751 stIORequestCB.BufferLength = pIrpSp->Parameters.Write.Length;
753 stIOResultCB.BytesProcessed = 0;
755 ulBytesReturned = sizeof( AFSPIOCtlIOResultCB);
758 // Issue the request to the service
761 ntStatus = AFSProcessRequest( AFS_REQUEST_TYPE_PIOCTL_WRITE,
762 AFS_REQUEST_FLAG_SYNCHRONOUS,
766 (void *)&stIORequestCB,
767 sizeof( AFSPIOCtlIORequestCB),
771 if( !NT_SUCCESS( ntStatus))
774 try_return( ntStatus);
778 // Update the length written
781 Irp->IoStatus.Information = stIOResultCB.BytesProcessed;
785 if( stIORequestCB.MappedBuffer != NULL)
788 AFSUnmapServiceMappedBuffer( stIORequestCB.MappedBuffer,
795 AFSReleaseResource( &pFcb->NPFcb->Resource);
803 // This function is called when we know we have to read from the AFS Cache.
805 // It ensures that we have exents for the entirety of the write and
806 // then pins the extents into memory (meaning that although we may
807 // add we will not remove). Then it creates a scatter gather write
808 // and fires off the IRPs
812 AFSNonCachedWrite( IN PDEVICE_OBJECT DeviceObject,
814 IN LARGE_INTEGER StartingByte,
817 NTSTATUS ntStatus = STATUS_UNSUCCESSFUL;
818 VOID *pSystemBuffer = NULL;
819 BOOLEAN bPagingIo = BooleanFlagOn( Irp->Flags, IRP_PAGING_IO);
820 BOOLEAN bLocked = FALSE;
821 BOOLEAN bCompleteIrp = TRUE;
822 BOOLEAN bExtentsMapped = FALSE;
823 AFSGatherIo *pGatherIo = NULL;
824 AFSIoRun *pIoRuns = NULL;
825 AFSIoRun stIoRuns[AFS_MAX_STACK_IO_RUNS];
826 ULONG extentsCount = 0, runCount = 0;
827 AFSExtent *pStartExtent = NULL;
828 AFSExtent *pIgnoreExtent = NULL;
829 IO_STACK_LOCATION *pIrpSp = IoGetCurrentIrpStackLocation( Irp);
830 PFILE_OBJECT pFileObject = pIrpSp->FileObject;
831 AFSFcb *pFcb = (AFSFcb *)pFileObject->FsContext;
832 AFSCcb *pCcb = (AFSCcb *)pFileObject->FsContext2;
833 BOOLEAN bSynchronousFo = BooleanFlagOn( pFileObject->Flags, FO_SYNCHRONOUS_IO);
834 AFSDeviceExt *pDevExt = (AFSDeviceExt *)DeviceObject->DeviceExtension;
835 ULONG ulRequestCount = 0;
836 LARGE_INTEGER liCurrentTime, liLastRequestTime;
837 AFSDeviceExt *pControlDevExt = (AFSDeviceExt *)AFSControlDeviceObject->DeviceExtension;
838 PFILE_OBJECT pCacheFileObject = NULL;
839 BOOLEAN bDerefExtents = FALSE;
843 Irp->IoStatus.Information = 0;
845 if (ByteCount > pDevExt->Specific.RDR.MaxIo.QuadPart)
848 AFSDbgLogMsg( AFS_SUBSYSTEM_IO_PROCESSING,
849 AFS_TRACE_LEVEL_ERROR,
850 "AFSNonCachedWrite (%08lX) Request %08lX Actual %08lX larger than MaxIO %I64X\n",
853 pIrpSp->Parameters.Write.Length,
854 pDevExt->Specific.RDR.MaxIo.QuadPart);
856 try_return( ntStatus = STATUS_UNSUCCESSFUL);
860 // Get the mapping for the buffer
862 pSystemBuffer = AFSLockSystemBuffer( Irp,
865 if( pSystemBuffer == NULL)
868 AFSDbgLogMsg( AFS_SUBSYSTEM_IO_PROCESSING,
869 AFS_TRACE_LEVEL_ERROR,
870 "AFSNonCachedWrite (%08lX) Failed to map system buffer\n",
873 try_return( ntStatus = STATUS_INSUFFICIENT_RESOURCES);
878 // Provoke a get of the extents - if we need to.
881 AFSDbgLogMsg( AFS_SUBSYSTEM_EXTENT_PROCESSING,
882 AFS_TRACE_LEVEL_VERBOSE,
883 "AFSNonCachedWrite Requesting extents for fid %08lX-%08lX-%08lX-%08lX Offset %I64X Length %08lX\n",
884 pFcb->ObjectInformation->FileId.Cell,
885 pFcb->ObjectInformation->FileId.Volume,
886 pFcb->ObjectInformation->FileId.Vnode,
887 pFcb->ObjectInformation->FileId.Unique,
888 StartingByte.QuadPart,
891 ntStatus = AFSRequestExtentsAsync( pFcb,
896 if (!NT_SUCCESS(ntStatus))
899 AFSDbgLogMsg( AFS_SUBSYSTEM_IO_PROCESSING,
900 AFS_TRACE_LEVEL_ERROR,
901 "AFSNonCachedWrite (%08lX) Failed to request extents Status %08lX\n",
905 try_return( ntStatus);
908 KeQueryTickCount( &liLastRequestTime);
913 AFSDbgLogMsg( AFS_SUBSYSTEM_LOCK_PROCESSING,
914 AFS_TRACE_LEVEL_VERBOSE,
915 "AFSNonCachedWrite Acquiring Fcb extents lock %08lX SHARED %08lX\n",
916 &pFcb->NPFcb->Specific.File.ExtentsResource,
917 PsGetCurrentThread());
919 ASSERT( !ExIsResourceAcquiredLite( &pFcb->NPFcb->Specific.File.ExtentsResource ));
921 AFSAcquireShared( &pFcb->NPFcb->Specific.File.ExtentsResource, TRUE );
925 pIgnoreExtent = NULL;
927 if ( AFSDoExtentsMapRegion( pFcb, &StartingByte, ByteCount, &pStartExtent, &pIgnoreExtent ))
932 KeClearEvent( &pFcb->NPFcb->Specific.File.ExtentsRequestComplete );
934 AFSDbgLogMsg( AFS_SUBSYSTEM_LOCK_PROCESSING,
935 AFS_TRACE_LEVEL_VERBOSE,
936 "AFSNonCachedWrite Releasing(1) Fcb extents lock %08lX SHARED %08lX\n",
937 &pFcb->NPFcb->Specific.File.ExtentsResource,
938 PsGetCurrentThread());
940 AFSReleaseResource( &pFcb->NPFcb->Specific.File.ExtentsResource );
944 // We will re-request the extents after waiting for them
947 KeQueryTickCount( &liCurrentTime);
949 if( liCurrentTime.QuadPart - liLastRequestTime.QuadPart >= pControlDevExt->Specific.Control.ExtentRequestTimeCount.QuadPart)
952 AFSDbgLogMsg( AFS_SUBSYSTEM_EXTENT_PROCESSING,
953 AFS_TRACE_LEVEL_VERBOSE,
954 "AFSNonCachedWrite Requesting extents, again, for fid %08lX-%08lX-%08lX-%08lX Offset %I64X Length %08lX\n",
955 pFcb->ObjectInformation->FileId.Cell,
956 pFcb->ObjectInformation->FileId.Volume,
957 pFcb->ObjectInformation->FileId.Vnode,
958 pFcb->ObjectInformation->FileId.Unique,
959 StartingByte.QuadPart,
962 ntStatus = AFSRequestExtentsAsync( pFcb,
967 if (!NT_SUCCESS(ntStatus))
970 AFSDbgLogMsg( AFS_SUBSYSTEM_IO_PROCESSING,
971 AFS_TRACE_LEVEL_ERROR,
972 "AFSNonCachedWrite (%08lX) Failed to request extents Status %08lX\n",
976 try_return( ntStatus);
979 KeQueryTickCount( &liLastRequestTime);
983 AFSDbgLogMsg( AFS_SUBSYSTEM_EXTENT_PROCESSING,
984 AFS_TRACE_LEVEL_VERBOSE,
985 "AFSNonCachedWrite Waiting for extents for fid %08lX-%08lX-%08lX-%08lX Offset %I64X Length %08lX\n",
986 pFcb->ObjectInformation->FileId.Cell,
987 pFcb->ObjectInformation->FileId.Volume,
988 pFcb->ObjectInformation->FileId.Vnode,
989 pFcb->ObjectInformation->FileId.Unique,
990 StartingByte.QuadPart,
997 ntStatus = AFSWaitForExtentMapping ( pFcb, pCcb);
999 if (!NT_SUCCESS(ntStatus))
1002 AFSDbgLogMsg( AFS_SUBSYSTEM_EXTENT_PROCESSING,
1003 AFS_TRACE_LEVEL_ERROR,
1004 "AFSNonCachedWrite Failed wait for extents for fid %08lX-%08lX-%08lX-%08lX Offset %I64X Length %08lX Status %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,
1013 try_return( ntStatus);
1018 // As per the read path -
1021 AFSDbgLogMsg( AFS_SUBSYSTEM_EXTENT_PROCESSING,
1022 AFS_TRACE_LEVEL_VERBOSE,
1023 "AFSNonCachedWrite Extents located for fid %08lX-%08lX-%08lX-%08lX Offset %I64X Length %08lX\n",
1024 pFcb->ObjectInformation->FileId.Cell,
1025 pFcb->ObjectInformation->FileId.Volume,
1026 pFcb->ObjectInformation->FileId.Vnode,
1027 pFcb->ObjectInformation->FileId.Unique,
1028 StartingByte.QuadPart,
1031 ntStatus = AFSGetExtents( pFcb,
1038 if (!NT_SUCCESS(ntStatus))
1041 AFSDbgLogMsg( AFS_SUBSYSTEM_IO_PROCESSING,
1042 AFS_TRACE_LEVEL_ERROR,
1043 "AFSNonCachedWrite (%08lX) Failed to retrieve mapped extents Status %08lX\n",
1047 try_return( ntStatus );
1050 AFSDbgLogMsg( AFS_SUBSYSTEM_IO_PROCESSING,
1051 AFS_TRACE_LEVEL_VERBOSE,
1052 "AFSNonCachedWrite (%08lX) Successfully retrieved map extents count %08lX run count %08lX\n",
1057 if( BooleanFlagOn( AFSLibControlFlags, AFS_REDIR_LIB_FLAGS_NONPERSISTENT_CACHE))
1060 Irp->IoStatus.Information = ByteCount;
1064 // Setup the MD5 for each extent
1067 AFSSetupMD5Hash( pFcb,
1075 ntStatus = AFSProcessExtentRun( pSystemBuffer,
1081 if (!NT_SUCCESS(ntStatus))
1084 AFSDbgLogMsg( AFS_SUBSYSTEM_IO_PROCESSING,
1085 AFS_TRACE_LEVEL_ERROR,
1086 "AFSNonCachedWrite (%08lX) Failed to process extent run for non-persistent cache Status %08lX\n",
1091 try_return( ntStatus);
1095 // Retrieve the cache file object
1098 pCacheFileObject = AFSReferenceCacheFileObject();
1100 if( pCacheFileObject == NULL)
1103 ntStatus = STATUS_DEVICE_NOT_READY;
1105 AFSDbgLogMsg( AFS_SUBSYSTEM_EXTENT_PROCESSING,
1106 AFS_TRACE_LEVEL_ERROR,
1107 "AFSNonCachedWrite Failed to retrieve cache fileobject for fid %08lX-%08lX-%08lX-%08lX Offset %I64X Length %08lX Status %08lX\n",
1108 pFcb->ObjectInformation->FileId.Cell,
1109 pFcb->ObjectInformation->FileId.Volume,
1110 pFcb->ObjectInformation->FileId.Vnode,
1111 pFcb->ObjectInformation->FileId.Unique,
1112 StartingByte.QuadPart,
1116 try_return( ntStatus);
1119 if (runCount > AFS_MAX_STACK_IO_RUNS)
1122 pIoRuns = (AFSIoRun*) AFSExAllocatePoolWithTag( PagedPool,
1123 runCount * sizeof( AFSIoRun ),
1125 if (NULL == pIoRuns)
1128 AFSDbgLogMsg( AFS_SUBSYSTEM_IO_PROCESSING,
1129 AFS_TRACE_LEVEL_ERROR,
1130 "AFSNonCachedWrite (%08lX) Failed to allocate IO run block\n",
1133 try_return( ntStatus = STATUS_INSUFFICIENT_RESOURCES );
1142 RtlZeroMemory( pIoRuns, runCount * sizeof( AFSIoRun ));
1144 ntStatus = AFSSetupIoRun( IoGetRelatedDeviceObject( pCacheFileObject),
1153 if (!NT_SUCCESS(ntStatus))
1156 AFSDbgLogMsg( AFS_SUBSYSTEM_IO_PROCESSING,
1157 AFS_TRACE_LEVEL_ERROR,
1158 "AFSNonCachedWrite (%08lX) Failed to initialize IO run block Status %08lX\n",
1162 try_return( ntStatus );
1165 AFSReferenceActiveExtents( pStartExtent,
1168 bDerefExtents = TRUE;
1170 AFSDbgLogMsg( AFS_SUBSYSTEM_LOCK_PROCESSING,
1171 AFS_TRACE_LEVEL_VERBOSE,
1172 "AFSNonCachedWrite Releasing(2) Fcb extents lock %08lX SHARED %08lX\n",
1173 &pFcb->NPFcb->Specific.File.ExtentsResource,
1174 PsGetCurrentThread());
1176 AFSReleaseResource( &pFcb->NPFcb->Specific.File.ExtentsResource );
1179 pGatherIo = (AFSGatherIo*) AFSExAllocatePoolWithTag( NonPagedPool,
1180 sizeof( AFSGatherIo),
1183 if (NULL == pGatherIo)
1186 AFSDbgLogMsg( AFS_SUBSYSTEM_IO_PROCESSING,
1187 AFS_TRACE_LEVEL_ERROR,
1188 "AFSNonCachedWrite (%08lX) Failed to allocate IO gather block\n",
1191 AFSDbgLogMsg( AFS_SUBSYSTEM_LOCK_PROCESSING,
1192 AFS_TRACE_LEVEL_VERBOSE,
1193 "AFSNonCachedWrite Acquiring(1) Fcb extents lock %08lX SHARED %08lX\n",
1194 &pFcb->NPFcb->Specific.File.ExtentsResource,
1195 PsGetCurrentThread());
1197 AFSAcquireShared( &pFcb->NPFcb->Specific.File.ExtentsResource,
1201 AFSDereferenceActiveExtents( pStartExtent,
1204 try_return (ntStatus = STATUS_INSUFFICIENT_RESOURCES );
1207 RtlZeroMemory( pGatherIo, sizeof( AFSGatherIo ));
1210 // Initialize count to 1, that was we won't get an early
1211 // completion if the first irp completes before the second is
1214 pGatherIo->Count = 1;
1215 pGatherIo->Status = STATUS_SUCCESS;
1216 pGatherIo->MasterIrp = Irp;
1217 pGatherIo->Synchronous = TRUE;
1218 pGatherIo->CompleteMasterIrp = FALSE;
1220 bCompleteIrp = TRUE;
1222 if( pGatherIo->Synchronous)
1224 KeInitializeEvent( &pGatherIo->Event, NotificationEvent, FALSE );
1229 // Setup the MD5 for each extent
1232 AFSSetupMD5Hash( pFcb,
1241 // Pre-emptively set up the count
1244 Irp->IoStatus.Information = ByteCount;
1246 ntStatus = AFSQueueStartIos( pCacheFileObject,
1248 IRP_WRITE_OPERATION | IRP_SYNCHRONOUS_API,
1253 AFSDbgLogMsg( AFS_SUBSYSTEM_IO_PROCESSING,
1254 AFS_TRACE_LEVEL_VERBOSE,
1255 "AFSNonCachedWrite (%08lX) AFSStartIos completed Status %08lX\n",
1259 if( !NT_SUCCESS( ntStatus))
1262 AFSDbgLogMsg( AFS_SUBSYSTEM_LOCK_PROCESSING,
1263 AFS_TRACE_LEVEL_VERBOSE,
1264 "AFSNonCachedWrite Acquiring(2) Fcb extents lock %08lX SHARED %08lX\n",
1265 &pFcb->NPFcb->Specific.File.ExtentsResource,
1266 PsGetCurrentThread());
1268 AFSAcquireShared( &pFcb->NPFcb->Specific.File.ExtentsResource,
1272 AFSDereferenceActiveExtents( pStartExtent,
1275 try_return( ntStatus);
1279 // Wait for completion of All IOs we started.
1282 ntStatus = KeWaitForSingleObject( &pGatherIo->Event,
1288 if( NT_SUCCESS( ntStatus))
1291 ntStatus = pGatherIo->Status;
1294 if( !NT_SUCCESS( ntStatus))
1297 AFSDbgLogMsg( AFS_SUBSYSTEM_LOCK_PROCESSING,
1298 AFS_TRACE_LEVEL_VERBOSE,
1299 "AFSNonCachedWrite Acquiring(3) Fcb extents lock %08lX SHARED %08lX\n",
1300 &pFcb->NPFcb->Specific.File.ExtentsResource,
1301 PsGetCurrentThread());
1303 AFSAcquireShared( &pFcb->NPFcb->Specific.File.ExtentsResource,
1307 AFSDereferenceActiveExtents( pStartExtent,
1310 try_return( ntStatus);
1315 if( NT_SUCCESS( ntStatus) &&
1316 pStartExtent != NULL &&
1317 Irp->IoStatus.Information > 0)
1321 // Since this is dirty we can mark the extents dirty now.
1322 // AFSMarkDirty will dereference the extents. Do not call
1323 // AFSDereferenceActiveExtents() in this code path.
1335 // This was an uncached user write - tell the server to do
1336 // the flush when the worker thread next wakes up
1338 pFcb->Specific.File.LastServerFlush.QuadPart = 0;
1342 if( pCacheFileObject != NULL)
1344 AFSReleaseCacheFileObject( pCacheFileObject);
1347 AFSDbgLogMsg( AFS_SUBSYSTEM_IO_PROCESSING,
1348 AFS_TRACE_LEVEL_VERBOSE,
1349 "AFSNonCachedWrite (%08lX) Completed request Status %08lX\n",
1353 if (NT_SUCCESS(ntStatus) &&
1358 pFileObject->CurrentByteOffset.QuadPart = StartingByte.QuadPart + ByteCount;
1364 AFSDbgLogMsg( AFS_SUBSYSTEM_LOCK_PROCESSING,
1365 AFS_TRACE_LEVEL_VERBOSE,
1366 "AFSNonCachedWrite Releasing Fcb extents lock %08lX SHARED %08lX\n",
1367 &pFcb->NPFcb->Specific.File.ExtentsResource,
1368 PsGetCurrentThread());
1370 AFSReleaseResource( &pFcb->NPFcb->Specific.File.ExtentsResource );
1375 AFSExFreePool(pGatherIo);
1378 if( NULL != pIoRuns &&
1379 stIoRuns != pIoRuns)
1381 AFSExFreePool(pIoRuns);
1387 AFSDbgLogMsg( AFS_SUBSYSTEM_IO_PROCESSING,
1388 AFS_TRACE_LEVEL_VERBOSE,
1389 "AFSNonCachedWrite Completing Irp %08lX Status %08lX Info %08lX\n",
1392 Irp->IoStatus.Information);
1394 AFSCompleteRequest( Irp, ntStatus);
1403 AFSCachedWrite( IN PDEVICE_OBJECT DeviceObject,
1405 IN LARGE_INTEGER StartingByte,
1407 IN BOOLEAN ForceFlush)
1409 PVOID pSystemBuffer = NULL;
1410 NTSTATUS ntStatus = STATUS_SUCCESS;
1411 IO_STATUS_BLOCK iosbFlush;
1412 IO_STACK_LOCATION *pIrpSp = IoGetCurrentIrpStackLocation( Irp);
1413 PFILE_OBJECT pFileObject = pIrpSp->FileObject;
1414 AFSFcb *pFcb = (AFSFcb *)pFileObject->FsContext;
1415 AFSCcb *pCcb = (AFSCcb *)pFileObject->FsContext2;
1416 BOOLEAN bSynchronousFo = BooleanFlagOn( pFileObject->Flags, FO_SYNCHRONOUS_IO);
1417 BOOLEAN bMapped = FALSE;
1418 ULONG ulCurrentIO = 0, ulTotalLen = ByteCount;
1419 PMDL pCurrentMdl = Irp->MdlAddress;
1420 LARGE_INTEGER liCurrentOffset;
1425 Irp->IoStatus.Information = 0;
1427 if( BooleanFlagOn( pIrpSp->MinorFunction, IRP_MN_MDL))
1433 CcPrepareMdlWrite( pFileObject,
1439 ntStatus = Irp->IoStatus.Status;
1441 __except( EXCEPTION_EXECUTE_HANDLER)
1443 ntStatus = GetExceptionCode();
1445 AFSDbgLogMsg( AFS_SUBSYSTEM_IO_PROCESSING,
1446 AFS_TRACE_LEVEL_ERROR,
1447 "AFSCachedWrite (%08lX) Exception thrown while preparing mdl write Status %08lX\n",
1452 if( !NT_SUCCESS( ntStatus))
1456 // Free up any potentially allocated mdl's
1459 CcMdlWriteComplete( pFileObject,
1463 Irp->MdlAddress = NULL;
1465 AFSDbgLogMsg( AFS_SUBSYSTEM_IO_PROCESSING,
1466 AFS_TRACE_LEVEL_ERROR,
1467 "AFSCachedWrite (%08lX) Failed to process MDL write Status %08lX\n",
1472 try_return( ntStatus);
1475 liCurrentOffset.QuadPart = StartingByte.QuadPart;
1477 while( ulTotalLen > 0)
1480 ntStatus = STATUS_SUCCESS;
1482 if( pCurrentMdl != NULL)
1485 pSystemBuffer = MmGetSystemAddressForMdlSafe( pCurrentMdl,
1486 NormalPagePriority);
1488 ulCurrentIO = MmGetMdlByteCount( pCurrentMdl);
1490 if( ulCurrentIO > ulTotalLen)
1492 ulCurrentIO = ulTotalLen;
1498 pSystemBuffer = AFSLockSystemBuffer( Irp,
1501 ulCurrentIO = ulTotalLen;
1504 if( pSystemBuffer == NULL)
1507 AFSDbgLogMsg( AFS_SUBSYSTEM_IO_PROCESSING,
1508 AFS_TRACE_LEVEL_ERROR,
1509 "AFSCachedWrite (%08lX) Failed to lock system buffer\n",
1512 try_return( ntStatus = STATUS_INSUFFICIENT_RESOURCES);
1518 if( !CcCopyWrite( pFileObject,
1525 // Failed to process request.
1528 AFSDbgLogMsg( AFS_SUBSYSTEM_FILE_PROCESSING,
1529 AFS_TRACE_LEVEL_ERROR,
1530 "AFSCachedWrite (%08lX) Failed to issue CcCopyWrite %wZ @ %0I64X Status %08lX\n",
1532 &pFileObject->FileName,
1533 liCurrentOffset.QuadPart,
1534 Irp->IoStatus.Status);
1536 try_return( ntStatus = STATUS_UNSUCCESSFUL);
1539 __except( EXCEPTION_EXECUTE_HANDLER)
1542 ntStatus = GetExceptionCode();
1544 AFSDbgLogMsg( AFS_SUBSYSTEM_IO_PROCESSING,
1545 AFS_TRACE_LEVEL_ERROR,
1546 "AFSCachedWrite (%08lX) CcCopyWrite Threw exception %wZ @ %0I64X Status %08lX\n",
1548 &pFileObject->FileName,
1549 liCurrentOffset.QuadPart,
1553 if( !NT_SUCCESS( ntStatus))
1555 try_return( ntStatus);
1562 // We have detected a file we do a write through with.
1565 CcFlushCache(&pFcb->NPFcb->SectionObjectPointers,
1570 if( !NT_SUCCESS( iosbFlush.Status))
1573 AFSDbgLogMsg( AFS_SUBSYSTEM_IO_PROCESSING,
1574 AFS_TRACE_LEVEL_ERROR,
1575 "AFSCachedWrite (%08lX) CcFlushCache failure %wZ FID %08lX-%08lX-%08lX-%08lX Status 0x%08lX Bytes 0x%08lX\n",
1577 &pFileObject->FileName,
1578 pFcb->ObjectInformation->FileId.Cell,
1579 pFcb->ObjectInformation->FileId.Volume,
1580 pFcb->ObjectInformation->FileId.Vnode,
1581 pFcb->ObjectInformation->FileId.Unique,
1583 iosbFlush.Information);
1585 try_return( ntStatus = iosbFlush.Status);
1589 if( ulTotalLen <= ulCurrentIO)
1594 liCurrentOffset.QuadPart += ulCurrentIO;
1596 ulTotalLen -= ulCurrentIO;
1598 pCurrentMdl = pCurrentMdl->Next;
1603 if( NT_SUCCESS( ntStatus))
1606 Irp->IoStatus.Information = ByteCount;
1611 pFileObject->CurrentByteOffset.QuadPart = StartingByte.QuadPart + ByteCount;
1615 // If this extended the Vdl, then update it accordingly
1618 if( StartingByte.QuadPart + ByteCount > pFcb->Header.ValidDataLength.QuadPart)
1621 pFcb->Header.ValidDataLength.QuadPart = StartingByte.QuadPart + ByteCount;
1624 if (BooleanFlagOn(pFileObject->Flags, (FO_NO_INTERMEDIATE_BUFFERING + FO_WRITE_THROUGH)))
1627 // Write through asked for... Set things so that we get
1628 // flush when the worker thread next wakes up
1630 pFcb->Specific.File.LastServerFlush.QuadPart = 0;
1633 if( !BooleanFlagOn( pFcb->Flags, AFS_FCB_FLAG_UPDATE_LAST_WRITE_TIME))
1636 SetFlag( pFcb->Flags, AFS_FCB_FLAG_UPDATE_WRITE_TIME);
1638 KeQuerySystemTime( &pFcb->ObjectInformation->LastWriteTime);
1642 AFSCompleteRequest( Irp,
1651 AFSExtendingWrite( IN AFSFcb *Fcb,
1652 IN PFILE_OBJECT FileObject,
1653 IN LONGLONG NewLength)
1655 LARGE_INTEGER liSaveFileSize = Fcb->Header.FileSize;
1656 LARGE_INTEGER liSaveAllocation = Fcb->Header.AllocationSize;
1657 NTSTATUS ntStatus = STATUS_SUCCESS;
1658 AFSCcb *pCcb = (AFSCcb *)FileObject->FsContext2;
1660 if( NewLength > Fcb->Header.AllocationSize.QuadPart)
1663 Fcb->Header.AllocationSize.QuadPart = NewLength;
1665 Fcb->ObjectInformation->AllocationSize = Fcb->Header.AllocationSize;
1668 if( NewLength > Fcb->Header.FileSize.QuadPart)
1671 Fcb->Header.FileSize.QuadPart = NewLength;
1673 Fcb->ObjectInformation->EndOfFile = Fcb->Header.FileSize;
1680 ntStatus = AFSUpdateFileInformation( &Fcb->ObjectInformation->ParentObjectInformation->FileId,
1681 Fcb->ObjectInformation,
1684 AFSDbgLogMsg( AFS_SUBSYSTEM_LOCK_PROCESSING,
1685 AFS_TRACE_LEVEL_VERBOSE,
1686 "AFSExtendingWrite Acquiring Fcb lock %08lX EXCL %08lX\n",
1687 &Fcb->NPFcb->Resource,
1688 PsGetCurrentThread());
1690 if (NT_SUCCESS(ntStatus))
1693 KeQuerySystemTime( &Fcb->ObjectInformation->ChangeTime);
1695 SetFlag( Fcb->Flags, AFS_FCB_FLAG_FILE_MODIFIED | AFS_FCB_FLAG_UPDATE_CHANGE_TIME);
1698 // If the file is currently cached, then let the MM know about the extension
1701 if( CcIsFileCached( FileObject))
1703 CcSetFileSizes( FileObject,
1704 (PCC_FILE_SIZES)&Fcb->Header.AllocationSize);
1709 Fcb->Header.FileSize = liSaveFileSize;
1710 Fcb->Header.AllocationSize = liSaveAllocation;
1714 // DownConvert file resource to shared
1716 ExConvertExclusiveToSharedLite( &Fcb->NPFcb->Resource);
1722 AFSShareWrite( IN PDEVICE_OBJECT DeviceObject,
1726 NTSTATUS ntStatus = STATUS_SUCCESS;
1727 AFSPIOCtlIORequestCB stIORequestCB;
1728 PIO_STACK_LOCATION pIrpSp = IoGetCurrentIrpStackLocation( Irp);
1729 AFSFcb *pFcb = NULL;
1730 AFSCcb *pCcb = NULL;
1731 AFSPipeIORequestCB *pIoRequest = NULL;
1732 void *pBuffer = NULL;
1733 AFSPipeIOResultCB stIoResult;
1734 ULONG ulBytesReturned = 0;
1739 pFcb = (AFSFcb *)pIrpSp->FileObject->FsContext;
1741 pCcb = (AFSCcb *)pIrpSp->FileObject->FsContext2;
1743 AFSDbgLogMsg( AFS_SUBSYSTEM_PIPE_PROCESSING,
1744 AFS_TRACE_LEVEL_VERBOSE,
1745 "AFSShareWrite On pipe %wZ Length %08lX\n",
1746 &pCcb->DirectoryCB->NameInformation.FileName,
1747 pIrpSp->Parameters.Write.Length);
1749 if( pIrpSp->Parameters.Write.Length == 0)
1753 // Nothing to do in this case
1756 try_return( ntStatus);
1760 // Retrieve the buffer for the read request
1763 pBuffer = AFSLockSystemBuffer( Irp,
1764 pIrpSp->Parameters.Write.Length);
1766 if( pBuffer == NULL)
1769 AFSDbgLogMsg( AFS_SUBSYSTEM_PIPE_PROCESSING,
1770 AFS_TRACE_LEVEL_ERROR,
1771 "AFSShareWrite Failed to map buffer on pipe %wZ\n",
1772 &pCcb->DirectoryCB->NameInformation.FileName);
1774 try_return( ntStatus = STATUS_INSUFFICIENT_RESOURCES);
1777 AFSAcquireShared( &pFcb->NPFcb->Resource,
1780 pIoRequest = (AFSPipeIORequestCB *)AFSExAllocatePoolWithTag( PagedPool,
1781 sizeof( AFSPipeIORequestCB) +
1782 pIrpSp->Parameters.Write.Length,
1783 AFS_GENERIC_MEMORY_14_TAG);
1785 if( pIoRequest == NULL)
1788 try_return( ntStatus = STATUS_INSUFFICIENT_RESOURCES);
1791 RtlZeroMemory( pIoRequest,
1792 sizeof( AFSPipeIORequestCB) + pIrpSp->Parameters.Write.Length);
1794 pIoRequest->RequestId = pCcb->RequestID;
1796 pIoRequest->RootId = pFcb->ObjectInformation->VolumeCB->ObjectInformation.FileId;
1798 pIoRequest->BufferLength = pIrpSp->Parameters.Write.Length;
1800 RtlCopyMemory( (void *)((char *)pIoRequest + sizeof( AFSPipeIORequestCB)),
1802 pIrpSp->Parameters.Write.Length);
1804 stIoResult.BytesProcessed = 0;
1806 ulBytesReturned = sizeof( AFSPipeIOResultCB);
1809 // Issue the open request to the service
1812 ntStatus = AFSProcessRequest( AFS_REQUEST_TYPE_PIPE_WRITE,
1813 AFS_REQUEST_FLAG_SYNCHRONOUS,
1815 &pCcb->DirectoryCB->NameInformation.FileName,
1818 sizeof( AFSPipeIORequestCB) +
1819 pIrpSp->Parameters.Write.Length,
1823 if( !NT_SUCCESS( ntStatus))
1826 AFSDbgLogMsg( AFS_SUBSYSTEM_FILE_PROCESSING,
1827 AFS_TRACE_LEVEL_ERROR,
1828 "AFSShareWrite (%08lX) Failed service write Status %08lX\n",
1832 try_return( ntStatus);
1835 AFSDbgLogMsg( AFS_SUBSYSTEM_PIPE_PROCESSING,
1836 AFS_TRACE_LEVEL_VERBOSE,
1837 "AFSShareWrite Completed on pipe %wZ Length read %08lX\n",
1838 &pCcb->DirectoryCB->NameInformation.FileName,
1839 stIoResult.BytesProcessed);
1841 Irp->IoStatus.Information = stIoResult.BytesProcessed;
1848 AFSReleaseResource( &pFcb->NPFcb->Resource);
1851 if( pIoRequest != NULL)
1854 AFSExFreePool( pIoRequest);