vz_driver.c 97.9 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 38 39 40 41 42
 * <http://www.gnu.org/licenses/>.
 *
 */

#include <config.h>

#include <sys/types.h>
#include <sys/poll.h>
#include <limits.h>
#include <string.h>
#include <stdio.h>
#include <stdarg.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.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"
43
#include "virerror.h"
44
#include "viralloc.h"
45
#include "virlog.h"
46
#include "vircommand.h"
D
Dmitry Guryanov 已提交
47
#include "configmake.h"
48
#include "virfile.h"
49
#include "virstoragefile.h"
D
Dmitry Guryanov 已提交
50
#include "nodeinfo.h"
51
#include "virstring.h"
52
#include "cpu/cpu.h"
53
#include "virtypedparam.h"
54 55
#include "virhostmem.h"
#include "virhostcpu.h"
56
#include "viraccessapicheck.h"
D
Dmitry Guryanov 已提交
57

58 59 60
#include "vz_driver.h"
#include "vz_utils.h"
#include "vz_sdk.h"
D
Dmitry Guryanov 已提交
61 62 63

#define VIR_FROM_THIS VIR_FROM_PARALLELS

64 65
VIR_LOG_INIT("parallels.parallels_driver");

D
Dmitry Guryanov 已提交
66 67
#define PRLCTL                      "prlctl"

68
static virClassPtr vzDriverClass;
D
Dmitry Guryanov 已提交
69

70 71 72 73 74 75 76
static virMutex vz_driver_lock;
static vzDriverPtr vz_driver;
static vzConnPtr vz_conn_list;

static vzDriverPtr
vzDriverObjNew(void);

D
Dmitry Guryanov 已提交
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97
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 已提交
98
static virCapsPtr
99
vzBuildCapabilities(void)
D
Dmitry Guryanov 已提交
100
{
101 102 103 104
    virCapsPtr caps = NULL;
    virCPUDefPtr cpu = NULL;
    virCPUDataPtr data = NULL;
    virNodeInfo nodeinfo;
D
Dmitry Guryanov 已提交
105 106 107 108 109
    virDomainOSType ostypes[] = {
        VIR_DOMAIN_OSTYPE_HVM,
        VIR_DOMAIN_OSTYPE_EXE
    };
    virArch archs[] = { VIR_ARCH_I686, VIR_ARCH_X86_64 };
110
    const char *const emulators[] = { "vz", "parallels"};
D
Dmitry Guryanov 已提交
111
    virDomainVirtType virt_types[] = {
112 113
        VIR_DOMAIN_VIRT_VZ,
        VIR_DOMAIN_VIRT_PARALLELS
D
Dmitry Guryanov 已提交
114 115
    };
    size_t i, j, k;
D
Dmitry Guryanov 已提交
116

117
    if ((caps = virCapabilitiesNew(virArchFromHost(),
118
                                   false, false)) == NULL)
119
        return NULL;
D
Dmitry Guryanov 已提交
120

121
    if (nodeCapsInitNUMA(caps) < 0)
122
        goto error;
D
Dmitry Guryanov 已提交
123

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

131
    if (nodeGetInfo(&nodeinfo))
132 133
        goto error;

134
    if (VIR_ALLOC(cpu) < 0)
135 136 137 138 139 140 141 142 143 144
        goto error;

    cpu->arch = caps->host.arch;
    cpu->type = VIR_CPU_TYPE_HOST;
    cpu->sockets = nodeinfo.sockets;
    cpu->cores = nodeinfo.cores;
    cpu->threads = nodeinfo.threads;

    caps->host.cpu = cpu;

145 146 147
    if (virCapabilitiesAddHostMigrateTransport(caps, "vzmigr") < 0)
        goto error;

148 149 150 151 152 153 154
    if (!(data = cpuNodeData(cpu->arch))
        || cpuDecode(cpu, data, NULL, 0, NULL) < 0) {
        goto cleanup;
    }

 cleanup:
    cpuDataFree(data);
D
Dmitry Guryanov 已提交
155 156
    return caps;

157
 error:
158
    virObjectUnref(caps);
159
    goto cleanup;
D
Dmitry Guryanov 已提交
160 161
}

162 163 164 165 166 167 168 169 170 171 172 173 174
static void vzDriverDispose(void * obj)
{
    vzDriverPtr driver = obj;

    if (driver->server) {
        prlsdkUnsubscribeFromPCSEvents(driver);
        prlsdkDisconnect(driver);
    }

    virObjectUnref(driver->domains);
    virObjectUnref(driver->caps);
    virObjectUnref(driver->xmlopt);
    virObjectEventStateFree(driver->domainEventState);
175
    virSysinfoDefFree(driver->hostsysinfo);
176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226
}

static int vzDriverOnceInit(void)
{
    if (!(vzDriverClass = virClassNew(virClassForObjectLockable(),
                                      "vzDriver",
                                      sizeof(vzDriver),
                                      vzDriverDispose)))
        return -1;

    return 0;
}
VIR_ONCE_GLOBAL_INIT(vzDriver)

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);
    driver = vz_driver;
    vz_driver = NULL;

    privconn_list = vz_conn_list;
    vz_conn_list = NULL;

    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 已提交
227
static char *
228
vzConnectGetCapabilities(virConnectPtr conn)
D
Dmitry Guryanov 已提交
229
{
230
    vzConnPtr privconn = conn->privateData;
D
Dmitry Guryanov 已提交
231 232
    char *xml;

233 234 235
    if (virConnectGetCapabilitiesEnsureACL(conn) < 0)
        return NULL;

236
    xml = virCapabilitiesFormatXML(privconn->driver->caps);
D
Dmitry Guryanov 已提交
237 238
    return xml;
}
239

240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260
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 已提交
261

262
static int
263
vzDomainDefPostParse(virDomainDefPtr def,
M
Michal Privoznik 已提交
264
                     virCapsPtr caps ATTRIBUTE_UNUSED,
265
                     unsigned int parseFlags ATTRIBUTE_UNUSED,
266 267
                     void *opaque ATTRIBUTE_UNUSED,
                     void *parseOpaque ATTRIBUTE_UNUSED)
268
{
269 270 271
    if (vzDomainDefAddDefaultInputDevices(def) < 0)
        return -1;

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

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

283 284 285 286
    return 0;
}

static int
287
vzDomainDeviceDefPostParse(virDomainDeviceDefPtr dev,
M
Michal Privoznik 已提交
288 289
                           const virDomainDef *def,
                           virCapsPtr caps ATTRIBUTE_UNUSED,
290
                           unsigned int parseFlags ATTRIBUTE_UNUSED,
291 292
                           void *opaque ATTRIBUTE_UNUSED,
                           void *parseOpaque ATTRIBUTE_UNUSED)
293
{
294 295 296 297
    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 &&
298
        def->os.type == VIR_DOMAIN_OSTYPE_HVM &&
299
        VIR_STRDUP(dev->data.net->model, "e1000") < 0)
300
        return -1;
301

302 303 304 305 306 307 308 309
    return 0;
}

static int
vzDomainDeviceDefValidate(const virDomainDeviceDef *dev,
                          const virDomainDef *def,
                          void *opaque ATTRIBUTE_UNUSED)
{
310 311
    if (dev->type == VIR_DOMAIN_DEVICE_DISK)
        return vzCheckUnsupportedDisk(def, dev->data.disk, opaque);
312 313
    else if (dev->type == VIR_DOMAIN_DEVICE_GRAPHICS)
        return vzCheckUnsupportedGraphics(dev->data.graphics);
314 315

    return 0;
316 317
}

318 319 320 321
static virDomainXMLPrivateDataCallbacks vzDomainXMLPrivateDataCallbacksPtr = {
    .alloc = vzDomObjAlloc,
    .free = vzDomObjFree,
};
322

M
Maxim Nestratov 已提交
323
static virDomainDefParserConfig vzDomainDefParserConfig = {
324
    .macPrefix = {0x42, 0x1C, 0x00},
325
    .domainPostParseCallback = vzDomainDefPostParse,
326 327 328
    .devicesPostParseCallback = vzDomainDeviceDefPostParse,
    .domainValidateCallback = vzDomainDefValidate,
    .deviceValidateCallback = vzDomainDeviceDefValidate,
329 330
};

331 332
static vzDriverPtr
vzDriverObjNew(void)
D
Dmitry Guryanov 已提交
333
{
334
    vzDriverPtr driver;
D
Dmitry Guryanov 已提交
335

336 337
    if (vzDriverInitialize() < 0)
        return NULL;
D
Dmitry Guryanov 已提交
338

339 340
    if (!(driver = virObjectLockableNew(vzDriverClass)))
        return NULL;
341

342 343 344 345
    vzDomainDefParserConfig.priv = &driver->vzCaps;

    if (!(driver->caps = vzBuildCapabilities()) ||
        !(driver->xmlopt = virDomainXMLOptionNew(&vzDomainDefParserConfig,
346 347
                                                 &vzDomainXMLPrivateDataCallbacksPtr,
                                                 NULL)) ||
348 349 350 351
        !(driver->domains = virDomainObjListNew()) ||
        !(driver->domainEventState = virObjectEventStateNew()) ||
        (vzInitVersion(driver) < 0) ||
        (prlsdkConnect(driver) < 0) ||
352
        (prlsdkSubscribeToPCSEvents(driver) < 0)) {
353 354 355
        virObjectUnref(driver);
        return NULL;
    }
D
Dmitry Guryanov 已提交
356

357
    driver->hostsysinfo = virSysinfoRead();
358
    ignore_value(prlsdkLoadDomains(driver));
359 360 361 362 363

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

364
    return driver;
D
Dmitry Guryanov 已提交
365 366 367
}

static virDrvOpenStatus
368
vzConnectOpen(virConnectPtr conn,
M
Michal Privoznik 已提交
369
              virConnectAuthPtr auth ATTRIBUTE_UNUSED,
370
              virConfPtr conf ATTRIBUTE_UNUSED,
M
Michal Privoznik 已提交
371
              unsigned int flags)
D
Dmitry Guryanov 已提交
372
{
373 374
    vzDriverPtr driver = NULL;
    vzConnPtr privconn = NULL;
D
Dmitry Guryanov 已提交
375 376 377 378 379 380

    virCheckFlags(VIR_CONNECT_RO, VIR_DRV_OPEN_ERROR);

    if (!conn->uri)
        return VIR_DRV_OPEN_DECLINED;

381 382 383 384 385 386 387 388 389 390 391
    if (!conn->uri->scheme)
        return VIR_DRV_OPEN_DECLINED;

    if (STRNEQ(conn->uri->scheme, "vz") &&
        STRNEQ(conn->uri->scheme, "parallels"))
        return VIR_DRV_OPEN_DECLINED;

    if (STREQ(conn->uri->scheme, "vz") && STRNEQ(conn->driver->name, "vz"))
        return VIR_DRV_OPEN_DECLINED;

    if (STREQ(conn->uri->scheme, "parallels") && STRNEQ(conn->driver->name, "Parallels"))
D
Dmitry Guryanov 已提交
392 393 394 395 396 397 398
        return VIR_DRV_OPEN_DECLINED;

    /* Remote driver should handle these. */
    if (conn->uri->server)
        return VIR_DRV_OPEN_DECLINED;

    /* From this point on, the connection is for us. */
399
    if (STRNEQ_NULLABLE(conn->uri->path, "/system")) {
400
        virReportError(VIR_ERR_INTERNAL_ERROR,
401
                       _("Unexpected Virtuozzo URI path '%s', try vz:///system"),
402
                       conn->uri->path);
D
Dmitry Guryanov 已提交
403 404 405
        return VIR_DRV_OPEN_ERROR;
    }

406 407 408
    if (virConnectOpenEnsureACL(conn) < 0)
        return VIR_DRV_OPEN_ERROR;

409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424
    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 已提交
425 426

    return VIR_DRV_OPEN_SUCCESS;
427 428 429 430 431 432 433

 error:

    conn->privateData = NULL;
    virObjectUnref(driver);
    VIR_FREE(privconn);
    return VIR_DRV_OPEN_ERROR;
D
Dmitry Guryanov 已提交
434 435 436
}

static int
437
vzConnectClose(virConnectPtr conn)
D
Dmitry Guryanov 已提交
438
{
439
    vzConnPtr curr, *prev = &vz_conn_list;
440
    vzConnPtr privconn = conn->privateData;
D
Dmitry Guryanov 已提交
441

442 443 444
    if (!privconn)
        return 0;

445 446 447 448 449 450 451
    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 已提交
452

453
    virMutexUnlock(&vz_driver_lock);
D
Dmitry Guryanov 已提交
454

455 456
    virObjectUnref(privconn->closeCallback);
    virObjectUnref(privconn->driver);
D
Dmitry Guryanov 已提交
457
    VIR_FREE(privconn);
458
    conn->privateData = NULL;
D
Dmitry Guryanov 已提交
459 460 461 462
    return 0;
}

static int
463
vzConnectGetVersion(virConnectPtr conn, unsigned long *hvVer)
D
Dmitry Guryanov 已提交
464
{
465
    vzConnPtr privconn = conn->privateData;
466 467 468 469

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

470
    *hvVer = privconn->driver->vzVersion;
471
    return 0;
472 473
}

474

475
static char *vzConnectGetHostname(virConnectPtr conn)
476
{
477 478 479
    if (virConnectGetHostnameEnsureACL(conn) < 0)
        return NULL;

480 481 482
    return virGetHostname();
}

483 484 485 486 487 488 489 490 491
static char *
vzConnectGetSysinfo(virConnectPtr conn, unsigned int flags)
{
    vzConnPtr privconn = conn->privateData;
    vzDriverPtr driver = privconn->driver;
    virBuffer buf = VIR_BUFFER_INITIALIZER;

    virCheckFlags(0, NULL);

492 493 494
    if (virConnectGetSysinfoEnsureACL(conn) < 0)
        return NULL;

495 496 497 498 499 500 501 502 503 504 505 506 507
    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);
}
508

509
static int
510
vzConnectListDomains(virConnectPtr conn, int *ids, int maxids)
511
{
512
    vzConnPtr privconn = conn->privateData;
513 514
    int n;

515 516 517
    if (virConnectListDomainsEnsureACL(conn) < 0)
        return -1;

518
    n = virDomainObjListGetActiveIDs(privconn->driver->domains, ids, maxids,
519
                                     virConnectListDomainsCheckACL, conn);
520 521 522 523 524

    return n;
}

static int
525
vzConnectNumOfDomains(virConnectPtr conn)
526
{
527
    vzConnPtr privconn = conn->privateData;
528 529
    int count;

530 531 532
    if (virConnectNumOfDomainsEnsureACL(conn) < 0)
        return -1;

533
    count = virDomainObjListNumOfDomains(privconn->driver->domains, true,
534
                                         virConnectNumOfDomainsCheckACL, conn);
535 536 537 538 539

    return count;
}

static int
540
vzConnectListDefinedDomains(virConnectPtr conn, char **const names, int maxnames)
541
{
542
    vzConnPtr privconn = conn->privateData;
543 544
    int n;

545 546 547
    if (virConnectListDefinedDomainsEnsureACL(conn) < 0)
        return -1;

548
    memset(names, 0, sizeof(*names) * maxnames);
549
    n = virDomainObjListGetInactiveNames(privconn->driver->domains, names,
550 551 552
                                         maxnames,
                                         virConnectListDefinedDomainsCheckACL,
                                         conn);
553 554 555 556 557

    return n;
}

static int
558
vzConnectNumOfDefinedDomains(virConnectPtr conn)
559
{
560
    vzConnPtr privconn = conn->privateData;
561 562
    int count;

563 564 565
    if (virConnectNumOfDefinedDomainsEnsureACL(conn) < 0)
        return -1;

566
    count = virDomainObjListNumOfDomains(privconn->driver->domains, false,
567 568
                                         virConnectNumOfDefinedDomainsCheckACL,
                                         conn);
569 570 571 572
    return count;
}

static int
573
vzConnectListAllDomains(virConnectPtr conn,
M
Michal Privoznik 已提交
574 575
                        virDomainPtr **domains,
                        unsigned int flags)
576
{
577
    vzConnPtr privconn = conn->privateData;
578 579
    int ret = -1;

O
Osier Yang 已提交
580
    virCheckFlags(VIR_CONNECT_LIST_DOMAINS_FILTERS_ALL, -1);
581 582 583 584

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

585
    ret = virDomainObjListExport(privconn->driver->domains, conn, domains,
586
                                 virConnectListAllDomainsCheckACL, flags);
587 588 589 590 591

    return ret;
}

static virDomainPtr
592
vzDomainLookupByID(virConnectPtr conn, int id)
593
{
594
    vzConnPtr privconn = conn->privateData;
595
    virDomainPtr ret = NULL;
596 597
    virDomainObjPtr dom;

598
    dom = virDomainObjListFindByID(privconn->driver->domains, id);
599 600 601

    if (dom == NULL) {
        virReportError(VIR_ERR_NO_DOMAIN, NULL);
602
        return NULL;
603 604
    }

605 606 607
    if (virDomainLookupByIDEnsureACL(conn, dom->def) < 0)
        goto cleanup;

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

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

static virDomainPtr
618
vzDomainLookupByUUID(virConnectPtr conn, const unsigned char *uuid)
619
{
620
    vzConnPtr privconn = conn->privateData;
621
    virDomainPtr ret = NULL;
622 623
    virDomainObjPtr dom;

624
    dom = virDomainObjListFindByUUID(privconn->driver->domains, uuid);
625 626 627 628 629 630

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

634 635 636
    if (virDomainLookupByUUIDEnsureACL(conn, dom->def) < 0)
        goto cleanup;

637 638 639 640
    ret = virGetDomain(conn, dom->def->name, dom->def->uuid);
    if (ret)
        ret->id = dom->def->id;

641
 cleanup:
642
    virObjectUnlock(dom);
643 644 645 646
    return ret;
}

static virDomainPtr
647
vzDomainLookupByName(virConnectPtr conn, const char *name)
648
{
649
    vzConnPtr privconn = conn->privateData;
650
    virDomainPtr ret = NULL;
651 652
    virDomainObjPtr dom;

653
    dom = virDomainObjListFindByName(privconn->driver->domains, name);
654 655 656 657

    if (dom == NULL) {
        virReportError(VIR_ERR_NO_DOMAIN,
                       _("no domain with matching name '%s'"), name);
658
        return NULL;
659 660
    }

661 662 663
    if (virDomainLookupByNameEnsureACL(conn, dom->def) < 0)
        goto cleanup;

664 665 666 667
    ret = virGetDomain(conn, dom->def->name, dom->def->uuid);
    if (ret)
        ret->id = dom->def->id;

668
 cleanup:
669
    virDomainObjEndAPI(&dom);
670 671 672 673
    return ret;
}

static int
674
vzDomainGetInfo(virDomainPtr domain, virDomainInfoPtr info)
675
{
676
    virDomainObjPtr dom;
677
    vzDomObjPtr privdom;
678 679
    int ret = -1;

680
    if (!(dom = vzDomObjFromDomainRef(domain)))
681 682
        goto cleanup;

683 684 685
    if (virDomainGetInfoEnsureACL(domain->conn, dom->def) < 0)
        goto cleanup;

686 687
    info->state = virDomainObjGetState(dom, NULL);
    info->memory = dom->def->mem.cur_balloon;
688
    info->maxMem = virDomainDefGetMemoryTotal(dom->def);
689
    info->nrVirtCpu = virDomainDefGetVcpus(dom->def);
690
    info->cpuTime = 0;
691

692 693 694
    privdom = dom->privateData;

    if (PRL_INVALID_HANDLE != privdom->stats && virDomainObjIsActive(dom)) {
695 696 697
        unsigned long long vtime;
        size_t i;

698
        for (i = 0; i < virDomainDefGetVcpus(dom->def); ++i) {
699
            if (prlsdkGetVcpuStats(privdom->stats, i, &vtime) < 0) {
700 701 702 703 704 705 706
                virReportError(VIR_ERR_OPERATION_FAILED, "%s",
                               _("cannot read cputime for domain"));
                goto cleanup;
            }
            info->cpuTime += vtime;
        }
    }
707 708
    ret = 0;

709
 cleanup:
710
    virDomainObjEndAPI(&dom);
711 712 713 714
    return ret;
}

static char *
715
vzDomainGetOSType(virDomainPtr domain)
716
{
717
    virDomainObjPtr dom;
718 719
    char *ret = NULL;

720
    if (!(dom = vzDomObjFromDomain(domain)))
721
        return NULL;
722

723 724 725
    if (virDomainGetOSTypeEnsureACL(domain->conn, dom->def) < 0)
        goto cleanup;

726
    ignore_value(VIR_STRDUP(ret, virDomainOSTypeToString(dom->def->os.type)));
727

728
 cleanup:
729
    virObjectUnlock(dom);
730 731 732 733
    return ret;
}

static int
734
vzDomainIsPersistent(virDomainPtr domain)
735
{
736
    virDomainObjPtr dom;
737
    int ret = -1;
738

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

742 743 744 745 746 747
    if (virDomainIsPersistentEnsureACL(domain->conn, dom->def) < 0)
        goto cleanup;

    ret = 1;

 cleanup:
748
    virObjectUnlock(dom);
749
    return ret;
750 751 752
}

static int
753
vzDomainGetState(virDomainPtr domain,
M
Michal Privoznik 已提交
754
                 int *state, int *reason, unsigned int flags)
755
{
756
    virDomainObjPtr dom;
757
    int ret = -1;
758

759 760
    virCheckFlags(0, -1);

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

764 765 766
    if (virDomainGetStateEnsureACL(domain->conn, dom->def) < 0)
        goto cleanup;

767
    *state = virDomainObjGetState(dom, reason);
768
    ret = 0;
769

770
 cleanup:
771
    virObjectUnlock(dom);
772
    return ret;
773 774 775
}

static char *
776
vzDomainGetXMLDesc(virDomainPtr domain, unsigned int flags)
777
{
778
    vzConnPtr privconn = domain->conn->privateData;
779
    virDomainDefPtr def;
780
    virDomainObjPtr dom;
781 782 783 784
    char *ret = NULL;

    /* Flags checked by virDomainDefFormat */

785
    if (!(dom = vzDomObjFromDomain(domain)))
786
        return NULL;
787

788 789 790
    if (virDomainGetXMLDescEnsureACL(domain->conn, dom->def, flags) < 0)
        goto cleanup;

791
    def = (flags & VIR_DOMAIN_XML_INACTIVE) &&
792
        dom->newDef ? dom->newDef : dom->def;
793

794
    ret = virDomainDefFormat(def, privconn->driver->caps, flags);
795

796
 cleanup:
797
    virObjectUnlock(dom);
798 799 800 801
    return ret;
}

static int
802
vzDomainGetAutostart(virDomainPtr domain, int *autostart)
803
{
804
    virDomainObjPtr dom;
805
    int ret = -1;
806

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

810 811 812
    if (virDomainGetAutostartEnsureACL(domain->conn, dom->def) < 0)
        goto cleanup;

813
    *autostart = dom->autostart;
814
    ret = 0;
815

816
 cleanup:
817
    virObjectUnlock(dom);
818
    return ret;
D
Dmitry Guryanov 已提交
819 820
}

821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836
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;
}

837
static virDomainPtr
838
vzDomainDefineXMLFlags(virConnectPtr conn, const char *xml, unsigned int flags)
839
{
840
    vzConnPtr privconn = conn->privateData;
841
    virDomainPtr retdom = NULL;
842
    virDomainDefPtr def;
843
    virDomainObjPtr dom = NULL;
844
    unsigned int parse_flags = VIR_DOMAIN_DEF_PARSE_INACTIVE;
845
    vzDriverPtr driver = privconn->driver;
846
    bool job = false;
847

848 849 850
    virCheckFlags(VIR_DOMAIN_DEFINE_VALIDATE, NULL);

    if (flags & VIR_DOMAIN_DEFINE_VALIDATE)
851
        parse_flags |= VIR_DOMAIN_DEF_PARSE_VALIDATE_SCHEMA;
852

853
    if ((def = virDomainDefParseString(xml, driver->caps, driver->xmlopt,
854
                                       NULL, parse_flags)) == NULL)
855 856
        goto cleanup;

857 858 859
    if (virDomainDefineXMLFlagsEnsureACL(conn, def) < 0)
        goto cleanup;

860
    dom = virDomainObjListFindByUUIDRef(driver->domains, def->uuid);
861
    if (dom == NULL) {
862
        virResetLastError();
863
        if (def->os.type == VIR_DOMAIN_OSTYPE_HVM) {
864
            if (prlsdkCreateVm(driver, def))
865
                goto cleanup;
866
        } else if (def->os.type == VIR_DOMAIN_OSTYPE_EXE) {
867
            if (prlsdkCreateCt(conn, def))
868 869 870
                goto cleanup;
        } else {
            virReportError(VIR_ERR_INVALID_ARG,
871 872
                           _("Unsupported OS type: %s"),
                           virDomainOSTypeToString(def->os.type));
873
            goto cleanup;
874
        }
875

876
        if (!(dom = prlsdkAddDomainByUUID(driver, def->uuid)))
877
            goto cleanup;
878
    } else {
879 880
        int state, reason;

881
        state = virDomainObjGetState(dom, &reason);
882 883 884 885 886 887 888 889 890 891 892 893 894

        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. */

895
            if (!virDomainDefCheckABIStability(dom->def, def)) {
896 897 898 899 900 901
                virReportError(VIR_ERR_ARGUMENT_UNSUPPORTED, "%s",
                               _("Can't change domain configuration "
                                 "in managed save state"));
                goto cleanup;
            }
        } else {
902 903 904 905
            if (vzDomainObjBeginJob(dom) < 0)
                goto cleanup;
            job = true;

906 907 908
            if (vzEnsureDomainExists(dom) < 0)
                goto cleanup;

909
            if (prlsdkApplyConfig(driver, dom, def))
910
                goto cleanup;
911

912
            if (prlsdkUpdateDomain(driver, dom))
913 914
                goto cleanup;
        }
915 916
    }

917 918 919
    retdom = virGetDomain(conn, def->name, def->uuid);
    if (retdom)
        retdom->id = def->id;
920

921
 cleanup:
922 923 924
    if (job)
        vzDomainObjEndJob(dom);
    virDomainObjEndAPI(&dom);
925
    virDomainDefFree(def);
926
    return retdom;
927 928
}

929
static virDomainPtr
930
vzDomainDefineXML(virConnectPtr conn, const char *xml)
931
{
932
    return vzDomainDefineXMLFlags(conn, xml, 0);
933 934 935
}


936
static int
937
vzNodeGetInfo(virConnectPtr conn,
M
Michal Privoznik 已提交
938
              virNodeInfoPtr nodeinfo)
939
{
940 941 942
    if (virNodeGetInfoEnsureACL(conn) < 0)
        return -1;

943
    return nodeGetInfo(nodeinfo);
944 945
}

946
static int vzConnectIsEncrypted(virConnectPtr conn ATTRIBUTE_UNUSED)
947 948 949 950 951
{
    /* Encryption is not relevant / applicable to way we talk to PCS */
    return 0;
}

952
static int vzConnectIsSecure(virConnectPtr conn ATTRIBUTE_UNUSED)
953 954 955 956 957
{
    /* We run CLI tools directly so this is secure */
    return 1;
}

958
static int vzConnectIsAlive(virConnectPtr conn ATTRIBUTE_UNUSED)
959 960 961 962
{
    return 1;
}

963

964
static char *
965
vzConnectBaselineCPU(virConnectPtr conn,
M
Michal Privoznik 已提交
966 967 968
                     const char **xmlCPUs,
                     unsigned int ncpus,
                     unsigned int flags)
969 970 971
{
    virCheckFlags(VIR_CONNECT_BASELINE_CPU_EXPAND_FEATURES, NULL);

972 973 974
    if (virConnectBaselineCPUEnsureACL(conn) < 0)
        return NULL;

975 976 977 978
    return cpuBaselineXML(xmlCPUs, ncpus, NULL, 0, flags);
}


979
static int
980
vzDomainGetVcpus(virDomainPtr domain,
M
Michal Privoznik 已提交
981 982 983 984
                 virVcpuInfoPtr info,
                 int maxinfo,
                 unsigned char *cpumaps,
                 int maplen)
985
{
986
    virDomainObjPtr dom = NULL;
987 988 989
    size_t i;
    int ret = -1;

990
    if (!(dom = vzDomObjFromDomainRef(domain)))
991
        return -1;
992

993 994 995
    if (virDomainGetVcpusEnsureACL(domain->conn, dom->def) < 0)
        goto cleanup;

996
    if (!virDomainObjIsActive(dom)) {
997 998 999 1000 1001 1002 1003 1004
        virReportError(VIR_ERR_OPERATION_INVALID,
                       "%s",
                       _("cannot list vcpu pinning for an inactive domain"));
        goto cleanup;
    }

    if (maxinfo >= 1) {
        if (info != NULL) {
1005 1006
        vzDomObjPtr privdom;

1007
            memset(info, 0, sizeof(*info) * maxinfo);
1008 1009
            privdom = dom->privateData;

1010 1011 1012
            for (i = 0; i < maxinfo; i++) {
                info[i].number = i;
                info[i].state = VIR_VCPU_RUNNING;
1013
                if (prlsdkGetVcpuStats(privdom->stats, i, &info[i].cpuTime) < 0)
N
Nikolay Shirokovskiy 已提交
1014
                    goto cleanup;
1015 1016 1017 1018
            }
        }
        if (cpumaps != NULL) {
            memset(cpumaps, 0, maplen * maxinfo);
1019
            for (i = 0; i < maxinfo; i++)
1020
                virBitmapToDataBuf(dom->def->cpumask,
1021 1022
                                   VIR_GET_CPUMAP(cpumaps, maplen, i),
                                   maplen);
1023 1024 1025 1026 1027
        }
    }
    ret = maxinfo;

 cleanup:
1028
    virDomainObjEndAPI(&dom);
1029 1030 1031 1032
    return ret;
}


1033
static int
1034
vzNodeGetCPUMap(virConnectPtr conn,
M
Michal Privoznik 已提交
1035 1036 1037
                unsigned char **cpumap,
                unsigned int *online,
                unsigned int flags)
1038
{
1039 1040 1041
    if (virNodeGetCPUMapEnsureACL(conn) < 0)
        return -1;

1042
    return virHostCPUGetMap(cpumap, online, flags);
1043 1044
}

1045
static int
1046
vzConnectDomainEventRegisterAny(virConnectPtr conn,
M
Michal Privoznik 已提交
1047 1048 1049 1050 1051
                                virDomainPtr domain,
                                int eventID,
                                virConnectDomainEventGenericCallback callback,
                                void *opaque,
                                virFreeCallback freecb)
1052 1053
{
    int ret = -1;
1054
    vzConnPtr privconn = conn->privateData;
1055 1056 1057 1058

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

1059
    if (virDomainEventStateRegisterID(conn,
1060
                                      privconn->driver->domainEventState,
1061 1062 1063 1064 1065 1066 1067
                                      domain, eventID,
                                      callback, opaque, freecb, &ret) < 0)
        ret = -1;
    return ret;
}

static int
1068
vzConnectDomainEventDeregisterAny(virConnectPtr conn,
M
Michal Privoznik 已提交
1069
                                  int callbackID)
1070
{
1071
    vzConnPtr privconn = conn->privateData;
1072

1073 1074 1075
    if (virConnectDomainEventDeregisterAnyEnsureACL(conn) < 0)
        return -1;

1076
    if (virObjectEventStateDeregisterID(conn,
1077
                                        privconn->driver->domainEventState,
1078
                                        callbackID) < 0)
1079
        return -1;
1080

1081
    return 0;
1082
}
1083

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

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

1095 1096 1097
    if (virDomainSuspendEnsureACL(domain->conn, dom->def) < 0)
        goto cleanup;

1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118
    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;
1119 1120
}

1121 1122
static int
vzDomainResume(virDomainPtr domain)
1123
{
1124 1125 1126 1127 1128 1129 1130 1131
    vzConnPtr privconn = domain->conn->privateData;
    virDomainObjPtr dom;
    int ret = -1;
    bool job = false;

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

1132 1133 1134
    if (virDomainResumeEnsureACL(domain->conn, dom->def) < 0)
        goto cleanup;

1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155
    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;
1156 1157
}

1158
static int
1159
vzDomainCreateWithFlags(virDomainPtr domain, unsigned int flags)
1160
{
1161 1162 1163 1164 1165
    vzConnPtr privconn = domain->conn->privateData;
    virDomainObjPtr dom;
    int ret = -1;
    bool job = false;

1166 1167
    virCheckFlags(0, -1);

1168 1169 1170
    if (!(dom = vzDomObjFromDomainRef(domain)))
        return -1;

1171 1172 1173
    if (virDomainCreateWithFlagsEnsureACL(domain->conn, dom->def) < 0)
        goto cleanup;

1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194
    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;
1195 1196
}

1197
static int
1198
vzDomainDestroyFlags(virDomainPtr domain, unsigned int flags)
1199
{
1200 1201 1202 1203 1204
    vzConnPtr privconn = domain->conn->privateData;
    virDomainObjPtr dom;
    int ret = -1;
    bool job = false;

1205 1206
    virCheckFlags(0, -1);

1207 1208 1209
    if (!(dom = vzDomObjFromDomainRef(domain)))
        return -1;

1210 1211 1212
    if (virDomainDestroyFlagsEnsureACL(domain->conn, dom->def) < 0)
        goto cleanup;

1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233
    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;
1234 1235
}

1236
static int
1237 1238 1239 1240 1241 1242 1243
vzDomainDestroy(virDomainPtr dom)
{
    return vzDomainDestroyFlags(dom, 0);
}

static int
vzDomainShutdownFlags(virDomainPtr domain, unsigned int flags)
1244
{
1245 1246 1247 1248 1249
    vzConnPtr privconn = domain->conn->privateData;
    virDomainObjPtr dom;
    int ret = -1;
    bool job = false;

1250 1251
    virCheckFlags(0, -1);

1252 1253 1254
    if (!(dom = vzDomObjFromDomainRef(domain)))
        return -1;

1255 1256 1257
    if (virDomainShutdownFlagsEnsureACL(domain->conn, dom->def, flags) < 0)
        goto cleanup;

1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278
    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;
1279 1280
}

1281 1282 1283 1284 1285
static int vzDomainShutdown(virDomainPtr dom)
{
    return vzDomainShutdownFlags(dom, 0);
}

1286 1287
static int
vzDomainReboot(virDomainPtr domain, unsigned int flags)
1288
{
1289 1290 1291 1292 1293
    vzConnPtr privconn = domain->conn->privateData;
    virDomainObjPtr dom;
    int ret = -1;
    bool job = false;

1294
    virCheckFlags(0, -1);
1295 1296 1297 1298

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

1299 1300 1301
    if (virDomainRebootEnsureACL(domain->conn, dom->def, flags) < 0)
        goto cleanup;

1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322
    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;
1323 1324
}

1325
static int vzDomainIsActive(virDomainPtr domain)
1326 1327 1328 1329
{
    virDomainObjPtr dom = NULL;
    int ret = -1;

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

1333 1334 1335
    if (virDomainIsActiveEnsureACL(domain->conn, dom->def) < 0)
        goto cleanup;

1336
    ret = virDomainObjIsActive(dom);
1337 1338

 cleanup:
1339 1340 1341 1342 1343
    virObjectUnlock(dom);

    return ret;
}

1344
static int
1345
vzDomainCreate(virDomainPtr domain)
1346
{
1347
    return vzDomainCreateWithFlags(domain, 0);
1348 1349
}

1350
static int
1351
vzDomainUndefineFlags(virDomainPtr domain,
M
Michal Privoznik 已提交
1352
                      unsigned int flags)
1353
{
1354
    vzConnPtr privconn = domain->conn->privateData;
1355
    virDomainObjPtr dom = NULL;
1356 1357
    int ret = -1;
    bool job = false;
1358

1359 1360
    virCheckFlags(VIR_DOMAIN_UNDEFINE_MANAGED_SAVE |
                  VIR_DOMAIN_UNDEFINE_SNAPSHOTS_METADATA, -1);
1361

1362
    if (!(dom = vzDomObjFromDomainRef(domain)))
1363 1364
        return -1;

1365 1366 1367
    if (virDomainUndefineFlagsEnsureACL(domain->conn, dom->def) < 0)
        goto cleanup;

1368 1369 1370 1371
    if (vzDomainObjBeginJob(dom) < 0)
        goto cleanup;
    job = true;

1372 1373 1374
    if (vzEnsureDomainExists(dom) < 0)
        goto cleanup;

1375
    ret = prlsdkUnregisterDomain(privconn->driver, dom, flags);
1376 1377 1378 1379 1380 1381

 cleanup:

    if (job)
        vzDomainObjEndJob(dom);
    virDomainObjEndAPI(&dom);
1382 1383

    return ret;
1384 1385 1386
}

static int
1387
vzDomainUndefine(virDomainPtr domain)
1388
{
1389
    return vzDomainUndefineFlags(domain, 0);
1390 1391
}

1392
static int
1393
vzDomainHasManagedSaveImage(virDomainPtr domain, unsigned int flags)
1394 1395
{
    virDomainObjPtr dom = NULL;
1396
    int state, reason;
1397
    int ret = -1;
1398 1399 1400

    virCheckFlags(0, -1);

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

1404 1405 1406
    if (virDomainHasManagedSaveImageEnsureACL(domain->conn, dom->def) < 0)
        goto cleanup;

1407 1408 1409
    state = virDomainObjGetState(dom, &reason);
    if (state == VIR_DOMAIN_SHUTOFF && reason == VIR_DOMAIN_SHUTOFF_SAVED)
        ret = 1;
1410 1411 1412 1413
    else
        ret = 0;

 cleanup:
1414 1415
    virObjectUnlock(dom);

1416 1417 1418 1419
    return ret;
}

static int
1420
vzDomainManagedSave(virDomainPtr domain, unsigned int flags)
1421
{
1422
    vzConnPtr privconn = domain->conn->privateData;
1423 1424 1425
    virDomainObjPtr dom = NULL;
    int state, reason;
    int ret = -1;
1426
    bool job = false;
1427 1428 1429 1430

    virCheckFlags(VIR_DOMAIN_SAVE_RUNNING |
                  VIR_DOMAIN_SAVE_PAUSED, -1);

1431
    if (!(dom = vzDomObjFromDomainRef(domain)))
1432 1433
        return -1;

1434 1435 1436
    if (virDomainManagedSaveEnsureACL(domain->conn, dom->def) < 0)
        goto cleanup;

1437 1438 1439 1440
    if (vzDomainObjBeginJob(dom) < 0)
        goto cleanup;
    job = true;

1441 1442 1443
    if (vzEnsureDomainExists(dom) < 0)
        goto cleanup;

1444 1445
    state = virDomainObjGetState(dom, &reason);

1446 1447 1448
    if (state == VIR_DOMAIN_RUNNING && (flags & VIR_DOMAIN_SAVE_PAUSED) &&
        prlsdkPause(dom) < 0)
        goto cleanup;
1449

1450 1451 1452 1453 1454 1455 1456
    if (prlsdkSuspend(dom) < 0)
        goto cleanup;

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

    ret = 0;
1457 1458

 cleanup:
1459 1460 1461
    if (job)
        vzDomainObjEndJob(dom);
    virDomainObjEndAPI(&dom);
1462 1463 1464 1465
    return ret;
}

static int
1466
vzDomainManagedSaveRemove(virDomainPtr domain, unsigned int flags)
1467 1468 1469 1470 1471 1472 1473
{
    virDomainObjPtr dom = NULL;
    int state, reason;
    int ret = -1;

    virCheckFlags(0, -1);

1474
    if (!(dom = vzDomObjFromDomainRef(domain)))
1475 1476
        return -1;

1477 1478 1479
    if (virDomainManagedSaveRemoveEnsureACL(domain->conn, dom->def) < 0)
        goto cleanup;

1480 1481 1482 1483 1484
    state = virDomainObjGetState(dom, &reason);

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

1485
    ret = prlsdkDomainManagedSaveRemove(dom);
1486 1487

 cleanup:
1488
    virDomainObjEndAPI(&dom);
1489
    return ret;
1490 1491
}

1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513
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;
}

1514
static int vzDomainAttachDeviceFlags(virDomainPtr domain, const char *xml,
M
Michal Privoznik 已提交
1515
                                     unsigned int flags)
1516 1517
{
    int ret = -1;
1518
    vzConnPtr privconn = domain->conn->privateData;
1519
    virDomainDeviceDefPtr dev = NULL;
1520
    virDomainObjPtr dom = NULL;
1521
    vzDriverPtr driver = privconn->driver;
1522
    bool job = false;
1523 1524 1525 1526

    virCheckFlags(VIR_DOMAIN_AFFECT_LIVE |
                  VIR_DOMAIN_AFFECT_CONFIG, -1);

1527
    if (!(dom = vzDomObjFromDomainRef(domain)))
1528
        return -1;
1529

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

1533 1534 1535
    if (virDomainAttachDeviceFlagsEnsureACL(domain->conn, dom->def, flags) < 0)
        goto cleanup;

1536 1537
    dev = virDomainDeviceDefParse(xml, dom->def, driver->caps,
                                  driver->xmlopt, VIR_DOMAIN_XML_INACTIVE);
1538 1539 1540
    if (dev == NULL)
        goto cleanup;

1541 1542 1543 1544
    if (vzDomainObjBeginJob(dom) < 0)
        goto cleanup;
    job = true;

1545 1546 1547
    if (vzEnsureDomainExists(dom) < 0)
        goto cleanup;

1548 1549
    if (prlsdkAttachDevice(driver, dom, dev) < 0)
        goto cleanup;
1550

1551 1552 1553
    if (prlsdkUpdateDomain(driver, dom) < 0)
        goto cleanup;

1554 1555
    ret = 0;
 cleanup:
1556
    virDomainDeviceDefFree(dev);
1557 1558 1559
    if (job)
        vzDomainObjEndJob(dom);
    virDomainObjEndAPI(&dom);
1560 1561 1562
    return ret;
}

1563
static int vzDomainAttachDevice(virDomainPtr domain, const char *xml)
1564
{
1565
    return vzDomainAttachDeviceFlags(domain, xml,
M
Michal Privoznik 已提交
1566
                                     VIR_DOMAIN_AFFECT_CONFIG | VIR_DOMAIN_AFFECT_LIVE);
1567 1568
}

1569
static int vzDomainDetachDeviceFlags(virDomainPtr domain, const char *xml,
M
Michal Privoznik 已提交
1570
                                     unsigned int flags)
1571 1572
{
    int ret = -1;
1573
    vzConnPtr privconn = domain->conn->privateData;
1574
    virDomainDeviceDefPtr dev = NULL;
1575
    virDomainObjPtr dom = NULL;
1576
    vzDriverPtr driver = privconn->driver;
1577
    bool job = false;
1578 1579 1580 1581

    virCheckFlags(VIR_DOMAIN_AFFECT_LIVE |
                  VIR_DOMAIN_AFFECT_CONFIG, -1);

1582
    dom = vzDomObjFromDomainRef(domain);
1583
    if (dom == NULL)
1584 1585
        return -1;

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

1589 1590 1591
    if (virDomainDetachDeviceFlagsEnsureACL(domain->conn, dom->def, flags) < 0)
        goto cleanup;

1592 1593
    dev = virDomainDeviceDefParse(xml, dom->def, driver->caps,
                                  driver->xmlopt,
1594 1595
                                  VIR_DOMAIN_XML_INACTIVE |
                                  VIR_DOMAIN_DEF_PARSE_SKIP_VALIDATE);
1596 1597 1598
    if (dev == NULL)
        goto cleanup;

1599 1600 1601 1602
    if (vzDomainObjBeginJob(dom) < 0)
        goto cleanup;
    job = true;

1603 1604 1605
    if (vzEnsureDomainExists(dom) < 0)
        goto cleanup;

1606 1607
    if (prlsdkDetachDevice(driver, dom, dev) < 0)
        goto cleanup;
1608

1609 1610 1611
    if (prlsdkUpdateDomain(driver, dom) < 0)
        goto cleanup;

1612 1613
    ret = 0;
 cleanup:
1614
    virDomainDeviceDefFree(dev);
1615 1616 1617
    if (job)
        vzDomainObjEndJob(dom);
    virDomainObjEndAPI(&dom);
1618

1619 1620 1621
    return ret;
}

1622
static int vzDomainDetachDevice(virDomainPtr domain, const char *xml)
1623
{
1624
    return vzDomainDetachDeviceFlags(domain, xml,
M
Michal Privoznik 已提交
1625
                                     VIR_DOMAIN_AFFECT_CONFIG | VIR_DOMAIN_AFFECT_LIVE);
1626 1627
}

1628 1629 1630 1631 1632 1633 1634 1635
static int
vzDomainSetUserPassword(virDomainPtr domain,
                        const char *user,
                        const char *password,
                        unsigned int flags)
{
    virDomainObjPtr dom = NULL;
    int ret = -1;
1636
    bool job = false;
1637 1638

    virCheckFlags(0, -1);
1639
    if (!(dom = vzDomObjFromDomainRef(domain)))
1640 1641
        return -1;

1642 1643 1644
    if (virDomainSetUserPasswordEnsureACL(domain->conn, dom->def) < 0)
        goto cleanup;

1645 1646 1647 1648
    if (vzDomainObjBeginJob(dom) < 0)
        goto cleanup;
    job = true;

1649 1650 1651
    if (vzEnsureDomainExists(dom) < 0)
        goto cleanup;

1652 1653
    ret = prlsdkDomainSetUserPassword(dom, user, password);

1654 1655 1656 1657
 cleanup:
    if (job)
        vzDomainObjEndJob(dom);
    virDomainObjEndAPI(&dom);
1658 1659 1660
    return ret;
}

1661
static int vzDomainUpdateDeviceFlags(virDomainPtr domain,
N
Nikolay Shirokovskiy 已提交
1662 1663 1664 1665
                                     const char *xml,
                                     unsigned int flags)
{
    int ret = -1;
1666 1667
    vzConnPtr privconn = domain->conn->privateData;
    virDomainObjPtr dom = NULL;
N
Nikolay Shirokovskiy 已提交
1668 1669
    virDomainDeviceDefPtr dev = NULL;
    vzDriverPtr driver = privconn->driver;
1670
    bool job = false;
N
Nikolay Shirokovskiy 已提交
1671 1672 1673 1674

    virCheckFlags(VIR_DOMAIN_AFFECT_LIVE |
                  VIR_DOMAIN_AFFECT_CONFIG, -1);

1675
    if (!(dom = vzDomObjFromDomainRef(domain)))
N
Nikolay Shirokovskiy 已提交
1676 1677
        return -1;

1678 1679 1680
    if (virDomainUpdateDeviceFlagsEnsureACL(domain->conn, dom->def, flags) < 0)
        goto cleanup;

1681
    if (vzCheckConfigUpdateFlags(dom, &flags) < 0)
N
Nikolay Shirokovskiy 已提交
1682 1683
        goto cleanup;

1684
    if (!(dev = virDomainDeviceDefParse(xml, dom->def, driver->caps,
N
Nikolay Shirokovskiy 已提交
1685 1686 1687 1688
                                        driver->xmlopt,
                                        VIR_DOMAIN_XML_INACTIVE)))
        goto cleanup;

1689 1690 1691 1692
    if (vzDomainObjBeginJob(dom) < 0)
        goto cleanup;
    job = true;

1693 1694 1695
    if (vzEnsureDomainExists(dom) < 0)
        goto cleanup;

1696
    if (prlsdkUpdateDevice(driver, dom, dev) < 0)
N
Nikolay Shirokovskiy 已提交
1697 1698
        goto cleanup;

1699 1700 1701
    if (prlsdkUpdateDomain(driver, dom) < 0)
        goto cleanup;

N
Nikolay Shirokovskiy 已提交
1702 1703 1704 1705
    ret = 0;
 cleanup:

    virDomainDeviceDefFree(dev);
1706 1707 1708
    if (job)
        vzDomainObjEndJob(dom);
    virDomainObjEndAPI(&dom);
N
Nikolay Shirokovskiy 已提交
1709 1710 1711 1712
    return ret;
}


1713
static unsigned long long
1714
vzDomainGetMaxMemory(virDomainPtr domain)
1715 1716 1717 1718
{
    virDomainObjPtr dom = NULL;
    int ret = -1;

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

1722 1723 1724
    if (virDomainGetMaxMemoryEnsureACL(domain->conn, dom->def) < 0)
        goto cleanup;

1725
    ret = virDomainDefGetMemoryTotal(dom->def);
1726 1727

 cleanup:
1728 1729 1730 1731
    virObjectUnlock(dom);
    return ret;
}

1732
static int
1733 1734 1735
vzDomainBlockStatsImpl(virDomainObjPtr dom,
                       const char *path,
                       virDomainBlockStatsPtr stats)
1736
{
1737
    vzDomObjPtr privdom = dom->privateData;
1738 1739 1740 1741 1742 1743
    size_t i;
    int idx;

    if (*path) {
        if ((idx = virDomainDiskIndexByName(dom->def, path, false)) < 0) {
            virReportError(VIR_ERR_INVALID_ARG, _("invalid path: %s"), path);
1744
            return -1;
1745
        }
1746
        if (prlsdkGetBlockStats(privdom->stats, dom->def->disks[idx], stats) < 0)
1747
            return -1;
1748 1749 1750
    } else {
        virDomainBlockStatsStruct s;

M
Michal Privoznik 已提交
1751
#define PARALLELS_ZERO_STATS(VAR, TYPE, NAME)      \
1752 1753 1754 1755 1756 1757 1758
        stats->VAR = 0;

        PARALLELS_BLOCK_STATS_FOREACH(PARALLELS_ZERO_STATS)

#undef PARALLELS_ZERO_STATS

        for (i = 0; i < dom->def->ndisks; i++) {
1759
            if (prlsdkGetBlockStats(privdom->stats, dom->def->disks[i], &s) < 0)
1760
                return -1;
1761

1762
#define PARALLELS_SUM_STATS(VAR, TYPE, NAME)        \
M
Michal Privoznik 已提交
1763 1764
    if (s.VAR != -1)                                \
        stats->VAR += s.VAR;
1765

M
Michal Privoznik 已提交
1766
        PARALLELS_BLOCK_STATS_FOREACH(PARALLELS_SUM_STATS)
1767

1768
#undef PARALLELS_SUM_STATS
1769 1770 1771
        }
    }
    stats->errs = -1;
1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785
    return 0;
}

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

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

1786 1787 1788
    if (virDomainBlockStatsEnsureACL(domain->conn, dom->def) < 0)
        goto cleanup;

1789 1790 1791
    if (vzDomainBlockStatsImpl(dom, path, stats) < 0)
        goto cleanup;

1792 1793 1794
    ret = 0;

 cleanup:
1795
    virDomainObjEndAPI(&dom);
1796 1797 1798 1799

    return ret;
}

1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834
static int
vzDomainBlockStatsToParams(virDomainBlockStatsPtr stats,
                           virTypedParameterPtr params,
                           int *nparams)
{
    size_t i;

    if (*nparams == 0) {
#define PARALLELS_COUNT_STATS(VAR, TYPE, NAME)      \
        if ((stats->VAR) != -1)                     \
            ++*nparams;

        PARALLELS_BLOCK_STATS_FOREACH(PARALLELS_COUNT_STATS)

#undef PARALLELS_COUNT_STATS
        return 0;
    }

    i = 0;
#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++;                                                                    \
    }

    PARALLELS_BLOCK_STATS_FOREACH(PARALLELS_BLOCK_STATS_ASSIGN_PARAM)

#undef PARALLELS_BLOCK_STATS_ASSIGN_PARAM

    *nparams = i;
    return 0;
}

1835
static int
1836
vzDomainBlockStatsFlags(virDomainPtr domain,
M
Michal Privoznik 已提交
1837 1838 1839 1840
                        const char *path,
                        virTypedParameterPtr params,
                        int *nparams,
                        unsigned int flags)
1841 1842
{
    virDomainBlockStatsStruct stats;
1843
    virDomainObjPtr dom;
1844 1845 1846 1847 1848 1849
    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;

1850 1851 1852
    if (!(dom = vzDomObjFromDomainRef(domain)))
        return -1;

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

1856
    if (vzDomainBlockStatsImpl(dom, path, &stats) < 0)
1857 1858
        goto cleanup;

1859
    if (vzDomainBlockStatsToParams(&stats, params, nparams) < 0)
1860 1861 1862 1863 1864
        goto cleanup;

    ret = 0;

 cleanup:
1865 1866
    virDomainObjEndAPI(&dom);

1867 1868 1869
    return ret;
}

1870 1871 1872 1873 1874 1875
static int
vzDomainInterfaceStats(virDomainPtr domain,
                         const char *path,
                         virDomainInterfaceStatsPtr stats)
{
    virDomainObjPtr dom = NULL;
1876
    vzDomObjPtr privdom;
1877
    int ret = -1;
1878 1879 1880 1881

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

1882 1883 1884
    if (virDomainInterfaceStatsEnsureACL(domain->conn, dom->def) < 0)
        goto cleanup;

1885 1886 1887
    privdom = dom->privateData;

    ret = prlsdkGetNetStats(privdom->stats, privdom->sdkdom, path, stats);
1888 1889

 cleanup:
1890 1891 1892 1893
    virDomainObjEndAPI(&dom);

    return ret;
}
1894

N
Nikolay Shirokovskiy 已提交
1895 1896 1897 1898 1899 1900 1901
static int
vzDomainMemoryStats(virDomainPtr domain,
                    virDomainMemoryStatPtr stats,
                    unsigned int nr_stats,
                    unsigned int flags)
{
    virDomainObjPtr dom = NULL;
1902
    vzDomObjPtr privdom;
N
Nikolay Shirokovskiy 已提交
1903 1904 1905 1906 1907 1908
    int ret = -1;

    virCheckFlags(0, -1);
    if (!(dom = vzDomObjFromDomainRef(domain)))
        return -1;

1909 1910 1911
    if (virDomainMemoryStatsEnsureACL(domain->conn, dom->def) < 0)
        goto cleanup;

1912 1913 1914
    privdom = dom->privateData;

    ret = prlsdkGetMemoryStats(privdom->stats, stats, nr_stats);
1915 1916

 cleanup:
N
Nikolay Shirokovskiy 已提交
1917 1918 1919 1920 1921
    virDomainObjEndAPI(&dom);

    return ret;
}

1922
static int
1923
vzDomainGetVcpusFlags(virDomainPtr domain,
1924 1925
                      unsigned int flags)
{
1926
    virDomainObjPtr dom;
1927
    int ret = -1;
1928 1929 1930 1931 1932

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

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

1936 1937 1938
    if (virDomainGetVcpusFlagsEnsureACL(domain->conn, dom->def, flags) < 0)
        goto cleanup;

1939
    if (flags & VIR_DOMAIN_VCPU_MAXIMUM)
1940
        ret = virDomainDefGetVcpusMax(dom->def);
1941
    else
1942
        ret = virDomainDefGetVcpus(dom->def);
1943

1944
 cleanup:
1945
    virObjectUnlock(dom);
1946 1947 1948 1949

    return ret;
}

1950
static int vzDomainGetMaxVcpus(virDomainPtr domain)
1951
{
1952 1953
    return vzDomainGetVcpusFlags(domain, (VIR_DOMAIN_AFFECT_LIVE |
                                          VIR_DOMAIN_VCPU_MAXIMUM));
1954 1955
}

1956
static int vzDomainIsUpdated(virDomainPtr domain)
1957
{
1958
    virDomainObjPtr dom;
1959
    int ret = -1;
1960 1961 1962

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

1966 1967 1968 1969 1970 1971
    if (virDomainIsUpdatedEnsureACL(domain->conn, dom->def) < 0)
        goto cleanup;

    ret = 0;

 cleanup:
1972
    virObjectUnlock(dom);
1973
    return ret;
1974 1975
}

1976
static int vzConnectGetMaxVcpus(virConnectPtr conn,
1977 1978
                                const char *type)
{
1979 1980 1981
    if (virConnectGetMaxVcpusEnsureACL(conn) < 0)
        return -1;

1982 1983 1984 1985 1986 1987 1988 1989 1990 1991
    /* 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;
}

1992
static int
1993
vzNodeGetCPUStats(virConnectPtr conn,
1994 1995 1996 1997 1998
                  int cpuNum,
                  virNodeCPUStatsPtr params,
                  int *nparams,
                  unsigned int flags)
{
1999 2000 2001
    if (virNodeGetCPUStatsEnsureACL(conn) < 0)
        return -1;

2002
    return virHostCPUGetStats(cpuNum, params, nparams, flags);
2003 2004 2005
}

static int
2006
vzNodeGetMemoryStats(virConnectPtr conn,
2007 2008 2009 2010 2011
                     int cellNum,
                     virNodeMemoryStatsPtr params,
                     int *nparams,
                     unsigned int flags)
{
2012 2013 2014
    if (virNodeGetMemoryStatsEnsureACL(conn) < 0)
        return -1;

2015
    return virHostMemGetStats(cellNum, params, nparams, flags);
2016 2017 2018
}

static int
2019
vzNodeGetCellsFreeMemory(virConnectPtr conn,
2020 2021 2022 2023
                         unsigned long long *freeMems,
                         int startCell,
                         int maxCells)
{
2024 2025 2026
    if (virNodeGetCellsFreeMemoryEnsureACL(conn) < 0)
        return -1;

2027
    return virHostMemGetCellsFree(freeMems, startCell, maxCells);
2028 2029 2030
}

static unsigned long long
2031
vzNodeGetFreeMemory(virConnectPtr conn)
2032 2033
{
    unsigned long long freeMem;
2034 2035 2036 2037

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

2038
    if (virHostMemGetInfo(NULL, &freeMem) < 0)
2039 2040 2041 2042
        return 0;
    return freeMem;
}

2043 2044 2045 2046 2047 2048 2049 2050 2051
static int
vzConnectRegisterCloseCallback(virConnectPtr conn,
                               virConnectCloseFunc cb,
                               void *opaque,
                               virFreeCallback freecb)
{
    vzConnPtr privconn = conn->privateData;
    int ret = -1;

2052 2053 2054
    if (virConnectRegisterCloseCallbackEnsureACL(conn) < 0)
        return -1;

2055 2056
    virObjectLock(privconn->driver);

2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067
    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:
2068
    virObjectUnlock(privconn->driver);
2069 2070 2071 2072 2073 2074 2075 2076 2077 2078

    return ret;
}

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

2079 2080 2081
    if (virConnectUnregisterCloseCallbackEnsureACL(conn) < 0)
        return -1;

2082
    virObjectLock(privconn->driver);
2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093

    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:
2094
    virObjectUnlock(privconn->driver);
2095 2096 2097 2098

    return ret;
}

2099 2100
static int vzDomainSetMemoryFlags(virDomainPtr domain, unsigned long memory,
                                  unsigned int flags)
2101 2102 2103
{
    virDomainObjPtr dom = NULL;
    int ret = -1;
2104
    bool job = false;
2105 2106 2107 2108

    virCheckFlags(VIR_DOMAIN_AFFECT_LIVE |
                  VIR_DOMAIN_AFFECT_CONFIG, -1);

2109
    if (!(dom = vzDomObjFromDomainRef(domain)))
2110 2111
        return -1;

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

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

2118 2119 2120 2121
    if (vzDomainObjBeginJob(dom) < 0)
        goto cleanup;
    job = true;

2122 2123 2124
    if (vzEnsureDomainExists(dom) < 0)
        goto cleanup;

2125 2126 2127 2128
    ret = prlsdkSetMemsize(dom, memory >> 10);

 cleanup:

2129 2130 2131
    if (job)
        vzDomainObjEndJob(dom);
    virDomainObjEndAPI(&dom);
2132 2133 2134 2135 2136
    return ret;
}

static int vzDomainSetMemory(virDomainPtr domain, unsigned long memory)
{
2137 2138 2139 2140 2141 2142 2143
    virDomainObjPtr dom = NULL;
    int ret = -1;
    bool job = false;

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

2144 2145 2146
    if (virDomainSetMemoryEnsureACL(domain->conn, dom->def) < 0)
        goto cleanup;

2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161
    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;
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 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215
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);

2216
    if (!(dom = vzDomObjFromDomainRef(domain)))
2217 2218
        return -1;

2219 2220 2221
    if (virDomainSnapshotNumEnsureACL(domain->conn, dom->def) < 0)
        goto cleanup;

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

    n = virDomainSnapshotObjListNum(snapshots, NULL, flags);

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

    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);

2247
    if (!(dom = vzDomObjFromDomainRef(domain)))
2248 2249
        return -1;

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

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

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

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

    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);

2277
    if (!(dom = vzDomObjFromDomainRef(domain)))
2278 2279
        return -1;

2280 2281 2282
    if (virDomainListAllSnapshotsEnsureACL(domain->conn, dom->def) < 0)
        goto cleanup;

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

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

 cleanup:
    virDomainSnapshotObjListFree(snapshots);
2290
    virDomainObjEndAPI(&dom);
2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306

    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);

2307
    if (!(dom = vzDomObjFromDomainRef(snapshot->domain)))
2308 2309
        return NULL;

2310 2311 2312
    if (virDomainSnapshotGetXMLDescEnsureACL(snapshot->domain->conn, dom->def, flags) < 0)
        goto cleanup;

2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326
    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,
                                     virDomainDefFormatConvertXMLFlags(flags),
                                     0);

 cleanup:
    virDomainSnapshotObjListFree(snapshots);
2327
    virDomainObjEndAPI(&dom);
2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342

    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);

2343
    if (!(dom = vzDomObjFromDomainRef(snapshot->domain)))
2344 2345
        return -1;

2346 2347 2348
    if (virDomainSnapshotNumChildrenEnsureACL(snapshot->domain->conn, dom->def) < 0)
        goto cleanup;

2349 2350 2351 2352 2353 2354 2355 2356 2357 2358
    if (!(snapshots = prlsdkLoadSnapshots(dom)))
        goto cleanup;

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

    n = virDomainSnapshotObjListNum(snapshots, snap, flags);

 cleanup:
    virDomainSnapshotObjListFree(snapshots);
2359
    virDomainObjEndAPI(&dom);
2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377

    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);

2378
    if (!(dom = vzDomObjFromDomainRef(snapshot->domain)))
2379 2380
        return -1;

2381 2382 2383
    if (virDomainSnapshotListChildrenNamesEnsureACL(snapshot->domain->conn, dom->def) < 0)
        goto cleanup;

2384 2385 2386 2387 2388 2389 2390 2391 2392 2393
    if (!(snapshots = prlsdkLoadSnapshots(dom)))
        goto cleanup;

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

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

 cleanup:
    virDomainSnapshotObjListFree(snapshots);
2394
    virDomainObjEndAPI(&dom);
2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411

    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);

2412
    if (!(dom = vzDomObjFromDomainRef(snapshot->domain)))
2413 2414
        return -1;

2415 2416 2417
    if (virDomainSnapshotListAllChildrenEnsureACL(snapshot->domain->conn, dom->def) < 0)
        goto cleanup;

2418 2419 2420 2421 2422 2423 2424 2425 2426 2427
    if (!(snapshots = prlsdkLoadSnapshots(dom)))
        goto cleanup;

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

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

 cleanup:
    virDomainSnapshotObjListFree(snapshots);
2428
    virDomainObjEndAPI(&dom);
2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444

    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);

2445
    if (!(dom = vzDomObjFromDomainRef(domain)))
2446 2447
        return NULL;

2448 2449 2450
    if (virDomainSnapshotLookupByNameEnsureACL(domain->conn, dom->def) < 0)
        goto cleanup;

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

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

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

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

    return snapshot;
}

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

    virCheckFlags(0, -1);

2475
    if (!(dom = vzDomObjFromDomainRef(domain)))
2476 2477
        return -1;

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

2481 2482 2483 2484 2485 2486 2487
    if (!(snapshots = prlsdkLoadSnapshots(dom)))
        goto cleanup;

    ret = vzFindCurrentSnapshot(snapshots) != NULL;

 cleanup:
    virDomainSnapshotObjListFree(snapshots);
2488
    virDomainObjEndAPI(&dom);
2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502

    return ret;
}

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

    virCheckFlags(0, NULL);

2503
    if (!(dom = vzDomObjFromDomainRef(snapshot->domain)))
2504 2505
        return NULL;

2506 2507 2508
    if (virDomainSnapshotGetParentEnsureACL(snapshot->domain->conn, dom->def) < 0)
        goto cleanup;

2509 2510 2511 2512 2513 2514 2515 2516 2517 2518 2519 2520 2521 2522 2523 2524 2525
    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);
2526
    virDomainObjEndAPI(&dom);
2527 2528 2529 2530 2531 2532 2533 2534 2535 2536 2537 2538 2539 2540

    return parent;
}

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

    virCheckFlags(0, NULL);

2541
    if (!(dom = vzDomObjFromDomainRef(domain)))
2542 2543
        return NULL;

2544 2545 2546
    if (virDomainSnapshotCurrentEnsureACL(domain->conn, dom->def) < 0)
        goto cleanup;

2547 2548 2549 2550 2551 2552 2553 2554 2555 2556 2557 2558 2559
    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);
2560
    virDomainObjEndAPI(&dom);
2561 2562 2563 2564 2565 2566 2567 2568 2569 2570 2571 2572 2573 2574

    return snapshot;
}

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

    virCheckFlags(0, -1);

2575
    if (!(dom = vzDomObjFromDomainRef(snapshot->domain)))
2576 2577
        return -1;

2578 2579 2580
    if (virDomainSnapshotIsCurrentEnsureACL(snapshot->domain->conn, dom->def) < 0)
        goto cleanup;

2581 2582 2583 2584 2585 2586 2587 2588
    if (!(snapshots = prlsdkLoadSnapshots(dom)))
        goto cleanup;

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

 cleanup:
    virDomainSnapshotObjListFree(snapshots);
2589
    virDomainObjEndAPI(&dom);
2590 2591 2592 2593 2594 2595 2596 2597 2598 2599 2600 2601 2602 2603 2604

    return ret;
}

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

    virCheckFlags(0, -1);

2605
    if (!(dom = vzDomObjFromDomainRef(snapshot->domain)))
2606 2607
        return -1;

2608 2609 2610
    if (virDomainSnapshotHasMetadataEnsureACL(snapshot->domain->conn, dom->def) < 0)
        goto cleanup;

2611 2612 2613 2614 2615 2616 2617 2618 2619 2620
    if (!(snapshots = prlsdkLoadSnapshots(dom)))
        goto cleanup;

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

    ret = 1;

 cleanup:
    virDomainSnapshotObjListFree(snapshots);
2621 2622
    virDomainObjEndAPI(&dom);

2623 2624 2625 2626 2627 2628 2629 2630 2631 2632 2633 2634 2635 2636 2637 2638
    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;
2639
    bool job = false;
2640 2641 2642

    virCheckFlags(0, NULL);

2643
    if (!(dom = vzDomObjFromDomainRef(domain)))
2644 2645
        return NULL;

2646 2647 2648
    if (virDomainSnapshotCreateXMLEnsureACL(domain->conn, dom->def, flags) < 0)
        goto cleanup;

2649 2650 2651 2652 2653 2654 2655 2656 2657 2658 2659 2660 2661 2662 2663 2664
    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;
    }

2665 2666 2667 2668
    if (vzDomainObjBeginJob(dom) < 0)
        goto cleanup;
    job = true;

2669 2670 2671
    if (vzEnsureDomainExists(dom) < 0)
        goto cleanup;

2672 2673 2674 2675 2676 2677 2678 2679 2680 2681 2682 2683 2684 2685 2686 2687 2688 2689 2690
    /* 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);
2691 2692 2693
    if (job)
        vzDomainObjEndJob(dom);
    virDomainObjEndAPI(&dom);
2694 2695 2696 2697 2698 2699 2700 2701 2702 2703 2704 2705

    return snapshot;
}

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

    virCheckFlags(VIR_DOMAIN_SNAPSHOT_DELETE_CHILDREN, -1);

2706
    if (!(dom = vzDomObjFromDomainRef(snapshot->domain)))
2707 2708
        return -1;

2709 2710 2711
    if (virDomainSnapshotDeleteEnsureACL(snapshot->domain->conn, dom->def) < 0)
        goto cleanup;

2712 2713 2714
    ret = prlsdkDeleteSnapshot(dom, snapshot->name,
                               flags & VIR_DOMAIN_SNAPSHOT_DELETE_CHILDREN);

2715
 cleanup:
2716
    virDomainObjEndAPI(&dom);
2717 2718 2719 2720 2721 2722 2723 2724 2725

    return ret;
}

static int
vzDomainRevertToSnapshot(virDomainSnapshotPtr snapshot, unsigned int flags)
{
    virDomainObjPtr dom;
    int ret = -1;
2726
    bool job = false;
2727 2728 2729 2730 2731 2732

    virCheckFlags(VIR_DOMAIN_SNAPSHOT_REVERT_PAUSED, -1);

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

2733 2734 2735
    if (virDomainRevertToSnapshotEnsureACL(snapshot->domain->conn, dom->def) < 0)
        goto cleanup;

2736 2737 2738 2739
    if (vzDomainObjBeginJob(dom) < 0)
        goto cleanup;
    job = true;

2740 2741 2742
    if (vzEnsureDomainExists(dom) < 0)
        goto cleanup;

2743 2744
    ret = prlsdkSwitchToSnapshot(dom, snapshot->name,
                                 flags & VIR_DOMAIN_SNAPSHOT_REVERT_PAUSED);
2745 2746 2747 2748
 cleanup:
    if (job)
        vzDomainObjEndJob(dom);
    virDomainObjEndAPI(&dom);
2749 2750 2751 2752

    return ret;
}

2753 2754 2755 2756 2757 2758
enum vzMigrationCookieFeatures {
    VZ_MIGRATION_COOKIE_SESSION_UUID  = (1 << 0),
    VZ_MIGRATION_COOKIE_DOMAIN_UUID = (1 << 1),
    VZ_MIGRATION_COOKIE_DOMAIN_NAME = (1 << 1),
};

2759 2760 2761
typedef struct _vzMigrationCookie vzMigrationCookie;
typedef vzMigrationCookie *vzMigrationCookiePtr;
struct _vzMigrationCookie {
2762 2763
    unsigned char *session_uuid;
    unsigned char *uuid;
2764 2765 2766 2767 2768 2769 2770 2771
    char *name;
};

static void
vzMigrationCookieFree(vzMigrationCookiePtr mig)
{
    if (!mig)
        return;
2772 2773 2774

    VIR_FREE(mig->session_uuid);
    VIR_FREE(mig->uuid);
2775 2776 2777 2778 2779
    VIR_FREE(mig->name);
    VIR_FREE(mig);
}

static int
2780 2781 2782 2783
vzBakeCookie(vzDriverPtr driver,
             virDomainObjPtr dom,
             char **cookieout, int *cookieoutlen,
             unsigned int flags)
2784 2785 2786 2787 2788 2789 2790 2791 2792 2793 2794 2795 2796 2797 2798
{
    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);
2799 2800 2801 2802 2803 2804 2805 2806 2807 2808 2809 2810 2811 2812 2813 2814 2815 2816 2817 2818 2819 2820

    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__");
    }

2821 2822 2823 2824 2825 2826 2827 2828 2829 2830 2831 2832 2833
    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
2834
vzEatCookie(const char *cookiein, int cookieinlen, unsigned int flags)
2835 2836 2837
{
    xmlDocPtr doc = NULL;
    xmlXPathContextPtr ctx = NULL;
2838
    char *tmp = NULL;
2839 2840 2841 2842 2843 2844 2845 2846 2847 2848 2849 2850 2851 2852 2853
    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;

2854 2855 2856 2857
    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))) {
2858
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
2859 2860
                       _("missing or malformed session-uuid element "
                         "in migration data"));
2861 2862 2863 2864 2865
        VIR_FREE(tmp);
        goto error;
    }
    VIR_FREE(tmp);

2866 2867 2868 2869
    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))) {
2870
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
2871
                       _("missing or malformed uuid element in migration data"));
2872 2873 2874 2875 2876
        VIR_FREE(tmp);
        goto error;
    }
    VIR_FREE(tmp);

2877 2878
    if ((flags & VZ_MIGRATION_COOKIE_DOMAIN_NAME)
        && !(mig->name = virXPathString("string(./name[1])", ctx))) {
2879 2880 2881 2882 2883 2884 2885 2886 2887 2888 2889 2890 2891 2892 2893 2894
        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;
}

2895 2896 2897 2898 2899
#define VZ_MIGRATION_FLAGS         (VIR_MIGRATE_PAUSED |          \
                                    VIR_MIGRATE_PEER2PEER |       \
                                    VIR_MIGRATE_LIVE |            \
                                    VIR_MIGRATE_UNDEFINE_SOURCE | \
                                    VIR_MIGRATE_PERSIST_DEST)
2900 2901 2902 2903 2904

#define VZ_MIGRATION_PARAMETERS                                 \
    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, \
2905
    VIR_MIGRATE_PARAM_BANDWIDTH,        VIR_TYPED_PARAM_ULLONG, \
2906 2907
    NULL

2908 2909 2910 2911 2912 2913 2914 2915 2916 2917 2918 2919 2920 2921 2922 2923 2924 2925 2926 2927 2928 2929 2930 2931 2932 2933 2934 2935
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);
}

2936 2937 2938 2939 2940 2941 2942 2943 2944 2945 2946
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;
2947
    unsigned long long bandwidth = 0;
2948 2949 2950 2951 2952 2953

    virCheckFlags(VZ_MIGRATION_FLAGS, NULL);

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

2954 2955 2956 2957 2958 2959 2960 2961 2962 2963
    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;
    }

2964 2965 2966
    if (!(dom = vzDomObjFromDomain(domain)))
        goto cleanup;

2967 2968 2969
    if (virDomainMigrateBegin3ParamsEnsureACL(domain->conn, dom->def) < 0)
        goto cleanup;

2970 2971
    xml = vzDomainMigrateBeginStep(dom, privconn->driver, params, nparams,
                                   cookieout, cookieoutlen);
2972 2973 2974 2975 2976 2977 2978 2979 2980 2981 2982 2983 2984 2985 2986 2987 2988 2989 2990 2991 2992 2993 2994 2995 2996 2997 2998 2999 3000 3001 3002 3003 3004 3005 3006 3007

 cleanup:

    if (dom)
        virObjectUnlock(dom);
    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,
3008 3009
                              const char *cookiein ATTRIBUTE_UNUSED,
                              int cookieinlen ATTRIBUTE_UNUSED,
3010 3011 3012 3013 3014 3015
                              char **cookieout,
                              int *cookieoutlen,
                              char **uri_out,
                              unsigned int flags)
{
    vzConnPtr privconn = conn->privateData;
3016
    vzDriverPtr driver = privconn->driver;
3017 3018
    const char *miguri = NULL;
    const char *dname = NULL;
3019 3020
    const char *dom_xml = NULL;
    virDomainDefPtr def = NULL;
3021 3022 3023 3024 3025 3026 3027 3028 3029
    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 ||
3030 3031
        virTypedParamsGetString(params, nparams,
                                VIR_MIGRATE_PARAM_DEST_XML, &dom_xml) < 0 ||
3032 3033 3034 3035 3036 3037 3038 3039 3040
        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;

3041 3042 3043 3044 3045 3046
    /* 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)
3047 3048
        goto cleanup;

3049
    if (!(def = virDomainDefParseString(dom_xml, driver->caps, driver->xmlopt,
3050
                                        NULL,
3051 3052 3053 3054 3055 3056 3057 3058 3059 3060 3061 3062
                                        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;

3063 3064 3065
    ret = 0;

 cleanup:
3066
    virDomainDefFree(def);
3067 3068 3069 3070 3071 3072
    return ret;
}

static int
vzConnectSupportsFeature(virConnectPtr conn ATTRIBUTE_UNUSED, int feature)
{
3073 3074 3075
    if (virConnectSupportsFeatureEnsureACL(conn) < 0)
        return -1;

3076 3077
    switch (feature) {
    case VIR_DRV_FEATURE_MIGRATION_PARAMS:
3078
    case VIR_DRV_FEATURE_MIGRATION_P2P:
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 3107 3108 3109 3110 3111 3112 3113 3114 3115 3116 3117 3118 3119 3120 3121
        return 1;
    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
3122 3123
vzDomainMigratePerformStep(virDomainObjPtr dom,
                           vzDriverPtr driver,
3124 3125 3126 3127 3128
                           virTypedParameterPtr params,
                           int nparams,
                           const char *cookiein,
                           int cookieinlen,
                           unsigned int flags)
3129 3130
{
    int ret = -1;
3131
    vzDomObjPtr privdom = dom->privateData;
3132 3133 3134 3135
    virURIPtr vzuri = NULL;
    const char *miguri = NULL;
    const char *dname = NULL;
    vzMigrationCookiePtr mig = NULL;
3136
    bool job = false;
3137 3138 3139 3140 3141 3142 3143 3144 3145 3146 3147 3148 3149

    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;
    }

3150 3151
    if (!(mig = vzEatCookie(cookiein, cookieinlen,
                            VZ_MIGRATION_COOKIE_SESSION_UUID)))
3152 3153
        goto cleanup;

3154 3155 3156
    if (vzDomainObjBeginJob(dom) < 0)
        goto cleanup;
    job = true;
3157
    privdom->job.hasProgress = true;
3158

3159 3160 3161
    if (vzEnsureDomainExists(dom) < 0)
        goto cleanup;

3162 3163 3164 3165 3166 3167
    if (!(vzuri = vzParseVzURI(miguri)))
        goto cleanup;

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

3168
    virDomainObjListRemove(driver->domains, dom);
3169
    virObjectLock(dom);
3170 3171 3172 3173

    ret = 0;

 cleanup:
3174 3175
    if (job)
        vzDomainObjEndJob(dom);
3176 3177 3178 3179 3180 3181
    virURIFree(vzuri);
    vzMigrationCookieFree(mig);

    return ret;
}

3182
static int
3183 3184
vzDomainMigratePerformP2P(virDomainObjPtr dom,
                          vzDriverPtr driver,
3185 3186 3187 3188 3189 3190 3191 3192 3193 3194 3195 3196 3197 3198 3199 3200 3201 3202 3203 3204 3205 3206 3207 3208
                          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;

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

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

3209 3210
    if (!(dom_xml = vzDomainMigrateBeginStep(dom, driver, params, nparams,
                                             &cookieout, &cookieoutlen)))
3211 3212 3213 3214 3215 3216
        goto done;

    cookiein = cookieout;
    cookieinlen = cookieoutlen;
    cookieout = NULL;
    cookieoutlen = 0;
3217 3218
    virObjectUnlock(dom);
    ret = dconn->driver->domainMigratePrepare3Params
3219
            (dconn, params, nparams, cookiein, cookieinlen,
3220 3221 3222
             &cookieout, &cookieoutlen, &uri, flags);
    virObjectLock(dom);
    if (ret < 0)
3223
        goto done;
3224
    ret = -1;
3225 3226 3227 3228 3229 3230 3231 3232 3233 3234 3235 3236 3237 3238 3239

    /* 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);
    cookiein = cookieout;
    cookieinlen = cookieoutlen;
    cookieout = NULL;
    cookieoutlen = 0;
3240
    if (vzDomainMigratePerformStep(dom, driver, params, nparams, cookiein,
3241 3242 3243 3244 3245 3246 3247 3248 3249 3250 3251 3252
                                   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,
3253
                                    dom->def->name) < 0)
3254 3255
        goto done;

3256
    virObjectUnlock(dom);
3257 3258 3259
    ddomain = dconn->driver->domainMigrateFinish3Params(dconn, params, nparams,
                                                        NULL, 0, NULL, NULL,
                                                        flags, cancelled);
3260
    virObjectLock(dom);
3261 3262 3263 3264 3265 3266 3267 3268 3269 3270 3271 3272 3273 3274 3275 3276 3277 3278 3279 3280 3281 3282 3283 3284 3285 3286 3287 3288 3289 3290 3291
    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)
{
3292
    int ret = -1;
3293 3294 3295
    virDomainObjPtr dom;
    vzConnPtr privconn = domain->conn->privateData;

3296 3297 3298 3299 3300
    virCheckFlags(VZ_MIGRATION_FLAGS, -1);

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

3301 3302 3303
    if (!(dom = vzDomObjFromDomainRef(domain)))
        return -1;

3304 3305 3306
    if (virDomainMigratePerform3ParamsEnsureACL(domain->conn, dom->def) < 0)
        goto cleanup;

3307
    if (flags & VIR_MIGRATE_PEER2PEER)
3308 3309
        ret = vzDomainMigratePerformP2P(dom, privconn->driver, dconnuri,
                                        params, nparams, flags);
3310
    else
3311 3312
        ret = vzDomainMigratePerformStep(dom, privconn->driver, params, nparams,
                                         cookiein, cookieinlen, flags);
3313

3314
 cleanup:
3315 3316 3317
    virDomainObjEndAPI(&dom);

    return ret;
3318 3319
}

3320 3321 3322 3323 3324 3325 3326 3327 3328 3329 3330 3331 3332 3333 3334 3335 3336 3337 3338 3339
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)
3340 3341 3342 3343
        return NULL;

    if (cancelled)
        return NULL;
3344 3345 3346

    if (virTypedParamsGetString(params, nparams,
                                VIR_MIGRATE_PARAM_DEST_NAME, &name) < 0)
3347
        return NULL;
3348 3349


3350
    if (!(dom = prlsdkAddDomainByName(driver, name)))
3351 3352
        goto cleanup;

3353 3354 3355 3356 3357
    /* 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;

3358 3359 3360 3361 3362 3363 3364
    domain = virGetDomain(dconn, dom->def->name, dom->def->uuid);
    if (domain)
        domain->id = dom->def->id;

 cleanup:
    /* In this situation we have to restore domain on source. But the migration
     * is already finished. */
3365
    if (!domain)
3366
        VIR_WARN("Can't provide domain '%s' after successfull migration.", name);
3367
    virDomainObjEndAPI(&dom);
3368 3369 3370 3371 3372 3373 3374 3375 3376 3377 3378 3379 3380 3381 3382 3383 3384 3385 3386 3387
    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;
}

3388 3389 3390 3391 3392 3393 3394 3395 3396 3397 3398 3399 3400 3401 3402 3403 3404 3405 3406 3407 3408 3409 3410 3411 3412 3413 3414
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;
3415
    int ret = -1;
3416 3417 3418 3419

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

3420 3421 3422
    if (virDomainGetJobInfoEnsureACL(domain->conn, dom->def) < 0)
        goto cleanup;

3423 3424
    ret = vzDomainGetJobInfoImpl(dom, info);

3425
 cleanup:
3426 3427 3428 3429
    virObjectUnlock(dom);
    return ret;
}

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 3466 3467 3468 3469 3470 3471 3472 3473 3474 3475 3476 3477 3478 3479 3480
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;

3481 3482 3483
    if (virDomainGetJobStatsEnsureACL(domain->conn, dom->def) < 0)
        goto cleanup;

3484 3485 3486 3487 3488 3489 3490 3491 3492 3493 3494 3495 3496 3497 3498 3499 3500 3501 3502
    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:
    virObjectUnlock(dom);

    return ret;
}

3503
static virHypervisorDriver vzHypervisorDriver = {
3504
    .name = "vz",
3505 3506 3507 3508
    .connectOpen = vzConnectOpen,            /* 0.10.0 */
    .connectClose = vzConnectClose,          /* 0.10.0 */
    .connectGetVersion = vzConnectGetVersion,   /* 0.10.0 */
    .connectGetHostname = vzConnectGetHostname,      /* 0.10.0 */
3509
    .connectGetSysinfo = vzConnectGetSysinfo, /* 1.3.4 */
3510
    .connectGetMaxVcpus = vzConnectGetMaxVcpus, /* 1.2.21 */
3511
    .nodeGetInfo = vzNodeGetInfo,      /* 0.10.0 */
3512 3513 3514 3515
    .nodeGetCPUStats = vzNodeGetCPUStats,      /* 1.2.21 */
    .nodeGetMemoryStats = vzNodeGetMemoryStats, /* 1.2.21 */
    .nodeGetCellsFreeMemory = vzNodeGetCellsFreeMemory, /* 1.2.21 */
    .nodeGetFreeMemory = vzNodeGetFreeMemory, /* 1.2.21 */
3516 3517 3518 3519 3520 3521 3522 3523 3524 3525 3526 3527 3528 3529 3530 3531 3532 3533 3534 3535
    .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 */
3536
    .domainDestroyFlags = vzDomainDestroyFlags,  /* 2.2.0 */
3537
    .domainShutdown = vzDomainShutdown, /* 0.10.0 */
3538
    .domainShutdownFlags = vzDomainShutdownFlags, /* 2.2.0 */
3539 3540
    .domainCreate = vzDomainCreate,    /* 0.10.0 */
    .domainCreateWithFlags = vzDomainCreateWithFlags, /* 1.2.10 */
P
Pavel Hrdina 已提交
3541
    .domainReboot = vzDomainReboot, /* 1.3.0 */
3542 3543 3544 3545 3546 3547 3548 3549 3550
    .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 */
3551 3552 3553
    .domainIsUpdated = vzDomainIsUpdated,     /* 1.2.21 */
    .domainGetVcpusFlags = vzDomainGetVcpusFlags, /* 1.2.21 */
    .domainGetMaxVcpus = vzDomainGetMaxVcpus, /* 1.2.21 */
3554
    .domainSetUserPassword = vzDomainSetUserPassword, /* 2.0.0 */
3555 3556 3557 3558 3559 3560 3561 3562 3563 3564
    .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 */
3565 3566 3567 3568
    .domainBlockStats = vzDomainBlockStats, /* 1.2.17 */
    .domainBlockStatsFlags = vzDomainBlockStatsFlags, /* 1.2.17 */
    .domainInterfaceStats = vzDomainInterfaceStats, /* 1.2.17 */
    .domainMemoryStats = vzDomainMemoryStats, /* 1.2.17 */
3569 3570
    .connectRegisterCloseCallback = vzConnectRegisterCloseCallback, /* 1.3.2 */
    .connectUnregisterCloseCallback = vzConnectUnregisterCloseCallback, /* 1.3.2 */
3571 3572
    .domainSetMemoryFlags = vzDomainSetMemoryFlags, /* 1.3.4 */
    .domainSetMemory = vzDomainSetMemory, /* 1.3.4 */
3573 3574 3575 3576 3577 3578 3579 3580 3581 3582 3583 3584 3585 3586 3587 3588
    .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 */
3589 3590 3591 3592 3593 3594
    .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 已提交
3595
    .domainUpdateDeviceFlags = vzDomainUpdateDeviceFlags, /* 2.0.0 */
3596
    .domainGetJobInfo = vzDomainGetJobInfo, /* 2.2.0 */
3597
    .domainGetJobStats = vzDomainGetJobStats, /* 2.2.0 */
D
Dmitry Guryanov 已提交
3598 3599
};

3600
static virConnectDriver vzConnectDriver = {
3601
    .hypervisorDriver = &vzHypervisorDriver,
3602 3603
};

3604 3605 3606 3607 3608 3609 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
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,
};

3643
/* Parallels domain type backward compatibility*/
3644
static virHypervisorDriver parallelsHypervisorDriver;
3645 3646
static virConnectDriver parallelsConnectDriver;

D
Dmitry Guryanov 已提交
3647
/**
3648
 * vzRegister:
D
Dmitry Guryanov 已提交
3649
 *
3650
 * Registers the vz driver
D
Dmitry Guryanov 已提交
3651 3652
 */
int
3653
vzRegister(void)
D
Dmitry Guryanov 已提交
3654 3655 3656 3657 3658 3659 3660 3661 3662 3663 3664
{
    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);

3665
    /* Backward compatibility with Parallels domain type */
3666 3667
    parallelsHypervisorDriver = vzHypervisorDriver;
    parallelsHypervisorDriver.name = "Parallels";
3668
    parallelsConnectDriver = vzConnectDriver;
3669
    parallelsConnectDriver.hypervisorDriver = &parallelsHypervisorDriver;
3670
    if (virRegisterConnectDriver(&parallelsConnectDriver, true) < 0)
D
Dmitry Guryanov 已提交
3671
        return -1;
D
Dmitry Guryanov 已提交
3672

3673
    if (virRegisterConnectDriver(&vzConnectDriver, true) < 0)
3674 3675
        return -1;

3676 3677 3678
    if (virRegisterStateDriver(&vzStateDriver) < 0)
        return -1;

D
Dmitry Guryanov 已提交
3679 3680
    return 0;
}