Source code of Windows XP (NT5)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

842 lines
20 KiB

  1. /*++
  2. Copyright (c) 1997-2000 Microsoft Corporation
  3. Module Name:
  4. Pnp.c
  5. Abstract:
  6. This module implements the Plug and Play routines for FAT called by
  7. the dispatch driver.
  8. // @@BEGIN_DDKSPLIT
  9. Author:
  10. Dan Lovinger [DanLo] 23-Jul-1997
  11. Revision History:
  12. // @@END_DDKSPLIT
  13. --*/
  14. #include "FatProcs.h"
  15. //
  16. // The Bug check file id for this module
  17. //
  18. #define BugCheckFileId (FAT_BUG_CHECK_PNP)
  19. NTSTATUS
  20. FatPnpQueryRemove (
  21. PIRP_CONTEXT IrpContext,
  22. PIRP Irp,
  23. PVCB Vcb
  24. );
  25. NTSTATUS
  26. FatPnpRemove (
  27. PIRP_CONTEXT IrpContext,
  28. PIRP Irp,
  29. PVCB Vcb
  30. );
  31. NTSTATUS
  32. FatPnpSurpriseRemove (
  33. PIRP_CONTEXT IrpContext,
  34. PIRP Irp,
  35. PVCB Vcb
  36. );
  37. NTSTATUS
  38. FatPnpCancelRemove (
  39. PIRP_CONTEXT IrpContext,
  40. PIRP Irp,
  41. PVCB Vcb
  42. );
  43. NTSTATUS
  44. FatPnpCompletionRoutine (
  45. IN PDEVICE_OBJECT DeviceObject,
  46. IN PIRP Irp,
  47. IN PVOID Contxt
  48. );
  49. #ifdef ALLOC_PRAGMA
  50. #pragma alloc_text(PAGE, FatCommonPnp)
  51. #pragma alloc_text(PAGE, FatFsdPnp)
  52. #pragma alloc_text(PAGE, FatPnpCancelRemove)
  53. #pragma alloc_text(PAGE, FatPnpQueryRemove)
  54. #pragma alloc_text(PAGE, FatPnpRemove)
  55. #pragma alloc_text(PAGE, FatPnpSurpriseRemove)
  56. #endif
  57. NTSTATUS
  58. FatFsdPnp (
  59. IN PVOLUME_DEVICE_OBJECT VolumeDeviceObject,
  60. IN PIRP Irp
  61. )
  62. /*++
  63. Routine Description:
  64. This routine implements the FSD part of PnP operations
  65. Arguments:
  66. VolumeDeviceObject - Supplies the volume device object where the
  67. file exists
  68. Irp - Supplies the Irp being processed
  69. Return Value:
  70. NTSTATUS - The FSD status for the IRP
  71. --*/
  72. {
  73. NTSTATUS Status;
  74. PIRP_CONTEXT IrpContext = NULL;
  75. BOOLEAN TopLevel;
  76. BOOLEAN Wait;
  77. DebugTrace(+1, Dbg, "FatFsdPnp\n", 0);
  78. FsRtlEnterFileSystem();
  79. TopLevel = FatIsIrpTopLevel( Irp );
  80. try {
  81. //
  82. // We expect there to never be a fileobject, in which case we will always
  83. // wait. Since at the moment we don't have any concept of pending Pnp
  84. // operations, this is a bit nitpicky.
  85. //
  86. if (IoGetCurrentIrpStackLocation( Irp )->FileObject == NULL) {
  87. Wait = TRUE;
  88. } else {
  89. Wait = CanFsdWait( Irp );
  90. }
  91. IrpContext = FatCreateIrpContext( Irp, Wait );
  92. Status = FatCommonPnp( IrpContext, Irp );
  93. } except(FatExceptionFilter( IrpContext, GetExceptionInformation() )) {
  94. //
  95. // We had some trouble trying to perform the requested
  96. // operation, so we'll abort the I/O request with
  97. // the error status that we get back from the
  98. // execption code
  99. //
  100. Status = FatProcessException( IrpContext, Irp, GetExceptionCode() );
  101. }
  102. if (TopLevel) { IoSetTopLevelIrp( NULL ); }
  103. FsRtlExitFileSystem();
  104. //
  105. // And return to our caller
  106. //
  107. DebugTrace(-1, Dbg, "FatFsdPnp -> %08lx\n", Status);
  108. UNREFERENCED_PARAMETER( VolumeDeviceObject );
  109. return Status;
  110. }
  111. NTSTATUS
  112. FatCommonPnp (
  113. IN PIRP_CONTEXT IrpContext,
  114. IN PIRP Irp
  115. )
  116. /*++
  117. Routine Description:
  118. This is the common routine for doing PnP operations called
  119. by both the fsd and fsp threads
  120. Arguments:
  121. Irp - Supplies the Irp to process
  122. Return Value:
  123. NTSTATUS - The return status for the operation
  124. --*/
  125. {
  126. NTSTATUS Status;
  127. PIO_STACK_LOCATION IrpSp;
  128. PVOLUME_DEVICE_OBJECT OurDeviceObject;
  129. PVCB Vcb;
  130. //
  131. // Get the current Irp stack location.
  132. //
  133. IrpSp = IoGetCurrentIrpStackLocation( Irp );
  134. //
  135. // Find our Vcb. This is tricky since we have no file object in the Irp.
  136. //
  137. OurDeviceObject = (PVOLUME_DEVICE_OBJECT) IrpSp->DeviceObject;
  138. //
  139. // Make sure this device object really is big enough to be a volume device
  140. // object. If it isn't, we need to get out before we try to reference some
  141. // field that takes us past the end of an ordinary device object.
  142. //
  143. if (OurDeviceObject->DeviceObject.Size != sizeof(VOLUME_DEVICE_OBJECT) ||
  144. NodeType( &OurDeviceObject->Vcb ) != FAT_NTC_VCB) {
  145. //
  146. // We were called with something we don't understand.
  147. //
  148. Status = STATUS_INVALID_PARAMETER;
  149. FatCompleteRequest( IrpContext, Irp, Status );
  150. return Status;
  151. }
  152. //
  153. // Force everything to wait.
  154. //
  155. SetFlag(IrpContext->Flags, IRP_CONTEXT_FLAG_WAIT);
  156. Vcb = &OurDeviceObject->Vcb;
  157. //
  158. // Case on the minor code.
  159. //
  160. switch ( IrpSp->MinorFunction ) {
  161. case IRP_MN_QUERY_REMOVE_DEVICE:
  162. Status = FatPnpQueryRemove( IrpContext, Irp, Vcb );
  163. break;
  164. case IRP_MN_SURPRISE_REMOVAL:
  165. Status = FatPnpSurpriseRemove( IrpContext, Irp, Vcb );
  166. break;
  167. case IRP_MN_REMOVE_DEVICE:
  168. Status = FatPnpRemove( IrpContext, Irp, Vcb );
  169. break;
  170. case IRP_MN_CANCEL_REMOVE_DEVICE:
  171. Status = FatPnpCancelRemove( IrpContext, Irp, Vcb );
  172. break;
  173. default:
  174. //
  175. // Just pass the IRP on. As we do not need to be in the
  176. // way on return, ellide ourselves out of the stack.
  177. //
  178. IoSkipCurrentIrpStackLocation( Irp );
  179. Status = IoCallDriver(Vcb->TargetDeviceObject, Irp);
  180. //
  181. // Cleanup our Irp Context. The driver has completed the Irp.
  182. //
  183. FatCompleteRequest( IrpContext, NULL, STATUS_SUCCESS );
  184. break;
  185. }
  186. return Status;
  187. }
  188. NTSTATUS
  189. FatPnpQueryRemove (
  190. PIRP_CONTEXT IrpContext,
  191. PIRP Irp,
  192. PVCB Vcb
  193. )
  194. /*++
  195. Routine Description:
  196. This routine handles the PnP query remove operation. The filesystem
  197. is responsible for answering whether there are any reasons it sees
  198. that the volume can not go away (and the device removed). Initiation
  199. of the dismount begins when we answer yes to this question.
  200. Query will be followed by a Cancel or Remove.
  201. Arguments:
  202. Irp - Supplies the Irp to process
  203. Vcb - Supplies the volume being queried.
  204. Return Value:
  205. NTSTATUS - The return status for the operation
  206. --*/
  207. {
  208. NTSTATUS Status;
  209. KEVENT Event;
  210. BOOLEAN VcbDeleted = FALSE;
  211. BOOLEAN GlobalHeld = FALSE;
  212. //
  213. // Having said yes to a QUERY, any communication with the
  214. // underlying storage stack is undefined (and may block)
  215. // until the bounding CANCEL or REMOVE is sent.
  216. //
  217. //
  218. // Acquire the global resource so that we can try to vaporize
  219. // the volume, and the vcb resource itself.
  220. //
  221. FatAcquireExclusiveVcb( IrpContext, Vcb );
  222. try {
  223. Status = FatLockVolumeInternal( IrpContext, Vcb, NULL );
  224. //
  225. // Reacquire the resources in the right order.
  226. //
  227. FatReleaseVcb( IrpContext, Vcb );
  228. FatAcquireExclusiveGlobal( IrpContext );
  229. GlobalHeld = TRUE;
  230. FatAcquireExclusiveVcb( IrpContext, Vcb );
  231. if (NT_SUCCESS( Status )) {
  232. //
  233. // With the volume held locked, note that we must finalize as much
  234. // as possible right now.
  235. //
  236. FatFlushAndCleanVolume( IrpContext, Irp, Vcb, Flush );
  237. //
  238. // We need to pass this down before starting the dismount, which
  239. // could disconnect us immediately from the stack.
  240. //
  241. //
  242. // Get the next stack location, and copy over the stack location
  243. //
  244. IoCopyCurrentIrpStackLocationToNext( Irp );
  245. //
  246. // Set up the completion routine
  247. //
  248. KeInitializeEvent( &Event, NotificationEvent, FALSE );
  249. IoSetCompletionRoutine( Irp,
  250. FatPnpCompletionRoutine,
  251. &Event,
  252. TRUE,
  253. TRUE,
  254. TRUE );
  255. //
  256. // Send the request and wait.
  257. //
  258. Status = IoCallDriver(Vcb->TargetDeviceObject, Irp);
  259. if (Status == STATUS_PENDING) {
  260. KeWaitForSingleObject( &Event,
  261. Executive,
  262. KernelMode,
  263. FALSE,
  264. NULL );
  265. Status = Irp->IoStatus.Status;
  266. }
  267. //
  268. // Now if no one below us failed already, initiate the dismount
  269. // on this volume, make it go away. PnP needs to see our internal
  270. // streams close and drop their references to the target device.
  271. //
  272. // Since we were able to lock the volume, we are guaranteed to
  273. // move this volume into dismount state and disconnect it from
  274. // the underlying storage stack. The force on our part is actually
  275. // unnecesary, though complete.
  276. //
  277. // What is not strictly guaranteed, though, is that the closes
  278. // for the metadata streams take effect synchronously underneath
  279. // of this call. This would leave references on the target device
  280. // even though we are disconnected!
  281. //
  282. if (NT_SUCCESS( Status )) {
  283. VcbDeleted = FatCheckForDismount( IrpContext, Vcb, TRUE );
  284. ASSERT( VcbDeleted || Vcb->VcbCondition == VcbBad );
  285. }
  286. }
  287. //
  288. // Release the Vcb if it could still remain.
  289. //
  290. // Note: if everything else succeeded and the Vcb is persistent because the
  291. // internal streams did not vaporize, we really need to pend this IRP off on
  292. // the side until the dismount is completed. I can't think of a reasonable
  293. // case (in FAT) where this would actually happen, though it might still need
  294. // to be implemented.
  295. //
  296. // The reason this is the case is that handles/fileobjects place a reference
  297. // on the device objects they overly. In the filesystem case, these references
  298. // are on our target devices. PnP correcly thinks that if references remain
  299. // on the device objects in the stack that someone has a handle, and that this
  300. // counts as a reason to not succeed the query - even though every interrogated
  301. // driver thinks that it is OK.
  302. //
  303. ASSERT( !(NT_SUCCESS( Status ) && !VcbDeleted ));
  304. } finally {
  305. if (!VcbDeleted) {
  306. FatReleaseVcb( IrpContext, Vcb );
  307. }
  308. if (GlobalHeld) {
  309. FatReleaseGlobal( IrpContext );
  310. }
  311. }
  312. //
  313. // Cleanup our IrpContext and complete the IRP if neccesary.
  314. //
  315. FatCompleteRequest( IrpContext, Irp, Status );
  316. return Status;
  317. }
  318. NTSTATUS
  319. FatPnpRemove (
  320. PIRP_CONTEXT IrpContext,
  321. PIRP Irp,
  322. PVCB Vcb
  323. )
  324. /*++
  325. Routine Description:
  326. This routine handles the PnP remove operation. This is our notification
  327. that the underlying storage device for the volume we have is gone, and
  328. an excellent indication that the volume will never reappear. The filesystem
  329. is responsible for initiation or completion of the dismount.
  330. Arguments:
  331. Irp - Supplies the Irp to process
  332. Vcb - Supplies the volume being removed.
  333. Return Value:
  334. NTSTATUS - The return status for the operation
  335. --*/
  336. {
  337. NTSTATUS Status;
  338. KEVENT Event;
  339. BOOLEAN VcbDeleted;
  340. //
  341. // REMOVE - a storage device is now gone. We either got
  342. // QUERY'd and said yes OR got a SURPRISE OR a storage
  343. // stack failed to spin back up from a sleep/stop state
  344. // (the only case in which this will be the first warning).
  345. //
  346. // Note that it is entirely unlikely that we will be around
  347. // for a REMOVE in the first two cases, as we try to intiate
  348. // dismount.
  349. //
  350. //
  351. // Acquire the global resource so that we can try to vaporize
  352. // the volume, and the vcb resource itself.
  353. //
  354. FatAcquireExclusiveGlobal( IrpContext );
  355. FatAcquireExclusiveVcb( IrpContext, Vcb );
  356. //
  357. // The device will be going away. Remove our lock (benign
  358. // if we never had it).
  359. //
  360. (VOID) FatUnlockVolumeInternal( IrpContext, Vcb, NULL );
  361. //
  362. // We need to pass this down before starting the dismount, which
  363. // could disconnect us immediately from the stack.
  364. //
  365. //
  366. // Get the next stack location, and copy over the stack location
  367. //
  368. IoCopyCurrentIrpStackLocationToNext( Irp );
  369. //
  370. // Set up the completion routine
  371. //
  372. KeInitializeEvent( &Event, NotificationEvent, FALSE );
  373. IoSetCompletionRoutine( Irp,
  374. FatPnpCompletionRoutine,
  375. &Event,
  376. TRUE,
  377. TRUE,
  378. TRUE );
  379. //
  380. // Send the request and wait.
  381. //
  382. Status = IoCallDriver(Vcb->TargetDeviceObject, Irp);
  383. if (Status == STATUS_PENDING) {
  384. KeWaitForSingleObject( &Event,
  385. Executive,
  386. KernelMode,
  387. FALSE,
  388. NULL );
  389. Status = Irp->IoStatus.Status;
  390. }
  391. try {
  392. //
  393. // Knock as many files down for this volume as we can.
  394. //
  395. FatFlushAndCleanVolume( IrpContext, Irp, Vcb, NoFlush );
  396. //
  397. // Now make our dismount happen. This may not vaporize the
  398. // Vcb, of course, since there could be any number of handles
  399. // outstanding if we were not preceeded by a QUERY.
  400. //
  401. // PnP will take care of disconnecting this stack if we
  402. // couldn't get off of it immediately.
  403. //
  404. VcbDeleted = FatCheckForDismount( IrpContext, Vcb, TRUE );
  405. } finally {
  406. //
  407. // Release the Vcb if it could still remain.
  408. //
  409. if (!VcbDeleted) {
  410. FatReleaseVcb( IrpContext, Vcb );
  411. }
  412. FatReleaseGlobal( IrpContext );
  413. }
  414. //
  415. // Cleanup our IrpContext and complete the IRP.
  416. //
  417. FatCompleteRequest( IrpContext, Irp, Status );
  418. return Status;
  419. }
  420. NTSTATUS
  421. FatPnpSurpriseRemove (
  422. PIRP_CONTEXT IrpContext,
  423. PIRP Irp,
  424. PVCB Vcb
  425. )
  426. /*++
  427. Routine Description:
  428. This routine handles the PnP surprise remove operation. This is another
  429. type of notification that the underlying storage device for the volume we
  430. have is gone, and is excellent indication that the volume will never reappear.
  431. The filesystem is responsible for initiation or completion the dismount.
  432. For the most part, only "real" drivers care about the distinction of a
  433. surprise remove, which is a result of our noticing that a user (usually)
  434. physically reached into the machine and pulled something out.
  435. Surprise will be followed by a Remove when all references have been shut down.
  436. Arguments:
  437. Irp - Supplies the Irp to process
  438. Vcb - Supplies the volume being removed.
  439. Return Value:
  440. NTSTATUS - The return status for the operation
  441. --*/
  442. {
  443. NTSTATUS Status;
  444. KEVENT Event;
  445. BOOLEAN VcbDeleted;
  446. //
  447. // SURPRISE - a device was physically yanked away without
  448. // any warning. This means external forces.
  449. //
  450. FatAcquireExclusiveGlobal( IrpContext );
  451. FatAcquireExclusiveVcb( IrpContext, Vcb );
  452. //
  453. // We need to pass this down before starting the dismount, which
  454. // could disconnect us immediately from the stack.
  455. //
  456. //
  457. // Get the next stack location, and copy over the stack location
  458. //
  459. IoCopyCurrentIrpStackLocationToNext( Irp );
  460. //
  461. // Set up the completion routine
  462. //
  463. KeInitializeEvent( &Event, NotificationEvent, FALSE );
  464. IoSetCompletionRoutine( Irp,
  465. FatPnpCompletionRoutine,
  466. &Event,
  467. TRUE,
  468. TRUE,
  469. TRUE );
  470. //
  471. // Send the request and wait.
  472. //
  473. Status = IoCallDriver(Vcb->TargetDeviceObject, Irp);
  474. if (Status == STATUS_PENDING) {
  475. KeWaitForSingleObject( &Event,
  476. Executive,
  477. KernelMode,
  478. FALSE,
  479. NULL );
  480. Status = Irp->IoStatus.Status;
  481. }
  482. try {
  483. //
  484. // Knock as many files down for this volume as we can.
  485. //
  486. FatFlushAndCleanVolume( IrpContext, Irp, Vcb, NoFlush );
  487. //
  488. // Now make our dismount happen. This may not vaporize the
  489. // Vcb, of course, since there could be any number of handles
  490. // outstanding since this is an out of band notification.
  491. //
  492. VcbDeleted = FatCheckForDismount( IrpContext, Vcb, TRUE );
  493. } finally {
  494. //
  495. // Release the Vcb if it could still remain.
  496. //
  497. if (!VcbDeleted) {
  498. FatReleaseVcb( IrpContext, Vcb );
  499. }
  500. FatReleaseGlobal( IrpContext );
  501. }
  502. //
  503. // Cleanup our IrpContext and complete the IRP.
  504. //
  505. FatCompleteRequest( IrpContext, Irp, Status );
  506. return Status;
  507. }
  508. NTSTATUS
  509. FatPnpCancelRemove (
  510. PIRP_CONTEXT IrpContext,
  511. PIRP Irp,
  512. PVCB Vcb
  513. )
  514. /*++
  515. Routine Description:
  516. This routine handles the PnP cancel remove operation. This is our
  517. notification that a previously proposed remove (query) was eventually
  518. vetoed by a component. The filesystem is responsible for cleaning up
  519. and getting ready for more IO.
  520. Arguments:
  521. Irp - Supplies the Irp to process
  522. Vcb - Supplies the volume being removed.
  523. Return Value:
  524. NTSTATUS - The return status for the operation
  525. --*/
  526. {
  527. NTSTATUS Status;
  528. //
  529. // CANCEL - a previous QUERY has been rescinded as a result
  530. // of someone vetoing. Since PnP cannot figure out who may
  531. // have gotten the QUERY (think about it: stacked drivers),
  532. // we must expect to deal with getting a CANCEL without having
  533. // seen the QUERY.
  534. //
  535. // For FAT, this is quite easy. In fact, we can't get a
  536. // CANCEL if the underlying drivers succeeded the QUERY since
  537. // we disconnect the Vpb on our dismount initiation. This is
  538. // actually pretty important because if PnP could get to us
  539. // after the disconnect we'd be thoroughly unsynchronized
  540. // with respect to the Vcb getting torn apart - merely referencing
  541. // the volume device object is insufficient to keep us intact.
  542. //
  543. FatAcquireExclusiveVcb( IrpContext, Vcb );
  544. //
  545. // Unlock the volume. This is benign if we never had seen
  546. // a QUERY.
  547. //
  548. Status = FatUnlockVolumeInternal( IrpContext, Vcb, NULL );
  549. try {
  550. //
  551. // We must re-enable allocation support if we got through
  552. // the first stages of a QUERY_REMOVE; i.e., we decided we
  553. // could place a lock on the volume.
  554. //
  555. if (NT_SUCCESS( Status )) {
  556. FatSetupAllocationSupport( IrpContext, Vcb );
  557. }
  558. } finally {
  559. FatReleaseVcb( IrpContext, Vcb );
  560. }
  561. //
  562. // Send the request. The underlying driver will complete the
  563. // IRP. Since we don't need to be in the way, simply ellide
  564. // ourselves out of the IRP stack.
  565. //
  566. IoSkipCurrentIrpStackLocation( Irp );
  567. Status = IoCallDriver(Vcb->TargetDeviceObject, Irp);
  568. FatCompleteRequest( IrpContext, NULL, STATUS_SUCCESS );
  569. return Status;
  570. }
  571. //
  572. // Local support routine
  573. //
  574. NTSTATUS
  575. FatPnpCompletionRoutine (
  576. IN PDEVICE_OBJECT DeviceObject,
  577. IN PIRP Irp,
  578. IN PVOID Contxt
  579. )
  580. {
  581. PKEVENT Event = (PKEVENT) Contxt;
  582. KeSetEvent( Event, 0, FALSE );
  583. return STATUS_MORE_PROCESSING_REQUIRED;
  584. UNREFERENCED_PARAMETER( DeviceObject );
  585. UNREFERENCED_PARAMETER( Contxt );
  586. }