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.

731 lines
18 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 UDFS 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 "UdfProcs.h"
  15. //
  16. // The Bug check file id for this module
  17. //
  18. #define BugCheckFileId (UDFS_BUG_CHECK_PNP)
  19. NTSTATUS
  20. UdfPnpQueryRemove (
  21. PIRP_CONTEXT IrpContext,
  22. PIRP Irp,
  23. PVCB Vcb
  24. );
  25. NTSTATUS
  26. UdfPnpRemove (
  27. PIRP_CONTEXT IrpContext,
  28. PIRP Irp,
  29. PVCB Vcb
  30. );
  31. NTSTATUS
  32. UdfPnpSurpriseRemove (
  33. PIRP_CONTEXT IrpContext,
  34. PIRP Irp,
  35. PVCB Vcb
  36. );
  37. NTSTATUS
  38. UdfPnpCancelRemove (
  39. PIRP_CONTEXT IrpContext,
  40. PIRP Irp,
  41. PVCB Vcb
  42. );
  43. NTSTATUS
  44. UdfPnpCompletionRoutine (
  45. IN PDEVICE_OBJECT DeviceObject,
  46. IN PIRP Irp,
  47. IN PVOID Contxt
  48. );
  49. #ifdef ALLOC_PRAGMA
  50. #pragma alloc_text(PAGE, UdfCommonPnp)
  51. #pragma alloc_text(PAGE, UdfPnpCancelRemove)
  52. #pragma alloc_text(PAGE, UdfPnpQueryRemove)
  53. #pragma alloc_text(PAGE, UdfPnpRemove)
  54. #pragma alloc_text(PAGE, UdfPnpSurpriseRemove)
  55. #endif
  56. NTSTATUS
  57. UdfCommonPnp (
  58. IN PIRP_CONTEXT IrpContext,
  59. IN PIRP Irp
  60. )
  61. /*++
  62. Routine Description:
  63. This is the common routine for doing PnP operations called
  64. by both the fsd and fsp threads
  65. Arguments:
  66. Irp - Supplies the Irp to process
  67. Return Value:
  68. NTSTATUS - The return status for the operation
  69. --*/
  70. {
  71. NTSTATUS Status;
  72. PIO_STACK_LOCATION IrpSp;
  73. PVOLUME_DEVICE_OBJECT OurDeviceObject;
  74. PVCB Vcb;
  75. //
  76. // Get the current Irp stack location.
  77. //
  78. IrpSp = IoGetCurrentIrpStackLocation( Irp );
  79. //
  80. // Find our Vcb. This is tricky since we have no file object in the Irp.
  81. //
  82. OurDeviceObject = (PVOLUME_DEVICE_OBJECT) IrpSp->DeviceObject;
  83. //
  84. // Make sure this device object really is big enough to be a volume device
  85. // object. If it isn't, we need to get out before we try to reference some
  86. // field that takes us past the end of an ordinary device object.
  87. //
  88. if (OurDeviceObject->DeviceObject.Size != sizeof(VOLUME_DEVICE_OBJECT) ||
  89. NodeType( &OurDeviceObject->Vcb ) != UDFS_NTC_VCB) {
  90. //
  91. // We were called with something we don't understand.
  92. //
  93. Status = STATUS_INVALID_PARAMETER;
  94. UdfCompleteRequest( IrpContext, Irp, Status );
  95. return Status;
  96. }
  97. //
  98. // Force all PnP operations to be synchronous.
  99. //
  100. SetFlag( IrpContext->Flags, IRP_CONTEXT_FLAG_WAIT );
  101. Vcb = &OurDeviceObject->Vcb;
  102. //
  103. // Case on the minor code.
  104. //
  105. switch ( IrpSp->MinorFunction ) {
  106. case IRP_MN_QUERY_REMOVE_DEVICE:
  107. Status = UdfPnpQueryRemove( IrpContext, Irp, Vcb );
  108. break;
  109. case IRP_MN_SURPRISE_REMOVAL:
  110. Status = UdfPnpSurpriseRemove( IrpContext, Irp, Vcb );
  111. break;
  112. case IRP_MN_REMOVE_DEVICE:
  113. Status = UdfPnpRemove( IrpContext, Irp, Vcb );
  114. break;
  115. case IRP_MN_CANCEL_REMOVE_DEVICE:
  116. Status = UdfPnpCancelRemove( IrpContext, Irp, Vcb );
  117. break;
  118. default:
  119. //
  120. // Just pass the IRP on. As we do not need to be in the
  121. // way on return, ellide ourselves out of the stack.
  122. //
  123. IoSkipCurrentIrpStackLocation( Irp );
  124. Status = IoCallDriver(Vcb->TargetDeviceObject, Irp);
  125. //
  126. // Cleanup our Irp Context. The driver has completed the Irp.
  127. //
  128. UdfCompleteRequest( IrpContext, NULL, STATUS_SUCCESS );
  129. break;
  130. }
  131. return Status;
  132. }
  133. NTSTATUS
  134. UdfPnpQueryRemove (
  135. PIRP_CONTEXT IrpContext,
  136. PIRP Irp,
  137. PVCB Vcb
  138. )
  139. /*++
  140. Routine Description:
  141. This routine handles the PnP query remove operation. The filesystem
  142. is responsible for answering whether there are any reasons it sees
  143. that the volume can not go away (and the device removed). Initiation
  144. of the dismount begins when we answer yes to this question.
  145. Query will be followed by a Cancel or Remove.
  146. Arguments:
  147. Irp - Supplies the Irp to process
  148. Vcb - Supplies the volume being queried.
  149. Return Value:
  150. NTSTATUS - The return status for the operation
  151. --*/
  152. {
  153. NTSTATUS Status;
  154. KEVENT Event;
  155. BOOLEAN VcbPresent = TRUE;
  156. //
  157. // Having said yes to a QUERY, any communication with the
  158. // underlying storage stack is undefined (and may block)
  159. // until the bounding CANCEL or REMOVE is sent.
  160. //
  161. //
  162. // Acquire the global resource so that we can try to vaporize
  163. // the volume, and the vcb resource itself.
  164. //
  165. UdfAcquireVcbExclusive( IrpContext, Vcb, FALSE );
  166. Status = UdfLockVolumeInternal( IrpContext, Vcb, NULL );
  167. UdfReleaseVcb( IrpContext, Vcb );
  168. UdfAcquireUdfData( IrpContext );
  169. UdfAcquireVcbExclusive( IrpContext, Vcb, FALSE );
  170. if (NT_SUCCESS( Status )) {
  171. //
  172. // We need to pass this down before starting the dismount, which
  173. // could disconnect us immediately from the stack.
  174. //
  175. //
  176. // Get the next stack location, and copy over the stack location
  177. //
  178. IoCopyCurrentIrpStackLocationToNext( Irp );
  179. //
  180. // Set up the completion routine
  181. //
  182. KeInitializeEvent( &Event, NotificationEvent, FALSE );
  183. IoSetCompletionRoutine( Irp,
  184. UdfPnpCompletionRoutine,
  185. &Event,
  186. TRUE,
  187. TRUE,
  188. TRUE );
  189. //
  190. // Send the request and wait.
  191. //
  192. Status = IoCallDriver(Vcb->TargetDeviceObject, Irp);
  193. if (Status == STATUS_PENDING) {
  194. KeWaitForSingleObject( &Event,
  195. Executive,
  196. KernelMode,
  197. FALSE,
  198. NULL );
  199. Status = Irp->IoStatus.Status;
  200. }
  201. //
  202. // Now if no one below us failed already, initiate the dismount
  203. // on this volume, make it go away. PnP needs to see our internal
  204. // streams close and drop their references to the target device.
  205. //
  206. // Since we were able to lock the volume, we are guaranteed to
  207. // move this volume into dismount state and disconnect it from
  208. // the underlying storage stack. The force on our part is actually
  209. // unnecesary, though complete.
  210. //
  211. // What is not strictly guaranteed, though, is that the closes
  212. // for the metadata streams take effect synchronously underneath
  213. // of this call. This would leave references on the target device
  214. // even though we are disconnected!
  215. //
  216. if (NT_SUCCESS( Status )) {
  217. VcbPresent = UdfCheckForDismount( IrpContext, Vcb, TRUE );
  218. ASSERT( !VcbPresent || Vcb->VcbCondition == VcbDismountInProgress );
  219. }
  220. }
  221. //
  222. // Release the Vcb if it could still remain.
  223. //
  224. // Note: if everything else succeeded and the Vcb is persistent because the
  225. // internal streams did not vaporize, we really need to pend this IRP off on
  226. // the side until the dismount is completed. I can't think of a reasonable
  227. // case (in UDFS) where this would actually happen, though it might still need
  228. // to be implemented.
  229. //
  230. // The reason this is the case is that handles/fileobjects place a reference
  231. // on the device objects they overly. In the filesystem case, these references
  232. // are on our target devices. PnP correcly thinks that if references remain
  233. // on the device objects in the stack that someone has a handle, and that this
  234. // counts as a reason to not succeed the query - even though every interrogated
  235. // driver thinks that it is OK.
  236. //
  237. ASSERT( !(NT_SUCCESS( Status ) && VcbPresent && Vcb->VcbReference != 0));
  238. if (VcbPresent) {
  239. UdfReleaseVcb( IrpContext, Vcb );
  240. }
  241. UdfReleaseUdfData( IrpContext );
  242. //
  243. // Cleanup our IrpContext and complete the IRP if neccesary.
  244. //
  245. UdfCompleteRequest( IrpContext, Irp, Status );
  246. return Status;
  247. }
  248. NTSTATUS
  249. UdfPnpRemove (
  250. PIRP_CONTEXT IrpContext,
  251. PIRP Irp,
  252. PVCB Vcb
  253. )
  254. /*++
  255. Routine Description:
  256. This routine handles the PnP remove operation. This is our notification
  257. that the underlying storage device for the volume we have is gone, and
  258. an excellent indication that the volume will never reappear. The filesystem
  259. is responsible for initiation or completion the dismount.
  260. Arguments:
  261. Irp - Supplies the Irp to process
  262. Vcb - Supplies the volume being removed.
  263. Return Value:
  264. NTSTATUS - The return status for the operation
  265. --*/
  266. {
  267. NTSTATUS Status;
  268. KEVENT Event;
  269. BOOLEAN VcbPresent = TRUE;
  270. //
  271. // REMOVE - a storage device is now gone. We either got
  272. // QUERY'd and said yes OR got a SURPRISE OR a storage
  273. // stack failed to spin back up from a sleep/stop state
  274. // (the only case in which this will be the first warning).
  275. //
  276. // Note that it is entirely unlikely that we will be around
  277. // for a REMOVE in the first two cases, as we try to intiate
  278. // dismount.
  279. //
  280. //
  281. // Acquire the global resource so that we can try to vaporize
  282. // the volume, and the vcb resource itself.
  283. //
  284. UdfAcquireUdfData( IrpContext );
  285. UdfAcquireVcbExclusive( IrpContext, Vcb, FALSE );
  286. //
  287. // The device will be going away. Remove our lock and find
  288. // out if we ever had one in the first place.
  289. //
  290. Status = UdfUnlockVolumeInternal( IrpContext, Vcb, NULL );
  291. //
  292. // If the volume had not been locked, we must invalidate the
  293. // volume to ensure it goes away properly. The remove will
  294. // succeed.
  295. //
  296. if (!NT_SUCCESS( Status )) {
  297. UdfLockVcb( IrpContext, Vcb );
  298. if (Vcb->VcbCondition != VcbDismountInProgress) {
  299. UdfSetVcbCondition( Vcb, VcbInvalid);
  300. }
  301. UdfUnlockVcb( IrpContext, Vcb );
  302. Status = STATUS_SUCCESS;
  303. }
  304. //
  305. // We need to pass this down before starting the dismount, which
  306. // could disconnect us immediately from the stack.
  307. //
  308. //
  309. // Get the next stack location, and copy over the stack location
  310. //
  311. IoCopyCurrentIrpStackLocationToNext( Irp );
  312. //
  313. // Set up the completion routine
  314. //
  315. KeInitializeEvent( &Event, NotificationEvent, FALSE );
  316. IoSetCompletionRoutine( Irp,
  317. UdfPnpCompletionRoutine,
  318. &Event,
  319. TRUE,
  320. TRUE,
  321. TRUE );
  322. //
  323. // Send the request and wait.
  324. //
  325. Status = IoCallDriver(Vcb->TargetDeviceObject, Irp);
  326. if (Status == STATUS_PENDING) {
  327. KeWaitForSingleObject( &Event,
  328. Executive,
  329. KernelMode,
  330. FALSE,
  331. NULL );
  332. Status = Irp->IoStatus.Status;
  333. }
  334. //
  335. // Now make our dismount happen. This may not vaporize the
  336. // Vcb, of course, since there could be any number of handles
  337. // outstanding if we were not preceeded by a QUERY.
  338. //
  339. // PnP will take care of disconnecting this stack if we
  340. // couldn't get off of it immediately.
  341. //
  342. VcbPresent = UdfCheckForDismount( IrpContext, Vcb, TRUE );
  343. //
  344. // Release the Vcb if it could still remain.
  345. //
  346. if (VcbPresent) {
  347. UdfReleaseVcb( IrpContext, Vcb );
  348. }
  349. UdfReleaseUdfData( IrpContext );
  350. //
  351. // Cleanup our IrpContext and complete the IRP.
  352. //
  353. UdfCompleteRequest( IrpContext, Irp, Status );
  354. return Status;
  355. }
  356. NTSTATUS
  357. UdfPnpSurpriseRemove (
  358. PIRP_CONTEXT IrpContext,
  359. PIRP Irp,
  360. PVCB Vcb
  361. )
  362. /*++
  363. Routine Description:
  364. This routine handles the PnP surprise remove operation. This is another
  365. type of notification that the underlying storage device for the volume we
  366. have is gone, and is excellent indication that the volume will never reappear.
  367. The filesystem is responsible for initiation or completion the dismount.
  368. For the most part, only "real" drivers care about the distinction of a
  369. surprise remove, which is a result of our noticing that a user (usually)
  370. physically reached into the machine and pulled something out.
  371. Surprise will be followed by a Remove when all references have been shut down.
  372. Arguments:
  373. Irp - Supplies the Irp to process
  374. Vcb - Supplies the volume being removed.
  375. Return Value:
  376. NTSTATUS - The return status for the operation
  377. --*/
  378. {
  379. NTSTATUS Status;
  380. KEVENT Event;
  381. BOOLEAN VcbPresent = TRUE;
  382. //
  383. // SURPRISE - a device was physically yanked away without
  384. // any warning. This means external forces.
  385. //
  386. UdfAcquireUdfData( IrpContext );
  387. UdfAcquireVcbExclusive( IrpContext, Vcb, FALSE );
  388. //
  389. // Invalidate the volume right now.
  390. //
  391. // The intent here is to make every subsequent operation
  392. // on the volume fail and grease the rails toward dismount.
  393. // By definition there is no going back from a SURPRISE.
  394. //
  395. UdfLockVcb( IrpContext, Vcb );
  396. if (Vcb->VcbCondition != VcbDismountInProgress) {
  397. UdfSetVcbCondition( Vcb, VcbInvalid);
  398. }
  399. UdfUnlockVcb( IrpContext, Vcb );
  400. //
  401. // We need to pass this down before starting the dismount, which
  402. // could disconnect us immediately from the stack.
  403. //
  404. //
  405. // Get the next stack location, and copy over the stack location
  406. //
  407. IoCopyCurrentIrpStackLocationToNext( Irp );
  408. //
  409. // Set up the completion routine
  410. //
  411. KeInitializeEvent( &Event, NotificationEvent, FALSE );
  412. IoSetCompletionRoutine( Irp,
  413. UdfPnpCompletionRoutine,
  414. &Event,
  415. TRUE,
  416. TRUE,
  417. TRUE );
  418. //
  419. // Send the request and wait.
  420. //
  421. Status = IoCallDriver(Vcb->TargetDeviceObject, Irp);
  422. if (Status == STATUS_PENDING) {
  423. KeWaitForSingleObject( &Event,
  424. Executive,
  425. KernelMode,
  426. FALSE,
  427. NULL );
  428. Status = Irp->IoStatus.Status;
  429. }
  430. //
  431. // Now make our dismount happen. This may not vaporize the
  432. // Vcb, of course, since there could be any number of handles
  433. // outstanding since this is an out of band notification.
  434. //
  435. VcbPresent = UdfCheckForDismount( IrpContext, Vcb, TRUE );
  436. //
  437. // Release the Vcb if it could still remain.
  438. //
  439. if (VcbPresent) {
  440. UdfReleaseVcb( IrpContext, Vcb );
  441. }
  442. UdfReleaseUdfData( IrpContext );
  443. //
  444. // Cleanup our IrpContext and complete the IRP.
  445. //
  446. UdfCompleteRequest( IrpContext, Irp, Status );
  447. return Status;
  448. }
  449. NTSTATUS
  450. UdfPnpCancelRemove (
  451. PIRP_CONTEXT IrpContext,
  452. PIRP Irp,
  453. PVCB Vcb
  454. )
  455. /*++
  456. Routine Description:
  457. This routine handles the PnP cancel remove operation. This is our
  458. notification that a previously proposed remove (query) was eventually
  459. vetoed by a component. The filesystem is responsible for cleaning up
  460. and getting ready for more IO.
  461. Arguments:
  462. Irp - Supplies the Irp to process
  463. Vcb - Supplies the volume being removed.
  464. Return Value:
  465. NTSTATUS - The return status for the operation
  466. --*/
  467. {
  468. NTSTATUS Status;
  469. //
  470. // CANCEL - a previous QUERY has been rescinded as a result
  471. // of someone vetoing. Since PnP cannot figure out who may
  472. // have gotten the QUERY (think about it: stacked drivers),
  473. // we must expect to deal with getting a CANCEL without having
  474. // seen the QUERY.
  475. //
  476. // For UDFS, this is quite easy. In fact, we can't get a
  477. // CANCEL if the underlying drivers succeeded the QUERY since
  478. // we disconnect the Vpb on our dismount initiation. This is
  479. // actually pretty important because if PnP could get to us
  480. // after the disconnect we'd be thoroughly unsynchronized
  481. // with respect to the Vcb getting torn apart - merely referencing
  482. // the volume device object is insufficient to keep us intact.
  483. //
  484. UdfAcquireVcbExclusive( IrpContext, Vcb, FALSE );
  485. //
  486. // Unlock the volume. This is benign if we never had seen
  487. // a QUERY.
  488. //
  489. (VOID) UdfUnlockVolumeInternal( IrpContext, Vcb, NULL );
  490. UdfReleaseVcb( IrpContext, Vcb );
  491. //
  492. // Send the request. The underlying driver will complete the
  493. // IRP. Since we don't need to be in the way, simply ellide
  494. // ourselves out of the IRP stack.
  495. //
  496. IoSkipCurrentIrpStackLocation( Irp );
  497. Status = IoCallDriver(Vcb->TargetDeviceObject, Irp);
  498. UdfCompleteRequest( IrpContext, NULL, STATUS_SUCCESS );
  499. return Status;
  500. }
  501. //
  502. // Local support routine
  503. //
  504. NTSTATUS
  505. UdfPnpCompletionRoutine (
  506. IN PDEVICE_OBJECT DeviceObject,
  507. IN PIRP Irp,
  508. IN PVOID Contxt
  509. )
  510. {
  511. PKEVENT Event = (PKEVENT) Contxt;
  512. KeSetEvent( Event, 0, FALSE );
  513. return STATUS_MORE_PROCESSING_REQUIRED;
  514. UNREFERENCED_PARAMETER( DeviceObject );
  515. UNREFERENCED_PARAMETER( Contxt );
  516. }