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 NTSTATUS ntStatus = STATUS_SUCCESS;
49 IO_STACK_LOCATION *pIrpSp = IoGetCurrentIrpStackLocation( Irp);
50 PFILE_OBJECT pFileObject = pIrpSp->FileObject;
51 AFSFcb *pFcb = (AFSFcb *)pFileObject->FsContext;
52 AFSCcb *pCcb = (AFSCcb *)pFileObject->FsContext2;
53 BOOLEAN bSynchronousIo = BooleanFlagOn( pFileObject->Flags, FO_SYNCHRONOUS_IO);
54 VOID *pSystemBuffer = NULL;
55 ULONG ulCurrentIO = 0, ulTotalLen = ByteCount;
56 PMDL pCurrentMdl = Irp->MdlAddress;
57 LARGE_INTEGER liCurrentOffset;
62 Irp->IoStatus.Information = 0;
64 liCurrentOffset.QuadPart = StartingByte.QuadPart;
66 while( ulTotalLen > 0)
69 ntStatus = STATUS_SUCCESS;
71 if( pCurrentMdl != NULL)
74 ASSERT( pCurrentMdl != NULL);
76 pSystemBuffer = MmGetSystemAddressForMdlSafe( pCurrentMdl,
79 ulCurrentIO = MmGetMdlByteCount( pCurrentMdl);
81 if( ulCurrentIO > ulTotalLen)
83 ulCurrentIO = ulTotalLen;
89 pSystemBuffer = AFSLockSystemBuffer( Irp,
92 ulCurrentIO = ulTotalLen;
95 if( pSystemBuffer == NULL)
98 AFSDbgLogMsg( AFS_SUBSYSTEM_IO_PROCESSING,
99 AFS_TRACE_LEVEL_ERROR,
100 "AFSCachedRead (%08lX) Failed to lock system buffer\n",
103 try_return( ntStatus = STATUS_INSUFFICIENT_RESOURCES);
109 if( !CcCopyRead( pFileObject,
118 // Failed to process request.
121 AFSDbgLogMsg( AFS_SUBSYSTEM_IO_PROCESSING,
122 AFS_TRACE_LEVEL_ERROR,
123 "AFSCachedRead (%08lX) Failed CcCopyRead() %wZ @ %0I64X Status %08lX\n",
125 &pFileObject->FileName,
126 liCurrentOffset.QuadPart,
127 Irp->IoStatus.Status);
129 try_return( ntStatus = Irp->IoStatus.Status);
132 __except( EXCEPTION_EXECUTE_HANDLER)
134 ntStatus = GetExceptionCode();
136 AFSDbgLogMsg( AFS_SUBSYSTEM_IO_PROCESSING,
137 AFS_TRACE_LEVEL_ERROR,
138 "AFSCachedRead (%08lX) CcCopyRead() Threw exception %wZ @ %0I64X Status %08lX\n",
140 &pFileObject->FileName,
141 liCurrentOffset.QuadPart,
145 if( !NT_SUCCESS( ntStatus))
148 try_return( ntStatus);
151 if( ulTotalLen <= ulCurrentIO)
156 liCurrentOffset.QuadPart += ulCurrentIO;
158 ulTotalLen -= ulCurrentIO;
160 pCurrentMdl = pCurrentMdl->Next;
165 if( NT_SUCCESS( ntStatus))
168 Irp->IoStatus.Information = ByteCount;
171 // Update the CBO if this is a sync read
177 pFileObject->CurrentByteOffset.QuadPart = StartingByte.QuadPart + ByteCount;
181 AFSCompleteRequest( Irp,
190 AFSNonCachedRead( IN PDEVICE_OBJECT DeviceObject,
192 IN LARGE_INTEGER StartingByte)
194 NTSTATUS ntStatus = STATUS_UNSUCCESSFUL;
195 IO_STACK_LOCATION *pIrpSp = IoGetCurrentIrpStackLocation( Irp);
196 PFILE_OBJECT pFileObject = pIrpSp->FileObject;
197 AFSFcb *pFcb = (AFSFcb *)pFileObject->FsContext;
198 AFSCcb *pCcb = (AFSCcb *)pFileObject->FsContext2;
199 BOOLEAN bSynchronousIo = IoIsOperationSynchronous(Irp);
200 VOID *pSystemBuffer = NULL;
201 BOOLEAN bPagingIo = BooleanFlagOn( Irp->Flags, IRP_PAGING_IO);
202 BOOLEAN bLocked = FALSE;
203 AFSGatherIo *pGatherIo = NULL;
204 AFSIoRun *pIoRuns = NULL;
205 AFSIoRun stIoRuns[AFS_MAX_STACK_IO_RUNS];
206 ULONG extentsCount = 0, runCount = 0;
207 AFSExtent *pStartExtent = NULL;
208 AFSExtent *pIgnoreExtent = NULL;
209 BOOLEAN bExtentsMapped = FALSE;
210 BOOLEAN bCompleteIrp = TRUE;
211 ULONG ulReadByteCount;
213 AFSDeviceExt *pDevExt = (AFSDeviceExt *)DeviceObject->DeviceExtension;
214 ULONG ulRequestCount = 0;
215 LARGE_INTEGER liCurrentTime, liLastRequestTime;
216 AFSDeviceExt *pControlDevExt = (AFSDeviceExt *)AFSControlDeviceObject->DeviceExtension;
217 PFILE_OBJECT pCacheFileObject = NULL;
222 ulByteCount = pIrpSp->Parameters.Read.Length;
224 if (ulByteCount > pDevExt->Specific.RDR.MaxIo.QuadPart)
227 AFSDbgLogMsg( AFS_SUBSYSTEM_IO_PROCESSING,
228 AFS_TRACE_LEVEL_ERROR,
229 "AFSNonCachedRead (%08lX) Request larger than MaxIO %I64X\n",
231 pDevExt->Specific.RDR.MaxIo.QuadPart);
233 try_return( ntStatus = STATUS_UNSUCCESSFUL);
236 pSystemBuffer = AFSLockSystemBuffer( Irp,
239 if( pSystemBuffer == NULL)
242 AFSDbgLogMsg( AFS_SUBSYSTEM_IO_PROCESSING,
243 AFS_TRACE_LEVEL_ERROR,
244 "AFSNonCachedRead (%08lX) Failed to map system buffer\n",
247 try_return( ntStatus = STATUS_INSUFFICIENT_RESOURCES);
250 if( StartingByte.QuadPart + ulByteCount > pFcb->Header.FileSize.QuadPart)
252 ULONG zeroCount = (ULONG) (StartingByte.QuadPart + ulByteCount - pFcb->Header.FileSize.QuadPart);
253 ulReadByteCount = (ULONG)(pFcb->Header.FileSize.QuadPart - StartingByte.QuadPart);
259 AFSDbgLogMsg( AFS_SUBSYSTEM_IO_PROCESSING,
260 AFS_TRACE_LEVEL_VERBOSE,
261 "AFSNonCachedRead (%08lX) Zeroing to EOF zero byte length %08lX\n",
265 RtlZeroMemory( ((PCHAR)pSystemBuffer) + ulReadByteCount, zeroCount);
269 ulReadByteCount = ulByteCount;
273 // Provoke a get of the extents - if we need to.
278 AFSDbgLogMsg( AFS_SUBSYSTEM_EXTENT_PROCESSING,
279 AFS_TRACE_LEVEL_VERBOSE,
280 "AFSNonCachedRead Requesting extents for fid %08lX-%08lX-%08lX-%08lX Offset %I64X Length %08lX\n",
281 pFcb->ObjectInformation->FileId.Cell,
282 pFcb->ObjectInformation->FileId.Volume,
283 pFcb->ObjectInformation->FileId.Vnode,
284 pFcb->ObjectInformation->FileId.Unique,
285 StartingByte.QuadPart,
288 ntStatus = AFSRequestExtentsAsync( pFcb,
293 if (!NT_SUCCESS(ntStatus))
296 AFSDbgLogMsg( AFS_SUBSYSTEM_EXTENT_PROCESSING,
297 AFS_TRACE_LEVEL_ERROR,
298 "AFSNonCachedRead (%08lX) Failed to request extents Status %08lX\n",
302 try_return( ntStatus);
305 KeQueryTickCount( &liLastRequestTime);
310 AFSDbgLogMsg( AFS_SUBSYSTEM_LOCK_PROCESSING,
311 AFS_TRACE_LEVEL_VERBOSE,
312 "AFSNonCachedRead Acquiring Fcb extents lock %08lX SHARED %08lX\n",
313 &pFcb->NPFcb->Specific.File.ExtentsResource,
314 PsGetCurrentThread());
316 AFSAcquireShared( &pFcb->NPFcb->Specific.File.ExtentsResource, TRUE );
320 pIgnoreExtent = NULL;
322 if( AFSDoExtentsMapRegion( pFcb,
331 KeClearEvent( &pFcb->NPFcb->Specific.File.ExtentsRequestComplete);
333 AFSDbgLogMsg( AFS_SUBSYSTEM_LOCK_PROCESSING,
334 AFS_TRACE_LEVEL_VERBOSE,
335 "AFSNonCachedRead Releasing Fcb extents lock %08lX SHARED %08lX\n",
336 &pFcb->NPFcb->Specific.File.ExtentsResource,
337 PsGetCurrentThread());
339 AFSReleaseResource( &pFcb->NPFcb->Specific.File.ExtentsResource );
343 // We will re-request the extents after 10 seconds of waiting for them
346 KeQueryTickCount( &liCurrentTime);
348 if( liCurrentTime.QuadPart - liLastRequestTime.QuadPart >= pControlDevExt->Specific.Control.ExtentRequestTimeCount.QuadPart)
351 AFSDbgLogMsg( AFS_SUBSYSTEM_EXTENT_PROCESSING,
352 AFS_TRACE_LEVEL_VERBOSE,
353 "AFSNonCachedRead Requesting extents, again, for fid %08lX-%08lX-%08lX-%08lX Offset %I64X Length %08lX\n",
354 pFcb->ObjectInformation->FileId.Cell,
355 pFcb->ObjectInformation->FileId.Volume,
356 pFcb->ObjectInformation->FileId.Vnode,
357 pFcb->ObjectInformation->FileId.Unique,
358 StartingByte.QuadPart,
361 ntStatus = AFSRequestExtentsAsync( pFcb,
366 if (!NT_SUCCESS(ntStatus))
369 AFSDbgLogMsg( AFS_SUBSYSTEM_IO_PROCESSING,
370 AFS_TRACE_LEVEL_ERROR,
371 "AFSNonCachedRead (%08lX) Failed to request extents Status %08lX\n",
375 try_return( ntStatus);
378 KeQueryTickCount( &liLastRequestTime);
381 AFSDbgLogMsg( AFS_SUBSYSTEM_EXTENT_PROCESSING,
382 AFS_TRACE_LEVEL_VERBOSE,
383 "AFSNonCachedRead Waiting for extents for fid %08lX-%08lX-%08lX-%08lX Offset %I64X Length %08lX\n",
384 pFcb->ObjectInformation->FileId.Cell,
385 pFcb->ObjectInformation->FileId.Volume,
386 pFcb->ObjectInformation->FileId.Vnode,
387 pFcb->ObjectInformation->FileId.Unique,
388 StartingByte.QuadPart,
391 ntStatus = AFSWaitForExtentMapping ( pFcb, pCcb);
393 if (!NT_SUCCESS(ntStatus))
396 AFSDbgLogMsg( AFS_SUBSYSTEM_EXTENT_PROCESSING,
397 AFS_TRACE_LEVEL_ERROR,
398 "AFSNonCachedRead Failed wait for extents for fid %08lX-%08lX-%08lX-%08lX Offset %I64X Length %08lX Status %08lX\n",
399 pFcb->ObjectInformation->FileId.Cell,
400 pFcb->ObjectInformation->FileId.Volume,
401 pFcb->ObjectInformation->FileId.Vnode,
402 pFcb->ObjectInformation->FileId.Unique,
403 StartingByte.QuadPart,
407 try_return( ntStatus);
412 // At this stage we know that the extents are fully mapped and
413 // that, because we took a reference they won't be unmapped.
414 // Thus the list will not move between the start and end.
417 AFSDbgLogMsg( AFS_SUBSYSTEM_EXTENT_PROCESSING,
418 AFS_TRACE_LEVEL_VERBOSE,
419 "AFSNonCachedRead Extents mapped for fid %08lX-%08lX-%08lX-%08lX Offset %I64X Length %08lX\n",
420 pFcb->ObjectInformation->FileId.Cell,
421 pFcb->ObjectInformation->FileId.Volume,
422 pFcb->ObjectInformation->FileId.Vnode,
423 pFcb->ObjectInformation->FileId.Unique,
424 StartingByte.QuadPart,
427 ntStatus = AFSGetExtents( pFcb,
434 if (!NT_SUCCESS(ntStatus))
437 AFSDbgLogMsg( AFS_SUBSYSTEM_EXTENT_PROCESSING,
438 AFS_TRACE_LEVEL_ERROR,
439 "AFSNonCachedRead (%08lX) Failed to retrieve mapped extents Status %08lX\n",
443 try_return( ntStatus );
446 AFSDbgLogMsg( AFS_SUBSYSTEM_IO_PROCESSING,
447 AFS_TRACE_LEVEL_VERBOSE,
448 "AFSNonCachedRead (%08lX) Successfully retrieved map extents count %08lX run count %08lX\n",
453 if( BooleanFlagOn( AFSLibControlFlags, AFS_REDIR_LIB_FLAGS_NONPERSISTENT_CACHE))
456 Irp->IoStatus.Information = ulReadByteCount;
458 ntStatus = AFSProcessExtentRun( pSystemBuffer,
464 if (!NT_SUCCESS(ntStatus))
467 AFSDbgLogMsg( AFS_SUBSYSTEM_IO_PROCESSING,
468 AFS_TRACE_LEVEL_ERROR,
469 "AFSNonCachedRead (%08lX) Failed to process extent run for non-persistent cache Status %08lX\n",
474 try_return( ntStatus);
478 // Retrieve the cache file object
481 pCacheFileObject = AFSReferenceCacheFileObject();
483 if( pCacheFileObject == NULL)
486 ntStatus = STATUS_DEVICE_NOT_READY;
488 AFSDbgLogMsg( AFS_SUBSYSTEM_EXTENT_PROCESSING,
489 AFS_TRACE_LEVEL_ERROR,
490 "AFSNonCachedRead Failed to retrieve cache fileobject for fid %08lX-%08lX-%08lX-%08lX Offset %I64X Length %08lX Status %08lX\n",
491 pFcb->ObjectInformation->FileId.Cell,
492 pFcb->ObjectInformation->FileId.Volume,
493 pFcb->ObjectInformation->FileId.Vnode,
494 pFcb->ObjectInformation->FileId.Unique,
495 StartingByte.QuadPart,
499 try_return( ntStatus);
502 if (runCount > AFS_MAX_STACK_IO_RUNS)
505 pIoRuns = (AFSIoRun*) AFSExAllocatePoolWithTag( PagedPool,
506 runCount * sizeof( AFSIoRun ),
511 AFSDbgLogMsg( AFS_SUBSYSTEM_IO_PROCESSING,
512 AFS_TRACE_LEVEL_ERROR,
513 "AFSNonCachedRead (%08lX) Failed to allocate IO run block\n",
516 try_return( ntStatus = STATUS_INSUFFICIENT_RESOURCES );
525 RtlZeroMemory( pIoRuns, runCount * sizeof( AFSIoRun ));
527 ntStatus = AFSSetupIoRun( IoGetRelatedDeviceObject( pCacheFileObject),
536 if (!NT_SUCCESS(ntStatus))
539 AFSDbgLogMsg( AFS_SUBSYSTEM_IO_PROCESSING,
540 AFS_TRACE_LEVEL_ERROR,
541 "AFSNonCachedRead (%08lX) Failed to initialize IO run block Status %08lX\n",
545 try_return( ntStatus );
548 AFSReferenceActiveExtents( pStartExtent,
551 AFSDbgLogMsg( AFS_SUBSYSTEM_LOCK_PROCESSING,
552 AFS_TRACE_LEVEL_VERBOSE,
553 "AFSNonCachedRead Releasing Fcb extents lock %08lX SHARED %08lX\n",
554 &pFcb->NPFcb->Specific.File.ExtentsResource,
555 PsGetCurrentThread());
557 AFSReleaseResource( &pFcb->NPFcb->Specific.File.ExtentsResource );
560 pGatherIo = (AFSGatherIo*) AFSExAllocatePoolWithTag( NonPagedPool,
561 sizeof( AFSGatherIo ),
564 if (NULL == pGatherIo)
567 AFSDbgLogMsg( AFS_SUBSYSTEM_IO_PROCESSING,
568 AFS_TRACE_LEVEL_ERROR,
569 "AFSNonCachedRead (%08lX) Failed to allocate IO gather block\n",
572 AFSDbgLogMsg( AFS_SUBSYSTEM_LOCK_PROCESSING,
573 AFS_TRACE_LEVEL_VERBOSE,
574 "AFSNonCachedRead Acquiring(2) Fcb extents lock %08lX SHARED %08lX\n",
575 &pFcb->NPFcb->Specific.File.ExtentsResource,
576 PsGetCurrentThread());
578 AFSAcquireShared( &pFcb->NPFcb->Specific.File.ExtentsResource,
582 AFSDereferenceActiveExtents( pStartExtent,
585 try_return (ntStatus = STATUS_INSUFFICIENT_RESOURCES );
588 RtlZeroMemory( pGatherIo, sizeof( AFSGatherIo ));
591 // Initialize count to 1, that was we won't get an early
592 // completion if the first irp completes before the second is
595 pGatherIo->Count = 1;
596 pGatherIo->Status = STATUS_SUCCESS;
597 pGatherIo->MasterIrp = Irp;
598 pGatherIo->Synchronous = TRUE;
599 pGatherIo->Fcb = pFcb;
600 pGatherIo->CompleteMasterIrp = FALSE;
604 if (pGatherIo->Synchronous)
606 KeInitializeEvent( &pGatherIo->Event, NotificationEvent, FALSE );
610 IoMarkIrpPending( Irp);
614 // Pre-emptively set up the count
617 Irp->IoStatus.Information = ulByteCount;
619 ntStatus = AFSQueueStartIos( pCacheFileObject,
621 IRP_READ_OPERATION | IRP_SYNCHRONOUS_API,
626 AFSDbgLogMsg( AFS_SUBSYSTEM_IO_PROCESSING,
627 AFS_TRACE_LEVEL_VERBOSE,
628 "AFSNonCachedRead (%08lX) AFSStartIos completed Status %08lX\n",
632 if( !NT_SUCCESS( ntStatus))
635 AFSDbgLogMsg( AFS_SUBSYSTEM_LOCK_PROCESSING,
636 AFS_TRACE_LEVEL_VERBOSE,
637 "AFSNonCachedRead Acquiring(3) Fcb extents lock %08lX SHARED %08lX\n",
638 &pFcb->NPFcb->Specific.File.ExtentsResource,
639 PsGetCurrentThread());
641 AFSAcquireShared( &pFcb->NPFcb->Specific.File.ExtentsResource,
645 AFSDereferenceActiveExtents( pStartExtent,
648 try_return( ntStatus);
652 // Wait for completion of all IOs we started.
654 (VOID) KeWaitForSingleObject( &pGatherIo->Event,
660 ntStatus = pGatherIo->Status;
662 AFSDbgLogMsg( AFS_SUBSYSTEM_IO_PROCESSING,
663 AFS_TRACE_LEVEL_VERBOSE,
664 "AFSNonCachedRead (%08lX) AFSStartIos wait completed Status %08lX\n",
668 AFSDbgLogMsg( AFS_SUBSYSTEM_LOCK_PROCESSING,
669 AFS_TRACE_LEVEL_VERBOSE,
670 "AFSNonCachedRead Acquiring(4) Fcb extents lock %08lX SHARED %08lX\n",
671 &pFcb->NPFcb->Specific.File.ExtentsResource,
672 PsGetCurrentThread());
674 AFSAcquireShared( &pFcb->NPFcb->Specific.File.ExtentsResource,
678 AFSDereferenceActiveExtents( pStartExtent,
681 AFSDbgLogMsg( AFS_SUBSYSTEM_LOCK_PROCESSING,
682 AFS_TRACE_LEVEL_VERBOSE,
683 "AFSNonCachedRead Releasing Fcb extents lock %08lX SHARED %08lX\n",
684 &pFcb->NPFcb->Specific.File.ExtentsResource,
685 PsGetCurrentThread());
687 AFSReleaseResource( &pFcb->NPFcb->Specific.File.ExtentsResource );
691 // The data is there now. Give back the extents now so the service
695 if ( pFcb->Specific.File.ExtentLength > 4096)
698 (VOID) AFSReleaseExtentsWithFlush( pFcb,
705 if( pCacheFileObject != NULL)
707 AFSReleaseCacheFileObject( pCacheFileObject);
710 AFSDbgLogMsg( AFS_SUBSYSTEM_IO_PROCESSING,
711 AFS_TRACE_LEVEL_VERBOSE,
712 "AFSNonCachedRead (%08lX) Completed request Status %08lX\n",
716 if (NT_SUCCESS(ntStatus) &&
718 BooleanFlagOn( pFileObject->Flags, FO_SYNCHRONOUS_IO))
721 // Update the CBO if this is a sync, nopaging read
723 pFileObject->CurrentByteOffset.QuadPart = StartingByte.QuadPart + ulByteCount;
729 AFSDbgLogMsg( AFS_SUBSYSTEM_LOCK_PROCESSING,
730 AFS_TRACE_LEVEL_VERBOSE,
731 "AFSNonCachedRead Releasing Fcb extents lock %08lX SHARED %08lX\n",
732 &pFcb->NPFcb->Specific.File.ExtentsResource,
733 PsGetCurrentThread());
735 AFSReleaseResource( &pFcb->NPFcb->Specific.File.ExtentsResource );
740 AFSExFreePoolWithTag(pGatherIo, AFS_GATHER_TAG);
743 if (NULL != pIoRuns && stIoRuns != pIoRuns)
745 AFSExFreePoolWithTag(pIoRuns, AFS_IO_RUN_TAG);
751 AFSDbgLogMsg( AFS_SUBSYSTEM_IO_PROCESSING,
752 AFS_TRACE_LEVEL_VERBOSE,
753 "AFSNonCachedRead Completing irp %08lX Status %08lX Info %08lX\n",
756 Irp->IoStatus.Information);
758 AFSCompleteRequest( Irp, ntStatus );
764 // Function: AFSDispatch
768 // A shim around AFSCommonRead (qv)
771 AFSRead( IN PDEVICE_OBJECT LibDeviceObject,
775 NTSTATUS ntStatus = STATUS_SUCCESS;
780 ntStatus = AFSCommonRead( AFSRDRDeviceObject, Irp, NULL);
782 __except( AFSExceptionFilter( __FUNCTION__, GetExceptionCode(), GetExceptionInformation()) )
785 ntStatus = STATUS_INSUFFICIENT_RESOURCES;
787 AFSDumpTraceFilesFnc();
797 // This function is a slightly widened Dispatch handler for the
798 // AFS Read function. The third parameter is NULL if we were called
799 // at our dispatch point and a process handle if we have been posted.
801 // After doing the obvious (completing MDL writes and so forth)
802 // we then post, or not.
806 // A status is returned for the function
810 AFSCommonRead( IN PDEVICE_OBJECT DeviceObject,
812 IN HANDLE OnBehalfOf)
815 NTSTATUS ntStatus = STATUS_SUCCESS;
816 AFSDeviceExt *pDeviceExt;
817 IO_STACK_LOCATION *pIrpSp;
820 BOOLEAN bReleaseMain = FALSE;
821 BOOLEAN bReleaseSectionObject = FALSE;
822 BOOLEAN bReleasePaging = FALSE;
823 BOOLEAN bPagingIo = FALSE;
824 BOOLEAN bNonCachedIo = FALSE;
825 BOOLEAN bCompleteIrp = TRUE;
827 PFILE_OBJECT pFileObject = NULL;
828 LARGE_INTEGER liStartingByte;
830 VOID *pSystemBuffer = NULL;
832 pIrpSp = IoGetCurrentIrpStackLocation( Irp);
833 pDeviceExt = (AFSDeviceExt *)DeviceObject->DeviceExtension;
838 // Decode the fileobject
840 pFileObject = pIrpSp->FileObject;
843 // There is a risk (albeit infinitely small) that the Irp will
844 // complete before this function exits, then a cleanup and
845 // close will happen and the FCB will be torn down before we
846 // get to the try_exit. Pin the file Object which will pin the FCB
849 ObReferenceObject( pFileObject);
852 // If we are in shutdown mode then fail the request
855 if( BooleanFlagOn( pDeviceExt->DeviceFlags, AFS_DEVICE_FLAG_REDIRECTOR_SHUTDOWN))
858 AFSDbgLogMsg( AFS_SUBSYSTEM_FILE_PROCESSING,
859 AFS_TRACE_LEVEL_WARNING,
860 "AFSCommonRead (%08lX) Open request after shutdown\n",
863 try_return( ntStatus = STATUS_TOO_LATE);
866 pFcb = (AFSFcb *)pFileObject->FsContext;
868 pCcb = (AFSCcb *)pFileObject->FsContext2;
873 AFSDbgLogMsg( AFS_SUBSYSTEM_IO_PROCESSING,
874 AFS_TRACE_LEVEL_ERROR,
875 "AFSCommonRead Attempted read (%08lX) when pFcb == NULL\n",
878 try_return( ntStatus = STATUS_INVALID_DEVICE_REQUEST);
881 bPagingIo = BooleanFlagOn( Irp->Flags, IRP_PAGING_IO);
882 bNonCachedIo = BooleanFlagOn( Irp->Flags,IRP_NOCACHE);
884 liStartingByte = pIrpSp->Parameters.Read.ByteOffset;
885 ulByteCount = pIrpSp->Parameters.Read.Length;
887 if( pFcb->Header.NodeTypeCode != AFS_IOCTL_FCB &&
888 pFcb->Header.NodeTypeCode != AFS_FILE_FCB &&
889 pFcb->Header.NodeTypeCode != AFS_SPECIAL_SHARE_FCB)
892 AFSDbgLogMsg( AFS_SUBSYSTEM_IO_PROCESSING,
893 AFS_TRACE_LEVEL_ERROR,
894 "AFSCommonRead Attempted read (%08lX) on an invalid node type %08lX\n",
896 pFcb->Header.NodeTypeCode);
898 try_return( ntStatus = STATUS_INVALID_DEVICE_REQUEST);
902 // If this is a read against an IOCtl node then handle it
903 // in a different pathway
906 if( pFcb->Header.NodeTypeCode == AFS_IOCTL_FCB)
909 AFSDbgLogMsg( AFS_SUBSYSTEM_IO_PROCESSING,
910 AFS_TRACE_LEVEL_VERBOSE,
911 "AFSCommonRead (%08lX) Processing file (PIOCTL) Offset %I64X Length %08lX Irp Flags %08lX\n",
913 liStartingByte.QuadPart,
917 ntStatus = AFSIOCtlRead( DeviceObject,
920 try_return( ntStatus);
922 else if( pFcb->Header.NodeTypeCode == AFS_SPECIAL_SHARE_FCB)
925 AFSDbgLogMsg( AFS_SUBSYSTEM_IO_PROCESSING,
926 AFS_TRACE_LEVEL_VERBOSE,
927 "AFSCommonRead (%08lX) Processing file (SHARE) Offset %I64X Length %08lX Irp Flags %08lX\n",
929 liStartingByte.QuadPart,
933 ntStatus = AFSShareRead( DeviceObject,
936 try_return( ntStatus);
940 // No fileobject yet? Bail.
942 if( !BooleanFlagOn( AFSLibControlFlags, AFS_REDIR_LIB_FLAGS_NONPERSISTENT_CACHE) &&
943 NULL == pDeviceExt->Specific.RDR.CacheFileObject)
946 AFSDbgLogMsg( AFS_SUBSYSTEM_IO_PROCESSING,
947 AFS_TRACE_LEVEL_ERROR,
948 "AFSCommonRead (%08lX) Request failed due to AFS cache closed\n",
951 try_return( ntStatus = STATUS_TOO_LATE );
954 if( pIrpSp->Parameters.Read.Length == 0)
957 AFSDbgLogMsg( AFS_SUBSYSTEM_IO_PROCESSING,
958 AFS_TRACE_LEVEL_VERBOSE,
959 "AFSCommonRead (%08lX) Request completed due to zero length\n",
962 try_return( ntStatus);
965 if ( FlagOn(pIrpSp->MinorFunction, IRP_MN_COMPLETE) )
968 AFSDbgLogMsg( AFS_SUBSYSTEM_LOCK_PROCESSING,
969 AFS_TRACE_LEVEL_VERBOSE,
970 "AFSCommonRead Acquiring Fcb SectionObject lock %08lX SHARED %08lX\n",
971 &pFcb->NPFcb->SectionObjectResource,
972 PsGetCurrentThread());
974 AFSAcquireShared( &pFcb->NPFcb->SectionObjectResource,
977 bReleaseSectionObject = TRUE;
979 AFSDbgLogMsg( AFS_SUBSYSTEM_IO_PROCESSING,
980 AFS_TRACE_LEVEL_VERBOSE,
981 "AFSCommonRead (%08lX) IRP_MN_COMPLETE being processed\n",
984 CcMdlReadComplete(pIrpSp->FileObject, Irp->MdlAddress);
987 // Mdl is now Deallocated
990 Irp->MdlAddress = NULL;
992 try_return( ntStatus = STATUS_SUCCESS );
996 // If we get a non cached IO for a cached file we should do a purge.
997 // For now we will just promote to cached
999 if (NULL != pFileObject->SectionObjectPointer->DataSectionObject && !bPagingIo)
1002 bNonCachedIo = FALSE;
1006 // We acquire the main/paging resource first to synchronize
1007 // against size checks.
1013 AFSDbgLogMsg( AFS_SUBSYSTEM_LOCK_PROCESSING,
1014 AFS_TRACE_LEVEL_VERBOSE,
1015 "AFSCommonRead Acquiring Fcb PagingIo lock %08lX SHARED %08lX\n",
1016 &pFcb->NPFcb->PagingResource,
1017 PsGetCurrentThread());
1019 AFSAcquireShared( &pFcb->NPFcb->PagingResource,
1022 bReleasePaging = TRUE;
1024 AFSDbgLogMsg( AFS_SUBSYSTEM_LOCK_PROCESSING,
1025 AFS_TRACE_LEVEL_VERBOSE,
1026 "AFSCommonRead Acquiring Fcb SectionObject lock %08lX SHARED %08lX\n",
1027 &pFcb->NPFcb->SectionObjectResource,
1028 PsGetCurrentThread());
1030 AFSAcquireShared( &pFcb->NPFcb->SectionObjectResource,
1033 bReleaseSectionObject = TRUE;
1039 AFSDbgLogMsg( AFS_SUBSYSTEM_LOCK_PROCESSING,
1040 AFS_TRACE_LEVEL_VERBOSE,
1041 "AFSCommonRead Acquiring Fcb SectionObject lock %08lX SHARED %08lX\n",
1042 &pFcb->NPFcb->SectionObjectResource,
1043 PsGetCurrentThread());
1045 AFSAcquireShared( &pFcb->NPFcb->SectionObjectResource,
1048 bReleaseSectionObject = TRUE;
1051 // Check the BR locks
1054 if( !FsRtlCheckLockForReadAccess( &pFcb->Specific.File.FileLock,
1058 AFSDbgLogMsg( AFS_SUBSYSTEM_IO_PROCESSING,
1059 AFS_TRACE_LEVEL_ERROR,
1060 "AFSCommonRead (%08lX) Request failed due to lock conflict\n",
1063 try_return( ntStatus = STATUS_FILE_LOCK_CONFLICT);
1067 if( BooleanFlagOn( pCcb->DirectoryCB->Flags, AFS_DIR_ENTRY_DELETED) ||
1068 BooleanFlagOn( pFcb->ObjectInformation->Flags, AFS_OBJECT_FLAGS_DELETED))
1071 AFSDbgLogMsg( AFS_SUBSYSTEM_IO_PROCESSING,
1072 AFS_TRACE_LEVEL_ERROR,
1073 "AFSCommonRead (%08lX) Request failed due to file deleted\n",
1076 try_return( ntStatus = STATUS_FILE_DELETED);
1080 // If the read starts beyond End of File, return EOF.
1083 if( liStartingByte.QuadPart >= pFcb->Header.FileSize.QuadPart)
1086 AFSDbgLogMsg( AFS_SUBSYSTEM_IO_PROCESSING,
1087 AFS_TRACE_LEVEL_VERBOSE,
1088 "AFSCommonRead (%08lX) Request beyond EOF %I64X\n",
1090 pFcb->Header.FileSize.QuadPart);
1092 try_return( ntStatus = STATUS_END_OF_FILE);
1098 if( liStartingByte.QuadPart + ulByteCount > pFcb->Header.FileSize.QuadPart)
1101 ulByteCount = (ULONG)(pFcb->Header.FileSize.QuadPart - liStartingByte.QuadPart);
1103 AFSDbgLogMsg( AFS_SUBSYSTEM_IO_PROCESSING,
1104 AFS_TRACE_LEVEL_VERBOSE,
1105 "AFSCommonRead (%08lX) Truncated read request to %08lX\n",
1111 // Is this Fcb valid???
1114 if( BooleanFlagOn( pFcb->ObjectInformation->Flags, AFS_OBJECT_FLAGS_OBJECT_INVALID))
1117 AFSDbgLogMsg( AFS_SUBSYSTEM_IO_PROCESSING,
1118 AFS_TRACE_LEVEL_ERROR,
1119 "AFSCommonRead (%08lX) Failing request due to INVALID fcb\n",
1122 Irp->IoStatus.Information = 0;
1124 try_return( ntStatus = STATUS_FILE_DELETED);
1128 // If this is an cached IO
1130 if( (!bPagingIo && !bNonCachedIo))
1133 // This is a top level irp. Init the caching if it has not yet
1134 // been initialzed for this FO
1137 if( pFileObject->PrivateCacheMap == NULL)
1143 AFSDbgLogMsg( AFS_SUBSYSTEM_IO_PROCESSING,
1144 AFS_TRACE_LEVEL_VERBOSE,
1145 "AFSCommonRead Initialize caching on Fcb %08lX FO %08lX\n",
1149 CcInitializeCacheMap( pFileObject,
1150 (PCC_FILE_SIZES)&pFcb->Header.AllocationSize,
1152 AFSLibCacheManagerCallbacks,
1155 CcSetReadAheadGranularity( pFileObject,
1156 pDeviceExt->Specific.RDR.MaximumRPCLength);
1158 CcSetDirtyPageThreshold( pFileObject,
1159 AFS_DIRTY_CHUNK_THRESHOLD * pDeviceExt->Specific.RDR.MaximumRPCLength);
1161 __except( EXCEPTION_EXECUTE_HANDLER)
1164 ntStatus = GetExceptionCode();
1166 AFSDbgLogMsg( AFS_SUBSYSTEM_IO_PROCESSING,
1167 AFS_TRACE_LEVEL_ERROR,
1168 "AFSCommonRead (%08lX) Exception thrown while initializing cache map Status %08lX\n",
1173 if( !NT_SUCCESS( ntStatus))
1176 try_return( ntStatus);
1181 // And if this is MDL operation, deal with it now (yes we
1182 // could post, but we are almost certainly posted
1183 // already and we don't want to grow and SVA for it..)
1186 if( BooleanFlagOn( pIrpSp->MinorFunction, IRP_MN_MDL))
1191 CcMdlRead( pFileObject,
1196 ntStatus = Irp->IoStatus.Status;
1198 __except( EXCEPTION_EXECUTE_HANDLER)
1200 ntStatus = GetExceptionCode();
1202 AFSDbgLogMsg( AFS_SUBSYSTEM_IO_PROCESSING,
1203 AFS_TRACE_LEVEL_ERROR,
1204 "AFSCommonRead (%08lX) Exception thrown during mdl read Status %08lX\n",
1209 if( !NT_SUCCESS( ntStatus))
1212 AFSDbgLogMsg( AFS_SUBSYSTEM_IO_PROCESSING,
1213 AFS_TRACE_LEVEL_ERROR,
1214 "AFSCommonRead (%08lX) Failed to process MDL read Status %08lX\n",
1218 try_return( ntStatus);
1222 // Update the CBO if this is a sync read
1225 if( BooleanFlagOn( pFileObject->Flags, FO_SYNCHRONOUS_IO))
1228 pFileObject->CurrentByteOffset.QuadPart = liStartingByte.QuadPart + ulByteCount;
1231 try_return( ntStatus);
1236 // The called request completes the IRP for us.
1239 bCompleteIrp = FALSE;
1245 AFSDbgLogMsg( AFS_SUBSYSTEM_IO_PROCESSING,
1246 AFS_TRACE_LEVEL_VERBOSE,
1247 "AFSCommonRead (%08lX) Processing CACHED request Offset %I64X Len %08lX\n",
1249 liStartingByte.QuadPart,
1252 ntStatus = AFSCachedRead( DeviceObject, Irp, liStartingByte, ulByteCount);
1260 AFSReleaseResource( &pFcb->NPFcb->PagingResource);
1262 bReleasePaging = FALSE;
1265 if( bReleaseSectionObject)
1268 AFSReleaseResource( &pFcb->NPFcb->SectionObjectResource);
1270 bReleaseSectionObject = FALSE;
1276 AFSReleaseResource( &pFcb->NPFcb->Resource);
1278 bReleaseMain = FALSE;
1281 AFSDbgLogMsg( AFS_SUBSYSTEM_IO_PROCESSING,
1282 AFS_TRACE_LEVEL_VERBOSE,
1283 "AFSCommonRead (%08lX) Processing NON-CACHED request Offset %I64X Len %08lX\n",
1285 liStartingByte.QuadPart,
1288 ntStatus = AFSNonCachedRead( DeviceObject, Irp, liStartingByte);
1293 AFSDbgLogMsg( AFS_SUBSYSTEM_IO_PROCESSING,
1294 AFS_TRACE_LEVEL_VERBOSE,
1295 "AFSCommonRead (%08lX) Process complete Status %08lX\n",
1302 AFSReleaseResource( &pFcb->NPFcb->PagingResource);
1305 if( bReleaseSectionObject)
1308 AFSReleaseResource( &pFcb->NPFcb->SectionObjectResource);
1314 AFSReleaseResource( &pFcb->NPFcb->Resource);
1320 AFSCompleteRequest( Irp, ntStatus);
1323 ObDereferenceObject( pFileObject);
1330 AFSIOCtlRead( IN PDEVICE_OBJECT DeviceObject,
1334 NTSTATUS ntStatus = STATUS_SUCCESS;
1335 AFSPIOCtlIORequestCB stIORequestCB;
1336 PIO_STACK_LOCATION pIrpSp = IoGetCurrentIrpStackLocation( Irp);
1337 AFSFcb *pFcb = NULL;
1338 AFSCcb *pCcb = NULL;
1339 AFSPIOCtlIOResultCB stIOResultCB;
1340 ULONG ulBytesReturned = 0;
1341 AFSFileID stParentFID;
1346 RtlZeroMemory( &stIORequestCB,
1347 sizeof( AFSPIOCtlIORequestCB));
1349 if( pIrpSp->Parameters.Read.Length == 0)
1353 // Nothing to do in this case
1356 try_return( ntStatus);
1359 pFcb = (AFSFcb *)pIrpSp->FileObject->FsContext;
1361 pCcb = (AFSCcb *)pIrpSp->FileObject->FsContext2;
1363 AFSDbgLogMsg( AFS_SUBSYSTEM_LOCK_PROCESSING,
1364 AFS_TRACE_LEVEL_VERBOSE,
1365 "AFSIOCtlRead Acquiring Fcb lock %08lX SHARED %08lX\n",
1366 &pFcb->NPFcb->Resource,
1367 PsGetCurrentThread());
1369 AFSAcquireShared( &pFcb->NPFcb->Resource,
1373 // Get the parent fid to pass to the cm
1376 RtlZeroMemory( &stParentFID,
1377 sizeof( AFSFileID));
1379 if( pFcb->ObjectInformation->ParentObjectInformation != NULL)
1383 // The parent directory FID of the node
1386 ASSERT( pFcb->ObjectInformation->ParentObjectInformation->FileType == AFS_FILE_TYPE_DIRECTORY);
1388 stParentFID = pFcb->ObjectInformation->ParentObjectInformation->FileId;
1392 // Set the control block up
1395 stIORequestCB.RequestId = pCcb->RequestID;
1397 if( pFcb->ObjectInformation->VolumeCB != NULL)
1399 stIORequestCB.RootId = pFcb->ObjectInformation->VolumeCB->ObjectInformation.FileId;
1403 // Lock down the buffer
1406 stIORequestCB.MappedBuffer = AFSMapToService( Irp,
1407 pIrpSp->Parameters.Read.Length);
1409 if( stIORequestCB.MappedBuffer == NULL)
1412 try_return( ntStatus = STATUS_INSUFFICIENT_RESOURCES);
1415 stIORequestCB.BufferLength = pIrpSp->Parameters.Read.Length;
1417 stIOResultCB.BytesProcessed = 0;
1419 ulBytesReturned = sizeof( AFSPIOCtlIOResultCB);
1422 // Issue the request to the service
1425 ntStatus = AFSProcessRequest( AFS_REQUEST_TYPE_PIOCTL_READ,
1426 AFS_REQUEST_FLAG_SYNCHRONOUS,
1430 (void *)&stIORequestCB,
1431 sizeof( AFSPIOCtlIORequestCB),
1435 if( !NT_SUCCESS( ntStatus))
1438 try_return( ntStatus);
1442 // Update the length read
1445 Irp->IoStatus.Information = stIOResultCB.BytesProcessed;
1449 if( stIORequestCB.MappedBuffer != NULL)
1452 AFSUnmapServiceMappedBuffer( stIORequestCB.MappedBuffer,
1459 AFSReleaseResource( &pFcb->NPFcb->Resource);
1467 AFSShareRead( IN PDEVICE_OBJECT DeviceObject,
1471 NTSTATUS ntStatus = STATUS_SUCCESS;
1472 AFSPIOCtlIORequestCB stIORequestCB;
1473 PIO_STACK_LOCATION pIrpSp = IoGetCurrentIrpStackLocation( Irp);
1474 AFSFcb *pFcb = NULL;
1475 AFSCcb *pCcb = NULL;
1476 ULONG ulBytesReturned = 0;
1477 void *pBuffer = NULL;
1478 AFSPipeIORequestCB stIoRequest;
1483 pFcb = (AFSFcb *)pIrpSp->FileObject->FsContext;
1485 pCcb = (AFSCcb *)pIrpSp->FileObject->FsContext2;
1487 AFSDbgLogMsg( AFS_SUBSYSTEM_PIPE_PROCESSING,
1488 AFS_TRACE_LEVEL_VERBOSE,
1489 "AFSShareRead On pipe %wZ Length %08lX\n",
1490 &pCcb->DirectoryCB->NameInformation.FileName,
1491 pIrpSp->Parameters.Read.Length);
1493 if( pIrpSp->Parameters.Read.Length == 0)
1497 // Nothing to do in this case
1500 try_return( ntStatus);
1503 AFSAcquireShared( &pFcb->NPFcb->Resource,
1507 // Retrieve the buffer for the read request
1510 pBuffer = AFSLockSystemBuffer( Irp,
1511 pIrpSp->Parameters.Read.Length);
1513 if( pBuffer == NULL)
1516 AFSDbgLogMsg( AFS_SUBSYSTEM_PIPE_PROCESSING,
1517 AFS_TRACE_LEVEL_ERROR,
1518 "AFSShareRead Failed to map buffer on pipe %wZ\n",
1519 &pCcb->DirectoryCB->NameInformation.FileName);
1521 try_return( ntStatus = STATUS_INSUFFICIENT_RESOURCES);
1524 RtlZeroMemory( &stIoRequest,
1525 sizeof( AFSPipeIORequestCB));
1527 stIoRequest.RequestId = pCcb->RequestID;
1529 stIoRequest.RootId = pFcb->ObjectInformation->VolumeCB->ObjectInformation.FileId;
1531 stIoRequest.BufferLength = pIrpSp->Parameters.Read.Length;
1533 ulBytesReturned = pIrpSp->Parameters.Read.Length;
1536 // Issue the open request to the service
1539 ntStatus = AFSProcessRequest( AFS_REQUEST_TYPE_PIPE_READ,
1540 AFS_REQUEST_FLAG_SYNCHRONOUS,
1542 &pCcb->DirectoryCB->NameInformation.FileName,
1544 (void *)&stIoRequest,
1545 sizeof( AFSPipeIORequestCB),
1549 if( !NT_SUCCESS( ntStatus) &&
1550 ntStatus != STATUS_BUFFER_OVERFLOW)
1553 AFSDbgLogMsg( AFS_SUBSYSTEM_FILE_PROCESSING,
1554 AFS_TRACE_LEVEL_ERROR,
1555 "AFSShareRead (%08lX) Failed service read Status %08lX\n",
1559 try_return( ntStatus);
1562 AFSDbgLogMsg( AFS_SUBSYSTEM_PIPE_PROCESSING,
1563 AFS_TRACE_LEVEL_VERBOSE,
1564 "AFSShareRead Completed on pipe %wZ Length read %08lX Status %08lX\n",
1565 &pCcb->DirectoryCB->NameInformation.FileName,
1569 Irp->IoStatus.Information = ulBytesReturned;
1576 AFSReleaseResource( &pFcb->NPFcb->Resource);