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.

1466 lines
36 KiB

  1. /*++
  2. Copyright (c) 1989-2000 Microsoft Corporation
  3. Module Name:
  4. UdfData.c
  5. Abstract:
  6. This module declares the global data used by the Udfs 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. Dan Lovinger [DanLo] 24-May-1996
  12. Revision History:
  13. Tom Jolly [TomJolly] 1-March-2000 UDF 2.01 support
  14. // @@END_DDKSPLIT
  15. --*/
  16. #include "UdfProcs.h"
  17. //
  18. // The Bug check file id for this module
  19. //
  20. #define BugCheckFileId (UDFS_BUG_CHECK_UDFDATA)
  21. //
  22. // The local debug trace level
  23. //
  24. #define Dbg (UDFS_DEBUG_LEVEL_UDFDATA)
  25. //
  26. // Global data structures
  27. //
  28. UDF_DATA UdfData;
  29. FAST_IO_DISPATCH UdfFastIoDispatch;
  30. //
  31. // Debug trace levels
  32. //
  33. #ifdef UDF_SANITY
  34. //
  35. // For UdfDebugTrace (only live in checked builds) to be able to swing
  36. // variable argument lists and varargs printfs.
  37. //
  38. #include <stdarg.h>
  39. #include <stdio.h>
  40. BOOLEAN UdfTestTopLevel = TRUE;
  41. BOOLEAN UdfTestRaisedStatus = TRUE;
  42. LONG UdfDebugTraceLevel = 0;
  43. LONG UdfDebugTraceIndent = 0;
  44. //
  45. // Control whether UdfVerifyDescriptor will only emit info on failure (FALSE) or
  46. // all of the time (TRUE).
  47. //
  48. BOOLEAN UdfNoisyVerifyDescriptor = FALSE;
  49. #endif
  50. //
  51. // This is the time reported for any files whose FE is corrupt during
  52. // directory enumeration.
  53. //
  54. LARGE_INTEGER UdfCorruptFileTime;
  55. //
  56. // Reserved directory strings.
  57. //
  58. WCHAR UdfUnicodeSelfArray[] = { L'.' };
  59. WCHAR UdfUnicodeParentArray[] = { L'.', L'.' };
  60. UNICODE_STRING UdfUnicodeDirectoryNames[] = {
  61. { sizeof(UdfUnicodeSelfArray), sizeof(UdfUnicodeSelfArray), UdfUnicodeSelfArray},
  62. { sizeof(UdfUnicodeParentArray), sizeof(UdfUnicodeParentArray), UdfUnicodeParentArray}
  63. };
  64. //
  65. // Identifier strings defined by UDF.
  66. //
  67. CHAR UdfCS0IdentifierArray[] = { 'O', 'S', 'T', 'A', ' ',
  68. 'C', 'o', 'm', 'p', 'r', 'e', 's', 's', 'e', 'd', ' ',
  69. 'U', 'n', 'i', 'c', 'o', 'd', 'e' };
  70. STRING UdfCS0Identifier = {
  71. sizeof(UdfCS0IdentifierArray),
  72. sizeof(UdfCS0IdentifierArray),
  73. UdfCS0IdentifierArray
  74. };
  75. CHAR UdfDomainIdentifierArray[] = { '*', 'O', 'S', 'T', 'A', ' ',
  76. 'U', 'D', 'F', ' ',
  77. 'C', 'o', 'm', 'p', 'l', 'i', 'a', 'n', 't' };
  78. STRING UdfDomainIdentifier = {
  79. sizeof(UdfDomainIdentifierArray),
  80. sizeof(UdfDomainIdentifierArray),
  81. UdfDomainIdentifierArray
  82. };
  83. CHAR UdfVirtualPartitionDomainIdentifierArray[] = { '*', 'U', 'D', 'F', ' ',
  84. 'V', 'i', 'r', 't', 'u', 'a', 'l', ' ',
  85. 'P', 'a', 'r', 't', 'i', 't', 'i', 'o', 'n' };
  86. STRING UdfVirtualPartitionDomainIdentifier = {
  87. sizeof(UdfVirtualPartitionDomainIdentifierArray),
  88. sizeof(UdfVirtualPartitionDomainIdentifierArray),
  89. UdfVirtualPartitionDomainIdentifierArray
  90. };
  91. CHAR UdfVatTableIdentifierArray[] = { '*', 'U', 'D', 'F', ' ',
  92. 'V', 'i', 'r', 't', 'u', 'a', 'l', ' ',
  93. 'A', 'l', 'l', 'o', 'c', ' ',
  94. 'T', 'b', 'l' };
  95. STRING UdfVatTableIdentifier = {
  96. sizeof(UdfVatTableIdentifierArray),
  97. sizeof(UdfVatTableIdentifierArray),
  98. UdfVatTableIdentifierArray
  99. };
  100. CHAR UdfSparablePartitionDomainIdentifierArray[] = { '*', 'U', 'D', 'F', ' ',
  101. 'S', 'p', 'a', 'r', 'a', 'b', 'l', 'e', ' ',
  102. 'P', 'a', 'r', 't', 'i', 't', 'i', 'o', 'n' };
  103. STRING UdfSparablePartitionDomainIdentifier = {
  104. sizeof(UdfSparablePartitionDomainIdentifierArray),
  105. sizeof(UdfSparablePartitionDomainIdentifierArray),
  106. UdfSparablePartitionDomainIdentifierArray
  107. };
  108. CHAR UdfSparingTableIdentifierArray[] = { '*', 'U', 'D', 'F', ' ',
  109. 'S', 'p', 'a', 'r', 'i', 'n', 'g', ' ',
  110. 'T', 'a', 'b', 'l', 'e' };
  111. STRING UdfSparingTableIdentifier = {
  112. sizeof(UdfSparingTableIdentifierArray),
  113. sizeof(UdfSparingTableIdentifierArray),
  114. UdfSparingTableIdentifierArray
  115. };
  116. CHAR UdfNSR02IdentifierArray[] = NSR_PART_CONTID_NSR02;
  117. STRING UdfNSR02Identifier = {
  118. sizeof(UdfNSR02IdentifierArray),
  119. sizeof(UdfNSR02IdentifierArray),
  120. UdfNSR02IdentifierArray
  121. };
  122. CHAR UdfNSR03IdentifierArray[] = NSR_PART_CONTID_NSR03;
  123. STRING UdfNSR03Identifier = {
  124. sizeof(UdfNSR03IdentifierArray),
  125. sizeof(UdfNSR03IdentifierArray),
  126. UdfNSR03IdentifierArray
  127. };
  128. //
  129. // Tables of tokens we have to parse up from mount-time on-disk structures
  130. //
  131. PARSE_KEYVALUE VsdIdentParseTable[] = {
  132. { VSD_IDENT_BEA01, VsdIdentBEA01 },
  133. { VSD_IDENT_TEA01, VsdIdentTEA01 },
  134. { VSD_IDENT_CDROM, VsdIdentCDROM },
  135. { VSD_IDENT_CD001, VsdIdentCD001 },
  136. { VSD_IDENT_CDW01, VsdIdentCDW01 },
  137. { VSD_IDENT_CDW02, VsdIdentCDW02 },
  138. { VSD_IDENT_NSR01, VsdIdentNSR01 },
  139. { VSD_IDENT_NSR02, VsdIdentNSR02 },
  140. { VSD_IDENT_BOOT2, VsdIdentBOOT2 },
  141. { VSD_IDENT_NSR03, VsdIdentNSR03 },
  142. { NULL, VsdIdentBad }
  143. };
  144. PARSE_KEYVALUE NsrPartContIdParseTable[] = {
  145. { NSR_PART_CONTID_FDC01, NsrPartContIdFDC01 },
  146. { NSR_PART_CONTID_CD001, NsrPartContIdCD001 },
  147. { NSR_PART_CONTID_CDW01, NsrPartContIdCDW01 },
  148. { NSR_PART_CONTID_CDW02, NsrPartContIdCDW02 },
  149. { NSR_PART_CONTID_NSR01, NsrPartContIdNSR01 },
  150. { NSR_PART_CONTID_NSR02, NsrPartContIdNSR02 },
  151. { NSR_PART_CONTID_NSR03, NsrPartContIdNSR03 },
  152. { NULL, NsrPartContIdBad }
  153. };
  154. //
  155. // Lookaside allocation lists for various volatile structures
  156. //
  157. NPAGED_LOOKASIDE_LIST UdfFcbNonPagedLookasideList;
  158. NPAGED_LOOKASIDE_LIST UdfIrpContextLookasideList;
  159. PAGED_LOOKASIDE_LIST UdfCcbLookasideList;
  160. PAGED_LOOKASIDE_LIST UdfFcbIndexLookasideList;
  161. PAGED_LOOKASIDE_LIST UdfFcbDataLookasideList;
  162. PAGED_LOOKASIDE_LIST UdfLcbLookasideList;
  163. //
  164. // 16bit CRC table
  165. //
  166. PUSHORT UdfCrcTable;
  167. #ifdef ALLOC_PRAGMA
  168. #pragma alloc_text(PAGE, UdfComputeCrc16)
  169. #pragma alloc_text(PAGE, UdfComputeCrc16Uni)
  170. #ifdef UDF_SANITY
  171. #pragma alloc_text(PAGE, UdfDebugTrace)
  172. #endif
  173. #pragma alloc_text(PAGE, UdfFastIoCheckIfPossible)
  174. #pragma alloc_text(PAGE, UdfHighBit)
  175. #pragma alloc_text(PAGE, UdfInitializeCrc16)
  176. #pragma alloc_text(PAGE, UdfSerial32)
  177. #endif
  178. NTSTATUS
  179. UdfFsdDispatch (
  180. IN PVOLUME_DEVICE_OBJECT VolumeDeviceObject,
  181. IN PIRP Irp
  182. )
  183. /*++
  184. Routine Description:
  185. This is the driver entry to all of the Fsd dispatch points.
  186. Conceptually the Io routine will call this routine on all requests
  187. to the file system. We case on the type of request and invoke the
  188. correct handler for this type of request. There is an exception filter
  189. to catch any exceptions in the UDFS code as well as the UDFS process
  190. exception routine.
  191. This routine allocates and initializes the IrpContext for this request as
  192. well as updating the top-level thread context as necessary. We may loop
  193. in this routine if we need to retry the request for any reason. The
  194. status code STATUS_CANT_WAIT is used to indicate this. Suppose the disk
  195. in the drive has changed. An Fsd request will proceed normally until it
  196. recognizes this condition. STATUS_VERIFY_REQUIRED is raised at that point
  197. and the exception code will handle the verify and either return
  198. STATUS_CANT_WAIT or STATUS_PENDING depending on whether the request was
  199. posted.
  200. Arguments:
  201. VolumeDeviceObject - Supplies the volume device object for this request
  202. Irp - Supplies the Irp being processed
  203. Return Value:
  204. NTSTATUS - The FSD status for the IRP
  205. --*/
  206. {
  207. THREAD_CONTEXT ThreadContext;
  208. PIRP_CONTEXT IrpContext = NULL;
  209. BOOLEAN Wait;
  210. #ifdef UDF_SANITY
  211. PVOID PreviousTopLevel;
  212. #endif
  213. NTSTATUS Status;
  214. KIRQL SaveIrql = KeGetCurrentIrql();
  215. ASSERT_OPTIONAL_IRP( Irp );
  216. FsRtlEnterFileSystem();
  217. #ifdef UDF_SANITY
  218. PreviousTopLevel = IoGetTopLevelIrp();
  219. #endif
  220. //
  221. // Loop until this request has been completed or posted.
  222. //
  223. do {
  224. //
  225. // Use a try-except to handle the exception cases.
  226. //
  227. try {
  228. //
  229. // If the IrpContext is NULL then this is the first pass through
  230. // this loop.
  231. //
  232. if (IrpContext == NULL) {
  233. //
  234. // Decide if this request is waitable an allocate the IrpContext.
  235. // If the file object in the stack location is NULL then this
  236. // is a mount which is always waitable. Otherwise we look at
  237. // the file object flags.
  238. //
  239. if (IoGetCurrentIrpStackLocation( Irp )->FileObject == NULL) {
  240. Wait = TRUE;
  241. } else {
  242. Wait = CanFsdWait( Irp );
  243. }
  244. IrpContext = UdfCreateIrpContext( Irp, Wait );
  245. //
  246. // Update the thread context information.
  247. //
  248. UdfSetThreadContext( IrpContext, &ThreadContext );
  249. #ifdef UDF_SANITY
  250. ASSERT( !UdfTestTopLevel ||
  251. SafeNodeType( IrpContext->TopLevel ) == UDFS_NTC_IRP_CONTEXT );
  252. #endif
  253. //
  254. // Otherwise cleanup the IrpContext for the retry.
  255. //
  256. } else {
  257. //
  258. // Set the MORE_PROCESSING flag to make sure the IrpContext
  259. // isn't inadvertently deleted here. Then cleanup the
  260. // IrpContext to perform the retry.
  261. //
  262. SetFlag( IrpContext->Flags, IRP_CONTEXT_FLAG_MORE_PROCESSING );
  263. UdfCleanupIrpContext( IrpContext, FALSE );
  264. }
  265. //
  266. // Case on the major irp code.
  267. //
  268. switch (IrpContext->MajorFunction) {
  269. case IRP_MJ_CLEANUP :
  270. Status = UdfCommonCleanup( IrpContext, Irp );
  271. break;
  272. case IRP_MJ_CLOSE :
  273. Status = UdfCommonClose( IrpContext, Irp );
  274. break;
  275. case IRP_MJ_CREATE :
  276. Status = UdfCommonCreate( IrpContext, Irp );
  277. break;
  278. case IRP_MJ_DEVICE_CONTROL :
  279. Status = UdfCommonDevControl( IrpContext, Irp );
  280. break;
  281. case IRP_MJ_DIRECTORY_CONTROL :
  282. Status = UdfCommonDirControl( IrpContext, Irp );
  283. break;
  284. case IRP_MJ_FILE_SYSTEM_CONTROL :
  285. Status = UdfCommonFsControl( IrpContext, Irp );
  286. break;
  287. case IRP_MJ_LOCK_CONTROL :
  288. Status = UdfCommonLockControl( IrpContext, Irp );
  289. break;
  290. case IRP_MJ_PNP :
  291. Status = UdfCommonPnp( IrpContext, Irp );
  292. break;
  293. case IRP_MJ_QUERY_INFORMATION :
  294. Status = UdfCommonQueryInfo( IrpContext, Irp );
  295. break;
  296. case IRP_MJ_QUERY_VOLUME_INFORMATION :
  297. Status = UdfCommonQueryVolInfo( IrpContext, Irp );
  298. break;
  299. case IRP_MJ_READ :
  300. //
  301. // If this is an Mdl complete request, don't go through
  302. // common read.
  303. //
  304. if (FlagOn( IrpContext->MinorFunction, IRP_MN_COMPLETE )) {
  305. Status = UdfCompleteMdl( IrpContext, Irp );
  306. } else {
  307. Status = UdfCommonRead( IrpContext, Irp );
  308. }
  309. break;
  310. case IRP_MJ_WRITE :
  311. Status = UdfCommonWrite( IrpContext, Irp );
  312. break;
  313. case IRP_MJ_SET_INFORMATION :
  314. Status = UdfCommonSetInfo( IrpContext, Irp );
  315. break;
  316. default :
  317. Status = STATUS_INVALID_DEVICE_REQUEST;
  318. UdfCompleteRequest( IrpContext, Irp, Status );
  319. }
  320. } except( UdfExceptionFilter( IrpContext, GetExceptionInformation() )) {
  321. Status = UdfProcessException( IrpContext, Irp, GetExceptionCode() );
  322. }
  323. } while (Status == STATUS_CANT_WAIT);
  324. #ifdef UDF_SANITY
  325. ASSERT( !UdfTestTopLevel ||
  326. (PreviousTopLevel == IoGetTopLevelIrp()) );
  327. #endif
  328. FsRtlExitFileSystem();
  329. ASSERT( SaveIrql == KeGetCurrentIrql( ));
  330. return Status;
  331. }
  332. LONG
  333. UdfExceptionFilter (
  334. IN PIRP_CONTEXT IrpContext,
  335. IN PEXCEPTION_POINTERS ExceptionPointer
  336. )
  337. /*++
  338. Routine Description:
  339. This routine is used to decide whether we will handle a raised exception
  340. status. If UDFS explicitly raised an error then this status is already
  341. in the IrpContext. We choose which is the correct status code and
  342. either indicate that we will handle the exception or bug-check the system.
  343. Arguments:
  344. ExceptionCode - Supplies the exception code to being checked.
  345. Return Value:
  346. ULONG - returns EXCEPTION_EXECUTE_HANDLER or bugchecks
  347. --*/
  348. {
  349. NTSTATUS ExceptionCode;
  350. BOOLEAN TestStatus = TRUE;
  351. ASSERT_OPTIONAL_IRP_CONTEXT( IrpContext );
  352. ExceptionCode = ExceptionPointer->ExceptionRecord->ExceptionCode;
  353. DebugTrace(( 0, Dbg,
  354. "UdfExceptionFilter: %08x (exr %08x cxr %08x)\n",
  355. ExceptionCode,
  356. ExceptionPointer->ExceptionRecord,
  357. ExceptionPointer->ContextRecord ));
  358. //
  359. // If the exception is STATUS_IN_PAGE_ERROR, get the I/O error code
  360. // from the exception record.
  361. //
  362. if ((ExceptionCode == STATUS_IN_PAGE_ERROR) &&
  363. (ExceptionPointer->ExceptionRecord->NumberParameters >= 3)) {
  364. ExceptionCode = (NTSTATUS) ExceptionPointer->ExceptionRecord->ExceptionInformation[2];
  365. }
  366. //
  367. // If there is an Irp context then check which status code to use.
  368. //
  369. if (ARGUMENT_PRESENT( IrpContext )) {
  370. if (IrpContext->ExceptionStatus == STATUS_SUCCESS) {
  371. //
  372. // Store the real status into the IrpContext.
  373. //
  374. IrpContext->ExceptionStatus = ExceptionCode;
  375. } else {
  376. //
  377. // No need to test the status code if we raised it ourselves.
  378. //
  379. TestStatus = FALSE;
  380. }
  381. }
  382. //
  383. // Bug check if this status is not supported.
  384. //
  385. if (TestStatus && !FsRtlIsNtstatusExpected( ExceptionCode )) {
  386. UdfBugCheck( (ULONG_PTR) ExceptionPointer->ExceptionRecord,
  387. (ULONG_PTR) ExceptionPointer->ContextRecord,
  388. (ULONG_PTR) ExceptionPointer->ExceptionRecord->ExceptionAddress );
  389. }
  390. return EXCEPTION_EXECUTE_HANDLER;
  391. }
  392. LONG
  393. UdfQueryDirExceptionFilter(
  394. IN PEXCEPTION_POINTERS ExceptionPointers
  395. )
  396. /*++
  397. Routine Description:
  398. This routine is used to decide whether we will handle a raised exception
  399. while opening FES during directory traversal.
  400. Arguments:
  401. ExceptionCode - Supplies the exception code to being checked.
  402. Return Value:
  403. ULONG - returns EXCEPTION_EXECUTE_HANDLER or EXCEPTION_CONTINUE_SEARCH
  404. --*/
  405. {
  406. NTSTATUS ExceptionCode = ExceptionPointers->ExceptionRecord->ExceptionCode;
  407. if ((ExceptionCode == STATUS_FILE_CORRUPT_ERROR) ||
  408. (ExceptionCode == STATUS_CRC_ERROR)) {
  409. return EXCEPTION_EXECUTE_HANDLER;
  410. }
  411. return EXCEPTION_CONTINUE_SEARCH;
  412. }
  413. NTSTATUS
  414. UdfProcessException (
  415. IN PIRP_CONTEXT IrpContext OPTIONAL,
  416. IN PIRP Irp,
  417. IN NTSTATUS ExceptionCode
  418. )
  419. /*++
  420. Routine Description:
  421. This routine processes an exception. It either completes the request
  422. with the exception status in the IrpContext, sends this off to the Fsp
  423. workque or causes it to be retried in the current thread if a verification
  424. is needed.
  425. If the volume needs to be verified (STATUS_VERIFY_REQUIRED) and we can
  426. do the work in the current thread we will translate the status code
  427. to STATUS_CANT_WAIT to indicate that we need to retry the request.
  428. Arguments:
  429. Irp - Supplies the Irp being processed
  430. ExceptionCode - Supplies the normalized exception status being handled
  431. Return Value:
  432. NTSTATUS - Returns the results of either posting the Irp or the
  433. saved completion status.
  434. --*/
  435. {
  436. PDEVICE_OBJECT Device;
  437. PVPB Vpb;
  438. PETHREAD Thread;
  439. ASSERT_OPTIONAL_IRP_CONTEXT( IrpContext );
  440. ASSERT_IRP( Irp );
  441. //
  442. // If there is not an irp context, then complete the request with the
  443. // current status code.
  444. //
  445. if (!ARGUMENT_PRESENT( IrpContext )) {
  446. UdfCompleteRequest( NULL, Irp, ExceptionCode );
  447. return ExceptionCode;
  448. }
  449. //
  450. // Get the real exception status from the IrpContext.
  451. //
  452. ExceptionCode = IrpContext->ExceptionStatus;
  453. //
  454. // If we are not a top level request then we just complete the request
  455. // with the current status code.
  456. //
  457. if (!FlagOn( IrpContext->Flags, IRP_CONTEXT_FLAG_TOP_LEVEL )) {
  458. UdfCompleteRequest( IrpContext, Irp, ExceptionCode );
  459. return ExceptionCode;
  460. }
  461. //
  462. // Check if we are posting this request. One of the following must be true
  463. // if we are to post a request.
  464. //
  465. // - Status code is STATUS_CANT_WAIT and the request is asynchronous
  466. // or we are forcing this to be posted.
  467. //
  468. // - Status code is STATUS_VERIFY_REQUIRED and we are at APC level
  469. // or higher. Can't wait for IO in the verify path in this case.
  470. //
  471. // Set the MORE_PROCESSING flag in the IrpContext to keep if from being
  472. // deleted if this is a retryable condition.
  473. //
  474. // Note: Children of UdfFsdPostRequest() can raise.
  475. //
  476. try {
  477. if (ExceptionCode == STATUS_CANT_WAIT) {
  478. if (FlagOn( IrpContext->Flags, IRP_CONTEXT_FLAG_FORCE_POST )) {
  479. ExceptionCode = UdfFsdPostRequest( IrpContext, Irp );
  480. }
  481. } else if (ExceptionCode == STATUS_VERIFY_REQUIRED) {
  482. if (KeGetCurrentIrql() >= APC_LEVEL) {
  483. ExceptionCode = UdfFsdPostRequest( IrpContext, Irp );
  484. }
  485. }
  486. }
  487. except (UdfExceptionFilter( IrpContext, GetExceptionInformation())) {
  488. ExceptionCode = GetExceptionCode();
  489. }
  490. //
  491. // If we posted the request or our caller will retry then just return here.
  492. //
  493. if ((ExceptionCode == STATUS_PENDING) ||
  494. (ExceptionCode == STATUS_CANT_WAIT)) {
  495. return ExceptionCode;
  496. }
  497. ClearFlag( IrpContext->Flags, IRP_CONTEXT_FLAG_MORE_PROCESSING );
  498. //
  499. // Store this error into the Irp for posting back to the Io system.
  500. //
  501. Irp->IoStatus.Status = ExceptionCode;
  502. if (IoIsErrorUserInduced( ExceptionCode )) {
  503. //
  504. // Check for the various error conditions that can be caused by,
  505. // and possibly resolved my the user.
  506. //
  507. if (ExceptionCode == STATUS_VERIFY_REQUIRED) {
  508. //
  509. // Now we are at the top level file system entry point.
  510. //
  511. // If we have already posted this request then the device to
  512. // verify is in the original thread. Find this via the Irp.
  513. //
  514. Device = IoGetDeviceToVerify( Irp->Tail.Overlay.Thread );
  515. IoSetDeviceToVerify( Irp->Tail.Overlay.Thread, NULL );
  516. //
  517. // If there is no device in that location then check in the
  518. // current thread.
  519. //
  520. if (Device == NULL) {
  521. Device = IoGetDeviceToVerify( PsGetCurrentThread() );
  522. IoSetDeviceToVerify( PsGetCurrentThread(), NULL );
  523. ASSERT( Device != NULL );
  524. //
  525. // Let's not BugCheck just because the driver is messed up.
  526. //
  527. if (Device == NULL) {
  528. ExceptionCode = STATUS_DRIVER_INTERNAL_ERROR;
  529. UdfCompleteRequest( IrpContext, Irp, ExceptionCode );
  530. return ExceptionCode;
  531. }
  532. }
  533. //
  534. // CdPerformVerify() will do the right thing with the Irp.
  535. // If we return STATUS_CANT_WAIT then the current thread
  536. // can retry the request.
  537. //
  538. return UdfPerformVerify( IrpContext, Irp, Device );
  539. }
  540. //
  541. // The other user induced conditions generate an error unless
  542. // they have been disabled for this request.
  543. //
  544. if (FlagOn( IrpContext->Flags, IRP_CONTEXT_FLAG_DISABLE_POPUPS )) {
  545. UdfCompleteRequest( IrpContext, Irp, ExceptionCode );
  546. return ExceptionCode;
  547. }
  548. //
  549. // Generate a pop-up.
  550. //
  551. else {
  552. if (IoGetCurrentIrpStackLocation( Irp )->FileObject != NULL) {
  553. Vpb = IoGetCurrentIrpStackLocation( Irp )->FileObject->Vpb;
  554. } else {
  555. Vpb = NULL;
  556. }
  557. //
  558. // The device to verify is either in my thread local storage
  559. // or that of the thread that owns the Irp.
  560. //
  561. Thread = Irp->Tail.Overlay.Thread;
  562. Device = IoGetDeviceToVerify( Thread );
  563. if (Device == NULL) {
  564. Thread = PsGetCurrentThread();
  565. Device = IoGetDeviceToVerify( Thread );
  566. ASSERT( Device != NULL );
  567. //
  568. // Let's not BugCheck just because the driver is messed up.
  569. //
  570. if (Device == NULL) {
  571. UdfCompleteRequest( IrpContext, Irp, ExceptionCode );
  572. return ExceptionCode;
  573. }
  574. }
  575. //
  576. // This routine actually causes the pop-up. It usually
  577. // does this by queuing an APC to the callers thread,
  578. // but in some cases it will complete the request immediately,
  579. // so it is very important to IoMarkIrpPending() first.
  580. //
  581. DebugTrace(( 0, Dbg | UDFS_DEBUG_LEVEL_VERFYSUP,
  582. "Raising hard error on exception %x\n", ExceptionCode));
  583. IoMarkIrpPending( Irp );
  584. IoRaiseHardError( Irp, Vpb, Device );
  585. //
  586. // We will be handing control back to the caller here, so
  587. // reset the saved device object.
  588. //
  589. IoSetDeviceToVerify( Thread, NULL );
  590. //
  591. // The Irp will be completed by Io or resubmitted. In either
  592. // case we must clean up the IrpContext here.
  593. //
  594. UdfCompleteRequest( IrpContext, NULL, STATUS_SUCCESS );
  595. return STATUS_PENDING;
  596. }
  597. }
  598. //
  599. // If we were in the process of mount, then we need to morph any
  600. // corruption errors to STATUS_UNRECOGNIZED_VOLUME, so that Io continues
  601. // the mount process and RAW will get the volume. Otherwise we keep
  602. // getting remount requests when we try to reformat a corrupt volume
  603. // (or do anything with the drive).
  604. //
  605. #if 0
  606. // Not sure about this. Format should do a 'raw allowed' open, so....
  607. if ((IRP_MJ_FILE_SYSTEM_CONTROL == IrpContext->MajorFunction) &&
  608. (IRP_MN_MOUNT_VOLUME == IrpContext->MinorFunction)) {
  609. ExceptionCode = STATUS_UNRECOGNIZED_VOLUME;
  610. }
  611. #endif
  612. //
  613. // This is just a run of the mill error.
  614. //
  615. UdfCompleteRequest( IrpContext, Irp, ExceptionCode );
  616. return ExceptionCode;
  617. }
  618. VOID
  619. UdfCompleteRequest (
  620. IN PIRP_CONTEXT IrpContext OPTIONAL,
  621. IN PIRP Irp OPTIONAL,
  622. IN NTSTATUS Status
  623. )
  624. /*++
  625. Routine Description:
  626. This routine completes a Irp and cleans up the IrpContext. Either or
  627. both of these may not be specified.
  628. Arguments:
  629. Irp - Supplies the Irp being processed.
  630. Status - Supplies the status to complete the Irp with
  631. Return Value:
  632. None.
  633. --*/
  634. {
  635. ASSERT_OPTIONAL_IRP_CONTEXT( IrpContext );
  636. ASSERT_OPTIONAL_IRP( Irp );
  637. //
  638. // Cleanup the IrpContext if passed in here.
  639. //
  640. if (ARGUMENT_PRESENT( IrpContext )) {
  641. UdfCleanupIrpContext( IrpContext, FALSE );
  642. }
  643. //
  644. // If we have an Irp then complete the irp.
  645. //
  646. if (ARGUMENT_PRESENT( Irp )) {
  647. //
  648. // Clear the information field in case we have used this Irp
  649. // internally.
  650. //
  651. if (NT_ERROR( Status ) &&
  652. FlagOn( Irp->Flags, IRP_INPUT_OPERATION )) {
  653. Irp->IoStatus.Information = 0;
  654. }
  655. Irp->IoStatus.Status = Status;
  656. IoCompleteRequest( Irp, IO_CD_ROM_INCREMENT );
  657. }
  658. return;
  659. }
  660. VOID
  661. UdfSetThreadContext (
  662. IN PIRP_CONTEXT IrpContext,
  663. IN PTHREAD_CONTEXT ThreadContext
  664. )
  665. /*++
  666. Routine Description:
  667. This routine is called at each Fsd/Fsp entry point set up the IrpContext
  668. and thread local storage to track top level requests. If there is
  669. not a Udfs context in the thread local storage then we use the input one.
  670. Otherwise we use the one already there. This routine also updates the
  671. IrpContext based on the state of the top-level context.
  672. If the TOP_LEVEL flag in the IrpContext is already set when we are called
  673. then we force this request to appear top level.
  674. Arguments:
  675. ThreadContext - Address on stack for local storage if not already present.
  676. ForceTopLevel - We force this request to appear top level regardless of
  677. any previous stack value.
  678. Return Value:
  679. None
  680. --*/
  681. {
  682. PTHREAD_CONTEXT CurrentThreadContext;
  683. ULONG_PTR StackTop;
  684. ULONG_PTR StackBottom;
  685. PAGED_CODE();
  686. ASSERT_IRP_CONTEXT( IrpContext );
  687. //
  688. // Get the current top-level irp out of the thread storage.
  689. // If NULL then this is the top-level request.
  690. //
  691. CurrentThreadContext = (PTHREAD_CONTEXT) IoGetTopLevelIrp();
  692. if (CurrentThreadContext == NULL) {
  693. SetFlag( IrpContext->Flags, IRP_CONTEXT_FLAG_TOP_LEVEL );
  694. }
  695. //
  696. // Initialize the input context unless we are using the current
  697. // thread context block. We use the new block if our caller
  698. // specified this or the existing block is invalid.
  699. //
  700. // The following must be true for the current to be a valid Udfs context.
  701. //
  702. // Structure must lie within current stack.
  703. // Address must be ULONG aligned.
  704. // Udfs signature must be present.
  705. //
  706. // If this is not a valid Udfs context then use the input thread
  707. // context and store it in the top level context.
  708. //
  709. IoGetStackLimits( &StackTop, &StackBottom);
  710. if (FlagOn( IrpContext->Flags, IRP_CONTEXT_FLAG_TOP_LEVEL ) ||
  711. (((ULONG_PTR) CurrentThreadContext > StackBottom - sizeof( THREAD_CONTEXT )) ||
  712. ((ULONG_PTR) CurrentThreadContext <= StackTop) ||
  713. LongOffsetPtr( CurrentThreadContext ) ||
  714. (CurrentThreadContext->Udfs != UDFS_SIGNATURE))) {
  715. ThreadContext->Udfs = UDFS_SIGNATURE;
  716. ThreadContext->SavedTopLevelIrp = (PIRP) CurrentThreadContext;
  717. ThreadContext->TopLevelIrpContext = IrpContext;
  718. IoSetTopLevelIrp( (PIRP) ThreadContext );
  719. IrpContext->TopLevel = IrpContext;
  720. IrpContext->ThreadContext = ThreadContext;
  721. SetFlag( IrpContext->Flags, IRP_CONTEXT_FLAG_TOP_LEVEL_UDFS );
  722. //
  723. // Otherwise use the IrpContext in the thread context.
  724. //
  725. } else {
  726. IrpContext->TopLevel = CurrentThreadContext->TopLevelIrpContext;
  727. }
  728. return;
  729. }
  730. BOOLEAN
  731. UdfFastIoCheckIfPossible (
  732. IN PFILE_OBJECT FileObject,
  733. IN PLARGE_INTEGER FileOffset,
  734. IN ULONG Length,
  735. IN BOOLEAN Wait,
  736. IN ULONG LockKey,
  737. IN BOOLEAN CheckForReadOperation,
  738. OUT PIO_STATUS_BLOCK IoStatus,
  739. IN PDEVICE_OBJECT DeviceObject
  740. )
  741. /*++
  742. Routine Description:
  743. This routine checks if fast i/o is possible for a read/write operation
  744. Arguments:
  745. FileObject - Supplies the file object used in the query
  746. FileOffset - Supplies the starting byte offset for the read/write operation
  747. Length - Supplies the length, in bytes, of the read/write operation
  748. Wait - Indicates if we can wait
  749. LockKey - Supplies the lock key
  750. CheckForReadOperation - Indicates if this is a check for a read or write
  751. operation
  752. IoStatus - Receives the status of the operation if our return value is
  753. FastIoReturnError
  754. Return Value:
  755. BOOLEAN - TRUE if fast I/O is possible and FALSE if the caller needs
  756. to take the long route.
  757. --*/
  758. {
  759. PAGED_CODE();
  760. return TRUE;
  761. }
  762. ULONG
  763. UdfSerial32 (
  764. IN PCHAR Buffer,
  765. IN ULONG ByteCount
  766. )
  767. /*++
  768. Routine Description:
  769. This routine is called to generate a 32 bit serial number. This is
  770. done by doing four separate checksums into an array of bytes and
  771. then treating the bytes as a ULONG.
  772. Arguments:
  773. Buffer - Pointer to the buffer to generate the ID for.
  774. ByteCount - Number of bytes in the buffer.
  775. Return Value:
  776. ULONG - The 32 bit serial number.
  777. --*/
  778. {
  779. union {
  780. UCHAR Bytes[4];
  781. ULONG SerialId;
  782. } Checksum;
  783. PAGED_CODE();
  784. //
  785. // Initialize the serial number.
  786. //
  787. Checksum.SerialId = 0;
  788. //
  789. // Continue while there are more bytes to use.
  790. //
  791. while (ByteCount--) {
  792. //
  793. // Increment this sub-checksum.
  794. //
  795. Checksum.Bytes[ByteCount & 0x3] += *(Buffer++);
  796. }
  797. //
  798. // Return the checksums as a ULONG.
  799. //
  800. return Checksum.SerialId;
  801. }
  802. VOID
  803. UdfInitializeCrc16 (
  804. ULONG Polynomial
  805. )
  806. /*++
  807. Routine Description:
  808. This routine generates the 16bit CRC Table to be used in CRC calculation.
  809. Arguments:
  810. Polynomial - Starting seed for the generation
  811. Return Value:
  812. None
  813. --*/
  814. {
  815. ULONG n, i, Crc;
  816. //
  817. // All CRC code was devised by Don P. Mitchell of AT&T Bell Laboratories
  818. // and Ned W. Rhodes of Software Systems Group. It has been published in
  819. // "Design and Validation of Computer Protocols", Prentice Hall, Englewood
  820. // Cliffs, NJ, 1991, Chapter 3, ISBN 0-13-539925-4.
  821. //
  822. // Copyright is held by AT&T.
  823. //
  824. // AT&T gives permission for the free use of the source code.
  825. //
  826. UdfCrcTable = (PUSHORT) FsRtlAllocatePoolWithTag( UdfPagedPool,
  827. 256 * sizeof(USHORT),
  828. TAG_CRC_TABLE );
  829. for (n = 0; n < 256; n++) {
  830. Crc = n << 8;
  831. for (i = 0; i < 8; i++) {
  832. if(Crc & 0x8000) {
  833. Crc = (Crc << 1) ^ Polynomial;
  834. } else {
  835. Crc <<= 1;
  836. }
  837. Crc &= 0xffff;
  838. }
  839. UdfCrcTable[n] = (USHORT) Crc;
  840. }
  841. }
  842. USHORT
  843. UdfComputeCrc16 (
  844. PUCHAR Buffer,
  845. ULONG ByteCount
  846. )
  847. /*++
  848. Routine Description:
  849. This routine generates a 16 bit CRC of the input buffer in accordance
  850. with the precomputed CRC table.
  851. Arguments:
  852. Buffer - Pointer to the buffer to generate the CRC for.
  853. ByteCount - Number of bytes in the buffer.
  854. Return Value:
  855. USHORT - The 16bit CRC
  856. --*/
  857. {
  858. USHORT Crc = 0;
  859. //
  860. // All CRC code was devised by Don P. Mitchell of AT&T Bell Laboratories
  861. // and Ned W. Rhodes of Software Systems Group. It has been published in
  862. // "Design and Validation of Computer Protocols", Prentice Hall, Englewood
  863. // Cliffs, NJ, 1991, Chapter 3, ISBN 0-13-539925-4.
  864. //
  865. // Copyright is held by AT&T.
  866. //
  867. // AT&T gives permission for the free use of the source code.
  868. //
  869. while (ByteCount-- > 0) {
  870. Crc = UdfCrcTable[((Crc >> 8) ^ *Buffer++) & 0xff] ^ (Crc << 8);
  871. }
  872. return Crc;
  873. }
  874. USHORT
  875. UdfComputeCrc16Uni (
  876. PWCHAR Buffer,
  877. ULONG CharCount
  878. )
  879. /*++
  880. Routine Description:
  881. This routine generates a 16 bit CRC of the input buffer in accordance
  882. with the precomputed CRC table.
  883. It performs a byte-order independent crc (hi then lo). This is a bit
  884. suspect, but is called for in the specification.
  885. Arguments:
  886. Buffer - Pointer to the buffer to generate the CRC for.
  887. ShortCount - Number of wide characters in the buffer.
  888. Return Value:
  889. USHORT - The 16bit CRC
  890. --*/
  891. {
  892. USHORT Crc = 0;
  893. //
  894. // Byte order independent CRC, hi byte to low byte per character.
  895. //
  896. while (CharCount-- > 0) {
  897. Crc = UdfCrcTable[((Crc >> 8) ^ (*Buffer >> 8)) & 0xff] ^ (Crc << 8);
  898. Crc = UdfCrcTable[((Crc >> 8) ^ (*Buffer++ & 0xff)) & 0xff] ^ (Crc << 8);
  899. }
  900. return Crc;
  901. }
  902. ULONG
  903. UdfHighBit (
  904. ULONG Word
  905. )
  906. /*++
  907. Routine Description:
  908. This routine discovers the highest set bit of the input word. It is
  909. equivalent to the integer logarithim base 2.
  910. Arguments:
  911. Word - word to check
  912. Return Value:
  913. Bit offset of highest set bit. If no bit is set, return is zero.
  914. --*/
  915. {
  916. ULONG Offset = 31;
  917. ULONG Mask = (ULONG)(1 << 31);
  918. if (Word == 0) {
  919. return 0;
  920. }
  921. while ((Word & Mask) == 0) {
  922. Offset--;
  923. Mask >>= 1;
  924. }
  925. return Offset;
  926. }
  927. #ifdef UDF_SANITY
  928. BOOLEAN
  929. UdfDebugTrace (
  930. LONG IndentIncrement,
  931. ULONG TraceMask,
  932. PCHAR Format,
  933. ...
  934. )
  935. /*++
  936. Routine Description:
  937. This routine is a simple debug info printer that returns a constant boolean value. This
  938. makes it possible to splice it into the middle of boolean expressions to discover which
  939. elements are firing.
  940. We will use this as our general debug printer. See udfdata.h for how we use the DebugTrace
  941. macro to accomplish the effect.
  942. Arguments:
  943. IndentIncrement - amount to change the indentation by.
  944. TraceMask - specification of what debug trace level this call should be noisy at.
  945. Return Value:
  946. USHORT - The 16bit CRC
  947. --*/
  948. {
  949. va_list Arglist;
  950. LONG i;
  951. UCHAR Buffer[256];
  952. int Bytes;
  953. int ThreadIdChars;
  954. if (TraceMask == 0 || (UdfDebugTraceLevel & TraceMask) != 0) {
  955. if (IndentIncrement < 0) {
  956. UdfDebugTraceIndent += IndentIncrement;
  957. }
  958. if (UdfDebugTraceIndent < 0) {
  959. UdfDebugTraceIndent = 0;
  960. }
  961. //
  962. // Print out the current thread, otherwise all this trace is completely
  963. // useless in the face of multi threaded issues.
  964. //
  965. ThreadIdChars = sprintf(Buffer, "%p ", PsGetCurrentThread());
  966. // DbgPrint( Buffer );
  967. //
  968. // The indent stuff doesn't work too well across multi thread output.
  969. //
  970. #if 0
  971. //
  972. // Build the indent in big chunks since calling DbgPrint repeatedly is expensive.
  973. //
  974. for (i = UdfDebugTraceIndent; i > 0; i -= (sizeof(Buffer) - 1)) {
  975. RtlFillMemory( Buffer, Min( i, (sizeof(Buffer) - 1 )), ' ');
  976. *(Buffer + Min( i, (sizeof(Buffer) - 1 ))) = '\0';
  977. DbgPrint( Buffer );
  978. }
  979. #endif
  980. //
  981. // Format the output into a buffer and then print it.
  982. //
  983. va_start( Arglist, Format );
  984. Bytes = _vsnprintf( Buffer + ThreadIdChars, sizeof(Buffer) - ThreadIdChars, Format, Arglist );
  985. va_end( Arglist );
  986. //
  987. // detect buffer overflow
  988. //
  989. if (Bytes == -1) {
  990. Buffer[sizeof(Buffer) - 1] = '\n';
  991. }
  992. DbgPrint( Buffer );
  993. if (IndentIncrement > 0) {
  994. UdfDebugTraceIndent += IndentIncrement;
  995. }
  996. }
  997. return TRUE;
  998. }
  999. #endif