-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathread.cpp
More file actions
250 lines (183 loc) · 7.21 KB
/
Copy pathread.cpp
File metadata and controls
250 lines (183 loc) · 7.21 KB
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
#include "qcache.h"
//
// Handles IRP_MJ_READ by first walking the pending lazy-write
// queue and only then if any ranges left to read, request those
// reads from underlying driver.
//
NTSTATUS
QCacheRead(IN PDEVICE_OBJECT DeviceObject, IN PIRP Irp)
{
auto device_extension = (PDEVICE_EXTENSION)DeviceObject->DeviceExtension;
if (device_extension->Statistics.Size.QuadPart == 0)
{
return QCacheSendToNextDriver(DeviceObject, Irp);
}
Irp->IoStatus.Information = 0;
auto io_stack = IoGetCurrentIrpStackLocation(Irp);
NTSTATUS status;
InterlockedIncrement64(&device_extension->Statistics.ReadRequests);
InterlockedAdd64(&device_extension->Statistics.ReadBytes,
io_stack->Parameters.Read.Length);
if (io_stack->Parameters.Read.Length == 0)
{
return QCacheIgnore(DeviceObject, Irp);
}
if (io_stack->Parameters.Read.Length >
device_extension->Statistics.LargestReadSize)
{
device_extension->Statistics.LargestReadSize =
io_stack->Parameters.Read.Length;
KdPrint(("QCache: Largest read size is now %u KB\n",
device_extension->Statistics.LargestReadSize >> 10));
}
if (((io_stack->Parameters.Read.ByteOffset.QuadPart & 0x1ff) != 0) ||
((io_stack->Parameters.Read.Length & 0x1ff) != 0))
{
Irp->IoStatus.Status = STATUS_INVALID_PARAMETER;
IoCompleteRequest(Irp, IO_NO_INCREMENT);
KdBreakPoint();
return STATUS_INVALID_PARAMETER;
}
LONGLONG highest_byte =
io_stack->Parameters.Read.ByteOffset.QuadPart +
io_stack->Parameters.Read.Length;
if ((io_stack->Parameters.Read.ByteOffset.QuadPart >=
device_extension->Statistics.Size.QuadPart) ||
(highest_byte <= 0) ||
(highest_byte > device_extension->Statistics.Size.QuadPart))
{
Irp->IoStatus.Status = STATUS_END_OF_MEDIA;
IoCompleteRequest(Irp, IO_NO_INCREMENT);
KdBreakPoint();
return STATUS_END_OF_MEDIA;
}
auto system_buffer = (PUCHAR)
MmGetSystemAddressForMdlSafe(Irp->MdlAddress, NormalPagePriority);
if (system_buffer == NULL)
{
Irp->IoStatus.Status = STATUS_INSUFFICIENT_RESOURCES;
IoCompleteRequest(Irp, IO_NO_INCREMENT);
KdBreakPoint();
return STATUS_INSUFFICIENT_RESOURCES;
}
RTL_BITMAP bitmap;
WPoolMem<ULONG, NonPagedPool> bitmap_buffer(
(io_stack->Parameters.Read.Length >> 9) + sizeof(ULONG) - 1);
if (!bitmap_buffer)
{
Irp->IoStatus.Status = STATUS_INSUFFICIENT_RESOURCES;
IoCompleteRequest(Irp, IO_NO_INCREMENT);
KdBreakPoint();
return STATUS_INSUFFICIENT_RESOURCES;
}
bitmap_buffer.Clear();
RtlInitializeBitMap(&bitmap, bitmap_buffer,
io_stack->Parameters.Read.Length >> 9);
KLOCK_QUEUE_HANDLE lock_handle;
KIRQL lowest_irql = PASSIVE_LEVEL;
bool any_from_write_queue = false;
QCacheAcquireLock(&device_extension->WriteQueueLock, &lock_handle,
lowest_irql);
for (
auto entry = device_extension->WriteQueue.Flink;
entry != &device_extension->WriteQueue;
entry = entry->Flink)
{
auto item = CONTAINING_RECORD(entry, WRITE_QUEUE_ITEM, ListEntry);
if ((item->MajorFunction == IRP_MJ_WRITE) &&
(item->Offset.QuadPart <
(io_stack->Parameters.Read.ByteOffset.QuadPart +
io_stack->Parameters.Read.Length)) &&
((item->Offset.QuadPart + item->Length) >
io_stack->Parameters.Read.ByteOffset.QuadPart))
{
LONGLONG start_pos = max(item->Offset.QuadPart,
io_stack->Parameters.Read.ByteOffset.QuadPart);
LONGLONG end_pos = min(item->Offset.QuadPart + item->Length,
io_stack->Parameters.Read.ByteOffset.QuadPart +
io_stack->Parameters.Read.Length);
RtlCopyMemory(system_buffer + start_pos -
io_stack->Parameters.Read.ByteOffset.QuadPart,
item->Buffer + start_pos - item->Offset.QuadPart,
(SIZE_T)(end_pos - start_pos));
RtlSetBits(&bitmap,
(ULONG)((start_pos -
io_stack->Parameters.Read.ByteOffset.QuadPart) >> 9),
(ULONG)((end_pos - start_pos) >> 9));
any_from_write_queue = true;
++device_extension->Statistics.ReadRequestsFromCache;
device_extension->Statistics.ReadBytesFromCache +=
end_pos - start_pos;
}
}
QCacheReleaseLock(&lock_handle, &lowest_irql);
// If none of buffer filled by write queue
if (!any_from_write_queue)
{
InterlockedIncrement64(
&device_extension->Statistics.ReadRequestsReroutedToOriginal);
InterlockedAdd64(
&device_extension->Statistics.ReadBytesReroutedToOriginal,
io_stack->Parameters.Read.Length);
IoSkipCurrentIrpStackLocation(Irp);
return IoCallDriver(device_extension->TargetDeviceObject, Irp);
}
ULONG clear_index;
auto clear_bits = RtlFindFirstRunClear(&bitmap, &clear_index);
// If entire buffer filled by write queue
if (clear_bits == 0)
{
Irp->IoStatus.Status = STATUS_SUCCESS;
Irp->IoStatus.Information = io_stack->Parameters.Read.Length;
IoCompleteRequest(Irp, IO_NO_INCREMENT);
KdPrint(("QCache:QCacheRead: Entire buffer filled by write queue\n"));
return STATUS_SUCCESS;
}
PSCATTERED_IRP scatter;
status = SCATTERED_IRP::Create(
&scatter,
DeviceObject,
Irp,
&device_extension->RemoveLock,
(DeviceObject->Flags & DO_BUFFERED_IO) ?
(PUCHAR)Irp->AssociatedIrp.SystemBuffer :
NULL);
if (!NT_SUCCESS(status))
{
Irp->IoStatus.Status = status;
IoCompleteRequest(Irp, IO_NO_INCREMENT);
KdBreakPoint();
return status;
}
ULONG splits = 0;
do
{
LARGE_INTEGER lower_offset;
lower_offset.QuadPart =
io_stack->Parameters.Read.ByteOffset.QuadPart +
((LONGLONG)clear_index << 9);
auto lower_irp = scatter->BuildIrp(
IRP_MJ_READ,
device_extension->TargetDeviceObject,
clear_index << 9,
clear_bits << 9,
&lower_offset);
if (lower_irp == NULL)
{
break;
}
InterlockedAdd64(&device_extension->Statistics.ReadBytesFromOriginal,
(LONGLONG)clear_bits << 9);
IoCallDriver(device_extension->TargetDeviceObject, lower_irp);
clear_bits = RtlFindNextForwardRunClear(
&bitmap, clear_index + clear_bits, &clear_index);
++splits;
} while (clear_bits > 0);
if (splits > 1)
{
InterlockedAdd64(&device_extension->Statistics.SplitReads, splits);
}
// Decrement reference counter and complete if all partials are finished
scatter->Complete();
return STATUS_PENDING;
} // end QCacheReadWrite()