forked from winfsp/winfsp
-
Notifications
You must be signed in to change notification settings - Fork 5
/
callbacks.c
314 lines (258 loc) · 8.56 KB
/
callbacks.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
/**
* @file sys/callbacks.c
* Fast I/O and resource acquisition callbacks.
*
* @copyright 2015-2020 Bill Zissimopoulos
*/
/*
* This file is part of WinFsp.
*
* 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>
FAST_IO_CHECK_IF_POSSIBLE FspFastIoCheckIfPossible;
FAST_IO_ACQUIRE_FILE FspAcquireFileForNtCreateSection;
FAST_IO_RELEASE_FILE FspReleaseFileForNtCreateSection;
FAST_IO_ACQUIRE_FOR_MOD_WRITE FspAcquireForModWrite;
FAST_IO_RELEASE_FOR_MOD_WRITE FspReleaseForModWrite;
FAST_IO_ACQUIRE_FOR_CCFLUSH FspAcquireForCcFlush;
FAST_IO_RELEASE_FOR_CCFLUSH FspReleaseForCcFlush;
BOOLEAN FspAcquireForLazyWrite(
PVOID Context,
BOOLEAN Wait);
VOID FspReleaseFromLazyWrite(
PVOID Context);
BOOLEAN FspAcquireForReadAhead(
PVOID Context,
BOOLEAN Wait);
VOID FspReleaseFromReadAhead(
PVOID Context);
VOID FspPropagateTopFlags(PIRP Irp, PIRP TopLevelFlags);
#ifdef ALLOC_PRAGMA
#pragma alloc_text(PAGE, FspFastIoCheckIfPossible)
#pragma alloc_text(PAGE, FspAcquireFileForNtCreateSection)
#pragma alloc_text(PAGE, FspReleaseFileForNtCreateSection)
#pragma alloc_text(PAGE, FspAcquireForModWrite)
#pragma alloc_text(PAGE, FspReleaseForModWrite)
#pragma alloc_text(PAGE, FspAcquireForCcFlush)
#pragma alloc_text(PAGE, FspReleaseForCcFlush)
#pragma alloc_text(PAGE, FspAcquireForLazyWrite)
#pragma alloc_text(PAGE, FspReleaseFromLazyWrite)
#pragma alloc_text(PAGE, FspAcquireForReadAhead)
#pragma alloc_text(PAGE, FspReleaseFromReadAhead)
#pragma alloc_text(PAGE, FspPropagateTopFlags)
#endif
BOOLEAN FspFastIoCheckIfPossible(
PFILE_OBJECT FileObject,
PLARGE_INTEGER FileOffset,
ULONG Length,
BOOLEAN Wait,
ULONG LockKey,
BOOLEAN CheckForReadOperation,
PIO_STATUS_BLOCK IoStatus,
PDEVICE_OBJECT DeviceObject)
{
FSP_ENTER_BOOL(PAGED_CODE());
Result = FALSE;
FSP_LEAVE_BOOL("FileObject=%p", FileObject);
}
VOID FspAcquireFileForNtCreateSection(
PFILE_OBJECT FileObject)
{
/* Callers:
* CcWriteBehind
*/
FSP_ENTER_VOID(PAGED_CODE());
FSP_FILE_NODE *FileNode = FileObject->FsContext;
FspFileNodeAcquireExclusive(FileNode, Full);
FSP_LEAVE_VOID("FileObject=%p", FileObject);
}
VOID FspReleaseFileForNtCreateSection(
PFILE_OBJECT FileObject)
{
/* Callers:
* CcWriteBehind
*/
FSP_ENTER_VOID(PAGED_CODE());
FSP_FILE_NODE *FileNode = FileObject->FsContext;
FspFileNodeRelease(FileNode, Full);
FSP_LEAVE_VOID("FileObject=%p", FileObject);
}
NTSTATUS FspAcquireForModWrite(
PFILE_OBJECT FileObject,
PLARGE_INTEGER EndingOffset,
PERESOURCE *ResourceToRelease,
PDEVICE_OBJECT DeviceObject)
{
FSP_ENTER(PAGED_CODE());
FSP_FILE_NODE *FileNode = FileObject->FsContext;
BOOLEAN Success;
ASSERT((PIRP)FSRTL_MOD_WRITE_TOP_LEVEL_IRP == IoGetTopLevelIrp());
Success = FspFileNodeTryAcquireExclusiveF(FileNode, FspFileNodeAcquireFull, FALSE);
if (Success)
{
*ResourceToRelease = 0 == FileNode->MainFileNode ?
FileNode->Header.PagingIoResource :
FileNode->MainFileNode->Header.PagingIoResource;
Result = STATUS_SUCCESS;
}
else
{
*ResourceToRelease = 0;
Result = STATUS_CANT_WAIT;
}
FSP_LEAVE("FileObject=%p", FileObject);
}
NTSTATUS FspReleaseForModWrite(
PFILE_OBJECT FileObject,
PERESOURCE ResourceToRelease,
PDEVICE_OBJECT DeviceObject)
{
FSP_ENTER(PAGED_CODE());
FSP_FILE_NODE *FileNode = FileObject->FsContext;
FspFileNodeRelease(FileNode, Full);
ASSERT((PIRP)FSRTL_MOD_WRITE_TOP_LEVEL_IRP == IoGetTopLevelIrp());
FSP_LEAVE("FileObject=%p", FileObject);
}
NTSTATUS FspAcquireForCcFlush(
PFILE_OBJECT FileObject,
PDEVICE_OBJECT DeviceObject)
{
FSP_ENTER(PAGED_CODE());
FSP_FILE_NODE *FileNode = FileObject->FsContext;
PIRP TopLevelIrp = IoGetTopLevelIrp();
ULONG TopFlags;
if ((PIRP)FSRTL_MAX_TOP_LEVEL_IRP_FLAG >= TopLevelIrp)
{
FspFileNodeAcquireExclusive(FileNode, Full);
ASSERT(0 == FileNode->Tls.CcFlush.TopLevelIrp);
FileNode->Tls.CcFlush.TopLevelIrp = TopLevelIrp;
IoSetTopLevelIrp((PIRP)FSRTL_CACHE_TOP_LEVEL_IRP);
}
else
{
TopFlags = FspIrpTopFlags(TopLevelIrp);
FspIrpSetTopFlags(TopLevelIrp, FspIrpFlags(TopLevelIrp));
FspFileNodeAcquireExclusive(FileNode, Full);
ASSERT(0 == FileNode->Tls.CcFlush.TopFlags);
FileNode->Tls.CcFlush.TopFlags = TopFlags;
}
FSP_LEAVE("FileObject=%p", FileObject);
}
NTSTATUS FspReleaseForCcFlush(
PFILE_OBJECT FileObject,
PDEVICE_OBJECT DeviceObject)
{
FSP_ENTER(PAGED_CODE());
FSP_FILE_NODE *FileNode = FileObject->FsContext;
PIRP TopLevelIrp = IoGetTopLevelIrp();
ULONG TopFlags;
if ((PIRP)FSRTL_MAX_TOP_LEVEL_IRP_FLAG >= TopLevelIrp)
{
ASSERT(0 == TopLevelIrp || (PIRP)FSRTL_CACHE_TOP_LEVEL_IRP == TopLevelIrp);
if ((PIRP)FSRTL_CACHE_TOP_LEVEL_IRP == TopLevelIrp)
IoSetTopLevelIrp(FileNode->Tls.CcFlush.TopLevelIrp);
FileNode->Tls.CcFlush.TopLevelIrp = 0;
FspFileNodeRelease(FileNode, Full);
}
else
{
TopFlags = FileNode->Tls.CcFlush.TopFlags;
FileNode->Tls.CcFlush.TopFlags = 0;
FspFileNodeRelease(FileNode, Full);
FspIrpSetTopFlags(TopLevelIrp, TopFlags);
}
FSP_LEAVE("FileObject=%p", FileObject);
}
BOOLEAN FspAcquireForLazyWrite(
PVOID Context,
BOOLEAN Wait)
{
/* Callers:
* CcWriteBehind
*/
FSP_ENTER_BOOL(PAGED_CODE());
FSP_FILE_NODE *FileNode = Context;
ASSERT(0 == IoGetTopLevelIrp());
Result = FspFileNodeTryAcquireExclusiveF(FileNode, FspFileNodeAcquireFull, Wait);
if (Result)
{
ASSERT(0 == FileNode->Tls.LazyWriteThread);
FileNode->Tls.LazyWriteThread = PsGetCurrentThread();
IoSetTopLevelIrp((PIRP)FSRTL_CACHE_TOP_LEVEL_IRP);
}
FSP_LEAVE_BOOL("Context=%p, Wait=%d", Context, Wait);
}
VOID FspReleaseFromLazyWrite(
PVOID Context)
{
/* Callers:
* CcWriteBehind
*/
FSP_ENTER_VOID(PAGED_CODE());
FSP_FILE_NODE *FileNode = Context;
ASSERT((PIRP)FSRTL_CACHE_TOP_LEVEL_IRP == IoGetTopLevelIrp());
ASSERT(PsGetCurrentThread() == FileNode->Tls.LazyWriteThread);
IoSetTopLevelIrp(0);
FileNode->Tls.LazyWriteThread = 0;
FspFileNodeRelease(FileNode, Full);
FSP_LEAVE_VOID("Context=%p", Context);
}
BOOLEAN FspAcquireForReadAhead(
PVOID Context,
BOOLEAN Wait)
{
FSP_ENTER_BOOL(PAGED_CODE());
FSP_FILE_NODE *FileNode = Context;
ASSERT(0 == IoGetTopLevelIrp());
Result = FspFileNodeTryAcquireSharedF(FileNode, FspFileNodeAcquireFull, Wait);
if (Result)
IoSetTopLevelIrp((PIRP)FSRTL_CACHE_TOP_LEVEL_IRP);
FSP_LEAVE_BOOL("Context=%p, Wait=%d", Context, Wait);
}
VOID FspReleaseFromReadAhead(
PVOID Context)
{
FSP_ENTER_VOID(PAGED_CODE());
FSP_FILE_NODE *FileNode = Context;
ASSERT((PIRP)FSRTL_CACHE_TOP_LEVEL_IRP == IoGetTopLevelIrp());
IoSetTopLevelIrp(0);
FspFileNodeRelease(FileNode, Full);
FSP_LEAVE_VOID("Context=%p", Context);
}
VOID FspPropagateTopFlags(PIRP Irp, PIRP TopLevelIrp)
{
/*
* We place FspPropagateTopFlags in this file, because the top flags
* are related to the resources acquired in FspAcquire*.
*/
PAGED_CODE();
ASSERT(0 != Irp && 0 != TopLevelIrp);
if ((PIRP)FSRTL_MAX_TOP_LEVEL_IRP_FLAG >= TopLevelIrp)
{
DEBUGBREAK_EX(iorecu);
FspIrpSetTopFlags(Irp, FspFileNodeAcquireFull);
}
else if ((PIRP)MM_SYSTEM_RANGE_START <= TopLevelIrp && IO_TYPE_IRP == TopLevelIrp->Type)
{
PFILE_OBJECT FileObject = IoGetCurrentIrpStackLocation(Irp)->FileObject;
PFILE_OBJECT TopLevelFileObject = IoGetCurrentIrpStackLocation(TopLevelIrp)->FileObject;
if (0 != FileObject && 0 != TopLevelFileObject &&
FileObject->FsContext == TopLevelFileObject->FsContext &&
FspFileNodeIsValid(FileObject->FsContext))
{
DEBUGBREAK_EX(iorecu);
FspIrpSetTopFlags(Irp, FspIrpTopFlags(TopLevelIrp) | FspIrpFlags(TopLevelIrp));
}
}
}