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.

652 lines
16 KiB

  1. /*++
  2. Copyright (c) 1990-2000 Microsoft Corporation
  3. Module Name:
  4. Cache.c
  5. Abstract:
  6. This module implements the cache management routines for the Cdfs
  7. FSD and FSP, by calling the Common Cache Manager.
  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_CACHESUP)
  19. //
  20. // Local debug trace level
  21. //
  22. #ifdef ALLOC_PRAGMA
  23. #pragma alloc_text(PAGE, CdCompleteMdl)
  24. #pragma alloc_text(PAGE, CdCreateInternalStream)
  25. #pragma alloc_text(PAGE, CdDeleteInternalStream)
  26. #pragma alloc_text(PAGE, CdPurgeVolume)
  27. #endif
  28. VOID
  29. CdCreateInternalStream (
  30. IN PIRP_CONTEXT IrpContext,
  31. IN PVCB Vcb,
  32. IN PFCB Fcb
  33. )
  34. /*++
  35. Routine Description:
  36. This function creates an internal stream file for interaction
  37. with the cache manager. The Fcb here can be for either a
  38. directory stream or for a path table stream.
  39. Arguments:
  40. Vcb - Vcb for this volume.
  41. Fcb - Points to the Fcb for this file. It is either an Index or
  42. Path Table Fcb.
  43. Return Value:
  44. None.
  45. --*/
  46. {
  47. PFILE_OBJECT StreamFile = NULL;
  48. BOOLEAN DecrementReference = FALSE;
  49. BOOLEAN CleanupDirContext = FALSE;
  50. BOOLEAN UpdateFcbSizes = FALSE;
  51. DIRENT Dirent;
  52. DIRENT_ENUM_CONTEXT DirContext;
  53. PAGED_CODE();
  54. ASSERT_IRP_CONTEXT( IrpContext );
  55. ASSERT_FCB( Fcb );
  56. //
  57. // We may only have the Fcb shared. Lock the Fcb and do a
  58. // safe test to see if we need to really create the file object.
  59. //
  60. CdLockFcb( IrpContext, Fcb );
  61. if (Fcb->FileObject != NULL) {
  62. CdUnlockFcb( IrpContext, Fcb );
  63. return;
  64. }
  65. //
  66. // Use a try-finally to facilitate cleanup.
  67. //
  68. try {
  69. //
  70. // Create the internal stream. The Vpb should be pointing at our volume
  71. // device object at this point.
  72. //
  73. StreamFile = IoCreateStreamFileObject( NULL, Vcb->Vpb->RealDevice );
  74. if (StreamFile == NULL) {
  75. CdRaiseStatus( IrpContext, STATUS_INSUFFICIENT_RESOURCES );
  76. }
  77. //
  78. // Initialize the fields of the file object.
  79. //
  80. StreamFile->ReadAccess = TRUE;
  81. StreamFile->WriteAccess = FALSE;
  82. StreamFile->DeleteAccess = FALSE;
  83. StreamFile->SectionObjectPointer = &Fcb->FcbNonpaged->SegmentObject;
  84. //
  85. // Set the file object type and increment the Vcb counts.
  86. //
  87. CdSetFileObject( IrpContext,
  88. StreamFile,
  89. StreamFileOpen,
  90. Fcb,
  91. NULL );
  92. //
  93. // We will reference the current Fcb twice to keep it from going
  94. // away in the error path. Otherwise if we dereference it
  95. // below in the finally clause a close could cause the Fcb to
  96. // be deallocated.
  97. //
  98. CdLockVcb( IrpContext, Vcb );
  99. CdIncrementReferenceCounts( IrpContext, Fcb, 2, 0 );
  100. CdUnlockVcb( IrpContext, Vcb );
  101. DecrementReference = TRUE;
  102. //
  103. // Initialize the cache map for the file.
  104. //
  105. CcInitializeCacheMap( StreamFile,
  106. (PCC_FILE_SIZES)&Fcb->AllocationSize,
  107. TRUE,
  108. &CdData.CacheManagerCallbacks,
  109. Fcb );
  110. //
  111. // Go ahead and store the stream file into the Fcb.
  112. //
  113. Fcb->FileObject = StreamFile;
  114. StreamFile = NULL;
  115. //
  116. // If this is the first file object for a directory then we need to
  117. // read the self entry for this directory and update the sizes
  118. // in the Fcb. We know that the Fcb has been initialized so
  119. // that we have a least one sector available to read.
  120. //
  121. if (!FlagOn( Fcb->FcbState, FCB_STATE_INITIALIZED )) {
  122. ULONG NewDataLength;
  123. //
  124. // Initialize the search structures.
  125. //
  126. CdInitializeDirContext( IrpContext, &DirContext );
  127. CdInitializeDirent( IrpContext, &Dirent );
  128. CleanupDirContext = TRUE;
  129. //
  130. // Read the dirent from disk and transfer the data to the
  131. // in-memory dirent.
  132. //
  133. CdLookupDirent( IrpContext,
  134. Fcb,
  135. Fcb->StreamOffset,
  136. &DirContext );
  137. CdUpdateDirentFromRawDirent( IrpContext, Fcb, &DirContext, &Dirent );
  138. //
  139. // Verify that this really for the self entry. We do this by
  140. // updating the name in the dirent and then checking that it matches
  141. // one of the hard coded names.
  142. //
  143. CdUpdateDirentName( IrpContext, &Dirent, FALSE );
  144. if (Dirent.CdFileName.FileName.Buffer != CdUnicodeSelfArray) {
  145. CdRaiseStatus( IrpContext, STATUS_FILE_CORRUPT_ERROR );
  146. }
  147. //
  148. // If the data sizes are different then update the header
  149. // and Mcb for this Fcb.
  150. //
  151. NewDataLength = BlockAlign( Vcb, Dirent.DataLength + Fcb->StreamOffset );
  152. if (NewDataLength == 0) {
  153. CdRaiseStatus( IrpContext, STATUS_FILE_CORRUPT_ERROR );
  154. }
  155. if (NewDataLength != Fcb->FileSize.QuadPart) {
  156. Fcb->AllocationSize.QuadPart =
  157. Fcb->FileSize.QuadPart =
  158. Fcb->ValidDataLength.QuadPart = NewDataLength;
  159. CcSetFileSizes( Fcb->FileObject, (PCC_FILE_SIZES) &Fcb->AllocationSize );
  160. CdTruncateAllocation( IrpContext, Fcb, 0 );
  161. CdAddInitialAllocation( IrpContext,
  162. Fcb,
  163. Dirent.StartingOffset,
  164. NewDataLength );
  165. UpdateFcbSizes = TRUE;
  166. }
  167. //
  168. // Check for the existence flag and transform to hidden.
  169. //
  170. if (FlagOn( Dirent.DirentFlags, CD_ATTRIBUTE_HIDDEN )) {
  171. SetFlag( Fcb->FileAttributes, FILE_ATTRIBUTE_HIDDEN );
  172. }
  173. //
  174. // Convert the time to NT time.
  175. //
  176. CdConvertCdTimeToNtTime( IrpContext,
  177. Dirent.CdTime,
  178. (PLARGE_INTEGER) &Fcb->CreationTime );
  179. //
  180. // Update the Fcb flags to indicate we have read the
  181. // self entry.
  182. //
  183. SetFlag( Fcb->FcbState, FCB_STATE_INITIALIZED );
  184. //
  185. // If we updated the sizes then we want to purge the file. Go
  186. // ahead and unpin and then purge the first page.
  187. //
  188. CdCleanupDirContext( IrpContext, &DirContext );
  189. CdCleanupDirent( IrpContext, &Dirent );
  190. CleanupDirContext = FALSE;
  191. if (UpdateFcbSizes) {
  192. CcPurgeCacheSection( &Fcb->FcbNonpaged->SegmentObject,
  193. NULL,
  194. 0,
  195. FALSE );
  196. }
  197. }
  198. } finally {
  199. //
  200. // Cleanup any dirent structures we may have used.
  201. //
  202. if (CleanupDirContext) {
  203. CdCleanupDirContext( IrpContext, &DirContext );
  204. CdCleanupDirent( IrpContext, &Dirent );
  205. }
  206. //
  207. // If we raised then we need to dereference the file object.
  208. //
  209. if (StreamFile != NULL) {
  210. ObDereferenceObject( StreamFile );
  211. Fcb->FileObject = NULL;
  212. }
  213. //
  214. // Dereference and unlock the Fcb.
  215. //
  216. if (DecrementReference) {
  217. CdLockVcb( IrpContext, Vcb );
  218. CdDecrementReferenceCounts( IrpContext, Fcb, 1, 0 );
  219. CdUnlockVcb( IrpContext, Vcb );
  220. }
  221. CdUnlockFcb( IrpContext, Fcb );
  222. }
  223. return;
  224. }
  225. VOID
  226. CdDeleteInternalStream (
  227. IN PIRP_CONTEXT IrpContext,
  228. IN PFCB Fcb
  229. )
  230. /*++
  231. Routine Description:
  232. This function creates an internal stream file for interaction
  233. with the cache manager. The Fcb here can be for either a
  234. directory stream or for a path table stream.
  235. Arguments:
  236. Fcb - Points to the Fcb for this file. It is either an Index or
  237. Path Table Fcb.
  238. Return Value:
  239. None.
  240. --*/
  241. {
  242. PFILE_OBJECT FileObject;
  243. PAGED_CODE();
  244. ASSERT_IRP_CONTEXT( IrpContext );
  245. ASSERT_FCB( Fcb );
  246. //
  247. // Lock the Fcb.
  248. //
  249. CdLockFcb( IrpContext, Fcb );
  250. //
  251. // Capture the file object.
  252. //
  253. FileObject = Fcb->FileObject;
  254. Fcb->FileObject = NULL;
  255. //
  256. // It is now safe to unlock the Fcb.
  257. //
  258. CdUnlockFcb( IrpContext, Fcb );
  259. //
  260. // Dereference the file object if present.
  261. //
  262. if (FileObject != NULL) {
  263. if (FileObject->PrivateCacheMap != NULL) {
  264. CcUninitializeCacheMap( FileObject, NULL, NULL );
  265. }
  266. ObDereferenceObject( FileObject );
  267. }
  268. return;
  269. }
  270. NTSTATUS
  271. CdCompleteMdl (
  272. IN PIRP_CONTEXT IrpContext,
  273. IN PIRP Irp
  274. )
  275. /*++
  276. Routine Description:
  277. This routine performs the function of completing Mdl reads.
  278. It should be called only from CdFsdRead.
  279. Arguments:
  280. Irp - Supplies the originating Irp.
  281. Return Value:
  282. NTSTATUS - Will always be STATUS_SUCCESS.
  283. --*/
  284. {
  285. PFILE_OBJECT FileObject;
  286. PAGED_CODE();
  287. //
  288. // Do completion processing.
  289. //
  290. FileObject = IoGetCurrentIrpStackLocation( Irp )->FileObject;
  291. CcMdlReadComplete( FileObject, Irp->MdlAddress );
  292. //
  293. // Mdl is now deallocated.
  294. //
  295. Irp->MdlAddress = NULL;
  296. //
  297. // Complete the request and exit right away.
  298. //
  299. CdCompleteRequest( IrpContext, Irp, STATUS_SUCCESS );
  300. return STATUS_SUCCESS;
  301. }
  302. NTSTATUS
  303. CdPurgeVolume (
  304. IN PIRP_CONTEXT IrpContext,
  305. IN PVCB Vcb,
  306. IN BOOLEAN DismountUnderway
  307. )
  308. /*++
  309. Routine Description:
  310. This routine is called to purge the volume. The purpose is to make all the stale file
  311. objects in the system go away in order to lock the volume.
  312. The Vcb is already acquired exclusively. We will lock out all file operations by
  313. acquiring the global file resource. Then we will walk through all of the Fcb's and
  314. perform the purge.
  315. Arguments:
  316. Vcb - Vcb for the volume to purge.
  317. DismountUnderway - Indicates that we are trying to delete all of the objects.
  318. We will purge the Path Table and VolumeDasd and dereference all
  319. internal streams.
  320. Return Value:
  321. NTSTATUS - The first failure of the purge operation.
  322. --*/
  323. {
  324. NTSTATUS Status = STATUS_SUCCESS;
  325. PVOID RestartKey = NULL;
  326. PFCB ThisFcb = NULL;
  327. PFCB NextFcb;
  328. BOOLEAN RemovedFcb;
  329. PAGED_CODE();
  330. ASSERT_EXCLUSIVE_VCB( Vcb);
  331. //
  332. // Force any remaining Fcb's in the delayed close queue to be closed.
  333. //
  334. CdFspClose( Vcb );
  335. //
  336. // Acquire the global file resource.
  337. //
  338. CdAcquireAllFiles( IrpContext, Vcb );
  339. //
  340. // Loop through each Fcb in the Fcb Table and perform the flush.
  341. //
  342. while (TRUE) {
  343. //
  344. // Lock the Vcb to lookup the next Fcb.
  345. //
  346. CdLockVcb( IrpContext, Vcb );
  347. NextFcb = CdGetNextFcb( IrpContext, Vcb, &RestartKey );
  348. //
  349. // Reference the NextFcb if present.
  350. //
  351. if (NextFcb != NULL) {
  352. NextFcb->FcbReference += 1;
  353. }
  354. //
  355. // If the last Fcb is present then decrement reference count and call teardown
  356. // to see if it should be removed.
  357. //
  358. if (ThisFcb != NULL) {
  359. ThisFcb->FcbReference -= 1;
  360. CdUnlockVcb( IrpContext, Vcb );
  361. CdTeardownStructures( IrpContext, ThisFcb, &RemovedFcb );
  362. } else {
  363. CdUnlockVcb( IrpContext, Vcb );
  364. }
  365. //
  366. // Break out of the loop if no more Fcb's.
  367. //
  368. if (NextFcb == NULL) {
  369. break;
  370. }
  371. //
  372. // Move to the next Fcb.
  373. //
  374. ThisFcb = NextFcb;
  375. //
  376. // If there is a image section then see if that can be closed.
  377. //
  378. if (ThisFcb->FcbNonpaged->SegmentObject.ImageSectionObject != NULL) {
  379. MmFlushImageSection( &ThisFcb->FcbNonpaged->SegmentObject, MmFlushForWrite );
  380. }
  381. //
  382. // If there is a data section then purge this. If there is an image
  383. // section then we won't be able to. Remember this if it is our first
  384. // error.
  385. //
  386. if ((ThisFcb->FcbNonpaged->SegmentObject.DataSectionObject != NULL) &&
  387. !CcPurgeCacheSection( &ThisFcb->FcbNonpaged->SegmentObject,
  388. NULL,
  389. 0,
  390. FALSE ) &&
  391. (Status == STATUS_SUCCESS)) {
  392. Status = STATUS_UNABLE_TO_DELETE_SECTION;
  393. }
  394. //
  395. // Dereference the internal stream if dismounting.
  396. //
  397. if (DismountUnderway &&
  398. (SafeNodeType( ThisFcb ) != CDFS_NTC_FCB_DATA) &&
  399. (ThisFcb->FileObject != NULL)) {
  400. CdDeleteInternalStream( IrpContext, ThisFcb );
  401. }
  402. }
  403. //
  404. // Now look at the path table and volume Dasd Fcb's.
  405. //
  406. if (DismountUnderway) {
  407. if (Vcb->PathTableFcb != NULL) {
  408. ThisFcb = Vcb->PathTableFcb;
  409. InterlockedIncrement( &Vcb->PathTableFcb->FcbReference );
  410. if ((ThisFcb->FcbNonpaged->SegmentObject.DataSectionObject != NULL) &&
  411. !CcPurgeCacheSection( &ThisFcb->FcbNonpaged->SegmentObject,
  412. NULL,
  413. 0,
  414. FALSE ) &&
  415. (Status == STATUS_SUCCESS)) {
  416. Status = STATUS_UNABLE_TO_DELETE_SECTION;
  417. }
  418. CdDeleteInternalStream( IrpContext, ThisFcb );
  419. InterlockedDecrement( &ThisFcb->FcbReference );
  420. CdTeardownStructures( IrpContext, ThisFcb, &RemovedFcb );
  421. }
  422. if (Vcb->VolumeDasdFcb != NULL) {
  423. ThisFcb = Vcb->VolumeDasdFcb;
  424. InterlockedIncrement( &ThisFcb->FcbReference );
  425. if ((ThisFcb->FcbNonpaged->SegmentObject.DataSectionObject != NULL) &&
  426. !CcPurgeCacheSection( &ThisFcb->FcbNonpaged->SegmentObject,
  427. NULL,
  428. 0,
  429. FALSE ) &&
  430. (Status == STATUS_SUCCESS)) {
  431. Status = STATUS_UNABLE_TO_DELETE_SECTION;
  432. }
  433. InterlockedDecrement( &ThisFcb->FcbReference );
  434. CdTeardownStructures( IrpContext, ThisFcb, &RemovedFcb );
  435. }
  436. }
  437. //
  438. // Release all of the files.
  439. //
  440. CdReleaseAllFiles( IrpContext, Vcb );
  441. return Status;
  442. }