DiskCache.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541
  1. /*++
  2. Copyright (c) 2005 - 2007, Intel Corporation. All rights reserved.<BR>
  3. This program and the accompanying materials are licensed and made available
  4. under the terms and conditions of the BSD License which accompanies this
  5. distribution. The full text of the license may be found at
  6. http://opensource.org/licenses/bsd-license.php
  7. THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
  8. WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
  9. Module Name:
  10. DiskCache.c
  11. Abstract:
  12. Cache implementation for EFI FAT File system driver
  13. Revision History
  14. --*/
  15. #include "Fat.h"
  16. STATIC
  17. VOID
  18. FatFlushDataCacheRange (
  19. IN FAT_VOLUME *Volume,
  20. IN IO_MODE IoMode,
  21. IN UINTN StartPageNo,
  22. IN UINTN EndPageNo,
  23. OUT UINT8 *Buffer
  24. )
  25. /*++
  26. Routine Description:
  27. This function is used by the Data Cache.
  28. When this function is called by write command, all entries in this range
  29. are older than the contents in disk, so they are invalid; just mark them invalid.
  30. When this function is called by read command, if any entry in this range
  31. is dirty, it means that the relative info directly readed from media is older than
  32. than the info in the cache; So need to update the relative info in the Buffer.
  33. Arguments:
  34. Volume - FAT file system volume.
  35. IoMode - This function is called by read command or write command
  36. StartPageNo - First PageNo to be checked in the cache.
  37. EndPageNo - Last PageNo to be checked in the cache.
  38. Buffer - The user buffer need to update. Only when doing the read command
  39. and there is dirty cache in the cache range, this parameter will be used.
  40. Returns:
  41. None.
  42. --*/
  43. {
  44. UINTN PageNo;
  45. UINTN GroupNo;
  46. UINTN GroupMask;
  47. UINTN PageSize;
  48. UINT8 PageAlignment;
  49. DISK_CACHE *DiskCache;
  50. CACHE_TAG *CacheTag;
  51. UINT8 *BaseAddress;
  52. DiskCache = &Volume->DiskCache[CACHE_DATA];
  53. BaseAddress = DiskCache->CacheBase;
  54. GroupMask = DiskCache->GroupMask;
  55. PageAlignment = DiskCache->PageAlignment;
  56. PageSize = (UINTN)1 << PageAlignment;
  57. for (PageNo = StartPageNo; PageNo < EndPageNo; PageNo++) {
  58. GroupNo = PageNo & GroupMask;
  59. CacheTag = &DiskCache->CacheTag[GroupNo];
  60. if (CacheTag->RealSize > 0 && CacheTag->PageNo == PageNo) {
  61. //
  62. // When reading data form disk directly, if some dirty data
  63. // in cache is in this rang, this data in the Buffer need to
  64. // be updated with the cache's dirty data.
  65. //
  66. if (IoMode == READ_DISK) {
  67. if (CacheTag->Dirty) {
  68. CopyMem (
  69. Buffer + ((PageNo - StartPageNo) << PageAlignment),
  70. BaseAddress + (GroupNo << PageAlignment),
  71. PageSize
  72. );
  73. }
  74. } else {
  75. //
  76. // Make all valid entries in this range invalid.
  77. //
  78. CacheTag->RealSize = 0;
  79. }
  80. }
  81. }
  82. }
  83. STATIC
  84. EFI_STATUS
  85. FatExchangeCachePage (
  86. IN FAT_VOLUME *Volume,
  87. IN CACHE_DATA_TYPE DataType,
  88. IN IO_MODE IoMode,
  89. IN CACHE_TAG *CacheTag
  90. )
  91. /*++
  92. Routine Description:
  93. Exchange the cache page with the image on the disk
  94. Arguments:
  95. Volume - FAT file system volume.
  96. DataType - Indicate the cache type.
  97. IoMode - Indicate whether to load this page from disk or store this page to disk.
  98. CacheTag - The Cache Tag for the current cache page.
  99. Returns:
  100. EFI_SUCCESS - Cache page exchanged successfully.
  101. Others - An error occurred when exchanging cache page.
  102. --*/
  103. {
  104. EFI_STATUS Status;
  105. UINTN GroupNo;
  106. UINTN PageNo;
  107. UINTN WriteCount;
  108. UINTN RealSize;
  109. UINT64 EntryPos;
  110. UINT64 MaxSize;
  111. DISK_CACHE *DiskCache;
  112. VOID *PageAddress;
  113. UINT8 PageAlignment;
  114. DiskCache = &Volume->DiskCache[DataType];
  115. PageNo = CacheTag->PageNo;
  116. GroupNo = PageNo & DiskCache->GroupMask;
  117. PageAlignment = DiskCache->PageAlignment;
  118. PageAddress = DiskCache->CacheBase + (GroupNo << PageAlignment);
  119. EntryPos = DiskCache->BaseAddress + LShiftU64 (PageNo, PageAlignment);
  120. RealSize = CacheTag->RealSize;
  121. if (IoMode == READ_DISK) {
  122. RealSize = (UINTN)1 << PageAlignment;
  123. MaxSize = DiskCache->LimitAddress - EntryPos;
  124. if (MaxSize < RealSize) {
  125. DEBUG ((EFI_D_INFO, "FatDiskIo: Cache Page OutBound occurred! \n"));
  126. RealSize = (UINTN) MaxSize;
  127. }
  128. }
  129. WriteCount = 1;
  130. if (DataType == CACHE_FAT && IoMode == WRITE_DISK) {
  131. WriteCount = Volume->NumFats;
  132. }
  133. do {
  134. //
  135. // Only fat table writing will execute more than once
  136. //
  137. Status = FatDiskIo (Volume, IoMode, EntryPos, RealSize, PageAddress);
  138. if (EFI_ERROR (Status)) {
  139. return Status;
  140. }
  141. EntryPos += Volume->FatSize;
  142. } while (--WriteCount > 0);
  143. CacheTag->Dirty = FALSE;
  144. CacheTag->RealSize = RealSize;
  145. return EFI_SUCCESS;
  146. }
  147. STATIC
  148. EFI_STATUS
  149. FatGetCachePage (
  150. IN FAT_VOLUME *Volume,
  151. IN CACHE_DATA_TYPE CacheDataType,
  152. IN UINTN PageNo,
  153. IN CACHE_TAG *CacheTag
  154. )
  155. /*++
  156. Routine Description:
  157. Get one cache page by specified PageNo.
  158. Arguments:
  159. Volume - FAT file system volume.
  160. CacheDataType - The cache type: CACHE_FAT or CACHE_DATA.
  161. PageNo - PageNo to match with the cache.
  162. CacheTag - The Cache Tag for the current cache page.
  163. Returns:
  164. EFI_SUCCESS - Get the cache page successfully.
  165. other - An error occurred when accessing data.
  166. --*/
  167. {
  168. EFI_STATUS Status;
  169. UINTN OldPageNo;
  170. OldPageNo = CacheTag->PageNo;
  171. if (CacheTag->RealSize > 0 && OldPageNo == PageNo) {
  172. //
  173. // Cache Hit occurred
  174. //
  175. return EFI_SUCCESS;
  176. }
  177. //
  178. // Write dirty cache page back to disk
  179. //
  180. if (CacheTag->RealSize > 0 && CacheTag->Dirty) {
  181. Status = FatExchangeCachePage (Volume, CacheDataType, WRITE_DISK, CacheTag);
  182. if (EFI_ERROR (Status)) {
  183. return Status;
  184. }
  185. }
  186. //
  187. // Load new data from disk;
  188. //
  189. CacheTag->PageNo = PageNo;
  190. Status = FatExchangeCachePage (Volume, CacheDataType, READ_DISK, CacheTag);
  191. return Status;
  192. }
  193. STATIC
  194. EFI_STATUS
  195. FatAccessUnalignedCachePage (
  196. IN FAT_VOLUME *Volume,
  197. IN CACHE_DATA_TYPE CacheDataType,
  198. IN IO_MODE IoMode,
  199. IN UINTN PageNo,
  200. IN UINTN Offset,
  201. IN UINTN Length,
  202. IN OUT VOID *Buffer
  203. )
  204. /*++
  205. Routine Description:
  206. Read Length bytes from the position of Offset into Buffer, or
  207. write Length bytes from Buffer into the position of Offset.
  208. Arguments:
  209. Volume - FAT file system volume.
  210. CacheDataType - The type of cache: CACHE_DATA or CACHE_FAT.
  211. IoMode - Indicate the type of disk access.
  212. PageNo - The number of unaligned cache page.
  213. Offset - The starting byte of cache page.
  214. Length - The number of bytes that is read or written
  215. Buffer - Buffer containing cache data.
  216. Returns:
  217. EFI_SUCCESS - The data was accessed correctly.
  218. Others - An error occurred when accessing unaligned cache page.
  219. --*/
  220. {
  221. EFI_STATUS Status;
  222. VOID *Source;
  223. VOID *Destination;
  224. DISK_CACHE *DiskCache;
  225. CACHE_TAG *CacheTag;
  226. UINTN GroupNo;
  227. DiskCache = &Volume->DiskCache[CacheDataType];
  228. GroupNo = PageNo & DiskCache->GroupMask;
  229. CacheTag = &DiskCache->CacheTag[GroupNo];
  230. Status = FatGetCachePage (Volume, CacheDataType, PageNo, CacheTag);
  231. if (!EFI_ERROR (Status)) {
  232. Source = DiskCache->CacheBase + (GroupNo << DiskCache->PageAlignment) + Offset;
  233. Destination = Buffer;
  234. if (IoMode != READ_DISK) {
  235. CacheTag->Dirty = TRUE;
  236. DiskCache->Dirty = TRUE;
  237. Destination = Source;
  238. Source = Buffer;
  239. }
  240. CopyMem (Destination, Source, Length);
  241. }
  242. return Status;
  243. }
  244. EFI_STATUS
  245. FatAccessCache (
  246. IN FAT_VOLUME *Volume,
  247. IN CACHE_DATA_TYPE CacheDataType,
  248. IN IO_MODE IoMode,
  249. IN UINT64 Offset,
  250. IN UINTN BufferSize,
  251. IN OUT UINT8 *Buffer
  252. )
  253. /*++
  254. Routine Description:
  255. Read BufferSize bytes from the position of Offset into Buffer,
  256. or write BufferSize bytes from Buffer into the position of Offset.
  257. Base on the parameter of CACHE_DATA_TYPE, the data access will be divided into
  258. the access of FAT cache (CACHE_FAT) and the access of Data cache (CACHE_DATA):
  259. 1. Access of FAT cache (CACHE_FAT): Access the data in the FAT cache, if there is cache
  260. page hit, just return the cache page; else update the related cache page and return
  261. the right cache page.
  262. 2. Access of Data cache (CACHE_DATA):
  263. The access data will be divided into UnderRun data, Aligned data and OverRun data;
  264. The UnderRun data and OverRun data will be accessed by the Data cache,
  265. but the Aligned data will be accessed with disk directly.
  266. Arguments:
  267. Volume - FAT file system volume.
  268. CacheDataType - The type of cache: CACHE_DATA or CACHE_FAT.
  269. IoMode - Indicate the type of disk access.
  270. Offset - The starting byte offset to read from.
  271. BufferSize - Size of Buffer.
  272. Buffer - Buffer containing cache data.
  273. Returns:
  274. EFI_SUCCESS - The data was accessed correctly.
  275. EFI_MEDIA_CHANGED - The MediaId does not match the current device.
  276. Others - An error occurred when accessing cache.
  277. --*/
  278. {
  279. EFI_STATUS Status;
  280. UINTN PageSize;
  281. UINTN UnderRun;
  282. UINTN OverRun;
  283. UINTN AlignedSize;
  284. UINTN Length;
  285. UINTN PageNo;
  286. UINTN AlignedPageCount;
  287. UINTN OverRunPageNo;
  288. DISK_CACHE *DiskCache;
  289. UINT64 EntryPos;
  290. UINT8 PageAlignment;
  291. ASSERT (Volume->CacheBuffer != NULL);
  292. Status = EFI_SUCCESS;
  293. DiskCache = &Volume->DiskCache[CacheDataType];
  294. EntryPos = Offset - DiskCache->BaseAddress;
  295. PageAlignment = DiskCache->PageAlignment;
  296. PageSize = (UINTN)1 << PageAlignment;
  297. PageNo = (UINTN) RShiftU64 (EntryPos, PageAlignment);
  298. UnderRun = ((UINTN) EntryPos) & (PageSize - 1);
  299. if (UnderRun > 0) {
  300. Length = PageSize - UnderRun;
  301. if (Length > BufferSize) {
  302. Length = BufferSize;
  303. }
  304. Status = FatAccessUnalignedCachePage (Volume, CacheDataType, IoMode, PageNo, UnderRun, Length, Buffer);
  305. if (EFI_ERROR (Status)) {
  306. return Status;
  307. }
  308. Buffer += Length;
  309. BufferSize -= Length;
  310. PageNo++;
  311. }
  312. AlignedPageCount = BufferSize >> PageAlignment;
  313. OverRunPageNo = PageNo + AlignedPageCount;
  314. //
  315. // The access of the Aligned data
  316. //
  317. if (AlignedPageCount > 0) {
  318. //
  319. // Accessing fat table cannot have alignment data
  320. //
  321. ASSERT (CacheDataType == CACHE_DATA);
  322. EntryPos = Volume->RootPos + LShiftU64 (PageNo, PageAlignment);
  323. AlignedSize = AlignedPageCount << PageAlignment;
  324. Status = FatDiskIo (Volume, IoMode, EntryPos, AlignedSize, Buffer);
  325. if (EFI_ERROR (Status)) {
  326. return Status;
  327. }
  328. //
  329. // If these access data over laps the relative cache range, these cache pages need
  330. // to be updated.
  331. //
  332. FatFlushDataCacheRange (Volume, IoMode, PageNo, OverRunPageNo, Buffer);
  333. Buffer += AlignedSize;
  334. BufferSize -= AlignedSize;
  335. }
  336. //
  337. // The access of the OverRun data
  338. //
  339. OverRun = BufferSize;
  340. if (OverRun > 0) {
  341. //
  342. // Last read is not a complete page
  343. //
  344. Status = FatAccessUnalignedCachePage (Volume, CacheDataType, IoMode, OverRunPageNo, 0, OverRun, Buffer);
  345. }
  346. return Status;
  347. }
  348. EFI_STATUS
  349. FatVolumeFlushCache (
  350. IN FAT_VOLUME *Volume
  351. )
  352. /*++
  353. Routine Description:
  354. Flush all the dirty cache back, include the FAT cache and the Data cache.
  355. Arguments:
  356. Volume - FAT file system volume.
  357. Returns:
  358. EFI_SUCCESS - Flush all the dirty cache back successfully
  359. other - An error occurred when writing the data into the disk
  360. --*/
  361. {
  362. EFI_STATUS Status;
  363. CACHE_DATA_TYPE CacheDataType;
  364. UINTN GroupIndex;
  365. UINTN GroupMask;
  366. DISK_CACHE *DiskCache;
  367. CACHE_TAG *CacheTag;
  368. for (CacheDataType = (CACHE_DATA_TYPE) 0; CacheDataType < CACHE_MAX_TYPE; CacheDataType++) {
  369. DiskCache = &Volume->DiskCache[CacheDataType];
  370. if (DiskCache->Dirty) {
  371. //
  372. // Data cache or fat cache is dirty, write the dirty data back
  373. //
  374. GroupMask = DiskCache->GroupMask;
  375. for (GroupIndex = 0; GroupIndex <= GroupMask; GroupIndex++) {
  376. CacheTag = &DiskCache->CacheTag[GroupIndex];
  377. if (CacheTag->RealSize > 0 && CacheTag->Dirty) {
  378. //
  379. // Write back all Dirty Data Cache Page to disk
  380. //
  381. Status = FatExchangeCachePage (Volume, CacheDataType, WRITE_DISK, CacheTag);
  382. if (EFI_ERROR (Status)) {
  383. return Status;
  384. }
  385. }
  386. }
  387. DiskCache->Dirty = FALSE;
  388. }
  389. }
  390. //
  391. // Flush the block device.
  392. //
  393. Status = Volume->BlockIo->FlushBlocks (Volume->BlockIo);
  394. return Status;
  395. }
  396. EFI_STATUS
  397. FatInitializeDiskCache (
  398. IN FAT_VOLUME *Volume
  399. )
  400. /*++
  401. Routine Description:
  402. Initialize the disk cache according to Volume's FatType.
  403. Arguments:
  404. Volume - FAT file system volume.
  405. Returns:
  406. EFI_SUCCESS - The disk cache is successfully initialized.
  407. EFI_OUT_OF_RESOURCES - Not enough memory to allocate disk cache.
  408. --*/
  409. {
  410. DISK_CACHE *DiskCache;
  411. UINTN FatCacheGroupCount;
  412. UINTN DataCacheSize;
  413. UINTN FatCacheSize;
  414. UINT8 *CacheBuffer;
  415. DiskCache = Volume->DiskCache;
  416. //
  417. // Configure the parameters of disk cache
  418. //
  419. if (Volume->FatType == FAT12) {
  420. FatCacheGroupCount = FAT_FATCACHE_GROUP_MIN_COUNT;
  421. DiskCache[CACHE_FAT].PageAlignment = FAT_FATCACHE_PAGE_MIN_ALIGNMENT;
  422. DiskCache[CACHE_DATA].PageAlignment = FAT_DATACACHE_PAGE_MIN_ALIGNMENT;
  423. } else {
  424. FatCacheGroupCount = FAT_FATCACHE_GROUP_MAX_COUNT;
  425. DiskCache[CACHE_FAT].PageAlignment = FAT_FATCACHE_PAGE_MAX_ALIGNMENT;
  426. DiskCache[CACHE_DATA].PageAlignment = FAT_DATACACHE_PAGE_MAX_ALIGNMENT;
  427. }
  428. DiskCache[CACHE_DATA].GroupMask = FAT_DATACACHE_GROUP_COUNT - 1;
  429. DiskCache[CACHE_DATA].BaseAddress = Volume->RootPos;
  430. DiskCache[CACHE_DATA].LimitAddress = Volume->VolumeSize;
  431. DiskCache[CACHE_FAT].GroupMask = FatCacheGroupCount - 1;
  432. DiskCache[CACHE_FAT].BaseAddress = Volume->FatPos;
  433. DiskCache[CACHE_FAT].LimitAddress = Volume->FatPos + Volume->FatSize;
  434. FatCacheSize = FatCacheGroupCount << DiskCache[CACHE_FAT].PageAlignment;
  435. DataCacheSize = FAT_DATACACHE_GROUP_COUNT << DiskCache[CACHE_DATA].PageAlignment;
  436. //
  437. // Allocate the Fat Cache buffer
  438. //
  439. CacheBuffer = AllocatePool (FatCacheSize + DataCacheSize);
  440. if (CacheBuffer == NULL) {
  441. return EFI_OUT_OF_RESOURCES;
  442. }
  443. Volume->CacheBuffer = CacheBuffer;
  444. DiskCache[CACHE_FAT].CacheBase = CacheBuffer;
  445. DiskCache[CACHE_DATA].CacheBase = CacheBuffer + FatCacheSize;
  446. return EFI_SUCCESS;
  447. }