Sync with trunk r58740.
[reactos.git] / drivers / storage / classpnp / create.c
1 /*++
2
3 Copyright (C) Microsoft Corporation, 1991 - 1999
4
5 Module Name:
6
7 class.c
8
9 Abstract:
10
11 SCSI class driver routines
12
13 Environment:
14
15 kernel mode only
16
17 Notes:
18
19
20 Revision History:
21
22 --*/
23
24 #define CLASS_INIT_GUID 0
25 #include "classp.h"
26 #include "debug.h"
27
28 ULONG BreakOnClose = 0;
29
30 PUCHAR LockTypeStrings[] = {
31 "Simple",
32 "Secure",
33 "Internal"
34 };
35
36
37 PFILE_OBJECT_EXTENSION
38 NTAPI
39 ClasspGetFsContext(
40 IN PCOMMON_DEVICE_EXTENSION CommonExtension,
41 IN PFILE_OBJECT FileObject
42 );
43
44 VOID
45 NTAPI
46 ClasspCleanupDisableMcn(
47 IN PFILE_OBJECT_EXTENSION FsContext
48 );
49
50 #ifdef ALLOC_PRAGMA
51 #pragma alloc_text(PAGE, ClassCreateClose)
52 #pragma alloc_text(PAGE, ClasspCreateClose)
53 #pragma alloc_text(PAGE, ClasspCleanupProtectedLocks)
54 #pragma alloc_text(PAGE, ClasspEjectionControl)
55 #pragma alloc_text(PAGE, ClasspCleanupDisableMcn)
56 #pragma alloc_text(PAGE, ClasspGetFsContext)
57 #endif
58
59 NTSTATUS
60 NTAPI
61 ClassCreateClose(
62 IN PDEVICE_OBJECT DeviceObject,
63 IN PIRP Irp
64 )
65
66 /*++
67
68 Routine Description:
69
70 SCSI class driver create and close routine. This is called by the I/O system
71 when the device is opened or closed.
72
73 Arguments:
74
75 DriverObject - Pointer to driver object created by system.
76
77 Irp - IRP involved.
78
79 Return Value:
80
81 Device-specific drivers return value or STATUS_SUCCESS.
82
83 --*/
84
85 {
86 PCOMMON_DEVICE_EXTENSION commonExtension = DeviceObject->DeviceExtension;
87 ULONG removeState;
88 NTSTATUS status;
89
90 PAGED_CODE();
91
92 //
93 // If we're getting a close request then we know the device object hasn't
94 // been completely destroyed. Let the driver cleanup if necessary.
95 //
96
97 removeState = ClassAcquireRemoveLock(DeviceObject, Irp);
98
99 //
100 // Invoke the device-specific routine, if one exists. Otherwise complete
101 // with SUCCESS
102 //
103
104 if((removeState == NO_REMOVE) ||
105 IS_CLEANUP_REQUEST(IoGetCurrentIrpStackLocation(Irp)->MajorFunction)) {
106
107 status = ClasspCreateClose(DeviceObject, Irp);
108
109 if((NT_SUCCESS(status)) &&
110 (commonExtension->DevInfo->ClassCreateClose)) {
111
112 return commonExtension->DevInfo->ClassCreateClose(DeviceObject, Irp);
113 }
114
115 } else {
116 status = STATUS_DEVICE_DOES_NOT_EXIST;
117 }
118
119 Irp->IoStatus.Status = status;
120 ClassReleaseRemoveLock(DeviceObject, Irp);
121 ClassCompleteRequest(DeviceObject, Irp, IO_NO_INCREMENT);
122 return status;
123 }
124
125 NTSTATUS
126 NTAPI
127 ClasspCreateClose(
128 IN PDEVICE_OBJECT DeviceObject,
129 IN PIRP Irp
130 )
131 /*++
132
133 Routine Description:
134
135 This routine will handle create/close operations for a given classpnp
136 device if the class driver doesn't supply it's own handler. If there
137 is a file object supplied for our driver (if it's a FO_DIRECT_DEVICE_OPEN
138 file object) then it will initialize a file extension on create or destroy
139 the extension on a close.
140
141 Arguments:
142
143 DeviceObject - the device object being opened or closed.
144
145 Irp - the create/close irp
146
147 Return Value:
148
149 status
150
151 --*/
152 {
153 PCOMMON_DEVICE_EXTENSION commonExtension = DeviceObject->DeviceExtension;
154 PIO_STACK_LOCATION irpStack = IoGetCurrentIrpStackLocation(Irp);
155
156 PFILE_OBJECT fileObject = irpStack->FileObject;
157
158 NTSTATUS status = STATUS_SUCCESS;
159
160 PAGED_CODE();
161
162
163 //
164 // ISSUE-2000/3/28-henrygab - if lower stack fails create/close, we end up
165 // in an inconsistent state. re-write to verify all args and allocate all
166 // required resources, then pass the irp down, then complete the
167 // transaction. this is because we also cannot forward the irp, then fail
168 // it after it has succeeded a lower-level driver.
169 //
170
171 if(irpStack->MajorFunction == IRP_MJ_CREATE) {
172
173 PIO_SECURITY_CONTEXT securityContext =
174 irpStack->Parameters.Create.SecurityContext;
175 DebugPrint((2,
176 "ClasspCREATEClose: create received for device %p\n",
177 DeviceObject));
178 DebugPrint((2,
179 "ClasspCREATEClose: desired access %lx\n",
180 securityContext->DesiredAccess));
181 DebugPrint((2,
182 "ClasspCREATEClose: file object %lx\n",
183 irpStack->FileObject));
184
185 ASSERT(BreakOnClose == FALSE);
186
187 if(irpStack->FileObject != NULL) {
188
189 PFILE_OBJECT_EXTENSION fsContext;
190
191 //
192 // Allocate our own file object extension for this device object.
193 //
194
195 status = AllocateDictionaryEntry(
196 &commonExtension->FileObjectDictionary,
197 (ULONGLONG) irpStack->FileObject,
198 sizeof(FILE_OBJECT_EXTENSION),
199 CLASS_TAG_FILE_OBJECT_EXTENSION,
200 &fsContext);
201
202 if(NT_SUCCESS(status)) {
203
204 RtlZeroMemory(fsContext,
205 sizeof(FILE_OBJECT_EXTENSION));
206
207 fsContext->FileObject = irpStack->FileObject;
208 fsContext->DeviceObject = DeviceObject;
209 } else if (status == STATUS_OBJECT_NAME_COLLISION) {
210 status = STATUS_SUCCESS;
211 }
212 }
213
214 } else {
215
216 DebugPrint((2,
217 "ClasspCreateCLOSE: close received for device %p\n",
218 DeviceObject));
219 DebugPrint((2,
220 "ClasspCreateCLOSE: file object %p\n",
221 fileObject));
222
223 if(irpStack->FileObject != NULL) {
224
225 PFILE_OBJECT_EXTENSION fsContext =
226 ClasspGetFsContext(commonExtension, irpStack->FileObject);
227
228 DebugPrint((2,
229 "ClasspCreateCLOSE: file extension %p\n",
230 fsContext));
231
232 if(fsContext != NULL) {
233
234 DebugPrint((2,
235 "ClasspCreateCLOSE: extension is ours - "
236 "freeing\n"));
237 ASSERT(BreakOnClose == FALSE);
238
239 ClasspCleanupProtectedLocks(fsContext);
240
241 ClasspCleanupDisableMcn(fsContext);
242
243 FreeDictionaryEntry(&(commonExtension->FileObjectDictionary),
244 fsContext);
245 }
246 }
247 }
248
249 //
250 // Notify the lower levels about the create or close operation - give them
251 // a chance to cleanup too.
252 //
253
254 DebugPrint((2,
255 "ClasspCreateClose: %s for devobj %p\n",
256 (NT_SUCCESS(status) ? "Success" : "FAILED"),
257 DeviceObject));
258
259
260 if(NT_SUCCESS(status)) {
261
262 KEVENT event;
263
264 //
265 // Set up the event to wait on
266 //
267
268 KeInitializeEvent(&event, SynchronizationEvent, FALSE);
269
270 IoCopyCurrentIrpStackLocationToNext(Irp);
271 IoSetCompletionRoutine( Irp, ClassSignalCompletion, &event,
272 TRUE, TRUE, TRUE);
273
274 status = IoCallDriver(commonExtension->LowerDeviceObject, Irp);
275
276 if(status == STATUS_PENDING) {
277 KeWaitForSingleObject(&event,
278 Executive,
279 KernelMode,
280 FALSE,
281 NULL);
282 status = Irp->IoStatus.Status;
283 }
284
285 if (!NT_SUCCESS(status)) {
286 DebugPrint((ClassDebugError,
287 "ClasspCreateClose: Lower driver failed, but we "
288 "succeeded. This is a problem, lock counts will be "
289 "out of sync between levels.\n"));
290 }
291
292 }
293
294
295 return status;
296 }
297
298 VOID
299 NTAPI
300 ClasspCleanupProtectedLocks(
301 IN PFILE_OBJECT_EXTENSION FsContext
302 )
303 {
304 PCOMMON_DEVICE_EXTENSION commonExtension =
305 FsContext->DeviceObject->DeviceExtension;
306
307 PFUNCTIONAL_DEVICE_EXTENSION fdoExtension =
308 commonExtension->PartitionZeroExtension;
309
310 ULONG newDeviceLockCount = 1;
311
312 PAGED_CODE();
313
314 DebugPrint((2,
315 "ClasspCleanupProtectedLocks called for %p\n",
316 FsContext->DeviceObject));
317 DebugPrint((2,
318 "ClasspCleanupProtectedLocks - FsContext %p is locked "
319 "%d times\n", FsContext, FsContext->LockCount));
320
321 ASSERT(BreakOnClose == FALSE);
322
323 //
324 // Synchronize with ejection and ejection control requests.
325 //
326
327 KeEnterCriticalRegion();
328 KeWaitForSingleObject(&(fdoExtension->EjectSynchronizationEvent),
329 UserRequest,
330 UserMode,
331 FALSE,
332 NULL);
333
334 //
335 // For each secure lock on this handle decrement the secured lock count
336 // for the FDO. Keep track of the new value.
337 //
338
339 if(FsContext->LockCount != 0) {
340
341 do {
342
343 InterlockedDecrement(&FsContext->LockCount);
344
345 newDeviceLockCount =
346 InterlockedDecrement(&fdoExtension->ProtectedLockCount);
347
348 } while(FsContext->LockCount != 0);
349
350 //
351 // If the new lock count has been dropped to zero then issue a lock
352 // command to the device.
353 //
354
355 DebugPrint((2,
356 "ClasspCleanupProtectedLocks: FDO secured lock count = %d "
357 "lock count = %d\n",
358 fdoExtension->ProtectedLockCount,
359 fdoExtension->LockCount));
360
361 if((newDeviceLockCount == 0) && (fdoExtension->LockCount == 0)) {
362
363 SCSI_REQUEST_BLOCK srb;
364 PCDB cdb;
365 NTSTATUS status;
366
367 DebugPrint((2,
368 "ClasspCleanupProtectedLocks: FDO lock count dropped "
369 "to zero\n"));
370
371 RtlZeroMemory(&srb, sizeof(SCSI_REQUEST_BLOCK));
372 cdb = (PCDB) &(srb.Cdb);
373
374 srb.CdbLength = 6;
375
376 cdb->MEDIA_REMOVAL.OperationCode = SCSIOP_MEDIUM_REMOVAL;
377
378 //
379 // TRUE - prevent media removal.
380 // FALSE - allow media removal.
381 //
382
383 cdb->MEDIA_REMOVAL.Prevent = FALSE;
384
385 //
386 // Set timeout value.
387 //
388
389 srb.TimeOutValue = fdoExtension->TimeOutValue;
390 status = ClassSendSrbSynchronous(fdoExtension->DeviceObject,
391 &srb,
392 NULL,
393 0,
394 FALSE);
395
396 DebugPrint((2,
397 "ClasspCleanupProtectedLocks: unlock request to drive "
398 "returned status %lx\n", status));
399 }
400 }
401
402 KeSetEvent(&fdoExtension->EjectSynchronizationEvent,
403 IO_NO_INCREMENT,
404 FALSE);
405 KeLeaveCriticalRegion();
406 return;
407 }
408
409 VOID
410 NTAPI
411 ClasspCleanupDisableMcn(
412 IN PFILE_OBJECT_EXTENSION FsContext
413 )
414 {
415 PCOMMON_DEVICE_EXTENSION commonExtension =
416 FsContext->DeviceObject->DeviceExtension;
417
418 PFUNCTIONAL_DEVICE_EXTENSION fdoExtension =
419 commonExtension->PartitionZeroExtension;
420
421 ULONG newCount = 1;
422
423 PAGED_CODE();
424
425 DebugPrint((ClassDebugTrace,
426 "ClasspCleanupDisableMcn called for %p\n",
427 FsContext->DeviceObject));
428 DebugPrint((ClassDebugTrace,
429 "ClasspCleanupDisableMcn - FsContext %p is disabled "
430 "%d times\n", FsContext, FsContext->McnDisableCount));
431
432 //
433 // For each secure lock on this handle decrement the secured lock count
434 // for the FDO. Keep track of the new value.
435 //
436
437 while(FsContext->McnDisableCount != 0) {
438 FsContext->McnDisableCount--;
439 ClassEnableMediaChangeDetection(fdoExtension);
440 }
441
442 return;
443 }
444
445 #if 1
446 /*
447 * BUGBUG REMOVE this old function implementation as soon as the
448 * boottime pagefile problems with the new one (below)
449 * are resolved.
450 */
451 NTSTATUS
452 NTAPI
453 ClasspEjectionControl(
454 IN PDEVICE_OBJECT Fdo,
455 IN PIRP Irp,
456 IN MEDIA_LOCK_TYPE LockType,
457 IN BOOLEAN Lock
458 )
459 {
460 PFUNCTIONAL_DEVICE_EXTENSION FdoExtension = Fdo->DeviceExtension;
461 PCOMMON_DEVICE_EXTENSION commonExtension =
462 (PCOMMON_DEVICE_EXTENSION) FdoExtension;
463
464 PFILE_OBJECT_EXTENSION fsContext = NULL;
465 NTSTATUS status;
466 volatile PSCSI_REQUEST_BLOCK srb = NULL;
467 BOOLEAN countChanged = FALSE;
468
469 PAGED_CODE();
470
471 //
472 // Interlock with ejection and secure lock cleanup code. This is a
473 // user request so we can allow the stack to get swapped out while we
474 // wait for synchronization.
475 //
476
477 status = KeWaitForSingleObject(
478 &(FdoExtension->EjectSynchronizationEvent),
479 UserRequest,
480 UserMode,
481 FALSE,
482 NULL);
483
484 ASSERT(status == STATUS_SUCCESS);
485
486 DebugPrint((2,
487 "ClasspEjectionControl: "
488 "Received request for %s lock type\n",
489 LockTypeStrings[LockType]
490 ));
491
492 _SEH2_TRY {
493 PCDB cdb;
494
495 srb = ClasspAllocateSrb(FdoExtension);
496
497 if(srb == NULL) {
498 status = STATUS_INSUFFICIENT_RESOURCES;
499 _SEH2_LEAVE;
500 }
501
502 RtlZeroMemory(srb, sizeof(SCSI_REQUEST_BLOCK));
503
504 cdb = (PCDB) srb->Cdb;
505
506 //
507 // Determine if this is a "secured" request.
508 //
509
510 if(LockType == SecureMediaLock) {
511
512 PIO_STACK_LOCATION irpStack = IoGetCurrentIrpStackLocation(Irp);
513 PFILE_OBJECT fileObject = irpStack->FileObject;
514
515 //
516 // Make sure that the file object we are supplied has a
517 // proper FsContext before we try doing a secured lock.
518 //
519
520 if(fileObject != NULL) {
521 fsContext = ClasspGetFsContext(commonExtension, fileObject);
522 }
523
524 if (fsContext == NULL) {
525
526 //
527 // This handle isn't setup correctly. We can't let the
528 // operation go.
529 //
530
531 status = STATUS_INVALID_PARAMETER;
532 _SEH2_LEAVE;
533 }
534 }
535
536 if(Lock) {
537
538 //
539 // This is a lock command. Reissue the command in case bus or
540 // device was reset and the lock was cleared.
541 // note: may need to decrement count if actual lock operation
542 // failed....
543 //
544
545 switch(LockType) {
546
547 case SimpleMediaLock: {
548 FdoExtension->LockCount++;
549 countChanged = TRUE;
550 break;
551 }
552
553 case SecureMediaLock: {
554 fsContext->LockCount++;
555 FdoExtension->ProtectedLockCount++;
556 countChanged = TRUE;
557 break;
558 }
559
560 case InternalMediaLock: {
561 FdoExtension->InternalLockCount++;
562 countChanged = TRUE;
563 break;
564 }
565 }
566
567 } else {
568
569 //
570 // This is an unlock command. If it's a secured one then make sure
571 // the caller has a lock outstanding or return an error.
572 // note: may need to re-increment the count if actual unlock
573 // operation fails....
574 //
575
576 switch(LockType) {
577
578 case SimpleMediaLock: {
579 if(FdoExtension->LockCount != 0) {
580 FdoExtension->LockCount--;
581 countChanged = TRUE;
582 }
583 break;
584 }
585
586 case SecureMediaLock: {
587 if(fsContext->LockCount == 0) {
588 status = STATUS_INVALID_DEVICE_STATE;
589 _SEH2_LEAVE;
590 }
591 fsContext->LockCount--;
592 FdoExtension->ProtectedLockCount--;
593 countChanged = TRUE;
594 break;
595 }
596
597 case InternalMediaLock: {
598 ASSERT(FdoExtension->InternalLockCount != 0);
599 FdoExtension->InternalLockCount--;
600 countChanged = TRUE;
601 break;
602 }
603 }
604
605 //
606 // We only send an unlock command to the drive if both the
607 // secured and unsecured lock counts have dropped to zero.
608 //
609
610 if((FdoExtension->ProtectedLockCount != 0) ||
611 (FdoExtension->InternalLockCount != 0) ||
612 (FdoExtension->LockCount != 0)) {
613
614 status = STATUS_SUCCESS;
615 _SEH2_LEAVE;
616 }
617 }
618
619 status = STATUS_SUCCESS;
620 if (TEST_FLAG(Fdo->Characteristics, FILE_REMOVABLE_MEDIA)) {
621
622 srb->CdbLength = 6;
623 cdb->MEDIA_REMOVAL.OperationCode = SCSIOP_MEDIUM_REMOVAL;
624
625 //
626 // TRUE - prevent media removal.
627 // FALSE - allow media removal.
628 //
629
630 cdb->MEDIA_REMOVAL.Prevent = Lock;
631
632 //
633 // Set timeout value.
634 //
635
636 srb->TimeOutValue = FdoExtension->TimeOutValue;
637
638 //
639 // The actual lock operation on the device isn't so important
640 // as the internal lock counts. Ignore failures.
641 //
642
643 status = ClassSendSrbSynchronous(FdoExtension->DeviceObject,
644 srb,
645 NULL,
646 0,
647 FALSE);
648 }
649
650 } _SEH2_FINALLY {
651
652 if (!NT_SUCCESS(status)) {
653 DebugPrint((2,
654 "ClasspEjectionControl: FAILED status %x -- "
655 "reverting lock counts\n", status));
656
657 if (countChanged) {
658
659 //
660 // have to revert to previous counts if the
661 // lock/unlock operation actually failed.
662 //
663
664 if(Lock) {
665
666 switch(LockType) {
667
668 case SimpleMediaLock: {
669 FdoExtension->LockCount--;
670 break;
671 }
672
673 case SecureMediaLock: {
674 fsContext->LockCount--;
675 FdoExtension->ProtectedLockCount--;
676 break;
677 }
678
679 case InternalMediaLock: {
680 FdoExtension->InternalLockCount--;
681 break;
682 }
683 }
684
685 } else {
686
687 switch(LockType) {
688
689 case SimpleMediaLock: {
690 FdoExtension->LockCount++;
691 break;
692 }
693
694 case SecureMediaLock: {
695 fsContext->LockCount++;
696 FdoExtension->ProtectedLockCount++;
697 break;
698 }
699
700 case InternalMediaLock: {
701 FdoExtension->InternalLockCount++;
702 break;
703 }
704 }
705 }
706
707 }
708
709 } else {
710
711 DebugPrint((2,
712 "ClasspEjectionControl: Succeeded\n"));
713
714 }
715
716 DebugPrint((2,
717 "ClasspEjectionControl: "
718 "Current Counts: Internal: %x Secure: %x Simple: %x\n",
719 FdoExtension->InternalLockCount,
720 FdoExtension->ProtectedLockCount,
721 FdoExtension->LockCount
722 ));
723
724 KeSetEvent(&(FdoExtension->EjectSynchronizationEvent),
725 IO_NO_INCREMENT,
726 FALSE);
727 if (srb) {
728 ClassFreeOrReuseSrb(FdoExtension, srb);
729 }
730
731 } _SEH2_END;
732 return status;
733 }
734
735 #else
736
737 /*
738 * BUGBUG RESTORE
739 * This is a new implementation of the function that doesn't thrash memory
740 * or depend on the srbLookasideList.
741 * HOWEVER, it seems to cause pagefile initialization to fail during boot
742 * for some reason. Need to resolve this before switching to this function.
743 */
744 NTSTATUS
745 NTAPI
746 ClasspEjectionControl(
747 IN PDEVICE_OBJECT Fdo,
748 IN PIRP Irp,
749 IN MEDIA_LOCK_TYPE LockType,
750 IN BOOLEAN Lock
751 )
752 {
753 PFUNCTIONAL_DEVICE_EXTENSION fdoExt = Fdo->DeviceExtension;
754 PFILE_OBJECT_EXTENSION fsContext;
755 BOOLEAN fileHandleOk = TRUE;
756 BOOLEAN countChanged = FALSE;
757 NTSTATUS status;
758
759 PAGED_CODE();
760
761 status = KeWaitForSingleObject(
762 &fdoExt->EjectSynchronizationEvent,
763 UserRequest,
764 UserMode,
765 FALSE,
766 NULL);
767 ASSERT(status == STATUS_SUCCESS);
768
769 /*
770 * If this is a "secured" request, we have to make sure
771 * that the file handle is valid.
772 */
773 if (LockType == SecureMediaLock){
774 PIO_STACK_LOCATION thisSp = IoGetCurrentIrpStackLocation(Irp);
775
776 /*
777 * Make sure that the file object we are supplied has a
778 * proper FsContext before we try doing a secured lock.
779 */
780 if (thisSp->FileObject){
781 PCOMMON_DEVICE_EXTENSION commonExt = (PCOMMON_DEVICE_EXTENSION)fdoExt;
782 fsContext = ClasspGetFsContext(commonExt, thisSp->FileObject);
783 }
784 else {
785 fsContext = NULL;
786 }
787
788 if (!fsContext){
789 ASSERT(fsContext);
790 fileHandleOk = FALSE;
791 }
792 }
793
794 if (fileHandleOk){
795
796 /*
797 * Adjust the lock counts and make sure they make sense.
798 */
799 status = STATUS_SUCCESS;
800 if (Lock){
801 switch(LockType) {
802 case SimpleMediaLock:
803 fdoExt->LockCount++;
804 countChanged = TRUE;
805 break;
806 case SecureMediaLock:
807 fsContext->LockCount++;
808 fdoExt->ProtectedLockCount++;
809 countChanged = TRUE;
810 break;
811 case InternalMediaLock:
812 fdoExt->InternalLockCount++;
813 countChanged = TRUE;
814 break;
815 }
816 }
817 else {
818 /*
819 * This is an unlock command. If it's a secured one then make sure
820 * the caller has a lock outstanding or return an error.
821 */
822 switch (LockType){
823 case SimpleMediaLock:
824 if (fdoExt->LockCount > 0){
825 fdoExt->LockCount--;
826 countChanged = TRUE;
827 }
828 else {
829 ASSERT(fdoExt->LockCount > 0);
830 status = STATUS_INTERNAL_ERROR;
831 }
832 break;
833 case SecureMediaLock:
834 if (fsContext->LockCount > 0){
835 ASSERT(fdoExt->ProtectedLockCount > 0);
836 fsContext->LockCount--;
837 fdoExt->ProtectedLockCount--;
838 countChanged = TRUE;
839 }
840 else {
841 ASSERT(fsContext->LockCount > 0);
842 status = STATUS_INVALID_DEVICE_STATE;
843 }
844 break;
845 case InternalMediaLock:
846 ASSERT(fdoExt->InternalLockCount > 0);
847 fdoExt->InternalLockCount--;
848 countChanged = TRUE;
849 break;
850 }
851 }
852
853 if (NT_SUCCESS(status)){
854 /*
855 * We only send an unlock command to the drive if
856 * all the lock counts have dropped to zero.
857 */
858 if (!Lock &&
859 (fdoExt->ProtectedLockCount ||
860 fdoExt->InternalLockCount ||
861 fdoExt->LockCount)){
862
863 /*
864 * The lock count is still positive, so don't unlock yet.
865 */
866 status = STATUS_SUCCESS;
867 }
868 else if (!TEST_FLAG(Fdo->Characteristics, FILE_REMOVABLE_MEDIA)) {
869 /*
870 * The device isn't removable media. don't send a cmd.
871 */
872 status = STATUS_SUCCESS;
873 }
874 else {
875 TRANSFER_PACKET *pkt;
876
877 pkt = DequeueFreeTransferPacket(Fdo, TRUE);
878 if (pkt){
879 KEVENT event;
880
881 /*
882 * Store the number of packets servicing the irp (one)
883 * inside the original IRP. It will be used to counted down
884 * to zero when the packet completes.
885 * Initialize the original IRP's status to success.
886 * If the packet fails, we will set it to the error status.
887 */
888 Irp->Tail.Overlay.DriverContext[0] = LongToPtr(1);
889 Irp->IoStatus.Status = STATUS_SUCCESS;
890
891 /*
892 * Set this up as a SYNCHRONOUS transfer, submit it,
893 * and wait for the packet to complete. The result
894 * status will be written to the original irp.
895 */
896 KeInitializeEvent(&event, SynchronizationEvent, FALSE);
897 SetupEjectionTransferPacket(pkt, Lock, &event, Irp);
898 SubmitTransferPacket(pkt);
899 KeWaitForSingleObject(&event, Executive, KernelMode, FALSE, NULL);
900 status = Irp->IoStatus.Status;
901 }
902 else {
903 status = STATUS_INSUFFICIENT_RESOURCES;
904 }
905 }
906 }
907 }
908 else {
909 status = STATUS_INVALID_PARAMETER;
910 }
911
912 if (!NT_SUCCESS(status) && countChanged) {
913
914 //
915 // have to revert to previous counts if the
916 // lock/unlock operation actually failed.
917 //
918
919 if(Lock) {
920
921 switch(LockType) {
922
923 case SimpleMediaLock: {
924 FdoExtension->LockCount--;
925 break;
926 }
927
928 case SecureMediaLock: {
929 fsContext->LockCount--;
930 FdoExtension->ProtectedLockCount--;
931 break;
932 }
933
934 case InternalMediaLock: {
935 FdoExtension->InternalLockCount--;
936 break;
937 }
938 }
939
940 } else {
941
942 switch(LockType) {
943
944 case SimpleMediaLock: {
945 FdoExtension->LockCount++;
946 break;
947 }
948
949 case SecureMediaLock: {
950 fsContext->LockCount++;
951 FdoExtension->ProtectedLockCount++;
952 break;
953 }
954
955 case InternalMediaLock: {
956 FdoExtension->InternalLockCount++;
957 break;
958 }
959 }
960 }
961 }
962
963
964
965 KeSetEvent(&fdoExt->EjectSynchronizationEvent, IO_NO_INCREMENT, FALSE);
966
967 return status;
968 }
969 #endif
970
971 PFILE_OBJECT_EXTENSION
972 NTAPI
973 ClasspGetFsContext(
974 IN PCOMMON_DEVICE_EXTENSION CommonExtension,
975 IN PFILE_OBJECT FileObject
976 )
977 {
978 PAGED_CODE();
979 return GetDictionaryEntry(&(CommonExtension->FileObjectDictionary),
980 (ULONGLONG) FileObject);
981 }