diff mbox

[v3,08/21] block: Allow reference for bdrv_file_open()

Message ID 1386785473-26157-9-git-send-email-mreitz@redhat.com
State New
Headers show

Commit Message

Max Reitz Dec. 11, 2013, 6:11 p.m. UTC
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: Max Reitz <mreitz@redhat.com>
---
 block.c               | 27 ++++++++++++++++++++++++---
 block/blkdebug.c      |  2 +-
 block/blkverify.c     |  2 +-
 block/cow.c           |  3 ++-
 block/qcow.c          |  3 ++-
 block/qcow2.c         |  2 +-
 block/qed.c           |  4 ++--
 block/sheepdog.c      |  4 ++--
 block/vhdx.c          |  2 +-
 block/vmdk.c          |  4 ++--
 include/block/block.h |  3 ++-
 qemu-io.c             |  2 +-
 12 files changed, 41 insertions(+), 17 deletions(-)

Comments

Fam Zheng Dec. 12, 2013, 8:01 a.m. UTC | #1
On 2013年12月12日 02:11, Max Reitz wrote:
> 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: Max Reitz <mreitz@redhat.com>
> ---
>   block.c               | 27 ++++++++++++++++++++++++---
>   block/blkdebug.c      |  2 +-
>   block/blkverify.c     |  2 +-
>   block/cow.c           |  3 ++-
>   block/qcow.c          |  3 ++-
>   block/qcow2.c         |  2 +-
>   block/qed.c           |  4 ++--
>   block/sheepdog.c      |  4 ++--
>   block/vhdx.c          |  2 +-
>   block/vmdk.c          |  4 ++--
>   include/block/block.h |  3 ++-
>   qemu-io.c             |  2 +-
>   12 files changed, 41 insertions(+), 17 deletions(-)
>
> diff --git a/block.c b/block.c
> index 13f001a..9e197b3 100644
> --- a/block.c
> +++ b/block.c
> @@ -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,26 @@ 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");
> +            ret = -EINVAL;
> +            goto fail;
> +        }
> +        QDECREF(options);

QDECREF called...

> +
> +        bs = bdrv_find(reference);
> +        if (!bs) {
> +            error_setg(errp, "Cannot find block device '%s'", reference);
> +            ret = -ENODEV;
> +            goto fail;

Jump to fail for QDECREF again. Duplicated?

Fam
Max Reitz Dec. 13, 2013, 3:55 p.m. UTC | #2
On 12.12.2013 09:01, Fam Zheng wrote:
> On 2013年12月12日 02:11, Max Reitz wrote:
>> 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: Max Reitz <mreitz@redhat.com>
>> ---
>> block.c | 27 ++++++++++++++++++++++++---
>> block/blkdebug.c | 2 +-
>> block/blkverify.c | 2 +-
>> block/cow.c | 3 ++-
>> block/qcow.c | 3 ++-
>> block/qcow2.c | 2 +-
>> block/qed.c | 4 ++--
>> block/sheepdog.c | 4 ++--
>> block/vhdx.c | 2 +-
>> block/vmdk.c | 4 ++--
>> include/block/block.h | 3 ++-
>> qemu-io.c | 2 +-
>> 12 files changed, 41 insertions(+), 17 deletions(-)
>>
>> diff --git a/block.c b/block.c
>> index 13f001a..9e197b3 100644
>> --- a/block.c
>> +++ b/block.c
>> @@ -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,26 @@ 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");
>> + ret = -EINVAL;
>> + goto fail;
>> + }
>> + QDECREF(options);
>
> QDECREF called...
>
>> +
>> + bs = bdrv_find(reference);
>> + if (!bs) {
>> + error_setg(errp, "Cannot find block device '%s'", reference);
>> + ret = -ENODEV;
>> + goto fail;
>
> Jump to fail for QDECREF again. Duplicated?

Oh, right, thanks.

Max
diff mbox

Patch

diff --git a/block.c b/block.c
index 13f001a..9e197b3 100644
--- a/block.c
+++ b/block.c
@@ -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,26 @@  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");
+            ret = -EINVAL;
+            goto fail;
+        }
+        QDECREF(options);
+
+        bs = bdrv_find(reference);
+        if (!bs) {
+            error_setg(errp, "Cannot find block device '%s'", reference);
+            ret = -ENODEV;
+            goto fail;
+        }
+        bdrv_ref(bs);
+        *pbs = bs;
+        return 0;
+    }
+
     bs = bdrv_new("");
     bs->options = options;
     options = qdict_clone_shallow(options);
@@ -1124,7 +1145,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;
diff --git a/block/blkdebug.c b/block/blkdebug.c
index 584b1d6..35c2dae 100644
--- a/block/blkdebug.c
+++ b/block/blkdebug.c
@@ -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;
diff --git a/block/blkverify.c b/block/blkverify.c
index 3c63528..c6eb287 100644
--- a/block/blkverify.c
+++ b/block/blkverify.c
@@ -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;
diff --git a/block/cow.c b/block/cow.c
index dc15e46..7fc0b12 100644
--- a/block/cow.c
+++ b/block/cow.c
@@ -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);
diff --git a/block/qcow.c b/block/qcow.c
index c470e05..948b0c5 100644
--- a/block/qcow.c
+++ b/block/qcow.c
@@ -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);
diff --git a/block/qcow2.c b/block/qcow2.c
index f29aa88..62e3f6a 100644
--- a/block/qcow2.c
+++ b/block/qcow2.c
@@ -1483,7 +1483,7 @@  static int qcow2_create2(const char *filename, int64_t total_size,
         return ret;
     }
 
-    ret = bdrv_file_open(&bs, filename, NULL, BDRV_O_RDWR, &local_err);
+    ret = bdrv_file_open(&bs, filename, NULL, NULL, BDRV_O_RDWR, &local_err);
     if (ret < 0) {
         error_propagate(errp, local_err);
         return ret;
diff --git a/block/qed.c b/block/qed.c
index 450a1fa..0dd5c58 100644
--- a/block/qed.c
+++ b/block/qed.c
@@ -563,8 +563,8 @@  static int qed_create(const char *filename, uint32_t cluster_size,
         return ret;
     }
 
-    ret = bdrv_file_open(&bs, filename, NULL, BDRV_O_RDWR | BDRV_O_CACHE_WB,
-                         &local_err);
+    ret = bdrv_file_open(&bs, filename, NULL, NULL,
+                         BDRV_O_RDWR | BDRV_O_CACHE_WB, &local_err);
     if (ret < 0) {
         qerror_report_err(local_err);
         error_free(local_err);
diff --git a/block/sheepdog.c b/block/sheepdog.c
index b4ae50f..7b80b2f 100644
--- a/block/sheepdog.c
+++ b/block/sheepdog.c
@@ -1534,7 +1534,7 @@  static int sd_prealloc(const char *filename)
     Error *local_err = NULL;
     int ret;
 
-    ret = bdrv_file_open(&bs, filename, NULL, BDRV_O_RDWR, &local_err);
+    ret = bdrv_file_open(&bs, filename, NULL, NULL, BDRV_O_RDWR, &local_err);
     if (ret < 0) {
         qerror_report_err(local_err);
         error_free(local_err);
@@ -1693,7 +1693,7 @@  static int sd_create(const char *filename, QEMUOptionParameter *options,
             goto out;
         }
 
-        ret = bdrv_file_open(&bs, backing_file, NULL, 0, &local_err);
+        ret = bdrv_file_open(&bs, backing_file, NULL, NULL, 0, &local_err);
         if (ret < 0) {
             qerror_report_err(local_err);
             error_free(local_err);
diff --git a/block/vhdx.c b/block/vhdx.c
index 67bbe10..4809056 100644
--- a/block/vhdx.c
+++ b/block/vhdx.c
@@ -1798,7 +1798,7 @@  static int vhdx_create(const char *filename, QEMUOptionParameter *options,
         goto exit;
     }
 
-    ret = bdrv_file_open(&bs, filename, NULL, BDRV_O_RDWR, &local_err);
+    ret = bdrv_file_open(&bs, filename, NULL, NULL, BDRV_O_RDWR, &local_err);
     if (ret < 0) {
         error_propagate(errp, local_err);
         goto exit;
diff --git a/block/vmdk.c b/block/vmdk.c
index 0734bc2..a095f1e 100644
--- a/block/vmdk.c
+++ b/block/vmdk.c
@@ -764,8 +764,8 @@  static int vmdk_parse_extents(const char *desc, BlockDriverState *bs,
 
         path_combine(extent_path, sizeof(extent_path),
                 desc_file_path, fname);
-        ret = bdrv_file_open(&extent_file, extent_path, NULL, bs->open_flags,
-                             errp);
+        ret = bdrv_file_open(&extent_file, extent_path, NULL, NULL,
+                             bs->open_flags, errp);
         if (ret) {
             return ret;
         }
diff --git a/include/block/block.h b/include/block/block.h
index 36efaea..e2b2a15 100644
--- a/include/block/block.h
+++ b/include/block/block.h
@@ -184,7 +184,8 @@  void bdrv_append(BlockDriverState *bs_new, BlockDriverState *bs_top);
 int bdrv_parse_cache_flags(const char *mode, int *flags);
 int bdrv_parse_discard_flags(const char *mode, int *flags);
 int bdrv_file_open(BlockDriverState **pbs, const char *filename,
-                   QDict *options, int flags, Error **errp);
+                   const char *reference, QDict *options, int flags,
+                   Error **errp);
 int bdrv_open_backing_file(BlockDriverState *bs, QDict *options, Error **errp);
 int bdrv_open(BlockDriverState *bs, const char *filename, QDict *options,
               int flags, BlockDriver *drv, Error **errp);
diff --git a/qemu-io.c b/qemu-io.c
index 3b3340a..f180813 100644
--- a/qemu-io.c
+++ b/qemu-io.c
@@ -56,7 +56,7 @@  static int openfile(char *name, int flags, int growable, QDict *opts)
     }
 
     if (growable) {
-        if (bdrv_file_open(&qemuio_bs, name, opts, flags, &local_err)) {
+        if (bdrv_file_open(&qemuio_bs, name, NULL, opts, flags, &local_err)) {
             fprintf(stderr, "%s: can't open device %s: %s\n", progname, name,
                     error_get_pretty(local_err));
             error_free(local_err);