qemu_blockjob.c 12.0 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 25 26
/*
 * qemu_blockjob.c: helper functions for QEMU block jobs
 *
 * Copyright (C) 2006-2015 Red Hat, Inc.
 * Copyright (C) 2006 Daniel P. Berrange
 *
 * 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 "internal.h"

#include "qemu_blockjob.h"
27
#include "qemu_block.h"
28 29 30 31 32 33 34
#include "qemu_domain.h"

#include "conf/domain_conf.h"
#include "conf/domain_event.h"

#include "virlog.h"
#include "virstoragefile.h"
35 36
#include "virthread.h"
#include "virtime.h"
37
#include "locking/domain_lock.h"
38
#include "viralloc.h"
39 40 41 42 43

#define VIR_FROM_THIS VIR_FROM_QEMU

VIR_LOG_INIT("qemu.qemu_blockjob");

44

45 46 47 48 49
static virClassPtr qemuBlockJobDataClass;


static void
qemuBlockJobDataDispose(void *obj)
50
{
51
    qemuBlockJobDataPtr job = obj;
52 53

    VIR_FREE(job->errmsg);
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68
}


static int
qemuBlockJobDataOnceInit(void)
{
    if (!VIR_CLASS_NEW(qemuBlockJobData, virClassForObject()))
        return -1;

    return 0;
}


VIR_ONCE_GLOBAL_INIT(qemuBlockJobData)

69 70
static qemuBlockJobDataPtr
qemuBlockJobDataNew(qemuBlockJobType type)
71
{
72 73
    qemuBlockJobDataPtr job = NULL;

74 75 76
    if (qemuBlockJobDataInitialize() < 0)
        return NULL;

77 78
    if (!(job = virObjectNew(qemuBlockJobDataClass)))
        return NULL;
79

80
    job->state = QEMU_BLOCKJOB_STATE_NEW;
81
    job->newstate = -1;
82 83 84
    job->type = type;

    return job;
85 86 87 88 89 90 91 92 93 94 95 96
}


/**
 * qemuBlockJobDiskNew:
 * @disk: disk definition
 *
 * Start/associate a new blockjob with @disk.
 *
 * Returns 0 on success and -1 on failure.
 */
qemuBlockJobDataPtr
97 98
qemuBlockJobDiskNew(virDomainDiskDefPtr disk,
                    qemuBlockJobType type)
99
{
100
    qemuBlockJobDataPtr job = NULL;
101

102 103
    if (!(job = qemuBlockJobDataNew(type)))
        return NULL;
104

105 106 107
    job->disk = disk;
    if (disk)
        QEMU_DOMAIN_DISK_PRIVATE(disk)->blockjob = virObjectRef(job);
108

109
    return job;
110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139
}


/**
 * qemuBlockJobDiskGetJob:
 * @disk: disk definition
 *
 * Get a reference to the block job data object associated with @disk.
 */
qemuBlockJobDataPtr
qemuBlockJobDiskGetJob(virDomainDiskDefPtr disk)
{
    qemuBlockJobDataPtr job = QEMU_DOMAIN_DISK_PRIVATE(disk)->blockjob;

    if (!job)
        return NULL;

    return virObjectRef(job);
}


/**
 * qemuBlockJobStarted:
 * @job: job data
 *
 * Mark @job as started in qemu.
 */
void
qemuBlockJobStarted(qemuBlockJobDataPtr job)
{
140
    job->state = QEMU_BLOCKJOB_STATE_RUNNING;
141 142 143
}


144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159
static void
qemuBlockJobTerminate(qemuBlockJobDataPtr job)
{
    qemuDomainDiskPrivatePtr diskPriv;

    if (job->disk) {
        diskPriv = QEMU_DOMAIN_DISK_PRIVATE(job->disk);

        if (job == diskPriv->blockjob) {
            virObjectUnref(diskPriv->blockjob);
            diskPriv->blockjob = NULL;
        }
    }
}


160 161 162 163 164 165 166 167 168 169 170 171 172 173
/**
 * qemuBlockJobStartupFinalize:
 * @job: job being started
 *
 * Cancels and clears the job private data if the job was not started with
 * qemu (see qemuBlockJobStarted) or just clears up the local reference
 * to @job if it was started.
 */
void
qemuBlockJobStartupFinalize(qemuBlockJobDataPtr job)
{
    if (!job)
        return;

174
    if (job->state == QEMU_BLOCKJOB_STATE_NEW)
175
        qemuBlockJobTerminate(job);
176 177 178 179 180

    virObjectUnref(job);
}


181 182 183 184 185 186 187 188
bool
qemuBlockJobIsRunning(qemuBlockJobDataPtr job)
{
    return job->state == QEMU_BLOCKJOB_STATE_RUNNING ||
           job->state == QEMU_BLOCKJOB_STATE_READY;
}


189 190 191 192
/**
 * qemuBlockJobEmitEvents:
 *
 * Emits the VIR_DOMAIN_EVENT_ID_BLOCK_JOB and VIR_DOMAIN_EVENT_ID_BLOCK_JOB_2
193
 * for a block job. The former event is emitted only for local disks.
194 195 196 197 198 199 200 201 202 203 204
 */
static void
qemuBlockJobEmitEvents(virQEMUDriverPtr driver,
                       virDomainObjPtr vm,
                       virDomainDiskDefPtr disk,
                       virDomainBlockJobType type,
                       virConnectDomainEventBlockJobStatus status)
{
    virObjectEventPtr event = NULL;
    virObjectEventPtr event2 = NULL;

205 206 207 208 209 210
    if (virStorageSourceIsLocalStorage(disk->src) &&
        !virStorageSourceIsEmpty(disk->src)) {
        event = virDomainEventBlockJobNewFromObj(vm, virDomainDiskGetSource(disk),
                                                 type, status);
        virObjectEventStateQueue(driver->domainEventState, event);
    }
211 212 213 214 215 216

    event2 = virDomainEventBlockJob2NewFromObj(vm, disk->dst, type, status);
    virObjectEventStateQueue(driver->domainEventState, event2);
}


217 218 219
static void
qemuBlockJobEventProcessLegacyCompleted(virQEMUDriverPtr driver,
                                        virDomainObjPtr vm,
220
                                        qemuBlockJobDataPtr job,
221 222
                                        int asyncJob)
{
223
    virDomainDiskDefPtr disk = job->disk;
224 225
    virDomainDiskDefPtr persistDisk = NULL;

226 227 228
    if (!disk)
        return;

229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279
    if (disk->mirrorState == VIR_DOMAIN_DISK_MIRROR_STATE_PIVOT) {
        if (vm->newDef) {
            virStorageSourcePtr copy = NULL;

            if ((persistDisk = virDomainDiskByName(vm->newDef,
                                                   disk->dst, false))) {
                copy = virStorageSourceCopy(disk->mirror, false);
                if (!copy ||
                    virStorageSourceInitChainElement(copy,
                                                     persistDisk->src,
                                                     true) < 0) {
                    VIR_WARN("Unable to update persistent definition "
                             "on vm %s after block job",
                             vm->def->name);
                    virStorageSourceFree(copy);
                    copy = NULL;
                    persistDisk = NULL;
                }
            }
            if (copy) {
                virStorageSourceFree(persistDisk->src);
                persistDisk->src = copy;
            }
        }

        /* XXX We want to revoke security labels as well as audit that
         * revocation, before dropping the original source.  But it gets
         * tricky if both source and mirror share common backing files (we
         * want to only revoke the non-shared portion of the chain); so for
         * now, we leak the access to the original.  */
        virDomainLockImageDetach(driver->lockManager, vm, disk->src);
        virStorageSourceFree(disk->src);
        disk->src = disk->mirror;
    } else {
        if (disk->mirror) {
            virDomainLockImageDetach(driver->lockManager, vm, disk->mirror);
            virStorageSourceFree(disk->mirror);
        }
    }

    /* Recompute the cached backing chain to match our
     * updates.  Better would be storing the chain ourselves
     * rather than reprobing, but we haven't quite completed
     * that conversion to use our XML tracking. */
    disk->mirror = NULL;
    disk->mirrorState = VIR_DOMAIN_DISK_MIRROR_STATE_NONE;
    disk->mirrorJob = VIR_DOMAIN_BLOCK_JOB_TYPE_UNKNOWN;
    disk->src->id = 0;
    virStorageSourceBackingStoreClear(disk->src);
    ignore_value(qemuDomainDetermineDiskChain(driver, vm, disk, true));
    ignore_value(qemuBlockNodeNamesDetect(driver, vm, asyncJob));
280
    qemuBlockJobTerminate(job);
281 282 283
}


284
/**
285
 * qemuBlockJobEventProcessLegacy:
286 287
 * @driver: qemu driver
 * @vm: domain
288
 * @job: job to process events for
289 290 291 292 293
 *
 * Update disk's mirror state in response to a block job event
 * from QEMU. For mirror state's that must survive libvirt
 * restart, also update the domain's status XML.
 */
294
static void
295 296
qemuBlockJobEventProcessLegacy(virQEMUDriverPtr driver,
                               virDomainObjPtr vm,
297 298
                               qemuBlockJobDataPtr job,
                               int asyncJob)
299 300
{
    virQEMUDriverConfigPtr cfg = virQEMUDriverGetConfig(driver);
301
    virDomainDiskDefPtr disk = job->disk;
302

303
    VIR_DEBUG("disk=%s, mirrorState=%s, type=%d, state=%d, newstate=%d",
304 305
              disk->dst,
              NULLSTR(virDomainDiskMirrorStateTypeToString(disk->mirrorState)),
306
              job->type,
307
              job->state,
308
              job->newstate);
309

310 311 312
    if (job->newstate == -1)
        return;

313
    qemuBlockJobEmitEvents(driver, vm, disk, job->type, job->newstate);
314 315 316

    /* If we completed a block pull or commit, then update the XML
     * to match.  */
317
    switch ((virConnectDomainEventBlockJobStatus) job->newstate) {
318
    case VIR_DOMAIN_BLOCK_JOB_COMPLETED:
319
        qemuBlockJobEventProcessLegacyCompleted(driver, vm, job, asyncJob);
320 321 322 323 324 325 326 327
        break;

    case VIR_DOMAIN_BLOCK_JOB_READY:
        disk->mirrorState = VIR_DOMAIN_DISK_MIRROR_STATE_READY;
        break;

    case VIR_DOMAIN_BLOCK_JOB_FAILED:
    case VIR_DOMAIN_BLOCK_JOB_CANCELED:
328 329 330 331 332
        if (disk->mirror) {
            virDomainLockImageDetach(driver->lockManager, vm, disk->mirror);
            virStorageSourceFree(disk->mirror);
            disk->mirror = NULL;
        }
333
        disk->mirrorState = VIR_DOMAIN_DISK_MIRROR_STATE_NONE;
334
        disk->mirrorJob = VIR_DOMAIN_BLOCK_JOB_TYPE_UNKNOWN;
335
        qemuBlockJobTerminate(job);
336 337 338 339 340 341
        break;

    case VIR_DOMAIN_BLOCK_JOB_LAST:
        break;
    }

342
    job->state = job->newstate;
343
    job->newstate = -1;
344

345 346 347
    if (virDomainSaveStatus(driver->xmlopt, cfg->stateDir, vm, driver->caps) < 0)
        VIR_WARN("Unable to save status on vm %s after block job", vm->def->name);

348
    if (job->newstate == VIR_DOMAIN_BLOCK_JOB_COMPLETED && vm->newDef) {
349 350 351 352
        if (virDomainSaveConfig(cfg->configDir, driver->caps, vm->newDef) < 0)
            VIR_WARN("Unable to update persistent definition on vm %s "
                     "after block job", vm->def->name);
    }
353 354 355

    virObjectUnref(cfg);
}
356 357


358
/**
359
 * qemuBlockJobUpdate:
360
 * @vm: domain
361 362
 * @job: job data
 * @asyncJob: current qemu asynchronous job type
363 364 365 366 367 368 369
 *
 * Update disk's mirror state in response to a block job event stored in
 * blockJobStatus by qemuProcessHandleBlockJob event handler.
 *
 * Returns the block job event processed or -1 if there was no pending event.
 */
int
370 371 372
qemuBlockJobUpdate(virDomainObjPtr vm,
                   qemuBlockJobDataPtr job,
                   int asyncJob)
373 374 375
{
    qemuDomainObjPrivatePtr priv = vm->privateData;

376 377 378 379
    if (job->newstate == -1)
        return -1;

    qemuBlockJobEventProcessLegacy(priv->driver, vm, job, asyncJob);
380

381
    return job->state;
382 383 384
}


385
/**
386 387
 * qemuBlockJobSyncBegin:
 * @job: block job data
388 389 390
 * @disk: domain disk
 *
 * Begin a new synchronous block job for @disk. The synchronous
391
 * block job is ended by a call to qemuBlockJobSyncEndDisk, or by
392 393 394 395
 * the guest quitting.
 *
 * During a synchronous block job, a block job event for @disk
 * will not be processed asynchronously. Instead, it will be
396
 * processed only when qemuBlockJobUpdate or qemuBlockJobSyncEndDisk
397
 * is called.
398 399
 */
void
400
qemuBlockJobSyncBegin(qemuBlockJobDataPtr job)
401
{
402
    const char *diskdst = NULL;
403

404 405 406 407
    if (job->disk)
        diskdst = job->disk->dst;

    VIR_DEBUG("disk=%s", NULLSTR(diskdst));
408
    job->synchronous = true;
409
    job->newstate = -1;
410 411 412 413
}


/**
414
 * qemuBlockJobSyncEndDisk:
415 416 417 418
 * @vm: domain
 * @disk: domain disk
 *
 * End a synchronous block job for @disk. Any pending block job event
419 420 421
 * for the disk is processed. Note that it's not necessary to call this function
 * in case the block job was not started successfully if
 * qemuBlockJobStartupFinalize will be called.
422 423
 */
void
424
qemuBlockJobSyncEndDisk(virDomainObjPtr vm,
425
                        int asyncJob,
426
                        virDomainDiskDefPtr disk)
427
{
428 429 430 431 432
    qemuBlockJobDataPtr job = QEMU_DOMAIN_DISK_PRIVATE(disk)->blockjob;

    if (!job)
        return;

433
    VIR_DEBUG("disk=%s", disk->dst);
434 435
    qemuBlockJobUpdate(vm, job, asyncJob);
    job->synchronous = false;
436
}
437 438 439 440 441 442 443 444 445 446 447 448


qemuBlockJobDataPtr
qemuBlockJobGetByDisk(virDomainDiskDefPtr disk)
{
    qemuBlockJobDataPtr job = QEMU_DOMAIN_DISK_PRIVATE(disk)->blockjob;

    if (!job)
        return NULL;

    return virObjectRef(job);
}