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
40 IN PCOMMON_DEVICE_EXTENSION CommonExtension
,
41 IN PFILE_OBJECT FileObject
46 ClasspCleanupDisableMcn(
47 IN PFILE_OBJECT_EXTENSION FsContext
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)
62 IN PDEVICE_OBJECT DeviceObject
,
70 SCSI class driver create and close routine. This is called by the I/O system
71 when the device is opened or closed.
75 DriverObject - Pointer to driver object created by system.
81 Device-specific drivers return value or STATUS_SUCCESS.
86 PCOMMON_DEVICE_EXTENSION commonExtension
= DeviceObject
->DeviceExtension
;
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.
97 removeState
= ClassAcquireRemoveLock(DeviceObject
, Irp
);
100 // Invoke the device-specific routine, if one exists. Otherwise complete
104 if((removeState
== NO_REMOVE
) ||
105 IS_CLEANUP_REQUEST(IoGetCurrentIrpStackLocation(Irp
)->MajorFunction
)) {
107 status
= ClasspCreateClose(DeviceObject
, Irp
);
109 if((NT_SUCCESS(status
)) &&
110 (commonExtension
->DevInfo
->ClassCreateClose
)) {
112 return commonExtension
->DevInfo
->ClassCreateClose(DeviceObject
, Irp
);
116 status
= STATUS_DEVICE_DOES_NOT_EXIST
;
119 Irp
->IoStatus
.Status
= status
;
120 ClassReleaseRemoveLock(DeviceObject
, Irp
);
121 ClassCompleteRequest(DeviceObject
, Irp
, IO_NO_INCREMENT
);
128 IN PDEVICE_OBJECT DeviceObject
,
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.
143 DeviceObject - the device object being opened or closed.
145 Irp - the create/close irp
153 PCOMMON_DEVICE_EXTENSION commonExtension
= DeviceObject
->DeviceExtension
;
154 PIO_STACK_LOCATION irpStack
= IoGetCurrentIrpStackLocation(Irp
);
156 PFILE_OBJECT fileObject
= irpStack
->FileObject
;
158 NTSTATUS status
= STATUS_SUCCESS
;
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.
171 if(irpStack
->MajorFunction
== IRP_MJ_CREATE
) {
173 PIO_SECURITY_CONTEXT securityContext
=
174 irpStack
->Parameters
.Create
.SecurityContext
;
176 "ClasspCREATEClose: create received for device %p\n",
179 "ClasspCREATEClose: desired access %lx\n",
180 securityContext
->DesiredAccess
));
182 "ClasspCREATEClose: file object %lx\n",
183 irpStack
->FileObject
));
185 ASSERT(BreakOnClose
== FALSE
);
187 if(irpStack
->FileObject
!= NULL
) {
189 PFILE_OBJECT_EXTENSION fsContext
;
192 // Allocate our own file object extension for this device object.
195 status
= AllocateDictionaryEntry(
196 &commonExtension
->FileObjectDictionary
,
197 (ULONGLONG
) irpStack
->FileObject
,
198 sizeof(FILE_OBJECT_EXTENSION
),
199 CLASS_TAG_FILE_OBJECT_EXTENSION
,
202 if(NT_SUCCESS(status
)) {
204 RtlZeroMemory(fsContext
,
205 sizeof(FILE_OBJECT_EXTENSION
));
207 fsContext
->FileObject
= irpStack
->FileObject
;
208 fsContext
->DeviceObject
= DeviceObject
;
209 } else if (status
== STATUS_OBJECT_NAME_COLLISION
) {
210 status
= STATUS_SUCCESS
;
217 "ClasspCreateCLOSE: close received for device %p\n",
220 "ClasspCreateCLOSE: file object %p\n",
223 if(irpStack
->FileObject
!= NULL
) {
225 PFILE_OBJECT_EXTENSION fsContext
=
226 ClasspGetFsContext(commonExtension
, irpStack
->FileObject
);
229 "ClasspCreateCLOSE: file extension %p\n",
232 if(fsContext
!= NULL
) {
235 "ClasspCreateCLOSE: extension is ours - "
237 ASSERT(BreakOnClose
== FALSE
);
239 ClasspCleanupProtectedLocks(fsContext
);
241 ClasspCleanupDisableMcn(fsContext
);
243 FreeDictionaryEntry(&(commonExtension
->FileObjectDictionary
),
250 // Notify the lower levels about the create or close operation - give them
251 // a chance to cleanup too.
255 "ClasspCreateClose: %s for devobj %p\n",
256 (NT_SUCCESS(status
) ? "Success" : "FAILED"),
260 if(NT_SUCCESS(status
)) {
265 // Set up the event to wait on
268 KeInitializeEvent(&event
, SynchronizationEvent
, FALSE
);
270 IoCopyCurrentIrpStackLocationToNext(Irp
);
271 IoSetCompletionRoutine( Irp
, ClassSignalCompletion
, &event
,
274 status
= IoCallDriver(commonExtension
->LowerDeviceObject
, Irp
);
276 if(status
== STATUS_PENDING
) {
277 KeWaitForSingleObject(&event
,
282 status
= Irp
->IoStatus
.Status
;
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"));
300 ClasspCleanupProtectedLocks(
301 IN PFILE_OBJECT_EXTENSION FsContext
304 PCOMMON_DEVICE_EXTENSION commonExtension
=
305 FsContext
->DeviceObject
->DeviceExtension
;
307 PFUNCTIONAL_DEVICE_EXTENSION fdoExtension
=
308 commonExtension
->PartitionZeroExtension
;
310 ULONG newDeviceLockCount
= 1;
315 "ClasspCleanupProtectedLocks called for %p\n",
316 FsContext
->DeviceObject
));
318 "ClasspCleanupProtectedLocks - FsContext %p is locked "
319 "%d times\n", FsContext
, FsContext
->LockCount
));
321 ASSERT(BreakOnClose
== FALSE
);
324 // Synchronize with ejection and ejection control requests.
327 KeEnterCriticalRegion();
328 KeWaitForSingleObject(&(fdoExtension
->EjectSynchronizationEvent
),
335 // For each secure lock on this handle decrement the secured lock count
336 // for the FDO. Keep track of the new value.
339 if(FsContext
->LockCount
!= 0) {
343 InterlockedDecrement(&FsContext
->LockCount
);
346 InterlockedDecrement(&fdoExtension
->ProtectedLockCount
);
348 } while(FsContext
->LockCount
!= 0);
351 // If the new lock count has been dropped to zero then issue a lock
352 // command to the device.
356 "ClasspCleanupProtectedLocks: FDO secured lock count = %d "
358 fdoExtension
->ProtectedLockCount
,
359 fdoExtension
->LockCount
));
361 if((newDeviceLockCount
== 0) && (fdoExtension
->LockCount
== 0)) {
363 SCSI_REQUEST_BLOCK srb
;
368 "ClasspCleanupProtectedLocks: FDO lock count dropped "
371 RtlZeroMemory(&srb
, sizeof(SCSI_REQUEST_BLOCK
));
372 cdb
= (PCDB
) &(srb
.Cdb
);
376 cdb
->MEDIA_REMOVAL
.OperationCode
= SCSIOP_MEDIUM_REMOVAL
;
379 // TRUE - prevent media removal.
380 // FALSE - allow media removal.
383 cdb
->MEDIA_REMOVAL
.Prevent
= FALSE
;
386 // Set timeout value.
389 srb
.TimeOutValue
= fdoExtension
->TimeOutValue
;
390 status
= ClassSendSrbSynchronous(fdoExtension
->DeviceObject
,
397 "ClasspCleanupProtectedLocks: unlock request to drive "
398 "returned status %lx\n", status
));
402 KeSetEvent(&fdoExtension
->EjectSynchronizationEvent
,
405 KeLeaveCriticalRegion();
411 ClasspCleanupDisableMcn(
412 IN PFILE_OBJECT_EXTENSION FsContext
415 PCOMMON_DEVICE_EXTENSION commonExtension
=
416 FsContext
->DeviceObject
->DeviceExtension
;
418 PFUNCTIONAL_DEVICE_EXTENSION fdoExtension
=
419 commonExtension
->PartitionZeroExtension
;
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
));
433 // For each secure lock on this handle decrement the secured lock count
434 // for the FDO. Keep track of the new value.
437 while(FsContext
->McnDisableCount
!= 0) {
438 FsContext
->McnDisableCount
--;
439 ClassEnableMediaChangeDetection(fdoExtension
);
447 * BUGBUG REMOVE this old function implementation as soon as the
448 * boottime pagefile problems with the new one (below)
453 ClasspEjectionControl(
454 IN PDEVICE_OBJECT Fdo
,
456 IN MEDIA_LOCK_TYPE LockType
,
460 PFUNCTIONAL_DEVICE_EXTENSION FdoExtension
= Fdo
->DeviceExtension
;
461 PCOMMON_DEVICE_EXTENSION commonExtension
=
462 (PCOMMON_DEVICE_EXTENSION
) FdoExtension
;
464 PFILE_OBJECT_EXTENSION fsContext
= NULL
;
466 volatile PSCSI_REQUEST_BLOCK srb
= NULL
;
467 BOOLEAN countChanged
= FALSE
;
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.
477 status
= KeWaitForSingleObject(
478 &(FdoExtension
->EjectSynchronizationEvent
),
484 ASSERT(status
== STATUS_SUCCESS
);
487 "ClasspEjectionControl: "
488 "Received request for %s lock type\n",
489 LockTypeStrings
[LockType
]
495 srb
= ClasspAllocateSrb(FdoExtension
);
498 status
= STATUS_INSUFFICIENT_RESOURCES
;
502 RtlZeroMemory(srb
, sizeof(SCSI_REQUEST_BLOCK
));
504 cdb
= (PCDB
) srb
->Cdb
;
507 // Determine if this is a "secured" request.
510 if(LockType
== SecureMediaLock
) {
512 PIO_STACK_LOCATION irpStack
= IoGetCurrentIrpStackLocation(Irp
);
513 PFILE_OBJECT fileObject
= irpStack
->FileObject
;
516 // Make sure that the file object we are supplied has a
517 // proper FsContext before we try doing a secured lock.
520 if(fileObject
!= NULL
) {
521 fsContext
= ClasspGetFsContext(commonExtension
, fileObject
);
524 if (fsContext
== NULL
) {
527 // This handle isn't setup correctly. We can't let the
531 status
= STATUS_INVALID_PARAMETER
;
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
547 case SimpleMediaLock
: {
548 FdoExtension
->LockCount
++;
553 case SecureMediaLock
: {
554 fsContext
->LockCount
++;
555 FdoExtension
->ProtectedLockCount
++;
560 case InternalMediaLock
: {
561 FdoExtension
->InternalLockCount
++;
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....
578 case SimpleMediaLock
: {
579 if(FdoExtension
->LockCount
!= 0) {
580 FdoExtension
->LockCount
--;
586 case SecureMediaLock
: {
587 if(fsContext
->LockCount
== 0) {
588 status
= STATUS_INVALID_DEVICE_STATE
;
591 fsContext
->LockCount
--;
592 FdoExtension
->ProtectedLockCount
--;
597 case InternalMediaLock
: {
598 ASSERT(FdoExtension
->InternalLockCount
!= 0);
599 FdoExtension
->InternalLockCount
--;
606 // We only send an unlock command to the drive if both the
607 // secured and unsecured lock counts have dropped to zero.
610 if((FdoExtension
->ProtectedLockCount
!= 0) ||
611 (FdoExtension
->InternalLockCount
!= 0) ||
612 (FdoExtension
->LockCount
!= 0)) {
614 status
= STATUS_SUCCESS
;
619 status
= STATUS_SUCCESS
;
620 if (TEST_FLAG(Fdo
->Characteristics
, FILE_REMOVABLE_MEDIA
)) {
623 cdb
->MEDIA_REMOVAL
.OperationCode
= SCSIOP_MEDIUM_REMOVAL
;
626 // TRUE - prevent media removal.
627 // FALSE - allow media removal.
630 cdb
->MEDIA_REMOVAL
.Prevent
= Lock
;
633 // Set timeout value.
636 srb
->TimeOutValue
= FdoExtension
->TimeOutValue
;
639 // The actual lock operation on the device isn't so important
640 // as the internal lock counts. Ignore failures.
643 status
= ClassSendSrbSynchronous(FdoExtension
->DeviceObject
,
652 if (!NT_SUCCESS(status
)) {
654 "ClasspEjectionControl: FAILED status %x -- "
655 "reverting lock counts\n", status
));
660 // have to revert to previous counts if the
661 // lock/unlock operation actually failed.
668 case SimpleMediaLock
: {
669 FdoExtension
->LockCount
--;
673 case SecureMediaLock
: {
674 fsContext
->LockCount
--;
675 FdoExtension
->ProtectedLockCount
--;
679 case InternalMediaLock
: {
680 FdoExtension
->InternalLockCount
--;
689 case SimpleMediaLock
: {
690 FdoExtension
->LockCount
++;
694 case SecureMediaLock
: {
695 fsContext
->LockCount
++;
696 FdoExtension
->ProtectedLockCount
++;
700 case InternalMediaLock
: {
701 FdoExtension
->InternalLockCount
++;
712 "ClasspEjectionControl: Succeeded\n"));
717 "ClasspEjectionControl: "
718 "Current Counts: Internal: %x Secure: %x Simple: %x\n",
719 FdoExtension
->InternalLockCount
,
720 FdoExtension
->ProtectedLockCount
,
721 FdoExtension
->LockCount
724 KeSetEvent(&(FdoExtension
->EjectSynchronizationEvent
),
728 ClassFreeOrReuseSrb(FdoExtension
, srb
);
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.
746 ClasspEjectionControl(
747 IN PDEVICE_OBJECT Fdo
,
749 IN MEDIA_LOCK_TYPE LockType
,
753 PFUNCTIONAL_DEVICE_EXTENSION fdoExt
= Fdo
->DeviceExtension
;
754 PFILE_OBJECT_EXTENSION fsContext
;
755 BOOLEAN fileHandleOk
= TRUE
;
756 BOOLEAN countChanged
= FALSE
;
761 status
= KeWaitForSingleObject(
762 &fdoExt
->EjectSynchronizationEvent
,
767 ASSERT(status
== STATUS_SUCCESS
);
770 * If this is a "secured" request, we have to make sure
771 * that the file handle is valid.
773 if (LockType
== SecureMediaLock
){
774 PIO_STACK_LOCATION thisSp
= IoGetCurrentIrpStackLocation(Irp
);
777 * Make sure that the file object we are supplied has a
778 * proper FsContext before we try doing a secured lock.
780 if (thisSp
->FileObject
){
781 PCOMMON_DEVICE_EXTENSION commonExt
= (PCOMMON_DEVICE_EXTENSION
)fdoExt
;
782 fsContext
= ClasspGetFsContext(commonExt
, thisSp
->FileObject
);
790 fileHandleOk
= FALSE
;
797 * Adjust the lock counts and make sure they make sense.
799 status
= STATUS_SUCCESS
;
802 case SimpleMediaLock
:
806 case SecureMediaLock
:
807 fsContext
->LockCount
++;
808 fdoExt
->ProtectedLockCount
++;
811 case InternalMediaLock
:
812 fdoExt
->InternalLockCount
++;
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.
823 case SimpleMediaLock
:
824 if (fdoExt
->LockCount
> 0){
829 ASSERT(fdoExt
->LockCount
> 0);
830 status
= STATUS_INTERNAL_ERROR
;
833 case SecureMediaLock
:
834 if (fsContext
->LockCount
> 0){
835 ASSERT(fdoExt
->ProtectedLockCount
> 0);
836 fsContext
->LockCount
--;
837 fdoExt
->ProtectedLockCount
--;
841 ASSERT(fsContext
->LockCount
> 0);
842 status
= STATUS_INVALID_DEVICE_STATE
;
845 case InternalMediaLock
:
846 ASSERT(fdoExt
->InternalLockCount
> 0);
847 fdoExt
->InternalLockCount
--;
853 if (NT_SUCCESS(status
)){
855 * We only send an unlock command to the drive if
856 * all the lock counts have dropped to zero.
859 (fdoExt
->ProtectedLockCount
||
860 fdoExt
->InternalLockCount
||
864 * The lock count is still positive, so don't unlock yet.
866 status
= STATUS_SUCCESS
;
868 else if (!TEST_FLAG(Fdo
->Characteristics
, FILE_REMOVABLE_MEDIA
)) {
870 * The device isn't removable media. don't send a cmd.
872 status
= STATUS_SUCCESS
;
875 TRANSFER_PACKET
*pkt
;
877 pkt
= DequeueFreeTransferPacket(Fdo
, TRUE
);
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.
888 Irp
->Tail
.Overlay
.DriverContext
[0] = LongToPtr(1);
889 Irp
->IoStatus
.Status
= STATUS_SUCCESS
;
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.
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
;
903 status
= STATUS_INSUFFICIENT_RESOURCES
;
909 status
= STATUS_INVALID_PARAMETER
;
912 if (!NT_SUCCESS(status
) && countChanged
) {
915 // have to revert to previous counts if the
916 // lock/unlock operation actually failed.
923 case SimpleMediaLock
: {
924 FdoExtension
->LockCount
--;
928 case SecureMediaLock
: {
929 fsContext
->LockCount
--;
930 FdoExtension
->ProtectedLockCount
--;
934 case InternalMediaLock
: {
935 FdoExtension
->InternalLockCount
--;
944 case SimpleMediaLock
: {
945 FdoExtension
->LockCount
++;
949 case SecureMediaLock
: {
950 fsContext
->LockCount
++;
951 FdoExtension
->ProtectedLockCount
++;
955 case InternalMediaLock
: {
956 FdoExtension
->InternalLockCount
++;
965 KeSetEvent(&fdoExt
->EjectSynchronizationEvent
, IO_NO_INCREMENT
, FALSE
);
971 PFILE_OBJECT_EXTENSION
974 IN PCOMMON_DEVICE_EXTENSION CommonExtension
,
975 IN PFILE_OBJECT FileObject
979 return GetDictionaryEntry(&(CommonExtension
->FileObjectDictionary
),
980 (ULONGLONG
) FileObject
);