datatypes.c 28.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 28
#include "memory.h"

29 30
#define VIR_FROM_THIS VIR_FROM_NONE

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

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

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

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

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

/**
 * 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)
{
108
    return (virUnrefStorageVol(vol));
109 110 111 112 113 114 115 116 117 118 119 120 121 122
}

/**
 * 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) {
123
        virReportOOMError(NULL);
124 125
        goto failed;
    }
126 127 128 129 130
    if (virMutexInit(&ret->lock) < 0) {
        VIR_FREE(ret);
        goto failed;
    }

131 132 133 134 135
    ret->magic = VIR_CONNECT_MAGIC;
    ret->driver = NULL;
    ret->networkDriver = NULL;
    ret->privateData = NULL;
    ret->networkPrivateData = NULL;
D
Daniel Veillard 已提交
136
    ret->interfacePrivateData = NULL;
137 138 139 140 141 142
    ret->domains = virHashCreate(20);
    if (ret->domains == NULL)
        goto failed;
    ret->networks = virHashCreate(20);
    if (ret->networks == NULL)
        goto failed;
D
Daniel Veillard 已提交
143 144 145
    ret->interfaces = virHashCreate(20);
    if (ret->interfaces == NULL)
        goto failed;
146 147 148 149 150 151
    ret->storagePools = virHashCreate(20);
    if (ret->storagePools == NULL)
        goto failed;
    ret->storageVols = virHashCreate(20);
    if (ret->storageVols == NULL)
        goto failed;
152 153 154
    ret->nodeDevices = virHashCreate(256);
    if (ret->nodeDevices == NULL)
        goto failed;
155 156 157 158 159 160 161 162 163 164

    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 已提交
165 166
        if (ret->interfaces != NULL)
           virHashFree(ret->interfaces, (virHashDeallocator) virInterfaceFreeName);
167 168 169 170
        if (ret->storagePools != NULL)
            virHashFree(ret->storagePools, (virHashDeallocator) virStoragePoolFreeName);
        if (ret->storageVols != NULL)
            virHashFree(ret->storageVols, (virHashDeallocator) virStorageVolFreeName);
171 172
        if (ret->nodeDevices != NULL)
            virHashFree(ret->nodeDevices, (virHashDeallocator) virNodeDeviceFree);
173

174
        virMutexDestroy(&ret->lock);
175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190
        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) {
191
    DEBUG("release connection %p", conn);
192 193 194 195
    if (conn->domains != NULL)
        virHashFree(conn->domains, (virHashDeallocator) virDomainFreeName);
    if (conn->networks != NULL)
        virHashFree(conn->networks, (virHashDeallocator) virNetworkFreeName);
D
Daniel Veillard 已提交
196 197
    if (conn->interfaces != NULL)
        virHashFree(conn->interfaces, (virHashDeallocator) virInterfaceFreeName);
198 199 200 201
    if (conn->storagePools != NULL)
        virHashFree(conn->storagePools, (virHashDeallocator) virStoragePoolFreeName);
    if (conn->storageVols != NULL)
        virHashFree(conn->storageVols, (virHashDeallocator) virStorageVolFreeName);
202 203
    if (conn->nodeDevices != NULL)
        virHashFree(conn->nodeDevices, (virHashDeallocator) virNodeDeviceFree);
204 205 206

    virResetError(&conn->err);

207
    xmlFreeURI(conn->uri);
208

209 210
    virMutexUnlock(&conn->lock);
    virMutexDestroy(&conn->lock);
211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227
    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))) {
228
        virLibConnError(NULL, VIR_ERR_INVALID_ARG, __FUNCTION__);
229 230
        return(-1);
    }
231
    virMutexLock(&conn->lock);
232
    DEBUG("unref connection %p %d", conn, conn->refs);
233 234 235 236 237 238 239
    conn->refs--;
    refs = conn->refs;
    if (refs == 0) {
        virReleaseConnect(conn);
        /* Already unlocked mutex */
        return (0);
    }
240
    virMutexUnlock(&conn->lock);
241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257
    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 已提交
258
virGetDomain(virConnectPtr conn, const char *name, const unsigned char *uuid) {
259 260 261
    virDomainPtr ret = NULL;

    if ((!VIR_IS_CONNECT(conn)) || (name == NULL) || (uuid == NULL)) {
262
        virLibConnError(NULL, VIR_ERR_INVALID_ARG, __FUNCTION__);
263 264
        return(NULL);
    }
265
    virMutexLock(&conn->lock);
266 267 268 269 270 271 272

    /* 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) {
273
            virReportOOMError(conn);
274 275 276 277
            goto error;
        }
        ret->name = strdup(name);
        if (ret->name == NULL) {
278
            virReportOOMError(conn);
279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297
            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) {
            virLibConnError(conn, VIR_ERR_INTERNAL_ERROR,
                            _("failed to add domain to connection hash table"));
            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++;
298
    virMutexUnlock(&conn->lock);
299 300 301
    return(ret);

 error:
302
    virMutexUnlock(&conn->lock);
303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336
    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 */
    if (virHashRemoveEntry(conn->domains, domain->name, NULL) < 0)
        virLibConnError(conn, VIR_ERR_INTERNAL_ERROR,
                        _("domain missing from connection hash table"));

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

337
    DEBUG("unref connection %p %d", conn, conn->refs);
338 339 340 341 342 343 344
    conn->refs--;
    if (conn->refs == 0) {
        virReleaseConnect(conn);
        /* Already unlocked mutex */
        return;
    }

345
    virMutexUnlock(&conn->lock);
346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362
}


/**
 * 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)) {
363
        virLibConnError(NULL, VIR_ERR_INVALID_ARG, __FUNCTION__);
364 365
        return(-1);
    }
366
    virMutexLock(&domain->conn->lock);
367 368 369 370 371 372 373 374 375
    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);
    }

376
    virMutexUnlock(&domain->conn->lock);
377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393
    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 已提交
394
virGetNetwork(virConnectPtr conn, const char *name, const unsigned char *uuid) {
395 396 397
    virNetworkPtr ret = NULL;

    if ((!VIR_IS_CONNECT(conn)) || (name == NULL) || (uuid == NULL)) {
398
        virLibConnError(NULL, VIR_ERR_INVALID_ARG, __FUNCTION__);
399 400
        return(NULL);
    }
401
    virMutexLock(&conn->lock);
402 403 404 405 406 407 408

    /* 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) {
409
            virReportOOMError(conn);
410 411 412 413
            goto error;
        }
        ret->name = strdup(name);
        if (ret->name == NULL) {
414
            virReportOOMError(conn);
415 416 417 418 419 420 421 422 423 424 425 426 427 428 429
            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) {
            virLibConnError(conn, VIR_ERR_INTERNAL_ERROR,
                            _("failed to add network to connection hash table"));
            goto error;
        }
        conn->refs++;
    }
    ret->refs++;
430
    virMutexUnlock(&conn->lock);
431 432 433
    return(ret);

 error:
434
    virMutexUnlock(&conn->lock);
435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467
    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 */
    if (virHashRemoveEntry(conn->networks, network->name, NULL) < 0)
        virLibConnError(conn, VIR_ERR_INTERNAL_ERROR,
                        _("network missing from connection hash table"));

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

468
    DEBUG("unref connection %p %d", conn, conn->refs);
469 470 471 472 473 474 475
    conn->refs--;
    if (conn->refs == 0) {
        virReleaseConnect(conn);
        /* Already unlocked mutex */
        return;
    }

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


/**
 * 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)) {
494
        virLibConnError(NULL, VIR_ERR_INVALID_ARG, __FUNCTION__);
495 496
        return(-1);
    }
497
    virMutexLock(&network->conn->lock);
498 499 500 501 502 503 504 505 506
    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);
    }

507
    virMutexUnlock(&network->conn->lock);
508 509 510 511
    return (refs);
}


D
Daniel Veillard 已提交
512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 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 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 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 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649
/**
 * 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,
 * 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
 * virUnrefInterface() is needed to not leak data.
 *
 * 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);

    /* TODO search by MAC first as they are better differentiators */

    ret = (virInterfacePtr) virHashLookup(conn->interfaces, name);
    /* TODO check the MAC */
    if (ret == NULL) {
        if (VIR_ALLOC(ret) < 0) {
            virReportOOMError(conn);
            goto error;
        }
        ret->name = strdup(name);
        if (ret->name == NULL) {
            virReportOOMError(conn);
            goto error;
        }
        ret->mac = strdup(mac);
        if (ret->mac == NULL) {
            virReportOOMError(conn);
            goto error;
        }

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

        if (virHashAddEntry(conn->interfaces, name, ret) < 0) {
            virLibConnError(conn, VIR_ERR_INTERNAL_ERROR,
                            _("failed to add interface to connection hash table"));
            goto error;
        }
        conn->refs++;
    }
    ret->refs++;
    virMutexUnlock(&conn->lock);
    return(ret);

 error:
    virMutexUnlock(&conn->lock);
    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
virReleaseInterface(virInterfacePtr interface) {
    virConnectPtr conn = interface->conn;
    DEBUG("release interface %p %s", interface, interface->name);

    /* TODO search by MAC first as they are better differenciators */
    if (virHashRemoveEntry(conn->interfaces, interface->name, NULL) < 0)
        virLibConnError(conn, VIR_ERR_INTERNAL_ERROR,
                        _("interface missing from connection hash table"));

    interface->magic = -1;
    VIR_FREE(interface->name);
    VIR_FREE(interface->mac);
    VIR_FREE(interface);

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

    virMutexUnlock(&conn->lock);
}


/**
 * 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
virUnrefInterface(virInterfacePtr interface) {
    int refs;

    if (!VIR_IS_CONNECTED_INTERFACE(interface)) {
        virLibConnError(NULL, VIR_ERR_INVALID_ARG, __FUNCTION__);
        return(-1);
    }
    virMutexLock(&interface->conn->lock);
    DEBUG("unref interface %p %s %d", interface, interface->name, interface->refs);
    interface->refs--;
    refs = interface->refs;
    if (refs == 0) {
        virReleaseInterface(interface);
        /* Already unlocked mutex */
        return (0);
    }

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


650 651 652 653 654 655 656 657 658 659 660 661 662 663
/**
 * 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 已提交
664
virGetStoragePool(virConnectPtr conn, const char *name, const unsigned char *uuid) {
665 666 667
    virStoragePoolPtr ret = NULL;

    if ((!VIR_IS_CONNECT(conn)) || (name == NULL) || (uuid == NULL)) {
668
        virLibConnError(NULL, VIR_ERR_INVALID_ARG, __FUNCTION__);
669 670
        return(NULL);
    }
671
    virMutexLock(&conn->lock);
672 673 674 675 676 677 678

    /* 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) {
679
            virReportOOMError(conn);
680 681 682 683
            goto error;
        }
        ret->name = strdup(name);
        if (ret->name == NULL) {
684
            virReportOOMError(conn);
685 686 687 688 689 690 691 692 693 694 695 696 697 698 699
            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) {
            virLibConnError(conn, VIR_ERR_INTERNAL_ERROR,
                            _("failed to add storage pool to connection hash table"));
            goto error;
        }
        conn->refs++;
    }
    ret->refs++;
700
    virMutexUnlock(&conn->lock);
701 702 703
    return(ret);

error:
704
    virMutexUnlock(&conn->lock);
705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738
    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 */
    if (virHashRemoveEntry(conn->storagePools, pool->name, NULL) < 0)
        virLibConnError(conn, VIR_ERR_INTERNAL_ERROR,
                        _("pool missing from connection hash table"));

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

739
    DEBUG("unref connection %p %d", conn, conn->refs);
740 741 742 743 744 745 746
    conn->refs--;
    if (conn->refs == 0) {
        virReleaseConnect(conn);
        /* Already unlocked mutex */
        return;
    }

747
    virMutexUnlock(&conn->lock);
748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764
}


/**
 * 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)) {
765
        virLibConnError(NULL, VIR_ERR_INVALID_ARG, __FUNCTION__);
766 767
        return(-1);
    }
768
    virMutexLock(&pool->conn->lock);
769 770 771 772 773 774 775 776 777
    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);
    }

778
    virMutexUnlock(&pool->conn->lock);
779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797
    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 已提交
798
virGetStorageVol(virConnectPtr conn, const char *pool, const char *name, const char *key) {
799 800 801
    virStorageVolPtr ret = NULL;

    if ((!VIR_IS_CONNECT(conn)) || (name == NULL) || (key == NULL)) {
802
        virLibConnError(NULL, VIR_ERR_INVALID_ARG, __FUNCTION__);
803 804
        return(NULL);
    }
805
    virMutexLock(&conn->lock);
806 807 808 809

    ret = (virStorageVolPtr) virHashLookup(conn->storageVols, key);
    if (ret == NULL) {
        if (VIR_ALLOC(ret) < 0) {
810
            virReportOOMError(conn);
811 812 813 814
            goto error;
        }
        ret->pool = strdup(pool);
        if (ret->pool == NULL) {
815
            virReportOOMError(conn);
816 817 818 819
            goto error;
        }
        ret->name = strdup(name);
        if (ret->name == NULL) {
820
            virReportOOMError(conn);
821 822 823 824 825 826 827 828 829 830 831 832 833 834 835
            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) {
            virLibConnError(conn, VIR_ERR_INTERNAL_ERROR,
                            _("failed to add storage vol to connection hash table"));
            goto error;
        }
        conn->refs++;
    }
    ret->refs++;
836
    virMutexUnlock(&conn->lock);
837 838 839
    return(ret);

error:
840
    virMutexUnlock(&conn->lock);
841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876
    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 */
    if (virHashRemoveEntry(conn->storageVols, vol->key, NULL) < 0)
        virLibConnError(conn, VIR_ERR_INTERNAL_ERROR,
                        _("vol missing from connection hash table"));

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

877
    DEBUG("unref connection %p %d", conn, conn->refs);
878 879 880 881 882 883 884
    conn->refs--;
    if (conn->refs == 0) {
        virReleaseConnect(conn);
        /* Already unlocked mutex */
        return;
    }

885
    virMutexUnlock(&conn->lock);
886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902
}


/**
 * 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)) {
903
        virLibConnError(NULL, VIR_ERR_INVALID_ARG, __FUNCTION__);
904 905
        return(-1);
    }
906
    virMutexLock(&vol->conn->lock);
907 908 909 910 911 912 913 914 915
    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);
    }

916
    virMutexUnlock(&vol->conn->lock);
917 918
    return (refs);
}
919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938


/**
 * 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)) {
939
        virLibConnError(NULL, VIR_ERR_INVALID_ARG, __FUNCTION__);
940 941
        return(NULL);
    }
942
    virMutexLock(&conn->lock);
943 944 945 946

    ret = (virNodeDevicePtr) virHashLookup(conn->nodeDevices, name);
    if (ret == NULL) {
       if (VIR_ALLOC(ret) < 0) {
947
            virReportOOMError(conn);
948 949 950 951 952 953
            goto error;
        }
        ret->magic = VIR_NODE_DEVICE_MAGIC;
        ret->conn = conn;
        ret->name = strdup(name);
        if (ret->name == NULL) {
954
            virReportOOMError(conn);
955 956 957 958 959 960 961 962 963 964 965
            goto error;
        }

        if (virHashAddEntry(conn->nodeDevices, name, ret) < 0) {
            virLibConnError(conn, VIR_ERR_INTERNAL_ERROR,
                            _("failed to add node dev to conn hash table"));
            goto error;
        }
        conn->refs++;
    }
    ret->refs++;
966
    virMutexUnlock(&conn->lock);
967 968 969
    return(ret);

error:
970
    virMutexUnlock(&conn->lock);
971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001
    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);

    if (virHashRemoveEntry(conn->nodeDevices, dev->name, NULL) < 0)
        virLibConnError(conn, VIR_ERR_INTERNAL_ERROR,
                        _("dev missing from connection hash table"));

    dev->magic = -1;
    VIR_FREE(dev->name);
1002
    VIR_FREE(dev->parent);
1003 1004 1005 1006 1007 1008 1009 1010 1011 1012
    VIR_FREE(dev);

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

1013
    virMutexUnlock(&conn->lock);
1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029
}


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

1030
    virMutexLock(&dev->conn->lock);
1031 1032 1033 1034 1035 1036 1037 1038 1039
    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);
    }

1040
    virMutexUnlock(&dev->conn->lock);
1041 1042
    return (refs);
}