Skip to contentSkip to conversation

block: logical block provisioning enhancements

PATCH Superseded Peter Lieven 3 participants 39 messages Sep 13, 2013 – Sep 16, 2013 Block layer

v1 of 7 Superseded Latest is v7 →

lore
Share & tools
lore
#0 lore raw

cover block: logical block provisioning enhancements

this patch adds the ability for targets to stay sparse during block migration (if the zero_blocks capability is set) and qemu-img convert even if the target does not have has_zero_init = 1.

the series was especially developed for iSCSI, but it should also work with other drivers with little or no adjustments. these adjustments should be limited to providing block provisioning information through get_block_info and/or honouring BDRV_REQ_MAY_UNMAP on writing zeroes.

the last 4 patches are fixes/enhancements for the get_block_status API discovered during development of this series.

Peter Lieven (12):
block: make BdrvRequestFlags public
block: add flags to bdrv_*_write_zeroes
block: introduce BDRV_REQ_MAY_UNMAP in bdrv_co_write_zeroes
iscsi: add .bdrv_co_write_zeroes
block: add logical block provisioning information to BlockDriverInfo
iscsi: add .bdrv_get_info
block: introduce bdrv_zeroize
qemu-img: conditionally zero out target on convert block/get_block_status: set *pnum = 0 on error block/get_block_status: avoid segfault if there is no backing_hd block/get_block_status: avoid redundant callouts on raw devices block/get_block_status: fix BDRV_BLOCK_ZERO for unallocated blocks

 block-migration.c         |    3 +-
 block.c                   |   86 ++++++++++++++++++++++++++++++--------
 block/backup.c            |    3 +-
 block/iscsi.c             |  102 +++++++++++++++++++++++++++++++++++++++++++++
 block/qcow2.c             |    2 +-
 block/qed.c               |    3 +-
 block/raw_bsd.c           |    5 ++-
 block/vmdk.c              |    3 +-
 include/block/block.h     |   20 ++++++++-
 include/block/block_int.h |    2 +-
 qemu-img.c                |   22 +++++++---
 qemu-io-cmds.c            |    2 +-
 12 files changed, 218 insertions(+), 35 deletions(-)
Signature · 1 line
1.7.9.5
#1 lore raw

PATCH 1/12 block: make BdrvRequestFlags public

---
 block.c               |    5 -----
 include/block/block.h |    5 +++++
 2 files changed, 5 insertions(+), 5 deletions(-)
block.c +0 -5
index a325efc..878f365 100644
@@ -51,11 +51,6 @@
 
 #define NOT_DONE 0x7fffffff /* used while emulated sync operation in progress */
 
-typedef enum {
-    BDRV_REQ_COPY_ON_READ = 0x1,
-    BDRV_REQ_ZERO_WRITE   = 0x2,
-} BdrvRequestFlags;
-
 static void bdrv_dev_change_media_cb(BlockDriverState *bs, bool load);
 static BlockDriverAIOCB *bdrv_aio_readv_em(BlockDriverState *bs,
         int64_t sector_num, QEMUIOVector *qiov, int nb_sectors,
include/block/block.h +5 -0
index 728ec1a..3249261 100644
@@ -62,6 +62,11 @@ typedef struct BlockDevOps {
     void (*resize_cb)(void *opaque);
 } BlockDevOps;
 
+typedef enum {
+    BDRV_REQ_COPY_ON_READ = 0x1,
+    BDRV_REQ_ZERO_WRITE   = 0x2,
+} BdrvRequestFlags;
+
 #define BDRV_O_RDWR        0x0002
 #define BDRV_O_SNAPSHOT    0x0008 /* open the file read only and save writes in a snapshot */
 #define BDRV_O_NOCACHE     0x0020 /* do not use the host page cache */
Signature · 1 line
1.7.9.5
Reviewed-by on v1 1/12
#2 lore raw

Re: [Qemu-devel] [PATCH 01/12] block: make BdrvRequestFlags public

Quoted 5 lines · Peter Lieven
---
 block.c               |    5 -----
 include/block/block.h |    5 +++++
 2 files changed, 5 insertions(+), 5 deletions(-)
Signature · 2 lines
Eric Blake   eblake redhat com    +1-919-301-3266
Libvirt virtualization library http://libvirt.org
#3 lore raw

PATCH 2/12 block: add flags to bdrv_*_write_zeroes

In reply to Peter Lieven
---
 block-migration.c         |    2 +-
 block.c                   |   20 +++++++++++---------
 block/backup.c            |    3 ++-
 block/qcow2.c             |    2 +-
 block/qed.c               |    3 ++-
 block/raw_bsd.c           |    5 +++--
 block/vmdk.c              |    3 ++-
 include/block/block.h     |    4 ++--
 include/block/block_int.h |    2 +-
 qemu-io-cmds.c            |    2 +-
 10 files changed, 26 insertions(+), 20 deletions(-)
block-migration.c +1 -1
index daf9ec1..713a8e3 100644
@@ -780,7 +780,7 @@ static int block_load(QEMUFile *f, void *opaque, int version_id)
             }
 
             if (flags & BLK_MIG_FLAG_ZERO_BLOCK) {
-                ret = bdrv_write_zeroes(bs, addr, nr_sectors);
+                ret = bdrv_write_zeroes(bs, addr, nr_sectors, 0);
             } else {
                 buf = g_malloc(BLOCK_SIZE);
                 qemu_get_buffer(f, buf, BLOCK_SIZE);
block.c +11 -9
index 878f365..e5918a7 100644
@@ -79,7 +79,7 @@ static BlockDriverAIOCB *bdrv_co_aio_rw_vector(BlockDriverState *bs,
                                                bool is_write);
 static void coroutine_fn bdrv_co_do_rw(void *opaque);
 static int coroutine_fn bdrv_co_do_write_zeroes(BlockDriverState *bs,
-    int64_t sector_num, int nb_sectors);
+    int64_t sector_num, int nb_sectors, BdrvRequestFlags flags);
 
 static QTAILQ_HEAD(, BlockDriverState) bdrv_states =
     QTAILQ_HEAD_INITIALIZER(bdrv_states);
@@ -2335,10 +2335,11 @@ int bdrv_writev(BlockDriverState *bs, int64_t sector_num, QEMUIOVector *qiov)
     return bdrv_rwv_co(bs, sector_num, qiov, true, 0);
 }
 
-int bdrv_write_zeroes(BlockDriverState *bs, int64_t sector_num, int nb_sectors)
+int bdrv_write_zeroes(BlockDriverState *bs, int64_t sector_num,
+                      int nb_sectors, BdrvRequestFlags flags)
 {
     return bdrv_rw_co(bs, sector_num, NULL, nb_sectors, true,
-                      BDRV_REQ_ZERO_WRITE);
+                      BDRV_REQ_ZERO_WRITE | flags);
 }
 
 int bdrv_pread(BlockDriverState *bs, int64_t offset,
@@ -2520,7 +2521,7 @@ static int coroutine_fn bdrv_co_do_copy_on_readv(BlockDriverState *bs,
     if (drv->bdrv_co_write_zeroes &&
         buffer_is_zero(bounce_buffer, iov.iov_len)) {
         ret = bdrv_co_do_write_zeroes(bs, cluster_sector_num,
-                                      cluster_nb_sectors);
+                                      cluster_nb_sectors, 0);
     } else {
         /* This does not change the data on the disk, it is not necessary
          * to flush even in cache=writethrough mode.
@@ -2654,7 +2655,7 @@ int coroutine_fn bdrv_co_copy_on_readv(BlockDriverState *bs,
 }
 
 static int coroutine_fn bdrv_co_do_write_zeroes(BlockDriverState *bs,
-    int64_t sector_num, int nb_sectors)
+    int64_t sector_num, int nb_sectors, BdrvRequestFlags flags)
 {
     BlockDriver *drv = bs->drv;
     QEMUIOVector qiov;
@@ -2666,7 +2667,7 @@ static int coroutine_fn bdrv_co_do_write_zeroes(BlockDriverState *bs,
 
     /* First try the efficient write zeroes operation */
     if (drv->bdrv_co_write_zeroes) {
-        ret = drv->bdrv_co_write_zeroes(bs, sector_num, nb_sectors);
+        ret = drv->bdrv_co_write_zeroes(bs, sector_num, nb_sectors, flags);
         if (ret != -ENOTSUP) {
             return ret;
         }
@@ -2721,7 +2722,7 @@ static int coroutine_fn bdrv_co_do_writev(BlockDriverState *bs,
     if (ret < 0) {
         /* Do nothing, write notifier decided to fail this request */
     } else if (flags & BDRV_REQ_ZERO_WRITE) {
-        ret = bdrv_co_do_write_zeroes(bs, sector_num, nb_sectors);
+        ret = bdrv_co_do_write_zeroes(bs, sector_num, nb_sectors, flags);
     } else {
         ret = drv->bdrv_co_writev(bs, sector_num, nb_sectors, qiov);
     }
@@ -2755,12 +2756,13 @@ int coroutine_fn bdrv_co_writev(BlockDriverState *bs, int64_t sector_num,
 }
 
 int coroutine_fn bdrv_co_write_zeroes(BlockDriverState *bs,
-                                      int64_t sector_num, int nb_sectors)
+                                      int64_t sector_num, int nb_sectors,
+                                      BdrvRequestFlags flags)
 {
     trace_bdrv_co_write_zeroes(bs, sector_num, nb_sectors);
 
     return bdrv_co_do_writev(bs, sector_num, nb_sectors, NULL,
-                             BDRV_REQ_ZERO_WRITE);
+                             BDRV_REQ_ZERO_WRITE | flags);
 }
 
 /**
block/backup.c +2 -1
index 04c4b5c..5f6a642 100644
@@ -138,7 +138,8 @@ static int coroutine_fn backup_do_cow(BlockDriverState *bs,
 
         if (buffer_is_zero(iov.iov_base, iov.iov_len)) {
             ret = bdrv_co_write_zeroes(job->target,
-                                       start * BACKUP_SECTORS_PER_CLUSTER, n);
+                                       start * BACKUP_SECTORS_PER_CLUSTER,
+                                       n, 0);
         } else {
             ret = bdrv_co_writev(job->target,
                                  start * BACKUP_SECTORS_PER_CLUSTER, n,
block/qcow2.c +1 -1
index 578792f..2cb59a2 100644
@@ -1555,7 +1555,7 @@ static int qcow2_make_empty(BlockDriverState *bs)
 }
 
 static coroutine_fn int qcow2_co_write_zeroes(BlockDriverState *bs,
-    int64_t sector_num, int nb_sectors)
+    int64_t sector_num, int nb_sectors, BdrvRequestFlags flags)
 {
     int ret;
     BDRVQcowState *s = bs->opaque;
block/qed.c +2 -1
index 49b3a37..d6e42f3 100644
@@ -1389,7 +1389,8 @@ static void coroutine_fn qed_co_write_zeroes_cb(void *opaque, int ret)
 
 static int coroutine_fn bdrv_qed_co_write_zeroes(BlockDriverState *bs,
                                                  int64_t sector_num,
-                                                 int nb_sectors)
+                                                 int nb_sectors,
+                                                 BdrvRequestFlags flags)
 {
     BlockDriverAIOCB *blockacb;
     BDRVQEDState *s = bs->opaque;
block/raw_bsd.c +3 -2
index a9060ca..bd4811b 100644
@@ -66,9 +66,10 @@ static int64_t coroutine_fn raw_co_get_block_status(BlockDriverState *bs,
 }
 
 static int coroutine_fn raw_co_write_zeroes(BlockDriverState *bs,
-                                            int64_t sector_num, int nb_sectors)
+                                            int64_t sector_num, int nb_sectors,
+                                            BdrvRequestFlags flags)
 {
-    return bdrv_co_write_zeroes(bs->file, sector_num, nb_sectors);
+    return bdrv_co_write_zeroes(bs->file, sector_num, nb_sectors, flags);
 }
 
 static int coroutine_fn raw_co_discard(BlockDriverState *bs,
block/vmdk.c +2 -1
index fb5b529..914a9b7 100644
@@ -1386,7 +1386,8 @@ static coroutine_fn int vmdk_co_write(BlockDriverState *bs, int64_t sector_num,
 
 static int coroutine_fn vmdk_co_write_zeroes(BlockDriverState *bs,
                                              int64_t sector_num,
-                                             int nb_sectors)
+                                             int nb_sectors,
+                                             BdrvRequestFlags flags)
 {
     int ret;
     BDRVVmdkState *s = bs->opaque;
include/block/block.h +2 -2
index 3249261..ddb6870 100644
@@ -187,7 +187,7 @@ int bdrv_read_unthrottled(BlockDriverState *bs, int64_t sector_num,
 int bdrv_write(BlockDriverState *bs, int64_t sector_num,
                const uint8_t *buf, int nb_sectors);
 int bdrv_write_zeroes(BlockDriverState *bs, int64_t sector_num,
-               int nb_sectors);
+               int nb_sectors, BdrvRequestFlags flags);
 int bdrv_writev(BlockDriverState *bs, int64_t sector_num, QEMUIOVector *qiov);
 int bdrv_pread(BlockDriverState *bs, int64_t offset,
                void *buf, int count);
@@ -209,7 +209,7 @@ int coroutine_fn bdrv_co_writev(BlockDriverState *bs, int64_t sector_num,
  * because it may allocate memory for the entire region.
  */
 int coroutine_fn bdrv_co_write_zeroes(BlockDriverState *bs, int64_t sector_num,
-    int nb_sectors);
+    int nb_sectors, BdrvRequestFlags flags);
 BlockDriverState *bdrv_find_backing_image(BlockDriverState *bs,
     const char *backing_file);
 int bdrv_get_backing_file_depth(BlockDriverState *bs);
include/block/block_int.h +1 -1
index 7c35198..26bad36 100644
@@ -115,7 +115,7 @@ struct BlockDriver {
      * instead.
      */
     int coroutine_fn (*bdrv_co_write_zeroes)(BlockDriverState *bs,
-        int64_t sector_num, int nb_sectors);
+        int64_t sector_num, int nb_sectors, BdrvRequestFlags flags);
     int coroutine_fn (*bdrv_co_discard)(BlockDriverState *bs,
         int64_t sector_num, int nb_sectors);
     int64_t coroutine_fn (*bdrv_co_get_block_status)(BlockDriverState *bs,
qemu-io-cmds.c +1 -1
index 8565d49..66c0b9e 100644
@@ -441,7 +441,7 @@ static void coroutine_fn co_write_zeroes_entry(void *opaque)
     CoWriteZeroes *data = opaque;
 
     data->ret = bdrv_co_write_zeroes(data->bs, data->offset / BDRV_SECTOR_SIZE,
-                                     data->count / BDRV_SECTOR_SIZE);
+                                     data->count / BDRV_SECTOR_SIZE, 0);
     data->done = true;
     if (data->ret < 0) {
         *data->total = data->ret;
Signature · 1 line
1.7.9.5
Reviewed-by on v1 2/12
#4 lore raw

Re: [Qemu-devel] [PATCH 02/12] block: add flags to bdrv_*_write_zeroes

Quoted 13 lines · Peter Lieven
---
 block-migration.c         |    2 +-
 block.c                   |   20 +++++++++++---------
 block/backup.c            |    3 ++-
 block/qcow2.c             |    2 +-
 block/qed.c               |    3 ++-
 block/raw_bsd.c           |    5 +++--
 block/vmdk.c              |    3 ++-
 include/block/block.h     |    4 ++--
 include/block/block_int.h |    2 +-
 qemu-io-cmds.c            |    2 +-
 10 files changed, 26 insertions(+), 20 deletions(-)

block.c int bdrv_writev(BlockDriverState *bs, int64_t sector_num, QEMUIOVector *qiov) · line 2339

Quoted 4 lines
block.c +2 -1
-int bdrv_write_zeroes(BlockDriverState *bs, int64_t sector_num, int nb_sectors)
+int bdrv_write_zeroes(BlockDriverState *bs, int64_t sector_num,
+                      int nb_sectors, BdrvRequestFlags flags)

Technically, you are allowing any bitwise combination of BdrvRequestFlags, including combinations that are NOT enumerated values within BdrvRequestFlags. There are some type-strict languages where you'd fail to compile when passing in a non-enum value. But C allows this (ab)use of enum types in place of an integer type, and I find it better documented this way, so I'm fine with leaving it as-is. That, and you're just copying existing style already in qemu :)

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

PATCH 3/12 block: introduce BDRV_REQ_MAY_UNMAP in bdrv_co_write_zeroes

In reply to Peter Lieven

the BDRV_REQ_MAY_UNMAP flag is used to indicate that block driver is allowed to optimze a write zeroes request by unmapping (discarding) blocks if it is guaranteed that the result will read back as zeroes.

the flag is only passed to the driver if the block device is opened with BDRV_O_UNMAP.

---
 block-migration.c     |    3 ++-
 block.c               |    4 ++++
 block/backup.c        |    2 +-
 include/block/block.h |    1 +
 4 files changed, 8 insertions(+), 2 deletions(-)
block-migration.c +2 -1
index 713a8e3..fc4ef93 100644
@@ -780,7 +780,8 @@ static int block_load(QEMUFile *f, void *opaque, int version_id)
             }
 
             if (flags & BLK_MIG_FLAG_ZERO_BLOCK) {
-                ret = bdrv_write_zeroes(bs, addr, nr_sectors, 0);
+                ret = bdrv_write_zeroes(bs, addr, nr_sectors,
+                                        BDRV_REQ_MAY_UNMAP);
             } else {
                 buf = g_malloc(BLOCK_SIZE);
                 qemu_get_buffer(f, buf, BLOCK_SIZE);
block.c +4 -0
index e5918a7..6f498fc 100644
@@ -2761,6 +2761,10 @@ int coroutine_fn bdrv_co_write_zeroes(BlockDriverState *bs,
 {
     trace_bdrv_co_write_zeroes(bs, sector_num, nb_sectors);
 
+    if (!(bs->open_flags & BDRV_O_UNMAP)) {
+        flags &= ~BDRV_REQ_MAY_UNMAP;
+    }
+
     return bdrv_co_do_writev(bs, sector_num, nb_sectors, NULL,
                              BDRV_REQ_ZERO_WRITE | flags);
 }
block/backup.c +1 -1
index 5f6a642..1ab080d 100644
@@ -139,7 +139,7 @@ static int coroutine_fn backup_do_cow(BlockDriverState *bs,
         if (buffer_is_zero(iov.iov_base, iov.iov_len)) {
             ret = bdrv_co_write_zeroes(job->target,
                                        start * BACKUP_SECTORS_PER_CLUSTER,
-                                       n, 0);
+                                       n, BDRV_REQ_MAY_UNMAP);
         } else {
             ret = bdrv_co_writev(job->target,
                                  start * BACKUP_SECTORS_PER_CLUSTER, n,
include/block/block.h +1 -0
index ddb6870..599de7d 100644
@@ -65,6 +65,7 @@ typedef struct BlockDevOps {
 typedef enum {
     BDRV_REQ_COPY_ON_READ = 0x1,
     BDRV_REQ_ZERO_WRITE   = 0x2,
+    BDRV_REQ_MAY_UNMAP    = 0x4,
 } BdrvRequestFlags;
 
 #define BDRV_O_RDWR        0x0002
Signature · 1 line
1.7.9.5
#6 lore raw

Re: [Qemu-devel] [PATCH 03/12] block: introduce BDRV_REQ_MAY_UNMAP in bdrv_co_write_zeroes

Quoted 2 lines · Peter Lieven

the BDRV_REQ_MAY_UNMAP flag is used to indicate that block driver is allowed to optimze a write zeroes request by unmapping (discarding)

s/optimze/optimize/

Quoted 11 lines

blocks if it is guaranteed that the result will read back as zeroes.

the flag is only passed to the driver if the block device is opened with BDRV_O_UNMAP.

---

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

include/block/block.h typedef struct BlockDevOps { · line 68

Quoted 4 lines
include/block/block.h +1 -0
@@ -65,6 +65,7 @@ typedef struct BlockDevOps {
 typedef enum {
     BDRV_REQ_COPY_ON_READ = 0x1,
     BDRV_REQ_ZERO_WRITE   = 0x2,
+    BDRV_REQ_MAY_UNMAP    = 0x4,

Now that this enum is public, it would be useful to have inline documentation, rather than making people dig up the git history of when it was introduced to learn its semantics.

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

PATCH 4/12 iscsi: add .bdrv_co_write_zeroes

In reply to Peter Lieven
---
 block/iscsi.c |   78 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 78 insertions(+)
block/iscsi.c +78 -0
index 68f99d3..3d7961d 100644
@@ -56,6 +56,7 @@ typedef struct IscsiLun {
     uint8_t lbprz;
     struct scsi_inquiry_logical_block_provisioning lbp;
     struct scsi_inquiry_block_limits bl;
+    unsigned char *zeroblock;
 } IscsiLun;
 
 typedef struct IscsiTask {
@@ -88,6 +89,7 @@ typedef struct IscsiAIOCB {
 #define MAX_NOP_FAILURES 3
 #define ISCSI_CMD_RETRIES 5
 #define ISCSI_MAX_UNMAP 131072
+#define ISCSI_MAX_WRITESAME 131072
 
 static void
 iscsi_bh_cb(void *p)
@@ -972,6 +974,80 @@ retry:
     return 0;
 }
 
+static int
+coroutine_fn iscsi_co_write_zeroes(BlockDriverState *bs, int64_t sector_num,
+                                   int nb_sectors, BdrvRequestFlags flags)
+{
+    IscsiLun *iscsilun = bs->opaque;
+    struct IscsiTask iTask;
+    uint64_t lba;
+    uint32_t nb_blocks, max_ws_len;
+
+    if (!is_request_lun_aligned(sector_num, nb_sectors, iscsilun)) {
+        return -EINVAL;
+    }
+
+    if (!iscsilun->lbp.lbpws) {
+        /* WRITE SAME is not supported by the target */
+        return -ENOTSUP;
+    }
+
+    lba = sector_qemu2lun(sector_num, iscsilun);
+    nb_blocks = sector_qemu2lun(nb_sectors, iscsilun);
+
+    max_ws_len = iscsilun->bl.max_ws_len;
+    if (max_ws_len == 0xffffffff) {
+        max_ws_len = ISCSI_MAX_WRITESAME;
+    }
+
+    if (iscsilun->zeroblock == NULL) {
+        iscsilun->zeroblock = g_malloc(iscsilun->block_size);
+        if (iscsilun->zeroblock == NULL) {
+            return -ENOMEM;
+        }
+        memset(iscsilun->zeroblock, 0x00, iscsilun->block_size);
+    }
+
+    while (nb_blocks > 0) {
+        uint32_t num;
+        iscsi_co_init_iscsitask(iscsilun, &iTask);
+        num = nb_blocks;
+        if (num > max_ws_len) {
+            num = max_ws_len;
+        }
+retry:
+        if (iscsi_writesame16_task(iscsilun->iscsi, iscsilun->lun, lba,
+                                   iscsilun->zeroblock, iscsilun->block_size,
+                                   num, 0, !!(flags & BDRV_REQ_MAY_UNMAP),
+                                   0, 0, iscsi_co_generic_cb, &iTask) == NULL) {
+            return -EIO;
+        }
+
+        while (!iTask.complete) {
+            iscsi_set_events(iscsilun);
+            qemu_coroutine_yield();
+        }
+
+        if (iTask.task != NULL) {
+            scsi_free_scsi_task(iTask.task);
+            iTask.task = NULL;
+        }
+
+        if (iTask.do_retry) {
+            goto retry;
+        }
+
+        if (iTask.status != SCSI_STATUS_GOOD) {
+            return -EIO;
+        }
+
+        lba += num;
+        nb_blocks -= num;
+    }
+
+    return 0;
+}
+
 static int parse_chap(struct iscsi_context *iscsi, const char *target)
 {
     QemuOptsList *list;
@@ -1419,6 +1495,7 @@ static void iscsi_close(BlockDriverState *bs)
     }
     qemu_aio_set_fd_handler(iscsi_get_fd(iscsi), NULL, NULL, NULL);
     iscsi_destroy_context(iscsi);
+    g_free(iscsilun->zeroblock);
     memset(iscsilun, 0, sizeof(IscsiLun));
 }
 
@@ -1524,6 +1601,7 @@ static BlockDriver bdrv_iscsi = {
 
     .bdrv_co_get_block_status = iscsi_co_get_block_status,
     .bdrv_co_discard      = iscsi_co_discard,
+    .bdrv_co_write_zeroes = iscsi_co_write_zeroes,
 
     .bdrv_aio_readv  = iscsi_aio_readv,
     .bdrv_aio_writev = iscsi_aio_writev,
Signature · 1 line
1.7.9.5
#8 lore raw

Re: [Qemu-devel] [PATCH 04/12] iscsi: add .bdrv_co_write_zeroes

block/iscsi.c retry: · line 1006

Quoted 10 lines · Peter Lieven
---
 block/iscsi.c |   78 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 78 insertions(+)
block/iscsi.c +4 -0
+    if (iscsilun->zeroblock == NULL) {
+        iscsilun->zeroblock = g_malloc(iscsilun->block_size);
+        if (iscsilun->zeroblock == NULL) {
+            return -ENOMEM;

g_malloc cannot return NULL (it aborts the program on OOM instead), so this is dead code.

Review

Quoted 2 lines
(quoted diff) +2 -0
+        }
+        memset(iscsilun->zeroblock, 0x00, iscsilun->block_size);

Furthermore, if you had used g_malloc0(), you wouldn't need this memset.

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

PATCH 5/12 block: add logical block provisioning information to BlockDriverInfo

In reply to Peter Lieven
---
 include/block/block.h |    9 +++++++++
 1 file changed, 9 insertions(+)
include/block/block.h +9 -0
index 599de7d..ee17048 100644
@@ -18,6 +18,15 @@ typedef struct BlockDriverInfo {
     /* offset at which the VM state can be saved (0 if not possible) */
     int64_t vm_state_offset;
     bool is_dirty;
+    /* do discarded blocks read back as zeroes? */
+    bool discard_zeroes;
+    /* is write zeroes optimized by a discard/unmap operation?
+     * this requires support for the BDRV_REQ_MAY_UNMAP flag. */
+    bool discard_write_zeroes;
+    /* maximum number of sectors that can be discarded at once */
+    int max_discard;
+    /* maximum number of sectors that can zeroized at once */
+    int max_write_zeroes;
 } BlockDriverInfo;
 
 typedef struct BlockFragInfo {
Signature · 1 line
1.7.9.5
#10 lore raw

Re: [Qemu-devel] [PATCH 05/12] block: add logical block provisioning information to BlockDriverInfo

Il 13/09/2013 12:25, Peter Lieven ha scritto:

include/block/block.h typedef struct BlockDriverInfo { · line 29

Quoted 4 lines
include/block/block.h +4 -0
+    /* maximum number of sectors that can be discarded at once */
+    int max_discard;
+    /* maximum number of sectors that can zeroized at once */
+    int max_write_zeroes;

These should not be needed outside the driver.

If you want to make them private between block.c and block/iscsi.c, you can add them to BlockDriverState.

Paolo

#11 lore raw

Re: [Qemu-devel] [PATCH 05/12] block: add logical block provisioning information to BlockDriverInfo

Quoted 9 lines · Paolo Bonzini

Il 13/09/2013 12:25, Peter Lieven ha scritto:

include/block/block.h typedef struct BlockDriverInfo { · line 29

Quoted 4 lines
include/block/block.h +4 -0
+    /* maximum number of sectors that can be discarded at once */
+    int max_discard;
+    /* maximum number of sectors that can zeroized at once */
+    int max_write_zeroes;

These should not be needed outside the driver.

If you want to make them private between block.c and block/iscsi.c, you can add them to BlockDriverState.

The question is, if the discard_zeroes or discard_write_zeroes is needed outside the driver as well?

I can put the max_* information in the block driver state. I also thought to add alignment and granularity information even if they are currently not yet used.

Peter

#12 lore raw

Re: [Qemu-devel] [PATCH 05/12] block: add logical block provisioning information to BlockDriverInfo

Il 13/09/2013 12:44, Peter Lieven ha scritto:

Quoted 8 lines · Paolo Bonzini
Quoted 7 lines

Il 13/09/2013 12:25, Peter Lieven ha scritto:

include/block/block.h typedef struct BlockDriverInfo { · line 29

Quoted 4 lines
include/block/block.h +4 -0
+    /* maximum number of sectors that can be discarded at once */
+    int max_discard;
+    /* maximum number of sectors that can zeroized at once */
+    int max_write_zeroes;

These should not be needed outside the driver.

Quoted 8 lines
Quoted 2 lines

If you want to make them private between block.c and block/iscsi.c, you can add them to BlockDriverState.

The question is, if the discard_zeroes or discard_write_zeroes is needed outside the driver as well?

I can put the max_* information in the block driver state. I also thought to add alignment and granularity information even if they are currently not yet used.

Yeah, in fact bdrv_write_zeroes and bdrv_discard can be taught to split requests according to these parameters instead of introducing a new function bdrv_zeroize. You don't need bdrv_zeroize I think; you can simply use bdrv_write_zeroes. This is why I don't like this information in BlockDriverInfo.

On the contrary, discard_write_zeroes is useful to "generic" clients, and your qemu-img patch shows why.

Discard_zeroes is somewhere in the middle. You only use it in bdrv_get_block_status, but it is not something that should be hidden to users of the high-level block.c API. So it is fine to leave it in BlockDriverInfo.

Paolo

#13 lore raw

Re: [Qemu-devel] [PATCH 05/12] block: add logical block provisioning information to BlockDriverInfo

Am 13.09.2013 13:45, schrieb Paolo Bonzini:

Quoted 14 lines

Il 13/09/2013 12:44, Peter Lieven ha scritto:

Quoted 13 lines · Paolo Bonzini
Quoted 9 lines

Il 13/09/2013 12:25, Peter Lieven ha scritto:

include/block/block.h typedef struct BlockDriverInfo { · line 29

Quoted 4 lines
include/block/block.h +4 -0
+    /* maximum number of sectors that can be discarded at once */
+    int max_discard;
+    /* maximum number of sectors that can zeroized at once */
+    int max_write_zeroes;

These should not be needed outside the driver.

If you want to make them private between block.c and block/iscsi.c, you can add them to BlockDriverState.

The question is, if the discard_zeroes or discard_write_zeroes is needed outside the driver as well?

Quoted 8 lines
Quoted 3 lines

I can put the max_* information in the block driver state. I also thought to add alignment and granularity information even if they are currently not yet used.

Yeah, in fact bdrv_write_zeroes and bdrv_discard can be taught to split requests according to these parameters instead of introducing a new function bdrv_zeroize. You don't need bdrv_zeroize I think; you can simply use bdrv_write_zeroes. This is why I don't like this information in BlockDriverInfo.

bdrv_zeroize has one big advantage over a bdrv_write_zeroes over the whole device: it checks the block status before it sends requests. this can be a great performance benefit if a lot of blocks are already unmapped. so i would like to keep it in, but simplifiy it (see below).

Quoted 8 lines

On the contrary, discard_write_zeroes is useful to "generic" clients, and your qemu-img patch shows why.

Discard_zeroes is somewhere in the middle. You only use it in bdrv_get_block_status, but it is not something that should be hidden to users of the high-level block.c API. So it is fine to leave it in BlockDriverInfo.

okay, i will leave them in and put

max_discard
discard_alignment
max_write_zeroes
write_zeroes_alignment

into the BlockDriverState. I would then like to call out to all driver developers to set these values for their drivers to good values.

For now I can factor out the request split logic out of iscsi_co_discard, iscsi_co_write_zeroes and bdrv_sanitize and put them in bdrv_co_discard and bdrv_co_write_zeroes.

I would like to leave the misalignment logic to a later patch.

What would you think are reasonable default values for max_discard and max_write_zeroes?

Peter

#14 lore raw

Re: [Qemu-devel] [PATCH 05/12] block: add logical block provisioning information to BlockDriverInfo

Il 13/09/2013 14:22, Peter Lieven ha scritto:

Quoted 24 lines

Am 13.09.2013 13:45, schrieb Paolo Bonzini:

Quoted 14 lines

Il 13/09/2013 12:44, Peter Lieven ha scritto:

Quoted 13 lines · Paolo Bonzini
Quoted 9 lines

Il 13/09/2013 12:25, Peter Lieven ha scritto:

include/block/block.h typedef struct BlockDriverInfo { · line 29

Quoted 4 lines
include/block/block.h +4 -0
+    /* maximum number of sectors that can be discarded at once */
+    int max_discard;
+    /* maximum number of sectors that can zeroized at once */
+    int max_write_zeroes;

These should not be needed outside the driver.

If you want to make them private between block.c and block/iscsi.c, you can add them to BlockDriverState.

The question is, if the discard_zeroes or discard_write_zeroes is needed outside the driver as well?

Quoted 8 lines
Quoted 3 lines

I can put the max_* information in the block driver state. I also thought to add alignment and granularity information even if they are currently not yet used.

Yeah, in fact bdrv_write_zeroes and bdrv_discard can be taught to split requests according to these parameters instead of introducing a new function bdrv_zeroize. You don't need bdrv_zeroize I think; you can simply use bdrv_write_zeroes. This is why I don't like this information in BlockDriverInfo.

Quoted 4 lines

bdrv_zeroize has one big advantage over a bdrv_write_zeroes over the whole device: it checks the block status before it sends requests. this can be a great performance benefit if a lot of blocks are already unmapped. so i would like to keep it in, but simplifiy it (see below).

Good idea.

Quoted 5 lines

For now I can factor out the request split logic out of iscsi_co_discard, iscsi_co_write_zeroes and bdrv_sanitize and put them in bdrv_co_discard and bdrv_co_write_zeroes.

I would like to leave the misalignment logic to a later patch.

Sure.

Quoted 2 lines

What would you think are reasonable default values for max_discard and max_write_zeroes?

32768 (16 MB) is the highest power-of-two amount that fits in 16 bits assuming 512-byte sectors. But you could also take a look at what the Linux kernel does in drivers/scsi/sd.c.

Paolo

#15 lore raw

Re: [Qemu-devel] [PATCH 05/12] block: add logical block provisioning information to BlockDriverInfo

In reply to Paolo Bonzini
Quoted 23 lines · Paolo Bonzini

Il 13/09/2013 12:44, Peter Lieven ha scritto:

Quoted 8 lines · Paolo Bonzini
Quoted 7 lines

Il 13/09/2013 12:25, Peter Lieven ha scritto:

include/block/block.h typedef struct BlockDriverInfo { · line 29

Quoted 4 lines
include/block/block.h +4 -0
+    /* maximum number of sectors that can be discarded at once */
+    int max_discard;
+    /* maximum number of sectors that can zeroized at once */
+    int max_write_zeroes;

These should not be needed outside the driver.

Quoted 8 lines
Quoted 2 lines

If you want to make them private between block.c and block/iscsi.c, you can add them to BlockDriverState.

The question is, if the discard_zeroes or discard_write_zeroes is needed outside the driver as well?

I can put the max_* information in the block driver state. I also thought to add alignment and granularity information even if they are currently not yet used.

Yeah, in fact bdrv_write_zeroes and bdrv_discard can be taught to split requests according to these parameters instead of introducing a new function bdrv_zeroize. You don't need bdrv_zeroize I think; you can simply use bdrv_write_zeroes. This is why I don't like this information in BlockDriverInfo.

Quoted 7 lines

On the contrary, discard_write_zeroes is useful to "generic" clients, and your qemu-img patch shows why.

Discard_zeroes is somewhere in the middle. You only use it in bdrv_get_block_status, but it is not something that should be hidden to users of the high-level block.c API. So it is fine to leave it in BlockDriverInfo.

Would you also be ok to introduce bdrv_has_discard_zeroes()
and bdrv_has_discard_write_zeroes() as Kevin suggested to avoid the need to add the logic to return 0 if there is a bs->backing_hd everywhere.

This would also make the use of it easier as it avoids the steps necessary to invoke bdrv_get_info().

Peter

#16 lore raw

Re: [Qemu-devel] [PATCH 05/12] block: add logical block provisioning information to BlockDriverInfo

Il 16/09/2013 13:30, Peter Lieven ha scritto:

Quoted 28 lines · Paolo Bonzini
Quoted 27 lines

Il 13/09/2013 12:44, Peter Lieven ha scritto:

Quoted 10 lines · Paolo Bonzini
Quoted 9 lines

Il 13/09/2013 12:25, Peter Lieven ha scritto:

include/block/block.h typedef struct BlockDriverInfo { · line 29

Quoted 4 lines
include/block/block.h +4 -0
+    /* maximum number of sectors that can be discarded at once */
+    int max_discard;
+    /* maximum number of sectors that can zeroized at once */
+    int max_write_zeroes;

These should not be needed outside the driver.

If you want to make them private between block.c and block/iscsi.c, you can add them to BlockDriverState.

Quoted 7 lines

The question is, if the discard_zeroes or discard_write_zeroes is needed outside the driver as well?

I can put the max_* information in the block driver state. I also thought
to add alignment and granularity information even if they are currently not yet used.

Yeah, in fact bdrv_write_zeroes and bdrv_discard can be taught to split requests according to these parameters instead of introducing a new function bdrv_zeroize. You don't need bdrv_zeroize I think; you can simply use bdrv_write_zeroes. This is why I don't like this information in BlockDriverInfo.

On the contrary, discard_write_zeroes is useful to "generic" clients, and your qemu-img patch shows why.

Quoted 8 lines
Quoted 4 lines

Discard_zeroes is somewhere in the middle. You only use it in bdrv_get_block_status, but it is not something that should be hidden to users of the high-level block.c API. So it is fine to leave it in BlockDriverInfo.

Would you also be ok to introduce bdrv_has_discard_zeroes()
and bdrv_has_discard_write_zeroes() as Kevin suggested to avoid the need to add the logic to return 0 if there is a bs->backing_hd everywhere.

If Kevin says it, I agree. :)

Quoted 2 lines

This would also make the use of it easier as it avoids the steps necessary to invoke bdrv_get_info().

Another possibility could be to put the "info" in a member of BlockDriverState, since it is static. Then bdrv_get_info() could be simply

    return &bs->info;

which is faster and removes the need for a BlockDriverInfo temporary.

Paolo

#17 lore raw

PATCH 6/12 iscsi: add .bdrv_get_info

In reply to Peter Lieven
---
 block/iscsi.c |   24 ++++++++++++++++++++++++
 1 file changed, 24 insertions(+)
block/iscsi.c +24 -0
index 3d7961d..438d45e 100644
@@ -1577,6 +1577,29 @@ out:
     return ret;
 }
 
+static int iscsi_get_info(BlockDriverState *bs, BlockDriverInfo *bdi)
+{
+    IscsiLun *iscsilun = bs->opaque;
+    int max_discard = iscsilun->bl.max_unmap;
+    int max_write_zeroes = iscsilun->bl.max_ws_len;
+
+    if (max_discard == 0xffffffff) {
+        max_discard = ISCSI_MAX_UNMAP;
+    }
+    bdi->max_discard = sector_lun2qemu(max_discard, iscsilun);
+
+    if (max_write_zeroes == 0xffffffff) {
+        max_write_zeroes = ISCSI_MAX_WRITESAME;
+    }
+    bdi->max_write_zeroes = sector_lun2qemu(max_write_zeroes, iscsilun);
+
+    bdi->discard_zeroes =  iscsilun->lbprz;
+    bdi->discard_write_zeroes =  (bs->open_flags & BDRV_O_UNMAP) &&
+                                 iscsilun->lbprz && iscsilun->lbp.lbpws;
+
+    return 0;
+}
+
 static QEMUOptionParameter iscsi_create_options[] = {
     {
         .name = BLOCK_OPT_SIZE,
@@ -1597,6 +1620,7 @@ static BlockDriver bdrv_iscsi = {
     .create_options  = iscsi_create_options,
 
     .bdrv_getlength  = iscsi_getlength,
+    .bdrv_get_info   = iscsi_get_info,
     .bdrv_truncate   = iscsi_truncate,
 
     .bdrv_co_get_block_status = iscsi_co_get_block_status,
Signature · 1 line
1.7.9.5
Reviewed-by on v1 6/12
#18 lore raw

Re: [Qemu-devel] [PATCH 06/12] iscsi: add .bdrv_get_info

block/iscsi.c out: · line 1597

Quoted 9 lines · Peter Lieven
---
 block/iscsi.c |   24 ++++++++++++++++++++++++
 1 file changed, 24 insertions(+)
block/iscsi.c +3 -0
+
+    bdi->discard_zeroes =  iscsilun->lbprz;
+    bdi->discard_write_zeroes =  (bs->open_flags & BDRV_O_UNMAP) &&

Why two spaces after = (two instances)?

But that's cosmetic, so you can fix before adding:

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

PATCH 7/12 block: introduce bdrv_zeroize

In reply to Peter Lieven

this patch adds a call to completely zero out a block device. the operation is speed up by checking the block status and only writing zeroes to the device if they currently do not return zeroes. optionally the zero writing can be speed up by setting the flag BDRV_REQ_MAY_UNMAP to emulate the zero write by unmapping if the driver supports it.

---
 block.c               |   45 +++++++++++++++++++++++++++++++++++++++++++++
 include/block/block.h |    1 +
 2 files changed, 46 insertions(+)
block.c +45 -0
index 6f498fc..76a6621 100644
@@ -2342,6 +2342,51 @@ int bdrv_write_zeroes(BlockDriverState *bs, int64_t sector_num,
                       BDRV_REQ_ZERO_WRITE | flags);
 }
 
+int bdrv_zeroize(BlockDriverState *bs, BdrvRequestFlags flags)
+{
+    BlockDriverInfo bdi;
+    int64_t target_size = bdrv_getlength(bs) / BDRV_SECTOR_SIZE;
+    int64_t ret, nb_sectors, sector_num = 0;
+    int n;
+    /* split the write requests into 1MB chunks if the driver
+     * does not return a maximal size via bdi */
+    int max_write_zeroes = 1 << (20 - BDRV_SECTOR_BITS);
+    if (bdrv_get_info(bs, &bdi) == 0 &&
+        bdi.max_write_zeroes > 0) {
+        max_write_zeroes = bdi.max_write_zeroes;
+    }
+    for (;;) {
+        nb_sectors = target_size - sector_num;
+        if (nb_sectors <= 0) {
+            return 0;
+        }
+        if (nb_sectors > INT_MAX) {
+            nb_sectors = INT_MAX;
+        }
+        ret = bdrv_get_block_status(bs, sector_num, nb_sectors, &n);
+        if (ret & BDRV_BLOCK_ZERO) {
+            sector_num += n;
+            continue;
+        }
+        while (n > 0) {
+            nb_sectors = n;
+            if (nb_sectors > max_write_zeroes) {
+                nb_sectors = max_write_zeroes;
+            }
+            ret = bdrv_write_zeroes(bs, sector_num, nb_sectors,
+                                    flags & BDRV_REQ_MAY_UNMAP);
+            if (ret < 0) {
+                error_report("error writing zeroes at sector %" PRId64 ": %s",
+                             sector_num, strerror(-ret));
+                return -EIO;
+            }
+            sector_num += nb_sectors;
+            n -= nb_sectors;
+        }
+    }
+}
+
+
 int bdrv_pread(BlockDriverState *bs, int64_t offset,
                void *buf, int count1)
 {
include/block/block.h +1 -0
index ee17048..71e9fa1 100644
@@ -198,6 +198,7 @@ int bdrv_write(BlockDriverState *bs, int64_t sector_num,
                const uint8_t *buf, int nb_sectors);
 int bdrv_write_zeroes(BlockDriverState *bs, int64_t sector_num,
                int nb_sectors, BdrvRequestFlags flags);
+int bdrv_zeroize(BlockDriverState *bs, BdrvRequestFlags flags);
 int bdrv_writev(BlockDriverState *bs, int64_t sector_num, QEMUIOVector *qiov);
 int bdrv_pread(BlockDriverState *bs, int64_t offset,
                void *buf, int count);
Signature · 1 line
1.7.9.5
#20 lore raw

Re: [Qemu-devel] [PATCH 07/12] block: introduce bdrv_zeroize

Quoted 33 lines · Peter Lieven

this patch adds a call to completely zero out a block device. the operation is speed up by checking the block status and only writing zeroes to the device if they currently do not return zeroes. optionally the zero writing can be speed up by setting the flag BDRV_REQ_MAY_UNMAP to emulate the zero write by unmapping if the driver supports it.

---
  block.c               |   45 +++++++++++++++++++++++++++++++++++++++++++++
  include/block/block.h |    1 +
  2 files changed, 46 insertions(+)
block.c +12 -0
index 6f498fc..76a6621 100644
@@ -2342,6 +2342,51 @@ int bdrv_write_zeroes(BlockDriverState *bs, int64_t sector_num,
                        BDRV_REQ_ZERO_WRITE | flags);
  }
 
+int bdrv_zeroize(BlockDriverState *bs, BdrvRequestFlags flags)
+{
+    BlockDriverInfo bdi;
+    int64_t target_size = bdrv_getlength(bs) / BDRV_SECTOR_SIZE;
+    int64_t ret, nb_sectors, sector_num = 0;
+    int n;
+    /* split the write requests into 1MB chunks if the driver
+     * does not return a maximal size via bdi */
+    int max_write_zeroes = 1 << (20 - BDRV_SECTOR_BITS);
+    if (bdrv_get_info(bs, &bdi) == 0 &&
+        bdi.max_write_zeroes > 0) {
+        max_write_zeroes = bdi.max_write_zeroes;

block.c int bdrv_write_zeroes(BlockDriverState *bs, int64_t sector_num, · line 2366

Quoted 10 lines
block.c +10 -0
+    }
+    for (;;) {
+        nb_sectors = target_size - sector_num;
+        if (nb_sectors <= 0) {
+            return 0;
+        }
+        if (nb_sectors > INT_MAX) {
+            nb_sectors = INT_MAX;
+        }
+        ret = bdrv_get_block_status(bs, sector_num, nb_sectors, &n);

forgot to check for ret < 0 here.

Peter

#21 lore raw

Re: [Qemu-devel] [PATCH 07/12] block: introduce bdrv_zeroize

In reply to Peter Lieven
Quoted 2 lines · Peter Lieven

this patch adds a call to completely zero out a block device. the operation is speed up by checking the block status and

s/speed/sped/

Quoted 2 lines

only writing zeroes to the device if they currently do not return zeroes. optionally the zero writing can be speed up

and again

Quoted 9 lines

by setting the flag BDRV_REQ_MAY_UNMAP to emulate the zero write by unmapping if the driver supports it.

---
 block.c               |   45 +++++++++++++++++++++++++++++++++++++++++++++
 include/block/block.h |    1 +
 2 files changed, 46 insertions(+)
Signature · 2 lines
Eric Blake   eblake redhat com    +1-919-301-3266
Libvirt virtualization library http://libvirt.org
#22 lore raw

PATCH 8/12 qemu-img: conditionally zero out target on convert

In reply to Peter Lieven

if the target has_zero_init = 0, but supports efficiently writing zeroes by unmapping we call bdrv_zeroize to avoid fully allocating the target. this currently is designed especially for iscsi.

---
 qemu-img.c |   22 ++++++++++++++++------
 1 file changed, 16 insertions(+), 6 deletions(-)
qemu-img.c +16 -6
index 3e5e388..6eaddc6 100644
@@ -1354,7 +1354,8 @@ static int img_convert(int argc, char **argv)
         }
     }
 
-    flags = BDRV_O_RDWR;
+    flags = BDRV_O_RDWR | BDRV_O_UNMAP;
+
     ret = bdrv_parse_cache_flags(cache, &flags);
     if (ret < 0) {
         error_report("Invalid cache option: %s", cache);
@@ -1386,12 +1387,13 @@ static int img_convert(int argc, char **argv)
         }
     }
 
+    ret = bdrv_get_info(out_bs, &bdi);
+    if (ret < 0) {
+        error_report("could not get block driver info");
+        goto out;
+    }
+
     if (compress) {
-        ret = bdrv_get_info(out_bs, &bdi);
-        if (ret < 0) {
-            error_report("could not get block driver info");
-            goto out;
-        }
         cluster_size = bdi.cluster_size;
         if (cluster_size <= 0 || cluster_size > IO_BUF_SIZE) {
             error_report("invalid cluster size");
@@ -1470,6 +1472,14 @@ static int img_convert(int argc, char **argv)
     } else {
         int has_zero_init = bdrv_has_zero_init(out_bs);
 
+        if (!has_zero_init && !out_baseimg && bdi.discard_write_zeroes) {
+            ret = bdrv_zeroize(out_bs, BDRV_REQ_MAY_UNMAP);
+            if (ret < 0) {
+                goto out;
+            }
+            has_zero_init = 1;
+        }
+
         sector_num = 0; // total number of sectors converted so far
         nb_sectors = total_sectors - sector_num;
         if (nb_sectors != 0) {
Signature · 1 line
1.7.9.5
#23 lore raw

Re: [Qemu-devel] [PATCH 08/12] qemu-img: conditionally zero out target on convert

Il 13/09/2013 12:25, Peter Lieven ha scritto:

Quoted 15 lines

if the target has_zero_init = 0, but supports efficiently writing zeroes by unmapping we call bdrv_zeroize to avoid fully allocating the target. this currently is designed especially for iscsi.

---
 qemu-img.c |   22 ++++++++++++++++------
 1 file changed, 16 insertions(+), 6 deletions(-)
qemu-img.c +0 -0
index 3e5e388..6eaddc6 100644

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

Quoted 5 lines
qemu-img.c +1 -1
@@ -1354,7 +1354,8 @@ static int img_convert(int argc, char **argv)
         }
     }
 
-    flags = BDRV_O_RDWR;
+    flags = BDRV_O_RDWR | BDRV_O_UNMAP;

I think this should be a new command-line flag.

Paolo

Quoted 39 lines
qemu-img.c +15 -5
+
     ret = bdrv_parse_cache_flags(cache, &flags);
     if (ret < 0) {
         error_report("Invalid cache option: %s", cache);
@@ -1386,12 +1387,13 @@ static int img_convert(int argc, char **argv)
         }
     }
 
+    ret = bdrv_get_info(out_bs, &bdi);
+    if (ret < 0) {
+        error_report("could not get block driver info");
+        goto out;
+    }
+
     if (compress) {
-        ret = bdrv_get_info(out_bs, &bdi);
-        if (ret < 0) {
-            error_report("could not get block driver info");
-            goto out;
-        }
         cluster_size = bdi.cluster_size;
         if (cluster_size <= 0 || cluster_size > IO_BUF_SIZE) {
             error_report("invalid cluster size");
@@ -1470,6 +1472,14 @@ static int img_convert(int argc, char **argv)
     } else {
         int has_zero_init = bdrv_has_zero_init(out_bs);
 
+        if (!has_zero_init && !out_baseimg && bdi.discard_write_zeroes) {
+            ret = bdrv_zeroize(out_bs, BDRV_REQ_MAY_UNMAP);
+            if (ret < 0) {
+                goto out;
+            }
+            has_zero_init = 1;
+        }
+
         sector_num = 0; // total number of sectors converted so far
         nb_sectors = total_sectors - sector_num;
         if (nb_sectors != 0) {
#24 lore raw

Re: [Qemu-devel] [PATCH 08/12] qemu-img: conditionally zero out target on convert

Quoted 19 lines · Paolo Bonzini

Il 13/09/2013 12:25, Peter Lieven ha scritto:

Quoted 18 lines

if the target has_zero_init = 0, but supports efficiently writing zeroes by unmapping we call bdrv_zeroize to avoid fully allocating the target. this currently is designed especially for iscsi.

---
  qemu-img.c |   22 ++++++++++++++++------
  1 file changed, 16 insertions(+), 6 deletions(-)
qemu-img.c +0 -0
index 3e5e388..6eaddc6 100644
@@ -1354,7 +1354,8 @@ static int img_convert(int argc, char **argv)
          }
      }
 
Quoted 3 lines

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

Quoted 2 lines
qemu-img.c +1 -1
-    flags = BDRV_O_RDWR;
+    flags = BDRV_O_RDWR | BDRV_O_UNMAP;

I think this should be a new command-line flag.

In an earlier version there where no objections. I think it would make the usage of qemu-img convert more complicated. For most targets has_zero_init is 1 anyway.

Peter

#25 lore raw

Re: [Qemu-devel] [PATCH 08/12] qemu-img: conditionally zero out target on convert

Il 13/09/2013 12:46, Peter Lieven ha scritto:

Quoted 7 lines
Quoted 4 lines
Quoted 3 lines

- flags = BDRV_O_RDWR;
+ flags = BDRV_O_RDWR | BDRV_O_UNMAP;

I think this should be a new command-line flag.

In an earlier version there where no objections. I think it would make the usage of qemu-img convert more complicated. For most targets has_zero_init is 1 anyway.

If Kevin is okay with having BDRV_O_UNMAP as the default that's fine. However there should be an option to disable it. Perhaps it could be "-S 0" with something like this added:

qemu-img.c +4 -0
index 3e5e388..f42c90c 100644
@@ -769,6 +769,10 @@
     int ret;
     int num_checked, num_used;
 
+    if (!min) {
+        return 1;
+    }
+
     if (n < min) {
         min = n;
     }

Paolo

#26 lore raw

Re: [Qemu-devel] [PATCH 08/12] qemu-img: conditionally zero out target on convert

In reply to Paolo Bonzini
Quoted 19 lines · Paolo Bonzini

Il 13/09/2013 12:25, Peter Lieven ha scritto:

Quoted 18 lines

if the target has_zero_init = 0, but supports efficiently writing zeroes by unmapping we call bdrv_zeroize to avoid fully allocating the target. this currently is designed especially for iscsi.

---
 qemu-img.c |   22 ++++++++++++++++------
 1 file changed, 16 insertions(+), 6 deletions(-)
qemu-img.c +0 -0
index 3e5e388..6eaddc6 100644
@@ -1354,7 +1354,8 @@ static int img_convert(int argc, char **argv)
         }
     }
 
Quoted 4 lines

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

Quoted 2 lines
qemu-img.c +1 -1
-    flags = BDRV_O_RDWR;
+    flags = BDRV_O_RDWR | BDRV_O_UNMAP;

I think this should be a new command-line flag.

I agree - while 'sparse by default' may be reasonable, it is also feasible to want a mode that guarantees expansion rather than unmapped or sparse.

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

Re: [Qemu-devel] [PATCH 08/12] qemu-img: conditionally zero out target on convert

Am 13.09.2013 20:25, schrieb Eric Blake:

Quoted 20 lines · Paolo Bonzini
Quoted 19 lines

Il 13/09/2013 12:25, Peter Lieven ha scritto:

Quoted 18 lines

if the target has_zero_init = 0, but supports efficiently writing zeroes by unmapping we call bdrv_zeroize to avoid fully allocating the target. this currently is designed especially for iscsi.

---
 qemu-img.c |   22 ++++++++++++++++------
 1 file changed, 16 insertions(+), 6 deletions(-)
qemu-img.c +0 -0
index 3e5e388..6eaddc6 100644
@@ -1354,7 +1354,8 @@ static int img_convert(int argc, char **argv)
         }
     }
 
Quoted 7 lines
Quoted 3 lines

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

Quoted 2 lines
qemu-img.c +1 -1
-    flags = BDRV_O_RDWR;
+    flags = BDRV_O_RDWR | BDRV_O_UNMAP;

I think this should be a new command-line flag.

I agree - while 'sparse by default' may be reasonable, it is also feasible to want a mode that guarantees expansion rather than unmapped or sparse.

Ok, so do you find the proposed -S 0 bei Paolo a good choice? If this is supplied I would go as far as completly setting has_zero_init = 0 also for targets which default to 1. This would guaranteed exspansion and full allocation for all drivers.

Peter

#28 lore raw

Re: [Qemu-devel] [PATCH 08/12] qemu-img: conditionally zero out target on convert

Quoted 10 lines · Peter Lieven
Quoted 6 lines
Quoted 2 lines
Quoted 1 line

+ flags = BDRV_O_RDWR | BDRV_O_UNMAP;

I think this should be a new command-line flag.

I agree - while 'sparse by default' may be reasonable, it is also feasible to want a mode that guarantees expansion rather than unmapped or sparse.

Ok, so do you find the proposed -S 0 bei Paolo a good choice? If this is supplied I would go as far as completly setting has_zero_init = 0 also for targets which default to 1. This would guaranteed exspansion and full allocation for all drivers.

Yeah, works for me - an optional mode that guarantees full allocation.

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

Re: [Qemu-devel] [PATCH 08/12] qemu-img: conditionally zero out target on convert

In reply to Peter Lieven

Il 13/09/2013 21:48, Peter Lieven ha scritto:

Quoted 24 lines

Am 13.09.2013 20:25, schrieb Eric Blake:

Quoted 23 lines · Paolo Bonzini
Quoted 22 lines

Il 13/09/2013 12:25, Peter Lieven ha scritto:

Quoted 15 lines

if the target has_zero_init = 0, but supports efficiently writing zeroes by unmapping we call bdrv_zeroize to avoid fully allocating the target. this currently is designed especially for iscsi.

---
 qemu-img.c |   22 ++++++++++++++++------
 1 file changed, 16 insertions(+), 6 deletions(-)
qemu-img.c +0 -0
index 3e5e388..6eaddc6 100644

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

Quoted 5 lines
qemu-img.c +1 -1
@@ -1354,7 +1354,8 @@ static int img_convert(int argc, char **argv)
         }
     }
 
-    flags = BDRV_O_RDWR;
+    flags = BDRV_O_RDWR | BDRV_O_UNMAP;

I think this should be a new command-line flag.

Quoted 8 lines
Quoted 4 lines

I agree - while 'sparse by default' may be reasonable, it is also feasible to want a mode that guarantees expansion rather than unmapped or sparse.

Ok, so do you find the proposed -S 0 bei Paolo a good choice? If this is supplied I would go as far as completly setting has_zero_init = 0 also for targets which default to 1. This would guaranteed exspansion and full allocation for all drivers.

Sounds good.

Paolo

#30 lore raw

PATCH 9/12 block/get_block_status: set *pnum = 0 on error

In reply to Peter Lieven

if the call is invoked through bdrv_is_allocated the caller might expect *pnum = 0 on error. however, a new implementation of bdrv_get_block_status might only return a negative exit value on error while keeping *pnum untouched.

---
 block.c |    1 +
 1 file changed, 1 insertion(+)
block.c +1 -0
index 76a6621..4922fb9 100644
@@ -3149,6 +3149,7 @@ static int64_t coroutine_fn bdrv_co_get_block_status(BlockDriverState *bs,
 
     ret = bs->drv->bdrv_co_get_block_status(bs, sector_num, nb_sectors, pnum);
     if (ret < 0) {
+        *pnum = 0;
         return ret;
     }
 
Signature · 1 line
1.7.9.5
Reviewed-by on v1 9/12
#31 lore raw

Re: [Qemu-devel] [PATCH 09/12] block/get_block_status: set *pnum = 0 on error

Quoted 9 lines · Peter Lieven

if the call is invoked through bdrv_is_allocated the caller might expect *pnum = 0 on error. however, a new implementation of bdrv_get_block_status might only return a negative exit value on error while keeping *pnum untouched.

---
 block.c |    1 +
 1 file changed, 1 insertion(+)
Quoted 14 lines
block.c +1 -0
index 76a6621..4922fb9 100644
@@ -3149,6 +3149,7 @@ static int64_t coroutine_fn bdrv_co_get_block_status(BlockDriverState *bs,
 
     ret = bs->drv->bdrv_co_get_block_status(bs, sector_num, nb_sectors, pnum);
     if (ret < 0) {
+        *pnum = 0;
         return ret;
     }
 
Signature · 2 lines
Eric Blake   eblake redhat com    +1-919-301-3266
Libvirt virtualization library http://libvirt.org
#32 lore raw

PATCH 10/12 block/get_block_status: avoid segfault if there is no backing_hd

In reply to Peter Lieven
---
 block.c |    2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
block.c +1 -1
index 4922fb9..cf9db62 100644
@@ -3156,7 +3156,7 @@ static int64_t coroutine_fn bdrv_co_get_block_status(BlockDriverState *bs,
     if (!(ret & BDRV_BLOCK_DATA)) {
         if (bdrv_has_zero_init(bs)) {
             ret |= BDRV_BLOCK_ZERO;
-        } else {
+        } else if (bs->backing_hd) {
             BlockDriverState *bs2 = bs->backing_hd;
             int64_t length2 = bdrv_getlength(bs2);
             if (length2 >= 0 && sector_num >= (length2 >> BDRV_SECTOR_BITS)) {
Signature · 1 line
1.7.9.5
Reviewed-by on v1 10/12
#33 lore raw

Re: [Qemu-devel] [PATCH 10/12] block/get_block_status: avoid segfault if there is no backing_hd

Quoted 4 lines · Peter Lieven
---
 block.c |    2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
Quoted 15 lines
block.c +1 -1
index 4922fb9..cf9db62 100644
@@ -3156,7 +3156,7 @@ static int64_t coroutine_fn bdrv_co_get_block_status(BlockDriverState *bs,
     if (!(ret & BDRV_BLOCK_DATA)) {
         if (bdrv_has_zero_init(bs)) {
             ret |= BDRV_BLOCK_ZERO;
-        } else {
+        } else if (bs->backing_hd) {
             BlockDriverState *bs2 = bs->backing_hd;
             int64_t length2 = bdrv_getlength(bs2);
             if (length2 >= 0 && sector_num >= (length2 >> BDRV_SECTOR_BITS)) {
Signature · 2 lines
Eric Blake   eblake redhat com    +1-919-301-3266
Libvirt virtualization library http://libvirt.org
#34 lore raw

PATCH 11/12 block/get_block_status: avoid redundant callouts on raw devices

In reply to Peter Lieven

if a raw device like an iscsi target or host device is used the current implementation makes a second call out to get the block status of bs->file. however, the raw driver already has called bdrv_get_block_status on bs->file.

---
 block.c |    3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
block.c +2 -1
index cf9db62..282943b 100644
@@ -3167,7 +3167,8 @@ static int64_t coroutine_fn bdrv_co_get_block_status(BlockDriverState *bs,
 
     if (bs->file &&
         (ret & BDRV_BLOCK_DATA) && !(ret & BDRV_BLOCK_ZERO) &&
-        (ret & BDRV_BLOCK_OFFSET_VALID)) {
+        (ret & BDRV_BLOCK_OFFSET_VALID) &&
+        strcmp(bs->drv->format_name, "raw")) {
         ret2 = bdrv_co_get_block_status(bs->file, ret >> BDRV_SECTOR_BITS,
                                         *pnum, pnum);
         if (ret2 >= 0) {
Signature · 1 line
1.7.9.5
Reviewed-by on v1 11/12
#35 lore raw

Re: [Qemu-devel] [PATCH 11/12] block/get_block_status: avoid redundant callouts on raw devices

Quoted 9 lines · Peter Lieven

if a raw device like an iscsi target or host device is used the current implementation makes a second call out to get the block status of bs->file. however, the raw driver already has called bdrv_get_block_status on bs->file.

---
 block.c |    3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
Quoted 16 lines
block.c +2 -1
index cf9db62..282943b 100644
@@ -3167,7 +3167,8 @@ static int64_t coroutine_fn bdrv_co_get_block_status(BlockDriverState *bs,
 
     if (bs->file &&
         (ret & BDRV_BLOCK_DATA) && !(ret & BDRV_BLOCK_ZERO) &&
-        (ret & BDRV_BLOCK_OFFSET_VALID)) {
+        (ret & BDRV_BLOCK_OFFSET_VALID) &&
+        strcmp(bs->drv->format_name, "raw")) {
         ret2 = bdrv_co_get_block_status(bs->file, ret >> BDRV_SECTOR_BITS,
                                         *pnum, pnum);
         if (ret2 >= 0) {
Signature · 2 lines
Eric Blake   eblake redhat com    +1-919-301-3266
Libvirt virtualization library http://libvirt.org
#36 lore raw

PATCH 12/12 block/get_block_status: fix BDRV_BLOCK_ZERO for unallocated blocks

In reply to Peter Lieven

this patch does 2 things:
a) only do additional call outs if BDRV_BLOCK_ZERO is not already set.
b) use bdi.discard_zeroes to return the zero state of an unallocated block. the callout to bdrv_has_zero_init() is only valid right after bdrv_create.

---
 block.c |    6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)
block.c +4 -2
index 282943b..6698c10 100644
@@ -3153,8 +3153,10 @@ static int64_t coroutine_fn bdrv_co_get_block_status(BlockDriverState *bs,
         return ret;
     }
 
-    if (!(ret & BDRV_BLOCK_DATA)) {
-        if (bdrv_has_zero_init(bs)) {
+    if (!(ret & BDRV_BLOCK_DATA) && !(ret & BDRV_BLOCK_ZERO)) {
+        BlockDriverInfo bdi;
+        if (bdrv_get_info(bs, &bdi) == 0 &&
+            bdi.discard_zeroes) {
             ret |= BDRV_BLOCK_ZERO;
         } else if (bs->backing_hd) {
             BlockDriverState *bs2 = bs->backing_hd;
Signature · 1 line
1.7.9.5
#37 lore raw

Re: [Qemu-devel] [PATCH 12/12] block/get_block_status: fix BDRV_BLOCK_ZERO for unallocated blocks

Quoted 15 lines · Peter Lieven

this patch does 2 things:
a) only do additional call outs if BDRV_BLOCK_ZERO is not already set.
b) use bdi.discard_zeroes to return the zero state of an unallocated block. the callout to bdrv_has_zero_init() is only valid right after bdrv_create.

---
 block.c |    6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)
block.c +0 -0
index 282943b..6698c10 100644

block.c static int64_t coroutine_fn bdrv_co_get_block_status(BlockDriverState *bs, · line 3156

Quoted 6 lines
block.c +1 -2
@@ -3153,8 +3153,10 @@ static int64_t coroutine_fn bdrv_co_get_block_status(BlockDriverState *bs,
         return ret;
     }
 
-    if (!(ret & BDRV_BLOCK_DATA)) {
-        if (bdrv_has_zero_init(bs)) {
+    if (!(ret & BDRV_BLOCK_DATA) && !(ret & BDRV_BLOCK_ZERO)) {

Why not avoid the &&:

if ((ret & (BDRV_BLOCK_DATA | BDRV_BLOCK_ZERO)) == 0) {

Then again, any decent compiler will probably already do that optimization on your behalf.

Quoted 7 lines
block.c +3 -0
+        BlockDriverInfo bdi;
+        if (bdrv_get_info(bs, &bdi) == 0 &&
+            bdi.discard_zeroes) {
             ret |= BDRV_BLOCK_ZERO;
         } else if (bs->backing_hd) {
             BlockDriverState *bs2 = bs->backing_hd;
Signature · 2 lines
Eric Blake   eblake redhat com    +1-919-301-3266
Libvirt virtualization library http://libvirt.org
#38 lore raw

Re: [Qemu-devel] [PATCH 12/12] block/get_block_status: fix BDRV_BLOCK_ZERO for unallocated blocks

In reply to Peter Lieven
Quoted 17 lines · Peter Lieven

this patch does 2 things:
a) only do additional call outs if BDRV_BLOCK_ZERO is not already set.
b) use bdi.discard_zeroes to return the zero state of an unallocated block. the callout to bdrv_has_zero_init() is only valid right after bdrv_create.

---
  block.c |    6 ++++--
  1 file changed, 4 insertions(+), 2 deletions(-)
block.c +0 -0
index 282943b..6698c10 100644
@@ -3153,8 +3153,10 @@ static int64_t coroutine_fn bdrv_co_get_block_status(BlockDriverState *bs,
          return ret;
      }

block.c static int64_t coroutine_fn bdrv_co_get_block_status(BlockDriverState *bs, · line 3162

Quoted 10 lines
block.c +4 -2
 
-    if (!(ret & BDRV_BLOCK_DATA)) {
-        if (bdrv_has_zero_init(bs)) {
+    if (!(ret & BDRV_BLOCK_DATA) && !(ret & BDRV_BLOCK_ZERO)) {
+        BlockDriverInfo bdi;
+        if (bdrv_get_info(bs, &bdi) == 0 &&
+            bdi.discard_zeroes) {
              ret |= BDRV_BLOCK_ZERO;
          } else if (bs->backing_hd) {
              BlockDriverState *bs2 = bs->backing_hd;

Kevin, according to your comment that we need a separate function like bdrv_has_discard_zeroes. It should also be ok to change the order and first check for bs->backing_hd here or not?

Peter