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.

725 lines
18 KiB

  1. /*++
  2. Copyright (c) 1996-2000 Microsoft Corporation
  3. Module Name:
  4. CacheSup.c
  5. Abstract:
  6. This module implements the cache management routines for the Udfs
  7. FSD and FSP, by calling the Common Cache Manager.
  8. // @@BEGIN_DDKSPLIT
  9. Author:
  10. Dan Lovinger [DanLo] 12-Sep-1996
  11. Revision History:
  12. Tom Jolly [tomjolly] 21-Jan-2000 CcPurge and append at end of vmcb stream
  13. // @@END_DDKSPLIT
  14. --*/
  15. #include "UdfProcs.h"
  16. //
  17. // The Bug check file id for this module
  18. //
  19. #define BugCheckFileId (UDFS_BUG_CHECK_CACHESUP)
  20. //
  21. // The local debug trace level
  22. //
  23. #define Dbg (UDFS_DEBUG_LEVEL_CACHESUP)
  24. #ifdef ALLOC_PRAGMA
  25. #pragma alloc_text(PAGE, UdfCompleteMdl)
  26. #pragma alloc_text(PAGE, UdfCreateInternalStream)
  27. #pragma alloc_text(PAGE, UdfDeleteInternalStream)
  28. #pragma alloc_text(PAGE, UdfMapMetadataView)
  29. #pragma alloc_text(PAGE, UdfPurgeVolume)
  30. #endif
  31. VOID
  32. UdfCreateInternalStream (
  33. IN PIRP_CONTEXT IrpContext,
  34. IN PVCB Vcb,
  35. IN PFCB Fcb
  36. )
  37. /*++
  38. Routine Description:
  39. This function creates an internal stream file for interaction
  40. with the cache manager. The Fcb here will be for a directory
  41. stream.
  42. Arguments:
  43. Vcb - Vcb for this volume.
  44. Fcb - Points to the Fcb for this file. It is an Index Fcb.
  45. Return Value:
  46. None.
  47. --*/
  48. {
  49. PFILE_OBJECT StreamFile = NULL;
  50. BOOLEAN DecrementReference = FALSE;
  51. PAGED_CODE();
  52. //
  53. // Check inputs.
  54. //
  55. ASSERT_IRP_CONTEXT( IrpContext );
  56. ASSERT_FCB_INDEX( Fcb );
  57. //
  58. // We may only have the Fcb shared. Lock the Fcb and do a
  59. // safe test to see if we need to really create the file object.
  60. //
  61. UdfLockFcb( IrpContext, Fcb );
  62. if (Fcb->FileObject != NULL) {
  63. UdfUnlockFcb( IrpContext, Fcb );
  64. return;
  65. }
  66. //
  67. // Use a try-finally to facilitate cleanup.
  68. //
  69. try {
  70. //
  71. // Create the internal stream. The Vpb should be pointing at our volume
  72. // device object at this point.
  73. //
  74. StreamFile = IoCreateStreamFileObject( NULL, Vcb->Vpb->RealDevice );
  75. if (StreamFile == NULL) {
  76. UdfRaiseStatus( IrpContext, STATUS_INSUFFICIENT_RESOURCES );
  77. }
  78. //
  79. // Initialize the fields of the file object.
  80. //
  81. StreamFile->ReadAccess = TRUE;
  82. StreamFile->WriteAccess = FALSE;
  83. StreamFile->DeleteAccess = FALSE;
  84. StreamFile->SectionObjectPointer = &Fcb->FcbNonpaged->SegmentObject;
  85. //
  86. // Set the file object type and increment the Vcb counts.
  87. //
  88. UdfSetFileObject( IrpContext,
  89. StreamFile,
  90. StreamFileOpen,
  91. Fcb,
  92. NULL );
  93. //
  94. // We will reference the current Fcb twice to keep it from going
  95. // away in the error path. Otherwise if we dereference it
  96. // below in the finally clause a close could cause the Fcb to
  97. // be deallocated.
  98. //
  99. UdfLockVcb( IrpContext, Vcb );
  100. DebugTrace(( +1, Dbg,
  101. "UdfCreateInternalStream, Fcb %08x Vcb %d/%d Fcb %d/%d\n",
  102. Fcb,
  103. Vcb->VcbReference,
  104. Vcb->VcbUserReference,
  105. Fcb->FcbReference,
  106. Fcb->FcbUserReference ));
  107. UdfIncrementReferenceCounts( IrpContext, Fcb, 2, 0 );
  108. UdfUnlockVcb( IrpContext, Vcb );
  109. DecrementReference = TRUE;
  110. //
  111. // Initialize the cache map for the file.
  112. //
  113. CcInitializeCacheMap( StreamFile,
  114. (PCC_FILE_SIZES)&Fcb->AllocationSize,
  115. TRUE,
  116. &UdfData.CacheManagerCallbacks,
  117. Fcb );
  118. //
  119. // Go ahead and store the stream file into the Fcb.
  120. //
  121. Fcb->FileObject = StreamFile;
  122. StreamFile = NULL;
  123. } finally {
  124. DebugUnwind( "UdfCreateInternalStream" );
  125. //
  126. // If we raised then we need to dereference the file object.
  127. //
  128. if (StreamFile != NULL) {
  129. ObDereferenceObject( StreamFile );
  130. Fcb->FileObject = NULL;
  131. }
  132. //
  133. // Dereference and unlock the Fcb.
  134. //
  135. if (DecrementReference) {
  136. UdfLockVcb( IrpContext, Vcb );
  137. UdfDecrementReferenceCounts( IrpContext, Fcb, 1, 0 );
  138. DebugTrace(( -1, Dbg,
  139. "UdfCreateInternalStream, Vcb %d/%d Fcb %d/%d\n",
  140. Vcb->VcbReference,
  141. Vcb->VcbUserReference,
  142. Fcb->FcbReference,
  143. Fcb->FcbUserReference ));
  144. UdfUnlockVcb( IrpContext, Vcb );
  145. }
  146. UdfUnlockFcb( IrpContext, Fcb );
  147. }
  148. return;
  149. }
  150. VOID
  151. UdfDeleteInternalStream (
  152. IN PIRP_CONTEXT IrpContext,
  153. IN PFCB Fcb
  154. )
  155. /*++
  156. Routine Description:
  157. This function creates an internal stream file for interaction
  158. with the cache manager. The Fcb here can be for either a
  159. directory stream or for a metadata stream.
  160. Arguments:
  161. Fcb - Points to the Fcb for this file. It is either an Index or
  162. Metadata Fcb.
  163. Return Value:
  164. None.
  165. --*/
  166. {
  167. PFILE_OBJECT FileObject;
  168. PAGED_CODE();
  169. ASSERT_IRP_CONTEXT( IrpContext );
  170. ASSERT_FCB( Fcb );
  171. //
  172. // Lock the Fcb.
  173. //
  174. UdfLockFcb( IrpContext, Fcb );
  175. //
  176. // Capture the file object.
  177. //
  178. FileObject = Fcb->FileObject;
  179. Fcb->FileObject = NULL;
  180. //
  181. // It is now safe to unlock the Fcb.
  182. //
  183. UdfUnlockFcb( IrpContext, Fcb );
  184. //
  185. // Dereference the file object if present.
  186. //
  187. if (FileObject != NULL) {
  188. if (FileObject->PrivateCacheMap != NULL) {
  189. CcUninitializeCacheMap( FileObject, NULL, NULL );
  190. }
  191. ObDereferenceObject( FileObject );
  192. }
  193. return;
  194. }
  195. NTSTATUS
  196. UdfCompleteMdl (
  197. IN PIRP_CONTEXT IrpContext,
  198. IN PIRP Irp
  199. )
  200. /*++
  201. Routine Description:
  202. This routine performs the function of completing Mdl reads.
  203. It should be called only from UdfCommonRead.
  204. Arguments:
  205. Irp - Supplies the originating Irp.
  206. Return Value:
  207. NTSTATUS - Will always be STATUS_SUCCESS.
  208. --*/
  209. {
  210. PFILE_OBJECT FileObject;
  211. PAGED_CODE();
  212. //
  213. // Do completion processing.
  214. //
  215. FileObject = IoGetCurrentIrpStackLocation( Irp )->FileObject;
  216. CcMdlReadComplete( FileObject, Irp->MdlAddress );
  217. //
  218. // Mdl is now deallocated.
  219. //
  220. Irp->MdlAddress = NULL;
  221. //
  222. // Complete the request and exit right away.
  223. //
  224. UdfCompleteRequest( IrpContext, Irp, STATUS_SUCCESS );
  225. return STATUS_SUCCESS;
  226. }
  227. VOID
  228. UdfMapMetadataView (
  229. IN PIRP_CONTEXT IrpContext,
  230. IN PMAPPED_PVIEW View,
  231. IN PVCB Vcb,
  232. IN USHORT Partition,
  233. IN ULONG Lbn,
  234. IN ULONG Length,
  235. IN MAPMETAOP Operation
  236. )
  237. /*++
  238. Routine Description:
  239. Perform the common work of mapping an extent of metadata into a mapped view.
  240. Any existing view in the supplied MAPPED_VIEW is unmapped.
  241. Any single thread must only ever have ONE mapping ESTABLISHED through the
  242. Vmcb stream at any one time. Failure to observe this may result in deadlocks
  243. when the Vmcb package tries to extend an existing mapping and hence do a
  244. purge. I.e. no more than one MAPPED_VIEW should be in use (actually mapped)
  245. by any given thread at any moment.
  246. Acquires Vcb->VmcbMappingResource shared (will be held on return, except for
  247. INIT_ONLY operation). May acquire exclusive before shared if the mapping
  248. is not present in the vmcb, so calling threads must have no other active
  249. mappings through the vmcb stream.
  250. Arguments:
  251. View - View structure to map the bytes into
  252. Vcb - Vcb of the volume the extent is on
  253. Partition - Partition of the extent
  254. Lbn - Lbn of the extent
  255. Length - Length of the extent
  256. Operation - METAMAPOP_INIT_VIEW_ONLY - Just store the part/lbn/len. Doesn't
  257. access the vmcb, or do a CcMap.
  258. METAMAPOP_REMAP_VIEW - Do the CcMap through the vmcb using
  259. the partition/lbn/len already in
  260. the supplied view record
  261. METAMAPOP_INIT_AND_MAP - Does both of the above in sequence.
  262. Return Value:
  263. None.
  264. --*/
  265. {
  266. LARGE_INTEGER Offset;
  267. ASSERT_IRP_CONTEXT( IrpContext );
  268. //
  269. // Remove any existing mapping & release Vmcb mapping resource
  270. //
  271. UdfUnpinView( IrpContext, View );
  272. if ( METAMAPOP_REMAP_VIEW != Operation) {
  273. //
  274. // Update the view information if we're not remapping using the
  275. // existing values in the view record.
  276. //
  277. View->Partition = Partition;
  278. View->Lbn = Lbn;
  279. View->Length = Length;
  280. View->Vsn = UDF_INVALID_VSN;
  281. View->Bcb = View->View = NULL;
  282. }
  283. if ( METAMAPOP_INIT_VIEW_ONLY != Operation) {
  284. ASSERT_NOT_HELD_VMCB( Vcb);
  285. //
  286. // Find (or add) the mapping for this extent in the vmcb stream. We now
  287. // store the Vsn in the MAPPED_VIEW, so we don't have to do the lookup
  288. // again later (simplifies locking, amongst other things).
  289. //
  290. View->Vsn = UdfLookupMetaVsnOfExtent( IrpContext,
  291. Vcb,
  292. View->Partition,
  293. View->Lbn,
  294. View->Length,
  295. FALSE );
  296. Offset.QuadPart = LlBytesFromSectors( Vcb, View->Vsn );
  297. //
  298. // Map the extent. Acquire the vmcb map resource to synchronise against
  299. // purges of the vmcb stream. See comments in Vmcb code for more detail.
  300. //
  301. UdfAcquireVmcbForCcMap( IrpContext, Vcb);
  302. try {
  303. CcMapData( Vcb->MetadataFcb->FileObject,
  304. &Offset,
  305. View->Length,
  306. TRUE,
  307. &View->Bcb,
  308. &View->View );
  309. }
  310. finally {
  311. //
  312. // If this raised, we should release the mapping lock. Callers will
  313. // only cleanup and release if a non-NULL BCB is present in the pview.
  314. //
  315. if (AbnormalTermination()) {
  316. UdfReleaseVmcb( IrpContext, Vcb);
  317. View->View = View->Bcb = NULL;
  318. }
  319. else {
  320. ASSERT( View->View && View->Bcb);
  321. }
  322. }
  323. }
  324. }
  325. NTSTATUS
  326. UdfPurgeVolume (
  327. IN PIRP_CONTEXT IrpContext,
  328. IN PVCB Vcb,
  329. IN BOOLEAN DismountUnderway
  330. )
  331. /*++
  332. Routine Description:
  333. This routine is called to purge the volume. The purpose is to make all the stale file
  334. objects in the system go away, minimizing the reference counts, so that the volume may
  335. be locked or deleted.
  336. The Vcb is already acquired exclusively. We will lock out all file operations by
  337. acquiring the global file resource. Then we will walk through all of the Fcb's and
  338. perform the purge.
  339. Arguments:
  340. Vcb - Vcb for the volume to purge.
  341. DismountUnderway - Indicates that we are trying to delete all of the objects.
  342. We will purge the Metadata and VolumeDasd and dereference all internal streams.
  343. Return Value:
  344. NTSTATUS - The first failure of the purge operation.
  345. --*/
  346. {
  347. NTSTATUS Status = STATUS_SUCCESS;
  348. PVOID RestartKey = NULL;
  349. PFCB ThisFcb = NULL;
  350. PFCB NextFcb;
  351. BOOLEAN RemovedFcb;
  352. PAGED_CODE();
  353. ASSERT_EXCLUSIVE_VCB( Vcb);
  354. //
  355. // Force any remaining Fcb's in the delayed close queue to be closed.
  356. //
  357. UdfFspClose( Vcb );
  358. //
  359. // Acquire the global file resource.
  360. //
  361. UdfAcquireAllFiles( IrpContext, Vcb );
  362. //
  363. // Loop through each Fcb in the Fcb Table and perform the flush.
  364. //
  365. while (TRUE) {
  366. //
  367. // Lock the Vcb to lookup the next Fcb.
  368. //
  369. UdfLockVcb( IrpContext, Vcb );
  370. NextFcb = UdfGetNextFcb( IrpContext, Vcb, &RestartKey );
  371. //
  372. // Reference the NextFcb if present.
  373. //
  374. if (NextFcb != NULL) {
  375. NextFcb->FcbReference += 1;
  376. }
  377. //
  378. // If the last Fcb is present then decrement reference count and call teardown
  379. // to see if it should be removed.
  380. //
  381. if (ThisFcb != NULL) {
  382. ThisFcb->FcbReference -= 1;
  383. UdfUnlockVcb( IrpContext, Vcb );
  384. UdfTeardownStructures( IrpContext, ThisFcb, FALSE, &RemovedFcb );
  385. } else {
  386. UdfUnlockVcb( IrpContext, Vcb );
  387. }
  388. //
  389. // Break out of the loop if no more Fcb's.
  390. //
  391. if (NextFcb == NULL) {
  392. break;
  393. }
  394. //
  395. // Move to the next Fcb.
  396. //
  397. ThisFcb = NextFcb;
  398. //
  399. // If there is a image section then see if that can be closed.
  400. //
  401. if (ThisFcb->FcbNonpaged->SegmentObject.ImageSectionObject != NULL) {
  402. MmFlushImageSection( &ThisFcb->FcbNonpaged->SegmentObject, MmFlushForWrite );
  403. }
  404. //
  405. // If there is a data section then purge this. If there is an image
  406. // section then we won't be able to. Remember this if it is our first
  407. // error.
  408. //
  409. if ((ThisFcb->FcbNonpaged->SegmentObject.DataSectionObject != NULL) &&
  410. !CcPurgeCacheSection( &ThisFcb->FcbNonpaged->SegmentObject,
  411. NULL,
  412. 0,
  413. FALSE ) &&
  414. (Status == STATUS_SUCCESS)) {
  415. Status = STATUS_UNABLE_TO_DELETE_SECTION;
  416. }
  417. //
  418. // Dereference the internal stream if dismounting.
  419. //
  420. if (DismountUnderway &&
  421. (SafeNodeType( ThisFcb ) != UDFS_NTC_FCB_DATA) &&
  422. (ThisFcb->FileObject != NULL)) {
  423. UdfDeleteInternalStream( IrpContext, ThisFcb );
  424. }
  425. }
  426. //
  427. // Now look at the Root Index, Metadata, Volume Dasd and VAT Fcbs.
  428. // Note that we usually hit the Root Index in the loop above, but
  429. // it is possible miss it if it didn't get into the Fcb table in the
  430. // first place!
  431. //
  432. if (DismountUnderway) {
  433. if (Vcb->RootIndexFcb != NULL) {
  434. ThisFcb = Vcb->RootIndexFcb;
  435. InterlockedIncrement( &ThisFcb->FcbReference );
  436. if ((ThisFcb->FcbNonpaged->SegmentObject.DataSectionObject != NULL) &&
  437. !CcPurgeCacheSection( &ThisFcb->FcbNonpaged->SegmentObject,
  438. NULL,
  439. 0,
  440. FALSE ) &&
  441. (Status == STATUS_SUCCESS)) {
  442. Status = STATUS_UNABLE_TO_DELETE_SECTION;
  443. }
  444. UdfDeleteInternalStream( IrpContext, ThisFcb );
  445. InterlockedDecrement( &ThisFcb->FcbReference );
  446. UdfTeardownStructures( IrpContext, ThisFcb, FALSE, &RemovedFcb );
  447. }
  448. if (Vcb->MetadataFcb != NULL) {
  449. ThisFcb = Vcb->MetadataFcb;
  450. InterlockedIncrement( &ThisFcb->FcbReference );
  451. if ((ThisFcb->FcbNonpaged->SegmentObject.DataSectionObject != NULL) &&
  452. !CcPurgeCacheSection( &ThisFcb->FcbNonpaged->SegmentObject,
  453. NULL,
  454. 0,
  455. FALSE ) &&
  456. (Status == STATUS_SUCCESS)) {
  457. Status = STATUS_UNABLE_TO_DELETE_SECTION;
  458. }
  459. UdfDeleteInternalStream( IrpContext, ThisFcb );
  460. InterlockedDecrement( &ThisFcb->FcbReference );
  461. UdfTeardownStructures( IrpContext, ThisFcb, FALSE, &RemovedFcb );
  462. }
  463. if (Vcb->VatFcb != NULL) {
  464. ThisFcb = Vcb->VatFcb;
  465. InterlockedIncrement( &ThisFcb->FcbReference );
  466. if ((ThisFcb->FcbNonpaged->SegmentObject.DataSectionObject != NULL) &&
  467. !CcPurgeCacheSection( &ThisFcb->FcbNonpaged->SegmentObject,
  468. NULL,
  469. 0,
  470. FALSE ) &&
  471. (Status == STATUS_SUCCESS)) {
  472. Status = STATUS_UNABLE_TO_DELETE_SECTION;
  473. }
  474. UdfDeleteInternalStream( IrpContext, ThisFcb );
  475. InterlockedDecrement( &ThisFcb->FcbReference );
  476. UdfTeardownStructures( IrpContext, ThisFcb, FALSE, &RemovedFcb );
  477. }
  478. if (Vcb->VolumeDasdFcb != NULL) {
  479. ThisFcb = Vcb->VolumeDasdFcb;
  480. InterlockedIncrement( &ThisFcb->FcbReference );
  481. if ((ThisFcb->FcbNonpaged->SegmentObject.DataSectionObject != NULL) &&
  482. !CcPurgeCacheSection( &ThisFcb->FcbNonpaged->SegmentObject,
  483. NULL,
  484. 0,
  485. FALSE ) &&
  486. (Status == STATUS_SUCCESS)) {
  487. Status = STATUS_UNABLE_TO_DELETE_SECTION;
  488. }
  489. InterlockedDecrement( &ThisFcb->FcbReference );
  490. UdfTeardownStructures( IrpContext, ThisFcb, FALSE, &RemovedFcb );
  491. }
  492. }
  493. //
  494. // Release all of the files.
  495. //
  496. UdfReleaseAllFiles( IrpContext, Vcb );
  497. return Status;
  498. }