-
Notifications
You must be signed in to change notification settings - Fork 53
/
stgunit.c
469 lines (395 loc) · 13.2 KB
/
stgunit.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
/**
* @file sys/stgunit.c
*
* @copyright 2018-2020 Bill Zissimopoulos
*/
/*
* This file is part of WinSpd.
*
* You can redistribute it and/or modify it under the terms of the GNU
* General Public License version 3 as published by the Free Software
* Foundation.
*
* Licensees holding a valid commercial license may use this software
* in accordance with the commercial license agreement provided in
* conjunction with the software. The terms and conditions of any such
* commercial license agreement shall govern, supersede, and render
* ineffective any application of the GPLv3 license to this software,
* notwithstanding of any reference thereto in the software or
* associated repository.
*/
#include <sys/driver.h>
ERESOURCE SpdGlobalDeviceResource;
SPD_DEVICE_EXTENSION *SpdGlobalDeviceExtension;
ULONG SpdStorageUnitCapacity = SPD_IOCTL_STORAGE_UNIT_CAPACITY;
static VOID SpdDeviceExtensionNotifyRoutine(HANDLE ParentId, HANDLE ProcessId0, BOOLEAN Create);
NTSTATUS SpdDeviceExtensionInit(SPD_DEVICE_EXTENSION *DeviceExtension, PVOID BusInformation)
{
ASSERT(PASSIVE_LEVEL == KeGetCurrentIrql());
ASSERT(0 != DeviceExtension);
NTSTATUS Result;
KeEnterCriticalRegion();
ExAcquireResourceExclusiveLite(&SpdGlobalDeviceResource, TRUE);
if (0 != SpdGlobalDeviceExtension)
{
Result = DeviceExtension == SpdGlobalDeviceExtension ?
STATUS_SUCCESS : STATUS_INVALID_PARAMETER;
goto exit;
}
Result = PsSetCreateProcessNotifyRoutine(SpdDeviceExtensionNotifyRoutine, FALSE);
if (!NT_SUCCESS(Result))
goto exit;
KeInitializeSpinLock(&DeviceExtension->SpinLock);
DeviceExtension->DeviceObject = BusInformation;
DeviceExtension->StorageUnitCapacity = SpdStorageUnitCapacity;
SpdGlobalDeviceExtension = DeviceExtension;
Result = STATUS_SUCCESS;
exit:
ExReleaseResourceLite(&SpdGlobalDeviceResource);
KeLeaveCriticalRegion();
return Result;
}
VOID SpdDeviceExtensionFini(SPD_DEVICE_EXTENSION *DeviceExtension)
{
ASSERT(PASSIVE_LEVEL == KeGetCurrentIrql());
ASSERT(0 != DeviceExtension);
KeEnterCriticalRegion();
ExAcquireResourceExclusiveLite(&SpdGlobalDeviceResource, TRUE);
if (DeviceExtension == SpdGlobalDeviceExtension)
{
PsSetCreateProcessNotifyRoutine(SpdDeviceExtensionNotifyRoutine, TRUE);
SpdGlobalDeviceExtension = 0;
}
ExReleaseResourceLite(&SpdGlobalDeviceResource);
KeLeaveCriticalRegion();
}
static VOID SpdDeviceExtensionNotifyRoutine(HANDLE ParentId, HANDLE ProcessId0, BOOLEAN Create)
{
ASSERT(PASSIVE_LEVEL == KeGetCurrentIrql());
if (Create)
return;
ULONG ProcessId = (ULONG)(UINT_PTR)ProcessId0;
UINT8 Bitmap[32];
ULONG Count;
KeEnterCriticalRegion();
ExAcquireResourceSharedLite(&SpdGlobalDeviceResource, TRUE);
ASSERT(0 != SpdGlobalDeviceExtension);
Count = SpdStorageUnitGetUseBitmap(SpdGlobalDeviceExtension, &ProcessId, Bitmap);
for (ULONG I = 0; 0 < Count && sizeof Bitmap * 8 > I; I++)
if (FlagOn(Bitmap[I >> 3], 1 << (I & 7)))
{
SpdStorageUnitUnprovision(SpdGlobalDeviceExtension, 0, I, ProcessId);
Count--;
}
ExReleaseResourceLite(&SpdGlobalDeviceResource);
KeLeaveCriticalRegion();
}
SPD_DEVICE_EXTENSION *SpdDeviceExtensionAcquire(VOID)
{
KeEnterCriticalRegion();
ExAcquireResourceSharedLite(&SpdGlobalDeviceResource, TRUE);
if (0 != SpdGlobalDeviceExtension)
return SpdGlobalDeviceExtension;
ExReleaseResourceLite(&SpdGlobalDeviceResource);
KeLeaveCriticalRegion();
return 0;
}
VOID SpdDeviceExtensionRelease(SPD_DEVICE_EXTENSION *DeviceExtension)
{
ExReleaseResourceLite(&SpdGlobalDeviceResource);
KeLeaveCriticalRegion();
}
NTSTATUS SpdStorageUnitProvision(
SPD_DEVICE_EXTENSION *DeviceExtension,
SPD_IOCTL_STORAGE_UNIT_PARAMS *StorageUnitParams,
ULONG ProcessId,
PUINT32 PBtl)
{
ASSERT(PASSIVE_LEVEL == KeGetCurrentIrql());
ASSERT(0 != DeviceExtension);
NTSTATUS Result;
CHAR SerialNumber[RTL_FIELD_SIZE(SPD_STORAGE_UNIT, SerialNumber) + 1];
SPD_STORAGE_UNIT *StorageUnit = 0;
SPD_STORAGE_UNIT *DuplicateUnit;
UINT32 Btl;
KIRQL Irql;
*PBtl = (UINT32)-1;
StorageUnit = SpdAllocNonPaged(sizeof *StorageUnit, SpdTagStorageUnit);
if (0 == StorageUnit)
{
Result = STATUS_INSUFFICIENT_RESOURCES;
goto exit;
}
RtlZeroMemory(StorageUnit, sizeof *StorageUnit);
StorageUnit->RefCount = 1;
RtlCopyMemory(&StorageUnit->StorageUnitParams, StorageUnitParams,
sizeof *StorageUnitParams);
/* "left align" ProductId except that we allow all-NUL for testing */
if ('\0' != StorageUnit->StorageUnitParams.ProductId[0])
for (UCHAR *P = StorageUnit->StorageUnitParams.ProductId,
*EndP = P + sizeof StorageUnit->StorageUnitParams.ProductId,
Spaces = FALSE;
EndP > P; P++)
{
if (Spaces || ' ' > *P || *P >= 0x7f)
{
*P = ' ';
Spaces = TRUE;
}
}
/* "left align" ProductRevisionLevel except that we allow all-NUL for testing */
if ('\0' != StorageUnit->StorageUnitParams.ProductRevisionLevel[0])
for (UCHAR *P = StorageUnit->StorageUnitParams.ProductRevisionLevel,
*EndP = P + sizeof StorageUnit->StorageUnitParams.ProductRevisionLevel,
Spaces = FALSE;
EndP > P; P++)
{
if (Spaces || ' ' > *P || *P >= 0x7f)
{
*P = ' ';
Spaces = TRUE;
}
}
#define Guid StorageUnit->StorageUnitParams.Guid
RtlStringCbPrintfA(SerialNumber, sizeof SerialNumber,
"%08lx-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x",
Guid.Data1, Guid.Data2, Guid.Data3,
Guid.Data4[0], Guid.Data4[1], Guid.Data4[2], Guid.Data4[3],
Guid.Data4[4], Guid.Data4[5], Guid.Data4[6], Guid.Data4[7]);
#undef Guid
RtlCopyMemory(StorageUnit->SerialNumber, SerialNumber, sizeof StorageUnit->SerialNumber);
StorageUnit->OwnerProcessId = ProcessId;
StorageUnit->TransactProcessId = ProcessId;
Result = SpdIoqCreate(DeviceExtension, &StorageUnit->Ioq);
if (!NT_SUCCESS(Result))
goto exit;
KeAcquireSpinLock(&DeviceExtension->SpinLock, &Irql);
DuplicateUnit = 0;
Btl = (UINT32)-1;
for (ULONG I = 0; DeviceExtension->StorageUnitCapacity > I; I++)
{
SPD_STORAGE_UNIT *Unit = DeviceExtension->StorageUnits[I];
if (0 == Unit)
{
if ((UINT32)-1 == Btl)
Btl = SPD_BTL_FROM_INDEX(I);
continue;
}
if (RtlEqualMemory(&StorageUnit->StorageUnitParams.Guid, &Unit->StorageUnitParams.Guid,
sizeof Unit->StorageUnitParams.Guid))
{
DuplicateUnit = Unit;
break;
}
}
if (0 == DuplicateUnit && -1 != Btl)
{
DeviceExtension->StorageUnits[SPD_INDEX_FROM_BTL(Btl)] = StorageUnit;
DeviceExtension->StorageUnitCount++;
}
KeReleaseSpinLock(&DeviceExtension->SpinLock, Irql);
if (0 != DuplicateUnit)
{
Result = STATUS_OBJECT_NAME_COLLISION;
goto exit;
}
if (-1 == Btl)
{
Result = STATUS_CANNOT_MAKE;
goto exit;
}
StorPortNotification(BusChangeDetected, DeviceExtension, (UCHAR)0);
*PBtl = Btl;
Result = STATUS_SUCCESS;
exit:
if (!NT_SUCCESS(Result))
{
if (0 != StorageUnit->Ioq)
SpdIoqDelete(StorageUnit->Ioq);
if (0 != StorageUnit)
SpdFree(StorageUnit, SpdTagStorageUnit);
}
return Result;
}
NTSTATUS SpdStorageUnitUnprovision(
SPD_DEVICE_EXTENSION *DeviceExtension,
PGUID Guid, ULONG Index,
ULONG ProcessId)
{
ASSERT(PASSIVE_LEVEL == KeGetCurrentIrql());
ASSERT(0 != DeviceExtension);
NTSTATUS Result;
SPD_STORAGE_UNIT *StorageUnit;
KIRQL Irql;
KeAcquireSpinLock(&DeviceExtension->SpinLock, &Irql);
StorageUnit = 0;
if (0 != Guid)
{
for (ULONG I = 0; DeviceExtension->StorageUnitCapacity > I; I++)
{
SPD_STORAGE_UNIT *Unit = DeviceExtension->StorageUnits[I];
if (0 == Unit)
continue;
if (RtlEqualMemory(Guid, &Unit->StorageUnitParams.Guid,
sizeof Unit->StorageUnitParams.Guid))
{
StorageUnit = Unit;
Index = I;
break;
}
}
}
else
{
if (DeviceExtension->StorageUnitCapacity > Index)
StorageUnit = DeviceExtension->StorageUnits[Index];
}
if (0 != StorageUnit && ProcessId == StorageUnit->OwnerProcessId)
{
DeviceExtension->StorageUnitCount--;
DeviceExtension->StorageUnits[Index] = 0;
}
KeReleaseSpinLock(&DeviceExtension->SpinLock, Irql);
if (0 == StorageUnit)
{
Result = STATUS_OBJECT_NAME_NOT_FOUND;
goto exit;
}
if (ProcessId != StorageUnit->OwnerProcessId)
{
Result = STATUS_ACCESS_DENIED;
goto exit;
}
/* stop the ioq and dereference the storage unit */
SpdIoqReset(StorageUnit->Ioq, TRUE);
SpdStorageUnitDereference(DeviceExtension, StorageUnit);
StorPortNotification(BusChangeDetected, DeviceExtension, (UCHAR)0);
Result = STATUS_SUCCESS;
exit:
return Result;
}
SPD_STORAGE_UNIT *SpdStorageUnitReferenceByBtl(
SPD_DEVICE_EXTENSION *DeviceExtension,
UINT32 Btl)
{
SPD_STORAGE_UNIT *StorageUnit;
UINT8 B, T, L;
KIRQL Irql;
B = SPD_IOCTL_BTL_B(Btl);
T = SPD_IOCTL_BTL_T(Btl);
L = SPD_IOCTL_BTL_L(Btl);
if (0 != B || 0 != L)
return 0;
KeAcquireSpinLock(&DeviceExtension->SpinLock, &Irql);
StorageUnit = DeviceExtension->StorageUnitCapacity > T ? DeviceExtension->StorageUnits[T] : 0;
if (0 != StorageUnit)
StorageUnit->RefCount++;
KeReleaseSpinLock(&DeviceExtension->SpinLock, Irql);
return StorageUnit;
}
static SPD_STORAGE_UNIT *SpdStorageUnitReferenceByDevice(
SPD_DEVICE_EXTENSION *DeviceExtension,
PDEVICE_OBJECT DeviceObject)
{
SPD_STORAGE_UNIT *StorageUnit;
KIRQL Irql;
KeAcquireSpinLock(&DeviceExtension->SpinLock, &Irql);
StorageUnit = 0;
for (ULONG I = 0; DeviceExtension->StorageUnitCapacity > I; I++)
{
SPD_STORAGE_UNIT *Unit = DeviceExtension->StorageUnits[I];
if (0 == Unit)
continue;
if (DeviceObject == Unit->DeviceObject)
{
StorageUnit = Unit;
break;
}
}
if (0 != StorageUnit)
StorageUnit->RefCount++;
KeReleaseSpinLock(&DeviceExtension->SpinLock, Irql);
return StorageUnit;
}
VOID SpdStorageUnitDereference(
SPD_DEVICE_EXTENSION *DeviceExtension,
SPD_STORAGE_UNIT *StorageUnit)
{
BOOLEAN Delete;
KIRQL Irql;
KeAcquireSpinLock(&DeviceExtension->SpinLock, &Irql);
StorageUnit->RefCount--;
Delete = 0 == StorageUnit->RefCount;
KeReleaseSpinLock(&DeviceExtension->SpinLock, Irql);
if (Delete)
{
SpdIoqDelete(StorageUnit->Ioq);
SpdFree(StorageUnit, SpdTagStorageUnit);
}
}
ULONG SpdStorageUnitGetUseBitmap(
SPD_DEVICE_EXTENSION *DeviceExtension,
PULONG PProcessId,
UINT8 Bitmap[32])
{
ULONG Count = 0;
KIRQL Irql;
RtlZeroMemory(Bitmap, 32);
KeAcquireSpinLock(&DeviceExtension->SpinLock, &Irql);
for (ULONG I = 0;
DeviceExtension->StorageUnitCount > Count && DeviceExtension->StorageUnitCapacity > I;
I++)
{
SPD_STORAGE_UNIT *Unit = DeviceExtension->StorageUnits[I];
if (0 != Unit && (0 == PProcessId || *PProcessId == Unit->OwnerProcessId))
{
SetFlag(Bitmap[I >> 3], 1 << (I & 7));
Count++;
}
}
KeReleaseSpinLock(&DeviceExtension->SpinLock, Irql);
return Count;
}
NTSTATUS SpdStorageUnitGlobalSetDevice(
PDEVICE_OBJECT DeviceObject)
{
SPD_STORAGE_UNIT *StorageUnit;
SCSI_ADDRESS ScsiAddress;
NTSTATUS Result;
Result = SpdGetScsiAddress(DeviceObject, &ScsiAddress);
if (!NT_SUCCESS(Result))
return Result;
Result = STATUS_OBJECT_NAME_NOT_FOUND;
if (0 != SpdDeviceExtensionAcquire())
{
StorageUnit = SpdStorageUnitReferenceByBtl(SpdGlobalDeviceExtension,
SPD_IOCTL_BTL(ScsiAddress.PathId, ScsiAddress.TargetId, ScsiAddress.Lun));
if (0 != StorageUnit)
{
Result = 0 == InterlockedCompareExchangePointer(&StorageUnit->DeviceObject, DeviceObject, 0) ?
STATUS_SUCCESS : STATUS_OBJECT_NAME_COLLISION;
SpdStorageUnitDereference(SpdGlobalDeviceExtension, StorageUnit);
}
SpdDeviceExtensionRelease(SpdGlobalDeviceExtension);
}
return Result;
}
SPD_STORAGE_UNIT *SpdStorageUnitGlobalReferenceByDevice(
PDEVICE_OBJECT DeviceObject)
{
SPD_STORAGE_UNIT *StorageUnit = 0;
if (0 != SpdDeviceExtensionAcquire())
{
StorageUnit = SpdStorageUnitReferenceByDevice(SpdGlobalDeviceExtension,
DeviceObject);
if (0 == StorageUnit)
SpdDeviceExtensionRelease(SpdGlobalDeviceExtension);
}
return StorageUnit;
}
VOID SpdStorageUnitGlobalDereference(
SPD_STORAGE_UNIT *StorageUnit)
{
SpdStorageUnitDereference(SpdGlobalDeviceExtension, StorageUnit);
SpdDeviceExtensionRelease(SpdGlobalDeviceExtension);
}