2017-09-08 08:02:43 +00:00
|
|
|
/* Copyright (c) Mark Harmstone 2016-17
|
|
|
|
*
|
2016-09-04 15:27:46 +00:00
|
|
|
* This file is part of WinBtrfs.
|
2017-09-08 08:02:43 +00:00
|
|
|
*
|
2016-09-04 15:27:46 +00:00
|
|
|
* WinBtrfs is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU Lesser General Public Licence as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the Licence, or
|
|
|
|
* (at your option) any later version.
|
2017-09-08 08:02:43 +00:00
|
|
|
*
|
2016-09-04 15:27:46 +00:00
|
|
|
* WinBtrfs 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 Lesser General Public Licence for more details.
|
2017-09-08 08:02:43 +00:00
|
|
|
*
|
2016-09-04 15:27:46 +00:00
|
|
|
* You should have received a copy of the GNU Lesser General Public Licence
|
|
|
|
* along with WinBtrfs. If not, see <http://www.gnu.org/licenses/>. */
|
|
|
|
|
2016-05-05 17:26:47 +00:00
|
|
|
#include "btrfs_drv.h"
|
|
|
|
|
|
|
|
struct pnp_context;
|
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
struct pnp_context* context;
|
|
|
|
PIRP Irp;
|
|
|
|
IO_STATUS_BLOCK iosb;
|
|
|
|
NTSTATUS Status;
|
2017-01-01 17:12:12 +00:00
|
|
|
device* dev;
|
2016-05-05 17:26:47 +00:00
|
|
|
} pnp_stripe;
|
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
KEVENT Event;
|
|
|
|
NTSTATUS Status;
|
|
|
|
LONG left;
|
|
|
|
pnp_stripe* stripes;
|
|
|
|
} pnp_context;
|
|
|
|
|
2017-09-08 08:02:43 +00:00
|
|
|
extern ERESOURCE pdo_list_lock;
|
|
|
|
extern LIST_ENTRY pdo_list;
|
|
|
|
|
|
|
|
_Function_class_(IO_COMPLETION_ROUTINE)
|
|
|
|
#ifdef __REACTOS__
|
|
|
|
static NTSTATUS NTAPI pnp_completion(PDEVICE_OBJECT DeviceObject, PIRP Irp, PVOID conptr) {
|
|
|
|
#else
|
|
|
|
static NTSTATUS pnp_completion(PDEVICE_OBJECT DeviceObject, PIRP Irp, PVOID conptr) {
|
|
|
|
#endif
|
2016-05-05 17:26:47 +00:00
|
|
|
pnp_stripe* stripe = conptr;
|
|
|
|
pnp_context* context = (pnp_context*)stripe->context;
|
2017-09-08 08:02:43 +00:00
|
|
|
|
|
|
|
UNUSED(DeviceObject);
|
|
|
|
|
2016-05-05 17:26:47 +00:00
|
|
|
stripe->Status = Irp->IoStatus.Status;
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2016-05-05 17:26:47 +00:00
|
|
|
InterlockedDecrement(&context->left);
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2016-05-05 17:26:47 +00:00
|
|
|
if (context->left == 0)
|
|
|
|
KeSetEvent(&context->Event, 0, FALSE);
|
|
|
|
|
|
|
|
return STATUS_MORE_PROCESSING_REQUIRED;
|
|
|
|
}
|
|
|
|
|
|
|
|
static NTSTATUS send_disks_pnp_message(device_extension* Vcb, UCHAR minor) {
|
2017-09-08 08:02:43 +00:00
|
|
|
pnp_context context;
|
|
|
|
ULONG num_devices, i;
|
2016-05-05 17:26:47 +00:00
|
|
|
NTSTATUS Status;
|
2017-01-01 17:12:12 +00:00
|
|
|
LIST_ENTRY* le;
|
2017-09-08 08:02:43 +00:00
|
|
|
|
|
|
|
RtlZeroMemory(&context, sizeof(pnp_context));
|
|
|
|
KeInitializeEvent(&context.Event, NotificationEvent, FALSE);
|
|
|
|
|
|
|
|
num_devices = (ULONG)min(0xffffffff, Vcb->superblock.num_devices);
|
|
|
|
|
|
|
|
context.stripes = ExAllocatePoolWithTag(NonPagedPool, sizeof(pnp_stripe) * num_devices, ALLOC_TAG);
|
|
|
|
if (!context.stripes) {
|
2016-05-05 17:26:47 +00:00
|
|
|
ERR("out of memory\n");
|
|
|
|
return STATUS_INSUFFICIENT_RESOURCES;
|
|
|
|
}
|
2017-09-08 08:02:43 +00:00
|
|
|
|
|
|
|
RtlZeroMemory(context.stripes, sizeof(pnp_stripe) * num_devices);
|
|
|
|
|
2017-01-01 17:12:12 +00:00
|
|
|
i = 0;
|
|
|
|
le = Vcb->devices.Flink;
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2017-01-01 17:12:12 +00:00
|
|
|
while (le != &Vcb->devices) {
|
2016-05-05 17:26:47 +00:00
|
|
|
PIO_STACK_LOCATION IrpSp;
|
2017-01-01 17:12:12 +00:00
|
|
|
device* dev = CONTAINING_RECORD(le, device, list_entry);
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2017-01-01 17:12:12 +00:00
|
|
|
if (dev->devobj) {
|
2017-09-08 08:02:43 +00:00
|
|
|
context.stripes[i].context = (struct pnp_context*)&context;
|
|
|
|
|
|
|
|
context.stripes[i].Irp = IoAllocateIrp(dev->devobj->StackSize, FALSE);
|
2016-05-05 17:26:47 +00:00
|
|
|
|
2017-09-08 08:02:43 +00:00
|
|
|
if (!context.stripes[i].Irp) {
|
2016-05-05 17:26:47 +00:00
|
|
|
UINT64 j;
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2016-05-05 17:26:47 +00:00
|
|
|
ERR("IoAllocateIrp failed\n");
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2016-05-05 17:26:47 +00:00
|
|
|
for (j = 0; j < i; j++) {
|
2017-09-08 08:02:43 +00:00
|
|
|
if (context.stripes[j].dev->devobj) {
|
|
|
|
IoFreeIrp(context.stripes[j].Irp);
|
2016-05-05 17:26:47 +00:00
|
|
|
}
|
|
|
|
}
|
2017-09-08 08:02:43 +00:00
|
|
|
ExFreePool(context.stripes);
|
|
|
|
|
|
|
|
return STATUS_INSUFFICIENT_RESOURCES;
|
2016-05-05 17:26:47 +00:00
|
|
|
}
|
2017-09-08 08:02:43 +00:00
|
|
|
|
|
|
|
IrpSp = IoGetNextIrpStackLocation(context.stripes[i].Irp);
|
2016-05-05 17:26:47 +00:00
|
|
|
IrpSp->MajorFunction = IRP_MJ_PNP;
|
|
|
|
IrpSp->MinorFunction = minor;
|
|
|
|
|
2017-09-08 08:02:43 +00:00
|
|
|
context.stripes[i].Irp->UserIosb = &context.stripes[i].iosb;
|
|
|
|
|
|
|
|
IoSetCompletionRoutine(context.stripes[i].Irp, pnp_completion, &context.stripes[i], TRUE, TRUE, TRUE);
|
|
|
|
|
|
|
|
context.stripes[i].Irp->IoStatus.Status = STATUS_NOT_SUPPORTED;
|
|
|
|
context.stripes[i].dev = dev;
|
|
|
|
|
|
|
|
context.left++;
|
2016-05-05 17:26:47 +00:00
|
|
|
}
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2017-01-01 17:12:12 +00:00
|
|
|
le = le->Flink;
|
2016-05-05 17:26:47 +00:00
|
|
|
}
|
2017-09-08 08:02:43 +00:00
|
|
|
|
|
|
|
if (context.left == 0) {
|
2016-05-05 17:26:47 +00:00
|
|
|
Status = STATUS_SUCCESS;
|
|
|
|
goto end;
|
|
|
|
}
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2016-05-05 17:26:47 +00:00
|
|
|
for (i = 0; i < num_devices; i++) {
|
2017-09-08 08:02:43 +00:00
|
|
|
if (context.stripes[i].Irp) {
|
|
|
|
IoCallDriver(context.stripes[i].dev->devobj, context.stripes[i].Irp);
|
2016-05-05 17:26:47 +00:00
|
|
|
}
|
|
|
|
}
|
2017-09-08 08:02:43 +00:00
|
|
|
|
|
|
|
KeWaitForSingleObject(&context.Event, Executive, KernelMode, FALSE, NULL);
|
|
|
|
|
2016-05-05 17:26:47 +00:00
|
|
|
Status = STATUS_SUCCESS;
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2016-05-05 17:26:47 +00:00
|
|
|
for (i = 0; i < num_devices; i++) {
|
2017-09-08 08:02:43 +00:00
|
|
|
if (context.stripes[i].Irp) {
|
|
|
|
if (context.stripes[i].Status != STATUS_SUCCESS)
|
|
|
|
Status = context.stripes[i].Status;
|
2016-05-05 17:26:47 +00:00
|
|
|
}
|
|
|
|
}
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2016-05-05 17:26:47 +00:00
|
|
|
end:
|
|
|
|
for (i = 0; i < num_devices; i++) {
|
2017-09-08 08:02:43 +00:00
|
|
|
if (context.stripes[i].Irp) {
|
|
|
|
IoFreeIrp(context.stripes[i].Irp);
|
2016-05-05 17:26:47 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-09-08 08:02:43 +00:00
|
|
|
ExFreePool(context.stripes);
|
2016-05-05 17:26:47 +00:00
|
|
|
|
|
|
|
return Status;
|
|
|
|
}
|
|
|
|
|
2017-09-08 08:02:43 +00:00
|
|
|
static NTSTATUS pnp_cancel_remove_device(PDEVICE_OBJECT DeviceObject) {
|
|
|
|
device_extension* Vcb = DeviceObject->DeviceExtension;
|
2016-05-05 17:26:47 +00:00
|
|
|
NTSTATUS Status;
|
2017-09-08 08:02:43 +00:00
|
|
|
|
|
|
|
ExAcquireResourceSharedLite(&Vcb->tree_lock, TRUE);
|
|
|
|
|
2017-10-16 18:05:33 +00:00
|
|
|
acquire_fcb_lock_exclusive(Vcb);
|
2016-05-05 17:26:47 +00:00
|
|
|
|
2016-10-29 17:05:10 +00:00
|
|
|
if (Vcb->root_fileref && Vcb->root_fileref->fcb && (Vcb->root_fileref->open_count > 0 || has_open_children(Vcb->root_fileref))) {
|
2016-05-05 17:26:47 +00:00
|
|
|
Status = STATUS_ACCESS_DENIED;
|
|
|
|
goto end;
|
|
|
|
}
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2016-05-05 17:26:47 +00:00
|
|
|
Status = send_disks_pnp_message(Vcb, IRP_MN_CANCEL_REMOVE_DEVICE);
|
|
|
|
if (!NT_SUCCESS(Status)) {
|
|
|
|
WARN("send_disks_pnp_message returned %08x\n", Status);
|
|
|
|
goto end;
|
|
|
|
}
|
|
|
|
|
|
|
|
end:
|
2017-10-16 18:05:33 +00:00
|
|
|
release_fcb_lock(Vcb);
|
2017-09-08 08:02:43 +00:00
|
|
|
ExReleaseResourceLite(&Vcb->tree_lock);
|
|
|
|
|
2016-05-05 17:26:47 +00:00
|
|
|
return STATUS_SUCCESS;
|
|
|
|
}
|
|
|
|
|
2017-09-08 08:02:43 +00:00
|
|
|
NTSTATUS pnp_query_remove_device(PDEVICE_OBJECT DeviceObject, PIRP Irp) {
|
2016-05-05 17:26:47 +00:00
|
|
|
device_extension* Vcb = DeviceObject->DeviceExtension;
|
|
|
|
NTSTATUS Status;
|
2017-09-08 08:02:43 +00:00
|
|
|
|
|
|
|
ExAcquireResourceExclusiveLite(&Vcb->tree_lock, TRUE);
|
|
|
|
|
2017-10-16 18:05:33 +00:00
|
|
|
acquire_fcb_lock_exclusive(Vcb);
|
2016-05-05 17:26:47 +00:00
|
|
|
|
2016-10-29 17:05:10 +00:00
|
|
|
if (Vcb->root_fileref && Vcb->root_fileref->fcb && (Vcb->root_fileref->open_count > 0 || has_open_children(Vcb->root_fileref))) {
|
2016-05-05 17:26:47 +00:00
|
|
|
Status = STATUS_ACCESS_DENIED;
|
|
|
|
goto end;
|
|
|
|
}
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2016-05-05 17:26:47 +00:00
|
|
|
Status = send_disks_pnp_message(Vcb, IRP_MN_QUERY_REMOVE_DEVICE);
|
|
|
|
if (!NT_SUCCESS(Status)) {
|
|
|
|
WARN("send_disks_pnp_message returned %08x\n", Status);
|
|
|
|
goto end;
|
|
|
|
}
|
|
|
|
|
|
|
|
Vcb->removing = TRUE;
|
|
|
|
|
2017-09-08 08:02:43 +00:00
|
|
|
if (Vcb->need_write && !Vcb->readonly) {
|
|
|
|
Status = do_write(Vcb, Irp);
|
|
|
|
|
|
|
|
free_trees(Vcb);
|
|
|
|
|
|
|
|
if (!NT_SUCCESS(Status)) {
|
|
|
|
ERR("do_write returned %08x\n", Status);
|
|
|
|
goto end;
|
|
|
|
}
|
|
|
|
}
|
2016-05-05 17:26:47 +00:00
|
|
|
|
|
|
|
|
|
|
|
Status = STATUS_SUCCESS;
|
|
|
|
end:
|
2017-10-16 18:05:33 +00:00
|
|
|
release_fcb_lock(Vcb);
|
2017-09-08 08:02:43 +00:00
|
|
|
|
|
|
|
ExReleaseResourceLite(&Vcb->tree_lock);
|
|
|
|
|
2016-05-05 17:26:47 +00:00
|
|
|
return Status;
|
|
|
|
}
|
|
|
|
|
2017-09-08 08:02:43 +00:00
|
|
|
static NTSTATUS pnp_remove_device(PDEVICE_OBJECT DeviceObject) {
|
2016-05-05 17:26:47 +00:00
|
|
|
device_extension* Vcb = DeviceObject->DeviceExtension;
|
|
|
|
NTSTATUS Status;
|
2017-09-08 08:02:43 +00:00
|
|
|
|
|
|
|
ExAcquireResourceSharedLite(&Vcb->tree_lock, TRUE);
|
|
|
|
|
2016-05-05 17:26:47 +00:00
|
|
|
Status = send_disks_pnp_message(Vcb, IRP_MN_REMOVE_DEVICE);
|
2017-09-08 08:02:43 +00:00
|
|
|
|
|
|
|
if (!NT_SUCCESS(Status))
|
2016-05-05 17:26:47 +00:00
|
|
|
WARN("send_disks_pnp_message returned %08x\n", Status);
|
2017-09-08 08:02:43 +00:00
|
|
|
|
|
|
|
ExReleaseResourceLite(&Vcb->tree_lock);
|
|
|
|
|
2016-05-05 17:26:47 +00:00
|
|
|
if (DeviceObject->Vpb->Flags & VPB_MOUNTED) {
|
2016-07-27 19:24:26 +00:00
|
|
|
Status = FsRtlNotifyVolumeEvent(Vcb->root_file, FSRTL_VOLUME_DISMOUNT);
|
|
|
|
if (!NT_SUCCESS(Status)) {
|
|
|
|
WARN("FsRtlNotifyVolumeEvent returned %08x\n", Status);
|
|
|
|
}
|
2017-09-08 08:02:43 +00:00
|
|
|
|
|
|
|
if (Vcb->vde)
|
|
|
|
Vcb->vde->mounted_device = NULL;
|
|
|
|
|
|
|
|
ExAcquireResourceExclusiveLite(&Vcb->tree_lock, TRUE);
|
|
|
|
Vcb->removing = TRUE;
|
|
|
|
Vcb->Vpb->Flags &= ~VPB_MOUNTED;
|
|
|
|
Vcb->Vpb->Flags |= VPB_DIRECT_WRITES_ALLOWED;
|
|
|
|
ExReleaseResourceLite(&Vcb->tree_lock);
|
|
|
|
|
|
|
|
if (Vcb->open_files == 0)
|
2016-10-29 17:05:10 +00:00
|
|
|
uninit(Vcb, FALSE);
|
2016-05-05 17:26:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return STATUS_SUCCESS;
|
|
|
|
}
|
|
|
|
|
2017-09-08 08:02:43 +00:00
|
|
|
NTSTATUS pnp_surprise_removal(PDEVICE_OBJECT DeviceObject, PIRP Irp) {
|
2016-09-04 15:27:46 +00:00
|
|
|
device_extension* Vcb = DeviceObject->DeviceExtension;
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2016-09-04 15:27:46 +00:00
|
|
|
TRACE("(%p, %p)\n", DeviceObject, Irp);
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2016-09-04 15:27:46 +00:00
|
|
|
if (DeviceObject->Vpb->Flags & VPB_MOUNTED) {
|
2017-09-08 08:02:43 +00:00
|
|
|
ExAcquireResourceExclusiveLite(&Vcb->tree_lock, TRUE);
|
|
|
|
|
|
|
|
if (Vcb->vde)
|
|
|
|
Vcb->vde->mounted_device = NULL;
|
|
|
|
|
|
|
|
Vcb->removing = TRUE;
|
|
|
|
Vcb->Vpb->Flags &= ~VPB_MOUNTED;
|
|
|
|
Vcb->Vpb->Flags |= VPB_DIRECT_WRITES_ALLOWED;
|
|
|
|
|
|
|
|
ExReleaseResourceLite(&Vcb->tree_lock);
|
|
|
|
|
|
|
|
if (Vcb->open_files == 0)
|
2016-10-29 17:05:10 +00:00
|
|
|
uninit(Vcb, FALSE);
|
2016-09-04 15:27:46 +00:00
|
|
|
}
|
2016-05-05 17:26:47 +00:00
|
|
|
|
|
|
|
return STATUS_SUCCESS;
|
|
|
|
}
|
|
|
|
|
2017-09-08 08:02:43 +00:00
|
|
|
static void bus_query_capabilities(PIRP Irp) {
|
|
|
|
PIO_STACK_LOCATION IrpSp = IoGetCurrentIrpStackLocation(Irp);
|
|
|
|
PDEVICE_CAPABILITIES dc = IrpSp->Parameters.DeviceCapabilities.Capabilities;
|
|
|
|
|
|
|
|
dc->UniqueID = TRUE;
|
|
|
|
dc->SilentInstall = TRUE;
|
|
|
|
|
|
|
|
Irp->IoStatus.Status = STATUS_SUCCESS;
|
|
|
|
}
|
|
|
|
|
|
|
|
static NTSTATUS bus_query_device_relations(PIRP Irp) {
|
|
|
|
NTSTATUS Status;
|
|
|
|
ULONG num_children;
|
|
|
|
LIST_ENTRY* le;
|
|
|
|
ULONG drsize, i;
|
|
|
|
DEVICE_RELATIONS* dr;
|
|
|
|
|
|
|
|
ExAcquireResourceSharedLite(&pdo_list_lock, TRUE);
|
|
|
|
|
|
|
|
num_children = 0;
|
|
|
|
|
|
|
|
le = pdo_list.Flink;
|
|
|
|
while (le != &pdo_list) {
|
|
|
|
num_children++;
|
|
|
|
|
|
|
|
le = le->Flink;
|
|
|
|
}
|
|
|
|
|
|
|
|
drsize = offsetof(DEVICE_RELATIONS, Objects[0]) + (num_children * sizeof(PDEVICE_OBJECT));
|
|
|
|
dr = ExAllocatePoolWithTag(PagedPool, drsize, ALLOC_TAG);
|
|
|
|
|
|
|
|
if (!dr) {
|
|
|
|
ERR("out of memory\n");
|
|
|
|
Status = STATUS_INSUFFICIENT_RESOURCES;
|
|
|
|
goto end;
|
|
|
|
}
|
|
|
|
|
|
|
|
dr->Count = num_children;
|
|
|
|
|
|
|
|
i = 0;
|
|
|
|
le = pdo_list.Flink;
|
|
|
|
while (le != &pdo_list) {
|
|
|
|
pdo_device_extension* pdode = CONTAINING_RECORD(le, pdo_device_extension, list_entry);
|
|
|
|
|
|
|
|
ObReferenceObject(pdode->pdo);
|
|
|
|
dr->Objects[i] = pdode->pdo;
|
|
|
|
i++;
|
|
|
|
|
|
|
|
le = le->Flink;
|
|
|
|
}
|
|
|
|
|
|
|
|
Irp->IoStatus.Information = (ULONG_PTR)dr;
|
|
|
|
|
|
|
|
Status = STATUS_SUCCESS;
|
|
|
|
|
|
|
|
end:
|
|
|
|
ExReleaseResourceLite(&pdo_list_lock);
|
|
|
|
|
|
|
|
Irp->IoStatus.Status = Status;
|
|
|
|
IoCompleteRequest(Irp, IO_NO_INCREMENT);
|
|
|
|
|
|
|
|
return Status;
|
|
|
|
}
|
|
|
|
|
|
|
|
static NTSTATUS bus_query_hardware_ids(PIRP Irp) {
|
|
|
|
WCHAR* out;
|
|
|
|
|
|
|
|
static WCHAR ids[] = L"ROOT\\btrfs\0";
|
|
|
|
|
|
|
|
out = ExAllocatePoolWithTag(PagedPool, sizeof(ids), ALLOC_TAG);
|
|
|
|
if (!out) {
|
|
|
|
ERR("out of memory\n");
|
|
|
|
return STATUS_INSUFFICIENT_RESOURCES;
|
|
|
|
}
|
|
|
|
|
|
|
|
RtlCopyMemory(out, ids, sizeof(ids));
|
|
|
|
|
|
|
|
Irp->IoStatus.Information = (ULONG_PTR)out;
|
|
|
|
|
|
|
|
return STATUS_SUCCESS;
|
|
|
|
}
|
|
|
|
|
|
|
|
static NTSTATUS bus_pnp(control_device_extension* cde, PIRP Irp) {
|
|
|
|
PIO_STACK_LOCATION IrpSp = IoGetCurrentIrpStackLocation(Irp);
|
|
|
|
|
|
|
|
switch (IrpSp->MinorFunction) {
|
|
|
|
case IRP_MN_QUERY_CAPABILITIES:
|
|
|
|
bus_query_capabilities(Irp);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case IRP_MN_QUERY_DEVICE_RELATIONS:
|
|
|
|
if (IrpSp->Parameters.QueryDeviceRelations.Type != BusRelations || no_pnp)
|
|
|
|
break;
|
|
|
|
|
|
|
|
return bus_query_device_relations(Irp);
|
|
|
|
|
|
|
|
case IRP_MN_QUERY_ID:
|
|
|
|
{
|
|
|
|
NTSTATUS Status;
|
|
|
|
|
|
|
|
if (IrpSp->Parameters.QueryId.IdType != BusQueryHardwareIDs)
|
|
|
|
break;
|
|
|
|
|
|
|
|
Status = bus_query_hardware_ids(Irp);
|
|
|
|
|
|
|
|
Irp->IoStatus.Status = Status;
|
|
|
|
IoCompleteRequest(Irp, IO_NO_INCREMENT);
|
|
|
|
|
|
|
|
return Status;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
IoSkipCurrentIrpStackLocation(Irp);
|
|
|
|
return IoCallDriver(cde->attached_device, Irp);
|
|
|
|
}
|
|
|
|
|
|
|
|
static NTSTATUS pdo_query_device_id(pdo_device_extension* pdode, PIRP Irp) {
|
|
|
|
WCHAR name[100], *noff, *out;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
static WCHAR pref[] = L"Btrfs\\";
|
|
|
|
|
|
|
|
RtlCopyMemory(name, pref, wcslen(pref) * sizeof(WCHAR));
|
|
|
|
|
|
|
|
noff = &name[wcslen(pref)];
|
|
|
|
for (i = 0; i < 16; i++) {
|
|
|
|
*noff = hex_digit(pdode->uuid.uuid[i] >> 4); noff++;
|
|
|
|
*noff = hex_digit(pdode->uuid.uuid[i] & 0xf); noff++;
|
|
|
|
|
|
|
|
if (i == 3 || i == 5 || i == 7 || i == 9) {
|
|
|
|
*noff = '-';
|
|
|
|
noff++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
*noff = 0;
|
|
|
|
|
|
|
|
out = ExAllocatePoolWithTag(PagedPool, (wcslen(name) + 1) * sizeof(WCHAR), ALLOC_TAG);
|
|
|
|
if (!out) {
|
|
|
|
ERR("out of memory\n");
|
|
|
|
return STATUS_INSUFFICIENT_RESOURCES;
|
|
|
|
}
|
|
|
|
|
|
|
|
RtlCopyMemory(out, name, (wcslen(name) + 1) * sizeof(WCHAR));
|
|
|
|
|
|
|
|
Irp->IoStatus.Information = (ULONG_PTR)out;
|
|
|
|
|
|
|
|
return STATUS_SUCCESS;
|
|
|
|
}
|
|
|
|
|
|
|
|
static NTSTATUS pdo_query_hardware_ids(PIRP Irp) {
|
|
|
|
WCHAR* out;
|
|
|
|
|
|
|
|
static WCHAR ids[] = L"BtrfsVolume\0";
|
|
|
|
|
|
|
|
out = ExAllocatePoolWithTag(PagedPool, sizeof(ids), ALLOC_TAG);
|
|
|
|
if (!out) {
|
|
|
|
ERR("out of memory\n");
|
|
|
|
return STATUS_INSUFFICIENT_RESOURCES;
|
|
|
|
}
|
|
|
|
|
|
|
|
RtlCopyMemory(out, ids, sizeof(ids));
|
|
|
|
|
|
|
|
Irp->IoStatus.Information = (ULONG_PTR)out;
|
|
|
|
|
|
|
|
return STATUS_SUCCESS;
|
|
|
|
}
|
|
|
|
|
|
|
|
static NTSTATUS pdo_query_id(pdo_device_extension* pdode, PIRP Irp) {
|
|
|
|
PIO_STACK_LOCATION IrpSp = IoGetCurrentIrpStackLocation(Irp);
|
|
|
|
|
|
|
|
switch (IrpSp->Parameters.QueryId.IdType) {
|
|
|
|
case BusQueryDeviceID:
|
|
|
|
TRACE("BusQueryDeviceID\n");
|
|
|
|
return pdo_query_device_id(pdode, Irp);
|
|
|
|
|
|
|
|
case BusQueryHardwareIDs:
|
|
|
|
TRACE("BusQueryHardwareIDs\n");
|
|
|
|
return pdo_query_hardware_ids(Irp);
|
|
|
|
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return Irp->IoStatus.Status;
|
|
|
|
}
|
|
|
|
|
|
|
|
static NTSTATUS pdo_pnp(PDEVICE_OBJECT pdo, PIRP Irp) {
|
|
|
|
PIO_STACK_LOCATION IrpSp = IoGetCurrentIrpStackLocation(Irp);
|
|
|
|
pdo_device_extension* pdode = pdo->DeviceExtension;
|
|
|
|
|
|
|
|
switch (IrpSp->MinorFunction) {
|
|
|
|
case IRP_MN_QUERY_ID:
|
|
|
|
return pdo_query_id(pdode, Irp);
|
|
|
|
|
|
|
|
case IRP_MN_START_DEVICE:
|
|
|
|
case IRP_MN_CANCEL_REMOVE_DEVICE:
|
|
|
|
case IRP_MN_SURPRISE_REMOVAL:
|
|
|
|
case IRP_MN_REMOVE_DEVICE:
|
|
|
|
return STATUS_SUCCESS;
|
|
|
|
|
|
|
|
case IRP_MN_QUERY_REMOVE_DEVICE:
|
|
|
|
return STATUS_UNSUCCESSFUL;
|
|
|
|
}
|
|
|
|
|
|
|
|
return Irp->IoStatus.Status;
|
|
|
|
}
|
|
|
|
|
|
|
|
_Dispatch_type_(IRP_MJ_PNP)
|
|
|
|
_Function_class_(DRIVER_DISPATCH)
|
2018-08-21 05:05:40 +00:00
|
|
|
NTSTATUS NTAPI drv_pnp(PDEVICE_OBJECT DeviceObject, PIRP Irp) {
|
2016-05-05 17:26:47 +00:00
|
|
|
PIO_STACK_LOCATION IrpSp = IoGetCurrentIrpStackLocation(Irp);
|
|
|
|
device_extension* Vcb = DeviceObject->DeviceExtension;
|
|
|
|
NTSTATUS Status;
|
|
|
|
BOOL top_level;
|
|
|
|
|
|
|
|
FsRtlEnterFileSystem();
|
|
|
|
|
|
|
|
top_level = is_top_level(Irp);
|
2017-09-08 08:02:43 +00:00
|
|
|
|
|
|
|
if (Vcb && Vcb->type == VCB_TYPE_CONTROL) {
|
|
|
|
Status = bus_pnp(DeviceObject->DeviceExtension, Irp);
|
|
|
|
goto exit;
|
|
|
|
} else if (Vcb && Vcb->type == VCB_TYPE_VOLUME) {
|
|
|
|
volume_device_extension* vde = DeviceObject->DeviceExtension;
|
|
|
|
IoSkipCurrentIrpStackLocation(Irp);
|
|
|
|
Status = IoCallDriver(vde->pdo, Irp);
|
|
|
|
goto exit;
|
|
|
|
} else if (Vcb && Vcb->type == VCB_TYPE_PDO) {
|
|
|
|
Status = pdo_pnp(DeviceObject, Irp);
|
|
|
|
goto end;
|
|
|
|
} else if (!Vcb || Vcb->type != VCB_TYPE_FS) {
|
|
|
|
Status = STATUS_INVALID_PARAMETER;
|
2016-07-27 19:24:26 +00:00
|
|
|
goto end;
|
|
|
|
}
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2016-05-05 17:26:47 +00:00
|
|
|
Status = STATUS_NOT_IMPLEMENTED;
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2016-05-05 17:26:47 +00:00
|
|
|
switch (IrpSp->MinorFunction) {
|
|
|
|
case IRP_MN_CANCEL_REMOVE_DEVICE:
|
2017-09-08 08:02:43 +00:00
|
|
|
Status = pnp_cancel_remove_device(DeviceObject);
|
2016-05-05 17:26:47 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case IRP_MN_QUERY_REMOVE_DEVICE:
|
|
|
|
Status = pnp_query_remove_device(DeviceObject, Irp);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case IRP_MN_REMOVE_DEVICE:
|
2017-09-08 08:02:43 +00:00
|
|
|
Status = pnp_remove_device(DeviceObject);
|
2016-05-05 17:26:47 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case IRP_MN_SURPRISE_REMOVAL:
|
|
|
|
Status = pnp_surprise_removal(DeviceObject, Irp);
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
TRACE("passing minor function 0x%x on\n", IrpSp->MinorFunction);
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2016-05-05 17:26:47 +00:00
|
|
|
IoSkipCurrentIrpStackLocation(Irp);
|
2017-01-01 17:12:12 +00:00
|
|
|
Status = IoCallDriver(Vcb->Vpb->RealDevice, Irp);
|
2017-09-08 08:02:43 +00:00
|
|
|
goto exit;
|
2016-05-05 17:26:47 +00:00
|
|
|
}
|
|
|
|
|
2017-09-08 08:02:43 +00:00
|
|
|
end:
|
2016-05-05 17:26:47 +00:00
|
|
|
Irp->IoStatus.Status = Status;
|
|
|
|
|
|
|
|
IoCompleteRequest(Irp, IO_NO_INCREMENT);
|
2017-09-08 08:02:43 +00:00
|
|
|
|
|
|
|
exit:
|
|
|
|
TRACE("returning %08x\n", Status);
|
|
|
|
|
|
|
|
if (top_level)
|
2016-05-05 17:26:47 +00:00
|
|
|
IoSetTopLevelIrp(NULL);
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2016-05-05 17:26:47 +00:00
|
|
|
FsRtlExitFileSystem();
|
|
|
|
|
|
|
|
return Status;
|
|
|
|
}
|