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.

1085 lines
28 KiB

  1. /*++
  2. Copyright (c) 1989-2000 Microsoft Corporation
  3. Module Name:
  4. CdData.c
  5. Abstract:
  6. This module declares the global data used by the Cdfs file system.
  7. This module also handles the dispath routines in the Fsd threads as well as
  8. handling the IrpContext and Irp through the exception path.
  9. // @@BEGIN_DDKSPLIT
  10. Author:
  11. Brian Andrew [BrianAn] 01-July-1995
  12. Revision History:
  13. // @@END_DDKSPLIT
  14. --*/
  15. #include "CdProcs.h"
  16. #ifdef CD_SANITY
  17. BOOLEAN CdTestTopLevel = TRUE;
  18. BOOLEAN CdTestRaisedStatus = TRUE;
  19. #endif
  20. //
  21. // The Bug check file id for this module
  22. //
  23. #define BugCheckFileId (CDFS_BUG_CHECK_CDDATA)
  24. //
  25. // Global data structures
  26. //
  27. CD_DATA CdData;
  28. FAST_IO_DISPATCH CdFastIoDispatch;
  29. //
  30. // Reserved directory strings.
  31. //
  32. WCHAR CdUnicodeSelfArray[] = { L'.' };
  33. WCHAR CdUnicodeParentArray[] = { L'.', L'.' };
  34. UNICODE_STRING CdUnicodeDirectoryNames[] = {
  35. { 2, 2, CdUnicodeSelfArray},
  36. { 4, 4, CdUnicodeParentArray}
  37. };
  38. //
  39. // Volume descriptor identifier strings.
  40. //
  41. CHAR CdHsgId[] = { 'C', 'D', 'R', 'O', 'M' };
  42. CHAR CdIsoId[] = { 'C', 'D', '0', '0', '1' };
  43. CHAR CdXaId[] = { 'C', 'D', '-', 'X', 'A', '0', '0', '1' };
  44. //
  45. // Volume label for audio disks.
  46. //
  47. WCHAR CdAudioLabel[] = { L'A', L'u', L'd', L'i', L'o', L' ', L'C', L'D' };
  48. USHORT CdAudioLabelLength = sizeof( CdAudioLabel );
  49. //
  50. // Pseudo file names for audio disks.
  51. //
  52. CHAR CdAudioFileName[] = { 'T', 'r', 'a', 'c', 'k', '0', '0', '.', 'c', 'd', 'a' };
  53. UCHAR CdAudioFileNameLength = sizeof( CdAudioFileName );
  54. ULONG CdAudioDirentSize = FIELD_OFFSET( RAW_DIRENT, FileId ) + sizeof( CdAudioFileName ) + sizeof( SYSTEM_USE_XA );
  55. ULONG CdAudioDirentsPerSector = SECTOR_SIZE / (FIELD_OFFSET( RAW_DIRENT, FileId ) + sizeof( CdAudioFileName ) + sizeof( SYSTEM_USE_XA ));
  56. ULONG CdAudioSystemUseOffset = FIELD_OFFSET( RAW_DIRENT, FileId ) + sizeof( CdAudioFileName );
  57. //
  58. // Escape sequences for mounting Unicode volumes.
  59. //
  60. PCHAR CdJolietEscape[] = { "%/@", "%/C", "%/E" };
  61. //
  62. // Audio Play Files consist completely of this header block. These
  63. // files are readable in the root of any audio disc regardless of
  64. // the capabilities of the drive.
  65. //
  66. // The "Unique Disk ID Number" is a calculated value consisting of
  67. // a combination of parameters, including the number of tracks and
  68. // the starting locations of those tracks.
  69. //
  70. // Applications interpreting CDDA RIFF files should be advised that
  71. // additional RIFF file chunks may be added to this header in the
  72. // future in order to add information, such as the disk and song title.
  73. //
  74. LONG CdAudioPlayHeader[] = {
  75. 0x46464952, // Chunk ID = 'RIFF'
  76. 4 * 11 - 8, // Chunk Size = (file size - 8)
  77. 0x41444443, // 'CDDA'
  78. 0x20746d66, // 'fmt '
  79. 24, // Chunk Size (of 'fmt ' subchunk) = 24
  80. 0x00000001, // WORD Format Tag, WORD Track Number
  81. 0x00000000, // DWORD Unique Disk ID Number
  82. 0x00000000, // DWORD Track Starting Sector (LBN)
  83. 0x00000000, // DWORD Track Length (LBN count)
  84. 0x00000000, // DWORD Track Starting Sector (MSF)
  85. 0x00000000 // DWORD Track Length (MSF)
  86. };
  87. // Audio Philes begin with this header block to identify the data as a
  88. // PCM waveform. AudioPhileHeader is coded as if it has no data included
  89. // in the waveform. Data must be added in 2352-byte multiples.
  90. //
  91. // Fields marked 'ADJUST' need to be adjusted based on the size of the
  92. // data: Add (nSectors*2352) to the DWORDs at offsets 1*4 and 10*4.
  93. //
  94. // File Size of TRACK??.WAV = nSectors*2352 + sizeof(AudioPhileHeader)
  95. // RIFF('WAVE' fmt(1, 2, 44100, 176400, 16, 4) data( <CD Audio Raw Data> )
  96. //
  97. // The number of sectors in a CD-XA CD-DA file is (DataLen/2048).
  98. // CDFS will expose these files to applications as if they were just
  99. // 'WAVE' files, adjusting the file size so that the RIFF file is valid.
  100. //
  101. // NT NOTE: We do not do any fidelity adjustment. These are presented as raw
  102. // 2352 byte sectors - 95 has the glimmer of an idea to allow CDFS to expose
  103. // the CDXA CDDA data at different sampling rates in a virtual directory
  104. // structure, but we will never do that.
  105. //
  106. LONG CdXAAudioPhileHeader[] = {
  107. 0x46464952, // Chunk ID = 'RIFF'
  108. -8, // Chunk Size = (file size - 8) ADJUST1
  109. 0x45564157, // 'WAVE'
  110. 0x20746d66, // 'fmt '
  111. 16, // Chunk Size (of 'fmt ' subchunk) = 16
  112. 0x00020001, // WORD Format Tag WORD nChannels
  113. 44100, // DWORD nSamplesPerSecond
  114. 2352 * 75, // DWORD nAvgBytesPerSec
  115. 0x00100004, // WORD nBlockAlign WORD nBitsPerSample
  116. 0x61746164, // 'data'
  117. -44 // <CD Audio Raw Data> ADJUST2
  118. };
  119. //
  120. // XA Files begin with this RIFF header block to identify the data as
  121. // raw CD-XA sectors. Data must be added in 2352-byte multiples.
  122. //
  123. // This header is added to all CD-XA files which are marked as having
  124. // mode2form2 sectors.
  125. //
  126. // Fields marked 'ADJUST' need to be adjusted based on the size of the
  127. // data: Add file size to the marked DWORDS.
  128. //
  129. // File Size of TRACK??.WAV = nSectors*2352 + sizeof(XAFileHeader)
  130. //
  131. // RIFF('CDXA' FMT(Owner, Attr, 'X', 'A', FileNum, 0) data ( <CDXA Raw Data> )
  132. //
  133. LONG CdXAFileHeader[] = {
  134. 0x46464952, // Chunk ID = 'RIFF'
  135. -8, // Chunk Size = (file size - 8) ADJUST
  136. 0x41584443, // 'CDXA'
  137. 0x20746d66, // 'fmt '
  138. 16, // Chunk Size (of CDXA chunk) = 16
  139. 0, // DWORD Owner ID
  140. 0x41580000, // WORD Attributes
  141. // BYTE Signature byte 1 'X'
  142. // BYTE Signature byte 2 'A'
  143. 0, // BYTE File Number
  144. 0, // BYTE Reserved[7]
  145. 0x61746164, // 'data'
  146. -44 // <CD-XA Raw Sectors> ADJUST
  147. };
  148. #ifdef ALLOC_PRAGMA
  149. #pragma alloc_text(PAGE, CdFastIoCheckIfPossible)
  150. #pragma alloc_text(PAGE, CdSerial32)
  151. #endif
  152. NTSTATUS
  153. CdFsdDispatch (
  154. IN PVOLUME_DEVICE_OBJECT VolumeDeviceObject,
  155. IN PIRP Irp
  156. )
  157. /*++
  158. Routine Description:
  159. This is the driver entry to all of the Fsd dispatch points.
  160. Conceptually the Io routine will call this routine on all requests
  161. to the file system. We case on the type of request and invoke the
  162. correct handler for this type of request. There is an exception filter
  163. to catch any exceptions in the CDFS code as well as the CDFS process
  164. exception routine.
  165. This routine allocates and initializes the IrpContext for this request as
  166. well as updating the top-level thread context as necessary. We may loop
  167. in this routine if we need to retry the request for any reason. The
  168. status code STATUS_CANT_WAIT is used to indicate this. Suppose the disk
  169. in the drive has changed. An Fsd request will proceed normally until it
  170. recognizes this condition. STATUS_VERIFY_REQUIRED is raised at that point
  171. and the exception code will handle the verify and either return
  172. STATUS_CANT_WAIT or STATUS_PENDING depending on whether the request was
  173. posted.
  174. Arguments:
  175. VolumeDeviceObject - Supplies the volume device object for this request
  176. Irp - Supplies the Irp being processed
  177. Return Value:
  178. NTSTATUS - The FSD status for the IRP
  179. --*/
  180. {
  181. THREAD_CONTEXT ThreadContext;
  182. PIRP_CONTEXT IrpContext = NULL;
  183. BOOLEAN Wait;
  184. #ifdef CD_SANITY
  185. PVOID PreviousTopLevel;
  186. #endif
  187. NTSTATUS Status;
  188. KIRQL SaveIrql = KeGetCurrentIrql();
  189. ASSERT_OPTIONAL_IRP( Irp );
  190. FsRtlEnterFileSystem();
  191. #ifdef CD_SANITY
  192. PreviousTopLevel = IoGetTopLevelIrp();
  193. #endif
  194. //
  195. // Loop until this request has been completed or posted.
  196. //
  197. do {
  198. //
  199. // Use a try-except to handle the exception cases.
  200. //
  201. try {
  202. //
  203. // If the IrpContext is NULL then this is the first pass through
  204. // this loop.
  205. //
  206. if (IrpContext == NULL) {
  207. //
  208. // Decide if this request is waitable an allocate the IrpContext.
  209. // If the file object in the stack location is NULL then this
  210. // is a mount which is always waitable. Otherwise we look at
  211. // the file object flags.
  212. //
  213. if (IoGetCurrentIrpStackLocation( Irp )->FileObject == NULL) {
  214. Wait = TRUE;
  215. } else {
  216. Wait = CanFsdWait( Irp );
  217. }
  218. IrpContext = CdCreateIrpContext( Irp, Wait );
  219. //
  220. // Update the thread context information.
  221. //
  222. CdSetThreadContext( IrpContext, &ThreadContext );
  223. #ifdef CD_SANITY
  224. ASSERT( !CdTestTopLevel ||
  225. SafeNodeType( IrpContext->TopLevel ) == CDFS_NTC_IRP_CONTEXT );
  226. #endif
  227. //
  228. // Otherwise cleanup the IrpContext for the retry.
  229. //
  230. } else {
  231. //
  232. // Set the MORE_PROCESSING flag to make sure the IrpContext
  233. // isn't inadvertently deleted here. Then cleanup the
  234. // IrpContext to perform the retry.
  235. //
  236. SetFlag( IrpContext->Flags, IRP_CONTEXT_FLAG_MORE_PROCESSING );
  237. CdCleanupIrpContext( IrpContext, FALSE );
  238. }
  239. //
  240. // Case on the major irp code.
  241. //
  242. switch (IrpContext->MajorFunction) {
  243. case IRP_MJ_CREATE :
  244. Status = CdCommonCreate( IrpContext, Irp );
  245. break;
  246. case IRP_MJ_CLOSE :
  247. Status = CdCommonClose( IrpContext, Irp );
  248. break;
  249. case IRP_MJ_READ :
  250. //
  251. // If this is an Mdl complete request, don't go through
  252. // common read.
  253. //
  254. if (FlagOn( IrpContext->MinorFunction, IRP_MN_COMPLETE )) {
  255. Status = CdCompleteMdl( IrpContext, Irp );
  256. } else {
  257. Status = CdCommonRead( IrpContext, Irp );
  258. }
  259. break;
  260. case IRP_MJ_QUERY_INFORMATION :
  261. Status = CdCommonQueryInfo( IrpContext, Irp );
  262. break;
  263. case IRP_MJ_SET_INFORMATION :
  264. Status = CdCommonSetInfo( IrpContext, Irp );
  265. break;
  266. case IRP_MJ_QUERY_VOLUME_INFORMATION :
  267. Status = CdCommonQueryVolInfo( IrpContext, Irp );
  268. break;
  269. case IRP_MJ_DIRECTORY_CONTROL :
  270. Status = CdCommonDirControl( IrpContext, Irp );
  271. break;
  272. case IRP_MJ_FILE_SYSTEM_CONTROL :
  273. Status = CdCommonFsControl( IrpContext, Irp );
  274. break;
  275. case IRP_MJ_DEVICE_CONTROL :
  276. Status = CdCommonDevControl( IrpContext, Irp );
  277. break;
  278. case IRP_MJ_LOCK_CONTROL :
  279. Status = CdCommonLockControl( IrpContext, Irp );
  280. break;
  281. case IRP_MJ_CLEANUP :
  282. Status = CdCommonCleanup( IrpContext, Irp );
  283. break;
  284. case IRP_MJ_PNP :
  285. Status = CdCommonPnp( IrpContext, Irp );
  286. break;
  287. default :
  288. Status = STATUS_INVALID_DEVICE_REQUEST;
  289. CdCompleteRequest( IrpContext, Irp, Status );
  290. }
  291. } except( CdExceptionFilter( IrpContext, GetExceptionInformation() )) {
  292. Status = CdProcessException( IrpContext, Irp, GetExceptionCode() );
  293. }
  294. } while (Status == STATUS_CANT_WAIT);
  295. #ifdef CD_SANITY
  296. ASSERT( !CdTestTopLevel ||
  297. (PreviousTopLevel == IoGetTopLevelIrp()) );
  298. #endif
  299. FsRtlExitFileSystem();
  300. ASSERT( SaveIrql == KeGetCurrentIrql( ));
  301. return Status;
  302. }
  303. LONG
  304. CdExceptionFilter (
  305. IN PIRP_CONTEXT IrpContext,
  306. IN PEXCEPTION_POINTERS ExceptionPointer
  307. )
  308. /*++
  309. Routine Description:
  310. This routine is used to decide whether we will handle a raised exception
  311. status. If CDFS explicitly raised an error then this status is already
  312. in the IrpContext. We choose which is the correct status code and
  313. either indicate that we will handle the exception or bug-check the system.
  314. Arguments:
  315. ExceptionCode - Supplies the exception code to being checked.
  316. Return Value:
  317. ULONG - returns EXCEPTION_EXECUTE_HANDLER or bugchecks
  318. --*/
  319. {
  320. NTSTATUS ExceptionCode;
  321. BOOLEAN TestStatus = TRUE;
  322. ASSERT_OPTIONAL_IRP_CONTEXT( IrpContext );
  323. ExceptionCode = ExceptionPointer->ExceptionRecord->ExceptionCode;
  324. //
  325. // If the exception is STATUS_IN_PAGE_ERROR, get the I/O error code
  326. // from the exception record.
  327. //
  328. if ((ExceptionCode == STATUS_IN_PAGE_ERROR) &&
  329. (ExceptionPointer->ExceptionRecord->NumberParameters >= 3)) {
  330. ExceptionCode =
  331. (NTSTATUS)ExceptionPointer->ExceptionRecord->ExceptionInformation[2];
  332. }
  333. //
  334. // If there is an Irp context then check which status code to use.
  335. //
  336. if (ARGUMENT_PRESENT( IrpContext )) {
  337. if (IrpContext->ExceptionStatus == STATUS_SUCCESS) {
  338. //
  339. // Store the real status into the IrpContext.
  340. //
  341. IrpContext->ExceptionStatus = ExceptionCode;
  342. } else {
  343. //
  344. // No need to test the status code if we raised it ourselves.
  345. //
  346. TestStatus = FALSE;
  347. }
  348. }
  349. AssertVerifyDevice( IrpContext, IrpContext->ExceptionStatus );
  350. //
  351. // Bug check if this status is not supported.
  352. //
  353. if (TestStatus && !FsRtlIsNtstatusExpected( ExceptionCode )) {
  354. CdBugCheck( (ULONG_PTR) ExceptionPointer->ExceptionRecord,
  355. (ULONG_PTR) ExceptionPointer->ContextRecord,
  356. (ULONG_PTR) ExceptionPointer->ExceptionRecord->ExceptionAddress );
  357. }
  358. return EXCEPTION_EXECUTE_HANDLER;
  359. }
  360. NTSTATUS
  361. CdProcessException (
  362. IN PIRP_CONTEXT IrpContext OPTIONAL,
  363. IN PIRP Irp,
  364. IN NTSTATUS ExceptionCode
  365. )
  366. /*++
  367. Routine Description:
  368. This routine processes an exception. It either completes the request
  369. with the exception status in the IrpContext, sends this off to the Fsp
  370. workque or causes it to be retried in the current thread if a verification
  371. is needed.
  372. If the volume needs to be verified (STATUS_VERIFY_REQUIRED) and we can
  373. do the work in the current thread we will translate the status code
  374. to STATUS_CANT_WAIT to indicate that we need to retry the request.
  375. Arguments:
  376. Irp - Supplies the Irp being processed
  377. ExceptionCode - Supplies the normalized exception status being handled
  378. Return Value:
  379. NTSTATUS - Returns the results of either posting the Irp or the
  380. saved completion status.
  381. --*/
  382. {
  383. PDEVICE_OBJECT Device;
  384. PVPB Vpb;
  385. PETHREAD Thread;
  386. ASSERT_OPTIONAL_IRP_CONTEXT( IrpContext );
  387. ASSERT_IRP( Irp );
  388. //
  389. // If there is not an irp context, then complete the request with the
  390. // current status code.
  391. //
  392. if (!ARGUMENT_PRESENT( IrpContext )) {
  393. CdCompleteRequest( NULL, Irp, ExceptionCode );
  394. return ExceptionCode;
  395. }
  396. //
  397. // Get the real exception status from the IrpContext.
  398. //
  399. ExceptionCode = IrpContext->ExceptionStatus;
  400. //
  401. // If we are not a top level request then we just complete the request
  402. // with the current status code.
  403. //
  404. if (!FlagOn( IrpContext->Flags, IRP_CONTEXT_FLAG_TOP_LEVEL )) {
  405. CdCompleteRequest( IrpContext, Irp, ExceptionCode );
  406. return ExceptionCode;
  407. }
  408. //
  409. // Check if we are posting this request. One of the following must be true
  410. // if we are to post a request.
  411. //
  412. // - Status code is STATUS_CANT_WAIT and the request is asynchronous
  413. // or we are forcing this to be posted.
  414. //
  415. // - Status code is STATUS_VERIFY_REQUIRED and we are at APC level
  416. // or higher. Can't wait for IO in the verify path in this case.
  417. //
  418. // Set the MORE_PROCESSING flag in the IrpContext to keep if from being
  419. // deleted if this is a retryable condition.
  420. //
  421. //
  422. // Note that (children of) CdFsdPostRequest can raise (Mdl allocation).
  423. //
  424. try {
  425. if (ExceptionCode == STATUS_CANT_WAIT) {
  426. if (FlagOn( IrpContext->Flags, IRP_CONTEXT_FLAG_FORCE_POST )) {
  427. ExceptionCode = CdFsdPostRequest( IrpContext, Irp );
  428. }
  429. } else if (ExceptionCode == STATUS_VERIFY_REQUIRED) {
  430. if (KeGetCurrentIrql() >= APC_LEVEL) {
  431. ExceptionCode = CdFsdPostRequest( IrpContext, Irp );
  432. }
  433. }
  434. }
  435. except( CdExceptionFilter( IrpContext, GetExceptionInformation() )) {
  436. ExceptionCode = GetExceptionCode();
  437. }
  438. //
  439. // If we posted the request or our caller will retry then just return here.
  440. //
  441. if ((ExceptionCode == STATUS_PENDING) ||
  442. (ExceptionCode == STATUS_CANT_WAIT)) {
  443. return ExceptionCode;
  444. }
  445. ClearFlag( IrpContext->Flags, IRP_CONTEXT_FLAG_MORE_PROCESSING );
  446. //
  447. // Store this error into the Irp for posting back to the Io system.
  448. //
  449. Irp->IoStatus.Status = ExceptionCode;
  450. if (IoIsErrorUserInduced( ExceptionCode )) {
  451. //
  452. // Check for the various error conditions that can be caused by,
  453. // and possibly resolved my the user.
  454. //
  455. if (ExceptionCode == STATUS_VERIFY_REQUIRED) {
  456. //
  457. // Now we are at the top level file system entry point.
  458. //
  459. // If we have already posted this request then the device to
  460. // verify is in the original thread. Find this via the Irp.
  461. //
  462. Device = IoGetDeviceToVerify( Irp->Tail.Overlay.Thread );
  463. IoSetDeviceToVerify( Irp->Tail.Overlay.Thread, NULL );
  464. //
  465. // If there is no device in that location then check in the
  466. // current thread.
  467. //
  468. if (Device == NULL) {
  469. Device = IoGetDeviceToVerify( PsGetCurrentThread() );
  470. IoSetDeviceToVerify( PsGetCurrentThread(), NULL );
  471. ASSERT( Device != NULL );
  472. //
  473. // Let's not BugCheck just because the driver messes up.
  474. //
  475. if (Device == NULL) {
  476. ExceptionCode = STATUS_DRIVER_INTERNAL_ERROR;
  477. CdCompleteRequest( IrpContext, Irp, ExceptionCode );
  478. return ExceptionCode;
  479. }
  480. }
  481. //
  482. // CdPerformVerify() will do the right thing with the Irp.
  483. // If we return STATUS_CANT_WAIT then the current thread
  484. // can retry the request.
  485. //
  486. return CdPerformVerify( IrpContext, Irp, Device );
  487. }
  488. //
  489. // The other user induced conditions generate an error unless
  490. // they have been disabled for this request.
  491. //
  492. if (FlagOn( IrpContext->Flags, IRP_CONTEXT_FLAG_DISABLE_POPUPS )) {
  493. CdCompleteRequest( IrpContext, Irp, ExceptionCode );
  494. return ExceptionCode;
  495. }
  496. //
  497. // Generate a pop-up.
  498. //
  499. else {
  500. if (IoGetCurrentIrpStackLocation( Irp )->FileObject != NULL) {
  501. Vpb = IoGetCurrentIrpStackLocation( Irp )->FileObject->Vpb;
  502. } else {
  503. Vpb = NULL;
  504. }
  505. //
  506. // The device to verify is either in my thread local storage
  507. // or that of the thread that owns the Irp.
  508. //
  509. Thread = Irp->Tail.Overlay.Thread;
  510. Device = IoGetDeviceToVerify( Thread );
  511. if (Device == NULL) {
  512. Thread = PsGetCurrentThread();
  513. Device = IoGetDeviceToVerify( Thread );
  514. ASSERT( Device != NULL );
  515. //
  516. // Let's not BugCheck just because the driver messes up.
  517. //
  518. if (Device == NULL) {
  519. CdCompleteRequest( IrpContext, Irp, ExceptionCode );
  520. return ExceptionCode;
  521. }
  522. }
  523. //
  524. // This routine actually causes the pop-up. It usually
  525. // does this by queuing an APC to the callers thread,
  526. // but in some cases it will complete the request immediately,
  527. // so it is very important to IoMarkIrpPending() first.
  528. //
  529. IoMarkIrpPending( Irp );
  530. IoRaiseHardError( Irp, Vpb, Device );
  531. //
  532. // We will be handing control back to the caller here, so
  533. // reset the saved device object.
  534. //
  535. IoSetDeviceToVerify( Thread, NULL );
  536. //
  537. // The Irp will be completed by Io or resubmitted. In either
  538. // case we must clean up the IrpContext here.
  539. //
  540. CdCompleteRequest( IrpContext, NULL, STATUS_SUCCESS );
  541. return STATUS_PENDING;
  542. }
  543. }
  544. //
  545. // This is just a run of the mill error.
  546. //
  547. CdCompleteRequest( IrpContext, Irp, ExceptionCode );
  548. return ExceptionCode;
  549. }
  550. VOID
  551. CdCompleteRequest (
  552. IN PIRP_CONTEXT IrpContext OPTIONAL,
  553. IN PIRP Irp OPTIONAL,
  554. IN NTSTATUS Status
  555. )
  556. /*++
  557. Routine Description:
  558. This routine completes a Irp and cleans up the IrpContext. Either or
  559. both of these may not be specified.
  560. Arguments:
  561. Irp - Supplies the Irp being processed.
  562. Status - Supplies the status to complete the Irp with
  563. Return Value:
  564. None.
  565. --*/
  566. {
  567. ASSERT_OPTIONAL_IRP_CONTEXT( IrpContext );
  568. ASSERT_OPTIONAL_IRP( Irp );
  569. //
  570. // Cleanup the IrpContext if passed in here.
  571. //
  572. if (ARGUMENT_PRESENT( IrpContext )) {
  573. CdCleanupIrpContext( IrpContext, FALSE );
  574. }
  575. //
  576. // If we have an Irp then complete the irp.
  577. //
  578. if (ARGUMENT_PRESENT( Irp )) {
  579. //
  580. // Clear the information field in case we have used this Irp
  581. // internally.
  582. //
  583. if (NT_ERROR( Status ) &&
  584. FlagOn( Irp->Flags, IRP_INPUT_OPERATION )) {
  585. Irp->IoStatus.Information = 0;
  586. }
  587. Irp->IoStatus.Status = Status;
  588. AssertVerifyDeviceIrp( Irp );
  589. IoCompleteRequest( Irp, IO_CD_ROM_INCREMENT );
  590. }
  591. return;
  592. }
  593. VOID
  594. CdSetThreadContext (
  595. IN PIRP_CONTEXT IrpContext,
  596. IN PTHREAD_CONTEXT ThreadContext
  597. )
  598. /*++
  599. Routine Description:
  600. This routine is called at each Fsd/Fsp entry point set up the IrpContext
  601. and thread local storage to track top level requests. If there is
  602. not a Cdfs context in the thread local storage then we use the input one.
  603. Otherwise we use the one already there. This routine also updates the
  604. IrpContext based on the state of the top-level context.
  605. If the TOP_LEVEL flag in the IrpContext is already set when we are called
  606. then we force this request to appear top level.
  607. Arguments:
  608. ThreadContext - Address on stack for local storage if not already present.
  609. ForceTopLevel - We force this request to appear top level regardless of
  610. any previous stack value.
  611. Return Value:
  612. None
  613. --*/
  614. {
  615. PTHREAD_CONTEXT CurrentThreadContext;
  616. ULONG_PTR StackTop;
  617. ULONG_PTR StackBottom;
  618. PAGED_CODE();
  619. ASSERT_IRP_CONTEXT( IrpContext );
  620. //
  621. // Get the current top-level irp out of the thread storage.
  622. // If NULL then this is the top-level request.
  623. //
  624. CurrentThreadContext = (PTHREAD_CONTEXT) IoGetTopLevelIrp();
  625. if (CurrentThreadContext == NULL) {
  626. SetFlag( IrpContext->Flags, IRP_CONTEXT_FLAG_TOP_LEVEL );
  627. }
  628. //
  629. // Initialize the input context unless we are using the current
  630. // thread context block. We use the new block if our caller
  631. // specified this or the existing block is invalid.
  632. //
  633. // The following must be true for the current to be a valid Cdfs context.
  634. //
  635. // Structure must lie within current stack.
  636. // Address must be ULONG aligned.
  637. // Cdfs signature must be present.
  638. //
  639. // If this is not a valid Cdfs context then use the input thread
  640. // context and store it in the top level context.
  641. //
  642. IoGetStackLimits( &StackTop, &StackBottom);
  643. if (FlagOn( IrpContext->Flags, IRP_CONTEXT_FLAG_TOP_LEVEL ) ||
  644. (((ULONG_PTR) CurrentThreadContext > StackBottom - sizeof( THREAD_CONTEXT )) ||
  645. ((ULONG_PTR) CurrentThreadContext <= StackTop) ||
  646. FlagOn( (ULONG_PTR) CurrentThreadContext, 0x3 ) ||
  647. (CurrentThreadContext->Cdfs != 0x53464443))) {
  648. ThreadContext->Cdfs = 0x53464443;
  649. ThreadContext->SavedTopLevelIrp = (PIRP) CurrentThreadContext;
  650. ThreadContext->TopLevelIrpContext = IrpContext;
  651. IoSetTopLevelIrp( (PIRP) ThreadContext );
  652. IrpContext->TopLevel = IrpContext;
  653. IrpContext->ThreadContext = ThreadContext;
  654. SetFlag( IrpContext->Flags, IRP_CONTEXT_FLAG_TOP_LEVEL_CDFS );
  655. //
  656. // Otherwise use the IrpContext in the thread context.
  657. //
  658. } else {
  659. IrpContext->TopLevel = CurrentThreadContext->TopLevelIrpContext;
  660. }
  661. return;
  662. }
  663. BOOLEAN
  664. CdFastIoCheckIfPossible (
  665. IN PFILE_OBJECT FileObject,
  666. IN PLARGE_INTEGER FileOffset,
  667. IN ULONG Length,
  668. IN BOOLEAN Wait,
  669. IN ULONG LockKey,
  670. IN BOOLEAN CheckForReadOperation,
  671. OUT PIO_STATUS_BLOCK IoStatus,
  672. IN PDEVICE_OBJECT DeviceObject
  673. )
  674. /*++
  675. Routine Description:
  676. This routine checks if fast i/o is possible for a read/write operation
  677. Arguments:
  678. FileObject - Supplies the file object used in the query
  679. FileOffset - Supplies the starting byte offset for the read/write operation
  680. Length - Supplies the length, in bytes, of the read/write operation
  681. Wait - Indicates if we can wait
  682. LockKey - Supplies the lock key
  683. CheckForReadOperation - Indicates if this is a check for a read or write
  684. operation
  685. IoStatus - Receives the status of the operation if our return value is
  686. FastIoReturnError
  687. Return Value:
  688. BOOLEAN - TRUE if fast I/O is possible and FALSE if the caller needs
  689. to take the long route.
  690. --*/
  691. {
  692. PFCB Fcb;
  693. TYPE_OF_OPEN TypeOfOpen;
  694. LARGE_INTEGER LargeLength;
  695. PAGED_CODE();
  696. //
  697. // Decode the type of file object we're being asked to process and
  698. // make sure that is is only a user file open.
  699. //
  700. TypeOfOpen = CdFastDecodeFileObject( FileObject, &Fcb );
  701. if ((TypeOfOpen != UserFileOpen) || !CheckForReadOperation) {
  702. IoStatus->Status = STATUS_INVALID_PARAMETER;
  703. return TRUE;
  704. }
  705. LargeLength.QuadPart = Length;
  706. //
  707. // Check whether the file locks will allow for fast io.
  708. //
  709. if ((Fcb->FileLock == NULL) ||
  710. FsRtlFastCheckLockForRead( Fcb->FileLock,
  711. FileOffset,
  712. &LargeLength,
  713. LockKey,
  714. FileObject,
  715. PsGetCurrentProcess() )) {
  716. return TRUE;
  717. }
  718. return FALSE;
  719. }
  720. ULONG
  721. CdSerial32 (
  722. IN PCHAR Buffer,
  723. IN ULONG ByteCount
  724. )
  725. /*++
  726. Routine Description:
  727. This routine is called to generate a 32 bit serial number. This is
  728. done by doing four separate checksums into an array of bytes and
  729. then treating the bytes as a ULONG.
  730. Arguments:
  731. Buffer - Pointer to the buffer to generate the ID for.
  732. ByteCount - Number of bytes in the buffer.
  733. Return Value:
  734. ULONG - The 32 bit serial number.
  735. --*/
  736. {
  737. union {
  738. UCHAR Bytes[4];
  739. ULONG SerialId;
  740. } Checksum;
  741. PAGED_CODE();
  742. //
  743. // Initialize the serial number.
  744. //
  745. Checksum.SerialId = 0;
  746. //
  747. // Continue while there are more bytes to use.
  748. //
  749. while (ByteCount--) {
  750. //
  751. // Increment this sub-checksum.
  752. //
  753. Checksum.Bytes[ByteCount & 0x3] += *(Buffer++);
  754. }
  755. //
  756. // Return the checksums as a ULONG.
  757. //
  758. return Checksum.SerialId;
  759. }