Something went wrong. Try again.
Reactos
Something went wrong. Try again.
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928/** PROJECT: Filesystem Filter Manager* LICENSE: GPL - See COPYING in the top level directory* FILE: drivers/filters/fltmgr/interface.c* PURPOSE: Implements the driver interface* PROGRAMMERS: Ged Murphy (gedmurphy@reactos.org)*/
/* INCLUDES ******************************************************************/
#include "fltmgr.h"#include "fltmgrint.h"
//#define NDEBUG#include <debug.h>
/* DATA *********************************************************************/
#define VALID_FAST_IO_DISPATCH_HANDLER(_FastIoDispatchPtr, _FieldName) \ (((_FastIoDispatchPtr) != NULL) && \ (((_FastIoDispatchPtr)->SizeOfFastIoDispatch) >= \ (FIELD_OFFSET(FAST_IO_DISPATCH, _FieldName) + sizeof(void *))) && \ ((_FastIoDispatchPtr)->_FieldName != NULL))
#define IS_MY_DEVICE_OBJECT(_devObj) \ (((_devObj) != NULL) && \ ((_devObj)->DriverObject == Dispatcher::DriverObject) && \ ((_devObj)->DeviceExtension != NULL))
extern PDEVICE_OBJECT CommsDeviceObject;extern LIST_ENTRY FilterList;extern ERESOURCE FilterListLock;
DRIVER_DATA DriverData;
typedef struct _DETACH_DEVICE_WORK_ITEM{ WORK_QUEUE_ITEM WorkItem; PDEVICE_OBJECT SourceDevice; PDEVICE_OBJECT TargetDevice;
} DETACH_DEVICE_WORK_ITEM, *PDETACH_DEVICE_WORK_ITEM;
/* LOCAL FUNCTIONS ****************************************/
staticVOIDFltpCleanupDeviceObject(_In_ PDEVICE_OBJECT DeviceObject){ PFLTMGR_DEVICE_EXTENSION DeviceExtension;
DeviceExtension = DeviceObject->DeviceExtension; if (DeviceExtension) { // cleanup device extension }}
CODE_SEG("PAGE")staticNTSTATUSFltpAttachDeviceObject(_In_ PDEVICE_OBJECT SourceDevice, _In_ PDEVICE_OBJECT TargetDevice, _Out_ PDEVICE_OBJECT *AttachedToDeviceObject){ NTSTATUS Status;
PAGED_CODE();
/* Before attaching, copy the flags from the device we're going to attach to */ if (FlagOn(SourceDevice->Flags, DO_BUFFERED_IO)) { SetFlag(TargetDevice->Flags, DO_BUFFERED_IO); } if (FlagOn(SourceDevice->Flags, DO_DIRECT_IO)) { SetFlag(TargetDevice->Flags, DO_DIRECT_IO); } if (FlagOn(SourceDevice->Flags, DO_SYSTEM_BOOT_PARTITION)) { SetFlag(TargetDevice->Characteristics, FILE_DEVICE_SECURE_OPEN); }
/* Attach this device to the top of the driver stack */ Status = IoAttachDeviceToDeviceStackSafe(SourceDevice, TargetDevice, AttachedToDeviceObject);
return Status;}
CODE_SEG("PAGE")staticBOOLEANFltpIsAttachedToDevice(_In_ PDEVICE_OBJECT DeviceObject, _In_opt_ PDEVICE_OBJECT *AttachedDeviceObject){ PDEVICE_OBJECT CurrentDeviceObject; PDEVICE_OBJECT NextDeviceObject;
PAGED_CODE();
/* Initialize the return pointer */ if (AttachedDeviceObject) *AttachedDeviceObject = NULL;
/* Start by getting the top level device in the chain */ CurrentDeviceObject = IoGetAttachedDeviceReference(DeviceObject);
/* Loop while there are attached devices */ while (CurrentDeviceObject) { /* Check if this device driver matches ours */ if (CurrentDeviceObject->DriverObject == DriverData.DriverObject) { FLT_ASSERT(CurrentDeviceObject->DeviceExtension != NULL);
/* We're attached, return the device object if the caller asked for it */ if (AttachedDeviceObject) { *AttachedDeviceObject = CurrentDeviceObject; } else { /* We aren't returning the reference, so decrement the count */ ObDereferenceObject(CurrentDeviceObject); }
return TRUE; }
/* Get the next device in the chain */ NextDeviceObject = IoGetLowerDeviceObject(CurrentDeviceObject);
/* Decrement the count on the last device before we update the pointer */ ObDereferenceObject(CurrentDeviceObject); CurrentDeviceObject = NextDeviceObject; }
return FALSE;}
CODE_SEG("PAGE")staticNTSTATUSFltpEnumerateFileSystemVolumes(_In_ PDEVICE_OBJECT DeviceObject){ PFLTMGR_DEVICE_EXTENSION NewDeviceExtension; PDEVICE_OBJECT BaseDeviceObject; PDEVICE_OBJECT NewDeviceObject; PDEVICE_OBJECT *DeviceList; PDEVICE_OBJECT StorageStackDeviceObject; UNICODE_STRING DeviceName; ULONG NumDevices; ULONG i; NTSTATUS Status;
PAGED_CODE();
/* Get the base device */ BaseDeviceObject = IoGetDeviceAttachmentBaseRef(DeviceObject);
/* get the number of device object linked to the base file system */ Status = IoEnumerateDeviceObjectList(BaseDeviceObject->DriverObject, NULL, 0, &NumDevices); if (Status != STATUS_BUFFER_TOO_SMALL) return Status;
/* Add a few more slots in case the size changed between calls and allocate some memory to hold the pointers */ NumDevices += 4; DeviceList = ExAllocatePoolWithTag(NonPagedPool, (NumDevices * sizeof(PDEVICE_OBJECT)), FM_TAG_DEV_OBJ_PTRS); if (DeviceList == NULL) return STATUS_INSUFFICIENT_RESOURCES;
/* Now get all the device objects that this base driver has created */ Status = IoEnumerateDeviceObjectList(BaseDeviceObject->DriverObject, DeviceList, (NumDevices * sizeof(PDEVICE_OBJECT)), &NumDevices); if (!NT_SUCCESS(Status)) { ExFreePoolWithTag(DeviceList, FM_TAG_DEV_OBJ_PTRS); return Status; }
/* Loop through all the devices looking for ones to attach to */ for (i = 0; i < NumDevices; i++) { RtlInitUnicodeString(&DeviceName, NULL); StorageStackDeviceObject = NULL; NewDeviceObject = NULL;
/* Ignore the device we passed in, and devices of the wrong type */ if ((DeviceList[i] == BaseDeviceObject) || (DeviceList[i]->DeviceType != BaseDeviceObject->DeviceType)) { goto CleanupAndNext; }
/* Ignore this device if we're already attached to it */ if (FltpIsAttachedToDevice(DeviceList[i], NULL) == FALSE) { goto CleanupAndNext; }
/* * If the device has a name, it must be a control device. * This handles drivers with more then one control device (like FastFat) */ FltpGetBaseDeviceObjectName(DeviceList[i], &DeviceName); if (NT_SUCCESS(Status) && DeviceName.Length > 0) { goto CleanupAndNext; }
/* * Try to get the storage stack (disk) device object associated with * this file system device object. Ignore the device if we don't have one */ Status = IoGetDiskDeviceObject(DeviceList[i], &StorageStackDeviceObject); if (!NT_SUCCESS(Status)) { goto CleanupAndNext; }
/* * TODO: Don't attach to shadow copy volumes, * ros doesn't have any so it's not an issues yet */
/* * We're far enough to be ready to attach, create a device * object which we'll use to do so */ Status = IoCreateDevice(DriverData.DriverObject, sizeof(FLTMGR_DEVICE_EXTENSION), NULL, DeviceList[i]->DeviceType, 0, FALSE, &NewDeviceObject); if (!NT_SUCCESS(Status)) { goto CleanupAndNext; }
/* Get the device extension for this new object and store our disk object there */ NewDeviceExtension = NewDeviceObject->DeviceExtension; NewDeviceExtension->StorageStackDeviceObject = StorageStackDeviceObject;
/* Lookup and store the device name for the storage stack */ RtlInitEmptyUnicodeString(&NewDeviceExtension->DeviceName, NewDeviceExtension->DeviceNameBuffer, sizeof(NewDeviceExtension->DeviceNameBuffer)); FltpGetObjectName(StorageStackDeviceObject, &NewDeviceExtension->DeviceName);
/* Grab the attach lock before we attempt to attach */ ExAcquireFastMutex(&DriverData.FilterAttachLock);
/* Check again that we aren't already attached. It may have changed since our last check */ if (FltpIsAttachedToDevice(DeviceList[i], NULL) == FALSE) { FLT_ASSERT(NewDeviceObject->DriverObject == DriverData.DriverObject);
/* Finally, attach to the volume */ Status = FltpAttachDeviceObject(DeviceList[i], NewDeviceObject, &NewDeviceExtension->AttachedToDeviceObject); if (NT_SUCCESS(Status)) { /* Clean the initializing flag so other filters can attach to our device object */ ClearFlag(NewDeviceObject->Flags, DO_DEVICE_INITIALIZING); } } else { /* We're already attached. Just cleanup */ Status = STATUS_DEVICE_ALREADY_ATTACHED; }
ExReleaseFastMutex(&DriverData.FilterAttachLock);
CleanupAndNext:
if (!NT_SUCCESS(Status)) { if (NewDeviceObject) { FltpCleanupDeviceObject(NewDeviceObject); IoDeleteDevice(NewDeviceObject); } }
if (StorageStackDeviceObject) { /* A ref was added for us when we attached, so we can deref ours now */ ObDereferenceObject(StorageStackDeviceObject); }
/* Remove the ref which was added by IoEnumerateDeviceObjectList */ ObDereferenceObject(DeviceList[i]);
/* Free the buffer that FltpGetBaseDeviceObjectName added */ FltpFreeUnicodeString(&DeviceName);
}
/* Free the memory we allocated for the list */ ExFreePoolWithTag(DeviceList, FM_TAG_DEV_OBJ_PTRS);
return STATUS_SUCCESS;}
CODE_SEG("PAGE")staticNTSTATUSFltpAttachToFileSystemDevice(_In_ PDEVICE_OBJECT DeviceObject, _In_ PUNICODE_STRING DeviceName){ PFLTMGR_DEVICE_EXTENSION DeviceExtension; PDEVICE_OBJECT NewDeviceObject; WCHAR Buffer[MAX_DEVNAME_LENGTH]; UNICODE_STRING FileSystemDeviceName; UNICODE_STRING FsRecDeviceName; NTSTATUS Status;
PAGED_CODE();
/* Only handle device types we're interested in */ if (DeviceObject->DeviceType != FILE_DEVICE_DISK_FILE_SYSTEM && DeviceObject->DeviceType != FILE_DEVICE_CD_ROM_FILE_SYSTEM && DeviceObject->DeviceType != FILE_DEVICE_NETWORK_FILE_SYSTEM) { return STATUS_SUCCESS; }
/* Setup the buffer to hold the device name */ RtlInitEmptyUnicodeString(&FileSystemDeviceName, Buffer, MAX_DEVNAME_LENGTH * sizeof(WCHAR));
/* Get the the name of the file system device */ Status = FltpGetObjectName(DeviceObject->DriverObject, &FileSystemDeviceName); if (!NT_SUCCESS(Status)) return Status;
DPRINT("Found device %wZ, checking if we need to attach...\n", &FileSystemDeviceName);
/* Build up the name of the file system recognizer device */ RtlInitUnicodeString(&FsRecDeviceName, L"\\FileSystem\\Fs_Rec");
/* We don't attach to recognizer devices, so bail if this is one */ if (RtlCompareUnicodeString(&FileSystemDeviceName, &FsRecDeviceName, TRUE) == 0) { return STATUS_SUCCESS; }
/* Create a device object which we can attach to this file system */ Status = IoCreateDevice(DriverData.DriverObject, sizeof(FLTMGR_DEVICE_EXTENSION), NULL, DeviceObject->DeviceType, 0, FALSE, &NewDeviceObject); if (!NT_SUCCESS(Status)) { DPRINT1("Failed to create a DO for attaching to a FS : 0x%X\n", Status); return Status; }
/* Cast the device extension to something we understand */ DeviceExtension = NewDeviceObject->DeviceExtension;
/* Attach this device to the top of the driver stack and store the DO we attached to in the DE */ Status = FltpAttachDeviceObject(NewDeviceObject, DeviceObject, &DeviceExtension->AttachedToDeviceObject); if (NT_SUCCESS(Status)) { DPRINT("Attached to %wZ\n", &FileSystemDeviceName); } else { DPRINT1("Failed to attach to the driver stack : 0x%X\n", Status); goto Cleanup; }
/* Setup the unicode string buffer and copy the device name to the device extension */ RtlInitEmptyUnicodeString(&DeviceExtension->DeviceName, DeviceExtension->DeviceNameBuffer, MAX_DEVNAME_LENGTH * sizeof(WCHAR)); RtlCopyUnicodeString(&DeviceExtension->DeviceName, DeviceName);
/* We're done, remove the initializing flag */ ClearFlag(NewDeviceObject->Flags, DO_DEVICE_INITIALIZING);
/* Look for existing mounted devices for this file system */ Status = FltpEnumerateFileSystemVolumes(DeviceObject); if (!NT_SUCCESS(Status)) { DPRINT1("Failed to enumerate file system volumes for this file system : 0x%X\n", Status); IoDetachDevice(DeviceExtension->AttachedToDeviceObject); }
Cleanup:
if (!NT_SUCCESS(Status)) { IoDeleteDevice(NewDeviceObject); }
return Status;}
CODE_SEG("PAGE")staticLONG_PTRFltpDetachFromFileSystemDevice(_In_ PDEVICE_OBJECT DeviceObject){ PDEVICE_OBJECT AttachedDevice, NextDevice; PFLTMGR_DEVICE_EXTENSION DeviceExtension; LONG_PTR Count;
PAGED_CODE();
/* Get the top device in the chain and increment the ref count on it */ AttachedDevice = IoGetAttachedDeviceReference(DeviceObject);
/* Loop all attached devices looking for our file system driver */ while (AttachedDevice->DriverObject != DriverData.DriverObject) { FLT_ASSERT(AttachedDevice != NULL);
/* Get the next lower device object. This adds a ref on NextDevice */ NextDevice = IoGetLowerDeviceObject(AttachedDevice);
/* Remove the reference we added */ Count = ObDereferenceObject(AttachedDevice);
/* Bail if this is the last one */ if (NextDevice == NULL) return Count;
/* Try the next one */ AttachedDevice = NextDevice; }
DeviceExtension = AttachedDevice->DeviceExtension; if (DeviceExtension) { // // FIXME: Put any device extension cleanup code here // }
/* Detach the device from the chain and delete the object */ IoDetachDevice(DeviceObject); IoDeleteDevice(AttachedDevice);
/* Remove the reference we added so the delete can complete */ return ObDereferenceObject(AttachedDevice);}
/* DISPATCH ROUTINES **********************************************/
NTSTATUSNTAPIFltpPreFsFilterOperation(_In_ PFS_FILTER_CALLBACK_DATA Data, _Out_ PVOID *CompletionContext){ UNREFERENCED_PARAMETER(Data); UNREFERENCED_PARAMETER(CompletionContext); __debugbreak(); return STATUS_SUCCESS;}
VOIDNTAPIFltpPostFsFilterOperation(_In_ PFS_FILTER_CALLBACK_DATA Data, _In_ NTSTATUS OperationStatus, _In_ PVOID CompletionContext){ UNREFERENCED_PARAMETER(Data); UNREFERENCED_PARAMETER(OperationStatus); UNREFERENCED_PARAMETER(CompletionContext); __debugbreak();}
NTSTATUSNTAPIFltpDispatch(_In_ PDEVICE_OBJECT DeviceObject, _Inout_ PIRP Irp){ PFLTMGR_DEVICE_EXTENSION DeviceExtension; PIO_STACK_LOCATION StackPtr; NTSTATUS Status;
DeviceExtension = DeviceObject->DeviceExtension;
/* Check if this is a request for us */ if (DeviceObject == DriverData.DeviceObject) { FLT_ASSERT(DeviceObject->DriverObject == DriverData.DriverObject); FLT_ASSERT(DeviceExtension == NULL);
/* Hand it off to our internal handler */ Status = FltpDispatchHandler(DeviceObject, Irp); if (Status != STATUS_REPARSE) { Irp->IoStatus.Status = Status; Irp->IoStatus.Information = 0; IoCompleteRequest(Irp, 0); } return Status; }
/* Check if this is a request for a the messaging device */ if (DeviceObject == CommsDeviceObject) { /* Hand off to our internal routine */ return FltpMsgDispatch(DeviceObject, Irp); }
FLT_ASSERT(DeviceExtension && DeviceExtension->AttachedToDeviceObject);
StackPtr = IoGetCurrentIrpStackLocation(Irp); if (StackPtr->MajorFunction == IRP_MJ_SHUTDOWN) { // handle shutdown request }
DPRINT1("Received %X from %wZ\n", StackPtr->MajorFunction, &DeviceExtension->DeviceName);
/* Just pass the IRP down the stack */ IoSkipCurrentIrpStackLocation(Irp); return IoCallDriver(DeviceExtension->AttachedToDeviceObject, Irp);}
CODE_SEG("PAGE")NTSTATUSNTAPIFltpCreate(_In_ PDEVICE_OBJECT DeviceObject, _Inout_ PIRP Irp){ PFLTMGR_DEVICE_EXTENSION DeviceExtension;
PAGED_CODE();
DeviceExtension = DeviceObject->DeviceExtension;
/* Check if this is a request for us */ if (DeviceObject == DriverData.DeviceObject) { FLT_ASSERT(DeviceObject->DriverObject == DriverData.DriverObject); FLT_ASSERT(DeviceExtension == NULL);
/* Someone wants a handle to the fltmgr, allow it */ Irp->IoStatus.Status = STATUS_SUCCESS; Irp->IoStatus.Information = 0; IofCompleteRequest(Irp, 0); return STATUS_SUCCESS; }
/* Check if this is a request for a the new comms connection */ if (DeviceObject == CommsDeviceObject) { /* Hand off to our internal routine */ return FltpMsgCreate(DeviceObject, Irp); }
FLT_ASSERT(DeviceExtension && DeviceExtension->AttachedToDeviceObject);
DPRINT1("Received create from %wZ (%lu)\n", &DeviceExtension->DeviceName, PsGetCurrentProcessId());
/* Just pass the IRP down the stack */ IoSkipCurrentIrpStackLocation(Irp); return IoCallDriver(DeviceExtension->AttachedToDeviceObject, Irp);}
CODE_SEG("PAGE")NTSTATUSNTAPIFltpFsControl(_In_ PDEVICE_OBJECT DeviceObject, _Inout_ PIRP Irp){ PFLTMGR_DEVICE_EXTENSION DeviceExtension;
PAGED_CODE();
/* Check if this is a request for us */ if (DeviceObject == DriverData.DeviceObject) { /* We don't handle this request */ Irp->IoStatus.Information = 0; Irp->IoStatus.Status = STATUS_INVALID_DEVICE_REQUEST; IofCompleteRequest(Irp, 0); return STATUS_INVALID_DEVICE_REQUEST; }
DeviceExtension = DeviceObject->DeviceExtension;
FLT_ASSERT(DeviceExtension && DeviceExtension->AttachedToDeviceObject);
/* Just pass the IRP down the stack */ IoSkipCurrentIrpStackLocation(Irp); return IoCallDriver(DeviceExtension->AttachedToDeviceObject, Irp);}
NTSTATUSNTAPIFltpDeviceControl(_In_ PDEVICE_OBJECT DeviceObject, _Inout_ PIRP Irp){ PFLTMGR_DEVICE_EXTENSION DeviceExtension; NTSTATUS Status;
/* Check if the request was meant for us */ if (DeviceObject == DriverData.DeviceObject) { Status = FltpDeviceControlHandler(DeviceObject, Irp); if (Status != STATUS_REPARSE) { Irp->IoStatus.Status = Status; Irp->IoStatus.Information = 0; IoCompleteRequest(Irp, 0); }
return Status; }
DeviceExtension = DeviceObject->DeviceExtension;
FLT_ASSERT(DeviceExtension && DeviceExtension->AttachedToDeviceObject);
/* Just pass the IRP down the stack */ IoSkipCurrentIrpStackLocation(Irp); return IoCallDriver(DeviceExtension->AttachedToDeviceObject, Irp);}
/* FASTIO ROUTINES ************************************************/
CODE_SEG("PAGE")BOOLEANNTAPIFltpFastIoCheckIfPossible(_In_ PFILE_OBJECT FileObject, _In_ PLARGE_INTEGER FileOffset, _In_ ULONG Length, _In_ BOOLEAN Wait, _In_ ULONG LockKey, _In_ BOOLEAN CheckForReadOperation, _Out_ PIO_STATUS_BLOCK IoStatus, _In_ PDEVICE_OBJECT DeviceObject)
{ PFLTMGR_DEVICE_EXTENSION DeviceExtension; PDEVICE_OBJECT AttachedDeviceObject; PFAST_IO_DISPATCH FastIoDispatch;
PAGED_CODE();
/* If it doesn't have a device extension, then it's not our device object */ if (DeviceObject->DeviceExtension == NULL) { /* Fail the call */ IoStatus->Status = STATUS_INVALID_DEVICE_REQUEST; IoStatus->Information = 0; return TRUE; }
DeviceExtension = DeviceObject->DeviceExtension; FLT_ASSERT(DeviceExtension->AttachedToDeviceObject);
/* Get the device that we attached to */ AttachedDeviceObject = DeviceExtension->AttachedToDeviceObject; FastIoDispatch = AttachedDeviceObject->DriverObject->FastIoDispatch;
/* Make sure our FastIo table is valid */ if (FastIoDispatch && FastIoDispatch->FastIoCheckIfPossible) { /* Forward the call onto the device we attached to */ return FastIoDispatch->FastIoCheckIfPossible(FileObject, FileOffset, Length, Wait, LockKey, CheckForReadOperation, IoStatus, AttachedDeviceObject); }
/* We failed to handle the request, send it down the slow path */ FLT_ASSERT(FALSE); return FALSE;}
CODE_SEG("PAGE")BOOLEANNTAPIFltpFastIoRead(_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){ PFLTMGR_DEVICE_EXTENSION DeviceExtension; PDEVICE_OBJECT AttachedDeviceObject; PFAST_IO_DISPATCH FastIoDispatch;
PAGED_CODE();
/* If it doesn't have a device extension, then it's not our device object */ if (DeviceObject->DeviceExtension == NULL) { /* Fail the call */ IoStatus->Status = STATUS_INVALID_DEVICE_REQUEST; IoStatus->Information = 0; return TRUE; }
DeviceExtension = DeviceObject->DeviceExtension; FLT_ASSERT(DeviceExtension->AttachedToDeviceObject);
/* Get the device that we attached to */ AttachedDeviceObject = DeviceExtension->AttachedToDeviceObject; FastIoDispatch = AttachedDeviceObject->DriverObject->FastIoDispatch;
/* Make sure our FastIo table is valid */ if (FastIoDispatch && FastIoDispatch->FastIoRead) { /* Forward the call onto the device we attached to */ return FastIoDispatch->FastIoRead(FileObject, FileOffset, Length, Wait, LockKey, Buffer, IoStatus, AttachedDeviceObject); }
/* We failed to handle the request, send it down the slow path */ FLT_ASSERT(FALSE); return FALSE;}
CODE_SEG("PAGE")BOOLEANNTAPIFltpFastIoWrite(_In_ PFILE_OBJECT FileObject, _In_ PLARGE_INTEGER FileOffset, _In_ ULONG Length, _In_ BOOLEAN Wait, _In_ ULONG LockKey, _In_ PVOID Buffer, _Out_ PIO_STATUS_BLOCK IoStatus, _In_ PDEVICE_OBJECT DeviceObject){ PFLTMGR_DEVICE_EXTENSION DeviceExtension; PDEVICE_OBJECT AttachedDeviceObject; PFAST_IO_DISPATCH FastIoDispatch;
PAGED_CODE();
/* If it doesn't have a device extension, then it's not our device object */ if (DeviceObject->DeviceExtension == NULL) { /* Fail the call */ IoStatus->Status = STATUS_INVALID_DEVICE_REQUEST; IoStatus->Information = 0; return TRUE; }
DeviceExtension = DeviceObject->DeviceExtension; FLT_ASSERT(DeviceExtension->AttachedToDeviceObject);
/* Get the device that we attached to */ AttachedDeviceObject = DeviceExtension->AttachedToDeviceObject; FastIoDispatch = AttachedDeviceObject->DriverObject->FastIoDispatch;
/* Make sure our FastIo table is valid */ if (FastIoDispatch && FastIoDispatch->FastIoWrite) { /* Forward the call onto the device we attached to */ return FastIoDispatch->FastIoWrite(FileObject, FileOffset, Length, Wait, LockKey, Buffer, IoStatus, AttachedDeviceObject); }
/* We failed to handle the request, send it down the slow path */ FLT_ASSERT(FALSE); return FALSE;}
CODE_SEG("PAGE")BOOLEANNTAPIFltpFastIoQueryBasicInfo(_In_ PFILE_OBJECT FileObject, _In_ BOOLEAN Wait, _Out_ PFILE_BASIC_INFORMATION Buffer, _Out_ PIO_STATUS_BLOCK IoStatus, _In_ PDEVICE_OBJECT DeviceObject){ PFLTMGR_DEVICE_EXTENSION DeviceExtension; PDEVICE_OBJECT AttachedDeviceObject; PFAST_IO_DISPATCH FastIoDispatch;
PAGED_CODE();
/* If it doesn't have a device extension, then it's not our device object */ if (DeviceObject->DeviceExtension == NULL) { /* Fail the call */ IoStatus->Status = STATUS_INVALID_DEVICE_REQUEST; IoStatus->Information = 0; return TRUE; }
DeviceExtension = DeviceObject->DeviceExtension; FLT_ASSERT(DeviceExtension->AttachedToDeviceObject);
/* Get the device that we attached to */ AttachedDeviceObject = DeviceExtension->AttachedToDeviceObject; FastIoDispatch = AttachedDeviceObject->DriverObject->FastIoDispatch;
/* Make sure our FastIo table is valid */ if (FastIoDispatch && FastIoDispatch->FastIoQueryBasicInfo) { /* Forward the call onto the device we attached to */ return FastIoDispatch->FastIoQueryBasicInfo(FileObject, Wait, Buffer, IoStatus, AttachedDeviceObject); }
/* We failed to handle the request, send it down the slow path */ FLT_ASSERT(FALSE); return FALSE;}
CODE_SEG("PAGE")BOOLEANNTAPIFltpFastIoQueryStandardInfo(_In_ PFILE_OBJECT FileObject, _In_ BOOLEAN Wait, _Out_ PFILE_STANDARD_INFORMATION Buffer, _Out_ PIO_STATUS_BLOCK IoStatus, _In_ PDEVICE_OBJECT DeviceObject){ PFLTMGR_DEVICE_EXTENSION DeviceExtension; PDEVICE_OBJECT AttachedDeviceObject; PFAST_IO_DISPATCH FastIoDispatch;
PAGED_CODE();
/* If it doesn't have a device extension, then it's not our device object */ if (DeviceObject->DeviceExtension == NULL) { /* Fail the call */ IoStatus->Status = STATUS_INVALID_DEVICE_REQUEST; IoStatus->Information = 0; return TRUE; }
DeviceExtension = DeviceObject->DeviceExtension; FLT_ASSERT(DeviceExtension->AttachedToDeviceObject);
/* Get the device that we attached to */ AttachedDeviceObject = DeviceExtension->AttachedToDeviceObject; FastIoDispatch = AttachedDeviceObject->DriverObject->FastIoDispatch;
/* Make sure our FastIo table is valid */ if (FastIoDispatch && FastIoDispatch->FastIoQueryStandardInfo) { /* Forward the call onto the device we attached to */ return FastIoDispatch->FastIoQueryStandardInfo(FileObject, Wait, Buffer, IoStatus, AttachedDeviceObject); }
/* We failed to handle the request, send it down the slow path */ FLT_ASSERT(FALSE); return FALSE;}
CODE_SEG("PAGE")BOOLEANNTAPIFltpFastIoLock(_In_ PFILE_OBJECT FileObject, _In_ PLARGE_INTEGER FileOffset, _In_ PLARGE_INTEGER Length, _In_ PEPROCESS ProcessId, _In_ ULONG Key, _In_ BOOLEAN FailImmediately, _In_ BOOLEAN ExclusiveLock, _Out_ PIO_STATUS_BLOCK IoStatus, _In_ PDEVICE_OBJECT DeviceObject){ PFLTMGR_DEVICE_EXTENSION DeviceExtension; PDEVICE_OBJECT AttachedDeviceObject; PFAST_IO_DISPATCH FastIoDispatch;
PAGED_CODE();
/* If it doesn't have a device extension, then it's not our device object */ if (DeviceObject->DeviceExtension == NULL) { /* Fail the call */ IoStatus->Status = STATUS_INVALID_DEVICE_REQUEST; IoStatus->Information = 0; return TRUE; }
DeviceExtension = DeviceObject->DeviceExtension; FLT_ASSERT(DeviceExtension->AttachedToDeviceObject);
/* Get the device that we attached to */ AttachedDeviceObject = DeviceExtension->AttachedToDeviceObject; FastIoDispatch = AttachedDeviceObject->DriverObject->FastIoDispatch;
/* Make sure our FastIo table is valid */ if (FastIoDispatch && FastIoDispatch->FastIoLock) { /* Forward the call onto the device we attached to */ return FastIoDispatch->FastIoLock(FileObject, FileOffset, Length, ProcessId, Key, FailImmediately, ExclusiveLock, IoStatus, AttachedDeviceObject); }
/* We failed to handle the request, send it down the slow path */ FLT_ASSERT(FALSE); return FALSE;}
CODE_SEG("PAGE")BOOLEANNTAPIFltpFastIoUnlockSingle(_In_ PFILE_OBJECT FileObject, _In_ PLARGE_INTEGER FileOffset, _In_ PLARGE_INTEGER Length, _In_ PEPROCESS ProcessId, _In_ ULONG Key, _Out_ PIO_STATUS_BLOCK IoStatus, _In_ PDEVICE_OBJECT DeviceObject){ PFLTMGR_DEVICE_EXTENSION DeviceExtension; PDEVICE_OBJECT AttachedDeviceObject; PFAST_IO_DISPATCH FastIoDispatch;
PAGED_CODE();
/* If it doesn't have a device extension, then it's not our device object */ if (DeviceObject->DeviceExtension == NULL) { /* Fail the call */ IoStatus->Status = STATUS_INVALID_DEVICE_REQUEST; IoStatus->Information = 0; return TRUE; }
DeviceExtension = DeviceObject->DeviceExtension; FLT_ASSERT(DeviceExtension->AttachedToDeviceObject);
/* Get the device that we attached to */ AttachedDeviceObject = DeviceExtension->AttachedToDeviceObject; FastIoDispatch = AttachedDeviceObject->DriverObject->FastIoDispatch;
/* Make sure our FastIo table is valid */ if (FastIoDispatch && FastIoDispatch->FastIoUnlockSingle) { /* Forward the call onto the device we attached to */ return FastIoDispatch->FastIoUnlockSingle(FileObject, FileOffset, Length, ProcessId, Key, IoStatus, AttachedDeviceObject); }
/* We failed to handle the request, send it down the slow path */ FLT_ASSERT(FALSE); return FALSE;}
CODE_SEG("PAGE")BOOLEANNTAPIFltpFastIoUnlockAll(_In_ PFILE_OBJECT FileObject, _In_ PEPROCESS ProcessId, _Out_ PIO_STATUS_BLOCK IoStatus, _In_ PDEVICE_OBJECT DeviceObject)
{ PFLTMGR_DEVICE_EXTENSION DeviceExtension; PDEVICE_OBJECT AttachedDeviceObject; PFAST_IO_DISPATCH FastIoDispatch;
PAGED_CODE();
/* If it doesn't have a device extension, then it's not our device object */ if (DeviceObject->DeviceExtension == NULL) { /* Fail the call */ IoStatus->Status = STATUS_INVALID_DEVICE_REQUEST; IoStatus->Information = 0; return TRUE; }
DeviceExtension = DeviceObject->DeviceExtension; FLT_ASSERT(DeviceExtension->AttachedToDeviceObject);
/* Get the device that we attached to */ AttachedDeviceObject = DeviceExtension->AttachedToDeviceObject; FastIoDispatch = AttachedDeviceObject->DriverObject->FastIoDispatch;
/* Make sure our FastIo table is valid */ if (FastIoDispatch && FastIoDispatch->FastIoUnlockAll) { /* Forward the call onto the device we attached to */ return FastIoDispatch->FastIoUnlockAll(FileObject, ProcessId, IoStatus, AttachedDeviceObject); }
/* We failed to handle the request, send it down the slow path */ FLT_ASSERT(FALSE); return FALSE;}
CODE_SEG("PAGE")BOOLEANNTAPIFltpFastIoUnlockAllByKey(_In_ PFILE_OBJECT FileObject, _In_ PVOID ProcessId, _In_ ULONG Key, _Out_ PIO_STATUS_BLOCK IoStatus, _In_ PDEVICE_OBJECT DeviceObject){ PFLTMGR_DEVICE_EXTENSION DeviceExtension; PDEVICE_OBJECT AttachedDeviceObject; PFAST_IO_DISPATCH FastIoDispatch;
PAGED_CODE();
/* If it doesn't have a device extension, then it's not our device object */ if (DeviceObject->DeviceExtension == NULL) { /* Fail the call */ IoStatus->Status = STATUS_INVALID_DEVICE_REQUEST; IoStatus->Information = 0; return TRUE; }
DeviceExtension = DeviceObject->DeviceExtension; FLT_ASSERT(DeviceExtension->AttachedToDeviceObject);
/* Get the device that we attached to */ AttachedDeviceObject = DeviceExtension->AttachedToDeviceObject; FastIoDispatch = AttachedDeviceObject->DriverObject->FastIoDispatch;
/* Make sure our FastIo table is valid */ if (FastIoDispatch && FastIoDispatch->FastIoUnlockAllByKey) { /* Forward the call onto the device we attached to */ return FastIoDispatch->FastIoUnlockAllByKey(FileObject, ProcessId, Key, IoStatus, AttachedDeviceObject); }
/* We failed to handle the request, send it down the slow path */ FLT_ASSERT(FALSE); return FALSE;}
CODE_SEG("PAGE")BOOLEANNTAPIFltpFastIoDeviceControl(_In_ PFILE_OBJECT FileObject, _In_ BOOLEAN Wait, _In_opt_ PVOID InputBuffer, _In_ ULONG InputBufferLength, _Out_opt_ PVOID OutputBuffer, _In_ ULONG OutputBufferLength, _In_ ULONG IoControlCode, _Out_ PIO_STATUS_BLOCK IoStatus, _In_ PDEVICE_OBJECT DeviceObject){ PFLTMGR_DEVICE_EXTENSION DeviceExtension; PDEVICE_OBJECT AttachedDeviceObject; PFAST_IO_DISPATCH FastIoDispatch;
PAGED_CODE();
/* If it doesn't have a device extension, then it's not our device object */ if (DeviceObject->DeviceExtension == NULL) { /* Fail the request, send it down the slow path */ return FALSE; }
DeviceExtension = DeviceObject->DeviceExtension; FLT_ASSERT(DeviceExtension->AttachedToDeviceObject);
/* Get the device that we attached to */ AttachedDeviceObject = DeviceExtension->AttachedToDeviceObject; FastIoDispatch = AttachedDeviceObject->DriverObject->FastIoDispatch;
/* Make sure our FastIo table is valid */ if (FastIoDispatch && FastIoDispatch->FastIoDeviceControl) { /* Forward the call onto the device we attached to */ return FastIoDispatch->FastIoDeviceControl(FileObject, Wait, InputBuffer, InputBufferLength, OutputBuffer, OutputBufferLength, IoControlCode, IoStatus, AttachedDeviceObject); }
/* We failed to handle the request, send it down the slow path */ FLT_ASSERT(FALSE); return FALSE;}
VOIDNTAPIFltpFastIoDetachDeviceWorker(_In_ PVOID Parameter){ PDETACH_DEVICE_WORK_ITEM DetachDeviceWorkItem = Parameter;
/* Run any cleanup routines */ FltpCleanupDeviceObject(DetachDeviceWorkItem->SourceDevice);
/* Detach from the target device */ IoDetachDevice(DetachDeviceWorkItem->TargetDevice);
/* Delete the source */ IoDeleteDevice(DetachDeviceWorkItem->SourceDevice);
/* Free the pool we allocated in FltpFastIoDetachDevice */ ExFreePoolWithTag(DetachDeviceWorkItem, 0x1234);}
CODE_SEG("PAGE")VOIDNTAPIFltpFastIoDetachDevice(_In_ PDEVICE_OBJECT SourceDevice, _In_ PDEVICE_OBJECT TargetDevice){ PDETACH_DEVICE_WORK_ITEM DetachDeviceWorkItem;
PAGED_CODE();
/* * Detaching and deleting devices is a lot of work and takes too long * to be a worthwhile FastIo candidate, so we defer this call to speed * it up. There's no return value so we're okay to do this. */
/* Allocate the work item and it's corresponding data */ DetachDeviceWorkItem = ExAllocatePoolWithTag(NonPagedPool, sizeof(DETACH_DEVICE_WORK_ITEM), 0x1234); if (DetachDeviceWorkItem) { /* Initialize the work item */ ExInitializeWorkItem(&DetachDeviceWorkItem->WorkItem, FltpFastIoDetachDeviceWorker, DetachDeviceWorkItem);
/* Queue the work item and return the call */ ExQueueWorkItem(&DetachDeviceWorkItem->WorkItem, DelayedWorkQueue); } else { /* We failed to defer, just cleanup here */ FltpCleanupDeviceObject(SourceDevice); IoDetachDevice(TargetDevice); IoDeleteDevice(SourceDevice); }
}
CODE_SEG("PAGE")BOOLEANNTAPIFltpFastIoQueryNetworkOpenInfo(_In_ PFILE_OBJECT FileObject, _In_ BOOLEAN Wait, _Out_ PFILE_NETWORK_OPEN_INFORMATION Buffer, _Out_ PIO_STATUS_BLOCK IoStatus, _In_ PDEVICE_OBJECT DeviceObject){ PFLTMGR_DEVICE_EXTENSION DeviceExtension; PDEVICE_OBJECT AttachedDeviceObject; PFAST_IO_DISPATCH FastIoDispatch;
PAGED_CODE();
/* If it doesn't have a device extension, then it's not our device object */ if (DeviceObject->DeviceExtension == NULL) { /* Fail the call */ IoStatus->Status = STATUS_INVALID_DEVICE_REQUEST; IoStatus->Information = 0; return TRUE; }
DeviceExtension = DeviceObject->DeviceExtension; FLT_ASSERT(DeviceExtension->AttachedToDeviceObject);
/* Get the device that we attached to */ AttachedDeviceObject = DeviceExtension->AttachedToDeviceObject; FastIoDispatch = AttachedDeviceObject->DriverObject->FastIoDispatch;
/* Make sure our FastIo table is valid */ if (FastIoDispatch && FastIoDispatch->FastIoQueryNetworkOpenInfo) { /* Forward the call onto the device we attached to */ return FastIoDispatch->FastIoQueryNetworkOpenInfo(FileObject, Wait, Buffer, IoStatus, AttachedDeviceObject); }
/* We failed to handle the request, send it down the slow path */ FLT_ASSERT(FALSE); return FALSE;}
CODE_SEG("PAGE")BOOLEANNTAPIFltpFastIoMdlRead(_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){ PFLTMGR_DEVICE_EXTENSION DeviceExtension; PDEVICE_OBJECT AttachedDeviceObject; PFAST_IO_DISPATCH FastIoDispatch;
PAGED_CODE();
/* If it doesn't have a device extension, then it's not our device object */ if (DeviceObject->DeviceExtension == NULL) { /* Fail the call */ IoStatus->Status = STATUS_INVALID_DEVICE_REQUEST; IoStatus->Information = 0; return TRUE; }
DeviceExtension = DeviceObject->DeviceExtension; FLT_ASSERT(DeviceExtension->AttachedToDeviceObject);
/* Get the device that we attached to */ AttachedDeviceObject = DeviceExtension->AttachedToDeviceObject; FastIoDispatch = AttachedDeviceObject->DriverObject->FastIoDispatch;
/* Make sure our FastIo table is valid */ if (FastIoDispatch && FastIoDispatch->MdlRead) { /* Forward the call onto the device we attached to */ return FastIoDispatch->MdlRead(FileObject, FileOffset, Length, LockKey, MdlChain, IoStatus, AttachedDeviceObject); }
/* We failed to handle the request, send it down the slow path */ FLT_ASSERT(FALSE); return FALSE;}
CODE_SEG("PAGE")BOOLEANNTAPIFltpFastIoMdlReadComplete(_In_ PFILE_OBJECT FileObject, _In_ PMDL MdlChain, _In_ PDEVICE_OBJECT DeviceObject)
{ PFLTMGR_DEVICE_EXTENSION DeviceExtension; PDEVICE_OBJECT AttachedDeviceObject; PFAST_IO_DISPATCH FastIoDispatch;
PAGED_CODE();
/* If it doesn't have a device extension, then it's not our device object */ if (DeviceObject->DeviceExtension == NULL) { /* Fail the request, send it down the slow path */ return FALSE; }
DeviceExtension = DeviceObject->DeviceExtension; FLT_ASSERT(DeviceExtension->AttachedToDeviceObject);
/* Get the device that we attached to */ AttachedDeviceObject = DeviceExtension->AttachedToDeviceObject; FastIoDispatch = AttachedDeviceObject->DriverObject->FastIoDispatch;
/* Make sure our FastIo table is valid */ if (FastIoDispatch && FastIoDispatch->MdlReadComplete) { /* Forward the call onto the device we attached to */ return FastIoDispatch->MdlReadComplete(FileObject, MdlChain, AttachedDeviceObject); }
/* We failed to handle the request, send it down the slow path */ FLT_ASSERT(FALSE); return FALSE;}
CODE_SEG("PAGE")BOOLEANNTAPIFltpFastIoPrepareMdlWrite(_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){ PFLTMGR_DEVICE_EXTENSION DeviceExtension; PDEVICE_OBJECT AttachedDeviceObject; PFAST_IO_DISPATCH FastIoDispatch;
PAGED_CODE();
/* If it doesn't have a device extension, then it's not our device object */ if (DeviceObject->DeviceExtension == NULL) { /* Fail the call */ IoStatus->Status = STATUS_INVALID_DEVICE_REQUEST; IoStatus->Information = 0; return TRUE; }
DeviceExtension = DeviceObject->DeviceExtension; FLT_ASSERT(DeviceExtension->AttachedToDeviceObject);
/* Get the device that we attached to */ AttachedDeviceObject = DeviceExtension->AttachedToDeviceObject; FastIoDispatch = AttachedDeviceObject->DriverObject->FastIoDispatch;
/* Make sure our FastIo table is valid */ if (FastIoDispatch && FastIoDispatch->PrepareMdlWrite) { /* Forward the call onto the device we attached to */ return FastIoDispatch->PrepareMdlWrite(FileObject, FileOffset, Length, LockKey, MdlChain, IoStatus, AttachedDeviceObject); }
/* We failed to handle the request, send it down the slow path */ FLT_ASSERT(FALSE); return FALSE;}
CODE_SEG("PAGE")BOOLEANNTAPIFltpFastIoMdlWriteComplete(_In_ PFILE_OBJECT FileObject, _In_ PLARGE_INTEGER FileOffset, _In_ PMDL MdlChain, _In_ PDEVICE_OBJECT DeviceObject){ PFLTMGR_DEVICE_EXTENSION DeviceExtension; PDEVICE_OBJECT AttachedDeviceObject; PFAST_IO_DISPATCH FastIoDispatch;
PAGED_CODE();
/* If it doesn't have a device extension, then it's not our device object */ if (DeviceObject->DeviceExtension == NULL) { /* Fail the request, send it down the slow path */ return FALSE; }
DeviceExtension = DeviceObject->DeviceExtension; FLT_ASSERT(DeviceExtension->AttachedToDeviceObject);
/* Get the device that we attached to */ AttachedDeviceObject = DeviceExtension->AttachedToDeviceObject; FastIoDispatch = AttachedDeviceObject->DriverObject->FastIoDispatch;
/* Make sure our FastIo table is valid */ if (FastIoDispatch && FastIoDispatch->MdlWriteComplete) { /* Forward the call onto the device we attached to */ return FastIoDispatch->MdlWriteComplete(FileObject, FileOffset, MdlChain, AttachedDeviceObject); }
/* We failed to handle the request, send it down the slow path */ FLT_ASSERT(FALSE); return FALSE;}
CODE_SEG("PAGE")BOOLEANNTAPIFltpFastIoReadCompressed(_In_ PFILE_OBJECT FileObject, _In_ PLARGE_INTEGER FileOffset, _In_ ULONG Length, _In_ ULONG LockKey, _Out_ PVOID Buffer, _Out_ PMDL *MdlChain, _Out_ PIO_STATUS_BLOCK IoStatus, _Out_ PCOMPRESSED_DATA_INFO CompressedDataInfo, _In_ ULONG CompressedDataInfoLength, _In_ PDEVICE_OBJECT DeviceObject){ PFLTMGR_DEVICE_EXTENSION DeviceExtension; PDEVICE_OBJECT AttachedDeviceObject; PFAST_IO_DISPATCH FastIoDispatch;
PAGED_CODE();
/* If it doesn't have a device extension, then it's not our device object */ if (DeviceObject->DeviceExtension == NULL) { /* Fail the request, send it down the slow path */ return FALSE; }
DeviceExtension = DeviceObject->DeviceExtension; FLT_ASSERT(DeviceExtension->AttachedToDeviceObject);
/* Get the device that we attached to */ AttachedDeviceObject = DeviceExtension->AttachedToDeviceObject; FastIoDispatch = AttachedDeviceObject->DriverObject->FastIoDispatch;
/* Make sure our FastIo table is valid */ if (FastIoDispatch && FastIoDispatch->FastIoReadCompressed) { /* Forward the call onto the device we attached to */ return FastIoDispatch->FastIoReadCompressed(FileObject, FileOffset, Length, LockKey, Buffer, MdlChain, IoStatus, CompressedDataInfo, CompressedDataInfoLength, AttachedDeviceObject); }
/* We failed to handle the request, send it down the slow path */ FLT_ASSERT(FALSE); return FALSE;}
CODE_SEG("PAGE")BOOLEANNTAPIFltpFastIoWriteCompressed(_In_ PFILE_OBJECT FileObject, _In_ PLARGE_INTEGER FileOffset, _In_ ULONG Length, _In_ ULONG LockKey, _In_ PVOID Buffer, _Out_ PMDL *MdlChain, _Out_ PIO_STATUS_BLOCK IoStatus, _In_ PCOMPRESSED_DATA_INFO CompressedDataInfo, _In_ ULONG CompressedDataInfoLength, _In_ PDEVICE_OBJECT DeviceObject){ PFLTMGR_DEVICE_EXTENSION DeviceExtension; PDEVICE_OBJECT AttachedDeviceObject; PFAST_IO_DISPATCH FastIoDispatch;
PAGED_CODE();
/* If it doesn't have a device extension, then it's not our device object */ if (DeviceObject->DeviceExtension == NULL) { /* Fail the request, send it down the slow path */ return FALSE; }
DeviceExtension = DeviceObject->DeviceExtension; FLT_ASSERT(DeviceExtension->AttachedToDeviceObject);
/* Get the device that we attached to */ AttachedDeviceObject = DeviceExtension->AttachedToDeviceObject; FastIoDispatch = AttachedDeviceObject->DriverObject->FastIoDispatch;
/* Make sure our FastIo table is valid */ if (FastIoDispatch && FastIoDispatch->FastIoWriteCompressed) { /* Forward the call onto the device we attached to */ return FastIoDispatch->FastIoWriteCompressed(FileObject, FileOffset, Length, LockKey, Buffer, MdlChain, IoStatus, CompressedDataInfo, CompressedDataInfoLength, AttachedDeviceObject); }
/* We failed to handle the request, send it down the slow path */ FLT_ASSERT(FALSE); return FALSE;}
CODE_SEG("PAGE")BOOLEANNTAPIFltpFastIoMdlReadCompleteCompressed(_In_ PFILE_OBJECT FileObject, _In_ PMDL MdlChain, _In_ PDEVICE_OBJECT DeviceObject){ PFLTMGR_DEVICE_EXTENSION DeviceExtension; PDEVICE_OBJECT AttachedDeviceObject; PFAST_IO_DISPATCH FastIoDispatch;
PAGED_CODE();
/* If it doesn't have a device extension, then it's not our device object */ if (DeviceObject->DeviceExtension == NULL) { return FALSE; }
DeviceExtension = DeviceObject->DeviceExtension; FLT_ASSERT(DeviceExtension->AttachedToDeviceObject);
/* Get the device that we attached to */ AttachedDeviceObject = DeviceExtension->AttachedToDeviceObject; FastIoDispatch = AttachedDeviceObject->DriverObject->FastIoDispatch;
/* Make sure our FastIo table is valid */ if (FastIoDispatch && FastIoDispatch->MdlReadCompleteCompressed) { /* Forward the call onto the device we attached to */ return FastIoDispatch->MdlReadCompleteCompressed(FileObject, MdlChain, AttachedDeviceObject); }
/* We failed to handle the request, send it down the slow path */ FLT_ASSERT(FALSE); return FALSE;}
CODE_SEG("PAGE")BOOLEANNTAPIFltpFastIoMdlWriteCompleteCompressed(_In_ PFILE_OBJECT FileObject, _In_ PLARGE_INTEGER FileOffset, _In_ PMDL MdlChain, _In_ PDEVICE_OBJECT DeviceObject){ PFLTMGR_DEVICE_EXTENSION DeviceExtension; PDEVICE_OBJECT AttachedDeviceObject; PFAST_IO_DISPATCH FastIoDispatch;
PAGED_CODE();
/* If it doesn't have a device extension, then it's not our device object */ if (DeviceObject->DeviceExtension == NULL) { return FALSE; }
DeviceExtension = DeviceObject->DeviceExtension; FLT_ASSERT(DeviceExtension->AttachedToDeviceObject);
/* Get the device that we attached to */ AttachedDeviceObject = DeviceExtension->AttachedToDeviceObject; FastIoDispatch = AttachedDeviceObject->DriverObject->FastIoDispatch;
/* Make sure our FastIo table is valid */ if (FastIoDispatch && FastIoDispatch->MdlWriteCompleteCompressed) { /* Forward the call onto the device we attached to */ return FastIoDispatch->MdlWriteCompleteCompressed(FileObject, FileOffset, MdlChain, AttachedDeviceObject); }
/* We failed to handle the request, send it down the slow path */ FLT_ASSERT(FALSE); return FALSE;}
CODE_SEG("PAGE")BOOLEANNTAPIFltpFastIoQueryOpen(_Inout_ PIRP Irp, _Out_ PFILE_NETWORK_OPEN_INFORMATION NetworkInformation, _In_ PDEVICE_OBJECT DeviceObject){ PFLTMGR_DEVICE_EXTENSION DeviceExtension; PDEVICE_OBJECT AttachedDeviceObject; PFAST_IO_DISPATCH FastIoDispatch; BOOLEAN Success;
PAGED_CODE();
/* If it doesn't have a device extension, then it's not our device object */ if (DeviceObject->DeviceExtension == NULL) { return FALSE; }
DeviceExtension = DeviceObject->DeviceExtension; FLT_ASSERT(DeviceExtension->AttachedToDeviceObject);
/* Get the device that we attached to */ AttachedDeviceObject = DeviceExtension->AttachedToDeviceObject; FastIoDispatch = AttachedDeviceObject->DriverObject->FastIoDispatch;
/* Make sure our FastIo table is valid */ if (FastIoDispatch && FastIoDispatch->FastIoQueryOpen) { PIO_STACK_LOCATION StackPtr = IoGetCurrentIrpStackLocation(Irp);
/* Update the stack to contain the correct device for the next filter */ StackPtr->DeviceObject = AttachedDeviceObject;
/* Now forward the call */ Success = FastIoDispatch->FastIoQueryOpen(Irp, NetworkInformation, AttachedDeviceObject);
/* Restore the DeviceObject as we found it */ StackPtr->DeviceObject = DeviceObject; return Success; }
/* We failed to handle the request, send it down the slow path */ FLT_ASSERT(FALSE); return FALSE;}
CODE_SEG("PAGE")DRIVER_FS_NOTIFICATION FltpFsNotification;
CODE_SEG("PAGE")VOIDNTAPIFltpFsNotification(_In_ PDEVICE_OBJECT DeviceObject, _In_ BOOLEAN FsActive){ UNICODE_STRING DeviceName; NTSTATUS Status;
PAGED_CODE();
/* Set an empty string */ RtlInitUnicodeString(&DeviceName, NULL);
/* Get the name of the lowest device object on the stack */ Status = FltpGetBaseDeviceObjectName(DeviceObject, &DeviceName); if (NT_SUCCESS(Status)) { /* Check if it's attaching or detaching */ if (FsActive) { /* Run the attach routine */ FltpAttachToFileSystemDevice(DeviceObject, &DeviceName); } else { /* Run the detach routine */ FltpDetachFromFileSystemDevice(DeviceObject); }
/* Free the buffer which FltpGetBaseDeviceObjectName allocated */ FltpFreeUnicodeString(&DeviceName); }}
staticCODE_SEG("INIT")NTSTATUSSetupDispatchAndCallbacksTables(_In_ PDRIVER_OBJECT DriverObject){ PFAST_IO_DISPATCH FastIoDispatch; FS_FILTER_CALLBACKS Callbacks; ULONG i;
/* Plug all the IRPs */ for (i = 0; i <= IRP_MJ_MAXIMUM_FUNCTION; i++) { DriverObject->MajorFunction[i] = FltpDispatch; }
/* Override the ones we're interested in */ DriverObject->MajorFunction[IRP_MJ_CREATE] = FltpCreate; DriverObject->MajorFunction[IRP_MJ_CREATE_NAMED_PIPE] = FltpCreate; DriverObject->MajorFunction[IRP_MJ_CREATE_MAILSLOT] = FltpCreate; DriverObject->MajorFunction[IRP_MJ_FILE_SYSTEM_CONTROL] = FltpFsControl; DriverObject->MajorFunction[IRP_MJ_DEVICE_CONTROL] = FltpDeviceControl;
/* The FastIo dispatch table is stored in the pool along with a tag */ FastIoDispatch = ExAllocatePoolWithTag(NonPagedPool, sizeof(FAST_IO_DISPATCH), FM_TAG_DISPATCH_TABLE); if (FastIoDispatch == NULL) return STATUS_INSUFFICIENT_RESOURCES;
/* Fill out the FastIo table */ RtlZeroMemory(FastIoDispatch, sizeof(FAST_IO_DISPATCH)); FastIoDispatch->SizeOfFastIoDispatch = sizeof(FAST_IO_DISPATCH); FastIoDispatch->FastIoCheckIfPossible = FltpFastIoCheckIfPossible; FastIoDispatch->FastIoRead = FltpFastIoRead; FastIoDispatch->FastIoWrite = FltpFastIoWrite; FastIoDispatch->FastIoQueryBasicInfo = FltpFastIoQueryBasicInfo; FastIoDispatch->FastIoQueryStandardInfo = FltpFastIoQueryStandardInfo; FastIoDispatch->FastIoLock = FltpFastIoLock; FastIoDispatch->FastIoUnlockSingle = FltpFastIoUnlockSingle; FastIoDispatch->FastIoUnlockAll = FltpFastIoUnlockAll; FastIoDispatch->FastIoUnlockAllByKey = FltpFastIoUnlockAllByKey; FastIoDispatch->FastIoDeviceControl = FltpFastIoDeviceControl; FastIoDispatch->FastIoDetachDevice = FltpFastIoDetachDevice; FastIoDispatch->FastIoQueryNetworkOpenInfo = FltpFastIoQueryNetworkOpenInfo; FastIoDispatch->MdlRead = FltpFastIoMdlRead; FastIoDispatch->MdlReadComplete = FltpFastIoMdlReadComplete; FastIoDispatch->PrepareMdlWrite = FltpFastIoPrepareMdlWrite; FastIoDispatch->MdlWriteComplete = FltpFastIoMdlWriteComplete; FastIoDispatch->FastIoReadCompressed = FltpFastIoReadCompressed; FastIoDispatch->FastIoWriteCompressed = FltpFastIoWriteCompressed; FastIoDispatch->MdlReadCompleteCompressed = FltpFastIoMdlReadCompleteCompressed; FastIoDispatch->MdlWriteCompleteCompressed = FltpFastIoMdlWriteCompleteCompressed; FastIoDispatch->FastIoQueryOpen = FltpFastIoQueryOpen;
/* Store the FastIo table for internal and our access */ DriverObject->FastIoDispatch = FastIoDispatch; DriverData.FastIoDispatch = FastIoDispatch;
/* Initialize the callback table */ Callbacks.SizeOfFsFilterCallbacks = sizeof(FS_FILTER_CALLBACKS); Callbacks.PreAcquireForSectionSynchronization = FltpPreFsFilterOperation; Callbacks.PostAcquireForSectionSynchronization = FltpPostFsFilterOperation; Callbacks.PreReleaseForSectionSynchronization = FltpPreFsFilterOperation; Callbacks.PostReleaseForSectionSynchronization = FltpPostFsFilterOperation; Callbacks.PreAcquireForCcFlush = FltpPreFsFilterOperation; Callbacks.PostAcquireForCcFlush = FltpPostFsFilterOperation; Callbacks.PreReleaseForCcFlush = FltpPreFsFilterOperation; Callbacks.PostReleaseForCcFlush = FltpPostFsFilterOperation; Callbacks.PreAcquireForModifiedPageWriter = FltpPreFsFilterOperation; Callbacks.PostAcquireForModifiedPageWriter = FltpPostFsFilterOperation; Callbacks.PreReleaseForModifiedPageWriter = FltpPreFsFilterOperation; Callbacks.PostReleaseForModifiedPageWriter = FltpPostFsFilterOperation;
/* Register our callbacks */ return FsRtlRegisterFileSystemFilterCallbacks(DriverObject, &Callbacks);}
CODE_SEG("INIT") DRIVER_INITIALIZE DriverEntry;
CODE_SEG("INIT")NTSTATUSNTAPIDriverEntry(_In_ PDRIVER_OBJECT DriverObject, _In_ PUNICODE_STRING RegistryPath){ UNICODE_STRING DeviceName = RTL_CONSTANT_STRING(L"\\FileSystem\\Filters\\"DRIVER_NAME); PDEVICE_OBJECT RawDeviceObject; PDEVICE_OBJECT DeviceObject; PFILE_OBJECT RawFileObject; UNICODE_STRING ObjectName; UNICODE_STRING SymLink;
NTSTATUS Status;
RtlZeroMemory(&DriverData, sizeof(DRIVER_DATA)); DriverData.DriverObject = DriverObject;
/* Save the registry key for this driver */ DriverData.ServiceKey.Length = RegistryPath->Length; DriverData.ServiceKey.MaximumLength = RegistryPath->MaximumLength; DriverData.ServiceKey.Buffer = (PWCHAR)ExAllocatePoolWithTag(NonPagedPool, RegistryPath->MaximumLength, FM_TAG_REGISTRY_DATA); if (!DriverData.ServiceKey.Buffer) return STATUS_INSUFFICIENT_RESOURCES; RtlCopyUnicodeString(&DriverData.ServiceKey, RegistryPath);
/* Do some initialization */ ExInitializeFastMutex(&DriverData.FilterAttachLock);
/* Create the main filter manager device object */ Status = IoCreateDevice(DriverObject, 0, &DeviceName, FILE_DEVICE_DISK_FILE_SYSTEM, FILE_DEVICE_SECURE_OPEN, FALSE, &DeviceObject); if (!NT_SUCCESS(Status)) { DPRINT1("fltmgr IoCreateDevice failed. Status = %X\n", Status); goto Cleanup; }
/* Store a global reference so we can access from callbacks */ DriverData.DeviceObject = DeviceObject;
/* Generate the symbolic link name */ RtlInitUnicodeString(&SymLink, L"\\??\\"DRIVER_NAME); Status = IoCreateSymbolicLink(&SymLink, &DeviceName); if (!NT_SUCCESS(Status)) goto Cleanup;
/* Create the callbacks for the dispatch table, FastIo and FS callbacks */ Status = SetupDispatchAndCallbacksTables(DriverObject); if (!NT_SUCCESS(Status)) goto Cleanup;
/* Initialize the comms objects */ Status = FltpSetupCommunicationObjects(DriverObject); if (!NT_SUCCESS(Status)) goto Cleanup;
/* Register for notifications when a new file system is loaded. This also enumerates any existing file systems */ Status = IoRegisterFsRegistrationChange(DriverObject, FltpFsNotification); FLT_ASSERT(Status != STATUS_DEVICE_ALREADY_ATTACHED); // Windows checks for this, I'm not sure how it can happen. Needs investigation?? if (!NT_SUCCESS(Status)) goto Cleanup;
InitializeListHead(&FilterList); ExInitializeResourceLite(&FilterListLock);
/* IoRegisterFsRegistrationChange isn't notified about the raw file systems, so we attach to them manually */ RtlInitUnicodeString(&ObjectName, L"\\Device\\RawDisk"); Status = IoGetDeviceObjectPointer(&ObjectName, FILE_READ_ATTRIBUTES, &RawFileObject, &RawDeviceObject); if (NT_SUCCESS(Status)) { FltpFsNotification(RawDeviceObject, TRUE); ObDereferenceObject(RawFileObject); }
RtlInitUnicodeString(&ObjectName, L"\\Device\\RawCdRom"); Status = IoGetDeviceObjectPointer(&ObjectName, FILE_READ_ATTRIBUTES, &RawFileObject, &RawDeviceObject); if (NT_SUCCESS(Status)) { FltpFsNotification(RawDeviceObject, TRUE); ObDereferenceObject(RawFileObject); }
/* We're done, clear the initializing flag */ ClearFlag(DeviceObject->Flags, DO_DEVICE_INITIALIZING); Status = STATUS_SUCCESS;
Cleanup:
if (!NT_SUCCESS(Status)) { if (DriverData.FastIoDispatch) { DriverObject->FastIoDispatch = NULL; ExFreePoolWithTag(DriverData.FastIoDispatch, FM_TAG_DISPATCH_TABLE); }
IoDeleteSymbolicLink(&SymLink);
if (DeviceObject) IoDeleteDevice(DeviceObject);
if (DriverData.ServiceKey.Buffer) ExFreePoolWithTag(DriverData.ServiceKey.Buffer, FM_TAG_REGISTRY_DATA); }
return Status;}