-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathutil.c
568 lines (496 loc) · 16.7 KB
/
util.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
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
/*
* Copyright (c) 2019 SUSE LLC
*
* Licensed under LGPL-2.1 (see LICENSE)
*/
#include "common.h"
#include <initguid.h>
#include <ntddstor.h>
#include <devpkey.h>
#include "debug.h"
#include "scsi_driver_extensions.h"
#include "scsi_function.h"
#include "scsi_trace.h"
#include "srb_helper.h"
#include "userspace.h"
#include "util.h"
#include "options.h"
VOID DrainDeviceQueue(_In_ PWNBD_DISK_DEVICE Device,
_In_ BOOLEAN SubmittedRequests,
_In_ BOOLEAN CheckStaleConn)
{
PLIST_ENTRY Request;
PSRB_QUEUE_ELEMENT Element;
PLIST_ENTRY ListHead;
PKSPIN_LOCK ListLock;
if (SubmittedRequests) {
ListHead = &Device->SubmittedReqListHead;
ListLock = &Device->SubmittedReqListLock;
}
else {
ListHead = &Device->PendingReqListHead;
ListLock = &Device->PendingReqListLock;
}
UINT64 TimeNow = KeQueryInterruptTime();
BOOLEAN StaleConnDetected = FALSE;
BOOLEAN RemoveStaleConnections =
WnbdDriverOptions[OptRemoveStaleConnections].Value.Data.AsBool;
while ((Request = ExInterlockedRemoveHeadList(ListHead, ListLock)) != NULL) {
Element = CONTAINING_RECORD(Request, SRB_QUEUE_ELEMENT, Link);
SrbSetDataTransferLength(Element->Srb, 0);
SrbSetSrbStatus(Element->Srb, SRB_STATUS_ABORTED);
if (!Element->Aborted) {
Element->Aborted = 1;
if (SubmittedRequests)
InterlockedIncrement64(&Device->Stats.AbortedSubmittedIORequests);
else
InterlockedIncrement64(&Device->Stats.AbortedUnsubmittedIORequests);
}
// Storport resets the lun after hitting request timeouts. However,
// it never actually removes the disk. Having a stale disk can be
// troublesome, leading to an unresponsive host in certain situations
// (e.g. cache deadlocks, hanging persistent reservation
// requests, etc).
//
// For this reason, we'll detect stale connections and disconnect
// the disk. This feature along with the timeouts are configurable.
// By default, we'll consider a connection to be stale if at least one
// request older than 15s got aborted and if no IO reply was received
// in the last minute.
if (CheckStaleConn && !StaleConnDetected) {
UINT64 StaleReqTimeoutMs =
(UINT64) WnbdDriverOptions[OptStaleReqTimeoutMs].Value.Data.AsInt64;
if (!StaleReqTimeoutMs) {
StaleReqTimeoutMs = WNBD_DEFAULT_STALE_REQ_TIMEOUT_MS;
}
UINT64 StaleConnTimeoutMs =
(UINT64) WnbdDriverOptions[OptStaleConnTimeoutMs].Value.Data.AsInt64;
if (!StaleConnTimeoutMs) {
StaleConnTimeoutMs = WNBD_DEFAULT_STALE_CONN_TIMEOUT_MS;
}
if (Element->ReqTimestamp &&
Element->ReqTimestamp < TimeNow &&
StaleReqTimeoutMs < (
(TimeNow - Element->ReqTimestamp) / 10000) &&
Device->Stats.LastReplyTimestamp &&
Device->Stats.LastReplyTimestamp < TimeNow &&
StaleConnTimeoutMs < (
TimeNow - Device->Stats.LastReplyTimestamp) / 10000) {
StaleConnDetected = TRUE;
WNBD_LOG_WARN(
"Stale connection detected. "
"Time since last IO reply (ms): %lld."
"Time since the aborted request was issued (ms): %lld.",
(TimeNow - Device->Stats.LastReplyTimestamp) / 10000,
(TimeNow - Element->ReqTimestamp) / 10000);
}
}
CompleteRequest(Device, Element, TRUE);
}
if (StaleConnDetected) {
if (RemoveStaleConnections) {
WNBD_LOG_ERROR("Removing stale connection.");
WnbdDisconnectAsync(Device);
} else {
WNBD_LOG_WARN("Ignoring stale connection as per WNBD settings.");
}
}
}
BOOLEAN HasPendingAsyncRequests(_In_ PWNBD_DISK_DEVICE Device)
{
KIRQL IrqlSubmitted = { 0 };
KIRQL IrqlPending = { 0 };
KeAcquireSpinLock(&Device->SubmittedReqListLock, &IrqlSubmitted);
KeAcquireSpinLock(&Device->PendingReqListLock, &IrqlPending);
BOOLEAN HasRequests = FALSE;
if (!IsListEmpty(&Device->SubmittedReqListHead)) {
WNBD_LOG_DEBUG("pending submitted requests");
HasRequests = TRUE;
} else {
WNBD_LOG_DEBUG("no pending submitted requests");
}
if (!IsListEmpty(&Device->PendingReqListHead)) {
WNBD_LOG_DEBUG("pending unsubmitted requests");
HasRequests = TRUE;
} else {
WNBD_LOG_DEBUG("no pending unsubmitted requests");
}
KeReleaseSpinLock(&Device->PendingReqListLock, IrqlPending);
KeReleaseSpinLock(&Device->SubmittedReqListLock, IrqlSubmitted);
return HasRequests;
}
VOID
WnbdCleanupAllDevices(_In_ PWNBD_EXTENSION DeviceExtension)
{
KeSetEvent(&DeviceExtension->GlobalDeviceRemovalEvent, IO_NO_INCREMENT, FALSE);
// The rundown protection is a device reference count. We're going to wait
// for them to be removed after signaling the global device removal event.
ExWaitForRundownProtectionRelease(&DeviceExtension->RundownProtection);
}
BOOLEAN
WnbdAcquireDevice(_In_ PWNBD_DISK_DEVICE Device)
{
BOOLEAN Acquired = FALSE;
// TODO: limit the scope of critical regions.
if (!Device)
return Acquired;
KIRQL Irql = KeGetCurrentIrql();
if (Irql <= APC_LEVEL) {
KeEnterCriticalRegion();
}
Acquired = ExAcquireRundownProtection(&Device->RundownProtection);
if (Irql <= APC_LEVEL) {
KeLeaveCriticalRegion();
}
return Acquired;
}
VOID
WnbdReleaseDevice(_In_ PWNBD_DISK_DEVICE Device)
{
if (!Device)
return;
KIRQL Irql = KeGetCurrentIrql();
if (Irql <= APC_LEVEL) {
KeEnterCriticalRegion();
}
ExReleaseRundownProtection(&Device->RundownProtection);
if (Irql <= APC_LEVEL) {
KeLeaveCriticalRegion();
}
}
// The returned device must be subsequently relased using WnbdReleaseDevice,
// if "Acquire" is set. Unacquired device pointers must not be dereferenced.
PWNBD_DISK_DEVICE
WnbdFindDeviceByAddr(
_In_ PWNBD_EXTENSION DeviceExtension,
_In_ UCHAR PathId,
_In_ UCHAR TargetId,
_In_ UCHAR Lun,
_In_ BOOLEAN Acquire)
{
ASSERT(DeviceExtension);
KIRQL Irql = { 0 };
KeAcquireSpinLock(&DeviceExtension->DeviceListLock, &Irql);
PWNBD_DISK_DEVICE Device = NULL;
for (PLIST_ENTRY Entry = DeviceExtension->DeviceList.Flink;
Entry != &DeviceExtension->DeviceList; Entry = Entry->Flink)
{
Device = (PWNBD_DISK_DEVICE) CONTAINING_RECORD(Entry, WNBD_DISK_DEVICE, ListEntry);
if (Device->Bus == PathId
&& Device->Target == TargetId
&& Device->Lun == Lun)
{
if (Acquire && !WnbdAcquireDevice(Device)) {
WNBD_LOG_DEBUG("Found device but couldn't acquire reference. "
"It's probably being removed.");
Device = NULL;
}
break;
}
Device = NULL;
}
KeReleaseSpinLock(&DeviceExtension->DeviceListLock, Irql);
return Device;
}
// The returned device must be subsequently relased using WnbdReleaseDevice,
// if "Acquire" is set. Unacquired device pointers must not be dereferenced.
PWNBD_DISK_DEVICE
WnbdFindDeviceByConnId(
_In_ PWNBD_EXTENSION DeviceExtension,
_In_ UINT64 ConnectionId,
_In_ BOOLEAN Acquire)
{
ASSERT(DeviceExtension);
KIRQL Irql = { 0 };
KeAcquireSpinLock(&DeviceExtension->DeviceListLock, &Irql);
PWNBD_DISK_DEVICE Device = NULL;
for (PLIST_ENTRY Entry = DeviceExtension->DeviceList.Flink;
Entry != &DeviceExtension->DeviceList; Entry = Entry->Flink)
{
Device = (PWNBD_DISK_DEVICE) CONTAINING_RECORD(Entry, WNBD_DISK_DEVICE, ListEntry);
if (Device->ConnectionId == ConnectionId) {
if (Acquire && !WnbdAcquireDevice(Device))
Device = NULL;
break;
}
Device = NULL;
}
KeReleaseSpinLock(&DeviceExtension->DeviceListLock, Irql);
return Device;
}
// The returned device must be subsequently relased using WnbdReleaseDevice,
// if "Acquire" is set. Unacquired device pointers must not be dereferenced.
PWNBD_DISK_DEVICE
WnbdFindDeviceByInstanceName(
_In_ PWNBD_EXTENSION DeviceExtension,
_In_ PCHAR InstanceName,
_In_ BOOLEAN Acquire)
{
ASSERT(DeviceExtension);
KIRQL Irql = { 0 };
KeAcquireSpinLock(&DeviceExtension->DeviceListLock, &Irql);
PWNBD_DISK_DEVICE Device = NULL;
for (PLIST_ENTRY Entry = DeviceExtension->DeviceList.Flink;
Entry != &DeviceExtension->DeviceList; Entry = Entry->Flink)
{
Device = (PWNBD_DISK_DEVICE) CONTAINING_RECORD(Entry, WNBD_DISK_DEVICE, ListEntry);
if (!strcmp((CONST CHAR*)&Device->Properties.InstanceName, InstanceName)) {
if (Acquire && !WnbdAcquireDevice(Device))
Device = NULL;
break;
}
Device = NULL;
}
KeReleaseSpinLock(&DeviceExtension->DeviceListLock, Irql);
return Device;
}
VOID
WnbdDisconnectAsync(PWNBD_DISK_DEVICE Device)
{
ASSERT(Device);
Device->HardRemoveDevice = TRUE;
KeSetEvent(&Device->DeviceRemovalEvent, IO_NO_INCREMENT, FALSE);
}
// The specified device must be acquired. It will be released by
// WnbdDisconnectSync.
VOID
WnbdDisconnectSync(_In_ PWNBD_DISK_DEVICE Device)
{
// We're holding a device reference, preventing it from being
// cleaned up while we're accessing it.
PVOID DeviceMonitorThread = Device->DeviceMonitorThread;
// Make sure that the thread handle stays valid.
ObReferenceObject(DeviceMonitorThread);
KeSetEvent(&Device->DeviceRemovalEvent, IO_NO_INCREMENT, FALSE);
// It's very important to release our device reference, allowing it to be removed.
// Do not access the device after releasing it.
WnbdReleaseDevice(Device);
KeWaitForSingleObject(DeviceMonitorThread, Executive, KernelMode, FALSE, NULL);
ObDereferenceObject(DeviceMonitorThread);
}
BOOLEAN
IsReadSrb(_In_ PVOID Srb)
{
PCDB Cdb = SrbGetCdb(Srb);
if(!Cdb) {
return FALSE;
}
switch (Cdb->AsByte[0]) {
case SCSIOP_READ6:
case SCSIOP_READ:
case SCSIOP_READ12:
case SCSIOP_READ16:
return TRUE;
default:
return FALSE;
}
}
BOOLEAN
IsPerResInSrb(_In_ PVOID Srb)
{
PCDB Cdb = SrbGetCdb(Srb);
if (!Cdb) {
return FALSE;
}
return Cdb->AsByte[0] == SCSIOP_PERSISTENT_RESERVE_IN;
}
BOOLEAN
ValidateScsiRequest(
_In_ PWNBD_DISK_DEVICE Device,
_In_ PSRB_QUEUE_ELEMENT Element)
{
PCDB Cdb = SrbGetCdb(Element->Srb);
if (!Cdb) {
WNBD_LOG_ERROR("Missing CDB.");
return FALSE;
}
int ScsiOp = Cdb->AsByte[0];
int WnbdReqType = ScsiOpToWnbdReqType(ScsiOp);
PWNBD_PROPERTIES DevProps = &Device->Properties;
switch (WnbdReqType) {
case WnbdReqTypeUnmap:
case WnbdReqTypeWrite:
case WnbdReqTypeFlush:
case WnbdReqTypePersistResOut:
if (DevProps->Flags.ReadOnly) {
WNBD_LOG_DEBUG(
"Write, flush, trim or PR out requested "
"on a read-only disk.");
return FALSE;
}
case WnbdReqTypePersistResIn:
case WnbdReqTypeRead:
break;
default:
WNBD_LOG_DEBUG("Unsupported SCSI operation: %d.", ScsiOp);
return FALSE;
}
switch (WnbdReqType) {
case WnbdReqTypeUnmap:
if (!DevProps->Flags.UnmapSupported) {
WNBD_LOG_DEBUG("The backend doesn't accept TRIM/UNMAP.");
return FALSE;
}
break;
case WnbdReqTypeFlush:
if (!DevProps->Flags.FlushSupported) {
WNBD_LOG_DEBUG("The backend doesn't accept flush requests");
return FALSE;
}
break;
case WnbdReqTypePersistResIn:
case WnbdReqTypePersistResOut:
if (!DevProps->Flags.PersistResSupported) {
WNBD_LOG_DEBUG(
"The backend doesn't accept persistent reservations");
return FALSE;
}
break;
}
return TRUE;
}
void SetSrbStatus(
PVOID Srb,
PWNBD_STATUS Status)
{
UCHAR SrbStatus = SRB_STATUS_ERROR;
PSENSE_DATA SenseInfoBuffer = SrbGetSenseInfoBuffer(Srb);
UCHAR SenseInfoBufferLength = SrbGetSenseInfoBufferLength(Srb);
SrbSetScsiStatus(Srb, Status->ScsiStatus);
if (SenseInfoBuffer && sizeof(SENSE_DATA) <= SenseInfoBufferLength &&
!(SrbGetSrbFlags(Srb) & SRB_FLAGS_DISABLE_AUTOSENSE))
{
RtlZeroMemory(SenseInfoBuffer, SenseInfoBufferLength);
SenseInfoBuffer->ErrorCode = SCSI_SENSE_ERRORCODE_FIXED_CURRENT;
SenseInfoBuffer->SenseKey = Status->SenseKey;
SenseInfoBuffer->AdditionalSenseCode = Status->ASC;
SenseInfoBuffer->AdditionalSenseCodeQualifier = Status->ASCQ;
SenseInfoBuffer->AdditionalSenseLength = sizeof(SENSE_DATA) -
RTL_SIZEOF_THROUGH_FIELD(SENSE_DATA, AdditionalSenseLength);
if (Status->InformationValid)
{
// TODO: should we use REVERSE_BYTES_8? What's the expected endianness?
(*(PUINT64)SenseInfoBuffer->Information) = Status->Information;
SenseInfoBuffer->Valid = 1;
}
// We'll avoid overriding non-zero scsi status
if (!Status->ScsiStatus) {
SrbSetScsiStatus(Srb, SCSISTAT_CHECK_CONDITION);
}
SrbStatus |= SRB_STATUS_AUTOSENSE_VALID;
}
SrbSetSrbStatus(Srb, SrbStatus);
}
VOID CompleteRequest(
_In_ PWNBD_DISK_DEVICE Device,
_In_ PSRB_QUEUE_ELEMENT Element,
_In_ BOOLEAN FreeElement)
{
// We must be very careful not to complete the same SRB twice in order
// to avoid crashes.
if (!InterlockedExchange8((CHAR*)&Element->Completed, TRUE)) {
WNBD_LOG_DEBUG(
"Notifying StorPort of completion of %p 0x%llx status: 0x%x(%s)",
Element->Srb, Element->Tag, SrbGetSrbStatus(Element->Srb),
WnbdToStringSrbStatus(SrbGetSrbStatus(Element->Srb)));
StorPortNotification(RequestComplete, Element->DeviceExtension, Element->Srb);
InterlockedDecrement64(&Device->Stats.OutstandingIOCount);
}
if (FreeElement) {
ExFreePool(Element);
}
}
VOID WnbdSendIoctl(
ULONG ControlCode,
PDEVICE_OBJECT DeviceObject,
PVOID InputBuffer,
ULONG InputBufferLength,
PVOID OutputBuffer,
ULONG OutputBufferLength,
PIO_STATUS_BLOCK IoStatus)
{
ASSERT(!KeAreAllApcsDisabled());
KEVENT Event;
KeInitializeEvent(&Event, NotificationEvent, FALSE);
PIRP Irp = IoBuildDeviceIoControlRequest(
ControlCode,
DeviceObject,
InputBuffer,
InputBufferLength,
OutputBuffer,
OutputBufferLength,
FALSE,
&Event,
IoStatus);
if (!Irp)
{
IoStatus->Information = 0;
IoStatus->Status = STATUS_INSUFFICIENT_RESOURCES;
return;
}
NTSTATUS Result = IoCallDriver(DeviceObject, Irp);
if (NT_ERROR(Result))
{
IoStatus->Status = Result;
IoStatus->Information = 0;
}
if (STATUS_PENDING == Result) {
KeWaitForSingleObject(&Event, Executive, KernelMode, FALSE, 0);
}
}
NTSTATUS WnbdGetScsiAddress(
PDEVICE_OBJECT DeviceObject,
PSCSI_ADDRESS ScsiAddress)
{
IO_STATUS_BLOCK IoStatus = { 0 };
RtlZeroMemory(ScsiAddress, sizeof(SCSI_ADDRESS));
WnbdSendIoctl(
IOCTL_SCSI_GET_ADDRESS,
DeviceObject,
0, 0,
ScsiAddress, sizeof(SCSI_ADDRESS),
&IoStatus);
if (!NT_SUCCESS(IoStatus.Status))
return IoStatus.Status;
if (IoStatus.Information < sizeof(SCSI_ADDRESS))
return STATUS_OBJECT_NAME_NOT_FOUND;
return STATUS_SUCCESS;
}
NTSTATUS WnbdGetDiskInstancePath(
PDEVICE_OBJECT DeviceObject,
PWSTR Buffer,
DWORD BufferSize,
PULONG RequiredBufferSize)
{
DEVPROPKEY PropertyKey = DEVPKEY_Device_InstanceId;
DEVPROPTYPE ReturnedType;
NTSTATUS Status = IoGetDevicePropertyData(
DeviceObject,
&PropertyKey,
LOCALE_NEUTRAL,
0,
BufferSize,
Buffer,
RequiredBufferSize,
&ReturnedType);
return Status;
}
NTSTATUS WnbdGetDiskNumber(
PDEVICE_OBJECT DeviceObject,
PULONG DiskNumber)
{
IO_STATUS_BLOCK IoStatus = { 0 };
STORAGE_DEVICE_NUMBER DeviceData = { 0 };
WnbdSendIoctl(
IOCTL_STORAGE_GET_DEVICE_NUMBER,
DeviceObject,
0, 0,
&DeviceData, sizeof(STORAGE_DEVICE_NUMBER),
&IoStatus);
if (!NT_SUCCESS(IoStatus.Status))
return IoStatus.Status;
if (IoStatus.Information < sizeof(DeviceData))
return STATUS_OBJECT_NAME_NOT_FOUND;
*DiskNumber = DeviceData.DeviceNumber;
return STATUS_SUCCESS;
}