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
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
|
/*++
Copyright (c) 1997-2000 Microsoft Corporation
Module Name:
Pnp.c
Abstract:
This module implements the Plug and Play routines for FAT called by
the dispatch driver.
--*/
#include "FatProcs.h"
//
// The Bug check file id for this module
//
#define BugCheckFileId (FAT_BUG_CHECK_PNP)
#define Dbg (DEBUG_TRACE_PNP)
_Requires_lock_held_(_Global_critical_region_)
_Requires_lock_held_(FatData.Resource)
NTSTATUS
FatPnpQueryRemove (
PIRP_CONTEXT IrpContext,
PIRP Irp,
PVCB Vcb
);
_Requires_lock_held_(_Global_critical_region_)
NTSTATUS
FatPnpRemove (
PIRP_CONTEXT IrpContext,
PIRP Irp,
PVCB Vcb
);
_Requires_lock_held_(_Global_critical_region_)
NTSTATUS
FatPnpSurpriseRemove (
PIRP_CONTEXT IrpContext,
PIRP Irp,
PVCB Vcb
);
_Requires_lock_held_(_Global_critical_region_)
NTSTATUS
FatPnpCancelRemove (
PIRP_CONTEXT IrpContext,
PIRP Irp,
PVCB Vcb
);
IO_COMPLETION_ROUTINE FatPnpCompletionRoutine;
NTSTATUS
FatPnpCompletionRoutine (
_In_ PDEVICE_OBJECT DeviceObject,
_In_ PIRP Irp,
_In_reads_opt_(_Inexpressible_("varies")) PVOID Contxt
);
#ifdef ALLOC_PRAGMA
#pragma alloc_text(PAGE, FatCommonPnp)
#pragma alloc_text(PAGE, FatFsdPnp)
#pragma alloc_text(PAGE, FatPnpCancelRemove)
#pragma alloc_text(PAGE, FatPnpQueryRemove)
#pragma alloc_text(PAGE, FatPnpRemove)
#pragma alloc_text(PAGE, FatPnpSurpriseRemove)
#endif
_Function_class_(IRP_MJ_PNP)
_Function_class_(DRIVER_DISPATCH)
NTSTATUS
FatFsdPnp (
_In_ PVOLUME_DEVICE_OBJECT VolumeDeviceObject,
_Inout_ PIRP Irp
)
/*++
Routine Description:
This routine implements the FSD part of PnP operations
Arguments:
VolumeDeviceObject - Supplies the volume device object where the
file exists
Irp - Supplies the Irp being processed
Return Value:
NTSTATUS - The FSD status for the IRP
--*/
{
NTSTATUS Status;
PIRP_CONTEXT IrpContext = NULL;
BOOLEAN TopLevel;
BOOLEAN Wait;
PAGED_CODE();
DebugTrace(+1, Dbg, "FatFsdPnp\n", 0);
FsRtlEnterFileSystem();
TopLevel = FatIsIrpTopLevel( Irp );
try {
//
// We expect there to never be a fileobject, in which case we will always
// wait. Since at the moment we don't have any concept of pending Pnp
// operations, this is a bit nitpicky.
//
if (IoGetCurrentIrpStackLocation( Irp )->FileObject == NULL) {
Wait = TRUE;
} else {
Wait = CanFsdWait( Irp );
}
IrpContext = FatCreateIrpContext( Irp, Wait );
Status = FatCommonPnp( IrpContext, Irp );
} except(FatExceptionFilter( IrpContext, GetExceptionInformation() )) {
//
// We had some trouble trying to perform the requested
// operation, so we'll abort the I/O request with
// the error status that we get back from the
// execption code
//
Status = FatProcessException( IrpContext, Irp, GetExceptionCode() );
}
if (TopLevel) { IoSetTopLevelIrp( NULL ); }
FsRtlExitFileSystem();
//
// And return to our caller
//
DebugTrace(-1, Dbg, "FatFsdPnp -> %08lx\n", Status);
UNREFERENCED_PARAMETER( VolumeDeviceObject );
return Status;
}
_Requires_lock_held_(_Global_critical_region_)
NTSTATUS
FatCommonPnp (
IN PIRP_CONTEXT IrpContext,
IN PIRP Irp
)
/*++
Routine Description:
This is the common routine for doing PnP operations called
by both the fsd and fsp threads
Arguments:
Irp - Supplies the Irp to process
Return Value:
NTSTATUS - The return status for the operation
--*/
{
NTSTATUS Status;
PIO_STACK_LOCATION IrpSp;
PVOLUME_DEVICE_OBJECT OurDeviceObject;
PVCB Vcb;
PAGED_CODE();
//
// Force everything to wait.
//
SetFlag( IrpContext->Flags, IRP_CONTEXT_FLAG_WAIT);
//
// Get the current Irp stack location.
//
IrpSp = IoGetCurrentIrpStackLocation( Irp );
//
// Find our Vcb. This is tricky since we have no file object in the Irp.
//
OurDeviceObject = (PVOLUME_DEVICE_OBJECT) IrpSp->DeviceObject;
//
// Take the global lock to synchronise against volume teardown.
//
#pragma prefast( push )
#pragma prefast( disable: 28137, "prefast wants the wait to be a constant, but that isn't possible for the way fastfat is designed" )
#pragma prefast( disable: 28193, "this will always wait" )
FatAcquireExclusiveGlobal( IrpContext );
#pragma prefast( pop )
//
// Make sure this device object really is big enough to be a volume device
// object. If it isn't, we need to get out before we try to reference some
// field that takes us past the end of an ordinary device object.
//
#pragma prefast( suppress: 28175, "touching Size is ok for a filesystem" )
if (OurDeviceObject->DeviceObject.Size != sizeof(VOLUME_DEVICE_OBJECT) ||
NodeType( &OurDeviceObject->Vcb ) != FAT_NTC_VCB) {
//
// We were called with something we don't understand.
//
FatReleaseGlobal( IrpContext );
Status = STATUS_INVALID_PARAMETER;
FatCompleteRequest( IrpContext, Irp, Status );
return Status;
}
Vcb = &OurDeviceObject->Vcb;
//
// Case on the minor code.
//
switch ( IrpSp->MinorFunction ) {
case IRP_MN_QUERY_REMOVE_DEVICE:
Status = FatPnpQueryRemove( IrpContext, Irp, Vcb );
break;
case IRP_MN_SURPRISE_REMOVAL:
Status = FatPnpSurpriseRemove( IrpContext, Irp, Vcb );
break;
case IRP_MN_REMOVE_DEVICE:
Status = FatPnpRemove( IrpContext, Irp, Vcb );
break;
case IRP_MN_CANCEL_REMOVE_DEVICE:
Status = FatPnpCancelRemove( IrpContext, Irp, Vcb );
break;
default:
FatReleaseGlobal( IrpContext );
//
// Just pass the IRP on. As we do not need to be in the
// way on return, ellide ourselves out of the stack.
//
IoSkipCurrentIrpStackLocation( Irp );
Status = IoCallDriver(Vcb->TargetDeviceObject, Irp);
//
// Cleanup our Irp Context. The driver has completed the Irp.
//
FatCompleteRequest( IrpContext, NULL, STATUS_SUCCESS );
break;
}
return Status;
}
VOID
FatPnpAdjustVpbRefCount(
IN PVCB Vcb,
IN ULONG Delta
)
{
KIRQL OldIrql;
IoAcquireVpbSpinLock( &OldIrql);
Vcb->Vpb->ReferenceCount += Delta;
IoReleaseVpbSpinLock( OldIrql);
}
_Requires_lock_held_(_Global_critical_region_)
_Requires_lock_held_(FatData.Resource)
NTSTATUS
FatPnpQueryRemove (
PIRP_CONTEXT IrpContext,
PIRP Irp,
PVCB Vcb
)
/*++
Routine Description:
This routine handles the PnP query remove operation. The filesystem
is responsible for answering whether there are any reasons it sees
that the volume can not go away (and the device removed). Initiation
of the dismount begins when we answer yes to this question.
Query will be followed by a Cancel or Remove.
Arguments:
Irp - Supplies the Irp to process
Vcb - Supplies the volume being queried.
Return Value:
NTSTATUS - The return status for the operation
--*/
{
NTSTATUS Status = STATUS_SUCCESS;
KEVENT Event;
BOOLEAN VcbDeleted = FALSE;
BOOLEAN GlobalHeld = TRUE;
PAGED_CODE();
//
// Having said yes to a QUERY, any communication with the
// underlying storage stack is undefined (and may block)
// until the bounding CANCEL or REMOVE is sent.
//
FatAcquireExclusiveVcb( IrpContext, Vcb );
FatReleaseGlobal( IrpContext);
GlobalHeld = FALSE;
try {
Status = FatLockVolumeInternal( IrpContext, Vcb, NULL );
//
// Drop an additional reference on the Vpb so that the volume cannot be
// torn down when we drop all the locks below.
//
FatPnpAdjustVpbRefCount( Vcb, 1);
//
// Drop and reacquire the resources in the right order.
//
FatReleaseVcb( IrpContext, Vcb );
NT_ASSERT( FlagOn(IrpContext->Flags, IRP_CONTEXT_FLAG_WAIT) );
#pragma prefast( push )
#pragma prefast( disable: 28137, "prefast wants the wait to be a constant, but that isn't possible for the way fastfat is designed" )
#pragma prefast( disable: 28193, "this will always wait" )
FatAcquireExclusiveGlobal( IrpContext );
GlobalHeld = TRUE;
#pragma prefast( pop )
FatAcquireExclusiveVcb( IrpContext, Vcb );
//
// Drop the reference we added above.
//
FatPnpAdjustVpbRefCount( Vcb, (ULONG)-1);
if (NT_SUCCESS( Status )) {
//
// With the volume held locked, note that we must finalize as much
// as possible right now.
//
FatFlushAndCleanVolume( IrpContext, Irp, Vcb, Flush );
//
// We need to pass this down before starting the dismount, which
// could disconnect us immediately from the stack.
//
//
// Get the next stack location, and copy over the stack location
//
IoCopyCurrentIrpStackLocationToNext( Irp );
//
// Set up the completion routine
//
KeInitializeEvent( &Event, NotificationEvent, FALSE );
IoSetCompletionRoutine( Irp,
FatPnpCompletionRoutine,
&Event,
TRUE,
TRUE,
TRUE );
//
// Send the request and wait.
//
Status = IoCallDriver(Vcb->TargetDeviceObject, Irp);
if (Status == STATUS_PENDING) {
KeWaitForSingleObject( &Event,
Executive,
KernelMode,
FALSE,
NULL );
Status = Irp->IoStatus.Status;
}
//
// Now if no one below us failed already, initiate the dismount
// on this volume, make it go away. PnP needs to see our internal
// streams close and drop their references to the target device.
//
// Since we were able to lock the volume, we are guaranteed to
// move this volume into dismount state and disconnect it from
// the underlying storage stack. The force on our part is actually
// unnecesary, though complete.
//
// What is not strictly guaranteed, though, is that the closes
// for the metadata streams take effect synchronously underneath
// of this call. This would leave references on the target device
// even though we are disconnected!
//
if (NT_SUCCESS( Status )) {
VcbDeleted = FatCheckForDismount( IrpContext, Vcb, TRUE );
NT_ASSERT( VcbDeleted || Vcb->VcbCondition == VcbBad );
}
}
} finally {
//
// Release the Vcb if it could still remain.
//
if (!VcbDeleted) {
FatReleaseVcb( IrpContext, Vcb );
}
if (GlobalHeld) {
FatReleaseGlobal( IrpContext );
}
}
//
// Cleanup our IrpContext and complete the IRP if neccesary.
//
FatCompleteRequest( IrpContext, Irp, Status );
return Status;
}
_Requires_lock_held_(_Global_critical_region_)
NTSTATUS
FatPnpRemove (
PIRP_CONTEXT IrpContext,
PIRP Irp,
PVCB Vcb
)
/*++
Routine Description:
This routine handles the PnP remove operation. This is our notification
that the underlying storage device for the volume we have is gone, and
an excellent indication that the volume will never reappear. The filesystem
is responsible for initiation or completion of the dismount.
Arguments:
Irp - Supplies the Irp to process
Vcb - Supplies the volume being removed.
Return Value:
NTSTATUS - The return status for the operation
--*/
{
NTSTATUS Status;
KEVENT Event;
BOOLEAN VcbDeleted = FALSE;
PAGED_CODE();
//
// REMOVE - a storage device is now gone. We either got
// QUERY'd and said yes OR got a SURPRISE OR a storage
// stack failed to spin back up from a sleep/stop state
// (the only case in which this will be the first warning).
//
// Note that it is entirely unlikely that we will be around
// for a REMOVE in the first two cases, as we try to intiate
// dismount.
//
//
// Acquire the global resource so that we can try to vaporize
// the volume, and the vcb resource itself.
//
FatAcquireExclusiveVcb( IrpContext, Vcb );
//
// The device will be going away. Remove our lock (benign
// if we never had it).
//
(VOID) FatUnlockVolumeInternal( IrpContext, Vcb, NULL );
//
// We need to pass this down before starting the dismount, which
// could disconnect us immediately from the stack.
//
//
// Get the next stack location, and copy over the stack location
//
IoCopyCurrentIrpStackLocationToNext( Irp );
//
// Set up the completion routine
//
KeInitializeEvent( &Event, NotificationEvent, FALSE );
IoSetCompletionRoutine( Irp,
FatPnpCompletionRoutine,
&Event,
TRUE,
TRUE,
TRUE );
//
// Send the request and wait.
//
Status = IoCallDriver(Vcb->TargetDeviceObject, Irp);
if (Status == STATUS_PENDING) {
KeWaitForSingleObject( &Event,
Executive,
KernelMode,
FALSE,
NULL );
Status = Irp->IoStatus.Status;
}
try {
//
// Knock as many files down for this volume as we can.
//
FatFlushAndCleanVolume( IrpContext, Irp, Vcb, NoFlush );
//
// Now make our dismount happen. This may not vaporize the
// Vcb, of course, since there could be any number of handles
// outstanding if we were not preceeded by a QUERY.
//
// PnP will take care of disconnecting this stack if we
// couldn't get off of it immediately.
//
VcbDeleted = FatCheckForDismount( IrpContext, Vcb, TRUE );
} finally {
//
// Release the Vcb if it could still remain.
//
if (!VcbDeleted) {
FatReleaseVcb( IrpContext, Vcb );
}
FatReleaseGlobal( IrpContext );
}
//
// Cleanup our IrpContext and complete the IRP.
//
FatCompleteRequest( IrpContext, Irp, Status );
return Status;
}
_Requires_lock_held_(_Global_critical_region_)
NTSTATUS
FatPnpSurpriseRemove (
PIRP_CONTEXT IrpContext,
PIRP Irp,
PVCB Vcb
)
/*++
Routine Description:
This routine handles the PnP surprise remove operation. This is another
type of notification that the underlying storage device for the volume we
have is gone, and is excellent indication that the volume will never reappear.
The filesystem is responsible for initiation or completion the dismount.
For the most part, only "real" drivers care about the distinction of a
surprise remove, which is a result of our noticing that a user (usually)
physically reached into the machine and pulled something out.
Surprise will be followed by a Remove when all references have been shut down.
Arguments:
Irp - Supplies the Irp to process
Vcb - Supplies the volume being removed.
Return Value:
NTSTATUS - The return status for the operation
--*/
{
NTSTATUS Status;
KEVENT Event;
BOOLEAN VcbDeleted = FALSE;
PAGED_CODE();
//
// SURPRISE - a device was physically yanked away without
// any warning. This means external forces.
//
FatAcquireExclusiveVcb( IrpContext, Vcb );
//
// We need to pass this down before starting the dismount, which
// could disconnect us immediately from the stack.
//
//
// Get the next stack location, and copy over the stack location
//
IoCopyCurrentIrpStackLocationToNext( Irp );
//
// Set up the completion routine
//
KeInitializeEvent( &Event, NotificationEvent, FALSE );
IoSetCompletionRoutine( Irp,
FatPnpCompletionRoutine,
&Event,
TRUE,
TRUE,
TRUE );
//
// Send the request and wait.
//
Status = IoCallDriver(Vcb->TargetDeviceObject, Irp);
if (Status == STATUS_PENDING) {
KeWaitForSingleObject( &Event,
Executive,
KernelMode,
FALSE,
NULL );
Status = Irp->IoStatus.Status;
}
try {
//
// Knock as many files down for this volume as we can.
//
FatFlushAndCleanVolume( IrpContext, Irp, Vcb, NoFlush );
//
// Now make our dismount happen. This may not vaporize the
// Vcb, of course, since there could be any number of handles
// outstanding since this is an out of band notification.
//
VcbDeleted = FatCheckForDismount( IrpContext, Vcb, TRUE );
} finally {
//
// Release the Vcb if it could still remain.
//
if (!VcbDeleted) {
FatReleaseVcb( IrpContext, Vcb );
}
FatReleaseGlobal( IrpContext );
}
//
// Cleanup our IrpContext and complete the IRP.
//
FatCompleteRequest( IrpContext, Irp, Status );
return Status;
}
_Requires_lock_held_(_Global_critical_region_)
NTSTATUS
FatPnpCancelRemove (
PIRP_CONTEXT IrpContext,
PIRP Irp,
PVCB Vcb
)
/*++
Routine Description:
This routine handles the PnP cancel remove operation. This is our
notification that a previously proposed remove (query) was eventually
vetoed by a component. The filesystem is responsible for cleaning up
and getting ready for more IO.
Arguments:
Irp - Supplies the Irp to process
Vcb - Supplies the volume being removed.
Return Value:
NTSTATUS - The return status for the operation
--*/
{
NTSTATUS Status = STATUS_SUCCESS;
PAGED_CODE();
//
// CANCEL - a previous QUERY has been rescinded as a result
// of someone vetoing. Since PnP cannot figure out who may
// have gotten the QUERY (think about it: stacked drivers),
// we must expect to deal with getting a CANCEL without having
// seen the QUERY.
//
// For FAT, this is quite easy. In fact, we can't get a
// CANCEL if the underlying drivers succeeded the QUERY since
// we disconnect the Vpb on our dismount initiation. This is
// actually pretty important because if PnP could get to us
// after the disconnect we'd be thoroughly unsynchronized
// with respect to the Vcb getting torn apart - merely referencing
// the volume device object is insufficient to keep us intact.
//
FatAcquireExclusiveVcb( IrpContext, Vcb );
FatReleaseGlobal( IrpContext);
//
// Unlock the volume. This is benign if we never had seen
// a QUERY.
//
(VOID)FatUnlockVolumeInternal( IrpContext, Vcb, NULL );
try {
//
// Send the request. The underlying driver will complete the
// IRP. Since we don't need to be in the way, simply ellide
// ourselves out of the IRP stack.
//
IoSkipCurrentIrpStackLocation( Irp );
Status = IoCallDriver(Vcb->TargetDeviceObject, Irp);
}
finally {
FatReleaseVcb( IrpContext, Vcb );
}
FatCompleteRequest( IrpContext, NULL, STATUS_SUCCESS );
return Status;
}
//
// Local support routine
//
NTSTATUS
FatPnpCompletionRoutine (
_In_ PDEVICE_OBJECT DeviceObject,
_In_ PIRP Irp,
_In_reads_opt_(_Inexpressible_("varies")) PVOID Contxt
)
{
PKEVENT Event = (PKEVENT) Contxt;
KeSetEvent( Event, 0, FALSE );
return STATUS_MORE_PROCESSING_REQUIRED;
UNREFERENCED_PARAMETER( DeviceObject );
UNREFERENCED_PARAMETER( Contxt );
UNREFERENCED_PARAMETER( Irp );
}
|