Leaked source code of windows server 2003
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.

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