reactos/drivers/filesystems/cdfs_new/read.c
Joachim Henze 688875e411 [0.4.7][CDFS_NEW/NTOSKRNL/NDK] Switch from our old CDFS to MS-PL CDFS_NEW
The main motivation to switch to that newer driver is, that our old one
simply can not read all isos. Especially complex ones made trouble and were
only shown as empty in explorer.
It is still possible to build and use the old driver when needed, only thing
that needs to be done for that is to revert 0.4.8-dev-164-g ec6b3ecbe4

Porting back the state up to 0.4.8-release-100-g8f947b5 implies:

Fixing the following JIRA-IDs (or avoid introducing them):
CORE-18029 "Mute noisy DPRINT 'SectionObject has ImageSection'"
CORE-17405 "Fix a macro-copy-paste and shrink the binary size"
CORE-15659 "Unable to build the gcc Release version in Windows using RosBE 2.1.6 (module cdfs fails)"
CORE-14315 "CDFS_NEW assertion during first stage setup due to new CcPerformReadAhead"
CORE-14128 "Avast! Free Antivirus 7.0 hangs the system when trying to detect a newly created virus"
CORE-14067 "CDFS_NEW assertions and exceptions"
CORE-14003 "Shutting down LiveCD asserts since introduction of MS PL CDFS_NEW"
CORE-13184 "Restore ability to install from disk-image"

by picking the following commits:
0.4.8-release-100-g 8f947b5322 [NTOSKRNL] Mute noisy DPRINT 'SectionObject has ImageSection' CORE-18029
0.4.8-release-80-g eb1ea19588 [CDFS_NEW] == 0.4.15-dev-1456-g 889eab7 CORE-17405
0.4.8-release-62-g 8c07aad4a8 [CDFS_NEW/XDK] == 0.4.11-dev-39-g a2f9762 + 0.4.11-dev-40-g 6d7ec8c CORE-14067
0.4.8-release-3-g 5d976d04e8 [CDFS_NEW] == 0.4.12-dev-431-g bccad87f3c + 0.4.12-dev-432-g 3463b2db9f CORE-15659
0.4.8-RC-3-g 51f9494d48 [CDFS_NEW] superseded later by the proper fix 0.4.8-release-62-g 8c07aad4a8 CORE-14067
0.4.8-dev-1069-g a5e89014dc [CDFS_NEW] CORE-14315
0.4.8-dev-475-g a59d4674de [NTOSKRNL] io/iomgr/device.c (forgotten assert) CORE-14128
0.4.8-dev-221-g 9d67a24799 [CDFS_NEW]
0.4.8-dev-220-g 67a7e45e35 [CDFS_NEW/DOC]
0.4.8-dev-219-g 6a3bbf24e0 [CDFS_NEW]
0.4.8-dev-218-g ec26cde4a1 [CDFS_NEW]
0.4.8-dev-217-g bc2378a356 [CDFS_NEW]
0.4.8-dev-216-g 5429771b99 [CDFS_NEW]
0.4.8-dev-215-g fd34548263 [CDFS_NEW] Sync with MS-PL driver
0.4.8-dev-164-g ec6b3ecbe4 [FILESYSTEMS] switch from CDFS to CDFS_NEW in CMakeLists.txt
0.4.8-dev-160-g 2b217e4ecf [NTOSKRNL] Mute spam CcSetReadAheadGranularity()
0.4.8-dev-159-g 64cb138a67 [NTOSKRNL] Mute spam CcPurgeCacheSection()
0.4.8-dev-150-g f723d230a0 [CDFS_NEW]
0.4.8-dev-133-g faee3753ea [CDFS_NEW] CORE-14003
0.4.8-dev-132-g 1d777ffab5 [NTOSKRNL] iofunc.c CORE-14003
0.4.8-dev-131-g c3d5a3f2bd [NTOSKRNL] iofunc.c CORE-14003
0.4.8-dev-130-g 3b64f7f8fb [NTOSKRNL] ob/obref.c & co CORE-14003
0.4.8-dev-129-g 7eefe70294 [NTOSKRNL] io/iomgr.c & co CORE-14003
0.4.8-dev-127-g 5f255827d3 [CDFS_NEW]
0.4.8-dev-126-g 1bef48796e [NTOSKRNL] just a comment, superseded later
0.4.8-dev-125-g cbf0430b56 [CDFS_NEW]
0.4.8-dev-123-g f88fe43abd [NTOSKRNL] io/iomgr/device.c (forbidden DPRINT)
0.4.8-dev-122-g 6c73385625 [CDFS_NEW] CORE-13184
0.4.8-dev-97-g 94298313c0 [CDFS_NEW]
0.4.8-dev-95-g e88eeb21af [CDFS_NEW/NTOSKRNL] CcWaitForCurrentLazyWriterActivity() stub return Success
0.4.8-dev-94-g 03d5be6437 [CDFS_NEW]
0.4.8-dev-93-g fa1c60db50 [CDFS_NEW]
0.4.8-dev-92-g 8b2fd60829 [CDFS_NEW]
0.4.8-dev-91-g e4da7ecc50 [CDFS_NEW]
0.4.8-dev-90-g 7b19676e2b [CDFS_NEW]
0.4.8-dev-89-g 3d4b8783fd [CDFS_NEW]
0.4.8-dev-88-g 818025ecc8 [CDFS_NEW]
0.4.8-dev-87-g 2639dd6736 [CDFS_NEW]
0.4.8-dev-86-g 755bdb5d0b [CDFS_NEW]
0.4.8-dev-85-g 3cbcb1bade [CDFS_NEW]

and mute spam in opcode INSTEAD of picking:
0.4.8-dev-165-g 2284a457a3 [NTOSKRNL] oplock.c Fixup
0.4.8-dev-163-g d3d5853956 [NTOSKRNL] oplock.c Implement oplock-support
0.4.12-dev-232-g f488102c86 [CDFS] was also left out for now

I am aware, that the backport introduces white-space-glitches within CDFS_NEW.
I decided to live with them in favor of better sync to master and newer releases.
2022-01-27 21:11:23 +01:00

577 lines
15 KiB
C
Executable file
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/*++
Copyright (c) 1989-2000 Microsoft Corporation
Module Name:
Read.c
Abstract:
This module implements the File Read routine for Read called by the
Fsd/Fsp dispatch drivers.
--*/
#include "cdprocs.h"
//
// The Bug check file id for this module
//
#define BugCheckFileId (CDFS_BUG_CHECK_READ)
//
// VOID
// SafeZeroMemory (
// _Out_ PUCHAR At,
// _In_ ULONG ByteCount
// );
//
//
// This macro just puts a nice little try-except around RtlZeroMemory
//
#ifndef __REACTOS__
#define SafeZeroMemory(IC,AT,BYTE_COUNT) { \
_SEH2_TRY { \
RtlZeroMemory( (AT), (BYTE_COUNT) ); \
__pragma(warning(suppress: 6320)) \
} _SEH2_EXCEPT( EXCEPTION_EXECUTE_HANDLER ) { \
CdRaiseStatus( IC, STATUS_INVALID_USER_BUFFER ); \
} _SEH2_END; \
}
#else
#define SafeZeroMemory(IC,AT,BYTE_COUNT) { \
_SEH2_TRY { \
RtlZeroMemory( (AT), (BYTE_COUNT) ); \
} _SEH2_EXCEPT( EXCEPTION_EXECUTE_HANDLER ) { \
CdRaiseStatus( IC, STATUS_INVALID_USER_BUFFER ); \
} _SEH2_END; \
}
#endif
//
// Read ahead amount used for normal data files
//
#define READ_AHEAD_GRANULARITY (0x10000)
#ifdef ALLOC_PRAGMA
#pragma alloc_text(PAGE, CdCommonRead)
#endif
_Requires_lock_held_(_Global_critical_region_)
NTSTATUS
CdCommonRead (
_Inout_ PIRP_CONTEXT IrpContext,
_Inout_ PIRP Irp
)
/*++
Routine Description:
This is the common entry point for NtReadFile calls. For synchronous requests,
CommonRead will complete the request in the current thread. If not
synchronous the request will be passed to the Fsp if there is a need to
block.
Arguments:
Irp - Supplies the Irp to process
Return Value:
NTSTATUS - The result of this operation.
--*/
{
NTSTATUS Status = STATUS_SUCCESS;
PIO_STACK_LOCATION IrpSp = IoGetCurrentIrpStackLocation( Irp );
TYPE_OF_OPEN TypeOfOpen;
PFCB Fcb;
PCCB Ccb;
BOOLEAN Wait;
ULONG PagingIo;
ULONG SynchronousIo;
ULONG NonCachedIo;
PVOID UserBuffer;
LONGLONG StartingOffset;
LONGLONG ByteRange;
ULONG ByteCount;
ULONG ReadByteCount;
ULONG OriginalByteCount;
PVOID SystemBuffer;
BOOLEAN ReleaseFile = TRUE;
CD_IO_CONTEXT LocalIoContext;
PAGED_CODE();
//
// If this is a zero length read then return SUCCESS immediately.
//
if (IrpSp->Parameters.Read.Length == 0) {
CdCompleteRequest( IrpContext, Irp, STATUS_SUCCESS );
return STATUS_SUCCESS;
}
//
// Decode the file object and verify we support read on this. It
// must be a user file, stream file or volume file (for a data disk).
//
TypeOfOpen = CdDecodeFileObject( IrpContext, IrpSp->FileObject, &Fcb, &Ccb );
// Internal lock object is acquired if return status is STATUS_PENDING
_Analysis_suppress_lock_checking_(Fcb->Resource);
if ((TypeOfOpen == UnopenedFileObject) ||
(TypeOfOpen == UserDirectoryOpen)) {
CdCompleteRequest( IrpContext, Irp, STATUS_INVALID_DEVICE_REQUEST );
return STATUS_INVALID_DEVICE_REQUEST;
}
//
// Examine our input parameters to determine if this is noncached and/or
// a paging io operation.
//
Wait = BooleanFlagOn( IrpContext->Flags, IRP_CONTEXT_FLAG_WAIT );
PagingIo = FlagOn( Irp->Flags, IRP_PAGING_IO );
NonCachedIo = FlagOn( Irp->Flags, IRP_NOCACHE );
SynchronousIo = FlagOn( IrpSp->FileObject->Flags, FO_SYNCHRONOUS_IO );
//
// Extract the range of the Io.
//
StartingOffset = IrpSp->Parameters.Read.ByteOffset.QuadPart;
OriginalByteCount = ByteCount = IrpSp->Parameters.Read.Length;
ByteRange = StartingOffset + ByteCount;
//
// Make sure that Dasd access is always non-cached.
//
if (TypeOfOpen == UserVolumeOpen) {
NonCachedIo = TRUE;
}
//
// Acquire the file shared to perform the read. If we are doing paging IO,
// it may be the case that we would have a deadlock imminent because we may
// block on shared access, so starve out any exclusive waiters. This requires
// a degree of caution - we believe that any paging IO bursts will recede and
// allow the exclusive waiter in.
//
if (PagingIo) {
CdAcquireFileSharedStarveExclusive( IrpContext, Fcb );
} else {
CdAcquireFileShared( IrpContext, Fcb );
}
//
// Use a try-finally to facilitate cleanup.
//
_SEH2_TRY {
//
// Verify the Fcb. Allow reads if this is a DASD handle that is
// dismounting the volume.
//
if ((TypeOfOpen != UserVolumeOpen) || (NULL == Ccb) ||
!FlagOn( Ccb->Flags, CCB_FLAG_DISMOUNT_ON_CLOSE)) {
CdVerifyFcbOperation( IrpContext, Fcb );
}
//
// If this is a non-cached then check whether we need to post this
// request if this thread can't block.
//
if (!Wait && NonCachedIo) {
//
// XA requests must always be waitable.
//
if (FlagOn( Fcb->FcbState, FCB_STATE_RAWSECTOR_MASK )) {
SetFlag( IrpContext->Flags, IRP_CONTEXT_FLAG_FORCE_POST );
try_return( Status = STATUS_CANT_WAIT );
}
}
//
// If this is a user request then verify the oplock and filelock state.
//
if (TypeOfOpen == UserFileOpen) {
//
// We check whether we can proceed
// based on the state of the file oplocks.
//
Status = FsRtlCheckOplock( CdGetFcbOplock(Fcb),
Irp,
IrpContext,
(PVOID)CdOplockComplete,/* ReactOS Change: GCC "assignment from incompatible pointer type" */
(PVOID)CdPrePostIrp );/* ReactOS Change: GCC "assignment from incompatible pointer type" */
//
// If the result is not STATUS_SUCCESS then the Irp was completed
// elsewhere.
//
if (Status != STATUS_SUCCESS) {
Irp = NULL;
IrpContext = NULL;
try_return( NOTHING );
}
if (!PagingIo &&
(Fcb->FileLock != NULL) &&
!FsRtlCheckLockForReadAccess( Fcb->FileLock, Irp )) {
try_return( Status = STATUS_FILE_LOCK_CONFLICT );
}
}
//
// Check request beyond end of file if this is not a read on a volume
// handle marked for extended DASD IO.
//
if ((TypeOfOpen != UserVolumeOpen) ||
(!FlagOn( Ccb->Flags, CCB_FLAG_ALLOW_EXTENDED_DASD_IO ))) {
//
// Complete the request if it begins beyond the end of file.
//
if (StartingOffset >= Fcb->FileSize.QuadPart) {
try_return( Status = STATUS_END_OF_FILE );
}
//
// Truncate the read if it extends beyond the end of the file.
//
if (ByteRange > Fcb->FileSize.QuadPart) {
ByteCount = (ULONG) (Fcb->FileSize.QuadPart - StartingOffset);
ByteRange = Fcb->FileSize.QuadPart;
}
}
//
// Handle the non-cached read first.
//
if (NonCachedIo) {
//
// If we have an unaligned transfer then post this request if
// we can't wait. Unaligned means that the starting offset
// is not on a sector boundary or the read is not integral
// sectors.
//
ReadByteCount = BlockAlign( Fcb->Vcb, ByteCount );
if (SectorOffset( StartingOffset ) ||
SectorOffset( ReadByteCount ) ||
(ReadByteCount > OriginalByteCount)) {
if (!Wait) {
CdRaiseStatus( IrpContext, STATUS_CANT_WAIT );
}
//
// Make sure we don't overwrite the buffer.
//
ReadByteCount = ByteCount;
}
//
// Initialize the IoContext for the read.
// If there is a context pointer, we need to make sure it was
// allocated and not a stale stack pointer.
//
if (IrpContext->IoContext == NULL ||
!FlagOn( IrpContext->Flags, IRP_CONTEXT_FLAG_ALLOC_IO )) {
//
// If we can wait, use the context on the stack. Otherwise
// we need to allocate one.
//
if (Wait) {
IrpContext->IoContext = &LocalIoContext;
ClearFlag( IrpContext->Flags, IRP_CONTEXT_FLAG_ALLOC_IO );
} else {
IrpContext->IoContext = CdAllocateIoContext();
SetFlag( IrpContext->Flags, IRP_CONTEXT_FLAG_ALLOC_IO );
}
}
RtlZeroMemory( IrpContext->IoContext, sizeof( CD_IO_CONTEXT ));
//
// Store whether we allocated this context structure in the structure
// itself.
//
IrpContext->IoContext->AllocatedContext =
BooleanFlagOn( IrpContext->Flags, IRP_CONTEXT_FLAG_ALLOC_IO );
if (Wait) {
KeInitializeEvent( &IrpContext->IoContext->SyncEvent,
NotificationEvent,
FALSE );
} else {
IrpContext->IoContext->ResourceThreadId = ExGetCurrentResourceThread();
IrpContext->IoContext->Resource = Fcb->Resource;
IrpContext->IoContext->RequestedByteCount = ByteCount;
}
Irp->IoStatus.Information = ReadByteCount;
//
// Call one of the NonCacheIo routines to perform the actual
// read.
//
if (FlagOn( Fcb->FcbState, FCB_STATE_RAWSECTOR_MASK )) {
Status = CdNonCachedXARead( IrpContext, Fcb, StartingOffset, ReadByteCount );
} else {
Status = CdNonCachedRead( IrpContext, Fcb, StartingOffset, ReadByteCount );
}
//
// Don't complete this request now if STATUS_PENDING was returned.
//
if (Status == STATUS_PENDING) {
Irp = NULL;
ReleaseFile = FALSE;
//
// Test is we should zero part of the buffer or update the
// synchronous file position.
//
} else {
//
// Convert any unknown error code to IO_ERROR.
//
if (!NT_SUCCESS( Status )) {
//
// Set the information field to zero.
//
Irp->IoStatus.Information = 0;
//
// Raise if this is a user induced error.
//
if (IoIsErrorUserInduced( Status )) {
CdRaiseStatus( IrpContext, Status );
}
Status = FsRtlNormalizeNtstatus( Status, STATUS_UNEXPECTED_IO_ERROR );
//
// Check if there is any portion of the user's buffer to zero.
//
} else if (ReadByteCount != ByteCount) {
CdMapUserBuffer( IrpContext, &UserBuffer);
SafeZeroMemory( IrpContext,
Add2Ptr( UserBuffer,
ByteCount,
PVOID ),
ReadByteCount - ByteCount );
Irp->IoStatus.Information = ByteCount;
}
//
// Update the file position if this is a synchronous request.
//
if (SynchronousIo && !PagingIo && NT_SUCCESS( Status )) {
IrpSp->FileObject->CurrentByteOffset.QuadPart = ByteRange;
}
}
try_return( NOTHING );
}
//
// Handle the cached case. Start by initializing the private
// cache map.
//
if (IrpSp->FileObject->PrivateCacheMap == NULL) {
//
// Now initialize the cache map.
//
CcInitializeCacheMap( IrpSp->FileObject,
(PCC_FILE_SIZES) &Fcb->AllocationSize,
FALSE,
&CdData.CacheManagerCallbacks,
Fcb );
CcSetReadAheadGranularity( IrpSp->FileObject, READ_AHEAD_GRANULARITY );
}
//
// Read from the cache if this is not an Mdl read.
//
if (!FlagOn( IrpContext->MinorFunction, IRP_MN_MDL )) {
//
// If we are in the Fsp now because we had to wait earlier,
// we must map the user buffer, otherwise we can use the
// user's buffer directly.
//
CdMapUserBuffer( IrpContext, &SystemBuffer );
//
// Now try to do the copy.
//
if (!CcCopyRead( IrpSp->FileObject,
(PLARGE_INTEGER) &StartingOffset,
ByteCount,
Wait,
SystemBuffer,
&Irp->IoStatus )) {
try_return( Status = STATUS_CANT_WAIT );
}
//
// If the call didn't succeed, raise the error status
//
if (!NT_SUCCESS( Irp->IoStatus.Status )) {
CdNormalizeAndRaiseStatus( IrpContext, Irp->IoStatus.Status );
}
//
// Otherwise perform the MdlRead operation.
//
} else {
CcMdlRead( IrpSp->FileObject,
(PLARGE_INTEGER) &StartingOffset,
ByteCount,
&Irp->MdlAddress,
&Irp->IoStatus );
Status = Irp->IoStatus.Status;
}
//
// Update the current file position in the user file object.
//
if (SynchronousIo && !PagingIo && NT_SUCCESS( Status )) {
IrpSp->FileObject->CurrentByteOffset.QuadPart = ByteRange;
}
try_exit: NOTHING;
} _SEH2_FINALLY {
//
// Release the Fcb.
//
if (ReleaseFile) {
CdReleaseFile( IrpContext, Fcb );
}
} _SEH2_END;
//
// Post the request if we got CANT_WAIT.
//
if (Status == STATUS_CANT_WAIT) {
Status = CdFsdPostRequest( IrpContext, Irp );
//
// Otherwise complete the request.
//
} else {
CdCompleteRequest( IrpContext, Irp, Status );
}
return Status;
}