datatypes.c 47.6 KB
Newer Older
1 2 3
/*
 * datatypes.h: management of structs for public data types
 *
4
 * Copyright (C) 2006-2011 Red Hat, Inc.
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
 *
 * 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
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307  USA
 *
 */

#include <config.h>
D
Daniel P. Berrange 已提交
23
#include <unistd.h>
24 25 26

#include "datatypes.h"
#include "virterror_internal.h"
27
#include "logging.h"
28
#include "memory.h"
29
#include "uuid.h"
C
Chris Lalancette 已提交
30
#include "util.h"
31

32 33
#define VIR_FROM_THIS VIR_FROM_NONE

34 35
#define virLibConnError(code, ...)                                \
    virReportErrorHelper(NULL, VIR_FROM_THIS, code, __FILE__,     \
36
                         __FUNCTION__, __LINE__, __VA_ARGS__)
37

38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54
/************************************************************************
 *									*
 *			Domain and Connections allocations		*
 *									*
 ************************************************************************/

/**
 * virDomainFreeName:
 * @domain: a domain object
 *
 * Destroy the domain object, this is just used by the domain hash callback.
 *
 * Returns 0 in case of success and -1 in case of failure.
 */
static int
virDomainFreeName(virDomainPtr domain, const char *name ATTRIBUTE_UNUSED)
{
55
    return (virUnrefDomain(domain));
56 57 58 59 60 61 62 63 64 65 66 67 68
}

/**
 * virNetworkFreeName:
 * @network: a network object
 *
 * Destroy the network object, this is just used by the network hash callback.
 *
 * Returns 0 in case of success and -1 in case of failure.
 */
static int
virNetworkFreeName(virNetworkPtr network, const char *name ATTRIBUTE_UNUSED)
{
69
    return (virUnrefNetwork(network));
70 71
}

D
Daniel Veillard 已提交
72 73
/**
 * virInterfaceFreeName:
D
Dan Kenigsberg 已提交
74
 * @interface: an interface object
D
Daniel Veillard 已提交
75 76 77 78 79 80
 *
 * Destroy the interface object, this is just used by the interface hash callback.
 *
 * Returns 0 in case of success and -1 in case of failure.
 */
static int
81
virInterfaceFreeName(virInterfacePtr iface, const char *name ATTRIBUTE_UNUSED)
D
Daniel Veillard 已提交
82
{
83
    return (virUnrefInterface(iface));
D
Daniel Veillard 已提交
84 85
}

86 87 88 89 90 91 92 93 94 95 96
/**
 * virStoragePoolFreeName:
 * @pool: a pool object
 *
 * Destroy the pool object, this is just used by the pool hash callback.
 *
 * Returns 0 in case of success and -1 in case of failure.
 */
static int
virStoragePoolFreeName(virStoragePoolPtr pool, const char *name ATTRIBUTE_UNUSED)
{
97
    return (virUnrefStoragePool(pool));
98 99 100 101 102 103 104 105 106 107 108 109 110
}

/**
 * virStorageVolFreeName:
 * @vol: a vol object
 *
 * Destroy the vol object, this is just used by the vol hash callback.
 *
 * Returns 0 in case of success and -1 in case of failure.
 */
static int
virStorageVolFreeName(virStorageVolPtr vol, const char *name ATTRIBUTE_UNUSED)
{
111
    return (virUnrefStorageVol(vol));
112 113
}

114 115
/**
 * virSecretFreeName:
116
 * @secret: a secret object
117 118 119 120 121
 *
 * Destroy the secret object, this is just used by the secret hash callback.
 *
 * Returns 0 in case of success and -1 in case of failure.
 */
122 123
static int
virSecretFreeName(virSecretPtr secret, const char *name ATTRIBUTE_UNUSED)
124
{
125
    return virUnrefSecret(secret);
126 127
}

128
/**
129 130
 * virNWFilterFreeName:
 * @nwfilter: a nwfilter object
131
 *
132
 * Destroy the nwfilter object, this is just used by the nwfilter hash callback.
133 134 135 136
 *
 * Returns 0 in case of success and -1 in case of failure.
 */
static int
137
virNWFilterFreeName(virNWFilterPtr nwfilter, const char *name ATTRIBUTE_UNUSED)
138
{
139
    return virUnrefNWFilter(nwfilter);
140 141
}

C
Chris Lalancette 已提交
142 143 144 145 146 147 148 149 150 151 152 153 154 155
/**
 * virDomainSnapshotFreeName:
 * @snapshot: a domain snapshotobject
 *
 * Destroy the domain snapshot object, this is just used by the domain hash callback.
 *
 * Returns 0 in case of success and -1 in case of failure.
 */
static int
virDomainSnapshotFreeName(virDomainSnapshotPtr snapshot, const char *name ATTRIBUTE_UNUSED)
{
    return (virUnrefDomainSnapshot(snapshot));
}

156 157 158 159 160 161 162 163 164 165 166 167
/**
 * virGetConnect:
 *
 * Allocates a new hypervisor connection structure
 *
 * Returns a new pointer or NULL in case of error.
 */
virConnectPtr
virGetConnect(void) {
    virConnectPtr ret;

    if (VIR_ALLOC(ret) < 0) {
168
        virReportOOMError();
169 170
        goto failed;
    }
171 172 173 174 175
    if (virMutexInit(&ret->lock) < 0) {
        VIR_FREE(ret);
        goto failed;
    }

176 177 178 179 180
    ret->magic = VIR_CONNECT_MAGIC;
    ret->driver = NULL;
    ret->networkDriver = NULL;
    ret->privateData = NULL;
    ret->networkPrivateData = NULL;
D
Daniel Veillard 已提交
181
    ret->interfacePrivateData = NULL;
182 183 184 185 186 187
    ret->domains = virHashCreate(20);
    if (ret->domains == NULL)
        goto failed;
    ret->networks = virHashCreate(20);
    if (ret->networks == NULL)
        goto failed;
D
Daniel Veillard 已提交
188 189 190
    ret->interfaces = virHashCreate(20);
    if (ret->interfaces == NULL)
        goto failed;
191 192 193 194 195 196
    ret->storagePools = virHashCreate(20);
    if (ret->storagePools == NULL)
        goto failed;
    ret->storageVols = virHashCreate(20);
    if (ret->storageVols == NULL)
        goto failed;
197 198 199
    ret->nodeDevices = virHashCreate(256);
    if (ret->nodeDevices == NULL)
        goto failed;
200 201 202
    ret->secrets = virHashCreate(20);
    if (ret->secrets == NULL)
        goto failed;
203 204
    ret->nwfilters = virHashCreate(20);
    if (ret->nwfilters == NULL)
S
Stefan Berger 已提交
205
        goto failed;
206 207 208 209 210 211 212 213 214 215

    ret->refs = 1;
    return(ret);

failed:
    if (ret != NULL) {
        if (ret->domains != NULL)
            virHashFree(ret->domains, (virHashDeallocator) virDomainFreeName);
        if (ret->networks != NULL)
            virHashFree(ret->networks, (virHashDeallocator) virNetworkFreeName);
D
Daniel Veillard 已提交
216 217
        if (ret->interfaces != NULL)
           virHashFree(ret->interfaces, (virHashDeallocator) virInterfaceFreeName);
218 219 220 221
        if (ret->storagePools != NULL)
            virHashFree(ret->storagePools, (virHashDeallocator) virStoragePoolFreeName);
        if (ret->storageVols != NULL)
            virHashFree(ret->storageVols, (virHashDeallocator) virStorageVolFreeName);
222 223
        if (ret->nodeDevices != NULL)
            virHashFree(ret->nodeDevices, (virHashDeallocator) virNodeDeviceFree);
224
        if (ret->secrets != NULL)
225
            virHashFree(ret->secrets, (virHashDeallocator) virSecretFreeName);
226 227
        if (ret->nwfilters != NULL)
            virHashFree(ret->nwfilters, (virHashDeallocator) virNWFilterFreeName);
228

229
        virMutexDestroy(&ret->lock);
230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245
        VIR_FREE(ret);
    }
    return(NULL);
}

/**
 * virReleaseConnect:
 * @conn: the hypervisor connection to release
 *
 * Unconditionally release all memory associated with a connection.
 * The conn.lock mutex must be held prior to calling this, and will
 * be released prior to this returning. The connection obj must not
 * be used once this method returns.
 */
static void
virReleaseConnect(virConnectPtr conn) {
246
    DEBUG("release connection %p", conn);
247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269

    /* make sure to release the connection lock before we call the
     * close() callbacks, otherwise we will deadlock if an error
     * is raised by any of the callbacks */
    virMutexUnlock(&conn->lock);

    if (conn->networkDriver)
        conn->networkDriver->close (conn);
    if (conn->interfaceDriver)
        conn->interfaceDriver->close (conn);
    if (conn->storageDriver)
        conn->storageDriver->close (conn);
    if (conn->deviceMonitor)
        conn->deviceMonitor->close (conn);
    if (conn->secretDriver)
        conn->secretDriver->close (conn);
    if (conn->nwfilterDriver)
        conn->nwfilterDriver->close (conn);
    if (conn->driver)
        conn->driver->close (conn);

    virMutexLock(&conn->lock);

270 271 272 273
    if (conn->domains != NULL)
        virHashFree(conn->domains, (virHashDeallocator) virDomainFreeName);
    if (conn->networks != NULL)
        virHashFree(conn->networks, (virHashDeallocator) virNetworkFreeName);
D
Daniel Veillard 已提交
274 275
    if (conn->interfaces != NULL)
        virHashFree(conn->interfaces, (virHashDeallocator) virInterfaceFreeName);
276 277 278 279
    if (conn->storagePools != NULL)
        virHashFree(conn->storagePools, (virHashDeallocator) virStoragePoolFreeName);
    if (conn->storageVols != NULL)
        virHashFree(conn->storageVols, (virHashDeallocator) virStorageVolFreeName);
280 281
    if (conn->nodeDevices != NULL)
        virHashFree(conn->nodeDevices, (virHashDeallocator) virNodeDeviceFree);
282
    if (conn->secrets != NULL)
283
        virHashFree(conn->secrets, (virHashDeallocator) virSecretFreeName);
284 285
    if (conn->nwfilters != NULL)
        virHashFree(conn->nwfilters, (virHashDeallocator) virNWFilterFreeName);
286 287 288

    virResetError(&conn->err);

289
    xmlFreeURI(conn->uri);
290

291 292
    virMutexUnlock(&conn->lock);
    virMutexDestroy(&conn->lock);
293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309
    VIR_FREE(conn);
}

/**
 * virUnrefConnect:
 * @conn: the hypervisor connection to unreference
 *
 * Unreference the connection. If the use count drops to zero, the structure is
 * actually freed.
 *
 * Returns the reference count or -1 in case of failure.
 */
int
virUnrefConnect(virConnectPtr conn) {
    int refs;

    if ((!VIR_IS_CONNECT(conn))) {
310 311
        virLibConnError(VIR_ERR_INVALID_ARG, _("no connection"));
        return -1;
312
    }
313
    virMutexLock(&conn->lock);
314
    DEBUG("unref connection %p %d", conn, conn->refs);
315 316 317 318 319 320 321
    conn->refs--;
    refs = conn->refs;
    if (refs == 0) {
        virReleaseConnect(conn);
        /* Already unlocked mutex */
        return (0);
    }
322
    virMutexUnlock(&conn->lock);
323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339
    return (refs);
}

/**
 * virGetDomain:
 * @conn: the hypervisor connection
 * @name: pointer to the domain name
 * @uuid: pointer to the uuid
 *
 * Lookup if the domain is already registered for that connection,
 * if yes return a new pointer to it, if no allocate a new structure,
 * and register it in the table. In any case a corresponding call to
 * virUnrefDomain() is needed to not leak data.
 *
 * Returns a pointer to the domain, or NULL in case of failure
 */
virDomainPtr
D
Daniel P. Berrange 已提交
340
virGetDomain(virConnectPtr conn, const char *name, const unsigned char *uuid) {
341
    virDomainPtr ret = NULL;
342
    char uuidstr[VIR_UUID_STRING_BUFLEN];
343

344 345 346 347 348 349 350 351 352 353 354
    if (!VIR_IS_CONNECT(conn)) {
        virLibConnError(VIR_ERR_INVALID_ARG, _("no connection"));
        return NULL;
    }
    if (name == NULL) {
        virLibConnError(VIR_ERR_INVALID_ARG, _("missing name"));
        return NULL;
    }
    if (uuid == NULL) {
        virLibConnError(VIR_ERR_INVALID_ARG, _("missing uuid"));
        return NULL;
355
    }
356
    virMutexLock(&conn->lock);
357

358
    virUUIDFormat(uuid, uuidstr);
359

360
    ret = (virDomainPtr) virHashLookup(conn->domains, uuidstr);
361 362
    if (ret == NULL) {
        if (VIR_ALLOC(ret) < 0) {
363
            virMutexUnlock(&conn->lock);
364
            virReportOOMError();
365 366 367 368
            goto error;
        }
        ret->name = strdup(name);
        if (ret->name == NULL) {
369
            virMutexUnlock(&conn->lock);
370
            virReportOOMError();
371 372 373 374 375
            goto error;
        }
        ret->magic = VIR_DOMAIN_MAGIC;
        ret->conn = conn;
        ret->id = -1;
376
        memcpy(&(ret->uuid[0]), uuid, VIR_UUID_BUFLEN);
C
Chris Lalancette 已提交
377
        ret->snapshots = virHashCreate(20);
378

379
        if (virHashAddEntry(conn->domains, uuidstr, ret) < 0) {
380
            virMutexUnlock(&conn->lock);
381 382
            virLibConnError(VIR_ERR_INTERNAL_ERROR, "%s",
                            _("failed to add domain to connection hash table"));
383 384 385 386 387 388 389 390
            goto error;
        }
        conn->refs++;
        DEBUG("New hash entry %p", ret);
    } else {
        DEBUG("Existing hash entry %p: refs now %d", ret, ret->refs+1);
    }
    ret->refs++;
391
    virMutexUnlock(&conn->lock);
392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416
    return(ret);

 error:
    if (ret != NULL) {
        VIR_FREE(ret->name);
        VIR_FREE(ret);
    }
    return(NULL);
}

/**
 * virReleaseDomain:
 * @domain: the domain to release
 *
 * Unconditionally release all memory associated with a domain.
 * The conn.lock mutex must be held prior to calling this, and will
 * be released prior to this returning. The domain obj must not
 * be used once this method returns.
 *
 * It will also unreference the associated connection object,
 * which may also be released if its ref count hits zero.
 */
static void
virReleaseDomain(virDomainPtr domain) {
    virConnectPtr conn = domain->conn;
417
    char uuidstr[VIR_UUID_STRING_BUFLEN];
418

419 420 421 422
    virUUIDFormat(domain->uuid, uuidstr);
    DEBUG("release domain %p %s %s", domain, domain->name, uuidstr);

    if (virHashRemoveEntry(conn->domains, uuidstr, NULL) < 0) {
423
        virMutexUnlock(&conn->lock);
424 425
        virLibConnError(VIR_ERR_INTERNAL_ERROR, "%s",
                        _("domain missing from connection hash table"));
426 427
        conn = NULL;
    }
428 429 430 431

    domain->magic = -1;
    domain->id = -1;
    VIR_FREE(domain->name);
C
Chris Lalancette 已提交
432 433
    if (domain->snapshots != NULL)
        virHashFree(domain->snapshots, (virHashDeallocator) virDomainSnapshotFreeName);
434 435
    VIR_FREE(domain);

436 437 438 439 440 441 442 443 444
    if (conn) {
        DEBUG("unref connection %p %d", conn, conn->refs);
        conn->refs--;
        if (conn->refs == 0) {
            virReleaseConnect(conn);
            /* Already unlocked mutex */
            return;
        }
        virMutexUnlock(&conn->lock);
445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462
    }
}


/**
 * virUnrefDomain:
 * @domain: the domain to unreference
 *
 * Unreference the domain. If the use count drops to zero, the structure is
 * actually freed.
 *
 * Returns the reference count or -1 in case of failure.
 */
int
virUnrefDomain(virDomainPtr domain) {
    int refs;

    if (!VIR_IS_CONNECTED_DOMAIN(domain)) {
463 464
        virLibConnError(VIR_ERR_INVALID_ARG, _("bad domain or no connection"));
        return -1;
465
    }
466
    virMutexLock(&domain->conn->lock);
467 468 469 470 471 472 473 474 475
    DEBUG("unref domain %p %s %d", domain, domain->name, domain->refs);
    domain->refs--;
    refs = domain->refs;
    if (refs == 0) {
        virReleaseDomain(domain);
        /* Already unlocked mutex */
        return (0);
    }

476
    virMutexUnlock(&domain->conn->lock);
477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493
    return (refs);
}

/**
 * virGetNetwork:
 * @conn: the hypervisor connection
 * @name: pointer to the network name
 * @uuid: pointer to the uuid
 *
 * Lookup if the network is already registered for that connection,
 * if yes return a new pointer to it, if no allocate a new structure,
 * and register it in the table. In any case a corresponding call to
 * virUnrefNetwork() is needed to not leak data.
 *
 * Returns a pointer to the network, or NULL in case of failure
 */
virNetworkPtr
D
Daniel P. Berrange 已提交
494
virGetNetwork(virConnectPtr conn, const char *name, const unsigned char *uuid) {
495
    virNetworkPtr ret = NULL;
496
    char uuidstr[VIR_UUID_STRING_BUFLEN];
497

498 499 500 501 502 503 504 505 506 507 508
    if (!VIR_IS_CONNECT(conn)) {
        virLibConnError(VIR_ERR_INVALID_ARG, _("no connection"));
        return NULL;
    }
    if (name == NULL) {
        virLibConnError(VIR_ERR_INVALID_ARG, _("missing name"));
        return NULL;
    }
    if (uuid == NULL) {
        virLibConnError(VIR_ERR_INVALID_ARG, _("missing uuid"));
        return NULL;
509
    }
510
    virMutexLock(&conn->lock);
511

512
    virUUIDFormat(uuid, uuidstr);
513

514
    ret = (virNetworkPtr) virHashLookup(conn->networks, uuidstr);
515 516
    if (ret == NULL) {
        if (VIR_ALLOC(ret) < 0) {
517
            virMutexUnlock(&conn->lock);
518
            virReportOOMError();
519 520 521 522
            goto error;
        }
        ret->name = strdup(name);
        if (ret->name == NULL) {
523
            virMutexUnlock(&conn->lock);
524
            virReportOOMError();
525 526 527 528
            goto error;
        }
        ret->magic = VIR_NETWORK_MAGIC;
        ret->conn = conn;
529
        memcpy(&(ret->uuid[0]), uuid, VIR_UUID_BUFLEN);
530

531
        if (virHashAddEntry(conn->networks, uuidstr, ret) < 0) {
532
            virMutexUnlock(&conn->lock);
533 534
            virLibConnError(VIR_ERR_INTERNAL_ERROR, "%s",
                            _("failed to add network to connection hash table"));
535 536 537 538 539
            goto error;
        }
        conn->refs++;
    }
    ret->refs++;
540
    virMutexUnlock(&conn->lock);
541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565
    return(ret);

 error:
    if (ret != NULL) {
        VIR_FREE(ret->name);
        VIR_FREE(ret);
    }
    return(NULL);
}

/**
 * virReleaseNetwork:
 * @network: the network to release
 *
 * Unconditionally release all memory associated with a network.
 * The conn.lock mutex must be held prior to calling this, and will
 * be released prior to this returning. The network obj must not
 * be used once this method returns.
 *
 * It will also unreference the associated connection object,
 * which may also be released if its ref count hits zero.
 */
static void
virReleaseNetwork(virNetworkPtr network) {
    virConnectPtr conn = network->conn;
566
    char uuidstr[VIR_UUID_STRING_BUFLEN];
567

568 569 570 571
    virUUIDFormat(network->uuid, uuidstr);
    DEBUG("release network %p %s %s", network, network->name, uuidstr);

    if (virHashRemoveEntry(conn->networks, uuidstr, NULL) < 0) {
572
        virMutexUnlock(&conn->lock);
573 574
        virLibConnError(VIR_ERR_INTERNAL_ERROR, "%s",
                        _("network missing from connection hash table"));
575 576
        conn = NULL;
    }
577 578 579 580 581

    network->magic = -1;
    VIR_FREE(network->name);
    VIR_FREE(network);

582 583 584 585 586 587 588 589 590
    if (conn) {
        DEBUG("unref connection %p %d", conn, conn->refs);
        conn->refs--;
        if (conn->refs == 0) {
            virReleaseConnect(conn);
            /* Already unlocked mutex */
            return;
        }
        virMutexUnlock(&conn->lock);
591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608
    }
}


/**
 * virUnrefNetwork:
 * @network: the network to unreference
 *
 * Unreference the network. If the use count drops to zero, the structure is
 * actually freed.
 *
 * Returns the reference count or -1 in case of failure.
 */
int
virUnrefNetwork(virNetworkPtr network) {
    int refs;

    if (!VIR_IS_CONNECTED_NETWORK(network)) {
609 610 611
        virLibConnError(VIR_ERR_INVALID_ARG,
                        _("bad network or no connection"));
        return -1;
612
    }
613
    virMutexLock(&network->conn->lock);
614 615 616 617 618 619 620 621 622
    DEBUG("unref network %p %s %d", network, network->name, network->refs);
    network->refs--;
    refs = network->refs;
    if (refs == 0) {
        virReleaseNetwork(network);
        /* Already unlocked mutex */
        return (0);
    }

623
    virMutexUnlock(&network->conn->lock);
624 625 626 627
    return (refs);
}


D
Daniel Veillard 已提交
628 629 630 631 632 633 634
/**
 * virGetInterface:
 * @conn: the hypervisor connection
 * @name: pointer to the interface name
 * @mac: pointer to the mac
 *
 * Lookup if the interface is already registered for that connection,
635 636 637 638
 * if yes return a new pointer to it (possibly updating the MAC
 * address), if no allocate a new structure, and register it in the
 * table. In any case a corresponding call to virUnrefInterface() is
 * needed to not leak data.
D
Daniel Veillard 已提交
639 640 641 642 643 644 645
 *
 * Returns a pointer to the interface, or NULL in case of failure
 */
virInterfacePtr
virGetInterface(virConnectPtr conn, const char *name, const char *mac) {
    virInterfacePtr ret = NULL;

646 647 648 649 650 651 652
    if (!VIR_IS_CONNECT(conn)) {
        virLibConnError(VIR_ERR_INVALID_ARG, _("no connection"));
        return NULL;
    }
    if (name == NULL) {
        virLibConnError(VIR_ERR_INVALID_ARG, _("missing name"));
        return NULL;
D
Daniel Veillard 已提交
653
    }
654 655 656 657 658

    /* a NULL mac from caller is okay. Treat it as blank */
    if (mac == NULL)
       mac = "";

D
Daniel Veillard 已提交
659 660 661
    virMutexLock(&conn->lock);

    ret = (virInterfacePtr) virHashLookup(conn->interfaces, name);
662

663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686
    if (ret != NULL) {
        if (STRCASENEQ(ret->mac, mac)) {
            /*
             * If the mac address has changed, try to modify it in
             * place, which will only work if the new mac is the
             * same length as, or shorter than, the old mac.
             */
            size_t newmaclen = strlen(mac);
            size_t oldmaclen = strlen(ret->mac);
            if (newmaclen <= oldmaclen) {
                strcpy(ret->mac, mac);
            } else {
                /*
                 * If it's longer, we're kind of screwed, because we
                 * can't add a new hashtable entry (it would clash
                 * with the existing entry of same name), and we can't
                 * free/re-alloc the existing entry's mac, as some
                 * other thread may already be using the existing mac
                 * pointer.  Fortunately, this should never happen,
                 * since the length of the mac address for any
                 * interface is determined by the type of the
                 * interface, and that is unlikely to change.
                 */
                virMutexUnlock(&conn->lock);
687 688 689
                virLibConnError(VIR_ERR_INTERNAL_ERROR,
                                _("Failed to change interface mac address "
                                  "from %s to %s due to differing lengths."),
690 691 692 693 694 695
                                ret->mac, mac);
                ret = NULL;
                goto error;
            }
        }
    } else {
D
Daniel Veillard 已提交
696
        if (VIR_ALLOC(ret) < 0) {
697
            virMutexUnlock(&conn->lock);
698
            virReportOOMError();
D
Daniel Veillard 已提交
699 700 701 702
            goto error;
        }
        ret->name = strdup(name);
        if (ret->name == NULL) {
703
            virMutexUnlock(&conn->lock);
704
            virReportOOMError();
D
Daniel Veillard 已提交
705 706 707 708
            goto error;
        }
        ret->mac = strdup(mac);
        if (ret->mac == NULL) {
709
            virMutexUnlock(&conn->lock);
710
            virReportOOMError();
D
Daniel Veillard 已提交
711 712 713 714 715 716 717
            goto error;
        }

        ret->magic = VIR_INTERFACE_MAGIC;
        ret->conn = conn;

        if (virHashAddEntry(conn->interfaces, name, ret) < 0) {
718
            virMutexUnlock(&conn->lock);
719 720
            virLibConnError(VIR_ERR_INTERNAL_ERROR, "%s",
                            _("failed to add interface to connection hash table"));
D
Daniel Veillard 已提交
721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741
            goto error;
        }
        conn->refs++;
    }
    ret->refs++;
    virMutexUnlock(&conn->lock);
    return(ret);

 error:
    if (ret != NULL) {
        VIR_FREE(ret->name);
        VIR_FREE(ret->mac);
        VIR_FREE(ret);
    }
    return(NULL);
}

/**
 * virReleaseInterface:
 * @interface: the interface to release
 *
D
Dan Kenigsberg 已提交
742
 * Unconditionally release all memory associated with an interface.
D
Daniel Veillard 已提交
743 744 745 746 747 748 749 750
 * The conn.lock mutex must be held prior to calling this, and will
 * be released prior to this returning. The interface obj must not
 * be used once this method returns.
 *
 * It will also unreference the associated connection object,
 * which may also be released if its ref count hits zero.
 */
static void
751 752 753
virReleaseInterface(virInterfacePtr iface) {
    virConnectPtr conn = iface->conn;
    DEBUG("release interface %p %s", iface, iface->name);
D
Daniel Veillard 已提交
754

755 756 757
    if (virHashRemoveEntry(conn->interfaces, iface->name, NULL) < 0) {
        /* unlock before reporting error because error report grabs lock */
        virMutexUnlock(&conn->lock);
758 759
        virLibConnError(VIR_ERR_INTERNAL_ERROR, "%s",
                        _("interface missing from connection hash table"));
760 761 762
        /* don't decr the conn refct if we weren't connected to it */
        conn = NULL;
    }
D
Daniel Veillard 已提交
763

764 765 766 767
    iface->magic = -1;
    VIR_FREE(iface->name);
    VIR_FREE(iface->mac);
    VIR_FREE(iface);
D
Daniel Veillard 已提交
768

769 770 771 772 773 774 775 776 777
    if (conn) {
        DEBUG("unref connection %p %d", conn, conn->refs);
        conn->refs--;
        if (conn->refs == 0) {
            virReleaseConnect(conn);
            /* Already unlocked mutex */
            return;
        }
        virMutexUnlock(&conn->lock);
D
Daniel Veillard 已提交
778 779 780 781 782 783 784 785 786 787 788 789 790 791
    }
}


/**
 * virUnrefInterface:
 * @interface: the interface to unreference
 *
 * Unreference the interface. If the use count drops to zero, the structure is
 * actually freed.
 *
 * Returns the reference count or -1 in case of failure.
 */
int
792
virUnrefInterface(virInterfacePtr iface) {
D
Daniel Veillard 已提交
793 794
    int refs;

795
    if (!VIR_IS_CONNECTED_INTERFACE(iface)) {
796 797 798
        virLibConnError(VIR_ERR_INVALID_ARG,
                        _("bad interface or no connection"));
        return -1;
D
Daniel Veillard 已提交
799
    }
800 801 802 803
    virMutexLock(&iface->conn->lock);
    DEBUG("unref interface %p %s %d", iface, iface->name, iface->refs);
    iface->refs--;
    refs = iface->refs;
D
Daniel Veillard 已提交
804
    if (refs == 0) {
805
        virReleaseInterface(iface);
D
Daniel Veillard 已提交
806 807 808 809
        /* Already unlocked mutex */
        return (0);
    }

810
    virMutexUnlock(&iface->conn->lock);
D
Daniel Veillard 已提交
811 812 813 814
    return (refs);
}


815 816 817 818 819 820 821 822 823
/**
 * virGetStoragePool:
 * @conn: the hypervisor connection
 * @name: pointer to the storage pool name
 * @uuid: pointer to the uuid
 *
 * Lookup if the storage pool is already registered for that connection,
 * if yes return a new pointer to it, if no allocate a new structure,
 * and register it in the table. In any case a corresponding call to
824
 * virUnrefStoragePool() is needed to not leak data.
825 826 827 828
 *
 * Returns a pointer to the network, or NULL in case of failure
 */
virStoragePoolPtr
829 830
virGetStoragePool(virConnectPtr conn, const char *name,
                  const unsigned char *uuid) {
831
    virStoragePoolPtr ret = NULL;
832
    char uuidstr[VIR_UUID_STRING_BUFLEN];
833

834 835 836 837 838 839 840 841 842 843 844
    if (!VIR_IS_CONNECT(conn)) {
        virLibConnError(VIR_ERR_INVALID_ARG, _("no connection"));
        return NULL;
    }
    if (name == NULL) {
        virLibConnError(VIR_ERR_INVALID_ARG, _("missing name"));
        return NULL;
    }
    if (uuid == NULL) {
        virLibConnError(VIR_ERR_INVALID_ARG, _("missing uuid"));
        return NULL;
845
    }
846
    virMutexLock(&conn->lock);
847

848
    virUUIDFormat(uuid, uuidstr);
849

850
    ret = (virStoragePoolPtr) virHashLookup(conn->storagePools, uuidstr);
851 852
    if (ret == NULL) {
        if (VIR_ALLOC(ret) < 0) {
853
            virMutexUnlock(&conn->lock);
854
            virReportOOMError();
855 856 857 858
            goto error;
        }
        ret->name = strdup(name);
        if (ret->name == NULL) {
859
            virMutexUnlock(&conn->lock);
860
            virReportOOMError();
861 862 863 864
            goto error;
        }
        ret->magic = VIR_STORAGE_POOL_MAGIC;
        ret->conn = conn;
865
        memcpy(&(ret->uuid[0]), uuid, VIR_UUID_BUFLEN);
866

867
        if (virHashAddEntry(conn->storagePools, uuidstr, ret) < 0) {
868
            virMutexUnlock(&conn->lock);
869
            virLibConnError(VIR_ERR_INTERNAL_ERROR,
870
                            "%s", _("failed to add storage pool to connection hash table"));
871 872 873 874 875
            goto error;
        }
        conn->refs++;
    }
    ret->refs++;
876
    virMutexUnlock(&conn->lock);
877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902
    return(ret);

error:
    if (ret != NULL) {
        VIR_FREE(ret->name);
        VIR_FREE(ret);
    }
    return(NULL);
}


/**
 * virReleaseStoragePool:
 * @pool: the pool to release
 *
 * Unconditionally release all memory associated with a pool.
 * The conn.lock mutex must be held prior to calling this, and will
 * be released prior to this returning. The pool obj must not
 * be used once this method returns.
 *
 * It will also unreference the associated connection object,
 * which may also be released if its ref count hits zero.
 */
static void
virReleaseStoragePool(virStoragePoolPtr pool) {
    virConnectPtr conn = pool->conn;
903
    char uuidstr[VIR_UUID_STRING_BUFLEN];
904

905 906 907 908
    virUUIDFormat(pool->uuid, uuidstr);
    DEBUG("release pool %p %s %s", pool, pool->name, uuidstr);

    if (virHashRemoveEntry(conn->storagePools, uuidstr, NULL) < 0) {
909
        virMutexUnlock(&conn->lock);
910 911
        virLibConnError(VIR_ERR_INTERNAL_ERROR, "%s",
                        _("pool missing from connection hash table"));
912 913
        conn = NULL;
    }
914 915 916 917 918

    pool->magic = -1;
    VIR_FREE(pool->name);
    VIR_FREE(pool);

919 920 921 922 923 924 925 926 927
    if (conn) {
        DEBUG("unref connection %p %d", conn, conn->refs);
        conn->refs--;
        if (conn->refs == 0) {
            virReleaseConnect(conn);
            /* Already unlocked mutex */
            return;
        }
        virMutexUnlock(&conn->lock);
928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945
    }
}


/**
 * virUnrefStoragePool:
 * @pool: the pool to unreference
 *
 * Unreference the pool. If the use count drops to zero, the structure is
 * actually freed.
 *
 * Returns the reference count or -1 in case of failure.
 */
int
virUnrefStoragePool(virStoragePoolPtr pool) {
    int refs;

    if (!VIR_IS_CONNECTED_STORAGE_POOL(pool)) {
946 947 948
        virLibConnError(VIR_ERR_INVALID_ARG,
                        _("bad storage pool or no connection"));
        return -1;
949
    }
950
    virMutexLock(&pool->conn->lock);
951 952 953 954 955 956 957 958 959
    DEBUG("unref pool %p %s %d", pool, pool->name, pool->refs);
    pool->refs--;
    refs = pool->refs;
    if (refs == 0) {
        virReleaseStoragePool(pool);
        /* Already unlocked mutex */
        return (0);
    }

960
    virMutexUnlock(&pool->conn->lock);
961 962 963 964 965 966 967 968 969
    return (refs);
}


/**
 * virGetStorageVol:
 * @conn: the hypervisor connection
 * @pool: pool owning the volume
 * @name: pointer to the storage vol name
J
Jiri Denemark 已提交
970
 * @key: pointer to unique key of the volume
971 972 973 974
 *
 * Lookup if the storage vol is already registered for that connection,
 * if yes return a new pointer to it, if no allocate a new structure,
 * and register it in the table. In any case a corresponding call to
975
 * virUnrefStorageVol() is needed to not leak data.
976 977 978 979
 *
 * Returns a pointer to the storage vol, or NULL in case of failure
 */
virStorageVolPtr
980 981
virGetStorageVol(virConnectPtr conn, const char *pool, const char *name,
                 const char *key) {
982 983
    virStorageVolPtr ret = NULL;

984 985 986 987 988 989 990 991 992 993 994
    if (!VIR_IS_CONNECT(conn)) {
        virLibConnError(VIR_ERR_INVALID_ARG, _("no connection"));
        return NULL;
    }
    if (name == NULL) {
        virLibConnError(VIR_ERR_INVALID_ARG, _("missing name"));
        return NULL;
    }
    if (key == NULL) {
        virLibConnError(VIR_ERR_INVALID_ARG, _("missing key"));
        return NULL;
995
    }
996
    virMutexLock(&conn->lock);
997 998 999 1000

    ret = (virStorageVolPtr) virHashLookup(conn->storageVols, key);
    if (ret == NULL) {
        if (VIR_ALLOC(ret) < 0) {
1001
            virMutexUnlock(&conn->lock);
1002
            virReportOOMError();
1003 1004 1005 1006
            goto error;
        }
        ret->pool = strdup(pool);
        if (ret->pool == NULL) {
1007
            virMutexUnlock(&conn->lock);
1008
            virReportOOMError();
1009 1010 1011 1012
            goto error;
        }
        ret->name = strdup(name);
        if (ret->name == NULL) {
1013
            virMutexUnlock(&conn->lock);
1014
            virReportOOMError();
1015 1016
            goto error;
        }
C
Chris Lalancette 已提交
1017 1018
        if (virStrcpyStatic(ret->key, key) == NULL) {
            virMutexUnlock(&conn->lock);
1019
            virLibConnError(VIR_ERR_INTERNAL_ERROR,
C
Chris Lalancette 已提交
1020 1021 1022
                            _("Volume key %s too large for destination"), key);
            goto error;
        }
1023 1024 1025 1026
        ret->magic = VIR_STORAGE_VOL_MAGIC;
        ret->conn = conn;

        if (virHashAddEntry(conn->storageVols, key, ret) < 0) {
1027
            virMutexUnlock(&conn->lock);
1028 1029
            virLibConnError(VIR_ERR_INTERNAL_ERROR, "%s",
                            _("failed to add storage vol to connection hash table"));
1030 1031 1032 1033 1034
            goto error;
        }
        conn->refs++;
    }
    ret->refs++;
1035
    virMutexUnlock(&conn->lock);
1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064
    return(ret);

error:
    if (ret != NULL) {
        VIR_FREE(ret->name);
        VIR_FREE(ret->pool);
        VIR_FREE(ret);
    }
    return(NULL);
}


/**
 * virReleaseStorageVol:
 * @vol: the vol to release
 *
 * Unconditionally release all memory associated with a vol.
 * The conn.lock mutex must be held prior to calling this, and will
 * be released prior to this returning. The vol obj must not
 * be used once this method returns.
 *
 * It will also unreference the associated connection object,
 * which may also be released if its ref count hits zero.
 */
static void
virReleaseStorageVol(virStorageVolPtr vol) {
    virConnectPtr conn = vol->conn;
    DEBUG("release vol %p %s", vol, vol->name);

1065 1066
    if (virHashRemoveEntry(conn->storageVols, vol->key, NULL) < 0) {
        virMutexUnlock(&conn->lock);
1067 1068
        virLibConnError(VIR_ERR_INTERNAL_ERROR, "%s",
                        _("vol missing from connection hash table"));
1069 1070
        conn = NULL;
    }
1071 1072 1073 1074 1075 1076

    vol->magic = -1;
    VIR_FREE(vol->name);
    VIR_FREE(vol->pool);
    VIR_FREE(vol);

1077 1078 1079 1080 1081 1082 1083 1084 1085
    if (conn) {
        DEBUG("unref connection %p %d", conn, conn->refs);
        conn->refs--;
        if (conn->refs == 0) {
            virReleaseConnect(conn);
            /* Already unlocked mutex */
            return;
        }
        virMutexUnlock(&conn->lock);
1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103
    }
}


/**
 * virUnrefStorageVol:
 * @vol: the vol to unreference
 *
 * Unreference the vol. If the use count drops to zero, the structure is
 * actually freed.
 *
 * Returns the reference count or -1 in case of failure.
 */
int
virUnrefStorageVol(virStorageVolPtr vol) {
    int refs;

    if (!VIR_IS_CONNECTED_STORAGE_VOL(vol)) {
1104 1105 1106
        virLibConnError(VIR_ERR_INVALID_ARG,
                        _("bad storage volume or no connection"));
        return -1;
1107
    }
1108
    virMutexLock(&vol->conn->lock);
1109 1110 1111 1112 1113 1114 1115 1116 1117
    DEBUG("unref vol %p %s %d", vol, vol->name, vol->refs);
    vol->refs--;
    refs = vol->refs;
    if (refs == 0) {
        virReleaseStorageVol(vol);
        /* Already unlocked mutex */
        return (0);
    }

1118
    virMutexUnlock(&vol->conn->lock);
1119 1120
    return (refs);
}
1121 1122 1123 1124 1125 1126 1127 1128 1129 1130


/**
 * virGetNodeDevice:
 * @conn: the hypervisor connection
 * @name: device name (unique on node)
 *
 * Lookup if the device is already registered for that connection,
 * if yes return a new pointer to it, if no allocate a new structure,
 * and register it in the table. In any case a corresponding call to
1131
 * virUnrefNodeDevice() is needed to not leak data.
1132 1133 1134 1135 1136 1137 1138 1139
 *
 * Returns a pointer to the node device, or NULL in case of failure
 */
virNodeDevicePtr
virGetNodeDevice(virConnectPtr conn, const char *name)
{
    virNodeDevicePtr ret = NULL;

1140 1141 1142 1143 1144 1145 1146
    if (!VIR_IS_CONNECT(conn)) {
        virLibConnError(VIR_ERR_INVALID_ARG, _("no connection"));
        return NULL;
    }
    if (name == NULL) {
        virLibConnError(VIR_ERR_INVALID_ARG, _("missing name"));
        return NULL;
1147
    }
1148
    virMutexLock(&conn->lock);
1149 1150 1151

    ret = (virNodeDevicePtr) virHashLookup(conn->nodeDevices, name);
    if (ret == NULL) {
1152 1153
        if (VIR_ALLOC(ret) < 0) {
            virMutexUnlock(&conn->lock);
1154
            virReportOOMError();
1155 1156 1157 1158 1159 1160
            goto error;
        }
        ret->magic = VIR_NODE_DEVICE_MAGIC;
        ret->conn = conn;
        ret->name = strdup(name);
        if (ret->name == NULL) {
1161
            virMutexUnlock(&conn->lock);
1162
            virReportOOMError();
1163 1164 1165 1166
            goto error;
        }

        if (virHashAddEntry(conn->nodeDevices, name, ret) < 0) {
1167
            virMutexUnlock(&conn->lock);
1168
            virLibConnError(VIR_ERR_INTERNAL_ERROR,
1169
                            "%s", _("failed to add node dev to conn hash table"));
1170 1171 1172 1173 1174
            goto error;
        }
        conn->refs++;
    }
    ret->refs++;
1175
    virMutexUnlock(&conn->lock);
1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203
    return(ret);

error:
    if (ret != NULL) {
        VIR_FREE(ret->name);
        VIR_FREE(ret);
    }
    return(NULL);
}


/**
 * virReleaseNodeDevice:
 * @dev: the dev to release
 *
 * Unconditionally release all memory associated with a dev.
 * The conn.lock mutex must be held prior to calling this, and will
 * be released prior to this returning. The dev obj must not
 * be used once this method returns.
 *
 * It will also unreference the associated connection object,
 * which may also be released if its ref count hits zero.
 */
static void
virReleaseNodeDevice(virNodeDevicePtr dev) {
    virConnectPtr conn = dev->conn;
    DEBUG("release dev %p %s", dev, dev->name);

1204 1205
    if (virHashRemoveEntry(conn->nodeDevices, dev->name, NULL) < 0) {
        virMutexUnlock(&conn->lock);
1206
        virLibConnError(VIR_ERR_INTERNAL_ERROR,
1207
                        "%s", _("dev missing from connection hash table"));
1208 1209
        conn = NULL;
    }
1210 1211 1212

    dev->magic = -1;
    VIR_FREE(dev->name);
1213
    VIR_FREE(dev->parent);
1214 1215
    VIR_FREE(dev);

1216 1217 1218 1219 1220 1221 1222 1223 1224
    if (conn) {
        DEBUG("unref connection %p %d", conn, conn->refs);
        conn->refs--;
        if (conn->refs == 0) {
            virReleaseConnect(conn);
            /* Already unlocked mutex */
            return;
        }
        virMutexUnlock(&conn->lock);
1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241
    }
}


/**
 * virUnrefNodeDevice:
 * @dev: the dev to unreference
 *
 * Unreference the dev. If the use count drops to zero, the structure is
 * actually freed.
 *
 * Returns the reference count or -1 in case of failure.
 */
int
virUnrefNodeDevice(virNodeDevicePtr dev) {
    int refs;

1242
    virMutexLock(&dev->conn->lock);
1243 1244 1245 1246 1247 1248 1249 1250 1251
    DEBUG("unref dev %p %s %d", dev, dev->name, dev->refs);
    dev->refs--;
    refs = dev->refs;
    if (refs == 0) {
        virReleaseNodeDevice(dev);
        /* Already unlocked mutex */
        return (0);
    }

1252
    virMutexUnlock(&dev->conn->lock);
1253 1254
    return (refs);
}
1255

1256

1257 1258 1259 1260 1261 1262 1263
/**
 * virGetSecret:
 * @conn: the hypervisor connection
 * @uuid: secret UUID
 *
 * Lookup if the secret is already registered for that connection, if so return
 * a pointer to it, otherwise allocate a new structure, and register it in the
1264
 * table. In any case a corresponding call to virUnrefSecret() is needed to not
1265 1266 1267 1268 1269
 * leak data.
 *
 * Returns a pointer to the secret, or NULL in case of failure
 */
virSecretPtr
1270 1271
virGetSecret(virConnectPtr conn, const unsigned char *uuid,
             int usageType, const char *usageID)
1272 1273
{
    virSecretPtr ret = NULL;
1274
    char uuidstr[VIR_UUID_STRING_BUFLEN];
1275

1276 1277 1278 1279 1280 1281 1282 1283 1284 1285
    if (!VIR_IS_CONNECT(conn)) {
        virLibConnError(VIR_ERR_INVALID_ARG, _("no connection"));
        return NULL;
    }
    if (uuid == NULL) {
        virLibConnError(VIR_ERR_INVALID_ARG, _("missing uuid"));
        return NULL;
    }
    if (usageID == NULL) {
        virLibConnError(VIR_ERR_INVALID_ARG, _("missing usageID"));
1286 1287 1288 1289
        return NULL;
    }
    virMutexLock(&conn->lock);

1290 1291 1292
    virUUIDFormat(uuid, uuidstr);

    ret = virHashLookup(conn->secrets, uuidstr);
1293 1294 1295
    if (ret == NULL) {
        if (VIR_ALLOC(ret) < 0) {
            virMutexUnlock(&conn->lock);
1296
            virReportOOMError();
1297 1298 1299 1300
            goto error;
        }
        ret->magic = VIR_SECRET_MAGIC;
        ret->conn = conn;
1301
        memcpy(&(ret->uuid[0]), uuid, VIR_UUID_BUFLEN);
1302 1303 1304
        ret->usageType = usageType;
        if (!(ret->usageID = strdup(usageID))) {
            virMutexUnlock(&conn->lock);
1305
            virReportOOMError();
1306 1307
            goto error;
        }
1308
        if (virHashAddEntry(conn->secrets, uuidstr, ret) < 0) {
1309
            virMutexUnlock(&conn->lock);
1310 1311
            virLibConnError(VIR_ERR_INTERNAL_ERROR, "%s",
                            _("failed to add secret to conn hash table"));
1312 1313 1314 1315 1316 1317 1318 1319 1320 1321
            goto error;
        }
        conn->refs++;
    }
    ret->refs++;
    virMutexUnlock(&conn->lock);
    return ret;

error:
    if (ret != NULL) {
1322
        VIR_FREE(ret->usageID);
1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341
        VIR_FREE(ret);
    }
    return NULL;
}

/**
 * virReleaseSecret:
 * @secret: the secret to release
 *
 * Unconditionally release all memory associated with a secret.  The conn.lock
 * mutex must be held prior to calling this, and will be released prior to this
 * returning. The secret obj must not be used once this method returns.
 *
 * It will also unreference the associated connection object, which may also be
 * released if its ref count hits zero.
 */
static void
virReleaseSecret(virSecretPtr secret) {
    virConnectPtr conn = secret->conn;
1342
    char uuidstr[VIR_UUID_STRING_BUFLEN];
1343

1344
    virUUIDFormat(secret->uuid, uuidstr);
J
Jiri Denemark 已提交
1345 1346
    DEBUG("release secret %p %s", secret, uuidstr);

1347
    if (virHashRemoveEntry(conn->secrets, uuidstr, NULL) < 0) {
1348
        virMutexUnlock(&conn->lock);
1349 1350
        virLibConnError(VIR_ERR_INTERNAL_ERROR, "%s",
                        _("secret missing from connection hash table"));
1351 1352 1353
        conn = NULL;
    }

1354
    VIR_FREE(secret->usageID);
1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383
    secret->magic = -1;
    VIR_FREE(secret);

    if (conn) {
        DEBUG("unref connection %p %d", conn, conn->refs);
        conn->refs--;
        if (conn->refs == 0) {
            virReleaseConnect(conn);
            /* Already unlocked mutex */
            return;
        }
        virMutexUnlock(&conn->lock);
    }
}

/**
 * virUnrefSecret:
 * @secret: the secret to unreference
 *
 * Unreference the secret. If the use count drops to zero, the structure is
 * actually freed.
 *
 * Returns the reference count or -1 in case of failure.
 */
int
virUnrefSecret(virSecretPtr secret) {
    int refs;

    if (!VIR_IS_CONNECTED_SECRET(secret)) {
1384
        virLibConnError(VIR_ERR_INVALID_ARG, _("bad secret or no connection"));
1385 1386 1387
        return -1;
    }
    virMutexLock(&secret->conn->lock);
1388
    DEBUG("unref secret %p %p %d", secret, secret->uuid, secret->refs);
1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399
    secret->refs--;
    refs = secret->refs;
    if (refs == 0) {
        virReleaseSecret(secret);
        /* Already unlocked mutex */
        return 0;
    }

    virMutexUnlock(&secret->conn->lock);
    return refs;
}
1400 1401 1402 1403 1404 1405 1406

virStreamPtr virGetStream(virConnectPtr conn) {
    virStreamPtr ret = NULL;

    virMutexLock(&conn->lock);

    if (VIR_ALLOC(ret) < 0) {
1407
        virReportOOMError();
1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457
        goto error;
    }
    ret->magic = VIR_STREAM_MAGIC;
    ret->conn = conn;
    conn->refs++;
    ret->refs++;
    virMutexUnlock(&conn->lock);
    return(ret);

error:
    virMutexUnlock(&conn->lock);
    VIR_FREE(ret);
    return(NULL);
}

static void
virReleaseStream(virStreamPtr st) {
    virConnectPtr conn = st->conn;
    DEBUG("release dev %p", st);

    st->magic = -1;
    VIR_FREE(st);

    DEBUG("unref connection %p %d", conn, conn->refs);
    conn->refs--;
    if (conn->refs == 0) {
        virReleaseConnect(conn);
        /* Already unlocked mutex */
        return;
    }

    virMutexUnlock(&conn->lock);
}

int virUnrefStream(virStreamPtr st) {
    int refs;

    virMutexLock(&st->conn->lock);
    DEBUG("unref stream %p %d", st, st->refs);
    st->refs--;
    refs = st->refs;
    if (refs == 0) {
        virReleaseStream(st);
        /* Already unlocked mutex */
        return (0);
    }

    virMutexUnlock(&st->conn->lock);
    return (refs);
}
S
Stefan Berger 已提交
1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468


/**
 * virGetNWFilter:
 * @conn: the hypervisor connection
 * @name: pointer to the network filter pool name
 * @uuid: pointer to the uuid
 *
 * Lookup if the network filter is already registered for that connection,
 * if yes return a new pointer to it, if no allocate a new structure,
 * and register it in the table. In any case a corresponding call to
1469
 * virUnrefNWFilter() is needed to not leak data.
S
Stefan Berger 已提交
1470 1471 1472 1473 1474 1475
 *
 * Returns a pointer to the network, or NULL in case of failure
 */
virNWFilterPtr
virGetNWFilter(virConnectPtr conn, const char *name, const unsigned char *uuid) {
    virNWFilterPtr ret = NULL;
1476
    char uuidstr[VIR_UUID_STRING_BUFLEN];
S
Stefan Berger 已提交
1477

1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488
    if (!VIR_IS_CONNECT(conn)) {
        virLibConnError(VIR_ERR_INVALID_ARG, _("no connection"));
        return NULL;
    }
    if (name == NULL) {
        virLibConnError(VIR_ERR_INVALID_ARG, _("missing name"));
        return NULL;
    }
    if (uuid == NULL) {
        virLibConnError(VIR_ERR_INVALID_ARG, _("missing uuid"));
        return NULL;
S
Stefan Berger 已提交
1489 1490 1491
    }
    virMutexLock(&conn->lock);

1492
    virUUIDFormat(uuid, uuidstr);
S
Stefan Berger 已提交
1493

1494
    ret = (virNWFilterPtr) virHashLookup(conn->nwfilters, uuidstr);
S
Stefan Berger 已提交
1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508
    if (ret == NULL) {
        if (VIR_ALLOC(ret) < 0) {
            virMutexUnlock(&conn->lock);
            virReportOOMError();
            goto error;
        }
        ret->name = strdup(name);
        if (ret->name == NULL) {
            virMutexUnlock(&conn->lock);
            virReportOOMError();
            goto error;
        }
        ret->magic = VIR_NWFILTER_MAGIC;
        ret->conn = conn;
1509
        memcpy(&(ret->uuid[0]), uuid, VIR_UUID_BUFLEN);
S
Stefan Berger 已提交
1510

1511
        if (virHashAddEntry(conn->nwfilters, uuidstr, ret) < 0) {
S
Stefan Berger 已提交
1512
            virMutexUnlock(&conn->lock);
1513
            virLibConnError(VIR_ERR_INTERNAL_ERROR, "%s",
1514
                            _("failed to add network filter to connection hash table"));
S
Stefan Berger 已提交
1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532
            goto error;
        }
        conn->refs++;
    }
    ret->refs++;
    virMutexUnlock(&conn->lock);
    return(ret);

error:
    if (ret != NULL) {
        VIR_FREE(ret->name);
        VIR_FREE(ret);
    }
    return(NULL);
}


/**
1533 1534
 * virReleaseNWFilter:
 * @nwfilter: the nwfilter to release
S
Stefan Berger 已提交
1535
 *
1536
 * Unconditionally release all memory associated with a nwfilter.
S
Stefan Berger 已提交
1537
 * The conn.lock mutex must be held prior to calling this, and will
1538
 * be released prior to this returning. The nwfilter obj must not
S
Stefan Berger 已提交
1539 1540 1541 1542 1543 1544
 * be used once this method returns.
 *
 * It will also unreference the associated connection object,
 * which may also be released if its ref count hits zero.
 */
static void
1545 1546 1547
virReleaseNWFilter(virNWFilterPtr nwfilter)
{
    virConnectPtr conn = nwfilter->conn;
1548
    char uuidstr[VIR_UUID_STRING_BUFLEN];
S
Stefan Berger 已提交
1549

1550 1551
    virUUIDFormat(nwfilter->uuid, uuidstr);
    DEBUG("release nwfilter %p %s %s", nwfilter, nwfilter->name, uuidstr);
1552

1553
    if (virHashRemoveEntry(conn->nwfilters, uuidstr, NULL) < 0) {
S
Stefan Berger 已提交
1554
        virMutexUnlock(&conn->lock);
1555 1556
        virLibConnError(VIR_ERR_INTERNAL_ERROR, "%s",
                        _("pool missing from connection hash table"));
S
Stefan Berger 已提交
1557 1558 1559
        conn = NULL;
    }

1560 1561 1562
    nwfilter->magic = -1;
    VIR_FREE(nwfilter->name);
    VIR_FREE(nwfilter);
S
Stefan Berger 已提交
1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586

    if (conn) {
        DEBUG("unref connection %p %d", conn, conn->refs);
        conn->refs--;
        if (conn->refs == 0) {
            virReleaseConnect(conn);
            /* Already unlocked mutex */
            return;
        }
        virMutexUnlock(&conn->lock);
    }
}


/**
 * virUnrefNWFilter:
 * @pool: the nwfilter to unreference
 *
 * Unreference the networkf itler. If the use count drops to zero, the
 * structure is actually freed.
 *
 * Returns the reference count or -1 in case of failure.
 */
int
1587 1588
virUnrefNWFilter(virNWFilterPtr nwfilter)
{
S
Stefan Berger 已提交
1589 1590
    int refs;

1591
    if (!VIR_IS_CONNECTED_NWFILTER(nwfilter)) {
1592 1593
        virLibConnError(VIR_ERR_INVALID_ARG,
                        _("bad nwfilter or no connection"));
S
Stefan Berger 已提交
1594 1595
        return -1;
    }
1596 1597 1598 1599
    virMutexLock(&nwfilter->conn->lock);
    DEBUG("unref pool %p %s %d", nwfilter, nwfilter->name, nwfilter->refs);
    nwfilter->refs--;
    refs = nwfilter->refs;
S
Stefan Berger 已提交
1600
    if (refs == 0) {
1601
        virReleaseNWFilter(nwfilter);
S
Stefan Berger 已提交
1602 1603 1604 1605
        /* Already unlocked mutex */
        return (0);
    }

1606
    virMutexUnlock(&nwfilter->conn->lock);
S
Stefan Berger 已提交
1607 1608
    return (refs);
}
C
Chris Lalancette 已提交
1609 1610 1611 1612 1613 1614 1615


virDomainSnapshotPtr
virGetDomainSnapshot(virDomainPtr domain, const char *name)
{
    virDomainSnapshotPtr ret = NULL;

1616 1617 1618 1619 1620 1621 1622
    if (!VIR_IS_DOMAIN(domain)) {
        virLibConnError(VIR_ERR_INVALID_ARG, _("bad domain"));
        return NULL;
    }
    if (name == NULL) {
        virLibConnError(VIR_ERR_INVALID_ARG, _("missing name"));
        return NULL;
C
Chris Lalancette 已提交
1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643
    }
    virMutexLock(&domain->conn->lock);

    ret = (virDomainSnapshotPtr) virHashLookup(domain->snapshots, name);
    if (ret == NULL) {
        if (VIR_ALLOC(ret) < 0) {
            virMutexUnlock(&domain->conn->lock);
            virReportOOMError();
            goto error;
        }
        ret->name = strdup(name);
        if (ret->name == NULL) {
            virMutexUnlock(&domain->conn->lock);
            virReportOOMError();
            goto error;
        }
        ret->magic = VIR_SNAPSHOT_MAGIC;
        ret->domain = domain;

        if (virHashAddEntry(domain->snapshots, name, ret) < 0) {
            virMutexUnlock(&domain->conn->lock);
1644
            virLibConnError(VIR_ERR_INTERNAL_ERROR,
C
Chris Lalancette 已提交
1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673
                            "%s", _("failed to add snapshot to domain hash table"));
            goto error;
        }
        domain->refs++;
        DEBUG("New hash entry %p", ret);
    } else {
        DEBUG("Existing hash entry %p: refs now %d", ret, ret->refs+1);
    }
    ret->refs++;
    virMutexUnlock(&domain->conn->lock);
    return(ret);

 error:
    if (ret != NULL) {
        VIR_FREE(ret->name);
        VIR_FREE(ret);
    }
    return(NULL);
}


static void
virReleaseDomainSnapshot(virDomainSnapshotPtr snapshot)
{
    virDomainPtr domain = snapshot->domain;
    DEBUG("release snapshot %p %s", snapshot, snapshot->name);

    if (virHashRemoveEntry(domain->snapshots, snapshot->name, NULL) < 0) {
        virMutexUnlock(&domain->conn->lock);
1674
        virLibConnError(VIR_ERR_INTERNAL_ERROR,
C
Chris Lalancette 已提交
1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700
                        "%s", _("snapshot missing from domain hash table"));
        domain = NULL;
    }

    snapshot->magic = -1;
    VIR_FREE(snapshot->name);
    VIR_FREE(snapshot);

    if (domain) {
        DEBUG("unref domain %p %d", domain, domain->refs);
        domain->refs--;
        if (domain->refs == 0) {
            virReleaseDomain(domain);
            /* Already unlocked mutex */
            return;
        }
        virMutexUnlock(&domain->conn->lock);
    }
}

int
virUnrefDomainSnapshot(virDomainSnapshotPtr snapshot)
{
    int refs;

    if (!VIR_IS_DOMAIN_SNAPSHOT(snapshot)) {
1701 1702
        virLibConnError(VIR_ERR_INVALID_ARG, _("not a snapshot"));
        return -1;
C
Chris Lalancette 已提交
1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717
    }

    virMutexLock(&snapshot->domain->conn->lock);
    DEBUG("unref snapshot %p %s %d", snapshot, snapshot->name, snapshot->refs);
    snapshot->refs--;
    refs = snapshot->refs;
    if (refs == 0) {
        virReleaseDomainSnapshot(snapshot);
        /* Already unlocked mutex */
        return (0);
    }

    virMutexUnlock(&snapshot->domain->conn->lock);
    return (refs);
}