Skip to contentSkip to conversation

block: Remove "growable", add blk_new_open()

PATCH Superseded Max Reitz 2 participants 17 messages Jan 22, 2015 – Jan 26, 2015 Block layer

v2 of 4 Superseded Latest is v4 →

lore
Share & tools
lore

Max Reitz

author
#0 lore raw

v2 cover block: Remove "growable", add blk_new_open()

This series removes the "growable" field from the BlockDriverState object. Its use was to clamp guest requests against the limits of the BDS; however, this can now be done more easily by moving those checks into the BlockBackend functions.

In a future series, "growable" may be reintroduced (maybe with a different name); it will then signify whether a BDS is able to grow (in contrast to the current "growable", which signifies whether it is allowed to). Maybe I will add it to the BlockDriver instead of the BDS, though.

To be able to remove that field, qemu-io needs to be converted to BlockBackend, which is done by this series as well. While working on that I decided to convert blk_new_with_bs()+bdrv_open() to blk_new_open(). I was skeptical about that decision at first, but it seems good now that I was able to replace nearly every blk_new_with_bs() call by blk_new_open(). In a future series I may try to convert some remaining bdrv_open() calls to blk_new_open() as well. (And, in fact, in a future series I *will* replace the last remaining blk_new_with_bs() outside of blk_new_open() by blk_new_open().)

Finally, the question needs to be asked: If, after this series, every BDS is allowed to grow, are there any users which do not use BB, but should still be disallowed from reading/writing beyond a BDS's limits? The only users I could see were the block jobs. Some of them should indeed be converted to BB; but none of them takes a user-supplied offset or size, all work on the full BDS (or only on parts which have been modified, etc.). Therefore, it is by design impossible for them to exceed the BDS's limits, which makes making all BDS's growable safe.

v2:
- Rebased [Kevin]
- Patch 2: Added a TODO comment about removing @filename and @flags from blk_new_open() when possible [Kevin]

git-backport-diff against v1:

Key:
[----] : patches are identical
[####] : number of functional differences between upstream/downstream patch [down] : patch is downstream-only
The flags [FC] indicate (F)unctional and (C)ontextual differences, respectively

001/14:[----] [--] 'block: Lift some BDS functions to the BlockBackend' 002/14:[0006] [FC] 'block: Add blk_new_open()'
003/14:[----] [-C] 'blockdev: Use blk_new_open() in blockdev_init()' 004/14:[----] [--] 'block/xen: Use blk_new_open() in blk_connect()' 005/14:[----] [--] 'qemu-img: Use blk_new_open() in img_open()' 006/14:[----] [--] 'qemu-img: Use blk_new_open() in img_rebase()' 007/14:[----] [--] 'qemu-img: Use BlockBackend as far as possible' 008/14:[----] [--] 'qemu-nbd: Use blk_new_open() in main()' 009/14:[----] [--] 'qemu-io: Use blk_new_open() in openfile()' 010/14:[0002] [FC] 'qemu-io: Remove "growable" option' 011/14:[----] [--] 'qemu-io: Use BlockBackend'
012/14:[----] [--] 'block: Clamp BlockBackend requests' 013/14:[----] [--] 'block: Remove "growable" from BDS' 014/14:[----] [--] 'block: Keep bdrv_check*_request()'s return value'

Max Reitz (14):
block: Lift some BDS functions to the BlockBackend
block: Add blk_new_open()
blockdev: Use blk_new_open() in blockdev_init() block/xen: Use blk_new_open() in blk_connect()
qemu-img: Use blk_new_open() in img_open()
qemu-img: Use blk_new_open() in img_rebase()
qemu-img: Use BlockBackend as far as possible
qemu-nbd: Use blk_new_open() in main()
qemu-io: Use blk_new_open() in openfile()
qemu-io: Remove "growable" option
qemu-io: Use BlockBackend
block: Clamp BlockBackend requests
block: Remove "growable" from BDS
block: Keep bdrv_check*_request()'s return value

 block.c                        |  59 +++++-----
 block/block-backend.c          | 219 +++++++++++++++++++++++++++++++++++++
 block/qcow2.c                  |   6 --
 block/raw-posix.c              |   2 +-
 block/raw-win32.c              |   2 +-
 block/sheepdog.c               |   2 +-
 blockdev.c                     |  92 ++++++++--------
 hmp.c                          |   9 +-
 hw/block/xen_disk.c            |  24 ++---
 include/block/block_int.h      |   3 -
 include/qemu-io.h              |   4 +-
 include/sysemu/block-backend.h |  12 +++
 qemu-img.c                     | 171 ++++++++++++++---------------
 qemu-io-cmds.c                 | 238 ++++++++++++++++++++++-------------------
 qemu-io.c                      |  58 ++++------
 qemu-nbd.c                     |  25 ++---
 tests/qemu-iotests/016         |  73 -------------
 tests/qemu-iotests/016.out     |  23 ----
 tests/qemu-iotests/051.out     |  60 +++++------
 tests/qemu-iotests/087.out     |   8 +-
 tests/qemu-iotests/group       |   1 -
 21 files changed, 590 insertions(+), 501 deletions(-)
 delete mode 100755 tests/qemu-iotests/016
 delete mode 100644 tests/qemu-iotests/016.out
Signature · 1 line
2.1.0
author
#1 lore raw

PATCH v2 1/14 block: Lift some BDS functions to the BlockBackend

Create the blk_* counterparts for the following bdrv_* functions (which make sense to call on the BlockBackend level):
- bdrv_co_write_zeroes()
- bdrv_write_compressed()
- bdrv_truncate()
- bdrv_discard()
- bdrv_load_vmstate()
- bdrv_save_vmstate()

---
 block/block-backend.c          | 33 +++++++++++++++++++++++++++++++++
 include/sysemu/block-backend.h |  9 +++++++++
 2 files changed, 42 insertions(+)
block/block-backend.c +33 -0
index d00c129..b359545 100644
@@ -663,3 +663,36 @@ void *blk_aio_get(const AIOCBInfo *aiocb_info, BlockBackend *blk,
 {
     return qemu_aio_get(aiocb_info, blk_bs(blk), cb, opaque);
 }
+
+int coroutine_fn blk_co_write_zeroes(BlockBackend *blk, int64_t sector_num,
+                                     int nb_sectors, BdrvRequestFlags flags)
+{
+    return bdrv_co_write_zeroes(blk->bs, sector_num, nb_sectors, flags);
+}
+
+int blk_write_compressed(BlockBackend *blk, int64_t sector_num,
+                         const uint8_t *buf, int nb_sectors)
+{
+    return bdrv_write_compressed(blk->bs, sector_num, buf, nb_sectors);
+}
+
+int blk_truncate(BlockBackend *blk, int64_t offset)
+{
+    return bdrv_truncate(blk->bs, offset);
+}
+
+int blk_discard(BlockBackend *blk, int64_t sector_num, int nb_sectors)
+{
+    return bdrv_discard(blk->bs, sector_num, nb_sectors);
+}
+
+int blk_save_vmstate(BlockBackend *blk, const uint8_t *buf,
+                     int64_t pos, int size)
+{
+    return bdrv_save_vmstate(blk->bs, buf, pos, size);
+}
+
+int blk_load_vmstate(BlockBackend *blk, uint8_t *buf, int64_t pos, int size)
+{
+    return bdrv_load_vmstate(blk->bs, buf, pos, size);
+}
include/sysemu/block-backend.h +9 -0
index 8871a02..38afa79 100644
@@ -150,5 +150,14 @@ BlockAcctStats *blk_get_stats(BlockBackend *blk);
 
 void *blk_aio_get(const AIOCBInfo *aiocb_info, BlockBackend *blk,
                   BlockCompletionFunc *cb, void *opaque);
+int coroutine_fn blk_co_write_zeroes(BlockBackend *blk, int64_t sector_num,
+                                     int nb_sectors, BdrvRequestFlags flags);
+int blk_write_compressed(BlockBackend *blk, int64_t sector_num,
+                         const uint8_t *buf, int nb_sectors);
+int blk_truncate(BlockBackend *blk, int64_t offset);
+int blk_discard(BlockBackend *blk, int64_t sector_num, int nb_sectors);
+int blk_save_vmstate(BlockBackend *blk, const uint8_t *buf,
+                     int64_t pos, int size);
+int blk_load_vmstate(BlockBackend *blk, uint8_t *buf, int64_t pos, int size);
 
 #endif
Signature · 1 line
2.1.0
author
#2 lore raw

PATCH v2 2/14 block: Add blk_new_open()

In reply to Max Reitz

blk_new_with_bs() creates a BlockBackend with an empty BlockDriverState attached to it. Empty BDSs are not nice, therefore add an alternative function which combines blk_new_with_bs() with bdrv_open().

Note: In contrast to bdrv_open() which takes a BlockDriver parameter, blk_new_open() does not take such a parameter. This is because bdrv_open() opens a BlockDriverState, therefore it is naturally to be able to set the BlockDriver for that BDS. The fact that bdrv_open() can open more than a single BDS is merely some form of a byproduct.

blk_new_open() on the other hand is intended to be used to create a whole tree of BlockDriverStates. Therefore, setting a single BlockDriver does not make much sense. Instead, the drivers to be used for each of the nodes must be configured through the "options" QDict; including the driver of the root BDS.

---
 block/block-backend.c          | 34 ++++++++++++++++++++++++++++++++++
 include/sysemu/block-backend.h |  3 +++
 2 files changed, 37 insertions(+)
block/block-backend.c +34 -0
index b359545..543edaa 100644
@@ -91,6 +91,40 @@ BlockBackend *blk_new_with_bs(const char *name, Error **errp)
     return blk;
 }
 
+/*
+ * Calls blk_new_with_bs() and then calls bdrv_open() on the BlockDriverState.
+ *
+ * Just as with bdrv_open(), after having called this function the reference to
+ * @options belongs to the block layer (even on failure).
+ *
+ * TODO: Remove @filename and @flags; it should be possible to specify a whole
+ * BDS tree just by specifying the @options QDict (or @reference,
+ * alternatively). At the time of adding this function, this is not possible,
+ * though, so callers of this function have to be able to specify @filename and
+ * @flags.
+ */
+BlockBackend *blk_new_open(const char *name, const char *filename,
+                           const char *reference, QDict *options, int flags,
+                           Error **errp)
+{
+    BlockBackend *blk;
+    int ret;
+
+    blk = blk_new_with_bs(name, errp);
+    if (!blk) {
+        QDECREF(options);
+        return NULL;
+    }
+
+    ret = bdrv_open(&blk->bs, filename, reference, options, flags, NULL, errp);
+    if (ret < 0) {
+        blk_unref(blk);
+        return NULL;
+    }
+
+    return blk;
+}
+
 static void blk_delete(BlockBackend *blk)
 {
     assert(!blk->refcnt);
include/sysemu/block-backend.h +3 -0
index 38afa79..f39bb5c 100644
@@ -62,6 +62,9 @@ typedef struct BlockDevOps {
 
 BlockBackend *blk_new(const char *name, Error **errp);
 BlockBackend *blk_new_with_bs(const char *name, Error **errp);
+BlockBackend *blk_new_open(const char *name, const char *filename,
+                           const char *reference, QDict *options, int flags,
+                           Error **errp);
 void blk_ref(BlockBackend *blk);
 void blk_unref(BlockBackend *blk);
 const char *blk_name(BlockBackend *blk);
Signature · 1 line
2.1.0
author
#3 lore raw

PATCH v2 3/14 blockdev: Use blk_new_open() in blockdev_init()

In reply to Max Reitz

Due to different error propagation, this breaks tests 051 and 087; fix their output.

---
 blockdev.c                 | 92 +++++++++++++++++++++-------------------------
 tests/qemu-iotests/051.out | 60 +++++++++++++++---------------
 tests/qemu-iotests/087.out |  8 ++--
 3 files changed, 76 insertions(+), 84 deletions(-)
blockdev.c +42 -50
index d59efd3..7573746 100644
@@ -354,13 +354,11 @@ static BlockBackend *blockdev_init(const char *file, QDict *bs_opts,
     ThrottleConfig cfg;
     int snapshot = 0;
     bool copy_on_read;
-    int ret;
     Error *error = NULL;
     QemuOpts *opts;
     const char *id;
     bool has_driver_specific_opts;
     BlockdevDetectZeroesOptions detect_zeroes;
-    BlockDriver *drv = NULL;
 
     /* Check common options by copying from bs_opts to opts, all other options
      * stay in bs_opts for processing by bdrv_open(). */
@@ -426,11 +424,11 @@ static BlockBackend *blockdev_init(const char *file, QDict *bs_opts,
             goto early_err;
         }
 
-        drv = bdrv_find_format(buf);
-        if (!drv) {
-            error_setg(errp, "'%s' invalid format", buf);
+        if (qdict_haskey(bs_opts, "driver")) {
+            error_setg(errp, "Cannot specify both 'driver' and 'format'");
             goto early_err;
         }
+        qdict_put_obj(bs_opts, "driver", QOBJECT(qstring_from_str(buf)));
     }
 
     /* disk I/O throttling */
@@ -505,70 +503,64 @@ static BlockBackend *blockdev_init(const char *file, QDict *bs_opts,
     }
 
     /* init */
-    blk = blk_new_with_bs(qemu_opts_id(opts), errp);
-    if (!blk) {
-        goto early_err;
-    }
-    bs = blk_bs(blk);
-    bs->open_flags = snapshot ? BDRV_O_SNAPSHOT : 0;
-    bs->read_only = ro;
-    bs->detect_zeroes = detect_zeroes;
-
-    bdrv_set_on_error(bs, on_read_error, on_write_error);
+    if ((!file || !*file) && !has_driver_specific_opts) {
+        blk = blk_new_with_bs(qemu_opts_id(opts), errp);
+        if (!blk) {
+            goto early_err;
+        }
 
-    /* disk I/O throttling */
-    if (throttle_enabled(&cfg)) {
-        bdrv_io_limits_enable(bs);
-        bdrv_set_io_limits(bs, &cfg);
-    }
+        bs = blk_bs(blk);
+        bs->open_flags = snapshot ? BDRV_O_SNAPSHOT : 0;
+        bs->read_only = ro;
 
-    if (!file || !*file) {
-        if (has_driver_specific_opts) {
+        QDECREF(bs_opts);
+    } else {
+        if (file && !*file) {
             file = NULL;
-        } else {
-            QDECREF(bs_opts);
-            qemu_opts_del(opts);
-            return blk;
         }
-    }
-    if (snapshot) {
-        /* always use cache=unsafe with snapshot */
-        bdrv_flags &= ~BDRV_O_CACHE_MASK;
-        bdrv_flags |= (BDRV_O_SNAPSHOT|BDRV_O_CACHE_WB|BDRV_O_NO_FLUSH);
-    }
 
-    if (copy_on_read) {
-        bdrv_flags |= BDRV_O_COPY_ON_READ;
-    }
+        if (snapshot) {
+            /* always use cache=unsafe with snapshot */
+            bdrv_flags &= ~BDRV_O_CACHE_MASK;
+            bdrv_flags |= (BDRV_O_SNAPSHOT|BDRV_O_CACHE_WB|BDRV_O_NO_FLUSH);
+        }
+
+        if (copy_on_read) {
+            bdrv_flags |= BDRV_O_COPY_ON_READ;
+        }
+
+        if (runstate_check(RUN_STATE_INMIGRATE)) {
+            bdrv_flags |= BDRV_O_INCOMING;
+        }
 
-    if (runstate_check(RUN_STATE_INMIGRATE)) {
-        bdrv_flags |= BDRV_O_INCOMING;
+        bdrv_flags |= ro ? 0 : BDRV_O_RDWR;
+
+        blk = blk_new_open(qemu_opts_id(opts), file, NULL, bs_opts, bdrv_flags,
+                           errp);
+        if (!blk) {
+            goto err_no_bs_opts;
+        }
+        bs = blk_bs(blk);
     }
 
-    bdrv_flags |= ro ? 0 : BDRV_O_RDWR;
+    bs->detect_zeroes = detect_zeroes;
 
-    QINCREF(bs_opts);
-    ret = bdrv_open(&bs, file, NULL, bs_opts, bdrv_flags, drv, &error);
-    assert(bs == blk_bs(blk));
+    bdrv_set_on_error(bs, on_read_error, on_write_error);
 
-    if (ret < 0) {
-        error_setg(errp, "could not open disk image %s: %s",
-                   file ?: blk_name(blk), error_get_pretty(error));
-        error_free(error);
-        goto err;
+    /* disk I/O throttling */
+    if (throttle_enabled(&cfg)) {
+        bdrv_io_limits_enable(bs);
+        bdrv_set_io_limits(bs, &cfg);
     }
 
     if (bdrv_key_required(bs)) {
         autostart = 0;
     }
 
-    QDECREF(bs_opts);
+err_no_bs_opts:
     qemu_opts_del(opts);
-
     return blk;
 
-err:
-    blk_unref(blk);
 early_err:
     qemu_opts_del(opts);
 err_no_opts:
tests/qemu-iotests/051.out +30 -30

157 lines not on this page · raw message

tests/qemu-iotests/087.out +4 -4
index 91f4ea1..0ba2e43 100644
@@ -21,9 +21,9 @@ QMP_VERSION
 {"return": {}}
 {"error": {"class": "GenericError", "desc": "Device with id 'disk' already exists"}}
 {"error": {"class": "GenericError", "desc": "Device name 'test-node' conflicts with an existing node name"}}
-{"error": {"class": "GenericError", "desc": "could not open disk image disk2: node-name=disk is conflicting with a device id"}}
-{"error": {"class": "GenericError", "desc": "could not open disk image disk2: Duplicate node name"}}
-{"error": {"class": "GenericError", "desc": "could not open disk image disk3: node-name=disk3 is conflicting with a device id"}}
+{"error": {"class": "GenericError", "desc": "node-name=disk is conflicting with a device id"}}
+{"error": {"class": "GenericError", "desc": "Duplicate node name"}}
+{"error": {"class": "GenericError", "desc": "node-name=disk3 is conflicting with a device id"}}
 {"return": {}}
 {"timestamp": {"seconds":  TIMESTAMP, "microseconds":  TIMESTAMP}, "event": "SHUTDOWN"}
 {"timestamp": {"seconds":  TIMESTAMP, "microseconds":  TIMESTAMP}, "event": "DEVICE_TRAY_MOVED", "data": {"device": "ide1-cd0", "tray-open": true}}
@@ -57,7 +57,7 @@ QMP_VERSION
 Testing:
 QMP_VERSION
 {"return": {}}
-{"error": {"class": "GenericError", "desc": "could not open disk image disk: Guest must be stopped for opening of encrypted image"}}
+{"error": {"class": "GenericError", "desc": "Guest must be stopped for opening of encrypted image"}}
 {"return": {}}
 {"timestamp": {"seconds":  TIMESTAMP, "microseconds":  TIMESTAMP}, "event": "SHUTDOWN"}
 {"timestamp": {"seconds":  TIMESTAMP, "microseconds":  TIMESTAMP}, "event": "DEVICE_TRAY_MOVED", "data": {"device": "ide1-cd0", "tray-open": true}}
Signature · 1 line
2.1.0
author
#4 lore raw

PATCH v2 4/14 block/xen: Use blk_new_open() in blk_connect()

In reply to Max Reitz
---
 hw/block/xen_disk.c | 24 ++++++++++--------------
 1 file changed, 10 insertions(+), 14 deletions(-)
hw/block/xen_disk.c +10 -14
index 21842a0..da333a4 100644
@@ -899,28 +899,24 @@ static int blk_connect(struct XenDevice *xendev)
         Error *local_err = NULL;
         BlockBackend *blk;
         BlockDriver *drv;
-        BlockDriverState *bs;
+        QDict *options = NULL;
 
-        /* setup via xenbus -> create new block driver instance */
-        xen_be_printf(&blkdev->xendev, 2, "create new bdrv (xenbus setup)\n");
-        blk = blk_new_with_bs(blkdev->dev, NULL);
-        if (!blk) {
-            return -1;
+        if (strcmp(blkdev->fileproto, "<unset>")) {
+            options = qdict_new();
+            qdict_put_obj(options, "driver",
+                          QOBJECT(qstring_from_str(blkdev->fileproto)));
         }
-        blkdev->blk = blk;
 
-        bs = blk_bs(blk);
-        drv = bdrv_find_whitelisted_format(blkdev->fileproto, readonly);
-        if (bdrv_open(&bs, blkdev->filename, NULL, NULL, qflags,
-                      drv, &local_err) != 0) {
+        /* setup via xenbus -> create new block driver instance */
+        xen_be_printf(&blkdev->xendev, 2, "create new bdrv (xenbus setup)\n");
+        blkdev->blk = blk_new_open(blkdev->dev, blkdev->filename, NULL, options,
+                                   qflags, &local_err);
+        if (!blkdev->blk) {
             xen_be_printf(&blkdev->xendev, 0, "error: %s\n",
                           error_get_pretty(local_err));
             error_free(local_err);
-            blk_unref(blk);
-            blkdev->blk = NULL;
             return -1;
         }
-        assert(bs == blk_bs(blk));
     } else {
         /* setup via qemu cmdline -> already setup for us */
         xen_be_printf(&blkdev->xendev, 2, "get configured bdrv (cmdline setup)\n");
Signature · 1 line
2.1.0
author
#5 lore raw

PATCH v2 5/14 qemu-img: Use blk_new_open() in img_open()

In reply to Max Reitz
---
 qemu-img.c | 20 ++++++--------------
 1 file changed, 6 insertions(+), 14 deletions(-)
qemu-img.c +6 -14
index 7876258..f82f62b 100644
@@ -291,32 +291,24 @@ static BlockBackend *img_open(const char *id, const char *filename,
 {
     BlockBackend *blk;
     BlockDriverState *bs;
-    BlockDriver *drv;
     char password[256];
     Error *local_err = NULL;
-    int ret;
-
-    blk = blk_new_with_bs(id, &error_abort);
-    bs = blk_bs(blk);
+    QDict *options = NULL;
 
     if (fmt) {
-        drv = bdrv_find_format(fmt);
-        if (!drv) {
-            error_report("Unknown file format '%s'", fmt);
-            goto fail;
-        }
-    } else {
-        drv = NULL;
+        options = qdict_new();
+        qdict_put_obj(options, "driver", QOBJECT(qstring_from_str(fmt)));
     }
 
-    ret = bdrv_open(&bs, filename, NULL, NULL, flags, drv, &local_err);
-    if (ret < 0) {
+    blk = blk_new_open(id, filename, NULL, options, flags, &local_err);
+    if (!blk) {
         error_report("Could not open '%s': %s", filename,
                      error_get_pretty(local_err));
         error_free(local_err);
         goto fail;
     }
 
+    bs = blk_bs(blk);
     if (bdrv_is_encrypted(bs) && require_io) {
         qprintf(quiet, "Disk image '%s' is encrypted.\n", filename);
         if (read_password(password, sizeof(password)) < 0) {
Signature · 1 line
2.1.0
author
#6 lore raw

PATCH v2 6/14 qemu-img: Use blk_new_open() in img_rebase()

In reply to Max Reitz
---
 qemu-img.c | 57 ++++++++++++++++++++++++---------------------------------
 1 file changed, 24 insertions(+), 33 deletions(-)
qemu-img.c +24 -33
index f82f62b..404580e 100644
@@ -2430,7 +2430,6 @@ static int img_rebase(int argc, char **argv)
 {
     BlockBackend *blk = NULL, *blk_old_backing = NULL, *blk_new_backing = NULL;
     BlockDriverState *bs = NULL, *bs_old_backing = NULL, *bs_new_backing = NULL;
-    BlockDriver *old_backing_drv, *new_backing_drv;
     char *filename;
     const char *fmt, *cache, *src_cache, *out_basefmt, *out_baseimg;
     int c, flags, src_flags, ret;
@@ -2524,54 +2523,46 @@ static int img_rebase(int argc, char **argv)
     }
     bs = blk_bs(blk);
 
-    /* Find the right drivers for the backing files */
-    old_backing_drv = NULL;
-    new_backing_drv = NULL;
-
-    if (!unsafe && bs->backing_format[0] != '\0') {
-        old_backing_drv = bdrv_find_format(bs->backing_format);
-        if (old_backing_drv == NULL) {
-            error_report("Invalid format name: '%s'", bs->backing_format);
-            ret = -1;
-            goto out;
-        }
-    }
-
-    if (out_basefmt != NULL) {
-        new_backing_drv = bdrv_find_format(out_basefmt);
-        if (new_backing_drv == NULL) {
-            error_report("Invalid format name: '%s'", out_basefmt);
-            ret = -1;
-            goto out;
-        }
-    }
-
     /* For safe rebasing we need to compare old and new backing file */
     if (!unsafe) {
         char backing_name[1024];
+        QDict *options = NULL;
+
+        if (!unsafe && bs->backing_format[0] != '\0') {
+            options = qdict_new();
+            qdict_put_obj(options, "driver",
+                          QOBJECT(qstring_from_str(bs->backing_format)));
+        }
 
-        blk_old_backing = blk_new_with_bs("old_backing", &error_abort);
-        bs_old_backing = blk_bs(blk_old_backing);
         bdrv_get_backing_filename(bs, backing_name, sizeof(backing_name));
-        ret = bdrv_open(&bs_old_backing, backing_name, NULL, NULL, src_flags,
-                        old_backing_drv, &local_err);
-        if (ret) {
+        blk_old_backing = blk_new_open("old_backing", backing_name, NULL,
+                                       options, src_flags, &local_err);
+        if (!blk_old_backing) {
             error_report("Could not open old backing file '%s': %s",
                          backing_name, error_get_pretty(local_err));
             error_free(local_err);
             goto out;
         }
+        bs_old_backing = blk_bs(blk_old_backing);
+
         if (out_baseimg[0]) {
-            blk_new_backing = blk_new_with_bs("new_backing", &error_abort);
-            bs_new_backing = blk_bs(blk_new_backing);
-            ret = bdrv_open(&bs_new_backing, out_baseimg, NULL, NULL, src_flags,
-                            new_backing_drv, &local_err);
-            if (ret) {
+            if (out_basefmt) {
+                options = qdict_new();
+                qdict_put_obj(options, "driver",
+                              QOBJECT(qstring_from_str(out_basefmt)));
+            } else {
+                options = NULL;
+            }
+
+            blk_new_backing = blk_new_open("new_backing", out_baseimg, NULL,
+                                           options, src_flags, &local_err);
+            if (!blk_new_backing) {
                 error_report("Could not open new backing file '%s': %s",
                              out_baseimg, error_get_pretty(local_err));
                 error_free(local_err);
                 goto out;
             }
+            bs_new_backing = blk_bs(blk_new_backing);
         }
     }
 
Signature · 1 line
2.1.0
author
#7 lore raw

PATCH v2 7/14 qemu-img: Use BlockBackend as far as possible

In reply to Max Reitz

Although qemu-img already creates BlockBackends, it does not do accesses to the images through them. This patch converts all of the bdrv_* calls for which this is currently possible to blk_* calls. Most of the remaining calls will probably stay bdrv_* calls because they really do operate on the BDS level instead of the BB level.

---
 qemu-img.c | 98 ++++++++++++++++++++++++++++++++++----------------------------
 1 file changed, 54 insertions(+), 44 deletions(-)
qemu-img.c +54 -44
index 404580e..a28093c 100644
@@ -1008,19 +1008,19 @@ static int64_t sectors_to_process(int64_t total, int64_t from)
  * Returns 0 in case sectors are filled with 0, 1 if sectors contain non-zero
  * data and negative value on error.
  *
- * @param bs:  Driver used for accessing file
+ * @param blk:  BlockBackend for the image
  * @param sect_num: Number of first sector to check
  * @param sect_count: Number of sectors to check
  * @param filename: Name of disk file we are checking (logging purpose)
  * @param buffer: Allocated buffer for storing read data
  * @param quiet: Flag for quiet mode
  */
-static int check_empty_sectors(BlockDriverState *bs, int64_t sect_num,
+static int check_empty_sectors(BlockBackend *blk, int64_t sect_num,
                                int sect_count, const char *filename,
                                uint8_t *buffer, bool quiet)
 {
     int pnum, ret = 0;
-    ret = bdrv_read(bs, sect_num, buffer, sect_count);
+    ret = blk_read(blk, sect_num, buffer, sect_count);
     if (ret < 0) {
         error_report("Error while reading offset %" PRId64 " of %s: %s",
                      sectors_to_bytes(sect_num), filename, strerror(-ret));
@@ -1130,22 +1130,26 @@ static int img_compare(int argc, char **argv)
     }
     bs2 = blk_bs(blk2);
 
-    buf1 = qemu_blockalign(bs1, IO_BUF_SIZE);
-    buf2 = qemu_blockalign(bs2, IO_BUF_SIZE);
-    total_sectors1 = bdrv_nb_sectors(bs1);
+    buf1 = blk_blockalign(blk1, IO_BUF_SIZE);
+    buf2 = blk_blockalign(blk2, IO_BUF_SIZE);
+    total_sectors1 = blk_getlength(blk1);
     if (total_sectors1 < 0) {
         error_report("Can't get size of %s: %s",
                      filename1, strerror(-total_sectors1));
         ret = 4;
         goto out;
     }
-    total_sectors2 = bdrv_nb_sectors(bs2);
+    total_sectors2 = blk_getlength(blk2);
     if (total_sectors2 < 0) {
         error_report("Can't get size of %s: %s",
                      filename2, strerror(-total_sectors2));
         ret = 4;
         goto out;
     }
+
+    total_sectors1 /= BDRV_SECTOR_SIZE;
+    total_sectors2 /= BDRV_SECTOR_SIZE;
+
     total_sectors = MIN(total_sectors1, total_sectors2);
     progress_base = MAX(total_sectors1, total_sectors2);
 
@@ -1181,7 +1185,7 @@ static int img_compare(int argc, char **argv)
 
         if (allocated1 == allocated2) {
             if (allocated1) {
-                ret = bdrv_read(bs1, sector_num, buf1, nb_sectors);
+                ret = blk_read(blk1, sector_num, buf1, nb_sectors);
                 if (ret < 0) {
                     error_report("Error while reading offset %" PRId64 " of %s:"
                                  " %s", sectors_to_bytes(sector_num), filename1,
@@ -1189,7 +1193,7 @@ static int img_compare(int argc, char **argv)
                     ret = 4;
                     goto out;
                 }
-                ret = bdrv_read(bs2, sector_num, buf2, nb_sectors);
+                ret = blk_read(blk2, sector_num, buf2, nb_sectors);
                 if (ret < 0) {
                     error_report("Error while reading offset %" PRId64
                                  " of %s: %s", sectors_to_bytes(sector_num),
@@ -1216,10 +1220,10 @@ static int img_compare(int argc, char **argv)
             }
 
             if (allocated1) {
-                ret = check_empty_sectors(bs1, sector_num, nb_sectors,
+                ret = check_empty_sectors(blk1, sector_num, nb_sectors,
                                           filename1, buf1, quiet);
             } else {
-                ret = check_empty_sectors(bs2, sector_num, nb_sectors,
+                ret = check_empty_sectors(blk2, sector_num, nb_sectors,
                                           filename2, buf1, quiet);
             }
             if (ret) {
@@ -1236,18 +1240,18 @@ static int img_compare(int argc, char **argv)
     }
 
     if (total_sectors1 != total_sectors2) {
-        BlockDriverState *bs_over;
+        BlockBackend *blk_over;
         int64_t total_sectors_over;
         const char *filename_over;
 
         qprintf(quiet, "Warning: Image size mismatch!\n");
         if (total_sectors1 > total_sectors2) {
             total_sectors_over = total_sectors1;
-            bs_over = bs1;
+            blk_over = blk1;
             filename_over = filename1;
         } else {
             total_sectors_over = total_sectors2;
-            bs_over = bs2;
+            blk_over = blk2;
             filename_over = filename2;
         }
 
@@ -1256,7 +1260,7 @@ static int img_compare(int argc, char **argv)
             if (nb_sectors <= 0) {
                 break;
             }
-            ret = bdrv_is_allocated_above(bs_over, NULL, sector_num,
+            ret = bdrv_is_allocated_above(blk_bs(blk_over), NULL, sector_num,
                                           nb_sectors, &pnum);
             if (ret < 0) {
                 ret = 3;
@@ -1267,7 +1271,7 @@ static int img_compare(int argc, char **argv)
             }
             nb_sectors = pnum;
             if (ret) {
-                ret = check_empty_sectors(bs_over, sector_num, nb_sectors,
+                ret = check_empty_sectors(blk_over, sector_num, nb_sectors,
                                           filename_over, buf1, quiet);
                 if (ret) {
                     if (ret < 0) {
@@ -1476,13 +1480,14 @@ static int img_convert(int argc, char **argv)
             goto out;
         }
         bs[bs_i] = blk_bs(blk[bs_i]);
-        bs_sectors[bs_i] = bdrv_nb_sectors(bs[bs_i]);
+        bs_sectors[bs_i] = blk_getlength(blk[bs_i]);
         if (bs_sectors[bs_i] < 0) {
             error_report("Could not get size of %s: %s",
                          argv[optind + bs_i], strerror(-bs_sectors[bs_i]));
             ret = -1;
             goto out;
         }
+        bs_sectors[bs_i] /= BDRV_SECTOR_SIZE;
         total_sectors += bs_sectors[bs_i];
     }
 
@@ -1625,16 +1630,19 @@ static int img_convert(int argc, char **argv)
                                          out_bs->bl.discard_alignment))
                     );
 
-    buf = qemu_blockalign(out_bs, bufsectors * BDRV_SECTOR_SIZE);
+    buf = blk_blockalign(out_blk, bufsectors * BDRV_SECTOR_SIZE);
 
     if (skip_create) {
-        int64_t output_sectors = bdrv_nb_sectors(out_bs);
+        int64_t output_sectors = blk_getlength(out_blk);
         if (output_sectors < 0) {
             error_report("unable to get output image length: %s\n",
                          strerror(-output_sectors));
             ret = -1;
             goto out;
-        } else if (output_sectors < total_sectors) {
+        }
+
+        output_sectors /= BDRV_SECTOR_SIZE;
+        if (output_sectors < total_sectors) {
             error_report("output file is smaller than input file");
             ret = -1;
             goto out;
@@ -1696,7 +1704,7 @@ static int img_convert(int argc, char **argv)
                 nlow = remainder > bs_sectors[bs_i] - bs_num
                     ? bs_sectors[bs_i] - bs_num : remainder;
 
-                ret = bdrv_read(bs[bs_i], bs_num, buf2, nlow);
+                ret = blk_read(blk[bs_i], bs_num, buf2, nlow);
                 if (ret < 0) {
                     error_report("error while reading sector %" PRId64 ": %s",
                                  bs_num, strerror(-ret));
@@ -1711,7 +1719,7 @@ static int img_convert(int argc, char **argv)
             assert (remainder == 0);
 
             if (!buffer_is_zero(buf, n * BDRV_SECTOR_SIZE)) {
-                ret = bdrv_write_compressed(out_bs, sector_num, buf, n);
+                ret = blk_write_compressed(out_blk, sector_num, buf, n);
                 if (ret != 0) {
                     error_report("error while compressing sector %" PRId64
                                  ": %s", sector_num, strerror(-ret));
@@ -1722,7 +1730,7 @@ static int img_convert(int argc, char **argv)
             qemu_progress_print(100.0 * sector_num / total_sectors, 0);
         }
         /* signal EOF to align */
-        bdrv_write_compressed(out_bs, 0, NULL, 0);
+        blk_write_compressed(out_blk, 0, NULL, 0);
     } else {
         int64_t sectors_to_read, sectors_read, sector_num_next_status;
         bool count_allocated_sectors;
@@ -1823,7 +1831,7 @@ restart:
             }
 
             n1 = n;
-            ret = bdrv_read(bs[bs_i], sector_num - bs_offset, buf, n);
+            ret = blk_read(blk[bs_i], sector_num - bs_offset, buf, n);
             if (ret < 0) {
                 error_report("error while reading sector %" PRId64 ": %s",
                              sector_num - bs_offset, strerror(-ret));
@@ -1836,7 +1844,7 @@ restart:
             while (n > 0) {
                 if (!has_zero_init ||
                     is_allocated_sectors_min(buf1, n, &n1, min_sparse)) {
-                    ret = bdrv_write(out_bs, sector_num, buf1, n1);
+                    ret = blk_write(out_blk, sector_num, buf1, n1);
                     if (ret < 0) {
                         error_report("error while writing sector %" PRId64
                                      ": %s", sector_num, strerror(-ret));
@@ -2260,7 +2268,7 @@ static int img_map(int argc, char **argv)
         printf("%-16s%-16s%-16s%s\n", "Offset", "Length", "Mapped to", "File");
     }
 
-    length = bdrv_getlength(bs);
+    length = blk_getlength(blk);
     while (curr.start + curr.length < length) {
         int64_t nsectors_left;
         int64_t sector_num;
@@ -2429,7 +2437,7 @@ static int img_snapshot(int argc, char **argv)
 static int img_rebase(int argc, char **argv)
 {
     BlockBackend *blk = NULL, *blk_old_backing = NULL, *blk_new_backing = NULL;
-    BlockDriverState *bs = NULL, *bs_old_backing = NULL, *bs_new_backing = NULL;
+    BlockDriverState *bs = NULL;
     char *filename;
     const char *fmt, *cache, *src_cache, *out_basefmt, *out_baseimg;
     int c, flags, src_flags, ret;
@@ -2543,7 +2551,6 @@ static int img_rebase(int argc, char **argv)
             error_free(local_err);
             goto out;
         }
-        bs_old_backing = blk_bs(blk_old_backing);
 
         if (out_baseimg[0]) {
             if (out_basefmt) {
@@ -2562,7 +2569,6 @@ static int img_rebase(int argc, char **argv)
                 error_free(local_err);
                 goto out;
             }
-            bs_new_backing = blk_bs(blk_new_backing);
         }
     }
 
@@ -2585,17 +2591,17 @@ static int img_rebase(int argc, char **argv)
         uint8_t * buf_new;
         float local_progress = 0;
 
-        buf_old = qemu_blockalign(bs, IO_BUF_SIZE);
-        buf_new = qemu_blockalign(bs, IO_BUF_SIZE);
+        buf_old = blk_blockalign(blk, IO_BUF_SIZE);
+        buf_new = blk_blockalign(blk, IO_BUF_SIZE);
 
-        num_sectors = bdrv_nb_sectors(bs);
+        num_sectors = blk_getlength(blk);
         if (num_sectors < 0) {
             error_report("Could not get size of '%s': %s",
                          filename, strerror(-num_sectors));
             ret = -1;
             goto out;
         }
-        old_backing_num_sectors = bdrv_nb_sectors(bs_old_backing);
+        old_backing_num_sectors = blk_getlength(blk_old_backing);
         if (old_backing_num_sectors < 0) {
             char backing_name[1024];
 
@@ -2605,14 +2611,20 @@ static int img_rebase(int argc, char **argv)
             ret = -1;
             goto out;
         }
-        if (bs_new_backing) {
-            new_backing_num_sectors = bdrv_nb_sectors(bs_new_backing);
+
+        num_sectors /= BDRV_SECTOR_SIZE;
+        old_backing_num_sectors /= BDRV_SECTOR_SIZE;
+
+        if (blk_new_backing) {
+            new_backing_num_sectors = blk_getlength(blk_new_backing);
             if (new_backing_num_sectors < 0) {
                 error_report("Could not get size of '%s': %s",
                              out_baseimg, strerror(-new_backing_num_sectors));
                 ret = -1;
                 goto out;
             }
+
+            new_backing_num_sectors /= BDRV_SECTOR_SIZE;
         }
 
         if (num_sectors != 0) {
@@ -2651,21 +2663,21 @@ static int img_rebase(int argc, char **argv)
                     n = old_backing_num_sectors - sector;
                 }
 
-                ret = bdrv_read(bs_old_backing, sector, buf_old, n);
+                ret = blk_read(blk_old_backing, sector, buf_old, n);
                 if (ret < 0) {
                     error_report("error while reading from old backing file");
                     goto out;
                 }
             }
 
-            if (sector >= new_backing_num_sectors || !bs_new_backing) {
+            if (sector >= new_backing_num_sectors || !blk_new_backing) {
                 memset(buf_new, 0, n * BDRV_SECTOR_SIZE);
             } else {
                 if (sector + n > new_backing_num_sectors) {
                     n = new_backing_num_sectors - sector;
                 }
 
-                ret = bdrv_read(bs_new_backing, sector, buf_new, n);
+                ret = blk_read(blk_new_backing, sector, buf_new, n);
                 if (ret < 0) {
                     error_report("error while reading from new backing file");
                     goto out;
@@ -2681,8 +2693,8 @@ static int img_rebase(int argc, char **argv)
                 if (compare_sectors(buf_old + written * 512,
                     buf_new + written * 512, n - written, &pnum))
                 {
-                    ret = bdrv_write(bs, sector + written,
-                        buf_old + written * 512, pnum);
+                    ret = blk_write(blk, sector + written,
+                                    buf_old + written * 512, pnum);
                     if (ret < 0) {
                         error_report("Error while writing to COW image: %s",
                             strerror(-ret));
@@ -2747,7 +2759,6 @@ static int img_resize(int argc, char **argv)
     int64_t n, total_size;
     bool quiet = false;
     BlockBackend *blk = NULL;
-    BlockDriverState *bs = NULL;
     QemuOpts *param;
     static QemuOptsList resize_options = {
         .name = "resize_options",
@@ -2829,10 +2840,9 @@ static int img_resize(int argc, char **argv)
         ret = -1;
         goto out;
     }
-    bs = blk_bs(blk);
 
     if (relative) {
-        total_size = bdrv_getlength(bs) + n * relative;
+        total_size = blk_getlength(blk) + n * relative;
     } else {
         total_size = n;
     }
@@ -2842,7 +2852,7 @@ static int img_resize(int argc, char **argv)
         goto out;
     }
 
-    ret = bdrv_truncate(bs, total_size);
+    ret = blk_truncate(blk, total_size);
     switch (ret) {
     case 0:
         qprintf(quiet, "Image resized.\n");
Signature · 1 line
2.1.0
author
#8 lore raw

PATCH v2 8/14 qemu-nbd: Use blk_new_open() in main()

In reply to Max Reitz
---
 qemu-nbd.c | 25 +++++++++----------------
 1 file changed, 9 insertions(+), 16 deletions(-)
qemu-nbd.c +9 -16
index d222512..fdc9590 100644
@@ -386,7 +386,6 @@ int main(int argc, char **argv)
 {
     BlockBackend *blk;
     BlockDriverState *bs;
-    BlockDriver *drv;
     off_t dev_offset = 0;
     uint32_t nbdflags = 0;
     bool disconnect = false;
@@ -429,7 +428,7 @@ int main(int argc, char **argv)
     char *end;
     int flags = BDRV_O_RDWR;
     int partition = -1;
-    int ret;
+    int ret = 0;
     int fd;
     bool seen_cache = false;
     bool seen_discard = false;
@@ -440,6 +439,7 @@ int main(int argc, char **argv)
     const char *fmt = NULL;
     Error *local_err = NULL;
     BlockdevDetectZeroesOptions detect_zeroes = BLOCKDEV_DETECT_ZEROES_OPTIONS_OFF;
+    QDict *options = NULL;
 
     /* The client thread uses SIGTERM to interrupt the server.  A signal
      * handler ensures that "qemu-nbd -v -c" exits with a nice status code.
@@ -684,24 +684,17 @@ int main(int argc, char **argv)
     atexit(bdrv_close_all);
 
     if (fmt) {
-        drv = bdrv_find_format(fmt);
-        if (!drv) {
-            errx(EXIT_FAILURE, "Unknown file format '%s'", fmt);
-        }
-    } else {
-        drv = NULL;
+        options = qdict_new();
+        qdict_put_obj(options, "driver", QOBJECT(qstring_from_str(fmt)));
     }
 
-    blk = blk_new_with_bs("hda", &error_abort);
-    bs = blk_bs(blk);
-
     srcpath = argv[optind];
-    ret = bdrv_open(&bs, srcpath, NULL, NULL, flags, drv, &local_err);
-    if (ret < 0) {
-        errno = -ret;
-        err(EXIT_FAILURE, "Failed to bdrv_open '%s': %s", argv[optind],
-            error_get_pretty(local_err));
+    blk = blk_new_open("hda", srcpath, NULL, options, flags, &local_err);
+    if (!blk) {
+        errx(EXIT_FAILURE, "Failed to blk_new_open '%s': %s", argv[optind],
+             error_get_pretty(local_err));
     }
+    bs = blk_bs(blk);
 
     if (sn_opts) {
         ret = bdrv_snapshot_load_tmp(bs,
Signature · 1 line
2.1.0
author
#9 lore raw

PATCH v2 9/14 qemu-io: Use blk_new_open() in openfile()

In reply to Max Reitz
---
 qemu-io.c | 31 ++++++++++++-------------------
 1 file changed, 12 insertions(+), 19 deletions(-)
qemu-io.c +12 -19
index 91a445a..81f8f64 100644
@@ -39,7 +39,6 @@ static ReadLineState *readline_state;
 static int close_f(BlockDriverState *bs, int argc, char **argv)
 {
     blk_unref(qemuio_blk);
-    qemuio_bs = NULL;
     qemuio_blk = NULL;
     return 0;
 }
@@ -51,34 +50,29 @@ static const cmdinfo_t close_cmd = {
     .oneline    = "close the current open file",
 };
 
-static int openfile(char *name, BlockDriver *drv, int flags, int growable,
-                    QDict *opts)
+static int openfile(char *name, int flags, int growable, QDict *opts)
 {
     Error *local_err = NULL;
 
-    if (qemuio_bs) {
+    if (qemuio_blk) {
         fprintf(stderr, "file open already, try 'help close'\n");
         QDECREF(opts);
         return 1;
     }
 
-    qemuio_blk = blk_new_with_bs("hda", &error_abort);
-    qemuio_bs = blk_bs(qemuio_blk);
-
     if (growable) {
         flags |= BDRV_O_PROTOCOL;
     }
 
-    if (bdrv_open(&qemuio_bs, name, NULL, opts, flags, drv, &local_err) < 0) {
+    qemuio_blk = blk_new_open("hda", name, NULL, opts, flags, &local_err);
+    if (!qemuio_blk) {
         fprintf(stderr, "%s: can't open%s%s: %s\n", progname,
                 name ? " device " : "", name ?: "",
                 error_get_pretty(local_err));
         error_free(local_err);
-        blk_unref(qemuio_blk);
-        qemuio_bs = NULL;
-        qemuio_blk = NULL;
         return 1;
     }
+    qemuio_bs = blk_bs(qemuio_blk);
 
     return 0;
 }
@@ -170,9 +164,9 @@ static int open_f(BlockDriverState *bs, int argc, char **argv)
     qemu_opts_reset(&empty_opts);
 
     if (optind == argc - 1) {
-        return openfile(argv[optind], NULL, flags, growable, opts);
+        return openfile(argv[optind], flags, growable, opts);
     } else if (optind == argc) {
-        return openfile(NULL, NULL, flags, growable, opts);
+        return openfile(NULL, flags, growable, opts);
     } else {
         QDECREF(opts);
         return qemuio_command_usage(&open_cmd);
@@ -387,8 +381,8 @@ int main(int argc, char **argv)
     int c;
     int opt_index = 0;
     int flags = BDRV_O_UNMAP;
-    BlockDriver *drv = NULL;
     Error *local_error = NULL;
+    QDict *opts = NULL;
 
 #ifdef CONFIG_POSIX
     signal(SIGPIPE, SIG_IGN);
@@ -414,11 +408,10 @@ int main(int argc, char **argv)
             }
             break;
         case 'f':
-            drv = bdrv_find_format(optarg);
-            if (!drv) {
-                error_report("Invalid format '%s'", optarg);
-                exit(EXIT_FAILURE);
+            if (!opts) {
+                opts = qdict_new();
             }
+            qdict_put_obj(opts, "driver", QOBJECT(qstring_from_str(optarg)));
             break;
         case 'c':
             add_user_command(optarg);
@@ -489,7 +482,7 @@ int main(int argc, char **argv)
     }
 
     if ((argc - optind) == 1) {
-        openfile(argv[optind], drv, flags, growable, NULL);
+        openfile(argv[optind], flags, growable, opts);
     }
     command_loop();
 
Signature · 1 line
2.1.0
author
#10 lore raw

PATCH v2 10/14 qemu-io: Remove "growable" option

In reply to Max Reitz

Remove "growable" option from the "open" command and from the qemu-io command line. qemu-io is about to be converted to BlockBackend which will make sure that no request exceeds the image size, so the only way to keep "growable" would be to use BlockBackend if it is not given and to directly access the BDS if it is.

qemu-io is a debugging tool, therefore removing a rarely used option will have only a very small impact, if any. There was only one qemu-iotest which used the option; since it is not critical, this patch just removes it.

---
 qemu-io.c                  | 23 +++------------
 tests/qemu-iotests/016     | 73 ----------------------------------------------
 tests/qemu-iotests/016.out | 23 ---------------
 tests/qemu-iotests/group   |  1 -
 4 files changed, 4 insertions(+), 116 deletions(-)
 delete mode 100755 tests/qemu-iotests/016
 delete mode 100644 tests/qemu-iotests/016.out
qemu-io.c +4 -19
index 81f8f64..0237ecb 100644
@@ -50,7 +50,7 @@ static const cmdinfo_t close_cmd = {
     .oneline    = "close the current open file",
 };
 
-static int openfile(char *name, int flags, int growable, QDict *opts)
+static int openfile(char *name, int flags, QDict *opts)
 {
     Error *local_err = NULL;
 
@@ -60,10 +60,6 @@ static int openfile(char *name, int flags, int growable, QDict *opts)
         return 1;
     }
 
-    if (growable) {
-        flags |= BDRV_O_PROTOCOL;
-    }
-
     qemuio_blk = blk_new_open("hda", name, NULL, opts, flags, &local_err);
     if (!qemuio_blk) {
         fprintf(stderr, "%s: can't open%s%s: %s\n", progname,
@@ -90,7 +86,6 @@ static void open_help(void)
 " -r, -- open file read-only\n"
 " -s, -- use snapshot file\n"
 " -n, -- disable host cache\n"
-" -g, -- allow file to grow (only applies to protocols)\n"
 " -o, -- options to be given to the block driver"
 "\n");
 }
@@ -123,7 +118,6 @@ static int open_f(BlockDriverState *bs, int argc, char **argv)
 {
     int flags = 0;
     int readonly = 0;
-    int growable = 0;
     int c;
     QemuOpts *qopts;
     QDict *opts;
@@ -139,9 +133,6 @@ static int open_f(BlockDriverState *bs, int argc, char **argv)
         case 'r':
             readonly = 1;
             break;
-        case 'g':
-            growable = 1;
-            break;
         case 'o':
             if (!qemu_opts_parse(&empty_opts, optarg, 0)) {
                 printf("could not parse option list -- %s\n", optarg);
@@ -164,9 +155,9 @@ static int open_f(BlockDriverState *bs, int argc, char **argv)
     qemu_opts_reset(&empty_opts);
 
     if (optind == argc - 1) {
-        return openfile(argv[optind], flags, growable, opts);
+        return openfile(argv[optind], flags, opts);
     } else if (optind == argc) {
-        return openfile(NULL, flags, growable, opts);
+        return openfile(NULL, flags, opts);
     } else {
         QDECREF(opts);
         return qemuio_command_usage(&open_cmd);
@@ -200,7 +191,6 @@ static void usage(const char *name)
 "  -r, --read-only      export read-only\n"
 "  -s, --snapshot       use snapshot file\n"
 "  -n, --nocache        disable host cache\n"
-"  -g, --growable       allow file to grow (only applies to protocols)\n"
 "  -m, --misalign       misalign allocations for O_DIRECT\n"
 "  -k, --native-aio     use kernel AIO implementation (on Linux only)\n"
 "  -t, --cache=MODE     use the given cache mode for the image\n"
@@ -359,7 +349,6 @@ static void reenable_tty_echo(void)
 int main(int argc, char **argv)
 {
     int readonly = 0;
-    int growable = 0;
     const char *sopt = "hVc:d:f:rsnmgkt:T:";
     const struct option lopt[] = {
         { "help", 0, NULL, 'h' },
@@ -371,7 +360,6 @@ int main(int argc, char **argv)
         { "snapshot", 0, NULL, 's' },
         { "nocache", 0, NULL, 'n' },
         { "misalign", 0, NULL, 'm' },
-        { "growable", 0, NULL, 'g' },
         { "native-aio", 0, NULL, 'k' },
         { "discard", 1, NULL, 'd' },
         { "cache", 1, NULL, 't' },
@@ -422,9 +410,6 @@ int main(int argc, char **argv)
         case 'm':
             qemuio_misalign = true;
             break;
-        case 'g':
-            growable = 1;
-            break;
         case 'k':
             flags |= BDRV_O_NATIVE_AIO;
             break;
@@ -482,7 +467,7 @@ int main(int argc, char **argv)
     }
 
     if ((argc - optind) == 1) {
-        openfile(argv[optind], flags, growable, opts);
+        openfile(argv[optind], flags, opts);
     }
     command_loop();
 
tests/qemu-iotests/016 deleted +0 -73

73 lines not on this page · raw message

tests/qemu-iotests/016.out deleted +0 -23
deleted file mode 100644
index acbd60b..0000000
@@ -1,23 +0,0 @@
-QA output created by 016
-Formatting 'TEST_DIR/t.IMGFMT', fmt=IMGFMT size=134217728
-
-== reading at EOF ==
-read 512/512 bytes at offset 134217728
-512 bytes, X ops; XX:XX:XX.X (XXX YYY/sec and XXX ops/sec)
-
-== reading far past EOF ==
-read 512/512 bytes at offset 268435456
-512 bytes, X ops; XX:XX:XX.X (XXX YYY/sec and XXX ops/sec)
-
-== writing at EOF ==
-wrote 512/512 bytes at offset 134217728
-512 bytes, X ops; XX:XX:XX.X (XXX YYY/sec and XXX ops/sec)
-read 512/512 bytes at offset 134217728
-512 bytes, X ops; XX:XX:XX.X (XXX YYY/sec and XXX ops/sec)
-
-== writing far past EOF ==
-wrote 512/512 bytes at offset 268435456
-512 bytes, X ops; XX:XX:XX.X (XXX YYY/sec and XXX ops/sec)
-read 512/512 bytes at offset 268435456
-512 bytes, X ops; XX:XX:XX.X (XXX YYY/sec and XXX ops/sec)
-*** done
tests/qemu-iotests/group +0 -1
index 4b2b93b..ddfedb5 100644
@@ -22,7 +22,6 @@
 013 rw auto
 014 rw auto
 015 rw snapshot auto
-016 rw auto quick
 017 rw backing auto quick
 018 rw backing auto quick
 019 rw backing auto quick
Signature · 1 line
2.1.0
author
#11 lore raw

PATCH v2 11/14 qemu-io: Use BlockBackend

In reply to Max Reitz

qemu-io should behave like a guest, therefore it should use BlockBackend to access the block layer.

There are a couple of places where that is infeasible: First, the bdrv_debug_* functions could theoretically be mirrored in the BlockBackend, but since these are functions internal to the block layer, they should not be visible externally (qemu-io as a test tool is excempt from this).

Second, bdrv_get_info() and bdrv_get_specific_info() work on a single BDS alone, therefore they should stay BDS-specific.

Third, bdrv_is_allocated() mainly works on a single BDS as well. Some data may be passed through from the BDS's file (if sectors which are apparently allocated in the file are not really allocated there but just zero).

---
 hmp.c             |   9 ++-
 include/qemu-io.h |   4 +-
 qemu-io-cmds.c    | 238 ++++++++++++++++++++++++++++--------------------------
 qemu-io.c         |  12 +--
 4 files changed, 138 insertions(+), 125 deletions(-)
hmp.c +5 -4
index 481be80..f806387 100644
@@ -16,6 +16,7 @@
 #include "hmp.h"
 #include "net/net.h"
 #include "sysemu/char.h"
+#include "sysemu/block-backend.h"
 #include "qemu/option.h"
 #include "qemu/timer.h"
 #include "qmp-commands.h"
@@ -1714,14 +1715,14 @@ void hmp_chardev_remove(Monitor *mon, const QDict *qdict)
 
 void hmp_qemu_io(Monitor *mon, const QDict *qdict)
 {
-    BlockDriverState *bs;
+    BlockBackend *blk;
     const char* device = qdict_get_str(qdict, "device");
     const char* command = qdict_get_str(qdict, "command");
     Error *err = NULL;
 
-    bs = bdrv_find(device);
-    if (bs) {
-        qemuio_command(bs, command);
+    blk = blk_by_name(device);
+    if (blk) {
+        qemuio_command(blk, command);
     } else {
         error_set(&err, QERR_DEVICE_NOT_FOUND, device);
     }
include/qemu-io.h +2 -2
index 5d6006f..4d402b9 100644
@@ -22,7 +22,7 @@
 
 #define CMD_FLAG_GLOBAL ((int)0x80000000) /* don't iterate "args" */
 
-typedef int (*cfunc_t)(BlockDriverState *bs, int argc, char **argv);
+typedef int (*cfunc_t)(BlockBackend *blk, int argc, char **argv);
 typedef void (*helpfunc_t)(void);
 
 typedef struct cmdinfo {
@@ -40,7 +40,7 @@ typedef struct cmdinfo {
 
 extern bool qemuio_misalign;
 
-bool qemuio_command(BlockDriverState *bs, const char *cmd);
+bool qemuio_command(BlockBackend *blk, const char *cmd);
 
 void qemuio_add_command(const cmdinfo_t *ci);
 int qemuio_command_usage(const cmdinfo_t *ci);
qemu-io-cmds.c +125 -113

758 lines not on this page · raw message

qemu-io.c +6 -6
index 0237ecb..bde97f8 100644
@@ -36,7 +36,7 @@ static char **cmdline;
 
 static ReadLineState *readline_state;
 
-static int close_f(BlockDriverState *bs, int argc, char **argv)
+static int close_f(BlockBackend *blk, int argc, char **argv)
 {
     blk_unref(qemuio_blk);
     qemuio_blk = NULL;
@@ -90,7 +90,7 @@ static void open_help(void)
 "\n");
 }
 
-static int open_f(BlockDriverState *bs, int argc, char **argv);
+static int open_f(BlockBackend *blk, int argc, char **argv);
 
 static const cmdinfo_t open_cmd = {
     .name       = "open",
@@ -114,7 +114,7 @@ static QemuOptsList empty_opts = {
     },
 };
 
-static int open_f(BlockDriverState *bs, int argc, char **argv)
+static int open_f(BlockBackend *blk, int argc, char **argv)
 {
     int flags = 0;
     int readonly = 0;
@@ -164,7 +164,7 @@ static int open_f(BlockDriverState *bs, int argc, char **argv)
     }
 }
 
-static int quit_f(BlockDriverState *bs, int argc, char **argv)
+static int quit_f(BlockBackend *blk, int argc, char **argv)
 {
     return 1;
 }
@@ -301,7 +301,7 @@ static void command_loop(void)
     char *input;
 
     for (i = 0; !done && i < ncmdline; i++) {
-        done = qemuio_command(qemuio_bs, cmdline[i]);
+        done = qemuio_command(qemuio_blk, cmdline[i]);
     }
     if (cmdline) {
         g_free(cmdline);
@@ -326,7 +326,7 @@ static void command_loop(void)
         if (input == NULL) {
             break;
         }
-        done = qemuio_command(qemuio_bs, input);
+        done = qemuio_command(qemuio_blk, input);
         g_free(input);
 
         prompted = 0;
Signature · 1 line
2.1.0
author
#12 lore raw

PATCH v2 12/14 block: Clamp BlockBackend requests

In reply to Max Reitz

BlockBackend is used as the interface between the block layer and guest devices. It should therefore assure that all requests are clamped to the image size.

---
 block/block-backend.c | 152 ++++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 152 insertions(+)
block/block-backend.c +152 -0
index 543edaa..96a5bc6 100644
@@ -31,6 +31,16 @@ struct BlockBackend {
     void *dev_opaque;
 };
 
+typedef struct BlockBackendAIOCB {
+    BlockAIOCB common;
+    QEMUBH *bh;
+    int ret;
+} BlockBackendAIOCB;
+
+static const AIOCBInfo block_backend_aiocb_info = {
+    .aiocb_size = sizeof(BlockBackendAIOCB),
+};
+
 static void drive_info_del(DriveInfo *dinfo);
 
 /* All the BlockBackends (except for hidden ones) */
@@ -428,39 +438,137 @@ void blk_iostatus_enable(BlockBackend *blk)
     bdrv_iostatus_enable(blk->bs);
 }
 
+static int blk_check_byte_request(BlockBackend *blk, int64_t offset,
+                                  size_t size)
+{
+    int64_t len;
+
+    if (size > INT_MAX) {
+        return -EIO;
+    }
+
+    if (!blk_is_inserted(blk)) {
+        return -ENOMEDIUM;
+    }
+
+    len = blk_getlength(blk);
+    if (len < 0) {
+        return len;
+    }
+
+    if (offset < 0) {
+        return -EIO;
+    }
+
+    if (offset > len || len - offset < size) {
+        return -EIO;
+    }
+
+    return 0;
+}
+
+static int blk_check_request(BlockBackend *blk, int64_t sector_num,
+                             int nb_sectors)
+{
+    if (sector_num < 0 || sector_num > INT64_MAX / BDRV_SECTOR_SIZE) {
+        return -EIO;
+    }
+
+    if (nb_sectors < 0 || nb_sectors > INT_MAX / BDRV_SECTOR_SIZE) {
+        return -EIO;
+    }
+
+    return blk_check_byte_request(blk, sector_num * BDRV_SECTOR_SIZE,
+                                  nb_sectors * BDRV_SECTOR_SIZE);
+}
+
 int blk_read(BlockBackend *blk, int64_t sector_num, uint8_t *buf,
              int nb_sectors)
 {
+    int ret = blk_check_request(blk, sector_num, nb_sectors);
+    if (ret < 0) {
+        return ret;
+    }
+
     return bdrv_read(blk->bs, sector_num, buf, nb_sectors);
 }
 
 int blk_read_unthrottled(BlockBackend *blk, int64_t sector_num, uint8_t *buf,
                          int nb_sectors)
 {
+    int ret = blk_check_request(blk, sector_num, nb_sectors);
+    if (ret < 0) {
+        return ret;
+    }
+
     return bdrv_read_unthrottled(blk->bs, sector_num, buf, nb_sectors);
 }
 
 int blk_write(BlockBackend *blk, int64_t sector_num, const uint8_t *buf,
               int nb_sectors)
 {
+    int ret = blk_check_request(blk, sector_num, nb_sectors);
+    if (ret < 0) {
+        return ret;
+    }
+
     return bdrv_write(blk->bs, sector_num, buf, nb_sectors);
 }
 
+static void error_callback_bh(void *opaque)
+{
+    struct BlockBackendAIOCB *acb = opaque;
+    qemu_bh_delete(acb->bh);
+    acb->common.cb(acb->common.opaque, acb->ret);
+    qemu_aio_unref(acb);
+}
+
+static BlockAIOCB *abort_aio_request(BlockBackend *blk, BlockCompletionFunc *cb,
+                                     void *opaque, int ret)
+{
+    struct BlockBackendAIOCB *acb;
+    QEMUBH *bh;
+
+    acb = blk_aio_get(&block_backend_aiocb_info, blk, cb, opaque);
+    acb->ret = ret;
+
+    bh = aio_bh_new(blk_get_aio_context(blk), error_callback_bh, acb);
+    acb->bh = bh;
+    qemu_bh_schedule(bh);
+
+    return &acb->common;
+}
+
 BlockAIOCB *blk_aio_write_zeroes(BlockBackend *blk, int64_t sector_num,
                                  int nb_sectors, BdrvRequestFlags flags,
                                  BlockCompletionFunc *cb, void *opaque)
 {
+    int ret = blk_check_request(blk, sector_num, nb_sectors);
+    if (ret < 0) {
+        return abort_aio_request(blk, cb, opaque, ret);
+    }
+
     return bdrv_aio_write_zeroes(blk->bs, sector_num, nb_sectors, flags,
                                  cb, opaque);
 }
 
 int blk_pread(BlockBackend *blk, int64_t offset, void *buf, int count)
 {
+    int ret = blk_check_byte_request(blk, offset, count);
+    if (ret < 0) {
+        return ret;
+    }
+
     return bdrv_pread(blk->bs, offset, buf, count);
 }
 
 int blk_pwrite(BlockBackend *blk, int64_t offset, const void *buf, int count)
 {
+    int ret = blk_check_byte_request(blk, offset, count);
+    if (ret < 0) {
+        return ret;
+    }
+
     return bdrv_pwrite(blk->bs, offset, buf, count);
 }
 
@@ -478,6 +586,11 @@ BlockAIOCB *blk_aio_readv(BlockBackend *blk, int64_t sector_num,
                           QEMUIOVector *iov, int nb_sectors,
                           BlockCompletionFunc *cb, void *opaque)
 {
+    int ret = blk_check_request(blk, sector_num, nb_sectors);
+    if (ret < 0) {
+        return abort_aio_request(blk, cb, opaque, ret);
+    }
+
     return bdrv_aio_readv(blk->bs, sector_num, iov, nb_sectors, cb, opaque);
 }
 
@@ -485,6 +598,11 @@ BlockAIOCB *blk_aio_writev(BlockBackend *blk, int64_t sector_num,
                            QEMUIOVector *iov, int nb_sectors,
                            BlockCompletionFunc *cb, void *opaque)
 {
+    int ret = blk_check_request(blk, sector_num, nb_sectors);
+    if (ret < 0) {
+        return abort_aio_request(blk, cb, opaque, ret);
+    }
+
     return bdrv_aio_writev(blk->bs, sector_num, iov, nb_sectors, cb, opaque);
 }
 
@@ -498,6 +616,11 @@ BlockAIOCB *blk_aio_discard(BlockBackend *blk,
                             int64_t sector_num, int nb_sectors,
                             BlockCompletionFunc *cb, void *opaque)
 {
+    int ret = blk_check_request(blk, sector_num, nb_sectors);
+    if (ret < 0) {
+        return abort_aio_request(blk, cb, opaque, ret);
+    }
+
     return bdrv_aio_discard(blk->bs, sector_num, nb_sectors, cb, opaque);
 }
 
@@ -513,6 +636,15 @@ void blk_aio_cancel_async(BlockAIOCB *acb)
 
 int blk_aio_multiwrite(BlockBackend *blk, BlockRequest *reqs, int num_reqs)
 {
+    int i, ret;
+
+    for (i = 0; i < num_reqs; i++) {
+        ret = blk_check_request(blk, reqs[i].sector, reqs[i].nb_sectors);
+        if (ret < 0) {
+            return ret;
+        }
+    }
+
     return bdrv_aio_multiwrite(blk->bs, reqs, num_reqs);
 }
 
@@ -529,6 +661,11 @@ BlockAIOCB *blk_aio_ioctl(BlockBackend *blk, unsigned long int req, void *buf,
 
 int blk_co_discard(BlockBackend *blk, int64_t sector_num, int nb_sectors)
 {
+    int ret = blk_check_request(blk, sector_num, nb_sectors);
+    if (ret < 0) {
+        return ret;
+    }
+
     return bdrv_co_discard(blk->bs, sector_num, nb_sectors);
 }
 
@@ -701,12 +838,22 @@ void *blk_aio_get(const AIOCBInfo *aiocb_info, BlockBackend *blk,
 int coroutine_fn blk_co_write_zeroes(BlockBackend *blk, int64_t sector_num,
                                      int nb_sectors, BdrvRequestFlags flags)
 {
+    int ret = blk_check_request(blk, sector_num, nb_sectors);
+    if (ret < 0) {
+        return ret;
+    }
+
     return bdrv_co_write_zeroes(blk->bs, sector_num, nb_sectors, flags);
 }
 
 int blk_write_compressed(BlockBackend *blk, int64_t sector_num,
                          const uint8_t *buf, int nb_sectors)
 {
+    int ret = blk_check_request(blk, sector_num, nb_sectors);
+    if (ret < 0) {
+        return ret;
+    }
+
     return bdrv_write_compressed(blk->bs, sector_num, buf, nb_sectors);
 }
 
@@ -717,6 +864,11 @@ int blk_truncate(BlockBackend *blk, int64_t offset)
 
 int blk_discard(BlockBackend *blk, int64_t sector_num, int nb_sectors)
 {
+    int ret = blk_check_request(blk, sector_num, nb_sectors);
+    if (ret < 0) {
+        return ret;
+    }
+
     return bdrv_discard(blk->bs, sector_num, nb_sectors);
 }
 
Signature · 1 line
2.1.0
author
#13 lore raw

PATCH v2 13/14 block: Remove "growable" from BDS

In reply to Max Reitz

Now that request clamping is done in the BlockBackend, the "growable" field can be removed from the BlockDriverState. All BDSs are now treated as being "growable" (that is, they are allowed to grow; they are not necessarily actually able to).

---
 block.c                   | 24 +++++-------------------
 block/qcow2.c             |  6 ------
 block/raw-posix.c         |  2 +-
 block/raw-win32.c         |  2 +-
 block/sheepdog.c          |  2 +-
 include/block/block_int.h |  3 ---
 6 files changed, 8 insertions(+), 31 deletions(-)
block.c +5 -19
index cbe4a32..9412377 100644
@@ -970,7 +970,6 @@ static int bdrv_open_common(BlockDriverState *bs, BlockDriverState *file,
     bs->zero_beyond_eof = true;
     open_flags = bdrv_open_flags(bs, flags);
     bs->read_only = !(open_flags & BDRV_O_RDWR);
-    bs->growable = !!(flags & BDRV_O_PROTOCOL);
 
     if (use_bdrv_whitelist && !bdrv_is_whitelisted(drv, bs->read_only)) {
         error_setg(errp,
@@ -1885,7 +1884,6 @@ void bdrv_close(BlockDriverState *bs)
         bs->encrypted = 0;
         bs->valid_key = 0;
         bs->sg = 0;
-        bs->growable = 0;
         bs->zero_beyond_eof = false;
         QDECREF(bs->options);
         bs->options = NULL;
@@ -2648,25 +2646,13 @@ exit:
 static int bdrv_check_byte_request(BlockDriverState *bs, int64_t offset,
                                    size_t size)
 {
-    int64_t len;
-
     if (size > INT_MAX) {
         return -EIO;
     }
 
-    if (!bdrv_is_inserted(bs))
+    if (!bdrv_is_inserted(bs)) {
         return -ENOMEDIUM;
-
-    if (bs->growable)
-        return 0;
-
-    len = bdrv_getlength(bs);
-
-    if (offset < 0)
-        return -EIO;
-
-    if ((offset > len) || (len - offset < size))
-        return -EIO;
+    }
 
     return 0;
 }
@@ -3048,10 +3034,10 @@ static int coroutine_fn bdrv_aligned_preadv(BlockDriverState *bs,
     }
 
     /* Forward the request to the BlockDriver */
-    if (!(bs->zero_beyond_eof && bs->growable)) {
+    if (!bs->zero_beyond_eof) {
         ret = drv->bdrv_co_readv(bs, sector_num, nb_sectors, qiov);
     } else {
-        /* Read zeros after EOF of growable BDSes */
+        /* Read zeros after EOF */
         int64_t total_sectors, max_nb_sectors;
 
         total_sectors = bdrv_nb_sectors(bs);
@@ -3331,7 +3317,7 @@ static int coroutine_fn bdrv_aligned_pwritev(BlockDriverState *bs,
 
     block_acct_highest_sector(&bs->stats, sector_num, nb_sectors);
 
-    if (bs->growable && ret >= 0) {
+    if (ret >= 0) {
         bs->total_sectors = MAX(bs->total_sectors, sector_num + nb_sectors);
     }
 
block/qcow2.c +0 -6
index e4e690a..947cd6c 100644
@@ -2520,15 +2520,12 @@ static int qcow2_save_vmstate(BlockDriverState *bs, QEMUIOVector *qiov,
 {
     BDRVQcowState *s = bs->opaque;
     int64_t total_sectors = bs->total_sectors;
-    int growable = bs->growable;
     bool zero_beyond_eof = bs->zero_beyond_eof;
     int ret;
 
     BLKDBG_EVENT(bs->file, BLKDBG_VMSTATE_SAVE);
-    bs->growable = 1;
     bs->zero_beyond_eof = false;
     ret = bdrv_pwritev(bs, qcow2_vm_state_offset(s) + pos, qiov);
-    bs->growable = growable;
     bs->zero_beyond_eof = zero_beyond_eof;
 
     /* bdrv_co_do_writev will have increased the total_sectors value to include
@@ -2543,15 +2540,12 @@ static int qcow2_load_vmstate(BlockDriverState *bs, uint8_t *buf,
                               int64_t pos, int size)
 {
     BDRVQcowState *s = bs->opaque;
-    int growable = bs->growable;
     bool zero_beyond_eof = bs->zero_beyond_eof;
     int ret;
 
     BLKDBG_EVENT(bs->file, BLKDBG_VMSTATE_LOAD);
-    bs->growable = 1;
     bs->zero_beyond_eof = false;
     ret = bdrv_pread(bs, qcow2_vm_state_offset(s) + pos, buf, size);
-    bs->growable = growable;
     bs->zero_beyond_eof = zero_beyond_eof;
 
     return ret;
block/raw-posix.c +1 -1
index e51293a..5debc72 100644
@@ -984,7 +984,7 @@ static int aio_worker(void *arg)
     switch (aiocb->aio_type & QEMU_AIO_TYPE_MASK) {
     case QEMU_AIO_READ:
         ret = handle_aiocb_rw(aiocb);
-        if (ret >= 0 && ret < aiocb->aio_nbytes && aiocb->bs->growable) {
+        if (ret >= 0 && ret < aiocb->aio_nbytes) {
             iov_memset(aiocb->aio_iov, aiocb->aio_niov, ret,
                       0, aiocb->aio_nbytes - ret);
 
block/raw-win32.c +1 -1
index 06243d7..dae5d2f 100644
@@ -101,7 +101,7 @@ static int aio_worker(void *arg)
     switch (aiocb->aio_type & QEMU_AIO_TYPE_MASK) {
     case QEMU_AIO_READ:
         count = handle_aiocb_rw(aiocb);
-        if (count < aiocb->aio_nbytes && aiocb->bs->growable) {
+        if (count < aiocb->aio_nbytes) {
             /* A short read means that we have reached EOF. Pad the buffer
              * with zeros for bytes after EOF. */
             iov_memset(aiocb->aio_iov, aiocb->aio_niov, count,
block/sheepdog.c +1 -1
index be3176f..b84e215 100644
@@ -2117,7 +2117,7 @@ static coroutine_fn int sd_co_writev(BlockDriverState *bs, int64_t sector_num,
     int64_t offset = (sector_num + nb_sectors) * BDRV_SECTOR_SIZE;
     BDRVSheepdogState *s = bs->opaque;
 
-    if (bs->growable && offset > s->inode.vdi_size) {
+    if (offset > s->inode.vdi_size) {
         ret = sd_truncate(bs, offset);
         if (ret < 0) {
             return ret;
include/block/block_int.h +0 -3
index 0c56755..538a356 100644
@@ -369,9 +369,6 @@ struct BlockDriverState {
     /* I/O Limits */
     BlockLimits bl;
 
-    /* Whether the disk can expand beyond total_sectors */
-    int growable;
-
     /* Whether produces zeros when read beyond eof */
     bool zero_beyond_eof;
 
Signature · 1 line
2.1.0
author
#14 lore raw

PATCH v2 14/14 block: Keep bdrv_check*_request()'s return value

In reply to Max Reitz

Do not throw away the value returned by bdrv_check_request() and bdrv_check_byte_request().

Fix up some coding style issues in the proximity of the affected hunks.

---
 block.c | 35 ++++++++++++++++++++++++-----------
 1 file changed, 24 insertions(+), 11 deletions(-)
block.c +24 -11
index 9412377..aa1758b 100644
@@ -3099,8 +3099,10 @@ static int coroutine_fn bdrv_co_do_preadv(BlockDriverState *bs,
     if (!drv) {
         return -ENOMEDIUM;
     }
-    if (bdrv_check_byte_request(bs, offset, bytes)) {
-        return -EIO;
+
+    ret = bdrv_check_byte_request(bs, offset, bytes);
+    if (ret < 0) {
+        return ret;
     }
 
     if (bs->copy_on_read) {
@@ -3346,8 +3348,10 @@ static int coroutine_fn bdrv_co_do_pwritev(BlockDriverState *bs,
     if (bs->read_only) {
         return -EACCES;
     }
-    if (bdrv_check_byte_request(bs, offset, bytes)) {
-        return -EIO;
+
+    ret = bdrv_check_byte_request(bs, offset, bytes);
+    if (ret < 0) {
+        return ret;
     }
 
     /* throttling disk I/O */
@@ -4171,12 +4175,18 @@ int bdrv_write_compressed(BlockDriverState *bs, int64_t sector_num,
                           const uint8_t *buf, int nb_sectors)
 {
     BlockDriver *drv = bs->drv;
-    if (!drv)
+    int ret;
+
+    if (!drv) {
         return -ENOMEDIUM;
-    if (!drv->bdrv_write_compressed)
+    }
+    if (!drv->bdrv_write_compressed) {
         return -ENOTSUP;
-    if (bdrv_check_request(bs, sector_num, nb_sectors))
-        return -EIO;
+    }
+    ret = bdrv_check_request(bs, sector_num, nb_sectors);
+    if (ret < 0) {
+        return ret;
+    }
 
     assert(QLIST_EMPTY(&bs->dirty_bitmaps));
 
@@ -5094,12 +5104,15 @@ static void coroutine_fn bdrv_discard_co_entry(void *opaque)
 int coroutine_fn bdrv_co_discard(BlockDriverState *bs, int64_t sector_num,
                                  int nb_sectors)
 {
-    int max_discard;
+    int max_discard, ret;
 
     if (!bs->drv) {
         return -ENOMEDIUM;
-    } else if (bdrv_check_request(bs, sector_num, nb_sectors)) {
-        return -EIO;
+    }
+
+    ret = bdrv_check_request(bs, sector_num, nb_sectors);
+    if (ret < 0) {
+        return ret;
     } else if (bs->read_only) {
         return -EROFS;
     }
Signature · 1 line
2.1.0
#15 lore raw
In reply to Max Reitz
Quoted 21 lines · Max Reitz

This series removes the "growable" field from the BlockDriverState object. Its use was to clamp guest requests against the limits of the BDS; however, this can now be done more easily by moving those checks into the BlockBackend functions.

In a future series, "growable" may be reintroduced (maybe with a different name); it will then signify whether a BDS is able to grow (in contrast to the current "growable", which signifies whether it is allowed to). Maybe I will add it to the BlockDriver instead of the BDS, though.

To be able to remove that field, qemu-io needs to be converted to BlockBackend, which is done by this series as well. While working on that I decided to convert blk_new_with_bs()+bdrv_open() to blk_new_open(). I was skeptical about that decision at first, but it seems good now that I was able to replace nearly every blk_new_with_bs() call by blk_new_open(). In a future series I may try to convert some remaining bdrv_open() calls to blk_new_open() as well. (And, in fact, in a future series I *will* replace the last remaining blk_new_with_bs() outside of blk_new_open() by blk_new_open().)

Quoted 8 lines

Finally, the question needs to be asked: If, after this series, every BDS is allowed to grow, are there any users which do not use BB, but should still be disallowed from reading/writing beyond a BDS's limits? The only users I could see were the block jobs. Some of them should indeed be converted to BB; but none of them takes a user-supplied offset or size, all work on the full BDS (or only on parts which have been modified, etc.). Therefore, it is by design impossible for them to exceed the BDS's limits, which makes making all BDS's growable safe.

Hello Max,
I applied the first four patches in this series, and this is what I get:

hw/block/xen_disk.c: In function ‘blk_connect’:
hw/block/xen_disk.c:907:27: error: implicit declaration of function ‘qstring_from_str’ [-Werror=implicit-function-declaration] hw/block/xen_disk.c:907:27: error: nested extern declaration of ‘qstring_from_str’ [-Werror=nested-externs] hw/block/xen_disk.c:907:27: error: invalid type argument of ‘->’ (have ‘int’)
hw/block/xen_disk.c:901:22: error: unused variable ‘drv’ [-Werror=unused-variable] hw/block/xen_disk.c:900:23: error: unused variable ‘blk’ [-Werror=unused-variable]

it would be great if you could build test it.

Quoted 76 lines

v2:
- Rebased [Kevin]
- Patch 2: Added a TODO comment about removing @filename and @flags from blk_new_open() when possible [Kevin]

git-backport-diff against v1:

Key:
[----] : patches are identical
[####] : number of functional differences between upstream/downstream patch [down] : patch is downstream-only
The flags [FC] indicate (F)unctional and (C)ontextual differences, respectively

001/14:[----] [--] 'block: Lift some BDS functions to the BlockBackend' 002/14:[0006] [FC] 'block: Add blk_new_open()'
003/14:[----] [-C] 'blockdev: Use blk_new_open() in blockdev_init()' 004/14:[----] [--] 'block/xen: Use blk_new_open() in blk_connect()' 005/14:[----] [--] 'qemu-img: Use blk_new_open() in img_open()' 006/14:[----] [--] 'qemu-img: Use blk_new_open() in img_rebase()' 007/14:[----] [--] 'qemu-img: Use BlockBackend as far as possible' 008/14:[----] [--] 'qemu-nbd: Use blk_new_open() in main()' 009/14:[----] [--] 'qemu-io: Use blk_new_open() in openfile()' 010/14:[0002] [FC] 'qemu-io: Remove "growable" option' 011/14:[----] [--] 'qemu-io: Use BlockBackend'
012/14:[----] [--] 'block: Clamp BlockBackend requests' 013/14:[----] [--] 'block: Remove "growable" from BDS' 014/14:[----] [--] 'block: Keep bdrv_check*_request()'s return value'

Max Reitz (14):
block: Lift some BDS functions to the BlockBackend
block: Add blk_new_open()
blockdev: Use blk_new_open() in blockdev_init() block/xen: Use blk_new_open() in blk_connect()
qemu-img: Use blk_new_open() in img_open()
qemu-img: Use blk_new_open() in img_rebase()
qemu-img: Use BlockBackend as far as possible
qemu-nbd: Use blk_new_open() in main()
qemu-io: Use blk_new_open() in openfile()
qemu-io: Remove "growable" option
qemu-io: Use BlockBackend
block: Clamp BlockBackend requests
block: Remove "growable" from BDS
block: Keep bdrv_check*_request()'s return value

 block.c                        |  59 +++++-----
 block/block-backend.c          | 219 +++++++++++++++++++++++++++++++++++++
 block/qcow2.c                  |   6 --
 block/raw-posix.c              |   2 +-
 block/raw-win32.c              |   2 +-
 block/sheepdog.c               |   2 +-
 blockdev.c                     |  92 ++++++++--------
 hmp.c                          |   9 +-
 hw/block/xen_disk.c            |  24 ++---
 include/block/block_int.h      |   3 -
 include/qemu-io.h              |   4 +-
 include/sysemu/block-backend.h |  12 +++
 qemu-img.c                     | 171 ++++++++++++++---------------
 qemu-io-cmds.c                 | 238 ++++++++++++++++++++++-------------------
 qemu-io.c                      |  58 ++++------
 qemu-nbd.c                     |  25 ++---
 tests/qemu-iotests/016         |  73 -------------
 tests/qemu-iotests/016.out     |  23 ----
 tests/qemu-iotests/051.out     |  60 +++++------
 tests/qemu-iotests/087.out     |   8 +-
 tests/qemu-iotests/group       |   1 -
 21 files changed, 590 insertions(+), 501 deletions(-)
 delete mode 100755 tests/qemu-iotests/016
 delete mode 100644 tests/qemu-iotests/016.out
Signature · 1 line
2.1.0
author
#16 lore raw
Quoted 33 lines · Stefano Stabellini
Quoted 21 lines · Max Reitz

This series removes the "growable" field from the BlockDriverState object. Its use was to clamp guest requests against the limits of the BDS; however, this can now be done more easily by moving those checks into the BlockBackend functions.

In a future series, "growable" may be reintroduced (maybe with a different name); it will then signify whether a BDS is able to grow (in contrast to the current "growable", which signifies whether it is allowed to). Maybe I will add it to the BlockDriver instead of the BDS, though.

To be able to remove that field, qemu-io needs to be converted to BlockBackend, which is done by this series as well. While working on that I decided to convert blk_new_with_bs()+bdrv_open() to blk_new_open(). I was skeptical about that decision at first, but it seems good now that I was able to replace nearly every blk_new_with_bs() call by blk_new_open(). In a future series I may try to convert some remaining bdrv_open() calls to blk_new_open() as well. (And, in fact, in a future series I *will* replace the last remaining blk_new_with_bs() outside of blk_new_open() by blk_new_open().)

Quoted 8 lines

Finally, the question needs to be asked: If, after this series, every BDS is allowed to grow, are there any users which do not use BB, but should still be disallowed from reading/writing beyond a BDS's limits? The only users I could see were the block jobs. Some of them should indeed be converted to BB; but none of them takes a user-supplied offset or size, all work on the full BDS (or only on parts which have been modified, etc.). Therefore, it is by design impossible for them to exceed the BDS's limits, which makes making all BDS's growable safe.

Hello Max,
I applied the first four patches in this series, and this is what I get:

Quoted 8 lines

hw/block/xen_disk.c: In function ‘blk_connect’:
hw/block/xen_disk.c:907:27: error: implicit declaration of function ‘qstring_from_str’ [-Werror=implicit-function-declaration] hw/block/xen_disk.c:907:27: error: nested extern declaration of ‘qstring_from_str’ [-Werror=nested-externs] hw/block/xen_disk.c:907:27: error: invalid type argument of ‘->’ (have ‘int’)
hw/block/xen_disk.c:901:22: error: unused variable ‘drv’ [-Werror=unused-variable] hw/block/xen_disk.c:900:23: error: unused variable ‘blk’ [-Werror=unused-variable]

it would be great if you could build test it.

Sorry, of course I build test my patches, but I just don't have some subsystems enabled (there was no real reason not to have the Xen backends enabled, but that's just how it was; another example are Windows-specific areas), so I have to work blind on them. In this case I forgot to include the QMP type headers and to remove the now unused variables.

Thank you for looking at my patches and finding this issue! I'll send a fixed v3.

Max

Quoted 74 lines
Quoted 74 lines

v2:
- Rebased [Kevin]
- Patch 2: Added a TODO comment about removing @filename and @flags from blk_new_open() when possible [Kevin]

git-backport-diff against v1:

Key:
[----] : patches are identical
[####] : number of functional differences between upstream/downstream patch [down] : patch is downstream-only
The flags [FC] indicate (F)unctional and (C)ontextual differences, respectively

001/14:[----] [--] 'block: Lift some BDS functions to the BlockBackend' 002/14:[0006] [FC] 'block: Add blk_new_open()'
003/14:[----] [-C] 'blockdev: Use blk_new_open() in blockdev_init()' 004/14:[----] [--] 'block/xen: Use blk_new_open() in blk_connect()' 005/14:[----] [--] 'qemu-img: Use blk_new_open() in img_open()' 006/14:[----] [--] 'qemu-img: Use blk_new_open() in img_rebase()' 007/14:[----] [--] 'qemu-img: Use BlockBackend as far as possible' 008/14:[----] [--] 'qemu-nbd: Use blk_new_open() in main()' 009/14:[----] [--] 'qemu-io: Use blk_new_open() in openfile()' 010/14:[0002] [FC] 'qemu-io: Remove "growable" option' 011/14:[----] [--] 'qemu-io: Use BlockBackend'
012/14:[----] [--] 'block: Clamp BlockBackend requests' 013/14:[----] [--] 'block: Remove "growable" from BDS' 014/14:[----] [--] 'block: Keep bdrv_check*_request()'s return value'

Max Reitz (14):
block: Lift some BDS functions to the BlockBackend
block: Add blk_new_open()
blockdev: Use blk_new_open() in blockdev_init() block/xen: Use blk_new_open() in blk_connect()
qemu-img: Use blk_new_open() in img_open()
qemu-img: Use blk_new_open() in img_rebase()
qemu-img: Use BlockBackend as far as possible
qemu-nbd: Use blk_new_open() in main()
qemu-io: Use blk_new_open() in openfile()
qemu-io: Remove "growable" option
qemu-io: Use BlockBackend
block: Clamp BlockBackend requests
block: Remove "growable" from BDS
block: Keep bdrv_check*_request()'s return value

  block.c                        |  59 +++++-----
  block/block-backend.c          | 219 +++++++++++++++++++++++++++++++++++++
  block/qcow2.c                  |   6 --
  block/raw-posix.c              |   2 +-
  block/raw-win32.c              |   2 +-
  block/sheepdog.c               |   2 +-
  blockdev.c                     |  92 ++++++++--------
  hmp.c                          |   9 +-
  hw/block/xen_disk.c            |  24 ++---
  include/block/block_int.h      |   3 -
  include/qemu-io.h              |   4 +-
  include/sysemu/block-backend.h |  12 +++
  qemu-img.c                     | 171 ++++++++++++++---------------
  qemu-io-cmds.c                 | 238 ++++++++++++++++++++++-------------------
  qemu-io.c                      |  58 ++++------
  qemu-nbd.c                     |  25 ++---
  tests/qemu-iotests/016         |  73 -------------
  tests/qemu-iotests/016.out     |  23 ----
  tests/qemu-iotests/051.out     |  60 +++++------
  tests/qemu-iotests/087.out     |   8 +-
  tests/qemu-iotests/group       |   1 -
  21 files changed, 590 insertions(+), 501 deletions(-)

delete mode 100755 tests/qemu-iotests/016
delete mode 100644 tests/qemu-iotests/016.out

Signature · 1 line
2.1.0