2002-06-25 22:23:06 +00:00
|
|
|
/*
|
|
|
|
* ReactOS kernel
|
|
|
|
* Copyright (C) 2002 ReactOS Team
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, write to the Free Software
|
2008-03-08 21:45:51 +00:00
|
|
|
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
|
2002-06-25 22:23:06 +00:00
|
|
|
*
|
|
|
|
* COPYRIGHT: See COPYING in the top level directory
|
|
|
|
* PROJECT: ReactOS kernel
|
2008-02-10 11:20:29 +00:00
|
|
|
* FILE: drivers/filesystem/ntfs/blockdev.c
|
2002-06-25 22:23:06 +00:00
|
|
|
* PURPOSE: NTFS filesystem driver
|
2016-05-01 16:21:53 +00:00
|
|
|
* PROGRAMMERS: Eric Kohl
|
|
|
|
* Trevor Thompson
|
2002-06-25 22:23:06 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
/* INCLUDES *****************************************************************/
|
|
|
|
|
2005-07-20 02:52:52 +00:00
|
|
|
#include "ntfs.h"
|
2002-06-25 22:23:06 +00:00
|
|
|
|
|
|
|
#define NDEBUG
|
|
|
|
#include <debug.h>
|
|
|
|
|
|
|
|
/* FUNCTIONS ****************************************************************/
|
|
|
|
|
|
|
|
NTSTATUS
|
2014-09-26 06:41:51 +00:00
|
|
|
NtfsReadDisk(IN PDEVICE_OBJECT DeviceObject,
|
|
|
|
IN LONGLONG StartingOffset,
|
|
|
|
IN ULONG Length,
|
2014-11-04 07:56:20 +00:00
|
|
|
IN ULONG SectorSize,
|
2014-09-26 06:41:51 +00:00
|
|
|
IN OUT PUCHAR Buffer,
|
|
|
|
IN BOOLEAN Override)
|
2002-06-25 22:23:06 +00:00
|
|
|
{
|
2013-06-16 12:15:06 +00:00
|
|
|
PIO_STACK_LOCATION Stack;
|
|
|
|
IO_STATUS_BLOCK IoStatus;
|
|
|
|
LARGE_INTEGER Offset;
|
|
|
|
KEVENT Event;
|
|
|
|
PIRP Irp;
|
|
|
|
NTSTATUS Status;
|
2014-11-04 07:56:20 +00:00
|
|
|
ULONGLONG RealReadOffset;
|
|
|
|
ULONG RealLength;
|
|
|
|
BOOLEAN AllocatedBuffer = FALSE;
|
|
|
|
PUCHAR ReadBuffer = Buffer;
|
2013-06-16 12:15:06 +00:00
|
|
|
|
2017-06-26 05:17:08 +00:00
|
|
|
DPRINT("NtfsReadDisk(%p, %I64x, %lu, %lu, %p, %d)\n", DeviceObject, StartingOffset, Length, SectorSize, Buffer, Override);
|
2014-09-26 06:41:51 +00:00
|
|
|
|
2013-06-16 12:15:06 +00:00
|
|
|
KeInitializeEvent(&Event,
|
|
|
|
NotificationEvent,
|
|
|
|
FALSE);
|
|
|
|
|
2014-11-04 07:56:20 +00:00
|
|
|
RealReadOffset = (ULONGLONG)StartingOffset;
|
|
|
|
RealLength = Length;
|
|
|
|
|
|
|
|
if ((RealReadOffset % SectorSize) != 0 || (RealLength % SectorSize) != 0)
|
|
|
|
{
|
|
|
|
RealReadOffset = ROUND_DOWN(StartingOffset, SectorSize);
|
|
|
|
RealLength = ROUND_UP(Length, SectorSize);
|
|
|
|
|
|
|
|
ReadBuffer = ExAllocatePoolWithTag(NonPagedPool, RealLength + SectorSize, TAG_NTFS);
|
|
|
|
if (ReadBuffer == NULL)
|
|
|
|
{
|
|
|
|
DPRINT1("Not enough memory!\n");
|
|
|
|
return STATUS_INSUFFICIENT_RESOURCES;
|
|
|
|
}
|
|
|
|
AllocatedBuffer = TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
Offset.QuadPart = RealReadOffset;
|
2013-06-16 12:15:06 +00:00
|
|
|
|
|
|
|
DPRINT("Building synchronous FSD Request...\n");
|
|
|
|
Irp = IoBuildSynchronousFsdRequest(IRP_MJ_READ,
|
|
|
|
DeviceObject,
|
2014-11-04 07:56:20 +00:00
|
|
|
ReadBuffer,
|
|
|
|
RealLength,
|
2013-06-16 12:15:06 +00:00
|
|
|
&Offset,
|
|
|
|
&Event,
|
|
|
|
&IoStatus);
|
|
|
|
if (Irp == NULL)
|
2002-06-25 22:23:06 +00:00
|
|
|
{
|
2013-06-16 12:15:06 +00:00
|
|
|
DPRINT("IoBuildSynchronousFsdRequest failed\n");
|
2016-04-13 19:52:42 +00:00
|
|
|
|
|
|
|
if (AllocatedBuffer)
|
|
|
|
{
|
|
|
|
ExFreePoolWithTag(ReadBuffer, TAG_NTFS);
|
|
|
|
}
|
|
|
|
|
2013-06-16 12:15:06 +00:00
|
|
|
return STATUS_INSUFFICIENT_RESOURCES;
|
2002-06-25 22:23:06 +00:00
|
|
|
}
|
|
|
|
|
2013-06-16 12:15:06 +00:00
|
|
|
if (Override)
|
2002-06-25 22:23:06 +00:00
|
|
|
{
|
2013-06-16 12:15:06 +00:00
|
|
|
Stack = IoGetNextIrpStackLocation(Irp);
|
|
|
|
Stack->Flags |= SL_OVERRIDE_VERIFY_VOLUME;
|
2002-06-25 22:23:06 +00:00
|
|
|
}
|
|
|
|
|
2013-06-16 12:15:06 +00:00
|
|
|
DPRINT("Calling IO Driver... with irp %p\n", Irp);
|
|
|
|
Status = IoCallDriver(DeviceObject, Irp);
|
2002-06-25 22:23:06 +00:00
|
|
|
|
2013-06-16 12:15:06 +00:00
|
|
|
DPRINT("Waiting for IO Operation for %p\n", Irp);
|
|
|
|
if (Status == STATUS_PENDING)
|
2002-06-25 22:23:06 +00:00
|
|
|
{
|
2013-06-16 12:15:06 +00:00
|
|
|
DPRINT("Operation pending\n");
|
|
|
|
KeWaitForSingleObject(&Event, Suspended, KernelMode, FALSE, NULL);
|
|
|
|
DPRINT("Getting IO Status... for %p\n", Irp);
|
|
|
|
Status = IoStatus.Status;
|
2002-06-25 22:23:06 +00:00
|
|
|
}
|
|
|
|
|
2016-04-13 19:52:42 +00:00
|
|
|
if (AllocatedBuffer)
|
2014-11-04 07:56:20 +00:00
|
|
|
{
|
2016-04-13 19:52:42 +00:00
|
|
|
if (NT_SUCCESS(Status))
|
|
|
|
{
|
|
|
|
RtlCopyMemory(Buffer, ReadBuffer + (StartingOffset - RealReadOffset), Length);
|
|
|
|
}
|
|
|
|
|
2014-11-04 07:56:20 +00:00
|
|
|
ExFreePoolWithTag(ReadBuffer, TAG_NTFS);
|
|
|
|
}
|
|
|
|
|
2014-09-26 06:41:51 +00:00
|
|
|
DPRINT("NtfsReadDisk() done (Status %x)\n", Status);
|
2002-06-25 22:23:06 +00:00
|
|
|
|
2013-06-16 12:15:06 +00:00
|
|
|
return Status;
|
2002-06-25 22:23:06 +00:00
|
|
|
}
|
|
|
|
|
2016-05-01 16:21:53 +00:00
|
|
|
/**
|
|
|
|
* @name NtfsWriteDisk
|
|
|
|
* @implemented
|
|
|
|
*
|
|
|
|
* Writes data from the given buffer to the given DeviceObject.
|
|
|
|
*
|
|
|
|
* @param DeviceObject
|
|
|
|
* Device to write to
|
|
|
|
*
|
|
|
|
* @param StartingOffset
|
|
|
|
* Offset, in bytes, from the start of the device object where the data will be written
|
|
|
|
*
|
|
|
|
* @param Length
|
|
|
|
* How much data will be written, in bytes
|
|
|
|
*
|
|
|
|
* @param SectorSize
|
|
|
|
* Size of the sector on the disk that the write must be aligned to
|
|
|
|
*
|
|
|
|
* @param Buffer
|
|
|
|
* The data that's being written to the device
|
|
|
|
*
|
|
|
|
* @return
|
|
|
|
* STATUS_SUCCESS in case of success, STATUS_INSUFFICIENT_RESOURCES if a memory allocation failed,
|
|
|
|
* or whatever status IoCallDriver() sets.
|
|
|
|
*
|
|
|
|
* @remarks Called by NtfsWriteFile(). May perform a read-modify-write operation if the
|
|
|
|
* requested write is not sector-aligned.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
NTSTATUS
|
|
|
|
NtfsWriteDisk(IN PDEVICE_OBJECT DeviceObject,
|
|
|
|
IN LONGLONG StartingOffset,
|
|
|
|
IN ULONG Length,
|
|
|
|
IN ULONG SectorSize,
|
|
|
|
IN const PUCHAR Buffer)
|
|
|
|
{
|
|
|
|
IO_STATUS_BLOCK IoStatus;
|
|
|
|
LARGE_INTEGER Offset;
|
|
|
|
KEVENT Event;
|
|
|
|
PIRP Irp;
|
|
|
|
NTSTATUS Status;
|
|
|
|
ULONGLONG RealWriteOffset;
|
|
|
|
ULONG RealLength;
|
|
|
|
BOOLEAN AllocatedBuffer = FALSE;
|
|
|
|
PUCHAR TempBuffer = NULL;
|
|
|
|
|
2017-06-26 05:17:08 +00:00
|
|
|
DPRINT("NtfsWriteDisk(%p, %I64x, %lu, %lu, %p)\n", DeviceObject, StartingOffset, Length, SectorSize, Buffer);
|
2016-05-01 16:21:53 +00:00
|
|
|
|
|
|
|
if (Length == 0)
|
|
|
|
return STATUS_SUCCESS;
|
|
|
|
|
|
|
|
RealWriteOffset = (ULONGLONG)StartingOffset;
|
|
|
|
RealLength = Length;
|
|
|
|
|
|
|
|
// Does the write need to be adjusted to be sector-aligned?
|
|
|
|
if ((RealWriteOffset % SectorSize) != 0 || (RealLength % SectorSize) != 0)
|
|
|
|
{
|
|
|
|
ULONGLONG relativeOffset;
|
|
|
|
|
2021-06-11 12:29:21 +00:00
|
|
|
// We need to do a read-modify-write. We'll start be copying the entire
|
|
|
|
// contents of every sector that will be overwritten.
|
2016-05-01 16:21:53 +00:00
|
|
|
// TODO: Optimize (read no more than necessary)
|
|
|
|
|
|
|
|
RealWriteOffset = ROUND_DOWN(StartingOffset, SectorSize);
|
|
|
|
RealLength = ROUND_UP(Length, SectorSize);
|
|
|
|
|
|
|
|
// Would the end of our sector-aligned write fall short of the requested write?
|
|
|
|
if (RealWriteOffset + RealLength < StartingOffset + Length)
|
|
|
|
{
|
|
|
|
RealLength += SectorSize;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Did we underestimate the memory required somehow?
|
|
|
|
if (RealLength + RealWriteOffset < StartingOffset + Length)
|
|
|
|
{
|
|
|
|
DPRINT1("\a\t\t\t\t\tFIXME: calculated less memory than needed!\n");
|
|
|
|
DPRINT1("StartingOffset: %lu\tLength: %lu\tRealWriteOffset: %lu\tRealLength: %lu\n",
|
|
|
|
StartingOffset, Length, RealWriteOffset, RealLength);
|
|
|
|
|
|
|
|
RealLength += SectorSize;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Allocate a buffer to copy the existing data to
|
|
|
|
TempBuffer = ExAllocatePoolWithTag(NonPagedPool, RealLength, TAG_NTFS);
|
|
|
|
|
|
|
|
// Did we fail to allocate it?
|
|
|
|
if (TempBuffer == NULL)
|
|
|
|
{
|
|
|
|
DPRINT1("Not enough memory!\n");
|
|
|
|
|
|
|
|
return STATUS_INSUFFICIENT_RESOURCES;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Read the sectors we'll be overwriting into TempBuffer
|
|
|
|
Status = NtfsReadDisk(DeviceObject, RealWriteOffset, RealLength, SectorSize, TempBuffer, FALSE);
|
|
|
|
|
|
|
|
// Did we fail the read?
|
|
|
|
if (!NT_SUCCESS(Status))
|
|
|
|
{
|
|
|
|
RtlSecureZeroMemory(TempBuffer, RealLength);
|
|
|
|
ExFreePoolWithTag(TempBuffer, TAG_NTFS);
|
|
|
|
return Status;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Calculate where the new data should be written to, relative to the start of TempBuffer
|
|
|
|
relativeOffset = StartingOffset - RealWriteOffset;
|
|
|
|
|
|
|
|
// Modify the tempbuffer with the data being read
|
|
|
|
RtlCopyMemory(TempBuffer + relativeOffset, Buffer, Length);
|
|
|
|
|
|
|
|
AllocatedBuffer = TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
// set the destination offset
|
|
|
|
Offset.QuadPart = RealWriteOffset;
|
|
|
|
|
|
|
|
// setup the notification event for the write
|
|
|
|
KeInitializeEvent(&Event,
|
|
|
|
NotificationEvent,
|
|
|
|
FALSE);
|
|
|
|
|
|
|
|
DPRINT("Building synchronous FSD Request...\n");
|
|
|
|
|
|
|
|
// Build an IRP requesting the lower-level [disk] driver to perform the write
|
|
|
|
// TODO: Forward the existing IRP instead
|
|
|
|
Irp = IoBuildSynchronousFsdRequest(IRP_MJ_WRITE,
|
|
|
|
DeviceObject,
|
2021-06-11 12:29:21 +00:00
|
|
|
// if we allocated a temp buffer, use that instead of the Buffer parameter
|
2016-05-01 16:21:53 +00:00
|
|
|
((AllocatedBuffer) ? TempBuffer : Buffer),
|
|
|
|
RealLength,
|
|
|
|
&Offset,
|
|
|
|
&Event,
|
|
|
|
&IoStatus);
|
|
|
|
// Did we fail to build the IRP?
|
|
|
|
if (Irp == NULL)
|
|
|
|
{
|
|
|
|
DPRINT1("IoBuildSynchronousFsdRequest failed\n");
|
|
|
|
|
|
|
|
if (AllocatedBuffer)
|
|
|
|
{
|
|
|
|
RtlSecureZeroMemory(TempBuffer, RealLength);
|
|
|
|
ExFreePoolWithTag(TempBuffer, TAG_NTFS);
|
|
|
|
}
|
|
|
|
|
|
|
|
return STATUS_INSUFFICIENT_RESOURCES;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Call the next-lower driver to perform the write
|
|
|
|
DPRINT("Calling IO Driver with irp %p\n", Irp);
|
|
|
|
Status = IoCallDriver(DeviceObject, Irp);
|
|
|
|
|
|
|
|
// Wait until the next-lower driver has completed the IRP
|
|
|
|
DPRINT("Waiting for IO Operation for %p\n", Irp);
|
|
|
|
if (Status == STATUS_PENDING)
|
|
|
|
{
|
|
|
|
DPRINT("Operation pending\n");
|
|
|
|
KeWaitForSingleObject(&Event, Suspended, KernelMode, FALSE, NULL);
|
|
|
|
DPRINT("Getting IO Status... for %p\n", Irp);
|
|
|
|
Status = IoStatus.Status;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (AllocatedBuffer)
|
|
|
|
{
|
|
|
|
// zero the buffer before freeing it, so private user data can't be snooped
|
|
|
|
RtlSecureZeroMemory(TempBuffer, RealLength);
|
|
|
|
|
|
|
|
ExFreePoolWithTag(TempBuffer, TAG_NTFS);
|
|
|
|
}
|
|
|
|
|
|
|
|
DPRINT("NtfsWriteDisk() done (Status %x)\n", Status);
|
|
|
|
|
|
|
|
return Status;
|
|
|
|
}
|
|
|
|
|
2014-09-26 06:41:51 +00:00
|
|
|
NTSTATUS
|
|
|
|
NtfsReadSectors(IN PDEVICE_OBJECT DeviceObject,
|
|
|
|
IN ULONG DiskSector,
|
|
|
|
IN ULONG SectorCount,
|
|
|
|
IN ULONG SectorSize,
|
|
|
|
IN OUT PUCHAR Buffer,
|
|
|
|
IN BOOLEAN Override)
|
|
|
|
{
|
|
|
|
LONGLONG Offset;
|
|
|
|
ULONG BlockSize;
|
|
|
|
|
|
|
|
Offset = (LONGLONG)DiskSector * (LONGLONG)SectorSize;
|
|
|
|
BlockSize = SectorCount * SectorSize;
|
|
|
|
|
2014-11-04 07:56:20 +00:00
|
|
|
return NtfsReadDisk(DeviceObject, Offset, BlockSize, SectorSize, Buffer, Override);
|
2014-09-26 06:41:51 +00:00
|
|
|
}
|
|
|
|
|
2002-06-25 22:23:06 +00:00
|
|
|
|
|
|
|
NTSTATUS
|
|
|
|
NtfsDeviceIoControl(IN PDEVICE_OBJECT DeviceObject,
|
2013-06-16 12:15:06 +00:00
|
|
|
IN ULONG ControlCode,
|
|
|
|
IN PVOID InputBuffer,
|
|
|
|
IN ULONG InputBufferSize,
|
|
|
|
IN OUT PVOID OutputBuffer,
|
|
|
|
IN OUT PULONG OutputBufferSize,
|
|
|
|
IN BOOLEAN Override)
|
2002-06-25 22:23:06 +00:00
|
|
|
{
|
2013-06-16 12:15:06 +00:00
|
|
|
PIO_STACK_LOCATION Stack;
|
|
|
|
IO_STATUS_BLOCK IoStatus;
|
|
|
|
KEVENT Event;
|
|
|
|
PIRP Irp;
|
|
|
|
NTSTATUS Status;
|
|
|
|
|
|
|
|
KeInitializeEvent(&Event, NotificationEvent, FALSE);
|
|
|
|
|
|
|
|
DPRINT("Building device I/O control request ...\n");
|
|
|
|
Irp = IoBuildDeviceIoControlRequest(ControlCode,
|
|
|
|
DeviceObject,
|
|
|
|
InputBuffer,
|
|
|
|
InputBufferSize,
|
|
|
|
OutputBuffer,
|
|
|
|
(OutputBufferSize) ? *OutputBufferSize : 0,
|
|
|
|
FALSE,
|
|
|
|
&Event,
|
|
|
|
&IoStatus);
|
|
|
|
if (Irp == NULL)
|
2002-06-25 22:23:06 +00:00
|
|
|
{
|
2013-06-16 12:15:06 +00:00
|
|
|
DPRINT("IoBuildDeviceIoControlRequest() failed\n");
|
|
|
|
return STATUS_INSUFFICIENT_RESOURCES;
|
2003-11-12 15:30:21 +00:00
|
|
|
}
|
|
|
|
|
2013-06-16 12:15:06 +00:00
|
|
|
if (Override)
|
2003-11-12 15:30:21 +00:00
|
|
|
{
|
2013-06-16 12:15:06 +00:00
|
|
|
Stack = IoGetNextIrpStackLocation(Irp);
|
|
|
|
Stack->Flags |= SL_OVERRIDE_VERIFY_VOLUME;
|
2002-06-25 22:23:06 +00:00
|
|
|
}
|
|
|
|
|
2013-06-16 12:15:06 +00:00
|
|
|
DPRINT("Calling IO Driver... with irp %p\n", Irp);
|
|
|
|
Status = IoCallDriver(DeviceObject, Irp);
|
|
|
|
if (Status == STATUS_PENDING)
|
2002-06-25 22:23:06 +00:00
|
|
|
{
|
2013-06-16 12:15:06 +00:00
|
|
|
KeWaitForSingleObject(&Event, Suspended, KernelMode, FALSE, NULL);
|
|
|
|
Status = IoStatus.Status;
|
2002-06-25 22:23:06 +00:00
|
|
|
}
|
|
|
|
|
2013-06-16 12:15:06 +00:00
|
|
|
if (OutputBufferSize)
|
2002-06-25 22:23:06 +00:00
|
|
|
{
|
2013-06-16 12:15:06 +00:00
|
|
|
*OutputBufferSize = IoStatus.Information;
|
2002-06-25 22:23:06 +00:00
|
|
|
}
|
|
|
|
|
2013-06-16 12:15:06 +00:00
|
|
|
return Status;
|
2002-06-25 22:23:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* EOF */
|