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.

340 lines
8.4 KiB

  1. //***************************************************************************
  2. //
  3. // (c) 2001 by Microsoft Corp. All Rights Reserved.
  4. //
  5. // PAGEMGR.H
  6. //
  7. // Declarations for CPageFile, CPageSource for WMI repository for
  8. // Windows XP.
  9. //
  10. // 21-Feb-01 raymcc
  11. //
  12. //***************************************************************************
  13. #ifndef _PAGEMGR_H_
  14. #define _PAGEMGR_H_
  15. #define WMIREP_PAGE_SIZE (8192)
  16. #define WMIREP_INVALID_PAGE 0xFFFFFFFF
  17. #define WMIREP_RESERVED_PAGE 0xFFFFFFFE
  18. #define WMIREP_OBJECT_DATA L"OBJECTS.DATA"
  19. #define WMIREP_BTREE_DATA L"INDEX.BTR"
  20. #define WMIREP_MAP_1 L"MAPPING1.MAP"
  21. #define WMIREP_MAP_2 L"MAPPING2.MAP"
  22. #define WMIREP_MAP_VER L"MAPPING.VER"
  23. //Old files for upgrade purpose only
  24. #define WMIREP_OBJECT_MAP L"OBJECTS.MAP"
  25. #define WMIREP_OBJECT_MAP_NEW L"OBJECTS.MAP.NEW"
  26. #define WMIREP_BTREE_MAP L"INDEX.MAP"
  27. #define WMIREP_BTREE_MAP_NEW L"INDEX.MAP.NEW"
  28. #define WMIREP_ROLL_FORWARD L"ROLL_FORWARD"
  29. #include <vector>
  30. #include <wstlallc.h>
  31. #include <wstring.h>
  32. #include <sync.h>
  33. typedef LONG NTSTATUS;
  34. struct SCachePage
  35. {
  36. BOOL m_bDirty;
  37. DWORD m_dwPhysId;
  38. LPBYTE m_pPage;
  39. SCachePage() { m_bDirty = 0; m_dwPhysId = 0; m_pPage = 0; }
  40. ~SCachePage() { if (m_pPage) delete [] m_pPage; }
  41. };
  42. class AutoClose
  43. {
  44. HANDLE m_hHandle;
  45. public:
  46. AutoClose(HANDLE h) { m_hHandle = h; }
  47. ~AutoClose() { CloseHandle(m_hHandle); }
  48. };
  49. class CPageCache
  50. {
  51. private:
  52. const wchar_t *m_wszStoreName;
  53. DWORD m_dwPageSize;
  54. DWORD m_dwCacheSize;
  55. DWORD m_dwCachePromoteThreshold;
  56. DWORD m_dwCacheSpillRatio;
  57. DWORD m_dwLastFlushTime;
  58. DWORD m_dwWritesSinceFlush;
  59. DWORD m_dwLastCacheAccess;
  60. DWORD m_dwReadHits;
  61. DWORD m_dwReadMisses;
  62. DWORD m_dwWriteHits;
  63. DWORD m_dwWriteMisses;
  64. HANDLE m_hFile;
  65. // Page r/w cache
  66. std::vector <SCachePage *, wbem_allocator<SCachePage *> > m_aCache;
  67. public:
  68. DWORD ReadPhysPage( // Does a real disk access
  69. IN DWORD dwPage, // Always physical ID
  70. OUT LPBYTE *pPageMem // Returns read-only pointer (copy of ptr in SCachePage struct)
  71. );
  72. DWORD WritePhysPage( // Does a real disk access
  73. DWORD dwPageId, // Always physical ID
  74. LPBYTE pPageMem // Read-only; doesn't acquire pointer
  75. );
  76. DWORD Spill();
  77. DWORD Empty();
  78. // Private methods
  79. public:
  80. CPageCache(const wchar_t *wszStoreName);
  81. ~CPageCache();
  82. DWORD Init(
  83. LPCWSTR pszFilename, // File
  84. DWORD dwPageSize, // In bytes
  85. DWORD dwCacheSize = 64, // Pages in cache
  86. DWORD dwCachePromoteThreshold = 16, // When to ignore promote-to-front
  87. DWORD dwCacheSpillRatio = 4 // How many additional pages to write on cache write-through
  88. );
  89. DWORD DeInit();
  90. // Cache operations
  91. DWORD Write( // Only usable from within a transaction
  92. DWORD dwPhysId,
  93. LPBYTE pPageMem // Acquires memory (operator new required)
  94. );
  95. DWORD Read(
  96. IN DWORD dwPhysId,
  97. OUT LPBYTE *pMem // Use operator delete
  98. );
  99. DWORD Flush();
  100. DWORD GetFileSize(LARGE_INTEGER *pFileSize);
  101. DWORD EmptyCache() { DWORD dwRet = Flush(); if (dwRet == ERROR_SUCCESS) Empty(); return dwRet; }
  102. void Dump(FILE *f);
  103. DWORD GetReadHits() { return m_dwReadHits; }
  104. DWORD GetReadMisses() { return m_dwReadMisses; }
  105. DWORD GetWriteHits() { return m_dwWriteHits; }
  106. DWORD GetWriteMisses() { return m_dwWriteMisses; }
  107. SIZE_T GetCacheSize(){ return m_aCache.size(); };
  108. };
  109. class CPageFile
  110. {
  111. private:
  112. friend class CPageSource;
  113. const wchar_t *m_wszStoreName;
  114. LONG m_lRef;
  115. DWORD m_dwPageSize;
  116. DWORD m_dwCacheSpillRatio;
  117. CRITICAL_SECTION m_cs;
  118. bool m_bCsInit;
  119. CPageCache m_Cache;
  120. BOOL m_bInTransaction;
  121. DWORD m_dwLastCheckpoint;
  122. DWORD m_dwTransVersion;
  123. // Generation A Mapping
  124. std::vector <DWORD, wbem_allocator<DWORD> > m_aPageMapA;
  125. std::vector <DWORD, wbem_allocator<DWORD> > m_aPhysFreeListA;
  126. std::vector <DWORD, wbem_allocator<DWORD> > m_aLogicalFreeListA;
  127. std::vector <DWORD, wbem_allocator<DWORD> > m_aReplacedPagesA;
  128. DWORD m_dwPhysPagesA;
  129. // Generation B Mapping
  130. std::vector <DWORD, wbem_allocator<DWORD> > m_aPageMapB;
  131. std::vector <DWORD, wbem_allocator<DWORD> > m_aPhysFreeListB;
  132. std::vector <DWORD, wbem_allocator<DWORD> > m_aLogicalFreeListB;
  133. std::vector <DWORD, wbem_allocator<DWORD> > m_aReplacedPagesB;
  134. DWORD m_dwPhysPagesB;
  135. std::vector <DWORD, wbem_allocator<DWORD> > m_aDeferredFreeList;
  136. public: // temp for testing
  137. // Internal methods
  138. DWORD Trans_Begin();
  139. DWORD Trans_Rollback();
  140. DWORD Trans_Commit();
  141. DWORD Trans_Checkpoint(HANDLE hFile);
  142. DWORD InitFreeList();
  143. DWORD ResyncMaps();
  144. DWORD ReadMap(HANDLE hFile);
  145. DWORD WriteMap(HANDLE hFile);
  146. DWORD ValidateMapFile();
  147. DWORD AllocPhysPage(DWORD *pdwId);
  148. DWORD ReclaimLogicalPages(
  149. DWORD dwCount,
  150. DWORD *pdwId
  151. );
  152. DWORD GetTransVersion() { return m_dwTransVersion; }
  153. void IncrementTransVersion(){ m_dwTransVersion++; }
  154. void DumpFreeListInfo();
  155. public:
  156. CPageFile(const wchar_t *wszStoreName);
  157. ~CPageFile();
  158. //First-time initializatoin of structure
  159. DWORD Init(
  160. WString &sMainFile,
  161. DWORD dwRepPageSize,
  162. DWORD dwCacheSize,
  163. DWORD dwCacheSpillRatio
  164. );
  165. //Clear all caches and structures out
  166. DWORD DeInit();
  167. static DWORD RollForwardV1Maps(WString& sDirectory);
  168. ULONG AddRef();
  169. ULONG Release();
  170. DWORD GetPage(
  171. DWORD dwId, // page zero is admin page; doesn't require NewPage() call
  172. DWORD dwFlags,
  173. LPVOID pPage
  174. );
  175. DWORD PutPage(
  176. DWORD dwId,
  177. DWORD dwFlags,
  178. LPVOID pPage
  179. );
  180. DWORD NewPage(
  181. DWORD dwFlags,
  182. DWORD dwCount,
  183. DWORD *pdwFirstId
  184. );
  185. DWORD FreePage(
  186. DWORD dwFlags,
  187. DWORD dwId
  188. );
  189. DWORD GetPageSize() { return m_dwPageSize; }
  190. DWORD GetNumPages() { return m_aPageMapB.size(); }
  191. DWORD GetPhysPage(DWORD dwLogical) { return m_aPageMapB[dwLogical]; }
  192. DWORD EmptyCache()
  193. {
  194. return m_Cache.EmptyCache();
  195. }
  196. #ifdef WMI_PRIVATE_DBG
  197. DWORD CPageFile::DumpFileInformation(HANDLE hFile);
  198. #endif
  199. void Dump(FILE *f);
  200. DWORD CompactPages(DWORD dwNumPages);
  201. };
  202. class CPageSource
  203. {
  204. private:
  205. DWORD m_dwStatus;
  206. DWORD m_dwPageSize;
  207. DWORD m_dwCacheSize;
  208. DWORD m_dwCacheSpillRatio;
  209. DWORD m_dwLastCheckpoint;
  210. WString m_sDirectory;
  211. CPageFile m_BTreePF;
  212. CPageFile m_ObjPF;
  213. WString m_FileMainData;
  214. WString m_FileMainBtr;
  215. WString m_FileMap1;
  216. WString m_FileMap2;
  217. WString m_FileMapVer;
  218. HANDLE m_hFileMap1;
  219. HANDLE m_hFileMap2;
  220. HANDLE m_hFileMapVer;
  221. DWORD m_dwFileMapVer;
  222. DWORD Startup();
  223. DWORD OpenMapFiles();
  224. DWORD CloseMapFiles();
  225. DWORD V1ReposititoryExists(bool *pbV1RepositoryExists);
  226. DWORD V2ReposititoryExists(bool *pbV2RepositoryExists);
  227. DWORD UpgradeV1Maps();
  228. DWORD DeleteRepository();
  229. public:
  230. CPageSource();
  231. ~CPageSource();
  232. DWORD Init( );
  233. DWORD Shutdown(DWORD dwFlags);
  234. DWORD GetBTreePageFile(OUT CPageFile **pPF); // Use Release() when done
  235. DWORD GetObjectHeapPageFile(OUT CPageFile **pPF); // Use Release() when done
  236. // Transactions
  237. DWORD BeginTrans();
  238. DWORD CommitTrans();
  239. DWORD RollbackTrans();
  240. // Checkpoint
  241. DWORD Checkpoint(bool bCompactPages);
  242. // Cache discard
  243. DWORD EmptyCaches();
  244. void Dump(FILE *f);
  245. //Compacts the last n physical pages of the files to the start of the file.
  246. //It does this for the BTree and ObjHeap.
  247. HRESULT CompactPages(DWORD dwNumPages);
  248. static BOOL FileExists(LPCWSTR pFile, NTSTATUS& lastStatus);
  249. };
  250. #endif