-
Notifications
You must be signed in to change notification settings - Fork 26
/
driver.c
319 lines (286 loc) · 10.8 KB
/
driver.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
/*
* Copyright (c) 2019 SUSE LLC
*
* Licensed under LGPL-2.1 (see LICENSE)
*/
#include "common.h"
#include "debug.h"
#include "driver.h"
#include "scsi_driver_extensions.h"
#include "scsi_trace.h"
#include "userspace.h"
#include "util.h"
#include "options.h"
#include "events.h"
DRIVER_INITIALIZE DriverEntry;
DRIVER_UNLOAD WnbdDriverUnload;
DRIVER_DISPATCH WnbdDispatchPnp;
PDRIVER_UNLOAD StorPortDriverUnload;
PDRIVER_DISPATCH StorPortDispatchPnp;
PWNBD_EXTENSION GlobalExt = NULL;
extern HANDLE GlobalDrvRegHandle = NULL;
extern UINT32 GlobalLogLevel = 0;
_Use_decl_annotations_
NTSTATUS
DriverEntry(PDRIVER_OBJECT DriverObject,
PUNICODE_STRING RegistryPath)
{
ExInitializeDriverRuntime(DrvRtPoolNxOptIn);
/*
* Register with ETW
*/
EventRegisterWNBD();
WPP_INIT_TRACING(DriverObject, RegistryPath);
/*
* Register Virtual Storport Miniport data
*/
NTSTATUS Status;
HW_INITIALIZATION_DATA WnbdInitData = { 0 };
WnbdInitData.HwInitializationDataSize = sizeof(HW_INITIALIZATION_DATA);
GlobalLogLevel = 0;
/*
* Set our SCSI Driver Extensions
*/
WnbdInitData.HwAdapterControl = WnbdHwAdapterControl;
WnbdInitData.HwCompleteServiceIrp = 0;
WnbdInitData.HwFindAdapter = (PVOID)WnbdHwFindAdapter;
WnbdInitData.HwFreeAdapterResources = WnbdHwFreeAdapterResources;
WnbdInitData.HwInitialize = WnbdHwInitialize;
WnbdInitData.HwProcessServiceRequest = WnbdHwProcessServiceRequest;
WnbdInitData.HwResetBus = WnbdHwResetBus;
WnbdInitData.HwStartIo = WnbdHwStartIo;
WnbdInitData.AdapterInterfaceType = Internal;
WnbdInitData.MultipleRequestPerLu = TRUE;
WnbdInitData.PortVersionFlags = 0;
WnbdInitData.DeviceExtensionSize = sizeof(WNBD_EXTENSION);
WnbdInitData.SpecificLuExtensionSize = 0;
WnbdInitData.SrbExtensionSize = 0;
/*
* NOOP for virtual devices
*/
WnbdInitData.HwInterrupt = 0;
WnbdInitData.HwDmaStarted = 0;
WnbdInitData.HwAdapterState = 0;
WnbdInitData.MapBuffers = STOR_MAP_NON_READ_WRITE_BUFFERS;
WnbdInitData.TaggedQueuing = TRUE;
WnbdInitData.AutoRequestSense = TRUE;
WnbdInitData.MultipleRequestPerLu = TRUE;
WnbdInitData.FeatureSupport |=
STOR_FEATURE_VIRTUAL_MINIPORT |
STOR_FEATURE_FULL_PNP_DEVICE_CAPABILITIES |
// STOR_FEATURE_DEVICE_NAME_NO_SUFFIX |
STOR_FEATURE_ADAPTER_NOT_REQUIRE_IO_PORT;
WnbdInitData.SrbTypeFlags = SRB_TYPE_FLAG_STORAGE_REQUEST_BLOCK |
SRB_TYPE_FLAG_SCSI_REQUEST_BLOCK;
Status = IoOpenDriverRegistryKey(
DriverObject,
DriverRegKeyPersistentState,
GENERIC_READ | GENERIC_WRITE,
0,
&GlobalDrvRegHandle);
if (!NT_SUCCESS(Status)) {
WNBD_LOG_ERROR(
"Couldn't open driver registry key. Status: 0x%x", Status);
} else {
WnbdReloadPersistentOptions();
}
/*
* Register our driver
*/
Status = StorPortInitialize(DriverObject,
RegistryPath,
&WnbdInitData,
NULL);
if (!NT_SUCCESS(Status)) {
WNBD_LOG_ERROR("DriverEntry failure in call to StorPortInitialize. Status: 0x%x", Status);
ASSERT(FALSE);
return Status;
}
/*
* Set up PNP and Unload routines
*/
StorPortDriverUnload = DriverObject->DriverUnload;
DriverObject->DriverUnload = WnbdDriverUnload;
StorPortDispatchPnp = DriverObject->MajorFunction[IRP_MJ_PNP];
DriverObject->MajorFunction[IRP_MJ_PNP] = 0 != StorPortDispatchPnp ? WnbdDispatchPnp : 0;
GlobalExt = NULL;
/*
* Report status in upper layers
*/
return Status;
}
_Use_decl_annotations_
NTSTATUS
WnbdDispatchPnp(PDEVICE_OBJECT DeviceObject,
PIRP Irp)
{
ASSERT(Irp);
NTSTATUS Status = STATUS_INVALID_DEVICE_REQUEST;
PIO_STACK_LOCATION IoLocation = IoGetCurrentIrpStackLocation(Irp);
SCSI_ADDRESS ScsiAddress = { 0 };
ASSERT(IoLocation);
UCHAR MinorFunction = IoLocation->MinorFunction;
WNBD_LOG_DEBUG("Received PnP request: %s (%d).",
WnbdToStringPnpMinorFunction(MinorFunction), MinorFunction);
switch (MinorFunction) {
case IRP_MN_QUERY_CAPABILITIES:
IoLocation->Parameters.DeviceCapabilities.Capabilities->SilentInstall = 1;
// We're disabling SurpriseRemovalOK in order to
// receive device removal PnP events.
IoLocation->Parameters.DeviceCapabilities.Capabilities->SurpriseRemovalOK = 0;
IoLocation->Parameters.DeviceCapabilities.Capabilities->Removable = 1;
IoLocation->Parameters.DeviceCapabilities.Capabilities->EjectSupported = 1;
break;
case IRP_MN_START_DEVICE:
{
if (!GlobalExt) {
WNBD_LOG_DEBUG("IRP_MN_START_DEVICE received but the extension "
"hasn't been initialized.");
break;
}
Status = WnbdGetScsiAddress(DeviceObject, &ScsiAddress);
if (Status) {
WNBD_LOG_ERROR("Could not query SCSI address. Error: %d.", Status);
break;
}
WNBD_LOG_INFO("Starting device.");
PWNBD_DISK_DEVICE Device = WnbdFindDeviceByAddr(
GlobalExt, ScsiAddress.PathId,
ScsiAddress.TargetId, ScsiAddress.Lun, TRUE);
if (!Device) {
break;
}
Device->PDO = DeviceObject;
PDEVICE_OBJECT AttachedDisk = IoGetAttachedDeviceReference(DeviceObject);
if (AttachedDisk != DeviceObject) {
Status = WnbdGetDiskNumber(
AttachedDisk, (PULONG) &Device->DiskNumber);
if (Status) {
WNBD_LOG_WARN("Could not get disk number. Error: %d.",
Status);
}
}
else {
WNBD_LOG_WARN("Couldn't not get disk number. "
"Couldn't get attached PDO.");
}
ObDereferenceObject(AttachedDisk);
DWORD RequiredSize = 0;
Status = WnbdGetDiskInstancePath(
DeviceObject, Device->PNPDeviceID,
sizeof(Device->PNPDeviceID),
&RequiredSize);
if (Status) {
WNBD_LOG_WARN("Couldn't get PNP device id. Error: %d", Status);
}
WnbdReleaseDevice(Device);
}
break;
// We won't remove the device upon receiving IRP_MN_QUERY_REMOVE_DEVICE.
// The device removal might be vetoed by other parts of the storage stack,
// so we'd affect soft removals. The only downside is that if the remove
// gets vetoed, uninstalling the driver will require a reboot.
case IRP_MN_QUERY_REMOVE_DEVICE:
{
if (NULL == GlobalExt || !GlobalExt->DeviceCount) {
break;
}
Status = WnbdGetScsiAddress(DeviceObject, &ScsiAddress);
if (Status) {
WNBD_LOG_WARN("Could not query SCSI address. Error: 0x%x.", Status);
break;
}
WNBD_LOG_DEBUG("Device about to be removed, "
"checking for pending requests");
PWNBD_DISK_DEVICE Device = WnbdFindDeviceByAddr(
GlobalExt, ScsiAddress.PathId,
ScsiAddress.TargetId, ScsiAddress.Lun, TRUE);
if (!Device) {
WNBD_LOG_DEBUG("Device already removed.");
break;
}
char InstanceName[256] = { 0 };
// Copy InstanceName to a local buffer in order to log it
// without accessing the released device
RtlCopyMemory(&InstanceName, &Device->Properties.InstanceName,
strlen(Device->Properties.InstanceName));
if (Device->PDO != DeviceObject) {
WNBD_LOG_INFO(
"Different device found at the specified address. "
"The requested device might've been removed already.");
WnbdReleaseDevice(Device);
break;
}
BOOLEAN HasPendingRequests = HasPendingAsyncRequests(Device);
WnbdReleaseDevice(Device);
if (HasPendingRequests) {
Status = STATUS_DEVICE_BUSY;
WNBD_LOG_WARN("Device removal requested while having pending IO, "
"reporting device busy: %s", InstanceName);
Irp->IoStatus.Status = Status;
IoCompleteRequest(Irp, IO_NO_INCREMENT);
WNBD_LOG_DEBUG("Exit: 0x%x", Status);
return Status;
} else {
WNBD_LOG_INFO("Device removal requested, no pending IO: %s",
InstanceName);
}
}
break;
case IRP_MN_REMOVE_DEVICE:
{
if (NULL == GlobalExt || !GlobalExt->DeviceCount) {
break;
}
Status = WnbdGetScsiAddress(DeviceObject, &ScsiAddress);
if (Status) {
WNBD_LOG_WARN("Could not query SCSI address. Error: 0x%x.", Status);
break;
}
WNBD_LOG_DEBUG("Removing disk device.");
PWNBD_DISK_DEVICE Device = WnbdFindDeviceByAddr(
GlobalExt, ScsiAddress.PathId,
ScsiAddress.TargetId, ScsiAddress.Lun, TRUE);
if (!Device) {
WNBD_LOG_DEBUG("Device already removed.");
break;
}
char InstanceName[256] = { 0 };
// Copy InstanceName to a local buffer in order to log it
// without accessing the released device
RtlCopyMemory(&InstanceName, &Device->Properties.InstanceName,
strlen(Device->Properties.InstanceName));
if (Device->PDO != DeviceObject) {
WNBD_LOG_INFO(
"Different device found at the specified address. "
"The requested device might've been removed already.");
WnbdReleaseDevice(Device);
break;
}
WNBD_LOG_INFO("Disconnecting disk: %s.", InstanceName);
WnbdDisconnectSync(Device);
WNBD_LOG_INFO("Successfully disconnected disk: %s", InstanceName);
}
break;
}
Status = StorPortDispatchPnp(DeviceObject, Irp);
WNBD_LOG_DEBUG("Exit: 0x%x", Status);
return Status;
}
_Use_decl_annotations_
VOID
WnbdDriverUnload(PDRIVER_OBJECT DriverObject)
{
if (0 != StorPortDriverUnload) {
StorPortDriverUnload(DriverObject);
}
/*
* Unregister from ETW
*/
EventUnregisterWNBD();
WPP_CLEANUP(DriverObject);
if (GlobalDrvRegHandle) {
ZwClose(GlobalDrvRegHandle);
GlobalDrvRegHandle = NULL;
}
}