2 * QEMU Guest Agent win32 VSS Requester implementations
4 * Copyright Hitachi Data Systems Corp. 2013
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 "requester.h"
17 #include <inc/win2003/vswriter.h>
18 #include <inc/win2003/vsbackup.h>
20 /* Max wait time for frozen event (VSS can only hold writes for 10 seconds) */
21 #define VSS_TIMEOUT_FREEZE_MSEC 10000
23 /* Call QueryStatus every 10 ms while waiting for frozen event */
24 #define VSS_TIMEOUT_EVENT_MSEC 10
26 #define err_set(e, err, fmt, ...) \
27 ((e)->error_setg_win32_wrapper((e)->errp, __FILE__, __LINE__, __func__, \
28 err, fmt, ## __VA_ARGS__))
29 /* Bad idea, works only when (e)->errp != NULL: */
30 #define err_is_set(e) ((e)->errp && *(e)->errp)
31 /* To lift this restriction, error_propagate(), like we do in QEMU code */
33 /* Handle to VSSAPI.DLL */
36 /* Functions in VSSAPI.DLL */
37 typedef HRESULT(STDAPICALLTYPE
* t_CreateVssBackupComponents
)(
38 OUT IVssBackupComponents
**);
39 typedef void(APIENTRY
* t_VssFreeSnapshotProperties
)(IN VSS_SNAPSHOT_PROP
*);
40 static t_CreateVssBackupComponents pCreateVssBackupComponents
;
41 static t_VssFreeSnapshotProperties pVssFreeSnapshotProperties
;
43 /* Variables used while applications and filesystes are frozen by VSS */
44 static struct QGAVSSContext
{
45 IVssBackupComponents
*pVssbc
; /* VSS requester interface */
46 IVssAsync
*pAsyncSnapshot
; /* async info of VSS snapshot operation */
47 HANDLE hEventFrozen
; /* notify fs/writer freeze from provider */
48 HANDLE hEventThaw
; /* request provider to thaw */
49 HANDLE hEventTimeout
; /* notify timeout in provider */
50 int cFrozenVols
; /* number of frozen volumes */
53 STDAPI
requester_init(void)
55 COMInitializer initializer
; /* to call CoInitializeSecurity */
56 HRESULT hr
= CoInitializeSecurity(
57 NULL
, -1, NULL
, NULL
, RPC_C_AUTHN_LEVEL_PKT_PRIVACY
,
58 RPC_C_IMP_LEVEL_IDENTIFY
, NULL
, EOAC_NONE
, NULL
);
60 fprintf(stderr
, "failed to CoInitializeSecurity (error %lx)\n", hr
);
64 hLib
= LoadLibraryA("VSSAPI.DLL");
66 fprintf(stderr
, "failed to load VSSAPI.DLL\n");
67 return HRESULT_FROM_WIN32(GetLastError());
70 pCreateVssBackupComponents
= (t_CreateVssBackupComponents
)
72 #ifdef _WIN64 /* 64bit environment */
73 "?CreateVssBackupComponents@@YAJPEAPEAVIVssBackupComponents@@@Z"
74 #else /* 32bit environment */
75 "?CreateVssBackupComponents@@YGJPAPAVIVssBackupComponents@@@Z"
78 if (!pCreateVssBackupComponents
) {
79 fprintf(stderr
, "failed to get proc address from VSSAPI.DLL\n");
80 return HRESULT_FROM_WIN32(GetLastError());
83 pVssFreeSnapshotProperties
= (t_VssFreeSnapshotProperties
)
84 GetProcAddress(hLib
, "VssFreeSnapshotProperties");
85 if (!pVssFreeSnapshotProperties
) {
86 fprintf(stderr
, "failed to get proc address from VSSAPI.DLL\n");
87 return HRESULT_FROM_WIN32(GetLastError());
93 static void requester_cleanup(void)
95 if (vss_ctx
.hEventFrozen
) {
96 CloseHandle(vss_ctx
.hEventFrozen
);
97 vss_ctx
.hEventFrozen
= NULL
;
99 if (vss_ctx
.hEventThaw
) {
100 CloseHandle(vss_ctx
.hEventThaw
);
101 vss_ctx
.hEventThaw
= NULL
;
103 if (vss_ctx
.hEventTimeout
) {
104 CloseHandle(vss_ctx
.hEventTimeout
);
105 vss_ctx
.hEventTimeout
= NULL
;
107 if (vss_ctx
.pAsyncSnapshot
) {
108 vss_ctx
.pAsyncSnapshot
->Release();
109 vss_ctx
.pAsyncSnapshot
= NULL
;
111 if (vss_ctx
.pVssbc
) {
112 vss_ctx
.pVssbc
->Release();
113 vss_ctx
.pVssbc
= NULL
;
115 vss_ctx
.cFrozenVols
= 0;
118 STDAPI
requester_deinit(void)
122 pCreateVssBackupComponents
= NULL
;
123 pVssFreeSnapshotProperties
= NULL
;
132 static HRESULT
WaitForAsync(IVssAsync
*pAsync
)
142 hr
= pAsync
->QueryStatus(&ret
, NULL
);
147 } while (ret
== VSS_S_ASYNC_PENDING
);
152 static void AddComponents(ErrorSet
*errset
)
154 unsigned int cWriters
, i
;
155 VSS_ID id
, idInstance
, idWriter
;
156 BSTR bstrWriterName
= NULL
;
157 VSS_USAGE_TYPE usage
;
158 VSS_SOURCE_TYPE source
;
159 unsigned int cComponents
, c1
, c2
, j
;
160 COMPointer
<IVssExamineWriterMetadata
> pMetadata
;
161 COMPointer
<IVssWMComponent
> pComponent
;
162 PVSSCOMPONENTINFO info
;
165 hr
= vss_ctx
.pVssbc
->GetWriterMetadataCount(&cWriters
);
167 err_set(errset
, hr
, "failed to get writer metadata count");
171 for (i
= 0; i
< cWriters
; i
++) {
172 hr
= vss_ctx
.pVssbc
->GetWriterMetadata(i
, &id
, pMetadata
.replace());
174 err_set(errset
, hr
, "failed to get writer metadata of %d/%d",
179 hr
= pMetadata
->GetIdentity(&idInstance
, &idWriter
,
180 &bstrWriterName
, &usage
, &source
);
182 err_set(errset
, hr
, "failed to get identity of writer %d/%d",
187 hr
= pMetadata
->GetFileCounts(&c1
, &c2
, &cComponents
);
189 err_set(errset
, hr
, "failed to get file counts of %S",
194 for (j
= 0; j
< cComponents
; j
++) {
195 hr
= pMetadata
->GetComponent(j
, pComponent
.replace());
198 "failed to get component %d/%d of %S",
199 j
, cComponents
, bstrWriterName
);
203 hr
= pComponent
->GetComponentInfo(&info
);
206 "failed to get component info %d/%d of %S",
207 j
, cComponents
, bstrWriterName
);
211 if (info
->bSelectable
) {
212 hr
= vss_ctx
.pVssbc
->AddComponent(idInstance
, idWriter
,
214 info
->bstrLogicalPath
,
215 info
->bstrComponentName
);
217 err_set(errset
, hr
, "failed to add component %S(%S)",
218 info
->bstrComponentName
, bstrWriterName
);
222 SysFreeString(bstrWriterName
);
223 bstrWriterName
= NULL
;
224 pComponent
->FreeComponentInfo(info
);
229 if (bstrWriterName
) {
230 SysFreeString(bstrWriterName
);
232 if (pComponent
&& info
) {
233 pComponent
->FreeComponentInfo(info
);
237 void requester_freeze(int *num_vols
, ErrorSet
*errset
)
239 COMPointer
<IVssAsync
> pAsync
;
243 GUID guidSnapshotSet
= GUID_NULL
;
244 SECURITY_DESCRIPTOR sd
;
245 SECURITY_ATTRIBUTES sa
;
246 WCHAR short_volume_name
[64], *display_name
= short_volume_name
;
248 int num_fixed_drives
= 0, i
;
250 if (vss_ctx
.pVssbc
) { /* already frozen */
257 /* Allow unrestricted access to events */
258 InitializeSecurityDescriptor(&sd
, SECURITY_DESCRIPTOR_REVISION
);
259 SetSecurityDescriptorDacl(&sd
, TRUE
, NULL
, FALSE
);
260 sa
.nLength
= sizeof(sa
);
261 sa
.lpSecurityDescriptor
= &sd
;
262 sa
.bInheritHandle
= FALSE
;
264 vss_ctx
.hEventFrozen
= CreateEvent(&sa
, TRUE
, FALSE
, EVENT_NAME_FROZEN
);
265 if (!vss_ctx
.hEventFrozen
) {
266 err_set(errset
, GetLastError(), "failed to create event %s",
270 vss_ctx
.hEventThaw
= CreateEvent(&sa
, TRUE
, FALSE
, EVENT_NAME_THAW
);
271 if (!vss_ctx
.hEventThaw
) {
272 err_set(errset
, GetLastError(), "failed to create event %s",
276 vss_ctx
.hEventTimeout
= CreateEvent(&sa
, TRUE
, FALSE
, EVENT_NAME_TIMEOUT
);
277 if (!vss_ctx
.hEventTimeout
) {
278 err_set(errset
, GetLastError(), "failed to create event %s",
283 assert(pCreateVssBackupComponents
!= NULL
);
284 hr
= pCreateVssBackupComponents(&vss_ctx
.pVssbc
);
286 err_set(errset
, hr
, "failed to create VSS backup components");
290 hr
= vss_ctx
.pVssbc
->InitializeForBackup();
292 err_set(errset
, hr
, "failed to initialize for backup");
296 hr
= vss_ctx
.pVssbc
->SetBackupState(true, true, VSS_BT_FULL
, false);
298 err_set(errset
, hr
, "failed to set backup state");
303 * Currently writable snapshots are not supported.
304 * To prevent the final commit (which requires to write to snapshots),
305 * ATTR_NO_AUTORECOVERY and ATTR_TRANSPORTABLE are specified here.
307 ctx
= VSS_CTX_APP_ROLLBACK
| VSS_VOLSNAP_ATTR_TRANSPORTABLE
|
308 VSS_VOLSNAP_ATTR_NO_AUTORECOVERY
| VSS_VOLSNAP_ATTR_TXF_RECOVERY
;
309 hr
= vss_ctx
.pVssbc
->SetContext(ctx
);
310 if (hr
== (HRESULT
)VSS_E_UNSUPPORTED_CONTEXT
) {
311 /* Non-server version of Windows doesn't support ATTR_TRANSPORTABLE */
312 ctx
&= ~VSS_VOLSNAP_ATTR_TRANSPORTABLE
;
313 hr
= vss_ctx
.pVssbc
->SetContext(ctx
);
316 err_set(errset
, hr
, "failed to set backup context");
320 hr
= vss_ctx
.pVssbc
->GatherWriterMetadata(pAsync
.replace());
322 hr
= WaitForAsync(pAsync
);
325 err_set(errset
, hr
, "failed to gather writer metadata");
329 AddComponents(errset
);
330 if (err_is_set(errset
)) {
334 hr
= vss_ctx
.pVssbc
->StartSnapshotSet(&guidSnapshotSet
);
336 err_set(errset
, hr
, "failed to start snapshot set");
340 volume
= FindFirstVolumeW(short_volume_name
, sizeof(short_volume_name
));
341 if (volume
== INVALID_HANDLE_VALUE
) {
342 err_set(errset
, hr
, "failed to find first volume");
346 if (GetDriveTypeW(short_volume_name
) == DRIVE_FIXED
) {
348 hr
= vss_ctx
.pVssbc
->AddToSnapshotSet(short_volume_name
,
349 g_gProviderId
, &pid
);
351 WCHAR volume_path_name
[PATH_MAX
];
352 if (GetVolumePathNamesForVolumeNameW(
353 short_volume_name
, volume_path_name
,
354 sizeof(volume_path_name
), NULL
) && *volume_path_name
) {
355 display_name
= volume_path_name
;
357 err_set(errset
, hr
, "failed to add %S to snapshot set",
359 FindVolumeClose(volume
);
364 if (!FindNextVolumeW(volume
, short_volume_name
,
365 sizeof(short_volume_name
))) {
366 FindVolumeClose(volume
);
371 if (num_fixed_drives
== 0) {
372 goto out
; /* If there is no fixed drive, just exit. */
375 hr
= vss_ctx
.pVssbc
->PrepareForBackup(pAsync
.replace());
377 hr
= WaitForAsync(pAsync
);
380 err_set(errset
, hr
, "failed to prepare for backup");
384 hr
= vss_ctx
.pVssbc
->GatherWriterStatus(pAsync
.replace());
386 hr
= WaitForAsync(pAsync
);
389 err_set(errset
, hr
, "failed to gather writer status");
394 * Start VSS quiescing operations.
395 * CQGAVssProvider::CommitSnapshots will kick vss_ctx.hEventFrozen
396 * after the applications and filesystems are frozen.
398 hr
= vss_ctx
.pVssbc
->DoSnapshotSet(&vss_ctx
.pAsyncSnapshot
);
400 err_set(errset
, hr
, "failed to do snapshot set");
404 /* Need to call QueryStatus several times to make VSS provider progress */
405 for (i
= 0; i
< VSS_TIMEOUT_FREEZE_MSEC
/VSS_TIMEOUT_EVENT_MSEC
; i
++) {
406 HRESULT hr2
= vss_ctx
.pAsyncSnapshot
->QueryStatus(&hr
, NULL
);
408 err_set(errset
, hr
, "failed to do snapshot set");
411 if (hr
!= VSS_S_ASYNC_PENDING
) {
412 err_set(errset
, E_FAIL
,
413 "DoSnapshotSet exited without Frozen event");
416 wait_status
= WaitForSingleObject(vss_ctx
.hEventFrozen
,
417 VSS_TIMEOUT_EVENT_MSEC
);
418 if (wait_status
!= WAIT_TIMEOUT
) {
423 if (wait_status
== WAIT_TIMEOUT
) {
424 err_set(errset
, E_FAIL
,
425 "timeout when try to receive Frozen event from VSS provider");
426 /* If we are here, VSS had timeout.
427 * Don't call AbortBackup, just return directly.
432 if (wait_status
!= WAIT_OBJECT_0
) {
433 err_set(errset
, E_FAIL
,
434 "couldn't receive Frozen event from VSS provider");
438 *num_vols
= vss_ctx
.cFrozenVols
= num_fixed_drives
;
442 if (vss_ctx
.pVssbc
) {
443 vss_ctx
.pVssbc
->AbortBackup();
452 void requester_thaw(int *num_vols
, ErrorSet
*errset
)
454 COMPointer
<IVssAsync
> pAsync
;
456 if (!vss_ctx
.hEventThaw
) {
458 * In this case, DoSnapshotSet is aborted or not started,
459 * and no volumes must be frozen. We return without an error.
465 /* Tell the provider that the snapshot is finished. */
466 SetEvent(vss_ctx
.hEventThaw
);
468 assert(vss_ctx
.pVssbc
);
469 assert(vss_ctx
.pAsyncSnapshot
);
471 HRESULT hr
= WaitForAsync(vss_ctx
.pAsyncSnapshot
);
473 case VSS_S_ASYNC_FINISHED
:
474 hr
= vss_ctx
.pVssbc
->BackupComplete(pAsync
.replace());
476 hr
= WaitForAsync(pAsync
);
479 err_set(errset
, hr
, "failed to complete backup");
483 case (HRESULT
)VSS_E_OBJECT_NOT_FOUND
:
485 * On Windows earlier than 2008 SP2 which does not support
486 * VSS_VOLSNAP_ATTR_NO_AUTORECOVERY context, the final commit is not
487 * skipped and VSS is aborted by VSS_E_OBJECT_NOT_FOUND. However, as
488 * the system had been frozen until fsfreeze-thaw command was issued,
489 * we ignore this error.
491 vss_ctx
.pVssbc
->AbortBackup();
494 case VSS_E_UNEXPECTED_PROVIDER_ERROR
:
495 if (WaitForSingleObject(vss_ctx
.hEventTimeout
, 0) != WAIT_OBJECT_0
) {
496 err_set(errset
, hr
, "unexpected error in VSS provider");
499 /* fall through if hEventTimeout is signaled */
501 case (HRESULT
)VSS_E_HOLD_WRITES_TIMEOUT
:
502 err_set(errset
, hr
, "couldn't hold writes: "
503 "fsfreeze is limited up to 10 seconds");
507 err_set(errset
, hr
, "failed to do snapshot set");
510 if (err_is_set(errset
)) {
511 vss_ctx
.pVssbc
->AbortBackup();
513 *num_vols
= vss_ctx
.cFrozenVols
;