-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathpartialirp.cpp
More file actions
191 lines (160 loc) · 5.46 KB
/
Copy pathpartialirp.cpp
File metadata and controls
191 lines (160 loc) · 5.46 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
#include "qcache.h"
PIRP SCATTERED_IRP::BuildIrp(
UCHAR MajorFunction,
PDEVICE_OBJECT DeviceObject,
ULONG OriginalIrpOffset,
ULONG BytesThisIrp,
PLARGE_INTEGER LowerDeviceOffset)
{
auto io_stack = IoGetCurrentIrpStackLocation(OriginalIrp);
PIRP lower_irp = NULL;
PIO_STACK_LOCATION lower_io_stack = NULL;
if ((DeviceObject->Flags & DO_DIRECT_IO) &&
(OriginalDeviceObject->Flags & DO_BUFFERED_IO))
{
lower_irp = IoBuildAsynchronousFsdRequest(
MajorFunction,
DeviceObject,
SystemBuffer + OriginalIrpOffset,
BytesThisIrp,
LowerDeviceOffset,
NULL);
if (lower_irp != NULL)
{
lower_io_stack = IoGetNextIrpStackLocation(lower_irp);
}
}
else
{
lower_irp =
IoAllocateIrp(DeviceObject->StackSize,
FALSE);
if (lower_irp != NULL)
{
lower_io_stack = IoGetNextIrpStackLocation(lower_irp);
lower_io_stack->MajorFunction = MajorFunction;
lower_io_stack->Parameters.Write.ByteOffset =
*LowerDeviceOffset;
lower_io_stack->Parameters.Write.Length = BytesThisIrp;
if (DeviceObject->Flags & DO_DIRECT_IO)
{
auto mdl = IoAllocateMdl(
(PUCHAR)
MmGetMdlVirtualAddress(OriginalIrp->MdlAddress) +
OriginalIrpOffset,
BytesThisIrp, FALSE, FALSE, lower_irp);
if (mdl == NULL)
{
IoFreeIrp(lower_irp);
InterlockedExchange(&LastFailedStatus,
STATUS_INSUFFICIENT_RESOURCES);
return NULL;
}
IoBuildPartialMdl(
OriginalIrp->MdlAddress,
mdl, (PUCHAR)
MmGetMdlVirtualAddress(OriginalIrp->MdlAddress) +
OriginalIrpOffset,
BytesThisIrp);
}
else
{
if (SystemBuffer == NULL)
{
SystemBuffer =
(PUCHAR)MmGetSystemAddressForMdlSafe(
OriginalIrp->MdlAddress, HighPagePriority);
if (SystemBuffer == NULL)
{
IoFreeIrp(lower_irp);
InterlockedExchange(&LastFailedStatus,
STATUS_INSUFFICIENT_RESOURCES);
return NULL;
}
AllocatedBuffer =
new UCHAR[io_stack->Parameters.Write.Length];
if (AllocatedBuffer == NULL)
{
IoFreeIrp(lower_irp);
InterlockedExchange(&LastFailedStatus,
STATUS_INSUFFICIENT_RESOURCES);
return NULL;
}
if (MajorFunction == IRP_MJ_WRITE)
{
RtlCopyMemory(AllocatedBuffer,
SystemBuffer,
io_stack->Parameters.Write.Length);
}
else if (MajorFunction == IRP_MJ_READ)
{
CopyBack = TRUE;
}
}
auto buffer = AllocatedBuffer != NULL ?
AllocatedBuffer : SystemBuffer;
if (DeviceObject->Flags & DO_BUFFERED_IO)
{
lower_irp->AssociatedIrp.SystemBuffer =
buffer + OriginalIrpOffset;
}
else
{
lower_irp->UserBuffer = buffer + OriginalIrpOffset;
}
}
}
}
if (lower_irp == NULL)
{
InterlockedExchange(&LastFailedStatus,
STATUS_INSUFFICIENT_RESOURCES);
return NULL;
}
lower_irp->Tail.Overlay.Thread = OriginalIrp->Tail.Overlay.Thread;
if (MajorFunction == IRP_MJ_WRITE)
{
lower_irp->Flags |= IRP_WRITE_OPERATION | SL_WRITE_THROUGH |
IRP_NOCACHE;
}
else if (MajorFunction == IRP_MJ_READ)
{
lower_irp->Flags |= IRP_READ_OPERATION | IRP_NOCACHE;
}
IoSetCompletionRoutine(lower_irp, IrpCompletionRoutine,
this, TRUE, TRUE, TRUE);
InterlockedIncrement(&ScatterCount);
return lower_irp;
}
NTSTATUS
SCATTERED_IRP::IrpCompletionRoutine(
PDEVICE_OBJECT DeviceObject,
PIRP Irp,
PVOID Context
)
{
UNREFERENCED_PARAMETER(DeviceObject);
__analysis_assume(Context != NULL);
auto scatter = (PSCATTERED_IRP)Context;
if (NT_SUCCESS(Irp->IoStatus.Status))
{
InterlockedAddPtr(&scatter->BytesCompleted, Irp->IoStatus.Information);
}
else
{
KdPrint(("QCache: Lower level I/O failed: 0x%X\n",
Irp->IoStatus.Status));
KdBreakPoint();
InterlockedExchange(&scatter->LastFailedStatus, Irp->IoStatus.Status);
}
if (Irp->MdlAddress != scatter->OriginalIrp->MdlAddress)
{
QCacheFreeIrpWithMdls(Irp);
}
else
{
IoFreeIrp(Irp);
}
scatter->Complete();
return STATUS_MORE_PROCESSING_REQUIRED;
}