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.

243 lines
6.6 KiB

  1. /*++
  2. Copyright (c) 1991 Microsoft Corporation
  3. Module Name:
  4. blcache.h
  5. Abstract:
  6. This module declares parameters, data structures and functions for
  7. a general purpose disk caching for the boot loader. Although it is
  8. mostly general purpose, it is mainly used for caching filesystem
  9. metadata, since that is the only frequently reaccessed data in the
  10. boot loader. In order to use caching on a device, you must make
  11. sure that there is only one unique BlFileTable entry for that
  12. device and the same device is not opened & cached simultaneously
  13. multiple times under different device ids. Otherwise there will be
  14. cache inconsistencies, since cached data and structures are
  15. maintained based on device id. Also you must make sure to stop
  16. caching when the device is closed.
  17. Author:
  18. Cenk Ergan (cenke) 14-Jan-2000
  19. Revision History:
  20. --*/
  21. #ifndef _BLCACHE_H
  22. #define _BLCACHE_H
  23. #include "bldr.h"
  24. #include "blrange.h"
  25. //
  26. // Define boot loader disk cache parameters and data structures.
  27. //
  28. //
  29. // Currently device id - cache header pairing is done using a global
  30. // table. This determines maximum number of entries in this table. We
  31. // really need just two entries, one for LoadDevice, one for
  32. // SystemDevice. Caches for all devices share the same resources
  33. // [e.g. cache blocks and Most Recently Used list etc.]
  34. //
  35. #define BL_DISKCACHE_DEVICE_TABLE_SIZE 2
  36. //
  37. // Size of ranges that are cached. We read & cache fixed size blocks
  38. // from the devices which makes memory management easy, since we can't
  39. // use boot loader's: there is no HeapFree, just HeapAlloc! BLOCK_SIZE
  40. // has to be a power of two for alignment arithmetic to work.
  41. //
  42. #define BL_DISKCACHE_BLOCK_SIZE (32 * 1024)
  43. //
  44. // Maximum number of bytes cached in the disk cache [i.e. maximum size
  45. // disk cache may grow to]. This should be a multiple of BLOCK_SIZE below.
  46. //
  47. #define BL_DISKCACHE_SIZE (64 * BL_DISKCACHE_BLOCK_SIZE)
  48. //
  49. // Maximum number of cache blocks / range entries there can be in the
  50. // cache. There will be a range entry for each block, i.e. it should
  51. // be cache size / block size.
  52. //
  53. #define BL_DISKCACHE_NUM_BLOCKS (BL_DISKCACHE_SIZE / BL_DISKCACHE_BLOCK_SIZE)
  54. //
  55. // Size of the buffer needed for storing maximum number of overlapping
  56. // or distinct range entries for a 64KBs request given BLOCK_SIZE. We
  57. // reserve this on the stack [i.e. buffer is a local variable] in
  58. // BlDiskCacheRead: make sure it is not too big! We base it assuming
  59. // that distincts buffer will be larger [since it is BLCRANGE entries
  60. // instead where as overlaps buffer contains BLCRANGE_ENTRY pointers.]
  61. //
  62. #define BL_DISKCACHE_FIND_RANGES_BUF_SIZE \
  63. (((64 * 1024 / BL_DISKCACHE_BLOCK_SIZE) + 3) * (sizeof(BLCRANGE)))
  64. //
  65. // This is the header for the cache for a particular device. The
  66. // cached ranges on this device are stored in the Ranges list.
  67. //
  68. typedef struct _BL_DISK_SUBCACHE
  69. {
  70. BOOLEAN Initialized;
  71. ULONG DeviceId;
  72. BLCRANGE_LIST Ranges;
  73. } BL_DISK_SUBCACHE, *PBL_DISK_SUBCACHE;
  74. //
  75. // Define structure for the global boot loader diskcache.
  76. //
  77. typedef struct _BL_DISKCACHE
  78. {
  79. //
  80. // Table that contains cache headers. Cache - DeviceId pairing is
  81. // also done using this table.
  82. //
  83. BL_DISK_SUBCACHE DeviceTable[BL_DISKCACHE_DEVICE_TABLE_SIZE];
  84. //
  85. // Most recently used list for cached blocks [range
  86. // entries]. Entries are linked through the UserLink field in the
  87. // BLCRANGE_ENTRY. The least recently used block is at the very
  88. // end. Any entries placed on a cache's range list is also put on
  89. // this list. When removing a range from a cache's range list, the
  90. // callback removes it from this list too. BlDiskCacheRead updates
  91. // this list and prunes it if normal cache entry allocation fails.
  92. //
  93. LIST_ENTRY MRUBlockList;
  94. //
  95. // This is where cached data is stored. Its size is
  96. // BL_DISKCACHE_SIZE. It is divided up into BL_DISKCACHE_NUM_BLOCKS
  97. // blocks. Nth block belongs to Nth entry in the EntryBuffer.
  98. //
  99. PUCHAR DataBuffer;
  100. //
  101. // Range entries to be put into the cache's range lists are
  102. // allocated from here. It has BL_DISKCACHE_NUM_BLOCKS elements.
  103. //
  104. PBLCRANGE_ENTRY EntryBuffer;
  105. //
  106. // This array is used for keeping track of the free range entries
  107. // in EntryBuffer. Free range entries get linked up on this list
  108. // using the UserLink field. Once the entry is allocated, the
  109. // UserLink field is usually used to link it up to the MRU list.
  110. //
  111. LIST_ENTRY FreeEntryList;
  112. //
  113. // Keep track of whether we are initialized or not.
  114. //
  115. BOOLEAN Initialized;
  116. } BL_DISKCACHE, *PBL_DISKCACHE;
  117. //
  118. // Declare global variables.
  119. //
  120. //
  121. // This is the boot loader disk cache with all its bells and whistles.
  122. //
  123. extern BL_DISKCACHE BlDiskCache;
  124. //
  125. // Debug defines. Use these to actively debug the disk cache. These
  126. // are not turned on for checked build, because they would spew out
  127. // too much to the console.
  128. //
  129. #ifdef BL_DISKCACHE_DEBUG
  130. #define DPRINT(_x) DbgPrint _x;
  131. #define DASSERT(_c) do { if (_c) DbgBreakPoint(); } while (0);
  132. #else // BL_DISKCACHE_DEBUG
  133. #define DPRINT(_x)
  134. #define DASSERT(_c)
  135. #endif // BL_DISKCACHE_DEBUG
  136. //
  137. // These two defines are used as the last parameter to BlDiskCacheRead.
  138. // They specify whether any new data read from the disk should be put
  139. // into the disk cache or not. In the boot loader file systems we usually
  140. // choose to cache new data if we are reading metadata, and not choose to
  141. // cache it if we are reading file data.
  142. //
  143. #define CACHE_NEW_DATA (TRUE)
  144. #define DONT_CACHE_NEW_DATA (FALSE)
  145. //
  146. // Useful macros. Be mindful of expression reevaluation as with
  147. // all macros.
  148. //
  149. #define BLCMIN(a,b) (((a) <= (b)) ? (a) : (b))
  150. #define BLCMAX(a,b) (((a) >= (b)) ? (a) : (b))
  151. //
  152. // Cache function prototypes. See ntos\boot\lib\blcache.c for comments
  153. // and implementation.
  154. //
  155. ARC_STATUS
  156. BlDiskCacheInitialize(
  157. VOID
  158. );
  159. VOID
  160. BlDiskCacheUninitialize(
  161. VOID
  162. );
  163. PBL_DISK_SUBCACHE
  164. BlDiskCacheStartCachingOnDevice(
  165. ULONG DeviceId
  166. );
  167. VOID
  168. BlDiskCacheStopCachingOnDevice(
  169. ULONG DeviceId
  170. );
  171. ARC_STATUS
  172. BlDiskCacheRead (
  173. ULONG DeviceId,
  174. PLARGE_INTEGER pOffset,
  175. PVOID Buffer,
  176. ULONG Length,
  177. PULONG pCount,
  178. BOOLEAN CacheNewData
  179. );
  180. ARC_STATUS
  181. BlDiskCacheWrite (
  182. ULONG DeviceId,
  183. PLARGE_INTEGER pOffset,
  184. PVOID Buffer,
  185. ULONG Length,
  186. PULONG pCount
  187. );
  188. #endif // _BLCACHE_H