1999-01-13 15:14:48 +00:00
|
|
|
/*
|
|
|
|
* COPYRIGHT: See COPYING in the top level directory
|
|
|
|
* PROJECT: ReactOS kernel
|
2003-10-11 17:51:56 +00:00
|
|
|
* FILE: drivers/fs/vfat/blockdev.c
|
1999-01-13 15:14:48 +00:00
|
|
|
* PURPOSE: Temporary sector reading support
|
2001-01-16 09:55:02 +00:00
|
|
|
* PROGRAMMER: David Welch (welch@cwcom.net)
|
2005-05-08 02:16:32 +00:00
|
|
|
* UPDATE HISTORY:
|
1999-01-13 15:14:48 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
/* INCLUDES *****************************************************************/
|
|
|
|
|
|
|
|
#include "vfat.h"
|
|
|
|
|
2013-12-19 16:20:28 +00:00
|
|
|
#define NDEBUG
|
|
|
|
#include <debug.h>
|
|
|
|
|
1999-01-13 15:14:48 +00:00
|
|
|
/* FUNCTIONS ***************************************************************/
|
|
|
|
|
2007-11-21 16:50:07 +00:00
|
|
|
static IO_COMPLETION_ROUTINE VfatReadWritePartialCompletion;
|
2013-12-09 10:35:15 +00:00
|
|
|
|
|
|
|
static
|
|
|
|
NTSTATUS
|
|
|
|
NTAPI
|
|
|
|
VfatReadWritePartialCompletion(
|
|
|
|
IN PDEVICE_OBJECT DeviceObject,
|
|
|
|
IN PIRP Irp,
|
|
|
|
IN PVOID Context)
|
2004-05-15 23:00:02 +00:00
|
|
|
{
|
2013-12-09 10:35:15 +00:00
|
|
|
PVFAT_IRP_CONTEXT IrpContext;
|
|
|
|
PMDL Mdl;
|
2004-05-15 23:00:02 +00:00
|
|
|
|
2013-12-09 10:35:15 +00:00
|
|
|
UNREFERENCED_PARAMETER(DeviceObject);
|
2009-06-19 00:21:21 +00:00
|
|
|
|
2013-12-09 10:35:15 +00:00
|
|
|
DPRINT("VfatReadWritePartialCompletion() called\n");
|
2004-05-15 23:00:02 +00:00
|
|
|
|
2013-12-09 10:35:15 +00:00
|
|
|
IrpContext = (PVFAT_IRP_CONTEXT)Context;
|
2004-05-15 23:00:02 +00:00
|
|
|
|
2013-12-09 10:35:15 +00:00
|
|
|
while ((Mdl = Irp->MdlAddress))
|
2004-05-15 23:00:02 +00:00
|
|
|
{
|
2013-12-09 10:35:15 +00:00
|
|
|
Irp->MdlAddress = Mdl->Next;
|
|
|
|
IoFreeMdl(Mdl);
|
2004-05-15 23:00:02 +00:00
|
|
|
}
|
2013-12-09 10:35:15 +00:00
|
|
|
|
|
|
|
if (Irp->PendingReturned)
|
2004-05-15 23:00:02 +00:00
|
|
|
{
|
2013-12-09 10:35:15 +00:00
|
|
|
IrpContext->Flags |= IRPCONTEXT_PENDINGRETURNED;
|
2004-05-15 23:00:02 +00:00
|
|
|
}
|
2013-12-09 10:35:15 +00:00
|
|
|
else
|
2011-06-19 20:03:49 +00:00
|
|
|
{
|
2013-12-09 10:35:15 +00:00
|
|
|
IrpContext->Flags &= ~IRPCONTEXT_PENDINGRETURNED;
|
2011-06-19 20:03:49 +00:00
|
|
|
}
|
2013-12-09 10:35:15 +00:00
|
|
|
|
|
|
|
if (!NT_SUCCESS(Irp->IoStatus.Status))
|
2004-05-15 23:00:02 +00:00
|
|
|
{
|
2013-12-09 10:35:15 +00:00
|
|
|
IrpContext->Irp->IoStatus.Status = Irp->IoStatus.Status;
|
2004-05-15 23:00:02 +00:00
|
|
|
}
|
2013-12-09 10:35:15 +00:00
|
|
|
|
|
|
|
if (0 == InterlockedDecrement((PLONG)&IrpContext->RefCount) &&
|
2017-02-17 17:58:18 +00:00
|
|
|
BooleanFlagOn(IrpContext->Flags, IRPCONTEXT_PENDINGRETURNED))
|
2004-05-15 23:00:02 +00:00
|
|
|
{
|
2013-12-09 10:35:15 +00:00
|
|
|
KeSetEvent(&IrpContext->Event, IO_NO_INCREMENT, FALSE);
|
2004-05-15 23:00:02 +00:00
|
|
|
}
|
|
|
|
|
2013-12-09 10:35:15 +00:00
|
|
|
IoFreeIrp(Irp);
|
|
|
|
|
|
|
|
DPRINT("VfatReadWritePartialCompletion() done\n");
|
2004-05-15 23:00:02 +00:00
|
|
|
|
2013-12-09 10:35:15 +00:00
|
|
|
return STATUS_MORE_PROCESSING_REQUIRED;
|
2004-05-15 23:00:02 +00:00
|
|
|
}
|
|
|
|
|
2001-01-08 02:14:06 +00:00
|
|
|
NTSTATUS
|
2013-12-09 10:35:15 +00:00
|
|
|
VfatReadDisk(
|
|
|
|
IN PDEVICE_OBJECT pDeviceObject,
|
|
|
|
IN PLARGE_INTEGER ReadOffset,
|
|
|
|
IN ULONG ReadLength,
|
|
|
|
IN OUT PUCHAR Buffer,
|
|
|
|
IN BOOLEAN Override)
|
1999-01-13 15:14:48 +00:00
|
|
|
{
|
2013-12-09 10:35:15 +00:00
|
|
|
PIO_STACK_LOCATION Stack;
|
|
|
|
PIRP Irp;
|
|
|
|
IO_STATUS_BLOCK IoStatus;
|
|
|
|
KEVENT Event;
|
|
|
|
NTSTATUS Status;
|
2000-12-29 23:17:12 +00:00
|
|
|
|
2011-06-19 20:03:49 +00:00
|
|
|
again:
|
2013-12-09 10:35:15 +00:00
|
|
|
KeInitializeEvent(&Event, NotificationEvent, FALSE);
|
|
|
|
|
|
|
|
DPRINT("VfatReadDisk(pDeviceObject %p, Offset %I64x, Length %u, Buffer %p)\n",
|
|
|
|
pDeviceObject, ReadOffset->QuadPart, ReadLength, Buffer);
|
|
|
|
|
|
|
|
DPRINT ("Building synchronous FSD Request...\n");
|
|
|
|
Irp = IoBuildSynchronousFsdRequest(IRP_MJ_READ,
|
|
|
|
pDeviceObject,
|
|
|
|
Buffer,
|
|
|
|
ReadLength,
|
|
|
|
ReadOffset,
|
|
|
|
&Event,
|
|
|
|
&IoStatus);
|
|
|
|
if (Irp == NULL)
|
2000-12-29 23:17:12 +00:00
|
|
|
{
|
2013-12-09 10:35:15 +00:00
|
|
|
DPRINT("IoBuildSynchronousFsdRequest failed\n");
|
|
|
|
return STATUS_UNSUCCESSFUL;
|
1999-01-13 15:14:48 +00:00
|
|
|
}
|
|
|
|
|
2013-12-09 10:35:15 +00:00
|
|
|
if (Override)
|
2003-11-12 15:26:44 +00:00
|
|
|
{
|
2013-12-09 10:35:15 +00:00
|
|
|
Stack = IoGetNextIrpStackLocation(Irp);
|
|
|
|
Stack->Flags |= SL_OVERRIDE_VERIFY_VOLUME;
|
2003-11-12 15:26:44 +00:00
|
|
|
}
|
|
|
|
|
2013-12-09 10:35:15 +00:00
|
|
|
DPRINT("Calling IO Driver... with irp %p\n", Irp);
|
|
|
|
Status = IoCallDriver (pDeviceObject, Irp);
|
2000-12-29 23:17:12 +00:00
|
|
|
|
2013-12-09 10:35:15 +00:00
|
|
|
DPRINT("Waiting for IO Operation for %p\n", Irp);
|
|
|
|
if (Status == STATUS_PENDING)
|
2000-12-29 23:17:12 +00:00
|
|
|
{
|
2013-12-09 10:35:15 +00:00
|
|
|
DPRINT("Operation pending\n");
|
|
|
|
KeWaitForSingleObject(&Event, Suspended, KernelMode, FALSE, NULL);
|
|
|
|
DPRINT("Getting IO Status... for %p\n", Irp);
|
|
|
|
Status = IoStatus.Status;
|
1999-01-13 15:14:48 +00:00
|
|
|
}
|
|
|
|
|
2013-12-09 10:35:15 +00:00
|
|
|
if (Status == STATUS_VERIFY_REQUIRED)
|
2011-06-19 20:03:49 +00:00
|
|
|
{
|
2013-12-09 10:35:15 +00:00
|
|
|
PDEVICE_OBJECT DeviceToVerify;
|
2011-06-19 20:03:49 +00:00
|
|
|
|
2013-12-09 10:35:15 +00:00
|
|
|
DPRINT1 ("Media change detected!\n");
|
2011-06-19 20:03:49 +00:00
|
|
|
|
2013-12-09 10:35:15 +00:00
|
|
|
/* Find the device to verify and reset the thread field to empty value again. */
|
|
|
|
DeviceToVerify = IoGetDeviceToVerify(PsGetCurrentThread());
|
|
|
|
IoSetDeviceToVerify(PsGetCurrentThread(), NULL);
|
|
|
|
Status = IoVerifyVolume(DeviceToVerify,
|
|
|
|
FALSE);
|
|
|
|
if (NT_SUCCESS(Status))
|
2011-06-19 20:03:49 +00:00
|
|
|
{
|
2013-12-09 10:35:15 +00:00
|
|
|
DPRINT1("Volume verification successful; Reissuing read request\n");
|
|
|
|
goto again;
|
2011-06-19 20:03:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-12-09 10:35:15 +00:00
|
|
|
if (!NT_SUCCESS(Status))
|
2000-12-29 23:17:12 +00:00
|
|
|
{
|
2013-12-09 10:35:15 +00:00
|
|
|
DPRINT("IO failed!!! VfatReadDisk : Error code: %x\n", Status);
|
|
|
|
DPRINT("(pDeviceObject %p, Offset %I64x, Size %u, Buffer %p\n",
|
|
|
|
pDeviceObject, ReadOffset->QuadPart, ReadLength, Buffer);
|
|
|
|
return Status;
|
1999-01-13 15:14:48 +00:00
|
|
|
}
|
2013-12-09 10:35:15 +00:00
|
|
|
DPRINT("Block request succeeded for %p\n", Irp);
|
|
|
|
return STATUS_SUCCESS;
|
1999-01-13 15:14:48 +00:00
|
|
|
}
|
|
|
|
|
2001-01-08 02:14:06 +00:00
|
|
|
NTSTATUS
|
2013-12-09 10:35:15 +00:00
|
|
|
VfatReadDiskPartial(
|
|
|
|
IN PVFAT_IRP_CONTEXT IrpContext,
|
|
|
|
IN PLARGE_INTEGER ReadOffset,
|
|
|
|
IN ULONG ReadLength,
|
|
|
|
ULONG BufferOffset,
|
|
|
|
IN BOOLEAN Wait)
|
1999-01-13 15:14:48 +00:00
|
|
|
{
|
2013-12-09 10:35:15 +00:00
|
|
|
PIRP Irp;
|
|
|
|
PIO_STACK_LOCATION StackPtr;
|
|
|
|
NTSTATUS Status;
|
|
|
|
PVOID Buffer;
|
2000-12-29 23:17:12 +00:00
|
|
|
|
2013-12-09 10:35:15 +00:00
|
|
|
DPRINT("VfatReadDiskPartial(IrpContext %p, ReadOffset %I64x, ReadLength %u, BufferOffset %u, Wait %u)\n",
|
|
|
|
IrpContext, ReadOffset->QuadPart, ReadLength, BufferOffset, Wait);
|
2000-12-29 23:17:12 +00:00
|
|
|
|
2013-12-09 10:35:15 +00:00
|
|
|
DPRINT("Building asynchronous FSD Request...\n");
|
2004-05-15 23:00:02 +00:00
|
|
|
|
2013-12-09 10:35:15 +00:00
|
|
|
Buffer = (PCHAR)MmGetMdlVirtualAddress(IrpContext->Irp->MdlAddress) + BufferOffset;
|
2005-05-08 02:16:32 +00:00
|
|
|
|
2011-06-19 20:03:49 +00:00
|
|
|
again:
|
2013-12-09 10:35:15 +00:00
|
|
|
Irp = IoAllocateIrp(IrpContext->DeviceExt->StorageDevice->StackSize, TRUE);
|
|
|
|
if (Irp == NULL)
|
2004-05-15 23:00:02 +00:00
|
|
|
{
|
2013-12-09 10:35:15 +00:00
|
|
|
DPRINT("IoAllocateIrp failed\n");
|
|
|
|
return STATUS_UNSUCCESSFUL;
|
2004-05-15 23:00:02 +00:00
|
|
|
}
|
|
|
|
|
2013-12-09 10:35:15 +00:00
|
|
|
Irp->UserIosb = NULL;
|
|
|
|
Irp->Tail.Overlay.Thread = PsGetCurrentThread();
|
|
|
|
|
|
|
|
StackPtr = IoGetNextIrpStackLocation(Irp);
|
|
|
|
StackPtr->MajorFunction = IRP_MJ_READ;
|
|
|
|
StackPtr->MinorFunction = 0;
|
|
|
|
StackPtr->Flags = 0;
|
|
|
|
StackPtr->Control = 0;
|
|
|
|
StackPtr->DeviceObject = IrpContext->DeviceExt->StorageDevice;
|
|
|
|
StackPtr->FileObject = NULL;
|
|
|
|
StackPtr->CompletionRoutine = NULL;
|
|
|
|
StackPtr->Parameters.Read.Length = ReadLength;
|
|
|
|
StackPtr->Parameters.Read.ByteOffset = *ReadOffset;
|
|
|
|
|
|
|
|
if (!IoAllocateMdl(Buffer, ReadLength, FALSE, FALSE, Irp))
|
2004-05-15 23:00:02 +00:00
|
|
|
{
|
2013-12-09 10:35:15 +00:00
|
|
|
DPRINT("IoAllocateMdl failed\n");
|
|
|
|
IoFreeIrp(Irp);
|
|
|
|
return STATUS_UNSUCCESSFUL;
|
2004-05-15 23:00:02 +00:00
|
|
|
}
|
|
|
|
|
2013-12-09 10:35:15 +00:00
|
|
|
IoBuildPartialMdl(IrpContext->Irp->MdlAddress, Irp->MdlAddress, Buffer, ReadLength);
|
2004-05-15 23:00:02 +00:00
|
|
|
|
2013-12-09 10:35:15 +00:00
|
|
|
IoSetCompletionRoutine(Irp,
|
|
|
|
VfatReadWritePartialCompletion,
|
|
|
|
IrpContext,
|
|
|
|
TRUE,
|
|
|
|
TRUE,
|
|
|
|
TRUE);
|
2004-05-15 23:00:02 +00:00
|
|
|
|
2013-12-09 10:35:15 +00:00
|
|
|
if (Wait)
|
2004-05-15 23:00:02 +00:00
|
|
|
{
|
2013-12-09 10:35:15 +00:00
|
|
|
KeInitializeEvent(&IrpContext->Event, NotificationEvent, FALSE);
|
|
|
|
IrpContext->RefCount = 1;
|
2004-05-15 23:00:02 +00:00
|
|
|
}
|
2013-12-09 10:35:15 +00:00
|
|
|
else
|
2004-05-15 23:00:02 +00:00
|
|
|
{
|
2013-12-09 10:35:15 +00:00
|
|
|
InterlockedIncrement((PLONG)&IrpContext->RefCount);
|
2004-05-15 23:00:02 +00:00
|
|
|
}
|
|
|
|
|
2013-12-09 10:35:15 +00:00
|
|
|
DPRINT("Calling IO Driver... with irp %p\n", Irp);
|
|
|
|
Status = IoCallDriver(IrpContext->DeviceExt->StorageDevice, Irp);
|
2004-05-15 23:00:02 +00:00
|
|
|
|
2013-12-09 10:35:15 +00:00
|
|
|
if (Wait && Status == STATUS_PENDING)
|
2004-05-15 23:00:02 +00:00
|
|
|
{
|
2013-12-09 10:35:15 +00:00
|
|
|
KeWaitForSingleObject(&IrpContext->Event, Executive, KernelMode, FALSE, NULL);
|
|
|
|
Status = IrpContext->Irp->IoStatus.Status;
|
2004-05-15 23:00:02 +00:00
|
|
|
}
|
|
|
|
|
2013-12-09 10:35:15 +00:00
|
|
|
if (Status == STATUS_VERIFY_REQUIRED)
|
2011-06-19 20:03:49 +00:00
|
|
|
{
|
2013-12-09 10:35:15 +00:00
|
|
|
PDEVICE_OBJECT DeviceToVerify;
|
2011-06-19 20:03:49 +00:00
|
|
|
|
2013-12-09 10:35:15 +00:00
|
|
|
DPRINT1("Media change detected!\n");
|
2011-06-19 20:03:49 +00:00
|
|
|
|
2013-12-09 10:35:15 +00:00
|
|
|
/* Find the device to verify and reset the thread field to empty value again. */
|
|
|
|
DeviceToVerify = IoGetDeviceToVerify(PsGetCurrentThread());
|
|
|
|
IoSetDeviceToVerify(PsGetCurrentThread(), NULL);
|
|
|
|
Status = IoVerifyVolume(DeviceToVerify,
|
|
|
|
FALSE);
|
|
|
|
if (NT_SUCCESS(Status))
|
2011-06-19 20:03:49 +00:00
|
|
|
{
|
2013-12-09 10:35:15 +00:00
|
|
|
DPRINT1("Volume verification successful; Reissuing read request\n");
|
|
|
|
goto again;
|
2011-06-19 20:03:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-12-09 10:35:15 +00:00
|
|
|
DPRINT("%x\n", Status);
|
|
|
|
return Status;
|
2004-05-15 23:00:02 +00:00
|
|
|
}
|
|
|
|
|
[FASTFAT] Completely rewrite support for dirty volumes.
Until now, our support for dirty volumes was totally broken
to a point where, on FAT32 volume, the dirty couldn't even
be written nor read from the disk.
This commit totally rewrites its handling, for both FAT16 and FAT32
so that it's now fully functionnal. Furthermore, it also gets
totally compatible with our vfatlib, and thus, autochk.
Now, on mount, FastFAT will check if the volume is dirty or not, and
autochk will be able to ask for a repair if dirty. vfatlib will
repair the volume and remove the dirty bit. So that, on next
reboot, the volume will be mounted clean.
As a reminder, the dirty bit is set immediately after mounting
the volume, so that, if you crash or have a powercut, autochk
will always attempt to repair your volume (with more or less,
that's FAT!).
If you want to experience without breaking your FAT volume,
just boot, open a cmd prompt and type: fsutil dirty set c:
and reboot!
CORE-13758
CORE-13760
CORE-13759
2018-05-18 21:00:13 +00:00
|
|
|
/* Used by dirty bit code, likely to be killed the day it's properly handle
|
|
|
|
* This is just a copy paste from VfatReadDisk()
|
|
|
|
*/
|
|
|
|
NTSTATUS
|
|
|
|
VfatWriteDisk(
|
|
|
|
IN PDEVICE_OBJECT pDeviceObject,
|
|
|
|
IN PLARGE_INTEGER WriteOffset,
|
|
|
|
IN ULONG WriteLength,
|
|
|
|
IN OUT PUCHAR Buffer,
|
|
|
|
IN BOOLEAN Override)
|
|
|
|
{
|
|
|
|
PIO_STACK_LOCATION Stack;
|
|
|
|
PIRP Irp;
|
|
|
|
IO_STATUS_BLOCK IoStatus;
|
|
|
|
KEVENT Event;
|
|
|
|
NTSTATUS Status;
|
|
|
|
|
|
|
|
again:
|
|
|
|
KeInitializeEvent(&Event, NotificationEvent, FALSE);
|
|
|
|
|
|
|
|
DPRINT("VfatWriteDisk(pDeviceObject %p, Offset %I64x, Length %u, Buffer %p)\n",
|
|
|
|
pDeviceObject, WriteOffset->QuadPart, WriteLength, Buffer);
|
|
|
|
|
|
|
|
DPRINT ("Building synchronous FSD Request...\n");
|
|
|
|
Irp = IoBuildSynchronousFsdRequest(IRP_MJ_WRITE,
|
|
|
|
pDeviceObject,
|
|
|
|
Buffer,
|
|
|
|
WriteLength,
|
|
|
|
WriteOffset,
|
|
|
|
&Event,
|
|
|
|
&IoStatus);
|
|
|
|
if (Irp == NULL)
|
|
|
|
{
|
|
|
|
DPRINT("IoBuildSynchronousFsdRequest failed\n");
|
|
|
|
return STATUS_UNSUCCESSFUL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (Override)
|
|
|
|
{
|
|
|
|
Stack = IoGetNextIrpStackLocation(Irp);
|
|
|
|
Stack->Flags |= SL_OVERRIDE_VERIFY_VOLUME;
|
|
|
|
}
|
|
|
|
|
|
|
|
DPRINT("Calling IO Driver... with irp %p\n", Irp);
|
|
|
|
Status = IoCallDriver (pDeviceObject, 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 (Status == STATUS_VERIFY_REQUIRED)
|
|
|
|
{
|
|
|
|
PDEVICE_OBJECT DeviceToVerify;
|
|
|
|
|
|
|
|
DPRINT1 ("Media change detected!\n");
|
|
|
|
|
|
|
|
/* Find the device to verify and reset the thread field to empty value again. */
|
|
|
|
DeviceToVerify = IoGetDeviceToVerify(PsGetCurrentThread());
|
|
|
|
IoSetDeviceToVerify(PsGetCurrentThread(), NULL);
|
|
|
|
Status = IoVerifyVolume(DeviceToVerify,
|
|
|
|
FALSE);
|
|
|
|
if (NT_SUCCESS(Status))
|
|
|
|
{
|
|
|
|
DPRINT1("Volume verification successful; Reissuing write request\n");
|
|
|
|
goto again;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!NT_SUCCESS(Status))
|
|
|
|
{
|
|
|
|
DPRINT("IO failed!!! VfatWriteDisk : Error code: %x\n", Status);
|
|
|
|
DPRINT("(pDeviceObject %p, Offset %I64x, Size %u, Buffer %p\n",
|
|
|
|
pDeviceObject, WriteOffset->QuadPart, WriteLength, Buffer);
|
|
|
|
return Status;
|
|
|
|
}
|
|
|
|
DPRINT("Block request succeeded for %p\n", Irp);
|
|
|
|
return STATUS_SUCCESS;
|
|
|
|
}
|
2004-05-15 23:00:02 +00:00
|
|
|
|
|
|
|
NTSTATUS
|
2013-12-09 10:35:15 +00:00
|
|
|
VfatWriteDiskPartial(
|
|
|
|
IN PVFAT_IRP_CONTEXT IrpContext,
|
|
|
|
IN PLARGE_INTEGER WriteOffset,
|
|
|
|
IN ULONG WriteLength,
|
|
|
|
IN ULONG BufferOffset,
|
|
|
|
IN BOOLEAN Wait)
|
2004-05-15 23:00:02 +00:00
|
|
|
{
|
2013-12-09 10:35:15 +00:00
|
|
|
PIRP Irp;
|
|
|
|
PIO_STACK_LOCATION StackPtr;
|
|
|
|
NTSTATUS Status;
|
|
|
|
PVOID Buffer;
|
2004-05-15 23:00:02 +00:00
|
|
|
|
2013-12-09 10:35:15 +00:00
|
|
|
DPRINT("VfatWriteDiskPartial(IrpContext %p, WriteOffset %I64x, WriteLength %u, BufferOffset %x, Wait %u)\n",
|
|
|
|
IrpContext, WriteOffset->QuadPart, WriteLength, BufferOffset, Wait);
|
2004-05-15 23:00:02 +00:00
|
|
|
|
2013-12-09 10:35:15 +00:00
|
|
|
Buffer = (PCHAR)MmGetMdlVirtualAddress(IrpContext->Irp->MdlAddress) + BufferOffset;
|
2000-12-29 23:17:12 +00:00
|
|
|
|
2011-06-19 20:03:49 +00:00
|
|
|
again:
|
2013-12-09 10:35:15 +00:00
|
|
|
DPRINT("Building asynchronous FSD Request...\n");
|
|
|
|
Irp = IoAllocateIrp(IrpContext->DeviceExt->StorageDevice->StackSize, TRUE);
|
|
|
|
if (Irp == NULL)
|
2000-12-29 23:17:12 +00:00
|
|
|
{
|
2013-12-09 10:35:15 +00:00
|
|
|
DPRINT("IoAllocateIrp failed\n");
|
|
|
|
return STATUS_UNSUCCESSFUL;
|
1999-01-13 15:14:48 +00:00
|
|
|
}
|
|
|
|
|
2013-12-09 10:35:15 +00:00
|
|
|
Irp->UserIosb = NULL;
|
|
|
|
Irp->Tail.Overlay.Thread = PsGetCurrentThread();
|
|
|
|
|
|
|
|
StackPtr = IoGetNextIrpStackLocation(Irp);
|
|
|
|
StackPtr->MajorFunction = IRP_MJ_WRITE;
|
|
|
|
StackPtr->MinorFunction = 0;
|
|
|
|
StackPtr->Flags = 0;
|
|
|
|
StackPtr->Control = 0;
|
|
|
|
StackPtr->DeviceObject = IrpContext->DeviceExt->StorageDevice;
|
|
|
|
StackPtr->FileObject = NULL;
|
|
|
|
StackPtr->CompletionRoutine = NULL;
|
|
|
|
StackPtr->Parameters.Read.Length = WriteLength;
|
|
|
|
StackPtr->Parameters.Read.ByteOffset = *WriteOffset;
|
|
|
|
|
|
|
|
if (!IoAllocateMdl(Buffer, WriteLength, FALSE, FALSE, Irp))
|
2004-05-15 23:00:02 +00:00
|
|
|
{
|
2013-12-09 10:35:15 +00:00
|
|
|
DPRINT("IoAllocateMdl failed\n");
|
|
|
|
IoFreeIrp(Irp);
|
|
|
|
return STATUS_UNSUCCESSFUL;
|
2004-05-15 23:00:02 +00:00
|
|
|
}
|
|
|
|
|
2013-12-09 10:35:15 +00:00
|
|
|
IoBuildPartialMdl(IrpContext->Irp->MdlAddress, Irp->MdlAddress, Buffer, WriteLength);
|
|
|
|
|
|
|
|
IoSetCompletionRoutine(Irp,
|
|
|
|
VfatReadWritePartialCompletion,
|
|
|
|
IrpContext,
|
|
|
|
TRUE,
|
|
|
|
TRUE,
|
|
|
|
TRUE);
|
2003-12-31 15:07:10 +00:00
|
|
|
|
2013-12-09 10:35:15 +00:00
|
|
|
if (Wait)
|
2000-12-29 23:17:12 +00:00
|
|
|
{
|
2013-12-09 10:35:15 +00:00
|
|
|
KeInitializeEvent(&IrpContext->Event, NotificationEvent, FALSE);
|
|
|
|
IrpContext->RefCount = 1;
|
2004-05-15 23:00:02 +00:00
|
|
|
}
|
2013-12-09 10:35:15 +00:00
|
|
|
else
|
2004-05-15 23:00:02 +00:00
|
|
|
{
|
2013-12-09 10:35:15 +00:00
|
|
|
InterlockedIncrement((PLONG)&IrpContext->RefCount);
|
1999-01-13 15:14:48 +00:00
|
|
|
}
|
|
|
|
|
2013-12-09 10:35:15 +00:00
|
|
|
DPRINT("Calling IO Driver...\n");
|
|
|
|
Status = IoCallDriver(IrpContext->DeviceExt->StorageDevice, Irp);
|
|
|
|
if (Wait && Status == STATUS_PENDING)
|
2000-12-29 23:17:12 +00:00
|
|
|
{
|
2013-12-09 10:35:15 +00:00
|
|
|
KeWaitForSingleObject(&IrpContext->Event, Executive, KernelMode, FALSE, NULL);
|
|
|
|
Status = IrpContext->Irp->IoStatus.Status;
|
1999-01-13 15:14:48 +00:00
|
|
|
}
|
|
|
|
|
2013-12-09 10:35:15 +00:00
|
|
|
if (Status == STATUS_VERIFY_REQUIRED)
|
2011-06-19 20:03:49 +00:00
|
|
|
{
|
2013-12-09 10:35:15 +00:00
|
|
|
PDEVICE_OBJECT DeviceToVerify;
|
2011-06-19 20:03:49 +00:00
|
|
|
|
2013-12-09 10:35:15 +00:00
|
|
|
DPRINT1("Media change detected!\n");
|
2011-06-19 20:03:49 +00:00
|
|
|
|
2013-12-09 10:35:15 +00:00
|
|
|
/* Find the device to verify and reset the thread field to empty value again. */
|
|
|
|
DeviceToVerify = IoGetDeviceToVerify(PsGetCurrentThread());
|
|
|
|
IoSetDeviceToVerify(PsGetCurrentThread(), NULL);
|
|
|
|
Status = IoVerifyVolume(DeviceToVerify,
|
|
|
|
FALSE);
|
|
|
|
if (NT_SUCCESS(Status))
|
2011-06-19 20:03:49 +00:00
|
|
|
{
|
2013-12-09 10:35:15 +00:00
|
|
|
DPRINT1("Volume verification successful; Reissuing write request\n");
|
|
|
|
goto again;
|
2011-06-19 20:03:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-12-09 10:35:15 +00:00
|
|
|
return Status;
|
1999-01-13 15:14:48 +00:00
|
|
|
}
|
2002-03-18 22:37:13 +00:00
|
|
|
|
|
|
|
NTSTATUS
|
2013-12-09 10:35:15 +00:00
|
|
|
VfatBlockDeviceIoControl(
|
|
|
|
IN PDEVICE_OBJECT DeviceObject,
|
|
|
|
IN ULONG CtlCode,
|
|
|
|
IN PVOID InputBuffer OPTIONAL,
|
|
|
|
IN ULONG InputBufferSize,
|
|
|
|
IN OUT PVOID OutputBuffer OPTIONAL,
|
|
|
|
IN OUT PULONG OutputBufferSize,
|
|
|
|
IN BOOLEAN Override)
|
2002-03-18 22:37:13 +00:00
|
|
|
{
|
2013-12-09 10:35:15 +00:00
|
|
|
PIO_STACK_LOCATION Stack;
|
|
|
|
KEVENT Event;
|
|
|
|
PIRP Irp;
|
|
|
|
IO_STATUS_BLOCK IoStatus;
|
|
|
|
NTSTATUS Status;
|
|
|
|
|
|
|
|
DPRINT("VfatBlockDeviceIoControl(DeviceObject %p, CtlCode %x, "
|
|
|
|
"InputBuffer %p, InputBufferSize %x, OutputBuffer %p, "
|
|
|
|
"OutputBufferSize %p (%x)\n", DeviceObject, CtlCode,
|
|
|
|
InputBuffer, InputBufferSize, OutputBuffer, OutputBufferSize,
|
|
|
|
OutputBufferSize ? *OutputBufferSize : 0);
|
2003-11-12 15:26:44 +00:00
|
|
|
|
2011-06-19 20:03:49 +00:00
|
|
|
again:
|
2013-12-09 10:35:15 +00:00
|
|
|
KeInitializeEvent(&Event, NotificationEvent, FALSE);
|
|
|
|
|
|
|
|
DPRINT("Building device I/O control request ...\n");
|
|
|
|
Irp = IoBuildDeviceIoControlRequest(CtlCode,
|
|
|
|
DeviceObject,
|
|
|
|
InputBuffer,
|
|
|
|
InputBufferSize,
|
|
|
|
OutputBuffer,
|
|
|
|
(OutputBufferSize) ? *OutputBufferSize : 0,
|
|
|
|
FALSE,
|
|
|
|
&Event,
|
|
|
|
&IoStatus);
|
|
|
|
if (Irp == NULL)
|
2003-11-12 15:26:44 +00:00
|
|
|
{
|
2013-12-09 10:35:15 +00:00
|
|
|
DPRINT("IoBuildDeviceIoControlRequest failed\n");
|
|
|
|
return STATUS_INSUFFICIENT_RESOURCES;
|
2003-11-12 15:26:44 +00:00
|
|
|
}
|
|
|
|
|
2013-12-09 10:35:15 +00:00
|
|
|
if (Override)
|
2003-11-12 15:26:44 +00:00
|
|
|
{
|
2013-12-09 10:35:15 +00:00
|
|
|
Stack = IoGetNextIrpStackLocation(Irp);
|
|
|
|
Stack->Flags |= SL_OVERRIDE_VERIFY_VOLUME;
|
2003-11-12 15:26:44 +00:00
|
|
|
}
|
|
|
|
|
2013-12-09 10:35:15 +00:00
|
|
|
DPRINT("Calling IO Driver... with irp %p\n", Irp);
|
|
|
|
Status = IoCallDriver(DeviceObject, Irp);
|
2003-11-12 15:26:44 +00:00
|
|
|
|
2013-12-09 10:35:15 +00:00
|
|
|
DPRINT("Waiting for IO Operation for %p\n", Irp);
|
|
|
|
if (Status == STATUS_PENDING)
|
2003-11-12 15:26:44 +00:00
|
|
|
{
|
2013-12-09 10:35:15 +00:00
|
|
|
DPRINT("Operation pending\n");
|
|
|
|
KeWaitForSingleObject (&Event, Suspended, KernelMode, FALSE, NULL);
|
|
|
|
DPRINT("Getting IO Status... for %p\n", Irp);
|
2003-11-12 15:26:44 +00:00
|
|
|
|
2013-12-09 10:35:15 +00:00
|
|
|
Status = IoStatus.Status;
|
2003-11-12 15:26:44 +00:00
|
|
|
}
|
2013-12-09 10:35:15 +00:00
|
|
|
|
|
|
|
if (Status == STATUS_VERIFY_REQUIRED)
|
2011-06-19 20:03:49 +00:00
|
|
|
{
|
2013-12-09 10:35:15 +00:00
|
|
|
PDEVICE_OBJECT DeviceToVerify;
|
2011-06-19 20:03:49 +00:00
|
|
|
|
2013-12-09 10:35:15 +00:00
|
|
|
DPRINT1("Media change detected!\n");
|
2011-06-19 20:03:49 +00:00
|
|
|
|
2013-12-09 10:35:15 +00:00
|
|
|
/* Find the device to verify and reset the thread field to empty value again. */
|
|
|
|
DeviceToVerify = IoGetDeviceToVerify(PsGetCurrentThread());
|
|
|
|
IoSetDeviceToVerify(PsGetCurrentThread(), NULL);
|
|
|
|
Status = IoVerifyVolume(DeviceToVerify,
|
|
|
|
FALSE);
|
2011-06-19 20:03:49 +00:00
|
|
|
|
2013-12-09 10:35:15 +00:00
|
|
|
if (NT_SUCCESS(Status))
|
2011-06-19 20:03:49 +00:00
|
|
|
{
|
2013-12-09 10:35:15 +00:00
|
|
|
DPRINT1("Volume verification successful; Reissuing IOCTL request\n");
|
|
|
|
goto again;
|
2011-06-19 20:03:49 +00:00
|
|
|
}
|
|
|
|
}
|
2003-11-12 15:26:44 +00:00
|
|
|
|
2013-12-09 10:35:15 +00:00
|
|
|
if (OutputBufferSize)
|
2003-11-12 15:26:44 +00:00
|
|
|
{
|
2013-12-09 10:35:15 +00:00
|
|
|
*OutputBufferSize = IoStatus.Information;
|
2003-11-12 15:26:44 +00:00
|
|
|
}
|
|
|
|
|
2013-12-09 10:35:15 +00:00
|
|
|
DPRINT("Returning Status %x\n", Status);
|
2003-11-12 15:26:44 +00:00
|
|
|
|
2013-12-09 10:35:15 +00:00
|
|
|
return Status;
|
2002-03-18 22:37:13 +00:00
|
|
|
}
|