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 AFSCachedRead( IN PDEVICE_OBJECT DeviceObject,
45 IN LARGE_INTEGER StartingByte,
48 UNREFERENCED_PARAMETER(DeviceObject);
49 NTSTATUS ntStatus = STATUS_SUCCESS;
50 IO_STACK_LOCATION *pIrpSp = IoGetCurrentIrpStackLocation( Irp);
51 PFILE_OBJECT pFileObject = pIrpSp->FileObject;
52 BOOLEAN bSynchronousIo = BooleanFlagOn( pFileObject->Flags, FO_SYNCHRONOUS_IO);
53 VOID *pSystemBuffer = NULL;
54 ULONG ulCurrentIO = 0, ulTotalLen = ByteCount;
55 PMDL pCurrentMdl = Irp->MdlAddress;
56 LARGE_INTEGER liCurrentOffset;
61 Irp->IoStatus.Information = 0;
63 liCurrentOffset.QuadPart = StartingByte.QuadPart;
65 while( ulTotalLen > 0)
68 ntStatus = STATUS_SUCCESS;
70 if( pCurrentMdl != NULL)
73 ASSERT( pCurrentMdl != NULL);
75 pSystemBuffer = MmGetSystemAddressForMdlSafe( pCurrentMdl,
78 ulCurrentIO = MmGetMdlByteCount( pCurrentMdl);
80 if( ulCurrentIO > ulTotalLen)
82 ulCurrentIO = ulTotalLen;
88 pSystemBuffer = AFSLockSystemBuffer( Irp,
91 ulCurrentIO = ulTotalLen;
94 if( pSystemBuffer == NULL)
97 AFSDbgLogMsg( AFS_SUBSYSTEM_IO_PROCESSING,
98 AFS_TRACE_LEVEL_ERROR,
99 "AFSCachedRead (%p) Failed to lock system buffer\n",
102 try_return( ntStatus = STATUS_INSUFFICIENT_RESOURCES);
108 if( !CcCopyRead( pFileObject,
117 // Failed to process request.
120 AFSDbgLogMsg( AFS_SUBSYSTEM_IO_PROCESSING,
121 AFS_TRACE_LEVEL_ERROR,
122 "AFSCachedRead (%p) Failed CcCopyRead() %wZ @ %0I64X Status %08lX\n",
124 &pFileObject->FileName,
125 liCurrentOffset.QuadPart,
126 Irp->IoStatus.Status);
128 try_return( ntStatus = Irp->IoStatus.Status);
131 __except( EXCEPTION_EXECUTE_HANDLER)
133 ntStatus = GetExceptionCode();
135 AFSDbgLogMsg( AFS_SUBSYSTEM_IO_PROCESSING,
136 AFS_TRACE_LEVEL_ERROR,
137 "AFSCachedRead (%p) CcCopyRead() Threw exception %wZ @ %0I64X Status %08lX\n",
139 &pFileObject->FileName,
140 liCurrentOffset.QuadPart,
144 if( !NT_SUCCESS( ntStatus))
147 try_return( ntStatus);
150 if( ulTotalLen <= ulCurrentIO)
155 liCurrentOffset.QuadPart += ulCurrentIO;
157 ulTotalLen -= ulCurrentIO;
159 pCurrentMdl = pCurrentMdl->Next;
164 if( NT_SUCCESS( ntStatus))
167 Irp->IoStatus.Information = ByteCount;
170 // Update the CBO if this is a sync read
176 pFileObject->CurrentByteOffset.QuadPart = StartingByte.QuadPart + ByteCount;
180 AFSCompleteRequest( Irp,
189 AFSNonCachedRead( IN PDEVICE_OBJECT DeviceObject,
191 IN LARGE_INTEGER StartingByte)
193 NTSTATUS ntStatus = STATUS_UNSUCCESSFUL;
194 IO_STACK_LOCATION *pIrpSp = IoGetCurrentIrpStackLocation( Irp);
195 PFILE_OBJECT pFileObject = pIrpSp->FileObject;
196 AFSFcb *pFcb = (AFSFcb *)pFileObject->FsContext;
197 AFSCcb *pCcb = (AFSCcb *)pFileObject->FsContext2;
198 VOID *pSystemBuffer = NULL;
199 BOOLEAN bPagingIo = BooleanFlagOn( Irp->Flags, IRP_PAGING_IO);
200 BOOLEAN bLocked = FALSE;
201 AFSGatherIo *pGatherIo = NULL;
202 AFSIoRun *pIoRuns = NULL;
203 AFSIoRun stIoRuns[AFS_MAX_STACK_IO_RUNS];
204 ULONG extentsCount = 0, runCount = 0;
205 AFSExtent *pStartExtent = NULL;
206 AFSExtent *pIgnoreExtent = NULL;
207 BOOLEAN bCompleteIrp = TRUE;
208 ULONG ulReadByteCount;
210 AFSDeviceExt *pDevExt = (AFSDeviceExt *)DeviceObject->DeviceExtension;
211 LARGE_INTEGER liCurrentTime, liLastRequestTime;
212 AFSDeviceExt *pControlDevExt = (AFSDeviceExt *)AFSControlDeviceObject->DeviceExtension;
213 PFILE_OBJECT pCacheFileObject = NULL;
218 ulByteCount = pIrpSp->Parameters.Read.Length;
220 if (ulByteCount > pDevExt->Specific.RDR.MaxIo.QuadPart)
223 AFSDbgLogMsg( AFS_SUBSYSTEM_IO_PROCESSING,
224 AFS_TRACE_LEVEL_ERROR,
225 "AFSNonCachedRead (%p) Request larger than MaxIO %I64X\n",
227 pDevExt->Specific.RDR.MaxIo.QuadPart);
229 try_return( ntStatus = STATUS_UNSUCCESSFUL);
232 pSystemBuffer = AFSLockSystemBuffer( Irp,
235 if( pSystemBuffer == NULL)
238 AFSDbgLogMsg( AFS_SUBSYSTEM_IO_PROCESSING,
239 AFS_TRACE_LEVEL_ERROR,
240 "AFSNonCachedRead (%p) Failed to map system buffer\n",
243 try_return( ntStatus = STATUS_INSUFFICIENT_RESOURCES);
246 if( StartingByte.QuadPart + ulByteCount > pFcb->Header.FileSize.QuadPart)
248 ULONG zeroCount = (ULONG) (StartingByte.QuadPart + ulByteCount - pFcb->Header.FileSize.QuadPart);
249 ulReadByteCount = (ULONG)(pFcb->Header.FileSize.QuadPart - StartingByte.QuadPart);
255 AFSDbgLogMsg( AFS_SUBSYSTEM_IO_PROCESSING,
256 AFS_TRACE_LEVEL_VERBOSE,
257 "AFSNonCachedRead (%p) Zeroing to EOF zero byte length %08lX\n",
261 RtlZeroMemory( ((PCHAR)pSystemBuffer) + ulReadByteCount, zeroCount);
265 ulReadByteCount = ulByteCount;
269 // Provoke a get of the extents - if we need to.
274 AFSDbgLogMsg( AFS_SUBSYSTEM_EXTENT_PROCESSING,
275 AFS_TRACE_LEVEL_VERBOSE,
276 "AFSNonCachedRead Requesting extents for fid %08lX-%08lX-%08lX-%08lX Offset %I64X Length %08lX\n",
277 pFcb->ObjectInformation->FileId.Cell,
278 pFcb->ObjectInformation->FileId.Volume,
279 pFcb->ObjectInformation->FileId.Vnode,
280 pFcb->ObjectInformation->FileId.Unique,
281 StartingByte.QuadPart,
284 ntStatus = AFSRequestExtentsAsync( pFcb,
289 if (!NT_SUCCESS(ntStatus))
292 AFSDbgLogMsg( AFS_SUBSYSTEM_EXTENT_PROCESSING,
293 AFS_TRACE_LEVEL_ERROR,
294 "AFSNonCachedRead (%p) Failed to request extents Status %08lX\n",
298 try_return( ntStatus);
301 KeQueryTickCount( &liLastRequestTime);
306 AFSDbgLogMsg( AFS_SUBSYSTEM_LOCK_PROCESSING,
307 AFS_TRACE_LEVEL_VERBOSE,
308 "AFSNonCachedRead Acquiring Fcb extents lock %p SHARED %08lX\n",
309 &pFcb->NPFcb->Specific.File.ExtentsResource,
310 PsGetCurrentThread());
312 AFSAcquireShared( &pFcb->NPFcb->Specific.File.ExtentsResource, TRUE );
316 pIgnoreExtent = NULL;
318 if( AFSDoExtentsMapRegion( pFcb,
327 KeClearEvent( &pFcb->NPFcb->Specific.File.ExtentsRequestComplete);
329 AFSDbgLogMsg( AFS_SUBSYSTEM_LOCK_PROCESSING,
330 AFS_TRACE_LEVEL_VERBOSE,
331 "AFSNonCachedRead Releasing Fcb extents lock %p SHARED %08lX\n",
332 &pFcb->NPFcb->Specific.File.ExtentsResource,
333 PsGetCurrentThread());
335 AFSReleaseResource( &pFcb->NPFcb->Specific.File.ExtentsResource );
339 // We will re-request the extents after 10 seconds of waiting for them
342 KeQueryTickCount( &liCurrentTime);
344 if( liCurrentTime.QuadPart - liLastRequestTime.QuadPart >= pControlDevExt->Specific.Control.ExtentRequestTimeCount.QuadPart)
347 AFSDbgLogMsg( AFS_SUBSYSTEM_EXTENT_PROCESSING,
348 AFS_TRACE_LEVEL_VERBOSE,
349 "AFSNonCachedRead Requesting extents, again, for fid %08lX-%08lX-%08lX-%08lX Offset %I64X Length %08lX\n",
350 pFcb->ObjectInformation->FileId.Cell,
351 pFcb->ObjectInformation->FileId.Volume,
352 pFcb->ObjectInformation->FileId.Vnode,
353 pFcb->ObjectInformation->FileId.Unique,
354 StartingByte.QuadPart,
357 ntStatus = AFSRequestExtentsAsync( pFcb,
362 if (!NT_SUCCESS(ntStatus))
365 AFSDbgLogMsg( AFS_SUBSYSTEM_IO_PROCESSING,
366 AFS_TRACE_LEVEL_ERROR,
367 "AFSNonCachedRead (%p) Failed to request extents Status %08lX\n",
371 try_return( ntStatus);
374 KeQueryTickCount( &liLastRequestTime);
377 AFSDbgLogMsg( AFS_SUBSYSTEM_EXTENT_PROCESSING,
378 AFS_TRACE_LEVEL_VERBOSE,
379 "AFSNonCachedRead Waiting for extents for fid %08lX-%08lX-%08lX-%08lX Offset %I64X Length %08lX\n",
380 pFcb->ObjectInformation->FileId.Cell,
381 pFcb->ObjectInformation->FileId.Volume,
382 pFcb->ObjectInformation->FileId.Vnode,
383 pFcb->ObjectInformation->FileId.Unique,
384 StartingByte.QuadPart,
387 ntStatus = AFSWaitForExtentMapping ( pFcb, pCcb);
389 if (!NT_SUCCESS(ntStatus))
392 AFSDbgLogMsg( AFS_SUBSYSTEM_EXTENT_PROCESSING,
393 AFS_TRACE_LEVEL_ERROR,
394 "AFSNonCachedRead Failed wait for extents for fid %08lX-%08lX-%08lX-%08lX Offset %I64X Length %08lX Status %08lX\n",
395 pFcb->ObjectInformation->FileId.Cell,
396 pFcb->ObjectInformation->FileId.Volume,
397 pFcb->ObjectInformation->FileId.Vnode,
398 pFcb->ObjectInformation->FileId.Unique,
399 StartingByte.QuadPart,
403 try_return( ntStatus);
408 // At this stage we know that the extents are fully mapped and
409 // that, because we took a reference they won't be unmapped.
410 // Thus the list will not move between the start and end.
413 AFSDbgLogMsg( AFS_SUBSYSTEM_EXTENT_PROCESSING,
414 AFS_TRACE_LEVEL_VERBOSE,
415 "AFSNonCachedRead Extents mapped for fid %08lX-%08lX-%08lX-%08lX Offset %I64X Length %08lX\n",
416 pFcb->ObjectInformation->FileId.Cell,
417 pFcb->ObjectInformation->FileId.Volume,
418 pFcb->ObjectInformation->FileId.Vnode,
419 pFcb->ObjectInformation->FileId.Unique,
420 StartingByte.QuadPart,
423 ntStatus = AFSGetExtents( pFcb,
430 if (!NT_SUCCESS(ntStatus))
433 AFSDbgLogMsg( AFS_SUBSYSTEM_EXTENT_PROCESSING,
434 AFS_TRACE_LEVEL_ERROR,
435 "AFSNonCachedRead (%p) Failed to retrieve mapped extents Status %08lX\n",
439 try_return( ntStatus );
442 AFSDbgLogMsg( AFS_SUBSYSTEM_IO_PROCESSING,
443 AFS_TRACE_LEVEL_VERBOSE,
444 "AFSNonCachedRead (%p) Successfully retrieved map extents count %d run count %d\n",
449 if( BooleanFlagOn( AFSLibControlFlags, AFS_REDIR_LIB_FLAGS_NONPERSISTENT_CACHE))
452 Irp->IoStatus.Information = ulReadByteCount;
454 ntStatus = AFSProcessExtentRun( pSystemBuffer,
460 if (!NT_SUCCESS(ntStatus))
463 AFSDbgLogMsg( AFS_SUBSYSTEM_IO_PROCESSING,
464 AFS_TRACE_LEVEL_ERROR,
465 "AFSNonCachedRead (%p) Failed to process extent run for non-persistent cache Status %08lX\n",
470 try_return( ntStatus);
474 // Retrieve the cache file object
477 pCacheFileObject = AFSReferenceCacheFileObject();
479 if( pCacheFileObject == NULL)
482 ntStatus = STATUS_DEVICE_NOT_READY;
484 AFSDbgLogMsg( AFS_SUBSYSTEM_EXTENT_PROCESSING,
485 AFS_TRACE_LEVEL_ERROR,
486 "AFSNonCachedRead Failed to retrieve cache fileobject for fid %08lX-%08lX-%08lX-%08lX Offset %I64X Length %08lX Status %08lX\n",
487 pFcb->ObjectInformation->FileId.Cell,
488 pFcb->ObjectInformation->FileId.Volume,
489 pFcb->ObjectInformation->FileId.Vnode,
490 pFcb->ObjectInformation->FileId.Unique,
491 StartingByte.QuadPart,
495 try_return( ntStatus);
498 if (runCount > AFS_MAX_STACK_IO_RUNS)
501 pIoRuns = (AFSIoRun*) AFSExAllocatePoolWithTag( PagedPool,
502 runCount * sizeof( AFSIoRun ),
507 AFSDbgLogMsg( AFS_SUBSYSTEM_IO_PROCESSING,
508 AFS_TRACE_LEVEL_ERROR,
509 "AFSNonCachedRead (%p) Failed to allocate IO run block\n",
512 try_return( ntStatus = STATUS_INSUFFICIENT_RESOURCES );
521 RtlZeroMemory( pIoRuns, runCount * sizeof( AFSIoRun ));
523 ntStatus = AFSSetupIoRun( IoGetRelatedDeviceObject( pCacheFileObject),
532 if (!NT_SUCCESS(ntStatus))
535 AFSDbgLogMsg( AFS_SUBSYSTEM_IO_PROCESSING,
536 AFS_TRACE_LEVEL_ERROR,
537 "AFSNonCachedRead (%p) Failed to initialize IO run block Status %08lX\n",
541 try_return( ntStatus );
544 AFSReferenceActiveExtents( pStartExtent,
547 AFSDbgLogMsg( AFS_SUBSYSTEM_LOCK_PROCESSING,
548 AFS_TRACE_LEVEL_VERBOSE,
549 "AFSNonCachedRead Releasing Fcb extents lock %p SHARED %08lX\n",
550 &pFcb->NPFcb->Specific.File.ExtentsResource,
551 PsGetCurrentThread());
553 AFSReleaseResource( &pFcb->NPFcb->Specific.File.ExtentsResource );
556 pGatherIo = (AFSGatherIo*) AFSExAllocatePoolWithTag( NonPagedPool,
557 sizeof( AFSGatherIo ),
560 if (NULL == pGatherIo)
563 AFSDbgLogMsg( AFS_SUBSYSTEM_IO_PROCESSING,
564 AFS_TRACE_LEVEL_ERROR,
565 "AFSNonCachedRead (%p) Failed to allocate IO gather block\n",
568 AFSDbgLogMsg( AFS_SUBSYSTEM_LOCK_PROCESSING,
569 AFS_TRACE_LEVEL_VERBOSE,
570 "AFSNonCachedRead Acquiring(2) Fcb extents lock %p SHARED %08lX\n",
571 &pFcb->NPFcb->Specific.File.ExtentsResource,
572 PsGetCurrentThread());
574 AFSAcquireShared( &pFcb->NPFcb->Specific.File.ExtentsResource,
578 AFSDereferenceActiveExtents( pStartExtent,
581 try_return (ntStatus = STATUS_INSUFFICIENT_RESOURCES );
584 RtlZeroMemory( pGatherIo, sizeof( AFSGatherIo ));
587 // Initialize count to 1, that was we won't get an early
588 // completion if the first irp completes before the second is
591 pGatherIo->Count = 1;
592 pGatherIo->Status = STATUS_SUCCESS;
593 pGatherIo->MasterIrp = Irp;
594 pGatherIo->Synchronous = TRUE;
595 pGatherIo->Fcb = pFcb;
596 pGatherIo->CompleteMasterIrp = FALSE;
600 if (pGatherIo->Synchronous)
602 KeInitializeEvent( &pGatherIo->Event, NotificationEvent, FALSE );
606 IoMarkIrpPending( Irp);
610 // Pre-emptively set up the count
613 Irp->IoStatus.Information = ulByteCount;
615 ntStatus = AFSQueueStartIos( pCacheFileObject,
617 IRP_READ_OPERATION | IRP_SYNCHRONOUS_API,
622 AFSDbgLogMsg( AFS_SUBSYSTEM_IO_PROCESSING,
623 AFS_TRACE_LEVEL_VERBOSE,
624 "AFSNonCachedRead (%p) AFSStartIos completed Status %08lX\n",
628 if( !NT_SUCCESS( ntStatus))
631 AFSDbgLogMsg( AFS_SUBSYSTEM_LOCK_PROCESSING,
632 AFS_TRACE_LEVEL_VERBOSE,
633 "AFSNonCachedRead Acquiring(3) Fcb extents lock %p SHARED %08lX\n",
634 &pFcb->NPFcb->Specific.File.ExtentsResource,
635 PsGetCurrentThread());
637 AFSAcquireShared( &pFcb->NPFcb->Specific.File.ExtentsResource,
641 AFSDereferenceActiveExtents( pStartExtent,
644 try_return( ntStatus);
648 // Wait for completion of all IOs we started.
650 (VOID) KeWaitForSingleObject( &pGatherIo->Event,
656 ntStatus = pGatherIo->Status;
658 AFSDbgLogMsg( AFS_SUBSYSTEM_IO_PROCESSING,
659 AFS_TRACE_LEVEL_VERBOSE,
660 "AFSNonCachedRead (%p) AFSStartIos wait completed Status %08lX\n",
664 AFSDbgLogMsg( AFS_SUBSYSTEM_LOCK_PROCESSING,
665 AFS_TRACE_LEVEL_VERBOSE,
666 "AFSNonCachedRead Acquiring(4) Fcb extents lock %p SHARED %08lX\n",
667 &pFcb->NPFcb->Specific.File.ExtentsResource,
668 PsGetCurrentThread());
670 AFSAcquireShared( &pFcb->NPFcb->Specific.File.ExtentsResource,
674 AFSDereferenceActiveExtents( pStartExtent,
677 AFSDbgLogMsg( AFS_SUBSYSTEM_LOCK_PROCESSING,
678 AFS_TRACE_LEVEL_VERBOSE,
679 "AFSNonCachedRead Releasing Fcb extents lock %p SHARED %08lX\n",
680 &pFcb->NPFcb->Specific.File.ExtentsResource,
681 PsGetCurrentThread());
683 AFSReleaseResource( &pFcb->NPFcb->Specific.File.ExtentsResource );
687 // The data is there now. Give back the extents now so the service
691 if ( pFcb->Specific.File.ExtentLength > 4096)
694 (VOID) AFSReleaseExtentsWithFlush( pFcb,
701 if( pCacheFileObject != NULL)
703 AFSReleaseCacheFileObject( pCacheFileObject);
706 AFSDbgLogMsg( AFS_SUBSYSTEM_IO_PROCESSING,
707 AFS_TRACE_LEVEL_VERBOSE,
708 "AFSNonCachedRead (%p) Completed request Status %08lX\n",
712 if (NT_SUCCESS(ntStatus) &&
714 BooleanFlagOn( pFileObject->Flags, FO_SYNCHRONOUS_IO))
717 // Update the CBO if this is a sync, nopaging read
719 pFileObject->CurrentByteOffset.QuadPart = StartingByte.QuadPart + ulByteCount;
725 AFSDbgLogMsg( AFS_SUBSYSTEM_LOCK_PROCESSING,
726 AFS_TRACE_LEVEL_VERBOSE,
727 "AFSNonCachedRead Releasing Fcb extents lock %p SHARED %08lX\n",
728 &pFcb->NPFcb->Specific.File.ExtentsResource,
729 PsGetCurrentThread());
731 AFSReleaseResource( &pFcb->NPFcb->Specific.File.ExtentsResource );
736 AFSExFreePoolWithTag(pGatherIo, AFS_GATHER_TAG);
739 if (NULL != pIoRuns && stIoRuns != pIoRuns)
741 AFSExFreePoolWithTag(pIoRuns, AFS_IO_RUN_TAG);
747 AFSDbgLogMsg( AFS_SUBSYSTEM_IO_PROCESSING,
748 AFS_TRACE_LEVEL_VERBOSE,
749 "AFSNonCachedRead Completing irp %08lX Status %08lX Info %08lX\n",
752 Irp->IoStatus.Information);
754 AFSCompleteRequest( Irp, ntStatus );
760 // Function: AFSDispatch
764 // A shim around AFSCommonRead (qv)
767 AFSRead( IN PDEVICE_OBJECT LibDeviceObject,
771 UNREFERENCED_PARAMETER(LibDeviceObject);
772 NTSTATUS ntStatus = STATUS_SUCCESS;
777 ntStatus = AFSCommonRead( AFSRDRDeviceObject, Irp, NULL);
779 __except( AFSExceptionFilter( __FUNCTION__, GetExceptionCode(), GetExceptionInformation()) )
782 ntStatus = STATUS_INSUFFICIENT_RESOURCES;
784 AFSDumpTraceFilesFnc();
794 // This function is a slightly widened Dispatch handler for the
795 // AFS Read function. The third parameter is NULL if we were called
796 // at our dispatch point and a process handle if we have been posted.
798 // After doing the obvious (completing MDL writes and so forth)
799 // we then post, or not.
803 // A status is returned for the function
807 AFSCommonRead( IN PDEVICE_OBJECT DeviceObject,
809 IN HANDLE OnBehalfOf)
812 UNREFERENCED_PARAMETER(OnBehalfOf);
813 NTSTATUS ntStatus = STATUS_SUCCESS;
814 AFSDeviceExt *pDeviceExt;
815 IO_STACK_LOCATION *pIrpSp;
818 BOOLEAN bReleaseMain = FALSE;
819 BOOLEAN bReleaseSectionObject = FALSE;
820 BOOLEAN bReleasePaging = FALSE;
821 BOOLEAN bPagingIo = FALSE;
822 BOOLEAN bNonCachedIo = FALSE;
823 BOOLEAN bCompleteIrp = TRUE;
824 PFILE_OBJECT pFileObject = NULL;
825 LARGE_INTEGER liStartingByte;
828 pIrpSp = IoGetCurrentIrpStackLocation( Irp);
829 pDeviceExt = (AFSDeviceExt *)DeviceObject->DeviceExtension;
834 // Decode the fileobject
836 pFileObject = pIrpSp->FileObject;
839 // There is a risk (albeit infinitely small) that the Irp will
840 // complete before this function exits, then a cleanup and
841 // close will happen and the FCB will be torn down before we
842 // get to the try_exit. Pin the file Object which will pin the FCB
845 ObReferenceObject( pFileObject);
848 // If we are in shutdown mode then fail the request
851 if( BooleanFlagOn( pDeviceExt->DeviceFlags, AFS_DEVICE_FLAG_REDIRECTOR_SHUTDOWN))
854 AFSDbgLogMsg( AFS_SUBSYSTEM_FILE_PROCESSING,
855 AFS_TRACE_LEVEL_WARNING,
856 "AFSCommonRead (%p) Open request after shutdown\n",
859 try_return( ntStatus = STATUS_TOO_LATE);
862 pFcb = (AFSFcb *)pFileObject->FsContext;
864 pCcb = (AFSCcb *)pFileObject->FsContext2;
869 AFSDbgLogMsg( AFS_SUBSYSTEM_IO_PROCESSING,
870 AFS_TRACE_LEVEL_ERROR,
871 "AFSCommonRead Attempted read (%p) when pFcb == NULL\n",
874 try_return( ntStatus = STATUS_INVALID_DEVICE_REQUEST);
877 bPagingIo = BooleanFlagOn( Irp->Flags, IRP_PAGING_IO);
878 bNonCachedIo = BooleanFlagOn( Irp->Flags,IRP_NOCACHE);
880 liStartingByte = pIrpSp->Parameters.Read.ByteOffset;
881 ulByteCount = pIrpSp->Parameters.Read.Length;
883 if( pFcb->Header.NodeTypeCode != AFS_IOCTL_FCB &&
884 pFcb->Header.NodeTypeCode != AFS_FILE_FCB &&
885 pFcb->Header.NodeTypeCode != AFS_SPECIAL_SHARE_FCB)
888 AFSDbgLogMsg( AFS_SUBSYSTEM_IO_PROCESSING,
889 AFS_TRACE_LEVEL_ERROR,
890 "AFSCommonRead Attempted read (%p) on an invalid node type %08lX\n",
892 pFcb->Header.NodeTypeCode);
894 try_return( ntStatus = STATUS_INVALID_DEVICE_REQUEST);
898 // If this is a read against an IOCtl node then handle it
899 // in a different pathway
902 if( pFcb->Header.NodeTypeCode == AFS_IOCTL_FCB)
905 AFSDbgLogMsg( AFS_SUBSYSTEM_IO_PROCESSING,
906 AFS_TRACE_LEVEL_VERBOSE,
907 "AFSCommonRead (%p) Processing file (PIOCTL) Offset %I64X Length %08lX Irp Flags %08lX\n",
909 liStartingByte.QuadPart,
913 ntStatus = AFSIOCtlRead( DeviceObject,
916 try_return( ntStatus);
918 else if( pFcb->Header.NodeTypeCode == AFS_SPECIAL_SHARE_FCB)
921 AFSDbgLogMsg( AFS_SUBSYSTEM_IO_PROCESSING,
922 AFS_TRACE_LEVEL_VERBOSE,
923 "AFSCommonRead (%p) Processing file (SHARE) Offset %I64X Length %08lX Irp Flags %08lX\n",
925 liStartingByte.QuadPart,
929 ntStatus = AFSShareRead( DeviceObject,
932 try_return( ntStatus);
936 // No fileobject yet? Bail.
938 if( !BooleanFlagOn( AFSLibControlFlags, AFS_REDIR_LIB_FLAGS_NONPERSISTENT_CACHE) &&
939 NULL == pDeviceExt->Specific.RDR.CacheFileObject)
942 AFSDbgLogMsg( AFS_SUBSYSTEM_IO_PROCESSING,
943 AFS_TRACE_LEVEL_ERROR,
944 "AFSCommonRead (%p) Request failed due to AFS cache closed\n",
947 try_return( ntStatus = STATUS_TOO_LATE );
950 if( pIrpSp->Parameters.Read.Length == 0)
953 AFSDbgLogMsg( AFS_SUBSYSTEM_IO_PROCESSING,
954 AFS_TRACE_LEVEL_VERBOSE,
955 "AFSCommonRead (%p) Request completed due to zero length\n",
958 try_return( ntStatus);
961 if ( FlagOn(pIrpSp->MinorFunction, IRP_MN_COMPLETE) )
964 AFSDbgLogMsg( AFS_SUBSYSTEM_LOCK_PROCESSING,
965 AFS_TRACE_LEVEL_VERBOSE,
966 "AFSCommonRead Acquiring Fcb SectionObject lock %p SHARED %08lX\n",
967 &pFcb->NPFcb->SectionObjectResource,
968 PsGetCurrentThread());
970 AFSAcquireShared( &pFcb->NPFcb->SectionObjectResource,
973 bReleaseSectionObject = TRUE;
975 AFSDbgLogMsg( AFS_SUBSYSTEM_IO_PROCESSING,
976 AFS_TRACE_LEVEL_VERBOSE,
977 "AFSCommonRead (%p) IRP_MN_COMPLETE being processed\n",
980 CcMdlReadComplete(pIrpSp->FileObject, Irp->MdlAddress);
983 // Mdl is now Deallocated
986 Irp->MdlAddress = NULL;
988 try_return( ntStatus = STATUS_SUCCESS );
992 // If we get a non cached IO for a cached file we should do a purge.
993 // For now we will just promote to cached
995 if (NULL != pFileObject->SectionObjectPointer->DataSectionObject && !bPagingIo)
998 bNonCachedIo = FALSE;
1002 // We acquire the main/paging resource first to synchronize
1003 // against size checks.
1009 AFSDbgLogMsg( AFS_SUBSYSTEM_LOCK_PROCESSING,
1010 AFS_TRACE_LEVEL_VERBOSE,
1011 "AFSCommonRead Acquiring Fcb PagingIo lock %p SHARED %08lX\n",
1012 &pFcb->NPFcb->PagingResource,
1013 PsGetCurrentThread());
1015 AFSAcquireShared( &pFcb->NPFcb->PagingResource,
1018 bReleasePaging = TRUE;
1020 AFSDbgLogMsg( AFS_SUBSYSTEM_LOCK_PROCESSING,
1021 AFS_TRACE_LEVEL_VERBOSE,
1022 "AFSCommonRead Acquiring Fcb SectionObject lock %p SHARED %08lX\n",
1023 &pFcb->NPFcb->SectionObjectResource,
1024 PsGetCurrentThread());
1026 AFSAcquireShared( &pFcb->NPFcb->SectionObjectResource,
1029 bReleaseSectionObject = TRUE;
1035 AFSDbgLogMsg( AFS_SUBSYSTEM_LOCK_PROCESSING,
1036 AFS_TRACE_LEVEL_VERBOSE,
1037 "AFSCommonRead Acquiring Fcb SectionObject lock %p SHARED %08lX\n",
1038 &pFcb->NPFcb->SectionObjectResource,
1039 PsGetCurrentThread());
1041 AFSAcquireShared( &pFcb->NPFcb->SectionObjectResource,
1044 bReleaseSectionObject = TRUE;
1047 // Check the BR locks
1050 if( !FsRtlCheckLockForReadAccess( &pFcb->Specific.File.FileLock,
1054 AFSDbgLogMsg( AFS_SUBSYSTEM_IO_PROCESSING,
1055 AFS_TRACE_LEVEL_ERROR,
1056 "AFSCommonRead (%p) Request failed due to lock conflict\n",
1059 try_return( ntStatus = STATUS_FILE_LOCK_CONFLICT);
1063 if( BooleanFlagOn( pCcb->DirectoryCB->Flags, AFS_DIR_ENTRY_DELETED) ||
1064 BooleanFlagOn( pFcb->ObjectInformation->Flags, AFS_OBJECT_FLAGS_DELETED))
1067 AFSDbgLogMsg( AFS_SUBSYSTEM_IO_PROCESSING,
1068 AFS_TRACE_LEVEL_ERROR,
1069 "AFSCommonRead (%p) Request failed due to file deleted\n",
1072 try_return( ntStatus = STATUS_FILE_DELETED);
1076 // If the read starts beyond End of File, return EOF.
1079 if( liStartingByte.QuadPart >= pFcb->Header.FileSize.QuadPart)
1082 AFSDbgLogMsg( AFS_SUBSYSTEM_IO_PROCESSING,
1083 AFS_TRACE_LEVEL_VERBOSE,
1084 "AFSCommonRead (%p) Request beyond EOF %I64X\n",
1086 pFcb->Header.FileSize.QuadPart);
1088 try_return( ntStatus = STATUS_END_OF_FILE);
1094 if( liStartingByte.QuadPart + ulByteCount > pFcb->Header.FileSize.QuadPart)
1097 ulByteCount = (ULONG)(pFcb->Header.FileSize.QuadPart - liStartingByte.QuadPart);
1099 AFSDbgLogMsg( AFS_SUBSYSTEM_IO_PROCESSING,
1100 AFS_TRACE_LEVEL_VERBOSE,
1101 "AFSCommonRead (%p) Truncated read request to %08lX\n",
1107 // Is this Fcb valid???
1110 if( BooleanFlagOn( pFcb->ObjectInformation->Flags, AFS_OBJECT_FLAGS_OBJECT_INVALID))
1113 AFSDbgLogMsg( AFS_SUBSYSTEM_IO_PROCESSING,
1114 AFS_TRACE_LEVEL_ERROR,
1115 "AFSCommonRead (%p) Failing request due to INVALID fcb\n",
1118 Irp->IoStatus.Information = 0;
1120 try_return( ntStatus = STATUS_FILE_DELETED);
1124 // If this is an cached IO
1126 if( (!bPagingIo && !bNonCachedIo))
1129 // This is a top level irp. Init the caching if it has not yet
1130 // been initialzed for this FO
1133 if( pFileObject->PrivateCacheMap == NULL)
1139 AFSDbgLogMsg( AFS_SUBSYSTEM_IO_PROCESSING,
1140 AFS_TRACE_LEVEL_VERBOSE,
1141 "AFSCommonRead Initialize caching on Fcb %p FO %p\n",
1145 CcInitializeCacheMap( pFileObject,
1146 (PCC_FILE_SIZES)&pFcb->Header.AllocationSize,
1148 AFSLibCacheManagerCallbacks,
1151 CcSetReadAheadGranularity( pFileObject,
1152 pDeviceExt->Specific.RDR.MaximumRPCLength);
1154 CcSetDirtyPageThreshold( pFileObject,
1155 AFS_DIRTY_CHUNK_THRESHOLD * pDeviceExt->Specific.RDR.MaximumRPCLength);
1157 __except( EXCEPTION_EXECUTE_HANDLER)
1160 ntStatus = GetExceptionCode();
1162 AFSDbgLogMsg( AFS_SUBSYSTEM_IO_PROCESSING,
1163 AFS_TRACE_LEVEL_ERROR,
1164 "AFSCommonRead (%p) Exception thrown while initializing cache map Status %08lX\n",
1169 if( !NT_SUCCESS( ntStatus))
1172 try_return( ntStatus);
1177 // And if this is MDL operation, deal with it now (yes we
1178 // could post, but we are almost certainly posted
1179 // already and we don't want to grow and SVA for it..)
1182 if( BooleanFlagOn( pIrpSp->MinorFunction, IRP_MN_MDL))
1187 CcMdlRead( pFileObject,
1192 ntStatus = Irp->IoStatus.Status;
1194 __except( EXCEPTION_EXECUTE_HANDLER)
1196 ntStatus = GetExceptionCode();
1198 AFSDbgLogMsg( AFS_SUBSYSTEM_IO_PROCESSING,
1199 AFS_TRACE_LEVEL_ERROR,
1200 "AFSCommonRead (%p) Exception thrown during mdl read Status %08lX\n",
1205 if( !NT_SUCCESS( ntStatus))
1208 AFSDbgLogMsg( AFS_SUBSYSTEM_IO_PROCESSING,
1209 AFS_TRACE_LEVEL_ERROR,
1210 "AFSCommonRead (%p) Failed to process MDL read Status %08lX\n",
1214 try_return( ntStatus);
1218 // Update the CBO if this is a sync read
1221 if( BooleanFlagOn( pFileObject->Flags, FO_SYNCHRONOUS_IO))
1224 pFileObject->CurrentByteOffset.QuadPart = liStartingByte.QuadPart + ulByteCount;
1227 try_return( ntStatus);
1232 // The called request completes the IRP for us.
1235 bCompleteIrp = FALSE;
1241 AFSDbgLogMsg( AFS_SUBSYSTEM_IO_PROCESSING,
1242 AFS_TRACE_LEVEL_VERBOSE,
1243 "AFSCommonRead (%p) Processing CACHED request Offset %I64X Len %08lX\n",
1245 liStartingByte.QuadPart,
1248 ntStatus = AFSCachedRead( DeviceObject, Irp, liStartingByte, ulByteCount);
1256 AFSReleaseResource( &pFcb->NPFcb->PagingResource);
1258 bReleasePaging = FALSE;
1261 if( bReleaseSectionObject)
1264 AFSReleaseResource( &pFcb->NPFcb->SectionObjectResource);
1266 bReleaseSectionObject = FALSE;
1272 AFSReleaseResource( &pFcb->NPFcb->Resource);
1274 bReleaseMain = FALSE;
1277 AFSDbgLogMsg( AFS_SUBSYSTEM_IO_PROCESSING,
1278 AFS_TRACE_LEVEL_VERBOSE,
1279 "AFSCommonRead (%p) Processing NON-CACHED request Offset %I64X Len %08lX\n",
1281 liStartingByte.QuadPart,
1284 ntStatus = AFSNonCachedRead( DeviceObject, Irp, liStartingByte);
1289 AFSDbgLogMsg( AFS_SUBSYSTEM_IO_PROCESSING,
1290 AFS_TRACE_LEVEL_VERBOSE,
1291 "AFSCommonRead (%p) Process complete Status %08lX\n",
1298 AFSReleaseResource( &pFcb->NPFcb->PagingResource);
1301 if( bReleaseSectionObject)
1304 AFSReleaseResource( &pFcb->NPFcb->SectionObjectResource);
1310 AFSReleaseResource( &pFcb->NPFcb->Resource);
1316 AFSCompleteRequest( Irp, ntStatus);
1319 ObDereferenceObject( pFileObject);
1326 AFSIOCtlRead( IN PDEVICE_OBJECT DeviceObject,
1330 UNREFERENCED_PARAMETER(DeviceObject);
1331 NTSTATUS ntStatus = STATUS_SUCCESS;
1332 AFSPIOCtlIORequestCB stIORequestCB;
1333 PIO_STACK_LOCATION pIrpSp = IoGetCurrentIrpStackLocation( Irp);
1334 AFSFcb *pFcb = NULL;
1335 AFSCcb *pCcb = NULL;
1336 AFSPIOCtlIOResultCB stIOResultCB;
1337 ULONG ulBytesReturned = 0;
1338 AFSFileID stParentFID;
1343 RtlZeroMemory( &stIORequestCB,
1344 sizeof( AFSPIOCtlIORequestCB));
1346 if( pIrpSp->Parameters.Read.Length == 0)
1350 // Nothing to do in this case
1353 try_return( ntStatus);
1356 pFcb = (AFSFcb *)pIrpSp->FileObject->FsContext;
1358 pCcb = (AFSCcb *)pIrpSp->FileObject->FsContext2;
1360 AFSDbgLogMsg( AFS_SUBSYSTEM_LOCK_PROCESSING,
1361 AFS_TRACE_LEVEL_VERBOSE,
1362 "AFSIOCtlRead Acquiring Fcb lock %p SHARED %08lX\n",
1363 &pFcb->NPFcb->Resource,
1364 PsGetCurrentThread());
1366 AFSAcquireShared( &pFcb->NPFcb->Resource,
1370 // Get the parent fid to pass to the cm
1373 RtlZeroMemory( &stParentFID,
1374 sizeof( AFSFileID));
1376 if( pFcb->ObjectInformation->ParentObjectInformation != NULL)
1380 // The parent directory FID of the node
1383 ASSERT( pFcb->ObjectInformation->ParentObjectInformation->FileType == AFS_FILE_TYPE_DIRECTORY);
1385 stParentFID = pFcb->ObjectInformation->ParentObjectInformation->FileId;
1389 // Set the control block up
1392 stIORequestCB.RequestId = pCcb->RequestID;
1394 if( pFcb->ObjectInformation->VolumeCB != NULL)
1396 stIORequestCB.RootId = pFcb->ObjectInformation->VolumeCB->ObjectInformation.FileId;
1400 // Lock down the buffer
1403 stIORequestCB.MappedBuffer = AFSMapToService( Irp,
1404 pIrpSp->Parameters.Read.Length);
1406 if( stIORequestCB.MappedBuffer == NULL)
1409 try_return( ntStatus = STATUS_INSUFFICIENT_RESOURCES);
1412 stIORequestCB.BufferLength = pIrpSp->Parameters.Read.Length;
1414 stIOResultCB.BytesProcessed = 0;
1416 ulBytesReturned = sizeof( AFSPIOCtlIOResultCB);
1419 // Issue the request to the service
1422 ntStatus = AFSProcessRequest( AFS_REQUEST_TYPE_PIOCTL_READ,
1423 AFS_REQUEST_FLAG_SYNCHRONOUS,
1427 (void *)&stIORequestCB,
1428 sizeof( AFSPIOCtlIORequestCB),
1432 if( !NT_SUCCESS( ntStatus))
1435 try_return( ntStatus);
1439 // Update the length read
1442 Irp->IoStatus.Information = stIOResultCB.BytesProcessed;
1446 if( stIORequestCB.MappedBuffer != NULL)
1449 AFSUnmapServiceMappedBuffer( stIORequestCB.MappedBuffer,
1456 AFSReleaseResource( &pFcb->NPFcb->Resource);
1464 AFSShareRead( IN PDEVICE_OBJECT DeviceObject,
1468 UNREFERENCED_PARAMETER(DeviceObject);
1469 NTSTATUS ntStatus = STATUS_SUCCESS;
1470 PIO_STACK_LOCATION pIrpSp = IoGetCurrentIrpStackLocation( Irp);
1471 AFSFcb *pFcb = NULL;
1472 AFSCcb *pCcb = NULL;
1473 ULONG ulBytesReturned = 0;
1474 void *pBuffer = NULL;
1475 AFSPipeIORequestCB stIoRequest;
1480 pFcb = (AFSFcb *)pIrpSp->FileObject->FsContext;
1482 pCcb = (AFSCcb *)pIrpSp->FileObject->FsContext2;
1484 AFSDbgLogMsg( AFS_SUBSYSTEM_PIPE_PROCESSING,
1485 AFS_TRACE_LEVEL_VERBOSE,
1486 "AFSShareRead On pipe %wZ Length %08lX\n",
1487 &pCcb->DirectoryCB->NameInformation.FileName,
1488 pIrpSp->Parameters.Read.Length);
1490 if( pIrpSp->Parameters.Read.Length == 0)
1494 // Nothing to do in this case
1497 try_return( ntStatus);
1500 AFSAcquireShared( &pFcb->NPFcb->Resource,
1504 // Retrieve the buffer for the read request
1507 pBuffer = AFSLockSystemBuffer( Irp,
1508 pIrpSp->Parameters.Read.Length);
1510 if( pBuffer == NULL)
1513 AFSDbgLogMsg( AFS_SUBSYSTEM_PIPE_PROCESSING,
1514 AFS_TRACE_LEVEL_ERROR,
1515 "AFSShareRead Failed to map buffer on pipe %wZ\n",
1516 &pCcb->DirectoryCB->NameInformation.FileName);
1518 try_return( ntStatus = STATUS_INSUFFICIENT_RESOURCES);
1521 RtlZeroMemory( &stIoRequest,
1522 sizeof( AFSPipeIORequestCB));
1524 stIoRequest.RequestId = pCcb->RequestID;
1526 stIoRequest.RootId = pFcb->ObjectInformation->VolumeCB->ObjectInformation.FileId;
1528 stIoRequest.BufferLength = pIrpSp->Parameters.Read.Length;
1530 ulBytesReturned = pIrpSp->Parameters.Read.Length;
1533 // Issue the open request to the service
1536 ntStatus = AFSProcessRequest( AFS_REQUEST_TYPE_PIPE_READ,
1537 AFS_REQUEST_FLAG_SYNCHRONOUS,
1539 &pCcb->DirectoryCB->NameInformation.FileName,
1541 (void *)&stIoRequest,
1542 sizeof( AFSPipeIORequestCB),
1546 if( !NT_SUCCESS( ntStatus) &&
1547 ntStatus != STATUS_BUFFER_OVERFLOW)
1550 AFSDbgLogMsg( AFS_SUBSYSTEM_FILE_PROCESSING,
1551 AFS_TRACE_LEVEL_ERROR,
1552 "AFSShareRead (%p) Failed service read Status %08lX\n",
1556 try_return( ntStatus);
1559 AFSDbgLogMsg( AFS_SUBSYSTEM_PIPE_PROCESSING,
1560 AFS_TRACE_LEVEL_VERBOSE,
1561 "AFSShareRead Completed on pipe %wZ Length read %08lX Status %08lX\n",
1562 &pCcb->DirectoryCB->NameInformation.FileName,
1566 Irp->IoStatus.Information = ulBytesReturned;
1573 AFSReleaseResource( &pFcb->NPFcb->Resource);