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
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
|
/*++
Copyright (c) 1990 Microsoft Corporation
Module Name:
logsup.c
Abstract:
This module implements the special cache manager support for logging
file systems.
Author:
Tom Miller [TomM] 30-Jul-1991
Revision History:
--*/
#include "cc.h"
//
// Define our debug constant
//
#define me 0x0000040
VOID
CcSetAdditionalCacheAttributes (
IN PFILE_OBJECT FileObject,
IN BOOLEAN DisableReadAhead,
IN BOOLEAN DisableWriteBehind
)
/*++
Routine Description:
This routine supports the setting of disable read ahead or disable write
behind flags to control Cache Manager operation. This routine may be
called any time after calling CcInitializeCacheMap. Initially both
read ahead and write behind are enabled. Note that the state of both
of these flags must be specified on each call to this routine.
Arguments:
FileObject - File object for which the respective flags are to be set.
DisableReadAhead - FALSE to enable read ahead, TRUE to disable it.
DisableWriteBehind - FALSE to enable write behind, TRUE to disable it.
Return Value:
None.
--*/
{
PSHARED_CACHE_MAP SharedCacheMap;
KIRQL OldIrql;
//
// Get pointer to SharedCacheMap.
//
SharedCacheMap = FileObject->SectionObjectPointer->SharedCacheMap;
//
// Now set the flags and return.
//
ExAcquireFastLock( &CcMasterSpinLock, &OldIrql );
if (DisableReadAhead) {
SetFlag(SharedCacheMap->Flags, DISABLE_READ_AHEAD);
} else {
ClearFlag(SharedCacheMap->Flags, DISABLE_READ_AHEAD);
}
if (DisableWriteBehind) {
SetFlag(SharedCacheMap->Flags, DISABLE_WRITE_BEHIND | MODIFIED_WRITE_DISABLED);
} else {
ClearFlag(SharedCacheMap->Flags, DISABLE_WRITE_BEHIND);
}
ExReleaseFastLock( &CcMasterSpinLock, OldIrql );
}
VOID
CcSetLogHandleForFile (
IN PFILE_OBJECT FileObject,
IN PVOID LogHandle,
IN PFLUSH_TO_LSN FlushToLsnRoutine
)
/*++
Routine Description:
This routine may be called to instruct the Cache Manager to store the
specified log handle with the shared cache map for a file, to support
subsequent calls to the other routines in this module which effectively
perform an associative search for files by log handle.
Arguments:
FileObject - File for which the log handle should be stored.
LogHandle - Log Handle to store.
FlushToLsnRoutine - A routine to call before flushing buffers for this
file, to insure a log file is flushed to the most
recent Lsn for any Bcb being flushed.
Return Value:
None.
--*/
{
PSHARED_CACHE_MAP SharedCacheMap;
//
// Get pointer to SharedCacheMap.
//
SharedCacheMap = FileObject->SectionObjectPointer->SharedCacheMap;
//
// Now set the log file handle and flush routine
//
SharedCacheMap->LogHandle = LogHandle;
SharedCacheMap->FlushToLsnRoutine = FlushToLsnRoutine;
}
LARGE_INTEGER
CcGetDirtyPages (
IN PVOID LogHandle,
IN PDIRTY_PAGE_ROUTINE DirtyPageRoutine,
IN PVOID Context1,
IN PVOID Context2
)
/*++
Routine Description:
This routine may be called to return all of the dirty pages in all files
for a given log handle. Each page is returned by an individual call to
the Dirty Page Routine. The Dirty Page Routine is defined by a prototype
in ntos\inc\cache.h.
Arguments:
LogHandle - Log Handle which must match the log handle previously stored
for all files which are to be returned.
DirtyPageRoutine -- The routine to call as each dirty page for this log
handle is found.
Context1 - First context parameter to be passed to the Dirty Page Routine.
Context2 - First context parameter to be passed to the Dirty Page Routine.
Return Value:
LARGE_INTEGER - Oldest Lsn found of all the dirty pages, or 0 if no dirty pages
--*/
{
PSHARED_CACHE_MAP SharedCacheMap;
PBCB Bcb, BcbToUnpin;
KIRQL OldIrql;
NTSTATUS ExceptionStatus;
LARGE_INTEGER SavedFileOffset, SavedOldestLsn, SavedNewestLsn;
ULONG SavedByteLength;
ULONG LoopsWithLockHeld = 0;
LARGE_INTEGER OldestLsn = {0,0};
//
// Synchronize with changes to the SharedCacheMap list.
//
ExAcquireFastLock( &CcMasterSpinLock, &OldIrql );
SharedCacheMap = CONTAINING_RECORD( CcDirtySharedCacheMapList.SharedCacheMapLinks.Flink,
SHARED_CACHE_MAP,
SharedCacheMapLinks );
BcbToUnpin = NULL;
while (&SharedCacheMap->SharedCacheMapLinks != &CcDirtySharedCacheMapList.SharedCacheMapLinks) {
//
// Skip over cursors, SharedCacheMaps for other LogHandles, and ones with
// no dirty pages
//
if (!FlagOn(SharedCacheMap->Flags, IS_CURSOR) && (SharedCacheMap->LogHandle == LogHandle) &&
(SharedCacheMap->DirtyPages != 0)) {
//
// This SharedCacheMap should stick around for a while in the dirty list.
//
SharedCacheMap->OpenCount += 1;
SharedCacheMap->DirtyPages += 1;
//
// Set our initial resume point and point to first Bcb in List.
//
Bcb = CONTAINING_RECORD( SharedCacheMap->BcbList.Flink, BCB, BcbLinks );
//
// Scan to the end of the Bcb list.
//
while (&Bcb->BcbLinks != &SharedCacheMap->BcbList) {
//
// If the Bcb is dirty, then capture the inputs for the
// callback routine so we can call without holding a spinlock.
//
LoopsWithLockHeld += 1;
if ((Bcb->NodeTypeCode == CACHE_NTC_BCB) && Bcb->Dirty) {
SavedFileOffset = Bcb->FileOffset;
SavedByteLength = Bcb->ByteLength;
SavedOldestLsn = Bcb->OldestLsn;
SavedNewestLsn = Bcb->NewestLsn;
//
// Increment PinCount so the Bcb sticks around
//
Bcb->PinCount += 1;
ExReleaseFastLock( &CcMasterSpinLock, OldIrql );
//
// Any Bcb to unpin from a previous loop?
//
if (BcbToUnpin != NULL) {
CcUnpinFileData( BcbToUnpin, TRUE, UNPIN );
BcbToUnpin = NULL;
}
//
// Call the file system
//
(*DirtyPageRoutine)( SharedCacheMap->FileObject,
&SavedFileOffset,
SavedByteLength,
&SavedOldestLsn,
&SavedNewestLsn,
Context1,
Context2 );
//
// Possibly update OldestLsn
//
if ((SavedOldestLsn.QuadPart != 0) &&
((OldestLsn.QuadPart == 0) || (SavedOldestLsn.QuadPart < OldestLsn.QuadPart ))) {
OldestLsn = SavedOldestLsn;
}
//
// Now reacquire the spinlock and scan from the resume point
// point to the next Bcb to return in the descending list.
//
ExAcquireFastLock( &CcMasterSpinLock, &OldIrql );
//
// Normally the Bcb can stay around a while, but if not,
// we will just remember it for the next time we do not
// have the spin lock. We cannot unpin it now, because
// we would lose our place in the list.
//
if (Bcb->Dirty || (Bcb->PinCount > 1)) {
Bcb->PinCount -= 1;
} else {
BcbToUnpin = Bcb;
}
//
// Normally the Bcb is not going away now, but if it is
// we need to free it by calling the normal routine
LoopsWithLockHeld = 0;
}
Bcb = CONTAINING_RECORD( Bcb->BcbLinks.Flink, BCB, BcbLinks );
}
//
// We need to unpin any Bcb we are holding before moving on to
// the next SharedCacheMap, or else CcDeleteSharedCacheMap will
// also delete this Bcb.
//
if (BcbToUnpin != NULL) {
ExReleaseFastLock( &CcMasterSpinLock, OldIrql );
CcUnpinFileData( BcbToUnpin, TRUE, UNPIN );
BcbToUnpin = NULL;
ExAcquireFastLock( &CcMasterSpinLock, &OldIrql );
}
//
// Now release the SharedCacheMap, leaving it in the dirty list.
//
SharedCacheMap->OpenCount -= 1;
SharedCacheMap->DirtyPages -= 1;
}
//
// Make sure we occassionally drop the lock. Set WRITE_QUEUED
// to keep the guy from going away, and increment DirtyPages to
// keep in in this list.
//
if ((++LoopsWithLockHeld >= 20) &&
!FlagOn(SharedCacheMap->Flags, WRITE_QUEUED | IS_CURSOR)) {
SetFlag(SharedCacheMap->Flags, WRITE_QUEUED);
SharedCacheMap->DirtyPages += 1;
ExReleaseFastLock( &CcMasterSpinLock, OldIrql );
LoopsWithLockHeld = 0;
ExAcquireFastLock( &CcMasterSpinLock, &OldIrql );
ClearFlag(SharedCacheMap->Flags, WRITE_QUEUED);
SharedCacheMap->DirtyPages -= 1;
}
//
// Now loop back for the next cache map.
//
SharedCacheMap =
CONTAINING_RECORD( SharedCacheMap->SharedCacheMapLinks.Flink,
SHARED_CACHE_MAP,
SharedCacheMapLinks );
}
ExReleaseFastLock( &CcMasterSpinLock, OldIrql );
return OldestLsn;
}
BOOLEAN
CcIsThereDirtyData (
IN PVPB Vpb
)
/*++
Routine Description:
This routine returns TRUE if the specified Vcb has any unwritten dirty
data in the cache.
Arguments:
Vpb - specifies Vpb to check for
Return Value:
FALSE - if the Vpb has no dirty data
TRUE - if the Vpb has dirty data
--*/
{
PSHARED_CACHE_MAP SharedCacheMap;
KIRQL OldIrql;
ULONG LoopsWithLockHeld = 0;
//
// Synchronize with changes to the SharedCacheMap list.
//
ExAcquireFastLock( &CcMasterSpinLock, &OldIrql );
SharedCacheMap = CONTAINING_RECORD( CcDirtySharedCacheMapList.SharedCacheMapLinks.Flink,
SHARED_CACHE_MAP,
SharedCacheMapLinks );
while (&SharedCacheMap->SharedCacheMapLinks != &CcDirtySharedCacheMapList.SharedCacheMapLinks) {
//
// Look at this one if the Vpb matches and if there is dirty data.
// For what it's worth, don't worry about dirty data in temporary files,
// as that should not concern the caller if it wants to dismount.
//
if (!FlagOn(SharedCacheMap->Flags, IS_CURSOR) &&
(SharedCacheMap->FileObject->Vpb == Vpb) &&
(SharedCacheMap->DirtyPages != 0) &&
!FlagOn(SharedCacheMap->FileObject->Flags, FO_TEMPORARY_FILE)) {
ExReleaseFastLock( &CcMasterSpinLock, OldIrql );
return TRUE;
}
//
// Make sure we occassionally drop the lock. Set WRITE_QUEUED
// to keep the guy from going away, and increment DirtyPages to
// keep in in this list.
//
if ((++LoopsWithLockHeld >= 20) &&
!FlagOn(SharedCacheMap->Flags, WRITE_QUEUED | IS_CURSOR)) {
SetFlag(SharedCacheMap->Flags, WRITE_QUEUED);
SharedCacheMap->DirtyPages += 1;
ExReleaseFastLock( &CcMasterSpinLock, OldIrql );
LoopsWithLockHeld = 0;
ExAcquireFastLock( &CcMasterSpinLock, &OldIrql );
ClearFlag(SharedCacheMap->Flags, WRITE_QUEUED);
SharedCacheMap->DirtyPages -= 1;
}
//
// Now loop back for the next cache map.
//
SharedCacheMap =
CONTAINING_RECORD( SharedCacheMap->SharedCacheMapLinks.Flink,
SHARED_CACHE_MAP,
SharedCacheMapLinks );
}
ExReleaseFastLock( &CcMasterSpinLock, OldIrql );
return FALSE;
}
LARGE_INTEGER
CcGetLsnForFileObject(
IN PFILE_OBJECT FileObject,
OUT PLARGE_INTEGER OldestLsn OPTIONAL
)
/*++
Routine Description:
This routine returns the oldest and newest LSNs for a file object.
Arguments:
FileObject - File for which the log handle should be stored.
OldestLsn - pointer to location to store oldest LSN for file object.
Return Value:
The newest LSN for the file object.
--*/
{
PBCB Bcb;
KIRQL OldIrql;
LARGE_INTEGER Oldest, Newest;
PSHARED_CACHE_MAP SharedCacheMap = FileObject->SectionObjectPointer->SharedCacheMap;
//
// initialize lsn variables
//
Oldest.LowPart = 0;
Oldest.HighPart = 0;
Newest.LowPart = 0;
Newest.HighPart = 0;
if(SharedCacheMap == NULL) {
return Oldest;
}
ExAcquireFastLock(&CcMasterSpinLock, &OldIrql);
//
// Now point to first Bcb in List, and loop through it.
//
Bcb = CONTAINING_RECORD( SharedCacheMap->BcbList.Flink, BCB, BcbLinks );
while (&Bcb->BcbLinks != &SharedCacheMap->BcbList) {
//
// If the Bcb is dirty then capture the oldest and newest lsn
//
if ((Bcb->NodeTypeCode == CACHE_NTC_BCB) && Bcb->Dirty) {
LARGE_INTEGER BcbLsn, BcbNewest;
BcbLsn = Bcb->OldestLsn;
BcbNewest = Bcb->NewestLsn;
if ((BcbLsn.QuadPart != 0) &&
((Oldest.QuadPart == 0) ||
(BcbLsn.QuadPart < Oldest.QuadPart))) {
Oldest = BcbLsn;
}
if ((BcbLsn.QuadPart != 0) && (BcbNewest.QuadPart > Newest.QuadPart)) {
Newest = BcbNewest;
}
}
Bcb = CONTAINING_RECORD( Bcb->BcbLinks.Flink, BCB, BcbLinks );
}
//
// Now release the spin lock for this Bcb list and generate a callback
// if we got something.
//
ExReleaseFastLock( &CcMasterSpinLock, OldIrql );
if (ARGUMENT_PRESENT(OldestLsn)) {
*OldestLsn = Oldest;
}
return Newest;
}
|