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;
664 // Setup the MD5 for each extent
667 AFSSetupMD5Hash( pFcb,
674 AFSDbgLogMsg( AFS_SUBSYSTEM_IO_PROCESSING,
675 AFS_TRACE_LEVEL_VERBOSE,
676 "AFSNonCachedRead (%08lX) AFSStartIos wait completed Status %08lX\n",
680 AFSDbgLogMsg( AFS_SUBSYSTEM_LOCK_PROCESSING,
681 AFS_TRACE_LEVEL_VERBOSE,
682 "AFSNonCachedRead Acquiring(4) Fcb extents lock %08lX SHARED %08lX\n",
683 &pFcb->NPFcb->Specific.File.ExtentsResource,
684 PsGetCurrentThread());
686 AFSAcquireShared( &pFcb->NPFcb->Specific.File.ExtentsResource,
690 AFSDereferenceActiveExtents( pStartExtent,
693 AFSDbgLogMsg( AFS_SUBSYSTEM_LOCK_PROCESSING,
694 AFS_TRACE_LEVEL_VERBOSE,
695 "AFSNonCachedRead Releasing Fcb extents lock %08lX SHARED %08lX\n",
696 &pFcb->NPFcb->Specific.File.ExtentsResource,
697 PsGetCurrentThread());
699 AFSReleaseResource( &pFcb->NPFcb->Specific.File.ExtentsResource );
703 // The data is there now. Give back the extents now so the service
706 (VOID) AFSReleaseExtentsWithFlush( pFcb,
711 if( pCacheFileObject != NULL)
713 AFSReleaseCacheFileObject( pCacheFileObject);
716 AFSDbgLogMsg( AFS_SUBSYSTEM_IO_PROCESSING,
717 AFS_TRACE_LEVEL_VERBOSE,
718 "AFSNonCachedRead (%08lX) Completed request Status %08lX\n",
722 if (NT_SUCCESS(ntStatus) &&
724 BooleanFlagOn( pFileObject->Flags, FO_SYNCHRONOUS_IO))
727 // Update the CBO if this is a sync, nopaging read
729 pFileObject->CurrentByteOffset.QuadPart = StartingByte.QuadPart + ulByteCount;
735 AFSDbgLogMsg( AFS_SUBSYSTEM_LOCK_PROCESSING,
736 AFS_TRACE_LEVEL_VERBOSE,
737 "AFSNonCachedRead Releasing Fcb extents lock %08lX SHARED %08lX\n",
738 &pFcb->NPFcb->Specific.File.ExtentsResource,
739 PsGetCurrentThread());
741 AFSReleaseResource( &pFcb->NPFcb->Specific.File.ExtentsResource );
746 AFSExFreePool(pGatherIo);
749 if (NULL != pIoRuns && stIoRuns != pIoRuns)
751 AFSExFreePool(pIoRuns);
757 AFSDbgLogMsg( AFS_SUBSYSTEM_IO_PROCESSING,
758 AFS_TRACE_LEVEL_VERBOSE,
759 "AFSNonCachedRead Completing irp %08lX Status %08lX Info %08lX\n",
762 Irp->IoStatus.Information);
764 AFSCompleteRequest( Irp, ntStatus );
770 // Function: AFSDispatch
774 // A shim around AFSCommonRead (qv)
777 AFSRead( IN PDEVICE_OBJECT LibDeviceObject,
781 NTSTATUS ntStatus = STATUS_SUCCESS;
786 ntStatus = AFSCommonRead( AFSRDRDeviceObject, Irp, NULL);
788 __except( AFSExceptionFilter( GetExceptionCode(), GetExceptionInformation()) )
791 ntStatus = STATUS_INSUFFICIENT_RESOURCES;
801 // This function is a slightly widened Dispatch handler for the
802 // AFS Read function. The thrid parameter is FALSE if we were called
803 // at our dispatch point and TRUE if we have been posted.
805 // After doing the obvious (completing MDL writes and so forth)
806 // we then post, or not.
810 // A status is returned for the function
814 AFSCommonRead( IN PDEVICE_OBJECT DeviceObject,
816 IN HANDLE OnBehalfOf)
819 NTSTATUS ntStatus = STATUS_SUCCESS;
820 AFSDeviceExt *pDeviceExt;
821 IO_STACK_LOCATION *pIrpSp;
824 BOOLEAN bReleaseMain = FALSE;
825 BOOLEAN bReleasePaging = FALSE;
826 BOOLEAN bPagingIo = FALSE;
827 BOOLEAN bNonCachedIo = FALSE;
828 BOOLEAN bCompleteIrp = TRUE;
830 PFILE_OBJECT pFileObject = NULL;
831 LARGE_INTEGER liStartingByte;
833 VOID *pSystemBuffer = NULL;
834 HANDLE hCallingUser = NULL;
836 pIrpSp = IoGetCurrentIrpStackLocation( Irp);
837 pDeviceExt = (AFSDeviceExt *)DeviceObject->DeviceExtension;
842 // Decode the fileobject
844 pFileObject = pIrpSp->FileObject;
847 // There is a risk (albeit infinitely small) that the Irp will
848 // complete before this function exits, then a cleanup and
849 // close will happen and the FCB will be torn down before we
850 // get to the try_exit. Pin the file Object which will pin the FCB
853 ObReferenceObject( pFileObject);
856 // If we are in shutdown mode then fail the request
859 if( BooleanFlagOn( pDeviceExt->DeviceFlags, AFS_DEVICE_FLAG_REDIRECTOR_SHUTDOWN))
862 AFSDbgLogMsg( AFS_SUBSYSTEM_FILE_PROCESSING,
863 AFS_TRACE_LEVEL_WARNING,
864 "AFSCommonRead (%08lX) Open request after shutdown\n",
867 try_return( ntStatus = STATUS_TOO_LATE);
870 pFcb = (AFSFcb *)pFileObject->FsContext;
872 pCcb = (AFSCcb *)pFileObject->FsContext2;
877 AFSDbgLogMsg( AFS_SUBSYSTEM_IO_PROCESSING,
878 AFS_TRACE_LEVEL_ERROR,
879 "AFSCommonRead Attempted read (%08lX) when pFcb == NULL\n",
882 try_return( ntStatus = STATUS_INVALID_DEVICE_REQUEST);
885 bPagingIo = BooleanFlagOn( Irp->Flags, IRP_PAGING_IO);
886 bNonCachedIo = BooleanFlagOn( Irp->Flags,IRP_NOCACHE);
888 liStartingByte = pIrpSp->Parameters.Read.ByteOffset;
889 ulByteCount = pIrpSp->Parameters.Read.Length;
891 if( pFcb->Header.NodeTypeCode != AFS_IOCTL_FCB &&
892 pFcb->Header.NodeTypeCode != AFS_FILE_FCB &&
893 pFcb->Header.NodeTypeCode != AFS_SPECIAL_SHARE_FCB)
896 AFSDbgLogMsg( AFS_SUBSYSTEM_IO_PROCESSING,
897 AFS_TRACE_LEVEL_ERROR,
898 "AFSCommonRead Attempted read (%08lX) on an invalid node type %08lX\n",
900 pFcb->Header.NodeTypeCode);
902 try_return( ntStatus = STATUS_INVALID_DEVICE_REQUEST);
906 // If this is a read against an IOCtl node then handle it
907 // in a different pathway
910 if( pFcb->Header.NodeTypeCode == AFS_IOCTL_FCB)
913 AFSDbgLogMsg( AFS_SUBSYSTEM_IO_PROCESSING,
914 AFS_TRACE_LEVEL_VERBOSE,
915 "AFSCommonRead (%08lX) Processing file (PIOCTL) Offset %I64X Length %08lX Irp Flags %08lX\n",
917 liStartingByte.QuadPart,
921 ntStatus = AFSIOCtlRead( DeviceObject,
924 try_return( ntStatus);
926 else if( pFcb->Header.NodeTypeCode == AFS_SPECIAL_SHARE_FCB)
929 AFSDbgLogMsg( AFS_SUBSYSTEM_IO_PROCESSING,
930 AFS_TRACE_LEVEL_VERBOSE,
931 "AFSCommonRead (%08lX) Processing file (SHARE) Offset %I64X Length %08lX Irp Flags %08lX\n",
933 liStartingByte.QuadPart,
937 ntStatus = AFSShareRead( DeviceObject,
940 try_return( ntStatus);
944 // No fileobject yet? Bail.
946 if( !BooleanFlagOn( AFSLibControlFlags, AFS_REDIR_LIB_FLAGS_NONPERSISTENT_CACHE) &&
947 NULL == pDeviceExt->Specific.RDR.CacheFileObject)
950 AFSDbgLogMsg( AFS_SUBSYSTEM_IO_PROCESSING,
951 AFS_TRACE_LEVEL_ERROR,
952 "AFSCommonRead (%08lX) Request failed due to AFS cache closed\n",
955 try_return( ntStatus = STATUS_TOO_LATE );
958 if( pIrpSp->Parameters.Read.Length == 0)
961 AFSDbgLogMsg( AFS_SUBSYSTEM_IO_PROCESSING,
962 AFS_TRACE_LEVEL_VERBOSE,
963 "AFSCommonRead (%08lX) Request completed due to zero length\n",
966 try_return( ntStatus);
969 if ( FlagOn(pIrpSp->MinorFunction, IRP_MN_COMPLETE) )
972 AFSDbgLogMsg( AFS_SUBSYSTEM_IO_PROCESSING,
973 AFS_TRACE_LEVEL_VERBOSE,
974 "AFSCommonRead (%08lX) IRP_MN_COMPLETE being processed\n",
977 CcMdlReadComplete(pIrpSp->FileObject, Irp->MdlAddress);
980 // Mdl is now Deallocated
983 Irp->MdlAddress = NULL;
985 try_return( ntStatus = STATUS_SUCCESS );
989 // If we get a non cached IO for a cached file we should do a purge.
990 // For now we will just promote to cached
992 if (NULL != pFileObject->SectionObjectPointer->DataSectionObject && !bPagingIo)
995 bNonCachedIo = FALSE;
999 // Setup the calling process
1002 if( NULL == OnBehalfOf)
1005 hCallingUser = PsGetCurrentProcessId();
1010 hCallingUser = OnBehalfOf;
1014 // We acquire the main/paging resource first to synchronize
1015 // against size checks.
1021 AFSDbgLogMsg( AFS_SUBSYSTEM_LOCK_PROCESSING,
1022 AFS_TRACE_LEVEL_VERBOSE,
1023 "AFSCommonRead Acquiring Fcb PagingIo lock %08lX SHARED %08lX\n",
1024 &pFcb->NPFcb->PagingResource,
1025 PsGetCurrentThread());
1027 AFSAcquireShared( &pFcb->NPFcb->PagingResource,
1030 bReleasePaging = TRUE;
1035 AFSDbgLogMsg( AFS_SUBSYSTEM_LOCK_PROCESSING,
1036 AFS_TRACE_LEVEL_VERBOSE,
1037 "AFSCommonRead Acquiring Fcb lock %08lX SHARED %08lX\n",
1038 &pFcb->NPFcb->Resource,
1039 PsGetCurrentThread());
1041 AFSAcquireShared( &pFcb->NPFcb->Resource,
1044 bReleaseMain = 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 (%08lX) 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 (%08lX) 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 (%08lX) 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 (%08lX) 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 (%08lX) Failing request due to INVALID fcb\n",
1118 Irp->IoStatus.Information = 0;
1120 try_return( ntStatus = STATUS_FILE_DELETED);
1124 // Save off the PID if this is not a paging IO
1128 ( pFcb->Specific.File.ExtentProcessId == 0 ||
1129 ( PsGetCurrentProcessId() != AFSSysProcess &&
1130 pFcb->Specific.File.ExtentProcessId != (ULONGLONG)PsGetCurrentProcessId())))
1133 pFcb->Specific.File.ExtentProcessId = (ULONGLONG)PsGetCurrentProcessId();
1135 if( pFcb->Specific.File.ExtentProcessId == (ULONGLONG)AFSSysProcess)
1137 AFSDbgLogMsg( AFS_SUBSYSTEM_EXTENT_PROCESSING,
1138 AFS_TRACE_LEVEL_WARNING,
1139 "%s Setting ExtentProcessId to system process for Fcb %p\n",
1144 pFcb->Specific.File.ExtentThreadId = (ULONGLONG)PsGetCurrentThreadId();
1149 // If this is going to be posted OR this is a cached read,
1150 // then ask for the extents before we post.
1153 if( !bPagingIo && !bNonCachedIo)
1156 ntStatus = AFSRequestExtentsAsync( pFcb, pCcb, &liStartingByte, ulByteCount);
1158 if (!NT_SUCCESS(ntStatus))
1161 AFSDbgLogMsg( AFS_SUBSYSTEM_EXTENT_PROCESSING,
1162 AFS_TRACE_LEVEL_ERROR,
1163 "AFSCommonRead (%08lX) Failed to request extents Status %08lX\n",
1167 try_return( ntStatus );
1173 // If this is an cached IO
1175 if( (!bPagingIo && !bNonCachedIo))
1178 // This is a top level irp. Init the caching if it has not yet
1179 // been initialzed for this FO
1182 if( pFileObject->PrivateCacheMap == NULL)
1188 AFSDbgLogMsg( AFS_SUBSYSTEM_IO_PROCESSING,
1189 AFS_TRACE_LEVEL_VERBOSE,
1190 "AFSCommonRead Initialize caching on Fcb %08lX FO %08lX\n",
1194 CcInitializeCacheMap( pFileObject,
1195 (PCC_FILE_SIZES)&pFcb->Header.AllocationSize,
1197 AFSLibCacheManagerCallbacks,
1200 CcSetReadAheadGranularity( pFileObject,
1201 pDeviceExt->Specific.RDR.MaximumRPCLength);
1203 CcSetDirtyPageThreshold( pFileObject,
1204 AFS_DIRTY_CHUNK_THRESHOLD * pDeviceExt->Specific.RDR.MaximumRPCLength);
1206 __except( EXCEPTION_EXECUTE_HANDLER)
1209 ntStatus = GetExceptionCode();
1211 AFSDbgLogMsg( AFS_SUBSYSTEM_IO_PROCESSING,
1212 AFS_TRACE_LEVEL_ERROR,
1213 "AFSCommonRead (%08lX) Exception thrown while initializing cache map Status %08lX\n",
1218 if( !NT_SUCCESS( ntStatus))
1221 try_return( ntStatus);
1226 // And if this is MDL operation, deal with it now (yes we
1227 // could post, but we are almost certainly posted
1228 // already and we don't want to grow and SVA for it..)
1231 if( BooleanFlagOn( pIrpSp->MinorFunction, IRP_MN_MDL))
1236 CcMdlRead( pFileObject,
1241 ntStatus = Irp->IoStatus.Status;
1243 __except( EXCEPTION_EXECUTE_HANDLER)
1245 ntStatus = GetExceptionCode();
1247 AFSDbgLogMsg( AFS_SUBSYSTEM_IO_PROCESSING,
1248 AFS_TRACE_LEVEL_ERROR,
1249 "AFSCachedWrite (%08lX) Exception thrown during mdl read Status %08lX\n",
1254 if( !NT_SUCCESS( ntStatus))
1257 AFSDbgLogMsg( AFS_SUBSYSTEM_IO_PROCESSING,
1258 AFS_TRACE_LEVEL_ERROR,
1259 "AFSCommonRead (%08lX) Failed to process MDL read Status %08lX\n",
1263 try_return( ntStatus);
1267 // Update the CBO if this is a sync read
1270 if( BooleanFlagOn( pFileObject->Flags, FO_SYNCHRONOUS_IO))
1273 pFileObject->CurrentByteOffset.QuadPart = liStartingByte.QuadPart + ulByteCount;
1276 try_return( ntStatus);
1281 // The called request completes the IRP for us.
1284 bCompleteIrp = FALSE;
1290 AFSDbgLogMsg( AFS_SUBSYSTEM_IO_PROCESSING,
1291 AFS_TRACE_LEVEL_VERBOSE,
1292 "AFSCommonRead (%08lX) Processing CACHED request Offset %I64X Len %08lX\n",
1294 liStartingByte.QuadPart,
1297 ntStatus = AFSCachedRead( DeviceObject, Irp, liStartingByte, ulByteCount);
1302 AFSDbgLogMsg( AFS_SUBSYSTEM_IO_PROCESSING,
1303 AFS_TRACE_LEVEL_VERBOSE,
1304 "AFSCommonRead (%08lX) Processing NON-CACHED request Offset %I64X Len %08lX\n",
1306 liStartingByte.QuadPart,
1309 ntStatus = AFSNonCachedRead( DeviceObject, Irp, liStartingByte);
1313 // We won't do this for now in read processing ...
1320 if( pFcb->Specific.File.ExtentsDirtyCount > 0 ||
1321 pFcb->Specific.File.ExtentLength > 1500)
1324 AFSQueueFlushExtents( pFcb,
1331 AFSDbgLogMsg( AFS_SUBSYSTEM_IO_PROCESSING,
1332 AFS_TRACE_LEVEL_VERBOSE,
1333 "AFSCommonRead (%08lX) Process complete Status %08lX\n",
1340 AFSReleaseResource( &pFcb->NPFcb->PagingResource);
1346 AFSReleaseResource( &pFcb->NPFcb->Resource);
1352 AFSCompleteRequest( Irp, ntStatus);
1355 ObDereferenceObject( pFileObject);
1362 AFSIOCtlRead( IN PDEVICE_OBJECT DeviceObject,
1366 NTSTATUS ntStatus = STATUS_SUCCESS;
1367 AFSPIOCtlIORequestCB stIORequestCB;
1368 PIO_STACK_LOCATION pIrpSp = IoGetCurrentIrpStackLocation( Irp);
1369 AFSFcb *pFcb = NULL;
1370 AFSCcb *pCcb = NULL;
1371 AFSPIOCtlIOResultCB stIOResultCB;
1372 ULONG ulBytesReturned = 0;
1373 AFSFileID stParentFID;
1378 RtlZeroMemory( &stIORequestCB,
1379 sizeof( AFSPIOCtlIORequestCB));
1381 if( pIrpSp->Parameters.Read.Length == 0)
1385 // Nothing to do in this case
1388 try_return( ntStatus);
1391 pFcb = (AFSFcb *)pIrpSp->FileObject->FsContext;
1393 pCcb = (AFSCcb *)pIrpSp->FileObject->FsContext2;
1395 AFSDbgLogMsg( AFS_SUBSYSTEM_LOCK_PROCESSING,
1396 AFS_TRACE_LEVEL_VERBOSE,
1397 "AFSIOCtlRead Acquiring Fcb lock %08lX SHARED %08lX\n",
1398 &pFcb->NPFcb->Resource,
1399 PsGetCurrentThread());
1401 AFSAcquireShared( &pFcb->NPFcb->Resource,
1405 // Get the parent fid to pass to the cm
1408 RtlZeroMemory( &stParentFID,
1409 sizeof( AFSFileID));
1411 if( pFcb->ObjectInformation->ParentObjectInformation != NULL)
1415 // The parent directory FID of the node
1418 ASSERT( pFcb->ObjectInformation->ParentObjectInformation->FileType == AFS_FILE_TYPE_DIRECTORY);
1420 stParentFID = pFcb->ObjectInformation->ParentObjectInformation->FileId;
1424 // Set the control block up
1427 stIORequestCB.RequestId = pCcb->RequestID;
1429 if( pFcb->ObjectInformation->VolumeCB != NULL)
1431 stIORequestCB.RootId = pFcb->ObjectInformation->VolumeCB->ObjectInformation.FileId;
1435 // Lock down the buffer
1438 stIORequestCB.MappedBuffer = AFSMapToService( Irp,
1439 pIrpSp->Parameters.Read.Length);
1441 if( stIORequestCB.MappedBuffer == NULL)
1444 try_return( ntStatus = STATUS_INSUFFICIENT_RESOURCES);
1447 stIORequestCB.BufferLength = pIrpSp->Parameters.Read.Length;
1449 stIOResultCB.BytesProcessed = 0;
1451 ulBytesReturned = sizeof( AFSPIOCtlIOResultCB);
1454 // Issue the request to the service
1457 ntStatus = AFSProcessRequest( AFS_REQUEST_TYPE_PIOCTL_READ,
1458 AFS_REQUEST_FLAG_SYNCHRONOUS,
1462 (void *)&stIORequestCB,
1463 sizeof( AFSPIOCtlIORequestCB),
1467 if( !NT_SUCCESS( ntStatus))
1470 try_return( ntStatus);
1474 // Update the length read
1477 Irp->IoStatus.Information = stIOResultCB.BytesProcessed;
1481 if( stIORequestCB.MappedBuffer != NULL)
1484 AFSUnmapServiceMappedBuffer( stIORequestCB.MappedBuffer,
1491 AFSReleaseResource( &pFcb->NPFcb->Resource);
1499 AFSShareRead( IN PDEVICE_OBJECT DeviceObject,
1503 NTSTATUS ntStatus = STATUS_SUCCESS;
1504 AFSPIOCtlIORequestCB stIORequestCB;
1505 PIO_STACK_LOCATION pIrpSp = IoGetCurrentIrpStackLocation( Irp);
1506 AFSFcb *pFcb = NULL;
1507 AFSCcb *pCcb = NULL;
1508 ULONG ulBytesReturned = 0;
1509 void *pBuffer = NULL;
1510 AFSPipeIORequestCB stIoRequest;
1515 pFcb = (AFSFcb *)pIrpSp->FileObject->FsContext;
1517 pCcb = (AFSCcb *)pIrpSp->FileObject->FsContext2;
1519 AFSDbgLogMsg( AFS_SUBSYSTEM_PIPE_PROCESSING,
1520 AFS_TRACE_LEVEL_VERBOSE,
1521 "AFSShareRead On pipe %wZ Length %08lX\n",
1522 &pCcb->DirectoryCB->NameInformation.FileName,
1523 pIrpSp->Parameters.Read.Length);
1525 if( pIrpSp->Parameters.Read.Length == 0)
1529 // Nothing to do in this case
1532 try_return( ntStatus);
1535 AFSAcquireShared( &pFcb->NPFcb->Resource,
1539 // Retrieve the buffer for the read request
1542 pBuffer = AFSLockSystemBuffer( Irp,
1543 pIrpSp->Parameters.Read.Length);
1545 if( pBuffer == NULL)
1548 AFSDbgLogMsg( AFS_SUBSYSTEM_PIPE_PROCESSING,
1549 AFS_TRACE_LEVEL_ERROR,
1550 "AFSShareRead Failed to map buffer on pipe %wZ\n",
1551 &pCcb->DirectoryCB->NameInformation.FileName);
1553 try_return( ntStatus = STATUS_INSUFFICIENT_RESOURCES);
1556 RtlZeroMemory( &stIoRequest,
1557 sizeof( AFSPipeIORequestCB));
1559 stIoRequest.RequestId = pCcb->RequestID;
1561 stIoRequest.RootId = pFcb->ObjectInformation->VolumeCB->ObjectInformation.FileId;
1563 stIoRequest.BufferLength = pIrpSp->Parameters.Read.Length;
1565 ulBytesReturned = pIrpSp->Parameters.Read.Length;
1568 // Issue the open request to the service
1571 ntStatus = AFSProcessRequest( AFS_REQUEST_TYPE_PIPE_READ,
1572 AFS_REQUEST_FLAG_SYNCHRONOUS,
1574 &pCcb->DirectoryCB->NameInformation.FileName,
1576 (void *)&stIoRequest,
1577 sizeof( AFSPipeIORequestCB),
1581 if( !NT_SUCCESS( ntStatus) &&
1582 ntStatus != STATUS_BUFFER_OVERFLOW)
1585 AFSDbgLogMsg( AFS_SUBSYSTEM_FILE_PROCESSING,
1586 AFS_TRACE_LEVEL_ERROR,
1587 "AFSShareRead (%08lX) Failed service read Status %08lX\n",
1591 try_return( ntStatus);
1594 AFSDbgLogMsg( AFS_SUBSYSTEM_PIPE_PROCESSING,
1595 AFS_TRACE_LEVEL_VERBOSE,
1596 "AFSShareRead Completed on pipe %wZ Length read %08lX Status %08lX\n",
1597 &pCcb->DirectoryCB->NameInformation.FileName,
1601 Irp->IoStatus.Information = ulBytesReturned;
1608 AFSReleaseResource( &pFcb->NPFcb->Resource);