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.

558 lines
14 KiB

  1. /*++
  2. Copyright (c) 1989-2000 Microsoft Corporation
  3. Module Name:
  4. Read.c
  5. Abstract:
  6. This module implements the File Read routine for Read called by the
  7. Fsd/Fsp dispatch drivers.
  8. // @@BEGIN_DDKSPLIT
  9. Author:
  10. Brian Andrew [BrianAn] 01-July-1995
  11. Revision History:
  12. // @@END_DDKSPLIT
  13. --*/
  14. #include "CdProcs.h"
  15. //
  16. // The Bug check file id for this module
  17. //
  18. #define BugCheckFileId (CDFS_BUG_CHECK_READ)
  19. //
  20. // VOID
  21. // SafeZeroMemory (
  22. // IN PUCHAR At,
  23. // IN ULONG ByteCount
  24. // );
  25. //
  26. //
  27. // This macro just puts a nice little try-except around RtlZeroMemory
  28. //
  29. #define SafeZeroMemory(IC,AT,BYTE_COUNT) { \
  30. try { \
  31. RtlZeroMemory( (AT), (BYTE_COUNT) ); \
  32. } except( EXCEPTION_EXECUTE_HANDLER ) { \
  33. CdRaiseStatus( IC, STATUS_INVALID_USER_BUFFER ); \
  34. } \
  35. }
  36. //
  37. // Read ahead amount used for normal data files
  38. //
  39. #define READ_AHEAD_GRANULARITY (0x10000)
  40. #ifdef ALLOC_PRAGMA
  41. #pragma alloc_text(PAGE, CdCommonRead)
  42. #endif
  43. NTSTATUS
  44. CdCommonRead (
  45. IN PIRP_CONTEXT IrpContext,
  46. IN PIRP Irp
  47. )
  48. /*++
  49. Routine Description:
  50. This is the common entry point for NtReadFile calls. For synchronous requests,
  51. CommonRead will complete the request in the current thread. If not
  52. synchronous the request will be passed to the Fsp if there is a need to
  53. block.
  54. Arguments:
  55. Irp - Supplies the Irp to process
  56. Return Value:
  57. NTSTATUS - The result of this operation.
  58. --*/
  59. {
  60. NTSTATUS Status;
  61. PIO_STACK_LOCATION IrpSp = IoGetCurrentIrpStackLocation( Irp );
  62. TYPE_OF_OPEN TypeOfOpen;
  63. PFCB Fcb;
  64. PCCB Ccb;
  65. BOOLEAN Wait;
  66. ULONG PagingIo;
  67. ULONG SynchronousIo;
  68. ULONG NonCachedIo;
  69. PVOID UserBuffer;
  70. LONGLONG StartingOffset;
  71. LONGLONG ByteRange;
  72. ULONG ByteCount;
  73. ULONG ReadByteCount;
  74. ULONG OriginalByteCount;
  75. PVOID SystemBuffer;
  76. BOOLEAN ReleaseFile = TRUE;
  77. CD_IO_CONTEXT LocalIoContext;
  78. PAGED_CODE();
  79. //
  80. // If this is a zero length read then return SUCCESS immediately.
  81. //
  82. if (IrpSp->Parameters.Read.Length == 0) {
  83. CdCompleteRequest( IrpContext, Irp, STATUS_SUCCESS );
  84. return STATUS_SUCCESS;
  85. }
  86. //
  87. // Decode the file object and verify we support read on this. It
  88. // must be a user file, stream file or volume file (for a data disk).
  89. //
  90. TypeOfOpen = CdDecodeFileObject( IrpContext, IrpSp->FileObject, &Fcb, &Ccb );
  91. if ((TypeOfOpen == UnopenedFileObject) ||
  92. (TypeOfOpen == UserDirectoryOpen)) {
  93. CdCompleteRequest( IrpContext, Irp, STATUS_INVALID_DEVICE_REQUEST );
  94. return STATUS_INVALID_DEVICE_REQUEST;
  95. }
  96. //
  97. // Examine our input parameters to determine if this is noncached and/or
  98. // a paging io operation.
  99. //
  100. Wait = BooleanFlagOn( IrpContext->Flags, IRP_CONTEXT_FLAG_WAIT );
  101. PagingIo = FlagOn( Irp->Flags, IRP_PAGING_IO );
  102. NonCachedIo = FlagOn( Irp->Flags, IRP_NOCACHE );
  103. SynchronousIo = FlagOn( IrpSp->FileObject->Flags, FO_SYNCHRONOUS_IO );
  104. //
  105. // Extract the range of the Io.
  106. //
  107. StartingOffset = IrpSp->Parameters.Read.ByteOffset.QuadPart;
  108. OriginalByteCount = ByteCount = IrpSp->Parameters.Read.Length;
  109. ByteRange = StartingOffset + ByteCount;
  110. //
  111. // Make sure that Dasd access is always non-cached.
  112. //
  113. if (TypeOfOpen == UserVolumeOpen) {
  114. NonCachedIo = TRUE;
  115. }
  116. //
  117. // Acquire the file shared to perform the read. If we are doing paging IO,
  118. // it may be the case that we would have a deadlock imminent because we may
  119. // block on shared access, so starve out any exclusive waiters. This requires
  120. // a degree of caution - we believe that any paging IO bursts will recede and
  121. // allow the exclusive waiter in.
  122. //
  123. if (PagingIo) {
  124. CdAcquireFileSharedStarveExclusive( IrpContext, Fcb );
  125. } else {
  126. CdAcquireFileShared( IrpContext, Fcb );
  127. }
  128. //
  129. // Use a try-finally to facilitate cleanup.
  130. //
  131. try {
  132. //
  133. // Verify the Fcb. Allow reads if this is a DASD handle that is
  134. // dismounting the volume.
  135. //
  136. if ((TypeOfOpen != UserVolumeOpen) || (NULL == Ccb) ||
  137. !FlagOn( Ccb->Flags, CCB_FLAG_DISMOUNT_ON_CLOSE)) {
  138. CdVerifyFcbOperation( IrpContext, Fcb );
  139. }
  140. //
  141. // If this is a non-cached then check whether we need to post this
  142. // request if this thread can't block.
  143. //
  144. if (!Wait && NonCachedIo) {
  145. //
  146. // XA requests must always be waitable.
  147. //
  148. if (FlagOn( Fcb->FcbState, FCB_STATE_RAWSECTOR_MASK )) {
  149. SetFlag( IrpContext->Flags, IRP_CONTEXT_FLAG_FORCE_POST );
  150. try_return( Status = STATUS_CANT_WAIT );
  151. }
  152. }
  153. //
  154. // If this is a user request then verify the oplock and filelock state.
  155. //
  156. if (TypeOfOpen == UserFileOpen) {
  157. //
  158. // We check whether we can proceed
  159. // based on the state of the file oplocks.
  160. //
  161. Status = FsRtlCheckOplock( &Fcb->Oplock,
  162. Irp,
  163. IrpContext,
  164. CdOplockComplete,
  165. CdPrePostIrp );
  166. //
  167. // If the result is not STATUS_SUCCESS then the Irp was completed
  168. // elsewhere.
  169. //
  170. if (Status != STATUS_SUCCESS) {
  171. Irp = NULL;
  172. IrpContext = NULL;
  173. try_return( NOTHING );
  174. }
  175. if (!PagingIo &&
  176. (Fcb->FileLock != NULL) &&
  177. !FsRtlCheckLockForReadAccess( Fcb->FileLock, Irp )) {
  178. try_return( Status = STATUS_FILE_LOCK_CONFLICT );
  179. }
  180. }
  181. //
  182. // Complete the request if it begins beyond the end of file.
  183. //
  184. if (StartingOffset >= Fcb->FileSize.QuadPart) {
  185. try_return( Status = STATUS_END_OF_FILE );
  186. }
  187. //
  188. // Truncate the read if it extends beyond the end of the file.
  189. //
  190. if (ByteRange > Fcb->FileSize.QuadPart) {
  191. ByteCount = (ULONG) (Fcb->FileSize.QuadPart - StartingOffset);
  192. ByteRange = Fcb->FileSize.QuadPart;
  193. }
  194. //
  195. // Handle the non-cached read first.
  196. //
  197. if (NonCachedIo) {
  198. //
  199. // If we have an unaligned transfer then post this request if
  200. // we can't wait. Unaligned means that the starting offset
  201. // is not on a sector boundary or the read is not integral
  202. // sectors.
  203. //
  204. ReadByteCount = BlockAlign( Fcb->Vcb, ByteCount );
  205. if (SectorOffset( StartingOffset ) ||
  206. SectorOffset( ReadByteCount ) ||
  207. (ReadByteCount > OriginalByteCount)) {
  208. if (!Wait) {
  209. CdRaiseStatus( IrpContext, STATUS_CANT_WAIT );
  210. }
  211. //
  212. // Make sure we don't overwrite the buffer.
  213. //
  214. ReadByteCount = ByteCount;
  215. }
  216. //
  217. // Initialize the IoContext for the read.
  218. // If there is a context pointer, we need to make sure it was
  219. // allocated and not a stale stack pointer.
  220. //
  221. if (IrpContext->IoContext == NULL ||
  222. !FlagOn( IrpContext->Flags, IRP_CONTEXT_FLAG_ALLOC_IO )) {
  223. //
  224. // If we can wait, use the context on the stack. Otherwise
  225. // we need to allocate one.
  226. //
  227. if (Wait) {
  228. IrpContext->IoContext = &LocalIoContext;
  229. ClearFlag( IrpContext->Flags, IRP_CONTEXT_FLAG_ALLOC_IO );
  230. } else {
  231. IrpContext->IoContext = CdAllocateIoContext();
  232. SetFlag( IrpContext->Flags, IRP_CONTEXT_FLAG_ALLOC_IO );
  233. }
  234. }
  235. RtlZeroMemory( IrpContext->IoContext, sizeof( CD_IO_CONTEXT ));
  236. //
  237. // Store whether we allocated this context structure in the structure
  238. // itself.
  239. //
  240. IrpContext->IoContext->AllocatedContext =
  241. BooleanFlagOn( IrpContext->Flags, IRP_CONTEXT_FLAG_ALLOC_IO );
  242. if (Wait) {
  243. KeInitializeEvent( &IrpContext->IoContext->SyncEvent,
  244. NotificationEvent,
  245. FALSE );
  246. } else {
  247. IrpContext->IoContext->ResourceThreadId = ExGetCurrentResourceThread();
  248. IrpContext->IoContext->Resource = Fcb->Resource;
  249. IrpContext->IoContext->RequestedByteCount = ByteCount;
  250. }
  251. Irp->IoStatus.Information = ReadByteCount;
  252. //
  253. // Call one of the NonCacheIo routines to perform the actual
  254. // read.
  255. //
  256. if (FlagOn( Fcb->FcbState, FCB_STATE_RAWSECTOR_MASK )) {
  257. Status = CdNonCachedXARead( IrpContext, Fcb, StartingOffset, ReadByteCount );
  258. } else {
  259. Status = CdNonCachedRead( IrpContext, Fcb, StartingOffset, ReadByteCount );
  260. }
  261. //
  262. // Don't complete this request now if STATUS_PENDING was returned.
  263. //
  264. if (Status == STATUS_PENDING) {
  265. Irp = NULL;
  266. ReleaseFile = FALSE;
  267. //
  268. // Test is we should zero part of the buffer or update the
  269. // synchronous file position.
  270. //
  271. } else {
  272. //
  273. // Convert any unknown error code to IO_ERROR.
  274. //
  275. if (!NT_SUCCESS( Status )) {
  276. //
  277. // Set the information field to zero.
  278. //
  279. Irp->IoStatus.Information = 0;
  280. //
  281. // Raise if this is a user induced error.
  282. //
  283. if (IoIsErrorUserInduced( Status )) {
  284. CdRaiseStatus( IrpContext, Status );
  285. }
  286. Status = FsRtlNormalizeNtstatus( Status, STATUS_UNEXPECTED_IO_ERROR );
  287. //
  288. // Check if there is any portion of the user's buffer to zero.
  289. //
  290. } else if (ReadByteCount != ByteCount) {
  291. CdMapUserBuffer( IrpContext, &UserBuffer);
  292. SafeZeroMemory( IrpContext,
  293. Add2Ptr( UserBuffer,
  294. ByteCount,
  295. PVOID ),
  296. ReadByteCount - ByteCount );
  297. Irp->IoStatus.Information = ByteCount;
  298. }
  299. //
  300. // Update the file position if this is a synchronous request.
  301. //
  302. if (SynchronousIo && !PagingIo && NT_SUCCESS( Status )) {
  303. IrpSp->FileObject->CurrentByteOffset.QuadPart = ByteRange;
  304. }
  305. }
  306. try_return( NOTHING );
  307. }
  308. //
  309. // Handle the cached case. Start by initializing the private
  310. // cache map.
  311. //
  312. if (IrpSp->FileObject->PrivateCacheMap == NULL) {
  313. //
  314. // Now initialize the cache map.
  315. //
  316. CcInitializeCacheMap( IrpSp->FileObject,
  317. (PCC_FILE_SIZES) &Fcb->AllocationSize,
  318. FALSE,
  319. &CdData.CacheManagerCallbacks,
  320. Fcb );
  321. CcSetReadAheadGranularity( IrpSp->FileObject, READ_AHEAD_GRANULARITY );
  322. }
  323. //
  324. // Read from the cache if this is not an Mdl read.
  325. //
  326. if (!FlagOn( IrpContext->MinorFunction, IRP_MN_MDL )) {
  327. //
  328. // If we are in the Fsp now because we had to wait earlier,
  329. // we must map the user buffer, otherwise we can use the
  330. // user's buffer directly.
  331. //
  332. CdMapUserBuffer( IrpContext, &SystemBuffer );
  333. //
  334. // Now try to do the copy.
  335. //
  336. if (!CcCopyRead( IrpSp->FileObject,
  337. (PLARGE_INTEGER) &StartingOffset,
  338. ByteCount,
  339. Wait,
  340. SystemBuffer,
  341. &Irp->IoStatus )) {
  342. try_return( Status = STATUS_CANT_WAIT );
  343. }
  344. //
  345. // If the call didn't succeed, raise the error status
  346. //
  347. if (!NT_SUCCESS( Irp->IoStatus.Status )) {
  348. CdNormalizeAndRaiseStatus( IrpContext, Irp->IoStatus.Status );
  349. }
  350. //
  351. // Otherwise perform the MdlRead operation.
  352. //
  353. } else {
  354. CcMdlRead( IrpSp->FileObject,
  355. (PLARGE_INTEGER) &StartingOffset,
  356. ByteCount,
  357. &Irp->MdlAddress,
  358. &Irp->IoStatus );
  359. Status = Irp->IoStatus.Status;
  360. }
  361. //
  362. // Update the current file position in the user file object.
  363. //
  364. if (SynchronousIo && !PagingIo && NT_SUCCESS( Status )) {
  365. IrpSp->FileObject->CurrentByteOffset.QuadPart = ByteRange;
  366. }
  367. try_exit: NOTHING;
  368. } finally {
  369. //
  370. // Release the Fcb.
  371. //
  372. if (ReleaseFile) {
  373. CdReleaseFile( IrpContext, Fcb );
  374. }
  375. }
  376. //
  377. // Post the request if we got CANT_WAIT.
  378. //
  379. if (Status == STATUS_CANT_WAIT) {
  380. Status = CdFsdPostRequest( IrpContext, Irp );
  381. //
  382. // Otherwise complete the request.
  383. //
  384. } else {
  385. CdCompleteRequest( IrpContext, Irp, Status );
  386. }
  387. return Status;
  388. }