datatypes.c 36.9 KB
Newer Older
1 2 3
/*
 * datatypes.h: management of structs for public data types
 *
4
 * Copyright (C) 2006-2009 Red Hat, Inc.
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
 *
 * 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>

#include "datatypes.h"
#include "virterror_internal.h"
26
#include "logging.h"
27
#include "memory.h"
28
#include "uuid.h"
29

30 31
#define VIR_FROM_THIS VIR_FROM_NONE

32 33 34 35
#define virLibConnError(conn, code, fmt...)                       \
    virReportErrorHelper(conn, VIR_FROM_THIS, code, __FILE__,     \
                         __FUNCTION__, __LINE__, fmt)

36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
/************************************************************************
 *									*
 *			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)
{
53
    return (virUnrefDomain(domain));
54 55 56 57 58 59 60 61 62 63 64 65 66
}

/**
 * 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)
{
67
    return (virUnrefNetwork(network));
68 69
}

D
Daniel Veillard 已提交
70 71 72 73 74 75 76 77 78
/**
 * virInterfaceFreeName:
 * @interface: a interface object
 *
 * 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
79
virInterfaceFreeName(virInterfacePtr iface, const char *name ATTRIBUTE_UNUSED)
D
Daniel Veillard 已提交
80
{
81
    return (virUnrefInterface(iface));
D
Daniel Veillard 已提交
82 83
}

84 85 86 87 88 89 90 91 92 93 94
/**
 * 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)
{
95
    return (virUnrefStoragePool(pool));
96 97 98 99 100 101 102 103 104 105 106 107 108
}

/**
 * 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)
{
109
    return (virUnrefStorageVol(vol));
110 111
}

112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128
/**
 * virSecretFreeName:
 * @secret_: a secret object
 *
 * 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.
 */
static void
virSecretFreeName(void *secret_, const char *name ATTRIBUTE_UNUSED)
{
    virSecretPtr secret;

    secret = secret_;
    virUnrefSecret(secret);
}

129 130 131 132 133 134 135 136 137 138 139 140
/**
 * 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) {
141
        virReportOOMError(NULL);
142 143
        goto failed;
    }
144 145 146 147 148
    if (virMutexInit(&ret->lock) < 0) {
        VIR_FREE(ret);
        goto failed;
    }

149 150 151 152 153
    ret->magic = VIR_CONNECT_MAGIC;
    ret->driver = NULL;
    ret->networkDriver = NULL;
    ret->privateData = NULL;
    ret->networkPrivateData = NULL;
D
Daniel Veillard 已提交
154
    ret->interfacePrivateData = NULL;
155 156 157 158 159 160
    ret->domains = virHashCreate(20);
    if (ret->domains == NULL)
        goto failed;
    ret->networks = virHashCreate(20);
    if (ret->networks == NULL)
        goto failed;
D
Daniel Veillard 已提交
161 162 163
    ret->interfaces = virHashCreate(20);
    if (ret->interfaces == NULL)
        goto failed;
164 165 166 167 168 169
    ret->storagePools = virHashCreate(20);
    if (ret->storagePools == NULL)
        goto failed;
    ret->storageVols = virHashCreate(20);
    if (ret->storageVols == NULL)
        goto failed;
170 171 172
    ret->nodeDevices = virHashCreate(256);
    if (ret->nodeDevices == NULL)
        goto failed;
173 174 175
    ret->secrets = virHashCreate(20);
    if (ret->secrets == NULL)
        goto failed;
176 177 178 179 180 181 182 183 184 185

    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 已提交
186 187
        if (ret->interfaces != NULL)
           virHashFree(ret->interfaces, (virHashDeallocator) virInterfaceFreeName);
188 189 190 191
        if (ret->storagePools != NULL)
            virHashFree(ret->storagePools, (virHashDeallocator) virStoragePoolFreeName);
        if (ret->storageVols != NULL)
            virHashFree(ret->storageVols, (virHashDeallocator) virStorageVolFreeName);
192 193
        if (ret->nodeDevices != NULL)
            virHashFree(ret->nodeDevices, (virHashDeallocator) virNodeDeviceFree);
194 195
        if (ret->secrets != NULL)
            virHashFree(ret->secrets, virSecretFreeName);
196

197
        virMutexDestroy(&ret->lock);
198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213
        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) {
214
    DEBUG("release connection %p", conn);
215 216 217 218
    if (conn->domains != NULL)
        virHashFree(conn->domains, (virHashDeallocator) virDomainFreeName);
    if (conn->networks != NULL)
        virHashFree(conn->networks, (virHashDeallocator) virNetworkFreeName);
D
Daniel Veillard 已提交
219 220
    if (conn->interfaces != NULL)
        virHashFree(conn->interfaces, (virHashDeallocator) virInterfaceFreeName);
221 222 223 224
    if (conn->storagePools != NULL)
        virHashFree(conn->storagePools, (virHashDeallocator) virStoragePoolFreeName);
    if (conn->storageVols != NULL)
        virHashFree(conn->storageVols, (virHashDeallocator) virStorageVolFreeName);
225 226
    if (conn->nodeDevices != NULL)
        virHashFree(conn->nodeDevices, (virHashDeallocator) virNodeDeviceFree);
227 228
    if (conn->secrets != NULL)
        virHashFree(conn->secrets, virSecretFreeName);
229 230 231

    virResetError(&conn->err);

232
    xmlFreeURI(conn->uri);
233

234 235
    virMutexUnlock(&conn->lock);
    virMutexDestroy(&conn->lock);
236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252
    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))) {
253
        virLibConnError(NULL, VIR_ERR_INVALID_ARG, __FUNCTION__);
254 255
        return(-1);
    }
256
    virMutexLock(&conn->lock);
257
    DEBUG("unref connection %p %d", conn, conn->refs);
258 259 260
    conn->refs--;
    refs = conn->refs;
    if (refs == 0) {
261 262 263 264 265 266 267 268 269 270 271 272 273
        /* 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);
274 275
        if (conn->secretDriver)
            conn->secretDriver->close (conn);
276 277 278 279 280 281 282
        if (conn->driver)
            conn->driver->close (conn);

        /* reacquire the connection lock since virReleaseConnect expects
         * it to already be held
         */
        virMutexLock(&conn->lock);
283 284 285 286
        virReleaseConnect(conn);
        /* Already unlocked mutex */
        return (0);
    }
287
    virMutexUnlock(&conn->lock);
288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304
    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 已提交
305
virGetDomain(virConnectPtr conn, const char *name, const unsigned char *uuid) {
306 307 308
    virDomainPtr ret = NULL;

    if ((!VIR_IS_CONNECT(conn)) || (name == NULL) || (uuid == NULL)) {
309
        virLibConnError(NULL, VIR_ERR_INVALID_ARG, __FUNCTION__);
310 311
        return(NULL);
    }
312
    virMutexLock(&conn->lock);
313 314 315 316 317 318 319

    /* TODO search by UUID first as they are better differenciators */

    ret = (virDomainPtr) virHashLookup(conn->domains, name);
    /* TODO check the UUID */
    if (ret == NULL) {
        if (VIR_ALLOC(ret) < 0) {
320
            virMutexUnlock(&conn->lock);
321
            virReportOOMError(conn);
322 323 324 325
            goto error;
        }
        ret->name = strdup(name);
        if (ret->name == NULL) {
326
            virMutexUnlock(&conn->lock);
327
            virReportOOMError(conn);
328 329 330 331 332 333 334 335 336
            goto error;
        }
        ret->magic = VIR_DOMAIN_MAGIC;
        ret->conn = conn;
        ret->id = -1;
        if (uuid != NULL)
            memcpy(&(ret->uuid[0]), uuid, VIR_UUID_BUFLEN);

        if (virHashAddEntry(conn->domains, name, ret) < 0) {
337
            virMutexUnlock(&conn->lock);
338
            virLibConnError(conn, VIR_ERR_INTERNAL_ERROR,
339
                            "%s", _("failed to add domain to connection hash table"));
340 341 342 343 344 345 346 347
            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++;
348
    virMutexUnlock(&conn->lock);
349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376
    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;
    DEBUG("release domain %p %s", domain, domain->name);

    /* TODO search by UUID first as they are better differenciators */
377 378
    if (virHashRemoveEntry(conn->domains, domain->name, NULL) < 0) {
        virMutexUnlock(&conn->lock);
379
        virLibConnError(conn, VIR_ERR_INTERNAL_ERROR,
380
                        "%s", _("domain missing from connection hash table"));
381 382
        conn = NULL;
    }
383 384 385 386 387 388

    domain->magic = -1;
    domain->id = -1;
    VIR_FREE(domain->name);
    VIR_FREE(domain);

389 390 391 392 393 394 395 396 397
    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);
398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415
    }
}


/**
 * 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)) {
416
        virLibConnError(NULL, VIR_ERR_INVALID_ARG, __FUNCTION__);
417 418
        return(-1);
    }
419
    virMutexLock(&domain->conn->lock);
420 421 422 423 424 425 426 427 428
    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);
    }

429
    virMutexUnlock(&domain->conn->lock);
430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446
    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 已提交
447
virGetNetwork(virConnectPtr conn, const char *name, const unsigned char *uuid) {
448 449 450
    virNetworkPtr ret = NULL;

    if ((!VIR_IS_CONNECT(conn)) || (name == NULL) || (uuid == NULL)) {
451
        virLibConnError(NULL, VIR_ERR_INVALID_ARG, __FUNCTION__);
452 453
        return(NULL);
    }
454
    virMutexLock(&conn->lock);
455 456 457 458 459 460 461

    /* TODO search by UUID first as they are better differenciators */

    ret = (virNetworkPtr) virHashLookup(conn->networks, name);
    /* TODO check the UUID */
    if (ret == NULL) {
        if (VIR_ALLOC(ret) < 0) {
462
            virMutexUnlock(&conn->lock);
463
            virReportOOMError(conn);
464 465 466 467
            goto error;
        }
        ret->name = strdup(name);
        if (ret->name == NULL) {
468
            virMutexUnlock(&conn->lock);
469
            virReportOOMError(conn);
470 471 472 473 474 475 476 477
            goto error;
        }
        ret->magic = VIR_NETWORK_MAGIC;
        ret->conn = conn;
        if (uuid != NULL)
            memcpy(&(ret->uuid[0]), uuid, VIR_UUID_BUFLEN);

        if (virHashAddEntry(conn->networks, name, ret) < 0) {
478
            virMutexUnlock(&conn->lock);
479
            virLibConnError(conn, VIR_ERR_INTERNAL_ERROR,
480
                            "%s", _("failed to add network to connection hash table"));
481 482 483 484 485
            goto error;
        }
        conn->refs++;
    }
    ret->refs++;
486
    virMutexUnlock(&conn->lock);
487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514
    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;
    DEBUG("release network %p %s", network, network->name);

    /* TODO search by UUID first as they are better differenciators */
515 516
    if (virHashRemoveEntry(conn->networks, network->name, NULL) < 0) {
        virMutexUnlock(&conn->lock);
517
        virLibConnError(conn, VIR_ERR_INTERNAL_ERROR,
518
                        "%s", _("network missing from connection hash table"));
519 520
        conn = NULL;
    }
521 522 523 524 525

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

526 527 528 529 530 531 532 533 534
    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);
535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552
    }
}


/**
 * 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)) {
553
        virLibConnError(NULL, VIR_ERR_INVALID_ARG, __FUNCTION__);
554 555
        return(-1);
    }
556
    virMutexLock(&network->conn->lock);
557 558 559 560 561 562 563 564 565
    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);
    }

566
    virMutexUnlock(&network->conn->lock);
567 568 569 570
    return (refs);
}


D
Daniel Veillard 已提交
571 572 573 574 575 576 577
/**
 * 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,
578 579 580 581
 * 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 已提交
582 583 584 585 586 587 588 589 590 591 592 593 594 595
 *
 * 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;

    if ((!VIR_IS_CONNECT(conn)) || (name == NULL) || (mac == NULL)) {
        virLibConnError(NULL, VIR_ERR_INVALID_ARG, __FUNCTION__);
        return(NULL);
    }
    virMutexLock(&conn->lock);

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

597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628
    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);
                virLibConnError(conn, VIR_ERR_INTERNAL_ERROR,
_("Failed to change interface mac address from %s to %s due to differing lengths."),
                                ret->mac, mac);
                ret = NULL;
                goto error;
            }
        }
    } else {
D
Daniel Veillard 已提交
629
        if (VIR_ALLOC(ret) < 0) {
630
            virMutexUnlock(&conn->lock);
D
Daniel Veillard 已提交
631 632 633 634 635
            virReportOOMError(conn);
            goto error;
        }
        ret->name = strdup(name);
        if (ret->name == NULL) {
636
            virMutexUnlock(&conn->lock);
D
Daniel Veillard 已提交
637 638 639 640 641
            virReportOOMError(conn);
            goto error;
        }
        ret->mac = strdup(mac);
        if (ret->mac == NULL) {
642
            virMutexUnlock(&conn->lock);
D
Daniel Veillard 已提交
643 644 645 646 647 648 649 650
            virReportOOMError(conn);
            goto error;
        }

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

        if (virHashAddEntry(conn->interfaces, name, ret) < 0) {
651
            virMutexUnlock(&conn->lock);
D
Daniel Veillard 已提交
652
            virLibConnError(conn, VIR_ERR_INTERNAL_ERROR,
653
                            "%s", _("failed to add interface to connection hash table"));
D
Daniel Veillard 已提交
654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683
            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
 *
 * Unconditionally release all memory associated with a interface.
 * 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
684 685 686
virReleaseInterface(virInterfacePtr iface) {
    virConnectPtr conn = iface->conn;
    DEBUG("release interface %p %s", iface, iface->name);
D
Daniel Veillard 已提交
687

688 689 690
    if (virHashRemoveEntry(conn->interfaces, iface->name, NULL) < 0) {
        /* unlock before reporting error because error report grabs lock */
        virMutexUnlock(&conn->lock);
D
Daniel Veillard 已提交
691
        virLibConnError(conn, VIR_ERR_INTERNAL_ERROR,
692
                        "%s", _("interface missing from connection hash table"));
693 694 695
        /* don't decr the conn refct if we weren't connected to it */
        conn = NULL;
    }
D
Daniel Veillard 已提交
696

697 698 699 700
    iface->magic = -1;
    VIR_FREE(iface->name);
    VIR_FREE(iface->mac);
    VIR_FREE(iface);
D
Daniel Veillard 已提交
701

702 703 704 705 706 707 708 709 710
    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 已提交
711 712 713 714 715 716 717 718 719 720 721 722 723 724
    }
}


/**
 * 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
725
virUnrefInterface(virInterfacePtr iface) {
D
Daniel Veillard 已提交
726 727
    int refs;

728
    if (!VIR_IS_CONNECTED_INTERFACE(iface)) {
D
Daniel Veillard 已提交
729 730 731
        virLibConnError(NULL, VIR_ERR_INVALID_ARG, __FUNCTION__);
        return(-1);
    }
732 733 734 735
    virMutexLock(&iface->conn->lock);
    DEBUG("unref interface %p %s %d", iface, iface->name, iface->refs);
    iface->refs--;
    refs = iface->refs;
D
Daniel Veillard 已提交
736
    if (refs == 0) {
737
        virReleaseInterface(iface);
D
Daniel Veillard 已提交
738 739 740 741
        /* Already unlocked mutex */
        return (0);
    }

742
    virMutexUnlock(&iface->conn->lock);
D
Daniel Veillard 已提交
743 744 745 746
    return (refs);
}


747 748 749 750 751 752 753 754 755 756 757 758 759 760
/**
 * 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
 * virFreeStoragePool() is needed to not leak data.
 *
 * Returns a pointer to the network, or NULL in case of failure
 */
virStoragePoolPtr
D
Daniel P. Berrange 已提交
761
virGetStoragePool(virConnectPtr conn, const char *name, const unsigned char *uuid) {
762 763 764
    virStoragePoolPtr ret = NULL;

    if ((!VIR_IS_CONNECT(conn)) || (name == NULL) || (uuid == NULL)) {
765
        virLibConnError(NULL, VIR_ERR_INVALID_ARG, __FUNCTION__);
766 767
        return(NULL);
    }
768
    virMutexLock(&conn->lock);
769 770 771 772 773 774 775

    /* TODO search by UUID first as they are better differenciators */

    ret = (virStoragePoolPtr) virHashLookup(conn->storagePools, name);
    /* TODO check the UUID */
    if (ret == NULL) {
        if (VIR_ALLOC(ret) < 0) {
776
            virMutexUnlock(&conn->lock);
777
            virReportOOMError(conn);
778 779 780 781
            goto error;
        }
        ret->name = strdup(name);
        if (ret->name == NULL) {
782
            virMutexUnlock(&conn->lock);
783
            virReportOOMError(conn);
784 785 786 787 788 789 790 791
            goto error;
        }
        ret->magic = VIR_STORAGE_POOL_MAGIC;
        ret->conn = conn;
        if (uuid != NULL)
            memcpy(&(ret->uuid[0]), uuid, VIR_UUID_BUFLEN);

        if (virHashAddEntry(conn->storagePools, name, ret) < 0) {
792
            virMutexUnlock(&conn->lock);
793
            virLibConnError(conn, VIR_ERR_INTERNAL_ERROR,
794
                            "%s", _("failed to add storage pool to connection hash table"));
795 796 797 798 799
            goto error;
        }
        conn->refs++;
    }
    ret->refs++;
800
    virMutexUnlock(&conn->lock);
801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829
    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;
    DEBUG("release pool %p %s", pool, pool->name);

    /* TODO search by UUID first as they are better differenciators */
830 831
    if (virHashRemoveEntry(conn->storagePools, pool->name, NULL) < 0) {
        virMutexUnlock(&conn->lock);
832
        virLibConnError(conn, VIR_ERR_INTERNAL_ERROR,
833
                        "%s", _("pool missing from connection hash table"));
834 835
        conn = NULL;
    }
836 837 838 839 840

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

841 842 843 844 845 846 847 848 849
    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);
850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867
    }
}


/**
 * 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)) {
868
        virLibConnError(NULL, VIR_ERR_INVALID_ARG, __FUNCTION__);
869 870
        return(-1);
    }
871
    virMutexLock(&pool->conn->lock);
872 873 874 875 876 877 878 879 880
    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);
    }

881
    virMutexUnlock(&pool->conn->lock);
882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900
    return (refs);
}


/**
 * virGetStorageVol:
 * @conn: the hypervisor connection
 * @pool: pool owning the volume
 * @name: pointer to the storage vol name
 * @uuid: pointer to the uuid
 *
 * 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
 * virFreeStorageVol() is needed to not leak data.
 *
 * Returns a pointer to the storage vol, or NULL in case of failure
 */
virStorageVolPtr
D
Daniel P. Berrange 已提交
901
virGetStorageVol(virConnectPtr conn, const char *pool, const char *name, const char *key) {
902 903 904
    virStorageVolPtr ret = NULL;

    if ((!VIR_IS_CONNECT(conn)) || (name == NULL) || (key == NULL)) {
905
        virLibConnError(NULL, VIR_ERR_INVALID_ARG, __FUNCTION__);
906 907
        return(NULL);
    }
908
    virMutexLock(&conn->lock);
909 910 911 912

    ret = (virStorageVolPtr) virHashLookup(conn->storageVols, key);
    if (ret == NULL) {
        if (VIR_ALLOC(ret) < 0) {
913
            virMutexUnlock(&conn->lock);
914
            virReportOOMError(conn);
915 916 917 918
            goto error;
        }
        ret->pool = strdup(pool);
        if (ret->pool == NULL) {
919
            virMutexUnlock(&conn->lock);
920
            virReportOOMError(conn);
921 922 923 924
            goto error;
        }
        ret->name = strdup(name);
        if (ret->name == NULL) {
925
            virMutexUnlock(&conn->lock);
926
            virReportOOMError(conn);
927 928 929 930 931 932 933 934
            goto error;
        }
        strncpy(ret->key, key, sizeof(ret->key)-1);
        ret->key[sizeof(ret->key)-1] = '\0';
        ret->magic = VIR_STORAGE_VOL_MAGIC;
        ret->conn = conn;

        if (virHashAddEntry(conn->storageVols, key, ret) < 0) {
935
            virMutexUnlock(&conn->lock);
936
            virLibConnError(conn, VIR_ERR_INTERNAL_ERROR,
937
                            "%s", _("failed to add storage vol to connection hash table"));
938 939 940 941 942
            goto error;
        }
        conn->refs++;
    }
    ret->refs++;
943
    virMutexUnlock(&conn->lock);
944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973
    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);

    /* TODO search by UUID first as they are better differenciators */
974 975
    if (virHashRemoveEntry(conn->storageVols, vol->key, NULL) < 0) {
        virMutexUnlock(&conn->lock);
976
        virLibConnError(conn, VIR_ERR_INTERNAL_ERROR,
977
                        "%s", _("vol missing from connection hash table"));
978 979
        conn = NULL;
    }
980 981 982 983 984 985

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

986 987 988 989 990 991 992 993 994
    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);
995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012
    }
}


/**
 * 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)) {
1013
        virLibConnError(NULL, VIR_ERR_INVALID_ARG, __FUNCTION__);
1014 1015
        return(-1);
    }
1016
    virMutexLock(&vol->conn->lock);
1017 1018 1019 1020 1021 1022 1023 1024 1025
    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);
    }

1026
    virMutexUnlock(&vol->conn->lock);
1027 1028
    return (refs);
}
1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048


/**
 * 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
 * virFreeNodeDevice() is needed to not leak data.
 *
 * Returns a pointer to the node device, or NULL in case of failure
 */
virNodeDevicePtr
virGetNodeDevice(virConnectPtr conn, const char *name)
{
    virNodeDevicePtr ret = NULL;

    if ((!VIR_IS_CONNECT(conn)) || (name == NULL)) {
1049
        virLibConnError(NULL, VIR_ERR_INVALID_ARG, __FUNCTION__);
1050 1051
        return(NULL);
    }
1052
    virMutexLock(&conn->lock);
1053 1054 1055

    ret = (virNodeDevicePtr) virHashLookup(conn->nodeDevices, name);
    if (ret == NULL) {
1056 1057
        if (VIR_ALLOC(ret) < 0) {
            virMutexUnlock(&conn->lock);
1058
            virReportOOMError(conn);
1059 1060 1061 1062 1063 1064
            goto error;
        }
        ret->magic = VIR_NODE_DEVICE_MAGIC;
        ret->conn = conn;
        ret->name = strdup(name);
        if (ret->name == NULL) {
1065
            virMutexUnlock(&conn->lock);
1066
            virReportOOMError(conn);
1067 1068 1069 1070
            goto error;
        }

        if (virHashAddEntry(conn->nodeDevices, name, ret) < 0) {
1071
            virMutexUnlock(&conn->lock);
1072
            virLibConnError(conn, VIR_ERR_INTERNAL_ERROR,
1073
                            "%s", _("failed to add node dev to conn hash table"));
1074 1075 1076 1077 1078
            goto error;
        }
        conn->refs++;
    }
    ret->refs++;
1079
    virMutexUnlock(&conn->lock);
1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107
    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);

1108 1109
    if (virHashRemoveEntry(conn->nodeDevices, dev->name, NULL) < 0) {
        virMutexUnlock(&conn->lock);
1110
        virLibConnError(conn, VIR_ERR_INTERNAL_ERROR,
1111
                        "%s", _("dev missing from connection hash table"));
1112 1113
        conn = NULL;
    }
1114 1115 1116

    dev->magic = -1;
    VIR_FREE(dev->name);
1117
    VIR_FREE(dev->parent);
1118 1119
    VIR_FREE(dev);

1120 1121 1122 1123 1124 1125 1126 1127 1128
    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);
1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145
    }
}


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

1146
    virMutexLock(&dev->conn->lock);
1147 1148 1149 1150 1151 1152 1153 1154 1155
    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);
    }

1156
    virMutexUnlock(&dev->conn->lock);
1157 1158
    return (refs);
}
1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172

/**
 * 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
 * table. In any case a corresponding call to virFreeSecret() is needed to not
 * leak data.
 *
 * Returns a pointer to the secret, or NULL in case of failure
 */
virSecretPtr
1173 1174
virGetSecret(virConnectPtr conn, const unsigned char *uuid,
             int usageType, const char *usageID)
1175 1176
{
    virSecretPtr ret = NULL;
1177
    char uuidstr[VIR_UUID_STRING_BUFLEN];
1178

1179
    if (!VIR_IS_CONNECT(conn) || uuid == NULL || usageID == NULL) {
1180 1181 1182 1183 1184
        virLibConnError(NULL, VIR_ERR_INVALID_ARG, __FUNCTION__);
        return NULL;
    }
    virMutexLock(&conn->lock);

1185 1186 1187
    virUUIDFormat(uuid, uuidstr);

    ret = virHashLookup(conn->secrets, uuidstr);
1188 1189 1190 1191 1192 1193 1194 1195
    if (ret == NULL) {
        if (VIR_ALLOC(ret) < 0) {
            virMutexUnlock(&conn->lock);
            virReportOOMError(conn);
            goto error;
        }
        ret->magic = VIR_SECRET_MAGIC;
        ret->conn = conn;
1196
        memcpy(&(ret->uuid[0]), uuid, VIR_UUID_BUFLEN);
1197 1198 1199 1200 1201 1202
        ret->usageType = usageType;
        if (!(ret->usageID = strdup(usageID))) {
            virMutexUnlock(&conn->lock);
            virReportOOMError(conn);
            goto error;
        }
1203
        if (virHashAddEntry(conn->secrets, uuidstr, ret) < 0) {
1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216
            virMutexUnlock(&conn->lock);
            virLibConnError(conn, VIR_ERR_INTERNAL_ERROR,
                            "%s", _("failed to add secret to conn hash table"));
            goto error;
        }
        conn->refs++;
    }
    ret->refs++;
    virMutexUnlock(&conn->lock);
    return ret;

error:
    if (ret != NULL) {
1217
        VIR_FREE(ret->usageID);
1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237
        VIR_FREE(ret->uuid);
        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;
1238 1239
    char uuidstr[VIR_UUID_STRING_BUFLEN];
    DEBUG("release secret %p %p", secret, secret->uuid);
1240

1241 1242
    virUUIDFormat(secret->uuid, uuidstr);
    if (virHashRemoveEntry(conn->secrets, uuidstr, NULL) < 0) {
1243 1244 1245 1246 1247 1248
        virMutexUnlock(&conn->lock);
        virLibConnError(conn, VIR_ERR_INTERNAL_ERROR,
                        "%s", _("secret missing from connection hash table"));
        conn = NULL;
    }

1249
    VIR_FREE(secret->usageID);
1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282
    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)) {
        virLibConnError(NULL, VIR_ERR_INVALID_ARG, __FUNCTION__);
        return -1;
    }
    virMutexLock(&secret->conn->lock);
1283
    DEBUG("unref secret %p %p %d", secret, secret->uuid, secret->refs);
1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294
    secret->refs--;
    refs = secret->refs;
    if (refs == 0) {
        virReleaseSecret(secret);
        /* Already unlocked mutex */
        return 0;
    }

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