2017-09-08 08:02:43 +00:00
|
|
|
/* Copyright (c) Mark Harmstone 2016-17
|
|
|
|
*
|
2016-10-29 17:05:10 +00:00
|
|
|
* This file is part of WinBtrfs.
|
2017-09-08 08:02:43 +00:00
|
|
|
*
|
2016-10-29 17:05:10 +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-10-29 17:05:10 +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-10-29 17:05:10 +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/>. */
|
|
|
|
|
|
|
|
#include "btrfs_drv.h"
|
2017-09-08 08:02:43 +00:00
|
|
|
#include <ntdddisk.h>
|
2016-10-29 17:05:10 +00:00
|
|
|
#include <mountdev.h>
|
|
|
|
#include <diskguid.h>
|
|
|
|
|
2017-09-08 08:02:43 +00:00
|
|
|
extern PDRIVER_OBJECT drvobj;
|
2017-01-01 17:12:12 +00:00
|
|
|
extern LIST_ENTRY VcbList;
|
|
|
|
extern ERESOURCE global_loading_lock;
|
|
|
|
|
2016-10-29 17:05:10 +00:00
|
|
|
static NTSTATUS mountdev_query_stable_guid(device_extension* Vcb, PIRP Irp) {
|
|
|
|
MOUNTDEV_STABLE_GUID* msg = Irp->UserBuffer;
|
|
|
|
PIO_STACK_LOCATION IrpSp = IoGetCurrentIrpStackLocation(Irp);
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2016-10-29 17:05:10 +00:00
|
|
|
TRACE("IOCTL_MOUNTDEV_QUERY_STABLE_GUID\n");
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2016-10-29 17:05:10 +00:00
|
|
|
if (IrpSp->Parameters.DeviceIoControl.OutputBufferLength < sizeof(MOUNTDEV_STABLE_GUID))
|
|
|
|
return STATUS_INVALID_PARAMETER;
|
|
|
|
|
|
|
|
RtlCopyMemory(&msg->StableGuid, &Vcb->superblock.uuid, sizeof(GUID));
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2016-10-29 17:05:10 +00:00
|
|
|
Irp->IoStatus.Information = sizeof(MOUNTDEV_STABLE_GUID);
|
|
|
|
|
|
|
|
return STATUS_SUCCESS;
|
|
|
|
}
|
|
|
|
|
2017-09-08 08:02:43 +00:00
|
|
|
static NTSTATUS is_writable(device_extension* Vcb) {
|
2016-10-29 17:05:10 +00:00
|
|
|
TRACE("IOCTL_DISK_IS_WRITABLE\n");
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2016-10-29 17:05:10 +00:00
|
|
|
return Vcb->readonly ? STATUS_MEDIA_WRITE_PROTECTED : STATUS_SUCCESS;
|
|
|
|
}
|
|
|
|
|
2017-01-01 17:12:12 +00:00
|
|
|
static NTSTATUS query_filesystems(void* data, ULONG length) {
|
|
|
|
NTSTATUS Status;
|
|
|
|
LIST_ENTRY *le, *le2;
|
|
|
|
btrfs_filesystem* bfs = NULL;
|
|
|
|
ULONG itemsize;
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2019-09-01 12:53:20 +00:00
|
|
|
ExAcquireResourceSharedLite(&global_loading_lock, true);
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2017-01-01 17:12:12 +00:00
|
|
|
if (IsListEmpty(&VcbList)) {
|
|
|
|
if (length < sizeof(btrfs_filesystem)) {
|
|
|
|
Status = STATUS_BUFFER_OVERFLOW;
|
|
|
|
goto end;
|
|
|
|
} else {
|
|
|
|
RtlZeroMemory(data, sizeof(btrfs_filesystem));
|
|
|
|
Status = STATUS_SUCCESS;
|
|
|
|
goto end;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
le = VcbList.Flink;
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2017-01-01 17:12:12 +00:00
|
|
|
while (le != &VcbList) {
|
|
|
|
device_extension* Vcb = CONTAINING_RECORD(le, device_extension, list_entry);
|
|
|
|
btrfs_filesystem_device* bfd;
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2017-01-01 17:12:12 +00:00
|
|
|
if (bfs) {
|
|
|
|
bfs->next_entry = itemsize;
|
2019-09-01 12:53:20 +00:00
|
|
|
bfs = (btrfs_filesystem*)((uint8_t*)bfs + itemsize);
|
2017-01-01 17:12:12 +00:00
|
|
|
} else
|
|
|
|
bfs = data;
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2017-01-01 17:12:12 +00:00
|
|
|
if (length < offsetof(btrfs_filesystem, device)) {
|
|
|
|
Status = STATUS_BUFFER_OVERFLOW;
|
|
|
|
goto end;
|
|
|
|
}
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2017-01-01 17:12:12 +00:00
|
|
|
itemsize = offsetof(btrfs_filesystem, device);
|
|
|
|
length -= offsetof(btrfs_filesystem, device);
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2017-01-01 17:12:12 +00:00
|
|
|
bfs->next_entry = 0;
|
|
|
|
RtlCopyMemory(&bfs->uuid, &Vcb->superblock.uuid, sizeof(BTRFS_UUID));
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2019-09-01 12:53:20 +00:00
|
|
|
ExAcquireResourceSharedLite(&Vcb->tree_lock, true);
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2019-09-01 12:53:20 +00:00
|
|
|
bfs->num_devices = (uint32_t)Vcb->superblock.num_devices;
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2017-01-01 17:12:12 +00:00
|
|
|
bfd = NULL;
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2017-01-01 17:12:12 +00:00
|
|
|
le2 = Vcb->devices.Flink;
|
|
|
|
while (le2 != &Vcb->devices) {
|
|
|
|
device* dev = CONTAINING_RECORD(le2, device, list_entry);
|
|
|
|
MOUNTDEV_NAME mdn;
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2017-01-01 17:12:12 +00:00
|
|
|
if (bfd)
|
2019-09-01 12:53:20 +00:00
|
|
|
bfd = (btrfs_filesystem_device*)((uint8_t*)bfd + offsetof(btrfs_filesystem_device, name[0]) + bfd->name_length);
|
2017-01-01 17:12:12 +00:00
|
|
|
else
|
|
|
|
bfd = &bfs->device;
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2017-01-01 17:12:12 +00:00
|
|
|
if (length < offsetof(btrfs_filesystem_device, name[0])) {
|
|
|
|
ExReleaseResourceLite(&Vcb->tree_lock);
|
|
|
|
Status = STATUS_BUFFER_OVERFLOW;
|
|
|
|
goto end;
|
|
|
|
}
|
2017-09-08 08:02:43 +00:00
|
|
|
|
|
|
|
itemsize += (ULONG)offsetof(btrfs_filesystem_device, name[0]);
|
|
|
|
length -= (ULONG)offsetof(btrfs_filesystem_device, name[0]);
|
|
|
|
|
2017-01-01 17:12:12 +00:00
|
|
|
RtlCopyMemory(&bfd->uuid, &dev->devitem.device_uuid, sizeof(BTRFS_UUID));
|
2017-09-08 08:02:43 +00:00
|
|
|
|
|
|
|
if (dev->devobj) {
|
2019-09-01 12:53:20 +00:00
|
|
|
Status = dev_ioctl(dev->devobj, IOCTL_MOUNTDEV_QUERY_DEVICE_NAME, NULL, 0, &mdn, sizeof(MOUNTDEV_NAME), true, NULL);
|
2017-09-08 08:02:43 +00:00
|
|
|
if (!NT_SUCCESS(Status) && Status != STATUS_BUFFER_OVERFLOW) {
|
|
|
|
ExReleaseResourceLite(&Vcb->tree_lock);
|
2020-04-23 02:38:57 +00:00
|
|
|
ERR("IOCTL_MOUNTDEV_QUERY_DEVICE_NAME returned %08lx\n", Status);
|
2017-09-08 08:02:43 +00:00
|
|
|
goto end;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mdn.NameLength > length) {
|
|
|
|
ExReleaseResourceLite(&Vcb->tree_lock);
|
|
|
|
Status = STATUS_BUFFER_OVERFLOW;
|
|
|
|
goto end;
|
|
|
|
}
|
|
|
|
|
2019-09-01 12:53:20 +00:00
|
|
|
Status = dev_ioctl(dev->devobj, IOCTL_MOUNTDEV_QUERY_DEVICE_NAME, NULL, 0, &bfd->name_length, (ULONG)offsetof(MOUNTDEV_NAME, Name[0]) + mdn.NameLength, true, NULL);
|
2017-09-08 08:02:43 +00:00
|
|
|
if (!NT_SUCCESS(Status) && Status != STATUS_BUFFER_OVERFLOW) {
|
|
|
|
ExReleaseResourceLite(&Vcb->tree_lock);
|
2020-04-23 02:38:57 +00:00
|
|
|
ERR("IOCTL_MOUNTDEV_QUERY_DEVICE_NAME returned %08lx\n", Status);
|
2017-09-08 08:02:43 +00:00
|
|
|
goto end;
|
|
|
|
}
|
|
|
|
|
|
|
|
itemsize += bfd->name_length;
|
|
|
|
length -= bfd->name_length;
|
|
|
|
} else {
|
2019-09-01 12:53:20 +00:00
|
|
|
bfd->missing = true;
|
2017-09-08 08:02:43 +00:00
|
|
|
bfd->name_length = 0;
|
2017-01-01 17:12:12 +00:00
|
|
|
}
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2017-01-01 17:12:12 +00:00
|
|
|
le2 = le2->Flink;
|
|
|
|
}
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2017-01-01 17:12:12 +00:00
|
|
|
ExReleaseResourceLite(&Vcb->tree_lock);
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2017-01-01 17:12:12 +00:00
|
|
|
le = le->Flink;
|
|
|
|
}
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2017-01-01 17:12:12 +00:00
|
|
|
Status = STATUS_SUCCESS;
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2017-01-01 17:12:12 +00:00
|
|
|
end:
|
|
|
|
ExReleaseResourceLite(&global_loading_lock);
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2017-01-01 17:12:12 +00:00
|
|
|
return Status;
|
|
|
|
}
|
|
|
|
|
2017-09-08 08:02:43 +00:00
|
|
|
static NTSTATUS probe_volume(void* data, ULONG length, KPROCESSOR_MODE processor_mode) {
|
|
|
|
MOUNTDEV_NAME* mdn = (MOUNTDEV_NAME*)data;
|
|
|
|
UNICODE_STRING path, pnp_name;
|
|
|
|
NTSTATUS Status;
|
|
|
|
PDEVICE_OBJECT DeviceObject;
|
|
|
|
PFILE_OBJECT FileObject;
|
|
|
|
const GUID* guid;
|
|
|
|
|
|
|
|
if (length < sizeof(MOUNTDEV_NAME))
|
|
|
|
return STATUS_INVALID_PARAMETER;
|
|
|
|
|
|
|
|
if (length < offsetof(MOUNTDEV_NAME, Name[0]) + mdn->NameLength)
|
|
|
|
return STATUS_INVALID_PARAMETER;
|
|
|
|
|
2020-04-23 02:38:57 +00:00
|
|
|
TRACE("%.*S\n", (int)(mdn->NameLength / sizeof(WCHAR)), mdn->Name);
|
2017-09-08 08:02:43 +00:00
|
|
|
|
|
|
|
if (!SeSinglePrivilegeCheck(RtlConvertLongToLuid(SE_MANAGE_VOLUME_PRIVILEGE), processor_mode))
|
|
|
|
return STATUS_PRIVILEGE_NOT_HELD;
|
|
|
|
|
|
|
|
path.Buffer = mdn->Name;
|
|
|
|
path.Length = path.MaximumLength = mdn->NameLength;
|
|
|
|
|
|
|
|
Status = IoGetDeviceObjectPointer(&path, FILE_READ_ATTRIBUTES, &FileObject, &DeviceObject);
|
|
|
|
if (!NT_SUCCESS(Status)) {
|
2020-04-23 02:38:57 +00:00
|
|
|
ERR("IoGetDeviceObjectPointer returned %08lx\n", Status);
|
2017-09-08 08:02:43 +00:00
|
|
|
return Status;
|
|
|
|
}
|
|
|
|
|
|
|
|
Status = get_device_pnp_name(DeviceObject, &pnp_name, &guid);
|
|
|
|
if (!NT_SUCCESS(Status)) {
|
2020-04-23 02:38:57 +00:00
|
|
|
ERR("get_device_pnp_name returned %08lx\n", Status);
|
2017-09-08 08:02:43 +00:00
|
|
|
ObDereferenceObject(FileObject);
|
|
|
|
return Status;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (RtlCompareMemory(guid, &GUID_DEVINTERFACE_DISK, sizeof(GUID)) == sizeof(GUID)) {
|
2019-09-01 12:53:20 +00:00
|
|
|
Status = dev_ioctl(DeviceObject, IOCTL_DISK_UPDATE_PROPERTIES, NULL, 0, NULL, 0, true, NULL);
|
2017-09-08 08:02:43 +00:00
|
|
|
if (!NT_SUCCESS(Status))
|
2020-04-23 02:38:57 +00:00
|
|
|
WARN("IOCTL_DISK_UPDATE_PROPERTIES returned %08lx\n", Status);
|
2017-09-08 08:02:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ObDereferenceObject(FileObject);
|
|
|
|
|
|
|
|
volume_removal(drvobj, &pnp_name);
|
|
|
|
|
|
|
|
if (RtlCompareMemory(guid, &GUID_DEVINTERFACE_DISK, sizeof(GUID)) == sizeof(GUID))
|
|
|
|
disk_arrival(drvobj, &pnp_name);
|
|
|
|
else
|
|
|
|
volume_arrival(drvobj, &pnp_name);
|
|
|
|
|
|
|
|
return STATUS_SUCCESS;
|
|
|
|
}
|
|
|
|
|
2019-11-12 18:32:46 +00:00
|
|
|
static NTSTATUS ioctl_unload(PIRP Irp) {
|
|
|
|
if (!SeSinglePrivilegeCheck(RtlConvertLongToLuid(SE_LOAD_DRIVER_PRIVILEGE), Irp->RequestorMode)) {
|
|
|
|
ERR("insufficient privileges\n");
|
|
|
|
return STATUS_PRIVILEGE_NOT_HELD;
|
|
|
|
}
|
|
|
|
|
|
|
|
do_shutdown(Irp);
|
|
|
|
|
|
|
|
return STATUS_SUCCESS;
|
|
|
|
}
|
|
|
|
|
2017-01-01 17:12:12 +00:00
|
|
|
static NTSTATUS control_ioctl(PIRP Irp) {
|
|
|
|
PIO_STACK_LOCATION IrpSp = IoGetCurrentIrpStackLocation(Irp);
|
|
|
|
NTSTATUS Status;
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2017-01-01 17:12:12 +00:00
|
|
|
switch (IrpSp->Parameters.DeviceIoControl.IoControlCode) {
|
|
|
|
case IOCTL_BTRFS_QUERY_FILESYSTEMS:
|
2017-09-08 08:02:43 +00:00
|
|
|
Status = query_filesystems(map_user_buffer(Irp, NormalPagePriority), IrpSp->Parameters.FileSystemControl.OutputBufferLength);
|
2017-01-01 17:12:12 +00:00
|
|
|
break;
|
2017-09-08 08:02:43 +00:00
|
|
|
|
|
|
|
case IOCTL_BTRFS_PROBE_VOLUME:
|
|
|
|
Status = probe_volume(Irp->AssociatedIrp.SystemBuffer, IrpSp->Parameters.FileSystemControl.InputBufferLength, Irp->RequestorMode);
|
|
|
|
break;
|
|
|
|
|
2019-11-12 18:32:46 +00:00
|
|
|
case IOCTL_BTRFS_UNLOAD:
|
|
|
|
Status = ioctl_unload(Irp);
|
|
|
|
break;
|
|
|
|
|
2017-01-01 17:12:12 +00:00
|
|
|
default:
|
2020-04-23 02:38:57 +00:00
|
|
|
TRACE("unhandled ioctl %lx\n", IrpSp->Parameters.DeviceIoControl.IoControlCode);
|
2017-01-01 17:12:12 +00:00
|
|
|
Status = STATUS_NOT_IMPLEMENTED;
|
|
|
|
break;
|
|
|
|
}
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2017-01-01 17:12:12 +00:00
|
|
|
return Status;
|
|
|
|
}
|
|
|
|
|
2017-09-08 08:02:43 +00:00
|
|
|
_Dispatch_type_(IRP_MJ_DEVICE_CONTROL)
|
|
|
|
_Function_class_(DRIVER_DISPATCH)
|
2019-09-01 12:53:20 +00:00
|
|
|
NTSTATUS __stdcall drv_device_control(IN PDEVICE_OBJECT DeviceObject, IN PIRP Irp) {
|
2016-10-29 17:05:10 +00:00
|
|
|
NTSTATUS Status;
|
|
|
|
PIO_STACK_LOCATION IrpSp = IoGetCurrentIrpStackLocation(Irp);
|
|
|
|
device_extension* Vcb = DeviceObject->DeviceExtension;
|
2019-09-01 12:53:20 +00:00
|
|
|
bool top_level;
|
2016-10-29 17:05:10 +00:00
|
|
|
|
|
|
|
FsRtlEnterFileSystem();
|
|
|
|
|
|
|
|
top_level = is_top_level(Irp);
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2016-10-29 17:05:10 +00:00
|
|
|
Irp->IoStatus.Information = 0;
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2017-01-01 17:12:12 +00:00
|
|
|
if (Vcb) {
|
2017-09-08 08:02:43 +00:00
|
|
|
if (Vcb->type == VCB_TYPE_CONTROL) {
|
2017-01-01 17:12:12 +00:00
|
|
|
Status = control_ioctl(Irp);
|
|
|
|
goto end;
|
2017-09-08 08:02:43 +00:00
|
|
|
} else if (Vcb->type == VCB_TYPE_VOLUME) {
|
|
|
|
Status = vol_device_control(DeviceObject, Irp);
|
|
|
|
goto end;
|
|
|
|
} else if (Vcb->type != VCB_TYPE_FS) {
|
|
|
|
Status = STATUS_INVALID_PARAMETER;
|
|
|
|
goto end;
|
2017-01-01 17:12:12 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
Status = STATUS_INVALID_PARAMETER;
|
|
|
|
goto end;
|
|
|
|
}
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2016-10-29 17:05:10 +00:00
|
|
|
switch (IrpSp->Parameters.DeviceIoControl.IoControlCode) {
|
|
|
|
case IOCTL_MOUNTDEV_QUERY_STABLE_GUID:
|
|
|
|
Status = mountdev_query_stable_guid(Vcb, Irp);
|
|
|
|
goto end;
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2016-10-29 17:05:10 +00:00
|
|
|
case IOCTL_DISK_IS_WRITABLE:
|
2017-09-08 08:02:43 +00:00
|
|
|
Status = is_writable(Vcb);
|
2016-10-29 17:05:10 +00:00
|
|
|
goto end;
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2016-10-29 17:05:10 +00:00
|
|
|
default:
|
2020-04-23 02:38:57 +00:00
|
|
|
TRACE("unhandled control code %lx\n", IrpSp->Parameters.DeviceIoControl.IoControlCode);
|
2016-10-29 17:05:10 +00:00
|
|
|
break;
|
|
|
|
}
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2016-10-29 17:05:10 +00:00
|
|
|
IoSkipCurrentIrpStackLocation(Irp);
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2017-01-01 17:12:12 +00:00
|
|
|
Status = IoCallDriver(Vcb->Vpb->RealDevice, Irp);
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2016-10-29 17:05:10 +00:00
|
|
|
goto end2;
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2016-10-29 17:05:10 +00:00
|
|
|
end:
|
|
|
|
Irp->IoStatus.Status = Status;
|
|
|
|
|
|
|
|
if (Status != STATUS_PENDING)
|
|
|
|
IoCompleteRequest(Irp, IO_NO_INCREMENT);
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2016-10-29 17:05:10 +00:00
|
|
|
end2:
|
2020-04-23 02:38:57 +00:00
|
|
|
TRACE("returning %08lx\n", Status);
|
2017-09-08 08:02:43 +00:00
|
|
|
|
|
|
|
if (top_level)
|
2016-10-29 17:05:10 +00:00
|
|
|
IoSetTopLevelIrp(NULL);
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2016-10-29 17:05:10 +00:00
|
|
|
FsRtlExitFileSystem();
|
|
|
|
|
|
|
|
return Status;
|
|
|
|
}
|