Fix iSCSI crash on SG_IO with an iovector
Don't assume that SG_IO is always invoked with a simple buffer,
check the iovec_count and if it is >= 1 then we need to pass an array
of iovectors to libiscsi instead of just a plain buffer.
Signed-off-by: Ronnie Sahlberg <ronniesahlberg@gmail.com>
Cc: qemu-stable@nongnu.org
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
(cherry picked from commit 0a53f01074)
Signed-off-by: Michael Roth <mdroth@linux.vnet.ibm.com>
			
			
This commit is contained in:
		
							parent
							
								
									5e2053dd15
								
							
						
					
					
						commit
						89ca606d49
					
				| 
						 | 
					@ -32,6 +32,7 @@
 | 
				
			||||||
#include "block/block_int.h"
 | 
					#include "block/block_int.h"
 | 
				
			||||||
#include "trace.h"
 | 
					#include "trace.h"
 | 
				
			||||||
#include "block/scsi.h"
 | 
					#include "block/scsi.h"
 | 
				
			||||||
 | 
					#include "qemu/iov.h"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
#include <iscsi/iscsi.h>
 | 
					#include <iscsi/iscsi.h>
 | 
				
			||||||
#include <iscsi/scsi-lowlevel.h>
 | 
					#include <iscsi/scsi-lowlevel.h>
 | 
				
			||||||
| 
						 | 
					@ -651,6 +652,9 @@ iscsi_aio_ioctl_cb(struct iscsi_context *iscsi, int status,
 | 
				
			||||||
{
 | 
					{
 | 
				
			||||||
    IscsiAIOCB *acb = opaque;
 | 
					    IscsiAIOCB *acb = opaque;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    g_free(acb->buf);
 | 
				
			||||||
 | 
					    acb->buf = NULL;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    if (acb->canceled != 0) {
 | 
					    if (acb->canceled != 0) {
 | 
				
			||||||
        return;
 | 
					        return;
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
| 
						 | 
					@ -727,14 +731,30 @@ static BlockDriverAIOCB *iscsi_aio_ioctl(BlockDriverState *bs,
 | 
				
			||||||
    memcpy(&acb->task->cdb[0], acb->ioh->cmdp, acb->ioh->cmd_len);
 | 
					    memcpy(&acb->task->cdb[0], acb->ioh->cmdp, acb->ioh->cmd_len);
 | 
				
			||||||
    acb->task->expxferlen = acb->ioh->dxfer_len;
 | 
					    acb->task->expxferlen = acb->ioh->dxfer_len;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    data.size = 0;
 | 
				
			||||||
    if (acb->task->xfer_dir == SCSI_XFER_WRITE) {
 | 
					    if (acb->task->xfer_dir == SCSI_XFER_WRITE) {
 | 
				
			||||||
 | 
					        if (acb->ioh->iovec_count == 0) {
 | 
				
			||||||
            data.data = acb->ioh->dxferp;
 | 
					            data.data = acb->ioh->dxferp;
 | 
				
			||||||
            data.size = acb->ioh->dxfer_len;
 | 
					            data.size = acb->ioh->dxfer_len;
 | 
				
			||||||
 | 
					        } else {
 | 
				
			||||||
 | 
					#if defined(LIBISCSI_FEATURE_IOVECTOR)
 | 
				
			||||||
 | 
					            scsi_task_set_iov_out(acb->task,
 | 
				
			||||||
 | 
					                                 (struct scsi_iovec *) acb->ioh->dxferp,
 | 
				
			||||||
 | 
					                                 acb->ioh->iovec_count);
 | 
				
			||||||
 | 
					#else
 | 
				
			||||||
 | 
					            struct iovec *iov = (struct iovec *)acb->ioh->dxferp;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					            acb->buf = g_malloc(acb->ioh->dxfer_len);
 | 
				
			||||||
 | 
					            data.data = acb->buf;
 | 
				
			||||||
 | 
					            data.size = iov_to_buf(iov, acb->ioh->iovec_count, 0,
 | 
				
			||||||
 | 
					                                   acb->buf, acb->ioh->dxfer_len);
 | 
				
			||||||
 | 
					#endif
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    if (iscsi_scsi_command_async(iscsi, iscsilun->lun, acb->task,
 | 
					    if (iscsi_scsi_command_async(iscsi, iscsilun->lun, acb->task,
 | 
				
			||||||
                                 iscsi_aio_ioctl_cb,
 | 
					                                 iscsi_aio_ioctl_cb,
 | 
				
			||||||
                                 (acb->task->xfer_dir == SCSI_XFER_WRITE) ?
 | 
					                                 (data.size > 0) ? &data : NULL,
 | 
				
			||||||
                                     &data : NULL,
 | 
					 | 
				
			||||||
                                 acb) != 0) {
 | 
					                                 acb) != 0) {
 | 
				
			||||||
        scsi_free_scsi_task(acb->task);
 | 
					        scsi_free_scsi_task(acb->task);
 | 
				
			||||||
        qemu_aio_release(acb);
 | 
					        qemu_aio_release(acb);
 | 
				
			||||||
| 
						 | 
					@ -743,9 +763,26 @@ static BlockDriverAIOCB *iscsi_aio_ioctl(BlockDriverState *bs,
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    /* tell libiscsi to read straight into the buffer we got from ioctl */
 | 
					    /* tell libiscsi to read straight into the buffer we got from ioctl */
 | 
				
			||||||
    if (acb->task->xfer_dir == SCSI_XFER_READ) {
 | 
					    if (acb->task->xfer_dir == SCSI_XFER_READ) {
 | 
				
			||||||
 | 
					        if (acb->ioh->iovec_count == 0) {
 | 
				
			||||||
            scsi_task_add_data_in_buffer(acb->task,
 | 
					            scsi_task_add_data_in_buffer(acb->task,
 | 
				
			||||||
                                         acb->ioh->dxfer_len,
 | 
					                                         acb->ioh->dxfer_len,
 | 
				
			||||||
                                         acb->ioh->dxferp);
 | 
					                                         acb->ioh->dxferp);
 | 
				
			||||||
 | 
					        } else {
 | 
				
			||||||
 | 
					#if defined(LIBISCSI_FEATURE_IOVECTOR)
 | 
				
			||||||
 | 
					            scsi_task_set_iov_in(acb->task,
 | 
				
			||||||
 | 
					                                 (struct scsi_iovec *) acb->ioh->dxferp,
 | 
				
			||||||
 | 
					                                 acb->ioh->iovec_count);
 | 
				
			||||||
 | 
					#else
 | 
				
			||||||
 | 
					            int i;
 | 
				
			||||||
 | 
					            for (i = 0; i < acb->ioh->iovec_count; i++) {
 | 
				
			||||||
 | 
					                struct iovec *iov = (struct iovec *)acb->ioh->dxferp;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					                scsi_task_add_data_in_buffer(acb->task,
 | 
				
			||||||
 | 
					                    iov[i].iov_len,
 | 
				
			||||||
 | 
					                    iov[i].iov_base);
 | 
				
			||||||
 | 
					            }
 | 
				
			||||||
 | 
					#endif
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    iscsi_set_events(iscsilun);
 | 
					    iscsi_set_events(iscsilun);
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
		Reference in New Issue