2009-01-18 10:23:15 +00:00
|
|
|
/*
|
|
|
|
* PROJECT: ReactOS Kernel
|
|
|
|
* LICENSE: GPL - See COPYING in the top level directory
|
|
|
|
* FILE: ntoskrnl/fsrtl/fastio.c
|
|
|
|
* PURPOSE: Provides Fast I/O entrypoints to the Cache Manager
|
2013-08-29 22:24:46 +00:00
|
|
|
* PROGRAMMERS: Dominique Cote (buzdelabuz2@gmail.com)
|
|
|
|
* Alex Ionescu (alex.ionescu@reactos.org)
|
|
|
|
* Aleksey Bragin (aleksey@reactos.org)
|
2009-01-18 10:23:15 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
/* INCLUDES ******************************************************************/
|
|
|
|
|
|
|
|
#include <ntoskrnl.h>
|
|
|
|
#define NDEBUG
|
|
|
|
#include <debug.h>
|
|
|
|
|
|
|
|
/* PUBLIC FUNCTIONS **********************************************************/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* @implemented
|
|
|
|
*/
|
|
|
|
VOID
|
|
|
|
NTAPI
|
|
|
|
FsRtlIncrementCcFastReadResourceMiss(VOID)
|
|
|
|
{
|
|
|
|
CcFastReadResourceMiss++;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* @implemented
|
|
|
|
*/
|
|
|
|
VOID
|
|
|
|
NTAPI
|
|
|
|
FsRtlIncrementCcFastReadNotPossible(VOID)
|
|
|
|
{
|
|
|
|
CcFastReadNotPossible++;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* @implemented
|
|
|
|
*/
|
|
|
|
VOID
|
|
|
|
NTAPI
|
|
|
|
FsRtlIncrementCcFastReadWait(VOID)
|
|
|
|
{
|
|
|
|
CcFastReadWait++;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* @implemented
|
|
|
|
*/
|
|
|
|
VOID
|
|
|
|
NTAPI
|
|
|
|
FsRtlIncrementCcFastReadNoWait(VOID)
|
|
|
|
{
|
|
|
|
CcFastReadNoWait++;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* @implemented
|
|
|
|
*/
|
|
|
|
BOOLEAN
|
|
|
|
NTAPI
|
|
|
|
FsRtlCopyRead(IN PFILE_OBJECT FileObject,
|
|
|
|
IN PLARGE_INTEGER FileOffset,
|
|
|
|
IN ULONG Length,
|
|
|
|
IN BOOLEAN Wait,
|
|
|
|
IN ULONG LockKey,
|
|
|
|
OUT PVOID Buffer,
|
|
|
|
OUT PIO_STATUS_BLOCK IoStatus,
|
|
|
|
IN PDEVICE_OBJECT DeviceObject)
|
|
|
|
{
|
|
|
|
|
|
|
|
PFSRTL_COMMON_FCB_HEADER FcbHeader;
|
|
|
|
LARGE_INTEGER Offset;
|
|
|
|
PFAST_IO_DISPATCH FastIoDispatch;
|
|
|
|
PDEVICE_OBJECT Device;
|
|
|
|
BOOLEAN Result = TRUE;
|
2009-11-16 01:56:48 +00:00
|
|
|
ULONG PageCount = ADDRESS_AND_SIZE_TO_SPAN_PAGES(FileOffset, Length);
|
2009-01-18 10:23:15 +00:00
|
|
|
|
2009-11-16 01:56:48 +00:00
|
|
|
PAGED_CODE();
|
2009-01-18 10:23:15 +00:00
|
|
|
ASSERT(FileObject);
|
|
|
|
ASSERT(FileObject->FsContext);
|
|
|
|
|
|
|
|
/* No actual read */
|
|
|
|
if (!Length)
|
|
|
|
{
|
|
|
|
/* Return success */
|
|
|
|
IoStatus->Status = STATUS_SUCCESS;
|
|
|
|
IoStatus->Information = 0;
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2009-11-16 01:57:24 +00:00
|
|
|
if (Length > MAXLONGLONG - FileOffset->QuadPart)
|
2009-11-16 01:56:48 +00:00
|
|
|
{
|
|
|
|
IoStatus->Status = STATUS_INVALID_PARAMETER;
|
|
|
|
IoStatus->Information = 0;
|
|
|
|
return FALSE;
|
2009-01-18 10:23:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Get the offset and FCB header */
|
|
|
|
Offset.QuadPart = FileOffset->QuadPart + Length;
|
|
|
|
FcbHeader = (PFSRTL_COMMON_FCB_HEADER)FileObject->FsContext;
|
|
|
|
|
2009-11-16 01:56:48 +00:00
|
|
|
if (Wait)
|
|
|
|
{
|
2009-01-18 10:23:15 +00:00
|
|
|
/* Use a Resource Acquire */
|
|
|
|
FsRtlEnterFileSystem();
|
2009-11-16 01:56:48 +00:00
|
|
|
CcFastReadWait++;
|
2009-01-18 10:23:15 +00:00
|
|
|
ExAcquireResourceSharedLite(FcbHeader->Resource, TRUE);
|
2009-11-16 01:56:48 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* Acquire the resource without blocking. Return false and the I/O manager
|
|
|
|
* will retry using the standard IRP method. Use a Resource Acquire.
|
|
|
|
*/
|
2009-01-18 10:23:15 +00:00
|
|
|
FsRtlEnterFileSystem();
|
2009-11-16 01:56:48 +00:00
|
|
|
if (!ExAcquireResourceSharedLite(FcbHeader->Resource, FALSE))
|
|
|
|
{
|
|
|
|
FsRtlExitFileSystem();
|
|
|
|
FsRtlIncrementCcFastReadResourceMiss();
|
|
|
|
return FALSE;
|
2009-01-18 10:23:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Check if this is a fast I/O cached file */
|
|
|
|
if (!(FileObject->PrivateCacheMap) ||
|
2009-11-16 01:56:48 +00:00
|
|
|
(FcbHeader->IsFastIoPossible == FastIoIsNotPossible))
|
|
|
|
{
|
2009-01-18 10:23:15 +00:00
|
|
|
/* It's not, so fail */
|
2009-11-16 01:56:48 +00:00
|
|
|
Result = FALSE;
|
|
|
|
goto Cleanup;
|
2009-01-18 10:23:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Check if we need to find out if fast I/O is available */
|
|
|
|
if (FcbHeader->IsFastIoPossible == FastIoIsQuestionable)
|
|
|
|
{
|
|
|
|
/* Sanity check */
|
|
|
|
ASSERT(!KeIsExecutingDpc());
|
|
|
|
|
|
|
|
/* Get the Fast I/O table */
|
|
|
|
Device = IoGetRelatedDeviceObject(FileObject);
|
2009-11-16 01:56:48 +00:00
|
|
|
FastIoDispatch = Device->DriverObject->FastIoDispatch;
|
2009-01-18 10:23:15 +00:00
|
|
|
|
|
|
|
/* Sanity check */
|
|
|
|
ASSERT(FastIoDispatch != NULL);
|
|
|
|
ASSERT(FastIoDispatch->FastIoCheckIfPossible != NULL);
|
|
|
|
|
|
|
|
/* Ask the driver if we can do it */
|
|
|
|
if (!FastIoDispatch->FastIoCheckIfPossible(FileObject,
|
|
|
|
FileOffset,
|
|
|
|
Length,
|
|
|
|
TRUE,
|
|
|
|
LockKey,
|
|
|
|
TRUE,
|
|
|
|
IoStatus,
|
|
|
|
Device))
|
|
|
|
{
|
|
|
|
/* It's not, fail */
|
|
|
|
Result = FALSE;
|
|
|
|
goto Cleanup;
|
|
|
|
}
|
2009-11-16 01:56:48 +00:00
|
|
|
}
|
2009-01-18 10:23:15 +00:00
|
|
|
|
|
|
|
/* Check if we read too much */
|
2009-11-16 01:56:48 +00:00
|
|
|
if (Offset.QuadPart > FcbHeader->FileSize.QuadPart)
|
|
|
|
{
|
2009-01-18 10:23:15 +00:00
|
|
|
/* We did, check if the file offset is past the end */
|
2009-11-16 01:56:48 +00:00
|
|
|
if (FileOffset->QuadPart >= FcbHeader->FileSize.QuadPart)
|
|
|
|
{
|
2009-01-18 10:23:15 +00:00
|
|
|
/* Set end of file */
|
|
|
|
IoStatus->Status = STATUS_END_OF_FILE;
|
|
|
|
IoStatus->Information = 0;
|
|
|
|
goto Cleanup;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Otherwise, just normalize the length */
|
|
|
|
Length = (ULONG)(FcbHeader->FileSize.QuadPart - FileOffset->QuadPart);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Set this as top-level IRP */
|
2009-11-16 01:56:48 +00:00
|
|
|
PsGetCurrentThread()->TopLevelIrp = FSRTL_FAST_IO_TOP_LEVEL_IRP;
|
2009-01-18 10:23:15 +00:00
|
|
|
|
2009-11-16 01:56:48 +00:00
|
|
|
_SEH2_TRY
|
|
|
|
{
|
|
|
|
/* Make sure the IO and file size is below 4GB */
|
|
|
|
if (Wait && !(Offset.HighPart | FcbHeader->FileSize.HighPart))
|
|
|
|
{
|
2009-01-18 10:23:15 +00:00
|
|
|
|
2009-11-16 01:56:48 +00:00
|
|
|
/* Call the cache controller */
|
|
|
|
CcFastCopyRead(FileObject,
|
|
|
|
FileOffset->LowPart,
|
|
|
|
Length,
|
|
|
|
PageCount,
|
|
|
|
Buffer,
|
|
|
|
IoStatus);
|
2009-01-18 10:23:15 +00:00
|
|
|
|
2009-11-16 01:56:48 +00:00
|
|
|
/* File was accessed */
|
|
|
|
FileObject->Flags |= FO_FILE_FAST_IO_READ;
|
|
|
|
|
|
|
|
if (IoStatus->Status != STATUS_END_OF_FILE)
|
|
|
|
{
|
2011-09-14 14:01:41 +00:00
|
|
|
ASSERT((ULONGLONG)FcbHeader->FileSize.QuadPart >=
|
[HAL/NDK]
- Make Vector parameter in HalEnableSystemInterrupt, HalDisableSystemInterrupt and HalBeginSystemInterrupt an ULONG, not an UCHAR
[NDK]
- 64bit fixes for HANDLE_TABLE, KPROCESS, SECTION_IMAGE_INFORMATION, MMADDRESS_LIST, MMVAD_FLAGS, MMVAD, MMVAD_LONG, MMVAD_SHORT, MEMORY_DESCRIPTOR, MEMORY_ALLOCATION_DESCRIPTOR, LdrVerifyMappedImageMatchesChecksum
- KDPC_DATA::DpcQueueDepth is signed on amd64, unsigned on x86
[NTOSKRNL]
- Fix hundreds of MSVC and amd64 warnings
- add a pragma message to FstubFixupEfiPartition, since it looks broken
- Move portable Ke constants from <arch>/cpu.c to krnlinit.c
- Fixed a bug in amd64 KiGeneralProtectionFaultHandler
svn path=/trunk/; revision=53734
2011-09-18 13:11:45 +00:00
|
|
|
((ULONGLONG)FileOffset->QuadPart + IoStatus->Information));
|
2009-11-16 01:56:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2009-01-18 10:23:15 +00:00
|
|
|
|
|
|
|
/* Call the cache controller */
|
2009-11-16 01:56:48 +00:00
|
|
|
Result = CcCopyRead(FileObject,
|
|
|
|
FileOffset,
|
|
|
|
Length,
|
|
|
|
Wait,
|
|
|
|
Buffer,
|
|
|
|
IoStatus);
|
|
|
|
|
2009-01-18 10:23:15 +00:00
|
|
|
/* File was accessed */
|
2009-11-16 01:56:48 +00:00
|
|
|
FileObject->Flags |= FO_FILE_FAST_IO_READ;
|
|
|
|
|
2014-11-15 22:47:51 +00:00
|
|
|
if (Result != FALSE)
|
2009-11-16 01:56:48 +00:00
|
|
|
{
|
|
|
|
ASSERT((IoStatus->Status == STATUS_END_OF_FILE) ||
|
[HAL/NDK]
- Make Vector parameter in HalEnableSystemInterrupt, HalDisableSystemInterrupt and HalBeginSystemInterrupt an ULONG, not an UCHAR
[NDK]
- 64bit fixes for HANDLE_TABLE, KPROCESS, SECTION_IMAGE_INFORMATION, MMADDRESS_LIST, MMVAD_FLAGS, MMVAD, MMVAD_LONG, MMVAD_SHORT, MEMORY_DESCRIPTOR, MEMORY_ALLOCATION_DESCRIPTOR, LdrVerifyMappedImageMatchesChecksum
- KDPC_DATA::DpcQueueDepth is signed on amd64, unsigned on x86
[NTOSKRNL]
- Fix hundreds of MSVC and amd64 warnings
- add a pragma message to FstubFixupEfiPartition, since it looks broken
- Move portable Ke constants from <arch>/cpu.c to krnlinit.c
- Fixed a bug in amd64 KiGeneralProtectionFaultHandler
svn path=/trunk/; revision=53734
2011-09-18 13:11:45 +00:00
|
|
|
(((ULONGLONG)FileOffset->QuadPart + IoStatus->Information) <=
|
2011-09-14 14:01:41 +00:00
|
|
|
(ULONGLONG)FcbHeader->FileSize.QuadPart));
|
2009-11-16 01:56:48 +00:00
|
|
|
}
|
|
|
|
}
|
2009-01-18 10:23:15 +00:00
|
|
|
|
|
|
|
/* Update the current file offset */
|
2014-11-15 22:47:51 +00:00
|
|
|
if (Result != FALSE)
|
2009-11-16 01:56:48 +00:00
|
|
|
{
|
2013-08-31 16:36:34 +00:00
|
|
|
FileObject->CurrentByteOffset.QuadPart = FileOffset->QuadPart + IoStatus->Information;
|
2009-11-16 01:56:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
_SEH2_EXCEPT(FsRtlIsNtstatusExpected(_SEH2_GetExceptionCode()) ?
|
|
|
|
EXCEPTION_EXECUTE_HANDLER : EXCEPTION_CONTINUE_SEARCH)
|
|
|
|
{
|
|
|
|
Result = FALSE;
|
|
|
|
}
|
|
|
|
_SEH2_END;
|
2009-01-18 10:23:15 +00:00
|
|
|
|
2009-11-16 01:56:48 +00:00
|
|
|
PsGetCurrentThread()->TopLevelIrp = 0;
|
2009-01-18 10:23:15 +00:00
|
|
|
|
|
|
|
/* Return to caller */
|
|
|
|
Cleanup:
|
|
|
|
|
|
|
|
ExReleaseResourceLite(FcbHeader->Resource);
|
|
|
|
FsRtlExitFileSystem();
|
|
|
|
|
2009-11-16 01:56:48 +00:00
|
|
|
if (Result == FALSE)
|
|
|
|
{
|
2009-01-18 10:23:15 +00:00
|
|
|
CcFastReadNotPossible += 1;
|
2009-11-16 01:56:48 +00:00
|
|
|
}
|
2009-01-18 10:23:15 +00:00
|
|
|
|
|
|
|
return Result;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* @implemented
|
|
|
|
*/
|
|
|
|
BOOLEAN
|
|
|
|
NTAPI
|
|
|
|
FsRtlCopyWrite(IN PFILE_OBJECT FileObject,
|
|
|
|
IN PLARGE_INTEGER FileOffset,
|
|
|
|
IN ULONG Length,
|
|
|
|
IN BOOLEAN Wait,
|
|
|
|
IN ULONG LockKey,
|
|
|
|
OUT PVOID Buffer,
|
|
|
|
OUT PIO_STATUS_BLOCK IoStatus,
|
|
|
|
IN PDEVICE_OBJECT DeviceObject)
|
|
|
|
{
|
|
|
|
BOOLEAN Result = TRUE;
|
|
|
|
PFAST_IO_DISPATCH FastIoDispatch;
|
|
|
|
PDEVICE_OBJECT Device;
|
|
|
|
PFSRTL_COMMON_FCB_HEADER FcbHeader;
|
2009-11-16 01:56:48 +00:00
|
|
|
PSHARED_CACHE_MAP SharedCacheMap;
|
2009-01-18 10:23:15 +00:00
|
|
|
|
2009-11-16 01:56:48 +00:00
|
|
|
/* WDK doc.
|
|
|
|
* Offset == 0xffffffffffffffff indicates append to the end of file.
|
|
|
|
*/
|
|
|
|
BOOLEAN FileOffsetAppend = (FileOffset->HighPart == (LONG)0xffffffff) &&
|
|
|
|
(FileOffset->LowPart == 0xffffffff);
|
2009-01-18 10:23:15 +00:00
|
|
|
|
2013-08-31 16:36:34 +00:00
|
|
|
BOOLEAN ResourceAcquiredShared = FALSE;
|
2009-01-18 10:23:15 +00:00
|
|
|
BOOLEAN b_4GB = FALSE;
|
|
|
|
BOOLEAN FileSizeModified = FALSE;
|
|
|
|
LARGE_INTEGER OldFileSize;
|
|
|
|
LARGE_INTEGER OldValidDataLength;
|
|
|
|
LARGE_INTEGER NewSize;
|
|
|
|
LARGE_INTEGER Offset;
|
|
|
|
|
|
|
|
PAGED_CODE();
|
|
|
|
|
|
|
|
ASSERT(FileObject);
|
|
|
|
ASSERT(FileObject->FsContext);
|
|
|
|
|
|
|
|
/* Initialize some of the vars and pointers */
|
|
|
|
NewSize.QuadPart = 0;
|
|
|
|
Offset.QuadPart = FileOffset->QuadPart + Length;
|
|
|
|
FcbHeader = (PFSRTL_COMMON_FCB_HEADER)FileObject->FsContext;
|
|
|
|
|
2009-11-16 01:56:48 +00:00
|
|
|
/* Nagar p.544.
|
|
|
|
* Check with Cc if we can write and check if the IO > 64kB (WDK macro).
|
|
|
|
*/
|
|
|
|
if ((CcCanIWrite(FileObject, Length, Wait, FALSE) == FALSE) ||
|
|
|
|
(CcCopyWriteWontFlush(FileObject, FileOffset, Length) == FALSE) ||
|
2011-11-19 15:45:20 +00:00
|
|
|
((FileObject->Flags & FO_WRITE_THROUGH)))
|
2009-01-18 10:23:15 +00:00
|
|
|
{
|
2009-11-16 01:56:48 +00:00
|
|
|
return FALSE;
|
2009-01-18 10:23:15 +00:00
|
|
|
}
|
|
|
|
|
2018-08-17 17:12:30 +00:00
|
|
|
/* Already init IO_STATUS_BLOCK */
|
|
|
|
IoStatus->Status = STATUS_SUCCESS;
|
|
|
|
IoStatus->Information = Length;
|
|
|
|
|
2009-01-18 10:23:15 +00:00
|
|
|
/* No actual read */
|
|
|
|
if (!Length)
|
|
|
|
{
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
FsRtlEnterFileSystem();
|
|
|
|
|
2009-11-16 01:56:48 +00:00
|
|
|
/* Nagar p.544/545.
|
|
|
|
* The CcFastCopyWrite doesn't deal with filesize beyond 4GB.
|
|
|
|
*/
|
2009-01-18 10:23:15 +00:00
|
|
|
if (Wait && (FcbHeader->AllocationSize.HighPart == 0))
|
|
|
|
{
|
2009-11-16 01:56:48 +00:00
|
|
|
/* If the file offset is not past the file size,
|
|
|
|
* then we can acquire the lock shared.
|
|
|
|
*/
|
|
|
|
if ((FileOffsetAppend == FALSE) &&
|
|
|
|
(Offset.LowPart <= FcbHeader->ValidDataLength.LowPart))
|
|
|
|
{
|
|
|
|
ExAcquireResourceSharedLite(FcbHeader->Resource, TRUE);
|
2013-08-31 16:36:34 +00:00
|
|
|
ResourceAcquiredShared = TRUE;
|
2009-11-16 01:56:48 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
ExAcquireResourceExclusiveLite(FcbHeader->Resource, TRUE);
|
2009-01-18 10:23:15 +00:00
|
|
|
}
|
|
|
|
|
2009-11-16 01:56:48 +00:00
|
|
|
/* Nagar p.544/545.
|
|
|
|
* If we append, use the file size as offset.
|
|
|
|
* Also, check that we aren't crossing the 4GB boundary.
|
|
|
|
*/
|
2014-11-15 22:47:51 +00:00
|
|
|
if (FileOffsetAppend != FALSE)
|
2009-11-16 01:56:48 +00:00
|
|
|
{
|
|
|
|
Offset.LowPart = FcbHeader->FileSize.LowPart;
|
|
|
|
NewSize.LowPart = FcbHeader->FileSize.LowPart + Length;
|
|
|
|
b_4GB = (NewSize.LowPart < FcbHeader->FileSize.LowPart);
|
2009-01-18 10:23:15 +00:00
|
|
|
|
2009-11-16 01:56:48 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
Offset.LowPart = FileOffset->LowPart;
|
|
|
|
NewSize.LowPart = FileOffset->LowPart + Length;
|
|
|
|
b_4GB = (NewSize.LowPart < FileOffset->LowPart) ||
|
|
|
|
(FileOffset->HighPart != 0);
|
2009-01-18 10:23:15 +00:00
|
|
|
}
|
|
|
|
|
2009-11-16 01:56:48 +00:00
|
|
|
/* Nagar p.544/545.
|
|
|
|
* Make sure that caching is initated.
|
|
|
|
* That fast are allowed for this file stream.
|
|
|
|
* That we are not extending past the allocated size.
|
|
|
|
* That we are not creating a hole bigger than 8k.
|
|
|
|
* That we are not crossing the 4GB boundary.
|
|
|
|
*/
|
|
|
|
if ((FileObject->PrivateCacheMap != NULL) &&
|
|
|
|
(FcbHeader->IsFastIoPossible != FastIoIsNotPossible) &&
|
|
|
|
(FcbHeader->AllocationSize.LowPart >= NewSize.LowPart) &&
|
|
|
|
(Offset.LowPart < FcbHeader->ValidDataLength.LowPart + 0x2000) &&
|
|
|
|
!b_4GB)
|
2009-01-18 10:23:15 +00:00
|
|
|
{
|
2009-11-16 01:56:48 +00:00
|
|
|
/* If we are extending past the file size, we need to
|
|
|
|
* release the lock and acquire it exclusively, because
|
|
|
|
* we are going to need to update the FcbHeader.
|
|
|
|
*/
|
2013-08-31 16:36:34 +00:00
|
|
|
if (ResourceAcquiredShared &&
|
2009-11-16 01:56:48 +00:00
|
|
|
(NewSize.LowPart > FcbHeader->ValidDataLength.LowPart + 0x2000))
|
|
|
|
{
|
|
|
|
/* Then we need to acquire the resource exclusive */
|
|
|
|
ExReleaseResourceLite(FcbHeader->Resource);
|
|
|
|
ExAcquireResourceExclusiveLite(FcbHeader->Resource, TRUE);
|
2014-11-15 22:47:51 +00:00
|
|
|
if (FileOffsetAppend != FALSE)
|
2009-11-16 01:56:48 +00:00
|
|
|
{
|
|
|
|
Offset.LowPart = FcbHeader->FileSize.LowPart; // ??
|
|
|
|
NewSize.LowPart = FcbHeader->FileSize.LowPart + Length;
|
|
|
|
|
|
|
|
/* Make sure we don't cross the 4GB boundary */
|
|
|
|
b_4GB = (NewSize.LowPart < Offset.LowPart);
|
|
|
|
}
|
2009-01-18 10:23:15 +00:00
|
|
|
|
|
|
|
/* Recheck some of the conditions since we let the lock go */
|
2009-11-16 01:56:48 +00:00
|
|
|
if ((FileObject->PrivateCacheMap != NULL) &&
|
|
|
|
(FcbHeader->IsFastIoPossible != FastIoIsNotPossible) &&
|
|
|
|
(FcbHeader->AllocationSize.LowPart >= NewSize.LowPart) &&
|
|
|
|
(FcbHeader->AllocationSize.HighPart == 0) &&
|
|
|
|
!b_4GB)
|
|
|
|
{
|
|
|
|
/* Do nothing? */
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
goto FailAndCleanup;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
else
|
2009-01-18 10:23:15 +00:00
|
|
|
{
|
2009-11-16 01:56:48 +00:00
|
|
|
goto FailAndCleanup;
|
2009-01-18 10:23:15 +00:00
|
|
|
}
|
|
|
|
|
2009-11-16 01:56:48 +00:00
|
|
|
/* Check if we need to find out if fast I/O is available */
|
|
|
|
if (FcbHeader->IsFastIoPossible == FastIoIsQuestionable)
|
|
|
|
{
|
|
|
|
IO_STATUS_BLOCK FastIoCheckIfPossibleStatus;
|
|
|
|
|
|
|
|
/* Sanity check */
|
|
|
|
ASSERT(!KeIsExecutingDpc());
|
|
|
|
|
|
|
|
/* Get the Fast I/O table */
|
|
|
|
Device = IoGetRelatedDeviceObject(FileObject);
|
|
|
|
FastIoDispatch = Device->DriverObject->FastIoDispatch;
|
|
|
|
|
|
|
|
/* Sanity check */
|
|
|
|
ASSERT(FastIoDispatch != NULL);
|
|
|
|
ASSERT(FastIoDispatch->FastIoCheckIfPossible != NULL);
|
|
|
|
|
|
|
|
/* Ask the driver if we can do it */
|
|
|
|
if (!FastIoDispatch->FastIoCheckIfPossible(FileObject,
|
|
|
|
FileOffsetAppend ?
|
|
|
|
&FcbHeader->FileSize :
|
|
|
|
FileOffset,
|
|
|
|
Length,
|
|
|
|
TRUE,
|
|
|
|
LockKey,
|
|
|
|
FALSE,
|
|
|
|
&FastIoCheckIfPossibleStatus,
|
|
|
|
Device))
|
|
|
|
{
|
|
|
|
/* It's not, fail */
|
|
|
|
goto FailAndCleanup;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* If we are going to extend the file then save
|
|
|
|
* the old file size in case the operation fails.
|
|
|
|
*/
|
|
|
|
if (NewSize.LowPart > FcbHeader->FileSize.LowPart)
|
|
|
|
{
|
|
|
|
FileSizeModified = TRUE;
|
|
|
|
OldFileSize.LowPart = FcbHeader->FileSize.LowPart;
|
|
|
|
OldValidDataLength.LowPart = FcbHeader->ValidDataLength.LowPart;
|
|
|
|
FcbHeader->FileSize.LowPart = NewSize.LowPart;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Set this as top-level IRP */
|
|
|
|
PsGetCurrentThread()->TopLevelIrp = FSRTL_FAST_IO_TOP_LEVEL_IRP;
|
|
|
|
|
|
|
|
_SEH2_TRY
|
|
|
|
{
|
|
|
|
if (Offset.LowPart > FcbHeader->ValidDataLength.LowPart)
|
|
|
|
{
|
|
|
|
LARGE_INTEGER OffsetVar;
|
|
|
|
OffsetVar.LowPart = Offset.LowPart;
|
|
|
|
OffsetVar.HighPart = 0;
|
|
|
|
CcZeroData(FileObject, &FcbHeader->ValidDataLength, &OffsetVar, TRUE);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Call the cache manager */
|
|
|
|
CcFastCopyWrite(FileObject, Offset.LowPart, Length, Buffer);
|
|
|
|
}
|
|
|
|
_SEH2_EXCEPT(FsRtlIsNtstatusExpected(_SEH2_GetExceptionCode()) ?
|
|
|
|
EXCEPTION_EXECUTE_HANDLER :
|
|
|
|
EXCEPTION_CONTINUE_SEARCH)
|
|
|
|
{
|
|
|
|
Result = FALSE;
|
|
|
|
}
|
|
|
|
_SEH2_END;
|
2009-01-18 10:23:15 +00:00
|
|
|
|
|
|
|
/* Remove ourselves at the top level component after the IO is done */
|
2009-11-16 01:56:48 +00:00
|
|
|
PsGetCurrentThread()->TopLevelIrp = 0;
|
2009-01-18 10:23:15 +00:00
|
|
|
|
2009-11-16 01:56:48 +00:00
|
|
|
/* Did the operation succeed? */
|
2014-11-15 22:47:51 +00:00
|
|
|
if (Result != FALSE)
|
2009-11-16 01:56:48 +00:00
|
|
|
{
|
|
|
|
/* Update the valid file size if necessary */
|
|
|
|
if (NewSize.LowPart > FcbHeader->ValidDataLength.LowPart)
|
|
|
|
{
|
|
|
|
FcbHeader->ValidDataLength.LowPart = NewSize.LowPart;
|
|
|
|
}
|
2009-01-18 10:23:15 +00:00
|
|
|
|
|
|
|
/* Flag the file as modified */
|
2009-11-16 01:56:48 +00:00
|
|
|
FileObject->Flags |= FO_FILE_MODIFIED;
|
2009-01-18 10:23:15 +00:00
|
|
|
|
2009-11-16 01:56:48 +00:00
|
|
|
/* Update the strucutres if the file size changed */
|
|
|
|
if (FileSizeModified)
|
|
|
|
{
|
|
|
|
SharedCacheMap =
|
|
|
|
(PSHARED_CACHE_MAP)FileObject->SectionObjectPointer->SharedCacheMap;
|
|
|
|
SharedCacheMap->FileSize.LowPart = NewSize.LowPart;
|
|
|
|
FileObject->Flags |= FO_FILE_SIZE_CHANGED;
|
|
|
|
}
|
2009-01-18 10:23:15 +00:00
|
|
|
|
|
|
|
/* Update the file object current file offset */
|
2009-11-16 01:56:48 +00:00
|
|
|
FileObject->CurrentByteOffset.QuadPart = NewSize.LowPart;
|
|
|
|
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* Result == FALSE if we get here */
|
|
|
|
if (FileSizeModified)
|
|
|
|
{
|
|
|
|
/* If the file size was modified then restore the old file size */
|
|
|
|
if (FcbHeader->PagingIoResource != NULL)
|
|
|
|
{
|
|
|
|
/* Nagar P.544.
|
|
|
|
* Restore the old file size if operation didn't succeed.
|
|
|
|
*/
|
|
|
|
ExAcquireResourceExclusiveLite(FcbHeader->PagingIoResource, TRUE);
|
|
|
|
FcbHeader->FileSize.LowPart = OldFileSize.LowPart;
|
|
|
|
FcbHeader->ValidDataLength.LowPart = OldValidDataLength.LowPart;
|
|
|
|
ExReleaseResourceLite(FcbHeader->PagingIoResource);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* If there is no lock and do it without */
|
|
|
|
FcbHeader->FileSize.LowPart = OldFileSize.LowPart;
|
|
|
|
FcbHeader->ValidDataLength.LowPart = OldValidDataLength.LowPart;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* Do nothing? */
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
goto Cleanup;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
LARGE_INTEGER OldFileSize;
|
|
|
|
|
|
|
|
/* Sanity check */
|
|
|
|
ASSERT(!KeIsExecutingDpc());
|
|
|
|
|
|
|
|
/* Nagar P.544.
|
|
|
|
* Check if we need to acquire the resource exclusive.
|
|
|
|
*/
|
|
|
|
if ((FileOffsetAppend == FALSE) &&
|
|
|
|
(FileOffset->QuadPart + Length <= FcbHeader->ValidDataLength.QuadPart))
|
|
|
|
{
|
|
|
|
/* Acquire the resource shared */
|
|
|
|
if (!ExAcquireResourceSharedLite(FcbHeader->Resource, Wait))
|
|
|
|
{
|
|
|
|
goto LeaveCriticalAndFail;
|
|
|
|
}
|
2013-08-31 16:36:34 +00:00
|
|
|
ResourceAcquiredShared = TRUE;
|
2009-11-16 01:56:48 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* Acquire the resource exclusive */
|
|
|
|
if (!ExAcquireResourceExclusiveLite(FcbHeader->Resource, Wait))
|
|
|
|
{
|
|
|
|
goto LeaveCriticalAndFail;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Check if we are appending */
|
2014-11-15 22:47:51 +00:00
|
|
|
if (FileOffsetAppend != FALSE)
|
2009-11-16 01:56:48 +00:00
|
|
|
{
|
|
|
|
Offset.QuadPart = FcbHeader->FileSize.QuadPart;
|
|
|
|
NewSize.QuadPart = FcbHeader->FileSize.QuadPart + Length;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
Offset.QuadPart = FileOffset->QuadPart;
|
|
|
|
NewSize.QuadPart += FileOffset->QuadPart + Length;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Nagar p.544/545.
|
|
|
|
* Make sure that caching is initated.
|
|
|
|
* That fast are allowed for this file stream.
|
|
|
|
* That we are not extending past the allocated size.
|
|
|
|
* That we are not creating a hole bigger than 8k.
|
|
|
|
*/
|
|
|
|
if ((FileObject->PrivateCacheMap != NULL) &&
|
|
|
|
(FcbHeader->IsFastIoPossible != FastIoIsNotPossible) &&
|
|
|
|
(FcbHeader->ValidDataLength.QuadPart + 0x2000 > Offset.QuadPart) &&
|
2009-11-16 01:57:24 +00:00
|
|
|
(Length <= MAXLONGLONG - Offset.QuadPart) &&
|
2009-11-16 01:56:48 +00:00
|
|
|
(FcbHeader->AllocationSize.QuadPart >= NewSize.QuadPart))
|
|
|
|
{
|
|
|
|
/* Check if we can keep the lock shared */
|
2013-08-31 16:36:34 +00:00
|
|
|
if (ResourceAcquiredShared &&
|
2009-11-16 01:56:48 +00:00
|
|
|
(NewSize.QuadPart > FcbHeader->ValidDataLength.QuadPart))
|
|
|
|
{
|
|
|
|
ExReleaseResourceLite(FcbHeader->Resource);
|
|
|
|
if (!ExAcquireResourceExclusiveLite(FcbHeader->Resource, Wait))
|
|
|
|
{
|
|
|
|
goto LeaveCriticalAndFail;
|
|
|
|
}
|
2009-01-18 10:23:15 +00:00
|
|
|
|
|
|
|
/* Compute the offset and the new filesize */
|
2009-11-16 01:56:48 +00:00
|
|
|
if (FileOffsetAppend)
|
|
|
|
{
|
|
|
|
Offset.QuadPart = FcbHeader->FileSize.QuadPart;
|
|
|
|
NewSize.QuadPart = FcbHeader->FileSize.QuadPart + Length;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Recheck the above points since we released and reacquire the lock */
|
|
|
|
if ((FileObject->PrivateCacheMap != NULL) &&
|
|
|
|
(FcbHeader->IsFastIoPossible != FastIoIsNotPossible) &&
|
|
|
|
(FcbHeader->AllocationSize.QuadPart > NewSize.QuadPart))
|
|
|
|
{
|
|
|
|
/* Do nothing */
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
goto FailAndCleanup;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Check if we need to find out if fast I/O is available */
|
|
|
|
if (FcbHeader->IsFastIoPossible == FastIoIsQuestionable)
|
|
|
|
{
|
|
|
|
IO_STATUS_BLOCK FastIoCheckIfPossibleStatus;
|
|
|
|
|
|
|
|
/* Sanity check */
|
|
|
|
ASSERT(!KeIsExecutingDpc());
|
|
|
|
|
|
|
|
/* Get the Fast I/O table */
|
|
|
|
Device = IoGetRelatedDeviceObject(FileObject);
|
|
|
|
FastIoDispatch = Device->DriverObject->FastIoDispatch;
|
|
|
|
|
|
|
|
/* Sanity check */
|
|
|
|
ASSERT(FastIoDispatch != NULL);
|
|
|
|
ASSERT(FastIoDispatch->FastIoCheckIfPossible != NULL);
|
|
|
|
|
|
|
|
/* Ask the driver if we can do it */
|
|
|
|
if (!FastIoDispatch->FastIoCheckIfPossible(FileObject,
|
|
|
|
FileOffsetAppend ?
|
|
|
|
&FcbHeader->FileSize :
|
|
|
|
FileOffset,
|
|
|
|
Length,
|
2013-08-31 16:36:34 +00:00
|
|
|
Wait,
|
2009-11-16 01:56:48 +00:00
|
|
|
LockKey,
|
|
|
|
FALSE,
|
|
|
|
&FastIoCheckIfPossibleStatus,
|
|
|
|
Device))
|
|
|
|
{
|
|
|
|
/* It's not, fail */
|
|
|
|
goto FailAndCleanup;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* If we are going to modify the filesize,
|
|
|
|
* save the old fs in case the operation fails.
|
|
|
|
*/
|
|
|
|
if (NewSize.QuadPart > FcbHeader->FileSize.QuadPart)
|
|
|
|
{
|
|
|
|
FileSizeModified = TRUE;
|
|
|
|
OldFileSize.QuadPart = FcbHeader->FileSize.QuadPart;
|
|
|
|
OldValidDataLength.QuadPart = FcbHeader->ValidDataLength.QuadPart;
|
|
|
|
|
|
|
|
/* If the high part of the filesize is going
|
|
|
|
* to change, grab the Paging IoResouce.
|
|
|
|
*/
|
|
|
|
if (NewSize.HighPart != FcbHeader->FileSize.HighPart &&
|
|
|
|
FcbHeader->PagingIoResource)
|
|
|
|
{
|
|
|
|
ExAcquireResourceExclusiveLite(FcbHeader->PagingIoResource, TRUE);
|
|
|
|
FcbHeader->FileSize.QuadPart = NewSize.QuadPart;
|
|
|
|
ExReleaseResourceLite(FcbHeader->PagingIoResource);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
FcbHeader->FileSize.QuadPart = NewSize.QuadPart;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Nagar p.544.
|
|
|
|
* Set ourselves as top component.
|
|
|
|
*/
|
|
|
|
PsGetCurrentThread()->TopLevelIrp = FSRTL_FAST_IO_TOP_LEVEL_IRP;
|
|
|
|
|
|
|
|
_SEH2_TRY
|
|
|
|
{
|
|
|
|
BOOLEAN CallCc = TRUE;
|
|
|
|
|
|
|
|
/* Check if there is a gap between the end of the file
|
|
|
|
* and the offset. If yes, then we have to zero the data.
|
|
|
|
*/
|
|
|
|
if (Offset.QuadPart > FcbHeader->ValidDataLength.QuadPart)
|
|
|
|
{
|
|
|
|
if (!(Result = CcZeroData(FileObject,
|
|
|
|
&FcbHeader->ValidDataLength,
|
|
|
|
&Offset,
|
|
|
|
Wait)))
|
|
|
|
{
|
2013-08-31 16:36:34 +00:00
|
|
|
/* If this operation fails, then we have to exit. We can't jump
|
|
|
|
* outside the SEH, so I am using a variable to exit normally.
|
2009-11-16 01:56:48 +00:00
|
|
|
*/
|
|
|
|
CallCc = FALSE;
|
|
|
|
}
|
|
|
|
}
|
2009-01-18 10:23:15 +00:00
|
|
|
|
|
|
|
/* Unless the CcZeroData failed, call the cache manager */
|
2009-11-16 01:56:48 +00:00
|
|
|
if (CallCc)
|
|
|
|
{
|
|
|
|
Result = CcCopyWrite(FileObject, &Offset, Length, Wait, Buffer);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
_SEH2_EXCEPT(FsRtlIsNtstatusExpected(_SEH2_GetExceptionCode()) ?
|
|
|
|
EXCEPTION_EXECUTE_HANDLER :
|
|
|
|
EXCEPTION_CONTINUE_SEARCH)
|
|
|
|
{
|
|
|
|
Result = FALSE;
|
|
|
|
}
|
|
|
|
_SEH2_END;
|
2009-01-18 10:23:15 +00:00
|
|
|
|
|
|
|
/* Reset the top component */
|
2009-11-16 01:56:48 +00:00
|
|
|
PsGetCurrentThread()->TopLevelIrp = FSRTL_FAST_IO_TOP_LEVEL_IRP;
|
2009-01-18 10:23:15 +00:00
|
|
|
|
|
|
|
/* Did the operation suceeded */
|
2009-11-16 01:56:48 +00:00
|
|
|
if (Result)
|
|
|
|
{
|
|
|
|
/* Check if we need to update the filesize */
|
|
|
|
if (NewSize.QuadPart > FcbHeader->ValidDataLength.QuadPart)
|
|
|
|
{
|
|
|
|
if (NewSize.HighPart != FcbHeader->ValidDataLength.HighPart &&
|
|
|
|
FcbHeader->PagingIoResource)
|
|
|
|
{
|
|
|
|
ExAcquireResourceExclusiveLite(FcbHeader->PagingIoResource, TRUE);
|
|
|
|
FcbHeader->ValidDataLength.QuadPart = NewSize.QuadPart;
|
|
|
|
ExReleaseResourceLite(FcbHeader->PagingIoResource);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
FcbHeader->ValidDataLength.QuadPart = NewSize.QuadPart;
|
|
|
|
}
|
|
|
|
}
|
2009-01-18 10:23:15 +00:00
|
|
|
|
|
|
|
/* Flag the file as modified */
|
2009-11-16 01:56:48 +00:00
|
|
|
FileObject->Flags |= FO_FILE_MODIFIED;
|
|
|
|
|
|
|
|
/* Check if the filesize has changed */
|
|
|
|
if (FileSizeModified)
|
|
|
|
{
|
|
|
|
/* Update the file sizes */
|
|
|
|
SharedCacheMap =
|
|
|
|
(PSHARED_CACHE_MAP)FileObject->SectionObjectPointer->SharedCacheMap;
|
|
|
|
SharedCacheMap->FileSize.QuadPart = NewSize.QuadPart;
|
|
|
|
FileObject->Flags |= FO_FILE_SIZE_CHANGED;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Update the current FO byte offset */
|
|
|
|
FileObject->CurrentByteOffset.QuadPart = NewSize.QuadPart;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* The operation did not succeed.
|
|
|
|
* Reset the file size to what it should be.
|
|
|
|
*/
|
|
|
|
if (FileSizeModified)
|
|
|
|
{
|
|
|
|
if (FcbHeader->PagingIoResource)
|
|
|
|
{
|
|
|
|
ExAcquireResourceExclusiveLite(FcbHeader->PagingIoResource, TRUE);
|
|
|
|
FcbHeader->FileSize.QuadPart = OldFileSize.QuadPart;
|
|
|
|
FcbHeader->ValidDataLength.QuadPart = OldValidDataLength.QuadPart;
|
|
|
|
ExReleaseResourceLite(FcbHeader->PagingIoResource);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
FcbHeader->FileSize.QuadPart = OldFileSize.QuadPart;
|
|
|
|
FcbHeader->ValidDataLength.QuadPart = OldValidDataLength.QuadPart;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
goto Cleanup;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
goto FailAndCleanup;
|
|
|
|
}
|
|
|
|
}
|
2009-01-18 10:23:15 +00:00
|
|
|
|
|
|
|
LeaveCriticalAndFail:
|
2009-11-16 01:56:48 +00:00
|
|
|
|
2009-01-18 10:23:15 +00:00
|
|
|
FsRtlExitFileSystem();
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
FailAndCleanup:
|
|
|
|
|
|
|
|
ExReleaseResourceLite(FcbHeader->Resource);
|
|
|
|
FsRtlExitFileSystem();
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
Cleanup:
|
|
|
|
|
|
|
|
ExReleaseResourceLite(FcbHeader->Resource);
|
|
|
|
FsRtlExitFileSystem();
|
|
|
|
return Result;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* @implemented
|
|
|
|
*/
|
|
|
|
NTSTATUS
|
|
|
|
NTAPI
|
2009-11-16 01:56:48 +00:00
|
|
|
FsRtlGetFileSize(IN PFILE_OBJECT FileObject,
|
2009-01-18 10:23:15 +00:00
|
|
|
IN OUT PLARGE_INTEGER FileSize)
|
|
|
|
{
|
|
|
|
FILE_STANDARD_INFORMATION Info;
|
|
|
|
NTSTATUS Status;
|
|
|
|
IO_STATUS_BLOCK IoStatus;
|
|
|
|
PDEVICE_OBJECT DeviceObject;
|
|
|
|
PFAST_IO_DISPATCH FastDispatch;
|
|
|
|
KEVENT Event;
|
|
|
|
PIO_STACK_LOCATION IoStackLocation;
|
|
|
|
PIRP Irp;
|
|
|
|
BOOLEAN OldHardError;
|
|
|
|
|
|
|
|
PAGED_CODE();
|
|
|
|
|
|
|
|
/* Get Device Object and Fast Calls */
|
|
|
|
DeviceObject = IoGetRelatedDeviceObject(FileObject);
|
|
|
|
FastDispatch = DeviceObject->DriverObject->FastIoDispatch;
|
|
|
|
|
2009-11-16 01:56:48 +00:00
|
|
|
/* Check if we support Fast Calls, and check FastIoQueryStandardInfo.
|
|
|
|
* Call the function and see if it succeeds.
|
|
|
|
*/
|
|
|
|
if (!FastDispatch ||
|
|
|
|
!FastDispatch->FastIoQueryStandardInfo ||
|
|
|
|
!FastDispatch->FastIoQueryStandardInfo(FileObject,
|
|
|
|
TRUE,
|
|
|
|
&Info,
|
|
|
|
&IoStatus,
|
|
|
|
DeviceObject))
|
2009-01-18 10:23:15 +00:00
|
|
|
{
|
2009-11-16 01:56:48 +00:00
|
|
|
/* If any of the above failed, then we are going to send an
|
|
|
|
* IRP to the device object. Initialize the event for the IO.
|
|
|
|
*/
|
|
|
|
KeInitializeEvent(&Event, NotificationEvent, FALSE);
|
|
|
|
|
2009-01-18 10:23:15 +00:00
|
|
|
/* Allocate the IRP */
|
2009-11-16 01:56:48 +00:00
|
|
|
Irp = IoAllocateIrp(DeviceObject->StackSize, FALSE);
|
|
|
|
|
2009-01-18 10:23:15 +00:00
|
|
|
if (Irp == NULL)
|
|
|
|
{
|
|
|
|
return STATUS_INSUFFICIENT_RESOURCES;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Don't process hard error */
|
|
|
|
OldHardError = IoSetThreadHardErrorMode(FALSE);
|
|
|
|
|
|
|
|
/* Setup the IRP */
|
|
|
|
Irp->UserIosb = &IoStatus;
|
|
|
|
Irp->UserEvent = &Event;
|
|
|
|
Irp->Tail.Overlay.Thread = PsGetCurrentThread();
|
|
|
|
Irp->Flags = IRP_SYNCHRONOUS_PAGING_IO | IRP_PAGING_IO;
|
|
|
|
Irp->RequestorMode = KernelMode;
|
|
|
|
Irp->Tail.Overlay.OriginalFileObject = FileObject;
|
|
|
|
Irp->AssociatedIrp.SystemBuffer = &Info;
|
|
|
|
|
|
|
|
/* Setup out stack location */
|
|
|
|
IoStackLocation = Irp->Tail.Overlay.CurrentStackLocation;
|
|
|
|
IoStackLocation--;
|
|
|
|
IoStackLocation->MajorFunction = IRP_MJ_QUERY_INFORMATION;
|
|
|
|
IoStackLocation->FileObject = FileObject;
|
|
|
|
IoStackLocation->DeviceObject = DeviceObject;
|
2009-11-16 01:56:48 +00:00
|
|
|
IoStackLocation->Parameters.QueryFile.Length =
|
2013-07-16 22:37:01 +00:00
|
|
|
sizeof(FILE_STANDARD_INFORMATION);
|
2009-11-16 01:56:48 +00:00
|
|
|
IoStackLocation->Parameters.QueryFile.FileInformationClass =
|
|
|
|
FileStandardInformation;
|
2009-01-18 10:23:15 +00:00
|
|
|
|
|
|
|
/* Send the IRP to the related device object */
|
2009-11-16 01:56:48 +00:00
|
|
|
Status = IoCallDriver(DeviceObject, Irp);
|
2009-01-18 10:23:15 +00:00
|
|
|
|
|
|
|
/* Standard DDK IRP result processing */
|
|
|
|
if (Status == STATUS_PENDING)
|
|
|
|
{
|
2009-11-16 01:56:48 +00:00
|
|
|
KeWaitForSingleObject(&Event, Executive, KernelMode, FALSE, NULL);
|
2009-01-18 10:23:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* If there was a synchronous error, signal it */
|
|
|
|
if (!NT_SUCCESS(Status))
|
|
|
|
{
|
|
|
|
IoStatus.Status = Status;
|
|
|
|
}
|
|
|
|
|
|
|
|
IoSetThreadHardErrorMode(OldHardError);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Check the sync/async IO result */
|
|
|
|
if (NT_SUCCESS(IoStatus.Status))
|
|
|
|
{
|
2009-11-16 01:56:48 +00:00
|
|
|
/* Was the request for a directory? */
|
2009-01-18 10:23:15 +00:00
|
|
|
if (Info.Directory)
|
|
|
|
{
|
|
|
|
IoStatus.Status = STATUS_FILE_IS_A_DIRECTORY;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
FileSize->QuadPart = Info.EndOfFile.QuadPart;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return IoStatus.Status;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* @implemented
|
|
|
|
*/
|
|
|
|
BOOLEAN
|
|
|
|
NTAPI
|
|
|
|
FsRtlMdlRead(IN PFILE_OBJECT FileObject,
|
|
|
|
IN PLARGE_INTEGER FileOffset,
|
|
|
|
IN ULONG Length,
|
|
|
|
IN ULONG LockKey,
|
|
|
|
OUT PMDL *MdlChain,
|
|
|
|
OUT PIO_STATUS_BLOCK IoStatus)
|
|
|
|
{
|
|
|
|
PDEVICE_OBJECT DeviceObject, BaseDeviceObject;
|
|
|
|
PFAST_IO_DISPATCH FastDispatch;
|
|
|
|
|
|
|
|
/* Get Device Object and Fast Calls */
|
|
|
|
DeviceObject = IoGetRelatedDeviceObject(FileObject);
|
|
|
|
FastDispatch = DeviceObject->DriverObject->FastIoDispatch;
|
|
|
|
|
|
|
|
/* Check if we support Fast Calls, and check this one */
|
|
|
|
if (FastDispatch && FastDispatch->MdlRead)
|
|
|
|
{
|
|
|
|
/* Use the fast path */
|
|
|
|
return FastDispatch->MdlRead(FileObject,
|
|
|
|
FileOffset,
|
|
|
|
Length,
|
|
|
|
LockKey,
|
|
|
|
MdlChain,
|
|
|
|
IoStatus,
|
|
|
|
DeviceObject);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Get the Base File System (Volume) and Fast Calls */
|
|
|
|
BaseDeviceObject = IoGetBaseFileSystemDeviceObject(FileObject);
|
|
|
|
FastDispatch = BaseDeviceObject->DriverObject->FastIoDispatch;
|
|
|
|
|
|
|
|
/* If the Base Device Object has its own FastDispatch Routine, fail */
|
2009-11-16 01:56:48 +00:00
|
|
|
if (FastDispatch && FastDispatch->MdlRead && BaseDeviceObject != DeviceObject)
|
2009-01-18 10:23:15 +00:00
|
|
|
{
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* No fast path, use slow path */
|
|
|
|
return FsRtlMdlReadDev(FileObject,
|
|
|
|
FileOffset,
|
|
|
|
Length,
|
|
|
|
LockKey,
|
|
|
|
MdlChain,
|
|
|
|
IoStatus,
|
|
|
|
DeviceObject);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* @implemented
|
|
|
|
*/
|
|
|
|
BOOLEAN
|
|
|
|
NTAPI
|
|
|
|
FsRtlMdlReadComplete(IN PFILE_OBJECT FileObject,
|
|
|
|
IN OUT PMDL MdlChain)
|
|
|
|
{
|
|
|
|
PDEVICE_OBJECT DeviceObject, BaseDeviceObject;
|
|
|
|
PFAST_IO_DISPATCH FastDispatch;
|
|
|
|
|
|
|
|
/* Get Device Object and Fast Calls */
|
|
|
|
DeviceObject = IoGetRelatedDeviceObject(FileObject);
|
|
|
|
FastDispatch = DeviceObject->DriverObject->FastIoDispatch;
|
|
|
|
|
|
|
|
/* Check if we support Fast Calls, and check this one */
|
|
|
|
if (FastDispatch && FastDispatch->MdlReadComplete)
|
|
|
|
{
|
|
|
|
/* Use the fast path */
|
2009-11-16 01:56:48 +00:00
|
|
|
return FastDispatch->MdlReadComplete(FileObject, MdlChain, DeviceObject);
|
2009-01-18 10:23:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Get the Base File System (Volume) and Fast Calls */
|
|
|
|
BaseDeviceObject = IoGetBaseFileSystemDeviceObject(FileObject);
|
|
|
|
FastDispatch = BaseDeviceObject->DriverObject->FastIoDispatch;
|
|
|
|
|
|
|
|
/* If the Base Device Object has its own FastDispatch Routine, fail */
|
2009-11-16 01:56:48 +00:00
|
|
|
if ((BaseDeviceObject != DeviceObject) &&
|
|
|
|
FastDispatch &&
|
|
|
|
FastDispatch->MdlReadComplete)
|
2009-01-18 10:23:15 +00:00
|
|
|
{
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* No fast path, use slow path */
|
|
|
|
return FsRtlMdlReadCompleteDev(FileObject, MdlChain, DeviceObject);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* @implemented
|
|
|
|
*/
|
2009-11-16 01:56:48 +00:00
|
|
|
BOOLEAN
|
|
|
|
NTAPI
|
2009-01-18 10:23:15 +00:00
|
|
|
FsRtlMdlReadCompleteDev(IN PFILE_OBJECT FileObject,
|
|
|
|
IN PMDL MemoryDescriptorList,
|
|
|
|
IN PDEVICE_OBJECT DeviceObject)
|
|
|
|
{
|
|
|
|
/* Call the Cache Manager */
|
2013-07-16 22:37:01 +00:00
|
|
|
CcMdlReadComplete2(FileObject, MemoryDescriptorList);
|
2009-01-18 10:23:15 +00:00
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* @implemented
|
|
|
|
*/
|
|
|
|
BOOLEAN
|
|
|
|
NTAPI
|
|
|
|
FsRtlMdlReadDev(IN PFILE_OBJECT FileObject,
|
|
|
|
IN PLARGE_INTEGER FileOffset,
|
|
|
|
IN ULONG Length,
|
|
|
|
IN ULONG LockKey,
|
|
|
|
OUT PMDL *MdlChain,
|
|
|
|
OUT PIO_STATUS_BLOCK IoStatus,
|
|
|
|
IN PDEVICE_OBJECT DeviceObject)
|
|
|
|
{
|
|
|
|
PFSRTL_COMMON_FCB_HEADER FcbHeader;
|
|
|
|
BOOLEAN Result = TRUE;
|
|
|
|
LARGE_INTEGER Offset;
|
|
|
|
PFAST_IO_DISPATCH FastIoDispatch;
|
|
|
|
PDEVICE_OBJECT Device;
|
|
|
|
PAGED_CODE();
|
|
|
|
|
|
|
|
/* No actual read */
|
|
|
|
if (!Length)
|
|
|
|
{
|
|
|
|
/* Return success */
|
|
|
|
IoStatus->Status = STATUS_SUCCESS;
|
|
|
|
IoStatus->Information = 0;
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Sanity check */
|
|
|
|
ASSERT(MAXLONGLONG - FileOffset->QuadPart >= (LONGLONG)Length);
|
|
|
|
|
|
|
|
/* Get the offset and FCB header */
|
|
|
|
Offset.QuadPart = FileOffset->QuadPart + Length;
|
|
|
|
FcbHeader = (PFSRTL_COMMON_FCB_HEADER)FileObject->FsContext;
|
|
|
|
|
|
|
|
/* Enter the FS */
|
|
|
|
FsRtlEnterFileSystem();
|
|
|
|
CcFastMdlReadWait++;
|
|
|
|
|
|
|
|
/* Lock the FCB */
|
|
|
|
ExAcquireResourceShared(FcbHeader->Resource, TRUE);
|
|
|
|
|
|
|
|
/* Check if this is a fast I/O cached file */
|
|
|
|
if (!(FileObject->PrivateCacheMap) ||
|
|
|
|
(FcbHeader->IsFastIoPossible == FastIoIsNotPossible))
|
|
|
|
{
|
|
|
|
/* It's not, so fail */
|
|
|
|
CcFastMdlReadNotPossible += 1;
|
|
|
|
Result = FALSE;
|
|
|
|
goto Cleanup;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Check if we need to find out if fast I/O is available */
|
|
|
|
if (FcbHeader->IsFastIoPossible == FastIoIsQuestionable)
|
|
|
|
{
|
|
|
|
/* Get the Fast I/O table */
|
|
|
|
Device = IoGetRelatedDeviceObject(FileObject);
|
|
|
|
FastIoDispatch = Device->DriverObject->FastIoDispatch;
|
|
|
|
|
|
|
|
/* Sanity check */
|
|
|
|
ASSERT(!KeIsExecutingDpc());
|
|
|
|
ASSERT(FastIoDispatch != NULL);
|
|
|
|
ASSERT(FastIoDispatch->FastIoCheckIfPossible != NULL);
|
|
|
|
|
|
|
|
/* Ask the driver if we can do it */
|
|
|
|
if (!FastIoDispatch->FastIoCheckIfPossible(FileObject,
|
|
|
|
FileOffset,
|
|
|
|
Length,
|
|
|
|
TRUE,
|
|
|
|
LockKey,
|
|
|
|
TRUE,
|
|
|
|
IoStatus,
|
|
|
|
Device))
|
|
|
|
{
|
|
|
|
/* It's not, fail */
|
|
|
|
CcFastMdlReadNotPossible += 1;
|
|
|
|
Result = FALSE;
|
|
|
|
goto Cleanup;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Check if we read too much */
|
|
|
|
if (Offset.QuadPart > FcbHeader->FileSize.QuadPart)
|
|
|
|
{
|
|
|
|
/* We did, check if the file offset is past the end */
|
|
|
|
if (FileOffset->QuadPart >= FcbHeader->FileSize.QuadPart)
|
|
|
|
{
|
|
|
|
/* Set end of file */
|
|
|
|
IoStatus->Status = STATUS_END_OF_FILE;
|
|
|
|
IoStatus->Information = 0;
|
|
|
|
goto Cleanup;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Otherwise, just normalize the length */
|
|
|
|
Length = (ULONG)(FcbHeader->FileSize.QuadPart - FileOffset->QuadPart);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Set this as top-level IRP */
|
|
|
|
PsGetCurrentThread()->TopLevelIrp = FSRTL_FAST_IO_TOP_LEVEL_IRP;
|
|
|
|
|
|
|
|
_SEH2_TRY
|
|
|
|
{
|
|
|
|
/* Attempt a read */
|
|
|
|
CcMdlRead(FileObject, FileOffset, Length, MdlChain, IoStatus);
|
|
|
|
FileObject->Flags |= FO_FILE_FAST_IO_READ;
|
|
|
|
}
|
2009-11-16 01:56:48 +00:00
|
|
|
_SEH2_EXCEPT(FsRtlIsNtstatusExpected(_SEH2_GetExceptionCode()) ?
|
|
|
|
EXCEPTION_EXECUTE_HANDLER :
|
|
|
|
EXCEPTION_CONTINUE_SEARCH)
|
|
|
|
{
|
|
|
|
Result = FALSE;
|
|
|
|
}
|
|
|
|
_SEH2_END;
|
2009-01-18 10:23:15 +00:00
|
|
|
|
|
|
|
|
|
|
|
/* Remove the top-level IRP flag */
|
|
|
|
PsGetCurrentThread()->TopLevelIrp = 0;
|
|
|
|
|
|
|
|
/* Return to caller */
|
|
|
|
Cleanup:
|
2009-11-16 01:56:48 +00:00
|
|
|
|
2009-01-18 10:23:15 +00:00
|
|
|
ExReleaseResourceLite(FcbHeader->Resource);
|
|
|
|
FsRtlExitFileSystem();
|
2009-11-16 01:56:48 +00:00
|
|
|
|
2009-01-18 10:23:15 +00:00
|
|
|
return Result;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* @implemented
|
|
|
|
*/
|
|
|
|
BOOLEAN
|
|
|
|
NTAPI
|
|
|
|
FsRtlMdlWriteComplete(IN PFILE_OBJECT FileObject,
|
|
|
|
IN PLARGE_INTEGER FileOffset,
|
|
|
|
IN PMDL MdlChain)
|
|
|
|
{
|
|
|
|
PDEVICE_OBJECT DeviceObject, BaseDeviceObject;
|
|
|
|
PFAST_IO_DISPATCH FastDispatch;
|
|
|
|
|
|
|
|
/* Get Device Object and Fast Calls */
|
|
|
|
DeviceObject = IoGetRelatedDeviceObject(FileObject);
|
|
|
|
FastDispatch = DeviceObject->DriverObject->FastIoDispatch;
|
|
|
|
|
|
|
|
/* Check if we support Fast Calls, and check this one */
|
|
|
|
if (FastDispatch && FastDispatch->MdlWriteComplete)
|
|
|
|
{
|
|
|
|
/* Use the fast path */
|
|
|
|
return FastDispatch->MdlWriteComplete(FileObject,
|
|
|
|
FileOffset,
|
|
|
|
MdlChain,
|
|
|
|
DeviceObject);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Get the Base File System (Volume) and Fast Calls */
|
|
|
|
BaseDeviceObject = IoGetBaseFileSystemDeviceObject(FileObject);
|
|
|
|
FastDispatch = BaseDeviceObject->DriverObject->FastIoDispatch;
|
|
|
|
|
|
|
|
/* If the Base Device Object has its own FastDispatch Routine, fail */
|
2009-11-16 01:56:48 +00:00
|
|
|
if (FastDispatch &&
|
|
|
|
FastDispatch->MdlWriteComplete &&
|
2009-01-18 10:23:15 +00:00
|
|
|
BaseDeviceObject != DeviceObject)
|
|
|
|
{
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* No fast path, use slow path */
|
|
|
|
return FsRtlMdlWriteCompleteDev(FileObject,
|
|
|
|
FileOffset,
|
|
|
|
MdlChain,
|
|
|
|
DeviceObject);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* @implemented
|
|
|
|
*/
|
|
|
|
BOOLEAN
|
|
|
|
NTAPI
|
|
|
|
FsRtlMdlWriteCompleteDev(IN PFILE_OBJECT FileObject,
|
|
|
|
IN PLARGE_INTEGER FileOffset,
|
|
|
|
IN PMDL MdlChain,
|
|
|
|
IN PDEVICE_OBJECT DeviceObject)
|
|
|
|
{
|
|
|
|
if (FileObject->Flags & FO_WRITE_THROUGH)
|
|
|
|
{
|
2009-11-16 01:56:48 +00:00
|
|
|
return FALSE;
|
2009-01-18 10:23:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Call the Cache Manager */
|
2009-11-16 01:56:48 +00:00
|
|
|
CcMdlWriteComplete2(FileObject, FileOffset, MdlChain);
|
2009-01-18 10:23:15 +00:00
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* @implemented
|
|
|
|
*/
|
|
|
|
BOOLEAN
|
|
|
|
NTAPI
|
|
|
|
FsRtlPrepareMdlWrite(IN PFILE_OBJECT FileObject,
|
|
|
|
IN PLARGE_INTEGER FileOffset,
|
|
|
|
IN ULONG Length,
|
|
|
|
IN ULONG LockKey,
|
|
|
|
OUT PMDL *MdlChain,
|
|
|
|
OUT PIO_STATUS_BLOCK IoStatus)
|
|
|
|
{
|
|
|
|
PDEVICE_OBJECT DeviceObject, BaseDeviceObject;
|
|
|
|
PFAST_IO_DISPATCH FastDispatch;
|
|
|
|
|
|
|
|
/* Get Device Object and Fast Calls */
|
|
|
|
DeviceObject = IoGetRelatedDeviceObject(FileObject);
|
|
|
|
FastDispatch = DeviceObject->DriverObject->FastIoDispatch;
|
|
|
|
|
|
|
|
/* Check if we support Fast Calls, and check this one */
|
|
|
|
if (FastDispatch && FastDispatch->PrepareMdlWrite)
|
|
|
|
{
|
|
|
|
/* Use the fast path */
|
|
|
|
return FastDispatch->PrepareMdlWrite(FileObject,
|
|
|
|
FileOffset,
|
|
|
|
Length,
|
|
|
|
LockKey,
|
|
|
|
MdlChain,
|
|
|
|
IoStatus,
|
|
|
|
DeviceObject);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Get the Base File System (Volume) and Fast Calls */
|
|
|
|
BaseDeviceObject = IoGetBaseFileSystemDeviceObject(FileObject);
|
|
|
|
FastDispatch = BaseDeviceObject->DriverObject->FastIoDispatch;
|
|
|
|
|
|
|
|
/* If the Base Device Object has its own FastDispatch Routine, fail */
|
2009-11-16 01:56:48 +00:00
|
|
|
if (FastDispatch &&
|
|
|
|
FastDispatch->PrepareMdlWrite &&
|
2009-01-18 10:23:15 +00:00
|
|
|
BaseDeviceObject != DeviceObject)
|
|
|
|
{
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* No fast path, use slow path */
|
|
|
|
return FsRtlPrepareMdlWriteDev(FileObject,
|
|
|
|
FileOffset,
|
|
|
|
Length,
|
|
|
|
LockKey,
|
|
|
|
MdlChain,
|
|
|
|
IoStatus,
|
|
|
|
DeviceObject);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* @implemented
|
|
|
|
*/
|
|
|
|
BOOLEAN
|
|
|
|
NTAPI
|
|
|
|
FsRtlPrepareMdlWriteDev(IN PFILE_OBJECT FileObject,
|
|
|
|
IN PLARGE_INTEGER FileOffset,
|
|
|
|
IN ULONG Length,
|
|
|
|
IN ULONG LockKey,
|
|
|
|
OUT PMDL *MdlChain,
|
|
|
|
OUT PIO_STATUS_BLOCK IoStatus,
|
|
|
|
IN PDEVICE_OBJECT DeviceObject)
|
|
|
|
{
|
|
|
|
BOOLEAN Result = TRUE;
|
|
|
|
PFAST_IO_DISPATCH FastIoDispatch;
|
|
|
|
PDEVICE_OBJECT Device;
|
|
|
|
PFSRTL_COMMON_FCB_HEADER FcbHeader;
|
2009-11-16 01:56:48 +00:00
|
|
|
PSHARED_CACHE_MAP SharedCacheMap;
|
2009-01-18 10:23:15 +00:00
|
|
|
|
|
|
|
LARGE_INTEGER OldFileSize;
|
|
|
|
LARGE_INTEGER OldValidDataLength;
|
|
|
|
LARGE_INTEGER NewSize;
|
|
|
|
LARGE_INTEGER Offset;
|
|
|
|
|
2009-11-16 01:56:48 +00:00
|
|
|
/* WDK doc.
|
|
|
|
* Offset == 0xffffffffffffffff indicates append to the end of file.
|
|
|
|
*/
|
|
|
|
BOOLEAN FileOffsetAppend = (FileOffset->HighPart == (LONG)0xffffffff) &&
|
|
|
|
(FileOffset->LowPart == 0xffffffff);
|
|
|
|
|
2009-01-18 10:23:15 +00:00
|
|
|
BOOLEAN FileSizeModified = FALSE;
|
2013-08-31 16:36:34 +00:00
|
|
|
BOOLEAN ResourceAcquiredShared = FALSE;
|
2009-01-18 10:23:15 +00:00
|
|
|
|
|
|
|
/* Initialize some of the vars and pointers */
|
|
|
|
OldFileSize.QuadPart = 0;
|
|
|
|
OldValidDataLength.QuadPart = 0;
|
|
|
|
|
|
|
|
PAGED_CODE();
|
|
|
|
|
|
|
|
Offset.QuadPart = FileOffset->QuadPart + Length;
|
|
|
|
|
2009-11-16 01:56:48 +00:00
|
|
|
/* Nagar p.544.
|
|
|
|
* Check with Cc if we can write.
|
|
|
|
*/
|
|
|
|
if (!CcCanIWrite(FileObject, Length, TRUE, FALSE) ||
|
|
|
|
(FileObject->Flags & FO_WRITE_THROUGH))
|
2009-01-18 10:23:15 +00:00
|
|
|
{
|
2009-11-16 01:56:48 +00:00
|
|
|
return FALSE;
|
2009-01-18 10:23:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
IoStatus->Status = STATUS_SUCCESS;
|
|
|
|
|
|
|
|
/* No actual read */
|
|
|
|
if (!Length)
|
|
|
|
{
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
FcbHeader = (PFSRTL_COMMON_FCB_HEADER)FileObject->FsContext;
|
|
|
|
FsRtlEnterFileSystem();
|
|
|
|
|
|
|
|
/* Check we are going to extend the file */
|
2009-11-16 01:56:48 +00:00
|
|
|
if ((FileOffsetAppend == FALSE) &&
|
|
|
|
(FileOffset->QuadPart + Length <= FcbHeader->ValidDataLength.QuadPart))
|
|
|
|
{
|
|
|
|
/* Acquire the resource shared */
|
|
|
|
ExAcquireResourceSharedLite(FcbHeader->Resource, TRUE);
|
2013-08-31 16:36:34 +00:00
|
|
|
ResourceAcquiredShared = TRUE;
|
2009-11-16 01:56:48 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* Acquire the resource exclusive */
|
|
|
|
ExAcquireResourceExclusiveLite(FcbHeader->Resource, TRUE);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Check if we are appending */
|
2014-11-15 22:47:51 +00:00
|
|
|
if (FileOffsetAppend != FALSE)
|
2009-11-16 01:56:48 +00:00
|
|
|
{
|
|
|
|
Offset.QuadPart = FcbHeader->FileSize.QuadPart;
|
|
|
|
NewSize.QuadPart = FcbHeader->FileSize.QuadPart + Length;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
Offset.QuadPart = FileOffset->QuadPart;
|
|
|
|
NewSize.QuadPart = FileOffset->QuadPart + Length;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((FileObject->PrivateCacheMap) &&
|
|
|
|
(FcbHeader->IsFastIoPossible) &&
|
2009-11-16 01:57:24 +00:00
|
|
|
(Length <= MAXLONGLONG - FileOffset->QuadPart) &&
|
2009-11-16 01:56:48 +00:00
|
|
|
(NewSize.QuadPart <= FcbHeader->AllocationSize.QuadPart))
|
2009-01-18 10:23:15 +00:00
|
|
|
{
|
|
|
|
/* Check if we can keep the lock shared */
|
2013-08-31 16:36:34 +00:00
|
|
|
if (ResourceAcquiredShared &&
|
2009-11-16 01:56:48 +00:00
|
|
|
(NewSize.QuadPart > FcbHeader->ValidDataLength.QuadPart))
|
|
|
|
{
|
|
|
|
ExReleaseResourceLite(FcbHeader->Resource);
|
|
|
|
ExAcquireResourceExclusiveLite(FcbHeader->Resource, TRUE);
|
2009-01-18 10:23:15 +00:00
|
|
|
|
|
|
|
/* Compute the offset and the new filesize */
|
2009-11-16 01:56:48 +00:00
|
|
|
if (FileOffsetAppend)
|
|
|
|
{
|
|
|
|
Offset.QuadPart = FcbHeader->FileSize.QuadPart;
|
|
|
|
NewSize.QuadPart = FcbHeader->FileSize.QuadPart + Length;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Recheck the above points since we released and reacquire the lock */
|
|
|
|
if ((FileObject->PrivateCacheMap != NULL) &&
|
|
|
|
(FcbHeader->IsFastIoPossible != FastIoIsNotPossible) &&
|
|
|
|
(FcbHeader->AllocationSize.QuadPart > NewSize.QuadPart))
|
|
|
|
{
|
|
|
|
/* Do nothing */
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
goto FailAndCleanup;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Check if we need to find out if fast I/O is available */
|
|
|
|
if (FcbHeader->IsFastIoPossible == FastIoIsQuestionable)
|
|
|
|
{
|
|
|
|
/* Sanity check */
|
|
|
|
/* ASSERT(!KeIsExecutingDpc()); */
|
|
|
|
|
|
|
|
/* Get the Fast I/O table */
|
|
|
|
Device = IoGetRelatedDeviceObject(FileObject);
|
|
|
|
FastIoDispatch = Device->DriverObject->FastIoDispatch;
|
|
|
|
|
|
|
|
/* Sanity check */
|
|
|
|
ASSERT(FastIoDispatch != NULL);
|
|
|
|
ASSERT(FastIoDispatch->FastIoCheckIfPossible != NULL);
|
|
|
|
|
|
|
|
/* Ask the driver if we can do it */
|
|
|
|
if (!FastIoDispatch->FastIoCheckIfPossible(FileObject,
|
|
|
|
FileOffset,
|
|
|
|
Length,
|
|
|
|
TRUE,
|
|
|
|
LockKey,
|
|
|
|
FALSE,
|
|
|
|
IoStatus,
|
|
|
|
Device))
|
|
|
|
{
|
|
|
|
/* It's not, fail */
|
|
|
|
goto FailAndCleanup;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* If we are going to modify the filesize,
|
|
|
|
* save the old fs in case the operation fails.
|
|
|
|
*/
|
|
|
|
if (NewSize.QuadPart > FcbHeader->FileSize.QuadPart)
|
|
|
|
{
|
|
|
|
FileSizeModified = TRUE;
|
|
|
|
OldFileSize.QuadPart = FcbHeader->FileSize.QuadPart;
|
|
|
|
OldValidDataLength.QuadPart = FcbHeader->ValidDataLength.QuadPart;
|
|
|
|
|
|
|
|
/* If the high part of the filesize is going
|
|
|
|
* to change, grab the Paging IoResouce.
|
|
|
|
*/
|
|
|
|
if (NewSize.HighPart != FcbHeader->FileSize.HighPart &&
|
|
|
|
FcbHeader->PagingIoResource)
|
|
|
|
{
|
|
|
|
ExAcquireResourceExclusiveLite(FcbHeader->PagingIoResource, TRUE);
|
|
|
|
FcbHeader->FileSize.QuadPart = NewSize.QuadPart;
|
|
|
|
ExReleaseResourceLite(FcbHeader->PagingIoResource);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
FcbHeader->FileSize.QuadPart = NewSize.QuadPart;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* Nagar p.544.
|
|
|
|
* Set ourselves as top component.
|
|
|
|
*/
|
|
|
|
PsGetCurrentThread()->TopLevelIrp = FSRTL_FAST_IO_TOP_LEVEL_IRP;
|
|
|
|
_SEH2_TRY
|
|
|
|
{
|
|
|
|
/* Check if there is a gap between the end of the file and the offset.
|
|
|
|
* If yes, then we have to zero the data.
|
|
|
|
*/
|
|
|
|
if (Offset.QuadPart > FcbHeader->ValidDataLength.QuadPart)
|
|
|
|
{
|
|
|
|
Result = CcZeroData(FileObject,
|
|
|
|
&FcbHeader->ValidDataLength,
|
|
|
|
&Offset,
|
|
|
|
TRUE);
|
|
|
|
if (Result)
|
|
|
|
{
|
|
|
|
CcPrepareMdlWrite(FileObject,
|
|
|
|
&Offset,
|
|
|
|
Length,
|
|
|
|
MdlChain,
|
|
|
|
IoStatus);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
CcPrepareMdlWrite(FileObject, &Offset, Length, MdlChain, IoStatus);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
_SEH2_EXCEPT(FsRtlIsNtstatusExpected(_SEH2_GetExceptionCode()) ?
|
|
|
|
EXCEPTION_EXECUTE_HANDLER :
|
|
|
|
EXCEPTION_CONTINUE_SEARCH)
|
|
|
|
{
|
|
|
|
Result = FALSE;
|
|
|
|
}
|
|
|
|
_SEH2_END;
|
2009-01-18 10:23:15 +00:00
|
|
|
|
|
|
|
/* Reset the top component */
|
2009-11-16 01:56:48 +00:00
|
|
|
PsGetCurrentThread()->TopLevelIrp = 0;
|
2009-01-18 10:23:15 +00:00
|
|
|
|
|
|
|
/* Did the operation suceeded */
|
2009-11-16 01:56:48 +00:00
|
|
|
if (Result)
|
|
|
|
{
|
|
|
|
/* Check if we need to update the filesize */
|
|
|
|
if (NewSize.QuadPart > FcbHeader->ValidDataLength.QuadPart)
|
|
|
|
{
|
|
|
|
if (NewSize.HighPart != FcbHeader->ValidDataLength.HighPart &&
|
|
|
|
FcbHeader->PagingIoResource)
|
|
|
|
{
|
|
|
|
ExAcquireResourceExclusiveLite(FcbHeader->PagingIoResource, TRUE);
|
|
|
|
FcbHeader->ValidDataLength.QuadPart = NewSize.QuadPart;
|
|
|
|
ExReleaseResourceLite(FcbHeader->PagingIoResource);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
FcbHeader->ValidDataLength.QuadPart = NewSize.QuadPart;
|
|
|
|
}
|
|
|
|
}
|
2009-01-18 10:23:15 +00:00
|
|
|
|
|
|
|
/* Flag the file as modified */
|
2009-11-16 01:56:48 +00:00
|
|
|
FileObject->Flags |= FO_FILE_MODIFIED;
|
|
|
|
|
|
|
|
/* Check if the filesize has changed */
|
|
|
|
if (FileSizeModified)
|
|
|
|
{
|
|
|
|
SharedCacheMap =
|
|
|
|
(PSHARED_CACHE_MAP)FileObject->SectionObjectPointer->SharedCacheMap;
|
|
|
|
SharedCacheMap->FileSize.QuadPart = NewSize.QuadPart;
|
|
|
|
FileObject->Flags |= FO_FILE_SIZE_CHANGED;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* The operation did not succeed.
|
|
|
|
* Reset the file size to what it should be.
|
|
|
|
*/
|
|
|
|
if (FileSizeModified)
|
|
|
|
{
|
|
|
|
if (FcbHeader->PagingIoResource)
|
|
|
|
{
|
|
|
|
ExAcquireResourceExclusiveLite(FcbHeader->PagingIoResource, TRUE);
|
|
|
|
FcbHeader->FileSize.QuadPart = OldFileSize.QuadPart;
|
|
|
|
FcbHeader->ValidDataLength.QuadPart = OldValidDataLength.QuadPart;
|
|
|
|
ExReleaseResourceLite(FcbHeader->PagingIoResource);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
FcbHeader->FileSize.QuadPart = OldFileSize.QuadPart;
|
|
|
|
FcbHeader->ValidDataLength.QuadPart = OldValidDataLength.QuadPart;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2009-01-18 10:23:15 +00:00
|
|
|
|
2009-11-16 01:56:48 +00:00
|
|
|
goto Cleanup;
|
2009-01-18 10:23:15 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
goto FailAndCleanup;
|
|
|
|
}
|
|
|
|
|
|
|
|
FailAndCleanup:
|
|
|
|
|
|
|
|
ExReleaseResourceLite(FcbHeader->Resource);
|
|
|
|
FsRtlExitFileSystem();
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
Cleanup:
|
|
|
|
|
|
|
|
ExReleaseResourceLite(FcbHeader->Resource);
|
|
|
|
FsRtlExitFileSystem();
|
|
|
|
return Result;
|
|
|
|
}
|
|
|
|
|
2013-08-29 22:07:24 +00:00
|
|
|
NTSTATUS
|
|
|
|
NTAPI
|
|
|
|
FsRtlAcquireFileExclusiveCommon(IN PFILE_OBJECT FileObject,
|
|
|
|
IN FS_FILTER_SECTION_SYNC_TYPE SyncType,
|
|
|
|
IN ULONG Reserved)
|
|
|
|
{
|
|
|
|
PFSRTL_COMMON_FCB_HEADER FcbHeader;
|
|
|
|
PDEVICE_OBJECT DeviceObject;
|
|
|
|
PFAST_IO_DISPATCH FastDispatch;
|
2020-11-16 08:24:58 +00:00
|
|
|
PEXTENDED_DRIVER_EXTENSION DriverExtension;
|
|
|
|
PFS_FILTER_CALLBACKS FilterCallbacks;
|
2013-08-29 22:07:24 +00:00
|
|
|
|
|
|
|
/* Get Device Object and Fast Calls */
|
|
|
|
FcbHeader = (PFSRTL_COMMON_FCB_HEADER)FileObject->FsContext;
|
|
|
|
DeviceObject = IoGetRelatedDeviceObject(FileObject);
|
|
|
|
|
|
|
|
/* Get master FsRtl lock */
|
|
|
|
FsRtlEnterFileSystem();
|
|
|
|
|
2020-11-16 08:24:58 +00:00
|
|
|
DriverExtension = (PEXTENDED_DRIVER_EXTENSION)DeviceObject->DriverObject->DriverExtension;
|
|
|
|
FilterCallbacks = DriverExtension->FsFilterCallbacks;
|
|
|
|
|
|
|
|
/* Check if Filter Cllbacks are supported */
|
|
|
|
if (FilterCallbacks && FilterCallbacks->PreAcquireForSectionSynchronization)
|
|
|
|
{
|
|
|
|
NTSTATUS Status;
|
|
|
|
PVOID CompletionContext;
|
|
|
|
|
|
|
|
FS_FILTER_CALLBACK_DATA CbData;
|
|
|
|
|
|
|
|
RtlZeroMemory(&CbData, sizeof(CbData));
|
|
|
|
|
|
|
|
CbData.SizeOfFsFilterCallbackData = sizeof(CbData);
|
|
|
|
CbData.Operation = FS_FILTER_ACQUIRE_FOR_SECTION_SYNCHRONIZATION;
|
|
|
|
CbData.DeviceObject = DeviceObject;
|
|
|
|
CbData.FileObject = FileObject;
|
|
|
|
CbData.Parameters.AcquireForSectionSynchronization.PageProtection = Reserved;
|
|
|
|
CbData.Parameters.AcquireForSectionSynchronization.SyncType = SyncType;
|
|
|
|
|
|
|
|
Status = FilterCallbacks->PreAcquireForSectionSynchronization(&CbData, &CompletionContext);
|
|
|
|
if (!NT_SUCCESS(Status))
|
|
|
|
{
|
|
|
|
FsRtlExitFileSystem();
|
|
|
|
return Status;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Should we do something in-between ? */
|
|
|
|
|
|
|
|
if (FilterCallbacks->PostAcquireForSectionSynchronization)
|
|
|
|
{
|
|
|
|
FilterCallbacks->PostAcquireForSectionSynchronization(&CbData, Status, CompletionContext);
|
|
|
|
}
|
|
|
|
|
[NTOS:FSRTL] FsRtlAcquireFileExclusiveCommon: don't return before acquiring a file resource, except special cases (#7273)
Don't return before file object's resource is acquired in FsRtlAcquireFileExclusiveCommon, except some special return cases, when return is required. Based on hpoussin_filter_extra.patch by Herve Poussineau (@hpoussin) with improved comment, which matches the actual behaviour now.
This is required by fltmgr.sys driver from Windows XP/Server 2003 to work correctly, so this change fixes asserts/exceptions when releasing the file via FsRtlReleaseFile after acquiring, when using 3rd party filter drivers from several antivirus programs (e. g., Avast Free Antivirus all versions, AVG Antivirus Free 18.8, Avira AntiVir Personal 8.2, Dr. Web Security Space 8.0, Kaspersky Antivirus 2012 etc. etc.).
CORE-14157, CORE-14635, CORE-19318
2024-11-20 15:47:32 +00:00
|
|
|
/* Return here when the status is based on the synchonization type and write access to the file */
|
|
|
|
if (Status == STATUS_FSFILTER_OP_COMPLETED_SUCCESSFULLY ||
|
|
|
|
Status == STATUS_FILE_LOCKED_WITH_ONLY_READERS ||
|
|
|
|
Status == STATUS_FILE_LOCKED_WITH_WRITERS)
|
|
|
|
{
|
|
|
|
return Status;
|
|
|
|
}
|
2020-11-16 08:24:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
FastDispatch = DeviceObject->DriverObject->FastIoDispatch;
|
|
|
|
|
2013-08-29 22:07:24 +00:00
|
|
|
/* Check if Fast Calls are supported, and check AcquireFileForNtCreateSection */
|
|
|
|
if (FastDispatch &&
|
|
|
|
FastDispatch->AcquireFileForNtCreateSection)
|
|
|
|
{
|
|
|
|
/* Call the AcquireFileForNtCreateSection FastIo handler */
|
|
|
|
FastDispatch->AcquireFileForNtCreateSection(FileObject);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* No FastIo handler, acquire file's resource exclusively */
|
|
|
|
if (FcbHeader && FcbHeader->Resource) ExAcquireResourceExclusiveLite(FcbHeader->Resource, TRUE);
|
|
|
|
}
|
|
|
|
|
|
|
|
return STATUS_SUCCESS;
|
|
|
|
}
|
|
|
|
|
2009-01-18 10:23:15 +00:00
|
|
|
/*
|
|
|
|
* @implemented
|
|
|
|
*/
|
|
|
|
VOID
|
|
|
|
NTAPI
|
|
|
|
FsRtlAcquireFileExclusive(IN PFILE_OBJECT FileObject)
|
|
|
|
{
|
2013-08-29 22:07:24 +00:00
|
|
|
PAGED_CODE();
|
|
|
|
|
|
|
|
/* Call the common routine. Don't care about the result */
|
|
|
|
(VOID)FsRtlAcquireFileExclusiveCommon(FileObject, SyncTypeOther, 0);
|
2009-01-18 10:23:15 +00:00
|
|
|
}
|
|
|
|
|
2020-11-16 08:24:58 +00:00
|
|
|
/*
|
|
|
|
* @implemented
|
|
|
|
*/
|
|
|
|
NTSTATUS
|
|
|
|
NTAPI
|
|
|
|
FsRtlAcquireToCreateMappedSection(_In_ PFILE_OBJECT FileObject,
|
|
|
|
_In_ ULONG SectionPageProtection)
|
|
|
|
{
|
|
|
|
PAGED_CODE();
|
|
|
|
|
|
|
|
return FsRtlAcquireFileExclusiveCommon(FileObject, SyncTypeCreateSection, SectionPageProtection);
|
|
|
|
}
|
2009-01-18 10:23:15 +00:00
|
|
|
/*
|
|
|
|
* @implemented
|
|
|
|
*/
|
|
|
|
VOID
|
|
|
|
NTAPI
|
|
|
|
FsRtlReleaseFile(IN PFILE_OBJECT FileObject)
|
|
|
|
{
|
2013-08-29 22:07:24 +00:00
|
|
|
PFSRTL_COMMON_FCB_HEADER FcbHeader;
|
|
|
|
PDEVICE_OBJECT DeviceObject;
|
|
|
|
PFAST_IO_DISPATCH FastDispatch;
|
|
|
|
|
|
|
|
/* Get Device Object and Fast Calls */
|
|
|
|
FcbHeader = (PFSRTL_COMMON_FCB_HEADER)FileObject->FsContext;
|
|
|
|
DeviceObject = IoGetRelatedDeviceObject(FileObject);
|
|
|
|
FastDispatch = DeviceObject->DriverObject->FastIoDispatch;
|
|
|
|
|
|
|
|
/* Check if Fast Calls are supported and check ReleaseFileForNtCreateSection */
|
|
|
|
if (FastDispatch &&
|
|
|
|
FastDispatch->ReleaseFileForNtCreateSection)
|
|
|
|
{
|
|
|
|
/* Call the ReleaseFileForNtCreateSection FastIo handler */
|
|
|
|
FastDispatch->ReleaseFileForNtCreateSection(FileObject);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* No FastIo handler, release file's resource */
|
|
|
|
if (FcbHeader && FcbHeader->Resource) ExReleaseResourceLite(FcbHeader->Resource);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Release master FsRtl lock */
|
|
|
|
FsRtlExitFileSystem();
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* @implemented
|
|
|
|
*/
|
|
|
|
NTSTATUS
|
|
|
|
NTAPI
|
|
|
|
FsRtlAcquireFileForCcFlushEx(IN PFILE_OBJECT FileObject)
|
|
|
|
{
|
|
|
|
PFSRTL_COMMON_FCB_HEADER FcbHeader;
|
|
|
|
PDEVICE_OBJECT DeviceObject, BaseDeviceObject;
|
|
|
|
PFAST_IO_DISPATCH FastDispatch;
|
2013-08-31 16:36:34 +00:00
|
|
|
NTSTATUS Status;
|
2013-08-29 22:07:24 +00:00
|
|
|
|
|
|
|
/* Get the Base File System (Volume) and Fast Calls */
|
|
|
|
FcbHeader = (PFSRTL_COMMON_FCB_HEADER)FileObject->FsContext;
|
|
|
|
DeviceObject = IoGetRelatedDeviceObject(FileObject);
|
|
|
|
BaseDeviceObject = IoGetBaseFileSystemDeviceObject(FileObject);
|
|
|
|
FastDispatch = DeviceObject->DriverObject->FastIoDispatch;
|
|
|
|
|
|
|
|
/* Get master FsRtl lock */
|
|
|
|
FsRtlEnterFileSystem();
|
|
|
|
|
|
|
|
/* Check if Fast Calls are supported, and check AcquireForCcFlush */
|
|
|
|
if (FastDispatch &&
|
|
|
|
FastDispatch->AcquireForCcFlush)
|
|
|
|
{
|
|
|
|
/* Call the AcquireForCcFlush FastIo handler */
|
2013-08-31 16:36:34 +00:00
|
|
|
Status = FastDispatch->AcquireForCcFlush(FileObject, BaseDeviceObject);
|
|
|
|
|
|
|
|
/* Return either success or inability to wait.
|
|
|
|
In case of other failure - fall through */
|
2021-01-27 08:22:32 +00:00
|
|
|
if (NT_SUCCESS(Status))
|
|
|
|
return Status;
|
|
|
|
|
|
|
|
if (Status == STATUS_CANT_WAIT)
|
2013-08-29 22:07:24 +00:00
|
|
|
{
|
2021-01-27 08:22:32 +00:00
|
|
|
DPRINT1("STATUS_CANT_WAIT\n");
|
|
|
|
FsRtlExitFileSystem();
|
2013-08-31 16:36:34 +00:00
|
|
|
return Status;
|
2013-08-29 22:07:24 +00:00
|
|
|
}
|
2013-08-31 16:36:34 +00:00
|
|
|
}
|
2013-08-29 22:07:24 +00:00
|
|
|
|
2013-08-31 16:36:34 +00:00
|
|
|
/* No FastIo handler (or it failed). Acquire Main resource */
|
|
|
|
if (FcbHeader->Resource)
|
|
|
|
{
|
|
|
|
/* Acquire it - either shared if it's already acquired
|
|
|
|
or exclusively if we are the first */
|
|
|
|
if (ExIsResourceAcquiredSharedLite(FcbHeader->Resource))
|
|
|
|
ExAcquireResourceSharedLite(FcbHeader->Resource, TRUE);
|
|
|
|
else
|
|
|
|
ExAcquireResourceExclusiveLite(FcbHeader->Resource, TRUE);
|
2013-08-29 22:07:24 +00:00
|
|
|
}
|
|
|
|
|
2013-08-31 16:36:34 +00:00
|
|
|
/* Also acquire its PagingIO resource */
|
|
|
|
if (FcbHeader->PagingIoResource)
|
|
|
|
ExAcquireResourceSharedLite(FcbHeader->PagingIoResource, TRUE);
|
|
|
|
|
2013-08-29 22:07:24 +00:00
|
|
|
return STATUS_SUCCESS;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* @implemented
|
|
|
|
*/
|
|
|
|
VOID
|
|
|
|
NTAPI
|
|
|
|
FsRtlAcquireFileForCcFlush(IN PFILE_OBJECT FileObject)
|
|
|
|
{
|
|
|
|
PAGED_CODE();
|
|
|
|
|
|
|
|
/* Call the common routine. Don't care about the result */
|
|
|
|
(VOID)FsRtlAcquireFileForCcFlushEx(FileObject);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* @implemented
|
|
|
|
*/
|
|
|
|
VOID
|
|
|
|
NTAPI
|
|
|
|
FsRtlReleaseFileForCcFlush(IN PFILE_OBJECT FileObject)
|
|
|
|
{
|
|
|
|
PFSRTL_COMMON_FCB_HEADER FcbHeader;
|
|
|
|
PDEVICE_OBJECT DeviceObject, BaseDeviceObject;
|
|
|
|
PFAST_IO_DISPATCH FastDispatch;
|
2013-08-31 16:36:34 +00:00
|
|
|
NTSTATUS Status = STATUS_INVALID_DEVICE_REQUEST;
|
2013-08-29 22:07:24 +00:00
|
|
|
|
|
|
|
/* Get Device Object and Fast Calls */
|
|
|
|
FcbHeader = (PFSRTL_COMMON_FCB_HEADER)FileObject->FsContext;
|
|
|
|
DeviceObject = IoGetRelatedDeviceObject(FileObject);
|
|
|
|
BaseDeviceObject = IoGetBaseFileSystemDeviceObject(FileObject);
|
|
|
|
FastDispatch = DeviceObject->DriverObject->FastIoDispatch;
|
|
|
|
|
|
|
|
/* Check if Fast Calls are supported, and check ReleaseForCcFlush */
|
|
|
|
if (FastDispatch &&
|
|
|
|
FastDispatch->ReleaseForCcFlush)
|
|
|
|
{
|
|
|
|
/* Call the ReleaseForCcFlush FastIo handler */
|
2013-08-31 16:36:34 +00:00
|
|
|
Status = FastDispatch->ReleaseForCcFlush(FileObject, BaseDeviceObject);
|
2013-08-29 22:07:24 +00:00
|
|
|
}
|
2013-08-31 16:36:34 +00:00
|
|
|
|
|
|
|
if (!NT_SUCCESS(Status))
|
2013-08-29 22:07:24 +00:00
|
|
|
{
|
2013-08-31 16:36:34 +00:00
|
|
|
/* No FastIo handler (or it failed). Release PagingIO resource and
|
|
|
|
then Main resource */
|
2013-08-29 22:07:24 +00:00
|
|
|
if (FcbHeader->PagingIoResource) ExReleaseResourceLite(FcbHeader->PagingIoResource);
|
|
|
|
if (FcbHeader->Resource) ExReleaseResourceLite(FcbHeader->Resource);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Release master FsRtl lock */
|
|
|
|
FsRtlExitFileSystem();
|
2009-01-18 10:23:15 +00:00
|
|
|
}
|
|
|
|
|
2022-09-05 17:33:19 +00:00
|
|
|
/**
|
|
|
|
* @brief Get the resource to acquire when Mod Writer flushes data to disk
|
|
|
|
*
|
|
|
|
* @param FcbHeader - FCB header from the file object
|
|
|
|
* @param EndingOffset - The end offset of the write to be done
|
|
|
|
* @param ResourceToAcquire - Pointer receiving the resource to acquire before doing the write
|
|
|
|
*
|
|
|
|
* @return BOOLEAN specifying whether the resource must be acquired exclusively
|
|
|
|
*/
|
|
|
|
static
|
|
|
|
BOOLEAN
|
|
|
|
FsRtlpGetResourceForModWrite(_In_ PFSRTL_COMMON_FCB_HEADER FcbHeader,
|
|
|
|
_In_ PLARGE_INTEGER EndingOffset,
|
|
|
|
_Outptr_result_maybenull_ PERESOURCE* ResourceToAcquire)
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
* Decide on type of locking and type of resource based on
|
|
|
|
* - Flags
|
|
|
|
* - Whether we're extending ValidDataLength
|
|
|
|
*/
|
|
|
|
if (FlagOn(FcbHeader->Flags, FSRTL_FLAG_ACQUIRE_MAIN_RSRC_EX))
|
|
|
|
{
|
|
|
|
/* Acquire main resource, exclusive */
|
|
|
|
*ResourceToAcquire = FcbHeader->Resource;
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* We will acquire shared. Which one ? */
|
|
|
|
if (FlagOn(FcbHeader->Flags, FSRTL_FLAG_ACQUIRE_MAIN_RSRC_SH))
|
|
|
|
{
|
|
|
|
*ResourceToAcquire = FcbHeader->Resource;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
*ResourceToAcquire = FcbHeader->PagingIoResource;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* We force exclusive lock if this write modifies the valid data length */
|
|
|
|
return (EndingOffset->QuadPart > FcbHeader->ValidDataLength.QuadPart);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Lock a file object before flushing pages to disk.
|
|
|
|
* To be called by the Modified Page Writer (MPW)
|
|
|
|
*
|
|
|
|
* @param FileObject - The file object to lock
|
|
|
|
* @param EndingOffset - The end offset of the write to be done
|
|
|
|
* @param ResourceToRelease - Pointer receiving the resource to release after the write
|
|
|
|
*
|
|
|
|
* @return Relevant NTSTATUS value
|
|
|
|
*/
|
|
|
|
_Check_return_
|
2013-08-31 16:36:34 +00:00
|
|
|
NTSTATUS
|
|
|
|
NTAPI
|
2022-09-05 17:33:19 +00:00
|
|
|
FsRtlAcquireFileForModWriteEx(_In_ PFILE_OBJECT FileObject,
|
|
|
|
_In_ PLARGE_INTEGER EndingOffset,
|
|
|
|
_Outptr_result_maybenull_ PERESOURCE *ResourceToRelease)
|
2013-08-31 16:36:34 +00:00
|
|
|
{
|
|
|
|
PFSRTL_COMMON_FCB_HEADER FcbHeader;
|
|
|
|
PDEVICE_OBJECT DeviceObject, BaseDeviceObject;
|
|
|
|
PFAST_IO_DISPATCH FastDispatch;
|
|
|
|
PERESOURCE ResourceToAcquire = NULL;
|
2022-09-05 17:33:19 +00:00
|
|
|
BOOLEAN Exclusive;
|
2013-08-31 16:36:34 +00:00
|
|
|
BOOLEAN Result;
|
|
|
|
NTSTATUS Status = STATUS_SUCCESS;
|
|
|
|
|
|
|
|
/* Get Device Object and Fast Calls */
|
|
|
|
FcbHeader = (PFSRTL_COMMON_FCB_HEADER)FileObject->FsContext;
|
|
|
|
DeviceObject = IoGetRelatedDeviceObject(FileObject);
|
|
|
|
BaseDeviceObject = IoGetBaseFileSystemDeviceObject(FileObject);
|
|
|
|
FastDispatch = DeviceObject->DriverObject->FastIoDispatch;
|
|
|
|
|
|
|
|
/* Check if Fast Calls are supported, and check AcquireForModWrite */
|
|
|
|
if (FastDispatch &&
|
|
|
|
FastDispatch->AcquireForModWrite)
|
|
|
|
{
|
|
|
|
/* Call the AcquireForModWrite FastIo handler */
|
|
|
|
Status = FastDispatch->AcquireForModWrite(FileObject,
|
|
|
|
EndingOffset,
|
|
|
|
ResourceToRelease,
|
|
|
|
BaseDeviceObject);
|
|
|
|
|
|
|
|
/* Return either success or inability to wait.
|
|
|
|
In case of other failure - fall through */
|
|
|
|
if (Status == STATUS_SUCCESS ||
|
|
|
|
Status == STATUS_CANT_WAIT)
|
|
|
|
{
|
|
|
|
return Status;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-05 17:33:19 +00:00
|
|
|
/* Check what and how we should acquire */
|
|
|
|
Exclusive = FsRtlpGetResourceForModWrite(FcbHeader, EndingOffset, &ResourceToAcquire);
|
2013-08-31 16:36:34 +00:00
|
|
|
|
2022-09-05 17:33:19 +00:00
|
|
|
/* Acquire the resource and loop until we're sure we got this right. */
|
2013-08-31 16:36:34 +00:00
|
|
|
while (TRUE)
|
|
|
|
{
|
2022-09-05 17:33:19 +00:00
|
|
|
BOOLEAN OldExclusive;
|
|
|
|
PERESOURCE OldResourceToAcquire;
|
2013-08-31 16:36:34 +00:00
|
|
|
|
2022-09-05 17:33:19 +00:00
|
|
|
if (ResourceToAcquire == NULL)
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
* There's nothing to acquire, we can simply return success
|
|
|
|
*/
|
2013-08-31 16:36:34 +00:00
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2022-09-05 17:33:19 +00:00
|
|
|
if (Exclusive)
|
2013-08-31 16:36:34 +00:00
|
|
|
{
|
2022-09-05 17:33:19 +00:00
|
|
|
Result = ExAcquireResourceExclusiveLite(ResourceToAcquire, FALSE);
|
2013-08-31 16:36:34 +00:00
|
|
|
}
|
2022-09-05 17:33:19 +00:00
|
|
|
else
|
2013-08-31 16:36:34 +00:00
|
|
|
{
|
2022-09-05 17:33:19 +00:00
|
|
|
Result = ExAcquireSharedWaitForExclusive(ResourceToAcquire, FALSE);
|
2013-08-31 16:36:34 +00:00
|
|
|
}
|
2022-09-05 17:33:19 +00:00
|
|
|
|
|
|
|
if (!Result)
|
2013-08-31 16:36:34 +00:00
|
|
|
{
|
2022-09-05 17:33:19 +00:00
|
|
|
return STATUS_CANT_WAIT;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Does this still hold true? */
|
|
|
|
OldExclusive = Exclusive;
|
|
|
|
OldResourceToAcquire = ResourceToAcquire;
|
|
|
|
Exclusive = FsRtlpGetResourceForModWrite(FcbHeader, EndingOffset, &ResourceToAcquire);
|
2013-08-31 16:36:34 +00:00
|
|
|
|
2022-09-05 17:33:19 +00:00
|
|
|
if ((OldExclusive == Exclusive) && (OldResourceToAcquire == ResourceToAcquire))
|
|
|
|
{
|
|
|
|
/* We're good */
|
2013-08-31 16:36:34 +00:00
|
|
|
break;
|
|
|
|
}
|
2022-09-05 17:33:19 +00:00
|
|
|
|
|
|
|
/* Can we fix this situation? */
|
|
|
|
if ((OldResourceToAcquire == ResourceToAcquire) && !Exclusive)
|
2013-08-31 16:36:34 +00:00
|
|
|
{
|
2022-09-05 17:33:19 +00:00
|
|
|
/* We can easily do so */
|
2013-08-31 16:36:34 +00:00
|
|
|
ExConvertExclusiveToSharedLite(ResourceToAcquire);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2022-09-05 17:33:19 +00:00
|
|
|
/* Things have changed since we acquired the lock. Start again */
|
|
|
|
ExReleaseResourceLite(OldResourceToAcquire);
|
|
|
|
}
|
2013-08-31 16:36:34 +00:00
|
|
|
|
2022-09-05 17:33:19 +00:00
|
|
|
/* If we're here, this means that we succeeded */
|
|
|
|
*ResourceToRelease = ResourceToAcquire;
|
|
|
|
return STATUS_SUCCESS;
|
2013-08-31 16:36:34 +00:00
|
|
|
}
|
|
|
|
|
2022-09-05 17:33:19 +00:00
|
|
|
/**
|
|
|
|
* @brief Unlock a file object after flushing pages to disk.
|
|
|
|
* To be called by the Modified Page Writer (MPW) after a succesful call to
|
|
|
|
* FsRtlAcquireFileForModWriteEx
|
|
|
|
*
|
|
|
|
* @param FileObject - The file object to unlock
|
|
|
|
* @param ResourceToRelease - The resource to release
|
|
|
|
*/
|
2013-08-31 16:36:34 +00:00
|
|
|
VOID
|
|
|
|
NTAPI
|
2022-09-05 17:33:19 +00:00
|
|
|
FsRtlReleaseFileForModWrite(_In_ PFILE_OBJECT FileObject,
|
|
|
|
_In_ PERESOURCE ResourceToRelease)
|
2013-08-31 16:36:34 +00:00
|
|
|
{
|
|
|
|
PDEVICE_OBJECT DeviceObject, BaseDeviceObject;
|
|
|
|
PFAST_IO_DISPATCH FastDispatch;
|
2022-09-05 17:33:19 +00:00
|
|
|
NTSTATUS Status = STATUS_UNSUCCESSFUL;
|
2013-08-31 16:36:34 +00:00
|
|
|
|
|
|
|
/* Get Device Object and Fast Calls */
|
|
|
|
DeviceObject = IoGetRelatedDeviceObject(FileObject);
|
|
|
|
BaseDeviceObject = IoGetBaseFileSystemDeviceObject(FileObject);
|
|
|
|
FastDispatch = DeviceObject->DriverObject->FastIoDispatch;
|
|
|
|
|
2022-09-05 17:33:19 +00:00
|
|
|
/* Check if Fast Calls are supported and check ReleaseForModWrite */
|
2013-08-31 16:36:34 +00:00
|
|
|
if (FastDispatch &&
|
|
|
|
FastDispatch->ReleaseForModWrite)
|
|
|
|
{
|
|
|
|
/* Call the ReleaseForModWrite FastIo handler */
|
|
|
|
Status = FastDispatch->ReleaseForModWrite(FileObject,
|
|
|
|
ResourceToRelease,
|
|
|
|
BaseDeviceObject);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Just release the resource if previous op failed */
|
|
|
|
if (!NT_SUCCESS(Status))
|
|
|
|
{
|
|
|
|
ExReleaseResourceLite(ResourceToRelease);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-01-18 10:23:15 +00:00
|
|
|
/*++
|
|
|
|
* @name FsRtlRegisterFileSystemFilterCallbacks
|
|
|
|
* @unimplemented
|
|
|
|
*
|
|
|
|
* FILLME
|
|
|
|
*
|
|
|
|
* @param FilterDriverObject
|
|
|
|
* FILLME
|
|
|
|
*
|
|
|
|
* @param Callbacks
|
|
|
|
* FILLME
|
|
|
|
*
|
|
|
|
* @return None
|
|
|
|
*
|
|
|
|
* @remarks None
|
|
|
|
*
|
|
|
|
*--*/
|
|
|
|
NTSTATUS
|
|
|
|
NTAPI
|
2014-01-28 23:40:27 +00:00
|
|
|
FsRtlRegisterFileSystemFilterCallbacks(
|
|
|
|
PDRIVER_OBJECT FilterDriverObject,
|
|
|
|
PFS_FILTER_CALLBACKS Callbacks)
|
2009-01-18 10:23:15 +00:00
|
|
|
{
|
2014-01-28 23:40:27 +00:00
|
|
|
PFS_FILTER_CALLBACKS NewCallbacks;
|
|
|
|
PEXTENDED_DRIVER_EXTENSION DriverExtension;
|
|
|
|
PAGED_CODE();
|
|
|
|
|
|
|
|
/* Verify parameters */
|
|
|
|
if ((FilterDriverObject == NULL) || (Callbacks == NULL))
|
|
|
|
{
|
|
|
|
return STATUS_INVALID_PARAMETER;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Allocate a buffer for a copy of the callbacks */
|
|
|
|
NewCallbacks = ExAllocatePoolWithTag(NonPagedPool,
|
|
|
|
Callbacks->SizeOfFsFilterCallbacks,
|
|
|
|
'gmSF');
|
|
|
|
if (NewCallbacks == NULL)
|
|
|
|
{
|
|
|
|
return STATUS_INSUFFICIENT_RESOURCES;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Copy the callbacks */
|
|
|
|
RtlCopyMemory(NewCallbacks, Callbacks, Callbacks->SizeOfFsFilterCallbacks);
|
|
|
|
|
|
|
|
/* Set the callbacks in the driver extension */
|
|
|
|
DriverExtension = (PEXTENDED_DRIVER_EXTENSION)FilterDriverObject->DriverExtension;
|
|
|
|
DriverExtension->FsFilterCallbacks = NewCallbacks;
|
|
|
|
|
|
|
|
return STATUS_SUCCESS;
|
2009-01-18 10:23:15 +00:00
|
|
|
}
|