3 Copyright (C) Microsoft Corporation, 1991 - 1999
11 SCSI class driver routines
24 #define CLASS_INIT_GUID 0
28 ULONG BreakOnClose
= 0;
30 PUCHAR LockTypeStrings
[] = {
37 PFILE_OBJECT_EXTENSION
39 IN PCOMMON_DEVICE_EXTENSION CommonExtension
,
40 IN PFILE_OBJECT FileObject
44 ClasspCleanupDisableMcn(
45 IN PFILE_OBJECT_EXTENSION FsContext
49 #pragma alloc_text(PAGE, ClassCreateClose)
50 #pragma alloc_text(PAGE, ClasspCreateClose)
51 #pragma alloc_text(PAGE, ClasspCleanupProtectedLocks)
52 #pragma alloc_text(PAGE, ClasspEjectionControl)
53 #pragma alloc_text(PAGE, ClasspCleanupDisableMcn)
54 #pragma alloc_text(PAGE, ClasspGetFsContext)
59 IN PDEVICE_OBJECT DeviceObject
,
67 SCSI class driver create and close routine. This is called by the I/O system
68 when the device is opened or closed.
72 DriverObject - Pointer to driver object created by system.
78 Device-specific drivers return value or STATUS_SUCCESS.
83 PCOMMON_DEVICE_EXTENSION commonExtension
= DeviceObject
->DeviceExtension
;
90 // If we're getting a close request then we know the device object hasn't
91 // been completely destroyed. Let the driver cleanup if necessary.
94 removeState
= ClassAcquireRemoveLock(DeviceObject
, Irp
);
97 // Invoke the device-specific routine, if one exists. Otherwise complete
101 if((removeState
== NO_REMOVE
) ||
102 IS_CLEANUP_REQUEST(IoGetCurrentIrpStackLocation(Irp
)->MajorFunction
)) {
104 status
= ClasspCreateClose(DeviceObject
, Irp
);
106 if((NT_SUCCESS(status
)) &&
107 (commonExtension
->DevInfo
->ClassCreateClose
)) {
109 return commonExtension
->DevInfo
->ClassCreateClose(DeviceObject
, Irp
);
113 status
= STATUS_DEVICE_DOES_NOT_EXIST
;
116 Irp
->IoStatus
.Status
= status
;
117 ClassReleaseRemoveLock(DeviceObject
, Irp
);
118 ClassCompleteRequest(DeviceObject
, Irp
, IO_NO_INCREMENT
);
125 IN PDEVICE_OBJECT DeviceObject
,
132 This routine will handle create/close operations for a given classpnp
133 device if the class driver doesn't supply it's own handler. If there
134 is a file object supplied for our driver (if it's a FO_DIRECT_DEVICE_OPEN
135 file object) then it will initialize a file extension on create or destroy
136 the extension on a close.
140 DeviceObject - the device object being opened or closed.
142 Irp - the create/close irp
150 PCOMMON_DEVICE_EXTENSION commonExtension
= DeviceObject
->DeviceExtension
;
151 PIO_STACK_LOCATION irpStack
= IoGetCurrentIrpStackLocation(Irp
);
153 PFILE_OBJECT fileObject
= irpStack
->FileObject
;
155 NTSTATUS status
= STATUS_SUCCESS
;
161 // ISSUE-2000/3/28-henrygab - if lower stack fails create/close, we end up
162 // in an inconsistent state. re-write to verify all args and allocate all
163 // required resources, then pass the irp down, then complete the
164 // transaction. this is because we also cannot forward the irp, then fail
165 // it after it has succeeded a lower-level driver.
168 if(irpStack
->MajorFunction
== IRP_MJ_CREATE
) {
170 PIO_SECURITY_CONTEXT securityContext
=
171 irpStack
->Parameters
.Create
.SecurityContext
;
173 "ClasspCREATEClose: create received for device %p\n",
176 "ClasspCREATEClose: desired access %lx\n",
177 securityContext
->DesiredAccess
));
179 "ClasspCREATEClose: file object %lx\n",
180 irpStack
->FileObject
));
182 ASSERT(BreakOnClose
== FALSE
);
184 if(irpStack
->FileObject
!= NULL
) {
186 PFILE_OBJECT_EXTENSION fsContext
;
189 // Allocate our own file object extension for this device object.
192 status
= AllocateDictionaryEntry(
193 &commonExtension
->FileObjectDictionary
,
194 (ULONGLONG
) irpStack
->FileObject
,
195 sizeof(FILE_OBJECT_EXTENSION
),
196 CLASS_TAG_FILE_OBJECT_EXTENSION
,
199 if(NT_SUCCESS(status
)) {
201 RtlZeroMemory(fsContext
,
202 sizeof(FILE_OBJECT_EXTENSION
));
204 fsContext
->FileObject
= irpStack
->FileObject
;
205 fsContext
->DeviceObject
= DeviceObject
;
206 } else if (status
== STATUS_OBJECT_NAME_COLLISION
) {
207 status
= STATUS_SUCCESS
;
214 "ClasspCreateCLOSE: close received for device %p\n",
217 "ClasspCreateCLOSE: file object %p\n",
220 if(irpStack
->FileObject
!= NULL
) {
222 PFILE_OBJECT_EXTENSION fsContext
=
223 ClasspGetFsContext(commonExtension
, irpStack
->FileObject
);
226 "ClasspCreateCLOSE: file extension %p\n",
229 if(fsContext
!= NULL
) {
232 "ClasspCreateCLOSE: extension is ours - "
234 ASSERT(BreakOnClose
== FALSE
);
236 ClasspCleanupProtectedLocks(fsContext
);
238 ClasspCleanupDisableMcn(fsContext
);
240 FreeDictionaryEntry(&(commonExtension
->FileObjectDictionary
),
247 // Notify the lower levels about the create or close operation - give them
248 // a chance to cleanup too.
252 "ClasspCreateClose: %s for devobj %p\n",
253 (NT_SUCCESS(status
) ? "Success" : "FAILED"),
257 if(NT_SUCCESS(status
)) {
262 // Set up the event to wait on
265 KeInitializeEvent(&event
, SynchronizationEvent
, FALSE
);
267 IoCopyCurrentIrpStackLocationToNext(Irp
);
268 IoSetCompletionRoutine( Irp
, ClassSignalCompletion
, &event
,
271 status
= IoCallDriver(commonExtension
->LowerDeviceObject
, Irp
);
273 if(status
== STATUS_PENDING
) {
274 KeWaitForSingleObject(&event
,
279 status
= Irp
->IoStatus
.Status
;
282 if (!NT_SUCCESS(status
)) {
283 DebugPrint((ClassDebugError
,
284 "ClasspCreateClose: Lower driver failed, but we "
285 "succeeded. This is a problem, lock counts will be "
286 "out of sync between levels.\n"));
297 ClasspCleanupProtectedLocks(
298 IN PFILE_OBJECT_EXTENSION FsContext
301 PCOMMON_DEVICE_EXTENSION commonExtension
=
302 FsContext
->DeviceObject
->DeviceExtension
;
304 PFUNCTIONAL_DEVICE_EXTENSION fdoExtension
=
305 commonExtension
->PartitionZeroExtension
;
307 ULONG newDeviceLockCount
= 1;
312 "ClasspCleanupProtectedLocks called for %p\n",
313 FsContext
->DeviceObject
));
315 "ClasspCleanupProtectedLocks - FsContext %p is locked "
316 "%d times\n", FsContext
, FsContext
->LockCount
));
318 ASSERT(BreakOnClose
== FALSE
);
321 // Synchronize with ejection and ejection control requests.
324 KeEnterCriticalRegion();
325 KeWaitForSingleObject(&(fdoExtension
->EjectSynchronizationEvent
),
332 // For each secure lock on this handle decrement the secured lock count
333 // for the FDO. Keep track of the new value.
336 if(FsContext
->LockCount
!= 0) {
340 InterlockedDecrement(&FsContext
->LockCount
);
343 InterlockedDecrement(&fdoExtension
->ProtectedLockCount
);
345 } while(FsContext
->LockCount
!= 0);
348 // If the new lock count has been dropped to zero then issue a lock
349 // command to the device.
353 "ClasspCleanupProtectedLocks: FDO secured lock count = %d "
355 fdoExtension
->ProtectedLockCount
,
356 fdoExtension
->LockCount
));
358 if((newDeviceLockCount
== 0) && (fdoExtension
->LockCount
== 0)) {
360 SCSI_REQUEST_BLOCK srb
;
365 "ClasspCleanupProtectedLocks: FDO lock count dropped "
368 RtlZeroMemory(&srb
, sizeof(SCSI_REQUEST_BLOCK
));
369 cdb
= (PCDB
) &(srb
.Cdb
);
373 cdb
->MEDIA_REMOVAL
.OperationCode
= SCSIOP_MEDIUM_REMOVAL
;
376 // TRUE - prevent media removal.
377 // FALSE - allow media removal.
380 cdb
->MEDIA_REMOVAL
.Prevent
= FALSE
;
383 // Set timeout value.
386 srb
.TimeOutValue
= fdoExtension
->TimeOutValue
;
387 status
= ClassSendSrbSynchronous(fdoExtension
->DeviceObject
,
394 "ClasspCleanupProtectedLocks: unlock request to drive "
395 "returned status %lx\n", status
));
399 KeSetEvent(&fdoExtension
->EjectSynchronizationEvent
,
402 KeLeaveCriticalRegion();
408 ClasspCleanupDisableMcn(
409 IN PFILE_OBJECT_EXTENSION FsContext
412 PCOMMON_DEVICE_EXTENSION commonExtension
=
413 FsContext
->DeviceObject
->DeviceExtension
;
415 PFUNCTIONAL_DEVICE_EXTENSION fdoExtension
=
416 commonExtension
->PartitionZeroExtension
;
422 DebugPrint((ClassDebugTrace
,
423 "ClasspCleanupDisableMcn called for %p\n",
424 FsContext
->DeviceObject
));
425 DebugPrint((ClassDebugTrace
,
426 "ClasspCleanupDisableMcn - FsContext %p is disabled "
427 "%d times\n", FsContext
, FsContext
->McnDisableCount
));
430 // For each secure lock on this handle decrement the secured lock count
431 // for the FDO. Keep track of the new value.
434 while(FsContext
->McnDisableCount
!= 0) {
435 FsContext
->McnDisableCount
--;
436 ClassEnableMediaChangeDetection(fdoExtension
);
445 * BUGBUG REMOVE this old function implementation as soon as the
446 * boottime pagefile problems with the new one (below)
450 ClasspEjectionControl(
451 IN PDEVICE_OBJECT Fdo
,
453 IN MEDIA_LOCK_TYPE LockType
,
457 PFUNCTIONAL_DEVICE_EXTENSION FdoExtension
= Fdo
->DeviceExtension
;
458 PCOMMON_DEVICE_EXTENSION commonExtension
=
459 (PCOMMON_DEVICE_EXTENSION
) FdoExtension
;
461 PFILE_OBJECT_EXTENSION fsContext
= NULL
;
463 PSCSI_REQUEST_BLOCK srb
= NULL
;
464 BOOLEAN countChanged
= FALSE
;
469 // Interlock with ejection and secure lock cleanup code. This is a
470 // user request so we can allow the stack to get swapped out while we
471 // wait for synchronization.
474 status
= KeWaitForSingleObject(
475 &(FdoExtension
->EjectSynchronizationEvent
),
481 ASSERT(status
== STATUS_SUCCESS
);
484 "ClasspEjectionControl: "
485 "Received request for %s lock type\n",
486 LockTypeStrings
[LockType
]
492 srb
= ClasspAllocateSrb(FdoExtension
);
495 status
= STATUS_INSUFFICIENT_RESOURCES
;
499 RtlZeroMemory(srb
, sizeof(SCSI_REQUEST_BLOCK
));
501 cdb
= (PCDB
) srb
->Cdb
;
504 // Determine if this is a "secured" request.
507 if(LockType
== SecureMediaLock
) {
509 PIO_STACK_LOCATION irpStack
= IoGetCurrentIrpStackLocation(Irp
);
510 PFILE_OBJECT fileObject
= irpStack
->FileObject
;
513 // Make sure that the file object we are supplied has a
514 // proper FsContext before we try doing a secured lock.
517 if(fileObject
!= NULL
) {
518 fsContext
= ClasspGetFsContext(commonExtension
, fileObject
);
521 if (fsContext
== NULL
) {
524 // This handle isn't setup correctly. We can't let the
528 status
= STATUS_INVALID_PARAMETER
;
536 // This is a lock command. Reissue the command in case bus or
537 // device was reset and the lock was cleared.
538 // note: may need to decrement count if actual lock operation
544 case SimpleMediaLock
: {
545 FdoExtension
->LockCount
++;
550 case SecureMediaLock
: {
551 fsContext
->LockCount
++;
552 FdoExtension
->ProtectedLockCount
++;
557 case InternalMediaLock
: {
558 FdoExtension
->InternalLockCount
++;
567 // This is an unlock command. If it's a secured one then make sure
568 // the caller has a lock outstanding or return an error.
569 // note: may need to re-increment the count if actual unlock
570 // operation fails....
575 case SimpleMediaLock
: {
576 if(FdoExtension
->LockCount
!= 0) {
577 FdoExtension
->LockCount
--;
583 case SecureMediaLock
: {
584 if(fsContext
->LockCount
== 0) {
585 status
= STATUS_INVALID_DEVICE_STATE
;
588 fsContext
->LockCount
--;
589 FdoExtension
->ProtectedLockCount
--;
594 case InternalMediaLock
: {
595 ASSERT(FdoExtension
->InternalLockCount
!= 0);
596 FdoExtension
->InternalLockCount
--;
603 // We only send an unlock command to the drive if both the
604 // secured and unsecured lock counts have dropped to zero.
607 if((FdoExtension
->ProtectedLockCount
!= 0) ||
608 (FdoExtension
->InternalLockCount
!= 0) ||
609 (FdoExtension
->LockCount
!= 0)) {
611 status
= STATUS_SUCCESS
;
616 status
= STATUS_SUCCESS
;
617 if (TEST_FLAG(Fdo
->Characteristics
, FILE_REMOVABLE_MEDIA
)) {
620 cdb
->MEDIA_REMOVAL
.OperationCode
= SCSIOP_MEDIUM_REMOVAL
;
623 // TRUE - prevent media removal.
624 // FALSE - allow media removal.
627 cdb
->MEDIA_REMOVAL
.Prevent
= Lock
;
630 // Set timeout value.
633 srb
->TimeOutValue
= FdoExtension
->TimeOutValue
;
636 // The actual lock operation on the device isn't so important
637 // as the internal lock counts. Ignore failures.
640 status
= ClassSendSrbSynchronous(FdoExtension
->DeviceObject
,
649 if (!NT_SUCCESS(status
)) {
651 "ClasspEjectionControl: FAILED status %x -- "
652 "reverting lock counts\n", status
));
657 // have to revert to previous counts if the
658 // lock/unlock operation actually failed.
665 case SimpleMediaLock
: {
666 FdoExtension
->LockCount
--;
670 case SecureMediaLock
: {
671 fsContext
->LockCount
--;
672 FdoExtension
->ProtectedLockCount
--;
676 case InternalMediaLock
: {
677 FdoExtension
->InternalLockCount
--;
686 case SimpleMediaLock
: {
687 FdoExtension
->LockCount
++;
691 case SecureMediaLock
: {
692 fsContext
->LockCount
++;
693 FdoExtension
->ProtectedLockCount
++;
697 case InternalMediaLock
: {
698 FdoExtension
->InternalLockCount
++;
709 "ClasspEjectionControl: Succeeded\n"));
714 "ClasspEjectionControl: "
715 "Current Counts: Internal: %x Secure: %x Simple: %x\n",
716 FdoExtension
->InternalLockCount
,
717 FdoExtension
->ProtectedLockCount
,
718 FdoExtension
->LockCount
721 KeSetEvent(&(FdoExtension
->EjectSynchronizationEvent
),
725 ClassFreeOrReuseSrb(FdoExtension
, srb
);
736 * This is a new implementation of the function that doesn't thrash memory
737 * or depend on the srbLookasideList.
738 * HOWEVER, it seems to cause pagefile initialization to fail during boot
739 * for some reason. Need to resolve this before switching to this function.
742 ClasspEjectionControl(
743 IN PDEVICE_OBJECT Fdo
,
745 IN MEDIA_LOCK_TYPE LockType
,
749 PFUNCTIONAL_DEVICE_EXTENSION fdoExt
= Fdo
->DeviceExtension
;
750 PFILE_OBJECT_EXTENSION fsContext
;
751 BOOLEAN fileHandleOk
= TRUE
;
752 BOOLEAN countChanged
= FALSE
;
757 status
= KeWaitForSingleObject(
758 &fdoExt
->EjectSynchronizationEvent
,
763 ASSERT(status
== STATUS_SUCCESS
);
766 * If this is a "secured" request, we have to make sure
767 * that the file handle is valid.
769 if (LockType
== SecureMediaLock
){
770 PIO_STACK_LOCATION thisSp
= IoGetCurrentIrpStackLocation(Irp
);
773 * Make sure that the file object we are supplied has a
774 * proper FsContext before we try doing a secured lock.
776 if (thisSp
->FileObject
){
777 PCOMMON_DEVICE_EXTENSION commonExt
= (PCOMMON_DEVICE_EXTENSION
)fdoExt
;
778 fsContext
= ClasspGetFsContext(commonExt
, thisSp
->FileObject
);
786 fileHandleOk
= FALSE
;
793 * Adjust the lock counts and make sure they make sense.
795 status
= STATUS_SUCCESS
;
798 case SimpleMediaLock
:
802 case SecureMediaLock
:
803 fsContext
->LockCount
++;
804 fdoExt
->ProtectedLockCount
++;
807 case InternalMediaLock
:
808 fdoExt
->InternalLockCount
++;
815 * This is an unlock command. If it's a secured one then make sure
816 * the caller has a lock outstanding or return an error.
819 case SimpleMediaLock
:
820 if (fdoExt
->LockCount
> 0){
825 ASSERT(fdoExt
->LockCount
> 0);
826 status
= STATUS_INTERNAL_ERROR
;
829 case SecureMediaLock
:
830 if (fsContext
->LockCount
> 0){
831 ASSERT(fdoExt
->ProtectedLockCount
> 0);
832 fsContext
->LockCount
--;
833 fdoExt
->ProtectedLockCount
--;
837 ASSERT(fsContext
->LockCount
> 0);
838 status
= STATUS_INVALID_DEVICE_STATE
;
841 case InternalMediaLock
:
842 ASSERT(fdoExt
->InternalLockCount
> 0);
843 fdoExt
->InternalLockCount
--;
849 if (NT_SUCCESS(status
)){
851 * We only send an unlock command to the drive if
852 * all the lock counts have dropped to zero.
855 (fdoExt
->ProtectedLockCount
||
856 fdoExt
->InternalLockCount
||
860 * The lock count is still positive, so don't unlock yet.
862 status
= STATUS_SUCCESS
;
864 else if (!TEST_FLAG(Fdo
->Characteristics
, FILE_REMOVABLE_MEDIA
)) {
866 * The device isn't removable media. don't send a cmd.
868 status
= STATUS_SUCCESS
;
871 TRANSFER_PACKET
*pkt
;
873 pkt
= DequeueFreeTransferPacket(Fdo
, TRUE
);
878 * Store the number of packets servicing the irp (one)
879 * inside the original IRP. It will be used to counted down
880 * to zero when the packet completes.
881 * Initialize the original IRP's status to success.
882 * If the packet fails, we will set it to the error status.
884 Irp
->Tail
.Overlay
.DriverContext
[0] = LongToPtr(1);
885 Irp
->IoStatus
.Status
= STATUS_SUCCESS
;
888 * Set this up as a SYNCHRONOUS transfer, submit it,
889 * and wait for the packet to complete. The result
890 * status will be written to the original irp.
892 KeInitializeEvent(&event
, SynchronizationEvent
, FALSE
);
893 SetupEjectionTransferPacket(pkt
, Lock
, &event
, Irp
);
894 SubmitTransferPacket(pkt
);
895 KeWaitForSingleObject(&event
, Executive
, KernelMode
, FALSE
, NULL
);
896 status
= Irp
->IoStatus
.Status
;
899 status
= STATUS_INSUFFICIENT_RESOURCES
;
905 status
= STATUS_INVALID_PARAMETER
;
908 if (!NT_SUCCESS(status
) && countChanged
) {
911 // have to revert to previous counts if the
912 // lock/unlock operation actually failed.
919 case SimpleMediaLock
: {
920 FdoExtension
->LockCount
--;
924 case SecureMediaLock
: {
925 fsContext
->LockCount
--;
926 FdoExtension
->ProtectedLockCount
--;
930 case InternalMediaLock
: {
931 FdoExtension
->InternalLockCount
--;
940 case SimpleMediaLock
: {
941 FdoExtension
->LockCount
++;
945 case SecureMediaLock
: {
946 fsContext
->LockCount
++;
947 FdoExtension
->ProtectedLockCount
++;
951 case InternalMediaLock
: {
952 FdoExtension
->InternalLockCount
++;
961 KeSetEvent(&fdoExt
->EjectSynchronizationEvent
, IO_NO_INCREMENT
, FALSE
);
967 PFILE_OBJECT_EXTENSION
969 IN PCOMMON_DEVICE_EXTENSION CommonExtension
,
970 IN PFILE_OBJECT FileObject
974 return GetDictionaryEntry(&(CommonExtension
->FileObjectDictionary
),
975 (ULONGLONG
) FileObject
);