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.

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