2018-01-09 23:50:57 +08:00
|
|
|
/*
|
|
|
|
* Block layer code related to image creation
|
|
|
|
*
|
|
|
|
* Copyright (c) 2018 Kevin Wolf <kwolf@redhat.com>
|
|
|
|
*
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
* of this software and associated documentation files (the "Software"), to deal
|
|
|
|
* in the Software without restriction, including without limitation the rights
|
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
* copies of the Software, and to permit persons to whom the Software is
|
|
|
|
* furnished to do so, subject to the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice shall be included in
|
|
|
|
* all copies or substantial portions of the Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
|
|
|
|
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
* THE SOFTWARE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "qemu/osdep.h"
|
|
|
|
#include "block/block_int.h"
|
2018-01-18 21:33:04 +08:00
|
|
|
#include "qemu/job.h"
|
2018-01-09 23:50:57 +08:00
|
|
|
#include "qapi/qapi-commands-block-core.h"
|
2018-01-18 21:33:04 +08:00
|
|
|
#include "qapi/qapi-visit-block-core.h"
|
|
|
|
#include "qapi/clone-visitor.h"
|
2018-01-09 23:50:57 +08:00
|
|
|
#include "qapi/error.h"
|
|
|
|
|
2018-01-18 21:33:04 +08:00
|
|
|
typedef struct BlockdevCreateJob {
|
|
|
|
Job common;
|
2018-01-09 23:50:57 +08:00
|
|
|
BlockDriver *drv;
|
|
|
|
BlockdevCreateOptions *opts;
|
2018-01-18 21:33:04 +08:00
|
|
|
} BlockdevCreateJob;
|
2018-01-09 23:50:57 +08:00
|
|
|
|
2018-08-30 09:57:26 +08:00
|
|
|
static int coroutine_fn blockdev_create_run(Job *job, Error **errp)
|
2018-01-09 23:50:57 +08:00
|
|
|
{
|
2018-08-30 09:57:26 +08:00
|
|
|
BlockdevCreateJob *s = container_of(job, BlockdevCreateJob, common);
|
2018-08-30 09:57:31 +08:00
|
|
|
int ret;
|
2018-01-18 21:33:04 +08:00
|
|
|
|
|
|
|
job_progress_set_remaining(&s->common, 1);
|
2018-08-30 09:57:31 +08:00
|
|
|
ret = s->drv->bdrv_co_create(s->opts, errp);
|
2018-01-18 21:33:04 +08:00
|
|
|
job_progress_update(&s->common, 1);
|
|
|
|
|
|
|
|
qapi_free_BlockdevCreateOptions(s->opts);
|
2018-08-30 09:57:26 +08:00
|
|
|
|
2018-08-30 09:57:31 +08:00
|
|
|
return ret;
|
2018-01-18 21:33:04 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static const JobDriver blockdev_create_job_driver = {
|
|
|
|
.instance_size = sizeof(BlockdevCreateJob),
|
|
|
|
.job_type = JOB_TYPE_CREATE,
|
2018-08-30 09:57:26 +08:00
|
|
|
.run = blockdev_create_run,
|
2018-01-18 21:33:04 +08:00
|
|
|
};
|
|
|
|
|
2018-05-26 00:24:51 +08:00
|
|
|
void qmp_blockdev_create(const char *job_id, BlockdevCreateOptions *options,
|
|
|
|
Error **errp)
|
2018-01-18 21:33:04 +08:00
|
|
|
{
|
|
|
|
BlockdevCreateJob *s;
|
2018-01-09 23:50:57 +08:00
|
|
|
const char *fmt = BlockdevDriver_str(options->driver);
|
|
|
|
BlockDriver *drv = bdrv_find_format(fmt);
|
|
|
|
|
|
|
|
/* If the driver is in the schema, we know that it exists. But it may not
|
|
|
|
* be whitelisted. */
|
|
|
|
assert(drv);
|
|
|
|
if (bdrv_uses_whitelist() && !bdrv_is_whitelisted(drv, false)) {
|
|
|
|
error_setg(errp, "Driver is not whitelisted");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-01-18 21:33:04 +08:00
|
|
|
/* Error out if the driver doesn't support .bdrv_co_create */
|
2018-01-09 23:50:57 +08:00
|
|
|
if (!drv->bdrv_co_create) {
|
|
|
|
error_setg(errp, "Driver does not support blockdev-create");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-01-18 21:33:04 +08:00
|
|
|
/* Create the block job */
|
|
|
|
/* TODO Running in the main context. Block drivers need to error out or add
|
|
|
|
* locking when they use a BDS in a different AioContext. */
|
|
|
|
s = job_create(job_id, &blockdev_create_job_driver, NULL,
|
|
|
|
qemu_get_aio_context(), JOB_DEFAULT | JOB_MANUAL_DISMISS,
|
|
|
|
NULL, NULL, errp);
|
|
|
|
if (!s) {
|
|
|
|
return;
|
2018-01-09 23:50:57 +08:00
|
|
|
}
|
2018-01-18 21:33:04 +08:00
|
|
|
|
|
|
|
s->drv = drv,
|
|
|
|
s->opts = QAPI_CLONE(BlockdevCreateOptions, options),
|
|
|
|
|
|
|
|
job_start(&s->common);
|
2018-01-09 23:50:57 +08:00
|
|
|
}
|