ppc/spapr_caps: Don't disable cap_cfpc on POWER8 by default
[qemu/rayw.git] / qga / vss-win32 / provider.cpp
blob72d8b0e19d561fc7b738d528122921cc58e9654a
1 /*
2 * QEMU Guest Agent win32 VSS Provider implementations
4 * Copyright Hitachi Data Systems Corp. 2013
6 * Authors:
7 * Tomoki Sekiyama <tomoki.sekiyama@hds.com>
9 * This work is licensed under the terms of the GNU GPL, version 2 or later.
10 * See the COPYING file in the top-level directory.
13 #include "qemu/osdep.h"
14 #include "vss-common.h"
15 #include <inc/win2003/vscoordint.h>
16 #include <inc/win2003/vsprov.h>
18 #define VSS_TIMEOUT_MSEC (60*1000)
20 static long g_nComObjsInUse;
21 HINSTANCE g_hinstDll;
23 /* VSS common GUID's */
25 const CLSID CLSID_VSSCoordinator = { 0xE579AB5F, 0x1CC4, 0x44b4,
26 {0xBE, 0xD9, 0xDE, 0x09, 0x91, 0xFF, 0x06, 0x23} };
27 const IID IID_IVssAdmin = { 0x77ED5996, 0x2F63, 0x11d3,
28 {0x8A, 0x39, 0x00, 0xC0, 0x4F, 0x72, 0xD8, 0xE3} };
30 const IID IID_IVssHardwareSnapshotProvider = { 0x9593A157, 0x44E9, 0x4344,
31 {0xBB, 0xEB, 0x44, 0xFB, 0xF9, 0xB0, 0x6B, 0x10} };
32 const IID IID_IVssSoftwareSnapshotProvider = { 0x609e123e, 0x2c5a, 0x44d3,
33 {0x8f, 0x01, 0x0b, 0x1d, 0x9a, 0x47, 0xd1, 0xff} };
34 const IID IID_IVssProviderCreateSnapshotSet = { 0x5F894E5B, 0x1E39, 0x4778,
35 {0x8E, 0x23, 0x9A, 0xBA, 0xD9, 0xF0, 0xE0, 0x8C} };
36 const IID IID_IVssProviderNotifications = { 0xE561901F, 0x03A5, 0x4afe,
37 {0x86, 0xD0, 0x72, 0xBA, 0xEE, 0xCE, 0x70, 0x04} };
39 const IID IID_IVssEnumObject = { 0xAE1C7110, 0x2F60, 0x11d3,
40 {0x8A, 0x39, 0x00, 0xC0, 0x4F, 0x72, 0xD8, 0xE3} };
43 void LockModule(BOOL lock)
45 if (lock) {
46 InterlockedIncrement(&g_nComObjsInUse);
47 } else {
48 InterlockedDecrement(&g_nComObjsInUse);
52 /* Empty enumerator for VssObject */
54 class CQGAVSSEnumObject : public IVssEnumObject
56 public:
57 STDMETHODIMP QueryInterface(REFIID riid, void **ppObj);
58 STDMETHODIMP_(ULONG) AddRef();
59 STDMETHODIMP_(ULONG) Release();
61 /* IVssEnumObject Methods */
62 STDMETHODIMP Next(
63 ULONG celt, VSS_OBJECT_PROP *rgelt, ULONG *pceltFetched);
64 STDMETHODIMP Skip(ULONG celt);
65 STDMETHODIMP Reset(void);
66 STDMETHODIMP Clone(IVssEnumObject **ppenum);
68 /* CQGAVSSEnumObject Methods */
69 CQGAVSSEnumObject();
70 ~CQGAVSSEnumObject();
72 private:
73 long m_nRefCount;
76 CQGAVSSEnumObject::CQGAVSSEnumObject()
78 m_nRefCount = 0;
79 LockModule(TRUE);
82 CQGAVSSEnumObject::~CQGAVSSEnumObject()
84 LockModule(FALSE);
87 STDMETHODIMP CQGAVSSEnumObject::QueryInterface(REFIID riid, void **ppObj)
89 if (riid == IID_IUnknown || riid == IID_IVssEnumObject) {
90 *ppObj = static_cast<void*>(static_cast<IVssEnumObject*>(this));
91 AddRef();
92 return S_OK;
94 *ppObj = NULL;
95 return E_NOINTERFACE;
98 STDMETHODIMP_(ULONG) CQGAVSSEnumObject::AddRef()
100 return InterlockedIncrement(&m_nRefCount);
103 STDMETHODIMP_(ULONG) CQGAVSSEnumObject::Release()
105 long nRefCount = InterlockedDecrement(&m_nRefCount);
106 if (m_nRefCount == 0) {
107 delete this;
109 return nRefCount;
112 STDMETHODIMP CQGAVSSEnumObject::Next(
113 ULONG celt, VSS_OBJECT_PROP *rgelt, ULONG *pceltFetched)
115 *pceltFetched = 0;
116 return S_FALSE;
119 STDMETHODIMP CQGAVSSEnumObject::Skip(ULONG celt)
121 return S_FALSE;
124 STDMETHODIMP CQGAVSSEnumObject::Reset(void)
126 return S_OK;
129 STDMETHODIMP CQGAVSSEnumObject::Clone(IVssEnumObject **ppenum)
131 return E_NOTIMPL;
135 /* QGAVssProvider */
137 class CQGAVssProvider :
138 public IVssSoftwareSnapshotProvider,
139 public IVssProviderCreateSnapshotSet,
140 public IVssProviderNotifications
142 public:
143 STDMETHODIMP QueryInterface(REFIID riid, void **ppObj);
144 STDMETHODIMP_(ULONG) AddRef();
145 STDMETHODIMP_(ULONG) Release();
147 /* IVssSoftwareSnapshotProvider Methods */
148 STDMETHODIMP SetContext(LONG lContext);
149 STDMETHODIMP GetSnapshotProperties(
150 VSS_ID SnapshotId, VSS_SNAPSHOT_PROP *pProp);
151 STDMETHODIMP Query(
152 VSS_ID QueriedObjectId, VSS_OBJECT_TYPE eQueriedObjectType,
153 VSS_OBJECT_TYPE eReturnedObjectsType, IVssEnumObject **ppEnum);
154 STDMETHODIMP DeleteSnapshots(
155 VSS_ID SourceObjectId, VSS_OBJECT_TYPE eSourceObjectType,
156 BOOL bForceDelete, LONG *plDeletedSnapshots,
157 VSS_ID *pNondeletedSnapshotID);
158 STDMETHODIMP BeginPrepareSnapshot(
159 VSS_ID SnapshotSetId, VSS_ID SnapshotId,
160 VSS_PWSZ pwszVolumeName, LONG lNewContext);
161 STDMETHODIMP IsVolumeSupported(
162 VSS_PWSZ pwszVolumeName, BOOL *pbSupportedByThisProvider);
163 STDMETHODIMP IsVolumeSnapshotted(
164 VSS_PWSZ pwszVolumeName, BOOL *pbSnapshotsPresent,
165 LONG *plSnapshotCompatibility);
166 STDMETHODIMP SetSnapshotProperty(
167 VSS_ID SnapshotId, VSS_SNAPSHOT_PROPERTY_ID eSnapshotPropertyId,
168 VARIANT vProperty);
169 STDMETHODIMP RevertToSnapshot(VSS_ID SnapshotId);
170 STDMETHODIMP QueryRevertStatus(VSS_PWSZ pwszVolume, IVssAsync **ppAsync);
172 /* IVssProviderCreateSnapshotSet Methods */
173 STDMETHODIMP EndPrepareSnapshots(VSS_ID SnapshotSetId);
174 STDMETHODIMP PreCommitSnapshots(VSS_ID SnapshotSetId);
175 STDMETHODIMP CommitSnapshots(VSS_ID SnapshotSetId);
176 STDMETHODIMP PostCommitSnapshots(
177 VSS_ID SnapshotSetId, LONG lSnapshotsCount);
178 STDMETHODIMP PreFinalCommitSnapshots(VSS_ID SnapshotSetId);
179 STDMETHODIMP PostFinalCommitSnapshots(VSS_ID SnapshotSetId);
180 STDMETHODIMP AbortSnapshots(VSS_ID SnapshotSetId);
182 /* IVssProviderNotifications Methods */
183 STDMETHODIMP OnLoad(IUnknown *pCallback);
184 STDMETHODIMP OnUnload(BOOL bForceUnload);
186 /* CQGAVssProvider Methods */
187 CQGAVssProvider();
188 ~CQGAVssProvider();
190 private:
191 long m_nRefCount;
194 CQGAVssProvider::CQGAVssProvider()
196 m_nRefCount = 0;
197 LockModule(TRUE);
200 CQGAVssProvider::~CQGAVssProvider()
202 LockModule(FALSE);
205 STDMETHODIMP CQGAVssProvider::QueryInterface(REFIID riid, void **ppObj)
207 if (riid == IID_IUnknown) {
208 *ppObj = static_cast<void*>(this);
209 AddRef();
210 return S_OK;
212 if (riid == IID_IVssSoftwareSnapshotProvider) {
213 *ppObj = static_cast<void*>(
214 static_cast<IVssSoftwareSnapshotProvider*>(this));
215 AddRef();
216 return S_OK;
218 if (riid == IID_IVssProviderCreateSnapshotSet) {
219 *ppObj = static_cast<void*>(
220 static_cast<IVssProviderCreateSnapshotSet*>(this));
221 AddRef();
222 return S_OK;
224 if (riid == IID_IVssProviderNotifications) {
225 *ppObj = static_cast<void*>(
226 static_cast<IVssProviderNotifications*>(this));
227 AddRef();
228 return S_OK;
230 *ppObj = NULL;
231 return E_NOINTERFACE;
234 STDMETHODIMP_(ULONG) CQGAVssProvider::AddRef()
236 return InterlockedIncrement(&m_nRefCount);
239 STDMETHODIMP_(ULONG) CQGAVssProvider::Release()
241 long nRefCount = InterlockedDecrement(&m_nRefCount);
242 if (m_nRefCount == 0) {
243 delete this;
245 return nRefCount;
250 * IVssSoftwareSnapshotProvider methods
253 STDMETHODIMP CQGAVssProvider::SetContext(LONG lContext)
255 return S_OK;
258 STDMETHODIMP CQGAVssProvider::GetSnapshotProperties(
259 VSS_ID SnapshotId, VSS_SNAPSHOT_PROP *pProp)
261 return VSS_E_OBJECT_NOT_FOUND;
264 STDMETHODIMP CQGAVssProvider::Query(
265 VSS_ID QueriedObjectId, VSS_OBJECT_TYPE eQueriedObjectType,
266 VSS_OBJECT_TYPE eReturnedObjectsType, IVssEnumObject **ppEnum)
268 try {
269 *ppEnum = new CQGAVSSEnumObject;
270 } catch (...) {
271 return E_OUTOFMEMORY;
273 (*ppEnum)->AddRef();
274 return S_OK;
277 STDMETHODIMP CQGAVssProvider::DeleteSnapshots(
278 VSS_ID SourceObjectId, VSS_OBJECT_TYPE eSourceObjectType,
279 BOOL bForceDelete, LONG *plDeletedSnapshots, VSS_ID *pNondeletedSnapshotID)
281 *plDeletedSnapshots = 0;
282 *pNondeletedSnapshotID = SourceObjectId;
283 return S_OK;
286 STDMETHODIMP CQGAVssProvider::BeginPrepareSnapshot(
287 VSS_ID SnapshotSetId, VSS_ID SnapshotId,
288 VSS_PWSZ pwszVolumeName, LONG lNewContext)
290 return S_OK;
293 STDMETHODIMP CQGAVssProvider::IsVolumeSupported(
294 VSS_PWSZ pwszVolumeName, BOOL *pbSupportedByThisProvider)
296 HANDLE hEventFrozen;
298 /* Check if a requester is qemu-ga by whether an event is created */
299 hEventFrozen = OpenEvent(EVENT_ALL_ACCESS, FALSE, EVENT_NAME_FROZEN);
300 if (!hEventFrozen) {
301 *pbSupportedByThisProvider = FALSE;
302 return S_OK;
304 CloseHandle(hEventFrozen);
306 *pbSupportedByThisProvider = TRUE;
307 return S_OK;
310 STDMETHODIMP CQGAVssProvider::IsVolumeSnapshotted(VSS_PWSZ pwszVolumeName,
311 BOOL *pbSnapshotsPresent, LONG *plSnapshotCompatibility)
313 *pbSnapshotsPresent = FALSE;
314 *plSnapshotCompatibility = 0;
315 return S_OK;
318 STDMETHODIMP CQGAVssProvider::SetSnapshotProperty(VSS_ID SnapshotId,
319 VSS_SNAPSHOT_PROPERTY_ID eSnapshotPropertyId, VARIANT vProperty)
321 return E_NOTIMPL;
324 STDMETHODIMP CQGAVssProvider::RevertToSnapshot(VSS_ID SnapshotId)
326 return E_NOTIMPL;
329 STDMETHODIMP CQGAVssProvider::QueryRevertStatus(
330 VSS_PWSZ pwszVolume, IVssAsync **ppAsync)
332 return E_NOTIMPL;
337 * IVssProviderCreateSnapshotSet methods
340 STDMETHODIMP CQGAVssProvider::EndPrepareSnapshots(VSS_ID SnapshotSetId)
342 return S_OK;
345 STDMETHODIMP CQGAVssProvider::PreCommitSnapshots(VSS_ID SnapshotSetId)
347 return S_OK;
350 STDMETHODIMP CQGAVssProvider::CommitSnapshots(VSS_ID SnapshotSetId)
352 HRESULT hr = S_OK;
353 HANDLE hEventFrozen, hEventThaw, hEventTimeout;
355 hEventFrozen = OpenEvent(EVENT_ALL_ACCESS, FALSE, EVENT_NAME_FROZEN);
356 if (!hEventFrozen) {
357 return E_FAIL;
360 hEventThaw = OpenEvent(EVENT_ALL_ACCESS, FALSE, EVENT_NAME_THAW);
361 if (!hEventThaw) {
362 CloseHandle(hEventFrozen);
363 return E_FAIL;
366 hEventTimeout = OpenEvent(EVENT_ALL_ACCESS, FALSE, EVENT_NAME_TIMEOUT);
367 if (!hEventTimeout) {
368 CloseHandle(hEventFrozen);
369 CloseHandle(hEventThaw);
370 return E_FAIL;
373 /* Send event to qemu-ga to notify filesystem is frozen */
374 SetEvent(hEventFrozen);
376 /* Wait until the snapshot is taken by the host. */
377 if (WaitForSingleObject(hEventThaw, VSS_TIMEOUT_MSEC) != WAIT_OBJECT_0) {
378 /* Send event to qemu-ga to notify the provider is timed out */
379 SetEvent(hEventTimeout);
382 CloseHandle(hEventThaw);
383 CloseHandle(hEventFrozen);
384 CloseHandle(hEventTimeout);
385 return hr;
388 STDMETHODIMP CQGAVssProvider::PostCommitSnapshots(
389 VSS_ID SnapshotSetId, LONG lSnapshotsCount)
391 return S_OK;
394 STDMETHODIMP CQGAVssProvider::PreFinalCommitSnapshots(VSS_ID SnapshotSetId)
396 return S_OK;
399 STDMETHODIMP CQGAVssProvider::PostFinalCommitSnapshots(VSS_ID SnapshotSetId)
401 return S_OK;
404 STDMETHODIMP CQGAVssProvider::AbortSnapshots(VSS_ID SnapshotSetId)
406 return S_OK;
410 * IVssProviderNotifications methods
413 STDMETHODIMP CQGAVssProvider::OnLoad(IUnknown *pCallback)
415 return S_OK;
418 STDMETHODIMP CQGAVssProvider::OnUnload(BOOL bForceUnload)
420 return S_OK;
425 * CQGAVssProviderFactory class
428 class CQGAVssProviderFactory : public IClassFactory
430 public:
431 STDMETHODIMP QueryInterface(REFIID riid, void **ppv);
432 STDMETHODIMP_(ULONG) AddRef();
433 STDMETHODIMP_(ULONG) Release();
434 STDMETHODIMP CreateInstance(
435 IUnknown *pUnknownOuter, REFIID iid, void **ppv);
436 STDMETHODIMP LockServer(BOOL lock) { return E_NOTIMPL; }
438 CQGAVssProviderFactory();
439 ~CQGAVssProviderFactory();
441 private:
442 long m_nRefCount;
445 CQGAVssProviderFactory::CQGAVssProviderFactory()
447 m_nRefCount = 0;
448 LockModule(TRUE);
451 CQGAVssProviderFactory::~CQGAVssProviderFactory()
453 LockModule(FALSE);
456 STDMETHODIMP CQGAVssProviderFactory::QueryInterface(REFIID riid, void **ppv)
458 if (riid == IID_IUnknown || riid == IID_IClassFactory) {
459 *ppv = static_cast<void*>(this);
460 AddRef();
461 return S_OK;
463 *ppv = NULL;
464 return E_NOINTERFACE;
467 STDMETHODIMP_(ULONG) CQGAVssProviderFactory::AddRef()
469 return InterlockedIncrement(&m_nRefCount);
472 STDMETHODIMP_(ULONG) CQGAVssProviderFactory::Release()
474 long nRefCount = InterlockedDecrement(&m_nRefCount);
475 if (m_nRefCount == 0) {
476 delete this;
478 return nRefCount;
481 STDMETHODIMP CQGAVssProviderFactory::CreateInstance(
482 IUnknown *pUnknownOuter, REFIID iid, void **ppv)
484 CQGAVssProvider *pObj;
486 if (pUnknownOuter) {
487 return CLASS_E_NOAGGREGATION;
489 try {
490 pObj = new CQGAVssProvider;
491 } catch (...) {
492 return E_OUTOFMEMORY;
494 HRESULT hr = pObj->QueryInterface(iid, ppv);
495 if (FAILED(hr)) {
496 delete pObj;
498 return hr;
503 * DLL functions
506 STDAPI DllGetClassObject(REFCLSID rclsid, REFIID riid, LPVOID *ppv)
508 CQGAVssProviderFactory *factory;
509 try {
510 factory = new CQGAVssProviderFactory;
511 } catch (...) {
512 return E_OUTOFMEMORY;
514 factory->AddRef();
515 HRESULT hr = factory->QueryInterface(riid, ppv);
516 factory->Release();
517 return hr;
520 STDAPI DllCanUnloadNow()
522 return g_nComObjsInUse == 0 ? S_OK : S_FALSE;
525 EXTERN_C
526 BOOL WINAPI DllMain(HINSTANCE hinstDll, DWORD dwReason, LPVOID lpReserved)
528 if (dwReason == DLL_PROCESS_ATTACH) {
529 g_hinstDll = hinstDll;
530 DisableThreadLibraryCalls(hinstDll);
532 return TRUE;