Sample Code

windows driver samples/ cdfs file system driver/ C++/ cachesup.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
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
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
/*++
 
Copyright (c) 1990-2000 Microsoft Corporation
 
Module Name:
 
    Cache.c
 
Abstract:
 
    This module implements the cache management routines for the Cdfs
    FSD and FSP, by calling the Common Cache Manager.
 
 
--*/
 
#include "CdProcs.h"
 
//
//  The Bug check file id for this module
//
 
#define BugCheckFileId                   (CDFS_BUG_CHECK_CACHESUP)
 
//
//  Local debug trace level
//
 
#ifdef ALLOC_PRAGMA
#pragma alloc_text(PAGE, CdCompleteMdl)
#pragma alloc_text(PAGE, CdCreateInternalStream)
#pragma alloc_text(PAGE, CdDeleteInternalStream)
#pragma alloc_text(PAGE, CdPurgeVolume)
#endif
 
VOID
CdCreateInternalStream (
    _In_ PIRP_CONTEXT IrpContext,
    _In_ PVCB Vcb,
    _Inout_ PFCB Fcb,
    _In_ PUNICODE_STRING Name
    )
 
/*++
 
Routine Description:
 
    This function creates an internal stream file for interaction
    with the cache manager.  The Fcb here can be for either a
    directory stream or for a path table stream.
 
Arguments:
 
    Vcb - Vcb for this volume.
 
    Fcb - Points to the Fcb for this file.  It is either an Index or
        Path Table Fcb.
 
Return Value:
 
    None.
 
--*/
 
{
    PFILE_OBJECT StreamFile = NULL;
    BOOLEAN DecrementReference = FALSE;
 
    BOOLEAN CleanupDirContext = FALSE;
    BOOLEAN UpdateFcbSizes = FALSE;
 
    DIRENT Dirent = {0};
    DIRENT_ENUM_CONTEXT DirContext = {0};
 
    PAGED_CODE();
 
    ASSERT_IRP_CONTEXT( IrpContext );
    ASSERT_FCB( Fcb );
 
    //
    //  We may only have the Fcb shared.  Lock the Fcb and do a
    //  safe test to see if we need to really create the file object.
    //
 
    CdLockFcb( IrpContext, Fcb );
 
    if (Fcb->FileObject != NULL) {
 
        CdUnlockFcb( IrpContext, Fcb );
        return;
    }
 
    //
    //  Use a try-finally to facilitate cleanup.
    //
 
    try {
 
        //
        //  Create the internal stream.  The Vpb should be pointing at our volume
        //  device object at this point.
        //
 
        StreamFile = IoCreateStreamFileObjectLite( NULL, Vcb->Vpb->RealDevice );
 
        if (StreamFile == NULL) {
 
            CdRaiseStatus( IrpContext, STATUS_INSUFFICIENT_RESOURCES );
        }
 
        //
        //  Initialize the fields of the file object.
        //
 
        StreamFile->ReadAccess = TRUE;
        StreamFile->WriteAccess = FALSE;
        StreamFile->DeleteAccess = FALSE;
 
        StreamFile->SectionObjectPointer = &Fcb->FcbNonpaged->SegmentObject;
 
        //
        //  Set the file object type and increment the Vcb counts.
        //
 
        CdSetFileObject( IrpContext,
                         StreamFile,
                         StreamFileOpen,
                         Fcb,
                         NULL );
         
        //
        //  We'll give stream file objects a name to aid IO profiling etc. We
        //  NULL this in CdDeleteInternalStream before OB deletes the file object,
        //  and before CdRemovePrefix is called (which frees Fcb names).
        //
 
        StreamFile->FileName = *Name;
 
        //
        //  We will reference the current Fcb twice to keep it from going
        //  away in the error path.  Otherwise if we dereference it
        //  below in the finally clause a close could cause the Fcb to
        //  be deallocated.
        //
 
        CdLockVcb( IrpContext, Vcb );
        CdIncrementReferenceCounts( IrpContext, Fcb, 2, 0 );
        CdUnlockVcb( IrpContext, Vcb );
        DecrementReference = TRUE;
 
        //
        //  Initialize the cache map for the file.
        //
 
        CcInitializeCacheMap( StreamFile,
                              (PCC_FILE_SIZES)&Fcb->AllocationSize,
                              TRUE,
                              &CdData.CacheManagerCallbacks,
                              Fcb );
 
        //
        //  Go ahead and store the stream file into the Fcb.
        //
 
        Fcb->FileObject = StreamFile;
        StreamFile = NULL;
 
        //
        //  If this is the first file object for a directory then we need to
        //  read the self entry for this directory and update the sizes
        //  in the Fcb.  We know that the Fcb has been initialized so
        //  that we have a least one sector available to read.
        //
 
        if (!FlagOn( Fcb->FcbState, FCB_STATE_INITIALIZED )) {
 
            ULONG NewDataLength;
 
            //
            //  Initialize the search structures.
            //
 
            CdInitializeDirContext( IrpContext, &DirContext );
            CdInitializeDirent( IrpContext, &Dirent );
            CleanupDirContext = TRUE;
 
            //
            //  Read the dirent from disk and transfer the data to the
            //  in-memory dirent.
            //
 
            CdLookupDirent( IrpContext,
                            Fcb,
                            Fcb->StreamOffset,
                            &DirContext );
 
            CdUpdateDirentFromRawDirent( IrpContext, Fcb, &DirContext, &Dirent );
 
            //
            //  Verify that this really for the self entry.  We do this by
            //  updating the name in the dirent and then checking that it matches
            //  one of the hard coded names.
            //
 
            CdUpdateDirentName( IrpContext, &Dirent, FALSE );
 
            if (Dirent.CdFileName.FileName.Buffer != CdUnicodeSelfArray) {
 
                CdRaiseStatus( IrpContext, STATUS_FILE_CORRUPT_ERROR );
            }
 
            //
            //  If the data sizes are different then update the header
            //  and Mcb for this Fcb.
            //
 
            NewDataLength = BlockAlign( Vcb, Dirent.DataLength + Fcb->StreamOffset );
 
            if (NewDataLength == 0) {
 
                CdRaiseStatus( IrpContext, STATUS_FILE_CORRUPT_ERROR );
            }
 
            if (NewDataLength != Fcb->FileSize.QuadPart) {
 
                Fcb->AllocationSize.QuadPart =
                Fcb->FileSize.QuadPart =
                Fcb->ValidDataLength.QuadPart = NewDataLength;
 
                CcSetFileSizes( Fcb->FileObject, (PCC_FILE_SIZES) &Fcb->AllocationSize );
 
                CdTruncateAllocation( IrpContext, Fcb, 0 );
                CdAddInitialAllocation( IrpContext,
                                        Fcb,
                                        Dirent.StartingOffset,
                                        NewDataLength );
 
                UpdateFcbSizes = TRUE;
            }
 
            //
            //  Check for the existence flag and transform to hidden.
            //
 
            if (FlagOn( Dirent.DirentFlags, CD_ATTRIBUTE_HIDDEN )) {
 
                SetFlag( Fcb->FileAttributes, FILE_ATTRIBUTE_HIDDEN );
            }
 
            //
            //  Convert the time to NT time.
            //
 
            CdConvertCdTimeToNtTime( IrpContext,
                                     Dirent.CdTime,
                                     (PLARGE_INTEGER) &Fcb->CreationTime );
 
            //
            //  Update the Fcb flags to indicate we have read the
            //  self entry.
            //
 
            SetFlag( Fcb->FcbState, FCB_STATE_INITIALIZED );
 
            //
            //  If we updated the sizes then we want to purge the file.  Go
            //  ahead and unpin and then purge the first page.
            //
 
            CdCleanupDirContext( IrpContext, &DirContext );
            CdCleanupDirent( IrpContext, &Dirent );
            CleanupDirContext = FALSE;
 
            if (UpdateFcbSizes) {
 
                CcPurgeCacheSection( &Fcb->FcbNonpaged->SegmentObject,
                                     NULL,
                                     0,
                                     FALSE );
            }
        }
 
    } finally {
 
        //
        //  Cleanup any dirent structures we may have used.
        //
 
        if (CleanupDirContext) {
 
            CdCleanupDirContext( IrpContext, &DirContext );
            CdCleanupDirent( IrpContext, &Dirent );
        }
 
        //
        //  If we raised then we need to dereference the file object.
        //
 
        if (StreamFile != NULL) {
 
            //
            //  Null the name pointer, since the stream file object never actually
            //  'owns' the names, we just point it to existing ones.
            //
 
            StreamFile->FileName.Buffer = NULL;
            StreamFile->FileName.MaximumLength = StreamFile->FileName.Length = 0;
 
            ObDereferenceObject( StreamFile );
            Fcb->FileObject = NULL;
        }
 
        //
        //  Dereference and unlock the Fcb.
        //
 
        if (DecrementReference) {
 
            CdLockVcb( IrpContext, Vcb );
            CdDecrementReferenceCounts( IrpContext, Fcb, 1, 0 );
            CdUnlockVcb( IrpContext, Vcb );
        }
 
        CdUnlockFcb( IrpContext, Fcb );
    }
 
    return;
}
 
VOID
CdDeleteInternalStream (
    _In_ PIRP_CONTEXT IrpContext,
    _Inout_ PFCB Fcb
    )
 
/*++
 
Routine Description:
 
    This function creates an internal stream file for interaction
    with the cache manager.  The Fcb here can be for either a
    directory stream or for a path table stream.
 
Arguments:
 
    Fcb - Points to the Fcb for this file.  It is either an Index or
        Path Table Fcb.
 
Return Value:
 
    None.
 
--*/
 
{
    PFILE_OBJECT FileObject;
 
    PAGED_CODE();
 
    UNREFERENCED_PARAMETER( IrpContext );
 
    ASSERT_IRP_CONTEXT( IrpContext );
    ASSERT_FCB( Fcb );
 
    //
    //  Lock the Fcb.
    //
 
    CdLockFcb( IrpContext, Fcb );
 
    //
    //  Capture the file object.
    //
 
    FileObject = Fcb->FileObject;
    Fcb->FileObject = NULL;
 
    //
    //  It is now safe to unlock the Fcb.
    //
 
    CdUnlockFcb( IrpContext, Fcb );
 
    //
    //  Dereference the file object if present.
    //
 
    if (FileObject != NULL) {
 
        if (FileObject->PrivateCacheMap != NULL) {
 
            CcUninitializeCacheMap( FileObject, NULL, NULL );
        }
 
        //
        //  Null the name pointer, since the stream file object never actually
        //  'owns' the names, we just point it to existing ones.
        //
         
        FileObject->FileName.Buffer = NULL;
        FileObject->FileName.MaximumLength = FileObject->FileName.Length = 0;
 
        ObDereferenceObject( FileObject );
    }
}
 
NTSTATUS
CdCompleteMdl (
    _In_ PIRP_CONTEXT IrpContext,
    _Inout_ PIRP Irp
    )
 
/*++
 
Routine Description:
 
    This routine performs the function of completing Mdl reads.
    It should be called only from CdFsdRead.
 
Arguments:
 
    Irp - Supplies the originating Irp.
 
Return Value:
 
    NTSTATUS - Will always be STATUS_SUCCESS.
 
--*/
 
{
    PFILE_OBJECT FileObject;
 
    PAGED_CODE();
 
    //
    // Do completion processing.
    //
 
    FileObject = IoGetCurrentIrpStackLocation( Irp )->FileObject;
 
    CcMdlReadComplete( FileObject, Irp->MdlAddress );
 
    //
    // Mdl is now deallocated.
    //
 
    Irp->MdlAddress = NULL;
 
    //
    // Complete the request and exit right away.
    //
 
    CdCompleteRequest( IrpContext, Irp, STATUS_SUCCESS );
 
    return STATUS_SUCCESS;
}
 
 
_Requires_lock_held_(_Global_critical_region_)
NTSTATUS
CdPurgeVolume (
    _In_ PIRP_CONTEXT IrpContext,
    _In_ PVCB Vcb,
    _In_ BOOLEAN DismountUnderway
    )
 
/*++
 
Routine Description:
 
    This routine is called to purge the volume.  The purpose is to make all the stale file
    objects in the system go away in order to lock the volume.
 
    The Vcb is already acquired exclusively.  We will lock out all file operations by
    acquiring the global file resource.  Then we will walk through all of the Fcb's and
    perform the purge.
 
Arguments:
 
    Vcb - Vcb for the volume to purge.
 
    DismountUnderway - Indicates that we are trying to delete all of the objects.
        We will purge the Path Table and VolumeDasd and dereference all
        internal streams.
 
Return Value:
 
    NTSTATUS - The first failure of the purge operation.
 
--*/
 
{
    NTSTATUS Status = STATUS_SUCCESS;
 
    PVOID RestartKey = NULL;
    PFCB ThisFcb = NULL;
    PFCB NextFcb;
 
    BOOLEAN RemovedFcb;
 
    PAGED_CODE();
 
    ASSERT_EXCLUSIVE_VCB( Vcb);
 
    //
    //  Force any remaining Fcb's in the delayed close queue to be closed.
    //
 
    CdFspClose( Vcb );
 
    //
    //  Acquire the global file resource.
    //
 
    CdAcquireAllFiles( IrpContext, Vcb );
 
    //
    //  Loop through each Fcb in the Fcb Table and perform the flush.
    //
 
    while (TRUE) {
 
        //
        //  Lock the Vcb to lookup the next Fcb.
        //
 
        CdLockVcb( IrpContext, Vcb );
        NextFcb = CdGetNextFcb( IrpContext, Vcb, &RestartKey );
 
        //
        //  Reference the NextFcb if present.
        //
 
        if (NextFcb != NULL) {
 
            NextFcb->FcbReference += 1;
        }
 
        //
        //  If the last Fcb is present then decrement reference count and call teardown
        //  to see if it should be removed.
        //
 
        if (ThisFcb != NULL) {
 
            ThisFcb->FcbReference -= 1;
 
            CdUnlockVcb( IrpContext, Vcb );
 
            CdTeardownStructures( IrpContext, ThisFcb, &RemovedFcb );
 
        } else {
 
            CdUnlockVcb( IrpContext, Vcb );
        }
 
        //
        //  Break out of the loop if no more Fcb's.
        //
 
        if (NextFcb == NULL) {
 
            break;
        }
 
        //
        //  Move to the next Fcb.
        //
 
        ThisFcb = NextFcb;
 
        //
        //  If there is a image section then see if that can be closed.
        //
 
        if (ThisFcb->FcbNonpaged->SegmentObject.ImageSectionObject != NULL) {
 
            MmFlushImageSection( &ThisFcb->FcbNonpaged->SegmentObject, MmFlushForWrite );
        }
 
        //
        //  If there is a data section then purge this.  If there is an image
        //  section then we won't be able to.  Remember this if it is our first
        //  error.
        //
 
        if ((ThisFcb->FcbNonpaged->SegmentObject.DataSectionObject != NULL) &&
            !CcPurgeCacheSection( &ThisFcb->FcbNonpaged->SegmentObject,
                                   NULL,
                                   0,
                                   FALSE ) &&
            (Status == STATUS_SUCCESS)) {
 
            Status = STATUS_UNABLE_TO_DELETE_SECTION;
        }
 
        //
        //  Dereference the internal stream if dismounting.
        //
 
        if (DismountUnderway &&
            (SafeNodeType( ThisFcb ) != CDFS_NTC_FCB_DATA) &&
            (ThisFcb->FileObject != NULL)) {
 
            CdDeleteInternalStream( IrpContext, ThisFcb );
        }
    }
 
    //
    //  Now look at the path table and volume Dasd Fcb's.
    //
 
    if (DismountUnderway) {
 
        if (Vcb->PathTableFcb != NULL) {
 
            ThisFcb = Vcb->PathTableFcb;
            InterlockedIncrement( (LONG*)&Vcb->PathTableFcb->FcbReference );
 
            if ((ThisFcb->FcbNonpaged->SegmentObject.DataSectionObject != NULL) &&
                !CcPurgeCacheSection( &ThisFcb->FcbNonpaged->SegmentObject,
                                       NULL,
                                       0,
                                       FALSE ) &&
                (Status == STATUS_SUCCESS)) {
 
                Status = STATUS_UNABLE_TO_DELETE_SECTION;
            }
 
            CdDeleteInternalStream( IrpContext, ThisFcb );
 
            InterlockedDecrement( (LONG*)&ThisFcb->FcbReference );
 
            CdTeardownStructures( IrpContext, ThisFcb, &RemovedFcb );
        }
 
        if (Vcb->VolumeDasdFcb != NULL) {
 
            ThisFcb = Vcb->VolumeDasdFcb;
            InterlockedIncrement( (LONG*)&ThisFcb->FcbReference );
 
            if ((ThisFcb->FcbNonpaged->SegmentObject.DataSectionObject != NULL) &&
                !CcPurgeCacheSection( &ThisFcb->FcbNonpaged->SegmentObject,
                                       NULL,
                                       0,
                                       FALSE ) &&
                (Status == STATUS_SUCCESS)) {
 
                Status = STATUS_UNABLE_TO_DELETE_SECTION;
            }
 
            InterlockedDecrement( (LONG*)&ThisFcb->FcbReference );
 
            CdTeardownStructures( IrpContext, ThisFcb, &RemovedFcb );
        }
    }
 
    //
    //  Release all of the files.
    //
 
    CdReleaseAllFiles( IrpContext, Vcb );
 
    return Status;
}

Our Services

  • What our customers say about us?

© 2011-2025 All Rights Reserved. Joya Systems. 4425 South Mopac Building II Suite 101 Austin, TX 78735 Tel: 800-DEV-KERNEL

Privacy Policy. Terms of use. Valid XHTML & CSS