Ronnie Sahlberg | c589b24 | 2011-10-25 19:24:24 +1100 | [diff] [blame] | 1 | /* |
| 2 | * QEMU Block driver for iSCSI images |
| 3 | * |
| 4 | * Copyright (c) 2010-2011 Ronnie Sahlberg <ronniesahlberg@gmail.com> |
Peter Lieven | 03e40fe | 2015-04-16 16:08:32 +0200 | [diff] [blame] | 5 | * Copyright (c) 2012-2015 Peter Lieven <pl@kamp.de> |
Ronnie Sahlberg | c589b24 | 2011-10-25 19:24:24 +1100 | [diff] [blame] | 6 | * |
| 7 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 8 | * of this software and associated documentation files (the "Software"), to deal |
| 9 | * in the Software without restriction, including without limitation the rights |
| 10 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 11 | * copies of the Software, and to permit persons to whom the Software is |
| 12 | * furnished to do so, subject to the following conditions: |
| 13 | * |
| 14 | * The above copyright notice and this permission notice shall be included in |
| 15 | * all copies or substantial portions of the Software. |
| 16 | * |
| 17 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 18 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 19 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL |
| 20 | * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 21 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 22 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 23 | * THE SOFTWARE. |
| 24 | */ |
| 25 | |
Peter Maydell | 80c71a2 | 2016-01-18 18:01:42 +0000 | [diff] [blame] | 26 | #include "qemu/osdep.h" |
Ronnie Sahlberg | c589b24 | 2011-10-25 19:24:24 +1100 | [diff] [blame] | 27 | |
| 28 | #include <poll.h> |
Peter Lieven | efc6de0 | 2014-05-30 23:36:47 +0200 | [diff] [blame] | 29 | #include <math.h> |
Ronnie Sahlberg | f4dfa67 | 2012-05-22 20:10:05 +1000 | [diff] [blame] | 30 | #include <arpa/inet.h> |
Ronnie Sahlberg | c589b24 | 2011-10-25 19:24:24 +1100 | [diff] [blame] | 31 | #include "qemu-common.h" |
Paolo Bonzini | 1de7afc | 2012-12-17 18:20:00 +0100 | [diff] [blame] | 32 | #include "qemu/config-file.h" |
| 33 | #include "qemu/error-report.h" |
Peter Lieven | b03c380 | 2014-04-28 13:11:32 +0200 | [diff] [blame] | 34 | #include "qemu/bitops.h" |
| 35 | #include "qemu/bitmap.h" |
Paolo Bonzini | 737e150 | 2012-12-17 18:19:44 +0100 | [diff] [blame] | 36 | #include "block/block_int.h" |
Paolo Bonzini | 0d09e41 | 2013-02-05 17:06:20 +0100 | [diff] [blame] | 37 | #include "block/scsi.h" |
Ronnie Sahlberg | 0a53f01 | 2013-06-23 08:07:08 -0700 | [diff] [blame] | 38 | #include "qemu/iov.h" |
Paolo Bonzini | 5accc84 | 2013-08-02 17:02:01 +0200 | [diff] [blame] | 39 | #include "sysemu/sysemu.h" |
| 40 | #include "qmp-commands.h" |
Markus Armbruster | d49b683 | 2015-03-17 18:29:20 +0100 | [diff] [blame] | 41 | #include "qapi/qmp/qstring.h" |
Daniel P. Berrange | b189346 | 2016-01-21 14:19:21 +0000 | [diff] [blame] | 42 | #include "crypto/secret.h" |
Ronnie Sahlberg | c589b24 | 2011-10-25 19:24:24 +1100 | [diff] [blame] | 43 | |
| 44 | #include <iscsi/iscsi.h> |
| 45 | #include <iscsi/scsi-lowlevel.h> |
| 46 | |
Ronnie Sahlberg | 9839245 | 2012-05-25 21:59:01 +1000 | [diff] [blame] | 47 | #ifdef __linux__ |
| 48 | #include <scsi/sg.h> |
Paolo Bonzini | 0d09e41 | 2013-02-05 17:06:20 +0100 | [diff] [blame] | 49 | #include <block/scsi.h> |
Ronnie Sahlberg | 9839245 | 2012-05-25 21:59:01 +1000 | [diff] [blame] | 50 | #endif |
Ronnie Sahlberg | c589b24 | 2011-10-25 19:24:24 +1100 | [diff] [blame] | 51 | |
| 52 | typedef struct IscsiLun { |
| 53 | struct iscsi_context *iscsi; |
Stefan Hajnoczi | 80cf625 | 2014-05-08 16:34:42 +0200 | [diff] [blame] | 54 | AioContext *aio_context; |
Ronnie Sahlberg | c589b24 | 2011-10-25 19:24:24 +1100 | [diff] [blame] | 55 | int lun; |
Ronnie Sahlberg | dbfff6d | 2012-05-25 21:59:01 +1000 | [diff] [blame] | 56 | enum scsi_inquiry_peripheral_device_type type; |
Ronnie Sahlberg | c589b24 | 2011-10-25 19:24:24 +1100 | [diff] [blame] | 57 | int block_size; |
Paolo Bonzini | c7b4a95 | 2012-05-26 09:41:13 +0200 | [diff] [blame] | 58 | uint64_t num_blocks; |
Ronnie Sahlberg | c9b9f68 | 2012-05-22 19:56:36 +1000 | [diff] [blame] | 59 | int events; |
Peter Lieven | 5b5d34e | 2012-12-06 10:46:47 +0100 | [diff] [blame] | 60 | QEMUTimer *nop_timer; |
Peter Lieven | 05b685f | 2015-04-07 22:08:15 +0200 | [diff] [blame] | 61 | QEMUTimer *event_timer; |
Peter Lieven | f18a7cb | 2013-07-19 09:19:39 +0200 | [diff] [blame] | 62 | struct scsi_inquiry_logical_block_provisioning lbp; |
| 63 | struct scsi_inquiry_block_limits bl; |
Peter Lieven | d4cd961 | 2013-10-24 12:07:02 +0200 | [diff] [blame] | 64 | unsigned char *zeroblock; |
Peter Lieven | b03c380 | 2014-04-28 13:11:32 +0200 | [diff] [blame] | 65 | unsigned long *allocationmap; |
| 66 | int cluster_sectors; |
Peter Lieven | 9281fe9 | 2014-06-04 15:47:39 +0200 | [diff] [blame] | 67 | bool use_16_for_rw; |
Fam Zheng | 43ae8fb | 2015-02-25 12:40:08 +0800 | [diff] [blame] | 68 | bool write_protected; |
Peter Lieven | 0a386e4 | 2015-04-16 16:08:26 +0200 | [diff] [blame] | 69 | bool lbpme; |
| 70 | bool lbprz; |
Peter Lieven | 752ce45 | 2015-04-16 16:08:28 +0200 | [diff] [blame] | 71 | bool dpofua; |
Peter Lieven | 0a386e4 | 2015-04-16 16:08:26 +0200 | [diff] [blame] | 72 | bool has_write_same; |
Peter Lieven | 5dd7a53 | 2015-06-16 13:45:07 +0200 | [diff] [blame] | 73 | bool request_timed_out; |
Ronnie Sahlberg | c589b24 | 2011-10-25 19:24:24 +1100 | [diff] [blame] | 74 | } IscsiLun; |
| 75 | |
Peter Lieven | 54a5c1d | 2013-07-19 09:19:40 +0200 | [diff] [blame] | 76 | typedef struct IscsiTask { |
| 77 | int status; |
| 78 | int complete; |
| 79 | int retries; |
| 80 | int do_retry; |
| 81 | struct scsi_task *task; |
| 82 | Coroutine *co; |
Peter Lieven | 8b9dfe9 | 2013-12-14 17:31:40 +0100 | [diff] [blame] | 83 | QEMUBH *bh; |
Stefan Hajnoczi | 80cf625 | 2014-05-08 16:34:42 +0200 | [diff] [blame] | 84 | IscsiLun *iscsilun; |
Peter Lieven | efc6de0 | 2014-05-30 23:36:47 +0200 | [diff] [blame] | 85 | QEMUTimer retry_timer; |
Fam Zheng | e01dd3d | 2015-11-05 13:00:09 +0800 | [diff] [blame] | 86 | int err_code; |
Peter Lieven | 54a5c1d | 2013-07-19 09:19:40 +0200 | [diff] [blame] | 87 | } IscsiTask; |
| 88 | |
Ronnie Sahlberg | c589b24 | 2011-10-25 19:24:24 +1100 | [diff] [blame] | 89 | typedef struct IscsiAIOCB { |
Markus Armbruster | 7c84b1b | 2014-10-07 13:59:14 +0200 | [diff] [blame] | 90 | BlockAIOCB common; |
Ronnie Sahlberg | c589b24 | 2011-10-25 19:24:24 +1100 | [diff] [blame] | 91 | QEMUIOVector *qiov; |
| 92 | QEMUBH *bh; |
| 93 | IscsiLun *iscsilun; |
| 94 | struct scsi_task *task; |
| 95 | uint8_t *buf; |
| 96 | int status; |
Peter Lieven | 1dde716 | 2013-02-21 16:15:54 +0100 | [diff] [blame] | 97 | int64_t sector_num; |
| 98 | int nb_sectors; |
Fam Zheng | 4bb17ab | 2015-11-09 18:16:49 +0800 | [diff] [blame] | 99 | int ret; |
Ronnie Sahlberg | 9839245 | 2012-05-25 21:59:01 +1000 | [diff] [blame] | 100 | #ifdef __linux__ |
| 101 | sg_io_hdr_t *ioh; |
| 102 | #endif |
Ronnie Sahlberg | c589b24 | 2011-10-25 19:24:24 +1100 | [diff] [blame] | 103 | } IscsiAIOCB; |
| 104 | |
Peter Lieven | 5dd7a53 | 2015-06-16 13:45:07 +0200 | [diff] [blame] | 105 | /* libiscsi uses time_t so its enough to process events every second */ |
| 106 | #define EVENT_INTERVAL 1000 |
Peter Lieven | 5b5d34e | 2012-12-06 10:46:47 +0100 | [diff] [blame] | 107 | #define NOP_INTERVAL 5000 |
| 108 | #define MAX_NOP_FAILURES 3 |
Peter Lieven | efc6de0 | 2014-05-30 23:36:47 +0200 | [diff] [blame] | 109 | #define ISCSI_CMD_RETRIES ARRAY_SIZE(iscsi_retry_times) |
Peter Lieven | 59dd0a2 | 2015-04-16 16:08:30 +0200 | [diff] [blame] | 110 | static const unsigned iscsi_retry_times[] = {8, 32, 128, 512, 2048, 8192, 32768}; |
Peter Lieven | 5b5d34e | 2012-12-06 10:46:47 +0100 | [diff] [blame] | 111 | |
Stefan Weil | 5d831be | 2014-06-13 20:42:57 +0200 | [diff] [blame] | 112 | /* this threshold is a trade-off knob to choose between |
Peter Lieven | 5917af8 | 2014-04-28 17:11:33 +0200 | [diff] [blame] | 113 | * the potential additional overhead of an extra GET_LBA_STATUS request |
| 114 | * vs. unnecessarily reading a lot of zero sectors over the wire. |
| 115 | * If a read request is greater or equal than ISCSI_CHECKALLOC_THRES |
| 116 | * sectors we check the allocation status of the area covered by the |
| 117 | * request first if the allocationmap indicates that the area might be |
| 118 | * unallocated. */ |
| 119 | #define ISCSI_CHECKALLOC_THRES 64 |
Ronnie Sahlberg | c589b24 | 2011-10-25 19:24:24 +1100 | [diff] [blame] | 120 | |
Ronnie Sahlberg | c589b24 | 2011-10-25 19:24:24 +1100 | [diff] [blame] | 121 | static void |
Paolo Bonzini | cfb3f50 | 2012-08-18 23:38:03 +0200 | [diff] [blame] | 122 | iscsi_bh_cb(void *p) |
Paolo Bonzini | 27cbd82 | 2012-08-18 23:37:31 +0200 | [diff] [blame] | 123 | { |
| 124 | IscsiAIOCB *acb = p; |
| 125 | |
| 126 | qemu_bh_delete(acb->bh); |
| 127 | |
Paolo Bonzini | 4790b03 | 2013-01-22 17:34:29 +0100 | [diff] [blame] | 128 | g_free(acb->buf); |
| 129 | acb->buf = NULL; |
| 130 | |
Fam Zheng | 722d933 | 2014-09-11 13:41:15 +0800 | [diff] [blame] | 131 | acb->common.cb(acb->common.opaque, acb->status); |
Paolo Bonzini | 27cbd82 | 2012-08-18 23:37:31 +0200 | [diff] [blame] | 132 | |
Paolo Bonzini | 1bd075f | 2012-08-18 23:35:49 +0200 | [diff] [blame] | 133 | if (acb->task != NULL) { |
| 134 | scsi_free_scsi_task(acb->task); |
| 135 | acb->task = NULL; |
| 136 | } |
| 137 | |
Fam Zheng | 8007429 | 2014-09-11 13:41:28 +0800 | [diff] [blame] | 138 | qemu_aio_unref(acb); |
Paolo Bonzini | 27cbd82 | 2012-08-18 23:37:31 +0200 | [diff] [blame] | 139 | } |
| 140 | |
Paolo Bonzini | cfb3f50 | 2012-08-18 23:38:03 +0200 | [diff] [blame] | 141 | static void |
| 142 | iscsi_schedule_bh(IscsiAIOCB *acb) |
Paolo Bonzini | 27cbd82 | 2012-08-18 23:37:31 +0200 | [diff] [blame] | 143 | { |
Paolo Bonzini | 1bd075f | 2012-08-18 23:35:49 +0200 | [diff] [blame] | 144 | if (acb->bh) { |
| 145 | return; |
| 146 | } |
Stefan Hajnoczi | 80cf625 | 2014-05-08 16:34:42 +0200 | [diff] [blame] | 147 | acb->bh = aio_bh_new(acb->iscsilun->aio_context, iscsi_bh_cb, acb); |
Paolo Bonzini | 27cbd82 | 2012-08-18 23:37:31 +0200 | [diff] [blame] | 148 | qemu_bh_schedule(acb->bh); |
Paolo Bonzini | 27cbd82 | 2012-08-18 23:37:31 +0200 | [diff] [blame] | 149 | } |
| 150 | |
Peter Lieven | 8b9dfe9 | 2013-12-14 17:31:40 +0100 | [diff] [blame] | 151 | static void iscsi_co_generic_bh_cb(void *opaque) |
| 152 | { |
| 153 | struct IscsiTask *iTask = opaque; |
Peter Lieven | fcd470d | 2014-06-10 09:52:16 +0200 | [diff] [blame] | 154 | iTask->complete = 1; |
Peter Lieven | 8b9dfe9 | 2013-12-14 17:31:40 +0100 | [diff] [blame] | 155 | qemu_bh_delete(iTask->bh); |
| 156 | qemu_coroutine_enter(iTask->co, NULL); |
| 157 | } |
| 158 | |
Peter Lieven | efc6de0 | 2014-05-30 23:36:47 +0200 | [diff] [blame] | 159 | static void iscsi_retry_timer_expired(void *opaque) |
| 160 | { |
| 161 | struct IscsiTask *iTask = opaque; |
Peter Lieven | fcd470d | 2014-06-10 09:52:16 +0200 | [diff] [blame] | 162 | iTask->complete = 1; |
Peter Lieven | efc6de0 | 2014-05-30 23:36:47 +0200 | [diff] [blame] | 163 | if (iTask->co) { |
| 164 | qemu_coroutine_enter(iTask->co, NULL); |
| 165 | } |
| 166 | } |
| 167 | |
| 168 | static inline unsigned exp_random(double mean) |
| 169 | { |
| 170 | return -mean * log((double)rand() / RAND_MAX); |
| 171 | } |
| 172 | |
Fam Zheng | e01dd3d | 2015-11-05 13:00:09 +0800 | [diff] [blame] | 173 | /* SCSI_SENSE_ASCQ_INVALID_FIELD_IN_PARAMETER_LIST was introduced in |
| 174 | * libiscsi 1.10.0, together with other constants we need. Use it as |
| 175 | * a hint that we have to define them ourselves if needed, to keep the |
| 176 | * minimum required libiscsi version at 1.9.0. We use an ASCQ macro for |
| 177 | * the test because SCSI_STATUS_* is an enum. |
| 178 | * |
| 179 | * To guard against future changes where SCSI_SENSE_ASCQ_* also becomes |
| 180 | * an enum, check against the LIBISCSI_API_VERSION macro, which was |
| 181 | * introduced in 1.11.0. If it is present, there is no need to define |
| 182 | * anything. |
| 183 | */ |
| 184 | #if !defined(SCSI_SENSE_ASCQ_INVALID_FIELD_IN_PARAMETER_LIST) && \ |
| 185 | !defined(LIBISCSI_API_VERSION) |
| 186 | #define SCSI_STATUS_TASK_SET_FULL 0x28 |
| 187 | #define SCSI_STATUS_TIMEOUT 0x0f000002 |
| 188 | #define SCSI_SENSE_ASCQ_INVALID_FIELD_IN_PARAMETER_LIST 0x2600 |
| 189 | #define SCSI_SENSE_ASCQ_PARAMETER_LIST_LENGTH_ERROR 0x1a00 |
Peter Lieven | 9049736 | 2015-06-26 12:18:01 +0200 | [diff] [blame] | 190 | #endif |
| 191 | |
Fam Zheng | e01dd3d | 2015-11-05 13:00:09 +0800 | [diff] [blame] | 192 | static int iscsi_translate_sense(struct scsi_sense *sense) |
| 193 | { |
| 194 | int ret; |
| 195 | |
| 196 | switch (sense->key) { |
| 197 | case SCSI_SENSE_NOT_READY: |
| 198 | return -EBUSY; |
| 199 | case SCSI_SENSE_DATA_PROTECTION: |
| 200 | return -EACCES; |
| 201 | case SCSI_SENSE_COMMAND_ABORTED: |
| 202 | return -ECANCELED; |
| 203 | case SCSI_SENSE_ILLEGAL_REQUEST: |
| 204 | /* Parse ASCQ */ |
| 205 | break; |
| 206 | default: |
| 207 | return -EIO; |
| 208 | } |
| 209 | switch (sense->ascq) { |
| 210 | case SCSI_SENSE_ASCQ_PARAMETER_LIST_LENGTH_ERROR: |
| 211 | case SCSI_SENSE_ASCQ_INVALID_OPERATION_CODE: |
| 212 | case SCSI_SENSE_ASCQ_INVALID_FIELD_IN_CDB: |
| 213 | case SCSI_SENSE_ASCQ_INVALID_FIELD_IN_PARAMETER_LIST: |
| 214 | ret = -EINVAL; |
| 215 | break; |
| 216 | case SCSI_SENSE_ASCQ_LBA_OUT_OF_RANGE: |
| 217 | ret = -ENOSPC; |
| 218 | break; |
| 219 | case SCSI_SENSE_ASCQ_LOGICAL_UNIT_NOT_SUPPORTED: |
| 220 | ret = -ENOTSUP; |
| 221 | break; |
| 222 | case SCSI_SENSE_ASCQ_MEDIUM_NOT_PRESENT: |
| 223 | case SCSI_SENSE_ASCQ_MEDIUM_NOT_PRESENT_TRAY_CLOSED: |
| 224 | case SCSI_SENSE_ASCQ_MEDIUM_NOT_PRESENT_TRAY_OPEN: |
| 225 | ret = -ENOMEDIUM; |
| 226 | break; |
| 227 | case SCSI_SENSE_ASCQ_WRITE_PROTECTED: |
| 228 | ret = -EACCES; |
| 229 | break; |
| 230 | default: |
| 231 | ret = -EIO; |
| 232 | break; |
| 233 | } |
| 234 | return ret; |
| 235 | } |
| 236 | |
Peter Lieven | 54a5c1d | 2013-07-19 09:19:40 +0200 | [diff] [blame] | 237 | static void |
| 238 | iscsi_co_generic_cb(struct iscsi_context *iscsi, int status, |
| 239 | void *command_data, void *opaque) |
| 240 | { |
| 241 | struct IscsiTask *iTask = opaque; |
| 242 | struct scsi_task *task = command_data; |
| 243 | |
Peter Lieven | 54a5c1d | 2013-07-19 09:19:40 +0200 | [diff] [blame] | 244 | iTask->status = status; |
| 245 | iTask->do_retry = 0; |
| 246 | iTask->task = task; |
| 247 | |
Peter Lieven | 54a5c1d | 2013-07-19 09:19:40 +0200 | [diff] [blame] | 248 | if (status != SCSI_STATUS_GOOD) { |
Peter Lieven | efc6de0 | 2014-05-30 23:36:47 +0200 | [diff] [blame] | 249 | if (iTask->retries++ < ISCSI_CMD_RETRIES) { |
| 250 | if (status == SCSI_STATUS_CHECK_CONDITION |
| 251 | && task->sense.key == SCSI_SENSE_UNIT_ATTENTION) { |
| 252 | error_report("iSCSI CheckCondition: %s", |
| 253 | iscsi_get_error(iscsi)); |
| 254 | iTask->do_retry = 1; |
| 255 | goto out; |
| 256 | } |
Peter Lieven | 9049736 | 2015-06-26 12:18:01 +0200 | [diff] [blame] | 257 | if (status == SCSI_STATUS_BUSY || |
Fam Zheng | e01dd3d | 2015-11-05 13:00:09 +0800 | [diff] [blame] | 258 | status == SCSI_STATUS_TIMEOUT || |
| 259 | status == SCSI_STATUS_TASK_SET_FULL) { |
Peter Lieven | efc6de0 | 2014-05-30 23:36:47 +0200 | [diff] [blame] | 260 | unsigned retry_time = |
| 261 | exp_random(iscsi_retry_times[iTask->retries - 1]); |
Fam Zheng | e01dd3d | 2015-11-05 13:00:09 +0800 | [diff] [blame] | 262 | if (status == SCSI_STATUS_TIMEOUT) { |
Peter Lieven | 5dd7a53 | 2015-06-16 13:45:07 +0200 | [diff] [blame] | 263 | /* make sure the request is rescheduled AFTER the |
| 264 | * reconnect is initiated */ |
| 265 | retry_time = EVENT_INTERVAL * 2; |
| 266 | iTask->iscsilun->request_timed_out = true; |
| 267 | } |
| 268 | error_report("iSCSI Busy/TaskSetFull/TimeOut" |
| 269 | " (retry #%u in %u ms): %s", |
Peter Lieven | efc6de0 | 2014-05-30 23:36:47 +0200 | [diff] [blame] | 270 | iTask->retries, retry_time, |
| 271 | iscsi_get_error(iscsi)); |
| 272 | aio_timer_init(iTask->iscsilun->aio_context, |
| 273 | &iTask->retry_timer, QEMU_CLOCK_REALTIME, |
| 274 | SCALE_MS, iscsi_retry_timer_expired, iTask); |
| 275 | timer_mod(&iTask->retry_timer, |
| 276 | qemu_clock_get_ms(QEMU_CLOCK_REALTIME) + retry_time); |
| 277 | iTask->do_retry = 1; |
| 278 | return; |
| 279 | } |
| 280 | } |
Fam Zheng | e01dd3d | 2015-11-05 13:00:09 +0800 | [diff] [blame] | 281 | iTask->err_code = iscsi_translate_sense(&task->sense); |
Peter Lieven | 837c390 | 2014-02-18 13:08:39 +0100 | [diff] [blame] | 282 | error_report("iSCSI Failure: %s", iscsi_get_error(iscsi)); |
Peter Lieven | 54a5c1d | 2013-07-19 09:19:40 +0200 | [diff] [blame] | 283 | } |
| 284 | |
| 285 | out: |
| 286 | if (iTask->co) { |
Stefan Hajnoczi | 80cf625 | 2014-05-08 16:34:42 +0200 | [diff] [blame] | 287 | iTask->bh = aio_bh_new(iTask->iscsilun->aio_context, |
| 288 | iscsi_co_generic_bh_cb, iTask); |
Peter Lieven | 8b9dfe9 | 2013-12-14 17:31:40 +0100 | [diff] [blame] | 289 | qemu_bh_schedule(iTask->bh); |
Peter Lieven | fcd470d | 2014-06-10 09:52:16 +0200 | [diff] [blame] | 290 | } else { |
| 291 | iTask->complete = 1; |
Peter Lieven | 54a5c1d | 2013-07-19 09:19:40 +0200 | [diff] [blame] | 292 | } |
| 293 | } |
| 294 | |
| 295 | static void iscsi_co_init_iscsitask(IscsiLun *iscsilun, struct IscsiTask *iTask) |
| 296 | { |
| 297 | *iTask = (struct IscsiTask) { |
Peter Lieven | efc6de0 | 2014-05-30 23:36:47 +0200 | [diff] [blame] | 298 | .co = qemu_coroutine_self(), |
Peter Lieven | efc6de0 | 2014-05-30 23:36:47 +0200 | [diff] [blame] | 299 | .iscsilun = iscsilun, |
Peter Lieven | 54a5c1d | 2013-07-19 09:19:40 +0200 | [diff] [blame] | 300 | }; |
| 301 | } |
Paolo Bonzini | 27cbd82 | 2012-08-18 23:37:31 +0200 | [diff] [blame] | 302 | |
| 303 | static void |
Ronnie Sahlberg | c589b24 | 2011-10-25 19:24:24 +1100 | [diff] [blame] | 304 | iscsi_abort_task_cb(struct iscsi_context *iscsi, int status, void *command_data, |
| 305 | void *private_data) |
| 306 | { |
Paolo Bonzini | 1bd075f | 2012-08-18 23:35:49 +0200 | [diff] [blame] | 307 | IscsiAIOCB *acb = private_data; |
| 308 | |
| 309 | acb->status = -ECANCELED; |
| 310 | iscsi_schedule_bh(acb); |
Ronnie Sahlberg | c589b24 | 2011-10-25 19:24:24 +1100 | [diff] [blame] | 311 | } |
| 312 | |
| 313 | static void |
Markus Armbruster | 7c84b1b | 2014-10-07 13:59:14 +0200 | [diff] [blame] | 314 | iscsi_aio_cancel(BlockAIOCB *blockacb) |
Ronnie Sahlberg | c589b24 | 2011-10-25 19:24:24 +1100 | [diff] [blame] | 315 | { |
| 316 | IscsiAIOCB *acb = (IscsiAIOCB *)blockacb; |
| 317 | IscsiLun *iscsilun = acb->iscsilun; |
| 318 | |
Paolo Bonzini | 1bd075f | 2012-08-18 23:35:49 +0200 | [diff] [blame] | 319 | if (acb->status != -EINPROGRESS) { |
| 320 | return; |
| 321 | } |
| 322 | |
Paolo Bonzini | b209091 | 2012-08-19 00:12:39 +0200 | [diff] [blame] | 323 | /* send a task mgmt call to the target to cancel the task on the target */ |
Stefan Priebe | 64e69e8 | 2012-08-15 09:09:54 +0200 | [diff] [blame] | 324 | iscsi_task_mgmt_abort_task_async(iscsilun->iscsi, acb->task, |
Paolo Bonzini | 1bd075f | 2012-08-18 23:35:49 +0200 | [diff] [blame] | 325 | iscsi_abort_task_cb, acb); |
Paolo Bonzini | b209091 | 2012-08-19 00:12:39 +0200 | [diff] [blame] | 326 | |
Ronnie Sahlberg | c589b24 | 2011-10-25 19:24:24 +1100 | [diff] [blame] | 327 | } |
| 328 | |
Stefan Hajnoczi | d7331be | 2012-10-31 16:34:37 +0100 | [diff] [blame] | 329 | static const AIOCBInfo iscsi_aiocb_info = { |
Ronnie Sahlberg | c589b24 | 2011-10-25 19:24:24 +1100 | [diff] [blame] | 330 | .aiocb_size = sizeof(IscsiAIOCB), |
Fam Zheng | 722d933 | 2014-09-11 13:41:15 +0800 | [diff] [blame] | 331 | .cancel_async = iscsi_aio_cancel, |
Ronnie Sahlberg | c589b24 | 2011-10-25 19:24:24 +1100 | [diff] [blame] | 332 | }; |
| 333 | |
| 334 | |
| 335 | static void iscsi_process_read(void *arg); |
| 336 | static void iscsi_process_write(void *arg); |
| 337 | |
Ronnie Sahlberg | c589b24 | 2011-10-25 19:24:24 +1100 | [diff] [blame] | 338 | static void |
| 339 | iscsi_set_events(IscsiLun *iscsilun) |
| 340 | { |
| 341 | struct iscsi_context *iscsi = iscsilun->iscsi; |
Peter Lieven | 05b685f | 2015-04-07 22:08:15 +0200 | [diff] [blame] | 342 | int ev = iscsi_which_events(iscsi); |
Ronnie Sahlberg | c589b24 | 2011-10-25 19:24:24 +1100 | [diff] [blame] | 343 | |
Ronnie Sahlberg | c9b9f68 | 2012-05-22 19:56:36 +1000 | [diff] [blame] | 344 | if (ev != iscsilun->events) { |
Fam Zheng | dca21ef | 2015-10-23 11:08:05 +0800 | [diff] [blame] | 345 | aio_set_fd_handler(iscsilun->aio_context, iscsi_get_fd(iscsi), |
| 346 | false, |
Peter Lieven | 05b685f | 2015-04-07 22:08:15 +0200 | [diff] [blame] | 347 | (ev & POLLIN) ? iscsi_process_read : NULL, |
Stefan Hajnoczi | 80cf625 | 2014-05-08 16:34:42 +0200 | [diff] [blame] | 348 | (ev & POLLOUT) ? iscsi_process_write : NULL, |
| 349 | iscsilun); |
Peter Lieven | 05b685f | 2015-04-07 22:08:15 +0200 | [diff] [blame] | 350 | iscsilun->events = ev; |
Ronnie Sahlberg | c9b9f68 | 2012-05-22 19:56:36 +1000 | [diff] [blame] | 351 | } |
Peter Lieven | 05b685f | 2015-04-07 22:08:15 +0200 | [diff] [blame] | 352 | } |
| 353 | |
Peter Lieven | 5dd7a53 | 2015-06-16 13:45:07 +0200 | [diff] [blame] | 354 | static void iscsi_timed_check_events(void *opaque) |
Peter Lieven | 05b685f | 2015-04-07 22:08:15 +0200 | [diff] [blame] | 355 | { |
| 356 | IscsiLun *iscsilun = opaque; |
Peter Lieven | 5dd7a53 | 2015-06-16 13:45:07 +0200 | [diff] [blame] | 357 | |
| 358 | /* check for timed out requests */ |
| 359 | iscsi_service(iscsilun->iscsi, 0); |
| 360 | |
| 361 | if (iscsilun->request_timed_out) { |
| 362 | iscsilun->request_timed_out = false; |
| 363 | iscsi_reconnect(iscsilun->iscsi); |
| 364 | } |
| 365 | |
| 366 | /* newer versions of libiscsi may return zero events. Ensure we are able |
| 367 | * to return to service once this situation changes. */ |
Peter Lieven | 05b685f | 2015-04-07 22:08:15 +0200 | [diff] [blame] | 368 | iscsi_set_events(iscsilun); |
Peter Lieven | 5dd7a53 | 2015-06-16 13:45:07 +0200 | [diff] [blame] | 369 | |
| 370 | timer_mod(iscsilun->event_timer, |
| 371 | qemu_clock_get_ms(QEMU_CLOCK_REALTIME) + EVENT_INTERVAL); |
Ronnie Sahlberg | c589b24 | 2011-10-25 19:24:24 +1100 | [diff] [blame] | 372 | } |
| 373 | |
| 374 | static void |
| 375 | iscsi_process_read(void *arg) |
| 376 | { |
| 377 | IscsiLun *iscsilun = arg; |
| 378 | struct iscsi_context *iscsi = iscsilun->iscsi; |
| 379 | |
| 380 | iscsi_service(iscsi, POLLIN); |
| 381 | iscsi_set_events(iscsilun); |
| 382 | } |
| 383 | |
| 384 | static void |
| 385 | iscsi_process_write(void *arg) |
| 386 | { |
| 387 | IscsiLun *iscsilun = arg; |
| 388 | struct iscsi_context *iscsi = iscsilun->iscsi; |
| 389 | |
| 390 | iscsi_service(iscsi, POLLOUT); |
| 391 | iscsi_set_events(iscsilun); |
| 392 | } |
| 393 | |
Peter Lieven | 0777b5d | 2013-07-11 14:16:25 +0200 | [diff] [blame] | 394 | static int64_t sector_lun2qemu(int64_t sector, IscsiLun *iscsilun) |
| 395 | { |
| 396 | return sector * iscsilun->block_size / BDRV_SECTOR_SIZE; |
| 397 | } |
| 398 | |
Ronnie Sahlberg | c589b24 | 2011-10-25 19:24:24 +1100 | [diff] [blame] | 399 | static int64_t sector_qemu2lun(int64_t sector, IscsiLun *iscsilun) |
| 400 | { |
| 401 | return sector * BDRV_SECTOR_SIZE / iscsilun->block_size; |
| 402 | } |
| 403 | |
Peter Lieven | 91bea4e | 2013-07-11 14:16:27 +0200 | [diff] [blame] | 404 | static bool is_request_lun_aligned(int64_t sector_num, int nb_sectors, |
| 405 | IscsiLun *iscsilun) |
| 406 | { |
| 407 | if ((sector_num * BDRV_SECTOR_SIZE) % iscsilun->block_size || |
| 408 | (nb_sectors * BDRV_SECTOR_SIZE) % iscsilun->block_size) { |
Richard W.M. Jones | f507522 | 2013-07-31 22:20:26 +0100 | [diff] [blame] | 409 | error_report("iSCSI misaligned request: " |
| 410 | "iscsilun->block_size %u, sector_num %" PRIi64 |
| 411 | ", nb_sectors %d", |
Peter Lieven | 91bea4e | 2013-07-11 14:16:27 +0200 | [diff] [blame] | 412 | iscsilun->block_size, sector_num, nb_sectors); |
| 413 | return 0; |
| 414 | } |
| 415 | return 1; |
| 416 | } |
| 417 | |
Peter Lieven | a9fe4c9 | 2014-09-30 09:09:12 +0200 | [diff] [blame] | 418 | static unsigned long *iscsi_allocationmap_init(IscsiLun *iscsilun) |
| 419 | { |
| 420 | return bitmap_try_new(DIV_ROUND_UP(sector_lun2qemu(iscsilun->num_blocks, |
| 421 | iscsilun), |
| 422 | iscsilun->cluster_sectors)); |
| 423 | } |
| 424 | |
Peter Lieven | b03c380 | 2014-04-28 13:11:32 +0200 | [diff] [blame] | 425 | static void iscsi_allocationmap_set(IscsiLun *iscsilun, int64_t sector_num, |
| 426 | int nb_sectors) |
| 427 | { |
| 428 | if (iscsilun->allocationmap == NULL) { |
| 429 | return; |
| 430 | } |
| 431 | bitmap_set(iscsilun->allocationmap, |
| 432 | sector_num / iscsilun->cluster_sectors, |
| 433 | DIV_ROUND_UP(nb_sectors, iscsilun->cluster_sectors)); |
| 434 | } |
| 435 | |
| 436 | static void iscsi_allocationmap_clear(IscsiLun *iscsilun, int64_t sector_num, |
| 437 | int nb_sectors) |
| 438 | { |
| 439 | int64_t cluster_num, nb_clusters; |
| 440 | if (iscsilun->allocationmap == NULL) { |
| 441 | return; |
| 442 | } |
| 443 | cluster_num = DIV_ROUND_UP(sector_num, iscsilun->cluster_sectors); |
| 444 | nb_clusters = (sector_num + nb_sectors) / iscsilun->cluster_sectors |
| 445 | - cluster_num; |
| 446 | if (nb_clusters > 0) { |
| 447 | bitmap_clear(iscsilun->allocationmap, cluster_num, nb_clusters); |
| 448 | } |
| 449 | } |
| 450 | |
Kevin Wolf | 9f0eb9e | 2016-03-10 13:55:50 +0100 | [diff] [blame] | 451 | static int coroutine_fn |
| 452 | iscsi_co_writev_flags(BlockDriverState *bs, int64_t sector_num, int nb_sectors, |
| 453 | QEMUIOVector *iov, int flags) |
Ronnie Sahlberg | c589b24 | 2011-10-25 19:24:24 +1100 | [diff] [blame] | 454 | { |
Peter Lieven | 063c337 | 2013-12-05 16:47:17 +0100 | [diff] [blame] | 455 | IscsiLun *iscsilun = bs->opaque; |
| 456 | struct IscsiTask iTask; |
Ronnie Sahlberg | f4dfa67 | 2012-05-22 20:10:05 +1000 | [diff] [blame] | 457 | uint64_t lba; |
Peter Lieven | 063c337 | 2013-12-05 16:47:17 +0100 | [diff] [blame] | 458 | uint32_t num_sectors; |
Eric Blake | 4df863f | 2016-05-03 16:39:06 -0600 | [diff] [blame] | 459 | bool fua = flags & BDRV_REQ_FUA; |
Peter Lieven | 063c337 | 2013-12-05 16:47:17 +0100 | [diff] [blame] | 460 | |
Eric Blake | 4df863f | 2016-05-03 16:39:06 -0600 | [diff] [blame] | 461 | if (fua) { |
| 462 | assert(iscsilun->dpofua); |
| 463 | } |
Peter Lieven | 063c337 | 2013-12-05 16:47:17 +0100 | [diff] [blame] | 464 | if (!is_request_lun_aligned(sector_num, nb_sectors, iscsilun)) { |
| 465 | return -EINVAL; |
| 466 | } |
| 467 | |
Peter Lieven | dc9e716 | 2014-10-27 10:18:48 +0100 | [diff] [blame] | 468 | if (bs->bl.max_transfer_length && nb_sectors > bs->bl.max_transfer_length) { |
| 469 | error_report("iSCSI Error: Write of %d sectors exceeds max_xfer_len " |
| 470 | "of %d sectors", nb_sectors, bs->bl.max_transfer_length); |
| 471 | return -EINVAL; |
| 472 | } |
| 473 | |
Peter Lieven | 063c337 | 2013-12-05 16:47:17 +0100 | [diff] [blame] | 474 | lba = sector_qemu2lun(sector_num, iscsilun); |
| 475 | num_sectors = sector_qemu2lun(nb_sectors, iscsilun); |
Peter Lieven | 063c337 | 2013-12-05 16:47:17 +0100 | [diff] [blame] | 476 | iscsi_co_init_iscsitask(iscsilun, &iTask); |
| 477 | retry: |
Peter Lieven | 9281fe9 | 2014-06-04 15:47:39 +0200 | [diff] [blame] | 478 | if (iscsilun->use_16_for_rw) { |
| 479 | iTask.task = iscsi_write16_task(iscsilun->iscsi, iscsilun->lun, lba, |
Peter Lieven | 8c215a9 | 2014-06-18 18:40:22 +0200 | [diff] [blame] | 480 | NULL, num_sectors * iscsilun->block_size, |
Peter Lieven | 73b5394 | 2015-04-16 16:08:29 +0200 | [diff] [blame] | 481 | iscsilun->block_size, 0, 0, fua, 0, 0, |
Peter Lieven | 9281fe9 | 2014-06-04 15:47:39 +0200 | [diff] [blame] | 482 | iscsi_co_generic_cb, &iTask); |
| 483 | } else { |
| 484 | iTask.task = iscsi_write10_task(iscsilun->iscsi, iscsilun->lun, lba, |
Peter Lieven | 8c215a9 | 2014-06-18 18:40:22 +0200 | [diff] [blame] | 485 | NULL, num_sectors * iscsilun->block_size, |
Peter Lieven | 73b5394 | 2015-04-16 16:08:29 +0200 | [diff] [blame] | 486 | iscsilun->block_size, 0, 0, fua, 0, 0, |
Peter Lieven | 9281fe9 | 2014-06-04 15:47:39 +0200 | [diff] [blame] | 487 | iscsi_co_generic_cb, &iTask); |
| 488 | } |
Peter Lieven | 063c337 | 2013-12-05 16:47:17 +0100 | [diff] [blame] | 489 | if (iTask.task == NULL) { |
Peter Lieven | 9239711 | 2013-12-20 10:02:47 +0100 | [diff] [blame] | 490 | return -ENOMEM; |
Ronnie Sahlberg | f4dfa67 | 2012-05-22 20:10:05 +1000 | [diff] [blame] | 491 | } |
Peter Lieven | 063c337 | 2013-12-05 16:47:17 +0100 | [diff] [blame] | 492 | scsi_task_set_iov_out(iTask.task, (struct scsi_iovec *) iov->iov, |
| 493 | iov->niov); |
Peter Lieven | 063c337 | 2013-12-05 16:47:17 +0100 | [diff] [blame] | 494 | while (!iTask.complete) { |
| 495 | iscsi_set_events(iscsilun); |
| 496 | qemu_coroutine_yield(); |
Ronnie Sahlberg | c589b24 | 2011-10-25 19:24:24 +1100 | [diff] [blame] | 497 | } |
| 498 | |
Peter Lieven | 063c337 | 2013-12-05 16:47:17 +0100 | [diff] [blame] | 499 | if (iTask.task != NULL) { |
| 500 | scsi_free_scsi_task(iTask.task); |
| 501 | iTask.task = NULL; |
| 502 | } |
| 503 | |
| 504 | if (iTask.do_retry) { |
Peter Lieven | 837c390 | 2014-02-18 13:08:39 +0100 | [diff] [blame] | 505 | iTask.complete = 0; |
Peter Lieven | 063c337 | 2013-12-05 16:47:17 +0100 | [diff] [blame] | 506 | goto retry; |
| 507 | } |
| 508 | |
Peter Lieven | 063c337 | 2013-12-05 16:47:17 +0100 | [diff] [blame] | 509 | if (iTask.status != SCSI_STATUS_GOOD) { |
Fam Zheng | e01dd3d | 2015-11-05 13:00:09 +0800 | [diff] [blame] | 510 | return iTask.err_code; |
Peter Lieven | 063c337 | 2013-12-05 16:47:17 +0100 | [diff] [blame] | 511 | } |
Peter Lieven | 7371d56 | 2012-12-03 20:35:15 +0100 | [diff] [blame] | 512 | |
Peter Lieven | b03c380 | 2014-04-28 13:11:32 +0200 | [diff] [blame] | 513 | iscsi_allocationmap_set(iscsilun, sector_num, nb_sectors); |
| 514 | |
Peter Lieven | 1dde716 | 2013-02-21 16:15:54 +0100 | [diff] [blame] | 515 | return 0; |
| 516 | } |
Ronnie Sahlberg | c589b24 | 2011-10-25 19:24:24 +1100 | [diff] [blame] | 517 | |
Peter Lieven | b03c380 | 2014-04-28 13:11:32 +0200 | [diff] [blame] | 518 | |
| 519 | static bool iscsi_allocationmap_is_allocated(IscsiLun *iscsilun, |
| 520 | int64_t sector_num, int nb_sectors) |
| 521 | { |
| 522 | unsigned long size; |
| 523 | if (iscsilun->allocationmap == NULL) { |
| 524 | return true; |
| 525 | } |
| 526 | size = DIV_ROUND_UP(sector_num + nb_sectors, iscsilun->cluster_sectors); |
| 527 | return !(find_next_bit(iscsilun->allocationmap, size, |
| 528 | sector_num / iscsilun->cluster_sectors) == size); |
| 529 | } |
| 530 | |
Peter Lieven | b03c380 | 2014-04-28 13:11:32 +0200 | [diff] [blame] | 531 | static int64_t coroutine_fn iscsi_co_get_block_status(BlockDriverState *bs, |
| 532 | int64_t sector_num, |
Fam Zheng | 67a0fd2 | 2016-01-26 11:58:48 +0800 | [diff] [blame] | 533 | int nb_sectors, int *pnum, |
| 534 | BlockDriverState **file) |
Peter Lieven | b03c380 | 2014-04-28 13:11:32 +0200 | [diff] [blame] | 535 | { |
| 536 | IscsiLun *iscsilun = bs->opaque; |
| 537 | struct scsi_get_lba_status *lbas = NULL; |
| 538 | struct scsi_lba_status_descriptor *lbasd = NULL; |
| 539 | struct IscsiTask iTask; |
| 540 | int64_t ret; |
| 541 | |
| 542 | iscsi_co_init_iscsitask(iscsilun, &iTask); |
| 543 | |
| 544 | if (!is_request_lun_aligned(sector_num, nb_sectors, iscsilun)) { |
| 545 | ret = -EINVAL; |
| 546 | goto out; |
| 547 | } |
| 548 | |
| 549 | /* default to all sectors allocated */ |
| 550 | ret = BDRV_BLOCK_DATA; |
| 551 | ret |= (sector_num << BDRV_SECTOR_BITS) | BDRV_BLOCK_OFFSET_VALID; |
| 552 | *pnum = nb_sectors; |
| 553 | |
| 554 | /* LUN does not support logical block provisioning */ |
Peter Lieven | 0a386e4 | 2015-04-16 16:08:26 +0200 | [diff] [blame] | 555 | if (!iscsilun->lbpme) { |
Peter Lieven | b03c380 | 2014-04-28 13:11:32 +0200 | [diff] [blame] | 556 | goto out; |
| 557 | } |
| 558 | |
| 559 | retry: |
| 560 | if (iscsi_get_lba_status_task(iscsilun->iscsi, iscsilun->lun, |
| 561 | sector_qemu2lun(sector_num, iscsilun), |
| 562 | 8 + 16, iscsi_co_generic_cb, |
| 563 | &iTask) == NULL) { |
| 564 | ret = -ENOMEM; |
| 565 | goto out; |
| 566 | } |
| 567 | |
| 568 | while (!iTask.complete) { |
| 569 | iscsi_set_events(iscsilun); |
| 570 | qemu_coroutine_yield(); |
| 571 | } |
| 572 | |
| 573 | if (iTask.do_retry) { |
| 574 | if (iTask.task != NULL) { |
| 575 | scsi_free_scsi_task(iTask.task); |
| 576 | iTask.task = NULL; |
| 577 | } |
| 578 | iTask.complete = 0; |
| 579 | goto retry; |
| 580 | } |
| 581 | |
| 582 | if (iTask.status != SCSI_STATUS_GOOD) { |
| 583 | /* in case the get_lba_status_callout fails (i.e. |
| 584 | * because the device is busy or the cmd is not |
| 585 | * supported) we pretend all blocks are allocated |
| 586 | * for backwards compatibility */ |
| 587 | goto out; |
| 588 | } |
| 589 | |
| 590 | lbas = scsi_datain_unmarshall(iTask.task); |
| 591 | if (lbas == NULL) { |
| 592 | ret = -EIO; |
| 593 | goto out; |
| 594 | } |
| 595 | |
| 596 | lbasd = &lbas->descriptors[0]; |
| 597 | |
| 598 | if (sector_qemu2lun(sector_num, iscsilun) != lbasd->lba) { |
| 599 | ret = -EIO; |
| 600 | goto out; |
| 601 | } |
| 602 | |
| 603 | *pnum = sector_lun2qemu(lbasd->num_blocks, iscsilun); |
| 604 | |
| 605 | if (lbasd->provisioning == SCSI_PROVISIONING_TYPE_DEALLOCATED || |
| 606 | lbasd->provisioning == SCSI_PROVISIONING_TYPE_ANCHORED) { |
| 607 | ret &= ~BDRV_BLOCK_DATA; |
| 608 | if (iscsilun->lbprz) { |
| 609 | ret |= BDRV_BLOCK_ZERO; |
| 610 | } |
| 611 | } |
| 612 | |
| 613 | if (ret & BDRV_BLOCK_ZERO) { |
| 614 | iscsi_allocationmap_clear(iscsilun, sector_num, *pnum); |
| 615 | } else { |
| 616 | iscsi_allocationmap_set(iscsilun, sector_num, *pnum); |
| 617 | } |
| 618 | |
| 619 | if (*pnum > nb_sectors) { |
| 620 | *pnum = nb_sectors; |
| 621 | } |
| 622 | out: |
| 623 | if (iTask.task != NULL) { |
| 624 | scsi_free_scsi_task(iTask.task); |
| 625 | } |
Fam Zheng | 3399833 | 2016-01-26 11:58:52 +0800 | [diff] [blame] | 626 | if (ret > 0 && ret & BDRV_BLOCK_OFFSET_VALID) { |
| 627 | *file = bs; |
| 628 | } |
Peter Lieven | b03c380 | 2014-04-28 13:11:32 +0200 | [diff] [blame] | 629 | return ret; |
| 630 | } |
| 631 | |
Peter Lieven | 063c337 | 2013-12-05 16:47:17 +0100 | [diff] [blame] | 632 | static int coroutine_fn iscsi_co_readv(BlockDriverState *bs, |
| 633 | int64_t sector_num, int nb_sectors, |
| 634 | QEMUIOVector *iov) |
Peter Lieven | 1dde716 | 2013-02-21 16:15:54 +0100 | [diff] [blame] | 635 | { |
| 636 | IscsiLun *iscsilun = bs->opaque; |
Peter Lieven | 063c337 | 2013-12-05 16:47:17 +0100 | [diff] [blame] | 637 | struct IscsiTask iTask; |
Peter Lieven | 1dde716 | 2013-02-21 16:15:54 +0100 | [diff] [blame] | 638 | uint64_t lba; |
| 639 | uint32_t num_sectors; |
Ronnie Sahlberg | c589b24 | 2011-10-25 19:24:24 +1100 | [diff] [blame] | 640 | |
Peter Lieven | 063c337 | 2013-12-05 16:47:17 +0100 | [diff] [blame] | 641 | if (!is_request_lun_aligned(sector_num, nb_sectors, iscsilun)) { |
| 642 | return -EINVAL; |
Ronnie Sahlberg | f4dfa67 | 2012-05-22 20:10:05 +1000 | [diff] [blame] | 643 | } |
Ronnie Sahlberg | f4dfa67 | 2012-05-22 20:10:05 +1000 | [diff] [blame] | 644 | |
Peter Lieven | dc9e716 | 2014-10-27 10:18:48 +0100 | [diff] [blame] | 645 | if (bs->bl.max_transfer_length && nb_sectors > bs->bl.max_transfer_length) { |
| 646 | error_report("iSCSI Error: Read of %d sectors exceeds max_xfer_len " |
| 647 | "of %d sectors", nb_sectors, bs->bl.max_transfer_length); |
| 648 | return -EINVAL; |
| 649 | } |
| 650 | |
Peter Lieven | 5917af8 | 2014-04-28 17:11:33 +0200 | [diff] [blame] | 651 | if (iscsilun->lbprz && nb_sectors >= ISCSI_CHECKALLOC_THRES && |
Peter Lieven | b03c380 | 2014-04-28 13:11:32 +0200 | [diff] [blame] | 652 | !iscsi_allocationmap_is_allocated(iscsilun, sector_num, nb_sectors)) { |
| 653 | int64_t ret; |
| 654 | int pnum; |
Fam Zheng | 67a0fd2 | 2016-01-26 11:58:48 +0800 | [diff] [blame] | 655 | BlockDriverState *file; |
| 656 | ret = iscsi_co_get_block_status(bs, sector_num, INT_MAX, &pnum, &file); |
Peter Lieven | b03c380 | 2014-04-28 13:11:32 +0200 | [diff] [blame] | 657 | if (ret < 0) { |
| 658 | return ret; |
| 659 | } |
| 660 | if (ret & BDRV_BLOCK_ZERO && pnum >= nb_sectors) { |
| 661 | qemu_iovec_memset(iov, 0, 0x00, iov->size); |
| 662 | return 0; |
| 663 | } |
| 664 | } |
Peter Lieven | b03c380 | 2014-04-28 13:11:32 +0200 | [diff] [blame] | 665 | |
Peter Lieven | 063c337 | 2013-12-05 16:47:17 +0100 | [diff] [blame] | 666 | lba = sector_qemu2lun(sector_num, iscsilun); |
| 667 | num_sectors = sector_qemu2lun(nb_sectors, iscsilun); |
Ronnie Sahlberg | f4dfa67 | 2012-05-22 20:10:05 +1000 | [diff] [blame] | 668 | |
Peter Lieven | 063c337 | 2013-12-05 16:47:17 +0100 | [diff] [blame] | 669 | iscsi_co_init_iscsitask(iscsilun, &iTask); |
| 670 | retry: |
Peter Lieven | 9281fe9 | 2014-06-04 15:47:39 +0200 | [diff] [blame] | 671 | if (iscsilun->use_16_for_rw) { |
Peter Lieven | 063c337 | 2013-12-05 16:47:17 +0100 | [diff] [blame] | 672 | iTask.task = iscsi_read16_task(iscsilun->iscsi, iscsilun->lun, lba, |
| 673 | num_sectors * iscsilun->block_size, |
| 674 | iscsilun->block_size, 0, 0, 0, 0, 0, |
| 675 | iscsi_co_generic_cb, &iTask); |
Peter Lieven | 9281fe9 | 2014-06-04 15:47:39 +0200 | [diff] [blame] | 676 | } else { |
Peter Lieven | 063c337 | 2013-12-05 16:47:17 +0100 | [diff] [blame] | 677 | iTask.task = iscsi_read10_task(iscsilun->iscsi, iscsilun->lun, lba, |
| 678 | num_sectors * iscsilun->block_size, |
Stefan Weil | 219c252 | 2013-12-17 08:57:10 +0100 | [diff] [blame] | 679 | iscsilun->block_size, |
Stefan Weil | 219c252 | 2013-12-17 08:57:10 +0100 | [diff] [blame] | 680 | 0, 0, 0, 0, 0, |
Peter Lieven | 063c337 | 2013-12-05 16:47:17 +0100 | [diff] [blame] | 681 | iscsi_co_generic_cb, &iTask); |
Ronnie Sahlberg | f4dfa67 | 2012-05-22 20:10:05 +1000 | [diff] [blame] | 682 | } |
Peter Lieven | 063c337 | 2013-12-05 16:47:17 +0100 | [diff] [blame] | 683 | if (iTask.task == NULL) { |
Peter Lieven | 9239711 | 2013-12-20 10:02:47 +0100 | [diff] [blame] | 684 | return -ENOMEM; |
Ronnie Sahlberg | c589b24 | 2011-10-25 19:24:24 +1100 | [diff] [blame] | 685 | } |
Peter Lieven | 063c337 | 2013-12-05 16:47:17 +0100 | [diff] [blame] | 686 | scsi_task_set_iov_in(iTask.task, (struct scsi_iovec *) iov->iov, iov->niov); |
Peter Lieven | 1dde716 | 2013-02-21 16:15:54 +0100 | [diff] [blame] | 687 | |
Peter Lieven | 063c337 | 2013-12-05 16:47:17 +0100 | [diff] [blame] | 688 | while (!iTask.complete) { |
| 689 | iscsi_set_events(iscsilun); |
| 690 | qemu_coroutine_yield(); |
Peter Lieven | 91bea4e | 2013-07-11 14:16:27 +0200 | [diff] [blame] | 691 | } |
| 692 | |
Peter Lieven | 063c337 | 2013-12-05 16:47:17 +0100 | [diff] [blame] | 693 | if (iTask.task != NULL) { |
| 694 | scsi_free_scsi_task(iTask.task); |
| 695 | iTask.task = NULL; |
Peter Lieven | 1dde716 | 2013-02-21 16:15:54 +0100 | [diff] [blame] | 696 | } |
Ronnie Sahlberg | c589b24 | 2011-10-25 19:24:24 +1100 | [diff] [blame] | 697 | |
Peter Lieven | 063c337 | 2013-12-05 16:47:17 +0100 | [diff] [blame] | 698 | if (iTask.do_retry) { |
Peter Lieven | 837c390 | 2014-02-18 13:08:39 +0100 | [diff] [blame] | 699 | iTask.complete = 0; |
Peter Lieven | 063c337 | 2013-12-05 16:47:17 +0100 | [diff] [blame] | 700 | goto retry; |
Ronnie Sahlberg | c589b24 | 2011-10-25 19:24:24 +1100 | [diff] [blame] | 701 | } |
| 702 | |
Peter Lieven | 063c337 | 2013-12-05 16:47:17 +0100 | [diff] [blame] | 703 | if (iTask.status != SCSI_STATUS_GOOD) { |
Fam Zheng | e01dd3d | 2015-11-05 13:00:09 +0800 | [diff] [blame] | 704 | return iTask.err_code; |
Peter Lieven | 1dde716 | 2013-02-21 16:15:54 +0100 | [diff] [blame] | 705 | } |
| 706 | |
| 707 | return 0; |
| 708 | } |
| 709 | |
Peter Lieven | 063c337 | 2013-12-05 16:47:17 +0100 | [diff] [blame] | 710 | static int coroutine_fn iscsi_co_flush(BlockDriverState *bs) |
Peter Lieven | 1dde716 | 2013-02-21 16:15:54 +0100 | [diff] [blame] | 711 | { |
| 712 | IscsiLun *iscsilun = bs->opaque; |
Peter Lieven | 063c337 | 2013-12-05 16:47:17 +0100 | [diff] [blame] | 713 | struct IscsiTask iTask; |
Peter Lieven | 1dde716 | 2013-02-21 16:15:54 +0100 | [diff] [blame] | 714 | |
Peter Lieven | 73b5394 | 2015-04-16 16:08:29 +0200 | [diff] [blame] | 715 | iscsi_co_init_iscsitask(iscsilun, &iTask); |
Peter Lieven | 063c337 | 2013-12-05 16:47:17 +0100 | [diff] [blame] | 716 | retry: |
| 717 | if (iscsi_synchronizecache10_task(iscsilun->iscsi, iscsilun->lun, 0, 0, 0, |
| 718 | 0, iscsi_co_generic_cb, &iTask) == NULL) { |
Peter Lieven | 9239711 | 2013-12-20 10:02:47 +0100 | [diff] [blame] | 719 | return -ENOMEM; |
Ronnie Sahlberg | c589b24 | 2011-10-25 19:24:24 +1100 | [diff] [blame] | 720 | } |
| 721 | |
Peter Lieven | 063c337 | 2013-12-05 16:47:17 +0100 | [diff] [blame] | 722 | while (!iTask.complete) { |
| 723 | iscsi_set_events(iscsilun); |
| 724 | qemu_coroutine_yield(); |
| 725 | } |
Ronnie Sahlberg | c589b24 | 2011-10-25 19:24:24 +1100 | [diff] [blame] | 726 | |
Peter Lieven | 063c337 | 2013-12-05 16:47:17 +0100 | [diff] [blame] | 727 | if (iTask.task != NULL) { |
| 728 | scsi_free_scsi_task(iTask.task); |
| 729 | iTask.task = NULL; |
| 730 | } |
| 731 | |
| 732 | if (iTask.do_retry) { |
Peter Lieven | 837c390 | 2014-02-18 13:08:39 +0100 | [diff] [blame] | 733 | iTask.complete = 0; |
Peter Lieven | 063c337 | 2013-12-05 16:47:17 +0100 | [diff] [blame] | 734 | goto retry; |
| 735 | } |
| 736 | |
| 737 | if (iTask.status != SCSI_STATUS_GOOD) { |
Fam Zheng | e01dd3d | 2015-11-05 13:00:09 +0800 | [diff] [blame] | 738 | return iTask.err_code; |
Peter Lieven | 063c337 | 2013-12-05 16:47:17 +0100 | [diff] [blame] | 739 | } |
| 740 | |
| 741 | return 0; |
Ronnie Sahlberg | c589b24 | 2011-10-25 19:24:24 +1100 | [diff] [blame] | 742 | } |
| 743 | |
Ronnie Sahlberg | 9839245 | 2012-05-25 21:59:01 +1000 | [diff] [blame] | 744 | #ifdef __linux__ |
| 745 | static void |
| 746 | iscsi_aio_ioctl_cb(struct iscsi_context *iscsi, int status, |
| 747 | void *command_data, void *opaque) |
| 748 | { |
| 749 | IscsiAIOCB *acb = opaque; |
| 750 | |
Ronnie Sahlberg | 0a53f01 | 2013-06-23 08:07:08 -0700 | [diff] [blame] | 751 | g_free(acb->buf); |
| 752 | acb->buf = NULL; |
| 753 | |
Ronnie Sahlberg | 9839245 | 2012-05-25 21:59:01 +1000 | [diff] [blame] | 754 | acb->status = 0; |
| 755 | if (status < 0) { |
| 756 | error_report("Failed to ioctl(SG_IO) to iSCSI lun. %s", |
| 757 | iscsi_get_error(iscsi)); |
Fam Zheng | e01dd3d | 2015-11-05 13:00:09 +0800 | [diff] [blame] | 758 | acb->status = iscsi_translate_sense(&acb->task->sense); |
Ronnie Sahlberg | 9839245 | 2012-05-25 21:59:01 +1000 | [diff] [blame] | 759 | } |
| 760 | |
| 761 | acb->ioh->driver_status = 0; |
| 762 | acb->ioh->host_status = 0; |
| 763 | acb->ioh->resid = 0; |
Vadim Rozenfeld | 644c6869d | 2016-05-13 13:03:22 +0200 | [diff] [blame] | 764 | acb->ioh->status = status; |
Ronnie Sahlberg | 9839245 | 2012-05-25 21:59:01 +1000 | [diff] [blame] | 765 | |
| 766 | #define SG_ERR_DRIVER_SENSE 0x08 |
| 767 | |
| 768 | if (status == SCSI_STATUS_CHECK_CONDITION && acb->task->datain.size >= 2) { |
| 769 | int ss; |
| 770 | |
| 771 | acb->ioh->driver_status |= SG_ERR_DRIVER_SENSE; |
| 772 | |
| 773 | acb->ioh->sb_len_wr = acb->task->datain.size - 2; |
| 774 | ss = (acb->ioh->mx_sb_len >= acb->ioh->sb_len_wr) ? |
| 775 | acb->ioh->mx_sb_len : acb->ioh->sb_len_wr; |
| 776 | memcpy(acb->ioh->sbp, &acb->task->datain.data[2], ss); |
| 777 | } |
| 778 | |
Paolo Bonzini | cfb3f50 | 2012-08-18 23:38:03 +0200 | [diff] [blame] | 779 | iscsi_schedule_bh(acb); |
Ronnie Sahlberg | 9839245 | 2012-05-25 21:59:01 +1000 | [diff] [blame] | 780 | } |
| 781 | |
Fam Zheng | 4bb17ab | 2015-11-09 18:16:49 +0800 | [diff] [blame] | 782 | static void iscsi_ioctl_bh_completion(void *opaque) |
| 783 | { |
| 784 | IscsiAIOCB *acb = opaque; |
| 785 | |
| 786 | qemu_bh_delete(acb->bh); |
| 787 | acb->common.cb(acb->common.opaque, acb->ret); |
| 788 | qemu_aio_unref(acb); |
| 789 | } |
| 790 | |
| 791 | static void iscsi_ioctl_handle_emulated(IscsiAIOCB *acb, int req, void *buf) |
| 792 | { |
| 793 | BlockDriverState *bs = acb->common.bs; |
| 794 | IscsiLun *iscsilun = bs->opaque; |
| 795 | int ret = 0; |
| 796 | |
| 797 | switch (req) { |
| 798 | case SG_GET_VERSION_NUM: |
| 799 | *(int *)buf = 30000; |
| 800 | break; |
| 801 | case SG_GET_SCSI_ID: |
| 802 | ((struct sg_scsi_id *)buf)->scsi_type = iscsilun->type; |
| 803 | break; |
| 804 | default: |
| 805 | ret = -EINVAL; |
| 806 | } |
| 807 | assert(!acb->bh); |
| 808 | acb->bh = aio_bh_new(bdrv_get_aio_context(bs), |
| 809 | iscsi_ioctl_bh_completion, acb); |
| 810 | acb->ret = ret; |
| 811 | qemu_bh_schedule(acb->bh); |
| 812 | } |
| 813 | |
Markus Armbruster | 7c84b1b | 2014-10-07 13:59:14 +0200 | [diff] [blame] | 814 | static BlockAIOCB *iscsi_aio_ioctl(BlockDriverState *bs, |
Ronnie Sahlberg | 9839245 | 2012-05-25 21:59:01 +1000 | [diff] [blame] | 815 | unsigned long int req, void *buf, |
Markus Armbruster | 097310b | 2014-10-07 13:59:15 +0200 | [diff] [blame] | 816 | BlockCompletionFunc *cb, void *opaque) |
Ronnie Sahlberg | 9839245 | 2012-05-25 21:59:01 +1000 | [diff] [blame] | 817 | { |
| 818 | IscsiLun *iscsilun = bs->opaque; |
| 819 | struct iscsi_context *iscsi = iscsilun->iscsi; |
| 820 | struct iscsi_data data; |
| 821 | IscsiAIOCB *acb; |
| 822 | |
Stefan Hajnoczi | d7331be | 2012-10-31 16:34:37 +0100 | [diff] [blame] | 823 | acb = qemu_aio_get(&iscsi_aiocb_info, bs, cb, opaque); |
Ronnie Sahlberg | 9839245 | 2012-05-25 21:59:01 +1000 | [diff] [blame] | 824 | |
| 825 | acb->iscsilun = iscsilun; |
Paolo Bonzini | 1bd075f | 2012-08-18 23:35:49 +0200 | [diff] [blame] | 826 | acb->bh = NULL; |
| 827 | acb->status = -EINPROGRESS; |
Ronnie Sahlberg | 9839245 | 2012-05-25 21:59:01 +1000 | [diff] [blame] | 828 | acb->buf = NULL; |
| 829 | acb->ioh = buf; |
| 830 | |
Fam Zheng | 4bb17ab | 2015-11-09 18:16:49 +0800 | [diff] [blame] | 831 | if (req != SG_IO) { |
| 832 | iscsi_ioctl_handle_emulated(acb, req, buf); |
| 833 | return &acb->common; |
| 834 | } |
| 835 | |
Ronnie Sahlberg | 9839245 | 2012-05-25 21:59:01 +1000 | [diff] [blame] | 836 | acb->task = malloc(sizeof(struct scsi_task)); |
| 837 | if (acb->task == NULL) { |
| 838 | error_report("iSCSI: Failed to allocate task for scsi command. %s", |
| 839 | iscsi_get_error(iscsi)); |
Fam Zheng | 8007429 | 2014-09-11 13:41:28 +0800 | [diff] [blame] | 840 | qemu_aio_unref(acb); |
Ronnie Sahlberg | 9839245 | 2012-05-25 21:59:01 +1000 | [diff] [blame] | 841 | return NULL; |
| 842 | } |
| 843 | memset(acb->task, 0, sizeof(struct scsi_task)); |
| 844 | |
| 845 | switch (acb->ioh->dxfer_direction) { |
| 846 | case SG_DXFER_TO_DEV: |
| 847 | acb->task->xfer_dir = SCSI_XFER_WRITE; |
| 848 | break; |
| 849 | case SG_DXFER_FROM_DEV: |
| 850 | acb->task->xfer_dir = SCSI_XFER_READ; |
| 851 | break; |
| 852 | default: |
| 853 | acb->task->xfer_dir = SCSI_XFER_NONE; |
| 854 | break; |
| 855 | } |
| 856 | |
| 857 | acb->task->cdb_size = acb->ioh->cmd_len; |
| 858 | memcpy(&acb->task->cdb[0], acb->ioh->cmdp, acb->ioh->cmd_len); |
| 859 | acb->task->expxferlen = acb->ioh->dxfer_len; |
| 860 | |
Ronnie Sahlberg | 0a53f01 | 2013-06-23 08:07:08 -0700 | [diff] [blame] | 861 | data.size = 0; |
Ronnie Sahlberg | 9839245 | 2012-05-25 21:59:01 +1000 | [diff] [blame] | 862 | if (acb->task->xfer_dir == SCSI_XFER_WRITE) { |
Ronnie Sahlberg | 0a53f01 | 2013-06-23 08:07:08 -0700 | [diff] [blame] | 863 | if (acb->ioh->iovec_count == 0) { |
| 864 | data.data = acb->ioh->dxferp; |
| 865 | data.size = acb->ioh->dxfer_len; |
| 866 | } else { |
Ronnie Sahlberg | 0a53f01 | 2013-06-23 08:07:08 -0700 | [diff] [blame] | 867 | scsi_task_set_iov_out(acb->task, |
| 868 | (struct scsi_iovec *) acb->ioh->dxferp, |
| 869 | acb->ioh->iovec_count); |
Ronnie Sahlberg | 0a53f01 | 2013-06-23 08:07:08 -0700 | [diff] [blame] | 870 | } |
Ronnie Sahlberg | 9839245 | 2012-05-25 21:59:01 +1000 | [diff] [blame] | 871 | } |
Ronnie Sahlberg | 0a53f01 | 2013-06-23 08:07:08 -0700 | [diff] [blame] | 872 | |
Ronnie Sahlberg | 9839245 | 2012-05-25 21:59:01 +1000 | [diff] [blame] | 873 | if (iscsi_scsi_command_async(iscsi, iscsilun->lun, acb->task, |
| 874 | iscsi_aio_ioctl_cb, |
Ronnie Sahlberg | 0a53f01 | 2013-06-23 08:07:08 -0700 | [diff] [blame] | 875 | (data.size > 0) ? &data : NULL, |
Ronnie Sahlberg | 9839245 | 2012-05-25 21:59:01 +1000 | [diff] [blame] | 876 | acb) != 0) { |
| 877 | scsi_free_scsi_task(acb->task); |
Fam Zheng | 8007429 | 2014-09-11 13:41:28 +0800 | [diff] [blame] | 878 | qemu_aio_unref(acb); |
Ronnie Sahlberg | 9839245 | 2012-05-25 21:59:01 +1000 | [diff] [blame] | 879 | return NULL; |
| 880 | } |
| 881 | |
| 882 | /* tell libiscsi to read straight into the buffer we got from ioctl */ |
| 883 | if (acb->task->xfer_dir == SCSI_XFER_READ) { |
Ronnie Sahlberg | 0a53f01 | 2013-06-23 08:07:08 -0700 | [diff] [blame] | 884 | if (acb->ioh->iovec_count == 0) { |
| 885 | scsi_task_add_data_in_buffer(acb->task, |
| 886 | acb->ioh->dxfer_len, |
| 887 | acb->ioh->dxferp); |
| 888 | } else { |
Ronnie Sahlberg | 0a53f01 | 2013-06-23 08:07:08 -0700 | [diff] [blame] | 889 | scsi_task_set_iov_in(acb->task, |
| 890 | (struct scsi_iovec *) acb->ioh->dxferp, |
| 891 | acb->ioh->iovec_count); |
Ronnie Sahlberg | 0a53f01 | 2013-06-23 08:07:08 -0700 | [diff] [blame] | 892 | } |
Ronnie Sahlberg | 9839245 | 2012-05-25 21:59:01 +1000 | [diff] [blame] | 893 | } |
| 894 | |
| 895 | iscsi_set_events(iscsilun); |
| 896 | |
| 897 | return &acb->common; |
| 898 | } |
| 899 | |
Ronnie Sahlberg | 9839245 | 2012-05-25 21:59:01 +1000 | [diff] [blame] | 900 | #endif |
| 901 | |
Ronnie Sahlberg | c589b24 | 2011-10-25 19:24:24 +1100 | [diff] [blame] | 902 | static int64_t |
| 903 | iscsi_getlength(BlockDriverState *bs) |
| 904 | { |
| 905 | IscsiLun *iscsilun = bs->opaque; |
| 906 | int64_t len; |
| 907 | |
| 908 | len = iscsilun->num_blocks; |
| 909 | len *= iscsilun->block_size; |
| 910 | |
| 911 | return len; |
| 912 | } |
| 913 | |
Peter Lieven | 65f3e33 | 2013-07-19 09:19:41 +0200 | [diff] [blame] | 914 | static int |
| 915 | coroutine_fn iscsi_co_discard(BlockDriverState *bs, int64_t sector_num, |
| 916 | int nb_sectors) |
| 917 | { |
| 918 | IscsiLun *iscsilun = bs->opaque; |
| 919 | struct IscsiTask iTask; |
| 920 | struct unmap_list list; |
Peter Lieven | 65f3e33 | 2013-07-19 09:19:41 +0200 | [diff] [blame] | 921 | |
| 922 | if (!is_request_lun_aligned(sector_num, nb_sectors, iscsilun)) { |
| 923 | return -EINVAL; |
| 924 | } |
| 925 | |
| 926 | if (!iscsilun->lbp.lbpu) { |
| 927 | /* UNMAP is not supported by the target */ |
| 928 | return 0; |
| 929 | } |
| 930 | |
| 931 | list.lba = sector_qemu2lun(sector_num, iscsilun); |
Peter Lieven | 01a6a23 | 2013-10-24 12:07:01 +0200 | [diff] [blame] | 932 | list.num = sector_qemu2lun(nb_sectors, iscsilun); |
Peter Lieven | 65f3e33 | 2013-07-19 09:19:41 +0200 | [diff] [blame] | 933 | |
Peter Lieven | 01a6a23 | 2013-10-24 12:07:01 +0200 | [diff] [blame] | 934 | iscsi_co_init_iscsitask(iscsilun, &iTask); |
| 935 | retry: |
| 936 | if (iscsi_unmap_task(iscsilun->iscsi, iscsilun->lun, 0, 0, &list, 1, |
| 937 | iscsi_co_generic_cb, &iTask) == NULL) { |
Peter Lieven | 9239711 | 2013-12-20 10:02:47 +0100 | [diff] [blame] | 938 | return -ENOMEM; |
Peter Lieven | 65f3e33 | 2013-07-19 09:19:41 +0200 | [diff] [blame] | 939 | } |
| 940 | |
Peter Lieven | 01a6a23 | 2013-10-24 12:07:01 +0200 | [diff] [blame] | 941 | while (!iTask.complete) { |
| 942 | iscsi_set_events(iscsilun); |
| 943 | qemu_coroutine_yield(); |
| 944 | } |
Peter Lieven | 65f3e33 | 2013-07-19 09:19:41 +0200 | [diff] [blame] | 945 | |
Peter Lieven | 01a6a23 | 2013-10-24 12:07:01 +0200 | [diff] [blame] | 946 | if (iTask.task != NULL) { |
| 947 | scsi_free_scsi_task(iTask.task); |
| 948 | iTask.task = NULL; |
| 949 | } |
Peter Lieven | 65f3e33 | 2013-07-19 09:19:41 +0200 | [diff] [blame] | 950 | |
Peter Lieven | 01a6a23 | 2013-10-24 12:07:01 +0200 | [diff] [blame] | 951 | if (iTask.do_retry) { |
Peter Lieven | 837c390 | 2014-02-18 13:08:39 +0100 | [diff] [blame] | 952 | iTask.complete = 0; |
Peter Lieven | 01a6a23 | 2013-10-24 12:07:01 +0200 | [diff] [blame] | 953 | goto retry; |
| 954 | } |
Peter Lieven | 65f3e33 | 2013-07-19 09:19:41 +0200 | [diff] [blame] | 955 | |
Peter Lieven | 01a6a23 | 2013-10-24 12:07:01 +0200 | [diff] [blame] | 956 | if (iTask.status == SCSI_STATUS_CHECK_CONDITION) { |
| 957 | /* the target might fail with a check condition if it |
| 958 | is not happy with the alignment of the UNMAP request |
| 959 | we silently fail in this case */ |
| 960 | return 0; |
| 961 | } |
Peter Lieven | 65f3e33 | 2013-07-19 09:19:41 +0200 | [diff] [blame] | 962 | |
Peter Lieven | 01a6a23 | 2013-10-24 12:07:01 +0200 | [diff] [blame] | 963 | if (iTask.status != SCSI_STATUS_GOOD) { |
Fam Zheng | e01dd3d | 2015-11-05 13:00:09 +0800 | [diff] [blame] | 964 | return iTask.err_code; |
Peter Lieven | 65f3e33 | 2013-07-19 09:19:41 +0200 | [diff] [blame] | 965 | } |
| 966 | |
Peter Lieven | b03c380 | 2014-04-28 13:11:32 +0200 | [diff] [blame] | 967 | iscsi_allocationmap_clear(iscsilun, sector_num, nb_sectors); |
| 968 | |
Peter Lieven | 65f3e33 | 2013-07-19 09:19:41 +0200 | [diff] [blame] | 969 | return 0; |
| 970 | } |
| 971 | |
Peter Lieven | d4cd961 | 2013-10-24 12:07:02 +0200 | [diff] [blame] | 972 | static int |
| 973 | coroutine_fn iscsi_co_write_zeroes(BlockDriverState *bs, int64_t sector_num, |
| 974 | int nb_sectors, BdrvRequestFlags flags) |
| 975 | { |
| 976 | IscsiLun *iscsilun = bs->opaque; |
| 977 | struct IscsiTask iTask; |
| 978 | uint64_t lba; |
| 979 | uint32_t nb_blocks; |
Peter Lieven | 9281fe9 | 2014-06-04 15:47:39 +0200 | [diff] [blame] | 980 | bool use_16_for_ws = iscsilun->use_16_for_rw; |
Peter Lieven | d4cd961 | 2013-10-24 12:07:02 +0200 | [diff] [blame] | 981 | |
| 982 | if (!is_request_lun_aligned(sector_num, nb_sectors, iscsilun)) { |
| 983 | return -EINVAL; |
| 984 | } |
| 985 | |
Peter Lieven | 9281fe9 | 2014-06-04 15:47:39 +0200 | [diff] [blame] | 986 | if (flags & BDRV_REQ_MAY_UNMAP) { |
| 987 | if (!use_16_for_ws && !iscsilun->lbp.lbpws10) { |
| 988 | /* WRITESAME10 with UNMAP is unsupported try WRITESAME16 */ |
| 989 | use_16_for_ws = true; |
| 990 | } |
| 991 | if (use_16_for_ws && !iscsilun->lbp.lbpws) { |
| 992 | /* WRITESAME16 with UNMAP is not supported by the target, |
| 993 | * fall back and try WRITESAME10/16 without UNMAP */ |
| 994 | flags &= ~BDRV_REQ_MAY_UNMAP; |
| 995 | use_16_for_ws = iscsilun->use_16_for_rw; |
| 996 | } |
Paolo Bonzini | fa6252b | 2013-11-22 13:39:54 +0100 | [diff] [blame] | 997 | } |
| 998 | |
Peter Lieven | dbe5c58 | 2014-04-28 13:23:25 +0200 | [diff] [blame] | 999 | if (!(flags & BDRV_REQ_MAY_UNMAP) && !iscsilun->has_write_same) { |
Peter Lieven | 9281fe9 | 2014-06-04 15:47:39 +0200 | [diff] [blame] | 1000 | /* WRITESAME without UNMAP is not supported by the target */ |
Peter Lieven | d4cd961 | 2013-10-24 12:07:02 +0200 | [diff] [blame] | 1001 | return -ENOTSUP; |
| 1002 | } |
| 1003 | |
| 1004 | lba = sector_qemu2lun(sector_num, iscsilun); |
| 1005 | nb_blocks = sector_qemu2lun(nb_sectors, iscsilun); |
| 1006 | |
| 1007 | if (iscsilun->zeroblock == NULL) { |
Kevin Wolf | 4d5a3f8 | 2014-05-20 13:30:49 +0200 | [diff] [blame] | 1008 | iscsilun->zeroblock = g_try_malloc0(iscsilun->block_size); |
| 1009 | if (iscsilun->zeroblock == NULL) { |
| 1010 | return -ENOMEM; |
| 1011 | } |
Peter Lieven | d4cd961 | 2013-10-24 12:07:02 +0200 | [diff] [blame] | 1012 | } |
| 1013 | |
| 1014 | iscsi_co_init_iscsitask(iscsilun, &iTask); |
| 1015 | retry: |
Peter Lieven | 9281fe9 | 2014-06-04 15:47:39 +0200 | [diff] [blame] | 1016 | if (use_16_for_ws) { |
| 1017 | iTask.task = iscsi_writesame16_task(iscsilun->iscsi, iscsilun->lun, lba, |
| 1018 | iscsilun->zeroblock, iscsilun->block_size, |
| 1019 | nb_blocks, 0, !!(flags & BDRV_REQ_MAY_UNMAP), |
| 1020 | 0, 0, iscsi_co_generic_cb, &iTask); |
| 1021 | } else { |
| 1022 | iTask.task = iscsi_writesame10_task(iscsilun->iscsi, iscsilun->lun, lba, |
| 1023 | iscsilun->zeroblock, iscsilun->block_size, |
| 1024 | nb_blocks, 0, !!(flags & BDRV_REQ_MAY_UNMAP), |
| 1025 | 0, 0, iscsi_co_generic_cb, &iTask); |
| 1026 | } |
| 1027 | if (iTask.task == NULL) { |
Peter Lieven | 9239711 | 2013-12-20 10:02:47 +0100 | [diff] [blame] | 1028 | return -ENOMEM; |
Peter Lieven | d4cd961 | 2013-10-24 12:07:02 +0200 | [diff] [blame] | 1029 | } |
| 1030 | |
| 1031 | while (!iTask.complete) { |
| 1032 | iscsi_set_events(iscsilun); |
| 1033 | qemu_coroutine_yield(); |
| 1034 | } |
| 1035 | |
Peter Lieven | d9738fd | 2014-02-22 13:17:24 +0100 | [diff] [blame] | 1036 | if (iTask.status == SCSI_STATUS_CHECK_CONDITION && |
| 1037 | iTask.task->sense.key == SCSI_SENSE_ILLEGAL_REQUEST && |
Paolo Bonzini | 27898a5 | 2014-04-02 12:12:50 +0200 | [diff] [blame] | 1038 | (iTask.task->sense.ascq == SCSI_SENSE_ASCQ_INVALID_OPERATION_CODE || |
| 1039 | iTask.task->sense.ascq == SCSI_SENSE_ASCQ_INVALID_FIELD_IN_CDB)) { |
Peter Lieven | d9738fd | 2014-02-22 13:17:24 +0100 | [diff] [blame] | 1040 | /* WRITE SAME is not supported by the target */ |
| 1041 | iscsilun->has_write_same = false; |
| 1042 | scsi_free_scsi_task(iTask.task); |
| 1043 | return -ENOTSUP; |
| 1044 | } |
| 1045 | |
Peter Lieven | d4cd961 | 2013-10-24 12:07:02 +0200 | [diff] [blame] | 1046 | if (iTask.task != NULL) { |
| 1047 | scsi_free_scsi_task(iTask.task); |
| 1048 | iTask.task = NULL; |
| 1049 | } |
| 1050 | |
| 1051 | if (iTask.do_retry) { |
Peter Lieven | 837c390 | 2014-02-18 13:08:39 +0100 | [diff] [blame] | 1052 | iTask.complete = 0; |
Peter Lieven | d4cd961 | 2013-10-24 12:07:02 +0200 | [diff] [blame] | 1053 | goto retry; |
| 1054 | } |
| 1055 | |
| 1056 | if (iTask.status != SCSI_STATUS_GOOD) { |
Fam Zheng | e01dd3d | 2015-11-05 13:00:09 +0800 | [diff] [blame] | 1057 | return iTask.err_code; |
Peter Lieven | d4cd961 | 2013-10-24 12:07:02 +0200 | [diff] [blame] | 1058 | } |
| 1059 | |
Peter Lieven | b03c380 | 2014-04-28 13:11:32 +0200 | [diff] [blame] | 1060 | if (flags & BDRV_REQ_MAY_UNMAP) { |
| 1061 | iscsi_allocationmap_clear(iscsilun, sector_num, nb_sectors); |
| 1062 | } else { |
| 1063 | iscsi_allocationmap_set(iscsilun, sector_num, nb_sectors); |
| 1064 | } |
| 1065 | |
Peter Lieven | d4cd961 | 2013-10-24 12:07:02 +0200 | [diff] [blame] | 1066 | return 0; |
| 1067 | } |
| 1068 | |
Paolo Bonzini | f291785 | 2014-02-17 14:43:53 +0100 | [diff] [blame] | 1069 | static void parse_chap(struct iscsi_context *iscsi, const char *target, |
| 1070 | Error **errp) |
Ronnie Sahlberg | f9dadc9 | 2012-01-26 09:39:02 +1100 | [diff] [blame] | 1071 | { |
| 1072 | QemuOptsList *list; |
| 1073 | QemuOpts *opts; |
| 1074 | const char *user = NULL; |
| 1075 | const char *password = NULL; |
Daniel P. Berrange | b189346 | 2016-01-21 14:19:21 +0000 | [diff] [blame] | 1076 | const char *secretid; |
| 1077 | char *secret = NULL; |
Ronnie Sahlberg | f9dadc9 | 2012-01-26 09:39:02 +1100 | [diff] [blame] | 1078 | |
| 1079 | list = qemu_find_opts("iscsi"); |
| 1080 | if (!list) { |
Paolo Bonzini | f291785 | 2014-02-17 14:43:53 +0100 | [diff] [blame] | 1081 | return; |
Ronnie Sahlberg | f9dadc9 | 2012-01-26 09:39:02 +1100 | [diff] [blame] | 1082 | } |
| 1083 | |
| 1084 | opts = qemu_opts_find(list, target); |
| 1085 | if (opts == NULL) { |
| 1086 | opts = QTAILQ_FIRST(&list->head); |
| 1087 | if (!opts) { |
Paolo Bonzini | f291785 | 2014-02-17 14:43:53 +0100 | [diff] [blame] | 1088 | return; |
Ronnie Sahlberg | f9dadc9 | 2012-01-26 09:39:02 +1100 | [diff] [blame] | 1089 | } |
| 1090 | } |
| 1091 | |
| 1092 | user = qemu_opt_get(opts, "user"); |
| 1093 | if (!user) { |
Paolo Bonzini | f291785 | 2014-02-17 14:43:53 +0100 | [diff] [blame] | 1094 | return; |
Ronnie Sahlberg | f9dadc9 | 2012-01-26 09:39:02 +1100 | [diff] [blame] | 1095 | } |
| 1096 | |
Daniel P. Berrange | b189346 | 2016-01-21 14:19:21 +0000 | [diff] [blame] | 1097 | secretid = qemu_opt_get(opts, "password-secret"); |
Ronnie Sahlberg | f9dadc9 | 2012-01-26 09:39:02 +1100 | [diff] [blame] | 1098 | password = qemu_opt_get(opts, "password"); |
Daniel P. Berrange | b189346 | 2016-01-21 14:19:21 +0000 | [diff] [blame] | 1099 | if (secretid && password) { |
| 1100 | error_setg(errp, "'password' and 'password-secret' properties are " |
| 1101 | "mutually exclusive"); |
| 1102 | return; |
| 1103 | } |
| 1104 | if (secretid) { |
| 1105 | secret = qcrypto_secret_lookup_as_utf8(secretid, errp); |
| 1106 | if (!secret) { |
| 1107 | return; |
| 1108 | } |
| 1109 | password = secret; |
| 1110 | } else if (!password) { |
Paolo Bonzini | f291785 | 2014-02-17 14:43:53 +0100 | [diff] [blame] | 1111 | error_setg(errp, "CHAP username specified but no password was given"); |
| 1112 | return; |
Ronnie Sahlberg | f9dadc9 | 2012-01-26 09:39:02 +1100 | [diff] [blame] | 1113 | } |
| 1114 | |
| 1115 | if (iscsi_set_initiator_username_pwd(iscsi, user, password)) { |
Paolo Bonzini | f291785 | 2014-02-17 14:43:53 +0100 | [diff] [blame] | 1116 | error_setg(errp, "Failed to set initiator username and password"); |
Ronnie Sahlberg | f9dadc9 | 2012-01-26 09:39:02 +1100 | [diff] [blame] | 1117 | } |
Daniel P. Berrange | b189346 | 2016-01-21 14:19:21 +0000 | [diff] [blame] | 1118 | |
| 1119 | g_free(secret); |
Ronnie Sahlberg | f9dadc9 | 2012-01-26 09:39:02 +1100 | [diff] [blame] | 1120 | } |
| 1121 | |
Paolo Bonzini | f291785 | 2014-02-17 14:43:53 +0100 | [diff] [blame] | 1122 | static void parse_header_digest(struct iscsi_context *iscsi, const char *target, |
| 1123 | Error **errp) |
Ronnie Sahlberg | f9dadc9 | 2012-01-26 09:39:02 +1100 | [diff] [blame] | 1124 | { |
| 1125 | QemuOptsList *list; |
| 1126 | QemuOpts *opts; |
| 1127 | const char *digest = NULL; |
| 1128 | |
| 1129 | list = qemu_find_opts("iscsi"); |
| 1130 | if (!list) { |
| 1131 | return; |
| 1132 | } |
| 1133 | |
| 1134 | opts = qemu_opts_find(list, target); |
| 1135 | if (opts == NULL) { |
| 1136 | opts = QTAILQ_FIRST(&list->head); |
| 1137 | if (!opts) { |
| 1138 | return; |
| 1139 | } |
| 1140 | } |
| 1141 | |
| 1142 | digest = qemu_opt_get(opts, "header-digest"); |
| 1143 | if (!digest) { |
| 1144 | return; |
| 1145 | } |
| 1146 | |
| 1147 | if (!strcmp(digest, "CRC32C")) { |
| 1148 | iscsi_set_header_digest(iscsi, ISCSI_HEADER_DIGEST_CRC32C); |
| 1149 | } else if (!strcmp(digest, "NONE")) { |
| 1150 | iscsi_set_header_digest(iscsi, ISCSI_HEADER_DIGEST_NONE); |
| 1151 | } else if (!strcmp(digest, "CRC32C-NONE")) { |
| 1152 | iscsi_set_header_digest(iscsi, ISCSI_HEADER_DIGEST_CRC32C_NONE); |
| 1153 | } else if (!strcmp(digest, "NONE-CRC32C")) { |
| 1154 | iscsi_set_header_digest(iscsi, ISCSI_HEADER_DIGEST_NONE_CRC32C); |
| 1155 | } else { |
Paolo Bonzini | f291785 | 2014-02-17 14:43:53 +0100 | [diff] [blame] | 1156 | error_setg(errp, "Invalid header-digest setting : %s", digest); |
Ronnie Sahlberg | f9dadc9 | 2012-01-26 09:39:02 +1100 | [diff] [blame] | 1157 | } |
| 1158 | } |
| 1159 | |
| 1160 | static char *parse_initiator_name(const char *target) |
| 1161 | { |
| 1162 | QemuOptsList *list; |
| 1163 | QemuOpts *opts; |
Paolo Bonzini | 5accc84 | 2013-08-02 17:02:01 +0200 | [diff] [blame] | 1164 | const char *name; |
| 1165 | char *iscsi_name; |
| 1166 | UuidInfo *uuid_info; |
Ronnie Sahlberg | f9dadc9 | 2012-01-26 09:39:02 +1100 | [diff] [blame] | 1167 | |
| 1168 | list = qemu_find_opts("iscsi"); |
Paolo Bonzini | f2ef4a6 | 2012-08-06 10:54:41 +0200 | [diff] [blame] | 1169 | if (list) { |
| 1170 | opts = qemu_opts_find(list, target); |
Ronnie Sahlberg | f9dadc9 | 2012-01-26 09:39:02 +1100 | [diff] [blame] | 1171 | if (!opts) { |
Paolo Bonzini | f2ef4a6 | 2012-08-06 10:54:41 +0200 | [diff] [blame] | 1172 | opts = QTAILQ_FIRST(&list->head); |
| 1173 | } |
| 1174 | if (opts) { |
| 1175 | name = qemu_opt_get(opts, "initiator-name"); |
Paolo Bonzini | 5accc84 | 2013-08-02 17:02:01 +0200 | [diff] [blame] | 1176 | if (name) { |
| 1177 | return g_strdup(name); |
| 1178 | } |
Ronnie Sahlberg | f9dadc9 | 2012-01-26 09:39:02 +1100 | [diff] [blame] | 1179 | } |
| 1180 | } |
| 1181 | |
Paolo Bonzini | 5accc84 | 2013-08-02 17:02:01 +0200 | [diff] [blame] | 1182 | uuid_info = qmp_query_uuid(NULL); |
| 1183 | if (strcmp(uuid_info->UUID, UUID_NONE) == 0) { |
| 1184 | name = qemu_get_vm_name(); |
Paolo Bonzini | f2ef4a6 | 2012-08-06 10:54:41 +0200 | [diff] [blame] | 1185 | } else { |
Paolo Bonzini | 5accc84 | 2013-08-02 17:02:01 +0200 | [diff] [blame] | 1186 | name = uuid_info->UUID; |
Ronnie Sahlberg | f9dadc9 | 2012-01-26 09:39:02 +1100 | [diff] [blame] | 1187 | } |
Paolo Bonzini | 5accc84 | 2013-08-02 17:02:01 +0200 | [diff] [blame] | 1188 | iscsi_name = g_strdup_printf("iqn.2008-11.org.linux-kvm%s%s", |
| 1189 | name ? ":" : "", name ? name : ""); |
| 1190 | qapi_free_UuidInfo(uuid_info); |
| 1191 | return iscsi_name; |
Ronnie Sahlberg | f9dadc9 | 2012-01-26 09:39:02 +1100 | [diff] [blame] | 1192 | } |
| 1193 | |
Peter Lieven | 5dd7a53 | 2015-06-16 13:45:07 +0200 | [diff] [blame] | 1194 | static int parse_timeout(const char *target) |
| 1195 | { |
| 1196 | QemuOptsList *list; |
| 1197 | QemuOpts *opts; |
| 1198 | const char *timeout; |
| 1199 | |
| 1200 | list = qemu_find_opts("iscsi"); |
| 1201 | if (list) { |
| 1202 | opts = qemu_opts_find(list, target); |
| 1203 | if (!opts) { |
| 1204 | opts = QTAILQ_FIRST(&list->head); |
| 1205 | } |
| 1206 | if (opts) { |
| 1207 | timeout = qemu_opt_get(opts, "timeout"); |
| 1208 | if (timeout) { |
| 1209 | return atoi(timeout); |
| 1210 | } |
| 1211 | } |
| 1212 | } |
| 1213 | |
| 1214 | return 0; |
| 1215 | } |
| 1216 | |
Peter Lieven | 5b5d34e | 2012-12-06 10:46:47 +0100 | [diff] [blame] | 1217 | static void iscsi_nop_timed_event(void *opaque) |
| 1218 | { |
| 1219 | IscsiLun *iscsilun = opaque; |
| 1220 | |
Peter Lieven | 5dd7a53 | 2015-06-16 13:45:07 +0200 | [diff] [blame] | 1221 | if (iscsi_get_nops_in_flight(iscsilun->iscsi) >= MAX_NOP_FAILURES) { |
Peter Lieven | 5b5d34e | 2012-12-06 10:46:47 +0100 | [diff] [blame] | 1222 | error_report("iSCSI: NOP timeout. Reconnecting..."); |
Peter Lieven | 5dd7a53 | 2015-06-16 13:45:07 +0200 | [diff] [blame] | 1223 | iscsilun->request_timed_out = true; |
| 1224 | } else if (iscsi_nop_out_async(iscsilun->iscsi, NULL, NULL, 0, NULL) != 0) { |
Peter Lieven | 5b5d34e | 2012-12-06 10:46:47 +0100 | [diff] [blame] | 1225 | error_report("iSCSI: failed to sent NOP-Out. Disabling NOP messages."); |
| 1226 | return; |
| 1227 | } |
| 1228 | |
Alex Bligh | bc72ad6 | 2013-08-21 16:03:08 +0100 | [diff] [blame] | 1229 | timer_mod(iscsilun->nop_timer, qemu_clock_get_ms(QEMU_CLOCK_REALTIME) + NOP_INTERVAL); |
Peter Lieven | 5b5d34e | 2012-12-06 10:46:47 +0100 | [diff] [blame] | 1230 | iscsi_set_events(iscsilun); |
| 1231 | } |
Peter Lieven | 5b5d34e | 2012-12-06 10:46:47 +0100 | [diff] [blame] | 1232 | |
Paolo Bonzini | f291785 | 2014-02-17 14:43:53 +0100 | [diff] [blame] | 1233 | static void iscsi_readcapacity_sync(IscsiLun *iscsilun, Error **errp) |
Peter Lieven | cb1b83e | 2013-02-18 14:50:46 +0100 | [diff] [blame] | 1234 | { |
| 1235 | struct scsi_task *task = NULL; |
| 1236 | struct scsi_readcapacity10 *rc10 = NULL; |
| 1237 | struct scsi_readcapacity16 *rc16 = NULL; |
Peter Lieven | cb1b83e | 2013-02-18 14:50:46 +0100 | [diff] [blame] | 1238 | int retries = ISCSI_CMD_RETRIES; |
| 1239 | |
Paolo Bonzini | 1288844 | 2013-05-31 13:56:24 +0200 | [diff] [blame] | 1240 | do { |
| 1241 | if (task != NULL) { |
| 1242 | scsi_free_scsi_task(task); |
| 1243 | task = NULL; |
Peter Lieven | cb1b83e | 2013-02-18 14:50:46 +0100 | [diff] [blame] | 1244 | } |
Peter Lieven | cb1b83e | 2013-02-18 14:50:46 +0100 | [diff] [blame] | 1245 | |
Paolo Bonzini | 1288844 | 2013-05-31 13:56:24 +0200 | [diff] [blame] | 1246 | switch (iscsilun->type) { |
| 1247 | case TYPE_DISK: |
| 1248 | task = iscsi_readcapacity16_sync(iscsilun->iscsi, iscsilun->lun); |
| 1249 | if (task != NULL && task->status == SCSI_STATUS_GOOD) { |
| 1250 | rc16 = scsi_datain_unmarshall(task); |
| 1251 | if (rc16 == NULL) { |
Paolo Bonzini | f291785 | 2014-02-17 14:43:53 +0100 | [diff] [blame] | 1252 | error_setg(errp, "iSCSI: Failed to unmarshall readcapacity16 data."); |
Paolo Bonzini | 1288844 | 2013-05-31 13:56:24 +0200 | [diff] [blame] | 1253 | } else { |
| 1254 | iscsilun->block_size = rc16->block_length; |
| 1255 | iscsilun->num_blocks = rc16->returned_lba + 1; |
Peter Lieven | 0a386e4 | 2015-04-16 16:08:26 +0200 | [diff] [blame] | 1256 | iscsilun->lbpme = !!rc16->lbpme; |
| 1257 | iscsilun->lbprz = !!rc16->lbprz; |
Peter Lieven | 9281fe9 | 2014-06-04 15:47:39 +0200 | [diff] [blame] | 1258 | iscsilun->use_16_for_rw = (rc16->returned_lba > 0xffffffff); |
Paolo Bonzini | 1288844 | 2013-05-31 13:56:24 +0200 | [diff] [blame] | 1259 | } |
Zhu Lingshan | 1cb6d13 | 2015-12-29 11:32:14 +0800 | [diff] [blame] | 1260 | break; |
Paolo Bonzini | 1288844 | 2013-05-31 13:56:24 +0200 | [diff] [blame] | 1261 | } |
Zhu Lingshan | 1cb6d13 | 2015-12-29 11:32:14 +0800 | [diff] [blame] | 1262 | if (task != NULL && task->status == SCSI_STATUS_CHECK_CONDITION |
| 1263 | && task->sense.key == SCSI_SENSE_UNIT_ATTENTION) { |
| 1264 | break; |
| 1265 | } |
| 1266 | /* Fall through and try READ CAPACITY(10) instead. */ |
Paolo Bonzini | 1288844 | 2013-05-31 13:56:24 +0200 | [diff] [blame] | 1267 | case TYPE_ROM: |
| 1268 | task = iscsi_readcapacity10_sync(iscsilun->iscsi, iscsilun->lun, 0, 0); |
| 1269 | if (task != NULL && task->status == SCSI_STATUS_GOOD) { |
| 1270 | rc10 = scsi_datain_unmarshall(task); |
| 1271 | if (rc10 == NULL) { |
Paolo Bonzini | f291785 | 2014-02-17 14:43:53 +0100 | [diff] [blame] | 1272 | error_setg(errp, "iSCSI: Failed to unmarshall readcapacity10 data."); |
Paolo Bonzini | 1288844 | 2013-05-31 13:56:24 +0200 | [diff] [blame] | 1273 | } else { |
| 1274 | iscsilun->block_size = rc10->block_size; |
| 1275 | if (rc10->lba == 0) { |
| 1276 | /* blank disk loaded */ |
| 1277 | iscsilun->num_blocks = 0; |
| 1278 | } else { |
| 1279 | iscsilun->num_blocks = rc10->lba + 1; |
| 1280 | } |
| 1281 | } |
| 1282 | } |
| 1283 | break; |
| 1284 | default: |
Paolo Bonzini | f291785 | 2014-02-17 14:43:53 +0100 | [diff] [blame] | 1285 | return; |
Paolo Bonzini | 1288844 | 2013-05-31 13:56:24 +0200 | [diff] [blame] | 1286 | } |
| 1287 | } while (task != NULL && task->status == SCSI_STATUS_CHECK_CONDITION |
| 1288 | && task->sense.key == SCSI_SENSE_UNIT_ATTENTION |
| 1289 | && retries-- > 0); |
| 1290 | |
| 1291 | if (task == NULL || task->status != SCSI_STATUS_GOOD) { |
Zhu Lingshan | 240125b | 2015-12-16 12:40:40 +0800 | [diff] [blame] | 1292 | error_setg(errp, "iSCSI: failed to send readcapacity10/16 command"); |
Peter Lieven | 6d1f252 | 2015-08-14 13:33:36 +0200 | [diff] [blame] | 1293 | } else if (!iscsilun->block_size || |
| 1294 | iscsilun->block_size % BDRV_SECTOR_SIZE) { |
| 1295 | error_setg(errp, "iSCSI: the target returned an invalid " |
| 1296 | "block size of %d.", iscsilun->block_size); |
Paolo Bonzini | 1288844 | 2013-05-31 13:56:24 +0200 | [diff] [blame] | 1297 | } |
Peter Lieven | cb1b83e | 2013-02-18 14:50:46 +0100 | [diff] [blame] | 1298 | if (task) { |
| 1299 | scsi_free_scsi_task(task); |
| 1300 | } |
Peter Lieven | cb1b83e | 2013-02-18 14:50:46 +0100 | [diff] [blame] | 1301 | } |
| 1302 | |
Kevin Wolf | 60beb34 | 2013-04-12 17:59:59 +0200 | [diff] [blame] | 1303 | /* TODO Convert to fine grained options */ |
| 1304 | static QemuOptsList runtime_opts = { |
| 1305 | .name = "iscsi", |
| 1306 | .head = QTAILQ_HEAD_INITIALIZER(runtime_opts.head), |
| 1307 | .desc = { |
| 1308 | { |
| 1309 | .name = "filename", |
| 1310 | .type = QEMU_OPT_STRING, |
| 1311 | .help = "URL to the iscsi image", |
| 1312 | }, |
| 1313 | { /* end of list */ } |
| 1314 | }, |
| 1315 | }; |
| 1316 | |
Paolo Bonzini | 35cb174 | 2014-02-17 14:43:52 +0100 | [diff] [blame] | 1317 | static struct scsi_task *iscsi_do_inquiry(struct iscsi_context *iscsi, int lun, |
Peter Lieven | 24d3bd6 | 2014-02-17 18:34:08 +0100 | [diff] [blame] | 1318 | int evpd, int pc, void **inq, Error **errp) |
Paolo Bonzini | 35cb174 | 2014-02-17 14:43:52 +0100 | [diff] [blame] | 1319 | { |
| 1320 | int full_size; |
| 1321 | struct scsi_task *task = NULL; |
| 1322 | task = iscsi_inquiry_sync(iscsi, lun, evpd, pc, 64); |
| 1323 | if (task == NULL || task->status != SCSI_STATUS_GOOD) { |
| 1324 | goto fail; |
| 1325 | } |
| 1326 | full_size = scsi_datain_getfullsize(task); |
| 1327 | if (full_size > task->datain.size) { |
| 1328 | scsi_free_scsi_task(task); |
| 1329 | |
| 1330 | /* we need more data for the full list */ |
| 1331 | task = iscsi_inquiry_sync(iscsi, lun, evpd, pc, full_size); |
Peter Lieven | f18a7cb | 2013-07-19 09:19:39 +0200 | [diff] [blame] | 1332 | if (task == NULL || task->status != SCSI_STATUS_GOOD) { |
| 1333 | goto fail; |
| 1334 | } |
Paolo Bonzini | 35cb174 | 2014-02-17 14:43:52 +0100 | [diff] [blame] | 1335 | } |
Peter Lieven | f18a7cb | 2013-07-19 09:19:39 +0200 | [diff] [blame] | 1336 | |
Peter Lieven | 24d3bd6 | 2014-02-17 18:34:08 +0100 | [diff] [blame] | 1337 | *inq = scsi_datain_unmarshall(task); |
| 1338 | if (*inq == NULL) { |
| 1339 | error_setg(errp, "iSCSI: failed to unmarshall inquiry datain blob"); |
Markus Armbruster | 172fc4d | 2014-04-25 16:50:35 +0200 | [diff] [blame] | 1340 | goto fail_with_err; |
Peter Lieven | 24d3bd6 | 2014-02-17 18:34:08 +0100 | [diff] [blame] | 1341 | } |
| 1342 | |
Paolo Bonzini | 35cb174 | 2014-02-17 14:43:52 +0100 | [diff] [blame] | 1343 | return task; |
Peter Lieven | f18a7cb | 2013-07-19 09:19:39 +0200 | [diff] [blame] | 1344 | |
| 1345 | fail: |
Markus Armbruster | 172fc4d | 2014-04-25 16:50:35 +0200 | [diff] [blame] | 1346 | error_setg(errp, "iSCSI: Inquiry command failed : %s", |
| 1347 | iscsi_get_error(iscsi)); |
| 1348 | fail_with_err: |
Peter Lieven | 24d3bd6 | 2014-02-17 18:34:08 +0100 | [diff] [blame] | 1349 | if (task != NULL) { |
Paolo Bonzini | 35cb174 | 2014-02-17 14:43:52 +0100 | [diff] [blame] | 1350 | scsi_free_scsi_task(task); |
Paolo Bonzini | 35cb174 | 2014-02-17 14:43:52 +0100 | [diff] [blame] | 1351 | } |
| 1352 | return NULL; |
Peter Lieven | f18a7cb | 2013-07-19 09:19:39 +0200 | [diff] [blame] | 1353 | } |
| 1354 | |
Stefan Hajnoczi | 80cf625 | 2014-05-08 16:34:42 +0200 | [diff] [blame] | 1355 | static void iscsi_detach_aio_context(BlockDriverState *bs) |
| 1356 | { |
| 1357 | IscsiLun *iscsilun = bs->opaque; |
| 1358 | |
Fam Zheng | dca21ef | 2015-10-23 11:08:05 +0800 | [diff] [blame] | 1359 | aio_set_fd_handler(iscsilun->aio_context, iscsi_get_fd(iscsilun->iscsi), |
| 1360 | false, NULL, NULL, NULL); |
Stefan Hajnoczi | 80cf625 | 2014-05-08 16:34:42 +0200 | [diff] [blame] | 1361 | iscsilun->events = 0; |
| 1362 | |
| 1363 | if (iscsilun->nop_timer) { |
| 1364 | timer_del(iscsilun->nop_timer); |
| 1365 | timer_free(iscsilun->nop_timer); |
| 1366 | iscsilun->nop_timer = NULL; |
| 1367 | } |
Peter Lieven | 05b685f | 2015-04-07 22:08:15 +0200 | [diff] [blame] | 1368 | if (iscsilun->event_timer) { |
| 1369 | timer_del(iscsilun->event_timer); |
| 1370 | timer_free(iscsilun->event_timer); |
| 1371 | iscsilun->event_timer = NULL; |
| 1372 | } |
Stefan Hajnoczi | 80cf625 | 2014-05-08 16:34:42 +0200 | [diff] [blame] | 1373 | } |
| 1374 | |
| 1375 | static void iscsi_attach_aio_context(BlockDriverState *bs, |
| 1376 | AioContext *new_context) |
| 1377 | { |
| 1378 | IscsiLun *iscsilun = bs->opaque; |
| 1379 | |
| 1380 | iscsilun->aio_context = new_context; |
| 1381 | iscsi_set_events(iscsilun); |
| 1382 | |
Stefan Hajnoczi | 80cf625 | 2014-05-08 16:34:42 +0200 | [diff] [blame] | 1383 | /* Set up a timer for sending out iSCSI NOPs */ |
| 1384 | iscsilun->nop_timer = aio_timer_new(iscsilun->aio_context, |
| 1385 | QEMU_CLOCK_REALTIME, SCALE_MS, |
| 1386 | iscsi_nop_timed_event, iscsilun); |
| 1387 | timer_mod(iscsilun->nop_timer, |
| 1388 | qemu_clock_get_ms(QEMU_CLOCK_REALTIME) + NOP_INTERVAL); |
Peter Lieven | 05b685f | 2015-04-07 22:08:15 +0200 | [diff] [blame] | 1389 | |
Peter Lieven | 5dd7a53 | 2015-06-16 13:45:07 +0200 | [diff] [blame] | 1390 | /* Set up a timer for periodic calls to iscsi_set_events and to |
| 1391 | * scan for command timeout */ |
Peter Lieven | 05b685f | 2015-04-07 22:08:15 +0200 | [diff] [blame] | 1392 | iscsilun->event_timer = aio_timer_new(iscsilun->aio_context, |
| 1393 | QEMU_CLOCK_REALTIME, SCALE_MS, |
Peter Lieven | 5dd7a53 | 2015-06-16 13:45:07 +0200 | [diff] [blame] | 1394 | iscsi_timed_check_events, iscsilun); |
| 1395 | timer_mod(iscsilun->event_timer, |
| 1396 | qemu_clock_get_ms(QEMU_CLOCK_REALTIME) + EVENT_INTERVAL); |
Stefan Hajnoczi | 80cf625 | 2014-05-08 16:34:42 +0200 | [diff] [blame] | 1397 | } |
| 1398 | |
Peter Lieven | 7191f20 | 2015-04-16 16:08:27 +0200 | [diff] [blame] | 1399 | static void iscsi_modesense_sync(IscsiLun *iscsilun) |
Fam Zheng | c1d4096 | 2014-10-30 19:23:46 +0800 | [diff] [blame] | 1400 | { |
| 1401 | struct scsi_task *task; |
| 1402 | struct scsi_mode_sense *ms = NULL; |
Peter Lieven | 7191f20 | 2015-04-16 16:08:27 +0200 | [diff] [blame] | 1403 | iscsilun->write_protected = false; |
Peter Lieven | 752ce45 | 2015-04-16 16:08:28 +0200 | [diff] [blame] | 1404 | iscsilun->dpofua = false; |
Fam Zheng | c1d4096 | 2014-10-30 19:23:46 +0800 | [diff] [blame] | 1405 | |
| 1406 | task = iscsi_modesense6_sync(iscsilun->iscsi, iscsilun->lun, |
| 1407 | 1, SCSI_MODESENSE_PC_CURRENT, |
| 1408 | 0x3F, 0, 255); |
| 1409 | if (task == NULL) { |
| 1410 | error_report("iSCSI: Failed to send MODE_SENSE(6) command: %s", |
| 1411 | iscsi_get_error(iscsilun->iscsi)); |
| 1412 | goto out; |
| 1413 | } |
| 1414 | |
| 1415 | if (task->status != SCSI_STATUS_GOOD) { |
| 1416 | error_report("iSCSI: Failed MODE_SENSE(6), LUN assumed writable"); |
| 1417 | goto out; |
| 1418 | } |
| 1419 | ms = scsi_datain_unmarshall(task); |
| 1420 | if (!ms) { |
| 1421 | error_report("iSCSI: Failed to unmarshall MODE_SENSE(6) data: %s", |
| 1422 | iscsi_get_error(iscsilun->iscsi)); |
| 1423 | goto out; |
| 1424 | } |
Peter Lieven | 7191f20 | 2015-04-16 16:08:27 +0200 | [diff] [blame] | 1425 | iscsilun->write_protected = ms->device_specific_parameter & 0x80; |
Peter Lieven | 752ce45 | 2015-04-16 16:08:28 +0200 | [diff] [blame] | 1426 | iscsilun->dpofua = ms->device_specific_parameter & 0x10; |
Fam Zheng | c1d4096 | 2014-10-30 19:23:46 +0800 | [diff] [blame] | 1427 | |
| 1428 | out: |
| 1429 | if (task) { |
| 1430 | scsi_free_scsi_task(task); |
| 1431 | } |
Fam Zheng | c1d4096 | 2014-10-30 19:23:46 +0800 | [diff] [blame] | 1432 | } |
| 1433 | |
Ronnie Sahlberg | c589b24 | 2011-10-25 19:24:24 +1100 | [diff] [blame] | 1434 | /* |
| 1435 | * We support iscsi url's on the form |
| 1436 | * iscsi://[<username>%<password>@]<host>[:<port>]/<targetname>/<lun> |
| 1437 | */ |
Max Reitz | 015a103 | 2013-09-05 14:22:29 +0200 | [diff] [blame] | 1438 | static int iscsi_open(BlockDriverState *bs, QDict *options, int flags, |
| 1439 | Error **errp) |
Ronnie Sahlberg | c589b24 | 2011-10-25 19:24:24 +1100 | [diff] [blame] | 1440 | { |
| 1441 | IscsiLun *iscsilun = bs->opaque; |
| 1442 | struct iscsi_context *iscsi = NULL; |
| 1443 | struct iscsi_url *iscsi_url = NULL; |
Peter Lieven | e829b0b | 2012-11-17 14:37:39 +0100 | [diff] [blame] | 1444 | struct scsi_task *task = NULL; |
| 1445 | struct scsi_inquiry_standard *inq = NULL; |
Peter Lieven | 24d3bd6 | 2014-02-17 18:34:08 +0100 | [diff] [blame] | 1446 | struct scsi_inquiry_supported_pages *inq_vpd; |
Ronnie Sahlberg | f9dadc9 | 2012-01-26 09:39:02 +1100 | [diff] [blame] | 1447 | char *initiator_name = NULL; |
Kevin Wolf | 60beb34 | 2013-04-12 17:59:59 +0200 | [diff] [blame] | 1448 | QemuOpts *opts; |
| 1449 | Error *local_err = NULL; |
| 1450 | const char *filename; |
Peter Lieven | 9049736 | 2015-06-26 12:18:01 +0200 | [diff] [blame] | 1451 | int i, ret = 0, timeout = 0; |
Ronnie Sahlberg | c589b24 | 2011-10-25 19:24:24 +1100 | [diff] [blame] | 1452 | |
Peter Crosthwaite | 87ea75d | 2014-01-01 18:49:17 -0800 | [diff] [blame] | 1453 | opts = qemu_opts_create(&runtime_opts, NULL, 0, &error_abort); |
Kevin Wolf | 60beb34 | 2013-04-12 17:59:59 +0200 | [diff] [blame] | 1454 | qemu_opts_absorb_qdict(opts, options, &local_err); |
Markus Armbruster | 84d18f0 | 2014-01-30 15:07:28 +0100 | [diff] [blame] | 1455 | if (local_err) { |
Paolo Bonzini | f291785 | 2014-02-17 14:43:53 +0100 | [diff] [blame] | 1456 | error_propagate(errp, local_err); |
Kevin Wolf | 60beb34 | 2013-04-12 17:59:59 +0200 | [diff] [blame] | 1457 | ret = -EINVAL; |
| 1458 | goto out; |
| 1459 | } |
| 1460 | |
| 1461 | filename = qemu_opt_get(opts, "filename"); |
| 1462 | |
Ronnie Sahlberg | c589b24 | 2011-10-25 19:24:24 +1100 | [diff] [blame] | 1463 | iscsi_url = iscsi_parse_full_url(iscsi, filename); |
| 1464 | if (iscsi_url == NULL) { |
Paolo Bonzini | f291785 | 2014-02-17 14:43:53 +0100 | [diff] [blame] | 1465 | error_setg(errp, "Failed to parse URL : %s", filename); |
Ronnie Sahlberg | c589b24 | 2011-10-25 19:24:24 +1100 | [diff] [blame] | 1466 | ret = -EINVAL; |
Paolo Bonzini | b93c94f | 2012-08-06 10:52:22 +0200 | [diff] [blame] | 1467 | goto out; |
Ronnie Sahlberg | c589b24 | 2011-10-25 19:24:24 +1100 | [diff] [blame] | 1468 | } |
| 1469 | |
Ronnie Sahlberg | f9dadc9 | 2012-01-26 09:39:02 +1100 | [diff] [blame] | 1470 | memset(iscsilun, 0, sizeof(IscsiLun)); |
| 1471 | |
| 1472 | initiator_name = parse_initiator_name(iscsi_url->target); |
| 1473 | |
| 1474 | iscsi = iscsi_create_context(initiator_name); |
| 1475 | if (iscsi == NULL) { |
Paolo Bonzini | f291785 | 2014-02-17 14:43:53 +0100 | [diff] [blame] | 1476 | error_setg(errp, "iSCSI: Failed to create iSCSI context."); |
Ronnie Sahlberg | f9dadc9 | 2012-01-26 09:39:02 +1100 | [diff] [blame] | 1477 | ret = -ENOMEM; |
Paolo Bonzini | b93c94f | 2012-08-06 10:52:22 +0200 | [diff] [blame] | 1478 | goto out; |
Ronnie Sahlberg | f9dadc9 | 2012-01-26 09:39:02 +1100 | [diff] [blame] | 1479 | } |
| 1480 | |
Ronnie Sahlberg | c589b24 | 2011-10-25 19:24:24 +1100 | [diff] [blame] | 1481 | if (iscsi_set_targetname(iscsi, iscsi_url->target)) { |
Paolo Bonzini | f291785 | 2014-02-17 14:43:53 +0100 | [diff] [blame] | 1482 | error_setg(errp, "iSCSI: Failed to set target name."); |
Ronnie Sahlberg | c589b24 | 2011-10-25 19:24:24 +1100 | [diff] [blame] | 1483 | ret = -EINVAL; |
Paolo Bonzini | b93c94f | 2012-08-06 10:52:22 +0200 | [diff] [blame] | 1484 | goto out; |
Ronnie Sahlberg | c589b24 | 2011-10-25 19:24:24 +1100 | [diff] [blame] | 1485 | } |
| 1486 | |
Stefan Weil | 532cee4 | 2015-03-07 10:14:30 +0100 | [diff] [blame] | 1487 | if (iscsi_url->user[0] != '\0') { |
Ronnie Sahlberg | c589b24 | 2011-10-25 19:24:24 +1100 | [diff] [blame] | 1488 | ret = iscsi_set_initiator_username_pwd(iscsi, iscsi_url->user, |
| 1489 | iscsi_url->passwd); |
| 1490 | if (ret != 0) { |
Paolo Bonzini | f291785 | 2014-02-17 14:43:53 +0100 | [diff] [blame] | 1491 | error_setg(errp, "Failed to set initiator username and password"); |
Ronnie Sahlberg | c589b24 | 2011-10-25 19:24:24 +1100 | [diff] [blame] | 1492 | ret = -EINVAL; |
Paolo Bonzini | b93c94f | 2012-08-06 10:52:22 +0200 | [diff] [blame] | 1493 | goto out; |
Ronnie Sahlberg | c589b24 | 2011-10-25 19:24:24 +1100 | [diff] [blame] | 1494 | } |
| 1495 | } |
Ronnie Sahlberg | f9dadc9 | 2012-01-26 09:39:02 +1100 | [diff] [blame] | 1496 | |
| 1497 | /* check if we got CHAP username/password via the options */ |
Paolo Bonzini | f291785 | 2014-02-17 14:43:53 +0100 | [diff] [blame] | 1498 | parse_chap(iscsi, iscsi_url->target, &local_err); |
| 1499 | if (local_err != NULL) { |
| 1500 | error_propagate(errp, local_err); |
Ronnie Sahlberg | f9dadc9 | 2012-01-26 09:39:02 +1100 | [diff] [blame] | 1501 | ret = -EINVAL; |
Paolo Bonzini | b93c94f | 2012-08-06 10:52:22 +0200 | [diff] [blame] | 1502 | goto out; |
Ronnie Sahlberg | f9dadc9 | 2012-01-26 09:39:02 +1100 | [diff] [blame] | 1503 | } |
| 1504 | |
Ronnie Sahlberg | c589b24 | 2011-10-25 19:24:24 +1100 | [diff] [blame] | 1505 | if (iscsi_set_session_type(iscsi, ISCSI_SESSION_NORMAL) != 0) { |
Paolo Bonzini | f291785 | 2014-02-17 14:43:53 +0100 | [diff] [blame] | 1506 | error_setg(errp, "iSCSI: Failed to set session type to normal."); |
Ronnie Sahlberg | c589b24 | 2011-10-25 19:24:24 +1100 | [diff] [blame] | 1507 | ret = -EINVAL; |
Paolo Bonzini | b93c94f | 2012-08-06 10:52:22 +0200 | [diff] [blame] | 1508 | goto out; |
Ronnie Sahlberg | c589b24 | 2011-10-25 19:24:24 +1100 | [diff] [blame] | 1509 | } |
| 1510 | |
| 1511 | iscsi_set_header_digest(iscsi, ISCSI_HEADER_DIGEST_NONE_CRC32C); |
| 1512 | |
Ronnie Sahlberg | f9dadc9 | 2012-01-26 09:39:02 +1100 | [diff] [blame] | 1513 | /* check if we got HEADER_DIGEST via the options */ |
Paolo Bonzini | f291785 | 2014-02-17 14:43:53 +0100 | [diff] [blame] | 1514 | parse_header_digest(iscsi, iscsi_url->target, &local_err); |
| 1515 | if (local_err != NULL) { |
| 1516 | error_propagate(errp, local_err); |
| 1517 | ret = -EINVAL; |
| 1518 | goto out; |
| 1519 | } |
Ronnie Sahlberg | f9dadc9 | 2012-01-26 09:39:02 +1100 | [diff] [blame] | 1520 | |
Peter Lieven | 9049736 | 2015-06-26 12:18:01 +0200 | [diff] [blame] | 1521 | /* timeout handling is broken in libiscsi before 1.15.0 */ |
| 1522 | timeout = parse_timeout(iscsi_url->target); |
| 1523 | #if defined(LIBISCSI_API_VERSION) && LIBISCSI_API_VERSION >= 20150621 |
| 1524 | iscsi_set_timeout(iscsi, timeout); |
| 1525 | #else |
| 1526 | if (timeout) { |
| 1527 | error_report("iSCSI: ignoring timeout value for libiscsi <1.15.0"); |
| 1528 | } |
| 1529 | #endif |
Peter Lieven | 5dd7a53 | 2015-06-16 13:45:07 +0200 | [diff] [blame] | 1530 | |
Peter Lieven | e829b0b | 2012-11-17 14:37:39 +0100 | [diff] [blame] | 1531 | if (iscsi_full_connect_sync(iscsi, iscsi_url->portal, iscsi_url->lun) != 0) { |
Paolo Bonzini | f291785 | 2014-02-17 14:43:53 +0100 | [diff] [blame] | 1532 | error_setg(errp, "iSCSI: Failed to connect to LUN : %s", |
Peter Lieven | e829b0b | 2012-11-17 14:37:39 +0100 | [diff] [blame] | 1533 | iscsi_get_error(iscsi)); |
| 1534 | ret = -EINVAL; |
| 1535 | goto out; |
| 1536 | } |
Ronnie Sahlberg | c589b24 | 2011-10-25 19:24:24 +1100 | [diff] [blame] | 1537 | |
| 1538 | iscsilun->iscsi = iscsi; |
Stefan Hajnoczi | 80cf625 | 2014-05-08 16:34:42 +0200 | [diff] [blame] | 1539 | iscsilun->aio_context = bdrv_get_aio_context(bs); |
Ronnie Sahlberg | c589b24 | 2011-10-25 19:24:24 +1100 | [diff] [blame] | 1540 | iscsilun->lun = iscsi_url->lun; |
Paolo Bonzini | fa6252b | 2013-11-22 13:39:54 +0100 | [diff] [blame] | 1541 | iscsilun->has_write_same = true; |
Peter Lieven | e829b0b | 2012-11-17 14:37:39 +0100 | [diff] [blame] | 1542 | |
Peter Lieven | 24d3bd6 | 2014-02-17 18:34:08 +0100 | [diff] [blame] | 1543 | task = iscsi_do_inquiry(iscsilun->iscsi, iscsilun->lun, 0, 0, |
| 1544 | (void **) &inq, errp); |
| 1545 | if (task == NULL) { |
| 1546 | ret = -EINVAL; |
| 1547 | goto out; |
| 1548 | } |
| 1549 | iscsilun->type = inq->periperal_device_type; |
| 1550 | scsi_free_scsi_task(task); |
| 1551 | task = NULL; |
| 1552 | |
Peter Lieven | 7191f20 | 2015-04-16 16:08:27 +0200 | [diff] [blame] | 1553 | iscsi_modesense_sync(iscsilun); |
Eric Blake | 4df863f | 2016-05-03 16:39:06 -0600 | [diff] [blame] | 1554 | if (iscsilun->dpofua) { |
| 1555 | bs->supported_write_flags = BDRV_REQ_FUA; |
| 1556 | } |
Eric Blake | 465fe88 | 2016-05-03 16:39:07 -0600 | [diff] [blame] | 1557 | bs->supported_zero_flags = BDRV_REQ_MAY_UNMAP; |
Peter Lieven | 7191f20 | 2015-04-16 16:08:27 +0200 | [diff] [blame] | 1558 | |
Fam Zheng | c1d4096 | 2014-10-30 19:23:46 +0800 | [diff] [blame] | 1559 | /* Check the write protect flag of the LUN if we want to write */ |
| 1560 | if (iscsilun->type == TYPE_DISK && (flags & BDRV_O_RDWR) && |
Fam Zheng | 43ae8fb | 2015-02-25 12:40:08 +0800 | [diff] [blame] | 1561 | iscsilun->write_protected) { |
Fam Zheng | c1d4096 | 2014-10-30 19:23:46 +0800 | [diff] [blame] | 1562 | error_setg(errp, "Cannot open a write protected LUN as read-write"); |
| 1563 | ret = -EACCES; |
| 1564 | goto out; |
| 1565 | } |
| 1566 | |
Paolo Bonzini | f291785 | 2014-02-17 14:43:53 +0100 | [diff] [blame] | 1567 | iscsi_readcapacity_sync(iscsilun, &local_err); |
| 1568 | if (local_err != NULL) { |
| 1569 | error_propagate(errp, local_err); |
Fam Zheng | cd82b6f | 2014-04-10 09:33:55 +0800 | [diff] [blame] | 1570 | ret = -EINVAL; |
Peter Lieven | cb1b83e | 2013-02-18 14:50:46 +0100 | [diff] [blame] | 1571 | goto out; |
Peter Lieven | e829b0b | 2012-11-17 14:37:39 +0100 | [diff] [blame] | 1572 | } |
Peter Lieven | 0777b5d | 2013-07-11 14:16:25 +0200 | [diff] [blame] | 1573 | bs->total_sectors = sector_lun2qemu(iscsilun->num_blocks, iscsilun); |
Paolo Bonzini | 2c9880c | 2011-11-29 12:41:35 +0100 | [diff] [blame] | 1574 | bs->request_alignment = iscsilun->block_size; |
Peter Lieven | e829b0b | 2012-11-17 14:37:39 +0100 | [diff] [blame] | 1575 | |
Kevin Wolf | f47c3f5 | 2014-03-05 15:45:00 +0100 | [diff] [blame] | 1576 | /* We don't have any emulation for devices other than disks and CD-ROMs, so |
| 1577 | * this must be sg ioctl compatible. We force it to be sg, otherwise qemu |
| 1578 | * will try to read from the device to guess the image format. |
Ronnie Sahlberg | 622695a | 2012-05-26 14:56:41 +1000 | [diff] [blame] | 1579 | */ |
Kevin Wolf | f47c3f5 | 2014-03-05 15:45:00 +0100 | [diff] [blame] | 1580 | if (iscsilun->type != TYPE_DISK && iscsilun->type != TYPE_ROM) { |
Ronnie Sahlberg | 622695a | 2012-05-26 14:56:41 +1000 | [diff] [blame] | 1581 | bs->sg = 1; |
| 1582 | } |
| 1583 | |
Peter Lieven | 24d3bd6 | 2014-02-17 18:34:08 +0100 | [diff] [blame] | 1584 | task = iscsi_do_inquiry(iscsilun->iscsi, iscsilun->lun, 1, |
| 1585 | SCSI_INQUIRY_PAGECODE_SUPPORTED_VPD_PAGES, |
| 1586 | (void **) &inq_vpd, errp); |
| 1587 | if (task == NULL) { |
| 1588 | ret = -EINVAL; |
| 1589 | goto out; |
| 1590 | } |
| 1591 | for (i = 0; i < inq_vpd->num_pages; i++) { |
| 1592 | struct scsi_task *inq_task; |
Peter Lieven | f18a7cb | 2013-07-19 09:19:39 +0200 | [diff] [blame] | 1593 | struct scsi_inquiry_logical_block_provisioning *inq_lbp; |
Peter Lieven | f18a7cb | 2013-07-19 09:19:39 +0200 | [diff] [blame] | 1594 | struct scsi_inquiry_block_limits *inq_bl; |
Peter Lieven | 24d3bd6 | 2014-02-17 18:34:08 +0100 | [diff] [blame] | 1595 | switch (inq_vpd->pages[i]) { |
| 1596 | case SCSI_INQUIRY_PAGECODE_LOGICAL_BLOCK_PROVISIONING: |
| 1597 | inq_task = iscsi_do_inquiry(iscsilun->iscsi, iscsilun->lun, 1, |
| 1598 | SCSI_INQUIRY_PAGECODE_LOGICAL_BLOCK_PROVISIONING, |
| 1599 | (void **) &inq_lbp, errp); |
| 1600 | if (inq_task == NULL) { |
| 1601 | ret = -EINVAL; |
| 1602 | goto out; |
| 1603 | } |
| 1604 | memcpy(&iscsilun->lbp, inq_lbp, |
| 1605 | sizeof(struct scsi_inquiry_logical_block_provisioning)); |
| 1606 | scsi_free_scsi_task(inq_task); |
| 1607 | break; |
| 1608 | case SCSI_INQUIRY_PAGECODE_BLOCK_LIMITS: |
| 1609 | inq_task = iscsi_do_inquiry(iscsilun->iscsi, iscsilun->lun, 1, |
| 1610 | SCSI_INQUIRY_PAGECODE_BLOCK_LIMITS, |
| 1611 | (void **) &inq_bl, errp); |
| 1612 | if (inq_task == NULL) { |
| 1613 | ret = -EINVAL; |
| 1614 | goto out; |
| 1615 | } |
| 1616 | memcpy(&iscsilun->bl, inq_bl, |
| 1617 | sizeof(struct scsi_inquiry_block_limits)); |
| 1618 | scsi_free_scsi_task(inq_task); |
| 1619 | break; |
| 1620 | default: |
| 1621 | break; |
Peter Lieven | f18a7cb | 2013-07-19 09:19:39 +0200 | [diff] [blame] | 1622 | } |
Peter Lieven | f18a7cb | 2013-07-19 09:19:39 +0200 | [diff] [blame] | 1623 | } |
Peter Lieven | 24d3bd6 | 2014-02-17 18:34:08 +0100 | [diff] [blame] | 1624 | scsi_free_scsi_task(task); |
| 1625 | task = NULL; |
Peter Lieven | f18a7cb | 2013-07-19 09:19:39 +0200 | [diff] [blame] | 1626 | |
Stefan Hajnoczi | 80cf625 | 2014-05-08 16:34:42 +0200 | [diff] [blame] | 1627 | iscsi_attach_aio_context(bs, iscsilun->aio_context); |
Peter Lieven | 5b5d34e | 2012-12-06 10:46:47 +0100 | [diff] [blame] | 1628 | |
Peter Lieven | b03c380 | 2014-04-28 13:11:32 +0200 | [diff] [blame] | 1629 | /* Guess the internal cluster (page) size of the iscsi target by the means |
| 1630 | * of opt_unmap_gran. Transfer the unmap granularity only if it has a |
| 1631 | * reasonable size */ |
Peter Lieven | 3d2acaa | 2014-04-28 17:18:32 +0200 | [diff] [blame] | 1632 | if (iscsilun->bl.opt_unmap_gran * iscsilun->block_size >= 4 * 1024 && |
Peter Lieven | b03c380 | 2014-04-28 13:11:32 +0200 | [diff] [blame] | 1633 | iscsilun->bl.opt_unmap_gran * iscsilun->block_size <= 16 * 1024 * 1024) { |
| 1634 | iscsilun->cluster_sectors = (iscsilun->bl.opt_unmap_gran * |
| 1635 | iscsilun->block_size) >> BDRV_SECTOR_BITS; |
Peter Lieven | 9eac362 | 2015-04-16 16:08:33 +0200 | [diff] [blame] | 1636 | if (iscsilun->lbprz) { |
Peter Lieven | a9fe4c9 | 2014-09-30 09:09:12 +0200 | [diff] [blame] | 1637 | iscsilun->allocationmap = iscsi_allocationmap_init(iscsilun); |
| 1638 | if (iscsilun->allocationmap == NULL) { |
| 1639 | ret = -ENOMEM; |
| 1640 | } |
Peter Lieven | b03c380 | 2014-04-28 13:11:32 +0200 | [diff] [blame] | 1641 | } |
Peter Lieven | b03c380 | 2014-04-28 13:11:32 +0200 | [diff] [blame] | 1642 | } |
| 1643 | |
Paolo Bonzini | b93c94f | 2012-08-06 10:52:22 +0200 | [diff] [blame] | 1644 | out: |
Kevin Wolf | 60beb34 | 2013-04-12 17:59:59 +0200 | [diff] [blame] | 1645 | qemu_opts_del(opts); |
Markus Armbruster | f7047c2 | 2014-06-06 18:25:12 +0200 | [diff] [blame] | 1646 | g_free(initiator_name); |
Ronnie Sahlberg | c589b24 | 2011-10-25 19:24:24 +1100 | [diff] [blame] | 1647 | if (iscsi_url != NULL) { |
| 1648 | iscsi_destroy_url(iscsi_url); |
| 1649 | } |
Peter Lieven | e829b0b | 2012-11-17 14:37:39 +0100 | [diff] [blame] | 1650 | if (task != NULL) { |
| 1651 | scsi_free_scsi_task(task); |
| 1652 | } |
Paolo Bonzini | b93c94f | 2012-08-06 10:52:22 +0200 | [diff] [blame] | 1653 | |
| 1654 | if (ret) { |
| 1655 | if (iscsi != NULL) { |
Peter Lieven | 20474e9 | 2015-04-16 16:08:25 +0200 | [diff] [blame] | 1656 | if (iscsi_is_logged_in(iscsi)) { |
| 1657 | iscsi_logout_sync(iscsi); |
| 1658 | } |
Paolo Bonzini | b93c94f | 2012-08-06 10:52:22 +0200 | [diff] [blame] | 1659 | iscsi_destroy_context(iscsi); |
| 1660 | } |
| 1661 | memset(iscsilun, 0, sizeof(IscsiLun)); |
Ronnie Sahlberg | c589b24 | 2011-10-25 19:24:24 +1100 | [diff] [blame] | 1662 | } |
Ronnie Sahlberg | c589b24 | 2011-10-25 19:24:24 +1100 | [diff] [blame] | 1663 | return ret; |
| 1664 | } |
| 1665 | |
| 1666 | static void iscsi_close(BlockDriverState *bs) |
| 1667 | { |
| 1668 | IscsiLun *iscsilun = bs->opaque; |
| 1669 | struct iscsi_context *iscsi = iscsilun->iscsi; |
| 1670 | |
Stefan Hajnoczi | 80cf625 | 2014-05-08 16:34:42 +0200 | [diff] [blame] | 1671 | iscsi_detach_aio_context(bs); |
Peter Lieven | 20474e9 | 2015-04-16 16:08:25 +0200 | [diff] [blame] | 1672 | if (iscsi_is_logged_in(iscsi)) { |
| 1673 | iscsi_logout_sync(iscsi); |
| 1674 | } |
Ronnie Sahlberg | c589b24 | 2011-10-25 19:24:24 +1100 | [diff] [blame] | 1675 | iscsi_destroy_context(iscsi); |
Peter Lieven | d4cd961 | 2013-10-24 12:07:02 +0200 | [diff] [blame] | 1676 | g_free(iscsilun->zeroblock); |
Peter Lieven | b03c380 | 2014-04-28 13:11:32 +0200 | [diff] [blame] | 1677 | g_free(iscsilun->allocationmap); |
Ronnie Sahlberg | c589b24 | 2011-10-25 19:24:24 +1100 | [diff] [blame] | 1678 | memset(iscsilun, 0, sizeof(IscsiLun)); |
| 1679 | } |
| 1680 | |
Peter Lieven | 52f6fa1 | 2014-10-27 10:18:45 +0100 | [diff] [blame] | 1681 | static int sector_limits_lun2qemu(int64_t sector, IscsiLun *iscsilun) |
| 1682 | { |
| 1683 | return MIN(sector_lun2qemu(sector, iscsilun), INT_MAX / 2 + 1); |
| 1684 | } |
| 1685 | |
Kevin Wolf | 3baca89 | 2014-07-16 17:48:16 +0200 | [diff] [blame] | 1686 | static void iscsi_refresh_limits(BlockDriverState *bs, Error **errp) |
Kevin Wolf | d34682c | 2013-12-11 19:26:16 +0100 | [diff] [blame] | 1687 | { |
Kevin Wolf | d34682c | 2013-12-11 19:26:16 +0100 | [diff] [blame] | 1688 | /* We don't actually refresh here, but just return data queried in |
| 1689 | * iscsi_open(): iscsi targets don't change their limits. */ |
Peter Lieven | 52f6fa1 | 2014-10-27 10:18:45 +0100 | [diff] [blame] | 1690 | |
| 1691 | IscsiLun *iscsilun = bs->opaque; |
| 1692 | uint32_t max_xfer_len = iscsilun->use_16_for_rw ? 0xffffffff : 0xffff; |
| 1693 | |
| 1694 | if (iscsilun->bl.max_xfer_len) { |
| 1695 | max_xfer_len = MIN(max_xfer_len, iscsilun->bl.max_xfer_len); |
| 1696 | } |
| 1697 | |
| 1698 | bs->bl.max_transfer_length = sector_limits_lun2qemu(max_xfer_len, iscsilun); |
| 1699 | |
Paolo Bonzini | c97ca29 | 2014-04-02 15:30:29 +0200 | [diff] [blame] | 1700 | if (iscsilun->lbp.lbpu) { |
Kevin Wolf | d34682c | 2013-12-11 19:26:16 +0100 | [diff] [blame] | 1701 | if (iscsilun->bl.max_unmap < 0xffffffff) { |
Peter Lieven | 3dab155 | 2014-10-27 10:18:47 +0100 | [diff] [blame] | 1702 | bs->bl.max_discard = |
| 1703 | sector_limits_lun2qemu(iscsilun->bl.max_unmap, iscsilun); |
Kevin Wolf | d34682c | 2013-12-11 19:26:16 +0100 | [diff] [blame] | 1704 | } |
Peter Lieven | 3dab155 | 2014-10-27 10:18:47 +0100 | [diff] [blame] | 1705 | bs->bl.discard_alignment = |
| 1706 | sector_limits_lun2qemu(iscsilun->bl.opt_unmap_gran, iscsilun); |
Paolo Bonzini | c97ca29 | 2014-04-02 15:30:29 +0200 | [diff] [blame] | 1707 | } |
Kevin Wolf | d34682c | 2013-12-11 19:26:16 +0100 | [diff] [blame] | 1708 | |
Paolo Bonzini | c97ca29 | 2014-04-02 15:30:29 +0200 | [diff] [blame] | 1709 | if (iscsilun->bl.max_ws_len < 0xffffffff) { |
Peter Lieven | 3dab155 | 2014-10-27 10:18:47 +0100 | [diff] [blame] | 1710 | bs->bl.max_write_zeroes = |
| 1711 | sector_limits_lun2qemu(iscsilun->bl.max_ws_len, iscsilun); |
Paolo Bonzini | c97ca29 | 2014-04-02 15:30:29 +0200 | [diff] [blame] | 1712 | } |
| 1713 | if (iscsilun->lbp.lbpws) { |
Peter Lieven | 3dab155 | 2014-10-27 10:18:47 +0100 | [diff] [blame] | 1714 | bs->bl.write_zeroes_alignment = |
| 1715 | sector_limits_lun2qemu(iscsilun->bl.opt_unmap_gran, iscsilun); |
Kevin Wolf | d34682c | 2013-12-11 19:26:16 +0100 | [diff] [blame] | 1716 | } |
Peter Lieven | 3dab155 | 2014-10-27 10:18:47 +0100 | [diff] [blame] | 1717 | bs->bl.opt_transfer_length = |
| 1718 | sector_limits_lun2qemu(iscsilun->bl.opt_xfer_len, iscsilun); |
Anthony Liguori | e9f526a | 2014-01-24 15:50:14 -0800 | [diff] [blame] | 1719 | } |
Kevin Wolf | d34682c | 2013-12-11 19:26:16 +0100 | [diff] [blame] | 1720 | |
Fam Zheng | 43ae8fb | 2015-02-25 12:40:08 +0800 | [diff] [blame] | 1721 | /* Note that this will not re-establish a connection with an iSCSI target - it |
| 1722 | * is effectively a NOP. */ |
Jeff Cody | dc6afb9 | 2014-01-14 13:10:24 -0500 | [diff] [blame] | 1723 | static int iscsi_reopen_prepare(BDRVReopenState *state, |
| 1724 | BlockReopenQueue *queue, Error **errp) |
| 1725 | { |
Fam Zheng | 43ae8fb | 2015-02-25 12:40:08 +0800 | [diff] [blame] | 1726 | IscsiLun *iscsilun = state->bs->opaque; |
| 1727 | |
| 1728 | if (state->flags & BDRV_O_RDWR && iscsilun->write_protected) { |
| 1729 | error_setg(errp, "Cannot open a write protected LUN as read-write"); |
| 1730 | return -EACCES; |
| 1731 | } |
Kevin Wolf | d34682c | 2013-12-11 19:26:16 +0100 | [diff] [blame] | 1732 | return 0; |
| 1733 | } |
| 1734 | |
Peter Lieven | cb1b83e | 2013-02-18 14:50:46 +0100 | [diff] [blame] | 1735 | static int iscsi_truncate(BlockDriverState *bs, int64_t offset) |
| 1736 | { |
| 1737 | IscsiLun *iscsilun = bs->opaque; |
Paolo Bonzini | f291785 | 2014-02-17 14:43:53 +0100 | [diff] [blame] | 1738 | Error *local_err = NULL; |
Peter Lieven | cb1b83e | 2013-02-18 14:50:46 +0100 | [diff] [blame] | 1739 | |
| 1740 | if (iscsilun->type != TYPE_DISK) { |
| 1741 | return -ENOTSUP; |
| 1742 | } |
| 1743 | |
Paolo Bonzini | f291785 | 2014-02-17 14:43:53 +0100 | [diff] [blame] | 1744 | iscsi_readcapacity_sync(iscsilun, &local_err); |
| 1745 | if (local_err != NULL) { |
| 1746 | error_free(local_err); |
| 1747 | return -EIO; |
Peter Lieven | cb1b83e | 2013-02-18 14:50:46 +0100 | [diff] [blame] | 1748 | } |
| 1749 | |
| 1750 | if (offset > iscsi_getlength(bs)) { |
| 1751 | return -EINVAL; |
| 1752 | } |
| 1753 | |
Peter Lieven | b03c380 | 2014-04-28 13:11:32 +0200 | [diff] [blame] | 1754 | if (iscsilun->allocationmap != NULL) { |
| 1755 | g_free(iscsilun->allocationmap); |
Peter Lieven | a9fe4c9 | 2014-09-30 09:09:12 +0200 | [diff] [blame] | 1756 | iscsilun->allocationmap = iscsi_allocationmap_init(iscsilun); |
Peter Lieven | b03c380 | 2014-04-28 13:11:32 +0200 | [diff] [blame] | 1757 | } |
| 1758 | |
Peter Lieven | cb1b83e | 2013-02-18 14:50:46 +0100 | [diff] [blame] | 1759 | return 0; |
| 1760 | } |
| 1761 | |
Chunyan Liu | a59479e | 2014-06-05 17:20:55 +0800 | [diff] [blame] | 1762 | static int iscsi_create(const char *filename, QemuOpts *opts, Error **errp) |
Peter Lieven | de8864e | 2012-11-17 16:13:24 +0100 | [diff] [blame] | 1763 | { |
| 1764 | int ret = 0; |
| 1765 | int64_t total_size = 0; |
Fam Zheng | 13c91cb | 2013-08-23 09:14:45 +0800 | [diff] [blame] | 1766 | BlockDriverState *bs; |
Peter Lieven | de8864e | 2012-11-17 16:13:24 +0100 | [diff] [blame] | 1767 | IscsiLun *iscsilun = NULL; |
Kevin Wolf | 60beb34 | 2013-04-12 17:59:59 +0200 | [diff] [blame] | 1768 | QDict *bs_options; |
Peter Lieven | de8864e | 2012-11-17 16:13:24 +0100 | [diff] [blame] | 1769 | |
Markus Armbruster | e4e9986 | 2014-10-07 13:59:03 +0200 | [diff] [blame] | 1770 | bs = bdrv_new(); |
Peter Lieven | de8864e | 2012-11-17 16:13:24 +0100 | [diff] [blame] | 1771 | |
| 1772 | /* Read out options */ |
Hu Tao | c2eb918 | 2014-09-10 17:05:45 +0800 | [diff] [blame] | 1773 | total_size = DIV_ROUND_UP(qemu_opt_get_size_del(opts, BLOCK_OPT_SIZE, 0), |
| 1774 | BDRV_SECTOR_SIZE); |
Markus Armbruster | 5839e53 | 2014-08-19 10:31:08 +0200 | [diff] [blame] | 1775 | bs->opaque = g_new0(struct IscsiLun, 1); |
Fam Zheng | 13c91cb | 2013-08-23 09:14:45 +0800 | [diff] [blame] | 1776 | iscsilun = bs->opaque; |
Peter Lieven | de8864e | 2012-11-17 16:13:24 +0100 | [diff] [blame] | 1777 | |
Kevin Wolf | 60beb34 | 2013-04-12 17:59:59 +0200 | [diff] [blame] | 1778 | bs_options = qdict_new(); |
| 1779 | qdict_put(bs_options, "filename", qstring_from_str(filename)); |
Max Reitz | 015a103 | 2013-09-05 14:22:29 +0200 | [diff] [blame] | 1780 | ret = iscsi_open(bs, bs_options, 0, NULL); |
Kevin Wolf | 60beb34 | 2013-04-12 17:59:59 +0200 | [diff] [blame] | 1781 | QDECREF(bs_options); |
| 1782 | |
Peter Lieven | de8864e | 2012-11-17 16:13:24 +0100 | [diff] [blame] | 1783 | if (ret != 0) { |
| 1784 | goto out; |
| 1785 | } |
Stefan Hajnoczi | 80cf625 | 2014-05-08 16:34:42 +0200 | [diff] [blame] | 1786 | iscsi_detach_aio_context(bs); |
Peter Lieven | de8864e | 2012-11-17 16:13:24 +0100 | [diff] [blame] | 1787 | if (iscsilun->type != TYPE_DISK) { |
| 1788 | ret = -ENODEV; |
| 1789 | goto out; |
| 1790 | } |
Fam Zheng | 13c91cb | 2013-08-23 09:14:45 +0800 | [diff] [blame] | 1791 | if (bs->total_sectors < total_size) { |
Peter Lieven | de8864e | 2012-11-17 16:13:24 +0100 | [diff] [blame] | 1792 | ret = -ENOSPC; |
Peter Lieven | d3bda7b | 2013-07-11 14:16:24 +0200 | [diff] [blame] | 1793 | goto out; |
Peter Lieven | de8864e | 2012-11-17 16:13:24 +0100 | [diff] [blame] | 1794 | } |
| 1795 | |
| 1796 | ret = 0; |
| 1797 | out: |
| 1798 | if (iscsilun->iscsi != NULL) { |
| 1799 | iscsi_destroy_context(iscsilun->iscsi); |
| 1800 | } |
Fam Zheng | 13c91cb | 2013-08-23 09:14:45 +0800 | [diff] [blame] | 1801 | g_free(bs->opaque); |
| 1802 | bs->opaque = NULL; |
Fam Zheng | 4f6fd34 | 2013-08-23 09:14:47 +0800 | [diff] [blame] | 1803 | bdrv_unref(bs); |
Peter Lieven | de8864e | 2012-11-17 16:13:24 +0100 | [diff] [blame] | 1804 | return ret; |
| 1805 | } |
| 1806 | |
Peter Lieven | 186d4f2 | 2013-10-24 12:06:55 +0200 | [diff] [blame] | 1807 | static int iscsi_get_info(BlockDriverState *bs, BlockDriverInfo *bdi) |
| 1808 | { |
| 1809 | IscsiLun *iscsilun = bs->opaque; |
Peter Lieven | 0a386e4 | 2015-04-16 16:08:26 +0200 | [diff] [blame] | 1810 | bdi->unallocated_blocks_are_zero = iscsilun->lbprz; |
Peter Lieven | 186d4f2 | 2013-10-24 12:06:55 +0200 | [diff] [blame] | 1811 | bdi->can_write_zeroes_with_unmap = iscsilun->lbprz && iscsilun->lbp.lbpws; |
Peter Lieven | b03c380 | 2014-04-28 13:11:32 +0200 | [diff] [blame] | 1812 | bdi->cluster_size = iscsilun->cluster_sectors * BDRV_SECTOR_SIZE; |
Peter Lieven | 186d4f2 | 2013-10-24 12:06:55 +0200 | [diff] [blame] | 1813 | return 0; |
| 1814 | } |
| 1815 | |
Chunyan Liu | a59479e | 2014-06-05 17:20:55 +0800 | [diff] [blame] | 1816 | static QemuOptsList iscsi_create_opts = { |
| 1817 | .name = "iscsi-create-opts", |
| 1818 | .head = QTAILQ_HEAD_INITIALIZER(iscsi_create_opts.head), |
| 1819 | .desc = { |
| 1820 | { |
| 1821 | .name = BLOCK_OPT_SIZE, |
| 1822 | .type = QEMU_OPT_SIZE, |
| 1823 | .help = "Virtual disk size" |
| 1824 | }, |
| 1825 | { /* end of list */ } |
| 1826 | } |
Peter Lieven | de8864e | 2012-11-17 16:13:24 +0100 | [diff] [blame] | 1827 | }; |
| 1828 | |
Ronnie Sahlberg | c589b24 | 2011-10-25 19:24:24 +1100 | [diff] [blame] | 1829 | static BlockDriver bdrv_iscsi = { |
| 1830 | .format_name = "iscsi", |
| 1831 | .protocol_name = "iscsi", |
| 1832 | |
| 1833 | .instance_size = sizeof(IscsiLun), |
BenoƮt Canet | 030be32 | 2013-09-24 17:07:04 +0200 | [diff] [blame] | 1834 | .bdrv_needs_filename = true, |
Ronnie Sahlberg | c589b24 | 2011-10-25 19:24:24 +1100 | [diff] [blame] | 1835 | .bdrv_file_open = iscsi_open, |
| 1836 | .bdrv_close = iscsi_close, |
Chunyan Liu | c282e1f | 2014-06-05 17:21:11 +0800 | [diff] [blame] | 1837 | .bdrv_create = iscsi_create, |
Chunyan Liu | a59479e | 2014-06-05 17:20:55 +0800 | [diff] [blame] | 1838 | .create_opts = &iscsi_create_opts, |
Jeff Cody | dc6afb9 | 2014-01-14 13:10:24 -0500 | [diff] [blame] | 1839 | .bdrv_reopen_prepare = iscsi_reopen_prepare, |
Ronnie Sahlberg | c589b24 | 2011-10-25 19:24:24 +1100 | [diff] [blame] | 1840 | |
| 1841 | .bdrv_getlength = iscsi_getlength, |
Peter Lieven | 186d4f2 | 2013-10-24 12:06:55 +0200 | [diff] [blame] | 1842 | .bdrv_get_info = iscsi_get_info, |
Peter Lieven | cb1b83e | 2013-02-18 14:50:46 +0100 | [diff] [blame] | 1843 | .bdrv_truncate = iscsi_truncate, |
Kevin Wolf | d34682c | 2013-12-11 19:26:16 +0100 | [diff] [blame] | 1844 | .bdrv_refresh_limits = iscsi_refresh_limits, |
Ronnie Sahlberg | c589b24 | 2011-10-25 19:24:24 +1100 | [diff] [blame] | 1845 | |
Peter Lieven | 54a5c1d | 2013-07-19 09:19:40 +0200 | [diff] [blame] | 1846 | .bdrv_co_get_block_status = iscsi_co_get_block_status, |
Peter Lieven | 65f3e33 | 2013-07-19 09:19:41 +0200 | [diff] [blame] | 1847 | .bdrv_co_discard = iscsi_co_discard, |
Peter Lieven | d4cd961 | 2013-10-24 12:07:02 +0200 | [diff] [blame] | 1848 | .bdrv_co_write_zeroes = iscsi_co_write_zeroes, |
Peter Lieven | 063c337 | 2013-12-05 16:47:17 +0100 | [diff] [blame] | 1849 | .bdrv_co_readv = iscsi_co_readv, |
Kevin Wolf | 9f0eb9e | 2016-03-10 13:55:50 +0100 | [diff] [blame] | 1850 | .bdrv_co_writev_flags = iscsi_co_writev_flags, |
Peter Lieven | 063c337 | 2013-12-05 16:47:17 +0100 | [diff] [blame] | 1851 | .bdrv_co_flush_to_disk = iscsi_co_flush, |
Ronnie Sahlberg | fa6acb0 | 2012-04-24 16:29:04 +1000 | [diff] [blame] | 1852 | |
Ronnie Sahlberg | 9839245 | 2012-05-25 21:59:01 +1000 | [diff] [blame] | 1853 | #ifdef __linux__ |
Ronnie Sahlberg | 9839245 | 2012-05-25 21:59:01 +1000 | [diff] [blame] | 1854 | .bdrv_aio_ioctl = iscsi_aio_ioctl, |
| 1855 | #endif |
Stefan Hajnoczi | 80cf625 | 2014-05-08 16:34:42 +0200 | [diff] [blame] | 1856 | |
| 1857 | .bdrv_detach_aio_context = iscsi_detach_aio_context, |
| 1858 | .bdrv_attach_aio_context = iscsi_attach_aio_context, |
Ronnie Sahlberg | c589b24 | 2011-10-25 19:24:24 +1100 | [diff] [blame] | 1859 | }; |
| 1860 | |
Paolo Bonzini | 4d45457 | 2012-11-26 16:03:42 +0100 | [diff] [blame] | 1861 | static QemuOptsList qemu_iscsi_opts = { |
| 1862 | .name = "iscsi", |
| 1863 | .head = QTAILQ_HEAD_INITIALIZER(qemu_iscsi_opts.head), |
| 1864 | .desc = { |
| 1865 | { |
| 1866 | .name = "user", |
| 1867 | .type = QEMU_OPT_STRING, |
| 1868 | .help = "username for CHAP authentication to target", |
| 1869 | },{ |
| 1870 | .name = "password", |
| 1871 | .type = QEMU_OPT_STRING, |
| 1872 | .help = "password for CHAP authentication to target", |
| 1873 | },{ |
Daniel P. Berrange | b189346 | 2016-01-21 14:19:21 +0000 | [diff] [blame] | 1874 | .name = "password-secret", |
| 1875 | .type = QEMU_OPT_STRING, |
| 1876 | .help = "ID of the secret providing password for CHAP " |
| 1877 | "authentication to target", |
| 1878 | },{ |
Paolo Bonzini | 4d45457 | 2012-11-26 16:03:42 +0100 | [diff] [blame] | 1879 | .name = "header-digest", |
| 1880 | .type = QEMU_OPT_STRING, |
| 1881 | .help = "HeaderDigest setting. " |
| 1882 | "{CRC32C|CRC32C-NONE|NONE-CRC32C|NONE}", |
| 1883 | },{ |
| 1884 | .name = "initiator-name", |
| 1885 | .type = QEMU_OPT_STRING, |
| 1886 | .help = "Initiator iqn name to use when connecting", |
Peter Lieven | 5dd7a53 | 2015-06-16 13:45:07 +0200 | [diff] [blame] | 1887 | },{ |
| 1888 | .name = "timeout", |
| 1889 | .type = QEMU_OPT_NUMBER, |
| 1890 | .help = "Request timeout in seconds (default 0 = no timeout)", |
Paolo Bonzini | 4d45457 | 2012-11-26 16:03:42 +0100 | [diff] [blame] | 1891 | }, |
| 1892 | { /* end of list */ } |
| 1893 | }, |
| 1894 | }; |
| 1895 | |
Ronnie Sahlberg | c589b24 | 2011-10-25 19:24:24 +1100 | [diff] [blame] | 1896 | static void iscsi_block_init(void) |
| 1897 | { |
| 1898 | bdrv_register(&bdrv_iscsi); |
Paolo Bonzini | 4d45457 | 2012-11-26 16:03:42 +0100 | [diff] [blame] | 1899 | qemu_add_opts(&qemu_iscsi_opts); |
Ronnie Sahlberg | c589b24 | 2011-10-25 19:24:24 +1100 | [diff] [blame] | 1900 | } |
| 1901 | |
| 1902 | block_init(iscsi_block_init); |