Commit 72daa72e authored by Max Reitz's avatar Max Reitz Committed by Kevin Wolf
Browse files

block: Allow reference for bdrv_file_open()



Allow specifying a reference to an existing block device (by name) for
bdrv_file_open() instead of a filename and/or options.

Signed-off-by: default avatarMax Reitz <mreitz@redhat.com>
Reviewed-by: default avatarKevin Wolf <kwolf@redhat.com>
Signed-off-by: default avatarKevin Wolf <kwolf@redhat.com>
parent 89f2b21e
Loading
Loading
Loading
Loading
+22 −3
Original line number Diff line number Diff line
@@ -858,9 +858,10 @@ free_and_fail:
 * dictionary, it needs to use QINCREF() before calling bdrv_file_open.
 */
int bdrv_file_open(BlockDriverState **pbs, const char *filename,
                   QDict *options, int flags, Error **errp)
                   const char *reference, QDict *options, int flags,
                   Error **errp)
{
    BlockDriverState *bs;
    BlockDriverState *bs = NULL;
    BlockDriver *drv;
    const char *drvname;
    bool allow_protocol_prefix = false;
@@ -872,6 +873,24 @@ int bdrv_file_open(BlockDriverState **pbs, const char *filename,
        options = qdict_new();
    }

    if (reference) {
        if (filename || qdict_size(options)) {
            error_setg(errp, "Cannot reference an existing block device with "
                       "additional options or a new filename");
            return -EINVAL;
        }
        QDECREF(options);

        bs = bdrv_find(reference);
        if (!bs) {
            error_setg(errp, "Cannot find block device '%s'", reference);
            return -ENODEV;
        }
        bdrv_ref(bs);
        *pbs = bs;
        return 0;
    }

    bs = bdrv_new("");
    bs->options = options;
    options = qdict_clone_shallow(options);
@@ -1124,7 +1143,7 @@ int bdrv_open(BlockDriverState *bs, const char *filename, QDict *options,

    qdict_extract_subqdict(options, &file_options, "file.");

    ret = bdrv_file_open(&file, filename, file_options,
    ret = bdrv_file_open(&file, filename, NULL, file_options,
                         bdrv_open_flags(bs, flags | BDRV_O_UNMAP), &local_err);
    if (ret < 0) {
        goto fail;
+1 −1
Original line number Diff line number Diff line
@@ -403,7 +403,7 @@ static int blkdebug_open(BlockDriverState *bs, QDict *options, int flags,
        goto fail;
    }

    ret = bdrv_file_open(&bs->file, filename, NULL, flags, &local_err);
    ret = bdrv_file_open(&bs->file, filename, NULL, NULL, flags, &local_err);
    if (ret < 0) {
        error_propagate(errp, local_err);
        goto fail;
+1 −1
Original line number Diff line number Diff line
@@ -141,7 +141,7 @@ static int blkverify_open(BlockDriverState *bs, QDict *options, int flags,
        goto fail;
    }

    ret = bdrv_file_open(&bs->file, raw, NULL, flags, &local_err);
    ret = bdrv_file_open(&bs->file, raw, NULL, NULL, flags, &local_err);
    if (ret < 0) {
        error_propagate(errp, local_err);
        goto fail;
+2 −1
Original line number Diff line number Diff line
@@ -351,7 +351,8 @@ static int cow_create(const char *filename, QEMUOptionParameter *options,
        return ret;
    }

    ret = bdrv_file_open(&cow_bs, filename, NULL, BDRV_O_RDWR, &local_err);
    ret = bdrv_file_open(&cow_bs, filename, NULL, NULL, BDRV_O_RDWR,
                         &local_err);
    if (ret < 0) {
        qerror_report_err(local_err);
        error_free(local_err);
+2 −1
Original line number Diff line number Diff line
@@ -691,7 +691,8 @@ static int qcow_create(const char *filename, QEMUOptionParameter *options,
        return ret;
    }

    ret = bdrv_file_open(&qcow_bs, filename, NULL, BDRV_O_RDWR, &local_err);
    ret = bdrv_file_open(&qcow_bs, filename, NULL, NULL, BDRV_O_RDWR,
                         &local_err);
    if (ret < 0) {
        qerror_report_err(local_err);
        error_free(local_err);
Loading