Patchwork [04/12] iscsi: add .bdrv_co_write_zeroes

login
register
mail settings
Submitter Peter Lieven
Date Sept. 13, 2013, 10:25 a.m.
Message ID <1379067909-22984-5-git-send-email-pl@kamp.de>
Download mbox | patch
Permalink /patch/274713/
State New
Headers show

Comments

Peter Lieven - Sept. 13, 2013, 10:25 a.m.
Signed-off-by: Peter Lieven <pl@kamp.de>
---
 block/iscsi.c |   78 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 78 insertions(+)
Eric Blake - Sept. 13, 2013, 6:10 p.m.
On 09/13/2013 04:25 AM, Peter Lieven wrote:
> Signed-off-by: Peter Lieven <pl@kamp.de>
> ---
>  block/iscsi.c |   78 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++
>  1 file changed, 78 insertions(+)
> 

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

> +        }
> +        memset(iscsilun->zeroblock, 0x00, iscsilun->block_size);

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

Patch

diff --git a/block/iscsi.c b/block/iscsi.c
index 68f99d3..3d7961d 100644
--- a/block/iscsi.c
+++ b/block/iscsi.c
@@ -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,