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 "vss-debug.h"
16 #include "requester.h"
21 /* Max wait time for frozen event (VSS can only hold writes for 10 seconds) */
22 #define VSS_TIMEOUT_FREEZE_MSEC 60000
24 /* Call QueryStatus every 10 ms while waiting for frozen event */
25 #define VSS_TIMEOUT_EVENT_MSEC 10
27 #define DEFAULT_VSS_BACKUP_TYPE VSS_BT_FULL
29 #define err_set(e, err, fmt, ...) \
30 ((e)->error_setg_win32_wrapper((e)->errp, __FILE__, __LINE__, __func__, \
31 err, fmt, ## __VA_ARGS__))
32 /* Bad idea, works only when (e)->errp != NULL: */
33 #define err_is_set(e) ((e)->errp && *(e)->errp)
34 /* To lift this restriction, error_propagate(), like we do in QEMU code */
36 /* Handle to VSSAPI.DLL */
39 /* Functions in VSSAPI.DLL */
40 typedef HRESULT(STDAPICALLTYPE
* t_CreateVssBackupComponents
)(
41 OUT IVssBackupComponents
**);
42 typedef void(APIENTRY
* t_VssFreeSnapshotProperties
)(IN VSS_SNAPSHOT_PROP
*);
43 static t_CreateVssBackupComponents pCreateVssBackupComponents
;
44 static t_VssFreeSnapshotProperties pVssFreeSnapshotProperties
;
46 /* Variables used while applications and filesystes are frozen by VSS */
47 static struct QGAVSSContext
{
48 IVssBackupComponents
*pVssbc
; /* VSS requester interface */
49 IVssAsync
*pAsyncSnapshot
; /* async info of VSS snapshot operation */
50 HANDLE hEventFrozen
; /* notify fs/writer freeze from provider */
51 HANDLE hEventThaw
; /* request provider to thaw */
52 HANDLE hEventTimeout
; /* notify timeout in provider */
53 int cFrozenVols
; /* number of frozen volumes */
56 STDAPI
requester_init(void)
58 COMInitializer initializer
; /* to call CoInitializeSecurity */
59 HRESULT hr
= CoInitializeSecurity(
60 NULL
, -1, NULL
, NULL
, RPC_C_AUTHN_LEVEL_PKT_PRIVACY
,
61 RPC_C_IMP_LEVEL_IDENTIFY
, NULL
, EOAC_NONE
, NULL
);
63 qga_debug("failed to CoInitializeSecurity (error %lx)", hr
);
67 hLib
= LoadLibraryA("VSSAPI.DLL");
69 qga_debug("failed to load VSSAPI.DLL");
70 return HRESULT_FROM_WIN32(GetLastError());
73 pCreateVssBackupComponents
= (t_CreateVssBackupComponents
)
75 #ifdef _WIN64 /* 64bit environment */
76 "?CreateVssBackupComponents@@YAJPEAPEAVIVssBackupComponents@@@Z"
77 #else /* 32bit environment */
78 "?CreateVssBackupComponents@@YGJPAPAVIVssBackupComponents@@@Z"
81 if (!pCreateVssBackupComponents
) {
82 qga_debug("failed to get proc address from VSSAPI.DLL");
83 return HRESULT_FROM_WIN32(GetLastError());
86 pVssFreeSnapshotProperties
= (t_VssFreeSnapshotProperties
)
87 GetProcAddress(hLib
, "VssFreeSnapshotProperties");
88 if (!pVssFreeSnapshotProperties
) {
89 qga_debug("failed to get proc address from VSSAPI.DLL");
90 return HRESULT_FROM_WIN32(GetLastError());
96 static void requester_cleanup(void)
98 if (vss_ctx
.hEventFrozen
) {
99 CloseHandle(vss_ctx
.hEventFrozen
);
100 vss_ctx
.hEventFrozen
= NULL
;
102 if (vss_ctx
.hEventThaw
) {
103 CloseHandle(vss_ctx
.hEventThaw
);
104 vss_ctx
.hEventThaw
= NULL
;
106 if (vss_ctx
.hEventTimeout
) {
107 CloseHandle(vss_ctx
.hEventTimeout
);
108 vss_ctx
.hEventTimeout
= NULL
;
110 if (vss_ctx
.pAsyncSnapshot
) {
111 vss_ctx
.pAsyncSnapshot
->Release();
112 vss_ctx
.pAsyncSnapshot
= NULL
;
114 if (vss_ctx
.pVssbc
) {
115 vss_ctx
.pVssbc
->Release();
116 vss_ctx
.pVssbc
= NULL
;
118 vss_ctx
.cFrozenVols
= 0;
121 STDAPI
requester_deinit(void)
125 pCreateVssBackupComponents
= NULL
;
126 pVssFreeSnapshotProperties
= NULL
;
135 static HRESULT
WaitForAsync(IVssAsync
*pAsync
)
145 hr
= pAsync
->QueryStatus(&ret
, NULL
);
150 } while (ret
== VSS_S_ASYNC_PENDING
);
155 static void AddComponents(ErrorSet
*errset
)
157 unsigned int cWriters
, i
;
158 VSS_ID id
, idInstance
, idWriter
;
159 BSTR bstrWriterName
= NULL
;
160 VSS_USAGE_TYPE usage
;
161 VSS_SOURCE_TYPE source
;
162 unsigned int cComponents
, c1
, c2
, j
;
163 COMPointer
<IVssExamineWriterMetadata
> pMetadata
;
164 COMPointer
<IVssWMComponent
> pComponent
;
165 PVSSCOMPONENTINFO info
;
168 hr
= vss_ctx
.pVssbc
->GetWriterMetadataCount(&cWriters
);
170 err_set(errset
, hr
, "failed to get writer metadata count");
174 for (i
= 0; i
< cWriters
; i
++) {
175 hr
= vss_ctx
.pVssbc
->GetWriterMetadata(i
, &id
, pMetadata
.replace());
177 err_set(errset
, hr
, "failed to get writer metadata of %d/%d",
182 hr
= pMetadata
->GetIdentity(&idInstance
, &idWriter
,
183 &bstrWriterName
, &usage
, &source
);
185 err_set(errset
, hr
, "failed to get identity of writer %d/%d",
190 hr
= pMetadata
->GetFileCounts(&c1
, &c2
, &cComponents
);
192 err_set(errset
, hr
, "failed to get file counts of %S",
197 for (j
= 0; j
< cComponents
; j
++) {
198 hr
= pMetadata
->GetComponent(j
, pComponent
.replace());
201 "failed to get component %d/%d of %S",
202 j
, cComponents
, bstrWriterName
);
206 hr
= pComponent
->GetComponentInfo(&info
);
209 "failed to get component info %d/%d of %S",
210 j
, cComponents
, bstrWriterName
);
214 if (info
->bSelectable
) {
215 hr
= vss_ctx
.pVssbc
->AddComponent(idInstance
, idWriter
,
217 info
->bstrLogicalPath
,
218 info
->bstrComponentName
);
220 err_set(errset
, hr
, "failed to add component %S(%S)",
221 info
->bstrComponentName
, bstrWriterName
);
225 SysFreeString(bstrWriterName
);
226 bstrWriterName
= NULL
;
227 pComponent
->FreeComponentInfo(info
);
232 if (bstrWriterName
) {
233 SysFreeString(bstrWriterName
);
235 if (pComponent
&& info
) {
236 pComponent
->FreeComponentInfo(info
);
240 DWORD
get_reg_dword_value(HKEY baseKey
, LPCSTR subKey
, LPCSTR valueName
,
243 DWORD regGetValueError
;
245 DWORD dataSize
= sizeof(DWORD
);
247 regGetValueError
= RegGetValue(baseKey
, subKey
, valueName
, RRF_RT_DWORD
,
248 NULL
, &dwordData
, &dataSize
);
249 if (regGetValueError
!= ERROR_SUCCESS
) {
255 bool is_valid_vss_backup_type(VSS_BACKUP_TYPE vssBT
)
257 return (vssBT
> VSS_BT_UNDEFINED
&& vssBT
< VSS_BT_OTHER
);
260 VSS_BACKUP_TYPE
get_vss_backup_type(
261 VSS_BACKUP_TYPE defaultVssBT
= DEFAULT_VSS_BACKUP_TYPE
)
263 VSS_BACKUP_TYPE vssBackupType
;
265 vssBackupType
= static_cast<VSS_BACKUP_TYPE
>(
266 get_reg_dword_value(HKEY_LOCAL_MACHINE
,
267 QGA_PROVIDER_REGISTRY_ADDRESS
,
270 if (!is_valid_vss_backup_type(vssBackupType
)) {
273 return vssBackupType
;
276 void requester_freeze(int *num_vols
, void *mountpoints
, ErrorSet
*errset
)
278 COMPointer
<IVssAsync
> pAsync
;
282 GUID guidSnapshotSet
= GUID_NULL
;
283 SECURITY_DESCRIPTOR sd
;
284 SECURITY_ATTRIBUTES sa
;
285 WCHAR short_volume_name
[64], *display_name
= short_volume_name
;
287 int num_fixed_drives
= 0, i
;
288 int num_mount_points
= 0;
289 VSS_BACKUP_TYPE vss_bt
= get_vss_backup_type();
291 if (vss_ctx
.pVssbc
) { /* already frozen */
298 /* Allow unrestricted access to events */
299 InitializeSecurityDescriptor(&sd
, SECURITY_DESCRIPTOR_REVISION
);
300 SetSecurityDescriptorDacl(&sd
, TRUE
, NULL
, FALSE
);
301 sa
.nLength
= sizeof(sa
);
302 sa
.lpSecurityDescriptor
= &sd
;
303 sa
.bInheritHandle
= FALSE
;
305 vss_ctx
.hEventFrozen
= CreateEvent(&sa
, TRUE
, FALSE
, EVENT_NAME_FROZEN
);
306 if (!vss_ctx
.hEventFrozen
) {
307 err_set(errset
, GetLastError(), "failed to create event %s",
311 vss_ctx
.hEventThaw
= CreateEvent(&sa
, TRUE
, FALSE
, EVENT_NAME_THAW
);
312 if (!vss_ctx
.hEventThaw
) {
313 err_set(errset
, GetLastError(), "failed to create event %s",
317 vss_ctx
.hEventTimeout
= CreateEvent(&sa
, TRUE
, FALSE
, EVENT_NAME_TIMEOUT
);
318 if (!vss_ctx
.hEventTimeout
) {
319 err_set(errset
, GetLastError(), "failed to create event %s",
324 assert(pCreateVssBackupComponents
!= NULL
);
325 hr
= pCreateVssBackupComponents(&vss_ctx
.pVssbc
);
327 err_set(errset
, hr
, "failed to create VSS backup components");
331 hr
= vss_ctx
.pVssbc
->InitializeForBackup();
333 err_set(errset
, hr
, "failed to initialize for backup");
337 hr
= vss_ctx
.pVssbc
->SetBackupState(true, true, vss_bt
, false);
339 err_set(errset
, hr
, "failed to set backup state");
344 * Currently writable snapshots are not supported.
345 * To prevent the final commit (which requires to write to snapshots),
346 * ATTR_NO_AUTORECOVERY and ATTR_TRANSPORTABLE are specified here.
348 ctx
= VSS_CTX_APP_ROLLBACK
| VSS_VOLSNAP_ATTR_TRANSPORTABLE
|
349 VSS_VOLSNAP_ATTR_NO_AUTORECOVERY
| VSS_VOLSNAP_ATTR_TXF_RECOVERY
;
350 hr
= vss_ctx
.pVssbc
->SetContext(ctx
);
351 if (hr
== (HRESULT
)VSS_E_UNSUPPORTED_CONTEXT
) {
352 /* Non-server version of Windows doesn't support ATTR_TRANSPORTABLE */
353 ctx
&= ~VSS_VOLSNAP_ATTR_TRANSPORTABLE
;
354 hr
= vss_ctx
.pVssbc
->SetContext(ctx
);
357 err_set(errset
, hr
, "failed to set backup context");
361 hr
= vss_ctx
.pVssbc
->GatherWriterMetadata(pAsync
.replace());
363 hr
= WaitForAsync(pAsync
);
366 err_set(errset
, hr
, "failed to gather writer metadata");
370 AddComponents(errset
);
371 if (err_is_set(errset
)) {
375 hr
= vss_ctx
.pVssbc
->StartSnapshotSet(&guidSnapshotSet
);
377 err_set(errset
, hr
, "failed to start snapshot set");
382 PWCHAR volume_name_wchar
;
383 for (volList
*list
= (volList
*)mountpoints
; list
; list
= list
->next
) {
384 size_t len
= strlen(list
->value
) + 1;
385 size_t converted
= 0;
388 volume_name_wchar
= new wchar_t[len
];
389 mbstowcs_s(&converted
, volume_name_wchar
, len
,
390 list
->value
, _TRUNCATE
);
392 hr
= vss_ctx
.pVssbc
->AddToSnapshotSet(volume_name_wchar
,
393 g_gProviderId
, &pid
);
395 err_set(errset
, hr
, "failed to add %S to snapshot set",
397 delete[] volume_name_wchar
;
402 delete[] volume_name_wchar
;
405 if (num_mount_points
== 0) {
406 /* If there is no valid mount points, just exit. */
412 volume
= FindFirstVolumeW(short_volume_name
, sizeof(short_volume_name
));
413 if (volume
== INVALID_HANDLE_VALUE
) {
414 err_set(errset
, hr
, "failed to find first volume");
419 if (GetDriveTypeW(short_volume_name
) == DRIVE_FIXED
) {
421 hr
= vss_ctx
.pVssbc
->AddToSnapshotSet(short_volume_name
,
422 g_gProviderId
, &pid
);
424 WCHAR volume_path_name
[PATH_MAX
];
425 if (GetVolumePathNamesForVolumeNameW(
426 short_volume_name
, volume_path_name
,
427 sizeof(volume_path_name
), NULL
) &&
429 display_name
= volume_path_name
;
431 err_set(errset
, hr
, "failed to add %S to snapshot set",
433 FindVolumeClose(volume
);
438 if (!FindNextVolumeW(volume
, short_volume_name
,
439 sizeof(short_volume_name
))) {
440 FindVolumeClose(volume
);
445 if (num_fixed_drives
== 0) {
446 goto out
; /* If there is no fixed drive, just exit. */
450 hr
= vss_ctx
.pVssbc
->PrepareForBackup(pAsync
.replace());
452 hr
= WaitForAsync(pAsync
);
455 err_set(errset
, hr
, "failed to prepare for backup");
459 hr
= vss_ctx
.pVssbc
->GatherWriterStatus(pAsync
.replace());
461 hr
= WaitForAsync(pAsync
);
464 err_set(errset
, hr
, "failed to gather writer status");
469 * Start VSS quiescing operations.
470 * CQGAVssProvider::CommitSnapshots will kick vss_ctx.hEventFrozen
471 * after the applications and filesystems are frozen.
473 hr
= vss_ctx
.pVssbc
->DoSnapshotSet(&vss_ctx
.pAsyncSnapshot
);
475 err_set(errset
, hr
, "failed to do snapshot set");
479 /* Need to call QueryStatus several times to make VSS provider progress */
480 for (i
= 0; i
< VSS_TIMEOUT_FREEZE_MSEC
/VSS_TIMEOUT_EVENT_MSEC
; i
++) {
481 HRESULT hr2
= vss_ctx
.pAsyncSnapshot
->QueryStatus(&hr
, NULL
);
483 err_set(errset
, hr
, "failed to do snapshot set");
486 if (hr
!= VSS_S_ASYNC_PENDING
) {
487 err_set(errset
, E_FAIL
,
488 "DoSnapshotSet exited without Frozen event");
491 wait_status
= WaitForSingleObject(vss_ctx
.hEventFrozen
,
492 VSS_TIMEOUT_EVENT_MSEC
);
493 if (wait_status
!= WAIT_TIMEOUT
) {
498 if (wait_status
== WAIT_TIMEOUT
) {
499 err_set(errset
, E_FAIL
,
500 "timeout when try to receive Frozen event from VSS provider");
501 /* If we are here, VSS had timeout.
502 * Don't call AbortBackup, just return directly.
507 if (wait_status
!= WAIT_OBJECT_0
) {
508 err_set(errset
, E_FAIL
,
509 "couldn't receive Frozen event from VSS provider");
514 *num_vols
= vss_ctx
.cFrozenVols
= num_mount_points
;
516 *num_vols
= vss_ctx
.cFrozenVols
= num_fixed_drives
;
522 if (vss_ctx
.pVssbc
) {
523 vss_ctx
.pVssbc
->AbortBackup();
532 void requester_thaw(int *num_vols
, void *mountpints
, ErrorSet
*errset
)
534 COMPointer
<IVssAsync
> pAsync
;
536 if (!vss_ctx
.hEventThaw
) {
538 * In this case, DoSnapshotSet is aborted or not started,
539 * and no volumes must be frozen. We return without an error.
545 /* Tell the provider that the snapshot is finished. */
546 SetEvent(vss_ctx
.hEventThaw
);
548 assert(vss_ctx
.pVssbc
);
549 assert(vss_ctx
.pAsyncSnapshot
);
551 HRESULT hr
= WaitForAsync(vss_ctx
.pAsyncSnapshot
);
553 case VSS_S_ASYNC_FINISHED
:
554 hr
= vss_ctx
.pVssbc
->BackupComplete(pAsync
.replace());
556 hr
= WaitForAsync(pAsync
);
559 err_set(errset
, hr
, "failed to complete backup");
563 case (HRESULT
)VSS_E_OBJECT_NOT_FOUND
:
565 * On Windows earlier than 2008 SP2 which does not support
566 * VSS_VOLSNAP_ATTR_NO_AUTORECOVERY context, the final commit is not
567 * skipped and VSS is aborted by VSS_E_OBJECT_NOT_FOUND. However, as
568 * the system had been frozen until fsfreeze-thaw command was issued,
569 * we ignore this error.
571 vss_ctx
.pVssbc
->AbortBackup();
574 case VSS_E_UNEXPECTED_PROVIDER_ERROR
:
575 if (WaitForSingleObject(vss_ctx
.hEventTimeout
, 0) != WAIT_OBJECT_0
) {
576 err_set(errset
, hr
, "unexpected error in VSS provider");
579 /* fall through if hEventTimeout is signaled */
581 case (HRESULT
)VSS_E_HOLD_WRITES_TIMEOUT
:
582 err_set(errset
, hr
, "couldn't hold writes: "
583 "fsfreeze is limited up to 10 seconds");
587 err_set(errset
, hr
, "failed to do snapshot set");
590 if (err_is_set(errset
)) {
591 vss_ctx
.pVssbc
->AbortBackup();
593 *num_vols
= vss_ctx
.cFrozenVols
;