virlockspace.c 21.1 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
/*
 * virlockspace.c: simple file based lockspaces
 *
 * Copyright (C) 2012 Red Hat, Inc.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library;  If not, see
 * <http://www.gnu.org/licenses/>.
 *
 */

#include <config.h>

#include "virlockspace.h"
25
#include "virlog.h"
26
#include "viralloc.h"
27
#include "virerror.h"
28
#include "virutil.h"
29 30
#include "virfile.h"
#include "virhash.h"
31
#include "virthread.h"
32
#include "virstring.h"
33 34 35 36 37 38 39

#include <fcntl.h>
#include <unistd.h>
#include <sys/stat.h>

#define VIR_FROM_THIS VIR_FROM_LOCKSPACE

40 41
VIR_LOG_INIT("util.lockspace");

42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68
#define VIR_LOCKSPACE_TABLE_SIZE 10

typedef struct _virLockSpaceResource virLockSpaceResource;
typedef virLockSpaceResource *virLockSpaceResourcePtr;

struct _virLockSpaceResource {
    char *name;
    char *path;
    int fd;
    bool lockHeld;
    unsigned int flags;
    size_t nOwners;
    pid_t *owners;
};

struct _virLockSpace {
    char *dir;
    virMutex lock;

    virHashTablePtr resources;
};


static char *virLockSpaceGetResourcePath(virLockSpacePtr lockspace,
                                         const char *resname)
{
    char *ret;
69
    if (lockspace->dir)
70
        ret = g_strdup_printf("%s/%s", lockspace->dir, resname);
71
    else
72
        ret = g_strdup(resname);
73 74 75 76 77 78 79 80 81 82 83 84 85 86
    return ret;
}


static void virLockSpaceResourceFree(virLockSpaceResourcePtr res)
{
    if (!res)
        return;

    if (res->lockHeld &&
        (res->flags & VIR_LOCK_SPACE_ACQUIRE_AUTOCREATE)) {
        if (res->flags & VIR_LOCK_SPACE_ACQUIRE_SHARED) {
            /* We must upgrade to an exclusive lock to ensure
             * no one else still has it before trying to delete */
87
            if (virFileLock(res->fd, false, 0, 1, false) < 0) {
88 89 90 91 92
                VIR_DEBUG("Could not upgrade shared lease to exclusive, not deleting");
            } else {
                if (unlink(res->path) < 0 &&
                    errno != ENOENT) {
                    VIR_WARN("Failed to unlink resource %s: %s",
93
                             res->path, g_strerror(errno));
94 95 96 97 98 99
                }
            }
        } else {
            if (unlink(res->path) < 0 &&
                errno != ENOENT) {
                VIR_WARN("Failed to unlink resource %s: %s",
100
                         res->path, g_strerror(errno));
101 102 103 104
            }
        }
    }

105
    VIR_FREE(res->owners);
106 107 108 109 110 111 112 113 114 115
    VIR_FORCE_CLOSE(res->fd);
    VIR_FREE(res->path);
    VIR_FREE(res->name);
    VIR_FREE(res);
}


static virLockSpaceResourcePtr
virLockSpaceResourceNew(virLockSpacePtr lockspace,
                        const char *resname,
116 117
                        unsigned int flags,
                        pid_t owner)
118 119 120 121 122 123 124 125 126 127
{
    virLockSpaceResourcePtr res;
    bool shared = !!(flags & VIR_LOCK_SPACE_ACQUIRE_SHARED);

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

    res->fd = -1;
    res->flags = flags;

128
    res->name = g_strdup(resname);
129 130

    if (!(res->path = virLockSpaceGetResourcePath(lockspace, resname)))
131
        goto error;
132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156

    if (flags & VIR_LOCK_SPACE_ACQUIRE_AUTOCREATE) {
        while (1) {
            struct stat a, b;
            if ((res->fd = open(res->path, O_RDWR|O_CREAT, 0600)) < 0) {
                virReportSystemError(errno,
                                     _("Unable to open/create resource %s"),
                                     res->path);
                goto error;
            }

            if (virSetCloseExec(res->fd) < 0) {
                virReportSystemError(errno,
                                     _("Failed to set close-on-exec flag '%s'"),
                                     res->path);
                goto error;
            }

            if (fstat(res->fd, &b) < 0) {
                virReportSystemError(errno,
                                     _("Unable to check status of pid file '%s'"),
                                     res->path);
                goto error;
            }

157
            if (virFileLock(res->fd, shared, 0, 1, false) < 0) {
158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174
                if (errno == EACCES || errno == EAGAIN) {
                    virReportError(VIR_ERR_RESOURCE_BUSY,
                                   _("Lockspace resource '%s' is locked"),
                                   resname);
                } else {
                    virReportSystemError(errno,
                                         _("Unable to acquire lock on '%s'"),
                                         res->path);
                }
                goto error;
            }

            /* Now make sure the pidfile we locked is the same
             * one that now exists on the filesystem
             */
            if (stat(res->path, &a) < 0) {
                VIR_DEBUG("Resource '%s' disappeared: %s",
175
                          res->path, g_strerror(errno));
176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202
                VIR_FORCE_CLOSE(res->fd);
                /* Someone else must be racing with us, so try again */
                continue;
            }

            if (a.st_ino == b.st_ino)
                break;

            VIR_DEBUG("Resource '%s' was recreated", res->path);
            VIR_FORCE_CLOSE(res->fd);
            /* Someone else must be racing with us, so try again */
        }
    } else {
        if ((res->fd = open(res->path, O_RDWR)) < 0) {
            virReportSystemError(errno,
                                 _("Unable to open resource %s"),
                                 res->path);
            goto error;
        }

        if (virSetCloseExec(res->fd) < 0) {
            virReportSystemError(errno,
                                 _("Failed to set close-on-exec flag '%s'"),
                                 res->path);
            goto error;
        }

203
        if (virFileLock(res->fd, shared, 0, 1, false) < 0) {
204 205 206 207 208 209 210 211 212 213 214 215 216 217 218
            if (errno == EACCES || errno == EAGAIN) {
                virReportError(VIR_ERR_RESOURCE_BUSY,
                               _("Lockspace resource '%s' is locked"),
                               resname);
            } else {
                virReportSystemError(errno,
                                     _("Unable to acquire lock on '%s'"),
                                     res->path);
            }
            goto error;
        }
    }
    res->lockHeld = true;

    if (VIR_EXPAND_N(res->owners, res->nOwners, 1) < 0)
219
        goto error;
220 221 222 223 224

    res->owners[res->nOwners-1] = owner;

    return res;

225
 error:
226 227 228 229 230
    virLockSpaceResourceFree(res);
    return NULL;
}


231
static void virLockSpaceResourceDataFree(void *opaque)
232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253
{
    virLockSpaceResourcePtr res = opaque;
    virLockSpaceResourceFree(res);
}


virLockSpacePtr virLockSpaceNew(const char *directory)
{
    virLockSpacePtr lockspace;

    VIR_DEBUG("directory=%s", NULLSTR(directory));

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

    if (virMutexInit(&lockspace->lock) < 0) {
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                       _("Unable to initialize lockspace mutex"));
        VIR_FREE(lockspace);
        return NULL;
    }

254
    lockspace->dir = g_strdup(directory);
255 256 257 258 259 260 261 262 263 264

    if (!(lockspace->resources = virHashCreate(VIR_LOCKSPACE_TABLE_SIZE,
                                               virLockSpaceResourceDataFree)))
        goto error;

    if (directory) {
        if (virFileExists(directory)) {
            if (!virFileIsDir(directory)) {
                virReportError(VIR_ERR_INTERNAL_ERROR,
                               _("Lockspace location %s exists, but is not a directory"),
265
                               directory);
266 267 268 269 270 271 272 273 274 275 276 277 278 279
                goto error;
            }
        } else {
            if (virFileMakePathWithMode(directory, 0700) < 0) {
                virReportSystemError(errno,
                                     _("Unable to create lockspace %s"),
                                     directory);
                goto error;
            }
        }
    }

    return lockspace;

280
 error:
281 282 283 284 285
    virLockSpaceFree(lockspace);
    return NULL;
}


286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310

virLockSpacePtr virLockSpaceNewPostExecRestart(virJSONValuePtr object)
{
    virLockSpacePtr lockspace;
    virJSONValuePtr resources;
    size_t i;

    VIR_DEBUG("object=%p", object);

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

    if (virMutexInit(&lockspace->lock) < 0) {
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                       _("Unable to initialize lockspace mutex"));
        VIR_FREE(lockspace);
        return NULL;
    }

    if (!(lockspace->resources = virHashCreate(VIR_LOCKSPACE_TABLE_SIZE,
                                               virLockSpaceResourceDataFree)))
        goto error;

    if (virJSONValueObjectHasKey(object, "directory")) {
        const char *dir = virJSONValueObjectGetString(object, "directory");
311
        lockspace->dir = g_strdup(dir);
312 313 314 315 316 317 318 319
    }

    if (!(resources = virJSONValueObjectGet(object, "resources"))) {
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                       _("Missing resources value in JSON document"));
        goto error;
    }

320
    if (!virJSONValueIsArray(resources)) {
321
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
322
                       _("Malformed resources array"));
323 324 325
        goto error;
    }

326
    for (i = 0; i < virJSONValueArraySize(resources); i++) {
327 328 329 330 331
        virJSONValuePtr child = virJSONValueArrayGet(resources, i);
        virLockSpaceResourcePtr res;
        const char *tmp;
        virJSONValuePtr owners;
        size_t j;
332
        size_t m;
333

334
        if (VIR_ALLOC(res) < 0)
335 336 337 338 339 340 341 342 343
            goto error;
        res->fd = -1;

        if (!(tmp = virJSONValueObjectGetString(child, "name"))) {
            virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                           _("Missing resource name in JSON document"));
            virLockSpaceResourceFree(res);
            goto error;
        }
344
        res->name = g_strdup(tmp);
345 346 347 348 349 350 351

        if (!(tmp = virJSONValueObjectGetString(child, "path"))) {
            virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                           _("Missing resource path in JSON document"));
            virLockSpaceResourceFree(res);
            goto error;
        }
352
        res->path = g_strdup(tmp);
353 354 355 356 357 358 359 360 361
        if (virJSONValueObjectGetNumberInt(child, "fd", &res->fd) < 0) {
            virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                           _("Missing resource fd in JSON document"));
            virLockSpaceResourceFree(res);
            goto error;
        }
        if (virSetInherit(res->fd, false) < 0) {
            virReportSystemError(errno, "%s",
                                 _("Cannot enable close-on-exec flag"));
362
            virLockSpaceResourceFree(res);
363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385
            goto error;
        }
        if (virJSONValueObjectGetBoolean(child, "lockHeld", &res->lockHeld) < 0) {
            virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                           _("Missing resource lockHeld in JSON document"));
            virLockSpaceResourceFree(res);
            goto error;
        }

        if (virJSONValueObjectGetNumberUint(child, "flags", &res->flags) < 0) {
            virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                           _("Missing resource flags in JSON document"));
            virLockSpaceResourceFree(res);
            goto error;
        }

        if (!(owners = virJSONValueObjectGet(child, "owners"))) {
            virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                           _("Missing resource owners in JSON document"));
            virLockSpaceResourceFree(res);
            goto error;
        }

386
        if (!virJSONValueIsArray(owners)) {
387
            virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
388
                           _("Malformed owners array"));
389 390 391 392
            virLockSpaceResourceFree(res);
            goto error;
        }

393
        m = virJSONValueArraySize(owners);
394 395 396 397
        if (VIR_ALLOC_N(res->owners, res->nOwners) < 0) {
            virLockSpaceResourceFree(res);
            goto error;
        }
398
        res->nOwners = m;
399

400
        for (j = 0; j < res->nOwners; j++) {
401 402 403 404 405 406 407 408 409 410
            unsigned long long int owner;
            virJSONValuePtr ownerval = virJSONValueArrayGet(owners, j);

            if (virJSONValueGetNumberUlong(ownerval, &owner) < 0) {
                virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                               _("Malformed owner value in JSON document"));
                virLockSpaceResourceFree(res);
                goto error;
            }

411
            res->owners[j] = (pid_t)owner;
412 413 414 415 416 417 418 419 420 421
        }

        if (virHashAddEntry(lockspace->resources, res->name, res) < 0) {
            virLockSpaceResourceFree(res);
            goto error;
        }
    }

    return lockspace;

422
 error:
423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439
    virLockSpaceFree(lockspace);
    return NULL;
}


virJSONValuePtr virLockSpacePreExecRestart(virLockSpacePtr lockspace)
{
    virJSONValuePtr object = virJSONValueNewObject();
    virJSONValuePtr resources;
    virHashKeyValuePairPtr pairs = NULL, tmp;

    virMutexLock(&lockspace->lock);

    if (lockspace->dir &&
        virJSONValueObjectAppendString(object, "directory", lockspace->dir) < 0)
        goto error;

440
    resources = virJSONValueNewArray();
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 468 469 470 471

    if (virJSONValueObjectAppend(object, "resources", resources) < 0) {
        virJSONValueFree(resources);
        goto error;
    }

    tmp = pairs = virHashGetItems(lockspace->resources, NULL);
    while (tmp && tmp->value) {
        virLockSpaceResourcePtr res = (virLockSpaceResourcePtr)tmp->value;
        virJSONValuePtr child = virJSONValueNewObject();
        virJSONValuePtr owners = NULL;
        size_t i;

        if (virJSONValueArrayAppend(resources, child) < 0) {
            virJSONValueFree(child);
            goto error;
        }

        if (virJSONValueObjectAppendString(child, "name", res->name) < 0 ||
            virJSONValueObjectAppendString(child, "path", res->path) < 0 ||
            virJSONValueObjectAppendNumberInt(child, "fd", res->fd) < 0 ||
            virJSONValueObjectAppendBoolean(child, "lockHeld", res->lockHeld) < 0 ||
            virJSONValueObjectAppendNumberUint(child, "flags", res->flags) < 0)
            goto error;

        if (virSetInherit(res->fd, true) < 0) {
            virReportSystemError(errno, "%s",
                                 _("Cannot disable close-on-exec flag"));
            goto error;
        }

472
        owners = virJSONValueNewArray();
473 474 475 476 477 478

        if (virJSONValueObjectAppend(child, "owners", owners) < 0) {
            virJSONValueFree(owners);
            goto error;
        }

479
        for (i = 0; i < res->nOwners; i++) {
480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496
            virJSONValuePtr owner = virJSONValueNewNumberUlong(res->owners[i]);
            if (!owner)
                goto error;

            if (virJSONValueArrayAppend(owners, owner) < 0) {
                virJSONValueFree(owner);
                goto error;
            }
        }

        tmp++;
    }
    VIR_FREE(pairs);

    virMutexUnlock(&lockspace->lock);
    return object;

497
 error:
498 499 500 501 502 503 504
    VIR_FREE(pairs);
    virJSONValueFree(object);
    virMutexUnlock(&lockspace->lock);
    return NULL;
}


505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532
void virLockSpaceFree(virLockSpacePtr lockspace)
{
    if (!lockspace)
        return;

    virHashFree(lockspace->resources);
    VIR_FREE(lockspace->dir);
    virMutexDestroy(&lockspace->lock);
    VIR_FREE(lockspace);
}


const char *virLockSpaceGetDirectory(virLockSpacePtr lockspace)
{
    return lockspace->dir;
}


int virLockSpaceCreateResource(virLockSpacePtr lockspace,
                               const char *resname)
{
    int ret = -1;
    char *respath = NULL;

    VIR_DEBUG("lockspace=%p resname=%s", lockspace, resname);

    virMutexLock(&lockspace->lock);

533
    if (virHashLookup(lockspace->resources, resname) != NULL) {
534 535 536 537 538 539 540 541 542 543 544 545 546 547
        virReportError(VIR_ERR_RESOURCE_BUSY,
                       _("Lockspace resource '%s' is locked"),
                       resname);
        goto cleanup;
    }

    if (!(respath = virLockSpaceGetResourcePath(lockspace, resname)))
        goto cleanup;

    if (virFileTouch(respath, 0600) < 0)
        goto cleanup;

    ret = 0;

548
 cleanup:
549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564
    virMutexUnlock(&lockspace->lock);
    VIR_FREE(respath);
    return ret;
}


int virLockSpaceDeleteResource(virLockSpacePtr lockspace,
                               const char *resname)
{
    int ret = -1;
    char *respath = NULL;

    VIR_DEBUG("lockspace=%p resname=%s", lockspace, resname);

    virMutexLock(&lockspace->lock);

565
    if (virHashLookup(lockspace->resources, resname) != NULL) {
566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584
        virReportError(VIR_ERR_RESOURCE_BUSY,
                       _("Lockspace resource '%s' is locked"),
                       resname);
        goto cleanup;
    }

    if (!(respath = virLockSpaceGetResourcePath(lockspace, resname)))
        goto cleanup;

    if (unlink(respath) < 0 &&
        errno != ENOENT) {
        virReportSystemError(errno,
                             _("Unable to delete lockspace resource %s"),
                             respath);
        goto cleanup;
    }

    ret = 0;

585
 cleanup:
586 587 588 589 590 591 592 593 594 595 596 597 598 599
    virMutexUnlock(&lockspace->lock);
    VIR_FREE(respath);
    return ret;
}


int virLockSpaceAcquireResource(virLockSpacePtr lockspace,
                                const char *resname,
                                pid_t owner,
                                unsigned int flags)
{
    int ret = -1;
    virLockSpaceResourcePtr res;

600
    VIR_DEBUG("lockspace=%p resname=%s flags=0x%x owner=%lld",
601 602 603 604 605 606 607 608 609 610 611
              lockspace, resname, flags, (unsigned long long)owner);

    virCheckFlags(VIR_LOCK_SPACE_ACQUIRE_SHARED |
                  VIR_LOCK_SPACE_ACQUIRE_AUTOCREATE, -1);

    virMutexLock(&lockspace->lock);

    if ((res = virHashLookup(lockspace->resources, resname))) {
        if ((res->flags & VIR_LOCK_SPACE_ACQUIRE_SHARED) &&
            (flags & VIR_LOCK_SPACE_ACQUIRE_SHARED)) {

612
            if (VIR_EXPAND_N(res->owners, res->nOwners, 1) < 0)
613 614 615 616 617 618 619 620 621 622 623
                goto cleanup;
            res->owners[res->nOwners-1] = owner;

            goto done;
        }
        virReportError(VIR_ERR_RESOURCE_BUSY,
                       _("Lockspace resource '%s' is locked"),
                       resname);
        goto cleanup;
    }

624
    if (!(res = virLockSpaceResourceNew(lockspace, resname, flags, owner)))
625 626 627 628 629 630 631
        goto cleanup;

    if (virHashAddEntry(lockspace->resources, resname, res) < 0) {
        virLockSpaceResourceFree(res);
        goto cleanup;
    }

632
 done:
633 634
    ret = 0;

635
 cleanup:
636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660
    virMutexUnlock(&lockspace->lock);
    return ret;
}


int virLockSpaceReleaseResource(virLockSpacePtr lockspace,
                                const char *resname,
                                pid_t owner)
{
    int ret = -1;
    virLockSpaceResourcePtr res;
    size_t i;

    VIR_DEBUG("lockspace=%p resname=%s owner=%lld",
              lockspace, resname, (unsigned long long)owner);

    virMutexLock(&lockspace->lock);

    if (!(res = virHashLookup(lockspace->resources, resname))) {
        virReportError(VIR_ERR_RESOURCE_BUSY,
                       _("Lockspace resource '%s' is not locked"),
                       resname);
        goto cleanup;
    }

661
    for (i = 0; i < res->nOwners; i++) {
662
        if (res->owners[i] == owner)
663 664 665 666 667 668 669 670 671 672
            break;
    }

    if (i == res->nOwners) {
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("owner %lld does not hold the resource lock"),
                       (unsigned long long)owner);
        goto cleanup;
    }

673
    VIR_DELETE_ELEMENT(res->owners, i, res->nOwners);
674 675 676 677 678 679 680

    if ((res->nOwners == 0) &&
        virHashRemoveEntry(lockspace->resources, resname) < 0)
        goto cleanup;

    ret = 0;

681
 cleanup:
682 683 684 685 686 687 688 689 690 691 692 693 694
    virMutexUnlock(&lockspace->lock);
    return ret;
}


struct virLockSpaceRemoveData {
    pid_t owner;
    size_t count;
};


static int
virLockSpaceRemoveResourcesForOwner(const void *payload,
J
Ján Tomko 已提交
695
                                    const void *name G_GNUC_UNUSED,
696 697 698 699 700 701 702 703
                                    const void *opaque)
{
    virLockSpaceResourcePtr res = (virLockSpaceResourcePtr)payload;
    struct virLockSpaceRemoveData *data = (struct virLockSpaceRemoveData *)opaque;
    size_t i;

    VIR_DEBUG("res %s owner %lld", res->name, (unsigned long long)data->owner);

704
    for (i = 0; i < res->nOwners; i++) {
705
        if (res->owners[i] == data->owner)
706 707 708 709 710 711 712 713
            break;
    }

    if (i == res->nOwners)
        return 0;

    data->count++;

714
    VIR_DELETE_ELEMENT(res->owners, i, res->nOwners);
715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747

    if (res->nOwners) {
        VIR_DEBUG("Other shared owners remain");
        return 0;
    }

    VIR_DEBUG("No more owners, remove it");
    return 1;
}


int virLockSpaceReleaseResourcesForOwner(virLockSpacePtr lockspace,
                                         pid_t owner)
{
    int ret = 0;
    struct virLockSpaceRemoveData data = {
        owner, 0
    };

    VIR_DEBUG("lockspace=%p owner=%lld", lockspace, (unsigned long long)owner);

    virMutexLock(&lockspace->lock);

    if (virHashRemoveSet(lockspace->resources,
                         virLockSpaceRemoveResourcesForOwner,
                         &data) < 0)
        goto error;

    ret = data.count;

    virMutexUnlock(&lockspace->lock);
    return ret;

748
 error:
749 750 751
    virMutexUnlock(&lockspace->lock);
    return -1;
}