-
Notifications
You must be signed in to change notification settings - Fork 53
/
Copy pathioq.c
295 lines (230 loc) · 8.57 KB
/
ioq.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
/**
* @file sys/ioq.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>
NTSTATUS SpdIoqCreate(PVOID DeviceExtension, SPD_IOQ **PIoq)
{
SPD_IOQ *Ioq;
ULONG BucketCount = (PAGE_SIZE - sizeof *Ioq) / sizeof Ioq->ProcessBuckets[0];
*PIoq = 0;
Ioq = SpdAllocNonPaged(PAGE_SIZE, SpdTagIoq);
if (0 == Ioq)
return STATUS_INSUFFICIENT_RESOURCES;
RtlZeroMemory(Ioq, PAGE_SIZE);
Ioq->DeviceExtension = DeviceExtension;
KeInitializeSpinLock(&Ioq->SpinLock);
SpdQeventInitialize(&Ioq->PendingEvent, 0);
InitializeListHead(&Ioq->PendingList);
InitializeListHead(&Ioq->ProcessList);
Ioq->ProcessBucketCount = BucketCount;
*PIoq = Ioq;
return STATUS_SUCCESS;
}
VOID SpdIoqDelete(SPD_IOQ *Ioq)
{
SpdIoqReset(Ioq, FALSE);
SpdQeventFinalize(&Ioq->PendingEvent);
SpdFree(Ioq, SpdTagIoq);
}
VOID SpdIoqReset(SPD_IOQ *Ioq, BOOLEAN Stop)
{
KIRQL Irql;
KeAcquireSpinLock(&Ioq->SpinLock, &Irql);
if (!Ioq->Stopped)
{
PLIST_ENTRY PendingEntry, ProcessEntry, Flink;
PendingEntry = Ioq->PendingList.Flink;
ProcessEntry = Ioq->ProcessList.Flink;
InitializeListHead(&Ioq->PendingList);
InitializeListHead(&Ioq->ProcessList);
RtlZeroMemory(Ioq->ProcessBuckets,
Ioq->ProcessBucketCount * sizeof Ioq->ProcessBuckets[0]);
for (; PendingEntry != &Ioq->PendingList; PendingEntry = Flink)
{
/* store Flink now, because *PendingEntry becomes invalid after SpdSrbComplete */
Flink = PendingEntry->Flink;
SpdSrbComplete(
Ioq->DeviceExtension,
CONTAINING_RECORD(PendingEntry, SPD_SRB_EXTENSION, ListEntry)->Srb,
SRB_STATUS_ABORTED);
}
for (; ProcessEntry != &Ioq->ProcessList; ProcessEntry = Flink)
{
/* store Flink now, because *ProcessEntry becomes invalid after SpdSrbComplete */
Flink = ProcessEntry->Flink;
SpdSrbComplete(
Ioq->DeviceExtension,
CONTAINING_RECORD(ProcessEntry, SPD_SRB_EXTENSION, ListEntry)->Srb,
SRB_STATUS_ABORTED);
}
if (Stop)
{
Ioq->Stopped = TRUE;
/* we are being stopped, permanently wake up waiters */
SpdQeventSetNoLock(&Ioq->PendingEvent);
}
}
KeReleaseSpinLock(&Ioq->SpinLock, Irql);
}
BOOLEAN SpdIoqStopped(SPD_IOQ *Ioq)
{
BOOLEAN Result;
KIRQL Irql;
KeAcquireSpinLock(&Ioq->SpinLock, &Irql);
Result = Ioq->Stopped;
KeReleaseSpinLock(&Ioq->SpinLock, Irql);
return Result;
}
NTSTATUS SpdIoqCancelSrb(SPD_IOQ *Ioq, PVOID Srb)
{
NTSTATUS Result = STATUS_UNSUCCESSFUL;
KIRQL Irql;
KeAcquireSpinLock(&Ioq->SpinLock, &Irql);
if (!Ioq->Stopped)
{
SPD_SRB_EXTENSION *SrbExtension = SpdSrbExtension(Srb);
ULONG Index;
ASSERT(Srb == SrbExtension->Srb);
Index = SpdHashMixPointer(SrbExtension) % Ioq->ProcessBucketCount;
for (PVOID *P = &Ioq->ProcessBuckets[Index]; *P; P = &((SPD_SRB_EXTENSION *)(*P))->HashNext)
if (*P == SrbExtension)
{
*P = SrbExtension->HashNext;
SrbExtension->HashNext = 0;
break;
}
RemoveEntryList(&SrbExtension->ListEntry);
SrbExtension->ListEntry.Flink = SrbExtension->ListEntry.Blink = 0;
SrbExtension->Srb = 0;
SpdSrbComplete(Ioq->DeviceExtension, Srb, SRB_STATUS_ABORTED);
Result = STATUS_SUCCESS;
}
KeReleaseSpinLock(&Ioq->SpinLock, Irql);
return Result;
}
NTSTATUS SpdIoqPostSrb(SPD_IOQ *Ioq, PVOID Srb)
{
NTSTATUS Result = STATUS_CANCELLED;
KIRQL Irql;
KeAcquireSpinLock(&Ioq->SpinLock, &Irql);
if (!Ioq->Stopped)
{
SPD_SRB_EXTENSION *SrbExtension = SpdSrbExtension(Srb);
ASSERT(0 == SrbExtension->Srb);
SrbExtension->Srb = Srb;
ASSERT(0 == SrbExtension->ListEntry.Flink && 0 == SrbExtension->ListEntry.Blink);
InsertTailList(&Ioq->PendingList, &SrbExtension->ListEntry);
/* queue is not empty; wake up a waiter */
SpdQeventSetNoLock(&Ioq->PendingEvent);
Result = STATUS_SUCCESS;
}
KeReleaseSpinLock(&Ioq->SpinLock, Irql);
return Result;
}
NTSTATUS SpdIoqStartProcessingSrb(SPD_IOQ *Ioq, PLARGE_INTEGER Timeout, PIRP CancellableIrp,
VOID (*Prepare)(PVOID SrbExtension, PVOID Context, PVOID DataBuffer),
PVOID Context, PVOID DataBuffer)
{
NTSTATUS Result;
KIRQL Irql;
Result = SpdQeventCancellableWait(&Ioq->PendingEvent, Timeout, CancellableIrp);
if (STATUS_TIMEOUT == Result)
return STATUS_TIMEOUT;
if (STATUS_CANCELLED == Result || STATUS_THREAD_IS_TERMINATING == Result)
return STATUS_CANCELLED;
ASSERT(STATUS_SUCCESS == Result);
KeAcquireSpinLock(&Ioq->SpinLock, &Irql);
if (!Ioq->Stopped)
{
PLIST_ENTRY PendingEntry;
PendingEntry = &Ioq->PendingList;
if (PendingEntry->Flink != PendingEntry)
{
SPD_SRB_EXTENSION *SrbExtension =
CONTAINING_RECORD(PendingEntry->Flink, SPD_SRB_EXTENSION, ListEntry);
BOOLEAN Wake;
ULONG Index;
Wake = !RemoveEntryList(&SrbExtension->ListEntry);
Prepare(SrbExtension, Context, DataBuffer);
InsertTailList(&Ioq->ProcessList, &SrbExtension->ListEntry);
Index = SpdHashMixPointer(SrbExtension) % Ioq->ProcessBucketCount;
#if DBG
for (PVOID X = Ioq->ProcessBuckets[Index]; X; X = ((SPD_SRB_EXTENSION *)X)->HashNext)
ASSERT(X != SrbExtension);
ASSERT(0 == SrbExtension->HashNext);
#endif
SrbExtension->HashNext = Ioq->ProcessBuckets[Index];
Ioq->ProcessBuckets[Index] = SrbExtension;
if (Wake)
/* queue is not empty; wake up a waiter */
SpdQeventSetNoLock(&Ioq->PendingEvent);
Result = STATUS_SUCCESS;
}
else
Result = STATUS_UNSUCCESSFUL;
}
else
{
/* queue is stopped; wake up a waiter */
SpdQeventSetNoLock(&Ioq->PendingEvent);
Result = STATUS_CANCELLED;
}
KeReleaseSpinLock(&Ioq->SpinLock, Irql);
return Result;
}
VOID SpdIoqEndProcessingSrb(SPD_IOQ *Ioq, UINT64 Hint,
UCHAR (*Complete)(PVOID SrbExtension, PVOID Context, PVOID DataBuffer),
PVOID Context, PVOID DataBuffer)
{
KIRQL Irql;
KeAcquireSpinLock(&Ioq->SpinLock, &Irql);
if (!Ioq->Stopped)
{
SPD_SRB_EXTENSION *SrbExtension = (PVOID)(UINT_PTR)Hint;
ULONG Index;
Index = SpdHashMixPointer(SrbExtension) % Ioq->ProcessBucketCount;
for (PVOID *P = &Ioq->ProcessBuckets[Index]; *P; P = &((SPD_SRB_EXTENSION *)(*P))->HashNext)
if (*P == SrbExtension)
{
*P = SrbExtension->HashNext;
RemoveEntryList(&SrbExtension->ListEntry);
UCHAR SrbStatus = Complete(SrbExtension, Context, DataBuffer);
if (SRB_STATUS_PENDING == SrbStatus)
{
/*
* If Complete returns PENDING we need to repost the SRB
* and we will also place it at the queue head, so that it
* gets picked up immediately after.
*
* This functionality supports splitting SRB's into chunks,
* which is required for I/O that exceeds our MaxTransferLength.
* See https://tinyurl.com/ychyv62s
*/
InsertHeadList(&Ioq->PendingList, &SrbExtension->ListEntry);
/* queue is not empty; wake up a waiter */
SpdQeventSetNoLock(&Ioq->PendingEvent);
}
else
SpdSrbComplete(Ioq->DeviceExtension, SrbExtension->Srb, SrbStatus);
break;
}
}
KeReleaseSpinLock(&Ioq->SpinLock, Irql);
}