-
Notifications
You must be signed in to change notification settings - Fork 62
/
Copy pathwrite.c
283 lines (219 loc) · 8.19 KB
/
write.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
/*
Dokan : user-mode file system library for Windows
Copyright (C) 2008 Hiroki Asakawa [email protected]
http://dokan-dev.net/en
This program is free software; you can redistribute it and/or modify it under
the terms of the GNU Lesser General Public License as published by the Free
Software Foundation; either version 3 of the License, or (at your option) any
later version.
This program is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
You should have received a copy of the GNU Lesser General Public License along
with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "precomp.h"
#pragma hdrstop
NTSTATUS
DokanDispatchWrite(
__in PDEVICE_OBJECT DeviceObject,
__in PIRP Irp
)
{
PIO_STACK_LOCATION irpSp;
PFILE_OBJECT fileObject;
NTSTATUS status = STATUS_INVALID_PARAMETER;
PEVENT_CONTEXT eventContext;
ULONG eventLength;
PDokanCCB ccb;
PDokanFCB fcb;
PDokanVCB vcb;
PVOID buffer;
ULONG bufferLength;
//PAGED_CODE();
__try {
FsRtlEnterFileSystem();
DDbgPrint("==> DokanWrite");
irpSp = IoGetCurrentIrpStackLocation(Irp);
fileObject = irpSp->FileObject;
if (fileObject == NULL) {
DDbgPrint(" fileObject == NULL");
status = STATUS_INVALID_PARAMETER;
__leave;
}
vcb = DeviceObject->DeviceExtension;
if (GetIdentifierType(vcb) != VCB ||
!DokanCheckCCB(vcb->Dcb, fileObject->FsContext2)) {
status = STATUS_INVALID_PARAMETER;
__leave;
}
DDbgPrint(" ProcessId %lu\n", IoGetRequestorProcessId(Irp));
DokanPrintFileName(fileObject);
ccb = fileObject->FsContext2;
ASSERT(ccb != NULL);
fcb = ccb->Fcb;
ASSERT(fcb != NULL);
if (fcb->Flags & DOKAN_FILE_DIRECTORY) {
status = STATUS_INVALID_PARAMETER;
__leave;
}
if (irpSp->Parameters.Write.Length == 0) {
status = STATUS_SUCCESS;
__leave;
}
if (Irp->MdlAddress) {
DDbgPrint(" use MdlAddress");
buffer = MmGetSystemAddressForMdlSafe(Irp->MdlAddress, NormalPagePriority);
} else {
DDbgPrint(" use UserBuffer");
buffer = Irp->UserBuffer;
}
if (buffer == NULL) {
DDbgPrint(" buffer == NULL");
status = STATUS_INVALID_PARAMETER;
__leave;
}
// the length of EventContext is sum of length to write and length of file name
eventLength = sizeof(EVENT_CONTEXT)
+ irpSp->Parameters.Write.Length
+ fcb->FileName.Length;
eventContext = AllocateEventContext(vcb->Dcb, Irp, eventLength, ccb);
// no more memory!
if (eventContext == NULL) {
status = STATUS_INSUFFICIENT_RESOURCES;
__leave;
}
eventContext->Context = ccb->UserContext;
//DDbgPrint(" get Context %X\n", (ULONG)ccb->UserContext);
// When the length is bigger than usual event notitfication buffer,
// saves pointer in DiverContext to copy EventContext after allocating
// more bigger memory.
Irp->Tail.Overlay.DriverContext[DRIVER_CONTEXT_EVENT] = eventContext;
if (Irp->Flags & IRP_PAGING_IO) {
DDbgPrint(" Paging IO");
eventContext->FileFlags |= DOKAN_PAGING_IO;
}
if (fileObject->Flags & FO_SYNCHRONOUS_IO) {
DDbgPrint(" Synchronous IO");
eventContext->FileFlags |= DOKAN_SYNCHRONOUS_IO;
}
// offset of file to write
eventContext->Write.ByteOffset = irpSp->Parameters.Write.ByteOffset;
if (irpSp->Parameters.Write.ByteOffset.LowPart == FILE_WRITE_TO_END_OF_FILE
&& irpSp->Parameters.Write.ByteOffset.HighPart == -1) {
eventContext->FileFlags |= DOKAN_WRITE_TO_END_OF_FILE;
DDbgPrint(" WriteOffset = end of file");
}
if ((fileObject->Flags & FO_SYNCHRONOUS_IO) &&
((irpSp->Parameters.Write.ByteOffset.LowPart == FILE_USE_FILE_POINTER_POSITION) &&
(irpSp->Parameters.Write.ByteOffset.HighPart == -1))) {
// NOTE:
// http://msdn.microsoft.com/en-us/library/ms795960.aspx
// Do not check IrpSp->Parameters.Write.ByteOffset.QuadPart == 0
// Probably the document is wrong.
eventContext->Write.ByteOffset.QuadPart = fileObject->CurrentByteOffset.QuadPart;
}
// the size of buffer to write
eventContext->Write.BufferLength = irpSp->Parameters.Write.Length;
// the offset from the begining of structure
// the contents to write will be copyed to this offset
eventContext->Write.BufferOffset = FIELD_OFFSET(EVENT_CONTEXT, Write.FileName[0]) +
fcb->FileName.Length + sizeof(WCHAR); // adds last null char
// copies the content to write to EventContext
RtlCopyMemory((PCHAR)eventContext + eventContext->Write.BufferOffset,
buffer, irpSp->Parameters.Write.Length);
// copies file name
eventContext->Write.FileNameLength = fcb->FileName.Length;
RtlCopyMemory(eventContext->Write.FileName, fcb->FileName.Buffer, fcb->FileName.Length);
// When eventlength is less than event notification buffer,
// returns it to user-mode using pending event.
if (eventLength <= EVENT_CONTEXT_MAX_SIZE) {
DDbgPrint(" Offset %d:%d, Length %d\n",
irpSp->Parameters.Write.ByteOffset.HighPart,
irpSp->Parameters.Write.ByteOffset.LowPart,
irpSp->Parameters.Write.Length);
// EventContext is no longer needed, clear it
Irp->Tail.Overlay.DriverContext[DRIVER_CONTEXT_EVENT] = 0;
// register this IRP to IRP waiting list and make it pending status
status = DokanRegisterPendingIrp(DeviceObject, Irp, eventContext, 0);
// Resuests bigger memory
// eventContext will be freed later using Irp->Tail.Overlay.DriverContext[DRIVER_CONTEXT_EVENT]
} else {
// the length at lest file name can be stored
ULONG requestContextLength = max(sizeof(EVENT_CONTEXT), eventContext->Write.BufferOffset);
PEVENT_CONTEXT requestContext = AllocateEventContext(vcb->Dcb, Irp, requestContextLength, ccb);
// no more memory!
if (requestContext == NULL) {
status = STATUS_INSUFFICIENT_RESOURCES;
Irp->Tail.Overlay.DriverContext[DRIVER_CONTEXT_EVENT] = 0;
DokanFreeEventContext(eventContext);
__leave;
}
DDbgPrint(" Offset %d:%d, Length %d (request)\n",
irpSp->Parameters.Write.ByteOffset.HighPart,
irpSp->Parameters.Write.ByteOffset.LowPart,
irpSp->Parameters.Write.Length);
// copies from begining of EventContext to the end of file name
RtlCopyMemory(requestContext, eventContext, eventContext->Write.BufferOffset);
// puts actual size of RequestContext
requestContext->Length = requestContextLength;
// requsts enough size to copy EventContext
requestContext->Write.RequestLength = eventLength;
// regiters this IRP to IRP wainting list and make it pending status
status = DokanRegisterPendingIrp(DeviceObject, Irp, requestContext, 0);
}
} __finally {
// if status of IRP is not pending, must complete current IRP
if (status != STATUS_PENDING) {
Irp->IoStatus.Status = status;
Irp->IoStatus.Information = 0;
IoCompleteRequest(Irp, IO_NO_INCREMENT);
DokanPrintNTStatus(status);
} else {
DDbgPrint(" STATUS_PENDING");
}
DDbgPrint("<== DokanWrite");
FsRtlExitFileSystem();
}
return status;
}
VOID
DokanCompleteWrite(
__in PIRP_ENTRY IrpEntry,
__in PEVENT_INFORMATION EventInfo
)
{
PIRP irp;
PIO_STACK_LOCATION irpSp;
NTSTATUS status = STATUS_SUCCESS;
ULONG readLength = 0;
ULONG bufferLen = 0;
PVOID buffer = NULL;
PDokanCCB ccb;
PFILE_OBJECT fileObject;
fileObject = IrpEntry->FileObject;
ASSERT(fileObject != NULL);
DDbgPrint("==> DokanCompleteWrite %wZ\n", &fileObject->FileName);
irp = IrpEntry->Irp;
irpSp = IrpEntry->IrpSp;
ccb = fileObject->FsContext2;
ASSERT(ccb != NULL);
ccb->UserContext = EventInfo->Context;
//DDbgPrint(" set Context %X\n", (ULONG)ccb->UserContext);
status = EventInfo->Status;
irp->IoStatus.Status = status;
irp->IoStatus.Information = EventInfo->BufferLength;
if (NT_SUCCESS(status) &&
EventInfo->BufferLength != 0 &&
fileObject->Flags & FO_SYNCHRONOUS_IO &&
!(irp->Flags & IRP_PAGING_IO)) {
// update current byte offset only when synchronous IO and not paging IO
fileObject->CurrentByteOffset.QuadPart =
EventInfo->Write.CurrentByteOffset.QuadPart;
DDbgPrint(" Updated CurrentByteOffset %I64d\n",
fileObject->CurrentByteOffset.QuadPart);
}
IoCompleteRequest(irp, IO_NO_INCREMENT);
DokanPrintNTStatus(status);
DDbgPrint("<== DokanCompleteWrite");
}