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.

235 lines
7.9 KiB

  1. //
  2. // sqlsnap.h Define the interface to the nt/sql snapshot handler.
  3. //
  4. // The idea here is for a pure interface, making it easy to keep the
  5. // abstraction maximized (can move to COM later, if we like).
  6. //
  7. // No C++ exceptions will be thrown across the interfaces.
  8. //
  9. // To use this interface, the calling process must invoke:
  10. // InitSQLEnvironment - once to setup the environment, establishing
  11. // the error and trace loggers.
  12. // The trace logger is optional, but an error logger must be provided.
  13. // The loggers are created by deriving from CLogFacility and implementing
  14. // a "WriteImplementation" method.
  15. //
  16. // Thereafter, calls to "CreateSqlSnapshot" are used to create snapshot objects
  17. // which encapsulate the operations needed to support storage snapshots.
  18. //
  19. // *****************************************
  20. // LIMITATIONS
  21. //
  22. // - only SIMPLE databases can be snapshot (trunc on checkpoint = 'true')
  23. // Exception: SQL2000 full recovery db's are allowed. But master can't be restored with them.
  24. // - there is no serialization of services starting or adding/changing file lists during the snapshot
  25. // - servers which are not started when the snapshot starts are skipped (non-torn databases will be
  26. // backed up fine, torn databases won't be detected).
  27. // - sql7.0 databases which are "un"-useable will prevent snapshots (the list of files can't be obtained).
  28. //
  29. #include <stdio.h>
  30. #include <stdarg.h>
  31. #include <windows.h>
  32. ////////////////////////////////////////////////////////////////////////
  33. // Standard foo for file name aliasing. This code block must be after
  34. // all includes of VSS header files.
  35. //
  36. #ifdef VSS_FILE_ALIAS
  37. #undef VSS_FILE_ALIAS
  38. #endif
  39. #define VSS_FILE_ALIAS "INCSQLSH"
  40. //
  41. ////////////////////////////////////////////////////////////////////////
  42. HRESULT InitSQLEnvironment();
  43. // Caller must provide a path checker interface.
  44. // Used to provide info about the snapshot
  45. class CCheckPath
  46. {
  47. public:
  48. // ComponentBased snapshots can deal with non-simple recovery.
  49. // The backup app must handle VDI metadata if rollforward will be supported.
  50. //
  51. virtual
  52. bool
  53. IsComponentBased () throw() = 0;
  54. // ComponentBased snapshots provide an explicit list of databases
  55. //
  56. virtual
  57. PCWSTR
  58. EnumerateSelectedDatabases (const WCHAR *instanceName, UINT* nextIndex) throw () = 0;
  59. // return true if Path is part of the snapshot
  60. // Only intended for non-component-based backups.
  61. //
  62. virtual
  63. bool
  64. IsPathInSnapshot (const WCHAR* path) throw () = 0;
  65. };
  66. //------------------------------------------------------------------
  67. // Provide information Post-Freeze (or Thaw) about the databases
  68. // which were frozen and which support VDI metadata.
  69. //
  70. struct FrozenDatabaseInfo
  71. {
  72. const WCHAR* serverName;
  73. const WCHAR* databaseName;
  74. // BOOL isSimpleRecovery; THIS ISN'T CONVENIENTLY AVAILABLE....DO WE NEED IT?
  75. UINT metaDataSize;
  76. const BYTE* pMetaData;
  77. };
  78. //-------------------------------------------------------------
  79. // A handler for snapshots.
  80. //
  81. class CSqlSnapshot
  82. {
  83. public:
  84. virtual ~CSqlSnapshot () throw () {};
  85. virtual HRESULT Prepare (
  86. CCheckPath* checker) throw () = 0;
  87. virtual HRESULT Freeze () throw () = 0;
  88. virtual HRESULT Thaw () throw () = 0;
  89. // Call this at "Post-snapshot" time, after all databases are frozen and MD is complete.
  90. //
  91. // Iterate over the databases which were frozen.
  92. // Valid to call this after Freeze, Thaw until destruction or Prepare().
  93. //
  94. virtual HRESULT GetFirstDatabase (FrozenDatabaseInfo* fInfo) throw () = 0;
  95. virtual HRESULT GetNextDatabase (FrozenDatabaseInfo* fInfo) throw () = 0;
  96. };
  97. extern "C" CSqlSnapshot* CreateSqlSnapshot () throw ();
  98. //-------------------------------------------------------------
  99. // Handle the restore operations for "composite restore" situations.
  100. //
  101. // An object is used to cache the connection to a single instance.
  102. // Thus the caller should perform operations grouped by instance.
  103. //
  104. class CSqlRestore
  105. {
  106. public:
  107. // Inform SQLServer that data laydown is desired on the full database.
  108. // Performs a DETACH, freeing the files.
  109. //
  110. virtual HRESULT PrepareToRestore (
  111. const WCHAR* pInstance,
  112. const WCHAR* pDatabase)
  113. throw () = 0;
  114. // After data is laid down, this performs RESTORE WITH SNAPSHOT[,NORECOVERY]
  115. //
  116. virtual HRESULT FinalizeRestore (
  117. const WCHAR* pInstance,
  118. const WCHAR* pDatabase,
  119. bool compositeRestore, // true if WITH NORECOVERY desired
  120. const BYTE* pMetadata, // metadata obtained from BACKUP
  121. unsigned int dataLen) // size of metadata (in bytes)
  122. throw () = 0;
  123. };
  124. extern "C" CSqlRestore* CreateSqlRestore () throw ();
  125. //-------------------------------------------------------------
  126. // An enumerator for SQL objects.
  127. //
  128. // An object of this class can have only one active query at
  129. // a time. Requesting a new enumeration will close any previous
  130. // partially fetched result.
  131. //
  132. #define MAX_SERVERNAME 128
  133. #define MAX_DBNAME 128
  134. struct ServerInfo
  135. {
  136. bool isOnline; // true if the server is ready for connections
  137. // bool supportsCompositeRestore; // true if >=SQL2000 (RESTORE WITH NORECOVERY,SNAPSHOT)
  138. // not readily available. Ask Brian if we really need it....
  139. // this is easy to obtain when the databases are being enumerated.
  140. //
  141. WCHAR name [MAX_SERVERNAME]; // null terminated name of server
  142. };
  143. struct DatabaseInfo
  144. {
  145. bool isSimpleRecovery; // true if the recovery model is "SIMPLE"
  146. bool supportsFreeze; // true if this database can freeze (via dbcc or backup)
  147. UINT32 status; // status bits
  148. WCHAR name [MAX_DBNAME]; // null terminated name of database
  149. };
  150. struct DatabaseFileInfo
  151. {
  152. bool isLogFile; // true if this is a log file
  153. WCHAR name [MAX_PATH];
  154. };
  155. // A heirarchical enumerator
  156. // server(instance) (1:N) databases (1:N) files
  157. // ...add doc...
  158. //
  159. class CSqlEnumerator
  160. {
  161. public:
  162. virtual ~CSqlEnumerator () throw () {};
  163. virtual HRESULT FirstServer (
  164. ServerInfo* pServer) throw () = 0;
  165. virtual HRESULT NextServer (
  166. ServerInfo* pServer) throw () = 0;
  167. virtual HRESULT FirstDatabase (
  168. const WCHAR* pServerName,
  169. DatabaseInfo* pDbInfo) throw () = 0;
  170. virtual HRESULT NextDatabase (
  171. DatabaseInfo* pDbInfo) throw () = 0;
  172. virtual HRESULT FirstFile (
  173. const WCHAR* pServerName,
  174. const WCHAR* pDbName,
  175. DatabaseFileInfo* pFileInfo) throw () = 0;
  176. virtual HRESULT NextFile (
  177. DatabaseFileInfo* pFileInfo) throw () = 0;
  178. };
  179. extern "C" CSqlEnumerator* CreateSqlEnumerator () throw ();
  180. //------------------------------------------------------
  181. // HRESULTS returned by the interface.
  182. //
  183. // WARNING: I used facility = x78 arbitrarily!
  184. //
  185. #define SQLLIB_ERROR(code) MAKE_HRESULT(SEVERITY_ERROR, 0x78, code)
  186. #define SQLLIB_STATUS(code) MAKE_HRESULT(SEVERITY_SUCCESS, 0x78, code)
  187. // Status codes
  188. //
  189. #define S_SQLLIB_NOSERVERS SQLLIB_STATUS(1) // no SQLServers of interest (from Prepare)
  190. // Error codes
  191. //
  192. #define E_SQLLIB_GENERIC SQLLIB_ERROR(1) // something bad, check the errorlog
  193. #define E_SQLLIB_TORN_DB SQLLIB_ERROR(2) // database would be torn by the snapshot
  194. #define E_SQLLIB_NO_SUPPORT SQLLIB_ERROR(3) // 6.5 doesn't support snapshots
  195. #define E_SQLLIB_PROTO SQLLIB_ERROR(4) // protocol error (ex: freeze before prepare)
  196. #define E_SQLLIB_NONSIMPLE SQLLIB_ERROR(5) // only simple databases are supported