vz_driver.c 113.5 KB
Newer Older
D
Dmitry Guryanov 已提交
1
/*
2
 * vz_driver.c: core driver functions for managing
D
Dmitry Guryanov 已提交
3 4
 * Parallels Cloud Server hosts
 *
5
 * Copyright (C) 2014-2015 Red Hat, Inc.
D
Dmitry Guryanov 已提交
6 7 8 9 10 11 12 13 14 15 16 17 18
 * Copyright (C) 2012 Parallels, Inc.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
19
 * License along with this library.  If not, see
D
Dmitry Guryanov 已提交
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
 * <http://www.gnu.org/licenses/>.
 *
 */

#include <config.h>

#include <sys/types.h>
#include <sys/poll.h>
#include <stdarg.h>
#include <unistd.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <pwd.h>
#include <sys/wait.h>
#include <sys/time.h>
#include <sys/statvfs.h>

#include "datatypes.h"
38
#include "virerror.h"
39
#include "viralloc.h"
40
#include "virlog.h"
41
#include "vircommand.h"
D
Dmitry Guryanov 已提交
42
#include "configmake.h"
43
#include "virfile.h"
44
#include "virstoragefile.h"
45
#include "virstring.h"
46
#include "cpu/cpu.h"
47
#include "virtypedparam.h"
48 49
#include "virhostmem.h"
#include "virhostcpu.h"
50
#include "viraccessapicheck.h"
D
Dmitry Guryanov 已提交
51

52 53 54
#include "vz_driver.h"
#include "vz_utils.h"
#include "vz_sdk.h"
D
Dmitry Guryanov 已提交
55 56 57

#define VIR_FROM_THIS VIR_FROM_PARALLELS

58 59
VIR_LOG_INIT("parallels.parallels_driver");

D
Dmitry Guryanov 已提交
60 61
#define PRLCTL                      "prlctl"

62
static virClassPtr vzDriverClass;
D
Dmitry Guryanov 已提交
63

64 65 66 67 68 69 70
static virMutex vz_driver_lock;
static vzDriverPtr vz_driver;
static vzConnPtr vz_conn_list;

static vzDriverPtr
vzDriverObjNew(void);

D
Dmitry Guryanov 已提交
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91
static int
vzCapsAddGuestDomain(virCapsPtr caps,
                     virDomainOSType ostype,
                     virArch arch,
                     const char * emulator,
                     virDomainVirtType virt_type)
{
    virCapsGuestPtr guest;

    if ((guest = virCapabilitiesAddGuest(caps, ostype, arch, emulator,
                                         NULL, 0, NULL)) == NULL)
        return -1;


    if (virCapabilitiesAddGuestDomain(guest, virt_type,
                                      NULL, NULL, 0, NULL) == NULL)
        return -1;

    return 0;
}

D
Dmitry Guryanov 已提交
92
static virCapsPtr
93
vzBuildCapabilities(void)
D
Dmitry Guryanov 已提交
94
{
95 96
    virCapsPtr caps = NULL;
    virNodeInfo nodeinfo;
D
Dmitry Guryanov 已提交
97 98 99 100 101
    virDomainOSType ostypes[] = {
        VIR_DOMAIN_OSTYPE_HVM,
        VIR_DOMAIN_OSTYPE_EXE
    };
    virArch archs[] = { VIR_ARCH_I686, VIR_ARCH_X86_64 };
102
    const char *const emulators[] = { "vz", "parallels"};
D
Dmitry Guryanov 已提交
103
    virDomainVirtType virt_types[] = {
104 105
        VIR_DOMAIN_VIRT_VZ,
        VIR_DOMAIN_VIRT_PARALLELS
D
Dmitry Guryanov 已提交
106 107
    };
    size_t i, j, k;
D
Dmitry Guryanov 已提交
108

109
    if ((caps = virCapabilitiesNew(virArchFromHost(),
110
                                   false, false)) == NULL)
111
        return NULL;
D
Dmitry Guryanov 已提交
112

M
Martin Kletzander 已提交
113
    if (virCapabilitiesInitNUMA(caps) < 0)
114
        goto error;
D
Dmitry Guryanov 已提交
115

116 117
    if (virCapabilitiesInitCaches(caps) < 0)
        goto error;
118 119 120

    verify(ARRAY_CARDINALITY(archs) == ARRAY_CARDINALITY(emulators));

121
    for (i = 0; i < ARRAY_CARDINALITY(ostypes); i++)
122 123
        for (j = 0; j < ARRAY_CARDINALITY(archs); j++)
            for (k = 0; k < ARRAY_CARDINALITY(emulators); k++)
D
Dmitry Guryanov 已提交
124 125 126
                if (vzCapsAddGuestDomain(caps, ostypes[i], archs[j],
                                         emulators[k], virt_types[k]) < 0)
                    goto error;
127

M
Martin Kletzander 已提交
128
    if (virCapabilitiesGetNodeInfo(&nodeinfo))
129 130
        goto error;

131
    if (!(caps->host.cpu = virCPUGetHost(caps->host.arch, VIR_CPU_TYPE_HOST,
132
                                         &nodeinfo, NULL)))
133 134
        goto error;

135 136 137
    if (virCapabilitiesAddHostMigrateTransport(caps, "vzmigr") < 0)
        goto error;

D
Dmitry Guryanov 已提交
138 139
    return caps;

140
 error:
141
    virObjectUnref(caps);
142
    return NULL;
D
Dmitry Guryanov 已提交
143 144
}

145 146 147 148
static void vzDriverDispose(void * obj)
{
    vzDriverPtr driver = obj;

149
    prlsdkDisconnect(driver);
150 151 152
    virObjectUnref(driver->domains);
    virObjectUnref(driver->caps);
    virObjectUnref(driver->xmlopt);
153
    virObjectUnref(driver->domainEventState);
154
    virSysinfoDefFree(driver->hostsysinfo);
155 156 157 158
}

static int vzDriverOnceInit(void)
{
159
    if (!VIR_CLASS_NEW(vzDriver, virClassForObjectLockable()))
160 161 162 163
        return -1;

    return 0;
}
164
VIR_ONCE_GLOBAL_INIT(vzDriver);
165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184

vzDriverPtr
vzGetDriverConnection(void)
{
    virMutexLock(&vz_driver_lock);
    if (!vz_driver)
        vz_driver = vzDriverObjNew();
    virObjectRef(vz_driver);
    virMutexUnlock(&vz_driver_lock);

    return vz_driver;
}

void
vzDestroyDriverConnection(void)
{
    vzDriverPtr driver;
    vzConnPtr privconn_list;

    virMutexLock(&vz_driver_lock);
185 186
    VIR_STEAL_PTR(driver, vz_driver);
    VIR_STEAL_PTR(privconn_list, vz_conn_list);
187 188 189 190 191 192 193 194 195 196 197
    virMutexUnlock(&vz_driver_lock);

    while (privconn_list) {
        vzConnPtr privconn = privconn_list;
        privconn_list = privconn->next;
        virConnectCloseCallbackDataCall(privconn->closeCallback,
                                        VIR_CONNECT_CLOSE_REASON_EOF);
    }
    virObjectUnref(driver);
}

D
Dmitry Guryanov 已提交
198
static char *
199
vzConnectGetCapabilities(virConnectPtr conn)
D
Dmitry Guryanov 已提交
200
{
201
    vzConnPtr privconn = conn->privateData;
D
Dmitry Guryanov 已提交
202 203
    char *xml;

204 205 206
    if (virConnectGetCapabilitiesEnsureACL(conn) < 0)
        return NULL;

207
    xml = virCapabilitiesFormatXML(privconn->driver->caps);
D
Dmitry Guryanov 已提交
208 209
    return xml;
}
210

211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231
static int
vzDomainDefAddDefaultInputDevices(virDomainDefPtr def)
{
    if (def->ngraphics == 0)
        return 0;

    int bus = IS_CT(def) ? VIR_DOMAIN_INPUT_BUS_PARALLELS :
                           VIR_DOMAIN_INPUT_BUS_PS2;

    if (virDomainDefMaybeAddInput(def,
                                  VIR_DOMAIN_INPUT_TYPE_MOUSE,
                                  bus) < 0)
        return -1;

    if (virDomainDefMaybeAddInput(def,
                                  VIR_DOMAIN_INPUT_TYPE_KBD,
                                  bus) < 0)
        return -1;

    return 0;
}
D
Dmitry Guryanov 已提交
232

233
static int
234
vzDomainDefPostParse(virDomainDefPtr def,
M
Michal Privoznik 已提交
235
                     virCapsPtr caps ATTRIBUTE_UNUSED,
236
                     unsigned int parseFlags ATTRIBUTE_UNUSED,
237 238
                     void *opaque ATTRIBUTE_UNUSED,
                     void *parseOpaque ATTRIBUTE_UNUSED)
239
{
240 241 242
    if (vzDomainDefAddDefaultInputDevices(def) < 0)
        return -1;

243 244 245 246 247 248 249 250
    return 0;
}

static int
vzDomainDefValidate(const virDomainDef *def,
                    virCapsPtr caps ATTRIBUTE_UNUSED,
                    void *opaque)
{
251 252 253
    if (vzCheckUnsupportedControllers(def, opaque) < 0)
        return -1;

254 255 256 257
    return 0;
}

static int
258
vzDomainDeviceDefPostParse(virDomainDeviceDefPtr dev,
M
Michal Privoznik 已提交
259 260
                           const virDomainDef *def,
                           virCapsPtr caps ATTRIBUTE_UNUSED,
261
                           unsigned int parseFlags ATTRIBUTE_UNUSED,
262 263
                           void *opaque ATTRIBUTE_UNUSED,
                           void *parseOpaque ATTRIBUTE_UNUSED)
264
{
265 266 267 268
    if (dev->type == VIR_DOMAIN_DEVICE_NET &&
        (dev->data.net->type == VIR_DOMAIN_NET_TYPE_NETWORK ||
         dev->data.net->type == VIR_DOMAIN_NET_TYPE_BRIDGE) &&
        !dev->data.net->model &&
269
        def->os.type == VIR_DOMAIN_OSTYPE_HVM &&
270
        VIR_STRDUP(dev->data.net->model, "e1000") < 0)
271
        return -1;
272

273 274 275 276 277 278 279 280
    return 0;
}

static int
vzDomainDeviceDefValidate(const virDomainDeviceDef *dev,
                          const virDomainDef *def,
                          void *opaque ATTRIBUTE_UNUSED)
{
281 282
    if (dev->type == VIR_DOMAIN_DEVICE_DISK)
        return vzCheckUnsupportedDisk(def, dev->data.disk, opaque);
283 284
    else if (dev->type == VIR_DOMAIN_DEVICE_GRAPHICS)
        return vzCheckUnsupportedGraphics(dev->data.graphics);
285 286

    return 0;
287 288
}

289 290 291 292
static virDomainXMLPrivateDataCallbacks vzDomainXMLPrivateDataCallbacksPtr = {
    .alloc = vzDomObjAlloc,
    .free = vzDomObjFree,
};
293

M
Maxim Nestratov 已提交
294
static virDomainDefParserConfig vzDomainDefParserConfig = {
295
    .macPrefix = {0x42, 0x1C, 0x00},
296
    .domainPostParseCallback = vzDomainDefPostParse,
297 298 299
    .devicesPostParseCallback = vzDomainDeviceDefPostParse,
    .domainValidateCallback = vzDomainDefValidate,
    .deviceValidateCallback = vzDomainDeviceDefValidate,
300 301
};

302 303
static vzDriverPtr
vzDriverObjNew(void)
D
Dmitry Guryanov 已提交
304
{
305
    vzDriverPtr driver;
D
Dmitry Guryanov 已提交
306

307 308
    if (vzDriverInitialize() < 0)
        return NULL;
D
Dmitry Guryanov 已提交
309

310 311
    if (!(driver = virObjectLockableNew(vzDriverClass)))
        return NULL;
312

313 314 315 316
    vzDomainDefParserConfig.priv = &driver->vzCaps;

    if (!(driver->caps = vzBuildCapabilities()) ||
        !(driver->xmlopt = virDomainXMLOptionNew(&vzDomainDefParserConfig,
317
                                                 &vzDomainXMLPrivateDataCallbacksPtr,
318
                                                 NULL, NULL, NULL)) ||
319 320 321
        !(driver->domains = virDomainObjListNew()) ||
        !(driver->domainEventState = virObjectEventStateNew()) ||
        (vzInitVersion(driver) < 0) ||
322
        (prlsdkConnect(driver) < 0)) {
323 324 325
        virObjectUnref(driver);
        return NULL;
    }
D
Dmitry Guryanov 已提交
326

327
    driver->hostsysinfo = virSysinfoRead();
328
    ignore_value(prlsdkLoadDomains(driver));
329 330 331 332 333

    /* As far as waitDomainJob finally calls virReportErrorHelper
     * and we are not going to report it, reset it expicitly*/
    virResetLastError();

334
    return driver;
D
Dmitry Guryanov 已提交
335 336 337
}

static virDrvOpenStatus
338
vzConnectOpen(virConnectPtr conn,
M
Michal Privoznik 已提交
339
              virConnectAuthPtr auth ATTRIBUTE_UNUSED,
340
              virConfPtr conf ATTRIBUTE_UNUSED,
M
Michal Privoznik 已提交
341
              unsigned int flags)
D
Dmitry Guryanov 已提交
342
{
343 344
    vzDriverPtr driver = NULL;
    vzConnPtr privconn = NULL;
D
Dmitry Guryanov 已提交
345 346 347 348

    virCheckFlags(VIR_CONNECT_RO, VIR_DRV_OPEN_ERROR);

    /* From this point on, the connection is for us. */
349
    if (STRNEQ(conn->uri->path, "/system")) {
350
        virReportError(VIR_ERR_INTERNAL_ERROR,
351
                       _("Unexpected Virtuozzo URI path '%s', try vz:///system"),
352
                       conn->uri->path);
D
Dmitry Guryanov 已提交
353 354 355
        return VIR_DRV_OPEN_ERROR;
    }

356 357 358
    if (virConnectOpenEnsureACL(conn) < 0)
        return VIR_DRV_OPEN_ERROR;

359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374
    if (!(driver = vzGetDriverConnection()))
        return VIR_DRV_OPEN_ERROR;

    if (VIR_ALLOC(privconn) < 0)
        goto error;

    conn->privateData = privconn;
    privconn->driver = driver;

    if (!(privconn->closeCallback = virNewConnectCloseCallbackData()))
        goto error;

    virMutexLock(&vz_driver_lock);
    privconn->next = vz_conn_list;
    vz_conn_list = privconn;
    virMutexUnlock(&vz_driver_lock);
D
Dmitry Guryanov 已提交
375 376

    return VIR_DRV_OPEN_SUCCESS;
377 378 379 380 381 382 383

 error:

    conn->privateData = NULL;
    virObjectUnref(driver);
    VIR_FREE(privconn);
    return VIR_DRV_OPEN_ERROR;
D
Dmitry Guryanov 已提交
384 385 386
}

static int
387
vzConnectClose(virConnectPtr conn)
D
Dmitry Guryanov 已提交
388
{
389
    vzConnPtr curr, *prev = &vz_conn_list;
390
    vzConnPtr privconn = conn->privateData;
D
Dmitry Guryanov 已提交
391

392 393 394
    if (!privconn)
        return 0;

395 396 397 398 399 400 401
    virMutexLock(&vz_driver_lock);
    for (curr = vz_conn_list; curr; prev = &curr->next, curr = curr->next) {
        if (curr == privconn) {
            *prev = curr->next;
            break;
        }
    }
D
Dmitry Guryanov 已提交
402

403
    virMutexUnlock(&vz_driver_lock);
D
Dmitry Guryanov 已提交
404

405 406
    virObjectUnref(privconn->closeCallback);
    virObjectUnref(privconn->driver);
D
Dmitry Guryanov 已提交
407
    VIR_FREE(privconn);
408
    conn->privateData = NULL;
D
Dmitry Guryanov 已提交
409 410 411 412
    return 0;
}

static int
413
vzConnectGetVersion(virConnectPtr conn, unsigned long *hvVer)
D
Dmitry Guryanov 已提交
414
{
415
    vzConnPtr privconn = conn->privateData;
416 417 418 419

    if (virConnectGetVersionEnsureACL(conn) < 0)
        return -1;

420
    *hvVer = privconn->driver->vzVersion;
421
    return 0;
422 423
}

424

425
static char *vzConnectGetHostname(virConnectPtr conn)
426
{
427 428 429
    if (virConnectGetHostnameEnsureACL(conn) < 0)
        return NULL;

430 431 432
    return virGetHostname();
}

433 434 435 436 437 438 439 440 441
static char *
vzConnectGetSysinfo(virConnectPtr conn, unsigned int flags)
{
    vzConnPtr privconn = conn->privateData;
    vzDriverPtr driver = privconn->driver;
    virBuffer buf = VIR_BUFFER_INITIALIZER;

    virCheckFlags(0, NULL);

442 443 444
    if (virConnectGetSysinfoEnsureACL(conn) < 0)
        return NULL;

445 446 447 448 449 450 451 452 453 454 455 456 457
    if (!driver->hostsysinfo) {
        virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
                       _("Host SMBIOS information is not available"));
        return NULL;
    }

    if (virSysinfoFormat(&buf, driver->hostsysinfo) < 0)
        return NULL;
    if (virBufferCheckError(&buf) < 0)
        return NULL;

    return virBufferContentAndReset(&buf);
}
458

459
static int
460
vzConnectListDomains(virConnectPtr conn, int *ids, int maxids)
461
{
462
    vzConnPtr privconn = conn->privateData;
463 464
    int n;

465 466 467
    if (virConnectListDomainsEnsureACL(conn) < 0)
        return -1;

468
    n = virDomainObjListGetActiveIDs(privconn->driver->domains, ids, maxids,
469
                                     virConnectListDomainsCheckACL, conn);
470 471 472 473 474

    return n;
}

static int
475
vzConnectNumOfDomains(virConnectPtr conn)
476
{
477
    vzConnPtr privconn = conn->privateData;
478 479
    int count;

480 481 482
    if (virConnectNumOfDomainsEnsureACL(conn) < 0)
        return -1;

483
    count = virDomainObjListNumOfDomains(privconn->driver->domains, true,
484
                                         virConnectNumOfDomainsCheckACL, conn);
485 486 487 488 489

    return count;
}

static int
490
vzConnectListDefinedDomains(virConnectPtr conn, char **const names, int maxnames)
491
{
492
    vzConnPtr privconn = conn->privateData;
493 494
    int n;

495 496 497
    if (virConnectListDefinedDomainsEnsureACL(conn) < 0)
        return -1;

498
    memset(names, 0, sizeof(*names) * maxnames);
499
    n = virDomainObjListGetInactiveNames(privconn->driver->domains, names,
500 501 502
                                         maxnames,
                                         virConnectListDefinedDomainsCheckACL,
                                         conn);
503 504 505 506 507

    return n;
}

static int
508
vzConnectNumOfDefinedDomains(virConnectPtr conn)
509
{
510
    vzConnPtr privconn = conn->privateData;
511 512
    int count;

513 514 515
    if (virConnectNumOfDefinedDomainsEnsureACL(conn) < 0)
        return -1;

516
    count = virDomainObjListNumOfDomains(privconn->driver->domains, false,
517 518
                                         virConnectNumOfDefinedDomainsCheckACL,
                                         conn);
519 520 521 522
    return count;
}

static int
523
vzConnectListAllDomains(virConnectPtr conn,
M
Michal Privoznik 已提交
524 525
                        virDomainPtr **domains,
                        unsigned int flags)
526
{
527
    vzConnPtr privconn = conn->privateData;
528 529
    int ret = -1;

O
Osier Yang 已提交
530
    virCheckFlags(VIR_CONNECT_LIST_DOMAINS_FILTERS_ALL, -1);
531 532 533 534

    if (virConnectListAllDomainsEnsureACL(conn) < 0)
        return -1;

535
    ret = virDomainObjListExport(privconn->driver->domains, conn, domains,
536
                                 virConnectListAllDomainsCheckACL, flags);
537 538 539 540 541

    return ret;
}

static virDomainPtr
542
vzDomainLookupByID(virConnectPtr conn, int id)
543
{
544
    vzConnPtr privconn = conn->privateData;
545
    virDomainPtr ret = NULL;
546 547
    virDomainObjPtr dom;

548
    dom = virDomainObjListFindByID(privconn->driver->domains, id);
549 550 551

    if (dom == NULL) {
        virReportError(VIR_ERR_NO_DOMAIN, NULL);
552
        return NULL;
553 554
    }

555 556 557
    if (virDomainLookupByIDEnsureACL(conn, dom->def) < 0)
        goto cleanup;

558
    ret = virGetDomain(conn, dom->def->name, dom->def->uuid, dom->def->id);
559

560
 cleanup:
561
    virDomainObjEndAPI(&dom);
562 563 564 565
    return ret;
}

static virDomainPtr
566
vzDomainLookupByUUID(virConnectPtr conn, const unsigned char *uuid)
567
{
568
    vzConnPtr privconn = conn->privateData;
569
    virDomainPtr ret = NULL;
570 571
    virDomainObjPtr dom;

572
    dom = virDomainObjListFindByUUID(privconn->driver->domains, uuid);
573 574 575 576 577 578

    if (dom == NULL) {
        char uuidstr[VIR_UUID_STRING_BUFLEN];
        virUUIDFormat(uuid, uuidstr);
        virReportError(VIR_ERR_NO_DOMAIN,
                       _("no domain with matching uuid '%s'"), uuidstr);
579
        return NULL;
580 581
    }

582 583 584
    if (virDomainLookupByUUIDEnsureACL(conn, dom->def) < 0)
        goto cleanup;

585
    ret = virGetDomain(conn, dom->def->name, dom->def->uuid, dom->def->id);
586

587
 cleanup:
588
    virDomainObjEndAPI(&dom);
589 590 591 592
    return ret;
}

static virDomainPtr
593
vzDomainLookupByName(virConnectPtr conn, const char *name)
594
{
595
    vzConnPtr privconn = conn->privateData;
596
    virDomainPtr ret = NULL;
597 598
    virDomainObjPtr dom;

599
    dom = virDomainObjListFindByName(privconn->driver->domains, name);
600 601 602 603

    if (dom == NULL) {
        virReportError(VIR_ERR_NO_DOMAIN,
                       _("no domain with matching name '%s'"), name);
604
        return NULL;
605 606
    }

607 608 609
    if (virDomainLookupByNameEnsureACL(conn, dom->def) < 0)
        goto cleanup;

610
    ret = virGetDomain(conn, dom->def->name, dom->def->uuid, dom->def->id);
611

612
 cleanup:
613
    virDomainObjEndAPI(&dom);
614 615 616 617
    return ret;
}

static int
618
vzDomainGetInfo(virDomainPtr domain, virDomainInfoPtr info)
619
{
620
    virDomainObjPtr dom;
621
    vzDomObjPtr privdom;
622 623
    int ret = -1;

J
John Ferlan 已提交
624
    if (!(dom = vzDomObjFromDomain(domain)))
625 626
        goto cleanup;

627 628 629
    if (virDomainGetInfoEnsureACL(domain->conn, dom->def) < 0)
        goto cleanup;

630 631
    info->state = virDomainObjGetState(dom, NULL);
    info->memory = dom->def->mem.cur_balloon;
632
    info->maxMem = virDomainDefGetMemoryTotal(dom->def);
633
    info->nrVirtCpu = virDomainDefGetVcpus(dom->def);
634
    info->cpuTime = 0;
635

636 637 638
    privdom = dom->privateData;

    if (PRL_INVALID_HANDLE != privdom->stats && virDomainObjIsActive(dom)) {
639 640 641
        unsigned long long vtime;
        size_t i;

642
        for (i = 0; i < virDomainDefGetVcpus(dom->def); ++i) {
643
            if (prlsdkGetVcpuStats(privdom->stats, i, &vtime) < 0) {
644 645 646 647 648 649 650
                virReportError(VIR_ERR_OPERATION_FAILED, "%s",
                               _("cannot read cputime for domain"));
                goto cleanup;
            }
            info->cpuTime += vtime;
        }
    }
651 652
    ret = 0;

653
 cleanup:
654
    virDomainObjEndAPI(&dom);
655 656 657 658
    return ret;
}

static char *
659
vzDomainGetOSType(virDomainPtr domain)
660
{
661
    virDomainObjPtr dom;
662 663
    char *ret = NULL;

664
    if (!(dom = vzDomObjFromDomain(domain)))
665
        return NULL;
666

667 668 669
    if (virDomainGetOSTypeEnsureACL(domain->conn, dom->def) < 0)
        goto cleanup;

670
    ignore_value(VIR_STRDUP(ret, virDomainOSTypeToString(dom->def->os.type)));
671

672
 cleanup:
J
John Ferlan 已提交
673
    virDomainObjEndAPI(&dom);
674 675 676 677
    return ret;
}

static int
678
vzDomainIsPersistent(virDomainPtr domain)
679
{
680
    virDomainObjPtr dom;
681
    int ret = -1;
682

683
    if (!(dom = vzDomObjFromDomain(domain)))
684
        return -1;
685

686 687 688 689 690 691
    if (virDomainIsPersistentEnsureACL(domain->conn, dom->def) < 0)
        goto cleanup;

    ret = 1;

 cleanup:
J
John Ferlan 已提交
692
    virDomainObjEndAPI(&dom);
693
    return ret;
694 695 696
}

static int
697
vzDomainGetState(virDomainPtr domain,
M
Michal Privoznik 已提交
698
                 int *state, int *reason, unsigned int flags)
699
{
700
    virDomainObjPtr dom;
701
    int ret = -1;
702

703 704
    virCheckFlags(0, -1);

705
    if (!(dom = vzDomObjFromDomain(domain)))
706
        return -1;
707

708 709 710
    if (virDomainGetStateEnsureACL(domain->conn, dom->def) < 0)
        goto cleanup;

711
    *state = virDomainObjGetState(dom, reason);
712
    ret = 0;
713

714
 cleanup:
J
John Ferlan 已提交
715
    virDomainObjEndAPI(&dom);
716
    return ret;
717 718 719
}

static char *
720
vzDomainGetXMLDesc(virDomainPtr domain, unsigned int flags)
721
{
722
    vzConnPtr privconn = domain->conn->privateData;
723
    virDomainDefPtr def;
724
    virDomainObjPtr dom;
725 726
    char *ret = NULL;

727
    virCheckFlags(VIR_DOMAIN_XML_COMMON_FLAGS, NULL);
728

729
    if (!(dom = vzDomObjFromDomain(domain)))
730
        return NULL;
731

732 733 734
    if (virDomainGetXMLDescEnsureACL(domain->conn, dom->def, flags) < 0)
        goto cleanup;

735
    def = (flags & VIR_DOMAIN_XML_INACTIVE) &&
736
        dom->newDef ? dom->newDef : dom->def;
737

738
    ret = virDomainDefFormat(def, privconn->driver->caps, flags);
739

740
 cleanup:
J
John Ferlan 已提交
741
    virDomainObjEndAPI(&dom);
742 743 744 745
    return ret;
}

static int
746
vzDomainGetAutostart(virDomainPtr domain, int *autostart)
747
{
748
    virDomainObjPtr dom;
749
    int ret = -1;
750

751
    if (!(dom = vzDomObjFromDomain(domain)))
752
        return -1;
753

754 755 756
    if (virDomainGetAutostartEnsureACL(domain->conn, dom->def) < 0)
        goto cleanup;

757
    *autostart = dom->autostart;
758
    ret = 0;
759

760
 cleanup:
J
John Ferlan 已提交
761
    virDomainObjEndAPI(&dom);
762
    return ret;
D
Dmitry Guryanov 已提交
763 764
}

765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780
static int
vzEnsureDomainExists(virDomainObjPtr dom)
{
    char uuidstr[VIR_UUID_STRING_BUFLEN];

    if (!dom->removing)
        return 0;

    virUUIDFormat(dom->def->uuid, uuidstr);
    virReportError(VIR_ERR_NO_DOMAIN,
                   _("no domain with matching uuid '%s' (%s)"),
                   uuidstr, dom->def->name);

    return -1;
}

781
static virDomainPtr
782
vzDomainDefineXMLFlags(virConnectPtr conn, const char *xml, unsigned int flags)
783
{
784
    vzConnPtr privconn = conn->privateData;
785
    virDomainPtr retdom = NULL;
786
    virDomainDefPtr def;
787
    virDomainObjPtr dom = NULL;
788
    unsigned int parse_flags = VIR_DOMAIN_DEF_PARSE_INACTIVE;
789
    vzDriverPtr driver = privconn->driver;
790
    bool job = false;
791

792 793 794
    virCheckFlags(VIR_DOMAIN_DEFINE_VALIDATE, NULL);

    if (flags & VIR_DOMAIN_DEFINE_VALIDATE)
795
        parse_flags |= VIR_DOMAIN_DEF_PARSE_VALIDATE_SCHEMA;
796

797
    if ((def = virDomainDefParseString(xml, driver->caps, driver->xmlopt,
798
                                       NULL, parse_flags)) == NULL)
799 800
        goto cleanup;

801 802 803
    if (virXMLCheckIllegalChars("name", def->name, "\n") < 0)
        goto cleanup;

804 805 806
    if (virDomainDefineXMLFlagsEnsureACL(conn, def) < 0)
        goto cleanup;

807
    dom = virDomainObjListFindByUUID(driver->domains, def->uuid);
808
    if (dom == NULL) {
809
        virResetLastError();
810
        if (def->os.type == VIR_DOMAIN_OSTYPE_HVM) {
811
            if (prlsdkCreateVm(driver, def))
812
                goto cleanup;
813
        } else if (def->os.type == VIR_DOMAIN_OSTYPE_EXE) {
814
            if (prlsdkCreateCt(conn, def))
815 816 817
                goto cleanup;
        } else {
            virReportError(VIR_ERR_INVALID_ARG,
818 819
                           _("Unsupported OS type: %s"),
                           virDomainOSTypeToString(def->os.type));
820
            goto cleanup;
821
        }
822

823
        if (!(dom = prlsdkAddDomainByUUID(driver, def->uuid)))
824
            goto cleanup;
825
    } else {
826 827
        int state, reason;

828
        state = virDomainObjGetState(dom, &reason);
829 830 831 832 833 834 835 836 837 838 839 840 841

        if (state == VIR_DOMAIN_SHUTOFF &&
            reason == VIR_DOMAIN_SHUTOFF_SAVED) {

            /* PCS doesn't store domain config in managed save state file.
             * It's forbidden to change config for VMs in this state.
             * It's possible to change config for containers, but after
             * restoring domain will have that new config, not a config,
             * which domain had at the moment of virDomainManagedSave.
             *
             * So forbid this operation, if config is changed. If it's
             * not changed - just do nothing. */

842
            if (!virDomainDefCheckABIStability(dom->def, def, driver->xmlopt)) {
843 844 845 846 847 848
                virReportError(VIR_ERR_ARGUMENT_UNSUPPORTED, "%s",
                               _("Can't change domain configuration "
                                 "in managed save state"));
                goto cleanup;
            }
        } else {
849 850 851 852
            if (vzDomainObjBeginJob(dom) < 0)
                goto cleanup;
            job = true;

853 854 855
            if (vzEnsureDomainExists(dom) < 0)
                goto cleanup;

856
            if (prlsdkApplyConfig(driver, dom, def))
857
                goto cleanup;
858

859
            if (prlsdkUpdateDomain(driver, dom))
860 861
                goto cleanup;
        }
862 863
    }

864
    retdom = virGetDomain(conn, def->name, def->uuid, def->id);
865

866
 cleanup:
867 868 869
    if (job)
        vzDomainObjEndJob(dom);
    virDomainObjEndAPI(&dom);
870
    virDomainDefFree(def);
871
    return retdom;
872 873
}

874
static virDomainPtr
875
vzDomainDefineXML(virConnectPtr conn, const char *xml)
876
{
877
    return vzDomainDefineXMLFlags(conn, xml, 0);
878 879 880
}


881
static int
882
vzNodeGetInfo(virConnectPtr conn,
M
Michal Privoznik 已提交
883
              virNodeInfoPtr nodeinfo)
884
{
885 886 887
    if (virNodeGetInfoEnsureACL(conn) < 0)
        return -1;

M
Martin Kletzander 已提交
888
    return virCapabilitiesGetNodeInfo(nodeinfo);
889 890
}

891
static int vzConnectIsEncrypted(virConnectPtr conn ATTRIBUTE_UNUSED)
892 893 894 895 896
{
    /* Encryption is not relevant / applicable to way we talk to PCS */
    return 0;
}

897
static int vzConnectIsSecure(virConnectPtr conn ATTRIBUTE_UNUSED)
898 899 900 901 902
{
    /* We run CLI tools directly so this is secure */
    return 1;
}

903
static int vzConnectIsAlive(virConnectPtr conn ATTRIBUTE_UNUSED)
904 905 906 907
{
    return 1;
}

908

909
static char *
910
vzConnectBaselineCPU(virConnectPtr conn,
M
Michal Privoznik 已提交
911 912 913
                     const char **xmlCPUs,
                     unsigned int ncpus,
                     unsigned int flags)
914
{
J
Jiri Denemark 已提交
915 916 917 918
    virCPUDefPtr *cpus = NULL;
    virCPUDefPtr cpu = NULL;
    char *cpustr = NULL;

919 920
    virCheckFlags(VIR_CONNECT_BASELINE_CPU_EXPAND_FEATURES, NULL);

921 922 923
    if (virConnectBaselineCPUEnsureACL(conn) < 0)
        return NULL;

J
Jiri Denemark 已提交
924 925 926
    if (!(cpus = virCPUDefListParse(xmlCPUs, ncpus, VIR_CPU_TYPE_HOST)))
        goto cleanup;

927
    if (!(cpu = virCPUBaseline(VIR_ARCH_NONE, cpus, ncpus, NULL, NULL, false)))
J
Jiri Denemark 已提交
928 929 930 931 932 933
        goto cleanup;

    if ((flags & VIR_CONNECT_BASELINE_CPU_EXPAND_FEATURES) &&
        virCPUExpandFeatures(cpus[0]->arch, cpu) < 0)
        goto cleanup;

934
    cpustr = virCPUDefFormat(cpu, NULL);
J
Jiri Denemark 已提交
935 936 937 938 939 940

 cleanup:
    virCPUDefListFree(cpus);
    virCPUDefFree(cpu);

    return cpustr;
941 942 943
}


944
static int
945
vzDomainGetVcpus(virDomainPtr domain,
M
Michal Privoznik 已提交
946 947 948 949
                 virVcpuInfoPtr info,
                 int maxinfo,
                 unsigned char *cpumaps,
                 int maplen)
950
{
951
    virDomainObjPtr dom = NULL;
952 953 954
    size_t i;
    int ret = -1;

J
John Ferlan 已提交
955
    if (!(dom = vzDomObjFromDomain(domain)))
956
        return -1;
957

958 959 960
    if (virDomainGetVcpusEnsureACL(domain->conn, dom->def) < 0)
        goto cleanup;

961
    if (!virDomainObjIsActive(dom)) {
962 963 964 965 966 967 968 969
        virReportError(VIR_ERR_OPERATION_INVALID,
                       "%s",
                       _("cannot list vcpu pinning for an inactive domain"));
        goto cleanup;
    }

    if (maxinfo >= 1) {
        if (info != NULL) {
970 971
        vzDomObjPtr privdom;

972
            memset(info, 0, sizeof(*info) * maxinfo);
973 974
            privdom = dom->privateData;

975 976 977
            for (i = 0; i < maxinfo; i++) {
                info[i].number = i;
                info[i].state = VIR_VCPU_RUNNING;
978
                if (prlsdkGetVcpuStats(privdom->stats, i, &info[i].cpuTime) < 0)
N
Nikolay Shirokovskiy 已提交
979
                    goto cleanup;
980 981 982 983
            }
        }
        if (cpumaps != NULL) {
            memset(cpumaps, 0, maplen * maxinfo);
984
            for (i = 0; i < maxinfo; i++)
985
                virBitmapToDataBuf(dom->def->cpumask,
986 987
                                   VIR_GET_CPUMAP(cpumaps, maplen, i),
                                   maplen);
988 989 990 991 992
        }
    }
    ret = maxinfo;

 cleanup:
993
    virDomainObjEndAPI(&dom);
994 995 996 997
    return ret;
}


998
static int
999
vzNodeGetCPUMap(virConnectPtr conn,
M
Michal Privoznik 已提交
1000 1001 1002
                unsigned char **cpumap,
                unsigned int *online,
                unsigned int flags)
1003
{
1004 1005 1006
    if (virNodeGetCPUMapEnsureACL(conn) < 0)
        return -1;

1007
    return virHostCPUGetMap(cpumap, online, flags);
1008 1009
}

1010
static int
1011
vzConnectDomainEventRegisterAny(virConnectPtr conn,
M
Michal Privoznik 已提交
1012 1013 1014 1015 1016
                                virDomainPtr domain,
                                int eventID,
                                virConnectDomainEventGenericCallback callback,
                                void *opaque,
                                virFreeCallback freecb)
1017 1018
{
    int ret = -1;
1019
    vzConnPtr privconn = conn->privateData;
1020 1021 1022 1023

    if (virConnectDomainEventRegisterAnyEnsureACL(conn) < 0)
        return -1;

1024
    if (virDomainEventStateRegisterID(conn,
1025
                                      privconn->driver->domainEventState,
1026 1027 1028 1029 1030 1031 1032
                                      domain, eventID,
                                      callback, opaque, freecb, &ret) < 0)
        ret = -1;
    return ret;
}

static int
1033
vzConnectDomainEventDeregisterAny(virConnectPtr conn,
M
Michal Privoznik 已提交
1034
                                  int callbackID)
1035
{
1036
    vzConnPtr privconn = conn->privateData;
1037

1038 1039 1040
    if (virConnectDomainEventDeregisterAnyEnsureACL(conn) < 0)
        return -1;

1041
    if (virObjectEventStateDeregisterID(conn,
1042
                                        privconn->driver->domainEventState,
1043
                                        callbackID, true) < 0)
1044
        return -1;
1045

1046
    return 0;
1047
}
1048

1049 1050
static int
vzDomainSuspend(virDomainPtr domain)
1051
{
1052 1053 1054 1055 1056
    vzConnPtr privconn = domain->conn->privateData;
    virDomainObjPtr dom;
    int ret = -1;
    bool job = false;

J
John Ferlan 已提交
1057
    if (!(dom = vzDomObjFromDomain(domain)))
1058 1059
        return -1;

1060 1061 1062
    if (virDomainSuspendEnsureACL(domain->conn, dom->def) < 0)
        goto cleanup;

1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083
    if (vzDomainObjBeginJob(dom) < 0)
        goto cleanup;
    job = true;

    if (vzEnsureDomainExists(dom) < 0)
        goto cleanup;

    if (prlsdkPause(dom) < 0)
        goto cleanup;

    if (prlsdkUpdateDomain(privconn->driver, dom) < 0)
        goto cleanup;

    ret = 0;

 cleanup:
    if (job)
        vzDomainObjEndJob(dom);
    virDomainObjEndAPI(&dom);

    return ret;
1084 1085
}

1086 1087
static int
vzDomainResume(virDomainPtr domain)
1088
{
1089 1090 1091 1092 1093
    vzConnPtr privconn = domain->conn->privateData;
    virDomainObjPtr dom;
    int ret = -1;
    bool job = false;

J
John Ferlan 已提交
1094
    if (!(dom = vzDomObjFromDomain(domain)))
1095 1096
        return -1;

1097 1098 1099
    if (virDomainResumeEnsureACL(domain->conn, dom->def) < 0)
        goto cleanup;

1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120
    if (vzDomainObjBeginJob(dom) < 0)
        goto cleanup;
    job = true;

    if (vzEnsureDomainExists(dom) < 0)
        goto cleanup;

    if (prlsdkResume(dom) < 0)
        goto cleanup;

    if (prlsdkUpdateDomain(privconn->driver, dom) < 0)
        goto cleanup;

    ret = 0;

 cleanup:
    if (job)
        vzDomainObjEndJob(dom);
    virDomainObjEndAPI(&dom);

    return ret;
1121 1122
}

1123
static int
1124
vzDomainCreateWithFlags(virDomainPtr domain, unsigned int flags)
1125
{
1126 1127 1128 1129 1130
    vzConnPtr privconn = domain->conn->privateData;
    virDomainObjPtr dom;
    int ret = -1;
    bool job = false;

1131 1132
    virCheckFlags(0, -1);

J
John Ferlan 已提交
1133
    if (!(dom = vzDomObjFromDomain(domain)))
1134 1135
        return -1;

1136 1137 1138
    if (virDomainCreateWithFlagsEnsureACL(domain->conn, dom->def) < 0)
        goto cleanup;

1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159
    if (vzDomainObjBeginJob(dom) < 0)
        goto cleanup;
    job = true;

    if (vzEnsureDomainExists(dom) < 0)
        goto cleanup;

    if (prlsdkStart(dom) < 0)
        goto cleanup;

    if (prlsdkUpdateDomain(privconn->driver, dom) < 0)
        goto cleanup;

    ret = 0;

 cleanup:
    if (job)
        vzDomainObjEndJob(dom);
    virDomainObjEndAPI(&dom);

    return ret;
1160 1161
}

1162
static int
1163
vzDomainDestroyFlags(virDomainPtr domain, unsigned int flags)
1164
{
1165 1166 1167 1168 1169
    vzConnPtr privconn = domain->conn->privateData;
    virDomainObjPtr dom;
    int ret = -1;
    bool job = false;

1170 1171
    virCheckFlags(0, -1);

J
John Ferlan 已提交
1172
    if (!(dom = vzDomObjFromDomain(domain)))
1173 1174
        return -1;

1175 1176 1177
    if (virDomainDestroyFlagsEnsureACL(domain->conn, dom->def) < 0)
        goto cleanup;

1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198
    if (vzDomainObjBeginJob(dom) < 0)
        goto cleanup;
    job = true;

    if (vzEnsureDomainExists(dom) < 0)
        goto cleanup;

    if (prlsdkKill(dom) < 0)
        goto cleanup;

    if (prlsdkUpdateDomain(privconn->driver, dom) < 0)
        goto cleanup;

    ret = 0;

 cleanup:
    if (job)
        vzDomainObjEndJob(dom);
    virDomainObjEndAPI(&dom);

    return ret;
1199 1200
}

1201
static int
1202 1203 1204 1205 1206 1207 1208
vzDomainDestroy(virDomainPtr dom)
{
    return vzDomainDestroyFlags(dom, 0);
}

static int
vzDomainShutdownFlags(virDomainPtr domain, unsigned int flags)
1209
{
1210 1211 1212 1213 1214
    vzConnPtr privconn = domain->conn->privateData;
    virDomainObjPtr dom;
    int ret = -1;
    bool job = false;

1215 1216
    virCheckFlags(0, -1);

J
John Ferlan 已提交
1217
    if (!(dom = vzDomObjFromDomain(domain)))
1218 1219
        return -1;

1220 1221 1222
    if (virDomainShutdownFlagsEnsureACL(domain->conn, dom->def, flags) < 0)
        goto cleanup;

1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243
    if (vzDomainObjBeginJob(dom) < 0)
        goto cleanup;
    job = true;

    if (vzEnsureDomainExists(dom) < 0)
        goto cleanup;

    if (prlsdkStop(dom) < 0)
        goto cleanup;

    if (prlsdkUpdateDomain(privconn->driver, dom) < 0)
        goto cleanup;

    ret = 0;

 cleanup:
    if (job)
        vzDomainObjEndJob(dom);
    virDomainObjEndAPI(&dom);

    return ret;
1244 1245
}

1246 1247 1248 1249 1250
static int vzDomainShutdown(virDomainPtr dom)
{
    return vzDomainShutdownFlags(dom, 0);
}

1251 1252
static int
vzDomainReboot(virDomainPtr domain, unsigned int flags)
1253
{
1254 1255 1256 1257 1258
    vzConnPtr privconn = domain->conn->privateData;
    virDomainObjPtr dom;
    int ret = -1;
    bool job = false;

1259
    virCheckFlags(0, -1);
1260

J
John Ferlan 已提交
1261
    if (!(dom = vzDomObjFromDomain(domain)))
1262 1263
        return -1;

1264 1265 1266
    if (virDomainRebootEnsureACL(domain->conn, dom->def, flags) < 0)
        goto cleanup;

1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287
    if (vzDomainObjBeginJob(dom) < 0)
        goto cleanup;
    job = true;

    if (vzEnsureDomainExists(dom) < 0)
        goto cleanup;

    if (prlsdkRestart(dom) < 0)
        goto cleanup;

    if (prlsdkUpdateDomain(privconn->driver, dom) < 0)
        goto cleanup;

    ret = 0;

 cleanup:
    if (job)
        vzDomainObjEndJob(dom);
    virDomainObjEndAPI(&dom);

    return ret;
1288 1289
}

1290
static int vzDomainIsActive(virDomainPtr domain)
1291 1292 1293 1294
{
    virDomainObjPtr dom = NULL;
    int ret = -1;

1295
    if (!(dom = vzDomObjFromDomain(domain)))
1296 1297
        return -1;

1298 1299 1300
    if (virDomainIsActiveEnsureACL(domain->conn, dom->def) < 0)
        goto cleanup;

1301
    ret = virDomainObjIsActive(dom);
1302 1303

 cleanup:
J
John Ferlan 已提交
1304
    virDomainObjEndAPI(&dom);
1305 1306 1307 1308

    return ret;
}

1309
static int
1310
vzDomainCreate(virDomainPtr domain)
1311
{
1312
    return vzDomainCreateWithFlags(domain, 0);
1313 1314
}

1315
static int
1316
vzDomainUndefineFlags(virDomainPtr domain,
M
Michal Privoznik 已提交
1317
                      unsigned int flags)
1318
{
1319
    vzConnPtr privconn = domain->conn->privateData;
1320
    virDomainObjPtr dom = NULL;
1321 1322
    int ret = -1;
    bool job = false;
1323

1324 1325
    virCheckFlags(VIR_DOMAIN_UNDEFINE_MANAGED_SAVE |
                  VIR_DOMAIN_UNDEFINE_SNAPSHOTS_METADATA, -1);
1326

J
John Ferlan 已提交
1327
    if (!(dom = vzDomObjFromDomain(domain)))
1328 1329
        return -1;

1330 1331 1332
    if (virDomainUndefineFlagsEnsureACL(domain->conn, dom->def) < 0)
        goto cleanup;

1333 1334 1335 1336
    if (vzDomainObjBeginJob(dom) < 0)
        goto cleanup;
    job = true;

1337 1338 1339
    if (vzEnsureDomainExists(dom) < 0)
        goto cleanup;

1340
    ret = prlsdkUnregisterDomain(privconn->driver, dom, flags);
1341 1342 1343 1344 1345 1346

 cleanup:

    if (job)
        vzDomainObjEndJob(dom);
    virDomainObjEndAPI(&dom);
1347 1348

    return ret;
1349 1350 1351
}

static int
1352
vzDomainUndefine(virDomainPtr domain)
1353
{
1354
    return vzDomainUndefineFlags(domain, 0);
1355 1356
}

1357
static int
1358
vzDomainHasManagedSaveImage(virDomainPtr domain, unsigned int flags)
1359 1360
{
    virDomainObjPtr dom = NULL;
1361
    int state, reason;
1362
    int ret = -1;
1363 1364 1365

    virCheckFlags(0, -1);

1366
    if (!(dom = vzDomObjFromDomain(domain)))
1367 1368
        return -1;

1369 1370 1371
    if (virDomainHasManagedSaveImageEnsureACL(domain->conn, dom->def) < 0)
        goto cleanup;

1372 1373 1374
    state = virDomainObjGetState(dom, &reason);
    if (state == VIR_DOMAIN_SHUTOFF && reason == VIR_DOMAIN_SHUTOFF_SAVED)
        ret = 1;
1375 1376 1377 1378
    else
        ret = 0;

 cleanup:
J
John Ferlan 已提交
1379
    virDomainObjEndAPI(&dom);
1380

1381 1382 1383 1384
    return ret;
}

static int
1385
vzDomainManagedSave(virDomainPtr domain, unsigned int flags)
1386
{
1387
    vzConnPtr privconn = domain->conn->privateData;
1388 1389 1390
    virDomainObjPtr dom = NULL;
    int state, reason;
    int ret = -1;
1391
    bool job = false;
1392 1393 1394 1395

    virCheckFlags(VIR_DOMAIN_SAVE_RUNNING |
                  VIR_DOMAIN_SAVE_PAUSED, -1);

J
John Ferlan 已提交
1396
    if (!(dom = vzDomObjFromDomain(domain)))
1397 1398
        return -1;

1399 1400 1401
    if (virDomainManagedSaveEnsureACL(domain->conn, dom->def) < 0)
        goto cleanup;

1402 1403 1404 1405
    if (vzDomainObjBeginJob(dom) < 0)
        goto cleanup;
    job = true;

1406 1407 1408
    if (vzEnsureDomainExists(dom) < 0)
        goto cleanup;

1409 1410
    state = virDomainObjGetState(dom, &reason);

1411 1412 1413
    if (state == VIR_DOMAIN_RUNNING && (flags & VIR_DOMAIN_SAVE_PAUSED) &&
        prlsdkPause(dom) < 0)
        goto cleanup;
1414

1415 1416 1417 1418 1419 1420 1421
    if (prlsdkSuspend(dom) < 0)
        goto cleanup;

    if (prlsdkUpdateDomain(privconn->driver, dom) < 0)
        goto cleanup;

    ret = 0;
1422 1423

 cleanup:
1424 1425 1426
    if (job)
        vzDomainObjEndJob(dom);
    virDomainObjEndAPI(&dom);
1427 1428 1429 1430
    return ret;
}

static int
1431
vzDomainManagedSaveRemove(virDomainPtr domain, unsigned int flags)
1432 1433 1434 1435 1436 1437 1438
{
    virDomainObjPtr dom = NULL;
    int state, reason;
    int ret = -1;

    virCheckFlags(0, -1);

J
John Ferlan 已提交
1439
    if (!(dom = vzDomObjFromDomain(domain)))
1440 1441
        return -1;

1442 1443 1444
    if (virDomainManagedSaveRemoveEnsureACL(domain->conn, dom->def) < 0)
        goto cleanup;

1445 1446 1447 1448 1449
    state = virDomainObjGetState(dom, &reason);

    if (!(state == VIR_DOMAIN_SHUTOFF && reason == VIR_DOMAIN_SHUTOFF_SAVED))
        goto cleanup;

1450
    ret = prlsdkDomainManagedSaveRemove(dom);
1451 1452

 cleanup:
1453
    virDomainObjEndAPI(&dom);
1454
    return ret;
1455 1456
}

1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478
static int vzCheckConfigUpdateFlags(virDomainObjPtr dom, unsigned int *flags)
{
    if (virDomainObjUpdateModificationImpact(dom, flags) < 0)
        return -1;

    if (!(*flags & VIR_DOMAIN_AFFECT_CONFIG)) {
        virReportError(VIR_ERR_OPERATION_INVALID, "%s",
                       _("domain config update needs VIR_DOMAIN_AFFECT_CONFIG "
                         "flag to be set"));
        return -1;
    }

    if (virDomainObjIsActive(dom) && !(*flags & VIR_DOMAIN_AFFECT_LIVE)) {
        virReportError(VIR_ERR_OPERATION_INVALID, "%s",
                       _("Updates on a running domain need "
                         "VIR_DOMAIN_AFFECT_LIVE flag"));
        return -1;
    }

    return 0;
}

1479
static int vzDomainAttachDeviceFlags(virDomainPtr domain, const char *xml,
M
Michal Privoznik 已提交
1480
                                     unsigned int flags)
1481 1482
{
    int ret = -1;
1483
    vzConnPtr privconn = domain->conn->privateData;
1484
    virDomainDeviceDefPtr dev = NULL;
1485
    virDomainObjPtr dom = NULL;
1486
    vzDriverPtr driver = privconn->driver;
1487
    bool job = false;
1488 1489 1490 1491

    virCheckFlags(VIR_DOMAIN_AFFECT_LIVE |
                  VIR_DOMAIN_AFFECT_CONFIG, -1);

J
John Ferlan 已提交
1492
    if (!(dom = vzDomObjFromDomain(domain)))
1493
        return -1;
1494

1495
    if (vzCheckConfigUpdateFlags(dom, &flags) < 0)
1496 1497
        goto cleanup;

1498 1499 1500
    if (virDomainAttachDeviceFlagsEnsureACL(domain->conn, dom->def, flags) < 0)
        goto cleanup;

1501 1502
    dev = virDomainDeviceDefParse(xml, dom->def, driver->caps,
                                  driver->xmlopt, VIR_DOMAIN_XML_INACTIVE);
1503 1504 1505
    if (dev == NULL)
        goto cleanup;

1506 1507 1508 1509
    if (vzDomainObjBeginJob(dom) < 0)
        goto cleanup;
    job = true;

1510 1511 1512
    if (vzEnsureDomainExists(dom) < 0)
        goto cleanup;

1513 1514
    if (prlsdkAttachDevice(driver, dom, dev) < 0)
        goto cleanup;
1515

1516 1517 1518
    if (prlsdkUpdateDomain(driver, dom) < 0)
        goto cleanup;

1519 1520
    ret = 0;
 cleanup:
1521
    virDomainDeviceDefFree(dev);
1522 1523 1524
    if (job)
        vzDomainObjEndJob(dom);
    virDomainObjEndAPI(&dom);
1525 1526 1527
    return ret;
}

1528
static int vzDomainAttachDevice(virDomainPtr domain, const char *xml)
1529
{
1530
    return vzDomainAttachDeviceFlags(domain, xml,
M
Michal Privoznik 已提交
1531
                                     VIR_DOMAIN_AFFECT_CONFIG | VIR_DOMAIN_AFFECT_LIVE);
1532 1533
}

1534
static int vzDomainDetachDeviceFlags(virDomainPtr domain, const char *xml,
M
Michal Privoznik 已提交
1535
                                     unsigned int flags)
1536 1537
{
    int ret = -1;
1538
    vzConnPtr privconn = domain->conn->privateData;
1539
    virDomainDeviceDefPtr dev = NULL;
1540
    virDomainObjPtr dom = NULL;
1541
    vzDriverPtr driver = privconn->driver;
1542
    bool job = false;
1543 1544 1545 1546

    virCheckFlags(VIR_DOMAIN_AFFECT_LIVE |
                  VIR_DOMAIN_AFFECT_CONFIG, -1);

J
John Ferlan 已提交
1547
    dom = vzDomObjFromDomain(domain);
1548
    if (dom == NULL)
1549 1550
        return -1;

1551
    if (vzCheckConfigUpdateFlags(dom, &flags) < 0)
1552 1553
        goto cleanup;

1554 1555 1556
    if (virDomainDetachDeviceFlagsEnsureACL(domain->conn, dom->def, flags) < 0)
        goto cleanup;

1557 1558
    dev = virDomainDeviceDefParse(xml, dom->def, driver->caps,
                                  driver->xmlopt,
1559 1560
                                  VIR_DOMAIN_XML_INACTIVE |
                                  VIR_DOMAIN_DEF_PARSE_SKIP_VALIDATE);
1561 1562 1563
    if (dev == NULL)
        goto cleanup;

1564 1565 1566 1567
    if (vzDomainObjBeginJob(dom) < 0)
        goto cleanup;
    job = true;

1568 1569 1570
    if (vzEnsureDomainExists(dom) < 0)
        goto cleanup;

1571 1572
    if (prlsdkDetachDevice(driver, dom, dev) < 0)
        goto cleanup;
1573

1574 1575 1576
    if (prlsdkUpdateDomain(driver, dom) < 0)
        goto cleanup;

1577 1578
    ret = 0;
 cleanup:
1579
    virDomainDeviceDefFree(dev);
1580 1581 1582
    if (job)
        vzDomainObjEndJob(dom);
    virDomainObjEndAPI(&dom);
1583

1584 1585 1586
    return ret;
}

1587
static int vzDomainDetachDevice(virDomainPtr domain, const char *xml)
1588
{
1589
    return vzDomainDetachDeviceFlags(domain, xml,
M
Michal Privoznik 已提交
1590
                                     VIR_DOMAIN_AFFECT_CONFIG | VIR_DOMAIN_AFFECT_LIVE);
1591 1592
}

1593 1594 1595 1596 1597 1598 1599 1600
static int
vzDomainSetUserPassword(virDomainPtr domain,
                        const char *user,
                        const char *password,
                        unsigned int flags)
{
    virDomainObjPtr dom = NULL;
    int ret = -1;
1601
    bool job = false;
1602 1603

    virCheckFlags(0, -1);
J
John Ferlan 已提交
1604
    if (!(dom = vzDomObjFromDomain(domain)))
1605 1606
        return -1;

1607 1608 1609
    if (virDomainSetUserPasswordEnsureACL(domain->conn, dom->def) < 0)
        goto cleanup;

1610 1611 1612 1613
    if (vzDomainObjBeginJob(dom) < 0)
        goto cleanup;
    job = true;

1614 1615 1616
    if (vzEnsureDomainExists(dom) < 0)
        goto cleanup;

1617 1618
    ret = prlsdkDomainSetUserPassword(dom, user, password);

1619 1620 1621 1622
 cleanup:
    if (job)
        vzDomainObjEndJob(dom);
    virDomainObjEndAPI(&dom);
1623 1624 1625
    return ret;
}

1626
static int vzDomainUpdateDeviceFlags(virDomainPtr domain,
N
Nikolay Shirokovskiy 已提交
1627 1628 1629 1630
                                     const char *xml,
                                     unsigned int flags)
{
    int ret = -1;
1631 1632
    vzConnPtr privconn = domain->conn->privateData;
    virDomainObjPtr dom = NULL;
N
Nikolay Shirokovskiy 已提交
1633 1634
    virDomainDeviceDefPtr dev = NULL;
    vzDriverPtr driver = privconn->driver;
1635
    bool job = false;
N
Nikolay Shirokovskiy 已提交
1636 1637 1638 1639

    virCheckFlags(VIR_DOMAIN_AFFECT_LIVE |
                  VIR_DOMAIN_AFFECT_CONFIG, -1);

J
John Ferlan 已提交
1640
    if (!(dom = vzDomObjFromDomain(domain)))
N
Nikolay Shirokovskiy 已提交
1641 1642
        return -1;

1643 1644 1645
    if (virDomainUpdateDeviceFlagsEnsureACL(domain->conn, dom->def, flags) < 0)
        goto cleanup;

1646
    if (vzCheckConfigUpdateFlags(dom, &flags) < 0)
N
Nikolay Shirokovskiy 已提交
1647 1648
        goto cleanup;

1649
    if (!(dev = virDomainDeviceDefParse(xml, dom->def, driver->caps,
N
Nikolay Shirokovskiy 已提交
1650 1651 1652 1653
                                        driver->xmlopt,
                                        VIR_DOMAIN_XML_INACTIVE)))
        goto cleanup;

1654 1655 1656 1657
    if (vzDomainObjBeginJob(dom) < 0)
        goto cleanup;
    job = true;

1658 1659 1660
    if (vzEnsureDomainExists(dom) < 0)
        goto cleanup;

1661
    if (prlsdkUpdateDevice(driver, dom, dev) < 0)
N
Nikolay Shirokovskiy 已提交
1662 1663
        goto cleanup;

1664 1665 1666
    if (prlsdkUpdateDomain(driver, dom) < 0)
        goto cleanup;

N
Nikolay Shirokovskiy 已提交
1667 1668 1669 1670
    ret = 0;
 cleanup:

    virDomainDeviceDefFree(dev);
1671 1672 1673
    if (job)
        vzDomainObjEndJob(dom);
    virDomainObjEndAPI(&dom);
N
Nikolay Shirokovskiy 已提交
1674 1675 1676 1677
    return ret;
}


1678
static unsigned long long
1679
vzDomainGetMaxMemory(virDomainPtr domain)
1680 1681 1682 1683
{
    virDomainObjPtr dom = NULL;
    int ret = -1;

1684
    if (!(dom = vzDomObjFromDomain(domain)))
1685 1686
        return -1;

1687 1688 1689
    if (virDomainGetMaxMemoryEnsureACL(domain->conn, dom->def) < 0)
        goto cleanup;

1690
    ret = virDomainDefGetMemoryTotal(dom->def);
1691 1692

 cleanup:
J
John Ferlan 已提交
1693
    virDomainObjEndAPI(&dom);
1694 1695 1696
    return ret;
}

1697
static int
1698 1699 1700
vzDomainBlockStatsImpl(virDomainObjPtr dom,
                       const char *path,
                       virDomainBlockStatsPtr stats)
1701
{
1702
    vzDomObjPtr privdom = dom->privateData;
1703 1704 1705 1706 1707 1708
    size_t i;
    int idx;

    if (*path) {
        if ((idx = virDomainDiskIndexByName(dom->def, path, false)) < 0) {
            virReportError(VIR_ERR_INVALID_ARG, _("invalid path: %s"), path);
1709
            return -1;
1710
        }
M
Maxim Nestratov 已提交
1711 1712 1713 1714
        if (prlsdkGetBlockStats(privdom->stats,
                                dom->def->disks[idx],
                                stats,
                                IS_CT(dom->def)) < 0)
1715
            return -1;
1716 1717 1718
    } else {
        virDomainBlockStatsStruct s;

1719
#define PARALLELS_ZERO_STATS(VAR, TYPE, NAME) \
1720 1721 1722 1723 1724 1725 1726
        stats->VAR = 0;

        PARALLELS_BLOCK_STATS_FOREACH(PARALLELS_ZERO_STATS)

#undef PARALLELS_ZERO_STATS

        for (i = 0; i < dom->def->ndisks; i++) {
M
Maxim Nestratov 已提交
1727 1728 1729 1730
            if (prlsdkGetBlockStats(privdom->stats,
                                    dom->def->disks[i],
                                    &s,
                                    IS_CT(dom->def)) < 0)
1731
                return -1;
1732

1733 1734
#define PARALLELS_SUM_STATS(VAR, TYPE, NAME) \
    if (s.VAR != -1) \
M
Michal Privoznik 已提交
1735
        stats->VAR += s.VAR;
1736

M
Michal Privoznik 已提交
1737
        PARALLELS_BLOCK_STATS_FOREACH(PARALLELS_SUM_STATS)
1738

1739
#undef PARALLELS_SUM_STATS
1740 1741 1742
        }
    }
    stats->errs = -1;
1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753
    return 0;
}

static int
vzDomainBlockStats(virDomainPtr domain,
                   const char *path,
                   virDomainBlockStatsPtr stats)
{
    virDomainObjPtr dom;
    int ret = -1;

J
John Ferlan 已提交
1754
    if (!(dom = vzDomObjFromDomain(domain)))
1755 1756
        return -1;

1757 1758 1759
    if (virDomainBlockStatsEnsureACL(domain->conn, dom->def) < 0)
        goto cleanup;

1760 1761 1762
    if (vzDomainBlockStatsImpl(dom, path, stats) < 0)
        goto cleanup;

1763 1764 1765
    ret = 0;

 cleanup:
1766
    virDomainObjEndAPI(&dom);
1767 1768 1769 1770

    return ret;
}

1771 1772 1773 1774 1775 1776 1777 1778
static int
vzDomainBlockStatsToParams(virDomainBlockStatsPtr stats,
                           virTypedParameterPtr params,
                           int *nparams)
{
    size_t i;

    if (*nparams == 0) {
1779 1780
#define PARALLELS_COUNT_STATS(VAR, TYPE, NAME) \
        if ((stats->VAR) != -1) \
1781 1782 1783 1784 1785 1786 1787 1788 1789
            ++*nparams;

        PARALLELS_BLOCK_STATS_FOREACH(PARALLELS_COUNT_STATS)

#undef PARALLELS_COUNT_STATS
        return 0;
    }

    i = 0;
1790 1791 1792 1793 1794 1795
#define PARALLELS_BLOCK_STATS_ASSIGN_PARAM(VAR, TYPE, NAME) \
    if (i < *nparams && (stats->VAR) != -1) { \
        if (virTypedParameterAssign(params + i, TYPE, \
                                    VIR_TYPED_PARAM_LLONG, (stats->VAR)) < 0) \
            return -1; \
        i++; \
1796 1797 1798 1799 1800 1801 1802 1803 1804 1805
    }

    PARALLELS_BLOCK_STATS_FOREACH(PARALLELS_BLOCK_STATS_ASSIGN_PARAM)

#undef PARALLELS_BLOCK_STATS_ASSIGN_PARAM

    *nparams = i;
    return 0;
}

1806
static int
1807
vzDomainBlockStatsFlags(virDomainPtr domain,
M
Michal Privoznik 已提交
1808 1809 1810 1811
                        const char *path,
                        virTypedParameterPtr params,
                        int *nparams,
                        unsigned int flags)
1812 1813
{
    virDomainBlockStatsStruct stats;
1814
    virDomainObjPtr dom;
1815 1816 1817 1818 1819 1820
    int ret = -1;

    virCheckFlags(VIR_TYPED_PARAM_STRING_OKAY, -1);
    /* We don't return strings, and thus trivially support this flag.  */
    flags &= ~VIR_TYPED_PARAM_STRING_OKAY;

J
John Ferlan 已提交
1821
    if (!(dom = vzDomObjFromDomain(domain)))
1822 1823
        return -1;

1824 1825 1826
    if (virDomainBlockStatsFlagsEnsureACL(domain->conn, dom->def) < 0)
        goto cleanup;

1827
    if (vzDomainBlockStatsImpl(dom, path, &stats) < 0)
1828 1829
        goto cleanup;

1830
    if (vzDomainBlockStatsToParams(&stats, params, nparams) < 0)
1831 1832 1833 1834 1835
        goto cleanup;

    ret = 0;

 cleanup:
1836 1837
    virDomainObjEndAPI(&dom);

1838 1839 1840
    return ret;
}

1841 1842
static int
vzDomainInterfaceStats(virDomainPtr domain,
1843
                         const char *device,
1844 1845 1846
                         virDomainInterfaceStatsPtr stats)
{
    virDomainObjPtr dom = NULL;
1847
    vzDomObjPtr privdom;
1848
    int ret = -1;
1849

J
John Ferlan 已提交
1850
    if (!(dom = vzDomObjFromDomain(domain)))
1851 1852
        return -1;

1853 1854 1855
    if (virDomainInterfaceStatsEnsureACL(domain->conn, dom->def) < 0)
        goto cleanup;

1856 1857
    privdom = dom->privateData;

1858
    ret = prlsdkGetNetStats(privdom->stats, privdom->sdkdom, device, stats);
1859 1860

 cleanup:
1861 1862 1863 1864
    virDomainObjEndAPI(&dom);

    return ret;
}
1865

N
Nikolay Shirokovskiy 已提交
1866 1867 1868 1869 1870 1871 1872
static int
vzDomainMemoryStats(virDomainPtr domain,
                    virDomainMemoryStatPtr stats,
                    unsigned int nr_stats,
                    unsigned int flags)
{
    virDomainObjPtr dom = NULL;
1873
    vzDomObjPtr privdom;
N
Nikolay Shirokovskiy 已提交
1874 1875 1876
    int ret = -1;

    virCheckFlags(0, -1);
J
John Ferlan 已提交
1877
    if (!(dom = vzDomObjFromDomain(domain)))
N
Nikolay Shirokovskiy 已提交
1878 1879
        return -1;

1880 1881 1882
    if (virDomainMemoryStatsEnsureACL(domain->conn, dom->def) < 0)
        goto cleanup;

1883 1884 1885
    privdom = dom->privateData;

    ret = prlsdkGetMemoryStats(privdom->stats, stats, nr_stats);
1886 1887

 cleanup:
N
Nikolay Shirokovskiy 已提交
1888 1889 1890 1891 1892
    virDomainObjEndAPI(&dom);

    return ret;
}

1893
static int
1894
vzDomainGetVcpusFlags(virDomainPtr domain,
1895 1896
                      unsigned int flags)
{
1897
    virDomainObjPtr dom;
1898
    int ret = -1;
1899 1900 1901 1902 1903

    virCheckFlags(VIR_DOMAIN_AFFECT_LIVE |
                  VIR_DOMAIN_AFFECT_CONFIG |
                  VIR_DOMAIN_VCPU_MAXIMUM, -1);

1904
    if (!(dom = vzDomObjFromDomain(domain)))
1905
        return -1;
1906

1907 1908 1909
    if (virDomainGetVcpusFlagsEnsureACL(domain->conn, dom->def, flags) < 0)
        goto cleanup;

1910
    if (flags & VIR_DOMAIN_VCPU_MAXIMUM)
1911
        ret = virDomainDefGetVcpusMax(dom->def);
1912
    else
1913
        ret = virDomainDefGetVcpus(dom->def);
1914

1915
 cleanup:
J
John Ferlan 已提交
1916
    virDomainObjEndAPI(&dom);
1917 1918 1919 1920

    return ret;
}

1921
static int vzDomainGetMaxVcpus(virDomainPtr domain)
1922
{
1923 1924
    return vzDomainGetVcpusFlags(domain, (VIR_DOMAIN_AFFECT_LIVE |
                                          VIR_DOMAIN_VCPU_MAXIMUM));
1925 1926
}

1927
static int vzDomainIsUpdated(virDomainPtr domain)
1928
{
1929
    virDomainObjPtr dom;
1930
    int ret = -1;
1931 1932 1933

    /* As far as VZ domains are always updated (e.g. current==persistent),
     * we just check for domain existence */
1934
    if (!(dom = vzDomObjFromDomain(domain)))
1935
        return -1;
1936

1937 1938 1939 1940 1941 1942
    if (virDomainIsUpdatedEnsureACL(domain->conn, dom->def) < 0)
        goto cleanup;

    ret = 0;

 cleanup:
J
John Ferlan 已提交
1943
    virDomainObjEndAPI(&dom);
1944
    return ret;
1945 1946
}

1947
static int vzConnectGetMaxVcpus(virConnectPtr conn,
1948 1949
                                const char *type)
{
1950 1951 1952
    if (virConnectGetMaxVcpusEnsureACL(conn) < 0)
        return -1;

1953 1954 1955 1956 1957 1958 1959 1960 1961 1962
    /* As far as we have no limitation for containers
     * we report maximum */
    if (type == NULL || STRCASEEQ(type, "vz") || STRCASEEQ(type, "parallels"))
        return 1028;

    virReportError(VIR_ERR_INVALID_ARG,
                   _("unknown type '%s'"), type);
    return -1;
}

1963
static int
1964
vzNodeGetCPUStats(virConnectPtr conn,
1965 1966 1967 1968 1969
                  int cpuNum,
                  virNodeCPUStatsPtr params,
                  int *nparams,
                  unsigned int flags)
{
1970 1971 1972
    if (virNodeGetCPUStatsEnsureACL(conn) < 0)
        return -1;

1973
    return virHostCPUGetStats(cpuNum, params, nparams, flags);
1974 1975 1976
}

static int
1977
vzNodeGetMemoryStats(virConnectPtr conn,
1978 1979 1980 1981 1982
                     int cellNum,
                     virNodeMemoryStatsPtr params,
                     int *nparams,
                     unsigned int flags)
{
1983 1984 1985
    if (virNodeGetMemoryStatsEnsureACL(conn) < 0)
        return -1;

1986
    return virHostMemGetStats(cellNum, params, nparams, flags);
1987 1988 1989
}

static int
1990
vzNodeGetCellsFreeMemory(virConnectPtr conn,
1991 1992 1993 1994
                         unsigned long long *freeMems,
                         int startCell,
                         int maxCells)
{
1995 1996 1997
    if (virNodeGetCellsFreeMemoryEnsureACL(conn) < 0)
        return -1;

1998
    return virHostMemGetCellsFree(freeMems, startCell, maxCells);
1999 2000 2001
}

static unsigned long long
2002
vzNodeGetFreeMemory(virConnectPtr conn)
2003 2004
{
    unsigned long long freeMem;
2005 2006 2007 2008

    if (virNodeGetFreeMemoryEnsureACL(conn) < 0)
        return -1;

2009
    if (virHostMemGetInfo(NULL, &freeMem) < 0)
2010 2011 2012 2013
        return 0;
    return freeMem;
}

2014 2015 2016 2017 2018 2019 2020 2021 2022
static int
vzConnectRegisterCloseCallback(virConnectPtr conn,
                               virConnectCloseFunc cb,
                               void *opaque,
                               virFreeCallback freecb)
{
    vzConnPtr privconn = conn->privateData;
    int ret = -1;

2023 2024 2025
    if (virConnectRegisterCloseCallbackEnsureACL(conn) < 0)
        return -1;

2026 2027
    virObjectLock(privconn->driver);

2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038
    if (virConnectCloseCallbackDataGetCallback(privconn->closeCallback) != NULL) {
        virReportError(VIR_ERR_OPERATION_INVALID, "%s",
                       _("A close callback is already registered"));
        goto cleanup;
    }

    virConnectCloseCallbackDataRegister(privconn->closeCallback, conn, cb,
                                        opaque, freecb);
    ret = 0;

 cleanup:
2039
    virObjectUnlock(privconn->driver);
2040 2041 2042 2043 2044 2045 2046 2047 2048 2049

    return ret;
}

static int
vzConnectUnregisterCloseCallback(virConnectPtr conn, virConnectCloseFunc cb)
{
    vzConnPtr privconn = conn->privateData;
    int ret = -1;

2050 2051 2052
    if (virConnectUnregisterCloseCallbackEnsureACL(conn) < 0)
        return -1;

2053
    virObjectLock(privconn->driver);
2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064

    if (virConnectCloseCallbackDataGetCallback(privconn->closeCallback) != cb) {
        virReportError(VIR_ERR_OPERATION_INVALID, "%s",
                       _("A different callback was requested"));
        goto cleanup;
    }

    virConnectCloseCallbackDataUnregister(privconn->closeCallback, cb);
    ret = 0;

 cleanup:
2065
    virObjectUnlock(privconn->driver);
2066 2067 2068 2069

    return ret;
}

2070 2071
static int vzDomainSetMemoryFlags(virDomainPtr domain, unsigned long memory,
                                  unsigned int flags)
2072 2073 2074
{
    virDomainObjPtr dom = NULL;
    int ret = -1;
2075
    bool job = false;
2076 2077 2078 2079

    virCheckFlags(VIR_DOMAIN_AFFECT_LIVE |
                  VIR_DOMAIN_AFFECT_CONFIG, -1);

J
John Ferlan 已提交
2080
    if (!(dom = vzDomObjFromDomain(domain)))
2081 2082
        return -1;

2083
    if (vzCheckConfigUpdateFlags(dom, &flags) < 0)
2084 2085
        goto cleanup;

2086 2087 2088
    if (virDomainSetMemoryFlagsEnsureACL(domain->conn, dom->def, flags) < 0)
        goto cleanup;

2089 2090 2091 2092
    if (vzDomainObjBeginJob(dom) < 0)
        goto cleanup;
    job = true;

2093 2094 2095
    if (vzEnsureDomainExists(dom) < 0)
        goto cleanup;

2096 2097 2098 2099
    ret = prlsdkSetMemsize(dom, memory >> 10);

 cleanup:

2100 2101 2102
    if (job)
        vzDomainObjEndJob(dom);
    virDomainObjEndAPI(&dom);
2103 2104 2105 2106 2107
    return ret;
}

static int vzDomainSetMemory(virDomainPtr domain, unsigned long memory)
{
2108 2109 2110 2111
    virDomainObjPtr dom = NULL;
    int ret = -1;
    bool job = false;

J
John Ferlan 已提交
2112
    if (!(dom = vzDomObjFromDomain(domain)))
2113 2114
        return -1;

2115 2116 2117
    if (virDomainSetMemoryEnsureACL(domain->conn, dom->def) < 0)
        goto cleanup;

2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132
    if (vzDomainObjBeginJob(dom) < 0)
        goto cleanup;
    job = true;

    if (vzEnsureDomainExists(dom) < 0)
        goto cleanup;

    ret = prlsdkSetMemsize(dom, memory >> 10);

 cleanup:

    if (job)
        vzDomainObjEndJob(dom);
    virDomainObjEndAPI(&dom);
    return ret;
2133 2134
}

2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186
static virDomainSnapshotObjPtr
vzSnapObjFromName(virDomainSnapshotObjListPtr snapshots, const char *name)
{
    virDomainSnapshotObjPtr snap = NULL;
    snap = virDomainSnapshotFindByName(snapshots, name);
    if (!snap)
        virReportError(VIR_ERR_NO_DOMAIN_SNAPSHOT,
                       _("no domain snapshot with matching name '%s'"), name);

    return snap;
}

static virDomainSnapshotObjPtr
vzSnapObjFromSnapshot(virDomainSnapshotObjListPtr snapshots,
                      virDomainSnapshotPtr snapshot)
{
    return vzSnapObjFromName(snapshots, snapshot->name);
}

static int
vzCurrentSnapshotIterator(void *payload,
                              const void *name ATTRIBUTE_UNUSED,
                              void *data)
{
    virDomainSnapshotObjPtr snapshot = payload;
    virDomainSnapshotObjPtr *current = data;

    if (snapshot->def->current)
        *current = snapshot;

    return 0;
}

static virDomainSnapshotObjPtr
vzFindCurrentSnapshot(virDomainSnapshotObjListPtr snapshots)
{
    virDomainSnapshotObjPtr current = NULL;

    virDomainSnapshotForEach(snapshots, vzCurrentSnapshotIterator, &current);
    return current;
}

static int
vzDomainSnapshotNum(virDomainPtr domain, unsigned int flags)
{
    virDomainObjPtr dom;
    virDomainSnapshotObjListPtr snapshots = NULL;
    int n = -1;

    virCheckFlags(VIR_DOMAIN_SNAPSHOT_LIST_ROOTS |
                  VIR_DOMAIN_SNAPSHOT_FILTERS_ALL, -1);

J
John Ferlan 已提交
2187
    if (!(dom = vzDomObjFromDomain(domain)))
2188 2189
        return -1;

2190 2191 2192
    if (virDomainSnapshotNumEnsureACL(domain->conn, dom->def) < 0)
        goto cleanup;

2193 2194 2195 2196 2197 2198 2199
    if (!(snapshots = prlsdkLoadSnapshots(dom)))
        goto cleanup;

    n = virDomainSnapshotObjListNum(snapshots, NULL, flags);

 cleanup:
    virDomainSnapshotObjListFree(snapshots);
2200
    virDomainObjEndAPI(&dom);
2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217

    return n;
}

static int
vzDomainSnapshotListNames(virDomainPtr domain,
                          char **names,
                          int nameslen,
                          unsigned int flags)
{
    virDomainObjPtr dom;
    virDomainSnapshotObjListPtr snapshots = NULL;
    int n = -1;

    virCheckFlags(VIR_DOMAIN_SNAPSHOT_LIST_ROOTS |
                  VIR_DOMAIN_SNAPSHOT_FILTERS_ALL, -1);

J
John Ferlan 已提交
2218
    if (!(dom = vzDomObjFromDomain(domain)))
2219 2220
        return -1;

2221 2222 2223
    if (virDomainSnapshotListNamesEnsureACL(domain->conn, dom->def) < 0)
        goto cleanup;

2224 2225 2226 2227 2228 2229 2230
    if (!(snapshots = prlsdkLoadSnapshots(dom)))
        goto cleanup;

    n = virDomainSnapshotObjListGetNames(snapshots, NULL, names, nameslen, flags);

 cleanup:
    virDomainSnapshotObjListFree(snapshots);
2231
    virDomainObjEndAPI(&dom);
2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247

    return n;
}

static int
vzDomainListAllSnapshots(virDomainPtr domain,
                         virDomainSnapshotPtr **snaps,
                         unsigned int flags)
{
    virDomainObjPtr dom;
    virDomainSnapshotObjListPtr snapshots = NULL;
    int n = -1;

    virCheckFlags(VIR_DOMAIN_SNAPSHOT_LIST_ROOTS |
                  VIR_DOMAIN_SNAPSHOT_FILTERS_ALL, -1);

J
John Ferlan 已提交
2248
    if (!(dom = vzDomObjFromDomain(domain)))
2249 2250
        return -1;

2251 2252 2253
    if (virDomainListAllSnapshotsEnsureACL(domain->conn, dom->def) < 0)
        goto cleanup;

2254 2255 2256 2257 2258 2259 2260
    if (!(snapshots = prlsdkLoadSnapshots(dom)))
        goto cleanup;

    n = virDomainListSnapshots(snapshots, NULL, domain, snaps, flags);

 cleanup:
    virDomainSnapshotObjListFree(snapshots);
2261
    virDomainObjEndAPI(&dom);
2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277

    return n;
}

static char *
vzDomainSnapshotGetXMLDesc(virDomainSnapshotPtr snapshot, unsigned int flags)
{
    virDomainObjPtr dom;
    char *xml = NULL;
    virDomainSnapshotObjPtr snap;
    char uuidstr[VIR_UUID_STRING_BUFLEN];
    virDomainSnapshotObjListPtr snapshots = NULL;
    vzConnPtr privconn = snapshot->domain->conn->privateData;

    virCheckFlags(VIR_DOMAIN_XML_SECURE, NULL);

J
John Ferlan 已提交
2278
    if (!(dom = vzDomObjFromDomain(snapshot->domain)))
2279 2280
        return NULL;

2281 2282 2283
    if (virDomainSnapshotGetXMLDescEnsureACL(snapshot->domain->conn, dom->def, flags) < 0)
        goto cleanup;

2284 2285 2286 2287 2288 2289 2290 2291 2292
    if (!(snapshots = prlsdkLoadSnapshots(dom)))
        goto cleanup;

    if (!(snap = vzSnapObjFromSnapshot(snapshots, snapshot)))
        goto cleanup;

    virUUIDFormat(snapshot->domain->uuid, uuidstr);

    xml = virDomainSnapshotDefFormat(uuidstr, snap->def, privconn->driver->caps,
2293
                                     privconn->driver->xmlopt,
2294 2295 2296 2297 2298
                                     virDomainDefFormatConvertXMLFlags(flags),
                                     0);

 cleanup:
    virDomainSnapshotObjListFree(snapshots);
2299
    virDomainObjEndAPI(&dom);
2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314

    return xml;
}

static int
vzDomainSnapshotNumChildren(virDomainSnapshotPtr snapshot, unsigned int flags)
{
    virDomainObjPtr dom;
    virDomainSnapshotObjPtr snap;
    virDomainSnapshotObjListPtr snapshots = NULL;
    int n = -1;

    virCheckFlags(VIR_DOMAIN_SNAPSHOT_LIST_DESCENDANTS |
                  VIR_DOMAIN_SNAPSHOT_FILTERS_ALL, -1);

J
John Ferlan 已提交
2315
    if (!(dom = vzDomObjFromDomain(snapshot->domain)))
2316 2317
        return -1;

2318 2319 2320
    if (virDomainSnapshotNumChildrenEnsureACL(snapshot->domain->conn, dom->def) < 0)
        goto cleanup;

2321 2322 2323 2324 2325 2326 2327 2328 2329 2330
    if (!(snapshots = prlsdkLoadSnapshots(dom)))
        goto cleanup;

    if (!(snap = vzSnapObjFromSnapshot(snapshots, snapshot)))
        goto cleanup;

    n = virDomainSnapshotObjListNum(snapshots, snap, flags);

 cleanup:
    virDomainSnapshotObjListFree(snapshots);
2331
    virDomainObjEndAPI(&dom);
2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349

    return n;
}

static int
vzDomainSnapshotListChildrenNames(virDomainSnapshotPtr snapshot,
                                  char **names,
                                  int nameslen,
                                  unsigned int flags)
{
    virDomainObjPtr dom;
    virDomainSnapshotObjPtr snap;
    virDomainSnapshotObjListPtr snapshots = NULL;
    int n = -1;

    virCheckFlags(VIR_DOMAIN_SNAPSHOT_LIST_DESCENDANTS |
                  VIR_DOMAIN_SNAPSHOT_FILTERS_ALL, -1);

J
John Ferlan 已提交
2350
    if (!(dom = vzDomObjFromDomain(snapshot->domain)))
2351 2352
        return -1;

2353 2354 2355
    if (virDomainSnapshotListChildrenNamesEnsureACL(snapshot->domain->conn, dom->def) < 0)
        goto cleanup;

2356 2357 2358 2359 2360 2361 2362 2363 2364 2365
    if (!(snapshots = prlsdkLoadSnapshots(dom)))
        goto cleanup;

    if (!(snap = vzSnapObjFromSnapshot(snapshots, snapshot)))
        goto cleanup;

    n = virDomainSnapshotObjListGetNames(snapshots, snap, names, nameslen, flags);

 cleanup:
    virDomainSnapshotObjListFree(snapshots);
2366
    virDomainObjEndAPI(&dom);
2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383

    return n;
}

static int
vzDomainSnapshotListAllChildren(virDomainSnapshotPtr snapshot,
                                virDomainSnapshotPtr **snaps,
                                unsigned int flags)
{
    virDomainObjPtr dom;
    virDomainSnapshotObjPtr snap;
    virDomainSnapshotObjListPtr snapshots = NULL;
    int n = -1;

    virCheckFlags(VIR_DOMAIN_SNAPSHOT_LIST_DESCENDANTS |
                  VIR_DOMAIN_SNAPSHOT_FILTERS_ALL, -1);

J
John Ferlan 已提交
2384
    if (!(dom = vzDomObjFromDomain(snapshot->domain)))
2385 2386
        return -1;

2387 2388 2389
    if (virDomainSnapshotListAllChildrenEnsureACL(snapshot->domain->conn, dom->def) < 0)
        goto cleanup;

2390 2391 2392 2393 2394 2395 2396 2397 2398 2399
    if (!(snapshots = prlsdkLoadSnapshots(dom)))
        goto cleanup;

    if (!(snap = vzSnapObjFromSnapshot(snapshots, snapshot)))
        goto cleanup;

    n = virDomainListSnapshots(snapshots, snap, snapshot->domain, snaps, flags);

 cleanup:
    virDomainSnapshotObjListFree(snapshots);
2400
    virDomainObjEndAPI(&dom);
2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416

    return n;
}

static virDomainSnapshotPtr
vzDomainSnapshotLookupByName(virDomainPtr domain,
                             const char *name,
                             unsigned int flags)
{
    virDomainObjPtr dom;
    virDomainSnapshotObjPtr snap;
    virDomainSnapshotPtr snapshot = NULL;
    virDomainSnapshotObjListPtr snapshots = NULL;

    virCheckFlags(0, NULL);

J
John Ferlan 已提交
2417
    if (!(dom = vzDomObjFromDomain(domain)))
2418 2419
        return NULL;

2420 2421 2422
    if (virDomainSnapshotLookupByNameEnsureACL(domain->conn, dom->def) < 0)
        goto cleanup;

2423 2424 2425 2426 2427 2428 2429 2430 2431
    if (!(snapshots = prlsdkLoadSnapshots(dom)))
        goto cleanup;

    if (!(snap = vzSnapObjFromName(snapshots, name)))
        goto cleanup;

    snapshot = virGetDomainSnapshot(domain, snap->def->name);

 cleanup:
2432
    virDomainObjEndAPI(&dom);
2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446
    virDomainSnapshotObjListFree(snapshots);

    return snapshot;
}

static int
vzDomainHasCurrentSnapshot(virDomainPtr domain, unsigned int flags)
{
    virDomainObjPtr dom;
    virDomainSnapshotObjListPtr snapshots = NULL;
    int ret = -1;

    virCheckFlags(0, -1);

J
John Ferlan 已提交
2447
    if (!(dom = vzDomObjFromDomain(domain)))
2448 2449
        return -1;

2450 2451 2452
    if (virDomainHasCurrentSnapshotEnsureACL(domain->conn, dom->def) < 0)
        goto cleanup;

2453 2454 2455 2456 2457 2458 2459
    if (!(snapshots = prlsdkLoadSnapshots(dom)))
        goto cleanup;

    ret = vzFindCurrentSnapshot(snapshots) != NULL;

 cleanup:
    virDomainSnapshotObjListFree(snapshots);
2460
    virDomainObjEndAPI(&dom);
2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474

    return ret;
}

static virDomainSnapshotPtr
vzDomainSnapshotGetParent(virDomainSnapshotPtr snapshot, unsigned int flags)
{
    virDomainObjPtr dom;
    virDomainSnapshotObjPtr snap;
    virDomainSnapshotPtr parent = NULL;
    virDomainSnapshotObjListPtr snapshots = NULL;

    virCheckFlags(0, NULL);

J
John Ferlan 已提交
2475
    if (!(dom = vzDomObjFromDomain(snapshot->domain)))
2476 2477
        return NULL;

2478 2479 2480
    if (virDomainSnapshotGetParentEnsureACL(snapshot->domain->conn, dom->def) < 0)
        goto cleanup;

2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497
    if (!(snapshots = prlsdkLoadSnapshots(dom)))
        goto cleanup;

    if (!(snap = vzSnapObjFromSnapshot(snapshots, snapshot)))
        goto cleanup;

    if (!snap->def->parent) {
        virReportError(VIR_ERR_NO_DOMAIN_SNAPSHOT,
                       _("snapshot '%s' does not have a parent"),
                       snap->def->name);
        goto cleanup;
    }

    parent = virGetDomainSnapshot(snapshot->domain, snap->def->parent);

 cleanup:
    virDomainSnapshotObjListFree(snapshots);
2498
    virDomainObjEndAPI(&dom);
2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509 2510 2511 2512

    return parent;
}

static virDomainSnapshotPtr
vzDomainSnapshotCurrent(virDomainPtr domain, unsigned int flags)
{
    virDomainObjPtr dom;
    virDomainSnapshotPtr snapshot = NULL;
    virDomainSnapshotObjListPtr snapshots = NULL;
    virDomainSnapshotObjPtr current;

    virCheckFlags(0, NULL);

J
John Ferlan 已提交
2513
    if (!(dom = vzDomObjFromDomain(domain)))
2514 2515
        return NULL;

2516 2517 2518
    if (virDomainSnapshotCurrentEnsureACL(domain->conn, dom->def) < 0)
        goto cleanup;

2519 2520 2521 2522 2523 2524 2525 2526 2527 2528 2529 2530 2531
    if (!(snapshots = prlsdkLoadSnapshots(dom)))
        goto cleanup;

    if (!(current = vzFindCurrentSnapshot(snapshots))) {
        virReportError(VIR_ERR_NO_DOMAIN_SNAPSHOT, "%s",
                       _("the domain does not have a current snapshot"));
        goto cleanup;
    }

    snapshot = virGetDomainSnapshot(domain, current->def->name);

 cleanup:
    virDomainSnapshotObjListFree(snapshots);
2532
    virDomainObjEndAPI(&dom);
2533 2534 2535 2536 2537 2538 2539 2540 2541 2542 2543 2544 2545 2546

    return snapshot;
}

static int
vzDomainSnapshotIsCurrent(virDomainSnapshotPtr snapshot, unsigned int flags)
{
    virDomainObjPtr dom;
    int ret = -1;
    virDomainSnapshotObjListPtr snapshots = NULL;
    virDomainSnapshotObjPtr current;

    virCheckFlags(0, -1);

J
John Ferlan 已提交
2547
    if (!(dom = vzDomObjFromDomain(snapshot->domain)))
2548 2549
        return -1;

2550 2551 2552
    if (virDomainSnapshotIsCurrentEnsureACL(snapshot->domain->conn, dom->def) < 0)
        goto cleanup;

2553 2554 2555 2556 2557 2558 2559 2560
    if (!(snapshots = prlsdkLoadSnapshots(dom)))
        goto cleanup;

    current = vzFindCurrentSnapshot(snapshots);
    ret = current && STREQ(snapshot->name, current->def->name);

 cleanup:
    virDomainSnapshotObjListFree(snapshots);
2561
    virDomainObjEndAPI(&dom);
2562 2563 2564 2565 2566 2567 2568 2569 2570 2571 2572 2573 2574 2575 2576

    return ret;
}

static int
vzDomainSnapshotHasMetadata(virDomainSnapshotPtr snapshot,
                              unsigned int flags)
{
    virDomainObjPtr dom;
    int ret = -1;
    virDomainSnapshotObjPtr snap;
    virDomainSnapshotObjListPtr snapshots = NULL;

    virCheckFlags(0, -1);

J
John Ferlan 已提交
2577
    if (!(dom = vzDomObjFromDomain(snapshot->domain)))
2578 2579
        return -1;

2580 2581 2582
    if (virDomainSnapshotHasMetadataEnsureACL(snapshot->domain->conn, dom->def) < 0)
        goto cleanup;

2583 2584 2585 2586 2587 2588 2589 2590 2591 2592
    if (!(snapshots = prlsdkLoadSnapshots(dom)))
        goto cleanup;

    if (!(snap = vzSnapObjFromSnapshot(snapshots, snapshot)))
        goto cleanup;

    ret = 1;

 cleanup:
    virDomainSnapshotObjListFree(snapshots);
2593 2594
    virDomainObjEndAPI(&dom);

2595 2596 2597 2598 2599 2600 2601 2602 2603 2604 2605 2606 2607 2608 2609 2610
    return ret;
}

static virDomainSnapshotPtr
vzDomainSnapshotCreateXML(virDomainPtr domain,
                          const char *xmlDesc,
                          unsigned int flags)
{
    virDomainSnapshotDefPtr def = NULL;
    virDomainSnapshotPtr snapshot = NULL;
    virDomainObjPtr dom;
    vzConnPtr privconn = domain->conn->privateData;
    vzDriverPtr driver = privconn->driver;
    unsigned int parse_flags = VIR_DOMAIN_SNAPSHOT_PARSE_DISKS;
    virDomainSnapshotObjListPtr snapshots = NULL;
    virDomainSnapshotObjPtr current;
2611
    bool job = false;
2612 2613 2614

    virCheckFlags(0, NULL);

J
John Ferlan 已提交
2615
    if (!(dom = vzDomObjFromDomain(domain)))
2616 2617
        return NULL;

2618 2619 2620
    if (virDomainSnapshotCreateXMLEnsureACL(domain->conn, dom->def, flags) < 0)
        goto cleanup;

2621 2622 2623 2624 2625 2626 2627 2628 2629 2630 2631 2632 2633 2634 2635 2636
    if (!(def = virDomainSnapshotDefParseString(xmlDesc, driver->caps,
                                                driver->xmlopt, parse_flags)))
        goto cleanup;

    if (def->ndisks > 0) {
        virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
                       _("configuring disks is not supported for vz snapshots"));
        goto cleanup;
    }

    if (def->memory) {
        virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
                       _("configuring memory location is not supported"));
        goto cleanup;
    }

2637 2638 2639 2640
    if (vzDomainObjBeginJob(dom) < 0)
        goto cleanup;
    job = true;

2641 2642 2643
    if (vzEnsureDomainExists(dom) < 0)
        goto cleanup;

2644 2645 2646 2647 2648 2649 2650 2651 2652 2653 2654 2655 2656 2657 2658 2659 2660 2661 2662
    /* snaphot name is ignored, it will be set to auto generated by sdk uuid */
    if (prlsdkCreateSnapshot(dom, def->description) < 0)
        goto cleanup;

    if (!(snapshots = prlsdkLoadSnapshots(dom)))
        goto cleanup;

    if (!(current = vzFindCurrentSnapshot(snapshots))) {
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                       _("can't find created snapshot"));
        goto cleanup;
    }

    /* hopefully new current snapshot is newly created one */
    snapshot = virGetDomainSnapshot(domain, current->def->name);

 cleanup:
    virDomainSnapshotObjListFree(snapshots);
    virDomainSnapshotDefFree(def);
2663 2664 2665
    if (job)
        vzDomainObjEndJob(dom);
    virDomainObjEndAPI(&dom);
2666 2667 2668 2669 2670 2671 2672 2673 2674 2675 2676 2677

    return snapshot;
}

static int
vzDomainSnapshotDelete(virDomainSnapshotPtr snapshot, unsigned int flags)
{
    virDomainObjPtr dom;
    int ret = -1;

    virCheckFlags(VIR_DOMAIN_SNAPSHOT_DELETE_CHILDREN, -1);

J
John Ferlan 已提交
2678
    if (!(dom = vzDomObjFromDomain(snapshot->domain)))
2679 2680
        return -1;

2681 2682 2683
    if (virDomainSnapshotDeleteEnsureACL(snapshot->domain->conn, dom->def) < 0)
        goto cleanup;

2684 2685 2686
    ret = prlsdkDeleteSnapshot(dom, snapshot->name,
                               flags & VIR_DOMAIN_SNAPSHOT_DELETE_CHILDREN);

2687
 cleanup:
2688
    virDomainObjEndAPI(&dom);
2689 2690 2691 2692 2693 2694 2695 2696 2697

    return ret;
}

static int
vzDomainRevertToSnapshot(virDomainSnapshotPtr snapshot, unsigned int flags)
{
    virDomainObjPtr dom;
    int ret = -1;
2698
    bool job = false;
2699 2700 2701

    virCheckFlags(VIR_DOMAIN_SNAPSHOT_REVERT_PAUSED, -1);

J
John Ferlan 已提交
2702
    if (!(dom = vzDomObjFromDomain(snapshot->domain)))
2703 2704
        return -1;

2705 2706 2707
    if (virDomainRevertToSnapshotEnsureACL(snapshot->domain->conn, dom->def) < 0)
        goto cleanup;

2708 2709 2710 2711
    if (vzDomainObjBeginJob(dom) < 0)
        goto cleanup;
    job = true;

2712 2713 2714
    if (vzEnsureDomainExists(dom) < 0)
        goto cleanup;

2715 2716
    ret = prlsdkSwitchToSnapshot(dom, snapshot->name,
                                 flags & VIR_DOMAIN_SNAPSHOT_REVERT_PAUSED);
2717 2718 2719 2720
 cleanup:
    if (job)
        vzDomainObjEndJob(dom);
    virDomainObjEndAPI(&dom);
2721 2722 2723 2724

    return ret;
}

2725 2726 2727 2728 2729 2730
enum vzMigrationCookieFeatures {
    VZ_MIGRATION_COOKIE_SESSION_UUID  = (1 << 0),
    VZ_MIGRATION_COOKIE_DOMAIN_UUID = (1 << 1),
    VZ_MIGRATION_COOKIE_DOMAIN_NAME = (1 << 1),
};

2731 2732 2733
typedef struct _vzMigrationCookie vzMigrationCookie;
typedef vzMigrationCookie *vzMigrationCookiePtr;
struct _vzMigrationCookie {
2734 2735
    unsigned char *session_uuid;
    unsigned char *uuid;
2736 2737 2738 2739 2740 2741 2742 2743
    char *name;
};

static void
vzMigrationCookieFree(vzMigrationCookiePtr mig)
{
    if (!mig)
        return;
2744 2745 2746

    VIR_FREE(mig->session_uuid);
    VIR_FREE(mig->uuid);
2747 2748 2749 2750 2751
    VIR_FREE(mig->name);
    VIR_FREE(mig);
}

static int
2752 2753 2754 2755
vzBakeCookie(vzDriverPtr driver,
             virDomainObjPtr dom,
             char **cookieout, int *cookieoutlen,
             unsigned int flags)
2756 2757 2758 2759 2760 2761 2762 2763 2764 2765 2766 2767 2768 2769 2770
{
    char uuidstr[VIR_UUID_STRING_BUFLEN];
    virBuffer buf = VIR_BUFFER_INITIALIZER;

    if (!cookieout || !cookieoutlen) {
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                       _("Migration cookie parameters are not provided."));
        return -1;
    }

    *cookieout = NULL;
    *cookieoutlen = 0;

    virBufferAddLit(&buf, "<vz-migration>\n");
    virBufferAdjustIndent(&buf, 2);
2771 2772 2773 2774 2775 2776 2777 2778 2779 2780 2781 2782 2783 2784 2785 2786 2787 2788 2789 2790 2791 2792

    if (flags & VZ_MIGRATION_COOKIE_SESSION_UUID) {
        virUUIDFormat(driver->session_uuid, uuidstr);
        virBufferAsprintf(&buf, "<session-uuid>%s</session-uuid>\n", uuidstr);
    }

    if (flags & VZ_MIGRATION_COOKIE_DOMAIN_UUID) {
        unsigned char fakeuuid[VIR_UUID_BUFLEN] = { 0 };

        /* if dom is NULL just pass some parsable uuid for backward compat.
         * It is not used by peer */
        virUUIDFormat(dom ? dom->def->uuid : fakeuuid, uuidstr);
        virBufferAsprintf(&buf, "<uuid>%s</uuid>\n", uuidstr);
    }

    if (flags & VZ_MIGRATION_COOKIE_DOMAIN_NAME) {
        /* if dom is NULL just pass some name for backward compat.
         * It is not used by peer */
        virBufferAsprintf(&buf, "<name>%s</name>\n", dom ? dom->def->name :
                                                           "__fakename__");
    }

2793 2794 2795 2796 2797 2798 2799 2800 2801 2802 2803 2804 2805
    virBufferAdjustIndent(&buf, -2);
    virBufferAddLit(&buf, "</vz-migration>\n");

    if (virBufferCheckError(&buf) < 0)
        return -1;

    *cookieout = virBufferContentAndReset(&buf);
    *cookieoutlen = strlen(*cookieout) + 1;

    return 0;
}

static vzMigrationCookiePtr
2806
vzEatCookie(const char *cookiein, int cookieinlen, unsigned int flags)
2807 2808 2809
{
    xmlDocPtr doc = NULL;
    xmlXPathContextPtr ctx = NULL;
2810
    char *tmp = NULL;
2811 2812 2813 2814 2815 2816 2817 2818 2819 2820 2821 2822 2823 2824 2825
    vzMigrationCookiePtr mig = NULL;

    if (VIR_ALLOC(mig) < 0)
        return NULL;

    if (!cookiein || cookieinlen <= 0 || cookiein[cookieinlen - 1] != '\0') {
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                       _("Invalid migration cookie"));
        goto error;
    }

    if (!(doc = virXMLParseStringCtxt(cookiein,
                                      _("(_migration_cookie)"), &ctx)))
        goto error;

2826 2827 2828 2829
    if ((flags & VZ_MIGRATION_COOKIE_SESSION_UUID)
        && (!(tmp = virXPathString("string(./session-uuid[1])", ctx))
            || (VIR_ALLOC_N(mig->session_uuid, VIR_UUID_BUFLEN) < 0)
            || (virUUIDParse(tmp, mig->session_uuid) < 0))) {
2830
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
2831 2832
                       _("missing or malformed session-uuid element "
                         "in migration data"));
2833 2834 2835 2836 2837
        VIR_FREE(tmp);
        goto error;
    }
    VIR_FREE(tmp);

2838 2839 2840 2841
    if ((flags & VZ_MIGRATION_COOKIE_DOMAIN_UUID)
        && (!(tmp = virXPathString("string(./uuid[1])", ctx))
            || (VIR_ALLOC_N(mig->uuid, VIR_UUID_BUFLEN) < 0)
            || (virUUIDParse(tmp, mig->uuid) < 0))) {
2842
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
2843
                       _("missing or malformed uuid element in migration data"));
2844 2845 2846 2847 2848
        VIR_FREE(tmp);
        goto error;
    }
    VIR_FREE(tmp);

2849 2850
    if ((flags & VZ_MIGRATION_COOKIE_DOMAIN_NAME)
        && !(mig->name = virXPathString("string(./name[1])", ctx))) {
2851 2852 2853 2854 2855 2856 2857 2858 2859 2860 2861 2862 2863 2864 2865 2866
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                       _("missing name element in migration data"));
        goto error;
    }

 cleanup:
    xmlXPathFreeContext(ctx);
    xmlFreeDoc(doc);
    return mig;

 error:
    vzMigrationCookieFree(mig);
    mig = NULL;
    goto cleanup;
}

2867 2868 2869
#define VZ_MIGRATION_FLAGS         (VIR_MIGRATE_PAUSED | \
                                    VIR_MIGRATE_PEER2PEER | \
                                    VIR_MIGRATE_LIVE | \
2870
                                    VIR_MIGRATE_UNDEFINE_SOURCE | \
2871
                                    VIR_MIGRATE_PERSIST_DEST | \
2872
                                    VIR_MIGRATE_NON_SHARED_INC)
2873

2874
#define VZ_MIGRATION_PARAMETERS \
2875 2876 2877
    VIR_MIGRATE_PARAM_DEST_XML,         VIR_TYPED_PARAM_STRING, \
    VIR_MIGRATE_PARAM_URI,              VIR_TYPED_PARAM_STRING, \
    VIR_MIGRATE_PARAM_DEST_NAME,        VIR_TYPED_PARAM_STRING, \
2878
    VIR_MIGRATE_PARAM_BANDWIDTH,        VIR_TYPED_PARAM_ULLONG, \
2879 2880
    NULL

2881 2882 2883 2884 2885 2886 2887 2888 2889 2890 2891 2892 2893 2894 2895 2896 2897 2898 2899 2900 2901 2902 2903 2904 2905 2906 2907 2908
static char *
vzDomainMigrateBeginStep(virDomainObjPtr dom,
                         vzDriverPtr driver,
                         virTypedParameterPtr params,
                         int nparams,
                         char **cookieout,
                         int *cookieoutlen)
{
    /* we can't do this check via VZ_MIGRATION_PARAMETERS as on preparation
     * step domain xml will be passed via this parameter and it is a common
     * style to use single allowed parameter list definition in all steps */
    if (virTypedParamsGet(params, nparams, VIR_MIGRATE_PARAM_DEST_XML)) {
        virReportError(VIR_ERR_OPERATION_UNSUPPORTED, "%s",
                       _("Changing destination XML is not supported"));
        return NULL;
    }

    /* session uuid, domain uuid and domain name are for backward compat */
    if (vzBakeCookie(driver, dom, cookieout, cookieoutlen,
                     VZ_MIGRATION_COOKIE_SESSION_UUID
                     | VZ_MIGRATION_COOKIE_DOMAIN_UUID
                     | VZ_MIGRATION_COOKIE_DOMAIN_NAME) < 0)
        return NULL;

    return virDomainDefFormat(dom->def, driver->caps,
                              VIR_DOMAIN_XML_MIGRATABLE);
}

2909 2910 2911 2912 2913 2914 2915 2916 2917 2918 2919
static char *
vzDomainMigrateBegin3Params(virDomainPtr domain,
                            virTypedParameterPtr params,
                            int nparams,
                            char **cookieout,
                            int *cookieoutlen,
                            unsigned int flags)
{
    char *xml = NULL;
    virDomainObjPtr dom = NULL;
    vzConnPtr privconn = domain->conn->privateData;
2920
    unsigned long long bandwidth = 0;
2921 2922 2923 2924 2925 2926

    virCheckFlags(VZ_MIGRATION_FLAGS, NULL);

    if (virTypedParamsValidate(params, nparams, VZ_MIGRATION_PARAMETERS) < 0)
        goto cleanup;

2927 2928 2929 2930 2931 2932 2933 2934 2935 2936
    if (virTypedParamsGetULLong(params, nparams, VIR_MIGRATE_PARAM_BANDWIDTH,
                                &bandwidth) < 0)
        goto cleanup;

    if (bandwidth > 0) {
        virReportError(VIR_ERR_OPERATION_UNSUPPORTED, "%s",
                       _("Bandwidth rate limiting is not supported"));
        goto cleanup;
    }

2937 2938 2939
    if (!(dom = vzDomObjFromDomain(domain)))
        goto cleanup;

2940 2941 2942
    if (virDomainMigrateBegin3ParamsEnsureACL(domain->conn, dom->def) < 0)
        goto cleanup;

2943 2944
    xml = vzDomainMigrateBeginStep(dom, privconn->driver, params, nparams,
                                   cookieout, cookieoutlen);
2945 2946 2947

 cleanup:

J
John Ferlan 已提交
2948
    virDomainObjEndAPI(&dom);
2949 2950 2951 2952 2953 2954 2955 2956 2957 2958 2959 2960 2961 2962 2963 2964 2965 2966 2967 2968 2969 2970 2971 2972 2973 2974 2975 2976 2977 2978 2979
    return xml;
}

static char*
vzMigrationCreateURI(void)
{
    char *hostname = NULL;
    char *uri = NULL;

    if (!(hostname = virGetHostname()))
        goto cleanup;

    if (STRPREFIX(hostname, "localhost")) {
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                       _("hostname on destination resolved to localhost,"
                         " but migration requires an FQDN"));
        goto cleanup;
    }

    if (virAsprintf(&uri, "vzmigr://%s", hostname) < 0)
        goto cleanup;

 cleanup:
    VIR_FREE(hostname);
    return uri;
}

static int
vzDomainMigratePrepare3Params(virConnectPtr conn,
                              virTypedParameterPtr params,
                              int nparams,
2980 2981
                              const char *cookiein ATTRIBUTE_UNUSED,
                              int cookieinlen ATTRIBUTE_UNUSED,
2982 2983 2984 2985 2986 2987
                              char **cookieout,
                              int *cookieoutlen,
                              char **uri_out,
                              unsigned int flags)
{
    vzConnPtr privconn = conn->privateData;
2988
    vzDriverPtr driver = privconn->driver;
2989 2990
    const char *miguri = NULL;
    const char *dname = NULL;
2991 2992
    const char *dom_xml = NULL;
    virDomainDefPtr def = NULL;
2993 2994 2995 2996 2997 2998 2999 3000 3001
    int ret = -1;

    virCheckFlags(VZ_MIGRATION_FLAGS, -1);

    if (virTypedParamsValidate(params, nparams, VZ_MIGRATION_PARAMETERS) < 0)
        goto cleanup;

    if (virTypedParamsGetString(params, nparams,
                                VIR_MIGRATE_PARAM_URI, &miguri) < 0 ||
3002 3003
        virTypedParamsGetString(params, nparams,
                                VIR_MIGRATE_PARAM_DEST_XML, &dom_xml) < 0 ||
3004 3005 3006 3007 3008 3009 3010 3011 3012
        virTypedParamsGetString(params, nparams,
                                VIR_MIGRATE_PARAM_DEST_NAME, &dname) < 0)
        goto cleanup;

    /* We must set uri_out if miguri is not set. This is direct
     * managed migration requirement */
    if (!miguri && !(*uri_out = vzMigrationCreateURI()))
        goto cleanup;

3013 3014 3015 3016 3017 3018
    /* domain uuid and domain name are for backward compat */
    if (vzBakeCookie(privconn->driver, NULL,
                     cookieout, cookieoutlen,
                     VZ_MIGRATION_COOKIE_SESSION_UUID
                     | VZ_MIGRATION_COOKIE_DOMAIN_UUID
                     | VZ_MIGRATION_COOKIE_DOMAIN_NAME) < 0)
3019 3020
        goto cleanup;

3021
    if (!(def = virDomainDefParseString(dom_xml, driver->caps, driver->xmlopt,
3022
                                        NULL,
3023 3024 3025 3026 3027 3028 3029 3030 3031 3032 3033 3034
                                        VIR_DOMAIN_DEF_PARSE_INACTIVE)))
        goto cleanup;

    if (dname) {
        VIR_FREE(def->name);
        if (VIR_STRDUP(def->name, dname) < 0)
            goto cleanup;
    }

    if (virDomainMigratePrepare3ParamsEnsureACL(conn, def) < 0)
        goto cleanup;

3035 3036 3037
    ret = 0;

 cleanup:
3038
    virDomainDefFree(def);
3039 3040 3041 3042 3043 3044
    return ret;
}

static int
vzConnectSupportsFeature(virConnectPtr conn ATTRIBUTE_UNUSED, int feature)
{
3045 3046 3047
    if (virConnectSupportsFeatureEnsureACL(conn) < 0)
        return -1;

3048
    switch ((virDrvFeature) feature) {
3049
    case VIR_DRV_FEATURE_MIGRATION_PARAMS:
3050
    case VIR_DRV_FEATURE_MIGRATION_P2P:
3051
        return 1;
3052 3053 3054 3055 3056 3057 3058 3059 3060 3061 3062 3063 3064
    case VIR_DRV_FEATURE_FD_PASSING:
    case VIR_DRV_FEATURE_MIGRATE_CHANGE_PROTECTION:
    case VIR_DRV_FEATURE_MIGRATION_DIRECT:
    case VIR_DRV_FEATURE_MIGRATION_OFFLINE:
    case VIR_DRV_FEATURE_MIGRATION_V1:
    case VIR_DRV_FEATURE_MIGRATION_V2:
    case VIR_DRV_FEATURE_MIGRATION_V3:
    case VIR_DRV_FEATURE_PROGRAM_KEEPALIVE:
    case VIR_DRV_FEATURE_REMOTE:
    case VIR_DRV_FEATURE_REMOTE_CLOSE_CALLBACK:
    case VIR_DRV_FEATURE_REMOTE_EVENT_CALLBACK:
    case VIR_DRV_FEATURE_TYPED_PARAM_STRING:
    case VIR_DRV_FEATURE_XML_MIGRATABLE:
3065 3066 3067 3068 3069 3070 3071 3072 3073 3074 3075 3076 3077 3078 3079 3080 3081 3082 3083 3084 3085 3086 3087 3088 3089 3090 3091 3092 3093 3094 3095 3096 3097 3098 3099 3100 3101 3102 3103 3104 3105 3106
    default:
        return 0;
    }
}

static virURIPtr
vzParseVzURI(const char *uri_str)
{
    virURIPtr uri = NULL;

    if (!(uri = virURIParse(uri_str)))
        goto error;

    if (!uri->scheme || !uri->server) {
        virReportError(VIR_ERR_INVALID_ARG,
                       _("scheme and host are mandatory vz migration URI: %s"),
                       uri_str);
        goto error;
    }

    if (uri->user || uri->path || uri->query || uri->fragment) {
        virReportError(VIR_ERR_INVALID_ARG,
                       _("only scheme, host and port are supported in "
                         "vz migration URI: %s"), uri_str);
        goto error;
    }

    if (STRNEQ(uri->scheme, "vzmigr")) {
        virReportError(VIR_ERR_ARGUMENT_UNSUPPORTED,
                       _("unsupported scheme %s in migration URI %s"),
                       uri->scheme, uri_str);
        goto error;
    }

    return uri;

 error:
    virURIFree(uri);
    return NULL;
}

static int
3107 3108
vzDomainMigratePerformStep(virDomainObjPtr dom,
                           vzDriverPtr driver,
3109 3110 3111 3112 3113
                           virTypedParameterPtr params,
                           int nparams,
                           const char *cookiein,
                           int cookieinlen,
                           unsigned int flags)
3114 3115
{
    int ret = -1;
3116
    vzDomObjPtr privdom = dom->privateData;
3117 3118 3119 3120
    virURIPtr vzuri = NULL;
    const char *miguri = NULL;
    const char *dname = NULL;
    vzMigrationCookiePtr mig = NULL;
3121
    bool job = false;
3122 3123 3124 3125 3126 3127 3128 3129 3130 3131 3132 3133 3134

    if (virTypedParamsGetString(params, nparams,
                                VIR_MIGRATE_PARAM_URI, &miguri) < 0 ||
        virTypedParamsGetString(params, nparams,
                                VIR_MIGRATE_PARAM_DEST_NAME, &dname) < 0)
        goto cleanup;

    if (!miguri) {
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                       _("migrate uri is not set"));
        goto cleanup;
    }

3135 3136
    if (!(mig = vzEatCookie(cookiein, cookieinlen,
                            VZ_MIGRATION_COOKIE_SESSION_UUID)))
3137 3138
        goto cleanup;

3139 3140 3141
    if (vzDomainObjBeginJob(dom) < 0)
        goto cleanup;
    job = true;
3142
    privdom->job.hasProgress = true;
3143

3144 3145 3146
    if (vzEnsureDomainExists(dom) < 0)
        goto cleanup;

3147 3148 3149 3150 3151 3152
    if (!(vzuri = vzParseVzURI(miguri)))
        goto cleanup;

    if (prlsdkMigrate(dom, vzuri, mig->session_uuid, dname, flags) < 0)
        goto cleanup;

3153
    virDomainObjListRemove(driver->domains, dom);
3154 3155 3156 3157

    ret = 0;

 cleanup:
3158 3159
    if (job)
        vzDomainObjEndJob(dom);
3160 3161 3162 3163 3164 3165
    virURIFree(vzuri);
    vzMigrationCookieFree(mig);

    return ret;
}

3166
static int
3167 3168
vzDomainMigratePerformP2P(virDomainObjPtr dom,
                          vzDriverPtr driver,
3169 3170 3171 3172 3173 3174 3175 3176 3177 3178 3179 3180 3181 3182 3183 3184 3185
                          const char *dconnuri,
                          virTypedParameterPtr orig_params,
                          int nparams,
                          unsigned int flags)
{
    virDomainPtr ddomain = NULL;
    char *uri = NULL;
    char *cookiein = NULL;
    char *cookieout = NULL;
    char *dom_xml = NULL;
    int cookieinlen = 0;
    int cookieoutlen = 0;
    virErrorPtr orig_err = NULL;
    int cancelled = 1;
    virConnectPtr dconn = NULL;
    virTypedParameterPtr params = NULL;
    int ret = -1;
P
Pavel Glushchak 已提交
3186
    int maxparams = nparams;
3187 3188 3189 3190 3191 3192 3193

    if (virTypedParamsCopy(&params, orig_params, nparams) < 0)
        return -1;

    if (!(dconn = virConnectOpen(dconnuri)))
        goto done;

3194 3195
    if (!(dom_xml = vzDomainMigrateBeginStep(dom, driver, params, nparams,
                                             &cookieout, &cookieoutlen)))
3196 3197
        goto done;

P
Pavel Glushchak 已提交
3198 3199 3200 3201
    if (virTypedParamsAddString(&params, &nparams, &maxparams,
                                VIR_MIGRATE_PARAM_DEST_XML, dom_xml) < 0)
        goto done;

3202
    VIR_STEAL_PTR(cookiein, cookieout);
3203 3204
    cookieinlen = cookieoutlen;
    cookieoutlen = 0;
3205 3206
    virObjectUnlock(dom);
    ret = dconn->driver->domainMigratePrepare3Params
3207
            (dconn, params, nparams, cookiein, cookieinlen,
3208 3209 3210
             &cookieout, &cookieoutlen, &uri, flags);
    virObjectLock(dom);
    if (ret < 0)
3211
        goto done;
3212
    ret = -1;
3213 3214 3215 3216 3217 3218 3219 3220 3221 3222 3223

    /* preparation step was successful, thus on any error we must perform
     * finish step to finalize migration on target
     */
    if (uri && virTypedParamsReplaceString(&params, &nparams,
                                           VIR_MIGRATE_PARAM_URI, uri) < 0) {
        orig_err = virSaveLastError();
        goto finish;
    }

    VIR_FREE(cookiein);
3224
    VIR_STEAL_PTR(cookiein, cookieout);
3225 3226
    cookieinlen = cookieoutlen;
    cookieoutlen = 0;
3227
    if (vzDomainMigratePerformStep(dom, driver, params, nparams, cookiein,
3228 3229 3230 3231 3232 3233 3234 3235 3236 3237 3238 3239
                                   cookieinlen, flags) < 0) {
        orig_err = virSaveLastError();
        goto finish;
    }

    cancelled = 0;

 finish:
    if (virTypedParamsGetString(params, nparams,
                                VIR_MIGRATE_PARAM_DEST_NAME, NULL) <= 0 &&
        virTypedParamsReplaceString(&params, &nparams,
                                    VIR_MIGRATE_PARAM_DEST_NAME,
3240
                                    dom->def->name) < 0)
3241 3242
        goto done;

3243
    virObjectUnlock(dom);
3244 3245 3246
    ddomain = dconn->driver->domainMigrateFinish3Params(dconn, params, nparams,
                                                        NULL, 0, NULL, NULL,
                                                        flags, cancelled);
3247
    virObjectLock(dom);
3248 3249 3250 3251 3252 3253 3254 3255 3256 3257 3258 3259 3260 3261 3262 3263 3264 3265 3266 3267 3268 3269 3270 3271 3272 3273 3274 3275 3276 3277 3278
    if (ddomain)
        ret = 0;
    virObjectUnref(ddomain);

    /* confirm step is NOOP thus no need to call it */

 done:
    if (orig_err) {
        virSetError(orig_err);
        virFreeError(orig_err);
    }
    VIR_FREE(dom_xml);
    VIR_FREE(uri);
    VIR_FREE(cookiein);
    VIR_FREE(cookieout);
    virTypedParamsFree(params, nparams);
    virObjectUnref(dconn);
    return ret;
}

static int
vzDomainMigratePerform3Params(virDomainPtr domain,
                              const char *dconnuri,
                              virTypedParameterPtr params,
                              int nparams,
                              const char *cookiein,
                              int cookieinlen,
                              char **cookieout ATTRIBUTE_UNUSED,
                              int *cookieoutlen ATTRIBUTE_UNUSED,
                              unsigned int flags)
{
3279
    int ret = -1;
3280 3281 3282
    virDomainObjPtr dom;
    vzConnPtr privconn = domain->conn->privateData;

3283 3284 3285 3286 3287
    virCheckFlags(VZ_MIGRATION_FLAGS, -1);

    if (virTypedParamsValidate(params, nparams, VZ_MIGRATION_PARAMETERS) < 0)
        return -1;

J
John Ferlan 已提交
3288
    if (!(dom = vzDomObjFromDomain(domain)))
3289 3290
        return -1;

3291 3292 3293
    if (virDomainMigratePerform3ParamsEnsureACL(domain->conn, dom->def) < 0)
        goto cleanup;

3294
    if (flags & VIR_MIGRATE_PEER2PEER)
3295 3296
        ret = vzDomainMigratePerformP2P(dom, privconn->driver, dconnuri,
                                        params, nparams, flags);
3297
    else
3298 3299
        ret = vzDomainMigratePerformStep(dom, privconn->driver, params, nparams,
                                         cookiein, cookieinlen, flags);
3300

3301
 cleanup:
3302 3303 3304
    virDomainObjEndAPI(&dom);

    return ret;
3305 3306
}

3307 3308 3309 3310 3311 3312 3313 3314 3315 3316 3317 3318 3319 3320 3321 3322 3323 3324 3325 3326
static virDomainPtr
vzDomainMigrateFinish3Params(virConnectPtr dconn,
                             virTypedParameterPtr params,
                             int nparams,
                             const char *cookiein ATTRIBUTE_UNUSED,
                             int cookieinlen ATTRIBUTE_UNUSED,
                             char **cookieout ATTRIBUTE_UNUSED,
                             int *cookieoutlen ATTRIBUTE_UNUSED,
                             unsigned int flags,
                             int cancelled)
{
    virDomainObjPtr dom = NULL;
    virDomainPtr domain = NULL;
    vzConnPtr privconn = dconn->privateData;
    vzDriverPtr driver = privconn->driver;
    const char *name = NULL;

    virCheckFlags(VZ_MIGRATION_FLAGS, NULL);

    if (virTypedParamsValidate(params, nparams, VZ_MIGRATION_PARAMETERS) < 0)
3327 3328 3329 3330
        return NULL;

    if (cancelled)
        return NULL;
3331 3332 3333

    if (virTypedParamsGetString(params, nparams,
                                VIR_MIGRATE_PARAM_DEST_NAME, &name) < 0)
3334
        return NULL;
3335 3336


3337
    if (!(dom = prlsdkAddDomainByName(driver, name)))
3338 3339
        goto cleanup;

3340 3341 3342 3343 3344
    /* At first glace at may look strange that we add domain and
     * then check ACL but we touch only cache and not real system state */
    if (virDomainMigrateFinish3ParamsEnsureACL(dconn, dom->def) < 0)
        goto cleanup;

3345
    domain = virGetDomain(dconn, dom->def->name, dom->def->uuid, dom->def->id);
3346 3347 3348 3349

 cleanup:
    /* In this situation we have to restore domain on source. But the migration
     * is already finished. */
3350
    if (!domain)
3351
        VIR_WARN("Can't provide domain '%s' after successful migration.", name);
3352
    virDomainObjEndAPI(&dom);
3353 3354 3355 3356 3357 3358 3359 3360 3361 3362 3363 3364 3365 3366 3367 3368 3369 3370 3371 3372
    return domain;
}

static int
vzDomainMigrateConfirm3Params(virDomainPtr domain ATTRIBUTE_UNUSED,
                              virTypedParameterPtr params,
                              int nparams,
                              const char *cookiein ATTRIBUTE_UNUSED,
                              int cookieinlen ATTRIBUTE_UNUSED,
                              unsigned int flags,
                              int cancelled ATTRIBUTE_UNUSED)
{
    virCheckFlags(VZ_MIGRATION_FLAGS, -1);

    if (virTypedParamsValidate(params, nparams, VZ_MIGRATION_PARAMETERS) < 0)
        return -1;

    return 0;
}

3373 3374 3375 3376 3377 3378 3379 3380 3381 3382 3383 3384 3385 3386 3387 3388 3389 3390 3391 3392 3393 3394 3395 3396 3397 3398 3399
static int
vzDomainGetJobInfoImpl(virDomainObjPtr dom, virDomainJobInfoPtr info)
{
    vzDomObjPtr privdom = dom->privateData;
    vzDomainJobObjPtr job = &privdom->job;

    memset(info, 0, sizeof(*info));

    if (!job->active || !job->hasProgress)
        return 0;

    if (vzDomainJobUpdateTime(job) < 0)
        return -1;

    info->type = VIR_DOMAIN_JOB_UNBOUNDED;
    info->dataTotal = 100;
    info->dataProcessed = job->progress;
    info->dataRemaining = 100 - job->progress;
    info->timeElapsed = job->elapsed;

    return 0;
}

static int
vzDomainGetJobInfo(virDomainPtr domain, virDomainJobInfoPtr info)
{
    virDomainObjPtr dom;
3400
    int ret = -1;
3401 3402 3403 3404

    if (!(dom = vzDomObjFromDomain(domain)))
        return -1;

3405 3406 3407
    if (virDomainGetJobInfoEnsureACL(domain->conn, dom->def) < 0)
        goto cleanup;

3408 3409
    ret = vzDomainGetJobInfoImpl(dom, info);

3410
 cleanup:
J
John Ferlan 已提交
3411
    virDomainObjEndAPI(&dom);
3412 3413 3414
    return ret;
}

3415 3416 3417 3418 3419 3420 3421 3422 3423 3424 3425 3426 3427 3428 3429 3430 3431 3432 3433 3434 3435 3436 3437 3438 3439 3440 3441 3442 3443 3444 3445 3446 3447 3448 3449 3450 3451 3452 3453 3454 3455 3456 3457 3458 3459 3460 3461 3462 3463 3464 3465
static int
vzDomainJobInfoToParams(virDomainJobInfoPtr info,
                        int *type,
                        virTypedParameterPtr *params,
                        int *nparams)
{
    virTypedParameterPtr par = NULL;
    int maxpar = 0;
    int npar = 0;

    if (virTypedParamsAddULLong(&par, &npar, &maxpar,
                                VIR_DOMAIN_JOB_TIME_ELAPSED,
                                info->timeElapsed) < 0 ||
        virTypedParamsAddULLong(&par, &npar, &maxpar,
                                VIR_DOMAIN_JOB_DATA_TOTAL,
                                info->dataTotal) < 0 ||
        virTypedParamsAddULLong(&par, &npar, &maxpar,
                                VIR_DOMAIN_JOB_DATA_PROCESSED,
                                info->dataProcessed) < 0 ||
        virTypedParamsAddULLong(&par, &npar, &maxpar,
                                VIR_DOMAIN_JOB_DATA_REMAINING,
                                info->dataRemaining) < 0)
        goto error;


    *type = info->type;
    *params = par;
    *nparams = npar;
    return 0;

 error:
    virTypedParamsFree(par, npar);
    return -1;
}

static int
vzDomainGetJobStats(virDomainPtr domain,
                    int *type,
                    virTypedParameterPtr *params,
                    int *nparams,
                    unsigned int flags)
{
    virDomainJobInfo info;
    virDomainObjPtr dom;
    int ret = -1;

    virCheckFlags(0, -1);

    if (!(dom = vzDomObjFromDomain(domain)))
        return -1;

3466 3467 3468
    if (virDomainGetJobStatsEnsureACL(domain->conn, dom->def) < 0)
        goto cleanup;

3469 3470 3471 3472 3473 3474 3475 3476 3477 3478 3479 3480 3481 3482
    if (vzDomainGetJobInfoImpl(dom, &info) < 0)
        goto cleanup;

    if (info.type == VIR_DOMAIN_JOB_NONE) {
        *type = VIR_DOMAIN_JOB_NONE;
        *params = NULL;
        *nparams = 0;
        ret = 0;
        goto cleanup;
    }

    ret = vzDomainJobInfoToParams(&info, type, params, nparams);

 cleanup:
J
John Ferlan 已提交
3483
    virDomainObjEndAPI(&dom);
3484 3485 3486 3487

    return ret;
}

3488 3489 3490 3491 3492 3493 3494 3495 3496 3497 3498 3499
#define VZ_ADD_STAT_PARAM_UUL(group, field, counter) \
do { \
    if (stat.field != -1) { \
        snprintf(param_name, VIR_TYPED_PARAM_FIELD_LENGTH, \
                 group ".%zu." counter, i); \
        if (virTypedParamsAddULLong(&record->params, \
                                    &record->nparams, \
                                    maxparams, \
                                    param_name, \
                                    stat.field) < 0) \
            return -1; \
    } \
3500 3501 3502 3503 3504 3505 3506 3507 3508 3509 3510 3511 3512 3513 3514 3515 3516 3517 3518 3519 3520 3521 3522 3523 3524 3525 3526 3527 3528 3529 3530 3531 3532 3533 3534 3535 3536 3537 3538 3539 3540 3541 3542 3543 3544 3545 3546 3547 3548 3549 3550 3551 3552 3553 3554 3555 3556 3557 3558 3559 3560 3561 3562 3563 3564 3565 3566 3567 3568
} while (0)

static int
vzDomainGetBlockStats(virDomainObjPtr dom,
                      virDomainStatsRecordPtr record,
                      int *maxparams)
{
    vzDomObjPtr privdom = dom->privateData;
    size_t i;
    char param_name[VIR_TYPED_PARAM_FIELD_LENGTH];

    if (virTypedParamsAddUInt(&record->params,
                              &record->nparams,
                              maxparams,
                              "block.count",
                              dom->def->ndisks) < 0)
        return -1;

    for (i = 0; i < dom->def->ndisks; i++) {
        virDomainBlockStatsStruct stat;
        virDomainDiskDefPtr disk = dom->def->disks[i];

        if (prlsdkGetBlockStats(privdom->stats,
                                disk,
                                &stat,
                                IS_CT(dom->def)) < 0)
            return -1;

        snprintf(param_name, VIR_TYPED_PARAM_FIELD_LENGTH,
                 "block.%zu.name", i);
        if (virTypedParamsAddString(&record->params,
                                    &record->nparams,
                                    maxparams,
                                    param_name,
                                    disk->dst) < 0)
            return -1;

        if (virStorageSourceIsLocalStorage(disk->src) && disk->src->path) {
            snprintf(param_name, VIR_TYPED_PARAM_FIELD_LENGTH,
                     "block.%zu.path", i);
            if (virTypedParamsAddString(&record->params,
                                        &record->nparams,
                                        maxparams,
                                        param_name,
                                        disk->src->path) < 0)
                return -1;
        }

        VZ_ADD_STAT_PARAM_UUL("block", rd_req, "rd.reqs");
        VZ_ADD_STAT_PARAM_UUL("block", rd_bytes, "rd.bytes");
        VZ_ADD_STAT_PARAM_UUL("block", wr_req, "wr.reqs");
        VZ_ADD_STAT_PARAM_UUL("block", wr_bytes, "wr.bytes");

        if (disk->device == VIR_DOMAIN_DISK_DEVICE_DISK) {
            snprintf(param_name, VIR_TYPED_PARAM_FIELD_LENGTH,
                     "block.%zu.capacity", i);
            if (virTypedParamsAddULLong(&record->params,
                                        &record->nparams,
                                        maxparams,
                                        param_name,
                                        disk->src->capacity) < 0)
                return -1;
        }

    }

    return 0;
}

3569 3570 3571 3572 3573 3574 3575 3576 3577 3578 3579 3580 3581 3582 3583 3584 3585 3586 3587 3588 3589 3590 3591 3592 3593 3594 3595 3596 3597 3598 3599 3600 3601 3602 3603 3604 3605 3606 3607 3608 3609
static int
vzDomainGetNetStats(virDomainObjPtr dom,
                    virDomainStatsRecordPtr record,
                    int *maxparams)
{
    vzDomObjPtr privdom = dom->privateData;
    size_t i;
    char param_name[VIR_TYPED_PARAM_FIELD_LENGTH];

    if (virTypedParamsAddUInt(&record->params,
                              &record->nparams,
                              maxparams,
                              "net.count",
                              dom->def->nnets) < 0)
        return -1;

    for (i = 0; i < dom->def->nnets; i++) {
        virDomainInterfaceStatsStruct stat;
        virDomainNetDefPtr net = dom->def->nets[i];

        if (prlsdkGetNetStats(privdom->stats, privdom->sdkdom, net->ifname,
                              &stat) < 0)
            return -1;

        snprintf(param_name, VIR_TYPED_PARAM_FIELD_LENGTH, "net.%zu.name", i);
        if (virTypedParamsAddString(&record->params,
                                    &record->nparams,
                                    maxparams,
                                    param_name,
                                    net->ifname) < 0)
            return -1;

        VZ_ADD_STAT_PARAM_UUL("net", rx_bytes, "rx.bytes");
        VZ_ADD_STAT_PARAM_UUL("net", rx_packets, "rx.pkts");
        VZ_ADD_STAT_PARAM_UUL("net", tx_bytes, "tx.bytes");
        VZ_ADD_STAT_PARAM_UUL("net", tx_packets, "tx.pkts");
    }

    return 0;
}

3610 3611 3612 3613 3614 3615 3616 3617 3618 3619 3620 3621 3622 3623 3624 3625 3626 3627 3628 3629 3630 3631 3632 3633 3634 3635 3636 3637 3638 3639 3640 3641 3642 3643 3644 3645 3646 3647 3648 3649 3650 3651 3652 3653 3654 3655 3656 3657 3658 3659 3660
static int
vzDomainGetVCPUStats(virDomainObjPtr dom,
                     virDomainStatsRecordPtr record,
                     int *maxparams)
{
    vzDomObjPtr privdom = dom->privateData;
    size_t i;
    char param_name[VIR_TYPED_PARAM_FIELD_LENGTH];

    if (virTypedParamsAddUInt(&record->params,
                              &record->nparams,
                              maxparams,
                              "vcpu.current",
                              virDomainDefGetVcpus(dom->def)) < 0)
        return -1;

    if (virTypedParamsAddUInt(&record->params,
                              &record->nparams,
                              maxparams,
                              "vcpu.maximum",
                              virDomainDefGetVcpusMax(dom->def)) < 0)
        return -1;

    for (i = 0; i < virDomainDefGetVcpusMax(dom->def); i++) {
        int state = dom->def->vcpus[i]->online ? VIR_VCPU_RUNNING :
                                                 VIR_VCPU_OFFLINE;
        unsigned long long time;

        snprintf(param_name, VIR_TYPED_PARAM_FIELD_LENGTH, "vcpu.%zu.state", i);
        if (virTypedParamsAddInt(&record->params,
                                 &record->nparams,
                                 maxparams,
                                 param_name,
                                 state) < 0)
            return -1;

        if (prlsdkGetVcpuStats(privdom->stats, i, &time) < 0)
            return -1;

        snprintf(param_name, VIR_TYPED_PARAM_FIELD_LENGTH, "vcpu.%zu.time", i);
        if (virTypedParamsAddULLong(&record->params,
                                    &record->nparams,
                                    maxparams,
                                    param_name,
                                    time) < 0)
            return -1;
    }

    return 0;
}

3661 3662 3663 3664 3665 3666 3667 3668 3669 3670 3671 3672 3673 3674 3675 3676 3677 3678 3679 3680 3681 3682 3683 3684 3685 3686 3687 3688
static int
vzDomainGetBalloonStats(virDomainObjPtr dom,
                        virDomainStatsRecordPtr record,
                        int *maxparams)
{
    vzDomObjPtr privdom = dom->privateData;
    virDomainMemoryStatStruct stats[VIR_DOMAIN_MEMORY_STAT_NR];
    size_t i;
    int n;

    if (virTypedParamsAddULLong(&record->params,
                                &record->nparams,
                                maxparams,
                                "balloon.maximum",
                                virDomainDefGetMemoryTotal(dom->def)) < 0)
        return -1;

    if (virTypedParamsAddULLong(&record->params,
                                &record->nparams,
                                maxparams,
                                "balloon.current",
                                virDomainDefGetMemoryTotal(dom->def)) < 0)
        return -1;

    n = prlsdkGetMemoryStats(privdom->stats, stats, VIR_DOMAIN_MEMORY_STAT_NR);
    if (n < 0)
        return -1;

3689 3690 3691 3692 3693 3694 3695
#define STORE_MEM_RECORD(TAG, NAME) \
    if (stats[i].tag == VIR_DOMAIN_MEMORY_STAT_ ##TAG) \
        if (virTypedParamsAddULLong(&record->params, \
                                    &record->nparams, \
                                    maxparams, \
                                    "balloon." NAME, \
                                    stats[i].val) < 0) \
3696 3697 3698 3699 3700 3701 3702 3703 3704 3705 3706 3707 3708 3709 3710 3711
            return -1;

    for (i = 0; i < n; i++) {
        STORE_MEM_RECORD(SWAP_IN, "swap_in")
        STORE_MEM_RECORD(SWAP_OUT, "swap_out")
        STORE_MEM_RECORD(MAJOR_FAULT, "major_fault")
        STORE_MEM_RECORD(MINOR_FAULT, "minor_fault")
        STORE_MEM_RECORD(AVAILABLE, "available")
        STORE_MEM_RECORD(UNUSED, "unused")
    }

#undef STORE_MEM_RECORD

    return 0;
}

3712 3713 3714 3715 3716 3717 3718 3719 3720 3721 3722 3723 3724 3725 3726 3727 3728 3729 3730 3731 3732 3733
static int
vzDomainGetStateStats(virDomainObjPtr dom,
                      virDomainStatsRecordPtr record,
                      int *maxparams)
{
    if (virTypedParamsAddInt(&record->params,
                             &record->nparams,
                             maxparams,
                             "state.state",
                             dom->state.state) < 0)
        return -1;

    if (virTypedParamsAddInt(&record->params,
                             &record->nparams,
                             maxparams,
                             "state.reason",
                             dom->state.reason) < 0)
        return -1;

    return 0;
}

3734 3735 3736 3737 3738 3739 3740 3741 3742 3743
static virDomainStatsRecordPtr
vzDomainGetAllStats(virConnectPtr conn,
                    virDomainObjPtr dom)
{
    virDomainStatsRecordPtr stat;
    int maxparams = 0;

    if (VIR_ALLOC(stat) < 0)
        return NULL;

3744 3745 3746
    if (vzDomainGetStateStats(dom, stat, &maxparams) < 0)
        goto error;

3747 3748
    if (vzDomainGetBlockStats(dom, stat, &maxparams) < 0)
        goto error;
3749 3750 3751

    if (vzDomainGetNetStats(dom, stat, &maxparams) < 0)
        goto error;
3752 3753 3754

    if (vzDomainGetVCPUStats(dom, stat, &maxparams) < 0)
        goto error;
3755 3756 3757

    if (vzDomainGetBalloonStats(dom, stat, &maxparams) < 0)
        goto error;
3758

3759
    if (!(stat->dom = virGetDomain(conn, dom->def->name, dom->def->uuid, dom->def->id)))
3760 3761 3762 3763 3764 3765 3766 3767 3768 3769 3770 3771 3772 3773 3774 3775 3776 3777 3778 3779 3780 3781 3782 3783 3784 3785 3786 3787 3788 3789 3790 3791 3792 3793 3794 3795 3796 3797 3798 3799 3800 3801 3802 3803 3804 3805 3806 3807 3808 3809 3810 3811 3812 3813 3814 3815 3816 3817 3818 3819 3820 3821 3822 3823 3824 3825 3826 3827 3828 3829 3830 3831 3832 3833 3834 3835 3836 3837 3838 3839 3840 3841 3842 3843 3844 3845 3846 3847 3848 3849 3850 3851 3852 3853 3854 3855
        goto error;

    return stat;

 error:
    virTypedParamsFree(stat->params, stat->nparams);
    virObjectUnref(stat->dom);
    VIR_FREE(stat);
    return NULL;
}

static int
vzConnectGetAllDomainStats(virConnectPtr conn,
                           virDomainPtr *domains,
                           unsigned int ndomains,
                           unsigned int stats,
                           virDomainStatsRecordPtr **retStats,
                           unsigned int flags)
{
    vzConnPtr privconn = conn->privateData;
    vzDriverPtr driver = privconn->driver;
    unsigned int lflags = flags & (VIR_CONNECT_LIST_DOMAINS_FILTERS_ACTIVE |
                                   VIR_CONNECT_LIST_DOMAINS_FILTERS_PERSISTENT |
                                   VIR_CONNECT_LIST_DOMAINS_FILTERS_STATE);
    unsigned int supported = VIR_DOMAIN_STATS_STATE |
                             VIR_DOMAIN_STATS_VCPU |
                             VIR_DOMAIN_STATS_INTERFACE |
                             VIR_DOMAIN_STATS_BALLOON |
                             VIR_DOMAIN_STATS_BLOCK;
    virDomainObjPtr *doms = NULL;
    size_t ndoms;
    virDomainStatsRecordPtr *tmpstats = NULL;
    int nstats = 0;
    int ret = -1;
    size_t i;

    virCheckFlags(VIR_CONNECT_LIST_DOMAINS_FILTERS_ACTIVE |
                  VIR_CONNECT_LIST_DOMAINS_FILTERS_PERSISTENT |
                  VIR_CONNECT_LIST_DOMAINS_FILTERS_STATE |
                  VIR_CONNECT_GET_ALL_DOMAINS_STATS_ENFORCE_STATS, -1);

    if (virConnectGetAllDomainStatsEnsureACL(conn) < 0)
        return -1;

    if (!stats) {
        stats = supported;
    } else if ((flags & VIR_CONNECT_GET_ALL_DOMAINS_STATS_ENFORCE_STATS) &&
               (stats & ~supported)) {
        virReportError(VIR_ERR_ARGUMENT_UNSUPPORTED,
                       _("Stats types bits 0x%x are not supported by this daemon"),
                       stats & ~supported);
        return -1;
    }

    if (ndomains) {
        if (virDomainObjListConvert(driver->domains, conn, domains, ndomains, &doms,
                                    &ndoms, virConnectGetAllDomainStatsCheckACL,
                                    lflags, true) < 0)
            return -1;
    } else {
        if (virDomainObjListCollect(driver->domains, conn, &doms, &ndoms,
                                    virConnectGetAllDomainStatsCheckACL,
                                    lflags) < 0)
            return -1;
    }

    if (VIR_ALLOC_N(tmpstats, ndoms + 1) < 0)
        goto cleanup;

    for (i = 0; i < ndoms; i++) {
        virDomainStatsRecordPtr tmp;
        virDomainObjPtr dom = doms[i];

        virObjectLock(dom);
        tmp = vzDomainGetAllStats(conn, dom);
        virObjectUnlock(dom);

        if (!tmp)
            goto cleanup;

        tmpstats[nstats++] = tmp;
    }

    *retStats = tmpstats;
    tmpstats = NULL;
    ret = nstats;

 cleanup:
    virDomainStatsRecordListFree(tmpstats);
    virObjectListFreeCount(doms, ndoms);

    return ret;
}

#undef VZ_ADD_STAT_PARAM_UUL

3856 3857 3858 3859 3860 3861
static int
vzDomainAbortJob(virDomainPtr domain)
{
    virDomainObjPtr dom;
    int ret = -1;

J
John Ferlan 已提交
3862
    if (!(dom = vzDomObjFromDomain(domain)))
3863 3864 3865 3866 3867 3868 3869 3870 3871 3872 3873 3874 3875
        return -1;

    if (virDomainAbortJobEnsureACL(domain->conn, dom->def) < 0)
        goto cleanup;

    ret = prlsdkCancelJob(dom);

 cleanup:
    virDomainObjEndAPI(&dom);

    return ret;
}

3876 3877 3878 3879 3880 3881 3882 3883 3884
static int
vzDomainReset(virDomainPtr domain, unsigned int flags)
{
    virDomainObjPtr dom = NULL;
    int ret = -1;
    bool job = false;

    virCheckFlags(0, -1);

J
John Ferlan 已提交
3885
    if (!(dom = vzDomObjFromDomain(domain)))
3886 3887 3888 3889 3890 3891 3892 3893 3894 3895 3896 3897 3898 3899 3900 3901 3902 3903 3904 3905 3906
        return -1;

    if (virDomainResetEnsureACL(domain->conn, dom->def) < 0)
        goto cleanup;

    if (vzDomainObjBeginJob(dom) < 0)
        goto cleanup;
    job = true;

    if (vzEnsureDomainExists(dom) < 0)
        goto cleanup;

    ret = prlsdkReset(dom);

 cleanup:
    if (job)
        vzDomainObjEndJob(dom);
    virDomainObjEndAPI(&dom);
    return ret;
}

3907 3908 3909 3910 3911 3912 3913 3914 3915 3916
static int vzDomainSetVcpusFlags(virDomainPtr domain, unsigned int nvcpus,
                                 unsigned int flags)
{
    virDomainObjPtr dom = NULL;
    int ret = -1;
    bool job = false;

    virCheckFlags(VIR_DOMAIN_AFFECT_LIVE |
                  VIR_DOMAIN_AFFECT_CONFIG, -1);

J
John Ferlan 已提交
3917
    if (!(dom = vzDomObjFromDomain(domain)))
3918 3919 3920 3921 3922 3923 3924 3925 3926 3927 3928 3929 3930 3931 3932 3933 3934 3935 3936 3937 3938 3939 3940 3941 3942 3943 3944 3945 3946
        goto cleanup;

    if (vzCheckConfigUpdateFlags(dom, &flags) < 0)
        goto cleanup;

    if (virDomainSetVcpusFlagsEnsureACL(domain->conn, dom->def, flags) < 0)
        goto cleanup;

    if (vzDomainObjBeginJob(dom) < 0)
        goto cleanup;
    job = true;

    if (vzEnsureDomainExists(dom) < 0)
        goto cleanup;

    ret = prlsdkSetCpuCount(dom, nvcpus);

 cleanup:
    if (job)
        vzDomainObjEndJob(dom);
    virDomainObjEndAPI(&dom);
    return ret;
}

static int vzDomainSetVcpus(virDomainPtr dom, unsigned int nvcpus)
{
    return vzDomainSetVcpusFlags(dom, nvcpus,
                                 VIR_DOMAIN_AFFECT_LIVE | VIR_DOMAIN_AFFECT_CONFIG);
}
3947 3948 3949 3950 3951 3952 3953 3954 3955 3956 3957 3958 3959
static int
vzDomainBlockResize(virDomainPtr domain,
                    const char *path,
                    unsigned long long size,
                    unsigned int flags)
{
    virDomainObjPtr dom = NULL;
    virDomainDiskDefPtr disk = NULL;
    int ret = -1;
    bool job = false;

    virCheckFlags(VIR_DOMAIN_BLOCK_RESIZE_BYTES, -1);

J
John Ferlan 已提交
3960
    if (!(dom = vzDomObjFromDomain(domain)))
3961 3962 3963 3964 3965 3966 3967 3968 3969 3970 3971 3972 3973 3974 3975 3976 3977 3978 3979 3980 3981 3982 3983
        goto cleanup;

    if (virDomainBlockResizeEnsureACL(domain->conn, dom->def) < 0)
        goto cleanup;

    if (path[0] == '\0') {
        virReportError(VIR_ERR_INVALID_ARG,
                       "%s", _("empty path"));
        goto cleanup;
    }

    /* sdk wants Mb */
    if (flags & VIR_DOMAIN_BLOCK_RESIZE_BYTES)
        size /= 1024;
    size /= 1024;

    if (vzDomainObjBeginJob(dom) < 0)
        goto cleanup;
    job = true;

    if (vzEnsureDomainExists(dom) < 0)
        goto cleanup;

3984
    if (virDomainObjCheckActive(dom) < 0)
3985 3986
        goto cleanup;

3987 3988 3989 3990 3991 3992
    if (!(disk = virDomainDiskByName(dom->def, path, false))) {
        virReportError(VIR_ERR_INVALID_ARG,
                       _("invalid path: %s"), path);
        goto cleanup;
    }

3993 3994 3995 3996 3997 3998 3999 4000
    ret = prlsdkResizeImage(dom, disk, size);

 cleanup:
    if (job)
        vzDomainObjEndJob(dom);
    virDomainObjEndAPI(&dom);
    return ret;
}
4001

4002
static virHypervisorDriver vzHypervisorDriver = {
4003
    .name = "vz",
4004 4005 4006 4007
    .connectOpen = vzConnectOpen,            /* 0.10.0 */
    .connectClose = vzConnectClose,          /* 0.10.0 */
    .connectGetVersion = vzConnectGetVersion,   /* 0.10.0 */
    .connectGetHostname = vzConnectGetHostname,      /* 0.10.0 */
4008
    .connectGetSysinfo = vzConnectGetSysinfo, /* 1.3.4 */
4009
    .connectGetMaxVcpus = vzConnectGetMaxVcpus, /* 1.2.21 */
4010
    .nodeGetInfo = vzNodeGetInfo,      /* 0.10.0 */
4011 4012 4013 4014
    .nodeGetCPUStats = vzNodeGetCPUStats,      /* 1.2.21 */
    .nodeGetMemoryStats = vzNodeGetMemoryStats, /* 1.2.21 */
    .nodeGetCellsFreeMemory = vzNodeGetCellsFreeMemory, /* 1.2.21 */
    .nodeGetFreeMemory = vzNodeGetFreeMemory, /* 1.2.21 */
4015 4016 4017 4018 4019 4020 4021 4022 4023 4024 4025 4026 4027 4028 4029 4030 4031 4032 4033 4034
    .connectGetCapabilities = vzConnectGetCapabilities,      /* 0.10.0 */
    .connectBaselineCPU = vzConnectBaselineCPU, /* 1.2.6 */
    .connectListDomains = vzConnectListDomains,      /* 0.10.0 */
    .connectNumOfDomains = vzConnectNumOfDomains,    /* 0.10.0 */
    .connectListDefinedDomains = vzConnectListDefinedDomains,        /* 0.10.0 */
    .connectNumOfDefinedDomains = vzConnectNumOfDefinedDomains,      /* 0.10.0 */
    .connectListAllDomains = vzConnectListAllDomains, /* 0.10.0 */
    .domainLookupByID = vzDomainLookupByID,    /* 0.10.0 */
    .domainLookupByUUID = vzDomainLookupByUUID,        /* 0.10.0 */
    .domainLookupByName = vzDomainLookupByName,        /* 0.10.0 */
    .domainGetOSType = vzDomainGetOSType,    /* 0.10.0 */
    .domainGetInfo = vzDomainGetInfo,  /* 0.10.0 */
    .domainGetState = vzDomainGetState,        /* 0.10.0 */
    .domainGetXMLDesc = vzDomainGetXMLDesc,    /* 0.10.0 */
    .domainIsPersistent = vzDomainIsPersistent,        /* 0.10.0 */
    .domainGetAutostart = vzDomainGetAutostart,        /* 0.10.0 */
    .domainGetVcpus = vzDomainGetVcpus, /* 1.2.6 */
    .domainSuspend = vzDomainSuspend,    /* 0.10.0 */
    .domainResume = vzDomainResume,    /* 0.10.0 */
    .domainDestroy = vzDomainDestroy,  /* 0.10.0 */
4035
    .domainDestroyFlags = vzDomainDestroyFlags,  /* 2.2.0 */
4036
    .domainShutdown = vzDomainShutdown, /* 0.10.0 */
4037
    .domainShutdownFlags = vzDomainShutdownFlags, /* 2.2.0 */
4038 4039
    .domainCreate = vzDomainCreate,    /* 0.10.0 */
    .domainCreateWithFlags = vzDomainCreateWithFlags, /* 1.2.10 */
P
Pavel Hrdina 已提交
4040
    .domainReboot = vzDomainReboot, /* 1.3.0 */
4041 4042 4043 4044 4045 4046 4047 4048 4049
    .domainDefineXML = vzDomainDefineXML,      /* 0.10.0 */
    .domainDefineXMLFlags = vzDomainDefineXMLFlags, /* 1.2.12 */
    .domainUndefine = vzDomainUndefine, /* 1.2.10 */
    .domainUndefineFlags = vzDomainUndefineFlags, /* 1.2.10 */
    .domainAttachDevice = vzDomainAttachDevice, /* 1.2.15 */
    .domainAttachDeviceFlags = vzDomainAttachDeviceFlags, /* 1.2.15 */
    .domainDetachDevice = vzDomainDetachDevice, /* 1.2.15 */
    .domainDetachDeviceFlags = vzDomainDetachDeviceFlags, /* 1.2.15 */
    .domainIsActive = vzDomainIsActive, /* 1.2.10 */
4050
    .domainIsUpdated = vzDomainIsUpdated,     /* 1.2.21 */
4051 4052
    .domainSetVcpus = vzDomainSetVcpus, /* 3.3.0 */
    .domainSetVcpusFlags = vzDomainSetVcpusFlags, /* 3.3.0 */
4053 4054
    .domainGetVcpusFlags = vzDomainGetVcpusFlags, /* 1.2.21 */
    .domainGetMaxVcpus = vzDomainGetMaxVcpus, /* 1.2.21 */
4055
    .domainSetUserPassword = vzDomainSetUserPassword, /* 2.0.0 */
4056 4057 4058 4059 4060 4061 4062 4063 4064 4065
    .connectDomainEventRegisterAny = vzConnectDomainEventRegisterAny, /* 1.2.10 */
    .connectDomainEventDeregisterAny = vzConnectDomainEventDeregisterAny, /* 1.2.10 */
    .nodeGetCPUMap = vzNodeGetCPUMap, /* 1.2.8 */
    .connectIsEncrypted = vzConnectIsEncrypted, /* 1.2.5 */
    .connectIsSecure = vzConnectIsSecure, /* 1.2.5 */
    .connectIsAlive = vzConnectIsAlive, /* 1.2.5 */
    .domainHasManagedSaveImage = vzDomainHasManagedSaveImage, /* 1.2.13 */
    .domainManagedSave = vzDomainManagedSave, /* 1.2.14 */
    .domainManagedSaveRemove = vzDomainManagedSaveRemove, /* 1.2.14 */
    .domainGetMaxMemory = vzDomainGetMaxMemory, /* 1.2.15 */
4066 4067 4068 4069
    .domainBlockStats = vzDomainBlockStats, /* 1.2.17 */
    .domainBlockStatsFlags = vzDomainBlockStatsFlags, /* 1.2.17 */
    .domainInterfaceStats = vzDomainInterfaceStats, /* 1.2.17 */
    .domainMemoryStats = vzDomainMemoryStats, /* 1.2.17 */
4070 4071
    .connectRegisterCloseCallback = vzConnectRegisterCloseCallback, /* 1.3.2 */
    .connectUnregisterCloseCallback = vzConnectUnregisterCloseCallback, /* 1.3.2 */
4072 4073
    .domainSetMemoryFlags = vzDomainSetMemoryFlags, /* 1.3.4 */
    .domainSetMemory = vzDomainSetMemory, /* 1.3.4 */
4074 4075 4076 4077 4078 4079 4080 4081 4082 4083 4084 4085 4086 4087 4088 4089
    .domainSnapshotNum = vzDomainSnapshotNum, /* 1.3.5 */
    .domainSnapshotListNames = vzDomainSnapshotListNames, /* 1.3.5 */
    .domainListAllSnapshots = vzDomainListAllSnapshots, /* 1.3.5 */
    .domainSnapshotGetXMLDesc = vzDomainSnapshotGetXMLDesc, /* 1.3.5 */
    .domainSnapshotNumChildren = vzDomainSnapshotNumChildren, /* 1.3.5 */
    .domainSnapshotListChildrenNames = vzDomainSnapshotListChildrenNames, /* 1.3.5 */
    .domainSnapshotListAllChildren = vzDomainSnapshotListAllChildren, /* 1.3.5 */
    .domainSnapshotLookupByName = vzDomainSnapshotLookupByName, /* 1.3.5 */
    .domainHasCurrentSnapshot = vzDomainHasCurrentSnapshot, /* 1.3.5 */
    .domainSnapshotGetParent = vzDomainSnapshotGetParent, /* 1.3.5 */
    .domainSnapshotCurrent = vzDomainSnapshotCurrent, /* 1.3.5 */
    .domainSnapshotIsCurrent = vzDomainSnapshotIsCurrent, /* 1.3.5 */
    .domainSnapshotHasMetadata = vzDomainSnapshotHasMetadata, /* 1.3.5 */
    .domainSnapshotCreateXML = vzDomainSnapshotCreateXML, /* 1.3.5 */
    .domainSnapshotDelete = vzDomainSnapshotDelete, /* 1.3.5 */
    .domainRevertToSnapshot = vzDomainRevertToSnapshot, /* 1.3.5 */
4090 4091 4092 4093 4094 4095
    .connectSupportsFeature = vzConnectSupportsFeature, /* 1.3.5 */
    .domainMigrateBegin3Params = vzDomainMigrateBegin3Params, /* 1.3.5 */
    .domainMigratePrepare3Params = vzDomainMigratePrepare3Params, /* 1.3.5 */
    .domainMigratePerform3Params = vzDomainMigratePerform3Params, /* 1.3.5 */
    .domainMigrateFinish3Params = vzDomainMigrateFinish3Params, /* 1.3.5 */
    .domainMigrateConfirm3Params = vzDomainMigrateConfirm3Params, /* 1.3.5 */
N
Nikolay Shirokovskiy 已提交
4096
    .domainUpdateDeviceFlags = vzDomainUpdateDeviceFlags, /* 2.0.0 */
4097
    .domainGetJobInfo = vzDomainGetJobInfo, /* 2.2.0 */
4098
    .domainGetJobStats = vzDomainGetJobStats, /* 2.2.0 */
4099
    .connectGetAllDomainStats = vzConnectGetAllDomainStats, /* 3.1.0 */
4100
    .domainAbortJob = vzDomainAbortJob, /* 3.1.0 */
4101
    .domainReset = vzDomainReset, /* 3.1.0 */
4102
    .domainBlockResize = vzDomainBlockResize, /* 3.3.0 */
D
Dmitry Guryanov 已提交
4103 4104
};

4105
static virConnectDriver vzConnectDriver = {
4106
    .localOnly = true,
4107
    .uriSchemes = (const char *[]){ "vz", NULL },
4108
    .hypervisorDriver = &vzHypervisorDriver,
4109 4110
};

4111 4112 4113 4114 4115 4116 4117 4118 4119 4120 4121 4122 4123 4124 4125 4126 4127 4128 4129 4130 4131 4132 4133 4134 4135 4136 4137 4138 4139 4140 4141 4142 4143 4144 4145 4146 4147 4148 4149
static int
vzStateCleanup(void)
{
    virObjectUnref(vz_driver);
    vz_driver = NULL;
    virMutexDestroy(&vz_driver_lock);
    prlsdkDeinit();
    return 0;
}

static int
vzStateInitialize(bool privileged ATTRIBUTE_UNUSED,
                  virStateInhibitCallback callback ATTRIBUTE_UNUSED,
                  void *opaque ATTRIBUTE_UNUSED)
{
    if (prlsdkInit() < 0) {
        VIR_DEBUG("%s", _("Can't initialize Parallels SDK"));
        return -1;
    }

   if (virMutexInit(&vz_driver_lock) < 0)
        goto error;

    /* Failing to create driver here is not fatal and only means
     * that next driver client will try once more when connecting */
    vz_driver = vzDriverObjNew();
    return 0;

 error:
    vzStateCleanup();
    return -1;
}

static virStateDriver vzStateDriver = {
    .name = "vz",
    .stateInitialize = vzStateInitialize,
    .stateCleanup = vzStateCleanup,
};

4150
/* Parallels domain type backward compatibility*/
4151
static virHypervisorDriver parallelsHypervisorDriver;
N
Nikolay Shirokovskiy 已提交
4152 4153 4154 4155 4156
static virConnectDriver parallelsConnectDriver = {
    .localOnly = true,
    .uriSchemes = (const char *[]){ "parallels", NULL },
    .hypervisorDriver = &parallelsHypervisorDriver,
};
4157

D
Dmitry Guryanov 已提交
4158
/**
4159
 * vzRegister:
D
Dmitry Guryanov 已提交
4160
 *
4161
 * Registers the vz driver
D
Dmitry Guryanov 已提交
4162 4163
 */
int
4164
vzRegister(void)
D
Dmitry Guryanov 已提交
4165 4166 4167 4168 4169 4170 4171 4172 4173 4174 4175
{
    char *prlctl_path;

    prlctl_path = virFindFileInPath(PRLCTL);
    if (!prlctl_path) {
        VIR_DEBUG("%s", _("Can't find prlctl command in the PATH env"));
        return 0;
    }

    VIR_FREE(prlctl_path);

4176
    /* Backward compatibility with Parallels domain type */
4177 4178
    parallelsHypervisorDriver = vzHypervisorDriver;
    parallelsHypervisorDriver.name = "Parallels";
4179
    if (virRegisterConnectDriver(&parallelsConnectDriver, true) < 0)
D
Dmitry Guryanov 已提交
4180
        return -1;
D
Dmitry Guryanov 已提交
4181

4182
    if (virRegisterConnectDriver(&vzConnectDriver, true) < 0)
4183 4184
        return -1;

4185 4186 4187
    if (virRegisterStateDriver(&vzStateDriver) < 0)
        return -1;

D
Dmitry Guryanov 已提交
4188 4189
    return 0;
}