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 bReleasePaging = FALSE;
822 BOOLEAN bPagingIo = FALSE;
823 BOOLEAN bNonCachedIo = FALSE;
824 BOOLEAN bCompleteIrp = TRUE;
826 PFILE_OBJECT pFileObject = NULL;
827 LARGE_INTEGER liStartingByte;
829 VOID *pSystemBuffer = NULL;
831 pIrpSp = IoGetCurrentIrpStackLocation( Irp);
832 pDeviceExt = (AFSDeviceExt *)DeviceObject->DeviceExtension;
837 // Decode the fileobject
839 pFileObject = pIrpSp->FileObject;
842 // There is a risk (albeit infinitely small) that the Irp will
843 // complete before this function exits, then a cleanup and
844 // close will happen and the FCB will be torn down before we
845 // get to the try_exit. Pin the file Object which will pin the FCB
848 ObReferenceObject( pFileObject);
851 // If we are in shutdown mode then fail the request
854 if( BooleanFlagOn( pDeviceExt->DeviceFlags, AFS_DEVICE_FLAG_REDIRECTOR_SHUTDOWN))
857 AFSDbgLogMsg( AFS_SUBSYSTEM_FILE_PROCESSING,
858 AFS_TRACE_LEVEL_WARNING,
859 "AFSCommonRead (%08lX) Open request after shutdown\n",
862 try_return( ntStatus = STATUS_TOO_LATE);
865 pFcb = (AFSFcb *)pFileObject->FsContext;
867 pCcb = (AFSCcb *)pFileObject->FsContext2;
872 AFSDbgLogMsg( AFS_SUBSYSTEM_IO_PROCESSING,
873 AFS_TRACE_LEVEL_ERROR,
874 "AFSCommonRead Attempted read (%08lX) when pFcb == NULL\n",
877 try_return( ntStatus = STATUS_INVALID_DEVICE_REQUEST);
880 bPagingIo = BooleanFlagOn( Irp->Flags, IRP_PAGING_IO);
881 bNonCachedIo = BooleanFlagOn( Irp->Flags,IRP_NOCACHE);
883 liStartingByte = pIrpSp->Parameters.Read.ByteOffset;
884 ulByteCount = pIrpSp->Parameters.Read.Length;
886 if( pFcb->Header.NodeTypeCode != AFS_IOCTL_FCB &&
887 pFcb->Header.NodeTypeCode != AFS_FILE_FCB &&
888 pFcb->Header.NodeTypeCode != AFS_SPECIAL_SHARE_FCB)
891 AFSDbgLogMsg( AFS_SUBSYSTEM_IO_PROCESSING,
892 AFS_TRACE_LEVEL_ERROR,
893 "AFSCommonRead Attempted read (%08lX) on an invalid node type %08lX\n",
895 pFcb->Header.NodeTypeCode);
897 try_return( ntStatus = STATUS_INVALID_DEVICE_REQUEST);
901 // If this is a read against an IOCtl node then handle it
902 // in a different pathway
905 if( pFcb->Header.NodeTypeCode == AFS_IOCTL_FCB)
908 AFSDbgLogMsg( AFS_SUBSYSTEM_IO_PROCESSING,
909 AFS_TRACE_LEVEL_VERBOSE,
910 "AFSCommonRead (%08lX) Processing file (PIOCTL) Offset %I64X Length %08lX Irp Flags %08lX\n",
912 liStartingByte.QuadPart,
916 ntStatus = AFSIOCtlRead( DeviceObject,
919 try_return( ntStatus);
921 else if( pFcb->Header.NodeTypeCode == AFS_SPECIAL_SHARE_FCB)
924 AFSDbgLogMsg( AFS_SUBSYSTEM_IO_PROCESSING,
925 AFS_TRACE_LEVEL_VERBOSE,
926 "AFSCommonRead (%08lX) Processing file (SHARE) Offset %I64X Length %08lX Irp Flags %08lX\n",
928 liStartingByte.QuadPart,
932 ntStatus = AFSShareRead( DeviceObject,
935 try_return( ntStatus);
939 // No fileobject yet? Bail.
941 if( !BooleanFlagOn( AFSLibControlFlags, AFS_REDIR_LIB_FLAGS_NONPERSISTENT_CACHE) &&
942 NULL == pDeviceExt->Specific.RDR.CacheFileObject)
945 AFSDbgLogMsg( AFS_SUBSYSTEM_IO_PROCESSING,
946 AFS_TRACE_LEVEL_ERROR,
947 "AFSCommonRead (%08lX) Request failed due to AFS cache closed\n",
950 try_return( ntStatus = STATUS_TOO_LATE );
953 if( pIrpSp->Parameters.Read.Length == 0)
956 AFSDbgLogMsg( AFS_SUBSYSTEM_IO_PROCESSING,
957 AFS_TRACE_LEVEL_VERBOSE,
958 "AFSCommonRead (%08lX) Request completed due to zero length\n",
961 try_return( ntStatus);
964 if ( FlagOn(pIrpSp->MinorFunction, IRP_MN_COMPLETE) )
967 AFSDbgLogMsg( AFS_SUBSYSTEM_IO_PROCESSING,
968 AFS_TRACE_LEVEL_VERBOSE,
969 "AFSCommonRead (%08lX) IRP_MN_COMPLETE being processed\n",
972 CcMdlReadComplete(pIrpSp->FileObject, Irp->MdlAddress);
975 // Mdl is now Deallocated
978 Irp->MdlAddress = NULL;
980 try_return( ntStatus = STATUS_SUCCESS );
984 // If we get a non cached IO for a cached file we should do a purge.
985 // For now we will just promote to cached
987 if (NULL != pFileObject->SectionObjectPointer->DataSectionObject && !bPagingIo)
990 bNonCachedIo = FALSE;
994 // We acquire the main/paging resource first to synchronize
995 // against size checks.
1001 AFSDbgLogMsg( AFS_SUBSYSTEM_LOCK_PROCESSING,
1002 AFS_TRACE_LEVEL_VERBOSE,
1003 "AFSCommonRead Acquiring Fcb PagingIo lock %08lX SHARED %08lX\n",
1004 &pFcb->NPFcb->PagingResource,
1005 PsGetCurrentThread());
1007 AFSAcquireShared( &pFcb->NPFcb->PagingResource,
1010 bReleasePaging = TRUE;
1015 AFSDbgLogMsg( AFS_SUBSYSTEM_LOCK_PROCESSING,
1016 AFS_TRACE_LEVEL_VERBOSE,
1017 "AFSCommonRead Acquiring Fcb lock %08lX SHARED %08lX\n",
1018 &pFcb->NPFcb->Resource,
1019 PsGetCurrentThread());
1021 AFSAcquireShared( &pFcb->NPFcb->Resource,
1024 bReleaseMain = TRUE;
1027 // Check the BR locks
1030 if( !FsRtlCheckLockForReadAccess( &pFcb->Specific.File.FileLock,
1034 AFSDbgLogMsg( AFS_SUBSYSTEM_IO_PROCESSING,
1035 AFS_TRACE_LEVEL_ERROR,
1036 "AFSCommonRead (%08lX) Request failed due to lock conflict\n",
1039 try_return( ntStatus = STATUS_FILE_LOCK_CONFLICT);
1043 if( BooleanFlagOn( pCcb->DirectoryCB->Flags, AFS_DIR_ENTRY_DELETED) ||
1044 BooleanFlagOn( pFcb->ObjectInformation->Flags, AFS_OBJECT_FLAGS_DELETED))
1047 AFSDbgLogMsg( AFS_SUBSYSTEM_IO_PROCESSING,
1048 AFS_TRACE_LEVEL_ERROR,
1049 "AFSCommonRead (%08lX) Request failed due to file deleted\n",
1052 try_return( ntStatus = STATUS_FILE_DELETED);
1056 // If the read starts beyond End of File, return EOF.
1059 if( liStartingByte.QuadPart >= pFcb->Header.FileSize.QuadPart)
1062 AFSDbgLogMsg( AFS_SUBSYSTEM_IO_PROCESSING,
1063 AFS_TRACE_LEVEL_VERBOSE,
1064 "AFSCommonRead (%08lX) Request beyond EOF %I64X\n",
1066 pFcb->Header.FileSize.QuadPart);
1068 try_return( ntStatus = STATUS_END_OF_FILE);
1074 if( liStartingByte.QuadPart + ulByteCount > pFcb->Header.FileSize.QuadPart)
1077 ulByteCount = (ULONG)(pFcb->Header.FileSize.QuadPart - liStartingByte.QuadPart);
1079 AFSDbgLogMsg( AFS_SUBSYSTEM_IO_PROCESSING,
1080 AFS_TRACE_LEVEL_VERBOSE,
1081 "AFSCommonRead (%08lX) Truncated read request to %08lX\n",
1087 // Is this Fcb valid???
1090 if( BooleanFlagOn( pFcb->ObjectInformation->Flags, AFS_OBJECT_FLAGS_OBJECT_INVALID))
1093 AFSDbgLogMsg( AFS_SUBSYSTEM_IO_PROCESSING,
1094 AFS_TRACE_LEVEL_ERROR,
1095 "AFSCommonRead (%08lX) Failing request due to INVALID fcb\n",
1098 Irp->IoStatus.Information = 0;
1100 try_return( ntStatus = STATUS_FILE_DELETED);
1104 // If this is an cached IO
1106 if( (!bPagingIo && !bNonCachedIo))
1109 // This is a top level irp. Init the caching if it has not yet
1110 // been initialzed for this FO
1113 if( pFileObject->PrivateCacheMap == NULL)
1119 AFSDbgLogMsg( AFS_SUBSYSTEM_IO_PROCESSING,
1120 AFS_TRACE_LEVEL_VERBOSE,
1121 "AFSCommonRead Initialize caching on Fcb %08lX FO %08lX\n",
1125 CcInitializeCacheMap( pFileObject,
1126 (PCC_FILE_SIZES)&pFcb->Header.AllocationSize,
1128 AFSLibCacheManagerCallbacks,
1131 CcSetReadAheadGranularity( pFileObject,
1132 pDeviceExt->Specific.RDR.MaximumRPCLength);
1134 CcSetDirtyPageThreshold( pFileObject,
1135 AFS_DIRTY_CHUNK_THRESHOLD * pDeviceExt->Specific.RDR.MaximumRPCLength);
1137 __except( EXCEPTION_EXECUTE_HANDLER)
1140 ntStatus = GetExceptionCode();
1142 AFSDbgLogMsg( AFS_SUBSYSTEM_IO_PROCESSING,
1143 AFS_TRACE_LEVEL_ERROR,
1144 "AFSCommonRead (%08lX) Exception thrown while initializing cache map Status %08lX\n",
1149 if( !NT_SUCCESS( ntStatus))
1152 try_return( ntStatus);
1157 // And if this is MDL operation, deal with it now (yes we
1158 // could post, but we are almost certainly posted
1159 // already and we don't want to grow and SVA for it..)
1162 if( BooleanFlagOn( pIrpSp->MinorFunction, IRP_MN_MDL))
1167 CcMdlRead( pFileObject,
1172 ntStatus = Irp->IoStatus.Status;
1174 __except( EXCEPTION_EXECUTE_HANDLER)
1176 ntStatus = GetExceptionCode();
1178 AFSDbgLogMsg( AFS_SUBSYSTEM_IO_PROCESSING,
1179 AFS_TRACE_LEVEL_ERROR,
1180 "AFSCommonRead (%08lX) Exception thrown during mdl read Status %08lX\n",
1185 if( !NT_SUCCESS( ntStatus))
1188 AFSDbgLogMsg( AFS_SUBSYSTEM_IO_PROCESSING,
1189 AFS_TRACE_LEVEL_ERROR,
1190 "AFSCommonRead (%08lX) Failed to process MDL read Status %08lX\n",
1194 try_return( ntStatus);
1198 // Update the CBO if this is a sync read
1201 if( BooleanFlagOn( pFileObject->Flags, FO_SYNCHRONOUS_IO))
1204 pFileObject->CurrentByteOffset.QuadPart = liStartingByte.QuadPart + ulByteCount;
1207 try_return( ntStatus);
1212 // The called request completes the IRP for us.
1215 bCompleteIrp = FALSE;
1221 AFSDbgLogMsg( AFS_SUBSYSTEM_IO_PROCESSING,
1222 AFS_TRACE_LEVEL_VERBOSE,
1223 "AFSCommonRead (%08lX) Processing CACHED request Offset %I64X Len %08lX\n",
1225 liStartingByte.QuadPart,
1228 ntStatus = AFSCachedRead( DeviceObject, Irp, liStartingByte, ulByteCount);
1233 AFSDbgLogMsg( AFS_SUBSYSTEM_IO_PROCESSING,
1234 AFS_TRACE_LEVEL_VERBOSE,
1235 "AFSCommonRead (%08lX) Processing NON-CACHED request Offset %I64X Len %08lX\n",
1237 liStartingByte.QuadPart,
1240 ntStatus = AFSNonCachedRead( DeviceObject, Irp, liStartingByte);
1245 AFSDbgLogMsg( AFS_SUBSYSTEM_IO_PROCESSING,
1246 AFS_TRACE_LEVEL_VERBOSE,
1247 "AFSCommonRead (%08lX) Process complete Status %08lX\n",
1254 AFSReleaseResource( &pFcb->NPFcb->PagingResource);
1260 AFSReleaseResource( &pFcb->NPFcb->Resource);
1266 AFSCompleteRequest( Irp, ntStatus);
1269 ObDereferenceObject( pFileObject);
1276 AFSIOCtlRead( IN PDEVICE_OBJECT DeviceObject,
1280 NTSTATUS ntStatus = STATUS_SUCCESS;
1281 AFSPIOCtlIORequestCB stIORequestCB;
1282 PIO_STACK_LOCATION pIrpSp = IoGetCurrentIrpStackLocation( Irp);
1283 AFSFcb *pFcb = NULL;
1284 AFSCcb *pCcb = NULL;
1285 AFSPIOCtlIOResultCB stIOResultCB;
1286 ULONG ulBytesReturned = 0;
1287 AFSFileID stParentFID;
1292 RtlZeroMemory( &stIORequestCB,
1293 sizeof( AFSPIOCtlIORequestCB));
1295 if( pIrpSp->Parameters.Read.Length == 0)
1299 // Nothing to do in this case
1302 try_return( ntStatus);
1305 pFcb = (AFSFcb *)pIrpSp->FileObject->FsContext;
1307 pCcb = (AFSCcb *)pIrpSp->FileObject->FsContext2;
1309 AFSDbgLogMsg( AFS_SUBSYSTEM_LOCK_PROCESSING,
1310 AFS_TRACE_LEVEL_VERBOSE,
1311 "AFSIOCtlRead Acquiring Fcb lock %08lX SHARED %08lX\n",
1312 &pFcb->NPFcb->Resource,
1313 PsGetCurrentThread());
1315 AFSAcquireShared( &pFcb->NPFcb->Resource,
1319 // Get the parent fid to pass to the cm
1322 RtlZeroMemory( &stParentFID,
1323 sizeof( AFSFileID));
1325 if( pFcb->ObjectInformation->ParentObjectInformation != NULL)
1329 // The parent directory FID of the node
1332 ASSERT( pFcb->ObjectInformation->ParentObjectInformation->FileType == AFS_FILE_TYPE_DIRECTORY);
1334 stParentFID = pFcb->ObjectInformation->ParentObjectInformation->FileId;
1338 // Set the control block up
1341 stIORequestCB.RequestId = pCcb->RequestID;
1343 if( pFcb->ObjectInformation->VolumeCB != NULL)
1345 stIORequestCB.RootId = pFcb->ObjectInformation->VolumeCB->ObjectInformation.FileId;
1349 // Lock down the buffer
1352 stIORequestCB.MappedBuffer = AFSMapToService( Irp,
1353 pIrpSp->Parameters.Read.Length);
1355 if( stIORequestCB.MappedBuffer == NULL)
1358 try_return( ntStatus = STATUS_INSUFFICIENT_RESOURCES);
1361 stIORequestCB.BufferLength = pIrpSp->Parameters.Read.Length;
1363 stIOResultCB.BytesProcessed = 0;
1365 ulBytesReturned = sizeof( AFSPIOCtlIOResultCB);
1368 // Issue the request to the service
1371 ntStatus = AFSProcessRequest( AFS_REQUEST_TYPE_PIOCTL_READ,
1372 AFS_REQUEST_FLAG_SYNCHRONOUS,
1376 (void *)&stIORequestCB,
1377 sizeof( AFSPIOCtlIORequestCB),
1381 if( !NT_SUCCESS( ntStatus))
1384 try_return( ntStatus);
1388 // Update the length read
1391 Irp->IoStatus.Information = stIOResultCB.BytesProcessed;
1395 if( stIORequestCB.MappedBuffer != NULL)
1398 AFSUnmapServiceMappedBuffer( stIORequestCB.MappedBuffer,
1405 AFSReleaseResource( &pFcb->NPFcb->Resource);
1413 AFSShareRead( IN PDEVICE_OBJECT DeviceObject,
1417 NTSTATUS ntStatus = STATUS_SUCCESS;
1418 AFSPIOCtlIORequestCB stIORequestCB;
1419 PIO_STACK_LOCATION pIrpSp = IoGetCurrentIrpStackLocation( Irp);
1420 AFSFcb *pFcb = NULL;
1421 AFSCcb *pCcb = NULL;
1422 ULONG ulBytesReturned = 0;
1423 void *pBuffer = NULL;
1424 AFSPipeIORequestCB stIoRequest;
1429 pFcb = (AFSFcb *)pIrpSp->FileObject->FsContext;
1431 pCcb = (AFSCcb *)pIrpSp->FileObject->FsContext2;
1433 AFSDbgLogMsg( AFS_SUBSYSTEM_PIPE_PROCESSING,
1434 AFS_TRACE_LEVEL_VERBOSE,
1435 "AFSShareRead On pipe %wZ Length %08lX\n",
1436 &pCcb->DirectoryCB->NameInformation.FileName,
1437 pIrpSp->Parameters.Read.Length);
1439 if( pIrpSp->Parameters.Read.Length == 0)
1443 // Nothing to do in this case
1446 try_return( ntStatus);
1449 AFSAcquireShared( &pFcb->NPFcb->Resource,
1453 // Retrieve the buffer for the read request
1456 pBuffer = AFSLockSystemBuffer( Irp,
1457 pIrpSp->Parameters.Read.Length);
1459 if( pBuffer == NULL)
1462 AFSDbgLogMsg( AFS_SUBSYSTEM_PIPE_PROCESSING,
1463 AFS_TRACE_LEVEL_ERROR,
1464 "AFSShareRead Failed to map buffer on pipe %wZ\n",
1465 &pCcb->DirectoryCB->NameInformation.FileName);
1467 try_return( ntStatus = STATUS_INSUFFICIENT_RESOURCES);
1470 RtlZeroMemory( &stIoRequest,
1471 sizeof( AFSPipeIORequestCB));
1473 stIoRequest.RequestId = pCcb->RequestID;
1475 stIoRequest.RootId = pFcb->ObjectInformation->VolumeCB->ObjectInformation.FileId;
1477 stIoRequest.BufferLength = pIrpSp->Parameters.Read.Length;
1479 ulBytesReturned = pIrpSp->Parameters.Read.Length;
1482 // Issue the open request to the service
1485 ntStatus = AFSProcessRequest( AFS_REQUEST_TYPE_PIPE_READ,
1486 AFS_REQUEST_FLAG_SYNCHRONOUS,
1488 &pCcb->DirectoryCB->NameInformation.FileName,
1490 (void *)&stIoRequest,
1491 sizeof( AFSPipeIORequestCB),
1495 if( !NT_SUCCESS( ntStatus) &&
1496 ntStatus != STATUS_BUFFER_OVERFLOW)
1499 AFSDbgLogMsg( AFS_SUBSYSTEM_FILE_PROCESSING,
1500 AFS_TRACE_LEVEL_ERROR,
1501 "AFSShareRead (%08lX) Failed service read Status %08lX\n",
1505 try_return( ntStatus);
1508 AFSDbgLogMsg( AFS_SUBSYSTEM_PIPE_PROCESSING,
1509 AFS_TRACE_LEVEL_VERBOSE,
1510 "AFSShareRead Completed on pipe %wZ Length read %08lX Status %08lX\n",
1511 &pCcb->DirectoryCB->NameInformation.FileName,
1515 Irp->IoStatus.Information = ulBytesReturned;
1522 AFSReleaseResource( &pFcb->NPFcb->Resource);