I have created three Device on Control Device ,One Disk Device another is FS device . Driver Entry Routine initializing them properly and device object are visible in the WinObj.
But in unload routine when it delete the FS device then It Causes the BSOD with IRQL_NOT_LEss_then_EQUAL.Please suggest me what is wrong happening in this code. It alway causing error when the It delete FS device.
the Entry Routines and the Unload Routine are
NTSTATUS DriverEntry(In PDRIVER_OBJECT DriverObject, In PUNICODE_STRING RegistryPath){
UNREFERENCED_PARAMETER(DriverObject);
UNREFERENCED_PARAMETER(RegistryPath);
AFSPrint(“AFS:\t IN:DriverEntry\n”);
UNICODE_STRING ControllerName;
UNICODE_STRING VDName;
UNICODE_STRING FSName;
UNICODE_STRING SymController;
UNICODE_STRING SymVD;
UNICODE_STRING SymFS;
PDEVICE_OBJECT ControllerDevice ;
PDEVICE_OBJECT VirtualDiskDevice;
PDEVICE_OBJECT VolumeDevice;
NTSTATUS ReturnStatus = STATUS_SUCCESS;
RtlInitUnicodeString(&ControllerName, AFS_KERNEL_CONTORLLER_NAME);
RtlInitUnicodeString(&VDName, AFS_KERNEL_DISK_NAME);
RtlInitUnicodeString(&FSName, AFS_KERNEL_VOLUME_NAME);
RtlInitUnicodeString(&SymController, AFS_SYMBOLIC_CONTROLLER_NAME);
RtlInitUnicodeString(&SymVD, AFS_SYMBOLIC_DISK_NAME);
RtlInitUnicodeString(&SymFS, AFS_SYMBOLIC_VOLUME_NAME);
DriverObject->DriverUnload = UnloadDriver;
DriverObject->MajorFunction[IRP_MJ_CREATE] = CreateRoutine;
DriverObject->MajorFunction[IRP_MJ_CLOSE] = CloseRoutine;
DriverObject->MajorFunction[IRP_MJ_DEVICE_CONTROL] = DeviceControlRoutine;
DriverObject->MajorFunction[IRP_MJ_READ] = ReadRoutine;
DriverObject->MajorFunction[IRP_MJ_WRITE] = UnsupportedRoutine;
/*Creating Controller Device…*/
AFSPrint(“AFS:Intializing Controller…\n”);
ReturnStatus = IoCreateDevice(DriverObject, sizeof(CONTROLLER_INSTANCE), &ControllerName, FILE_DEVICE_UNKNOWN, FILE_DEVICE_SECURE_OPEN, FALSE, &ControllerDevice);
if (SUCCESS(ReturnStatus))
{
AFSPrint(“AFS:Creating Controller Symbolic Link…\n”);
ReturnStatus = IoCreateSymbolicLink(&SymController, &ControllerName);
if (!SUCCESS(ReturnStatus))
{
AFSStatus(“Failed While Creating Controller Symbolic Link(VOLUME) Error -%x”, ReturnStatus);
IoDeleteDevice(ControllerDevice);
return STATUS_UNSUCCESSFUL;
}
}
else
{
AFSStatus(“Failed While Creating Controller(VOLUME) Error -%x”, ReturnStatus);
return STATUS_UNSUCCESSFUL;
}
/*Creating Disk Device…*/
AFSPrint(“AFS:Intializing Disk…\n”);
ReturnStatus = IoCreateDevice(DriverObject, 0, &VDName, FILE_DEVICE_VIRTUAL_DISK, FILE_DEVICE_SECURE_OPEN, FALSE, &VirtualDiskDevice);
if (SUCCESS(ReturnStatus))
{
AFSPrint(“AFS:Creating Disk Symbolic Link…\n”);
ReturnStatus = IoCreateSymbolicLink(&SymVD, &VDName);
if (!SUCCESS(ReturnStatus))
{
AFSStatus(“Failed While Creating Symbolic Link(Disk) Error -%x”, ReturnStatus);
IoDeleteSymbolicLink(&SymController);
IoDeleteDevice(ControllerDevice);
IoDeleteDevice(VirtualDiskDevice);
return STATUS_UNSUCCESSFUL;
}
}
else
{
AFSStatus(“Failed While Creating Disk Error -%x”, ReturnStatus);
IoDeleteSymbolicLink(&SymController);
IoDeleteDevice(ControllerDevice);
return STATUS_UNSUCCESSFUL;
}
/*Creating File System Volume Device…*/
AFSPrint(“AFS:Intializing File System…\n”);
ReturnStatus = IoCreateDevice(DriverObject, 0, &FSName, FILE_DEVICE_DISK_FILE_SYSTEM, FILE_DEVICE_SECURE_OPEN, FALSE, &VolumeDevice);
if (SUCCESS(ReturnStatus))
{
AFSPrint(“AFS:Creating FileSystem Symbolic Link…\n”);
ReturnStatus = IoCreateSymbolicLink(&SymFS, &FSName);
if (!SUCCESS(ReturnStatus))
{
IoDeleteSymbolicLink(&SymController);
IoDeleteSymbolicLink(&SymVD);
IoDeleteDevice(ControllerDevice);
IoDeleteDevice(VirtualDiskDevice);
IoDeleteDevice(VolumeDevice);
AFSStatus(“Failed While Creating Symbolic Link(VOLUME) Error -%x”, ReturnStatus);
return STATUS_UNSUCCESSFUL;
}
}
else
{
AFSStatus(“Failed While Creating VOLUME Error -%x”, ReturnStatus);
IoDeleteSymbolicLink(&SymController);
IoDeleteSymbolicLink(&SymVD);
IoDeleteDevice(ControllerDevice);
IoDeleteDevice(VirtualDiskDevice);
return STATUS_UNSUCCESSFUL;
}
AFSPrint(“AFS:DriverEntry => All Device Intiallized SuccesFully”);
PCONTROLLER_INSTANCE controllerState = (PCONTROLLER_INSTANCE)AllocateFromNonPageTag(sizeof(CONTROLLER_INSTANCE));
controllerState->ControllerDevice = ControllerDevice;
controllerState->DiskDevice = VirtualDiskDevice;
controllerState->VolumeDevice = VolumeDevice;
DriverObject->DeviceObject = ControllerDevice;
ControllerDevice->DeviceExtension = controllerState;
AFSStatus(“AFS: Intilaialize %x”, ReturnStatus);
return ReturnStatus;
}
void UnloadDriver(In PDRIVER_OBJECT DriverObject){
PCONTROLLER_INSTANCE controllerState;
UNICODE_STRING ControllerName;
UNICODE_STRING VDName;
UNICODE_STRING FSName;
UNICODE_STRING SymController;
UNICODE_STRING SymVD;
UNICODE_STRING SymFS;
PDEVICE_OBJECT Cont, FS, Disk;
UNREFERENCED_PARAMETER(DriverObject);
AFSPrint(“AFS:\t IN:UnloadRoutine\n”);
RtlInitUnicodeString(&ControllerName, AFS_KERNEL_CONTORLLER_NAME);
RtlInitUnicodeString(&VDName, AFS_KERNEL_DISK_NAME);
RtlInitUnicodeString(&FSName, AFS_KERNEL_VOLUME_NAME);
RtlInitUnicodeString(&SymController, AFS_SYMBOLIC_CONTROLLER_NAME);
RtlInitUnicodeString(&SymVD, AFS_SYMBOLIC_DISK_NAME);
RtlInitUnicodeString(&SymFS, AFS_SYMBOLIC_VOLUME_NAME);
controllerState = (PCONTROLLER_INSTANCE)DriverObject->DeviceObject->DeviceExtension;
Cont = controllerState->ControllerDevice;
Disk = controllerState->DiskDevice;
FS = controllerState->VolumeDevice;
ASSERT(controllerState);
ASSERT(controllerState->ControllerDevice);
IoDeleteSymbolicLink(&SymController);
IoDeleteSymbolicLink(&SymVD);
IoDeleteSymbolicLink(&SymFS);
ASSERT(Cont);
IoDeleteDevice(Cont);
ASSERT(Disk);
IoDeleteDevice(Disk);
ASSERT(FS);
if (FS)
IoDeleteDevice(FS);
AFSPrint(“AFS:\t OUT:UnloadRoutine\n”);
// PrintDebug(“In:UnloadDriver\n”);
}