-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCacheManager.c
More file actions
340 lines (242 loc) · 8.03 KB
/
CacheManager.c
File metadata and controls
340 lines (242 loc) · 8.03 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
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
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
#include "Driver.h"
_Requires_lock_held_(_Global_critical_region_)
BOOLEAN
BlorgAcquireNodeForLazyWrite(
IN PVOID Context,
IN BOOLEAN Wait
)
/*++
Routine Description:
The address of this routine is specified when creating a CacheMap for
a file. It is subsequently called by the Lazy Writer prior to its
performing lazy writes to the file.
Arguments:
Context - The Fcb which was specified as a context parameter for this
routine.
Wait - TRUE if the caller is willing to block.
Return Value:
FALSE - if Wait was specified as FALSE and blocking would have
been required. The Fcb is not acquired.
TRUE - if the Fcb has been acquired
--*/
{
//
// Check here for the EA File. It turns out we need the normal
// resource shared in this case. Otherwise we take the paging
// I/O resource shared.
//
//
// Note that we do not need to disable APC delivery to guard
// against a rogue user issuing a suspend APC. That is because
// it is guaranteed that the caller is either in the system context,
// to which a user cannot deliver a suspend APC, or the caller has
// already disabled kernel APC delivery before calling. This is true
// for all the other pre-acquire routines as well.
//
if (!ExAcquireResourceSharedLite(C_CAST(PFCB, Context)->Header.PagingIoResource, Wait))
{
return FALSE;
}
//
// We assume the Lazy Writer only acquires this Fcb once.
// Therefore, it should be guaranteed that this flag is currently
// clear (the ASSERT), and then we will set this flag, to ensure
// that the Lazy Writer will never try to advance Valid Data, and
// also not deadlock by trying to get the Fcb exclusive.
//
NT_ASSERT(BLORGFS_FCB_SIGNATURE == GET_NODE_TYPE(Context));
NT_ASSERT(NULL != PsGetCurrentThread());
NT_ASSERT(NULL == C_CAST(PFCB, Context)->LazyWriteThread);
(C_CAST(PFCB, Context))->LazyWriteThread = PsGetCurrentThread();
if (!global.LazyWriteThread)
{
global.LazyWriteThread = PsGetCurrentThread();
}
//
// This is a kludge because Cc is really the top level. When it
// enters the file system, we will think it is a resursive call
// and complete the request with hard errors or verify. It will
// then have to deal with them, somehow....
//
NT_ASSERT(NULL == IoGetTopLevelIrp());
IoSetTopLevelIrp(C_CAST(PIRP, FSRTL_CACHE_TOP_LEVEL_IRP));
return TRUE;
}
_Requires_lock_held_(_Global_critical_region_)
VOID
BlorgReleaseNodeFromLazyWrite(
IN PVOID Context
)
/*++
Routine Description:
The address of this routine is specified when creating a CacheMap for
a file. It is subsequently called by the Lazy Writer after its
performing lazy writes to the file.
Arguments:
Context - The Fcb which was specified as a context parameter for this
routine.
Return Value:
None
--*/
{
//
// Assert that this really is an fcb and that this thread really owns
// the lazy writer mark in the fcb.
//
NT_ASSERT(BLORGFS_FCB_SIGNATURE == GET_NODE_TYPE(Context));
NT_ASSERT(NULL != PsGetCurrentThread());
NT_ASSERT(PsGetCurrentThread() == C_CAST(PFCB, Context)->LazyWriteThread);
//
// Release the lazy writer mark.
//
(C_CAST(PFCB, Context))->LazyWriteThread = NULL;
//
// Check here for the EA File. It turns out we needed the normal
// resource shared in this case. Otherwise it was the PagingIoResource.
//
ExReleaseResourceLite(C_CAST(PFCB, Context)->Header.PagingIoResource);
//
// Clear the kludge at this point.
//
NT_ASSERT(C_CAST(PIRP, FSRTL_CACHE_TOP_LEVEL_IRP) == IoGetTopLevelIrp());
IoSetTopLevelIrp(NULL);
}
_Requires_lock_held_(_Global_critical_region_)
BOOLEAN
BlorgAcquireNodeForReadAhead(
IN PVOID Context,
IN BOOLEAN Wait
)
/*++
Routine Description:
The address of this routine is specified when creating a CacheMap for
a file. It is subsequently called by the Lazy Writer prior to its
performing read ahead to the file.
Arguments:
Context - The Fcb which was specified as a context parameter for this
routine.
Wait - TRUE if the caller is willing to block.
Return Value:
FALSE - if Wait was specified as FALSE and blocking would have
been required. The Fcb is not acquired.
TRUE - if the Fcb has been acquired
--*/
{
//
// We acquire the normal file resource shared here to synchronize
// correctly with purges.
//
//
// Note that we do not need to disable APC delivery to guard
// against a rogue user issuing a suspend APC. That is because
// it is guaranteed that the caller is either in the system context,
// to which a user cannot deliver a suspend APC, or the caller has
// already disabled kernel APC delivery before calling. This is true
// for all the other pre-acquire routines as well.
//
if (!ExAcquireResourceSharedLite(C_CAST(PFCB, Context)->Header.Resource,
Wait))
{
return FALSE;
}
//
// This is a kludge because Cc is really the top level. We it
// enters the file system, we will think it is a resursive call
// and complete the request with hard errors or verify. It will
// have to deal with them, somehow....
//
NT_ASSERT(NULL == IoGetTopLevelIrp());
IoSetTopLevelIrp(C_CAST(PIRP, FSRTL_CACHE_TOP_LEVEL_IRP));
return TRUE;
}
_Requires_lock_held_(_Global_critical_region_)
VOID
BlorgReleaseNodeFromReadAhead(
IN PVOID Context
)
/*++
Routine Description:
The address of this routine is specified when creating a CacheMap for
a file. It is subsequently called by the Lazy Writer after its
read ahead.
Arguments:
Context - The Fcb which was specified as a context parameter for this
routine.
Return Value:
None
--*/
{
//
// Clear the kludge at this point.
//
NT_ASSERT(C_CAST(PIRP, FSRTL_CACHE_TOP_LEVEL_IRP) == IoGetTopLevelIrp());
IoSetTopLevelIrp(NULL);
ExReleaseResourceLite(C_CAST(PFCB, Context)->Header.Resource);
}
_Function_class_(FAST_IO_CHECK_IF_POSSIBLE)
BOOLEAN
FastIoCheckIfPossible(
IN PFILE_OBJECT FileObject,
IN PLARGE_INTEGER FileOffset,
IN ULONG Length,
IN BOOLEAN Wait,
IN ULONG LockKey,
IN BOOLEAN CheckForReadOperation,
OUT PIO_STATUS_BLOCK IoStatus,
IN PDEVICE_OBJECT DeviceObject
)
/*++
Routine Description:
This routine checks if fast i/o is possible for a read/write operation
Arguments:
FileObject - Supplies the file object used in the query
FileOffset - Supplies the starting byte offset for the read/write operation
Length - Supplies the length, in bytes, of the read/write operation
Wait - Indicates if we can wait
LockKey - Supplies the lock key
CheckForReadOperation - Indicates if this is a check for a read or write
operation
IoStatus - Receives the status of the operation if our return value is
FastIoReturnError
Return Value:
BOOLEAN - TRUE if fast I/O is possible and FALSE if the caller needs
to take the long route.
--*/
{
UNREFERENCED_PARAMETER(DeviceObject);
UNREFERENCED_PARAMETER(IoStatus);
UNREFERENCED_PARAMETER(Wait);
//
// Decode the file object to get our fcb, the only one we want
// to deal with is a UserFileOpen
//
if (BLORGFS_FCB_SIGNATURE != GET_NODE_TYPE(FileObject->FsContext))
{
return FALSE;
}
PFCB fcb = FileObject->FsContext;
LARGE_INTEGER largeLength =
{
.QuadPart = Length
};
//
// Based on whether this is a read or write operation we call
// fsrtl check for read/write
//
if (CheckForReadOperation)
{
if (FsRtlFastCheckLockForRead(&fcb->FileLock,
FileOffset,
&largeLength,
LockKey,
FileObject,
PsGetCurrentProcess()))
{
return TRUE;
}
}
//
// Blanket fail all writes for now
//
return FALSE;
}