Skip to contentSkip to conversation

block: Remove "growable", add blk_new_open()

PATCH Superseded Max Reitz 4 participants 49 messages Jan 26, 2015 – Feb 2, 2015 Block layer

v3 of 4 Superseded Latest is v4 →

lore
Share & tools
lore
author
#8 lore raw

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

In reply to Max Reitz
---
 hw/block/xen_disk.c | 28 ++++++++++++----------------
 1 file changed, 12 insertions(+), 16 deletions(-)
hw/block/xen_disk.c +12 -16
index 21842a0..1b0257c 100644
@@ -40,6 +40,8 @@
 #include "xen_blkif.h"
 #include "sysemu/blockdev.h"
 #include "sysemu/block-backend.h"
+#include "qapi/qmp/qdict.h"
+#include "qapi/qmp/qstring.h"
 
 /* ------------------------------------------------------------- */
 
@@ -897,30 +899,24 @@ static int blk_connect(struct XenDevice *xendev)
     blkdev->dinfo = drive_get(IF_XEN, 0, index);
     if (!blkdev->dinfo) {
         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
#24 lore raw

PATCH v3 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
#12 lore raw

PATCH v3 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 4e9a7f5..be1953d 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
#5 lore raw

PATCH v3 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
#16 lore raw

PATCH v3 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 be1953d..0b23c87 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[PATH_MAX];
+        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
#26 lore raw

PATCH v3 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
#21 lore raw

PATCH v3 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 0b23c87..8b4139e 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[PATH_MAX];
 
@@ -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
#30 lore raw

PATCH v3 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
#45 lore raw

PATCH v3 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 356a857..4a5f8fc 100644
@@ -3096,8 +3096,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) {
@@ -3343,8 +3345,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 */
@@ -4168,12 +4172,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));
 
@@ -5091,12 +5101,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
author
#39 lore raw

PATCH v3 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
#41 lore raw

PATCH v3 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 d45e4dd..356a857 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;
@@ -2645,25 +2643,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;
 }
@@ -3045,10 +3031,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);
@@ -3328,7 +3314,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 dbaf016..2b31dac 100644
@@ -2521,15 +2521,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
@@ -2544,15 +2541,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 7ad1950..b340e7e 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
#37 lore raw

PATCH v3 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

Max Reitz

author
#0 lore raw

v3 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.

v3:
- Rebased (onto Stefan's branch rebased onto master)
- Fixed patch 4 [Stefano]

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 v2:

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:[----] [--] 'block: Add blk_new_open()'
003/14:[----] [--] 'blockdev: Use blk_new_open() in blockdev_init()' 004/14:[0004] [FC] 'block/xen: Use blk_new_open() in blk_connect()' 005/14:[----] [--] 'qemu-img: Use blk_new_open() in img_open()' 006/14:[----] [-C] 'qemu-img: Use blk_new_open() in img_rebase()' 007/14:[----] [-C] '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:[----] [--] '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'

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:[0004] [FC] 'block/xen: Use blk_new_open() in blk_connect()' 005/14:[----] [--] 'qemu-img: Use blk_new_open() in img_open()' 006/14:[----] [-C] 'qemu-img: Use blk_new_open() in img_rebase()' 007/14:[----] [-C] '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            |  28 +++--
 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, 592 insertions(+), 503 deletions(-)
 delete mode 100755 tests/qemu-iotests/016
 delete mode 100644 tests/qemu-iotests/016.out
Signature · 1 line
2.1.0
author
#3 lore raw

PATCH v3 2/14 block: Add blk_new_open()

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
#1 lore raw

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

In reply to Max Reitz

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
#47 lore raw
In reply to Max Reitz
Quoted 31 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().)

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.

Quoted 3 lines

v3:
- Rebased (onto Stefan's branch rebased onto master)
- Fixed patch 4 [Stefano]

Thanks! It builds correctly and seems to work OK.

Quoted 98 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 v2:

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:[----] [--] 'block: Add blk_new_open()'
003/14:[----] [--] 'blockdev: Use blk_new_open() in blockdev_init()' 004/14:[0004] [FC] 'block/xen: Use blk_new_open() in blk_connect()' 005/14:[----] [--] 'qemu-img: Use blk_new_open() in img_open()' 006/14:[----] [-C] 'qemu-img: Use blk_new_open() in img_rebase()' 007/14:[----] [-C] '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:[----] [--] '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'

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:[0004] [FC] 'block/xen: Use blk_new_open() in blk_connect()' 005/14:[----] [--] 'qemu-img: Use blk_new_open() in img_open()' 006/14:[----] [-C] 'qemu-img: Use blk_new_open() in img_rebase()' 007/14:[----] [-C] '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            |  28 +++--
 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, 592 insertions(+), 503 deletions(-)
 delete mode 100755 tests/qemu-iotests/016
 delete mode 100644 tests/qemu-iotests/016.out
Signature · 1 line
2.1.0
Reviewed-by on v3 1/14
#2 lore raw

Re: [Qemu-devel] [PATCH v3 01/14] block: Lift some BDS functions to the BlockBackend

In reply to Max Reitz
Quoted 9 lines · Max Reitz

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()

Quoted 5 lines
---
 block/block-backend.c          | 33 +++++++++++++++++++++++++++++++++
 include/sysemu/block-backend.h |  9 +++++++++
 2 files changed, 42 insertions(+)
Signature · 2 lines
Eric Blake   eblake redhat com    +1-919-301-3266
Libvirt virtualization library http://libvirt.org
Reviewed-by on v3 2/14
#4 lore raw

Re: [Qemu-devel] [PATCH v3 02/14] block: Add blk_new_open()

In reply to Max Reitz
Quoted 7 lines · 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

s/naturally/natural/

Quoted 9 lines

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.

Quoted 6 lines
---
 block/block-backend.c          | 34 ++++++++++++++++++++++++++++++++++
 include/sysemu/block-backend.h |  3 +++
 2 files changed, 37 insertions(+)
Signature · 2 lines
Eric Blake   eblake redhat com    +1-919-301-3266
Libvirt virtualization library http://libvirt.org
Reviewed-by on v3 3/14
#6 lore raw

Re: [Qemu-devel] [PATCH v3 03/14] blockdev: Use blk_new_open() in blockdev_init()

In reply to Max Reitz
Quoted 12 lines · 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(-)

Testing: -drive file=TEST_DIR/t.qcow2,driver=raw,format=qcow2

tests/qemu-iotests/051.out Formatting 'TEST_DIR/t.IMGFMT', fmt=IMGFMT size=134217728 backing_file='TEST_DIR · line 44

Quoted 2 lines
tests/qemu-iotests/051.out +1 -1
-QEMU_PROG: -drive file=TEST_DIR/t.qcow2,driver=raw,format=qcow2: could not open disk image TEST_DIR/t.qcow2: Driver specified twice
+QEMU_PROG: -drive file=TEST_DIR/t.qcow2,driver=raw,format=qcow2: Cannot specify both 'driver' and 'format'

Is it possible to specify driver=qcow2,format=qcow2? Should it be? Either way, are we testing the outcome of that? (that is, there is a difference between two competing options, and two spellings of the same option - I could go for either rejecting the duplication, or for allowing it when the two are the same, whichever is easier, but would like to make sure it is tested so we know if we change our minds later whether we are risking a regression).

tests/qemu-iotests/051.out QEMU_PROG: -drive file=TEST_DIR/t.qcow2,readonly=on,read-only=off: 'read-only' a · line 326

Quoted 9 lines

=== Specifying both an option and its legacy alias ===

tests/qemu-iotests/051.out +1 -1
@@ -323,13 +323,13 @@ QEMU_PROG: -drive file=TEST_DIR/t.qcow2,readonly=on,read-only=off: 'read-only' a
 === Parsing protocol from file name ===
 
 Testing: -hda foo:bar
-QEMU_PROG: -hda foo:bar: could not open disk image foo:bar: Unknown protocol
+QEMU_PROG: -hda foo:bar: Unknown protocol

Not the fault of this patch, but can this error message be improved? Even 'Unknown protocol: foo' would read better.

All of the other shorter error messages still seem to read fine, and the decrease in verbosity could be argued as a feature. So overall, I'm fine with this patch.

Signature · 2 lines
Eric Blake   eblake redhat com    +1-919-301-3266
Libvirt virtualization library http://libvirt.org
Reviewed-by on v3 4/14
#9 lore raw

Re: [Qemu-devel] [PATCH v3 04/14] block/xen: Use blk_new_open() in blk_connect()

In reply to Max Reitz
Quoted 5 lines · Max Reitz
---
 hw/block/xen_disk.c | 28 ++++++++++++----------------
 1 file changed, 12 insertions(+), 16 deletions(-)
Signature · 2 lines
Eric Blake   eblake redhat com    +1-919-301-3266
Libvirt virtualization library http://libvirt.org
author
#7 lore raw

Re: [Qemu-devel] [PATCH v3 03/14] blockdev: Use blk_new_open() in blockdev_init()

In reply to Eric Blake
Quoted 11 lines · Eric Blake
Quoted 10 lines · 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(-)
Quoted 4 lines

tests/qemu-iotests/051.out Formatting 'TEST_DIR/t.IMGFMT', fmt=IMGFMT size=134217728 backing_file='TEST_DIR · line 44

Quoted 3 lines

Testing: -drive file=TEST_DIR/t.qcow2,driver=raw,format=qcow2

tests/qemu-iotests/051.out +1 -1
-QEMU_PROG: -drive file=TEST_DIR/t.qcow2,driver=raw,format=qcow2: could not open disk image TEST_DIR/t.qcow2: Driver specified twice
+QEMU_PROG: -drive file=TEST_DIR/t.qcow2,driver=raw,format=qcow2: Cannot specify both 'driver' and 'format'

Is it possible to specify driver=qcow2,format=qcow2? Should it be?

No, it isn't, and in my opinion it shouldn't be (just a personal feeling, though).

Quoted 6 lines

Either way, are we testing the outcome of that? (that is, there is a difference between two competing options, and two spellings of the same option - I could go for either rejecting the duplication, or for allowing it when the two are the same, whichever is easier, but would like to make sure it is tested so we know if we change our minds later whether we are risking a regression).

No, we aren't yet. Albeit not really related to this series, it is a good point, so I'll probably just add a test case in v4.

Quoted 11 lines

tests/qemu-iotests/051.out QEMU_PROG: -drive file=TEST_DIR/t.qcow2,readonly=on,read-only=off: 'read-only' a · line 326

Quoted 9 lines

=== Specifying both an option and its legacy alias ===

tests/qemu-iotests/051.out +1 -1
@@ -323,13 +323,13 @@ QEMU_PROG: -drive file=TEST_DIR/t.qcow2,readonly=on,read-only=off: 'read-only' a
  === Parsing protocol from file name ===
 
  Testing: -hda foo:bar
-QEMU_PROG: -hda foo:bar: could not open disk image foo:bar: Unknown protocol
+QEMU_PROG: -hda foo:bar: Unknown protocol

Not the fault of this patch, but can this error message be improved? Even 'Unknown protocol: foo' would read better.

Indeed, I'll fix it.

Quoted 6 lines

All of the other shorter error messages still seem to read fine, and the decrease in verbosity could be argued as a feature. So overall, I'm fine with this patch.

Thank you!

Max

Reviewed-by on v3 6/14
#17 lore raw

Re: [Qemu-devel] [PATCH v3 06/14] qemu-img: Use blk_new_open() in img_rebase()

In reply to Max Reitz

qemu-img.c static int img_rebase(int argc, char **argv) · line 2531

Quoted 13 lines · Max Reitz
---
 qemu-img.c | 57 ++++++++++++++++++++++++---------------------------------
 1 file changed, 24 insertions(+), 33 deletions(-)
qemu-img.c +3 -1
-
     /* For safe rebasing we need to compare old and new backing file */
     if (!unsafe) {
         char backing_name[PATH_MAX];
+        QDict *options = NULL;
+
+        if (!unsafe && bs->backing_format[0] != '\0') {

Useless conditional ('!unsafe' is always true here).

With that fixed,

Signature · 2 lines
Eric Blake   eblake redhat com    +1-919-301-3266
Libvirt virtualization library http://libvirt.org
Reviewed-by on v3 7/14
#22 lore raw

Re: [Qemu-devel] [PATCH v3 07/14] qemu-img: Use BlockBackend as far as possible

In reply to Max Reitz
Quoted 23 lines · 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 +1 -4
index 0b23c87..8b4139e 100644
--- a/qemu-img.c
@@ -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);

qemu-img.c static int img_compare(int argc, char **argv) · line 1142

Quoted 10 lines
qemu-img.c +3 -1
+    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);

The naming feels awkward; your conversion is now using bytes while the old code was using sectors, so 'total_sectors2' feels weird...

qemu-img.c static int img_compare(int argc, char **argv) · line 1151

Quoted 9 lines
qemu-img.c +3 -0
     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;

...at least you end up converting to sectors after all. But it makes me wonder if you should have blk_nb_sectors(), and/or temporary intermediate variables to avoid cross-unit confusion.

qemu-img.c static int img_convert(int argc, char **argv) · line 1490

Quoted 13 lines
qemu-img.c +2 -1
@@ -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;

Another instance of the confusion.

Quoted 13 lines
qemu-img.c +2 -2
         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);

qemu-img.c static int img_convert(int argc, char **argv) · line 1645

Quoted 10 lines
qemu-img.c +4 -1
         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) {

And another.

Quoted 12 lines
qemu-img.c +3 -3
@@ -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) {

...

qemu-img.c static int img_rebase(int argc, char **argv) · line 2616

Quoted 5 lines
qemu-img.c +3 -2
-        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;

and another.

I did not closely audit if there were any other conversions that should have been made. Also, I suspect that a blk_nb_sectors() as a pre-req patch would make this one feel cleaner if you respin and rebase. But if we don't add blk_nb_sectors(), at least this version of the patch appears to be clean with what it does, so you can consider this to be a rather weak:

Signature · 2 lines
Eric Blake   eblake redhat com    +1-919-301-3266
Libvirt virtualization library http://libvirt.org
author
#23 lore raw

Re: [Qemu-devel] [PATCH v3 07/14] qemu-img: Use BlockBackend as far as possible

In reply to Eric Blake
Quoted 108 lines · Eric Blake
Quoted 22 lines · 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 +1 -4
index 0b23c87..8b4139e 100644
--- a/qemu-img.c
@@ -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);

qemu-img.c static int img_compare(int argc, char **argv) · line 1142

Quoted 10 lines
qemu-img.c +3 -1
+    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);

The naming feels awkward; your conversion is now using bytes while the old code was using sectors, so 'total_sectors2' feels weird...

qemu-img.c static int img_compare(int argc, char **argv) · line 1151

Quoted 9 lines
qemu-img.c +3 -0
      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;

...at least you end up converting to sectors after all. But it makes me wonder if you should have blk_nb_sectors(), and/or temporary intermediate variables to avoid cross-unit confusion.

qemu-img.c static int img_convert(int argc, char **argv) · line 1490

Quoted 13 lines
qemu-img.c +2 -1
@@ -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;

Another instance of the confusion.

Quoted 13 lines
qemu-img.c +2 -2
          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);

qemu-img.c static int img_convert(int argc, char **argv) · line 1645

Quoted 10 lines
qemu-img.c +4 -1
          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) {

And another.

Quoted 12 lines
qemu-img.c +3 -3
@@ -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) {

...

qemu-img.c static int img_rebase(int argc, char **argv) · line 2616

Quoted 5 lines
qemu-img.c +3 -2
-        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;
Quoted 5 lines

and another.

I did not closely audit if there were any other conversions that should have been made. Also, I suspect that a blk_nb_sectors() as a pre-req patch would make this one feel cleaner if you respin and rebase.

The reason why I don't want to add blk_nb_sectors() is simply that we want to convert all the code to using bytes at a later point anyway, so I felt like it'd be a step backwards to introduce blk_nb_sectors().

However, I do see your point and intermediate variables probably don't make this any nicer. Also, if we get to convert the code to bytes, finding all instances of {blk,bdrv}_nb_sectors() will be one of the easier tasks, so I'll just introduce blk_nb_sectors().

Max

Quoted 7 lines

But if
we don't add blk_nb_sectors(), at least this version of the patch appears to be clean with what it does, so you can consider this to be a rather weak:

author
#18 lore raw

Re: [Qemu-devel] [PATCH v3 06/14] qemu-img: Use blk_new_open() in img_rebase()

In reply to Eric Blake
Quoted 6 lines · Eric Blake
Quoted 5 lines · Max Reitz
---
  qemu-img.c | 57 ++++++++++++++++++++++++---------------------------------
  1 file changed, 24 insertions(+), 33 deletions(-)
Quoted 8 lines

qemu-img.c static int img_rebase(int argc, char **argv) · line 2531

Quoted 7 lines
qemu-img.c +3 -1
-
      /* For safe rebasing we need to compare old and new backing file */
      if (!unsafe) {
          char backing_name[PATH_MAX];
+        QDict *options = NULL;
+
+        if (!unsafe && bs->backing_format[0] != '\0') {

Useless conditional ('!unsafe' is always true here).

Oh, right, thanks.

Max

Quoted 2 lines

With that fixed,

Reviewed-by on v3 10/14
#31 lore raw

Re: [Qemu-devel] [PATCH v3 10/14] qemu-io: Remove "growable" option

In reply to Max Reitz
Quoted 6 lines · 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.

Quoted 7 lines

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.

---

Do we want to ever reuse the test number that you are deleting?

Signature · 2 lines
Eric Blake   eblake redhat com    +1-919-301-3266
Libvirt virtualization library http://libvirt.org
author
#32 lore raw

Re: [Qemu-devel] [PATCH v3 10/14] qemu-io: Remove "growable" option

Quoted 12 lines · Eric Blake
Quoted 11 lines · 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.

Quoted 5 lines
Quoted 2 lines

---

Do we want to ever reuse the test number that you are deleting?

Good question, I think I have talked about that with Kevin before. It would not hurt too much if we were to accidentally reuse the test case number, most certainly not here in upstream.

However, for all downstream versions of qemu, this might make adding the new test 16 difficult; but certainly not impossible (if someone is affected by this issue, he/she can just use 999 or something). So we may want to keep in mind not to reuse number 16, but if someone does, so be it.

Max

Reviewed-by on v3 11/14
#38 lore raw

Re: [Qemu-devel] [PATCH v3 11/14] qemu-io: Use BlockBackend

In reply to Max Reitz
Quoted 7 lines · 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

s/excempt/exempt/

Quoted 24 lines

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).

---

qemu-io-cmds.c +2 -2
-static int map_f(BlockDriverState *bs, int argc, char **argv)
+static int map_f(BlockBackend *blk, int argc, char **argv)
 {
     int64_t offset;
-    int64_t nb_sectors;
+    int64_t nb_sectors, total_sectors;
     char s1[64];
     int64_t num;
     int ret;
     const char *retstr;
 

qemu-io-cmds.c static int map_is_allocated(BlockDriverState *bs, int64_t sector_num, · line 1934

Quoted 10 lines
qemu-io-cmds.c +8 -1
     offset = 0;
-    nb_sectors = bs->total_sectors;
+    total_sectors = blk_getlength(blk);
+    if (total_sectors < 0) {
+        error_report("Failed to query image length: %s",
+                     strerror(-total_sectors));
+        return 0;
+    }
+
+    total_sectors /= BDRV_SECTOR_SIZE;

Another spot that would benefit from a (possibly temporary) bdrv_nb_sectors().

Signature · 2 lines
Eric Blake   eblake redhat com    +1-919-301-3266
Libvirt virtualization library http://libvirt.org
#33 lore raw

Re: [Qemu-devel] [PATCH v3 10/14] qemu-io: Remove "growable" option

In reply to Max Reitz
Quoted 19 lines · Max Reitz
Quoted 12 lines · Eric Blake
Quoted 11 lines · 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.

Quoted 5 lines
Quoted 2 lines

---

Do we want to ever reuse the test number that you are deleting?

Quoted 8 lines

Good question, I think I have talked about that with Kevin before. It would not hurt too much if we were to accidentally reuse the test case number, most certainly not here in upstream.

However, for all downstream versions of qemu, this might make adding the new test 16 difficult; but certainly not impossible (if someone is affected by this issue, he/she can just use 999 or something). So we may want to keep in mind not to reuse number 16, but if someone does, so be it.

Is it worth a placeholder file that has a comment mentioning that the test number is intentionally reserved (and if someone attempts to run, always passes)?

Signature · 2 lines
Eric Blake   eblake redhat com    +1-919-301-3266
Libvirt virtualization library http://libvirt.org
author
#34 lore raw

Re: [Qemu-devel] [PATCH v3 10/14] qemu-io: Remove "growable" option

Quoted 23 lines · Eric Blake
Quoted 22 lines · Max Reitz
Quoted 12 lines · Eric Blake
Quoted 11 lines · 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.

Quoted 5 lines
Quoted 2 lines

---

Do we want to ever reuse the test number that you are deleting?

Good question, I think I have talked about that with Kevin before. It would not hurt too much if we were to accidentally reuse the test case number, most certainly not here in upstream.

Quoted 7 lines
Quoted 4 lines

However, for all downstream versions of qemu, this might make adding the new test 16 difficult; but certainly not impossible (if someone is affected by this issue, he/she can just use 999 or something). So we may want to keep in mind not to reuse number 16, but if someone does, so be it.

Is it worth a placeholder file that has a comment mentioning that the test number is intentionally reserved (and if someone attempts to run, always passes)?

Seems good to me. It's a minor effort now and may avert some hassle later.

Max

Reviewed-by on v3 12/14
#40 lore raw

Re: [Qemu-devel] [PATCH v3 12/14] block: Clamp BlockBackend requests

In reply to Max Reitz
Quoted 9 lines · 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(+)
Signature · 2 lines
Eric Blake   eblake redhat com    +1-919-301-3266
Libvirt virtualization library http://libvirt.org
Reviewed-by on v3 13/14
#42 lore raw

Re: [Qemu-devel] [PATCH v3 13/14] block: Remove "growable" from BDS

In reply to Max Reitz
Quoted 10 lines · 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).

---

+++ b/include/block/block_int.h

include/block/block_int.h struct BlockDriverState { · line 374

Quoted 6 lines
include/block/block_int.h +0 -3
@@ -369,9 +369,6 @@ struct BlockDriverState {
     /* I/O Limits */
     BlockLimits bl;
 
-    /* Whether the disk can expand beyond total_sectors */
-    int growable;
-

Yay - getting rid of an 'int' that was only used to hold 'bool' values :)

Signature · 2 lines
Eric Blake   eblake redhat com    +1-919-301-3266
Libvirt virtualization library http://libvirt.org
Reviewed-by on v3 14/14
#46 lore raw

Re: [Qemu-devel] [PATCH v3 14/14] block: Keep bdrv_check*_request()'s return value

In reply to Max Reitz
Quoted 9 lines · 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(-)
Signature · 2 lines
Eric Blake   eblake redhat com    +1-919-301-3266
Libvirt virtualization library http://libvirt.org
#10 lore raw

Re: [Qemu-devel] [PATCH v3 04/14] block/xen: Use blk_new_open() in blk_connect()

In reply to Max Reitz

Am 26.01.2015 um 16:00 hat Max Reitz geschrieben:

Quoted 23 lines
---
 hw/block/xen_disk.c | 28 ++++++++++++----------------
 1 file changed, 12 insertions(+), 16 deletions(-)
hw/block/xen_disk.c +2 -1
index 21842a0..1b0257c 100644
@@ -40,6 +40,8 @@
 #include "xen_blkif.h"
 #include "sysemu/blockdev.h"
 #include "sysemu/block-backend.h"
+#include "qapi/qmp/qdict.h"
+#include "qapi/qmp/qstring.h"
 
 /* ------------------------------------------------------------- */
 
@@ -897,30 +899,24 @@ static int blk_connect(struct XenDevice *xendev)
     blkdev->dinfo = drive_get(IF_XEN, 0, index);
     if (!blkdev->dinfo) {
         Error *local_err = NULL;
-        BlockBackend *blk;

hw/block/xen_disk.c static int blk_connect(struct XenDevice *xendev) · line 904

Quoted 10 lines
hw/block/xen_disk.c +2 -7
-        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>")) {

xen_disk's usage of the string "<unset>" to mark configurations where no driver is specific is quite ugly. I think it's possible for users to pass this string as the driver name, and we would end up probing the driver instead of returning an error.

Which was actually how any invalid driver name was handled before this
patch: bdrv_find_whitelisted_format() would return NULL, and instead of erroring out, NULL would be passed to bdrv_open(), which probed the driver then.

This patch improves the situation: Now any value that is not "<unset>" is passed as the "driver" option, so invalid drivers will produce an error now. Should be mentioned in the commit message.

However, if the user passes "<unset>", that still means probing. Ugly. Not sure why blkdev->fileproto == NULL wasn't used to specify that no driver was given. We could change that in a patch before this one if we wanted to clean it up. Or we could just feel reassured that xen_disk is horrible code, that this patch already fixes most of it and leave it alone.

Quoted 16 lines
hw/block/xen_disk.c +8 -5
+            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",

hw/block/xen_disk.c static int blk_connect(struct XenDevice *xendev) · line 922

Quoted 10 lines
hw/block/xen_disk.c +0 -3
                           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");

Kevin

#14 lore raw

Re: [Qemu-devel] [PATCH v3 05/14] qemu-img: Use blk_new_open() in img_open()

In reply to Max Reitz

Am 26.01.2015 um 16:00 hat Max Reitz geschrieben:

Quoted 22 lines
---
 qemu-img.c | 20 ++++++--------------
 1 file changed, 6 insertions(+), 14 deletions(-)
qemu-img.c +1 -5
index 4e9a7f5..be1953d 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;
 

qemu-img.c static BlockBackend *img_open(const char *id, const char *filename, · line 300

Quoted 10 lines
qemu-img.c +2 -7
     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)));

I'm only noticing it here, though you did the same in the previous patches. This can be written shorter as:

    qdict_put(options, "driver", qstring_from_str(fmt));
Quoted 6 lines
qemu-img.c +2 -2
     }
 
-    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) {

qemu-img.c static BlockBackend *img_open(const char *id, const char *filename, · line 314

Quoted 10 lines
qemu-img.c +1 -0
         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) {

Kevin

#19 lore raw

Re: [Qemu-devel] [PATCH v3 06/14] qemu-img: Use blk_new_open() in img_rebase()

In reply to Max Reitz

Am 26.01.2015 um 16:00 hat Max Reitz geschrieben:

Quoted 32 lines
---
 qemu-img.c | 57 ++++++++++++++++++++++++---------------------------------
 1 file changed, 24 insertions(+), 33 deletions(-)
qemu-img.c +0 -12
index be1953d..0b23c87 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;
-        }

qemu-img.c static int img_rebase(int argc, char **argv) · line 2547

Quoted 10 lines
qemu-img.c +0 -10
-    }
-
-    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;
-        }
-    }

You're removing the validity check of the new backing file format:

[master] $ ./qemu-img rebase -u -b /tmp/backing.qcow2 -F foobar /tmp/test.qcow2
qemu-img: Invalid format name: 'foobar'
[master] $

[growable-v3] $ ./qemu-img rebase -u -b /tmp/backing.qcow2 -F foobar /tmp/test.qcow2 [growable-v3] $

Kevin

#28 lore raw

Re: [Qemu-devel] [PATCH v3 09/14] qemu-io: Use blk_new_open() in openfile()

In reply to Max Reitz

Am 26.01.2015 um 16:00 hat Max Reitz geschrieben:

Quoted 10 lines
---
 qemu-io.c | 31 ++++++++++++-------------------
 1 file changed, 12 insertions(+), 19 deletions(-)
qemu-io.c +0 -0
index 91a445a..81f8f64 100644

qemu-io.c static ReadLineState *readline_state; · line 44

Quoted 7 lines
qemu-io.c +0 -1
@@ -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;
 }

This doesn't seem to be correct at this point in the series. qemuio_bs is still passed to qemuio_command(), which checks whether non-global commands are allowed to run using init_check_command().

At the end of the series, please remove qemuio_bs altogether. This version of the series leaves it around without any reader.

Kevin

#35 lore raw

Re: [Qemu-devel] [PATCH v3 10/14] qemu-io: Remove "growable" option

In reply to Max Reitz

Am 27.01.2015 um 18:11 hat Max Reitz geschrieben:

Quoted 28 lines · Eric Blake
Quoted 27 lines · Max Reitz
Quoted 26 lines · Eric Blake
Quoted 12 lines · Max Reitz
Quoted 11 lines

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.

Quoted 5 lines
Quoted 2 lines

---

Do we want to ever reuse the test number that you are deleting?

Good question, I think I have talked about that with Kevin before. It would not hurt too much if we were to accidentally reuse the test case number, most certainly not here in upstream.

However, for all downstream versions of qemu, this might make adding the new test 16 difficult; but certainly not impossible (if someone is affected by this issue, he/she can just use 999 or something). So we may want to keep in mind not to reuse number 16, but if someone does, so be it.

Quoted 5 lines
Quoted 3 lines

Is it worth a placeholder file that has a comment mentioning that the test number is intentionally reserved (and if someone attempts to run, always passes)?

Seems good to me. It's a minor effort now and may avert some hassle later.

How about just keeping a comment line in group?

Kevin

author
#11 lore raw

Re: [Qemu-devel] [PATCH v3 04/14] block/xen: Use blk_new_open() in blk_connect()

In reply to Kevin Wolf
Quoted 48 lines · Kevin Wolf

Am 26.01.2015 um 16:00 hat Max Reitz geschrieben:

Quoted 23 lines
---
  hw/block/xen_disk.c | 28 ++++++++++++----------------
  1 file changed, 12 insertions(+), 16 deletions(-)
hw/block/xen_disk.c +2 -1
index 21842a0..1b0257c 100644
@@ -40,6 +40,8 @@
  #include "xen_blkif.h"
  #include "sysemu/blockdev.h"
  #include "sysemu/block-backend.h"
+#include "qapi/qmp/qdict.h"
+#include "qapi/qmp/qstring.h"
 
  /* ------------------------------------------------------------- */
 
@@ -897,30 +899,24 @@ static int blk_connect(struct XenDevice *xendev)
      blkdev->dinfo = drive_get(IF_XEN, 0, index);
      if (!blkdev->dinfo) {
          Error *local_err = NULL;
-        BlockBackend *blk;

hw/block/xen_disk.c static int blk_connect(struct XenDevice *xendev) · line 904

Quoted 10 lines
hw/block/xen_disk.c +2 -7
-        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>")) {

xen_disk's usage of the string "<unset>" to mark configurations where no driver is specific is quite ugly. I think it's possible for users to pass this string as the driver name, and we would end up probing the driver instead of returning an error.

Which was actually how any invalid driver name was handled before this
patch: bdrv_find_whitelisted_format() would return NULL, and instead of erroring out, NULL would be passed to bdrv_open(), which probed the driver then.

This patch improves the situation: Now any value that is not "<unset>" is passed as the "driver" option, so invalid drivers will produce an error now. Should be mentioned in the commit message.

Quoted 6 lines

However, if the user passes "<unset>", that still means probing. Ugly. Not sure why blkdev->fileproto == NULL wasn't used to specify that no driver was given. We could change that in a patch before this one if we wanted to clean it up. Or we could just feel reassured that xen_disk is horrible code, that this patch already fixes most of it and leave it alone.

I was glad enough that I made it out alive after digging just a bit into the code. I'm fully in favor of leaving this as-is and adding a note to the commit message.

Thank you for reviewing!

Max

Quoted 27 lines
Quoted 16 lines
hw/block/xen_disk.c +8 -5
+            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",

hw/block/xen_disk.c static int blk_connect(struct XenDevice *xendev) · line 922

Quoted 10 lines
hw/block/xen_disk.c +0 -3
                            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");

Kevin

author
#15 lore raw

Re: [Qemu-devel] [PATCH v3 05/14] qemu-img: Use blk_new_open() in img_open()

In reply to Kevin Wolf
Quoted 36 lines · Kevin Wolf

Am 26.01.2015 um 16:00 hat Max Reitz geschrieben:

Quoted 22 lines
---
  qemu-img.c | 20 ++++++--------------
  1 file changed, 6 insertions(+), 14 deletions(-)
qemu-img.c +1 -5
index 4e9a7f5..be1953d 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;
 

qemu-img.c static BlockBackend *img_open(const char *id, const char *filename, · line 300

Quoted 10 lines
qemu-img.c +2 -7
      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)));

I'm only noticing it here, though you did the same in the previous patches. This can be written shorter as:

Quoted 1 line
     qdict_put(options, "driver", qstring_from_str(fmt));

Indeed, will do.

Max

Quoted 17 lines
Quoted 6 lines
qemu-img.c +2 -2
      }
 
-    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) {

qemu-img.c static BlockBackend *img_open(const char *id, const char *filename, · line 314

Quoted 10 lines
qemu-img.c +1 -0
          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) {

Kevin

#43 lore raw

Re: [Qemu-devel] [PATCH v3 13/14] block: Remove "growable" from BDS

In reply to Max Reitz

Am 26.01.2015 um 16:00 hat Max Reitz geschrieben:

Quoted 46 lines

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 +0 -5
index d45e4dd..356a857 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;
@@ -2645,25 +2643,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))

block.c exit: · line 2663

Quoted 10 lines
block.c +1 -8
+    if (!bdrv_is_inserted(bs)) {
         return -ENOMEDIUM;
-
-    if (bs->growable)
-        return 0;
-
-    len = bdrv_getlength(bs);
-
-    if (offset < 0)
-        return -EIO;

Wouldn't it be better to keep this one, even though bs->growable used to disable it?

Kevin

author
#20 lore raw

Re: [Qemu-devel] [PATCH v3 06/14] qemu-img: Use blk_new_open() in img_rebase()

In reply to Kevin Wolf
Quoted 45 lines · Kevin Wolf

Am 26.01.2015 um 16:00 hat Max Reitz geschrieben:

Quoted 32 lines
---
  qemu-img.c | 57 ++++++++++++++++++++++++---------------------------------
  1 file changed, 24 insertions(+), 33 deletions(-)
qemu-img.c +0 -12
index be1953d..0b23c87 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;
-        }

qemu-img.c static int img_rebase(int argc, char **argv) · line 2547

Quoted 10 lines
qemu-img.c +0 -10
-    }
-
-    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;
-        }
-    }

You're removing the validity check of the new backing file format:

Quoted 6 lines

[master] $ ./qemu-img rebase -u -b /tmp/backing.qcow2 -F foobar /tmp/test.qcow2
qemu-img: Invalid format name: 'foobar'
[master] $

[growable-v3] $ ./qemu-img rebase -u -b /tmp/backing.qcow2 -F foobar /tmp/test.qcow2 [growable-v3] $

Oops, right, I missed that this changes the unsafe path. Thanks for catching it! I'll fix it in v4.

Max

Reviewed-by on the v3 series
#48 lore raw
In reply to Max Reitz

Am 26.01.2015 um 16:00 hat Max Reitz geschrieben:

Quoted 21 lines

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.

Patches that I didn't comment on (all except 4, 5, 6, 9, 10, 13) are, assuming that you address Eric's comments, if any:

author
#29 lore raw

Re: [Qemu-devel] [PATCH v3 09/14] qemu-io: Use blk_new_open() in openfile()

In reply to Kevin Wolf
Quoted 6 lines · Kevin Wolf

Am 26.01.2015 um 16:00 hat Max Reitz geschrieben:

Quoted 5 lines
---
  qemu-io.c | 31 ++++++++++++-------------------
  1 file changed, 12 insertions(+), 19 deletions(-)
Quoted 15 lines

qemu-io.c static ReadLineState *readline_state; · line 44

Quoted 12 lines
qemu-io.c +0 -1
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;
  }

This doesn't seem to be correct at this point in the series. qemuio_bs is still passed to qemuio_command(), which checks whether non-global commands are allowed to run using init_check_command().

$ ./qemu-io -c close -c 'read 0 64k' test.qcow2
read failed: No medium found

Yep, will fix. Thanks for catching it!

Quoted 2 lines

At the end of the series, please remove qemuio_bs altogether. This version of the series leaves it around without any reader.

Okay, will do.

Max

author
#36 lore raw

Re: [Qemu-devel] [PATCH v3 10/14] qemu-io: Remove "growable" option

In reply to Kevin Wolf
Quoted 29 lines · Kevin Wolf

Am 27.01.2015 um 18:11 hat Max Reitz geschrieben:

Quoted 28 lines · Eric Blake
Quoted 27 lines · Max Reitz
Quoted 26 lines · Eric Blake
Quoted 12 lines · Max Reitz
Quoted 11 lines

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.

Quoted 5 lines
Quoted 2 lines

---

Do we want to ever reuse the test number that you are deleting?

Good question, I think I have talked about that with Kevin before. It would not hurt too much if we were to accidentally reuse the test case number, most certainly not here in upstream.

However, for all downstream versions of qemu, this might make adding the new test 16 difficult; but certainly not impossible (if someone is affected by this issue, he/she can just use 999 or something). So we may want to keep in mind not to reuse number 16, but if someone does, so be it.

Quoted 5 lines
Quoted 4 lines
Quoted 3 lines

Is it worth a placeholder file that has a comment mentioning that the test number is intentionally reserved (and if someone attempts to run, always passes)?

Seems good to me. It's a minor effort now and may avert some hassle later.

How about just keeping a comment line in group?

Oh, that would be too simple.

Or maybe just simple enough for me; yes, that seems nicer indeed.

Max

author
#44 lore raw

Re: [Qemu-devel] [PATCH v3 13/14] block: Remove "growable" from BDS

In reply to Kevin Wolf
Quoted 46 lines · Kevin Wolf

Am 26.01.2015 um 16:00 hat Max Reitz geschrieben:

Quoted 45 lines

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 +0 -4
index d45e4dd..356a857 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;
@@ -2645,25 +2643,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;
      }
 
Quoted 13 lines

block.c exit: · line 2663

Quoted 11 lines
block.c +1 -9
-    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;

Wouldn't it be better to keep this one, even though bs->growable used to disable it?

Indeed, there shouldn't be any harm in keeping it.

Max